1 From foo@baz Mon Jul 13 16:01:31 PDT 2009
2 Date: Mon, 13 Jul 2009 16:01:31 -0700
3 From: Hank Janssen <hjanssen@microsoft.com>
4 Subject: Staging: hv: add the Hyper-V virtual storage driver
6 From: Hank Janssen <hjanssen@microsoft.com>
8 This is the virtual storage driver when running Linux on top of Hyper-V.
10 Signed-off-by: Hank Janssen <hjanssen@microsoft.com>
11 Signed-off-by: Haiyang Zhang <haiyangz@microsoft.com>
12 Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
14 drivers/staging/hv/StorVsc.c | 967 ++++++++++++++++++++++++++
15 drivers/staging/hv/storvsc_drv.c | 1413 +++++++++++++++++++++++++++++++++++++++
16 2 files changed, 2380 insertions(+)
19 +++ b/drivers/staging/hv/StorVsc.c
23 + * Copyright (c) 2009, Microsoft Corporation.
25 + * This program is free software; you can redistribute it and/or modify it
26 + * under the terms and conditions of the GNU General Public License,
27 + * version 2, as published by the Free Software Foundation.
29 + * This program is distributed in the hope it will be useful, but WITHOUT
30 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
31 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
34 + * You should have received a copy of the GNU General Public License along with
35 + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
36 + * Place - Suite 330, Boston, MA 02111-1307 USA.
39 + * Haiyang Zhang <haiyangz@microsoft.com>
40 + * Hank Janssen <hjanssen@microsoft.com>
47 +#include "StorVscApi.h"
48 +#include "VmbusPacketFormat.h"
49 +#include "vstorage.h"
60 +typedef struct _STORVSC_REQUEST_EXTENSION {
61 + //LIST_ENTRY ListEntry;
63 + STORVSC_REQUEST *Request;
64 + DEVICE_OBJECT *Device;
66 + // Synchronize the request/response if needed
69 + VSTOR_PACKET VStorPacket;
70 +} STORVSC_REQUEST_EXTENSION;
73 +// A storvsc device is a device object that contains a vmbus channel
74 +typedef struct _STORVSC_DEVICE{
75 + DEVICE_OBJECT *Device;
77 + int RefCount; // 0 indicates the device is being destroyed
79 + int NumOutstandingRequests;
81 + // Each unique Port/Path/Target represents 1 channel ie scsi controller. In reality, the pathid, targetid is always 0
82 + // and the port is set by us
87 + //LIST_ENTRY OutstandingRequestList;
88 + //HANDLE OutstandingRequestLock;
90 + // Used for vsc/vsp channel reset process
91 + STORVSC_REQUEST_EXTENSION InitRequest;
93 + STORVSC_REQUEST_EXTENSION ResetRequest;
101 +static const char* gDriverName="storvsc";
103 +//{ba6163d9-04a1-4d29-b605-72e2ffb1dc7f}
104 +static const GUID gStorVscDeviceType={
105 + .Data = {0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d, 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f}
109 +// Internal routines
113 + DEVICE_OBJECT *Device,
114 + void *AdditionalInfo
118 +StorVscOnDeviceRemove(
119 + DEVICE_OBJECT *Device
124 + DEVICE_OBJECT *Device,
125 + STORVSC_REQUEST *Request
130 + DEVICE_OBJECT *Device
135 + DRIVER_OBJECT *Device
139 +StorVscOnChannelCallback(
144 +StorVscOnIOCompletion(
145 + DEVICE_OBJECT *Device,
146 + VSTOR_PACKET *VStorPacket,
147 + STORVSC_REQUEST_EXTENSION *RequestExt
152 + DEVICE_OBJECT *Device,
153 + VSTOR_PACKET *VStorPacket,
154 + STORVSC_REQUEST_EXTENSION *RequestExt
158 +StorVscConnectToVsp(
159 + DEVICE_OBJECT *Device
162 +static inline STORVSC_DEVICE* AllocStorDevice(DEVICE_OBJECT *Device)
164 + STORVSC_DEVICE *storDevice;
166 + storDevice = MemAllocZeroed(sizeof(STORVSC_DEVICE));
170 + // Set to 2 to allow both inbound and outbound traffics
171 + // (ie GetStorDevice() and MustGetStorDevice()) to proceed.
172 + InterlockedCompareExchange(&storDevice->RefCount, 2, 0);
174 + storDevice->Device = Device;
175 + Device->Extension = storDevice;
180 +static inline void FreeStorDevice(STORVSC_DEVICE *Device)
182 + ASSERT(Device->RefCount == 0);
186 +// Get the stordevice object iff exists and its refcount > 1
187 +static inline STORVSC_DEVICE* GetStorDevice(DEVICE_OBJECT *Device)
189 + STORVSC_DEVICE *storDevice;
191 + storDevice = (STORVSC_DEVICE*)Device->Extension;
192 + if (storDevice && storDevice->RefCount > 1)
194 + InterlockedIncrement(&storDevice->RefCount);
204 +// Get the stordevice object iff exists and its refcount > 0
205 +static inline STORVSC_DEVICE* MustGetStorDevice(DEVICE_OBJECT *Device)
207 + STORVSC_DEVICE *storDevice;
209 + storDevice = (STORVSC_DEVICE*)Device->Extension;
210 + if (storDevice && storDevice->RefCount)
212 + InterlockedIncrement(&storDevice->RefCount);
222 +static inline void PutStorDevice(DEVICE_OBJECT *Device)
224 + STORVSC_DEVICE *storDevice;
226 + storDevice = (STORVSC_DEVICE*)Device->Extension;
227 + ASSERT(storDevice);
229 + InterlockedDecrement(&storDevice->RefCount);
230 + ASSERT(storDevice->RefCount);
233 +// Drop ref count to 1 to effectively disable GetStorDevice()
234 +static inline STORVSC_DEVICE* ReleaseStorDevice(DEVICE_OBJECT *Device)
236 + STORVSC_DEVICE *storDevice;
238 + storDevice = (STORVSC_DEVICE*)Device->Extension;
239 + ASSERT(storDevice);
241 + // Busy wait until the ref drop to 2, then set it to 1
242 + while (InterlockedCompareExchange(&storDevice->RefCount, 1, 2) != 2)
250 +// Drop ref count to 0. No one can use StorDevice object.
251 +static inline STORVSC_DEVICE* FinalReleaseStorDevice(DEVICE_OBJECT *Device)
253 + STORVSC_DEVICE *storDevice;
255 + storDevice = (STORVSC_DEVICE*)Device->Extension;
256 + ASSERT(storDevice);
258 + // Busy wait until the ref drop to 1, then set it to 0
259 + while (InterlockedCompareExchange(&storDevice->RefCount, 0, 1) != 1)
264 + Device->Extension = NULL;
272 + StorVscInitialize()
280 + DRIVER_OBJECT *Driver
283 + STORVSC_DRIVER_OBJECT* storDriver = (STORVSC_DRIVER_OBJECT*)Driver;
286 + DPRINT_ENTER(STORVSC);
288 + DPRINT_DBG(STORVSC, "sizeof(STORVSC_REQUEST)=%d sizeof(STORVSC_REQUEST_EXTENSION)=%d sizeof(VSTOR_PACKET)=%d, sizeof(VMSCSI_REQUEST)=%d",
289 + sizeof(STORVSC_REQUEST), sizeof(STORVSC_REQUEST_EXTENSION), sizeof(VSTOR_PACKET), sizeof(VMSCSI_REQUEST));
291 + // Make sure we are at least 2 pages since 1 page is used for control
292 + ASSERT(storDriver->RingBufferSize >= (PAGE_SIZE << 1));
294 + Driver->name = gDriverName;
295 + memcpy(&Driver->deviceType, &gStorVscDeviceType, sizeof(GUID));
297 + storDriver->RequestExtSize = sizeof(STORVSC_REQUEST_EXTENSION);
299 + // Divide the ring buffer data size (which is 1 page less than the ring buffer size since that page is reserved for the ring buffer indices)
300 + // by the max request size (which is VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER + VSTOR_PACKET + UINT64)
301 + storDriver->MaxOutstandingRequestsPerChannel =
302 + ((storDriver->RingBufferSize - PAGE_SIZE) / ALIGN_UP(MAX_MULTIPAGE_BUFFER_PACKET + sizeof(VSTOR_PACKET) + sizeof(UINT64),sizeof(UINT64)));
304 + DPRINT_INFO(STORVSC, "max io %u, currently %u\n", storDriver->MaxOutstandingRequestsPerChannel, STORVSC_MAX_IO_REQUESTS);
306 + // Setup the dispatch table
307 + storDriver->Base.OnDeviceAdd = StorVscOnDeviceAdd;
308 + storDriver->Base.OnDeviceRemove = StorVscOnDeviceRemove;
309 + storDriver->Base.OnCleanup = StorVscOnCleanup;
311 + storDriver->OnIORequest = StorVscOnIORequest;
312 + storDriver->OnHostReset = StorVscOnHostReset;
314 + DPRINT_EXIT(STORVSC);
322 + StorVscOnDeviceAdd()
325 + Callback when the device belonging to this driver is added
330 + DEVICE_OBJECT *Device,
331 + void *AdditionalInfo
335 + STORVSC_DEVICE *storDevice;
336 + //VMSTORAGE_CHANNEL_PROPERTIES *props;
337 + STORVSC_DEVICE_INFO *deviceInfo = (STORVSC_DEVICE_INFO*)AdditionalInfo;
339 + DPRINT_ENTER(STORVSC);
341 + storDevice = AllocStorDevice(Device);
348 + // Save the channel properties to our storvsc channel
349 + //props = (VMSTORAGE_CHANNEL_PROPERTIES*) channel->offerMsg.Offer.u.Standard.UserDefined;
352 + // If we support more than 1 scsi channel, we need to set the port number here
353 + // to the scsi channel but how do we get the scsi channel prior to the bus scan
354 + /*storChannel->PortNumber = 0;
355 + storChannel->PathId = props->PathId;
356 + storChannel->TargetId = props->TargetId;*/
358 + storDevice->PortNumber = deviceInfo->PortNumber;
360 + ret = StorVscConnectToVsp(Device);
362 + //deviceInfo->PortNumber = storDevice->PortNumber;
363 + deviceInfo->PathId = storDevice->PathId;
364 + deviceInfo->TargetId = storDevice->TargetId;
366 + DPRINT_DBG(STORVSC, "assigned port %u, path %u target %u\n", storDevice->PortNumber, storDevice->PathId, storDevice->TargetId);
369 + DPRINT_EXIT(STORVSC);
374 +static int StorVscChannelInit(DEVICE_OBJECT *Device)
377 + STORVSC_DEVICE *storDevice;
378 + STORVSC_REQUEST_EXTENSION *request;
379 + VSTOR_PACKET *vstorPacket;
381 + storDevice = GetStorDevice(Device);
384 + DPRINT_ERR(STORVSC, "unable to get stor device...device being destroyed?");
385 + DPRINT_EXIT(STORVSC);
389 + request = &storDevice->InitRequest;
390 + vstorPacket = &request->VStorPacket;
392 + // Now, initiate the vsc/vsp initialization protocol on the open channel
394 + memset(request, sizeof(STORVSC_REQUEST_EXTENSION), 0);
395 + request->WaitEvent = WaitEventCreate();
397 + vstorPacket->Operation = VStorOperationBeginInitialization;
398 + vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
400 + /*SpinlockAcquire(gDriverExt.packetListLock);
401 + INSERT_TAIL_LIST(&gDriverExt.packetList, &packet->listEntry.entry);
402 + SpinlockRelease(gDriverExt.packetListLock);*/
404 + DPRINT_INFO(STORVSC, "BEGIN_INITIALIZATION_OPERATION...");
406 + ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
408 + sizeof(VSTOR_PACKET),
409 + (ULONG_PTR)request,
410 + VmbusPacketTypeDataInBand,
411 + VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
414 + DPRINT_ERR(STORVSC, "unable to send BEGIN_INITIALIZATION_OPERATION");
418 + WaitEventWait(request->WaitEvent);
420 + if (vstorPacket->Operation != VStorOperationCompleteIo || vstorPacket->Status != 0)
422 + DPRINT_ERR(STORVSC, "BEGIN_INITIALIZATION_OPERATION failed (op %d status 0x%x)", vstorPacket->Operation, vstorPacket->Status);
426 + DPRINT_INFO(STORVSC, "QUERY_PROTOCOL_VERSION_OPERATION...");
428 + // reuse the packet for version range supported
429 + memset(vstorPacket, sizeof(VSTOR_PACKET), 0);
430 + vstorPacket->Operation = VStorOperationQueryProtocolVersion;
431 + vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
433 + vstorPacket->Version.MajorMinor = VMSTOR_PROTOCOL_VERSION_CURRENT;
434 + FILL_VMSTOR_REVISION(vstorPacket->Version.Revision);
436 + ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
438 + sizeof(VSTOR_PACKET),
439 + (ULONG_PTR)request,
440 + VmbusPacketTypeDataInBand,
441 + VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
444 + DPRINT_ERR(STORVSC, "unable to send BEGIN_INITIALIZATION_OPERATION");
448 + WaitEventWait(request->WaitEvent);
450 + // TODO: Check returned version
451 + if (vstorPacket->Operation != VStorOperationCompleteIo || vstorPacket->Status != 0)
453 + DPRINT_ERR(STORVSC, "QUERY_PROTOCOL_VERSION_OPERATION failed (op %d status 0x%x)", vstorPacket->Operation, vstorPacket->Status);
457 + // Query channel properties
458 + DPRINT_INFO(STORVSC, "QUERY_PROPERTIES_OPERATION...");
460 + memset(vstorPacket, sizeof(VSTOR_PACKET), 0);
461 + vstorPacket->Operation = VStorOperationQueryProperties;
462 + vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
463 + vstorPacket->StorageChannelProperties.PortNumber = storDevice->PortNumber;
465 + ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
467 + sizeof(VSTOR_PACKET),
468 + (ULONG_PTR)request,
469 + VmbusPacketTypeDataInBand,
470 + VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
474 + DPRINT_ERR(STORVSC, "unable to send QUERY_PROPERTIES_OPERATION");
478 + WaitEventWait(request->WaitEvent);
480 + // TODO: Check returned version
481 + if (vstorPacket->Operation != VStorOperationCompleteIo || vstorPacket->Status != 0)
483 + DPRINT_ERR(STORVSC, "QUERY_PROPERTIES_OPERATION failed (op %d status 0x%x)", vstorPacket->Operation, vstorPacket->Status);
487 + //storDevice->PortNumber = vstorPacket->StorageChannelProperties.PortNumber;
488 + storDevice->PathId = vstorPacket->StorageChannelProperties.PathId;
489 + storDevice->TargetId = vstorPacket->StorageChannelProperties.TargetId;
491 + DPRINT_DBG(STORVSC, "channel flag 0x%x, max xfer len 0x%x", vstorPacket->StorageChannelProperties.Flags, vstorPacket->StorageChannelProperties.MaxTransferBytes);
493 + DPRINT_INFO(STORVSC, "END_INITIALIZATION_OPERATION...");
495 + memset(vstorPacket, sizeof(VSTOR_PACKET), 0);
496 + vstorPacket->Operation = VStorOperationEndInitialization;
497 + vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
499 + ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
501 + sizeof(VSTOR_PACKET),
502 + (ULONG_PTR)request,
503 + VmbusPacketTypeDataInBand,
504 + VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
508 + DPRINT_ERR(STORVSC, "unable to send END_INITIALIZATION_OPERATION");
512 + WaitEventWait(request->WaitEvent);
514 + if (vstorPacket->Operation != VStorOperationCompleteIo || vstorPacket->Status != 0)
516 + DPRINT_ERR(STORVSC, "END_INITIALIZATION_OPERATION failed (op %d status 0x%x)", vstorPacket->Operation, vstorPacket->Status);
520 + DPRINT_INFO(STORVSC, "**** storage channel up and running!! ****");
523 + if (request->WaitEvent)
525 + WaitEventClose(request->WaitEvent);
526 + request->WaitEvent = NULL;
529 + PutStorDevice(Device);
531 + DPRINT_EXIT(STORVSC);
537 +StorVscConnectToVsp(
538 + DEVICE_OBJECT *Device
542 + VMSTORAGE_CHANNEL_PROPERTIES props;
544 + STORVSC_DRIVER_OBJECT *storDriver = (STORVSC_DRIVER_OBJECT*) Device->Driver;;
546 + memset(&props, sizeof(VMSTORAGE_CHANNEL_PROPERTIES), 0);
548 + // Open the channel
549 + ret = Device->Driver->VmbusChannelInterface.Open(Device,
550 + storDriver->RingBufferSize,
551 + storDriver->RingBufferSize,
553 + sizeof(VMSTORAGE_CHANNEL_PROPERTIES),
554 + StorVscOnChannelCallback,
558 + DPRINT_DBG(STORVSC, "storage props: path id %d, tgt id %d, max xfer %d", props.PathId, props.TargetId, props.MaxTransferBytes);
562 + DPRINT_ERR(STORVSC, "unable to open channel: %d", ret);
566 + ret = StorVscChannelInit(Device);
575 + StorVscOnDeviceRemove()
578 + Callback when the our device is being removed
582 +StorVscOnDeviceRemove(
583 + DEVICE_OBJECT *Device
586 + STORVSC_DEVICE *storDevice;
589 + DPRINT_ENTER(STORVSC);
591 + DPRINT_INFO(STORVSC, "disabling storage device (%p)...", Device->Extension);
593 + storDevice = ReleaseStorDevice(Device);
595 + // At this point, all outbound traffic should be disable. We only allow inbound traffic (responses) to proceed
596 + // so that outstanding requests can be completed.
597 + while (storDevice->NumOutstandingRequests)
599 + DPRINT_INFO(STORVSC, "waiting for %d requests to complete...", storDevice->NumOutstandingRequests);
604 + DPRINT_INFO(STORVSC, "removing storage device (%p)...", Device->Extension);
606 + storDevice = FinalReleaseStorDevice(Device);
608 + DPRINT_INFO(STORVSC, "storage device (%p) safe to remove", storDevice);
610 + // Close the channel
611 + Device->Driver->VmbusChannelInterface.Close(Device);
613 + FreeStorDevice(storDevice);
615 + DPRINT_EXIT(STORVSC);
621 +//StorVscOnTargetRescan(
625 +// DEVICE_OBJECT *device=(DEVICE_OBJECT*)Context;
626 +// STORVSC_DRIVER_OBJECT *storDriver;
628 +// DPRINT_ENTER(STORVSC);
630 +// storDriver = (STORVSC_DRIVER_OBJECT*) device->Driver;
631 +// storDriver->OnHostRescan(device);
633 +// DPRINT_EXIT(STORVSC);
638 + DEVICE_OBJECT *Device
643 + STORVSC_DEVICE *storDevice;
644 + STORVSC_REQUEST_EXTENSION *request;
645 + VSTOR_PACKET *vstorPacket;
647 + DPRINT_ENTER(STORVSC);
649 + DPRINT_INFO(STORVSC, "resetting host adapter...");
651 + storDevice = GetStorDevice(Device);
654 + DPRINT_ERR(STORVSC, "unable to get stor device...device being destroyed?");
655 + DPRINT_EXIT(STORVSC);
659 + request = &storDevice->ResetRequest;
660 + vstorPacket = &request->VStorPacket;
662 + request->WaitEvent = WaitEventCreate();
664 + vstorPacket->Operation = VStorOperationResetBus;
665 + vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
666 + vstorPacket->VmSrb.PathId = storDevice->PathId;
668 + ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
670 + sizeof(VSTOR_PACKET),
671 + (ULONG_PTR)&storDevice->ResetRequest,
672 + VmbusPacketTypeDataInBand,
673 + VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
676 + DPRINT_ERR(STORVSC, "Unable to send reset packet %p ret %d", vstorPacket, ret);
680 + // FIXME: Add a timeout
681 + WaitEventWait(request->WaitEvent);
683 + WaitEventClose(request->WaitEvent);
684 + DPRINT_INFO(STORVSC, "host adapter reset completed");
686 + // At this point, all outstanding requests in the adapter should have been flushed out and return to us
689 + PutStorDevice(Device);
690 + DPRINT_EXIT(STORVSC);
697 + StorVscOnIORequest()
700 + Callback to initiate an I/O request
705 + DEVICE_OBJECT *Device,
706 + STORVSC_REQUEST *Request
709 + STORVSC_DEVICE *storDevice;
710 + STORVSC_REQUEST_EXTENSION* requestExtension = (STORVSC_REQUEST_EXTENSION*) Request->Extension;
711 + VSTOR_PACKET* vstorPacket =&requestExtension->VStorPacket;
714 + DPRINT_ENTER(STORVSC);
716 + storDevice = GetStorDevice(Device);
718 + DPRINT_DBG(STORVSC, "enter - Device %p, DeviceExt %p, Request %p, Extension %p",
719 + Device, storDevice, Request, requestExtension);
721 + DPRINT_DBG(STORVSC, "req %p len %d bus %d, target %d, lun %d cdblen %d",
722 + Request, Request->DataBuffer.Length, Request->Bus, Request->TargetId, Request->LunId, Request->CdbLen);
726 + DPRINT_ERR(STORVSC, "unable to get stor device...device being destroyed?");
727 + DPRINT_EXIT(STORVSC);
731 + //PrintBytes(Request->Cdb, Request->CdbLen);
733 + requestExtension->Request = Request;
734 + requestExtension->Device = Device;
736 + memset(vstorPacket, 0 , sizeof(VSTOR_PACKET));
738 + vstorPacket->Flags |= REQUEST_COMPLETION_FLAG;
740 + vstorPacket->VmSrb.Length = sizeof(VMSCSI_REQUEST);
742 + vstorPacket->VmSrb.PortNumber = Request->Host;
743 + vstorPacket->VmSrb.PathId = Request->Bus;
744 + vstorPacket->VmSrb.TargetId = Request->TargetId;
745 + vstorPacket->VmSrb.Lun = Request->LunId;
747 + vstorPacket->VmSrb.SenseInfoLength = SENSE_BUFFER_SIZE;
749 + // Copy over the scsi command descriptor block
750 + vstorPacket->VmSrb.CdbLength = Request->CdbLen;
751 + memcpy(&vstorPacket->VmSrb.Cdb, Request->Cdb, Request->CdbLen);
753 + vstorPacket->VmSrb.DataIn = Request->Type;
754 + vstorPacket->VmSrb.DataTransferLength = Request->DataBuffer.Length;
756 + vstorPacket->Operation = VStorOperationExecuteSRB;
758 + DPRINT_DBG(STORVSC, "srb - len %d port %d, path %d, target %d, lun %d senselen %d cdblen %d",
759 + vstorPacket->VmSrb.Length,
760 + vstorPacket->VmSrb.PortNumber,
761 + vstorPacket->VmSrb.PathId,
762 + vstorPacket->VmSrb.TargetId,
763 + vstorPacket->VmSrb.Lun,
764 + vstorPacket->VmSrb.SenseInfoLength,
765 + vstorPacket->VmSrb.CdbLength);
767 + if (requestExtension->Request->DataBuffer.Length)
769 + ret = Device->Driver->VmbusChannelInterface.SendPacketMultiPageBuffer(Device,
770 + &requestExtension->Request->DataBuffer,
772 + sizeof(VSTOR_PACKET),
773 + (ULONG_PTR)requestExtension);
777 + ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
779 + sizeof(VSTOR_PACKET),
780 + (ULONG_PTR)requestExtension,
781 + VmbusPacketTypeDataInBand,
782 + VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
787 + DPRINT_DBG(STORVSC, "Unable to send packet %p ret %d", vstorPacket, ret);
790 + InterlockedIncrement(&storDevice->NumOutstandingRequests);
792 + PutStorDevice(Device);
794 + DPRINT_EXIT(STORVSC);
804 + Perform any cleanup when the driver is removed
809 + DRIVER_OBJECT *Driver
812 + DPRINT_ENTER(STORVSC);
813 + DPRINT_EXIT(STORVSC);
818 +StorVscOnIOCompletion(
819 + DEVICE_OBJECT *Device,
820 + VSTOR_PACKET *VStorPacket,
821 + STORVSC_REQUEST_EXTENSION *RequestExt
824 + STORVSC_REQUEST *request;
825 + STORVSC_DEVICE *storDevice;
827 + DPRINT_ENTER(STORVSC);
829 + storDevice = MustGetStorDevice(Device);
832 + DPRINT_ERR(STORVSC, "unable to get stor device...device being destroyed?");
833 + DPRINT_EXIT(STORVSC);
837 + DPRINT_DBG(STORVSC, "IO_COMPLETE_OPERATION - request extension %p completed bytes xfer %u",
838 + RequestExt, VStorPacket->VmSrb.DataTransferLength);
840 + ASSERT(RequestExt != NULL);
841 + ASSERT(RequestExt->Request != NULL);
843 + request = RequestExt->Request;
845 + ASSERT(request->OnIOCompletion != NULL);
847 + // Copy over the status...etc
848 + request->Status = VStorPacket->VmSrb.ScsiStatus;
850 + if (request->Status != 0 || VStorPacket->VmSrb.SrbStatus != 1)
852 + DPRINT_WARN(STORVSC, "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
854 + VStorPacket->VmSrb.ScsiStatus,
855 + VStorPacket->VmSrb.SrbStatus);
858 + if ((request->Status & 0xFF) == 0x02) // CHECK_CONDITION
860 + if (VStorPacket->VmSrb.SrbStatus & 0x80) // autosense data available
862 + DPRINT_WARN(STORVSC, "storvsc pkt %p autosense data valid - len %d\n",
863 + RequestExt, VStorPacket->VmSrb.SenseInfoLength);
865 + ASSERT(VStorPacket->VmSrb.SenseInfoLength <= request->SenseBufferSize);
866 + memcpy(request->SenseBuffer,
867 + VStorPacket->VmSrb.SenseData,
868 + VStorPacket->VmSrb.SenseInfoLength);
870 + request->SenseBufferSize = VStorPacket->VmSrb.SenseInfoLength;
875 + request->BytesXfer = VStorPacket->VmSrb.DataTransferLength;
877 + request->OnIOCompletion(request);
879 + InterlockedDecrement(&storDevice->NumOutstandingRequests);
881 + PutStorDevice(Device);
883 + DPRINT_EXIT(STORVSC);
889 + DEVICE_OBJECT *Device,
890 + VSTOR_PACKET *VStorPacket,
891 + STORVSC_REQUEST_EXTENSION *RequestExt
894 + switch(VStorPacket->Operation)
896 + case VStorOperationCompleteIo:
898 + DPRINT_DBG(STORVSC, "IO_COMPLETE_OPERATION");
899 + StorVscOnIOCompletion(Device, VStorPacket, RequestExt);
902 + //case ENUMERATE_DEVICE_OPERATION:
904 + // DPRINT_INFO(STORVSC, "ENUMERATE_DEVICE_OPERATION");
906 + // StorVscOnTargetRescan(Device);
909 + case VStorOperationRemoveDevice:
911 + DPRINT_INFO(STORVSC, "REMOVE_DEVICE_OPERATION");
916 + DPRINT_INFO(STORVSC, "Unknown operation received - %d", VStorPacket->Operation);
922 +StorVscOnChannelCallback(
927 + DEVICE_OBJECT *device = (DEVICE_OBJECT*)Context;
928 + STORVSC_DEVICE *storDevice;
931 + UCHAR packet[ALIGN_UP(sizeof(VSTOR_PACKET),8)];
932 + STORVSC_REQUEST_EXTENSION *request;
934 + DPRINT_ENTER(STORVSC);
938 + storDevice = MustGetStorDevice(device);
941 + DPRINT_ERR(STORVSC, "unable to get stor device...device being destroyed?");
942 + DPRINT_EXIT(STORVSC);
948 + ret = device->Driver->VmbusChannelInterface.RecvPacket(device,
950 + ALIGN_UP(sizeof(VSTOR_PACKET),8),
953 + if (ret == 0 && bytesRecvd > 0)
955 + DPRINT_DBG(STORVSC, "receive %d bytes - tid %llx", bytesRecvd, requestId);
957 + //ASSERT(bytesRecvd == sizeof(VSTOR_PACKET));
959 + request = (STORVSC_REQUEST_EXTENSION*)(ULONG_PTR)requestId;
962 + //if (vstorPacket.Flags & SYNTHETIC_FLAG)
963 + if ((request == &storDevice->InitRequest) || (request == &storDevice->ResetRequest))
965 + //DPRINT_INFO(STORVSC, "reset completion - operation %u status %u", vstorPacket.Operation, vstorPacket.Status);
967 + memcpy(&request->VStorPacket, packet, sizeof(VSTOR_PACKET));
969 + WaitEventSet(request->WaitEvent);
973 + StorVscOnReceive(device, (VSTOR_PACKET*)packet, request);
978 + //DPRINT_DBG(STORVSC, "nothing else to read...");
983 + PutStorDevice(device);
985 + DPRINT_EXIT(STORVSC);
989 +++ b/drivers/staging/hv/storvsc_drv.c
993 + * Copyright (c) 2009, Microsoft Corporation.
995 + * This program is free software; you can redistribute it and/or modify it
996 + * under the terms and conditions of the GNU General Public License,
997 + * version 2, as published by the Free Software Foundation.
999 + * This program is distributed in the hope it will be useful, but WITHOUT
1000 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1001 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
1004 + * You should have received a copy of the GNU General Public License along with
1005 + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
1006 + * Place - Suite 330, Boston, MA 02111-1307 USA.
1009 + * Haiyang Zhang <haiyangz@microsoft.com>
1010 + * Hank Janssen <hjanssen@microsoft.com>
1015 +#include <linux/init.h>
1016 +#include <linux/module.h>
1017 +#include <linux/device.h>
1018 +#include <linux/blkdev.h>
1020 +#include <scsi/scsi.h>
1021 +#include <scsi/scsi_cmnd.h>
1022 +#include <scsi/scsi_host.h>
1023 +#include <scsi/scsi_device.h>
1024 +#include <scsi/scsi_tcq.h>
1025 +#include <scsi/scsi_eh.h>
1026 +#include <scsi/scsi_devinfo.h>
1028 +#ifdef KERNEL_2_6_5
1030 +#include <scsi/scsi_dbg.h>
1033 +#include "logging.h"
1036 +#include "StorVscApi.h"
1045 +struct host_device_context {
1046 + struct work_struct host_rescan_work; //must be 1st field
1047 + struct device_context *device_ctx; // point back to our device context
1048 +#ifdef KERNEL_2_6_27
1049 + struct kmem_cache *request_pool;
1051 + kmem_cache_t *request_pool;
1053 + unsigned int port;
1054 + unsigned char path;
1055 + unsigned char target;
1058 +struct storvsc_cmd_request {
1059 + struct list_head entry;
1060 + struct scsi_cmnd *cmd;
1062 + unsigned int bounce_sgl_count;
1063 + struct scatterlist *bounce_sgl;
1065 + STORVSC_REQUEST request;
1066 + // !!!DO NOT ADD ANYTHING BELOW HERE!!!
1067 + // The extension buffer falls right here and is pointed to by request.Extension;
1070 +struct storvsc_driver_context {
1071 + // !! These must be the first 2 fields !!
1072 + struct driver_context drv_ctx;
1073 + STORVSC_DRIVER_OBJECT drv_obj;
1077 +static int storvsc_probe(struct device *dev);
1078 +static int storvsc_queuecommand(struct scsi_cmnd *scmnd, void (*done)(struct scsi_cmnd *));
1079 +static int storvsc_device_alloc(struct scsi_device *);
1080 +static int storvsc_device_configure(struct scsi_device *);
1081 +static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd);
1082 +#ifdef KERNEL_2_6_27
1083 +static void storvsc_host_rescan_callback(struct work_struct *work);
1085 +static void storvsc_host_rescan_callback(void* context);
1087 +static void storvsc_host_rescan(DEVICE_OBJECT* device_obj);
1088 +static int storvsc_remove(struct device *dev);
1090 +static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count, unsigned int len);
1091 +static void destroy_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count);
1092 +static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count);
1093 +static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl, struct scatterlist *bounce_sgl, unsigned int orig_sgl_count);
1094 +static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl, struct scatterlist *bounce_sgl, unsigned int orig_sgl_count);
1096 +static int storvsc_report_luns(struct scsi_device *sdev, unsigned int luns[], unsigned int *lun_count);
1097 +static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev, sector_t capacity, int *info);
1100 +static int storvsc_ringbuffer_size = STORVSC_RING_BUFFER_SIZE;
1102 +// The one and only one
1103 +static struct storvsc_driver_context g_storvsc_drv;
1106 +static struct scsi_host_template scsi_driver = {
1107 + .module = THIS_MODULE,
1108 + .name = "storvsc_host_t",
1109 + .bios_param = storvsc_get_chs,
1110 + .queuecommand = storvsc_queuecommand,
1111 + .eh_host_reset_handler = storvsc_host_reset_handler,
1112 + .slave_alloc = storvsc_device_alloc,
1113 + .slave_configure = storvsc_device_configure,
1115 + .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS, // 64 max_queue * 1 target
1117 + // no use setting to 0 since ll_blk_rw reset it to 1
1118 + .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT,// currently 32
1119 + // ENABLE_CLUSTERING allows mutiple physically contig bio_vecs to merge into 1 sg element. If set, we must
1120 + // limit the max_segment_size to PAGE_SIZE, otherwise we may get 1 sg element that represents multiple
1121 + // physically contig pfns (ie sg[x].length > PAGE_SIZE).
1122 + .use_clustering = ENABLE_CLUSTERING,
1123 + // Make sure we dont get a sg segment crosses a page boundary
1124 + .dma_boundary = PAGE_SIZE-1,
1130 +Name: storvsc_drv_init()
1132 +Desc: StorVsc driver initialization.
1135 +int storvsc_drv_init(PFN_DRIVERINITIALIZE pfn_drv_init)
1138 + STORVSC_DRIVER_OBJECT *storvsc_drv_obj=&g_storvsc_drv.drv_obj;
1139 + struct driver_context *drv_ctx=&g_storvsc_drv.drv_ctx;
1141 + DPRINT_ENTER(STORVSC_DRV);
1143 + vmbus_get_interface(&storvsc_drv_obj->Base.VmbusChannelInterface);
1145 + storvsc_drv_obj->RingBufferSize = storvsc_ringbuffer_size;
1146 + storvsc_drv_obj->OnHostRescan = storvsc_host_rescan;
1148 + // Callback to client driver to complete the initialization
1149 + pfn_drv_init(&storvsc_drv_obj->Base);
1151 + DPRINT_INFO(STORVSC_DRV, "request extension size %u, max outstanding reqs %u", storvsc_drv_obj->RequestExtSize, storvsc_drv_obj->MaxOutstandingRequestsPerChannel);
1153 + if (storvsc_drv_obj->MaxOutstandingRequestsPerChannel < STORVSC_MAX_IO_REQUESTS)
1155 + DPRINT_ERR(STORVSC_DRV, "The number of outstanding io requests (%d) is larger than that supported (%d) internally.",
1156 + STORVSC_MAX_IO_REQUESTS, storvsc_drv_obj->MaxOutstandingRequestsPerChannel);
1160 + drv_ctx->driver.name = storvsc_drv_obj->Base.name;
1161 + memcpy(&drv_ctx->class_id, &storvsc_drv_obj->Base.deviceType, sizeof(GUID));
1163 +#if defined(KERNEL_2_6_5) || defined(KERNEL_2_6_9)
1164 + drv_ctx->driver.probe = storvsc_probe;
1165 + drv_ctx->driver.remove = storvsc_remove;
1167 + drv_ctx->probe = storvsc_probe;
1168 + drv_ctx->remove = storvsc_remove;
1171 + // The driver belongs to vmbus
1172 + vmbus_child_driver_register(drv_ctx);
1174 + DPRINT_EXIT(STORVSC_DRV);
1180 +static int storvsc_drv_exit_cb(struct device *dev, void *data)
1182 + struct device **curr = (struct device **)data;
1184 + return 1; // stop iterating
1189 +Name: storvsc_drv_exit()
1194 +void storvsc_drv_exit(void)
1196 + STORVSC_DRIVER_OBJECT *storvsc_drv_obj=&g_storvsc_drv.drv_obj;
1197 + struct driver_context *drv_ctx=&g_storvsc_drv.drv_ctx;
1199 + struct device *current_dev=NULL;
1201 +#if defined(KERNEL_2_6_5) || defined(KERNEL_2_6_9)
1202 +#define driver_for_each_device(drv, start, data, fn) \
1203 + struct list_head *ptr, *n; \
1204 + list_for_each_safe(ptr, n, &((drv)->devices)) {\
1205 + struct device *curr_dev;\
1206 + curr_dev = list_entry(ptr, struct device, driver_list);\
1207 + fn(curr_dev, data);\
1209 +#endif // KERNEL_2_6_9
1211 + DPRINT_ENTER(STORVSC_DRV);
1215 + current_dev = NULL;
1218 + driver_for_each_device(&drv_ctx->driver, NULL, (void*)¤t_dev, storvsc_drv_exit_cb);
1220 + if (current_dev == NULL)
1223 + // Initiate removal from the top-down
1224 + device_unregister(current_dev);
1227 + if (storvsc_drv_obj->Base.OnCleanup)
1228 + storvsc_drv_obj->Base.OnCleanup(&storvsc_drv_obj->Base);
1230 + vmbus_child_driver_unregister(drv_ctx);
1232 + DPRINT_EXIT(STORVSC_DRV);
1239 +Name: storvsc_probe()
1241 +Desc: Add a new device for this driver
1244 +static int storvsc_probe(struct device *device)
1248 + struct driver_context *driver_ctx = driver_to_driver_context(device->driver);
1249 + struct storvsc_driver_context *storvsc_drv_ctx = (struct storvsc_driver_context*)driver_ctx;
1250 + STORVSC_DRIVER_OBJECT* storvsc_drv_obj = &storvsc_drv_ctx->drv_obj;
1252 + struct device_context *device_ctx = device_to_device_context(device);
1253 + DEVICE_OBJECT* device_obj = &device_ctx->device_obj;
1255 + struct Scsi_Host *host;
1256 + struct host_device_context *host_device_ctx;
1257 + STORVSC_DEVICE_INFO device_info;
1259 + DPRINT_ENTER(STORVSC_DRV);
1261 + if (!storvsc_drv_obj->Base.OnDeviceAdd)
1264 + host = scsi_host_alloc(&scsi_driver, sizeof(struct host_device_context));
1267 + DPRINT_ERR(STORVSC_DRV, "unable to allocate scsi host object");
1271 + device->driver_data = host;
1273 + host_device_ctx = (struct host_device_context*)host->hostdata;
1274 + memset(host_device_ctx, 0, sizeof(struct host_device_context));
1276 + host_device_ctx->port = host->host_no;
1277 + host_device_ctx->device_ctx = device_ctx;
1279 +#if defined(KERNEL_2_6_5) || defined(KERNEL_2_6_9)
1280 +#elif defined(KERNEL_2_6_27)
1281 + INIT_WORK(&host_device_ctx->host_rescan_work, storvsc_host_rescan_callback);
1283 + INIT_WORK(&host_device_ctx->host_rescan_work, storvsc_host_rescan_callback, device_obj);
1286 +#if defined(KERNEL_2_6_27)
1287 + host_device_ctx->request_pool =
1289 + (device_ctx->device.bus_id,
1290 + sizeof(struct storvsc_cmd_request) + storvsc_drv_obj->RequestExtSize,
1292 + SLAB_HWCACHE_ALIGN, NULL);
1294 + host_device_ctx->request_pool =
1296 + (device_ctx->device.bus_id,
1297 + sizeof(struct storvsc_cmd_request) + storvsc_drv_obj->RequestExtSize,
1299 + SLAB_HWCACHE_ALIGN, NULL, NULL);
1302 + if (!host_device_ctx->request_pool)
1304 + scsi_host_put(host);
1305 + DPRINT_EXIT(STORVSC_DRV);
1310 + device_info.PortNumber = host->host_no;
1311 + // Call to the vsc driver to add the device
1312 + ret = storvsc_drv_obj->Base.OnDeviceAdd(device_obj, (void*)&device_info);
1315 + DPRINT_ERR(STORVSC_DRV, "unable to add scsi vsc device");
1316 + kmem_cache_destroy(host_device_ctx->request_pool);
1317 + scsi_host_put(host);
1318 + DPRINT_EXIT(STORVSC_DRV);
1323 + //host_device_ctx->port = device_info.PortNumber;
1324 + host_device_ctx->path = device_info.PathId;
1325 + host_device_ctx->target = device_info.TargetId;
1327 + host->max_lun = STORVSC_MAX_LUNS_PER_TARGET; // max # of devices per target
1328 + host->max_id = STORVSC_MAX_TARGETS; // max # of targets per channel
1329 + host->max_channel = STORVSC_MAX_CHANNELS -1; // max # of channels
1331 + // Register the HBA and start the scsi bus scan
1332 + ret = scsi_add_host(host, device);
1335 + DPRINT_ERR(STORVSC_DRV, "unable to add scsi host device");
1337 + storvsc_drv_obj->Base.OnDeviceRemove(device_obj);
1339 + kmem_cache_destroy(host_device_ctx->request_pool);
1340 + scsi_host_put(host);
1341 + DPRINT_EXIT(STORVSC_DRV);
1346 + scsi_scan_host(host);
1348 + DPRINT_EXIT(STORVSC_DRV);
1356 +Name: storvsc_remove()
1358 +Desc: Callback when our device is removed
1361 +static int storvsc_remove(struct device *device)
1365 + struct driver_context *driver_ctx = driver_to_driver_context(device->driver);
1366 + struct storvsc_driver_context *storvsc_drv_ctx = (struct storvsc_driver_context*)driver_ctx;
1367 + STORVSC_DRIVER_OBJECT* storvsc_drv_obj = &storvsc_drv_ctx->drv_obj;
1369 + struct device_context *device_ctx = device_to_device_context(device);
1370 + DEVICE_OBJECT* device_obj = &device_ctx->device_obj;
1372 + struct Scsi_Host *host = (struct Scsi_Host *)device->driver_data;
1373 + struct host_device_context *host_device_ctx=(struct host_device_context*)host->hostdata;
1376 + DPRINT_ENTER(STORVSC_DRV);
1378 + if (!storvsc_drv_obj->Base.OnDeviceRemove)
1380 + DPRINT_EXIT(STORVSC_DRV);
1384 + // Call to the vsc driver to let it know that the device is being removed
1385 + ret = storvsc_drv_obj->Base.OnDeviceRemove(device_obj);
1389 + DPRINT_ERR(STORVSC, "unable to remove vsc device (ret %d)", ret);
1392 + if (host_device_ctx->request_pool)
1394 + kmem_cache_destroy(host_device_ctx->request_pool);
1395 + host_device_ctx->request_pool = NULL;
1398 + DPRINT_INFO(STORVSC, "removing host adapter (%p)...", host);
1399 + scsi_remove_host(host);
1401 + DPRINT_INFO(STORVSC, "releasing host adapter (%p)...", host);
1402 + scsi_host_put(host);
1404 + DPRINT_EXIT(STORVSC_DRV);
1411 +Name: storvsc_commmand_completion()
1413 +Desc: Command completion processing
1416 +static void storvsc_commmand_completion(STORVSC_REQUEST* request)
1418 + struct storvsc_cmd_request *cmd_request = (struct storvsc_cmd_request*)request->Context;
1419 + struct scsi_cmnd *scmnd = cmd_request->cmd;
1420 + struct host_device_context *host_device_ctx = (struct host_device_context*)scmnd->device->host->hostdata;
1421 + void (*scsi_done_fn)(struct scsi_cmnd *);
1422 +#if defined(KERNEL_2_6_5) || defined(KERNEL_2_6_9)
1424 + struct scsi_sense_hdr sense_hdr;
1427 + ASSERT(request == &cmd_request->request);
1428 + ASSERT((unsigned long)scmnd->host_scribble == (unsigned long)cmd_request);
1430 + ASSERT(scmnd->scsi_done);
1432 + DPRINT_ENTER(STORVSC_DRV);
1434 + if (cmd_request->bounce_sgl_count)// using bounce buffer
1436 + //printk("copy_from_bounce_buffer\n");
1438 + // FIXME: We can optimize on writes by just skipping this
1439 +#ifdef KERNEL_2_6_27
1440 + copy_from_bounce_buffer(scsi_sglist(scmnd), cmd_request->bounce_sgl, scsi_sg_count(scmnd));
1442 + copy_from_bounce_buffer(scmnd->request_buffer, cmd_request->bounce_sgl, scmnd->use_sg);
1444 + destroy_bounce_buffer(cmd_request->bounce_sgl, cmd_request->bounce_sgl_count);
1447 + scmnd->result = request->Status;
1449 + if (scmnd->result)
1451 +#if defined(KERNEL_2_6_5) || defined(KERNEL_2_6_9)
1452 + DPRINT_INFO(STORVSC_DRV, "scsi result nonzero - %d", scmnd->result);
1454 + if (scsi_normalize_sense(scmnd->sense_buffer, request->SenseBufferSize, &sense_hdr))
1456 + scsi_print_sense_hdr("storvsc", &sense_hdr);
1461 + ASSERT(request->BytesXfer <= request->DataBuffer.Length);
1462 +#ifdef KERNEL_2_6_27
1463 + scsi_set_resid(scmnd, request->DataBuffer.Length - request->BytesXfer);
1465 + scmnd->resid = request->DataBuffer.Length - request->BytesXfer;
1468 + scsi_done_fn = scmnd->scsi_done;
1470 + scmnd->host_scribble = NULL;
1471 + scmnd->scsi_done = NULL;
1473 + // !!DO NOT MODIFY the scmnd after this call
1474 + scsi_done_fn(scmnd);
1476 + kmem_cache_free(host_device_ctx->request_pool, cmd_request);
1478 + DPRINT_EXIT(STORVSC_DRV);
1481 +static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
1485 + // No need to check
1489 + // We have at least 2 sg entries
1490 + for ( i=0; i<sg_count; i++ )
1492 + if (i == 0) // make sure 1st one does not have hole
1494 + if (sgl[i].offset + sgl[i].length != PAGE_SIZE)
1497 + else if (i == sg_count - 1) // make sure last one does not have hole
1499 + if (sgl[i].offset != 0)
1502 + else // make sure no hole in the middle
1504 + if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0)
1513 +static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count, unsigned int len)
1517 + struct scatterlist* bounce_sgl;
1518 + struct page *page_buf;
1520 + num_pages = ALIGN_UP(len, PAGE_SIZE) >> PAGE_SHIFT;
1522 + bounce_sgl = kzalloc(num_pages * sizeof(struct scatterlist), GFP_ATOMIC);
1528 + for(i=0; i<num_pages; i++)
1530 + page_buf = alloc_page(GFP_ATOMIC);
1535 +#ifdef KERNEL_2_6_27
1536 + sg_set_page(&bounce_sgl[i], page_buf, 0, 0);
1538 + bounce_sgl[i].page = page_buf;
1539 + bounce_sgl[i].offset = 0;
1540 + bounce_sgl[i].length = 0;
1544 + return bounce_sgl;
1547 + destroy_bounce_buffer(bounce_sgl, num_pages);
1551 +static void destroy_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
1554 + struct page *page_buf;
1556 + for (i=0; i<sg_count; i++)
1558 +#ifdef KERNEL_2_6_27
1559 + if ((page_buf = sg_page((&sgl[i]))) != NULL)
1561 + if ((page_buf = sgl[i].page) != NULL)
1565 + __free_page(page_buf);
1572 +// Assume the bounce_sgl has enough room ie using the create_bounce_buffer()
1573 +static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl, struct scatterlist *bounce_sgl, unsigned int orig_sgl_count)
1576 + unsigned long src, dest;
1577 + unsigned int srclen, destlen, copylen;
1578 + unsigned int total_copied=0;
1579 + unsigned long bounce_addr=0;
1580 + unsigned long src_addr=0;
1581 + unsigned long flags;
1583 + local_irq_save(flags);
1585 + for (i=0; i<orig_sgl_count; i++)
1587 +#ifdef KERNEL_2_6_27
1588 + src_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])), KM_IRQ0) + orig_sgl[i].offset;
1590 + src_addr = (unsigned long)kmap_atomic(orig_sgl[i].page, KM_IRQ0) + orig_sgl[i].offset;
1593 + srclen = orig_sgl[i].length;
1595 + //if (PageHighMem(orig_sgl[i].page))
1596 + // printk("HighMem page detected - addr %p", (void*)src);
1598 + ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE);
1602 +#ifdef KERNEL_2_6_27
1603 + bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
1605 + bounce_addr = (unsigned long)kmap_atomic(bounce_sgl[j].page, KM_IRQ0);
1611 + // assume bounce offset always == 0
1612 + dest = bounce_addr + bounce_sgl[j].length;
1613 + destlen = PAGE_SIZE - bounce_sgl[j].length;
1615 + copylen = MIN(srclen, destlen);
1616 + memcpy((void*)dest, (void*)src, copylen);
1618 + total_copied += copylen;
1619 + bounce_sgl[j].length += copylen;
1620 + srclen -= copylen;
1623 + if (bounce_sgl[j].length == PAGE_SIZE) // full..move to next entry
1625 + kunmap_atomic((void*)bounce_addr, KM_IRQ0);
1628 + // if we need to use another bounce buffer
1629 + if (srclen || i != orig_sgl_count -1)
1631 +#ifdef KERNEL_2_6_27
1632 + bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
1634 + bounce_addr = (unsigned long)kmap_atomic(bounce_sgl[j].page, KM_IRQ0);
1638 + else if (srclen == 0 && i == orig_sgl_count -1) // // unmap the last bounce that is < PAGE_SIZE
1640 + kunmap_atomic((void*)bounce_addr, KM_IRQ0);
1644 + kunmap_atomic((void*)(src_addr - orig_sgl[i].offset), KM_IRQ0);
1647 + local_irq_restore(flags);
1649 + return total_copied;
1652 +// Assume the original sgl has enough room
1653 +static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl, struct scatterlist *bounce_sgl, unsigned int orig_sgl_count)
1656 + unsigned long src, dest;
1657 + unsigned int srclen, destlen, copylen;
1658 + unsigned int total_copied=0;
1659 + unsigned long bounce_addr=0;
1660 + unsigned long dest_addr=0;
1661 + unsigned long flags;
1663 + local_irq_save(flags);
1665 + for (i=0; i<orig_sgl_count; i++)
1667 +#ifdef KERNEL_2_6_27
1668 + dest_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])), KM_IRQ0) + orig_sgl[i].offset;
1670 + dest_addr = (unsigned long)kmap_atomic(orig_sgl[i].page, KM_IRQ0) + orig_sgl[i].offset;
1673 + destlen = orig_sgl[i].length;
1674 + ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE);
1678 +#ifdef KERNEL_2_6_27
1679 + bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
1681 + bounce_addr = (unsigned long)kmap_atomic(bounce_sgl[j].page, KM_IRQ0);
1687 + src = bounce_addr + bounce_sgl[j].offset;
1688 + srclen = bounce_sgl[j].length - bounce_sgl[j].offset;
1690 + copylen = MIN(srclen, destlen);
1691 + memcpy((void*)dest, (void*)src, copylen);
1693 + total_copied += copylen;
1694 + bounce_sgl[j].offset += copylen;
1695 + destlen -= copylen;
1698 + if (bounce_sgl[j].offset == bounce_sgl[j].length) // full
1700 + kunmap_atomic((void*)bounce_addr, KM_IRQ0);
1703 + // if we need to use another bounce buffer
1704 + if (destlen || i != orig_sgl_count -1)
1706 +#ifdef KERNEL_2_6_27
1707 + bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
1709 + bounce_addr = (unsigned long)kmap_atomic(bounce_sgl[j].page, KM_IRQ0);
1713 + else if (destlen == 0 && i == orig_sgl_count -1) // unmap the last bounce that is < PAGE_SIZE
1715 + kunmap_atomic((void*)bounce_addr, KM_IRQ0);
1719 + kunmap_atomic((void*)(dest_addr - orig_sgl[i].offset), KM_IRQ0);
1722 + local_irq_restore(flags);
1724 + return total_copied;
1730 +Name: storvsc_queuecommand()
1732 +Desc: Initiate command processing
1735 +static int storvsc_queuecommand(struct scsi_cmnd *scmnd, void (*done)(struct scsi_cmnd *))
1738 + struct host_device_context *host_device_ctx = (struct host_device_context*)scmnd->device->host->hostdata;
1739 + struct device_context *device_ctx=host_device_ctx->device_ctx;
1740 + struct driver_context *driver_ctx = driver_to_driver_context(device_ctx->device.driver);
1741 + struct storvsc_driver_context *storvsc_drv_ctx = (struct storvsc_driver_context*)driver_ctx;
1742 + STORVSC_DRIVER_OBJECT* storvsc_drv_obj = &storvsc_drv_ctx->drv_obj;
1744 + STORVSC_REQUEST *request;
1745 + struct storvsc_cmd_request *cmd_request;
1746 + unsigned int request_size=0;
1748 + struct scatterlist *sgl;
1750 + DPRINT_ENTER(STORVSC_DRV);
1752 +#ifdef KERNEL_2_6_27
1753 + DPRINT_DBG(STORVSC_DRV, "scmnd %p dir %d, use_sg %d buf %p len %d queue depth %d tagged %d",
1755 + scmnd->sc_data_direction,
1756 + scsi_sg_count(scmnd),
1757 + scsi_sglist(scmnd),
1758 + scsi_bufflen(scmnd),
1759 + scmnd->device->queue_depth,
1760 + scmnd->device->tagged_supported);
1762 + DPRINT_DBG(STORVSC_DRV, "scmnd %p dir %d, use_sg %d buf %p len %d queue depth %d tagged %d",
1764 + scmnd->sc_data_direction,
1766 + scmnd->request_buffer,
1767 + scmnd->request_bufflen,
1768 + scmnd->device->queue_depth,
1769 + scmnd->device->tagged_supported);
1772 + // If retrying, no need to prep the cmd
1773 + if (scmnd->host_scribble)
1775 + ASSERT(scmnd->scsi_done != NULL);
1777 + cmd_request = (struct storvsc_cmd_request* )scmnd->host_scribble;
1778 + DPRINT_INFO(STORVSC_DRV, "retrying scmnd %p cmd_request %p", scmnd, cmd_request);
1780 + goto retry_request;
1783 + ASSERT(scmnd->scsi_done == NULL);
1784 + ASSERT(scmnd->host_scribble == NULL);
1786 + scmnd->scsi_done = done;
1788 + request_size = sizeof(struct storvsc_cmd_request);
1790 + cmd_request = kmem_cache_alloc(host_device_ctx->request_pool, GFP_ATOMIC);
1793 + DPRINT_ERR(STORVSC_DRV, "scmnd (%p) - unable to allocate storvsc_cmd_request...marking queue busy", scmnd);
1795 + scmnd->scsi_done = NULL;
1796 + return SCSI_MLQUEUE_DEVICE_BUSY;
1799 + // Setup the cmd request
1800 + cmd_request->bounce_sgl_count = 0;
1801 + cmd_request->bounce_sgl = NULL;
1802 + cmd_request->cmd = scmnd;
1804 + scmnd->host_scribble = (unsigned char*)cmd_request;
1806 + request = &cmd_request->request;
1808 + request->Extension = (void*)((unsigned long)cmd_request + request_size);
1809 + DPRINT_DBG(STORVSC_DRV, "req %p size %d ext %d", request, request_size, storvsc_drv_obj->RequestExtSize);
1812 + switch(scmnd->sc_data_direction)
1814 + case DMA_TO_DEVICE:
1815 + request->Type = WRITE_TYPE;
1817 + case DMA_FROM_DEVICE:
1818 + request->Type = READ_TYPE;
1821 + request->Type = UNKNOWN_TYPE;
1825 + request->OnIOCompletion = storvsc_commmand_completion;
1826 + request->Context = cmd_request;//scmnd;
1828 + //request->PortId = scmnd->device->channel;
1829 + request->Host = host_device_ctx->port;
1830 + request->Bus = scmnd->device->channel;
1831 + request->TargetId = scmnd->device->id;
1832 + request->LunId = scmnd->device->lun;
1834 + ASSERT(scmnd->cmd_len <= 16);
1835 + request->CdbLen = scmnd->cmd_len;
1836 + request->Cdb = scmnd->cmnd;
1838 + request->SenseBuffer = scmnd->sense_buffer;
1839 + request->SenseBufferSize = SCSI_SENSE_BUFFERSIZE;
1842 +#ifdef KERNEL_2_6_27
1843 + request->DataBuffer.Length = scsi_bufflen(scmnd);
1844 + if (scsi_sg_count(scmnd))
1846 + request->DataBuffer.Length = scmnd->request_bufflen;
1847 + if (scmnd->use_sg)
1850 +#ifdef KERNEL_2_6_27
1851 + sgl = (struct scatterlist*)scsi_sglist(scmnd);
1853 + sgl = (struct scatterlist*)(scmnd->request_buffer);
1856 + // check if we need to bounce the sgl
1857 +#ifdef KERNEL_2_6_27
1858 + if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1)
1860 + if (do_bounce_buffer(sgl, scmnd->use_sg) != -1)
1863 + DPRINT_INFO(STORVSC_DRV, "need to bounce buffer for this scmnd %p", scmnd);
1864 +#ifdef KERNEL_2_6_27
1865 + cmd_request->bounce_sgl = create_bounce_buffer(sgl, scsi_sg_count(scmnd), scsi_bufflen(scmnd));
1867 + cmd_request->bounce_sgl = create_bounce_buffer(
1869 + scmnd->use_sg, scmnd->request_bufflen);
1871 + if (!cmd_request->bounce_sgl)
1873 + DPRINT_ERR(STORVSC_DRV, "unable to create bounce buffer for this scmnd %p", scmnd);
1875 + scmnd->scsi_done = NULL;
1876 + scmnd->host_scribble = NULL;
1877 + kmem_cache_free(host_device_ctx->request_pool, cmd_request);
1879 + return SCSI_MLQUEUE_HOST_BUSY;
1882 +#ifdef KERNEL_2_6_27
1883 + cmd_request->bounce_sgl_count = ALIGN_UP(scsi_bufflen(scmnd), PAGE_SIZE) >> PAGE_SHIFT;
1885 + cmd_request->bounce_sgl_count = ALIGN_UP(scmnd->request_bufflen, PAGE_SIZE) >> PAGE_SHIFT;
1888 + //printk("bouncing buffer allocated %p original buffer %p\n", bounce_sgl, sgl);
1889 + //printk("copy_to_bounce_buffer\n");
1890 + // FIXME: We can optimize on reads by just skipping this
1891 +#ifdef KERNEL_2_6_27
1892 + copy_to_bounce_buffer(sgl, cmd_request->bounce_sgl, scsi_sg_count(scmnd));
1894 + copy_to_bounce_buffer(sgl, cmd_request->bounce_sgl, scmnd->use_sg);
1897 + sgl = cmd_request->bounce_sgl;
1900 + request->DataBuffer.Offset = sgl[0].offset;
1902 +#ifdef KERNEL_2_6_27
1903 + for (i = 0; i < scsi_sg_count(scmnd); i++ )
1905 + for (i = 0; i < scmnd->use_sg; i++ )
1908 + DPRINT_DBG(STORVSC_DRV, "sgl[%d] len %d offset %d \n", i, sgl[i].length, sgl[i].offset);
1909 +#ifdef KERNEL_2_6_27
1910 + request->DataBuffer.PfnArray[i] = page_to_pfn(sg_page((&sgl[i])));
1912 + request->DataBuffer.PfnArray[i] = page_to_pfn(sgl[i].page);
1917 +#ifdef KERNEL_2_6_27
1918 + else if (scsi_sglist(scmnd))
1920 + ASSERT(scsi_bufflen(scmnd) <= PAGE_SIZE);
1921 + request->DataBuffer.Offset = virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
1922 + request->DataBuffer.PfnArray[0] = virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
1926 + ASSERT(scsi_bufflen(scmnd) == 0);
1929 + else if (scmnd->request_buffer)
1931 + ASSERT(scmnd->request_bufflen <= PAGE_SIZE);
1932 + request->DataBuffer.Offset = virt_to_phys(scmnd->request_buffer) & (PAGE_SIZE-1);
1933 + request->DataBuffer.PfnArray[0] = virt_to_phys(scmnd->request_buffer) >> PAGE_SHIFT;
1937 + ASSERT(scmnd->request_bufflen == 0);
1943 + // Invokes the vsc to start an IO
1944 + ret = storvsc_drv_obj->OnIORequest(&device_ctx->device_obj, &cmd_request->request);
1945 + if (ret == -1) // no more space
1947 + DPRINT_ERR(STORVSC_DRV, "scmnd (%p) - queue FULL...marking queue busy", scmnd);
1949 + if (cmd_request->bounce_sgl_count)
1951 + // FIXME: We can optimize on writes by just skipping this
1952 +#ifdef KERNEL_2_6_27
1953 + copy_from_bounce_buffer(scsi_sglist(scmnd), cmd_request->bounce_sgl, scsi_sg_count(scmnd));
1955 + copy_from_bounce_buffer(
1956 + scmnd->request_buffer,
1957 + cmd_request->bounce_sgl,
1960 + destroy_bounce_buffer(cmd_request->bounce_sgl, cmd_request->bounce_sgl_count);
1963 + kmem_cache_free(host_device_ctx->request_pool, cmd_request);
1965 + scmnd->scsi_done = NULL;
1966 + scmnd->host_scribble = NULL;
1968 + ret = SCSI_MLQUEUE_DEVICE_BUSY;
1971 + DPRINT_EXIT(STORVSC_DRV);
1976 +#ifdef KERNEL_2_6_27
1977 +static int storvsc_merge_bvec(struct request_queue *q, struct bvec_merge_data *bmd, struct bio_vec *bvec)
1979 + return bvec->bv_len; //checking done by caller.
1982 +static int storvsc_merge_bvec(struct request_queue *q, struct bio *bio, struct bio_vec *bvec)
1984 + // Check if we are adding a new bvec
1985 + if (bio->bi_vcnt > 0)
1987 + //printk("storvsc_merge_bvec() - cnt %u offset %u len %u\n", bio->bi_vcnt, bvec->bv_offset, bvec->bv_len);
1989 + struct bio_vec *prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
1991 + return bvec->bv_len; // success
1993 + // Adding new bvec. Make sure the prev one is a complete page
1994 + if (prev->bv_len == PAGE_SIZE && prev->bv_offset == 0)
1996 + return bvec->bv_len; // success
2000 + // Dont reject if the new bvec starts off from the prev one since
2001 + // they will be merge into 1 bvec or blk_rq_map_sg() will merge them into 1 sg element
2002 + if ((bvec->bv_page == prev->bv_page) &&
2003 + (bvec->bv_offset == prev->bv_offset + prev->bv_len))
2005 + return bvec->bv_len; // success
2009 + DPRINT_INFO(STORVSC_DRV, "detected holes in bio request (%p) - cnt %u offset %u len %u", bio, bio->bi_vcnt, bvec->bv_offset, bvec->bv_len);
2010 + return 0; // dont add the bvec to this bio since we dont allow holes in the middle of a multi-pages bio
2015 + return bvec->bv_len; // success
2023 +Name: storvsc_device_configure()
2025 +Desc: Configure the specified scsi device
2028 +static int storvsc_device_alloc(struct scsi_device *sdevice)
2030 +#ifdef KERNEL_2_6_5
2032 + DPRINT_DBG(STORVSC_DRV, "sdev (%p) - setting device flag to %d", sdevice, BLIST_SPARSELUN);
2033 + // This enables luns to be located sparsely. Otherwise, we may not discovered them.
2034 + sdevice->sdev_bflags |= BLIST_SPARSELUN | BLIST_LARGELUN;
2039 +static int storvsc_device_configure(struct scsi_device *sdevice)
2041 + DPRINT_INFO(STORVSC_DRV, "sdev (%p) - curr queue depth %d", sdevice, sdevice->queue_depth);
2043 + DPRINT_INFO(STORVSC_DRV, "sdev (%p) - setting queue depth to %d", sdevice, STORVSC_MAX_IO_REQUESTS);
2044 + scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG, STORVSC_MAX_IO_REQUESTS);
2046 + DPRINT_INFO(STORVSC_DRV, "sdev (%p) - setting max segment size to %d", sdevice, PAGE_SIZE);
2047 + blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
2049 + DPRINT_INFO(STORVSC_DRV, "sdev (%p) - adding merge bio vec routine", sdevice);
2050 + blk_queue_merge_bvec(sdevice->request_queue, storvsc_merge_bvec);
2052 + blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
2053 + //sdevice->timeout = (2000 * HZ);//(75 * HZ);
2060 +Name: storvsc_host_reset_handler()
2062 +Desc: Reset the scsi HBA
2065 +static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
2068 + struct host_device_context *host_device_ctx = (struct host_device_context*)scmnd->device->host->hostdata;
2069 + struct device_context *device_ctx = host_device_ctx->device_ctx;
2070 + struct driver_context *driver_ctx = driver_to_driver_context(device_ctx->device.driver);
2071 + struct storvsc_driver_context *storvsc_drv_ctx = (struct storvsc_driver_context*)driver_ctx;
2073 + STORVSC_DRIVER_OBJECT *storvsc_drv_obj = &storvsc_drv_ctx->drv_obj;
2075 + DPRINT_ENTER(STORVSC_DRV);
2077 + DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host resetting...", scmnd->device, &device_ctx->device_obj);
2079 + // Invokes the vsc to reset the host/bus
2080 + ASSERT(storvsc_drv_obj->OnHostReset);
2081 + ret = storvsc_drv_obj->OnHostReset(&device_ctx->device_obj);
2084 + DPRINT_EXIT(STORVSC_DRV);
2088 + DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host reseted", scmnd->device, &device_ctx->device_obj);
2090 + DPRINT_EXIT(STORVSC_DRV);
2097 +Name: storvsc_host_rescan
2099 +Desc: Rescan the scsi HBA
2102 +#if defined(KERNEL_2_6_5) || defined(KERNEL_2_6_9)
2105 +#ifdef KERNEL_2_6_27
2106 +static void storvsc_host_rescan_callback(struct work_struct *work)
2108 + DEVICE_OBJECT* device_obj =
2109 + &((struct host_device_context*)work)->device_ctx->device_obj;
2111 +static void storvsc_host_rescan_callback(void* context)
2114 + DEVICE_OBJECT* device_obj = (DEVICE_OBJECT*)context;
2116 + struct device_context* device_ctx = to_device_context(device_obj);
2117 + struct Scsi_Host *host = (struct Scsi_Host *)device_ctx->device.driver_data;
2118 + struct scsi_device *sdev;
2119 + struct host_device_context *host_device_ctx;
2120 + struct scsi_device **sdevs_remove_list;
2121 + unsigned int sdevs_count=0;
2122 + unsigned int found;
2124 + unsigned int lun_count=0;
2125 + unsigned int *lun_list;
2127 + DPRINT_ENTER(STORVSC_DRV);
2129 + host_device_ctx = (struct host_device_context*)host->hostdata;
2130 + lun_list = kzalloc(sizeof(unsigned int)*STORVSC_MAX_LUNS_PER_TARGET, GFP_ATOMIC);
2133 + DPRINT_ERR(STORVSC_DRV, "unable to allocate lun list");
2137 + sdevs_remove_list = kzalloc(sizeof(void*)*STORVSC_MAX_LUNS_PER_TARGET, GFP_ATOMIC);
2138 + if (!sdevs_remove_list)
2141 + DPRINT_ERR(STORVSC_DRV, "unable to allocate lun remove list");
2145 + DPRINT_INFO(STORVSC_DRV, "rescanning host for new scsi devices...", device_obj, host_device_ctx->target, host_device_ctx->path);
2147 + // Rescan for new device
2148 + scsi_scan_target(&host->shost_gendev, host_device_ctx->path, host_device_ctx->target, SCAN_WILD_CARD, 1);
2150 + DPRINT_INFO(STORVSC_DRV, "rescanning host for removed scsi device...");
2152 + // Use the 1st device to send the report luns cmd
2153 + shost_for_each_device(sdev, host)
2155 + lun_count=STORVSC_MAX_LUNS_PER_TARGET;
2156 + storvsc_report_luns(sdev, lun_list, &lun_count);
2158 + DPRINT_INFO(STORVSC_DRV, "report luns on scsi device (%p) found %u luns ", sdev, lun_count);
2159 + DPRINT_INFO(STORVSC_DRV, "existing luns on scsi device (%p) host (%d)", sdev, host->host_no);
2161 + scsi_device_put(sdev);
2165 + for (i=0; i<lun_count; i++)
2167 + DPRINT_INFO(STORVSC_DRV, "%d) lun %u", i, lun_list[i]);
2170 + // Rescan for devices that may have been removed.
2171 + // We do not have to worry that new devices may have been added since
2172 + // this callback is serialized by the workqueue ie add/remove are done here.
2173 + shost_for_each_device(sdev, host)
2175 + // See if this device is still here
2177 + for (i=0; i<lun_count; i++)
2179 + if (sdev->lun == lun_list[i])
2187 + DPRINT_INFO(STORVSC_DRV, "lun (%u) does not exists", sdev->lun);
2188 + sdevs_remove_list[sdevs_count++] = sdev;
2192 + // Now remove the devices
2193 + for (i=0; i< sdevs_count; i++)
2195 + DPRINT_INFO(STORVSC_DRV, "removing scsi device (%p) lun (%u)...",
2196 + sdevs_remove_list[i], sdevs_remove_list[i]->lun);
2198 + // make sure it is not removed from underneath us
2199 + if (!scsi_device_get(sdevs_remove_list[i]))
2201 + scsi_remove_device(sdevs_remove_list[i]);
2202 + scsi_device_put(sdevs_remove_list[i]);
2206 + DPRINT_INFO(STORVSC_DRV, "rescan completed on dev obj (%p) target (%u) bus (%u)", device_obj, host_device_ctx->target, host_device_ctx->path);
2209 + kfree(sdevs_remove_list);
2211 + DPRINT_EXIT(STORVSC_DRV);
2214 +static int storvsc_report_luns(struct scsi_device *sdev, unsigned int luns[], unsigned int *lun_count)
2217 + unsigned int lun=0;
2218 + unsigned int num_luns;
2220 + unsigned char *data;
2221 +#if defined(KERNEL_2_6_5) || defined(KERNEL_2_6_9)
2223 + struct scsi_sense_hdr sshdr;
2225 + unsigned char cmd[16]={0};
2226 + unsigned int report_len = 8*(STORVSC_MAX_LUNS_PER_TARGET+1); // Add 1 to cover the report_lun header
2227 + unsigned long long *report_luns;
2228 + const unsigned int in_lun_count = *lun_count;
2232 + report_luns = kzalloc(report_len, GFP_ATOMIC);
2238 + cmd[0] = REPORT_LUNS;
2241 + *(unsigned int*)&cmd[6] = cpu_to_be32(report_len);
2243 + result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, (unsigned char*)report_luns, report_len, &sshdr, 30*HZ, 3);
2246 + kfree(report_luns);
2250 + // get the length from the first four bytes
2251 + report_len = be32_to_cpu(*(unsigned int*)&report_luns[0]);
2253 + num_luns = (report_len / sizeof(unsigned long long));
2254 + if (num_luns > in_lun_count)
2256 + kfree(report_luns);
2260 + *lun_count = num_luns;
2262 + DPRINT_DBG(STORVSC_DRV, "report luns on scsi device (%p) found %u luns ", sdev, num_luns);
2264 + // lun id starts at 1
2265 + for (i=1; i< num_luns+1; i++)
2268 + data = (unsigned char*)&report_luns[i];
2269 + for (j = 0; j < sizeof(lun); j += 2)
2271 + lun = lun | (((data[j] << 8) | data[j + 1]) << (j * 8));
2277 + kfree(report_luns);
2280 +#endif // KERNEL_2_6_9
2282 +static void storvsc_host_rescan(DEVICE_OBJECT* device_obj)
2284 + struct device_context* device_ctx = to_device_context(device_obj);
2285 + struct Scsi_Host *host = (struct Scsi_Host *)device_ctx->device.driver_data;
2286 + struct host_device_context *host_device_ctx;
2288 + DPRINT_ENTER(STORVSC_DRV);
2289 +#if defined(KERNEL_2_6_5) || defined(KERNEL_2_6_9)
2290 + DPRINT_ERR(STORVSC_DRV, "rescan not supported on 2.6.9 kernels!! You will need to reboot if you have added or removed the scsi lun device");
2293 + host_device_ctx = (struct host_device_context*)host->hostdata;
2295 + DPRINT_INFO(STORVSC_DRV, "initiating rescan on dev obj (%p) target (%u) bus (%u)...", device_obj, host_device_ctx->target, host_device_ctx->path);
2297 + // We need to queue this since the scanning may block and the caller may be in an intr context
2298 + //scsi_queue_work(host, &host_device_ctx->host_rescan_work);
2299 + schedule_work(&host_device_ctx->host_rescan_work);
2300 +#endif // KERNEL_2_6_9
2301 + DPRINT_EXIT(STORVSC_DRV);
2304 +static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev, sector_t capacity, int *info)
2306 + sector_t total_sectors = capacity;
2307 + sector_t cylinder_times_heads=0;
2310 + int sectors_per_track=0;
2315 + if (total_sectors > (65535 * 16 * 255)) {
2316 + total_sectors = (65535 * 16 * 255);
2319 + if (total_sectors >= (65535 * 16 * 63)) {
2320 + sectors_per_track = 255;
2323 + cylinder_times_heads = total_sectors;
2324 + rem = sector_div(cylinder_times_heads, sectors_per_track); // sector_div stores the quotient in cylinder_times_heads
2328 + sectors_per_track = 17;
2330 + cylinder_times_heads = total_sectors;
2331 + rem = sector_div(cylinder_times_heads, sectors_per_track); // sector_div stores the quotient in cylinder_times_heads
2333 + temp = cylinder_times_heads + 1023;
2334 + rem = sector_div(temp, 1024); // sector_div stores the quotient in temp
2342 + if (cylinder_times_heads >= (heads * 1024) || (heads > 16)) {
2343 + sectors_per_track = 31;
2346 + cylinder_times_heads = total_sectors;
2347 + rem = sector_div(cylinder_times_heads, sectors_per_track); // sector_div stores the quotient in cylinder_times_heads
2350 + if (cylinder_times_heads >= (heads * 1024)) {
2351 + sectors_per_track = 63;
2354 + cylinder_times_heads = total_sectors;
2355 + rem = sector_div(cylinder_times_heads, sectors_per_track); // sector_div stores the quotient in cylinder_times_heads
2359 + temp = cylinder_times_heads;
2360 + rem = sector_div(temp, heads); // sector_div stores the quotient in temp
2364 + info[1] = sectors_per_track;
2365 + info[2] = cylinders;
2367 + DPRINT_INFO(STORVSC_DRV, "CHS (%d, %d, %d)", cylinders, heads, sectors_per_track);
2372 +MODULE_LICENSE("GPL");
2374 +static int __init storvsc_init(void)
2378 + DPRINT_ENTER(STORVSC_DRV);
2380 + DPRINT_INFO(STORVSC_DRV, "Storvsc initializing....");
2382 + ret = storvsc_drv_init(StorVscInitialize);
2384 + DPRINT_EXIT(STORVSC_DRV);
2389 +static void __exit storvsc_exit(void)
2391 + DPRINT_ENTER(STORVSC_DRV);
2393 + storvsc_drv_exit();
2395 + DPRINT_ENTER(STORVSC_DRV);
2398 +module_param(storvsc_ringbuffer_size, int, S_IRUGO);
2400 +module_init(storvsc_init);
2401 +module_exit(storvsc_exit);