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