2 * Copyright (c) 2009, Microsoft Corporation.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307 USA.
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
21 #include <linux/init.h>
22 #include <linux/slab.h>
23 #include <linux/module.h>
24 #include <linux/device.h>
25 #include <linux/blkdev.h>
26 #include <scsi/scsi.h>
27 #include <scsi/scsi_cmnd.h>
28 #include <scsi/scsi_host.h>
29 #include <scsi/scsi_device.h>
30 #include <scsi/scsi_tcq.h>
31 #include <scsi/scsi_eh.h>
32 #include <scsi/scsi_devinfo.h>
33 #include <scsi/scsi_dbg.h>
36 #include "version_info.h"
38 #include "storvsc_api.h"
41 struct host_device_context
{
43 * FIXME this is a bug */
44 /* point back to our device context */
45 struct vm_device
*device_ctx
;
46 struct kmem_cache
*request_pool
;
52 struct storvsc_cmd_request
{
53 struct list_head entry
;
54 struct scsi_cmnd
*cmd
;
56 unsigned int bounce_sgl_count
;
57 struct scatterlist
*bounce_sgl
;
59 struct hv_storvsc_request request
;
60 /* !!!DO NOT ADD ANYTHING BELOW HERE!!! */
61 /* The extension buffer falls right here and is pointed to by
63 * Which sounds like a very bad design... */
66 struct storvsc_driver_context
{
67 /* !! These must be the first 2 fields !! */
68 /* FIXME this is a bug... */
69 struct driver_context drv_ctx
;
70 struct storvsc_driver_object drv_obj
;
74 static int storvsc_probe(struct device
*dev
);
75 static int storvsc_queuecommand(struct Scsi_Host
*shost
, struct scsi_cmnd
*scmnd
);
76 static int storvsc_device_alloc(struct scsi_device
*);
77 static int storvsc_device_configure(struct scsi_device
*);
78 static int storvsc_host_reset_handler(struct scsi_cmnd
*scmnd
);
79 static int storvsc_remove(struct device
*dev
);
81 static struct scatterlist
*create_bounce_buffer(struct scatterlist
*sgl
,
82 unsigned int sg_count
,
84 static void destroy_bounce_buffer(struct scatterlist
*sgl
,
85 unsigned int sg_count
);
86 static int do_bounce_buffer(struct scatterlist
*sgl
, unsigned int sg_count
);
87 static unsigned int copy_from_bounce_buffer(struct scatterlist
*orig_sgl
,
88 struct scatterlist
*bounce_sgl
,
89 unsigned int orig_sgl_count
);
90 static unsigned int copy_to_bounce_buffer(struct scatterlist
*orig_sgl
,
91 struct scatterlist
*bounce_sgl
,
92 unsigned int orig_sgl_count
);
94 static int storvsc_get_chs(struct scsi_device
*sdev
, struct block_device
*bdev
,
95 sector_t capacity
, int *info
);
98 static int storvsc_ringbuffer_size
= STORVSC_RING_BUFFER_SIZE
;
99 module_param(storvsc_ringbuffer_size
, int, S_IRUGO
);
100 MODULE_PARM_DESC(storvsc_ringbuffer_size
, "Ring buffer size (bytes)");
102 /* The one and only one */
103 static struct storvsc_driver_context g_storvsc_drv
;
106 static struct scsi_host_template scsi_driver
= {
107 .module
= THIS_MODULE
,
108 .name
= "storvsc_host_t",
109 .bios_param
= storvsc_get_chs
,
110 .queuecommand
= storvsc_queuecommand
,
111 .eh_host_reset_handler
= storvsc_host_reset_handler
,
112 .slave_alloc
= storvsc_device_alloc
,
113 .slave_configure
= storvsc_device_configure
,
115 /* 64 max_queue * 1 target */
116 .can_queue
= STORVSC_MAX_IO_REQUESTS
*STORVSC_MAX_TARGETS
,
118 /* no use setting to 0 since ll_blk_rw reset it to 1 */
120 .sg_tablesize
= MAX_MULTIPAGE_BUFFER_COUNT
,
122 * ENABLE_CLUSTERING allows mutiple physically contig bio_vecs to merge
123 * into 1 sg element. If set, we must limit the max_segment_size to
124 * PAGE_SIZE, otherwise we may get 1 sg element that represents
127 /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */
128 .use_clustering
= ENABLE_CLUSTERING
,
129 /* Make sure we dont get a sg segment crosses a page boundary */
130 .dma_boundary
= PAGE_SIZE
-1,
135 * storvsc_drv_init - StorVsc driver initialization.
137 static int storvsc_drv_init(int (*drv_init
)(struct hv_driver
*drv
))
140 struct storvsc_driver_object
*storvsc_drv_obj
= &g_storvsc_drv
.drv_obj
;
141 struct driver_context
*drv_ctx
= &g_storvsc_drv
.drv_ctx
;
143 storvsc_drv_obj
->RingBufferSize
= storvsc_ringbuffer_size
;
145 /* Callback to client driver to complete the initialization */
146 drv_init(&storvsc_drv_obj
->Base
);
148 DPRINT_INFO(STORVSC_DRV
,
149 "request extension size %u, max outstanding reqs %u",
150 storvsc_drv_obj
->RequestExtSize
,
151 storvsc_drv_obj
->MaxOutstandingRequestsPerChannel
);
153 if (storvsc_drv_obj
->MaxOutstandingRequestsPerChannel
<
154 STORVSC_MAX_IO_REQUESTS
) {
155 DPRINT_ERR(STORVSC_DRV
,
156 "The number of outstanding io requests (%d) "
157 "is larger than that supported (%d) internally.",
158 STORVSC_MAX_IO_REQUESTS
,
159 storvsc_drv_obj
->MaxOutstandingRequestsPerChannel
);
163 drv_ctx
->driver
.name
= storvsc_drv_obj
->Base
.name
;
164 memcpy(&drv_ctx
->class_id
, &storvsc_drv_obj
->Base
.deviceType
,
165 sizeof(struct hv_guid
));
167 drv_ctx
->probe
= storvsc_probe
;
168 drv_ctx
->remove
= storvsc_remove
;
170 /* The driver belongs to vmbus */
171 ret
= vmbus_child_driver_register(drv_ctx
);
176 static int storvsc_drv_exit_cb(struct device
*dev
, void *data
)
178 struct device
**curr
= (struct device
**)data
;
180 return 1; /* stop iterating */
183 static void storvsc_drv_exit(void)
185 struct storvsc_driver_object
*storvsc_drv_obj
= &g_storvsc_drv
.drv_obj
;
186 struct driver_context
*drv_ctx
= &g_storvsc_drv
.drv_ctx
;
187 struct device
*current_dev
= NULL
;
194 ret
= driver_for_each_device(&drv_ctx
->driver
, NULL
,
195 (void *) ¤t_dev
,
196 storvsc_drv_exit_cb
);
199 DPRINT_WARN(STORVSC_DRV
,
200 "driver_for_each_device returned %d", ret
);
202 if (current_dev
== NULL
)
205 /* Initiate removal from the top-down */
206 device_unregister(current_dev
);
209 if (storvsc_drv_obj
->Base
.OnCleanup
)
210 storvsc_drv_obj
->Base
.OnCleanup(&storvsc_drv_obj
->Base
);
212 vmbus_child_driver_unregister(drv_ctx
);
217 * storvsc_probe - Add a new device for this driver
219 static int storvsc_probe(struct device
*device
)
222 struct driver_context
*driver_ctx
=
223 driver_to_driver_context(device
->driver
);
224 struct storvsc_driver_context
*storvsc_drv_ctx
=
225 (struct storvsc_driver_context
*)driver_ctx
;
226 struct storvsc_driver_object
*storvsc_drv_obj
=
227 &storvsc_drv_ctx
->drv_obj
;
228 struct vm_device
*device_ctx
= device_to_vm_device(device
);
229 struct hv_device
*device_obj
= &device_ctx
->device_obj
;
230 struct Scsi_Host
*host
;
231 struct host_device_context
*host_device_ctx
;
232 struct storvsc_device_info device_info
;
234 if (!storvsc_drv_obj
->Base
.OnDeviceAdd
)
237 host
= scsi_host_alloc(&scsi_driver
,
238 sizeof(struct host_device_context
));
240 DPRINT_ERR(STORVSC_DRV
, "unable to allocate scsi host object");
244 dev_set_drvdata(device
, host
);
246 host_device_ctx
= (struct host_device_context
*)host
->hostdata
;
247 memset(host_device_ctx
, 0, sizeof(struct host_device_context
));
249 host_device_ctx
->port
= host
->host_no
;
250 host_device_ctx
->device_ctx
= device_ctx
;
252 host_device_ctx
->request_pool
=
253 kmem_cache_create(dev_name(&device_ctx
->device
),
254 sizeof(struct storvsc_cmd_request
) +
255 storvsc_drv_obj
->RequestExtSize
, 0,
256 SLAB_HWCACHE_ALIGN
, NULL
);
258 if (!host_device_ctx
->request_pool
) {
263 device_info
.PortNumber
= host
->host_no
;
264 /* Call to the vsc driver to add the device */
265 ret
= storvsc_drv_obj
->Base
.OnDeviceAdd(device_obj
,
266 (void *)&device_info
);
268 DPRINT_ERR(STORVSC_DRV
, "unable to add scsi vsc device");
269 kmem_cache_destroy(host_device_ctx
->request_pool
);
274 /* host_device_ctx->port = device_info.PortNumber; */
275 host_device_ctx
->path
= device_info
.PathId
;
276 host_device_ctx
->target
= device_info
.TargetId
;
278 /* max # of devices per target */
279 host
->max_lun
= STORVSC_MAX_LUNS_PER_TARGET
;
280 /* max # of targets per channel */
281 host
->max_id
= STORVSC_MAX_TARGETS
;
282 /* max # of channels */
283 host
->max_channel
= STORVSC_MAX_CHANNELS
- 1;
285 /* Register the HBA and start the scsi bus scan */
286 ret
= scsi_add_host(host
, device
);
288 DPRINT_ERR(STORVSC_DRV
, "unable to add scsi host device");
290 storvsc_drv_obj
->Base
.OnDeviceRemove(device_obj
);
292 kmem_cache_destroy(host_device_ctx
->request_pool
);
297 scsi_scan_host(host
);
302 * storvsc_remove - Callback when our device is removed
304 static int storvsc_remove(struct device
*device
)
307 struct driver_context
*driver_ctx
=
308 driver_to_driver_context(device
->driver
);
309 struct storvsc_driver_context
*storvsc_drv_ctx
=
310 (struct storvsc_driver_context
*)driver_ctx
;
311 struct storvsc_driver_object
*storvsc_drv_obj
=
312 &storvsc_drv_ctx
->drv_obj
;
313 struct vm_device
*device_ctx
= device_to_vm_device(device
);
314 struct hv_device
*device_obj
= &device_ctx
->device_obj
;
315 struct Scsi_Host
*host
= dev_get_drvdata(device
);
316 struct host_device_context
*host_device_ctx
=
317 (struct host_device_context
*)host
->hostdata
;
320 if (!storvsc_drv_obj
->Base
.OnDeviceRemove
)
324 * Call to the vsc driver to let it know that the device is being
327 ret
= storvsc_drv_obj
->Base
.OnDeviceRemove(device_obj
);
330 DPRINT_ERR(STORVSC
, "unable to remove vsc device (ret %d)",
334 if (host_device_ctx
->request_pool
) {
335 kmem_cache_destroy(host_device_ctx
->request_pool
);
336 host_device_ctx
->request_pool
= NULL
;
339 DPRINT_INFO(STORVSC
, "removing host adapter (%p)...", host
);
340 scsi_remove_host(host
);
342 DPRINT_INFO(STORVSC
, "releasing host adapter (%p)...", host
);
348 * storvsc_commmand_completion - Command completion processing
350 static void storvsc_commmand_completion(struct hv_storvsc_request
*request
)
352 struct storvsc_cmd_request
*cmd_request
=
353 (struct storvsc_cmd_request
*)request
->Context
;
354 struct scsi_cmnd
*scmnd
= cmd_request
->cmd
;
355 struct host_device_context
*host_device_ctx
=
356 (struct host_device_context
*)scmnd
->device
->host
->hostdata
;
357 void (*scsi_done_fn
)(struct scsi_cmnd
*);
358 struct scsi_sense_hdr sense_hdr
;
360 /* ASSERT(request == &cmd_request->request); */
362 /* ASSERT((unsigned long)scmnd->host_scribble == */
363 /* (unsigned long)cmd_request); */
364 /* ASSERT(scmnd->scsi_done); */
366 if (cmd_request
->bounce_sgl_count
) {
367 /* using bounce buffer */
368 /* printk("copy_from_bounce_buffer\n"); */
370 /* FIXME: We can optimize on writes by just skipping this */
371 copy_from_bounce_buffer(scsi_sglist(scmnd
),
372 cmd_request
->bounce_sgl
,
373 scsi_sg_count(scmnd
));
374 destroy_bounce_buffer(cmd_request
->bounce_sgl
,
375 cmd_request
->bounce_sgl_count
);
378 scmnd
->result
= request
->Status
;
381 if (scsi_normalize_sense(scmnd
->sense_buffer
,
382 request
->SenseBufferSize
, &sense_hdr
))
383 scsi_print_sense_hdr("storvsc", &sense_hdr
);
386 /* ASSERT(request->BytesXfer <= request->DataBuffer.Length); */
387 scsi_set_resid(scmnd
, request
->DataBuffer
.Length
- request
->BytesXfer
);
389 scsi_done_fn
= scmnd
->scsi_done
;
391 scmnd
->host_scribble
= NULL
;
392 scmnd
->scsi_done
= NULL
;
394 /* !!DO NOT MODIFY the scmnd after this call */
397 kmem_cache_free(host_device_ctx
->request_pool
, cmd_request
);
400 static int do_bounce_buffer(struct scatterlist
*sgl
, unsigned int sg_count
)
404 /* No need to check */
408 /* We have at least 2 sg entries */
409 for (i
= 0; i
< sg_count
; i
++) {
411 /* make sure 1st one does not have hole */
412 if (sgl
[i
].offset
+ sgl
[i
].length
!= PAGE_SIZE
)
414 } else if (i
== sg_count
- 1) {
415 /* make sure last one does not have hole */
416 if (sgl
[i
].offset
!= 0)
419 /* make sure no hole in the middle */
420 if (sgl
[i
].length
!= PAGE_SIZE
|| sgl
[i
].offset
!= 0)
427 static struct scatterlist
*create_bounce_buffer(struct scatterlist
*sgl
,
428 unsigned int sg_count
,
433 struct scatterlist
*bounce_sgl
;
434 struct page
*page_buf
;
436 num_pages
= ALIGN_UP(len
, PAGE_SIZE
) >> PAGE_SHIFT
;
438 bounce_sgl
= kcalloc(num_pages
, sizeof(struct scatterlist
), GFP_ATOMIC
);
442 for (i
= 0; i
< num_pages
; i
++) {
443 page_buf
= alloc_page(GFP_ATOMIC
);
446 sg_set_page(&bounce_sgl
[i
], page_buf
, 0, 0);
452 destroy_bounce_buffer(bounce_sgl
, num_pages
);
456 static void destroy_bounce_buffer(struct scatterlist
*sgl
,
457 unsigned int sg_count
)
460 struct page
*page_buf
;
462 for (i
= 0; i
< sg_count
; i
++) {
463 page_buf
= sg_page((&sgl
[i
]));
464 if (page_buf
!= NULL
)
465 __free_page(page_buf
);
471 /* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
472 static unsigned int copy_to_bounce_buffer(struct scatterlist
*orig_sgl
,
473 struct scatterlist
*bounce_sgl
,
474 unsigned int orig_sgl_count
)
478 unsigned long src
, dest
;
479 unsigned int srclen
, destlen
, copylen
;
480 unsigned int total_copied
= 0;
481 unsigned long bounce_addr
= 0;
482 unsigned long src_addr
= 0;
485 local_irq_save(flags
);
487 for (i
= 0; i
< orig_sgl_count
; i
++) {
488 src_addr
= (unsigned long)kmap_atomic(sg_page((&orig_sgl
[i
])),
489 KM_IRQ0
) + orig_sgl
[i
].offset
;
491 srclen
= orig_sgl
[i
].length
;
493 /* ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE); */
495 if (bounce_addr
== 0)
496 bounce_addr
= (unsigned long)kmap_atomic(sg_page((&bounce_sgl
[j
])), KM_IRQ0
);
499 /* assume bounce offset always == 0 */
500 dest
= bounce_addr
+ bounce_sgl
[j
].length
;
501 destlen
= PAGE_SIZE
- bounce_sgl
[j
].length
;
503 copylen
= min(srclen
, destlen
);
504 memcpy((void *)dest
, (void *)src
, copylen
);
506 total_copied
+= copylen
;
507 bounce_sgl
[j
].length
+= copylen
;
511 if (bounce_sgl
[j
].length
== PAGE_SIZE
) {
512 /* full..move to next entry */
513 kunmap_atomic((void *)bounce_addr
, KM_IRQ0
);
516 /* if we need to use another bounce buffer */
517 if (srclen
|| i
!= orig_sgl_count
- 1)
518 bounce_addr
= (unsigned long)kmap_atomic(sg_page((&bounce_sgl
[j
])), KM_IRQ0
);
519 } else if (srclen
== 0 && i
== orig_sgl_count
- 1) {
520 /* unmap the last bounce that is < PAGE_SIZE */
521 kunmap_atomic((void *)bounce_addr
, KM_IRQ0
);
525 kunmap_atomic((void *)(src_addr
- orig_sgl
[i
].offset
), KM_IRQ0
);
528 local_irq_restore(flags
);
533 /* Assume the original sgl has enough room */
534 static unsigned int copy_from_bounce_buffer(struct scatterlist
*orig_sgl
,
535 struct scatterlist
*bounce_sgl
,
536 unsigned int orig_sgl_count
)
540 unsigned long src
, dest
;
541 unsigned int srclen
, destlen
, copylen
;
542 unsigned int total_copied
= 0;
543 unsigned long bounce_addr
= 0;
544 unsigned long dest_addr
= 0;
547 local_irq_save(flags
);
549 for (i
= 0; i
< orig_sgl_count
; i
++) {
550 dest_addr
= (unsigned long)kmap_atomic(sg_page((&orig_sgl
[i
])),
551 KM_IRQ0
) + orig_sgl
[i
].offset
;
553 destlen
= orig_sgl
[i
].length
;
554 /* ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE); */
556 if (bounce_addr
== 0)
557 bounce_addr
= (unsigned long)kmap_atomic(sg_page((&bounce_sgl
[j
])), KM_IRQ0
);
560 src
= bounce_addr
+ bounce_sgl
[j
].offset
;
561 srclen
= bounce_sgl
[j
].length
- bounce_sgl
[j
].offset
;
563 copylen
= min(srclen
, destlen
);
564 memcpy((void *)dest
, (void *)src
, copylen
);
566 total_copied
+= copylen
;
567 bounce_sgl
[j
].offset
+= copylen
;
571 if (bounce_sgl
[j
].offset
== bounce_sgl
[j
].length
) {
573 kunmap_atomic((void *)bounce_addr
, KM_IRQ0
);
576 /* if we need to use another bounce buffer */
577 if (destlen
|| i
!= orig_sgl_count
- 1)
578 bounce_addr
= (unsigned long)kmap_atomic(sg_page((&bounce_sgl
[j
])), KM_IRQ0
);
579 } else if (destlen
== 0 && i
== orig_sgl_count
- 1) {
580 /* unmap the last bounce that is < PAGE_SIZE */
581 kunmap_atomic((void *)bounce_addr
, KM_IRQ0
);
585 kunmap_atomic((void *)(dest_addr
- orig_sgl
[i
].offset
),
589 local_irq_restore(flags
);
595 * storvsc_queuecommand - Initiate command processing
597 static int storvsc_queuecommand_lck(struct scsi_cmnd
*scmnd
,
598 void (*done
)(struct scsi_cmnd
*))
601 struct host_device_context
*host_device_ctx
=
602 (struct host_device_context
*)scmnd
->device
->host
->hostdata
;
603 struct vm_device
*device_ctx
= host_device_ctx
->device_ctx
;
604 struct driver_context
*driver_ctx
=
605 driver_to_driver_context(device_ctx
->device
.driver
);
606 struct storvsc_driver_context
*storvsc_drv_ctx
=
607 (struct storvsc_driver_context
*)driver_ctx
;
608 struct storvsc_driver_object
*storvsc_drv_obj
=
609 &storvsc_drv_ctx
->drv_obj
;
610 struct hv_storvsc_request
*request
;
611 struct storvsc_cmd_request
*cmd_request
;
612 unsigned int request_size
= 0;
614 struct scatterlist
*sgl
;
615 unsigned int sg_count
= 0;
617 DPRINT_DBG(STORVSC_DRV
, "scmnd %p dir %d, use_sg %d buf %p len %d "
618 "queue depth %d tagged %d", scmnd
, scmnd
->sc_data_direction
,
619 scsi_sg_count(scmnd
), scsi_sglist(scmnd
),
620 scsi_bufflen(scmnd
), scmnd
->device
->queue_depth
,
621 scmnd
->device
->tagged_supported
);
623 /* If retrying, no need to prep the cmd */
624 if (scmnd
->host_scribble
) {
625 /* ASSERT(scmnd->scsi_done != NULL); */
628 (struct storvsc_cmd_request
*)scmnd
->host_scribble
;
629 DPRINT_INFO(STORVSC_DRV
, "retrying scmnd %p cmd_request %p",
635 /* ASSERT(scmnd->scsi_done == NULL); */
636 /* ASSERT(scmnd->host_scribble == NULL); */
638 scmnd
->scsi_done
= done
;
640 request_size
= sizeof(struct storvsc_cmd_request
);
642 cmd_request
= kmem_cache_alloc(host_device_ctx
->request_pool
,
645 DPRINT_ERR(STORVSC_DRV
, "scmnd (%p) - unable to allocate "
646 "storvsc_cmd_request...marking queue busy", scmnd
);
647 scmnd
->scsi_done
= NULL
;
648 return SCSI_MLQUEUE_DEVICE_BUSY
;
651 /* Setup the cmd request */
652 cmd_request
->bounce_sgl_count
= 0;
653 cmd_request
->bounce_sgl
= NULL
;
654 cmd_request
->cmd
= scmnd
;
656 scmnd
->host_scribble
= (unsigned char *)cmd_request
;
658 request
= &cmd_request
->request
;
661 (void *)((unsigned long)cmd_request
+ request_size
);
662 DPRINT_DBG(STORVSC_DRV
, "req %p size %d ext %d", request
, request_size
,
663 storvsc_drv_obj
->RequestExtSize
);
666 switch (scmnd
->sc_data_direction
) {
668 request
->Type
= WRITE_TYPE
;
670 case DMA_FROM_DEVICE
:
671 request
->Type
= READ_TYPE
;
674 request
->Type
= UNKNOWN_TYPE
;
678 request
->OnIOCompletion
= storvsc_commmand_completion
;
679 request
->Context
= cmd_request
;/* scmnd; */
681 /* request->PortId = scmnd->device->channel; */
682 request
->Host
= host_device_ctx
->port
;
683 request
->Bus
= scmnd
->device
->channel
;
684 request
->TargetId
= scmnd
->device
->id
;
685 request
->LunId
= scmnd
->device
->lun
;
687 /* ASSERT(scmnd->cmd_len <= 16); */
688 request
->CdbLen
= scmnd
->cmd_len
;
689 request
->Cdb
= scmnd
->cmnd
;
691 request
->SenseBuffer
= scmnd
->sense_buffer
;
692 request
->SenseBufferSize
= SCSI_SENSE_BUFFERSIZE
;
695 request
->DataBuffer
.Length
= scsi_bufflen(scmnd
);
696 if (scsi_sg_count(scmnd
)) {
697 sgl
= (struct scatterlist
*)scsi_sglist(scmnd
);
698 sg_count
= scsi_sg_count(scmnd
);
700 /* check if we need to bounce the sgl */
701 if (do_bounce_buffer(sgl
, scsi_sg_count(scmnd
)) != -1) {
702 DPRINT_INFO(STORVSC_DRV
,
703 "need to bounce buffer for this scmnd %p",
705 cmd_request
->bounce_sgl
=
706 create_bounce_buffer(sgl
, scsi_sg_count(scmnd
),
707 scsi_bufflen(scmnd
));
708 if (!cmd_request
->bounce_sgl
) {
709 DPRINT_ERR(STORVSC_DRV
,
710 "unable to create bounce buffer for "
711 "this scmnd %p", scmnd
);
713 scmnd
->scsi_done
= NULL
;
714 scmnd
->host_scribble
= NULL
;
715 kmem_cache_free(host_device_ctx
->request_pool
,
718 return SCSI_MLQUEUE_HOST_BUSY
;
721 cmd_request
->bounce_sgl_count
=
722 ALIGN_UP(scsi_bufflen(scmnd
), PAGE_SIZE
) >>
726 * FIXME: We can optimize on reads by just skipping
729 copy_to_bounce_buffer(sgl
, cmd_request
->bounce_sgl
,
730 scsi_sg_count(scmnd
));
732 sgl
= cmd_request
->bounce_sgl
;
733 sg_count
= cmd_request
->bounce_sgl_count
;
736 request
->DataBuffer
.Offset
= sgl
[0].offset
;
738 for (i
= 0; i
< sg_count
; i
++) {
739 DPRINT_DBG(STORVSC_DRV
, "sgl[%d] len %d offset %d\n",
740 i
, sgl
[i
].length
, sgl
[i
].offset
);
741 request
->DataBuffer
.PfnArray
[i
] =
742 page_to_pfn(sg_page((&sgl
[i
])));
744 } else if (scsi_sglist(scmnd
)) {
745 /* ASSERT(scsi_bufflen(scmnd) <= PAGE_SIZE); */
746 request
->DataBuffer
.Offset
=
747 virt_to_phys(scsi_sglist(scmnd
)) & (PAGE_SIZE
-1);
748 request
->DataBuffer
.PfnArray
[0] =
749 virt_to_phys(scsi_sglist(scmnd
)) >> PAGE_SHIFT
;
753 /* Invokes the vsc to start an IO */
754 ret
= storvsc_drv_obj
->OnIORequest(&device_ctx
->device_obj
,
755 &cmd_request
->request
);
758 DPRINT_ERR(STORVSC_DRV
,
759 "scmnd (%p) - queue FULL...marking queue busy",
762 if (cmd_request
->bounce_sgl_count
) {
764 * FIXME: We can optimize on writes by just skipping
767 copy_from_bounce_buffer(scsi_sglist(scmnd
),
768 cmd_request
->bounce_sgl
,
769 scsi_sg_count(scmnd
));
770 destroy_bounce_buffer(cmd_request
->bounce_sgl
,
771 cmd_request
->bounce_sgl_count
);
774 kmem_cache_free(host_device_ctx
->request_pool
, cmd_request
);
776 scmnd
->scsi_done
= NULL
;
777 scmnd
->host_scribble
= NULL
;
779 ret
= SCSI_MLQUEUE_DEVICE_BUSY
;
785 static DEF_SCSI_QCMD(storvsc_queuecommand
)
787 static int storvsc_merge_bvec(struct request_queue
*q
,
788 struct bvec_merge_data
*bmd
, struct bio_vec
*bvec
)
790 /* checking done by caller. */
795 * storvsc_device_configure - Configure the specified scsi device
797 static int storvsc_device_alloc(struct scsi_device
*sdevice
)
799 DPRINT_DBG(STORVSC_DRV
, "sdev (%p) - setting device flag to %d",
800 sdevice
, BLIST_SPARSELUN
);
802 * This enables luns to be located sparsely. Otherwise, we may not
805 sdevice
->sdev_bflags
|= BLIST_SPARSELUN
| BLIST_LARGELUN
;
809 static int storvsc_device_configure(struct scsi_device
*sdevice
)
811 DPRINT_INFO(STORVSC_DRV
, "sdev (%p) - curr queue depth %d", sdevice
,
812 sdevice
->queue_depth
);
814 DPRINT_INFO(STORVSC_DRV
, "sdev (%p) - setting queue depth to %d",
815 sdevice
, STORVSC_MAX_IO_REQUESTS
);
816 scsi_adjust_queue_depth(sdevice
, MSG_SIMPLE_TAG
,
817 STORVSC_MAX_IO_REQUESTS
);
819 DPRINT_INFO(STORVSC_DRV
, "sdev (%p) - setting max segment size to %ld",
821 blk_queue_max_segment_size(sdevice
->request_queue
, PAGE_SIZE
);
823 DPRINT_INFO(STORVSC_DRV
, "sdev (%p) - adding merge bio vec routine",
825 blk_queue_merge_bvec(sdevice
->request_queue
, storvsc_merge_bvec
);
827 blk_queue_bounce_limit(sdevice
->request_queue
, BLK_BOUNCE_ANY
);
828 /* sdevice->timeout = (2000 * HZ);//(75 * HZ); */
834 * storvsc_host_reset_handler - Reset the scsi HBA
836 static int storvsc_host_reset_handler(struct scsi_cmnd
*scmnd
)
839 struct host_device_context
*host_device_ctx
=
840 (struct host_device_context
*)scmnd
->device
->host
->hostdata
;
841 struct vm_device
*device_ctx
= host_device_ctx
->device_ctx
;
843 DPRINT_INFO(STORVSC_DRV
, "sdev (%p) dev obj (%p) - host resetting...",
844 scmnd
->device
, &device_ctx
->device_obj
);
846 /* Invokes the vsc to reset the host/bus */
847 ret
= StorVscOnHostReset(&device_ctx
->device_obj
);
851 DPRINT_INFO(STORVSC_DRV
, "sdev (%p) dev obj (%p) - host reseted",
852 scmnd
->device
, &device_ctx
->device_obj
);
857 static int storvsc_get_chs(struct scsi_device
*sdev
, struct block_device
* bdev
,
858 sector_t capacity
, int *info
)
860 sector_t total_sectors
= capacity
;
861 sector_t cylinder_times_heads
= 0;
864 int sectors_per_track
= 0;
869 if (total_sectors
> (65535 * 16 * 255))
870 total_sectors
= (65535 * 16 * 255);
872 if (total_sectors
>= (65535 * 16 * 63)) {
873 sectors_per_track
= 255;
876 cylinder_times_heads
= total_sectors
;
877 /* sector_div stores the quotient in cylinder_times_heads */
878 rem
= sector_div(cylinder_times_heads
, sectors_per_track
);
880 sectors_per_track
= 17;
882 cylinder_times_heads
= total_sectors
;
883 /* sector_div stores the quotient in cylinder_times_heads */
884 rem
= sector_div(cylinder_times_heads
, sectors_per_track
);
886 temp
= cylinder_times_heads
+ 1023;
887 /* sector_div stores the quotient in temp */
888 rem
= sector_div(temp
, 1024);
895 if (cylinder_times_heads
>= (heads
* 1024) || (heads
> 16)) {
896 sectors_per_track
= 31;
899 cylinder_times_heads
= total_sectors
;
901 * sector_div stores the quotient in
902 * cylinder_times_heads
904 rem
= sector_div(cylinder_times_heads
,
908 if (cylinder_times_heads
>= (heads
* 1024)) {
909 sectors_per_track
= 63;
912 cylinder_times_heads
= total_sectors
;
914 * sector_div stores the quotient in
915 * cylinder_times_heads
917 rem
= sector_div(cylinder_times_heads
,
922 temp
= cylinder_times_heads
;
923 /* sector_div stores the quotient in temp */
924 rem
= sector_div(temp
, heads
);
928 info
[1] = sectors_per_track
;
931 DPRINT_INFO(STORVSC_DRV
, "CHS (%d, %d, %d)", cylinders
, heads
,
937 static int __init
storvsc_init(void)
941 DPRINT_INFO(STORVSC_DRV
, "Storvsc initializing....");
942 ret
= storvsc_drv_init(StorVscInitialize
);
946 static void __exit
storvsc_exit(void)
951 MODULE_LICENSE("GPL");
952 MODULE_VERSION(HV_DRV_VERSION
);
953 MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
954 module_init(storvsc_init
);
955 module_exit(storvsc_exit
);