virtio_pci.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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. free_irq(vp_dev->msix_entries[info->msix_vector].vector,
  400. vq);
  401. vp_del_vq(vq);
  402. }
  403. vp_dev->per_vq_vectors = false;
  404. vp_free_vectors(vdev);
  405. }
  406. static int vp_try_to_find_vqs(struct virtio_device *vdev, unsigned nvqs,
  407. struct virtqueue *vqs[],
  408. vq_callback_t *callbacks[],
  409. const char *names[],
  410. bool use_msix,
  411. bool per_vq_vectors)
  412. {
  413. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  414. u16 msix_vec;
  415. int i, err, nvectors, allocated_vectors;
  416. if (!use_msix) {
  417. /* Old style: one normal interrupt for change and all vqs. */
  418. err = vp_request_intx(vdev);
  419. if (err)
  420. goto error_request;
  421. } else {
  422. if (per_vq_vectors) {
  423. /* Best option: one for change interrupt, one per vq. */
  424. nvectors = 1;
  425. for (i = 0; i < nvqs; ++i)
  426. if (callbacks[i])
  427. ++nvectors;
  428. } else {
  429. /* Second best: one for change, shared for all vqs. */
  430. nvectors = 2;
  431. }
  432. err = vp_request_msix_vectors(vdev, nvectors, per_vq_vectors);
  433. if (err)
  434. goto error_request;
  435. }
  436. vp_dev->per_vq_vectors = per_vq_vectors;
  437. allocated_vectors = vp_dev->msix_used_vectors;
  438. for (i = 0; i < nvqs; ++i) {
  439. if (!callbacks[i] || !vp_dev->msix_enabled)
  440. msix_vec = VIRTIO_MSI_NO_VECTOR;
  441. else if (vp_dev->per_vq_vectors)
  442. msix_vec = allocated_vectors++;
  443. else
  444. msix_vec = VP_MSIX_VQ_VECTOR;
  445. vqs[i] = setup_vq(vdev, i, callbacks[i], names[i], msix_vec);
  446. if (IS_ERR(vqs[i])) {
  447. err = PTR_ERR(vqs[i]);
  448. goto error_find;
  449. }
  450. if (!vp_dev->per_vq_vectors || msix_vec == VIRTIO_MSI_NO_VECTOR)
  451. continue;
  452. /* allocate per-vq irq if available and necessary */
  453. snprintf(vp_dev->msix_names[msix_vec],
  454. sizeof *vp_dev->msix_names,
  455. "%s-%s",
  456. dev_name(&vp_dev->vdev.dev), names[i]);
  457. err = request_irq(vp_dev->msix_entries[msix_vec].vector,
  458. vring_interrupt, 0,
  459. vp_dev->msix_names[msix_vec],
  460. vqs[i]);
  461. if (err) {
  462. vp_del_vq(vqs[i]);
  463. goto error_find;
  464. }
  465. }
  466. return 0;
  467. error_find:
  468. vp_del_vqs(vdev);
  469. error_request:
  470. return err;
  471. }
  472. /* the config->find_vqs() implementation */
  473. static int vp_find_vqs(struct virtio_device *vdev, unsigned nvqs,
  474. struct virtqueue *vqs[],
  475. vq_callback_t *callbacks[],
  476. const char *names[])
  477. {
  478. int err;
  479. /* Try MSI-X with one vector per queue. */
  480. err = vp_try_to_find_vqs(vdev, nvqs, vqs, callbacks, names, true, true);
  481. if (!err)
  482. return 0;
  483. /* Fallback: MSI-X with one vector for config, one shared for queues. */
  484. err = vp_try_to_find_vqs(vdev, nvqs, vqs, callbacks, names,
  485. true, false);
  486. if (!err)
  487. return 0;
  488. /* Finally fall back to regular interrupts. */
  489. return vp_try_to_find_vqs(vdev, nvqs, vqs, callbacks, names,
  490. false, false);
  491. }
  492. static struct virtio_config_ops virtio_pci_config_ops = {
  493. .get = vp_get,
  494. .set = vp_set,
  495. .get_status = vp_get_status,
  496. .set_status = vp_set_status,
  497. .reset = vp_reset,
  498. .find_vqs = vp_find_vqs,
  499. .del_vqs = vp_del_vqs,
  500. .get_features = vp_get_features,
  501. .finalize_features = vp_finalize_features,
  502. };
  503. static void virtio_pci_release_dev(struct device *_d)
  504. {
  505. struct virtio_device *dev = container_of(_d, struct virtio_device, dev);
  506. struct virtio_pci_device *vp_dev = to_vp_device(dev);
  507. struct pci_dev *pci_dev = vp_dev->pci_dev;
  508. vp_del_vqs(dev);
  509. pci_set_drvdata(pci_dev, NULL);
  510. pci_iounmap(pci_dev, vp_dev->ioaddr);
  511. pci_release_regions(pci_dev);
  512. pci_disable_device(pci_dev);
  513. kfree(vp_dev);
  514. }
  515. /* the PCI probing function */
  516. static int __devinit virtio_pci_probe(struct pci_dev *pci_dev,
  517. const struct pci_device_id *id)
  518. {
  519. struct virtio_pci_device *vp_dev;
  520. int err;
  521. /* We only own devices >= 0x1000 and <= 0x103f: leave the rest. */
  522. if (pci_dev->device < 0x1000 || pci_dev->device > 0x103f)
  523. return -ENODEV;
  524. if (pci_dev->revision != VIRTIO_PCI_ABI_VERSION) {
  525. printk(KERN_ERR "virtio_pci: expected ABI version %d, got %d\n",
  526. VIRTIO_PCI_ABI_VERSION, pci_dev->revision);
  527. return -ENODEV;
  528. }
  529. /* allocate our structure and fill it out */
  530. vp_dev = kzalloc(sizeof(struct virtio_pci_device), GFP_KERNEL);
  531. if (vp_dev == NULL)
  532. return -ENOMEM;
  533. vp_dev->vdev.dev.parent = virtio_pci_root;
  534. vp_dev->vdev.dev.release = virtio_pci_release_dev;
  535. vp_dev->vdev.config = &virtio_pci_config_ops;
  536. vp_dev->pci_dev = pci_dev;
  537. INIT_LIST_HEAD(&vp_dev->virtqueues);
  538. spin_lock_init(&vp_dev->lock);
  539. /* enable the device */
  540. err = pci_enable_device(pci_dev);
  541. if (err)
  542. goto out;
  543. err = pci_request_regions(pci_dev, "virtio-pci");
  544. if (err)
  545. goto out_enable_device;
  546. vp_dev->ioaddr = pci_iomap(pci_dev, 0, 0);
  547. if (vp_dev->ioaddr == NULL)
  548. goto out_req_regions;
  549. pci_set_drvdata(pci_dev, vp_dev);
  550. /* we use the subsystem vendor/device id as the virtio vendor/device
  551. * id. this allows us to use the same PCI vendor/device id for all
  552. * virtio devices and to identify the particular virtio driver by
  553. * the subsytem ids */
  554. vp_dev->vdev.id.vendor = pci_dev->subsystem_vendor;
  555. vp_dev->vdev.id.device = pci_dev->subsystem_device;
  556. /* finally register the virtio device */
  557. err = register_virtio_device(&vp_dev->vdev);
  558. if (err)
  559. goto out_set_drvdata;
  560. return 0;
  561. out_set_drvdata:
  562. pci_set_drvdata(pci_dev, NULL);
  563. pci_iounmap(pci_dev, vp_dev->ioaddr);
  564. out_req_regions:
  565. pci_release_regions(pci_dev);
  566. out_enable_device:
  567. pci_disable_device(pci_dev);
  568. out:
  569. kfree(vp_dev);
  570. return err;
  571. }
  572. static void __devexit virtio_pci_remove(struct pci_dev *pci_dev)
  573. {
  574. struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
  575. unregister_virtio_device(&vp_dev->vdev);
  576. }
  577. #ifdef CONFIG_PM
  578. static int virtio_pci_suspend(struct pci_dev *pci_dev, pm_message_t state)
  579. {
  580. pci_save_state(pci_dev);
  581. pci_set_power_state(pci_dev, PCI_D3hot);
  582. return 0;
  583. }
  584. static int virtio_pci_resume(struct pci_dev *pci_dev)
  585. {
  586. pci_restore_state(pci_dev);
  587. pci_set_power_state(pci_dev, PCI_D0);
  588. return 0;
  589. }
  590. #endif
  591. static struct pci_driver virtio_pci_driver = {
  592. .name = "virtio-pci",
  593. .id_table = virtio_pci_id_table,
  594. .probe = virtio_pci_probe,
  595. .remove = virtio_pci_remove,
  596. #ifdef CONFIG_PM
  597. .suspend = virtio_pci_suspend,
  598. .resume = virtio_pci_resume,
  599. #endif
  600. };
  601. static int __init virtio_pci_init(void)
  602. {
  603. int err;
  604. virtio_pci_root = root_device_register("virtio-pci");
  605. if (IS_ERR(virtio_pci_root))
  606. return PTR_ERR(virtio_pci_root);
  607. err = pci_register_driver(&virtio_pci_driver);
  608. if (err)
  609. root_device_unregister(virtio_pci_root);
  610. return err;
  611. }
  612. module_init(virtio_pci_init);
  613. static void __exit virtio_pci_exit(void)
  614. {
  615. pci_unregister_driver(&virtio_pci_driver);
  616. root_device_unregister(virtio_pci_root);
  617. }
  618. module_exit(virtio_pci_exit);