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 "storvsc_api.h"
29 #include "vmbus_packet_format.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?");
192 request
= &storDevice
->InitRequest
;
193 vstorPacket
= &request
->VStorPacket
;
196 * Now, initiate the vsc/vsp initialization protocol on the open
199 memset(request
, 0, sizeof(struct storvsc_request_extension
));
200 request
->WaitEvent
= osd_WaitEventCreate();
201 if (!request
->WaitEvent
) {
206 vstorPacket
->Operation
= VStorOperationBeginInitialization
;
207 vstorPacket
->Flags
= REQUEST_COMPLETION_FLAG
;
209 /*SpinlockAcquire(gDriverExt.packetListLock);
210 INSERT_TAIL_LIST(&gDriverExt.packetList, &packet->listEntry.entry);
211 SpinlockRelease(gDriverExt.packetListLock);*/
213 DPRINT_INFO(STORVSC
, "BEGIN_INITIALIZATION_OPERATION...");
215 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
217 sizeof(struct vstor_packet
),
218 (unsigned long)request
,
219 VmbusPacketTypeDataInBand
,
220 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
223 "unable to send BEGIN_INITIALIZATION_OPERATION");
227 osd_WaitEventWait(request
->WaitEvent
);
229 if (vstorPacket
->Operation
!= VStorOperationCompleteIo
||
230 vstorPacket
->Status
!= 0) {
231 DPRINT_ERR(STORVSC
, "BEGIN_INITIALIZATION_OPERATION failed "
232 "(op %d status 0x%x)",
233 vstorPacket
->Operation
, vstorPacket
->Status
);
237 DPRINT_INFO(STORVSC
, "QUERY_PROTOCOL_VERSION_OPERATION...");
239 /* reuse the packet for version range supported */
240 memset(vstorPacket
, 0, sizeof(struct vstor_packet
));
241 vstorPacket
->Operation
= VStorOperationQueryProtocolVersion
;
242 vstorPacket
->Flags
= REQUEST_COMPLETION_FLAG
;
244 vstorPacket
->Version
.MajorMinor
= VMSTOR_PROTOCOL_VERSION_CURRENT
;
245 FILL_VMSTOR_REVISION(vstorPacket
->Version
.Revision
);
247 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
249 sizeof(struct vstor_packet
),
250 (unsigned long)request
,
251 VmbusPacketTypeDataInBand
,
252 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
255 "unable to send BEGIN_INITIALIZATION_OPERATION");
259 osd_WaitEventWait(request
->WaitEvent
);
261 /* TODO: Check returned version */
262 if (vstorPacket
->Operation
!= VStorOperationCompleteIo
||
263 vstorPacket
->Status
!= 0) {
264 DPRINT_ERR(STORVSC
, "QUERY_PROTOCOL_VERSION_OPERATION failed "
265 "(op %d status 0x%x)",
266 vstorPacket
->Operation
, vstorPacket
->Status
);
270 /* Query channel properties */
271 DPRINT_INFO(STORVSC
, "QUERY_PROPERTIES_OPERATION...");
273 memset(vstorPacket
, 0, sizeof(struct vstor_packet
));
274 vstorPacket
->Operation
= VStorOperationQueryProperties
;
275 vstorPacket
->Flags
= REQUEST_COMPLETION_FLAG
;
276 vstorPacket
->StorageChannelProperties
.PortNumber
=
277 storDevice
->PortNumber
;
279 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
281 sizeof(struct vstor_packet
),
282 (unsigned long)request
,
283 VmbusPacketTypeDataInBand
,
284 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
288 "unable to send QUERY_PROPERTIES_OPERATION");
292 osd_WaitEventWait(request
->WaitEvent
);
294 /* TODO: Check returned version */
295 if (vstorPacket
->Operation
!= VStorOperationCompleteIo
||
296 vstorPacket
->Status
!= 0) {
297 DPRINT_ERR(STORVSC
, "QUERY_PROPERTIES_OPERATION failed "
298 "(op %d status 0x%x)",
299 vstorPacket
->Operation
, vstorPacket
->Status
);
303 storDevice
->PathId
= vstorPacket
->StorageChannelProperties
.PathId
;
304 storDevice
->TargetId
= vstorPacket
->StorageChannelProperties
.TargetId
;
306 DPRINT_DBG(STORVSC
, "channel flag 0x%x, max xfer len 0x%x",
307 vstorPacket
->StorageChannelProperties
.Flags
,
308 vstorPacket
->StorageChannelProperties
.MaxTransferBytes
);
310 DPRINT_INFO(STORVSC
, "END_INITIALIZATION_OPERATION...");
312 memset(vstorPacket
, 0, sizeof(struct vstor_packet
));
313 vstorPacket
->Operation
= VStorOperationEndInitialization
;
314 vstorPacket
->Flags
= REQUEST_COMPLETION_FLAG
;
316 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
318 sizeof(struct vstor_packet
),
319 (unsigned long)request
,
320 VmbusPacketTypeDataInBand
,
321 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
325 "unable to send END_INITIALIZATION_OPERATION");
329 osd_WaitEventWait(request
->WaitEvent
);
331 if (vstorPacket
->Operation
!= VStorOperationCompleteIo
||
332 vstorPacket
->Status
!= 0) {
333 DPRINT_ERR(STORVSC
, "END_INITIALIZATION_OPERATION failed "
334 "(op %d status 0x%x)",
335 vstorPacket
->Operation
, vstorPacket
->Status
);
339 DPRINT_INFO(STORVSC
, "**** storage channel up and running!! ****");
342 kfree(request
->WaitEvent
);
343 request
->WaitEvent
= NULL
;
345 PutStorDevice(Device
);
349 static void StorVscOnIOCompletion(struct hv_device
*Device
,
350 struct vstor_packet
*VStorPacket
,
351 struct storvsc_request_extension
*RequestExt
)
353 struct hv_storvsc_request
*request
;
354 struct storvsc_device
*storDevice
;
356 storDevice
= MustGetStorDevice(Device
);
358 DPRINT_ERR(STORVSC
, "unable to get stor device..."
359 "device being destroyed?");
363 DPRINT_DBG(STORVSC
, "IO_COMPLETE_OPERATION - request extension %p "
364 "completed bytes xfer %u", RequestExt
,
365 VStorPacket
->VmSrb
.DataTransferLength
);
367 /* ASSERT(RequestExt != NULL); */
368 /* ASSERT(RequestExt->Request != NULL); */
370 request
= RequestExt
->Request
;
372 /* ASSERT(request->OnIOCompletion != NULL); */
374 /* Copy over the status...etc */
375 request
->Status
= VStorPacket
->VmSrb
.ScsiStatus
;
377 if (request
->Status
!= 0 || VStorPacket
->VmSrb
.SrbStatus
!= 1) {
379 "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
380 request
->Cdb
[0], VStorPacket
->VmSrb
.ScsiStatus
,
381 VStorPacket
->VmSrb
.SrbStatus
);
384 if ((request
->Status
& 0xFF) == 0x02) {
385 /* CHECK_CONDITION */
386 if (VStorPacket
->VmSrb
.SrbStatus
& 0x80) {
387 /* autosense data available */
388 DPRINT_WARN(STORVSC
, "storvsc pkt %p autosense data "
389 "valid - len %d\n", RequestExt
,
390 VStorPacket
->VmSrb
.SenseInfoLength
);
392 /* ASSERT(VStorPacket->VmSrb.SenseInfoLength <= */
393 /* request->SenseBufferSize); */
394 memcpy(request
->SenseBuffer
,
395 VStorPacket
->VmSrb
.SenseData
,
396 VStorPacket
->VmSrb
.SenseInfoLength
);
398 request
->SenseBufferSize
=
399 VStorPacket
->VmSrb
.SenseInfoLength
;
404 request
->BytesXfer
= VStorPacket
->VmSrb
.DataTransferLength
;
406 request
->OnIOCompletion(request
);
408 atomic_dec(&storDevice
->NumOutstandingRequests
);
410 PutStorDevice(Device
);
413 static void StorVscOnReceive(struct hv_device
*Device
,
414 struct vstor_packet
*VStorPacket
,
415 struct storvsc_request_extension
*RequestExt
)
417 switch (VStorPacket
->Operation
) {
418 case VStorOperationCompleteIo
:
419 DPRINT_DBG(STORVSC
, "IO_COMPLETE_OPERATION");
420 StorVscOnIOCompletion(Device
, VStorPacket
, RequestExt
);
422 case VStorOperationRemoveDevice
:
423 DPRINT_INFO(STORVSC
, "REMOVE_DEVICE_OPERATION");
428 DPRINT_INFO(STORVSC
, "Unknown operation received - %d",
429 VStorPacket
->Operation
);
434 static void StorVscOnChannelCallback(void *context
)
436 struct hv_device
*device
= (struct hv_device
*)context
;
437 struct storvsc_device
*storDevice
;
440 unsigned char packet
[ALIGN_UP(sizeof(struct vstor_packet
), 8)];
441 struct storvsc_request_extension
*request
;
444 /* ASSERT(device); */
446 storDevice
= MustGetStorDevice(device
);
448 DPRINT_ERR(STORVSC
, "unable to get stor device..."
449 "device being destroyed?");
454 ret
= device
->Driver
->VmbusChannelInterface
.RecvPacket(device
,
456 ALIGN_UP(sizeof(struct vstor_packet
), 8),
457 &bytesRecvd
, &requestId
);
458 if (ret
== 0 && bytesRecvd
> 0) {
459 DPRINT_DBG(STORVSC
, "receive %d bytes - tid %llx",
460 bytesRecvd
, requestId
);
462 /* ASSERT(bytesRecvd == sizeof(struct vstor_packet)); */
464 request
= (struct storvsc_request_extension
*)
465 (unsigned long)requestId
;
466 /* ASSERT(request);c */
468 /* if (vstorPacket.Flags & SYNTHETIC_FLAG) */
469 if ((request
== &storDevice
->InitRequest
) ||
470 (request
== &storDevice
->ResetRequest
)) {
471 /* DPRINT_INFO(STORVSC,
472 * "reset completion - operation "
474 * vstorPacket.Operation,
475 * vstorPacket.Status); */
477 memcpy(&request
->VStorPacket
, packet
,
478 sizeof(struct vstor_packet
));
480 osd_WaitEventSet(request
->WaitEvent
);
482 StorVscOnReceive(device
,
483 (struct vstor_packet
*)packet
,
487 /* DPRINT_DBG(STORVSC, "nothing else to read..."); */
492 PutStorDevice(device
);
496 static int StorVscConnectToVsp(struct hv_device
*Device
)
498 struct vmstorage_channel_properties props
;
499 struct storvsc_driver_object
*storDriver
;
502 storDriver
= (struct storvsc_driver_object
*)Device
->Driver
;
503 memset(&props
, 0, sizeof(struct vmstorage_channel_properties
));
505 /* Open the channel */
506 ret
= Device
->Driver
->VmbusChannelInterface
.Open(Device
,
507 storDriver
->RingBufferSize
,
508 storDriver
->RingBufferSize
,
510 sizeof(struct vmstorage_channel_properties
),
511 StorVscOnChannelCallback
,
514 DPRINT_DBG(STORVSC
, "storage props: path id %d, tgt id %d, max xfer %d",
515 props
.PathId
, props
.TargetId
, props
.MaxTransferBytes
);
518 DPRINT_ERR(STORVSC
, "unable to open channel: %d", ret
);
522 ret
= StorVscChannelInit(Device
);
528 * StorVscOnDeviceAdd - Callback when the device belonging to this driver is added
530 static int StorVscOnDeviceAdd(struct hv_device
*Device
, void *AdditionalInfo
)
532 struct storvsc_device
*storDevice
;
533 /* struct vmstorage_channel_properties *props; */
534 struct storvsc_device_info
*deviceInfo
;
537 deviceInfo
= (struct storvsc_device_info
*)AdditionalInfo
;
538 storDevice
= AllocStorDevice(Device
);
544 /* Save the channel properties to our storvsc channel */
545 /* props = (struct vmstorage_channel_properties *)
546 * channel->offerMsg.Offer.u.Standard.UserDefined; */
549 * If we support more than 1 scsi channel, we need to set the
550 * port number here to the scsi channel but how do we get the
551 * scsi channel prior to the bus scan
554 /* storChannel->PortNumber = 0;
555 storChannel->PathId = props->PathId;
556 storChannel->TargetId = props->TargetId; */
558 storDevice
->PortNumber
= deviceInfo
->PortNumber
;
559 /* Send it back up */
560 ret
= StorVscConnectToVsp(Device
);
562 /* deviceInfo->PortNumber = storDevice->PortNumber; */
563 deviceInfo
->PathId
= storDevice
->PathId
;
564 deviceInfo
->TargetId
= storDevice
->TargetId
;
566 DPRINT_DBG(STORVSC
, "assigned port %u, path %u target %u\n",
567 storDevice
->PortNumber
, storDevice
->PathId
,
568 storDevice
->TargetId
);
575 * StorVscOnDeviceRemove - Callback when the our device is being removed
577 static int StorVscOnDeviceRemove(struct hv_device
*Device
)
579 struct storvsc_device
*storDevice
;
581 DPRINT_INFO(STORVSC
, "disabling storage device (%p)...",
584 storDevice
= ReleaseStorDevice(Device
);
587 * At this point, all outbound traffic should be disable. We
588 * only allow inbound traffic (responses) to proceed so that
589 * outstanding requests can be completed.
591 while (atomic_read(&storDevice
->NumOutstandingRequests
)) {
592 DPRINT_INFO(STORVSC
, "waiting for %d requests to complete...",
593 atomic_read(&storDevice
->NumOutstandingRequests
));
597 DPRINT_INFO(STORVSC
, "removing storage device (%p)...",
600 storDevice
= FinalReleaseStorDevice(Device
);
602 DPRINT_INFO(STORVSC
, "storage device (%p) safe to remove", storDevice
);
604 /* Close the channel */
605 Device
->Driver
->VmbusChannelInterface
.Close(Device
);
607 FreeStorDevice(storDevice
);
611 int StorVscOnHostReset(struct hv_device
*Device
)
613 struct storvsc_device
*storDevice
;
614 struct storvsc_request_extension
*request
;
615 struct vstor_packet
*vstorPacket
;
618 DPRINT_INFO(STORVSC
, "resetting host adapter...");
620 storDevice
= GetStorDevice(Device
);
622 DPRINT_ERR(STORVSC
, "unable to get stor device..."
623 "device being destroyed?");
627 request
= &storDevice
->ResetRequest
;
628 vstorPacket
= &request
->VStorPacket
;
630 request
->WaitEvent
= osd_WaitEventCreate();
631 if (!request
->WaitEvent
) {
636 vstorPacket
->Operation
= VStorOperationResetBus
;
637 vstorPacket
->Flags
= REQUEST_COMPLETION_FLAG
;
638 vstorPacket
->VmSrb
.PathId
= storDevice
->PathId
;
640 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
642 sizeof(struct vstor_packet
),
643 (unsigned long)&storDevice
->ResetRequest
,
644 VmbusPacketTypeDataInBand
,
645 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
647 DPRINT_ERR(STORVSC
, "Unable to send reset packet %p ret %d",
652 osd_WaitEventWait(request
->WaitEvent
);
654 kfree(request
->WaitEvent
);
655 DPRINT_INFO(STORVSC
, "host adapter reset completed");
658 * At this point, all outstanding requests in the adapter
659 * should have been flushed out and return to us
663 PutStorDevice(Device
);
668 * StorVscOnIORequest - Callback to initiate an I/O request
670 static int StorVscOnIORequest(struct hv_device
*Device
,
671 struct hv_storvsc_request
*Request
)
673 struct storvsc_device
*storDevice
;
674 struct storvsc_request_extension
*requestExtension
;
675 struct vstor_packet
*vstorPacket
;
679 (struct storvsc_request_extension
*)Request
->Extension
;
680 vstorPacket
= &requestExtension
->VStorPacket
;
681 storDevice
= GetStorDevice(Device
);
683 DPRINT_DBG(STORVSC
, "enter - Device %p, DeviceExt %p, Request %p, "
684 "Extension %p", Device
, storDevice
, Request
,
687 DPRINT_DBG(STORVSC
, "req %p len %d bus %d, target %d, lun %d cdblen %d",
688 Request
, Request
->DataBuffer
.Length
, Request
->Bus
,
689 Request
->TargetId
, Request
->LunId
, Request
->CdbLen
);
692 DPRINT_ERR(STORVSC
, "unable to get stor device..."
693 "device being destroyed?");
697 /* print_hex_dump_bytes("", DUMP_PREFIX_NONE, Request->Cdb,
698 * Request->CdbLen); */
700 requestExtension
->Request
= Request
;
701 requestExtension
->Device
= Device
;
703 memset(vstorPacket
, 0 , sizeof(struct vstor_packet
));
705 vstorPacket
->Flags
|= REQUEST_COMPLETION_FLAG
;
707 vstorPacket
->VmSrb
.Length
= sizeof(struct vmscsi_request
);
709 vstorPacket
->VmSrb
.PortNumber
= Request
->Host
;
710 vstorPacket
->VmSrb
.PathId
= Request
->Bus
;
711 vstorPacket
->VmSrb
.TargetId
= Request
->TargetId
;
712 vstorPacket
->VmSrb
.Lun
= Request
->LunId
;
714 vstorPacket
->VmSrb
.SenseInfoLength
= SENSE_BUFFER_SIZE
;
716 /* Copy over the scsi command descriptor block */
717 vstorPacket
->VmSrb
.CdbLength
= Request
->CdbLen
;
718 memcpy(&vstorPacket
->VmSrb
.Cdb
, Request
->Cdb
, Request
->CdbLen
);
720 vstorPacket
->VmSrb
.DataIn
= Request
->Type
;
721 vstorPacket
->VmSrb
.DataTransferLength
= Request
->DataBuffer
.Length
;
723 vstorPacket
->Operation
= VStorOperationExecuteSRB
;
725 DPRINT_DBG(STORVSC
, "srb - len %d port %d, path %d, target %d, "
726 "lun %d senselen %d cdblen %d",
727 vstorPacket
->VmSrb
.Length
,
728 vstorPacket
->VmSrb
.PortNumber
,
729 vstorPacket
->VmSrb
.PathId
,
730 vstorPacket
->VmSrb
.TargetId
,
731 vstorPacket
->VmSrb
.Lun
,
732 vstorPacket
->VmSrb
.SenseInfoLength
,
733 vstorPacket
->VmSrb
.CdbLength
);
735 if (requestExtension
->Request
->DataBuffer
.Length
) {
736 ret
= Device
->Driver
->VmbusChannelInterface
.
737 SendPacketMultiPageBuffer(Device
,
738 &requestExtension
->Request
->DataBuffer
,
740 sizeof(struct vstor_packet
),
741 (unsigned long)requestExtension
);
743 ret
= Device
->Driver
->VmbusChannelInterface
.SendPacket(Device
,
745 sizeof(struct vstor_packet
),
746 (unsigned long)requestExtension
,
747 VmbusPacketTypeDataInBand
,
748 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
752 DPRINT_DBG(STORVSC
, "Unable to send packet %p ret %d",
756 atomic_inc(&storDevice
->NumOutstandingRequests
);
758 PutStorDevice(Device
);
763 * StorVscOnCleanup - Perform any cleanup when the driver is removed
765 static void StorVscOnCleanup(struct hv_driver
*Driver
)
770 * StorVscInitialize - Main entry point
772 int StorVscInitialize(struct hv_driver
*Driver
)
774 struct storvsc_driver_object
*storDriver
;
776 storDriver
= (struct storvsc_driver_object
*)Driver
;
778 DPRINT_DBG(STORVSC
, "sizeof(STORVSC_REQUEST)=%zd "
779 "sizeof(struct storvsc_request_extension)=%zd "
780 "sizeof(struct vstor_packet)=%zd, "
781 "sizeof(struct vmscsi_request)=%zd",
782 sizeof(struct hv_storvsc_request
),
783 sizeof(struct storvsc_request_extension
),
784 sizeof(struct vstor_packet
),
785 sizeof(struct vmscsi_request
));
787 /* Make sure we are at least 2 pages since 1 page is used for control */
788 /* ASSERT(storDriver->RingBufferSize >= (PAGE_SIZE << 1)); */
790 Driver
->name
= gDriverName
;
791 memcpy(&Driver
->deviceType
, &gStorVscDeviceType
,
792 sizeof(struct hv_guid
));
794 storDriver
->RequestExtSize
= sizeof(struct storvsc_request_extension
);
797 * Divide the ring buffer data size (which is 1 page less
798 * than the ring buffer size since that page is reserved for
799 * the ring buffer indices) by the max request size (which is
800 * VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER + struct vstor_packet + u64)
802 storDriver
->MaxOutstandingRequestsPerChannel
=
803 ((storDriver
->RingBufferSize
- PAGE_SIZE
) /
804 ALIGN_UP(MAX_MULTIPAGE_BUFFER_PACKET
+
805 sizeof(struct vstor_packet
) + sizeof(u64
),
808 DPRINT_INFO(STORVSC
, "max io %u, currently %u\n",
809 storDriver
->MaxOutstandingRequestsPerChannel
,
810 STORVSC_MAX_IO_REQUESTS
);
812 /* Setup the dispatch table */
813 storDriver
->Base
.OnDeviceAdd
= StorVscOnDeviceAdd
;
814 storDriver
->Base
.OnDeviceRemove
= StorVscOnDeviceRemove
;
815 storDriver
->Base
.OnCleanup
= StorVscOnCleanup
;
817 storDriver
->OnIORequest
= StorVscOnIORequest
;