vdpa_sim.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * VDPA networking device simulator.
  4. *
  5. * Copyright (c) 2020, Red Hat Inc. 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/fs.h>
  14. #include <linux/poll.h>
  15. #include <linux/slab.h>
  16. #include <linux/sched.h>
  17. #include <linux/wait.h>
  18. #include <linux/uuid.h>
  19. #include <linux/iommu.h>
  20. #include <linux/dma-map-ops.h>
  21. #include <linux/sysfs.h>
  22. #include <linux/file.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/vringh.h>
  25. #include <linux/vdpa.h>
  26. #include <linux/virtio_byteorder.h>
  27. #include <linux/vhost_iotlb.h>
  28. #include <uapi/linux/virtio_config.h>
  29. #include <uapi/linux/virtio_net.h>
  30. #define DRV_VERSION "0.1"
  31. #define DRV_AUTHOR "Jason Wang <jasowang@redhat.com>"
  32. #define DRV_DESC "vDPA Device Simulator"
  33. #define DRV_LICENSE "GPL v2"
  34. static int batch_mapping = 1;
  35. module_param(batch_mapping, int, 0444);
  36. MODULE_PARM_DESC(batch_mapping, "Batched mapping 1 -Enable; 0 - Disable");
  37. static char *macaddr;
  38. module_param(macaddr, charp, 0);
  39. MODULE_PARM_DESC(macaddr, "Ethernet MAC address");
  40. u8 macaddr_buf[ETH_ALEN];
  41. struct vdpasim_virtqueue {
  42. struct vringh vring;
  43. struct vringh_kiov iov;
  44. unsigned short head;
  45. bool ready;
  46. u64 desc_addr;
  47. u64 device_addr;
  48. u64 driver_addr;
  49. u32 num;
  50. void *private;
  51. irqreturn_t (*cb)(void *data);
  52. };
  53. #define VDPASIM_QUEUE_ALIGN PAGE_SIZE
  54. #define VDPASIM_QUEUE_MAX 256
  55. #define VDPASIM_DEVICE_ID 0x1
  56. #define VDPASIM_VENDOR_ID 0
  57. #define VDPASIM_VQ_NUM 0x2
  58. #define VDPASIM_NAME "vdpasim-netdev"
  59. static u64 vdpasim_features = (1ULL << VIRTIO_F_ANY_LAYOUT) |
  60. (1ULL << VIRTIO_F_VERSION_1) |
  61. (1ULL << VIRTIO_F_ACCESS_PLATFORM) |
  62. (1ULL << VIRTIO_NET_F_MAC);
  63. struct vdpasim;
  64. struct vdpasim_dev_attr {
  65. size_t config_size;
  66. int nvqs;
  67. void (*get_config)(struct vdpasim *vdpasim, void *config);
  68. };
  69. /* State of each vdpasim device */
  70. struct vdpasim {
  71. struct vdpa_device vdpa;
  72. struct vdpasim_virtqueue *vqs;
  73. struct work_struct work;
  74. struct vdpasim_dev_attr dev_attr;
  75. /* spinlock to synchronize virtqueue state */
  76. spinlock_t lock;
  77. /* virtio config according to device type */
  78. void *config;
  79. struct vhost_iotlb *iommu;
  80. void *buffer;
  81. u32 status;
  82. u32 generation;
  83. u64 features;
  84. /* spinlock to synchronize iommu table */
  85. spinlock_t iommu_lock;
  86. };
  87. /* TODO: cross-endian support */
  88. static inline bool vdpasim_is_little_endian(struct vdpasim *vdpasim)
  89. {
  90. return virtio_legacy_is_little_endian() ||
  91. (vdpasim->features & (1ULL << VIRTIO_F_VERSION_1));
  92. }
  93. static inline u16 vdpasim16_to_cpu(struct vdpasim *vdpasim, __virtio16 val)
  94. {
  95. return __virtio16_to_cpu(vdpasim_is_little_endian(vdpasim), val);
  96. }
  97. static inline __virtio16 cpu_to_vdpasim16(struct vdpasim *vdpasim, u16 val)
  98. {
  99. return __cpu_to_virtio16(vdpasim_is_little_endian(vdpasim), val);
  100. }
  101. static struct vdpasim *vdpasim_dev;
  102. static struct vdpasim *vdpa_to_sim(struct vdpa_device *vdpa)
  103. {
  104. return container_of(vdpa, struct vdpasim, vdpa);
  105. }
  106. static struct vdpasim *dev_to_sim(struct device *dev)
  107. {
  108. struct vdpa_device *vdpa = dev_to_vdpa(dev);
  109. return vdpa_to_sim(vdpa);
  110. }
  111. static void vdpasim_queue_ready(struct vdpasim *vdpasim, unsigned int idx)
  112. {
  113. struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
  114. vringh_init_iotlb(&vq->vring, vdpasim_features,
  115. VDPASIM_QUEUE_MAX, false,
  116. (struct vring_desc *)(uintptr_t)vq->desc_addr,
  117. (struct vring_avail *)
  118. (uintptr_t)vq->driver_addr,
  119. (struct vring_used *)
  120. (uintptr_t)vq->device_addr);
  121. }
  122. static void vdpasim_vq_reset(struct vdpasim_virtqueue *vq)
  123. {
  124. vq->ready = false;
  125. vq->desc_addr = 0;
  126. vq->driver_addr = 0;
  127. vq->device_addr = 0;
  128. vq->cb = NULL;
  129. vq->private = NULL;
  130. vringh_init_iotlb(&vq->vring, vdpasim_features, VDPASIM_QUEUE_MAX,
  131. false, NULL, NULL, NULL);
  132. }
  133. static void vdpasim_reset(struct vdpasim *vdpasim)
  134. {
  135. int i;
  136. for (i = 0; i < vdpasim->dev_attr.nvqs; i++)
  137. vdpasim_vq_reset(&vdpasim->vqs[i]);
  138. spin_lock(&vdpasim->iommu_lock);
  139. vhost_iotlb_reset(vdpasim->iommu);
  140. spin_unlock(&vdpasim->iommu_lock);
  141. vdpasim->features = 0;
  142. vdpasim->status = 0;
  143. ++vdpasim->generation;
  144. }
  145. static void vdpasim_work(struct work_struct *work)
  146. {
  147. struct vdpasim *vdpasim = container_of(work, struct
  148. vdpasim, work);
  149. struct vdpasim_virtqueue *txq = &vdpasim->vqs[1];
  150. struct vdpasim_virtqueue *rxq = &vdpasim->vqs[0];
  151. ssize_t read, write;
  152. size_t total_write;
  153. int pkts = 0;
  154. int err;
  155. spin_lock(&vdpasim->lock);
  156. if (!(vdpasim->status & VIRTIO_CONFIG_S_DRIVER_OK))
  157. goto out;
  158. if (!txq->ready || !rxq->ready)
  159. goto out;
  160. while (true) {
  161. total_write = 0;
  162. err = vringh_getdesc_iotlb(&txq->vring, &txq->iov, NULL,
  163. &txq->head, GFP_ATOMIC);
  164. if (err <= 0)
  165. break;
  166. err = vringh_getdesc_iotlb(&rxq->vring, NULL, &rxq->iov,
  167. &rxq->head, GFP_ATOMIC);
  168. if (err <= 0) {
  169. vringh_complete_iotlb(&txq->vring, txq->head, 0);
  170. break;
  171. }
  172. while (true) {
  173. read = vringh_iov_pull_iotlb(&txq->vring, &txq->iov,
  174. vdpasim->buffer,
  175. PAGE_SIZE);
  176. if (read <= 0)
  177. break;
  178. write = vringh_iov_push_iotlb(&rxq->vring, &rxq->iov,
  179. vdpasim->buffer, read);
  180. if (write <= 0)
  181. break;
  182. total_write += write;
  183. }
  184. /* Make sure data is wrote before advancing index */
  185. smp_wmb();
  186. vringh_complete_iotlb(&txq->vring, txq->head, 0);
  187. vringh_complete_iotlb(&rxq->vring, rxq->head, total_write);
  188. /* Make sure used is visible before rasing the interrupt. */
  189. smp_wmb();
  190. local_bh_disable();
  191. if (txq->cb)
  192. txq->cb(txq->private);
  193. if (rxq->cb)
  194. rxq->cb(rxq->private);
  195. local_bh_enable();
  196. if (++pkts > 4) {
  197. schedule_work(&vdpasim->work);
  198. goto out;
  199. }
  200. }
  201. out:
  202. spin_unlock(&vdpasim->lock);
  203. }
  204. static int dir_to_perm(enum dma_data_direction dir)
  205. {
  206. int perm = -EFAULT;
  207. switch (dir) {
  208. case DMA_FROM_DEVICE:
  209. perm = VHOST_MAP_WO;
  210. break;
  211. case DMA_TO_DEVICE:
  212. perm = VHOST_MAP_RO;
  213. break;
  214. case DMA_BIDIRECTIONAL:
  215. perm = VHOST_MAP_RW;
  216. break;
  217. default:
  218. break;
  219. }
  220. return perm;
  221. }
  222. static dma_addr_t vdpasim_map_page(struct device *dev, struct page *page,
  223. unsigned long offset, size_t size,
  224. enum dma_data_direction dir,
  225. unsigned long attrs)
  226. {
  227. struct vdpasim *vdpasim = dev_to_sim(dev);
  228. struct vhost_iotlb *iommu = vdpasim->iommu;
  229. u64 pa = (page_to_pfn(page) << PAGE_SHIFT) + offset;
  230. int ret, perm = dir_to_perm(dir);
  231. if (perm < 0)
  232. return DMA_MAPPING_ERROR;
  233. /* For simplicity, use identical mapping to avoid e.g iova
  234. * allocator.
  235. */
  236. spin_lock(&vdpasim->iommu_lock);
  237. ret = vhost_iotlb_add_range(iommu, pa, pa + size - 1,
  238. pa, dir_to_perm(dir));
  239. spin_unlock(&vdpasim->iommu_lock);
  240. if (ret)
  241. return DMA_MAPPING_ERROR;
  242. return (dma_addr_t)(pa);
  243. }
  244. static void vdpasim_unmap_page(struct device *dev, dma_addr_t dma_addr,
  245. size_t size, enum dma_data_direction dir,
  246. unsigned long attrs)
  247. {
  248. struct vdpasim *vdpasim = dev_to_sim(dev);
  249. struct vhost_iotlb *iommu = vdpasim->iommu;
  250. spin_lock(&vdpasim->iommu_lock);
  251. vhost_iotlb_del_range(iommu, (u64)dma_addr,
  252. (u64)dma_addr + size - 1);
  253. spin_unlock(&vdpasim->iommu_lock);
  254. }
  255. static void *vdpasim_alloc_coherent(struct device *dev, size_t size,
  256. dma_addr_t *dma_addr, gfp_t flag,
  257. unsigned long attrs)
  258. {
  259. struct vdpasim *vdpasim = dev_to_sim(dev);
  260. struct vhost_iotlb *iommu = vdpasim->iommu;
  261. void *addr = kmalloc(size, flag);
  262. int ret;
  263. spin_lock(&vdpasim->iommu_lock);
  264. if (!addr) {
  265. *dma_addr = DMA_MAPPING_ERROR;
  266. } else {
  267. u64 pa = virt_to_phys(addr);
  268. ret = vhost_iotlb_add_range(iommu, (u64)pa,
  269. (u64)pa + size - 1,
  270. pa, VHOST_MAP_RW);
  271. if (ret) {
  272. *dma_addr = DMA_MAPPING_ERROR;
  273. kfree(addr);
  274. addr = NULL;
  275. } else
  276. *dma_addr = (dma_addr_t)pa;
  277. }
  278. spin_unlock(&vdpasim->iommu_lock);
  279. return addr;
  280. }
  281. static void vdpasim_free_coherent(struct device *dev, size_t size,
  282. void *vaddr, dma_addr_t dma_addr,
  283. unsigned long attrs)
  284. {
  285. struct vdpasim *vdpasim = dev_to_sim(dev);
  286. struct vhost_iotlb *iommu = vdpasim->iommu;
  287. spin_lock(&vdpasim->iommu_lock);
  288. vhost_iotlb_del_range(iommu, (u64)dma_addr,
  289. (u64)dma_addr + size - 1);
  290. spin_unlock(&vdpasim->iommu_lock);
  291. kfree(phys_to_virt((uintptr_t)dma_addr));
  292. }
  293. static const struct dma_map_ops vdpasim_dma_ops = {
  294. .map_page = vdpasim_map_page,
  295. .unmap_page = vdpasim_unmap_page,
  296. .alloc = vdpasim_alloc_coherent,
  297. .free = vdpasim_free_coherent,
  298. };
  299. static const struct vdpa_config_ops vdpasim_net_config_ops;
  300. static const struct vdpa_config_ops vdpasim_net_batch_config_ops;
  301. static struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr)
  302. {
  303. const struct vdpa_config_ops *ops;
  304. struct vdpasim *vdpasim;
  305. struct device *dev;
  306. int i, ret = -ENOMEM;
  307. if (batch_mapping)
  308. ops = &vdpasim_net_batch_config_ops;
  309. else
  310. ops = &vdpasim_net_config_ops;
  311. vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops,
  312. dev_attr->nvqs);
  313. if (!vdpasim)
  314. goto err_alloc;
  315. vdpasim->dev_attr = *dev_attr;
  316. INIT_WORK(&vdpasim->work, vdpasim_work);
  317. spin_lock_init(&vdpasim->lock);
  318. spin_lock_init(&vdpasim->iommu_lock);
  319. dev = &vdpasim->vdpa.dev;
  320. dev->dma_mask = &dev->coherent_dma_mask;
  321. if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)))
  322. goto err_iommu;
  323. set_dma_ops(dev, &vdpasim_dma_ops);
  324. vdpasim->config = kzalloc(dev_attr->config_size, GFP_KERNEL);
  325. if (!vdpasim->config)
  326. goto err_iommu;
  327. vdpasim->vqs = kcalloc(dev_attr->nvqs, sizeof(struct vdpasim_virtqueue),
  328. GFP_KERNEL);
  329. if (!vdpasim->vqs)
  330. goto err_iommu;
  331. vdpasim->iommu = vhost_iotlb_alloc(2048, 0);
  332. if (!vdpasim->iommu)
  333. goto err_iommu;
  334. vdpasim->buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
  335. if (!vdpasim->buffer)
  336. goto err_iommu;
  337. if (macaddr) {
  338. mac_pton(macaddr, macaddr_buf);
  339. if (!is_valid_ether_addr(macaddr_buf)) {
  340. ret = -EADDRNOTAVAIL;
  341. goto err_iommu;
  342. }
  343. } else {
  344. eth_random_addr(macaddr_buf);
  345. }
  346. for (i = 0; i < dev_attr->nvqs; i++)
  347. vringh_set_iotlb(&vdpasim->vqs[i].vring, vdpasim->iommu);
  348. vdpasim->vdpa.dma_dev = dev;
  349. ret = vdpa_register_device(&vdpasim->vdpa);
  350. if (ret)
  351. goto err_iommu;
  352. return vdpasim;
  353. err_iommu:
  354. put_device(dev);
  355. err_alloc:
  356. return ERR_PTR(ret);
  357. }
  358. static int vdpasim_set_vq_address(struct vdpa_device *vdpa, u16 idx,
  359. u64 desc_area, u64 driver_area,
  360. u64 device_area)
  361. {
  362. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  363. struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
  364. vq->desc_addr = desc_area;
  365. vq->driver_addr = driver_area;
  366. vq->device_addr = device_area;
  367. return 0;
  368. }
  369. static void vdpasim_set_vq_num(struct vdpa_device *vdpa, u16 idx, u32 num)
  370. {
  371. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  372. struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
  373. vq->num = num;
  374. }
  375. static void vdpasim_kick_vq(struct vdpa_device *vdpa, u16 idx)
  376. {
  377. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  378. struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
  379. if (vq->ready)
  380. schedule_work(&vdpasim->work);
  381. }
  382. static void vdpasim_set_vq_cb(struct vdpa_device *vdpa, u16 idx,
  383. struct vdpa_callback *cb)
  384. {
  385. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  386. struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
  387. vq->cb = cb->callback;
  388. vq->private = cb->private;
  389. }
  390. static void vdpasim_set_vq_ready(struct vdpa_device *vdpa, u16 idx, bool ready)
  391. {
  392. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  393. struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
  394. spin_lock(&vdpasim->lock);
  395. vq->ready = ready;
  396. if (vq->ready)
  397. vdpasim_queue_ready(vdpasim, idx);
  398. spin_unlock(&vdpasim->lock);
  399. }
  400. static bool vdpasim_get_vq_ready(struct vdpa_device *vdpa, u16 idx)
  401. {
  402. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  403. struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
  404. return vq->ready;
  405. }
  406. static int vdpasim_set_vq_state(struct vdpa_device *vdpa, u16 idx,
  407. const struct vdpa_vq_state *state)
  408. {
  409. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  410. struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
  411. struct vringh *vrh = &vq->vring;
  412. spin_lock(&vdpasim->lock);
  413. vrh->last_avail_idx = state->avail_index;
  414. spin_unlock(&vdpasim->lock);
  415. return 0;
  416. }
  417. static int vdpasim_get_vq_state(struct vdpa_device *vdpa, u16 idx,
  418. struct vdpa_vq_state *state)
  419. {
  420. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  421. struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
  422. struct vringh *vrh = &vq->vring;
  423. state->avail_index = vrh->last_avail_idx;
  424. return 0;
  425. }
  426. static u32 vdpasim_get_vq_align(struct vdpa_device *vdpa)
  427. {
  428. return VDPASIM_QUEUE_ALIGN;
  429. }
  430. static u64 vdpasim_get_features(struct vdpa_device *vdpa)
  431. {
  432. return vdpasim_features;
  433. }
  434. static int vdpasim_set_features(struct vdpa_device *vdpa, u64 features)
  435. {
  436. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  437. /* DMA mapping must be done by driver */
  438. if (!(features & (1ULL << VIRTIO_F_ACCESS_PLATFORM)))
  439. return -EINVAL;
  440. vdpasim->features = features & vdpasim_features;
  441. return 0;
  442. }
  443. static void vdpasim_set_config_cb(struct vdpa_device *vdpa,
  444. struct vdpa_callback *cb)
  445. {
  446. /* We don't support config interrupt */
  447. }
  448. static u16 vdpasim_get_vq_num_max(struct vdpa_device *vdpa)
  449. {
  450. return VDPASIM_QUEUE_MAX;
  451. }
  452. static u32 vdpasim_get_device_id(struct vdpa_device *vdpa)
  453. {
  454. return VDPASIM_DEVICE_ID;
  455. }
  456. static u32 vdpasim_get_vendor_id(struct vdpa_device *vdpa)
  457. {
  458. return VDPASIM_VENDOR_ID;
  459. }
  460. static u8 vdpasim_get_status(struct vdpa_device *vdpa)
  461. {
  462. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  463. u8 status;
  464. spin_lock(&vdpasim->lock);
  465. status = vdpasim->status;
  466. spin_unlock(&vdpasim->lock);
  467. return status;
  468. }
  469. static void vdpasim_set_status(struct vdpa_device *vdpa, u8 status)
  470. {
  471. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  472. spin_lock(&vdpasim->lock);
  473. vdpasim->status = status;
  474. if (status == 0)
  475. vdpasim_reset(vdpasim);
  476. spin_unlock(&vdpasim->lock);
  477. }
  478. static void vdpasim_get_config(struct vdpa_device *vdpa, unsigned int offset,
  479. void *buf, unsigned int len)
  480. {
  481. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  482. if (offset + len > vdpasim->dev_attr.config_size)
  483. return;
  484. if (vdpasim->dev_attr.get_config)
  485. vdpasim->dev_attr.get_config(vdpasim, vdpasim->config);
  486. memcpy(buf, vdpasim->config + offset, len);
  487. }
  488. static void vdpasim_set_config(struct vdpa_device *vdpa, unsigned int offset,
  489. const void *buf, unsigned int len)
  490. {
  491. /* No writable config supportted by vdpasim */
  492. }
  493. static u32 vdpasim_get_generation(struct vdpa_device *vdpa)
  494. {
  495. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  496. return vdpasim->generation;
  497. }
  498. static struct vdpa_iova_range vdpasim_get_iova_range(struct vdpa_device *vdpa)
  499. {
  500. struct vdpa_iova_range range = {
  501. .first = 0ULL,
  502. .last = ULLONG_MAX,
  503. };
  504. return range;
  505. }
  506. static int vdpasim_set_map(struct vdpa_device *vdpa,
  507. struct vhost_iotlb *iotlb)
  508. {
  509. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  510. struct vhost_iotlb_map *map;
  511. u64 start = 0ULL, last = 0ULL - 1;
  512. int ret;
  513. spin_lock(&vdpasim->iommu_lock);
  514. vhost_iotlb_reset(vdpasim->iommu);
  515. for (map = vhost_iotlb_itree_first(iotlb, start, last); map;
  516. map = vhost_iotlb_itree_next(map, start, last)) {
  517. ret = vhost_iotlb_add_range(vdpasim->iommu, map->start,
  518. map->last, map->addr, map->perm);
  519. if (ret)
  520. goto err;
  521. }
  522. spin_unlock(&vdpasim->iommu_lock);
  523. return 0;
  524. err:
  525. vhost_iotlb_reset(vdpasim->iommu);
  526. spin_unlock(&vdpasim->iommu_lock);
  527. return ret;
  528. }
  529. static int vdpasim_dma_map(struct vdpa_device *vdpa, u64 iova, u64 size,
  530. u64 pa, u32 perm)
  531. {
  532. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  533. int ret;
  534. spin_lock(&vdpasim->iommu_lock);
  535. ret = vhost_iotlb_add_range(vdpasim->iommu, iova, iova + size - 1, pa,
  536. perm);
  537. spin_unlock(&vdpasim->iommu_lock);
  538. return ret;
  539. }
  540. static int vdpasim_dma_unmap(struct vdpa_device *vdpa, u64 iova, u64 size)
  541. {
  542. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  543. spin_lock(&vdpasim->iommu_lock);
  544. vhost_iotlb_del_range(vdpasim->iommu, iova, iova + size - 1);
  545. spin_unlock(&vdpasim->iommu_lock);
  546. return 0;
  547. }
  548. static void vdpasim_free(struct vdpa_device *vdpa)
  549. {
  550. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  551. cancel_work_sync(&vdpasim->work);
  552. kfree(vdpasim->buffer);
  553. if (vdpasim->iommu)
  554. vhost_iotlb_free(vdpasim->iommu);
  555. kfree(vdpasim->vqs);
  556. kfree(vdpasim->config);
  557. }
  558. static const struct vdpa_config_ops vdpasim_net_config_ops = {
  559. .set_vq_address = vdpasim_set_vq_address,
  560. .set_vq_num = vdpasim_set_vq_num,
  561. .kick_vq = vdpasim_kick_vq,
  562. .set_vq_cb = vdpasim_set_vq_cb,
  563. .set_vq_ready = vdpasim_set_vq_ready,
  564. .get_vq_ready = vdpasim_get_vq_ready,
  565. .set_vq_state = vdpasim_set_vq_state,
  566. .get_vq_state = vdpasim_get_vq_state,
  567. .get_vq_align = vdpasim_get_vq_align,
  568. .get_features = vdpasim_get_features,
  569. .set_features = vdpasim_set_features,
  570. .set_config_cb = vdpasim_set_config_cb,
  571. .get_vq_num_max = vdpasim_get_vq_num_max,
  572. .get_device_id = vdpasim_get_device_id,
  573. .get_vendor_id = vdpasim_get_vendor_id,
  574. .get_status = vdpasim_get_status,
  575. .set_status = vdpasim_set_status,
  576. .get_config = vdpasim_get_config,
  577. .set_config = vdpasim_set_config,
  578. .get_generation = vdpasim_get_generation,
  579. .get_iova_range = vdpasim_get_iova_range,
  580. .dma_map = vdpasim_dma_map,
  581. .dma_unmap = vdpasim_dma_unmap,
  582. .free = vdpasim_free,
  583. };
  584. static const struct vdpa_config_ops vdpasim_net_batch_config_ops = {
  585. .set_vq_address = vdpasim_set_vq_address,
  586. .set_vq_num = vdpasim_set_vq_num,
  587. .kick_vq = vdpasim_kick_vq,
  588. .set_vq_cb = vdpasim_set_vq_cb,
  589. .set_vq_ready = vdpasim_set_vq_ready,
  590. .get_vq_ready = vdpasim_get_vq_ready,
  591. .set_vq_state = vdpasim_set_vq_state,
  592. .get_vq_state = vdpasim_get_vq_state,
  593. .get_vq_align = vdpasim_get_vq_align,
  594. .get_features = vdpasim_get_features,
  595. .set_features = vdpasim_set_features,
  596. .set_config_cb = vdpasim_set_config_cb,
  597. .get_vq_num_max = vdpasim_get_vq_num_max,
  598. .get_device_id = vdpasim_get_device_id,
  599. .get_vendor_id = vdpasim_get_vendor_id,
  600. .get_status = vdpasim_get_status,
  601. .set_status = vdpasim_set_status,
  602. .get_config = vdpasim_get_config,
  603. .set_config = vdpasim_set_config,
  604. .get_generation = vdpasim_get_generation,
  605. .get_iova_range = vdpasim_get_iova_range,
  606. .set_map = vdpasim_set_map,
  607. .free = vdpasim_free,
  608. };
  609. static void vdpasim_net_get_config(struct vdpasim *vdpasim, void *config)
  610. {
  611. struct virtio_net_config *net_config =
  612. (struct virtio_net_config *)config;
  613. net_config->mtu = cpu_to_vdpasim16(vdpasim, 1500);
  614. net_config->status = cpu_to_vdpasim16(vdpasim, VIRTIO_NET_S_LINK_UP);
  615. memcpy(net_config->mac, macaddr_buf, ETH_ALEN);
  616. }
  617. static int __init vdpasim_dev_init(void)
  618. {
  619. struct vdpasim_dev_attr dev_attr = {};
  620. dev_attr.nvqs = VDPASIM_VQ_NUM;
  621. dev_attr.config_size = sizeof(struct virtio_net_config);
  622. dev_attr.get_config = vdpasim_net_get_config;
  623. vdpasim_dev = vdpasim_create(&dev_attr);
  624. if (!IS_ERR(vdpasim_dev))
  625. return 0;
  626. return PTR_ERR(vdpasim_dev);
  627. }
  628. static void __exit vdpasim_dev_exit(void)
  629. {
  630. struct vdpa_device *vdpa = &vdpasim_dev->vdpa;
  631. vdpa_unregister_device(vdpa);
  632. }
  633. module_init(vdpasim_dev_init)
  634. module_exit(vdpasim_dev_exit)
  635. MODULE_VERSION(DRV_VERSION);
  636. MODULE_LICENSE(DRV_LICENSE);
  637. MODULE_AUTHOR(DRV_AUTHOR);
  638. MODULE_DESCRIPTION(DRV_DESC);