Staging: hv: move osd.h
[linux-2.6/linux-2.6-openrd.git] / drivers / staging / hv / StorVsc.c
blob32fa2914f45de3dd8f15fcc5bee7dd0543b84c72
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 "osd.h"
29 #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 struct hv_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 atomic_t RefCount; /* 0 indicates the device is being destroyed */
62 atomic_t 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 struct hv_storvsc_request *Request
114 static int
115 StorVscOnHostReset(
116 struct hv_device *Device
119 static void
120 StorVscOnCleanup(
121 struct hv_driver *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 atomic_cmpxchg(&storDevice->RefCount, 0, 2);
160 storDevice->Device = Device;
161 Device->Extension = storDevice;
163 return storDevice;
166 static inline void FreeStorDevice(STORVSC_DEVICE *Device)
168 ASSERT( atomic_read(&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 && atomic_read(&storDevice->RefCount) > 1)
179 atomic_inc(&storDevice->RefCount);
180 else
181 storDevice = NULL;
183 return storDevice;
186 /* Get the stordevice object iff exists and its refcount > 0 */
187 static inline STORVSC_DEVICE* MustGetStorDevice(struct hv_device *Device)
189 STORVSC_DEVICE *storDevice;
191 storDevice = (STORVSC_DEVICE*)Device->Extension;
192 if (storDevice && atomic_read(&storDevice->RefCount))
193 atomic_inc(&storDevice->RefCount);
194 else
195 storDevice = NULL;
197 return storDevice;
200 static inline void PutStorDevice(struct hv_device *Device)
202 STORVSC_DEVICE *storDevice;
204 storDevice = (STORVSC_DEVICE*)Device->Extension;
205 ASSERT(storDevice);
207 atomic_dec(&storDevice->RefCount);
208 ASSERT(atomic_read(&storDevice->RefCount));
211 /* Drop ref count to 1 to effectively disable GetStorDevice() */
212 static inline STORVSC_DEVICE* ReleaseStorDevice(struct hv_device *Device)
214 STORVSC_DEVICE *storDevice;
216 storDevice = (STORVSC_DEVICE*)Device->Extension;
217 ASSERT(storDevice);
219 /* Busy wait until the ref drop to 2, then set it to 1 */
220 while (atomic_cmpxchg(&storDevice->RefCount, 2, 1) != 2)
222 udelay(100);
225 return storDevice;
228 /* Drop ref count to 0. No one can use StorDevice object. */
229 static inline STORVSC_DEVICE* FinalReleaseStorDevice(struct hv_device *Device)
231 STORVSC_DEVICE *storDevice;
233 storDevice = (STORVSC_DEVICE*)Device->Extension;
234 ASSERT(storDevice);
236 /* Busy wait until the ref drop to 1, then set it to 0 */
237 while (atomic_cmpxchg(&storDevice->RefCount, 1, 0) != 1)
239 udelay(100);
242 Device->Extension = NULL;
243 return storDevice;
246 /*++;
249 Name:
250 StorVscInitialize()
252 Description:
253 Main entry point
255 --*/
257 StorVscInitialize(
258 struct hv_driver *Driver
261 STORVSC_DRIVER_OBJECT* storDriver = (STORVSC_DRIVER_OBJECT*)Driver;
262 int ret=0;
264 DPRINT_ENTER(STORVSC);
266 DPRINT_DBG(STORVSC, "sizeof(STORVSC_REQUEST)=%zd sizeof(STORVSC_REQUEST_EXTENSION)=%zd sizeof(VSTOR_PACKET)=%zd, sizeof(VMSCSI_REQUEST)=%zd",
267 sizeof(struct hv_storvsc_request), sizeof(STORVSC_REQUEST_EXTENSION), sizeof(VSTOR_PACKET), sizeof(VMSCSI_REQUEST));
269 /* Make sure we are at least 2 pages since 1 page is used for control */
270 ASSERT(storDriver->RingBufferSize >= (PAGE_SIZE << 1));
272 Driver->name = gDriverName;
273 memcpy(&Driver->deviceType, &gStorVscDeviceType, sizeof(GUID));
275 storDriver->RequestExtSize = sizeof(STORVSC_REQUEST_EXTENSION);
278 * Divide the ring buffer data size (which is 1 page less
279 * than the ring buffer size since that page is reserved for
280 * the ring buffer indices) by the max request size (which is
281 * VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER + VSTOR_PACKET + u64)
283 storDriver->MaxOutstandingRequestsPerChannel =
284 ((storDriver->RingBufferSize - PAGE_SIZE) / ALIGN_UP(MAX_MULTIPAGE_BUFFER_PACKET + sizeof(VSTOR_PACKET) + sizeof(u64),sizeof(u64)));
286 DPRINT_INFO(STORVSC, "max io %u, currently %u\n", storDriver->MaxOutstandingRequestsPerChannel, STORVSC_MAX_IO_REQUESTS);
288 /* Setup the dispatch table */
289 storDriver->Base.OnDeviceAdd = StorVscOnDeviceAdd;
290 storDriver->Base.OnDeviceRemove = StorVscOnDeviceRemove;
291 storDriver->Base.OnCleanup = StorVscOnCleanup;
293 storDriver->OnIORequest = StorVscOnIORequest;
294 storDriver->OnHostReset = StorVscOnHostReset;
296 DPRINT_EXIT(STORVSC);
298 return ret;
301 /*++
303 Name:
304 StorVscOnDeviceAdd()
306 Description:
307 Callback when the device belonging to this driver is added
309 --*/
310 static int
311 StorVscOnDeviceAdd(
312 struct hv_device *Device,
313 void *AdditionalInfo
316 int ret=0;
317 STORVSC_DEVICE *storDevice;
318 /* VMSTORAGE_CHANNEL_PROPERTIES *props; */
319 STORVSC_DEVICE_INFO *deviceInfo = (STORVSC_DEVICE_INFO*)AdditionalInfo;
321 DPRINT_ENTER(STORVSC);
323 storDevice = AllocStorDevice(Device);
324 if (!storDevice)
326 ret = -1;
327 goto Cleanup;
330 /* Save the channel properties to our storvsc channel */
331 /* props = (VMSTORAGE_CHANNEL_PROPERTIES*) channel->offerMsg.Offer.u.Standard.UserDefined; */
333 /* FIXME: */
335 * If we support more than 1 scsi channel, we need to set the
336 * port number here to the scsi channel but how do we get the
337 * scsi channel prior to the bus scan
340 /* storChannel->PortNumber = 0;
341 storChannel->PathId = props->PathId;
342 storChannel->TargetId = props->TargetId; */
344 storDevice->PortNumber = deviceInfo->PortNumber;
345 /* Send it back up */
346 ret = StorVscConnectToVsp(Device);
348 /* deviceInfo->PortNumber = storDevice->PortNumber; */
349 deviceInfo->PathId = storDevice->PathId;
350 deviceInfo->TargetId = storDevice->TargetId;
352 DPRINT_DBG(STORVSC, "assigned port %u, path %u target %u\n", storDevice->PortNumber, storDevice->PathId, storDevice->TargetId);
354 Cleanup:
355 DPRINT_EXIT(STORVSC);
357 return ret;
360 static int StorVscChannelInit(struct hv_device *Device)
362 int ret=0;
363 STORVSC_DEVICE *storDevice;
364 STORVSC_REQUEST_EXTENSION *request;
365 VSTOR_PACKET *vstorPacket;
367 storDevice = GetStorDevice(Device);
368 if (!storDevice)
370 DPRINT_ERR(STORVSC, "unable to get stor device...device being destroyed?");
371 DPRINT_EXIT(STORVSC);
372 return -1;
375 request = &storDevice->InitRequest;
376 vstorPacket = &request->VStorPacket;
378 /* Now, initiate the vsc/vsp initialization protocol on the open channel */
380 memset(request, sizeof(STORVSC_REQUEST_EXTENSION), 0);
381 request->WaitEvent = osd_WaitEventCreate();
383 vstorPacket->Operation = VStorOperationBeginInitialization;
384 vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
386 /*SpinlockAcquire(gDriverExt.packetListLock);
387 INSERT_TAIL_LIST(&gDriverExt.packetList, &packet->listEntry.entry);
388 SpinlockRelease(gDriverExt.packetListLock);*/
390 DPRINT_INFO(STORVSC, "BEGIN_INITIALIZATION_OPERATION...");
392 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
393 vstorPacket,
394 sizeof(VSTOR_PACKET),
395 (unsigned long)request,
396 VmbusPacketTypeDataInBand,
397 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
398 if ( ret != 0)
400 DPRINT_ERR(STORVSC, "unable to send BEGIN_INITIALIZATION_OPERATION");
401 goto Cleanup;
404 osd_WaitEventWait(request->WaitEvent);
406 if (vstorPacket->Operation != VStorOperationCompleteIo || vstorPacket->Status != 0)
408 DPRINT_ERR(STORVSC, "BEGIN_INITIALIZATION_OPERATION failed (op %d status 0x%x)", vstorPacket->Operation, vstorPacket->Status);
409 goto Cleanup;
412 DPRINT_INFO(STORVSC, "QUERY_PROTOCOL_VERSION_OPERATION...");
414 /* reuse the packet for version range supported */
415 memset(vstorPacket, sizeof(VSTOR_PACKET), 0);
416 vstorPacket->Operation = VStorOperationQueryProtocolVersion;
417 vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
419 vstorPacket->Version.MajorMinor = VMSTOR_PROTOCOL_VERSION_CURRENT;
420 FILL_VMSTOR_REVISION(vstorPacket->Version.Revision);
422 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
423 vstorPacket,
424 sizeof(VSTOR_PACKET),
425 (unsigned long)request,
426 VmbusPacketTypeDataInBand,
427 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
428 if ( ret != 0)
430 DPRINT_ERR(STORVSC, "unable to send BEGIN_INITIALIZATION_OPERATION");
431 goto Cleanup;
434 osd_WaitEventWait(request->WaitEvent);
436 /* TODO: Check returned version */
437 if (vstorPacket->Operation != VStorOperationCompleteIo || vstorPacket->Status != 0)
439 DPRINT_ERR(STORVSC, "QUERY_PROTOCOL_VERSION_OPERATION failed (op %d status 0x%x)", vstorPacket->Operation, vstorPacket->Status);
440 goto Cleanup;
443 /* Query channel properties */
444 DPRINT_INFO(STORVSC, "QUERY_PROPERTIES_OPERATION...");
446 memset(vstorPacket, sizeof(VSTOR_PACKET), 0);
447 vstorPacket->Operation = VStorOperationQueryProperties;
448 vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
449 vstorPacket->StorageChannelProperties.PortNumber = storDevice->PortNumber;
451 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
452 vstorPacket,
453 sizeof(VSTOR_PACKET),
454 (unsigned long)request,
455 VmbusPacketTypeDataInBand,
456 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
458 if ( ret != 0)
460 DPRINT_ERR(STORVSC, "unable to send QUERY_PROPERTIES_OPERATION");
461 goto Cleanup;
464 osd_WaitEventWait(request->WaitEvent);
466 /* TODO: Check returned version */
467 if (vstorPacket->Operation != VStorOperationCompleteIo || vstorPacket->Status != 0)
469 DPRINT_ERR(STORVSC, "QUERY_PROPERTIES_OPERATION failed (op %d status 0x%x)", vstorPacket->Operation, vstorPacket->Status);
470 goto Cleanup;
473 /* storDevice->PortNumber = vstorPacket->StorageChannelProperties.PortNumber; */
474 storDevice->PathId = vstorPacket->StorageChannelProperties.PathId;
475 storDevice->TargetId = vstorPacket->StorageChannelProperties.TargetId;
477 DPRINT_DBG(STORVSC, "channel flag 0x%x, max xfer len 0x%x", vstorPacket->StorageChannelProperties.Flags, vstorPacket->StorageChannelProperties.MaxTransferBytes);
479 DPRINT_INFO(STORVSC, "END_INITIALIZATION_OPERATION...");
481 memset(vstorPacket, sizeof(VSTOR_PACKET), 0);
482 vstorPacket->Operation = VStorOperationEndInitialization;
483 vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
485 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
486 vstorPacket,
487 sizeof(VSTOR_PACKET),
488 (unsigned long)request,
489 VmbusPacketTypeDataInBand,
490 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
492 if ( ret != 0)
494 DPRINT_ERR(STORVSC, "unable to send END_INITIALIZATION_OPERATION");
495 goto Cleanup;
498 osd_WaitEventWait(request->WaitEvent);
500 if (vstorPacket->Operation != VStorOperationCompleteIo || vstorPacket->Status != 0)
502 DPRINT_ERR(STORVSC, "END_INITIALIZATION_OPERATION failed (op %d status 0x%x)", vstorPacket->Operation, vstorPacket->Status);
503 goto Cleanup;
506 DPRINT_INFO(STORVSC, "**** storage channel up and running!! ****");
508 Cleanup:
509 if (request->WaitEvent)
511 kfree(request->WaitEvent);
512 request->WaitEvent = NULL;
515 PutStorDevice(Device);
517 DPRINT_EXIT(STORVSC);
518 return ret;
522 static int
523 StorVscConnectToVsp(
524 struct hv_device *Device
527 int ret=0;
528 VMSTORAGE_CHANNEL_PROPERTIES props;
530 STORVSC_DRIVER_OBJECT *storDriver = (STORVSC_DRIVER_OBJECT*) Device->Driver;;
532 memset(&props, sizeof(VMSTORAGE_CHANNEL_PROPERTIES), 0);
534 /* Open the channel */
535 ret = Device->Driver->VmbusChannelInterface.Open(Device,
536 storDriver->RingBufferSize,
537 storDriver->RingBufferSize,
538 (void *)&props,
539 sizeof(VMSTORAGE_CHANNEL_PROPERTIES),
540 StorVscOnChannelCallback,
541 Device
544 DPRINT_DBG(STORVSC, "storage props: path id %d, tgt id %d, max xfer %d", props.PathId, props.TargetId, props.MaxTransferBytes);
546 if (ret != 0)
548 DPRINT_ERR(STORVSC, "unable to open channel: %d", ret);
549 return -1;
552 ret = StorVscChannelInit(Device);
554 return ret;
558 /*++
560 Name:
561 StorVscOnDeviceRemove()
563 Description:
564 Callback when the our device is being removed
566 --*/
567 static int
568 StorVscOnDeviceRemove(
569 struct hv_device *Device
572 STORVSC_DEVICE *storDevice;
573 int ret=0;
575 DPRINT_ENTER(STORVSC);
577 DPRINT_INFO(STORVSC, "disabling storage device (%p)...", Device->Extension);
579 storDevice = ReleaseStorDevice(Device);
582 * At this point, all outbound traffic should be disable. We
583 * only allow inbound traffic (responses) to proceed so that
584 * outstanding requests can be completed.
586 while (atomic_read(&storDevice->NumOutstandingRequests))
588 DPRINT_INFO(STORVSC, "waiting for %d requests to complete...", atomic_read(&storDevice->NumOutstandingRequests));
590 udelay(100);
593 DPRINT_INFO(STORVSC, "removing storage device (%p)...", Device->Extension);
595 storDevice = FinalReleaseStorDevice(Device);
597 DPRINT_INFO(STORVSC, "storage device (%p) safe to remove", storDevice);
599 /* Close the channel */
600 Device->Driver->VmbusChannelInterface.Close(Device);
602 FreeStorDevice(storDevice);
604 DPRINT_EXIT(STORVSC);
605 return ret;
608 /* ***************
609 static void
610 StorVscOnTargetRescan(
611 void *Context
614 struct hv_device *device=(struct hv_device *)Context;
615 STORVSC_DRIVER_OBJECT *storDriver;
617 DPRINT_ENTER(STORVSC);
619 storDriver = (STORVSC_DRIVER_OBJECT*) device->Driver;
620 storDriver->OnHostRescan(device);
622 DPRINT_EXIT(STORVSC);
624 *********** */
626 static int
627 StorVscOnHostReset(
628 struct hv_device *Device
631 int ret=0;
633 STORVSC_DEVICE *storDevice;
634 STORVSC_REQUEST_EXTENSION *request;
635 VSTOR_PACKET *vstorPacket;
637 DPRINT_ENTER(STORVSC);
639 DPRINT_INFO(STORVSC, "resetting host adapter...");
641 storDevice = GetStorDevice(Device);
642 if (!storDevice)
644 DPRINT_ERR(STORVSC, "unable to get stor device...device being destroyed?");
645 DPRINT_EXIT(STORVSC);
646 return -1;
649 request = &storDevice->ResetRequest;
650 vstorPacket = &request->VStorPacket;
652 request->WaitEvent = osd_WaitEventCreate();
654 vstorPacket->Operation = VStorOperationResetBus;
655 vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
656 vstorPacket->VmSrb.PathId = storDevice->PathId;
658 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
659 vstorPacket,
660 sizeof(VSTOR_PACKET),
661 (unsigned long)&storDevice->ResetRequest,
662 VmbusPacketTypeDataInBand,
663 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
664 if (ret != 0)
666 DPRINT_ERR(STORVSC, "Unable to send reset packet %p ret %d", vstorPacket, ret);
667 goto Cleanup;
670 /* FIXME: Add a timeout */
671 osd_WaitEventWait(request->WaitEvent);
673 kfree(request->WaitEvent);
674 DPRINT_INFO(STORVSC, "host adapter reset completed");
677 * At this point, all outstanding requests in the adapter
678 * should have been flushed out and return to us
681 Cleanup:
682 PutStorDevice(Device);
683 DPRINT_EXIT(STORVSC);
684 return ret;
687 /*++
689 Name:
690 StorVscOnIORequest()
692 Description:
693 Callback to initiate an I/O request
695 --*/
696 static int
697 StorVscOnIORequest(
698 struct hv_device *Device,
699 struct hv_storvsc_request *Request
702 STORVSC_DEVICE *storDevice;
703 STORVSC_REQUEST_EXTENSION* requestExtension = (STORVSC_REQUEST_EXTENSION*) Request->Extension;
704 VSTOR_PACKET* vstorPacket =&requestExtension->VStorPacket;
705 int ret=0;
707 DPRINT_ENTER(STORVSC);
709 storDevice = GetStorDevice(Device);
711 DPRINT_DBG(STORVSC, "enter - Device %p, DeviceExt %p, Request %p, Extension %p",
712 Device, storDevice, Request, requestExtension);
714 DPRINT_DBG(STORVSC, "req %p len %d bus %d, target %d, lun %d cdblen %d",
715 Request, Request->DataBuffer.Length, Request->Bus, Request->TargetId, Request->LunId, Request->CdbLen);
717 if (!storDevice)
719 DPRINT_ERR(STORVSC, "unable to get stor device...device being destroyed?");
720 DPRINT_EXIT(STORVSC);
721 return -2;
724 /* print_hex_dump_bytes("", DUMP_PREFIX_NONE, Request->Cdb, Request->CdbLen); */
726 requestExtension->Request = Request;
727 requestExtension->Device = Device;
729 memset(vstorPacket, 0 , sizeof(VSTOR_PACKET));
731 vstorPacket->Flags |= REQUEST_COMPLETION_FLAG;
733 vstorPacket->VmSrb.Length = sizeof(VMSCSI_REQUEST);
735 vstorPacket->VmSrb.PortNumber = Request->Host;
736 vstorPacket->VmSrb.PathId = Request->Bus;
737 vstorPacket->VmSrb.TargetId = Request->TargetId;
738 vstorPacket->VmSrb.Lun = Request->LunId;
740 vstorPacket->VmSrb.SenseInfoLength = SENSE_BUFFER_SIZE;
742 /* Copy over the scsi command descriptor block */
743 vstorPacket->VmSrb.CdbLength = Request->CdbLen;
744 memcpy(&vstorPacket->VmSrb.Cdb, Request->Cdb, Request->CdbLen);
746 vstorPacket->VmSrb.DataIn = Request->Type;
747 vstorPacket->VmSrb.DataTransferLength = Request->DataBuffer.Length;
749 vstorPacket->Operation = VStorOperationExecuteSRB;
751 DPRINT_DBG(STORVSC, "srb - len %d port %d, path %d, target %d, lun %d senselen %d cdblen %d",
752 vstorPacket->VmSrb.Length,
753 vstorPacket->VmSrb.PortNumber,
754 vstorPacket->VmSrb.PathId,
755 vstorPacket->VmSrb.TargetId,
756 vstorPacket->VmSrb.Lun,
757 vstorPacket->VmSrb.SenseInfoLength,
758 vstorPacket->VmSrb.CdbLength);
760 if (requestExtension->Request->DataBuffer.Length)
762 ret = Device->Driver->VmbusChannelInterface.SendPacketMultiPageBuffer(Device,
763 &requestExtension->Request->DataBuffer,
764 vstorPacket,
765 sizeof(VSTOR_PACKET),
766 (unsigned long)requestExtension);
768 else
770 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
771 vstorPacket,
772 sizeof(VSTOR_PACKET),
773 (unsigned long)requestExtension,
774 VmbusPacketTypeDataInBand,
775 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
778 if (ret != 0)
780 DPRINT_DBG(STORVSC, "Unable to send packet %p ret %d", vstorPacket, ret);
783 atomic_inc(&storDevice->NumOutstandingRequests);
785 PutStorDevice(Device);
787 DPRINT_EXIT(STORVSC);
788 return ret;
791 /*++
793 Name:
794 StorVscOnCleanup()
796 Description:
797 Perform any cleanup when the driver is removed
799 --*/
800 static void
801 StorVscOnCleanup(
802 struct hv_driver *Driver
805 DPRINT_ENTER(STORVSC);
806 DPRINT_EXIT(STORVSC);
810 static void
811 StorVscOnIOCompletion(
812 struct hv_device *Device,
813 VSTOR_PACKET *VStorPacket,
814 STORVSC_REQUEST_EXTENSION *RequestExt
817 struct hv_storvsc_request *request;
818 STORVSC_DEVICE *storDevice;
820 DPRINT_ENTER(STORVSC);
822 storDevice = MustGetStorDevice(Device);
823 if (!storDevice)
825 DPRINT_ERR(STORVSC, "unable to get stor device...device being destroyed?");
826 DPRINT_EXIT(STORVSC);
827 return;
830 DPRINT_DBG(STORVSC, "IO_COMPLETE_OPERATION - request extension %p completed bytes xfer %u",
831 RequestExt, VStorPacket->VmSrb.DataTransferLength);
833 ASSERT(RequestExt != NULL);
834 ASSERT(RequestExt->Request != NULL);
836 request = RequestExt->Request;
838 ASSERT(request->OnIOCompletion != NULL);
840 /* Copy over the status...etc */
841 request->Status = VStorPacket->VmSrb.ScsiStatus;
843 if (request->Status != 0 || VStorPacket->VmSrb.SrbStatus != 1)
845 DPRINT_WARN(STORVSC, "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
846 request->Cdb[0],
847 VStorPacket->VmSrb.ScsiStatus,
848 VStorPacket->VmSrb.SrbStatus);
851 if ((request->Status & 0xFF) == 0x02) /* CHECK_CONDITION */
853 if (VStorPacket->VmSrb.SrbStatus & 0x80) /* autosense data available */
855 DPRINT_WARN(STORVSC, "storvsc pkt %p autosense data valid - len %d\n",
856 RequestExt, VStorPacket->VmSrb.SenseInfoLength);
858 ASSERT(VStorPacket->VmSrb.SenseInfoLength <= request->SenseBufferSize);
859 memcpy(request->SenseBuffer,
860 VStorPacket->VmSrb.SenseData,
861 VStorPacket->VmSrb.SenseInfoLength);
863 request->SenseBufferSize = VStorPacket->VmSrb.SenseInfoLength;
867 /* TODO: */
868 request->BytesXfer = VStorPacket->VmSrb.DataTransferLength;
870 request->OnIOCompletion(request);
872 atomic_dec(&storDevice->NumOutstandingRequests);
874 PutStorDevice(Device);
876 DPRINT_EXIT(STORVSC);
880 static void
881 StorVscOnReceive(
882 struct hv_device *Device,
883 VSTOR_PACKET *VStorPacket,
884 STORVSC_REQUEST_EXTENSION *RequestExt
887 switch(VStorPacket->Operation)
889 case VStorOperationCompleteIo:
891 DPRINT_DBG(STORVSC, "IO_COMPLETE_OPERATION");
892 StorVscOnIOCompletion(Device, VStorPacket, RequestExt);
893 break;
895 /* case ENUMERATE_DEVICE_OPERATION: */
897 /* DPRINT_INFO(STORVSC, "ENUMERATE_DEVICE_OPERATION"); */
899 /* StorVscOnTargetRescan(Device); */
900 /* break; */
902 case VStorOperationRemoveDevice:
904 DPRINT_INFO(STORVSC, "REMOVE_DEVICE_OPERATION");
905 /* TODO: */
906 break;
908 default:
909 DPRINT_INFO(STORVSC, "Unknown operation received - %d", VStorPacket->Operation);
910 break;
914 static void
915 StorVscOnChannelCallback(
916 void * Context
919 int ret=0;
920 struct hv_device *device = (struct hv_device*)Context;
921 STORVSC_DEVICE *storDevice;
922 u32 bytesRecvd;
923 u64 requestId;
924 unsigned char packet[ALIGN_UP(sizeof(VSTOR_PACKET),8)];
925 STORVSC_REQUEST_EXTENSION *request;
927 DPRINT_ENTER(STORVSC);
929 ASSERT(device);
931 storDevice = MustGetStorDevice(device);
932 if (!storDevice)
934 DPRINT_ERR(STORVSC, "unable to get stor device...device being destroyed?");
935 DPRINT_EXIT(STORVSC);
936 return;
941 ret = device->Driver->VmbusChannelInterface.RecvPacket(device,
942 packet,
943 ALIGN_UP(sizeof(VSTOR_PACKET),8),
944 &bytesRecvd,
945 &requestId);
946 if (ret == 0 && bytesRecvd > 0)
948 DPRINT_DBG(STORVSC, "receive %d bytes - tid %llx", bytesRecvd, requestId);
950 /* ASSERT(bytesRecvd == sizeof(VSTOR_PACKET)); */
952 request = (STORVSC_REQUEST_EXTENSION*)(unsigned long)requestId;
953 ASSERT(request);
955 /* if (vstorPacket.Flags & SYNTHETIC_FLAG) */
956 if ((request == &storDevice->InitRequest) || (request == &storDevice->ResetRequest))
958 /* DPRINT_INFO(STORVSC, "reset completion - operation %u status %u", vstorPacket.Operation, vstorPacket.Status); */
960 memcpy(&request->VStorPacket, packet, sizeof(VSTOR_PACKET));
962 osd_WaitEventSet(request->WaitEvent);
964 else
966 StorVscOnReceive(device, (VSTOR_PACKET*)packet, request);
969 else
971 /* DPRINT_DBG(STORVSC, "nothing else to read..."); */
972 break;
974 } while (1);
976 PutStorDevice(device);
978 DPRINT_EXIT(STORVSC);
979 return;