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>
23 #include <linux/kernel.h>
24 #include <linux/wait.h>
25 #include <linux/sched.h>
26 #include <linux/completion.h>
27 #include <linux/string.h>
29 #include <linux/delay.h>
30 #include <linux/init.h>
31 #include <linux/slab.h>
32 #include <linux/module.h>
33 #include <linux/device.h>
34 #include <linux/hyperv.h>
35 #include <linux/mempool.h>
36 #include <scsi/scsi.h>
37 #include <scsi/scsi_cmnd.h>
38 #include <scsi/scsi_host.h>
39 #include <scsi/scsi_device.h>
40 #include <scsi/scsi_tcq.h>
41 #include <scsi/scsi_eh.h>
42 #include <scsi/scsi_devinfo.h>
43 #include <scsi/scsi_dbg.h>
46 #define STORVSC_MIN_BUF_NR 64
47 #define STORVSC_RING_BUFFER_SIZE (20*PAGE_SIZE)
48 static int storvsc_ringbuffer_size = STORVSC_RING_BUFFER_SIZE;
50 module_param(storvsc_ringbuffer_size, int, S_IRUGO);
51 MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)");
53 /* to alert the user that structure sizes may be mismatched even though the */
54 /* protocol versions match. */
57 #define REVISION_STRING(REVISION_) #REVISION_
58 #define FILL_VMSTOR_REVISION(RESULT_LVALUE_) \
60 char *revision_string \
61 = REVISION_STRING($Rev : 6 $) + 6; \
63 while (*revision_string >= '0' \
64 && *revision_string <= '9') { \
65 RESULT_LVALUE_ *= 10; \
66 RESULT_LVALUE_ += *revision_string - '0'; \
71 /* Major/minor macros. Minor version is in LSB, meaning that earlier flat */
72 /* version numbers will be interpreted as "0.x" (i.e., 1 becomes 0.1). */
73 #define VMSTOR_PROTOCOL_MAJOR(VERSION_) (((VERSION_) >> 8) & 0xff)
74 #define VMSTOR_PROTOCOL_MINOR(VERSION_) (((VERSION_)) & 0xff)
75 #define VMSTOR_PROTOCOL_VERSION(MAJOR_, MINOR_) ((((MAJOR_) & 0xff) << 8) | \
77 #define VMSTOR_INVALID_PROTOCOL_VERSION (-1)
79 /* Version history: */
81 /* V1 RC < 2008/1/31 1.0 */
82 /* V1 RC > 2008/1/31 2.0 */
83 #define VMSTOR_PROTOCOL_VERSION_CURRENT VMSTOR_PROTOCOL_VERSION(4, 2)
88 /* This will get replaced with the max transfer length that is possible on */
89 /* the host adapter. */
90 /* The max transfer length will be published when we offer a vmbus channel. */
91 #define MAX_TRANSFER_LENGTH 0x40000
92 #define DEFAULT_PACKET_SIZE (sizeof(struct vmdata_gpa_direct) + \
93 sizeof(struct vstor_packet) + \
94 sizesizeof(u64) * (MAX_TRANSFER_LENGTH / PAGE_SIZE)))
97 /* Packet structure describing virtual storage requests. */
98 enum vstor_packet_operation {
99 VSTOR_OPERATION_COMPLETE_IO = 1,
100 VSTOR_OPERATION_REMOVE_DEVICE = 2,
101 VSTOR_OPERATION_EXECUTE_SRB = 3,
102 VSTOR_OPERATION_RESET_LUN = 4,
103 VSTOR_OPERATION_RESET_ADAPTER = 5,
104 VSTOR_OPERATION_RESET_BUS = 6,
105 VSTOR_OPERATION_BEGIN_INITIALIZATION = 7,
106 VSTOR_OPERATION_END_INITIALIZATION = 8,
107 VSTOR_OPERATION_QUERY_PROTOCOL_VERSION = 9,
108 VSTOR_OPERATION_QUERY_PROPERTIES = 10,
109 VSTOR_OPERATION_ENUMERATE_BUS = 11,
110 VSTOR_OPERATION_MAXIMUM = 11
114 * Platform neutral description of a scsi request -
115 * this remains the same across the write regardless of 32/64 bit
116 * note: it's patterned off the SCSI_PASS_THROUGH structure
118 #define CDB16GENERIC_LENGTH 0x10
120 #ifndef SENSE_BUFFER_SIZE
121 #define SENSE_BUFFER_SIZE 0x12
124 #define MAX_DATA_BUF_LEN_WITH_PADDING 0x14
126 struct vmscsi_request {
127 unsigned short length;
128 unsigned char srb_status;
129 unsigned char scsi_status;
131 unsigned char port_number;
132 unsigned char path_id;
133 unsigned char target_id;
136 unsigned char cdb_length;
137 unsigned char sense_info_length;
138 unsigned char data_in;
139 unsigned char reserved;
141 unsigned int data_transfer_length;
144 unsigned char cdb[CDB16GENERIC_LENGTH];
145 unsigned char sense_data[SENSE_BUFFER_SIZE];
146 unsigned char reserved_array[MAX_DATA_BUF_LEN_WITH_PADDING];
148 } __attribute((packed));
152 * This structure is sent during the intialization phase to get the different
153 * properties of the channel.
155 struct vmstorage_channel_properties {
156 unsigned short protocol_version;
157 unsigned char path_id;
158 unsigned char target_id;
160 /* Note: port number is only really known on the client side */
161 unsigned int port_number;
163 unsigned int max_transfer_bytes;
165 /* This id is unique for each channel and will correspond with */
166 /* vendor specific data in the inquirydata */
167 unsigned long long unique_id;
170 /* This structure is sent during the storage protocol negotiations. */
171 struct vmstorage_protocol_version {
172 /* Major (MSW) and minor (LSW) version numbers. */
173 unsigned short major_minor;
176 * Revision number is auto-incremented whenever this file is changed
177 * (See FILL_VMSTOR_REVISION macro above). Mismatch does not
178 * definitely indicate incompatibility--but it does indicate mismatched
181 unsigned short revision;
184 /* Channel Property Flags */
185 #define STORAGE_CHANNEL_REMOVABLE_FLAG 0x1
186 #define STORAGE_CHANNEL_EMULATED_IDE_FLAG 0x2
188 struct vstor_packet {
189 /* Requested operation type */
190 enum vstor_packet_operation operation;
192 /* Flags - see below for values */
195 /* Status of the request returned from the server side. */
198 /* Data payload area */
201 * Structure used to forward SCSI commands from the
202 * client to the server.
204 struct vmscsi_request vm_srb;
206 /* Structure used to query channel properties. */
207 struct vmstorage_channel_properties storage_channel_properties;
209 /* Used during version negotiations. */
210 struct vmstorage_protocol_version version;
216 * This flag indicates that the server should send back a completion for this
219 #define REQUEST_COMPLETION_FLAG 0x1
221 /* This is the set of flags that the vsc can set in any packets it sends */
222 #define VSC_LEGAL_FLAGS (REQUEST_COMPLETION_FLAG)
227 #define STORVSC_MAX_IO_REQUESTS 128
230 * In Hyper-V, each port/path/target maps to 1 scsi host adapter. In
231 * reality, the path/target is not used (ie always set to 0) so our
232 * scsi host adapter essentially has 1 bus with 1 target that contains
235 #define STORVSC_MAX_LUNS_PER_TARGET 64
236 #define STORVSC_MAX_TARGETS 1
237 #define STORVSC_MAX_CHANNELS 1
238 #define STORVSC_MAX_CMD_LEN 16
240 /* Matches Windows-end */
241 enum storvsc_request_type {
248 struct hv_storvsc_request {
249 struct hv_device *device;
251 /* Synchronize the request/response if needed */
252 struct completion wait_event;
254 unsigned char *sense_buffer;
256 void (*on_io_completion)(struct hv_storvsc_request *request);
257 struct hv_multipage_buffer data_buffer;
259 struct vstor_packet vstor_packet;
263 /* A storvsc device is a device object that contains a vmbus channel */
264 struct storvsc_device {
265 struct hv_device *device;
269 atomic_t num_outstanding_req;
270 struct Scsi_Host *host;
272 wait_queue_head_t waiting_to_drain;
275 * Each unique Port/Path/Target represents 1 channel ie scsi
276 * controller. In reality, the pathid, targetid is always 0
277 * and the port is set by us
279 unsigned int port_number;
280 unsigned char path_id;
281 unsigned char target_id;
283 /* Used for vsc/vsp channel reset process */
284 struct hv_storvsc_request init_request;
285 struct hv_storvsc_request reset_request;
288 struct hv_host_device {
289 struct hv_device *dev;
290 struct kmem_cache *request_pool;
291 mempool_t *request_mempool;
294 unsigned char target;
297 struct storvsc_cmd_request {
298 struct list_head entry;
299 struct scsi_cmnd *cmd;
301 unsigned int bounce_sgl_count;
302 struct scatterlist *bounce_sgl;
304 struct hv_storvsc_request request;
307 struct storvsc_scan_work {
308 struct work_struct work;
309 struct Scsi_Host *host;
313 static void storvsc_bus_scan(struct work_struct *work)
315 struct storvsc_scan_work *wrk;
318 wrk = container_of(work, struct storvsc_scan_work, work);
319 for (id = 0; id < wrk->host->max_id; ++id) {
320 if (wrk->host->reverse_ordering)
321 order_id = wrk->host->max_id - id - 1;
325 scsi_scan_target(&wrk->host->shost_gendev, 0,
326 order_id, SCAN_WILD_CARD, 1);
331 static void storvsc_remove_lun(struct work_struct *work)
333 struct storvsc_scan_work *wrk;
334 struct scsi_device *sdev;
336 wrk = container_of(work, struct storvsc_scan_work, work);
337 if (!scsi_host_get(wrk->host))
340 sdev = scsi_device_lookup(wrk->host, 0, 0, wrk->lun);
343 scsi_remove_device(sdev);
344 scsi_device_put(sdev);
346 scsi_host_put(wrk->host);
352 static inline struct storvsc_device *get_out_stor_device(
353 struct hv_device *device)
355 struct storvsc_device *stor_device;
357 stor_device = hv_get_drvdata(device);
359 if (stor_device && stor_device->destroy)
366 static inline void storvsc_wait_to_drain(struct storvsc_device *dev)
368 dev->drain_notify = true;
369 wait_event(dev->waiting_to_drain,
370 atomic_read(&dev->num_outstanding_req) == 0);
371 dev->drain_notify = false;
374 static inline struct storvsc_device *get_in_stor_device(
375 struct hv_device *device)
377 struct storvsc_device *stor_device;
379 stor_device = hv_get_drvdata(device);
385 * If the device is being destroyed; allow incoming
386 * traffic only to cleanup outstanding requests.
389 if (stor_device->destroy &&
390 (atomic_read(&stor_device->num_outstanding_req) == 0))
398 static int storvsc_channel_init(struct hv_device *device)
400 struct storvsc_device *stor_device;
401 struct hv_storvsc_request *request;
402 struct vstor_packet *vstor_packet;
405 stor_device = get_out_stor_device(device);
409 request = &stor_device->init_request;
410 vstor_packet = &request->vstor_packet;
413 * Now, initiate the vsc/vsp initialization protocol on the open
416 memset(request, 0, sizeof(struct hv_storvsc_request));
417 init_completion(&request->wait_event);
418 vstor_packet->operation = VSTOR_OPERATION_BEGIN_INITIALIZATION;
419 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
421 ret = vmbus_sendpacket(device->channel, vstor_packet,
422 sizeof(struct vstor_packet),
423 (unsigned long)request,
425 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
429 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
435 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
436 vstor_packet->status != 0)
440 /* reuse the packet for version range supported */
441 memset(vstor_packet, 0, sizeof(struct vstor_packet));
442 vstor_packet->operation = VSTOR_OPERATION_QUERY_PROTOCOL_VERSION;
443 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
445 vstor_packet->version.major_minor = VMSTOR_PROTOCOL_VERSION_CURRENT;
446 FILL_VMSTOR_REVISION(vstor_packet->version.revision);
448 ret = vmbus_sendpacket(device->channel, vstor_packet,
449 sizeof(struct vstor_packet),
450 (unsigned long)request,
452 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
456 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
462 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
463 vstor_packet->status != 0)
467 memset(vstor_packet, 0, sizeof(struct vstor_packet));
468 vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES;
469 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
470 vstor_packet->storage_channel_properties.port_number =
471 stor_device->port_number;
473 ret = vmbus_sendpacket(device->channel, vstor_packet,
474 sizeof(struct vstor_packet),
475 (unsigned long)request,
477 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
482 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
488 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
489 vstor_packet->status != 0)
492 stor_device->path_id = vstor_packet->storage_channel_properties.path_id;
493 stor_device->target_id
494 = vstor_packet->storage_channel_properties.target_id;
496 memset(vstor_packet, 0, sizeof(struct vstor_packet));
497 vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION;
498 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
500 ret = vmbus_sendpacket(device->channel, vstor_packet,
501 sizeof(struct vstor_packet),
502 (unsigned long)request,
504 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
509 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
515 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
516 vstor_packet->status != 0)
524 static void storvsc_on_io_completion(struct hv_device *device,
525 struct vstor_packet *vstor_packet,
526 struct hv_storvsc_request *request)
528 struct storvsc_device *stor_device;
529 struct vstor_packet *stor_pkt;
531 stor_device = hv_get_drvdata(device);
532 stor_pkt = &request->vstor_packet;
535 * The current SCSI handling on the host side does
536 * not correctly handle:
537 * INQUIRY command with page code parameter set to 0x80
538 * MODE_SENSE command with cmd[2] == 0x1c
540 * Setup srb and scsi status so this won't be fatal.
541 * We do this so we can distinguish truly fatal failues
542 * (srb status == 0x4) and off-line the device in that case.
545 if ((stor_pkt->vm_srb.cdb[0] == INQUIRY) ||
546 (stor_pkt->vm_srb.cdb[0] == MODE_SENSE)) {
547 vstor_packet->vm_srb.scsi_status = 0;
548 vstor_packet->vm_srb.srb_status = 0x1;
552 /* Copy over the status...etc */
553 stor_pkt->vm_srb.scsi_status = vstor_packet->vm_srb.scsi_status;
554 stor_pkt->vm_srb.srb_status = vstor_packet->vm_srb.srb_status;
555 stor_pkt->vm_srb.sense_info_length =
556 vstor_packet->vm_srb.sense_info_length;
558 if (vstor_packet->vm_srb.scsi_status != 0 ||
559 vstor_packet->vm_srb.srb_status != 1){
560 dev_warn(&device->device,
561 "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
562 stor_pkt->vm_srb.cdb[0],
563 vstor_packet->vm_srb.scsi_status,
564 vstor_packet->vm_srb.srb_status);
567 if ((vstor_packet->vm_srb.scsi_status & 0xFF) == 0x02) {
568 /* CHECK_CONDITION */
569 if (vstor_packet->vm_srb.srb_status & 0x80) {
570 /* autosense data available */
571 dev_warn(&device->device,
572 "stor pkt %p autosense data valid - len %d\n",
574 vstor_packet->vm_srb.sense_info_length);
576 memcpy(request->sense_buffer,
577 vstor_packet->vm_srb.sense_data,
578 vstor_packet->vm_srb.sense_info_length);
583 stor_pkt->vm_srb.data_transfer_length =
584 vstor_packet->vm_srb.data_transfer_length;
586 request->on_io_completion(request);
588 if (atomic_dec_and_test(&stor_device->num_outstanding_req) &&
589 stor_device->drain_notify)
590 wake_up(&stor_device->waiting_to_drain);
595 static void storvsc_on_receive(struct hv_device *device,
596 struct vstor_packet *vstor_packet,
597 struct hv_storvsc_request *request)
599 struct storvsc_scan_work *work;
600 struct storvsc_device *stor_device;
602 switch (vstor_packet->operation) {
603 case VSTOR_OPERATION_COMPLETE_IO:
604 storvsc_on_io_completion(device, vstor_packet, request);
607 case VSTOR_OPERATION_REMOVE_DEVICE:
608 case VSTOR_OPERATION_ENUMERATE_BUS:
609 stor_device = get_in_stor_device(device);
610 work = kmalloc(sizeof(struct storvsc_scan_work), GFP_ATOMIC);
614 INIT_WORK(&work->work, storvsc_bus_scan);
615 work->host = stor_device->host;
616 schedule_work(&work->work);
624 static void storvsc_on_channel_callback(void *context)
626 struct hv_device *device = (struct hv_device *)context;
627 struct storvsc_device *stor_device;
630 unsigned char packet[ALIGN(sizeof(struct vstor_packet), 8)];
631 struct hv_storvsc_request *request;
635 stor_device = get_in_stor_device(device);
640 ret = vmbus_recvpacket(device->channel, packet,
641 ALIGN(sizeof(struct vstor_packet), 8),
642 &bytes_recvd, &request_id);
643 if (ret == 0 && bytes_recvd > 0) {
645 request = (struct hv_storvsc_request *)
646 (unsigned long)request_id;
648 if ((request == &stor_device->init_request) ||
649 (request == &stor_device->reset_request)) {
651 memcpy(&request->vstor_packet, packet,
652 sizeof(struct vstor_packet));
653 complete(&request->wait_event);
655 storvsc_on_receive(device,
656 (struct vstor_packet *)packet,
667 static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size)
669 struct vmstorage_channel_properties props;
672 memset(&props, 0, sizeof(struct vmstorage_channel_properties));
674 /* Open the channel */
675 ret = vmbus_open(device->channel,
679 sizeof(struct vmstorage_channel_properties),
680 storvsc_on_channel_callback, device);
685 ret = storvsc_channel_init(device);
690 static int storvsc_dev_remove(struct hv_device *device)
692 struct storvsc_device *stor_device;
695 stor_device = hv_get_drvdata(device);
697 spin_lock_irqsave(&device->channel->inbound_lock, flags);
698 stor_device->destroy = true;
699 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
702 * At this point, all outbound traffic should be disable. We
703 * only allow inbound traffic (responses) to proceed so that
704 * outstanding requests can be completed.
707 storvsc_wait_to_drain(stor_device);
710 * Since we have already drained, we don't need to busy wait
711 * as was done in final_release_stor_device()
712 * Note that we cannot set the ext pointer to NULL until
713 * we have drained - to drain the outgoing packets, we need to
714 * allow incoming packets.
716 spin_lock_irqsave(&device->channel->inbound_lock, flags);
717 hv_set_drvdata(device, NULL);
718 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
720 /* Close the channel */
721 vmbus_close(device->channel);
727 static int storvsc_do_io(struct hv_device *device,
728 struct hv_storvsc_request *request)
730 struct storvsc_device *stor_device;
731 struct vstor_packet *vstor_packet;
734 vstor_packet = &request->vstor_packet;
735 stor_device = get_out_stor_device(device);
741 request->device = device;
744 vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
746 vstor_packet->vm_srb.length = sizeof(struct vmscsi_request);
749 vstor_packet->vm_srb.sense_info_length = SENSE_BUFFER_SIZE;
752 vstor_packet->vm_srb.data_transfer_length =
753 request->data_buffer.len;
755 vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;
757 if (request->data_buffer.len) {
758 ret = vmbus_sendpacket_multipagebuffer(device->channel,
759 &request->data_buffer,
761 sizeof(struct vstor_packet),
762 (unsigned long)request);
764 ret = vmbus_sendpacket(device->channel, vstor_packet,
765 sizeof(struct vstor_packet),
766 (unsigned long)request,
768 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
774 atomic_inc(&stor_device->num_outstanding_req);
779 static void storvsc_get_ide_info(struct hv_device *dev, int *target, int *path)
782 dev->dev_instance.b[5] << 8 | dev->dev_instance.b[4];
785 dev->dev_instance.b[3] << 24 |
786 dev->dev_instance.b[2] << 16 |
787 dev->dev_instance.b[1] << 8 | dev->dev_instance.b[0];
791 static int storvsc_device_alloc(struct scsi_device *sdevice)
794 * This enables luns to be located sparsely. Otherwise, we may not
797 sdevice->sdev_bflags |= BLIST_SPARSELUN | BLIST_LARGELUN;
801 static int storvsc_merge_bvec(struct request_queue *q,
802 struct bvec_merge_data *bmd, struct bio_vec *bvec)
804 /* checking done by caller. */
808 static int storvsc_device_configure(struct scsi_device *sdevice)
810 scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG,
811 STORVSC_MAX_IO_REQUESTS);
813 blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
815 blk_queue_merge_bvec(sdevice->request_queue, storvsc_merge_bvec);
817 blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
822 static void destroy_bounce_buffer(struct scatterlist *sgl,
823 unsigned int sg_count)
826 struct page *page_buf;
828 for (i = 0; i < sg_count; i++) {
829 page_buf = sg_page((&sgl[i]));
830 if (page_buf != NULL)
831 __free_page(page_buf);
837 static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
841 /* No need to check */
845 /* We have at least 2 sg entries */
846 for (i = 0; i < sg_count; i++) {
848 /* make sure 1st one does not have hole */
849 if (sgl[i].offset + sgl[i].length != PAGE_SIZE)
851 } else if (i == sg_count - 1) {
852 /* make sure last one does not have hole */
853 if (sgl[i].offset != 0)
856 /* make sure no hole in the middle */
857 if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0)
864 static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
865 unsigned int sg_count,
870 struct scatterlist *bounce_sgl;
871 struct page *page_buf;
873 num_pages = ALIGN(len, PAGE_SIZE) >> PAGE_SHIFT;
875 bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC);
879 for (i = 0; i < num_pages; i++) {
880 page_buf = alloc_page(GFP_ATOMIC);
883 sg_set_page(&bounce_sgl[i], page_buf, 0, 0);
889 destroy_bounce_buffer(bounce_sgl, num_pages);
894 /* Assume the original sgl has enough room */
895 static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
896 struct scatterlist *bounce_sgl,
897 unsigned int orig_sgl_count)
901 unsigned long src, dest;
902 unsigned int srclen, destlen, copylen;
903 unsigned int total_copied = 0;
904 unsigned long bounce_addr = 0;
905 unsigned long dest_addr = 0;
908 local_irq_save(flags);
910 for (i = 0; i < orig_sgl_count; i++) {
911 dest_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
912 KM_IRQ0) + orig_sgl[i].offset;
914 destlen = orig_sgl[i].length;
916 if (bounce_addr == 0)
918 (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])),
922 src = bounce_addr + bounce_sgl[j].offset;
923 srclen = bounce_sgl[j].length - bounce_sgl[j].offset;
925 copylen = min(srclen, destlen);
926 memcpy((void *)dest, (void *)src, copylen);
928 total_copied += copylen;
929 bounce_sgl[j].offset += copylen;
933 if (bounce_sgl[j].offset == bounce_sgl[j].length) {
935 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
938 /* if we need to use another bounce buffer */
939 if (destlen || i != orig_sgl_count - 1)
941 (unsigned long)kmap_atomic(
942 sg_page((&bounce_sgl[j])), KM_IRQ0);
943 } else if (destlen == 0 && i == orig_sgl_count - 1) {
944 /* unmap the last bounce that is < PAGE_SIZE */
945 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
949 kunmap_atomic((void *)(dest_addr - orig_sgl[i].offset),
953 local_irq_restore(flags);
959 /* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
960 static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
961 struct scatterlist *bounce_sgl,
962 unsigned int orig_sgl_count)
966 unsigned long src, dest;
967 unsigned int srclen, destlen, copylen;
968 unsigned int total_copied = 0;
969 unsigned long bounce_addr = 0;
970 unsigned long src_addr = 0;
973 local_irq_save(flags);
975 for (i = 0; i < orig_sgl_count; i++) {
976 src_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
977 KM_IRQ0) + orig_sgl[i].offset;
979 srclen = orig_sgl[i].length;
981 if (bounce_addr == 0)
983 (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])),
987 /* assume bounce offset always == 0 */
988 dest = bounce_addr + bounce_sgl[j].length;
989 destlen = PAGE_SIZE - bounce_sgl[j].length;
991 copylen = min(srclen, destlen);
992 memcpy((void *)dest, (void *)src, copylen);
994 total_copied += copylen;
995 bounce_sgl[j].length += copylen;
999 if (bounce_sgl[j].length == PAGE_SIZE) {
1000 /* full..move to next entry */
1001 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
1004 /* if we need to use another bounce buffer */
1005 if (srclen || i != orig_sgl_count - 1)
1007 (unsigned long)kmap_atomic(
1008 sg_page((&bounce_sgl[j])), KM_IRQ0);
1010 } else if (srclen == 0 && i == orig_sgl_count - 1) {
1011 /* unmap the last bounce that is < PAGE_SIZE */
1012 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
1016 kunmap_atomic((void *)(src_addr - orig_sgl[i].offset), KM_IRQ0);
1019 local_irq_restore(flags);
1021 return total_copied;
1025 static int storvsc_remove(struct hv_device *dev)
1027 struct storvsc_device *stor_device = hv_get_drvdata(dev);
1028 struct Scsi_Host *host = stor_device->host;
1029 struct hv_host_device *host_dev = shost_priv(host);
1031 scsi_remove_host(host);
1033 scsi_host_put(host);
1035 storvsc_dev_remove(dev);
1036 if (host_dev->request_pool) {
1037 mempool_destroy(host_dev->request_mempool);
1038 kmem_cache_destroy(host_dev->request_pool);
1039 host_dev->request_pool = NULL;
1040 host_dev->request_mempool = NULL;
1046 static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
1047 sector_t capacity, int *info)
1049 sector_t nsect = capacity;
1050 sector_t cylinders = nsect;
1051 int heads, sectors_pt;
1054 * We are making up these values; let us keep it simple.
1057 sectors_pt = 0x3f; /* Sectors per track */
1058 sector_div(cylinders, heads * sectors_pt);
1059 if ((sector_t)(cylinders + 1) * heads * sectors_pt < nsect)
1063 info[1] = sectors_pt;
1064 info[2] = (int)cylinders;
1069 static int storvsc_host_reset(struct hv_device *device)
1071 struct storvsc_device *stor_device;
1072 struct hv_storvsc_request *request;
1073 struct vstor_packet *vstor_packet;
1077 stor_device = get_out_stor_device(device);
1081 request = &stor_device->reset_request;
1082 vstor_packet = &request->vstor_packet;
1084 init_completion(&request->wait_event);
1086 vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
1087 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
1088 vstor_packet->vm_srb.path_id = stor_device->path_id;
1090 ret = vmbus_sendpacket(device->channel, vstor_packet,
1091 sizeof(struct vstor_packet),
1092 (unsigned long)&stor_device->reset_request,
1094 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1098 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
1100 return TIMEOUT_ERROR;
1104 * At this point, all outstanding requests in the adapter
1105 * should have been flushed out and return to us
1113 * storvsc_host_reset_handler - Reset the scsi HBA
1115 static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
1117 struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
1118 struct hv_device *dev = host_dev->dev;
1120 return storvsc_host_reset(dev);
1125 * storvsc_command_completion - Command completion processing
1127 static void storvsc_command_completion(struct hv_storvsc_request *request)
1129 struct storvsc_cmd_request *cmd_request =
1130 (struct storvsc_cmd_request *)request->context;
1131 struct scsi_cmnd *scmnd = cmd_request->cmd;
1132 struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
1133 void (*scsi_done_fn)(struct scsi_cmnd *);
1134 struct scsi_sense_hdr sense_hdr;
1135 struct vmscsi_request *vm_srb;
1136 struct storvsc_scan_work *wrk;
1138 vm_srb = &request->vstor_packet.vm_srb;
1139 if (cmd_request->bounce_sgl_count) {
1140 if (vm_srb->data_in == READ_TYPE) {
1141 copy_from_bounce_buffer(scsi_sglist(scmnd),
1142 cmd_request->bounce_sgl,
1143 scsi_sg_count(scmnd));
1144 destroy_bounce_buffer(cmd_request->bounce_sgl,
1145 cmd_request->bounce_sgl_count);
1150 * If there is an error; offline the device since all
1151 * error recovery strategies would have already been
1152 * deployed on the host side.
1154 if (vm_srb->srb_status == 0x4)
1155 scmnd->result = DID_TARGET_FAILURE << 16;
1157 scmnd->result = vm_srb->scsi_status;
1160 * If the LUN is invalid; remove the device.
1162 if (vm_srb->srb_status == 0x20) {
1163 struct storvsc_device *stor_dev;
1164 struct hv_device *dev = host_dev->dev;
1165 struct Scsi_Host *host;
1167 stor_dev = get_in_stor_device(dev);
1168 host = stor_dev->host;
1170 wrk = kmalloc(sizeof(struct storvsc_scan_work),
1173 scmnd->result = DID_TARGET_FAILURE << 16;
1176 wrk->lun = vm_srb->lun;
1177 INIT_WORK(&wrk->work, storvsc_remove_lun);
1178 schedule_work(&wrk->work);
1182 if (scmnd->result) {
1183 if (scsi_normalize_sense(scmnd->sense_buffer,
1184 SCSI_SENSE_BUFFERSIZE, &sense_hdr))
1185 scsi_print_sense_hdr("storvsc", &sense_hdr);
1188 scsi_set_resid(scmnd,
1189 request->data_buffer.len -
1190 vm_srb->data_transfer_length);
1192 scsi_done_fn = scmnd->scsi_done;
1194 scmnd->host_scribble = NULL;
1195 scmnd->scsi_done = NULL;
1197 scsi_done_fn(scmnd);
1199 mempool_free(cmd_request, host_dev->request_mempool);
1202 static bool storvsc_check_scsi_cmd(struct scsi_cmnd *scmnd)
1204 bool allowed = true;
1205 u8 scsi_op = scmnd->cmnd[0];
1208 /* smartd sends this command, which will offline the device */
1210 scmnd->result = ILLEGAL_REQUEST << 16;
1220 * storvsc_queuecommand - Initiate command processing
1222 static int storvsc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scmnd)
1225 struct hv_host_device *host_dev = shost_priv(host);
1226 struct hv_device *dev = host_dev->dev;
1227 struct hv_storvsc_request *request;
1228 struct storvsc_cmd_request *cmd_request;
1229 unsigned int request_size = 0;
1231 struct scatterlist *sgl;
1232 unsigned int sg_count = 0;
1233 struct vmscsi_request *vm_srb;
1235 if (storvsc_check_scsi_cmd(scmnd) == false) {
1236 scmnd->scsi_done(scmnd);
1240 /* If retrying, no need to prep the cmd */
1241 if (scmnd->host_scribble) {
1244 (struct storvsc_cmd_request *)scmnd->host_scribble;
1249 request_size = sizeof(struct storvsc_cmd_request);
1251 cmd_request = mempool_alloc(host_dev->request_mempool,
1254 return SCSI_MLQUEUE_DEVICE_BUSY;
1256 memset(cmd_request, 0, sizeof(struct storvsc_cmd_request));
1258 /* Setup the cmd request */
1259 cmd_request->bounce_sgl_count = 0;
1260 cmd_request->bounce_sgl = NULL;
1261 cmd_request->cmd = scmnd;
1263 scmnd->host_scribble = (unsigned char *)cmd_request;
1265 request = &cmd_request->request;
1266 vm_srb = &request->vstor_packet.vm_srb;
1270 switch (scmnd->sc_data_direction) {
1272 vm_srb->data_in = WRITE_TYPE;
1274 case DMA_FROM_DEVICE:
1275 vm_srb->data_in = READ_TYPE;
1278 vm_srb->data_in = UNKNOWN_TYPE;
1282 request->on_io_completion = storvsc_command_completion;
1283 request->context = cmd_request;/* scmnd; */
1285 vm_srb->port_number = host_dev->port;
1286 vm_srb->path_id = scmnd->device->channel;
1287 vm_srb->target_id = scmnd->device->id;
1288 vm_srb->lun = scmnd->device->lun;
1290 vm_srb->cdb_length = scmnd->cmd_len;
1292 memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
1294 request->sense_buffer = scmnd->sense_buffer;
1297 request->data_buffer.len = scsi_bufflen(scmnd);
1298 if (scsi_sg_count(scmnd)) {
1299 sgl = (struct scatterlist *)scsi_sglist(scmnd);
1300 sg_count = scsi_sg_count(scmnd);
1302 /* check if we need to bounce the sgl */
1303 if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) {
1304 cmd_request->bounce_sgl =
1305 create_bounce_buffer(sgl, scsi_sg_count(scmnd),
1306 scsi_bufflen(scmnd));
1307 if (!cmd_request->bounce_sgl) {
1308 scmnd->host_scribble = NULL;
1309 mempool_free(cmd_request,
1310 host_dev->request_mempool);
1312 return SCSI_MLQUEUE_HOST_BUSY;
1315 cmd_request->bounce_sgl_count =
1316 ALIGN(scsi_bufflen(scmnd), PAGE_SIZE) >>
1319 if (vm_srb->data_in == WRITE_TYPE)
1320 copy_to_bounce_buffer(sgl,
1321 cmd_request->bounce_sgl,
1322 scsi_sg_count(scmnd));
1324 sgl = cmd_request->bounce_sgl;
1325 sg_count = cmd_request->bounce_sgl_count;
1328 request->data_buffer.offset = sgl[0].offset;
1330 for (i = 0; i < sg_count; i++)
1331 request->data_buffer.pfn_array[i] =
1332 page_to_pfn(sg_page((&sgl[i])));
1334 } else if (scsi_sglist(scmnd)) {
1335 request->data_buffer.offset =
1336 virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
1337 request->data_buffer.pfn_array[0] =
1338 virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
1342 /* Invokes the vsc to start an IO */
1343 ret = storvsc_do_io(dev, &cmd_request->request);
1345 if (ret == -EAGAIN) {
1348 if (cmd_request->bounce_sgl_count)
1349 destroy_bounce_buffer(cmd_request->bounce_sgl,
1350 cmd_request->bounce_sgl_count);
1352 mempool_free(cmd_request, host_dev->request_mempool);
1354 scmnd->host_scribble = NULL;
1356 ret = SCSI_MLQUEUE_DEVICE_BUSY;
1363 static struct scsi_host_template scsi_driver = {
1364 .module = THIS_MODULE,
1365 .name = "storvsc_host_t",
1366 .bios_param = storvsc_get_chs,
1367 .queuecommand = storvsc_queuecommand,
1368 .eh_host_reset_handler = storvsc_host_reset_handler,
1369 .slave_alloc = storvsc_device_alloc,
1370 .slave_configure = storvsc_device_configure,
1372 /* 64 max_queue * 1 target */
1373 .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS,
1375 /* no use setting to 0 since ll_blk_rw reset it to 1 */
1377 .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT,
1379 * ENABLE_CLUSTERING allows mutiple physically contig bio_vecs to merge
1380 * into 1 sg element. If set, we must limit the max_segment_size to
1381 * PAGE_SIZE, otherwise we may get 1 sg element that represents
1384 /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */
1385 .use_clustering = ENABLE_CLUSTERING,
1386 /* Make sure we dont get a sg segment crosses a page boundary */
1387 .dma_boundary = PAGE_SIZE-1,
1395 static const struct hv_vmbus_device_id id_table[] = {
1397 { VMBUS_DEVICE(0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
1398 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f)
1399 .driver_data = SCSI_GUID },
1401 { VMBUS_DEVICE(0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44,
1402 0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5)
1403 .driver_data = IDE_GUID },
1407 MODULE_DEVICE_TABLE(vmbus, id_table);
1411 * storvsc_probe - Add a new device for this driver
1414 static int storvsc_probe(struct hv_device *device,
1415 const struct hv_vmbus_device_id *dev_id)
1418 int number = STORVSC_MIN_BUF_NR;
1419 struct Scsi_Host *host;
1420 struct hv_host_device *host_dev;
1421 bool dev_is_ide = ((dev_id->driver_data == IDE_GUID) ? true : false);
1424 struct storvsc_device *stor_device;
1426 host = scsi_host_alloc(&scsi_driver,
1427 sizeof(struct hv_host_device));
1431 host_dev = shost_priv(host);
1432 memset(host_dev, 0, sizeof(struct hv_host_device));
1434 host_dev->port = host->host_no;
1435 host_dev->dev = device;
1437 host_dev->request_pool =
1438 kmem_cache_create(dev_name(&device->device),
1439 sizeof(struct storvsc_cmd_request), 0,
1440 SLAB_HWCACHE_ALIGN, NULL);
1442 if (!host_dev->request_pool) {
1443 scsi_host_put(host);
1447 host_dev->request_mempool = mempool_create(number, mempool_alloc_slab,
1449 host_dev->request_pool);
1451 if (!host_dev->request_mempool) {
1456 stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL);
1462 stor_device->destroy = false;
1463 init_waitqueue_head(&stor_device->waiting_to_drain);
1464 stor_device->device = device;
1465 stor_device->host = host;
1466 hv_set_drvdata(device, stor_device);
1468 stor_device->port_number = host->host_no;
1469 ret = storvsc_connect_to_vsp(device, storvsc_ringbuffer_size);
1474 storvsc_get_ide_info(device, &target, &path);
1476 host_dev->path = stor_device->path_id;
1477 host_dev->target = stor_device->target_id;
1479 /* max # of devices per target */
1480 host->max_lun = STORVSC_MAX_LUNS_PER_TARGET;
1481 /* max # of targets per channel */
1482 host->max_id = STORVSC_MAX_TARGETS;
1483 /* max # of channels */
1484 host->max_channel = STORVSC_MAX_CHANNELS - 1;
1485 /* max cmd length */
1486 host->max_cmd_len = STORVSC_MAX_CMD_LEN;
1488 /* Register the HBA and start the scsi bus scan */
1489 ret = scsi_add_host(host, &device->device);
1494 scsi_scan_host(host);
1497 ret = scsi_add_device(host, 0, target, 0);
1499 scsi_remove_host(host);
1506 * Once we have connected with the host, we would need to
1507 * to invoke storvsc_dev_remove() to rollback this state and
1508 * this call also frees up the stor_device; hence the jump around
1511 storvsc_dev_remove(device);
1518 mempool_destroy(host_dev->request_mempool);
1521 kmem_cache_destroy(host_dev->request_pool);
1522 scsi_host_put(host);
1526 /* The one and only one */
1528 static struct hv_driver storvsc_drv = {
1529 .name = KBUILD_MODNAME,
1530 .id_table = id_table,
1531 .probe = storvsc_probe,
1532 .remove = storvsc_remove,
1535 static int __init storvsc_drv_init(void)
1537 u32 max_outstanding_req_per_channel;
1540 * Divide the ring buffer data size (which is 1 page less
1541 * than the ring buffer size since that page is reserved for
1542 * the ring buffer indices) by the max request size (which is
1543 * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
1545 max_outstanding_req_per_channel =
1546 ((storvsc_ringbuffer_size - PAGE_SIZE) /
1547 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
1548 sizeof(struct vstor_packet) + sizeof(u64),
1551 if (max_outstanding_req_per_channel <
1552 STORVSC_MAX_IO_REQUESTS)
1555 return vmbus_driver_register(&storvsc_drv);
1558 static void __exit storvsc_drv_exit(void)
1560 vmbus_driver_unregister(&storvsc_drv);
1563 MODULE_LICENSE("GPL");
1564 MODULE_VERSION(HV_DRV_VERSION);
1565 MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
1566 module_init(storvsc_drv_init);
1567 module_exit(storvsc_drv_exit);