GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / staging / hv / storvsc_drv.c
blobaadea7d9d94cbb6f53d96e59700170bd8b227d80
1 /*
2 * Copyright (c) 2009, Microsoft Corporation.
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.
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.
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.
17 * Authors:
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
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"
41 struct host_device_context {
42 /* point back to our device context */
43 struct vm_device *device_ctx;
44 struct kmem_cache *request_pool;
45 unsigned int port;
46 unsigned char path;
47 unsigned char target;
50 struct storvsc_cmd_request {
51 struct list_head entry;
52 struct scsi_cmnd *cmd;
54 unsigned int bounce_sgl_count;
55 struct scatterlist *bounce_sgl;
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... */
64 struct storvsc_driver_context {
65 /* !! These must be the first 2 fields !! */
66 struct driver_context drv_ctx;
67 struct storvsc_driver_object drv_obj;
70 /* Static decl */
71 static int storvsc_probe(struct device *dev);
72 static int storvsc_queuecommand(struct scsi_cmnd *scmnd,
73 void (*done)(struct scsi_cmnd *));
74 static int storvsc_device_alloc(struct scsi_device *);
75 static int storvsc_device_configure(struct scsi_device *);
76 static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd);
77 static int storvsc_remove(struct device *dev);
79 static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
80 unsigned int sg_count,
81 unsigned int len);
82 static void destroy_bounce_buffer(struct scatterlist *sgl,
83 unsigned int sg_count);
84 static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count);
85 static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
86 struct scatterlist *bounce_sgl,
87 unsigned int orig_sgl_count);
88 static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
89 struct scatterlist *bounce_sgl,
90 unsigned int orig_sgl_count);
92 static int storvsc_get_chs(struct scsi_device *sdev, struct block_device *bdev,
93 sector_t capacity, int *info);
96 static int storvsc_ringbuffer_size = STORVSC_RING_BUFFER_SIZE;
97 module_param(storvsc_ringbuffer_size, int, S_IRUGO);
98 MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)");
100 /* The one and only one */
101 static struct storvsc_driver_context g_storvsc_drv;
103 /* Scsi driver */
104 static struct scsi_host_template scsi_driver = {
105 .module = THIS_MODULE,
106 .name = "storvsc_host_t",
107 .bios_param = storvsc_get_chs,
108 .queuecommand = storvsc_queuecommand,
109 .eh_host_reset_handler = storvsc_host_reset_handler,
110 .slave_alloc = storvsc_device_alloc,
111 .slave_configure = storvsc_device_configure,
112 .cmd_per_lun = 1,
113 /* 64 max_queue * 1 target */
114 .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS,
115 .this_id = -1,
116 /* no use setting to 0 since ll_blk_rw reset it to 1 */
117 /* currently 32 */
118 .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT,
120 * ENABLE_CLUSTERING allows mutiple physically contig bio_vecs to merge
121 * into 1 sg element. If set, we must limit the max_segment_size to
122 * PAGE_SIZE, otherwise we may get 1 sg element that represents
123 * multiple
125 /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */
126 .use_clustering = ENABLE_CLUSTERING,
127 /* Make sure we dont get a sg segment crosses a page boundary */
128 .dma_boundary = PAGE_SIZE-1,
133 * storvsc_drv_init - StorVsc driver initialization.
135 static int storvsc_drv_init(int (*drv_init)(struct hv_driver *drv))
137 int ret;
138 struct storvsc_driver_object *storvsc_drv_obj = &g_storvsc_drv.drv_obj;
139 struct driver_context *drv_ctx = &g_storvsc_drv.drv_ctx;
141 vmbus_get_interface(&storvsc_drv_obj->Base.VmbusChannelInterface);
143 storvsc_drv_obj->RingBufferSize = storvsc_ringbuffer_size;
145 /* Callback to client driver to complete the initialization */
146 drv_init(&storvsc_drv_obj->Base);
148 DPRINT_INFO(STORVSC_DRV,
149 "request extension size %u, max outstanding reqs %u",
150 storvsc_drv_obj->RequestExtSize,
151 storvsc_drv_obj->MaxOutstandingRequestsPerChannel);
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;
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));
167 drv_ctx->probe = storvsc_probe;
168 drv_ctx->remove = storvsc_remove;
170 /* The driver belongs to vmbus */
171 ret = vmbus_child_driver_register(drv_ctx);
173 return ret;
176 static int storvsc_drv_exit_cb(struct device *dev, void *data)
178 struct device **curr = (struct device **)data;
179 *curr = dev;
180 return 1; /* stop iterating */
183 static void storvsc_drv_exit(void)
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;
190 while (1) {
191 current_dev = NULL;
193 /* Get the device */
194 ret = driver_for_each_device(&drv_ctx->driver, NULL,
195 (void *) &current_dev,
196 storvsc_drv_exit_cb);
198 if (ret)
199 DPRINT_WARN(STORVSC_DRV,
200 "driver_for_each_device returned %d", ret);
202 if (current_dev == NULL)
203 break;
205 /* Initiate removal from the top-down */
206 device_unregister(current_dev);
209 if (storvsc_drv_obj->Base.OnCleanup)
210 storvsc_drv_obj->Base.OnCleanup(&storvsc_drv_obj->Base);
212 vmbus_child_driver_unregister(drv_ctx);
213 return;
217 * storvsc_probe - Add a new device for this driver
219 static int storvsc_probe(struct device *device)
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;
234 if (!storvsc_drv_obj->Base.OnDeviceAdd)
235 return -1;
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;
244 dev_set_drvdata(device, host);
246 host_device_ctx = (struct host_device_context *)host->hostdata;
247 memset(host_device_ctx, 0, sizeof(struct host_device_context));
249 host_device_ctx->port = host->host_no;
250 host_device_ctx->device_ctx = device_ctx;
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);
258 if (!host_device_ctx->request_pool) {
259 scsi_host_put(host);
260 return -ENOMEM;
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;
274 /* host_device_ctx->port = device_info.PortNumber; */
275 host_device_ctx->path = device_info.PathId;
276 host_device_ctx->target = device_info.TargetId;
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;
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");
290 storvsc_drv_obj->Base.OnDeviceRemove(device_obj);
292 kmem_cache_destroy(host_device_ctx->request_pool);
293 scsi_host_put(host);
294 return -1;
297 scsi_scan_host(host);
298 return ret;
302 * storvsc_remove - Callback when our device is removed
304 static int storvsc_remove(struct device *device)
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;
320 if (!storvsc_drv_obj->Base.OnDeviceRemove)
321 return -1;
324 * Call to the vsc driver to let it know that the device is being
325 * removed
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);
334 if (host_device_ctx->request_pool) {
335 kmem_cache_destroy(host_device_ctx->request_pool);
336 host_device_ctx->request_pool = NULL;
339 DPRINT_INFO(STORVSC, "removing host adapter (%p)...", host);
340 scsi_remove_host(host);
342 DPRINT_INFO(STORVSC, "releasing host adapter (%p)...", host);
343 scsi_host_put(host);
344 return ret;
348 * storvsc_commmand_completion - Command completion processing
350 static void storvsc_commmand_completion(struct hv_storvsc_request *request)
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;
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); */
366 if (cmd_request->bounce_sgl_count) {
367 /* using bounce buffer */
368 /* printk("copy_from_bounce_buffer\n"); */
370 copy_from_bounce_buffer(scsi_sglist(scmnd),
371 cmd_request->bounce_sgl,
372 scsi_sg_count(scmnd));
373 destroy_bounce_buffer(cmd_request->bounce_sgl,
374 cmd_request->bounce_sgl_count);
377 scmnd->result = request->Status;
379 if (scmnd->result) {
380 if (scsi_normalize_sense(scmnd->sense_buffer,
381 request->SenseBufferSize, &sense_hdr))
382 scsi_print_sense_hdr("storvsc", &sense_hdr);
385 /* ASSERT(request->BytesXfer <= request->DataBuffer.Length); */
386 scsi_set_resid(scmnd, request->DataBuffer.Length - request->BytesXfer);
388 scsi_done_fn = scmnd->scsi_done;
390 scmnd->host_scribble = NULL;
391 scmnd->scsi_done = NULL;
393 /* !!DO NOT MODIFY the scmnd after this call */
394 scsi_done_fn(scmnd);
396 kmem_cache_free(host_device_ctx->request_pool, cmd_request);
399 static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
401 int i;
403 /* No need to check */
404 if (sg_count < 2)
405 return -1;
407 /* We have at least 2 sg entries */
408 for (i = 0; i < sg_count; i++) {
409 if (i == 0) {
410 /* make sure 1st one does not have hole */
411 if (sgl[i].offset + sgl[i].length != PAGE_SIZE)
412 return i;
413 } else if (i == sg_count - 1) {
414 /* make sure last one does not have hole */
415 if (sgl[i].offset != 0)
416 return i;
417 } else {
418 /* make sure no hole in the middle */
419 if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0)
420 return i;
423 return -1;
426 static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
427 unsigned int sg_count,
428 unsigned int len)
430 int i;
431 int num_pages;
432 struct scatterlist *bounce_sgl;
433 struct page *page_buf;
435 num_pages = ALIGN_UP(len, PAGE_SIZE) >> PAGE_SHIFT;
437 bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC);
438 if (!bounce_sgl)
439 return NULL;
441 for (i = 0; i < num_pages; i++) {
442 page_buf = alloc_page(GFP_ATOMIC);
443 if (!page_buf)
444 goto cleanup;
445 sg_set_page(&bounce_sgl[i], page_buf, 0, 0);
448 return bounce_sgl;
450 cleanup:
451 destroy_bounce_buffer(bounce_sgl, num_pages);
452 return NULL;
455 static void destroy_bounce_buffer(struct scatterlist *sgl,
456 unsigned int sg_count)
458 int i;
459 struct page *page_buf;
461 for (i = 0; i < sg_count; i++) {
462 page_buf = sg_page((&sgl[i]));
463 if (page_buf != NULL)
464 __free_page(page_buf);
467 kfree(sgl);
470 /* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
471 static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
472 struct scatterlist *bounce_sgl,
473 unsigned int orig_sgl_count)
475 int i;
476 int j = 0;
477 unsigned long src, dest;
478 unsigned int srclen, destlen, copylen;
479 unsigned int total_copied = 0;
480 unsigned long bounce_addr = 0;
481 unsigned long src_addr = 0;
482 unsigned long flags;
484 local_irq_save(flags);
486 for (i = 0; i < orig_sgl_count; i++) {
487 src_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
488 KM_IRQ0) + orig_sgl[i].offset;
489 src = src_addr;
490 srclen = orig_sgl[i].length;
492 /* ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE); */
494 if (bounce_addr == 0)
495 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
497 while (srclen) {
498 /* assume bounce offset always == 0 */
499 dest = bounce_addr + bounce_sgl[j].length;
500 destlen = PAGE_SIZE - bounce_sgl[j].length;
502 copylen = min(srclen, destlen);
503 memcpy((void *)dest, (void *)src, copylen);
505 total_copied += copylen;
506 bounce_sgl[j].length += copylen;
507 srclen -= copylen;
508 src += copylen;
510 if (bounce_sgl[j].length == PAGE_SIZE) {
511 /* full..move to next entry */
512 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
513 j++;
515 /* if we need to use another bounce buffer */
516 if (srclen || i != orig_sgl_count - 1)
517 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
518 } else if (srclen == 0 && i == orig_sgl_count - 1) {
519 /* unmap the last bounce that is < PAGE_SIZE */
520 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
524 kunmap_atomic((void *)(src_addr - orig_sgl[i].offset), KM_IRQ0);
527 local_irq_restore(flags);
529 return total_copied;
532 /* Assume the original sgl has enough room */
533 static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
534 struct scatterlist *bounce_sgl,
535 unsigned int orig_sgl_count)
537 int i;
538 int j = 0;
539 unsigned long src, dest;
540 unsigned int srclen, destlen, copylen;
541 unsigned int total_copied = 0;
542 unsigned long bounce_addr = 0;
543 unsigned long dest_addr = 0;
544 unsigned long flags;
546 local_irq_save(flags);
548 for (i = 0; i < orig_sgl_count; i++) {
549 dest_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
550 KM_IRQ0) + orig_sgl[i].offset;
551 dest = dest_addr;
552 destlen = orig_sgl[i].length;
553 /* ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE); */
555 if (bounce_addr == 0)
556 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
558 while (destlen) {
559 src = bounce_addr + bounce_sgl[j].offset;
560 srclen = bounce_sgl[j].length - bounce_sgl[j].offset;
562 copylen = min(srclen, destlen);
563 memcpy((void *)dest, (void *)src, copylen);
565 total_copied += copylen;
566 bounce_sgl[j].offset += copylen;
567 destlen -= copylen;
568 dest += copylen;
570 if (bounce_sgl[j].offset == bounce_sgl[j].length) {
571 /* full */
572 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
573 j++;
575 /* if we need to use another bounce buffer */
576 if (destlen || i != orig_sgl_count - 1)
577 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
578 } else if (destlen == 0 && i == orig_sgl_count - 1) {
579 /* unmap the last bounce that is < PAGE_SIZE */
580 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
584 kunmap_atomic((void *)(dest_addr - orig_sgl[i].offset),
585 KM_IRQ0);
588 local_irq_restore(flags);
590 return total_copied;
594 * storvsc_queuecommand - Initiate command processing
596 static int storvsc_queuecommand(struct scsi_cmnd *scmnd,
597 void (*done)(struct scsi_cmnd *))
599 int ret;
600 struct host_device_context *host_device_ctx =
601 (struct host_device_context *)scmnd->device->host->hostdata;
602 struct vm_device *device_ctx = host_device_ctx->device_ctx;
603 struct driver_context *driver_ctx =
604 driver_to_driver_context(device_ctx->device.driver);
605 struct storvsc_driver_context *storvsc_drv_ctx =
606 (struct storvsc_driver_context *)driver_ctx;
607 struct storvsc_driver_object *storvsc_drv_obj =
608 &storvsc_drv_ctx->drv_obj;
609 struct hv_storvsc_request *request;
610 struct storvsc_cmd_request *cmd_request;
611 unsigned int request_size = 0;
612 int i;
613 struct scatterlist *sgl;
614 unsigned int sg_count = 0;
616 DPRINT_DBG(STORVSC_DRV, "scmnd %p dir %d, use_sg %d buf %p len %d "
617 "queue depth %d tagged %d", scmnd, scmnd->sc_data_direction,
618 scsi_sg_count(scmnd), scsi_sglist(scmnd),
619 scsi_bufflen(scmnd), scmnd->device->queue_depth,
620 scmnd->device->tagged_supported);
622 /* If retrying, no need to prep the cmd */
623 if (scmnd->host_scribble) {
624 /* ASSERT(scmnd->scsi_done != NULL); */
626 cmd_request =
627 (struct storvsc_cmd_request *)scmnd->host_scribble;
628 DPRINT_INFO(STORVSC_DRV, "retrying scmnd %p cmd_request %p",
629 scmnd, cmd_request);
631 goto retry_request;
634 /* ASSERT(scmnd->scsi_done == NULL); */
635 /* ASSERT(scmnd->host_scribble == NULL); */
637 scmnd->scsi_done = done;
639 request_size = sizeof(struct storvsc_cmd_request);
641 cmd_request = kmem_cache_alloc(host_device_ctx->request_pool,
642 GFP_ATOMIC);
643 if (!cmd_request) {
644 DPRINT_ERR(STORVSC_DRV, "scmnd (%p) - unable to allocate "
645 "storvsc_cmd_request...marking queue busy", scmnd);
646 scmnd->scsi_done = NULL;
647 return SCSI_MLQUEUE_DEVICE_BUSY;
650 /* Setup the cmd request */
651 cmd_request->bounce_sgl_count = 0;
652 cmd_request->bounce_sgl = NULL;
653 cmd_request->cmd = scmnd;
655 scmnd->host_scribble = (unsigned char *)cmd_request;
657 request = &cmd_request->request;
659 request->Extension =
660 (void *)((unsigned long)cmd_request + request_size);
661 DPRINT_DBG(STORVSC_DRV, "req %p size %d ext %d", request, request_size,
662 storvsc_drv_obj->RequestExtSize);
664 /* Build the SRB */
665 switch (scmnd->sc_data_direction) {
666 case DMA_TO_DEVICE:
667 request->Type = WRITE_TYPE;
668 break;
669 case DMA_FROM_DEVICE:
670 request->Type = READ_TYPE;
671 break;
672 default:
673 request->Type = UNKNOWN_TYPE;
674 break;
677 request->OnIOCompletion = storvsc_commmand_completion;
678 request->Context = cmd_request;/* scmnd; */
680 /* request->PortId = scmnd->device->channel; */
681 request->Host = host_device_ctx->port;
682 request->Bus = scmnd->device->channel;
683 request->TargetId = scmnd->device->id;
684 request->LunId = scmnd->device->lun;
686 /* ASSERT(scmnd->cmd_len <= 16); */
687 request->CdbLen = scmnd->cmd_len;
688 request->Cdb = scmnd->cmnd;
690 request->SenseBuffer = scmnd->sense_buffer;
691 request->SenseBufferSize = SCSI_SENSE_BUFFERSIZE;
694 request->DataBuffer.Length = scsi_bufflen(scmnd);
695 if (scsi_sg_count(scmnd)) {
696 sgl = (struct scatterlist *)scsi_sglist(scmnd);
697 sg_count = scsi_sg_count(scmnd);
699 /* check if we need to bounce the sgl */
700 if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) {
701 DPRINT_INFO(STORVSC_DRV,
702 "need to bounce buffer for this scmnd %p",
703 scmnd);
704 cmd_request->bounce_sgl =
705 create_bounce_buffer(sgl, scsi_sg_count(scmnd),
706 scsi_bufflen(scmnd));
707 if (!cmd_request->bounce_sgl) {
708 DPRINT_ERR(STORVSC_DRV,
709 "unable to create bounce buffer for "
710 "this scmnd %p", scmnd);
712 scmnd->scsi_done = NULL;
713 scmnd->host_scribble = NULL;
714 kmem_cache_free(host_device_ctx->request_pool,
715 cmd_request);
717 return SCSI_MLQUEUE_HOST_BUSY;
720 cmd_request->bounce_sgl_count =
721 ALIGN_UP(scsi_bufflen(scmnd), PAGE_SIZE) >>
722 PAGE_SHIFT;
724 copy_to_bounce_buffer(sgl, cmd_request->bounce_sgl,
725 scsi_sg_count(scmnd));
727 sgl = cmd_request->bounce_sgl;
728 sg_count = cmd_request->bounce_sgl_count;
731 request->DataBuffer.Offset = sgl[0].offset;
733 for (i = 0; i < sg_count; i++) {
734 DPRINT_DBG(STORVSC_DRV, "sgl[%d] len %d offset %d\n",
735 i, sgl[i].length, sgl[i].offset);
736 request->DataBuffer.PfnArray[i] =
737 page_to_pfn(sg_page((&sgl[i])));
739 } else if (scsi_sglist(scmnd)) {
740 /* ASSERT(scsi_bufflen(scmnd) <= PAGE_SIZE); */
741 request->DataBuffer.Offset =
742 virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
743 request->DataBuffer.PfnArray[0] =
744 virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
747 retry_request:
748 /* Invokes the vsc to start an IO */
749 ret = storvsc_drv_obj->OnIORequest(&device_ctx->device_obj,
750 &cmd_request->request);
751 if (ret == -1) {
752 /* no more space */
753 DPRINT_ERR(STORVSC_DRV,
754 "scmnd (%p) - queue FULL...marking queue busy",
755 scmnd);
757 if (cmd_request->bounce_sgl_count) {
758 copy_from_bounce_buffer(scsi_sglist(scmnd),
759 cmd_request->bounce_sgl,
760 scsi_sg_count(scmnd));
761 destroy_bounce_buffer(cmd_request->bounce_sgl,
762 cmd_request->bounce_sgl_count);
765 kmem_cache_free(host_device_ctx->request_pool, cmd_request);
767 scmnd->scsi_done = NULL;
768 scmnd->host_scribble = NULL;
770 ret = SCSI_MLQUEUE_DEVICE_BUSY;
773 return ret;
776 static int storvsc_merge_bvec(struct request_queue *q,
777 struct bvec_merge_data *bmd, struct bio_vec *bvec)
779 /* checking done by caller. */
780 return bvec->bv_len;
784 * storvsc_device_configure - Configure the specified scsi device
786 static int storvsc_device_alloc(struct scsi_device *sdevice)
788 DPRINT_DBG(STORVSC_DRV, "sdev (%p) - setting device flag to %d",
789 sdevice, BLIST_SPARSELUN);
791 * This enables luns to be located sparsely. Otherwise, we may not
792 * discovered them.
794 sdevice->sdev_bflags |= BLIST_SPARSELUN | BLIST_LARGELUN;
795 return 0;
798 static int storvsc_device_configure(struct scsi_device *sdevice)
800 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - curr queue depth %d", sdevice,
801 sdevice->queue_depth);
803 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - setting queue depth to %d",
804 sdevice, STORVSC_MAX_IO_REQUESTS);
805 scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG,
806 STORVSC_MAX_IO_REQUESTS);
808 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - setting max segment size to %ld",
809 sdevice, PAGE_SIZE);
810 blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
812 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - adding merge bio vec routine",
813 sdevice);
814 blk_queue_merge_bvec(sdevice->request_queue, storvsc_merge_bvec);
816 blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
817 /* sdevice->timeout = (2000 * HZ);//(75 * HZ); */
819 return 0;
823 * storvsc_host_reset_handler - Reset the scsi HBA
825 static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
827 int ret;
828 struct host_device_context *host_device_ctx =
829 (struct host_device_context *)scmnd->device->host->hostdata;
830 struct vm_device *device_ctx = host_device_ctx->device_ctx;
832 DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host resetting...",
833 scmnd->device, &device_ctx->device_obj);
835 /* Invokes the vsc to reset the host/bus */
836 ret = StorVscOnHostReset(&device_ctx->device_obj);
837 if (ret != 0)
838 return ret;
840 DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host reseted",
841 scmnd->device, &device_ctx->device_obj);
843 return ret;
846 static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
847 sector_t capacity, int *info)
849 sector_t total_sectors = capacity;
850 sector_t cylinder_times_heads = 0;
851 sector_t temp = 0;
853 int sectors_per_track = 0;
854 int heads = 0;
855 int cylinders = 0;
856 int rem = 0;
858 if (total_sectors > (65535 * 16 * 255))
859 total_sectors = (65535 * 16 * 255);
861 if (total_sectors >= (65535 * 16 * 63)) {
862 sectors_per_track = 255;
863 heads = 16;
865 cylinder_times_heads = total_sectors;
866 /* sector_div stores the quotient in cylinder_times_heads */
867 rem = sector_div(cylinder_times_heads, sectors_per_track);
868 } else {
869 sectors_per_track = 17;
871 cylinder_times_heads = total_sectors;
872 /* sector_div stores the quotient in cylinder_times_heads */
873 rem = sector_div(cylinder_times_heads, sectors_per_track);
875 temp = cylinder_times_heads + 1023;
876 /* sector_div stores the quotient in temp */
877 rem = sector_div(temp, 1024);
879 heads = temp;
881 if (heads < 4)
882 heads = 4;
884 if (cylinder_times_heads >= (heads * 1024) || (heads > 16)) {
885 sectors_per_track = 31;
886 heads = 16;
888 cylinder_times_heads = total_sectors;
890 * sector_div stores the quotient in
891 * cylinder_times_heads
893 rem = sector_div(cylinder_times_heads,
894 sectors_per_track);
897 if (cylinder_times_heads >= (heads * 1024)) {
898 sectors_per_track = 63;
899 heads = 16;
901 cylinder_times_heads = total_sectors;
903 * sector_div stores the quotient in
904 * cylinder_times_heads
906 rem = sector_div(cylinder_times_heads,
907 sectors_per_track);
911 temp = cylinder_times_heads;
912 /* sector_div stores the quotient in temp */
913 rem = sector_div(temp, heads);
914 cylinders = temp;
916 info[0] = heads;
917 info[1] = sectors_per_track;
918 info[2] = cylinders;
920 DPRINT_INFO(STORVSC_DRV, "CHS (%d, %d, %d)", cylinders, heads,
921 sectors_per_track);
923 return 0;
926 static int __init storvsc_init(void)
928 int ret;
930 DPRINT_INFO(STORVSC_DRV, "Storvsc initializing....");
931 ret = storvsc_drv_init(StorVscInitialize);
932 return ret;
935 static void __exit storvsc_exit(void)
937 storvsc_drv_exit();
940 MODULE_LICENSE("GPL");
941 MODULE_VERSION(HV_DRV_VERSION);
942 MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
943 module_init(storvsc_init);
944 module_exit(storvsc_exit);