virtio_pci.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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 bool vp_notify(struct virtqueue *vq)
  168. {
  169. struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
  170. /* we write the queue's selector into the notification register to
  171. * signal the other end */
  172. iowrite16(vq->index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NOTIFY);
  173. return true;
  174. }
  175. /* Handle a configuration change: Tell driver if it wants to know. */
  176. static irqreturn_t vp_config_changed(int irq, void *opaque)
  177. {
  178. struct virtio_pci_device *vp_dev = opaque;
  179. struct virtio_driver *drv;
  180. drv = container_of(vp_dev->vdev.dev.driver,
  181. struct virtio_driver, driver);
  182. if (drv && drv->config_changed)
  183. drv->config_changed(&vp_dev->vdev);
  184. return IRQ_HANDLED;
  185. }
  186. /* Notify all virtqueues on an interrupt. */
  187. static irqreturn_t vp_vring_interrupt(int irq, void *opaque)
  188. {
  189. struct virtio_pci_device *vp_dev = opaque;
  190. struct virtio_pci_vq_info *info;
  191. irqreturn_t ret = IRQ_NONE;
  192. unsigned long flags;
  193. spin_lock_irqsave(&vp_dev->lock, flags);
  194. list_for_each_entry(info, &vp_dev->virtqueues, node) {
  195. if (vring_interrupt(irq, info->vq) == IRQ_HANDLED)
  196. ret = IRQ_HANDLED;
  197. }
  198. spin_unlock_irqrestore(&vp_dev->lock, flags);
  199. return ret;
  200. }
  201. /* A small wrapper to also acknowledge the interrupt when it's handled.
  202. * I really need an EIO hook for the vring so I can ack the interrupt once we
  203. * know that we'll be handling the IRQ but before we invoke the callback since
  204. * the callback may notify the host which results in the host attempting to
  205. * raise an interrupt that we would then mask once we acknowledged the
  206. * interrupt. */
  207. static irqreturn_t vp_interrupt(int irq, void *opaque)
  208. {
  209. struct virtio_pci_device *vp_dev = opaque;
  210. u8 isr;
  211. /* reading the ISR has the effect of also clearing it so it's very
  212. * important to save off the value. */
  213. isr = ioread8(vp_dev->ioaddr + VIRTIO_PCI_ISR);
  214. /* It's definitely not us if the ISR was not high */
  215. if (!isr)
  216. return IRQ_NONE;
  217. /* Configuration change? Tell driver if it wants to know. */
  218. if (isr & VIRTIO_PCI_ISR_CONFIG)
  219. vp_config_changed(irq, opaque);
  220. return vp_vring_interrupt(irq, opaque);
  221. }
  222. static void vp_free_vectors(struct virtio_device *vdev)
  223. {
  224. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  225. int i;
  226. if (vp_dev->intx_enabled) {
  227. free_irq(vp_dev->pci_dev->irq, vp_dev);
  228. vp_dev->intx_enabled = 0;
  229. }
  230. for (i = 0; i < vp_dev->msix_used_vectors; ++i)
  231. free_irq(vp_dev->msix_entries[i].vector, vp_dev);
  232. for (i = 0; i < vp_dev->msix_vectors; i++)
  233. if (vp_dev->msix_affinity_masks[i])
  234. free_cpumask_var(vp_dev->msix_affinity_masks[i]);
  235. if (vp_dev->msix_enabled) {
  236. /* Disable the vector used for configuration */
  237. iowrite16(VIRTIO_MSI_NO_VECTOR,
  238. vp_dev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR);
  239. /* Flush the write out to device */
  240. ioread16(vp_dev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR);
  241. pci_disable_msix(vp_dev->pci_dev);
  242. vp_dev->msix_enabled = 0;
  243. }
  244. vp_dev->msix_vectors = 0;
  245. vp_dev->msix_used_vectors = 0;
  246. kfree(vp_dev->msix_names);
  247. vp_dev->msix_names = NULL;
  248. kfree(vp_dev->msix_entries);
  249. vp_dev->msix_entries = NULL;
  250. kfree(vp_dev->msix_affinity_masks);
  251. vp_dev->msix_affinity_masks = NULL;
  252. }
  253. static int vp_request_msix_vectors(struct virtio_device *vdev, int nvectors,
  254. bool per_vq_vectors)
  255. {
  256. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  257. const char *name = dev_name(&vp_dev->vdev.dev);
  258. unsigned i, v;
  259. int err = -ENOMEM;
  260. vp_dev->msix_vectors = nvectors;
  261. vp_dev->msix_entries = kmalloc(nvectors * sizeof *vp_dev->msix_entries,
  262. GFP_KERNEL);
  263. if (!vp_dev->msix_entries)
  264. goto error;
  265. vp_dev->msix_names = kmalloc(nvectors * sizeof *vp_dev->msix_names,
  266. GFP_KERNEL);
  267. if (!vp_dev->msix_names)
  268. goto error;
  269. vp_dev->msix_affinity_masks
  270. = kzalloc(nvectors * sizeof *vp_dev->msix_affinity_masks,
  271. GFP_KERNEL);
  272. if (!vp_dev->msix_affinity_masks)
  273. goto error;
  274. for (i = 0; i < nvectors; ++i)
  275. if (!alloc_cpumask_var(&vp_dev->msix_affinity_masks[i],
  276. GFP_KERNEL))
  277. goto error;
  278. for (i = 0; i < nvectors; ++i)
  279. vp_dev->msix_entries[i].entry = i;
  280. /* pci_enable_msix returns positive if we can't get this many. */
  281. err = pci_enable_msix(vp_dev->pci_dev, vp_dev->msix_entries, nvectors);
  282. if (err > 0)
  283. err = -ENOSPC;
  284. if (err)
  285. goto error;
  286. vp_dev->msix_enabled = 1;
  287. /* Set the vector used for configuration */
  288. v = vp_dev->msix_used_vectors;
  289. snprintf(vp_dev->msix_names[v], sizeof *vp_dev->msix_names,
  290. "%s-config", name);
  291. err = request_irq(vp_dev->msix_entries[v].vector,
  292. vp_config_changed, 0, vp_dev->msix_names[v],
  293. vp_dev);
  294. if (err)
  295. goto error;
  296. ++vp_dev->msix_used_vectors;
  297. iowrite16(v, vp_dev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR);
  298. /* Verify we had enough resources to assign the vector */
  299. v = ioread16(vp_dev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR);
  300. if (v == VIRTIO_MSI_NO_VECTOR) {
  301. err = -EBUSY;
  302. goto error;
  303. }
  304. if (!per_vq_vectors) {
  305. /* Shared vector for all VQs */
  306. v = vp_dev->msix_used_vectors;
  307. snprintf(vp_dev->msix_names[v], sizeof *vp_dev->msix_names,
  308. "%s-virtqueues", name);
  309. err = request_irq(vp_dev->msix_entries[v].vector,
  310. vp_vring_interrupt, 0, vp_dev->msix_names[v],
  311. vp_dev);
  312. if (err)
  313. goto error;
  314. ++vp_dev->msix_used_vectors;
  315. }
  316. return 0;
  317. error:
  318. vp_free_vectors(vdev);
  319. return err;
  320. }
  321. static int vp_request_intx(struct virtio_device *vdev)
  322. {
  323. int err;
  324. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  325. err = request_irq(vp_dev->pci_dev->irq, vp_interrupt,
  326. IRQF_SHARED, dev_name(&vdev->dev), vp_dev);
  327. if (!err)
  328. vp_dev->intx_enabled = 1;
  329. return err;
  330. }
  331. static struct virtqueue *setup_vq(struct virtio_device *vdev, unsigned index,
  332. void (*callback)(struct virtqueue *vq),
  333. const char *name,
  334. u16 msix_vec)
  335. {
  336. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  337. struct virtio_pci_vq_info *info;
  338. struct virtqueue *vq;
  339. unsigned long flags, size;
  340. u16 num;
  341. int err;
  342. /* Select the queue we're interested in */
  343. iowrite16(index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL);
  344. /* Check if queue is either not available or already active. */
  345. num = ioread16(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NUM);
  346. if (!num || ioread32(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN))
  347. return ERR_PTR(-ENOENT);
  348. /* allocate and fill out our structure the represents an active
  349. * queue */
  350. info = kmalloc(sizeof(struct virtio_pci_vq_info), GFP_KERNEL);
  351. if (!info)
  352. return ERR_PTR(-ENOMEM);
  353. info->num = num;
  354. info->msix_vector = msix_vec;
  355. size = PAGE_ALIGN(vring_size(num, VIRTIO_PCI_VRING_ALIGN));
  356. info->queue = alloc_pages_exact(size, GFP_KERNEL|__GFP_ZERO);
  357. if (info->queue == NULL) {
  358. err = -ENOMEM;
  359. goto out_info;
  360. }
  361. /* activate the queue */
  362. iowrite32(virt_to_phys(info->queue) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT,
  363. vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
  364. /* create the vring */
  365. vq = vring_new_virtqueue(index, info->num, VIRTIO_PCI_VRING_ALIGN, vdev,
  366. true, info->queue, vp_notify, callback, name);
  367. if (!vq) {
  368. err = -ENOMEM;
  369. goto out_activate_queue;
  370. }
  371. vq->priv = info;
  372. info->vq = vq;
  373. if (msix_vec != VIRTIO_MSI_NO_VECTOR) {
  374. iowrite16(msix_vec, vp_dev->ioaddr + VIRTIO_MSI_QUEUE_VECTOR);
  375. msix_vec = ioread16(vp_dev->ioaddr + VIRTIO_MSI_QUEUE_VECTOR);
  376. if (msix_vec == VIRTIO_MSI_NO_VECTOR) {
  377. err = -EBUSY;
  378. goto out_assign;
  379. }
  380. }
  381. if (callback) {
  382. spin_lock_irqsave(&vp_dev->lock, flags);
  383. list_add(&info->node, &vp_dev->virtqueues);
  384. spin_unlock_irqrestore(&vp_dev->lock, flags);
  385. } else {
  386. INIT_LIST_HEAD(&info->node);
  387. }
  388. return vq;
  389. out_assign:
  390. vring_del_virtqueue(vq);
  391. out_activate_queue:
  392. iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
  393. free_pages_exact(info->queue, size);
  394. out_info:
  395. kfree(info);
  396. return ERR_PTR(err);
  397. }
  398. static void vp_del_vq(struct virtqueue *vq)
  399. {
  400. struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
  401. struct virtio_pci_vq_info *info = vq->priv;
  402. unsigned long flags, size;
  403. spin_lock_irqsave(&vp_dev->lock, flags);
  404. list_del(&info->node);
  405. spin_unlock_irqrestore(&vp_dev->lock, flags);
  406. iowrite16(vq->index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL);
  407. if (vp_dev->msix_enabled) {
  408. iowrite16(VIRTIO_MSI_NO_VECTOR,
  409. vp_dev->ioaddr + VIRTIO_MSI_QUEUE_VECTOR);
  410. /* Flush the write out to device */
  411. ioread8(vp_dev->ioaddr + VIRTIO_PCI_ISR);
  412. }
  413. vring_del_virtqueue(vq);
  414. /* Select and deactivate the queue */
  415. iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
  416. size = PAGE_ALIGN(vring_size(info->num, VIRTIO_PCI_VRING_ALIGN));
  417. free_pages_exact(info->queue, size);
  418. kfree(info);
  419. }
  420. /* the config->del_vqs() implementation */
  421. static void vp_del_vqs(struct virtio_device *vdev)
  422. {
  423. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  424. struct virtqueue *vq, *n;
  425. struct virtio_pci_vq_info *info;
  426. list_for_each_entry_safe(vq, n, &vdev->vqs, list) {
  427. info = vq->priv;
  428. if (vp_dev->per_vq_vectors &&
  429. info->msix_vector != VIRTIO_MSI_NO_VECTOR)
  430. free_irq(vp_dev->msix_entries[info->msix_vector].vector,
  431. vq);
  432. vp_del_vq(vq);
  433. }
  434. vp_dev->per_vq_vectors = false;
  435. vp_free_vectors(vdev);
  436. }
  437. static int vp_try_to_find_vqs(struct virtio_device *vdev, unsigned nvqs,
  438. struct virtqueue *vqs[],
  439. vq_callback_t *callbacks[],
  440. const char *names[],
  441. bool use_msix,
  442. bool per_vq_vectors)
  443. {
  444. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  445. u16 msix_vec;
  446. int i, err, nvectors, allocated_vectors;
  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_request;
  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_request;
  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. error_request:
  504. return err;
  505. }
  506. /* the config->find_vqs() implementation */
  507. static int vp_find_vqs(struct virtio_device *vdev, unsigned nvqs,
  508. struct virtqueue *vqs[],
  509. vq_callback_t *callbacks[],
  510. const char *names[])
  511. {
  512. int err;
  513. /* Try MSI-X with one vector per queue. */
  514. err = vp_try_to_find_vqs(vdev, nvqs, vqs, callbacks, names, true, true);
  515. if (!err)
  516. return 0;
  517. /* Fallback: MSI-X with one vector for config, one shared for queues. */
  518. err = vp_try_to_find_vqs(vdev, nvqs, vqs, callbacks, names,
  519. true, false);
  520. if (!err)
  521. return 0;
  522. /* Finally fall back to regular interrupts. */
  523. return vp_try_to_find_vqs(vdev, nvqs, vqs, callbacks, names,
  524. false, false);
  525. }
  526. static const char *vp_bus_name(struct virtio_device *vdev)
  527. {
  528. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  529. return pci_name(vp_dev->pci_dev);
  530. }
  531. /* Setup the affinity for a virtqueue:
  532. * - force the affinity for per vq vector
  533. * - OR over all affinities for shared MSI
  534. * - ignore the affinity request if we're using INTX
  535. */
  536. static int vp_set_vq_affinity(struct virtqueue *vq, int cpu)
  537. {
  538. struct virtio_device *vdev = vq->vdev;
  539. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  540. struct virtio_pci_vq_info *info = vq->priv;
  541. struct cpumask *mask;
  542. unsigned int irq;
  543. if (!vq->callback)
  544. return -EINVAL;
  545. if (vp_dev->msix_enabled) {
  546. mask = vp_dev->msix_affinity_masks[info->msix_vector];
  547. irq = vp_dev->msix_entries[info->msix_vector].vector;
  548. if (cpu == -1)
  549. irq_set_affinity_hint(irq, NULL);
  550. else {
  551. cpumask_set_cpu(cpu, mask);
  552. irq_set_affinity_hint(irq, mask);
  553. }
  554. }
  555. return 0;
  556. }
  557. static const struct virtio_config_ops virtio_pci_config_ops = {
  558. .get = vp_get,
  559. .set = vp_set,
  560. .get_status = vp_get_status,
  561. .set_status = vp_set_status,
  562. .reset = vp_reset,
  563. .find_vqs = vp_find_vqs,
  564. .del_vqs = vp_del_vqs,
  565. .get_features = vp_get_features,
  566. .finalize_features = vp_finalize_features,
  567. .bus_name = vp_bus_name,
  568. .set_vq_affinity = vp_set_vq_affinity,
  569. };
  570. static void virtio_pci_release_dev(struct device *_d)
  571. {
  572. /*
  573. * No need for a release method as we allocate/free
  574. * all devices together with the pci devices.
  575. * Provide an empty one to avoid getting a warning from core.
  576. */
  577. }
  578. /* the PCI probing function */
  579. static int virtio_pci_probe(struct pci_dev *pci_dev,
  580. const struct pci_device_id *id)
  581. {
  582. struct virtio_pci_device *vp_dev;
  583. int err;
  584. /* We only own devices >= 0x1000 and <= 0x103f: leave the rest. */
  585. if (pci_dev->device < 0x1000 || pci_dev->device > 0x103f)
  586. return -ENODEV;
  587. if (pci_dev->revision != VIRTIO_PCI_ABI_VERSION) {
  588. printk(KERN_ERR "virtio_pci: expected ABI version %d, got %d\n",
  589. VIRTIO_PCI_ABI_VERSION, pci_dev->revision);
  590. return -ENODEV;
  591. }
  592. /* allocate our structure and fill it out */
  593. vp_dev = kzalloc(sizeof(struct virtio_pci_device), GFP_KERNEL);
  594. if (vp_dev == NULL)
  595. return -ENOMEM;
  596. vp_dev->vdev.dev.parent = &pci_dev->dev;
  597. vp_dev->vdev.dev.release = virtio_pci_release_dev;
  598. vp_dev->vdev.config = &virtio_pci_config_ops;
  599. vp_dev->pci_dev = pci_dev;
  600. INIT_LIST_HEAD(&vp_dev->virtqueues);
  601. spin_lock_init(&vp_dev->lock);
  602. /* Disable MSI/MSIX to bring device to a known good state. */
  603. pci_msi_off(pci_dev);
  604. /* enable the device */
  605. err = pci_enable_device(pci_dev);
  606. if (err)
  607. goto out;
  608. err = pci_request_regions(pci_dev, "virtio-pci");
  609. if (err)
  610. goto out_enable_device;
  611. vp_dev->ioaddr = pci_iomap(pci_dev, 0, 0);
  612. if (vp_dev->ioaddr == NULL) {
  613. err = -ENOMEM;
  614. goto out_req_regions;
  615. }
  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_set_drvdata(pci_dev, NULL);
  631. pci_iounmap(pci_dev, vp_dev->ioaddr);
  632. out_req_regions:
  633. pci_release_regions(pci_dev);
  634. out_enable_device:
  635. pci_disable_device(pci_dev);
  636. out:
  637. kfree(vp_dev);
  638. return err;
  639. }
  640. static void virtio_pci_remove(struct pci_dev *pci_dev)
  641. {
  642. struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
  643. unregister_virtio_device(&vp_dev->vdev);
  644. vp_del_vqs(&vp_dev->vdev);
  645. pci_set_drvdata(pci_dev, NULL);
  646. pci_iounmap(pci_dev, vp_dev->ioaddr);
  647. pci_release_regions(pci_dev);
  648. pci_disable_device(pci_dev);
  649. kfree(vp_dev);
  650. }
  651. #ifdef CONFIG_PM_SLEEP
  652. static int virtio_pci_freeze(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. struct virtio_driver *drv;
  657. int ret;
  658. drv = container_of(vp_dev->vdev.dev.driver,
  659. struct virtio_driver, driver);
  660. ret = 0;
  661. vp_dev->saved_status = vp_get_status(&vp_dev->vdev);
  662. if (drv && drv->freeze)
  663. ret = drv->freeze(&vp_dev->vdev);
  664. if (!ret)
  665. pci_disable_device(pci_dev);
  666. return ret;
  667. }
  668. static int virtio_pci_restore(struct device *dev)
  669. {
  670. struct pci_dev *pci_dev = to_pci_dev(dev);
  671. struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
  672. struct virtio_driver *drv;
  673. int ret;
  674. drv = container_of(vp_dev->vdev.dev.driver,
  675. struct virtio_driver, driver);
  676. ret = pci_enable_device(pci_dev);
  677. if (ret)
  678. return ret;
  679. pci_set_master(pci_dev);
  680. vp_finalize_features(&vp_dev->vdev);
  681. if (drv && drv->restore)
  682. ret = drv->restore(&vp_dev->vdev);
  683. /* Finally, tell the device we're all set */
  684. if (!ret)
  685. vp_set_status(&vp_dev->vdev, vp_dev->saved_status);
  686. return ret;
  687. }
  688. static const struct dev_pm_ops virtio_pci_pm_ops = {
  689. SET_SYSTEM_SLEEP_PM_OPS(virtio_pci_freeze, virtio_pci_restore)
  690. };
  691. #endif
  692. static struct pci_driver virtio_pci_driver = {
  693. .name = "virtio-pci",
  694. .id_table = virtio_pci_id_table,
  695. .probe = virtio_pci_probe,
  696. .remove = virtio_pci_remove,
  697. #ifdef CONFIG_PM_SLEEP
  698. .driver.pm = &virtio_pci_pm_ops,
  699. #endif
  700. };
  701. module_pci_driver(virtio_pci_driver);