virtio_pci.c 19 KB

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