3 * Copyright (c) 2009, Microsoft Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16 * Place - Suite 330, Boston, MA 02111-1307 USA.
19 * Haiyang Zhang <haiyangz@microsoft.com>
20 * Hank Janssen <hjanssen@microsoft.com>
24 #include <linux/kernel.h>
25 #include <linux/string.h>
27 #include <linux/delay.h>
28 #include "include/logging.h"
30 #include "include/StorVscApi.h"
31 #include "include/VmbusPacketFormat.h"
32 #include "include/vstorage.h"
43 typedef struct _STORVSC_REQUEST_EXTENSION
{
44 /* LIST_ENTRY ListEntry; */
46 STORVSC_REQUEST
*Request
;
47 struct hv_device
*Device
;
49 /* Synchronize the request/response if needed */
50 struct osd_waitevent
*WaitEvent
;
52 VSTOR_PACKET VStorPacket
;
53 } STORVSC_REQUEST_EXTENSION
;
56 /* A storvsc device is a device object that contains a vmbus channel */
57 typedef struct _STORVSC_DEVICE
{
58 struct hv_device
*Device
;
60 int RefCount
; /* 0 indicates the device is being destroyed */
62 int NumOutstandingRequests
;
65 * Each unique Port/Path/Target represents 1 channel ie scsi
66 * controller. In reality, the pathid, targetid is always 0
67 * and the port is set by us
69 unsigned int PortNumber
;
71 unsigned char TargetId
;
73 /* LIST_ENTRY OutstandingRequestList; */
74 /* HANDLE OutstandingRequestLock; */
76 /* Used for vsc/vsp channel reset process */
77 STORVSC_REQUEST_EXTENSION InitRequest
;
79 STORVSC_REQUEST_EXTENSION ResetRequest
;
87 static const char* gDriverName
="storvsc";
89 /* {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f} */
90 static const GUID gStorVscDeviceType
={
91 .Data
= {0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d, 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f}
95 /* Internal routines */
99 struct hv_device
*Device
,
104 StorVscOnDeviceRemove(
105 struct hv_device
*Device
110 struct hv_device
*Device
,
111 STORVSC_REQUEST
*Request
116 struct hv_device
*Device
121 DRIVER_OBJECT
*Device
125 StorVscOnChannelCallback(
130 StorVscOnIOCompletion(
131 struct hv_device
*Device
,
132 VSTOR_PACKET
*VStorPacket
,
133 STORVSC_REQUEST_EXTENSION
*RequestExt
138 struct hv_device
*Device
,
139 VSTOR_PACKET
*VStorPacket
,
140 STORVSC_REQUEST_EXTENSION
*RequestExt
145 struct hv_device
*Device
148 static inline STORVSC_DEVICE
* AllocStorDevice(struct hv_device
*Device
)
150 STORVSC_DEVICE
*storDevice
;
152 storDevice
= kzalloc(sizeof(STORVSC_DEVICE
), GFP_KERNEL
);
156 /* Set to 2 to allow both inbound and outbound traffics */
157 /* (ie GetStorDevice() and MustGetStorDevice()) to proceed. */
158 InterlockedCompareExchange(&storDevice
->RefCount
, 2, 0);
160 storDevice
->Device
= Device
;
161 Device
->Extension
= storDevice
;
166 static inline void FreeStorDevice(STORVSC_DEVICE
*Device
)
168 ASSERT(Device
->RefCount
== 0);
172 /* Get the stordevice object iff exists and its refcount > 1 */
173 static inline STORVSC_DEVICE
* GetStorDevice(struct hv_device
*Device
)
175 STORVSC_DEVICE
*storDevice
;
177 storDevice
= (STORVSC_DEVICE
*)Device
->Extension
;
178 if (storDevice
&& storDevice
->RefCount
> 1)
180 InterlockedIncrement(&storDevice
->RefCount
);
190 /* Get the stordevice object iff exists and its refcount > 0 */
191 static inline STORVSC_DEVICE
* MustGetStorDevice(struct hv_device
*Device
)
193 STORVSC_DEVICE
*storDevice
;
195 storDevice
= (STORVSC_DEVICE
*)Device
->Extension
;
196 if (storDevice
&& storDevice
->RefCount
)
198 InterlockedIncrement(&storDevice
->RefCount
);
208 static inline void PutStorDevice(struct hv_device
*Device
)
210 STORVSC_DEVICE
*storDevice
;
212 storDevice
= (STORVSC_DEVICE
*)Device
->Extension
;
215 InterlockedDecrement(&storDevice
->RefCount
);
216 ASSERT(storDevice
->RefCount
);
219 /* Drop ref count to 1 to effectively disable GetStorDevice() */
220 static inline STORVSC_DEVICE
* ReleaseStorDevice(struct hv_device
*Device
)
222 STORVSC_DEVICE
*storDevice
;
224 storDevice
= (STORVSC_DEVICE
*)Device
->Extension
;
227 /* Busy wait until the ref drop to 2, then set it to 1 */
228 while (InterlockedCompareExchange(&storDevice
->RefCount
, 1, 2) != 2)
236 /* Drop ref count to 0. No one can use StorDevice object. */
237 static inline STORVSC_DEVICE
* FinalReleaseStorDevice(struct hv_device
*Device
)
239 STORVSC_DEVICE
*storDevice
;
241 storDevice
= (STORVSC_DEVICE
*)Device
->Extension
;
244 /* Busy wait until the ref drop to 1, then set it to 0 */
245 while (InterlockedCompareExchange(&storDevice
->RefCount
, 0, 1) != 1)
250 Device
->Extension
= NULL
;
266 DRIVER_OBJECT
*Driver
269 STORVSC_DRIVER_OBJECT
* storDriver
= (STORVSC_DRIVER_OBJECT
*)Driver
;
272 DPRINT_ENTER(STORVSC
);
274 DPRINT_DBG(STORVSC
, "sizeof(STORVSC_REQUEST)=%zd sizeof(STORVSC_REQUEST_EXTENSION)=%zd sizeof(VSTOR_PACKET)=%zd, sizeof(VMSCSI_REQUEST)=%zd",
275 sizeof(STORVSC_REQUEST
), sizeof(STORVSC_REQUEST_EXTENSION
), sizeof(VSTOR_PACKET
), sizeof(VMSCSI_REQUEST
));
277 /* Make sure we are at least 2 pages since 1 page is used for control */
278 ASSERT(storDriver
->RingBufferSize
>= (PAGE_SIZE
<< 1));
280 Driver
->name
= gDriverName
;
281 memcpy(&Driver
->deviceType
, &gStorVscDeviceType
, sizeof(GUID
));
283 storDriver
->RequestExtSize
= sizeof(STORVSC_REQUEST_EXTENSION
);
286 * Divide the ring buffer data size (which is 1 page less
287 * than the ring buffer size since that page is reserved for
288 * the ring buffer indices) by the max request size (which is
289 * VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER + VSTOR_PACKET + u64)
291 storDriver
->MaxOutstandingRequestsPerChannel
=
292 ((storDriver
->RingBufferSize
- PAGE_SIZE
) / ALIGN_UP(MAX_MULTIPAGE_BUFFER_PACKET
+ sizeof(VSTOR_PACKET
) + sizeof(u64
),sizeof(u64
)));
294 DPRINT_INFO(STORVSC
, "max io %u, currently %u\n", storDriver
->MaxOutstandingRequestsPerChannel
, STORVSC_MAX_IO_REQUESTS
);
296 /* Setup the dispatch table */
297 storDriver
->Base
.OnDeviceAdd
= StorVscOnDeviceAdd
;
298 storDriver
->Base
.OnDeviceRemove
= StorVscOnDeviceRemove
;
299 storDriver
->Base
.OnCleanup
= StorVscOnCleanup
;
301 storDriver
->OnIORequest
= StorVscOnIORequest
;
302 storDriver
->OnHostReset
= StorVscOnHostReset
;
304 DPRINT_EXIT(STORVSC
);
315 Callback when the device belonging to this driver is added
320 struct hv_device
*Device
,
325 STORVSC_DEVICE
*storDevice
;
326 /* VMSTORAGE_CHANNEL_PROPERTIES *props; */
327 STORVSC_DEVICE_INFO
*deviceInfo
= (STORVSC_DEVICE_INFO
*)AdditionalInfo
;
329 DPRINT_ENTER(STORVSC
);
331 storDevice
= AllocStorDevice(Device
);
338 /* Save the channel properties to our storvsc channel */
339 /* props = (VMSTORAGE_CHANNEL_PROPERTIES*) channel->offerMsg.Offer.u.Standard.UserDefined; */
343 * If we support more than 1 scsi channel, we need to set the
344 * port number here to the scsi channel but how do we get the
345 * scsi channel prior to the bus scan
348 /* storChannel->PortNumber = 0;
349 storChannel->PathId = props->PathId;
350 storChannel->TargetId = props->TargetId; */
352 storDevice
->PortNumber
= deviceInfo
->PortNumber
;
353 /* Send it back up */
354 ret
= StorVscConnectToVsp(Device
);
356 /* deviceInfo->PortNumber = storDevice->PortNumber; */
357 deviceInfo
->PathId
= storDevice
->PathId
;
358 deviceInfo
->TargetId
= storDevice
->TargetId
;
360 DPRINT_DBG(STORVSC
, "assigned port %u, path %u target %u\n", storDevice
->PortNumber
, storDevice
->PathId
, storDevice
->TargetId
);
363 DPRINT_EXIT(STORVSC
);
368 static int StorVscChannelInit(struct hv_device
*Device
)
371 STORVSC_DEVICE
*storDevice
;
372 STORVSC_REQUEST_EXTENSION
*request
;
373 VSTOR_PACKET
*vstorPacket
;
375 storDevice
= GetStorDevice(Device
);
378 DPRINT_ERR(STORVSC
, "unable to get stor device...device being destroyed?");
379 DPRINT_EXIT(STORVSC
);
383 request
= &storDevice
->InitRequest
;
384 vstorPacket
= &request
->VStorPacket
;
386 /* Now, initiate the vsc/vsp initialization protocol on the open channel */
388 memset(request
, sizeof(STORVSC_REQUEST_EXTENSION
), 0);
389 request
->WaitEvent
= WaitEventCreate();
391 vstorPacket
->Operation
= VStorOperationBeginInitialization
;
392 vstorPacket
->Flags
= REQUEST_COMPLETION_FLAG
;
394 /*SpinlockAcquire(gDriverExt.packetListLock);
395 INSERT_TAIL_LIST(&gDriverExt.packetList, &packet->listEntry.entry);
396 SpinlockRelease(gDriverExt.packetListLock);*/
398 DPRINT_INFO(STORVSC
, "BEGIN_INITIALIZATION_OPERATION...");
400 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
402 sizeof(VSTOR_PACKET
),
403 (unsigned long)request
,
404 VmbusPacketTypeDataInBand
,
405 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
408 DPRINT_ERR(STORVSC
, "unable to send BEGIN_INITIALIZATION_OPERATION");
412 WaitEventWait(request
->WaitEvent
);
414 if (vstorPacket
->Operation
!= VStorOperationCompleteIo
|| vstorPacket
->Status
!= 0)
416 DPRINT_ERR(STORVSC
, "BEGIN_INITIALIZATION_OPERATION failed (op %d status 0x%x)", vstorPacket
->Operation
, vstorPacket
->Status
);
420 DPRINT_INFO(STORVSC
, "QUERY_PROTOCOL_VERSION_OPERATION...");
422 /* reuse the packet for version range supported */
423 memset(vstorPacket
, sizeof(VSTOR_PACKET
), 0);
424 vstorPacket
->Operation
= VStorOperationQueryProtocolVersion
;
425 vstorPacket
->Flags
= REQUEST_COMPLETION_FLAG
;
427 vstorPacket
->Version
.MajorMinor
= VMSTOR_PROTOCOL_VERSION_CURRENT
;
428 FILL_VMSTOR_REVISION(vstorPacket
->Version
.Revision
);
430 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
432 sizeof(VSTOR_PACKET
),
433 (unsigned long)request
,
434 VmbusPacketTypeDataInBand
,
435 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
438 DPRINT_ERR(STORVSC
, "unable to send BEGIN_INITIALIZATION_OPERATION");
442 WaitEventWait(request
->WaitEvent
);
444 /* TODO: Check returned version */
445 if (vstorPacket
->Operation
!= VStorOperationCompleteIo
|| vstorPacket
->Status
!= 0)
447 DPRINT_ERR(STORVSC
, "QUERY_PROTOCOL_VERSION_OPERATION failed (op %d status 0x%x)", vstorPacket
->Operation
, vstorPacket
->Status
);
451 /* Query channel properties */
452 DPRINT_INFO(STORVSC
, "QUERY_PROPERTIES_OPERATION...");
454 memset(vstorPacket
, sizeof(VSTOR_PACKET
), 0);
455 vstorPacket
->Operation
= VStorOperationQueryProperties
;
456 vstorPacket
->Flags
= REQUEST_COMPLETION_FLAG
;
457 vstorPacket
->StorageChannelProperties
.PortNumber
= storDevice
->PortNumber
;
459 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
461 sizeof(VSTOR_PACKET
),
462 (unsigned long)request
,
463 VmbusPacketTypeDataInBand
,
464 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
468 DPRINT_ERR(STORVSC
, "unable to send QUERY_PROPERTIES_OPERATION");
472 WaitEventWait(request
->WaitEvent
);
474 /* TODO: Check returned version */
475 if (vstorPacket
->Operation
!= VStorOperationCompleteIo
|| vstorPacket
->Status
!= 0)
477 DPRINT_ERR(STORVSC
, "QUERY_PROPERTIES_OPERATION failed (op %d status 0x%x)", vstorPacket
->Operation
, vstorPacket
->Status
);
481 /* storDevice->PortNumber = vstorPacket->StorageChannelProperties.PortNumber; */
482 storDevice
->PathId
= vstorPacket
->StorageChannelProperties
.PathId
;
483 storDevice
->TargetId
= vstorPacket
->StorageChannelProperties
.TargetId
;
485 DPRINT_DBG(STORVSC
, "channel flag 0x%x, max xfer len 0x%x", vstorPacket
->StorageChannelProperties
.Flags
, vstorPacket
->StorageChannelProperties
.MaxTransferBytes
);
487 DPRINT_INFO(STORVSC
, "END_INITIALIZATION_OPERATION...");
489 memset(vstorPacket
, sizeof(VSTOR_PACKET
), 0);
490 vstorPacket
->Operation
= VStorOperationEndInitialization
;
491 vstorPacket
->Flags
= REQUEST_COMPLETION_FLAG
;
493 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
495 sizeof(VSTOR_PACKET
),
496 (unsigned long)request
,
497 VmbusPacketTypeDataInBand
,
498 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
502 DPRINT_ERR(STORVSC
, "unable to send END_INITIALIZATION_OPERATION");
506 WaitEventWait(request
->WaitEvent
);
508 if (vstorPacket
->Operation
!= VStorOperationCompleteIo
|| vstorPacket
->Status
!= 0)
510 DPRINT_ERR(STORVSC
, "END_INITIALIZATION_OPERATION failed (op %d status 0x%x)", vstorPacket
->Operation
, vstorPacket
->Status
);
514 DPRINT_INFO(STORVSC
, "**** storage channel up and running!! ****");
517 if (request
->WaitEvent
)
519 WaitEventClose(request
->WaitEvent
);
520 request
->WaitEvent
= NULL
;
523 PutStorDevice(Device
);
525 DPRINT_EXIT(STORVSC
);
532 struct hv_device
*Device
536 VMSTORAGE_CHANNEL_PROPERTIES props
;
538 STORVSC_DRIVER_OBJECT
*storDriver
= (STORVSC_DRIVER_OBJECT
*) Device
->Driver
;;
540 memset(&props
, sizeof(VMSTORAGE_CHANNEL_PROPERTIES
), 0);
542 /* Open the channel */
543 ret
= Device
->Driver
->VmbusChannelInterface
.Open(Device
,
544 storDriver
->RingBufferSize
,
545 storDriver
->RingBufferSize
,
547 sizeof(VMSTORAGE_CHANNEL_PROPERTIES
),
548 StorVscOnChannelCallback
,
552 DPRINT_DBG(STORVSC
, "storage props: path id %d, tgt id %d, max xfer %d", props
.PathId
, props
.TargetId
, props
.MaxTransferBytes
);
556 DPRINT_ERR(STORVSC
, "unable to open channel: %d", ret
);
560 ret
= StorVscChannelInit(Device
);
569 StorVscOnDeviceRemove()
572 Callback when the our device is being removed
576 StorVscOnDeviceRemove(
577 struct hv_device
*Device
580 STORVSC_DEVICE
*storDevice
;
583 DPRINT_ENTER(STORVSC
);
585 DPRINT_INFO(STORVSC
, "disabling storage device (%p)...", Device
->Extension
);
587 storDevice
= ReleaseStorDevice(Device
);
590 * At this point, all outbound traffic should be disable. We
591 * only allow inbound traffic (responses) to proceed so that
592 * outstanding requests can be completed.
594 while (storDevice
->NumOutstandingRequests
)
596 DPRINT_INFO(STORVSC
, "waiting for %d requests to complete...", storDevice
->NumOutstandingRequests
);
601 DPRINT_INFO(STORVSC
, "removing storage device (%p)...", Device
->Extension
);
603 storDevice
= FinalReleaseStorDevice(Device
);
605 DPRINT_INFO(STORVSC
, "storage device (%p) safe to remove", storDevice
);
607 /* Close the channel */
608 Device
->Driver
->VmbusChannelInterface
.Close(Device
);
610 FreeStorDevice(storDevice
);
612 DPRINT_EXIT(STORVSC
);
618 StorVscOnTargetRescan(
622 struct hv_device *device=(struct hv_device *)Context;
623 STORVSC_DRIVER_OBJECT *storDriver;
625 DPRINT_ENTER(STORVSC);
627 storDriver = (STORVSC_DRIVER_OBJECT*) device->Driver;
628 storDriver->OnHostRescan(device);
630 DPRINT_EXIT(STORVSC);
636 struct hv_device
*Device
641 STORVSC_DEVICE
*storDevice
;
642 STORVSC_REQUEST_EXTENSION
*request
;
643 VSTOR_PACKET
*vstorPacket
;
645 DPRINT_ENTER(STORVSC
);
647 DPRINT_INFO(STORVSC
, "resetting host adapter...");
649 storDevice
= GetStorDevice(Device
);
652 DPRINT_ERR(STORVSC
, "unable to get stor device...device being destroyed?");
653 DPRINT_EXIT(STORVSC
);
657 request
= &storDevice
->ResetRequest
;
658 vstorPacket
= &request
->VStorPacket
;
660 request
->WaitEvent
= WaitEventCreate();
662 vstorPacket
->Operation
= VStorOperationResetBus
;
663 vstorPacket
->Flags
= REQUEST_COMPLETION_FLAG
;
664 vstorPacket
->VmSrb
.PathId
= storDevice
->PathId
;
666 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
668 sizeof(VSTOR_PACKET
),
669 (unsigned long)&storDevice
->ResetRequest
,
670 VmbusPacketTypeDataInBand
,
671 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
674 DPRINT_ERR(STORVSC
, "Unable to send reset packet %p ret %d", vstorPacket
, ret
);
678 /* FIXME: Add a timeout */
679 WaitEventWait(request
->WaitEvent
);
681 WaitEventClose(request
->WaitEvent
);
682 DPRINT_INFO(STORVSC
, "host adapter reset completed");
685 * At this point, all outstanding requests in the adapter
686 * should have been flushed out and return to us
690 PutStorDevice(Device
);
691 DPRINT_EXIT(STORVSC
);
701 Callback to initiate an I/O request
706 struct hv_device
*Device
,
707 STORVSC_REQUEST
*Request
710 STORVSC_DEVICE
*storDevice
;
711 STORVSC_REQUEST_EXTENSION
* requestExtension
= (STORVSC_REQUEST_EXTENSION
*) Request
->Extension
;
712 VSTOR_PACKET
* vstorPacket
=&requestExtension
->VStorPacket
;
715 DPRINT_ENTER(STORVSC
);
717 storDevice
= GetStorDevice(Device
);
719 DPRINT_DBG(STORVSC
, "enter - Device %p, DeviceExt %p, Request %p, Extension %p",
720 Device
, storDevice
, Request
, requestExtension
);
722 DPRINT_DBG(STORVSC
, "req %p len %d bus %d, target %d, lun %d cdblen %d",
723 Request
, Request
->DataBuffer
.Length
, Request
->Bus
, Request
->TargetId
, Request
->LunId
, Request
->CdbLen
);
727 DPRINT_ERR(STORVSC
, "unable to get stor device...device being destroyed?");
728 DPRINT_EXIT(STORVSC
);
732 /* print_hex_dump_bytes("", DUMP_PREFIX_NONE, Request->Cdb, Request->CdbLen); */
734 requestExtension
->Request
= Request
;
735 requestExtension
->Device
= Device
;
737 memset(vstorPacket
, 0 , sizeof(VSTOR_PACKET
));
739 vstorPacket
->Flags
|= REQUEST_COMPLETION_FLAG
;
741 vstorPacket
->VmSrb
.Length
= sizeof(VMSCSI_REQUEST
);
743 vstorPacket
->VmSrb
.PortNumber
= Request
->Host
;
744 vstorPacket
->VmSrb
.PathId
= Request
->Bus
;
745 vstorPacket
->VmSrb
.TargetId
= Request
->TargetId
;
746 vstorPacket
->VmSrb
.Lun
= Request
->LunId
;
748 vstorPacket
->VmSrb
.SenseInfoLength
= SENSE_BUFFER_SIZE
;
750 /* Copy over the scsi command descriptor block */
751 vstorPacket
->VmSrb
.CdbLength
= Request
->CdbLen
;
752 memcpy(&vstorPacket
->VmSrb
.Cdb
, Request
->Cdb
, Request
->CdbLen
);
754 vstorPacket
->VmSrb
.DataIn
= Request
->Type
;
755 vstorPacket
->VmSrb
.DataTransferLength
= Request
->DataBuffer
.Length
;
757 vstorPacket
->Operation
= VStorOperationExecuteSRB
;
759 DPRINT_DBG(STORVSC
, "srb - len %d port %d, path %d, target %d, lun %d senselen %d cdblen %d",
760 vstorPacket
->VmSrb
.Length
,
761 vstorPacket
->VmSrb
.PortNumber
,
762 vstorPacket
->VmSrb
.PathId
,
763 vstorPacket
->VmSrb
.TargetId
,
764 vstorPacket
->VmSrb
.Lun
,
765 vstorPacket
->VmSrb
.SenseInfoLength
,
766 vstorPacket
->VmSrb
.CdbLength
);
768 if (requestExtension
->Request
->DataBuffer
.Length
)
770 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacketMultiPageBuffer(Device
,
771 &requestExtension
->Request
->DataBuffer
,
773 sizeof(VSTOR_PACKET
),
774 (unsigned long)requestExtension
);
778 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
780 sizeof(VSTOR_PACKET
),
781 (unsigned long)requestExtension
,
782 VmbusPacketTypeDataInBand
,
783 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
788 DPRINT_DBG(STORVSC
, "Unable to send packet %p ret %d", vstorPacket
, ret
);
791 InterlockedIncrement(&storDevice
->NumOutstandingRequests
);
793 PutStorDevice(Device
);
795 DPRINT_EXIT(STORVSC
);
805 Perform any cleanup when the driver is removed
810 DRIVER_OBJECT
*Driver
813 DPRINT_ENTER(STORVSC
);
814 DPRINT_EXIT(STORVSC
);
819 StorVscOnIOCompletion(
820 struct hv_device
*Device
,
821 VSTOR_PACKET
*VStorPacket
,
822 STORVSC_REQUEST_EXTENSION
*RequestExt
825 STORVSC_REQUEST
*request
;
826 STORVSC_DEVICE
*storDevice
;
828 DPRINT_ENTER(STORVSC
);
830 storDevice
= MustGetStorDevice(Device
);
833 DPRINT_ERR(STORVSC
, "unable to get stor device...device being destroyed?");
834 DPRINT_EXIT(STORVSC
);
838 DPRINT_DBG(STORVSC
, "IO_COMPLETE_OPERATION - request extension %p completed bytes xfer %u",
839 RequestExt
, VStorPacket
->VmSrb
.DataTransferLength
);
841 ASSERT(RequestExt
!= NULL
);
842 ASSERT(RequestExt
->Request
!= NULL
);
844 request
= RequestExt
->Request
;
846 ASSERT(request
->OnIOCompletion
!= NULL
);
848 /* Copy over the status...etc */
849 request
->Status
= VStorPacket
->VmSrb
.ScsiStatus
;
851 if (request
->Status
!= 0 || VStorPacket
->VmSrb
.SrbStatus
!= 1)
853 DPRINT_WARN(STORVSC
, "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
855 VStorPacket
->VmSrb
.ScsiStatus
,
856 VStorPacket
->VmSrb
.SrbStatus
);
859 if ((request
->Status
& 0xFF) == 0x02) /* CHECK_CONDITION */
861 if (VStorPacket
->VmSrb
.SrbStatus
& 0x80) /* autosense data available */
863 DPRINT_WARN(STORVSC
, "storvsc pkt %p autosense data valid - len %d\n",
864 RequestExt
, VStorPacket
->VmSrb
.SenseInfoLength
);
866 ASSERT(VStorPacket
->VmSrb
.SenseInfoLength
<= request
->SenseBufferSize
);
867 memcpy(request
->SenseBuffer
,
868 VStorPacket
->VmSrb
.SenseData
,
869 VStorPacket
->VmSrb
.SenseInfoLength
);
871 request
->SenseBufferSize
= VStorPacket
->VmSrb
.SenseInfoLength
;
876 request
->BytesXfer
= VStorPacket
->VmSrb
.DataTransferLength
;
878 request
->OnIOCompletion(request
);
880 InterlockedDecrement(&storDevice
->NumOutstandingRequests
);
882 PutStorDevice(Device
);
884 DPRINT_EXIT(STORVSC
);
890 struct hv_device
*Device
,
891 VSTOR_PACKET
*VStorPacket
,
892 STORVSC_REQUEST_EXTENSION
*RequestExt
895 switch(VStorPacket
->Operation
)
897 case VStorOperationCompleteIo
:
899 DPRINT_DBG(STORVSC
, "IO_COMPLETE_OPERATION");
900 StorVscOnIOCompletion(Device
, VStorPacket
, RequestExt
);
903 /* case ENUMERATE_DEVICE_OPERATION: */
905 /* DPRINT_INFO(STORVSC, "ENUMERATE_DEVICE_OPERATION"); */
907 /* StorVscOnTargetRescan(Device); */
910 case VStorOperationRemoveDevice
:
912 DPRINT_INFO(STORVSC
, "REMOVE_DEVICE_OPERATION");
917 DPRINT_INFO(STORVSC
, "Unknown operation received - %d", VStorPacket
->Operation
);
923 StorVscOnChannelCallback(
928 struct hv_device
*device
= (struct hv_device
*)Context
;
929 STORVSC_DEVICE
*storDevice
;
932 unsigned char packet
[ALIGN_UP(sizeof(VSTOR_PACKET
),8)];
933 STORVSC_REQUEST_EXTENSION
*request
;
935 DPRINT_ENTER(STORVSC
);
939 storDevice
= MustGetStorDevice(device
);
942 DPRINT_ERR(STORVSC
, "unable to get stor device...device being destroyed?");
943 DPRINT_EXIT(STORVSC
);
949 ret
= device
->Driver
->VmbusChannelInterface
.RecvPacket(device
,
951 ALIGN_UP(sizeof(VSTOR_PACKET
),8),
954 if (ret
== 0 && bytesRecvd
> 0)
956 DPRINT_DBG(STORVSC
, "receive %d bytes - tid %llx", bytesRecvd
, requestId
);
958 /* ASSERT(bytesRecvd == sizeof(VSTOR_PACKET)); */
960 request
= (STORVSC_REQUEST_EXTENSION
*)(unsigned long)requestId
;
963 /* if (vstorPacket.Flags & SYNTHETIC_FLAG) */
964 if ((request
== &storDevice
->InitRequest
) || (request
== &storDevice
->ResetRequest
))
966 /* DPRINT_INFO(STORVSC, "reset completion - operation %u status %u", vstorPacket.Operation, vstorPacket.Status); */
968 memcpy(&request
->VStorPacket
, packet
, sizeof(VSTOR_PACKET
));
970 WaitEventSet(request
->WaitEvent
);
974 StorVscOnReceive(device
, (VSTOR_PACKET
*)packet
, request
);
979 /* DPRINT_DBG(STORVSC, "nothing else to read..."); */
984 PutStorDevice(device
);
986 DPRINT_EXIT(STORVSC
);