@@ -696,6 +696,23 @@ static void storvsc_change_target_cpu(struct vmbus_channel *channel, u32 old,
696
696
spin_unlock_irqrestore (& stor_device -> lock , flags );
697
697
}
698
698
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
+
699
716
static void handle_sc_creation (struct vmbus_channel * new_sc )
700
717
{
701
718
struct hv_device * device = new_sc -> primary_channel -> device_obj ;
@@ -711,11 +728,7 @@ static void handle_sc_creation(struct vmbus_channel *new_sc)
711
728
memset (& props , 0 , sizeof (struct vmstorage_channel_properties ));
712
729
new_sc -> max_pkt_size = STORVSC_MAX_PKT_SIZE ;
713
730
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 ;
719
732
720
733
ret = vmbus_open (new_sc ,
721
734
storvsc_ringbuffer_size ,
@@ -782,7 +795,7 @@ static void handle_multichannel_storage(struct hv_device *device, int max_chns)
782
795
ret = vmbus_sendpacket (device -> channel , vstor_packet ,
783
796
(sizeof (struct vstor_packet ) -
784
797
stor_device -> vmscsi_size_delta ),
785
- ( unsigned long ) request ,
798
+ VMBUS_RQST_INIT ,
786
799
VM_PKT_DATA_INBAND ,
787
800
VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED );
788
801
@@ -851,7 +864,7 @@ static int storvsc_execute_vstor_op(struct hv_device *device,
851
864
ret = vmbus_sendpacket (device -> channel , vstor_packet ,
852
865
(sizeof (struct vstor_packet ) -
853
866
stor_device -> vmscsi_size_delta ),
854
- ( unsigned long ) request ,
867
+ VMBUS_RQST_INIT ,
855
868
VM_PKT_DATA_INBAND ,
856
869
VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED );
857
870
if (ret != 0 )
@@ -1253,6 +1266,7 @@ static void storvsc_on_channel_callback(void *context)
1253
1266
const struct vmpacket_descriptor * desc ;
1254
1267
struct hv_device * device ;
1255
1268
struct storvsc_device * stor_device ;
1269
+ struct Scsi_Host * shost ;
1256
1270
1257
1271
if (channel -> primary_channel != NULL )
1258
1272
device = channel -> primary_channel -> device_obj ;
@@ -1263,35 +1277,57 @@ static void storvsc_on_channel_callback(void *context)
1263
1277
if (!stor_device )
1264
1278
return ;
1265
1279
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 ;
1278
1281
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 ;
1280
1286
1281
1287
if (hv_pkt_datalen (desc ) < sizeof (struct vstor_packet ) -
1282
1288
stor_device -> vmscsi_size_delta ) {
1283
1289
dev_err (& device -> device , "Invalid packet len\n" );
1284
1290
continue ;
1285
1291
}
1286
1292
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 ;
1292
1297
} 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
+
1293
1324
storvsc_on_receive (stor_device , packet , request );
1325
+ continue ;
1294
1326
}
1327
+
1328
+ memcpy (& request -> vstor_packet , packet ,
1329
+ (sizeof (struct vstor_packet ) - stor_device -> vmscsi_size_delta ));
1330
+ complete (& request -> wait_event );
1295
1331
}
1296
1332
}
1297
1333
@@ -1304,11 +1340,7 @@ static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size,
1304
1340
memset (& props , 0 , sizeof (struct vmstorage_channel_properties ));
1305
1341
1306
1342
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 ;
1312
1344
1313
1345
ret = vmbus_open (device -> channel ,
1314
1346
ring_size ,
@@ -1634,7 +1666,7 @@ static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
1634
1666
ret = vmbus_sendpacket (device -> channel , vstor_packet ,
1635
1667
(sizeof (struct vstor_packet ) -
1636
1668
stor_device -> vmscsi_size_delta ),
1637
- ( unsigned long ) & stor_device -> reset_request ,
1669
+ VMBUS_RQST_RESET ,
1638
1670
VM_PKT_DATA_INBAND ,
1639
1671
VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED );
1640
1672
if (ret != 0 )
0 commit comments