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 "StorVscApi.h"
29 #include "VmbusPacketFormat.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 DPRINT_ENTER(STORVSC
);
361 storDevice
= MustGetStorDevice(Device
);
363 DPRINT_ERR(STORVSC
, "unable to get stor device..."
364 "device being destroyed?");
365 DPRINT_EXIT(STORVSC
);
369 DPRINT_DBG(STORVSC
, "IO_COMPLETE_OPERATION - request extension %p "
370 "completed bytes xfer %u", RequestExt
,
371 VStorPacket
->VmSrb
.DataTransferLength
);
373 /* ASSERT(RequestExt != NULL); */
374 /* ASSERT(RequestExt->Request != NULL); */
376 request
= RequestExt
->Request
;
378 /* ASSERT(request->OnIOCompletion != NULL); */
380 /* Copy over the status...etc */
381 request
->Status
= VStorPacket
->VmSrb
.ScsiStatus
;
383 if (request
->Status
!= 0 || VStorPacket
->VmSrb
.SrbStatus
!= 1) {
385 "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
386 request
->Cdb
[0], VStorPacket
->VmSrb
.ScsiStatus
,
387 VStorPacket
->VmSrb
.SrbStatus
);
390 if ((request
->Status
& 0xFF) == 0x02) {
391 /* CHECK_CONDITION */
392 if (VStorPacket
->VmSrb
.SrbStatus
& 0x80) {
393 /* autosense data available */
394 DPRINT_WARN(STORVSC
, "storvsc pkt %p autosense data "
395 "valid - len %d\n", RequestExt
,
396 VStorPacket
->VmSrb
.SenseInfoLength
);
398 /* ASSERT(VStorPacket->VmSrb.SenseInfoLength <= */
399 /* request->SenseBufferSize); */
400 memcpy(request
->SenseBuffer
,
401 VStorPacket
->VmSrb
.SenseData
,
402 VStorPacket
->VmSrb
.SenseInfoLength
);
404 request
->SenseBufferSize
=
405 VStorPacket
->VmSrb
.SenseInfoLength
;
410 request
->BytesXfer
= VStorPacket
->VmSrb
.DataTransferLength
;
412 request
->OnIOCompletion(request
);
414 atomic_dec(&storDevice
->NumOutstandingRequests
);
416 PutStorDevice(Device
);
418 DPRINT_EXIT(STORVSC
);
421 static void StorVscOnReceive(struct hv_device
*Device
,
422 struct vstor_packet
*VStorPacket
,
423 struct storvsc_request_extension
*RequestExt
)
425 switch (VStorPacket
->Operation
) {
426 case VStorOperationCompleteIo
:
427 DPRINT_DBG(STORVSC
, "IO_COMPLETE_OPERATION");
428 StorVscOnIOCompletion(Device
, VStorPacket
, RequestExt
);
430 case VStorOperationRemoveDevice
:
431 DPRINT_INFO(STORVSC
, "REMOVE_DEVICE_OPERATION");
436 DPRINT_INFO(STORVSC
, "Unknown operation received - %d",
437 VStorPacket
->Operation
);
442 static void StorVscOnChannelCallback(void *context
)
444 struct hv_device
*device
= (struct hv_device
*)context
;
445 struct storvsc_device
*storDevice
;
448 unsigned char packet
[ALIGN_UP(sizeof(struct vstor_packet
), 8)];
449 struct storvsc_request_extension
*request
;
452 DPRINT_ENTER(STORVSC
);
454 /* ASSERT(device); */
456 storDevice
= MustGetStorDevice(device
);
458 DPRINT_ERR(STORVSC
, "unable to get stor device..."
459 "device being destroyed?");
460 DPRINT_EXIT(STORVSC
);
465 ret
= device
->Driver
->VmbusChannelInterface
.RecvPacket(device
,
467 ALIGN_UP(sizeof(struct vstor_packet
), 8),
468 &bytesRecvd
, &requestId
);
469 if (ret
== 0 && bytesRecvd
> 0) {
470 DPRINT_DBG(STORVSC
, "receive %d bytes - tid %llx",
471 bytesRecvd
, requestId
);
473 /* ASSERT(bytesRecvd == sizeof(struct vstor_packet)); */
475 request
= (struct storvsc_request_extension
*)
476 (unsigned long)requestId
;
477 /* ASSERT(request);c */
479 /* if (vstorPacket.Flags & SYNTHETIC_FLAG) */
480 if ((request
== &storDevice
->InitRequest
) ||
481 (request
== &storDevice
->ResetRequest
)) {
482 /* DPRINT_INFO(STORVSC,
483 * "reset completion - operation "
485 * vstorPacket.Operation,
486 * vstorPacket.Status); */
488 memcpy(&request
->VStorPacket
, packet
,
489 sizeof(struct vstor_packet
));
491 osd_WaitEventSet(request
->WaitEvent
);
493 StorVscOnReceive(device
,
494 (struct vstor_packet
*)packet
,
498 /* DPRINT_DBG(STORVSC, "nothing else to read..."); */
503 PutStorDevice(device
);
505 DPRINT_EXIT(STORVSC
);
509 static int StorVscConnectToVsp(struct hv_device
*Device
)
511 struct vmstorage_channel_properties props
;
512 struct storvsc_driver_object
*storDriver
;
515 storDriver
= (struct storvsc_driver_object
*)Device
->Driver
;
516 memset(&props
, 0, sizeof(struct vmstorage_channel_properties
));
518 /* Open the channel */
519 ret
= Device
->Driver
->VmbusChannelInterface
.Open(Device
,
520 storDriver
->RingBufferSize
,
521 storDriver
->RingBufferSize
,
523 sizeof(struct vmstorage_channel_properties
),
524 StorVscOnChannelCallback
,
527 DPRINT_DBG(STORVSC
, "storage props: path id %d, tgt id %d, max xfer %d",
528 props
.PathId
, props
.TargetId
, props
.MaxTransferBytes
);
531 DPRINT_ERR(STORVSC
, "unable to open channel: %d", ret
);
535 ret
= StorVscChannelInit(Device
);
541 * StorVscOnDeviceAdd - Callback when the device belonging to this driver is added
543 static int StorVscOnDeviceAdd(struct hv_device
*Device
, void *AdditionalInfo
)
545 struct storvsc_device
*storDevice
;
546 /* struct vmstorage_channel_properties *props; */
547 struct storvsc_device_info
*deviceInfo
;
550 DPRINT_ENTER(STORVSC
);
552 deviceInfo
= (struct storvsc_device_info
*)AdditionalInfo
;
553 storDevice
= AllocStorDevice(Device
);
559 /* Save the channel properties to our storvsc channel */
560 /* props = (struct vmstorage_channel_properties *)
561 * channel->offerMsg.Offer.u.Standard.UserDefined; */
565 * If we support more than 1 scsi channel, we need to set the
566 * port number here to the scsi channel but how do we get the
567 * scsi channel prior to the bus scan
570 /* storChannel->PortNumber = 0;
571 storChannel->PathId = props->PathId;
572 storChannel->TargetId = props->TargetId; */
574 storDevice
->PortNumber
= deviceInfo
->PortNumber
;
575 /* Send it back up */
576 ret
= StorVscConnectToVsp(Device
);
578 /* deviceInfo->PortNumber = storDevice->PortNumber; */
579 deviceInfo
->PathId
= storDevice
->PathId
;
580 deviceInfo
->TargetId
= storDevice
->TargetId
;
582 DPRINT_DBG(STORVSC
, "assigned port %u, path %u target %u\n",
583 storDevice
->PortNumber
, storDevice
->PathId
,
584 storDevice
->TargetId
);
587 DPRINT_EXIT(STORVSC
);
593 * StorVscOnDeviceRemove - Callback when the our device is being removed
595 static int StorVscOnDeviceRemove(struct hv_device
*Device
)
597 struct storvsc_device
*storDevice
;
599 DPRINT_ENTER(STORVSC
);
601 DPRINT_INFO(STORVSC
, "disabling storage device (%p)...",
604 storDevice
= ReleaseStorDevice(Device
);
607 * At this point, all outbound traffic should be disable. We
608 * only allow inbound traffic (responses) to proceed so that
609 * outstanding requests can be completed.
611 while (atomic_read(&storDevice
->NumOutstandingRequests
)) {
612 DPRINT_INFO(STORVSC
, "waiting for %d requests to complete...",
613 atomic_read(&storDevice
->NumOutstandingRequests
));
617 DPRINT_INFO(STORVSC
, "removing storage device (%p)...",
620 storDevice
= FinalReleaseStorDevice(Device
);
622 DPRINT_INFO(STORVSC
, "storage device (%p) safe to remove", storDevice
);
624 /* Close the channel */
625 Device
->Driver
->VmbusChannelInterface
.Close(Device
);
627 FreeStorDevice(storDevice
);
629 DPRINT_EXIT(STORVSC
);
633 int StorVscOnHostReset(struct hv_device
*Device
)
635 struct storvsc_device
*storDevice
;
636 struct storvsc_request_extension
*request
;
637 struct vstor_packet
*vstorPacket
;
640 DPRINT_ENTER(STORVSC
);
642 DPRINT_INFO(STORVSC
, "resetting host adapter...");
644 storDevice
= GetStorDevice(Device
);
646 DPRINT_ERR(STORVSC
, "unable to get stor device..."
647 "device being destroyed?");
648 DPRINT_EXIT(STORVSC
);
652 request
= &storDevice
->ResetRequest
;
653 vstorPacket
= &request
->VStorPacket
;
655 request
->WaitEvent
= osd_WaitEventCreate();
656 if (!request
->WaitEvent
) {
661 vstorPacket
->Operation
= VStorOperationResetBus
;
662 vstorPacket
->Flags
= REQUEST_COMPLETION_FLAG
;
663 vstorPacket
->VmSrb
.PathId
= storDevice
->PathId
;
665 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
667 sizeof(struct vstor_packet
),
668 (unsigned long)&storDevice
->ResetRequest
,
669 VmbusPacketTypeDataInBand
,
670 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
672 DPRINT_ERR(STORVSC
, "Unable to send reset packet %p ret %d",
677 /* FIXME: Add a timeout */
678 osd_WaitEventWait(request
->WaitEvent
);
680 kfree(request
->WaitEvent
);
681 DPRINT_INFO(STORVSC
, "host adapter reset completed");
684 * At this point, all outstanding requests in the adapter
685 * should have been flushed out and return to us
689 PutStorDevice(Device
);
690 DPRINT_EXIT(STORVSC
);
695 * StorVscOnIORequest - Callback to initiate an I/O request
697 static int StorVscOnIORequest(struct hv_device
*Device
,
698 struct hv_storvsc_request
*Request
)
700 struct storvsc_device
*storDevice
;
701 struct storvsc_request_extension
*requestExtension
;
702 struct vstor_packet
*vstorPacket
;
705 DPRINT_ENTER(STORVSC
);
708 (struct storvsc_request_extension
*)Request
->Extension
;
709 vstorPacket
= &requestExtension
->VStorPacket
;
710 storDevice
= GetStorDevice(Device
);
712 DPRINT_DBG(STORVSC
, "enter - Device %p, DeviceExt %p, Request %p, "
713 "Extension %p", Device
, storDevice
, Request
,
716 DPRINT_DBG(STORVSC
, "req %p len %d bus %d, target %d, lun %d cdblen %d",
717 Request
, Request
->DataBuffer
.Length
, Request
->Bus
,
718 Request
->TargetId
, Request
->LunId
, Request
->CdbLen
);
721 DPRINT_ERR(STORVSC
, "unable to get stor device..."
722 "device being destroyed?");
723 DPRINT_EXIT(STORVSC
);
727 /* print_hex_dump_bytes("", DUMP_PREFIX_NONE, Request->Cdb,
728 * Request->CdbLen); */
730 requestExtension
->Request
= Request
;
731 requestExtension
->Device
= Device
;
733 memset(vstorPacket
, 0 , sizeof(struct vstor_packet
));
735 vstorPacket
->Flags
|= REQUEST_COMPLETION_FLAG
;
737 vstorPacket
->VmSrb
.Length
= sizeof(struct vmscsi_request
);
739 vstorPacket
->VmSrb
.PortNumber
= Request
->Host
;
740 vstorPacket
->VmSrb
.PathId
= Request
->Bus
;
741 vstorPacket
->VmSrb
.TargetId
= Request
->TargetId
;
742 vstorPacket
->VmSrb
.Lun
= Request
->LunId
;
744 vstorPacket
->VmSrb
.SenseInfoLength
= SENSE_BUFFER_SIZE
;
746 /* Copy over the scsi command descriptor block */
747 vstorPacket
->VmSrb
.CdbLength
= Request
->CdbLen
;
748 memcpy(&vstorPacket
->VmSrb
.Cdb
, Request
->Cdb
, Request
->CdbLen
);
750 vstorPacket
->VmSrb
.DataIn
= Request
->Type
;
751 vstorPacket
->VmSrb
.DataTransferLength
= Request
->DataBuffer
.Length
;
753 vstorPacket
->Operation
= VStorOperationExecuteSRB
;
755 DPRINT_DBG(STORVSC
, "srb - len %d port %d, path %d, target %d, "
756 "lun %d senselen %d cdblen %d",
757 vstorPacket
->VmSrb
.Length
,
758 vstorPacket
->VmSrb
.PortNumber
,
759 vstorPacket
->VmSrb
.PathId
,
760 vstorPacket
->VmSrb
.TargetId
,
761 vstorPacket
->VmSrb
.Lun
,
762 vstorPacket
->VmSrb
.SenseInfoLength
,
763 vstorPacket
->VmSrb
.CdbLength
);
765 if (requestExtension
->Request
->DataBuffer
.Length
) {
766 ret
= Device
->Driver
->VmbusChannelInterface
.
767 SendPacketMultiPageBuffer(Device
,
768 &requestExtension
->Request
->DataBuffer
,
770 sizeof(struct vstor_packet
),
771 (unsigned long)requestExtension
);
773 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
775 sizeof(struct vstor_packet
),
776 (unsigned long)requestExtension
,
777 VmbusPacketTypeDataInBand
,
778 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
782 DPRINT_DBG(STORVSC
, "Unable to send packet %p ret %d",
786 atomic_inc(&storDevice
->NumOutstandingRequests
);
788 PutStorDevice(Device
);
790 DPRINT_EXIT(STORVSC
);
795 * StorVscOnCleanup - Perform any cleanup when the driver is removed
797 static void StorVscOnCleanup(struct hv_driver
*Driver
)
799 DPRINT_ENTER(STORVSC
);
800 DPRINT_EXIT(STORVSC
);
804 * StorVscInitialize - Main entry point
806 int StorVscInitialize(struct hv_driver
*Driver
)
808 struct storvsc_driver_object
*storDriver
;
810 DPRINT_ENTER(STORVSC
);
812 storDriver
= (struct storvsc_driver_object
*)Driver
;
814 DPRINT_DBG(STORVSC
, "sizeof(STORVSC_REQUEST)=%zd "
815 "sizeof(struct storvsc_request_extension)=%zd "
816 "sizeof(struct vstor_packet)=%zd, "
817 "sizeof(struct vmscsi_request)=%zd",
818 sizeof(struct hv_storvsc_request
),
819 sizeof(struct storvsc_request_extension
),
820 sizeof(struct vstor_packet
),
821 sizeof(struct vmscsi_request
));
823 /* Make sure we are at least 2 pages since 1 page is used for control */
824 /* ASSERT(storDriver->RingBufferSize >= (PAGE_SIZE << 1)); */
826 Driver
->name
= gDriverName
;
827 memcpy(&Driver
->deviceType
, &gStorVscDeviceType
,
828 sizeof(struct hv_guid
));
830 storDriver
->RequestExtSize
= sizeof(struct storvsc_request_extension
);
833 * Divide the ring buffer data size (which is 1 page less
834 * than the ring buffer size since that page is reserved for
835 * the ring buffer indices) by the max request size (which is
836 * VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER + struct vstor_packet + u64)
838 storDriver
->MaxOutstandingRequestsPerChannel
=
839 ((storDriver
->RingBufferSize
- PAGE_SIZE
) /
840 ALIGN_UP(MAX_MULTIPAGE_BUFFER_PACKET
+
841 sizeof(struct vstor_packet
) + sizeof(u64
),
844 DPRINT_INFO(STORVSC
, "max io %u, currently %u\n",
845 storDriver
->MaxOutstandingRequestsPerChannel
,
846 STORVSC_MAX_IO_REQUESTS
);
848 /* Setup the dispatch table */
849 storDriver
->Base
.OnDeviceAdd
= StorVscOnDeviceAdd
;
850 storDriver
->Base
.OnDeviceRemove
= StorVscOnDeviceRemove
;
851 storDriver
->Base
.OnCleanup
= StorVscOnCleanup
;
853 storDriver
->OnIORequest
= StorVscOnIORequest
;
855 DPRINT_EXIT(STORVSC
);