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