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 inline struct storvsc_device
*AllocStorDevice(struct hv_device
*Device
)
84 struct storvsc_device
*storDevice
;
86 storDevice
= kzalloc(sizeof(struct storvsc_device
), GFP_KERNEL
);
90 /* Set to 2 to allow both inbound and outbound traffics */
91 /* (ie GetStorDevice() and MustGetStorDevice()) to proceed. */
92 atomic_cmpxchg(&storDevice
->RefCount
, 0, 2);
94 storDevice
->Device
= Device
;
95 Device
->Extension
= storDevice
;
100 static inline void FreeStorDevice(struct storvsc_device
*Device
)
102 ASSERT(atomic_read(&Device
->RefCount
) == 0);
106 /* Get the stordevice object iff exists and its refcount > 1 */
107 static inline struct storvsc_device
*GetStorDevice(struct hv_device
*Device
)
109 struct storvsc_device
*storDevice
;
111 storDevice
= (struct storvsc_device
*)Device
->Extension
;
112 if (storDevice
&& atomic_read(&storDevice
->RefCount
) > 1)
113 atomic_inc(&storDevice
->RefCount
);
120 /* Get the stordevice object iff exists and its refcount > 0 */
121 static inline struct storvsc_device
*MustGetStorDevice(struct hv_device
*Device
)
123 struct storvsc_device
*storDevice
;
125 storDevice
= (struct storvsc_device
*)Device
->Extension
;
126 if (storDevice
&& atomic_read(&storDevice
->RefCount
))
127 atomic_inc(&storDevice
->RefCount
);
134 static inline void PutStorDevice(struct hv_device
*Device
)
136 struct storvsc_device
*storDevice
;
138 storDevice
= (struct storvsc_device
*)Device
->Extension
;
141 atomic_dec(&storDevice
->RefCount
);
142 ASSERT(atomic_read(&storDevice
->RefCount
));
145 /* Drop ref count to 1 to effectively disable GetStorDevice() */
146 static inline struct storvsc_device
*ReleaseStorDevice(struct hv_device
*Device
)
148 struct storvsc_device
*storDevice
;
150 storDevice
= (struct storvsc_device
*)Device
->Extension
;
153 /* Busy wait until the ref drop to 2, then set it to 1 */
154 while (atomic_cmpxchg(&storDevice
->RefCount
, 2, 1) != 2)
160 /* Drop ref count to 0. No one can use StorDevice object. */
161 static inline struct storvsc_device
*FinalReleaseStorDevice(
162 struct hv_device
*Device
)
164 struct storvsc_device
*storDevice
;
166 storDevice
= (struct storvsc_device
*)Device
->Extension
;
169 /* Busy wait until the ref drop to 1, then set it to 0 */
170 while (atomic_cmpxchg(&storDevice
->RefCount
, 1, 0) != 1)
173 Device
->Extension
= NULL
;
177 static int StorVscChannelInit(struct hv_device
*Device
)
179 struct storvsc_device
*storDevice
;
180 struct storvsc_request_extension
*request
;
181 struct vstor_packet
*vstorPacket
;
184 storDevice
= GetStorDevice(Device
);
186 DPRINT_ERR(STORVSC
, "unable to get stor device..."
187 "device being destroyed?");
188 DPRINT_EXIT(STORVSC
);
192 request
= &storDevice
->InitRequest
;
193 vstorPacket
= &request
->VStorPacket
;
196 * Now, initiate the vsc/vsp initialization protocol on the open
199 memset(request
, sizeof(struct storvsc_request_extension
), 0);
200 request
->WaitEvent
= osd_WaitEventCreate();
202 vstorPacket
->Operation
= VStorOperationBeginInitialization
;
203 vstorPacket
->Flags
= REQUEST_COMPLETION_FLAG
;
205 /*SpinlockAcquire(gDriverExt.packetListLock);
206 INSERT_TAIL_LIST(&gDriverExt.packetList, &packet->listEntry.entry);
207 SpinlockRelease(gDriverExt.packetListLock);*/
209 DPRINT_INFO(STORVSC
, "BEGIN_INITIALIZATION_OPERATION...");
211 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
213 sizeof(struct vstor_packet
),
214 (unsigned long)request
,
215 VmbusPacketTypeDataInBand
,
216 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
219 "unable to send BEGIN_INITIALIZATION_OPERATION");
223 osd_WaitEventWait(request
->WaitEvent
);
225 if (vstorPacket
->Operation
!= VStorOperationCompleteIo
||
226 vstorPacket
->Status
!= 0) {
227 DPRINT_ERR(STORVSC
, "BEGIN_INITIALIZATION_OPERATION failed "
228 "(op %d status 0x%x)",
229 vstorPacket
->Operation
, vstorPacket
->Status
);
233 DPRINT_INFO(STORVSC
, "QUERY_PROTOCOL_VERSION_OPERATION...");
235 /* reuse the packet for version range supported */
236 memset(vstorPacket
, sizeof(struct vstor_packet
), 0);
237 vstorPacket
->Operation
= VStorOperationQueryProtocolVersion
;
238 vstorPacket
->Flags
= REQUEST_COMPLETION_FLAG
;
240 vstorPacket
->Version
.MajorMinor
= VMSTOR_PROTOCOL_VERSION_CURRENT
;
241 FILL_VMSTOR_REVISION(vstorPacket
->Version
.Revision
);
243 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
245 sizeof(struct vstor_packet
),
246 (unsigned long)request
,
247 VmbusPacketTypeDataInBand
,
248 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
251 "unable to send BEGIN_INITIALIZATION_OPERATION");
255 osd_WaitEventWait(request
->WaitEvent
);
257 /* TODO: Check returned version */
258 if (vstorPacket
->Operation
!= VStorOperationCompleteIo
||
259 vstorPacket
->Status
!= 0) {
260 DPRINT_ERR(STORVSC
, "QUERY_PROTOCOL_VERSION_OPERATION failed "
261 "(op %d status 0x%x)",
262 vstorPacket
->Operation
, vstorPacket
->Status
);
266 /* Query channel properties */
267 DPRINT_INFO(STORVSC
, "QUERY_PROPERTIES_OPERATION...");
269 memset(vstorPacket
, sizeof(struct vstor_packet
), 0);
270 vstorPacket
->Operation
= VStorOperationQueryProperties
;
271 vstorPacket
->Flags
= REQUEST_COMPLETION_FLAG
;
272 vstorPacket
->StorageChannelProperties
.PortNumber
=
273 storDevice
->PortNumber
;
275 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
277 sizeof(struct vstor_packet
),
278 (unsigned long)request
,
279 VmbusPacketTypeDataInBand
,
280 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
284 "unable to send QUERY_PROPERTIES_OPERATION");
288 osd_WaitEventWait(request
->WaitEvent
);
290 /* TODO: Check returned version */
291 if (vstorPacket
->Operation
!= VStorOperationCompleteIo
||
292 vstorPacket
->Status
!= 0) {
293 DPRINT_ERR(STORVSC
, "QUERY_PROPERTIES_OPERATION failed "
294 "(op %d status 0x%x)",
295 vstorPacket
->Operation
, vstorPacket
->Status
);
299 storDevice
->PathId
= vstorPacket
->StorageChannelProperties
.PathId
;
300 storDevice
->TargetId
= vstorPacket
->StorageChannelProperties
.TargetId
;
302 DPRINT_DBG(STORVSC
, "channel flag 0x%x, max xfer len 0x%x",
303 vstorPacket
->StorageChannelProperties
.Flags
,
304 vstorPacket
->StorageChannelProperties
.MaxTransferBytes
);
306 DPRINT_INFO(STORVSC
, "END_INITIALIZATION_OPERATION...");
308 memset(vstorPacket
, sizeof(struct vstor_packet
), 0);
309 vstorPacket
->Operation
= VStorOperationEndInitialization
;
310 vstorPacket
->Flags
= REQUEST_COMPLETION_FLAG
;
312 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
314 sizeof(struct vstor_packet
),
315 (unsigned long)request
,
316 VmbusPacketTypeDataInBand
,
317 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
321 "unable to send END_INITIALIZATION_OPERATION");
325 osd_WaitEventWait(request
->WaitEvent
);
327 if (vstorPacket
->Operation
!= VStorOperationCompleteIo
||
328 vstorPacket
->Status
!= 0) {
329 DPRINT_ERR(STORVSC
, "END_INITIALIZATION_OPERATION failed "
330 "(op %d status 0x%x)",
331 vstorPacket
->Operation
, vstorPacket
->Status
);
335 DPRINT_INFO(STORVSC
, "**** storage channel up and running!! ****");
338 kfree(request
->WaitEvent
);
339 request
->WaitEvent
= NULL
;
341 PutStorDevice(Device
);
343 DPRINT_EXIT(STORVSC
);
347 static void StorVscOnIOCompletion(struct hv_device
*Device
,
348 struct vstor_packet
*VStorPacket
,
349 struct storvsc_request_extension
*RequestExt
)
351 struct hv_storvsc_request
*request
;
352 struct storvsc_device
*storDevice
;
354 DPRINT_ENTER(STORVSC
);
356 storDevice
= MustGetStorDevice(Device
);
358 DPRINT_ERR(STORVSC
, "unable to get stor device..."
359 "device being destroyed?");
360 DPRINT_EXIT(STORVSC
);
364 DPRINT_DBG(STORVSC
, "IO_COMPLETE_OPERATION - request extension %p "
365 "completed bytes xfer %u", RequestExt
,
366 VStorPacket
->VmSrb
.DataTransferLength
);
368 ASSERT(RequestExt
!= NULL
);
369 ASSERT(RequestExt
->Request
!= NULL
);
371 request
= RequestExt
->Request
;
373 ASSERT(request
->OnIOCompletion
!= NULL
);
375 /* Copy over the status...etc */
376 request
->Status
= VStorPacket
->VmSrb
.ScsiStatus
;
378 if (request
->Status
!= 0 || VStorPacket
->VmSrb
.SrbStatus
!= 1) {
380 "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
381 request
->Cdb
[0], VStorPacket
->VmSrb
.ScsiStatus
,
382 VStorPacket
->VmSrb
.SrbStatus
);
385 if ((request
->Status
& 0xFF) == 0x02) {
386 /* CHECK_CONDITION */
387 if (VStorPacket
->VmSrb
.SrbStatus
& 0x80) {
388 /* autosense data available */
389 DPRINT_WARN(STORVSC
, "storvsc pkt %p autosense data "
390 "valid - len %d\n", RequestExt
,
391 VStorPacket
->VmSrb
.SenseInfoLength
);
393 ASSERT(VStorPacket
->VmSrb
.SenseInfoLength
<=
394 request
->SenseBufferSize
);
395 memcpy(request
->SenseBuffer
,
396 VStorPacket
->VmSrb
.SenseData
,
397 VStorPacket
->VmSrb
.SenseInfoLength
);
399 request
->SenseBufferSize
=
400 VStorPacket
->VmSrb
.SenseInfoLength
;
405 request
->BytesXfer
= VStorPacket
->VmSrb
.DataTransferLength
;
407 request
->OnIOCompletion(request
);
409 atomic_dec(&storDevice
->NumOutstandingRequests
);
411 PutStorDevice(Device
);
413 DPRINT_EXIT(STORVSC
);
416 static void StorVscOnReceive(struct hv_device
*Device
,
417 struct vstor_packet
*VStorPacket
,
418 struct storvsc_request_extension
*RequestExt
)
420 switch (VStorPacket
->Operation
) {
421 case VStorOperationCompleteIo
:
422 DPRINT_DBG(STORVSC
, "IO_COMPLETE_OPERATION");
423 StorVscOnIOCompletion(Device
, VStorPacket
, RequestExt
);
425 case VStorOperationRemoveDevice
:
426 DPRINT_INFO(STORVSC
, "REMOVE_DEVICE_OPERATION");
431 DPRINT_INFO(STORVSC
, "Unknown operation received - %d",
432 VStorPacket
->Operation
);
437 static void StorVscOnChannelCallback(void *context
)
439 struct hv_device
*device
= (struct hv_device
*)context
;
440 struct storvsc_device
*storDevice
;
443 unsigned char packet
[ALIGN_UP(sizeof(struct vstor_packet
), 8)];
444 struct storvsc_request_extension
*request
;
447 DPRINT_ENTER(STORVSC
);
451 storDevice
= MustGetStorDevice(device
);
453 DPRINT_ERR(STORVSC
, "unable to get stor device..."
454 "device being destroyed?");
455 DPRINT_EXIT(STORVSC
);
460 ret
= device
->Driver
->VmbusChannelInterface
.RecvPacket(device
,
462 ALIGN_UP(sizeof(struct vstor_packet
), 8),
463 &bytesRecvd
, &requestId
);
464 if (ret
== 0 && bytesRecvd
> 0) {
465 DPRINT_DBG(STORVSC
, "receive %d bytes - tid %llx",
466 bytesRecvd
, requestId
);
468 /* ASSERT(bytesRecvd == sizeof(struct vstor_packet)); */
470 request
= (struct storvsc_request_extension
*)
471 (unsigned long)requestId
;
474 /* if (vstorPacket.Flags & SYNTHETIC_FLAG) */
475 if ((request
== &storDevice
->InitRequest
) ||
476 (request
== &storDevice
->ResetRequest
)) {
477 /* DPRINT_INFO(STORVSC,
478 * "reset completion - operation "
480 * vstorPacket.Operation,
481 * vstorPacket.Status); */
483 memcpy(&request
->VStorPacket
, packet
,
484 sizeof(struct vstor_packet
));
486 osd_WaitEventSet(request
->WaitEvent
);
488 StorVscOnReceive(device
,
489 (struct vstor_packet
*)packet
,
493 /* DPRINT_DBG(STORVSC, "nothing else to read..."); */
498 PutStorDevice(device
);
500 DPRINT_EXIT(STORVSC
);
504 static int StorVscConnectToVsp(struct hv_device
*Device
)
506 struct vmstorage_channel_properties props
;
507 struct storvsc_driver_object
*storDriver
;
510 storDriver
= (struct storvsc_driver_object
*)Device
->Driver
;
511 memset(&props
, sizeof(struct vmstorage_channel_properties
), 0);
513 /* Open the channel */
514 ret
= Device
->Driver
->VmbusChannelInterface
.Open(Device
,
515 storDriver
->RingBufferSize
,
516 storDriver
->RingBufferSize
,
518 sizeof(struct vmstorage_channel_properties
),
519 StorVscOnChannelCallback
,
522 DPRINT_DBG(STORVSC
, "storage props: path id %d, tgt id %d, max xfer %d",
523 props
.PathId
, props
.TargetId
, props
.MaxTransferBytes
);
526 DPRINT_ERR(STORVSC
, "unable to open channel: %d", ret
);
530 ret
= StorVscChannelInit(Device
);
536 * StorVscOnDeviceAdd - Callback when the device belonging to this driver is added
538 static int StorVscOnDeviceAdd(struct hv_device
*Device
, void *AdditionalInfo
)
540 struct storvsc_device
*storDevice
;
541 /* struct vmstorage_channel_properties *props; */
542 struct storvsc_device_info
*deviceInfo
;
545 DPRINT_ENTER(STORVSC
);
547 deviceInfo
= (struct storvsc_device_info
*)AdditionalInfo
;
548 storDevice
= AllocStorDevice(Device
);
554 /* Save the channel properties to our storvsc channel */
555 /* props = (struct vmstorage_channel_properties *)
556 * channel->offerMsg.Offer.u.Standard.UserDefined; */
560 * If we support more than 1 scsi channel, we need to set the
561 * port number here to the scsi channel but how do we get the
562 * scsi channel prior to the bus scan
565 /* storChannel->PortNumber = 0;
566 storChannel->PathId = props->PathId;
567 storChannel->TargetId = props->TargetId; */
569 storDevice
->PortNumber
= deviceInfo
->PortNumber
;
570 /* Send it back up */
571 ret
= StorVscConnectToVsp(Device
);
573 /* deviceInfo->PortNumber = storDevice->PortNumber; */
574 deviceInfo
->PathId
= storDevice
->PathId
;
575 deviceInfo
->TargetId
= storDevice
->TargetId
;
577 DPRINT_DBG(STORVSC
, "assigned port %u, path %u target %u\n",
578 storDevice
->PortNumber
, storDevice
->PathId
,
579 storDevice
->TargetId
);
582 DPRINT_EXIT(STORVSC
);
588 * StorVscOnDeviceRemove - Callback when the our device is being removed
590 static int StorVscOnDeviceRemove(struct hv_device
*Device
)
592 struct storvsc_device
*storDevice
;
594 DPRINT_ENTER(STORVSC
);
596 DPRINT_INFO(STORVSC
, "disabling storage device (%p)...",
599 storDevice
= ReleaseStorDevice(Device
);
602 * At this point, all outbound traffic should be disable. We
603 * only allow inbound traffic (responses) to proceed so that
604 * outstanding requests can be completed.
606 while (atomic_read(&storDevice
->NumOutstandingRequests
)) {
607 DPRINT_INFO(STORVSC
, "waiting for %d requests to complete...",
608 atomic_read(&storDevice
->NumOutstandingRequests
));
612 DPRINT_INFO(STORVSC
, "removing storage device (%p)...",
615 storDevice
= FinalReleaseStorDevice(Device
);
617 DPRINT_INFO(STORVSC
, "storage device (%p) safe to remove", storDevice
);
619 /* Close the channel */
620 Device
->Driver
->VmbusChannelInterface
.Close(Device
);
622 FreeStorDevice(storDevice
);
624 DPRINT_EXIT(STORVSC
);
628 static int StorVscOnHostReset(struct hv_device
*Device
)
630 struct storvsc_device
*storDevice
;
631 struct storvsc_request_extension
*request
;
632 struct vstor_packet
*vstorPacket
;
635 DPRINT_ENTER(STORVSC
);
637 DPRINT_INFO(STORVSC
, "resetting host adapter...");
639 storDevice
= GetStorDevice(Device
);
641 DPRINT_ERR(STORVSC
, "unable to get stor device..."
642 "device being destroyed?");
643 DPRINT_EXIT(STORVSC
);
647 request
= &storDevice
->ResetRequest
;
648 vstorPacket
= &request
->VStorPacket
;
650 request
->WaitEvent
= osd_WaitEventCreate();
652 vstorPacket
->Operation
= VStorOperationResetBus
;
653 vstorPacket
->Flags
= REQUEST_COMPLETION_FLAG
;
654 vstorPacket
->VmSrb
.PathId
= storDevice
->PathId
;
656 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
658 sizeof(struct vstor_packet
),
659 (unsigned long)&storDevice
->ResetRequest
,
660 VmbusPacketTypeDataInBand
,
661 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
663 DPRINT_ERR(STORVSC
, "Unable to send reset packet %p ret %d",
668 /* FIXME: Add a timeout */
669 osd_WaitEventWait(request
->WaitEvent
);
671 kfree(request
->WaitEvent
);
672 DPRINT_INFO(STORVSC
, "host adapter reset completed");
675 * At this point, all outstanding requests in the adapter
676 * should have been flushed out and return to us
680 PutStorDevice(Device
);
681 DPRINT_EXIT(STORVSC
);
686 * StorVscOnIORequest - Callback to initiate an I/O request
688 static int StorVscOnIORequest(struct hv_device
*Device
,
689 struct hv_storvsc_request
*Request
)
691 struct storvsc_device
*storDevice
;
692 struct storvsc_request_extension
*requestExtension
;
693 struct vstor_packet
*vstorPacket
;
696 DPRINT_ENTER(STORVSC
);
699 (struct storvsc_request_extension
*)Request
->Extension
;
700 vstorPacket
= &requestExtension
->VStorPacket
;
701 storDevice
= GetStorDevice(Device
);
703 DPRINT_DBG(STORVSC
, "enter - Device %p, DeviceExt %p, Request %p, "
704 "Extension %p", Device
, storDevice
, Request
,
707 DPRINT_DBG(STORVSC
, "req %p len %d bus %d, target %d, lun %d cdblen %d",
708 Request
, Request
->DataBuffer
.Length
, Request
->Bus
,
709 Request
->TargetId
, Request
->LunId
, Request
->CdbLen
);
712 DPRINT_ERR(STORVSC
, "unable to get stor device..."
713 "device being destroyed?");
714 DPRINT_EXIT(STORVSC
);
718 /* print_hex_dump_bytes("", DUMP_PREFIX_NONE, Request->Cdb,
719 * Request->CdbLen); */
721 requestExtension
->Request
= Request
;
722 requestExtension
->Device
= Device
;
724 memset(vstorPacket
, 0 , sizeof(struct vstor_packet
));
726 vstorPacket
->Flags
|= REQUEST_COMPLETION_FLAG
;
728 vstorPacket
->VmSrb
.Length
= sizeof(struct vmscsi_request
);
730 vstorPacket
->VmSrb
.PortNumber
= Request
->Host
;
731 vstorPacket
->VmSrb
.PathId
= Request
->Bus
;
732 vstorPacket
->VmSrb
.TargetId
= Request
->TargetId
;
733 vstorPacket
->VmSrb
.Lun
= Request
->LunId
;
735 vstorPacket
->VmSrb
.SenseInfoLength
= SENSE_BUFFER_SIZE
;
737 /* Copy over the scsi command descriptor block */
738 vstorPacket
->VmSrb
.CdbLength
= Request
->CdbLen
;
739 memcpy(&vstorPacket
->VmSrb
.Cdb
, Request
->Cdb
, Request
->CdbLen
);
741 vstorPacket
->VmSrb
.DataIn
= Request
->Type
;
742 vstorPacket
->VmSrb
.DataTransferLength
= Request
->DataBuffer
.Length
;
744 vstorPacket
->Operation
= VStorOperationExecuteSRB
;
746 DPRINT_DBG(STORVSC
, "srb - len %d port %d, path %d, target %d, "
747 "lun %d senselen %d cdblen %d",
748 vstorPacket
->VmSrb
.Length
,
749 vstorPacket
->VmSrb
.PortNumber
,
750 vstorPacket
->VmSrb
.PathId
,
751 vstorPacket
->VmSrb
.TargetId
,
752 vstorPacket
->VmSrb
.Lun
,
753 vstorPacket
->VmSrb
.SenseInfoLength
,
754 vstorPacket
->VmSrb
.CdbLength
);
756 if (requestExtension
->Request
->DataBuffer
.Length
) {
757 ret
= Device
->Driver
->VmbusChannelInterface
.
758 SendPacketMultiPageBuffer(Device
,
759 &requestExtension
->Request
->DataBuffer
,
761 sizeof(struct vstor_packet
),
762 (unsigned long)requestExtension
);
764 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
766 sizeof(struct vstor_packet
),
767 (unsigned long)requestExtension
,
768 VmbusPacketTypeDataInBand
,
769 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
773 DPRINT_DBG(STORVSC
, "Unable to send packet %p ret %d",
777 atomic_inc(&storDevice
->NumOutstandingRequests
);
779 PutStorDevice(Device
);
781 DPRINT_EXIT(STORVSC
);
786 * StorVscOnCleanup - Perform any cleanup when the driver is removed
788 static void StorVscOnCleanup(struct hv_driver
*Driver
)
790 DPRINT_ENTER(STORVSC
);
791 DPRINT_EXIT(STORVSC
);
795 * StorVscInitialize - Main entry point
797 int StorVscInitialize(struct hv_driver
*Driver
)
799 struct storvsc_driver_object
*storDriver
;
801 DPRINT_ENTER(STORVSC
);
803 storDriver
= (struct storvsc_driver_object
*)Driver
;
805 DPRINT_DBG(STORVSC
, "sizeof(STORVSC_REQUEST)=%zd "
806 "sizeof(struct storvsc_request_extension)=%zd "
807 "sizeof(struct vstor_packet)=%zd, "
808 "sizeof(struct vmscsi_request)=%zd",
809 sizeof(struct hv_storvsc_request
),
810 sizeof(struct storvsc_request_extension
),
811 sizeof(struct vstor_packet
),
812 sizeof(struct vmscsi_request
));
814 /* Make sure we are at least 2 pages since 1 page is used for control */
815 ASSERT(storDriver
->RingBufferSize
>= (PAGE_SIZE
<< 1));
817 Driver
->name
= gDriverName
;
818 memcpy(&Driver
->deviceType
, &gStorVscDeviceType
,
819 sizeof(struct hv_guid
));
821 storDriver
->RequestExtSize
= sizeof(struct storvsc_request_extension
);
824 * Divide the ring buffer data size (which is 1 page less
825 * than the ring buffer size since that page is reserved for
826 * the ring buffer indices) by the max request size (which is
827 * VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER + struct vstor_packet + u64)
829 storDriver
->MaxOutstandingRequestsPerChannel
=
830 ((storDriver
->RingBufferSize
- PAGE_SIZE
) /
831 ALIGN_UP(MAX_MULTIPAGE_BUFFER_PACKET
+
832 sizeof(struct vstor_packet
) + sizeof(u64
),
835 DPRINT_INFO(STORVSC
, "max io %u, currently %u\n",
836 storDriver
->MaxOutstandingRequestsPerChannel
,
837 STORVSC_MAX_IO_REQUESTS
);
839 /* Setup the dispatch table */
840 storDriver
->Base
.OnDeviceAdd
= StorVscOnDeviceAdd
;
841 storDriver
->Base
.OnDeviceRemove
= StorVscOnDeviceRemove
;
842 storDriver
->Base
.OnCleanup
= StorVscOnCleanup
;
844 storDriver
->OnIORequest
= StorVscOnIORequest
;
845 storDriver
->OnHostReset
= StorVscOnHostReset
;
847 DPRINT_EXIT(STORVSC
);