virtio_pci.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. /*
  2. * Virtio PCI driver
  3. *
  4. * This module allows virtio devices to be used over a virtual PCI device.
  5. * This can be used with QEMU based VMMs like KVM or Xen.
  6. *
  7. * Copyright IBM Corp. 2007
  8. *
  9. * Authors:
  10. * Anthony Liguori <aliguori@us.ibm.com>
  11. *
  12. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  13. * See the COPYING file in the top-level directory.
  14. *
  15. */
  16. #include <linux/module.h>
  17. #include <linux/list.h>
  18. #include <linux/pci.h>
  19. #include <linux/slab.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/virtio.h>
  22. #include <linux/virtio_config.h>
  23. #include <linux/virtio_ring.h>
  24. #include <linux/virtio_pci.h>
  25. #include <linux/highmem.h>
  26. #include <linux/spinlock.h>
  27. MODULE_AUTHOR("Anthony Liguori <aliguori@us.ibm.com>");
  28. MODULE_DESCRIPTION("virtio-pci");
  29. MODULE_LICENSE("GPL");
  30. MODULE_VERSION("1");
  31. /* Our device structure */
  32. struct virtio_pci_device
  33. {
  34. struct virtio_device vdev;
  35. struct pci_dev *pci_dev;
  36. /* the IO mapping for the PCI config space */
  37. void __iomem *ioaddr;
  38. /* a list of queues so we can dispatch IRQs */
  39. spinlock_t lock;
  40. struct list_head virtqueues;
  41. /* MSI-X support */
  42. int msix_enabled;
  43. int intx_enabled;
  44. struct msix_entry *msix_entries;
  45. /* Name strings for interrupts. This size should be enough,
  46. * and I'm too lazy to allocate each name separately. */
  47. char (*msix_names)[256];
  48. /* Number of available vectors */
  49. unsigned msix_vectors;
  50. /* Vectors allocated, excluding per-vq vectors if any */
  51. unsigned msix_used_vectors;
  52. /* Whether we have vector per vq */
  53. bool per_vq_vectors;
  54. };
  55. /* Constants for MSI-X */
  56. /* Use first vector for configuration changes, second and the rest for
  57. * virtqueues Thus, we need at least 2 vectors for MSI. */
  58. enum {
  59. VP_MSIX_CONFIG_VECTOR = 0,
  60. VP_MSIX_VQ_VECTOR = 1,
  61. };
  62. struct virtio_pci_vq_info
  63. {
  64. /* the actual virtqueue */
  65. struct virtqueue *vq;
  66. /* the number of entries in the queue */
  67. int num;
  68. /* the index of the queue */
  69. int queue_index;
  70. /* the virtual address of the ring queue */
  71. void *queue;
  72. /* the list node for the virtqueues list */
  73. struct list_head node;
  74. /* MSI-X vector (or none) */
  75. unsigned msix_vector;
  76. };
  77. /* Qumranet donated their vendor ID for devices 0x1000 thru 0x10FF. */
  78. static struct pci_device_id virtio_pci_id_table[] = {
  79. { 0x1af4, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  80. { 0 },
  81. };
  82. MODULE_DEVICE_TABLE(pci, virtio_pci_id_table);
  83. /* Convert a generic virtio device to our structure */
  84. static struct virtio_pci_device *to_vp_device(struct virtio_device *vdev)
  85. {
  86. return container_of(vdev, struct virtio_pci_device, vdev);
  87. }
  88. /* virtio config->get_features() implementation */
  89. static u32 vp_get_features(struct virtio_device *vdev)
  90. {
  91. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  92. /* When someone needs more than 32 feature bits, we'll need to
  93. * steal a bit to indicate that the rest are somewhere else. */
  94. return ioread32(vp_dev->ioaddr + VIRTIO_PCI_HOST_FEATURES);
  95. }
  96. /* virtio config->finalize_features() implementation */
  97. static void vp_finalize_features(struct virtio_device *vdev)
  98. {
  99. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  100. /* Give virtio_ring a chance to accept features. */
  101. vring_transport_features(vdev);
  102. /* We only support 32 feature bits. */
  103. BUILD_BUG_ON(ARRAY_SIZE(vdev->features) != 1);
  104. iowrite32(vdev->features[0], vp_dev->ioaddr+VIRTIO_PCI_GUEST_FEATURES);
  105. }
  106. /* virtio config->get() implementation */
  107. static void vp_get(struct virtio_device *vdev, unsigned offset,
  108. void *buf, unsigned len)
  109. {
  110. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  111. void __iomem *ioaddr = vp_dev->ioaddr +
  112. VIRTIO_PCI_CONFIG(vp_dev) + offset;
  113. u8 *ptr = buf;
  114. int i;
  115. for (i = 0; i < len; i++)
  116. ptr[i] = ioread8(ioaddr + i);
  117. }
  118. /* the config->set() implementation. it's symmetric to the config->get()
  119. * implementation */
  120. static void vp_set(struct virtio_device *vdev, unsigned offset,
  121. const void *buf, unsigned len)
  122. {
  123. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  124. void __iomem *ioaddr = vp_dev->ioaddr +
  125. VIRTIO_PCI_CONFIG(vp_dev) + offset;
  126. const u8 *ptr = buf;
  127. int i;
  128. for (i = 0; i < len; i++)
  129. iowrite8(ptr[i], ioaddr + i);
  130. }
  131. /* config->{get,set}_status() implementations */
  132. static u8 vp_get_status(struct virtio_device *vdev)
  133. {
  134. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  135. return ioread8(vp_dev->ioaddr + VIRTIO_PCI_STATUS);
  136. }
  137. static void vp_set_status(struct virtio_device *vdev, u8 status)
  138. {
  139. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  140. /* We should never be setting status to 0. */
  141. BUG_ON(status == 0);
  142. iowrite8(status, vp_dev->ioaddr + VIRTIO_PCI_STATUS);
  143. }
  144. static void vp_reset(struct virtio_device *vdev)
  145. {
  146. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  147. /* 0 status means a reset. */
  148. iowrite8(0, vp_dev->ioaddr + VIRTIO_PCI_STATUS);
  149. }
  150. /* the notify function used when creating a virt queue */
  151. static void vp_notify(struct virtqueue *vq)
  152. {
  153. struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
  154. struct virtio_pci_vq_info *info = vq->priv;
  155. /* we write the queue's selector into the notification register to
  156. * signal the other end */
  157. iowrite16(info->queue_index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NOTIFY);
  158. }
  159. /* Handle a configuration change: Tell driver if it wants to know. */
  160. static irqreturn_t vp_config_changed(int irq, void *opaque)
  161. {
  162. struct virtio_pci_device *vp_dev = opaque;
  163. struct virtio_driver *drv;
  164. drv = container_of(vp_dev->vdev.dev.driver,
  165. struct virtio_driver, driver);
  166. if (drv && drv->config_changed)
  167. drv->config_changed(&vp_dev->vdev);
  168. return IRQ_HANDLED;
  169. }
  170. /* Notify all virtqueues on an interrupt. */
  171. static irqreturn_t vp_vring_interrupt(int irq, void *opaque)
  172. {
  173. struct virtio_pci_device *vp_dev = opaque;
  174. struct virtio_pci_vq_info *info;
  175. irqreturn_t ret = IRQ_NONE;
  176. unsigned long flags;
  177. spin_lock_irqsave(&vp_dev->lock, flags);
  178. list_for_each_entry(info, &vp_dev->virtqueues, node) {
  179. if (vring_interrupt(irq, info->vq) == IRQ_HANDLED)
  180. ret = IRQ_HANDLED;
  181. }
  182. spin_unlock_irqrestore(&vp_dev->lock, flags);
  183. return ret;
  184. }
  185. /* A small wrapper to also acknowledge the interrupt when it's handled.
  186. * I really need an EIO hook for the vring so I can ack the interrupt once we
  187. * know that we'll be handling the IRQ but before we invoke the callback since
  188. * the callback may notify the host which results in the host attempting to
  189. * raise an interrupt that we would then mask once we acknowledged the
  190. * interrupt. */
  191. static irqreturn_t vp_interrupt(int irq, void *opaque)
  192. {
  193. struct virtio_pci_device *vp_dev = opaque;
  194. u8 isr;
  195. /* reading the ISR has the effect of also clearing it so it's very
  196. * important to save off the value. */
  197. isr = ioread8(vp_dev->ioaddr + VIRTIO_PCI_ISR);
  198. /* It's definitely not us if the ISR was not high */
  199. if (!isr)
  200. return IRQ_NONE;
  201. /* Configuration change? Tell driver if it wants to know. */
  202. if (isr & VIRTIO_PCI_ISR_CONFIG)
  203. vp_config_changed(irq, opaque);
  204. return vp_vring_interrupt(irq, opaque);
  205. }
  206. static void vp_free_vectors(struct virtio_device *vdev)
  207. {
  208. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  209. int i;
  210. if (vp_dev->intx_enabled) {
  211. free_irq(vp_dev->pci_dev->irq, vp_dev);
  212. vp_dev->intx_enabled = 0;
  213. }
  214. for (i = 0; i < vp_dev->msix_used_vectors; ++i)
  215. free_irq(vp_dev->msix_entries[i].vector, vp_dev);
  216. if (vp_dev->msix_enabled) {
  217. /* Disable the vector used for configuration */
  218. iowrite16(VIRTIO_MSI_NO_VECTOR,
  219. vp_dev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR);
  220. /* Flush the write out to device */
  221. ioread16(vp_dev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR);
  222. pci_disable_msix(vp_dev->pci_dev);
  223. vp_dev->msix_enabled = 0;
  224. vp_dev->msix_vectors = 0;
  225. }
  226. vp_dev->msix_used_vectors = 0;
  227. kfree(vp_dev->msix_names);
  228. vp_dev->msix_names = NULL;
  229. kfree(vp_dev->msix_entries);
  230. vp_dev->msix_entries = NULL;
  231. }
  232. static int vp_request_msix_vectors(struct virtio_device *vdev, int nvectors,
  233. bool per_vq_vectors)
  234. {
  235. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  236. const char *name = dev_name(&vp_dev->vdev.dev);
  237. unsigned i, v;
  238. int err = -ENOMEM;
  239. vp_dev->msix_entries = kmalloc(nvectors * sizeof *vp_dev->msix_entries,
  240. GFP_KERNEL);
  241. if (!vp_dev->msix_entries)
  242. goto error;
  243. vp_dev->msix_names = kmalloc(nvectors * sizeof *vp_dev->msix_names,
  244. GFP_KERNEL);
  245. if (!vp_dev->msix_names)
  246. goto error;
  247. for (i = 0; i < nvectors; ++i)
  248. vp_dev->msix_entries[i].entry = i;
  249. /* pci_enable_msix returns positive if we can't get this many. */
  250. err = pci_enable_msix(vp_dev->pci_dev, vp_dev->msix_entries, nvectors);
  251. if (err > 0)
  252. err = -ENOSPC;
  253. if (err)
  254. goto error;
  255. vp_dev->msix_vectors = nvectors;
  256. vp_dev->msix_enabled = 1;
  257. /* Set the vector used for configuration */
  258. v = vp_dev->msix_used_vectors;
  259. snprintf(vp_dev->msix_names[v], sizeof *vp_dev->msix_names,
  260. "%s-config", name);
  261. err = request_irq(vp_dev->msix_entries[v].vector,
  262. vp_config_changed, 0, vp_dev->msix_names[v],
  263. vp_dev);
  264. if (err)
  265. goto error;
  266. ++vp_dev->msix_used_vectors;
  267. iowrite16(v, vp_dev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR);
  268. /* Verify we had enough resources to assign the vector */
  269. v = ioread16(vp_dev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR);
  270. if (v == VIRTIO_MSI_NO_VECTOR) {
  271. err = -EBUSY;
  272. goto error;
  273. }
  274. if (!per_vq_vectors) {
  275. /* Shared vector for all VQs */
  276. v = vp_dev->msix_used_vectors;
  277. snprintf(vp_dev->msix_names[v], sizeof *vp_dev->msix_names,
  278. "%s-virtqueues", name);
  279. err = request_irq(vp_dev->msix_entries[v].vector,
  280. vp_vring_interrupt, 0, vp_dev->msix_names[v],
  281. vp_dev);
  282. if (err)
  283. goto error;
  284. ++vp_dev->msix_used_vectors;
  285. }
  286. return 0;
  287. error:
  288. vp_free_vectors(vdev);
  289. return err;
  290. }
  291. static int vp_request_intx(struct virtio_device *vdev)
  292. {
  293. int err;
  294. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  295. err = request_irq(vp_dev->pci_dev->irq, vp_interrupt,
  296. IRQF_SHARED, dev_name(&vdev->dev), vp_dev);
  297. if (!err)
  298. vp_dev->intx_enabled = 1;
  299. return err;
  300. }
  301. static struct virtqueue *setup_vq(struct virtio_device *vdev, unsigned index,
  302. void (*callback)(struct virtqueue *vq),
  303. const char *name,
  304. u16 msix_vec)
  305. {
  306. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  307. struct virtio_pci_vq_info *info;
  308. struct virtqueue *vq;
  309. unsigned long flags, size;
  310. u16 num;
  311. int err;
  312. /* Select the queue we're interested in */
  313. iowrite16(index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL);
  314. /* Check if queue is either not available or already active. */
  315. num = ioread16(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NUM);
  316. if (!num || ioread32(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN))
  317. return ERR_PTR(-ENOENT);
  318. /* allocate and fill out our structure the represents an active
  319. * queue */
  320. info = kmalloc(sizeof(struct virtio_pci_vq_info), GFP_KERNEL);
  321. if (!info)
  322. return ERR_PTR(-ENOMEM);
  323. info->queue_index = index;
  324. info->num = num;
  325. info->msix_vector = msix_vec;
  326. size = PAGE_ALIGN(vring_size(num, VIRTIO_PCI_VRING_ALIGN));
  327. info->queue = alloc_pages_exact(size, GFP_KERNEL|__GFP_ZERO);
  328. if (info->queue == NULL) {
  329. err = -ENOMEM;
  330. goto out_info;
  331. }
  332. /* activate the queue */
  333. iowrite32(virt_to_phys(info->queue) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT,
  334. vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
  335. /* create the vring */
  336. vq = vring_new_virtqueue(info->num, VIRTIO_PCI_VRING_ALIGN,
  337. vdev, info->queue, vp_notify, callback, name);
  338. if (!vq) {
  339. err = -ENOMEM;
  340. goto out_activate_queue;
  341. }
  342. vq->priv = info;
  343. info->vq = vq;
  344. if (msix_vec != VIRTIO_MSI_NO_VECTOR) {
  345. iowrite16(msix_vec, vp_dev->ioaddr + VIRTIO_MSI_QUEUE_VECTOR);
  346. msix_vec = ioread16(vp_dev->ioaddr + VIRTIO_MSI_QUEUE_VECTOR);
  347. if (msix_vec == VIRTIO_MSI_NO_VECTOR) {
  348. err = -EBUSY;
  349. goto out_assign;
  350. }
  351. }
  352. if (callback) {
  353. spin_lock_irqsave(&vp_dev->lock, flags);
  354. list_add(&info->node, &vp_dev->virtqueues);
  355. spin_unlock_irqrestore(&vp_dev->lock, flags);
  356. } else {
  357. INIT_LIST_HEAD(&info->node);
  358. }
  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 const char *vp_bus_name(struct virtio_device *vdev)
  495. {
  496. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  497. return pci_name(vp_dev->pci_dev);
  498. }
  499. static struct virtio_config_ops virtio_pci_config_ops = {
  500. .get = vp_get,
  501. .set = vp_set,
  502. .get_status = vp_get_status,
  503. .set_status = vp_set_status,
  504. .reset = vp_reset,
  505. .find_vqs = vp_find_vqs,
  506. .del_vqs = vp_del_vqs,
  507. .get_features = vp_get_features,
  508. .finalize_features = vp_finalize_features,
  509. .bus_name = vp_bus_name,
  510. };
  511. static void virtio_pci_release_dev(struct device *_d)
  512. {
  513. struct virtio_device *dev = container_of(_d, struct virtio_device,
  514. dev);
  515. struct virtio_pci_device *vp_dev = to_vp_device(dev);
  516. kfree(vp_dev);
  517. }
  518. /* the PCI probing function */
  519. static int __devinit virtio_pci_probe(struct pci_dev *pci_dev,
  520. const struct pci_device_id *id)
  521. {
  522. struct virtio_pci_device *vp_dev;
  523. int err;
  524. /* We only own devices >= 0x1000 and <= 0x103f: leave the rest. */
  525. if (pci_dev->device < 0x1000 || pci_dev->device > 0x103f)
  526. return -ENODEV;
  527. if (pci_dev->revision != VIRTIO_PCI_ABI_VERSION) {
  528. printk(KERN_ERR "virtio_pci: expected ABI version %d, got %d\n",
  529. VIRTIO_PCI_ABI_VERSION, pci_dev->revision);
  530. return -ENODEV;
  531. }
  532. /* allocate our structure and fill it out */
  533. vp_dev = kzalloc(sizeof(struct virtio_pci_device), GFP_KERNEL);
  534. if (vp_dev == NULL)
  535. return -ENOMEM;
  536. vp_dev->vdev.dev.parent = &pci_dev->dev;
  537. vp_dev->vdev.dev.release = virtio_pci_release_dev;
  538. vp_dev->vdev.config = &virtio_pci_config_ops;
  539. vp_dev->pci_dev = pci_dev;
  540. INIT_LIST_HEAD(&vp_dev->virtqueues);
  541. spin_lock_init(&vp_dev->lock);
  542. /* Disable MSI/MSIX to bring device to a known good state. */
  543. pci_msi_off(pci_dev);
  544. /* enable the device */
  545. err = pci_enable_device(pci_dev);
  546. if (err)
  547. goto out;
  548. err = pci_request_regions(pci_dev, "virtio-pci");
  549. if (err)
  550. goto out_enable_device;
  551. vp_dev->ioaddr = pci_iomap(pci_dev, 0, 0);
  552. if (vp_dev->ioaddr == NULL)
  553. goto out_req_regions;
  554. pci_set_drvdata(pci_dev, vp_dev);
  555. pci_set_master(pci_dev);
  556. /* we use the subsystem vendor/device id as the virtio vendor/device
  557. * id. this allows us to use the same PCI vendor/device id for all
  558. * virtio devices and to identify the particular virtio driver by
  559. * the subsystem ids */
  560. vp_dev->vdev.id.vendor = pci_dev->subsystem_vendor;
  561. vp_dev->vdev.id.device = pci_dev->subsystem_device;
  562. /* finally register the virtio device */
  563. err = register_virtio_device(&vp_dev->vdev);
  564. if (err)
  565. goto out_set_drvdata;
  566. return 0;
  567. out_set_drvdata:
  568. pci_set_drvdata(pci_dev, NULL);
  569. pci_iounmap(pci_dev, vp_dev->ioaddr);
  570. out_req_regions:
  571. pci_release_regions(pci_dev);
  572. out_enable_device:
  573. pci_disable_device(pci_dev);
  574. out:
  575. kfree(vp_dev);
  576. return err;
  577. }
  578. static void __devexit virtio_pci_remove(struct pci_dev *pci_dev)
  579. {
  580. struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
  581. unregister_virtio_device(&vp_dev->vdev);
  582. vp_del_vqs(&vp_dev->vdev);
  583. pci_set_drvdata(pci_dev, NULL);
  584. pci_iounmap(pci_dev, vp_dev->ioaddr);
  585. pci_release_regions(pci_dev);
  586. pci_disable_device(pci_dev);
  587. }
  588. #ifdef CONFIG_PM
  589. static int virtio_pci_suspend(struct pci_dev *pci_dev, pm_message_t state)
  590. {
  591. pci_save_state(pci_dev);
  592. pci_set_power_state(pci_dev, PCI_D3hot);
  593. return 0;
  594. }
  595. static int virtio_pci_resume(struct pci_dev *pci_dev)
  596. {
  597. pci_restore_state(pci_dev);
  598. pci_set_power_state(pci_dev, PCI_D0);
  599. return 0;
  600. }
  601. #endif
  602. static struct pci_driver virtio_pci_driver = {
  603. .name = "virtio-pci",
  604. .id_table = virtio_pci_id_table,
  605. .probe = virtio_pci_probe,
  606. .remove = __devexit_p(virtio_pci_remove),
  607. #ifdef CONFIG_PM
  608. .suspend = virtio_pci_suspend,
  609. .resume = virtio_pci_resume,
  610. #endif
  611. };
  612. static int __init virtio_pci_init(void)
  613. {
  614. return pci_register_driver(&virtio_pci_driver);
  615. }
  616. module_init(virtio_pci_init);
  617. static void __exit virtio_pci_exit(void)
  618. {
  619. pci_unregister_driver(&virtio_pci_driver);
  620. }
  621. module_exit(virtio_pci_exit);