3 * Copyright (c) 2009, Microsoft Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16 * Place - Suite 330, Boston, MA 02111-1307 USA.
19 * Haiyang Zhang <haiyangz@microsoft.com>
20 * Hank Janssen <hjanssen@microsoft.com>
24 #include <linux/init.h>
25 #include <linux/module.h>
26 #include <linux/device.h>
27 #include <linux/blkdev.h>
29 #include <scsi/scsi.h>
30 #include <scsi/scsi_cmnd.h>
31 #include <scsi/scsi_host.h>
32 #include <scsi/scsi_device.h>
33 #include <scsi/scsi_tcq.h>
34 #include <scsi/scsi_eh.h>
35 #include <scsi/scsi_devinfo.h>
37 #include <scsi/scsi_dbg.h>
42 #include "StorVscApi.h"
51 struct host_device_context {
52 struct work_struct host_rescan_work; /* must be 1st field */
53 struct device_context *device_ctx; /* point back to our device context */
54 struct kmem_cache *request_pool;
60 struct storvsc_cmd_request {
61 struct list_head entry;
62 struct scsi_cmnd *cmd;
64 unsigned int bounce_sgl_count;
65 struct scatterlist *bounce_sgl;
67 struct hv_storvsc_request request;
68 /* !!!DO NOT ADD ANYTHING BELOW HERE!!! */
69 /* The extension buffer falls right here and is pointed to by request.Extension; */
72 struct storvsc_driver_context {
73 /* !! These must be the first 2 fields !! */
74 struct driver_context drv_ctx;
75 struct storvsc_driver_object drv_obj;
79 static int storvsc_probe(struct device *dev);
80 static int storvsc_queuecommand(struct scsi_cmnd *scmnd, void (*done)(struct scsi_cmnd *));
81 static int storvsc_device_alloc(struct scsi_device *);
82 static int storvsc_device_configure(struct scsi_device *);
83 static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd);
84 static void storvsc_host_rescan_callback(struct work_struct *work);
85 static void storvsc_host_rescan(struct hv_device* device_obj);
86 static int storvsc_remove(struct device *dev);
88 static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count, unsigned int len);
89 static void destroy_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count);
90 static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count);
91 static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl, struct scatterlist *bounce_sgl, unsigned int orig_sgl_count);
92 static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl, struct scatterlist *bounce_sgl, unsigned int orig_sgl_count);
94 static int storvsc_report_luns(struct scsi_device *sdev, unsigned int luns[], unsigned int *lun_count);
95 static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev, sector_t capacity, int *info);
98 static int storvsc_ringbuffer_size = STORVSC_RING_BUFFER_SIZE;
100 /* The one and only one */
101 static struct storvsc_driver_context g_storvsc_drv;
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,
113 .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS, /* 64 max_queue * 1 target */
115 /* no use setting to 0 since ll_blk_rw reset it to 1 */
116 .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT,/* currently 32 */
117 /* ENABLE_CLUSTERING allows mutiple physically contig bio_vecs to merge into 1 sg element. If set, we must */
118 /* limit the max_segment_size to PAGE_SIZE, otherwise we may get 1 sg element that represents multiple */
119 /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */
120 .use_clustering = ENABLE_CLUSTERING,
121 /* Make sure we dont get a sg segment crosses a page boundary */
122 .dma_boundary = PAGE_SIZE-1,
128 Name: storvsc_drv_init()
130 Desc: StorVsc driver initialization.
133 static int storvsc_drv_init(PFN_DRIVERINITIALIZE pfn_drv_init)
136 struct storvsc_driver_object *storvsc_drv_obj = &g_storvsc_drv.drv_obj;
137 struct driver_context *drv_ctx=&g_storvsc_drv.drv_ctx;
139 DPRINT_ENTER(STORVSC_DRV);
141 vmbus_get_interface(&storvsc_drv_obj->Base.VmbusChannelInterface);
143 storvsc_drv_obj->RingBufferSize = storvsc_ringbuffer_size;
144 storvsc_drv_obj->OnHostRescan = storvsc_host_rescan;
146 /* Callback to client driver to complete the initialization */
147 pfn_drv_init(&storvsc_drv_obj->Base);
149 DPRINT_INFO(STORVSC_DRV, "request extension size %u, max outstanding reqs %u", storvsc_drv_obj->RequestExtSize, storvsc_drv_obj->MaxOutstandingRequestsPerChannel);
151 if (storvsc_drv_obj->MaxOutstandingRequestsPerChannel < STORVSC_MAX_IO_REQUESTS)
153 DPRINT_ERR(STORVSC_DRV, "The number of outstanding io requests (%d) is larger than that supported (%d) internally.",
154 STORVSC_MAX_IO_REQUESTS, storvsc_drv_obj->MaxOutstandingRequestsPerChannel);
158 drv_ctx->driver.name = storvsc_drv_obj->Base.name;
159 memcpy(&drv_ctx->class_id, &storvsc_drv_obj->Base.deviceType, sizeof(struct hv_guid));
161 drv_ctx->probe = storvsc_probe;
162 drv_ctx->remove = storvsc_remove;
164 /* The driver belongs to vmbus */
165 ret = vmbus_child_driver_register(drv_ctx);
167 DPRINT_EXIT(STORVSC_DRV);
173 static int storvsc_drv_exit_cb(struct device *dev, void *data)
175 struct device **curr = (struct device **)data;
177 return 1; /* stop iterating */
182 Name: storvsc_drv_exit()
187 static void storvsc_drv_exit(void)
189 struct storvsc_driver_object *storvsc_drv_obj = &g_storvsc_drv.drv_obj;
190 struct driver_context *drv_ctx=&g_storvsc_drv.drv_ctx;
191 struct device *current_dev=NULL;
194 DPRINT_ENTER(STORVSC_DRV);
201 ret = driver_for_each_device(&drv_ctx->driver, NULL,
202 (void *) ¤t_dev,
203 storvsc_drv_exit_cb);
206 DPRINT_WARN(STORVSC_DRV,
207 "driver_for_each_device returned %d", ret);
209 if (current_dev == NULL)
212 /* Initiate removal from the top-down */
213 device_unregister(current_dev);
216 if (storvsc_drv_obj->Base.OnCleanup)
217 storvsc_drv_obj->Base.OnCleanup(&storvsc_drv_obj->Base);
219 vmbus_child_driver_unregister(drv_ctx);
221 DPRINT_EXIT(STORVSC_DRV);
228 Name: storvsc_probe()
230 Desc: Add a new device for this driver
233 static int storvsc_probe(struct device *device)
237 struct driver_context *driver_ctx = driver_to_driver_context(device->driver);
238 struct storvsc_driver_context *storvsc_drv_ctx = (struct storvsc_driver_context*)driver_ctx;
239 struct storvsc_driver_object *storvsc_drv_obj = &storvsc_drv_ctx->drv_obj;
241 struct device_context *device_ctx = device_to_device_context(device);
242 struct hv_device *device_obj = &device_ctx->device_obj;
244 struct Scsi_Host *host;
245 struct host_device_context *host_device_ctx;
246 struct storvsc_device_info device_info;
248 DPRINT_ENTER(STORVSC_DRV);
250 if (!storvsc_drv_obj->Base.OnDeviceAdd)
253 host = scsi_host_alloc(&scsi_driver, sizeof(struct host_device_context));
256 DPRINT_ERR(STORVSC_DRV, "unable to allocate scsi host object");
260 dev_set_drvdata(device, host);
262 host_device_ctx = (struct host_device_context*)host->hostdata;
263 memset(host_device_ctx, 0, sizeof(struct host_device_context));
265 host_device_ctx->port = host->host_no;
266 host_device_ctx->device_ctx = device_ctx;
268 INIT_WORK(&host_device_ctx->host_rescan_work, storvsc_host_rescan_callback);
270 host_device_ctx->request_pool =
272 (dev_name(&device_ctx->device),
273 sizeof(struct storvsc_cmd_request) + storvsc_drv_obj->RequestExtSize,
275 SLAB_HWCACHE_ALIGN, NULL);
277 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, (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 host->max_lun = STORVSC_MAX_LUNS_PER_TARGET; /* max # of devices per target */
303 host->max_id = STORVSC_MAX_TARGETS; /* max # of targets per channel */
304 host->max_channel = STORVSC_MAX_CHANNELS -1; /* max # of channels */
306 /* Register the HBA and start the scsi bus scan */
307 ret = scsi_add_host(host, device);
310 DPRINT_ERR(STORVSC_DRV, "unable to add scsi host device");
312 storvsc_drv_obj->Base.OnDeviceRemove(device_obj);
314 kmem_cache_destroy(host_device_ctx->request_pool);
316 DPRINT_EXIT(STORVSC_DRV);
321 scsi_scan_host(host);
323 DPRINT_EXIT(STORVSC_DRV);
331 Name: storvsc_remove()
333 Desc: Callback when our device is removed
336 static int storvsc_remove(struct device *device)
340 struct driver_context *driver_ctx = driver_to_driver_context(device->driver);
341 struct storvsc_driver_context *storvsc_drv_ctx = (struct storvsc_driver_context*)driver_ctx;
342 struct storvsc_driver_object *storvsc_drv_obj = &storvsc_drv_ctx->drv_obj;
344 struct device_context *device_ctx = device_to_device_context(device);
345 struct hv_device *device_obj = &device_ctx->device_obj;
347 struct Scsi_Host *host = dev_get_drvdata(device);
348 struct host_device_context *host_device_ctx=(struct host_device_context*)host->hostdata;
351 DPRINT_ENTER(STORVSC_DRV);
353 if (!storvsc_drv_obj->Base.OnDeviceRemove)
355 DPRINT_EXIT(STORVSC_DRV);
359 /* Call to the vsc driver to let it know that the device is being removed */
360 ret = storvsc_drv_obj->Base.OnDeviceRemove(device_obj);
364 DPRINT_ERR(STORVSC, "unable to remove vsc device (ret %d)", ret);
367 if (host_device_ctx->request_pool)
369 kmem_cache_destroy(host_device_ctx->request_pool);
370 host_device_ctx->request_pool = NULL;
373 DPRINT_INFO(STORVSC, "removing host adapter (%p)...", host);
374 scsi_remove_host(host);
376 DPRINT_INFO(STORVSC, "releasing host adapter (%p)...", host);
379 DPRINT_EXIT(STORVSC_DRV);
386 Name: storvsc_commmand_completion()
388 Desc: Command completion processing
391 static void storvsc_commmand_completion(struct hv_storvsc_request *request)
393 struct storvsc_cmd_request *cmd_request = (struct storvsc_cmd_request*)request->Context;
394 struct scsi_cmnd *scmnd = cmd_request->cmd;
395 struct host_device_context *host_device_ctx = (struct host_device_context*)scmnd->device->host->hostdata;
396 void (*scsi_done_fn)(struct scsi_cmnd *);
397 struct scsi_sense_hdr sense_hdr;
399 ASSERT(request == &cmd_request->request);
400 ASSERT((unsigned long)scmnd->host_scribble == (unsigned long)cmd_request);
402 ASSERT(scmnd->scsi_done);
404 DPRINT_ENTER(STORVSC_DRV);
406 if (cmd_request->bounce_sgl_count)/* using bounce buffer */
408 /* printk("copy_from_bounce_buffer\n"); */
410 /* FIXME: We can optimize on writes by just skipping this */
411 copy_from_bounce_buffer(scsi_sglist(scmnd), cmd_request->bounce_sgl, scsi_sg_count(scmnd));
412 destroy_bounce_buffer(cmd_request->bounce_sgl, cmd_request->bounce_sgl_count);
415 scmnd->result = request->Status;
419 if (scsi_normalize_sense(scmnd->sense_buffer, request->SenseBufferSize, &sense_hdr))
421 scsi_print_sense_hdr("storvsc", &sense_hdr);
425 ASSERT(request->BytesXfer <= request->DataBuffer.Length);
426 scsi_set_resid(scmnd, request->DataBuffer.Length - request->BytesXfer);
428 scsi_done_fn = scmnd->scsi_done;
430 scmnd->host_scribble = NULL;
431 scmnd->scsi_done = NULL;
433 /* !!DO NOT MODIFY the scmnd after this call */
436 kmem_cache_free(host_device_ctx->request_pool, cmd_request);
438 DPRINT_EXIT(STORVSC_DRV);
441 static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
445 /* No need to check */
449 /* We have at least 2 sg entries */
450 for ( i=0; i<sg_count; i++ )
452 if (i == 0) /* make sure 1st one does not have hole */
454 if (sgl[i].offset + sgl[i].length != PAGE_SIZE)
457 else if (i == sg_count - 1) /* make sure last one does not have hole */
459 if (sgl[i].offset != 0)
462 else /* make sure no hole in the middle */
464 if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0)
473 static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count, unsigned int len)
477 struct scatterlist* bounce_sgl;
478 struct page *page_buf;
480 num_pages = ALIGN_UP(len, PAGE_SIZE) >> PAGE_SHIFT;
482 bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC);
488 for(i=0; i<num_pages; i++)
490 page_buf = alloc_page(GFP_ATOMIC);
495 sg_set_page(&bounce_sgl[i], page_buf, 0, 0);
501 destroy_bounce_buffer(bounce_sgl, num_pages);
505 static void destroy_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
508 struct page *page_buf;
510 for (i=0; i<sg_count; i++)
512 if ((page_buf = sg_page((&sgl[i]))) != NULL)
515 __free_page(page_buf);
522 /* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
523 static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl, struct scatterlist *bounce_sgl, unsigned int orig_sgl_count)
526 unsigned long src, dest;
527 unsigned int srclen, destlen, copylen;
528 unsigned int total_copied=0;
529 unsigned long bounce_addr=0;
530 unsigned long src_addr=0;
533 local_irq_save(flags);
535 for (i=0; i<orig_sgl_count; i++)
537 src_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])), KM_IRQ0) + orig_sgl[i].offset;
539 srclen = orig_sgl[i].length;
541 /* if (PageHighMem(orig_sgl[i].page)) */
542 /* printk("HighMem page detected - addr %p", (void*)src); */
544 ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE);
548 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
553 /* assume bounce offset always == 0 */
554 dest = bounce_addr + bounce_sgl[j].length;
555 destlen = PAGE_SIZE - bounce_sgl[j].length;
557 copylen = min(srclen, destlen);
558 memcpy((void*)dest, (void*)src, copylen);
560 total_copied += copylen;
561 bounce_sgl[j].length += copylen;
565 if (bounce_sgl[j].length == PAGE_SIZE) /* full..move to next entry */
567 kunmap_atomic((void*)bounce_addr, KM_IRQ0);
570 /* if we need to use another bounce buffer */
571 if (srclen || i != orig_sgl_count -1)
573 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
576 else if (srclen == 0 && i == orig_sgl_count -1) /* unmap the last bounce that is < PAGE_SIZE */
578 kunmap_atomic((void*)bounce_addr, KM_IRQ0);
582 kunmap_atomic((void*)(src_addr - orig_sgl[i].offset), KM_IRQ0);
585 local_irq_restore(flags);
590 /* Assume the original sgl has enough room */
591 static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl, struct scatterlist *bounce_sgl, unsigned int orig_sgl_count)
594 unsigned long src, dest;
595 unsigned int srclen, destlen, copylen;
596 unsigned int total_copied=0;
597 unsigned long bounce_addr=0;
598 unsigned long dest_addr=0;
601 local_irq_save(flags);
603 for (i=0; i<orig_sgl_count; i++)
605 dest_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])), KM_IRQ0) + orig_sgl[i].offset;
607 destlen = orig_sgl[i].length;
608 ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE);
612 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
617 src = bounce_addr + bounce_sgl[j].offset;
618 srclen = bounce_sgl[j].length - bounce_sgl[j].offset;
620 copylen = min(srclen, destlen);
621 memcpy((void*)dest, (void*)src, copylen);
623 total_copied += copylen;
624 bounce_sgl[j].offset += copylen;
628 if (bounce_sgl[j].offset == bounce_sgl[j].length) /* full */
630 kunmap_atomic((void*)bounce_addr, KM_IRQ0);
633 /* if we need to use another bounce buffer */
634 if (destlen || i != orig_sgl_count -1)
636 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
639 else if (destlen == 0 && i == orig_sgl_count -1) /* unmap the last bounce that is < PAGE_SIZE */
641 kunmap_atomic((void*)bounce_addr, KM_IRQ0);
645 kunmap_atomic((void*)(dest_addr - orig_sgl[i].offset), KM_IRQ0);
648 local_irq_restore(flags);
656 Name: storvsc_queuecommand()
658 Desc: Initiate command processing
661 static int storvsc_queuecommand(struct scsi_cmnd *scmnd, void (*done)(struct scsi_cmnd *))
664 struct host_device_context *host_device_ctx = (struct host_device_context*)scmnd->device->host->hostdata;
665 struct device_context *device_ctx=host_device_ctx->device_ctx;
666 struct driver_context *driver_ctx = driver_to_driver_context(device_ctx->device.driver);
667 struct storvsc_driver_context *storvsc_drv_ctx = (struct storvsc_driver_context*)driver_ctx;
668 struct storvsc_driver_object *storvsc_drv_obj = &storvsc_drv_ctx->drv_obj;
670 struct hv_storvsc_request *request;
671 struct storvsc_cmd_request *cmd_request;
672 unsigned int request_size=0;
674 struct scatterlist *sgl;
676 DPRINT_ENTER(STORVSC_DRV);
678 DPRINT_DBG(STORVSC_DRV, "scmnd %p dir %d, use_sg %d buf %p len %d queue depth %d tagged %d",
680 scmnd->sc_data_direction,
681 scsi_sg_count(scmnd),
684 scmnd->device->queue_depth,
685 scmnd->device->tagged_supported);
687 /* If retrying, no need to prep the cmd */
688 if (scmnd->host_scribble)
690 ASSERT(scmnd->scsi_done != NULL);
692 cmd_request = (struct storvsc_cmd_request* )scmnd->host_scribble;
693 DPRINT_INFO(STORVSC_DRV, "retrying scmnd %p cmd_request %p", scmnd, cmd_request);
698 ASSERT(scmnd->scsi_done == NULL);
699 ASSERT(scmnd->host_scribble == NULL);
701 scmnd->scsi_done = done;
703 request_size = sizeof(struct storvsc_cmd_request);
705 cmd_request = kmem_cache_alloc(host_device_ctx->request_pool, GFP_ATOMIC);
708 DPRINT_ERR(STORVSC_DRV, "scmnd (%p) - unable to allocate storvsc_cmd_request...marking queue busy", scmnd);
710 scmnd->scsi_done = NULL;
711 return SCSI_MLQUEUE_DEVICE_BUSY;
714 /* Setup the cmd request */
715 cmd_request->bounce_sgl_count = 0;
716 cmd_request->bounce_sgl = NULL;
717 cmd_request->cmd = scmnd;
719 scmnd->host_scribble = (unsigned char*)cmd_request;
721 request = &cmd_request->request;
723 request->Extension = (void*)((unsigned long)cmd_request + request_size);
724 DPRINT_DBG(STORVSC_DRV, "req %p size %d ext %d", request, request_size, storvsc_drv_obj->RequestExtSize);
727 switch(scmnd->sc_data_direction)
730 request->Type = WRITE_TYPE;
732 case DMA_FROM_DEVICE:
733 request->Type = READ_TYPE;
736 request->Type = UNKNOWN_TYPE;
740 request->OnIOCompletion = storvsc_commmand_completion;
741 request->Context = cmd_request;/* scmnd; */
743 /* request->PortId = scmnd->device->channel; */
744 request->Host = host_device_ctx->port;
745 request->Bus = scmnd->device->channel;
746 request->TargetId = scmnd->device->id;
747 request->LunId = scmnd->device->lun;
749 ASSERT(scmnd->cmd_len <= 16);
750 request->CdbLen = scmnd->cmd_len;
751 request->Cdb = scmnd->cmnd;
753 request->SenseBuffer = scmnd->sense_buffer;
754 request->SenseBufferSize = SCSI_SENSE_BUFFERSIZE;
757 request->DataBuffer.Length = scsi_bufflen(scmnd);
758 if (scsi_sg_count(scmnd))
760 sgl = (struct scatterlist*)scsi_sglist(scmnd);
762 /* check if we need to bounce the sgl */
763 if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1)
765 DPRINT_INFO(STORVSC_DRV, "need to bounce buffer for this scmnd %p", scmnd);
766 cmd_request->bounce_sgl = create_bounce_buffer(sgl, scsi_sg_count(scmnd), scsi_bufflen(scmnd));
767 if (!cmd_request->bounce_sgl)
769 DPRINT_ERR(STORVSC_DRV, "unable to create bounce buffer for this scmnd %p", scmnd);
771 scmnd->scsi_done = NULL;
772 scmnd->host_scribble = NULL;
773 kmem_cache_free(host_device_ctx->request_pool, cmd_request);
775 return SCSI_MLQUEUE_HOST_BUSY;
778 cmd_request->bounce_sgl_count = ALIGN_UP(scsi_bufflen(scmnd), PAGE_SIZE) >> PAGE_SHIFT;
780 /* printk("bouncing buffer allocated %p original buffer %p\n", bounce_sgl, sgl); */
781 /* printk("copy_to_bounce_buffer\n"); */
782 /* FIXME: We can optimize on reads by just skipping this */
783 copy_to_bounce_buffer(sgl, cmd_request->bounce_sgl, scsi_sg_count(scmnd));
785 sgl = cmd_request->bounce_sgl;
788 request->DataBuffer.Offset = sgl[0].offset;
790 for (i = 0; i < scsi_sg_count(scmnd); i++ )
792 DPRINT_DBG(STORVSC_DRV, "sgl[%d] len %d offset %d \n", i, sgl[i].length, sgl[i].offset);
793 request->DataBuffer.PfnArray[i] = page_to_pfn(sg_page((&sgl[i])));
797 else if (scsi_sglist(scmnd))
799 ASSERT(scsi_bufflen(scmnd) <= PAGE_SIZE);
800 request->DataBuffer.Offset = virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
801 request->DataBuffer.PfnArray[0] = virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
805 ASSERT(scsi_bufflen(scmnd) == 0);
810 /* Invokes the vsc to start an IO */
811 ret = storvsc_drv_obj->OnIORequest(&device_ctx->device_obj, &cmd_request->request);
812 if (ret == -1) /* no more space */
814 DPRINT_ERR(STORVSC_DRV, "scmnd (%p) - queue FULL...marking queue busy", scmnd);
816 if (cmd_request->bounce_sgl_count)
818 /* FIXME: We can optimize on writes by just skipping this */
819 copy_from_bounce_buffer(scsi_sglist(scmnd), cmd_request->bounce_sgl, scsi_sg_count(scmnd));
820 destroy_bounce_buffer(cmd_request->bounce_sgl, cmd_request->bounce_sgl_count);
823 kmem_cache_free(host_device_ctx->request_pool, cmd_request);
825 scmnd->scsi_done = NULL;
826 scmnd->host_scribble = NULL;
828 ret = SCSI_MLQUEUE_DEVICE_BUSY;
831 DPRINT_EXIT(STORVSC_DRV);
836 static int storvsc_merge_bvec(struct request_queue *q, struct bvec_merge_data *bmd, struct bio_vec *bvec)
838 return bvec->bv_len; /* checking done by caller. */
843 Name: storvsc_device_configure()
845 Desc: Configure the specified scsi device
848 static int storvsc_device_alloc(struct scsi_device *sdevice)
850 DPRINT_DBG(STORVSC_DRV, "sdev (%p) - setting device flag to %d", sdevice, BLIST_SPARSELUN);
851 /* This enables luns to be located sparsely. Otherwise, we may not discovered them. */
852 sdevice->sdev_bflags |= BLIST_SPARSELUN | BLIST_LARGELUN;
856 static int storvsc_device_configure(struct scsi_device *sdevice)
858 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - curr queue depth %d", sdevice, sdevice->queue_depth);
860 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - setting queue depth to %d", sdevice, STORVSC_MAX_IO_REQUESTS);
861 scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG, STORVSC_MAX_IO_REQUESTS);
863 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - setting max segment size to %ld", sdevice, PAGE_SIZE);
864 blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
866 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - adding merge bio vec routine", sdevice);
867 blk_queue_merge_bvec(sdevice->request_queue, storvsc_merge_bvec);
869 blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
870 /* sdevice->timeout = (2000 * HZ);//(75 * HZ); */
877 Name: storvsc_host_reset_handler()
879 Desc: Reset the scsi HBA
882 static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
885 struct host_device_context *host_device_ctx = (struct host_device_context*)scmnd->device->host->hostdata;
886 struct device_context *device_ctx = host_device_ctx->device_ctx;
887 struct driver_context *driver_ctx = driver_to_driver_context(device_ctx->device.driver);
888 struct storvsc_driver_context *storvsc_drv_ctx = (struct storvsc_driver_context*)driver_ctx;
890 struct storvsc_driver_object *storvsc_drv_obj = &storvsc_drv_ctx->drv_obj;
892 DPRINT_ENTER(STORVSC_DRV);
894 DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host resetting...", 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);
901 DPRINT_EXIT(STORVSC_DRV);
905 DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host reseted", scmnd->device, &device_ctx->device_obj);
907 DPRINT_EXIT(STORVSC_DRV);
914 Name: storvsc_host_rescan
916 Desc: Rescan the scsi HBA
919 static void storvsc_host_rescan_callback(struct work_struct *work)
921 struct hv_device *device_obj =
922 &((struct host_device_context*)work)->device_ctx->device_obj;
923 struct device_context* device_ctx = to_device_context(device_obj);
924 struct Scsi_Host *host = dev_get_drvdata(&device_ctx->device);
925 struct scsi_device *sdev;
926 struct host_device_context *host_device_ctx;
927 struct scsi_device **sdevs_remove_list;
928 unsigned int sdevs_count=0;
931 unsigned int lun_count=0;
932 unsigned int *lun_list;
934 DPRINT_ENTER(STORVSC_DRV);
936 host_device_ctx = (struct host_device_context*)host->hostdata;
937 lun_list = kcalloc(STORVSC_MAX_LUNS_PER_TARGET, sizeof(unsigned int), GFP_ATOMIC);
940 DPRINT_ERR(STORVSC_DRV, "unable to allocate lun list");
944 sdevs_remove_list = kcalloc(STORVSC_MAX_LUNS_PER_TARGET, sizeof(void *), GFP_ATOMIC);
945 if (!sdevs_remove_list)
948 DPRINT_ERR(STORVSC_DRV, "unable to allocate lun remove list");
952 DPRINT_INFO(STORVSC_DRV, "rescanning host for new scsi devices...");
954 /* Rescan for new device */
955 scsi_scan_target(&host->shost_gendev, host_device_ctx->path, host_device_ctx->target, SCAN_WILD_CARD, 1);
957 DPRINT_INFO(STORVSC_DRV, "rescanning host for removed scsi device...");
959 /* Use the 1st device to send the report luns cmd */
960 shost_for_each_device(sdev, host)
962 lun_count=STORVSC_MAX_LUNS_PER_TARGET;
963 storvsc_report_luns(sdev, lun_list, &lun_count);
965 DPRINT_INFO(STORVSC_DRV, "report luns on scsi device (%p) found %u luns ", sdev, lun_count);
966 DPRINT_INFO(STORVSC_DRV, "existing luns on scsi device (%p) host (%d)", sdev, host->host_no);
968 scsi_device_put(sdev);
972 for (i=0; i<lun_count; i++)
974 DPRINT_INFO(STORVSC_DRV, "%d) lun %u", i, lun_list[i]);
977 /* Rescan for devices that may have been removed. */
978 /* We do not have to worry that new devices may have been added since */
979 /* this callback is serialized by the workqueue ie add/remove are done here. */
980 shost_for_each_device(sdev, host)
982 /* See if this device is still here */
984 for (i=0; i<lun_count; i++)
986 if (sdev->lun == lun_list[i])
994 DPRINT_INFO(STORVSC_DRV, "lun (%u) does not exists", sdev->lun);
995 sdevs_remove_list[sdevs_count++] = sdev;
999 /* Now remove the devices */
1000 for (i=0; i< sdevs_count; i++)
1002 DPRINT_INFO(STORVSC_DRV, "removing scsi device (%p) lun (%u)...",
1003 sdevs_remove_list[i], sdevs_remove_list[i]->lun);
1005 /* make sure it is not removed from underneath us */
1006 if (!scsi_device_get(sdevs_remove_list[i]))
1008 scsi_remove_device(sdevs_remove_list[i]);
1009 scsi_device_put(sdevs_remove_list[i]);
1013 DPRINT_INFO(STORVSC_DRV, "rescan completed on dev obj (%p) target (%u) bus (%u)", device_obj, host_device_ctx->target, host_device_ctx->path);
1016 kfree(sdevs_remove_list);
1018 DPRINT_EXIT(STORVSC_DRV);
1021 static int storvsc_report_luns(struct scsi_device *sdev, unsigned int luns[], unsigned int *lun_count)
1025 unsigned int num_luns;
1027 unsigned char *data;
1028 struct scsi_sense_hdr sshdr;
1029 unsigned char cmd[16]={0};
1030 unsigned int report_len = 8*(STORVSC_MAX_LUNS_PER_TARGET+1); /* Add 1 to cover the report_lun header */
1031 unsigned long long *report_luns;
1032 const unsigned int in_lun_count = *lun_count;
1036 report_luns = kzalloc(report_len, GFP_ATOMIC);
1042 cmd[0] = REPORT_LUNS;
1045 *(unsigned int*)&cmd[6] = cpu_to_be32(report_len);
1047 result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, (unsigned char*)report_luns, report_len, &sshdr, 30*HZ, 3, NULL);
1054 /* get the length from the first four bytes */
1055 report_len = be32_to_cpu(*(unsigned int*)&report_luns[0]);
1057 num_luns = (report_len / sizeof(unsigned long long));
1058 if (num_luns > in_lun_count)
1064 *lun_count = num_luns;
1066 DPRINT_DBG(STORVSC_DRV, "report luns on scsi device (%p) found %u luns ", sdev, num_luns);
1068 /* lun id starts at 1 */
1069 for (i=1; i< num_luns+1; i++)
1072 data = (unsigned char*)&report_luns[i];
1073 for (j = 0; j < sizeof(lun); j += 2)
1075 lun = lun | (((data[j] << 8) | data[j + 1]) << (j * 8));
1085 static void storvsc_host_rescan(struct hv_device *device_obj)
1087 struct device_context* device_ctx = to_device_context(device_obj);
1088 struct Scsi_Host *host = dev_get_drvdata(&device_ctx->device);
1089 struct host_device_context *host_device_ctx;
1091 DPRINT_ENTER(STORVSC_DRV);
1093 host_device_ctx = (struct host_device_context*)host->hostdata;
1095 DPRINT_INFO(STORVSC_DRV, "initiating rescan on dev obj (%p) target (%u) bus (%u)...", device_obj, host_device_ctx->target, host_device_ctx->path);
1097 /* We need to queue this since the scanning may block and the caller may be in an intr context */
1098 /* scsi_queue_work(host, &host_device_ctx->host_rescan_work); */
1099 schedule_work(&host_device_ctx->host_rescan_work);
1100 DPRINT_EXIT(STORVSC_DRV);
1103 static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev, sector_t capacity, int *info)
1105 sector_t total_sectors = capacity;
1106 sector_t cylinder_times_heads=0;
1109 int sectors_per_track=0;
1114 if (total_sectors > (65535 * 16 * 255)) {
1115 total_sectors = (65535 * 16 * 255);
1118 if (total_sectors >= (65535 * 16 * 63)) {
1119 sectors_per_track = 255;
1122 cylinder_times_heads = total_sectors;
1123 rem = sector_div(cylinder_times_heads, sectors_per_track); /* sector_div stores the quotient in cylinder_times_heads */
1127 sectors_per_track = 17;
1129 cylinder_times_heads = total_sectors;
1130 rem = sector_div(cylinder_times_heads, sectors_per_track); /* sector_div stores the quotient in cylinder_times_heads */
1132 temp = cylinder_times_heads + 1023;
1133 rem = sector_div(temp, 1024); /* sector_div stores the quotient in temp */
1141 if (cylinder_times_heads >= (heads * 1024) || (heads > 16)) {
1142 sectors_per_track = 31;
1145 cylinder_times_heads = total_sectors;
1146 rem = sector_div(cylinder_times_heads, sectors_per_track); /* sector_div stores the quotient in cylinder_times_heads */
1149 if (cylinder_times_heads >= (heads * 1024)) {
1150 sectors_per_track = 63;
1153 cylinder_times_heads = total_sectors;
1154 rem = sector_div(cylinder_times_heads, sectors_per_track); /* sector_div stores the quotient in cylinder_times_heads */
1158 temp = cylinder_times_heads;
1159 rem = sector_div(temp, heads); /* sector_div stores the quotient in temp */
1163 info[1] = sectors_per_track;
1164 info[2] = cylinders;
1166 DPRINT_INFO(STORVSC_DRV, "CHS (%d, %d, %d)", cylinders, heads, sectors_per_track);
1171 MODULE_LICENSE("GPL");
1173 static int __init storvsc_init(void)
1177 DPRINT_ENTER(STORVSC_DRV);
1179 DPRINT_INFO(STORVSC_DRV, "Storvsc initializing....");
1181 ret = storvsc_drv_init(StorVscInitialize);
1183 DPRINT_EXIT(STORVSC_DRV);
1188 static void __exit storvsc_exit(void)
1190 DPRINT_ENTER(STORVSC_DRV);
1194 DPRINT_ENTER(STORVSC_DRV);
1197 module_param(storvsc_ringbuffer_size, int, S_IRUGO);
1199 module_init(storvsc_init);
1200 module_exit(storvsc_exit);