2 * Copyright (c) 2009-2012 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>
41 #include <sys/systm.h>
42 #include <sys/sockio.h>
44 #include <sys/malloc.h>
45 #include <sys/module.h>
46 #include <sys/kernel.h>
47 #include <sys/queue.h>
50 #include <sys/taskqueue.h>
52 #include <sys/mutex.h>
53 #include <sys/callout.h>
60 #include <cam/cam_ccb.h>
61 #include <cam/cam_periph.h>
62 #include <cam/cam_sim.h>
63 #include <cam/cam_xpt_sim.h>
64 #include <cam/cam_xpt_internal.h>
65 #include <cam/cam_debug.h>
66 #include <cam/scsi/scsi_all.h>
67 #include <cam/scsi/scsi_message.h>
70 #include <dev/hyperv/include/hyperv.h>
71 #include "hv_vstorage.h"
73 #define STORVSC_RINGBUFFER_SIZE (20*PAGE_SIZE)
74 #define STORVSC_MAX_LUNS_PER_TARGET (64)
75 #define STORVSC_MAX_IO_REQUESTS (STORVSC_MAX_LUNS_PER_TARGET * 2)
76 #define BLKVSC_MAX_IDE_DISKS_PER_TARGET (1)
77 #define BLKVSC_MAX_IO_REQUESTS STORVSC_MAX_IO_REQUESTS
78 #define STORVSC_MAX_TARGETS (1)
82 enum storvsc_request_type {
88 struct hv_storvsc_request {
89 LIST_ENTRY(hv_storvsc_request) link;
90 struct vstor_packet vstor_packet;
91 hv_vmbus_multipage_buffer data_buf;
93 uint8_t sense_info_len;
96 struct storvsc_softc *softc;
97 struct callout callout;
98 struct sema synch_sema; /*Synchronize the request/response if needed */
101 struct storvsc_softc {
102 struct hv_device *hs_dev;
103 LIST_HEAD(, hv_storvsc_request) hs_free_list;
105 struct storvsc_driver_props *hs_drv_props;
108 struct cam_sim *hs_sim;
109 struct cam_path *hs_path;
110 uint32_t hs_num_out_reqs;
111 boolean_t hs_destroy;
112 boolean_t hs_drain_notify;
113 struct sema hs_drain_sema;
114 struct hv_storvsc_request hs_init_req;
115 struct hv_storvsc_request hs_reset_req;
120 * HyperV storvsc timeout testing cases:
121 * a. IO returned after first timeout;
122 * b. IO returned after second timeout and queue freeze;
123 * c. IO returned while timer handler is running
124 * The first can be tested by "sg_senddiag -vv /dev/daX",
125 * and the second and third can be done by
126 * "sg_wr_mode -v -p 08 -c 0,1a -m 0,ff /dev/daX".
128 #define HVS_TIMEOUT_TEST 0
131 * Bus/adapter reset functionality on the Hyper-V host is
132 * buggy and it will be disabled until
133 * it can be further tested.
135 #define HVS_HOST_RESET 0
137 struct storvsc_driver_props {
140 uint8_t drv_max_luns_per_target;
141 uint8_t drv_max_ios_per_target;
142 uint32_t drv_ringbuffer_size;
145 enum hv_storage_type {
151 #define HS_MAX_ADAPTERS 10
153 /* {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f} */
154 static const hv_guid gStorVscDeviceType={
155 .data = {0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
156 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f}
159 /* {32412632-86cb-44a2-9b5c-50d1417354f5} */
160 static const hv_guid gBlkVscDeviceType={
161 .data = {0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44,
162 0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5}
165 static struct storvsc_driver_props g_drv_props_table[] = {
166 {"blkvsc", "Hyper-V IDE Storage Interface",
167 BLKVSC_MAX_IDE_DISKS_PER_TARGET, BLKVSC_MAX_IO_REQUESTS,
168 STORVSC_RINGBUFFER_SIZE},
169 {"storvsc", "Hyper-V SCSI Storage Interface",
170 STORVSC_MAX_LUNS_PER_TARGET, STORVSC_MAX_IO_REQUESTS,
171 STORVSC_RINGBUFFER_SIZE}
174 /* static functions */
175 static int storvsc_probe(device_t dev);
176 static int storvsc_attach(device_t dev);
177 static int storvsc_detach(device_t dev);
178 static void storvsc_poll(struct cam_sim * sim);
179 static void storvsc_action(struct cam_sim * sim, union ccb * ccb);
180 static void create_storvsc_request(union ccb *ccb, struct hv_storvsc_request *reqp);
181 static void storvsc_free_request(struct storvsc_softc *sc, struct hv_storvsc_request *reqp);
182 static enum hv_storage_type storvsc_get_storage_type(device_t dev);
183 static void hv_storvsc_on_channel_callback(void *context);
184 static void hv_storvsc_on_iocompletion( struct storvsc_softc *sc,
185 struct vstor_packet *vstor_packet,
186 struct hv_storvsc_request *request);
187 static int hv_storvsc_connect_vsp(struct hv_device *device);
188 static void storvsc_io_done(struct hv_storvsc_request *reqp);
190 static device_method_t storvsc_methods[] = {
191 /* Device interface */
192 DEVMETHOD(device_probe, storvsc_probe),
193 DEVMETHOD(device_attach, storvsc_attach),
194 DEVMETHOD(device_detach, storvsc_detach),
195 DEVMETHOD(device_shutdown, bus_generic_shutdown),
199 static driver_t storvsc_driver = {
200 "storvsc", storvsc_methods, sizeof(struct storvsc_softc),
203 static devclass_t storvsc_devclass;
204 DRIVER_MODULE(storvsc, vmbus, storvsc_driver, storvsc_devclass, 0, 0);
205 MODULE_VERSION(storvsc, 1);
206 MODULE_DEPEND(storvsc, vmbus, 1, 1, 1);
210 * The host is capable of sending messages to us that are
211 * completely unsolicited. So, we need to address the race
212 * condition where we may be in the process of unloading the
213 * driver when the host may send us an unsolicited message.
214 * We address this issue by implementing a sequentially
215 * consistent protocol:
217 * 1. Channel callback is invoked while holding the the channel lock
218 * and an unloading driver will reset the channel callback under
219 * the protection of this channel lock.
221 * 2. To ensure bounded wait time for unloading a driver, we don't
222 * permit outgoing traffic once the device is marked as being
225 * 3. Once the device is marked as being destroyed, we only
226 * permit incoming traffic to properly account for
227 * packets already sent out.
229 static inline struct storvsc_softc *
230 get_stor_device(struct hv_device *device,
233 struct storvsc_softc *sc;
235 sc = device_get_softc(device->device);
242 * Here we permit outgoing I/O only
243 * if the device is not being destroyed.
246 if (sc->hs_destroy) {
251 * inbound case; if being destroyed
252 * only permit to account for
253 * messages already sent out.
255 if (sc->hs_destroy && (sc->hs_num_out_reqs == 0)) {
263 * @brief initialize channel connection to parent partition
265 * @param dev a Hyper-V device pointer
266 * @returns 0 on success, non-zero error on failure
269 hv_storvsc_channel_init(struct hv_device *dev)
272 struct hv_storvsc_request *request;
273 struct vstor_packet *vstor_packet;
274 struct storvsc_softc *sc;
276 sc = get_stor_device(dev, TRUE);
281 request = &sc->hs_init_req;
282 memset(request, 0, sizeof(struct hv_storvsc_request));
283 vstor_packet = &request->vstor_packet;
287 * Initiate the vsc/vsp initialization protocol on the open channel
289 sema_init(&request->synch_sema, 0, ("stor_synch_sema"));
291 vstor_packet->operation = VSTOR_OPERATION_BEGININITIALIZATION;
292 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
295 ret = hv_vmbus_channel_send_packet(
298 sizeof(struct vstor_packet),
299 (uint64_t)(uintptr_t)request,
300 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND,
301 HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
307 ret = sema_timedwait(&request->synch_sema, 500); /* KYS 5 seconds */
313 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETEIO ||
314 vstor_packet->status != 0) {
318 /* reuse the packet for version range supported */
320 memset(vstor_packet, 0, sizeof(struct vstor_packet));
321 vstor_packet->operation = VSTOR_OPERATION_QUERYPROTOCOLVERSION;
322 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
324 vstor_packet->u.version.major_minor = VMSTOR_PROTOCOL_VERSION_CURRENT;
326 /* revision is only significant for Windows guests */
327 vstor_packet->u.version.revision = 0;
329 ret = hv_vmbus_channel_send_packet(
332 sizeof(struct vstor_packet),
333 (uint64_t)(uintptr_t)request,
334 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND,
335 HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
341 ret = sema_timedwait(&request->synch_sema, 500); /* KYS 5 seconds */
347 /* TODO: Check returned version */
348 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETEIO ||
349 vstor_packet->status != 0) {
354 * Query channel properties
356 memset(vstor_packet, 0, sizeof(struct vstor_packet));
357 vstor_packet->operation = VSTOR_OPERATION_QUERYPROPERTIES;
358 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
360 ret = hv_vmbus_channel_send_packet(
363 sizeof(struct vstor_packet),
364 (uint64_t)(uintptr_t)request,
365 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND,
366 HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
372 ret = sema_timedwait(&request->synch_sema, 500); /* KYS 5 seconds */
378 /* TODO: Check returned version */
379 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETEIO ||
380 vstor_packet->status != 0) {
384 memset(vstor_packet, 0, sizeof(struct vstor_packet));
385 vstor_packet->operation = VSTOR_OPERATION_ENDINITIALIZATION;
386 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
388 ret = hv_vmbus_channel_send_packet(
391 sizeof(struct vstor_packet),
392 (uint64_t)(uintptr_t)request,
393 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND,
394 HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
400 ret = sema_timedwait(&request->synch_sema, 500); /* KYS 5 seconds */
406 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETEIO ||
407 vstor_packet->status != 0) {
412 sema_destroy(&request->synch_sema);
417 * @brief Open channel connection to paraent partition StorVSP driver
419 * Open and initialize channel connection to parent partition StorVSP driver.
421 * @param pointer to a Hyper-V device
422 * @returns 0 on success, non-zero error on failure
425 hv_storvsc_connect_vsp(struct hv_device *dev)
428 struct vmstor_chan_props props;
429 struct storvsc_softc *sc;
431 sc = device_get_softc(dev->device);
433 memset(&props, 0, sizeof(struct vmstor_chan_props));
439 ret = hv_vmbus_channel_open(
441 sc->hs_drv_props->drv_ringbuffer_size,
442 sc->hs_drv_props->drv_ringbuffer_size,
444 sizeof(struct vmstor_chan_props),
445 hv_storvsc_on_channel_callback,
453 ret = hv_storvsc_channel_init(dev);
460 hv_storvsc_host_reset(struct hv_device *dev)
463 struct storvsc_softc *sc;
465 struct hv_storvsc_request *request;
466 struct vstor_packet *vstor_packet;
468 sc = get_stor_device(dev, TRUE);
473 request = &sc->hs_reset_req;
475 vstor_packet = &request->vstor_packet;
477 sema_init(&request->synch_sema, 0, "stor synch sema");
479 vstor_packet->operation = VSTOR_OPERATION_RESETBUS;
480 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
482 ret = hv_vmbus_channel_send_packet(dev->channel,
484 sizeof(struct vstor_packet),
485 (uint64_t)(uintptr_t)&sc->hs_reset_req,
486 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND,
487 HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
493 ret = sema_timedwait(&request->synch_sema, 500); /* KYS 5 seconds */
501 * At this point, all outstanding requests in the adapter
502 * should have been flushed out and return to us
506 sema_destroy(&request->synch_sema);
509 #endif /* HVS_HOST_RESET */
512 * @brief Function to initiate an I/O request
514 * @param device Hyper-V device pointer
515 * @param request pointer to a request structure
516 * @returns 0 on success, non-zero error on failure
519 hv_storvsc_io_request(struct hv_device *device,
520 struct hv_storvsc_request *request)
522 struct storvsc_softc *sc;
523 struct vstor_packet *vstor_packet = &request->vstor_packet;
526 sc = get_stor_device(device, TRUE);
532 vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
534 vstor_packet->u.vm_srb.length = sizeof(struct vmscsi_req);
536 vstor_packet->u.vm_srb.sense_info_len = SENSE_BUFFER_SIZE;
538 vstor_packet->u.vm_srb.transfer_len = request->data_buf.length;
540 vstor_packet->operation = VSTOR_OPERATION_EXECUTESRB;
543 mtx_unlock(&request->softc->hs_lock);
544 if (request->data_buf.length) {
545 ret = hv_vmbus_channel_send_packet_multipagebuffer(
549 sizeof(struct vstor_packet),
550 (uint64_t)(uintptr_t)request);
553 ret = hv_vmbus_channel_send_packet(
556 sizeof(struct vstor_packet),
557 (uint64_t)(uintptr_t)request,
558 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND,
559 HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
561 mtx_lock(&request->softc->hs_lock);
564 printf("Unable to send packet %p ret %d", vstor_packet, ret);
566 atomic_add_int(&sc->hs_num_out_reqs, 1);
574 * Process IO_COMPLETION_OPERATION and ready
575 * the result to be completed for upper layer
576 * processing by the CAM layer.
579 hv_storvsc_on_iocompletion(struct storvsc_softc *sc,
580 struct vstor_packet *vstor_packet,
581 struct hv_storvsc_request *request)
583 struct vmscsi_req *vm_srb;
585 vm_srb = &vstor_packet->u.vm_srb;
587 request->sense_info_len = 0;
588 if (((vm_srb->scsi_status & 0xFF) == SCSI_STATUS_CHECK_COND) &&
589 (vm_srb->srb_status & SRB_STATUS_AUTOSENSE_VALID)) {
590 /* Autosense data available */
592 KASSERT(vm_srb->sense_info_len <= request->sense_info_len,
593 ("vm_srb->sense_info_len <= "
594 "request->sense_info_len"));
596 memcpy(request->sense_data, vm_srb->u.sense_data,
597 vm_srb->sense_info_len);
599 request->sense_info_len = vm_srb->sense_info_len;
602 /* Complete request by passing to the CAM layer */
603 storvsc_io_done(request);
604 atomic_subtract_int(&sc->hs_num_out_reqs, 1);
605 if (sc->hs_drain_notify && (sc->hs_num_out_reqs == 0)) {
606 sema_post(&sc->hs_drain_sema);
611 hv_storvsc_on_channel_callback(void *context)
614 struct hv_device *device = (struct hv_device *)context;
615 struct storvsc_softc *sc;
616 uint32_t bytes_recvd;
618 uint8_t packet[roundup2(sizeof(struct vstor_packet), 8)];
619 struct hv_storvsc_request *request;
620 struct vstor_packet *vstor_packet;
622 sc = get_stor_device(device, FALSE);
627 KASSERT(device, ("device"));
629 ret = hv_vmbus_channel_recv_packet(
632 roundup2(sizeof(struct vstor_packet), 8),
636 while ((ret == 0) && (bytes_recvd > 0)) {
637 request = (struct hv_storvsc_request *)(uintptr_t)request_id;
638 KASSERT(request, ("request"));
640 if ((request == &sc->hs_init_req) ||
641 (request == &sc->hs_reset_req)) {
642 memcpy(&request->vstor_packet, packet,
643 sizeof(struct vstor_packet));
644 sema_post(&request->synch_sema);
646 vstor_packet = (struct vstor_packet *)packet;
647 switch(vstor_packet->operation) {
648 case VSTOR_OPERATION_COMPLETEIO:
649 hv_storvsc_on_iocompletion(sc,
650 vstor_packet, request);
652 case VSTOR_OPERATION_REMOVEDEVICE:
653 /* TODO: implement */
659 ret = hv_vmbus_channel_recv_packet(
662 roundup2(sizeof(struct vstor_packet), 8),
669 * @brief StorVSC probe function
671 * Device probe function. Returns 0 if the input device is a StorVSC
672 * device. Otherwise, a ENXIO is returned. If the input device is
673 * for BlkVSC (paravirtual IDE) device and this support is disabled in
674 * favor of the emulated ATA/IDE device, return ENXIO.
677 * @returns 0 on success, ENXIO if not a matcing StorVSC device
680 storvsc_probe(device_t dev)
682 int ata_disk_enable = 0;
685 switch (storvsc_get_storage_type(dev)) {
688 device_printf(dev, "DRIVER_BLKVSC-Emulated ATA/IDE probe\n");
689 if (!getenv_int("hw.ata.disk_enable", &ata_disk_enable)) {
692 "Enlightened ATA/IDE detected\n");
694 } else if(bootverbose)
695 device_printf(dev, "Emulated ATA/IDE set (hw.ata.disk_enable set)\n");
699 device_printf(dev, "Enlightened SCSI device detected\n");
709 * @brief StorVSC attach function
711 * Function responsible for allocating per-device structures,
712 * setting up CAM interfaces and scanning for available LUNs to
713 * be used for SCSI device peripherals.
716 * @returns 0 on success or an error on failure
719 storvsc_attach(device_t dev)
721 struct hv_device *hv_dev = vmbus_get_devctx(dev);
722 enum hv_storage_type stor_type;
723 struct storvsc_softc *sc;
724 struct cam_devq *devq;
726 struct hv_storvsc_request *reqp;
727 struct root_hold_token *root_mount_token = NULL;
730 * We need to serialize storvsc attach calls.
732 root_mount_token = root_mount_hold("storvsc");
734 sc = device_get_softc(dev);
740 stor_type = storvsc_get_storage_type(dev);
742 if (stor_type == DRIVER_UNKNOWN) {
747 bzero(sc, sizeof(struct storvsc_softc));
749 /* fill in driver specific properties */
750 sc->hs_drv_props = &g_drv_props_table[stor_type];
752 /* fill in device specific properties */
753 sc->hs_unit = device_get_unit(dev);
755 device_set_desc(dev, g_drv_props_table[stor_type].drv_desc);
757 LIST_INIT(&sc->hs_free_list);
758 mtx_init(&sc->hs_lock, "hvslck", NULL, MTX_DEF);
760 for (i = 0; i < sc->hs_drv_props->drv_max_ios_per_target; ++i) {
761 reqp = malloc(sizeof(struct hv_storvsc_request),
762 M_DEVBUF, M_WAITOK|M_ZERO);
765 LIST_INSERT_HEAD(&sc->hs_free_list, reqp, link);
768 sc->hs_destroy = FALSE;
769 sc->hs_drain_notify = FALSE;
770 sema_init(&sc->hs_drain_sema, 0, "Store Drain Sema");
772 ret = hv_storvsc_connect_vsp(hv_dev);
778 * Create the device queue.
779 * Hyper-V maps each target to one SCSI HBA
781 devq = cam_simq_alloc(sc->hs_drv_props->drv_max_ios_per_target);
783 device_printf(dev, "Failed to alloc device queue\n");
788 sc->hs_sim = cam_sim_alloc(storvsc_action,
790 sc->hs_drv_props->drv_name,
794 sc->hs_drv_props->drv_max_ios_per_target,
797 if (sc->hs_sim == NULL) {
798 device_printf(dev, "Failed to alloc sim\n");
804 mtx_lock(&sc->hs_lock);
805 /* bus_id is set to 0, need to get it from VMBUS channel query? */
806 if (xpt_bus_register(sc->hs_sim, dev, 0) != CAM_SUCCESS) {
807 cam_sim_free(sc->hs_sim, /*free_devq*/TRUE);
808 mtx_unlock(&sc->hs_lock);
809 device_printf(dev, "Unable to register SCSI bus\n");
814 if (xpt_create_path(&sc->hs_path, /*periph*/NULL,
815 cam_sim_path(sc->hs_sim),
816 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
817 xpt_bus_deregister(cam_sim_path(sc->hs_sim));
818 cam_sim_free(sc->hs_sim, /*free_devq*/TRUE);
819 mtx_unlock(&sc->hs_lock);
820 device_printf(dev, "Unable to create path\n");
825 mtx_unlock(&sc->hs_lock);
827 root_mount_rel(root_mount_token);
832 root_mount_rel(root_mount_token);
833 while (!LIST_EMPTY(&sc->hs_free_list)) {
834 reqp = LIST_FIRST(&sc->hs_free_list);
835 LIST_REMOVE(reqp, link);
836 free(reqp, M_DEVBUF);
842 * @brief StorVSC device detach function
844 * This function is responsible for safely detaching a
845 * StorVSC device. This includes waiting for inbound responses
846 * to complete and freeing associated per-device structures.
848 * @param dev a device
849 * returns 0 on success
852 storvsc_detach(device_t dev)
854 struct storvsc_softc *sc = device_get_softc(dev);
855 struct hv_storvsc_request *reqp = NULL;
856 struct hv_device *hv_device = vmbus_get_devctx(dev);
858 mtx_lock(&hv_device->channel->inbound_lock);
859 sc->hs_destroy = TRUE;
860 mtx_unlock(&hv_device->channel->inbound_lock);
863 * At this point, all outbound traffic should be disabled. We
864 * only allow inbound traffic (responses) to proceed so that
865 * outstanding requests can be completed.
868 sc->hs_drain_notify = TRUE;
869 sema_wait(&sc->hs_drain_sema);
870 sc->hs_drain_notify = FALSE;
873 * Since we have already drained, we don't need to busy wait.
874 * The call to close the channel will reset the callback
875 * under the protection of the incoming channel lock.
878 hv_vmbus_channel_close(hv_device->channel);
880 mtx_lock(&sc->hs_lock);
881 while (!LIST_EMPTY(&sc->hs_free_list)) {
882 reqp = LIST_FIRST(&sc->hs_free_list);
883 LIST_REMOVE(reqp, link);
885 free(reqp, M_DEVBUF);
887 mtx_unlock(&sc->hs_lock);
893 * @brief unit test for timed out operations
895 * This function provides unit testing capability to simulate
896 * timed out operations. Recompilation with HV_TIMEOUT_TEST=1
899 * @param reqp pointer to a request structure
900 * @param opcode SCSI operation being performed
901 * @param wait if 1, wait for I/O to complete
904 storvsc_timeout_test(struct hv_storvsc_request *reqp,
905 uint8_t opcode, int wait)
908 union ccb *ccb = reqp->ccb;
909 struct storvsc_softc *sc = reqp->softc;
911 if (reqp->vstor_packet.vm_srb.cdb[0] != opcode) {
916 mtx_lock(&reqp->event.mtx);
918 ret = hv_storvsc_io_request(sc->hs_dev, reqp);
921 mtx_unlock(&reqp->event.mtx);
923 printf("%s: io_request failed with %d.\n",
925 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
926 mtx_lock(&sc->hs_lock);
927 storvsc_free_request(sc, reqp);
929 mtx_unlock(&sc->hs_lock);
934 xpt_print(ccb->ccb_h.path,
935 "%u: %s: waiting for IO return.\n",
937 ret = cv_timedwait(&reqp->event.cv, &reqp->event.mtx, 60*hz);
938 mtx_unlock(&reqp->event.mtx);
939 xpt_print(ccb->ccb_h.path, "%u: %s: %s.\n",
940 ticks, __func__, (ret == 0)?
941 "IO return detected" :
942 "IO return not detected");
944 * Now both the timer handler and io done are running
945 * simultaneously. We want to confirm the io done always
946 * finishes after the timer handler exits. So reqp used by
947 * timer handler is not freed or stale. Do busy loop for
948 * another 1/10 second to make sure io done does
949 * wait for the timer handler to complete.
952 mtx_lock(&sc->hs_lock);
953 xpt_print(ccb->ccb_h.path,
954 "%u: %s: finishing, queue frozen %d, "
955 "ccb status 0x%x scsi_status 0x%x.\n",
956 ticks, __func__, sc->hs_frozen,
958 ccb->csio.scsi_status);
959 mtx_unlock(&sc->hs_lock);
962 #endif /* HVS_TIMEOUT_TEST */
965 * @brief timeout handler for requests
967 * This function is called as a result of a callout expiring.
969 * @param arg pointer to a request
972 storvsc_timeout(void *arg)
974 struct hv_storvsc_request *reqp = arg;
975 struct storvsc_softc *sc = reqp->softc;
976 union ccb *ccb = reqp->ccb;
978 if (reqp->retries == 0) {
979 mtx_lock(&sc->hs_lock);
980 xpt_print(ccb->ccb_h.path,
981 "%u: IO timed out (req=0x%p), wait for another %u secs.\n",
982 ticks, reqp, ccb->ccb_h.timeout / 1000);
983 cam_error_print(ccb, CAM_ESF_ALL, CAM_EPF_ALL);
984 mtx_unlock(&sc->hs_lock);
987 callout_reset(&reqp->callout,
988 (ccb->ccb_h.timeout * hz) / 1000,
989 storvsc_timeout, reqp);
991 storvsc_timeout_test(reqp, SEND_DIAGNOSTIC, 0);
996 mtx_lock(&sc->hs_lock);
997 xpt_print(ccb->ccb_h.path,
998 "%u: IO (reqp = 0x%p) did not return for %u seconds, %s.\n",
999 ticks, reqp, ccb->ccb_h.timeout * (reqp->retries+1) / 1000,
1000 (sc->hs_frozen == 0)?
1001 "freezing the queue" : "the queue is already frozen");
1002 if (sc->hs_frozen == 0) {
1004 xpt_freeze_simq(xpt_path_sim(ccb->ccb_h.path), 1);
1006 mtx_unlock(&sc->hs_lock);
1008 #if HVS_TIMEOUT_TEST
1009 storvsc_timeout_test(reqp, MODE_SELECT_10, 1);
1014 * @brief StorVSC device poll function
1016 * This function is responsible for servicing requests when
1017 * interrupts are disabled (i.e when we are dumping core.)
1019 * @param sim a pointer to a CAM SCSI interface module
1022 storvsc_poll(struct cam_sim *sim)
1024 struct storvsc_softc *sc = cam_sim_softc(sim);
1026 mtx_assert(&sc->hs_lock, MA_OWNED);
1027 mtx_unlock(&sc->hs_lock);
1028 hv_storvsc_on_channel_callback(sc->hs_dev);
1029 mtx_lock(&sc->hs_lock);
1033 * @brief StorVSC device action function
1035 * This function is responsible for handling SCSI operations which
1036 * are passed from the CAM layer. The requests are in the form of
1037 * CAM control blocks which indicate the action being performed.
1038 * Not all actions require converting the request to a VSCSI protocol
1039 * message - these actions can be responded to by this driver.
1040 * Requests which are destined for a backend storage device are converted
1041 * to a VSCSI protocol message and sent on the channel connection associated
1044 * @param sim pointer to a CAM SCSI interface module
1045 * @param ccb pointer to a CAM control block
1048 storvsc_action(struct cam_sim *sim, union ccb *ccb)
1050 struct storvsc_softc *sc = cam_sim_softc(sim);
1053 mtx_assert(&sc->hs_lock, MA_OWNED);
1054 switch (ccb->ccb_h.func_code) {
1055 case XPT_PATH_INQ: {
1056 struct ccb_pathinq *cpi = &ccb->cpi;
1058 cpi->version_num = 1;
1059 cpi->hba_inquiry = PI_TAG_ABLE|PI_SDTR_ABLE;
1060 cpi->target_sprt = 0;
1061 cpi->hba_misc = PIM_NOBUSRESET;
1062 cpi->hba_eng_cnt = 0;
1063 cpi->max_target = STORVSC_MAX_TARGETS;
1064 cpi->max_lun = sc->hs_drv_props->drv_max_luns_per_target;
1065 cpi->initiator_id = cpi->max_target;
1066 cpi->bus_id = cam_sim_bus(sim);
1067 cpi->base_transfer_speed = 300000;
1068 cpi->transport = XPORT_SAS;
1069 cpi->transport_version = 0;
1070 cpi->protocol = PROTO_SCSI;
1071 cpi->protocol_version = SCSI_REV_SPC2;
1072 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1073 strncpy(cpi->hba_vid, sc->hs_drv_props->drv_name, HBA_IDLEN);
1074 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1075 cpi->unit_number = cam_sim_unit(sim);
1077 ccb->ccb_h.status = CAM_REQ_CMP;
1081 case XPT_GET_TRAN_SETTINGS: {
1082 struct ccb_trans_settings *cts = &ccb->cts;
1084 cts->transport = XPORT_SAS;
1085 cts->transport_version = 0;
1086 cts->protocol = PROTO_SCSI;
1087 cts->protocol_version = SCSI_REV_SPC2;
1089 /* enable tag queuing and disconnected mode */
1090 cts->proto_specific.valid = CTS_SCSI_VALID_TQ;
1091 cts->proto_specific.scsi.valid = CTS_SCSI_VALID_TQ;
1092 cts->proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB;
1093 cts->xport_specific.valid = CTS_SPI_VALID_DISC;
1094 cts->xport_specific.spi.flags = CTS_SPI_FLAGS_DISC_ENB;
1096 ccb->ccb_h.status = CAM_REQ_CMP;
1100 case XPT_SET_TRAN_SETTINGS: {
1101 ccb->ccb_h.status = CAM_REQ_CMP;
1105 case XPT_CALC_GEOMETRY:{
1106 cam_calc_geometry(&ccb->ccg, 1);
1111 case XPT_RESET_DEV:{
1113 if ((res = hv_storvsc_host_reset(sc->hs_dev)) != 0) {
1114 xpt_print(ccb->ccb_h.path,
1115 "hv_storvsc_host_reset failed with %d\n", res);
1116 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
1120 ccb->ccb_h.status = CAM_REQ_CMP;
1124 xpt_print(ccb->ccb_h.path,
1125 "%s reset not supported.\n",
1126 (ccb->ccb_h.func_code == XPT_RESET_BUS)?
1128 ccb->ccb_h.status = CAM_REQ_INVALID;
1131 #endif /* HVS_HOST_RESET */
1134 case XPT_IMMED_NOTIFY: {
1135 struct hv_storvsc_request *reqp = NULL;
1137 if (ccb->csio.cdb_len == 0) {
1138 panic("cdl_len is 0\n");
1141 if (LIST_EMPTY(&sc->hs_free_list)) {
1142 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1143 if (sc->hs_frozen == 0) {
1145 xpt_freeze_simq(sim, /* count*/1);
1151 reqp = LIST_FIRST(&sc->hs_free_list);
1152 LIST_REMOVE(reqp, link);
1154 bzero(reqp, sizeof(struct hv_storvsc_request));
1157 ccb->ccb_h.status |= CAM_SIM_QUEUED;
1158 create_storvsc_request(ccb, reqp);
1160 if (ccb->ccb_h.timeout != CAM_TIME_INFINITY) {
1161 callout_init(&reqp->callout, CALLOUT_MPSAFE);
1162 callout_reset(&reqp->callout,
1163 (ccb->ccb_h.timeout * hz) / 1000,
1164 storvsc_timeout, reqp);
1165 #if HVS_TIMEOUT_TEST
1166 cv_init(&reqp->event.cv, "storvsc timeout cv");
1167 mtx_init(&reqp->event.mtx, "storvsc timeout mutex",
1169 switch (reqp->vstor_packet.vm_srb.cdb[0]) {
1170 case MODE_SELECT_10:
1171 case SEND_DIAGNOSTIC:
1172 /* To have timer send the request. */
1177 #endif /* HVS_TIMEOUT_TEST */
1180 if ((res = hv_storvsc_io_request(sc->hs_dev, reqp)) != 0) {
1181 xpt_print(ccb->ccb_h.path,
1182 "hv_storvsc_io_request failed with %d\n", res);
1183 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
1184 storvsc_free_request(sc, reqp);
1192 ccb->ccb_h.status = CAM_REQ_INVALID;
1199 * @brief Fill in a request structure based on a CAM control block
1201 * Fills in a request structure based on the contents of a CAM control
1202 * block. The request structure holds the payload information for
1203 * VSCSI protocol request.
1205 * @param ccb pointer to a CAM contorl block
1206 * @param reqp pointer to a request structure
1209 create_storvsc_request(union ccb *ccb, struct hv_storvsc_request *reqp)
1211 struct ccb_scsiio *csio = &ccb->csio;
1213 uint32_t bytes_to_copy = 0;
1214 uint32_t pfn_num = 0;
1217 /* refer to struct vmscsi_req for meanings of these two fields */
1218 reqp->vstor_packet.u.vm_srb.port =
1219 cam_sim_unit(xpt_path_sim(ccb->ccb_h.path));
1220 reqp->vstor_packet.u.vm_srb.path_id =
1221 cam_sim_bus(xpt_path_sim(ccb->ccb_h.path));
1223 reqp->vstor_packet.u.vm_srb.target_id = ccb->ccb_h.target_id;
1224 reqp->vstor_packet.u.vm_srb.lun = ccb->ccb_h.target_lun;
1226 reqp->vstor_packet.u.vm_srb.cdb_len = csio->cdb_len;
1227 if(ccb->ccb_h.flags & CAM_CDB_POINTER) {
1228 memcpy(&reqp->vstor_packet.u.vm_srb.u.cdb, csio->cdb_io.cdb_ptr,
1231 memcpy(&reqp->vstor_packet.u.vm_srb.u.cdb, csio->cdb_io.cdb_bytes,
1235 switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
1237 reqp->vstor_packet.u.vm_srb.data_in = WRITE_TYPE;
1240 reqp->vstor_packet.u.vm_srb.data_in = READ_TYPE;
1243 reqp->vstor_packet.u.vm_srb.data_in = UNKNOWN_TYPE;
1246 reqp->vstor_packet.u.vm_srb.data_in = UNKNOWN_TYPE;
1250 reqp->sense_data = &csio->sense_data;
1251 reqp->sense_info_len = csio->sense_len;
1255 KASSERT((ccb->ccb_h.flags & CAM_SCATTER_VALID) == 0,
1256 ("ccb is scatter gather valid\n"));
1258 if (csio->dxfer_len != 0) {
1259 reqp->data_buf.length = csio->dxfer_len;
1260 bytes_to_copy = csio->dxfer_len;
1261 phys_addr = vtophys(csio->data_ptr);
1262 reqp->data_buf.offset = phys_addr - trunc_page(phys_addr);
1265 while (bytes_to_copy != 0) {
1266 int bytes, page_offset;
1267 phys_addr = vtophys(&csio->data_ptr[reqp->data_buf.length -
1269 pfn = phys_addr >> PAGE_SHIFT;
1270 reqp->data_buf.pfn_array[pfn_num] = pfn;
1271 page_offset = phys_addr - trunc_page(phys_addr);
1273 bytes = min(PAGE_SIZE - page_offset, bytes_to_copy);
1275 bytes_to_copy -= bytes;
1281 * @brief completion function before returning to CAM
1283 * I/O process has been completed and the result needs
1284 * to be passed to the CAM layer.
1285 * Free resources related to this request.
1287 * @param reqp pointer to a request structure
1290 storvsc_io_done(struct hv_storvsc_request *reqp)
1292 union ccb *ccb = reqp->ccb;
1293 struct ccb_scsiio *csio = &ccb->csio;
1294 struct storvsc_softc *sc = reqp->softc;
1295 struct vmscsi_req *vm_srb = &reqp->vstor_packet.u.vm_srb;
1297 if (reqp->retries > 0) {
1298 mtx_lock(&sc->hs_lock);
1299 #if HVS_TIMEOUT_TEST
1300 xpt_print(ccb->ccb_h.path,
1301 "%u: IO returned after timeout, "
1302 "waking up timer handler if any.\n", ticks);
1303 mtx_lock(&reqp->event.mtx);
1304 cv_signal(&reqp->event.cv);
1305 mtx_unlock(&reqp->event.mtx);
1308 xpt_print(ccb->ccb_h.path,
1309 "%u: IO returned after timeout, "
1310 "stopping timer if any.\n", ticks);
1311 mtx_unlock(&sc->hs_lock);
1315 * callout_drain() will wait for the timer handler to finish
1316 * if it is running. So we don't need any lock to synchronize
1317 * between this routine and the timer handler.
1318 * Note that we need to make sure reqp is not freed when timer
1319 * handler is using or will use it.
1321 if (ccb->ccb_h.timeout != CAM_TIME_INFINITY) {
1322 callout_drain(&reqp->callout);
1325 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1326 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1327 if (vm_srb->scsi_status == SCSI_STATUS_OK) {
1328 ccb->ccb_h.status |= CAM_REQ_CMP;
1330 mtx_lock(&sc->hs_lock);
1331 xpt_print(ccb->ccb_h.path,
1332 "srovsc scsi_status = %d\n",
1333 vm_srb->scsi_status);
1334 mtx_unlock(&sc->hs_lock);
1335 ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1338 ccb->csio.scsi_status = (vm_srb->scsi_status & 0xFF);
1339 ccb->csio.resid = ccb->csio.dxfer_len - vm_srb->transfer_len;
1341 if (reqp->sense_info_len != 0) {
1342 csio->sense_resid = csio->sense_len - reqp->sense_info_len;
1343 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
1346 mtx_lock(&sc->hs_lock);
1347 if (reqp->softc->hs_frozen == 1) {
1348 xpt_print(ccb->ccb_h.path,
1349 "%u: storvsc unfreezing softc 0x%p.\n",
1350 ticks, reqp->softc);
1351 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1352 reqp->softc->hs_frozen = 0;
1354 storvsc_free_request(sc, reqp);
1356 mtx_unlock(&sc->hs_lock);
1360 * @brief Free a request structure
1362 * Free a request structure by returning it to the free list
1364 * @param sc pointer to a softc
1365 * @param reqp pointer to a request structure
1368 storvsc_free_request(struct storvsc_softc *sc, struct hv_storvsc_request *reqp)
1371 LIST_INSERT_HEAD(&sc->hs_free_list, reqp, link);
1375 * @brief Determine type of storage device from GUID
1377 * Using the type GUID, determine if this is a StorVSC (paravirtual
1378 * SCSI or BlkVSC (paravirtual IDE) device.
1380 * @param dev a device
1383 static enum hv_storage_type
1384 storvsc_get_storage_type(device_t dev)
1386 const char *p = vmbus_get_type(dev);
1388 if (!memcmp(p, &gBlkVscDeviceType, sizeof(hv_guid))) {
1389 return DRIVER_BLKVSC;
1390 } else if (!memcmp(p, &gStorVscDeviceType, sizeof(hv_guid))) {
1391 return DRIVER_STORVSC;
1393 return (DRIVER_UNKNOWN);