staging/hv/osd: don't reimplement ALIGN macro
[pandora-kernel.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 #include "channel.h"
32
33
34 struct storvsc_request_extension {
35         /* LIST_ENTRY ListEntry; */
36
37         struct hv_storvsc_request *request;
38         struct hv_device *device;
39
40         /* Synchronize the request/response if needed */
41         struct osd_waitevent *wait_event;
42
43         struct vstor_packet vstor_packet;
44 };
45
46 /* A storvsc device is a device object that contains a vmbus channel */
47 struct storvsc_device {
48         struct hv_device *device;
49
50         /* 0 indicates the device is being destroyed */
51         atomic_t ref_count;
52
53         atomic_t num_outstanding_req;
54
55         /*
56          * Each unique Port/Path/Target represents 1 channel ie scsi
57          * controller. In reality, the pathid, targetid is always 0
58          * and the port is set by us
59          */
60         unsigned int port_number;
61         unsigned char path_id;
62         unsigned char target_id;
63
64         /* LIST_ENTRY OutstandingRequestList; */
65         /* HANDLE OutstandingRequestLock; */
66
67         /* Used for vsc/vsp channel reset process */
68         struct storvsc_request_extension init_request;
69         struct storvsc_request_extension reset_request;
70 };
71
72
73 static const char *g_driver_name = "storvsc";
74
75 /* {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f} */
76 static const struct hv_guid gStorVscDeviceType = {
77         .data = {
78                 0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
79                 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f
80         }
81 };
82
83
84 static inline struct storvsc_device *alloc_stor_device(struct hv_device *device)
85 {
86         struct storvsc_device *stor_device;
87
88         stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL);
89         if (!stor_device)
90                 return NULL;
91
92         /* Set to 2 to allow both inbound and outbound traffics */
93         /* (ie get_stor_device() and must_get_stor_device()) to proceed. */
94         atomic_cmpxchg(&stor_device->ref_count, 0, 2);
95
96         stor_device->device = device;
97         device->Extension = stor_device;
98
99         return stor_device;
100 }
101
102 static inline void free_stor_device(struct storvsc_device *device)
103 {
104         /* ASSERT(atomic_read(&device->ref_count) == 0); */
105         kfree(device);
106 }
107
108 /* Get the stordevice object iff exists and its refcount > 1 */
109 static inline struct storvsc_device *get_stor_device(struct hv_device *device)
110 {
111         struct storvsc_device *stor_device;
112
113         stor_device = (struct storvsc_device *)device->Extension;
114         if (stor_device && atomic_read(&stor_device->ref_count) > 1)
115                 atomic_inc(&stor_device->ref_count);
116         else
117                 stor_device = NULL;
118
119         return stor_device;
120 }
121
122 /* Get the stordevice object iff exists and its refcount > 0 */
123 static inline struct storvsc_device *must_get_stor_device(
124                                         struct hv_device *device)
125 {
126         struct storvsc_device *stor_device;
127
128         stor_device = (struct storvsc_device *)device->Extension;
129         if (stor_device && atomic_read(&stor_device->ref_count))
130                 atomic_inc(&stor_device->ref_count);
131         else
132                 stor_device = NULL;
133
134         return stor_device;
135 }
136
137 static inline void put_stor_device(struct hv_device *device)
138 {
139         struct storvsc_device *stor_device;
140
141         stor_device = (struct storvsc_device *)device->Extension;
142         /* ASSERT(stor_device); */
143
144         atomic_dec(&stor_device->ref_count);
145         /* ASSERT(atomic_read(&stor_device->ref_count)); */
146 }
147
148 /* Drop ref count to 1 to effectively disable get_stor_device() */
149 static inline struct storvsc_device *release_stor_device(
150                                         struct hv_device *device)
151 {
152         struct storvsc_device *stor_device;
153
154         stor_device = (struct storvsc_device *)device->Extension;
155         /* ASSERT(stor_device); */
156
157         /* Busy wait until the ref drop to 2, then set it to 1 */
158         while (atomic_cmpxchg(&stor_device->ref_count, 2, 1) != 2)
159                 udelay(100);
160
161         return stor_device;
162 }
163
164 /* Drop ref count to 0. No one can use stor_device object. */
165 static inline struct storvsc_device *final_release_stor_device(
166                         struct hv_device *device)
167 {
168         struct storvsc_device *stor_device;
169
170         stor_device = (struct storvsc_device *)device->Extension;
171         /* ASSERT(stor_device); */
172
173         /* Busy wait until the ref drop to 1, then set it to 0 */
174         while (atomic_cmpxchg(&stor_device->ref_count, 1, 0) != 1)
175                 udelay(100);
176
177         device->Extension = NULL;
178         return stor_device;
179 }
180
181 static int stor_vsc_channel_init(struct hv_device *device)
182 {
183         struct storvsc_device *stor_device;
184         struct storvsc_request_extension *request;
185         struct vstor_packet *vstor_packet;
186         int ret;
187
188         stor_device = get_stor_device(device);
189         if (!stor_device) {
190                 DPRINT_ERR(STORVSC, "unable to get stor device..."
191                            "device being destroyed?");
192                 return -1;
193         }
194
195         request = &stor_device->init_request;
196         vstor_packet = &request->vstor_packet;
197
198         /*
199          * Now, initiate the vsc/vsp initialization protocol on the open
200          * channel
201          */
202         memset(request, 0, sizeof(struct storvsc_request_extension));
203         request->wait_event = osd_waitevent_create();
204         if (!request->wait_event) {
205                 ret = -ENOMEM;
206                 goto nomem;
207         }
208
209         vstor_packet->operation = VSTOR_OPERATION_BEGIN_INITIALIZATION;
210         vstor_packet->flags = REQUEST_COMPLETION_FLAG;
211
212         /*SpinlockAcquire(gDriverExt.packetListLock);
213         INSERT_TAIL_LIST(&gDriverExt.packetList, &packet->listEntry.entry);
214         SpinlockRelease(gDriverExt.packetListLock);*/
215
216         DPRINT_INFO(STORVSC, "BEGIN_INITIALIZATION_OPERATION...");
217
218         ret = vmbus_sendpacket(device->channel, vstor_packet,
219                                sizeof(struct vstor_packet),
220                                (unsigned long)request,
221                                VmbusPacketTypeDataInBand,
222                                VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
223         if (ret != 0) {
224                 DPRINT_ERR(STORVSC,
225                            "unable to send BEGIN_INITIALIZATION_OPERATION");
226                 goto Cleanup;
227         }
228
229         osd_waitevent_wait(request->wait_event);
230
231         if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
232             vstor_packet->status != 0) {
233                 DPRINT_ERR(STORVSC, "BEGIN_INITIALIZATION_OPERATION failed "
234                            "(op %d status 0x%x)",
235                            vstor_packet->operation, vstor_packet->status);
236                 goto Cleanup;
237         }
238
239         DPRINT_INFO(STORVSC, "QUERY_PROTOCOL_VERSION_OPERATION...");
240
241         /* reuse the packet for version range supported */
242         memset(vstor_packet, 0, sizeof(struct vstor_packet));
243         vstor_packet->operation = VSTOR_OPERATION_QUERY_PROTOCOL_VERSION;
244         vstor_packet->flags = REQUEST_COMPLETION_FLAG;
245
246         vstor_packet->version.major_minor = VMSTOR_PROTOCOL_VERSION_CURRENT;
247         FILL_VMSTOR_REVISION(vstor_packet->version.revision);
248
249         ret = vmbus_sendpacket(device->channel, vstor_packet,
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_waitevent_wait(request->wait_event);
261
262         /* TODO: Check returned version */
263         if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
264             vstor_packet->status != 0) {
265                 DPRINT_ERR(STORVSC, "QUERY_PROTOCOL_VERSION_OPERATION failed "
266                            "(op %d status 0x%x)",
267                            vstor_packet->operation, vstor_packet->status);
268                 goto Cleanup;
269         }
270
271         /* Query channel properties */
272         DPRINT_INFO(STORVSC, "QUERY_PROPERTIES_OPERATION...");
273
274         memset(vstor_packet, 0, sizeof(struct vstor_packet));
275         vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES;
276         vstor_packet->flags = REQUEST_COMPLETION_FLAG;
277         vstor_packet->storage_channel_properties.port_number =
278                                         stor_device->port_number;
279
280         ret = vmbus_sendpacket(device->channel, vstor_packet,
281                                sizeof(struct vstor_packet),
282                                (unsigned long)request,
283                                VmbusPacketTypeDataInBand,
284                                VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
285
286         if (ret != 0) {
287                 DPRINT_ERR(STORVSC,
288                            "unable to send QUERY_PROPERTIES_OPERATION");
289                 goto Cleanup;
290         }
291
292         osd_waitevent_wait(request->wait_event);
293
294         /* TODO: Check returned version */
295         if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
296             vstor_packet->status != 0) {
297                 DPRINT_ERR(STORVSC, "QUERY_PROPERTIES_OPERATION failed "
298                            "(op %d status 0x%x)",
299                            vstor_packet->operation, vstor_packet->status);
300                 goto Cleanup;
301         }
302
303         stor_device->path_id = vstor_packet->storage_channel_properties.path_id;
304         stor_device->target_id
305                 = vstor_packet->storage_channel_properties.target_id;
306
307         DPRINT_DBG(STORVSC, "channel flag 0x%x, max xfer len 0x%x",
308                    vstor_packet->storage_channel_properties.flags,
309                    vstor_packet->storage_channel_properties.max_transfer_bytes);
310
311         DPRINT_INFO(STORVSC, "END_INITIALIZATION_OPERATION...");
312
313         memset(vstor_packet, 0, sizeof(struct vstor_packet));
314         vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION;
315         vstor_packet->flags = REQUEST_COMPLETION_FLAG;
316
317         ret = vmbus_sendpacket(device->channel, vstor_packet,
318                                sizeof(struct vstor_packet),
319                                (unsigned long)request,
320                                VmbusPacketTypeDataInBand,
321                                VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
322
323         if (ret != 0) {
324                 DPRINT_ERR(STORVSC,
325                            "unable to send END_INITIALIZATION_OPERATION");
326                 goto Cleanup;
327         }
328
329         osd_waitevent_wait(request->wait_event);
330
331         if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
332             vstor_packet->status != 0) {
333                 DPRINT_ERR(STORVSC, "END_INITIALIZATION_OPERATION failed "
334                            "(op %d status 0x%x)",
335                            vstor_packet->operation, vstor_packet->status);
336                 goto Cleanup;
337         }
338
339         DPRINT_INFO(STORVSC, "**** storage channel up and running!! ****");
340
341 Cleanup:
342         kfree(request->wait_event);
343         request->wait_event = NULL;
344 nomem:
345         put_stor_device(device);
346         return ret;
347 }
348
349 static void stor_vsc_on_io_completion(struct hv_device *device,
350                                   struct vstor_packet *vstor_packet,
351                                   struct storvsc_request_extension *request_ext)
352 {
353         struct hv_storvsc_request *request;
354         struct storvsc_device *stor_device;
355
356         stor_device = must_get_stor_device(device);
357         if (!stor_device) {
358                 DPRINT_ERR(STORVSC, "unable to get stor device..."
359                            "device being destroyed?");
360                 return;
361         }
362
363         DPRINT_DBG(STORVSC, "IO_COMPLETE_OPERATION - request extension %p "
364                    "completed bytes xfer %u", request_ext,
365                    vstor_packet->vm_srb.data_transfer_length);
366
367         /* ASSERT(request_ext != NULL); */
368         /* ASSERT(request_ext->request != NULL); */
369
370         request = request_ext->request;
371
372         /* ASSERT(request->OnIOCompletion != NULL); */
373
374         /* Copy over the status...etc */
375         request->status = vstor_packet->vm_srb.scsi_status;
376
377         if (request->status != 0 || vstor_packet->vm_srb.srb_status != 1) {
378                 DPRINT_WARN(STORVSC,
379                             "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
380                             request->cdb[0], vstor_packet->vm_srb.scsi_status,
381                             vstor_packet->vm_srb.srb_status);
382         }
383
384         if ((request->status & 0xFF) == 0x02) {
385                 /* CHECK_CONDITION */
386                 if (vstor_packet->vm_srb.srb_status & 0x80) {
387                         /* autosense data available */
388                         DPRINT_WARN(STORVSC, "storvsc pkt %p autosense data "
389                                     "valid - len %d\n", request_ext,
390                                     vstor_packet->vm_srb.sense_info_length);
391
392                         /* ASSERT(vstor_packet->vm_srb.sense_info_length <= */
393                         /*      request->SenseBufferSize); */
394                         memcpy(request->sense_buffer,
395                                vstor_packet->vm_srb.sense_data,
396                                vstor_packet->vm_srb.sense_info_length);
397
398                         request->sense_buffer_size =
399                                         vstor_packet->vm_srb.sense_info_length;
400                 }
401         }
402
403         /* TODO: */
404         request->bytes_xfer = vstor_packet->vm_srb.data_transfer_length;
405
406         request->on_io_completion(request);
407
408         atomic_dec(&stor_device->num_outstanding_req);
409
410         put_stor_device(device);
411 }
412
413 static void stor_vsc_on_receive(struct hv_device *device,
414                              struct vstor_packet *vstor_packet,
415                              struct storvsc_request_extension *request_ext)
416 {
417         switch (vstor_packet->operation) {
418         case VSTOR_OPERATION_COMPLETE_IO:
419                 DPRINT_DBG(STORVSC, "IO_COMPLETE_OPERATION");
420                 stor_vsc_on_io_completion(device, vstor_packet, request_ext);
421                 break;
422         case VSTOR_OPERATION_REMOVE_DEVICE:
423                 DPRINT_INFO(STORVSC, "REMOVE_DEVICE_OPERATION");
424                 /* TODO: */
425                 break;
426
427         default:
428                 DPRINT_INFO(STORVSC, "Unknown operation received - %d",
429                             vstor_packet->operation);
430                 break;
431         }
432 }
433
434 static void stor_vsc_on_channel_callback(void *context)
435 {
436         struct hv_device *device = (struct hv_device *)context;
437         struct storvsc_device *stor_device;
438         u32 bytes_recvd;
439         u64 request_id;
440         unsigned char packet[ALIGN(sizeof(struct vstor_packet), 8)];
441         struct storvsc_request_extension *request;
442         int ret;
443
444         /* ASSERT(device); */
445
446         stor_device = must_get_stor_device(device);
447         if (!stor_device) {
448                 DPRINT_ERR(STORVSC, "unable to get stor device..."
449                            "device being destroyed?");
450                 return;
451         }
452
453         do {
454                 ret = vmbus_recvpacket(device->channel, packet,
455                                        ALIGN(sizeof(struct vstor_packet), 8),
456                                        &bytes_recvd, &request_id);
457                 if (ret == 0 && bytes_recvd > 0) {
458                         DPRINT_DBG(STORVSC, "receive %d bytes - tid %llx",
459                                    bytes_recvd, request_id);
460
461                         /* ASSERT(bytes_recvd ==
462                                         sizeof(struct vstor_packet)); */
463
464                         request = (struct storvsc_request_extension *)
465                                         (unsigned long)request_id;
466                         /* ASSERT(request);c */
467
468                         /* if (vstor_packet.Flags & SYNTHETIC_FLAG) */
469                         if ((request == &stor_device->init_request) ||
470                             (request == &stor_device->reset_request)) {
471                                 /* DPRINT_INFO(STORVSC,
472                                  *             "reset completion - operation "
473                                  *             "%u status %u",
474                                  *             vstor_packet.Operation,
475                                  *             vstor_packet.Status); */
476
477                                 memcpy(&request->vstor_packet, packet,
478                                        sizeof(struct vstor_packet));
479
480                                 osd_waitevent_set(request->wait_event);
481                         } else {
482                                 stor_vsc_on_receive(device,
483                                                 (struct vstor_packet *)packet,
484                                                 request);
485                         }
486                 } else {
487                         /* DPRINT_DBG(STORVSC, "nothing else to read..."); */
488                         break;
489                 }
490         } while (1);
491
492         put_stor_device(device);
493         return;
494 }
495
496 static int stor_vsc_connect_to_vsp(struct hv_device *device)
497 {
498         struct vmstorage_channel_properties props;
499         struct storvsc_driver_object *stor_driver;
500         int ret;
501
502         stor_driver = (struct storvsc_driver_object *)device->Driver;
503         memset(&props, 0, sizeof(struct vmstorage_channel_properties));
504
505         /* Open the channel */
506         ret = vmbus_open(device->channel,
507                          stor_driver->ring_buffer_size,
508                          stor_driver->ring_buffer_size,
509                          (void *)&props,
510                          sizeof(struct vmstorage_channel_properties),
511                          stor_vsc_on_channel_callback, device);
512
513         DPRINT_DBG(STORVSC, "storage props: path id %d, tgt id %d, max xfer %d",
514                    props.path_id, props.target_id, props.max_transfer_bytes);
515
516         if (ret != 0) {
517                 DPRINT_ERR(STORVSC, "unable to open channel: %d", ret);
518                 return -1;
519         }
520
521         ret = stor_vsc_channel_init(device);
522
523         return ret;
524 }
525
526 /*
527  * stor_vsc_on_device_add - Callback when the device belonging to this driver
528  * is added
529  */
530 static int stor_vsc_on_device_add(struct hv_device *device,
531                                         void *additional_info)
532 {
533         struct storvsc_device *stor_device;
534         /* struct vmstorage_channel_properties *props; */
535         struct storvsc_device_info *device_info;
536         int ret = 0;
537
538         device_info = (struct storvsc_device_info *)additional_info;
539         stor_device = alloc_stor_device(device);
540         if (!stor_device) {
541                 ret = -1;
542                 goto Cleanup;
543         }
544
545         /* Save the channel properties to our storvsc channel */
546         /* props = (struct vmstorage_channel_properties *)
547          *              channel->offerMsg.Offer.u.Standard.UserDefined; */
548
549         /* FIXME: */
550         /*
551          * If we support more than 1 scsi channel, we need to set the
552          * port number here to the scsi channel but how do we get the
553          * scsi channel prior to the bus scan
554          */
555
556         /* storChannel->PortNumber = 0;
557         storChannel->PathId = props->PathId;
558         storChannel->TargetId = props->TargetId; */
559
560         stor_device->port_number = device_info->port_number;
561         /* Send it back up */
562         ret = stor_vsc_connect_to_vsp(device);
563
564         /* device_info->PortNumber = stor_device->PortNumber; */
565         device_info->path_id = stor_device->path_id;
566         device_info->target_id = stor_device->target_id;
567
568         DPRINT_DBG(STORVSC, "assigned port %u, path %u target %u\n",
569                    stor_device->port_number, stor_device->path_id,
570                    stor_device->target_id);
571
572 Cleanup:
573         return ret;
574 }
575
576 /*
577  * stor_vsc_on_device_remove - Callback when the our device is being removed
578  */
579 static int stor_vsc_on_device_remove(struct hv_device *device)
580 {
581         struct storvsc_device *stor_device;
582
583         DPRINT_INFO(STORVSC, "disabling storage device (%p)...",
584                     device->Extension);
585
586         stor_device = release_stor_device(device);
587
588         /*
589          * At this point, all outbound traffic should be disable. We
590          * only allow inbound traffic (responses) to proceed so that
591          * outstanding requests can be completed.
592          */
593         while (atomic_read(&stor_device->num_outstanding_req)) {
594                 DPRINT_INFO(STORVSC, "waiting for %d requests to complete...",
595                             atomic_read(&stor_device->num_outstanding_req));
596                 udelay(100);
597         }
598
599         DPRINT_INFO(STORVSC, "removing storage device (%p)...",
600                     device->Extension);
601
602         stor_device = final_release_stor_device(device);
603
604         DPRINT_INFO(STORVSC, "storage device (%p) safe to remove", stor_device);
605
606         /* Close the channel */
607         vmbus_close(device->channel);
608
609         free_stor_device(stor_device);
610         return 0;
611 }
612
613 int stor_vsc_on_host_reset(struct hv_device *device)
614 {
615         struct storvsc_device *stor_device;
616         struct storvsc_request_extension *request;
617         struct vstor_packet *vstor_packet;
618         int ret;
619
620         DPRINT_INFO(STORVSC, "resetting host adapter...");
621
622         stor_device = get_stor_device(device);
623         if (!stor_device) {
624                 DPRINT_ERR(STORVSC, "unable to get stor device..."
625                            "device being destroyed?");
626                 return -1;
627         }
628
629         request = &stor_device->reset_request;
630         vstor_packet = &request->vstor_packet;
631
632         request->wait_event = osd_waitevent_create();
633         if (!request->wait_event) {
634                 ret = -ENOMEM;
635                 goto Cleanup;
636         }
637
638         vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
639         vstor_packet->flags = REQUEST_COMPLETION_FLAG;
640         vstor_packet->vm_srb.path_id = stor_device->path_id;
641
642         ret = vmbus_sendpacket(device->channel, vstor_packet,
643                                sizeof(struct vstor_packet),
644                                (unsigned long)&stor_device->reset_request,
645                                VmbusPacketTypeDataInBand,
646                                VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
647         if (ret != 0) {
648                 DPRINT_ERR(STORVSC, "Unable to send reset packet %p ret %d",
649                            vstor_packet, ret);
650                 goto Cleanup;
651         }
652
653         /* FIXME: Add a timeout */
654         osd_waitevent_wait(request->wait_event);
655
656         kfree(request->wait_event);
657         DPRINT_INFO(STORVSC, "host adapter reset completed");
658
659         /*
660          * At this point, all outstanding requests in the adapter
661          * should have been flushed out and return to us
662          */
663
664 Cleanup:
665         put_stor_device(device);
666         return ret;
667 }
668
669 /*
670  * stor_vsc_on_io_request - Callback to initiate an I/O request
671  */
672 static int stor_vsc_on_io_request(struct hv_device *device,
673                               struct hv_storvsc_request *request)
674 {
675         struct storvsc_device *stor_device;
676         struct storvsc_request_extension *request_extension;
677         struct vstor_packet *vstor_packet;
678         int ret = 0;
679
680         request_extension =
681                 (struct storvsc_request_extension *)request->extension;
682         vstor_packet = &request_extension->vstor_packet;
683         stor_device = get_stor_device(device);
684
685         DPRINT_DBG(STORVSC, "enter - Device %p, DeviceExt %p, Request %p, "
686                    "Extension %p", device, stor_device, request,
687                    request_extension);
688
689         DPRINT_DBG(STORVSC, "req %p len %d bus %d, target %d, lun %d cdblen %d",
690                    request, request->data_buffer.Length, request->bus,
691                    request->target_id, request->lun_id, request->cdb_len);
692
693         if (!stor_device) {
694                 DPRINT_ERR(STORVSC, "unable to get stor device..."
695                            "device being destroyed?");
696                 return -2;
697         }
698
699         /* print_hex_dump_bytes("", DUMP_PREFIX_NONE, request->Cdb,
700          *                      request->CdbLen); */
701
702         request_extension->request = request;
703         request_extension->device  = device;
704
705         memset(vstor_packet, 0 , sizeof(struct vstor_packet));
706
707         vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
708
709         vstor_packet->vm_srb.length = sizeof(struct vmscsi_request);
710
711         vstor_packet->vm_srb.port_number = request->host;
712         vstor_packet->vm_srb.path_id = request->bus;
713         vstor_packet->vm_srb.target_id = request->target_id;
714         vstor_packet->vm_srb.lun = request->lun_id;
715
716         vstor_packet->vm_srb.sense_info_length = SENSE_BUFFER_SIZE;
717
718         /* Copy over the scsi command descriptor block */
719         vstor_packet->vm_srb.cdb_length = request->cdb_len;
720         memcpy(&vstor_packet->vm_srb.cdb, request->cdb, request->cdb_len);
721
722         vstor_packet->vm_srb.data_in = request->type;
723         vstor_packet->vm_srb.data_transfer_length = request->data_buffer.Length;
724
725         vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;
726
727         DPRINT_DBG(STORVSC, "srb - len %d port %d, path %d, target %d, "
728                    "lun %d senselen %d cdblen %d",
729                    vstor_packet->vm_srb.length,
730                    vstor_packet->vm_srb.port_number,
731                    vstor_packet->vm_srb.path_id,
732                    vstor_packet->vm_srb.target_id,
733                    vstor_packet->vm_srb.lun,
734                    vstor_packet->vm_srb.sense_info_length,
735                    vstor_packet->vm_srb.cdb_length);
736
737         if (request_extension->request->data_buffer.Length) {
738                 ret = vmbus_sendpacket_multipagebuffer(device->channel,
739                                 &request_extension->request->data_buffer,
740                                 vstor_packet,
741                                 sizeof(struct vstor_packet),
742                                 (unsigned long)request_extension);
743         } else {
744                 ret = vmbus_sendpacket(device->channel, vstor_packet,
745                                        sizeof(struct vstor_packet),
746                                        (unsigned long)request_extension,
747                                        VmbusPacketTypeDataInBand,
748                                        VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
749         }
750
751         if (ret != 0) {
752                 DPRINT_DBG(STORVSC, "Unable to send packet %p ret %d",
753                            vstor_packet, ret);
754         }
755
756         atomic_inc(&stor_device->num_outstanding_req);
757
758         put_stor_device(device);
759         return ret;
760 }
761
762 /*
763  * stor_vsc_on_cleanup - Perform any cleanup when the driver is removed
764  */
765 static void stor_vsc_on_cleanup(struct hv_driver *driver)
766 {
767 }
768
769 /*
770  * stor_vsc_initialize - Main entry point
771  */
772 int stor_vsc_initialize(struct hv_driver *driver)
773 {
774         struct storvsc_driver_object *stor_driver;
775
776         stor_driver = (struct storvsc_driver_object *)driver;
777
778         DPRINT_DBG(STORVSC, "sizeof(STORVSC_REQUEST)=%zd "
779                    "sizeof(struct storvsc_request_extension)=%zd "
780                    "sizeof(struct vstor_packet)=%zd, "
781                    "sizeof(struct vmscsi_request)=%zd",
782                    sizeof(struct hv_storvsc_request),
783                    sizeof(struct storvsc_request_extension),
784                    sizeof(struct vstor_packet),
785                    sizeof(struct vmscsi_request));
786
787         /* Make sure we are at least 2 pages since 1 page is used for control */
788         /* ASSERT(stor_driver->RingBufferSize >= (PAGE_SIZE << 1)); */
789
790         driver->name = g_driver_name;
791         memcpy(&driver->deviceType, &gStorVscDeviceType,
792                sizeof(struct hv_guid));
793
794         stor_driver->request_ext_size =
795                         sizeof(struct storvsc_request_extension);
796
797         /*
798          * Divide the ring buffer data size (which is 1 page less
799          * than the ring buffer size since that page is reserved for
800          * the ring buffer indices) by the max request size (which is
801          * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
802          */
803         stor_driver->max_outstanding_req_per_channel =
804                 ((stor_driver->ring_buffer_size - PAGE_SIZE) /
805                   ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
806                            sizeof(struct vstor_packet) + sizeof(u64),
807                            sizeof(u64)));
808
809         DPRINT_INFO(STORVSC, "max io %u, currently %u\n",
810                     stor_driver->max_outstanding_req_per_channel,
811                     STORVSC_MAX_IO_REQUESTS);
812
813         /* Setup the dispatch table */
814         stor_driver->base.OnDeviceAdd   = stor_vsc_on_device_add;
815         stor_driver->base.OnDeviceRemove        = stor_vsc_on_device_remove;
816         stor_driver->base.OnCleanup     = stor_vsc_on_cleanup;
817
818         stor_driver->on_io_request      = stor_vsc_on_io_request;
819
820         return 0;
821 }