virtio_pci_modern.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Virtio PCI driver - modern (virtio 1.0) device support
  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 <[email protected]>
  13. * Rusty Russell <[email protected]>
  14. * Michael S. Tsirkin <[email protected]>
  15. */
  16. #include <linux/delay.h>
  17. #define VIRTIO_PCI_NO_LEGACY
  18. #define VIRTIO_RING_NO_LEGACY
  19. #include "virtio_pci_common.h"
  20. static u64 vp_get_features(struct virtio_device *vdev)
  21. {
  22. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  23. return vp_modern_get_features(&vp_dev->mdev);
  24. }
  25. static void vp_transport_features(struct virtio_device *vdev, u64 features)
  26. {
  27. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  28. struct pci_dev *pci_dev = vp_dev->pci_dev;
  29. if ((features & BIT_ULL(VIRTIO_F_SR_IOV)) &&
  30. pci_find_ext_capability(pci_dev, PCI_EXT_CAP_ID_SRIOV))
  31. __virtio_set_bit(vdev, VIRTIO_F_SR_IOV);
  32. if (features & BIT_ULL(VIRTIO_F_RING_RESET))
  33. __virtio_set_bit(vdev, VIRTIO_F_RING_RESET);
  34. }
  35. /* virtio config->finalize_features() implementation */
  36. static int vp_finalize_features(struct virtio_device *vdev)
  37. {
  38. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  39. u64 features = vdev->features;
  40. /* Give virtio_ring a chance to accept features. */
  41. vring_transport_features(vdev);
  42. /* Give virtio_pci a chance to accept features. */
  43. vp_transport_features(vdev, features);
  44. if (!__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
  45. dev_err(&vdev->dev, "virtio: device uses modern interface "
  46. "but does not have VIRTIO_F_VERSION_1\n");
  47. return -EINVAL;
  48. }
  49. vp_modern_set_features(&vp_dev->mdev, vdev->features);
  50. return 0;
  51. }
  52. /* virtio config->get() implementation */
  53. static void vp_get(struct virtio_device *vdev, unsigned int offset,
  54. void *buf, unsigned int len)
  55. {
  56. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  57. struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
  58. void __iomem *device = mdev->device;
  59. u8 b;
  60. __le16 w;
  61. __le32 l;
  62. BUG_ON(offset + len > mdev->device_len);
  63. switch (len) {
  64. case 1:
  65. b = ioread8(device + offset);
  66. memcpy(buf, &b, sizeof b);
  67. break;
  68. case 2:
  69. w = cpu_to_le16(ioread16(device + offset));
  70. memcpy(buf, &w, sizeof w);
  71. break;
  72. case 4:
  73. l = cpu_to_le32(ioread32(device + offset));
  74. memcpy(buf, &l, sizeof l);
  75. break;
  76. case 8:
  77. l = cpu_to_le32(ioread32(device + offset));
  78. memcpy(buf, &l, sizeof l);
  79. l = cpu_to_le32(ioread32(device + offset + sizeof l));
  80. memcpy(buf + sizeof l, &l, sizeof l);
  81. break;
  82. default:
  83. BUG();
  84. }
  85. }
  86. /* the config->set() implementation. it's symmetric to the config->get()
  87. * implementation */
  88. static void vp_set(struct virtio_device *vdev, unsigned int offset,
  89. const void *buf, unsigned int len)
  90. {
  91. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  92. struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
  93. void __iomem *device = mdev->device;
  94. u8 b;
  95. __le16 w;
  96. __le32 l;
  97. BUG_ON(offset + len > mdev->device_len);
  98. switch (len) {
  99. case 1:
  100. memcpy(&b, buf, sizeof b);
  101. iowrite8(b, device + offset);
  102. break;
  103. case 2:
  104. memcpy(&w, buf, sizeof w);
  105. iowrite16(le16_to_cpu(w), device + offset);
  106. break;
  107. case 4:
  108. memcpy(&l, buf, sizeof l);
  109. iowrite32(le32_to_cpu(l), device + offset);
  110. break;
  111. case 8:
  112. memcpy(&l, buf, sizeof l);
  113. iowrite32(le32_to_cpu(l), device + offset);
  114. memcpy(&l, buf + sizeof l, sizeof l);
  115. iowrite32(le32_to_cpu(l), device + offset + sizeof l);
  116. break;
  117. default:
  118. BUG();
  119. }
  120. }
  121. static u32 vp_generation(struct virtio_device *vdev)
  122. {
  123. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  124. return vp_modern_generation(&vp_dev->mdev);
  125. }
  126. /* config->{get,set}_status() implementations */
  127. static u8 vp_get_status(struct virtio_device *vdev)
  128. {
  129. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  130. return vp_modern_get_status(&vp_dev->mdev);
  131. }
  132. static void vp_set_status(struct virtio_device *vdev, u8 status)
  133. {
  134. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  135. /* We should never be setting status to 0. */
  136. BUG_ON(status == 0);
  137. vp_modern_set_status(&vp_dev->mdev, status);
  138. }
  139. static void vp_reset(struct virtio_device *vdev)
  140. {
  141. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  142. struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
  143. /* 0 status means a reset. */
  144. vp_modern_set_status(mdev, 0);
  145. /* After writing 0 to device_status, the driver MUST wait for a read of
  146. * device_status to return 0 before reinitializing the device.
  147. * This will flush out the status write, and flush in device writes,
  148. * including MSI-X interrupts, if any.
  149. */
  150. while (vp_modern_get_status(mdev))
  151. msleep(1);
  152. /* Flush pending VQ/configuration callbacks. */
  153. vp_synchronize_vectors(vdev);
  154. }
  155. static int vp_active_vq(struct virtqueue *vq, u16 msix_vec)
  156. {
  157. struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
  158. struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
  159. unsigned long index;
  160. index = vq->index;
  161. /* activate the queue */
  162. vp_modern_set_queue_size(mdev, index, virtqueue_get_vring_size(vq));
  163. vp_modern_queue_address(mdev, index, virtqueue_get_desc_addr(vq),
  164. virtqueue_get_avail_addr(vq),
  165. virtqueue_get_used_addr(vq));
  166. if (msix_vec != VIRTIO_MSI_NO_VECTOR) {
  167. msix_vec = vp_modern_queue_vector(mdev, index, msix_vec);
  168. if (msix_vec == VIRTIO_MSI_NO_VECTOR)
  169. return -EBUSY;
  170. }
  171. return 0;
  172. }
  173. static int vp_modern_disable_vq_and_reset(struct virtqueue *vq)
  174. {
  175. struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
  176. struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
  177. struct virtio_pci_vq_info *info;
  178. unsigned long flags;
  179. if (!virtio_has_feature(vq->vdev, VIRTIO_F_RING_RESET))
  180. return -ENOENT;
  181. vp_modern_set_queue_reset(mdev, vq->index);
  182. info = vp_dev->vqs[vq->index];
  183. /* delete vq from irq handler */
  184. spin_lock_irqsave(&vp_dev->lock, flags);
  185. list_del(&info->node);
  186. spin_unlock_irqrestore(&vp_dev->lock, flags);
  187. INIT_LIST_HEAD(&info->node);
  188. #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
  189. __virtqueue_break(vq);
  190. #endif
  191. /* For the case where vq has an exclusive irq, call synchronize_irq() to
  192. * wait for completion.
  193. *
  194. * note: We can't use disable_irq() since it conflicts with the affinity
  195. * managed IRQ that is used by some drivers.
  196. */
  197. if (vp_dev->per_vq_vectors && info->msix_vector != VIRTIO_MSI_NO_VECTOR)
  198. synchronize_irq(pci_irq_vector(vp_dev->pci_dev, info->msix_vector));
  199. vq->reset = true;
  200. return 0;
  201. }
  202. static int vp_modern_enable_vq_after_reset(struct virtqueue *vq)
  203. {
  204. struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
  205. struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
  206. struct virtio_pci_vq_info *info;
  207. unsigned long flags, index;
  208. int err;
  209. if (!vq->reset)
  210. return -EBUSY;
  211. index = vq->index;
  212. info = vp_dev->vqs[index];
  213. if (vp_modern_get_queue_reset(mdev, index))
  214. return -EBUSY;
  215. if (vp_modern_get_queue_enable(mdev, index))
  216. return -EBUSY;
  217. err = vp_active_vq(vq, info->msix_vector);
  218. if (err)
  219. return err;
  220. if (vq->callback) {
  221. spin_lock_irqsave(&vp_dev->lock, flags);
  222. list_add(&info->node, &vp_dev->virtqueues);
  223. spin_unlock_irqrestore(&vp_dev->lock, flags);
  224. } else {
  225. INIT_LIST_HEAD(&info->node);
  226. }
  227. #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
  228. __virtqueue_unbreak(vq);
  229. #endif
  230. vp_modern_set_queue_enable(&vp_dev->mdev, index, true);
  231. vq->reset = false;
  232. return 0;
  233. }
  234. static u16 vp_config_vector(struct virtio_pci_device *vp_dev, u16 vector)
  235. {
  236. return vp_modern_config_vector(&vp_dev->mdev, vector);
  237. }
  238. static struct virtqueue *setup_vq(struct virtio_pci_device *vp_dev,
  239. struct virtio_pci_vq_info *info,
  240. unsigned int index,
  241. void (*callback)(struct virtqueue *vq),
  242. const char *name,
  243. bool ctx,
  244. u16 msix_vec)
  245. {
  246. struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
  247. struct virtqueue *vq;
  248. u16 num;
  249. int err;
  250. if (index >= vp_modern_get_num_queues(mdev))
  251. return ERR_PTR(-EINVAL);
  252. /* Check if queue is either not available or already active. */
  253. num = vp_modern_get_queue_size(mdev, index);
  254. if (!num || vp_modern_get_queue_enable(mdev, index))
  255. return ERR_PTR(-ENOENT);
  256. if (num & (num - 1)) {
  257. dev_warn(&vp_dev->pci_dev->dev, "bad queue size %u", num);
  258. return ERR_PTR(-EINVAL);
  259. }
  260. info->msix_vector = msix_vec;
  261. /* create the vring */
  262. vq = vring_create_virtqueue(index, num,
  263. SMP_CACHE_BYTES, &vp_dev->vdev,
  264. true, true, ctx,
  265. vp_notify, callback, name);
  266. if (!vq)
  267. return ERR_PTR(-ENOMEM);
  268. vq->num_max = num;
  269. err = vp_active_vq(vq, msix_vec);
  270. if (err)
  271. goto err;
  272. vq->priv = (void __force *)vp_modern_map_vq_notify(mdev, index, NULL);
  273. if (!vq->priv) {
  274. err = -ENOMEM;
  275. goto err;
  276. }
  277. return vq;
  278. err:
  279. vring_del_virtqueue(vq);
  280. return ERR_PTR(err);
  281. }
  282. static int vp_modern_find_vqs(struct virtio_device *vdev, unsigned int nvqs,
  283. struct virtqueue *vqs[],
  284. vq_callback_t *callbacks[],
  285. const char * const names[], const bool *ctx,
  286. struct irq_affinity *desc)
  287. {
  288. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  289. struct virtqueue *vq;
  290. int rc = vp_find_vqs(vdev, nvqs, vqs, callbacks, names, ctx, desc);
  291. if (rc)
  292. return rc;
  293. /* Select and activate all queues. Has to be done last: once we do
  294. * this, there's no way to go back except reset.
  295. */
  296. list_for_each_entry(vq, &vdev->vqs, list)
  297. vp_modern_set_queue_enable(&vp_dev->mdev, vq->index, true);
  298. return 0;
  299. }
  300. static void del_vq(struct virtio_pci_vq_info *info)
  301. {
  302. struct virtqueue *vq = info->vq;
  303. struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
  304. struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
  305. if (vp_dev->msix_enabled)
  306. vp_modern_queue_vector(mdev, vq->index,
  307. VIRTIO_MSI_NO_VECTOR);
  308. if (!mdev->notify_base)
  309. pci_iounmap(mdev->pci_dev, (void __force __iomem *)vq->priv);
  310. vring_del_virtqueue(vq);
  311. }
  312. static int virtio_pci_find_shm_cap(struct pci_dev *dev, u8 required_id,
  313. u8 *bar, u64 *offset, u64 *len)
  314. {
  315. int pos;
  316. for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR); pos > 0;
  317. pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR)) {
  318. u8 type, cap_len, id, res_bar;
  319. u32 tmp32;
  320. u64 res_offset, res_length;
  321. pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
  322. cfg_type), &type);
  323. if (type != VIRTIO_PCI_CAP_SHARED_MEMORY_CFG)
  324. continue;
  325. pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
  326. cap_len), &cap_len);
  327. if (cap_len != sizeof(struct virtio_pci_cap64)) {
  328. dev_err(&dev->dev, "%s: shm cap with bad size offset:"
  329. " %d size: %d\n", __func__, pos, cap_len);
  330. continue;
  331. }
  332. pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
  333. id), &id);
  334. if (id != required_id)
  335. continue;
  336. pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
  337. bar), &res_bar);
  338. if (res_bar >= PCI_STD_NUM_BARS)
  339. continue;
  340. /* Type and ID match, and the BAR value isn't reserved.
  341. * Looks good.
  342. */
  343. /* Read the lower 32bit of length and offset */
  344. pci_read_config_dword(dev, pos + offsetof(struct virtio_pci_cap,
  345. offset), &tmp32);
  346. res_offset = tmp32;
  347. pci_read_config_dword(dev, pos + offsetof(struct virtio_pci_cap,
  348. length), &tmp32);
  349. res_length = tmp32;
  350. /* and now the top half */
  351. pci_read_config_dword(dev,
  352. pos + offsetof(struct virtio_pci_cap64,
  353. offset_hi), &tmp32);
  354. res_offset |= ((u64)tmp32) << 32;
  355. pci_read_config_dword(dev,
  356. pos + offsetof(struct virtio_pci_cap64,
  357. length_hi), &tmp32);
  358. res_length |= ((u64)tmp32) << 32;
  359. *bar = res_bar;
  360. *offset = res_offset;
  361. *len = res_length;
  362. return pos;
  363. }
  364. return 0;
  365. }
  366. static bool vp_get_shm_region(struct virtio_device *vdev,
  367. struct virtio_shm_region *region, u8 id)
  368. {
  369. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  370. struct pci_dev *pci_dev = vp_dev->pci_dev;
  371. u8 bar;
  372. u64 offset, len;
  373. phys_addr_t phys_addr;
  374. size_t bar_len;
  375. if (!virtio_pci_find_shm_cap(pci_dev, id, &bar, &offset, &len))
  376. return false;
  377. phys_addr = pci_resource_start(pci_dev, bar);
  378. bar_len = pci_resource_len(pci_dev, bar);
  379. if ((offset + len) < offset) {
  380. dev_err(&pci_dev->dev, "%s: cap offset+len overflow detected\n",
  381. __func__);
  382. return false;
  383. }
  384. if (offset + len > bar_len) {
  385. dev_err(&pci_dev->dev, "%s: bar shorter than cap offset+len\n",
  386. __func__);
  387. return false;
  388. }
  389. region->len = len;
  390. region->addr = (u64) phys_addr + offset;
  391. return true;
  392. }
  393. static const struct virtio_config_ops virtio_pci_config_nodev_ops = {
  394. .get = NULL,
  395. .set = NULL,
  396. .generation = vp_generation,
  397. .get_status = vp_get_status,
  398. .set_status = vp_set_status,
  399. .reset = vp_reset,
  400. .find_vqs = vp_modern_find_vqs,
  401. .del_vqs = vp_del_vqs,
  402. .synchronize_cbs = vp_synchronize_vectors,
  403. .get_features = vp_get_features,
  404. .finalize_features = vp_finalize_features,
  405. .bus_name = vp_bus_name,
  406. .set_vq_affinity = vp_set_vq_affinity,
  407. .get_vq_affinity = vp_get_vq_affinity,
  408. .get_shm_region = vp_get_shm_region,
  409. .disable_vq_and_reset = vp_modern_disable_vq_and_reset,
  410. .enable_vq_after_reset = vp_modern_enable_vq_after_reset,
  411. };
  412. static const struct virtio_config_ops virtio_pci_config_ops = {
  413. .get = vp_get,
  414. .set = vp_set,
  415. .generation = vp_generation,
  416. .get_status = vp_get_status,
  417. .set_status = vp_set_status,
  418. .reset = vp_reset,
  419. .find_vqs = vp_modern_find_vqs,
  420. .del_vqs = vp_del_vqs,
  421. .synchronize_cbs = vp_synchronize_vectors,
  422. .get_features = vp_get_features,
  423. .finalize_features = vp_finalize_features,
  424. .bus_name = vp_bus_name,
  425. .set_vq_affinity = vp_set_vq_affinity,
  426. .get_vq_affinity = vp_get_vq_affinity,
  427. .get_shm_region = vp_get_shm_region,
  428. .disable_vq_and_reset = vp_modern_disable_vq_and_reset,
  429. .enable_vq_after_reset = vp_modern_enable_vq_after_reset,
  430. };
  431. /* the PCI probing function */
  432. int virtio_pci_modern_probe(struct virtio_pci_device *vp_dev)
  433. {
  434. struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
  435. struct pci_dev *pci_dev = vp_dev->pci_dev;
  436. int err;
  437. mdev->pci_dev = pci_dev;
  438. err = vp_modern_probe(mdev);
  439. if (err)
  440. return err;
  441. if (mdev->device)
  442. vp_dev->vdev.config = &virtio_pci_config_ops;
  443. else
  444. vp_dev->vdev.config = &virtio_pci_config_nodev_ops;
  445. vp_dev->config_vector = vp_config_vector;
  446. vp_dev->setup_vq = setup_vq;
  447. vp_dev->del_vq = del_vq;
  448. vp_dev->isr = mdev->isr;
  449. vp_dev->vdev.id = mdev->id;
  450. return 0;
  451. }
  452. void virtio_pci_modern_remove(struct virtio_pci_device *vp_dev)
  453. {
  454. struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
  455. vp_modern_remove(mdev);
  456. }