]> git.ipfire.org Git - ipfire-2.x.git/blame - src/patches/suse-2.6.27.39/patches.drivers/staging-hv-add-the-hyper-v-virtual-storage-driver.patch
Fix oinkmaster patch.
[ipfire-2.x.git] / src / patches / suse-2.6.27.39 / patches.drivers / staging-hv-add-the-hyper-v-virtual-storage-driver.patch
CommitLineData
2cb7cef9
BS
1From foo@baz Mon Jul 13 16:01:31 PDT 2009
2Date: Mon, 13 Jul 2009 16:01:31 -0700
3From: Hank Janssen <hjanssen@microsoft.com>
4Subject: Staging: hv: add the Hyper-V virtual storage driver
5
6From: Hank Janssen <hjanssen@microsoft.com>
7
8This is the virtual storage driver when running Linux on top of Hyper-V.
9
10Signed-off-by: Hank Janssen <hjanssen@microsoft.com>
11Signed-off-by: Haiyang Zhang <haiyangz@microsoft.com>
12Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
13---
14 drivers/staging/hv/StorVsc.c | 967 ++++++++++++++++++++++++++
15 drivers/staging/hv/storvsc_drv.c | 1413 +++++++++++++++++++++++++++++++++++++++
16 2 files changed, 2380 insertions(+)
17
18--- /dev/null
19+++ b/drivers/staging/hv/StorVsc.c
20@@ -0,0 +1,967 @@
21+/*
22+ *
23+ * Copyright (c) 2009, Microsoft Corporation.
24+ *
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.
28+ *
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
32+ * more details.
33+ *
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.
37+ *
38+ * Authors:
39+ * Haiyang Zhang <haiyangz@microsoft.com>
40+ * Hank Janssen <hjanssen@microsoft.com>
41+ *
42+ */
43+
44+
45+#include "logging.h"
46+
47+#include "StorVscApi.h"
48+#include "VmbusPacketFormat.h"
49+#include "vstorage.h"
50+
51+
52+//
53+// #defines
54+//
55+
56+//
57+// Data types
58+//
59+
60+typedef struct _STORVSC_REQUEST_EXTENSION {
61+ //LIST_ENTRY ListEntry;
62+
63+ STORVSC_REQUEST *Request;
64+ DEVICE_OBJECT *Device;
65+
66+ // Synchronize the request/response if needed
67+ HANDLE WaitEvent;
68+
69+ VSTOR_PACKET VStorPacket;
70+} STORVSC_REQUEST_EXTENSION;
71+
72+
73+// A storvsc device is a device object that contains a vmbus channel
74+typedef struct _STORVSC_DEVICE{
75+ DEVICE_OBJECT *Device;
76+
77+ int RefCount; // 0 indicates the device is being destroyed
78+
79+ int NumOutstandingRequests;
80+
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
83+ ULONG PortNumber;
84+ UCHAR PathId;
85+ UCHAR TargetId;
86+
87+ //LIST_ENTRY OutstandingRequestList;
88+ //HANDLE OutstandingRequestLock;
89+
90+ // Used for vsc/vsp channel reset process
91+ STORVSC_REQUEST_EXTENSION InitRequest;
92+
93+ STORVSC_REQUEST_EXTENSION ResetRequest;
94+
95+} STORVSC_DEVICE;
96+
97+
98+//
99+// Globals
100+//
101+static const char* gDriverName="storvsc";
102+
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}
106+};
107+
108+//
109+// Internal routines
110+//
111+static int
112+StorVscOnDeviceAdd(
113+ DEVICE_OBJECT *Device,
114+ void *AdditionalInfo
115+ );
116+
117+static int
118+StorVscOnDeviceRemove(
119+ DEVICE_OBJECT *Device
120+ );
121+
122+static int
123+StorVscOnIORequest(
124+ DEVICE_OBJECT *Device,
125+ STORVSC_REQUEST *Request
126+ );
127+
128+static int
129+StorVscOnHostReset(
130+ DEVICE_OBJECT *Device
131+ );
132+
133+static void
134+StorVscOnCleanup(
135+ DRIVER_OBJECT *Device
136+ );
137+
138+static void
139+StorVscOnChannelCallback(
140+ PVOID Context
141+ );
142+
143+static void
144+StorVscOnIOCompletion(
145+ DEVICE_OBJECT *Device,
146+ VSTOR_PACKET *VStorPacket,
147+ STORVSC_REQUEST_EXTENSION *RequestExt
148+ );
149+
150+static void
151+StorVscOnReceive(
152+ DEVICE_OBJECT *Device,
153+ VSTOR_PACKET *VStorPacket,
154+ STORVSC_REQUEST_EXTENSION *RequestExt
155+ );
156+
157+static int
158+StorVscConnectToVsp(
159+ DEVICE_OBJECT *Device
160+ );
161+
162+static inline STORVSC_DEVICE* AllocStorDevice(DEVICE_OBJECT *Device)
163+{
164+ STORVSC_DEVICE *storDevice;
165+
166+ storDevice = MemAllocZeroed(sizeof(STORVSC_DEVICE));
167+ if (!storDevice)
168+ return NULL;
169+
170+ // Set to 2 to allow both inbound and outbound traffics
171+ // (ie GetStorDevice() and MustGetStorDevice()) to proceed.
172+ InterlockedCompareExchange(&storDevice->RefCount, 2, 0);
173+
174+ storDevice->Device = Device;
175+ Device->Extension = storDevice;
176+
177+ return storDevice;
178+}
179+
180+static inline void FreeStorDevice(STORVSC_DEVICE *Device)
181+{
182+ ASSERT(Device->RefCount == 0);
183+ MemFree(Device);
184+}
185+
186+// Get the stordevice object iff exists and its refcount > 1
187+static inline STORVSC_DEVICE* GetStorDevice(DEVICE_OBJECT *Device)
188+{
189+ STORVSC_DEVICE *storDevice;
190+
191+ storDevice = (STORVSC_DEVICE*)Device->Extension;
192+ if (storDevice && storDevice->RefCount > 1)
193+ {
194+ InterlockedIncrement(&storDevice->RefCount);
195+ }
196+ else
197+ {
198+ storDevice = NULL;
199+ }
200+
201+ return storDevice;
202+}
203+
204+// Get the stordevice object iff exists and its refcount > 0
205+static inline STORVSC_DEVICE* MustGetStorDevice(DEVICE_OBJECT *Device)
206+{
207+ STORVSC_DEVICE *storDevice;
208+
209+ storDevice = (STORVSC_DEVICE*)Device->Extension;
210+ if (storDevice && storDevice->RefCount)
211+ {
212+ InterlockedIncrement(&storDevice->RefCount);
213+ }
214+ else
215+ {
216+ storDevice = NULL;
217+ }
218+
219+ return storDevice;
220+}
221+
222+static inline void PutStorDevice(DEVICE_OBJECT *Device)
223+{
224+ STORVSC_DEVICE *storDevice;
225+
226+ storDevice = (STORVSC_DEVICE*)Device->Extension;
227+ ASSERT(storDevice);
228+
229+ InterlockedDecrement(&storDevice->RefCount);
230+ ASSERT(storDevice->RefCount);
231+}
232+
233+// Drop ref count to 1 to effectively disable GetStorDevice()
234+static inline STORVSC_DEVICE* ReleaseStorDevice(DEVICE_OBJECT *Device)
235+{
236+ STORVSC_DEVICE *storDevice;
237+
238+ storDevice = (STORVSC_DEVICE*)Device->Extension;
239+ ASSERT(storDevice);
240+
241+ // Busy wait until the ref drop to 2, then set it to 1
242+ while (InterlockedCompareExchange(&storDevice->RefCount, 1, 2) != 2)
243+ {
244+ Sleep(100);
245+ }
246+
247+ return storDevice;
248+}
249+
250+// Drop ref count to 0. No one can use StorDevice object.
251+static inline STORVSC_DEVICE* FinalReleaseStorDevice(DEVICE_OBJECT *Device)
252+{
253+ STORVSC_DEVICE *storDevice;
254+
255+ storDevice = (STORVSC_DEVICE*)Device->Extension;
256+ ASSERT(storDevice);
257+
258+ // Busy wait until the ref drop to 1, then set it to 0
259+ while (InterlockedCompareExchange(&storDevice->RefCount, 0, 1) != 1)
260+ {
261+ Sleep(100);
262+ }
263+
264+ Device->Extension = NULL;
265+ return storDevice;
266+}
267+
268+/*++;
269+
270+
271+Name:
272+ StorVscInitialize()
273+
274+Description:
275+ Main entry point
276+
277+--*/
278+int
279+StorVscInitialize(
280+ DRIVER_OBJECT *Driver
281+ )
282+{
283+ STORVSC_DRIVER_OBJECT* storDriver = (STORVSC_DRIVER_OBJECT*)Driver;
284+ int ret=0;
285+
286+ DPRINT_ENTER(STORVSC);
287+
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));
290+
291+ // Make sure we are at least 2 pages since 1 page is used for control
292+ ASSERT(storDriver->RingBufferSize >= (PAGE_SIZE << 1));
293+
294+ Driver->name = gDriverName;
295+ memcpy(&Driver->deviceType, &gStorVscDeviceType, sizeof(GUID));
296+
297+ storDriver->RequestExtSize = sizeof(STORVSC_REQUEST_EXTENSION);
298+
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)));
303+
304+ DPRINT_INFO(STORVSC, "max io %u, currently %u\n", storDriver->MaxOutstandingRequestsPerChannel, STORVSC_MAX_IO_REQUESTS);
305+
306+ // Setup the dispatch table
307+ storDriver->Base.OnDeviceAdd = StorVscOnDeviceAdd;
308+ storDriver->Base.OnDeviceRemove = StorVscOnDeviceRemove;
309+ storDriver->Base.OnCleanup = StorVscOnCleanup;
310+
311+ storDriver->OnIORequest = StorVscOnIORequest;
312+ storDriver->OnHostReset = StorVscOnHostReset;
313+
314+ DPRINT_EXIT(STORVSC);
315+
316+ return ret;
317+}
318+
319+/*++
320+
321+Name:
322+ StorVscOnDeviceAdd()
323+
324+Description:
325+ Callback when the device belonging to this driver is added
326+
327+--*/
328+int
329+StorVscOnDeviceAdd(
330+ DEVICE_OBJECT *Device,
331+ void *AdditionalInfo
332+ )
333+{
334+ int ret=0;
335+ STORVSC_DEVICE *storDevice;
336+ //VMSTORAGE_CHANNEL_PROPERTIES *props;
337+ STORVSC_DEVICE_INFO *deviceInfo = (STORVSC_DEVICE_INFO*)AdditionalInfo;
338+
339+ DPRINT_ENTER(STORVSC);
340+
341+ storDevice = AllocStorDevice(Device);
342+ if (!storDevice)
343+ {
344+ ret = -1;
345+ goto Cleanup;
346+ }
347+
348+ // Save the channel properties to our storvsc channel
349+ //props = (VMSTORAGE_CHANNEL_PROPERTIES*) channel->offerMsg.Offer.u.Standard.UserDefined;
350+
351+ // FIXME:
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;*/
357+
358+ storDevice->PortNumber = deviceInfo->PortNumber;
359+ // Send it back up
360+ ret = StorVscConnectToVsp(Device);
361+
362+ //deviceInfo->PortNumber = storDevice->PortNumber;
363+ deviceInfo->PathId = storDevice->PathId;
364+ deviceInfo->TargetId = storDevice->TargetId;
365+
366+ DPRINT_DBG(STORVSC, "assigned port %u, path %u target %u\n", storDevice->PortNumber, storDevice->PathId, storDevice->TargetId);
367+
368+Cleanup:
369+ DPRINT_EXIT(STORVSC);
370+
371+ return ret;
372+}
373+
374+static int StorVscChannelInit(DEVICE_OBJECT *Device)
375+{
376+ int ret=0;
377+ STORVSC_DEVICE *storDevice;
378+ STORVSC_REQUEST_EXTENSION *request;
379+ VSTOR_PACKET *vstorPacket;
380+
381+ storDevice = GetStorDevice(Device);
382+ if (!storDevice)
383+ {
384+ DPRINT_ERR(STORVSC, "unable to get stor device...device being destroyed?");
385+ DPRINT_EXIT(STORVSC);
386+ return -1;
387+ }
388+
389+ request = &storDevice->InitRequest;
390+ vstorPacket = &request->VStorPacket;
391+
392+ // Now, initiate the vsc/vsp initialization protocol on the open channel
393+
394+ memset(request, sizeof(STORVSC_REQUEST_EXTENSION), 0);
395+ request->WaitEvent = WaitEventCreate();
396+
397+ vstorPacket->Operation = VStorOperationBeginInitialization;
398+ vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
399+
400+ /*SpinlockAcquire(gDriverExt.packetListLock);
401+ INSERT_TAIL_LIST(&gDriverExt.packetList, &packet->listEntry.entry);
402+ SpinlockRelease(gDriverExt.packetListLock);*/
403+
404+ DPRINT_INFO(STORVSC, "BEGIN_INITIALIZATION_OPERATION...");
405+
406+ ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
407+ vstorPacket,
408+ sizeof(VSTOR_PACKET),
409+ (ULONG_PTR)request,
410+ VmbusPacketTypeDataInBand,
411+ VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
412+ if ( ret != 0)
413+ {
414+ DPRINT_ERR(STORVSC, "unable to send BEGIN_INITIALIZATION_OPERATION");
415+ goto Cleanup;
416+ }
417+
418+ WaitEventWait(request->WaitEvent);
419+
420+ if (vstorPacket->Operation != VStorOperationCompleteIo || vstorPacket->Status != 0)
421+ {
422+ DPRINT_ERR(STORVSC, "BEGIN_INITIALIZATION_OPERATION failed (op %d status 0x%x)", vstorPacket->Operation, vstorPacket->Status);
423+ goto Cleanup;
424+ }
425+
426+ DPRINT_INFO(STORVSC, "QUERY_PROTOCOL_VERSION_OPERATION...");
427+
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;
432+
433+ vstorPacket->Version.MajorMinor = VMSTOR_PROTOCOL_VERSION_CURRENT;
434+ FILL_VMSTOR_REVISION(vstorPacket->Version.Revision);
435+
436+ ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
437+ vstorPacket,
438+ sizeof(VSTOR_PACKET),
439+ (ULONG_PTR)request,
440+ VmbusPacketTypeDataInBand,
441+ VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
442+ if ( ret != 0)
443+ {
444+ DPRINT_ERR(STORVSC, "unable to send BEGIN_INITIALIZATION_OPERATION");
445+ goto Cleanup;
446+ }
447+
448+ WaitEventWait(request->WaitEvent);
449+
450+ // TODO: Check returned version
451+ if (vstorPacket->Operation != VStorOperationCompleteIo || vstorPacket->Status != 0)
452+ {
453+ DPRINT_ERR(STORVSC, "QUERY_PROTOCOL_VERSION_OPERATION failed (op %d status 0x%x)", vstorPacket->Operation, vstorPacket->Status);
454+ goto Cleanup;
455+ }
456+
457+ // Query channel properties
458+ DPRINT_INFO(STORVSC, "QUERY_PROPERTIES_OPERATION...");
459+
460+ memset(vstorPacket, sizeof(VSTOR_PACKET), 0);
461+ vstorPacket->Operation = VStorOperationQueryProperties;
462+ vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
463+ vstorPacket->StorageChannelProperties.PortNumber = storDevice->PortNumber;
464+
465+ ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
466+ vstorPacket,
467+ sizeof(VSTOR_PACKET),
468+ (ULONG_PTR)request,
469+ VmbusPacketTypeDataInBand,
470+ VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
471+
472+ if ( ret != 0)
473+ {
474+ DPRINT_ERR(STORVSC, "unable to send QUERY_PROPERTIES_OPERATION");
475+ goto Cleanup;
476+ }
477+
478+ WaitEventWait(request->WaitEvent);
479+
480+ // TODO: Check returned version
481+ if (vstorPacket->Operation != VStorOperationCompleteIo || vstorPacket->Status != 0)
482+ {
483+ DPRINT_ERR(STORVSC, "QUERY_PROPERTIES_OPERATION failed (op %d status 0x%x)", vstorPacket->Operation, vstorPacket->Status);
484+ goto Cleanup;
485+ }
486+
487+ //storDevice->PortNumber = vstorPacket->StorageChannelProperties.PortNumber;
488+ storDevice->PathId = vstorPacket->StorageChannelProperties.PathId;
489+ storDevice->TargetId = vstorPacket->StorageChannelProperties.TargetId;
490+
491+ DPRINT_DBG(STORVSC, "channel flag 0x%x, max xfer len 0x%x", vstorPacket->StorageChannelProperties.Flags, vstorPacket->StorageChannelProperties.MaxTransferBytes);
492+
493+ DPRINT_INFO(STORVSC, "END_INITIALIZATION_OPERATION...");
494+
495+ memset(vstorPacket, sizeof(VSTOR_PACKET), 0);
496+ vstorPacket->Operation = VStorOperationEndInitialization;
497+ vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
498+
499+ ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
500+ vstorPacket,
501+ sizeof(VSTOR_PACKET),
502+ (ULONG_PTR)request,
503+ VmbusPacketTypeDataInBand,
504+ VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
505+
506+ if ( ret != 0)
507+ {
508+ DPRINT_ERR(STORVSC, "unable to send END_INITIALIZATION_OPERATION");
509+ goto Cleanup;
510+ }
511+
512+ WaitEventWait(request->WaitEvent);
513+
514+ if (vstorPacket->Operation != VStorOperationCompleteIo || vstorPacket->Status != 0)
515+ {
516+ DPRINT_ERR(STORVSC, "END_INITIALIZATION_OPERATION failed (op %d status 0x%x)", vstorPacket->Operation, vstorPacket->Status);
517+ goto Cleanup;
518+ }
519+
520+ DPRINT_INFO(STORVSC, "**** storage channel up and running!! ****");
521+
522+Cleanup:
523+ if (request->WaitEvent)
524+ {
525+ WaitEventClose(request->WaitEvent);
526+ request->WaitEvent = NULL;
527+ }
528+
529+ PutStorDevice(Device);
530+
531+ DPRINT_EXIT(STORVSC);
532+ return ret;
533+}
534+
535+
536+int
537+StorVscConnectToVsp(
538+ DEVICE_OBJECT *Device
539+ )
540+{
541+ int ret=0;
542+ VMSTORAGE_CHANNEL_PROPERTIES props;
543+
544+ STORVSC_DRIVER_OBJECT *storDriver = (STORVSC_DRIVER_OBJECT*) Device->Driver;;
545+
546+ memset(&props, sizeof(VMSTORAGE_CHANNEL_PROPERTIES), 0);
547+
548+ // Open the channel
549+ ret = Device->Driver->VmbusChannelInterface.Open(Device,
550+ storDriver->RingBufferSize,
551+ storDriver->RingBufferSize,
552+ (PVOID)&props,
553+ sizeof(VMSTORAGE_CHANNEL_PROPERTIES),
554+ StorVscOnChannelCallback,
555+ Device
556+ );
557+
558+ DPRINT_DBG(STORVSC, "storage props: path id %d, tgt id %d, max xfer %d", props.PathId, props.TargetId, props.MaxTransferBytes);
559+
560+ if (ret != 0)
561+ {
562+ DPRINT_ERR(STORVSC, "unable to open channel: %d", ret);
563+ return -1;
564+ }
565+
566+ ret = StorVscChannelInit(Device);
567+
568+ return ret;
569+}
570+
571+
572+/*++
573+
574+Name:
575+ StorVscOnDeviceRemove()
576+
577+Description:
578+ Callback when the our device is being removed
579+
580+--*/
581+int
582+StorVscOnDeviceRemove(
583+ DEVICE_OBJECT *Device
584+ )
585+{
586+ STORVSC_DEVICE *storDevice;
587+ int ret=0;
588+
589+ DPRINT_ENTER(STORVSC);
590+
591+ DPRINT_INFO(STORVSC, "disabling storage device (%p)...", Device->Extension);
592+
593+ storDevice = ReleaseStorDevice(Device);
594+
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)
598+ {
599+ DPRINT_INFO(STORVSC, "waiting for %d requests to complete...", storDevice->NumOutstandingRequests);
600+
601+ Sleep(100);
602+ }
603+
604+ DPRINT_INFO(STORVSC, "removing storage device (%p)...", Device->Extension);
605+
606+ storDevice = FinalReleaseStorDevice(Device);
607+
608+ DPRINT_INFO(STORVSC, "storage device (%p) safe to remove", storDevice);
609+
610+ // Close the channel
611+ Device->Driver->VmbusChannelInterface.Close(Device);
612+
613+ FreeStorDevice(storDevice);
614+
615+ DPRINT_EXIT(STORVSC);
616+ return ret;
617+}
618+
619+
620+//static void
621+//StorVscOnTargetRescan(
622+// void *Context
623+// )
624+//{
625+// DEVICE_OBJECT *device=(DEVICE_OBJECT*)Context;
626+// STORVSC_DRIVER_OBJECT *storDriver;
627+//
628+// DPRINT_ENTER(STORVSC);
629+//
630+// storDriver = (STORVSC_DRIVER_OBJECT*) device->Driver;
631+// storDriver->OnHostRescan(device);
632+//
633+// DPRINT_EXIT(STORVSC);
634+//}
635+
636+int
637+StorVscOnHostReset(
638+ DEVICE_OBJECT *Device
639+ )
640+{
641+ int ret=0;
642+
643+ STORVSC_DEVICE *storDevice;
644+ STORVSC_REQUEST_EXTENSION *request;
645+ VSTOR_PACKET *vstorPacket;
646+
647+ DPRINT_ENTER(STORVSC);
648+
649+ DPRINT_INFO(STORVSC, "resetting host adapter...");
650+
651+ storDevice = GetStorDevice(Device);
652+ if (!storDevice)
653+ {
654+ DPRINT_ERR(STORVSC, "unable to get stor device...device being destroyed?");
655+ DPRINT_EXIT(STORVSC);
656+ return -1;
657+ }
658+
659+ request = &storDevice->ResetRequest;
660+ vstorPacket = &request->VStorPacket;
661+
662+ request->WaitEvent = WaitEventCreate();
663+
664+ vstorPacket->Operation = VStorOperationResetBus;
665+ vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
666+ vstorPacket->VmSrb.PathId = storDevice->PathId;
667+
668+ ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
669+ vstorPacket,
670+ sizeof(VSTOR_PACKET),
671+ (ULONG_PTR)&storDevice->ResetRequest,
672+ VmbusPacketTypeDataInBand,
673+ VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
674+ if (ret != 0)
675+ {
676+ DPRINT_ERR(STORVSC, "Unable to send reset packet %p ret %d", vstorPacket, ret);
677+ goto Cleanup;
678+ }
679+
680+ // FIXME: Add a timeout
681+ WaitEventWait(request->WaitEvent);
682+
683+ WaitEventClose(request->WaitEvent);
684+ DPRINT_INFO(STORVSC, "host adapter reset completed");
685+
686+ // At this point, all outstanding requests in the adapter should have been flushed out and return to us
687+
688+Cleanup:
689+ PutStorDevice(Device);
690+ DPRINT_EXIT(STORVSC);
691+ return ret;
692+}
693+
694+/*++
695+
696+Name:
697+ StorVscOnIORequest()
698+
699+Description:
700+ Callback to initiate an I/O request
701+
702+--*/
703+int
704+StorVscOnIORequest(
705+ DEVICE_OBJECT *Device,
706+ STORVSC_REQUEST *Request
707+ )
708+{
709+ STORVSC_DEVICE *storDevice;
710+ STORVSC_REQUEST_EXTENSION* requestExtension = (STORVSC_REQUEST_EXTENSION*) Request->Extension;
711+ VSTOR_PACKET* vstorPacket =&requestExtension->VStorPacket;
712+ int ret=0;
713+
714+ DPRINT_ENTER(STORVSC);
715+
716+ storDevice = GetStorDevice(Device);
717+
718+ DPRINT_DBG(STORVSC, "enter - Device %p, DeviceExt %p, Request %p, Extension %p",
719+ Device, storDevice, Request, requestExtension);
720+
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);
723+
724+ if (!storDevice)
725+ {
726+ DPRINT_ERR(STORVSC, "unable to get stor device...device being destroyed?");
727+ DPRINT_EXIT(STORVSC);
728+ return -2;
729+ }
730+
731+ //PrintBytes(Request->Cdb, Request->CdbLen);
732+
733+ requestExtension->Request = Request;
734+ requestExtension->Device = Device;
735+
736+ memset(vstorPacket, 0 , sizeof(VSTOR_PACKET));
737+
738+ vstorPacket->Flags |= REQUEST_COMPLETION_FLAG;
739+
740+ vstorPacket->VmSrb.Length = sizeof(VMSCSI_REQUEST);
741+
742+ vstorPacket->VmSrb.PortNumber = Request->Host;
743+ vstorPacket->VmSrb.PathId = Request->Bus;
744+ vstorPacket->VmSrb.TargetId = Request->TargetId;
745+ vstorPacket->VmSrb.Lun = Request->LunId;
746+
747+ vstorPacket->VmSrb.SenseInfoLength = SENSE_BUFFER_SIZE;
748+
749+ // Copy over the scsi command descriptor block
750+ vstorPacket->VmSrb.CdbLength = Request->CdbLen;
751+ memcpy(&vstorPacket->VmSrb.Cdb, Request->Cdb, Request->CdbLen);
752+
753+ vstorPacket->VmSrb.DataIn = Request->Type;
754+ vstorPacket->VmSrb.DataTransferLength = Request->DataBuffer.Length;
755+
756+ vstorPacket->Operation = VStorOperationExecuteSRB;
757+
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);
766+
767+ if (requestExtension->Request->DataBuffer.Length)
768+ {
769+ ret = Device->Driver->VmbusChannelInterface.SendPacketMultiPageBuffer(Device,
770+ &requestExtension->Request->DataBuffer,
771+ vstorPacket,
772+ sizeof(VSTOR_PACKET),
773+ (ULONG_PTR)requestExtension);
774+ }
775+ else
776+ {
777+ ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
778+ vstorPacket,
779+ sizeof(VSTOR_PACKET),
780+ (ULONG_PTR)requestExtension,
781+ VmbusPacketTypeDataInBand,
782+ VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
783+ }
784+
785+ if (ret != 0)
786+ {
787+ DPRINT_DBG(STORVSC, "Unable to send packet %p ret %d", vstorPacket, ret);
788+ }
789+
790+ InterlockedIncrement(&storDevice->NumOutstandingRequests);
791+
792+ PutStorDevice(Device);
793+
794+ DPRINT_EXIT(STORVSC);
795+ return ret;
796+}
797+
798+/*++
799+
800+Name:
801+ StorVscOnCleanup()
802+
803+Description:
804+ Perform any cleanup when the driver is removed
805+
806+--*/
807+void
808+StorVscOnCleanup(
809+ DRIVER_OBJECT *Driver
810+ )
811+{
812+ DPRINT_ENTER(STORVSC);
813+ DPRINT_EXIT(STORVSC);
814+}
815+
816+
817+static void
818+StorVscOnIOCompletion(
819+ DEVICE_OBJECT *Device,
820+ VSTOR_PACKET *VStorPacket,
821+ STORVSC_REQUEST_EXTENSION *RequestExt
822+ )
823+{
824+ STORVSC_REQUEST *request;
825+ STORVSC_DEVICE *storDevice;
826+
827+ DPRINT_ENTER(STORVSC);
828+
829+ storDevice = MustGetStorDevice(Device);
830+ if (!storDevice)
831+ {
832+ DPRINT_ERR(STORVSC, "unable to get stor device...device being destroyed?");
833+ DPRINT_EXIT(STORVSC);
834+ return;
835+ }
836+
837+ DPRINT_DBG(STORVSC, "IO_COMPLETE_OPERATION - request extension %p completed bytes xfer %u",
838+ RequestExt, VStorPacket->VmSrb.DataTransferLength);
839+
840+ ASSERT(RequestExt != NULL);
841+ ASSERT(RequestExt->Request != NULL);
842+
843+ request = RequestExt->Request;
844+
845+ ASSERT(request->OnIOCompletion != NULL);
846+
847+ // Copy over the status...etc
848+ request->Status = VStorPacket->VmSrb.ScsiStatus;
849+
850+ if (request->Status != 0 || VStorPacket->VmSrb.SrbStatus != 1)
851+ {
852+ DPRINT_WARN(STORVSC, "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
853+ request->Cdb[0],
854+ VStorPacket->VmSrb.ScsiStatus,
855+ VStorPacket->VmSrb.SrbStatus);
856+ }
857+
858+ if ((request->Status & 0xFF) == 0x02) // CHECK_CONDITION
859+ {
860+ if (VStorPacket->VmSrb.SrbStatus & 0x80) // autosense data available
861+ {
862+ DPRINT_WARN(STORVSC, "storvsc pkt %p autosense data valid - len %d\n",
863+ RequestExt, VStorPacket->VmSrb.SenseInfoLength);
864+
865+ ASSERT(VStorPacket->VmSrb.SenseInfoLength <= request->SenseBufferSize);
866+ memcpy(request->SenseBuffer,
867+ VStorPacket->VmSrb.SenseData,
868+ VStorPacket->VmSrb.SenseInfoLength);
869+
870+ request->SenseBufferSize = VStorPacket->VmSrb.SenseInfoLength;
871+ }
872+ }
873+
874+ // TODO:
875+ request->BytesXfer = VStorPacket->VmSrb.DataTransferLength;
876+
877+ request->OnIOCompletion(request);
878+
879+ InterlockedDecrement(&storDevice->NumOutstandingRequests);
880+
881+ PutStorDevice(Device);
882+
883+ DPRINT_EXIT(STORVSC);
884+}
885+
886+
887+static void
888+StorVscOnReceive(
889+ DEVICE_OBJECT *Device,
890+ VSTOR_PACKET *VStorPacket,
891+ STORVSC_REQUEST_EXTENSION *RequestExt
892+ )
893+{
894+ switch(VStorPacket->Operation)
895+ {
896+ case VStorOperationCompleteIo:
897+
898+ DPRINT_DBG(STORVSC, "IO_COMPLETE_OPERATION");
899+ StorVscOnIOCompletion(Device, VStorPacket, RequestExt);
900+ break;
901+
902+ //case ENUMERATE_DEVICE_OPERATION:
903+
904+ // DPRINT_INFO(STORVSC, "ENUMERATE_DEVICE_OPERATION");
905+
906+ // StorVscOnTargetRescan(Device);
907+ // break;
908+
909+ case VStorOperationRemoveDevice:
910+
911+ DPRINT_INFO(STORVSC, "REMOVE_DEVICE_OPERATION");
912+ // TODO:
913+ break;
914+
915+ default:
916+ DPRINT_INFO(STORVSC, "Unknown operation received - %d", VStorPacket->Operation);
917+ break;
918+ }
919+}
920+
921+void
922+StorVscOnChannelCallback(
923+ PVOID Context
924+ )
925+{
926+ int ret=0;
927+ DEVICE_OBJECT *device = (DEVICE_OBJECT*)Context;
928+ STORVSC_DEVICE *storDevice;
929+ UINT32 bytesRecvd;
930+ UINT64 requestId;
931+ UCHAR packet[ALIGN_UP(sizeof(VSTOR_PACKET),8)];
932+ STORVSC_REQUEST_EXTENSION *request;
933+
934+ DPRINT_ENTER(STORVSC);
935+
936+ ASSERT(device);
937+
938+ storDevice = MustGetStorDevice(device);
939+ if (!storDevice)
940+ {
941+ DPRINT_ERR(STORVSC, "unable to get stor device...device being destroyed?");
942+ DPRINT_EXIT(STORVSC);
943+ return;
944+ }
945+
946+ do
947+ {
948+ ret = device->Driver->VmbusChannelInterface.RecvPacket(device,
949+ packet,
950+ ALIGN_UP(sizeof(VSTOR_PACKET),8),
951+ &bytesRecvd,
952+ &requestId);
953+ if (ret == 0 && bytesRecvd > 0)
954+ {
955+ DPRINT_DBG(STORVSC, "receive %d bytes - tid %llx", bytesRecvd, requestId);
956+
957+ //ASSERT(bytesRecvd == sizeof(VSTOR_PACKET));
958+
959+ request = (STORVSC_REQUEST_EXTENSION*)(ULONG_PTR)requestId;
960+ ASSERT(request);
961+
962+ //if (vstorPacket.Flags & SYNTHETIC_FLAG)
963+ if ((request == &storDevice->InitRequest) || (request == &storDevice->ResetRequest))
964+ {
965+ //DPRINT_INFO(STORVSC, "reset completion - operation %u status %u", vstorPacket.Operation, vstorPacket.Status);
966+
967+ memcpy(&request->VStorPacket, packet, sizeof(VSTOR_PACKET));
968+
969+ WaitEventSet(request->WaitEvent);
970+ }
971+ else
972+ {
973+ StorVscOnReceive(device, (VSTOR_PACKET*)packet, request);
974+ }
975+ }
976+ else
977+ {
978+ //DPRINT_DBG(STORVSC, "nothing else to read...");
979+ break;
980+ }
981+ } while (1);
982+
983+ PutStorDevice(device);
984+
985+ DPRINT_EXIT(STORVSC);
986+ return;
987+}
988--- /dev/null
989+++ b/drivers/staging/hv/storvsc_drv.c
990@@ -0,0 +1,1413 @@
991+/*
992+ *
993+ * Copyright (c) 2009, Microsoft Corporation.
994+ *
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.
998+ *
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
1002+ * more details.
1003+ *
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.
1007+ *
1008+ * Authors:
1009+ * Haiyang Zhang <haiyangz@microsoft.com>
1010+ * Hank Janssen <hjanssen@microsoft.com>
1011+ *
1012+ */
1013+
1014+
1015+#include <linux/init.h>
1016+#include <linux/module.h>
1017+#include <linux/device.h>
1018+#include <linux/blkdev.h>
1019+
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>
1027+
1028+#ifdef KERNEL_2_6_5
1029+#else
1030+#include <scsi/scsi_dbg.h>
1031+#endif
1032+
1033+#include "logging.h"
1034+#include "vmbus.h"
1035+
1036+#include "StorVscApi.h"
1037+
1038+//
1039+// #defines
1040+//
1041+
1042+//
1043+// Data types
1044+//
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;
1050+#else
1051+ kmem_cache_t *request_pool;
1052+#endif
1053+ unsigned int port;
1054+ unsigned char path;
1055+ unsigned char target;
1056+};
1057+
1058+struct storvsc_cmd_request {
1059+ struct list_head entry;
1060+ struct scsi_cmnd *cmd;
1061+
1062+ unsigned int bounce_sgl_count;
1063+ struct scatterlist *bounce_sgl;
1064+
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;
1068+};
1069+
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;
1074+};
1075+
1076+// Static decl
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);
1084+#else
1085+static void storvsc_host_rescan_callback(void* context);
1086+#endif
1087+static void storvsc_host_rescan(DEVICE_OBJECT* device_obj);
1088+static int storvsc_remove(struct device *dev);
1089+
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);
1095+
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);
1098+
1099+
1100+static int storvsc_ringbuffer_size = STORVSC_RING_BUFFER_SIZE;
1101+
1102+// The one and only one
1103+static struct storvsc_driver_context g_storvsc_drv;
1104+
1105+// Scsi driver
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,
1114+ .cmd_per_lun = 1,
1115+ .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS, // 64 max_queue * 1 target
1116+ .this_id = -1,
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,
1125+};
1126+
1127+
1128+/*++
1129+
1130+Name: storvsc_drv_init()
1131+
1132+Desc: StorVsc driver initialization.
1133+
1134+--*/
1135+int storvsc_drv_init(PFN_DRIVERINITIALIZE pfn_drv_init)
1136+{
1137+ int ret=0;
1138+ STORVSC_DRIVER_OBJECT *storvsc_drv_obj=&g_storvsc_drv.drv_obj;
1139+ struct driver_context *drv_ctx=&g_storvsc_drv.drv_ctx;
1140+
1141+ DPRINT_ENTER(STORVSC_DRV);
1142+
1143+ vmbus_get_interface(&storvsc_drv_obj->Base.VmbusChannelInterface);
1144+
1145+ storvsc_drv_obj->RingBufferSize = storvsc_ringbuffer_size;
1146+ storvsc_drv_obj->OnHostRescan = storvsc_host_rescan;
1147+
1148+ // Callback to client driver to complete the initialization
1149+ pfn_drv_init(&storvsc_drv_obj->Base);
1150+
1151+ DPRINT_INFO(STORVSC_DRV, "request extension size %u, max outstanding reqs %u", storvsc_drv_obj->RequestExtSize, storvsc_drv_obj->MaxOutstandingRequestsPerChannel);
1152+
1153+ if (storvsc_drv_obj->MaxOutstandingRequestsPerChannel < STORVSC_MAX_IO_REQUESTS)
1154+ {
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);
1157+ return -1;
1158+ }
1159+
1160+ drv_ctx->driver.name = storvsc_drv_obj->Base.name;
1161+ memcpy(&drv_ctx->class_id, &storvsc_drv_obj->Base.deviceType, sizeof(GUID));
1162+
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;
1166+#else
1167+ drv_ctx->probe = storvsc_probe;
1168+ drv_ctx->remove = storvsc_remove;
1169+#endif
1170+
1171+ // The driver belongs to vmbus
1172+ vmbus_child_driver_register(drv_ctx);
1173+
1174+ DPRINT_EXIT(STORVSC_DRV);
1175+
1176+ return ret;
1177+}
1178+
1179+
1180+static int storvsc_drv_exit_cb(struct device *dev, void *data)
1181+{
1182+ struct device **curr = (struct device **)data;
1183+ *curr = dev;
1184+ return 1; // stop iterating
1185+}
1186+
1187+/*++
1188+
1189+Name: storvsc_drv_exit()
1190+
1191+Desc:
1192+
1193+--*/
1194+void storvsc_drv_exit(void)
1195+{
1196+ STORVSC_DRIVER_OBJECT *storvsc_drv_obj=&g_storvsc_drv.drv_obj;
1197+ struct driver_context *drv_ctx=&g_storvsc_drv.drv_ctx;
1198+
1199+ struct device *current_dev=NULL;
1200+
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);\
1208+ }
1209+#endif // KERNEL_2_6_9
1210+
1211+ DPRINT_ENTER(STORVSC_DRV);
1212+
1213+ while (1)
1214+ {
1215+ current_dev = NULL;
1216+
1217+ // Get the device
1218+ driver_for_each_device(&drv_ctx->driver, NULL, (void*)&current_dev, storvsc_drv_exit_cb);
1219+
1220+ if (current_dev == NULL)
1221+ break;
1222+
1223+ // Initiate removal from the top-down
1224+ device_unregister(current_dev);
1225+ }
1226+
1227+ if (storvsc_drv_obj->Base.OnCleanup)
1228+ storvsc_drv_obj->Base.OnCleanup(&storvsc_drv_obj->Base);
1229+
1230+ vmbus_child_driver_unregister(drv_ctx);
1231+
1232+ DPRINT_EXIT(STORVSC_DRV);
1233+
1234+ return;
1235+}
1236+
1237+/*++
1238+
1239+Name: storvsc_probe()
1240+
1241+Desc: Add a new device for this driver
1242+
1243+--*/
1244+static int storvsc_probe(struct device *device)
1245+{
1246+ int ret=0;
1247+
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;
1251+
1252+ struct device_context *device_ctx = device_to_device_context(device);
1253+ DEVICE_OBJECT* device_obj = &device_ctx->device_obj;
1254+
1255+ struct Scsi_Host *host;
1256+ struct host_device_context *host_device_ctx;
1257+ STORVSC_DEVICE_INFO device_info;
1258+
1259+ DPRINT_ENTER(STORVSC_DRV);
1260+
1261+ if (!storvsc_drv_obj->Base.OnDeviceAdd)
1262+ return -1;
1263+
1264+ host = scsi_host_alloc(&scsi_driver, sizeof(struct host_device_context));
1265+ if (!host)
1266+ {
1267+ DPRINT_ERR(STORVSC_DRV, "unable to allocate scsi host object");
1268+ return -ENOMEM;
1269+ }
1270+
1271+ device->driver_data = host;
1272+
1273+ host_device_ctx = (struct host_device_context*)host->hostdata;
1274+ memset(host_device_ctx, 0, sizeof(struct host_device_context));
1275+
1276+ host_device_ctx->port = host->host_no;
1277+ host_device_ctx->device_ctx = device_ctx;
1278+
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);
1282+#else
1283+ INIT_WORK(&host_device_ctx->host_rescan_work, storvsc_host_rescan_callback, device_obj);
1284+#endif
1285+
1286+#if defined(KERNEL_2_6_27)
1287+ host_device_ctx->request_pool =
1288+ kmem_cache_create
1289+ (device_ctx->device.bus_id,
1290+ sizeof(struct storvsc_cmd_request) + storvsc_drv_obj->RequestExtSize,
1291+ 0,
1292+ SLAB_HWCACHE_ALIGN, NULL);
1293+#else
1294+ host_device_ctx->request_pool =
1295+ kmem_cache_create
1296+ (device_ctx->device.bus_id,
1297+ sizeof(struct storvsc_cmd_request) + storvsc_drv_obj->RequestExtSize,
1298+ 0,
1299+ SLAB_HWCACHE_ALIGN, NULL, NULL);
1300+#endif
1301+
1302+ if (!host_device_ctx->request_pool)
1303+ {
1304+ scsi_host_put(host);
1305+ DPRINT_EXIT(STORVSC_DRV);
1306+
1307+ return -ENOMEM;
1308+ }
1309+
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);
1313+ if (ret != 0)
1314+ {
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);
1319+
1320+ return -1;
1321+ }
1322+
1323+ //host_device_ctx->port = device_info.PortNumber;
1324+ host_device_ctx->path = device_info.PathId;
1325+ host_device_ctx->target = device_info.TargetId;
1326+
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
1330+
1331+ // Register the HBA and start the scsi bus scan
1332+ ret = scsi_add_host(host, device);
1333+ if (ret != 0)
1334+ {
1335+ DPRINT_ERR(STORVSC_DRV, "unable to add scsi host device");
1336+
1337+ storvsc_drv_obj->Base.OnDeviceRemove(device_obj);
1338+
1339+ kmem_cache_destroy(host_device_ctx->request_pool);
1340+ scsi_host_put(host);
1341+ DPRINT_EXIT(STORVSC_DRV);
1342+
1343+ return -1;
1344+ }
1345+
1346+ scsi_scan_host(host);
1347+
1348+ DPRINT_EXIT(STORVSC_DRV);
1349+
1350+ return ret;
1351+}
1352+
1353+
1354+/*++
1355+
1356+Name: storvsc_remove()
1357+
1358+Desc: Callback when our device is removed
1359+
1360+--*/
1361+static int storvsc_remove(struct device *device)
1362+{
1363+ int ret=0;
1364+
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;
1368+
1369+ struct device_context *device_ctx = device_to_device_context(device);
1370+ DEVICE_OBJECT* device_obj = &device_ctx->device_obj;
1371+
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;
1374+
1375+
1376+ DPRINT_ENTER(STORVSC_DRV);
1377+
1378+ if (!storvsc_drv_obj->Base.OnDeviceRemove)
1379+ {
1380+ DPRINT_EXIT(STORVSC_DRV);
1381+ return -1;
1382+ }
1383+
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);
1386+ if (ret != 0)
1387+ {
1388+ // TODO:
1389+ DPRINT_ERR(STORVSC, "unable to remove vsc device (ret %d)", ret);
1390+ }
1391+
1392+ if (host_device_ctx->request_pool)
1393+ {
1394+ kmem_cache_destroy(host_device_ctx->request_pool);
1395+ host_device_ctx->request_pool = NULL;
1396+ }
1397+
1398+ DPRINT_INFO(STORVSC, "removing host adapter (%p)...", host);
1399+ scsi_remove_host(host);
1400+
1401+ DPRINT_INFO(STORVSC, "releasing host adapter (%p)...", host);
1402+ scsi_host_put(host);
1403+
1404+ DPRINT_EXIT(STORVSC_DRV);
1405+
1406+ return ret;
1407+}
1408+
1409+/*++
1410+
1411+Name: storvsc_commmand_completion()
1412+
1413+Desc: Command completion processing
1414+
1415+--*/
1416+static void storvsc_commmand_completion(STORVSC_REQUEST* request)
1417+{
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)
1423+#else
1424+ struct scsi_sense_hdr sense_hdr;
1425+#endif
1426+
1427+ ASSERT(request == &cmd_request->request);
1428+ ASSERT((unsigned long)scmnd->host_scribble == (unsigned long)cmd_request);
1429+ ASSERT(scmnd);
1430+ ASSERT(scmnd->scsi_done);
1431+
1432+ DPRINT_ENTER(STORVSC_DRV);
1433+
1434+ if (cmd_request->bounce_sgl_count)// using bounce buffer
1435+ {
1436+ //printk("copy_from_bounce_buffer\n");
1437+
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));
1441+#else
1442+ copy_from_bounce_buffer(scmnd->request_buffer, cmd_request->bounce_sgl, scmnd->use_sg);
1443+#endif
1444+ destroy_bounce_buffer(cmd_request->bounce_sgl, cmd_request->bounce_sgl_count);
1445+ }
1446+
1447+ scmnd->result = request->Status;
1448+
1449+ if (scmnd->result)
1450+ {
1451+#if defined(KERNEL_2_6_5) || defined(KERNEL_2_6_9)
1452+ DPRINT_INFO(STORVSC_DRV, "scsi result nonzero - %d", scmnd->result);
1453+#else
1454+ if (scsi_normalize_sense(scmnd->sense_buffer, request->SenseBufferSize, &sense_hdr))
1455+ {
1456+ scsi_print_sense_hdr("storvsc", &sense_hdr);
1457+ }
1458+#endif
1459+ }
1460+
1461+ ASSERT(request->BytesXfer <= request->DataBuffer.Length);
1462+#ifdef KERNEL_2_6_27
1463+ scsi_set_resid(scmnd, request->DataBuffer.Length - request->BytesXfer);
1464+#else
1465+ scmnd->resid = request->DataBuffer.Length - request->BytesXfer;
1466+#endif
1467+
1468+ scsi_done_fn = scmnd->scsi_done;
1469+
1470+ scmnd->host_scribble = NULL;
1471+ scmnd->scsi_done = NULL;
1472+
1473+ // !!DO NOT MODIFY the scmnd after this call
1474+ scsi_done_fn(scmnd);
1475+
1476+ kmem_cache_free(host_device_ctx->request_pool, cmd_request);
1477+
1478+ DPRINT_EXIT(STORVSC_DRV);
1479+}
1480+
1481+static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
1482+{
1483+ int i=0;
1484+
1485+ // No need to check
1486+ if (sg_count < 2)
1487+ return -1;
1488+
1489+ // We have at least 2 sg entries
1490+ for ( i=0; i<sg_count; i++ )
1491+ {
1492+ if (i == 0) // make sure 1st one does not have hole
1493+ {
1494+ if (sgl[i].offset + sgl[i].length != PAGE_SIZE)
1495+ return i;
1496+ }
1497+ else if (i == sg_count - 1) // make sure last one does not have hole
1498+ {
1499+ if (sgl[i].offset != 0)
1500+ return i;
1501+ }
1502+ else // make sure no hole in the middle
1503+ {
1504+ if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0)
1505+ {
1506+ return i;
1507+ }
1508+ }
1509+ }
1510+ return -1;
1511+}
1512+
1513+static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count, unsigned int len)
1514+{
1515+ int i;
1516+ int num_pages=0;
1517+ struct scatterlist* bounce_sgl;
1518+ struct page *page_buf;
1519+
1520+ num_pages = ALIGN_UP(len, PAGE_SIZE) >> PAGE_SHIFT;
1521+
1522+ bounce_sgl = kzalloc(num_pages * sizeof(struct scatterlist), GFP_ATOMIC);
1523+ if (!bounce_sgl)
1524+ {
1525+ return NULL;
1526+ }
1527+
1528+ for(i=0; i<num_pages; i++)
1529+ {
1530+ page_buf = alloc_page(GFP_ATOMIC);
1531+ if (!page_buf)
1532+ {
1533+ goto cleanup;
1534+ }
1535+#ifdef KERNEL_2_6_27
1536+ sg_set_page(&bounce_sgl[i], page_buf, 0, 0);
1537+#else
1538+ bounce_sgl[i].page = page_buf;
1539+ bounce_sgl[i].offset = 0;
1540+ bounce_sgl[i].length = 0;
1541+#endif
1542+ }
1543+
1544+ return bounce_sgl;
1545+
1546+cleanup:
1547+ destroy_bounce_buffer(bounce_sgl, num_pages);
1548+ return NULL;
1549+}
1550+
1551+static void destroy_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
1552+{
1553+ int i;
1554+ struct page *page_buf;
1555+
1556+ for (i=0; i<sg_count; i++)
1557+ {
1558+#ifdef KERNEL_2_6_27
1559+ if ((page_buf = sg_page((&sgl[i]))) != NULL)
1560+#else
1561+ if ((page_buf = sgl[i].page) != NULL)
1562+#endif
1563+
1564+ {
1565+ __free_page(page_buf);
1566+ }
1567+ }
1568+
1569+ kfree(sgl);
1570+}
1571+
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)
1574+{
1575+ int i=0,j=0;
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;
1582+
1583+ local_irq_save(flags);
1584+
1585+ for (i=0; i<orig_sgl_count; i++)
1586+ {
1587+#ifdef KERNEL_2_6_27
1588+ src_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])), KM_IRQ0) + orig_sgl[i].offset;
1589+#else
1590+ src_addr = (unsigned long)kmap_atomic(orig_sgl[i].page, KM_IRQ0) + orig_sgl[i].offset;
1591+#endif
1592+ src = src_addr;
1593+ srclen = orig_sgl[i].length;
1594+
1595+ //if (PageHighMem(orig_sgl[i].page))
1596+ // printk("HighMem page detected - addr %p", (void*)src);
1597+
1598+ ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE);
1599+
1600+ if (j == 0)
1601+ {
1602+#ifdef KERNEL_2_6_27
1603+ bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
1604+#else
1605+ bounce_addr = (unsigned long)kmap_atomic(bounce_sgl[j].page, KM_IRQ0);
1606+#endif
1607+ }
1608+
1609+ while (srclen)
1610+ {
1611+ // assume bounce offset always == 0
1612+ dest = bounce_addr + bounce_sgl[j].length;
1613+ destlen = PAGE_SIZE - bounce_sgl[j].length;
1614+
1615+ copylen = MIN(srclen, destlen);
1616+ memcpy((void*)dest, (void*)src, copylen);
1617+
1618+ total_copied += copylen;
1619+ bounce_sgl[j].length += copylen;
1620+ srclen -= copylen;
1621+ src += copylen;
1622+
1623+ if (bounce_sgl[j].length == PAGE_SIZE) // full..move to next entry
1624+ {
1625+ kunmap_atomic((void*)bounce_addr, KM_IRQ0);
1626+ j++;
1627+
1628+ // if we need to use another bounce buffer
1629+ if (srclen || i != orig_sgl_count -1)
1630+ {
1631+#ifdef KERNEL_2_6_27
1632+ bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
1633+#else
1634+ bounce_addr = (unsigned long)kmap_atomic(bounce_sgl[j].page, KM_IRQ0);
1635+#endif
1636+ }
1637+ }
1638+ else if (srclen == 0 && i == orig_sgl_count -1) // // unmap the last bounce that is < PAGE_SIZE
1639+ {
1640+ kunmap_atomic((void*)bounce_addr, KM_IRQ0);
1641+ }
1642+ }
1643+
1644+ kunmap_atomic((void*)(src_addr - orig_sgl[i].offset), KM_IRQ0);
1645+ }
1646+
1647+ local_irq_restore(flags);
1648+
1649+ return total_copied;
1650+}
1651+
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)
1654+{
1655+ int i=0,j=0;
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;
1662+
1663+ local_irq_save(flags);
1664+
1665+ for (i=0; i<orig_sgl_count; i++)
1666+ {
1667+#ifdef KERNEL_2_6_27
1668+ dest_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])), KM_IRQ0) + orig_sgl[i].offset;
1669+#else
1670+ dest_addr = (unsigned long)kmap_atomic(orig_sgl[i].page, KM_IRQ0) + orig_sgl[i].offset;
1671+#endif
1672+ dest = dest_addr;
1673+ destlen = orig_sgl[i].length;
1674+ ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE);
1675+
1676+ if (j == 0)
1677+ {
1678+#ifdef KERNEL_2_6_27
1679+ bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
1680+#else
1681+ bounce_addr = (unsigned long)kmap_atomic(bounce_sgl[j].page, KM_IRQ0);
1682+#endif
1683+ }
1684+
1685+ while (destlen)
1686+ {
1687+ src = bounce_addr + bounce_sgl[j].offset;
1688+ srclen = bounce_sgl[j].length - bounce_sgl[j].offset;
1689+
1690+ copylen = MIN(srclen, destlen);
1691+ memcpy((void*)dest, (void*)src, copylen);
1692+
1693+ total_copied += copylen;
1694+ bounce_sgl[j].offset += copylen;
1695+ destlen -= copylen;
1696+ dest += copylen;
1697+
1698+ if (bounce_sgl[j].offset == bounce_sgl[j].length) // full
1699+ {
1700+ kunmap_atomic((void*)bounce_addr, KM_IRQ0);
1701+ j++;
1702+
1703+ // if we need to use another bounce buffer
1704+ if (destlen || i != orig_sgl_count -1)
1705+ {
1706+#ifdef KERNEL_2_6_27
1707+ bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
1708+#else
1709+ bounce_addr = (unsigned long)kmap_atomic(bounce_sgl[j].page, KM_IRQ0);
1710+#endif
1711+ }
1712+ }
1713+ else if (destlen == 0 && i == orig_sgl_count -1) // unmap the last bounce that is < PAGE_SIZE
1714+ {
1715+ kunmap_atomic((void*)bounce_addr, KM_IRQ0);
1716+ }
1717+ }
1718+
1719+ kunmap_atomic((void*)(dest_addr - orig_sgl[i].offset), KM_IRQ0);
1720+ }
1721+
1722+ local_irq_restore(flags);
1723+
1724+ return total_copied;
1725+}
1726+
1727+
1728+/*++
1729+
1730+Name: storvsc_queuecommand()
1731+
1732+Desc: Initiate command processing
1733+
1734+--*/
1735+static int storvsc_queuecommand(struct scsi_cmnd *scmnd, void (*done)(struct scsi_cmnd *))
1736+{
1737+ int ret=0;
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;
1743+
1744+ STORVSC_REQUEST *request;
1745+ struct storvsc_cmd_request *cmd_request;
1746+ unsigned int request_size=0;
1747+ int i;
1748+ struct scatterlist *sgl;
1749+
1750+ DPRINT_ENTER(STORVSC_DRV);
1751+
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",
1754+ scmnd,
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);
1761+#else
1762+ DPRINT_DBG(STORVSC_DRV, "scmnd %p dir %d, use_sg %d buf %p len %d queue depth %d tagged %d",
1763+ scmnd,
1764+ scmnd->sc_data_direction,
1765+ scmnd->use_sg,
1766+ scmnd->request_buffer,
1767+ scmnd->request_bufflen,
1768+ scmnd->device->queue_depth,
1769+ scmnd->device->tagged_supported);
1770+#endif
1771+
1772+ // If retrying, no need to prep the cmd
1773+ if (scmnd->host_scribble)
1774+ {
1775+ ASSERT(scmnd->scsi_done != NULL);
1776+
1777+ cmd_request = (struct storvsc_cmd_request* )scmnd->host_scribble;
1778+ DPRINT_INFO(STORVSC_DRV, "retrying scmnd %p cmd_request %p", scmnd, cmd_request);
1779+
1780+ goto retry_request;
1781+ }
1782+
1783+ ASSERT(scmnd->scsi_done == NULL);
1784+ ASSERT(scmnd->host_scribble == NULL);
1785+
1786+ scmnd->scsi_done = done;
1787+
1788+ request_size = sizeof(struct storvsc_cmd_request);
1789+
1790+ cmd_request = kmem_cache_alloc(host_device_ctx->request_pool, GFP_ATOMIC);
1791+ if (!cmd_request)
1792+ {
1793+ DPRINT_ERR(STORVSC_DRV, "scmnd (%p) - unable to allocate storvsc_cmd_request...marking queue busy", scmnd);
1794+
1795+ scmnd->scsi_done = NULL;
1796+ return SCSI_MLQUEUE_DEVICE_BUSY;
1797+ }
1798+
1799+ // Setup the cmd request
1800+ cmd_request->bounce_sgl_count = 0;
1801+ cmd_request->bounce_sgl = NULL;
1802+ cmd_request->cmd = scmnd;
1803+
1804+ scmnd->host_scribble = (unsigned char*)cmd_request;
1805+
1806+ request = &cmd_request->request;
1807+
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);
1810+
1811+ // Build the SRB
1812+ switch(scmnd->sc_data_direction)
1813+ {
1814+ case DMA_TO_DEVICE:
1815+ request->Type = WRITE_TYPE;
1816+ break;
1817+ case DMA_FROM_DEVICE:
1818+ request->Type = READ_TYPE;
1819+ break;
1820+ default:
1821+ request->Type = UNKNOWN_TYPE;
1822+ break;
1823+ }
1824+
1825+ request->OnIOCompletion = storvsc_commmand_completion;
1826+ request->Context = cmd_request;//scmnd;
1827+
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;
1833+
1834+ ASSERT(scmnd->cmd_len <= 16);
1835+ request->CdbLen = scmnd->cmd_len;
1836+ request->Cdb = scmnd->cmnd;
1837+
1838+ request->SenseBuffer = scmnd->sense_buffer;
1839+ request->SenseBufferSize = SCSI_SENSE_BUFFERSIZE;
1840+
1841+
1842+#ifdef KERNEL_2_6_27
1843+ request->DataBuffer.Length = scsi_bufflen(scmnd);
1844+ if (scsi_sg_count(scmnd))
1845+#else
1846+ request->DataBuffer.Length = scmnd->request_bufflen;
1847+ if (scmnd->use_sg)
1848+#endif
1849+ {
1850+#ifdef KERNEL_2_6_27
1851+ sgl = (struct scatterlist*)scsi_sglist(scmnd);
1852+#else
1853+ sgl = (struct scatterlist*)(scmnd->request_buffer);
1854+#endif
1855+
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)
1859+#else
1860+ if (do_bounce_buffer(sgl, scmnd->use_sg) != -1)
1861+#endif
1862+ {
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));
1866+#else
1867+ cmd_request->bounce_sgl = create_bounce_buffer(
1868+ sgl,
1869+ scmnd->use_sg, scmnd->request_bufflen);
1870+#endif
1871+ if (!cmd_request->bounce_sgl)
1872+ {
1873+ DPRINT_ERR(STORVSC_DRV, "unable to create bounce buffer for this scmnd %p", scmnd);
1874+
1875+ scmnd->scsi_done = NULL;
1876+ scmnd->host_scribble = NULL;
1877+ kmem_cache_free(host_device_ctx->request_pool, cmd_request);
1878+
1879+ return SCSI_MLQUEUE_HOST_BUSY;
1880+ }
1881+
1882+#ifdef KERNEL_2_6_27
1883+ cmd_request->bounce_sgl_count = ALIGN_UP(scsi_bufflen(scmnd), PAGE_SIZE) >> PAGE_SHIFT;
1884+#else
1885+ cmd_request->bounce_sgl_count = ALIGN_UP(scmnd->request_bufflen, PAGE_SIZE) >> PAGE_SHIFT;
1886+#endif
1887+
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));
1893+#else
1894+ copy_to_bounce_buffer(sgl, cmd_request->bounce_sgl, scmnd->use_sg);
1895+#endif
1896+
1897+ sgl = cmd_request->bounce_sgl;
1898+ }
1899+
1900+ request->DataBuffer.Offset = sgl[0].offset;
1901+
1902+#ifdef KERNEL_2_6_27
1903+ for (i = 0; i < scsi_sg_count(scmnd); i++ )
1904+#else
1905+ for (i = 0; i < scmnd->use_sg; i++ )
1906+#endif
1907+ {
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])));
1911+#else
1912+ request->DataBuffer.PfnArray[i] = page_to_pfn(sgl[i].page);
1913+#endif
1914+ }
1915+ }
1916+
1917+#ifdef KERNEL_2_6_27
1918+ else if (scsi_sglist(scmnd))
1919+ {
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;
1923+ }
1924+ else
1925+ {
1926+ ASSERT(scsi_bufflen(scmnd) == 0);
1927+ }
1928+#else
1929+ else if (scmnd->request_buffer)
1930+ {
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;
1934+ }
1935+ else
1936+ {
1937+ ASSERT(scmnd->request_bufflen == 0);
1938+ }
1939+#endif
1940+
1941+retry_request:
1942+
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
1946+ {
1947+ DPRINT_ERR(STORVSC_DRV, "scmnd (%p) - queue FULL...marking queue busy", scmnd);
1948+
1949+ if (cmd_request->bounce_sgl_count)
1950+ {
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));
1954+#else
1955+ copy_from_bounce_buffer(
1956+ scmnd->request_buffer,
1957+ cmd_request->bounce_sgl,
1958+ scmnd->use_sg);
1959+#endif
1960+ destroy_bounce_buffer(cmd_request->bounce_sgl, cmd_request->bounce_sgl_count);
1961+ }
1962+
1963+ kmem_cache_free(host_device_ctx->request_pool, cmd_request);
1964+
1965+ scmnd->scsi_done = NULL;
1966+ scmnd->host_scribble = NULL;
1967+
1968+ ret = SCSI_MLQUEUE_DEVICE_BUSY;
1969+ }
1970+
1971+ DPRINT_EXIT(STORVSC_DRV);
1972+
1973+ return ret;
1974+}
1975+
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)
1978+{
1979+ return bvec->bv_len; //checking done by caller.
1980+}
1981+#else
1982+static int storvsc_merge_bvec(struct request_queue *q, struct bio *bio, struct bio_vec *bvec)
1983+{
1984+ // Check if we are adding a new bvec
1985+ if (bio->bi_vcnt > 0)
1986+ {
1987+ //printk("storvsc_merge_bvec() - cnt %u offset %u len %u\n", bio->bi_vcnt, bvec->bv_offset, bvec->bv_len);
1988+
1989+ struct bio_vec *prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
1990+ if (bvec == prev)
1991+ return bvec->bv_len; // success
1992+
1993+ // Adding new bvec. Make sure the prev one is a complete page
1994+ if (prev->bv_len == PAGE_SIZE && prev->bv_offset == 0)
1995+ {
1996+ return bvec->bv_len; // success
1997+ }
1998+ else
1999+ {
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))
2004+ {
2005+ return bvec->bv_len; // success
2006+ }
2007+ else
2008+ {
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
2011+ }
2012+ }
2013+ }
2014+
2015+ return bvec->bv_len; // success
2016+
2017+}
2018+
2019+#endif
2020+
2021+/*++
2022+
2023+Name: storvsc_device_configure()
2024+
2025+Desc: Configure the specified scsi device
2026+
2027+--*/
2028+static int storvsc_device_alloc(struct scsi_device *sdevice)
2029+{
2030+#ifdef KERNEL_2_6_5
2031+#else
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;
2035+#endif
2036+ return 0;
2037+}
2038+
2039+static int storvsc_device_configure(struct scsi_device *sdevice)
2040+{
2041+ DPRINT_INFO(STORVSC_DRV, "sdev (%p) - curr queue depth %d", sdevice, sdevice->queue_depth);
2042+
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);
2045+
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);
2048+
2049+ DPRINT_INFO(STORVSC_DRV, "sdev (%p) - adding merge bio vec routine", sdevice);
2050+ blk_queue_merge_bvec(sdevice->request_queue, storvsc_merge_bvec);
2051+
2052+ blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
2053+ //sdevice->timeout = (2000 * HZ);//(75 * HZ);
2054+
2055+ return 0;
2056+}
2057+
2058+/*++
2059+
2060+Name: storvsc_host_reset_handler()
2061+
2062+Desc: Reset the scsi HBA
2063+
2064+--*/
2065+static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
2066+{
2067+ int ret=SUCCESS;
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;
2072+
2073+ STORVSC_DRIVER_OBJECT *storvsc_drv_obj = &storvsc_drv_ctx->drv_obj;
2074+
2075+ DPRINT_ENTER(STORVSC_DRV);
2076+
2077+ DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host resetting...", scmnd->device, &device_ctx->device_obj);
2078+
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);
2082+ if (ret != 0)
2083+ {
2084+ DPRINT_EXIT(STORVSC_DRV);
2085+ return ret;
2086+ }
2087+
2088+ DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host reseted", scmnd->device, &device_ctx->device_obj);
2089+
2090+ DPRINT_EXIT(STORVSC_DRV);
2091+
2092+ return ret;
2093+}
2094+
2095+/*++
2096+
2097+Name: storvsc_host_rescan
2098+
2099+Desc: Rescan the scsi HBA
2100+
2101+--*/
2102+#if defined(KERNEL_2_6_5) || defined(KERNEL_2_6_9)
2103+#else
2104+
2105+#ifdef KERNEL_2_6_27
2106+static void storvsc_host_rescan_callback(struct work_struct *work)
2107+{
2108+ DEVICE_OBJECT* device_obj =
2109+ &((struct host_device_context*)work)->device_ctx->device_obj;
2110+#else
2111+static void storvsc_host_rescan_callback(void* context)
2112+{
2113+
2114+ DEVICE_OBJECT* device_obj = (DEVICE_OBJECT*)context;
2115+#endif
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;
2123+ unsigned int i;
2124+ unsigned int lun_count=0;
2125+ unsigned int *lun_list;
2126+
2127+ DPRINT_ENTER(STORVSC_DRV);
2128+
2129+ host_device_ctx = (struct host_device_context*)host->hostdata;
2130+ lun_list = kzalloc(sizeof(unsigned int)*STORVSC_MAX_LUNS_PER_TARGET, GFP_ATOMIC);
2131+ if (!lun_list)
2132+ {
2133+ DPRINT_ERR(STORVSC_DRV, "unable to allocate lun list");
2134+ return;
2135+ }
2136+
2137+ sdevs_remove_list = kzalloc(sizeof(void*)*STORVSC_MAX_LUNS_PER_TARGET, GFP_ATOMIC);
2138+ if (!sdevs_remove_list)
2139+ {
2140+ kfree(lun_list);
2141+ DPRINT_ERR(STORVSC_DRV, "unable to allocate lun remove list");
2142+ return;
2143+ }
2144+
2145+ DPRINT_INFO(STORVSC_DRV, "rescanning host for new scsi devices...", device_obj, host_device_ctx->target, host_device_ctx->path);
2146+
2147+ // Rescan for new device
2148+ scsi_scan_target(&host->shost_gendev, host_device_ctx->path, host_device_ctx->target, SCAN_WILD_CARD, 1);
2149+
2150+ DPRINT_INFO(STORVSC_DRV, "rescanning host for removed scsi device...");
2151+
2152+ // Use the 1st device to send the report luns cmd
2153+ shost_for_each_device(sdev, host)
2154+ {
2155+ lun_count=STORVSC_MAX_LUNS_PER_TARGET;
2156+ storvsc_report_luns(sdev, lun_list, &lun_count);
2157+
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);
2160+
2161+ scsi_device_put(sdev);
2162+ break;
2163+ }
2164+
2165+ for (i=0; i<lun_count; i++)
2166+ {
2167+ DPRINT_INFO(STORVSC_DRV, "%d) lun %u", i, lun_list[i]);
2168+ }
2169+
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)
2174+ {
2175+ // See if this device is still here
2176+ found = 0;
2177+ for (i=0; i<lun_count; i++)
2178+ {
2179+ if (sdev->lun == lun_list[i])
2180+ {
2181+ found = 1;
2182+ break;
2183+ }
2184+ }
2185+ if (!found)
2186+ {
2187+ DPRINT_INFO(STORVSC_DRV, "lun (%u) does not exists", sdev->lun);
2188+ sdevs_remove_list[sdevs_count++] = sdev;
2189+ }
2190+ }
2191+
2192+ // Now remove the devices
2193+ for (i=0; i< sdevs_count; i++)
2194+ {
2195+ DPRINT_INFO(STORVSC_DRV, "removing scsi device (%p) lun (%u)...",
2196+ sdevs_remove_list[i], sdevs_remove_list[i]->lun);
2197+
2198+ // make sure it is not removed from underneath us
2199+ if (!scsi_device_get(sdevs_remove_list[i]))
2200+ {
2201+ scsi_remove_device(sdevs_remove_list[i]);
2202+ scsi_device_put(sdevs_remove_list[i]);
2203+ }
2204+ }
2205+
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);
2207+
2208+ kfree(lun_list);
2209+ kfree(sdevs_remove_list);
2210+
2211+ DPRINT_EXIT(STORVSC_DRV);
2212+}
2213+
2214+static int storvsc_report_luns(struct scsi_device *sdev, unsigned int luns[], unsigned int *lun_count)
2215+{
2216+ int i,j;
2217+ unsigned int lun=0;
2218+ unsigned int num_luns;
2219+ int result;
2220+ unsigned char *data;
2221+#if defined(KERNEL_2_6_5) || defined(KERNEL_2_6_9)
2222+#else
2223+ struct scsi_sense_hdr sshdr;
2224+#endif
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;
2229+
2230+ *lun_count = 0;
2231+
2232+ report_luns = kzalloc(report_len, GFP_ATOMIC);
2233+ if (!report_luns)
2234+ {
2235+ return -ENOMEM;
2236+ }
2237+
2238+ cmd[0] = REPORT_LUNS;
2239+
2240+ // cmd length
2241+ *(unsigned int*)&cmd[6] = cpu_to_be32(report_len);
2242+
2243+ result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, (unsigned char*)report_luns, report_len, &sshdr, 30*HZ, 3);
2244+ if (result != 0)
2245+ {
2246+ kfree(report_luns);
2247+ return -EBUSY;
2248+ }
2249+
2250+ // get the length from the first four bytes
2251+ report_len = be32_to_cpu(*(unsigned int*)&report_luns[0]);
2252+
2253+ num_luns = (report_len / sizeof(unsigned long long));
2254+ if (num_luns > in_lun_count)
2255+ {
2256+ kfree(report_luns);
2257+ return -EINVAL;
2258+ }
2259+
2260+ *lun_count = num_luns;
2261+
2262+ DPRINT_DBG(STORVSC_DRV, "report luns on scsi device (%p) found %u luns ", sdev, num_luns);
2263+
2264+ // lun id starts at 1
2265+ for (i=1; i< num_luns+1; i++)
2266+ {
2267+ lun = 0;
2268+ data = (unsigned char*)&report_luns[i];
2269+ for (j = 0; j < sizeof(lun); j += 2)
2270+ {
2271+ lun = lun | (((data[j] << 8) | data[j + 1]) << (j * 8));
2272+ }
2273+
2274+ luns[i-1] = lun;
2275+ }
2276+
2277+ kfree(report_luns);
2278+ return 0;
2279+}
2280+#endif // KERNEL_2_6_9
2281+
2282+static void storvsc_host_rescan(DEVICE_OBJECT* device_obj)
2283+{
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;
2287+
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");
2291+#else
2292+
2293+ host_device_ctx = (struct host_device_context*)host->hostdata;
2294+
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);
2296+
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);
2302+}
2303+
2304+static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev, sector_t capacity, int *info)
2305+{
2306+ sector_t total_sectors = capacity;
2307+ sector_t cylinder_times_heads=0;
2308+ sector_t temp=0;
2309+
2310+ int sectors_per_track=0;
2311+ int heads=0;
2312+ int cylinders=0;
2313+ int rem=0;
2314+
2315+ if (total_sectors > (65535 * 16 * 255)) {
2316+ total_sectors = (65535 * 16 * 255);
2317+ }
2318+
2319+ if (total_sectors >= (65535 * 16 * 63)) {
2320+ sectors_per_track = 255;
2321+ heads = 16;
2322+
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
2325+ }
2326+ else
2327+ {
2328+ sectors_per_track = 17;
2329+
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
2332+
2333+ temp = cylinder_times_heads + 1023;
2334+ rem = sector_div(temp, 1024); // sector_div stores the quotient in temp
2335+
2336+ heads = temp;
2337+
2338+ if (heads < 4) {
2339+ heads = 4;
2340+ }
2341+
2342+ if (cylinder_times_heads >= (heads * 1024) || (heads > 16)) {
2343+ sectors_per_track = 31;
2344+ heads = 16;
2345+
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
2348+ }
2349+
2350+ if (cylinder_times_heads >= (heads * 1024)) {
2351+ sectors_per_track = 63;
2352+ heads = 16;
2353+
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
2356+ }
2357+ }
2358+
2359+ temp = cylinder_times_heads;
2360+ rem = sector_div(temp, heads); // sector_div stores the quotient in temp
2361+ cylinders = temp;
2362+
2363+ info[0] = heads;
2364+ info[1] = sectors_per_track;
2365+ info[2] = cylinders;
2366+
2367+ DPRINT_INFO(STORVSC_DRV, "CHS (%d, %d, %d)", cylinders, heads, sectors_per_track);
2368+
2369+ return 0;
2370+}
2371+
2372+MODULE_LICENSE("GPL");
2373+
2374+static int __init storvsc_init(void)
2375+{
2376+ int ret;
2377+
2378+ DPRINT_ENTER(STORVSC_DRV);
2379+
2380+ DPRINT_INFO(STORVSC_DRV, "Storvsc initializing....");
2381+
2382+ ret = storvsc_drv_init(StorVscInitialize);
2383+
2384+ DPRINT_EXIT(STORVSC_DRV);
2385+
2386+ return ret;
2387+}
2388+
2389+static void __exit storvsc_exit(void)
2390+{
2391+ DPRINT_ENTER(STORVSC_DRV);
2392+
2393+ storvsc_drv_exit();
2394+
2395+ DPRINT_ENTER(STORVSC_DRV);
2396+}
2397+
2398+module_param(storvsc_ringbuffer_size, int, S_IRUGO);
2399+
2400+module_init(storvsc_init);
2401+module_exit(storvsc_exit);
2402+
2403+// eof