virtio_vdpa.c 13 KB

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