1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2009-2012,2016-2017 Microsoft Corp.
5 * Copyright (c) 2012 NetApp Inc.
6 * Copyright (c) 2012 Citrix Inc.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice unmodified, this list of conditions, and the following
14 * disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 /**
32 * StorVSC driver for Hyper-V. This driver presents a SCSI HBA interface
33 * to the Comman Access Method (CAM) layer. CAM control blocks (CCBs) are
34 * converted into VSCSI protocol messages which are delivered to the parent
35 * partition StorVSP driver over the Hyper-V VMBUS.
36 */
37
38 #include <sys/param.h>
39 #include <sys/proc.h>
40 #include <sys/condvar.h>
41 #include <sys/time.h>
42 #include <sys/systm.h>
43 #include <sys/sysctl.h>
44 #include <sys/sockio.h>
45 #include <sys/mbuf.h>
46 #include <sys/malloc.h>
47 #include <sys/module.h>
48 #include <sys/kernel.h>
49 #include <sys/queue.h>
50 #include <sys/lock.h>
51 #include <sys/sx.h>
52 #include <sys/taskqueue.h>
53 #include <sys/bus.h>
54 #include <sys/mutex.h>
55 #include <sys/callout.h>
56 #include <sys/smp.h>
57 #include <vm/vm.h>
58 #include <vm/pmap.h>
59 #include <vm/uma.h>
60 #include <sys/lock.h>
61 #include <sys/sema.h>
62 #include <sys/eventhandler.h>
63 #include <machine/bus.h>
64
65 #include <cam/cam.h>
66 #include <cam/cam_ccb.h>
67 #include <cam/cam_periph.h>
68 #include <cam/cam_sim.h>
69 #include <cam/cam_xpt_sim.h>
70 #include <cam/cam_xpt_internal.h>
71 #include <cam/cam_debug.h>
72 #include <cam/scsi/scsi_all.h>
73 #include <cam/scsi/scsi_message.h>
74
75 #include <dev/hyperv/include/hyperv.h>
76 #include <dev/hyperv/include/vmbus.h>
77 #include "hv_vstorage.h"
78 #include "vmbus_if.h"
79
80 #define STORVSC_MAX_LUNS_PER_TARGET (64)
81 #define STORVSC_MAX_IO_REQUESTS (STORVSC_MAX_LUNS_PER_TARGET * 2)
82 #define BLKVSC_MAX_IDE_DISKS_PER_TARGET (1)
83 #define BLKVSC_MAX_IO_REQUESTS STORVSC_MAX_IO_REQUESTS
84 #define STORVSC_MAX_TARGETS (2)
85
86 #define VSTOR_PKT_SIZE (sizeof(struct vstor_packet) - vmscsi_size_delta)
87
88 /*
89 * 33 segments are needed to allow 128KB maxio, in case the data
90 * in the first page is _not_ PAGE_SIZE aligned, e.g.
91 *
92 * |<----------- 128KB ----------->|
93 * | |
94 * 0 2K 4K 8K 16K 124K 128K 130K
95 * | | | | | | | |
96 * +--+--+-----+-----+.......+-----+--+--+
97 * | | | | | | | | | DATA
98 * | | | | | | | | |
99 * +--+--+-----+-----+.......------+--+--+
100 * | | | |
101 * | 1| 31 | 1| ...... # of segments
102 */
103 #define STORVSC_DATA_SEGCNT_MAX 33
104 #define STORVSC_DATA_SEGSZ_MAX PAGE_SIZE
105 #define STORVSC_DATA_SIZE_MAX \
106 ((STORVSC_DATA_SEGCNT_MAX - 1) * STORVSC_DATA_SEGSZ_MAX)
107
108 struct storvsc_softc;
109
110 struct hv_sglist {
111 struct iovec sg_iov[STORVSC_DATA_SEGCNT_MAX];
112 u_short sg_nseg;
113 u_short sg_maxseg;
114 };
115
116 struct hv_sgl_node {
117 LIST_ENTRY(hv_sgl_node) link;
118 struct hv_sglist *sgl_data;
119 };
120
121 struct hv_sgl_page_pool{
122 LIST_HEAD(, hv_sgl_node) in_use_sgl_list;
123 LIST_HEAD(, hv_sgl_node) free_sgl_list;
124 boolean_t is_init;
125 } g_hv_sgl_page_pool;
126
127 enum storvsc_request_type {
128 WRITE_TYPE,
129 READ_TYPE,
130 UNKNOWN_TYPE
131 };
132
133 SYSCTL_NODE(_hw, OID_AUTO, storvsc, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
134 "Hyper-V storage interface");
135
136 static u_int hv_storvsc_use_win8ext_flags = 1;
137 SYSCTL_UINT(_hw_storvsc, OID_AUTO, use_win8ext_flags, CTLFLAG_RW,
138 &hv_storvsc_use_win8ext_flags, 0,
139 "Use win8 extension flags or not");
140
141 static u_int hv_storvsc_use_pim_unmapped = 1;
142 SYSCTL_UINT(_hw_storvsc, OID_AUTO, use_pim_unmapped, CTLFLAG_RDTUN,
143 &hv_storvsc_use_pim_unmapped, 0,
144 "Optimize storvsc by using unmapped I/O");
145
146 static u_int hv_storvsc_ringbuffer_size = (64 * PAGE_SIZE);
147 SYSCTL_UINT(_hw_storvsc, OID_AUTO, ringbuffer_size, CTLFLAG_RDTUN,
148 &hv_storvsc_ringbuffer_size, 0, "Hyper-V storage ringbuffer size");
149
150 static u_int hv_storvsc_max_io = 512;
151 SYSCTL_UINT(_hw_storvsc, OID_AUTO, max_io, CTLFLAG_RDTUN,
152 &hv_storvsc_max_io, 0, "Hyper-V storage max io limit");
153
154 static int hv_storvsc_chan_cnt = 0;
155 SYSCTL_INT(_hw_storvsc, OID_AUTO, chan_cnt, CTLFLAG_RDTUN,
156 &hv_storvsc_chan_cnt, 0, "# of channels to use");
157 #ifdef DIAGNOSTIC
158 static int hv_storvsc_srb_status = -1;
159 SYSCTL_INT(_hw_storvsc, OID_AUTO, srb_status, CTLFLAG_RW,
160 &hv_storvsc_srb_status, 0, "srb_status to inject");
161 TUNABLE_INT("hw_storvsc.srb_status", &hv_storvsc_srb_status);
162 #endif /* DIAGNOSTIC */
163
164 #define STORVSC_MAX_IO \
165 vmbus_chan_prplist_nelem(hv_storvsc_ringbuffer_size, \
166 STORVSC_DATA_SEGCNT_MAX, VSTOR_PKT_SIZE)
167
168 struct hv_storvsc_sysctl {
169 u_long data_bio_cnt;
170 u_long data_vaddr_cnt;
171 u_long data_sg_cnt;
172 u_long chan_send_cnt[MAXCPU];
173 };
174
175 struct storvsc_gpa_range {
176 struct vmbus_gpa_range gpa_range;
177 uint64_t gpa_page[STORVSC_DATA_SEGCNT_MAX];
178 } __packed;
179
180 struct hv_storvsc_request {
181 LIST_ENTRY(hv_storvsc_request) link;
182 struct vstor_packet vstor_packet;
183 int prp_cnt;
184 struct storvsc_gpa_range prp_list;
185 void *sense_data;
186 uint8_t sense_info_len;
187 uint8_t retries;
188 union ccb *ccb;
189 struct storvsc_softc *softc;
190 struct callout callout;
191 struct sema synch_sema; /*Synchronize the request/response if needed */
192 struct hv_sglist *bounce_sgl;
193 unsigned int bounce_sgl_count;
194 uint64_t not_aligned_seg_bits;
195 bus_dmamap_t data_dmap;
196 };
197
198 struct storvsc_softc {
199 struct vmbus_channel *hs_chan;
200 LIST_HEAD(, hv_storvsc_request) hs_free_list;
201 struct mtx hs_lock;
202 struct storvsc_driver_props *hs_drv_props;
203 int hs_unit;
204 uint32_t hs_frozen;
205 struct cam_sim *hs_sim;
206 struct cam_path *hs_path;
207 uint32_t hs_num_out_reqs;
208 boolean_t hs_destroy;
209 boolean_t hs_drain_notify;
210 struct sema hs_drain_sema;
211 struct hv_storvsc_request hs_init_req;
212 struct hv_storvsc_request hs_reset_req;
213 device_t hs_dev;
214 bus_dma_tag_t storvsc_req_dtag;
215 struct hv_storvsc_sysctl sysctl_data;
216 uint32_t hs_nchan;
217 struct vmbus_channel *hs_sel_chan[MAXCPU];
218 };
219
220 static eventhandler_tag storvsc_handler_tag;
221 /*
222 * The size of the vmscsi_request has changed in win8. The
223 * additional size is for the newly added elements in the
224 * structure. These elements are valid only when we are talking
225 * to a win8 host.
226 * Track the correct size we need to apply.
227 */
228 static int vmscsi_size_delta = sizeof(struct vmscsi_win8_extension);
229
230 /**
231 * HyperV storvsc timeout testing cases:
232 * a. IO returned after first timeout;
233 * b. IO returned after second timeout and queue freeze;
234 * c. IO returned while timer handler is running
235 * The first can be tested by "sg_senddiag -vv /dev/daX",
236 * and the second and third can be done by
237 * "sg_wr_mode -v -p 08 -c 0,1a -m 0,ff /dev/daX".
238 */
239 #define HVS_TIMEOUT_TEST 0
240
241 /*
242 * Bus/adapter reset functionality on the Hyper-V host is
243 * buggy and it will be disabled until
244 * it can be further tested.
245 */
246 #define HVS_HOST_RESET 0
247
248 struct storvsc_driver_props {
249 char *drv_name;
250 char *drv_desc;
251 uint8_t drv_max_luns_per_target;
252 uint32_t drv_max_ios_per_target;
253 uint32_t drv_ringbuffer_size;
254 };
255
256 enum hv_storage_type {
257 DRIVER_BLKVSC,
258 DRIVER_STORVSC,
259 DRIVER_UNKNOWN
260 };
261
262 #define HS_MAX_ADAPTERS 10
263
264 #define HV_STORAGE_SUPPORTS_MULTI_CHANNEL 0x1
265
266 /* {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f} */
267 static const struct hyperv_guid gStorVscDeviceType={
268 .hv_guid = {0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
269 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f}
270 };
271
272 /* {32412632-86cb-44a2-9b5c-50d1417354f5} */
273 static const struct hyperv_guid gBlkVscDeviceType={
274 .hv_guid = {0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44,
275 0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5}
276 };
277
278 static struct storvsc_driver_props g_drv_props_table[] = {
279 {"blkvsc", "Hyper-V IDE",
280 BLKVSC_MAX_IDE_DISKS_PER_TARGET, BLKVSC_MAX_IO_REQUESTS,
281 20*PAGE_SIZE},
282 {"storvsc", "Hyper-V SCSI",
283 STORVSC_MAX_LUNS_PER_TARGET, STORVSC_MAX_IO_REQUESTS,
284 20*PAGE_SIZE}
285 };
286
287 /*
288 * Sense buffer size changed in win8; have a run-time
289 * variable to track the size we should use.
290 */
291 static int sense_buffer_size = PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE;
292
293 /*
294 * The storage protocol version is determined during the
295 * initial exchange with the host. It will indicate which
296 * storage functionality is available in the host.
297 */
298 static int vmstor_proto_version;
299
300 struct vmstor_proto {
301 int proto_version;
302 int sense_buffer_size;
303 int vmscsi_size_delta;
304 };
305
306 static const struct vmstor_proto vmstor_proto_list[] = {
307 {
308 VMSTOR_PROTOCOL_VERSION_WIN10,
309 POST_WIN7_STORVSC_SENSE_BUFFER_SIZE,
310 0
311 },
312 {
313 VMSTOR_PROTOCOL_VERSION_WIN8_1,
314 POST_WIN7_STORVSC_SENSE_BUFFER_SIZE,
315 0
316 },
317 {
318 VMSTOR_PROTOCOL_VERSION_WIN8,
319 POST_WIN7_STORVSC_SENSE_BUFFER_SIZE,
320 0
321 },
322 {
323 VMSTOR_PROTOCOL_VERSION_WIN7,
324 PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE,
325 sizeof(struct vmscsi_win8_extension),
326 },
327 {
328 VMSTOR_PROTOCOL_VERSION_WIN6,
329 PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE,
330 sizeof(struct vmscsi_win8_extension),
331 }
332 };
333
334 /* static functions */
335 static int storvsc_probe(device_t dev);
336 static int storvsc_attach(device_t dev);
337 static int storvsc_detach(device_t dev);
338 static void storvsc_poll(struct cam_sim * sim);
339 static void storvsc_action(struct cam_sim * sim, union ccb * ccb);
340 static int create_storvsc_request(union ccb *ccb, struct hv_storvsc_request *reqp);
341 static void storvsc_free_request(struct storvsc_softc *sc, struct hv_storvsc_request *reqp);
342 static enum hv_storage_type storvsc_get_storage_type(device_t dev);
343 static void hv_storvsc_rescan_target(struct storvsc_softc *sc);
344 static void hv_storvsc_on_channel_callback(struct vmbus_channel *chan, void *xsc);
345 static void hv_storvsc_on_iocompletion( struct storvsc_softc *sc,
346 struct vstor_packet *vstor_packet,
347 struct hv_storvsc_request *request);
348 static int hv_storvsc_connect_vsp(struct storvsc_softc *);
349 static void storvsc_io_done(struct hv_storvsc_request *reqp);
350 static void storvsc_copy_sgl_to_bounce_buf(struct hv_sglist *bounce_sgl,
351 bus_dma_segment_t *orig_sgl,
352 unsigned int orig_sgl_count,
353 uint64_t seg_bits);
354 void storvsc_copy_from_bounce_buf_to_sgl(bus_dma_segment_t *dest_sgl,
355 unsigned int dest_sgl_count,
356 struct hv_sglist *src_sgl,
357 uint64_t seg_bits);
358
359 static device_method_t storvsc_methods[] = {
360 /* Device interface */
361 DEVMETHOD(device_probe, storvsc_probe),
362 DEVMETHOD(device_attach, storvsc_attach),
363 DEVMETHOD(device_detach, storvsc_detach),
364 DEVMETHOD(device_shutdown, bus_generic_shutdown),
365 DEVMETHOD_END
366 };
367
368 static driver_t storvsc_driver = {
369 "storvsc", storvsc_methods, sizeof(struct storvsc_softc),
370 };
371
372 DRIVER_MODULE(storvsc, vmbus, storvsc_driver, 0, 0);
373 MODULE_VERSION(storvsc, 1);
374 MODULE_DEPEND(storvsc, vmbus, 1, 1, 1);
375
376 static void
storvsc_subchan_attach(struct storvsc_softc * sc,struct vmbus_channel * new_channel)377 storvsc_subchan_attach(struct storvsc_softc *sc,
378 struct vmbus_channel *new_channel)
379 {
380 struct vmstor_chan_props props;
381
382 memset(&props, 0, sizeof(props));
383
384 vmbus_chan_cpu_rr(new_channel);
385 vmbus_chan_open(new_channel,
386 sc->hs_drv_props->drv_ringbuffer_size,
387 sc->hs_drv_props->drv_ringbuffer_size,
388 (void *)&props,
389 sizeof(struct vmstor_chan_props),
390 hv_storvsc_on_channel_callback, sc);
391 }
392
393 /**
394 * @brief Send multi-channel creation request to host
395 *
396 * @param device a Hyper-V device pointer
397 * @param max_chans the max channels supported by vmbus
398 */
399 static void
storvsc_send_multichannel_request(struct storvsc_softc * sc,int max_subch)400 storvsc_send_multichannel_request(struct storvsc_softc *sc, int max_subch)
401 {
402 struct vmbus_channel **subchan;
403 struct hv_storvsc_request *request;
404 struct vstor_packet *vstor_packet;
405 int request_subch;
406 int i;
407
408 /* get sub-channel count that need to create */
409 request_subch = MIN(max_subch, mp_ncpus - 1);
410
411 request = &sc->hs_init_req;
412
413 /* request the host to create multi-channel */
414 memset(request, 0, sizeof(struct hv_storvsc_request));
415
416 sema_init(&request->synch_sema, 0, ("stor_synch_sema"));
417
418 vstor_packet = &request->vstor_packet;
419
420 vstor_packet->operation = VSTOR_OPERATION_CREATE_MULTI_CHANNELS;
421 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
422 vstor_packet->u.multi_channels_cnt = request_subch;
423
424 vmbus_chan_send(sc->hs_chan,
425 VMBUS_CHANPKT_TYPE_INBAND, VMBUS_CHANPKT_FLAG_RC,
426 vstor_packet, VSTOR_PKT_SIZE, (uint64_t)(uintptr_t)request);
427
428 sema_wait(&request->synch_sema);
429
430 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETEIO ||
431 vstor_packet->status != 0) {
432 printf("Storvsc_error: create multi-channel invalid operation "
433 "(%d) or statue (%u)\n",
434 vstor_packet->operation, vstor_packet->status);
435 return;
436 }
437
438 /* Update channel count */
439 sc->hs_nchan = request_subch + 1;
440
441 /* Wait for sub-channels setup to complete. */
442 subchan = vmbus_subchan_get(sc->hs_chan, request_subch);
443
444 /* Attach the sub-channels. */
445 for (i = 0; i < request_subch; ++i)
446 storvsc_subchan_attach(sc, subchan[i]);
447
448 /* Release the sub-channels. */
449 vmbus_subchan_rel(subchan, request_subch);
450
451 if (bootverbose)
452 printf("Storvsc create multi-channel success!\n");
453 }
454
455 /**
456 * @brief initialize channel connection to parent partition
457 *
458 * @param dev a Hyper-V device pointer
459 * @returns 0 on success, non-zero error on failure
460 */
461 static int
hv_storvsc_channel_init(struct storvsc_softc * sc)462 hv_storvsc_channel_init(struct storvsc_softc *sc)
463 {
464 int ret = 0, i;
465 struct hv_storvsc_request *request;
466 struct vstor_packet *vstor_packet;
467 uint16_t max_subch;
468 boolean_t support_multichannel;
469 uint32_t version;
470
471 max_subch = 0;
472 support_multichannel = FALSE;
473
474 request = &sc->hs_init_req;
475 memset(request, 0, sizeof(struct hv_storvsc_request));
476 vstor_packet = &request->vstor_packet;
477 request->softc = sc;
478
479 /**
480 * Initiate the vsc/vsp initialization protocol on the open channel
481 */
482 sema_init(&request->synch_sema, 0, ("stor_synch_sema"));
483
484 vstor_packet->operation = VSTOR_OPERATION_BEGININITIALIZATION;
485 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
486
487
488 ret = vmbus_chan_send(sc->hs_chan,
489 VMBUS_CHANPKT_TYPE_INBAND, VMBUS_CHANPKT_FLAG_RC,
490 vstor_packet, VSTOR_PKT_SIZE, (uint64_t)(uintptr_t)request);
491
492 if (ret != 0)
493 goto cleanup;
494
495 sema_wait(&request->synch_sema);
496
497 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETEIO ||
498 vstor_packet->status != 0) {
499 goto cleanup;
500 }
501
502 for (i = 0; i < nitems(vmstor_proto_list); i++) {
503 /* reuse the packet for version range supported */
504
505 memset(vstor_packet, 0, sizeof(struct vstor_packet));
506 vstor_packet->operation = VSTOR_OPERATION_QUERYPROTOCOLVERSION;
507 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
508
509 vstor_packet->u.version.major_minor =
510 vmstor_proto_list[i].proto_version;
511
512 /* revision is only significant for Windows guests */
513 vstor_packet->u.version.revision = 0;
514
515 ret = vmbus_chan_send(sc->hs_chan,
516 VMBUS_CHANPKT_TYPE_INBAND, VMBUS_CHANPKT_FLAG_RC,
517 vstor_packet, VSTOR_PKT_SIZE, (uint64_t)(uintptr_t)request);
518
519 if (ret != 0)
520 goto cleanup;
521
522 sema_wait(&request->synch_sema);
523
524 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETEIO) {
525 ret = EINVAL;
526 goto cleanup;
527 }
528 if (vstor_packet->status == 0) {
529 vmstor_proto_version =
530 vmstor_proto_list[i].proto_version;
531 sense_buffer_size =
532 vmstor_proto_list[i].sense_buffer_size;
533 vmscsi_size_delta =
534 vmstor_proto_list[i].vmscsi_size_delta;
535 break;
536 }
537 }
538
539 if (vstor_packet->status != 0) {
540 ret = EINVAL;
541 goto cleanup;
542 }
543 /**
544 * Query channel properties
545 */
546 memset(vstor_packet, 0, sizeof(struct vstor_packet));
547 vstor_packet->operation = VSTOR_OPERATION_QUERYPROPERTIES;
548 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
549
550 ret = vmbus_chan_send(sc->hs_chan,
551 VMBUS_CHANPKT_TYPE_INBAND, VMBUS_CHANPKT_FLAG_RC,
552 vstor_packet, VSTOR_PKT_SIZE, (uint64_t)(uintptr_t)request);
553
554 if ( ret != 0)
555 goto cleanup;
556
557 sema_wait(&request->synch_sema);
558
559 /* TODO: Check returned version */
560 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETEIO ||
561 vstor_packet->status != 0) {
562 goto cleanup;
563 }
564
565 max_subch = vstor_packet->u.chan_props.max_channel_cnt;
566 if (hv_storvsc_chan_cnt > 0 && hv_storvsc_chan_cnt < (max_subch + 1))
567 max_subch = hv_storvsc_chan_cnt - 1;
568
569 /* multi-channels feature is supported by WIN8 and above version */
570 version = VMBUS_GET_VERSION(device_get_parent(sc->hs_dev), sc->hs_dev);
571 if (version != VMBUS_VERSION_WIN7 && version != VMBUS_VERSION_WS2008 &&
572 (vstor_packet->u.chan_props.flags &
573 HV_STORAGE_SUPPORTS_MULTI_CHANNEL)) {
574 support_multichannel = TRUE;
575 }
576 if (bootverbose) {
577 device_printf(sc->hs_dev, "max chans %d%s\n", max_subch + 1,
578 support_multichannel ? ", multi-chan capable" : "");
579 }
580
581 memset(vstor_packet, 0, sizeof(struct vstor_packet));
582 vstor_packet->operation = VSTOR_OPERATION_ENDINITIALIZATION;
583 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
584
585 ret = vmbus_chan_send(sc->hs_chan,
586 VMBUS_CHANPKT_TYPE_INBAND, VMBUS_CHANPKT_FLAG_RC,
587 vstor_packet, VSTOR_PKT_SIZE, (uint64_t)(uintptr_t)request);
588
589 if (ret != 0) {
590 goto cleanup;
591 }
592
593 sema_wait(&request->synch_sema);
594
595 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETEIO ||
596 vstor_packet->status != 0)
597 goto cleanup;
598
599 /*
600 * If multi-channel is supported, send multichannel create
601 * request to host.
602 */
603 if (support_multichannel && max_subch > 0)
604 storvsc_send_multichannel_request(sc, max_subch);
605 cleanup:
606 sema_destroy(&request->synch_sema);
607 return (ret);
608 }
609
610 /**
611 * @brief Open channel connection to paraent partition StorVSP driver
612 *
613 * Open and initialize channel connection to parent partition StorVSP driver.
614 *
615 * @param pointer to a Hyper-V device
616 * @returns 0 on success, non-zero error on failure
617 */
618 static int
hv_storvsc_connect_vsp(struct storvsc_softc * sc)619 hv_storvsc_connect_vsp(struct storvsc_softc *sc)
620 {
621 int ret = 0;
622 struct vmstor_chan_props props;
623
624 memset(&props, 0, sizeof(struct vmstor_chan_props));
625
626 /*
627 * Open the channel
628 */
629 vmbus_chan_cpu_rr(sc->hs_chan);
630 ret = vmbus_chan_open(
631 sc->hs_chan,
632 sc->hs_drv_props->drv_ringbuffer_size,
633 sc->hs_drv_props->drv_ringbuffer_size,
634 (void *)&props,
635 sizeof(struct vmstor_chan_props),
636 hv_storvsc_on_channel_callback, sc);
637
638 if (ret != 0) {
639 return ret;
640 }
641
642 ret = hv_storvsc_channel_init(sc);
643 return (ret);
644 }
645
646 #if HVS_HOST_RESET
647 static int
hv_storvsc_host_reset(struct storvsc_softc * sc)648 hv_storvsc_host_reset(struct storvsc_softc *sc)
649 {
650 int ret = 0;
651
652 struct hv_storvsc_request *request;
653 struct vstor_packet *vstor_packet;
654
655 request = &sc->hs_reset_req;
656 request->softc = sc;
657 vstor_packet = &request->vstor_packet;
658
659 sema_init(&request->synch_sema, 0, "stor synch sema");
660
661 vstor_packet->operation = VSTOR_OPERATION_RESETBUS;
662 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
663
664 ret = vmbus_chan_send(dev->channel,
665 VMBUS_CHANPKT_TYPE_INBAND, VMBUS_CHANPKT_FLAG_RC,
666 vstor_packet, VSTOR_PKT_SIZE,
667 (uint64_t)(uintptr_t)&sc->hs_reset_req);
668
669 if (ret != 0) {
670 goto cleanup;
671 }
672
673 sema_wait(&request->synch_sema);
674
675 /*
676 * At this point, all outstanding requests in the adapter
677 * should have been flushed out and return to us
678 */
679
680 cleanup:
681 sema_destroy(&request->synch_sema);
682 return (ret);
683 }
684 #endif /* HVS_HOST_RESET */
685
686 /**
687 * @brief Function to initiate an I/O request
688 *
689 * @param device Hyper-V device pointer
690 * @param request pointer to a request structure
691 * @returns 0 on success, non-zero error on failure
692 */
693 static int
hv_storvsc_io_request(struct storvsc_softc * sc,struct hv_storvsc_request * request)694 hv_storvsc_io_request(struct storvsc_softc *sc,
695 struct hv_storvsc_request *request)
696 {
697 struct vstor_packet *vstor_packet = &request->vstor_packet;
698 struct vmbus_channel* outgoing_channel = NULL;
699 int ret = 0, ch_sel;
700
701 vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
702
703 vstor_packet->u.vm_srb.length =
704 sizeof(struct vmscsi_req) - vmscsi_size_delta;
705
706 vstor_packet->u.vm_srb.sense_info_len = sense_buffer_size;
707
708 vstor_packet->u.vm_srb.transfer_len =
709 request->prp_list.gpa_range.gpa_len;
710
711 vstor_packet->operation = VSTOR_OPERATION_EXECUTESRB;
712
713 ch_sel = (vstor_packet->u.vm_srb.lun + curcpu) % sc->hs_nchan;
714 /*
715 * If we are panic'ing, then we are dumping core. Since storvsc_polls
716 * always uses sc->hs_chan, then we must send to that channel or a poll
717 * timeout will occur.
718 */
719 if (KERNEL_PANICKED()) {
720 outgoing_channel = sc->hs_chan;
721 } else {
722 outgoing_channel = sc->hs_sel_chan[ch_sel];
723 }
724
725 mtx_unlock(&request->softc->hs_lock);
726 if (request->prp_list.gpa_range.gpa_len) {
727 ret = vmbus_chan_send_prplist(outgoing_channel,
728 &request->prp_list.gpa_range, request->prp_cnt,
729 vstor_packet, VSTOR_PKT_SIZE, (uint64_t)(uintptr_t)request);
730 } else {
731 ret = vmbus_chan_send(outgoing_channel,
732 VMBUS_CHANPKT_TYPE_INBAND, VMBUS_CHANPKT_FLAG_RC,
733 vstor_packet, VSTOR_PKT_SIZE, (uint64_t)(uintptr_t)request);
734 }
735 /* statistic for successful request sending on each channel */
736 if (!ret) {
737 sc->sysctl_data.chan_send_cnt[ch_sel]++;
738 }
739 mtx_lock(&request->softc->hs_lock);
740
741 if (ret != 0) {
742 printf("Unable to send packet %p ret %d", vstor_packet, ret);
743 } else {
744 atomic_add_int(&sc->hs_num_out_reqs, 1);
745 }
746
747 return (ret);
748 }
749
750
751 /**
752 * Process IO_COMPLETION_OPERATION and ready
753 * the result to be completed for upper layer
754 * processing by the CAM layer.
755 */
756 static void
hv_storvsc_on_iocompletion(struct storvsc_softc * sc,struct vstor_packet * vstor_packet,struct hv_storvsc_request * request)757 hv_storvsc_on_iocompletion(struct storvsc_softc *sc,
758 struct vstor_packet *vstor_packet,
759 struct hv_storvsc_request *request)
760 {
761 struct vmscsi_req *vm_srb;
762
763 vm_srb = &vstor_packet->u.vm_srb;
764
765 /*
766 * Copy some fields of the host's response into the request structure,
767 * because the fields will be used later in storvsc_io_done().
768 */
769 request->vstor_packet.u.vm_srb.scsi_status = vm_srb->scsi_status;
770 request->vstor_packet.u.vm_srb.srb_status = vm_srb->srb_status;
771 request->vstor_packet.u.vm_srb.transfer_len = vm_srb->transfer_len;
772
773 if (((vm_srb->scsi_status & 0xFF) == SCSI_STATUS_CHECK_COND) &&
774 (vm_srb->srb_status & SRB_STATUS_AUTOSENSE_VALID)) {
775 /* Autosense data available */
776
777 KASSERT(vm_srb->sense_info_len <= request->sense_info_len,
778 ("vm_srb->sense_info_len <= "
779 "request->sense_info_len"));
780
781 memcpy(request->sense_data, vm_srb->u.sense_data,
782 vm_srb->sense_info_len);
783
784 request->sense_info_len = vm_srb->sense_info_len;
785 }
786
787 /* Complete request by passing to the CAM layer */
788 storvsc_io_done(request);
789 atomic_subtract_int(&sc->hs_num_out_reqs, 1);
790 if (sc->hs_drain_notify && (sc->hs_num_out_reqs == 0)) {
791 sema_post(&sc->hs_drain_sema);
792 }
793 }
794
795 static void
hv_storvsc_rescan_target(struct storvsc_softc * sc)796 hv_storvsc_rescan_target(struct storvsc_softc *sc)
797 {
798 path_id_t pathid;
799 target_id_t targetid;
800 union ccb *ccb;
801
802 pathid = cam_sim_path(sc->hs_sim);
803 targetid = CAM_TARGET_WILDCARD;
804
805 /*
806 * Allocate a CCB and schedule a rescan.
807 */
808 ccb = xpt_alloc_ccb_nowait();
809 if (ccb == NULL) {
810 printf("unable to alloc CCB for rescan\n");
811 return;
812 }
813
814 if (xpt_create_path(&ccb->ccb_h.path, NULL, pathid, targetid,
815 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
816 printf("unable to create path for rescan, pathid: %u,"
817 "targetid: %u\n", pathid, targetid);
818 xpt_free_ccb(ccb);
819 return;
820 }
821
822 if (targetid == CAM_TARGET_WILDCARD)
823 ccb->ccb_h.func_code = XPT_SCAN_BUS;
824 else
825 ccb->ccb_h.func_code = XPT_SCAN_TGT;
826
827 xpt_rescan(ccb);
828 }
829
830 static void
hv_storvsc_on_channel_callback(struct vmbus_channel * channel,void * xsc)831 hv_storvsc_on_channel_callback(struct vmbus_channel *channel, void *xsc)
832 {
833 int ret = 0;
834 struct storvsc_softc *sc = xsc;
835 uint32_t bytes_recvd;
836 uint64_t request_id;
837 uint8_t packet[roundup2(sizeof(struct vstor_packet), 8)];
838 struct hv_storvsc_request *request;
839 struct vstor_packet *vstor_packet;
840
841 bytes_recvd = roundup2(VSTOR_PKT_SIZE, 8);
842 ret = vmbus_chan_recv(channel, packet, &bytes_recvd, &request_id);
843 KASSERT(ret != ENOBUFS, ("storvsc recvbuf is not large enough"));
844 /* XXX check bytes_recvd to make sure that it contains enough data */
845
846 while ((ret == 0) && (bytes_recvd > 0)) {
847 request = (struct hv_storvsc_request *)(uintptr_t)request_id;
848
849 if ((request == &sc->hs_init_req) ||
850 (request == &sc->hs_reset_req)) {
851 memcpy(&request->vstor_packet, packet,
852 sizeof(struct vstor_packet));
853 sema_post(&request->synch_sema);
854 } else {
855 vstor_packet = (struct vstor_packet *)packet;
856 switch(vstor_packet->operation) {
857 case VSTOR_OPERATION_COMPLETEIO:
858 if (request == NULL)
859 panic("VMBUS: storvsc received a "
860 "packet with NULL request id in "
861 "COMPLETEIO operation.");
862
863 hv_storvsc_on_iocompletion(sc,
864 vstor_packet, request);
865 break;
866 case VSTOR_OPERATION_REMOVEDEVICE:
867 printf("VMBUS: storvsc operation %d not "
868 "implemented.\n", vstor_packet->operation);
869 /* TODO: implement */
870 break;
871 case VSTOR_OPERATION_ENUMERATE_BUS:
872 hv_storvsc_rescan_target(sc);
873 break;
874 default:
875 break;
876 }
877 }
878
879 bytes_recvd = roundup2(VSTOR_PKT_SIZE, 8),
880 ret = vmbus_chan_recv(channel, packet, &bytes_recvd,
881 &request_id);
882 KASSERT(ret != ENOBUFS,
883 ("storvsc recvbuf is not large enough"));
884 /*
885 * XXX check bytes_recvd to make sure that it contains
886 * enough data
887 */
888 }
889 }
890
891 /**
892 * @brief StorVSC probe function
893 *
894 * Device probe function. Returns 0 if the input device is a StorVSC
895 * device. Otherwise, a ENXIO is returned. If the input device is
896 * for BlkVSC (paravirtual IDE) device and this support is disabled in
897 * favor of the emulated ATA/IDE device, return ENXIO.
898 *
899 * @param a device
900 * @returns 0 on success, ENXIO if not a matcing StorVSC device
901 */
902 static int
storvsc_probe(device_t dev)903 storvsc_probe(device_t dev)
904 {
905 int ret = ENXIO;
906
907 switch (storvsc_get_storage_type(dev)) {
908 case DRIVER_BLKVSC:
909 if(bootverbose)
910 device_printf(dev,
911 "Enlightened ATA/IDE detected\n");
912 device_set_desc(dev, g_drv_props_table[DRIVER_BLKVSC].drv_desc);
913 ret = BUS_PROBE_DEFAULT;
914 break;
915 case DRIVER_STORVSC:
916 if(bootverbose)
917 device_printf(dev, "Enlightened SCSI device detected\n");
918 device_set_desc(dev, g_drv_props_table[DRIVER_STORVSC].drv_desc);
919 ret = BUS_PROBE_DEFAULT;
920 break;
921 default:
922 ret = ENXIO;
923 }
924 return (ret);
925 }
926
927 static void
storvsc_create_chan_sel(struct storvsc_softc * sc)928 storvsc_create_chan_sel(struct storvsc_softc *sc)
929 {
930 struct vmbus_channel **subch;
931 int i, nsubch;
932
933 sc->hs_sel_chan[0] = sc->hs_chan;
934 nsubch = sc->hs_nchan - 1;
935 if (nsubch == 0)
936 return;
937
938 subch = vmbus_subchan_get(sc->hs_chan, nsubch);
939 for (i = 0; i < nsubch; i++)
940 sc->hs_sel_chan[i + 1] = subch[i];
941 vmbus_subchan_rel(subch, nsubch);
942 }
943
944 static int
storvsc_init_requests(device_t dev)945 storvsc_init_requests(device_t dev)
946 {
947 struct storvsc_softc *sc = device_get_softc(dev);
948 struct hv_storvsc_request *reqp;
949 int error, i;
950
951 LIST_INIT(&sc->hs_free_list);
952
953 error = bus_dma_tag_create(
954 bus_get_dma_tag(dev), /* parent */
955 1, /* alignment */
956 PAGE_SIZE, /* boundary */
957 #if defined(__i386__) && defined(PAE)
958 BUS_SPACE_MAXADDR_48BIT, /* lowaddr */
959 BUS_SPACE_MAXADDR_48BIT, /* highaddr */
960 #else
961 BUS_SPACE_MAXADDR, /* lowaddr */
962 BUS_SPACE_MAXADDR, /* highaddr */
963 #endif
964 NULL, NULL, /* filter, filterarg */
965 STORVSC_DATA_SIZE_MAX, /* maxsize */
966 STORVSC_DATA_SEGCNT_MAX, /* nsegments */
967 STORVSC_DATA_SEGSZ_MAX, /* maxsegsize */
968 BUS_DMA_KEEP_PG_OFFSET, /* flags */
969 NULL, /* lockfunc */
970 NULL, /* lockfuncarg */
971 &sc->storvsc_req_dtag);
972 if (error) {
973 device_printf(dev, "failed to create storvsc dma tag\n");
974 return (error);
975 }
976
977 for (i = 0; i < sc->hs_drv_props->drv_max_ios_per_target; ++i) {
978 reqp = malloc(sizeof(struct hv_storvsc_request),
979 M_DEVBUF, M_WAITOK|M_ZERO);
980 reqp->softc = sc;
981 error = bus_dmamap_create(sc->storvsc_req_dtag, 0,
982 &reqp->data_dmap);
983 if (error) {
984 device_printf(dev, "failed to allocate storvsc "
985 "data dmamap\n");
986 goto cleanup;
987 }
988 LIST_INSERT_HEAD(&sc->hs_free_list, reqp, link);
989 }
990 return (0);
991
992 cleanup:
993 while ((reqp = LIST_FIRST(&sc->hs_free_list)) != NULL) {
994 LIST_REMOVE(reqp, link);
995 bus_dmamap_destroy(sc->storvsc_req_dtag, reqp->data_dmap);
996 free(reqp, M_DEVBUF);
997 }
998 return (error);
999 }
1000
1001 static void
storvsc_sysctl(device_t dev)1002 storvsc_sysctl(device_t dev)
1003 {
1004 struct sysctl_oid_list *child;
1005 struct sysctl_ctx_list *ctx;
1006 struct sysctl_oid *ch_tree, *chid_tree;
1007 struct storvsc_softc *sc;
1008 char name[16];
1009 int i;
1010
1011 sc = device_get_softc(dev);
1012 ctx = device_get_sysctl_ctx(dev);
1013 child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
1014
1015 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "data_bio_cnt",
1016 CTLFLAG_RW | CTLFLAG_STATS, &sc->sysctl_data.data_bio_cnt,
1017 "# of bio data block");
1018 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "data_vaddr_cnt",
1019 CTLFLAG_RW | CTLFLAG_STATS, &sc->sysctl_data.data_vaddr_cnt,
1020 "# of vaddr data block");
1021 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "data_sg_cnt",
1022 CTLFLAG_RW | CTLFLAG_STATS, &sc->sysctl_data.data_sg_cnt,
1023 "# of sg data block");
1024
1025 /* dev.storvsc.UNIT.channel */
1026 ch_tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "channel",
1027 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
1028 if (ch_tree == NULL)
1029 return;
1030
1031 for (i = 0; i < sc->hs_nchan; i++) {
1032 uint32_t ch_id;
1033
1034 ch_id = vmbus_chan_id(sc->hs_sel_chan[i]);
1035 snprintf(name, sizeof(name), "%d", ch_id);
1036 /* dev.storvsc.UNIT.channel.CHID */
1037 chid_tree = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(ch_tree),
1038 OID_AUTO, name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
1039 if (chid_tree == NULL)
1040 return;
1041 /* dev.storvsc.UNIT.channel.CHID.send_req */
1042 SYSCTL_ADD_ULONG(ctx, SYSCTL_CHILDREN(chid_tree), OID_AUTO,
1043 "send_req", CTLFLAG_RD, &sc->sysctl_data.chan_send_cnt[i],
1044 "# of request sending from this channel");
1045 }
1046 }
1047
1048 /**
1049 * @brief StorVSC attach function
1050 *
1051 * Function responsible for allocating per-device structures,
1052 * setting up CAM interfaces and scanning for available LUNs to
1053 * be used for SCSI device peripherals.
1054 *
1055 * @param a device
1056 * @returns 0 on success or an error on failure
1057 */
1058 static int
storvsc_attach(device_t dev)1059 storvsc_attach(device_t dev)
1060 {
1061 enum hv_storage_type stor_type;
1062 struct storvsc_softc *sc;
1063 struct cam_devq *devq;
1064 int ret, i, j;
1065 struct hv_storvsc_request *reqp;
1066 struct root_hold_token *root_mount_token = NULL;
1067 struct hv_sgl_node *sgl_node = NULL;
1068 void *tmp_buff = NULL;
1069
1070 /*
1071 * We need to serialize storvsc attach calls.
1072 */
1073 root_mount_token = root_mount_hold("storvsc");
1074
1075 sc = device_get_softc(dev);
1076 sc->hs_nchan = 1;
1077 sc->hs_chan = vmbus_get_channel(dev);
1078
1079 stor_type = storvsc_get_storage_type(dev);
1080
1081 if (stor_type == DRIVER_UNKNOWN) {
1082 ret = ENODEV;
1083 goto cleanup;
1084 }
1085
1086 /* fill in driver specific properties */
1087 sc->hs_drv_props = &g_drv_props_table[stor_type];
1088 sc->hs_drv_props->drv_ringbuffer_size = hv_storvsc_ringbuffer_size;
1089 sc->hs_drv_props->drv_max_ios_per_target =
1090 MIN(STORVSC_MAX_IO, hv_storvsc_max_io);
1091 if (bootverbose) {
1092 printf("storvsc ringbuffer size: %d, max_io: %d\n",
1093 sc->hs_drv_props->drv_ringbuffer_size,
1094 sc->hs_drv_props->drv_max_ios_per_target);
1095 }
1096 /* fill in device specific properties */
1097 sc->hs_unit = device_get_unit(dev);
1098 sc->hs_dev = dev;
1099
1100 mtx_init(&sc->hs_lock, "hvslck", NULL, MTX_DEF);
1101
1102 ret = storvsc_init_requests(dev);
1103 if (ret != 0)
1104 goto cleanup;
1105
1106 /* create sg-list page pool */
1107 if (FALSE == g_hv_sgl_page_pool.is_init) {
1108 g_hv_sgl_page_pool.is_init = TRUE;
1109 LIST_INIT(&g_hv_sgl_page_pool.in_use_sgl_list);
1110 LIST_INIT(&g_hv_sgl_page_pool.free_sgl_list);
1111
1112 /*
1113 * Pre-create SG list, each SG list with
1114 * STORVSC_DATA_SEGCNT_MAX segments, each
1115 * segment has one page buffer
1116 */
1117 for (i = 0; i < sc->hs_drv_props->drv_max_ios_per_target; i++) {
1118 sgl_node = malloc(sizeof(struct hv_sgl_node),
1119 M_DEVBUF, M_WAITOK|M_ZERO);
1120
1121 sgl_node->sgl_data = malloc(sizeof(struct hv_sglist),
1122 M_DEVBUF, M_WAITOK|M_ZERO);
1123
1124 for (j = 0; j < STORVSC_DATA_SEGCNT_MAX; j++) {
1125 tmp_buff = malloc(PAGE_SIZE,
1126 M_DEVBUF, M_WAITOK|M_ZERO);
1127
1128 sgl_node->sgl_data->sg_iov[j].iov_base =
1129 tmp_buff;
1130 }
1131
1132 LIST_INSERT_HEAD(&g_hv_sgl_page_pool.free_sgl_list,
1133 sgl_node, link);
1134 }
1135 }
1136
1137 sc->hs_destroy = FALSE;
1138 sc->hs_drain_notify = FALSE;
1139 sema_init(&sc->hs_drain_sema, 0, "Store Drain Sema");
1140
1141 ret = hv_storvsc_connect_vsp(sc);
1142 if (ret != 0) {
1143 goto cleanup;
1144 }
1145
1146 /* Construct cpu to channel mapping */
1147 storvsc_create_chan_sel(sc);
1148
1149 /*
1150 * Create the device queue.
1151 * Hyper-V maps each target to one SCSI HBA
1152 */
1153 devq = cam_simq_alloc(sc->hs_drv_props->drv_max_ios_per_target);
1154 if (devq == NULL) {
1155 device_printf(dev, "Failed to alloc device queue\n");
1156 ret = ENOMEM;
1157 goto cleanup;
1158 }
1159
1160 sc->hs_sim = cam_sim_alloc(storvsc_action,
1161 storvsc_poll,
1162 sc->hs_drv_props->drv_name,
1163 sc,
1164 sc->hs_unit,
1165 &sc->hs_lock, 1,
1166 sc->hs_drv_props->drv_max_ios_per_target,
1167 devq);
1168
1169 if (sc->hs_sim == NULL) {
1170 device_printf(dev, "Failed to alloc sim\n");
1171 cam_simq_free(devq);
1172 ret = ENOMEM;
1173 goto cleanup;
1174 }
1175
1176 mtx_lock(&sc->hs_lock);
1177 /* bus_id is set to 0, need to get it from VMBUS channel query? */
1178 if (xpt_bus_register(sc->hs_sim, dev, 0) != CAM_SUCCESS) {
1179 cam_sim_free(sc->hs_sim, /*free_devq*/TRUE);
1180 mtx_unlock(&sc->hs_lock);
1181 device_printf(dev, "Unable to register SCSI bus\n");
1182 ret = ENXIO;
1183 goto cleanup;
1184 }
1185
1186 if (xpt_create_path(&sc->hs_path, /*periph*/NULL,
1187 cam_sim_path(sc->hs_sim),
1188 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1189 xpt_bus_deregister(cam_sim_path(sc->hs_sim));
1190 cam_sim_free(sc->hs_sim, /*free_devq*/TRUE);
1191 mtx_unlock(&sc->hs_lock);
1192 device_printf(dev, "Unable to create path\n");
1193 ret = ENXIO;
1194 goto cleanup;
1195 }
1196
1197 mtx_unlock(&sc->hs_lock);
1198
1199 storvsc_sysctl(dev);
1200
1201 root_mount_rel(root_mount_token);
1202 return (0);
1203
1204
1205 cleanup:
1206 root_mount_rel(root_mount_token);
1207 while (!LIST_EMPTY(&sc->hs_free_list)) {
1208 reqp = LIST_FIRST(&sc->hs_free_list);
1209 LIST_REMOVE(reqp, link);
1210 bus_dmamap_destroy(sc->storvsc_req_dtag, reqp->data_dmap);
1211 free(reqp, M_DEVBUF);
1212 }
1213
1214 while (!LIST_EMPTY(&g_hv_sgl_page_pool.free_sgl_list)) {
1215 sgl_node = LIST_FIRST(&g_hv_sgl_page_pool.free_sgl_list);
1216 LIST_REMOVE(sgl_node, link);
1217 for (j = 0; j < STORVSC_DATA_SEGCNT_MAX; j++) {
1218 free(sgl_node->sgl_data->sg_iov[j].iov_base, M_DEVBUF);
1219 }
1220 free(sgl_node->sgl_data, M_DEVBUF);
1221 free(sgl_node, M_DEVBUF);
1222 }
1223
1224 return (ret);
1225 }
1226
1227 /**
1228 * @brief StorVSC device detach function
1229 *
1230 * This function is responsible for safely detaching a
1231 * StorVSC device. This includes waiting for inbound responses
1232 * to complete and freeing associated per-device structures.
1233 *
1234 * @param dev a device
1235 * returns 0 on success
1236 */
1237 static int
storvsc_detach(device_t dev)1238 storvsc_detach(device_t dev)
1239 {
1240 struct storvsc_softc *sc = device_get_softc(dev);
1241 struct hv_storvsc_request *reqp = NULL;
1242 struct hv_sgl_node *sgl_node = NULL;
1243 int j = 0;
1244
1245 sc->hs_destroy = TRUE;
1246
1247 /*
1248 * At this point, all outbound traffic should be disabled. We
1249 * only allow inbound traffic (responses) to proceed so that
1250 * outstanding requests can be completed.
1251 */
1252
1253 sc->hs_drain_notify = TRUE;
1254 sema_wait(&sc->hs_drain_sema);
1255 sc->hs_drain_notify = FALSE;
1256
1257 /*
1258 * Since we have already drained, we don't need to busy wait.
1259 * The call to close the channel will reset the callback
1260 * under the protection of the incoming channel lock.
1261 */
1262
1263 vmbus_chan_close(sc->hs_chan);
1264
1265 mtx_lock(&sc->hs_lock);
1266 while (!LIST_EMPTY(&sc->hs_free_list)) {
1267 reqp = LIST_FIRST(&sc->hs_free_list);
1268 LIST_REMOVE(reqp, link);
1269 bus_dmamap_destroy(sc->storvsc_req_dtag, reqp->data_dmap);
1270 free(reqp, M_DEVBUF);
1271 }
1272 mtx_unlock(&sc->hs_lock);
1273
1274 while (!LIST_EMPTY(&g_hv_sgl_page_pool.free_sgl_list)) {
1275 sgl_node = LIST_FIRST(&g_hv_sgl_page_pool.free_sgl_list);
1276 LIST_REMOVE(sgl_node, link);
1277 for (j = 0; j < STORVSC_DATA_SEGCNT_MAX; j++){
1278 free(sgl_node->sgl_data->sg_iov[j].iov_base, M_DEVBUF);
1279 }
1280 free(sgl_node->sgl_data, M_DEVBUF);
1281 free(sgl_node, M_DEVBUF);
1282 }
1283
1284 return (0);
1285 }
1286
1287 #if HVS_TIMEOUT_TEST
1288 /**
1289 * @brief unit test for timed out operations
1290 *
1291 * This function provides unit testing capability to simulate
1292 * timed out operations. Recompilation with HV_TIMEOUT_TEST=1
1293 * is required.
1294 *
1295 * @param reqp pointer to a request structure
1296 * @param opcode SCSI operation being performed
1297 * @param wait if 1, wait for I/O to complete
1298 */
1299 static void
storvsc_timeout_test(struct hv_storvsc_request * reqp,uint8_t opcode,int wait)1300 storvsc_timeout_test(struct hv_storvsc_request *reqp,
1301 uint8_t opcode, int wait)
1302 {
1303 int ret;
1304 union ccb *ccb = reqp->ccb;
1305 struct storvsc_softc *sc = reqp->softc;
1306
1307 if (reqp->vstor_packet.vm_srb.cdb[0] != opcode) {
1308 return;
1309 }
1310
1311 if (wait) {
1312 mtx_lock(&reqp->event.mtx);
1313 }
1314 ret = hv_storvsc_io_request(sc, reqp);
1315 if (ret != 0) {
1316 if (wait) {
1317 mtx_unlock(&reqp->event.mtx);
1318 }
1319 printf("%s: io_request failed with %d.\n",
1320 __func__, ret);
1321 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
1322 mtx_lock(&sc->hs_lock);
1323 storvsc_free_request(sc, reqp);
1324 xpt_done(ccb);
1325 mtx_unlock(&sc->hs_lock);
1326 return;
1327 }
1328
1329 if (wait) {
1330 xpt_print(ccb->ccb_h.path,
1331 "%u: %s: waiting for IO return.\n",
1332 ticks, __func__);
1333 ret = cv_timedwait(&reqp->event.cv, &reqp->event.mtx, 60*hz);
1334 mtx_unlock(&reqp->event.mtx);
1335 xpt_print(ccb->ccb_h.path, "%u: %s: %s.\n",
1336 ticks, __func__, (ret == 0)?
1337 "IO return detected" :
1338 "IO return not detected");
1339 /*
1340 * Now both the timer handler and io done are running
1341 * simultaneously. We want to confirm the io done always
1342 * finishes after the timer handler exits. So reqp used by
1343 * timer handler is not freed or stale. Do busy loop for
1344 * another 1/10 second to make sure io done does
1345 * wait for the timer handler to complete.
1346 */
1347 DELAY(100*1000);
1348 mtx_lock(&sc->hs_lock);
1349 xpt_print(ccb->ccb_h.path,
1350 "%u: %s: finishing, queue frozen %d, "
1351 "ccb status 0x%x scsi_status 0x%x.\n",
1352 ticks, __func__, sc->hs_frozen,
1353 ccb->ccb_h.status,
1354 ccb->csio.scsi_status);
1355 mtx_unlock(&sc->hs_lock);
1356 }
1357 }
1358 #endif /* HVS_TIMEOUT_TEST */
1359
1360 #ifdef notyet
1361 /**
1362 * @brief timeout handler for requests
1363 *
1364 * This function is called as a result of a callout expiring.
1365 *
1366 * @param arg pointer to a request
1367 */
1368 static void
storvsc_timeout(void * arg)1369 storvsc_timeout(void *arg)
1370 {
1371 struct hv_storvsc_request *reqp = arg;
1372 struct storvsc_softc *sc = reqp->softc;
1373 union ccb *ccb = reqp->ccb;
1374
1375 if (reqp->retries == 0) {
1376 mtx_lock(&sc->hs_lock);
1377 xpt_print(ccb->ccb_h.path,
1378 "%u: IO timed out (req=0x%p), wait for another %u secs.\n",
1379 ticks, reqp, ccb->ccb_h.timeout / 1000);
1380 cam_error_print(ccb, CAM_ESF_ALL, CAM_EPF_ALL);
1381 mtx_unlock(&sc->hs_lock);
1382
1383 reqp->retries++;
1384 callout_reset_sbt(&reqp->callout, SBT_1MS * ccb->ccb_h.timeout,
1385 0, storvsc_timeout, reqp, 0);
1386 #if HVS_TIMEOUT_TEST
1387 storvsc_timeout_test(reqp, SEND_DIAGNOSTIC, 0);
1388 #endif
1389 return;
1390 }
1391
1392 mtx_lock(&sc->hs_lock);
1393 xpt_print(ccb->ccb_h.path,
1394 "%u: IO (reqp = 0x%p) did not return for %u seconds, %s.\n",
1395 ticks, reqp, ccb->ccb_h.timeout * (reqp->retries+1) / 1000,
1396 (sc->hs_frozen == 0)?
1397 "freezing the queue" : "the queue is already frozen");
1398 if (sc->hs_frozen == 0) {
1399 sc->hs_frozen = 1;
1400 xpt_freeze_simq(xpt_path_sim(ccb->ccb_h.path), 1);
1401 }
1402 mtx_unlock(&sc->hs_lock);
1403
1404 #if HVS_TIMEOUT_TEST
1405 storvsc_timeout_test(reqp, MODE_SELECT_10, 1);
1406 #endif
1407 }
1408 #endif
1409
1410 /**
1411 * @brief StorVSC device poll function
1412 *
1413 * This function is responsible for servicing requests when
1414 * interrupts are disabled (i.e when we are dumping core.)
1415 *
1416 * @param sim a pointer to a CAM SCSI interface module
1417 */
1418 static void
storvsc_poll(struct cam_sim * sim)1419 storvsc_poll(struct cam_sim *sim)
1420 {
1421 struct storvsc_softc *sc = cam_sim_softc(sim);
1422
1423 mtx_assert(&sc->hs_lock, MA_OWNED);
1424 mtx_unlock(&sc->hs_lock);
1425 hv_storvsc_on_channel_callback(sc->hs_chan, sc);
1426 mtx_lock(&sc->hs_lock);
1427 }
1428
1429 /**
1430 * @brief StorVSC device action function
1431 *
1432 * This function is responsible for handling SCSI operations which
1433 * are passed from the CAM layer. The requests are in the form of
1434 * CAM control blocks which indicate the action being performed.
1435 * Not all actions require converting the request to a VSCSI protocol
1436 * message - these actions can be responded to by this driver.
1437 * Requests which are destined for a backend storage device are converted
1438 * to a VSCSI protocol message and sent on the channel connection associated
1439 * with this device.
1440 *
1441 * @param sim pointer to a CAM SCSI interface module
1442 * @param ccb pointer to a CAM control block
1443 */
1444 static void
storvsc_action(struct cam_sim * sim,union ccb * ccb)1445 storvsc_action(struct cam_sim *sim, union ccb *ccb)
1446 {
1447 struct storvsc_softc *sc = cam_sim_softc(sim);
1448 int res;
1449
1450 mtx_assert(&sc->hs_lock, MA_OWNED);
1451 switch (ccb->ccb_h.func_code) {
1452 case XPT_PATH_INQ: {
1453 struct ccb_pathinq *cpi = &ccb->cpi;
1454
1455 cpi->version_num = 1;
1456 cpi->hba_inquiry = PI_TAG_ABLE|PI_SDTR_ABLE;
1457 cpi->target_sprt = 0;
1458 cpi->hba_misc = PIM_NOBUSRESET;
1459 if (hv_storvsc_use_pim_unmapped)
1460 cpi->hba_misc |= PIM_UNMAPPED;
1461 cpi->maxio = STORVSC_DATA_SIZE_MAX;
1462 cpi->hba_eng_cnt = 0;
1463 cpi->max_target = STORVSC_MAX_TARGETS;
1464 cpi->max_lun = sc->hs_drv_props->drv_max_luns_per_target;
1465 cpi->initiator_id = cpi->max_target;
1466 cpi->bus_id = cam_sim_bus(sim);
1467 cpi->base_transfer_speed = 300000;
1468 cpi->transport = XPORT_SAS;
1469 cpi->transport_version = 0;
1470 cpi->protocol = PROTO_SCSI;
1471 cpi->protocol_version = SCSI_REV_SPC2;
1472 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1473 strlcpy(cpi->hba_vid, sc->hs_drv_props->drv_name, HBA_IDLEN);
1474 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1475 cpi->unit_number = cam_sim_unit(sim);
1476
1477 ccb->ccb_h.status = CAM_REQ_CMP;
1478 xpt_done(ccb);
1479 return;
1480 }
1481 case XPT_GET_TRAN_SETTINGS: {
1482 struct ccb_trans_settings *cts = &ccb->cts;
1483
1484 cts->transport = XPORT_SAS;
1485 cts->transport_version = 0;
1486 cts->protocol = PROTO_SCSI;
1487 cts->protocol_version = SCSI_REV_SPC2;
1488
1489 /* enable tag queuing and disconnected mode */
1490 cts->proto_specific.valid = CTS_SCSI_VALID_TQ;
1491 cts->proto_specific.scsi.valid = CTS_SCSI_VALID_TQ;
1492 cts->proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB;
1493 cts->xport_specific.valid = CTS_SPI_VALID_DISC;
1494 cts->xport_specific.spi.flags = CTS_SPI_FLAGS_DISC_ENB;
1495
1496 ccb->ccb_h.status = CAM_REQ_CMP;
1497 xpt_done(ccb);
1498 return;
1499 }
1500 case XPT_SET_TRAN_SETTINGS: {
1501 ccb->ccb_h.status = CAM_REQ_CMP;
1502 xpt_done(ccb);
1503 return;
1504 }
1505 case XPT_CALC_GEOMETRY:{
1506 cam_calc_geometry(&ccb->ccg, 1);
1507 xpt_done(ccb);
1508 return;
1509 }
1510 case XPT_RESET_BUS:
1511 case XPT_RESET_DEV:{
1512 #if HVS_HOST_RESET
1513 if ((res = hv_storvsc_host_reset(sc)) != 0) {
1514 xpt_print(ccb->ccb_h.path,
1515 "hv_storvsc_host_reset failed with %d\n", res);
1516 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
1517 xpt_done(ccb);
1518 return;
1519 }
1520 ccb->ccb_h.status = CAM_REQ_CMP;
1521 xpt_done(ccb);
1522 return;
1523 #else
1524 xpt_print(ccb->ccb_h.path,
1525 "%s reset not supported.\n",
1526 (ccb->ccb_h.func_code == XPT_RESET_BUS)?
1527 "bus" : "dev");
1528 ccb->ccb_h.status = CAM_REQ_INVALID;
1529 xpt_done(ccb);
1530 return;
1531 #endif /* HVS_HOST_RESET */
1532 }
1533 case XPT_SCSI_IO:
1534 case XPT_IMMED_NOTIFY: {
1535 struct hv_storvsc_request *reqp = NULL;
1536 bus_dmamap_t dmap_saved;
1537
1538 if (ccb->csio.cdb_len == 0) {
1539 panic("cdl_len is 0\n");
1540 }
1541
1542 if (LIST_EMPTY(&sc->hs_free_list)) {
1543 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1544 if (sc->hs_frozen == 0) {
1545 sc->hs_frozen = 1;
1546 xpt_freeze_simq(sim, /* count*/1);
1547 }
1548 xpt_done(ccb);
1549 return;
1550 }
1551
1552 reqp = LIST_FIRST(&sc->hs_free_list);
1553 LIST_REMOVE(reqp, link);
1554
1555 /* Save the data_dmap before reset request */
1556 dmap_saved = reqp->data_dmap;
1557
1558 /* XXX this is ugly */
1559 bzero(reqp, sizeof(struct hv_storvsc_request));
1560
1561 /* Restore necessary bits */
1562 reqp->data_dmap = dmap_saved;
1563 reqp->softc = sc;
1564
1565 ccb->ccb_h.status |= CAM_SIM_QUEUED;
1566 if ((res = create_storvsc_request(ccb, reqp)) != 0) {
1567 ccb->ccb_h.status = CAM_REQ_INVALID;
1568 xpt_done(ccb);
1569 return;
1570 }
1571
1572 #ifdef notyet
1573 if (ccb->ccb_h.timeout != CAM_TIME_INFINITY) {
1574 callout_init(&reqp->callout, 1);
1575 callout_reset_sbt(&reqp->callout,
1576 SBT_1MS * ccb->ccb_h.timeout, 0,
1577 storvsc_timeout, reqp, 0);
1578 #if HVS_TIMEOUT_TEST
1579 cv_init(&reqp->event.cv, "storvsc timeout cv");
1580 mtx_init(&reqp->event.mtx, "storvsc timeout mutex",
1581 NULL, MTX_DEF);
1582 switch (reqp->vstor_packet.vm_srb.cdb[0]) {
1583 case MODE_SELECT_10:
1584 case SEND_DIAGNOSTIC:
1585 /* To have timer send the request. */
1586 return;
1587 default:
1588 break;
1589 }
1590 #endif /* HVS_TIMEOUT_TEST */
1591 }
1592 #endif
1593
1594 if ((res = hv_storvsc_io_request(sc, reqp)) != 0) {
1595 xpt_print(ccb->ccb_h.path,
1596 "hv_storvsc_io_request failed with %d\n", res);
1597 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
1598 storvsc_free_request(sc, reqp);
1599 xpt_done(ccb);
1600 return;
1601 }
1602 return;
1603 }
1604
1605 default:
1606 ccb->ccb_h.status = CAM_REQ_INVALID;
1607 xpt_done(ccb);
1608 return;
1609 }
1610 }
1611
1612 /**
1613 * @brief destroy bounce buffer
1614 *
1615 * This function is responsible for destroy a Scatter/Gather list
1616 * that create by storvsc_create_bounce_buffer()
1617 *
1618 * @param sgl- the Scatter/Gather need be destroy
1619 * @param sg_count- page count of the SG list.
1620 *
1621 */
1622 static void
storvsc_destroy_bounce_buffer(struct hv_sglist * sgl)1623 storvsc_destroy_bounce_buffer(struct hv_sglist *sgl)
1624 {
1625 struct hv_sgl_node *sgl_node = NULL;
1626 if (LIST_EMPTY(&g_hv_sgl_page_pool.in_use_sgl_list)) {
1627 printf("storvsc error: not enough in use sgl\n");
1628 return;
1629 }
1630 sgl_node = LIST_FIRST(&g_hv_sgl_page_pool.in_use_sgl_list);
1631 LIST_REMOVE(sgl_node, link);
1632 sgl_node->sgl_data = sgl;
1633 LIST_INSERT_HEAD(&g_hv_sgl_page_pool.free_sgl_list, sgl_node, link);
1634 }
1635
1636 /**
1637 * @brief create bounce buffer
1638 *
1639 * This function is responsible for create a Scatter/Gather list,
1640 * which hold several pages that can be aligned with page size.
1641 *
1642 * @param seg_count- SG-list segments count
1643 * @param write - if WRITE_TYPE, set SG list page used size to 0,
1644 * otherwise set used size to page size.
1645 *
1646 * return NULL if create failed
1647 */
1648 static struct hv_sglist *
storvsc_create_bounce_buffer(uint16_t seg_count,int write)1649 storvsc_create_bounce_buffer(uint16_t seg_count, int write)
1650 {
1651 int i = 0;
1652 struct hv_sglist *bounce_sgl = NULL;
1653 unsigned int buf_len = ((write == WRITE_TYPE) ? 0 : PAGE_SIZE);
1654 struct hv_sgl_node *sgl_node = NULL;
1655
1656 /* get struct hv_sglist from free_sgl_list */
1657 if (LIST_EMPTY(&g_hv_sgl_page_pool.free_sgl_list)) {
1658 printf("storvsc error: not enough free sgl\n");
1659 return NULL;
1660 }
1661 sgl_node = LIST_FIRST(&g_hv_sgl_page_pool.free_sgl_list);
1662 LIST_REMOVE(sgl_node, link);
1663 bounce_sgl = sgl_node->sgl_data;
1664 LIST_INSERT_HEAD(&g_hv_sgl_page_pool.in_use_sgl_list, sgl_node, link);
1665
1666 bounce_sgl->sg_maxseg = seg_count;
1667
1668 if (write == WRITE_TYPE)
1669 bounce_sgl->sg_nseg = 0;
1670 else
1671 bounce_sgl->sg_nseg = seg_count;
1672
1673 for (i = 0; i < seg_count; i++)
1674 bounce_sgl->sg_iov[i].iov_len = buf_len;
1675
1676 return bounce_sgl;
1677 }
1678
1679 /**
1680 * @brief copy data from SG list to bounce buffer
1681 *
1682 * This function is responsible for copy data from one SG list's segments
1683 * to another SG list which used as bounce buffer.
1684 *
1685 * @param bounce_sgl - the destination SG list
1686 * @param orig_sgl - the segment of the source SG list.
1687 * @param orig_sgl_count - the count of segments.
1688 * @param orig_sgl_count - indicate which segment need bounce buffer,
1689 * set 1 means need.
1690 *
1691 */
1692 static void
storvsc_copy_sgl_to_bounce_buf(struct hv_sglist * bounce_sgl,bus_dma_segment_t * orig_sgl,unsigned int orig_sgl_count,uint64_t seg_bits)1693 storvsc_copy_sgl_to_bounce_buf(struct hv_sglist *bounce_sgl,
1694 bus_dma_segment_t *orig_sgl,
1695 unsigned int orig_sgl_count,
1696 uint64_t seg_bits)
1697 {
1698 int src_sgl_idx = 0;
1699
1700 for (src_sgl_idx = 0; src_sgl_idx < orig_sgl_count; src_sgl_idx++) {
1701 if (seg_bits & (1 << src_sgl_idx)) {
1702 memcpy(bounce_sgl->sg_iov[src_sgl_idx].iov_base,
1703 (void*)orig_sgl[src_sgl_idx].ds_addr,
1704 orig_sgl[src_sgl_idx].ds_len);
1705
1706 bounce_sgl->sg_iov[src_sgl_idx].iov_len =
1707 orig_sgl[src_sgl_idx].ds_len;
1708 }
1709 }
1710 }
1711
1712 /**
1713 * @brief copy data from SG list which used as bounce to another SG list
1714 *
1715 * This function is responsible for copy data from one SG list with bounce
1716 * buffer to another SG list's segments.
1717 *
1718 * @param dest_sgl - the destination SG list's segments
1719 * @param dest_sgl_count - the count of destination SG list's segment.
1720 * @param src_sgl - the source SG list.
1721 * @param seg_bits - indicate which segment used bounce buffer of src SG-list.
1722 *
1723 */
1724 void
storvsc_copy_from_bounce_buf_to_sgl(bus_dma_segment_t * dest_sgl,unsigned int dest_sgl_count,struct hv_sglist * src_sgl,uint64_t seg_bits)1725 storvsc_copy_from_bounce_buf_to_sgl(bus_dma_segment_t *dest_sgl,
1726 unsigned int dest_sgl_count,
1727 struct hv_sglist* src_sgl,
1728 uint64_t seg_bits)
1729 {
1730 int sgl_idx = 0;
1731
1732 for (sgl_idx = 0; sgl_idx < dest_sgl_count; sgl_idx++) {
1733 if (seg_bits & (1 << sgl_idx)) {
1734 memcpy((void*)(dest_sgl[sgl_idx].ds_addr),
1735 src_sgl->sg_iov[sgl_idx].iov_base,
1736 src_sgl->sg_iov[sgl_idx].iov_len);
1737 }
1738 }
1739 }
1740
1741 /**
1742 * @brief check SG list with bounce buffer or not
1743 *
1744 * This function is responsible for check if need bounce buffer for SG list.
1745 *
1746 * @param sgl - the SG list's segments
1747 * @param sg_count - the count of SG list's segment.
1748 * @param bits - segmengs number that need bounce buffer
1749 *
1750 * return -1 if SG list needless bounce buffer
1751 */
1752 static int
storvsc_check_bounce_buffer_sgl(bus_dma_segment_t * sgl,unsigned int sg_count,uint64_t * bits)1753 storvsc_check_bounce_buffer_sgl(bus_dma_segment_t *sgl,
1754 unsigned int sg_count,
1755 uint64_t *bits)
1756 {
1757 int i = 0;
1758 int offset = 0;
1759 uint64_t phys_addr = 0;
1760 uint64_t tmp_bits = 0;
1761 boolean_t found_hole = FALSE;
1762 boolean_t pre_aligned = TRUE;
1763
1764 if (sg_count < 2){
1765 return -1;
1766 }
1767
1768 *bits = 0;
1769
1770 phys_addr = vtophys(sgl[0].ds_addr);
1771 offset = phys_addr - trunc_page(phys_addr);
1772
1773 if (offset != 0) {
1774 pre_aligned = FALSE;
1775 tmp_bits |= 1;
1776 }
1777
1778 for (i = 1; i < sg_count; i++) {
1779 phys_addr = vtophys(sgl[i].ds_addr);
1780 offset = phys_addr - trunc_page(phys_addr);
1781
1782 if (offset == 0) {
1783 if (FALSE == pre_aligned){
1784 /*
1785 * This segment is aligned, if the previous
1786 * one is not aligned, find a hole
1787 */
1788 found_hole = TRUE;
1789 }
1790 pre_aligned = TRUE;
1791 } else {
1792 tmp_bits |= 1ULL << i;
1793 if (!pre_aligned) {
1794 if (phys_addr != vtophys(sgl[i-1].ds_addr +
1795 sgl[i-1].ds_len)) {
1796 /*
1797 * Check whether connect to previous
1798 * segment,if not, find the hole
1799 */
1800 found_hole = TRUE;
1801 }
1802 } else {
1803 found_hole = TRUE;
1804 }
1805 pre_aligned = FALSE;
1806 }
1807 }
1808
1809 if (!found_hole) {
1810 return (-1);
1811 } else {
1812 *bits = tmp_bits;
1813 return 0;
1814 }
1815 }
1816
1817 /**
1818 * Copy bus_dma segments to multiple page buffer, which requires
1819 * the pages are compact composed except for the 1st and last pages.
1820 */
1821 static void
storvsc_xferbuf_prepare(void * arg,bus_dma_segment_t * segs,int nsegs,int error)1822 storvsc_xferbuf_prepare(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
1823 {
1824 struct hv_storvsc_request *reqp = arg;
1825 union ccb *ccb = reqp->ccb;
1826 struct ccb_scsiio *csio = &ccb->csio;
1827 struct storvsc_gpa_range *prplist;
1828 int i;
1829
1830 prplist = &reqp->prp_list;
1831 prplist->gpa_range.gpa_len = csio->dxfer_len;
1832 prplist->gpa_range.gpa_ofs = segs[0].ds_addr & PAGE_MASK;
1833
1834 for (i = 0; i < nsegs; i++) {
1835 #ifdef INVARIANTS
1836 if (nsegs > 1) {
1837 if (i == 0) {
1838 KASSERT((segs[i].ds_addr & PAGE_MASK) +
1839 segs[i].ds_len == PAGE_SIZE,
1840 ("invalid 1st page, ofs 0x%jx, len %zu",
1841 (uintmax_t)segs[i].ds_addr,
1842 segs[i].ds_len));
1843 } else if (i == nsegs - 1) {
1844 KASSERT((segs[i].ds_addr & PAGE_MASK) == 0,
1845 ("invalid last page, ofs 0x%jx",
1846 (uintmax_t)segs[i].ds_addr));
1847 } else {
1848 KASSERT((segs[i].ds_addr & PAGE_MASK) == 0 &&
1849 segs[i].ds_len == PAGE_SIZE,
1850 ("not a full page, ofs 0x%jx, len %zu",
1851 (uintmax_t)segs[i].ds_addr,
1852 segs[i].ds_len));
1853 }
1854 }
1855 #endif
1856 prplist->gpa_page[i] = atop(segs[i].ds_addr);
1857 }
1858 reqp->prp_cnt = nsegs;
1859
1860 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1861 bus_dmasync_op_t op;
1862
1863 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1864 op = BUS_DMASYNC_PREREAD;
1865 else
1866 op = BUS_DMASYNC_PREWRITE;
1867
1868 bus_dmamap_sync(reqp->softc->storvsc_req_dtag,
1869 reqp->data_dmap, op);
1870 }
1871 }
1872
1873 /**
1874 * @brief Fill in a request structure based on a CAM control block
1875 *
1876 * Fills in a request structure based on the contents of a CAM control
1877 * block. The request structure holds the payload information for
1878 * VSCSI protocol request.
1879 *
1880 * @param ccb pointer to a CAM contorl block
1881 * @param reqp pointer to a request structure
1882 */
1883 static int
create_storvsc_request(union ccb * ccb,struct hv_storvsc_request * reqp)1884 create_storvsc_request(union ccb *ccb, struct hv_storvsc_request *reqp)
1885 {
1886 struct ccb_scsiio *csio = &ccb->csio;
1887 uint64_t phys_addr;
1888 uint32_t pfn;
1889 uint64_t not_aligned_seg_bits = 0;
1890 int error;
1891
1892 /* refer to struct vmscsi_req for meanings of these two fields */
1893 reqp->vstor_packet.u.vm_srb.port =
1894 cam_sim_unit(xpt_path_sim(ccb->ccb_h.path));
1895 reqp->vstor_packet.u.vm_srb.path_id =
1896 cam_sim_bus(xpt_path_sim(ccb->ccb_h.path));
1897
1898 reqp->vstor_packet.u.vm_srb.target_id = ccb->ccb_h.target_id;
1899 reqp->vstor_packet.u.vm_srb.lun = ccb->ccb_h.target_lun;
1900
1901 reqp->vstor_packet.u.vm_srb.cdb_len = csio->cdb_len;
1902 if(ccb->ccb_h.flags & CAM_CDB_POINTER) {
1903 memcpy(&reqp->vstor_packet.u.vm_srb.u.cdb, csio->cdb_io.cdb_ptr,
1904 csio->cdb_len);
1905 } else {
1906 memcpy(&reqp->vstor_packet.u.vm_srb.u.cdb, csio->cdb_io.cdb_bytes,
1907 csio->cdb_len);
1908 }
1909
1910 if (hv_storvsc_use_win8ext_flags) {
1911 reqp->vstor_packet.u.vm_srb.win8_extension.time_out_value = 60;
1912 reqp->vstor_packet.u.vm_srb.win8_extension.srb_flags |=
1913 SRB_FLAGS_DISABLE_SYNCH_TRANSFER;
1914 }
1915 switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
1916 case CAM_DIR_OUT:
1917 reqp->vstor_packet.u.vm_srb.data_in = WRITE_TYPE;
1918 if (hv_storvsc_use_win8ext_flags) {
1919 reqp->vstor_packet.u.vm_srb.win8_extension.srb_flags |=
1920 SRB_FLAGS_DATA_OUT;
1921 }
1922 break;
1923 case CAM_DIR_IN:
1924 reqp->vstor_packet.u.vm_srb.data_in = READ_TYPE;
1925 if (hv_storvsc_use_win8ext_flags) {
1926 reqp->vstor_packet.u.vm_srb.win8_extension.srb_flags |=
1927 SRB_FLAGS_DATA_IN;
1928 }
1929 break;
1930 case CAM_DIR_NONE:
1931 reqp->vstor_packet.u.vm_srb.data_in = UNKNOWN_TYPE;
1932 if (hv_storvsc_use_win8ext_flags) {
1933 reqp->vstor_packet.u.vm_srb.win8_extension.srb_flags |=
1934 SRB_FLAGS_NO_DATA_TRANSFER;
1935 }
1936 break;
1937 default:
1938 printf("Error: unexpected data direction: 0x%x\n",
1939 ccb->ccb_h.flags & CAM_DIR_MASK);
1940 return (EINVAL);
1941 }
1942
1943 reqp->sense_data = &csio->sense_data;
1944 reqp->sense_info_len = csio->sense_len;
1945
1946 reqp->ccb = ccb;
1947 ccb->ccb_h.spriv_ptr0 = reqp;
1948
1949 if (0 == csio->dxfer_len) {
1950 return (0);
1951 }
1952
1953 switch (ccb->ccb_h.flags & CAM_DATA_MASK) {
1954 case CAM_DATA_BIO:
1955 case CAM_DATA_VADDR:
1956 error = bus_dmamap_load_ccb(reqp->softc->storvsc_req_dtag,
1957 reqp->data_dmap, ccb, storvsc_xferbuf_prepare, reqp,
1958 BUS_DMA_NOWAIT);
1959 if (error) {
1960 xpt_print(ccb->ccb_h.path,
1961 "bus_dmamap_load_ccb failed: %d\n", error);
1962 return (error);
1963 }
1964 if ((ccb->ccb_h.flags & CAM_DATA_MASK) == CAM_DATA_BIO)
1965 reqp->softc->sysctl_data.data_bio_cnt++;
1966 else
1967 reqp->softc->sysctl_data.data_vaddr_cnt++;
1968 break;
1969
1970 case CAM_DATA_SG:
1971 {
1972 struct storvsc_gpa_range *prplist;
1973 int i = 0;
1974 int offset = 0;
1975 int ret;
1976
1977 bus_dma_segment_t *storvsc_sglist =
1978 (bus_dma_segment_t *)ccb->csio.data_ptr;
1979 u_int16_t storvsc_sg_count = ccb->csio.sglist_cnt;
1980
1981 prplist = &reqp->prp_list;
1982 prplist->gpa_range.gpa_len = csio->dxfer_len;
1983
1984 printf("Storvsc: get SG I/O operation, %d\n",
1985 reqp->vstor_packet.u.vm_srb.data_in);
1986
1987 if (storvsc_sg_count > STORVSC_DATA_SEGCNT_MAX){
1988 printf("Storvsc: %d segments is too much, "
1989 "only support %d segments\n",
1990 storvsc_sg_count, STORVSC_DATA_SEGCNT_MAX);
1991 return (EINVAL);
1992 }
1993
1994 /*
1995 * We create our own bounce buffer function currently. Idealy
1996 * we should use BUS_DMA(9) framework. But with current BUS_DMA
1997 * code there is no callback API to check the page alignment of
1998 * middle segments before busdma can decide if a bounce buffer
1999 * is needed for particular segment. There is callback,
2000 * "bus_dma_filter_t *filter", but the parrameters are not
2001 * sufficient for storvsc driver.
2002 * TODO:
2003 * Add page alignment check in BUS_DMA(9) callback. Once
2004 * this is complete, switch the following code to use
2005 * BUS_DMA(9) for storvsc bounce buffer support.
2006 */
2007 /* check if we need to create bounce buffer */
2008 ret = storvsc_check_bounce_buffer_sgl(storvsc_sglist,
2009 storvsc_sg_count, ¬_aligned_seg_bits);
2010 if (ret != -1) {
2011 reqp->bounce_sgl =
2012 storvsc_create_bounce_buffer(storvsc_sg_count,
2013 reqp->vstor_packet.u.vm_srb.data_in);
2014 if (NULL == reqp->bounce_sgl) {
2015 printf("Storvsc_error: "
2016 "create bounce buffer failed.\n");
2017 return (ENOMEM);
2018 }
2019
2020 reqp->bounce_sgl_count = storvsc_sg_count;
2021 reqp->not_aligned_seg_bits = not_aligned_seg_bits;
2022
2023 /*
2024 * if it is write, we need copy the original data
2025 *to bounce buffer
2026 */
2027 if (WRITE_TYPE == reqp->vstor_packet.u.vm_srb.data_in) {
2028 storvsc_copy_sgl_to_bounce_buf(
2029 reqp->bounce_sgl,
2030 storvsc_sglist,
2031 storvsc_sg_count,
2032 reqp->not_aligned_seg_bits);
2033 }
2034
2035 /* transfer virtual address to physical frame number */
2036 if (reqp->not_aligned_seg_bits & 0x1){
2037 phys_addr =
2038 vtophys(reqp->bounce_sgl->sg_iov[0].iov_base);
2039 }else{
2040 phys_addr =
2041 vtophys(storvsc_sglist[0].ds_addr);
2042 }
2043 prplist->gpa_range.gpa_ofs = phys_addr & PAGE_MASK;
2044
2045 pfn = phys_addr >> PAGE_SHIFT;
2046 prplist->gpa_page[0] = pfn;
2047
2048 for (i = 1; i < storvsc_sg_count; i++) {
2049 if (reqp->not_aligned_seg_bits & (1 << i)) {
2050 phys_addr =
2051 vtophys(reqp->bounce_sgl->sg_iov[i].iov_base);
2052 } else {
2053 phys_addr =
2054 vtophys(storvsc_sglist[i].ds_addr);
2055 }
2056
2057 pfn = phys_addr >> PAGE_SHIFT;
2058 prplist->gpa_page[i] = pfn;
2059 }
2060 reqp->prp_cnt = i;
2061 } else {
2062 phys_addr = vtophys(storvsc_sglist[0].ds_addr);
2063
2064 prplist->gpa_range.gpa_ofs = phys_addr & PAGE_MASK;
2065
2066 for (i = 0; i < storvsc_sg_count; i++) {
2067 phys_addr = vtophys(storvsc_sglist[i].ds_addr);
2068 pfn = phys_addr >> PAGE_SHIFT;
2069 prplist->gpa_page[i] = pfn;
2070 }
2071 reqp->prp_cnt = i;
2072
2073 /* check the last segment cross boundary or not */
2074 offset = phys_addr & PAGE_MASK;
2075 if (offset) {
2076 /* Add one more PRP entry */
2077 phys_addr =
2078 vtophys(storvsc_sglist[i-1].ds_addr +
2079 PAGE_SIZE - offset);
2080 pfn = phys_addr >> PAGE_SHIFT;
2081 prplist->gpa_page[i] = pfn;
2082 reqp->prp_cnt++;
2083 }
2084
2085 reqp->bounce_sgl_count = 0;
2086 }
2087 reqp->softc->sysctl_data.data_sg_cnt++;
2088 break;
2089 }
2090 default:
2091 printf("Unknow flags: %d\n", ccb->ccb_h.flags);
2092 return(EINVAL);
2093 }
2094
2095 return(0);
2096 }
2097
2098 static uint32_t
is_scsi_valid(const struct scsi_inquiry_data * inq_data)2099 is_scsi_valid(const struct scsi_inquiry_data *inq_data)
2100 {
2101 u_int8_t type;
2102
2103 type = SID_TYPE(inq_data);
2104 if (type == T_NODEVICE)
2105 return (0);
2106 if (SID_QUAL(inq_data) == SID_QUAL_BAD_LU)
2107 return (0);
2108 return (1);
2109 }
2110
2111 /**
2112 * @brief completion function before returning to CAM
2113 *
2114 * I/O process has been completed and the result needs
2115 * to be passed to the CAM layer.
2116 * Free resources related to this request.
2117 *
2118 * @param reqp pointer to a request structure
2119 */
2120 static void
storvsc_io_done(struct hv_storvsc_request * reqp)2121 storvsc_io_done(struct hv_storvsc_request *reqp)
2122 {
2123 union ccb *ccb = reqp->ccb;
2124 struct ccb_scsiio *csio = &ccb->csio;
2125 struct storvsc_softc *sc = reqp->softc;
2126 struct vmscsi_req *vm_srb = &reqp->vstor_packet.u.vm_srb;
2127 bus_dma_segment_t *ori_sglist = NULL;
2128 int ori_sg_count = 0;
2129 const struct scsi_generic *cmd;
2130
2131 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
2132 bus_dmasync_op_t op;
2133
2134 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
2135 op = BUS_DMASYNC_POSTREAD;
2136 else
2137 op = BUS_DMASYNC_POSTWRITE;
2138
2139 bus_dmamap_sync(reqp->softc->storvsc_req_dtag,
2140 reqp->data_dmap, op);
2141 bus_dmamap_unload(sc->storvsc_req_dtag, reqp->data_dmap);
2142 }
2143
2144 /* destroy bounce buffer if it is used */
2145 if (reqp->bounce_sgl_count) {
2146 ori_sglist = (bus_dma_segment_t *)ccb->csio.data_ptr;
2147 ori_sg_count = ccb->csio.sglist_cnt;
2148
2149 /*
2150 * If it is READ operation, we should copy back the data
2151 * to original SG list.
2152 */
2153 if (READ_TYPE == reqp->vstor_packet.u.vm_srb.data_in) {
2154 storvsc_copy_from_bounce_buf_to_sgl(ori_sglist,
2155 ori_sg_count,
2156 reqp->bounce_sgl,
2157 reqp->not_aligned_seg_bits);
2158 }
2159
2160 storvsc_destroy_bounce_buffer(reqp->bounce_sgl);
2161 reqp->bounce_sgl_count = 0;
2162 }
2163
2164 if (reqp->retries > 0) {
2165 mtx_lock(&sc->hs_lock);
2166 #if HVS_TIMEOUT_TEST
2167 xpt_print(ccb->ccb_h.path,
2168 "%u: IO returned after timeout, "
2169 "waking up timer handler if any.\n", ticks);
2170 mtx_lock(&reqp->event.mtx);
2171 cv_signal(&reqp->event.cv);
2172 mtx_unlock(&reqp->event.mtx);
2173 #endif
2174 reqp->retries = 0;
2175 xpt_print(ccb->ccb_h.path,
2176 "%u: IO returned after timeout, "
2177 "stopping timer if any.\n", ticks);
2178 mtx_unlock(&sc->hs_lock);
2179 }
2180
2181 #ifdef notyet
2182 /*
2183 * callout_drain() will wait for the timer handler to finish
2184 * if it is running. So we don't need any lock to synchronize
2185 * between this routine and the timer handler.
2186 * Note that we need to make sure reqp is not freed when timer
2187 * handler is using or will use it.
2188 */
2189 if (ccb->ccb_h.timeout != CAM_TIME_INFINITY) {
2190 callout_drain(&reqp->callout);
2191 }
2192 #endif
2193 cmd = (const struct scsi_generic *)
2194 ((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
2195 csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes);
2196
2197 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
2198 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2199 int srb_status = SRB_STATUS(vm_srb->srb_status);
2200 #ifdef DIAGNOSTIC
2201 if (hv_storvsc_srb_status != -1) {
2202 srb_status = SRB_STATUS(hv_storvsc_srb_status & 0x3f);
2203 hv_storvsc_srb_status = -1;
2204 }
2205 #endif /* DIAGNOSTIC */
2206 if (vm_srb->scsi_status == SCSI_STATUS_OK) {
2207 if (srb_status != SRB_STATUS_SUCCESS) {
2208 bool log_error = true;
2209 switch (srb_status) {
2210 case SRB_STATUS_PENDING:
2211 /* We should never get this */
2212 panic("storvsc_io_done: SRB_STATUS_PENDING");
2213 break;
2214 case SRB_STATUS_ABORTED:
2215 /*
2216 * storvsc doesn't support aborts yet
2217 * but if we ever get this status
2218 * the I/O is complete - treat it as a
2219 * timeout
2220 */
2221 ccb->ccb_h.status |= CAM_CMD_TIMEOUT;
2222 break;
2223 case SRB_STATUS_ABORT_FAILED:
2224 /* We should never get this */
2225 panic("storvsc_io_done: SRB_STATUS_ABORT_FAILED");
2226 break;
2227 case SRB_STATUS_ERROR:
2228 /*
2229 * We should never get this.
2230 * Treat it as a CAM_UNREC_HBA_ERROR.
2231 * It will be retried
2232 */
2233 ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
2234 break;
2235 case SRB_STATUS_BUSY:
2236 /* Host is busy. Delay and retry */
2237 ccb->ccb_h.status |= CAM_BUSY;
2238 break;
2239 case SRB_STATUS_INVALID_REQUEST:
2240 case SRB_STATUS_INVALID_PATH_ID:
2241 case SRB_STATUS_NO_DEVICE:
2242 case SRB_STATUS_INVALID_TARGET_ID:
2243 /*
2244 * These indicate an invalid address
2245 * and really should never be seen.
2246 * A CAM_PATH_INVALID could be
2247 * used here but I want to run
2248 * down retries. Do a CAM_BUSY
2249 * since the host might be having issues.
2250 */
2251 ccb->ccb_h.status |= CAM_BUSY;
2252 break;
2253 case SRB_STATUS_TIMEOUT:
2254 case SRB_STATUS_COMMAND_TIMEOUT:
2255 /* The backend has timed this out */
2256 ccb->ccb_h.status |= CAM_BUSY;
2257 break;
2258 /* Some old pSCSI errors below */
2259 case SRB_STATUS_SELECTION_TIMEOUT:
2260 case SRB_STATUS_MESSAGE_REJECTED:
2261 case SRB_STATUS_PARITY_ERROR:
2262 case SRB_STATUS_NO_HBA:
2263 case SRB_STATUS_DATA_OVERRUN:
2264 case SRB_STATUS_UNEXPECTED_BUS_FREE:
2265 case SRB_STATUS_PHASE_SEQUENCE_FAILURE:
2266 /*
2267 * Old pSCSI responses, should never get.
2268 * If we do treat as a CAM_UNREC_HBA_ERROR
2269 * which will be retried
2270 */
2271 ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
2272 break;
2273 case SRB_STATUS_BUS_RESET:
2274 ccb->ccb_h.status |= CAM_SCSI_BUS_RESET;
2275 break;
2276 case SRB_STATUS_BAD_SRB_BLOCK_LENGTH:
2277 /*
2278 * The request block is malformed and
2279 * I doubt it is from the guest. Just retry.
2280 */
2281 ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
2282 break;
2283 /* Not used statuses just retry */
2284 case SRB_STATUS_REQUEST_FLUSHED:
2285 case SRB_STATUS_BAD_FUNCTION:
2286 case SRB_STATUS_NOT_POWERED:
2287 ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
2288 break;
2289 case SRB_STATUS_INVALID_LUN:
2290 /*
2291 * Don't log an EMS for this response since
2292 * there is no device at this LUN. This is a
2293 * normal and expected response when a device
2294 * is detached.
2295 */
2296 ccb->ccb_h.status |= CAM_DEV_NOT_THERE;
2297 log_error = false;
2298 break;
2299 case SRB_STATUS_ERROR_RECOVERY:
2300 case SRB_STATUS_LINK_DOWN:
2301 /*
2302 * I don't ever expect these from
2303 * the host but if we ever get
2304 * retry after a delay
2305 */
2306 ccb->ccb_h.status |= CAM_BUSY;
2307 break;
2308 default:
2309 /*
2310 * An undefined response assert on
2311 * on debug builds else retry
2312 */
2313 ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
2314 KASSERT(srb_status <= SRB_STATUS_LINK_DOWN,
2315 ("storvsc: %s, unexpected srb_status of 0x%x",
2316 __func__, srb_status));
2317 break;
2318 }
2319 if (log_error) {
2320 xpt_print(ccb->ccb_h.path, "The hypervisor's I/O adapter "
2321 "driver received an unexpected response code 0x%x "
2322 "for operation: %s. If this continues to occur, "
2323 "report the condition to your hypervisor vendor so "
2324 "they can rectify the issue.\n", srb_status,
2325 scsi_op_desc(cmd->opcode, NULL));
2326 }
2327 } else {
2328 ccb->ccb_h.status |= CAM_REQ_CMP;
2329 }
2330
2331 if (cmd->opcode == INQUIRY &&
2332 srb_status == SRB_STATUS_SUCCESS) {
2333 int resp_xfer_len, resp_buf_len, data_len;
2334 uint8_t *resp_buf = (uint8_t *)csio->data_ptr;
2335 struct scsi_inquiry_data *inq_data =
2336 (struct scsi_inquiry_data *)csio->data_ptr;
2337
2338 /* Get the buffer length reported by host */
2339 resp_xfer_len = vm_srb->transfer_len;
2340
2341 /* Get the available buffer length */
2342 resp_buf_len = resp_xfer_len >= 5 ? resp_buf[4] + 5 : 0;
2343 data_len = (resp_buf_len < resp_xfer_len) ?
2344 resp_buf_len : resp_xfer_len;
2345 if (bootverbose && data_len >= 5) {
2346 xpt_print(ccb->ccb_h.path, "storvsc inquiry "
2347 "(%d) [%x %x %x %x %x ... ]\n", data_len,
2348 resp_buf[0], resp_buf[1], resp_buf[2],
2349 resp_buf[3], resp_buf[4]);
2350 }
2351 /*
2352 * XXX: Hyper-V (since win2012r2) responses inquiry with
2353 * unknown version (0) for GEN-2 DVD device.
2354 * Manually set the version number to SPC3 in order to
2355 * ask CAM to continue probing with "PROBE_REPORT_LUNS".
2356 * see probedone() in scsi_xpt.c
2357 */
2358 if (SID_TYPE(inq_data) == T_CDROM &&
2359 inq_data->version == 0 &&
2360 (vmstor_proto_version >= VMSTOR_PROTOCOL_VERSION_WIN8)) {
2361 inq_data->version = SCSI_REV_SPC3;
2362 if (bootverbose) {
2363 xpt_print(ccb->ccb_h.path,
2364 "set version from 0 to %d\n",
2365 inq_data->version);
2366 }
2367 }
2368 /*
2369 * XXX: Manually fix the wrong response returned from WS2012
2370 */
2371 if (!is_scsi_valid(inq_data) &&
2372 (vmstor_proto_version == VMSTOR_PROTOCOL_VERSION_WIN8_1 ||
2373 vmstor_proto_version == VMSTOR_PROTOCOL_VERSION_WIN8 ||
2374 vmstor_proto_version == VMSTOR_PROTOCOL_VERSION_WIN7)) {
2375 if (data_len >= 4 &&
2376 (resp_buf[2] == 0 || resp_buf[3] == 0)) {
2377 resp_buf[2] = SCSI_REV_SPC3;
2378 resp_buf[3] = 2; // resp fmt must be 2
2379 if (bootverbose)
2380 xpt_print(ccb->ccb_h.path,
2381 "fix version and resp fmt for 0x%x\n",
2382 vmstor_proto_version);
2383 }
2384 } else if (data_len >= SHORT_INQUIRY_LENGTH) {
2385 char vendor[16];
2386
2387 cam_strvis(vendor, inq_data->vendor,
2388 sizeof(inq_data->vendor), sizeof(vendor));
2389 /*
2390 * XXX: Upgrade SPC2 to SPC3 if host is WIN8 or
2391 * WIN2012 R2 in order to support UNMAP feature.
2392 */
2393 if (!strncmp(vendor, "Msft", 4) &&
2394 SID_ANSI_REV(inq_data) == SCSI_REV_SPC2 &&
2395 (vmstor_proto_version ==
2396 VMSTOR_PROTOCOL_VERSION_WIN8_1 ||
2397 vmstor_proto_version ==
2398 VMSTOR_PROTOCOL_VERSION_WIN8)) {
2399 inq_data->version = SCSI_REV_SPC3;
2400 if (bootverbose) {
2401 xpt_print(ccb->ccb_h.path,
2402 "storvsc upgrades "
2403 "SPC2 to SPC3\n");
2404 }
2405 }
2406 }
2407 }
2408 } else {
2409 /**
2410 * On Some Windows hosts TEST_UNIT_READY command can return
2411 * SRB_STATUS_ERROR and sense data, for example, asc=0x3a,1
2412 * "(Medium not present - tray closed)". This error can be
2413 * ignored since it will be sent to host periodically.
2414 */
2415 boolean_t unit_not_ready = \
2416 vm_srb->scsi_status == SCSI_STATUS_CHECK_COND &&
2417 cmd->opcode == TEST_UNIT_READY &&
2418 srb_status == SRB_STATUS_ERROR;
2419 if (!unit_not_ready && bootverbose) {
2420 mtx_lock(&sc->hs_lock);
2421 xpt_print(ccb->ccb_h.path,
2422 "storvsc scsi_status = %d, srb_status = %d\n",
2423 vm_srb->scsi_status, srb_status);
2424 mtx_unlock(&sc->hs_lock);
2425 }
2426 ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
2427 }
2428
2429 ccb->csio.scsi_status = (vm_srb->scsi_status & 0xFF);
2430 if (srb_status == SRB_STATUS_SUCCESS ||
2431 srb_status == SRB_STATUS_DATA_OVERRUN)
2432 ccb->csio.resid = ccb->csio.dxfer_len - vm_srb->transfer_len;
2433 else
2434 ccb->csio.resid = ccb->csio.dxfer_len;
2435
2436 if ((vm_srb->srb_status & SRB_STATUS_AUTOSENSE_VALID) != 0 &&
2437 reqp->sense_info_len != 0) {
2438 csio->sense_resid = csio->sense_len - reqp->sense_info_len;
2439 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
2440 }
2441
2442 mtx_lock(&sc->hs_lock);
2443 if (reqp->softc->hs_frozen == 1) {
2444 xpt_print(ccb->ccb_h.path,
2445 "%u: storvsc unfreezing softc 0x%p.\n",
2446 ticks, reqp->softc);
2447 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
2448 reqp->softc->hs_frozen = 0;
2449 }
2450 storvsc_free_request(sc, reqp);
2451 mtx_unlock(&sc->hs_lock);
2452
2453 xpt_done_direct(ccb);
2454 }
2455
2456 /**
2457 * @brief Free a request structure
2458 *
2459 * Free a request structure by returning it to the free list
2460 *
2461 * @param sc pointer to a softc
2462 * @param reqp pointer to a request structure
2463 */
2464 static void
storvsc_free_request(struct storvsc_softc * sc,struct hv_storvsc_request * reqp)2465 storvsc_free_request(struct storvsc_softc *sc, struct hv_storvsc_request *reqp)
2466 {
2467
2468 LIST_INSERT_HEAD(&sc->hs_free_list, reqp, link);
2469 }
2470
2471 /**
2472 * @brief Determine type of storage device from GUID
2473 *
2474 * Using the type GUID, determine if this is a StorVSC (paravirtual
2475 * SCSI or BlkVSC (paravirtual IDE) device.
2476 *
2477 * @param dev a device
2478 * returns an enum
2479 */
2480 static enum hv_storage_type
storvsc_get_storage_type(device_t dev)2481 storvsc_get_storage_type(device_t dev)
2482 {
2483 device_t parent = device_get_parent(dev);
2484
2485 if (VMBUS_PROBE_GUID(parent, dev, &gBlkVscDeviceType) == 0)
2486 return DRIVER_BLKVSC;
2487 if (VMBUS_PROBE_GUID(parent, dev, &gStorVscDeviceType) == 0)
2488 return DRIVER_STORVSC;
2489 return DRIVER_UNKNOWN;
2490 }
2491
2492 #define PCI_VENDOR_INTEL 0x8086
2493 #define PCI_PRODUCT_PIIX4 0x7111
2494
2495 static void
storvsc_ada_probe_veto(void * arg __unused,struct cam_path * path,struct ata_params * ident_buf __unused,int * veto)2496 storvsc_ada_probe_veto(void *arg __unused, struct cam_path *path,
2497 struct ata_params *ident_buf __unused, int *veto)
2498 {
2499
2500 /*
2501 * The ATA disks are shared with the controllers managed
2502 * by this driver, so veto the ATA disks' attachment; the
2503 * ATA disks will be attached as SCSI disks once this driver
2504 * attached.
2505 */
2506 if (path->device->protocol == PROTO_ATA) {
2507 struct ccb_pathinq cpi;
2508
2509 xpt_path_inq(&cpi, path);
2510 if (cpi.ccb_h.status == CAM_REQ_CMP &&
2511 cpi.hba_vendor == PCI_VENDOR_INTEL &&
2512 cpi.hba_device == PCI_PRODUCT_PIIX4) {
2513 (*veto)++;
2514 if (bootverbose) {
2515 xpt_print(path,
2516 "Disable ATA disks on "
2517 "simulated ATA controller (0x%04x%04x)\n",
2518 cpi.hba_device, cpi.hba_vendor);
2519 }
2520 }
2521 }
2522 }
2523
2524 static void
storvsc_sysinit(void * arg __unused)2525 storvsc_sysinit(void *arg __unused)
2526 {
2527 if (vm_guest == VM_GUEST_HV) {
2528 storvsc_handler_tag = EVENTHANDLER_REGISTER(ada_probe_veto,
2529 storvsc_ada_probe_veto, NULL, EVENTHANDLER_PRI_ANY);
2530 }
2531 }
2532 SYSINIT(storvsc_sys_init, SI_SUB_DRIVERS, SI_ORDER_SECOND, storvsc_sysinit,
2533 NULL);
2534
2535 static void
storvsc_sysuninit(void * arg __unused)2536 storvsc_sysuninit(void *arg __unused)
2537 {
2538 if (storvsc_handler_tag != NULL)
2539 EVENTHANDLER_DEREGISTER(ada_probe_veto, storvsc_handler_tag);
2540 }
2541 SYSUNINIT(storvsc_sys_uninit, SI_SUB_DRIVERS, SI_ORDER_SECOND,
2542 storvsc_sysuninit, NULL);
2543