virtio_pci.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  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. /* Name strings for interrupts. This size should be enough,
  46. * and I'm too lazy to allocate each name separately. */
  47. char (*msix_names)[256];
  48. /* Number of available vectors */
  49. unsigned msix_vectors;
  50. /* Vectors allocated, excluding per-vq vectors if any */
  51. unsigned msix_used_vectors;
  52. /* Whether we have vector per vq */
  53. bool per_vq_vectors;
  54. };
  55. /* Constants for MSI-X */
  56. /* Use first vector for configuration changes, second and the rest for
  57. * virtqueues Thus, we need at least 2 vectors for MSI. */
  58. enum {
  59. VP_MSIX_CONFIG_VECTOR = 0,
  60. VP_MSIX_VQ_VECTOR = 1,
  61. };
  62. struct virtio_pci_vq_info
  63. {
  64. /* the actual virtqueue */
  65. struct virtqueue *vq;
  66. /* the number of entries in the queue */
  67. int num;
  68. /* the index of the queue */
  69. int queue_index;
  70. /* the virtual address of the ring queue */
  71. void *queue;
  72. /* the list node for the virtqueues list */
  73. struct list_head node;
  74. /* MSI-X vector (or none) */
  75. unsigned msix_vector;
  76. };
  77. /* Qumranet donated their vendor ID for devices 0x1000 thru 0x10FF. */
  78. static struct pci_device_id virtio_pci_id_table[] = {
  79. { 0x1af4, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  80. { 0 },
  81. };
  82. MODULE_DEVICE_TABLE(pci, virtio_pci_id_table);
  83. /* Convert a generic virtio device to our structure */
  84. static struct virtio_pci_device *to_vp_device(struct virtio_device *vdev)
  85. {
  86. return container_of(vdev, struct virtio_pci_device, vdev);
  87. }
  88. /* virtio config->get_features() implementation */
  89. static u32 vp_get_features(struct virtio_device *vdev)
  90. {
  91. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  92. /* When someone needs more than 32 feature bits, we'll need to
  93. * steal a bit to indicate that the rest are somewhere else. */
  94. return ioread32(vp_dev->ioaddr + VIRTIO_PCI_HOST_FEATURES);
  95. }
  96. /* virtio config->finalize_features() implementation */
  97. static void vp_finalize_features(struct virtio_device *vdev)
  98. {
  99. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  100. /* Give virtio_ring a chance to accept features. */
  101. vring_transport_features(vdev);
  102. /* We only support 32 feature bits. */
  103. BUILD_BUG_ON(ARRAY_SIZE(vdev->features) != 1);
  104. iowrite32(vdev->features[0], vp_dev->ioaddr+VIRTIO_PCI_GUEST_FEATURES);
  105. }
  106. /* virtio config->get() implementation */
  107. static void vp_get(struct virtio_device *vdev, unsigned offset,
  108. void *buf, unsigned len)
  109. {
  110. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  111. void __iomem *ioaddr = vp_dev->ioaddr +
  112. VIRTIO_PCI_CONFIG(vp_dev) + offset;
  113. u8 *ptr = buf;
  114. int i;
  115. for (i = 0; i < len; i++)
  116. ptr[i] = ioread8(ioaddr + i);
  117. }
  118. /* the config->set() implementation. it's symmetric to the config->get()
  119. * implementation */
  120. static void vp_set(struct virtio_device *vdev, unsigned offset,
  121. const void *buf, unsigned len)
  122. {
  123. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  124. void __iomem *ioaddr = vp_dev->ioaddr +
  125. VIRTIO_PCI_CONFIG(vp_dev) + offset;
  126. const u8 *ptr = buf;
  127. int i;
  128. for (i = 0; i < len; i++)
  129. iowrite8(ptr[i], ioaddr + i);
  130. }
  131. /* config->{get,set}_status() implementations */
  132. static u8 vp_get_status(struct virtio_device *vdev)
  133. {
  134. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  135. return ioread8(vp_dev->ioaddr + VIRTIO_PCI_STATUS);
  136. }
  137. static void vp_set_status(struct virtio_device *vdev, u8 status)
  138. {
  139. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  140. /* We should never be setting status to 0. */
  141. BUG_ON(status == 0);
  142. iowrite8(status, vp_dev->ioaddr + VIRTIO_PCI_STATUS);
  143. }
  144. /* wait for pending irq handlers */
  145. static void vp_synchronize_vectors(struct virtio_device *vdev)
  146. {
  147. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  148. int i;
  149. if (vp_dev->intx_enabled)
  150. synchronize_irq(vp_dev->pci_dev->irq);
  151. for (i = 0; i < vp_dev->msix_vectors; ++i)
  152. synchronize_irq(vp_dev->msix_entries[i].vector);
  153. }
  154. static void vp_reset(struct virtio_device *vdev)
  155. {
  156. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  157. /* 0 status means a reset. */
  158. iowrite8(0, vp_dev->ioaddr + VIRTIO_PCI_STATUS);
  159. /* Flush out the status write, and flush in device writes,
  160. * including MSi-X interrupts, if any. */
  161. ioread8(vp_dev->ioaddr + VIRTIO_PCI_STATUS);
  162. /* Flush pending VQ/configuration callbacks. */
  163. vp_synchronize_vectors(vdev);
  164. }
  165. /* the notify function used when creating a virt queue */
  166. static void vp_notify(struct virtqueue *vq)
  167. {
  168. struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
  169. struct virtio_pci_vq_info *info = vq->priv;
  170. /* we write the queue's selector into the notification register to
  171. * signal the other end */
  172. iowrite16(info->queue_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. if (vp_dev->msix_enabled) {
  232. /* Disable the vector used for configuration */
  233. iowrite16(VIRTIO_MSI_NO_VECTOR,
  234. vp_dev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR);
  235. /* Flush the write out to device */
  236. ioread16(vp_dev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR);
  237. pci_disable_msix(vp_dev->pci_dev);
  238. vp_dev->msix_enabled = 0;
  239. vp_dev->msix_vectors = 0;
  240. }
  241. vp_dev->msix_used_vectors = 0;
  242. kfree(vp_dev->msix_names);
  243. vp_dev->msix_names = NULL;
  244. kfree(vp_dev->msix_entries);
  245. vp_dev->msix_entries = NULL;
  246. }
  247. static int vp_request_msix_vectors(struct virtio_device *vdev, int nvectors,
  248. bool per_vq_vectors)
  249. {
  250. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  251. const char *name = dev_name(&vp_dev->vdev.dev);
  252. unsigned i, v;
  253. int err = -ENOMEM;
  254. vp_dev->msix_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. for (i = 0; i < nvectors; ++i)
  263. vp_dev->msix_entries[i].entry = i;
  264. /* pci_enable_msix returns positive if we can't get this many. */
  265. err = pci_enable_msix(vp_dev->pci_dev, vp_dev->msix_entries, nvectors);
  266. if (err > 0)
  267. err = -ENOSPC;
  268. if (err)
  269. goto error;
  270. vp_dev->msix_vectors = nvectors;
  271. vp_dev->msix_enabled = 1;
  272. /* Set the vector used for configuration */
  273. v = vp_dev->msix_used_vectors;
  274. snprintf(vp_dev->msix_names[v], sizeof *vp_dev->msix_names,
  275. "%s-config", name);
  276. err = request_irq(vp_dev->msix_entries[v].vector,
  277. vp_config_changed, 0, vp_dev->msix_names[v],
  278. vp_dev);
  279. if (err)
  280. goto error;
  281. ++vp_dev->msix_used_vectors;
  282. iowrite16(v, vp_dev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR);
  283. /* Verify we had enough resources to assign the vector */
  284. v = ioread16(vp_dev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR);
  285. if (v == VIRTIO_MSI_NO_VECTOR) {
  286. err = -EBUSY;
  287. goto error;
  288. }
  289. if (!per_vq_vectors) {
  290. /* Shared vector for all VQs */
  291. v = vp_dev->msix_used_vectors;
  292. snprintf(vp_dev->msix_names[v], sizeof *vp_dev->msix_names,
  293. "%s-virtqueues", name);
  294. err = request_irq(vp_dev->msix_entries[v].vector,
  295. vp_vring_interrupt, 0, vp_dev->msix_names[v],
  296. vp_dev);
  297. if (err)
  298. goto error;
  299. ++vp_dev->msix_used_vectors;
  300. }
  301. return 0;
  302. error:
  303. vp_free_vectors(vdev);
  304. return err;
  305. }
  306. static int vp_request_intx(struct virtio_device *vdev)
  307. {
  308. int err;
  309. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  310. err = request_irq(vp_dev->pci_dev->irq, vp_interrupt,
  311. IRQF_SHARED, dev_name(&vdev->dev), vp_dev);
  312. if (!err)
  313. vp_dev->intx_enabled = 1;
  314. return err;
  315. }
  316. static struct virtqueue *setup_vq(struct virtio_device *vdev, unsigned index,
  317. void (*callback)(struct virtqueue *vq),
  318. const char *name,
  319. u16 msix_vec)
  320. {
  321. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  322. struct virtio_pci_vq_info *info;
  323. struct virtqueue *vq;
  324. unsigned long flags, size;
  325. u16 num;
  326. int err;
  327. /* Select the queue we're interested in */
  328. iowrite16(index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL);
  329. /* Check if queue is either not available or already active. */
  330. num = ioread16(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NUM);
  331. if (!num || ioread32(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN))
  332. return ERR_PTR(-ENOENT);
  333. /* allocate and fill out our structure the represents an active
  334. * queue */
  335. info = kmalloc(sizeof(struct virtio_pci_vq_info), GFP_KERNEL);
  336. if (!info)
  337. return ERR_PTR(-ENOMEM);
  338. info->queue_index = index;
  339. info->num = num;
  340. info->msix_vector = msix_vec;
  341. size = PAGE_ALIGN(vring_size(num, VIRTIO_PCI_VRING_ALIGN));
  342. info->queue = alloc_pages_exact(size, GFP_KERNEL|__GFP_ZERO);
  343. if (info->queue == NULL) {
  344. err = -ENOMEM;
  345. goto out_info;
  346. }
  347. /* activate the queue */
  348. iowrite32(virt_to_phys(info->queue) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT,
  349. vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
  350. /* create the vring */
  351. vq = vring_new_virtqueue(info->num, VIRTIO_PCI_VRING_ALIGN,
  352. vdev, info->queue, vp_notify, callback, name);
  353. if (!vq) {
  354. err = -ENOMEM;
  355. goto out_activate_queue;
  356. }
  357. vq->priv = info;
  358. info->vq = vq;
  359. if (msix_vec != VIRTIO_MSI_NO_VECTOR) {
  360. iowrite16(msix_vec, vp_dev->ioaddr + VIRTIO_MSI_QUEUE_VECTOR);
  361. msix_vec = ioread16(vp_dev->ioaddr + VIRTIO_MSI_QUEUE_VECTOR);
  362. if (msix_vec == VIRTIO_MSI_NO_VECTOR) {
  363. err = -EBUSY;
  364. goto out_assign;
  365. }
  366. }
  367. if (callback) {
  368. spin_lock_irqsave(&vp_dev->lock, flags);
  369. list_add(&info->node, &vp_dev->virtqueues);
  370. spin_unlock_irqrestore(&vp_dev->lock, flags);
  371. } else {
  372. INIT_LIST_HEAD(&info->node);
  373. }
  374. return vq;
  375. out_assign:
  376. vring_del_virtqueue(vq);
  377. out_activate_queue:
  378. iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
  379. free_pages_exact(info->queue, size);
  380. out_info:
  381. kfree(info);
  382. return ERR_PTR(err);
  383. }
  384. static void vp_del_vq(struct virtqueue *vq)
  385. {
  386. struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
  387. struct virtio_pci_vq_info *info = vq->priv;
  388. unsigned long flags, size;
  389. spin_lock_irqsave(&vp_dev->lock, flags);
  390. list_del(&info->node);
  391. spin_unlock_irqrestore(&vp_dev->lock, flags);
  392. iowrite16(info->queue_index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL);
  393. if (vp_dev->msix_enabled) {
  394. iowrite16(VIRTIO_MSI_NO_VECTOR,
  395. vp_dev->ioaddr + VIRTIO_MSI_QUEUE_VECTOR);
  396. /* Flush the write out to device */
  397. ioread8(vp_dev->ioaddr + VIRTIO_PCI_ISR);
  398. }
  399. vring_del_virtqueue(vq);
  400. /* Select and deactivate the queue */
  401. iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
  402. size = PAGE_ALIGN(vring_size(info->num, VIRTIO_PCI_VRING_ALIGN));
  403. free_pages_exact(info->queue, size);
  404. kfree(info);
  405. }
  406. /* the config->del_vqs() implementation */
  407. static void vp_del_vqs(struct virtio_device *vdev)
  408. {
  409. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  410. struct virtqueue *vq, *n;
  411. struct virtio_pci_vq_info *info;
  412. list_for_each_entry_safe(vq, n, &vdev->vqs, list) {
  413. info = vq->priv;
  414. if (vp_dev->per_vq_vectors &&
  415. info->msix_vector != VIRTIO_MSI_NO_VECTOR)
  416. free_irq(vp_dev->msix_entries[info->msix_vector].vector,
  417. vq);
  418. vp_del_vq(vq);
  419. }
  420. vp_dev->per_vq_vectors = false;
  421. vp_free_vectors(vdev);
  422. }
  423. static int vp_try_to_find_vqs(struct virtio_device *vdev, unsigned nvqs,
  424. struct virtqueue *vqs[],
  425. vq_callback_t *callbacks[],
  426. const char *names[],
  427. bool use_msix,
  428. bool per_vq_vectors)
  429. {
  430. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  431. u16 msix_vec;
  432. int i, err, nvectors, allocated_vectors;
  433. if (!use_msix) {
  434. /* Old style: one normal interrupt for change and all vqs. */
  435. err = vp_request_intx(vdev);
  436. if (err)
  437. goto error_request;
  438. } else {
  439. if (per_vq_vectors) {
  440. /* Best option: one for change interrupt, one per vq. */
  441. nvectors = 1;
  442. for (i = 0; i < nvqs; ++i)
  443. if (callbacks[i])
  444. ++nvectors;
  445. } else {
  446. /* Second best: one for change, shared for all vqs. */
  447. nvectors = 2;
  448. }
  449. err = vp_request_msix_vectors(vdev, nvectors, per_vq_vectors);
  450. if (err)
  451. goto error_request;
  452. }
  453. vp_dev->per_vq_vectors = per_vq_vectors;
  454. allocated_vectors = vp_dev->msix_used_vectors;
  455. for (i = 0; i < nvqs; ++i) {
  456. if (!callbacks[i] || !vp_dev->msix_enabled)
  457. msix_vec = VIRTIO_MSI_NO_VECTOR;
  458. else if (vp_dev->per_vq_vectors)
  459. msix_vec = allocated_vectors++;
  460. else
  461. msix_vec = VP_MSIX_VQ_VECTOR;
  462. vqs[i] = setup_vq(vdev, i, callbacks[i], names[i], msix_vec);
  463. if (IS_ERR(vqs[i])) {
  464. err = PTR_ERR(vqs[i]);
  465. goto error_find;
  466. }
  467. if (!vp_dev->per_vq_vectors || msix_vec == VIRTIO_MSI_NO_VECTOR)
  468. continue;
  469. /* allocate per-vq irq if available and necessary */
  470. snprintf(vp_dev->msix_names[msix_vec],
  471. sizeof *vp_dev->msix_names,
  472. "%s-%s",
  473. dev_name(&vp_dev->vdev.dev), names[i]);
  474. err = request_irq(vp_dev->msix_entries[msix_vec].vector,
  475. vring_interrupt, 0,
  476. vp_dev->msix_names[msix_vec],
  477. vqs[i]);
  478. if (err) {
  479. vp_del_vq(vqs[i]);
  480. goto error_find;
  481. }
  482. }
  483. return 0;
  484. error_find:
  485. vp_del_vqs(vdev);
  486. error_request:
  487. return err;
  488. }
  489. /* the config->find_vqs() implementation */
  490. static int vp_find_vqs(struct virtio_device *vdev, unsigned nvqs,
  491. struct virtqueue *vqs[],
  492. vq_callback_t *callbacks[],
  493. const char *names[])
  494. {
  495. int err;
  496. /* Try MSI-X with one vector per queue. */
  497. err = vp_try_to_find_vqs(vdev, nvqs, vqs, callbacks, names, true, true);
  498. if (!err)
  499. return 0;
  500. /* Fallback: MSI-X with one vector for config, one shared for queues. */
  501. err = vp_try_to_find_vqs(vdev, nvqs, vqs, callbacks, names,
  502. true, false);
  503. if (!err)
  504. return 0;
  505. /* Finally fall back to regular interrupts. */
  506. return vp_try_to_find_vqs(vdev, nvqs, vqs, callbacks, names,
  507. false, false);
  508. }
  509. static struct virtio_config_ops virtio_pci_config_ops = {
  510. .get = vp_get,
  511. .set = vp_set,
  512. .get_status = vp_get_status,
  513. .set_status = vp_set_status,
  514. .reset = vp_reset,
  515. .find_vqs = vp_find_vqs,
  516. .del_vqs = vp_del_vqs,
  517. .get_features = vp_get_features,
  518. .finalize_features = vp_finalize_features,
  519. };
  520. static void virtio_pci_release_dev(struct device *_d)
  521. {
  522. /*
  523. * No need for a release method as we allocate/free
  524. * all devices together with the pci devices.
  525. * Provide an empty one to avoid getting a warning from core.
  526. */
  527. }
  528. /* the PCI probing function */
  529. static int __devinit virtio_pci_probe(struct pci_dev *pci_dev,
  530. const struct pci_device_id *id)
  531. {
  532. struct virtio_pci_device *vp_dev;
  533. int err;
  534. /* We only own devices >= 0x1000 and <= 0x103f: leave the rest. */
  535. if (pci_dev->device < 0x1000 || pci_dev->device > 0x103f)
  536. return -ENODEV;
  537. if (pci_dev->revision != VIRTIO_PCI_ABI_VERSION) {
  538. printk(KERN_ERR "virtio_pci: expected ABI version %d, got %d\n",
  539. VIRTIO_PCI_ABI_VERSION, pci_dev->revision);
  540. return -ENODEV;
  541. }
  542. /* allocate our structure and fill it out */
  543. vp_dev = kzalloc(sizeof(struct virtio_pci_device), GFP_KERNEL);
  544. if (vp_dev == NULL)
  545. return -ENOMEM;
  546. vp_dev->vdev.dev.parent = &pci_dev->dev;
  547. vp_dev->vdev.dev.release = virtio_pci_release_dev;
  548. vp_dev->vdev.config = &virtio_pci_config_ops;
  549. vp_dev->pci_dev = pci_dev;
  550. INIT_LIST_HEAD(&vp_dev->virtqueues);
  551. spin_lock_init(&vp_dev->lock);
  552. /* Disable MSI/MSIX to bring device to a known good state. */
  553. pci_msi_off(pci_dev);
  554. /* enable the device */
  555. err = pci_enable_device(pci_dev);
  556. if (err)
  557. goto out;
  558. err = pci_request_regions(pci_dev, "virtio-pci");
  559. if (err)
  560. goto out_enable_device;
  561. vp_dev->ioaddr = pci_iomap(pci_dev, 0, 0);
  562. if (vp_dev->ioaddr == NULL)
  563. goto out_req_regions;
  564. pci_set_drvdata(pci_dev, vp_dev);
  565. pci_set_master(pci_dev);
  566. /* we use the subsystem vendor/device id as the virtio vendor/device
  567. * id. this allows us to use the same PCI vendor/device id for all
  568. * virtio devices and to identify the particular virtio driver by
  569. * the subsystem ids */
  570. vp_dev->vdev.id.vendor = pci_dev->subsystem_vendor;
  571. vp_dev->vdev.id.device = pci_dev->subsystem_device;
  572. /* finally register the virtio device */
  573. err = register_virtio_device(&vp_dev->vdev);
  574. if (err)
  575. goto out_set_drvdata;
  576. return 0;
  577. out_set_drvdata:
  578. pci_set_drvdata(pci_dev, NULL);
  579. pci_iounmap(pci_dev, vp_dev->ioaddr);
  580. out_req_regions:
  581. pci_release_regions(pci_dev);
  582. out_enable_device:
  583. pci_disable_device(pci_dev);
  584. out:
  585. kfree(vp_dev);
  586. return err;
  587. }
  588. static void __devexit virtio_pci_remove(struct pci_dev *pci_dev)
  589. {
  590. struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
  591. unregister_virtio_device(&vp_dev->vdev);
  592. vp_del_vqs(&vp_dev->vdev);
  593. pci_set_drvdata(pci_dev, NULL);
  594. pci_iounmap(pci_dev, vp_dev->ioaddr);
  595. pci_release_regions(pci_dev);
  596. pci_disable_device(pci_dev);
  597. kfree(vp_dev);
  598. }
  599. #ifdef CONFIG_PM
  600. static int virtio_pci_suspend(struct pci_dev *pci_dev, pm_message_t state)
  601. {
  602. pci_save_state(pci_dev);
  603. pci_set_power_state(pci_dev, PCI_D3hot);
  604. return 0;
  605. }
  606. static int virtio_pci_resume(struct pci_dev *pci_dev)
  607. {
  608. pci_restore_state(pci_dev);
  609. pci_set_power_state(pci_dev, PCI_D0);
  610. return 0;
  611. }
  612. #endif
  613. static struct pci_driver virtio_pci_driver = {
  614. .name = "virtio-pci",
  615. .id_table = virtio_pci_id_table,
  616. .probe = virtio_pci_probe,
  617. .remove = __devexit_p(virtio_pci_remove),
  618. #ifdef CONFIG_PM
  619. .suspend = virtio_pci_suspend,
  620. .resume = virtio_pci_resume,
  621. #endif
  622. };
  623. static int __init virtio_pci_init(void)
  624. {
  625. return pci_register_driver(&virtio_pci_driver);
  626. }
  627. module_init(virtio_pci_init);
  628. static void __exit virtio_pci_exit(void)
  629. {
  630. pci_unregister_driver(&virtio_pci_driver);
  631. }
  632. module_exit(virtio_pci_exit);