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 DEVICE_OBJECT *Device;
49 // Synchronize the request/response if needed
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 DEVICE_OBJECT *Device;
60 int RefCount; // 0 indicates the device is being destroyed
62 int NumOutstandingRequests;
64 // Each unique Port/Path/Target represents 1 channel ie scsi controller. In reality, the pathid, targetid is always 0
65 // and the port is set by us
66 unsigned int PortNumber;
68 unsigned char TargetId;
70 //LIST_ENTRY OutstandingRequestList;
71 //HANDLE OutstandingRequestLock;
73 // Used for vsc/vsp channel reset process
74 STORVSC_REQUEST_EXTENSION InitRequest;
76 STORVSC_REQUEST_EXTENSION ResetRequest;
84 static const char* gDriverName="storvsc";
86 //{ba6163d9-04a1-4d29-b605-72e2ffb1dc7f}
87 static const GUID gStorVscDeviceType={
88 .Data = {0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d, 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f}
96 DEVICE_OBJECT *Device,
101 StorVscOnDeviceRemove(
102 DEVICE_OBJECT *Device
107 DEVICE_OBJECT *Device,
108 STORVSC_REQUEST *Request
113 DEVICE_OBJECT *Device
118 DRIVER_OBJECT *Device
122 StorVscOnChannelCallback(
127 StorVscOnIOCompletion(
128 DEVICE_OBJECT *Device,
129 VSTOR_PACKET *VStorPacket,
130 STORVSC_REQUEST_EXTENSION *RequestExt
135 DEVICE_OBJECT *Device,
136 VSTOR_PACKET *VStorPacket,
137 STORVSC_REQUEST_EXTENSION *RequestExt
142 DEVICE_OBJECT *Device
145 static inline STORVSC_DEVICE* AllocStorDevice(DEVICE_OBJECT *Device)
147 STORVSC_DEVICE *storDevice;
149 storDevice = kzalloc(sizeof(STORVSC_DEVICE), GFP_KERNEL);
153 // Set to 2 to allow both inbound and outbound traffics
154 // (ie GetStorDevice() and MustGetStorDevice()) to proceed.
155 InterlockedCompareExchange(&storDevice->RefCount, 2, 0);
157 storDevice->Device = Device;
158 Device->Extension = storDevice;
163 static inline void FreeStorDevice(STORVSC_DEVICE *Device)
165 ASSERT(Device->RefCount == 0);
169 // Get the stordevice object iff exists and its refcount > 1
170 static inline STORVSC_DEVICE* GetStorDevice(DEVICE_OBJECT *Device)
172 STORVSC_DEVICE *storDevice;
174 storDevice = (STORVSC_DEVICE*)Device->Extension;
175 if (storDevice && storDevice->RefCount > 1)
177 InterlockedIncrement(&storDevice->RefCount);
187 // Get the stordevice object iff exists and its refcount > 0
188 static inline STORVSC_DEVICE* MustGetStorDevice(DEVICE_OBJECT *Device)
190 STORVSC_DEVICE *storDevice;
192 storDevice = (STORVSC_DEVICE*)Device->Extension;
193 if (storDevice && storDevice->RefCount)
195 InterlockedIncrement(&storDevice->RefCount);
205 static inline void PutStorDevice(DEVICE_OBJECT *Device)
207 STORVSC_DEVICE *storDevice;
209 storDevice = (STORVSC_DEVICE*)Device->Extension;
212 InterlockedDecrement(&storDevice->RefCount);
213 ASSERT(storDevice->RefCount);
216 // Drop ref count to 1 to effectively disable GetStorDevice()
217 static inline STORVSC_DEVICE* ReleaseStorDevice(DEVICE_OBJECT *Device)
219 STORVSC_DEVICE *storDevice;
221 storDevice = (STORVSC_DEVICE*)Device->Extension;
224 // Busy wait until the ref drop to 2, then set it to 1
225 while (InterlockedCompareExchange(&storDevice->RefCount, 1, 2) != 2)
233 // Drop ref count to 0. No one can use StorDevice object.
234 static inline STORVSC_DEVICE* FinalReleaseStorDevice(DEVICE_OBJECT *Device)
236 STORVSC_DEVICE *storDevice;
238 storDevice = (STORVSC_DEVICE*)Device->Extension;
241 // Busy wait until the ref drop to 1, then set it to 0
242 while (InterlockedCompareExchange(&storDevice->RefCount, 0, 1) != 1)
247 Device->Extension = NULL;
263 DRIVER_OBJECT *Driver
266 STORVSC_DRIVER_OBJECT* storDriver = (STORVSC_DRIVER_OBJECT*)Driver;
269 DPRINT_ENTER(STORVSC);
271 DPRINT_DBG(STORVSC, "sizeof(STORVSC_REQUEST)=%d sizeof(STORVSC_REQUEST_EXTENSION)=%d sizeof(VSTOR_PACKET)=%d, sizeof(VMSCSI_REQUEST)=%d",
272 sizeof(STORVSC_REQUEST), sizeof(STORVSC_REQUEST_EXTENSION), sizeof(VSTOR_PACKET), sizeof(VMSCSI_REQUEST));
274 // Make sure we are at least 2 pages since 1 page is used for control
275 ASSERT(storDriver->RingBufferSize >= (PAGE_SIZE << 1));
277 Driver->name = gDriverName;
278 memcpy(&Driver->deviceType, &gStorVscDeviceType, sizeof(GUID));
280 storDriver->RequestExtSize = sizeof(STORVSC_REQUEST_EXTENSION);
282 // Divide the ring buffer data size (which is 1 page less than the ring buffer size since that page is reserved for the ring buffer indices)
283 // by the max request size (which is VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER + VSTOR_PACKET + u64)
284 storDriver->MaxOutstandingRequestsPerChannel =
285 ((storDriver->RingBufferSize - PAGE_SIZE) / ALIGN_UP(MAX_MULTIPAGE_BUFFER_PACKET + sizeof(VSTOR_PACKET) + sizeof(u64),sizeof(u64)));
287 DPRINT_INFO(STORVSC, "max io %u, currently %u\n", storDriver->MaxOutstandingRequestsPerChannel, STORVSC_MAX_IO_REQUESTS);
289 // Setup the dispatch table
290 storDriver->Base.OnDeviceAdd = StorVscOnDeviceAdd;
291 storDriver->Base.OnDeviceRemove = StorVscOnDeviceRemove;
292 storDriver->Base.OnCleanup = StorVscOnCleanup;
294 storDriver->OnIORequest = StorVscOnIORequest;
295 storDriver->OnHostReset = StorVscOnHostReset;
297 DPRINT_EXIT(STORVSC);
308 Callback when the device belonging to this driver is added
313 DEVICE_OBJECT *Device,
318 STORVSC_DEVICE *storDevice;
319 //VMSTORAGE_CHANNEL_PROPERTIES *props;
320 STORVSC_DEVICE_INFO *deviceInfo = (STORVSC_DEVICE_INFO*)AdditionalInfo;
322 DPRINT_ENTER(STORVSC);
324 storDevice = AllocStorDevice(Device);
331 // Save the channel properties to our storvsc channel
332 //props = (VMSTORAGE_CHANNEL_PROPERTIES*) channel->offerMsg.Offer.u.Standard.UserDefined;
335 // If we support more than 1 scsi channel, we need to set the port number here
336 // to the scsi channel but how do we get the scsi channel prior to the bus scan
337 /*storChannel->PortNumber = 0;
338 storChannel->PathId = props->PathId;
339 storChannel->TargetId = props->TargetId;*/
341 storDevice->PortNumber = deviceInfo->PortNumber;
343 ret = StorVscConnectToVsp(Device);
345 //deviceInfo->PortNumber = storDevice->PortNumber;
346 deviceInfo->PathId = storDevice->PathId;
347 deviceInfo->TargetId = storDevice->TargetId;
349 DPRINT_DBG(STORVSC, "assigned port %lu, path %u target %u\n", storDevice->PortNumber, storDevice->PathId, storDevice->TargetId);
352 DPRINT_EXIT(STORVSC);
357 static int StorVscChannelInit(DEVICE_OBJECT *Device)
360 STORVSC_DEVICE *storDevice;
361 STORVSC_REQUEST_EXTENSION *request;
362 VSTOR_PACKET *vstorPacket;
364 storDevice = GetStorDevice(Device);
367 DPRINT_ERR(STORVSC, "unable to get stor device...device being destroyed?");
368 DPRINT_EXIT(STORVSC);
372 request = &storDevice->InitRequest;
373 vstorPacket = &request->VStorPacket;
375 // Now, initiate the vsc/vsp initialization protocol on the open channel
377 memset(request, sizeof(STORVSC_REQUEST_EXTENSION), 0);
378 request->WaitEvent = WaitEventCreate();
380 vstorPacket->Operation = VStorOperationBeginInitialization;
381 vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
383 /*SpinlockAcquire(gDriverExt.packetListLock);
384 INSERT_TAIL_LIST(&gDriverExt.packetList, &packet->listEntry.entry);
385 SpinlockRelease(gDriverExt.packetListLock);*/
387 DPRINT_INFO(STORVSC, "BEGIN_INITIALIZATION_OPERATION...");
389 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
391 sizeof(VSTOR_PACKET),
392 (unsigned long)request,
393 VmbusPacketTypeDataInBand,
394 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
397 DPRINT_ERR(STORVSC, "unable to send BEGIN_INITIALIZATION_OPERATION");
401 WaitEventWait(request->WaitEvent);
403 if (vstorPacket->Operation != VStorOperationCompleteIo || vstorPacket->Status != 0)
405 DPRINT_ERR(STORVSC, "BEGIN_INITIALIZATION_OPERATION failed (op %d status 0x%lx)", vstorPacket->Operation, vstorPacket->Status);
409 DPRINT_INFO(STORVSC, "QUERY_PROTOCOL_VERSION_OPERATION...");
411 // reuse the packet for version range supported
412 memset(vstorPacket, sizeof(VSTOR_PACKET), 0);
413 vstorPacket->Operation = VStorOperationQueryProtocolVersion;
414 vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
416 vstorPacket->Version.MajorMinor = VMSTOR_PROTOCOL_VERSION_CURRENT;
417 FILL_VMSTOR_REVISION(vstorPacket->Version.Revision);
419 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
421 sizeof(VSTOR_PACKET),
422 (unsigned long)request,
423 VmbusPacketTypeDataInBand,
424 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
427 DPRINT_ERR(STORVSC, "unable to send BEGIN_INITIALIZATION_OPERATION");
431 WaitEventWait(request->WaitEvent);
433 // TODO: Check returned version
434 if (vstorPacket->Operation != VStorOperationCompleteIo || vstorPacket->Status != 0)
436 DPRINT_ERR(STORVSC, "QUERY_PROTOCOL_VERSION_OPERATION failed (op %d status 0x%lx)", vstorPacket->Operation, vstorPacket->Status);
440 // Query channel properties
441 DPRINT_INFO(STORVSC, "QUERY_PROPERTIES_OPERATION...");
443 memset(vstorPacket, sizeof(VSTOR_PACKET), 0);
444 vstorPacket->Operation = VStorOperationQueryProperties;
445 vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
446 vstorPacket->StorageChannelProperties.PortNumber = storDevice->PortNumber;
448 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
450 sizeof(VSTOR_PACKET),
451 (unsigned long)request,
452 VmbusPacketTypeDataInBand,
453 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
457 DPRINT_ERR(STORVSC, "unable to send QUERY_PROPERTIES_OPERATION");
461 WaitEventWait(request->WaitEvent);
463 // TODO: Check returned version
464 if (vstorPacket->Operation != VStorOperationCompleteIo || vstorPacket->Status != 0)
466 DPRINT_ERR(STORVSC, "QUERY_PROPERTIES_OPERATION failed (op %d status 0x%lx)", vstorPacket->Operation, vstorPacket->Status);
470 //storDevice->PortNumber = vstorPacket->StorageChannelProperties.PortNumber;
471 storDevice->PathId = vstorPacket->StorageChannelProperties.PathId;
472 storDevice->TargetId = vstorPacket->StorageChannelProperties.TargetId;
474 DPRINT_DBG(STORVSC, "channel flag 0x%lx, max xfer len 0x%lx", vstorPacket->StorageChannelProperties.Flags, vstorPacket->StorageChannelProperties.MaxTransferBytes);
476 DPRINT_INFO(STORVSC, "END_INITIALIZATION_OPERATION...");
478 memset(vstorPacket, sizeof(VSTOR_PACKET), 0);
479 vstorPacket->Operation = VStorOperationEndInitialization;
480 vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
482 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
484 sizeof(VSTOR_PACKET),
485 (unsigned long)request,
486 VmbusPacketTypeDataInBand,
487 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
491 DPRINT_ERR(STORVSC, "unable to send END_INITIALIZATION_OPERATION");
495 WaitEventWait(request->WaitEvent);
497 if (vstorPacket->Operation != VStorOperationCompleteIo || vstorPacket->Status != 0)
499 DPRINT_ERR(STORVSC, "END_INITIALIZATION_OPERATION failed (op %d status 0x%lx)", vstorPacket->Operation, vstorPacket->Status);
503 DPRINT_INFO(STORVSC, "**** storage channel up and running!! ****");
506 if (request->WaitEvent)
508 WaitEventClose(request->WaitEvent);
509 request->WaitEvent = NULL;
512 PutStorDevice(Device);
514 DPRINT_EXIT(STORVSC);
521 DEVICE_OBJECT *Device
525 VMSTORAGE_CHANNEL_PROPERTIES props;
527 STORVSC_DRIVER_OBJECT *storDriver = (STORVSC_DRIVER_OBJECT*) Device->Driver;;
529 memset(&props, sizeof(VMSTORAGE_CHANNEL_PROPERTIES), 0);
532 ret = Device->Driver->VmbusChannelInterface.Open(Device,
533 storDriver->RingBufferSize,
534 storDriver->RingBufferSize,
536 sizeof(VMSTORAGE_CHANNEL_PROPERTIES),
537 StorVscOnChannelCallback,
541 DPRINT_DBG(STORVSC, "storage props: path id %d, tgt id %d, max xfer %ld", props.PathId, props.TargetId, props.MaxTransferBytes);
545 DPRINT_ERR(STORVSC, "unable to open channel: %d", ret);
549 ret = StorVscChannelInit(Device);
558 StorVscOnDeviceRemove()
561 Callback when the our device is being removed
565 StorVscOnDeviceRemove(
566 DEVICE_OBJECT *Device
569 STORVSC_DEVICE *storDevice;
572 DPRINT_ENTER(STORVSC);
574 DPRINT_INFO(STORVSC, "disabling storage device (%p)...", Device->Extension);
576 storDevice = ReleaseStorDevice(Device);
578 // At this point, all outbound traffic should be disable. We only allow inbound traffic (responses) to proceed
579 // so that outstanding requests can be completed.
580 while (storDevice->NumOutstandingRequests)
582 DPRINT_INFO(STORVSC, "waiting for %d requests to complete...", storDevice->NumOutstandingRequests);
587 DPRINT_INFO(STORVSC, "removing storage device (%p)...", Device->Extension);
589 storDevice = FinalReleaseStorDevice(Device);
591 DPRINT_INFO(STORVSC, "storage device (%p) safe to remove", storDevice);
594 Device->Driver->VmbusChannelInterface.Close(Device);
596 FreeStorDevice(storDevice);
598 DPRINT_EXIT(STORVSC);
604 //StorVscOnTargetRescan(
608 // DEVICE_OBJECT *device=(DEVICE_OBJECT*)Context;
609 // STORVSC_DRIVER_OBJECT *storDriver;
611 // DPRINT_ENTER(STORVSC);
613 // storDriver = (STORVSC_DRIVER_OBJECT*) device->Driver;
614 // storDriver->OnHostRescan(device);
616 // DPRINT_EXIT(STORVSC);
621 DEVICE_OBJECT *Device
626 STORVSC_DEVICE *storDevice;
627 STORVSC_REQUEST_EXTENSION *request;
628 VSTOR_PACKET *vstorPacket;
630 DPRINT_ENTER(STORVSC);
632 DPRINT_INFO(STORVSC, "resetting host adapter...");
634 storDevice = GetStorDevice(Device);
637 DPRINT_ERR(STORVSC, "unable to get stor device...device being destroyed?");
638 DPRINT_EXIT(STORVSC);
642 request = &storDevice->ResetRequest;
643 vstorPacket = &request->VStorPacket;
645 request->WaitEvent = WaitEventCreate();
647 vstorPacket->Operation = VStorOperationResetBus;
648 vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
649 vstorPacket->VmSrb.PathId = storDevice->PathId;
651 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
653 sizeof(VSTOR_PACKET),
654 (unsigned long)&storDevice->ResetRequest,
655 VmbusPacketTypeDataInBand,
656 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
659 DPRINT_ERR(STORVSC, "Unable to send reset packet %p ret %d", vstorPacket, ret);
663 // FIXME: Add a timeout
664 WaitEventWait(request->WaitEvent);
666 WaitEventClose(request->WaitEvent);
667 DPRINT_INFO(STORVSC, "host adapter reset completed");
669 // At this point, all outstanding requests in the adapter should have been flushed out and return to us
672 PutStorDevice(Device);
673 DPRINT_EXIT(STORVSC);
683 Callback to initiate an I/O request
688 DEVICE_OBJECT *Device,
689 STORVSC_REQUEST *Request
692 STORVSC_DEVICE *storDevice;
693 STORVSC_REQUEST_EXTENSION* requestExtension = (STORVSC_REQUEST_EXTENSION*) Request->Extension;
694 VSTOR_PACKET* vstorPacket =&requestExtension->VStorPacket;
697 DPRINT_ENTER(STORVSC);
699 storDevice = GetStorDevice(Device);
701 DPRINT_DBG(STORVSC, "enter - Device %p, DeviceExt %p, Request %p, Extension %p",
702 Device, storDevice, Request, requestExtension);
704 DPRINT_DBG(STORVSC, "req %p len %d bus %d, target %d, lun %d cdblen %d",
705 Request, Request->DataBuffer.Length, Request->Bus, Request->TargetId, Request->LunId, Request->CdbLen);
709 DPRINT_ERR(STORVSC, "unable to get stor device...device being destroyed?");
710 DPRINT_EXIT(STORVSC);
714 /* print_hex_dump_bytes("", DUMP_PREFIX_NONE, Request->Cdb, Request->CdbLen); */
716 requestExtension->Request = Request;
717 requestExtension->Device = Device;
719 memset(vstorPacket, 0 , sizeof(VSTOR_PACKET));
721 vstorPacket->Flags |= REQUEST_COMPLETION_FLAG;
723 vstorPacket->VmSrb.Length = sizeof(VMSCSI_REQUEST);
725 vstorPacket->VmSrb.PortNumber = Request->Host;
726 vstorPacket->VmSrb.PathId = Request->Bus;
727 vstorPacket->VmSrb.TargetId = Request->TargetId;
728 vstorPacket->VmSrb.Lun = Request->LunId;
730 vstorPacket->VmSrb.SenseInfoLength = SENSE_BUFFER_SIZE;
732 // Copy over the scsi command descriptor block
733 vstorPacket->VmSrb.CdbLength = Request->CdbLen;
734 memcpy(&vstorPacket->VmSrb.Cdb, Request->Cdb, Request->CdbLen);
736 vstorPacket->VmSrb.DataIn = Request->Type;
737 vstorPacket->VmSrb.DataTransferLength = Request->DataBuffer.Length;
739 vstorPacket->Operation = VStorOperationExecuteSRB;
741 DPRINT_DBG(STORVSC, "srb - len %d port %d, path %d, target %d, lun %d senselen %d cdblen %d",
742 vstorPacket->VmSrb.Length,
743 vstorPacket->VmSrb.PortNumber,
744 vstorPacket->VmSrb.PathId,
745 vstorPacket->VmSrb.TargetId,
746 vstorPacket->VmSrb.Lun,
747 vstorPacket->VmSrb.SenseInfoLength,
748 vstorPacket->VmSrb.CdbLength);
750 if (requestExtension->Request->DataBuffer.Length)
752 ret = Device->Driver->VmbusChannelInterface.SendPacketMultiPageBuffer(Device,
753 &requestExtension->Request->DataBuffer,
755 sizeof(VSTOR_PACKET),
756 (unsigned long)requestExtension);
760 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
762 sizeof(VSTOR_PACKET),
763 (unsigned long)requestExtension,
764 VmbusPacketTypeDataInBand,
765 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
770 DPRINT_DBG(STORVSC, "Unable to send packet %p ret %d", vstorPacket, ret);
773 InterlockedIncrement(&storDevice->NumOutstandingRequests);
775 PutStorDevice(Device);
777 DPRINT_EXIT(STORVSC);
787 Perform any cleanup when the driver is removed
792 DRIVER_OBJECT *Driver
795 DPRINT_ENTER(STORVSC);
796 DPRINT_EXIT(STORVSC);
801 StorVscOnIOCompletion(
802 DEVICE_OBJECT *Device,
803 VSTOR_PACKET *VStorPacket,
804 STORVSC_REQUEST_EXTENSION *RequestExt
807 STORVSC_REQUEST *request;
808 STORVSC_DEVICE *storDevice;
810 DPRINT_ENTER(STORVSC);
812 storDevice = MustGetStorDevice(Device);
815 DPRINT_ERR(STORVSC, "unable to get stor device...device being destroyed?");
816 DPRINT_EXIT(STORVSC);
820 DPRINT_DBG(STORVSC, "IO_COMPLETE_OPERATION - request extension %p completed bytes xfer %lu",
821 RequestExt, VStorPacket->VmSrb.DataTransferLength);
823 ASSERT(RequestExt != NULL);
824 ASSERT(RequestExt->Request != NULL);
826 request = RequestExt->Request;
828 ASSERT(request->OnIOCompletion != NULL);
830 // Copy over the status...etc
831 request->Status = VStorPacket->VmSrb.ScsiStatus;
833 if (request->Status != 0 || VStorPacket->VmSrb.SrbStatus != 1)
835 DPRINT_WARN(STORVSC, "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
837 VStorPacket->VmSrb.ScsiStatus,
838 VStorPacket->VmSrb.SrbStatus);
841 if ((request->Status & 0xFF) == 0x02) // CHECK_CONDITION
843 if (VStorPacket->VmSrb.SrbStatus & 0x80) // autosense data available
845 DPRINT_WARN(STORVSC, "storvsc pkt %p autosense data valid - len %d\n",
846 RequestExt, VStorPacket->VmSrb.SenseInfoLength);
848 ASSERT(VStorPacket->VmSrb.SenseInfoLength <= request->SenseBufferSize);
849 memcpy(request->SenseBuffer,
850 VStorPacket->VmSrb.SenseData,
851 VStorPacket->VmSrb.SenseInfoLength);
853 request->SenseBufferSize = VStorPacket->VmSrb.SenseInfoLength;
858 request->BytesXfer = VStorPacket->VmSrb.DataTransferLength;
860 request->OnIOCompletion(request);
862 InterlockedDecrement(&storDevice->NumOutstandingRequests);
864 PutStorDevice(Device);
866 DPRINT_EXIT(STORVSC);
872 DEVICE_OBJECT *Device,
873 VSTOR_PACKET *VStorPacket,
874 STORVSC_REQUEST_EXTENSION *RequestExt
877 switch(VStorPacket->Operation)
879 case VStorOperationCompleteIo:
881 DPRINT_DBG(STORVSC, "IO_COMPLETE_OPERATION");
882 StorVscOnIOCompletion(Device, VStorPacket, RequestExt);
885 //case ENUMERATE_DEVICE_OPERATION:
887 // DPRINT_INFO(STORVSC, "ENUMERATE_DEVICE_OPERATION");
889 // StorVscOnTargetRescan(Device);
892 case VStorOperationRemoveDevice:
894 DPRINT_INFO(STORVSC, "REMOVE_DEVICE_OPERATION");
899 DPRINT_INFO(STORVSC, "Unknown operation received - %d", VStorPacket->Operation);
905 StorVscOnChannelCallback(
910 DEVICE_OBJECT *device = (DEVICE_OBJECT*)Context;
911 STORVSC_DEVICE *storDevice;
914 unsigned char packet[ALIGN_UP(sizeof(VSTOR_PACKET),8)];
915 STORVSC_REQUEST_EXTENSION *request;
917 DPRINT_ENTER(STORVSC);
921 storDevice = MustGetStorDevice(device);
924 DPRINT_ERR(STORVSC, "unable to get stor device...device being destroyed?");
925 DPRINT_EXIT(STORVSC);
931 ret = device->Driver->VmbusChannelInterface.RecvPacket(device,
933 ALIGN_UP(sizeof(VSTOR_PACKET),8),
936 if (ret == 0 && bytesRecvd > 0)
938 DPRINT_DBG(STORVSC, "receive %d bytes - tid %llx", bytesRecvd, requestId);
940 //ASSERT(bytesRecvd == sizeof(VSTOR_PACKET));
942 request = (STORVSC_REQUEST_EXTENSION*)(unsigned long)requestId;
945 //if (vstorPacket.Flags & SYNTHETIC_FLAG)
946 if ((request == &storDevice->InitRequest) || (request == &storDevice->ResetRequest))
948 //DPRINT_INFO(STORVSC, "reset completion - operation %u status %u", vstorPacket.Operation, vstorPacket.Status);
950 memcpy(&request->VStorPacket, packet, sizeof(VSTOR_PACKET));
952 WaitEventSet(request->WaitEvent);
956 StorVscOnReceive(device, (VSTOR_PACKET*)packet, request);
961 //DPRINT_DBG(STORVSC, "nothing else to read...");
966 PutStorDevice(device);
968 DPRINT_EXIT(STORVSC);