storvsc_drv.c 47.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (c) 2009, Microsoft Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place - Suite 330, Boston, MA 02111-1307 USA.
 *
 * Authors:
 *   Haiyang Zhang <haiyangz@microsoft.com>
 *   Hank Janssen  <hjanssen@microsoft.com>
20
 *   K. Y. Srinivasan <kys@microsoft.com>
21
 */
22 23

#include <linux/kernel.h>
24
#include <linux/wait.h>
25 26 27 28 29
#include <linux/sched.h>
#include <linux/completion.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/delay.h>
30
#include <linux/init.h>
31
#include <linux/slab.h>
32 33
#include <linux/module.h>
#include <linux/device.h>
34
#include <linux/hyperv.h>
35
#include <linux/mempool.h>
36
#include <linux/blkdev.h>
37 38 39 40 41 42 43 44
#include <scsi/scsi.h>
#include <scsi/scsi_cmnd.h>
#include <scsi/scsi_host.h>
#include <scsi/scsi_device.h>
#include <scsi/scsi_tcq.h>
#include <scsi/scsi_eh.h>
#include <scsi/scsi_devinfo.h>
#include <scsi/scsi_dbg.h>
45

46
/*
47 48 49 50
 * All wire protocol details (storage protocol between the guest and the host)
 * are consolidated here.
 *
 * Begin protocol definitions.
51 52 53 54 55 56 57 58
 */

/*
 * Version history:
 * V1 Beta: 0.1
 * V1 RC < 2008/1/31: 1.0
 * V1 RC > 2008/1/31:  2.0
 * Win7: 4.2
59
 * Win8: 5.1
60 61
 */

62 63 64 65 66 67

#define VMSTOR_WIN7_MAJOR 4
#define VMSTOR_WIN7_MINOR 2

#define VMSTOR_WIN8_MAJOR 5
#define VMSTOR_WIN8_MINOR 1
68 69 70 71 72 73 74 75 76 77 78 79 80 81


/*  Packet structure describing virtual storage requests. */
enum vstor_packet_operation {
	VSTOR_OPERATION_COMPLETE_IO		= 1,
	VSTOR_OPERATION_REMOVE_DEVICE		= 2,
	VSTOR_OPERATION_EXECUTE_SRB		= 3,
	VSTOR_OPERATION_RESET_LUN		= 4,
	VSTOR_OPERATION_RESET_ADAPTER		= 5,
	VSTOR_OPERATION_RESET_BUS		= 6,
	VSTOR_OPERATION_BEGIN_INITIALIZATION	= 7,
	VSTOR_OPERATION_END_INITIALIZATION	= 8,
	VSTOR_OPERATION_QUERY_PROTOCOL_VERSION	= 9,
	VSTOR_OPERATION_QUERY_PROPERTIES	= 10,
82
	VSTOR_OPERATION_ENUMERATE_BUS		= 11,
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
	VSTOR_OPERATION_FCHBA_DATA              = 12,
	VSTOR_OPERATION_CREATE_SUB_CHANNELS     = 13,
	VSTOR_OPERATION_MAXIMUM                 = 13
};

/*
 * WWN packet for Fibre Channel HBA
 */

struct hv_fc_wwn_packet {
	bool	primary_active;
	u8	reserved1;
	u8	reserved2;
	u8	primary_port_wwn[8];
	u8	primary_node_wwn[8];
	u8	secondary_port_wwn[8];
	u8	secondary_node_wwn[8];
100 101
};

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136


/*
 * SRB Flag Bits
 */

#define SRB_FLAGS_QUEUE_ACTION_ENABLE		0x00000002
#define SRB_FLAGS_DISABLE_DISCONNECT		0x00000004
#define SRB_FLAGS_DISABLE_SYNCH_TRANSFER	0x00000008
#define SRB_FLAGS_BYPASS_FROZEN_QUEUE		0x00000010
#define SRB_FLAGS_DISABLE_AUTOSENSE		0x00000020
#define SRB_FLAGS_DATA_IN			0x00000040
#define SRB_FLAGS_DATA_OUT			0x00000080
#define SRB_FLAGS_NO_DATA_TRANSFER		0x00000000
#define SRB_FLAGS_UNSPECIFIED_DIRECTION	(SRB_FLAGS_DATA_IN | SRB_FLAGS_DATA_OUT)
#define SRB_FLAGS_NO_QUEUE_FREEZE		0x00000100
#define SRB_FLAGS_ADAPTER_CACHE_ENABLE		0x00000200
#define SRB_FLAGS_FREE_SENSE_BUFFER		0x00000400

/*
 * This flag indicates the request is part of the workflow for processing a D3.
 */
#define SRB_FLAGS_D3_PROCESSING			0x00000800
#define SRB_FLAGS_IS_ACTIVE			0x00010000
#define SRB_FLAGS_ALLOCATED_FROM_ZONE		0x00020000
#define SRB_FLAGS_SGLIST_FROM_POOL		0x00040000
#define SRB_FLAGS_BYPASS_LOCKED_QUEUE		0x00080000
#define SRB_FLAGS_NO_KEEP_AWAKE			0x00100000
#define SRB_FLAGS_PORT_DRIVER_ALLOCSENSE	0x00200000
#define SRB_FLAGS_PORT_DRIVER_SENSEHASPORT	0x00400000
#define SRB_FLAGS_DONT_START_NEXT_PACKET	0x00800000
#define SRB_FLAGS_PORT_DRIVER_RESERVED		0x0F000000
#define SRB_FLAGS_CLASS_DRIVER_RESERVED		0xF0000000


137 138 139 140 141
/*
 * Platform neutral description of a scsi request -
 * this remains the same across the write regardless of 32/64 bit
 * note: it's patterned off the SCSI_PASS_THROUGH structure
 */
142
#define STORVSC_MAX_CMD_LEN			0x10
143 144 145 146 147

#define POST_WIN7_STORVSC_SENSE_BUFFER_SIZE	0x14
#define PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE	0x12

#define STORVSC_SENSE_BUFFER_SIZE		0x14
148
#define STORVSC_MAX_BUF_LEN_WITH_PADDING	0x14
149

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
/*
 * Sense buffer size changed in win8; have a run-time
 * variable to track the size we should use.
 */
static int sense_buffer_size;

/*
 * The size of the vmscsi_request has changed in win8. The
 * additional size is because of new elements added to the
 * structure. These elements are valid only when we are talking
 * to a win8 host.
 * Track the correction to size we need to apply.
 */

static int vmscsi_size_delta;
static int vmstor_current_major;
static int vmstor_current_minor;

struct vmscsi_win8_extension {
	/*
	 * The following were added in Windows 8
	 */
	u16 reserve;
	u8  queue_tag;
	u8  queue_action;
	u32 srb_flags;
	u32 time_out_value;
	u32 queue_sort_ey;
} __packed;

180
struct vmscsi_request {
181 182 183
	u16 length;
	u8 srb_status;
	u8 scsi_status;
184

185 186 187 188
	u8  port_number;
	u8  path_id;
	u8  target_id;
	u8  lun;
189

190 191 192 193
	u8  cdb_length;
	u8  sense_info_length;
	u8  data_in;
	u8  reserved;
194

195
	u32 data_transfer_length;
196 197

	union {
198 199 200
		u8 cdb[STORVSC_MAX_CMD_LEN];
		u8 sense_data[STORVSC_SENSE_BUFFER_SIZE];
		u8 reserved_array[STORVSC_MAX_BUF_LEN_WITH_PADDING];
201
	};
202 203 204 205 206
	/*
	 * The following was added in win8.
	 */
	struct vmscsi_win8_extension win8_extension;

207 208 209 210 211 212 213
} __attribute((packed));


/*
 * This structure is sent during the intialization phase to get the different
 * properties of the channel.
 */
214 215 216

#define STORAGE_CHANNEL_SUPPORTS_MULTI_CHANNEL		0x1

217
struct vmstorage_channel_properties {
218 219 220
	u32 reserved;
	u16 max_channel_cnt;
	u16 reserved1;
221

222
	u32 flags;
223
	u32   max_transfer_bytes;
224

225
	u64  reserved2;
226 227 228 229 230
} __packed;

/*  This structure is sent during the storage protocol negotiations. */
struct vmstorage_protocol_version {
	/* Major (MSW) and minor (LSW) version numbers. */
231
	u16 major_minor;
232 233 234 235 236 237

	/*
	 * Revision number is auto-incremented whenever this file is changed
	 * (See FILL_VMSTOR_REVISION macro above).  Mismatch does not
	 * definitely indicate incompatibility--but it does indicate mismatched
	 * builds.
238
	 * This is only used on the windows side. Just set it to 0.
239
	 */
240
	u16 revision;
241 242 243 244 245 246 247 248 249 250 251
} __packed;

/* Channel Property Flags */
#define STORAGE_CHANNEL_REMOVABLE_FLAG		0x1
#define STORAGE_CHANNEL_EMULATED_IDE_FLAG	0x2

struct vstor_packet {
	/* Requested operation type */
	enum vstor_packet_operation operation;

	/*  Flags - see below for values */
252
	u32 flags;
253 254

	/* Status of the request returned from the server side. */
255
	u32 status;
256 257 258 259 260 261 262 263 264 265 266 267 268 269

	/* Data payload area */
	union {
		/*
		 * Structure used to forward SCSI commands from the
		 * client to the server.
		 */
		struct vmscsi_request vm_srb;

		/* Structure used to query channel properties. */
		struct vmstorage_channel_properties storage_channel_properties;

		/* Used during version negotiations. */
		struct vmstorage_protocol_version version;
270 271 272 273 274 275 276 277 278

		/* Fibre channel address packet */
		struct hv_fc_wwn_packet wwn_packet;

		/* Number of sub-channels to create */
		u16 sub_channel_count;

		/* This will be the maximum of the union members */
		u8  buffer[0x34];
279 280 281 282
	};
} __packed;

/*
283 284
 * Packet Flags:
 *
285 286 287
 * This flag indicates that the server should send back a completion for this
 * packet.
 */
288

289 290 291 292
#define REQUEST_COMPLETION_FLAG	0x1

/* Matches Windows-end */
enum storvsc_request_type {
293
	WRITE_TYPE = 0,
294 295 296 297
	READ_TYPE,
	UNKNOWN_TYPE,
};

298 299 300 301 302 303 304
/*
 * SRB status codes and masks; a subset of the codes used here.
 */

#define SRB_STATUS_AUTOSENSE_VALID	0x80
#define SRB_STATUS_INVALID_LUN	0x20
#define SRB_STATUS_SUCCESS	0x01
305
#define SRB_STATUS_ABORTED	0x02
306 307
#define SRB_STATUS_ERROR	0x04

308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
/*
 * This is the end of Protocol specific defines.
 */


/*
 * We setup a mempool to allocate request structures for this driver
 * on a per-lun basis. The following define specifies the number of
 * elements in the pool.
 */

#define STORVSC_MIN_BUF_NR				64
static int storvsc_ringbuffer_size = (20 * PAGE_SIZE);

module_param(storvsc_ringbuffer_size, int, S_IRUGO);
MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)");

325 326 327 328 329
/*
 * Timeout in seconds for all devices managed by this driver.
 */
static int storvsc_timeout = 180;

K
K. Y. Srinivasan 已提交
330 331
static int msft_blist_flags = BLIST_TRY_VPD_PAGES;

332
#define STORVSC_MAX_IO_REQUESTS				200
333

334 335
static void storvsc_on_channel_callback(void *context);

336 337 338
#define STORVSC_MAX_LUNS_PER_TARGET			255
#define STORVSC_MAX_TARGETS				2
#define STORVSC_MAX_CHANNELS				8
339

340 341 342
#define STORVSC_FC_MAX_LUNS_PER_TARGET			255
#define STORVSC_FC_MAX_TARGETS				128
#define STORVSC_FC_MAX_CHANNELS				8
343

344 345 346
#define STORVSC_IDE_MAX_LUNS_PER_TARGET			64
#define STORVSC_IDE_MAX_TARGETS				1
#define STORVSC_IDE_MAX_CHANNELS			1
347

348 349 350 351 352 353
struct storvsc_cmd_request {
	struct list_head entry;
	struct scsi_cmnd *cmd;

	unsigned int bounce_sgl_count;
	struct scatterlist *bounce_sgl;
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371

	struct hv_device *device;

	/* Synchronize the request/response if needed */
	struct completion wait_event;

	unsigned char *sense_buffer;
	struct hv_multipage_buffer data_buffer;
	struct vstor_packet vstor_packet;
};


/* A storvsc device is a device object that contains a vmbus channel */
struct storvsc_device {
	struct hv_device *device;

	bool	 destroy;
	bool	 drain_notify;
372
	bool	 open_sub_channel;
373
	atomic_t num_outstanding_req;
374
	struct Scsi_Host *host;
375 376 377 378 379 380 381 382 383 384 385 386 387

	wait_queue_head_t waiting_to_drain;

	/*
	 * Each unique Port/Path/Target represents 1 channel ie scsi
	 * controller. In reality, the pathid, targetid is always 0
	 * and the port is set by us
	 */
	unsigned int port_number;
	unsigned char path_id;
	unsigned char target_id;

	/* Used for vsc/vsp channel reset process */
388 389
	struct storvsc_cmd_request init_request;
	struct storvsc_cmd_request reset_request;
390 391
};

392
struct stor_mem_pools {
393
	struct kmem_cache *request_pool;
394
	mempool_t *request_mempool;
395 396 397 398
};

struct hv_host_device {
	struct hv_device *dev;
399 400 401 402 403
	unsigned int port;
	unsigned char path;
	unsigned char target;
};

404 405 406 407 408 409
struct storvsc_scan_work {
	struct work_struct work;
	struct Scsi_Host *host;
	uint lun;
};

410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
static void storvsc_device_scan(struct work_struct *work)
{
	struct storvsc_scan_work *wrk;
	uint lun;
	struct scsi_device *sdev;

	wrk = container_of(work, struct storvsc_scan_work, work);
	lun = wrk->lun;

	sdev = scsi_device_lookup(wrk->host, 0, 0, lun);
	if (!sdev)
		goto done;
	scsi_rescan_device(&sdev->sdev_gendev);
	scsi_device_put(sdev);

done:
	kfree(wrk);
}

429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
static void storvsc_bus_scan(struct work_struct *work)
{
	struct storvsc_scan_work *wrk;
	int id, order_id;

	wrk = container_of(work, struct storvsc_scan_work, work);
	for (id = 0; id < wrk->host->max_id; ++id) {
		if (wrk->host->reverse_ordering)
			order_id = wrk->host->max_id - id - 1;
		else
			order_id = id;

		scsi_scan_target(&wrk->host->shost_gendev, 0,
				order_id, SCAN_WILD_CARD, 1);
	}
	kfree(wrk);
}

447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
static void storvsc_remove_lun(struct work_struct *work)
{
	struct storvsc_scan_work *wrk;
	struct scsi_device *sdev;

	wrk = container_of(work, struct storvsc_scan_work, work);
	if (!scsi_host_get(wrk->host))
		goto done;

	sdev = scsi_device_lookup(wrk->host, 0, 0, wrk->lun);

	if (sdev) {
		scsi_remove_device(sdev);
		scsi_device_put(sdev);
	}
	scsi_host_put(wrk->host);

done:
	kfree(wrk);
}

468 469 470 471 472 473 474 475 476 477 478 479 480
/*
 * Major/minor macros.  Minor version is in LSB, meaning that earlier flat
 * version numbers will be interpreted as "0.x" (i.e., 1 becomes 0.1).
 */

static inline u16 storvsc_get_version(u8 major, u8 minor)
{
	u16 version;

	version = ((major << 8) | minor);
	return version;
}

481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
/*
 * We can get incoming messages from the host that are not in response to
 * messages that we have sent out. An example of this would be messages
 * received by the guest to notify dynamic addition/removal of LUNs. To
 * deal with potential race conditions where the driver may be in the
 * midst of being unloaded when we might receive an unsolicited message
 * from the host, we have implemented a mechanism to gurantee sequential
 * consistency:
 *
 * 1) Once the device is marked as being destroyed, we will fail all
 *    outgoing messages.
 * 2) We permit incoming messages when the device is being destroyed,
 *    only to properly account for messages already sent out.
 */

496 497 498 499 500
static inline struct storvsc_device *get_out_stor_device(
					struct hv_device *device)
{
	struct storvsc_device *stor_device;

501
	stor_device = hv_get_drvdata(device);
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516

	if (stor_device && stor_device->destroy)
		stor_device = NULL;

	return stor_device;
}


static inline void storvsc_wait_to_drain(struct storvsc_device *dev)
{
	dev->drain_notify = true;
	wait_event(dev->waiting_to_drain,
		   atomic_read(&dev->num_outstanding_req) == 0);
	dev->drain_notify = false;
}
517

518 519 520 521 522
static inline struct storvsc_device *get_in_stor_device(
					struct hv_device *device)
{
	struct storvsc_device *stor_device;

523
	stor_device = hv_get_drvdata(device);
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541

	if (!stor_device)
		goto get_in_err;

	/*
	 * If the device is being destroyed; allow incoming
	 * traffic only to cleanup outstanding requests.
	 */

	if (stor_device->destroy  &&
		(atomic_read(&stor_device->num_outstanding_req) == 0))
		stor_device = NULL;

get_in_err:
	return stor_device;

}

542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
static void destroy_bounce_buffer(struct scatterlist *sgl,
				  unsigned int sg_count)
{
	int i;
	struct page *page_buf;

	for (i = 0; i < sg_count; i++) {
		page_buf = sg_page((&sgl[i]));
		if (page_buf != NULL)
			__free_page(page_buf);
	}

	kfree(sgl);
}

static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
{
	int i;

	/* No need to check */
	if (sg_count < 2)
		return -1;

	/* We have at least 2 sg entries */
	for (i = 0; i < sg_count; i++) {
		if (i == 0) {
			/* make sure 1st one does not have hole */
			if (sgl[i].offset + sgl[i].length != PAGE_SIZE)
				return i;
		} else if (i == sg_count - 1) {
			/* make sure last one does not have hole */
			if (sgl[i].offset != 0)
				return i;
		} else {
			/* make sure no hole in the middle */
			if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0)
				return i;
		}
	}
	return -1;
}

static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
						unsigned int sg_count,
						unsigned int len,
						int write)
{
	int i;
	int num_pages;
	struct scatterlist *bounce_sgl;
	struct page *page_buf;
	unsigned int buf_len = ((write == WRITE_TYPE) ? 0 : PAGE_SIZE);

	num_pages = ALIGN(len, PAGE_SIZE) >> PAGE_SHIFT;

	bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC);
	if (!bounce_sgl)
		return NULL;

601
	sg_init_table(bounce_sgl, num_pages);
602 603 604 605 606 607 608 609 610 611 612 613 614 615
	for (i = 0; i < num_pages; i++) {
		page_buf = alloc_page(GFP_ATOMIC);
		if (!page_buf)
			goto cleanup;
		sg_set_page(&bounce_sgl[i], page_buf, buf_len, 0);
	}

	return bounce_sgl;

cleanup:
	destroy_bounce_buffer(bounce_sgl, num_pages);
	return NULL;
}

616 617 618 619 620 621
/* Disgusting wrapper functions */
static inline unsigned long sg_kmap_atomic(struct scatterlist *sgl, int idx)
{
	void *addr = kmap_atomic(sg_page(sgl + idx));
	return (unsigned long)addr;
}
622

623 624 625 626
static inline void sg_kunmap_atomic(unsigned long addr)
{
	kunmap_atomic((void *)addr);
}
627 628


629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
/* Assume the original sgl has enough room */
static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
					    struct scatterlist *bounce_sgl,
					    unsigned int orig_sgl_count,
					    unsigned int bounce_sgl_count)
{
	int i;
	int j = 0;
	unsigned long src, dest;
	unsigned int srclen, destlen, copylen;
	unsigned int total_copied = 0;
	unsigned long bounce_addr = 0;
	unsigned long dest_addr = 0;
	unsigned long flags;

	local_irq_save(flags);

	for (i = 0; i < orig_sgl_count; i++) {
647
		dest_addr = sg_kmap_atomic(orig_sgl,i) + orig_sgl[i].offset;
648 649 650 651
		dest = dest_addr;
		destlen = orig_sgl[i].length;

		if (bounce_addr == 0)
652
			bounce_addr = sg_kmap_atomic(bounce_sgl,j);
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667

		while (destlen) {
			src = bounce_addr + bounce_sgl[j].offset;
			srclen = bounce_sgl[j].length - bounce_sgl[j].offset;

			copylen = min(srclen, destlen);
			memcpy((void *)dest, (void *)src, copylen);

			total_copied += copylen;
			bounce_sgl[j].offset += copylen;
			destlen -= copylen;
			dest += copylen;

			if (bounce_sgl[j].offset == bounce_sgl[j].length) {
				/* full */
668
				sg_kunmap_atomic(bounce_addr);
669 670 671 672 673 674 675 676 677 678 679 680 681
				j++;

				/*
				 * It is possible that the number of elements
				 * in the bounce buffer may not be equal to
				 * the number of elements in the original
				 * scatter list. Handle this correctly.
				 */

				if (j == bounce_sgl_count) {
					/*
					 * We are done; cleanup and return.
					 */
682
					sg_kunmap_atomic(dest_addr - orig_sgl[i].offset);
683 684 685 686 687 688
					local_irq_restore(flags);
					return total_copied;
				}

				/* if we need to use another bounce buffer */
				if (destlen || i != orig_sgl_count - 1)
689
					bounce_addr = sg_kmap_atomic(bounce_sgl,j);
690 691
			} else if (destlen == 0 && i == orig_sgl_count - 1) {
				/* unmap the last bounce that is < PAGE_SIZE */
692
				sg_kunmap_atomic(bounce_addr);
693 694 695
			}
		}

696
		sg_kunmap_atomic(dest_addr - orig_sgl[i].offset);
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
	}

	local_irq_restore(flags);

	return total_copied;
}

/* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
					  struct scatterlist *bounce_sgl,
					  unsigned int orig_sgl_count)
{
	int i;
	int j = 0;
	unsigned long src, dest;
	unsigned int srclen, destlen, copylen;
	unsigned int total_copied = 0;
	unsigned long bounce_addr = 0;
	unsigned long src_addr = 0;
	unsigned long flags;

	local_irq_save(flags);

	for (i = 0; i < orig_sgl_count; i++) {
721
		src_addr = sg_kmap_atomic(orig_sgl,i) + orig_sgl[i].offset;
722 723 724 725
		src = src_addr;
		srclen = orig_sgl[i].length;

		if (bounce_addr == 0)
726
			bounce_addr = sg_kmap_atomic(bounce_sgl,j);
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742

		while (srclen) {
			/* assume bounce offset always == 0 */
			dest = bounce_addr + bounce_sgl[j].length;
			destlen = PAGE_SIZE - bounce_sgl[j].length;

			copylen = min(srclen, destlen);
			memcpy((void *)dest, (void *)src, copylen);

			total_copied += copylen;
			bounce_sgl[j].length += copylen;
			srclen -= copylen;
			src += copylen;

			if (bounce_sgl[j].length == PAGE_SIZE) {
				/* full..move to next entry */
743
				sg_kunmap_atomic(bounce_addr);
744 745 746 747
				j++;

				/* if we need to use another bounce buffer */
				if (srclen || i != orig_sgl_count - 1)
748
					bounce_addr = sg_kmap_atomic(bounce_sgl,j);
749 750 751

			} else if (srclen == 0 && i == orig_sgl_count - 1) {
				/* unmap the last bounce that is < PAGE_SIZE */
752
				sg_kunmap_atomic(bounce_addr);
753 754 755
			}
		}

756
		sg_kunmap_atomic(src_addr - orig_sgl[i].offset);
757 758 759 760 761 762 763
	}

	local_irq_restore(flags);

	return total_copied;
}

764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
static void handle_sc_creation(struct vmbus_channel *new_sc)
{
	struct hv_device *device = new_sc->primary_channel->device_obj;
	struct storvsc_device *stor_device;
	struct vmstorage_channel_properties props;

	stor_device = get_out_stor_device(device);
	if (!stor_device)
		return;

	if (stor_device->open_sub_channel == false)
		return;

	memset(&props, 0, sizeof(struct vmstorage_channel_properties));

	vmbus_open(new_sc,
		   storvsc_ringbuffer_size,
		   storvsc_ringbuffer_size,
		   (void *)&props,
		   sizeof(struct vmstorage_channel_properties),
		   storvsc_on_channel_callback, new_sc);
}

static void  handle_multichannel_storage(struct hv_device *device, int max_chns)
{
	struct storvsc_device *stor_device;
	int num_cpus = num_online_cpus();
	int num_sc;
	struct storvsc_cmd_request *request;
	struct vstor_packet *vstor_packet;
	int ret, t;

	num_sc = ((max_chns > num_cpus) ? num_cpus : max_chns);
	stor_device = get_out_stor_device(device);
	if (!stor_device)
		return;

	request = &stor_device->init_request;
	vstor_packet = &request->vstor_packet;

	stor_device->open_sub_channel = true;
	/*
	 * Establish a handler for dealing with subchannels.
	 */
	vmbus_set_sc_create_callback(device->channel, handle_sc_creation);

	/*
	 * Check to see if sub-channels have already been created. This
	 * can happen when this driver is re-loaded after unloading.
	 */

	if (vmbus_are_subchannels_present(device->channel))
		return;

	stor_device->open_sub_channel = false;
	/*
	 * Request the host to create sub-channels.
	 */
	memset(request, 0, sizeof(struct storvsc_cmd_request));
	init_completion(&request->wait_event);
	vstor_packet->operation = VSTOR_OPERATION_CREATE_SUB_CHANNELS;
	vstor_packet->flags = REQUEST_COMPLETION_FLAG;
	vstor_packet->sub_channel_count = num_sc;

	ret = vmbus_sendpacket(device->channel, vstor_packet,
			       (sizeof(struct vstor_packet) -
			       vmscsi_size_delta),
			       (unsigned long)request,
			       VM_PKT_DATA_INBAND,
			       VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);

	if (ret != 0)
		return;

	t = wait_for_completion_timeout(&request->wait_event, 10*HZ);
	if (t == 0)
		return;

	if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
	    vstor_packet->status != 0)
		return;

	/*
	 * Now that we created the sub-channels, invoke the check; this
	 * may trigger the callback.
	 */
	stor_device->open_sub_channel = true;
	vmbus_are_subchannels_present(device->channel);
}

854 855 856
static int storvsc_channel_init(struct hv_device *device)
{
	struct storvsc_device *stor_device;
857
	struct storvsc_cmd_request *request;
858 859
	struct vstor_packet *vstor_packet;
	int ret, t;
860 861
	int max_chns;
	bool process_sub_channels = false;
862 863 864 865 866 867 868 869 870 871 872 873

	stor_device = get_out_stor_device(device);
	if (!stor_device)
		return -ENODEV;

	request = &stor_device->init_request;
	vstor_packet = &request->vstor_packet;

	/*
	 * Now, initiate the vsc/vsp initialization protocol on the open
	 * channel
	 */
874
	memset(request, 0, sizeof(struct storvsc_cmd_request));
875 876 877 878 879
	init_completion(&request->wait_event);
	vstor_packet->operation = VSTOR_OPERATION_BEGIN_INITIALIZATION;
	vstor_packet->flags = REQUEST_COMPLETION_FLAG;

	ret = vmbus_sendpacket(device->channel, vstor_packet,
880 881
			       (sizeof(struct vstor_packet) -
			       vmscsi_size_delta),
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
			       (unsigned long)request,
			       VM_PKT_DATA_INBAND,
			       VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
	if (ret != 0)
		goto cleanup;

	t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
	if (t == 0) {
		ret = -ETIMEDOUT;
		goto cleanup;
	}

	if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
	    vstor_packet->status != 0)
		goto cleanup;


	/* reuse the packet for version range supported */
	memset(vstor_packet, 0, sizeof(struct vstor_packet));
	vstor_packet->operation = VSTOR_OPERATION_QUERY_PROTOCOL_VERSION;
	vstor_packet->flags = REQUEST_COMPLETION_FLAG;

904
	vstor_packet->version.major_minor =
905
		storvsc_get_version(vmstor_current_major, vmstor_current_minor);
906 907 908 909

	/*
	 * The revision number is only used in Windows; set it to 0.
	 */
910
	vstor_packet->version.revision = 0;
911 912

	ret = vmbus_sendpacket(device->channel, vstor_packet,
913 914
			       (sizeof(struct vstor_packet) -
				vmscsi_size_delta),
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936
			       (unsigned long)request,
			       VM_PKT_DATA_INBAND,
			       VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
	if (ret != 0)
		goto cleanup;

	t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
	if (t == 0) {
		ret = -ETIMEDOUT;
		goto cleanup;
	}

	if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
	    vstor_packet->status != 0)
		goto cleanup;


	memset(vstor_packet, 0, sizeof(struct vstor_packet));
	vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES;
	vstor_packet->flags = REQUEST_COMPLETION_FLAG;

	ret = vmbus_sendpacket(device->channel, vstor_packet,
937 938
			       (sizeof(struct vstor_packet) -
				vmscsi_size_delta),
939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955
			       (unsigned long)request,
			       VM_PKT_DATA_INBAND,
			       VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);

	if (ret != 0)
		goto cleanup;

	t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
	if (t == 0) {
		ret = -ETIMEDOUT;
		goto cleanup;
	}

	if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
	    vstor_packet->status != 0)
		goto cleanup;

956 957 958 959 960 961 962 963 964 965 966 967 968
	/*
	 * Check to see if multi-channel support is there.
	 * Hosts that implement protocol version of 5.1 and above
	 * support multi-channel.
	 */
	max_chns = vstor_packet->storage_channel_properties.max_channel_cnt;
	if ((vmbus_proto_version != VERSION_WIN7) &&
	   (vmbus_proto_version != VERSION_WS2008))  {
		if (vstor_packet->storage_channel_properties.flags &
		    STORAGE_CHANNEL_SUPPORTS_MULTI_CHANNEL)
			process_sub_channels = true;
	}

969 970 971 972 973
	memset(vstor_packet, 0, sizeof(struct vstor_packet));
	vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION;
	vstor_packet->flags = REQUEST_COMPLETION_FLAG;

	ret = vmbus_sendpacket(device->channel, vstor_packet,
974 975
			       (sizeof(struct vstor_packet) -
				vmscsi_size_delta),
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
			       (unsigned long)request,
			       VM_PKT_DATA_INBAND,
			       VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);

	if (ret != 0)
		goto cleanup;

	t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
	if (t == 0) {
		ret = -ETIMEDOUT;
		goto cleanup;
	}

	if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
	    vstor_packet->status != 0)
		goto cleanup;

993 994 995
	if (process_sub_channels)
		handle_multichannel_storage(device, max_chns);

996 997 998 999 1000

cleanup:
	return ret;
}

1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
static void storvsc_handle_error(struct vmscsi_request *vm_srb,
				struct scsi_cmnd *scmnd,
				struct Scsi_Host *host,
				u8 asc, u8 ascq)
{
	struct storvsc_scan_work *wrk;
	void (*process_err_fn)(struct work_struct *work);
	bool do_work = false;

	switch (vm_srb->srb_status) {
	case SRB_STATUS_ERROR:
		/*
		 * If there is an error; offline the device since all
		 * error recovery strategies would have already been
		 * deployed on the host side. However, if the command
		 * were a pass-through command deal with it appropriately.
		 */
		switch (scmnd->cmnd[0]) {
		case ATA_16:
		case ATA_12:
			set_host_byte(scmnd, DID_PASSTHROUGH);
			break;
1023 1024 1025 1026 1027 1028 1029
		/*
		 * On Some Windows hosts TEST_UNIT_READY command can return
		 * SRB_STATUS_ERROR, let the upper level code deal with it
		 * based on the sense information.
		 */
		case TEST_UNIT_READY:
			break;
1030 1031 1032 1033 1034 1035 1036 1037
		default:
			set_host_byte(scmnd, DID_TARGET_FAILURE);
		}
		break;
	case SRB_STATUS_INVALID_LUN:
		do_work = true;
		process_err_fn = storvsc_remove_lun;
		break;
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
	case (SRB_STATUS_ABORTED | SRB_STATUS_AUTOSENSE_VALID):
		if ((asc == 0x2a) && (ascq == 0x9)) {
			do_work = true;
			process_err_fn = storvsc_device_scan;
			/*
			 * Retry the I/O that trigerred this.
			 */
			set_host_byte(scmnd, DID_REQUEUE);
		}
		break;
1048
	}
1049

1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
	if (!do_work)
		return;

	/*
	 * We need to schedule work to process this error; schedule it.
	 */
	wrk = kmalloc(sizeof(struct storvsc_scan_work), GFP_ATOMIC);
	if (!wrk) {
		set_host_byte(scmnd, DID_TARGET_FAILURE);
		return;
	}

	wrk->host = host;
	wrk->lun = vm_srb->lun;
	INIT_WORK(&wrk->work, process_err_fn);
	schedule_work(&wrk->work);
}

1068

1069
static void storvsc_command_completion(struct storvsc_cmd_request *cmd_request)
1070
{
1071 1072 1073 1074 1075 1076
	struct scsi_cmnd *scmnd = cmd_request->cmd;
	struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
	void (*scsi_done_fn)(struct scsi_cmnd *);
	struct scsi_sense_hdr sense_hdr;
	struct vmscsi_request *vm_srb;
	struct stor_mem_pools *memp = scmnd->device->hostdata;
1077 1078 1079 1080 1081 1082
	struct Scsi_Host *host;
	struct storvsc_device *stor_dev;
	struct hv_device *dev = host_dev->dev;

	stor_dev = get_in_stor_device(dev);
	host = stor_dev->host;
1083

1084
	vm_srb = &cmd_request->vstor_packet.vm_srb;
1085 1086 1087 1088 1089 1090 1091 1092 1093
	if (cmd_request->bounce_sgl_count) {
		if (vm_srb->data_in == READ_TYPE)
			copy_from_bounce_buffer(scsi_sglist(scmnd),
					cmd_request->bounce_sgl,
					scsi_sg_count(scmnd),
					cmd_request->bounce_sgl_count);
		destroy_bounce_buffer(cmd_request->bounce_sgl,
					cmd_request->bounce_sgl_count);
	}
1094

1095 1096
	scmnd->result = vm_srb->scsi_status;

1097 1098 1099 1100 1101 1102
	if (scmnd->result) {
		if (scsi_normalize_sense(scmnd->sense_buffer,
				SCSI_SENSE_BUFFERSIZE, &sense_hdr))
			scsi_print_sense_hdr("storvsc", &sense_hdr);
	}

1103 1104 1105 1106
	if (vm_srb->srb_status != SRB_STATUS_SUCCESS)
		storvsc_handle_error(vm_srb, scmnd, host, sense_hdr.asc,
					 sense_hdr.ascq);

1107
	scsi_set_resid(scmnd,
1108
		cmd_request->data_buffer.len -
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
		vm_srb->data_transfer_length);

	scsi_done_fn = scmnd->scsi_done;

	scmnd->host_scribble = NULL;
	scmnd->scsi_done = NULL;

	scsi_done_fn(scmnd);

	mempool_free(cmd_request, memp->request_mempool);
}

static void storvsc_on_io_completion(struct hv_device *device,
				  struct vstor_packet *vstor_packet,
1123
				  struct storvsc_cmd_request *request)
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
{
	struct storvsc_device *stor_device;
	struct vstor_packet *stor_pkt;

	stor_device = hv_get_drvdata(device);
	stor_pkt = &request->vstor_packet;

	/*
	 * The current SCSI handling on the host side does
	 * not correctly handle:
	 * INQUIRY command with page code parameter set to 0x80
	 * MODE_SENSE command with cmd[2] == 0x1c
	 *
	 * Setup srb and scsi status so this won't be fatal.
	 * We do this so we can distinguish truly fatal failues
1139 1140 1141 1142
	 * (srb status == 0x4) and off-line the device in that case.
	 */

	if ((stor_pkt->vm_srb.cdb[0] == INQUIRY) ||
1143
	   (stor_pkt->vm_srb.cdb[0] == MODE_SENSE)) {
1144
		vstor_packet->vm_srb.scsi_status = 0;
1145
		vstor_packet->vm_srb.srb_status = SRB_STATUS_SUCCESS;
1146 1147
	}

1148 1149 1150 1151 1152 1153 1154 1155 1156 1157

	/* Copy over the status...etc */
	stor_pkt->vm_srb.scsi_status = vstor_packet->vm_srb.scsi_status;
	stor_pkt->vm_srb.srb_status = vstor_packet->vm_srb.srb_status;
	stor_pkt->vm_srb.sense_info_length =
	vstor_packet->vm_srb.sense_info_length;


	if ((vstor_packet->vm_srb.scsi_status & 0xFF) == 0x02) {
		/* CHECK_CONDITION */
1158 1159
		if (vstor_packet->vm_srb.srb_status &
			SRB_STATUS_AUTOSENSE_VALID) {
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
			/* autosense data available */

			memcpy(request->sense_buffer,
			       vstor_packet->vm_srb.sense_data,
			       vstor_packet->vm_srb.sense_info_length);

		}
	}

	stor_pkt->vm_srb.data_transfer_length =
	vstor_packet->vm_srb.data_transfer_length;

1172
	storvsc_command_completion(request);
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182

	if (atomic_dec_and_test(&stor_device->num_outstanding_req) &&
		stor_device->drain_notify)
		wake_up(&stor_device->waiting_to_drain);


}

static void storvsc_on_receive(struct hv_device *device,
			     struct vstor_packet *vstor_packet,
1183
			     struct storvsc_cmd_request *request)
1184
{
1185 1186 1187
	struct storvsc_scan_work *work;
	struct storvsc_device *stor_device;

1188 1189 1190 1191
	switch (vstor_packet->operation) {
	case VSTOR_OPERATION_COMPLETE_IO:
		storvsc_on_io_completion(device, vstor_packet, request);
		break;
1192

1193
	case VSTOR_OPERATION_REMOVE_DEVICE:
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
	case VSTOR_OPERATION_ENUMERATE_BUS:
		stor_device = get_in_stor_device(device);
		work = kmalloc(sizeof(struct storvsc_scan_work), GFP_ATOMIC);
		if (!work)
			return;

		INIT_WORK(&work->work, storvsc_bus_scan);
		work->host = stor_device->host;
		schedule_work(&work->work);
		break;
1204 1205 1206 1207 1208 1209 1210 1211

	default:
		break;
	}
}

static void storvsc_on_channel_callback(void *context)
{
1212 1213
	struct vmbus_channel *channel = (struct vmbus_channel *)context;
	struct hv_device *device;
1214 1215 1216 1217
	struct storvsc_device *stor_device;
	u32 bytes_recvd;
	u64 request_id;
	unsigned char packet[ALIGN(sizeof(struct vstor_packet), 8)];
1218
	struct storvsc_cmd_request *request;
1219 1220
	int ret;

1221 1222 1223 1224
	if (channel->primary_channel != NULL)
		device = channel->primary_channel->device_obj;
	else
		device = channel->device_obj;
1225 1226 1227 1228 1229 1230

	stor_device = get_in_stor_device(device);
	if (!stor_device)
		return;

	do {
1231
		ret = vmbus_recvpacket(channel, packet,
1232 1233
				       ALIGN((sizeof(struct vstor_packet) -
					     vmscsi_size_delta), 8),
1234 1235 1236
				       &bytes_recvd, &request_id);
		if (ret == 0 && bytes_recvd > 0) {

1237
			request = (struct storvsc_cmd_request *)
1238 1239 1240 1241 1242 1243
					(unsigned long)request_id;

			if ((request == &stor_device->init_request) ||
			    (request == &stor_device->reset_request)) {

				memcpy(&request->vstor_packet, packet,
1244 1245
				       (sizeof(struct vstor_packet) -
					vmscsi_size_delta));
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
				complete(&request->wait_event);
			} else {
				storvsc_on_receive(device,
						(struct vstor_packet *)packet,
						request);
			}
		} else {
			break;
		}
	} while (1);

	return;
}

static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size)
{
	struct vmstorage_channel_properties props;
	int ret;

	memset(&props, 0, sizeof(struct vmstorage_channel_properties));

	ret = vmbus_open(device->channel,
			 ring_size,
			 ring_size,
			 (void *)&props,
			 sizeof(struct vmstorage_channel_properties),
1272
			 storvsc_on_channel_callback, device->channel);
1273 1274 1275 1276 1277 1278 1279 1280 1281

	if (ret != 0)
		return ret;

	ret = storvsc_channel_init(device);

	return ret;
}

1282
static int storvsc_dev_remove(struct hv_device *device)
1283 1284 1285 1286
{
	struct storvsc_device *stor_device;
	unsigned long flags;

1287
	stor_device = hv_get_drvdata(device);
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308

	spin_lock_irqsave(&device->channel->inbound_lock, flags);
	stor_device->destroy = true;
	spin_unlock_irqrestore(&device->channel->inbound_lock, flags);

	/*
	 * At this point, all outbound traffic should be disable. We
	 * only allow inbound traffic (responses) to proceed so that
	 * outstanding requests can be completed.
	 */

	storvsc_wait_to_drain(stor_device);

	/*
	 * Since we have already drained, we don't need to busy wait
	 * as was done in final_release_stor_device()
	 * Note that we cannot set the ext pointer to NULL until
	 * we have drained - to drain the outgoing packets, we need to
	 * allow incoming packets.
	 */
	spin_lock_irqsave(&device->channel->inbound_lock, flags);
1309
	hv_set_drvdata(device, NULL);
1310 1311 1312 1313 1314 1315 1316 1317 1318
	spin_unlock_irqrestore(&device->channel->inbound_lock, flags);

	/* Close the channel */
	vmbus_close(device->channel);

	kfree(stor_device);
	return 0;
}

1319
static int storvsc_do_io(struct hv_device *device,
1320
			      struct storvsc_cmd_request *request)
1321 1322 1323
{
	struct storvsc_device *stor_device;
	struct vstor_packet *vstor_packet;
1324
	struct vmbus_channel *outgoing_channel;
1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
	int ret = 0;

	vstor_packet = &request->vstor_packet;
	stor_device = get_out_stor_device(device);

	if (!stor_device)
		return -ENODEV;


	request->device  = device;
1335 1336 1337 1338 1339
	/*
	 * Select an an appropriate channel to send the request out.
	 */

	outgoing_channel = vmbus_get_outgoing_channel(device->channel);
1340 1341 1342 1343


	vstor_packet->flags |= REQUEST_COMPLETION_FLAG;

1344 1345
	vstor_packet->vm_srb.length = (sizeof(struct vmscsi_request) -
					vmscsi_size_delta);
1346 1347


1348
	vstor_packet->vm_srb.sense_info_length = sense_buffer_size;
1349 1350 1351 1352 1353 1354 1355 1356


	vstor_packet->vm_srb.data_transfer_length =
	request->data_buffer.len;

	vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;

	if (request->data_buffer.len) {
1357
		ret = vmbus_sendpacket_multipagebuffer(outgoing_channel,
1358 1359
				&request->data_buffer,
				vstor_packet,
1360 1361
				(sizeof(struct vstor_packet) -
				vmscsi_size_delta),
1362 1363 1364
				(unsigned long)request);
	} else {
		ret = vmbus_sendpacket(device->channel, vstor_packet,
1365 1366
			       (sizeof(struct vstor_packet) -
				vmscsi_size_delta),
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
			       (unsigned long)request,
			       VM_PKT_DATA_INBAND,
			       VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
	}

	if (ret != 0)
		return ret;

	atomic_inc(&stor_device->num_outstanding_req);

	return ret;
}

1380 1381
static int storvsc_device_alloc(struct scsi_device *sdevice)
{
1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405
	struct stor_mem_pools *memp;
	int number = STORVSC_MIN_BUF_NR;

	memp = kzalloc(sizeof(struct stor_mem_pools), GFP_KERNEL);
	if (!memp)
		return -ENOMEM;

	memp->request_pool =
		kmem_cache_create(dev_name(&sdevice->sdev_dev),
				sizeof(struct storvsc_cmd_request), 0,
				SLAB_HWCACHE_ALIGN, NULL);

	if (!memp->request_pool)
		goto err0;

	memp->request_mempool = mempool_create(number, mempool_alloc_slab,
						mempool_free_slab,
						memp->request_pool);

	if (!memp->request_mempool)
		goto err1;

	sdevice->hostdata = memp;

1406
	return 0;
1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419

err1:
	kmem_cache_destroy(memp->request_pool);

err0:
	kfree(memp);
	return -ENOMEM;
}

static void storvsc_device_destroy(struct scsi_device *sdevice)
{
	struct stor_mem_pools *memp = sdevice->hostdata;

1420 1421 1422
	if (!memp)
		return;

1423 1424 1425 1426
	mempool_destroy(memp->request_mempool);
	kmem_cache_destroy(memp->request_pool);
	kfree(memp);
	sdevice->hostdata = NULL;
1427 1428
}

1429 1430 1431 1432 1433 1434 1435 1436 1437
static int storvsc_device_configure(struct scsi_device *sdevice)
{
	scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG,
				STORVSC_MAX_IO_REQUESTS);

	blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);

	blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);

1438 1439
	blk_queue_rq_timeout(sdevice->request_queue, (storvsc_timeout * HZ));

1440 1441
	sdevice->no_write_same = 1;

K
K. Y. Srinivasan 已提交
1442 1443 1444 1445 1446 1447 1448 1449
	/*
	 * Add blist flags to permit the reading of the VPD pages even when
	 * the target may claim SPC-2 compliance. MSFT targets currently
	 * claim SPC-2 compliance while they implement post SPC-2 features.
	 * With this patch we can correctly handle WRITE_SAME_16 issues.
	 */
	sdevice->sdev_bflags |= msft_blist_flags;

1450 1451 1452
	return 0;
}

1453 1454 1455
static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
			   sector_t capacity, int *info)
{
1456 1457 1458
	sector_t nsect = capacity;
	sector_t cylinders = nsect;
	int heads, sectors_pt;
1459

1460 1461 1462 1463 1464 1465 1466 1467
	/*
	 * We are making up these values; let us keep it simple.
	 */
	heads = 0xff;
	sectors_pt = 0x3f;      /* Sectors per track */
	sector_div(cylinders, heads * sectors_pt);
	if ((sector_t)(cylinders + 1) * heads * sectors_pt < nsect)
		cylinders = 0xffff;
1468 1469

	info[0] = heads;
1470 1471
	info[1] = sectors_pt;
	info[2] = (int)cylinders;
1472 1473 1474

	return 0;
}
1475

1476
static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
1477
{
1478 1479 1480
	struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
	struct hv_device *device = host_dev->dev;

1481
	struct storvsc_device *stor_device;
1482
	struct storvsc_cmd_request *request;
1483 1484 1485 1486
	struct vstor_packet *vstor_packet;
	int ret, t;


1487
	stor_device = get_out_stor_device(device);
1488
	if (!stor_device)
1489
		return FAILED;
1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500

	request = &stor_device->reset_request;
	vstor_packet = &request->vstor_packet;

	init_completion(&request->wait_event);

	vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
	vstor_packet->flags = REQUEST_COMPLETION_FLAG;
	vstor_packet->vm_srb.path_id = stor_device->path_id;

	ret = vmbus_sendpacket(device->channel, vstor_packet,
1501 1502
			       (sizeof(struct vstor_packet) -
				vmscsi_size_delta),
1503 1504 1505 1506
			       (unsigned long)&stor_device->reset_request,
			       VM_PKT_DATA_INBAND,
			       VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
	if (ret != 0)
1507
		return FAILED;
1508

1509
	t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
1510 1511
	if (t == 0)
		return TIMEOUT_ERROR;
1512 1513 1514 1515 1516


	/*
	 * At this point, all outstanding requests in the adapter
	 * should have been flushed out and return to us
1517 1518 1519 1520
	 * There is a potential race here where the host may be in
	 * the process of responding when we return from here.
	 * Just wait for all in-transit packets to be accounted for
	 * before we return from here.
1521
	 */
1522
	storvsc_wait_to_drain(stor_device);
1523

1524
	return SUCCESS;
1525 1526
}

1527 1528 1529 1530 1531 1532 1533 1534 1535 1536
/*
 * The host guarantees to respond to each command, although I/O latencies might
 * be unbounded on Azure.  Reset the timer unconditionally to give the host a
 * chance to perform EH.
 */
static enum blk_eh_timer_return storvsc_eh_timed_out(struct scsi_cmnd *scmnd)
{
	return BLK_EH_RESET_TIMER;
}

1537
static bool storvsc_scsi_cmd_ok(struct scsi_cmnd *scmnd)
1538 1539 1540 1541 1542
{
	bool allowed = true;
	u8 scsi_op = scmnd->cmnd[0];

	switch (scsi_op) {
1543 1544
	/* the host does not handle WRITE_SAME, log accident usage */
	case WRITE_SAME:
1545 1546 1547 1548
	/*
	 * smartd sends this command and the host does not handle
	 * this. So, don't send it.
	 */
1549
	case SET_WINDOW:
1550
		scmnd->result = ILLEGAL_REQUEST << 16;
1551 1552 1553 1554
		allowed = false;
		break;
	default:
		break;
1555 1556 1557
	}
	return allowed;
}
1558

1559
static int storvsc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scmnd)
1560 1561
{
	int ret;
1562
	struct hv_host_device *host_dev = shost_priv(host);
1563 1564 1565 1566 1567 1568 1569
	struct hv_device *dev = host_dev->dev;
	struct storvsc_cmd_request *cmd_request;
	unsigned int request_size = 0;
	int i;
	struct scatterlist *sgl;
	unsigned int sg_count = 0;
	struct vmscsi_request *vm_srb;
1570
	struct stor_mem_pools *memp = scmnd->device->hostdata;
1571

1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584
	if (vmstor_current_major <= VMSTOR_WIN8_MAJOR) {
		/*
		 * On legacy hosts filter unimplemented commands.
		 * Future hosts are expected to correctly handle
		 * unsupported commands. Furthermore, it is
		 * possible that some of the currently
		 * unsupported commands maybe supported in
		 * future versions of the host.
		 */
		if (!storvsc_scsi_cmd_ok(scmnd)) {
			scmnd->scsi_done(scmnd);
			return 0;
		}
1585
	}
1586 1587 1588

	request_size = sizeof(struct storvsc_cmd_request);

1589
	cmd_request = mempool_alloc(memp->request_mempool,
1590
				       GFP_ATOMIC);
1591 1592 1593 1594 1595

	/*
	 * We might be invoked in an interrupt context; hence
	 * mempool_alloc() can fail.
	 */
1596
	if (!cmd_request)
1597
		return SCSI_MLQUEUE_DEVICE_BUSY;
1598

1599
	memset(cmd_request, 0, sizeof(struct storvsc_cmd_request));
1600 1601 1602 1603 1604 1605

	/* Setup the cmd request */
	cmd_request->cmd = scmnd;

	scmnd->host_scribble = (unsigned char *)cmd_request;

1606
	vm_srb = &cmd_request->vstor_packet.vm_srb;
1607
	vm_srb->win8_extension.time_out_value = 60;
1608

1609 1610 1611
	vm_srb->win8_extension.srb_flags |=
		(SRB_FLAGS_QUEUE_ACTION_ENABLE |
		SRB_FLAGS_DISABLE_SYNCH_TRANSFER);
1612 1613 1614 1615 1616

	/* Build the SRB */
	switch (scmnd->sc_data_direction) {
	case DMA_TO_DEVICE:
		vm_srb->data_in = WRITE_TYPE;
1617
		vm_srb->win8_extension.srb_flags |= SRB_FLAGS_DATA_OUT;
1618 1619 1620
		break;
	case DMA_FROM_DEVICE:
		vm_srb->data_in = READ_TYPE;
1621
		vm_srb->win8_extension.srb_flags |= SRB_FLAGS_DATA_IN;
1622 1623 1624
		break;
	default:
		vm_srb->data_in = UNKNOWN_TYPE;
1625 1626
		vm_srb->win8_extension.srb_flags |= (SRB_FLAGS_DATA_IN |
						     SRB_FLAGS_DATA_OUT);
1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639
		break;
	}


	vm_srb->port_number = host_dev->port;
	vm_srb->path_id = scmnd->device->channel;
	vm_srb->target_id = scmnd->device->id;
	vm_srb->lun = scmnd->device->lun;

	vm_srb->cdb_length = scmnd->cmd_len;

	memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);

1640
	cmd_request->sense_buffer = scmnd->sense_buffer;
1641 1642


1643
	cmd_request->data_buffer.len = scsi_bufflen(scmnd);
1644 1645 1646 1647 1648 1649 1650 1651
	if (scsi_sg_count(scmnd)) {
		sgl = (struct scatterlist *)scsi_sglist(scmnd);
		sg_count = scsi_sg_count(scmnd);

		/* check if we need to bounce the sgl */
		if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) {
			cmd_request->bounce_sgl =
				create_bounce_buffer(sgl, scsi_sg_count(scmnd),
1652 1653
						     scsi_bufflen(scmnd),
						     vm_srb->data_in);
1654
			if (!cmd_request->bounce_sgl) {
1655 1656
				ret = SCSI_MLQUEUE_HOST_BUSY;
				goto queue_error;
1657 1658 1659 1660 1661 1662
			}

			cmd_request->bounce_sgl_count =
				ALIGN(scsi_bufflen(scmnd), PAGE_SIZE) >>
					PAGE_SHIFT;

1663 1664 1665 1666
			if (vm_srb->data_in == WRITE_TYPE)
				copy_to_bounce_buffer(sgl,
					cmd_request->bounce_sgl,
					scsi_sg_count(scmnd));
1667 1668 1669 1670 1671

			sgl = cmd_request->bounce_sgl;
			sg_count = cmd_request->bounce_sgl_count;
		}

1672
		cmd_request->data_buffer.offset = sgl[0].offset;
1673 1674

		for (i = 0; i < sg_count; i++)
1675
			cmd_request->data_buffer.pfn_array[i] =
1676 1677 1678
				page_to_pfn(sg_page((&sgl[i])));

	} else if (scsi_sglist(scmnd)) {
1679
		cmd_request->data_buffer.offset =
1680
			virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
1681
		cmd_request->data_buffer.pfn_array[0] =
1682 1683 1684 1685
			virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
	}

	/* Invokes the vsc to start an IO */
1686
	ret = storvsc_do_io(dev, cmd_request);
1687

1688
	if (ret == -EAGAIN) {
1689 1690
		/* no more space */

1691
		if (cmd_request->bounce_sgl_count) {
1692
			destroy_bounce_buffer(cmd_request->bounce_sgl,
1693
					cmd_request->bounce_sgl_count);
1694

1695 1696 1697
			ret = SCSI_MLQUEUE_DEVICE_BUSY;
			goto queue_error;
		}
1698 1699
	}

1700 1701 1702 1703 1704
	return 0;

queue_error:
	mempool_free(cmd_request, memp->request_mempool);
	scmnd->host_scribble = NULL;
1705 1706 1707
	return ret;
}

1708
static struct scsi_host_template scsi_driver = {
1709 1710 1711 1712 1713
	.module	=		THIS_MODULE,
	.name =			"storvsc_host_t",
	.bios_param =		storvsc_get_chs,
	.queuecommand =		storvsc_queuecommand,
	.eh_host_reset_handler =	storvsc_host_reset_handler,
1714
	.eh_timed_out =		storvsc_eh_timed_out,
1715
	.slave_alloc =		storvsc_device_alloc,
1716
	.slave_destroy =	storvsc_device_destroy,
1717
	.slave_configure =	storvsc_device_configure,
1718
	.cmd_per_lun =		255,
1719
	.can_queue =		STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS,
1720
	.this_id =		-1,
1721
	/* no use setting to 0 since ll_blk_rw reset it to 1 */
1722 1723
	/* currently 32 */
	.sg_tablesize =		MAX_MULTIPAGE_BUFFER_COUNT,
1724
	.use_clustering =	DISABLE_CLUSTERING,
1725
	/* Make sure we dont get a sg segment crosses a page boundary */
1726
	.dma_boundary =		PAGE_SIZE-1,
1727
	.no_write_same =	1,
1728 1729
};

1730 1731 1732
enum {
	SCSI_GUID,
	IDE_GUID,
1733
	SFC_GUID,
1734 1735
};

1736
static const struct hv_vmbus_device_id id_table[] = {
1737
	/* SCSI guid */
1738 1739 1740
	{ HV_SCSI_GUID,
	  .driver_data = SCSI_GUID
	},
1741
	/* IDE guid */
1742 1743 1744
	{ HV_IDE_GUID,
	  .driver_data = IDE_GUID
	},
1745 1746 1747 1748 1749
	/* Fibre Channel GUID */
	{
	  HV_SYNTHFC_GUID,
	  .driver_data = SFC_GUID
	},
1750
	{ },
1751
};
1752

1753
MODULE_DEVICE_TABLE(vmbus, id_table);
1754

1755 1756
static int storvsc_probe(struct hv_device *device,
			const struct hv_vmbus_device_id *dev_id)
1757
{
1758
	int ret;
1759
	struct Scsi_Host *host;
1760
	struct hv_host_device *host_dev;
1761
	bool dev_is_ide = ((dev_id->driver_data == IDE_GUID) ? true : false);
1762
	int target = 0;
1763
	struct storvsc_device *stor_device;
1764

1765 1766 1767 1768 1769
	/*
	 * Based on the windows host we are running on,
	 * set state to properly communicate with the host.
	 */

1770 1771 1772
	switch (vmbus_proto_version) {
	case VERSION_WS2008:
	case VERSION_WIN7:
1773 1774 1775 1776
		sense_buffer_size = PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE;
		vmscsi_size_delta = sizeof(struct vmscsi_win8_extension);
		vmstor_current_major = VMSTOR_WIN7_MAJOR;
		vmstor_current_minor = VMSTOR_WIN7_MINOR;
1777 1778 1779 1780 1781 1782 1783
		break;
	default:
		sense_buffer_size = POST_WIN7_STORVSC_SENSE_BUFFER_SIZE;
		vmscsi_size_delta = 0;
		vmstor_current_major = VMSTOR_WIN8_MAJOR;
		vmstor_current_minor = VMSTOR_WIN8_MINOR;
		break;
1784 1785
	}

1786 1787 1788
	if (dev_id->driver_data == SFC_GUID)
		scsi_driver.can_queue = (STORVSC_MAX_IO_REQUESTS *
					 STORVSC_FC_MAX_TARGETS);
1789
	host = scsi_host_alloc(&scsi_driver,
1790
			       sizeof(struct hv_host_device));
1791
	if (!host)
1792 1793
		return -ENOMEM;

1794
	host_dev = shost_priv(host);
1795
	memset(host_dev, 0, sizeof(struct hv_host_device));
1796

1797
	host_dev->port = host->host_no;
1798
	host_dev->dev = device;
1799

1800

1801
	stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL);
1802
	if (!stor_device) {
1803
		ret = -ENOMEM;
1804
		goto err_out0;
1805
	}
1806

1807
	stor_device->destroy = false;
1808
	stor_device->open_sub_channel = false;
1809 1810
	init_waitqueue_head(&stor_device->waiting_to_drain);
	stor_device->device = device;
1811 1812
	stor_device->host = host;
	hv_set_drvdata(device, stor_device);
1813

1814 1815
	stor_device->port_number = host->host_no;
	ret = storvsc_connect_to_vsp(device, storvsc_ringbuffer_size);
1816
	if (ret)
1817
		goto err_out1;
1818

1819 1820
	host_dev->path = stor_device->path_id;
	host_dev->target = stor_device->target_id;
1821

1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840
	switch (dev_id->driver_data) {
	case SFC_GUID:
		host->max_lun = STORVSC_FC_MAX_LUNS_PER_TARGET;
		host->max_id = STORVSC_FC_MAX_TARGETS;
		host->max_channel = STORVSC_FC_MAX_CHANNELS - 1;
		break;

	case SCSI_GUID:
		host->max_lun = STORVSC_MAX_LUNS_PER_TARGET;
		host->max_id = STORVSC_MAX_TARGETS;
		host->max_channel = STORVSC_MAX_CHANNELS - 1;
		break;

	default:
		host->max_lun = STORVSC_IDE_MAX_LUNS_PER_TARGET;
		host->max_id = STORVSC_IDE_MAX_TARGETS;
		host->max_channel = STORVSC_IDE_MAX_CHANNELS - 1;
		break;
	}
1841 1842
	/* max cmd length */
	host->max_cmd_len = STORVSC_MAX_CMD_LEN;
1843

1844
	/* Register the HBA and start the scsi bus scan */
1845
	ret = scsi_add_host(host, &device->device);
1846
	if (ret != 0)
1847
		goto err_out2;
1848

1849 1850
	if (!dev_is_ide) {
		scsi_scan_host(host);
1851 1852 1853 1854 1855 1856 1857 1858
	} else {
		target = (device->dev_instance.b[5] << 8 |
			 device->dev_instance.b[4]);
		ret = scsi_add_device(host, 0, target, 0);
		if (ret) {
			scsi_remove_host(host);
			goto err_out2;
		}
1859
	}
1860
	return 0;
1861

1862
err_out2:
1863 1864 1865 1866
	/*
	 * Once we have connected with the host, we would need to
	 * to invoke storvsc_dev_remove() to rollback this state and
	 * this call also frees up the stor_device; hence the jump around
1867
	 * err_out1 label.
1868
	 */
1869
	storvsc_dev_remove(device);
1870
	goto err_out0;
1871 1872

err_out1:
1873
	kfree(stor_device);
1874 1875

err_out0:
1876
	scsi_host_put(host);
1877
	return ret;
1878 1879
}

1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891
static int storvsc_remove(struct hv_device *dev)
{
	struct storvsc_device *stor_device = hv_get_drvdata(dev);
	struct Scsi_Host *host = stor_device->host;

	scsi_remove_host(host);
	storvsc_dev_remove(dev);
	scsi_host_put(host);

	return 0;
}

1892
static struct hv_driver storvsc_drv = {
1893
	.name = KBUILD_MODNAME,
1894
	.id_table = id_table,
1895 1896
	.probe = storvsc_probe,
	.remove = storvsc_remove,
1897
};
1898

1899
static int __init storvsc_drv_init(void)
1900
{
1901 1902 1903 1904 1905 1906 1907 1908 1909
	u32 max_outstanding_req_per_channel;

	/*
	 * Divide the ring buffer data size (which is 1 page less
	 * than the ring buffer size since that page is reserved for
	 * the ring buffer indices) by the max request size (which is
	 * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
	 */
	max_outstanding_req_per_channel =
1910 1911
		((storvsc_ringbuffer_size - PAGE_SIZE) /
		ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
1912 1913
		sizeof(struct vstor_packet) + sizeof(u64) -
		vmscsi_size_delta,
1914
		sizeof(u64)));
1915

1916
	if (max_outstanding_req_per_channel <
1917
	    STORVSC_MAX_IO_REQUESTS)
1918
		return -EINVAL;
1919

1920
	return vmbus_driver_register(&storvsc_drv);
1921 1922
}

1923
static void __exit storvsc_drv_exit(void)
1924
{
1925
	vmbus_driver_unregister(&storvsc_drv);
1926 1927
}

1928
MODULE_LICENSE("GPL");
1929
MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
1930
module_init(storvsc_drv_init);
1931
module_exit(storvsc_drv_exit);