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>
20 * K. Y. Srinivasan <kys@microsoft.com>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/module.h>
25 #include <linux/device.h>
26 #include <linux/blkdev.h>
27 #include <scsi/scsi.h>
28 #include <scsi/scsi_cmnd.h>
29 #include <scsi/scsi_host.h>
30 #include <scsi/scsi_device.h>
31 #include <scsi/scsi_tcq.h>
32 #include <scsi/scsi_eh.h>
33 #include <scsi/scsi_devinfo.h>
34 #include <scsi/scsi_dbg.h>
40 #include "storvsc_api.h"
43 static int storvsc_ringbuffer_size = STORVSC_RING_BUFFER_SIZE;
45 module_param(storvsc_ringbuffer_size, int, S_IRUGO);
46 MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)");
48 static const char *driver_name = "storvsc";
50 /* {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f} */
51 static const struct hv_guid gStorVscDeviceType = {
53 0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
54 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f
58 struct hv_host_device {
59 struct hv_device *dev;
60 struct kmem_cache *request_pool;
66 struct storvsc_cmd_request {
67 struct list_head entry;
68 struct scsi_cmnd *cmd;
70 unsigned int bounce_sgl_count;
71 struct scatterlist *bounce_sgl;
73 struct hv_storvsc_request request;
77 static int storvsc_device_alloc(struct scsi_device *sdevice)
80 * This enables luns to be located sparsely. Otherwise, we may not
83 sdevice->sdev_bflags |= BLIST_SPARSELUN | BLIST_LARGELUN;
87 static int storvsc_merge_bvec(struct request_queue *q,
88 struct bvec_merge_data *bmd, struct bio_vec *bvec)
90 /* checking done by caller. */
94 static int storvsc_device_configure(struct scsi_device *sdevice)
96 scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG,
97 STORVSC_MAX_IO_REQUESTS);
99 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - setting max segment size to %ld",
101 blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
103 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - adding merge bio vec routine",
105 blk_queue_merge_bvec(sdevice->request_queue, storvsc_merge_bvec);
107 blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
112 static void destroy_bounce_buffer(struct scatterlist *sgl,
113 unsigned int sg_count)
116 struct page *page_buf;
118 for (i = 0; i < sg_count; i++) {
119 page_buf = sg_page((&sgl[i]));
120 if (page_buf != NULL)
121 __free_page(page_buf);
127 static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
131 /* No need to check */
135 /* We have at least 2 sg entries */
136 for (i = 0; i < sg_count; i++) {
138 /* make sure 1st one does not have hole */
139 if (sgl[i].offset + sgl[i].length != PAGE_SIZE)
141 } else if (i == sg_count - 1) {
142 /* make sure last one does not have hole */
143 if (sgl[i].offset != 0)
146 /* make sure no hole in the middle */
147 if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0)
154 static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
155 unsigned int sg_count,
160 struct scatterlist *bounce_sgl;
161 struct page *page_buf;
163 num_pages = ALIGN(len, PAGE_SIZE) >> PAGE_SHIFT;
165 bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC);
169 for (i = 0; i < num_pages; i++) {
170 page_buf = alloc_page(GFP_ATOMIC);
173 sg_set_page(&bounce_sgl[i], page_buf, 0, 0);
179 destroy_bounce_buffer(bounce_sgl, num_pages);
184 /* Assume the original sgl has enough room */
185 static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
186 struct scatterlist *bounce_sgl,
187 unsigned int orig_sgl_count)
191 unsigned long src, dest;
192 unsigned int srclen, destlen, copylen;
193 unsigned int total_copied = 0;
194 unsigned long bounce_addr = 0;
195 unsigned long dest_addr = 0;
198 local_irq_save(flags);
200 for (i = 0; i < orig_sgl_count; i++) {
201 dest_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
202 KM_IRQ0) + orig_sgl[i].offset;
204 destlen = orig_sgl[i].length;
206 if (bounce_addr == 0)
208 (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])),
212 src = bounce_addr + bounce_sgl[j].offset;
213 srclen = bounce_sgl[j].length - bounce_sgl[j].offset;
215 copylen = min(srclen, destlen);
216 memcpy((void *)dest, (void *)src, copylen);
218 total_copied += copylen;
219 bounce_sgl[j].offset += copylen;
223 if (bounce_sgl[j].offset == bounce_sgl[j].length) {
225 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
228 /* if we need to use another bounce buffer */
229 if (destlen || i != orig_sgl_count - 1)
231 (unsigned long)kmap_atomic(
232 sg_page((&bounce_sgl[j])), KM_IRQ0);
233 } else if (destlen == 0 && i == orig_sgl_count - 1) {
234 /* unmap the last bounce that is < PAGE_SIZE */
235 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
239 kunmap_atomic((void *)(dest_addr - orig_sgl[i].offset),
243 local_irq_restore(flags);
249 /* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
250 static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
251 struct scatterlist *bounce_sgl,
252 unsigned int orig_sgl_count)
256 unsigned long src, dest;
257 unsigned int srclen, destlen, copylen;
258 unsigned int total_copied = 0;
259 unsigned long bounce_addr = 0;
260 unsigned long src_addr = 0;
263 local_irq_save(flags);
265 for (i = 0; i < orig_sgl_count; i++) {
266 src_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
267 KM_IRQ0) + orig_sgl[i].offset;
269 srclen = orig_sgl[i].length;
271 if (bounce_addr == 0)
273 (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])),
277 /* assume bounce offset always == 0 */
278 dest = bounce_addr + bounce_sgl[j].length;
279 destlen = PAGE_SIZE - bounce_sgl[j].length;
281 copylen = min(srclen, destlen);
282 memcpy((void *)dest, (void *)src, copylen);
284 total_copied += copylen;
285 bounce_sgl[j].length += copylen;
289 if (bounce_sgl[j].length == PAGE_SIZE) {
290 /* full..move to next entry */
291 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
294 /* if we need to use another bounce buffer */
295 if (srclen || i != orig_sgl_count - 1)
297 (unsigned long)kmap_atomic(
298 sg_page((&bounce_sgl[j])), KM_IRQ0);
300 } else if (srclen == 0 && i == orig_sgl_count - 1) {
301 /* unmap the last bounce that is < PAGE_SIZE */
302 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
306 kunmap_atomic((void *)(src_addr - orig_sgl[i].offset), KM_IRQ0);
309 local_irq_restore(flags);
316 * storvsc_remove - Callback when our device is removed
318 static int storvsc_remove(struct hv_device *dev)
320 struct Scsi_Host *host = dev_get_drvdata(&dev->device);
321 struct hv_host_device *host_dev =
322 (struct hv_host_device *)host->hostdata;
325 * Call to the vsc driver to let it know that the device is being
328 storvsc_dev_remove(dev);
330 if (host_dev->request_pool) {
331 kmem_cache_destroy(host_dev->request_pool);
332 host_dev->request_pool = NULL;
335 DPRINT_INFO(STORVSC, "removing host adapter (%p)...", host);
336 scsi_remove_host(host);
338 DPRINT_INFO(STORVSC, "releasing host adapter (%p)...", host);
344 static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
345 sector_t capacity, int *info)
347 sector_t nsect = capacity;
348 sector_t cylinders = nsect;
349 int heads, sectors_pt;
352 * We are making up these values; let us keep it simple.
355 sectors_pt = 0x3f; /* Sectors per track */
356 sector_div(cylinders, heads * sectors_pt);
357 if ((sector_t)(cylinders + 1) * heads * sectors_pt < nsect)
361 info[1] = sectors_pt;
362 info[2] = (int)cylinders;
364 DPRINT_INFO(STORVSC_DRV, "CHS (%d, %d, %d)", (int)cylinders, heads,
370 static int storvsc_host_reset(struct hv_device *device)
372 struct storvsc_device *stor_device;
373 struct hv_storvsc_request *request;
374 struct vstor_packet *vstor_packet;
377 DPRINT_INFO(STORVSC, "resetting host adapter...");
379 stor_device = get_stor_device(device);
383 request = &stor_device->reset_request;
384 vstor_packet = &request->vstor_packet;
386 init_completion(&request->wait_event);
388 vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
389 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
390 vstor_packet->vm_srb.path_id = stor_device->path_id;
392 ret = vmbus_sendpacket(device->channel, vstor_packet,
393 sizeof(struct vstor_packet),
394 (unsigned long)&stor_device->reset_request,
396 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
400 t = wait_for_completion_timeout(&request->wait_event, HZ);
406 DPRINT_INFO(STORVSC, "host adapter reset completed");
409 * At this point, all outstanding requests in the adapter
410 * should have been flushed out and return to us
414 put_stor_device(device);
420 * storvsc_host_reset_handler - Reset the scsi HBA
422 static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
425 struct hv_host_device *host_dev =
426 (struct hv_host_device *)scmnd->device->host->hostdata;
427 struct hv_device *dev = host_dev->dev;
429 DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host resetting...",
432 /* Invokes the vsc to reset the host/bus */
433 ret = storvsc_host_reset(dev);
437 DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host reseted",
445 * storvsc_commmand_completion - Command completion processing
447 static void storvsc_commmand_completion(struct hv_storvsc_request *request)
449 struct storvsc_cmd_request *cmd_request =
450 (struct storvsc_cmd_request *)request->context;
451 struct scsi_cmnd *scmnd = cmd_request->cmd;
452 struct hv_host_device *host_dev =
453 (struct hv_host_device *)scmnd->device->host->hostdata;
454 void (*scsi_done_fn)(struct scsi_cmnd *);
455 struct scsi_sense_hdr sense_hdr;
456 struct vmscsi_request *vm_srb;
458 if (cmd_request->bounce_sgl_count) {
460 /* FIXME: We can optimize on writes by just skipping this */
461 copy_from_bounce_buffer(scsi_sglist(scmnd),
462 cmd_request->bounce_sgl,
463 scsi_sg_count(scmnd));
464 destroy_bounce_buffer(cmd_request->bounce_sgl,
465 cmd_request->bounce_sgl_count);
468 vm_srb = &request->vstor_packet.vm_srb;
469 scmnd->result = vm_srb->scsi_status;
472 if (scsi_normalize_sense(scmnd->sense_buffer,
473 SCSI_SENSE_BUFFERSIZE, &sense_hdr))
474 scsi_print_sense_hdr("storvsc", &sense_hdr);
477 scsi_set_resid(scmnd,
478 request->data_buffer.len -
479 vm_srb->data_transfer_length);
481 scsi_done_fn = scmnd->scsi_done;
483 scmnd->host_scribble = NULL;
484 scmnd->scsi_done = NULL;
486 /* !!DO NOT MODIFY the scmnd after this call */
489 kmem_cache_free(host_dev->request_pool, cmd_request);
494 * storvsc_queuecommand - Initiate command processing
496 static int storvsc_queuecommand_lck(struct scsi_cmnd *scmnd,
497 void (*done)(struct scsi_cmnd *))
500 struct hv_host_device *host_dev =
501 (struct hv_host_device *)scmnd->device->host->hostdata;
502 struct hv_device *dev = host_dev->dev;
503 struct hv_storvsc_request *request;
504 struct storvsc_cmd_request *cmd_request;
505 unsigned int request_size = 0;
507 struct scatterlist *sgl;
508 unsigned int sg_count = 0;
509 struct vmscsi_request *vm_srb;
512 /* If retrying, no need to prep the cmd */
513 if (scmnd->host_scribble) {
516 (struct storvsc_cmd_request *)scmnd->host_scribble;
517 DPRINT_INFO(STORVSC_DRV, "retrying scmnd %p cmd_request %p",
523 scmnd->scsi_done = done;
525 request_size = sizeof(struct storvsc_cmd_request);
527 cmd_request = kmem_cache_zalloc(host_dev->request_pool,
530 scmnd->scsi_done = NULL;
531 return SCSI_MLQUEUE_DEVICE_BUSY;
534 /* Setup the cmd request */
535 cmd_request->bounce_sgl_count = 0;
536 cmd_request->bounce_sgl = NULL;
537 cmd_request->cmd = scmnd;
539 scmnd->host_scribble = (unsigned char *)cmd_request;
541 request = &cmd_request->request;
542 vm_srb = &request->vstor_packet.vm_srb;
546 switch (scmnd->sc_data_direction) {
548 vm_srb->data_in = WRITE_TYPE;
550 case DMA_FROM_DEVICE:
551 vm_srb->data_in = READ_TYPE;
554 vm_srb->data_in = UNKNOWN_TYPE;
558 request->on_io_completion = storvsc_commmand_completion;
559 request->context = cmd_request;/* scmnd; */
561 vm_srb->port_number = host_dev->port;
562 vm_srb->path_id = scmnd->device->channel;
563 vm_srb->target_id = scmnd->device->id;
564 vm_srb->lun = scmnd->device->lun;
566 vm_srb->cdb_length = scmnd->cmd_len;
568 memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
570 request->sense_buffer = scmnd->sense_buffer;
573 request->data_buffer.len = scsi_bufflen(scmnd);
574 if (scsi_sg_count(scmnd)) {
575 sgl = (struct scatterlist *)scsi_sglist(scmnd);
576 sg_count = scsi_sg_count(scmnd);
578 /* check if we need to bounce the sgl */
579 if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) {
580 cmd_request->bounce_sgl =
581 create_bounce_buffer(sgl, scsi_sg_count(scmnd),
582 scsi_bufflen(scmnd));
583 if (!cmd_request->bounce_sgl) {
584 scmnd->scsi_done = NULL;
585 scmnd->host_scribble = NULL;
586 kmem_cache_free(host_dev->request_pool,
589 return SCSI_MLQUEUE_HOST_BUSY;
592 cmd_request->bounce_sgl_count =
593 ALIGN(scsi_bufflen(scmnd), PAGE_SIZE) >>
597 * FIXME: We can optimize on reads by just skipping
600 copy_to_bounce_buffer(sgl, cmd_request->bounce_sgl,
601 scsi_sg_count(scmnd));
603 sgl = cmd_request->bounce_sgl;
604 sg_count = cmd_request->bounce_sgl_count;
607 request->data_buffer.offset = sgl[0].offset;
609 for (i = 0; i < sg_count; i++)
610 request->data_buffer.pfn_array[i] =
611 page_to_pfn(sg_page((&sgl[i])));
613 } else if (scsi_sglist(scmnd)) {
614 request->data_buffer.offset =
615 virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
616 request->data_buffer.pfn_array[0] =
617 virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
621 /* Invokes the vsc to start an IO */
622 ret = storvsc_do_io(dev, &cmd_request->request);
627 if (cmd_request->bounce_sgl_count) {
629 * FIXME: We can optimize on writes by just skipping
632 copy_from_bounce_buffer(scsi_sglist(scmnd),
633 cmd_request->bounce_sgl,
634 scsi_sg_count(scmnd));
635 destroy_bounce_buffer(cmd_request->bounce_sgl,
636 cmd_request->bounce_sgl_count);
639 kmem_cache_free(host_dev->request_pool, cmd_request);
641 scmnd->scsi_done = NULL;
642 scmnd->host_scribble = NULL;
644 ret = SCSI_MLQUEUE_DEVICE_BUSY;
650 static DEF_SCSI_QCMD(storvsc_queuecommand)
654 static struct scsi_host_template scsi_driver = {
655 .module = THIS_MODULE,
656 .name = "storvsc_host_t",
657 .bios_param = storvsc_get_chs,
658 .queuecommand = storvsc_queuecommand,
659 .eh_host_reset_handler = storvsc_host_reset_handler,
660 .slave_alloc = storvsc_device_alloc,
661 .slave_configure = storvsc_device_configure,
663 /* 64 max_queue * 1 target */
664 .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS,
666 /* no use setting to 0 since ll_blk_rw reset it to 1 */
668 .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT,
670 * ENABLE_CLUSTERING allows mutiple physically contig bio_vecs to merge
671 * into 1 sg element. If set, we must limit the max_segment_size to
672 * PAGE_SIZE, otherwise we may get 1 sg element that represents
675 /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */
676 .use_clustering = ENABLE_CLUSTERING,
677 /* Make sure we dont get a sg segment crosses a page boundary */
678 .dma_boundary = PAGE_SIZE-1,
683 * storvsc_probe - Add a new device for this driver
686 static int storvsc_probe(struct hv_device *device)
689 struct Scsi_Host *host;
690 struct hv_host_device *host_dev;
691 struct storvsc_device_info device_info;
693 host = scsi_host_alloc(&scsi_driver,
694 sizeof(struct hv_host_device));
698 dev_set_drvdata(&device->device, host);
700 host_dev = (struct hv_host_device *)host->hostdata;
701 memset(host_dev, 0, sizeof(struct hv_host_device));
703 host_dev->port = host->host_no;
704 host_dev->dev = device;
706 host_dev->request_pool =
707 kmem_cache_create(dev_name(&device->device),
708 sizeof(struct storvsc_cmd_request), 0,
709 SLAB_HWCACHE_ALIGN, NULL);
711 if (!host_dev->request_pool) {
716 device_info.port_number = host->host_no;
717 device_info.ring_buffer_size = storvsc_ringbuffer_size;
718 /* Call to the vsc driver to add the device */
719 ret = storvsc_dev_add(device, (void *)&device_info);
722 kmem_cache_destroy(host_dev->request_pool);
727 host_dev->path = device_info.path_id;
728 host_dev->target = device_info.target_id;
730 /* max # of devices per target */
731 host->max_lun = STORVSC_MAX_LUNS_PER_TARGET;
732 /* max # of targets per channel */
733 host->max_id = STORVSC_MAX_TARGETS;
734 /* max # of channels */
735 host->max_channel = STORVSC_MAX_CHANNELS - 1;
737 /* Register the HBA and start the scsi bus scan */
738 ret = scsi_add_host(host, &device->device);
741 storvsc_dev_remove(device);
743 kmem_cache_destroy(host_dev->request_pool);
748 scsi_scan_host(host);
752 /* The one and only one */
754 static struct hv_driver storvsc_drv = {
755 .probe = storvsc_probe,
756 .remove = storvsc_remove,
761 * storvsc_drv_init - StorVsc driver initialization.
763 static int storvsc_drv_init(void)
766 struct hv_driver *drv = &storvsc_drv;
767 u32 max_outstanding_req_per_channel;
770 * Divide the ring buffer data size (which is 1 page less
771 * than the ring buffer size since that page is reserved for
772 * the ring buffer indices) by the max request size (which is
773 * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
776 max_outstanding_req_per_channel =
777 ((storvsc_ringbuffer_size - PAGE_SIZE) /
778 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
779 sizeof(struct vstor_packet) + sizeof(u64),
782 memcpy(&drv->dev_type, &gStorVscDeviceType,
783 sizeof(struct hv_guid));
785 if (max_outstanding_req_per_channel <
786 STORVSC_MAX_IO_REQUESTS)
789 drv->name = driver_name;
790 drv->driver.name = driver_name;
793 /* The driver belongs to vmbus */
794 ret = vmbus_child_driver_register(&drv->driver);
799 static int storvsc_drv_exit_cb(struct device *dev, void *data)
801 struct device **curr = (struct device **)data;
803 return 1; /* stop iterating */
806 static void storvsc_drv_exit(void)
808 struct hv_driver *drv = &storvsc_drv;
809 struct device *current_dev = NULL;
816 ret = driver_for_each_device(&drv->driver, NULL,
817 (void *) ¤t_dev,
818 storvsc_drv_exit_cb);
821 if (current_dev == NULL)
824 /* Initiate removal from the top-down */
825 device_unregister(current_dev);
828 vmbus_child_driver_unregister(&drv->driver);
832 static int __init storvsc_init(void)
836 DPRINT_INFO(STORVSC_DRV, "Storvsc initializing....");
837 ret = storvsc_drv_init();
841 static void __exit storvsc_exit(void)
846 MODULE_LICENSE("GPL");
847 MODULE_VERSION(HV_DRV_VERSION);
848 MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
849 module_init(storvsc_init);
850 module_exit(storvsc_exit);