virtio_pci.c 21 KB

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