virtio_pci.c 21 KB

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