virtio_pci.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. /*
  2. * Virtio PCI driver
  3. *
  4. * This module allows virtio devices to be used over a virtual PCI device.
  5. * This can be used with QEMU based VMMs like KVM or Xen.
  6. *
  7. * Copyright IBM Corp. 2007
  8. *
  9. * Authors:
  10. * Anthony Liguori <aliguori@us.ibm.com>
  11. *
  12. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  13. * See the COPYING file in the top-level directory.
  14. *
  15. */
  16. #include <linux/module.h>
  17. #include <linux/list.h>
  18. #include <linux/pci.h>
  19. #include <linux/slab.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/virtio.h>
  22. #include <linux/virtio_config.h>
  23. #include <linux/virtio_ring.h>
  24. #include <linux/virtio_pci.h>
  25. #include <linux/highmem.h>
  26. #include <linux/spinlock.h>
  27. MODULE_AUTHOR("Anthony Liguori <aliguori@us.ibm.com>");
  28. MODULE_DESCRIPTION("virtio-pci");
  29. MODULE_LICENSE("GPL");
  30. MODULE_VERSION("1");
  31. /* Our device structure */
  32. struct virtio_pci_device
  33. {
  34. struct virtio_device vdev;
  35. struct pci_dev *pci_dev;
  36. /* the IO mapping for the PCI config space */
  37. void __iomem *ioaddr;
  38. /* a list of queues so we can dispatch IRQs */
  39. spinlock_t lock;
  40. struct list_head virtqueues;
  41. /* MSI-X support */
  42. int msix_enabled;
  43. int intx_enabled;
  44. struct msix_entry *msix_entries;
  45. /* Name strings for interrupts. This size should be enough,
  46. * and I'm too lazy to allocate each name separately. */
  47. char (*msix_names)[256];
  48. /* Number of available vectors */
  49. unsigned msix_vectors;
  50. /* Vectors allocated, excluding per-vq vectors if any */
  51. unsigned msix_used_vectors;
  52. /* Status saved during hibernate/restore */
  53. u8 saved_status;
  54. /* Whether we have vector per vq */
  55. bool per_vq_vectors;
  56. };
  57. /* Constants for MSI-X */
  58. /* Use first vector for configuration changes, second and the rest for
  59. * virtqueues Thus, we need at least 2 vectors for MSI. */
  60. enum {
  61. VP_MSIX_CONFIG_VECTOR = 0,
  62. VP_MSIX_VQ_VECTOR = 1,
  63. };
  64. struct virtio_pci_vq_info
  65. {
  66. /* the actual virtqueue */
  67. struct virtqueue *vq;
  68. /* the number of entries in the queue */
  69. int num;
  70. /* the index of the queue */
  71. int queue_index;
  72. /* the virtual address of the ring queue */
  73. void *queue;
  74. /* the list node for the virtqueues list */
  75. struct list_head node;
  76. /* MSI-X vector (or none) */
  77. unsigned msix_vector;
  78. };
  79. /* Qumranet donated their vendor ID for devices 0x1000 thru 0x10FF. */
  80. static struct pci_device_id virtio_pci_id_table[] = {
  81. { 0x1af4, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  82. { 0 },
  83. };
  84. MODULE_DEVICE_TABLE(pci, virtio_pci_id_table);
  85. /* Convert a generic virtio device to our structure */
  86. static struct virtio_pci_device *to_vp_device(struct virtio_device *vdev)
  87. {
  88. return container_of(vdev, struct virtio_pci_device, vdev);
  89. }
  90. /* virtio config->get_features() implementation */
  91. static u32 vp_get_features(struct virtio_device *vdev)
  92. {
  93. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  94. /* When someone needs more than 32 feature bits, we'll need to
  95. * steal a bit to indicate that the rest are somewhere else. */
  96. return ioread32(vp_dev->ioaddr + VIRTIO_PCI_HOST_FEATURES);
  97. }
  98. /* virtio config->finalize_features() implementation */
  99. static void vp_finalize_features(struct virtio_device *vdev)
  100. {
  101. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  102. /* Give virtio_ring a chance to accept features. */
  103. vring_transport_features(vdev);
  104. /* We only support 32 feature bits. */
  105. BUILD_BUG_ON(ARRAY_SIZE(vdev->features) != 1);
  106. iowrite32(vdev->features[0], vp_dev->ioaddr+VIRTIO_PCI_GUEST_FEATURES);
  107. }
  108. /* virtio config->get() implementation */
  109. static void vp_get(struct virtio_device *vdev, unsigned offset,
  110. void *buf, unsigned len)
  111. {
  112. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  113. void __iomem *ioaddr = vp_dev->ioaddr +
  114. VIRTIO_PCI_CONFIG(vp_dev) + offset;
  115. u8 *ptr = buf;
  116. int i;
  117. for (i = 0; i < len; i++)
  118. ptr[i] = ioread8(ioaddr + i);
  119. }
  120. /* the config->set() implementation. it's symmetric to the config->get()
  121. * implementation */
  122. static void vp_set(struct virtio_device *vdev, unsigned offset,
  123. const void *buf, unsigned len)
  124. {
  125. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  126. void __iomem *ioaddr = vp_dev->ioaddr +
  127. VIRTIO_PCI_CONFIG(vp_dev) + offset;
  128. const u8 *ptr = buf;
  129. int i;
  130. for (i = 0; i < len; i++)
  131. iowrite8(ptr[i], ioaddr + i);
  132. }
  133. /* config->{get,set}_status() implementations */
  134. static u8 vp_get_status(struct virtio_device *vdev)
  135. {
  136. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  137. return ioread8(vp_dev->ioaddr + VIRTIO_PCI_STATUS);
  138. }
  139. static void vp_set_status(struct virtio_device *vdev, u8 status)
  140. {
  141. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  142. /* We should never be setting status to 0. */
  143. BUG_ON(status == 0);
  144. iowrite8(status, vp_dev->ioaddr + VIRTIO_PCI_STATUS);
  145. }
  146. /* wait for pending irq handlers */
  147. static void vp_synchronize_vectors(struct virtio_device *vdev)
  148. {
  149. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  150. int i;
  151. if (vp_dev->intx_enabled)
  152. synchronize_irq(vp_dev->pci_dev->irq);
  153. for (i = 0; i < vp_dev->msix_vectors; ++i)
  154. synchronize_irq(vp_dev->msix_entries[i].vector);
  155. }
  156. static void vp_reset(struct virtio_device *vdev)
  157. {
  158. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  159. /* 0 status means a reset. */
  160. iowrite8(0, vp_dev->ioaddr + VIRTIO_PCI_STATUS);
  161. /* Flush out the status write, and flush in device writes,
  162. * including MSi-X interrupts, if any. */
  163. ioread8(vp_dev->ioaddr + VIRTIO_PCI_STATUS);
  164. /* Flush pending VQ/configuration callbacks. */
  165. vp_synchronize_vectors(vdev);
  166. }
  167. /* the notify function used when creating a virt queue */
  168. static void vp_notify(struct virtqueue *vq)
  169. {
  170. struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
  171. struct virtio_pci_vq_info *info = vq->priv;
  172. /* we write the queue's selector into the notification register to
  173. * signal the other end */
  174. iowrite16(info->queue_index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NOTIFY);
  175. }
  176. /* Handle a configuration change: Tell driver if it wants to know. */
  177. static irqreturn_t vp_config_changed(int irq, void *opaque)
  178. {
  179. struct virtio_pci_device *vp_dev = opaque;
  180. struct virtio_driver *drv;
  181. drv = container_of(vp_dev->vdev.dev.driver,
  182. struct virtio_driver, driver);
  183. if (drv && drv->config_changed)
  184. drv->config_changed(&vp_dev->vdev);
  185. return IRQ_HANDLED;
  186. }
  187. /* Notify all virtqueues on an interrupt. */
  188. static irqreturn_t vp_vring_interrupt(int irq, void *opaque)
  189. {
  190. struct virtio_pci_device *vp_dev = opaque;
  191. struct virtio_pci_vq_info *info;
  192. irqreturn_t ret = IRQ_NONE;
  193. unsigned long flags;
  194. spin_lock_irqsave(&vp_dev->lock, flags);
  195. list_for_each_entry(info, &vp_dev->virtqueues, node) {
  196. if (vring_interrupt(irq, info->vq) == IRQ_HANDLED)
  197. ret = IRQ_HANDLED;
  198. }
  199. spin_unlock_irqrestore(&vp_dev->lock, flags);
  200. return ret;
  201. }
  202. /* A small wrapper to also acknowledge the interrupt when it's handled.
  203. * I really need an EIO hook for the vring so I can ack the interrupt once we
  204. * know that we'll be handling the IRQ but before we invoke the callback since
  205. * the callback may notify the host which results in the host attempting to
  206. * raise an interrupt that we would then mask once we acknowledged the
  207. * interrupt. */
  208. static irqreturn_t vp_interrupt(int irq, void *opaque)
  209. {
  210. struct virtio_pci_device *vp_dev = opaque;
  211. u8 isr;
  212. /* reading the ISR has the effect of also clearing it so it's very
  213. * important to save off the value. */
  214. isr = ioread8(vp_dev->ioaddr + VIRTIO_PCI_ISR);
  215. /* It's definitely not us if the ISR was not high */
  216. if (!isr)
  217. return IRQ_NONE;
  218. /* Configuration change? Tell driver if it wants to know. */
  219. if (isr & VIRTIO_PCI_ISR_CONFIG)
  220. vp_config_changed(irq, opaque);
  221. return vp_vring_interrupt(irq, opaque);
  222. }
  223. static void vp_free_vectors(struct virtio_device *vdev)
  224. {
  225. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  226. int i;
  227. if (vp_dev->intx_enabled) {
  228. free_irq(vp_dev->pci_dev->irq, vp_dev);
  229. vp_dev->intx_enabled = 0;
  230. }
  231. for (i = 0; i < vp_dev->msix_used_vectors; ++i)
  232. free_irq(vp_dev->msix_entries[i].vector, vp_dev);
  233. if (vp_dev->msix_enabled) {
  234. /* Disable the vector used for configuration */
  235. iowrite16(VIRTIO_MSI_NO_VECTOR,
  236. vp_dev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR);
  237. /* Flush the write out to device */
  238. ioread16(vp_dev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR);
  239. pci_disable_msix(vp_dev->pci_dev);
  240. vp_dev->msix_enabled = 0;
  241. vp_dev->msix_vectors = 0;
  242. }
  243. vp_dev->msix_used_vectors = 0;
  244. kfree(vp_dev->msix_names);
  245. vp_dev->msix_names = NULL;
  246. kfree(vp_dev->msix_entries);
  247. vp_dev->msix_entries = NULL;
  248. }
  249. static int vp_request_msix_vectors(struct virtio_device *vdev, int nvectors,
  250. bool per_vq_vectors)
  251. {
  252. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  253. const char *name = dev_name(&vp_dev->vdev.dev);
  254. unsigned i, v;
  255. int err = -ENOMEM;
  256. vp_dev->msix_entries = kmalloc(nvectors * sizeof *vp_dev->msix_entries,
  257. GFP_KERNEL);
  258. if (!vp_dev->msix_entries)
  259. goto error;
  260. vp_dev->msix_names = kmalloc(nvectors * sizeof *vp_dev->msix_names,
  261. GFP_KERNEL);
  262. if (!vp_dev->msix_names)
  263. goto error;
  264. for (i = 0; i < nvectors; ++i)
  265. vp_dev->msix_entries[i].entry = i;
  266. /* pci_enable_msix returns positive if we can't get this many. */
  267. err = pci_enable_msix(vp_dev->pci_dev, vp_dev->msix_entries, nvectors);
  268. if (err > 0)
  269. err = -ENOSPC;
  270. if (err)
  271. goto error;
  272. vp_dev->msix_vectors = nvectors;
  273. vp_dev->msix_enabled = 1;
  274. /* Set the vector used for configuration */
  275. v = vp_dev->msix_used_vectors;
  276. snprintf(vp_dev->msix_names[v], sizeof *vp_dev->msix_names,
  277. "%s-config", name);
  278. err = request_irq(vp_dev->msix_entries[v].vector,
  279. vp_config_changed, 0, vp_dev->msix_names[v],
  280. vp_dev);
  281. if (err)
  282. goto error;
  283. ++vp_dev->msix_used_vectors;
  284. iowrite16(v, vp_dev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR);
  285. /* Verify we had enough resources to assign the vector */
  286. v = ioread16(vp_dev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR);
  287. if (v == VIRTIO_MSI_NO_VECTOR) {
  288. err = -EBUSY;
  289. goto error;
  290. }
  291. if (!per_vq_vectors) {
  292. /* Shared vector for all VQs */
  293. v = vp_dev->msix_used_vectors;
  294. snprintf(vp_dev->msix_names[v], sizeof *vp_dev->msix_names,
  295. "%s-virtqueues", name);
  296. err = request_irq(vp_dev->msix_entries[v].vector,
  297. vp_vring_interrupt, 0, vp_dev->msix_names[v],
  298. vp_dev);
  299. if (err)
  300. goto error;
  301. ++vp_dev->msix_used_vectors;
  302. }
  303. return 0;
  304. error:
  305. vp_free_vectors(vdev);
  306. return err;
  307. }
  308. static int vp_request_intx(struct virtio_device *vdev)
  309. {
  310. int err;
  311. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  312. err = request_irq(vp_dev->pci_dev->irq, vp_interrupt,
  313. IRQF_SHARED, dev_name(&vdev->dev), vp_dev);
  314. if (!err)
  315. vp_dev->intx_enabled = 1;
  316. return err;
  317. }
  318. static struct virtqueue *setup_vq(struct virtio_device *vdev, unsigned index,
  319. void (*callback)(struct virtqueue *vq),
  320. const char *name,
  321. u16 msix_vec)
  322. {
  323. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  324. struct virtio_pci_vq_info *info;
  325. struct virtqueue *vq;
  326. unsigned long flags, size;
  327. u16 num;
  328. int err;
  329. /* Select the queue we're interested in */
  330. iowrite16(index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL);
  331. /* Check if queue is either not available or already active. */
  332. num = ioread16(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NUM);
  333. if (!num || ioread32(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN))
  334. return ERR_PTR(-ENOENT);
  335. /* allocate and fill out our structure the represents an active
  336. * queue */
  337. info = kmalloc(sizeof(struct virtio_pci_vq_info), GFP_KERNEL);
  338. if (!info)
  339. return ERR_PTR(-ENOMEM);
  340. info->queue_index = index;
  341. info->num = num;
  342. info->msix_vector = msix_vec;
  343. size = PAGE_ALIGN(vring_size(num, VIRTIO_PCI_VRING_ALIGN));
  344. info->queue = alloc_pages_exact(size, GFP_KERNEL|__GFP_ZERO);
  345. if (info->queue == NULL) {
  346. err = -ENOMEM;
  347. goto out_info;
  348. }
  349. /* activate the queue */
  350. iowrite32(virt_to_phys(info->queue) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT,
  351. vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
  352. /* create the vring */
  353. vq = vring_new_virtqueue(info->num, VIRTIO_PCI_VRING_ALIGN, vdev,
  354. true, info->queue, vp_notify, callback, name);
  355. if (!vq) {
  356. err = -ENOMEM;
  357. goto out_activate_queue;
  358. }
  359. vq->priv = info;
  360. info->vq = vq;
  361. if (msix_vec != VIRTIO_MSI_NO_VECTOR) {
  362. iowrite16(msix_vec, vp_dev->ioaddr + VIRTIO_MSI_QUEUE_VECTOR);
  363. msix_vec = ioread16(vp_dev->ioaddr + VIRTIO_MSI_QUEUE_VECTOR);
  364. if (msix_vec == VIRTIO_MSI_NO_VECTOR) {
  365. err = -EBUSY;
  366. goto out_assign;
  367. }
  368. }
  369. if (callback) {
  370. spin_lock_irqsave(&vp_dev->lock, flags);
  371. list_add(&info->node, &vp_dev->virtqueues);
  372. spin_unlock_irqrestore(&vp_dev->lock, flags);
  373. } else {
  374. INIT_LIST_HEAD(&info->node);
  375. }
  376. return vq;
  377. out_assign:
  378. vring_del_virtqueue(vq);
  379. out_activate_queue:
  380. iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
  381. free_pages_exact(info->queue, size);
  382. out_info:
  383. kfree(info);
  384. return ERR_PTR(err);
  385. }
  386. static void vp_del_vq(struct virtqueue *vq)
  387. {
  388. struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
  389. struct virtio_pci_vq_info *info = vq->priv;
  390. unsigned long flags, size;
  391. spin_lock_irqsave(&vp_dev->lock, flags);
  392. list_del(&info->node);
  393. spin_unlock_irqrestore(&vp_dev->lock, flags);
  394. iowrite16(info->queue_index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL);
  395. if (vp_dev->msix_enabled) {
  396. iowrite16(VIRTIO_MSI_NO_VECTOR,
  397. vp_dev->ioaddr + VIRTIO_MSI_QUEUE_VECTOR);
  398. /* Flush the write out to device */
  399. ioread8(vp_dev->ioaddr + VIRTIO_PCI_ISR);
  400. }
  401. vring_del_virtqueue(vq);
  402. /* Select and deactivate the queue */
  403. iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
  404. size = PAGE_ALIGN(vring_size(info->num, VIRTIO_PCI_VRING_ALIGN));
  405. free_pages_exact(info->queue, size);
  406. kfree(info);
  407. }
  408. /* the config->del_vqs() implementation */
  409. static void vp_del_vqs(struct virtio_device *vdev)
  410. {
  411. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  412. struct virtqueue *vq, *n;
  413. struct virtio_pci_vq_info *info;
  414. list_for_each_entry_safe(vq, n, &vdev->vqs, list) {
  415. info = vq->priv;
  416. if (vp_dev->per_vq_vectors &&
  417. info->msix_vector != VIRTIO_MSI_NO_VECTOR)
  418. free_irq(vp_dev->msix_entries[info->msix_vector].vector,
  419. vq);
  420. vp_del_vq(vq);
  421. }
  422. vp_dev->per_vq_vectors = false;
  423. vp_free_vectors(vdev);
  424. }
  425. static int vp_try_to_find_vqs(struct virtio_device *vdev, unsigned nvqs,
  426. struct virtqueue *vqs[],
  427. vq_callback_t *callbacks[],
  428. const char *names[],
  429. bool use_msix,
  430. bool per_vq_vectors)
  431. {
  432. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  433. u16 msix_vec;
  434. int i, err, nvectors, allocated_vectors;
  435. if (!use_msix) {
  436. /* Old style: one normal interrupt for change and all vqs. */
  437. err = vp_request_intx(vdev);
  438. if (err)
  439. goto error_request;
  440. } else {
  441. if (per_vq_vectors) {
  442. /* Best option: one for change interrupt, one per vq. */
  443. nvectors = 1;
  444. for (i = 0; i < nvqs; ++i)
  445. if (callbacks[i])
  446. ++nvectors;
  447. } else {
  448. /* Second best: one for change, shared for all vqs. */
  449. nvectors = 2;
  450. }
  451. err = vp_request_msix_vectors(vdev, nvectors, per_vq_vectors);
  452. if (err)
  453. goto error_request;
  454. }
  455. vp_dev->per_vq_vectors = per_vq_vectors;
  456. allocated_vectors = vp_dev->msix_used_vectors;
  457. for (i = 0; i < nvqs; ++i) {
  458. if (!callbacks[i] || !vp_dev->msix_enabled)
  459. msix_vec = VIRTIO_MSI_NO_VECTOR;
  460. else if (vp_dev->per_vq_vectors)
  461. msix_vec = allocated_vectors++;
  462. else
  463. msix_vec = VP_MSIX_VQ_VECTOR;
  464. vqs[i] = setup_vq(vdev, i, callbacks[i], names[i], msix_vec);
  465. if (IS_ERR(vqs[i])) {
  466. err = PTR_ERR(vqs[i]);
  467. goto error_find;
  468. }
  469. if (!vp_dev->per_vq_vectors || msix_vec == VIRTIO_MSI_NO_VECTOR)
  470. continue;
  471. /* allocate per-vq irq if available and necessary */
  472. snprintf(vp_dev->msix_names[msix_vec],
  473. sizeof *vp_dev->msix_names,
  474. "%s-%s",
  475. dev_name(&vp_dev->vdev.dev), names[i]);
  476. err = request_irq(vp_dev->msix_entries[msix_vec].vector,
  477. vring_interrupt, 0,
  478. vp_dev->msix_names[msix_vec],
  479. vqs[i]);
  480. if (err) {
  481. vp_del_vq(vqs[i]);
  482. goto error_find;
  483. }
  484. }
  485. return 0;
  486. error_find:
  487. vp_del_vqs(vdev);
  488. error_request:
  489. return err;
  490. }
  491. /* the config->find_vqs() implementation */
  492. static int vp_find_vqs(struct virtio_device *vdev, unsigned nvqs,
  493. struct virtqueue *vqs[],
  494. vq_callback_t *callbacks[],
  495. const char *names[])
  496. {
  497. int err;
  498. /* Try MSI-X with one vector per queue. */
  499. err = vp_try_to_find_vqs(vdev, nvqs, vqs, callbacks, names, true, true);
  500. if (!err)
  501. return 0;
  502. /* Fallback: MSI-X with one vector for config, one shared for queues. */
  503. err = vp_try_to_find_vqs(vdev, nvqs, vqs, callbacks, names,
  504. true, false);
  505. if (!err)
  506. return 0;
  507. /* Finally fall back to regular interrupts. */
  508. return vp_try_to_find_vqs(vdev, nvqs, vqs, callbacks, names,
  509. false, false);
  510. }
  511. static const char *vp_bus_name(struct virtio_device *vdev)
  512. {
  513. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  514. return pci_name(vp_dev->pci_dev);
  515. }
  516. static struct virtio_config_ops virtio_pci_config_ops = {
  517. .get = vp_get,
  518. .set = vp_set,
  519. .get_status = vp_get_status,
  520. .set_status = vp_set_status,
  521. .reset = vp_reset,
  522. .find_vqs = vp_find_vqs,
  523. .del_vqs = vp_del_vqs,
  524. .get_features = vp_get_features,
  525. .finalize_features = vp_finalize_features,
  526. .bus_name = vp_bus_name,
  527. };
  528. static void virtio_pci_release_dev(struct device *_d)
  529. {
  530. struct virtio_device *vdev = dev_to_virtio(_d);
  531. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  532. /* As struct device is a kobject, it's not safe to
  533. * free the memory (including the reference counter itself)
  534. * until it's release callback. */
  535. kfree(vp_dev);
  536. }
  537. /* the PCI probing function */
  538. static int __devinit virtio_pci_probe(struct pci_dev *pci_dev,
  539. const struct pci_device_id *id)
  540. {
  541. struct virtio_pci_device *vp_dev;
  542. int err;
  543. /* We only own devices >= 0x1000 and <= 0x103f: leave the rest. */
  544. if (pci_dev->device < 0x1000 || pci_dev->device > 0x103f)
  545. return -ENODEV;
  546. if (pci_dev->revision != VIRTIO_PCI_ABI_VERSION) {
  547. printk(KERN_ERR "virtio_pci: expected ABI version %d, got %d\n",
  548. VIRTIO_PCI_ABI_VERSION, pci_dev->revision);
  549. return -ENODEV;
  550. }
  551. /* allocate our structure and fill it out */
  552. vp_dev = kzalloc(sizeof(struct virtio_pci_device), GFP_KERNEL);
  553. if (vp_dev == NULL)
  554. return -ENOMEM;
  555. vp_dev->vdev.dev.parent = &pci_dev->dev;
  556. vp_dev->vdev.dev.release = virtio_pci_release_dev;
  557. vp_dev->vdev.config = &virtio_pci_config_ops;
  558. vp_dev->pci_dev = pci_dev;
  559. INIT_LIST_HEAD(&vp_dev->virtqueues);
  560. spin_lock_init(&vp_dev->lock);
  561. /* Disable MSI/MSIX to bring device to a known good state. */
  562. pci_msi_off(pci_dev);
  563. /* enable the device */
  564. err = pci_enable_device(pci_dev);
  565. if (err)
  566. goto out;
  567. err = pci_request_regions(pci_dev, "virtio-pci");
  568. if (err)
  569. goto out_enable_device;
  570. vp_dev->ioaddr = pci_iomap(pci_dev, 0, 0);
  571. if (vp_dev->ioaddr == NULL)
  572. goto out_req_regions;
  573. pci_set_drvdata(pci_dev, vp_dev);
  574. pci_set_master(pci_dev);
  575. /* we use the subsystem vendor/device id as the virtio vendor/device
  576. * id. this allows us to use the same PCI vendor/device id for all
  577. * virtio devices and to identify the particular virtio driver by
  578. * the subsystem ids */
  579. vp_dev->vdev.id.vendor = pci_dev->subsystem_vendor;
  580. vp_dev->vdev.id.device = pci_dev->subsystem_device;
  581. /* finally register the virtio device */
  582. err = register_virtio_device(&vp_dev->vdev);
  583. if (err)
  584. goto out_set_drvdata;
  585. return 0;
  586. out_set_drvdata:
  587. pci_set_drvdata(pci_dev, NULL);
  588. pci_iounmap(pci_dev, vp_dev->ioaddr);
  589. out_req_regions:
  590. pci_release_regions(pci_dev);
  591. out_enable_device:
  592. pci_disable_device(pci_dev);
  593. out:
  594. kfree(vp_dev);
  595. return err;
  596. }
  597. static void __devexit virtio_pci_remove(struct pci_dev *pci_dev)
  598. {
  599. struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
  600. unregister_virtio_device(&vp_dev->vdev);
  601. vp_del_vqs(&vp_dev->vdev);
  602. pci_set_drvdata(pci_dev, NULL);
  603. pci_iounmap(pci_dev, vp_dev->ioaddr);
  604. pci_release_regions(pci_dev);
  605. pci_disable_device(pci_dev);
  606. }
  607. #ifdef CONFIG_PM
  608. static int virtio_pci_freeze(struct device *dev)
  609. {
  610. struct pci_dev *pci_dev = to_pci_dev(dev);
  611. struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
  612. struct virtio_driver *drv;
  613. int ret;
  614. drv = container_of(vp_dev->vdev.dev.driver,
  615. struct virtio_driver, driver);
  616. ret = 0;
  617. vp_dev->saved_status = vp_get_status(&vp_dev->vdev);
  618. if (drv && drv->freeze)
  619. ret = drv->freeze(&vp_dev->vdev);
  620. if (!ret)
  621. pci_disable_device(pci_dev);
  622. return ret;
  623. }
  624. static int virtio_pci_restore(struct device *dev)
  625. {
  626. struct pci_dev *pci_dev = to_pci_dev(dev);
  627. struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
  628. struct virtio_driver *drv;
  629. unsigned status = 0;
  630. int ret;
  631. drv = container_of(vp_dev->vdev.dev.driver,
  632. struct virtio_driver, driver);
  633. ret = pci_enable_device(pci_dev);
  634. if (ret)
  635. return ret;
  636. pci_set_master(pci_dev);
  637. /* We always start by resetting the device, in case a previous
  638. * driver messed it up. */
  639. vp_reset(&vp_dev->vdev);
  640. /* Acknowledge that we've seen the device. */
  641. status |= VIRTIO_CONFIG_S_ACKNOWLEDGE;
  642. vp_set_status(&vp_dev->vdev, status);
  643. /* Maybe driver failed before freeze.
  644. * Restore the failed status, for debugging. */
  645. status |= vp_dev->saved_status & VIRTIO_CONFIG_S_FAILED;
  646. vp_set_status(&vp_dev->vdev, status);
  647. if (!drv)
  648. return 0;
  649. /* We have a driver! */
  650. status |= VIRTIO_CONFIG_S_DRIVER;
  651. vp_set_status(&vp_dev->vdev, status);
  652. vp_finalize_features(&vp_dev->vdev);
  653. if (drv->restore) {
  654. ret = drv->restore(&vp_dev->vdev);
  655. if (ret) {
  656. status |= VIRTIO_CONFIG_S_FAILED;
  657. vp_set_status(&vp_dev->vdev, status);
  658. return ret;
  659. }
  660. }
  661. /* Finally, tell the device we're all set */
  662. status |= VIRTIO_CONFIG_S_DRIVER_OK;
  663. vp_set_status(&vp_dev->vdev, status);
  664. return ret;
  665. }
  666. static const struct dev_pm_ops virtio_pci_pm_ops = {
  667. SET_SYSTEM_SLEEP_PM_OPS(virtio_pci_freeze, virtio_pci_restore)
  668. };
  669. #endif
  670. static struct pci_driver virtio_pci_driver = {
  671. .name = "virtio-pci",
  672. .id_table = virtio_pci_id_table,
  673. .probe = virtio_pci_probe,
  674. .remove = __devexit_p(virtio_pci_remove),
  675. #ifdef CONFIG_PM
  676. .driver.pm = &virtio_pci_pm_ops,
  677. #endif
  678. };
  679. static int __init virtio_pci_init(void)
  680. {
  681. return pci_register_driver(&virtio_pci_driver);
  682. }
  683. module_init(virtio_pci_init);
  684. static void __exit virtio_pci_exit(void)
  685. {
  686. pci_unregister_driver(&virtio_pci_driver);
  687. }
  688. module_exit(virtio_pci_exit);