virtio_pci.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  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. /* Status saved during hibernate/restore */
  54. u8 saved_status;
  55. /* Whether we have vector per vq */
  56. bool per_vq_vectors;
  57. };
  58. /* Constants for MSI-X */
  59. /* Use first vector for configuration changes, second and the rest for
  60. * virtqueues Thus, we need at least 2 vectors for MSI. */
  61. enum {
  62. VP_MSIX_CONFIG_VECTOR = 0,
  63. VP_MSIX_VQ_VECTOR = 1,
  64. };
  65. struct virtio_pci_vq_info
  66. {
  67. /* the actual virtqueue */
  68. struct virtqueue *vq;
  69. /* the number of entries in the queue */
  70. int num;
  71. /* the virtual address of the ring queue */
  72. void *queue;
  73. /* the list node for the virtqueues list */
  74. struct list_head node;
  75. /* MSI-X vector (or none) */
  76. unsigned msix_vector;
  77. };
  78. /* Qumranet donated their vendor ID for devices 0x1000 thru 0x10FF. */
  79. static DEFINE_PCI_DEVICE_TABLE(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 u32 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 void 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. /* We only support 32 feature bits. */
  104. BUILD_BUG_ON(ARRAY_SIZE(vdev->features) != 1);
  105. iowrite32(vdev->features[0], vp_dev->ioaddr+VIRTIO_PCI_GUEST_FEATURES);
  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 void 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. }
  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. struct virtio_driver *drv;
  179. drv = container_of(vp_dev->vdev.dev.driver,
  180. struct virtio_driver, driver);
  181. if (drv && drv->config_changed)
  182. drv->config_changed(&vp_dev->vdev);
  183. return IRQ_HANDLED;
  184. }
  185. /* Notify all virtqueues on an interrupt. */
  186. static irqreturn_t vp_vring_interrupt(int irq, void *opaque)
  187. {
  188. struct virtio_pci_device *vp_dev = opaque;
  189. struct virtio_pci_vq_info *info;
  190. irqreturn_t ret = IRQ_NONE;
  191. unsigned long flags;
  192. spin_lock_irqsave(&vp_dev->lock, flags);
  193. list_for_each_entry(info, &vp_dev->virtqueues, node) {
  194. if (vring_interrupt(irq, info->vq) == IRQ_HANDLED)
  195. ret = IRQ_HANDLED;
  196. }
  197. spin_unlock_irqrestore(&vp_dev->lock, flags);
  198. return ret;
  199. }
  200. /* A small wrapper to also acknowledge the interrupt when it's handled.
  201. * I really need an EIO hook for the vring so I can ack the interrupt once we
  202. * know that we'll be handling the IRQ but before we invoke the callback since
  203. * the callback may notify the host which results in the host attempting to
  204. * raise an interrupt that we would then mask once we acknowledged the
  205. * interrupt. */
  206. static irqreturn_t vp_interrupt(int irq, void *opaque)
  207. {
  208. struct virtio_pci_device *vp_dev = opaque;
  209. u8 isr;
  210. /* reading the ISR has the effect of also clearing it so it's very
  211. * important to save off the value. */
  212. isr = ioread8(vp_dev->ioaddr + VIRTIO_PCI_ISR);
  213. /* It's definitely not us if the ISR was not high */
  214. if (!isr)
  215. return IRQ_NONE;
  216. /* Configuration change? Tell driver if it wants to know. */
  217. if (isr & VIRTIO_PCI_ISR_CONFIG)
  218. vp_config_changed(irq, opaque);
  219. return vp_vring_interrupt(irq, opaque);
  220. }
  221. static void vp_free_vectors(struct virtio_device *vdev)
  222. {
  223. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  224. int i;
  225. if (vp_dev->intx_enabled) {
  226. free_irq(vp_dev->pci_dev->irq, vp_dev);
  227. vp_dev->intx_enabled = 0;
  228. }
  229. for (i = 0; i < vp_dev->msix_used_vectors; ++i)
  230. free_irq(vp_dev->msix_entries[i].vector, vp_dev);
  231. for (i = 0; i < vp_dev->msix_vectors; i++)
  232. if (vp_dev->msix_affinity_masks[i])
  233. free_cpumask_var(vp_dev->msix_affinity_masks[i]);
  234. if (vp_dev->msix_enabled) {
  235. /* Disable the vector used for configuration */
  236. iowrite16(VIRTIO_MSI_NO_VECTOR,
  237. vp_dev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR);
  238. /* Flush the write out to device */
  239. ioread16(vp_dev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR);
  240. pci_disable_msix(vp_dev->pci_dev);
  241. vp_dev->msix_enabled = 0;
  242. vp_dev->msix_vectors = 0;
  243. }
  244. vp_dev->msix_used_vectors = 0;
  245. kfree(vp_dev->msix_names);
  246. vp_dev->msix_names = NULL;
  247. kfree(vp_dev->msix_entries);
  248. vp_dev->msix_entries = NULL;
  249. kfree(vp_dev->msix_affinity_masks);
  250. vp_dev->msix_affinity_masks = NULL;
  251. }
  252. static int vp_request_msix_vectors(struct virtio_device *vdev, int nvectors,
  253. bool per_vq_vectors)
  254. {
  255. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  256. const char *name = dev_name(&vp_dev->vdev.dev);
  257. unsigned i, v;
  258. int err = -ENOMEM;
  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. /* pci_enable_msix returns positive if we can't get this many. */
  279. err = pci_enable_msix(vp_dev->pci_dev, vp_dev->msix_entries, nvectors);
  280. if (err > 0)
  281. err = -ENOSPC;
  282. if (err)
  283. goto error;
  284. vp_dev->msix_vectors = nvectors;
  285. vp_dev->msix_enabled = 1;
  286. /* Set the vector used for configuration */
  287. v = vp_dev->msix_used_vectors;
  288. snprintf(vp_dev->msix_names[v], sizeof *vp_dev->msix_names,
  289. "%s-config", name);
  290. err = request_irq(vp_dev->msix_entries[v].vector,
  291. vp_config_changed, 0, vp_dev->msix_names[v],
  292. vp_dev);
  293. if (err)
  294. goto error;
  295. ++vp_dev->msix_used_vectors;
  296. iowrite16(v, vp_dev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR);
  297. /* Verify we had enough resources to assign the vector */
  298. v = ioread16(vp_dev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR);
  299. if (v == VIRTIO_MSI_NO_VECTOR) {
  300. err = -EBUSY;
  301. goto error;
  302. }
  303. if (!per_vq_vectors) {
  304. /* Shared vector for all VQs */
  305. v = vp_dev->msix_used_vectors;
  306. snprintf(vp_dev->msix_names[v], sizeof *vp_dev->msix_names,
  307. "%s-virtqueues", name);
  308. err = request_irq(vp_dev->msix_entries[v].vector,
  309. vp_vring_interrupt, 0, vp_dev->msix_names[v],
  310. vp_dev);
  311. if (err)
  312. goto error;
  313. ++vp_dev->msix_used_vectors;
  314. }
  315. return 0;
  316. error:
  317. vp_free_vectors(vdev);
  318. return err;
  319. }
  320. static int vp_request_intx(struct virtio_device *vdev)
  321. {
  322. int err;
  323. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  324. err = request_irq(vp_dev->pci_dev->irq, vp_interrupt,
  325. IRQF_SHARED, dev_name(&vdev->dev), vp_dev);
  326. if (!err)
  327. vp_dev->intx_enabled = 1;
  328. return err;
  329. }
  330. static struct virtqueue *setup_vq(struct virtio_device *vdev, unsigned index,
  331. void (*callback)(struct virtqueue *vq),
  332. const char *name,
  333. u16 msix_vec)
  334. {
  335. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  336. struct virtio_pci_vq_info *info;
  337. struct virtqueue *vq;
  338. unsigned long flags, size;
  339. u16 num;
  340. int err;
  341. /* Select the queue we're interested in */
  342. iowrite16(index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL);
  343. /* Check if queue is either not available or already active. */
  344. num = ioread16(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NUM);
  345. if (!num || ioread32(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN))
  346. return ERR_PTR(-ENOENT);
  347. /* allocate and fill out our structure the represents an active
  348. * queue */
  349. info = kmalloc(sizeof(struct virtio_pci_vq_info), GFP_KERNEL);
  350. if (!info)
  351. return ERR_PTR(-ENOMEM);
  352. info->num = num;
  353. info->msix_vector = msix_vec;
  354. size = PAGE_ALIGN(vring_size(num, VIRTIO_PCI_VRING_ALIGN));
  355. info->queue = alloc_pages_exact(size, GFP_KERNEL|__GFP_ZERO);
  356. if (info->queue == NULL) {
  357. err = -ENOMEM;
  358. goto out_info;
  359. }
  360. /* activate the queue */
  361. iowrite32(virt_to_phys(info->queue) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT,
  362. vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
  363. /* create the vring */
  364. vq = vring_new_virtqueue(index, info->num, VIRTIO_PCI_VRING_ALIGN, vdev,
  365. true, info->queue, vp_notify, callback, name);
  366. if (!vq) {
  367. err = -ENOMEM;
  368. goto out_activate_queue;
  369. }
  370. vq->priv = info;
  371. info->vq = vq;
  372. if (msix_vec != VIRTIO_MSI_NO_VECTOR) {
  373. iowrite16(msix_vec, vp_dev->ioaddr + VIRTIO_MSI_QUEUE_VECTOR);
  374. msix_vec = ioread16(vp_dev->ioaddr + VIRTIO_MSI_QUEUE_VECTOR);
  375. if (msix_vec == VIRTIO_MSI_NO_VECTOR) {
  376. err = -EBUSY;
  377. goto out_assign;
  378. }
  379. }
  380. if (callback) {
  381. spin_lock_irqsave(&vp_dev->lock, flags);
  382. list_add(&info->node, &vp_dev->virtqueues);
  383. spin_unlock_irqrestore(&vp_dev->lock, flags);
  384. } else {
  385. INIT_LIST_HEAD(&info->node);
  386. }
  387. return vq;
  388. out_assign:
  389. vring_del_virtqueue(vq);
  390. out_activate_queue:
  391. iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
  392. free_pages_exact(info->queue, size);
  393. out_info:
  394. kfree(info);
  395. return ERR_PTR(err);
  396. }
  397. static void vp_del_vq(struct virtqueue *vq)
  398. {
  399. struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
  400. struct virtio_pci_vq_info *info = vq->priv;
  401. unsigned long flags, size;
  402. spin_lock_irqsave(&vp_dev->lock, flags);
  403. list_del(&info->node);
  404. spin_unlock_irqrestore(&vp_dev->lock, flags);
  405. iowrite16(vq->index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL);
  406. if (vp_dev->msix_enabled) {
  407. iowrite16(VIRTIO_MSI_NO_VECTOR,
  408. vp_dev->ioaddr + VIRTIO_MSI_QUEUE_VECTOR);
  409. /* Flush the write out to device */
  410. ioread8(vp_dev->ioaddr + VIRTIO_PCI_ISR);
  411. }
  412. vring_del_virtqueue(vq);
  413. /* Select and deactivate the queue */
  414. iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
  415. size = PAGE_ALIGN(vring_size(info->num, VIRTIO_PCI_VRING_ALIGN));
  416. free_pages_exact(info->queue, size);
  417. kfree(info);
  418. }
  419. /* the config->del_vqs() implementation */
  420. static void vp_del_vqs(struct virtio_device *vdev)
  421. {
  422. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  423. struct virtqueue *vq, *n;
  424. struct virtio_pci_vq_info *info;
  425. list_for_each_entry_safe(vq, n, &vdev->vqs, list) {
  426. info = vq->priv;
  427. if (vp_dev->per_vq_vectors &&
  428. info->msix_vector != VIRTIO_MSI_NO_VECTOR)
  429. free_irq(vp_dev->msix_entries[info->msix_vector].vector,
  430. vq);
  431. vp_del_vq(vq);
  432. }
  433. vp_dev->per_vq_vectors = false;
  434. vp_free_vectors(vdev);
  435. }
  436. static int vp_try_to_find_vqs(struct virtio_device *vdev, unsigned nvqs,
  437. struct virtqueue *vqs[],
  438. vq_callback_t *callbacks[],
  439. const char *names[],
  440. bool use_msix,
  441. bool per_vq_vectors)
  442. {
  443. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  444. u16 msix_vec;
  445. int i, err, nvectors, allocated_vectors;
  446. if (!use_msix) {
  447. /* Old style: one normal interrupt for change and all vqs. */
  448. err = vp_request_intx(vdev);
  449. if (err)
  450. goto error_request;
  451. } else {
  452. if (per_vq_vectors) {
  453. /* Best option: one for change interrupt, one per vq. */
  454. nvectors = 1;
  455. for (i = 0; i < nvqs; ++i)
  456. if (callbacks[i])
  457. ++nvectors;
  458. } else {
  459. /* Second best: one for change, shared for all vqs. */
  460. nvectors = 2;
  461. }
  462. err = vp_request_msix_vectors(vdev, nvectors, per_vq_vectors);
  463. if (err)
  464. goto error_request;
  465. }
  466. vp_dev->per_vq_vectors = per_vq_vectors;
  467. allocated_vectors = vp_dev->msix_used_vectors;
  468. for (i = 0; i < nvqs; ++i) {
  469. if (!names[i]) {
  470. vqs[i] = NULL;
  471. continue;
  472. } else if (!callbacks[i] || !vp_dev->msix_enabled)
  473. msix_vec = VIRTIO_MSI_NO_VECTOR;
  474. else if (vp_dev->per_vq_vectors)
  475. msix_vec = allocated_vectors++;
  476. else
  477. msix_vec = VP_MSIX_VQ_VECTOR;
  478. vqs[i] = setup_vq(vdev, i, callbacks[i], names[i], msix_vec);
  479. if (IS_ERR(vqs[i])) {
  480. err = PTR_ERR(vqs[i]);
  481. goto error_find;
  482. }
  483. if (!vp_dev->per_vq_vectors || msix_vec == VIRTIO_MSI_NO_VECTOR)
  484. continue;
  485. /* allocate per-vq irq if available and necessary */
  486. snprintf(vp_dev->msix_names[msix_vec],
  487. sizeof *vp_dev->msix_names,
  488. "%s-%s",
  489. dev_name(&vp_dev->vdev.dev), names[i]);
  490. err = request_irq(vp_dev->msix_entries[msix_vec].vector,
  491. vring_interrupt, 0,
  492. vp_dev->msix_names[msix_vec],
  493. vqs[i]);
  494. if (err) {
  495. vp_del_vq(vqs[i]);
  496. goto error_find;
  497. }
  498. }
  499. return 0;
  500. error_find:
  501. vp_del_vqs(vdev);
  502. error_request:
  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 = vq->priv;
  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. pci_set_drvdata(pci_dev, vp_dev);
  616. pci_set_master(pci_dev);
  617. /* we use the subsystem vendor/device id as the virtio vendor/device
  618. * id. this allows us to use the same PCI vendor/device id for all
  619. * virtio devices and to identify the particular virtio driver by
  620. * the subsystem ids */
  621. vp_dev->vdev.id.vendor = pci_dev->subsystem_vendor;
  622. vp_dev->vdev.id.device = pci_dev->subsystem_device;
  623. /* finally register the virtio device */
  624. err = register_virtio_device(&vp_dev->vdev);
  625. if (err)
  626. goto out_set_drvdata;
  627. return 0;
  628. out_set_drvdata:
  629. pci_set_drvdata(pci_dev, NULL);
  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_set_drvdata(pci_dev, NULL);
  645. pci_iounmap(pci_dev, vp_dev->ioaddr);
  646. pci_release_regions(pci_dev);
  647. pci_disable_device(pci_dev);
  648. kfree(vp_dev);
  649. }
  650. #ifdef CONFIG_PM
  651. static int virtio_pci_freeze(struct device *dev)
  652. {
  653. struct pci_dev *pci_dev = to_pci_dev(dev);
  654. struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
  655. struct virtio_driver *drv;
  656. int ret;
  657. drv = container_of(vp_dev->vdev.dev.driver,
  658. struct virtio_driver, driver);
  659. ret = 0;
  660. vp_dev->saved_status = vp_get_status(&vp_dev->vdev);
  661. if (drv && drv->freeze)
  662. ret = drv->freeze(&vp_dev->vdev);
  663. if (!ret)
  664. pci_disable_device(pci_dev);
  665. return ret;
  666. }
  667. static int virtio_pci_restore(struct device *dev)
  668. {
  669. struct pci_dev *pci_dev = to_pci_dev(dev);
  670. struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
  671. struct virtio_driver *drv;
  672. int ret;
  673. drv = container_of(vp_dev->vdev.dev.driver,
  674. struct virtio_driver, driver);
  675. ret = pci_enable_device(pci_dev);
  676. if (ret)
  677. return ret;
  678. pci_set_master(pci_dev);
  679. vp_finalize_features(&vp_dev->vdev);
  680. if (drv && drv->restore)
  681. ret = drv->restore(&vp_dev->vdev);
  682. /* Finally, tell the device we're all set */
  683. if (!ret)
  684. vp_set_status(&vp_dev->vdev, vp_dev->saved_status);
  685. return ret;
  686. }
  687. static const struct dev_pm_ops virtio_pci_pm_ops = {
  688. SET_SYSTEM_SLEEP_PM_OPS(virtio_pci_freeze, virtio_pci_restore)
  689. };
  690. #endif
  691. static struct pci_driver virtio_pci_driver = {
  692. .name = "virtio-pci",
  693. .id_table = virtio_pci_id_table,
  694. .probe = virtio_pci_probe,
  695. .remove = virtio_pci_remove,
  696. #ifdef CONFIG_PM
  697. .driver.pm = &virtio_pci_pm_ops,
  698. #endif
  699. };
  700. module_pci_driver(virtio_pci_driver);