Staging: hv: remove wrapper functions for atomic operations
[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         struct hv_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         atomic_t RefCount; /* 0 indicates the device is being destroyed */
61
62         atomic_t 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         struct hv_storvsc_request *Request
112         );
113
114 static int
115 StorVscOnHostReset(
116         struct hv_device *Device
117         );
118
119 static void
120 StorVscOnCleanup(
121         struct hv_driver *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         atomic_cmpxchg(&storDevice->RefCount, 0, 2);
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( atomic_read(&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 && atomic_read(&storDevice->RefCount) > 1)
179                 atomic_inc(&storDevice->RefCount);
180         else
181                 storDevice = NULL;
182
183         return storDevice;
184 }
185
186 /* Get the stordevice object iff exists and its refcount > 0 */
187 static inline STORVSC_DEVICE* MustGetStorDevice(struct hv_device *Device)
188 {
189         STORVSC_DEVICE *storDevice;
190
191         storDevice = (STORVSC_DEVICE*)Device->Extension;
192         if (storDevice && atomic_read(&storDevice->RefCount))
193                 atomic_inc(&storDevice->RefCount);
194         else
195                 storDevice = NULL;
196
197         return storDevice;
198 }
199
200 static inline void PutStorDevice(struct hv_device *Device)
201 {
202         STORVSC_DEVICE *storDevice;
203
204         storDevice = (STORVSC_DEVICE*)Device->Extension;
205         ASSERT(storDevice);
206
207         atomic_dec(&storDevice->RefCount);
208         ASSERT(atomic_read(&storDevice->RefCount));
209 }
210
211 /* Drop ref count to 1 to effectively disable GetStorDevice() */
212 static inline STORVSC_DEVICE* ReleaseStorDevice(struct hv_device *Device)
213 {
214         STORVSC_DEVICE *storDevice;
215
216         storDevice = (STORVSC_DEVICE*)Device->Extension;
217         ASSERT(storDevice);
218
219         /* Busy wait until the ref drop to 2, then set it to 1 */
220         while (atomic_cmpxchg(&storDevice->RefCount, 2, 1) != 2)
221         {
222                 udelay(100);
223         }
224
225         return storDevice;
226 }
227
228 /* Drop ref count to 0. No one can use StorDevice object. */
229 static inline STORVSC_DEVICE* FinalReleaseStorDevice(struct hv_device *Device)
230 {
231         STORVSC_DEVICE *storDevice;
232
233         storDevice = (STORVSC_DEVICE*)Device->Extension;
234         ASSERT(storDevice);
235
236         /* Busy wait until the ref drop to 1, then set it to 0 */
237         while (atomic_cmpxchg(&storDevice->RefCount, 1, 0) != 1)
238         {
239                 udelay(100);
240         }
241
242         Device->Extension = NULL;
243         return storDevice;
244 }
245
246 /*++;
247
248
249 Name:
250         StorVscInitialize()
251
252 Description:
253         Main entry point
254
255 --*/
256 int
257 StorVscInitialize(
258         struct hv_driver *Driver
259         )
260 {
261         STORVSC_DRIVER_OBJECT* storDriver = (STORVSC_DRIVER_OBJECT*)Driver;
262         int ret=0;
263
264         DPRINT_ENTER(STORVSC);
265
266         DPRINT_DBG(STORVSC, "sizeof(STORVSC_REQUEST)=%zd sizeof(STORVSC_REQUEST_EXTENSION)=%zd sizeof(VSTOR_PACKET)=%zd, sizeof(VMSCSI_REQUEST)=%zd",
267                 sizeof(struct hv_storvsc_request), sizeof(STORVSC_REQUEST_EXTENSION), sizeof(VSTOR_PACKET), sizeof(VMSCSI_REQUEST));
268
269         /* Make sure we are at least 2 pages since 1 page is used for control */
270         ASSERT(storDriver->RingBufferSize >= (PAGE_SIZE << 1));
271
272         Driver->name = gDriverName;
273         memcpy(&Driver->deviceType, &gStorVscDeviceType, sizeof(GUID));
274
275         storDriver->RequestExtSize                      = sizeof(STORVSC_REQUEST_EXTENSION);
276
277         /*
278          * Divide the ring buffer data size (which is 1 page less
279          * than the ring buffer size since that page is reserved for
280          * the ring buffer indices) by the max request size (which is
281          * VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER + VSTOR_PACKET + u64)
282          */
283         storDriver->MaxOutstandingRequestsPerChannel =
284                 ((storDriver->RingBufferSize - PAGE_SIZE) / ALIGN_UP(MAX_MULTIPAGE_BUFFER_PACKET + sizeof(VSTOR_PACKET) + sizeof(u64),sizeof(u64)));
285
286         DPRINT_INFO(STORVSC, "max io %u, currently %u\n", storDriver->MaxOutstandingRequestsPerChannel, STORVSC_MAX_IO_REQUESTS);
287
288         /* Setup the dispatch table */
289         storDriver->Base.OnDeviceAdd                    = StorVscOnDeviceAdd;
290         storDriver->Base.OnDeviceRemove         = StorVscOnDeviceRemove;
291         storDriver->Base.OnCleanup                      = StorVscOnCleanup;
292
293         storDriver->OnIORequest                         = StorVscOnIORequest;
294         storDriver->OnHostReset                         = StorVscOnHostReset;
295
296         DPRINT_EXIT(STORVSC);
297
298         return ret;
299 }
300
301 /*++
302
303 Name:
304         StorVscOnDeviceAdd()
305
306 Description:
307         Callback when the device belonging to this driver is added
308
309 --*/
310 static int
311 StorVscOnDeviceAdd(
312         struct hv_device *Device,
313         void                    *AdditionalInfo
314         )
315 {
316         int ret=0;
317         STORVSC_DEVICE *storDevice;
318         /* VMSTORAGE_CHANNEL_PROPERTIES *props; */
319         STORVSC_DEVICE_INFO *deviceInfo = (STORVSC_DEVICE_INFO*)AdditionalInfo;
320
321         DPRINT_ENTER(STORVSC);
322
323         storDevice = AllocStorDevice(Device);
324         if (!storDevice)
325         {
326                 ret = -1;
327                 goto Cleanup;
328         }
329
330         /* Save the channel properties to our storvsc channel */
331         /* props = (VMSTORAGE_CHANNEL_PROPERTIES*) channel->offerMsg.Offer.u.Standard.UserDefined; */
332
333         /* FIXME: */
334         /*
335          * If we support more than 1 scsi channel, we need to set the
336          * port number here to the scsi channel but how do we get the
337          * scsi channel prior to the bus scan
338          */
339
340         /* storChannel->PortNumber = 0;
341         storChannel->PathId = props->PathId;
342         storChannel->TargetId = props->TargetId; */
343
344         storDevice->PortNumber = deviceInfo->PortNumber;
345         /* Send it back up */
346         ret = StorVscConnectToVsp(Device);
347
348         /* deviceInfo->PortNumber = storDevice->PortNumber; */
349         deviceInfo->PathId = storDevice->PathId;
350         deviceInfo->TargetId = storDevice->TargetId;
351
352         DPRINT_DBG(STORVSC, "assigned port %u, path %u target %u\n", storDevice->PortNumber, storDevice->PathId, storDevice->TargetId);
353
354 Cleanup:
355         DPRINT_EXIT(STORVSC);
356
357         return ret;
358 }
359
360 static int StorVscChannelInit(struct hv_device *Device)
361 {
362         int ret=0;
363         STORVSC_DEVICE *storDevice;
364         STORVSC_REQUEST_EXTENSION *request;
365         VSTOR_PACKET *vstorPacket;
366
367         storDevice = GetStorDevice(Device);
368         if (!storDevice)
369         {
370                 DPRINT_ERR(STORVSC, "unable to get stor device...device being destroyed?");
371                 DPRINT_EXIT(STORVSC);
372                 return -1;
373         }
374
375         request = &storDevice->InitRequest;
376         vstorPacket = &request->VStorPacket;
377
378         /* Now, initiate the vsc/vsp initialization protocol on the open channel */
379
380         memset(request, sizeof(STORVSC_REQUEST_EXTENSION), 0);
381         request->WaitEvent = WaitEventCreate();
382
383         vstorPacket->Operation = VStorOperationBeginInitialization;
384         vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
385
386         /*SpinlockAcquire(gDriverExt.packetListLock);
387         INSERT_TAIL_LIST(&gDriverExt.packetList, &packet->listEntry.entry);
388         SpinlockRelease(gDriverExt.packetListLock);*/
389
390         DPRINT_INFO(STORVSC, "BEGIN_INITIALIZATION_OPERATION...");
391
392         ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
393                                                                                                                         vstorPacket,
394                                                                                                                         sizeof(VSTOR_PACKET),
395                                                                                                                         (unsigned long)request,
396                                                                                                                         VmbusPacketTypeDataInBand,
397                                                                                                                         VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
398         if ( ret != 0)
399         {
400                 DPRINT_ERR(STORVSC, "unable to send BEGIN_INITIALIZATION_OPERATION");
401                 goto Cleanup;
402         }
403
404         WaitEventWait(request->WaitEvent);
405
406         if (vstorPacket->Operation != VStorOperationCompleteIo || vstorPacket->Status != 0)
407         {
408                 DPRINT_ERR(STORVSC, "BEGIN_INITIALIZATION_OPERATION failed (op %d status 0x%x)", vstorPacket->Operation, vstorPacket->Status);
409                 goto Cleanup;
410         }
411
412         DPRINT_INFO(STORVSC, "QUERY_PROTOCOL_VERSION_OPERATION...");
413
414         /* reuse the packet for version range supported */
415         memset(vstorPacket, sizeof(VSTOR_PACKET), 0);
416         vstorPacket->Operation = VStorOperationQueryProtocolVersion;
417         vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
418
419     vstorPacket->Version.MajorMinor = VMSTOR_PROTOCOL_VERSION_CURRENT;
420     FILL_VMSTOR_REVISION(vstorPacket->Version.Revision);
421
422         ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
423                                                                                                                         vstorPacket,
424                                                                                                                         sizeof(VSTOR_PACKET),
425                                                                                                                         (unsigned long)request,
426                                                                                                                         VmbusPacketTypeDataInBand,
427                                                                                                                         VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
428         if ( ret != 0)
429         {
430                 DPRINT_ERR(STORVSC, "unable to send BEGIN_INITIALIZATION_OPERATION");
431                 goto Cleanup;
432         }
433
434         WaitEventWait(request->WaitEvent);
435
436         /* TODO: Check returned version */
437         if (vstorPacket->Operation != VStorOperationCompleteIo || vstorPacket->Status != 0)
438         {
439                 DPRINT_ERR(STORVSC, "QUERY_PROTOCOL_VERSION_OPERATION failed (op %d status 0x%x)", vstorPacket->Operation, vstorPacket->Status);
440                 goto Cleanup;
441         }
442
443         /* Query channel properties */
444         DPRINT_INFO(STORVSC, "QUERY_PROPERTIES_OPERATION...");
445
446         memset(vstorPacket, sizeof(VSTOR_PACKET), 0);
447     vstorPacket->Operation = VStorOperationQueryProperties;
448         vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
449     vstorPacket->StorageChannelProperties.PortNumber = storDevice->PortNumber;
450
451         ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
452                                                                                                                         vstorPacket,
453                                                                                                                         sizeof(VSTOR_PACKET),
454                                                                                                                         (unsigned long)request,
455                                                                                                                         VmbusPacketTypeDataInBand,
456                                                                                                                         VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
457
458         if ( ret != 0)
459         {
460                 DPRINT_ERR(STORVSC, "unable to send QUERY_PROPERTIES_OPERATION");
461                 goto Cleanup;
462         }
463
464         WaitEventWait(request->WaitEvent);
465
466         /* TODO: Check returned version */
467         if (vstorPacket->Operation != VStorOperationCompleteIo || vstorPacket->Status != 0)
468         {
469                 DPRINT_ERR(STORVSC, "QUERY_PROPERTIES_OPERATION failed (op %d status 0x%x)", vstorPacket->Operation, vstorPacket->Status);
470                 goto Cleanup;
471         }
472
473         /* storDevice->PortNumber = vstorPacket->StorageChannelProperties.PortNumber; */
474         storDevice->PathId = vstorPacket->StorageChannelProperties.PathId;
475         storDevice->TargetId = vstorPacket->StorageChannelProperties.TargetId;
476
477         DPRINT_DBG(STORVSC, "channel flag 0x%x, max xfer len 0x%x", vstorPacket->StorageChannelProperties.Flags, vstorPacket->StorageChannelProperties.MaxTransferBytes);
478
479         DPRINT_INFO(STORVSC, "END_INITIALIZATION_OPERATION...");
480
481         memset(vstorPacket, sizeof(VSTOR_PACKET), 0);
482     vstorPacket->Operation = VStorOperationEndInitialization;
483         vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
484
485         ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
486                                                                                                                         vstorPacket,
487                                                                                                                         sizeof(VSTOR_PACKET),
488                                                                                                                         (unsigned long)request,
489                                                                                                                         VmbusPacketTypeDataInBand,
490                                                                                                                         VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
491
492         if ( ret != 0)
493         {
494                 DPRINT_ERR(STORVSC, "unable to send END_INITIALIZATION_OPERATION");
495                 goto Cleanup;
496         }
497
498         WaitEventWait(request->WaitEvent);
499
500         if (vstorPacket->Operation != VStorOperationCompleteIo || vstorPacket->Status != 0)
501         {
502                 DPRINT_ERR(STORVSC, "END_INITIALIZATION_OPERATION failed (op %d status 0x%x)", vstorPacket->Operation, vstorPacket->Status);
503                 goto Cleanup;
504         }
505
506         DPRINT_INFO(STORVSC, "**** storage channel up and running!! ****");
507
508 Cleanup:
509         if (request->WaitEvent)
510         {
511                 kfree(request->WaitEvent);
512                 request->WaitEvent = NULL;
513         }
514
515         PutStorDevice(Device);
516
517         DPRINT_EXIT(STORVSC);
518         return ret;
519 }
520
521
522 static int
523 StorVscConnectToVsp(
524         struct hv_device *Device
525         )
526 {
527         int ret=0;
528     VMSTORAGE_CHANNEL_PROPERTIES props;
529
530         STORVSC_DRIVER_OBJECT *storDriver = (STORVSC_DRIVER_OBJECT*) Device->Driver;;
531
532         memset(&props, sizeof(VMSTORAGE_CHANNEL_PROPERTIES), 0);
533
534         /* Open the channel */
535         ret = Device->Driver->VmbusChannelInterface.Open(Device,
536                 storDriver->RingBufferSize,
537                 storDriver->RingBufferSize,
538                 (void *)&props,
539                 sizeof(VMSTORAGE_CHANNEL_PROPERTIES),
540                 StorVscOnChannelCallback,
541                 Device
542                 );
543
544         DPRINT_DBG(STORVSC, "storage props: path id %d, tgt id %d, max xfer %d", props.PathId, props.TargetId, props.MaxTransferBytes);
545
546         if (ret != 0)
547         {
548                 DPRINT_ERR(STORVSC, "unable to open channel: %d", ret);
549                 return -1;
550         }
551
552         ret = StorVscChannelInit(Device);
553
554         return ret;
555 }
556
557
558 /*++
559
560 Name:
561         StorVscOnDeviceRemove()
562
563 Description:
564         Callback when the our device is being removed
565
566 --*/
567 static int
568 StorVscOnDeviceRemove(
569         struct hv_device *Device
570         )
571 {
572         STORVSC_DEVICE *storDevice;
573         int ret=0;
574
575         DPRINT_ENTER(STORVSC);
576
577         DPRINT_INFO(STORVSC, "disabling storage device (%p)...", Device->Extension);
578
579         storDevice = ReleaseStorDevice(Device);
580
581         /*
582          * At this point, all outbound traffic should be disable. We
583          * only allow inbound traffic (responses) to proceed so that
584          * outstanding requests can be completed.
585          */
586         while (atomic_read(&storDevice->NumOutstandingRequests))
587         {
588                 DPRINT_INFO(STORVSC, "waiting for %d requests to complete...", atomic_read(&storDevice->NumOutstandingRequests));
589
590                 udelay(100);
591         }
592
593         DPRINT_INFO(STORVSC, "removing storage device (%p)...", Device->Extension);
594
595         storDevice = FinalReleaseStorDevice(Device);
596
597         DPRINT_INFO(STORVSC, "storage device (%p) safe to remove", storDevice);
598
599         /* Close the channel */
600         Device->Driver->VmbusChannelInterface.Close(Device);
601
602         FreeStorDevice(storDevice);
603
604         DPRINT_EXIT(STORVSC);
605         return ret;
606 }
607
608 /* ***************
609 static void
610 StorVscOnTargetRescan(
611 void *Context
612 )
613 {
614 struct hv_device *device=(struct hv_device *)Context;
615 STORVSC_DRIVER_OBJECT *storDriver;
616
617 DPRINT_ENTER(STORVSC);
618
619 storDriver = (STORVSC_DRIVER_OBJECT*) device->Driver;
620 storDriver->OnHostRescan(device);
621
622 DPRINT_EXIT(STORVSC);
623 }
624 *********** */
625
626 static int
627 StorVscOnHostReset(
628         struct hv_device *Device
629         )
630 {
631         int ret=0;
632
633         STORVSC_DEVICE *storDevice;
634         STORVSC_REQUEST_EXTENSION *request;
635         VSTOR_PACKET *vstorPacket;
636
637         DPRINT_ENTER(STORVSC);
638
639         DPRINT_INFO(STORVSC, "resetting host adapter...");
640
641         storDevice = GetStorDevice(Device);
642         if (!storDevice)
643         {
644                 DPRINT_ERR(STORVSC, "unable to get stor device...device being destroyed?");
645                 DPRINT_EXIT(STORVSC);
646                 return -1;
647         }
648
649         request = &storDevice->ResetRequest;
650         vstorPacket = &request->VStorPacket;
651
652         request->WaitEvent = WaitEventCreate();
653
654     vstorPacket->Operation = VStorOperationResetBus;
655     vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
656     vstorPacket->VmSrb.PathId = storDevice->PathId;
657
658         ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
659                                                                                                                         vstorPacket,
660                                                                                                                         sizeof(VSTOR_PACKET),
661                                                                                                                         (unsigned long)&storDevice->ResetRequest,
662                                                                                                                         VmbusPacketTypeDataInBand,
663                                                                                                                         VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
664         if (ret != 0)
665         {
666                 DPRINT_ERR(STORVSC, "Unable to send reset packet %p ret %d", vstorPacket, ret);
667                 goto Cleanup;
668         }
669
670         /* FIXME: Add a timeout */
671         WaitEventWait(request->WaitEvent);
672
673         kfree(request->WaitEvent);
674         DPRINT_INFO(STORVSC, "host adapter reset completed");
675
676         /*
677          * At this point, all outstanding requests in the adapter
678          * should have been flushed out and return to us
679          */
680
681 Cleanup:
682         PutStorDevice(Device);
683         DPRINT_EXIT(STORVSC);
684         return ret;
685 }
686
687 /*++
688
689 Name:
690         StorVscOnIORequest()
691
692 Description:
693         Callback to initiate an I/O request
694
695 --*/
696 static int
697 StorVscOnIORequest(
698         struct hv_device *Device,
699         struct hv_storvsc_request *Request
700         )
701 {
702         STORVSC_DEVICE *storDevice;
703         STORVSC_REQUEST_EXTENSION* requestExtension = (STORVSC_REQUEST_EXTENSION*) Request->Extension;
704         VSTOR_PACKET* vstorPacket =&requestExtension->VStorPacket;
705         int ret=0;
706
707         DPRINT_ENTER(STORVSC);
708
709         storDevice = GetStorDevice(Device);
710
711         DPRINT_DBG(STORVSC, "enter - Device %p, DeviceExt %p, Request %p, Extension %p",
712                 Device, storDevice, Request, requestExtension);
713
714         DPRINT_DBG(STORVSC, "req %p len %d bus %d, target %d, lun %d cdblen %d",
715                 Request, Request->DataBuffer.Length, Request->Bus, Request->TargetId, Request->LunId, Request->CdbLen);
716
717         if (!storDevice)
718         {
719                 DPRINT_ERR(STORVSC, "unable to get stor device...device being destroyed?");
720                 DPRINT_EXIT(STORVSC);
721                 return -2;
722         }
723
724         /* print_hex_dump_bytes("", DUMP_PREFIX_NONE, Request->Cdb, Request->CdbLen); */
725
726         requestExtension->Request = Request;
727         requestExtension->Device  = Device;
728
729         memset(vstorPacket, 0 , sizeof(VSTOR_PACKET));
730
731         vstorPacket->Flags |= REQUEST_COMPLETION_FLAG;
732
733     vstorPacket->VmSrb.Length = sizeof(VMSCSI_REQUEST);
734
735         vstorPacket->VmSrb.PortNumber = Request->Host;
736     vstorPacket->VmSrb.PathId = Request->Bus;
737     vstorPacket->VmSrb.TargetId = Request->TargetId;
738     vstorPacket->VmSrb.Lun = Request->LunId;
739
740         vstorPacket->VmSrb.SenseInfoLength = SENSE_BUFFER_SIZE;
741
742         /* Copy over the scsi command descriptor block */
743     vstorPacket->VmSrb.CdbLength = Request->CdbLen;
744         memcpy(&vstorPacket->VmSrb.Cdb, Request->Cdb, Request->CdbLen);
745
746         vstorPacket->VmSrb.DataIn = Request->Type;
747         vstorPacket->VmSrb.DataTransferLength = Request->DataBuffer.Length;
748
749         vstorPacket->Operation = VStorOperationExecuteSRB;
750
751         DPRINT_DBG(STORVSC, "srb - len %d port %d, path %d, target %d, lun %d senselen %d cdblen %d",
752                 vstorPacket->VmSrb.Length,
753                 vstorPacket->VmSrb.PortNumber,
754                 vstorPacket->VmSrb.PathId,
755                 vstorPacket->VmSrb.TargetId,
756                 vstorPacket->VmSrb.Lun,
757                 vstorPacket->VmSrb.SenseInfoLength,
758                 vstorPacket->VmSrb.CdbLength);
759
760         if (requestExtension->Request->DataBuffer.Length)
761         {
762                 ret = Device->Driver->VmbusChannelInterface.SendPacketMultiPageBuffer(Device,
763                                 &requestExtension->Request->DataBuffer,
764                                 vstorPacket,
765                                 sizeof(VSTOR_PACKET),
766                                 (unsigned long)requestExtension);
767         }
768         else
769         {
770                 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
771                                                                                                                         vstorPacket,
772                                                                                                                         sizeof(VSTOR_PACKET),
773                                                                                                                         (unsigned long)requestExtension,
774                                                                                                                         VmbusPacketTypeDataInBand,
775                                                                                                                         VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
776         }
777
778         if (ret != 0)
779         {
780                 DPRINT_DBG(STORVSC, "Unable to send packet %p ret %d", vstorPacket, ret);
781         }
782
783         atomic_inc(&storDevice->NumOutstandingRequests);
784
785         PutStorDevice(Device);
786
787         DPRINT_EXIT(STORVSC);
788         return ret;
789 }
790
791 /*++
792
793 Name:
794         StorVscOnCleanup()
795
796 Description:
797         Perform any cleanup when the driver is removed
798
799 --*/
800 static void
801 StorVscOnCleanup(
802         struct hv_driver *Driver
803         )
804 {
805         DPRINT_ENTER(STORVSC);
806         DPRINT_EXIT(STORVSC);
807 }
808
809
810 static void
811 StorVscOnIOCompletion(
812         struct hv_device *Device,
813         VSTOR_PACKET    *VStorPacket,
814         STORVSC_REQUEST_EXTENSION *RequestExt
815         )
816 {
817         struct hv_storvsc_request *request;
818         STORVSC_DEVICE *storDevice;
819
820         DPRINT_ENTER(STORVSC);
821
822         storDevice = MustGetStorDevice(Device);
823         if (!storDevice)
824         {
825                 DPRINT_ERR(STORVSC, "unable to get stor device...device being destroyed?");
826                 DPRINT_EXIT(STORVSC);
827                 return;
828         }
829
830         DPRINT_DBG(STORVSC, "IO_COMPLETE_OPERATION - request extension %p completed bytes xfer %u",
831                 RequestExt, VStorPacket->VmSrb.DataTransferLength);
832
833         ASSERT(RequestExt != NULL);
834         ASSERT(RequestExt->Request != NULL);
835
836         request = RequestExt->Request;
837
838         ASSERT(request->OnIOCompletion != NULL);
839
840         /* Copy over the status...etc */
841         request->Status = VStorPacket->VmSrb.ScsiStatus;
842
843         if (request->Status != 0 || VStorPacket->VmSrb.SrbStatus != 1)
844         {
845                 DPRINT_WARN(STORVSC, "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
846                         request->Cdb[0],
847                         VStorPacket->VmSrb.ScsiStatus,
848                         VStorPacket->VmSrb.SrbStatus);
849         }
850
851         if ((request->Status & 0xFF) == 0x02) /* CHECK_CONDITION */
852         {
853                 if (VStorPacket->VmSrb.SrbStatus & 0x80) /* autosense data available */
854                 {
855                         DPRINT_WARN(STORVSC, "storvsc pkt %p autosense data valid - len %d\n",
856                                 RequestExt, VStorPacket->VmSrb.SenseInfoLength);
857
858                         ASSERT(VStorPacket->VmSrb.SenseInfoLength <=  request->SenseBufferSize);
859                         memcpy(request->SenseBuffer,
860                                 VStorPacket->VmSrb.SenseData,
861                                 VStorPacket->VmSrb.SenseInfoLength);
862
863                         request->SenseBufferSize = VStorPacket->VmSrb.SenseInfoLength;
864                 }
865         }
866
867         /* TODO: */
868         request->BytesXfer = VStorPacket->VmSrb.DataTransferLength;
869
870         request->OnIOCompletion(request);
871
872         atomic_dec(&storDevice->NumOutstandingRequests);
873
874         PutStorDevice(Device);
875
876         DPRINT_EXIT(STORVSC);
877 }
878
879
880 static void
881 StorVscOnReceive(
882         struct hv_device *Device,
883         VSTOR_PACKET    *VStorPacket,
884         STORVSC_REQUEST_EXTENSION *RequestExt
885         )
886 {
887         switch(VStorPacket->Operation)
888         {
889                 case VStorOperationCompleteIo:
890
891                         DPRINT_DBG(STORVSC, "IO_COMPLETE_OPERATION");
892                         StorVscOnIOCompletion(Device, VStorPacket, RequestExt);
893                         break;
894
895                 /* case ENUMERATE_DEVICE_OPERATION: */
896
897                 /* DPRINT_INFO(STORVSC, "ENUMERATE_DEVICE_OPERATION"); */
898
899                 /* StorVscOnTargetRescan(Device); */
900                 /* break; */
901
902         case VStorOperationRemoveDevice:
903
904                         DPRINT_INFO(STORVSC, "REMOVE_DEVICE_OPERATION");
905                         /* TODO: */
906                         break;
907
908                 default:
909                         DPRINT_INFO(STORVSC, "Unknown operation received - %d", VStorPacket->Operation);
910                         break;
911         }
912 }
913
914 static void
915 StorVscOnChannelCallback(
916         void * Context
917         )
918 {
919         int ret=0;
920         struct hv_device *device = (struct hv_device*)Context;
921         STORVSC_DEVICE *storDevice;
922         u32 bytesRecvd;
923         u64 requestId;
924         unsigned char packet[ALIGN_UP(sizeof(VSTOR_PACKET),8)];
925         STORVSC_REQUEST_EXTENSION *request;
926
927         DPRINT_ENTER(STORVSC);
928
929         ASSERT(device);
930
931         storDevice = MustGetStorDevice(device);
932         if (!storDevice)
933         {
934                 DPRINT_ERR(STORVSC, "unable to get stor device...device being destroyed?");
935                 DPRINT_EXIT(STORVSC);
936                 return;
937         }
938
939         do
940         {
941                 ret = device->Driver->VmbusChannelInterface.RecvPacket(device,
942                                                                                                                                 packet,
943                                                                                                                                 ALIGN_UP(sizeof(VSTOR_PACKET),8),
944                                                                                                                                 &bytesRecvd,
945                                                                                                                                 &requestId);
946                 if (ret == 0 && bytesRecvd > 0)
947                 {
948                         DPRINT_DBG(STORVSC, "receive %d bytes - tid %llx", bytesRecvd, requestId);
949
950                         /* ASSERT(bytesRecvd == sizeof(VSTOR_PACKET)); */
951
952                         request = (STORVSC_REQUEST_EXTENSION*)(unsigned long)requestId;
953                         ASSERT(request);
954
955                         /* if (vstorPacket.Flags & SYNTHETIC_FLAG) */
956                         if ((request == &storDevice->InitRequest) || (request == &storDevice->ResetRequest))
957                         {
958                                 /* DPRINT_INFO(STORVSC, "reset completion - operation %u status %u", vstorPacket.Operation, vstorPacket.Status); */
959
960                                 memcpy(&request->VStorPacket, packet, sizeof(VSTOR_PACKET));
961
962                                 WaitEventSet(request->WaitEvent);
963                         }
964                         else
965                         {
966                                 StorVscOnReceive(device, (VSTOR_PACKET*)packet, request);
967                         }
968                 }
969                 else
970                 {
971                         /* DPRINT_DBG(STORVSC, "nothing else to read..."); */
972                         break;
973                 }
974         } while (1);
975
976         PutStorDevice(device);
977
978         DPRINT_EXIT(STORVSC);
979         return;
980 }