2 * Copyright (c) 2009, Microsoft Corporation.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307 USA.
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
21 #include <linux/kernel.h>
22 #include <linux/string.h>
23 #include <linux/slab.h>
25 #include <linux/delay.h>
28 #include "storvsc_api.h"
29 #include "vmbus_packet_format.h"
33 struct storvsc_request_extension
{
34 /* LIST_ENTRY ListEntry; */
36 struct hv_storvsc_request
*Request
;
37 struct hv_device
*Device
;
39 /* Synchronize the request/response if needed */
40 struct osd_waitevent
*WaitEvent
;
42 struct vstor_packet VStorPacket
;
45 /* A storvsc device is a device object that contains a vmbus channel */
46 struct storvsc_device
{
47 struct hv_device
*Device
;
49 /* 0 indicates the device is being destroyed */
52 atomic_t NumOutstandingRequests
;
55 * Each unique Port/Path/Target represents 1 channel ie scsi
56 * controller. In reality, the pathid, targetid is always 0
57 * and the port is set by us
59 unsigned int PortNumber
;
61 unsigned char TargetId
;
63 /* LIST_ENTRY OutstandingRequestList; */
64 /* HANDLE OutstandingRequestLock; */
66 /* Used for vsc/vsp channel reset process */
67 struct storvsc_request_extension InitRequest
;
68 struct storvsc_request_extension ResetRequest
;
72 static const char *gDriverName
= "storvsc";
74 /* {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f} */
75 static const struct hv_guid gStorVscDeviceType
= {
77 0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
78 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f
83 static inline struct storvsc_device
*AllocStorDevice(struct hv_device
*Device
)
85 struct storvsc_device
*storDevice
;
87 storDevice
= kzalloc(sizeof(struct storvsc_device
), GFP_KERNEL
);
91 /* Set to 2 to allow both inbound and outbound traffics */
92 /* (ie GetStorDevice() and MustGetStorDevice()) to proceed. */
93 atomic_cmpxchg(&storDevice
->RefCount
, 0, 2);
95 storDevice
->Device
= Device
;
96 Device
->Extension
= storDevice
;
101 static inline void FreeStorDevice(struct storvsc_device
*Device
)
103 /* ASSERT(atomic_read(&Device->RefCount) == 0); */
107 /* Get the stordevice object iff exists and its refcount > 1 */
108 static inline struct storvsc_device
*GetStorDevice(struct hv_device
*Device
)
110 struct storvsc_device
*storDevice
;
112 storDevice
= (struct storvsc_device
*)Device
->Extension
;
113 if (storDevice
&& atomic_read(&storDevice
->RefCount
) > 1)
114 atomic_inc(&storDevice
->RefCount
);
121 /* Get the stordevice object iff exists and its refcount > 0 */
122 static inline struct storvsc_device
*MustGetStorDevice(struct hv_device
*Device
)
124 struct storvsc_device
*storDevice
;
126 storDevice
= (struct storvsc_device
*)Device
->Extension
;
127 if (storDevice
&& atomic_read(&storDevice
->RefCount
))
128 atomic_inc(&storDevice
->RefCount
);
135 static inline void PutStorDevice(struct hv_device
*Device
)
137 struct storvsc_device
*storDevice
;
139 storDevice
= (struct storvsc_device
*)Device
->Extension
;
140 /* ASSERT(storDevice); */
142 atomic_dec(&storDevice
->RefCount
);
143 /* ASSERT(atomic_read(&storDevice->RefCount)); */
146 /* Drop ref count to 1 to effectively disable GetStorDevice() */
147 static inline struct storvsc_device
*ReleaseStorDevice(struct hv_device
*Device
)
149 struct storvsc_device
*storDevice
;
151 storDevice
= (struct storvsc_device
*)Device
->Extension
;
152 /* ASSERT(storDevice); */
154 /* Busy wait until the ref drop to 2, then set it to 1 */
155 while (atomic_cmpxchg(&storDevice
->RefCount
, 2, 1) != 2)
161 /* Drop ref count to 0. No one can use StorDevice object. */
162 static inline struct storvsc_device
*FinalReleaseStorDevice(
163 struct hv_device
*Device
)
165 struct storvsc_device
*storDevice
;
167 storDevice
= (struct storvsc_device
*)Device
->Extension
;
168 /* ASSERT(storDevice); */
170 /* Busy wait until the ref drop to 1, then set it to 0 */
171 while (atomic_cmpxchg(&storDevice
->RefCount
, 1, 0) != 1)
174 Device
->Extension
= NULL
;
178 static int StorVscChannelInit(struct hv_device
*Device
)
180 struct storvsc_device
*storDevice
;
181 struct storvsc_request_extension
*request
;
182 struct vstor_packet
*vstorPacket
;
185 storDevice
= GetStorDevice(Device
);
187 DPRINT_ERR(STORVSC
, "unable to get stor device..."
188 "device being destroyed?");
189 DPRINT_EXIT(STORVSC
);
193 request
= &storDevice
->InitRequest
;
194 vstorPacket
= &request
->VStorPacket
;
197 * Now, initiate the vsc/vsp initialization protocol on the open
200 memset(request
, 0, sizeof(struct storvsc_request_extension
));
201 request
->WaitEvent
= osd_WaitEventCreate();
202 if (!request
->WaitEvent
) {
207 vstorPacket
->Operation
= VStorOperationBeginInitialization
;
208 vstorPacket
->Flags
= REQUEST_COMPLETION_FLAG
;
210 /*SpinlockAcquire(gDriverExt.packetListLock);
211 INSERT_TAIL_LIST(&gDriverExt.packetList, &packet->listEntry.entry);
212 SpinlockRelease(gDriverExt.packetListLock);*/
214 DPRINT_INFO(STORVSC
, "BEGIN_INITIALIZATION_OPERATION...");
216 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
218 sizeof(struct vstor_packet
),
219 (unsigned long)request
,
220 VmbusPacketTypeDataInBand
,
221 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
224 "unable to send BEGIN_INITIALIZATION_OPERATION");
228 osd_WaitEventWait(request
->WaitEvent
);
230 if (vstorPacket
->Operation
!= VStorOperationCompleteIo
||
231 vstorPacket
->Status
!= 0) {
232 DPRINT_ERR(STORVSC
, "BEGIN_INITIALIZATION_OPERATION failed "
233 "(op %d status 0x%x)",
234 vstorPacket
->Operation
, vstorPacket
->Status
);
238 DPRINT_INFO(STORVSC
, "QUERY_PROTOCOL_VERSION_OPERATION...");
240 /* reuse the packet for version range supported */
241 memset(vstorPacket
, 0, sizeof(struct vstor_packet
));
242 vstorPacket
->Operation
= VStorOperationQueryProtocolVersion
;
243 vstorPacket
->Flags
= REQUEST_COMPLETION_FLAG
;
245 vstorPacket
->Version
.MajorMinor
= VMSTOR_PROTOCOL_VERSION_CURRENT
;
246 FILL_VMSTOR_REVISION(vstorPacket
->Version
.Revision
);
248 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
250 sizeof(struct vstor_packet
),
251 (unsigned long)request
,
252 VmbusPacketTypeDataInBand
,
253 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
256 "unable to send BEGIN_INITIALIZATION_OPERATION");
260 osd_WaitEventWait(request
->WaitEvent
);
262 /* TODO: Check returned version */
263 if (vstorPacket
->Operation
!= VStorOperationCompleteIo
||
264 vstorPacket
->Status
!= 0) {
265 DPRINT_ERR(STORVSC
, "QUERY_PROTOCOL_VERSION_OPERATION failed "
266 "(op %d status 0x%x)",
267 vstorPacket
->Operation
, vstorPacket
->Status
);
271 /* Query channel properties */
272 DPRINT_INFO(STORVSC
, "QUERY_PROPERTIES_OPERATION...");
274 memset(vstorPacket
, 0, sizeof(struct vstor_packet
));
275 vstorPacket
->Operation
= VStorOperationQueryProperties
;
276 vstorPacket
->Flags
= REQUEST_COMPLETION_FLAG
;
277 vstorPacket
->StorageChannelProperties
.PortNumber
=
278 storDevice
->PortNumber
;
280 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
282 sizeof(struct vstor_packet
),
283 (unsigned long)request
,
284 VmbusPacketTypeDataInBand
,
285 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
289 "unable to send QUERY_PROPERTIES_OPERATION");
293 osd_WaitEventWait(request
->WaitEvent
);
295 /* TODO: Check returned version */
296 if (vstorPacket
->Operation
!= VStorOperationCompleteIo
||
297 vstorPacket
->Status
!= 0) {
298 DPRINT_ERR(STORVSC
, "QUERY_PROPERTIES_OPERATION failed "
299 "(op %d status 0x%x)",
300 vstorPacket
->Operation
, vstorPacket
->Status
);
304 storDevice
->PathId
= vstorPacket
->StorageChannelProperties
.PathId
;
305 storDevice
->TargetId
= vstorPacket
->StorageChannelProperties
.TargetId
;
307 DPRINT_DBG(STORVSC
, "channel flag 0x%x, max xfer len 0x%x",
308 vstorPacket
->StorageChannelProperties
.Flags
,
309 vstorPacket
->StorageChannelProperties
.MaxTransferBytes
);
311 DPRINT_INFO(STORVSC
, "END_INITIALIZATION_OPERATION...");
313 memset(vstorPacket
, 0, sizeof(struct vstor_packet
));
314 vstorPacket
->Operation
= VStorOperationEndInitialization
;
315 vstorPacket
->Flags
= REQUEST_COMPLETION_FLAG
;
317 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
319 sizeof(struct vstor_packet
),
320 (unsigned long)request
,
321 VmbusPacketTypeDataInBand
,
322 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
326 "unable to send END_INITIALIZATION_OPERATION");
330 osd_WaitEventWait(request
->WaitEvent
);
332 if (vstorPacket
->Operation
!= VStorOperationCompleteIo
||
333 vstorPacket
->Status
!= 0) {
334 DPRINT_ERR(STORVSC
, "END_INITIALIZATION_OPERATION failed "
335 "(op %d status 0x%x)",
336 vstorPacket
->Operation
, vstorPacket
->Status
);
340 DPRINT_INFO(STORVSC
, "**** storage channel up and running!! ****");
343 kfree(request
->WaitEvent
);
344 request
->WaitEvent
= NULL
;
346 PutStorDevice(Device
);
348 DPRINT_EXIT(STORVSC
);
352 static void StorVscOnIOCompletion(struct hv_device
*Device
,
353 struct vstor_packet
*VStorPacket
,
354 struct storvsc_request_extension
*RequestExt
)
356 struct hv_storvsc_request
*request
;
357 struct storvsc_device
*storDevice
;
359 storDevice
= MustGetStorDevice(Device
);
361 DPRINT_ERR(STORVSC
, "unable to get stor device..."
362 "device being destroyed?");
363 DPRINT_EXIT(STORVSC
);
367 DPRINT_DBG(STORVSC
, "IO_COMPLETE_OPERATION - request extension %p "
368 "completed bytes xfer %u", RequestExt
,
369 VStorPacket
->VmSrb
.DataTransferLength
);
371 /* ASSERT(RequestExt != NULL); */
372 /* ASSERT(RequestExt->Request != NULL); */
374 request
= RequestExt
->Request
;
376 /* ASSERT(request->OnIOCompletion != NULL); */
378 /* Copy over the status...etc */
379 request
->Status
= VStorPacket
->VmSrb
.ScsiStatus
;
381 if (request
->Status
!= 0 || VStorPacket
->VmSrb
.SrbStatus
!= 1) {
383 "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
384 request
->Cdb
[0], VStorPacket
->VmSrb
.ScsiStatus
,
385 VStorPacket
->VmSrb
.SrbStatus
);
388 if ((request
->Status
& 0xFF) == 0x02) {
389 /* CHECK_CONDITION */
390 if (VStorPacket
->VmSrb
.SrbStatus
& 0x80) {
391 /* autosense data available */
392 DPRINT_WARN(STORVSC
, "storvsc pkt %p autosense data "
393 "valid - len %d\n", RequestExt
,
394 VStorPacket
->VmSrb
.SenseInfoLength
);
396 /* ASSERT(VStorPacket->VmSrb.SenseInfoLength <= */
397 /* request->SenseBufferSize); */
398 memcpy(request
->SenseBuffer
,
399 VStorPacket
->VmSrb
.SenseData
,
400 VStorPacket
->VmSrb
.SenseInfoLength
);
402 request
->SenseBufferSize
=
403 VStorPacket
->VmSrb
.SenseInfoLength
;
408 request
->BytesXfer
= VStorPacket
->VmSrb
.DataTransferLength
;
410 request
->OnIOCompletion(request
);
412 atomic_dec(&storDevice
->NumOutstandingRequests
);
414 PutStorDevice(Device
);
416 DPRINT_EXIT(STORVSC
);
419 static void StorVscOnReceive(struct hv_device
*Device
,
420 struct vstor_packet
*VStorPacket
,
421 struct storvsc_request_extension
*RequestExt
)
423 switch (VStorPacket
->Operation
) {
424 case VStorOperationCompleteIo
:
425 DPRINT_DBG(STORVSC
, "IO_COMPLETE_OPERATION");
426 StorVscOnIOCompletion(Device
, VStorPacket
, RequestExt
);
428 case VStorOperationRemoveDevice
:
429 DPRINT_INFO(STORVSC
, "REMOVE_DEVICE_OPERATION");
434 DPRINT_INFO(STORVSC
, "Unknown operation received - %d",
435 VStorPacket
->Operation
);
440 static void StorVscOnChannelCallback(void *context
)
442 struct hv_device
*device
= (struct hv_device
*)context
;
443 struct storvsc_device
*storDevice
;
446 unsigned char packet
[ALIGN_UP(sizeof(struct vstor_packet
), 8)];
447 struct storvsc_request_extension
*request
;
450 /* ASSERT(device); */
452 storDevice
= MustGetStorDevice(device
);
454 DPRINT_ERR(STORVSC
, "unable to get stor device..."
455 "device being destroyed?");
456 DPRINT_EXIT(STORVSC
);
461 ret
= device
->Driver
->VmbusChannelInterface
.RecvPacket(device
,
463 ALIGN_UP(sizeof(struct vstor_packet
), 8),
464 &bytesRecvd
, &requestId
);
465 if (ret
== 0 && bytesRecvd
> 0) {
466 DPRINT_DBG(STORVSC
, "receive %d bytes - tid %llx",
467 bytesRecvd
, requestId
);
469 /* ASSERT(bytesRecvd == sizeof(struct vstor_packet)); */
471 request
= (struct storvsc_request_extension
*)
472 (unsigned long)requestId
;
473 /* ASSERT(request);c */
475 /* if (vstorPacket.Flags & SYNTHETIC_FLAG) */
476 if ((request
== &storDevice
->InitRequest
) ||
477 (request
== &storDevice
->ResetRequest
)) {
478 /* DPRINT_INFO(STORVSC,
479 * "reset completion - operation "
481 * vstorPacket.Operation,
482 * vstorPacket.Status); */
484 memcpy(&request
->VStorPacket
, packet
,
485 sizeof(struct vstor_packet
));
487 osd_WaitEventSet(request
->WaitEvent
);
489 StorVscOnReceive(device
,
490 (struct vstor_packet
*)packet
,
494 /* DPRINT_DBG(STORVSC, "nothing else to read..."); */
499 PutStorDevice(device
);
501 DPRINT_EXIT(STORVSC
);
505 static int StorVscConnectToVsp(struct hv_device
*Device
)
507 struct vmstorage_channel_properties props
;
508 struct storvsc_driver_object
*storDriver
;
511 storDriver
= (struct storvsc_driver_object
*)Device
->Driver
;
512 memset(&props
, 0, sizeof(struct vmstorage_channel_properties
));
514 /* Open the channel */
515 ret
= Device
->Driver
->VmbusChannelInterface
.Open(Device
,
516 storDriver
->RingBufferSize
,
517 storDriver
->RingBufferSize
,
519 sizeof(struct vmstorage_channel_properties
),
520 StorVscOnChannelCallback
,
523 DPRINT_DBG(STORVSC
, "storage props: path id %d, tgt id %d, max xfer %d",
524 props
.PathId
, props
.TargetId
, props
.MaxTransferBytes
);
527 DPRINT_ERR(STORVSC
, "unable to open channel: %d", ret
);
531 ret
= StorVscChannelInit(Device
);
537 * StorVscOnDeviceAdd - Callback when the device belonging to this driver is added
539 static int StorVscOnDeviceAdd(struct hv_device
*Device
, void *AdditionalInfo
)
541 struct storvsc_device
*storDevice
;
542 /* struct vmstorage_channel_properties *props; */
543 struct storvsc_device_info
*deviceInfo
;
546 deviceInfo
= (struct storvsc_device_info
*)AdditionalInfo
;
547 storDevice
= AllocStorDevice(Device
);
553 /* Save the channel properties to our storvsc channel */
554 /* props = (struct vmstorage_channel_properties *)
555 * channel->offerMsg.Offer.u.Standard.UserDefined; */
559 * If we support more than 1 scsi channel, we need to set the
560 * port number here to the scsi channel but how do we get the
561 * scsi channel prior to the bus scan
564 /* storChannel->PortNumber = 0;
565 storChannel->PathId = props->PathId;
566 storChannel->TargetId = props->TargetId; */
568 storDevice
->PortNumber
= deviceInfo
->PortNumber
;
569 /* Send it back up */
570 ret
= StorVscConnectToVsp(Device
);
572 /* deviceInfo->PortNumber = storDevice->PortNumber; */
573 deviceInfo
->PathId
= storDevice
->PathId
;
574 deviceInfo
->TargetId
= storDevice
->TargetId
;
576 DPRINT_DBG(STORVSC
, "assigned port %u, path %u target %u\n",
577 storDevice
->PortNumber
, storDevice
->PathId
,
578 storDevice
->TargetId
);
581 DPRINT_EXIT(STORVSC
);
587 * StorVscOnDeviceRemove - Callback when the our device is being removed
589 static int StorVscOnDeviceRemove(struct hv_device
*Device
)
591 struct storvsc_device
*storDevice
;
593 DPRINT_INFO(STORVSC
, "disabling storage device (%p)...",
596 storDevice
= ReleaseStorDevice(Device
);
599 * At this point, all outbound traffic should be disable. We
600 * only allow inbound traffic (responses) to proceed so that
601 * outstanding requests can be completed.
603 while (atomic_read(&storDevice
->NumOutstandingRequests
)) {
604 DPRINT_INFO(STORVSC
, "waiting for %d requests to complete...",
605 atomic_read(&storDevice
->NumOutstandingRequests
));
609 DPRINT_INFO(STORVSC
, "removing storage device (%p)...",
612 storDevice
= FinalReleaseStorDevice(Device
);
614 DPRINT_INFO(STORVSC
, "storage device (%p) safe to remove", storDevice
);
616 /* Close the channel */
617 Device
->Driver
->VmbusChannelInterface
.Close(Device
);
619 FreeStorDevice(storDevice
);
621 DPRINT_EXIT(STORVSC
);
625 int StorVscOnHostReset(struct hv_device
*Device
)
627 struct storvsc_device
*storDevice
;
628 struct storvsc_request_extension
*request
;
629 struct vstor_packet
*vstorPacket
;
632 DPRINT_INFO(STORVSC
, "resetting host adapter...");
634 storDevice
= GetStorDevice(Device
);
636 DPRINT_ERR(STORVSC
, "unable to get stor device..."
637 "device being destroyed?");
638 DPRINT_EXIT(STORVSC
);
642 request
= &storDevice
->ResetRequest
;
643 vstorPacket
= &request
->VStorPacket
;
645 request
->WaitEvent
= osd_WaitEventCreate();
646 if (!request
->WaitEvent
) {
651 vstorPacket
->Operation
= VStorOperationResetBus
;
652 vstorPacket
->Flags
= REQUEST_COMPLETION_FLAG
;
653 vstorPacket
->VmSrb
.PathId
= storDevice
->PathId
;
655 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
657 sizeof(struct vstor_packet
),
658 (unsigned long)&storDevice
->ResetRequest
,
659 VmbusPacketTypeDataInBand
,
660 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
662 DPRINT_ERR(STORVSC
, "Unable to send reset packet %p ret %d",
667 /* FIXME: Add a timeout */
668 osd_WaitEventWait(request
->WaitEvent
);
670 kfree(request
->WaitEvent
);
671 DPRINT_INFO(STORVSC
, "host adapter reset completed");
674 * At this point, all outstanding requests in the adapter
675 * should have been flushed out and return to us
679 PutStorDevice(Device
);
680 DPRINT_EXIT(STORVSC
);
685 * StorVscOnIORequest - Callback to initiate an I/O request
687 static int StorVscOnIORequest(struct hv_device
*Device
,
688 struct hv_storvsc_request
*Request
)
690 struct storvsc_device
*storDevice
;
691 struct storvsc_request_extension
*requestExtension
;
692 struct vstor_packet
*vstorPacket
;
696 (struct storvsc_request_extension
*)Request
->Extension
;
697 vstorPacket
= &requestExtension
->VStorPacket
;
698 storDevice
= GetStorDevice(Device
);
700 DPRINT_DBG(STORVSC
, "enter - Device %p, DeviceExt %p, Request %p, "
701 "Extension %p", Device
, storDevice
, Request
,
704 DPRINT_DBG(STORVSC
, "req %p len %d bus %d, target %d, lun %d cdblen %d",
705 Request
, Request
->DataBuffer
.Length
, Request
->Bus
,
706 Request
->TargetId
, Request
->LunId
, Request
->CdbLen
);
709 DPRINT_ERR(STORVSC
, "unable to get stor device..."
710 "device being destroyed?");
711 DPRINT_EXIT(STORVSC
);
715 /* print_hex_dump_bytes("", DUMP_PREFIX_NONE, Request->Cdb,
716 * Request->CdbLen); */
718 requestExtension
->Request
= Request
;
719 requestExtension
->Device
= Device
;
721 memset(vstorPacket
, 0 , sizeof(struct vstor_packet
));
723 vstorPacket
->Flags
|= REQUEST_COMPLETION_FLAG
;
725 vstorPacket
->VmSrb
.Length
= sizeof(struct vmscsi_request
);
727 vstorPacket
->VmSrb
.PortNumber
= Request
->Host
;
728 vstorPacket
->VmSrb
.PathId
= Request
->Bus
;
729 vstorPacket
->VmSrb
.TargetId
= Request
->TargetId
;
730 vstorPacket
->VmSrb
.Lun
= Request
->LunId
;
732 vstorPacket
->VmSrb
.SenseInfoLength
= SENSE_BUFFER_SIZE
;
734 /* Copy over the scsi command descriptor block */
735 vstorPacket
->VmSrb
.CdbLength
= Request
->CdbLen
;
736 memcpy(&vstorPacket
->VmSrb
.Cdb
, Request
->Cdb
, Request
->CdbLen
);
738 vstorPacket
->VmSrb
.DataIn
= Request
->Type
;
739 vstorPacket
->VmSrb
.DataTransferLength
= Request
->DataBuffer
.Length
;
741 vstorPacket
->Operation
= VStorOperationExecuteSRB
;
743 DPRINT_DBG(STORVSC
, "srb - len %d port %d, path %d, target %d, "
744 "lun %d senselen %d cdblen %d",
745 vstorPacket
->VmSrb
.Length
,
746 vstorPacket
->VmSrb
.PortNumber
,
747 vstorPacket
->VmSrb
.PathId
,
748 vstorPacket
->VmSrb
.TargetId
,
749 vstorPacket
->VmSrb
.Lun
,
750 vstorPacket
->VmSrb
.SenseInfoLength
,
751 vstorPacket
->VmSrb
.CdbLength
);
753 if (requestExtension
->Request
->DataBuffer
.Length
) {
754 ret
= Device
->Driver
->VmbusChannelInterface
.
755 SendPacketMultiPageBuffer(Device
,
756 &requestExtension
->Request
->DataBuffer
,
758 sizeof(struct vstor_packet
),
759 (unsigned long)requestExtension
);
761 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
763 sizeof(struct vstor_packet
),
764 (unsigned long)requestExtension
,
765 VmbusPacketTypeDataInBand
,
766 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
770 DPRINT_DBG(STORVSC
, "Unable to send packet %p ret %d",
774 atomic_inc(&storDevice
->NumOutstandingRequests
);
776 PutStorDevice(Device
);
778 DPRINT_EXIT(STORVSC
);
783 * StorVscOnCleanup - Perform any cleanup when the driver is removed
785 static void StorVscOnCleanup(struct hv_driver
*Driver
)
787 DPRINT_EXIT(STORVSC
);
791 * StorVscInitialize - Main entry point
793 int StorVscInitialize(struct hv_driver
*Driver
)
795 struct storvsc_driver_object
*storDriver
;
797 storDriver
= (struct storvsc_driver_object
*)Driver
;
799 DPRINT_DBG(STORVSC
, "sizeof(STORVSC_REQUEST)=%zd "
800 "sizeof(struct storvsc_request_extension)=%zd "
801 "sizeof(struct vstor_packet)=%zd, "
802 "sizeof(struct vmscsi_request)=%zd",
803 sizeof(struct hv_storvsc_request
),
804 sizeof(struct storvsc_request_extension
),
805 sizeof(struct vstor_packet
),
806 sizeof(struct vmscsi_request
));
808 /* Make sure we are at least 2 pages since 1 page is used for control */
809 /* ASSERT(storDriver->RingBufferSize >= (PAGE_SIZE << 1)); */
811 Driver
->name
= gDriverName
;
812 memcpy(&Driver
->deviceType
, &gStorVscDeviceType
,
813 sizeof(struct hv_guid
));
815 storDriver
->RequestExtSize
= sizeof(struct storvsc_request_extension
);
818 * Divide the ring buffer data size (which is 1 page less
819 * than the ring buffer size since that page is reserved for
820 * the ring buffer indices) by the max request size (which is
821 * VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER + struct vstor_packet + u64)
823 storDriver
->MaxOutstandingRequestsPerChannel
=
824 ((storDriver
->RingBufferSize
- PAGE_SIZE
) /
825 ALIGN_UP(MAX_MULTIPAGE_BUFFER_PACKET
+
826 sizeof(struct vstor_packet
) + sizeof(u64
),
829 DPRINT_INFO(STORVSC
, "max io %u, currently %u\n",
830 storDriver
->MaxOutstandingRequestsPerChannel
,
831 STORVSC_MAX_IO_REQUESTS
);
833 /* Setup the dispatch table */
834 storDriver
->Base
.OnDeviceAdd
= StorVscOnDeviceAdd
;
835 storDriver
->Base
.OnDeviceRemove
= StorVscOnDeviceRemove
;
836 storDriver
->Base
.OnCleanup
= StorVscOnCleanup
;
838 storDriver
->OnIORequest
= StorVscOnIORequest
;
840 DPRINT_EXIT(STORVSC
);