virtio_pci.c 21 KB

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