Btrfs: fix memory leaks in btrfs_new_inode()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / hv / storvsc_drv.c
blob17f1b344fbc5e4f75591142eb4d0ed6447175015
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 /* 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;
52 struct storvsc_cmd_request {
53 struct list_head entry;
54 struct scsi_cmnd *cmd;
56 unsigned int bounce_sgl_count;
57 struct scatterlist *bounce_sgl;
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... */
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;
73 /* Static decl */
74 static int storvsc_probe(struct device *dev);
75 static int storvsc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *scmnd);
76 static int storvsc_device_alloc(struct scsi_device *);
77 static int storvsc_device_configure(struct scsi_device *);
78 static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd);
79 static int storvsc_remove(struct device *dev);
81 static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
82 unsigned int sg_count,
83 unsigned int len);
84 static void destroy_bounce_buffer(struct scatterlist *sgl,
85 unsigned int sg_count);
86 static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count);
87 static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
88 struct scatterlist *bounce_sgl,
89 unsigned int orig_sgl_count);
90 static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
91 struct scatterlist *bounce_sgl,
92 unsigned int orig_sgl_count);
94 static int storvsc_get_chs(struct scsi_device *sdev, struct block_device *bdev,
95 sector_t capacity, int *info);
98 static int storvsc_ringbuffer_size = STORVSC_RING_BUFFER_SIZE;
99 module_param(storvsc_ringbuffer_size, int, S_IRUGO);
100 MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)");
102 /* The one and only one */
103 static struct storvsc_driver_context g_storvsc_drv;
105 /* Scsi driver */
106 static struct scsi_host_template scsi_driver = {
107 .module = THIS_MODULE,
108 .name = "storvsc_host_t",
109 .bios_param = storvsc_get_chs,
110 .queuecommand = storvsc_queuecommand,
111 .eh_host_reset_handler = storvsc_host_reset_handler,
112 .slave_alloc = storvsc_device_alloc,
113 .slave_configure = storvsc_device_configure,
114 .cmd_per_lun = 1,
115 /* 64 max_queue * 1 target */
116 .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS,
117 .this_id = -1,
118 /* no use setting to 0 since ll_blk_rw reset it to 1 */
119 /* currently 32 */
120 .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT,
122 * ENABLE_CLUSTERING allows mutiple physically contig bio_vecs to merge
123 * into 1 sg element. If set, we must limit the max_segment_size to
124 * PAGE_SIZE, otherwise we may get 1 sg element that represents
125 * multiple
127 /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */
128 .use_clustering = ENABLE_CLUSTERING,
129 /* Make sure we dont get a sg segment crosses a page boundary */
130 .dma_boundary = PAGE_SIZE-1,
135 * storvsc_drv_init - StorVsc driver initialization.
137 static int storvsc_drv_init(int (*drv_init)(struct hv_driver *drv))
139 int ret;
140 struct storvsc_driver_object *storvsc_drv_obj = &g_storvsc_drv.drv_obj;
141 struct driver_context *drv_ctx = &g_storvsc_drv.drv_ctx;
143 storvsc_drv_obj->ring_buffer_size = 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->request_ext_size,
151 storvsc_drv_obj->max_outstanding_req_per_channel);
153 if (storvsc_drv_obj->max_outstanding_req_per_channel <
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->max_outstanding_req_per_channel);
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->request_ext_size, 0,
256 SLAB_HWCACHE_ALIGN, NULL);
258 if (!host_device_ctx->request_pool) {
259 scsi_host_put(host);
260 return -ENOMEM;
263 device_info.port_number = 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.path_id;
276 host_device_ctx->target = device_info.target_id;
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 /* FIXME: We can optimize on writes by just skipping this */
371 copy_from_bounce_buffer(scsi_sglist(scmnd),
372 cmd_request->bounce_sgl,
373 scsi_sg_count(scmnd));
374 destroy_bounce_buffer(cmd_request->bounce_sgl,
375 cmd_request->bounce_sgl_count);
378 scmnd->result = request->status;
380 if (scmnd->result) {
381 if (scsi_normalize_sense(scmnd->sense_buffer,
382 request->sense_buffer_size, &sense_hdr))
383 scsi_print_sense_hdr("storvsc", &sense_hdr);
386 /* ASSERT(request->BytesXfer <= request->data_buffer.Length); */
387 scsi_set_resid(scmnd,
388 request->data_buffer.Length - request->bytes_xfer);
390 scsi_done_fn = scmnd->scsi_done;
392 scmnd->host_scribble = NULL;
393 scmnd->scsi_done = NULL;
395 /* !!DO NOT MODIFY the scmnd after this call */
396 scsi_done_fn(scmnd);
398 kmem_cache_free(host_device_ctx->request_pool, cmd_request);
401 static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
403 int i;
405 /* No need to check */
406 if (sg_count < 2)
407 return -1;
409 /* We have at least 2 sg entries */
410 for (i = 0; i < sg_count; i++) {
411 if (i == 0) {
412 /* make sure 1st one does not have hole */
413 if (sgl[i].offset + sgl[i].length != PAGE_SIZE)
414 return i;
415 } else if (i == sg_count - 1) {
416 /* make sure last one does not have hole */
417 if (sgl[i].offset != 0)
418 return i;
419 } else {
420 /* make sure no hole in the middle */
421 if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0)
422 return i;
425 return -1;
428 static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
429 unsigned int sg_count,
430 unsigned int len)
432 int i;
433 int num_pages;
434 struct scatterlist *bounce_sgl;
435 struct page *page_buf;
437 num_pages = ALIGN_UP(len, PAGE_SIZE) >> PAGE_SHIFT;
439 bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC);
440 if (!bounce_sgl)
441 return NULL;
443 for (i = 0; i < num_pages; i++) {
444 page_buf = alloc_page(GFP_ATOMIC);
445 if (!page_buf)
446 goto cleanup;
447 sg_set_page(&bounce_sgl[i], page_buf, 0, 0);
450 return bounce_sgl;
452 cleanup:
453 destroy_bounce_buffer(bounce_sgl, num_pages);
454 return NULL;
457 static void destroy_bounce_buffer(struct scatterlist *sgl,
458 unsigned int sg_count)
460 int i;
461 struct page *page_buf;
463 for (i = 0; i < sg_count; i++) {
464 page_buf = sg_page((&sgl[i]));
465 if (page_buf != NULL)
466 __free_page(page_buf);
469 kfree(sgl);
472 /* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
473 static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
474 struct scatterlist *bounce_sgl,
475 unsigned int orig_sgl_count)
477 int i;
478 int j = 0;
479 unsigned long src, dest;
480 unsigned int srclen, destlen, copylen;
481 unsigned int total_copied = 0;
482 unsigned long bounce_addr = 0;
483 unsigned long src_addr = 0;
484 unsigned long flags;
486 local_irq_save(flags);
488 for (i = 0; i < orig_sgl_count; i++) {
489 src_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
490 KM_IRQ0) + orig_sgl[i].offset;
491 src = src_addr;
492 srclen = orig_sgl[i].length;
494 /* ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE); */
496 if (bounce_addr == 0)
497 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
499 while (srclen) {
500 /* assume bounce offset always == 0 */
501 dest = bounce_addr + bounce_sgl[j].length;
502 destlen = PAGE_SIZE - bounce_sgl[j].length;
504 copylen = min(srclen, destlen);
505 memcpy((void *)dest, (void *)src, copylen);
507 total_copied += copylen;
508 bounce_sgl[j].length += copylen;
509 srclen -= copylen;
510 src += copylen;
512 if (bounce_sgl[j].length == PAGE_SIZE) {
513 /* full..move to next entry */
514 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
515 j++;
517 /* if we need to use another bounce buffer */
518 if (srclen || i != orig_sgl_count - 1)
519 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
520 } else if (srclen == 0 && i == orig_sgl_count - 1) {
521 /* unmap the last bounce that is < PAGE_SIZE */
522 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
526 kunmap_atomic((void *)(src_addr - orig_sgl[i].offset), KM_IRQ0);
529 local_irq_restore(flags);
531 return total_copied;
534 /* Assume the original sgl has enough room */
535 static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
536 struct scatterlist *bounce_sgl,
537 unsigned int orig_sgl_count)
539 int i;
540 int j = 0;
541 unsigned long src, dest;
542 unsigned int srclen, destlen, copylen;
543 unsigned int total_copied = 0;
544 unsigned long bounce_addr = 0;
545 unsigned long dest_addr = 0;
546 unsigned long flags;
548 local_irq_save(flags);
550 for (i = 0; i < orig_sgl_count; i++) {
551 dest_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
552 KM_IRQ0) + orig_sgl[i].offset;
553 dest = dest_addr;
554 destlen = orig_sgl[i].length;
555 /* ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE); */
557 if (bounce_addr == 0)
558 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
560 while (destlen) {
561 src = bounce_addr + bounce_sgl[j].offset;
562 srclen = bounce_sgl[j].length - bounce_sgl[j].offset;
564 copylen = min(srclen, destlen);
565 memcpy((void *)dest, (void *)src, copylen);
567 total_copied += copylen;
568 bounce_sgl[j].offset += copylen;
569 destlen -= copylen;
570 dest += copylen;
572 if (bounce_sgl[j].offset == bounce_sgl[j].length) {
573 /* full */
574 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
575 j++;
577 /* if we need to use another bounce buffer */
578 if (destlen || i != orig_sgl_count - 1)
579 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
580 } else if (destlen == 0 && i == orig_sgl_count - 1) {
581 /* unmap the last bounce that is < PAGE_SIZE */
582 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
586 kunmap_atomic((void *)(dest_addr - orig_sgl[i].offset),
587 KM_IRQ0);
590 local_irq_restore(flags);
592 return total_copied;
596 * storvsc_queuecommand - Initiate command processing
598 static int storvsc_queuecommand_lck(struct scsi_cmnd *scmnd,
599 void (*done)(struct scsi_cmnd *))
601 int ret;
602 struct host_device_context *host_device_ctx =
603 (struct host_device_context *)scmnd->device->host->hostdata;
604 struct vm_device *device_ctx = host_device_ctx->device_ctx;
605 struct driver_context *driver_ctx =
606 driver_to_driver_context(device_ctx->device.driver);
607 struct storvsc_driver_context *storvsc_drv_ctx =
608 (struct storvsc_driver_context *)driver_ctx;
609 struct storvsc_driver_object *storvsc_drv_obj =
610 &storvsc_drv_ctx->drv_obj;
611 struct hv_storvsc_request *request;
612 struct storvsc_cmd_request *cmd_request;
613 unsigned int request_size = 0;
614 int i;
615 struct scatterlist *sgl;
616 unsigned int sg_count = 0;
618 DPRINT_DBG(STORVSC_DRV, "scmnd %p dir %d, use_sg %d buf %p len %d "
619 "queue depth %d tagged %d", scmnd, scmnd->sc_data_direction,
620 scsi_sg_count(scmnd), scsi_sglist(scmnd),
621 scsi_bufflen(scmnd), scmnd->device->queue_depth,
622 scmnd->device->tagged_supported);
624 /* If retrying, no need to prep the cmd */
625 if (scmnd->host_scribble) {
626 /* ASSERT(scmnd->scsi_done != NULL); */
628 cmd_request =
629 (struct storvsc_cmd_request *)scmnd->host_scribble;
630 DPRINT_INFO(STORVSC_DRV, "retrying scmnd %p cmd_request %p",
631 scmnd, cmd_request);
633 goto retry_request;
636 /* ASSERT(scmnd->scsi_done == NULL); */
637 /* ASSERT(scmnd->host_scribble == NULL); */
639 scmnd->scsi_done = done;
641 request_size = sizeof(struct storvsc_cmd_request);
643 cmd_request = kmem_cache_alloc(host_device_ctx->request_pool,
644 GFP_ATOMIC);
645 if (!cmd_request) {
646 DPRINT_ERR(STORVSC_DRV, "scmnd (%p) - unable to allocate "
647 "storvsc_cmd_request...marking queue busy", scmnd);
648 scmnd->scsi_done = NULL;
649 return SCSI_MLQUEUE_DEVICE_BUSY;
652 /* Setup the cmd request */
653 cmd_request->bounce_sgl_count = 0;
654 cmd_request->bounce_sgl = NULL;
655 cmd_request->cmd = scmnd;
657 scmnd->host_scribble = (unsigned char *)cmd_request;
659 request = &cmd_request->request;
661 request->extension =
662 (void *)((unsigned long)cmd_request + request_size);
663 DPRINT_DBG(STORVSC_DRV, "req %p size %d ext %d", request, request_size,
664 storvsc_drv_obj->request_ext_size);
666 /* Build the SRB */
667 switch (scmnd->sc_data_direction) {
668 case DMA_TO_DEVICE:
669 request->type = WRITE_TYPE;
670 break;
671 case DMA_FROM_DEVICE:
672 request->type = READ_TYPE;
673 break;
674 default:
675 request->type = UNKNOWN_TYPE;
676 break;
679 request->on_io_completion = storvsc_commmand_completion;
680 request->context = cmd_request;/* scmnd; */
682 /* request->PortId = scmnd->device->channel; */
683 request->host = host_device_ctx->port;
684 request->bus = scmnd->device->channel;
685 request->target_id = scmnd->device->id;
686 request->lun_id = scmnd->device->lun;
688 /* ASSERT(scmnd->cmd_len <= 16); */
689 request->cdb_len = scmnd->cmd_len;
690 request->cdb = scmnd->cmnd;
692 request->sense_buffer = scmnd->sense_buffer;
693 request->sense_buffer_size = SCSI_SENSE_BUFFERSIZE;
696 request->data_buffer.Length = scsi_bufflen(scmnd);
697 if (scsi_sg_count(scmnd)) {
698 sgl = (struct scatterlist *)scsi_sglist(scmnd);
699 sg_count = scsi_sg_count(scmnd);
701 /* check if we need to bounce the sgl */
702 if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) {
703 DPRINT_INFO(STORVSC_DRV,
704 "need to bounce buffer for this scmnd %p",
705 scmnd);
706 cmd_request->bounce_sgl =
707 create_bounce_buffer(sgl, scsi_sg_count(scmnd),
708 scsi_bufflen(scmnd));
709 if (!cmd_request->bounce_sgl) {
710 DPRINT_ERR(STORVSC_DRV,
711 "unable to create bounce buffer for "
712 "this scmnd %p", scmnd);
714 scmnd->scsi_done = NULL;
715 scmnd->host_scribble = NULL;
716 kmem_cache_free(host_device_ctx->request_pool,
717 cmd_request);
719 return SCSI_MLQUEUE_HOST_BUSY;
722 cmd_request->bounce_sgl_count =
723 ALIGN_UP(scsi_bufflen(scmnd), PAGE_SIZE) >>
724 PAGE_SHIFT;
727 * FIXME: We can optimize on reads by just skipping
728 * this
730 copy_to_bounce_buffer(sgl, cmd_request->bounce_sgl,
731 scsi_sg_count(scmnd));
733 sgl = cmd_request->bounce_sgl;
734 sg_count = cmd_request->bounce_sgl_count;
737 request->data_buffer.Offset = sgl[0].offset;
739 for (i = 0; i < sg_count; i++) {
740 DPRINT_DBG(STORVSC_DRV, "sgl[%d] len %d offset %d\n",
741 i, sgl[i].length, sgl[i].offset);
742 request->data_buffer.PfnArray[i] =
743 page_to_pfn(sg_page((&sgl[i])));
745 } else if (scsi_sglist(scmnd)) {
746 /* ASSERT(scsi_bufflen(scmnd) <= PAGE_SIZE); */
747 request->data_buffer.Offset =
748 virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
749 request->data_buffer.PfnArray[0] =
750 virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
753 retry_request:
754 /* Invokes the vsc to start an IO */
755 ret = storvsc_drv_obj->on_io_request(&device_ctx->device_obj,
756 &cmd_request->request);
757 if (ret == -1) {
758 /* no more space */
759 DPRINT_ERR(STORVSC_DRV,
760 "scmnd (%p) - queue FULL...marking queue busy",
761 scmnd);
763 if (cmd_request->bounce_sgl_count) {
765 * FIXME: We can optimize on writes by just skipping
766 * this
768 copy_from_bounce_buffer(scsi_sglist(scmnd),
769 cmd_request->bounce_sgl,
770 scsi_sg_count(scmnd));
771 destroy_bounce_buffer(cmd_request->bounce_sgl,
772 cmd_request->bounce_sgl_count);
775 kmem_cache_free(host_device_ctx->request_pool, cmd_request);
777 scmnd->scsi_done = NULL;
778 scmnd->host_scribble = NULL;
780 ret = SCSI_MLQUEUE_DEVICE_BUSY;
783 return ret;
786 static DEF_SCSI_QCMD(storvsc_queuecommand)
788 static int storvsc_merge_bvec(struct request_queue *q,
789 struct bvec_merge_data *bmd, struct bio_vec *bvec)
791 /* checking done by caller. */
792 return bvec->bv_len;
796 * storvsc_device_configure - Configure the specified scsi device
798 static int storvsc_device_alloc(struct scsi_device *sdevice)
800 DPRINT_DBG(STORVSC_DRV, "sdev (%p) - setting device flag to %d",
801 sdevice, BLIST_SPARSELUN);
803 * This enables luns to be located sparsely. Otherwise, we may not
804 * discovered them.
806 sdevice->sdev_bflags |= BLIST_SPARSELUN | BLIST_LARGELUN;
807 return 0;
810 static int storvsc_device_configure(struct scsi_device *sdevice)
812 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - curr queue depth %d", sdevice,
813 sdevice->queue_depth);
815 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - setting queue depth to %d",
816 sdevice, STORVSC_MAX_IO_REQUESTS);
817 scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG,
818 STORVSC_MAX_IO_REQUESTS);
820 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - setting max segment size to %ld",
821 sdevice, PAGE_SIZE);
822 blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
824 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - adding merge bio vec routine",
825 sdevice);
826 blk_queue_merge_bvec(sdevice->request_queue, storvsc_merge_bvec);
828 blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
829 /* sdevice->timeout = (2000 * HZ);//(75 * HZ); */
831 return 0;
835 * storvsc_host_reset_handler - Reset the scsi HBA
837 static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
839 int ret;
840 struct host_device_context *host_device_ctx =
841 (struct host_device_context *)scmnd->device->host->hostdata;
842 struct vm_device *device_ctx = host_device_ctx->device_ctx;
844 DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host resetting...",
845 scmnd->device, &device_ctx->device_obj);
847 /* Invokes the vsc to reset the host/bus */
848 ret = stor_vsc_on_host_reset(&device_ctx->device_obj);
849 if (ret != 0)
850 return ret;
852 DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host reseted",
853 scmnd->device, &device_ctx->device_obj);
855 return ret;
858 static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
859 sector_t capacity, int *info)
861 sector_t total_sectors = capacity;
862 sector_t cylinder_times_heads = 0;
863 sector_t temp = 0;
865 int sectors_per_track = 0;
866 int heads = 0;
867 int cylinders = 0;
868 int rem = 0;
870 if (total_sectors > (65535 * 16 * 255))
871 total_sectors = (65535 * 16 * 255);
873 if (total_sectors >= (65535 * 16 * 63)) {
874 sectors_per_track = 255;
875 heads = 16;
877 cylinder_times_heads = total_sectors;
878 /* sector_div stores the quotient in cylinder_times_heads */
879 rem = sector_div(cylinder_times_heads, sectors_per_track);
880 } else {
881 sectors_per_track = 17;
883 cylinder_times_heads = total_sectors;
884 /* sector_div stores the quotient in cylinder_times_heads */
885 rem = sector_div(cylinder_times_heads, sectors_per_track);
887 temp = cylinder_times_heads + 1023;
888 /* sector_div stores the quotient in temp */
889 rem = sector_div(temp, 1024);
891 heads = temp;
893 if (heads < 4)
894 heads = 4;
896 if (cylinder_times_heads >= (heads * 1024) || (heads > 16)) {
897 sectors_per_track = 31;
898 heads = 16;
900 cylinder_times_heads = total_sectors;
902 * sector_div stores the quotient in
903 * cylinder_times_heads
905 rem = sector_div(cylinder_times_heads,
906 sectors_per_track);
909 if (cylinder_times_heads >= (heads * 1024)) {
910 sectors_per_track = 63;
911 heads = 16;
913 cylinder_times_heads = total_sectors;
915 * sector_div stores the quotient in
916 * cylinder_times_heads
918 rem = sector_div(cylinder_times_heads,
919 sectors_per_track);
923 temp = cylinder_times_heads;
924 /* sector_div stores the quotient in temp */
925 rem = sector_div(temp, heads);
926 cylinders = temp;
928 info[0] = heads;
929 info[1] = sectors_per_track;
930 info[2] = cylinders;
932 DPRINT_INFO(STORVSC_DRV, "CHS (%d, %d, %d)", cylinders, heads,
933 sectors_per_track);
935 return 0;
938 static int __init storvsc_init(void)
940 int ret;
942 DPRINT_INFO(STORVSC_DRV, "Storvsc initializing....");
943 ret = storvsc_drv_init(stor_vsc_initialize);
944 return ret;
947 static void __exit storvsc_exit(void)
949 storvsc_drv_exit();
952 MODULE_LICENSE("GPL");
953 MODULE_VERSION(HV_DRV_VERSION);
954 MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
955 module_init(storvsc_init);
956 module_exit(storvsc_exit);