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 "hyperv_storage.h"
41 * Copyright (c) 2009, Microsoft Corporation.
43 * This program is free software; you can redistribute it and/or modify it
44 * under the terms and conditions of the GNU General Public License,
45 * version 2, as published by the Free Software Foundation.
47 * This program is distributed in the hope it will be useful, but WITHOUT
48 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
49 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
52 * You should have received a copy of the GNU General Public License along with
53 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
54 * Place - Suite 330, Boston, MA 02111-1307 USA.
57 * Haiyang Zhang <haiyangz@microsoft.com>
58 * Hank Janssen <hjanssen@microsoft.com>
59 * K. Y. Srinivasan <kys@microsoft.com>
62 #include <linux/kernel.h>
63 #include <linux/sched.h>
64 #include <linux/completion.h>
65 #include <linux/string.h>
66 #include <linux/slab.h>
68 #include <linux/delay.h>
71 #include "hyperv_storage.h"
74 static inline struct storvsc_device *alloc_stor_device(struct hv_device *device)
76 struct storvsc_device *stor_device;
78 stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL);
82 stor_device->destroy = false;
83 init_waitqueue_head(&stor_device->waiting_to_drain);
84 stor_device->device = device;
85 device->ext = stor_device;
91 static inline struct storvsc_device *get_in_stor_device(
92 struct hv_device *device)
94 struct storvsc_device *stor_device;
97 spin_lock_irqsave(&device->channel->inbound_lock, flags);
98 stor_device = (struct storvsc_device *)device->ext;
104 * If the device is being destroyed; allow incoming
105 * traffic only to cleanup outstanding requests.
108 if (stor_device->destroy &&
109 (atomic_read(&stor_device->num_outstanding_req) == 0))
113 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
118 static int storvsc_channel_init(struct hv_device *device)
120 struct storvsc_device *stor_device;
121 struct hv_storvsc_request *request;
122 struct vstor_packet *vstor_packet;
125 stor_device = get_out_stor_device(device);
129 request = &stor_device->init_request;
130 vstor_packet = &request->vstor_packet;
133 * Now, initiate the vsc/vsp initialization protocol on the open
136 memset(request, 0, sizeof(struct hv_storvsc_request));
137 init_completion(&request->wait_event);
138 vstor_packet->operation = VSTOR_OPERATION_BEGIN_INITIALIZATION;
139 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
141 ret = vmbus_sendpacket(device->channel, vstor_packet,
142 sizeof(struct vstor_packet),
143 (unsigned long)request,
145 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
149 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
155 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
156 vstor_packet->status != 0)
160 /* reuse the packet for version range supported */
161 memset(vstor_packet, 0, sizeof(struct vstor_packet));
162 vstor_packet->operation = VSTOR_OPERATION_QUERY_PROTOCOL_VERSION;
163 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
165 vstor_packet->version.major_minor = VMSTOR_PROTOCOL_VERSION_CURRENT;
166 FILL_VMSTOR_REVISION(vstor_packet->version.revision);
168 ret = vmbus_sendpacket(device->channel, vstor_packet,
169 sizeof(struct vstor_packet),
170 (unsigned long)request,
172 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
176 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
182 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
183 vstor_packet->status != 0)
187 memset(vstor_packet, 0, sizeof(struct vstor_packet));
188 vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES;
189 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
190 vstor_packet->storage_channel_properties.port_number =
191 stor_device->port_number;
193 ret = vmbus_sendpacket(device->channel, vstor_packet,
194 sizeof(struct vstor_packet),
195 (unsigned long)request,
197 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
202 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
208 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
209 vstor_packet->status != 0)
212 stor_device->path_id = vstor_packet->storage_channel_properties.path_id;
213 stor_device->target_id
214 = vstor_packet->storage_channel_properties.target_id;
216 memset(vstor_packet, 0, sizeof(struct vstor_packet));
217 vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION;
218 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
220 ret = vmbus_sendpacket(device->channel, vstor_packet,
221 sizeof(struct vstor_packet),
222 (unsigned long)request,
224 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
229 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
235 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
236 vstor_packet->status != 0)
244 static void storvsc_on_io_completion(struct hv_device *device,
245 struct vstor_packet *vstor_packet,
246 struct hv_storvsc_request *request)
248 struct storvsc_device *stor_device;
249 struct vstor_packet *stor_pkt;
251 stor_device = (struct storvsc_device *)device->ext;
253 stor_pkt = &request->vstor_packet;
256 /* Copy over the status...etc */
257 stor_pkt->vm_srb.scsi_status = vstor_packet->vm_srb.scsi_status;
258 stor_pkt->vm_srb.srb_status = vstor_packet->vm_srb.srb_status;
259 stor_pkt->vm_srb.sense_info_length =
260 vstor_packet->vm_srb.sense_info_length;
262 if (vstor_packet->vm_srb.scsi_status != 0 ||
263 vstor_packet->vm_srb.srb_status != 1){
265 "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
266 stor_pkt->vm_srb.cdb[0],
267 vstor_packet->vm_srb.scsi_status,
268 vstor_packet->vm_srb.srb_status);
271 if ((vstor_packet->vm_srb.scsi_status & 0xFF) == 0x02) {
272 /* CHECK_CONDITION */
273 if (vstor_packet->vm_srb.srb_status & 0x80) {
274 /* autosense data available */
275 DPRINT_WARN(STORVSC, "storvsc pkt %p autosense data "
276 "valid - len %d\n", request,
277 vstor_packet->vm_srb.sense_info_length);
279 memcpy(request->sense_buffer,
280 vstor_packet->vm_srb.sense_data,
281 vstor_packet->vm_srb.sense_info_length);
286 stor_pkt->vm_srb.data_transfer_length =
287 vstor_packet->vm_srb.data_transfer_length;
289 request->on_io_completion(request);
291 if (atomic_dec_and_test(&stor_device->num_outstanding_req) &&
292 stor_device->drain_notify)
293 wake_up(&stor_device->waiting_to_drain);
298 static void storvsc_on_receive(struct hv_device *device,
299 struct vstor_packet *vstor_packet,
300 struct hv_storvsc_request *request)
302 switch (vstor_packet->operation) {
303 case VSTOR_OPERATION_COMPLETE_IO:
304 storvsc_on_io_completion(device, vstor_packet, request);
306 case VSTOR_OPERATION_REMOVE_DEVICE:
313 static void storvsc_on_channel_callback(void *context)
315 struct hv_device *device = (struct hv_device *)context;
316 struct storvsc_device *stor_device;
319 unsigned char packet[ALIGN(sizeof(struct vstor_packet), 8)];
320 struct hv_storvsc_request *request;
324 stor_device = get_in_stor_device(device);
329 ret = vmbus_recvpacket(device->channel, packet,
330 ALIGN(sizeof(struct vstor_packet), 8),
331 &bytes_recvd, &request_id);
332 if (ret == 0 && bytes_recvd > 0) {
334 request = (struct hv_storvsc_request *)
335 (unsigned long)request_id;
337 if ((request == &stor_device->init_request) ||
338 (request == &stor_device->reset_request)) {
340 memcpy(&request->vstor_packet, packet,
341 sizeof(struct vstor_packet));
342 complete(&request->wait_event);
344 storvsc_on_receive(device,
345 (struct vstor_packet *)packet,
356 static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size)
358 struct vmstorage_channel_properties props;
361 memset(&props, 0, sizeof(struct vmstorage_channel_properties));
363 /* Open the channel */
364 ret = vmbus_open(device->channel,
368 sizeof(struct vmstorage_channel_properties),
369 storvsc_on_channel_callback, device);
374 ret = storvsc_channel_init(device);
379 int storvsc_dev_add(struct hv_device *device,
380 void *additional_info)
382 struct storvsc_device *stor_device;
383 struct storvsc_device_info *device_info;
386 device_info = (struct storvsc_device_info *)additional_info;
387 stor_device = alloc_stor_device(device);
391 /* Save the channel properties to our storvsc channel */
394 * If we support more than 1 scsi channel, we need to set the
395 * port number here to the scsi channel but how do we get the
396 * scsi channel prior to the bus scan.
398 * The host does not support this.
401 stor_device->port_number = device_info->port_number;
402 /* Send it back up */
403 ret = storvsc_connect_to_vsp(device, device_info->ring_buffer_size);
408 device_info->path_id = stor_device->path_id;
409 device_info->target_id = stor_device->target_id;
414 int storvsc_dev_remove(struct hv_device *device)
416 struct storvsc_device *stor_device;
419 stor_device = (struct storvsc_device *)device->ext;
421 spin_lock_irqsave(&device->channel->inbound_lock, flags);
422 stor_device->destroy = true;
423 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
426 * At this point, all outbound traffic should be disable. We
427 * only allow inbound traffic (responses) to proceed so that
428 * outstanding requests can be completed.
431 storvsc_wait_to_drain(stor_device);
434 * Since we have already drained, we don't need to busy wait
435 * as was done in final_release_stor_device()
436 * Note that we cannot set the ext pointer to NULL until
437 * we have drained - to drain the outgoing packets, we need to
438 * allow incoming packets.
440 spin_lock_irqsave(&device->channel->inbound_lock, flags);
442 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
444 /* Close the channel */
445 vmbus_close(device->channel);
451 int storvsc_do_io(struct hv_device *device,
452 struct hv_storvsc_request *request)
454 struct storvsc_device *stor_device;
455 struct vstor_packet *vstor_packet;
458 vstor_packet = &request->vstor_packet;
459 stor_device = get_out_stor_device(device);
465 request->device = device;
468 vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
470 vstor_packet->vm_srb.length = sizeof(struct vmscsi_request);
473 vstor_packet->vm_srb.sense_info_length = SENSE_BUFFER_SIZE;
476 vstor_packet->vm_srb.data_transfer_length =
477 request->data_buffer.len;
479 vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;
481 if (request->data_buffer.len) {
482 ret = vmbus_sendpacket_multipagebuffer(device->channel,
483 &request->data_buffer,
485 sizeof(struct vstor_packet),
486 (unsigned long)request);
488 ret = vmbus_sendpacket(device->channel, vstor_packet,
489 sizeof(struct vstor_packet),
490 (unsigned long)request,
492 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
498 atomic_inc(&stor_device->num_outstanding_req);
504 * The channel properties uniquely specify how the device is to be
505 * presented to the guest. Map this information for use by the block
506 * driver. For Linux guests on Hyper-V, we emulate a scsi HBA in the guest
507 * (storvsc_drv) and so scsi devices in the guest are handled by
508 * native upper level Linux drivers. Consequently, Hyper-V
509 * block driver, while being a generic block driver, presently does not
510 * deal with anything other than devices that would need to be presented
511 * to the guest as an IDE disk.
513 * This function maps the channel properties as embedded in the input
514 * parameter device_info onto information necessary to register the
515 * corresponding block device.
517 * Currently, there is no way to stop the emulation of the block device
518 * on the host side. And so, to prevent the native IDE drivers in Linux
519 * from taking over these devices (to be managedby Hyper-V block
520 * driver), we will take over if need be the major of the IDE controllers.
524 int storvsc_get_major_info(struct storvsc_device_info *device_info,
525 struct storvsc_major_info *major_info)
527 static bool ide0_registered;
528 static bool ide1_registered;
531 * For now we only support IDE disks.
533 major_info->devname = "ide";
534 major_info->diskname = "hd";
536 if (device_info->path_id) {
537 major_info->major = 22;
538 if (!ide1_registered) {
539 major_info->do_register = true;
540 ide1_registered = true;
542 major_info->do_register = false;
544 if (device_info->target_id)
545 major_info->index = 3;
547 major_info->index = 2;
551 major_info->major = 3;
552 if (!ide0_registered) {
553 major_info->do_register = true;
554 ide0_registered = true;
556 major_info->do_register = false;
558 if (device_info->target_id)
559 major_info->index = 1;
561 major_info->index = 0;
568 static int storvsc_ringbuffer_size = STORVSC_RING_BUFFER_SIZE;
570 module_param(storvsc_ringbuffer_size, int, S_IRUGO);
571 MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)");
573 struct hv_host_device {
574 struct hv_device *dev;
575 struct kmem_cache *request_pool;
578 unsigned char target;
581 struct storvsc_cmd_request {
582 struct list_head entry;
583 struct scsi_cmnd *cmd;
585 unsigned int bounce_sgl_count;
586 struct scatterlist *bounce_sgl;
588 struct hv_storvsc_request request;
591 static void storvsc_get_ide_info(struct hv_device *dev, int *target, int *path)
594 dev->dev_instance.b[5] << 8 | dev->dev_instance.b[4];
597 dev->dev_instance.b[3] << 24 |
598 dev->dev_instance.b[2] << 16 |
599 dev->dev_instance.b[1] << 8 | dev->dev_instance.b[0];
603 static int storvsc_device_alloc(struct scsi_device *sdevice)
606 * This enables luns to be located sparsely. Otherwise, we may not
609 sdevice->sdev_bflags |= BLIST_SPARSELUN | BLIST_LARGELUN;
613 static int storvsc_merge_bvec(struct request_queue *q,
614 struct bvec_merge_data *bmd, struct bio_vec *bvec)
616 /* checking done by caller. */
620 static int storvsc_device_configure(struct scsi_device *sdevice)
622 scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG,
623 STORVSC_MAX_IO_REQUESTS);
625 blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
627 blk_queue_merge_bvec(sdevice->request_queue, storvsc_merge_bvec);
629 blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
634 static void destroy_bounce_buffer(struct scatterlist *sgl,
635 unsigned int sg_count)
638 struct page *page_buf;
640 for (i = 0; i < sg_count; i++) {
641 page_buf = sg_page((&sgl[i]));
642 if (page_buf != NULL)
643 __free_page(page_buf);
649 static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
653 /* No need to check */
657 /* We have at least 2 sg entries */
658 for (i = 0; i < sg_count; i++) {
660 /* make sure 1st one does not have hole */
661 if (sgl[i].offset + sgl[i].length != PAGE_SIZE)
663 } else if (i == sg_count - 1) {
664 /* make sure last one does not have hole */
665 if (sgl[i].offset != 0)
668 /* make sure no hole in the middle */
669 if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0)
676 static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
677 unsigned int sg_count,
682 struct scatterlist *bounce_sgl;
683 struct page *page_buf;
685 num_pages = ALIGN(len, PAGE_SIZE) >> PAGE_SHIFT;
687 bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC);
691 for (i = 0; i < num_pages; i++) {
692 page_buf = alloc_page(GFP_ATOMIC);
695 sg_set_page(&bounce_sgl[i], page_buf, 0, 0);
701 destroy_bounce_buffer(bounce_sgl, num_pages);
706 /* Assume the original sgl has enough room */
707 static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
708 struct scatterlist *bounce_sgl,
709 unsigned int orig_sgl_count)
713 unsigned long src, dest;
714 unsigned int srclen, destlen, copylen;
715 unsigned int total_copied = 0;
716 unsigned long bounce_addr = 0;
717 unsigned long dest_addr = 0;
720 local_irq_save(flags);
722 for (i = 0; i < orig_sgl_count; i++) {
723 dest_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
724 KM_IRQ0) + orig_sgl[i].offset;
726 destlen = orig_sgl[i].length;
728 if (bounce_addr == 0)
730 (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])),
734 src = bounce_addr + bounce_sgl[j].offset;
735 srclen = bounce_sgl[j].length - bounce_sgl[j].offset;
737 copylen = min(srclen, destlen);
738 memcpy((void *)dest, (void *)src, copylen);
740 total_copied += copylen;
741 bounce_sgl[j].offset += copylen;
745 if (bounce_sgl[j].offset == bounce_sgl[j].length) {
747 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
750 /* if we need to use another bounce buffer */
751 if (destlen || i != orig_sgl_count - 1)
753 (unsigned long)kmap_atomic(
754 sg_page((&bounce_sgl[j])), KM_IRQ0);
755 } else if (destlen == 0 && i == orig_sgl_count - 1) {
756 /* unmap the last bounce that is < PAGE_SIZE */
757 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
761 kunmap_atomic((void *)(dest_addr - orig_sgl[i].offset),
765 local_irq_restore(flags);
771 /* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
772 static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
773 struct scatterlist *bounce_sgl,
774 unsigned int orig_sgl_count)
778 unsigned long src, dest;
779 unsigned int srclen, destlen, copylen;
780 unsigned int total_copied = 0;
781 unsigned long bounce_addr = 0;
782 unsigned long src_addr = 0;
785 local_irq_save(flags);
787 for (i = 0; i < orig_sgl_count; i++) {
788 src_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
789 KM_IRQ0) + orig_sgl[i].offset;
791 srclen = orig_sgl[i].length;
793 if (bounce_addr == 0)
795 (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])),
799 /* assume bounce offset always == 0 */
800 dest = bounce_addr + bounce_sgl[j].length;
801 destlen = PAGE_SIZE - bounce_sgl[j].length;
803 copylen = min(srclen, destlen);
804 memcpy((void *)dest, (void *)src, copylen);
806 total_copied += copylen;
807 bounce_sgl[j].length += copylen;
811 if (bounce_sgl[j].length == PAGE_SIZE) {
812 /* full..move to next entry */
813 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
816 /* if we need to use another bounce buffer */
817 if (srclen || i != orig_sgl_count - 1)
819 (unsigned long)kmap_atomic(
820 sg_page((&bounce_sgl[j])), KM_IRQ0);
822 } else if (srclen == 0 && i == orig_sgl_count - 1) {
823 /* unmap the last bounce that is < PAGE_SIZE */
824 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
828 kunmap_atomic((void *)(src_addr - orig_sgl[i].offset), KM_IRQ0);
831 local_irq_restore(flags);
837 static int storvsc_remove(struct hv_device *dev)
839 struct Scsi_Host *host = dev_get_drvdata(&dev->device);
840 struct hv_host_device *host_dev =
841 (struct hv_host_device *)host->hostdata;
843 scsi_remove_host(host);
847 storvsc_dev_remove(dev);
848 if (host_dev->request_pool) {
849 kmem_cache_destroy(host_dev->request_pool);
850 host_dev->request_pool = NULL;
856 static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
857 sector_t capacity, int *info)
859 sector_t nsect = capacity;
860 sector_t cylinders = nsect;
861 int heads, sectors_pt;
864 * We are making up these values; let us keep it simple.
867 sectors_pt = 0x3f; /* Sectors per track */
868 sector_div(cylinders, heads * sectors_pt);
869 if ((sector_t)(cylinders + 1) * heads * sectors_pt < nsect)
873 info[1] = sectors_pt;
874 info[2] = (int)cylinders;
879 static int storvsc_host_reset(struct hv_device *device)
881 struct storvsc_device *stor_device;
882 struct hv_storvsc_request *request;
883 struct vstor_packet *vstor_packet;
887 stor_device = get_out_stor_device(device);
891 request = &stor_device->reset_request;
892 vstor_packet = &request->vstor_packet;
894 init_completion(&request->wait_event);
896 vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
897 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
898 vstor_packet->vm_srb.path_id = stor_device->path_id;
900 ret = vmbus_sendpacket(device->channel, vstor_packet,
901 sizeof(struct vstor_packet),
902 (unsigned long)&stor_device->reset_request,
904 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
908 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
916 * At this point, all outstanding requests in the adapter
917 * should have been flushed out and return to us
926 * storvsc_host_reset_handler - Reset the scsi HBA
928 static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
931 struct hv_host_device *host_dev =
932 (struct hv_host_device *)scmnd->device->host->hostdata;
933 struct hv_device *dev = host_dev->dev;
935 ret = storvsc_host_reset(dev);
944 * storvsc_commmand_completion - Command completion processing
946 static void storvsc_commmand_completion(struct hv_storvsc_request *request)
948 struct storvsc_cmd_request *cmd_request =
949 (struct storvsc_cmd_request *)request->context;
950 struct scsi_cmnd *scmnd = cmd_request->cmd;
951 struct hv_host_device *host_dev =
952 (struct hv_host_device *)scmnd->device->host->hostdata;
953 void (*scsi_done_fn)(struct scsi_cmnd *);
954 struct scsi_sense_hdr sense_hdr;
955 struct vmscsi_request *vm_srb;
957 vm_srb = &request->vstor_packet.vm_srb;
958 if (cmd_request->bounce_sgl_count) {
959 if (vm_srb->data_in == READ_TYPE) {
960 copy_from_bounce_buffer(scsi_sglist(scmnd),
961 cmd_request->bounce_sgl,
962 scsi_sg_count(scmnd));
963 destroy_bounce_buffer(cmd_request->bounce_sgl,
964 cmd_request->bounce_sgl_count);
968 scmnd->result = vm_srb->scsi_status;
971 if (scsi_normalize_sense(scmnd->sense_buffer,
972 SCSI_SENSE_BUFFERSIZE, &sense_hdr))
973 scsi_print_sense_hdr("storvsc", &sense_hdr);
976 scsi_set_resid(scmnd,
977 request->data_buffer.len -
978 vm_srb->data_transfer_length);
980 scsi_done_fn = scmnd->scsi_done;
982 scmnd->host_scribble = NULL;
983 scmnd->scsi_done = NULL;
987 kmem_cache_free(host_dev->request_pool, cmd_request);
992 * storvsc_queuecommand - Initiate command processing
994 static int storvsc_queuecommand_lck(struct scsi_cmnd *scmnd,
995 void (*done)(struct scsi_cmnd *))
998 struct hv_host_device *host_dev =
999 (struct hv_host_device *)scmnd->device->host->hostdata;
1000 struct hv_device *dev = host_dev->dev;
1001 struct hv_storvsc_request *request;
1002 struct storvsc_cmd_request *cmd_request;
1003 unsigned int request_size = 0;
1005 struct scatterlist *sgl;
1006 unsigned int sg_count = 0;
1007 struct vmscsi_request *vm_srb;
1010 /* If retrying, no need to prep the cmd */
1011 if (scmnd->host_scribble) {
1014 (struct storvsc_cmd_request *)scmnd->host_scribble;
1019 scmnd->scsi_done = done;
1021 request_size = sizeof(struct storvsc_cmd_request);
1023 cmd_request = kmem_cache_zalloc(host_dev->request_pool,
1026 scmnd->scsi_done = NULL;
1027 return SCSI_MLQUEUE_DEVICE_BUSY;
1030 /* Setup the cmd request */
1031 cmd_request->bounce_sgl_count = 0;
1032 cmd_request->bounce_sgl = NULL;
1033 cmd_request->cmd = scmnd;
1035 scmnd->host_scribble = (unsigned char *)cmd_request;
1037 request = &cmd_request->request;
1038 vm_srb = &request->vstor_packet.vm_srb;
1042 switch (scmnd->sc_data_direction) {
1044 vm_srb->data_in = WRITE_TYPE;
1046 case DMA_FROM_DEVICE:
1047 vm_srb->data_in = READ_TYPE;
1050 vm_srb->data_in = UNKNOWN_TYPE;
1054 request->on_io_completion = storvsc_commmand_completion;
1055 request->context = cmd_request;/* scmnd; */
1057 vm_srb->port_number = host_dev->port;
1058 vm_srb->path_id = scmnd->device->channel;
1059 vm_srb->target_id = scmnd->device->id;
1060 vm_srb->lun = scmnd->device->lun;
1062 vm_srb->cdb_length = scmnd->cmd_len;
1064 memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
1066 request->sense_buffer = scmnd->sense_buffer;
1069 request->data_buffer.len = scsi_bufflen(scmnd);
1070 if (scsi_sg_count(scmnd)) {
1071 sgl = (struct scatterlist *)scsi_sglist(scmnd);
1072 sg_count = scsi_sg_count(scmnd);
1074 /* check if we need to bounce the sgl */
1075 if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) {
1076 cmd_request->bounce_sgl =
1077 create_bounce_buffer(sgl, scsi_sg_count(scmnd),
1078 scsi_bufflen(scmnd));
1079 if (!cmd_request->bounce_sgl) {
1080 scmnd->scsi_done = NULL;
1081 scmnd->host_scribble = NULL;
1082 kmem_cache_free(host_dev->request_pool,
1085 return SCSI_MLQUEUE_HOST_BUSY;
1088 cmd_request->bounce_sgl_count =
1089 ALIGN(scsi_bufflen(scmnd), PAGE_SIZE) >>
1092 if (vm_srb->data_in == WRITE_TYPE)
1093 copy_to_bounce_buffer(sgl,
1094 cmd_request->bounce_sgl,
1095 scsi_sg_count(scmnd));
1097 sgl = cmd_request->bounce_sgl;
1098 sg_count = cmd_request->bounce_sgl_count;
1101 request->data_buffer.offset = sgl[0].offset;
1103 for (i = 0; i < sg_count; i++)
1104 request->data_buffer.pfn_array[i] =
1105 page_to_pfn(sg_page((&sgl[i])));
1107 } else if (scsi_sglist(scmnd)) {
1108 request->data_buffer.offset =
1109 virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
1110 request->data_buffer.pfn_array[0] =
1111 virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
1115 /* Invokes the vsc to start an IO */
1116 ret = storvsc_do_io(dev, &cmd_request->request);
1118 if (ret == -EAGAIN) {
1121 if (cmd_request->bounce_sgl_count) {
1123 * FIXME: We can optimize on writes by just skipping
1126 copy_from_bounce_buffer(scsi_sglist(scmnd),
1127 cmd_request->bounce_sgl,
1128 scsi_sg_count(scmnd));
1129 destroy_bounce_buffer(cmd_request->bounce_sgl,
1130 cmd_request->bounce_sgl_count);
1133 kmem_cache_free(host_dev->request_pool, cmd_request);
1135 scmnd->scsi_done = NULL;
1136 scmnd->host_scribble = NULL;
1138 ret = SCSI_MLQUEUE_DEVICE_BUSY;
1144 static DEF_SCSI_QCMD(storvsc_queuecommand)
1148 static struct scsi_host_template scsi_driver = {
1149 .module = THIS_MODULE,
1150 .name = "storvsc_host_t",
1151 .bios_param = storvsc_get_chs,
1152 .queuecommand = storvsc_queuecommand,
1153 .eh_host_reset_handler = storvsc_host_reset_handler,
1154 .slave_alloc = storvsc_device_alloc,
1155 .slave_configure = storvsc_device_configure,
1157 /* 64 max_queue * 1 target */
1158 .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS,
1160 /* no use setting to 0 since ll_blk_rw reset it to 1 */
1162 .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT,
1164 * ENABLE_CLUSTERING allows mutiple physically contig bio_vecs to merge
1165 * into 1 sg element. If set, we must limit the max_segment_size to
1166 * PAGE_SIZE, otherwise we may get 1 sg element that represents
1169 /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */
1170 .use_clustering = ENABLE_CLUSTERING,
1171 /* Make sure we dont get a sg segment crosses a page boundary */
1172 .dma_boundary = PAGE_SIZE-1,
1176 * The storvsc_probe function assumes that the IDE guid
1177 * is the second entry.
1179 static const struct hv_vmbus_device_id id_table[] = {
1181 { VMBUS_DEVICE(0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
1182 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f) },
1184 { VMBUS_DEVICE(0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44,
1185 0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5) },
1189 MODULE_DEVICE_TABLE(vmbus, id_table);
1193 * storvsc_probe - Add a new device for this driver
1196 static int storvsc_probe(struct hv_device *device)
1199 struct Scsi_Host *host;
1200 struct hv_host_device *host_dev;
1201 struct storvsc_device_info device_info;
1206 if (!memcmp(&device->dev_type.b, id_table[1].guid, sizeof(uuid_le)))
1211 host = scsi_host_alloc(&scsi_driver,
1212 sizeof(struct hv_host_device));
1216 dev_set_drvdata(&device->device, host);
1218 host_dev = (struct hv_host_device *)host->hostdata;
1219 memset(host_dev, 0, sizeof(struct hv_host_device));
1221 host_dev->port = host->host_no;
1222 host_dev->dev = device;
1224 host_dev->request_pool =
1225 kmem_cache_create(dev_name(&device->device),
1226 sizeof(struct storvsc_cmd_request), 0,
1227 SLAB_HWCACHE_ALIGN, NULL);
1229 if (!host_dev->request_pool) {
1230 scsi_host_put(host);
1234 device_info.port_number = host->host_no;
1235 device_info.ring_buffer_size = storvsc_ringbuffer_size;
1236 /* Call to the vsc driver to add the device */
1237 ret = storvsc_dev_add(device, (void *)&device_info);
1240 kmem_cache_destroy(host_dev->request_pool);
1241 scsi_host_put(host);
1246 storvsc_get_ide_info(device, &target, &path);
1248 host_dev->path = device_info.path_id;
1249 host_dev->target = device_info.target_id;
1251 /* max # of devices per target */
1252 host->max_lun = STORVSC_MAX_LUNS_PER_TARGET;
1253 /* max # of targets per channel */
1254 host->max_id = STORVSC_MAX_TARGETS;
1255 /* max # of channels */
1256 host->max_channel = STORVSC_MAX_CHANNELS - 1;
1258 /* Register the HBA and start the scsi bus scan */
1259 ret = scsi_add_host(host, &device->device);
1264 scsi_scan_host(host);
1267 ret = scsi_add_device(host, 0, target, 0);
1269 scsi_remove_host(host);
1275 storvsc_dev_remove(device);
1276 kmem_cache_destroy(host_dev->request_pool);
1277 scsi_host_put(host);
1281 /* The one and only one */
1283 static struct hv_driver storvsc_drv = {
1285 .id_table = id_table,
1286 .probe = storvsc_probe,
1287 .remove = storvsc_remove,
1290 static int __init storvsc_drv_init(void)
1292 u32 max_outstanding_req_per_channel;
1295 * Divide the ring buffer data size (which is 1 page less
1296 * than the ring buffer size since that page is reserved for
1297 * the ring buffer indices) by the max request size (which is
1298 * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
1300 max_outstanding_req_per_channel =
1301 ((storvsc_ringbuffer_size - PAGE_SIZE) /
1302 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
1303 sizeof(struct vstor_packet) + sizeof(u64),
1306 if (max_outstanding_req_per_channel <
1307 STORVSC_MAX_IO_REQUESTS)
1310 return vmbus_driver_register(&storvsc_drv);
1313 static void __exit storvsc_drv_exit(void)
1315 vmbus_driver_unregister(&storvsc_drv);
1318 MODULE_LICENSE("GPL");
1319 MODULE_VERSION(HV_DRV_VERSION);
1320 MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
1321 module_init(storvsc_drv_init);
1322 module_exit(storvsc_drv_exit);