virtio_pci_common.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Virtio PCI driver - common functionality for all device versions
  4. *
  5. * This module allows virtio devices to be used over a virtual PCI device.
  6. * This can be used with QEMU based VMMs like KVM or Xen.
  7. *
  8. * Copyright IBM Corp. 2007
  9. * Copyright Red Hat, Inc. 2014
  10. *
  11. * Authors:
  12. * Anthony Liguori <aliguori@us.ibm.com>
  13. * Rusty Russell <rusty@rustcorp.com.au>
  14. * Michael S. Tsirkin <mst@redhat.com>
  15. */
  16. #include "virtio_pci_common.h"
  17. static bool force_legacy = false;
  18. #if IS_ENABLED(CONFIG_VIRTIO_PCI_LEGACY)
  19. module_param(force_legacy, bool, 0444);
  20. MODULE_PARM_DESC(force_legacy,
  21. "Force legacy mode for transitional virtio 1 devices");
  22. #endif
  23. bool vp_is_avq(struct virtio_device *vdev, unsigned int index)
  24. {
  25. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  26. if (!virtio_has_feature(vdev, VIRTIO_F_ADMIN_VQ))
  27. return false;
  28. return index == vp_dev->admin_vq.vq_index;
  29. }
  30. /* wait for pending irq handlers */
  31. void vp_synchronize_vectors(struct virtio_device *vdev)
  32. {
  33. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  34. int i;
  35. if (vp_dev->intx_enabled)
  36. synchronize_irq(vp_dev->pci_dev->irq);
  37. for (i = 0; i < vp_dev->msix_vectors; ++i)
  38. synchronize_irq(pci_irq_vector(vp_dev->pci_dev, i));
  39. }
  40. /* the notify function used when creating a virt queue */
  41. bool vp_notify(struct virtqueue *vq)
  42. {
  43. /* we write the queue's selector into the notification register to
  44. * signal the other end */
  45. iowrite16(vq->index, (void __iomem *)vq->priv);
  46. return true;
  47. }
  48. /* Notify all slow path virtqueues on an interrupt. */
  49. static void vp_vring_slow_path_interrupt(int irq,
  50. struct virtio_pci_device *vp_dev)
  51. {
  52. struct virtio_pci_vq_info *info;
  53. unsigned long flags;
  54. spin_lock_irqsave(&vp_dev->lock, flags);
  55. list_for_each_entry(info, &vp_dev->slow_virtqueues, node)
  56. vring_interrupt(irq, info->vq);
  57. spin_unlock_irqrestore(&vp_dev->lock, flags);
  58. }
  59. /* Handle a configuration change: Tell driver if it wants to know. */
  60. static irqreturn_t vp_config_changed(int irq, void *opaque)
  61. {
  62. struct virtio_pci_device *vp_dev = opaque;
  63. virtio_config_changed(&vp_dev->vdev);
  64. vp_vring_slow_path_interrupt(irq, vp_dev);
  65. return IRQ_HANDLED;
  66. }
  67. /* Notify all virtqueues on an interrupt. */
  68. static irqreturn_t vp_vring_interrupt(int irq, void *opaque)
  69. {
  70. struct virtio_pci_device *vp_dev = opaque;
  71. struct virtio_pci_vq_info *info;
  72. irqreturn_t ret = IRQ_NONE;
  73. unsigned long flags;
  74. spin_lock_irqsave(&vp_dev->lock, flags);
  75. list_for_each_entry(info, &vp_dev->virtqueues, node) {
  76. if (vring_interrupt(irq, info->vq) == IRQ_HANDLED)
  77. ret = IRQ_HANDLED;
  78. }
  79. spin_unlock_irqrestore(&vp_dev->lock, flags);
  80. return ret;
  81. }
  82. /* A small wrapper to also acknowledge the interrupt when it's handled.
  83. * I really need an EIO hook for the vring so I can ack the interrupt once we
  84. * know that we'll be handling the IRQ but before we invoke the callback since
  85. * the callback may notify the host which results in the host attempting to
  86. * raise an interrupt that we would then mask once we acknowledged the
  87. * interrupt. */
  88. static irqreturn_t vp_interrupt(int irq, void *opaque)
  89. {
  90. struct virtio_pci_device *vp_dev = opaque;
  91. u8 isr;
  92. /* reading the ISR has the effect of also clearing it so it's very
  93. * important to save off the value. */
  94. isr = ioread8(vp_dev->isr);
  95. /* It's definitely not us if the ISR was not high */
  96. if (!isr)
  97. return IRQ_NONE;
  98. /* Configuration change? Tell driver if it wants to know. */
  99. if (isr & VIRTIO_PCI_ISR_CONFIG)
  100. vp_config_changed(irq, opaque);
  101. return vp_vring_interrupt(irq, opaque);
  102. }
  103. static int vp_request_msix_vectors(struct virtio_device *vdev, int nvectors,
  104. bool per_vq_vectors, struct irq_affinity *desc)
  105. {
  106. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  107. const char *name = dev_name(&vp_dev->vdev.dev);
  108. unsigned int flags = PCI_IRQ_MSIX;
  109. unsigned int i, v;
  110. int err = -ENOMEM;
  111. vp_dev->msix_vectors = nvectors;
  112. vp_dev->msix_names = kmalloc_array(nvectors,
  113. sizeof(*vp_dev->msix_names),
  114. GFP_KERNEL);
  115. if (!vp_dev->msix_names)
  116. goto error;
  117. vp_dev->msix_affinity_masks
  118. = kcalloc(nvectors, sizeof(*vp_dev->msix_affinity_masks),
  119. GFP_KERNEL);
  120. if (!vp_dev->msix_affinity_masks)
  121. goto error;
  122. for (i = 0; i < nvectors; ++i)
  123. if (!alloc_cpumask_var(&vp_dev->msix_affinity_masks[i],
  124. GFP_KERNEL))
  125. goto error;
  126. if (!per_vq_vectors)
  127. desc = NULL;
  128. if (desc) {
  129. flags |= PCI_IRQ_AFFINITY;
  130. desc->pre_vectors++; /* virtio config vector */
  131. }
  132. err = pci_alloc_irq_vectors_affinity(vp_dev->pci_dev, nvectors,
  133. nvectors, flags, desc);
  134. if (err < 0)
  135. goto error;
  136. vp_dev->msix_enabled = 1;
  137. /* Set the vector used for configuration */
  138. v = vp_dev->msix_used_vectors;
  139. snprintf(vp_dev->msix_names[v], sizeof *vp_dev->msix_names,
  140. "%s-config", name);
  141. err = request_irq(pci_irq_vector(vp_dev->pci_dev, v),
  142. vp_config_changed, 0, vp_dev->msix_names[v],
  143. vp_dev);
  144. if (err)
  145. goto error;
  146. ++vp_dev->msix_used_vectors;
  147. v = vp_dev->config_vector(vp_dev, v);
  148. /* Verify we had enough resources to assign the vector */
  149. if (v == VIRTIO_MSI_NO_VECTOR) {
  150. err = -EBUSY;
  151. goto error;
  152. }
  153. if (!per_vq_vectors) {
  154. /* Shared vector for all VQs */
  155. v = vp_dev->msix_used_vectors;
  156. snprintf(vp_dev->msix_names[v], sizeof *vp_dev->msix_names,
  157. "%s-virtqueues", name);
  158. err = request_irq(pci_irq_vector(vp_dev->pci_dev, v),
  159. vp_vring_interrupt, 0, vp_dev->msix_names[v],
  160. vp_dev);
  161. if (err)
  162. goto error;
  163. ++vp_dev->msix_used_vectors;
  164. }
  165. return 0;
  166. error:
  167. return err;
  168. }
  169. static bool vp_is_slow_path_vector(u16 msix_vec)
  170. {
  171. return msix_vec == VP_MSIX_CONFIG_VECTOR;
  172. }
  173. static struct virtqueue *vp_setup_vq(struct virtio_device *vdev, unsigned int index,
  174. void (*callback)(struct virtqueue *vq),
  175. const char *name,
  176. bool ctx,
  177. u16 msix_vec,
  178. struct virtio_pci_vq_info **p_info)
  179. {
  180. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  181. struct virtio_pci_vq_info *info = kmalloc(sizeof *info, GFP_KERNEL);
  182. struct virtqueue *vq;
  183. unsigned long flags;
  184. /* fill out our structure that represents an active queue */
  185. if (!info)
  186. return ERR_PTR(-ENOMEM);
  187. vq = vp_dev->setup_vq(vp_dev, info, index, callback, name, ctx,
  188. msix_vec);
  189. if (IS_ERR(vq))
  190. goto out_info;
  191. info->vq = vq;
  192. if (callback) {
  193. spin_lock_irqsave(&vp_dev->lock, flags);
  194. if (!vp_is_slow_path_vector(msix_vec))
  195. list_add(&info->node, &vp_dev->virtqueues);
  196. else
  197. list_add(&info->node, &vp_dev->slow_virtqueues);
  198. spin_unlock_irqrestore(&vp_dev->lock, flags);
  199. } else {
  200. INIT_LIST_HEAD(&info->node);
  201. }
  202. *p_info = info;
  203. return vq;
  204. out_info:
  205. kfree(info);
  206. return vq;
  207. }
  208. static void vp_del_vq(struct virtqueue *vq, struct virtio_pci_vq_info *info)
  209. {
  210. struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
  211. unsigned long flags;
  212. /*
  213. * If it fails during re-enable reset vq. This way we won't rejoin
  214. * info->node to the queue. Prevent unexpected irqs.
  215. */
  216. if (!vq->reset) {
  217. spin_lock_irqsave(&vp_dev->lock, flags);
  218. list_del(&info->node);
  219. spin_unlock_irqrestore(&vp_dev->lock, flags);
  220. }
  221. vp_dev->del_vq(info);
  222. kfree(info);
  223. }
  224. /* the config->del_vqs() implementation */
  225. void vp_del_vqs(struct virtio_device *vdev)
  226. {
  227. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  228. struct virtio_pci_vq_info *info;
  229. struct virtqueue *vq, *n;
  230. int i;
  231. list_for_each_entry_safe(vq, n, &vdev->vqs, list) {
  232. info = vp_is_avq(vdev, vq->index) ? vp_dev->admin_vq.info :
  233. vp_dev->vqs[vq->index];
  234. if (vp_dev->per_vq_vectors) {
  235. int v = info->msix_vector;
  236. if (v != VIRTIO_MSI_NO_VECTOR &&
  237. !vp_is_slow_path_vector(v)) {
  238. int irq = pci_irq_vector(vp_dev->pci_dev, v);
  239. irq_update_affinity_hint(irq, NULL);
  240. free_irq(irq, vq);
  241. }
  242. }
  243. vp_del_vq(vq, info);
  244. }
  245. vp_dev->per_vq_vectors = false;
  246. if (vp_dev->intx_enabled) {
  247. free_irq(vp_dev->pci_dev->irq, vp_dev);
  248. vp_dev->intx_enabled = 0;
  249. }
  250. for (i = 0; i < vp_dev->msix_used_vectors; ++i)
  251. free_irq(pci_irq_vector(vp_dev->pci_dev, i), vp_dev);
  252. if (vp_dev->msix_affinity_masks) {
  253. for (i = 0; i < vp_dev->msix_vectors; i++)
  254. free_cpumask_var(vp_dev->msix_affinity_masks[i]);
  255. }
  256. if (vp_dev->msix_enabled) {
  257. /* Disable the vector used for configuration */
  258. vp_dev->config_vector(vp_dev, VIRTIO_MSI_NO_VECTOR);
  259. pci_free_irq_vectors(vp_dev->pci_dev);
  260. vp_dev->msix_enabled = 0;
  261. }
  262. vp_dev->msix_vectors = 0;
  263. vp_dev->msix_used_vectors = 0;
  264. kfree(vp_dev->msix_names);
  265. vp_dev->msix_names = NULL;
  266. kfree(vp_dev->msix_affinity_masks);
  267. vp_dev->msix_affinity_masks = NULL;
  268. kfree(vp_dev->vqs);
  269. vp_dev->vqs = NULL;
  270. }
  271. enum vp_vq_vector_policy {
  272. VP_VQ_VECTOR_POLICY_EACH,
  273. VP_VQ_VECTOR_POLICY_SHARED_SLOW,
  274. VP_VQ_VECTOR_POLICY_SHARED,
  275. };
  276. static struct virtqueue *
  277. vp_find_one_vq_msix(struct virtio_device *vdev, int queue_idx,
  278. vq_callback_t *callback, const char *name, bool ctx,
  279. bool slow_path, int *allocated_vectors,
  280. enum vp_vq_vector_policy vector_policy,
  281. struct virtio_pci_vq_info **p_info)
  282. {
  283. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  284. struct virtqueue *vq;
  285. u16 msix_vec;
  286. int err;
  287. if (!callback)
  288. msix_vec = VIRTIO_MSI_NO_VECTOR;
  289. else if (vector_policy == VP_VQ_VECTOR_POLICY_EACH ||
  290. (vector_policy == VP_VQ_VECTOR_POLICY_SHARED_SLOW &&
  291. !slow_path))
  292. msix_vec = (*allocated_vectors)++;
  293. else if (vector_policy != VP_VQ_VECTOR_POLICY_EACH &&
  294. slow_path)
  295. msix_vec = VP_MSIX_CONFIG_VECTOR;
  296. else
  297. msix_vec = VP_MSIX_VQ_VECTOR;
  298. vq = vp_setup_vq(vdev, queue_idx, callback, name, ctx, msix_vec,
  299. p_info);
  300. if (IS_ERR(vq))
  301. return vq;
  302. if (vector_policy == VP_VQ_VECTOR_POLICY_SHARED ||
  303. msix_vec == VIRTIO_MSI_NO_VECTOR ||
  304. vp_is_slow_path_vector(msix_vec))
  305. return vq;
  306. /* allocate per-vq irq if available and necessary */
  307. snprintf(vp_dev->msix_names[msix_vec], sizeof(*vp_dev->msix_names),
  308. "%s-%s", dev_name(&vp_dev->vdev.dev), name);
  309. err = request_irq(pci_irq_vector(vp_dev->pci_dev, msix_vec),
  310. vring_interrupt, 0,
  311. vp_dev->msix_names[msix_vec], vq);
  312. if (err) {
  313. vp_del_vq(vq, *p_info);
  314. return ERR_PTR(err);
  315. }
  316. return vq;
  317. }
  318. static int vp_find_vqs_msix(struct virtio_device *vdev, unsigned int nvqs,
  319. struct virtqueue *vqs[],
  320. struct virtqueue_info vqs_info[],
  321. enum vp_vq_vector_policy vector_policy,
  322. struct irq_affinity *desc)
  323. {
  324. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  325. struct virtio_pci_admin_vq *avq = &vp_dev->admin_vq;
  326. struct virtqueue_info *vqi;
  327. int i, err, nvectors, allocated_vectors, queue_idx = 0;
  328. struct virtqueue *vq;
  329. bool per_vq_vectors;
  330. u16 avq_num = 0;
  331. vp_dev->vqs = kcalloc(nvqs, sizeof(*vp_dev->vqs), GFP_KERNEL);
  332. if (!vp_dev->vqs)
  333. return -ENOMEM;
  334. if (vp_dev->avq_index) {
  335. err = vp_dev->avq_index(vdev, &avq->vq_index, &avq_num);
  336. if (err)
  337. goto error_find;
  338. }
  339. per_vq_vectors = vector_policy != VP_VQ_VECTOR_POLICY_SHARED;
  340. if (per_vq_vectors) {
  341. /* Best option: one for change interrupt, one per vq. */
  342. nvectors = 1;
  343. for (i = 0; i < nvqs; ++i) {
  344. vqi = &vqs_info[i];
  345. if (vqi->name && vqi->callback)
  346. ++nvectors;
  347. }
  348. if (avq_num && vector_policy == VP_VQ_VECTOR_POLICY_EACH)
  349. ++nvectors;
  350. } else {
  351. /* Second best: one for change, shared for all vqs. */
  352. nvectors = 2;
  353. }
  354. err = vp_request_msix_vectors(vdev, nvectors, per_vq_vectors, desc);
  355. if (err)
  356. goto error_find;
  357. vp_dev->per_vq_vectors = per_vq_vectors;
  358. allocated_vectors = vp_dev->msix_used_vectors;
  359. for (i = 0; i < nvqs; ++i) {
  360. vqi = &vqs_info[i];
  361. if (!vqi->name) {
  362. vqs[i] = NULL;
  363. continue;
  364. }
  365. vqs[i] = vp_find_one_vq_msix(vdev, queue_idx++, vqi->callback,
  366. vqi->name, vqi->ctx, false,
  367. &allocated_vectors, vector_policy,
  368. &vp_dev->vqs[i]);
  369. if (IS_ERR(vqs[i])) {
  370. err = PTR_ERR(vqs[i]);
  371. goto error_find;
  372. }
  373. }
  374. if (!avq_num)
  375. return 0;
  376. sprintf(avq->name, "avq.%u", avq->vq_index);
  377. vq = vp_find_one_vq_msix(vdev, avq->vq_index, vp_modern_avq_done,
  378. avq->name, false, true, &allocated_vectors,
  379. vector_policy, &vp_dev->admin_vq.info);
  380. if (IS_ERR(vq)) {
  381. err = PTR_ERR(vq);
  382. goto error_find;
  383. }
  384. return 0;
  385. error_find:
  386. vp_del_vqs(vdev);
  387. return err;
  388. }
  389. static int vp_find_vqs_intx(struct virtio_device *vdev, unsigned int nvqs,
  390. struct virtqueue *vqs[],
  391. struct virtqueue_info vqs_info[])
  392. {
  393. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  394. struct virtio_pci_admin_vq *avq = &vp_dev->admin_vq;
  395. int i, err, queue_idx = 0;
  396. struct virtqueue *vq;
  397. u16 avq_num = 0;
  398. vp_dev->vqs = kcalloc(nvqs, sizeof(*vp_dev->vqs), GFP_KERNEL);
  399. if (!vp_dev->vqs)
  400. return -ENOMEM;
  401. if (vp_dev->avq_index) {
  402. err = vp_dev->avq_index(vdev, &avq->vq_index, &avq_num);
  403. if (err)
  404. goto out_del_vqs;
  405. }
  406. err = request_irq(vp_dev->pci_dev->irq, vp_interrupt, IRQF_SHARED,
  407. dev_name(&vdev->dev), vp_dev);
  408. if (err)
  409. goto out_del_vqs;
  410. vp_dev->intx_enabled = 1;
  411. vp_dev->per_vq_vectors = false;
  412. for (i = 0; i < nvqs; ++i) {
  413. struct virtqueue_info *vqi = &vqs_info[i];
  414. if (!vqi->name) {
  415. vqs[i] = NULL;
  416. continue;
  417. }
  418. vqs[i] = vp_setup_vq(vdev, queue_idx++, vqi->callback,
  419. vqi->name, vqi->ctx,
  420. VIRTIO_MSI_NO_VECTOR, &vp_dev->vqs[i]);
  421. if (IS_ERR(vqs[i])) {
  422. err = PTR_ERR(vqs[i]);
  423. goto out_del_vqs;
  424. }
  425. }
  426. if (!avq_num)
  427. return 0;
  428. sprintf(avq->name, "avq.%u", avq->vq_index);
  429. vq = vp_setup_vq(vdev, queue_idx++, vp_modern_avq_done, avq->name,
  430. false, VIRTIO_MSI_NO_VECTOR,
  431. &vp_dev->admin_vq.info);
  432. if (IS_ERR(vq)) {
  433. err = PTR_ERR(vq);
  434. goto out_del_vqs;
  435. }
  436. return 0;
  437. out_del_vqs:
  438. vp_del_vqs(vdev);
  439. return err;
  440. }
  441. /* the config->find_vqs() implementation */
  442. int vp_find_vqs(struct virtio_device *vdev, unsigned int nvqs,
  443. struct virtqueue *vqs[], struct virtqueue_info vqs_info[],
  444. struct irq_affinity *desc)
  445. {
  446. int err;
  447. /* Try MSI-X with one vector per queue. */
  448. err = vp_find_vqs_msix(vdev, nvqs, vqs, vqs_info,
  449. VP_VQ_VECTOR_POLICY_EACH, desc);
  450. if (!err)
  451. return 0;
  452. /* Fallback: MSI-X with one shared vector for config and
  453. * slow path queues, one vector per queue for the rest.
  454. */
  455. err = vp_find_vqs_msix(vdev, nvqs, vqs, vqs_info,
  456. VP_VQ_VECTOR_POLICY_SHARED_SLOW, desc);
  457. if (!err)
  458. return 0;
  459. /* Fallback: MSI-X with one vector for config, one shared for queues. */
  460. err = vp_find_vqs_msix(vdev, nvqs, vqs, vqs_info,
  461. VP_VQ_VECTOR_POLICY_SHARED, desc);
  462. if (!err)
  463. return 0;
  464. /* Is there an interrupt? If not give up. */
  465. if (!(to_vp_device(vdev)->pci_dev->irq))
  466. return err;
  467. /* Finally fall back to regular interrupts. */
  468. return vp_find_vqs_intx(vdev, nvqs, vqs, vqs_info);
  469. }
  470. const char *vp_bus_name(struct virtio_device *vdev)
  471. {
  472. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  473. return pci_name(vp_dev->pci_dev);
  474. }
  475. /* Setup the affinity for a virtqueue:
  476. * - force the affinity for per vq vector
  477. * - OR over all affinities for shared MSI
  478. * - ignore the affinity request if we're using INTX
  479. */
  480. int vp_set_vq_affinity(struct virtqueue *vq, const struct cpumask *cpu_mask)
  481. {
  482. struct virtio_device *vdev = vq->vdev;
  483. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  484. struct virtio_pci_vq_info *info = vp_dev->vqs[vq->index];
  485. struct cpumask *mask;
  486. unsigned int irq;
  487. if (!vq->callback)
  488. return -EINVAL;
  489. if (vp_dev->msix_enabled) {
  490. mask = vp_dev->msix_affinity_masks[info->msix_vector];
  491. irq = pci_irq_vector(vp_dev->pci_dev, info->msix_vector);
  492. if (!cpu_mask)
  493. irq_update_affinity_hint(irq, NULL);
  494. else {
  495. cpumask_copy(mask, cpu_mask);
  496. irq_set_affinity_and_hint(irq, mask);
  497. }
  498. }
  499. return 0;
  500. }
  501. const struct cpumask *vp_get_vq_affinity(struct virtio_device *vdev, int index)
  502. {
  503. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  504. if (!vp_dev->per_vq_vectors ||
  505. vp_dev->vqs[index]->msix_vector == VIRTIO_MSI_NO_VECTOR ||
  506. vp_is_slow_path_vector(vp_dev->vqs[index]->msix_vector))
  507. return NULL;
  508. return pci_irq_get_affinity(vp_dev->pci_dev,
  509. vp_dev->vqs[index]->msix_vector);
  510. }
  511. #ifdef CONFIG_PM_SLEEP
  512. static int virtio_pci_freeze(struct device *dev)
  513. {
  514. struct pci_dev *pci_dev = to_pci_dev(dev);
  515. struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
  516. int ret;
  517. ret = virtio_device_freeze(&vp_dev->vdev);
  518. if (!ret)
  519. pci_disable_device(pci_dev);
  520. return ret;
  521. }
  522. static int virtio_pci_restore(struct device *dev)
  523. {
  524. struct pci_dev *pci_dev = to_pci_dev(dev);
  525. struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
  526. int ret;
  527. ret = pci_enable_device(pci_dev);
  528. if (ret)
  529. return ret;
  530. pci_set_master(pci_dev);
  531. return virtio_device_restore(&vp_dev->vdev);
  532. }
  533. static bool vp_supports_pm_no_reset(struct device *dev)
  534. {
  535. struct pci_dev *pci_dev = to_pci_dev(dev);
  536. u16 pmcsr;
  537. if (!pci_dev->pm_cap)
  538. return false;
  539. pci_read_config_word(pci_dev, pci_dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  540. if (PCI_POSSIBLE_ERROR(pmcsr)) {
  541. dev_err(dev, "Unable to query pmcsr");
  542. return false;
  543. }
  544. return pmcsr & PCI_PM_CTRL_NO_SOFT_RESET;
  545. }
  546. static int virtio_pci_suspend(struct device *dev)
  547. {
  548. return vp_supports_pm_no_reset(dev) ? 0 : virtio_pci_freeze(dev);
  549. }
  550. static int virtio_pci_resume(struct device *dev)
  551. {
  552. return vp_supports_pm_no_reset(dev) ? 0 : virtio_pci_restore(dev);
  553. }
  554. static const struct dev_pm_ops virtio_pci_pm_ops = {
  555. .suspend = virtio_pci_suspend,
  556. .resume = virtio_pci_resume,
  557. .freeze = virtio_pci_freeze,
  558. .thaw = virtio_pci_restore,
  559. .poweroff = virtio_pci_freeze,
  560. .restore = virtio_pci_restore,
  561. };
  562. #endif
  563. /* Qumranet donated their vendor ID for devices 0x1000 thru 0x10FF. */
  564. static const struct pci_device_id virtio_pci_id_table[] = {
  565. { PCI_DEVICE(PCI_VENDOR_ID_REDHAT_QUMRANET, PCI_ANY_ID) },
  566. { 0 }
  567. };
  568. MODULE_DEVICE_TABLE(pci, virtio_pci_id_table);
  569. static void virtio_pci_release_dev(struct device *_d)
  570. {
  571. struct virtio_device *vdev = dev_to_virtio(_d);
  572. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  573. /* As struct device is a kobject, it's not safe to
  574. * free the memory (including the reference counter itself)
  575. * until it's release callback. */
  576. kfree(vp_dev);
  577. }
  578. static int virtio_pci_probe(struct pci_dev *pci_dev,
  579. const struct pci_device_id *id)
  580. {
  581. struct virtio_pci_device *vp_dev, *reg_dev = NULL;
  582. int rc;
  583. /* allocate our structure and fill it out */
  584. vp_dev = kzalloc(sizeof(struct virtio_pci_device), GFP_KERNEL);
  585. if (!vp_dev)
  586. return -ENOMEM;
  587. pci_set_drvdata(pci_dev, vp_dev);
  588. vp_dev->vdev.dev.parent = &pci_dev->dev;
  589. vp_dev->vdev.dev.release = virtio_pci_release_dev;
  590. vp_dev->pci_dev = pci_dev;
  591. INIT_LIST_HEAD(&vp_dev->virtqueues);
  592. INIT_LIST_HEAD(&vp_dev->slow_virtqueues);
  593. spin_lock_init(&vp_dev->lock);
  594. /* enable the device */
  595. rc = pci_enable_device(pci_dev);
  596. if (rc)
  597. goto err_enable_device;
  598. if (force_legacy) {
  599. rc = virtio_pci_legacy_probe(vp_dev);
  600. /* Also try modern mode if we can't map BAR0 (no IO space). */
  601. if (rc == -ENODEV || rc == -ENOMEM)
  602. rc = virtio_pci_modern_probe(vp_dev);
  603. if (rc)
  604. goto err_probe;
  605. } else {
  606. rc = virtio_pci_modern_probe(vp_dev);
  607. if (rc == -ENODEV)
  608. rc = virtio_pci_legacy_probe(vp_dev);
  609. if (rc)
  610. goto err_probe;
  611. }
  612. pci_set_master(pci_dev);
  613. rc = register_virtio_device(&vp_dev->vdev);
  614. reg_dev = vp_dev;
  615. if (rc)
  616. goto err_register;
  617. return 0;
  618. err_register:
  619. if (vp_dev->is_legacy)
  620. virtio_pci_legacy_remove(vp_dev);
  621. else
  622. virtio_pci_modern_remove(vp_dev);
  623. err_probe:
  624. pci_disable_device(pci_dev);
  625. err_enable_device:
  626. if (reg_dev)
  627. put_device(&vp_dev->vdev.dev);
  628. else
  629. kfree(vp_dev);
  630. return rc;
  631. }
  632. static void virtio_pci_remove(struct pci_dev *pci_dev)
  633. {
  634. struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
  635. struct device *dev = get_device(&vp_dev->vdev.dev);
  636. /*
  637. * Device is marked broken on surprise removal so that virtio upper
  638. * layers can abort any ongoing operation.
  639. */
  640. if (!pci_device_is_present(pci_dev))
  641. virtio_break_device(&vp_dev->vdev);
  642. pci_disable_sriov(pci_dev);
  643. unregister_virtio_device(&vp_dev->vdev);
  644. if (vp_dev->is_legacy)
  645. virtio_pci_legacy_remove(vp_dev);
  646. else
  647. virtio_pci_modern_remove(vp_dev);
  648. pci_disable_device(pci_dev);
  649. put_device(dev);
  650. }
  651. static int virtio_pci_sriov_configure(struct pci_dev *pci_dev, int num_vfs)
  652. {
  653. struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
  654. struct virtio_device *vdev = &vp_dev->vdev;
  655. int ret;
  656. if (!(vdev->config->get_status(vdev) & VIRTIO_CONFIG_S_DRIVER_OK))
  657. return -EBUSY;
  658. if (!__virtio_test_bit(vdev, VIRTIO_F_SR_IOV))
  659. return -EINVAL;
  660. if (pci_vfs_assigned(pci_dev))
  661. return -EPERM;
  662. if (num_vfs == 0) {
  663. pci_disable_sriov(pci_dev);
  664. return 0;
  665. }
  666. ret = pci_enable_sriov(pci_dev, num_vfs);
  667. if (ret < 0)
  668. return ret;
  669. return num_vfs;
  670. }
  671. static struct pci_driver virtio_pci_driver = {
  672. .name = "virtio-pci",
  673. .id_table = virtio_pci_id_table,
  674. .probe = virtio_pci_probe,
  675. .remove = virtio_pci_remove,
  676. #ifdef CONFIG_PM_SLEEP
  677. .driver.pm = &virtio_pci_pm_ops,
  678. #endif
  679. .sriov_configure = virtio_pci_sriov_configure,
  680. };
  681. struct virtio_device *virtio_pci_vf_get_pf_dev(struct pci_dev *pdev)
  682. {
  683. struct virtio_pci_device *pf_vp_dev;
  684. pf_vp_dev = pci_iov_get_pf_drvdata(pdev, &virtio_pci_driver);
  685. if (IS_ERR(pf_vp_dev))
  686. return NULL;
  687. return &pf_vp_dev->vdev;
  688. }
  689. module_pci_driver(virtio_pci_driver);
  690. MODULE_AUTHOR("Anthony Liguori <aliguori@us.ibm.com>");
  691. MODULE_DESCRIPTION("virtio-pci");
  692. MODULE_LICENSE("GPL");
  693. MODULE_VERSION("1");