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