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