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
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.
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
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>
36 #include "StorVscApi.h"
39 struct host_device_context {
41 * FIXME this is a bug */
42 struct work_struct host_rescan_work;
44 /* point back to our device context */
45 struct device_context *device_ctx;
46 struct kmem_cache *request_pool;
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
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;
74 static int storvsc_probe(struct device *dev);
75 static int storvsc_queuecommand(struct scsi_cmnd *scmnd,
76 void (*done)(struct scsi_cmnd *));
77 static int storvsc_device_alloc(struct scsi_device *);
78 static int storvsc_device_configure(struct scsi_device *);
79 static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd);
80 static void storvsc_host_rescan_callback(struct work_struct *work);
81 static void storvsc_host_rescan(struct hv_device *device_obj);
82 static int storvsc_remove(struct device *dev);
84 static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
85 unsigned int sg_count,
87 static void destroy_bounce_buffer(struct scatterlist *sgl,
88 unsigned int sg_count);
89 static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count);
90 static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
91 struct scatterlist *bounce_sgl,
92 unsigned int orig_sgl_count);
93 static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
94 struct scatterlist *bounce_sgl,
95 unsigned int orig_sgl_count);
97 static int storvsc_report_luns(struct scsi_device *sdev, unsigned int luns[],
98 unsigned int *lun_count);
99 static int storvsc_get_chs(struct scsi_device *sdev, struct block_device *bdev,
100 sector_t capacity, int *info);
103 static int storvsc_ringbuffer_size = STORVSC_RING_BUFFER_SIZE;
105 /* The one and only one */
106 static struct storvsc_driver_context g_storvsc_drv;
109 static struct scsi_host_template scsi_driver = {
110 .module = THIS_MODULE,
111 .name = "storvsc_host_t",
112 .bios_param = storvsc_get_chs,
113 .queuecommand = storvsc_queuecommand,
114 .eh_host_reset_handler = storvsc_host_reset_handler,
115 .slave_alloc = storvsc_device_alloc,
116 .slave_configure = storvsc_device_configure,
118 /* 64 max_queue * 1 target */
119 .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS,
121 /* no use setting to 0 since ll_blk_rw reset it to 1 */
123 .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT,
125 * ENABLE_CLUSTERING allows mutiple physically contig bio_vecs to merge
126 * into 1 sg element. If set, we must limit the max_segment_size to
127 * PAGE_SIZE, otherwise we may get 1 sg element that represents
130 /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */
131 .use_clustering = ENABLE_CLUSTERING,
132 /* Make sure we dont get a sg segment crosses a page boundary */
133 .dma_boundary = PAGE_SIZE-1,
138 * storvsc_drv_init - StorVsc driver initialization.
140 static int storvsc_drv_init(int (*drv_init)(struct hv_driver *drv))
143 struct storvsc_driver_object *storvsc_drv_obj = &g_storvsc_drv.drv_obj;
144 struct driver_context *drv_ctx = &g_storvsc_drv.drv_ctx;
146 DPRINT_ENTER(STORVSC_DRV);
148 vmbus_get_interface(&storvsc_drv_obj->Base.VmbusChannelInterface);
150 storvsc_drv_obj->RingBufferSize = storvsc_ringbuffer_size;
151 storvsc_drv_obj->OnHostRescan = storvsc_host_rescan;
153 /* Callback to client driver to complete the initialization */
154 drv_init(&storvsc_drv_obj->Base);
156 DPRINT_INFO(STORVSC_DRV,
157 "request extension size %u, max outstanding reqs %u",
158 storvsc_drv_obj->RequestExtSize,
159 storvsc_drv_obj->MaxOutstandingRequestsPerChannel);
161 if (storvsc_drv_obj->MaxOutstandingRequestsPerChannel <
162 STORVSC_MAX_IO_REQUESTS) {
163 DPRINT_ERR(STORVSC_DRV,
164 "The number of outstanding io requests (%d) "
165 "is larger than that supported (%d) internally.",
166 STORVSC_MAX_IO_REQUESTS,
167 storvsc_drv_obj->MaxOutstandingRequestsPerChannel);
171 drv_ctx->driver.name = storvsc_drv_obj->Base.name;
172 memcpy(&drv_ctx->class_id, &storvsc_drv_obj->Base.deviceType,
173 sizeof(struct hv_guid));
175 drv_ctx->probe = storvsc_probe;
176 drv_ctx->remove = storvsc_remove;
178 /* The driver belongs to vmbus */
179 ret = vmbus_child_driver_register(drv_ctx);
181 DPRINT_EXIT(STORVSC_DRV);
186 static int storvsc_drv_exit_cb(struct device *dev, void *data)
188 struct device **curr = (struct device **)data;
190 return 1; /* stop iterating */
193 static void storvsc_drv_exit(void)
195 struct storvsc_driver_object *storvsc_drv_obj = &g_storvsc_drv.drv_obj;
196 struct driver_context *drv_ctx = &g_storvsc_drv.drv_ctx;
197 struct device *current_dev = NULL;
200 DPRINT_ENTER(STORVSC_DRV);
206 ret = driver_for_each_device(&drv_ctx->driver, NULL,
207 (void *) ¤t_dev,
208 storvsc_drv_exit_cb);
211 DPRINT_WARN(STORVSC_DRV,
212 "driver_for_each_device returned %d", ret);
214 if (current_dev == NULL)
217 /* Initiate removal from the top-down */
218 device_unregister(current_dev);
221 if (storvsc_drv_obj->Base.OnCleanup)
222 storvsc_drv_obj->Base.OnCleanup(&storvsc_drv_obj->Base);
224 vmbus_child_driver_unregister(drv_ctx);
226 DPRINT_EXIT(STORVSC_DRV);
232 * storvsc_probe - Add a new device for this driver
234 static int storvsc_probe(struct device *device)
237 struct driver_context *driver_ctx =
238 driver_to_driver_context(device->driver);
239 struct storvsc_driver_context *storvsc_drv_ctx =
240 (struct storvsc_driver_context *)driver_ctx;
241 struct storvsc_driver_object *storvsc_drv_obj =
242 &storvsc_drv_ctx->drv_obj;
243 struct device_context *device_ctx = device_to_device_context(device);
244 struct hv_device *device_obj = &device_ctx->device_obj;
245 struct Scsi_Host *host;
246 struct host_device_context *host_device_ctx;
247 struct storvsc_device_info device_info;
249 DPRINT_ENTER(STORVSC_DRV);
251 if (!storvsc_drv_obj->Base.OnDeviceAdd)
254 host = scsi_host_alloc(&scsi_driver,
255 sizeof(struct host_device_context));
257 DPRINT_ERR(STORVSC_DRV, "unable to allocate scsi host object");
261 dev_set_drvdata(device, host);
263 host_device_ctx = (struct host_device_context *)host->hostdata;
264 memset(host_device_ctx, 0, sizeof(struct host_device_context));
266 host_device_ctx->port = host->host_no;
267 host_device_ctx->device_ctx = device_ctx;
269 INIT_WORK(&host_device_ctx->host_rescan_work,
270 storvsc_host_rescan_callback);
272 host_device_ctx->request_pool =
273 kmem_cache_create(dev_name(&device_ctx->device),
274 sizeof(struct storvsc_cmd_request) +
275 storvsc_drv_obj->RequestExtSize, 0,
276 SLAB_HWCACHE_ALIGN, NULL);
278 if (!host_device_ctx->request_pool) {
280 DPRINT_EXIT(STORVSC_DRV);
285 device_info.PortNumber = host->host_no;
286 /* Call to the vsc driver to add the device */
287 ret = storvsc_drv_obj->Base.OnDeviceAdd(device_obj,
288 (void *)&device_info);
290 DPRINT_ERR(STORVSC_DRV, "unable to add scsi vsc device");
291 kmem_cache_destroy(host_device_ctx->request_pool);
293 DPRINT_EXIT(STORVSC_DRV);
298 /* host_device_ctx->port = device_info.PortNumber; */
299 host_device_ctx->path = device_info.PathId;
300 host_device_ctx->target = device_info.TargetId;
302 /* max # of devices per target */
303 host->max_lun = STORVSC_MAX_LUNS_PER_TARGET;
304 /* max # of targets per channel */
305 host->max_id = STORVSC_MAX_TARGETS;
306 /* max # of channels */
307 host->max_channel = STORVSC_MAX_CHANNELS - 1;
309 /* Register the HBA and start the scsi bus scan */
310 ret = scsi_add_host(host, device);
312 DPRINT_ERR(STORVSC_DRV, "unable to add scsi host device");
314 storvsc_drv_obj->Base.OnDeviceRemove(device_obj);
316 kmem_cache_destroy(host_device_ctx->request_pool);
318 DPRINT_EXIT(STORVSC_DRV);
323 scsi_scan_host(host);
325 DPRINT_EXIT(STORVSC_DRV);
331 * storvsc_remove - Callback when our device is removed
333 static int storvsc_remove(struct device *device)
336 struct driver_context *driver_ctx =
337 driver_to_driver_context(device->driver);
338 struct storvsc_driver_context *storvsc_drv_ctx =
339 (struct storvsc_driver_context *)driver_ctx;
340 struct storvsc_driver_object *storvsc_drv_obj =
341 &storvsc_drv_ctx->drv_obj;
342 struct device_context *device_ctx = device_to_device_context(device);
343 struct hv_device *device_obj = &device_ctx->device_obj;
344 struct Scsi_Host *host = dev_get_drvdata(device);
345 struct host_device_context *host_device_ctx =
346 (struct host_device_context *)host->hostdata;
349 DPRINT_ENTER(STORVSC_DRV);
351 if (!storvsc_drv_obj->Base.OnDeviceRemove) {
352 DPRINT_EXIT(STORVSC_DRV);
357 * Call to the vsc driver to let it know that the device is being
360 ret = storvsc_drv_obj->Base.OnDeviceRemove(device_obj);
363 DPRINT_ERR(STORVSC, "unable to remove vsc device (ret %d)",
367 if (host_device_ctx->request_pool) {
368 kmem_cache_destroy(host_device_ctx->request_pool);
369 host_device_ctx->request_pool = NULL;
372 DPRINT_INFO(STORVSC, "removing host adapter (%p)...", host);
373 scsi_remove_host(host);
375 DPRINT_INFO(STORVSC, "releasing host adapter (%p)...", host);
378 DPRINT_EXIT(STORVSC_DRV);
384 * storvsc_commmand_completion - Command completion processing
386 static void storvsc_commmand_completion(struct hv_storvsc_request *request)
388 struct storvsc_cmd_request *cmd_request =
389 (struct storvsc_cmd_request *)request->Context;
390 struct scsi_cmnd *scmnd = cmd_request->cmd;
391 struct host_device_context *host_device_ctx =
392 (struct host_device_context *)scmnd->device->host->hostdata;
393 void (*scsi_done_fn)(struct scsi_cmnd *);
394 struct scsi_sense_hdr sense_hdr;
396 ASSERT(request == &cmd_request->request);
397 ASSERT((unsigned long)scmnd->host_scribble ==
398 (unsigned long)cmd_request);
400 ASSERT(scmnd->scsi_done);
402 DPRINT_ENTER(STORVSC_DRV);
404 if (cmd_request->bounce_sgl_count) {
405 /* using bounce buffer */
406 /* printk("copy_from_bounce_buffer\n"); */
408 /* FIXME: We can optimize on writes by just skipping this */
409 copy_from_bounce_buffer(scsi_sglist(scmnd),
410 cmd_request->bounce_sgl,
411 scsi_sg_count(scmnd));
412 destroy_bounce_buffer(cmd_request->bounce_sgl,
413 cmd_request->bounce_sgl_count);
416 scmnd->result = request->Status;
419 if (scsi_normalize_sense(scmnd->sense_buffer,
420 request->SenseBufferSize, &sense_hdr))
421 scsi_print_sense_hdr("storvsc", &sense_hdr);
424 ASSERT(request->BytesXfer <= request->DataBuffer.Length);
425 scsi_set_resid(scmnd, request->DataBuffer.Length - request->BytesXfer);
427 scsi_done_fn = scmnd->scsi_done;
429 scmnd->host_scribble = NULL;
430 scmnd->scsi_done = NULL;
432 /* !!DO NOT MODIFY the scmnd after this call */
435 kmem_cache_free(host_device_ctx->request_pool, cmd_request);
437 DPRINT_EXIT(STORVSC_DRV);
440 static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
444 /* No need to check */
448 /* We have at least 2 sg entries */
449 for (i = 0; i < sg_count; i++) {
451 /* make sure 1st one does not have hole */
452 if (sgl[i].offset + sgl[i].length != PAGE_SIZE)
454 } else if (i == sg_count - 1) {
455 /* make sure last one does not have hole */
456 if (sgl[i].offset != 0)
459 /* make sure no hole in the middle */
460 if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0)
467 static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
468 unsigned int sg_count,
473 struct scatterlist *bounce_sgl;
474 struct page *page_buf;
476 num_pages = ALIGN_UP(len, PAGE_SIZE) >> PAGE_SHIFT;
478 bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC);
482 for (i = 0; i < num_pages; i++) {
483 page_buf = alloc_page(GFP_ATOMIC);
486 sg_set_page(&bounce_sgl[i], page_buf, 0, 0);
492 destroy_bounce_buffer(bounce_sgl, num_pages);
496 static void destroy_bounce_buffer(struct scatterlist *sgl,
497 unsigned int sg_count)
500 struct page *page_buf;
502 for (i = 0; i < sg_count; i++) {
503 page_buf = sg_page((&sgl[i]));
504 if (page_buf != NULL)
505 __free_page(page_buf);
511 /* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
512 static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
513 struct scatterlist *bounce_sgl,
514 unsigned int orig_sgl_count)
518 unsigned long src, dest;
519 unsigned int srclen, destlen, copylen;
520 unsigned int total_copied = 0;
521 unsigned long bounce_addr = 0;
522 unsigned long src_addr = 0;
525 local_irq_save(flags);
527 for (i = 0; i < orig_sgl_count; i++) {
528 src_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
529 KM_IRQ0) + orig_sgl[i].offset;
531 srclen = orig_sgl[i].length;
533 ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE);
536 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
539 /* assume bounce offset always == 0 */
540 dest = bounce_addr + bounce_sgl[j].length;
541 destlen = PAGE_SIZE - bounce_sgl[j].length;
543 copylen = min(srclen, destlen);
544 memcpy((void *)dest, (void *)src, copylen);
546 total_copied += copylen;
547 bounce_sgl[j].length += copylen;
551 if (bounce_sgl[j].length == PAGE_SIZE) {
552 /* full..move to next entry */
553 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
556 /* if we need to use another bounce buffer */
557 if (srclen || i != orig_sgl_count - 1)
558 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
559 } else if (srclen == 0 && i == orig_sgl_count - 1) {
560 /* unmap the last bounce that is < PAGE_SIZE */
561 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
565 kunmap_atomic((void *)(src_addr - orig_sgl[i].offset), KM_IRQ0);
568 local_irq_restore(flags);
573 /* Assume the original sgl has enough room */
574 static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
575 struct scatterlist *bounce_sgl,
576 unsigned int orig_sgl_count)
580 unsigned long src, dest;
581 unsigned int srclen, destlen, copylen;
582 unsigned int total_copied = 0;
583 unsigned long bounce_addr = 0;
584 unsigned long dest_addr = 0;
587 local_irq_save(flags);
589 for (i = 0; i < orig_sgl_count; i++) {
590 dest_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
591 KM_IRQ0) + orig_sgl[i].offset;
593 destlen = orig_sgl[i].length;
594 ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE);
597 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
600 src = bounce_addr + bounce_sgl[j].offset;
601 srclen = bounce_sgl[j].length - bounce_sgl[j].offset;
603 copylen = min(srclen, destlen);
604 memcpy((void *)dest, (void *)src, copylen);
606 total_copied += copylen;
607 bounce_sgl[j].offset += copylen;
611 if (bounce_sgl[j].offset == bounce_sgl[j].length) {
613 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
616 /* if we need to use another bounce buffer */
617 if (destlen || i != orig_sgl_count - 1)
618 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
619 } else if (destlen == 0 && i == orig_sgl_count - 1) {
620 /* unmap the last bounce that is < PAGE_SIZE */
621 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
625 kunmap_atomic((void *)(dest_addr - orig_sgl[i].offset),
629 local_irq_restore(flags);
635 * storvsc_queuecommand - Initiate command processing
637 static int storvsc_queuecommand(struct scsi_cmnd *scmnd,
638 void (*done)(struct scsi_cmnd *))
641 struct host_device_context *host_device_ctx =
642 (struct host_device_context *)scmnd->device->host->hostdata;
643 struct device_context *device_ctx = host_device_ctx->device_ctx;
644 struct driver_context *driver_ctx =
645 driver_to_driver_context(device_ctx->device.driver);
646 struct storvsc_driver_context *storvsc_drv_ctx =
647 (struct storvsc_driver_context *)driver_ctx;
648 struct storvsc_driver_object *storvsc_drv_obj =
649 &storvsc_drv_ctx->drv_obj;
650 struct hv_storvsc_request *request;
651 struct storvsc_cmd_request *cmd_request;
652 unsigned int request_size = 0;
654 struct scatterlist *sgl;
656 DPRINT_ENTER(STORVSC_DRV);
658 DPRINT_DBG(STORVSC_DRV, "scmnd %p dir %d, use_sg %d buf %p len %d "
659 "queue depth %d tagged %d", scmnd, scmnd->sc_data_direction,
660 scsi_sg_count(scmnd), scsi_sglist(scmnd),
661 scsi_bufflen(scmnd), scmnd->device->queue_depth,
662 scmnd->device->tagged_supported);
664 /* If retrying, no need to prep the cmd */
665 if (scmnd->host_scribble) {
666 ASSERT(scmnd->scsi_done != NULL);
669 (struct storvsc_cmd_request *)scmnd->host_scribble;
670 DPRINT_INFO(STORVSC_DRV, "retrying scmnd %p cmd_request %p",
676 ASSERT(scmnd->scsi_done == NULL);
677 ASSERT(scmnd->host_scribble == NULL);
679 scmnd->scsi_done = done;
681 request_size = sizeof(struct storvsc_cmd_request);
683 cmd_request = kmem_cache_alloc(host_device_ctx->request_pool,
686 DPRINT_ERR(STORVSC_DRV, "scmnd (%p) - unable to allocate "
687 "storvsc_cmd_request...marking queue busy", scmnd);
688 scmnd->scsi_done = NULL;
689 return SCSI_MLQUEUE_DEVICE_BUSY;
692 /* Setup the cmd request */
693 cmd_request->bounce_sgl_count = 0;
694 cmd_request->bounce_sgl = NULL;
695 cmd_request->cmd = scmnd;
697 scmnd->host_scribble = (unsigned char *)cmd_request;
699 request = &cmd_request->request;
702 (void *)((unsigned long)cmd_request + request_size);
703 DPRINT_DBG(STORVSC_DRV, "req %p size %d ext %d", request, request_size,
704 storvsc_drv_obj->RequestExtSize);
707 switch (scmnd->sc_data_direction) {
709 request->Type = WRITE_TYPE;
711 case DMA_FROM_DEVICE:
712 request->Type = READ_TYPE;
715 request->Type = UNKNOWN_TYPE;
719 request->OnIOCompletion = storvsc_commmand_completion;
720 request->Context = cmd_request;/* scmnd; */
722 /* request->PortId = scmnd->device->channel; */
723 request->Host = host_device_ctx->port;
724 request->Bus = scmnd->device->channel;
725 request->TargetId = scmnd->device->id;
726 request->LunId = scmnd->device->lun;
728 ASSERT(scmnd->cmd_len <= 16);
729 request->CdbLen = scmnd->cmd_len;
730 request->Cdb = scmnd->cmnd;
732 request->SenseBuffer = scmnd->sense_buffer;
733 request->SenseBufferSize = SCSI_SENSE_BUFFERSIZE;
736 request->DataBuffer.Length = scsi_bufflen(scmnd);
737 if (scsi_sg_count(scmnd)) {
738 sgl = (struct scatterlist *)scsi_sglist(scmnd);
740 /* check if we need to bounce the sgl */
741 if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) {
742 DPRINT_INFO(STORVSC_DRV,
743 "need to bounce buffer for this scmnd %p",
745 cmd_request->bounce_sgl =
746 create_bounce_buffer(sgl, scsi_sg_count(scmnd),
747 scsi_bufflen(scmnd));
748 if (!cmd_request->bounce_sgl) {
749 DPRINT_ERR(STORVSC_DRV,
750 "unable to create bounce buffer for "
751 "this scmnd %p", scmnd);
753 scmnd->scsi_done = NULL;
754 scmnd->host_scribble = NULL;
755 kmem_cache_free(host_device_ctx->request_pool,
758 return SCSI_MLQUEUE_HOST_BUSY;
761 cmd_request->bounce_sgl_count =
762 ALIGN_UP(scsi_bufflen(scmnd), PAGE_SIZE) >>
766 * FIXME: We can optimize on reads by just skipping
769 copy_to_bounce_buffer(sgl, cmd_request->bounce_sgl,
770 scsi_sg_count(scmnd));
772 sgl = cmd_request->bounce_sgl;
775 request->DataBuffer.Offset = sgl[0].offset;
777 for (i = 0; i < scsi_sg_count(scmnd); i++) {
778 DPRINT_DBG(STORVSC_DRV, "sgl[%d] len %d offset %d \n",
779 i, sgl[i].length, sgl[i].offset);
780 request->DataBuffer.PfnArray[i] =
781 page_to_pfn(sg_page((&sgl[i])));
783 } else if (scsi_sglist(scmnd)) {
784 ASSERT(scsi_bufflen(scmnd) <= PAGE_SIZE);
785 request->DataBuffer.Offset =
786 virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
787 request->DataBuffer.PfnArray[0] =
788 virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
790 ASSERT(scsi_bufflen(scmnd) == 0);
794 /* Invokes the vsc to start an IO */
795 ret = storvsc_drv_obj->OnIORequest(&device_ctx->device_obj,
796 &cmd_request->request);
799 DPRINT_ERR(STORVSC_DRV,
800 "scmnd (%p) - queue FULL...marking queue busy",
803 if (cmd_request->bounce_sgl_count) {
805 * FIXME: We can optimize on writes by just skipping
808 copy_from_bounce_buffer(scsi_sglist(scmnd),
809 cmd_request->bounce_sgl,
810 scsi_sg_count(scmnd));
811 destroy_bounce_buffer(cmd_request->bounce_sgl,
812 cmd_request->bounce_sgl_count);
815 kmem_cache_free(host_device_ctx->request_pool, cmd_request);
817 scmnd->scsi_done = NULL;
818 scmnd->host_scribble = NULL;
820 ret = SCSI_MLQUEUE_DEVICE_BUSY;
823 DPRINT_EXIT(STORVSC_DRV);
828 static int storvsc_merge_bvec(struct request_queue *q,
829 struct bvec_merge_data *bmd, struct bio_vec *bvec)
831 /* checking done by caller. */
836 * storvsc_device_configure - Configure the specified scsi device
838 static int storvsc_device_alloc(struct scsi_device *sdevice)
840 DPRINT_DBG(STORVSC_DRV, "sdev (%p) - setting device flag to %d",
841 sdevice, BLIST_SPARSELUN);
843 * This enables luns to be located sparsely. Otherwise, we may not
846 sdevice->sdev_bflags |= BLIST_SPARSELUN | BLIST_LARGELUN;
850 static int storvsc_device_configure(struct scsi_device *sdevice)
852 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - curr queue depth %d", sdevice,
853 sdevice->queue_depth);
855 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - setting queue depth to %d",
856 sdevice, STORVSC_MAX_IO_REQUESTS);
857 scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG,
858 STORVSC_MAX_IO_REQUESTS);
860 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - setting max segment size to %ld",
862 blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
864 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - adding merge bio vec routine",
866 blk_queue_merge_bvec(sdevice->request_queue, storvsc_merge_bvec);
868 blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
869 /* sdevice->timeout = (2000 * HZ);//(75 * HZ); */
875 * storvsc_host_reset_handler - Reset the scsi HBA
877 static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
880 struct host_device_context *host_device_ctx =
881 (struct host_device_context *)scmnd->device->host->hostdata;
882 struct device_context *device_ctx = host_device_ctx->device_ctx;
883 struct driver_context *driver_ctx =
884 driver_to_driver_context(device_ctx->device.driver);
885 struct storvsc_driver_context *storvsc_drv_ctx =
886 (struct storvsc_driver_context *)driver_ctx;
888 struct storvsc_driver_object *storvsc_drv_obj =
889 &storvsc_drv_ctx->drv_obj;
891 DPRINT_ENTER(STORVSC_DRV);
893 DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host resetting...",
894 scmnd->device, &device_ctx->device_obj);
896 /* Invokes the vsc to reset the host/bus */
897 ASSERT(storvsc_drv_obj->OnHostReset);
898 ret = storvsc_drv_obj->OnHostReset(&device_ctx->device_obj);
900 DPRINT_EXIT(STORVSC_DRV);
904 DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host reseted",
905 scmnd->device, &device_ctx->device_obj);
907 DPRINT_EXIT(STORVSC_DRV);
913 * storvsc_host_rescan - Rescan the scsi HBA
915 static void storvsc_host_rescan_callback(struct work_struct *work)
917 struct hv_device *device_obj =
918 &((struct host_device_context *)work)->device_ctx->device_obj;
919 struct device_context *device_ctx = to_device_context(device_obj);
920 struct Scsi_Host *host = dev_get_drvdata(&device_ctx->device);
921 struct scsi_device *sdev;
922 struct host_device_context *host_device_ctx;
923 struct scsi_device **sdevs_remove_list;
924 unsigned int sdevs_count = 0;
927 unsigned int lun_count = 0;
928 unsigned int *lun_list;
930 DPRINT_ENTER(STORVSC_DRV);
932 host_device_ctx = (struct host_device_context *)host->hostdata;
933 lun_list = kcalloc(STORVSC_MAX_LUNS_PER_TARGET, sizeof(unsigned int),
936 DPRINT_ERR(STORVSC_DRV, "unable to allocate lun list");
940 sdevs_remove_list = kcalloc(STORVSC_MAX_LUNS_PER_TARGET,
941 sizeof(void *), GFP_ATOMIC);
942 if (!sdevs_remove_list) {
944 DPRINT_ERR(STORVSC_DRV, "unable to allocate lun remove list");
948 DPRINT_INFO(STORVSC_DRV, "rescanning host for new scsi devices...");
950 /* Rescan for new device */
951 scsi_scan_target(&host->shost_gendev, host_device_ctx->path,
952 host_device_ctx->target, SCAN_WILD_CARD, 1);
954 DPRINT_INFO(STORVSC_DRV, "rescanning host for removed scsi device...");
956 /* Use the 1st device to send the report luns cmd */
957 shost_for_each_device(sdev, host) {
958 lun_count = STORVSC_MAX_LUNS_PER_TARGET;
959 storvsc_report_luns(sdev, lun_list, &lun_count);
961 DPRINT_INFO(STORVSC_DRV,
962 "report luns on scsi device (%p) found %u luns ",
964 DPRINT_INFO(STORVSC_DRV,
965 "existing luns on scsi device (%p) host (%d)",
966 sdev, host->host_no);
968 scsi_device_put(sdev);
972 for (i = 0; i < lun_count; i++)
973 DPRINT_INFO(STORVSC_DRV, "%d) lun %u", i, lun_list[i]);
975 /* Rescan for devices that may have been removed.
976 * We do not have to worry that new devices may have been added since
977 * this callback is serialized by the workqueue ie add/remove are done
980 shost_for_each_device(sdev, host) {
981 /* See if this device is still here */
983 for (i = 0; i < lun_count; i++) {
984 if (sdev->lun == lun_list[i]) {
990 DPRINT_INFO(STORVSC_DRV, "lun (%u) does not exists",
992 sdevs_remove_list[sdevs_count++] = sdev;
996 /* Now remove the devices */
997 for (i = 0; i < sdevs_count; i++) {
998 DPRINT_INFO(STORVSC_DRV,
999 "removing scsi device (%p) lun (%u)...",
1000 sdevs_remove_list[i], sdevs_remove_list[i]->lun);
1002 /* make sure it is not removed from underneath us */
1003 if (!scsi_device_get(sdevs_remove_list[i])) {
1004 scsi_remove_device(sdevs_remove_list[i]);
1005 scsi_device_put(sdevs_remove_list[i]);
1009 DPRINT_INFO(STORVSC_DRV, "rescan completed on dev obj (%p) "
1010 "target (%u) bus (%u)", device_obj,
1011 host_device_ctx->target, host_device_ctx->path);
1014 kfree(sdevs_remove_list);
1016 DPRINT_EXIT(STORVSC_DRV);
1019 static int storvsc_report_luns(struct scsi_device *sdev, unsigned int luns[],
1020 unsigned int *lun_count)
1023 unsigned int lun = 0;
1024 unsigned int num_luns;
1026 unsigned char *data;
1027 struct scsi_sense_hdr sshdr;
1028 unsigned char cmd[16] = {0};
1029 /* Add 1 to cover the report_lun header */
1030 unsigned int report_len = 8 * (STORVSC_MAX_LUNS_PER_TARGET+1);
1031 unsigned long long *report_luns;
1032 const unsigned int in_lun_count = *lun_count;
1036 report_luns = kzalloc(report_len, GFP_ATOMIC);
1040 cmd[0] = REPORT_LUNS;
1043 *(unsigned int *)&cmd[6] = cpu_to_be32(report_len);
1045 result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE,
1046 (unsigned char *)report_luns, report_len,
1047 &sshdr, 30 * HZ, 3, NULL);
1053 /* get the length from the first four bytes */
1054 report_len = be32_to_cpu(*(unsigned int *)&report_luns[0]);
1056 num_luns = (report_len / sizeof(unsigned long long));
1057 if (num_luns > in_lun_count) {
1062 *lun_count = num_luns;
1064 DPRINT_DBG(STORVSC_DRV,
1065 "report luns on scsi device (%p) found %u luns ",
1068 /* lun id starts at 1 */
1069 for (i = 1; i < num_luns + 1; i++) {
1071 data = (unsigned char *)&report_luns[i];
1072 for (j = 0; j < sizeof(lun); j += 2) {
1073 lun = lun | (((data[j] << 8) | data[j + 1]) <<
1084 static void storvsc_host_rescan(struct hv_device *device_obj)
1086 struct device_context *device_ctx = to_device_context(device_obj);
1087 struct Scsi_Host *host = dev_get_drvdata(&device_ctx->device);
1088 struct host_device_context *host_device_ctx;
1090 DPRINT_ENTER(STORVSC_DRV);
1092 host_device_ctx = (struct host_device_context *)host->hostdata;
1094 DPRINT_INFO(STORVSC_DRV, "initiating rescan on dev obj (%p) "
1095 "target (%u) bus (%u)...", device_obj,
1096 host_device_ctx->target, host_device_ctx->path);
1099 * We need to queue this since the scanning may block and the caller
1100 * may be in an intr context
1102 /* scsi_queue_work(host, &host_device_ctx->host_rescan_work); */
1103 schedule_work(&host_device_ctx->host_rescan_work);
1104 DPRINT_EXIT(STORVSC_DRV);
1107 static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
1108 sector_t capacity, int *info)
1110 sector_t total_sectors = capacity;
1111 sector_t cylinder_times_heads = 0;
1114 int sectors_per_track = 0;
1119 if (total_sectors > (65535 * 16 * 255))
1120 total_sectors = (65535 * 16 * 255);
1122 if (total_sectors >= (65535 * 16 * 63)) {
1123 sectors_per_track = 255;
1126 cylinder_times_heads = total_sectors;
1127 /* sector_div stores the quotient in cylinder_times_heads */
1128 rem = sector_div(cylinder_times_heads, sectors_per_track);
1130 sectors_per_track = 17;
1132 cylinder_times_heads = total_sectors;
1133 /* sector_div stores the quotient in cylinder_times_heads */
1134 rem = sector_div(cylinder_times_heads, sectors_per_track);
1136 temp = cylinder_times_heads + 1023;
1137 /* sector_div stores the quotient in temp */
1138 rem = sector_div(temp, 1024);
1145 if (cylinder_times_heads >= (heads * 1024) || (heads > 16)) {
1146 sectors_per_track = 31;
1149 cylinder_times_heads = total_sectors;
1151 * sector_div stores the quotient in
1152 * cylinder_times_heads
1154 rem = sector_div(cylinder_times_heads,
1158 if (cylinder_times_heads >= (heads * 1024)) {
1159 sectors_per_track = 63;
1162 cylinder_times_heads = total_sectors;
1164 * sector_div stores the quotient in
1165 * cylinder_times_heads
1167 rem = sector_div(cylinder_times_heads,
1172 temp = cylinder_times_heads;
1173 /* sector_div stores the quotient in temp */
1174 rem = sector_div(temp, heads);
1178 info[1] = sectors_per_track;
1179 info[2] = cylinders;
1181 DPRINT_INFO(STORVSC_DRV, "CHS (%d, %d, %d)", cylinders, heads,
1187 static int __init storvsc_init(void)
1191 DPRINT_ENTER(STORVSC_DRV);
1192 DPRINT_INFO(STORVSC_DRV, "Storvsc initializing....");
1193 ret = storvsc_drv_init(StorVscInitialize);
1194 DPRINT_EXIT(STORVSC_DRV);
1198 static void __exit storvsc_exit(void)
1200 DPRINT_ENTER(STORVSC_DRV);
1202 DPRINT_ENTER(STORVSC_DRV);
1205 MODULE_LICENSE("GPL");
1206 module_param(storvsc_ringbuffer_size, int, S_IRUGO);
1207 module_init(storvsc_init);
1208 module_exit(storvsc_exit);