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>
24 #include <linux/delay.h>
27 #include "StorVscApi.h"
28 #include "VmbusPacketFormat.h"
32 struct storvsc_request_extension
{
33 /* LIST_ENTRY ListEntry; */
35 struct hv_storvsc_request
*Request
;
36 struct hv_device
*Device
;
38 /* Synchronize the request/response if needed */
39 struct osd_waitevent
*WaitEvent
;
41 struct vstor_packet VStorPacket
;
44 /* A storvsc device is a device object that contains a vmbus channel */
45 struct storvsc_device
{
46 struct hv_device
*Device
;
48 /* 0 indicates the device is being destroyed */
51 atomic_t NumOutstandingRequests
;
54 * Each unique Port/Path/Target represents 1 channel ie scsi
55 * controller. In reality, the pathid, targetid is always 0
56 * and the port is set by us
58 unsigned int PortNumber
;
60 unsigned char TargetId
;
62 /* LIST_ENTRY OutstandingRequestList; */
63 /* HANDLE OutstandingRequestLock; */
65 /* Used for vsc/vsp channel reset process */
66 struct storvsc_request_extension InitRequest
;
67 struct storvsc_request_extension ResetRequest
;
71 static const char *gDriverName
= "storvsc";
73 /* {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f} */
74 static const struct hv_guid gStorVscDeviceType
= {
76 0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
77 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f
82 static int StorVscOnDeviceAdd(struct hv_device
*Device
, void *AdditionalInfo
);
83 static int StorVscOnDeviceRemove(struct hv_device
*Device
);
84 static int StorVscOnIORequest(struct hv_device
*Device
,
85 struct hv_storvsc_request
*Request
);
86 static int StorVscOnHostReset(struct hv_device
*Device
);
87 static void StorVscOnCleanup(struct hv_driver
*Device
);
88 static void StorVscOnChannelCallback(void *Context
);
89 static void StorVscOnIOCompletion(struct hv_device
*Device
,
90 struct vstor_packet
*VStorPacket
,
91 struct storvsc_request_extension
*RequestExt
);
92 static void StorVscOnReceive(struct hv_device
*Device
,
93 struct vstor_packet
*VStorPacket
,
94 struct storvsc_request_extension
*RequestExt
);
95 static int StorVscConnectToVsp(struct hv_device
*Device
);
97 static inline struct storvsc_device
*AllocStorDevice(struct hv_device
*Device
)
99 struct storvsc_device
*storDevice
;
101 storDevice
= kzalloc(sizeof(struct storvsc_device
), GFP_KERNEL
);
105 /* Set to 2 to allow both inbound and outbound traffics */
106 /* (ie GetStorDevice() and MustGetStorDevice()) to proceed. */
107 atomic_cmpxchg(&storDevice
->RefCount
, 0, 2);
109 storDevice
->Device
= Device
;
110 Device
->Extension
= storDevice
;
115 static inline void FreeStorDevice(struct storvsc_device
*Device
)
117 ASSERT(atomic_read(&Device
->RefCount
) == 0);
121 /* Get the stordevice object iff exists and its refcount > 1 */
122 static inline struct storvsc_device
*GetStorDevice(struct hv_device
*Device
)
124 struct storvsc_device
*storDevice
;
126 storDevice
= (struct storvsc_device
*)Device
->Extension
;
127 if (storDevice
&& atomic_read(&storDevice
->RefCount
) > 1)
128 atomic_inc(&storDevice
->RefCount
);
135 /* Get the stordevice object iff exists and its refcount > 0 */
136 static inline struct storvsc_device
*MustGetStorDevice(struct hv_device
*Device
)
138 struct storvsc_device
*storDevice
;
140 storDevice
= (struct storvsc_device
*)Device
->Extension
;
141 if (storDevice
&& atomic_read(&storDevice
->RefCount
))
142 atomic_inc(&storDevice
->RefCount
);
149 static inline void PutStorDevice(struct hv_device
*Device
)
151 struct storvsc_device
*storDevice
;
153 storDevice
= (struct storvsc_device
*)Device
->Extension
;
156 atomic_dec(&storDevice
->RefCount
);
157 ASSERT(atomic_read(&storDevice
->RefCount
));
160 /* Drop ref count to 1 to effectively disable GetStorDevice() */
161 static inline struct storvsc_device
*ReleaseStorDevice(struct hv_device
*Device
)
163 struct storvsc_device
*storDevice
;
165 storDevice
= (struct storvsc_device
*)Device
->Extension
;
168 /* Busy wait until the ref drop to 2, then set it to 1 */
169 while (atomic_cmpxchg(&storDevice
->RefCount
, 2, 1) != 2)
175 /* Drop ref count to 0. No one can use StorDevice object. */
176 static inline struct storvsc_device
*FinalReleaseStorDevice(
177 struct hv_device
*Device
)
179 struct storvsc_device
*storDevice
;
181 storDevice
= (struct storvsc_device
*)Device
->Extension
;
184 /* Busy wait until the ref drop to 1, then set it to 0 */
185 while (atomic_cmpxchg(&storDevice
->RefCount
, 1, 0) != 1)
188 Device
->Extension
= NULL
;
193 * StorVscInitialize - Main entry point
195 int StorVscInitialize(struct hv_driver
*Driver
)
197 struct storvsc_driver_object
*storDriver
;
199 DPRINT_ENTER(STORVSC
);
201 storDriver
= (struct storvsc_driver_object
*)Driver
;
203 DPRINT_DBG(STORVSC
, "sizeof(STORVSC_REQUEST)=%zd "
204 "sizeof(struct storvsc_request_extension)=%zd "
205 "sizeof(struct vstor_packet)=%zd, "
206 "sizeof(struct vmscsi_request)=%zd",
207 sizeof(struct hv_storvsc_request
),
208 sizeof(struct storvsc_request_extension
),
209 sizeof(struct vstor_packet
),
210 sizeof(struct vmscsi_request
));
212 /* Make sure we are at least 2 pages since 1 page is used for control */
213 ASSERT(storDriver
->RingBufferSize
>= (PAGE_SIZE
<< 1));
215 Driver
->name
= gDriverName
;
216 memcpy(&Driver
->deviceType
, &gStorVscDeviceType
,
217 sizeof(struct hv_guid
));
219 storDriver
->RequestExtSize
= sizeof(struct storvsc_request_extension
);
222 * Divide the ring buffer data size (which is 1 page less
223 * than the ring buffer size since that page is reserved for
224 * the ring buffer indices) by the max request size (which is
225 * VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER + struct vstor_packet + u64)
227 storDriver
->MaxOutstandingRequestsPerChannel
=
228 ((storDriver
->RingBufferSize
- PAGE_SIZE
) /
229 ALIGN_UP(MAX_MULTIPAGE_BUFFER_PACKET
+
230 sizeof(struct vstor_packet
) + sizeof(u64
),
233 DPRINT_INFO(STORVSC
, "max io %u, currently %u\n",
234 storDriver
->MaxOutstandingRequestsPerChannel
,
235 STORVSC_MAX_IO_REQUESTS
);
237 /* Setup the dispatch table */
238 storDriver
->Base
.OnDeviceAdd
= StorVscOnDeviceAdd
;
239 storDriver
->Base
.OnDeviceRemove
= StorVscOnDeviceRemove
;
240 storDriver
->Base
.OnCleanup
= StorVscOnCleanup
;
242 storDriver
->OnIORequest
= StorVscOnIORequest
;
243 storDriver
->OnHostReset
= StorVscOnHostReset
;
245 DPRINT_EXIT(STORVSC
);
251 * StorVscOnDeviceAdd - Callback when the device belonging to this driver is added
253 static int StorVscOnDeviceAdd(struct hv_device
*Device
, void *AdditionalInfo
)
255 struct storvsc_device
*storDevice
;
256 /* struct vmstorage_channel_properties *props; */
257 struct storvsc_device_info
*deviceInfo
;
260 DPRINT_ENTER(STORVSC
);
262 deviceInfo
= (struct storvsc_device_info
*)AdditionalInfo
;
263 storDevice
= AllocStorDevice(Device
);
269 /* Save the channel properties to our storvsc channel */
270 /* props = (struct vmstorage_channel_properties *)
271 * channel->offerMsg.Offer.u.Standard.UserDefined; */
275 * If we support more than 1 scsi channel, we need to set the
276 * port number here to the scsi channel but how do we get the
277 * scsi channel prior to the bus scan
280 /* storChannel->PortNumber = 0;
281 storChannel->PathId = props->PathId;
282 storChannel->TargetId = props->TargetId; */
284 storDevice
->PortNumber
= deviceInfo
->PortNumber
;
285 /* Send it back up */
286 ret
= StorVscConnectToVsp(Device
);
288 /* deviceInfo->PortNumber = storDevice->PortNumber; */
289 deviceInfo
->PathId
= storDevice
->PathId
;
290 deviceInfo
->TargetId
= storDevice
->TargetId
;
292 DPRINT_DBG(STORVSC
, "assigned port %u, path %u target %u\n",
293 storDevice
->PortNumber
, storDevice
->PathId
,
294 storDevice
->TargetId
);
297 DPRINT_EXIT(STORVSC
);
302 static int StorVscChannelInit(struct hv_device
*Device
)
304 struct storvsc_device
*storDevice
;
305 struct storvsc_request_extension
*request
;
306 struct vstor_packet
*vstorPacket
;
309 storDevice
= GetStorDevice(Device
);
311 DPRINT_ERR(STORVSC
, "unable to get stor device..."
312 "device being destroyed?");
313 DPRINT_EXIT(STORVSC
);
317 request
= &storDevice
->InitRequest
;
318 vstorPacket
= &request
->VStorPacket
;
321 * Now, initiate the vsc/vsp initialization protocol on the open
324 memset(request
, sizeof(struct storvsc_request_extension
), 0);
325 request
->WaitEvent
= osd_WaitEventCreate();
327 vstorPacket
->Operation
= VStorOperationBeginInitialization
;
328 vstorPacket
->Flags
= REQUEST_COMPLETION_FLAG
;
330 /*SpinlockAcquire(gDriverExt.packetListLock);
331 INSERT_TAIL_LIST(&gDriverExt.packetList, &packet->listEntry.entry);
332 SpinlockRelease(gDriverExt.packetListLock);*/
334 DPRINT_INFO(STORVSC
, "BEGIN_INITIALIZATION_OPERATION...");
336 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
338 sizeof(struct vstor_packet
),
339 (unsigned long)request
,
340 VmbusPacketTypeDataInBand
,
341 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
344 "unable to send BEGIN_INITIALIZATION_OPERATION");
348 osd_WaitEventWait(request
->WaitEvent
);
350 if (vstorPacket
->Operation
!= VStorOperationCompleteIo
||
351 vstorPacket
->Status
!= 0) {
352 DPRINT_ERR(STORVSC
, "BEGIN_INITIALIZATION_OPERATION failed "
353 "(op %d status 0x%x)",
354 vstorPacket
->Operation
, vstorPacket
->Status
);
358 DPRINT_INFO(STORVSC
, "QUERY_PROTOCOL_VERSION_OPERATION...");
360 /* reuse the packet for version range supported */
361 memset(vstorPacket
, sizeof(struct vstor_packet
), 0);
362 vstorPacket
->Operation
= VStorOperationQueryProtocolVersion
;
363 vstorPacket
->Flags
= REQUEST_COMPLETION_FLAG
;
365 vstorPacket
->Version
.MajorMinor
= VMSTOR_PROTOCOL_VERSION_CURRENT
;
366 FILL_VMSTOR_REVISION(vstorPacket
->Version
.Revision
);
368 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
370 sizeof(struct vstor_packet
),
371 (unsigned long)request
,
372 VmbusPacketTypeDataInBand
,
373 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
376 "unable to send BEGIN_INITIALIZATION_OPERATION");
380 osd_WaitEventWait(request
->WaitEvent
);
382 /* TODO: Check returned version */
383 if (vstorPacket
->Operation
!= VStorOperationCompleteIo
||
384 vstorPacket
->Status
!= 0) {
385 DPRINT_ERR(STORVSC
, "QUERY_PROTOCOL_VERSION_OPERATION failed "
386 "(op %d status 0x%x)",
387 vstorPacket
->Operation
, vstorPacket
->Status
);
391 /* Query channel properties */
392 DPRINT_INFO(STORVSC
, "QUERY_PROPERTIES_OPERATION...");
394 memset(vstorPacket
, sizeof(struct vstor_packet
), 0);
395 vstorPacket
->Operation
= VStorOperationQueryProperties
;
396 vstorPacket
->Flags
= REQUEST_COMPLETION_FLAG
;
397 vstorPacket
->StorageChannelProperties
.PortNumber
=
398 storDevice
->PortNumber
;
400 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
402 sizeof(struct vstor_packet
),
403 (unsigned long)request
,
404 VmbusPacketTypeDataInBand
,
405 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
409 "unable to send QUERY_PROPERTIES_OPERATION");
413 osd_WaitEventWait(request
->WaitEvent
);
415 /* TODO: Check returned version */
416 if (vstorPacket
->Operation
!= VStorOperationCompleteIo
||
417 vstorPacket
->Status
!= 0) {
418 DPRINT_ERR(STORVSC
, "QUERY_PROPERTIES_OPERATION failed "
419 "(op %d status 0x%x)",
420 vstorPacket
->Operation
, vstorPacket
->Status
);
424 storDevice
->PathId
= vstorPacket
->StorageChannelProperties
.PathId
;
425 storDevice
->TargetId
= vstorPacket
->StorageChannelProperties
.TargetId
;
427 DPRINT_DBG(STORVSC
, "channel flag 0x%x, max xfer len 0x%x",
428 vstorPacket
->StorageChannelProperties
.Flags
,
429 vstorPacket
->StorageChannelProperties
.MaxTransferBytes
);
431 DPRINT_INFO(STORVSC
, "END_INITIALIZATION_OPERATION...");
433 memset(vstorPacket
, sizeof(struct vstor_packet
), 0);
434 vstorPacket
->Operation
= VStorOperationEndInitialization
;
435 vstorPacket
->Flags
= REQUEST_COMPLETION_FLAG
;
437 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
439 sizeof(struct vstor_packet
),
440 (unsigned long)request
,
441 VmbusPacketTypeDataInBand
,
442 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
446 "unable to send END_INITIALIZATION_OPERATION");
450 osd_WaitEventWait(request
->WaitEvent
);
452 if (vstorPacket
->Operation
!= VStorOperationCompleteIo
||
453 vstorPacket
->Status
!= 0) {
454 DPRINT_ERR(STORVSC
, "END_INITIALIZATION_OPERATION failed "
455 "(op %d status 0x%x)",
456 vstorPacket
->Operation
, vstorPacket
->Status
);
460 DPRINT_INFO(STORVSC
, "**** storage channel up and running!! ****");
463 kfree(request
->WaitEvent
);
464 request
->WaitEvent
= NULL
;
466 PutStorDevice(Device
);
468 DPRINT_EXIT(STORVSC
);
472 static int StorVscConnectToVsp(struct hv_device
*Device
)
474 struct vmstorage_channel_properties props
;
475 struct storvsc_driver_object
*storDriver
;
478 storDriver
= (struct storvsc_driver_object
*)Device
->Driver
;
479 memset(&props
, sizeof(struct vmstorage_channel_properties
), 0);
481 /* Open the channel */
482 ret
= Device
->Driver
->VmbusChannelInterface
.Open(Device
,
483 storDriver
->RingBufferSize
,
484 storDriver
->RingBufferSize
,
486 sizeof(struct vmstorage_channel_properties
),
487 StorVscOnChannelCallback
,
490 DPRINT_DBG(STORVSC
, "storage props: path id %d, tgt id %d, max xfer %d",
491 props
.PathId
, props
.TargetId
, props
.MaxTransferBytes
);
494 DPRINT_ERR(STORVSC
, "unable to open channel: %d", ret
);
498 ret
= StorVscChannelInit(Device
);
505 * StorVscOnDeviceRemove - Callback when the our device is being removed
507 static int StorVscOnDeviceRemove(struct hv_device
*Device
)
509 struct storvsc_device
*storDevice
;
511 DPRINT_ENTER(STORVSC
);
513 DPRINT_INFO(STORVSC
, "disabling storage device (%p)...",
516 storDevice
= ReleaseStorDevice(Device
);
519 * At this point, all outbound traffic should be disable. We
520 * only allow inbound traffic (responses) to proceed so that
521 * outstanding requests can be completed.
523 while (atomic_read(&storDevice
->NumOutstandingRequests
)) {
524 DPRINT_INFO(STORVSC
, "waiting for %d requests to complete...",
525 atomic_read(&storDevice
->NumOutstandingRequests
));
529 DPRINT_INFO(STORVSC
, "removing storage device (%p)...",
532 storDevice
= FinalReleaseStorDevice(Device
);
534 DPRINT_INFO(STORVSC
, "storage device (%p) safe to remove", storDevice
);
536 /* Close the channel */
537 Device
->Driver
->VmbusChannelInterface
.Close(Device
);
539 FreeStorDevice(storDevice
);
541 DPRINT_EXIT(STORVSC
);
545 static int StorVscOnHostReset(struct hv_device
*Device
)
547 struct storvsc_device
*storDevice
;
548 struct storvsc_request_extension
*request
;
549 struct vstor_packet
*vstorPacket
;
552 DPRINT_ENTER(STORVSC
);
554 DPRINT_INFO(STORVSC
, "resetting host adapter...");
556 storDevice
= GetStorDevice(Device
);
558 DPRINT_ERR(STORVSC
, "unable to get stor device..."
559 "device being destroyed?");
560 DPRINT_EXIT(STORVSC
);
564 request
= &storDevice
->ResetRequest
;
565 vstorPacket
= &request
->VStorPacket
;
567 request
->WaitEvent
= osd_WaitEventCreate();
569 vstorPacket
->Operation
= VStorOperationResetBus
;
570 vstorPacket
->Flags
= REQUEST_COMPLETION_FLAG
;
571 vstorPacket
->VmSrb
.PathId
= storDevice
->PathId
;
573 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
575 sizeof(struct vstor_packet
),
576 (unsigned long)&storDevice
->ResetRequest
,
577 VmbusPacketTypeDataInBand
,
578 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
580 DPRINT_ERR(STORVSC
, "Unable to send reset packet %p ret %d",
585 /* FIXME: Add a timeout */
586 osd_WaitEventWait(request
->WaitEvent
);
588 kfree(request
->WaitEvent
);
589 DPRINT_INFO(STORVSC
, "host adapter reset completed");
592 * At this point, all outstanding requests in the adapter
593 * should have been flushed out and return to us
597 PutStorDevice(Device
);
598 DPRINT_EXIT(STORVSC
);
603 * StorVscOnIORequest - Callback to initiate an I/O request
605 static int StorVscOnIORequest(struct hv_device
*Device
,
606 struct hv_storvsc_request
*Request
)
608 struct storvsc_device
*storDevice
;
609 struct storvsc_request_extension
*requestExtension
;
610 struct vstor_packet
*vstorPacket
;
613 DPRINT_ENTER(STORVSC
);
616 (struct storvsc_request_extension
*)Request
->Extension
;
617 vstorPacket
= &requestExtension
->VStorPacket
;
618 storDevice
= GetStorDevice(Device
);
620 DPRINT_DBG(STORVSC
, "enter - Device %p, DeviceExt %p, Request %p, "
621 "Extension %p", Device
, storDevice
, Request
,
624 DPRINT_DBG(STORVSC
, "req %p len %d bus %d, target %d, lun %d cdblen %d",
625 Request
, Request
->DataBuffer
.Length
, Request
->Bus
,
626 Request
->TargetId
, Request
->LunId
, Request
->CdbLen
);
629 DPRINT_ERR(STORVSC
, "unable to get stor device..."
630 "device being destroyed?");
631 DPRINT_EXIT(STORVSC
);
635 /* print_hex_dump_bytes("", DUMP_PREFIX_NONE, Request->Cdb,
636 * Request->CdbLen); */
638 requestExtension
->Request
= Request
;
639 requestExtension
->Device
= Device
;
641 memset(vstorPacket
, 0 , sizeof(struct vstor_packet
));
643 vstorPacket
->Flags
|= REQUEST_COMPLETION_FLAG
;
645 vstorPacket
->VmSrb
.Length
= sizeof(struct vmscsi_request
);
647 vstorPacket
->VmSrb
.PortNumber
= Request
->Host
;
648 vstorPacket
->VmSrb
.PathId
= Request
->Bus
;
649 vstorPacket
->VmSrb
.TargetId
= Request
->TargetId
;
650 vstorPacket
->VmSrb
.Lun
= Request
->LunId
;
652 vstorPacket
->VmSrb
.SenseInfoLength
= SENSE_BUFFER_SIZE
;
654 /* Copy over the scsi command descriptor block */
655 vstorPacket
->VmSrb
.CdbLength
= Request
->CdbLen
;
656 memcpy(&vstorPacket
->VmSrb
.Cdb
, Request
->Cdb
, Request
->CdbLen
);
658 vstorPacket
->VmSrb
.DataIn
= Request
->Type
;
659 vstorPacket
->VmSrb
.DataTransferLength
= Request
->DataBuffer
.Length
;
661 vstorPacket
->Operation
= VStorOperationExecuteSRB
;
663 DPRINT_DBG(STORVSC
, "srb - len %d port %d, path %d, target %d, "
664 "lun %d senselen %d cdblen %d",
665 vstorPacket
->VmSrb
.Length
,
666 vstorPacket
->VmSrb
.PortNumber
,
667 vstorPacket
->VmSrb
.PathId
,
668 vstorPacket
->VmSrb
.TargetId
,
669 vstorPacket
->VmSrb
.Lun
,
670 vstorPacket
->VmSrb
.SenseInfoLength
,
671 vstorPacket
->VmSrb
.CdbLength
);
673 if (requestExtension
->Request
->DataBuffer
.Length
) {
674 ret
= Device
->Driver
->VmbusChannelInterface
.
675 SendPacketMultiPageBuffer(Device
,
676 &requestExtension
->Request
->DataBuffer
,
678 sizeof(struct vstor_packet
),
679 (unsigned long)requestExtension
);
681 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
683 sizeof(struct vstor_packet
),
684 (unsigned long)requestExtension
,
685 VmbusPacketTypeDataInBand
,
686 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
690 DPRINT_DBG(STORVSC
, "Unable to send packet %p ret %d",
694 atomic_inc(&storDevice
->NumOutstandingRequests
);
696 PutStorDevice(Device
);
698 DPRINT_EXIT(STORVSC
);
703 * StorVscOnCleanup - Perform any cleanup when the driver is removed
705 static void StorVscOnCleanup(struct hv_driver
*Driver
)
707 DPRINT_ENTER(STORVSC
);
708 DPRINT_EXIT(STORVSC
);
711 static void StorVscOnIOCompletion(struct hv_device
*Device
,
712 struct vstor_packet
*VStorPacket
,
713 struct storvsc_request_extension
*RequestExt
)
715 struct hv_storvsc_request
*request
;
716 struct storvsc_device
*storDevice
;
718 DPRINT_ENTER(STORVSC
);
720 storDevice
= MustGetStorDevice(Device
);
722 DPRINT_ERR(STORVSC
, "unable to get stor device..."
723 "device being destroyed?");
724 DPRINT_EXIT(STORVSC
);
728 DPRINT_DBG(STORVSC
, "IO_COMPLETE_OPERATION - request extension %p "
729 "completed bytes xfer %u", RequestExt
,
730 VStorPacket
->VmSrb
.DataTransferLength
);
732 ASSERT(RequestExt
!= NULL
);
733 ASSERT(RequestExt
->Request
!= NULL
);
735 request
= RequestExt
->Request
;
737 ASSERT(request
->OnIOCompletion
!= NULL
);
739 /* Copy over the status...etc */
740 request
->Status
= VStorPacket
->VmSrb
.ScsiStatus
;
742 if (request
->Status
!= 0 || VStorPacket
->VmSrb
.SrbStatus
!= 1) {
744 "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
745 request
->Cdb
[0], VStorPacket
->VmSrb
.ScsiStatus
,
746 VStorPacket
->VmSrb
.SrbStatus
);
749 if ((request
->Status
& 0xFF) == 0x02) {
750 /* CHECK_CONDITION */
751 if (VStorPacket
->VmSrb
.SrbStatus
& 0x80) {
752 /* autosense data available */
753 DPRINT_WARN(STORVSC
, "storvsc pkt %p autosense data "
754 "valid - len %d\n", RequestExt
,
755 VStorPacket
->VmSrb
.SenseInfoLength
);
757 ASSERT(VStorPacket
->VmSrb
.SenseInfoLength
<=
758 request
->SenseBufferSize
);
759 memcpy(request
->SenseBuffer
,
760 VStorPacket
->VmSrb
.SenseData
,
761 VStorPacket
->VmSrb
.SenseInfoLength
);
763 request
->SenseBufferSize
=
764 VStorPacket
->VmSrb
.SenseInfoLength
;
769 request
->BytesXfer
= VStorPacket
->VmSrb
.DataTransferLength
;
771 request
->OnIOCompletion(request
);
773 atomic_dec(&storDevice
->NumOutstandingRequests
);
775 PutStorDevice(Device
);
777 DPRINT_EXIT(STORVSC
);
781 static void StorVscOnReceive(struct hv_device
*Device
,
782 struct vstor_packet
*VStorPacket
,
783 struct storvsc_request_extension
*RequestExt
)
785 switch (VStorPacket
->Operation
) {
786 case VStorOperationCompleteIo
:
787 DPRINT_DBG(STORVSC
, "IO_COMPLETE_OPERATION");
788 StorVscOnIOCompletion(Device
, VStorPacket
, RequestExt
);
790 case VStorOperationRemoveDevice
:
791 DPRINT_INFO(STORVSC
, "REMOVE_DEVICE_OPERATION");
796 DPRINT_INFO(STORVSC
, "Unknown operation received - %d",
797 VStorPacket
->Operation
);
802 static void StorVscOnChannelCallback(void *context
)
804 struct hv_device
*device
= (struct hv_device
*)context
;
805 struct storvsc_device
*storDevice
;
808 unsigned char packet
[ALIGN_UP(sizeof(struct vstor_packet
), 8)];
809 struct storvsc_request_extension
*request
;
812 DPRINT_ENTER(STORVSC
);
816 storDevice
= MustGetStorDevice(device
);
818 DPRINT_ERR(STORVSC
, "unable to get stor device..."
819 "device being destroyed?");
820 DPRINT_EXIT(STORVSC
);
825 ret
= device
->Driver
->VmbusChannelInterface
.RecvPacket(device
,
827 ALIGN_UP(sizeof(struct vstor_packet
), 8),
828 &bytesRecvd
, &requestId
);
829 if (ret
== 0 && bytesRecvd
> 0) {
830 DPRINT_DBG(STORVSC
, "receive %d bytes - tid %llx",
831 bytesRecvd
, requestId
);
833 /* ASSERT(bytesRecvd == sizeof(struct vstor_packet)); */
835 request
= (struct storvsc_request_extension
*)
836 (unsigned long)requestId
;
839 /* if (vstorPacket.Flags & SYNTHETIC_FLAG) */
840 if ((request
== &storDevice
->InitRequest
) ||
841 (request
== &storDevice
->ResetRequest
)) {
842 /* DPRINT_INFO(STORVSC,
843 * "reset completion - operation "
845 * vstorPacket.Operation,
846 * vstorPacket.Status); */
848 memcpy(&request
->VStorPacket
, packet
,
849 sizeof(struct vstor_packet
));
851 osd_WaitEventSet(request
->WaitEvent
);
853 StorVscOnReceive(device
,
854 (struct vstor_packet
*)packet
,
858 /* DPRINT_DBG(STORVSC, "nothing else to read..."); */
863 PutStorDevice(device
);
865 DPRINT_EXIT(STORVSC
);