@@ -696,6 +696,23 @@ static void storvsc_change_target_cpu(struct vmbus_channel *channel, u32 old,
696696 spin_unlock_irqrestore (& stor_device -> lock , flags );
697697}
698698
699+ static u64 storvsc_next_request_id (struct vmbus_channel * channel , u64 rqst_addr )
700+ {
701+ struct storvsc_cmd_request * request =
702+ (struct storvsc_cmd_request * )(unsigned long )rqst_addr ;
703+
704+ if (rqst_addr == VMBUS_RQST_INIT )
705+ return VMBUS_RQST_INIT ;
706+ if (rqst_addr == VMBUS_RQST_RESET )
707+ return VMBUS_RQST_RESET ;
708+
709+ /*
710+ * Cannot return an ID of 0, which is reserved for an unsolicited
711+ * message from Hyper-V.
712+ */
713+ return (u64 )blk_mq_unique_tag (request -> cmd -> request ) + 1 ;
714+ }
715+
699716static void handle_sc_creation (struct vmbus_channel * new_sc )
700717{
701718 struct hv_device * device = new_sc -> primary_channel -> device_obj ;
@@ -711,11 +728,7 @@ static void handle_sc_creation(struct vmbus_channel *new_sc)
711728 memset (& props , 0 , sizeof (struct vmstorage_channel_properties ));
712729 new_sc -> max_pkt_size = STORVSC_MAX_PKT_SIZE ;
713730
714- /*
715- * The size of vmbus_requestor is an upper bound on the number of requests
716- * that can be in-progress at any one time across all channels.
717- */
718- new_sc -> rqstor_size = scsi_driver .can_queue ;
731+ new_sc -> next_request_id_callback = storvsc_next_request_id ;
719732
720733 ret = vmbus_open (new_sc ,
721734 storvsc_ringbuffer_size ,
@@ -782,7 +795,7 @@ static void handle_multichannel_storage(struct hv_device *device, int max_chns)
782795 ret = vmbus_sendpacket (device -> channel , vstor_packet ,
783796 (sizeof (struct vstor_packet ) -
784797 stor_device -> vmscsi_size_delta ),
785- ( unsigned long ) request ,
798+ VMBUS_RQST_INIT ,
786799 VM_PKT_DATA_INBAND ,
787800 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED );
788801
@@ -851,7 +864,7 @@ static int storvsc_execute_vstor_op(struct hv_device *device,
851864 ret = vmbus_sendpacket (device -> channel , vstor_packet ,
852865 (sizeof (struct vstor_packet ) -
853866 stor_device -> vmscsi_size_delta ),
854- ( unsigned long ) request ,
867+ VMBUS_RQST_INIT ,
855868 VM_PKT_DATA_INBAND ,
856869 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED );
857870 if (ret != 0 )
@@ -1253,6 +1266,7 @@ static void storvsc_on_channel_callback(void *context)
12531266 const struct vmpacket_descriptor * desc ;
12541267 struct hv_device * device ;
12551268 struct storvsc_device * stor_device ;
1269+ struct Scsi_Host * shost ;
12561270
12571271 if (channel -> primary_channel != NULL )
12581272 device = channel -> primary_channel -> device_obj ;
@@ -1263,35 +1277,57 @@ static void storvsc_on_channel_callback(void *context)
12631277 if (!stor_device )
12641278 return ;
12651279
1266- foreach_vmbus_pkt (desc , channel ) {
1267- void * packet = hv_pkt_data (desc );
1268- struct storvsc_cmd_request * request ;
1269- u64 cmd_rqst ;
1270-
1271- cmd_rqst = vmbus_request_addr (& channel -> requestor ,
1272- desc -> trans_id );
1273- if (cmd_rqst == VMBUS_RQST_ERROR ) {
1274- dev_err (& device -> device ,
1275- "Incorrect transaction id\n" );
1276- continue ;
1277- }
1280+ shost = stor_device -> host ;
12781281
1279- request = (struct storvsc_cmd_request * )(unsigned long )cmd_rqst ;
1282+ foreach_vmbus_pkt (desc , channel ) {
1283+ struct vstor_packet * packet = hv_pkt_data (desc );
1284+ struct storvsc_cmd_request * request = NULL ;
1285+ u64 rqst_id = desc -> trans_id ;
12801286
12811287 if (hv_pkt_datalen (desc ) < sizeof (struct vstor_packet ) -
12821288 stor_device -> vmscsi_size_delta ) {
12831289 dev_err (& device -> device , "Invalid packet len\n" );
12841290 continue ;
12851291 }
12861292
1287- if (request == & stor_device -> init_request ||
1288- request == & stor_device -> reset_request ) {
1289- memcpy (& request -> vstor_packet , packet ,
1290- (sizeof (struct vstor_packet ) - stor_device -> vmscsi_size_delta ));
1291- complete (& request -> wait_event );
1293+ if (rqst_id == VMBUS_RQST_INIT ) {
1294+ request = & stor_device -> init_request ;
1295+ } else if (rqst_id == VMBUS_RQST_RESET ) {
1296+ request = & stor_device -> reset_request ;
12921297 } else {
1298+ /* Hyper-V can send an unsolicited message with ID of 0 */
1299+ if (rqst_id == 0 ) {
1300+ /*
1301+ * storvsc_on_receive() looks at the vstor_packet in the message
1302+ * from the ring buffer. If the operation in the vstor_packet is
1303+ * COMPLETE_IO, then we call storvsc_on_io_completion(), and
1304+ * dereference the guest memory address. Make sure we don't call
1305+ * storvsc_on_io_completion() with a guest memory address that is
1306+ * zero if Hyper-V were to construct and send such a bogus packet.
1307+ */
1308+ if (packet -> operation == VSTOR_OPERATION_COMPLETE_IO ) {
1309+ dev_err (& device -> device , "Invalid packet with ID of 0\n" );
1310+ continue ;
1311+ }
1312+ } else {
1313+ struct scsi_cmnd * scmnd ;
1314+
1315+ /* Transaction 'rqst_id' corresponds to tag 'rqst_id - 1' */
1316+ scmnd = scsi_host_find_tag (shost , rqst_id - 1 );
1317+ if (scmnd == NULL ) {
1318+ dev_err (& device -> device , "Incorrect transaction ID\n" );
1319+ continue ;
1320+ }
1321+ request = (struct storvsc_cmd_request * )scsi_cmd_priv (scmnd );
1322+ }
1323+
12931324 storvsc_on_receive (stor_device , packet , request );
1325+ continue ;
12941326 }
1327+
1328+ memcpy (& request -> vstor_packet , packet ,
1329+ (sizeof (struct vstor_packet ) - stor_device -> vmscsi_size_delta ));
1330+ complete (& request -> wait_event );
12951331 }
12961332}
12971333
@@ -1304,11 +1340,7 @@ static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size,
13041340 memset (& props , 0 , sizeof (struct vmstorage_channel_properties ));
13051341
13061342 device -> channel -> max_pkt_size = STORVSC_MAX_PKT_SIZE ;
1307- /*
1308- * The size of vmbus_requestor is an upper bound on the number of requests
1309- * that can be in-progress at any one time across all channels.
1310- */
1311- device -> channel -> rqstor_size = scsi_driver .can_queue ;
1343+ device -> channel -> next_request_id_callback = storvsc_next_request_id ;
13121344
13131345 ret = vmbus_open (device -> channel ,
13141346 ring_size ,
@@ -1634,7 +1666,7 @@ static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
16341666 ret = vmbus_sendpacket (device -> channel , vstor_packet ,
16351667 (sizeof (struct vstor_packet ) -
16361668 stor_device -> vmscsi_size_delta ),
1637- ( unsigned long ) & stor_device -> reset_request ,
1669+ VMBUS_RQST_RESET ,
16381670 VM_PKT_DATA_INBAND ,
16391671 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED );
16401672 if (ret != 0 )
0 commit comments