virtio_pci.c 20 KB

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