@@ -106,21 +106,11 @@ struct vhost_scsi_inflight {
106106struct vhost_scsi_cmd {
107107 /* Descriptor from vhost_get_vq_desc() for virt_queue segment */
108108 int tvc_vq_desc ;
109- /* virtio-scsi initiator task attribute */
110- int tvc_task_attr ;
111109 /* virtio-scsi response incoming iovecs */
112110 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 ;
119111 /* The number of scatterlists associated with this cmd */
120112 u32 tvc_sgl_count ;
121113 u32 tvc_prot_sgl_count ;
122- /* Saved unpacked SCSI LUN for vhost_scsi_target_queue_cmd() */
123- u32 tvc_lun ;
124114 u32 copied_iov :1 ;
125115 const void * saved_iter_addr ;
126116 struct iov_iter saved_iter ;
@@ -130,16 +120,10 @@ struct vhost_scsi_cmd {
130120 struct sg_table prot_table ;
131121 /* Pointer to response header iovec */
132122 struct iovec * tvc_resp_iov ;
133- /* Pointer to vhost_scsi for our device */
134- struct vhost_scsi * tvc_vhost ;
135123 /* Pointer to vhost_virtqueue for the cmd */
136124 struct vhost_virtqueue * tvc_vq ;
137- /* Pointer to vhost nexus memory */
138- struct vhost_scsi_nexus * tvc_nexus ;
139125 /* The TCM I/O descriptor that is accessed via container_of() */
140126 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 ];
143127 /* Sense buffer that will be mapped into outgoing status */
144128 unsigned char tvc_sense_buf [TRANSPORT_SENSE_BUFFER ];
145129 /* Completed commands list, serviced from vhost worker thread */
@@ -375,9 +359,9 @@ static void vhost_scsi_release_cmd_res(struct se_cmd *se_cmd)
375359{
376360 struct vhost_scsi_cmd * tv_cmd = container_of (se_cmd ,
377361 struct vhost_scsi_cmd , tvc_se_cmd );
378- struct vhost_scsi * vs = tv_cmd -> tvc_vhost ;
379362 struct vhost_scsi_virtqueue * svq = container_of (tv_cmd -> tvc_vq ,
380363 struct vhost_scsi_virtqueue , vq );
364+ struct vhost_scsi * vs = svq -> vs ;
381365 struct vhost_scsi_inflight * inflight = tv_cmd -> inflight ;
382366 struct scatterlist * sg ;
383367 struct page * page ;
@@ -671,24 +655,15 @@ static void vhost_scsi_complete_cmd_work(struct vhost_work *work)
671655}
672656
673657static struct vhost_scsi_cmd *
674- vhost_scsi_get_cmd (struct vhost_virtqueue * vq , struct vhost_scsi_tpg * tpg ,
675- unsigned char * cdb , u64 scsi_tag , u16 lun , u8 task_attr ,
676- u32 exp_data_len , int data_direction )
658+ vhost_scsi_get_cmd (struct vhost_virtqueue * vq , u64 scsi_tag )
677659{
678660 struct vhost_scsi_virtqueue * svq = container_of (vq ,
679661 struct vhost_scsi_virtqueue , vq );
680662 struct vhost_scsi_cmd * cmd ;
681- struct vhost_scsi_nexus * tv_nexus ;
682663 struct scatterlist * sgl , * prot_sgl ;
683664 struct iovec * tvc_resp_iov ;
684665 int tag ;
685666
686- tv_nexus = tpg -> tpg_nexus ;
687- if (!tv_nexus ) {
688- pr_err ("Unable to locate active struct vhost_scsi_nexus\n" );
689- return ERR_PTR (- EIO );
690- }
691-
692667 tag = sbitmap_get (& svq -> scsi_tags );
693668 if (tag < 0 ) {
694669 pr_warn_once ("Guest sent too many cmds. Returning TASK_SET_FULL.\n" );
@@ -703,17 +678,9 @@ vhost_scsi_get_cmd(struct vhost_virtqueue *vq, struct vhost_scsi_tpg *tpg,
703678 cmd -> sgl = sgl ;
704679 cmd -> prot_sgl = prot_sgl ;
705680 cmd -> tvc_se_cmd .map_tag = tag ;
706- cmd -> tvc_tag = scsi_tag ;
707- cmd -> tvc_lun = lun ;
708- cmd -> tvc_task_attr = task_attr ;
709- cmd -> tvc_exp_data_len = exp_data_len ;
710- cmd -> tvc_data_direction = data_direction ;
711- cmd -> tvc_nexus = tv_nexus ;
712681 cmd -> inflight = vhost_scsi_get_inflight (vq );
713682 cmd -> tvc_resp_iov = tvc_resp_iov ;
714683
715- memcpy (cmd -> tvc_cdb , cdb , VHOST_SCSI_MAX_CDB_SIZE );
716-
717684 return cmd ;
718685}
719686
@@ -831,15 +798,16 @@ vhost_scsi_calc_sgls(struct iov_iter *iter, size_t bytes, int max_sgls)
831798
832799static int
833800vhost_scsi_copy_iov_to_sgl (struct vhost_scsi_cmd * cmd , struct iov_iter * iter ,
834- struct sg_table * sg_table , int sg_count )
801+ struct sg_table * sg_table , int sg_count ,
802+ int data_dir )
835803{
836804 size_t len = iov_iter_count (iter );
837805 unsigned int nbytes = 0 ;
838806 struct scatterlist * sg ;
839807 struct page * page ;
840808 int i , ret ;
841809
842- if (cmd -> tvc_data_direction == DMA_FROM_DEVICE ) {
810+ if (data_dir == DMA_FROM_DEVICE ) {
843811 cmd -> saved_iter_addr = dup_iter (& cmd -> saved_iter , iter ,
844812 GFP_KERNEL );
845813 if (!cmd -> saved_iter_addr )
@@ -856,7 +824,7 @@ vhost_scsi_copy_iov_to_sgl(struct vhost_scsi_cmd *cmd, struct iov_iter *iter,
856824 nbytes = min_t (unsigned int , PAGE_SIZE , len );
857825 sg_set_page (sg , page , nbytes , 0 );
858826
859- if (cmd -> tvc_data_direction == DMA_TO_DEVICE &&
827+ if (data_dir == DMA_TO_DEVICE &&
860828 copy_page_from_iter (page , 0 , nbytes , iter ) != nbytes ) {
861829 ret = - EFAULT ;
862830 goto err ;
@@ -901,11 +869,10 @@ vhost_scsi_map_iov_to_sgl(struct vhost_scsi_cmd *cmd, struct iov_iter *iter,
901869}
902870
903871static int
904- vhost_scsi_mapal (struct vhost_scsi_cmd * cmd ,
872+ vhost_scsi_mapal (struct vhost_scsi * vs , struct vhost_scsi_cmd * cmd ,
905873 size_t prot_bytes , struct iov_iter * prot_iter ,
906- size_t data_bytes , struct iov_iter * data_iter )
874+ size_t data_bytes , struct iov_iter * data_iter , int data_dir )
907875{
908- struct vhost_scsi * vs = cmd -> tvc_vhost ;
909876 int sgl_count , ret ;
910877
911878 if (prot_bytes ) {
@@ -951,7 +918,7 @@ vhost_scsi_mapal(struct vhost_scsi_cmd *cmd,
951918 cmd -> tvc_sgl_count , false);
952919 if (ret == - EINVAL )
953920 ret = vhost_scsi_copy_iov_to_sgl (cmd , data_iter , & cmd -> table ,
954- cmd -> tvc_sgl_count );
921+ cmd -> tvc_sgl_count , data_dir );
955922 if (ret < 0 ) {
956923 sg_free_table_chained (& cmd -> table , vs -> inline_sg_cnt );
957924 cmd -> tvc_sgl_count = 0 ;
@@ -977,10 +944,13 @@ static int vhost_scsi_to_tcm_attr(int attr)
977944 return TCM_SIMPLE_TAG ;
978945}
979946
980- static void vhost_scsi_target_queue_cmd (struct vhost_scsi_cmd * cmd )
947+ static void vhost_scsi_target_queue_cmd (struct vhost_scsi_nexus * nexus ,
948+ struct vhost_scsi_cmd * cmd ,
949+ unsigned char * cdb , u16 lun ,
950+ int task_attr , int data_dir ,
951+ u32 exp_data_len )
981952{
982953 struct se_cmd * se_cmd = & cmd -> tvc_se_cmd ;
983- struct vhost_scsi_nexus * tv_nexus ;
984954 struct scatterlist * sg_ptr , * sg_prot_ptr = NULL ;
985955
986956 /* FIXME: BIDI operation */
@@ -994,15 +964,13 @@ static void vhost_scsi_target_queue_cmd(struct vhost_scsi_cmd *cmd)
994964 } else {
995965 sg_ptr = NULL ;
996966 }
997- tv_nexus = cmd -> tvc_nexus ;
998967
999968 se_cmd -> tag = 0 ;
1000- target_init_cmd (se_cmd , tv_nexus -> tvn_se_sess , & cmd -> tvc_sense_buf [0 ],
1001- cmd -> tvc_lun , cmd -> tvc_exp_data_len ,
1002- vhost_scsi_to_tcm_attr (cmd -> tvc_task_attr ),
1003- cmd -> tvc_data_direction , TARGET_SCF_ACK_KREF );
969+ target_init_cmd (se_cmd , nexus -> tvn_se_sess , & cmd -> tvc_sense_buf [0 ],
970+ lun , exp_data_len , vhost_scsi_to_tcm_attr (task_attr ),
971+ data_dir , TARGET_SCF_ACK_KREF );
1004972
1005- if (target_submit_prep (se_cmd , cmd -> tvc_cdb , sg_ptr ,
973+ if (target_submit_prep (se_cmd , cdb , sg_ptr ,
1006974 cmd -> tvc_sgl_count , NULL , 0 , sg_prot_ptr ,
1007975 cmd -> tvc_prot_sgl_count , GFP_KERNEL ))
1008976 return ;
@@ -1159,6 +1127,7 @@ vhost_scsi_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq)
11591127 struct vhost_scsi_tpg * * vs_tpg , * tpg ;
11601128 struct virtio_scsi_cmd_req v_req ;
11611129 struct virtio_scsi_cmd_req_pi v_req_pi ;
1130+ struct vhost_scsi_nexus * nexus ;
11621131 struct vhost_scsi_ctx vc ;
11631132 struct vhost_scsi_cmd * cmd ;
11641133 struct iov_iter in_iter , prot_iter , data_iter ;
@@ -1168,7 +1137,7 @@ vhost_scsi_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq)
11681137 u16 lun ;
11691138 u8 task_attr ;
11701139 bool t10_pi = vhost_has_feature (vq , VIRTIO_SCSI_F_T10_PI );
1171- void * cdb ;
1140+ u8 * cdb ;
11721141
11731142 mutex_lock (& vq -> mutex );
11741143 /*
@@ -1311,28 +1280,34 @@ vhost_scsi_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq)
13111280 scsi_command_size (cdb ), VHOST_SCSI_MAX_CDB_SIZE );
13121281 goto err ;
13131282 }
1314- cmd = vhost_scsi_get_cmd (vq , tpg , cdb , tag , lun , task_attr ,
1315- exp_data_len + prot_bytes ,
1316- data_direction );
1283+
1284+ nexus = tpg -> tpg_nexus ;
1285+ if (!nexus ) {
1286+ vq_err (vq , "Unable to locate active struct vhost_scsi_nexus\n" );
1287+ ret = - EIO ;
1288+ goto err ;
1289+ }
1290+
1291+ cmd = vhost_scsi_get_cmd (vq , tag );
13171292 if (IS_ERR (cmd )) {
13181293 ret = PTR_ERR (cmd );
13191294 vq_err (vq , "vhost_scsi_get_tag failed %dd\n" , ret );
13201295 goto err ;
13211296 }
1322- cmd -> tvc_vhost = vs ;
13231297 cmd -> tvc_vq = vq ;
13241298 for (i = 0 ; i < vc .in ; i ++ )
13251299 cmd -> tvc_resp_iov [i ] = vq -> iov [vc .out + i ];
13261300 cmd -> tvc_in_iovs = vc .in ;
13271301
13281302 pr_debug ("vhost_scsi got command opcode: %#02x, lun: %d\n" ,
1329- cmd -> tvc_cdb [0 ], cmd -> tvc_lun );
1303+ cdb [0 ], lun );
13301304 pr_debug ("cmd: %p exp_data_len: %d, prot_bytes: %d data_direction:"
13311305 " %d\n" , cmd , exp_data_len , prot_bytes , data_direction );
13321306
13331307 if (data_direction != DMA_NONE ) {
1334- ret = vhost_scsi_mapal (cmd , prot_bytes , & prot_iter ,
1335- exp_data_len , & data_iter );
1308+ ret = vhost_scsi_mapal (vs , cmd , prot_bytes , & prot_iter ,
1309+ exp_data_len , & data_iter ,
1310+ data_direction );
13361311 if (unlikely (ret )) {
13371312 vq_err (vq , "Failed to map iov to sgl\n" );
13381313 vhost_scsi_release_cmd_res (& cmd -> tvc_se_cmd );
@@ -1345,7 +1320,9 @@ vhost_scsi_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq)
13451320 * vhost_scsi_queue_data_in() and vhost_scsi_queue_status()
13461321 */
13471322 cmd -> tvc_vq_desc = vc .head ;
1348- vhost_scsi_target_queue_cmd (cmd );
1323+ vhost_scsi_target_queue_cmd (nexus , cmd , cdb , lun , task_attr ,
1324+ data_direction ,
1325+ exp_data_len + prot_bytes );
13491326 ret = 0 ;
13501327err :
13511328 /*
0 commit comments