]> rtime.felk.cvut.cz Git - lisovros/linux_canprio.git/blob - drivers/staging/hv/storvsc.c
e73130e49ccec769930f3266bc567a3ec7640fca
[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 "StorVscApi.h"
29 #include "VmbusPacketFormat.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         DPRINT_ENTER(STORVSC);
360
361         storDevice = MustGetStorDevice(Device);
362         if (!storDevice) {
363                 DPRINT_ERR(STORVSC, "unable to get stor device..."
364                            "device being destroyed?");
365                 DPRINT_EXIT(STORVSC);
366                 return;
367         }
368
369         DPRINT_DBG(STORVSC, "IO_COMPLETE_OPERATION - request extension %p "
370                    "completed bytes xfer %u", RequestExt,
371                    VStorPacket->VmSrb.DataTransferLength);
372
373         /* ASSERT(RequestExt != NULL); */
374         /* ASSERT(RequestExt->Request != NULL); */
375
376         request = RequestExt->Request;
377
378         /* ASSERT(request->OnIOCompletion != NULL); */
379
380         /* Copy over the status...etc */
381         request->Status = VStorPacket->VmSrb.ScsiStatus;
382
383         if (request->Status != 0 || VStorPacket->VmSrb.SrbStatus != 1) {
384                 DPRINT_WARN(STORVSC,
385                             "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
386                             request->Cdb[0], VStorPacket->VmSrb.ScsiStatus,
387                             VStorPacket->VmSrb.SrbStatus);
388         }
389
390         if ((request->Status & 0xFF) == 0x02) {
391                 /* CHECK_CONDITION */
392                 if (VStorPacket->VmSrb.SrbStatus & 0x80) {
393                         /* autosense data available */
394                         DPRINT_WARN(STORVSC, "storvsc pkt %p autosense data "
395                                     "valid - len %d\n", RequestExt,
396                                     VStorPacket->VmSrb.SenseInfoLength);
397
398                         /* ASSERT(VStorPacket->VmSrb.SenseInfoLength <= */
399                         /*      request->SenseBufferSize); */
400                         memcpy(request->SenseBuffer,
401                                VStorPacket->VmSrb.SenseData,
402                                VStorPacket->VmSrb.SenseInfoLength);
403
404                         request->SenseBufferSize =
405                                         VStorPacket->VmSrb.SenseInfoLength;
406                 }
407         }
408
409         /* TODO: */
410         request->BytesXfer = VStorPacket->VmSrb.DataTransferLength;
411
412         request->OnIOCompletion(request);
413
414         atomic_dec(&storDevice->NumOutstandingRequests);
415
416         PutStorDevice(Device);
417
418         DPRINT_EXIT(STORVSC);
419 }
420
421 static void StorVscOnReceive(struct hv_device *Device,
422                              struct vstor_packet *VStorPacket,
423                              struct storvsc_request_extension *RequestExt)
424 {
425         switch (VStorPacket->Operation) {
426         case VStorOperationCompleteIo:
427                 DPRINT_DBG(STORVSC, "IO_COMPLETE_OPERATION");
428                 StorVscOnIOCompletion(Device, VStorPacket, RequestExt);
429                 break;
430         case VStorOperationRemoveDevice:
431                 DPRINT_INFO(STORVSC, "REMOVE_DEVICE_OPERATION");
432                 /* TODO: */
433                 break;
434
435         default:
436                 DPRINT_INFO(STORVSC, "Unknown operation received - %d",
437                             VStorPacket->Operation);
438                 break;
439         }
440 }
441
442 static void StorVscOnChannelCallback(void *context)
443 {
444         struct hv_device *device = (struct hv_device *)context;
445         struct storvsc_device *storDevice;
446         u32 bytesRecvd;
447         u64 requestId;
448         unsigned char packet[ALIGN_UP(sizeof(struct vstor_packet), 8)];
449         struct storvsc_request_extension *request;
450         int ret;
451
452         DPRINT_ENTER(STORVSC);
453
454         /* ASSERT(device); */
455
456         storDevice = MustGetStorDevice(device);
457         if (!storDevice) {
458                 DPRINT_ERR(STORVSC, "unable to get stor device..."
459                            "device being destroyed?");
460                 DPRINT_EXIT(STORVSC);
461                 return;
462         }
463
464         do {
465                 ret = device->Driver->VmbusChannelInterface.RecvPacket(device,
466                                 packet,
467                                 ALIGN_UP(sizeof(struct vstor_packet), 8),
468                                 &bytesRecvd, &requestId);
469                 if (ret == 0 && bytesRecvd > 0) {
470                         DPRINT_DBG(STORVSC, "receive %d bytes - tid %llx",
471                                    bytesRecvd, requestId);
472
473                         /* ASSERT(bytesRecvd == sizeof(struct vstor_packet)); */
474
475                         request = (struct storvsc_request_extension *)
476                                         (unsigned long)requestId;
477                         /* ASSERT(request);c */
478
479                         /* if (vstorPacket.Flags & SYNTHETIC_FLAG) */
480                         if ((request == &storDevice->InitRequest) ||
481                             (request == &storDevice->ResetRequest)) {
482                                 /* DPRINT_INFO(STORVSC,
483                                  *             "reset completion - operation "
484                                  *             "%u status %u",
485                                  *             vstorPacket.Operation,
486                                  *             vstorPacket.Status); */
487
488                                 memcpy(&request->VStorPacket, packet,
489                                        sizeof(struct vstor_packet));
490
491                                 osd_WaitEventSet(request->WaitEvent);
492                         } else {
493                                 StorVscOnReceive(device,
494                                                 (struct vstor_packet *)packet,
495                                                 request);
496                         }
497                 } else {
498                         /* DPRINT_DBG(STORVSC, "nothing else to read..."); */
499                         break;
500                 }
501         } while (1);
502
503         PutStorDevice(device);
504
505         DPRINT_EXIT(STORVSC);
506         return;
507 }
508
509 static int StorVscConnectToVsp(struct hv_device *Device)
510 {
511         struct vmstorage_channel_properties props;
512         struct storvsc_driver_object *storDriver;
513         int ret;
514
515         storDriver = (struct storvsc_driver_object *)Device->Driver;
516         memset(&props, 0, sizeof(struct vmstorage_channel_properties));
517
518         /* Open the channel */
519         ret = Device->Driver->VmbusChannelInterface.Open(Device,
520                         storDriver->RingBufferSize,
521                         storDriver->RingBufferSize,
522                         (void *)&props,
523                         sizeof(struct vmstorage_channel_properties),
524                         StorVscOnChannelCallback,
525                         Device);
526
527         DPRINT_DBG(STORVSC, "storage props: path id %d, tgt id %d, max xfer %d",
528                    props.PathId, props.TargetId, props.MaxTransferBytes);
529
530         if (ret != 0) {
531                 DPRINT_ERR(STORVSC, "unable to open channel: %d", ret);
532                 return -1;
533         }
534
535         ret = StorVscChannelInit(Device);
536
537         return ret;
538 }
539
540 /*
541  * StorVscOnDeviceAdd - Callback when the device belonging to this driver is added
542  */
543 static int StorVscOnDeviceAdd(struct hv_device *Device, void *AdditionalInfo)
544 {
545         struct storvsc_device *storDevice;
546         /* struct vmstorage_channel_properties *props; */
547         struct storvsc_device_info *deviceInfo;
548         int ret = 0;
549
550         DPRINT_ENTER(STORVSC);
551
552         deviceInfo = (struct storvsc_device_info *)AdditionalInfo;
553         storDevice = AllocStorDevice(Device);
554         if (!storDevice) {
555                 ret = -1;
556                 goto Cleanup;
557         }
558
559         /* Save the channel properties to our storvsc channel */
560         /* props = (struct vmstorage_channel_properties *)
561          *              channel->offerMsg.Offer.u.Standard.UserDefined; */
562
563         /* FIXME: */
564         /*
565          * If we support more than 1 scsi channel, we need to set the
566          * port number here to the scsi channel but how do we get the
567          * scsi channel prior to the bus scan
568          */
569
570         /* storChannel->PortNumber = 0;
571         storChannel->PathId = props->PathId;
572         storChannel->TargetId = props->TargetId; */
573
574         storDevice->PortNumber = deviceInfo->PortNumber;
575         /* Send it back up */
576         ret = StorVscConnectToVsp(Device);
577
578         /* deviceInfo->PortNumber = storDevice->PortNumber; */
579         deviceInfo->PathId = storDevice->PathId;
580         deviceInfo->TargetId = storDevice->TargetId;
581
582         DPRINT_DBG(STORVSC, "assigned port %u, path %u target %u\n",
583                    storDevice->PortNumber, storDevice->PathId,
584                    storDevice->TargetId);
585
586 Cleanup:
587         DPRINT_EXIT(STORVSC);
588
589         return ret;
590 }
591
592 /*
593  * StorVscOnDeviceRemove - Callback when the our device is being removed
594  */
595 static int StorVscOnDeviceRemove(struct hv_device *Device)
596 {
597         struct storvsc_device *storDevice;
598
599         DPRINT_ENTER(STORVSC);
600
601         DPRINT_INFO(STORVSC, "disabling storage device (%p)...",
602                     Device->Extension);
603
604         storDevice = ReleaseStorDevice(Device);
605
606         /*
607          * At this point, all outbound traffic should be disable. We
608          * only allow inbound traffic (responses) to proceed so that
609          * outstanding requests can be completed.
610          */
611         while (atomic_read(&storDevice->NumOutstandingRequests)) {
612                 DPRINT_INFO(STORVSC, "waiting for %d requests to complete...",
613                             atomic_read(&storDevice->NumOutstandingRequests));
614                 udelay(100);
615         }
616
617         DPRINT_INFO(STORVSC, "removing storage device (%p)...",
618                     Device->Extension);
619
620         storDevice = FinalReleaseStorDevice(Device);
621
622         DPRINT_INFO(STORVSC, "storage device (%p) safe to remove", storDevice);
623
624         /* Close the channel */
625         Device->Driver->VmbusChannelInterface.Close(Device);
626
627         FreeStorDevice(storDevice);
628
629         DPRINT_EXIT(STORVSC);
630         return 0;
631 }
632
633 int StorVscOnHostReset(struct hv_device *Device)
634 {
635         struct storvsc_device *storDevice;
636         struct storvsc_request_extension *request;
637         struct vstor_packet *vstorPacket;
638         int ret;
639
640         DPRINT_ENTER(STORVSC);
641
642         DPRINT_INFO(STORVSC, "resetting host adapter...");
643
644         storDevice = GetStorDevice(Device);
645         if (!storDevice) {
646                 DPRINT_ERR(STORVSC, "unable to get stor device..."
647                            "device being destroyed?");
648                 DPRINT_EXIT(STORVSC);
649                 return -1;
650         }
651
652         request = &storDevice->ResetRequest;
653         vstorPacket = &request->VStorPacket;
654
655         request->WaitEvent = osd_WaitEventCreate();
656         if (!request->WaitEvent) {
657                 ret = -ENOMEM;
658                 goto Cleanup;
659         }
660
661         vstorPacket->Operation = VStorOperationResetBus;
662         vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
663         vstorPacket->VmSrb.PathId = storDevice->PathId;
664
665         ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
666                                 vstorPacket,
667                                 sizeof(struct vstor_packet),
668                                 (unsigned long)&storDevice->ResetRequest,
669                                 VmbusPacketTypeDataInBand,
670                                 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
671         if (ret != 0) {
672                 DPRINT_ERR(STORVSC, "Unable to send reset packet %p ret %d",
673                            vstorPacket, ret);
674                 goto Cleanup;
675         }
676
677         /* FIXME: Add a timeout */
678         osd_WaitEventWait(request->WaitEvent);
679
680         kfree(request->WaitEvent);
681         DPRINT_INFO(STORVSC, "host adapter reset completed");
682
683         /*
684          * At this point, all outstanding requests in the adapter
685          * should have been flushed out and return to us
686          */
687
688 Cleanup:
689         PutStorDevice(Device);
690         DPRINT_EXIT(STORVSC);
691         return ret;
692 }
693
694 /*
695  * StorVscOnIORequest - Callback to initiate an I/O request
696  */
697 static int StorVscOnIORequest(struct hv_device *Device,
698                               struct hv_storvsc_request *Request)
699 {
700         struct storvsc_device *storDevice;
701         struct storvsc_request_extension *requestExtension;
702         struct vstor_packet *vstorPacket;
703         int ret = 0;
704
705         DPRINT_ENTER(STORVSC);
706
707         requestExtension =
708                 (struct storvsc_request_extension *)Request->Extension;
709         vstorPacket = &requestExtension->VStorPacket;
710         storDevice = GetStorDevice(Device);
711
712         DPRINT_DBG(STORVSC, "enter - Device %p, DeviceExt %p, Request %p, "
713                    "Extension %p", Device, storDevice, Request,
714                    requestExtension);
715
716         DPRINT_DBG(STORVSC, "req %p len %d bus %d, target %d, lun %d cdblen %d",
717                    Request, Request->DataBuffer.Length, Request->Bus,
718                    Request->TargetId, Request->LunId, Request->CdbLen);
719
720         if (!storDevice) {
721                 DPRINT_ERR(STORVSC, "unable to get stor device..."
722                            "device being destroyed?");
723                 DPRINT_EXIT(STORVSC);
724                 return -2;
725         }
726
727         /* print_hex_dump_bytes("", DUMP_PREFIX_NONE, Request->Cdb,
728          *                      Request->CdbLen); */
729
730         requestExtension->Request = Request;
731         requestExtension->Device  = Device;
732
733         memset(vstorPacket, 0 , sizeof(struct vstor_packet));
734
735         vstorPacket->Flags |= REQUEST_COMPLETION_FLAG;
736
737         vstorPacket->VmSrb.Length = sizeof(struct vmscsi_request);
738
739         vstorPacket->VmSrb.PortNumber = Request->Host;
740         vstorPacket->VmSrb.PathId = Request->Bus;
741         vstorPacket->VmSrb.TargetId = Request->TargetId;
742         vstorPacket->VmSrb.Lun = Request->LunId;
743
744         vstorPacket->VmSrb.SenseInfoLength = SENSE_BUFFER_SIZE;
745
746         /* Copy over the scsi command descriptor block */
747         vstorPacket->VmSrb.CdbLength = Request->CdbLen;
748         memcpy(&vstorPacket->VmSrb.Cdb, Request->Cdb, Request->CdbLen);
749
750         vstorPacket->VmSrb.DataIn = Request->Type;
751         vstorPacket->VmSrb.DataTransferLength = Request->DataBuffer.Length;
752
753         vstorPacket->Operation = VStorOperationExecuteSRB;
754
755         DPRINT_DBG(STORVSC, "srb - len %d port %d, path %d, target %d, "
756                    "lun %d senselen %d cdblen %d",
757                    vstorPacket->VmSrb.Length,
758                    vstorPacket->VmSrb.PortNumber,
759                    vstorPacket->VmSrb.PathId,
760                    vstorPacket->VmSrb.TargetId,
761                    vstorPacket->VmSrb.Lun,
762                    vstorPacket->VmSrb.SenseInfoLength,
763                    vstorPacket->VmSrb.CdbLength);
764
765         if (requestExtension->Request->DataBuffer.Length) {
766                 ret = Device->Driver->VmbusChannelInterface.
767                         SendPacketMultiPageBuffer(Device,
768                                 &requestExtension->Request->DataBuffer,
769                                 vstorPacket,
770                                 sizeof(struct vstor_packet),
771                                 (unsigned long)requestExtension);
772         } else {
773                 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
774                                 vstorPacket,
775                                 sizeof(struct vstor_packet),
776                                 (unsigned long)requestExtension,
777                                 VmbusPacketTypeDataInBand,
778                                 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
779         }
780
781         if (ret != 0) {
782                 DPRINT_DBG(STORVSC, "Unable to send packet %p ret %d",
783                            vstorPacket, ret);
784         }
785
786         atomic_inc(&storDevice->NumOutstandingRequests);
787
788         PutStorDevice(Device);
789
790         DPRINT_EXIT(STORVSC);
791         return ret;
792 }
793
794 /*
795  * StorVscOnCleanup - Perform any cleanup when the driver is removed
796  */
797 static void StorVscOnCleanup(struct hv_driver *Driver)
798 {
799         DPRINT_ENTER(STORVSC);
800         DPRINT_EXIT(STORVSC);
801 }
802
803 /*
804  * StorVscInitialize - Main entry point
805  */
806 int StorVscInitialize(struct hv_driver *Driver)
807 {
808         struct storvsc_driver_object *storDriver;
809
810         DPRINT_ENTER(STORVSC);
811
812         storDriver = (struct storvsc_driver_object *)Driver;
813
814         DPRINT_DBG(STORVSC, "sizeof(STORVSC_REQUEST)=%zd "
815                    "sizeof(struct storvsc_request_extension)=%zd "
816                    "sizeof(struct vstor_packet)=%zd, "
817                    "sizeof(struct vmscsi_request)=%zd",
818                    sizeof(struct hv_storvsc_request),
819                    sizeof(struct storvsc_request_extension),
820                    sizeof(struct vstor_packet),
821                    sizeof(struct vmscsi_request));
822
823         /* Make sure we are at least 2 pages since 1 page is used for control */
824         /* ASSERT(storDriver->RingBufferSize >= (PAGE_SIZE << 1)); */
825
826         Driver->name = gDriverName;
827         memcpy(&Driver->deviceType, &gStorVscDeviceType,
828                sizeof(struct hv_guid));
829
830         storDriver->RequestExtSize = sizeof(struct storvsc_request_extension);
831
832         /*
833          * Divide the ring buffer data size (which is 1 page less
834          * than the ring buffer size since that page is reserved for
835          * the ring buffer indices) by the max request size (which is
836          * VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER + struct vstor_packet + u64)
837          */
838         storDriver->MaxOutstandingRequestsPerChannel =
839                 ((storDriver->RingBufferSize - PAGE_SIZE) /
840                   ALIGN_UP(MAX_MULTIPAGE_BUFFER_PACKET +
841                            sizeof(struct vstor_packet) + sizeof(u64),
842                            sizeof(u64)));
843
844         DPRINT_INFO(STORVSC, "max io %u, currently %u\n",
845                     storDriver->MaxOutstandingRequestsPerChannel,
846                     STORVSC_MAX_IO_REQUESTS);
847
848         /* Setup the dispatch table */
849         storDriver->Base.OnDeviceAdd    = StorVscOnDeviceAdd;
850         storDriver->Base.OnDeviceRemove = StorVscOnDeviceRemove;
851         storDriver->Base.OnCleanup      = StorVscOnCleanup;
852
853         storDriver->OnIORequest         = StorVscOnIORequest;
854
855         DPRINT_EXIT(STORVSC);
856
857         return 0;
858 }