virtio_vdpa.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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/group_cpus.h>
  16. #include <linux/virtio.h>
  17. #include <linux/vdpa.h>
  18. #include <linux/virtio_config.h>
  19. #include <linux/virtio_ring.h>
  20. #define MOD_VERSION "0.1"
  21. #define MOD_AUTHOR "Jason Wang <jasowang@redhat.com>"
  22. #define MOD_DESC "vDPA bus driver for virtio devices"
  23. #define MOD_LICENSE "GPL v2"
  24. struct virtio_vdpa_device {
  25. struct virtio_device vdev;
  26. struct vdpa_device *vdpa;
  27. u64 features;
  28. };
  29. static inline struct virtio_vdpa_device *
  30. to_virtio_vdpa_device(struct virtio_device *dev)
  31. {
  32. return container_of(dev, struct virtio_vdpa_device, vdev);
  33. }
  34. static struct vdpa_device *vd_get_vdpa(struct virtio_device *vdev)
  35. {
  36. return to_virtio_vdpa_device(vdev)->vdpa;
  37. }
  38. static void virtio_vdpa_get(struct virtio_device *vdev, unsigned int offset,
  39. void *buf, unsigned int len)
  40. {
  41. struct vdpa_device *vdpa = vd_get_vdpa(vdev);
  42. vdpa_get_config(vdpa, offset, buf, len);
  43. }
  44. static void virtio_vdpa_set(struct virtio_device *vdev, unsigned int offset,
  45. const void *buf, unsigned int len)
  46. {
  47. struct vdpa_device *vdpa = vd_get_vdpa(vdev);
  48. vdpa_set_config(vdpa, offset, buf, len);
  49. }
  50. static u32 virtio_vdpa_generation(struct virtio_device *vdev)
  51. {
  52. struct vdpa_device *vdpa = vd_get_vdpa(vdev);
  53. const struct vdpa_config_ops *ops = vdpa->config;
  54. if (ops->get_generation)
  55. return ops->get_generation(vdpa);
  56. return 0;
  57. }
  58. static u8 virtio_vdpa_get_status(struct virtio_device *vdev)
  59. {
  60. struct vdpa_device *vdpa = vd_get_vdpa(vdev);
  61. const struct vdpa_config_ops *ops = vdpa->config;
  62. return ops->get_status(vdpa);
  63. }
  64. static void virtio_vdpa_set_status(struct virtio_device *vdev, u8 status)
  65. {
  66. struct vdpa_device *vdpa = vd_get_vdpa(vdev);
  67. vdpa_set_status(vdpa, status);
  68. }
  69. static void virtio_vdpa_reset(struct virtio_device *vdev)
  70. {
  71. struct vdpa_device *vdpa = vd_get_vdpa(vdev);
  72. vdpa_reset(vdpa, 0);
  73. }
  74. static bool virtio_vdpa_notify(struct virtqueue *vq)
  75. {
  76. struct vdpa_device *vdpa = vd_get_vdpa(vq->vdev);
  77. const struct vdpa_config_ops *ops = vdpa->config;
  78. ops->kick_vq(vdpa, vq->index);
  79. return true;
  80. }
  81. static bool virtio_vdpa_notify_with_data(struct virtqueue *vq)
  82. {
  83. struct vdpa_device *vdpa = vd_get_vdpa(vq->vdev);
  84. const struct vdpa_config_ops *ops = vdpa->config;
  85. u32 data = vring_notification_data(vq);
  86. ops->kick_vq_with_data(vdpa, data);
  87. return true;
  88. }
  89. static irqreturn_t virtio_vdpa_config_cb(void *private)
  90. {
  91. struct virtio_vdpa_device *vd_dev = private;
  92. virtio_config_changed(&vd_dev->vdev);
  93. return IRQ_HANDLED;
  94. }
  95. static irqreturn_t virtio_vdpa_virtqueue_cb(void *private)
  96. {
  97. struct virtqueue *vq = private;
  98. return vring_interrupt(0, vq);
  99. }
  100. static struct virtqueue *
  101. virtio_vdpa_setup_vq(struct virtio_device *vdev, unsigned int index,
  102. void (*callback)(struct virtqueue *vq),
  103. const char *name, bool ctx)
  104. {
  105. struct vdpa_device *vdpa = vd_get_vdpa(vdev);
  106. const struct vdpa_config_ops *ops = vdpa->config;
  107. bool (*notify)(struct virtqueue *vq) = virtio_vdpa_notify;
  108. struct vdpa_callback cb;
  109. struct virtqueue *vq;
  110. u64 desc_addr, driver_addr, device_addr;
  111. union virtio_map map = {0};
  112. /* Assume split virtqueue, switch to packed if necessary */
  113. struct vdpa_vq_state state = {0};
  114. u32 align, max_num, min_num = 1;
  115. bool may_reduce_num = true;
  116. int err;
  117. if (!name)
  118. return NULL;
  119. if (index >= vdpa->nvqs)
  120. return ERR_PTR(-ENOENT);
  121. /* We cannot accept VIRTIO_F_NOTIFICATION_DATA without kick_vq_with_data */
  122. if (__virtio_test_bit(vdev, VIRTIO_F_NOTIFICATION_DATA)) {
  123. if (ops->kick_vq_with_data)
  124. notify = virtio_vdpa_notify_with_data;
  125. else
  126. __virtio_clear_bit(vdev, VIRTIO_F_NOTIFICATION_DATA);
  127. }
  128. /* Queue shouldn't already be set up. */
  129. if (ops->get_vq_ready(vdpa, index))
  130. return ERR_PTR(-ENOENT);
  131. if (ops->get_vq_size)
  132. max_num = ops->get_vq_size(vdpa, index);
  133. else
  134. max_num = ops->get_vq_num_max(vdpa);
  135. if (max_num == 0) {
  136. err = -ENOENT;
  137. goto error_new_virtqueue;
  138. }
  139. if (ops->get_vq_num_min)
  140. min_num = ops->get_vq_num_min(vdpa);
  141. may_reduce_num = (max_num != min_num);
  142. /* Create the vring */
  143. align = ops->get_vq_align(vdpa);
  144. if (ops->get_vq_map)
  145. map = ops->get_vq_map(vdpa, index);
  146. else
  147. map = vdpa_get_map(vdpa);
  148. vq = vring_create_virtqueue_map(index, max_num, align, vdev,
  149. true, may_reduce_num, ctx,
  150. notify, callback, name, map);
  151. if (!vq) {
  152. err = -ENOMEM;
  153. goto error_new_virtqueue;
  154. }
  155. if (index == 0)
  156. vdev->vmap = map;
  157. vq->num_max = max_num;
  158. /* Setup virtqueue callback */
  159. cb.callback = callback ? virtio_vdpa_virtqueue_cb : NULL;
  160. cb.private = vq;
  161. cb.trigger = NULL;
  162. ops->set_vq_cb(vdpa, index, &cb);
  163. ops->set_vq_num(vdpa, index, virtqueue_get_vring_size(vq));
  164. desc_addr = virtqueue_get_desc_addr(vq);
  165. driver_addr = virtqueue_get_avail_addr(vq);
  166. device_addr = virtqueue_get_used_addr(vq);
  167. if (ops->set_vq_address(vdpa, index,
  168. desc_addr, driver_addr,
  169. device_addr)) {
  170. err = -EINVAL;
  171. goto err_vq;
  172. }
  173. /* reset virtqueue state index */
  174. if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED)) {
  175. struct vdpa_vq_state_packed *s = &state.packed;
  176. s->last_avail_counter = 1;
  177. s->last_avail_idx = 0;
  178. s->last_used_counter = 1;
  179. s->last_used_idx = 0;
  180. }
  181. err = ops->set_vq_state(vdpa, index, &state);
  182. if (err)
  183. goto err_vq;
  184. ops->set_vq_ready(vdpa, index, 1);
  185. return vq;
  186. err_vq:
  187. vring_del_virtqueue(vq);
  188. error_new_virtqueue:
  189. ops->set_vq_ready(vdpa, index, 0);
  190. /* VDPA driver should make sure vq is stopeed here */
  191. WARN_ON(ops->get_vq_ready(vdpa, index));
  192. return ERR_PTR(err);
  193. }
  194. static void virtio_vdpa_del_vq(struct virtqueue *vq)
  195. {
  196. struct virtio_vdpa_device *vd_dev = to_virtio_vdpa_device(vq->vdev);
  197. struct vdpa_device *vdpa = vd_dev->vdpa;
  198. const struct vdpa_config_ops *ops = vdpa->config;
  199. unsigned int index = vq->index;
  200. /* Select and deactivate the queue (best effort) */
  201. ops->set_vq_ready(vdpa, index, 0);
  202. vring_del_virtqueue(vq);
  203. }
  204. static void virtio_vdpa_del_vqs(struct virtio_device *vdev)
  205. {
  206. struct virtqueue *vq, *n;
  207. list_for_each_entry_safe(vq, n, &vdev->vqs, list)
  208. virtio_vdpa_del_vq(vq);
  209. }
  210. static void default_calc_sets(struct irq_affinity *affd, unsigned int affvecs)
  211. {
  212. affd->nr_sets = 1;
  213. affd->set_size[0] = affvecs;
  214. }
  215. static struct cpumask *
  216. create_affinity_masks(unsigned int nvecs, struct irq_affinity *affd)
  217. {
  218. unsigned int affvecs = 0, curvec, usedvecs, i;
  219. struct cpumask *masks = NULL;
  220. if (nvecs > affd->pre_vectors + affd->post_vectors)
  221. affvecs = nvecs - affd->pre_vectors - affd->post_vectors;
  222. if (!affd->calc_sets)
  223. affd->calc_sets = default_calc_sets;
  224. affd->calc_sets(affd, affvecs);
  225. if (!affvecs)
  226. return NULL;
  227. masks = kzalloc_objs(*masks, nvecs);
  228. if (!masks)
  229. return NULL;
  230. /* Fill out vectors at the beginning that don't need affinity */
  231. for (curvec = 0; curvec < affd->pre_vectors; curvec++)
  232. cpumask_setall(&masks[curvec]);
  233. for (i = 0, usedvecs = 0; i < affd->nr_sets; i++) {
  234. unsigned int this_vecs = affd->set_size[i];
  235. unsigned int nr_masks;
  236. int j;
  237. struct cpumask *result = group_cpus_evenly(this_vecs, &nr_masks);
  238. if (!result) {
  239. kfree(masks);
  240. return NULL;
  241. }
  242. for (j = 0; j < nr_masks; j++)
  243. cpumask_copy(&masks[curvec + j], &result[j]);
  244. kfree(result);
  245. curvec += nr_masks;
  246. usedvecs += nr_masks;
  247. }
  248. /* Fill out vectors at the end that don't need affinity */
  249. if (usedvecs >= affvecs)
  250. curvec = affd->pre_vectors + affvecs;
  251. else
  252. curvec = affd->pre_vectors + usedvecs;
  253. for (; curvec < nvecs; curvec++)
  254. cpumask_setall(&masks[curvec]);
  255. return masks;
  256. }
  257. static int virtio_vdpa_find_vqs(struct virtio_device *vdev, unsigned int nvqs,
  258. struct virtqueue *vqs[],
  259. struct virtqueue_info vqs_info[],
  260. struct irq_affinity *desc)
  261. {
  262. struct virtio_vdpa_device *vd_dev = to_virtio_vdpa_device(vdev);
  263. struct vdpa_device *vdpa = vd_get_vdpa(vdev);
  264. const struct vdpa_config_ops *ops = vdpa->config;
  265. struct cpumask *masks;
  266. struct vdpa_callback cb;
  267. bool has_affinity = desc && ops->set_vq_affinity;
  268. int i, err, queue_idx = 0;
  269. if (has_affinity) {
  270. masks = create_affinity_masks(nvqs, desc);
  271. if (!masks)
  272. return -ENOMEM;
  273. }
  274. for (i = 0; i < nvqs; ++i) {
  275. struct virtqueue_info *vqi = &vqs_info[i];
  276. if (!vqi->name) {
  277. vqs[i] = NULL;
  278. continue;
  279. }
  280. vqs[i] = virtio_vdpa_setup_vq(vdev, queue_idx++, vqi->callback,
  281. vqi->name, vqi->ctx);
  282. if (IS_ERR(vqs[i])) {
  283. err = PTR_ERR(vqs[i]);
  284. goto err_setup_vq;
  285. }
  286. if (has_affinity)
  287. ops->set_vq_affinity(vdpa, i, &masks[i]);
  288. }
  289. cb.callback = virtio_vdpa_config_cb;
  290. cb.private = vd_dev;
  291. ops->set_config_cb(vdpa, &cb);
  292. if (has_affinity)
  293. kfree(masks);
  294. return 0;
  295. err_setup_vq:
  296. virtio_vdpa_del_vqs(vdev);
  297. if (has_affinity)
  298. kfree(masks);
  299. return err;
  300. }
  301. static u64 virtio_vdpa_get_features(struct virtio_device *vdev)
  302. {
  303. struct vdpa_device *vdpa = vd_get_vdpa(vdev);
  304. const struct vdpa_config_ops *ops = vdpa->config;
  305. return ops->get_device_features(vdpa);
  306. }
  307. static int virtio_vdpa_finalize_features(struct virtio_device *vdev)
  308. {
  309. struct vdpa_device *vdpa = vd_get_vdpa(vdev);
  310. /* Give virtio_ring a chance to accept features. */
  311. vring_transport_features(vdev);
  312. return vdpa_set_features(vdpa, vdev->features);
  313. }
  314. static const char *virtio_vdpa_bus_name(struct virtio_device *vdev)
  315. {
  316. struct virtio_vdpa_device *vd_dev = to_virtio_vdpa_device(vdev);
  317. struct vdpa_device *vdpa = vd_dev->vdpa;
  318. return dev_name(&vdpa->dev);
  319. }
  320. static int virtio_vdpa_set_vq_affinity(struct virtqueue *vq,
  321. const struct cpumask *cpu_mask)
  322. {
  323. struct virtio_vdpa_device *vd_dev = to_virtio_vdpa_device(vq->vdev);
  324. struct vdpa_device *vdpa = vd_dev->vdpa;
  325. const struct vdpa_config_ops *ops = vdpa->config;
  326. unsigned int index = vq->index;
  327. if (ops->set_vq_affinity)
  328. return ops->set_vq_affinity(vdpa, index, cpu_mask);
  329. return 0;
  330. }
  331. static const struct cpumask *
  332. virtio_vdpa_get_vq_affinity(struct virtio_device *vdev, int index)
  333. {
  334. struct vdpa_device *vdpa = vd_get_vdpa(vdev);
  335. const struct vdpa_config_ops *ops = vdpa->config;
  336. if (ops->get_vq_affinity)
  337. return ops->get_vq_affinity(vdpa, index);
  338. return NULL;
  339. }
  340. static const struct virtio_config_ops virtio_vdpa_config_ops = {
  341. .get = virtio_vdpa_get,
  342. .set = virtio_vdpa_set,
  343. .generation = virtio_vdpa_generation,
  344. .get_status = virtio_vdpa_get_status,
  345. .set_status = virtio_vdpa_set_status,
  346. .reset = virtio_vdpa_reset,
  347. .find_vqs = virtio_vdpa_find_vqs,
  348. .del_vqs = virtio_vdpa_del_vqs,
  349. .get_features = virtio_vdpa_get_features,
  350. .finalize_features = virtio_vdpa_finalize_features,
  351. .bus_name = virtio_vdpa_bus_name,
  352. .set_vq_affinity = virtio_vdpa_set_vq_affinity,
  353. .get_vq_affinity = virtio_vdpa_get_vq_affinity,
  354. };
  355. static void virtio_vdpa_release_dev(struct device *_d)
  356. {
  357. struct virtio_device *vdev =
  358. container_of(_d, struct virtio_device, dev);
  359. struct virtio_vdpa_device *vd_dev =
  360. container_of(vdev, struct virtio_vdpa_device, vdev);
  361. kfree(vd_dev);
  362. }
  363. static int virtio_vdpa_probe(struct vdpa_device *vdpa)
  364. {
  365. const struct vdpa_config_ops *ops = vdpa->config;
  366. struct virtio_vdpa_device *vd_dev, *reg_dev = NULL;
  367. int ret = -EINVAL;
  368. vd_dev = kzalloc_obj(*vd_dev);
  369. if (!vd_dev)
  370. return -ENOMEM;
  371. vd_dev->vdev.dev.parent = vdpa->map ? &vdpa->dev :
  372. vdpa_get_map(vdpa).dma_dev;
  373. vd_dev->vdev.dev.release = virtio_vdpa_release_dev;
  374. vd_dev->vdev.config = &virtio_vdpa_config_ops;
  375. vd_dev->vdev.map = vdpa->map;
  376. vd_dev->vdpa = vdpa;
  377. vd_dev->vdev.id.device = ops->get_device_id(vdpa);
  378. if (vd_dev->vdev.id.device == 0)
  379. goto err;
  380. vd_dev->vdev.id.vendor = ops->get_vendor_id(vdpa);
  381. ret = register_virtio_device(&vd_dev->vdev);
  382. reg_dev = vd_dev;
  383. if (ret)
  384. goto err;
  385. vdpa_set_drvdata(vdpa, vd_dev);
  386. return 0;
  387. err:
  388. if (reg_dev)
  389. put_device(&vd_dev->vdev.dev);
  390. else
  391. kfree(vd_dev);
  392. return ret;
  393. }
  394. static void virtio_vdpa_remove(struct vdpa_device *vdpa)
  395. {
  396. struct virtio_vdpa_device *vd_dev = vdpa_get_drvdata(vdpa);
  397. unregister_virtio_device(&vd_dev->vdev);
  398. }
  399. static struct vdpa_driver virtio_vdpa_driver = {
  400. .driver = {
  401. .name = "virtio_vdpa",
  402. },
  403. .probe = virtio_vdpa_probe,
  404. .remove = virtio_vdpa_remove,
  405. };
  406. module_vdpa_driver(virtio_vdpa_driver);
  407. MODULE_VERSION(MOD_VERSION);
  408. MODULE_LICENSE(MOD_LICENSE);
  409. MODULE_AUTHOR(MOD_AUTHOR);
  410. MODULE_DESCRIPTION(MOD_DESC);