2 * Copyright (c) 2009-2012,2016 Microsoft Corp.
3 * Copyright (c) 2012 NetApp Inc.
4 * Copyright (c) 2012 Citrix Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
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.
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.
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.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
40 #include <sys/condvar.h>
42 #include <sys/systm.h>
43 #include <sys/sysctl.h>
44 #include <sys/sockio.h>
46 #include <sys/malloc.h>
47 #include <sys/module.h>
48 #include <sys/kernel.h>
49 #include <sys/queue.h>
52 #include <sys/taskqueue.h>
54 #include <sys/mutex.h>
55 #include <sys/callout.h>
62 #include <sys/sglist.h>
63 #include <machine/bus.h>
64 #include <sys/bus_dma.h>
67 #include <cam/cam_ccb.h>
68 #include <cam/cam_periph.h>
69 #include <cam/cam_sim.h>
70 #include <cam/cam_xpt_sim.h>
71 #include <cam/cam_xpt_internal.h>
72 #include <cam/cam_debug.h>
73 #include <cam/scsi/scsi_all.h>
74 #include <cam/scsi/scsi_message.h>
76 #include <dev/hyperv/include/hyperv.h>
77 #include <dev/hyperv/include/vmbus.h>
79 #include "hv_vstorage.h"
82 #define STORVSC_RINGBUFFER_SIZE (20*PAGE_SIZE)
83 #define STORVSC_MAX_LUNS_PER_TARGET (64)
84 #define STORVSC_MAX_IO_REQUESTS (STORVSC_MAX_LUNS_PER_TARGET * 2)
85 #define BLKVSC_MAX_IDE_DISKS_PER_TARGET (1)
86 #define BLKVSC_MAX_IO_REQUESTS STORVSC_MAX_IO_REQUESTS
87 #define STORVSC_MAX_TARGETS (2)
89 #define VSTOR_PKT_SIZE (sizeof(struct vstor_packet) - vmscsi_size_delta)
91 #define STORVSC_DATA_SEGCNT_MAX VMBUS_CHAN_PRPLIST_MAX
92 #define STORVSC_DATA_SEGSZ_MAX PAGE_SIZE
93 #define STORVSC_DATA_SIZE_MAX \
94 (STORVSC_DATA_SEGCNT_MAX * STORVSC_DATA_SEGSZ_MAX)
99 LIST_ENTRY(hv_sgl_node) link;
100 struct sglist *sgl_data;
103 struct hv_sgl_page_pool{
104 LIST_HEAD(, hv_sgl_node) in_use_sgl_list;
105 LIST_HEAD(, hv_sgl_node) free_sgl_list;
107 } g_hv_sgl_page_pool;
109 #define STORVSC_MAX_SG_PAGE_CNT STORVSC_MAX_IO_REQUESTS * STORVSC_DATA_SEGCNT_MAX
111 enum storvsc_request_type {
117 SYSCTL_NODE(_hw, OID_AUTO, storvsc, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
118 "Hyper-V storage interface");
120 static u_int hv_storvsc_use_pim_unmapped = 1;
121 SYSCTL_INT(_hw_storvsc, OID_AUTO, use_pim_unmapped, CTLFLAG_RDTUN,
122 &hv_storvsc_use_pim_unmapped, 0,
123 "Optimize storvsc by using unmapped I/O");
125 struct hv_storvsc_sysctl {
127 u_long data_vaddr_cnt;
131 struct storvsc_gpa_range {
132 struct vmbus_gpa_range gpa_range;
133 uint64_t gpa_page[STORVSC_DATA_SEGCNT_MAX];
136 struct hv_storvsc_request {
137 LIST_ENTRY(hv_storvsc_request) link;
138 struct vstor_packet vstor_packet;
140 struct storvsc_gpa_range prp_list;
142 uint8_t sense_info_len;
145 struct storvsc_softc *softc;
146 struct callout callout;
147 struct sema synch_sema; /*Synchronize the request/response if needed */
148 struct sglist *bounce_sgl;
149 unsigned int bounce_sgl_count;
150 uint64_t not_aligned_seg_bits;
151 bus_dmamap_t data_dmap;
154 struct storvsc_softc {
155 struct vmbus_channel *hs_chan;
156 LIST_HEAD(, hv_storvsc_request) hs_free_list;
158 struct storvsc_driver_props *hs_drv_props;
161 struct cam_sim *hs_sim;
162 struct cam_path *hs_path;
163 uint32_t hs_num_out_reqs;
164 boolean_t hs_destroy;
165 boolean_t hs_drain_notify;
166 struct sema hs_drain_sema;
167 struct hv_storvsc_request hs_init_req;
168 struct hv_storvsc_request hs_reset_req;
170 bus_dma_tag_t storvsc_req_dtag;
171 struct hv_storvsc_sysctl sysctl_data;
173 struct vmbus_channel *hs_cpu2chan[MAXCPU];
178 * HyperV storvsc timeout testing cases:
179 * a. IO returned after first timeout;
180 * b. IO returned after second timeout and queue freeze;
181 * c. IO returned while timer handler is running
182 * The first can be tested by "sg_senddiag -vv /dev/daX",
183 * and the second and third can be done by
184 * "sg_wr_mode -v -p 08 -c 0,1a -m 0,ff /dev/daX".
186 #define HVS_TIMEOUT_TEST 0
189 * Bus/adapter reset functionality on the Hyper-V host is
190 * buggy and it will be disabled until
191 * it can be further tested.
193 #define HVS_HOST_RESET 0
195 struct storvsc_driver_props {
198 uint8_t drv_max_luns_per_target;
199 uint8_t drv_max_ios_per_target;
200 uint32_t drv_ringbuffer_size;
203 enum hv_storage_type {
209 #define HS_MAX_ADAPTERS 10
211 #define HV_STORAGE_SUPPORTS_MULTI_CHANNEL 0x1
213 /* {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f} */
214 static const struct hyperv_guid gStorVscDeviceType={
215 .hv_guid = {0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
216 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f}
219 /* {32412632-86cb-44a2-9b5c-50d1417354f5} */
220 static const struct hyperv_guid gBlkVscDeviceType={
221 .hv_guid = {0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44,
222 0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5}
225 static struct storvsc_driver_props g_drv_props_table[] = {
226 {"blkvsc", "Hyper-V IDE Storage Interface",
227 BLKVSC_MAX_IDE_DISKS_PER_TARGET, BLKVSC_MAX_IO_REQUESTS,
228 STORVSC_RINGBUFFER_SIZE},
229 {"storvsc", "Hyper-V SCSI Storage Interface",
230 STORVSC_MAX_LUNS_PER_TARGET, STORVSC_MAX_IO_REQUESTS,
231 STORVSC_RINGBUFFER_SIZE}
235 * Sense buffer size changed in win8; have a run-time
236 * variable to track the size we should use.
238 static int sense_buffer_size = PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE;
241 * The size of the vmscsi_request has changed in win8. The
242 * additional size is for the newly added elements in the
243 * structure. These elements are valid only when we are talking
245 * Track the correct size we need to apply.
247 static int vmscsi_size_delta;
249 * The storage protocol version is determined during the
250 * initial exchange with the host. It will indicate which
251 * storage functionality is available in the host.
253 static int vmstor_proto_version;
255 struct vmstor_proto {
257 int sense_buffer_size;
258 int vmscsi_size_delta;
261 static const struct vmstor_proto vmstor_proto_list[] = {
263 VMSTOR_PROTOCOL_VERSION_WIN10,
264 POST_WIN7_STORVSC_SENSE_BUFFER_SIZE,
268 VMSTOR_PROTOCOL_VERSION_WIN8_1,
269 POST_WIN7_STORVSC_SENSE_BUFFER_SIZE,
273 VMSTOR_PROTOCOL_VERSION_WIN8,
274 POST_WIN7_STORVSC_SENSE_BUFFER_SIZE,
278 VMSTOR_PROTOCOL_VERSION_WIN7,
279 PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE,
280 sizeof(struct vmscsi_win8_extension),
283 VMSTOR_PROTOCOL_VERSION_WIN6,
284 PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE,
285 sizeof(struct vmscsi_win8_extension),
289 /* static functions */
290 static int storvsc_probe(device_t dev);
291 static int storvsc_attach(device_t dev);
292 static int storvsc_detach(device_t dev);
293 static void storvsc_poll(struct cam_sim * sim);
294 static void storvsc_action(struct cam_sim * sim, union ccb * ccb);
295 static int create_storvsc_request(union ccb *ccb, struct hv_storvsc_request *reqp);
296 static void storvsc_free_request(struct storvsc_softc *sc, struct hv_storvsc_request *reqp);
297 static enum hv_storage_type storvsc_get_storage_type(device_t dev);
298 static void hv_storvsc_rescan_target(struct storvsc_softc *sc);
299 static void hv_storvsc_on_channel_callback(struct vmbus_channel *chan, void *xsc);
300 static void hv_storvsc_on_iocompletion( struct storvsc_softc *sc,
301 struct vstor_packet *vstor_packet,
302 struct hv_storvsc_request *request);
303 static int hv_storvsc_connect_vsp(struct storvsc_softc *);
304 static void storvsc_io_done(struct hv_storvsc_request *reqp);
305 static void storvsc_copy_sgl_to_bounce_buf(struct sglist *bounce_sgl,
306 bus_dma_segment_t *orig_sgl,
307 unsigned int orig_sgl_count,
309 void storvsc_copy_from_bounce_buf_to_sgl(bus_dma_segment_t *dest_sgl,
310 unsigned int dest_sgl_count,
311 struct sglist* src_sgl,
314 static device_method_t storvsc_methods[] = {
315 /* Device interface */
316 DEVMETHOD(device_probe, storvsc_probe),
317 DEVMETHOD(device_attach, storvsc_attach),
318 DEVMETHOD(device_detach, storvsc_detach),
319 DEVMETHOD(device_shutdown, bus_generic_shutdown),
323 static driver_t storvsc_driver = {
324 "storvsc", storvsc_methods, sizeof(struct storvsc_softc),
327 static devclass_t storvsc_devclass;
328 DRIVER_MODULE(storvsc, vmbus, storvsc_driver, storvsc_devclass, 0, 0);
329 MODULE_VERSION(storvsc, 1);
330 MODULE_DEPEND(storvsc, vmbus, 1, 1, 1);
333 storvsc_subchan_attach(struct storvsc_softc *sc,
334 struct vmbus_channel *new_channel)
336 struct vmstor_chan_props props;
339 memset(&props, 0, sizeof(props));
341 vmbus_chan_cpu_rr(new_channel);
342 ret = vmbus_chan_open(new_channel,
343 sc->hs_drv_props->drv_ringbuffer_size,
344 sc->hs_drv_props->drv_ringbuffer_size,
346 sizeof(struct vmstor_chan_props),
347 hv_storvsc_on_channel_callback, sc);
351 * @brief Send multi-channel creation request to host
353 * @param device a Hyper-V device pointer
354 * @param max_chans the max channels supported by vmbus
357 storvsc_send_multichannel_request(struct storvsc_softc *sc, int max_chans)
359 struct vmbus_channel **subchan;
360 struct hv_storvsc_request *request;
361 struct vstor_packet *vstor_packet;
362 int request_channels_cnt = 0;
365 /* get multichannels count that need to create */
366 request_channels_cnt = MIN(max_chans, mp_ncpus);
368 request = &sc->hs_init_req;
370 /* request the host to create multi-channel */
371 memset(request, 0, sizeof(struct hv_storvsc_request));
373 sema_init(&request->synch_sema, 0, ("stor_synch_sema"));
375 vstor_packet = &request->vstor_packet;
377 vstor_packet->operation = VSTOR_OPERATION_CREATE_MULTI_CHANNELS;
378 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
379 vstor_packet->u.multi_channels_cnt = request_channels_cnt;
381 ret = vmbus_chan_send(sc->hs_chan,
382 VMBUS_CHANPKT_TYPE_INBAND, VMBUS_CHANPKT_FLAG_RC,
383 vstor_packet, VSTOR_PKT_SIZE, (uint64_t)(uintptr_t)request);
385 /* wait for 5 seconds */
386 ret = sema_timedwait(&request->synch_sema, 5 * hz);
388 printf("Storvsc_error: create multi-channel timeout, %d\n",
393 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETEIO ||
394 vstor_packet->status != 0) {
395 printf("Storvsc_error: create multi-channel invalid operation "
396 "(%d) or statue (%u)\n",
397 vstor_packet->operation, vstor_packet->status);
401 /* Wait for sub-channels setup to complete. */
402 subchan = vmbus_subchan_get(sc->hs_chan, request_channels_cnt);
404 /* Attach the sub-channels. */
405 for (i = 0; i < request_channels_cnt; ++i)
406 storvsc_subchan_attach(sc, subchan[i]);
408 /* Release the sub-channels. */
409 vmbus_subchan_rel(subchan, request_channels_cnt);
412 printf("Storvsc create multi-channel success!\n");
416 * @brief initialize channel connection to parent partition
418 * @param dev a Hyper-V device pointer
419 * @returns 0 on success, non-zero error on failure
422 hv_storvsc_channel_init(struct storvsc_softc *sc)
425 struct hv_storvsc_request *request;
426 struct vstor_packet *vstor_packet;
427 uint16_t max_chans = 0;
428 boolean_t support_multichannel = FALSE;
432 support_multichannel = FALSE;
434 request = &sc->hs_init_req;
435 memset(request, 0, sizeof(struct hv_storvsc_request));
436 vstor_packet = &request->vstor_packet;
440 * Initiate the vsc/vsp initialization protocol on the open channel
442 sema_init(&request->synch_sema, 0, ("stor_synch_sema"));
444 vstor_packet->operation = VSTOR_OPERATION_BEGININITIALIZATION;
445 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
448 ret = vmbus_chan_send(sc->hs_chan,
449 VMBUS_CHANPKT_TYPE_INBAND, VMBUS_CHANPKT_FLAG_RC,
450 vstor_packet, VSTOR_PKT_SIZE, (uint64_t)(uintptr_t)request);
456 ret = sema_timedwait(&request->synch_sema, 5 * hz);
460 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETEIO ||
461 vstor_packet->status != 0) {
465 for (i = 0; i < nitems(vmstor_proto_list); i++) {
466 /* reuse the packet for version range supported */
468 memset(vstor_packet, 0, sizeof(struct vstor_packet));
469 vstor_packet->operation = VSTOR_OPERATION_QUERYPROTOCOLVERSION;
470 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
472 vstor_packet->u.version.major_minor =
473 vmstor_proto_list[i].proto_version;
475 /* revision is only significant for Windows guests */
476 vstor_packet->u.version.revision = 0;
478 ret = vmbus_chan_send(sc->hs_chan,
479 VMBUS_CHANPKT_TYPE_INBAND, VMBUS_CHANPKT_FLAG_RC,
480 vstor_packet, VSTOR_PKT_SIZE, (uint64_t)(uintptr_t)request);
486 ret = sema_timedwait(&request->synch_sema, 5 * hz);
491 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETEIO) {
495 if (vstor_packet->status == 0) {
496 vmstor_proto_version =
497 vmstor_proto_list[i].proto_version;
499 vmstor_proto_list[i].sense_buffer_size;
501 vmstor_proto_list[i].vmscsi_size_delta;
506 if (vstor_packet->status != 0) {
511 * Query channel properties
513 memset(vstor_packet, 0, sizeof(struct vstor_packet));
514 vstor_packet->operation = VSTOR_OPERATION_QUERYPROPERTIES;
515 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
517 ret = vmbus_chan_send(sc->hs_chan,
518 VMBUS_CHANPKT_TYPE_INBAND, VMBUS_CHANPKT_FLAG_RC,
519 vstor_packet, VSTOR_PKT_SIZE, (uint64_t)(uintptr_t)request);
525 ret = sema_timedwait(&request->synch_sema, 5 * hz);
530 /* TODO: Check returned version */
531 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETEIO ||
532 vstor_packet->status != 0) {
536 /* multi-channels feature is supported by WIN8 and above version */
537 max_chans = vstor_packet->u.chan_props.max_channel_cnt;
538 version = VMBUS_GET_VERSION(device_get_parent(sc->hs_dev), sc->hs_dev);
539 if (version != VMBUS_VERSION_WIN7 && version != VMBUS_VERSION_WS2008 &&
540 (vstor_packet->u.chan_props.flags &
541 HV_STORAGE_SUPPORTS_MULTI_CHANNEL)) {
542 support_multichannel = TRUE;
545 memset(vstor_packet, 0, sizeof(struct vstor_packet));
546 vstor_packet->operation = VSTOR_OPERATION_ENDINITIALIZATION;
547 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
549 ret = vmbus_chan_send(sc->hs_chan,
550 VMBUS_CHANPKT_TYPE_INBAND, VMBUS_CHANPKT_FLAG_RC,
551 vstor_packet, VSTOR_PKT_SIZE, (uint64_t)(uintptr_t)request);
558 ret = sema_timedwait(&request->synch_sema, 5 * hz);
563 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETEIO ||
564 vstor_packet->status != 0)
568 * If multi-channel is supported, send multichannel create
571 if (support_multichannel)
572 storvsc_send_multichannel_request(sc, max_chans);
575 sema_destroy(&request->synch_sema);
580 * @brief Open channel connection to paraent partition StorVSP driver
582 * Open and initialize channel connection to parent partition StorVSP driver.
584 * @param pointer to a Hyper-V device
585 * @returns 0 on success, non-zero error on failure
588 hv_storvsc_connect_vsp(struct storvsc_softc *sc)
591 struct vmstor_chan_props props;
593 memset(&props, 0, sizeof(struct vmstor_chan_props));
598 vmbus_chan_cpu_rr(sc->hs_chan);
599 ret = vmbus_chan_open(
601 sc->hs_drv_props->drv_ringbuffer_size,
602 sc->hs_drv_props->drv_ringbuffer_size,
604 sizeof(struct vmstor_chan_props),
605 hv_storvsc_on_channel_callback, sc);
611 ret = hv_storvsc_channel_init(sc);
618 hv_storvsc_host_reset(struct storvsc_softc *sc)
622 struct hv_storvsc_request *request;
623 struct vstor_packet *vstor_packet;
625 request = &sc->hs_reset_req;
627 vstor_packet = &request->vstor_packet;
629 sema_init(&request->synch_sema, 0, "stor synch sema");
631 vstor_packet->operation = VSTOR_OPERATION_RESETBUS;
632 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
634 ret = vmbus_chan_send(dev->channel,
635 VMBUS_CHANPKT_TYPE_INBAND, VMBUS_CHANPKT_FLAG_RC,
636 vstor_packet, VSTOR_PKT_SIZE,
637 (uint64_t)(uintptr_t)&sc->hs_reset_req);
643 ret = sema_timedwait(&request->synch_sema, 5 * hz); /* KYS 5 seconds */
651 * At this point, all outstanding requests in the adapter
652 * should have been flushed out and return to us
656 sema_destroy(&request->synch_sema);
659 #endif /* HVS_HOST_RESET */
662 * @brief Function to initiate an I/O request
664 * @param device Hyper-V device pointer
665 * @param request pointer to a request structure
666 * @returns 0 on success, non-zero error on failure
669 hv_storvsc_io_request(struct storvsc_softc *sc,
670 struct hv_storvsc_request *request)
672 struct vstor_packet *vstor_packet = &request->vstor_packet;
673 struct vmbus_channel* outgoing_channel = NULL;
676 vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
678 vstor_packet->u.vm_srb.length =
679 sizeof(struct vmscsi_req) - vmscsi_size_delta;
681 vstor_packet->u.vm_srb.sense_info_len = sense_buffer_size;
683 vstor_packet->u.vm_srb.transfer_len =
684 request->prp_list.gpa_range.gpa_len;
686 vstor_packet->operation = VSTOR_OPERATION_EXECUTESRB;
688 outgoing_channel = sc->hs_cpu2chan[curcpu];
690 mtx_unlock(&request->softc->hs_lock);
691 if (request->prp_list.gpa_range.gpa_len) {
692 ret = vmbus_chan_send_prplist(outgoing_channel,
693 &request->prp_list.gpa_range, request->prp_cnt,
694 vstor_packet, VSTOR_PKT_SIZE, (uint64_t)(uintptr_t)request);
696 ret = vmbus_chan_send(outgoing_channel,
697 VMBUS_CHANPKT_TYPE_INBAND, VMBUS_CHANPKT_FLAG_RC,
698 vstor_packet, VSTOR_PKT_SIZE, (uint64_t)(uintptr_t)request);
700 mtx_lock(&request->softc->hs_lock);
703 printf("Unable to send packet %p ret %d", vstor_packet, ret);
705 atomic_add_int(&sc->hs_num_out_reqs, 1);
713 * Process IO_COMPLETION_OPERATION and ready
714 * the result to be completed for upper layer
715 * processing by the CAM layer.
718 hv_storvsc_on_iocompletion(struct storvsc_softc *sc,
719 struct vstor_packet *vstor_packet,
720 struct hv_storvsc_request *request)
722 struct vmscsi_req *vm_srb;
724 vm_srb = &vstor_packet->u.vm_srb;
727 * Copy some fields of the host's response into the request structure,
728 * because the fields will be used later in storvsc_io_done().
730 request->vstor_packet.u.vm_srb.scsi_status = vm_srb->scsi_status;
731 request->vstor_packet.u.vm_srb.srb_status = vm_srb->srb_status;
732 request->vstor_packet.u.vm_srb.transfer_len = vm_srb->transfer_len;
734 if (((vm_srb->scsi_status & 0xFF) == SCSI_STATUS_CHECK_COND) &&
735 (vm_srb->srb_status & SRB_STATUS_AUTOSENSE_VALID)) {
736 /* Autosense data available */
738 KASSERT(vm_srb->sense_info_len <= request->sense_info_len,
739 ("vm_srb->sense_info_len <= "
740 "request->sense_info_len"));
742 memcpy(request->sense_data, vm_srb->u.sense_data,
743 vm_srb->sense_info_len);
745 request->sense_info_len = vm_srb->sense_info_len;
748 /* Complete request by passing to the CAM layer */
749 storvsc_io_done(request);
750 atomic_subtract_int(&sc->hs_num_out_reqs, 1);
751 if (sc->hs_drain_notify && (sc->hs_num_out_reqs == 0)) {
752 sema_post(&sc->hs_drain_sema);
757 hv_storvsc_rescan_target(struct storvsc_softc *sc)
760 target_id_t targetid;
763 pathid = cam_sim_path(sc->hs_sim);
764 targetid = CAM_TARGET_WILDCARD;
767 * Allocate a CCB and schedule a rescan.
769 ccb = xpt_alloc_ccb_nowait();
771 printf("unable to alloc CCB for rescan\n");
775 if (xpt_create_path(&ccb->ccb_h.path, NULL, pathid, targetid,
776 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
777 printf("unable to create path for rescan, pathid: %u,"
778 "targetid: %u\n", pathid, targetid);
783 if (targetid == CAM_TARGET_WILDCARD)
784 ccb->ccb_h.func_code = XPT_SCAN_BUS;
786 ccb->ccb_h.func_code = XPT_SCAN_TGT;
792 hv_storvsc_on_channel_callback(struct vmbus_channel *channel, void *xsc)
795 struct storvsc_softc *sc = xsc;
796 uint32_t bytes_recvd;
798 uint8_t packet[roundup2(sizeof(struct vstor_packet), 8)];
799 struct hv_storvsc_request *request;
800 struct vstor_packet *vstor_packet;
802 bytes_recvd = roundup2(VSTOR_PKT_SIZE, 8);
803 ret = vmbus_chan_recv(channel, packet, &bytes_recvd, &request_id);
804 KASSERT(ret != ENOBUFS, ("storvsc recvbuf is not large enough"));
805 /* XXX check bytes_recvd to make sure that it contains enough data */
807 while ((ret == 0) && (bytes_recvd > 0)) {
808 request = (struct hv_storvsc_request *)(uintptr_t)request_id;
810 if ((request == &sc->hs_init_req) ||
811 (request == &sc->hs_reset_req)) {
812 memcpy(&request->vstor_packet, packet,
813 sizeof(struct vstor_packet));
814 sema_post(&request->synch_sema);
816 vstor_packet = (struct vstor_packet *)packet;
817 switch(vstor_packet->operation) {
818 case VSTOR_OPERATION_COMPLETEIO:
820 panic("VMBUS: storvsc received a "
821 "packet with NULL request id in "
822 "COMPLETEIO operation.");
824 hv_storvsc_on_iocompletion(sc,
825 vstor_packet, request);
827 case VSTOR_OPERATION_REMOVEDEVICE:
828 printf("VMBUS: storvsc operation %d not "
829 "implemented.\n", vstor_packet->operation);
830 /* TODO: implement */
832 case VSTOR_OPERATION_ENUMERATE_BUS:
833 hv_storvsc_rescan_target(sc);
840 bytes_recvd = roundup2(VSTOR_PKT_SIZE, 8),
841 ret = vmbus_chan_recv(channel, packet, &bytes_recvd,
843 KASSERT(ret != ENOBUFS,
844 ("storvsc recvbuf is not large enough"));
846 * XXX check bytes_recvd to make sure that it contains
853 * @brief StorVSC probe function
855 * Device probe function. Returns 0 if the input device is a StorVSC
856 * device. Otherwise, a ENXIO is returned. If the input device is
857 * for BlkVSC (paravirtual IDE) device and this support is disabled in
858 * favor of the emulated ATA/IDE device, return ENXIO.
861 * @returns 0 on success, ENXIO if not a matcing StorVSC device
864 storvsc_probe(device_t dev)
866 int ata_disk_enable = 0;
869 switch (storvsc_get_storage_type(dev)) {
872 device_printf(dev, "DRIVER_BLKVSC-Emulated ATA/IDE probe\n");
873 if (!getenv_int("hw.ata.disk_enable", &ata_disk_enable)) {
876 "Enlightened ATA/IDE detected\n");
877 device_set_desc(dev, g_drv_props_table[DRIVER_BLKVSC].drv_desc);
878 ret = BUS_PROBE_DEFAULT;
879 } else if(bootverbose)
880 device_printf(dev, "Emulated ATA/IDE set (hw.ata.disk_enable set)\n");
884 device_printf(dev, "Enlightened SCSI device detected\n");
885 device_set_desc(dev, g_drv_props_table[DRIVER_STORVSC].drv_desc);
886 ret = BUS_PROBE_DEFAULT;
895 storvsc_create_cpu2chan(struct storvsc_softc *sc)
900 sc->hs_cpu2chan[cpu] = vmbus_chan_cpu2chan(sc->hs_chan, cpu);
902 device_printf(sc->hs_dev, "cpu%d -> chan%u\n",
903 cpu, vmbus_chan_id(sc->hs_cpu2chan[cpu]));
909 storvsc_init_requests(device_t dev)
911 struct storvsc_softc *sc = device_get_softc(dev);
912 struct hv_storvsc_request *reqp;
915 LIST_INIT(&sc->hs_free_list);
917 error = bus_dma_tag_create(
918 bus_get_dma_tag(dev), /* parent */
920 PAGE_SIZE, /* boundary */
921 BUS_SPACE_MAXADDR, /* lowaddr */
922 BUS_SPACE_MAXADDR, /* highaddr */
923 NULL, NULL, /* filter, filterarg */
924 STORVSC_DATA_SIZE_MAX, /* maxsize */
925 STORVSC_DATA_SEGCNT_MAX, /* nsegments */
926 STORVSC_DATA_SEGSZ_MAX, /* maxsegsize */
929 NULL, /* lockfuncarg */
930 &sc->storvsc_req_dtag);
932 device_printf(dev, "failed to create storvsc dma tag\n");
936 for (i = 0; i < sc->hs_drv_props->drv_max_ios_per_target; ++i) {
937 reqp = malloc(sizeof(struct hv_storvsc_request),
938 M_DEVBUF, M_WAITOK|M_ZERO);
940 error = bus_dmamap_create(sc->storvsc_req_dtag, 0,
943 device_printf(dev, "failed to allocate storvsc "
947 LIST_INSERT_HEAD(&sc->hs_free_list, reqp, link);
952 while ((reqp = LIST_FIRST(&sc->hs_free_list)) != NULL) {
953 LIST_REMOVE(reqp, link);
954 bus_dmamap_destroy(sc->storvsc_req_dtag, reqp->data_dmap);
955 free(reqp, M_DEVBUF);
961 storvsc_sysctl(device_t dev)
963 struct sysctl_oid_list *child;
964 struct sysctl_ctx_list *ctx;
965 struct storvsc_softc *sc;
967 sc = device_get_softc(dev);
968 ctx = device_get_sysctl_ctx(dev);
969 child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
971 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "data_bio_cnt", CTLFLAG_RW,
972 &sc->sysctl_data.data_bio_cnt, "# of bio data block");
973 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "data_vaddr_cnt", CTLFLAG_RW,
974 &sc->sysctl_data.data_vaddr_cnt, "# of vaddr data block");
975 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "data_sg_cnt", CTLFLAG_RW,
976 &sc->sysctl_data.data_sg_cnt, "# of sg data block");
980 * @brief StorVSC attach function
982 * Function responsible for allocating per-device structures,
983 * setting up CAM interfaces and scanning for available LUNs to
984 * be used for SCSI device peripherals.
987 * @returns 0 on success or an error on failure
990 storvsc_attach(device_t dev)
992 enum hv_storage_type stor_type;
993 struct storvsc_softc *sc;
994 struct cam_devq *devq;
996 struct hv_storvsc_request *reqp;
997 struct root_hold_token *root_mount_token = NULL;
998 struct hv_sgl_node *sgl_node = NULL;
999 void *tmp_buff = NULL;
1002 * We need to serialize storvsc attach calls.
1004 root_mount_token = root_mount_hold("storvsc");
1006 sc = device_get_softc(dev);
1007 sc->hs_chan = vmbus_get_channel(dev);
1009 stor_type = storvsc_get_storage_type(dev);
1011 if (stor_type == DRIVER_UNKNOWN) {
1016 /* fill in driver specific properties */
1017 sc->hs_drv_props = &g_drv_props_table[stor_type];
1019 /* fill in device specific properties */
1020 sc->hs_unit = device_get_unit(dev);
1023 mtx_init(&sc->hs_lock, "hvslck", NULL, MTX_DEF);
1025 ret = storvsc_init_requests(dev);
1029 /* create sg-list page pool */
1030 if (FALSE == g_hv_sgl_page_pool.is_init) {
1031 g_hv_sgl_page_pool.is_init = TRUE;
1032 LIST_INIT(&g_hv_sgl_page_pool.in_use_sgl_list);
1033 LIST_INIT(&g_hv_sgl_page_pool.free_sgl_list);
1036 * Pre-create SG list, each SG list with
1037 * STORVSC_DATA_SEGCNT_MAX segments, each
1038 * segment has one page buffer
1040 for (i = 0; i < STORVSC_MAX_IO_REQUESTS; i++) {
1041 sgl_node = malloc(sizeof(struct hv_sgl_node),
1042 M_DEVBUF, M_WAITOK|M_ZERO);
1044 sgl_node->sgl_data =
1045 sglist_alloc(STORVSC_DATA_SEGCNT_MAX,
1048 for (j = 0; j < STORVSC_DATA_SEGCNT_MAX; j++) {
1049 tmp_buff = malloc(PAGE_SIZE,
1050 M_DEVBUF, M_WAITOK|M_ZERO);
1052 sgl_node->sgl_data->sg_segs[j].ss_paddr =
1053 (vm_paddr_t)tmp_buff;
1056 LIST_INSERT_HEAD(&g_hv_sgl_page_pool.free_sgl_list,
1061 sc->hs_destroy = FALSE;
1062 sc->hs_drain_notify = FALSE;
1063 sema_init(&sc->hs_drain_sema, 0, "Store Drain Sema");
1065 ret = hv_storvsc_connect_vsp(sc);
1070 /* Construct cpu to channel mapping */
1071 storvsc_create_cpu2chan(sc);
1074 * Create the device queue.
1075 * Hyper-V maps each target to one SCSI HBA
1077 devq = cam_simq_alloc(sc->hs_drv_props->drv_max_ios_per_target);
1079 device_printf(dev, "Failed to alloc device queue\n");
1084 sc->hs_sim = cam_sim_alloc(storvsc_action,
1086 sc->hs_drv_props->drv_name,
1090 sc->hs_drv_props->drv_max_ios_per_target,
1093 if (sc->hs_sim == NULL) {
1094 device_printf(dev, "Failed to alloc sim\n");
1095 cam_simq_free(devq);
1100 mtx_lock(&sc->hs_lock);
1101 /* bus_id is set to 0, need to get it from VMBUS channel query? */
1102 if (xpt_bus_register(sc->hs_sim, dev, 0) != CAM_SUCCESS) {
1103 cam_sim_free(sc->hs_sim, /*free_devq*/TRUE);
1104 mtx_unlock(&sc->hs_lock);
1105 device_printf(dev, "Unable to register SCSI bus\n");
1110 if (xpt_create_path(&sc->hs_path, /*periph*/NULL,
1111 cam_sim_path(sc->hs_sim),
1112 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1113 xpt_bus_deregister(cam_sim_path(sc->hs_sim));
1114 cam_sim_free(sc->hs_sim, /*free_devq*/TRUE);
1115 mtx_unlock(&sc->hs_lock);
1116 device_printf(dev, "Unable to create path\n");
1121 mtx_unlock(&sc->hs_lock);
1123 storvsc_sysctl(dev);
1125 root_mount_rel(root_mount_token);
1130 root_mount_rel(root_mount_token);
1131 while (!LIST_EMPTY(&sc->hs_free_list)) {
1132 reqp = LIST_FIRST(&sc->hs_free_list);
1133 LIST_REMOVE(reqp, link);
1134 bus_dmamap_destroy(sc->storvsc_req_dtag, reqp->data_dmap);
1135 free(reqp, M_DEVBUF);
1138 while (!LIST_EMPTY(&g_hv_sgl_page_pool.free_sgl_list)) {
1139 sgl_node = LIST_FIRST(&g_hv_sgl_page_pool.free_sgl_list);
1140 LIST_REMOVE(sgl_node, link);
1141 for (j = 0; j < STORVSC_DATA_SEGCNT_MAX; j++) {
1143 (void*)sgl_node->sgl_data->sg_segs[j].ss_paddr) {
1144 free((void*)sgl_node->sgl_data->sg_segs[j].ss_paddr, M_DEVBUF);
1147 sglist_free(sgl_node->sgl_data);
1148 free(sgl_node, M_DEVBUF);
1155 * @brief StorVSC device detach function
1157 * This function is responsible for safely detaching a
1158 * StorVSC device. This includes waiting for inbound responses
1159 * to complete and freeing associated per-device structures.
1161 * @param dev a device
1162 * returns 0 on success
1165 storvsc_detach(device_t dev)
1167 struct storvsc_softc *sc = device_get_softc(dev);
1168 struct hv_storvsc_request *reqp = NULL;
1169 struct hv_sgl_node *sgl_node = NULL;
1172 sc->hs_destroy = TRUE;
1175 * At this point, all outbound traffic should be disabled. We
1176 * only allow inbound traffic (responses) to proceed so that
1177 * outstanding requests can be completed.
1180 sc->hs_drain_notify = TRUE;
1181 sema_wait(&sc->hs_drain_sema);
1182 sc->hs_drain_notify = FALSE;
1185 * Since we have already drained, we don't need to busy wait.
1186 * The call to close the channel will reset the callback
1187 * under the protection of the incoming channel lock.
1190 vmbus_chan_close(sc->hs_chan);
1192 mtx_lock(&sc->hs_lock);
1193 while (!LIST_EMPTY(&sc->hs_free_list)) {
1194 reqp = LIST_FIRST(&sc->hs_free_list);
1195 LIST_REMOVE(reqp, link);
1196 bus_dmamap_destroy(sc->storvsc_req_dtag, reqp->data_dmap);
1197 free(reqp, M_DEVBUF);
1199 mtx_unlock(&sc->hs_lock);
1201 while (!LIST_EMPTY(&g_hv_sgl_page_pool.free_sgl_list)) {
1202 sgl_node = LIST_FIRST(&g_hv_sgl_page_pool.free_sgl_list);
1203 LIST_REMOVE(sgl_node, link);
1204 for (j = 0; j < STORVSC_DATA_SEGCNT_MAX; j++){
1206 (void*)sgl_node->sgl_data->sg_segs[j].ss_paddr) {
1207 free((void*)sgl_node->sgl_data->sg_segs[j].ss_paddr, M_DEVBUF);
1210 sglist_free(sgl_node->sgl_data);
1211 free(sgl_node, M_DEVBUF);
1217 #if HVS_TIMEOUT_TEST
1219 * @brief unit test for timed out operations
1221 * This function provides unit testing capability to simulate
1222 * timed out operations. Recompilation with HV_TIMEOUT_TEST=1
1225 * @param reqp pointer to a request structure
1226 * @param opcode SCSI operation being performed
1227 * @param wait if 1, wait for I/O to complete
1230 storvsc_timeout_test(struct hv_storvsc_request *reqp,
1231 uint8_t opcode, int wait)
1234 union ccb *ccb = reqp->ccb;
1235 struct storvsc_softc *sc = reqp->softc;
1237 if (reqp->vstor_packet.vm_srb.cdb[0] != opcode) {
1242 mtx_lock(&reqp->event.mtx);
1244 ret = hv_storvsc_io_request(sc, reqp);
1247 mtx_unlock(&reqp->event.mtx);
1249 printf("%s: io_request failed with %d.\n",
1251 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
1252 mtx_lock(&sc->hs_lock);
1253 storvsc_free_request(sc, reqp);
1255 mtx_unlock(&sc->hs_lock);
1260 xpt_print(ccb->ccb_h.path,
1261 "%u: %s: waiting for IO return.\n",
1263 ret = cv_timedwait(&reqp->event.cv, &reqp->event.mtx, 60*hz);
1264 mtx_unlock(&reqp->event.mtx);
1265 xpt_print(ccb->ccb_h.path, "%u: %s: %s.\n",
1266 ticks, __func__, (ret == 0)?
1267 "IO return detected" :
1268 "IO return not detected");
1270 * Now both the timer handler and io done are running
1271 * simultaneously. We want to confirm the io done always
1272 * finishes after the timer handler exits. So reqp used by
1273 * timer handler is not freed or stale. Do busy loop for
1274 * another 1/10 second to make sure io done does
1275 * wait for the timer handler to complete.
1278 mtx_lock(&sc->hs_lock);
1279 xpt_print(ccb->ccb_h.path,
1280 "%u: %s: finishing, queue frozen %d, "
1281 "ccb status 0x%x scsi_status 0x%x.\n",
1282 ticks, __func__, sc->hs_frozen,
1284 ccb->csio.scsi_status);
1285 mtx_unlock(&sc->hs_lock);
1288 #endif /* HVS_TIMEOUT_TEST */
1292 * @brief timeout handler for requests
1294 * This function is called as a result of a callout expiring.
1296 * @param arg pointer to a request
1299 storvsc_timeout(void *arg)
1301 struct hv_storvsc_request *reqp = arg;
1302 struct storvsc_softc *sc = reqp->softc;
1303 union ccb *ccb = reqp->ccb;
1305 if (reqp->retries == 0) {
1306 mtx_lock(&sc->hs_lock);
1307 xpt_print(ccb->ccb_h.path,
1308 "%u: IO timed out (req=0x%p), wait for another %u secs.\n",
1309 ticks, reqp, ccb->ccb_h.timeout / 1000);
1310 cam_error_print(ccb, CAM_ESF_ALL, CAM_EPF_ALL);
1311 mtx_unlock(&sc->hs_lock);
1314 callout_reset_sbt(&reqp->callout, SBT_1MS * ccb->ccb_h.timeout,
1315 0, storvsc_timeout, reqp, 0);
1316 #if HVS_TIMEOUT_TEST
1317 storvsc_timeout_test(reqp, SEND_DIAGNOSTIC, 0);
1322 mtx_lock(&sc->hs_lock);
1323 xpt_print(ccb->ccb_h.path,
1324 "%u: IO (reqp = 0x%p) did not return for %u seconds, %s.\n",
1325 ticks, reqp, ccb->ccb_h.timeout * (reqp->retries+1) / 1000,
1326 (sc->hs_frozen == 0)?
1327 "freezing the queue" : "the queue is already frozen");
1328 if (sc->hs_frozen == 0) {
1330 xpt_freeze_simq(xpt_path_sim(ccb->ccb_h.path), 1);
1332 mtx_unlock(&sc->hs_lock);
1334 #if HVS_TIMEOUT_TEST
1335 storvsc_timeout_test(reqp, MODE_SELECT_10, 1);
1341 * @brief StorVSC device poll function
1343 * This function is responsible for servicing requests when
1344 * interrupts are disabled (i.e when we are dumping core.)
1346 * @param sim a pointer to a CAM SCSI interface module
1349 storvsc_poll(struct cam_sim *sim)
1351 struct storvsc_softc *sc = cam_sim_softc(sim);
1353 mtx_assert(&sc->hs_lock, MA_OWNED);
1354 mtx_unlock(&sc->hs_lock);
1355 hv_storvsc_on_channel_callback(sc->hs_chan, sc);
1356 mtx_lock(&sc->hs_lock);
1360 * @brief StorVSC device action function
1362 * This function is responsible for handling SCSI operations which
1363 * are passed from the CAM layer. The requests are in the form of
1364 * CAM control blocks which indicate the action being performed.
1365 * Not all actions require converting the request to a VSCSI protocol
1366 * message - these actions can be responded to by this driver.
1367 * Requests which are destined for a backend storage device are converted
1368 * to a VSCSI protocol message and sent on the channel connection associated
1371 * @param sim pointer to a CAM SCSI interface module
1372 * @param ccb pointer to a CAM control block
1375 storvsc_action(struct cam_sim *sim, union ccb *ccb)
1377 struct storvsc_softc *sc = cam_sim_softc(sim);
1380 mtx_assert(&sc->hs_lock, MA_OWNED);
1381 switch (ccb->ccb_h.func_code) {
1382 case XPT_PATH_INQ: {
1383 struct ccb_pathinq *cpi = &ccb->cpi;
1385 cpi->version_num = 1;
1386 cpi->hba_inquiry = PI_TAG_ABLE|PI_SDTR_ABLE;
1387 cpi->target_sprt = 0;
1388 cpi->hba_misc = PIM_NOBUSRESET;
1389 if (hv_storvsc_use_pim_unmapped)
1390 cpi->hba_misc |= PIM_UNMAPPED;
1391 cpi->hba_eng_cnt = 0;
1392 cpi->max_target = STORVSC_MAX_TARGETS;
1393 cpi->max_lun = sc->hs_drv_props->drv_max_luns_per_target;
1394 cpi->initiator_id = cpi->max_target;
1395 cpi->bus_id = cam_sim_bus(sim);
1396 cpi->base_transfer_speed = 300000;
1397 cpi->transport = XPORT_SAS;
1398 cpi->transport_version = 0;
1399 cpi->protocol = PROTO_SCSI;
1400 cpi->protocol_version = SCSI_REV_SPC2;
1401 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1402 strncpy(cpi->hba_vid, sc->hs_drv_props->drv_name, HBA_IDLEN);
1403 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1404 cpi->unit_number = cam_sim_unit(sim);
1406 ccb->ccb_h.status = CAM_REQ_CMP;
1410 case XPT_GET_TRAN_SETTINGS: {
1411 struct ccb_trans_settings *cts = &ccb->cts;
1413 cts->transport = XPORT_SAS;
1414 cts->transport_version = 0;
1415 cts->protocol = PROTO_SCSI;
1416 cts->protocol_version = SCSI_REV_SPC2;
1418 /* enable tag queuing and disconnected mode */
1419 cts->proto_specific.valid = CTS_SCSI_VALID_TQ;
1420 cts->proto_specific.scsi.valid = CTS_SCSI_VALID_TQ;
1421 cts->proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB;
1422 cts->xport_specific.valid = CTS_SPI_VALID_DISC;
1423 cts->xport_specific.spi.flags = CTS_SPI_FLAGS_DISC_ENB;
1425 ccb->ccb_h.status = CAM_REQ_CMP;
1429 case XPT_SET_TRAN_SETTINGS: {
1430 ccb->ccb_h.status = CAM_REQ_CMP;
1434 case XPT_CALC_GEOMETRY:{
1435 cam_calc_geometry(&ccb->ccg, 1);
1440 case XPT_RESET_DEV:{
1442 if ((res = hv_storvsc_host_reset(sc)) != 0) {
1443 xpt_print(ccb->ccb_h.path,
1444 "hv_storvsc_host_reset failed with %d\n", res);
1445 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
1449 ccb->ccb_h.status = CAM_REQ_CMP;
1453 xpt_print(ccb->ccb_h.path,
1454 "%s reset not supported.\n",
1455 (ccb->ccb_h.func_code == XPT_RESET_BUS)?
1457 ccb->ccb_h.status = CAM_REQ_INVALID;
1460 #endif /* HVS_HOST_RESET */
1463 case XPT_IMMED_NOTIFY: {
1464 struct hv_storvsc_request *reqp = NULL;
1465 bus_dmamap_t dmap_saved;
1467 if (ccb->csio.cdb_len == 0) {
1468 panic("cdl_len is 0\n");
1471 if (LIST_EMPTY(&sc->hs_free_list)) {
1472 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1473 if (sc->hs_frozen == 0) {
1475 xpt_freeze_simq(sim, /* count*/1);
1481 reqp = LIST_FIRST(&sc->hs_free_list);
1482 LIST_REMOVE(reqp, link);
1484 /* Save the data_dmap before reset request */
1485 dmap_saved = reqp->data_dmap;
1487 /* XXX this is ugly */
1488 bzero(reqp, sizeof(struct hv_storvsc_request));
1490 /* Restore necessary bits */
1491 reqp->data_dmap = dmap_saved;
1494 ccb->ccb_h.status |= CAM_SIM_QUEUED;
1495 if ((res = create_storvsc_request(ccb, reqp)) != 0) {
1496 ccb->ccb_h.status = CAM_REQ_INVALID;
1502 if (ccb->ccb_h.timeout != CAM_TIME_INFINITY) {
1503 callout_init(&reqp->callout, CALLOUT_MPSAFE);
1504 callout_reset_sbt(&reqp->callout,
1505 SBT_1MS * ccb->ccb_h.timeout, 0,
1506 storvsc_timeout, reqp, 0);
1507 #if HVS_TIMEOUT_TEST
1508 cv_init(&reqp->event.cv, "storvsc timeout cv");
1509 mtx_init(&reqp->event.mtx, "storvsc timeout mutex",
1511 switch (reqp->vstor_packet.vm_srb.cdb[0]) {
1512 case MODE_SELECT_10:
1513 case SEND_DIAGNOSTIC:
1514 /* To have timer send the request. */
1519 #endif /* HVS_TIMEOUT_TEST */
1523 if ((res = hv_storvsc_io_request(sc, reqp)) != 0) {
1524 xpt_print(ccb->ccb_h.path,
1525 "hv_storvsc_io_request failed with %d\n", res);
1526 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
1527 storvsc_free_request(sc, reqp);
1535 ccb->ccb_h.status = CAM_REQ_INVALID;
1542 * @brief destroy bounce buffer
1544 * This function is responsible for destroy a Scatter/Gather list
1545 * that create by storvsc_create_bounce_buffer()
1547 * @param sgl- the Scatter/Gather need be destroy
1548 * @param sg_count- page count of the SG list.
1552 storvsc_destroy_bounce_buffer(struct sglist *sgl)
1554 struct hv_sgl_node *sgl_node = NULL;
1555 if (LIST_EMPTY(&g_hv_sgl_page_pool.in_use_sgl_list)) {
1556 printf("storvsc error: not enough in use sgl\n");
1559 sgl_node = LIST_FIRST(&g_hv_sgl_page_pool.in_use_sgl_list);
1560 LIST_REMOVE(sgl_node, link);
1561 sgl_node->sgl_data = sgl;
1562 LIST_INSERT_HEAD(&g_hv_sgl_page_pool.free_sgl_list, sgl_node, link);
1566 * @brief create bounce buffer
1568 * This function is responsible for create a Scatter/Gather list,
1569 * which hold several pages that can be aligned with page size.
1571 * @param seg_count- SG-list segments count
1572 * @param write - if WRITE_TYPE, set SG list page used size to 0,
1573 * otherwise set used size to page size.
1575 * return NULL if create failed
1577 static struct sglist *
1578 storvsc_create_bounce_buffer(uint16_t seg_count, int write)
1581 struct sglist *bounce_sgl = NULL;
1582 unsigned int buf_len = ((write == WRITE_TYPE) ? 0 : PAGE_SIZE);
1583 struct hv_sgl_node *sgl_node = NULL;
1585 /* get struct sglist from free_sgl_list */
1586 if (LIST_EMPTY(&g_hv_sgl_page_pool.free_sgl_list)) {
1587 printf("storvsc error: not enough free sgl\n");
1590 sgl_node = LIST_FIRST(&g_hv_sgl_page_pool.free_sgl_list);
1591 LIST_REMOVE(sgl_node, link);
1592 bounce_sgl = sgl_node->sgl_data;
1593 LIST_INSERT_HEAD(&g_hv_sgl_page_pool.in_use_sgl_list, sgl_node, link);
1595 bounce_sgl->sg_maxseg = seg_count;
1597 if (write == WRITE_TYPE)
1598 bounce_sgl->sg_nseg = 0;
1600 bounce_sgl->sg_nseg = seg_count;
1602 for (i = 0; i < seg_count; i++)
1603 bounce_sgl->sg_segs[i].ss_len = buf_len;
1609 * @brief copy data from SG list to bounce buffer
1611 * This function is responsible for copy data from one SG list's segments
1612 * to another SG list which used as bounce buffer.
1614 * @param bounce_sgl - the destination SG list
1615 * @param orig_sgl - the segment of the source SG list.
1616 * @param orig_sgl_count - the count of segments.
1617 * @param orig_sgl_count - indicate which segment need bounce buffer,
1622 storvsc_copy_sgl_to_bounce_buf(struct sglist *bounce_sgl,
1623 bus_dma_segment_t *orig_sgl,
1624 unsigned int orig_sgl_count,
1627 int src_sgl_idx = 0;
1629 for (src_sgl_idx = 0; src_sgl_idx < orig_sgl_count; src_sgl_idx++) {
1630 if (seg_bits & (1 << src_sgl_idx)) {
1631 memcpy((void*)bounce_sgl->sg_segs[src_sgl_idx].ss_paddr,
1632 (void*)orig_sgl[src_sgl_idx].ds_addr,
1633 orig_sgl[src_sgl_idx].ds_len);
1635 bounce_sgl->sg_segs[src_sgl_idx].ss_len =
1636 orig_sgl[src_sgl_idx].ds_len;
1642 * @brief copy data from SG list which used as bounce to another SG list
1644 * This function is responsible for copy data from one SG list with bounce
1645 * buffer to another SG list's segments.
1647 * @param dest_sgl - the destination SG list's segments
1648 * @param dest_sgl_count - the count of destination SG list's segment.
1649 * @param src_sgl - the source SG list.
1650 * @param seg_bits - indicate which segment used bounce buffer of src SG-list.
1654 storvsc_copy_from_bounce_buf_to_sgl(bus_dma_segment_t *dest_sgl,
1655 unsigned int dest_sgl_count,
1656 struct sglist* src_sgl,
1661 for (sgl_idx = 0; sgl_idx < dest_sgl_count; sgl_idx++) {
1662 if (seg_bits & (1 << sgl_idx)) {
1663 memcpy((void*)(dest_sgl[sgl_idx].ds_addr),
1664 (void*)(src_sgl->sg_segs[sgl_idx].ss_paddr),
1665 src_sgl->sg_segs[sgl_idx].ss_len);
1671 * @brief check SG list with bounce buffer or not
1673 * This function is responsible for check if need bounce buffer for SG list.
1675 * @param sgl - the SG list's segments
1676 * @param sg_count - the count of SG list's segment.
1677 * @param bits - segmengs number that need bounce buffer
1679 * return -1 if SG list needless bounce buffer
1682 storvsc_check_bounce_buffer_sgl(bus_dma_segment_t *sgl,
1683 unsigned int sg_count,
1688 uint64_t phys_addr = 0;
1689 uint64_t tmp_bits = 0;
1690 boolean_t found_hole = FALSE;
1691 boolean_t pre_aligned = TRUE;
1699 phys_addr = vtophys(sgl[0].ds_addr);
1700 offset = phys_addr - trunc_page(phys_addr);
1703 pre_aligned = FALSE;
1707 for (i = 1; i < sg_count; i++) {
1708 phys_addr = vtophys(sgl[i].ds_addr);
1709 offset = phys_addr - trunc_page(phys_addr);
1712 if (FALSE == pre_aligned){
1714 * This segment is aligned, if the previous
1715 * one is not aligned, find a hole
1723 if (phys_addr != vtophys(sgl[i-1].ds_addr +
1726 * Check whether connect to previous
1727 * segment,if not, find the hole
1734 pre_aligned = FALSE;
1747 * Copy bus_dma segments to multiple page buffer, which requires
1748 * the pages are compact composed except for the 1st and last pages.
1751 storvsc_xferbuf_prepare(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
1753 struct hv_storvsc_request *reqp = arg;
1754 union ccb *ccb = reqp->ccb;
1755 struct ccb_scsiio *csio = &ccb->csio;
1756 struct storvsc_gpa_range *prplist;
1759 prplist = &reqp->prp_list;
1760 prplist->gpa_range.gpa_len = csio->dxfer_len;
1761 prplist->gpa_range.gpa_ofs = segs[0].ds_addr & PAGE_MASK;
1763 for (i = 0; i < nsegs; i++) {
1764 prplist->gpa_page[i] = atop(segs[i].ds_addr);
1766 if (i != 0 && i != nsegs - 1) {
1767 KASSERT((segs[i].ds_addr & PAGE_MASK) == 0 &&
1768 segs[i].ds_len == PAGE_SIZE, ("not a full page"));
1772 reqp->prp_cnt = nsegs;
1776 * @brief Fill in a request structure based on a CAM control block
1778 * Fills in a request structure based on the contents of a CAM control
1779 * block. The request structure holds the payload information for
1780 * VSCSI protocol request.
1782 * @param ccb pointer to a CAM contorl block
1783 * @param reqp pointer to a request structure
1786 create_storvsc_request(union ccb *ccb, struct hv_storvsc_request *reqp)
1788 struct ccb_scsiio *csio = &ccb->csio;
1791 uint64_t not_aligned_seg_bits = 0;
1794 /* refer to struct vmscsi_req for meanings of these two fields */
1795 reqp->vstor_packet.u.vm_srb.port =
1796 cam_sim_unit(xpt_path_sim(ccb->ccb_h.path));
1797 reqp->vstor_packet.u.vm_srb.path_id =
1798 cam_sim_bus(xpt_path_sim(ccb->ccb_h.path));
1800 reqp->vstor_packet.u.vm_srb.target_id = ccb->ccb_h.target_id;
1801 reqp->vstor_packet.u.vm_srb.lun = ccb->ccb_h.target_lun;
1803 reqp->vstor_packet.u.vm_srb.cdb_len = csio->cdb_len;
1804 if(ccb->ccb_h.flags & CAM_CDB_POINTER) {
1805 memcpy(&reqp->vstor_packet.u.vm_srb.u.cdb, csio->cdb_io.cdb_ptr,
1808 memcpy(&reqp->vstor_packet.u.vm_srb.u.cdb, csio->cdb_io.cdb_bytes,
1812 switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
1814 reqp->vstor_packet.u.vm_srb.data_in = WRITE_TYPE;
1817 reqp->vstor_packet.u.vm_srb.data_in = READ_TYPE;
1820 reqp->vstor_packet.u.vm_srb.data_in = UNKNOWN_TYPE;
1823 reqp->vstor_packet.u.vm_srb.data_in = UNKNOWN_TYPE;
1827 reqp->sense_data = &csio->sense_data;
1828 reqp->sense_info_len = csio->sense_len;
1832 if (0 == csio->dxfer_len) {
1836 switch (ccb->ccb_h.flags & CAM_DATA_MASK) {
1838 case CAM_DATA_VADDR:
1839 error = bus_dmamap_load_ccb(reqp->softc->storvsc_req_dtag,
1840 reqp->data_dmap, ccb, storvsc_xferbuf_prepare, reqp,
1843 xpt_print(ccb->ccb_h.path,
1844 "bus_dmamap_load_ccb failed: %d\n", error);
1847 if ((ccb->ccb_h.flags & CAM_DATA_MASK) == CAM_DATA_BIO)
1848 reqp->softc->sysctl_data.data_bio_cnt++;
1850 reqp->softc->sysctl_data.data_vaddr_cnt++;
1855 struct storvsc_gpa_range *prplist;
1860 bus_dma_segment_t *storvsc_sglist =
1861 (bus_dma_segment_t *)ccb->csio.data_ptr;
1862 u_int16_t storvsc_sg_count = ccb->csio.sglist_cnt;
1864 prplist = &reqp->prp_list;
1865 prplist->gpa_range.gpa_len = csio->dxfer_len;
1867 printf("Storvsc: get SG I/O operation, %d\n",
1868 reqp->vstor_packet.u.vm_srb.data_in);
1870 if (storvsc_sg_count > STORVSC_DATA_SEGCNT_MAX){
1871 printf("Storvsc: %d segments is too much, "
1872 "only support %d segments\n",
1873 storvsc_sg_count, STORVSC_DATA_SEGCNT_MAX);
1878 * We create our own bounce buffer function currently. Idealy
1879 * we should use BUS_DMA(9) framework. But with current BUS_DMA
1880 * code there is no callback API to check the page alignment of
1881 * middle segments before busdma can decide if a bounce buffer
1882 * is needed for particular segment. There is callback,
1883 * "bus_dma_filter_t *filter", but the parrameters are not
1884 * sufficient for storvsc driver.
1886 * Add page alignment check in BUS_DMA(9) callback. Once
1887 * this is complete, switch the following code to use
1888 * BUS_DMA(9) for storvsc bounce buffer support.
1890 /* check if we need to create bounce buffer */
1891 ret = storvsc_check_bounce_buffer_sgl(storvsc_sglist,
1892 storvsc_sg_count, ¬_aligned_seg_bits);
1895 storvsc_create_bounce_buffer(storvsc_sg_count,
1896 reqp->vstor_packet.u.vm_srb.data_in);
1897 if (NULL == reqp->bounce_sgl) {
1898 printf("Storvsc_error: "
1899 "create bounce buffer failed.\n");
1903 reqp->bounce_sgl_count = storvsc_sg_count;
1904 reqp->not_aligned_seg_bits = not_aligned_seg_bits;
1907 * if it is write, we need copy the original data
1910 if (WRITE_TYPE == reqp->vstor_packet.u.vm_srb.data_in) {
1911 storvsc_copy_sgl_to_bounce_buf(
1915 reqp->not_aligned_seg_bits);
1918 /* transfer virtual address to physical frame number */
1919 if (reqp->not_aligned_seg_bits & 0x1){
1921 vtophys(reqp->bounce_sgl->sg_segs[0].ss_paddr);
1924 vtophys(storvsc_sglist[0].ds_addr);
1926 prplist->gpa_range.gpa_ofs = phys_addr & PAGE_MASK;
1928 pfn = phys_addr >> PAGE_SHIFT;
1929 prplist->gpa_page[0] = pfn;
1931 for (i = 1; i < storvsc_sg_count; i++) {
1932 if (reqp->not_aligned_seg_bits & (1 << i)) {
1934 vtophys(reqp->bounce_sgl->sg_segs[i].ss_paddr);
1937 vtophys(storvsc_sglist[i].ds_addr);
1940 pfn = phys_addr >> PAGE_SHIFT;
1941 prplist->gpa_page[i] = pfn;
1945 phys_addr = vtophys(storvsc_sglist[0].ds_addr);
1947 prplist->gpa_range.gpa_ofs = phys_addr & PAGE_MASK;
1949 for (i = 0; i < storvsc_sg_count; i++) {
1950 phys_addr = vtophys(storvsc_sglist[i].ds_addr);
1951 pfn = phys_addr >> PAGE_SHIFT;
1952 prplist->gpa_page[i] = pfn;
1956 /* check the last segment cross boundary or not */
1957 offset = phys_addr & PAGE_MASK;
1959 /* Add one more PRP entry */
1961 vtophys(storvsc_sglist[i-1].ds_addr +
1962 PAGE_SIZE - offset);
1963 pfn = phys_addr >> PAGE_SHIFT;
1964 prplist->gpa_page[i] = pfn;
1968 reqp->bounce_sgl_count = 0;
1970 reqp->softc->sysctl_data.data_sg_cnt++;
1974 printf("Unknow flags: %d\n", ccb->ccb_h.flags);
1982 * @brief completion function before returning to CAM
1984 * I/O process has been completed and the result needs
1985 * to be passed to the CAM layer.
1986 * Free resources related to this request.
1988 * @param reqp pointer to a request structure
1991 storvsc_io_done(struct hv_storvsc_request *reqp)
1993 union ccb *ccb = reqp->ccb;
1994 struct ccb_scsiio *csio = &ccb->csio;
1995 struct storvsc_softc *sc = reqp->softc;
1996 struct vmscsi_req *vm_srb = &reqp->vstor_packet.u.vm_srb;
1997 bus_dma_segment_t *ori_sglist = NULL;
1998 int ori_sg_count = 0;
1999 /* destroy bounce buffer if it is used */
2000 if (reqp->bounce_sgl_count) {
2001 ori_sglist = (bus_dma_segment_t *)ccb->csio.data_ptr;
2002 ori_sg_count = ccb->csio.sglist_cnt;
2005 * If it is READ operation, we should copy back the data
2006 * to original SG list.
2008 if (READ_TYPE == reqp->vstor_packet.u.vm_srb.data_in) {
2009 storvsc_copy_from_bounce_buf_to_sgl(ori_sglist,
2012 reqp->not_aligned_seg_bits);
2015 storvsc_destroy_bounce_buffer(reqp->bounce_sgl);
2016 reqp->bounce_sgl_count = 0;
2019 if (reqp->retries > 0) {
2020 mtx_lock(&sc->hs_lock);
2021 #if HVS_TIMEOUT_TEST
2022 xpt_print(ccb->ccb_h.path,
2023 "%u: IO returned after timeout, "
2024 "waking up timer handler if any.\n", ticks);
2025 mtx_lock(&reqp->event.mtx);
2026 cv_signal(&reqp->event.cv);
2027 mtx_unlock(&reqp->event.mtx);
2030 xpt_print(ccb->ccb_h.path,
2031 "%u: IO returned after timeout, "
2032 "stopping timer if any.\n", ticks);
2033 mtx_unlock(&sc->hs_lock);
2038 * callout_drain() will wait for the timer handler to finish
2039 * if it is running. So we don't need any lock to synchronize
2040 * between this routine and the timer handler.
2041 * Note that we need to make sure reqp is not freed when timer
2042 * handler is using or will use it.
2044 if (ccb->ccb_h.timeout != CAM_TIME_INFINITY) {
2045 callout_drain(&reqp->callout);
2049 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
2050 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2051 if (vm_srb->scsi_status == SCSI_STATUS_OK) {
2052 const struct scsi_generic *cmd;
2054 if (vm_srb->srb_status != SRB_STATUS_SUCCESS) {
2055 if (vm_srb->srb_status == SRB_STATUS_INVALID_LUN) {
2056 xpt_print(ccb->ccb_h.path, "invalid LUN %d\n",
2059 xpt_print(ccb->ccb_h.path, "Unknown SRB flag: %d\n",
2060 vm_srb->srb_status);
2063 * If there are errors, for example, invalid LUN,
2064 * host will inform VM through SRB status.
2066 ccb->ccb_h.status |= CAM_SEL_TIMEOUT;
2068 ccb->ccb_h.status |= CAM_REQ_CMP;
2071 cmd = (const struct scsi_generic *)
2072 ((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
2073 csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes);
2074 if (cmd->opcode == INQUIRY) {
2075 struct scsi_inquiry_data *inq_data =
2076 (struct scsi_inquiry_data *)csio->data_ptr;
2077 uint8_t *resp_buf = (uint8_t *)csio->data_ptr;
2078 int resp_xfer_len, resp_buf_len, data_len;
2080 /* Get the buffer length reported by host */
2081 resp_xfer_len = vm_srb->transfer_len;
2082 /* Get the available buffer length */
2083 resp_buf_len = resp_xfer_len >= 5 ? resp_buf[4] + 5 : 0;
2084 data_len = (resp_buf_len < resp_xfer_len) ?
2085 resp_buf_len : resp_xfer_len;
2087 if (bootverbose && data_len >= 5) {
2088 xpt_print(ccb->ccb_h.path, "storvsc inquiry "
2089 "(%d) [%x %x %x %x %x ... ]\n", data_len,
2090 resp_buf[0], resp_buf[1], resp_buf[2],
2091 resp_buf[3], resp_buf[4]);
2093 if (vm_srb->srb_status == SRB_STATUS_SUCCESS &&
2094 data_len > SHORT_INQUIRY_LENGTH) {
2097 cam_strvis(vendor, inq_data->vendor,
2098 sizeof(inq_data->vendor), sizeof(vendor));
2101 * XXX: Upgrade SPC2 to SPC3 if host is WIN8 or
2102 * WIN2012 R2 in order to support UNMAP feature.
2104 if (!strncmp(vendor, "Msft", 4) &&
2105 SID_ANSI_REV(inq_data) == SCSI_REV_SPC2 &&
2106 (vmstor_proto_version ==
2107 VMSTOR_PROTOCOL_VERSION_WIN8_1 ||
2108 vmstor_proto_version ==
2109 VMSTOR_PROTOCOL_VERSION_WIN8)) {
2110 inq_data->version = SCSI_REV_SPC3;
2112 xpt_print(ccb->ccb_h.path,
2120 mtx_lock(&sc->hs_lock);
2121 xpt_print(ccb->ccb_h.path,
2122 "storvsc scsi_status = %d\n",
2123 vm_srb->scsi_status);
2124 mtx_unlock(&sc->hs_lock);
2125 ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
2128 ccb->csio.scsi_status = (vm_srb->scsi_status & 0xFF);
2129 ccb->csio.resid = ccb->csio.dxfer_len - vm_srb->transfer_len;
2131 if (reqp->sense_info_len != 0) {
2132 csio->sense_resid = csio->sense_len - reqp->sense_info_len;
2133 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
2136 mtx_lock(&sc->hs_lock);
2137 if (reqp->softc->hs_frozen == 1) {
2138 xpt_print(ccb->ccb_h.path,
2139 "%u: storvsc unfreezing softc 0x%p.\n",
2140 ticks, reqp->softc);
2141 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
2142 reqp->softc->hs_frozen = 0;
2144 storvsc_free_request(sc, reqp);
2145 mtx_unlock(&sc->hs_lock);
2147 xpt_done_direct(ccb);
2151 * @brief Free a request structure
2153 * Free a request structure by returning it to the free list
2155 * @param sc pointer to a softc
2156 * @param reqp pointer to a request structure
2159 storvsc_free_request(struct storvsc_softc *sc, struct hv_storvsc_request *reqp)
2162 LIST_INSERT_HEAD(&sc->hs_free_list, reqp, link);
2166 * @brief Determine type of storage device from GUID
2168 * Using the type GUID, determine if this is a StorVSC (paravirtual
2169 * SCSI or BlkVSC (paravirtual IDE) device.
2171 * @param dev a device
2174 static enum hv_storage_type
2175 storvsc_get_storage_type(device_t dev)
2177 device_t parent = device_get_parent(dev);
2179 if (VMBUS_PROBE_GUID(parent, dev, &gBlkVscDeviceType) == 0)
2180 return DRIVER_BLKVSC;
2181 if (VMBUS_PROBE_GUID(parent, dev, &gStorVscDeviceType) == 0)
2182 return DRIVER_STORVSC;
2183 return DRIVER_UNKNOWN;