23 #include <linux/kernel.h>
24 #include <linux/wait.h>
25 #include <linux/sched.h>
27 #include <linux/string.h>
31 #include <linux/slab.h>
32 #include <linux/module.h>
33 #include <linux/device.h>
36 #include <scsi/scsi.h>
60 #define VMSTOR_CURRENT_MAJOR 4
61 #define VMSTOR_CURRENT_MINOR 2
85 #define STORVSC_MAX_CMD_LEN 0x10
86 #define STORVSC_SENSE_BUFFER_SIZE 0x12
87 #define STORVSC_MAX_BUF_LEN_WITH_PADDING 0x14
152 #define STORAGE_CHANNEL_REMOVABLE_FLAG 0x1
153 #define STORAGE_CHANNEL_EMULATED_IDE_FLAG 0x2
188 #define REQUEST_COMPLETION_FLAG 0x1
201 #define SRB_STATUS_AUTOSENSE_VALID 0x80
202 #define SRB_STATUS_INVALID_LUN 0x20
203 #define SRB_STATUS_SUCCESS 0x01
204 #define SRB_STATUS_ERROR 0x04
217 #define STORVSC_MIN_BUF_NR 64
218 static int storvsc_ringbuffer_size = (20 *
PAGE_SIZE);
223 #define STORVSC_MAX_IO_REQUESTS 128
231 #define STORVSC_MAX_LUNS_PER_TARGET 64
232 #define STORVSC_MAX_TARGETS 1
233 #define STORVSC_MAX_CHANNELS 1
304 for (
id = 0;
id < wrk->
host->max_id; ++
id) {
305 if (wrk->
host->reverse_ordering)
306 order_id = wrk->
host->max_id -
id - 1;
342 static inline u16 storvsc_get_version(
u8 major,
u8 minor)
346 version = ((major << 8) | minor);
370 stor_device = hv_get_drvdata(device);
372 if (stor_device && stor_device->
destroy)
388 struct hv_device *device)
392 stor_device = hv_get_drvdata(device);
415 struct page *page_buf;
418 page_buf = sg_page((&sgl[i]));
419 if (page_buf !=
NULL)
426 static int do_bounce_buffer(
struct scatterlist *sgl,
unsigned int sg_count)
440 }
else if (i == sg_count - 1) {
454 unsigned int sg_count,
461 struct page *page_buf;
474 sg_set_page(&bounce_sgl[i], page_buf, buf_len, 0);
480 destroy_bounce_buffer(bounce_sgl, num_pages);
485 static inline unsigned long sg_kmap_atomic(
struct scatterlist *sgl,
int idx)
488 return (
unsigned long)
addr;
491 static inline void sg_kunmap_atomic(
unsigned long addr)
498 static unsigned int copy_from_bounce_buffer(
struct scatterlist *orig_sgl,
500 unsigned int orig_sgl_count,
501 unsigned int bounce_sgl_count)
506 unsigned int srclen, destlen, copylen;
507 unsigned int total_copied = 0;
508 unsigned long bounce_addr = 0;
514 for (i = 0; i < orig_sgl_count; i++) {
515 dest_addr = sg_kmap_atomic(orig_sgl,i) + orig_sgl[
i].
offset;
519 if (bounce_addr == 0)
520 bounce_addr = sg_kmap_atomic(bounce_sgl,j);
523 src = bounce_addr + bounce_sgl[
j].
offset;
526 copylen =
min(srclen, destlen);
527 memcpy((
void *)dest, (
void *)src, copylen);
529 total_copied += copylen;
530 bounce_sgl[
j].
offset += copylen;
536 sg_kunmap_atomic(bounce_addr);
546 if (j == bounce_sgl_count) {
550 sg_kunmap_atomic(dest_addr - orig_sgl[i].
offset);
556 if (destlen || i != orig_sgl_count - 1)
557 bounce_addr = sg_kmap_atomic(bounce_sgl,j);
558 }
else if (destlen == 0 && i == orig_sgl_count - 1) {
560 sg_kunmap_atomic(bounce_addr);
564 sg_kunmap_atomic(dest_addr - orig_sgl[i].
offset);
573 static unsigned int copy_to_bounce_buffer(
struct scatterlist *orig_sgl,
575 unsigned int orig_sgl_count)
580 unsigned int srclen, destlen, copylen;
581 unsigned int total_copied = 0;
582 unsigned long bounce_addr = 0;
588 for (i = 0; i < orig_sgl_count; i++) {
589 src_addr = sg_kmap_atomic(orig_sgl,i) + orig_sgl[
i].
offset;
593 if (bounce_addr == 0)
594 bounce_addr = sg_kmap_atomic(bounce_sgl,j);
598 dest = bounce_addr + bounce_sgl[
j].
length;
601 copylen =
min(srclen, destlen);
602 memcpy((
void *)dest, (
void *)src, copylen);
604 total_copied += copylen;
605 bounce_sgl[
j].
length += copylen;
611 sg_kunmap_atomic(bounce_addr);
615 if (srclen || i != orig_sgl_count - 1)
616 bounce_addr = sg_kmap_atomic(bounce_sgl,j);
618 }
else if (srclen == 0 && i == orig_sgl_count - 1) {
620 sg_kunmap_atomic(bounce_addr);
624 sg_kunmap_atomic(src_addr - orig_sgl[i].
offset);
632 static int storvsc_channel_init(
struct hv_device *device)
639 stor_device = get_out_stor_device(device);
656 sizeof(
struct vstor_packet),
657 (
unsigned long)request,
659 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
670 vstor_packet->
status != 0)
675 memset(vstor_packet, 0,
sizeof(
struct vstor_packet));
679 vstor_packet->
version.major_minor =
685 vstor_packet->
version.revision = 0;
688 sizeof(
struct vstor_packet),
689 (
unsigned long)request,
691 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
702 vstor_packet->
status != 0)
706 memset(vstor_packet, 0,
sizeof(
struct vstor_packet));
713 sizeof(
struct vstor_packet),
714 (
unsigned long)request,
716 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
728 vstor_packet->
status != 0)
735 memset(vstor_packet, 0,
sizeof(
struct vstor_packet));
740 sizeof(
struct vstor_packet),
741 (
unsigned long)request,
743 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
755 vstor_packet->
status != 0)
777 copy_from_bounce_buffer(scsi_sglist(scmnd),
779 scsi_sg_count(scmnd),
781 destroy_bounce_buffer(cmd_request->
bounce_sgl,
794 switch (scmnd->
cmnd[0]) {
810 struct hv_device *dev = host_dev->
dev;
813 stor_dev = get_in_stor_device(dev);
814 host = stor_dev->
host;
834 scsi_set_resid(scmnd,
848 static void storvsc_on_io_completion(
struct hv_device *device,
849 struct vstor_packet *vstor_packet,
853 struct vstor_packet *stor_pkt;
855 stor_device = hv_get_drvdata(device);
871 vstor_packet->
vm_srb.scsi_status = 0;
877 stor_pkt->
vm_srb.scsi_status = vstor_packet->
vm_srb.scsi_status;
878 stor_pkt->
vm_srb.srb_status = vstor_packet->
vm_srb.srb_status;
879 stor_pkt->
vm_srb.sense_info_length =
880 vstor_packet->
vm_srb.sense_info_length;
882 if (vstor_packet->
vm_srb.scsi_status != 0 ||
885 "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
887 vstor_packet->
vm_srb.scsi_status,
888 vstor_packet->
vm_srb.srb_status);
891 if ((vstor_packet->
vm_srb.scsi_status & 0xFF) == 0x02) {
893 if (vstor_packet->
vm_srb.srb_status &
897 "stor pkt %p autosense data valid - len %d\n",
899 vstor_packet->
vm_srb.sense_info_length);
902 vstor_packet->
vm_srb.sense_data,
903 vstor_packet->
vm_srb.sense_info_length);
908 stor_pkt->
vm_srb.data_transfer_length =
909 vstor_packet->
vm_srb.data_transfer_length;
911 storvsc_command_completion(request);
920 static void storvsc_on_receive(
struct hv_device *device,
921 struct vstor_packet *vstor_packet,
929 storvsc_on_io_completion(device, vstor_packet, request);
934 stor_device = get_in_stor_device(device);
949 static void storvsc_on_channel_callback(
void *
context)
951 struct hv_device *device = (
struct hv_device *)context;
955 unsigned char packet[
ALIGN(
sizeof(
struct vstor_packet), 8)];
960 stor_device = get_in_stor_device(device);
966 ALIGN(
sizeof(
struct vstor_packet), 8),
967 &bytes_recvd, &request_id);
968 if (ret == 0 && bytes_recvd > 0) {
977 sizeof(
struct vstor_packet));
980 storvsc_on_receive(device,
981 (
struct vstor_packet *)packet,
992 static int storvsc_connect_to_vsp(
struct hv_device *device,
u32 ring_size)
1004 storvsc_on_channel_callback, device);
1009 ret = storvsc_channel_init(device);
1014 static int storvsc_dev_remove(
struct hv_device *device)
1017 unsigned long flags;
1019 stor_device = hv_get_drvdata(device);
1023 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
1031 storvsc_wait_to_drain(stor_device);
1041 hv_set_drvdata(device,
NULL);
1042 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
1051 static int storvsc_do_io(
struct hv_device *device,
1055 struct vstor_packet *vstor_packet;
1059 stor_device = get_out_stor_device(device);
1065 request->
device = device;
1076 vstor_packet->
vm_srb.data_transfer_length =
1085 sizeof(
struct vstor_packet),
1086 (
unsigned long)request);
1089 sizeof(
struct vstor_packet),
1090 (
unsigned long)request,
1092 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1103 static int storvsc_device_alloc(
struct scsi_device *sdevice)
1139 static void storvsc_device_destroy(
struct scsi_device *sdevice)
1149 static int storvsc_device_configure(
struct scsi_device *sdevice)
1166 int heads, sectors_pt;
1174 if ((
sector_t)(cylinders + 1) * heads * sectors_pt < nsect)
1178 info[1] = sectors_pt;
1179 info[2] = (
int)cylinders;
1184 static int storvsc_host_reset_handler(
struct scsi_cmnd *scmnd)
1187 struct hv_device *device = host_dev->
dev;
1191 struct vstor_packet *vstor_packet;
1195 stor_device = get_out_stor_device(device);
1209 sizeof(
struct vstor_packet),
1212 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1229 storvsc_wait_to_drain(stor_device);
1234 static bool storvsc_scsi_cmd_ok(
struct scsi_cmnd *scmnd)
1236 bool allowed =
true;
1237 u8 scsi_op = scmnd->
cmnd[0];
1258 struct hv_device *dev = host_dev->
dev;
1260 unsigned int request_size = 0;
1263 unsigned int sg_count = 0;
1267 if (!storvsc_scsi_cmd_ok(scmnd)) {
1287 cmd_request->
cmd = scmnd;
1320 cmd_request->
data_buffer.len = scsi_bufflen(scmnd);
1321 if (scsi_sg_count(scmnd)) {
1323 sg_count = scsi_sg_count(scmnd);
1326 if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) {
1328 create_bounce_buffer(sgl, scsi_sg_count(scmnd),
1329 scsi_bufflen(scmnd),
1341 copy_to_bounce_buffer(sgl,
1343 scsi_sg_count(scmnd));
1355 }
else if (scsi_sglist(scmnd)) {
1363 ret = storvsc_do_io(dev, cmd_request);
1369 destroy_bounce_buffer(cmd_request->
bounce_sgl,
1387 .name =
"storvsc_host_t",
1388 .bios_param = storvsc_get_chs,
1389 .queuecommand = storvsc_queuecommand,
1390 .eh_host_reset_handler = storvsc_host_reset_handler,
1391 .slave_alloc = storvsc_device_alloc,
1392 .slave_destroy = storvsc_device_destroy,
1393 .slave_configure = storvsc_device_configure,
1400 .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT,
1413 { VMBUS_DEVICE(0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
1414 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f)
1417 { VMBUS_DEVICE(0x32, 0x26, 0x41, 0x32, 0xcb, 0
x86, 0xa2, 0x44,
1418 0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5)
1425 static int storvsc_probe(
struct hv_device *device,
1431 bool dev_is_ide = ((dev_id->driver_data ==
IDE_GUID) ?
true :
false);
1440 host_dev = shost_priv(host);
1457 hv_set_drvdata(device, stor_device);
1460 ret = storvsc_connect_to_vsp(device, storvsc_ringbuffer_size);
1477 ret = scsi_add_host(host, &device->device);
1484 target = (device->dev_instance.b[5] << 8 |
1485 device->dev_instance.b[4]);
1501 storvsc_dev_remove(device);
1512 static int storvsc_remove(
struct hv_device *dev)
1518 storvsc_dev_remove(dev);
1524 static struct hv_driver storvsc_drv = {
1525 .name = KBUILD_MODNAME,
1526 .id_table = id_table,
1527 .probe = storvsc_probe,
1528 .remove = storvsc_remove,
1531 static int __init storvsc_drv_init(
void)
1533 u32 max_outstanding_req_per_channel;
1541 max_outstanding_req_per_channel =
1542 ((storvsc_ringbuffer_size -
PAGE_SIZE) /
1543 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
1544 sizeof(
struct vstor_packet) +
sizeof(
u64),
1547 if (max_outstanding_req_per_channel <
1551 return vmbus_driver_register(&storvsc_drv);
1554 static void __exit storvsc_drv_exit(
void)