virtio_pci.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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. /* A PCI device has it's own struct device and so does a virtio device so
  84. * we create a place for the virtio devices to show up in sysfs. I think it
  85. * would make more sense for virtio to not insist on having it's own device. */
  86. static struct device *virtio_pci_root;
  87. /* Convert a generic virtio device to our structure */
  88. static struct virtio_pci_device *to_vp_device(struct virtio_device *vdev)
  89. {
  90. return container_of(vdev, struct virtio_pci_device, vdev);
  91. }
  92. /* virtio config->get_features() implementation */
  93. static u32 vp_get_features(struct virtio_device *vdev)
  94. {
  95. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  96. /* When someone needs more than 32 feature bits, we'll need to
  97. * steal a bit to indicate that the rest are somewhere else. */
  98. return ioread32(vp_dev->ioaddr + VIRTIO_PCI_HOST_FEATURES);
  99. }
  100. /* virtio config->finalize_features() implementation */
  101. static void vp_finalize_features(struct virtio_device *vdev)
  102. {
  103. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  104. /* Give virtio_ring a chance to accept features. */
  105. vring_transport_features(vdev);
  106. /* We only support 32 feature bits. */
  107. BUILD_BUG_ON(ARRAY_SIZE(vdev->features) != 1);
  108. iowrite32(vdev->features[0], vp_dev->ioaddr+VIRTIO_PCI_GUEST_FEATURES);
  109. }
  110. /* virtio config->get() implementation */
  111. static void vp_get(struct virtio_device *vdev, unsigned offset,
  112. void *buf, unsigned len)
  113. {
  114. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  115. void __iomem *ioaddr = vp_dev->ioaddr +
  116. VIRTIO_PCI_CONFIG(vp_dev) + offset;
  117. u8 *ptr = buf;
  118. int i;
  119. for (i = 0; i < len; i++)
  120. ptr[i] = ioread8(ioaddr + i);
  121. }
  122. /* the config->set() implementation. it's symmetric to the config->get()
  123. * implementation */
  124. static void vp_set(struct virtio_device *vdev, unsigned offset,
  125. const void *buf, unsigned len)
  126. {
  127. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  128. void __iomem *ioaddr = vp_dev->ioaddr +
  129. VIRTIO_PCI_CONFIG(vp_dev) + offset;
  130. const u8 *ptr = buf;
  131. int i;
  132. for (i = 0; i < len; i++)
  133. iowrite8(ptr[i], ioaddr + i);
  134. }
  135. /* config->{get,set}_status() implementations */
  136. static u8 vp_get_status(struct virtio_device *vdev)
  137. {
  138. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  139. return ioread8(vp_dev->ioaddr + VIRTIO_PCI_STATUS);
  140. }
  141. static void vp_set_status(struct virtio_device *vdev, u8 status)
  142. {
  143. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  144. /* We should never be setting status to 0. */
  145. BUG_ON(status == 0);
  146. iowrite8(status, vp_dev->ioaddr + VIRTIO_PCI_STATUS);
  147. }
  148. static void vp_reset(struct virtio_device *vdev)
  149. {
  150. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  151. /* 0 status means a reset. */
  152. iowrite8(0, vp_dev->ioaddr + VIRTIO_PCI_STATUS);
  153. }
  154. /* the notify function used when creating a virt queue */
  155. static void vp_notify(struct virtqueue *vq)
  156. {
  157. struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
  158. struct virtio_pci_vq_info *info = vq->priv;
  159. /* we write the queue's selector into the notification register to
  160. * signal the other end */
  161. iowrite16(info->queue_index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NOTIFY);
  162. }
  163. /* Handle a configuration change: Tell driver if it wants to know. */
  164. static irqreturn_t vp_config_changed(int irq, void *opaque)
  165. {
  166. struct virtio_pci_device *vp_dev = opaque;
  167. struct virtio_driver *drv;
  168. drv = container_of(vp_dev->vdev.dev.driver,
  169. struct virtio_driver, driver);
  170. if (drv && drv->config_changed)
  171. drv->config_changed(&vp_dev->vdev);
  172. return IRQ_HANDLED;
  173. }
  174. /* Notify all virtqueues on an interrupt. */
  175. static irqreturn_t vp_vring_interrupt(int irq, void *opaque)
  176. {
  177. struct virtio_pci_device *vp_dev = opaque;
  178. struct virtio_pci_vq_info *info;
  179. irqreturn_t ret = IRQ_NONE;
  180. unsigned long flags;
  181. spin_lock_irqsave(&vp_dev->lock, flags);
  182. list_for_each_entry(info, &vp_dev->virtqueues, node) {
  183. if (vring_interrupt(irq, info->vq) == IRQ_HANDLED)
  184. ret = IRQ_HANDLED;
  185. }
  186. spin_unlock_irqrestore(&vp_dev->lock, flags);
  187. return ret;
  188. }
  189. /* A small wrapper to also acknowledge the interrupt when it's handled.
  190. * I really need an EIO hook for the vring so I can ack the interrupt once we
  191. * know that we'll be handling the IRQ but before we invoke the callback since
  192. * the callback may notify the host which results in the host attempting to
  193. * raise an interrupt that we would then mask once we acknowledged the
  194. * interrupt. */
  195. static irqreturn_t vp_interrupt(int irq, void *opaque)
  196. {
  197. struct virtio_pci_device *vp_dev = opaque;
  198. u8 isr;
  199. /* reading the ISR has the effect of also clearing it so it's very
  200. * important to save off the value. */
  201. isr = ioread8(vp_dev->ioaddr + VIRTIO_PCI_ISR);
  202. /* It's definitely not us if the ISR was not high */
  203. if (!isr)
  204. return IRQ_NONE;
  205. /* Configuration change? Tell driver if it wants to know. */
  206. if (isr & VIRTIO_PCI_ISR_CONFIG)
  207. vp_config_changed(irq, opaque);
  208. return vp_vring_interrupt(irq, opaque);
  209. }
  210. static void vp_free_vectors(struct virtio_device *vdev)
  211. {
  212. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  213. int i;
  214. if (vp_dev->intx_enabled) {
  215. free_irq(vp_dev->pci_dev->irq, vp_dev);
  216. vp_dev->intx_enabled = 0;
  217. }
  218. for (i = 0; i < vp_dev->msix_used_vectors; ++i)
  219. free_irq(vp_dev->msix_entries[i].vector, vp_dev);
  220. if (vp_dev->msix_enabled) {
  221. /* Disable the vector used for configuration */
  222. iowrite16(VIRTIO_MSI_NO_VECTOR,
  223. vp_dev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR);
  224. /* Flush the write out to device */
  225. ioread16(vp_dev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR);
  226. pci_disable_msix(vp_dev->pci_dev);
  227. vp_dev->msix_enabled = 0;
  228. vp_dev->msix_vectors = 0;
  229. }
  230. vp_dev->msix_used_vectors = 0;
  231. kfree(vp_dev->msix_names);
  232. vp_dev->msix_names = NULL;
  233. kfree(vp_dev->msix_entries);
  234. vp_dev->msix_entries = NULL;
  235. }
  236. static int vp_request_msix_vectors(struct virtio_device *vdev, int nvectors,
  237. bool per_vq_vectors)
  238. {
  239. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  240. const char *name = dev_name(&vp_dev->vdev.dev);
  241. unsigned i, v;
  242. int err = -ENOMEM;
  243. vp_dev->msix_entries = kmalloc(nvectors * sizeof *vp_dev->msix_entries,
  244. GFP_KERNEL);
  245. if (!vp_dev->msix_entries)
  246. goto error;
  247. vp_dev->msix_names = kmalloc(nvectors * sizeof *vp_dev->msix_names,
  248. GFP_KERNEL);
  249. if (!vp_dev->msix_names)
  250. goto error;
  251. for (i = 0; i < nvectors; ++i)
  252. vp_dev->msix_entries[i].entry = i;
  253. /* pci_enable_msix returns positive if we can't get this many. */
  254. err = pci_enable_msix(vp_dev->pci_dev, vp_dev->msix_entries, nvectors);
  255. if (err > 0)
  256. err = -ENOSPC;
  257. if (err)
  258. goto error;
  259. vp_dev->msix_vectors = nvectors;
  260. vp_dev->msix_enabled = 1;
  261. /* Set the vector used for configuration */
  262. v = vp_dev->msix_used_vectors;
  263. snprintf(vp_dev->msix_names[v], sizeof *vp_dev->msix_names,
  264. "%s-config", name);
  265. err = request_irq(vp_dev->msix_entries[v].vector,
  266. vp_config_changed, 0, vp_dev->msix_names[v],
  267. vp_dev);
  268. if (err)
  269. goto error;
  270. ++vp_dev->msix_used_vectors;
  271. iowrite16(v, vp_dev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR);
  272. /* Verify we had enough resources to assign the vector */
  273. v = ioread16(vp_dev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR);
  274. if (v == VIRTIO_MSI_NO_VECTOR) {
  275. err = -EBUSY;
  276. goto error;
  277. }
  278. if (!per_vq_vectors) {
  279. /* Shared vector for all VQs */
  280. v = vp_dev->msix_used_vectors;
  281. snprintf(vp_dev->msix_names[v], sizeof *vp_dev->msix_names,
  282. "%s-virtqueues", name);
  283. err = request_irq(vp_dev->msix_entries[v].vector,
  284. vp_vring_interrupt, 0, vp_dev->msix_names[v],
  285. vp_dev);
  286. if (err)
  287. goto error;
  288. ++vp_dev->msix_used_vectors;
  289. }
  290. return 0;
  291. error:
  292. vp_free_vectors(vdev);
  293. return err;
  294. }
  295. static int vp_request_intx(struct virtio_device *vdev)
  296. {
  297. int err;
  298. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  299. err = request_irq(vp_dev->pci_dev->irq, vp_interrupt,
  300. IRQF_SHARED, dev_name(&vdev->dev), vp_dev);
  301. if (!err)
  302. vp_dev->intx_enabled = 1;
  303. return err;
  304. }
  305. static struct virtqueue *setup_vq(struct virtio_device *vdev, unsigned index,
  306. void (*callback)(struct virtqueue *vq),
  307. const char *name,
  308. u16 msix_vec)
  309. {
  310. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  311. struct virtio_pci_vq_info *info;
  312. struct virtqueue *vq;
  313. unsigned long flags, size;
  314. u16 num;
  315. int err;
  316. /* Select the queue we're interested in */
  317. iowrite16(index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL);
  318. /* Check if queue is either not available or already active. */
  319. num = ioread16(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NUM);
  320. if (!num || ioread32(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN))
  321. return ERR_PTR(-ENOENT);
  322. /* allocate and fill out our structure the represents an active
  323. * queue */
  324. info = kmalloc(sizeof(struct virtio_pci_vq_info), GFP_KERNEL);
  325. if (!info)
  326. return ERR_PTR(-ENOMEM);
  327. info->queue_index = index;
  328. info->num = num;
  329. info->msix_vector = msix_vec;
  330. size = PAGE_ALIGN(vring_size(num, VIRTIO_PCI_VRING_ALIGN));
  331. info->queue = alloc_pages_exact(size, GFP_KERNEL|__GFP_ZERO);
  332. if (info->queue == NULL) {
  333. err = -ENOMEM;
  334. goto out_info;
  335. }
  336. /* activate the queue */
  337. iowrite32(virt_to_phys(info->queue) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT,
  338. vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
  339. /* create the vring */
  340. vq = vring_new_virtqueue(info->num, VIRTIO_PCI_VRING_ALIGN,
  341. vdev, info->queue, vp_notify, callback, name);
  342. if (!vq) {
  343. err = -ENOMEM;
  344. goto out_activate_queue;
  345. }
  346. vq->priv = info;
  347. info->vq = vq;
  348. if (msix_vec != VIRTIO_MSI_NO_VECTOR) {
  349. iowrite16(msix_vec, vp_dev->ioaddr + VIRTIO_MSI_QUEUE_VECTOR);
  350. msix_vec = ioread16(vp_dev->ioaddr + VIRTIO_MSI_QUEUE_VECTOR);
  351. if (msix_vec == VIRTIO_MSI_NO_VECTOR) {
  352. err = -EBUSY;
  353. goto out_assign;
  354. }
  355. }
  356. spin_lock_irqsave(&vp_dev->lock, flags);
  357. list_add(&info->node, &vp_dev->virtqueues);
  358. spin_unlock_irqrestore(&vp_dev->lock, flags);
  359. return vq;
  360. out_assign:
  361. vring_del_virtqueue(vq);
  362. out_activate_queue:
  363. iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
  364. free_pages_exact(info->queue, size);
  365. out_info:
  366. kfree(info);
  367. return ERR_PTR(err);
  368. }
  369. static void vp_del_vq(struct virtqueue *vq)
  370. {
  371. struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
  372. struct virtio_pci_vq_info *info = vq->priv;
  373. unsigned long flags, size;
  374. spin_lock_irqsave(&vp_dev->lock, flags);
  375. list_del(&info->node);
  376. spin_unlock_irqrestore(&vp_dev->lock, flags);
  377. iowrite16(info->queue_index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL);
  378. if (vp_dev->msix_enabled) {
  379. iowrite16(VIRTIO_MSI_NO_VECTOR,
  380. vp_dev->ioaddr + VIRTIO_MSI_QUEUE_VECTOR);
  381. /* Flush the write out to device */
  382. ioread8(vp_dev->ioaddr + VIRTIO_PCI_ISR);
  383. }
  384. vring_del_virtqueue(vq);
  385. /* Select and deactivate the queue */
  386. iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
  387. size = PAGE_ALIGN(vring_size(info->num, VIRTIO_PCI_VRING_ALIGN));
  388. free_pages_exact(info->queue, size);
  389. kfree(info);
  390. }
  391. /* the config->del_vqs() implementation */
  392. static void vp_del_vqs(struct virtio_device *vdev)
  393. {
  394. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  395. struct virtqueue *vq, *n;
  396. struct virtio_pci_vq_info *info;
  397. list_for_each_entry_safe(vq, n, &vdev->vqs, list) {
  398. info = vq->priv;
  399. if (vp_dev->per_vq_vectors &&
  400. info->msix_vector != VIRTIO_MSI_NO_VECTOR)
  401. free_irq(vp_dev->msix_entries[info->msix_vector].vector,
  402. vq);
  403. vp_del_vq(vq);
  404. }
  405. vp_dev->per_vq_vectors = false;
  406. vp_free_vectors(vdev);
  407. }
  408. static int vp_try_to_find_vqs(struct virtio_device *vdev, unsigned nvqs,
  409. struct virtqueue *vqs[],
  410. vq_callback_t *callbacks[],
  411. const char *names[],
  412. bool use_msix,
  413. bool per_vq_vectors)
  414. {
  415. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  416. u16 msix_vec;
  417. int i, err, nvectors, allocated_vectors;
  418. if (!use_msix) {
  419. /* Old style: one normal interrupt for change and all vqs. */
  420. err = vp_request_intx(vdev);
  421. if (err)
  422. goto error_request;
  423. } else {
  424. if (per_vq_vectors) {
  425. /* Best option: one for change interrupt, one per vq. */
  426. nvectors = 1;
  427. for (i = 0; i < nvqs; ++i)
  428. if (callbacks[i])
  429. ++nvectors;
  430. } else {
  431. /* Second best: one for change, shared for all vqs. */
  432. nvectors = 2;
  433. }
  434. err = vp_request_msix_vectors(vdev, nvectors, per_vq_vectors);
  435. if (err)
  436. goto error_request;
  437. }
  438. vp_dev->per_vq_vectors = per_vq_vectors;
  439. allocated_vectors = vp_dev->msix_used_vectors;
  440. for (i = 0; i < nvqs; ++i) {
  441. if (!callbacks[i] || !vp_dev->msix_enabled)
  442. msix_vec = VIRTIO_MSI_NO_VECTOR;
  443. else if (vp_dev->per_vq_vectors)
  444. msix_vec = allocated_vectors++;
  445. else
  446. msix_vec = VP_MSIX_VQ_VECTOR;
  447. vqs[i] = setup_vq(vdev, i, callbacks[i], names[i], msix_vec);
  448. if (IS_ERR(vqs[i])) {
  449. err = PTR_ERR(vqs[i]);
  450. goto error_find;
  451. }
  452. if (!vp_dev->per_vq_vectors || msix_vec == VIRTIO_MSI_NO_VECTOR)
  453. continue;
  454. /* allocate per-vq irq if available and necessary */
  455. snprintf(vp_dev->msix_names[msix_vec],
  456. sizeof *vp_dev->msix_names,
  457. "%s-%s",
  458. dev_name(&vp_dev->vdev.dev), names[i]);
  459. err = request_irq(vp_dev->msix_entries[msix_vec].vector,
  460. vring_interrupt, 0,
  461. vp_dev->msix_names[msix_vec],
  462. vqs[i]);
  463. if (err) {
  464. vp_del_vq(vqs[i]);
  465. goto error_find;
  466. }
  467. }
  468. return 0;
  469. error_find:
  470. vp_del_vqs(vdev);
  471. error_request:
  472. return err;
  473. }
  474. /* the config->find_vqs() implementation */
  475. static int vp_find_vqs(struct virtio_device *vdev, unsigned nvqs,
  476. struct virtqueue *vqs[],
  477. vq_callback_t *callbacks[],
  478. const char *names[])
  479. {
  480. int err;
  481. /* Try MSI-X with one vector per queue. */
  482. err = vp_try_to_find_vqs(vdev, nvqs, vqs, callbacks, names, true, true);
  483. if (!err)
  484. return 0;
  485. /* Fallback: MSI-X with one vector for config, one shared for queues. */
  486. err = vp_try_to_find_vqs(vdev, nvqs, vqs, callbacks, names,
  487. true, false);
  488. if (!err)
  489. return 0;
  490. /* Finally fall back to regular interrupts. */
  491. return vp_try_to_find_vqs(vdev, nvqs, vqs, callbacks, names,
  492. false, false);
  493. }
  494. static struct virtio_config_ops virtio_pci_config_ops = {
  495. .get = vp_get,
  496. .set = vp_set,
  497. .get_status = vp_get_status,
  498. .set_status = vp_set_status,
  499. .reset = vp_reset,
  500. .find_vqs = vp_find_vqs,
  501. .del_vqs = vp_del_vqs,
  502. .get_features = vp_get_features,
  503. .finalize_features = vp_finalize_features,
  504. };
  505. static void virtio_pci_release_dev(struct device *_d)
  506. {
  507. struct virtio_device *dev = container_of(_d, struct virtio_device, dev);
  508. struct virtio_pci_device *vp_dev = to_vp_device(dev);
  509. struct pci_dev *pci_dev = vp_dev->pci_dev;
  510. vp_del_vqs(dev);
  511. pci_set_drvdata(pci_dev, NULL);
  512. pci_iounmap(pci_dev, vp_dev->ioaddr);
  513. pci_release_regions(pci_dev);
  514. pci_disable_device(pci_dev);
  515. kfree(vp_dev);
  516. }
  517. /* the PCI probing function */
  518. static int __devinit virtio_pci_probe(struct pci_dev *pci_dev,
  519. const struct pci_device_id *id)
  520. {
  521. struct virtio_pci_device *vp_dev;
  522. int err;
  523. /* We only own devices >= 0x1000 and <= 0x103f: leave the rest. */
  524. if (pci_dev->device < 0x1000 || pci_dev->device > 0x103f)
  525. return -ENODEV;
  526. if (pci_dev->revision != VIRTIO_PCI_ABI_VERSION) {
  527. printk(KERN_ERR "virtio_pci: expected ABI version %d, got %d\n",
  528. VIRTIO_PCI_ABI_VERSION, pci_dev->revision);
  529. return -ENODEV;
  530. }
  531. /* allocate our structure and fill it out */
  532. vp_dev = kzalloc(sizeof(struct virtio_pci_device), GFP_KERNEL);
  533. if (vp_dev == NULL)
  534. return -ENOMEM;
  535. vp_dev->vdev.dev.parent = virtio_pci_root;
  536. vp_dev->vdev.dev.release = virtio_pci_release_dev;
  537. vp_dev->vdev.config = &virtio_pci_config_ops;
  538. vp_dev->pci_dev = pci_dev;
  539. INIT_LIST_HEAD(&vp_dev->virtqueues);
  540. spin_lock_init(&vp_dev->lock);
  541. /* Disable MSI/MSIX to bring device to a known good state. */
  542. pci_msi_off(pci_dev);
  543. /* enable the device */
  544. err = pci_enable_device(pci_dev);
  545. if (err)
  546. goto out;
  547. err = pci_request_regions(pci_dev, "virtio-pci");
  548. if (err)
  549. goto out_enable_device;
  550. vp_dev->ioaddr = pci_iomap(pci_dev, 0, 0);
  551. if (vp_dev->ioaddr == NULL)
  552. goto out_req_regions;
  553. pci_set_drvdata(pci_dev, vp_dev);
  554. pci_set_master(pci_dev);
  555. /* we use the subsystem vendor/device id as the virtio vendor/device
  556. * id. this allows us to use the same PCI vendor/device id for all
  557. * virtio devices and to identify the particular virtio driver by
  558. * the subsystem ids */
  559. vp_dev->vdev.id.vendor = pci_dev->subsystem_vendor;
  560. vp_dev->vdev.id.device = pci_dev->subsystem_device;
  561. /* finally register the virtio device */
  562. err = register_virtio_device(&vp_dev->vdev);
  563. if (err)
  564. goto out_set_drvdata;
  565. return 0;
  566. out_set_drvdata:
  567. pci_set_drvdata(pci_dev, NULL);
  568. pci_iounmap(pci_dev, vp_dev->ioaddr);
  569. out_req_regions:
  570. pci_release_regions(pci_dev);
  571. out_enable_device:
  572. pci_disable_device(pci_dev);
  573. out:
  574. kfree(vp_dev);
  575. return err;
  576. }
  577. static void __devexit virtio_pci_remove(struct pci_dev *pci_dev)
  578. {
  579. struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
  580. unregister_virtio_device(&vp_dev->vdev);
  581. }
  582. #ifdef CONFIG_PM
  583. static int virtio_pci_suspend(struct pci_dev *pci_dev, pm_message_t state)
  584. {
  585. pci_save_state(pci_dev);
  586. pci_set_power_state(pci_dev, PCI_D3hot);
  587. return 0;
  588. }
  589. static int virtio_pci_resume(struct pci_dev *pci_dev)
  590. {
  591. pci_restore_state(pci_dev);
  592. pci_set_power_state(pci_dev, PCI_D0);
  593. return 0;
  594. }
  595. #endif
  596. static struct pci_driver virtio_pci_driver = {
  597. .name = "virtio-pci",
  598. .id_table = virtio_pci_id_table,
  599. .probe = virtio_pci_probe,
  600. .remove = __devexit_p(virtio_pci_remove),
  601. #ifdef CONFIG_PM
  602. .suspend = virtio_pci_suspend,
  603. .resume = virtio_pci_resume,
  604. #endif
  605. };
  606. static int __init virtio_pci_init(void)
  607. {
  608. int err;
  609. virtio_pci_root = root_device_register("virtio-pci");
  610. if (IS_ERR(virtio_pci_root))
  611. return PTR_ERR(virtio_pci_root);
  612. err = pci_register_driver(&virtio_pci_driver);
  613. if (err)
  614. root_device_unregister(virtio_pci_root);
  615. return err;
  616. }
  617. module_init(virtio_pci_init);
  618. static void __exit virtio_pci_exit(void)
  619. {
  620. pci_unregister_driver(&virtio_pci_driver);
  621. root_device_unregister(virtio_pci_root);
  622. }
  623. module_exit(virtio_pci_exit);