OMAP3: PM: Added DVFS OPP locking interface for VDD1 and VDD2
[linux-ginger.git] / drivers / staging / hv / StorVsc.c
blob14015c9279400436ab558e68ae6d30a5f730fa41
1 /*
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
11 * more details.
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.
17 * Authors:
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/mm.h>
24 #include <linux/delay.h>
25 #include "osd.h"
26 #include "logging.h"
27 #include "StorVscApi.h"
28 #include "VmbusPacketFormat.h"
29 #include "vstorage.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 */
49 atomic_t RefCount;
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;
59 unsigned char PathId;
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 = {
75 .data = {
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);
87 if (!storDevice)
88 return NULL;
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;
97 return storDevice;
100 static inline void FreeStorDevice(struct storvsc_device *Device)
102 ASSERT(atomic_read(&Device->RefCount) == 0);
103 kfree(Device);
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);
114 else
115 storDevice = NULL;
117 return storDevice;
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);
128 else
129 storDevice = NULL;
131 return storDevice;
134 static inline void PutStorDevice(struct hv_device *Device)
136 struct storvsc_device *storDevice;
138 storDevice = (struct storvsc_device *)Device->Extension;
139 ASSERT(storDevice);
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;
151 ASSERT(storDevice);
153 /* Busy wait until the ref drop to 2, then set it to 1 */
154 while (atomic_cmpxchg(&storDevice->RefCount, 2, 1) != 2)
155 udelay(100);
157 return storDevice;
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;
167 ASSERT(storDevice);
169 /* Busy wait until the ref drop to 1, then set it to 0 */
170 while (atomic_cmpxchg(&storDevice->RefCount, 1, 0) != 1)
171 udelay(100);
173 Device->Extension = NULL;
174 return storDevice;
177 static int StorVscChannelInit(struct hv_device *Device)
179 struct storvsc_device *storDevice;
180 struct storvsc_request_extension *request;
181 struct vstor_packet *vstorPacket;
182 int ret;
184 storDevice = GetStorDevice(Device);
185 if (!storDevice) {
186 DPRINT_ERR(STORVSC, "unable to get stor device..."
187 "device being destroyed?");
188 DPRINT_EXIT(STORVSC);
189 return -1;
192 request = &storDevice->InitRequest;
193 vstorPacket = &request->VStorPacket;
196 * Now, initiate the vsc/vsp initialization protocol on the open
197 * channel
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,
212 vstorPacket,
213 sizeof(struct vstor_packet),
214 (unsigned long)request,
215 VmbusPacketTypeDataInBand,
216 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
217 if (ret != 0) {
218 DPRINT_ERR(STORVSC,
219 "unable to send BEGIN_INITIALIZATION_OPERATION");
220 goto Cleanup;
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);
230 goto Cleanup;
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,
244 vstorPacket,
245 sizeof(struct vstor_packet),
246 (unsigned long)request,
247 VmbusPacketTypeDataInBand,
248 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
249 if (ret != 0) {
250 DPRINT_ERR(STORVSC,
251 "unable to send BEGIN_INITIALIZATION_OPERATION");
252 goto Cleanup;
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);
263 goto Cleanup;
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,
276 vstorPacket,
277 sizeof(struct vstor_packet),
278 (unsigned long)request,
279 VmbusPacketTypeDataInBand,
280 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
282 if (ret != 0) {
283 DPRINT_ERR(STORVSC,
284 "unable to send QUERY_PROPERTIES_OPERATION");
285 goto Cleanup;
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);
296 goto Cleanup;
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,
313 vstorPacket,
314 sizeof(struct vstor_packet),
315 (unsigned long)request,
316 VmbusPacketTypeDataInBand,
317 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
319 if (ret != 0) {
320 DPRINT_ERR(STORVSC,
321 "unable to send END_INITIALIZATION_OPERATION");
322 goto Cleanup;
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);
332 goto Cleanup;
335 DPRINT_INFO(STORVSC, "**** storage channel up and running!! ****");
337 Cleanup:
338 kfree(request->WaitEvent);
339 request->WaitEvent = NULL;
341 PutStorDevice(Device);
343 DPRINT_EXIT(STORVSC);
344 return ret;
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);
357 if (!storDevice) {
358 DPRINT_ERR(STORVSC, "unable to get stor device..."
359 "device being destroyed?");
360 DPRINT_EXIT(STORVSC);
361 return;
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) {
379 DPRINT_WARN(STORVSC,
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;
404 /* TODO: */
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);
424 break;
425 case VStorOperationRemoveDevice:
426 DPRINT_INFO(STORVSC, "REMOVE_DEVICE_OPERATION");
427 /* TODO: */
428 break;
430 default:
431 DPRINT_INFO(STORVSC, "Unknown operation received - %d",
432 VStorPacket->Operation);
433 break;
437 static void StorVscOnChannelCallback(void *context)
439 struct hv_device *device = (struct hv_device *)context;
440 struct storvsc_device *storDevice;
441 u32 bytesRecvd;
442 u64 requestId;
443 unsigned char packet[ALIGN_UP(sizeof(struct vstor_packet), 8)];
444 struct storvsc_request_extension *request;
445 int ret;
447 DPRINT_ENTER(STORVSC);
449 ASSERT(device);
451 storDevice = MustGetStorDevice(device);
452 if (!storDevice) {
453 DPRINT_ERR(STORVSC, "unable to get stor device..."
454 "device being destroyed?");
455 DPRINT_EXIT(STORVSC);
456 return;
459 do {
460 ret = device->Driver->VmbusChannelInterface.RecvPacket(device,
461 packet,
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;
472 ASSERT(request);
474 /* if (vstorPacket.Flags & SYNTHETIC_FLAG) */
475 if ((request == &storDevice->InitRequest) ||
476 (request == &storDevice->ResetRequest)) {
477 /* DPRINT_INFO(STORVSC,
478 * "reset completion - operation "
479 * "%u status %u",
480 * vstorPacket.Operation,
481 * vstorPacket.Status); */
483 memcpy(&request->VStorPacket, packet,
484 sizeof(struct vstor_packet));
486 osd_WaitEventSet(request->WaitEvent);
487 } else {
488 StorVscOnReceive(device,
489 (struct vstor_packet *)packet,
490 request);
492 } else {
493 /* DPRINT_DBG(STORVSC, "nothing else to read..."); */
494 break;
496 } while (1);
498 PutStorDevice(device);
500 DPRINT_EXIT(STORVSC);
501 return;
504 static int StorVscConnectToVsp(struct hv_device *Device)
506 struct vmstorage_channel_properties props;
507 struct storvsc_driver_object *storDriver;
508 int ret;
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,
517 (void *)&props,
518 sizeof(struct vmstorage_channel_properties),
519 StorVscOnChannelCallback,
520 Device);
522 DPRINT_DBG(STORVSC, "storage props: path id %d, tgt id %d, max xfer %d",
523 props.PathId, props.TargetId, props.MaxTransferBytes);
525 if (ret != 0) {
526 DPRINT_ERR(STORVSC, "unable to open channel: %d", ret);
527 return -1;
530 ret = StorVscChannelInit(Device);
532 return ret;
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;
543 int ret = 0;
545 DPRINT_ENTER(STORVSC);
547 deviceInfo = (struct storvsc_device_info *)AdditionalInfo;
548 storDevice = AllocStorDevice(Device);
549 if (!storDevice) {
550 ret = -1;
551 goto Cleanup;
554 /* Save the channel properties to our storvsc channel */
555 /* props = (struct vmstorage_channel_properties *)
556 * channel->offerMsg.Offer.u.Standard.UserDefined; */
558 /* FIXME: */
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);
581 Cleanup:
582 DPRINT_EXIT(STORVSC);
584 return ret;
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)...",
597 Device->Extension);
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));
609 udelay(100);
612 DPRINT_INFO(STORVSC, "removing storage device (%p)...",
613 Device->Extension);
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);
625 return 0;
628 static int StorVscOnHostReset(struct hv_device *Device)
630 struct storvsc_device *storDevice;
631 struct storvsc_request_extension *request;
632 struct vstor_packet *vstorPacket;
633 int ret;
635 DPRINT_ENTER(STORVSC);
637 DPRINT_INFO(STORVSC, "resetting host adapter...");
639 storDevice = GetStorDevice(Device);
640 if (!storDevice) {
641 DPRINT_ERR(STORVSC, "unable to get stor device..."
642 "device being destroyed?");
643 DPRINT_EXIT(STORVSC);
644 return -1;
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,
657 vstorPacket,
658 sizeof(struct vstor_packet),
659 (unsigned long)&storDevice->ResetRequest,
660 VmbusPacketTypeDataInBand,
661 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
662 if (ret != 0) {
663 DPRINT_ERR(STORVSC, "Unable to send reset packet %p ret %d",
664 vstorPacket, ret);
665 goto Cleanup;
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
679 Cleanup:
680 PutStorDevice(Device);
681 DPRINT_EXIT(STORVSC);
682 return ret;
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;
694 int ret = 0;
696 DPRINT_ENTER(STORVSC);
698 requestExtension =
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,
705 requestExtension);
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);
711 if (!storDevice) {
712 DPRINT_ERR(STORVSC, "unable to get stor device..."
713 "device being destroyed?");
714 DPRINT_EXIT(STORVSC);
715 return -2;
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,
760 vstorPacket,
761 sizeof(struct vstor_packet),
762 (unsigned long)requestExtension);
763 } else {
764 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
765 vstorPacket,
766 sizeof(struct vstor_packet),
767 (unsigned long)requestExtension,
768 VmbusPacketTypeDataInBand,
769 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
772 if (ret != 0) {
773 DPRINT_DBG(STORVSC, "Unable to send packet %p ret %d",
774 vstorPacket, ret);
777 atomic_inc(&storDevice->NumOutstandingRequests);
779 PutStorDevice(Device);
781 DPRINT_EXIT(STORVSC);
782 return ret;
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),
833 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);
849 return 0;