virtio_pci.c 21 KB

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