virtio_pci.c 21 KB

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