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>
29 #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 struct hv_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 atomic_t RefCount
; /* 0 indicates the device is being destroyed */
62 atomic_t 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 struct hv_storvsc_request
*Request
116 struct hv_device
*Device
121 struct hv_driver
*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 atomic_cmpxchg(&storDevice
->RefCount
, 0, 2);
160 storDevice
->Device
= Device
;
161 Device
->Extension
= storDevice
;
166 static inline void FreeStorDevice(STORVSC_DEVICE
*Device
)
168 ASSERT( atomic_read(&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
&& atomic_read(&storDevice
->RefCount
) > 1)
179 atomic_inc(&storDevice
->RefCount
);
186 /* Get the stordevice object iff exists and its refcount > 0 */
187 static inline STORVSC_DEVICE
* MustGetStorDevice(struct hv_device
*Device
)
189 STORVSC_DEVICE
*storDevice
;
191 storDevice
= (STORVSC_DEVICE
*)Device
->Extension
;
192 if (storDevice
&& atomic_read(&storDevice
->RefCount
))
193 atomic_inc(&storDevice
->RefCount
);
200 static inline void PutStorDevice(struct hv_device
*Device
)
202 STORVSC_DEVICE
*storDevice
;
204 storDevice
= (STORVSC_DEVICE
*)Device
->Extension
;
207 atomic_dec(&storDevice
->RefCount
);
208 ASSERT(atomic_read(&storDevice
->RefCount
));
211 /* Drop ref count to 1 to effectively disable GetStorDevice() */
212 static inline STORVSC_DEVICE
* ReleaseStorDevice(struct hv_device
*Device
)
214 STORVSC_DEVICE
*storDevice
;
216 storDevice
= (STORVSC_DEVICE
*)Device
->Extension
;
219 /* Busy wait until the ref drop to 2, then set it to 1 */
220 while (atomic_cmpxchg(&storDevice
->RefCount
, 2, 1) != 2)
228 /* Drop ref count to 0. No one can use StorDevice object. */
229 static inline STORVSC_DEVICE
* FinalReleaseStorDevice(struct hv_device
*Device
)
231 STORVSC_DEVICE
*storDevice
;
233 storDevice
= (STORVSC_DEVICE
*)Device
->Extension
;
236 /* Busy wait until the ref drop to 1, then set it to 0 */
237 while (atomic_cmpxchg(&storDevice
->RefCount
, 1, 0) != 1)
242 Device
->Extension
= NULL
;
258 struct hv_driver
*Driver
261 STORVSC_DRIVER_OBJECT
* storDriver
= (STORVSC_DRIVER_OBJECT
*)Driver
;
264 DPRINT_ENTER(STORVSC
);
266 DPRINT_DBG(STORVSC
, "sizeof(STORVSC_REQUEST)=%zd sizeof(STORVSC_REQUEST_EXTENSION)=%zd sizeof(VSTOR_PACKET)=%zd, sizeof(VMSCSI_REQUEST)=%zd",
267 sizeof(struct hv_storvsc_request
), sizeof(STORVSC_REQUEST_EXTENSION
), sizeof(VSTOR_PACKET
), sizeof(VMSCSI_REQUEST
));
269 /* Make sure we are at least 2 pages since 1 page is used for control */
270 ASSERT(storDriver
->RingBufferSize
>= (PAGE_SIZE
<< 1));
272 Driver
->name
= gDriverName
;
273 memcpy(&Driver
->deviceType
, &gStorVscDeviceType
, sizeof(GUID
));
275 storDriver
->RequestExtSize
= sizeof(STORVSC_REQUEST_EXTENSION
);
278 * Divide the ring buffer data size (which is 1 page less
279 * than the ring buffer size since that page is reserved for
280 * the ring buffer indices) by the max request size (which is
281 * VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER + VSTOR_PACKET + u64)
283 storDriver
->MaxOutstandingRequestsPerChannel
=
284 ((storDriver
->RingBufferSize
- PAGE_SIZE
) / ALIGN_UP(MAX_MULTIPAGE_BUFFER_PACKET
+ sizeof(VSTOR_PACKET
) + sizeof(u64
),sizeof(u64
)));
286 DPRINT_INFO(STORVSC
, "max io %u, currently %u\n", storDriver
->MaxOutstandingRequestsPerChannel
, STORVSC_MAX_IO_REQUESTS
);
288 /* Setup the dispatch table */
289 storDriver
->Base
.OnDeviceAdd
= StorVscOnDeviceAdd
;
290 storDriver
->Base
.OnDeviceRemove
= StorVscOnDeviceRemove
;
291 storDriver
->Base
.OnCleanup
= StorVscOnCleanup
;
293 storDriver
->OnIORequest
= StorVscOnIORequest
;
294 storDriver
->OnHostReset
= StorVscOnHostReset
;
296 DPRINT_EXIT(STORVSC
);
307 Callback when the device belonging to this driver is added
312 struct hv_device
*Device
,
317 STORVSC_DEVICE
*storDevice
;
318 /* VMSTORAGE_CHANNEL_PROPERTIES *props; */
319 STORVSC_DEVICE_INFO
*deviceInfo
= (STORVSC_DEVICE_INFO
*)AdditionalInfo
;
321 DPRINT_ENTER(STORVSC
);
323 storDevice
= AllocStorDevice(Device
);
330 /* Save the channel properties to our storvsc channel */
331 /* props = (VMSTORAGE_CHANNEL_PROPERTIES*) channel->offerMsg.Offer.u.Standard.UserDefined; */
335 * If we support more than 1 scsi channel, we need to set the
336 * port number here to the scsi channel but how do we get the
337 * scsi channel prior to the bus scan
340 /* storChannel->PortNumber = 0;
341 storChannel->PathId = props->PathId;
342 storChannel->TargetId = props->TargetId; */
344 storDevice
->PortNumber
= deviceInfo
->PortNumber
;
345 /* Send it back up */
346 ret
= StorVscConnectToVsp(Device
);
348 /* deviceInfo->PortNumber = storDevice->PortNumber; */
349 deviceInfo
->PathId
= storDevice
->PathId
;
350 deviceInfo
->TargetId
= storDevice
->TargetId
;
352 DPRINT_DBG(STORVSC
, "assigned port %u, path %u target %u\n", storDevice
->PortNumber
, storDevice
->PathId
, storDevice
->TargetId
);
355 DPRINT_EXIT(STORVSC
);
360 static int StorVscChannelInit(struct hv_device
*Device
)
363 STORVSC_DEVICE
*storDevice
;
364 STORVSC_REQUEST_EXTENSION
*request
;
365 VSTOR_PACKET
*vstorPacket
;
367 storDevice
= GetStorDevice(Device
);
370 DPRINT_ERR(STORVSC
, "unable to get stor device...device being destroyed?");
371 DPRINT_EXIT(STORVSC
);
375 request
= &storDevice
->InitRequest
;
376 vstorPacket
= &request
->VStorPacket
;
378 /* Now, initiate the vsc/vsp initialization protocol on the open channel */
380 memset(request
, sizeof(STORVSC_REQUEST_EXTENSION
), 0);
381 request
->WaitEvent
= osd_WaitEventCreate();
383 vstorPacket
->Operation
= VStorOperationBeginInitialization
;
384 vstorPacket
->Flags
= REQUEST_COMPLETION_FLAG
;
386 /*SpinlockAcquire(gDriverExt.packetListLock);
387 INSERT_TAIL_LIST(&gDriverExt.packetList, &packet->listEntry.entry);
388 SpinlockRelease(gDriverExt.packetListLock);*/
390 DPRINT_INFO(STORVSC
, "BEGIN_INITIALIZATION_OPERATION...");
392 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
394 sizeof(VSTOR_PACKET
),
395 (unsigned long)request
,
396 VmbusPacketTypeDataInBand
,
397 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
400 DPRINT_ERR(STORVSC
, "unable to send BEGIN_INITIALIZATION_OPERATION");
404 osd_WaitEventWait(request
->WaitEvent
);
406 if (vstorPacket
->Operation
!= VStorOperationCompleteIo
|| vstorPacket
->Status
!= 0)
408 DPRINT_ERR(STORVSC
, "BEGIN_INITIALIZATION_OPERATION failed (op %d status 0x%x)", vstorPacket
->Operation
, vstorPacket
->Status
);
412 DPRINT_INFO(STORVSC
, "QUERY_PROTOCOL_VERSION_OPERATION...");
414 /* reuse the packet for version range supported */
415 memset(vstorPacket
, sizeof(VSTOR_PACKET
), 0);
416 vstorPacket
->Operation
= VStorOperationQueryProtocolVersion
;
417 vstorPacket
->Flags
= REQUEST_COMPLETION_FLAG
;
419 vstorPacket
->Version
.MajorMinor
= VMSTOR_PROTOCOL_VERSION_CURRENT
;
420 FILL_VMSTOR_REVISION(vstorPacket
->Version
.Revision
);
422 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
424 sizeof(VSTOR_PACKET
),
425 (unsigned long)request
,
426 VmbusPacketTypeDataInBand
,
427 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
430 DPRINT_ERR(STORVSC
, "unable to send BEGIN_INITIALIZATION_OPERATION");
434 osd_WaitEventWait(request
->WaitEvent
);
436 /* TODO: Check returned version */
437 if (vstorPacket
->Operation
!= VStorOperationCompleteIo
|| vstorPacket
->Status
!= 0)
439 DPRINT_ERR(STORVSC
, "QUERY_PROTOCOL_VERSION_OPERATION failed (op %d status 0x%x)", vstorPacket
->Operation
, vstorPacket
->Status
);
443 /* Query channel properties */
444 DPRINT_INFO(STORVSC
, "QUERY_PROPERTIES_OPERATION...");
446 memset(vstorPacket
, sizeof(VSTOR_PACKET
), 0);
447 vstorPacket
->Operation
= VStorOperationQueryProperties
;
448 vstorPacket
->Flags
= REQUEST_COMPLETION_FLAG
;
449 vstorPacket
->StorageChannelProperties
.PortNumber
= storDevice
->PortNumber
;
451 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
453 sizeof(VSTOR_PACKET
),
454 (unsigned long)request
,
455 VmbusPacketTypeDataInBand
,
456 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
460 DPRINT_ERR(STORVSC
, "unable to send QUERY_PROPERTIES_OPERATION");
464 osd_WaitEventWait(request
->WaitEvent
);
466 /* TODO: Check returned version */
467 if (vstorPacket
->Operation
!= VStorOperationCompleteIo
|| vstorPacket
->Status
!= 0)
469 DPRINT_ERR(STORVSC
, "QUERY_PROPERTIES_OPERATION failed (op %d status 0x%x)", vstorPacket
->Operation
, vstorPacket
->Status
);
473 /* storDevice->PortNumber = vstorPacket->StorageChannelProperties.PortNumber; */
474 storDevice
->PathId
= vstorPacket
->StorageChannelProperties
.PathId
;
475 storDevice
->TargetId
= vstorPacket
->StorageChannelProperties
.TargetId
;
477 DPRINT_DBG(STORVSC
, "channel flag 0x%x, max xfer len 0x%x", vstorPacket
->StorageChannelProperties
.Flags
, vstorPacket
->StorageChannelProperties
.MaxTransferBytes
);
479 DPRINT_INFO(STORVSC
, "END_INITIALIZATION_OPERATION...");
481 memset(vstorPacket
, sizeof(VSTOR_PACKET
), 0);
482 vstorPacket
->Operation
= VStorOperationEndInitialization
;
483 vstorPacket
->Flags
= REQUEST_COMPLETION_FLAG
;
485 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
487 sizeof(VSTOR_PACKET
),
488 (unsigned long)request
,
489 VmbusPacketTypeDataInBand
,
490 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
494 DPRINT_ERR(STORVSC
, "unable to send END_INITIALIZATION_OPERATION");
498 osd_WaitEventWait(request
->WaitEvent
);
500 if (vstorPacket
->Operation
!= VStorOperationCompleteIo
|| vstorPacket
->Status
!= 0)
502 DPRINT_ERR(STORVSC
, "END_INITIALIZATION_OPERATION failed (op %d status 0x%x)", vstorPacket
->Operation
, vstorPacket
->Status
);
506 DPRINT_INFO(STORVSC
, "**** storage channel up and running!! ****");
509 if (request
->WaitEvent
)
511 kfree(request
->WaitEvent
);
512 request
->WaitEvent
= NULL
;
515 PutStorDevice(Device
);
517 DPRINT_EXIT(STORVSC
);
524 struct hv_device
*Device
528 VMSTORAGE_CHANNEL_PROPERTIES props
;
530 STORVSC_DRIVER_OBJECT
*storDriver
= (STORVSC_DRIVER_OBJECT
*) Device
->Driver
;;
532 memset(&props
, sizeof(VMSTORAGE_CHANNEL_PROPERTIES
), 0);
534 /* Open the channel */
535 ret
= Device
->Driver
->VmbusChannelInterface
.Open(Device
,
536 storDriver
->RingBufferSize
,
537 storDriver
->RingBufferSize
,
539 sizeof(VMSTORAGE_CHANNEL_PROPERTIES
),
540 StorVscOnChannelCallback
,
544 DPRINT_DBG(STORVSC
, "storage props: path id %d, tgt id %d, max xfer %d", props
.PathId
, props
.TargetId
, props
.MaxTransferBytes
);
548 DPRINT_ERR(STORVSC
, "unable to open channel: %d", ret
);
552 ret
= StorVscChannelInit(Device
);
561 StorVscOnDeviceRemove()
564 Callback when the our device is being removed
568 StorVscOnDeviceRemove(
569 struct hv_device
*Device
572 STORVSC_DEVICE
*storDevice
;
575 DPRINT_ENTER(STORVSC
);
577 DPRINT_INFO(STORVSC
, "disabling storage device (%p)...", Device
->Extension
);
579 storDevice
= ReleaseStorDevice(Device
);
582 * At this point, all outbound traffic should be disable. We
583 * only allow inbound traffic (responses) to proceed so that
584 * outstanding requests can be completed.
586 while (atomic_read(&storDevice
->NumOutstandingRequests
))
588 DPRINT_INFO(STORVSC
, "waiting for %d requests to complete...", atomic_read(&storDevice
->NumOutstandingRequests
));
593 DPRINT_INFO(STORVSC
, "removing storage device (%p)...", Device
->Extension
);
595 storDevice
= FinalReleaseStorDevice(Device
);
597 DPRINT_INFO(STORVSC
, "storage device (%p) safe to remove", storDevice
);
599 /* Close the channel */
600 Device
->Driver
->VmbusChannelInterface
.Close(Device
);
602 FreeStorDevice(storDevice
);
604 DPRINT_EXIT(STORVSC
);
610 StorVscOnTargetRescan(
614 struct hv_device *device=(struct hv_device *)Context;
615 STORVSC_DRIVER_OBJECT *storDriver;
617 DPRINT_ENTER(STORVSC);
619 storDriver = (STORVSC_DRIVER_OBJECT*) device->Driver;
620 storDriver->OnHostRescan(device);
622 DPRINT_EXIT(STORVSC);
628 struct hv_device
*Device
633 STORVSC_DEVICE
*storDevice
;
634 STORVSC_REQUEST_EXTENSION
*request
;
635 VSTOR_PACKET
*vstorPacket
;
637 DPRINT_ENTER(STORVSC
);
639 DPRINT_INFO(STORVSC
, "resetting host adapter...");
641 storDevice
= GetStorDevice(Device
);
644 DPRINT_ERR(STORVSC
, "unable to get stor device...device being destroyed?");
645 DPRINT_EXIT(STORVSC
);
649 request
= &storDevice
->ResetRequest
;
650 vstorPacket
= &request
->VStorPacket
;
652 request
->WaitEvent
= osd_WaitEventCreate();
654 vstorPacket
->Operation
= VStorOperationResetBus
;
655 vstorPacket
->Flags
= REQUEST_COMPLETION_FLAG
;
656 vstorPacket
->VmSrb
.PathId
= storDevice
->PathId
;
658 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
660 sizeof(VSTOR_PACKET
),
661 (unsigned long)&storDevice
->ResetRequest
,
662 VmbusPacketTypeDataInBand
,
663 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
666 DPRINT_ERR(STORVSC
, "Unable to send reset packet %p ret %d", vstorPacket
, ret
);
670 /* FIXME: Add a timeout */
671 osd_WaitEventWait(request
->WaitEvent
);
673 kfree(request
->WaitEvent
);
674 DPRINT_INFO(STORVSC
, "host adapter reset completed");
677 * At this point, all outstanding requests in the adapter
678 * should have been flushed out and return to us
682 PutStorDevice(Device
);
683 DPRINT_EXIT(STORVSC
);
693 Callback to initiate an I/O request
698 struct hv_device
*Device
,
699 struct hv_storvsc_request
*Request
702 STORVSC_DEVICE
*storDevice
;
703 STORVSC_REQUEST_EXTENSION
* requestExtension
= (STORVSC_REQUEST_EXTENSION
*) Request
->Extension
;
704 VSTOR_PACKET
* vstorPacket
=&requestExtension
->VStorPacket
;
707 DPRINT_ENTER(STORVSC
);
709 storDevice
= GetStorDevice(Device
);
711 DPRINT_DBG(STORVSC
, "enter - Device %p, DeviceExt %p, Request %p, Extension %p",
712 Device
, storDevice
, Request
, requestExtension
);
714 DPRINT_DBG(STORVSC
, "req %p len %d bus %d, target %d, lun %d cdblen %d",
715 Request
, Request
->DataBuffer
.Length
, Request
->Bus
, Request
->TargetId
, Request
->LunId
, Request
->CdbLen
);
719 DPRINT_ERR(STORVSC
, "unable to get stor device...device being destroyed?");
720 DPRINT_EXIT(STORVSC
);
724 /* print_hex_dump_bytes("", DUMP_PREFIX_NONE, Request->Cdb, Request->CdbLen); */
726 requestExtension
->Request
= Request
;
727 requestExtension
->Device
= Device
;
729 memset(vstorPacket
, 0 , sizeof(VSTOR_PACKET
));
731 vstorPacket
->Flags
|= REQUEST_COMPLETION_FLAG
;
733 vstorPacket
->VmSrb
.Length
= sizeof(VMSCSI_REQUEST
);
735 vstorPacket
->VmSrb
.PortNumber
= Request
->Host
;
736 vstorPacket
->VmSrb
.PathId
= Request
->Bus
;
737 vstorPacket
->VmSrb
.TargetId
= Request
->TargetId
;
738 vstorPacket
->VmSrb
.Lun
= Request
->LunId
;
740 vstorPacket
->VmSrb
.SenseInfoLength
= SENSE_BUFFER_SIZE
;
742 /* Copy over the scsi command descriptor block */
743 vstorPacket
->VmSrb
.CdbLength
= Request
->CdbLen
;
744 memcpy(&vstorPacket
->VmSrb
.Cdb
, Request
->Cdb
, Request
->CdbLen
);
746 vstorPacket
->VmSrb
.DataIn
= Request
->Type
;
747 vstorPacket
->VmSrb
.DataTransferLength
= Request
->DataBuffer
.Length
;
749 vstorPacket
->Operation
= VStorOperationExecuteSRB
;
751 DPRINT_DBG(STORVSC
, "srb - len %d port %d, path %d, target %d, lun %d senselen %d cdblen %d",
752 vstorPacket
->VmSrb
.Length
,
753 vstorPacket
->VmSrb
.PortNumber
,
754 vstorPacket
->VmSrb
.PathId
,
755 vstorPacket
->VmSrb
.TargetId
,
756 vstorPacket
->VmSrb
.Lun
,
757 vstorPacket
->VmSrb
.SenseInfoLength
,
758 vstorPacket
->VmSrb
.CdbLength
);
760 if (requestExtension
->Request
->DataBuffer
.Length
)
762 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacketMultiPageBuffer(Device
,
763 &requestExtension
->Request
->DataBuffer
,
765 sizeof(VSTOR_PACKET
),
766 (unsigned long)requestExtension
);
770 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
772 sizeof(VSTOR_PACKET
),
773 (unsigned long)requestExtension
,
774 VmbusPacketTypeDataInBand
,
775 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
780 DPRINT_DBG(STORVSC
, "Unable to send packet %p ret %d", vstorPacket
, ret
);
783 atomic_inc(&storDevice
->NumOutstandingRequests
);
785 PutStorDevice(Device
);
787 DPRINT_EXIT(STORVSC
);
797 Perform any cleanup when the driver is removed
802 struct hv_driver
*Driver
805 DPRINT_ENTER(STORVSC
);
806 DPRINT_EXIT(STORVSC
);
811 StorVscOnIOCompletion(
812 struct hv_device
*Device
,
813 VSTOR_PACKET
*VStorPacket
,
814 STORVSC_REQUEST_EXTENSION
*RequestExt
817 struct hv_storvsc_request
*request
;
818 STORVSC_DEVICE
*storDevice
;
820 DPRINT_ENTER(STORVSC
);
822 storDevice
= MustGetStorDevice(Device
);
825 DPRINT_ERR(STORVSC
, "unable to get stor device...device being destroyed?");
826 DPRINT_EXIT(STORVSC
);
830 DPRINT_DBG(STORVSC
, "IO_COMPLETE_OPERATION - request extension %p completed bytes xfer %u",
831 RequestExt
, VStorPacket
->VmSrb
.DataTransferLength
);
833 ASSERT(RequestExt
!= NULL
);
834 ASSERT(RequestExt
->Request
!= NULL
);
836 request
= RequestExt
->Request
;
838 ASSERT(request
->OnIOCompletion
!= NULL
);
840 /* Copy over the status...etc */
841 request
->Status
= VStorPacket
->VmSrb
.ScsiStatus
;
843 if (request
->Status
!= 0 || VStorPacket
->VmSrb
.SrbStatus
!= 1)
845 DPRINT_WARN(STORVSC
, "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
847 VStorPacket
->VmSrb
.ScsiStatus
,
848 VStorPacket
->VmSrb
.SrbStatus
);
851 if ((request
->Status
& 0xFF) == 0x02) /* CHECK_CONDITION */
853 if (VStorPacket
->VmSrb
.SrbStatus
& 0x80) /* autosense data available */
855 DPRINT_WARN(STORVSC
, "storvsc pkt %p autosense data valid - len %d\n",
856 RequestExt
, VStorPacket
->VmSrb
.SenseInfoLength
);
858 ASSERT(VStorPacket
->VmSrb
.SenseInfoLength
<= request
->SenseBufferSize
);
859 memcpy(request
->SenseBuffer
,
860 VStorPacket
->VmSrb
.SenseData
,
861 VStorPacket
->VmSrb
.SenseInfoLength
);
863 request
->SenseBufferSize
= VStorPacket
->VmSrb
.SenseInfoLength
;
868 request
->BytesXfer
= VStorPacket
->VmSrb
.DataTransferLength
;
870 request
->OnIOCompletion(request
);
872 atomic_dec(&storDevice
->NumOutstandingRequests
);
874 PutStorDevice(Device
);
876 DPRINT_EXIT(STORVSC
);
882 struct hv_device
*Device
,
883 VSTOR_PACKET
*VStorPacket
,
884 STORVSC_REQUEST_EXTENSION
*RequestExt
887 switch(VStorPacket
->Operation
)
889 case VStorOperationCompleteIo
:
891 DPRINT_DBG(STORVSC
, "IO_COMPLETE_OPERATION");
892 StorVscOnIOCompletion(Device
, VStorPacket
, RequestExt
);
895 /* case ENUMERATE_DEVICE_OPERATION: */
897 /* DPRINT_INFO(STORVSC, "ENUMERATE_DEVICE_OPERATION"); */
899 /* StorVscOnTargetRescan(Device); */
902 case VStorOperationRemoveDevice
:
904 DPRINT_INFO(STORVSC
, "REMOVE_DEVICE_OPERATION");
909 DPRINT_INFO(STORVSC
, "Unknown operation received - %d", VStorPacket
->Operation
);
915 StorVscOnChannelCallback(
920 struct hv_device
*device
= (struct hv_device
*)Context
;
921 STORVSC_DEVICE
*storDevice
;
924 unsigned char packet
[ALIGN_UP(sizeof(VSTOR_PACKET
),8)];
925 STORVSC_REQUEST_EXTENSION
*request
;
927 DPRINT_ENTER(STORVSC
);
931 storDevice
= MustGetStorDevice(device
);
934 DPRINT_ERR(STORVSC
, "unable to get stor device...device being destroyed?");
935 DPRINT_EXIT(STORVSC
);
941 ret
= device
->Driver
->VmbusChannelInterface
.RecvPacket(device
,
943 ALIGN_UP(sizeof(VSTOR_PACKET
),8),
946 if (ret
== 0 && bytesRecvd
> 0)
948 DPRINT_DBG(STORVSC
, "receive %d bytes - tid %llx", bytesRecvd
, requestId
);
950 /* ASSERT(bytesRecvd == sizeof(VSTOR_PACKET)); */
952 request
= (STORVSC_REQUEST_EXTENSION
*)(unsigned long)requestId
;
955 /* if (vstorPacket.Flags & SYNTHETIC_FLAG) */
956 if ((request
== &storDevice
->InitRequest
) || (request
== &storDevice
->ResetRequest
))
958 /* DPRINT_INFO(STORVSC, "reset completion - operation %u status %u", vstorPacket.Operation, vstorPacket.Status); */
960 memcpy(&request
->VStorPacket
, packet
, sizeof(VSTOR_PACKET
));
962 osd_WaitEventSet(request
->WaitEvent
);
966 StorVscOnReceive(device
, (VSTOR_PACKET
*)packet
, request
);
971 /* DPRINT_DBG(STORVSC, "nothing else to read..."); */
976 PutStorDevice(device
);
978 DPRINT_EXIT(STORVSC
);