Staging: hv: properly fix the printk() warnings
[safe/jmp/linux-2.6] / drivers / staging / hv / StorVsc.c
1 /*
2  *
3  * Copyright (c) 2009, Microsoft Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16  * Place - Suite 330, Boston, MA 02111-1307 USA.
17  *
18  * Authors:
19  *   Haiyang Zhang <haiyangz@microsoft.com>
20  *   Hank Janssen  <hjanssen@microsoft.com>
21  *
22  */
23
24 #include <linux/kernel.h>
25 #include <linux/string.h>
26 #include <linux/mm.h>
27 #include <linux/delay.h>
28 #include "include/logging.h"
29
30 #include "include/StorVscApi.h"
31 #include "include/VmbusPacketFormat.h"
32 #include "include/vstorage.h"
33
34
35
36 /* #defines */
37
38
39
40 /* Data types */
41
42
43 typedef struct _STORVSC_REQUEST_EXTENSION {
44         /* LIST_ENTRY                                           ListEntry; */
45
46         STORVSC_REQUEST                                 *Request;
47         struct hv_device *Device;
48
49         /* Synchronize the request/response if needed */
50         struct osd_waitevent *WaitEvent;
51
52         VSTOR_PACKET                                    VStorPacket;
53 } STORVSC_REQUEST_EXTENSION;
54
55
56 /* A storvsc device is a device object that contains a vmbus channel */
57 typedef struct _STORVSC_DEVICE{
58         struct hv_device *Device;
59
60         int                                                     RefCount; /* 0 indicates the device is being destroyed */
61
62         int                                                     NumOutstandingRequests;
63
64         /*
65          * Each unique Port/Path/Target represents 1 channel ie scsi
66          * controller. In reality, the pathid, targetid is always 0
67          * and the port is set by us
68          */
69         unsigned int                                            PortNumber;
70     unsigned char                                               PathId;
71     unsigned char                                               TargetId;
72
73         /* LIST_ENTRY                                   OutstandingRequestList; */
74         /* HANDLE                                               OutstandingRequestLock; */
75
76         /* Used for vsc/vsp channel reset process */
77         STORVSC_REQUEST_EXTENSION       InitRequest;
78
79         STORVSC_REQUEST_EXTENSION       ResetRequest;
80
81 } STORVSC_DEVICE;
82
83
84
85 /* Globals */
86
87 static const char* gDriverName="storvsc";
88
89 /* {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f} */
90 static const GUID gStorVscDeviceType={
91         .Data = {0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d, 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f}
92 };
93
94
95 /* Internal routines */
96
97 static int
98 StorVscOnDeviceAdd(
99         struct hv_device *Device,
100         void                    *AdditionalInfo
101         );
102
103 static int
104 StorVscOnDeviceRemove(
105         struct hv_device *Device
106         );
107
108 static int
109 StorVscOnIORequest(
110         struct hv_device *Device,
111         STORVSC_REQUEST *Request
112         );
113
114 static int
115 StorVscOnHostReset(
116         struct hv_device *Device
117         );
118
119 static void
120 StorVscOnCleanup(
121         DRIVER_OBJECT   *Device
122         );
123
124 static void
125 StorVscOnChannelCallback(
126         void * Context
127         );
128
129 static void
130 StorVscOnIOCompletion(
131         struct hv_device *Device,
132         VSTOR_PACKET    *VStorPacket,
133         STORVSC_REQUEST_EXTENSION *RequestExt
134         );
135
136 static void
137 StorVscOnReceive(
138         struct hv_device *Device,
139         VSTOR_PACKET    *VStorPacket,
140         STORVSC_REQUEST_EXTENSION *RequestExt
141         );
142
143 static int
144 StorVscConnectToVsp(
145         struct hv_device *Device
146         );
147
148 static inline STORVSC_DEVICE* AllocStorDevice(struct hv_device *Device)
149 {
150         STORVSC_DEVICE *storDevice;
151
152         storDevice = kzalloc(sizeof(STORVSC_DEVICE), GFP_KERNEL);
153         if (!storDevice)
154                 return NULL;
155
156         /* Set to 2 to allow both inbound and outbound traffics */
157         /* (ie GetStorDevice() and MustGetStorDevice()) to proceed. */
158         InterlockedCompareExchange(&storDevice->RefCount, 2, 0);
159
160         storDevice->Device = Device;
161         Device->Extension = storDevice;
162
163         return storDevice;
164 }
165
166 static inline void FreeStorDevice(STORVSC_DEVICE *Device)
167 {
168         ASSERT(Device->RefCount == 0);
169         kfree(Device);
170 }
171
172 /* Get the stordevice object iff exists and its refcount > 1 */
173 static inline STORVSC_DEVICE* GetStorDevice(struct hv_device *Device)
174 {
175         STORVSC_DEVICE *storDevice;
176
177         storDevice = (STORVSC_DEVICE*)Device->Extension;
178         if (storDevice && storDevice->RefCount > 1)
179         {
180                 InterlockedIncrement(&storDevice->RefCount);
181         }
182         else
183         {
184                 storDevice = NULL;
185         }
186
187         return storDevice;
188 }
189
190 /* Get the stordevice object iff exists and its refcount > 0 */
191 static inline STORVSC_DEVICE* MustGetStorDevice(struct hv_device *Device)
192 {
193         STORVSC_DEVICE *storDevice;
194
195         storDevice = (STORVSC_DEVICE*)Device->Extension;
196         if (storDevice && storDevice->RefCount)
197         {
198                 InterlockedIncrement(&storDevice->RefCount);
199         }
200         else
201         {
202                 storDevice = NULL;
203         }
204
205         return storDevice;
206 }
207
208 static inline void PutStorDevice(struct hv_device *Device)
209 {
210         STORVSC_DEVICE *storDevice;
211
212         storDevice = (STORVSC_DEVICE*)Device->Extension;
213         ASSERT(storDevice);
214
215         InterlockedDecrement(&storDevice->RefCount);
216         ASSERT(storDevice->RefCount);
217 }
218
219 /* Drop ref count to 1 to effectively disable GetStorDevice() */
220 static inline STORVSC_DEVICE* ReleaseStorDevice(struct hv_device *Device)
221 {
222         STORVSC_DEVICE *storDevice;
223
224         storDevice = (STORVSC_DEVICE*)Device->Extension;
225         ASSERT(storDevice);
226
227         /* Busy wait until the ref drop to 2, then set it to 1 */
228         while (InterlockedCompareExchange(&storDevice->RefCount, 1, 2) != 2)
229         {
230                 udelay(100);
231         }
232
233         return storDevice;
234 }
235
236 /* Drop ref count to 0. No one can use StorDevice object. */
237 static inline STORVSC_DEVICE* FinalReleaseStorDevice(struct hv_device *Device)
238 {
239         STORVSC_DEVICE *storDevice;
240
241         storDevice = (STORVSC_DEVICE*)Device->Extension;
242         ASSERT(storDevice);
243
244         /* Busy wait until the ref drop to 1, then set it to 0 */
245         while (InterlockedCompareExchange(&storDevice->RefCount, 0, 1) != 1)
246         {
247                 udelay(100);
248         }
249
250         Device->Extension = NULL;
251         return storDevice;
252 }
253
254 /*++;
255
256
257 Name:
258         StorVscInitialize()
259
260 Description:
261         Main entry point
262
263 --*/
264 int
265 StorVscInitialize(
266         DRIVER_OBJECT *Driver
267         )
268 {
269         STORVSC_DRIVER_OBJECT* storDriver = (STORVSC_DRIVER_OBJECT*)Driver;
270         int ret=0;
271
272         DPRINT_ENTER(STORVSC);
273
274         DPRINT_DBG(STORVSC, "sizeof(STORVSC_REQUEST)=%zd sizeof(STORVSC_REQUEST_EXTENSION)=%zd sizeof(VSTOR_PACKET)=%zd, sizeof(VMSCSI_REQUEST)=%zd",
275                 sizeof(STORVSC_REQUEST), sizeof(STORVSC_REQUEST_EXTENSION), sizeof(VSTOR_PACKET), sizeof(VMSCSI_REQUEST));
276
277         /* Make sure we are at least 2 pages since 1 page is used for control */
278         ASSERT(storDriver->RingBufferSize >= (PAGE_SIZE << 1));
279
280         Driver->name = gDriverName;
281         memcpy(&Driver->deviceType, &gStorVscDeviceType, sizeof(GUID));
282
283         storDriver->RequestExtSize                      = sizeof(STORVSC_REQUEST_EXTENSION);
284
285         /*
286          * Divide the ring buffer data size (which is 1 page less
287          * than the ring buffer size since that page is reserved for
288          * the ring buffer indices) by the max request size (which is
289          * VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER + VSTOR_PACKET + u64)
290          */
291         storDriver->MaxOutstandingRequestsPerChannel =
292                 ((storDriver->RingBufferSize - PAGE_SIZE) / ALIGN_UP(MAX_MULTIPAGE_BUFFER_PACKET + sizeof(VSTOR_PACKET) + sizeof(u64),sizeof(u64)));
293
294         DPRINT_INFO(STORVSC, "max io %u, currently %u\n", storDriver->MaxOutstandingRequestsPerChannel, STORVSC_MAX_IO_REQUESTS);
295
296         /* Setup the dispatch table */
297         storDriver->Base.OnDeviceAdd                    = StorVscOnDeviceAdd;
298         storDriver->Base.OnDeviceRemove         = StorVscOnDeviceRemove;
299         storDriver->Base.OnCleanup                      = StorVscOnCleanup;
300
301         storDriver->OnIORequest                         = StorVscOnIORequest;
302         storDriver->OnHostReset                         = StorVscOnHostReset;
303
304         DPRINT_EXIT(STORVSC);
305
306         return ret;
307 }
308
309 /*++
310
311 Name:
312         StorVscOnDeviceAdd()
313
314 Description:
315         Callback when the device belonging to this driver is added
316
317 --*/
318 int
319 StorVscOnDeviceAdd(
320         struct hv_device *Device,
321         void                    *AdditionalInfo
322         )
323 {
324         int ret=0;
325         STORVSC_DEVICE *storDevice;
326         /* VMSTORAGE_CHANNEL_PROPERTIES *props; */
327         STORVSC_DEVICE_INFO *deviceInfo = (STORVSC_DEVICE_INFO*)AdditionalInfo;
328
329         DPRINT_ENTER(STORVSC);
330
331         storDevice = AllocStorDevice(Device);
332         if (!storDevice)
333         {
334                 ret = -1;
335                 goto Cleanup;
336         }
337
338         /* Save the channel properties to our storvsc channel */
339         /* props = (VMSTORAGE_CHANNEL_PROPERTIES*) channel->offerMsg.Offer.u.Standard.UserDefined; */
340
341         /* FIXME: */
342         /*
343          * If we support more than 1 scsi channel, we need to set the
344          * port number here to the scsi channel but how do we get the
345          * scsi channel prior to the bus scan
346          */
347
348         /* storChannel->PortNumber = 0;
349         storChannel->PathId = props->PathId;
350         storChannel->TargetId = props->TargetId; */
351
352         storDevice->PortNumber = deviceInfo->PortNumber;
353         /* Send it back up */
354         ret = StorVscConnectToVsp(Device);
355
356         /* deviceInfo->PortNumber = storDevice->PortNumber; */
357         deviceInfo->PathId = storDevice->PathId;
358         deviceInfo->TargetId = storDevice->TargetId;
359
360         DPRINT_DBG(STORVSC, "assigned port %u, path %u target %u\n", storDevice->PortNumber, storDevice->PathId, storDevice->TargetId);
361
362 Cleanup:
363         DPRINT_EXIT(STORVSC);
364
365         return ret;
366 }
367
368 static int StorVscChannelInit(struct hv_device *Device)
369 {
370         int ret=0;
371         STORVSC_DEVICE *storDevice;
372         STORVSC_REQUEST_EXTENSION *request;
373         VSTOR_PACKET *vstorPacket;
374
375         storDevice = GetStorDevice(Device);
376         if (!storDevice)
377         {
378                 DPRINT_ERR(STORVSC, "unable to get stor device...device being destroyed?");
379                 DPRINT_EXIT(STORVSC);
380                 return -1;
381         }
382
383         request = &storDevice->InitRequest;
384         vstorPacket = &request->VStorPacket;
385
386         /* Now, initiate the vsc/vsp initialization protocol on the open channel */
387
388         memset(request, sizeof(STORVSC_REQUEST_EXTENSION), 0);
389         request->WaitEvent = WaitEventCreate();
390
391         vstorPacket->Operation = VStorOperationBeginInitialization;
392         vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
393
394         /*SpinlockAcquire(gDriverExt.packetListLock);
395         INSERT_TAIL_LIST(&gDriverExt.packetList, &packet->listEntry.entry);
396         SpinlockRelease(gDriverExt.packetListLock);*/
397
398         DPRINT_INFO(STORVSC, "BEGIN_INITIALIZATION_OPERATION...");
399
400         ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
401                                                                                                                         vstorPacket,
402                                                                                                                         sizeof(VSTOR_PACKET),
403                                                                                                                         (unsigned long)request,
404                                                                                                                         VmbusPacketTypeDataInBand,
405                                                                                                                         VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
406         if ( ret != 0)
407         {
408                 DPRINT_ERR(STORVSC, "unable to send BEGIN_INITIALIZATION_OPERATION");
409                 goto Cleanup;
410         }
411
412         WaitEventWait(request->WaitEvent);
413
414         if (vstorPacket->Operation != VStorOperationCompleteIo || vstorPacket->Status != 0)
415         {
416                 DPRINT_ERR(STORVSC, "BEGIN_INITIALIZATION_OPERATION failed (op %d status 0x%x)", vstorPacket->Operation, vstorPacket->Status);
417                 goto Cleanup;
418         }
419
420         DPRINT_INFO(STORVSC, "QUERY_PROTOCOL_VERSION_OPERATION...");
421
422         /* reuse the packet for version range supported */
423         memset(vstorPacket, sizeof(VSTOR_PACKET), 0);
424         vstorPacket->Operation = VStorOperationQueryProtocolVersion;
425         vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
426
427     vstorPacket->Version.MajorMinor = VMSTOR_PROTOCOL_VERSION_CURRENT;
428     FILL_VMSTOR_REVISION(vstorPacket->Version.Revision);
429
430         ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
431                                                                                                                         vstorPacket,
432                                                                                                                         sizeof(VSTOR_PACKET),
433                                                                                                                         (unsigned long)request,
434                                                                                                                         VmbusPacketTypeDataInBand,
435                                                                                                                         VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
436         if ( ret != 0)
437         {
438                 DPRINT_ERR(STORVSC, "unable to send BEGIN_INITIALIZATION_OPERATION");
439                 goto Cleanup;
440         }
441
442         WaitEventWait(request->WaitEvent);
443
444         /* TODO: Check returned version */
445         if (vstorPacket->Operation != VStorOperationCompleteIo || vstorPacket->Status != 0)
446         {
447                 DPRINT_ERR(STORVSC, "QUERY_PROTOCOL_VERSION_OPERATION failed (op %d status 0x%x)", vstorPacket->Operation, vstorPacket->Status);
448                 goto Cleanup;
449         }
450
451         /* Query channel properties */
452         DPRINT_INFO(STORVSC, "QUERY_PROPERTIES_OPERATION...");
453
454         memset(vstorPacket, sizeof(VSTOR_PACKET), 0);
455     vstorPacket->Operation = VStorOperationQueryProperties;
456         vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
457     vstorPacket->StorageChannelProperties.PortNumber = storDevice->PortNumber;
458
459         ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
460                                                                                                                         vstorPacket,
461                                                                                                                         sizeof(VSTOR_PACKET),
462                                                                                                                         (unsigned long)request,
463                                                                                                                         VmbusPacketTypeDataInBand,
464                                                                                                                         VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
465
466         if ( ret != 0)
467         {
468                 DPRINT_ERR(STORVSC, "unable to send QUERY_PROPERTIES_OPERATION");
469                 goto Cleanup;
470         }
471
472         WaitEventWait(request->WaitEvent);
473
474         /* TODO: Check returned version */
475         if (vstorPacket->Operation != VStorOperationCompleteIo || vstorPacket->Status != 0)
476         {
477                 DPRINT_ERR(STORVSC, "QUERY_PROPERTIES_OPERATION failed (op %d status 0x%x)", vstorPacket->Operation, vstorPacket->Status);
478                 goto Cleanup;
479         }
480
481         /* storDevice->PortNumber = vstorPacket->StorageChannelProperties.PortNumber; */
482         storDevice->PathId = vstorPacket->StorageChannelProperties.PathId;
483         storDevice->TargetId = vstorPacket->StorageChannelProperties.TargetId;
484
485         DPRINT_DBG(STORVSC, "channel flag 0x%x, max xfer len 0x%x", vstorPacket->StorageChannelProperties.Flags, vstorPacket->StorageChannelProperties.MaxTransferBytes);
486
487         DPRINT_INFO(STORVSC, "END_INITIALIZATION_OPERATION...");
488
489         memset(vstorPacket, sizeof(VSTOR_PACKET), 0);
490     vstorPacket->Operation = VStorOperationEndInitialization;
491         vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
492
493         ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
494                                                                                                                         vstorPacket,
495                                                                                                                         sizeof(VSTOR_PACKET),
496                                                                                                                         (unsigned long)request,
497                                                                                                                         VmbusPacketTypeDataInBand,
498                                                                                                                         VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
499
500         if ( ret != 0)
501         {
502                 DPRINT_ERR(STORVSC, "unable to send END_INITIALIZATION_OPERATION");
503                 goto Cleanup;
504         }
505
506         WaitEventWait(request->WaitEvent);
507
508         if (vstorPacket->Operation != VStorOperationCompleteIo || vstorPacket->Status != 0)
509         {
510                 DPRINT_ERR(STORVSC, "END_INITIALIZATION_OPERATION failed (op %d status 0x%x)", vstorPacket->Operation, vstorPacket->Status);
511                 goto Cleanup;
512         }
513
514         DPRINT_INFO(STORVSC, "**** storage channel up and running!! ****");
515
516 Cleanup:
517         if (request->WaitEvent)
518         {
519                 WaitEventClose(request->WaitEvent);
520                 request->WaitEvent = NULL;
521         }
522
523         PutStorDevice(Device);
524
525         DPRINT_EXIT(STORVSC);
526         return ret;
527 }
528
529
530 int
531 StorVscConnectToVsp(
532         struct hv_device *Device
533         )
534 {
535         int ret=0;
536     VMSTORAGE_CHANNEL_PROPERTIES props;
537
538         STORVSC_DRIVER_OBJECT *storDriver = (STORVSC_DRIVER_OBJECT*) Device->Driver;;
539
540         memset(&props, sizeof(VMSTORAGE_CHANNEL_PROPERTIES), 0);
541
542         /* Open the channel */
543         ret = Device->Driver->VmbusChannelInterface.Open(Device,
544                 storDriver->RingBufferSize,
545                 storDriver->RingBufferSize,
546                 (void *)&props,
547                 sizeof(VMSTORAGE_CHANNEL_PROPERTIES),
548                 StorVscOnChannelCallback,
549                 Device
550                 );
551
552         DPRINT_DBG(STORVSC, "storage props: path id %d, tgt id %d, max xfer %d", props.PathId, props.TargetId, props.MaxTransferBytes);
553
554         if (ret != 0)
555         {
556                 DPRINT_ERR(STORVSC, "unable to open channel: %d", ret);
557                 return -1;
558         }
559
560         ret = StorVscChannelInit(Device);
561
562         return ret;
563 }
564
565
566 /*++
567
568 Name:
569         StorVscOnDeviceRemove()
570
571 Description:
572         Callback when the our device is being removed
573
574 --*/
575 int
576 StorVscOnDeviceRemove(
577         struct hv_device *Device
578         )
579 {
580         STORVSC_DEVICE *storDevice;
581         int ret=0;
582
583         DPRINT_ENTER(STORVSC);
584
585         DPRINT_INFO(STORVSC, "disabling storage device (%p)...", Device->Extension);
586
587         storDevice = ReleaseStorDevice(Device);
588
589         /*
590          * At this point, all outbound traffic should be disable. We
591          * only allow inbound traffic (responses) to proceed so that
592          * outstanding requests can be completed.
593          */
594         while (storDevice->NumOutstandingRequests)
595         {
596                 DPRINT_INFO(STORVSC, "waiting for %d requests to complete...", storDevice->NumOutstandingRequests);
597
598                 udelay(100);
599         }
600
601         DPRINT_INFO(STORVSC, "removing storage device (%p)...", Device->Extension);
602
603         storDevice = FinalReleaseStorDevice(Device);
604
605         DPRINT_INFO(STORVSC, "storage device (%p) safe to remove", storDevice);
606
607         /* Close the channel */
608         Device->Driver->VmbusChannelInterface.Close(Device);
609
610         FreeStorDevice(storDevice);
611
612         DPRINT_EXIT(STORVSC);
613         return ret;
614 }
615
616 /* ***************
617 static void
618 StorVscOnTargetRescan(
619 void *Context
620 )
621 {
622 struct hv_device *device=(struct hv_device *)Context;
623 STORVSC_DRIVER_OBJECT *storDriver;
624
625 DPRINT_ENTER(STORVSC);
626
627 storDriver = (STORVSC_DRIVER_OBJECT*) device->Driver;
628 storDriver->OnHostRescan(device);
629
630 DPRINT_EXIT(STORVSC);
631 }
632 *********** */
633
634 int
635 StorVscOnHostReset(
636         struct hv_device *Device
637         )
638 {
639         int ret=0;
640
641         STORVSC_DEVICE *storDevice;
642         STORVSC_REQUEST_EXTENSION *request;
643         VSTOR_PACKET *vstorPacket;
644
645         DPRINT_ENTER(STORVSC);
646
647         DPRINT_INFO(STORVSC, "resetting host adapter...");
648
649         storDevice = GetStorDevice(Device);
650         if (!storDevice)
651         {
652                 DPRINT_ERR(STORVSC, "unable to get stor device...device being destroyed?");
653                 DPRINT_EXIT(STORVSC);
654                 return -1;
655         }
656
657         request = &storDevice->ResetRequest;
658         vstorPacket = &request->VStorPacket;
659
660         request->WaitEvent = WaitEventCreate();
661
662     vstorPacket->Operation = VStorOperationResetBus;
663     vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
664     vstorPacket->VmSrb.PathId = storDevice->PathId;
665
666         ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
667                                                                                                                         vstorPacket,
668                                                                                                                         sizeof(VSTOR_PACKET),
669                                                                                                                         (unsigned long)&storDevice->ResetRequest,
670                                                                                                                         VmbusPacketTypeDataInBand,
671                                                                                                                         VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
672         if (ret != 0)
673         {
674                 DPRINT_ERR(STORVSC, "Unable to send reset packet %p ret %d", vstorPacket, ret);
675                 goto Cleanup;
676         }
677
678         /* FIXME: Add a timeout */
679         WaitEventWait(request->WaitEvent);
680
681         WaitEventClose(request->WaitEvent);
682         DPRINT_INFO(STORVSC, "host adapter reset completed");
683
684         /*
685          * At this point, all outstanding requests in the adapter
686          * should have been flushed out and return to us
687          */
688
689 Cleanup:
690         PutStorDevice(Device);
691         DPRINT_EXIT(STORVSC);
692         return ret;
693 }
694
695 /*++
696
697 Name:
698         StorVscOnIORequest()
699
700 Description:
701         Callback to initiate an I/O request
702
703 --*/
704 int
705 StorVscOnIORequest(
706         struct hv_device *Device,
707         STORVSC_REQUEST *Request
708         )
709 {
710         STORVSC_DEVICE *storDevice;
711         STORVSC_REQUEST_EXTENSION* requestExtension = (STORVSC_REQUEST_EXTENSION*) Request->Extension;
712         VSTOR_PACKET* vstorPacket =&requestExtension->VStorPacket;
713         int ret=0;
714
715         DPRINT_ENTER(STORVSC);
716
717         storDevice = GetStorDevice(Device);
718
719         DPRINT_DBG(STORVSC, "enter - Device %p, DeviceExt %p, Request %p, Extension %p",
720                 Device, storDevice, Request, requestExtension);
721
722         DPRINT_DBG(STORVSC, "req %p len %d bus %d, target %d, lun %d cdblen %d",
723                 Request, Request->DataBuffer.Length, Request->Bus, Request->TargetId, Request->LunId, Request->CdbLen);
724
725         if (!storDevice)
726         {
727                 DPRINT_ERR(STORVSC, "unable to get stor device...device being destroyed?");
728                 DPRINT_EXIT(STORVSC);
729                 return -2;
730         }
731
732         /* print_hex_dump_bytes("", DUMP_PREFIX_NONE, Request->Cdb, Request->CdbLen); */
733
734         requestExtension->Request = Request;
735         requestExtension->Device  = Device;
736
737         memset(vstorPacket, 0 , sizeof(VSTOR_PACKET));
738
739         vstorPacket->Flags |= REQUEST_COMPLETION_FLAG;
740
741     vstorPacket->VmSrb.Length = sizeof(VMSCSI_REQUEST);
742
743         vstorPacket->VmSrb.PortNumber = Request->Host;
744     vstorPacket->VmSrb.PathId = Request->Bus;
745     vstorPacket->VmSrb.TargetId = Request->TargetId;
746     vstorPacket->VmSrb.Lun = Request->LunId;
747
748         vstorPacket->VmSrb.SenseInfoLength = SENSE_BUFFER_SIZE;
749
750         /* Copy over the scsi command descriptor block */
751     vstorPacket->VmSrb.CdbLength = Request->CdbLen;
752         memcpy(&vstorPacket->VmSrb.Cdb, Request->Cdb, Request->CdbLen);
753
754         vstorPacket->VmSrb.DataIn = Request->Type;
755         vstorPacket->VmSrb.DataTransferLength = Request->DataBuffer.Length;
756
757         vstorPacket->Operation = VStorOperationExecuteSRB;
758
759         DPRINT_DBG(STORVSC, "srb - len %d port %d, path %d, target %d, lun %d senselen %d cdblen %d",
760                 vstorPacket->VmSrb.Length,
761                 vstorPacket->VmSrb.PortNumber,
762                 vstorPacket->VmSrb.PathId,
763                 vstorPacket->VmSrb.TargetId,
764                 vstorPacket->VmSrb.Lun,
765                 vstorPacket->VmSrb.SenseInfoLength,
766                 vstorPacket->VmSrb.CdbLength);
767
768         if (requestExtension->Request->DataBuffer.Length)
769         {
770                 ret = Device->Driver->VmbusChannelInterface.SendPacketMultiPageBuffer(Device,
771                                 &requestExtension->Request->DataBuffer,
772                                 vstorPacket,
773                                 sizeof(VSTOR_PACKET),
774                                 (unsigned long)requestExtension);
775         }
776         else
777         {
778                 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
779                                                                                                                         vstorPacket,
780                                                                                                                         sizeof(VSTOR_PACKET),
781                                                                                                                         (unsigned long)requestExtension,
782                                                                                                                         VmbusPacketTypeDataInBand,
783                                                                                                                         VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
784         }
785
786         if (ret != 0)
787         {
788                 DPRINT_DBG(STORVSC, "Unable to send packet %p ret %d", vstorPacket, ret);
789         }
790
791         InterlockedIncrement(&storDevice->NumOutstandingRequests);
792
793         PutStorDevice(Device);
794
795         DPRINT_EXIT(STORVSC);
796         return ret;
797 }
798
799 /*++
800
801 Name:
802         StorVscOnCleanup()
803
804 Description:
805         Perform any cleanup when the driver is removed
806
807 --*/
808 void
809 StorVscOnCleanup(
810         DRIVER_OBJECT *Driver
811         )
812 {
813         DPRINT_ENTER(STORVSC);
814         DPRINT_EXIT(STORVSC);
815 }
816
817
818 static void
819 StorVscOnIOCompletion(
820         struct hv_device *Device,
821         VSTOR_PACKET    *VStorPacket,
822         STORVSC_REQUEST_EXTENSION *RequestExt
823         )
824 {
825         STORVSC_REQUEST *request;
826         STORVSC_DEVICE *storDevice;
827
828         DPRINT_ENTER(STORVSC);
829
830         storDevice = MustGetStorDevice(Device);
831         if (!storDevice)
832         {
833                 DPRINT_ERR(STORVSC, "unable to get stor device...device being destroyed?");
834                 DPRINT_EXIT(STORVSC);
835                 return;
836         }
837
838         DPRINT_DBG(STORVSC, "IO_COMPLETE_OPERATION - request extension %p completed bytes xfer %u",
839                 RequestExt, VStorPacket->VmSrb.DataTransferLength);
840
841         ASSERT(RequestExt != NULL);
842         ASSERT(RequestExt->Request != NULL);
843
844         request = RequestExt->Request;
845
846         ASSERT(request->OnIOCompletion != NULL);
847
848         /* Copy over the status...etc */
849         request->Status = VStorPacket->VmSrb.ScsiStatus;
850
851         if (request->Status != 0 || VStorPacket->VmSrb.SrbStatus != 1)
852         {
853                 DPRINT_WARN(STORVSC, "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
854                         request->Cdb[0],
855                         VStorPacket->VmSrb.ScsiStatus,
856                         VStorPacket->VmSrb.SrbStatus);
857         }
858
859         if ((request->Status & 0xFF) == 0x02) /* CHECK_CONDITION */
860         {
861                 if (VStorPacket->VmSrb.SrbStatus & 0x80) /* autosense data available */
862                 {
863                         DPRINT_WARN(STORVSC, "storvsc pkt %p autosense data valid - len %d\n",
864                                 RequestExt, VStorPacket->VmSrb.SenseInfoLength);
865
866                         ASSERT(VStorPacket->VmSrb.SenseInfoLength <=  request->SenseBufferSize);
867                         memcpy(request->SenseBuffer,
868                                 VStorPacket->VmSrb.SenseData,
869                                 VStorPacket->VmSrb.SenseInfoLength);
870
871                         request->SenseBufferSize = VStorPacket->VmSrb.SenseInfoLength;
872                 }
873         }
874
875         /* TODO: */
876         request->BytesXfer = VStorPacket->VmSrb.DataTransferLength;
877
878         request->OnIOCompletion(request);
879
880         InterlockedDecrement(&storDevice->NumOutstandingRequests);
881
882         PutStorDevice(Device);
883
884         DPRINT_EXIT(STORVSC);
885 }
886
887
888 static void
889 StorVscOnReceive(
890         struct hv_device *Device,
891         VSTOR_PACKET    *VStorPacket,
892         STORVSC_REQUEST_EXTENSION *RequestExt
893         )
894 {
895         switch(VStorPacket->Operation)
896         {
897                 case VStorOperationCompleteIo:
898
899                         DPRINT_DBG(STORVSC, "IO_COMPLETE_OPERATION");
900                         StorVscOnIOCompletion(Device, VStorPacket, RequestExt);
901                         break;
902
903                 /* case ENUMERATE_DEVICE_OPERATION: */
904
905                 /* DPRINT_INFO(STORVSC, "ENUMERATE_DEVICE_OPERATION"); */
906
907                 /* StorVscOnTargetRescan(Device); */
908                 /* break; */
909
910         case VStorOperationRemoveDevice:
911
912                         DPRINT_INFO(STORVSC, "REMOVE_DEVICE_OPERATION");
913                         /* TODO: */
914                         break;
915
916                 default:
917                         DPRINT_INFO(STORVSC, "Unknown operation received - %d", VStorPacket->Operation);
918                         break;
919         }
920 }
921
922 void
923 StorVscOnChannelCallback(
924         void * Context
925         )
926 {
927         int ret=0;
928         struct hv_device *device = (struct hv_device*)Context;
929         STORVSC_DEVICE *storDevice;
930         u32 bytesRecvd;
931         u64 requestId;
932         unsigned char packet[ALIGN_UP(sizeof(VSTOR_PACKET),8)];
933         STORVSC_REQUEST_EXTENSION *request;
934
935         DPRINT_ENTER(STORVSC);
936
937         ASSERT(device);
938
939         storDevice = MustGetStorDevice(device);
940         if (!storDevice)
941         {
942                 DPRINT_ERR(STORVSC, "unable to get stor device...device being destroyed?");
943                 DPRINT_EXIT(STORVSC);
944                 return;
945         }
946
947         do
948         {
949                 ret = device->Driver->VmbusChannelInterface.RecvPacket(device,
950                                                                                                                                 packet,
951                                                                                                                                 ALIGN_UP(sizeof(VSTOR_PACKET),8),
952                                                                                                                                 &bytesRecvd,
953                                                                                                                                 &requestId);
954                 if (ret == 0 && bytesRecvd > 0)
955                 {
956                         DPRINT_DBG(STORVSC, "receive %d bytes - tid %llx", bytesRecvd, requestId);
957
958                         /* ASSERT(bytesRecvd == sizeof(VSTOR_PACKET)); */
959
960                         request = (STORVSC_REQUEST_EXTENSION*)(unsigned long)requestId;
961                         ASSERT(request);
962
963                         /* if (vstorPacket.Flags & SYNTHETIC_FLAG) */
964                         if ((request == &storDevice->InitRequest) || (request == &storDevice->ResetRequest))
965                         {
966                                 /* DPRINT_INFO(STORVSC, "reset completion - operation %u status %u", vstorPacket.Operation, vstorPacket.Status); */
967
968                                 memcpy(&request->VStorPacket, packet, sizeof(VSTOR_PACKET));
969
970                                 WaitEventSet(request->WaitEvent);
971                         }
972                         else
973                         {
974                                 StorVscOnReceive(device, (VSTOR_PACKET*)packet, request);
975                         }
976                 }
977                 else
978                 {
979                         /* DPRINT_DBG(STORVSC, "nothing else to read..."); */
980                         break;
981                 }
982         } while (1);
983
984         PutStorDevice(device);
985
986         DPRINT_EXIT(STORVSC);
987         return;
988 }