]> Pileus Git - ~andy/linux/blob - drivers/staging/hv/storvsc_drv.c
Staging: hv: Get rid of the forward declaration of storvsc_remove()
[~andy/linux] / drivers / staging / hv / storvsc_drv.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  *   K. Y. Srinivasan <kys@microsoft.com>
21  */
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/module.h>
25 #include <linux/device.h>
26 #include <linux/blkdev.h>
27 #include <scsi/scsi.h>
28 #include <scsi/scsi_cmnd.h>
29 #include <scsi/scsi_host.h>
30 #include <scsi/scsi_device.h>
31 #include <scsi/scsi_tcq.h>
32 #include <scsi/scsi_eh.h>
33 #include <scsi/scsi_devinfo.h>
34 #include <scsi/scsi_dbg.h>
35 #include "hv_api.h"
36 #include "logging.h"
37 #include "version_info.h"
38 #include "vmbus.h"
39 #include "storvsc_api.h"
40 #include "vstorage.h"
41 #include "channel.h"
42
43
44 static const char *driver_name = "storvsc";
45
46 /* {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f} */
47 static const struct hv_guid gStorVscDeviceType = {
48         .data = {
49                 0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
50                 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f
51         }
52 };
53
54 struct hv_host_device {
55         struct hv_device *dev;
56         struct kmem_cache *request_pool;
57         unsigned int port;
58         unsigned char path;
59         unsigned char target;
60 };
61
62 struct storvsc_cmd_request {
63         struct list_head entry;
64         struct scsi_cmnd *cmd;
65
66         unsigned int bounce_sgl_count;
67         struct scatterlist *bounce_sgl;
68
69         struct hv_storvsc_request request;
70 };
71
72
73 /*
74  * stor_vsc_initialize - Main entry point
75  */
76 static int stor_vsc_initialize(struct hv_driver *driver)
77 {
78         struct storvsc_driver *stor_driver;
79
80         stor_driver = hvdr_to_stordr(driver);
81
82
83         /* Make sure we are at least 2 pages since 1 page is used for control */
84
85         driver->name = driver_name;
86         memcpy(&driver->dev_type, &gStorVscDeviceType,
87                sizeof(struct hv_guid));
88
89
90         /*
91          * Divide the ring buffer data size (which is 1 page less
92          * than the ring buffer size since that page is reserved for
93          * the ring buffer indices) by the max request size (which is
94          * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
95          */
96         stor_driver->max_outstanding_req_per_channel =
97                 ((stor_driver->ring_buffer_size - PAGE_SIZE) /
98                   ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
99                            sizeof(struct vstor_packet) + sizeof(u64),
100                            sizeof(u64)));
101
102         DPRINT_INFO(STORVSC, "max io %u, currently %u\n",
103                     stor_driver->max_outstanding_req_per_channel,
104                     STORVSC_MAX_IO_REQUESTS);
105
106         /* Setup the dispatch table */
107         stor_driver->base.dev_add       = storvsc_dev_add;
108         stor_driver->base.dev_rm        = storvsc_dev_remove;
109
110         stor_driver->on_io_request      = storvsc_do_io;
111
112         return 0;
113 }
114
115 static int storvsc_device_alloc(struct scsi_device *sdevice)
116 {
117         /*
118          * This enables luns to be located sparsely. Otherwise, we may not
119          * discovered them.
120          */
121         sdevice->sdev_bflags |= BLIST_SPARSELUN | BLIST_LARGELUN;
122         return 0;
123 }
124
125 static int storvsc_merge_bvec(struct request_queue *q,
126                               struct bvec_merge_data *bmd, struct bio_vec *bvec)
127 {
128         /* checking done by caller. */
129         return bvec->bv_len;
130 }
131
132 static int storvsc_device_configure(struct scsi_device *sdevice)
133 {
134         scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG,
135                                 STORVSC_MAX_IO_REQUESTS);
136
137         DPRINT_INFO(STORVSC_DRV, "sdev (%p) - setting max segment size to %ld",
138                     sdevice, PAGE_SIZE);
139         blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
140
141         DPRINT_INFO(STORVSC_DRV, "sdev (%p) - adding merge bio vec routine",
142                     sdevice);
143         blk_queue_merge_bvec(sdevice->request_queue, storvsc_merge_bvec);
144
145         blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
146         /* sdevice->timeout = (2000 * HZ);//(75 * HZ); */
147
148         return 0;
149 }
150
151 static void destroy_bounce_buffer(struct scatterlist *sgl,
152                                   unsigned int sg_count)
153 {
154         int i;
155         struct page *page_buf;
156
157         for (i = 0; i < sg_count; i++) {
158                 page_buf = sg_page((&sgl[i]));
159                 if (page_buf != NULL)
160                         __free_page(page_buf);
161         }
162
163         kfree(sgl);
164 }
165
166 static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
167 {
168         int i;
169
170         /* No need to check */
171         if (sg_count < 2)
172                 return -1;
173
174         /* We have at least 2 sg entries */
175         for (i = 0; i < sg_count; i++) {
176                 if (i == 0) {
177                         /* make sure 1st one does not have hole */
178                         if (sgl[i].offset + sgl[i].length != PAGE_SIZE)
179                                 return i;
180                 } else if (i == sg_count - 1) {
181                         /* make sure last one does not have hole */
182                         if (sgl[i].offset != 0)
183                                 return i;
184                 } else {
185                         /* make sure no hole in the middle */
186                         if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0)
187                                 return i;
188                 }
189         }
190         return -1;
191 }
192
193 static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
194                                                 unsigned int sg_count,
195                                                 unsigned int len)
196 {
197         int i;
198         int num_pages;
199         struct scatterlist *bounce_sgl;
200         struct page *page_buf;
201
202         num_pages = ALIGN(len, PAGE_SIZE) >> PAGE_SHIFT;
203
204         bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC);
205         if (!bounce_sgl)
206                 return NULL;
207
208         for (i = 0; i < num_pages; i++) {
209                 page_buf = alloc_page(GFP_ATOMIC);
210                 if (!page_buf)
211                         goto cleanup;
212                 sg_set_page(&bounce_sgl[i], page_buf, 0, 0);
213         }
214
215         return bounce_sgl;
216
217 cleanup:
218         destroy_bounce_buffer(bounce_sgl, num_pages);
219         return NULL;
220 }
221
222
223 /* Assume the original sgl has enough room */
224 static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
225                                             struct scatterlist *bounce_sgl,
226                                             unsigned int orig_sgl_count)
227 {
228         int i;
229         int j = 0;
230         unsigned long src, dest;
231         unsigned int srclen, destlen, copylen;
232         unsigned int total_copied = 0;
233         unsigned long bounce_addr = 0;
234         unsigned long dest_addr = 0;
235         unsigned long flags;
236
237         local_irq_save(flags);
238
239         for (i = 0; i < orig_sgl_count; i++) {
240                 dest_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
241                                         KM_IRQ0) + orig_sgl[i].offset;
242                 dest = dest_addr;
243                 destlen = orig_sgl[i].length;
244
245                 if (bounce_addr == 0)
246                         bounce_addr =
247                         (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])),
248                                                         KM_IRQ0);
249
250                 while (destlen) {
251                         src = bounce_addr + bounce_sgl[j].offset;
252                         srclen = bounce_sgl[j].length - bounce_sgl[j].offset;
253
254                         copylen = min(srclen, destlen);
255                         memcpy((void *)dest, (void *)src, copylen);
256
257                         total_copied += copylen;
258                         bounce_sgl[j].offset += copylen;
259                         destlen -= copylen;
260                         dest += copylen;
261
262                         if (bounce_sgl[j].offset == bounce_sgl[j].length) {
263                                 /* full */
264                                 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
265                                 j++;
266
267                                 /* if we need to use another bounce buffer */
268                                 if (destlen || i != orig_sgl_count - 1)
269                                         bounce_addr =
270                                         (unsigned long)kmap_atomic(
271                                         sg_page((&bounce_sgl[j])), KM_IRQ0);
272                         } else if (destlen == 0 && i == orig_sgl_count - 1) {
273                                 /* unmap the last bounce that is < PAGE_SIZE */
274                                 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
275                         }
276                 }
277
278                 kunmap_atomic((void *)(dest_addr - orig_sgl[i].offset),
279                               KM_IRQ0);
280         }
281
282         local_irq_restore(flags);
283
284         return total_copied;
285 }
286
287
288 /* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
289 static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
290                                           struct scatterlist *bounce_sgl,
291                                           unsigned int orig_sgl_count)
292 {
293         int i;
294         int j = 0;
295         unsigned long src, dest;
296         unsigned int srclen, destlen, copylen;
297         unsigned int total_copied = 0;
298         unsigned long bounce_addr = 0;
299         unsigned long src_addr = 0;
300         unsigned long flags;
301
302         local_irq_save(flags);
303
304         for (i = 0; i < orig_sgl_count; i++) {
305                 src_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
306                                 KM_IRQ0) + orig_sgl[i].offset;
307                 src = src_addr;
308                 srclen = orig_sgl[i].length;
309
310                 if (bounce_addr == 0)
311                         bounce_addr =
312                         (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])),
313                                                 KM_IRQ0);
314
315                 while (srclen) {
316                         /* assume bounce offset always == 0 */
317                         dest = bounce_addr + bounce_sgl[j].length;
318                         destlen = PAGE_SIZE - bounce_sgl[j].length;
319
320                         copylen = min(srclen, destlen);
321                         memcpy((void *)dest, (void *)src, copylen);
322
323                         total_copied += copylen;
324                         bounce_sgl[j].length += copylen;
325                         srclen -= copylen;
326                         src += copylen;
327
328                         if (bounce_sgl[j].length == PAGE_SIZE) {
329                                 /* full..move to next entry */
330                                 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
331                                 j++;
332
333                                 /* if we need to use another bounce buffer */
334                                 if (srclen || i != orig_sgl_count - 1)
335                                         bounce_addr =
336                                         (unsigned long)kmap_atomic(
337                                         sg_page((&bounce_sgl[j])), KM_IRQ0);
338
339                         } else if (srclen == 0 && i == orig_sgl_count - 1) {
340                                 /* unmap the last bounce that is < PAGE_SIZE */
341                                 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
342                         }
343                 }
344
345                 kunmap_atomic((void *)(src_addr - orig_sgl[i].offset), KM_IRQ0);
346         }
347
348         local_irq_restore(flags);
349
350         return total_copied;
351 }
352
353
354 /*
355  * storvsc_remove - Callback when our device is removed
356  */
357 static int storvsc_remove(struct hv_device *dev)
358 {
359         struct storvsc_driver *storvsc_drv_obj =
360                          drv_to_stordrv(dev->device.driver);
361         struct Scsi_Host *host = dev_get_drvdata(&dev->device);
362         struct hv_host_device *host_dev =
363                         (struct hv_host_device *)host->hostdata;
364
365         /*
366          * Call to the vsc driver to let it know that the device is being
367          * removed
368          */
369         storvsc_drv_obj->base.dev_rm(dev);
370
371         if (host_dev->request_pool) {
372                 kmem_cache_destroy(host_dev->request_pool);
373                 host_dev->request_pool = NULL;
374         }
375
376         DPRINT_INFO(STORVSC, "removing host adapter (%p)...", host);
377         scsi_remove_host(host);
378
379         DPRINT_INFO(STORVSC, "releasing host adapter (%p)...", host);
380         scsi_host_put(host);
381         return 0;
382 }
383
384 /* Static decl */
385 static int storvsc_probe(struct hv_device *dev);
386 static int storvsc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *scmnd);
387 static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd);
388
389 static int storvsc_get_chs(struct scsi_device *sdev, struct block_device *bdev,
390                            sector_t capacity, int *info);
391
392
393 static int storvsc_ringbuffer_size = STORVSC_RING_BUFFER_SIZE;
394 module_param(storvsc_ringbuffer_size, int, S_IRUGO);
395 MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)");
396
397 /* The one and only one */
398 static struct storvsc_driver g_storvsc_drv;
399
400 /* Scsi driver */
401 static struct scsi_host_template scsi_driver = {
402         .module =               THIS_MODULE,
403         .name =                 "storvsc_host_t",
404         .bios_param =           storvsc_get_chs,
405         .queuecommand =         storvsc_queuecommand,
406         .eh_host_reset_handler =        storvsc_host_reset_handler,
407         .slave_alloc =          storvsc_device_alloc,
408         .slave_configure =      storvsc_device_configure,
409         .cmd_per_lun =          1,
410         /* 64 max_queue * 1 target */
411         .can_queue =            STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS,
412         .this_id =              -1,
413         /* no use setting to 0 since ll_blk_rw reset it to 1 */
414         /* currently 32 */
415         .sg_tablesize =         MAX_MULTIPAGE_BUFFER_COUNT,
416         /*
417          * ENABLE_CLUSTERING allows mutiple physically contig bio_vecs to merge
418          * into 1 sg element. If set, we must limit the max_segment_size to
419          * PAGE_SIZE, otherwise we may get 1 sg element that represents
420          * multiple
421          */
422         /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */
423         .use_clustering =       ENABLE_CLUSTERING,
424         /* Make sure we dont get a sg segment crosses a page boundary */
425         .dma_boundary =         PAGE_SIZE-1,
426 };
427
428
429 /*
430  * storvsc_drv_init - StorVsc driver initialization.
431  */
432 static int storvsc_drv_init(void)
433 {
434         int ret;
435         struct storvsc_driver *storvsc_drv_obj = &g_storvsc_drv;
436         struct hv_driver *drv = &g_storvsc_drv.base;
437
438         storvsc_drv_obj->ring_buffer_size = storvsc_ringbuffer_size;
439
440         /* Callback to client driver to complete the initialization */
441         stor_vsc_initialize(&storvsc_drv_obj->base);
442
443         DPRINT_INFO(STORVSC_DRV,
444                     "max outstanding reqs %u",
445                     storvsc_drv_obj->max_outstanding_req_per_channel);
446
447         if (storvsc_drv_obj->max_outstanding_req_per_channel <
448             STORVSC_MAX_IO_REQUESTS)
449                 return -1;
450
451         drv->driver.name = storvsc_drv_obj->base.name;
452
453         drv->probe = storvsc_probe;
454         drv->remove = storvsc_remove;
455
456         /* The driver belongs to vmbus */
457         ret = vmbus_child_driver_register(&drv->driver);
458
459         return ret;
460 }
461
462
463 static int stor_vsc_on_host_reset(struct hv_device *device)
464 {
465         struct storvsc_device *stor_device;
466         struct hv_storvsc_request *request;
467         struct vstor_packet *vstor_packet;
468         int ret, t;
469
470         DPRINT_INFO(STORVSC, "resetting host adapter...");
471
472         stor_device = get_stor_device(device);
473         if (!stor_device)
474                 return -1;
475
476         request = &stor_device->reset_request;
477         vstor_packet = &request->vstor_packet;
478
479         init_completion(&request->wait_event);
480
481         vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
482         vstor_packet->flags = REQUEST_COMPLETION_FLAG;
483         vstor_packet->vm_srb.path_id = stor_device->path_id;
484
485         ret = vmbus_sendpacket(device->channel, vstor_packet,
486                                sizeof(struct vstor_packet),
487                                (unsigned long)&stor_device->reset_request,
488                                VM_PKT_DATA_INBAND,
489                                VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
490         if (ret != 0)
491                 goto cleanup;
492
493         t = wait_for_completion_timeout(&request->wait_event, HZ);
494         if (t == 0) {
495                 ret = -ETIMEDOUT;
496                 goto cleanup;
497         }
498
499         DPRINT_INFO(STORVSC, "host adapter reset completed");
500
501         /*
502          * At this point, all outstanding requests in the adapter
503          * should have been flushed out and return to us
504          */
505
506 cleanup:
507         put_stor_device(device);
508         return ret;
509 }
510
511 static int storvsc_drv_exit_cb(struct device *dev, void *data)
512 {
513         struct device **curr = (struct device **)data;
514         *curr = dev;
515         return 1; /* stop iterating */
516 }
517
518 static void storvsc_drv_exit(void)
519 {
520         struct storvsc_driver *storvsc_drv_obj = &g_storvsc_drv;
521         struct hv_driver *drv = &g_storvsc_drv.base;
522         struct device *current_dev = NULL;
523         int ret;
524
525         while (1) {
526                 current_dev = NULL;
527
528                 /* Get the device */
529                 ret = driver_for_each_device(&drv->driver, NULL,
530                                              (void *) &current_dev,
531                                              storvsc_drv_exit_cb);
532
533
534                 if (current_dev == NULL)
535                         break;
536
537                 /* Initiate removal from the top-down */
538                 device_unregister(current_dev);
539         }
540
541         if (storvsc_drv_obj->base.cleanup)
542                 storvsc_drv_obj->base.cleanup(&storvsc_drv_obj->base);
543
544         vmbus_child_driver_unregister(&drv->driver);
545         return;
546 }
547
548 /*
549  * storvsc_probe - Add a new device for this driver
550  */
551 static int storvsc_probe(struct hv_device *device)
552 {
553         int ret;
554         struct storvsc_driver *storvsc_drv_obj =
555                  drv_to_stordrv(device->device.driver);
556         struct Scsi_Host *host;
557         struct hv_host_device *host_dev;
558         struct storvsc_device_info device_info;
559
560         if (!storvsc_drv_obj->base.dev_add)
561                 return -1;
562
563         host = scsi_host_alloc(&scsi_driver,
564                                sizeof(struct hv_host_device));
565         if (!host)
566                 return -ENOMEM;
567
568         dev_set_drvdata(&device->device, host);
569
570         host_dev = (struct hv_host_device *)host->hostdata;
571         memset(host_dev, 0, sizeof(struct hv_host_device));
572
573         host_dev->port = host->host_no;
574         host_dev->dev = device;
575
576         host_dev->request_pool =
577                                 kmem_cache_create(dev_name(&device->device),
578                                         sizeof(struct storvsc_cmd_request), 0,
579                                         SLAB_HWCACHE_ALIGN, NULL);
580
581         if (!host_dev->request_pool) {
582                 scsi_host_put(host);
583                 return -ENOMEM;
584         }
585
586         device_info.port_number = host->host_no;
587         /* Call to the vsc driver to add the device */
588         ret = storvsc_drv_obj->base.dev_add(device, (void *)&device_info);
589
590         if (ret != 0) {
591                 kmem_cache_destroy(host_dev->request_pool);
592                 scsi_host_put(host);
593                 return -1;
594         }
595
596         host_dev->path = device_info.path_id;
597         host_dev->target = device_info.target_id;
598
599         /* max # of devices per target */
600         host->max_lun = STORVSC_MAX_LUNS_PER_TARGET;
601         /* max # of targets per channel */
602         host->max_id = STORVSC_MAX_TARGETS;
603         /* max # of channels */
604         host->max_channel = STORVSC_MAX_CHANNELS - 1;
605
606         /* Register the HBA and start the scsi bus scan */
607         ret = scsi_add_host(host, &device->device);
608         if (ret != 0) {
609
610                 storvsc_drv_obj->base.dev_rm(device);
611
612                 kmem_cache_destroy(host_dev->request_pool);
613                 scsi_host_put(host);
614                 return -1;
615         }
616
617         scsi_scan_host(host);
618         return ret;
619 }
620
621 /*
622  * storvsc_commmand_completion - Command completion processing
623  */
624 static void storvsc_commmand_completion(struct hv_storvsc_request *request)
625 {
626         struct storvsc_cmd_request *cmd_request =
627                 (struct storvsc_cmd_request *)request->context;
628         struct scsi_cmnd *scmnd = cmd_request->cmd;
629         struct hv_host_device *host_dev =
630                 (struct hv_host_device *)scmnd->device->host->hostdata;
631         void (*scsi_done_fn)(struct scsi_cmnd *);
632         struct scsi_sense_hdr sense_hdr;
633         struct vmscsi_request *vm_srb;
634
635         /* ASSERT(request == &cmd_request->request); */
636         /* ASSERT(scmnd); */
637         /* ASSERT((unsigned long)scmnd->host_scribble == */
638         /*        (unsigned long)cmd_request); */
639         /* ASSERT(scmnd->scsi_done); */
640
641         if (cmd_request->bounce_sgl_count) {
642                 /* using bounce buffer */
643                 /* printk("copy_from_bounce_buffer\n"); */
644
645                 /* FIXME: We can optimize on writes by just skipping this */
646                 copy_from_bounce_buffer(scsi_sglist(scmnd),
647                                         cmd_request->bounce_sgl,
648                                         scsi_sg_count(scmnd));
649                 destroy_bounce_buffer(cmd_request->bounce_sgl,
650                                       cmd_request->bounce_sgl_count);
651         }
652
653         vm_srb = &request->vstor_packet.vm_srb;
654         scmnd->result = vm_srb->scsi_status;
655
656         if (scmnd->result) {
657                 if (scsi_normalize_sense(scmnd->sense_buffer,
658                                 SCSI_SENSE_BUFFERSIZE, &sense_hdr))
659                         scsi_print_sense_hdr("storvsc", &sense_hdr);
660         }
661
662         /* ASSERT(request->BytesXfer <= request->data_buffer.Length); */
663         scsi_set_resid(scmnd,
664                 request->data_buffer.len -
665                 vm_srb->data_transfer_length);
666
667         scsi_done_fn = scmnd->scsi_done;
668
669         scmnd->host_scribble = NULL;
670         scmnd->scsi_done = NULL;
671
672         /* !!DO NOT MODIFY the scmnd after this call */
673         scsi_done_fn(scmnd);
674
675         kmem_cache_free(host_dev->request_pool, cmd_request);
676 }
677
678 /*
679  * storvsc_queuecommand - Initiate command processing
680  */
681 static int storvsc_queuecommand_lck(struct scsi_cmnd *scmnd,
682                                 void (*done)(struct scsi_cmnd *))
683 {
684         int ret;
685         struct hv_host_device *host_dev =
686                 (struct hv_host_device *)scmnd->device->host->hostdata;
687         struct hv_device *dev = host_dev->dev;
688         struct storvsc_driver *storvsc_drv_obj =
689                 drv_to_stordrv(dev->device.driver);
690         struct hv_storvsc_request *request;
691         struct storvsc_cmd_request *cmd_request;
692         unsigned int request_size = 0;
693         int i;
694         struct scatterlist *sgl;
695         unsigned int sg_count = 0;
696         struct vmscsi_request *vm_srb;
697
698
699         /* If retrying, no need to prep the cmd */
700         if (scmnd->host_scribble) {
701                 /* ASSERT(scmnd->scsi_done != NULL); */
702
703                 cmd_request =
704                         (struct storvsc_cmd_request *)scmnd->host_scribble;
705                 DPRINT_INFO(STORVSC_DRV, "retrying scmnd %p cmd_request %p",
706                             scmnd, cmd_request);
707
708                 goto retry_request;
709         }
710
711         /* ASSERT(scmnd->scsi_done == NULL); */
712         /* ASSERT(scmnd->host_scribble == NULL); */
713
714         scmnd->scsi_done = done;
715
716         request_size = sizeof(struct storvsc_cmd_request);
717
718         cmd_request = kmem_cache_zalloc(host_dev->request_pool,
719                                        GFP_ATOMIC);
720         if (!cmd_request) {
721                 scmnd->scsi_done = NULL;
722                 return SCSI_MLQUEUE_DEVICE_BUSY;
723         }
724
725         /* Setup the cmd request */
726         cmd_request->bounce_sgl_count = 0;
727         cmd_request->bounce_sgl = NULL;
728         cmd_request->cmd = scmnd;
729
730         scmnd->host_scribble = (unsigned char *)cmd_request;
731
732         request = &cmd_request->request;
733         vm_srb = &request->vstor_packet.vm_srb;
734
735
736         /* Build the SRB */
737         switch (scmnd->sc_data_direction) {
738         case DMA_TO_DEVICE:
739                 vm_srb->data_in = WRITE_TYPE;
740                 break;
741         case DMA_FROM_DEVICE:
742                 vm_srb->data_in = READ_TYPE;
743                 break;
744         default:
745                 vm_srb->data_in = UNKNOWN_TYPE;
746                 break;
747         }
748
749         request->on_io_completion = storvsc_commmand_completion;
750         request->context = cmd_request;/* scmnd; */
751
752         /* request->PortId = scmnd->device->channel; */
753         vm_srb->port_number = host_dev->port;
754         vm_srb->path_id = scmnd->device->channel;
755         vm_srb->target_id = scmnd->device->id;
756         vm_srb->lun = scmnd->device->lun;
757
758         /* ASSERT(scmnd->cmd_len <= 16); */
759         vm_srb->cdb_length = scmnd->cmd_len;
760
761         memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
762
763         request->sense_buffer = scmnd->sense_buffer;
764
765
766         request->data_buffer.len = scsi_bufflen(scmnd);
767         if (scsi_sg_count(scmnd)) {
768                 sgl = (struct scatterlist *)scsi_sglist(scmnd);
769                 sg_count = scsi_sg_count(scmnd);
770
771                 /* check if we need to bounce the sgl */
772                 if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) {
773                         cmd_request->bounce_sgl =
774                                 create_bounce_buffer(sgl, scsi_sg_count(scmnd),
775                                                      scsi_bufflen(scmnd));
776                         if (!cmd_request->bounce_sgl) {
777                                 scmnd->scsi_done = NULL;
778                                 scmnd->host_scribble = NULL;
779                                 kmem_cache_free(host_dev->request_pool,
780                                                 cmd_request);
781
782                                 return SCSI_MLQUEUE_HOST_BUSY;
783                         }
784
785                         cmd_request->bounce_sgl_count =
786                                 ALIGN(scsi_bufflen(scmnd), PAGE_SIZE) >>
787                                         PAGE_SHIFT;
788
789                         /*
790                          * FIXME: We can optimize on reads by just skipping
791                          * this
792                          */
793                         copy_to_bounce_buffer(sgl, cmd_request->bounce_sgl,
794                                               scsi_sg_count(scmnd));
795
796                         sgl = cmd_request->bounce_sgl;
797                         sg_count = cmd_request->bounce_sgl_count;
798                 }
799
800                 request->data_buffer.offset = sgl[0].offset;
801
802                 for (i = 0; i < sg_count; i++)
803                         request->data_buffer.pfn_array[i] =
804                                 page_to_pfn(sg_page((&sgl[i])));
805
806         } else if (scsi_sglist(scmnd)) {
807                 /* ASSERT(scsi_bufflen(scmnd) <= PAGE_SIZE); */
808                 request->data_buffer.offset =
809                         virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
810                 request->data_buffer.pfn_array[0] =
811                         virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
812         }
813
814 retry_request:
815         /* Invokes the vsc to start an IO */
816         ret = storvsc_drv_obj->on_io_request(dev,
817                                            &cmd_request->request);
818         if (ret == -1) {
819                 /* no more space */
820
821                 if (cmd_request->bounce_sgl_count) {
822                         /*
823                          * FIXME: We can optimize on writes by just skipping
824                          * this
825                          */
826                         copy_from_bounce_buffer(scsi_sglist(scmnd),
827                                                 cmd_request->bounce_sgl,
828                                                 scsi_sg_count(scmnd));
829                         destroy_bounce_buffer(cmd_request->bounce_sgl,
830                                               cmd_request->bounce_sgl_count);
831                 }
832
833                 kmem_cache_free(host_dev->request_pool, cmd_request);
834
835                 scmnd->scsi_done = NULL;
836                 scmnd->host_scribble = NULL;
837
838                 ret = SCSI_MLQUEUE_DEVICE_BUSY;
839         }
840
841         return ret;
842 }
843
844 static DEF_SCSI_QCMD(storvsc_queuecommand)
845
846 /*
847  * storvsc_host_reset_handler - Reset the scsi HBA
848  */
849 static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
850 {
851         int ret;
852         struct hv_host_device *host_dev =
853                 (struct hv_host_device *)scmnd->device->host->hostdata;
854         struct hv_device *dev = host_dev->dev;
855
856         DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host resetting...",
857                     scmnd->device, dev);
858
859         /* Invokes the vsc to reset the host/bus */
860         ret = stor_vsc_on_host_reset(dev);
861         if (ret != 0)
862                 return ret;
863
864         DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host reseted",
865                     scmnd->device, dev);
866
867         return ret;
868 }
869
870 static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
871                            sector_t capacity, int *info)
872 {
873         sector_t total_sectors = capacity;
874         sector_t cylinder_times_heads = 0;
875         sector_t temp = 0;
876
877         int sectors_per_track = 0;
878         int heads = 0;
879         int cylinders = 0;
880         int rem = 0;
881
882         if (total_sectors > (65535 * 16 * 255))
883                 total_sectors = (65535 * 16 * 255);
884
885         if (total_sectors >= (65535 * 16 * 63)) {
886                 sectors_per_track = 255;
887                 heads = 16;
888
889                 cylinder_times_heads = total_sectors;
890                 /* sector_div stores the quotient in cylinder_times_heads */
891                 rem = sector_div(cylinder_times_heads, sectors_per_track);
892         } else {
893                 sectors_per_track = 17;
894
895                 cylinder_times_heads = total_sectors;
896                 /* sector_div stores the quotient in cylinder_times_heads */
897                 rem = sector_div(cylinder_times_heads, sectors_per_track);
898
899                 temp = cylinder_times_heads + 1023;
900                 /* sector_div stores the quotient in temp */
901                 rem = sector_div(temp, 1024);
902
903                 heads = temp;
904
905                 if (heads < 4)
906                         heads = 4;
907
908                 if (cylinder_times_heads >= (heads * 1024) || (heads > 16)) {
909                         sectors_per_track = 31;
910                         heads = 16;
911
912                         cylinder_times_heads = total_sectors;
913                         /*
914                          * sector_div stores the quotient in
915                          * cylinder_times_heads
916                          */
917                         rem = sector_div(cylinder_times_heads,
918                                          sectors_per_track);
919                 }
920
921                 if (cylinder_times_heads >= (heads * 1024)) {
922                         sectors_per_track = 63;
923                         heads = 16;
924
925                         cylinder_times_heads = total_sectors;
926                         /*
927                          * sector_div stores the quotient in
928                          * cylinder_times_heads
929                          */
930                         rem = sector_div(cylinder_times_heads,
931                                          sectors_per_track);
932                 }
933         }
934
935         temp = cylinder_times_heads;
936         /* sector_div stores the quotient in temp */
937         rem = sector_div(temp, heads);
938         cylinders = temp;
939
940         info[0] = heads;
941         info[1] = sectors_per_track;
942         info[2] = cylinders;
943
944         DPRINT_INFO(STORVSC_DRV, "CHS (%d, %d, %d)", cylinders, heads,
945                     sectors_per_track);
946
947     return 0;
948 }
949
950 static int __init storvsc_init(void)
951 {
952         int ret;
953
954         DPRINT_INFO(STORVSC_DRV, "Storvsc initializing....");
955         ret = storvsc_drv_init();
956         return ret;
957 }
958
959 static void __exit storvsc_exit(void)
960 {
961         storvsc_drv_exit();
962 }
963
964 MODULE_LICENSE("GPL");
965 MODULE_VERSION(HV_DRV_VERSION);
966 MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
967 module_init(storvsc_init);
968 module_exit(storvsc_exit);