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