@@ -106,21 +106,11 @@ struct vhost_scsi_inflight {
106
106
struct vhost_scsi_cmd {
107
107
/* Descriptor from vhost_get_vq_desc() for virt_queue segment */
108
108
int tvc_vq_desc ;
109
- /* virtio-scsi initiator task attribute */
110
- int tvc_task_attr ;
111
109
/* virtio-scsi response incoming iovecs */
112
110
int tvc_in_iovs ;
113
- /* virtio-scsi initiator data direction */
114
- enum dma_data_direction tvc_data_direction ;
115
- /* Expected data transfer length from virtio-scsi header */
116
- u32 tvc_exp_data_len ;
117
- /* The Tag from include/linux/virtio_scsi.h:struct virtio_scsi_cmd_req */
118
- u64 tvc_tag ;
119
111
/* The number of scatterlists associated with this cmd */
120
112
u32 tvc_sgl_count ;
121
113
u32 tvc_prot_sgl_count ;
122
- /* Saved unpacked SCSI LUN for vhost_scsi_target_queue_cmd() */
123
- u32 tvc_lun ;
124
114
u32 copied_iov :1 ;
125
115
const void * saved_iter_addr ;
126
116
struct iov_iter saved_iter ;
@@ -130,16 +120,10 @@ struct vhost_scsi_cmd {
130
120
struct sg_table prot_table ;
131
121
/* Pointer to response header iovec */
132
122
struct iovec * tvc_resp_iov ;
133
- /* Pointer to vhost_scsi for our device */
134
- struct vhost_scsi * tvc_vhost ;
135
123
/* Pointer to vhost_virtqueue for the cmd */
136
124
struct vhost_virtqueue * tvc_vq ;
137
- /* Pointer to vhost nexus memory */
138
- struct vhost_scsi_nexus * tvc_nexus ;
139
125
/* The TCM I/O descriptor that is accessed via container_of() */
140
126
struct se_cmd tvc_se_cmd ;
141
- /* Copy of the incoming SCSI command descriptor block (CDB) */
142
- unsigned char tvc_cdb [VHOST_SCSI_MAX_CDB_SIZE ];
143
127
/* Sense buffer that will be mapped into outgoing status */
144
128
unsigned char tvc_sense_buf [TRANSPORT_SENSE_BUFFER ];
145
129
/* Completed commands list, serviced from vhost worker thread */
@@ -385,9 +369,9 @@ static void vhost_scsi_release_cmd_res(struct se_cmd *se_cmd)
385
369
{
386
370
struct vhost_scsi_cmd * tv_cmd = container_of (se_cmd ,
387
371
struct vhost_scsi_cmd , tvc_se_cmd );
388
- struct vhost_scsi * vs = tv_cmd -> tvc_vhost ;
389
372
struct vhost_scsi_virtqueue * svq = container_of (tv_cmd -> tvc_vq ,
390
373
struct vhost_scsi_virtqueue , vq );
374
+ struct vhost_scsi * vs = svq -> vs ;
391
375
struct vhost_scsi_inflight * inflight = tv_cmd -> inflight ;
392
376
struct scatterlist * sg ;
393
377
struct page * page ;
@@ -441,7 +425,10 @@ static void vhost_scsi_release_cmd(struct se_cmd *se_cmd)
441
425
} else {
442
426
struct vhost_scsi_cmd * cmd = container_of (se_cmd ,
443
427
struct vhost_scsi_cmd , tvc_se_cmd );
444
- struct vhost_scsi * vs = cmd -> tvc_vhost ;
428
+ struct vhost_scsi_virtqueue * svq = container_of (cmd -> tvc_vq ,
429
+ struct vhost_scsi_virtqueue ,
430
+ vq );
431
+ struct vhost_scsi * vs = svq -> vs ;
445
432
446
433
llist_add (& cmd -> tvc_completion_list , & vs -> vs_completion_list );
447
434
vhost_work_queue (& vs -> dev , & vs -> vs_completion_work );
@@ -685,24 +672,15 @@ static void vhost_scsi_complete_cmd_work(struct vhost_work *work)
685
672
}
686
673
687
674
static struct vhost_scsi_cmd *
688
- vhost_scsi_get_cmd (struct vhost_virtqueue * vq , struct vhost_scsi_tpg * tpg ,
689
- unsigned char * cdb , u64 scsi_tag , u16 lun , u8 task_attr ,
690
- u32 exp_data_len , int data_direction )
675
+ vhost_scsi_get_cmd (struct vhost_virtqueue * vq , u64 scsi_tag )
691
676
{
692
677
struct vhost_scsi_virtqueue * svq = container_of (vq ,
693
678
struct vhost_scsi_virtqueue , vq );
694
679
struct vhost_scsi_cmd * cmd ;
695
- struct vhost_scsi_nexus * tv_nexus ;
696
680
struct scatterlist * sgl , * prot_sgl ;
697
681
struct iovec * tvc_resp_iov ;
698
682
int tag ;
699
683
700
- tv_nexus = tpg -> tpg_nexus ;
701
- if (!tv_nexus ) {
702
- pr_err ("Unable to locate active struct vhost_scsi_nexus\n" );
703
- return ERR_PTR (- EIO );
704
- }
705
-
706
684
tag = sbitmap_get (& svq -> scsi_tags );
707
685
if (tag < 0 ) {
708
686
pr_warn_once ("Guest sent too many cmds. Returning TASK_SET_FULL.\n" );
@@ -717,17 +695,9 @@ vhost_scsi_get_cmd(struct vhost_virtqueue *vq, struct vhost_scsi_tpg *tpg,
717
695
cmd -> sgl = sgl ;
718
696
cmd -> prot_sgl = prot_sgl ;
719
697
cmd -> tvc_se_cmd .map_tag = tag ;
720
- cmd -> tvc_tag = scsi_tag ;
721
- cmd -> tvc_lun = lun ;
722
- cmd -> tvc_task_attr = task_attr ;
723
- cmd -> tvc_exp_data_len = exp_data_len ;
724
- cmd -> tvc_data_direction = data_direction ;
725
- cmd -> tvc_nexus = tv_nexus ;
726
698
cmd -> inflight = vhost_scsi_get_inflight (vq );
727
699
cmd -> tvc_resp_iov = tvc_resp_iov ;
728
700
729
- memcpy (cmd -> tvc_cdb , cdb , VHOST_SCSI_MAX_CDB_SIZE );
730
-
731
701
return cmd ;
732
702
}
733
703
@@ -847,15 +817,16 @@ vhost_scsi_calc_sgls(struct iov_iter *iter, size_t bytes, int max_sgls)
847
817
848
818
static int
849
819
vhost_scsi_copy_iov_to_sgl (struct vhost_scsi_cmd * cmd , struct iov_iter * iter ,
850
- struct sg_table * sg_table , int sg_count )
820
+ struct sg_table * sg_table , int sg_count ,
821
+ int data_dir )
851
822
{
852
823
size_t len = iov_iter_count (iter );
853
824
unsigned int nbytes = 0 ;
854
825
struct scatterlist * sg ;
855
826
struct page * page ;
856
827
int i , ret ;
857
828
858
- if (cmd -> tvc_data_direction == DMA_FROM_DEVICE ) {
829
+ if (data_dir == DMA_FROM_DEVICE ) {
859
830
cmd -> saved_iter_addr = dup_iter (& cmd -> saved_iter , iter ,
860
831
GFP_KERNEL );
861
832
if (!cmd -> saved_iter_addr )
@@ -872,7 +843,7 @@ vhost_scsi_copy_iov_to_sgl(struct vhost_scsi_cmd *cmd, struct iov_iter *iter,
872
843
nbytes = min_t (unsigned int , PAGE_SIZE , len );
873
844
sg_set_page (sg , page , nbytes , 0 );
874
845
875
- if (cmd -> tvc_data_direction == DMA_TO_DEVICE &&
846
+ if (data_dir == DMA_TO_DEVICE &&
876
847
copy_page_from_iter (page , 0 , nbytes , iter ) != nbytes ) {
877
848
ret = - EFAULT ;
878
849
goto err ;
@@ -917,11 +888,10 @@ vhost_scsi_map_iov_to_sgl(struct vhost_scsi_cmd *cmd, struct iov_iter *iter,
917
888
}
918
889
919
890
static int
920
- vhost_scsi_mapal (struct vhost_scsi_cmd * cmd ,
891
+ vhost_scsi_mapal (struct vhost_scsi * vs , struct vhost_scsi_cmd * cmd ,
921
892
size_t prot_bytes , struct iov_iter * prot_iter ,
922
- size_t data_bytes , struct iov_iter * data_iter )
893
+ size_t data_bytes , struct iov_iter * data_iter , int data_dir )
923
894
{
924
- struct vhost_scsi * vs = cmd -> tvc_vhost ;
925
895
int sgl_count , ret ;
926
896
927
897
if (prot_bytes ) {
@@ -967,7 +937,7 @@ vhost_scsi_mapal(struct vhost_scsi_cmd *cmd,
967
937
cmd -> tvc_sgl_count , false);
968
938
if (ret == - EINVAL )
969
939
ret = vhost_scsi_copy_iov_to_sgl (cmd , data_iter , & cmd -> table ,
970
- cmd -> tvc_sgl_count );
940
+ cmd -> tvc_sgl_count , data_dir );
971
941
if (ret < 0 ) {
972
942
sg_free_table_chained (& cmd -> table , vs -> inline_sg_cnt );
973
943
cmd -> tvc_sgl_count = 0 ;
@@ -993,10 +963,13 @@ static int vhost_scsi_to_tcm_attr(int attr)
993
963
return TCM_SIMPLE_TAG ;
994
964
}
995
965
996
- static void vhost_scsi_target_queue_cmd (struct vhost_scsi_cmd * cmd )
966
+ static void vhost_scsi_target_queue_cmd (struct vhost_scsi_nexus * nexus ,
967
+ struct vhost_scsi_cmd * cmd ,
968
+ unsigned char * cdb , u16 lun ,
969
+ int task_attr , int data_dir ,
970
+ u32 exp_data_len )
997
971
{
998
972
struct se_cmd * se_cmd = & cmd -> tvc_se_cmd ;
999
- struct vhost_scsi_nexus * tv_nexus ;
1000
973
struct scatterlist * sg_ptr , * sg_prot_ptr = NULL ;
1001
974
1002
975
/* FIXME: BIDI operation */
@@ -1010,15 +983,13 @@ static void vhost_scsi_target_queue_cmd(struct vhost_scsi_cmd *cmd)
1010
983
} else {
1011
984
sg_ptr = NULL ;
1012
985
}
1013
- tv_nexus = cmd -> tvc_nexus ;
1014
986
1015
987
se_cmd -> tag = 0 ;
1016
- target_init_cmd (se_cmd , tv_nexus -> tvn_se_sess , & cmd -> tvc_sense_buf [0 ],
1017
- cmd -> tvc_lun , cmd -> tvc_exp_data_len ,
1018
- vhost_scsi_to_tcm_attr (cmd -> tvc_task_attr ),
1019
- cmd -> tvc_data_direction , TARGET_SCF_ACK_KREF );
988
+ target_init_cmd (se_cmd , nexus -> tvn_se_sess , & cmd -> tvc_sense_buf [0 ],
989
+ lun , exp_data_len , vhost_scsi_to_tcm_attr (task_attr ),
990
+ data_dir , TARGET_SCF_ACK_KREF );
1020
991
1021
- if (target_submit_prep (se_cmd , cmd -> tvc_cdb , sg_ptr ,
992
+ if (target_submit_prep (se_cmd , cdb , sg_ptr ,
1022
993
cmd -> tvc_sgl_count , NULL , 0 , sg_prot_ptr ,
1023
994
cmd -> tvc_prot_sgl_count , GFP_KERNEL ))
1024
995
return ;
@@ -1175,6 +1146,7 @@ vhost_scsi_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq)
1175
1146
struct vhost_scsi_tpg * * vs_tpg , * tpg ;
1176
1147
struct virtio_scsi_cmd_req v_req ;
1177
1148
struct virtio_scsi_cmd_req_pi v_req_pi ;
1149
+ struct vhost_scsi_nexus * nexus ;
1178
1150
struct vhost_scsi_ctx vc ;
1179
1151
struct vhost_scsi_cmd * cmd ;
1180
1152
struct iov_iter in_iter , prot_iter , data_iter ;
@@ -1184,7 +1156,7 @@ vhost_scsi_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq)
1184
1156
u16 lun ;
1185
1157
u8 task_attr ;
1186
1158
bool t10_pi = vhost_has_feature (vq , VIRTIO_SCSI_F_T10_PI );
1187
- void * cdb ;
1159
+ u8 * cdb ;
1188
1160
1189
1161
mutex_lock (& vq -> mutex );
1190
1162
/*
@@ -1327,28 +1299,34 @@ vhost_scsi_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq)
1327
1299
scsi_command_size (cdb ), VHOST_SCSI_MAX_CDB_SIZE );
1328
1300
goto err ;
1329
1301
}
1330
- cmd = vhost_scsi_get_cmd (vq , tpg , cdb , tag , lun , task_attr ,
1331
- exp_data_len + prot_bytes ,
1332
- data_direction );
1302
+
1303
+ nexus = tpg -> tpg_nexus ;
1304
+ if (!nexus ) {
1305
+ vq_err (vq , "Unable to locate active struct vhost_scsi_nexus\n" );
1306
+ ret = - EIO ;
1307
+ goto err ;
1308
+ }
1309
+
1310
+ cmd = vhost_scsi_get_cmd (vq , tag );
1333
1311
if (IS_ERR (cmd )) {
1334
1312
ret = PTR_ERR (cmd );
1335
1313
vq_err (vq , "vhost_scsi_get_tag failed %dd\n" , ret );
1336
1314
goto err ;
1337
1315
}
1338
- cmd -> tvc_vhost = vs ;
1339
1316
cmd -> tvc_vq = vq ;
1340
1317
for (i = 0 ; i < vc .in ; i ++ )
1341
1318
cmd -> tvc_resp_iov [i ] = vq -> iov [vc .out + i ];
1342
1319
cmd -> tvc_in_iovs = vc .in ;
1343
1320
1344
1321
pr_debug ("vhost_scsi got command opcode: %#02x, lun: %d\n" ,
1345
- cmd -> tvc_cdb [0 ], cmd -> tvc_lun );
1322
+ cdb [0 ], lun );
1346
1323
pr_debug ("cmd: %p exp_data_len: %d, prot_bytes: %d data_direction:"
1347
1324
" %d\n" , cmd , exp_data_len , prot_bytes , data_direction );
1348
1325
1349
1326
if (data_direction != DMA_NONE ) {
1350
- ret = vhost_scsi_mapal (cmd , prot_bytes , & prot_iter ,
1351
- exp_data_len , & data_iter );
1327
+ ret = vhost_scsi_mapal (vs , cmd , prot_bytes , & prot_iter ,
1328
+ exp_data_len , & data_iter ,
1329
+ data_direction );
1352
1330
if (unlikely (ret )) {
1353
1331
vq_err (vq , "Failed to map iov to sgl\n" );
1354
1332
vhost_scsi_release_cmd_res (& cmd -> tvc_se_cmd );
@@ -1361,7 +1339,9 @@ vhost_scsi_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq)
1361
1339
* vhost_scsi_queue_data_in() and vhost_scsi_queue_status()
1362
1340
*/
1363
1341
cmd -> tvc_vq_desc = vc .head ;
1364
- vhost_scsi_target_queue_cmd (cmd );
1342
+ vhost_scsi_target_queue_cmd (nexus , cmd , cdb , lun , task_attr ,
1343
+ data_direction ,
1344
+ exp_data_len + prot_bytes );
1365
1345
ret = 0 ;
1366
1346
err :
1367
1347
/*
0 commit comments