virtio_pci.c 18 KB

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