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