hv_storvsc_drv_freebsd.c revision 306223
1/*-
2 * Copyright (c) 2009-2012,2016 Microsoft Corp.
3 * Copyright (c) 2012 NetApp Inc.
4 * Copyright (c) 2012 Citrix Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice unmodified, this list of conditions, and the following
12 *    disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/**
30 * StorVSC driver for Hyper-V.  This driver presents a SCSI HBA interface
31 * to the Comman Access Method (CAM) layer.  CAM control blocks (CCBs) are
32 * converted into VSCSI protocol messages which are delivered to the parent
33 * partition StorVSP driver over the Hyper-V VMBUS.
34 */
35#include <sys/cdefs.h>
36__FBSDID("$FreeBSD: stable/11/sys/dev/hyperv/storvsc/hv_storvsc_drv_freebsd.c 306223 2016-09-23 03:21:40Z sephe $");
37
38#include <sys/param.h>
39#include <sys/proc.h>
40#include <sys/condvar.h>
41#include <sys/time.h>
42#include <sys/systm.h>
43#include <sys/sockio.h>
44#include <sys/mbuf.h>
45#include <sys/malloc.h>
46#include <sys/module.h>
47#include <sys/kernel.h>
48#include <sys/queue.h>
49#include <sys/lock.h>
50#include <sys/sx.h>
51#include <sys/taskqueue.h>
52#include <sys/bus.h>
53#include <sys/mutex.h>
54#include <sys/callout.h>
55#include <vm/vm.h>
56#include <vm/pmap.h>
57#include <vm/uma.h>
58#include <sys/lock.h>
59#include <sys/sema.h>
60#include <sys/sglist.h>
61#include <machine/bus.h>
62#include <sys/bus_dma.h>
63
64#include <cam/cam.h>
65#include <cam/cam_ccb.h>
66#include <cam/cam_periph.h>
67#include <cam/cam_sim.h>
68#include <cam/cam_xpt_sim.h>
69#include <cam/cam_xpt_internal.h>
70#include <cam/cam_debug.h>
71#include <cam/scsi/scsi_all.h>
72#include <cam/scsi/scsi_message.h>
73
74#include <dev/hyperv/include/hyperv.h>
75#include "hv_vstorage.h"
76
77#define STORVSC_RINGBUFFER_SIZE		(20*PAGE_SIZE)
78#define STORVSC_MAX_LUNS_PER_TARGET	(64)
79#define STORVSC_MAX_IO_REQUESTS		(STORVSC_MAX_LUNS_PER_TARGET * 2)
80#define BLKVSC_MAX_IDE_DISKS_PER_TARGET	(1)
81#define BLKVSC_MAX_IO_REQUESTS		STORVSC_MAX_IO_REQUESTS
82#define STORVSC_MAX_TARGETS		(2)
83
84#define VSTOR_PKT_SIZE	(sizeof(struct vstor_packet) - vmscsi_size_delta)
85
86#define HV_ALIGN(x, a) roundup2(x, a)
87
88struct storvsc_softc;
89
90struct hv_sgl_node {
91	LIST_ENTRY(hv_sgl_node) link;
92	struct sglist *sgl_data;
93};
94
95struct hv_sgl_page_pool{
96	LIST_HEAD(, hv_sgl_node) in_use_sgl_list;
97	LIST_HEAD(, hv_sgl_node) free_sgl_list;
98	boolean_t                is_init;
99} g_hv_sgl_page_pool;
100
101#define STORVSC_MAX_SG_PAGE_CNT STORVSC_MAX_IO_REQUESTS * HV_MAX_MULTIPAGE_BUFFER_COUNT
102
103enum storvsc_request_type {
104	WRITE_TYPE,
105	READ_TYPE,
106	UNKNOWN_TYPE
107};
108
109struct hv_storvsc_request {
110	LIST_ENTRY(hv_storvsc_request) link;
111	struct vstor_packet	vstor_packet;
112	hv_vmbus_multipage_buffer data_buf;
113	void *sense_data;
114	uint8_t sense_info_len;
115	uint8_t retries;
116	union ccb *ccb;
117	struct storvsc_softc *softc;
118	struct callout callout;
119	struct sema synch_sema; /*Synchronize the request/response if needed */
120	struct sglist *bounce_sgl;
121	unsigned int bounce_sgl_count;
122	uint64_t not_aligned_seg_bits;
123};
124
125struct storvsc_softc {
126	struct hv_device		*hs_dev;
127	LIST_HEAD(, hv_storvsc_request)	hs_free_list;
128	struct mtx			hs_lock;
129	struct storvsc_driver_props	*hs_drv_props;
130	int 				hs_unit;
131	uint32_t			hs_frozen;
132	struct cam_sim			*hs_sim;
133	struct cam_path 		*hs_path;
134	uint32_t			hs_num_out_reqs;
135	boolean_t			hs_destroy;
136	boolean_t			hs_drain_notify;
137	struct sema 			hs_drain_sema;
138	struct hv_storvsc_request	hs_init_req;
139	struct hv_storvsc_request	hs_reset_req;
140};
141
142
143/**
144 * HyperV storvsc timeout testing cases:
145 * a. IO returned after first timeout;
146 * b. IO returned after second timeout and queue freeze;
147 * c. IO returned while timer handler is running
148 * The first can be tested by "sg_senddiag -vv /dev/daX",
149 * and the second and third can be done by
150 * "sg_wr_mode -v -p 08 -c 0,1a -m 0,ff /dev/daX".
151 */
152#define HVS_TIMEOUT_TEST 0
153
154/*
155 * Bus/adapter reset functionality on the Hyper-V host is
156 * buggy and it will be disabled until
157 * it can be further tested.
158 */
159#define HVS_HOST_RESET 0
160
161struct storvsc_driver_props {
162	char		*drv_name;
163	char		*drv_desc;
164	uint8_t		drv_max_luns_per_target;
165	uint8_t		drv_max_ios_per_target;
166	uint32_t	drv_ringbuffer_size;
167};
168
169enum hv_storage_type {
170	DRIVER_BLKVSC,
171	DRIVER_STORVSC,
172	DRIVER_UNKNOWN
173};
174
175#define HS_MAX_ADAPTERS 10
176
177#define HV_STORAGE_SUPPORTS_MULTI_CHANNEL 0x1
178
179/* {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f} */
180static const hv_guid gStorVscDeviceType={
181	.data = {0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
182		 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f}
183};
184
185/* {32412632-86cb-44a2-9b5c-50d1417354f5} */
186static const hv_guid gBlkVscDeviceType={
187	.data = {0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44,
188		 0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5}
189};
190
191static struct storvsc_driver_props g_drv_props_table[] = {
192	{"blkvsc", "Hyper-V IDE Storage Interface",
193	 BLKVSC_MAX_IDE_DISKS_PER_TARGET, BLKVSC_MAX_IO_REQUESTS,
194	 STORVSC_RINGBUFFER_SIZE},
195	{"storvsc", "Hyper-V SCSI Storage Interface",
196	 STORVSC_MAX_LUNS_PER_TARGET, STORVSC_MAX_IO_REQUESTS,
197	 STORVSC_RINGBUFFER_SIZE}
198};
199
200/*
201 * Sense buffer size changed in win8; have a run-time
202 * variable to track the size we should use.
203 */
204static int sense_buffer_size = PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE;
205
206/*
207 * The size of the vmscsi_request has changed in win8. The
208 * additional size is for the newly added elements in the
209 * structure. These elements are valid only when we are talking
210 * to a win8 host.
211 * Track the correct size we need to apply.
212 */
213static int vmscsi_size_delta;
214/*
215 * The storage protocol version is determined during the
216 * initial exchange with the host.  It will indicate which
217 * storage functionality is available in the host.
218*/
219static int vmstor_proto_version;
220
221struct vmstor_proto {
222        int proto_version;
223        int sense_buffer_size;
224        int vmscsi_size_delta;
225};
226
227static const struct vmstor_proto vmstor_proto_list[] = {
228        {
229                VMSTOR_PROTOCOL_VERSION_WIN10,
230                POST_WIN7_STORVSC_SENSE_BUFFER_SIZE,
231                0
232        },
233        {
234                VMSTOR_PROTOCOL_VERSION_WIN8_1,
235                POST_WIN7_STORVSC_SENSE_BUFFER_SIZE,
236                0
237        },
238        {
239                VMSTOR_PROTOCOL_VERSION_WIN8,
240                POST_WIN7_STORVSC_SENSE_BUFFER_SIZE,
241                0
242        },
243        {
244                VMSTOR_PROTOCOL_VERSION_WIN7,
245                PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE,
246                sizeof(struct vmscsi_win8_extension),
247        },
248        {
249                VMSTOR_PROTOCOL_VERSION_WIN6,
250                PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE,
251                sizeof(struct vmscsi_win8_extension),
252        }
253};
254
255/* static functions */
256static int storvsc_probe(device_t dev);
257static int storvsc_attach(device_t dev);
258static int storvsc_detach(device_t dev);
259static void storvsc_poll(struct cam_sim * sim);
260static void storvsc_action(struct cam_sim * sim, union ccb * ccb);
261static int create_storvsc_request(union ccb *ccb, struct hv_storvsc_request *reqp);
262static void storvsc_free_request(struct storvsc_softc *sc, struct hv_storvsc_request *reqp);
263static enum hv_storage_type storvsc_get_storage_type(device_t dev);
264static void hv_storvsc_rescan_target(struct storvsc_softc *sc);
265static void hv_storvsc_on_channel_callback(void *context);
266static void hv_storvsc_on_iocompletion( struct storvsc_softc *sc,
267					struct vstor_packet *vstor_packet,
268					struct hv_storvsc_request *request);
269static int hv_storvsc_connect_vsp(struct hv_device *device);
270static void storvsc_io_done(struct hv_storvsc_request *reqp);
271static void storvsc_copy_sgl_to_bounce_buf(struct sglist *bounce_sgl,
272				bus_dma_segment_t *orig_sgl,
273				unsigned int orig_sgl_count,
274				uint64_t seg_bits);
275void storvsc_copy_from_bounce_buf_to_sgl(bus_dma_segment_t *dest_sgl,
276				unsigned int dest_sgl_count,
277				struct sglist* src_sgl,
278				uint64_t seg_bits);
279
280static device_method_t storvsc_methods[] = {
281	/* Device interface */
282	DEVMETHOD(device_probe,		storvsc_probe),
283	DEVMETHOD(device_attach,	storvsc_attach),
284	DEVMETHOD(device_detach,	storvsc_detach),
285	DEVMETHOD(device_shutdown,      bus_generic_shutdown),
286	DEVMETHOD_END
287};
288
289static driver_t storvsc_driver = {
290	"storvsc", storvsc_methods, sizeof(struct storvsc_softc),
291};
292
293static devclass_t storvsc_devclass;
294DRIVER_MODULE(storvsc, vmbus, storvsc_driver, storvsc_devclass, 0, 0);
295MODULE_VERSION(storvsc, 1);
296MODULE_DEPEND(storvsc, vmbus, 1, 1, 1);
297
298
299/**
300 * The host is capable of sending messages to us that are
301 * completely unsolicited. So, we need to address the race
302 * condition where we may be in the process of unloading the
303 * driver when the host may send us an unsolicited message.
304 * We address this issue by implementing a sequentially
305 * consistent protocol:
306 *
307 * 1. Channel callback is invoked while holding the channel lock
308 *    and an unloading driver will reset the channel callback under
309 *    the protection of this channel lock.
310 *
311 * 2. To ensure bounded wait time for unloading a driver, we don't
312 *    permit outgoing traffic once the device is marked as being
313 *    destroyed.
314 *
315 * 3. Once the device is marked as being destroyed, we only
316 *    permit incoming traffic to properly account for
317 *    packets already sent out.
318 */
319static inline struct storvsc_softc *
320get_stor_device(struct hv_device *device,
321				boolean_t outbound)
322{
323	struct storvsc_softc *sc;
324
325	sc = device_get_softc(device->device);
326
327	if (outbound) {
328		/*
329		 * Here we permit outgoing I/O only
330		 * if the device is not being destroyed.
331		 */
332
333		if (sc->hs_destroy) {
334			sc = NULL;
335		}
336	} else {
337		/*
338		 * inbound case; if being destroyed
339		 * only permit to account for
340		 * messages already sent out.
341		 */
342		if (sc->hs_destroy && (sc->hs_num_out_reqs == 0)) {
343			sc = NULL;
344		}
345	}
346	return sc;
347}
348
349static void
350storvsc_subchan_attach(struct hv_vmbus_channel *new_channel)
351{
352	struct hv_device *device;
353	struct storvsc_softc *sc;
354	struct vmstor_chan_props props;
355	int ret = 0;
356
357	device = new_channel->device;
358	sc = get_stor_device(device, TRUE);
359	if (sc == NULL)
360		return;
361
362	memset(&props, 0, sizeof(props));
363
364	ret = hv_vmbus_channel_open(new_channel,
365	    sc->hs_drv_props->drv_ringbuffer_size,
366  	    sc->hs_drv_props->drv_ringbuffer_size,
367	    (void *)&props,
368	    sizeof(struct vmstor_chan_props),
369	    hv_storvsc_on_channel_callback,
370	    new_channel);
371
372	return;
373}
374
375/**
376 * @brief Send multi-channel creation request to host
377 *
378 * @param device  a Hyper-V device pointer
379 * @param max_chans  the max channels supported by vmbus
380 */
381static void
382storvsc_send_multichannel_request(struct hv_device *dev, int max_chans)
383{
384	struct hv_vmbus_channel **subchan;
385	struct storvsc_softc *sc;
386	struct hv_storvsc_request *request;
387	struct vstor_packet *vstor_packet;
388	int request_channels_cnt = 0;
389	int ret, i;
390
391	/* get multichannels count that need to create */
392	request_channels_cnt = MIN(max_chans, mp_ncpus);
393
394	sc = get_stor_device(dev, TRUE);
395	if (sc == NULL) {
396		printf("Storvsc_error: get sc failed while send mutilchannel "
397		    "request\n");
398		return;
399	}
400
401	request = &sc->hs_init_req;
402
403	/* request the host to create multi-channel */
404	memset(request, 0, sizeof(struct hv_storvsc_request));
405
406	sema_init(&request->synch_sema, 0, ("stor_synch_sema"));
407
408	vstor_packet = &request->vstor_packet;
409
410	vstor_packet->operation = VSTOR_OPERATION_CREATE_MULTI_CHANNELS;
411	vstor_packet->flags = REQUEST_COMPLETION_FLAG;
412	vstor_packet->u.multi_channels_cnt = request_channels_cnt;
413
414	ret = hv_vmbus_channel_send_packet(
415	    dev->channel,
416	    vstor_packet,
417	    VSTOR_PKT_SIZE,
418	    (uint64_t)(uintptr_t)request,
419	    HV_VMBUS_PACKET_TYPE_DATA_IN_BAND,
420	    HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
421
422	/* wait for 5 seconds */
423	ret = sema_timedwait(&request->synch_sema, 5 * hz);
424	if (ret != 0) {
425		printf("Storvsc_error: create multi-channel timeout, %d\n",
426		    ret);
427		return;
428	}
429
430	if (vstor_packet->operation != VSTOR_OPERATION_COMPLETEIO ||
431	    vstor_packet->status != 0) {
432		printf("Storvsc_error: create multi-channel invalid operation "
433		    "(%d) or statue (%u)\n",
434		    vstor_packet->operation, vstor_packet->status);
435		return;
436	}
437
438	/* Wait for sub-channels setup to complete. */
439	subchan = vmbus_get_subchan(dev->channel, request_channels_cnt);
440
441	/* Attach the sub-channels. */
442	for (i = 0; i < request_channels_cnt; ++i)
443		storvsc_subchan_attach(subchan[i]);
444
445	/* Release the sub-channels. */
446	vmbus_rel_subchan(subchan, request_channels_cnt);
447
448	if (bootverbose)
449		printf("Storvsc create multi-channel success!\n");
450}
451
452/**
453 * @brief initialize channel connection to parent partition
454 *
455 * @param dev  a Hyper-V device pointer
456 * @returns  0 on success, non-zero error on failure
457 */
458static int
459hv_storvsc_channel_init(struct hv_device *dev)
460{
461	int ret = 0, i;
462	struct hv_storvsc_request *request;
463	struct vstor_packet *vstor_packet;
464	struct storvsc_softc *sc;
465	uint16_t max_chans = 0;
466	boolean_t support_multichannel = FALSE;
467
468	max_chans = 0;
469	support_multichannel = FALSE;
470
471	sc = get_stor_device(dev, TRUE);
472	if (sc == NULL)
473		return (ENODEV);
474
475	request = &sc->hs_init_req;
476	memset(request, 0, sizeof(struct hv_storvsc_request));
477	vstor_packet = &request->vstor_packet;
478	request->softc = sc;
479
480	/**
481	 * Initiate the vsc/vsp initialization protocol on the open channel
482	 */
483	sema_init(&request->synch_sema, 0, ("stor_synch_sema"));
484
485	vstor_packet->operation = VSTOR_OPERATION_BEGININITIALIZATION;
486	vstor_packet->flags = REQUEST_COMPLETION_FLAG;
487
488
489	ret = hv_vmbus_channel_send_packet(
490			dev->channel,
491			vstor_packet,
492			VSTOR_PKT_SIZE,
493			(uint64_t)(uintptr_t)request,
494			HV_VMBUS_PACKET_TYPE_DATA_IN_BAND,
495			HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
496
497	if (ret != 0)
498		goto cleanup;
499
500	/* wait 5 seconds */
501	ret = sema_timedwait(&request->synch_sema, 5 * hz);
502	if (ret != 0)
503		goto cleanup;
504
505	if (vstor_packet->operation != VSTOR_OPERATION_COMPLETEIO ||
506		vstor_packet->status != 0) {
507		goto cleanup;
508	}
509
510	for (i = 0; i < nitems(vmstor_proto_list); i++) {
511		/* reuse the packet for version range supported */
512
513		memset(vstor_packet, 0, sizeof(struct vstor_packet));
514		vstor_packet->operation = VSTOR_OPERATION_QUERYPROTOCOLVERSION;
515		vstor_packet->flags = REQUEST_COMPLETION_FLAG;
516
517		vstor_packet->u.version.major_minor =
518			vmstor_proto_list[i].proto_version;
519
520		/* revision is only significant for Windows guests */
521		vstor_packet->u.version.revision = 0;
522
523		ret = hv_vmbus_channel_send_packet(
524			dev->channel,
525			vstor_packet,
526			VSTOR_PKT_SIZE,
527			(uint64_t)(uintptr_t)request,
528			HV_VMBUS_PACKET_TYPE_DATA_IN_BAND,
529			HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
530
531		if (ret != 0)
532			goto cleanup;
533
534		/* wait 5 seconds */
535		ret = sema_timedwait(&request->synch_sema, 5 * hz);
536
537		if (ret)
538			goto cleanup;
539
540		if (vstor_packet->operation != VSTOR_OPERATION_COMPLETEIO) {
541			ret = EINVAL;
542			goto cleanup;
543		}
544		if (vstor_packet->status == 0) {
545			vmstor_proto_version =
546				vmstor_proto_list[i].proto_version;
547			sense_buffer_size =
548				vmstor_proto_list[i].sense_buffer_size;
549			vmscsi_size_delta =
550				vmstor_proto_list[i].vmscsi_size_delta;
551			break;
552		}
553	}
554
555	if (vstor_packet->status != 0) {
556		ret = EINVAL;
557		goto cleanup;
558	}
559	/**
560	 * Query channel properties
561	 */
562	memset(vstor_packet, 0, sizeof(struct vstor_packet));
563	vstor_packet->operation = VSTOR_OPERATION_QUERYPROPERTIES;
564	vstor_packet->flags = REQUEST_COMPLETION_FLAG;
565
566	ret = hv_vmbus_channel_send_packet(
567				dev->channel,
568				vstor_packet,
569				VSTOR_PKT_SIZE,
570				(uint64_t)(uintptr_t)request,
571				HV_VMBUS_PACKET_TYPE_DATA_IN_BAND,
572				HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
573
574	if ( ret != 0)
575		goto cleanup;
576
577	/* wait 5 seconds */
578	ret = sema_timedwait(&request->synch_sema, 5 * hz);
579
580	if (ret != 0)
581		goto cleanup;
582
583	/* TODO: Check returned version */
584	if (vstor_packet->operation != VSTOR_OPERATION_COMPLETEIO ||
585	    vstor_packet->status != 0) {
586		goto cleanup;
587	}
588
589	/* multi-channels feature is supported by WIN8 and above version */
590	max_chans = vstor_packet->u.chan_props.max_channel_cnt;
591	if ((hv_vmbus_protocal_version != HV_VMBUS_VERSION_WIN7) &&
592	    (hv_vmbus_protocal_version != HV_VMBUS_VERSION_WS2008) &&
593	    (vstor_packet->u.chan_props.flags &
594	     HV_STORAGE_SUPPORTS_MULTI_CHANNEL)) {
595		support_multichannel = TRUE;
596	}
597
598	memset(vstor_packet, 0, sizeof(struct vstor_packet));
599	vstor_packet->operation = VSTOR_OPERATION_ENDINITIALIZATION;
600	vstor_packet->flags = REQUEST_COMPLETION_FLAG;
601
602	ret = hv_vmbus_channel_send_packet(
603			dev->channel,
604			vstor_packet,
605			VSTOR_PKT_SIZE,
606			(uint64_t)(uintptr_t)request,
607			HV_VMBUS_PACKET_TYPE_DATA_IN_BAND,
608			HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
609
610	if (ret != 0) {
611		goto cleanup;
612	}
613
614	/* wait 5 seconds */
615	ret = sema_timedwait(&request->synch_sema, 5 * hz);
616
617	if (ret != 0)
618		goto cleanup;
619
620	if (vstor_packet->operation != VSTOR_OPERATION_COMPLETEIO ||
621	    vstor_packet->status != 0)
622		goto cleanup;
623
624	/*
625	 * If multi-channel is supported, send multichannel create
626	 * request to host.
627	 */
628	if (support_multichannel)
629		storvsc_send_multichannel_request(dev, max_chans);
630
631cleanup:
632	sema_destroy(&request->synch_sema);
633	return (ret);
634}
635
636/**
637 * @brief Open channel connection to paraent partition StorVSP driver
638 *
639 * Open and initialize channel connection to parent partition StorVSP driver.
640 *
641 * @param pointer to a Hyper-V device
642 * @returns 0 on success, non-zero error on failure
643 */
644static int
645hv_storvsc_connect_vsp(struct hv_device *dev)
646{
647	int ret = 0;
648	struct vmstor_chan_props props;
649	struct storvsc_softc *sc;
650
651	sc = device_get_softc(dev->device);
652
653	memset(&props, 0, sizeof(struct vmstor_chan_props));
654
655	/*
656	 * Open the channel
657	 */
658
659	ret = hv_vmbus_channel_open(
660		dev->channel,
661		sc->hs_drv_props->drv_ringbuffer_size,
662		sc->hs_drv_props->drv_ringbuffer_size,
663		(void *)&props,
664		sizeof(struct vmstor_chan_props),
665		hv_storvsc_on_channel_callback,
666		dev->channel);
667
668	if (ret != 0) {
669		return ret;
670	}
671
672	ret = hv_storvsc_channel_init(dev);
673
674	return (ret);
675}
676
677#if HVS_HOST_RESET
678static int
679hv_storvsc_host_reset(struct hv_device *dev)
680{
681	int ret = 0;
682	struct storvsc_softc *sc;
683
684	struct hv_storvsc_request *request;
685	struct vstor_packet *vstor_packet;
686
687	sc = get_stor_device(dev, TRUE);
688	if (sc == NULL) {
689		return ENODEV;
690	}
691
692	request = &sc->hs_reset_req;
693	request->softc = sc;
694	vstor_packet = &request->vstor_packet;
695
696	sema_init(&request->synch_sema, 0, "stor synch sema");
697
698	vstor_packet->operation = VSTOR_OPERATION_RESETBUS;
699	vstor_packet->flags = REQUEST_COMPLETION_FLAG;
700
701	ret = hv_vmbus_channel_send_packet(dev->channel,
702			vstor_packet,
703			VSTOR_PKT_SIZE,
704			(uint64_t)(uintptr_t)&sc->hs_reset_req,
705			HV_VMBUS_PACKET_TYPE_DATA_IN_BAND,
706			HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
707
708	if (ret != 0) {
709		goto cleanup;
710	}
711
712	ret = sema_timedwait(&request->synch_sema, 5 * hz); /* KYS 5 seconds */
713
714	if (ret) {
715		goto cleanup;
716	}
717
718
719	/*
720	 * At this point, all outstanding requests in the adapter
721	 * should have been flushed out and return to us
722	 */
723
724cleanup:
725	sema_destroy(&request->synch_sema);
726	return (ret);
727}
728#endif /* HVS_HOST_RESET */
729
730/**
731 * @brief Function to initiate an I/O request
732 *
733 * @param device Hyper-V device pointer
734 * @param request pointer to a request structure
735 * @returns 0 on success, non-zero error on failure
736 */
737static int
738hv_storvsc_io_request(struct hv_device *device,
739					  struct hv_storvsc_request *request)
740{
741	struct storvsc_softc *sc;
742	struct vstor_packet *vstor_packet = &request->vstor_packet;
743	struct hv_vmbus_channel* outgoing_channel = NULL;
744	int ret = 0;
745
746	sc = get_stor_device(device, TRUE);
747
748	if (sc == NULL) {
749		return ENODEV;
750	}
751
752	vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
753
754	vstor_packet->u.vm_srb.length =
755	    sizeof(struct vmscsi_req) - vmscsi_size_delta;
756
757	vstor_packet->u.vm_srb.sense_info_len = sense_buffer_size;
758
759	vstor_packet->u.vm_srb.transfer_len = request->data_buf.length;
760
761	vstor_packet->operation = VSTOR_OPERATION_EXECUTESRB;
762
763	outgoing_channel = vmbus_select_outgoing_channel(device->channel);
764
765	mtx_unlock(&request->softc->hs_lock);
766	if (request->data_buf.length) {
767		ret = hv_vmbus_channel_send_packet_multipagebuffer(
768				outgoing_channel,
769				&request->data_buf,
770				vstor_packet,
771				VSTOR_PKT_SIZE,
772				(uint64_t)(uintptr_t)request);
773
774	} else {
775		ret = hv_vmbus_channel_send_packet(
776			outgoing_channel,
777			vstor_packet,
778			VSTOR_PKT_SIZE,
779			(uint64_t)(uintptr_t)request,
780			HV_VMBUS_PACKET_TYPE_DATA_IN_BAND,
781			HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
782	}
783	mtx_lock(&request->softc->hs_lock);
784
785	if (ret != 0) {
786		printf("Unable to send packet %p ret %d", vstor_packet, ret);
787	} else {
788		atomic_add_int(&sc->hs_num_out_reqs, 1);
789	}
790
791	return (ret);
792}
793
794
795/**
796 * Process IO_COMPLETION_OPERATION and ready
797 * the result to be completed for upper layer
798 * processing by the CAM layer.
799 */
800static void
801hv_storvsc_on_iocompletion(struct storvsc_softc *sc,
802			   struct vstor_packet *vstor_packet,
803			   struct hv_storvsc_request *request)
804{
805	struct vmscsi_req *vm_srb;
806
807	vm_srb = &vstor_packet->u.vm_srb;
808
809	/*
810	 * Copy some fields of the host's response into the request structure,
811	 * because the fields will be used later in storvsc_io_done().
812	 */
813	request->vstor_packet.u.vm_srb.scsi_status = vm_srb->scsi_status;
814	request->vstor_packet.u.vm_srb.srb_status = vm_srb->srb_status;
815	request->vstor_packet.u.vm_srb.transfer_len = vm_srb->transfer_len;
816
817	if (((vm_srb->scsi_status & 0xFF) == SCSI_STATUS_CHECK_COND) &&
818			(vm_srb->srb_status & SRB_STATUS_AUTOSENSE_VALID)) {
819		/* Autosense data available */
820
821		KASSERT(vm_srb->sense_info_len <= request->sense_info_len,
822				("vm_srb->sense_info_len <= "
823				 "request->sense_info_len"));
824
825		memcpy(request->sense_data, vm_srb->u.sense_data,
826			vm_srb->sense_info_len);
827
828		request->sense_info_len = vm_srb->sense_info_len;
829	}
830
831	/* Complete request by passing to the CAM layer */
832	storvsc_io_done(request);
833	atomic_subtract_int(&sc->hs_num_out_reqs, 1);
834	if (sc->hs_drain_notify && (sc->hs_num_out_reqs == 0)) {
835		sema_post(&sc->hs_drain_sema);
836	}
837}
838
839static void
840hv_storvsc_rescan_target(struct storvsc_softc *sc)
841{
842	path_id_t pathid;
843	target_id_t targetid;
844	union ccb *ccb;
845
846	pathid = cam_sim_path(sc->hs_sim);
847	targetid = CAM_TARGET_WILDCARD;
848
849	/*
850	 * Allocate a CCB and schedule a rescan.
851	 */
852	ccb = xpt_alloc_ccb_nowait();
853	if (ccb == NULL) {
854		printf("unable to alloc CCB for rescan\n");
855		return;
856	}
857
858	if (xpt_create_path(&ccb->ccb_h.path, NULL, pathid, targetid,
859	    CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
860		printf("unable to create path for rescan, pathid: %u,"
861		    "targetid: %u\n", pathid, targetid);
862		xpt_free_ccb(ccb);
863		return;
864	}
865
866	if (targetid == CAM_TARGET_WILDCARD)
867		ccb->ccb_h.func_code = XPT_SCAN_BUS;
868	else
869		ccb->ccb_h.func_code = XPT_SCAN_TGT;
870
871	xpt_rescan(ccb);
872}
873
874static void
875hv_storvsc_on_channel_callback(void *context)
876{
877	int ret = 0;
878	hv_vmbus_channel *channel = (hv_vmbus_channel *)context;
879	struct hv_device *device = NULL;
880	struct storvsc_softc *sc;
881	uint32_t bytes_recvd;
882	uint64_t request_id;
883	uint8_t packet[roundup2(sizeof(struct vstor_packet), 8)];
884	struct hv_storvsc_request *request;
885	struct vstor_packet *vstor_packet;
886
887	device = channel->device;
888	KASSERT(device, ("device is NULL"));
889
890	sc = get_stor_device(device, FALSE);
891	if (sc == NULL) {
892		printf("Storvsc_error: get stor device failed.\n");
893		return;
894	}
895
896	ret = hv_vmbus_channel_recv_packet(
897			channel,
898			packet,
899			roundup2(VSTOR_PKT_SIZE, 8),
900			&bytes_recvd,
901			&request_id);
902
903	while ((ret == 0) && (bytes_recvd > 0)) {
904		request = (struct hv_storvsc_request *)(uintptr_t)request_id;
905
906		if ((request == &sc->hs_init_req) ||
907			(request == &sc->hs_reset_req)) {
908			memcpy(&request->vstor_packet, packet,
909				   sizeof(struct vstor_packet));
910			sema_post(&request->synch_sema);
911		} else {
912			vstor_packet = (struct vstor_packet *)packet;
913			switch(vstor_packet->operation) {
914			case VSTOR_OPERATION_COMPLETEIO:
915				if (request == NULL)
916					panic("VMBUS: storvsc received a "
917					    "packet with NULL request id in "
918					    "COMPLETEIO operation.");
919
920				hv_storvsc_on_iocompletion(sc,
921							vstor_packet, request);
922				break;
923			case VSTOR_OPERATION_REMOVEDEVICE:
924				printf("VMBUS: storvsc operation %d not "
925				    "implemented.\n", vstor_packet->operation);
926				/* TODO: implement */
927				break;
928			case VSTOR_OPERATION_ENUMERATE_BUS:
929				hv_storvsc_rescan_target(sc);
930				break;
931			default:
932				break;
933			}
934		}
935		ret = hv_vmbus_channel_recv_packet(
936				channel,
937				packet,
938				roundup2(VSTOR_PKT_SIZE, 8),
939				&bytes_recvd,
940				&request_id);
941	}
942}
943
944/**
945 * @brief StorVSC probe function
946 *
947 * Device probe function.  Returns 0 if the input device is a StorVSC
948 * device.  Otherwise, a ENXIO is returned.  If the input device is
949 * for BlkVSC (paravirtual IDE) device and this support is disabled in
950 * favor of the emulated ATA/IDE device, return ENXIO.
951 *
952 * @param a device
953 * @returns 0 on success, ENXIO if not a matcing StorVSC device
954 */
955static int
956storvsc_probe(device_t dev)
957{
958	int ata_disk_enable = 0;
959	int ret	= ENXIO;
960
961	switch (storvsc_get_storage_type(dev)) {
962	case DRIVER_BLKVSC:
963		if(bootverbose)
964			device_printf(dev, "DRIVER_BLKVSC-Emulated ATA/IDE probe\n");
965		if (!getenv_int("hw.ata.disk_enable", &ata_disk_enable)) {
966			if(bootverbose)
967				device_printf(dev,
968					"Enlightened ATA/IDE detected\n");
969			device_set_desc(dev, g_drv_props_table[DRIVER_BLKVSC].drv_desc);
970			ret = BUS_PROBE_DEFAULT;
971		} else if(bootverbose)
972			device_printf(dev, "Emulated ATA/IDE set (hw.ata.disk_enable set)\n");
973		break;
974	case DRIVER_STORVSC:
975		if(bootverbose)
976			device_printf(dev, "Enlightened SCSI device detected\n");
977		device_set_desc(dev, g_drv_props_table[DRIVER_STORVSC].drv_desc);
978		ret = BUS_PROBE_DEFAULT;
979		break;
980	default:
981		ret = ENXIO;
982	}
983	return (ret);
984}
985
986/**
987 * @brief StorVSC attach function
988 *
989 * Function responsible for allocating per-device structures,
990 * setting up CAM interfaces and scanning for available LUNs to
991 * be used for SCSI device peripherals.
992 *
993 * @param a device
994 * @returns 0 on success or an error on failure
995 */
996static int
997storvsc_attach(device_t dev)
998{
999	struct hv_device *hv_dev = vmbus_get_devctx(dev);
1000	enum hv_storage_type stor_type;
1001	struct storvsc_softc *sc;
1002	struct cam_devq *devq;
1003	int ret, i, j;
1004	struct hv_storvsc_request *reqp;
1005	struct root_hold_token *root_mount_token = NULL;
1006	struct hv_sgl_node *sgl_node = NULL;
1007	void *tmp_buff = NULL;
1008
1009	/*
1010	 * We need to serialize storvsc attach calls.
1011	 */
1012	root_mount_token = root_mount_hold("storvsc");
1013
1014	sc = device_get_softc(dev);
1015
1016	stor_type = storvsc_get_storage_type(dev);
1017
1018	if (stor_type == DRIVER_UNKNOWN) {
1019		ret = ENODEV;
1020		goto cleanup;
1021	}
1022
1023	/* fill in driver specific properties */
1024	sc->hs_drv_props = &g_drv_props_table[stor_type];
1025
1026	/* fill in device specific properties */
1027	sc->hs_unit	= device_get_unit(dev);
1028	sc->hs_dev	= hv_dev;
1029
1030	LIST_INIT(&sc->hs_free_list);
1031	mtx_init(&sc->hs_lock, "hvslck", NULL, MTX_DEF);
1032
1033	for (i = 0; i < sc->hs_drv_props->drv_max_ios_per_target; ++i) {
1034		reqp = malloc(sizeof(struct hv_storvsc_request),
1035				 M_DEVBUF, M_WAITOK|M_ZERO);
1036		reqp->softc = sc;
1037
1038		LIST_INSERT_HEAD(&sc->hs_free_list, reqp, link);
1039	}
1040
1041	/* create sg-list page pool */
1042	if (FALSE == g_hv_sgl_page_pool.is_init) {
1043		g_hv_sgl_page_pool.is_init = TRUE;
1044		LIST_INIT(&g_hv_sgl_page_pool.in_use_sgl_list);
1045		LIST_INIT(&g_hv_sgl_page_pool.free_sgl_list);
1046
1047		/*
1048		 * Pre-create SG list, each SG list with
1049		 * HV_MAX_MULTIPAGE_BUFFER_COUNT segments, each
1050		 * segment has one page buffer
1051		 */
1052		for (i = 0; i < STORVSC_MAX_IO_REQUESTS; i++) {
1053	        	sgl_node = malloc(sizeof(struct hv_sgl_node),
1054			    M_DEVBUF, M_WAITOK|M_ZERO);
1055
1056			sgl_node->sgl_data =
1057			    sglist_alloc(HV_MAX_MULTIPAGE_BUFFER_COUNT,
1058			    M_WAITOK|M_ZERO);
1059
1060			for (j = 0; j < HV_MAX_MULTIPAGE_BUFFER_COUNT; j++) {
1061				tmp_buff = malloc(PAGE_SIZE,
1062				    M_DEVBUF, M_WAITOK|M_ZERO);
1063
1064				sgl_node->sgl_data->sg_segs[j].ss_paddr =
1065				    (vm_paddr_t)tmp_buff;
1066			}
1067
1068			LIST_INSERT_HEAD(&g_hv_sgl_page_pool.free_sgl_list,
1069			    sgl_node, link);
1070		}
1071	}
1072
1073	sc->hs_destroy = FALSE;
1074	sc->hs_drain_notify = FALSE;
1075	sema_init(&sc->hs_drain_sema, 0, "Store Drain Sema");
1076
1077	ret = hv_storvsc_connect_vsp(hv_dev);
1078	if (ret != 0) {
1079		goto cleanup;
1080	}
1081
1082	/*
1083	 * Create the device queue.
1084	 * Hyper-V maps each target to one SCSI HBA
1085	 */
1086	devq = cam_simq_alloc(sc->hs_drv_props->drv_max_ios_per_target);
1087	if (devq == NULL) {
1088		device_printf(dev, "Failed to alloc device queue\n");
1089		ret = ENOMEM;
1090		goto cleanup;
1091	}
1092
1093	sc->hs_sim = cam_sim_alloc(storvsc_action,
1094				storvsc_poll,
1095				sc->hs_drv_props->drv_name,
1096				sc,
1097				sc->hs_unit,
1098				&sc->hs_lock, 1,
1099				sc->hs_drv_props->drv_max_ios_per_target,
1100				devq);
1101
1102	if (sc->hs_sim == NULL) {
1103		device_printf(dev, "Failed to alloc sim\n");
1104		cam_simq_free(devq);
1105		ret = ENOMEM;
1106		goto cleanup;
1107	}
1108
1109	mtx_lock(&sc->hs_lock);
1110	/* bus_id is set to 0, need to get it from VMBUS channel query? */
1111	if (xpt_bus_register(sc->hs_sim, dev, 0) != CAM_SUCCESS) {
1112		cam_sim_free(sc->hs_sim, /*free_devq*/TRUE);
1113		mtx_unlock(&sc->hs_lock);
1114		device_printf(dev, "Unable to register SCSI bus\n");
1115		ret = ENXIO;
1116		goto cleanup;
1117	}
1118
1119	if (xpt_create_path(&sc->hs_path, /*periph*/NULL,
1120		 cam_sim_path(sc->hs_sim),
1121		CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1122		xpt_bus_deregister(cam_sim_path(sc->hs_sim));
1123		cam_sim_free(sc->hs_sim, /*free_devq*/TRUE);
1124		mtx_unlock(&sc->hs_lock);
1125		device_printf(dev, "Unable to create path\n");
1126		ret = ENXIO;
1127		goto cleanup;
1128	}
1129
1130	mtx_unlock(&sc->hs_lock);
1131
1132	root_mount_rel(root_mount_token);
1133	return (0);
1134
1135
1136cleanup:
1137	root_mount_rel(root_mount_token);
1138	while (!LIST_EMPTY(&sc->hs_free_list)) {
1139		reqp = LIST_FIRST(&sc->hs_free_list);
1140		LIST_REMOVE(reqp, link);
1141		free(reqp, M_DEVBUF);
1142	}
1143
1144	while (!LIST_EMPTY(&g_hv_sgl_page_pool.free_sgl_list)) {
1145		sgl_node = LIST_FIRST(&g_hv_sgl_page_pool.free_sgl_list);
1146		LIST_REMOVE(sgl_node, link);
1147		for (j = 0; j < HV_MAX_MULTIPAGE_BUFFER_COUNT; j++) {
1148			if (NULL !=
1149			    (void*)sgl_node->sgl_data->sg_segs[j].ss_paddr) {
1150				free((void*)sgl_node->sgl_data->sg_segs[j].ss_paddr, M_DEVBUF);
1151			}
1152		}
1153		sglist_free(sgl_node->sgl_data);
1154		free(sgl_node, M_DEVBUF);
1155	}
1156
1157	return (ret);
1158}
1159
1160/**
1161 * @brief StorVSC device detach function
1162 *
1163 * This function is responsible for safely detaching a
1164 * StorVSC device.  This includes waiting for inbound responses
1165 * to complete and freeing associated per-device structures.
1166 *
1167 * @param dev a device
1168 * returns 0 on success
1169 */
1170static int
1171storvsc_detach(device_t dev)
1172{
1173	struct storvsc_softc *sc = device_get_softc(dev);
1174	struct hv_storvsc_request *reqp = NULL;
1175	struct hv_device *hv_device = vmbus_get_devctx(dev);
1176	struct hv_sgl_node *sgl_node = NULL;
1177	int j = 0;
1178
1179	sc->hs_destroy = TRUE;
1180
1181	/*
1182	 * At this point, all outbound traffic should be disabled. We
1183	 * only allow inbound traffic (responses) to proceed so that
1184	 * outstanding requests can be completed.
1185	 */
1186
1187	sc->hs_drain_notify = TRUE;
1188	sema_wait(&sc->hs_drain_sema);
1189	sc->hs_drain_notify = FALSE;
1190
1191	/*
1192	 * Since we have already drained, we don't need to busy wait.
1193	 * The call to close the channel will reset the callback
1194	 * under the protection of the incoming channel lock.
1195	 */
1196
1197	hv_vmbus_channel_close(hv_device->channel);
1198
1199	mtx_lock(&sc->hs_lock);
1200	while (!LIST_EMPTY(&sc->hs_free_list)) {
1201		reqp = LIST_FIRST(&sc->hs_free_list);
1202		LIST_REMOVE(reqp, link);
1203
1204		free(reqp, M_DEVBUF);
1205	}
1206	mtx_unlock(&sc->hs_lock);
1207
1208	while (!LIST_EMPTY(&g_hv_sgl_page_pool.free_sgl_list)) {
1209		sgl_node = LIST_FIRST(&g_hv_sgl_page_pool.free_sgl_list);
1210		LIST_REMOVE(sgl_node, link);
1211		for (j = 0; j < HV_MAX_MULTIPAGE_BUFFER_COUNT; j++){
1212			if (NULL !=
1213			    (void*)sgl_node->sgl_data->sg_segs[j].ss_paddr) {
1214				free((void*)sgl_node->sgl_data->sg_segs[j].ss_paddr, M_DEVBUF);
1215			}
1216		}
1217		sglist_free(sgl_node->sgl_data);
1218		free(sgl_node, M_DEVBUF);
1219	}
1220
1221	return (0);
1222}
1223
1224#if HVS_TIMEOUT_TEST
1225/**
1226 * @brief unit test for timed out operations
1227 *
1228 * This function provides unit testing capability to simulate
1229 * timed out operations.  Recompilation with HV_TIMEOUT_TEST=1
1230 * is required.
1231 *
1232 * @param reqp pointer to a request structure
1233 * @param opcode SCSI operation being performed
1234 * @param wait if 1, wait for I/O to complete
1235 */
1236static void
1237storvsc_timeout_test(struct hv_storvsc_request *reqp,
1238		uint8_t opcode, int wait)
1239{
1240	int ret;
1241	union ccb *ccb = reqp->ccb;
1242	struct storvsc_softc *sc = reqp->softc;
1243
1244	if (reqp->vstor_packet.vm_srb.cdb[0] != opcode) {
1245		return;
1246	}
1247
1248	if (wait) {
1249		mtx_lock(&reqp->event.mtx);
1250	}
1251	ret = hv_storvsc_io_request(sc->hs_dev, reqp);
1252	if (ret != 0) {
1253		if (wait) {
1254			mtx_unlock(&reqp->event.mtx);
1255		}
1256		printf("%s: io_request failed with %d.\n",
1257				__func__, ret);
1258		ccb->ccb_h.status = CAM_PROVIDE_FAIL;
1259		mtx_lock(&sc->hs_lock);
1260		storvsc_free_request(sc, reqp);
1261		xpt_done(ccb);
1262		mtx_unlock(&sc->hs_lock);
1263		return;
1264	}
1265
1266	if (wait) {
1267		xpt_print(ccb->ccb_h.path,
1268				"%u: %s: waiting for IO return.\n",
1269				ticks, __func__);
1270		ret = cv_timedwait(&reqp->event.cv, &reqp->event.mtx, 60*hz);
1271		mtx_unlock(&reqp->event.mtx);
1272		xpt_print(ccb->ccb_h.path, "%u: %s: %s.\n",
1273				ticks, __func__, (ret == 0)?
1274				"IO return detected" :
1275				"IO return not detected");
1276		/*
1277		 * Now both the timer handler and io done are running
1278		 * simultaneously. We want to confirm the io done always
1279		 * finishes after the timer handler exits. So reqp used by
1280		 * timer handler is not freed or stale. Do busy loop for
1281		 * another 1/10 second to make sure io done does
1282		 * wait for the timer handler to complete.
1283		 */
1284		DELAY(100*1000);
1285		mtx_lock(&sc->hs_lock);
1286		xpt_print(ccb->ccb_h.path,
1287				"%u: %s: finishing, queue frozen %d, "
1288				"ccb status 0x%x scsi_status 0x%x.\n",
1289				ticks, __func__, sc->hs_frozen,
1290				ccb->ccb_h.status,
1291				ccb->csio.scsi_status);
1292		mtx_unlock(&sc->hs_lock);
1293	}
1294}
1295#endif /* HVS_TIMEOUT_TEST */
1296
1297#ifdef notyet
1298/**
1299 * @brief timeout handler for requests
1300 *
1301 * This function is called as a result of a callout expiring.
1302 *
1303 * @param arg pointer to a request
1304 */
1305static void
1306storvsc_timeout(void *arg)
1307{
1308	struct hv_storvsc_request *reqp = arg;
1309	struct storvsc_softc *sc = reqp->softc;
1310	union ccb *ccb = reqp->ccb;
1311
1312	if (reqp->retries == 0) {
1313		mtx_lock(&sc->hs_lock);
1314		xpt_print(ccb->ccb_h.path,
1315		    "%u: IO timed out (req=0x%p), wait for another %u secs.\n",
1316		    ticks, reqp, ccb->ccb_h.timeout / 1000);
1317		cam_error_print(ccb, CAM_ESF_ALL, CAM_EPF_ALL);
1318		mtx_unlock(&sc->hs_lock);
1319
1320		reqp->retries++;
1321		callout_reset_sbt(&reqp->callout, SBT_1MS * ccb->ccb_h.timeout,
1322		    0, storvsc_timeout, reqp, 0);
1323#if HVS_TIMEOUT_TEST
1324		storvsc_timeout_test(reqp, SEND_DIAGNOSTIC, 0);
1325#endif
1326		return;
1327	}
1328
1329	mtx_lock(&sc->hs_lock);
1330	xpt_print(ccb->ccb_h.path,
1331		"%u: IO (reqp = 0x%p) did not return for %u seconds, %s.\n",
1332		ticks, reqp, ccb->ccb_h.timeout * (reqp->retries+1) / 1000,
1333		(sc->hs_frozen == 0)?
1334		"freezing the queue" : "the queue is already frozen");
1335	if (sc->hs_frozen == 0) {
1336		sc->hs_frozen = 1;
1337		xpt_freeze_simq(xpt_path_sim(ccb->ccb_h.path), 1);
1338	}
1339	mtx_unlock(&sc->hs_lock);
1340
1341#if HVS_TIMEOUT_TEST
1342	storvsc_timeout_test(reqp, MODE_SELECT_10, 1);
1343#endif
1344}
1345#endif
1346
1347/**
1348 * @brief StorVSC device poll function
1349 *
1350 * This function is responsible for servicing requests when
1351 * interrupts are disabled (i.e when we are dumping core.)
1352 *
1353 * @param sim a pointer to a CAM SCSI interface module
1354 */
1355static void
1356storvsc_poll(struct cam_sim *sim)
1357{
1358	struct storvsc_softc *sc = cam_sim_softc(sim);
1359
1360	mtx_assert(&sc->hs_lock, MA_OWNED);
1361	mtx_unlock(&sc->hs_lock);
1362	hv_storvsc_on_channel_callback(sc->hs_dev->channel);
1363	mtx_lock(&sc->hs_lock);
1364}
1365
1366/**
1367 * @brief StorVSC device action function
1368 *
1369 * This function is responsible for handling SCSI operations which
1370 * are passed from the CAM layer.  The requests are in the form of
1371 * CAM control blocks which indicate the action being performed.
1372 * Not all actions require converting the request to a VSCSI protocol
1373 * message - these actions can be responded to by this driver.
1374 * Requests which are destined for a backend storage device are converted
1375 * to a VSCSI protocol message and sent on the channel connection associated
1376 * with this device.
1377 *
1378 * @param sim pointer to a CAM SCSI interface module
1379 * @param ccb pointer to a CAM control block
1380 */
1381static void
1382storvsc_action(struct cam_sim *sim, union ccb *ccb)
1383{
1384	struct storvsc_softc *sc = cam_sim_softc(sim);
1385	int res;
1386
1387	mtx_assert(&sc->hs_lock, MA_OWNED);
1388	switch (ccb->ccb_h.func_code) {
1389	case XPT_PATH_INQ: {
1390		struct ccb_pathinq *cpi = &ccb->cpi;
1391
1392		cpi->version_num = 1;
1393		cpi->hba_inquiry = PI_TAG_ABLE|PI_SDTR_ABLE;
1394		cpi->target_sprt = 0;
1395		cpi->hba_misc = PIM_NOBUSRESET;
1396		cpi->hba_eng_cnt = 0;
1397		cpi->max_target = STORVSC_MAX_TARGETS;
1398		cpi->max_lun = sc->hs_drv_props->drv_max_luns_per_target;
1399		cpi->initiator_id = cpi->max_target;
1400		cpi->bus_id = cam_sim_bus(sim);
1401		cpi->base_transfer_speed = 300000;
1402		cpi->transport = XPORT_SAS;
1403		cpi->transport_version = 0;
1404		cpi->protocol = PROTO_SCSI;
1405		cpi->protocol_version = SCSI_REV_SPC2;
1406		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1407		strncpy(cpi->hba_vid, sc->hs_drv_props->drv_name, HBA_IDLEN);
1408		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1409		cpi->unit_number = cam_sim_unit(sim);
1410
1411		ccb->ccb_h.status = CAM_REQ_CMP;
1412		xpt_done(ccb);
1413		return;
1414	}
1415	case XPT_GET_TRAN_SETTINGS: {
1416		struct  ccb_trans_settings *cts = &ccb->cts;
1417
1418		cts->transport = XPORT_SAS;
1419		cts->transport_version = 0;
1420		cts->protocol = PROTO_SCSI;
1421		cts->protocol_version = SCSI_REV_SPC2;
1422
1423		/* enable tag queuing and disconnected mode */
1424		cts->proto_specific.valid = CTS_SCSI_VALID_TQ;
1425		cts->proto_specific.scsi.valid = CTS_SCSI_VALID_TQ;
1426		cts->proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB;
1427		cts->xport_specific.valid = CTS_SPI_VALID_DISC;
1428		cts->xport_specific.spi.flags = CTS_SPI_FLAGS_DISC_ENB;
1429
1430		ccb->ccb_h.status = CAM_REQ_CMP;
1431		xpt_done(ccb);
1432		return;
1433	}
1434	case XPT_SET_TRAN_SETTINGS:	{
1435		ccb->ccb_h.status = CAM_REQ_CMP;
1436		xpt_done(ccb);
1437		return;
1438	}
1439	case XPT_CALC_GEOMETRY:{
1440		cam_calc_geometry(&ccb->ccg, 1);
1441		xpt_done(ccb);
1442		return;
1443	}
1444	case  XPT_RESET_BUS:
1445	case  XPT_RESET_DEV:{
1446#if HVS_HOST_RESET
1447		if ((res = hv_storvsc_host_reset(sc->hs_dev)) != 0) {
1448			xpt_print(ccb->ccb_h.path,
1449				"hv_storvsc_host_reset failed with %d\n", res);
1450			ccb->ccb_h.status = CAM_PROVIDE_FAIL;
1451			xpt_done(ccb);
1452			return;
1453		}
1454		ccb->ccb_h.status = CAM_REQ_CMP;
1455		xpt_done(ccb);
1456		return;
1457#else
1458		xpt_print(ccb->ccb_h.path,
1459				  "%s reset not supported.\n",
1460				  (ccb->ccb_h.func_code == XPT_RESET_BUS)?
1461				  "bus" : "dev");
1462		ccb->ccb_h.status = CAM_REQ_INVALID;
1463		xpt_done(ccb);
1464		return;
1465#endif	/* HVS_HOST_RESET */
1466	}
1467	case XPT_SCSI_IO:
1468	case XPT_IMMED_NOTIFY: {
1469		struct hv_storvsc_request *reqp = NULL;
1470
1471		if (ccb->csio.cdb_len == 0) {
1472			panic("cdl_len is 0\n");
1473		}
1474
1475		if (LIST_EMPTY(&sc->hs_free_list)) {
1476			ccb->ccb_h.status = CAM_REQUEUE_REQ;
1477			if (sc->hs_frozen == 0) {
1478				sc->hs_frozen = 1;
1479				xpt_freeze_simq(sim, /* count*/1);
1480			}
1481			xpt_done(ccb);
1482			return;
1483		}
1484
1485		reqp = LIST_FIRST(&sc->hs_free_list);
1486		LIST_REMOVE(reqp, link);
1487
1488		bzero(reqp, sizeof(struct hv_storvsc_request));
1489		reqp->softc = sc;
1490
1491		ccb->ccb_h.status |= CAM_SIM_QUEUED;
1492		if ((res = create_storvsc_request(ccb, reqp)) != 0) {
1493			ccb->ccb_h.status = CAM_REQ_INVALID;
1494			xpt_done(ccb);
1495			return;
1496		}
1497
1498#ifdef notyet
1499		if (ccb->ccb_h.timeout != CAM_TIME_INFINITY) {
1500			callout_init(&reqp->callout, 1);
1501			callout_reset_sbt(&reqp->callout,
1502			    SBT_1MS * ccb->ccb_h.timeout, 0,
1503			    storvsc_timeout, reqp, 0);
1504#if HVS_TIMEOUT_TEST
1505			cv_init(&reqp->event.cv, "storvsc timeout cv");
1506			mtx_init(&reqp->event.mtx, "storvsc timeout mutex",
1507					NULL, MTX_DEF);
1508			switch (reqp->vstor_packet.vm_srb.cdb[0]) {
1509				case MODE_SELECT_10:
1510				case SEND_DIAGNOSTIC:
1511					/* To have timer send the request. */
1512					return;
1513				default:
1514					break;
1515			}
1516#endif /* HVS_TIMEOUT_TEST */
1517		}
1518#endif
1519
1520		if ((res = hv_storvsc_io_request(sc->hs_dev, reqp)) != 0) {
1521			xpt_print(ccb->ccb_h.path,
1522				"hv_storvsc_io_request failed with %d\n", res);
1523			ccb->ccb_h.status = CAM_PROVIDE_FAIL;
1524			storvsc_free_request(sc, reqp);
1525			xpt_done(ccb);
1526			return;
1527		}
1528		return;
1529	}
1530
1531	default:
1532		ccb->ccb_h.status = CAM_REQ_INVALID;
1533		xpt_done(ccb);
1534		return;
1535	}
1536}
1537
1538/**
1539 * @brief destroy bounce buffer
1540 *
1541 * This function is responsible for destroy a Scatter/Gather list
1542 * that create by storvsc_create_bounce_buffer()
1543 *
1544 * @param sgl- the Scatter/Gather need be destroy
1545 * @param sg_count- page count of the SG list.
1546 *
1547 */
1548static void
1549storvsc_destroy_bounce_buffer(struct sglist *sgl)
1550{
1551	struct hv_sgl_node *sgl_node = NULL;
1552	if (LIST_EMPTY(&g_hv_sgl_page_pool.in_use_sgl_list)) {
1553		printf("storvsc error: not enough in use sgl\n");
1554		return;
1555	}
1556	sgl_node = LIST_FIRST(&g_hv_sgl_page_pool.in_use_sgl_list);
1557	LIST_REMOVE(sgl_node, link);
1558	sgl_node->sgl_data = sgl;
1559	LIST_INSERT_HEAD(&g_hv_sgl_page_pool.free_sgl_list, sgl_node, link);
1560}
1561
1562/**
1563 * @brief create bounce buffer
1564 *
1565 * This function is responsible for create a Scatter/Gather list,
1566 * which hold several pages that can be aligned with page size.
1567 *
1568 * @param seg_count- SG-list segments count
1569 * @param write - if WRITE_TYPE, set SG list page used size to 0,
1570 * otherwise set used size to page size.
1571 *
1572 * return NULL if create failed
1573 */
1574static struct sglist *
1575storvsc_create_bounce_buffer(uint16_t seg_count, int write)
1576{
1577	int i = 0;
1578	struct sglist *bounce_sgl = NULL;
1579	unsigned int buf_len = ((write == WRITE_TYPE) ? 0 : PAGE_SIZE);
1580	struct hv_sgl_node *sgl_node = NULL;
1581
1582	/* get struct sglist from free_sgl_list */
1583	if (LIST_EMPTY(&g_hv_sgl_page_pool.free_sgl_list)) {
1584		printf("storvsc error: not enough free sgl\n");
1585		return NULL;
1586	}
1587	sgl_node = LIST_FIRST(&g_hv_sgl_page_pool.free_sgl_list);
1588	LIST_REMOVE(sgl_node, link);
1589	bounce_sgl = sgl_node->sgl_data;
1590	LIST_INSERT_HEAD(&g_hv_sgl_page_pool.in_use_sgl_list, sgl_node, link);
1591
1592	bounce_sgl->sg_maxseg = seg_count;
1593
1594	if (write == WRITE_TYPE)
1595		bounce_sgl->sg_nseg = 0;
1596	else
1597		bounce_sgl->sg_nseg = seg_count;
1598
1599	for (i = 0; i < seg_count; i++)
1600	        bounce_sgl->sg_segs[i].ss_len = buf_len;
1601
1602	return bounce_sgl;
1603}
1604
1605/**
1606 * @brief copy data from SG list to bounce buffer
1607 *
1608 * This function is responsible for copy data from one SG list's segments
1609 * to another SG list which used as bounce buffer.
1610 *
1611 * @param bounce_sgl - the destination SG list
1612 * @param orig_sgl - the segment of the source SG list.
1613 * @param orig_sgl_count - the count of segments.
1614 * @param orig_sgl_count - indicate which segment need bounce buffer,
1615 *  set 1 means need.
1616 *
1617 */
1618static void
1619storvsc_copy_sgl_to_bounce_buf(struct sglist *bounce_sgl,
1620			       bus_dma_segment_t *orig_sgl,
1621			       unsigned int orig_sgl_count,
1622			       uint64_t seg_bits)
1623{
1624	int src_sgl_idx = 0;
1625
1626	for (src_sgl_idx = 0; src_sgl_idx < orig_sgl_count; src_sgl_idx++) {
1627		if (seg_bits & (1 << src_sgl_idx)) {
1628			memcpy((void*)bounce_sgl->sg_segs[src_sgl_idx].ss_paddr,
1629			    (void*)orig_sgl[src_sgl_idx].ds_addr,
1630			    orig_sgl[src_sgl_idx].ds_len);
1631
1632			bounce_sgl->sg_segs[src_sgl_idx].ss_len =
1633			    orig_sgl[src_sgl_idx].ds_len;
1634		}
1635	}
1636}
1637
1638/**
1639 * @brief copy data from SG list which used as bounce to another SG list
1640 *
1641 * This function is responsible for copy data from one SG list with bounce
1642 * buffer to another SG list's segments.
1643 *
1644 * @param dest_sgl - the destination SG list's segments
1645 * @param dest_sgl_count - the count of destination SG list's segment.
1646 * @param src_sgl - the source SG list.
1647 * @param seg_bits - indicate which segment used bounce buffer of src SG-list.
1648 *
1649 */
1650void
1651storvsc_copy_from_bounce_buf_to_sgl(bus_dma_segment_t *dest_sgl,
1652				    unsigned int dest_sgl_count,
1653				    struct sglist* src_sgl,
1654				    uint64_t seg_bits)
1655{
1656	int sgl_idx = 0;
1657
1658	for (sgl_idx = 0; sgl_idx < dest_sgl_count; sgl_idx++) {
1659		if (seg_bits & (1 << sgl_idx)) {
1660			memcpy((void*)(dest_sgl[sgl_idx].ds_addr),
1661			    (void*)(src_sgl->sg_segs[sgl_idx].ss_paddr),
1662			    src_sgl->sg_segs[sgl_idx].ss_len);
1663		}
1664	}
1665}
1666
1667/**
1668 * @brief check SG list with bounce buffer or not
1669 *
1670 * This function is responsible for check if need bounce buffer for SG list.
1671 *
1672 * @param sgl - the SG list's segments
1673 * @param sg_count - the count of SG list's segment.
1674 * @param bits - segmengs number that need bounce buffer
1675 *
1676 * return -1 if SG list needless bounce buffer
1677 */
1678static int
1679storvsc_check_bounce_buffer_sgl(bus_dma_segment_t *sgl,
1680				unsigned int sg_count,
1681				uint64_t *bits)
1682{
1683	int i = 0;
1684	int offset = 0;
1685	uint64_t phys_addr = 0;
1686	uint64_t tmp_bits = 0;
1687	boolean_t found_hole = FALSE;
1688	boolean_t pre_aligned = TRUE;
1689
1690	if (sg_count < 2){
1691		return -1;
1692	}
1693
1694	*bits = 0;
1695
1696	phys_addr = vtophys(sgl[0].ds_addr);
1697	offset =  phys_addr - trunc_page(phys_addr);
1698
1699	if (offset != 0) {
1700		pre_aligned = FALSE;
1701		tmp_bits |= 1;
1702	}
1703
1704	for (i = 1; i < sg_count; i++) {
1705		phys_addr = vtophys(sgl[i].ds_addr);
1706		offset =  phys_addr - trunc_page(phys_addr);
1707
1708		if (offset == 0) {
1709			if (FALSE == pre_aligned){
1710				/*
1711				 * This segment is aligned, if the previous
1712				 * one is not aligned, find a hole
1713				 */
1714				found_hole = TRUE;
1715			}
1716			pre_aligned = TRUE;
1717		} else {
1718			tmp_bits |= 1 << i;
1719			if (!pre_aligned) {
1720				if (phys_addr != vtophys(sgl[i-1].ds_addr +
1721				    sgl[i-1].ds_len)) {
1722					/*
1723					 * Check whether connect to previous
1724					 * segment,if not, find the hole
1725					 */
1726					found_hole = TRUE;
1727				}
1728			} else {
1729				found_hole = TRUE;
1730			}
1731			pre_aligned = FALSE;
1732		}
1733	}
1734
1735	if (!found_hole) {
1736		return (-1);
1737	} else {
1738		*bits = tmp_bits;
1739		return 0;
1740	}
1741}
1742
1743/**
1744 * @brief Fill in a request structure based on a CAM control block
1745 *
1746 * Fills in a request structure based on the contents of a CAM control
1747 * block.  The request structure holds the payload information for
1748 * VSCSI protocol request.
1749 *
1750 * @param ccb pointer to a CAM contorl block
1751 * @param reqp pointer to a request structure
1752 */
1753static int
1754create_storvsc_request(union ccb *ccb, struct hv_storvsc_request *reqp)
1755{
1756	struct ccb_scsiio *csio = &ccb->csio;
1757	uint64_t phys_addr;
1758	uint32_t bytes_to_copy = 0;
1759	uint32_t pfn_num = 0;
1760	uint32_t pfn;
1761	uint64_t not_aligned_seg_bits = 0;
1762
1763	/* refer to struct vmscsi_req for meanings of these two fields */
1764	reqp->vstor_packet.u.vm_srb.port =
1765		cam_sim_unit(xpt_path_sim(ccb->ccb_h.path));
1766	reqp->vstor_packet.u.vm_srb.path_id =
1767		cam_sim_bus(xpt_path_sim(ccb->ccb_h.path));
1768
1769	reqp->vstor_packet.u.vm_srb.target_id = ccb->ccb_h.target_id;
1770	reqp->vstor_packet.u.vm_srb.lun = ccb->ccb_h.target_lun;
1771
1772	reqp->vstor_packet.u.vm_srb.cdb_len = csio->cdb_len;
1773	if(ccb->ccb_h.flags & CAM_CDB_POINTER) {
1774		memcpy(&reqp->vstor_packet.u.vm_srb.u.cdb, csio->cdb_io.cdb_ptr,
1775			csio->cdb_len);
1776	} else {
1777		memcpy(&reqp->vstor_packet.u.vm_srb.u.cdb, csio->cdb_io.cdb_bytes,
1778			csio->cdb_len);
1779	}
1780
1781	switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
1782	case CAM_DIR_OUT:
1783		reqp->vstor_packet.u.vm_srb.data_in = WRITE_TYPE;
1784		break;
1785	case CAM_DIR_IN:
1786		reqp->vstor_packet.u.vm_srb.data_in = READ_TYPE;
1787		break;
1788	case CAM_DIR_NONE:
1789		reqp->vstor_packet.u.vm_srb.data_in = UNKNOWN_TYPE;
1790		break;
1791	default:
1792		reqp->vstor_packet.u.vm_srb.data_in = UNKNOWN_TYPE;
1793		break;
1794	}
1795
1796	reqp->sense_data     = &csio->sense_data;
1797	reqp->sense_info_len = csio->sense_len;
1798
1799	reqp->ccb = ccb;
1800
1801	if (0 == csio->dxfer_len) {
1802		return (0);
1803	}
1804
1805	reqp->data_buf.length = csio->dxfer_len;
1806
1807	switch (ccb->ccb_h.flags & CAM_DATA_MASK) {
1808	case CAM_DATA_VADDR:
1809	{
1810		bytes_to_copy = csio->dxfer_len;
1811		phys_addr = vtophys(csio->data_ptr);
1812		reqp->data_buf.offset = phys_addr & PAGE_MASK;
1813
1814		while (bytes_to_copy != 0) {
1815			int bytes, page_offset;
1816			phys_addr =
1817			    vtophys(&csio->data_ptr[reqp->data_buf.length -
1818			    bytes_to_copy]);
1819			pfn = phys_addr >> PAGE_SHIFT;
1820			reqp->data_buf.pfn_array[pfn_num] = pfn;
1821			page_offset = phys_addr & PAGE_MASK;
1822
1823			bytes = min(PAGE_SIZE - page_offset, bytes_to_copy);
1824
1825			bytes_to_copy -= bytes;
1826			pfn_num++;
1827		}
1828		break;
1829	}
1830
1831	case CAM_DATA_SG:
1832	{
1833		int i = 0;
1834		int offset = 0;
1835		int ret;
1836
1837		bus_dma_segment_t *storvsc_sglist =
1838		    (bus_dma_segment_t *)ccb->csio.data_ptr;
1839		u_int16_t storvsc_sg_count = ccb->csio.sglist_cnt;
1840
1841		printf("Storvsc: get SG I/O operation, %d\n",
1842		    reqp->vstor_packet.u.vm_srb.data_in);
1843
1844		if (storvsc_sg_count > HV_MAX_MULTIPAGE_BUFFER_COUNT){
1845			printf("Storvsc: %d segments is too much, "
1846			    "only support %d segments\n",
1847			    storvsc_sg_count, HV_MAX_MULTIPAGE_BUFFER_COUNT);
1848			return (EINVAL);
1849		}
1850
1851		/*
1852		 * We create our own bounce buffer function currently. Idealy
1853		 * we should use BUS_DMA(9) framework. But with current BUS_DMA
1854		 * code there is no callback API to check the page alignment of
1855		 * middle segments before busdma can decide if a bounce buffer
1856		 * is needed for particular segment. There is callback,
1857		 * "bus_dma_filter_t *filter", but the parrameters are not
1858		 * sufficient for storvsc driver.
1859		 * TODO:
1860		 *	Add page alignment check in BUS_DMA(9) callback. Once
1861		 *	this is complete, switch the following code to use
1862		 *	BUS_DMA(9) for storvsc bounce buffer support.
1863		 */
1864		/* check if we need to create bounce buffer */
1865		ret = storvsc_check_bounce_buffer_sgl(storvsc_sglist,
1866		    storvsc_sg_count, &not_aligned_seg_bits);
1867		if (ret != -1) {
1868			reqp->bounce_sgl =
1869			    storvsc_create_bounce_buffer(storvsc_sg_count,
1870			    reqp->vstor_packet.u.vm_srb.data_in);
1871			if (NULL == reqp->bounce_sgl) {
1872				printf("Storvsc_error: "
1873				    "create bounce buffer failed.\n");
1874				return (ENOMEM);
1875			}
1876
1877			reqp->bounce_sgl_count = storvsc_sg_count;
1878			reqp->not_aligned_seg_bits = not_aligned_seg_bits;
1879
1880			/*
1881			 * if it is write, we need copy the original data
1882			 *to bounce buffer
1883			 */
1884			if (WRITE_TYPE == reqp->vstor_packet.u.vm_srb.data_in) {
1885				storvsc_copy_sgl_to_bounce_buf(
1886				    reqp->bounce_sgl,
1887				    storvsc_sglist,
1888				    storvsc_sg_count,
1889				    reqp->not_aligned_seg_bits);
1890			}
1891
1892			/* transfer virtual address to physical frame number */
1893			if (reqp->not_aligned_seg_bits & 0x1){
1894 				phys_addr =
1895				    vtophys(reqp->bounce_sgl->sg_segs[0].ss_paddr);
1896			}else{
1897 				phys_addr =
1898					vtophys(storvsc_sglist[0].ds_addr);
1899			}
1900			reqp->data_buf.offset = phys_addr & PAGE_MASK;
1901
1902			pfn = phys_addr >> PAGE_SHIFT;
1903			reqp->data_buf.pfn_array[0] = pfn;
1904
1905			for (i = 1; i < storvsc_sg_count; i++) {
1906				if (reqp->not_aligned_seg_bits & (1 << i)) {
1907					phys_addr =
1908					    vtophys(reqp->bounce_sgl->sg_segs[i].ss_paddr);
1909				} else {
1910					phys_addr =
1911					    vtophys(storvsc_sglist[i].ds_addr);
1912				}
1913
1914				pfn = phys_addr >> PAGE_SHIFT;
1915				reqp->data_buf.pfn_array[i] = pfn;
1916			}
1917		} else {
1918			phys_addr = vtophys(storvsc_sglist[0].ds_addr);
1919
1920			reqp->data_buf.offset = phys_addr & PAGE_MASK;
1921
1922			for (i = 0; i < storvsc_sg_count; i++) {
1923				phys_addr = vtophys(storvsc_sglist[i].ds_addr);
1924				pfn = phys_addr >> PAGE_SHIFT;
1925				reqp->data_buf.pfn_array[i] = pfn;
1926			}
1927
1928			/* check the last segment cross boundary or not */
1929			offset = phys_addr & PAGE_MASK;
1930			if (offset) {
1931				phys_addr =
1932				    vtophys(storvsc_sglist[i-1].ds_addr +
1933				    PAGE_SIZE - offset);
1934				pfn = phys_addr >> PAGE_SHIFT;
1935				reqp->data_buf.pfn_array[i] = pfn;
1936			}
1937
1938			reqp->bounce_sgl_count = 0;
1939		}
1940		break;
1941	}
1942	default:
1943		printf("Unknow flags: %d\n", ccb->ccb_h.flags);
1944		return(EINVAL);
1945	}
1946
1947	return(0);
1948}
1949
1950/**
1951 * @brief completion function before returning to CAM
1952 *
1953 * I/O process has been completed and the result needs
1954 * to be passed to the CAM layer.
1955 * Free resources related to this request.
1956 *
1957 * @param reqp pointer to a request structure
1958 */
1959static void
1960storvsc_io_done(struct hv_storvsc_request *reqp)
1961{
1962	union ccb *ccb = reqp->ccb;
1963	struct ccb_scsiio *csio = &ccb->csio;
1964	struct storvsc_softc *sc = reqp->softc;
1965	struct vmscsi_req *vm_srb = &reqp->vstor_packet.u.vm_srb;
1966	bus_dma_segment_t *ori_sglist = NULL;
1967	int ori_sg_count = 0;
1968	/* destroy bounce buffer if it is used */
1969	if (reqp->bounce_sgl_count) {
1970		ori_sglist = (bus_dma_segment_t *)ccb->csio.data_ptr;
1971		ori_sg_count = ccb->csio.sglist_cnt;
1972
1973		/*
1974		 * If it is READ operation, we should copy back the data
1975		 * to original SG list.
1976		 */
1977		if (READ_TYPE == reqp->vstor_packet.u.vm_srb.data_in) {
1978			storvsc_copy_from_bounce_buf_to_sgl(ori_sglist,
1979			    ori_sg_count,
1980			    reqp->bounce_sgl,
1981			    reqp->not_aligned_seg_bits);
1982		}
1983
1984		storvsc_destroy_bounce_buffer(reqp->bounce_sgl);
1985		reqp->bounce_sgl_count = 0;
1986	}
1987
1988	if (reqp->retries > 0) {
1989		mtx_lock(&sc->hs_lock);
1990#if HVS_TIMEOUT_TEST
1991		xpt_print(ccb->ccb_h.path,
1992			"%u: IO returned after timeout, "
1993			"waking up timer handler if any.\n", ticks);
1994		mtx_lock(&reqp->event.mtx);
1995		cv_signal(&reqp->event.cv);
1996		mtx_unlock(&reqp->event.mtx);
1997#endif
1998		reqp->retries = 0;
1999		xpt_print(ccb->ccb_h.path,
2000			"%u: IO returned after timeout, "
2001			"stopping timer if any.\n", ticks);
2002		mtx_unlock(&sc->hs_lock);
2003	}
2004
2005#ifdef notyet
2006	/*
2007	 * callout_drain() will wait for the timer handler to finish
2008	 * if it is running. So we don't need any lock to synchronize
2009	 * between this routine and the timer handler.
2010	 * Note that we need to make sure reqp is not freed when timer
2011	 * handler is using or will use it.
2012	 */
2013	if (ccb->ccb_h.timeout != CAM_TIME_INFINITY) {
2014		callout_drain(&reqp->callout);
2015	}
2016#endif
2017
2018	ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
2019	ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2020	if (vm_srb->scsi_status == SCSI_STATUS_OK) {
2021		const struct scsi_generic *cmd;
2022
2023		if (vm_srb->srb_status != SRB_STATUS_SUCCESS) {
2024			if (vm_srb->srb_status == SRB_STATUS_INVALID_LUN) {
2025				xpt_print(ccb->ccb_h.path, "invalid LUN %d\n",
2026				    vm_srb->lun);
2027			} else {
2028				xpt_print(ccb->ccb_h.path, "Unknown SRB flag: %d\n",
2029				    vm_srb->srb_status);
2030			}
2031			/*
2032			 * If there are errors, for example, invalid LUN,
2033			 * host will inform VM through SRB status.
2034			 */
2035			ccb->ccb_h.status |= CAM_SEL_TIMEOUT;
2036		} else {
2037			ccb->ccb_h.status |= CAM_REQ_CMP;
2038		}
2039
2040		cmd = (const struct scsi_generic *)
2041		    ((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
2042		     csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes);
2043		if (cmd->opcode == INQUIRY) {
2044			struct scsi_inquiry_data *inq_data =
2045			    (struct scsi_inquiry_data *)csio->data_ptr;
2046			uint8_t *resp_buf = (uint8_t *)csio->data_ptr;
2047			int resp_xfer_len, resp_buf_len, data_len;
2048
2049			/* Get the buffer length reported by host */
2050			resp_xfer_len = vm_srb->transfer_len;
2051			/* Get the available buffer length */
2052			resp_buf_len = resp_xfer_len >= 5 ? resp_buf[4] + 5 : 0;
2053			data_len = (resp_buf_len < resp_xfer_len) ?
2054			    resp_buf_len : resp_xfer_len;
2055
2056			if (bootverbose && data_len >= 5) {
2057				xpt_print(ccb->ccb_h.path, "storvsc inquiry "
2058				    "(%d) [%x %x %x %x %x ... ]\n", data_len,
2059				    resp_buf[0], resp_buf[1], resp_buf[2],
2060				    resp_buf[3], resp_buf[4]);
2061			}
2062			if (vm_srb->srb_status == SRB_STATUS_SUCCESS &&
2063			    data_len > SHORT_INQUIRY_LENGTH) {
2064				char vendor[16];
2065
2066				cam_strvis(vendor, inq_data->vendor,
2067				    sizeof(inq_data->vendor), sizeof(vendor));
2068
2069				/*
2070				 * XXX: Upgrade SPC2 to SPC3 if host is WIN8 or
2071				 * WIN2012 R2 in order to support UNMAP feature.
2072				 */
2073				if (!strncmp(vendor, "Msft", 4) &&
2074				    SID_ANSI_REV(inq_data) == SCSI_REV_SPC2 &&
2075				    (vmstor_proto_version ==
2076				     VMSTOR_PROTOCOL_VERSION_WIN8_1 ||
2077				     vmstor_proto_version ==
2078				     VMSTOR_PROTOCOL_VERSION_WIN8)) {
2079					inq_data->version = SCSI_REV_SPC3;
2080					if (bootverbose) {
2081						xpt_print(ccb->ccb_h.path,
2082						    "storvsc upgrades "
2083						    "SPC2 to SPC3\n");
2084					}
2085				}
2086			}
2087		}
2088	} else {
2089		mtx_lock(&sc->hs_lock);
2090		xpt_print(ccb->ccb_h.path,
2091			"storvsc scsi_status = %d\n",
2092			vm_srb->scsi_status);
2093		mtx_unlock(&sc->hs_lock);
2094		ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
2095	}
2096
2097	ccb->csio.scsi_status = (vm_srb->scsi_status & 0xFF);
2098	ccb->csio.resid = ccb->csio.dxfer_len - vm_srb->transfer_len;
2099
2100	if (reqp->sense_info_len != 0) {
2101		csio->sense_resid = csio->sense_len - reqp->sense_info_len;
2102		ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
2103	}
2104
2105	mtx_lock(&sc->hs_lock);
2106	if (reqp->softc->hs_frozen == 1) {
2107		xpt_print(ccb->ccb_h.path,
2108			"%u: storvsc unfreezing softc 0x%p.\n",
2109			ticks, reqp->softc);
2110		ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
2111		reqp->softc->hs_frozen = 0;
2112	}
2113	storvsc_free_request(sc, reqp);
2114	mtx_unlock(&sc->hs_lock);
2115
2116	xpt_done_direct(ccb);
2117}
2118
2119/**
2120 * @brief Free a request structure
2121 *
2122 * Free a request structure by returning it to the free list
2123 *
2124 * @param sc pointer to a softc
2125 * @param reqp pointer to a request structure
2126 */
2127static void
2128storvsc_free_request(struct storvsc_softc *sc, struct hv_storvsc_request *reqp)
2129{
2130
2131	LIST_INSERT_HEAD(&sc->hs_free_list, reqp, link);
2132}
2133
2134/**
2135 * @brief Determine type of storage device from GUID
2136 *
2137 * Using the type GUID, determine if this is a StorVSC (paravirtual
2138 * SCSI or BlkVSC (paravirtual IDE) device.
2139 *
2140 * @param dev a device
2141 * returns an enum
2142 */
2143static enum hv_storage_type
2144storvsc_get_storage_type(device_t dev)
2145{
2146	const char *p = vmbus_get_type(dev);
2147
2148	if (!memcmp(p, &gBlkVscDeviceType, sizeof(hv_guid))) {
2149		return DRIVER_BLKVSC;
2150	} else if (!memcmp(p, &gStorVscDeviceType, sizeof(hv_guid))) {
2151		return DRIVER_STORVSC;
2152	}
2153	return (DRIVER_UNKNOWN);
2154}
2155
2156