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 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
;
78 * storvsc_initialize - Main entry point
80 static int storvsc_initialize(struct hv_driver
*driver
)
82 struct storvsc_driver
*stor_driver
;
84 stor_driver
= hvdr_to_stordr(driver
);
87 /* Make sure we are at least 2 pages since 1 page is used for control */
89 driver
->name
= driver_name
;
90 memcpy(&driver
->dev_type
, &gStorVscDeviceType
,
91 sizeof(struct hv_guid
));
95 * Divide the ring buffer data size (which is 1 page less
96 * than the ring buffer size since that page is reserved for
97 * the ring buffer indices) by the max request size (which is
98 * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
100 stor_driver
->max_outstanding_req_per_channel
=
101 ((stor_driver
->ring_buffer_size
- PAGE_SIZE
) /
102 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET
+
103 sizeof(struct vstor_packet
) + sizeof(u64
),
106 DPRINT_INFO(STORVSC
, "max io %u, currently %u\n",
107 stor_driver
->max_outstanding_req_per_channel
,
108 STORVSC_MAX_IO_REQUESTS
);
110 /* Setup the dispatch table */
111 stor_driver
->base
.dev_add
= storvsc_dev_add
;
112 stor_driver
->base
.dev_rm
= storvsc_dev_remove
;
114 stor_driver
->on_io_request
= storvsc_do_io
;
119 static int storvsc_device_alloc(struct scsi_device
*sdevice
)
122 * This enables luns to be located sparsely. Otherwise, we may not
125 sdevice
->sdev_bflags
|= BLIST_SPARSELUN
| BLIST_LARGELUN
;
129 static int storvsc_merge_bvec(struct request_queue
*q
,
130 struct bvec_merge_data
*bmd
, struct bio_vec
*bvec
)
132 /* checking done by caller. */
136 static int storvsc_device_configure(struct scsi_device
*sdevice
)
138 scsi_adjust_queue_depth(sdevice
, MSG_SIMPLE_TAG
,
139 STORVSC_MAX_IO_REQUESTS
);
141 DPRINT_INFO(STORVSC_DRV
, "sdev (%p) - setting max segment size to %ld",
143 blk_queue_max_segment_size(sdevice
->request_queue
, PAGE_SIZE
);
145 DPRINT_INFO(STORVSC_DRV
, "sdev (%p) - adding merge bio vec routine",
147 blk_queue_merge_bvec(sdevice
->request_queue
, storvsc_merge_bvec
);
149 blk_queue_bounce_limit(sdevice
->request_queue
, BLK_BOUNCE_ANY
);
150 /* sdevice->timeout = (2000 * HZ);//(75 * HZ); */
155 static void destroy_bounce_buffer(struct scatterlist
*sgl
,
156 unsigned int sg_count
)
159 struct page
*page_buf
;
161 for (i
= 0; i
< sg_count
; i
++) {
162 page_buf
= sg_page((&sgl
[i
]));
163 if (page_buf
!= NULL
)
164 __free_page(page_buf
);
170 static int do_bounce_buffer(struct scatterlist
*sgl
, unsigned int sg_count
)
174 /* No need to check */
178 /* We have at least 2 sg entries */
179 for (i
= 0; i
< sg_count
; i
++) {
181 /* make sure 1st one does not have hole */
182 if (sgl
[i
].offset
+ sgl
[i
].length
!= PAGE_SIZE
)
184 } else if (i
== sg_count
- 1) {
185 /* make sure last one does not have hole */
186 if (sgl
[i
].offset
!= 0)
189 /* make sure no hole in the middle */
190 if (sgl
[i
].length
!= PAGE_SIZE
|| sgl
[i
].offset
!= 0)
197 static struct scatterlist
*create_bounce_buffer(struct scatterlist
*sgl
,
198 unsigned int sg_count
,
203 struct scatterlist
*bounce_sgl
;
204 struct page
*page_buf
;
206 num_pages
= ALIGN(len
, PAGE_SIZE
) >> PAGE_SHIFT
;
208 bounce_sgl
= kcalloc(num_pages
, sizeof(struct scatterlist
), GFP_ATOMIC
);
212 for (i
= 0; i
< num_pages
; i
++) {
213 page_buf
= alloc_page(GFP_ATOMIC
);
216 sg_set_page(&bounce_sgl
[i
], page_buf
, 0, 0);
222 destroy_bounce_buffer(bounce_sgl
, num_pages
);
227 /* Assume the original sgl has enough room */
228 static unsigned int copy_from_bounce_buffer(struct scatterlist
*orig_sgl
,
229 struct scatterlist
*bounce_sgl
,
230 unsigned int orig_sgl_count
)
234 unsigned long src
, dest
;
235 unsigned int srclen
, destlen
, copylen
;
236 unsigned int total_copied
= 0;
237 unsigned long bounce_addr
= 0;
238 unsigned long dest_addr
= 0;
241 local_irq_save(flags
);
243 for (i
= 0; i
< orig_sgl_count
; i
++) {
244 dest_addr
= (unsigned long)kmap_atomic(sg_page((&orig_sgl
[i
])),
245 KM_IRQ0
) + orig_sgl
[i
].offset
;
247 destlen
= orig_sgl
[i
].length
;
249 if (bounce_addr
== 0)
251 (unsigned long)kmap_atomic(sg_page((&bounce_sgl
[j
])),
255 src
= bounce_addr
+ bounce_sgl
[j
].offset
;
256 srclen
= bounce_sgl
[j
].length
- bounce_sgl
[j
].offset
;
258 copylen
= min(srclen
, destlen
);
259 memcpy((void *)dest
, (void *)src
, copylen
);
261 total_copied
+= copylen
;
262 bounce_sgl
[j
].offset
+= copylen
;
266 if (bounce_sgl
[j
].offset
== bounce_sgl
[j
].length
) {
268 kunmap_atomic((void *)bounce_addr
, KM_IRQ0
);
271 /* if we need to use another bounce buffer */
272 if (destlen
|| i
!= orig_sgl_count
- 1)
274 (unsigned long)kmap_atomic(
275 sg_page((&bounce_sgl
[j
])), KM_IRQ0
);
276 } else if (destlen
== 0 && i
== orig_sgl_count
- 1) {
277 /* unmap the last bounce that is < PAGE_SIZE */
278 kunmap_atomic((void *)bounce_addr
, KM_IRQ0
);
282 kunmap_atomic((void *)(dest_addr
- orig_sgl
[i
].offset
),
286 local_irq_restore(flags
);
292 /* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
293 static unsigned int copy_to_bounce_buffer(struct scatterlist
*orig_sgl
,
294 struct scatterlist
*bounce_sgl
,
295 unsigned int orig_sgl_count
)
299 unsigned long src
, dest
;
300 unsigned int srclen
, destlen
, copylen
;
301 unsigned int total_copied
= 0;
302 unsigned long bounce_addr
= 0;
303 unsigned long src_addr
= 0;
306 local_irq_save(flags
);
308 for (i
= 0; i
< orig_sgl_count
; i
++) {
309 src_addr
= (unsigned long)kmap_atomic(sg_page((&orig_sgl
[i
])),
310 KM_IRQ0
) + orig_sgl
[i
].offset
;
312 srclen
= orig_sgl
[i
].length
;
314 if (bounce_addr
== 0)
316 (unsigned long)kmap_atomic(sg_page((&bounce_sgl
[j
])),
320 /* assume bounce offset always == 0 */
321 dest
= bounce_addr
+ bounce_sgl
[j
].length
;
322 destlen
= PAGE_SIZE
- bounce_sgl
[j
].length
;
324 copylen
= min(srclen
, destlen
);
325 memcpy((void *)dest
, (void *)src
, copylen
);
327 total_copied
+= copylen
;
328 bounce_sgl
[j
].length
+= copylen
;
332 if (bounce_sgl
[j
].length
== PAGE_SIZE
) {
333 /* full..move to next entry */
334 kunmap_atomic((void *)bounce_addr
, KM_IRQ0
);
337 /* if we need to use another bounce buffer */
338 if (srclen
|| i
!= orig_sgl_count
- 1)
340 (unsigned long)kmap_atomic(
341 sg_page((&bounce_sgl
[j
])), KM_IRQ0
);
343 } else if (srclen
== 0 && i
== orig_sgl_count
- 1) {
344 /* unmap the last bounce that is < PAGE_SIZE */
345 kunmap_atomic((void *)bounce_addr
, KM_IRQ0
);
349 kunmap_atomic((void *)(src_addr
- orig_sgl
[i
].offset
), KM_IRQ0
);
352 local_irq_restore(flags
);
359 * storvsc_remove - Callback when our device is removed
361 static int storvsc_remove(struct hv_device
*dev
)
363 struct storvsc_driver
*storvsc_drv_obj
=
364 drv_to_stordrv(dev
->device
.driver
);
365 struct Scsi_Host
*host
= dev_get_drvdata(&dev
->device
);
366 struct hv_host_device
*host_dev
=
367 (struct hv_host_device
*)host
->hostdata
;
370 * Call to the vsc driver to let it know that the device is being
373 storvsc_drv_obj
->base
.dev_rm(dev
);
375 if (host_dev
->request_pool
) {
376 kmem_cache_destroy(host_dev
->request_pool
);
377 host_dev
->request_pool
= NULL
;
380 DPRINT_INFO(STORVSC
, "removing host adapter (%p)...", host
);
381 scsi_remove_host(host
);
383 DPRINT_INFO(STORVSC
, "releasing host adapter (%p)...", host
);
389 static int storvsc_get_chs(struct scsi_device
*sdev
, struct block_device
* bdev
,
390 sector_t capacity
, int *info
)
392 sector_t total_sectors
= capacity
;
393 sector_t cylinder_times_heads
= 0;
396 int sectors_per_track
= 0;
401 if (total_sectors
> (65535 * 16 * 255))
402 total_sectors
= (65535 * 16 * 255);
404 if (total_sectors
>= (65535 * 16 * 63)) {
405 sectors_per_track
= 255;
408 cylinder_times_heads
= total_sectors
;
409 /* sector_div stores the quotient in cylinder_times_heads */
410 rem
= sector_div(cylinder_times_heads
, sectors_per_track
);
412 sectors_per_track
= 17;
414 cylinder_times_heads
= total_sectors
;
415 /* sector_div stores the quotient in cylinder_times_heads */
416 rem
= sector_div(cylinder_times_heads
, sectors_per_track
);
418 temp
= cylinder_times_heads
+ 1023;
419 /* sector_div stores the quotient in temp */
420 rem
= sector_div(temp
, 1024);
427 if (cylinder_times_heads
>= (heads
* 1024) || (heads
> 16)) {
428 sectors_per_track
= 31;
431 cylinder_times_heads
= total_sectors
;
433 * sector_div stores the quotient in
434 * cylinder_times_heads
436 rem
= sector_div(cylinder_times_heads
,
440 if (cylinder_times_heads
>= (heads
* 1024)) {
441 sectors_per_track
= 63;
444 cylinder_times_heads
= total_sectors
;
446 * sector_div stores the quotient in
447 * cylinder_times_heads
449 rem
= sector_div(cylinder_times_heads
,
454 temp
= cylinder_times_heads
;
455 /* sector_div stores the quotient in temp */
456 rem
= sector_div(temp
, heads
);
460 info
[1] = sectors_per_track
;
463 DPRINT_INFO(STORVSC_DRV
, "CHS (%d, %d, %d)", cylinders
, heads
,
469 static int storvsc_host_reset(struct hv_device
*device
)
471 struct storvsc_device
*stor_device
;
472 struct hv_storvsc_request
*request
;
473 struct vstor_packet
*vstor_packet
;
476 DPRINT_INFO(STORVSC
, "resetting host adapter...");
478 stor_device
= get_stor_device(device
);
482 request
= &stor_device
->reset_request
;
483 vstor_packet
= &request
->vstor_packet
;
485 init_completion(&request
->wait_event
);
487 vstor_packet
->operation
= VSTOR_OPERATION_RESET_BUS
;
488 vstor_packet
->flags
= REQUEST_COMPLETION_FLAG
;
489 vstor_packet
->vm_srb
.path_id
= stor_device
->path_id
;
491 ret
= vmbus_sendpacket(device
->channel
, vstor_packet
,
492 sizeof(struct vstor_packet
),
493 (unsigned long)&stor_device
->reset_request
,
495 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
499 t
= wait_for_completion_timeout(&request
->wait_event
, HZ
);
505 DPRINT_INFO(STORVSC
, "host adapter reset completed");
508 * At this point, all outstanding requests in the adapter
509 * should have been flushed out and return to us
513 put_stor_device(device
);
519 * storvsc_host_reset_handler - Reset the scsi HBA
521 static int storvsc_host_reset_handler(struct scsi_cmnd
*scmnd
)
524 struct hv_host_device
*host_dev
=
525 (struct hv_host_device
*)scmnd
->device
->host
->hostdata
;
526 struct hv_device
*dev
= host_dev
->dev
;
528 DPRINT_INFO(STORVSC_DRV
, "sdev (%p) dev obj (%p) - host resetting...",
531 /* Invokes the vsc to reset the host/bus */
532 ret
= storvsc_host_reset(dev
);
536 DPRINT_INFO(STORVSC_DRV
, "sdev (%p) dev obj (%p) - host reseted",
544 * storvsc_commmand_completion - Command completion processing
546 static void storvsc_commmand_completion(struct hv_storvsc_request
*request
)
548 struct storvsc_cmd_request
*cmd_request
=
549 (struct storvsc_cmd_request
*)request
->context
;
550 struct scsi_cmnd
*scmnd
= cmd_request
->cmd
;
551 struct hv_host_device
*host_dev
=
552 (struct hv_host_device
*)scmnd
->device
->host
->hostdata
;
553 void (*scsi_done_fn
)(struct scsi_cmnd
*);
554 struct scsi_sense_hdr sense_hdr
;
555 struct vmscsi_request
*vm_srb
;
557 /* ASSERT(request == &cmd_request->request); */
559 /* ASSERT((unsigned long)scmnd->host_scribble == */
560 /* (unsigned long)cmd_request); */
561 /* ASSERT(scmnd->scsi_done); */
563 if (cmd_request
->bounce_sgl_count
) {
564 /* using bounce buffer */
565 /* printk("copy_from_bounce_buffer\n"); */
567 /* FIXME: We can optimize on writes by just skipping this */
568 copy_from_bounce_buffer(scsi_sglist(scmnd
),
569 cmd_request
->bounce_sgl
,
570 scsi_sg_count(scmnd
));
571 destroy_bounce_buffer(cmd_request
->bounce_sgl
,
572 cmd_request
->bounce_sgl_count
);
575 vm_srb
= &request
->vstor_packet
.vm_srb
;
576 scmnd
->result
= vm_srb
->scsi_status
;
579 if (scsi_normalize_sense(scmnd
->sense_buffer
,
580 SCSI_SENSE_BUFFERSIZE
, &sense_hdr
))
581 scsi_print_sense_hdr("storvsc", &sense_hdr
);
584 /* ASSERT(request->BytesXfer <= request->data_buffer.Length); */
585 scsi_set_resid(scmnd
,
586 request
->data_buffer
.len
-
587 vm_srb
->data_transfer_length
);
589 scsi_done_fn
= scmnd
->scsi_done
;
591 scmnd
->host_scribble
= NULL
;
592 scmnd
->scsi_done
= NULL
;
594 /* !!DO NOT MODIFY the scmnd after this call */
597 kmem_cache_free(host_dev
->request_pool
, cmd_request
);
602 * storvsc_queuecommand - Initiate command processing
604 static int storvsc_queuecommand_lck(struct scsi_cmnd
*scmnd
,
605 void (*done
)(struct scsi_cmnd
*))
608 struct hv_host_device
*host_dev
=
609 (struct hv_host_device
*)scmnd
->device
->host
->hostdata
;
610 struct hv_device
*dev
= host_dev
->dev
;
611 struct storvsc_driver
*storvsc_drv_obj
=
612 drv_to_stordrv(dev
->device
.driver
);
613 struct hv_storvsc_request
*request
;
614 struct storvsc_cmd_request
*cmd_request
;
615 unsigned int request_size
= 0;
617 struct scatterlist
*sgl
;
618 unsigned int sg_count
= 0;
619 struct vmscsi_request
*vm_srb
;
622 /* If retrying, no need to prep the cmd */
623 if (scmnd
->host_scribble
) {
624 /* ASSERT(scmnd->scsi_done != NULL); */
627 (struct storvsc_cmd_request
*)scmnd
->host_scribble
;
628 DPRINT_INFO(STORVSC_DRV
, "retrying scmnd %p cmd_request %p",
634 /* ASSERT(scmnd->scsi_done == NULL); */
635 /* ASSERT(scmnd->host_scribble == NULL); */
637 scmnd
->scsi_done
= done
;
639 request_size
= sizeof(struct storvsc_cmd_request
);
641 cmd_request
= kmem_cache_zalloc(host_dev
->request_pool
,
644 scmnd
->scsi_done
= NULL
;
645 return SCSI_MLQUEUE_DEVICE_BUSY
;
648 /* Setup the cmd request */
649 cmd_request
->bounce_sgl_count
= 0;
650 cmd_request
->bounce_sgl
= NULL
;
651 cmd_request
->cmd
= scmnd
;
653 scmnd
->host_scribble
= (unsigned char *)cmd_request
;
655 request
= &cmd_request
->request
;
656 vm_srb
= &request
->vstor_packet
.vm_srb
;
660 switch (scmnd
->sc_data_direction
) {
662 vm_srb
->data_in
= WRITE_TYPE
;
664 case DMA_FROM_DEVICE
:
665 vm_srb
->data_in
= READ_TYPE
;
668 vm_srb
->data_in
= UNKNOWN_TYPE
;
672 request
->on_io_completion
= storvsc_commmand_completion
;
673 request
->context
= cmd_request
;/* scmnd; */
675 /* request->PortId = scmnd->device->channel; */
676 vm_srb
->port_number
= host_dev
->port
;
677 vm_srb
->path_id
= scmnd
->device
->channel
;
678 vm_srb
->target_id
= scmnd
->device
->id
;
679 vm_srb
->lun
= scmnd
->device
->lun
;
681 /* ASSERT(scmnd->cmd_len <= 16); */
682 vm_srb
->cdb_length
= scmnd
->cmd_len
;
684 memcpy(vm_srb
->cdb
, scmnd
->cmnd
, vm_srb
->cdb_length
);
686 request
->sense_buffer
= scmnd
->sense_buffer
;
689 request
->data_buffer
.len
= scsi_bufflen(scmnd
);
690 if (scsi_sg_count(scmnd
)) {
691 sgl
= (struct scatterlist
*)scsi_sglist(scmnd
);
692 sg_count
= scsi_sg_count(scmnd
);
694 /* check if we need to bounce the sgl */
695 if (do_bounce_buffer(sgl
, scsi_sg_count(scmnd
)) != -1) {
696 cmd_request
->bounce_sgl
=
697 create_bounce_buffer(sgl
, scsi_sg_count(scmnd
),
698 scsi_bufflen(scmnd
));
699 if (!cmd_request
->bounce_sgl
) {
700 scmnd
->scsi_done
= NULL
;
701 scmnd
->host_scribble
= NULL
;
702 kmem_cache_free(host_dev
->request_pool
,
705 return SCSI_MLQUEUE_HOST_BUSY
;
708 cmd_request
->bounce_sgl_count
=
709 ALIGN(scsi_bufflen(scmnd
), PAGE_SIZE
) >>
713 * FIXME: We can optimize on reads by just skipping
716 copy_to_bounce_buffer(sgl
, cmd_request
->bounce_sgl
,
717 scsi_sg_count(scmnd
));
719 sgl
= cmd_request
->bounce_sgl
;
720 sg_count
= cmd_request
->bounce_sgl_count
;
723 request
->data_buffer
.offset
= sgl
[0].offset
;
725 for (i
= 0; i
< sg_count
; i
++)
726 request
->data_buffer
.pfn_array
[i
] =
727 page_to_pfn(sg_page((&sgl
[i
])));
729 } else if (scsi_sglist(scmnd
)) {
730 /* ASSERT(scsi_bufflen(scmnd) <= PAGE_SIZE); */
731 request
->data_buffer
.offset
=
732 virt_to_phys(scsi_sglist(scmnd
)) & (PAGE_SIZE
-1);
733 request
->data_buffer
.pfn_array
[0] =
734 virt_to_phys(scsi_sglist(scmnd
)) >> PAGE_SHIFT
;
738 /* Invokes the vsc to start an IO */
739 ret
= storvsc_drv_obj
->on_io_request(dev
,
740 &cmd_request
->request
);
744 if (cmd_request
->bounce_sgl_count
) {
746 * FIXME: We can optimize on writes by just skipping
749 copy_from_bounce_buffer(scsi_sglist(scmnd
),
750 cmd_request
->bounce_sgl
,
751 scsi_sg_count(scmnd
));
752 destroy_bounce_buffer(cmd_request
->bounce_sgl
,
753 cmd_request
->bounce_sgl_count
);
756 kmem_cache_free(host_dev
->request_pool
, cmd_request
);
758 scmnd
->scsi_done
= NULL
;
759 scmnd
->host_scribble
= NULL
;
761 ret
= SCSI_MLQUEUE_DEVICE_BUSY
;
767 static DEF_SCSI_QCMD(storvsc_queuecommand
)
771 static struct scsi_host_template scsi_driver
= {
772 .module
= THIS_MODULE
,
773 .name
= "storvsc_host_t",
774 .bios_param
= storvsc_get_chs
,
775 .queuecommand
= storvsc_queuecommand
,
776 .eh_host_reset_handler
= storvsc_host_reset_handler
,
777 .slave_alloc
= storvsc_device_alloc
,
778 .slave_configure
= storvsc_device_configure
,
780 /* 64 max_queue * 1 target */
781 .can_queue
= STORVSC_MAX_IO_REQUESTS
*STORVSC_MAX_TARGETS
,
783 /* no use setting to 0 since ll_blk_rw reset it to 1 */
785 .sg_tablesize
= MAX_MULTIPAGE_BUFFER_COUNT
,
787 * ENABLE_CLUSTERING allows mutiple physically contig bio_vecs to merge
788 * into 1 sg element. If set, we must limit the max_segment_size to
789 * PAGE_SIZE, otherwise we may get 1 sg element that represents
792 /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */
793 .use_clustering
= ENABLE_CLUSTERING
,
794 /* Make sure we dont get a sg segment crosses a page boundary */
795 .dma_boundary
= PAGE_SIZE
-1,
800 * storvsc_probe - Add a new device for this driver
803 static int storvsc_probe(struct hv_device
*device
)
806 struct storvsc_driver
*storvsc_drv_obj
=
807 drv_to_stordrv(device
->device
.driver
);
808 struct Scsi_Host
*host
;
809 struct hv_host_device
*host_dev
;
810 struct storvsc_device_info device_info
;
812 if (!storvsc_drv_obj
->base
.dev_add
)
815 host
= scsi_host_alloc(&scsi_driver
,
816 sizeof(struct hv_host_device
));
820 dev_set_drvdata(&device
->device
, host
);
822 host_dev
= (struct hv_host_device
*)host
->hostdata
;
823 memset(host_dev
, 0, sizeof(struct hv_host_device
));
825 host_dev
->port
= host
->host_no
;
826 host_dev
->dev
= device
;
828 host_dev
->request_pool
=
829 kmem_cache_create(dev_name(&device
->device
),
830 sizeof(struct storvsc_cmd_request
), 0,
831 SLAB_HWCACHE_ALIGN
, NULL
);
833 if (!host_dev
->request_pool
) {
838 device_info
.port_number
= host
->host_no
;
839 /* Call to the vsc driver to add the device */
840 ret
= storvsc_drv_obj
->base
.dev_add(device
, (void *)&device_info
);
843 kmem_cache_destroy(host_dev
->request_pool
);
848 host_dev
->path
= device_info
.path_id
;
849 host_dev
->target
= device_info
.target_id
;
851 /* max # of devices per target */
852 host
->max_lun
= STORVSC_MAX_LUNS_PER_TARGET
;
853 /* max # of targets per channel */
854 host
->max_id
= STORVSC_MAX_TARGETS
;
855 /* max # of channels */
856 host
->max_channel
= STORVSC_MAX_CHANNELS
- 1;
858 /* Register the HBA and start the scsi bus scan */
859 ret
= scsi_add_host(host
, &device
->device
);
862 storvsc_drv_obj
->base
.dev_rm(device
);
864 kmem_cache_destroy(host_dev
->request_pool
);
869 scsi_scan_host(host
);
873 /* The one and only one */
875 static struct storvsc_driver storvsc_drv
;
879 * storvsc_drv_init - StorVsc driver initialization.
881 static int storvsc_drv_init(void)
884 struct storvsc_driver
*storvsc_drv_obj
= &storvsc_drv
;
885 struct hv_driver
*drv
= &storvsc_drv
.base
;
887 storvsc_drv_obj
->ring_buffer_size
= storvsc_ringbuffer_size
;
889 /* Callback to client driver to complete the initialization */
890 storvsc_initialize(&storvsc_drv_obj
->base
);
892 DPRINT_INFO(STORVSC_DRV
,
893 "max outstanding reqs %u",
894 storvsc_drv_obj
->max_outstanding_req_per_channel
);
896 if (storvsc_drv_obj
->max_outstanding_req_per_channel
<
897 STORVSC_MAX_IO_REQUESTS
)
900 drv
->driver
.name
= storvsc_drv_obj
->base
.name
;
902 drv
->probe
= storvsc_probe
;
903 drv
->remove
= storvsc_remove
;
905 /* The driver belongs to vmbus */
906 ret
= vmbus_child_driver_register(&drv
->driver
);
911 static int storvsc_drv_exit_cb(struct device
*dev
, void *data
)
913 struct device
**curr
= (struct device
**)data
;
915 return 1; /* stop iterating */
918 static void storvsc_drv_exit(void)
920 struct storvsc_driver
*storvsc_drv_obj
= &storvsc_drv
;
921 struct hv_driver
*drv
= &storvsc_drv
.base
;
922 struct device
*current_dev
= NULL
;
929 ret
= driver_for_each_device(&drv
->driver
, NULL
,
930 (void *) ¤t_dev
,
931 storvsc_drv_exit_cb
);
934 if (current_dev
== NULL
)
937 /* Initiate removal from the top-down */
938 device_unregister(current_dev
);
941 if (storvsc_drv_obj
->base
.cleanup
)
942 storvsc_drv_obj
->base
.cleanup(&storvsc_drv_obj
->base
);
944 vmbus_child_driver_unregister(&drv
->driver
);
948 static int __init
storvsc_init(void)
952 DPRINT_INFO(STORVSC_DRV
, "Storvsc initializing....");
953 ret
= storvsc_drv_init();
957 static void __exit
storvsc_exit(void)
962 MODULE_LICENSE("GPL");
963 MODULE_VERSION(HV_DRV_VERSION
);
964 MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
965 module_init(storvsc_init
);
966 module_exit(storvsc_exit
);