4343#define VDPASIM_BLK_AS_NUM 1
4444#define VDPASIM_BLK_GROUP_NUM 1
4545
46+ struct vdpasim_blk {
47+ struct vdpasim vdpasim ;
48+ void * buffer ;
49+ };
50+
51+ static struct vdpasim_blk * sim_to_blk (struct vdpasim * vdpasim )
52+ {
53+ return container_of (vdpasim , struct vdpasim_blk , vdpasim );
54+ }
55+
4656static char vdpasim_blk_id [VIRTIO_BLK_ID_BYTES ] = "vdpa_blk_sim" ;
4757
4858static bool vdpasim_blk_check_range (struct vdpasim * vdpasim , u64 start_sector ,
@@ -78,6 +88,7 @@ static bool vdpasim_blk_check_range(struct vdpasim *vdpasim, u64 start_sector,
7888static bool vdpasim_blk_handle_req (struct vdpasim * vdpasim ,
7989 struct vdpasim_virtqueue * vq )
8090{
91+ struct vdpasim_blk * blk = sim_to_blk (vdpasim );
8192 size_t pushed = 0 , to_pull , to_push ;
8293 struct virtio_blk_outhdr hdr ;
8394 bool handled = false;
@@ -144,8 +155,7 @@ static bool vdpasim_blk_handle_req(struct vdpasim *vdpasim,
144155 }
145156
146157 bytes = vringh_iov_push_iotlb (& vq -> vring , & vq -> in_iov ,
147- vdpasim -> buffer + offset ,
148- to_push );
158+ blk -> buffer + offset , to_push );
149159 if (bytes < 0 ) {
150160 dev_dbg (& vdpasim -> vdpa .dev ,
151161 "vringh_iov_push_iotlb() error: %zd offset: 0x%llx len: 0x%zx\n" ,
@@ -166,8 +176,7 @@ static bool vdpasim_blk_handle_req(struct vdpasim *vdpasim,
166176 }
167177
168178 bytes = vringh_iov_pull_iotlb (& vq -> vring , & vq -> out_iov ,
169- vdpasim -> buffer + offset ,
170- to_pull );
179+ blk -> buffer + offset , to_pull );
171180 if (bytes < 0 ) {
172181 dev_dbg (& vdpasim -> vdpa .dev ,
173182 "vringh_iov_pull_iotlb() error: %zd offset: 0x%llx len: 0x%zx\n" ,
@@ -247,7 +256,7 @@ static bool vdpasim_blk_handle_req(struct vdpasim *vdpasim,
247256 }
248257
249258 if (type == VIRTIO_BLK_T_WRITE_ZEROES ) {
250- memset (vdpasim -> buffer + offset , 0 ,
259+ memset (blk -> buffer + offset , 0 ,
251260 num_sectors << SECTOR_SHIFT );
252261 }
253262
@@ -353,6 +362,13 @@ static void vdpasim_blk_get_config(struct vdpasim *vdpasim, void *config)
353362
354363}
355364
365+ static void vdpasim_blk_free (struct vdpasim * vdpasim )
366+ {
367+ struct vdpasim_blk * blk = sim_to_blk (vdpasim );
368+
369+ kvfree (blk -> buffer );
370+ }
371+
356372static void vdpasim_blk_mgmtdev_release (struct device * dev )
357373{
358374}
@@ -366,6 +382,7 @@ static int vdpasim_blk_dev_add(struct vdpa_mgmt_dev *mdev, const char *name,
366382 const struct vdpa_dev_set_config * config )
367383{
368384 struct vdpasim_dev_attr dev_attr = {};
385+ struct vdpasim_blk * blk ;
369386 struct vdpasim * simdev ;
370387 int ret ;
371388
@@ -376,16 +393,25 @@ static int vdpasim_blk_dev_add(struct vdpa_mgmt_dev *mdev, const char *name,
376393 dev_attr .nvqs = VDPASIM_BLK_VQ_NUM ;
377394 dev_attr .ngroups = VDPASIM_BLK_GROUP_NUM ;
378395 dev_attr .nas = VDPASIM_BLK_AS_NUM ;
379- dev_attr .alloc_size = sizeof (struct vdpasim );
396+ dev_attr .alloc_size = sizeof (struct vdpasim_blk );
380397 dev_attr .config_size = sizeof (struct virtio_blk_config );
381398 dev_attr .get_config = vdpasim_blk_get_config ;
382399 dev_attr .work_fn = vdpasim_blk_work ;
383- dev_attr .buffer_size = VDPASIM_BLK_CAPACITY << SECTOR_SHIFT ;
400+ dev_attr .free = vdpasim_blk_free ;
384401
385402 simdev = vdpasim_create (& dev_attr , config );
386403 if (IS_ERR (simdev ))
387404 return PTR_ERR (simdev );
388405
406+ blk = sim_to_blk (simdev );
407+
408+ blk -> buffer = kvmalloc (VDPASIM_BLK_CAPACITY << SECTOR_SHIFT ,
409+ GFP_KERNEL );
410+ if (!blk -> buffer ) {
411+ ret = - ENOMEM ;
412+ goto put_dev ;
413+ }
414+
389415 ret = _vdpa_register_device (& simdev -> vdpa , VDPASIM_BLK_VQ_NUM );
390416 if (ret )
391417 goto put_dev ;
0 commit comments