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>
42 #include <sys/systm.h>
43 #include <sys/sockio.h>
45 #include <sys/malloc.h>
46 #include <sys/module.h>
47 #include <sys/kernel.h>
48 #include <sys/queue.h>
51 #include <sys/taskqueue.h>
53 #include <sys/mutex.h>
54 #include <sys/callout.h>
60 #include <sys/sglist.h>
61 #include <machine/bus.h>
62 #include <sys/bus_dma.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>
74 #include <dev/hyperv/include/hyperv.h>
75 #include "hv_vstorage.h"
77 #define STORVSC_RINGBUFFER_SIZE (20*PAGE_SIZE)
78 #define STORVSC_MAX_LUNS_PER_TARGET (64)
79 #define STORVSC_MAX_IO_REQUESTS (STORVSC_MAX_LUNS_PER_TARGET * 2)
80 #define BLKVSC_MAX_IDE_DISKS_PER_TARGET (1)
81 #define BLKVSC_MAX_IO_REQUESTS STORVSC_MAX_IO_REQUESTS
82 #define STORVSC_MAX_TARGETS (2)
84 #define STORVSC_WIN7_MAJOR 4
85 #define STORVSC_WIN7_MINOR 2
87 #define STORVSC_WIN8_MAJOR 5
88 #define STORVSC_WIN8_MINOR 1
90 #define HV_ALIGN(x, a) roundup2(x, a)
95 LIST_ENTRY(hv_sgl_node) link;
96 struct sglist *sgl_data;
99 struct hv_sgl_page_pool{
100 LIST_HEAD(, hv_sgl_node) in_use_sgl_list;
101 LIST_HEAD(, hv_sgl_node) free_sgl_list;
103 } g_hv_sgl_page_pool;
105 #define STORVSC_MAX_SG_PAGE_CNT STORVSC_MAX_IO_REQUESTS * HV_MAX_MULTIPAGE_BUFFER_COUNT
107 enum storvsc_request_type {
113 struct hv_storvsc_request {
114 LIST_ENTRY(hv_storvsc_request) link;
115 struct vstor_packet vstor_packet;
116 hv_vmbus_multipage_buffer data_buf;
118 uint8_t sense_info_len;
121 struct storvsc_softc *softc;
122 struct callout callout;
123 struct sema synch_sema; /*Synchronize the request/response if needed */
124 struct sglist *bounce_sgl;
125 unsigned int bounce_sgl_count;
126 uint64_t not_aligned_seg_bits;
129 struct storvsc_softc {
130 struct hv_device *hs_dev;
131 LIST_HEAD(, hv_storvsc_request) hs_free_list;
133 struct storvsc_driver_props *hs_drv_props;
136 struct cam_sim *hs_sim;
137 struct cam_path *hs_path;
138 uint32_t hs_num_out_reqs;
139 boolean_t hs_destroy;
140 boolean_t hs_drain_notify;
141 boolean_t hs_open_multi_channel;
142 struct sema hs_drain_sema;
143 struct hv_storvsc_request hs_init_req;
144 struct hv_storvsc_request hs_reset_req;
149 * HyperV storvsc timeout testing cases:
150 * a. IO returned after first timeout;
151 * b. IO returned after second timeout and queue freeze;
152 * c. IO returned while timer handler is running
153 * The first can be tested by "sg_senddiag -vv /dev/daX",
154 * and the second and third can be done by
155 * "sg_wr_mode -v -p 08 -c 0,1a -m 0,ff /dev/daX".
157 #define HVS_TIMEOUT_TEST 0
160 * Bus/adapter reset functionality on the Hyper-V host is
161 * buggy and it will be disabled until
162 * it can be further tested.
164 #define HVS_HOST_RESET 0
166 struct storvsc_driver_props {
169 uint8_t drv_max_luns_per_target;
170 uint8_t drv_max_ios_per_target;
171 uint32_t drv_ringbuffer_size;
174 enum hv_storage_type {
180 #define HS_MAX_ADAPTERS 10
182 #define HV_STORAGE_SUPPORTS_MULTI_CHANNEL 0x1
184 /* {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f} */
185 static const hv_guid gStorVscDeviceType={
186 .data = {0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
187 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f}
190 /* {32412632-86cb-44a2-9b5c-50d1417354f5} */
191 static const hv_guid gBlkVscDeviceType={
192 .data = {0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44,
193 0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5}
196 static struct storvsc_driver_props g_drv_props_table[] = {
197 {"blkvsc", "Hyper-V IDE Storage Interface",
198 BLKVSC_MAX_IDE_DISKS_PER_TARGET, BLKVSC_MAX_IO_REQUESTS,
199 STORVSC_RINGBUFFER_SIZE},
200 {"storvsc", "Hyper-V SCSI Storage Interface",
201 STORVSC_MAX_LUNS_PER_TARGET, STORVSC_MAX_IO_REQUESTS,
202 STORVSC_RINGBUFFER_SIZE}
205 static int storvsc_current_major;
206 static int storvsc_current_minor;
208 /* static functions */
209 static int storvsc_probe(device_t dev);
210 static int storvsc_attach(device_t dev);
211 static int storvsc_detach(device_t dev);
212 static void storvsc_poll(struct cam_sim * sim);
213 static void storvsc_action(struct cam_sim * sim, union ccb * ccb);
214 static int create_storvsc_request(union ccb *ccb, struct hv_storvsc_request *reqp);
215 static void storvsc_free_request(struct storvsc_softc *sc, struct hv_storvsc_request *reqp);
216 static enum hv_storage_type storvsc_get_storage_type(device_t dev);
217 static void hv_storvsc_on_channel_callback(void *context);
218 static void hv_storvsc_on_iocompletion( struct storvsc_softc *sc,
219 struct vstor_packet *vstor_packet,
220 struct hv_storvsc_request *request);
221 static int hv_storvsc_connect_vsp(struct hv_device *device);
222 static void storvsc_io_done(struct hv_storvsc_request *reqp);
223 static void storvsc_copy_sgl_to_bounce_buf(struct sglist *bounce_sgl,
224 bus_dma_segment_t *orig_sgl,
225 unsigned int orig_sgl_count,
227 void storvsc_copy_from_bounce_buf_to_sgl(bus_dma_segment_t *dest_sgl,
228 unsigned int dest_sgl_count,
229 struct sglist* src_sgl,
232 static device_method_t storvsc_methods[] = {
233 /* Device interface */
234 DEVMETHOD(device_probe, storvsc_probe),
235 DEVMETHOD(device_attach, storvsc_attach),
236 DEVMETHOD(device_detach, storvsc_detach),
237 DEVMETHOD(device_shutdown, bus_generic_shutdown),
241 static driver_t storvsc_driver = {
242 "storvsc", storvsc_methods, sizeof(struct storvsc_softc),
245 static devclass_t storvsc_devclass;
246 DRIVER_MODULE(storvsc, vmbus, storvsc_driver, storvsc_devclass, 0, 0);
247 MODULE_VERSION(storvsc, 1);
248 MODULE_DEPEND(storvsc, vmbus, 1, 1, 1);
252 * The host is capable of sending messages to us that are
253 * completely unsolicited. So, we need to address the race
254 * condition where we may be in the process of unloading the
255 * driver when the host may send us an unsolicited message.
256 * We address this issue by implementing a sequentially
257 * consistent protocol:
259 * 1. Channel callback is invoked while holding the the channel lock
260 * and an unloading driver will reset the channel callback under
261 * the protection of this channel lock.
263 * 2. To ensure bounded wait time for unloading a driver, we don't
264 * permit outgoing traffic once the device is marked as being
267 * 3. Once the device is marked as being destroyed, we only
268 * permit incoming traffic to properly account for
269 * packets already sent out.
271 static inline struct storvsc_softc *
272 get_stor_device(struct hv_device *device,
275 struct storvsc_softc *sc;
277 sc = device_get_softc(device->device);
284 * Here we permit outgoing I/O only
285 * if the device is not being destroyed.
288 if (sc->hs_destroy) {
293 * inbound case; if being destroyed
294 * only permit to account for
295 * messages already sent out.
297 if (sc->hs_destroy && (sc->hs_num_out_reqs == 0)) {
305 * @brief Callback handler, will be invoked when receive mutil-channel offer
307 * @param context new multi-channel
310 storvsc_handle_sc_creation(void *context)
312 hv_vmbus_channel *new_channel;
313 struct hv_device *device;
314 struct storvsc_softc *sc;
315 struct vmstor_chan_props props;
318 new_channel = (hv_vmbus_channel *)context;
319 device = new_channel->primary_channel->device;
320 sc = get_stor_device(device, TRUE);
324 if (FALSE == sc->hs_open_multi_channel)
327 memset(&props, 0, sizeof(props));
329 ret = hv_vmbus_channel_open(new_channel,
330 sc->hs_drv_props->drv_ringbuffer_size,
331 sc->hs_drv_props->drv_ringbuffer_size,
333 sizeof(struct vmstor_chan_props),
334 hv_storvsc_on_channel_callback,
341 * @brief Send multi-channel creation request to host
343 * @param device a Hyper-V device pointer
344 * @param max_chans the max channels supported by vmbus
347 storvsc_send_multichannel_request(struct hv_device *dev, int max_chans)
349 struct storvsc_softc *sc;
350 struct hv_storvsc_request *request;
351 struct vstor_packet *vstor_packet;
352 int request_channels_cnt = 0;
355 /* get multichannels count that need to create */
356 request_channels_cnt = MIN(max_chans, mp_ncpus);
358 sc = get_stor_device(dev, TRUE);
360 printf("Storvsc_error: get sc failed while send mutilchannel "
365 request = &sc->hs_init_req;
367 /* Establish a handler for multi-channel */
368 dev->channel->sc_creation_callback = storvsc_handle_sc_creation;
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 = hv_vmbus_channel_send_packet(
384 sizeof(struct vstor_packet),
385 (uint64_t)(uintptr_t)request,
386 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND,
387 HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
389 /* wait for 5 seconds */
390 ret = sema_timedwait(&request->synch_sema, 5 * hz);
392 printf("Storvsc_error: create multi-channel timeout, %d\n",
397 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETEIO ||
398 vstor_packet->status != 0) {
399 printf("Storvsc_error: create multi-channel invalid operation "
400 "(%d) or statue (%u)\n",
401 vstor_packet->operation, vstor_packet->status);
405 sc->hs_open_multi_channel = TRUE;
408 printf("Storvsc create multi-channel success!\n");
412 * @brief initialize channel connection to parent partition
414 * @param dev a Hyper-V device pointer
415 * @returns 0 on success, non-zero error on failure
418 hv_storvsc_channel_init(struct hv_device *dev)
421 struct hv_storvsc_request *request;
422 struct vstor_packet *vstor_packet;
423 struct storvsc_softc *sc;
424 uint16_t max_chans = 0;
425 boolean_t support_multichannel = FALSE;
428 support_multichannel = FALSE;
430 sc = get_stor_device(dev, TRUE);
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 = hv_vmbus_channel_send_packet(
451 sizeof(struct vstor_packet),
452 (uint64_t)(uintptr_t)request,
453 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND,
454 HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
460 ret = sema_timedwait(&request->synch_sema, 5 * hz);
464 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETEIO ||
465 vstor_packet->status != 0) {
469 /* reuse the packet for version range supported */
471 memset(vstor_packet, 0, sizeof(struct vstor_packet));
472 vstor_packet->operation = VSTOR_OPERATION_QUERYPROTOCOLVERSION;
473 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
475 vstor_packet->u.version.major_minor =
476 VMSTOR_PROTOCOL_VERSION(storvsc_current_major, storvsc_current_minor);
478 /* revision is only significant for Windows guests */
479 vstor_packet->u.version.revision = 0;
481 ret = hv_vmbus_channel_send_packet(
484 sizeof(struct vstor_packet),
485 (uint64_t)(uintptr_t)request,
486 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND,
487 HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
493 ret = sema_timedwait(&request->synch_sema, 5 * hz);
498 /* TODO: Check returned version */
499 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETEIO ||
500 vstor_packet->status != 0)
504 * Query channel properties
506 memset(vstor_packet, 0, sizeof(struct vstor_packet));
507 vstor_packet->operation = VSTOR_OPERATION_QUERYPROPERTIES;
508 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
510 ret = hv_vmbus_channel_send_packet(
513 sizeof(struct vstor_packet),
514 (uint64_t)(uintptr_t)request,
515 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND,
516 HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
522 ret = sema_timedwait(&request->synch_sema, 5 * hz);
527 /* TODO: Check returned version */
528 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETEIO ||
529 vstor_packet->status != 0) {
533 /* multi-channels feature is supported by WIN8 and above version */
534 max_chans = vstor_packet->u.chan_props.max_channel_cnt;
535 if ((hv_vmbus_protocal_version != HV_VMBUS_VERSION_WIN7) &&
536 (hv_vmbus_protocal_version != HV_VMBUS_VERSION_WS2008) &&
537 (vstor_packet->u.chan_props.flags &
538 HV_STORAGE_SUPPORTS_MULTI_CHANNEL)) {
539 support_multichannel = TRUE;
542 memset(vstor_packet, 0, sizeof(struct vstor_packet));
543 vstor_packet->operation = VSTOR_OPERATION_ENDINITIALIZATION;
544 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
546 ret = hv_vmbus_channel_send_packet(
549 sizeof(struct vstor_packet),
550 (uint64_t)(uintptr_t)request,
551 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND,
552 HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
559 ret = sema_timedwait(&request->synch_sema, 5 * hz);
564 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETEIO ||
565 vstor_packet->status != 0)
569 * If multi-channel is supported, send multichannel create
572 if (support_multichannel)
573 storvsc_send_multichannel_request(dev, max_chans);
576 sema_destroy(&request->synch_sema);
581 * @brief Open channel connection to paraent partition StorVSP driver
583 * Open and initialize channel connection to parent partition StorVSP driver.
585 * @param pointer to a Hyper-V device
586 * @returns 0 on success, non-zero error on failure
589 hv_storvsc_connect_vsp(struct hv_device *dev)
592 struct vmstor_chan_props props;
593 struct storvsc_softc *sc;
595 sc = device_get_softc(dev->device);
597 memset(&props, 0, sizeof(struct vmstor_chan_props));
603 ret = hv_vmbus_channel_open(
605 sc->hs_drv_props->drv_ringbuffer_size,
606 sc->hs_drv_props->drv_ringbuffer_size,
608 sizeof(struct vmstor_chan_props),
609 hv_storvsc_on_channel_callback,
616 ret = hv_storvsc_channel_init(dev);
623 hv_storvsc_host_reset(struct hv_device *dev)
626 struct storvsc_softc *sc;
628 struct hv_storvsc_request *request;
629 struct vstor_packet *vstor_packet;
631 sc = get_stor_device(dev, TRUE);
636 request = &sc->hs_reset_req;
638 vstor_packet = &request->vstor_packet;
640 sema_init(&request->synch_sema, 0, "stor synch sema");
642 vstor_packet->operation = VSTOR_OPERATION_RESETBUS;
643 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
645 ret = hv_vmbus_channel_send_packet(dev->channel,
647 sizeof(struct vstor_packet),
648 (uint64_t)(uintptr_t)&sc->hs_reset_req,
649 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND,
650 HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
656 ret = sema_timedwait(&request->synch_sema, 5 * hz); /* KYS 5 seconds */
664 * At this point, all outstanding requests in the adapter
665 * should have been flushed out and return to us
669 sema_destroy(&request->synch_sema);
672 #endif /* HVS_HOST_RESET */
675 * @brief Function to initiate an I/O request
677 * @param device Hyper-V device pointer
678 * @param request pointer to a request structure
679 * @returns 0 on success, non-zero error on failure
682 hv_storvsc_io_request(struct hv_device *device,
683 struct hv_storvsc_request *request)
685 struct storvsc_softc *sc;
686 struct vstor_packet *vstor_packet = &request->vstor_packet;
687 struct hv_vmbus_channel* outgoing_channel = NULL;
690 sc = get_stor_device(device, TRUE);
696 vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
698 vstor_packet->u.vm_srb.length = sizeof(struct vmscsi_req);
700 vstor_packet->u.vm_srb.sense_info_len = SENSE_BUFFER_SIZE;
702 vstor_packet->u.vm_srb.transfer_len = request->data_buf.length;
704 vstor_packet->operation = VSTOR_OPERATION_EXECUTESRB;
706 outgoing_channel = vmbus_select_outgoing_channel(device->channel);
708 mtx_unlock(&request->softc->hs_lock);
709 if (request->data_buf.length) {
710 ret = hv_vmbus_channel_send_packet_multipagebuffer(
714 sizeof(struct vstor_packet),
715 (uint64_t)(uintptr_t)request);
718 ret = hv_vmbus_channel_send_packet(
721 sizeof(struct vstor_packet),
722 (uint64_t)(uintptr_t)request,
723 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND,
724 HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
726 mtx_lock(&request->softc->hs_lock);
729 printf("Unable to send packet %p ret %d", vstor_packet, ret);
731 atomic_add_int(&sc->hs_num_out_reqs, 1);
739 * Process IO_COMPLETION_OPERATION and ready
740 * the result to be completed for upper layer
741 * processing by the CAM layer.
744 hv_storvsc_on_iocompletion(struct storvsc_softc *sc,
745 struct vstor_packet *vstor_packet,
746 struct hv_storvsc_request *request)
748 struct vmscsi_req *vm_srb;
750 vm_srb = &vstor_packet->u.vm_srb;
752 if (((vm_srb->scsi_status & 0xFF) == SCSI_STATUS_CHECK_COND) &&
753 (vm_srb->srb_status & SRB_STATUS_AUTOSENSE_VALID)) {
754 /* Autosense data available */
756 KASSERT(vm_srb->sense_info_len <= request->sense_info_len,
757 ("vm_srb->sense_info_len <= "
758 "request->sense_info_len"));
760 memcpy(request->sense_data, vm_srb->u.sense_data,
761 vm_srb->sense_info_len);
763 request->sense_info_len = vm_srb->sense_info_len;
766 /* Complete request by passing to the CAM layer */
767 storvsc_io_done(request);
768 atomic_subtract_int(&sc->hs_num_out_reqs, 1);
769 if (sc->hs_drain_notify && (sc->hs_num_out_reqs == 0)) {
770 sema_post(&sc->hs_drain_sema);
775 hv_storvsc_on_channel_callback(void *context)
778 hv_vmbus_channel *channel = (hv_vmbus_channel *)context;
779 struct hv_device *device = NULL;
780 struct storvsc_softc *sc;
781 uint32_t bytes_recvd;
783 uint8_t packet[roundup2(sizeof(struct vstor_packet), 8)];
784 struct hv_storvsc_request *request;
785 struct vstor_packet *vstor_packet;
787 if (channel->primary_channel != NULL){
788 device = channel->primary_channel->device;
790 device = channel->device;
793 KASSERT(device, ("device is NULL"));
795 sc = get_stor_device(device, FALSE);
797 printf("Storvsc_error: get stor device failed.\n");
801 ret = hv_vmbus_channel_recv_packet(
804 roundup2(sizeof(struct vstor_packet), 8),
808 while ((ret == 0) && (bytes_recvd > 0)) {
809 request = (struct hv_storvsc_request *)(uintptr_t)request_id;
811 if ((request == &sc->hs_init_req) ||
812 (request == &sc->hs_reset_req)) {
813 memcpy(&request->vstor_packet, packet,
814 sizeof(struct vstor_packet));
815 sema_post(&request->synch_sema);
817 vstor_packet = (struct vstor_packet *)packet;
818 switch(vstor_packet->operation) {
819 case VSTOR_OPERATION_COMPLETEIO:
821 panic("VMBUS: storvsc received a "
822 "packet with NULL request id in "
823 "COMPLETEIO operation.");
825 hv_storvsc_on_iocompletion(sc,
826 vstor_packet, request);
828 case VSTOR_OPERATION_REMOVEDEVICE:
829 case VSTOR_OPERATION_ENUMERATE_BUS:
830 printf("VMBUS: storvsc operation %d not "
831 "implemented.\n", vstor_packet->operation);
832 /* TODO: implement */
838 ret = hv_vmbus_channel_recv_packet(
841 roundup2(sizeof(struct vstor_packet), 8),
848 * @brief StorVSC probe function
850 * Device probe function. Returns 0 if the input device is a StorVSC
851 * device. Otherwise, a ENXIO is returned. If the input device is
852 * for BlkVSC (paravirtual IDE) device and this support is disabled in
853 * favor of the emulated ATA/IDE device, return ENXIO.
856 * @returns 0 on success, ENXIO if not a matcing StorVSC device
859 storvsc_probe(device_t dev)
861 int ata_disk_enable = 0;
864 if ((HV_VMBUS_VERSION_WIN8 == hv_vmbus_protocal_version) ||
865 (HV_VMBUS_VERSION_WIN8_1 == hv_vmbus_protocal_version)){
866 storvsc_current_major = STORVSC_WIN8_MAJOR;
867 storvsc_current_minor = STORVSC_WIN8_MINOR;
869 storvsc_current_major = STORVSC_WIN7_MAJOR;
870 storvsc_current_minor = STORVSC_WIN7_MINOR;
873 switch (storvsc_get_storage_type(dev)) {
876 device_printf(dev, "DRIVER_BLKVSC-Emulated ATA/IDE probe\n");
877 if (!getenv_int("hw.ata.disk_enable", &ata_disk_enable)) {
880 "Enlightened ATA/IDE detected\n");
881 ret = BUS_PROBE_DEFAULT;
882 } else if(bootverbose)
883 device_printf(dev, "Emulated ATA/IDE set (hw.ata.disk_enable set)\n");
887 device_printf(dev, "Enlightened SCSI device detected\n");
888 ret = BUS_PROBE_DEFAULT;
897 * @brief StorVSC attach function
899 * Function responsible for allocating per-device structures,
900 * setting up CAM interfaces and scanning for available LUNs to
901 * be used for SCSI device peripherals.
904 * @returns 0 on success or an error on failure
907 storvsc_attach(device_t dev)
909 struct hv_device *hv_dev = vmbus_get_devctx(dev);
910 enum hv_storage_type stor_type;
911 struct storvsc_softc *sc;
912 struct cam_devq *devq;
914 struct hv_storvsc_request *reqp;
915 struct root_hold_token *root_mount_token = NULL;
916 struct hv_sgl_node *sgl_node = NULL;
917 void *tmp_buff = NULL;
920 * We need to serialize storvsc attach calls.
922 root_mount_token = root_mount_hold("storvsc");
924 sc = device_get_softc(dev);
930 stor_type = storvsc_get_storage_type(dev);
932 if (stor_type == DRIVER_UNKNOWN) {
937 bzero(sc, sizeof(struct storvsc_softc));
939 /* fill in driver specific properties */
940 sc->hs_drv_props = &g_drv_props_table[stor_type];
942 /* fill in device specific properties */
943 sc->hs_unit = device_get_unit(dev);
945 device_set_desc(dev, g_drv_props_table[stor_type].drv_desc);
947 LIST_INIT(&sc->hs_free_list);
948 mtx_init(&sc->hs_lock, "hvslck", NULL, MTX_DEF);
950 for (i = 0; i < sc->hs_drv_props->drv_max_ios_per_target; ++i) {
951 reqp = malloc(sizeof(struct hv_storvsc_request),
952 M_DEVBUF, M_WAITOK|M_ZERO);
955 LIST_INSERT_HEAD(&sc->hs_free_list, reqp, link);
958 /* create sg-list page pool */
959 if (FALSE == g_hv_sgl_page_pool.is_init) {
960 g_hv_sgl_page_pool.is_init = TRUE;
961 LIST_INIT(&g_hv_sgl_page_pool.in_use_sgl_list);
962 LIST_INIT(&g_hv_sgl_page_pool.free_sgl_list);
965 * Pre-create SG list, each SG list with
966 * HV_MAX_MULTIPAGE_BUFFER_COUNT segments, each
967 * segment has one page buffer
969 for (i = 0; i < STORVSC_MAX_IO_REQUESTS; i++) {
970 sgl_node = malloc(sizeof(struct hv_sgl_node),
971 M_DEVBUF, M_WAITOK|M_ZERO);
974 sglist_alloc(HV_MAX_MULTIPAGE_BUFFER_COUNT,
977 for (j = 0; j < HV_MAX_MULTIPAGE_BUFFER_COUNT; j++) {
978 tmp_buff = malloc(PAGE_SIZE,
979 M_DEVBUF, M_WAITOK|M_ZERO);
981 sgl_node->sgl_data->sg_segs[j].ss_paddr =
982 (vm_paddr_t)tmp_buff;
985 LIST_INSERT_HEAD(&g_hv_sgl_page_pool.free_sgl_list,
990 sc->hs_destroy = FALSE;
991 sc->hs_drain_notify = FALSE;
992 sc->hs_open_multi_channel = FALSE;
993 sema_init(&sc->hs_drain_sema, 0, "Store Drain Sema");
995 ret = hv_storvsc_connect_vsp(hv_dev);
1001 * Create the device queue.
1002 * Hyper-V maps each target to one SCSI HBA
1004 devq = cam_simq_alloc(sc->hs_drv_props->drv_max_ios_per_target);
1006 device_printf(dev, "Failed to alloc device queue\n");
1011 sc->hs_sim = cam_sim_alloc(storvsc_action,
1013 sc->hs_drv_props->drv_name,
1017 sc->hs_drv_props->drv_max_ios_per_target,
1020 if (sc->hs_sim == NULL) {
1021 device_printf(dev, "Failed to alloc sim\n");
1022 cam_simq_free(devq);
1027 mtx_lock(&sc->hs_lock);
1028 /* bus_id is set to 0, need to get it from VMBUS channel query? */
1029 if (xpt_bus_register(sc->hs_sim, dev, 0) != CAM_SUCCESS) {
1030 cam_sim_free(sc->hs_sim, /*free_devq*/TRUE);
1031 mtx_unlock(&sc->hs_lock);
1032 device_printf(dev, "Unable to register SCSI bus\n");
1037 if (xpt_create_path(&sc->hs_path, /*periph*/NULL,
1038 cam_sim_path(sc->hs_sim),
1039 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1040 xpt_bus_deregister(cam_sim_path(sc->hs_sim));
1041 cam_sim_free(sc->hs_sim, /*free_devq*/TRUE);
1042 mtx_unlock(&sc->hs_lock);
1043 device_printf(dev, "Unable to create path\n");
1048 mtx_unlock(&sc->hs_lock);
1050 root_mount_rel(root_mount_token);
1055 root_mount_rel(root_mount_token);
1056 while (!LIST_EMPTY(&sc->hs_free_list)) {
1057 reqp = LIST_FIRST(&sc->hs_free_list);
1058 LIST_REMOVE(reqp, link);
1059 free(reqp, M_DEVBUF);
1062 while (!LIST_EMPTY(&g_hv_sgl_page_pool.free_sgl_list)) {
1063 sgl_node = LIST_FIRST(&g_hv_sgl_page_pool.free_sgl_list);
1064 LIST_REMOVE(sgl_node, link);
1065 for (j = 0; j < HV_MAX_MULTIPAGE_BUFFER_COUNT; j++) {
1067 (void*)sgl_node->sgl_data->sg_segs[j].ss_paddr) {
1068 free((void*)sgl_node->sgl_data->sg_segs[j].ss_paddr, M_DEVBUF);
1071 sglist_free(sgl_node->sgl_data);
1072 free(sgl_node, M_DEVBUF);
1079 * @brief StorVSC device detach function
1081 * This function is responsible for safely detaching a
1082 * StorVSC device. This includes waiting for inbound responses
1083 * to complete and freeing associated per-device structures.
1085 * @param dev a device
1086 * returns 0 on success
1089 storvsc_detach(device_t dev)
1091 struct storvsc_softc *sc = device_get_softc(dev);
1092 struct hv_storvsc_request *reqp = NULL;
1093 struct hv_device *hv_device = vmbus_get_devctx(dev);
1094 struct hv_sgl_node *sgl_node = NULL;
1097 mtx_lock(&hv_device->channel->inbound_lock);
1098 sc->hs_destroy = TRUE;
1099 mtx_unlock(&hv_device->channel->inbound_lock);
1102 * At this point, all outbound traffic should be disabled. We
1103 * only allow inbound traffic (responses) to proceed so that
1104 * outstanding requests can be completed.
1107 sc->hs_drain_notify = TRUE;
1108 sema_wait(&sc->hs_drain_sema);
1109 sc->hs_drain_notify = FALSE;
1112 * Since we have already drained, we don't need to busy wait.
1113 * The call to close the channel will reset the callback
1114 * under the protection of the incoming channel lock.
1117 hv_vmbus_channel_close(hv_device->channel);
1119 mtx_lock(&sc->hs_lock);
1120 while (!LIST_EMPTY(&sc->hs_free_list)) {
1121 reqp = LIST_FIRST(&sc->hs_free_list);
1122 LIST_REMOVE(reqp, link);
1124 free(reqp, M_DEVBUF);
1126 mtx_unlock(&sc->hs_lock);
1128 while (!LIST_EMPTY(&g_hv_sgl_page_pool.free_sgl_list)) {
1129 sgl_node = LIST_FIRST(&g_hv_sgl_page_pool.free_sgl_list);
1130 LIST_REMOVE(sgl_node, link);
1131 for (j = 0; j < HV_MAX_MULTIPAGE_BUFFER_COUNT; j++){
1133 (void*)sgl_node->sgl_data->sg_segs[j].ss_paddr) {
1134 free((void*)sgl_node->sgl_data->sg_segs[j].ss_paddr, M_DEVBUF);
1137 sglist_free(sgl_node->sgl_data);
1138 free(sgl_node, M_DEVBUF);
1144 #if HVS_TIMEOUT_TEST
1146 * @brief unit test for timed out operations
1148 * This function provides unit testing capability to simulate
1149 * timed out operations. Recompilation with HV_TIMEOUT_TEST=1
1152 * @param reqp pointer to a request structure
1153 * @param opcode SCSI operation being performed
1154 * @param wait if 1, wait for I/O to complete
1157 storvsc_timeout_test(struct hv_storvsc_request *reqp,
1158 uint8_t opcode, int wait)
1161 union ccb *ccb = reqp->ccb;
1162 struct storvsc_softc *sc = reqp->softc;
1164 if (reqp->vstor_packet.vm_srb.cdb[0] != opcode) {
1169 mtx_lock(&reqp->event.mtx);
1171 ret = hv_storvsc_io_request(sc->hs_dev, reqp);
1174 mtx_unlock(&reqp->event.mtx);
1176 printf("%s: io_request failed with %d.\n",
1178 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
1179 mtx_lock(&sc->hs_lock);
1180 storvsc_free_request(sc, reqp);
1182 mtx_unlock(&sc->hs_lock);
1187 xpt_print(ccb->ccb_h.path,
1188 "%u: %s: waiting for IO return.\n",
1190 ret = cv_timedwait(&reqp->event.cv, &reqp->event.mtx, 60*hz);
1191 mtx_unlock(&reqp->event.mtx);
1192 xpt_print(ccb->ccb_h.path, "%u: %s: %s.\n",
1193 ticks, __func__, (ret == 0)?
1194 "IO return detected" :
1195 "IO return not detected");
1197 * Now both the timer handler and io done are running
1198 * simultaneously. We want to confirm the io done always
1199 * finishes after the timer handler exits. So reqp used by
1200 * timer handler is not freed or stale. Do busy loop for
1201 * another 1/10 second to make sure io done does
1202 * wait for the timer handler to complete.
1205 mtx_lock(&sc->hs_lock);
1206 xpt_print(ccb->ccb_h.path,
1207 "%u: %s: finishing, queue frozen %d, "
1208 "ccb status 0x%x scsi_status 0x%x.\n",
1209 ticks, __func__, sc->hs_frozen,
1211 ccb->csio.scsi_status);
1212 mtx_unlock(&sc->hs_lock);
1215 #endif /* HVS_TIMEOUT_TEST */
1218 * @brief timeout handler for requests
1220 * This function is called as a result of a callout expiring.
1222 * @param arg pointer to a request
1225 storvsc_timeout(void *arg)
1227 struct hv_storvsc_request *reqp = arg;
1228 struct storvsc_softc *sc = reqp->softc;
1229 union ccb *ccb = reqp->ccb;
1231 if (reqp->retries == 0) {
1232 mtx_lock(&sc->hs_lock);
1233 xpt_print(ccb->ccb_h.path,
1234 "%u: IO timed out (req=0x%p), wait for another %u secs.\n",
1235 ticks, reqp, ccb->ccb_h.timeout / 1000);
1236 cam_error_print(ccb, CAM_ESF_ALL, CAM_EPF_ALL);
1237 mtx_unlock(&sc->hs_lock);
1240 callout_reset_sbt(&reqp->callout, SBT_1MS * ccb->ccb_h.timeout,
1241 0, storvsc_timeout, reqp, 0);
1242 #if HVS_TIMEOUT_TEST
1243 storvsc_timeout_test(reqp, SEND_DIAGNOSTIC, 0);
1248 mtx_lock(&sc->hs_lock);
1249 xpt_print(ccb->ccb_h.path,
1250 "%u: IO (reqp = 0x%p) did not return for %u seconds, %s.\n",
1251 ticks, reqp, ccb->ccb_h.timeout * (reqp->retries+1) / 1000,
1252 (sc->hs_frozen == 0)?
1253 "freezing the queue" : "the queue is already frozen");
1254 if (sc->hs_frozen == 0) {
1256 xpt_freeze_simq(xpt_path_sim(ccb->ccb_h.path), 1);
1258 mtx_unlock(&sc->hs_lock);
1260 #if HVS_TIMEOUT_TEST
1261 storvsc_timeout_test(reqp, MODE_SELECT_10, 1);
1266 * @brief StorVSC device poll function
1268 * This function is responsible for servicing requests when
1269 * interrupts are disabled (i.e when we are dumping core.)
1271 * @param sim a pointer to a CAM SCSI interface module
1274 storvsc_poll(struct cam_sim *sim)
1276 struct storvsc_softc *sc = cam_sim_softc(sim);
1278 mtx_assert(&sc->hs_lock, MA_OWNED);
1279 mtx_unlock(&sc->hs_lock);
1280 hv_storvsc_on_channel_callback(sc->hs_dev->channel);
1281 mtx_lock(&sc->hs_lock);
1285 * @brief StorVSC device action function
1287 * This function is responsible for handling SCSI operations which
1288 * are passed from the CAM layer. The requests are in the form of
1289 * CAM control blocks which indicate the action being performed.
1290 * Not all actions require converting the request to a VSCSI protocol
1291 * message - these actions can be responded to by this driver.
1292 * Requests which are destined for a backend storage device are converted
1293 * to a VSCSI protocol message and sent on the channel connection associated
1296 * @param sim pointer to a CAM SCSI interface module
1297 * @param ccb pointer to a CAM control block
1300 storvsc_action(struct cam_sim *sim, union ccb *ccb)
1302 struct storvsc_softc *sc = cam_sim_softc(sim);
1305 mtx_assert(&sc->hs_lock, MA_OWNED);
1306 switch (ccb->ccb_h.func_code) {
1307 case XPT_PATH_INQ: {
1308 struct ccb_pathinq *cpi = &ccb->cpi;
1310 cpi->version_num = 1;
1311 cpi->hba_inquiry = PI_TAG_ABLE|PI_SDTR_ABLE;
1312 cpi->target_sprt = 0;
1313 cpi->hba_misc = PIM_NOBUSRESET;
1314 cpi->hba_eng_cnt = 0;
1315 cpi->max_target = STORVSC_MAX_TARGETS;
1316 cpi->max_lun = sc->hs_drv_props->drv_max_luns_per_target;
1317 cpi->initiator_id = cpi->max_target;
1318 cpi->bus_id = cam_sim_bus(sim);
1319 cpi->base_transfer_speed = 300000;
1320 cpi->transport = XPORT_SAS;
1321 cpi->transport_version = 0;
1322 cpi->protocol = PROTO_SCSI;
1323 cpi->protocol_version = SCSI_REV_SPC2;
1324 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1325 strncpy(cpi->hba_vid, sc->hs_drv_props->drv_name, HBA_IDLEN);
1326 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1327 cpi->unit_number = cam_sim_unit(sim);
1329 ccb->ccb_h.status = CAM_REQ_CMP;
1333 case XPT_GET_TRAN_SETTINGS: {
1334 struct ccb_trans_settings *cts = &ccb->cts;
1336 cts->transport = XPORT_SAS;
1337 cts->transport_version = 0;
1338 cts->protocol = PROTO_SCSI;
1339 cts->protocol_version = SCSI_REV_SPC2;
1341 /* enable tag queuing and disconnected mode */
1342 cts->proto_specific.valid = CTS_SCSI_VALID_TQ;
1343 cts->proto_specific.scsi.valid = CTS_SCSI_VALID_TQ;
1344 cts->proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB;
1345 cts->xport_specific.valid = CTS_SPI_VALID_DISC;
1346 cts->xport_specific.spi.flags = CTS_SPI_FLAGS_DISC_ENB;
1348 ccb->ccb_h.status = CAM_REQ_CMP;
1352 case XPT_SET_TRAN_SETTINGS: {
1353 ccb->ccb_h.status = CAM_REQ_CMP;
1357 case XPT_CALC_GEOMETRY:{
1358 cam_calc_geometry(&ccb->ccg, 1);
1363 case XPT_RESET_DEV:{
1365 if ((res = hv_storvsc_host_reset(sc->hs_dev)) != 0) {
1366 xpt_print(ccb->ccb_h.path,
1367 "hv_storvsc_host_reset failed with %d\n", res);
1368 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
1372 ccb->ccb_h.status = CAM_REQ_CMP;
1376 xpt_print(ccb->ccb_h.path,
1377 "%s reset not supported.\n",
1378 (ccb->ccb_h.func_code == XPT_RESET_BUS)?
1380 ccb->ccb_h.status = CAM_REQ_INVALID;
1383 #endif /* HVS_HOST_RESET */
1386 case XPT_IMMED_NOTIFY: {
1387 struct hv_storvsc_request *reqp = NULL;
1389 if (ccb->csio.cdb_len == 0) {
1390 panic("cdl_len is 0\n");
1393 if (LIST_EMPTY(&sc->hs_free_list)) {
1394 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1395 if (sc->hs_frozen == 0) {
1397 xpt_freeze_simq(sim, /* count*/1);
1403 reqp = LIST_FIRST(&sc->hs_free_list);
1404 LIST_REMOVE(reqp, link);
1406 bzero(reqp, sizeof(struct hv_storvsc_request));
1409 ccb->ccb_h.status |= CAM_SIM_QUEUED;
1410 if ((res = create_storvsc_request(ccb, reqp)) != 0) {
1411 ccb->ccb_h.status = CAM_REQ_INVALID;
1416 if (ccb->ccb_h.timeout != CAM_TIME_INFINITY) {
1417 callout_init(&reqp->callout, CALLOUT_MPSAFE);
1418 callout_reset_sbt(&reqp->callout,
1419 SBT_1MS * ccb->ccb_h.timeout, 0,
1420 storvsc_timeout, reqp, 0);
1421 #if HVS_TIMEOUT_TEST
1422 cv_init(&reqp->event.cv, "storvsc timeout cv");
1423 mtx_init(&reqp->event.mtx, "storvsc timeout mutex",
1425 switch (reqp->vstor_packet.vm_srb.cdb[0]) {
1426 case MODE_SELECT_10:
1427 case SEND_DIAGNOSTIC:
1428 /* To have timer send the request. */
1433 #endif /* HVS_TIMEOUT_TEST */
1436 if ((res = hv_storvsc_io_request(sc->hs_dev, reqp)) != 0) {
1437 xpt_print(ccb->ccb_h.path,
1438 "hv_storvsc_io_request failed with %d\n", res);
1439 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
1440 storvsc_free_request(sc, reqp);
1448 ccb->ccb_h.status = CAM_REQ_INVALID;
1455 * @brief destroy bounce buffer
1457 * This function is responsible for destroy a Scatter/Gather list
1458 * that create by storvsc_create_bounce_buffer()
1460 * @param sgl- the Scatter/Gather need be destroy
1461 * @param sg_count- page count of the SG list.
1465 storvsc_destroy_bounce_buffer(struct sglist *sgl)
1467 struct hv_sgl_node *sgl_node = NULL;
1469 sgl_node = LIST_FIRST(&g_hv_sgl_page_pool.in_use_sgl_list);
1470 LIST_REMOVE(sgl_node, link);
1471 if (NULL == sgl_node) {
1472 printf("storvsc error: not enough in use sgl\n");
1475 sgl_node->sgl_data = sgl;
1476 LIST_INSERT_HEAD(&g_hv_sgl_page_pool.free_sgl_list, sgl_node, link);
1480 * @brief create bounce buffer
1482 * This function is responsible for create a Scatter/Gather list,
1483 * which hold several pages that can be aligned with page size.
1485 * @param seg_count- SG-list segments count
1486 * @param write - if WRITE_TYPE, set SG list page used size to 0,
1487 * otherwise set used size to page size.
1489 * return NULL if create failed
1491 static struct sglist *
1492 storvsc_create_bounce_buffer(uint16_t seg_count, int write)
1495 struct sglist *bounce_sgl = NULL;
1496 unsigned int buf_len = ((write == WRITE_TYPE) ? 0 : PAGE_SIZE);
1497 struct hv_sgl_node *sgl_node = NULL;
1499 /* get struct sglist from free_sgl_list */
1500 sgl_node = LIST_FIRST(&g_hv_sgl_page_pool.free_sgl_list);
1501 LIST_REMOVE(sgl_node, link);
1502 if (NULL == sgl_node) {
1503 printf("storvsc error: not enough free sgl\n");
1506 bounce_sgl = sgl_node->sgl_data;
1507 LIST_INSERT_HEAD(&g_hv_sgl_page_pool.in_use_sgl_list, sgl_node, link);
1509 bounce_sgl->sg_maxseg = seg_count;
1511 if (write == WRITE_TYPE)
1512 bounce_sgl->sg_nseg = 0;
1514 bounce_sgl->sg_nseg = seg_count;
1516 for (i = 0; i < seg_count; i++)
1517 bounce_sgl->sg_segs[i].ss_len = buf_len;
1523 * @brief copy data from SG list to bounce buffer
1525 * This function is responsible for copy data from one SG list's segments
1526 * to another SG list which used as bounce buffer.
1528 * @param bounce_sgl - the destination SG list
1529 * @param orig_sgl - the segment of the source SG list.
1530 * @param orig_sgl_count - the count of segments.
1531 * @param orig_sgl_count - indicate which segment need bounce buffer,
1536 storvsc_copy_sgl_to_bounce_buf(struct sglist *bounce_sgl,
1537 bus_dma_segment_t *orig_sgl,
1538 unsigned int orig_sgl_count,
1541 int src_sgl_idx = 0;
1543 for (src_sgl_idx = 0; src_sgl_idx < orig_sgl_count; src_sgl_idx++) {
1544 if (seg_bits & (1 << src_sgl_idx)) {
1545 memcpy((void*)bounce_sgl->sg_segs[src_sgl_idx].ss_paddr,
1546 (void*)orig_sgl[src_sgl_idx].ds_addr,
1547 orig_sgl[src_sgl_idx].ds_len);
1549 bounce_sgl->sg_segs[src_sgl_idx].ss_len =
1550 orig_sgl[src_sgl_idx].ds_len;
1556 * @brief copy data from SG list which used as bounce to another SG list
1558 * This function is responsible for copy data from one SG list with bounce
1559 * buffer to another SG list's segments.
1561 * @param dest_sgl - the destination SG list's segments
1562 * @param dest_sgl_count - the count of destination SG list's segment.
1563 * @param src_sgl - the source SG list.
1564 * @param seg_bits - indicate which segment used bounce buffer of src SG-list.
1568 storvsc_copy_from_bounce_buf_to_sgl(bus_dma_segment_t *dest_sgl,
1569 unsigned int dest_sgl_count,
1570 struct sglist* src_sgl,
1575 for (sgl_idx = 0; sgl_idx < dest_sgl_count; sgl_idx++) {
1576 if (seg_bits & (1 << sgl_idx)) {
1577 memcpy((void*)(dest_sgl[sgl_idx].ds_addr),
1578 (void*)(src_sgl->sg_segs[sgl_idx].ss_paddr),
1579 src_sgl->sg_segs[sgl_idx].ss_len);
1585 * @brief check SG list with bounce buffer or not
1587 * This function is responsible for check if need bounce buffer for SG list.
1589 * @param sgl - the SG list's segments
1590 * @param sg_count - the count of SG list's segment.
1591 * @param bits - segmengs number that need bounce buffer
1593 * return -1 if SG list needless bounce buffer
1596 storvsc_check_bounce_buffer_sgl(bus_dma_segment_t *sgl,
1597 unsigned int sg_count,
1602 uint64_t phys_addr = 0;
1603 uint64_t tmp_bits = 0;
1604 boolean_t found_hole = FALSE;
1605 boolean_t pre_aligned = TRUE;
1613 phys_addr = vtophys(sgl[0].ds_addr);
1614 offset = phys_addr - trunc_page(phys_addr);
1617 pre_aligned = FALSE;
1621 for (i = 1; i < sg_count; i++) {
1622 phys_addr = vtophys(sgl[i].ds_addr);
1623 offset = phys_addr - trunc_page(phys_addr);
1626 if (FALSE == pre_aligned){
1628 * This segment is aligned, if the previous
1629 * one is not aligned, find a hole
1637 if (phys_addr != vtophys(sgl[i-1].ds_addr +
1640 * Check whether connect to previous
1641 * segment,if not, find the hole
1648 pre_aligned = FALSE;
1661 * @brief Fill in a request structure based on a CAM control block
1663 * Fills in a request structure based on the contents of a CAM control
1664 * block. The request structure holds the payload information for
1665 * VSCSI protocol request.
1667 * @param ccb pointer to a CAM contorl block
1668 * @param reqp pointer to a request structure
1671 create_storvsc_request(union ccb *ccb, struct hv_storvsc_request *reqp)
1673 struct ccb_scsiio *csio = &ccb->csio;
1675 uint32_t bytes_to_copy = 0;
1676 uint32_t pfn_num = 0;
1678 uint64_t not_aligned_seg_bits = 0;
1680 /* refer to struct vmscsi_req for meanings of these two fields */
1681 reqp->vstor_packet.u.vm_srb.port =
1682 cam_sim_unit(xpt_path_sim(ccb->ccb_h.path));
1683 reqp->vstor_packet.u.vm_srb.path_id =
1684 cam_sim_bus(xpt_path_sim(ccb->ccb_h.path));
1686 reqp->vstor_packet.u.vm_srb.target_id = ccb->ccb_h.target_id;
1687 reqp->vstor_packet.u.vm_srb.lun = ccb->ccb_h.target_lun;
1689 reqp->vstor_packet.u.vm_srb.cdb_len = csio->cdb_len;
1690 if(ccb->ccb_h.flags & CAM_CDB_POINTER) {
1691 memcpy(&reqp->vstor_packet.u.vm_srb.u.cdb, csio->cdb_io.cdb_ptr,
1694 memcpy(&reqp->vstor_packet.u.vm_srb.u.cdb, csio->cdb_io.cdb_bytes,
1698 switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
1700 reqp->vstor_packet.u.vm_srb.data_in = WRITE_TYPE;
1703 reqp->vstor_packet.u.vm_srb.data_in = READ_TYPE;
1706 reqp->vstor_packet.u.vm_srb.data_in = UNKNOWN_TYPE;
1709 reqp->vstor_packet.u.vm_srb.data_in = UNKNOWN_TYPE;
1713 reqp->sense_data = &csio->sense_data;
1714 reqp->sense_info_len = csio->sense_len;
1718 if (0 == csio->dxfer_len) {
1722 reqp->data_buf.length = csio->dxfer_len;
1724 switch (ccb->ccb_h.flags & CAM_DATA_MASK) {
1725 case CAM_DATA_VADDR:
1727 bytes_to_copy = csio->dxfer_len;
1728 phys_addr = vtophys(csio->data_ptr);
1729 reqp->data_buf.offset = phys_addr & PAGE_MASK;
1731 while (bytes_to_copy != 0) {
1732 int bytes, page_offset;
1734 vtophys(&csio->data_ptr[reqp->data_buf.length -
1736 pfn = phys_addr >> PAGE_SHIFT;
1737 reqp->data_buf.pfn_array[pfn_num] = pfn;
1738 page_offset = phys_addr & PAGE_MASK;
1740 bytes = min(PAGE_SIZE - page_offset, bytes_to_copy);
1742 bytes_to_copy -= bytes;
1754 bus_dma_segment_t *storvsc_sglist =
1755 (bus_dma_segment_t *)ccb->csio.data_ptr;
1756 u_int16_t storvsc_sg_count = ccb->csio.sglist_cnt;
1758 printf("Storvsc: get SG I/O operation, %d\n",
1759 reqp->vstor_packet.u.vm_srb.data_in);
1761 if (storvsc_sg_count > HV_MAX_MULTIPAGE_BUFFER_COUNT){
1762 printf("Storvsc: %d segments is too much, "
1763 "only support %d segments\n",
1764 storvsc_sg_count, HV_MAX_MULTIPAGE_BUFFER_COUNT);
1769 * We create our own bounce buffer function currently. Idealy
1770 * we should use BUS_DMA(9) framework. But with current BUS_DMA
1771 * code there is no callback API to check the page alignment of
1772 * middle segments before busdma can decide if a bounce buffer
1773 * is needed for particular segment. There is callback,
1774 * "bus_dma_filter_t *filter", but the parrameters are not
1775 * sufficient for storvsc driver.
1777 * Add page alignment check in BUS_DMA(9) callback. Once
1778 * this is complete, switch the following code to use
1779 * BUS_DMA(9) for storvsc bounce buffer support.
1781 /* check if we need to create bounce buffer */
1782 ret = storvsc_check_bounce_buffer_sgl(storvsc_sglist,
1783 storvsc_sg_count, ¬_aligned_seg_bits);
1786 storvsc_create_bounce_buffer(storvsc_sg_count,
1787 reqp->vstor_packet.u.vm_srb.data_in);
1788 if (NULL == reqp->bounce_sgl) {
1789 printf("Storvsc_error: "
1790 "create bounce buffer failed.\n");
1794 reqp->bounce_sgl_count = storvsc_sg_count;
1795 reqp->not_aligned_seg_bits = not_aligned_seg_bits;
1798 * if it is write, we need copy the original data
1801 if (WRITE_TYPE == reqp->vstor_packet.u.vm_srb.data_in) {
1802 storvsc_copy_sgl_to_bounce_buf(
1806 reqp->not_aligned_seg_bits);
1809 /* transfer virtual address to physical frame number */
1810 if (reqp->not_aligned_seg_bits & 0x1){
1812 vtophys(reqp->bounce_sgl->sg_segs[0].ss_paddr);
1815 vtophys(storvsc_sglist[0].ds_addr);
1817 reqp->data_buf.offset = phys_addr & PAGE_MASK;
1819 pfn = phys_addr >> PAGE_SHIFT;
1820 reqp->data_buf.pfn_array[0] = pfn;
1822 for (i = 1; i < storvsc_sg_count; i++) {
1823 if (reqp->not_aligned_seg_bits & (1 << i)) {
1825 vtophys(reqp->bounce_sgl->sg_segs[i].ss_paddr);
1828 vtophys(storvsc_sglist[i].ds_addr);
1831 pfn = phys_addr >> PAGE_SHIFT;
1832 reqp->data_buf.pfn_array[i] = pfn;
1835 phys_addr = vtophys(storvsc_sglist[0].ds_addr);
1837 reqp->data_buf.offset = phys_addr & PAGE_MASK;
1839 for (i = 0; i < storvsc_sg_count; i++) {
1840 phys_addr = vtophys(storvsc_sglist[i].ds_addr);
1841 pfn = phys_addr >> PAGE_SHIFT;
1842 reqp->data_buf.pfn_array[i] = pfn;
1845 /* check the last segment cross boundary or not */
1846 offset = phys_addr & PAGE_MASK;
1849 vtophys(storvsc_sglist[i-1].ds_addr +
1850 PAGE_SIZE - offset);
1851 pfn = phys_addr >> PAGE_SHIFT;
1852 reqp->data_buf.pfn_array[i] = pfn;
1855 reqp->bounce_sgl_count = 0;
1860 printf("Unknow flags: %d\n", ccb->ccb_h.flags);
1868 * @brief completion function before returning to CAM
1870 * I/O process has been completed and the result needs
1871 * to be passed to the CAM layer.
1872 * Free resources related to this request.
1874 * @param reqp pointer to a request structure
1877 storvsc_io_done(struct hv_storvsc_request *reqp)
1879 union ccb *ccb = reqp->ccb;
1880 struct ccb_scsiio *csio = &ccb->csio;
1881 struct storvsc_softc *sc = reqp->softc;
1882 struct vmscsi_req *vm_srb = &reqp->vstor_packet.u.vm_srb;
1883 bus_dma_segment_t *ori_sglist = NULL;
1884 int ori_sg_count = 0;
1886 /* destroy bounce buffer if it is used */
1887 if (reqp->bounce_sgl_count) {
1888 ori_sglist = (bus_dma_segment_t *)ccb->csio.data_ptr;
1889 ori_sg_count = ccb->csio.sglist_cnt;
1892 * If it is READ operation, we should copy back the data
1893 * to original SG list.
1895 if (READ_TYPE == reqp->vstor_packet.u.vm_srb.data_in) {
1896 storvsc_copy_from_bounce_buf_to_sgl(ori_sglist,
1899 reqp->not_aligned_seg_bits);
1902 storvsc_destroy_bounce_buffer(reqp->bounce_sgl);
1903 reqp->bounce_sgl_count = 0;
1906 if (reqp->retries > 0) {
1907 mtx_lock(&sc->hs_lock);
1908 #if HVS_TIMEOUT_TEST
1909 xpt_print(ccb->ccb_h.path,
1910 "%u: IO returned after timeout, "
1911 "waking up timer handler if any.\n", ticks);
1912 mtx_lock(&reqp->event.mtx);
1913 cv_signal(&reqp->event.cv);
1914 mtx_unlock(&reqp->event.mtx);
1917 xpt_print(ccb->ccb_h.path,
1918 "%u: IO returned after timeout, "
1919 "stopping timer if any.\n", ticks);
1920 mtx_unlock(&sc->hs_lock);
1924 * callout_drain() will wait for the timer handler to finish
1925 * if it is running. So we don't need any lock to synchronize
1926 * between this routine and the timer handler.
1927 * Note that we need to make sure reqp is not freed when timer
1928 * handler is using or will use it.
1930 if (ccb->ccb_h.timeout != CAM_TIME_INFINITY) {
1931 callout_drain(&reqp->callout);
1934 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1935 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1936 if (vm_srb->scsi_status == SCSI_STATUS_OK) {
1937 ccb->ccb_h.status |= CAM_REQ_CMP;
1939 mtx_lock(&sc->hs_lock);
1940 xpt_print(ccb->ccb_h.path,
1941 "srovsc scsi_status = %d\n",
1942 vm_srb->scsi_status);
1943 mtx_unlock(&sc->hs_lock);
1944 ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1947 ccb->csio.scsi_status = (vm_srb->scsi_status & 0xFF);
1948 ccb->csio.resid = ccb->csio.dxfer_len - vm_srb->transfer_len;
1950 if (reqp->sense_info_len != 0) {
1951 csio->sense_resid = csio->sense_len - reqp->sense_info_len;
1952 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
1955 mtx_lock(&sc->hs_lock);
1956 if (reqp->softc->hs_frozen == 1) {
1957 xpt_print(ccb->ccb_h.path,
1958 "%u: storvsc unfreezing softc 0x%p.\n",
1959 ticks, reqp->softc);
1960 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1961 reqp->softc->hs_frozen = 0;
1963 storvsc_free_request(sc, reqp);
1965 mtx_unlock(&sc->hs_lock);
1969 * @brief Free a request structure
1971 * Free a request structure by returning it to the free list
1973 * @param sc pointer to a softc
1974 * @param reqp pointer to a request structure
1977 storvsc_free_request(struct storvsc_softc *sc, struct hv_storvsc_request *reqp)
1980 LIST_INSERT_HEAD(&sc->hs_free_list, reqp, link);
1984 * @brief Determine type of storage device from GUID
1986 * Using the type GUID, determine if this is a StorVSC (paravirtual
1987 * SCSI or BlkVSC (paravirtual IDE) device.
1989 * @param dev a device
1992 static enum hv_storage_type
1993 storvsc_get_storage_type(device_t dev)
1995 const char *p = vmbus_get_type(dev);
1997 if (!memcmp(p, &gBlkVscDeviceType, sizeof(hv_guid))) {
1998 return DRIVER_BLKVSC;
1999 } else if (!memcmp(p, &gStorVscDeviceType, sizeof(hv_guid))) {
2000 return DRIVER_STORVSC;
2002 return (DRIVER_UNKNOWN);