virtio_vdpa.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * VIRTIO based driver for vDPA device
  4. *
  5. * Copyright (c) 2020, Red Hat. All rights reserved.
  6. * Author: Jason Wang <jasowang@redhat.com>
  7. *
  8. */
  9. #include <linux/init.h>
  10. #include <linux/module.h>
  11. #include <linux/device.h>
  12. #include <linux/kernel.h>
  13. #include <linux/slab.h>
  14. #include <linux/uuid.h>
  15. #include <linux/virtio.h>
  16. #include <linux/vdpa.h>
  17. #include <linux/virtio_config.h>
  18. #include <linux/virtio_ring.h>
  19. #define MOD_VERSION "0.1"
  20. #define MOD_AUTHOR "Jason Wang <jasowang@redhat.com>"
  21. #define MOD_DESC "vDPA bus driver for virtio devices"
  22. #define MOD_LICENSE "GPL v2"
  23. struct virtio_vdpa_device {
  24. struct virtio_device vdev;
  25. struct vdpa_device *vdpa;
  26. u64 features;
  27. /* The lock to protect virtqueue list */
  28. spinlock_t lock;
  29. /* List of virtio_vdpa_vq_info */
  30. struct list_head virtqueues;
  31. };
  32. struct virtio_vdpa_vq_info {
  33. /* the actual virtqueue */
  34. struct virtqueue *vq;
  35. /* the list node for the virtqueues list */
  36. struct list_head node;
  37. };
  38. static inline struct virtio_vdpa_device *
  39. to_virtio_vdpa_device(struct virtio_device *dev)
  40. {
  41. return container_of(dev, struct virtio_vdpa_device, vdev);
  42. }
  43. static struct vdpa_device *vd_get_vdpa(struct virtio_device *vdev)
  44. {
  45. return to_virtio_vdpa_device(vdev)->vdpa;
  46. }
  47. static void virtio_vdpa_get(struct virtio_device *vdev, unsigned offset,
  48. void *buf, unsigned len)
  49. {
  50. struct vdpa_device *vdpa = vd_get_vdpa(vdev);
  51. vdpa_get_config(vdpa, offset, buf, len);
  52. }
  53. static void virtio_vdpa_set(struct virtio_device *vdev, unsigned offset,
  54. const void *buf, unsigned len)
  55. {
  56. struct vdpa_device *vdpa = vd_get_vdpa(vdev);
  57. const struct vdpa_config_ops *ops = vdpa->config;
  58. ops->set_config(vdpa, offset, buf, len);
  59. }
  60. static u32 virtio_vdpa_generation(struct virtio_device *vdev)
  61. {
  62. struct vdpa_device *vdpa = vd_get_vdpa(vdev);
  63. const struct vdpa_config_ops *ops = vdpa->config;
  64. if (ops->get_generation)
  65. return ops->get_generation(vdpa);
  66. return 0;
  67. }
  68. static u8 virtio_vdpa_get_status(struct virtio_device *vdev)
  69. {
  70. struct vdpa_device *vdpa = vd_get_vdpa(vdev);
  71. const struct vdpa_config_ops *ops = vdpa->config;
  72. return ops->get_status(vdpa);
  73. }
  74. static void virtio_vdpa_set_status(struct virtio_device *vdev, u8 status)
  75. {
  76. struct vdpa_device *vdpa = vd_get_vdpa(vdev);
  77. const struct vdpa_config_ops *ops = vdpa->config;
  78. return ops->set_status(vdpa, status);
  79. }
  80. static void virtio_vdpa_reset(struct virtio_device *vdev)
  81. {
  82. struct vdpa_device *vdpa = vd_get_vdpa(vdev);
  83. vdpa_reset(vdpa);
  84. }
  85. static bool virtio_vdpa_notify(struct virtqueue *vq)
  86. {
  87. struct vdpa_device *vdpa = vd_get_vdpa(vq->vdev);
  88. const struct vdpa_config_ops *ops = vdpa->config;
  89. ops->kick_vq(vdpa, vq->index);
  90. return true;
  91. }
  92. static irqreturn_t virtio_vdpa_config_cb(void *private)
  93. {
  94. struct virtio_vdpa_device *vd_dev = private;
  95. virtio_config_changed(&vd_dev->vdev);
  96. return IRQ_HANDLED;
  97. }
  98. static irqreturn_t virtio_vdpa_virtqueue_cb(void *private)
  99. {
  100. struct virtio_vdpa_vq_info *info = private;
  101. return vring_interrupt(0, info->vq);
  102. }
  103. static struct virtqueue *
  104. virtio_vdpa_setup_vq(struct virtio_device *vdev, unsigned int index,
  105. void (*callback)(struct virtqueue *vq),
  106. const char *name, bool ctx)
  107. {
  108. struct virtio_vdpa_device *vd_dev = to_virtio_vdpa_device(vdev);
  109. struct vdpa_device *vdpa = vd_get_vdpa(vdev);
  110. const struct vdpa_config_ops *ops = vdpa->config;
  111. struct virtio_vdpa_vq_info *info;
  112. struct vdpa_callback cb;
  113. struct virtqueue *vq;
  114. u64 desc_addr, driver_addr, device_addr;
  115. unsigned long flags;
  116. u32 align, num;
  117. int err;
  118. if (!name)
  119. return NULL;
  120. if (index >= vdpa->nvqs)
  121. return ERR_PTR(-ENOENT);
  122. /* Queue shouldn't already be set up. */
  123. if (ops->get_vq_ready(vdpa, index))
  124. return ERR_PTR(-ENOENT);
  125. /* Allocate and fill out our active queue description */
  126. info = kmalloc(sizeof(*info), GFP_KERNEL);
  127. if (!info)
  128. return ERR_PTR(-ENOMEM);
  129. num = ops->get_vq_num_max(vdpa);
  130. if (num == 0) {
  131. err = -ENOENT;
  132. goto error_new_virtqueue;
  133. }
  134. /* Create the vring */
  135. align = ops->get_vq_align(vdpa);
  136. vq = vring_create_virtqueue(index, num, align, vdev,
  137. true, true, ctx,
  138. virtio_vdpa_notify, callback, name);
  139. if (!vq) {
  140. err = -ENOMEM;
  141. goto error_new_virtqueue;
  142. }
  143. /* Setup virtqueue callback */
  144. cb.callback = virtio_vdpa_virtqueue_cb;
  145. cb.private = info;
  146. ops->set_vq_cb(vdpa, index, &cb);
  147. ops->set_vq_num(vdpa, index, virtqueue_get_vring_size(vq));
  148. desc_addr = virtqueue_get_desc_addr(vq);
  149. driver_addr = virtqueue_get_avail_addr(vq);
  150. device_addr = virtqueue_get_used_addr(vq);
  151. if (ops->set_vq_address(vdpa, index,
  152. desc_addr, driver_addr,
  153. device_addr)) {
  154. err = -EINVAL;
  155. goto err_vq;
  156. }
  157. ops->set_vq_ready(vdpa, index, 1);
  158. vq->priv = info;
  159. info->vq = vq;
  160. spin_lock_irqsave(&vd_dev->lock, flags);
  161. list_add(&info->node, &vd_dev->virtqueues);
  162. spin_unlock_irqrestore(&vd_dev->lock, flags);
  163. return vq;
  164. err_vq:
  165. vring_del_virtqueue(vq);
  166. error_new_virtqueue:
  167. ops->set_vq_ready(vdpa, index, 0);
  168. /* VDPA driver should make sure vq is stopeed here */
  169. WARN_ON(ops->get_vq_ready(vdpa, index));
  170. kfree(info);
  171. return ERR_PTR(err);
  172. }
  173. static void virtio_vdpa_del_vq(struct virtqueue *vq)
  174. {
  175. struct virtio_vdpa_device *vd_dev = to_virtio_vdpa_device(vq->vdev);
  176. struct vdpa_device *vdpa = vd_dev->vdpa;
  177. const struct vdpa_config_ops *ops = vdpa->config;
  178. struct virtio_vdpa_vq_info *info = vq->priv;
  179. unsigned int index = vq->index;
  180. unsigned long flags;
  181. spin_lock_irqsave(&vd_dev->lock, flags);
  182. list_del(&info->node);
  183. spin_unlock_irqrestore(&vd_dev->lock, flags);
  184. /* Select and deactivate the queue */
  185. ops->set_vq_ready(vdpa, index, 0);
  186. WARN_ON(ops->get_vq_ready(vdpa, index));
  187. vring_del_virtqueue(vq);
  188. kfree(info);
  189. }
  190. static void virtio_vdpa_del_vqs(struct virtio_device *vdev)
  191. {
  192. struct virtqueue *vq, *n;
  193. list_for_each_entry_safe(vq, n, &vdev->vqs, list)
  194. virtio_vdpa_del_vq(vq);
  195. }
  196. static int virtio_vdpa_find_vqs(struct virtio_device *vdev, unsigned nvqs,
  197. struct virtqueue *vqs[],
  198. vq_callback_t *callbacks[],
  199. const char * const names[],
  200. const bool *ctx,
  201. struct irq_affinity *desc)
  202. {
  203. struct virtio_vdpa_device *vd_dev = to_virtio_vdpa_device(vdev);
  204. struct vdpa_device *vdpa = vd_get_vdpa(vdev);
  205. const struct vdpa_config_ops *ops = vdpa->config;
  206. struct vdpa_callback cb;
  207. int i, err, queue_idx = 0;
  208. for (i = 0; i < nvqs; ++i) {
  209. if (!names[i]) {
  210. vqs[i] = NULL;
  211. continue;
  212. }
  213. vqs[i] = virtio_vdpa_setup_vq(vdev, queue_idx++,
  214. callbacks[i], names[i], ctx ?
  215. ctx[i] : false);
  216. if (IS_ERR(vqs[i])) {
  217. err = PTR_ERR(vqs[i]);
  218. goto err_setup_vq;
  219. }
  220. }
  221. cb.callback = virtio_vdpa_config_cb;
  222. cb.private = vd_dev;
  223. ops->set_config_cb(vdpa, &cb);
  224. return 0;
  225. err_setup_vq:
  226. virtio_vdpa_del_vqs(vdev);
  227. return err;
  228. }
  229. static u64 virtio_vdpa_get_features(struct virtio_device *vdev)
  230. {
  231. struct vdpa_device *vdpa = vd_get_vdpa(vdev);
  232. const struct vdpa_config_ops *ops = vdpa->config;
  233. return ops->get_features(vdpa);
  234. }
  235. static int virtio_vdpa_finalize_features(struct virtio_device *vdev)
  236. {
  237. struct vdpa_device *vdpa = vd_get_vdpa(vdev);
  238. /* Give virtio_ring a chance to accept features. */
  239. vring_transport_features(vdev);
  240. return vdpa_set_features(vdpa, vdev->features);
  241. }
  242. static const char *virtio_vdpa_bus_name(struct virtio_device *vdev)
  243. {
  244. struct virtio_vdpa_device *vd_dev = to_virtio_vdpa_device(vdev);
  245. struct vdpa_device *vdpa = vd_dev->vdpa;
  246. return dev_name(&vdpa->dev);
  247. }
  248. static const struct virtio_config_ops virtio_vdpa_config_ops = {
  249. .get = virtio_vdpa_get,
  250. .set = virtio_vdpa_set,
  251. .generation = virtio_vdpa_generation,
  252. .get_status = virtio_vdpa_get_status,
  253. .set_status = virtio_vdpa_set_status,
  254. .reset = virtio_vdpa_reset,
  255. .find_vqs = virtio_vdpa_find_vqs,
  256. .del_vqs = virtio_vdpa_del_vqs,
  257. .get_features = virtio_vdpa_get_features,
  258. .finalize_features = virtio_vdpa_finalize_features,
  259. .bus_name = virtio_vdpa_bus_name,
  260. };
  261. static void virtio_vdpa_release_dev(struct device *_d)
  262. {
  263. struct virtio_device *vdev =
  264. container_of(_d, struct virtio_device, dev);
  265. struct virtio_vdpa_device *vd_dev =
  266. container_of(vdev, struct virtio_vdpa_device, vdev);
  267. kfree(vd_dev);
  268. }
  269. static int virtio_vdpa_probe(struct vdpa_device *vdpa)
  270. {
  271. const struct vdpa_config_ops *ops = vdpa->config;
  272. struct virtio_vdpa_device *vd_dev, *reg_dev = NULL;
  273. int ret = -EINVAL;
  274. vd_dev = kzalloc(sizeof(*vd_dev), GFP_KERNEL);
  275. if (!vd_dev)
  276. return -ENOMEM;
  277. vd_dev->vdev.dev.parent = vdpa_get_dma_dev(vdpa);
  278. vd_dev->vdev.dev.release = virtio_vdpa_release_dev;
  279. vd_dev->vdev.config = &virtio_vdpa_config_ops;
  280. vd_dev->vdpa = vdpa;
  281. INIT_LIST_HEAD(&vd_dev->virtqueues);
  282. spin_lock_init(&vd_dev->lock);
  283. vd_dev->vdev.id.device = ops->get_device_id(vdpa);
  284. if (vd_dev->vdev.id.device == 0)
  285. goto err;
  286. vd_dev->vdev.id.vendor = ops->get_vendor_id(vdpa);
  287. ret = register_virtio_device(&vd_dev->vdev);
  288. reg_dev = vd_dev;
  289. if (ret)
  290. goto err;
  291. vdpa_set_drvdata(vdpa, vd_dev);
  292. return 0;
  293. err:
  294. if (reg_dev)
  295. put_device(&vd_dev->vdev.dev);
  296. else
  297. kfree(vd_dev);
  298. return ret;
  299. }
  300. static void virtio_vdpa_remove(struct vdpa_device *vdpa)
  301. {
  302. struct virtio_vdpa_device *vd_dev = vdpa_get_drvdata(vdpa);
  303. unregister_virtio_device(&vd_dev->vdev);
  304. }
  305. static struct vdpa_driver virtio_vdpa_driver = {
  306. .driver = {
  307. .name = "virtio_vdpa",
  308. },
  309. .probe = virtio_vdpa_probe,
  310. .remove = virtio_vdpa_remove,
  311. };
  312. module_vdpa_driver(virtio_vdpa_driver);
  313. MODULE_VERSION(MOD_VERSION);
  314. MODULE_LICENSE(MOD_LICENSE);
  315. MODULE_AUTHOR(MOD_AUTHOR);
  316. MODULE_DESCRIPTION(MOD_DESC);