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>
37 #include "version_info.h"
39 #include "storvsc_api.h"
43 static int storvsc_ringbuffer_size
= STORVSC_RING_BUFFER_SIZE
;
45 static const char *driver_name
= "storvsc";
47 /* {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f} */
48 static const struct hv_guid gStorVscDeviceType
= {
50 0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
51 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f
55 struct hv_host_device
{
56 struct hv_device
*dev
;
57 struct kmem_cache
*request_pool
;
63 struct storvsc_cmd_request
{
64 struct list_head entry
;
65 struct scsi_cmnd
*cmd
;
67 unsigned int bounce_sgl_count
;
68 struct scatterlist
*bounce_sgl
;
70 struct hv_storvsc_request request
;
75 * storvsc_initialize - Main entry point
77 static int storvsc_initialize(struct hv_driver
*driver
)
79 struct storvsc_driver
*stor_driver
;
81 stor_driver
= hvdr_to_stordr(driver
);
84 /* Make sure we are at least 2 pages since 1 page is used for control */
86 driver
->name
= driver_name
;
87 memcpy(&driver
->dev_type
, &gStorVscDeviceType
,
88 sizeof(struct hv_guid
));
92 * Divide the ring buffer data size (which is 1 page less
93 * than the ring buffer size since that page is reserved for
94 * the ring buffer indices) by the max request size (which is
95 * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
97 stor_driver
->max_outstanding_req_per_channel
=
98 ((stor_driver
->ring_buffer_size
- PAGE_SIZE
) /
99 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET
+
100 sizeof(struct vstor_packet
) + sizeof(u64
),
103 DPRINT_INFO(STORVSC
, "max io %u, currently %u\n",
104 stor_driver
->max_outstanding_req_per_channel
,
105 STORVSC_MAX_IO_REQUESTS
);
107 /* Setup the dispatch table */
108 stor_driver
->base
.dev_add
= storvsc_dev_add
;
109 stor_driver
->base
.dev_rm
= storvsc_dev_remove
;
111 stor_driver
->on_io_request
= storvsc_do_io
;
116 static int storvsc_device_alloc(struct scsi_device
*sdevice
)
119 * This enables luns to be located sparsely. Otherwise, we may not
122 sdevice
->sdev_bflags
|= BLIST_SPARSELUN
| BLIST_LARGELUN
;
126 static int storvsc_merge_bvec(struct request_queue
*q
,
127 struct bvec_merge_data
*bmd
, struct bio_vec
*bvec
)
129 /* checking done by caller. */
133 static int storvsc_device_configure(struct scsi_device
*sdevice
)
135 scsi_adjust_queue_depth(sdevice
, MSG_SIMPLE_TAG
,
136 STORVSC_MAX_IO_REQUESTS
);
138 DPRINT_INFO(STORVSC_DRV
, "sdev (%p) - setting max segment size to %ld",
140 blk_queue_max_segment_size(sdevice
->request_queue
, PAGE_SIZE
);
142 DPRINT_INFO(STORVSC_DRV
, "sdev (%p) - adding merge bio vec routine",
144 blk_queue_merge_bvec(sdevice
->request_queue
, storvsc_merge_bvec
);
146 blk_queue_bounce_limit(sdevice
->request_queue
, BLK_BOUNCE_ANY
);
147 /* sdevice->timeout = (2000 * HZ);//(75 * HZ); */
152 static void destroy_bounce_buffer(struct scatterlist
*sgl
,
153 unsigned int sg_count
)
156 struct page
*page_buf
;
158 for (i
= 0; i
< sg_count
; i
++) {
159 page_buf
= sg_page((&sgl
[i
]));
160 if (page_buf
!= NULL
)
161 __free_page(page_buf
);
167 static int do_bounce_buffer(struct scatterlist
*sgl
, unsigned int sg_count
)
171 /* No need to check */
175 /* We have at least 2 sg entries */
176 for (i
= 0; i
< sg_count
; i
++) {
178 /* make sure 1st one does not have hole */
179 if (sgl
[i
].offset
+ sgl
[i
].length
!= PAGE_SIZE
)
181 } else if (i
== sg_count
- 1) {
182 /* make sure last one does not have hole */
183 if (sgl
[i
].offset
!= 0)
186 /* make sure no hole in the middle */
187 if (sgl
[i
].length
!= PAGE_SIZE
|| sgl
[i
].offset
!= 0)
194 static struct scatterlist
*create_bounce_buffer(struct scatterlist
*sgl
,
195 unsigned int sg_count
,
200 struct scatterlist
*bounce_sgl
;
201 struct page
*page_buf
;
203 num_pages
= ALIGN(len
, PAGE_SIZE
) >> PAGE_SHIFT
;
205 bounce_sgl
= kcalloc(num_pages
, sizeof(struct scatterlist
), GFP_ATOMIC
);
209 for (i
= 0; i
< num_pages
; i
++) {
210 page_buf
= alloc_page(GFP_ATOMIC
);
213 sg_set_page(&bounce_sgl
[i
], page_buf
, 0, 0);
219 destroy_bounce_buffer(bounce_sgl
, num_pages
);
224 /* Assume the original sgl has enough room */
225 static unsigned int copy_from_bounce_buffer(struct scatterlist
*orig_sgl
,
226 struct scatterlist
*bounce_sgl
,
227 unsigned int orig_sgl_count
)
231 unsigned long src
, dest
;
232 unsigned int srclen
, destlen
, copylen
;
233 unsigned int total_copied
= 0;
234 unsigned long bounce_addr
= 0;
235 unsigned long dest_addr
= 0;
238 local_irq_save(flags
);
240 for (i
= 0; i
< orig_sgl_count
; i
++) {
241 dest_addr
= (unsigned long)kmap_atomic(sg_page((&orig_sgl
[i
])),
242 KM_IRQ0
) + orig_sgl
[i
].offset
;
244 destlen
= orig_sgl
[i
].length
;
246 if (bounce_addr
== 0)
248 (unsigned long)kmap_atomic(sg_page((&bounce_sgl
[j
])),
252 src
= bounce_addr
+ bounce_sgl
[j
].offset
;
253 srclen
= bounce_sgl
[j
].length
- bounce_sgl
[j
].offset
;
255 copylen
= min(srclen
, destlen
);
256 memcpy((void *)dest
, (void *)src
, copylen
);
258 total_copied
+= copylen
;
259 bounce_sgl
[j
].offset
+= copylen
;
263 if (bounce_sgl
[j
].offset
== bounce_sgl
[j
].length
) {
265 kunmap_atomic((void *)bounce_addr
, KM_IRQ0
);
268 /* if we need to use another bounce buffer */
269 if (destlen
|| i
!= orig_sgl_count
- 1)
271 (unsigned long)kmap_atomic(
272 sg_page((&bounce_sgl
[j
])), KM_IRQ0
);
273 } else if (destlen
== 0 && i
== orig_sgl_count
- 1) {
274 /* unmap the last bounce that is < PAGE_SIZE */
275 kunmap_atomic((void *)bounce_addr
, KM_IRQ0
);
279 kunmap_atomic((void *)(dest_addr
- orig_sgl
[i
].offset
),
283 local_irq_restore(flags
);
289 /* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
290 static unsigned int copy_to_bounce_buffer(struct scatterlist
*orig_sgl
,
291 struct scatterlist
*bounce_sgl
,
292 unsigned int orig_sgl_count
)
296 unsigned long src
, dest
;
297 unsigned int srclen
, destlen
, copylen
;
298 unsigned int total_copied
= 0;
299 unsigned long bounce_addr
= 0;
300 unsigned long src_addr
= 0;
303 local_irq_save(flags
);
305 for (i
= 0; i
< orig_sgl_count
; i
++) {
306 src_addr
= (unsigned long)kmap_atomic(sg_page((&orig_sgl
[i
])),
307 KM_IRQ0
) + orig_sgl
[i
].offset
;
309 srclen
= orig_sgl
[i
].length
;
311 if (bounce_addr
== 0)
313 (unsigned long)kmap_atomic(sg_page((&bounce_sgl
[j
])),
317 /* assume bounce offset always == 0 */
318 dest
= bounce_addr
+ bounce_sgl
[j
].length
;
319 destlen
= PAGE_SIZE
- bounce_sgl
[j
].length
;
321 copylen
= min(srclen
, destlen
);
322 memcpy((void *)dest
, (void *)src
, copylen
);
324 total_copied
+= copylen
;
325 bounce_sgl
[j
].length
+= copylen
;
329 if (bounce_sgl
[j
].length
== PAGE_SIZE
) {
330 /* full..move to next entry */
331 kunmap_atomic((void *)bounce_addr
, KM_IRQ0
);
334 /* if we need to use another bounce buffer */
335 if (srclen
|| i
!= orig_sgl_count
- 1)
337 (unsigned long)kmap_atomic(
338 sg_page((&bounce_sgl
[j
])), KM_IRQ0
);
340 } else if (srclen
== 0 && i
== orig_sgl_count
- 1) {
341 /* unmap the last bounce that is < PAGE_SIZE */
342 kunmap_atomic((void *)bounce_addr
, KM_IRQ0
);
346 kunmap_atomic((void *)(src_addr
- orig_sgl
[i
].offset
), KM_IRQ0
);
349 local_irq_restore(flags
);
356 * storvsc_remove - Callback when our device is removed
358 static int storvsc_remove(struct hv_device
*dev
)
360 struct storvsc_driver
*storvsc_drv_obj
=
361 drv_to_stordrv(dev
->device
.driver
);
362 struct Scsi_Host
*host
= dev_get_drvdata(&dev
->device
);
363 struct hv_host_device
*host_dev
=
364 (struct hv_host_device
*)host
->hostdata
;
367 * Call to the vsc driver to let it know that the device is being
370 storvsc_drv_obj
->base
.dev_rm(dev
);
372 if (host_dev
->request_pool
) {
373 kmem_cache_destroy(host_dev
->request_pool
);
374 host_dev
->request_pool
= NULL
;
377 DPRINT_INFO(STORVSC
, "removing host adapter (%p)...", host
);
378 scsi_remove_host(host
);
380 DPRINT_INFO(STORVSC
, "releasing host adapter (%p)...", host
);
386 static int storvsc_get_chs(struct scsi_device
*sdev
, struct block_device
* bdev
,
387 sector_t capacity
, int *info
)
389 sector_t total_sectors
= capacity
;
390 sector_t cylinder_times_heads
= 0;
393 int sectors_per_track
= 0;
398 if (total_sectors
> (65535 * 16 * 255))
399 total_sectors
= (65535 * 16 * 255);
401 if (total_sectors
>= (65535 * 16 * 63)) {
402 sectors_per_track
= 255;
405 cylinder_times_heads
= total_sectors
;
406 /* sector_div stores the quotient in cylinder_times_heads */
407 rem
= sector_div(cylinder_times_heads
, sectors_per_track
);
409 sectors_per_track
= 17;
411 cylinder_times_heads
= total_sectors
;
412 /* sector_div stores the quotient in cylinder_times_heads */
413 rem
= sector_div(cylinder_times_heads
, sectors_per_track
);
415 temp
= cylinder_times_heads
+ 1023;
416 /* sector_div stores the quotient in temp */
417 rem
= sector_div(temp
, 1024);
424 if (cylinder_times_heads
>= (heads
* 1024) || (heads
> 16)) {
425 sectors_per_track
= 31;
428 cylinder_times_heads
= total_sectors
;
430 * sector_div stores the quotient in
431 * cylinder_times_heads
433 rem
= sector_div(cylinder_times_heads
,
437 if (cylinder_times_heads
>= (heads
* 1024)) {
438 sectors_per_track
= 63;
441 cylinder_times_heads
= total_sectors
;
443 * sector_div stores the quotient in
444 * cylinder_times_heads
446 rem
= sector_div(cylinder_times_heads
,
451 temp
= cylinder_times_heads
;
452 /* sector_div stores the quotient in temp */
453 rem
= sector_div(temp
, heads
);
457 info
[1] = sectors_per_track
;
460 DPRINT_INFO(STORVSC_DRV
, "CHS (%d, %d, %d)", cylinders
, heads
,
466 static int storvsc_host_reset(struct hv_device
*device
)
468 struct storvsc_device
*stor_device
;
469 struct hv_storvsc_request
*request
;
470 struct vstor_packet
*vstor_packet
;
473 DPRINT_INFO(STORVSC
, "resetting host adapter...");
475 stor_device
= get_stor_device(device
);
479 request
= &stor_device
->reset_request
;
480 vstor_packet
= &request
->vstor_packet
;
482 init_completion(&request
->wait_event
);
484 vstor_packet
->operation
= VSTOR_OPERATION_RESET_BUS
;
485 vstor_packet
->flags
= REQUEST_COMPLETION_FLAG
;
486 vstor_packet
->vm_srb
.path_id
= stor_device
->path_id
;
488 ret
= vmbus_sendpacket(device
->channel
, vstor_packet
,
489 sizeof(struct vstor_packet
),
490 (unsigned long)&stor_device
->reset_request
,
492 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
496 t
= wait_for_completion_timeout(&request
->wait_event
, HZ
);
502 DPRINT_INFO(STORVSC
, "host adapter reset completed");
505 * At this point, all outstanding requests in the adapter
506 * should have been flushed out and return to us
510 put_stor_device(device
);
516 * storvsc_host_reset_handler - Reset the scsi HBA
518 static int storvsc_host_reset_handler(struct scsi_cmnd
*scmnd
)
521 struct hv_host_device
*host_dev
=
522 (struct hv_host_device
*)scmnd
->device
->host
->hostdata
;
523 struct hv_device
*dev
= host_dev
->dev
;
525 DPRINT_INFO(STORVSC_DRV
, "sdev (%p) dev obj (%p) - host resetting...",
528 /* Invokes the vsc to reset the host/bus */
529 ret
= storvsc_host_reset(dev
);
533 DPRINT_INFO(STORVSC_DRV
, "sdev (%p) dev obj (%p) - host reseted",
540 static int storvsc_probe(struct hv_device
*dev
);
541 static int storvsc_queuecommand(struct Scsi_Host
*shost
, struct scsi_cmnd
*scmnd
);
543 module_param(storvsc_ringbuffer_size
, int, S_IRUGO
);
544 MODULE_PARM_DESC(storvsc_ringbuffer_size
, "Ring buffer size (bytes)");
546 /* The one and only one */
547 static struct storvsc_driver g_storvsc_drv
;
550 static struct scsi_host_template scsi_driver
= {
551 .module
= THIS_MODULE
,
552 .name
= "storvsc_host_t",
553 .bios_param
= storvsc_get_chs
,
554 .queuecommand
= storvsc_queuecommand
,
555 .eh_host_reset_handler
= storvsc_host_reset_handler
,
556 .slave_alloc
= storvsc_device_alloc
,
557 .slave_configure
= storvsc_device_configure
,
559 /* 64 max_queue * 1 target */
560 .can_queue
= STORVSC_MAX_IO_REQUESTS
*STORVSC_MAX_TARGETS
,
562 /* no use setting to 0 since ll_blk_rw reset it to 1 */
564 .sg_tablesize
= MAX_MULTIPAGE_BUFFER_COUNT
,
566 * ENABLE_CLUSTERING allows mutiple physically contig bio_vecs to merge
567 * into 1 sg element. If set, we must limit the max_segment_size to
568 * PAGE_SIZE, otherwise we may get 1 sg element that represents
571 /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */
572 .use_clustering
= ENABLE_CLUSTERING
,
573 /* Make sure we dont get a sg segment crosses a page boundary */
574 .dma_boundary
= PAGE_SIZE
-1,
579 * storvsc_drv_init - StorVsc driver initialization.
581 static int storvsc_drv_init(void)
584 struct storvsc_driver
*storvsc_drv_obj
= &g_storvsc_drv
;
585 struct hv_driver
*drv
= &g_storvsc_drv
.base
;
587 storvsc_drv_obj
->ring_buffer_size
= storvsc_ringbuffer_size
;
589 /* Callback to client driver to complete the initialization */
590 storvsc_initialize(&storvsc_drv_obj
->base
);
592 DPRINT_INFO(STORVSC_DRV
,
593 "max outstanding reqs %u",
594 storvsc_drv_obj
->max_outstanding_req_per_channel
);
596 if (storvsc_drv_obj
->max_outstanding_req_per_channel
<
597 STORVSC_MAX_IO_REQUESTS
)
600 drv
->driver
.name
= storvsc_drv_obj
->base
.name
;
602 drv
->probe
= storvsc_probe
;
603 drv
->remove
= storvsc_remove
;
605 /* The driver belongs to vmbus */
606 ret
= vmbus_child_driver_register(&drv
->driver
);
611 static int storvsc_drv_exit_cb(struct device
*dev
, void *data
)
613 struct device
**curr
= (struct device
**)data
;
615 return 1; /* stop iterating */
618 static void storvsc_drv_exit(void)
620 struct storvsc_driver
*storvsc_drv_obj
= &g_storvsc_drv
;
621 struct hv_driver
*drv
= &g_storvsc_drv
.base
;
622 struct device
*current_dev
= NULL
;
629 ret
= driver_for_each_device(&drv
->driver
, NULL
,
630 (void *) ¤t_dev
,
631 storvsc_drv_exit_cb
);
634 if (current_dev
== NULL
)
637 /* Initiate removal from the top-down */
638 device_unregister(current_dev
);
641 if (storvsc_drv_obj
->base
.cleanup
)
642 storvsc_drv_obj
->base
.cleanup(&storvsc_drv_obj
->base
);
644 vmbus_child_driver_unregister(&drv
->driver
);
649 * storvsc_probe - Add a new device for this driver
651 static int storvsc_probe(struct hv_device
*device
)
654 struct storvsc_driver
*storvsc_drv_obj
=
655 drv_to_stordrv(device
->device
.driver
);
656 struct Scsi_Host
*host
;
657 struct hv_host_device
*host_dev
;
658 struct storvsc_device_info device_info
;
660 if (!storvsc_drv_obj
->base
.dev_add
)
663 host
= scsi_host_alloc(&scsi_driver
,
664 sizeof(struct hv_host_device
));
668 dev_set_drvdata(&device
->device
, host
);
670 host_dev
= (struct hv_host_device
*)host
->hostdata
;
671 memset(host_dev
, 0, sizeof(struct hv_host_device
));
673 host_dev
->port
= host
->host_no
;
674 host_dev
->dev
= device
;
676 host_dev
->request_pool
=
677 kmem_cache_create(dev_name(&device
->device
),
678 sizeof(struct storvsc_cmd_request
), 0,
679 SLAB_HWCACHE_ALIGN
, NULL
);
681 if (!host_dev
->request_pool
) {
686 device_info
.port_number
= host
->host_no
;
687 /* Call to the vsc driver to add the device */
688 ret
= storvsc_drv_obj
->base
.dev_add(device
, (void *)&device_info
);
691 kmem_cache_destroy(host_dev
->request_pool
);
696 host_dev
->path
= device_info
.path_id
;
697 host_dev
->target
= device_info
.target_id
;
699 /* max # of devices per target */
700 host
->max_lun
= STORVSC_MAX_LUNS_PER_TARGET
;
701 /* max # of targets per channel */
702 host
->max_id
= STORVSC_MAX_TARGETS
;
703 /* max # of channels */
704 host
->max_channel
= STORVSC_MAX_CHANNELS
- 1;
706 /* Register the HBA and start the scsi bus scan */
707 ret
= scsi_add_host(host
, &device
->device
);
710 storvsc_drv_obj
->base
.dev_rm(device
);
712 kmem_cache_destroy(host_dev
->request_pool
);
717 scsi_scan_host(host
);
722 * storvsc_commmand_completion - Command completion processing
724 static void storvsc_commmand_completion(struct hv_storvsc_request
*request
)
726 struct storvsc_cmd_request
*cmd_request
=
727 (struct storvsc_cmd_request
*)request
->context
;
728 struct scsi_cmnd
*scmnd
= cmd_request
->cmd
;
729 struct hv_host_device
*host_dev
=
730 (struct hv_host_device
*)scmnd
->device
->host
->hostdata
;
731 void (*scsi_done_fn
)(struct scsi_cmnd
*);
732 struct scsi_sense_hdr sense_hdr
;
733 struct vmscsi_request
*vm_srb
;
735 /* ASSERT(request == &cmd_request->request); */
737 /* ASSERT((unsigned long)scmnd->host_scribble == */
738 /* (unsigned long)cmd_request); */
739 /* ASSERT(scmnd->scsi_done); */
741 if (cmd_request
->bounce_sgl_count
) {
742 /* using bounce buffer */
743 /* printk("copy_from_bounce_buffer\n"); */
745 /* FIXME: We can optimize on writes by just skipping this */
746 copy_from_bounce_buffer(scsi_sglist(scmnd
),
747 cmd_request
->bounce_sgl
,
748 scsi_sg_count(scmnd
));
749 destroy_bounce_buffer(cmd_request
->bounce_sgl
,
750 cmd_request
->bounce_sgl_count
);
753 vm_srb
= &request
->vstor_packet
.vm_srb
;
754 scmnd
->result
= vm_srb
->scsi_status
;
757 if (scsi_normalize_sense(scmnd
->sense_buffer
,
758 SCSI_SENSE_BUFFERSIZE
, &sense_hdr
))
759 scsi_print_sense_hdr("storvsc", &sense_hdr
);
762 /* ASSERT(request->BytesXfer <= request->data_buffer.Length); */
763 scsi_set_resid(scmnd
,
764 request
->data_buffer
.len
-
765 vm_srb
->data_transfer_length
);
767 scsi_done_fn
= scmnd
->scsi_done
;
769 scmnd
->host_scribble
= NULL
;
770 scmnd
->scsi_done
= NULL
;
772 /* !!DO NOT MODIFY the scmnd after this call */
775 kmem_cache_free(host_dev
->request_pool
, cmd_request
);
779 * storvsc_queuecommand - Initiate command processing
781 static int storvsc_queuecommand_lck(struct scsi_cmnd
*scmnd
,
782 void (*done
)(struct scsi_cmnd
*))
785 struct hv_host_device
*host_dev
=
786 (struct hv_host_device
*)scmnd
->device
->host
->hostdata
;
787 struct hv_device
*dev
= host_dev
->dev
;
788 struct storvsc_driver
*storvsc_drv_obj
=
789 drv_to_stordrv(dev
->device
.driver
);
790 struct hv_storvsc_request
*request
;
791 struct storvsc_cmd_request
*cmd_request
;
792 unsigned int request_size
= 0;
794 struct scatterlist
*sgl
;
795 unsigned int sg_count
= 0;
796 struct vmscsi_request
*vm_srb
;
799 /* If retrying, no need to prep the cmd */
800 if (scmnd
->host_scribble
) {
801 /* ASSERT(scmnd->scsi_done != NULL); */
804 (struct storvsc_cmd_request
*)scmnd
->host_scribble
;
805 DPRINT_INFO(STORVSC_DRV
, "retrying scmnd %p cmd_request %p",
811 /* ASSERT(scmnd->scsi_done == NULL); */
812 /* ASSERT(scmnd->host_scribble == NULL); */
814 scmnd
->scsi_done
= done
;
816 request_size
= sizeof(struct storvsc_cmd_request
);
818 cmd_request
= kmem_cache_zalloc(host_dev
->request_pool
,
821 scmnd
->scsi_done
= NULL
;
822 return SCSI_MLQUEUE_DEVICE_BUSY
;
825 /* Setup the cmd request */
826 cmd_request
->bounce_sgl_count
= 0;
827 cmd_request
->bounce_sgl
= NULL
;
828 cmd_request
->cmd
= scmnd
;
830 scmnd
->host_scribble
= (unsigned char *)cmd_request
;
832 request
= &cmd_request
->request
;
833 vm_srb
= &request
->vstor_packet
.vm_srb
;
837 switch (scmnd
->sc_data_direction
) {
839 vm_srb
->data_in
= WRITE_TYPE
;
841 case DMA_FROM_DEVICE
:
842 vm_srb
->data_in
= READ_TYPE
;
845 vm_srb
->data_in
= UNKNOWN_TYPE
;
849 request
->on_io_completion
= storvsc_commmand_completion
;
850 request
->context
= cmd_request
;/* scmnd; */
852 /* request->PortId = scmnd->device->channel; */
853 vm_srb
->port_number
= host_dev
->port
;
854 vm_srb
->path_id
= scmnd
->device
->channel
;
855 vm_srb
->target_id
= scmnd
->device
->id
;
856 vm_srb
->lun
= scmnd
->device
->lun
;
858 /* ASSERT(scmnd->cmd_len <= 16); */
859 vm_srb
->cdb_length
= scmnd
->cmd_len
;
861 memcpy(vm_srb
->cdb
, scmnd
->cmnd
, vm_srb
->cdb_length
);
863 request
->sense_buffer
= scmnd
->sense_buffer
;
866 request
->data_buffer
.len
= scsi_bufflen(scmnd
);
867 if (scsi_sg_count(scmnd
)) {
868 sgl
= (struct scatterlist
*)scsi_sglist(scmnd
);
869 sg_count
= scsi_sg_count(scmnd
);
871 /* check if we need to bounce the sgl */
872 if (do_bounce_buffer(sgl
, scsi_sg_count(scmnd
)) != -1) {
873 cmd_request
->bounce_sgl
=
874 create_bounce_buffer(sgl
, scsi_sg_count(scmnd
),
875 scsi_bufflen(scmnd
));
876 if (!cmd_request
->bounce_sgl
) {
877 scmnd
->scsi_done
= NULL
;
878 scmnd
->host_scribble
= NULL
;
879 kmem_cache_free(host_dev
->request_pool
,
882 return SCSI_MLQUEUE_HOST_BUSY
;
885 cmd_request
->bounce_sgl_count
=
886 ALIGN(scsi_bufflen(scmnd
), PAGE_SIZE
) >>
890 * FIXME: We can optimize on reads by just skipping
893 copy_to_bounce_buffer(sgl
, cmd_request
->bounce_sgl
,
894 scsi_sg_count(scmnd
));
896 sgl
= cmd_request
->bounce_sgl
;
897 sg_count
= cmd_request
->bounce_sgl_count
;
900 request
->data_buffer
.offset
= sgl
[0].offset
;
902 for (i
= 0; i
< sg_count
; i
++)
903 request
->data_buffer
.pfn_array
[i
] =
904 page_to_pfn(sg_page((&sgl
[i
])));
906 } else if (scsi_sglist(scmnd
)) {
907 /* ASSERT(scsi_bufflen(scmnd) <= PAGE_SIZE); */
908 request
->data_buffer
.offset
=
909 virt_to_phys(scsi_sglist(scmnd
)) & (PAGE_SIZE
-1);
910 request
->data_buffer
.pfn_array
[0] =
911 virt_to_phys(scsi_sglist(scmnd
)) >> PAGE_SHIFT
;
915 /* Invokes the vsc to start an IO */
916 ret
= storvsc_drv_obj
->on_io_request(dev
,
917 &cmd_request
->request
);
921 if (cmd_request
->bounce_sgl_count
) {
923 * FIXME: We can optimize on writes by just skipping
926 copy_from_bounce_buffer(scsi_sglist(scmnd
),
927 cmd_request
->bounce_sgl
,
928 scsi_sg_count(scmnd
));
929 destroy_bounce_buffer(cmd_request
->bounce_sgl
,
930 cmd_request
->bounce_sgl_count
);
933 kmem_cache_free(host_dev
->request_pool
, cmd_request
);
935 scmnd
->scsi_done
= NULL
;
936 scmnd
->host_scribble
= NULL
;
938 ret
= SCSI_MLQUEUE_DEVICE_BUSY
;
944 static DEF_SCSI_QCMD(storvsc_queuecommand
)
946 static int __init
storvsc_init(void)
950 DPRINT_INFO(STORVSC_DRV
, "Storvsc initializing....");
951 ret
= storvsc_drv_init();
955 static void __exit
storvsc_exit(void)
960 MODULE_LICENSE("GPL");
961 MODULE_VERSION(HV_DRV_VERSION
);
962 MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
963 module_init(storvsc_init
);
964 module_exit(storvsc_exit
);