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