Staging: hv: properly fix the printk() warnings
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / hv / StorVsc.c
blob4a406f0ea67f9d186f7ed53cbc862cbc9bf72fe4
1 /*
3 * Copyright (c) 2009, Microsoft Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16 * Place - Suite 330, Boston, MA 02111-1307 USA.
18 * Authors:
19 * Haiyang Zhang <haiyangz@microsoft.com>
20 * Hank Janssen <hjanssen@microsoft.com>
24 #include <linux/kernel.h>
25 #include <linux/string.h>
26 #include <linux/mm.h>
27 #include <linux/delay.h>
28 #include "include/logging.h"
30 #include "include/StorVscApi.h"
31 #include "include/VmbusPacketFormat.h"
32 #include "include/vstorage.h"
36 /* #defines */
40 /* Data types */
43 typedef struct _STORVSC_REQUEST_EXTENSION {
44 /* LIST_ENTRY ListEntry; */
46 STORVSC_REQUEST *Request;
47 struct hv_device *Device;
49 /* Synchronize the request/response if needed */
50 struct osd_waitevent *WaitEvent;
52 VSTOR_PACKET VStorPacket;
53 } STORVSC_REQUEST_EXTENSION;
56 /* A storvsc device is a device object that contains a vmbus channel */
57 typedef struct _STORVSC_DEVICE{
58 struct hv_device *Device;
60 int RefCount; /* 0 indicates the device is being destroyed */
62 int NumOutstandingRequests;
65 * Each unique Port/Path/Target represents 1 channel ie scsi
66 * controller. In reality, the pathid, targetid is always 0
67 * and the port is set by us
69 unsigned int PortNumber;
70 unsigned char PathId;
71 unsigned char TargetId;
73 /* LIST_ENTRY OutstandingRequestList; */
74 /* HANDLE OutstandingRequestLock; */
76 /* Used for vsc/vsp channel reset process */
77 STORVSC_REQUEST_EXTENSION InitRequest;
79 STORVSC_REQUEST_EXTENSION ResetRequest;
81 } STORVSC_DEVICE;
85 /* Globals */
87 static const char* gDriverName="storvsc";
89 /* {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f} */
90 static const GUID gStorVscDeviceType={
91 .Data = {0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d, 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f}
95 /* Internal routines */
97 static int
98 StorVscOnDeviceAdd(
99 struct hv_device *Device,
100 void *AdditionalInfo
103 static int
104 StorVscOnDeviceRemove(
105 struct hv_device *Device
108 static int
109 StorVscOnIORequest(
110 struct hv_device *Device,
111 STORVSC_REQUEST *Request
114 static int
115 StorVscOnHostReset(
116 struct hv_device *Device
119 static void
120 StorVscOnCleanup(
121 DRIVER_OBJECT *Device
124 static void
125 StorVscOnChannelCallback(
126 void * Context
129 static void
130 StorVscOnIOCompletion(
131 struct hv_device *Device,
132 VSTOR_PACKET *VStorPacket,
133 STORVSC_REQUEST_EXTENSION *RequestExt
136 static void
137 StorVscOnReceive(
138 struct hv_device *Device,
139 VSTOR_PACKET *VStorPacket,
140 STORVSC_REQUEST_EXTENSION *RequestExt
143 static int
144 StorVscConnectToVsp(
145 struct hv_device *Device
148 static inline STORVSC_DEVICE* AllocStorDevice(struct hv_device *Device)
150 STORVSC_DEVICE *storDevice;
152 storDevice = kzalloc(sizeof(STORVSC_DEVICE), GFP_KERNEL);
153 if (!storDevice)
154 return NULL;
156 /* Set to 2 to allow both inbound and outbound traffics */
157 /* (ie GetStorDevice() and MustGetStorDevice()) to proceed. */
158 InterlockedCompareExchange(&storDevice->RefCount, 2, 0);
160 storDevice->Device = Device;
161 Device->Extension = storDevice;
163 return storDevice;
166 static inline void FreeStorDevice(STORVSC_DEVICE *Device)
168 ASSERT(Device->RefCount == 0);
169 kfree(Device);
172 /* Get the stordevice object iff exists and its refcount > 1 */
173 static inline STORVSC_DEVICE* GetStorDevice(struct hv_device *Device)
175 STORVSC_DEVICE *storDevice;
177 storDevice = (STORVSC_DEVICE*)Device->Extension;
178 if (storDevice && storDevice->RefCount > 1)
180 InterlockedIncrement(&storDevice->RefCount);
182 else
184 storDevice = NULL;
187 return storDevice;
190 /* Get the stordevice object iff exists and its refcount > 0 */
191 static inline STORVSC_DEVICE* MustGetStorDevice(struct hv_device *Device)
193 STORVSC_DEVICE *storDevice;
195 storDevice = (STORVSC_DEVICE*)Device->Extension;
196 if (storDevice && storDevice->RefCount)
198 InterlockedIncrement(&storDevice->RefCount);
200 else
202 storDevice = NULL;
205 return storDevice;
208 static inline void PutStorDevice(struct hv_device *Device)
210 STORVSC_DEVICE *storDevice;
212 storDevice = (STORVSC_DEVICE*)Device->Extension;
213 ASSERT(storDevice);
215 InterlockedDecrement(&storDevice->RefCount);
216 ASSERT(storDevice->RefCount);
219 /* Drop ref count to 1 to effectively disable GetStorDevice() */
220 static inline STORVSC_DEVICE* ReleaseStorDevice(struct hv_device *Device)
222 STORVSC_DEVICE *storDevice;
224 storDevice = (STORVSC_DEVICE*)Device->Extension;
225 ASSERT(storDevice);
227 /* Busy wait until the ref drop to 2, then set it to 1 */
228 while (InterlockedCompareExchange(&storDevice->RefCount, 1, 2) != 2)
230 udelay(100);
233 return storDevice;
236 /* Drop ref count to 0. No one can use StorDevice object. */
237 static inline STORVSC_DEVICE* FinalReleaseStorDevice(struct hv_device *Device)
239 STORVSC_DEVICE *storDevice;
241 storDevice = (STORVSC_DEVICE*)Device->Extension;
242 ASSERT(storDevice);
244 /* Busy wait until the ref drop to 1, then set it to 0 */
245 while (InterlockedCompareExchange(&storDevice->RefCount, 0, 1) != 1)
247 udelay(100);
250 Device->Extension = NULL;
251 return storDevice;
254 /*++;
257 Name:
258 StorVscInitialize()
260 Description:
261 Main entry point
263 --*/
265 StorVscInitialize(
266 DRIVER_OBJECT *Driver
269 STORVSC_DRIVER_OBJECT* storDriver = (STORVSC_DRIVER_OBJECT*)Driver;
270 int ret=0;
272 DPRINT_ENTER(STORVSC);
274 DPRINT_DBG(STORVSC, "sizeof(STORVSC_REQUEST)=%zd sizeof(STORVSC_REQUEST_EXTENSION)=%zd sizeof(VSTOR_PACKET)=%zd, sizeof(VMSCSI_REQUEST)=%zd",
275 sizeof(STORVSC_REQUEST), sizeof(STORVSC_REQUEST_EXTENSION), sizeof(VSTOR_PACKET), sizeof(VMSCSI_REQUEST));
277 /* Make sure we are at least 2 pages since 1 page is used for control */
278 ASSERT(storDriver->RingBufferSize >= (PAGE_SIZE << 1));
280 Driver->name = gDriverName;
281 memcpy(&Driver->deviceType, &gStorVscDeviceType, sizeof(GUID));
283 storDriver->RequestExtSize = sizeof(STORVSC_REQUEST_EXTENSION);
286 * Divide the ring buffer data size (which is 1 page less
287 * than the ring buffer size since that page is reserved for
288 * the ring buffer indices) by the max request size (which is
289 * VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER + VSTOR_PACKET + u64)
291 storDriver->MaxOutstandingRequestsPerChannel =
292 ((storDriver->RingBufferSize - PAGE_SIZE) / ALIGN_UP(MAX_MULTIPAGE_BUFFER_PACKET + sizeof(VSTOR_PACKET) + sizeof(u64),sizeof(u64)));
294 DPRINT_INFO(STORVSC, "max io %u, currently %u\n", storDriver->MaxOutstandingRequestsPerChannel, STORVSC_MAX_IO_REQUESTS);
296 /* Setup the dispatch table */
297 storDriver->Base.OnDeviceAdd = StorVscOnDeviceAdd;
298 storDriver->Base.OnDeviceRemove = StorVscOnDeviceRemove;
299 storDriver->Base.OnCleanup = StorVscOnCleanup;
301 storDriver->OnIORequest = StorVscOnIORequest;
302 storDriver->OnHostReset = StorVscOnHostReset;
304 DPRINT_EXIT(STORVSC);
306 return ret;
309 /*++
311 Name:
312 StorVscOnDeviceAdd()
314 Description:
315 Callback when the device belonging to this driver is added
317 --*/
319 StorVscOnDeviceAdd(
320 struct hv_device *Device,
321 void *AdditionalInfo
324 int ret=0;
325 STORVSC_DEVICE *storDevice;
326 /* VMSTORAGE_CHANNEL_PROPERTIES *props; */
327 STORVSC_DEVICE_INFO *deviceInfo = (STORVSC_DEVICE_INFO*)AdditionalInfo;
329 DPRINT_ENTER(STORVSC);
331 storDevice = AllocStorDevice(Device);
332 if (!storDevice)
334 ret = -1;
335 goto Cleanup;
338 /* Save the channel properties to our storvsc channel */
339 /* props = (VMSTORAGE_CHANNEL_PROPERTIES*) channel->offerMsg.Offer.u.Standard.UserDefined; */
341 /* FIXME: */
343 * If we support more than 1 scsi channel, we need to set the
344 * port number here to the scsi channel but how do we get the
345 * scsi channel prior to the bus scan
348 /* storChannel->PortNumber = 0;
349 storChannel->PathId = props->PathId;
350 storChannel->TargetId = props->TargetId; */
352 storDevice->PortNumber = deviceInfo->PortNumber;
353 /* Send it back up */
354 ret = StorVscConnectToVsp(Device);
356 /* deviceInfo->PortNumber = storDevice->PortNumber; */
357 deviceInfo->PathId = storDevice->PathId;
358 deviceInfo->TargetId = storDevice->TargetId;
360 DPRINT_DBG(STORVSC, "assigned port %u, path %u target %u\n", storDevice->PortNumber, storDevice->PathId, storDevice->TargetId);
362 Cleanup:
363 DPRINT_EXIT(STORVSC);
365 return ret;
368 static int StorVscChannelInit(struct hv_device *Device)
370 int ret=0;
371 STORVSC_DEVICE *storDevice;
372 STORVSC_REQUEST_EXTENSION *request;
373 VSTOR_PACKET *vstorPacket;
375 storDevice = GetStorDevice(Device);
376 if (!storDevice)
378 DPRINT_ERR(STORVSC, "unable to get stor device...device being destroyed?");
379 DPRINT_EXIT(STORVSC);
380 return -1;
383 request = &storDevice->InitRequest;
384 vstorPacket = &request->VStorPacket;
386 /* Now, initiate the vsc/vsp initialization protocol on the open channel */
388 memset(request, sizeof(STORVSC_REQUEST_EXTENSION), 0);
389 request->WaitEvent = WaitEventCreate();
391 vstorPacket->Operation = VStorOperationBeginInitialization;
392 vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
394 /*SpinlockAcquire(gDriverExt.packetListLock);
395 INSERT_TAIL_LIST(&gDriverExt.packetList, &packet->listEntry.entry);
396 SpinlockRelease(gDriverExt.packetListLock);*/
398 DPRINT_INFO(STORVSC, "BEGIN_INITIALIZATION_OPERATION...");
400 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
401 vstorPacket,
402 sizeof(VSTOR_PACKET),
403 (unsigned long)request,
404 VmbusPacketTypeDataInBand,
405 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
406 if ( ret != 0)
408 DPRINT_ERR(STORVSC, "unable to send BEGIN_INITIALIZATION_OPERATION");
409 goto Cleanup;
412 WaitEventWait(request->WaitEvent);
414 if (vstorPacket->Operation != VStorOperationCompleteIo || vstorPacket->Status != 0)
416 DPRINT_ERR(STORVSC, "BEGIN_INITIALIZATION_OPERATION failed (op %d status 0x%x)", vstorPacket->Operation, vstorPacket->Status);
417 goto Cleanup;
420 DPRINT_INFO(STORVSC, "QUERY_PROTOCOL_VERSION_OPERATION...");
422 /* reuse the packet for version range supported */
423 memset(vstorPacket, sizeof(VSTOR_PACKET), 0);
424 vstorPacket->Operation = VStorOperationQueryProtocolVersion;
425 vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
427 vstorPacket->Version.MajorMinor = VMSTOR_PROTOCOL_VERSION_CURRENT;
428 FILL_VMSTOR_REVISION(vstorPacket->Version.Revision);
430 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
431 vstorPacket,
432 sizeof(VSTOR_PACKET),
433 (unsigned long)request,
434 VmbusPacketTypeDataInBand,
435 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
436 if ( ret != 0)
438 DPRINT_ERR(STORVSC, "unable to send BEGIN_INITIALIZATION_OPERATION");
439 goto Cleanup;
442 WaitEventWait(request->WaitEvent);
444 /* TODO: Check returned version */
445 if (vstorPacket->Operation != VStorOperationCompleteIo || vstorPacket->Status != 0)
447 DPRINT_ERR(STORVSC, "QUERY_PROTOCOL_VERSION_OPERATION failed (op %d status 0x%x)", vstorPacket->Operation, vstorPacket->Status);
448 goto Cleanup;
451 /* Query channel properties */
452 DPRINT_INFO(STORVSC, "QUERY_PROPERTIES_OPERATION...");
454 memset(vstorPacket, sizeof(VSTOR_PACKET), 0);
455 vstorPacket->Operation = VStorOperationQueryProperties;
456 vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
457 vstorPacket->StorageChannelProperties.PortNumber = storDevice->PortNumber;
459 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
460 vstorPacket,
461 sizeof(VSTOR_PACKET),
462 (unsigned long)request,
463 VmbusPacketTypeDataInBand,
464 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
466 if ( ret != 0)
468 DPRINT_ERR(STORVSC, "unable to send QUERY_PROPERTIES_OPERATION");
469 goto Cleanup;
472 WaitEventWait(request->WaitEvent);
474 /* TODO: Check returned version */
475 if (vstorPacket->Operation != VStorOperationCompleteIo || vstorPacket->Status != 0)
477 DPRINT_ERR(STORVSC, "QUERY_PROPERTIES_OPERATION failed (op %d status 0x%x)", vstorPacket->Operation, vstorPacket->Status);
478 goto Cleanup;
481 /* storDevice->PortNumber = vstorPacket->StorageChannelProperties.PortNumber; */
482 storDevice->PathId = vstorPacket->StorageChannelProperties.PathId;
483 storDevice->TargetId = vstorPacket->StorageChannelProperties.TargetId;
485 DPRINT_DBG(STORVSC, "channel flag 0x%x, max xfer len 0x%x", vstorPacket->StorageChannelProperties.Flags, vstorPacket->StorageChannelProperties.MaxTransferBytes);
487 DPRINT_INFO(STORVSC, "END_INITIALIZATION_OPERATION...");
489 memset(vstorPacket, sizeof(VSTOR_PACKET), 0);
490 vstorPacket->Operation = VStorOperationEndInitialization;
491 vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
493 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
494 vstorPacket,
495 sizeof(VSTOR_PACKET),
496 (unsigned long)request,
497 VmbusPacketTypeDataInBand,
498 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
500 if ( ret != 0)
502 DPRINT_ERR(STORVSC, "unable to send END_INITIALIZATION_OPERATION");
503 goto Cleanup;
506 WaitEventWait(request->WaitEvent);
508 if (vstorPacket->Operation != VStorOperationCompleteIo || vstorPacket->Status != 0)
510 DPRINT_ERR(STORVSC, "END_INITIALIZATION_OPERATION failed (op %d status 0x%x)", vstorPacket->Operation, vstorPacket->Status);
511 goto Cleanup;
514 DPRINT_INFO(STORVSC, "**** storage channel up and running!! ****");
516 Cleanup:
517 if (request->WaitEvent)
519 WaitEventClose(request->WaitEvent);
520 request->WaitEvent = NULL;
523 PutStorDevice(Device);
525 DPRINT_EXIT(STORVSC);
526 return ret;
531 StorVscConnectToVsp(
532 struct hv_device *Device
535 int ret=0;
536 VMSTORAGE_CHANNEL_PROPERTIES props;
538 STORVSC_DRIVER_OBJECT *storDriver = (STORVSC_DRIVER_OBJECT*) Device->Driver;;
540 memset(&props, sizeof(VMSTORAGE_CHANNEL_PROPERTIES), 0);
542 /* Open the channel */
543 ret = Device->Driver->VmbusChannelInterface.Open(Device,
544 storDriver->RingBufferSize,
545 storDriver->RingBufferSize,
546 (void *)&props,
547 sizeof(VMSTORAGE_CHANNEL_PROPERTIES),
548 StorVscOnChannelCallback,
549 Device
552 DPRINT_DBG(STORVSC, "storage props: path id %d, tgt id %d, max xfer %d", props.PathId, props.TargetId, props.MaxTransferBytes);
554 if (ret != 0)
556 DPRINT_ERR(STORVSC, "unable to open channel: %d", ret);
557 return -1;
560 ret = StorVscChannelInit(Device);
562 return ret;
566 /*++
568 Name:
569 StorVscOnDeviceRemove()
571 Description:
572 Callback when the our device is being removed
574 --*/
576 StorVscOnDeviceRemove(
577 struct hv_device *Device
580 STORVSC_DEVICE *storDevice;
581 int ret=0;
583 DPRINT_ENTER(STORVSC);
585 DPRINT_INFO(STORVSC, "disabling storage device (%p)...", Device->Extension);
587 storDevice = ReleaseStorDevice(Device);
590 * At this point, all outbound traffic should be disable. We
591 * only allow inbound traffic (responses) to proceed so that
592 * outstanding requests can be completed.
594 while (storDevice->NumOutstandingRequests)
596 DPRINT_INFO(STORVSC, "waiting for %d requests to complete...", storDevice->NumOutstandingRequests);
598 udelay(100);
601 DPRINT_INFO(STORVSC, "removing storage device (%p)...", Device->Extension);
603 storDevice = FinalReleaseStorDevice(Device);
605 DPRINT_INFO(STORVSC, "storage device (%p) safe to remove", storDevice);
607 /* Close the channel */
608 Device->Driver->VmbusChannelInterface.Close(Device);
610 FreeStorDevice(storDevice);
612 DPRINT_EXIT(STORVSC);
613 return ret;
616 /* ***************
617 static void
618 StorVscOnTargetRescan(
619 void *Context
622 struct hv_device *device=(struct hv_device *)Context;
623 STORVSC_DRIVER_OBJECT *storDriver;
625 DPRINT_ENTER(STORVSC);
627 storDriver = (STORVSC_DRIVER_OBJECT*) device->Driver;
628 storDriver->OnHostRescan(device);
630 DPRINT_EXIT(STORVSC);
632 *********** */
635 StorVscOnHostReset(
636 struct hv_device *Device
639 int ret=0;
641 STORVSC_DEVICE *storDevice;
642 STORVSC_REQUEST_EXTENSION *request;
643 VSTOR_PACKET *vstorPacket;
645 DPRINT_ENTER(STORVSC);
647 DPRINT_INFO(STORVSC, "resetting host adapter...");
649 storDevice = GetStorDevice(Device);
650 if (!storDevice)
652 DPRINT_ERR(STORVSC, "unable to get stor device...device being destroyed?");
653 DPRINT_EXIT(STORVSC);
654 return -1;
657 request = &storDevice->ResetRequest;
658 vstorPacket = &request->VStorPacket;
660 request->WaitEvent = WaitEventCreate();
662 vstorPacket->Operation = VStorOperationResetBus;
663 vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
664 vstorPacket->VmSrb.PathId = storDevice->PathId;
666 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
667 vstorPacket,
668 sizeof(VSTOR_PACKET),
669 (unsigned long)&storDevice->ResetRequest,
670 VmbusPacketTypeDataInBand,
671 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
672 if (ret != 0)
674 DPRINT_ERR(STORVSC, "Unable to send reset packet %p ret %d", vstorPacket, ret);
675 goto Cleanup;
678 /* FIXME: Add a timeout */
679 WaitEventWait(request->WaitEvent);
681 WaitEventClose(request->WaitEvent);
682 DPRINT_INFO(STORVSC, "host adapter reset completed");
685 * At this point, all outstanding requests in the adapter
686 * should have been flushed out and return to us
689 Cleanup:
690 PutStorDevice(Device);
691 DPRINT_EXIT(STORVSC);
692 return ret;
695 /*++
697 Name:
698 StorVscOnIORequest()
700 Description:
701 Callback to initiate an I/O request
703 --*/
705 StorVscOnIORequest(
706 struct hv_device *Device,
707 STORVSC_REQUEST *Request
710 STORVSC_DEVICE *storDevice;
711 STORVSC_REQUEST_EXTENSION* requestExtension = (STORVSC_REQUEST_EXTENSION*) Request->Extension;
712 VSTOR_PACKET* vstorPacket =&requestExtension->VStorPacket;
713 int ret=0;
715 DPRINT_ENTER(STORVSC);
717 storDevice = GetStorDevice(Device);
719 DPRINT_DBG(STORVSC, "enter - Device %p, DeviceExt %p, Request %p, Extension %p",
720 Device, storDevice, Request, requestExtension);
722 DPRINT_DBG(STORVSC, "req %p len %d bus %d, target %d, lun %d cdblen %d",
723 Request, Request->DataBuffer.Length, Request->Bus, Request->TargetId, Request->LunId, Request->CdbLen);
725 if (!storDevice)
727 DPRINT_ERR(STORVSC, "unable to get stor device...device being destroyed?");
728 DPRINT_EXIT(STORVSC);
729 return -2;
732 /* print_hex_dump_bytes("", DUMP_PREFIX_NONE, Request->Cdb, Request->CdbLen); */
734 requestExtension->Request = Request;
735 requestExtension->Device = Device;
737 memset(vstorPacket, 0 , sizeof(VSTOR_PACKET));
739 vstorPacket->Flags |= REQUEST_COMPLETION_FLAG;
741 vstorPacket->VmSrb.Length = sizeof(VMSCSI_REQUEST);
743 vstorPacket->VmSrb.PortNumber = Request->Host;
744 vstorPacket->VmSrb.PathId = Request->Bus;
745 vstorPacket->VmSrb.TargetId = Request->TargetId;
746 vstorPacket->VmSrb.Lun = Request->LunId;
748 vstorPacket->VmSrb.SenseInfoLength = SENSE_BUFFER_SIZE;
750 /* Copy over the scsi command descriptor block */
751 vstorPacket->VmSrb.CdbLength = Request->CdbLen;
752 memcpy(&vstorPacket->VmSrb.Cdb, Request->Cdb, Request->CdbLen);
754 vstorPacket->VmSrb.DataIn = Request->Type;
755 vstorPacket->VmSrb.DataTransferLength = Request->DataBuffer.Length;
757 vstorPacket->Operation = VStorOperationExecuteSRB;
759 DPRINT_DBG(STORVSC, "srb - len %d port %d, path %d, target %d, lun %d senselen %d cdblen %d",
760 vstorPacket->VmSrb.Length,
761 vstorPacket->VmSrb.PortNumber,
762 vstorPacket->VmSrb.PathId,
763 vstorPacket->VmSrb.TargetId,
764 vstorPacket->VmSrb.Lun,
765 vstorPacket->VmSrb.SenseInfoLength,
766 vstorPacket->VmSrb.CdbLength);
768 if (requestExtension->Request->DataBuffer.Length)
770 ret = Device->Driver->VmbusChannelInterface.SendPacketMultiPageBuffer(Device,
771 &requestExtension->Request->DataBuffer,
772 vstorPacket,
773 sizeof(VSTOR_PACKET),
774 (unsigned long)requestExtension);
776 else
778 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
779 vstorPacket,
780 sizeof(VSTOR_PACKET),
781 (unsigned long)requestExtension,
782 VmbusPacketTypeDataInBand,
783 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
786 if (ret != 0)
788 DPRINT_DBG(STORVSC, "Unable to send packet %p ret %d", vstorPacket, ret);
791 InterlockedIncrement(&storDevice->NumOutstandingRequests);
793 PutStorDevice(Device);
795 DPRINT_EXIT(STORVSC);
796 return ret;
799 /*++
801 Name:
802 StorVscOnCleanup()
804 Description:
805 Perform any cleanup when the driver is removed
807 --*/
808 void
809 StorVscOnCleanup(
810 DRIVER_OBJECT *Driver
813 DPRINT_ENTER(STORVSC);
814 DPRINT_EXIT(STORVSC);
818 static void
819 StorVscOnIOCompletion(
820 struct hv_device *Device,
821 VSTOR_PACKET *VStorPacket,
822 STORVSC_REQUEST_EXTENSION *RequestExt
825 STORVSC_REQUEST *request;
826 STORVSC_DEVICE *storDevice;
828 DPRINT_ENTER(STORVSC);
830 storDevice = MustGetStorDevice(Device);
831 if (!storDevice)
833 DPRINT_ERR(STORVSC, "unable to get stor device...device being destroyed?");
834 DPRINT_EXIT(STORVSC);
835 return;
838 DPRINT_DBG(STORVSC, "IO_COMPLETE_OPERATION - request extension %p completed bytes xfer %u",
839 RequestExt, VStorPacket->VmSrb.DataTransferLength);
841 ASSERT(RequestExt != NULL);
842 ASSERT(RequestExt->Request != NULL);
844 request = RequestExt->Request;
846 ASSERT(request->OnIOCompletion != NULL);
848 /* Copy over the status...etc */
849 request->Status = VStorPacket->VmSrb.ScsiStatus;
851 if (request->Status != 0 || VStorPacket->VmSrb.SrbStatus != 1)
853 DPRINT_WARN(STORVSC, "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
854 request->Cdb[0],
855 VStorPacket->VmSrb.ScsiStatus,
856 VStorPacket->VmSrb.SrbStatus);
859 if ((request->Status & 0xFF) == 0x02) /* CHECK_CONDITION */
861 if (VStorPacket->VmSrb.SrbStatus & 0x80) /* autosense data available */
863 DPRINT_WARN(STORVSC, "storvsc pkt %p autosense data valid - len %d\n",
864 RequestExt, VStorPacket->VmSrb.SenseInfoLength);
866 ASSERT(VStorPacket->VmSrb.SenseInfoLength <= request->SenseBufferSize);
867 memcpy(request->SenseBuffer,
868 VStorPacket->VmSrb.SenseData,
869 VStorPacket->VmSrb.SenseInfoLength);
871 request->SenseBufferSize = VStorPacket->VmSrb.SenseInfoLength;
875 /* TODO: */
876 request->BytesXfer = VStorPacket->VmSrb.DataTransferLength;
878 request->OnIOCompletion(request);
880 InterlockedDecrement(&storDevice->NumOutstandingRequests);
882 PutStorDevice(Device);
884 DPRINT_EXIT(STORVSC);
888 static void
889 StorVscOnReceive(
890 struct hv_device *Device,
891 VSTOR_PACKET *VStorPacket,
892 STORVSC_REQUEST_EXTENSION *RequestExt
895 switch(VStorPacket->Operation)
897 case VStorOperationCompleteIo:
899 DPRINT_DBG(STORVSC, "IO_COMPLETE_OPERATION");
900 StorVscOnIOCompletion(Device, VStorPacket, RequestExt);
901 break;
903 /* case ENUMERATE_DEVICE_OPERATION: */
905 /* DPRINT_INFO(STORVSC, "ENUMERATE_DEVICE_OPERATION"); */
907 /* StorVscOnTargetRescan(Device); */
908 /* break; */
910 case VStorOperationRemoveDevice:
912 DPRINT_INFO(STORVSC, "REMOVE_DEVICE_OPERATION");
913 /* TODO: */
914 break;
916 default:
917 DPRINT_INFO(STORVSC, "Unknown operation received - %d", VStorPacket->Operation);
918 break;
922 void
923 StorVscOnChannelCallback(
924 void * Context
927 int ret=0;
928 struct hv_device *device = (struct hv_device*)Context;
929 STORVSC_DEVICE *storDevice;
930 u32 bytesRecvd;
931 u64 requestId;
932 unsigned char packet[ALIGN_UP(sizeof(VSTOR_PACKET),8)];
933 STORVSC_REQUEST_EXTENSION *request;
935 DPRINT_ENTER(STORVSC);
937 ASSERT(device);
939 storDevice = MustGetStorDevice(device);
940 if (!storDevice)
942 DPRINT_ERR(STORVSC, "unable to get stor device...device being destroyed?");
943 DPRINT_EXIT(STORVSC);
944 return;
949 ret = device->Driver->VmbusChannelInterface.RecvPacket(device,
950 packet,
951 ALIGN_UP(sizeof(VSTOR_PACKET),8),
952 &bytesRecvd,
953 &requestId);
954 if (ret == 0 && bytesRecvd > 0)
956 DPRINT_DBG(STORVSC, "receive %d bytes - tid %llx", bytesRecvd, requestId);
958 /* ASSERT(bytesRecvd == sizeof(VSTOR_PACKET)); */
960 request = (STORVSC_REQUEST_EXTENSION*)(unsigned long)requestId;
961 ASSERT(request);
963 /* if (vstorPacket.Flags & SYNTHETIC_FLAG) */
964 if ((request == &storDevice->InitRequest) || (request == &storDevice->ResetRequest))
966 /* DPRINT_INFO(STORVSC, "reset completion - operation %u status %u", vstorPacket.Operation, vstorPacket.Status); */
968 memcpy(&request->VStorPacket, packet, sizeof(VSTOR_PACKET));
970 WaitEventSet(request->WaitEvent);
972 else
974 StorVscOnReceive(device, (VSTOR_PACKET*)packet, request);
977 else
979 /* DPRINT_DBG(STORVSC, "nothing else to read..."); */
980 break;
982 } while (1);
984 PutStorDevice(device);
986 DPRINT_EXIT(STORVSC);
987 return;