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>
39 #include "include/logging.h"
40 #include "include/vmbus.h"
42 #include "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 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 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(DEVICE_OBJECT* 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 int storvsc_drv_init(PFN_DRIVERINITIALIZE pfn_drv_init)
136 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(GUID));
161 drv_ctx->probe = storvsc_probe;
162 drv_ctx->remove = storvsc_remove;
164 /* The driver belongs to vmbus */
165 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 void storvsc_drv_exit(void)
189 STORVSC_DRIVER_OBJECT *storvsc_drv_obj=&g_storvsc_drv.drv_obj;
190 struct driver_context *drv_ctx=&g_storvsc_drv.drv_ctx;
192 struct device *current_dev=NULL;
194 DPRINT_ENTER(STORVSC_DRV);
201 driver_for_each_device(&drv_ctx->driver, NULL, (void*)¤t_dev, storvsc_drv_exit_cb);
203 if (current_dev == NULL)
206 /* Initiate removal from the top-down */
207 device_unregister(current_dev);
210 if (storvsc_drv_obj->Base.OnCleanup)
211 storvsc_drv_obj->Base.OnCleanup(&storvsc_drv_obj->Base);
213 vmbus_child_driver_unregister(drv_ctx);
215 DPRINT_EXIT(STORVSC_DRV);
222 Name: storvsc_probe()
224 Desc: Add a new device for this driver
227 static int storvsc_probe(struct device *device)
231 struct driver_context *driver_ctx = driver_to_driver_context(device->driver);
232 struct storvsc_driver_context *storvsc_drv_ctx = (struct storvsc_driver_context*)driver_ctx;
233 STORVSC_DRIVER_OBJECT* storvsc_drv_obj = &storvsc_drv_ctx->drv_obj;
235 struct device_context *device_ctx = device_to_device_context(device);
236 DEVICE_OBJECT* device_obj = &device_ctx->device_obj;
238 struct Scsi_Host *host;
239 struct host_device_context *host_device_ctx;
240 STORVSC_DEVICE_INFO device_info;
242 DPRINT_ENTER(STORVSC_DRV);
244 if (!storvsc_drv_obj->Base.OnDeviceAdd)
247 host = scsi_host_alloc(&scsi_driver, sizeof(struct host_device_context));
250 DPRINT_ERR(STORVSC_DRV, "unable to allocate scsi host object");
254 dev_set_drvdata(device, host);
256 host_device_ctx = (struct host_device_context*)host->hostdata;
257 memset(host_device_ctx, 0, sizeof(struct host_device_context));
259 host_device_ctx->port = host->host_no;
260 host_device_ctx->device_ctx = device_ctx;
262 INIT_WORK(&host_device_ctx->host_rescan_work, storvsc_host_rescan_callback);
264 host_device_ctx->request_pool =
266 (dev_name(&device_ctx->device),
267 sizeof(struct storvsc_cmd_request) + storvsc_drv_obj->RequestExtSize,
269 SLAB_HWCACHE_ALIGN, NULL);
271 if (!host_device_ctx->request_pool)
274 DPRINT_EXIT(STORVSC_DRV);
279 device_info.PortNumber = host->host_no;
280 /* Call to the vsc driver to add the device */
281 ret = storvsc_drv_obj->Base.OnDeviceAdd(device_obj, (void*)&device_info);
284 DPRINT_ERR(STORVSC_DRV, "unable to add scsi vsc device");
285 kmem_cache_destroy(host_device_ctx->request_pool);
287 DPRINT_EXIT(STORVSC_DRV);
292 /* host_device_ctx->port = device_info.PortNumber; */
293 host_device_ctx->path = device_info.PathId;
294 host_device_ctx->target = device_info.TargetId;
296 host->max_lun = STORVSC_MAX_LUNS_PER_TARGET; /* max # of devices per target */
297 host->max_id = STORVSC_MAX_TARGETS; /* max # of targets per channel */
298 host->max_channel = STORVSC_MAX_CHANNELS -1; /* max # of channels */
300 /* Register the HBA and start the scsi bus scan */
301 ret = scsi_add_host(host, device);
304 DPRINT_ERR(STORVSC_DRV, "unable to add scsi host device");
306 storvsc_drv_obj->Base.OnDeviceRemove(device_obj);
308 kmem_cache_destroy(host_device_ctx->request_pool);
310 DPRINT_EXIT(STORVSC_DRV);
315 scsi_scan_host(host);
317 DPRINT_EXIT(STORVSC_DRV);
325 Name: storvsc_remove()
327 Desc: Callback when our device is removed
330 static int storvsc_remove(struct device *device)
334 struct driver_context *driver_ctx = driver_to_driver_context(device->driver);
335 struct storvsc_driver_context *storvsc_drv_ctx = (struct storvsc_driver_context*)driver_ctx;
336 STORVSC_DRIVER_OBJECT* storvsc_drv_obj = &storvsc_drv_ctx->drv_obj;
338 struct device_context *device_ctx = device_to_device_context(device);
339 DEVICE_OBJECT* device_obj = &device_ctx->device_obj;
341 struct Scsi_Host *host = dev_get_drvdata(device);
342 struct host_device_context *host_device_ctx=(struct host_device_context*)host->hostdata;
345 DPRINT_ENTER(STORVSC_DRV);
347 if (!storvsc_drv_obj->Base.OnDeviceRemove)
349 DPRINT_EXIT(STORVSC_DRV);
353 /* Call to the vsc driver to let it know that the device is being removed */
354 ret = storvsc_drv_obj->Base.OnDeviceRemove(device_obj);
358 DPRINT_ERR(STORVSC, "unable to remove vsc device (ret %d)", ret);
361 if (host_device_ctx->request_pool)
363 kmem_cache_destroy(host_device_ctx->request_pool);
364 host_device_ctx->request_pool = NULL;
367 DPRINT_INFO(STORVSC, "removing host adapter (%p)...", host);
368 scsi_remove_host(host);
370 DPRINT_INFO(STORVSC, "releasing host adapter (%p)...", host);
373 DPRINT_EXIT(STORVSC_DRV);
380 Name: storvsc_commmand_completion()
382 Desc: Command completion processing
385 static void storvsc_commmand_completion(STORVSC_REQUEST* request)
387 struct storvsc_cmd_request *cmd_request = (struct storvsc_cmd_request*)request->Context;
388 struct scsi_cmnd *scmnd = cmd_request->cmd;
389 struct host_device_context *host_device_ctx = (struct host_device_context*)scmnd->device->host->hostdata;
390 void (*scsi_done_fn)(struct scsi_cmnd *);
391 struct scsi_sense_hdr sense_hdr;
393 ASSERT(request == &cmd_request->request);
394 ASSERT((unsigned long)scmnd->host_scribble == (unsigned long)cmd_request);
396 ASSERT(scmnd->scsi_done);
398 DPRINT_ENTER(STORVSC_DRV);
400 if (cmd_request->bounce_sgl_count)/* using bounce buffer */
402 /* printk("copy_from_bounce_buffer\n"); */
404 /* FIXME: We can optimize on writes by just skipping this */
405 copy_from_bounce_buffer(scsi_sglist(scmnd), cmd_request->bounce_sgl, scsi_sg_count(scmnd));
406 destroy_bounce_buffer(cmd_request->bounce_sgl, cmd_request->bounce_sgl_count);
409 scmnd->result = request->Status;
413 if (scsi_normalize_sense(scmnd->sense_buffer, request->SenseBufferSize, &sense_hdr))
415 scsi_print_sense_hdr("storvsc", &sense_hdr);
419 ASSERT(request->BytesXfer <= request->DataBuffer.Length);
420 scsi_set_resid(scmnd, request->DataBuffer.Length - request->BytesXfer);
422 scsi_done_fn = scmnd->scsi_done;
424 scmnd->host_scribble = NULL;
425 scmnd->scsi_done = NULL;
427 /* !!DO NOT MODIFY the scmnd after this call */
430 kmem_cache_free(host_device_ctx->request_pool, cmd_request);
432 DPRINT_EXIT(STORVSC_DRV);
435 static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
439 /* No need to check */
443 /* We have at least 2 sg entries */
444 for ( i=0; i<sg_count; i++ )
446 if (i == 0) /* make sure 1st one does not have hole */
448 if (sgl[i].offset + sgl[i].length != PAGE_SIZE)
451 else if (i == sg_count - 1) /* make sure last one does not have hole */
453 if (sgl[i].offset != 0)
456 else /* make sure no hole in the middle */
458 if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0)
467 static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count, unsigned int len)
471 struct scatterlist* bounce_sgl;
472 struct page *page_buf;
474 num_pages = ALIGN_UP(len, PAGE_SIZE) >> PAGE_SHIFT;
476 bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC);
482 for(i=0; i<num_pages; i++)
484 page_buf = alloc_page(GFP_ATOMIC);
489 sg_set_page(&bounce_sgl[i], page_buf, 0, 0);
495 destroy_bounce_buffer(bounce_sgl, num_pages);
499 static void destroy_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
502 struct page *page_buf;
504 for (i=0; i<sg_count; i++)
506 if ((page_buf = sg_page((&sgl[i]))) != NULL)
509 __free_page(page_buf);
516 /* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
517 static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl, struct scatterlist *bounce_sgl, unsigned int orig_sgl_count)
520 unsigned long src, dest;
521 unsigned int srclen, destlen, copylen;
522 unsigned int total_copied=0;
523 unsigned long bounce_addr=0;
524 unsigned long src_addr=0;
527 local_irq_save(flags);
529 for (i=0; i<orig_sgl_count; i++)
531 src_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])), KM_IRQ0) + orig_sgl[i].offset;
533 srclen = orig_sgl[i].length;
535 /* if (PageHighMem(orig_sgl[i].page)) */
536 /* printk("HighMem page detected - addr %p", (void*)src); */
538 ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE);
542 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
547 /* assume bounce offset always == 0 */
548 dest = bounce_addr + bounce_sgl[j].length;
549 destlen = PAGE_SIZE - bounce_sgl[j].length;
551 copylen = min(srclen, destlen);
552 memcpy((void*)dest, (void*)src, copylen);
554 total_copied += copylen;
555 bounce_sgl[j].length += copylen;
559 if (bounce_sgl[j].length == PAGE_SIZE) /* full..move to next entry */
561 kunmap_atomic((void*)bounce_addr, KM_IRQ0);
564 /* if we need to use another bounce buffer */
565 if (srclen || i != orig_sgl_count -1)
567 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
570 else if (srclen == 0 && i == orig_sgl_count -1) /* unmap the last bounce that is < PAGE_SIZE */
572 kunmap_atomic((void*)bounce_addr, KM_IRQ0);
576 kunmap_atomic((void*)(src_addr - orig_sgl[i].offset), KM_IRQ0);
579 local_irq_restore(flags);
584 /* Assume the original sgl has enough room */
585 static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl, struct scatterlist *bounce_sgl, unsigned int orig_sgl_count)
588 unsigned long src, dest;
589 unsigned int srclen, destlen, copylen;
590 unsigned int total_copied=0;
591 unsigned long bounce_addr=0;
592 unsigned long dest_addr=0;
595 local_irq_save(flags);
597 for (i=0; i<orig_sgl_count; i++)
599 dest_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])), KM_IRQ0) + orig_sgl[i].offset;
601 destlen = orig_sgl[i].length;
602 ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE);
606 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
611 src = bounce_addr + bounce_sgl[j].offset;
612 srclen = bounce_sgl[j].length - bounce_sgl[j].offset;
614 copylen = min(srclen, destlen);
615 memcpy((void*)dest, (void*)src, copylen);
617 total_copied += copylen;
618 bounce_sgl[j].offset += copylen;
622 if (bounce_sgl[j].offset == bounce_sgl[j].length) /* full */
624 kunmap_atomic((void*)bounce_addr, KM_IRQ0);
627 /* if we need to use another bounce buffer */
628 if (destlen || i != orig_sgl_count -1)
630 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
633 else if (destlen == 0 && i == orig_sgl_count -1) /* unmap the last bounce that is < PAGE_SIZE */
635 kunmap_atomic((void*)bounce_addr, KM_IRQ0);
639 kunmap_atomic((void*)(dest_addr - orig_sgl[i].offset), KM_IRQ0);
642 local_irq_restore(flags);
650 Name: storvsc_queuecommand()
652 Desc: Initiate command processing
655 static int storvsc_queuecommand(struct scsi_cmnd *scmnd, void (*done)(struct scsi_cmnd *))
658 struct host_device_context *host_device_ctx = (struct host_device_context*)scmnd->device->host->hostdata;
659 struct device_context *device_ctx=host_device_ctx->device_ctx;
660 struct driver_context *driver_ctx = driver_to_driver_context(device_ctx->device.driver);
661 struct storvsc_driver_context *storvsc_drv_ctx = (struct storvsc_driver_context*)driver_ctx;
662 STORVSC_DRIVER_OBJECT* storvsc_drv_obj = &storvsc_drv_ctx->drv_obj;
664 STORVSC_REQUEST *request;
665 struct storvsc_cmd_request *cmd_request;
666 unsigned int request_size=0;
668 struct scatterlist *sgl;
670 DPRINT_ENTER(STORVSC_DRV);
672 DPRINT_DBG(STORVSC_DRV, "scmnd %p dir %d, use_sg %d buf %p len %d queue depth %d tagged %d",
674 scmnd->sc_data_direction,
675 scsi_sg_count(scmnd),
678 scmnd->device->queue_depth,
679 scmnd->device->tagged_supported);
681 /* If retrying, no need to prep the cmd */
682 if (scmnd->host_scribble)
684 ASSERT(scmnd->scsi_done != NULL);
686 cmd_request = (struct storvsc_cmd_request* )scmnd->host_scribble;
687 DPRINT_INFO(STORVSC_DRV, "retrying scmnd %p cmd_request %p", scmnd, cmd_request);
692 ASSERT(scmnd->scsi_done == NULL);
693 ASSERT(scmnd->host_scribble == NULL);
695 scmnd->scsi_done = done;
697 request_size = sizeof(struct storvsc_cmd_request);
699 cmd_request = kmem_cache_alloc(host_device_ctx->request_pool, GFP_ATOMIC);
702 DPRINT_ERR(STORVSC_DRV, "scmnd (%p) - unable to allocate storvsc_cmd_request...marking queue busy", scmnd);
704 scmnd->scsi_done = NULL;
705 return SCSI_MLQUEUE_DEVICE_BUSY;
708 /* Setup the cmd request */
709 cmd_request->bounce_sgl_count = 0;
710 cmd_request->bounce_sgl = NULL;
711 cmd_request->cmd = scmnd;
713 scmnd->host_scribble = (unsigned char*)cmd_request;
715 request = &cmd_request->request;
717 request->Extension = (void*)((unsigned long)cmd_request + request_size);
718 DPRINT_DBG(STORVSC_DRV, "req %p size %d ext %d", request, request_size, storvsc_drv_obj->RequestExtSize);
721 switch(scmnd->sc_data_direction)
724 request->Type = WRITE_TYPE;
726 case DMA_FROM_DEVICE:
727 request->Type = READ_TYPE;
730 request->Type = UNKNOWN_TYPE;
734 request->OnIOCompletion = storvsc_commmand_completion;
735 request->Context = cmd_request;/* scmnd; */
737 /* request->PortId = scmnd->device->channel; */
738 request->Host = host_device_ctx->port;
739 request->Bus = scmnd->device->channel;
740 request->TargetId = scmnd->device->id;
741 request->LunId = scmnd->device->lun;
743 ASSERT(scmnd->cmd_len <= 16);
744 request->CdbLen = scmnd->cmd_len;
745 request->Cdb = scmnd->cmnd;
747 request->SenseBuffer = scmnd->sense_buffer;
748 request->SenseBufferSize = SCSI_SENSE_BUFFERSIZE;
751 request->DataBuffer.Length = scsi_bufflen(scmnd);
752 if (scsi_sg_count(scmnd))
754 sgl = (struct scatterlist*)scsi_sglist(scmnd);
756 /* check if we need to bounce the sgl */
757 if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1)
759 DPRINT_INFO(STORVSC_DRV, "need to bounce buffer for this scmnd %p", scmnd);
760 cmd_request->bounce_sgl = create_bounce_buffer(sgl, scsi_sg_count(scmnd), scsi_bufflen(scmnd));
761 if (!cmd_request->bounce_sgl)
763 DPRINT_ERR(STORVSC_DRV, "unable to create bounce buffer for this scmnd %p", scmnd);
765 scmnd->scsi_done = NULL;
766 scmnd->host_scribble = NULL;
767 kmem_cache_free(host_device_ctx->request_pool, cmd_request);
769 return SCSI_MLQUEUE_HOST_BUSY;
772 cmd_request->bounce_sgl_count = ALIGN_UP(scsi_bufflen(scmnd), PAGE_SIZE) >> PAGE_SHIFT;
774 /* printk("bouncing buffer allocated %p original buffer %p\n", bounce_sgl, sgl); */
775 /* printk("copy_to_bounce_buffer\n"); */
776 /* FIXME: We can optimize on reads by just skipping this */
777 copy_to_bounce_buffer(sgl, cmd_request->bounce_sgl, scsi_sg_count(scmnd));
779 sgl = cmd_request->bounce_sgl;
782 request->DataBuffer.Offset = sgl[0].offset;
784 for (i = 0; i < scsi_sg_count(scmnd); i++ )
786 DPRINT_DBG(STORVSC_DRV, "sgl[%d] len %d offset %d \n", i, sgl[i].length, sgl[i].offset);
787 request->DataBuffer.PfnArray[i] = page_to_pfn(sg_page((&sgl[i])));
791 else if (scsi_sglist(scmnd))
793 ASSERT(scsi_bufflen(scmnd) <= PAGE_SIZE);
794 request->DataBuffer.Offset = virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
795 request->DataBuffer.PfnArray[0] = virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
799 ASSERT(scsi_bufflen(scmnd) == 0);
804 /* Invokes the vsc to start an IO */
805 ret = storvsc_drv_obj->OnIORequest(&device_ctx->device_obj, &cmd_request->request);
806 if (ret == -1) /* no more space */
808 DPRINT_ERR(STORVSC_DRV, "scmnd (%p) - queue FULL...marking queue busy", scmnd);
810 if (cmd_request->bounce_sgl_count)
812 /* FIXME: We can optimize on writes by just skipping this */
813 copy_from_bounce_buffer(scsi_sglist(scmnd), cmd_request->bounce_sgl, scsi_sg_count(scmnd));
814 destroy_bounce_buffer(cmd_request->bounce_sgl, cmd_request->bounce_sgl_count);
817 kmem_cache_free(host_device_ctx->request_pool, cmd_request);
819 scmnd->scsi_done = NULL;
820 scmnd->host_scribble = NULL;
822 ret = SCSI_MLQUEUE_DEVICE_BUSY;
825 DPRINT_EXIT(STORVSC_DRV);
830 static int storvsc_merge_bvec(struct request_queue *q, struct bvec_merge_data *bmd, struct bio_vec *bvec)
832 return bvec->bv_len; /* checking done by caller. */
837 Name: storvsc_device_configure()
839 Desc: Configure the specified scsi device
842 static int storvsc_device_alloc(struct scsi_device *sdevice)
844 DPRINT_DBG(STORVSC_DRV, "sdev (%p) - setting device flag to %d", sdevice, BLIST_SPARSELUN);
845 /* This enables luns to be located sparsely. Otherwise, we may not discovered them. */
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, sdevice->queue_depth);
854 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - setting queue depth to %d", sdevice, STORVSC_MAX_IO_REQUESTS);
855 scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG, STORVSC_MAX_IO_REQUESTS);
857 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - setting max segment size to %ld", sdevice, PAGE_SIZE);
858 blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
860 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - adding merge bio vec routine", sdevice);
861 blk_queue_merge_bvec(sdevice->request_queue, storvsc_merge_bvec);
863 blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
864 /* sdevice->timeout = (2000 * HZ);//(75 * HZ); */
871 Name: storvsc_host_reset_handler()
873 Desc: Reset the scsi HBA
876 static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
879 struct host_device_context *host_device_ctx = (struct host_device_context*)scmnd->device->host->hostdata;
880 struct device_context *device_ctx = host_device_ctx->device_ctx;
881 struct driver_context *driver_ctx = driver_to_driver_context(device_ctx->device.driver);
882 struct storvsc_driver_context *storvsc_drv_ctx = (struct storvsc_driver_context*)driver_ctx;
884 STORVSC_DRIVER_OBJECT *storvsc_drv_obj = &storvsc_drv_ctx->drv_obj;
886 DPRINT_ENTER(STORVSC_DRV);
888 DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host resetting...", scmnd->device, &device_ctx->device_obj);
890 /* Invokes the vsc to reset the host/bus */
891 ASSERT(storvsc_drv_obj->OnHostReset);
892 ret = storvsc_drv_obj->OnHostReset(&device_ctx->device_obj);
895 DPRINT_EXIT(STORVSC_DRV);
899 DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host reseted", scmnd->device, &device_ctx->device_obj);
901 DPRINT_EXIT(STORVSC_DRV);
908 Name: storvsc_host_rescan
910 Desc: Rescan the scsi HBA
913 static void storvsc_host_rescan_callback(struct work_struct *work)
915 DEVICE_OBJECT* device_obj =
916 &((struct host_device_context*)work)->device_ctx->device_obj;
917 struct device_context* device_ctx = to_device_context(device_obj);
918 struct Scsi_Host *host = dev_get_drvdata(&device_ctx->device);
919 struct scsi_device *sdev;
920 struct host_device_context *host_device_ctx;
921 struct scsi_device **sdevs_remove_list;
922 unsigned int sdevs_count=0;
925 unsigned int lun_count=0;
926 unsigned int *lun_list;
928 DPRINT_ENTER(STORVSC_DRV);
930 host_device_ctx = (struct host_device_context*)host->hostdata;
931 lun_list = kcalloc(STORVSC_MAX_LUNS_PER_TARGET, sizeof(unsigned int), GFP_ATOMIC);
934 DPRINT_ERR(STORVSC_DRV, "unable to allocate lun list");
938 sdevs_remove_list = kcalloc(STORVSC_MAX_LUNS_PER_TARGET, sizeof(void *), GFP_ATOMIC);
939 if (!sdevs_remove_list)
942 DPRINT_ERR(STORVSC_DRV, "unable to allocate lun remove list");
946 DPRINT_INFO(STORVSC_DRV, "rescanning host for new scsi devices...", device_obj, host_device_ctx->target, host_device_ctx->path);
948 /* Rescan for new device */
949 scsi_scan_target(&host->shost_gendev, host_device_ctx->path, host_device_ctx->target, SCAN_WILD_CARD, 1);
951 DPRINT_INFO(STORVSC_DRV, "rescanning host for removed scsi device...");
953 /* Use the 1st device to send the report luns cmd */
954 shost_for_each_device(sdev, host)
956 lun_count=STORVSC_MAX_LUNS_PER_TARGET;
957 storvsc_report_luns(sdev, lun_list, &lun_count);
959 DPRINT_INFO(STORVSC_DRV, "report luns on scsi device (%p) found %u luns ", sdev, lun_count);
960 DPRINT_INFO(STORVSC_DRV, "existing luns on scsi device (%p) host (%d)", sdev, host->host_no);
962 scsi_device_put(sdev);
966 for (i=0; i<lun_count; i++)
968 DPRINT_INFO(STORVSC_DRV, "%d) lun %u", i, lun_list[i]);
971 /* Rescan for devices that may have been removed. */
972 /* We do not have to worry that new devices may have been added since */
973 /* this callback is serialized by the workqueue ie add/remove are done here. */
974 shost_for_each_device(sdev, host)
976 /* See if this device is still here */
978 for (i=0; i<lun_count; i++)
980 if (sdev->lun == lun_list[i])
988 DPRINT_INFO(STORVSC_DRV, "lun (%u) does not exists", sdev->lun);
989 sdevs_remove_list[sdevs_count++] = sdev;
993 /* Now remove the devices */
994 for (i=0; i< sdevs_count; i++)
996 DPRINT_INFO(STORVSC_DRV, "removing scsi device (%p) lun (%u)...",
997 sdevs_remove_list[i], sdevs_remove_list[i]->lun);
999 /* make sure it is not removed from underneath us */
1000 if (!scsi_device_get(sdevs_remove_list[i]))
1002 scsi_remove_device(sdevs_remove_list[i]);
1003 scsi_device_put(sdevs_remove_list[i]);
1007 DPRINT_INFO(STORVSC_DRV, "rescan completed on dev obj (%p) target (%u) bus (%u)", device_obj, host_device_ctx->target, host_device_ctx->path);
1010 kfree(sdevs_remove_list);
1012 DPRINT_EXIT(STORVSC_DRV);
1015 static int storvsc_report_luns(struct scsi_device *sdev, unsigned int luns[], unsigned int *lun_count)
1019 unsigned int num_luns;
1021 unsigned char *data;
1022 struct scsi_sense_hdr sshdr;
1023 unsigned char cmd[16]={0};
1024 unsigned int report_len = 8*(STORVSC_MAX_LUNS_PER_TARGET+1); /* Add 1 to cover the report_lun header */
1025 unsigned long long *report_luns;
1026 const unsigned int in_lun_count = *lun_count;
1030 report_luns = kzalloc(report_len, GFP_ATOMIC);
1036 cmd[0] = REPORT_LUNS;
1039 *(unsigned int*)&cmd[6] = cpu_to_be32(report_len);
1041 result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, (unsigned char*)report_luns, report_len, &sshdr, 30*HZ, 3, NULL);
1048 /* get the length from the first four bytes */
1049 report_len = be32_to_cpu(*(unsigned int*)&report_luns[0]);
1051 num_luns = (report_len / sizeof(unsigned long long));
1052 if (num_luns > in_lun_count)
1058 *lun_count = num_luns;
1060 DPRINT_DBG(STORVSC_DRV, "report luns on scsi device (%p) found %u luns ", sdev, num_luns);
1062 /* lun id starts at 1 */
1063 for (i=1; i< num_luns+1; i++)
1066 data = (unsigned char*)&report_luns[i];
1067 for (j = 0; j < sizeof(lun); j += 2)
1069 lun = lun | (((data[j] << 8) | data[j + 1]) << (j * 8));
1079 static void storvsc_host_rescan(DEVICE_OBJECT* device_obj)
1081 struct device_context* device_ctx = to_device_context(device_obj);
1082 struct Scsi_Host *host = dev_get_drvdata(&device_ctx->device);
1083 struct host_device_context *host_device_ctx;
1085 DPRINT_ENTER(STORVSC_DRV);
1087 host_device_ctx = (struct host_device_context*)host->hostdata;
1089 DPRINT_INFO(STORVSC_DRV, "initiating rescan on dev obj (%p) target (%u) bus (%u)...", device_obj, host_device_ctx->target, host_device_ctx->path);
1091 /* We need to queue this since the scanning may block and the caller may be in an intr context */
1092 /* scsi_queue_work(host, &host_device_ctx->host_rescan_work); */
1093 schedule_work(&host_device_ctx->host_rescan_work);
1094 DPRINT_EXIT(STORVSC_DRV);
1097 static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev, sector_t capacity, int *info)
1099 sector_t total_sectors = capacity;
1100 sector_t cylinder_times_heads=0;
1103 int sectors_per_track=0;
1108 if (total_sectors > (65535 * 16 * 255)) {
1109 total_sectors = (65535 * 16 * 255);
1112 if (total_sectors >= (65535 * 16 * 63)) {
1113 sectors_per_track = 255;
1116 cylinder_times_heads = total_sectors;
1117 rem = sector_div(cylinder_times_heads, sectors_per_track); /* sector_div stores the quotient in cylinder_times_heads */
1121 sectors_per_track = 17;
1123 cylinder_times_heads = total_sectors;
1124 rem = sector_div(cylinder_times_heads, sectors_per_track); /* sector_div stores the quotient in cylinder_times_heads */
1126 temp = cylinder_times_heads + 1023;
1127 rem = sector_div(temp, 1024); /* sector_div stores the quotient in temp */
1135 if (cylinder_times_heads >= (heads * 1024) || (heads > 16)) {
1136 sectors_per_track = 31;
1139 cylinder_times_heads = total_sectors;
1140 rem = sector_div(cylinder_times_heads, sectors_per_track); /* sector_div stores the quotient in cylinder_times_heads */
1143 if (cylinder_times_heads >= (heads * 1024)) {
1144 sectors_per_track = 63;
1147 cylinder_times_heads = total_sectors;
1148 rem = sector_div(cylinder_times_heads, sectors_per_track); /* sector_div stores the quotient in cylinder_times_heads */
1152 temp = cylinder_times_heads;
1153 rem = sector_div(temp, heads); /* sector_div stores the quotient in temp */
1157 info[1] = sectors_per_track;
1158 info[2] = cylinders;
1160 DPRINT_INFO(STORVSC_DRV, "CHS (%d, %d, %d)", cylinders, heads, sectors_per_track);
1165 MODULE_LICENSE("GPL");
1167 static int __init storvsc_init(void)
1171 DPRINT_ENTER(STORVSC_DRV);
1173 DPRINT_INFO(STORVSC_DRV, "Storvsc initializing....");
1175 ret = storvsc_drv_init(StorVscInitialize);
1177 DPRINT_EXIT(STORVSC_DRV);
1182 static void __exit storvsc_exit(void)
1184 DPRINT_ENTER(STORVSC_DRV);
1188 DPRINT_ENTER(STORVSC_DRV);
1191 module_param(storvsc_ringbuffer_size, int, S_IRUGO);
1193 module_init(storvsc_init);
1194 module_exit(storvsc_exit);