xref: /freebsd/sys/cam/scsi/scsi_da.c (revision 43a5ec4eb41567cc92586503212743d89686d78f)
1 /*-
2  * Implementation of SCSI Direct Access Peripheral driver for CAM.
3  *
4  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5  *
6  * Copyright (c) 1997 Justin T. Gibbs.
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, this list of conditions, and the following disclaimer,
14  *    without modification, immediately at the beginning of the file.
15  * 2. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
22  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/param.h>
35 
36 #ifdef _KERNEL
37 #include "opt_da.h"
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/bio.h>
41 #include <sys/sysctl.h>
42 #include <sys/taskqueue.h>
43 #include <sys/lock.h>
44 #include <sys/mutex.h>
45 #include <sys/conf.h>
46 #include <sys/devicestat.h>
47 #include <sys/eventhandler.h>
48 #include <sys/malloc.h>
49 #include <sys/cons.h>
50 #include <sys/endian.h>
51 #include <sys/proc.h>
52 #include <sys/reboot.h>
53 #include <sys/sbuf.h>
54 #include <geom/geom.h>
55 #include <geom/geom_disk.h>
56 #include <machine/atomic.h>
57 #endif /* _KERNEL */
58 
59 #ifndef _KERNEL
60 #include <stdio.h>
61 #include <string.h>
62 #endif /* _KERNEL */
63 
64 #include <cam/cam.h>
65 #include <cam/cam_ccb.h>
66 #include <cam/cam_periph.h>
67 #include <cam/cam_xpt_periph.h>
68 #ifdef _KERNEL
69 #include <cam/cam_xpt_internal.h>
70 #endif /* _KERNEL */
71 #include <cam/cam_sim.h>
72 #include <cam/cam_iosched.h>
73 
74 #include <cam/scsi/scsi_message.h>
75 #include <cam/scsi/scsi_da.h>
76 
77 #ifdef _KERNEL
78 /*
79  * Note that there are probe ordering dependencies here.  The order isn't
80  * controlled by this enumeration, but by explicit state transitions in
81  * dastart() and dadone().  Here are some of the dependencies:
82  *
83  * 1. RC should come first, before RC16, unless there is evidence that RC16
84  *    is supported.
85  * 2. BDC needs to come before any of the ATA probes, or the ZONE probe.
86  * 3. The ATA probes should go in this order:
87  *    ATA -> LOGDIR -> IDDIR -> SUP -> ATA_ZONE
88  */
89 typedef enum {
90 	DA_STATE_PROBE_WP,
91 	DA_STATE_PROBE_RC,
92 	DA_STATE_PROBE_RC16,
93 	DA_STATE_PROBE_LBP,
94 	DA_STATE_PROBE_BLK_LIMITS,
95 	DA_STATE_PROBE_BDC,
96 	DA_STATE_PROBE_ATA,
97 	DA_STATE_PROBE_ATA_LOGDIR,
98 	DA_STATE_PROBE_ATA_IDDIR,
99 	DA_STATE_PROBE_ATA_SUP,
100 	DA_STATE_PROBE_ATA_ZONE,
101 	DA_STATE_PROBE_ZONE,
102 	DA_STATE_NORMAL
103 } da_state;
104 
105 typedef enum {
106 	DA_FLAG_PACK_INVALID	= 0x000001,
107 	DA_FLAG_NEW_PACK	= 0x000002,
108 	DA_FLAG_PACK_LOCKED	= 0x000004,
109 	DA_FLAG_PACK_REMOVABLE	= 0x000008,
110 	DA_FLAG_ROTATING	= 0x000010,
111 	DA_FLAG_NEED_OTAG	= 0x000020,
112 	DA_FLAG_WAS_OTAG	= 0x000040,
113 	DA_FLAG_RETRY_UA	= 0x000080,
114 	DA_FLAG_OPEN		= 0x000100,
115 	DA_FLAG_SCTX_INIT	= 0x000200,
116 	DA_FLAG_CAN_RC16	= 0x000400,
117 	DA_FLAG_PROBED		= 0x000800,
118 	DA_FLAG_DIRTY		= 0x001000,
119 	DA_FLAG_ANNOUNCED	= 0x002000,
120 	DA_FLAG_CAN_ATA_DMA	= 0x004000,
121 	DA_FLAG_CAN_ATA_LOG	= 0x008000,
122 	DA_FLAG_CAN_ATA_IDLOG	= 0x010000,
123 	DA_FLAG_CAN_ATA_SUPCAP	= 0x020000,
124 	DA_FLAG_CAN_ATA_ZONE	= 0x040000,
125 	DA_FLAG_TUR_PENDING	= 0x080000,
126 	DA_FLAG_UNMAPPEDIO	= 0x100000
127 } da_flags;
128 #define DA_FLAG_STRING		\
129 	"\020"			\
130 	"\001PACK_INVALID"	\
131 	"\002NEW_PACK"		\
132 	"\003PACK_LOCKED"	\
133 	"\004PACK_REMOVABLE"	\
134 	"\005ROTATING"		\
135 	"\006NEED_OTAG"		\
136 	"\007WAS_OTAG"		\
137 	"\010RETRY_UA"		\
138 	"\011OPEN"		\
139 	"\012SCTX_INIT"		\
140 	"\013CAN_RC16"		\
141 	"\014PROBED"		\
142 	"\015DIRTY"		\
143 	"\016ANNOUCNED"		\
144 	"\017CAN_ATA_DMA"	\
145 	"\020CAN_ATA_LOG"	\
146 	"\021CAN_ATA_IDLOG"	\
147 	"\022CAN_ATA_SUPACP"	\
148 	"\023CAN_ATA_ZONE"	\
149 	"\024TUR_PENDING"	\
150 	"\025UNMAPPEDIO"
151 
152 typedef enum {
153 	DA_Q_NONE		= 0x00,
154 	DA_Q_NO_SYNC_CACHE	= 0x01,
155 	DA_Q_NO_6_BYTE		= 0x02,
156 	DA_Q_NO_PREVENT		= 0x04,
157 	DA_Q_4K			= 0x08,
158 	DA_Q_NO_RC16		= 0x10,
159 	DA_Q_NO_UNMAP		= 0x20,
160 	DA_Q_RETRY_BUSY		= 0x40,
161 	DA_Q_SMR_DM		= 0x80,
162 	DA_Q_STRICT_UNMAP	= 0x100,
163 	DA_Q_128KB		= 0x200
164 } da_quirks;
165 
166 #define DA_Q_BIT_STRING		\
167 	"\020"			\
168 	"\001NO_SYNC_CACHE"	\
169 	"\002NO_6_BYTE"		\
170 	"\003NO_PREVENT"	\
171 	"\0044K"		\
172 	"\005NO_RC16"		\
173 	"\006NO_UNMAP"		\
174 	"\007RETRY_BUSY"	\
175 	"\010SMR_DM"		\
176 	"\011STRICT_UNMAP"	\
177 	"\012128KB"
178 
179 typedef enum {
180 	DA_CCB_PROBE_RC		= 0x01,
181 	DA_CCB_PROBE_RC16	= 0x02,
182 	DA_CCB_PROBE_LBP	= 0x03,
183 	DA_CCB_PROBE_BLK_LIMITS	= 0x04,
184 	DA_CCB_PROBE_BDC	= 0x05,
185 	DA_CCB_PROBE_ATA	= 0x06,
186 	DA_CCB_BUFFER_IO	= 0x07,
187 	DA_CCB_DUMP		= 0x0A,
188 	DA_CCB_DELETE		= 0x0B,
189 	DA_CCB_TUR		= 0x0C,
190 	DA_CCB_PROBE_ZONE	= 0x0D,
191 	DA_CCB_PROBE_ATA_LOGDIR	= 0x0E,
192 	DA_CCB_PROBE_ATA_IDDIR	= 0x0F,
193 	DA_CCB_PROBE_ATA_SUP	= 0x10,
194 	DA_CCB_PROBE_ATA_ZONE	= 0x11,
195 	DA_CCB_PROBE_WP		= 0x12,
196 	DA_CCB_TYPE_MASK	= 0x1F,
197 	DA_CCB_RETRY_UA		= 0x20
198 } da_ccb_state;
199 
200 /*
201  * Order here is important for method choice
202  *
203  * We prefer ATA_TRIM as tests run against a Sandforce 2281 SSD attached to
204  * LSI 2008 (mps) controller (FW: v12, Drv: v14) resulted 20% quicker deletes
205  * using ATA_TRIM than the corresponding UNMAP results for a real world mysql
206  * import taking 5mins.
207  *
208  */
209 typedef enum {
210 	DA_DELETE_NONE,
211 	DA_DELETE_DISABLE,
212 	DA_DELETE_ATA_TRIM,
213 	DA_DELETE_UNMAP,
214 	DA_DELETE_WS16,
215 	DA_DELETE_WS10,
216 	DA_DELETE_ZERO,
217 	DA_DELETE_MIN = DA_DELETE_ATA_TRIM,
218 	DA_DELETE_MAX = DA_DELETE_ZERO
219 } da_delete_methods;
220 
221 /*
222  * For SCSI, host managed drives show up as a separate device type.  For
223  * ATA, host managed drives also have a different device signature.
224  * XXX KDM figure out the ATA host managed signature.
225  */
226 typedef enum {
227 	DA_ZONE_NONE		= 0x00,
228 	DA_ZONE_DRIVE_MANAGED	= 0x01,
229 	DA_ZONE_HOST_AWARE	= 0x02,
230 	DA_ZONE_HOST_MANAGED	= 0x03
231 } da_zone_mode;
232 
233 /*
234  * We distinguish between these interface cases in addition to the drive type:
235  * o ATA drive behind a SCSI translation layer that knows about ZBC/ZAC
236  * o ATA drive behind a SCSI translation layer that does not know about
237  *   ZBC/ZAC, and so needs to be managed via ATA passthrough.  In this
238  *   case, we would need to share the ATA code with the ada(4) driver.
239  * o SCSI drive.
240  */
241 typedef enum {
242 	DA_ZONE_IF_SCSI,
243 	DA_ZONE_IF_ATA_PASS,
244 	DA_ZONE_IF_ATA_SAT,
245 } da_zone_interface;
246 
247 typedef enum {
248 	DA_ZONE_FLAG_RZ_SUP		= 0x0001,
249 	DA_ZONE_FLAG_OPEN_SUP		= 0x0002,
250 	DA_ZONE_FLAG_CLOSE_SUP		= 0x0004,
251 	DA_ZONE_FLAG_FINISH_SUP		= 0x0008,
252 	DA_ZONE_FLAG_RWP_SUP		= 0x0010,
253 	DA_ZONE_FLAG_SUP_MASK		= (DA_ZONE_FLAG_RZ_SUP |
254 					   DA_ZONE_FLAG_OPEN_SUP |
255 					   DA_ZONE_FLAG_CLOSE_SUP |
256 					   DA_ZONE_FLAG_FINISH_SUP |
257 					   DA_ZONE_FLAG_RWP_SUP),
258 	DA_ZONE_FLAG_URSWRZ		= 0x0020,
259 	DA_ZONE_FLAG_OPT_SEQ_SET	= 0x0040,
260 	DA_ZONE_FLAG_OPT_NONSEQ_SET	= 0x0080,
261 	DA_ZONE_FLAG_MAX_SEQ_SET	= 0x0100,
262 	DA_ZONE_FLAG_SET_MASK		= (DA_ZONE_FLAG_OPT_SEQ_SET |
263 					   DA_ZONE_FLAG_OPT_NONSEQ_SET |
264 					   DA_ZONE_FLAG_MAX_SEQ_SET)
265 } da_zone_flags;
266 
267 static struct da_zone_desc {
268 	da_zone_flags value;
269 	const char *desc;
270 } da_zone_desc_table[] = {
271 	{DA_ZONE_FLAG_RZ_SUP, "Report Zones" },
272 	{DA_ZONE_FLAG_OPEN_SUP, "Open" },
273 	{DA_ZONE_FLAG_CLOSE_SUP, "Close" },
274 	{DA_ZONE_FLAG_FINISH_SUP, "Finish" },
275 	{DA_ZONE_FLAG_RWP_SUP, "Reset Write Pointer" },
276 };
277 
278 typedef void da_delete_func_t (struct cam_periph *periph, union ccb *ccb,
279 			      struct bio *bp);
280 static da_delete_func_t da_delete_trim;
281 static da_delete_func_t da_delete_unmap;
282 static da_delete_func_t da_delete_ws;
283 
284 static const void * da_delete_functions[] = {
285 	NULL,
286 	NULL,
287 	da_delete_trim,
288 	da_delete_unmap,
289 	da_delete_ws,
290 	da_delete_ws,
291 	da_delete_ws
292 };
293 
294 static const char *da_delete_method_names[] =
295     { "NONE", "DISABLE", "ATA_TRIM", "UNMAP", "WS16", "WS10", "ZERO" };
296 static const char *da_delete_method_desc[] =
297     { "NONE", "DISABLED", "ATA TRIM", "UNMAP", "WRITE SAME(16) with UNMAP",
298       "WRITE SAME(10) with UNMAP", "ZERO" };
299 
300 /* Offsets into our private area for storing information */
301 #define ccb_state	ppriv_field0
302 #define ccb_bp		ppriv_ptr1
303 
304 struct disk_params {
305 	u_int8_t  heads;
306 	u_int32_t cylinders;
307 	u_int8_t  secs_per_track;
308 	u_int32_t secsize;	/* Number of bytes/sector */
309 	u_int64_t sectors;	/* total number sectors */
310 	u_int     stripesize;
311 	u_int     stripeoffset;
312 };
313 
314 #define UNMAP_RANGE_MAX		0xffffffff
315 #define UNMAP_HEAD_SIZE		8
316 #define UNMAP_RANGE_SIZE	16
317 #define UNMAP_MAX_RANGES	2048 /* Protocol Max is 4095 */
318 #define UNMAP_BUF_SIZE		((UNMAP_MAX_RANGES * UNMAP_RANGE_SIZE) + \
319 				UNMAP_HEAD_SIZE)
320 
321 #define WS10_MAX_BLKS		0xffff
322 #define WS16_MAX_BLKS		0xffffffff
323 #define ATA_TRIM_MAX_RANGES	((UNMAP_BUF_SIZE / \
324 	(ATA_DSM_RANGE_SIZE * ATA_DSM_BLK_SIZE)) * ATA_DSM_BLK_SIZE)
325 
326 #define DA_WORK_TUR		(1 << 16)
327 
328 typedef enum {
329 	DA_REF_OPEN = 1,
330 	DA_REF_OPEN_HOLD,
331 	DA_REF_CLOSE_HOLD,
332 	DA_REF_PROBE_HOLD,
333 	DA_REF_TUR,
334 	DA_REF_GEOM,
335 	DA_REF_SYSCTL,
336 	DA_REF_REPROBE,
337 	DA_REF_MAX		/* KEEP LAST */
338 } da_ref_token;
339 
340 struct da_softc {
341 	struct   cam_iosched_softc *cam_iosched;
342 	struct	 bio_queue_head delete_run_queue;
343 	LIST_HEAD(, ccb_hdr) pending_ccbs;
344 	int	 refcount;		/* Active xpt_action() calls */
345 	da_state state;
346 	da_flags flags;
347 	da_quirks quirks;
348 	int	 minimum_cmd_size;
349 	int	 error_inject;
350 	int	 trim_max_ranges;
351 	int	 delete_available;	/* Delete methods possibly available */
352 	da_zone_mode			zone_mode;
353 	da_zone_interface		zone_interface;
354 	da_zone_flags			zone_flags;
355 	struct ata_gp_log_dir		ata_logdir;
356 	int				valid_logdir_len;
357 	struct ata_identify_log_pages	ata_iddir;
358 	int				valid_iddir_len;
359 	uint64_t			optimal_seq_zones;
360 	uint64_t			optimal_nonseq_zones;
361 	uint64_t			max_seq_zones;
362 	u_int			maxio;
363 	uint32_t		unmap_max_ranges;
364 	uint32_t		unmap_max_lba; /* Max LBAs in UNMAP req */
365 	uint32_t		unmap_gran;
366 	uint32_t		unmap_gran_align;
367 	uint64_t		ws_max_blks;
368 	uint64_t		trim_count;
369 	uint64_t		trim_ranges;
370 	uint64_t		trim_lbas;
371 	da_delete_methods	delete_method_pref;
372 	da_delete_methods	delete_method;
373 	da_delete_func_t	*delete_func;
374 	int			p_type;
375 	struct	 disk_params params;
376 	struct	 disk *disk;
377 	struct task		sysctl_task;
378 	struct sysctl_ctx_list	sysctl_ctx;
379 	struct sysctl_oid	*sysctl_tree;
380 	struct callout		sendordered_c;
381 	uint64_t wwpn;
382 	uint8_t	 unmap_buf[UNMAP_BUF_SIZE];
383 	struct scsi_read_capacity_data_long rcaplong;
384 	struct callout		mediapoll_c;
385 	int			ref_flags[DA_REF_MAX];
386 #ifdef CAM_IO_STATS
387 	struct sysctl_ctx_list	sysctl_stats_ctx;
388 	struct sysctl_oid	*sysctl_stats_tree;
389 	u_int	errors;
390 	u_int	timeouts;
391 	u_int	invalidations;
392 #endif
393 #define DA_ANNOUNCETMP_SZ 160
394 	char			announce_temp[DA_ANNOUNCETMP_SZ];
395 #define DA_ANNOUNCE_SZ 400
396 	char			announcebuf[DA_ANNOUNCE_SZ];
397 };
398 
399 #define dadeleteflag(softc, delete_method, enable)			\
400 	if (enable) {							\
401 		softc->delete_available |= (1 << delete_method);	\
402 	} else {							\
403 		softc->delete_available &= ~(1 << delete_method);	\
404 	}
405 
406 static uma_zone_t da_ccb_zone;
407 
408 struct da_quirk_entry {
409 	struct scsi_inquiry_pattern inq_pat;
410 	da_quirks quirks;
411 };
412 
413 static const char quantum[] = "QUANTUM";
414 static const char microp[] = "MICROP";
415 
416 static struct da_quirk_entry da_quirk_table[] =
417 {
418 	/* SPI, FC devices */
419 	{
420 		/*
421 		 * Fujitsu M2513A MO drives.
422 		 * Tested devices: M2513A2 firmware versions 1200 & 1300.
423 		 * (dip switch selects whether T_DIRECT or T_OPTICAL device)
424 		 * Reported by: W.Scholten <whs@xs4all.nl>
425 		 */
426 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
427 		/*quirks*/ DA_Q_NO_SYNC_CACHE
428 	},
429 	{
430 		/* See above. */
431 		{T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
432 		/*quirks*/ DA_Q_NO_SYNC_CACHE
433 	},
434 	{
435 		/*
436 		 * This particular Fujitsu drive doesn't like the
437 		 * synchronize cache command.
438 		 * Reported by: Tom Jackson <toj@gorilla.net>
439 		 */
440 		{T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
441 		/*quirks*/ DA_Q_NO_SYNC_CACHE
442 	},
443 	{
444 		/*
445 		 * This drive doesn't like the synchronize cache command
446 		 * either.  Reported by: Matthew Jacob <mjacob@feral.com>
447 		 * in NetBSD PR kern/6027, August 24, 1998.
448 		 */
449 		{T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
450 		/*quirks*/ DA_Q_NO_SYNC_CACHE
451 	},
452 	{
453 		/*
454 		 * This drive doesn't like the synchronize cache command
455 		 * either.  Reported by: Hellmuth Michaelis (hm@kts.org)
456 		 * (PR 8882).
457 		 */
458 		{T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
459 		/*quirks*/ DA_Q_NO_SYNC_CACHE
460 	},
461 	{
462 		/*
463 		 * Doesn't like the synchronize cache command.
464 		 * Reported by: Blaz Zupan <blaz@gold.amis.net>
465 		 */
466 		{T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
467 		/*quirks*/ DA_Q_NO_SYNC_CACHE
468 	},
469 	{
470 		/*
471 		 * Doesn't like the synchronize cache command.
472 		 * Reported by: Blaz Zupan <blaz@gold.amis.net>
473 		 */
474 		{T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
475 		/*quirks*/ DA_Q_NO_SYNC_CACHE
476 	},
477 	{
478 		/*
479 		 * Doesn't like the synchronize cache command.
480 		 */
481 		{T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
482 		/*quirks*/ DA_Q_NO_SYNC_CACHE
483 	},
484 	{
485 		/*
486 		 * Doesn't like the synchronize cache command.
487 		 * Reported by: walter@pelissero.de
488 		 */
489 		{T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS540S", "*"},
490 		/*quirks*/ DA_Q_NO_SYNC_CACHE
491 	},
492 	{
493 		/*
494 		 * Doesn't work correctly with 6 byte reads/writes.
495 		 * Returns illegal request, and points to byte 9 of the
496 		 * 6-byte CDB.
497 		 * Reported by:  Adam McDougall <bsdx@spawnet.com>
498 		 */
499 		{T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
500 		/*quirks*/ DA_Q_NO_6_BYTE
501 	},
502 	{
503 		/* See above. */
504 		{T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
505 		/*quirks*/ DA_Q_NO_6_BYTE
506 	},
507 	{
508 		/*
509 		 * Doesn't like the synchronize cache command.
510 		 * Reported by: walter@pelissero.de
511 		 */
512 		{T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CP3500*", "*"},
513 		/*quirks*/ DA_Q_NO_SYNC_CACHE
514 	},
515 	{
516 		/*
517 		 * The CISS RAID controllers do not support SYNC_CACHE
518 		 */
519 		{T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"},
520 		/*quirks*/ DA_Q_NO_SYNC_CACHE
521 	},
522 	{
523 		/*
524 		 * The STEC SSDs sometimes hang on UNMAP.
525 		 */
526 		{T_DIRECT, SIP_MEDIA_FIXED, "STEC", "*", "*"},
527 		/*quirks*/ DA_Q_NO_UNMAP
528 	},
529 	{
530 		/*
531 		 * VMware returns BUSY status when storage has transient
532 		 * connectivity problems, so better wait.
533 		 * Also VMware returns odd errors on misaligned UNMAPs.
534 		 */
535 		{T_DIRECT, SIP_MEDIA_FIXED, "VMware*", "*", "*"},
536 		/*quirks*/ DA_Q_RETRY_BUSY | DA_Q_STRICT_UNMAP
537 	},
538 	/* USB mass storage devices supported by umass(4) */
539 	{
540 		/*
541 		 * EXATELECOM (Sigmatel) i-Bead 100/105 USB Flash MP3 Player
542 		 * PR: kern/51675
543 		 */
544 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "EXATEL", "i-BEAD10*", "*"},
545 		/*quirks*/ DA_Q_NO_SYNC_CACHE
546 	},
547 	{
548 		/*
549 		 * Power Quotient Int. (PQI) USB flash key
550 		 * PR: kern/53067
551 		 */
552 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "USB Flash Disk*",
553 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
554 	},
555 	{
556 		/*
557 		 * Creative Nomad MUVO mp3 player (USB)
558 		 * PR: kern/53094
559 		 */
560 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
561 		/*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
562 	},
563 	{
564 		/*
565 		 * Jungsoft NEXDISK USB flash key
566 		 * PR: kern/54737
567 		 */
568 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "JUNGSOFT", "NEXDISK*", "*"},
569 		/*quirks*/ DA_Q_NO_SYNC_CACHE
570 	},
571 	{
572 		/*
573 		 * FreeDik USB Mini Data Drive
574 		 * PR: kern/54786
575 		 */
576 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "FreeDik*", "Mini Data Drive",
577 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
578 	},
579 	{
580 		/*
581 		 * Sigmatel USB Flash MP3 Player
582 		 * PR: kern/57046
583 		 */
584 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "SigmaTel", "MSCN", "*"},
585 		/*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
586 	},
587 	{
588 		/*
589 		 * Neuros USB Digital Audio Computer
590 		 * PR: kern/63645
591 		 */
592 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "NEUROS", "dig. audio comp.",
593 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
594 	},
595 	{
596 		/*
597 		 * SEAGRAND NP-900 MP3 Player
598 		 * PR: kern/64563
599 		 */
600 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "SEAGRAND", "NP-900*", "*"},
601 		/*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
602 	},
603 	{
604 		/*
605 		 * iRiver iFP MP3 player (with UMS Firmware)
606 		 * PR: kern/54881, i386/63941, kern/66124
607 		 */
608 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "iRiver", "iFP*", "*"},
609 		/*quirks*/ DA_Q_NO_SYNC_CACHE
610 	},
611 	{
612 		/*
613 		 * Frontier Labs NEX IA+ Digital Audio Player, rev 1.10/0.01
614 		 * PR: kern/70158
615 		 */
616 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "FL" , "Nex*", "*"},
617 		/*quirks*/ DA_Q_NO_SYNC_CACHE
618 	},
619 	{
620 		/*
621 		 * ZICPlay USB MP3 Player with FM
622 		 * PR: kern/75057
623 		 */
624 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "ACTIONS*" , "USB DISK*", "*"},
625 		/*quirks*/ DA_Q_NO_SYNC_CACHE
626 	},
627 	{
628 		/*
629 		 * TEAC USB floppy mechanisms
630 		 */
631 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "TEAC" , "FD-05*", "*"},
632 		/*quirks*/ DA_Q_NO_SYNC_CACHE
633 	},
634 	{
635 		/*
636 		 * Kingston DataTraveler II+ USB Pen-Drive.
637 		 * Reported by: Pawel Jakub Dawidek <pjd@FreeBSD.org>
638 		 */
639 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston" , "DataTraveler II+",
640 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
641 	},
642 	{
643 		/*
644 		 * USB DISK Pro PMAP
645 		 * Reported by: jhs
646 		 * PR: usb/96381
647 		 */
648 		{T_DIRECT, SIP_MEDIA_REMOVABLE, " ", "USB DISK Pro", "PMAP"},
649 		/*quirks*/ DA_Q_NO_SYNC_CACHE
650 	},
651 	{
652 		/*
653 		 * Motorola E398 Mobile Phone (TransFlash memory card).
654 		 * Reported by: Wojciech A. Koszek <dunstan@FreeBSD.czest.pl>
655 		 * PR: usb/89889
656 		 */
657 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "Motorola" , "Motorola Phone",
658 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
659 	},
660 	{
661 		/*
662 		 * Qware BeatZkey! Pro
663 		 * PR: usb/79164
664 		 */
665 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "GENERIC", "USB DISK DEVICE",
666 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
667 	},
668 	{
669 		/*
670 		 * Time DPA20B 1GB MP3 Player
671 		 * PR: usb/81846
672 		 */
673 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "USB2.0*", "(FS) FLASH DISK*",
674 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
675 	},
676 	{
677 		/*
678 		 * Samsung USB key 128Mb
679 		 * PR: usb/90081
680 		 */
681 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "USB-DISK", "FreeDik-FlashUsb",
682 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
683 	},
684 	{
685 		/*
686 		 * Kingston DataTraveler 2.0 USB Flash memory.
687 		 * PR: usb/89196
688 		 */
689 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler 2.0",
690 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
691 	},
692 	{
693 		/*
694 		 * Creative MUVO Slim mp3 player (USB)
695 		 * PR: usb/86131
696 		 */
697 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "MuVo Slim",
698 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
699 		},
700 	{
701 		/*
702 		 * United MP5512 Portable MP3 Player (2-in-1 USB DISK/MP3)
703 		 * PR: usb/80487
704 		 */
705 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "MUSIC DISK",
706 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
707 	},
708 	{
709 		/*
710 		 * SanDisk Micro Cruzer 128MB
711 		 * PR: usb/75970
712 		 */
713 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "SanDisk" , "Micro Cruzer",
714 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
715 	},
716 	{
717 		/*
718 		 * TOSHIBA TransMemory USB sticks
719 		 * PR: kern/94660
720 		 */
721 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "TOSHIBA", "TransMemory",
722 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
723 	},
724 	{
725 		/*
726 		 * PNY USB 3.0 Flash Drives
727 		*/
728 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "PNY", "USB 3.0 FD*",
729 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_RC16
730 	},
731 	{
732 		/*
733 		 * PNY USB Flash keys
734 		 * PR: usb/75578, usb/72344, usb/65436
735 		 */
736 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "*" , "USB DISK*",
737 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
738 	},
739 	{
740 		/*
741 		 * Genesys GL3224
742 		 */
743 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "STORAGE DEVICE*",
744 		"120?"}, /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_4K | DA_Q_NO_RC16
745 	},
746 	{
747 		/*
748 		 * Genesys 6-in-1 Card Reader
749 		 * PR: usb/94647
750 		 */
751 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "STORAGE DEVICE*",
752 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
753 	},
754 	{
755 		/*
756 		 * Rekam Digital CAMERA
757 		 * PR: usb/98713
758 		 */
759 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "CAMERA*", "4MP-9J6*",
760 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
761 	},
762 	{
763 		/*
764 		 * iRiver H10 MP3 player
765 		 * PR: usb/102547
766 		 */
767 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "H10*",
768 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
769 	},
770 	{
771 		/*
772 		 * iRiver U10 MP3 player
773 		 * PR: usb/92306
774 		 */
775 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "U10*",
776 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
777 	},
778 	{
779 		/*
780 		 * X-Micro Flash Disk
781 		 * PR: usb/96901
782 		 */
783 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "X-Micro", "Flash Disk",
784 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
785 	},
786 	{
787 		/*
788 		 * EasyMP3 EM732X USB 2.0 Flash MP3 Player
789 		 * PR: usb/96546
790 		 */
791 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "EM732X", "MP3 Player*",
792 		"1.00"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
793 	},
794 	{
795 		/*
796 		 * Denver MP3 player
797 		 * PR: usb/107101
798 		 */
799 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "DENVER", "MP3 PLAYER",
800 		 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
801 	},
802 	{
803 		/*
804 		 * Philips USB Key Audio KEY013
805 		 * PR: usb/68412
806 		 */
807 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "PHILIPS", "Key*", "*"},
808 		/*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
809 	},
810 	{
811 		/*
812 		 * JNC MP3 Player
813 		 * PR: usb/94439
814 		 */
815 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "JNC*" , "MP3 Player*",
816 		 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
817 	},
818 	{
819 		/*
820 		 * SAMSUNG MP0402H
821 		 * PR: usb/108427
822 		 */
823 		{T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "MP0402H", "*"},
824 		/*quirks*/ DA_Q_NO_SYNC_CACHE
825 	},
826 	{
827 		/*
828 		 * I/O Magic USB flash - Giga Bank
829 		 * PR: usb/108810
830 		 */
831 		{T_DIRECT, SIP_MEDIA_FIXED, "GS-Magic", "stor*", "*"},
832 		/*quirks*/ DA_Q_NO_SYNC_CACHE
833 	},
834 	{
835 		/*
836 		 * JoyFly 128mb USB Flash Drive
837 		 * PR: 96133
838 		 */
839 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "Flash Disk*",
840 		 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
841 	},
842 	{
843 		/*
844 		 * ChipsBnk usb stick
845 		 * PR: 103702
846 		 */
847 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "ChipsBnk", "USB*",
848 		 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
849 	},
850 	{
851 		/*
852 		 * Storcase (Kingston) InfoStation IFS FC2/SATA-R 201A
853 		 * PR: 129858
854 		 */
855 		{T_DIRECT, SIP_MEDIA_FIXED, "IFS", "FC2/SATA-R*",
856 		 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
857 	},
858 	{
859 		/*
860 		 * Samsung YP-U3 mp3-player
861 		 * PR: 125398
862 		 */
863 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "Samsung", "YP-U3",
864 		 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
865 	},
866 	{
867 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "Netac", "OnlyDisk*",
868 		 "2000"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
869 	},
870 	{
871 		/*
872 		 * Sony Cyber-Shot DSC cameras
873 		 * PR: usb/137035
874 		 */
875 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Sony DSC", "*"},
876 		/*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
877 	},
878 	{
879 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler G3",
880 		 "1.00"}, /*quirks*/ DA_Q_NO_PREVENT
881 	},
882 	{
883 		/* At least several Transcent USB sticks lie on RC16. */
884 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "JetFlash", "Transcend*",
885 		 "*"}, /*quirks*/ DA_Q_NO_RC16
886 	},
887 	{
888 		/*
889 		 * I-O Data USB Flash Disk
890 		 * PR: usb/211716
891 		 */
892 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "I-O DATA", "USB Flash Disk*",
893 		 "*"}, /*quirks*/ DA_Q_NO_RC16
894 	},
895 	{
896 		/*
897 		 * SLC CHIPFANCIER USB drives
898 		 * PR: usb/234503 (RC10 right, RC16 wrong)
899 		 * 16GB, 32GB and 128GB confirmed to have same issue
900 		 */
901 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "*SLC", "CHIPFANCIER",
902 		 "*"}, /*quirks*/ DA_Q_NO_RC16
903        },
904 	/* ATA/SATA devices over SAS/USB/... */
905 	{
906 		/* Sandisk X400 */
907 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SanDisk SD8SB8U1*", "*" },
908 		/*quirks*/DA_Q_128KB
909 	},
910 	{
911 		/* Hitachi Advanced Format (4k) drives */
912 		{ T_DIRECT, SIP_MEDIA_FIXED, "Hitachi", "H??????????E3*", "*" },
913 		/*quirks*/DA_Q_4K
914 	},
915 	{
916 		/* Micron Advanced Format (4k) drives */
917 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Micron 5100 MTFDDAK*", "*" },
918 		/*quirks*/DA_Q_4K
919 	},
920 	{
921 		/* Samsung Advanced Format (4k) drives */
922 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD155UI*", "*" },
923 		/*quirks*/DA_Q_4K
924 	},
925 	{
926 		/* Samsung Advanced Format (4k) drives */
927 		{ T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD155UI*", "*" },
928 		/*quirks*/DA_Q_4K
929 	},
930 	{
931 		/* Samsung Advanced Format (4k) drives */
932 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD204UI*", "*" },
933 		/*quirks*/DA_Q_4K
934 	},
935 	{
936 		/* Samsung Advanced Format (4k) drives */
937 		{ T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD204UI*", "*" },
938 		/*quirks*/DA_Q_4K
939 	},
940 	{
941 		/* Seagate Barracuda Green Advanced Format (4k) drives */
942 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DL*", "*" },
943 		/*quirks*/DA_Q_4K
944 	},
945 	{
946 		/* Seagate Barracuda Green Advanced Format (4k) drives */
947 		{ T_DIRECT, SIP_MEDIA_FIXED, "ST????DL", "*", "*" },
948 		/*quirks*/DA_Q_4K
949 	},
950 	{
951 		/* Seagate Barracuda Green Advanced Format (4k) drives */
952 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???DM*", "*" },
953 		/*quirks*/DA_Q_4K
954 	},
955 	{
956 		/* Seagate Barracuda Green Advanced Format (4k) drives */
957 		{ T_DIRECT, SIP_MEDIA_FIXED, "ST???DM*", "*", "*" },
958 		/*quirks*/DA_Q_4K
959 	},
960 	{
961 		/* Seagate Barracuda Green Advanced Format (4k) drives */
962 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DM*", "*" },
963 		/*quirks*/DA_Q_4K
964 	},
965 	{
966 		/* Seagate Barracuda Green Advanced Format (4k) drives */
967 		{ T_DIRECT, SIP_MEDIA_FIXED, "ST????DM", "*", "*" },
968 		/*quirks*/DA_Q_4K
969 	},
970 	{
971 		/* Seagate Momentus Advanced Format (4k) drives */
972 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500423AS*", "*" },
973 		/*quirks*/DA_Q_4K
974 	},
975 	{
976 		/* Seagate Momentus Advanced Format (4k) drives */
977 		{ T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "3AS*", "*" },
978 		/*quirks*/DA_Q_4K
979 	},
980 	{
981 		/* Seagate Momentus Advanced Format (4k) drives */
982 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500424AS*", "*" },
983 		/*quirks*/DA_Q_4K
984 	},
985 	{
986 		/* Seagate Momentus Advanced Format (4k) drives */
987 		{ T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "4AS*", "*" },
988 		/*quirks*/DA_Q_4K
989 	},
990 	{
991 		/* Seagate Momentus Advanced Format (4k) drives */
992 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640423AS*", "*" },
993 		/*quirks*/DA_Q_4K
994 	},
995 	{
996 		/* Seagate Momentus Advanced Format (4k) drives */
997 		{ T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "3AS*", "*" },
998 		/*quirks*/DA_Q_4K
999 	},
1000 	{
1001 		/* Seagate Momentus Advanced Format (4k) drives */
1002 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640424AS*", "*" },
1003 		/*quirks*/DA_Q_4K
1004 	},
1005 	{
1006 		/* Seagate Momentus Advanced Format (4k) drives */
1007 		{ T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "4AS*", "*" },
1008 		/*quirks*/DA_Q_4K
1009 	},
1010 	{
1011 		/* Seagate Momentus Advanced Format (4k) drives */
1012 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750420AS*", "*" },
1013 		/*quirks*/DA_Q_4K
1014 	},
1015 	{
1016 		/* Seagate Momentus Advanced Format (4k) drives */
1017 		{ T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "0AS*", "*" },
1018 		/*quirks*/DA_Q_4K
1019 	},
1020 	{
1021 		/* Seagate Momentus Advanced Format (4k) drives */
1022 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750422AS*", "*" },
1023 		/*quirks*/DA_Q_4K
1024 	},
1025 	{
1026 		/* Seagate Momentus Advanced Format (4k) drives */
1027 		{ T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "2AS*", "*" },
1028 		/*quirks*/DA_Q_4K
1029 	},
1030 	{
1031 		/* Seagate Momentus Advanced Format (4k) drives */
1032 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750423AS*", "*" },
1033 		/*quirks*/DA_Q_4K
1034 	},
1035 	{
1036 		/* Seagate Momentus Advanced Format (4k) drives */
1037 		{ T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "3AS*", "*" },
1038 		/*quirks*/DA_Q_4K
1039 	},
1040 	{
1041 		/* Seagate Momentus Thin Advanced Format (4k) drives */
1042 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???LT*", "*" },
1043 		/*quirks*/DA_Q_4K
1044 	},
1045 	{
1046 		/* Seagate Momentus Thin Advanced Format (4k) drives */
1047 		{ T_DIRECT, SIP_MEDIA_FIXED, "ST???LT*", "*", "*" },
1048 		/*quirks*/DA_Q_4K
1049 	},
1050 	{
1051 		/* WDC Caviar Green Advanced Format (4k) drives */
1052 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RS*", "*" },
1053 		/*quirks*/DA_Q_4K
1054 	},
1055 	{
1056 		/* WDC Caviar Green Advanced Format (4k) drives */
1057 		{ T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RS*", "*" },
1058 		/*quirks*/DA_Q_4K
1059 	},
1060 	{
1061 		/* WDC Caviar Green Advanced Format (4k) drives */
1062 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RX*", "*" },
1063 		/*quirks*/DA_Q_4K
1064 	},
1065 	{
1066 		/* WDC Caviar Green Advanced Format (4k) drives */
1067 		{ T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RX*", "*" },
1068 		/*quirks*/DA_Q_4K
1069 	},
1070 	{
1071 		/* WDC Caviar Green Advanced Format (4k) drives */
1072 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RS*", "*" },
1073 		/*quirks*/DA_Q_4K
1074 	},
1075 	{
1076 		/* WDC Caviar Green Advanced Format (4k) drives */
1077 		{ T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RS*", "*" },
1078 		/*quirks*/DA_Q_4K
1079 	},
1080 	{
1081 		/* WDC Caviar Green Advanced Format (4k) drives */
1082 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RX*", "*" },
1083 		/*quirks*/DA_Q_4K
1084 	},
1085 	{
1086 		/* WDC Caviar Green Advanced Format (4k) drives */
1087 		{ T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RX*", "*" },
1088 		/*quirks*/DA_Q_4K
1089 	},
1090 	{
1091 		/* WDC Scorpio Black Advanced Format (4k) drives */
1092 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PKT*", "*" },
1093 		/*quirks*/DA_Q_4K
1094 	},
1095 	{
1096 		/* WDC Scorpio Black Advanced Format (4k) drives */
1097 		{ T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PKT*", "*" },
1098 		/*quirks*/DA_Q_4K
1099 	},
1100 	{
1101 		/* WDC Scorpio Black Advanced Format (4k) drives */
1102 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PKT*", "*" },
1103 		/*quirks*/DA_Q_4K
1104 	},
1105 	{
1106 		/* WDC Scorpio Black Advanced Format (4k) drives */
1107 		{ T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PKT*", "*" },
1108 		/*quirks*/DA_Q_4K
1109 	},
1110 	{
1111 		/* WDC Scorpio Blue Advanced Format (4k) drives */
1112 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PVT*", "*" },
1113 		/*quirks*/DA_Q_4K
1114 	},
1115 	{
1116 		/* WDC Scorpio Blue Advanced Format (4k) drives */
1117 		{ T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PVT*", "*" },
1118 		/*quirks*/DA_Q_4K
1119 	},
1120 	{
1121 		/* WDC Scorpio Blue Advanced Format (4k) drives */
1122 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PVT*", "*" },
1123 		/*quirks*/DA_Q_4K
1124 	},
1125 	{
1126 		/* WDC Scorpio Blue Advanced Format (4k) drives */
1127 		{ T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PVT*", "*" },
1128 		/*quirks*/DA_Q_4K
1129 	},
1130 	{
1131 		/*
1132 		 * Olympus digital cameras (C-3040ZOOM, C-2040ZOOM, C-1)
1133 		 * PR: usb/97472
1134 		 */
1135 		{ T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "C*", "*"},
1136 		/*quirks*/ DA_Q_NO_6_BYTE | DA_Q_NO_SYNC_CACHE
1137 	},
1138 	{
1139 		/*
1140 		 * Olympus digital cameras (D-370)
1141 		 * PR: usb/97472
1142 		 */
1143 		{ T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "D*", "*"},
1144 		/*quirks*/ DA_Q_NO_6_BYTE
1145 	},
1146 	{
1147 		/*
1148 		 * Olympus digital cameras (E-100RS, E-10).
1149 		 * PR: usb/97472
1150 		 */
1151 		{ T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "E*", "*"},
1152 		/*quirks*/ DA_Q_NO_6_BYTE | DA_Q_NO_SYNC_CACHE
1153 	},
1154 	{
1155 		/*
1156 		 * Olympus FE-210 camera
1157 		 */
1158 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "FE210*",
1159 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
1160 	},
1161 	{
1162 		/*
1163 		* Pentax Digital Camera
1164 		* PR: usb/93389
1165 		*/
1166 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "PENTAX", "DIGITAL CAMERA",
1167 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
1168 	},
1169 	{
1170 		/*
1171 		 * LG UP3S MP3 player
1172 		 */
1173 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "LG", "UP3S",
1174 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
1175 	},
1176 	{
1177 		/*
1178 		 * Laser MP3-2GA13 MP3 player
1179 		 */
1180 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "(HS) Flash Disk",
1181 		"*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
1182 	},
1183 	{
1184 		/*
1185 		 * LaCie external 250GB Hard drive des by Porsche
1186 		 * Submitted by: Ben Stuyts <ben@altesco.nl>
1187 		 * PR: 121474
1188 		 */
1189 		{T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HM250JI", "*"},
1190 		/*quirks*/ DA_Q_NO_SYNC_CACHE
1191 	},
1192 	/* SATA SSDs */
1193 	{
1194 		/*
1195 		 * Corsair Force 2 SSDs
1196 		 * 4k optimised & trim only works in 4k requests + 4k aligned
1197 		 */
1198 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair CSSD-F*", "*" },
1199 		/*quirks*/DA_Q_4K
1200 	},
1201 	{
1202 		/*
1203 		 * Corsair Force 3 SSDs
1204 		 * 4k optimised & trim only works in 4k requests + 4k aligned
1205 		 */
1206 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair Force 3*", "*" },
1207 		/*quirks*/DA_Q_4K
1208 	},
1209         {
1210 		/*
1211 		 * Corsair Neutron GTX SSDs
1212 		 * 4k optimised & trim only works in 4k requests + 4k aligned
1213 		 */
1214 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Neutron GTX*", "*" },
1215 		/*quirks*/DA_Q_4K
1216 	},
1217 	{
1218 		/*
1219 		 * Corsair Force GT & GS SSDs
1220 		 * 4k optimised & trim only works in 4k requests + 4k aligned
1221 		 */
1222 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair Force G*", "*" },
1223 		/*quirks*/DA_Q_4K
1224 	},
1225 	{
1226 		/*
1227 		 * Crucial M4 SSDs
1228 		 * 4k optimised & trim only works in 4k requests + 4k aligned
1229 		 */
1230 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "M4-CT???M4SSD2*", "*" },
1231 		/*quirks*/DA_Q_4K
1232 	},
1233 	{
1234 		/*
1235 		 * Crucial RealSSD C300 SSDs
1236 		 * 4k optimised
1237 		 */
1238 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "C300-CTFDDAC???MAG*",
1239 		"*" }, /*quirks*/DA_Q_4K
1240 	},
1241 	{
1242 		/*
1243 		 * Intel 320 Series SSDs
1244 		 * 4k optimised & trim only works in 4k requests + 4k aligned
1245 		 */
1246 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSA2CW*", "*" },
1247 		/*quirks*/DA_Q_4K
1248 	},
1249 	{
1250 		/*
1251 		 * Intel 330 Series SSDs
1252 		 * 4k optimised & trim only works in 4k requests + 4k aligned
1253 		 */
1254 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2CT*", "*" },
1255 		/*quirks*/DA_Q_4K
1256 	},
1257 	{
1258 		/*
1259 		 * Intel 510 Series SSDs
1260 		 * 4k optimised & trim only works in 4k requests + 4k aligned
1261 		 */
1262 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2MH*", "*" },
1263 		/*quirks*/DA_Q_4K
1264 	},
1265 	{
1266 		/*
1267 		 * Intel 520 Series SSDs
1268 		 * 4k optimised & trim only works in 4k requests + 4k aligned
1269 		 */
1270 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2BW*", "*" },
1271 		/*quirks*/DA_Q_4K
1272 	},
1273 	{
1274 		/*
1275 		 * Intel S3610 Series SSDs
1276 		 * 4k optimised & trim only works in 4k requests + 4k aligned
1277 		 */
1278 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2BX*", "*" },
1279 		/*quirks*/DA_Q_4K
1280 	},
1281 	{
1282 		/*
1283 		 * Intel X25-M Series SSDs
1284 		 * 4k optimised & trim only works in 4k requests + 4k aligned
1285 		 */
1286 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSA2M*", "*" },
1287 		/*quirks*/DA_Q_4K
1288 	},
1289 	{
1290 		/*
1291 		 * Kingston E100 Series SSDs
1292 		 * 4k optimised & trim only works in 4k requests + 4k aligned
1293 		 */
1294 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "KINGSTON SE100S3*", "*" },
1295 		/*quirks*/DA_Q_4K
1296 	},
1297 	{
1298 		/*
1299 		 * Kingston HyperX 3k SSDs
1300 		 * 4k optimised & trim only works in 4k requests + 4k aligned
1301 		 */
1302 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "KINGSTON SH103S3*", "*" },
1303 		/*quirks*/DA_Q_4K
1304 	},
1305 	{
1306 		/*
1307 		 * Marvell SSDs (entry taken from OpenSolaris)
1308 		 * 4k optimised & trim only works in 4k requests + 4k aligned
1309 		 */
1310 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "MARVELL SD88SA02*", "*" },
1311 		/*quirks*/DA_Q_4K
1312 	},
1313 	{
1314 		/*
1315 		 * OCZ Agility 2 SSDs
1316 		 * 4k optimised & trim only works in 4k requests + 4k aligned
1317 		 */
1318 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-AGILITY2*", "*" },
1319 		/*quirks*/DA_Q_4K
1320 	},
1321 	{
1322 		/*
1323 		 * OCZ Agility 3 SSDs
1324 		 * 4k optimised & trim only works in 4k requests + 4k aligned
1325 		 */
1326 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ-AGILITY3*", "*" },
1327 		/*quirks*/DA_Q_4K
1328 	},
1329 	{
1330 		/*
1331 		 * OCZ Deneva R Series SSDs
1332 		 * 4k optimised & trim only works in 4k requests + 4k aligned
1333 		 */
1334 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "DENRSTE251M45*", "*" },
1335 		/*quirks*/DA_Q_4K
1336 	},
1337 	{
1338 		/*
1339 		 * OCZ Vertex 2 SSDs (inc pro series)
1340 		 * 4k optimised & trim only works in 4k requests + 4k aligned
1341 		 */
1342 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ?VERTEX2*", "*" },
1343 		/*quirks*/DA_Q_4K
1344 	},
1345 	{
1346 		/*
1347 		 * OCZ Vertex 3 SSDs
1348 		 * 4k optimised & trim only works in 4k requests + 4k aligned
1349 		 */
1350 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ-VERTEX3*", "*" },
1351 		/*quirks*/DA_Q_4K
1352 	},
1353 	{
1354 		/*
1355 		 * OCZ Vertex 4 SSDs
1356 		 * 4k optimised & trim only works in 4k requests + 4k aligned
1357 		 */
1358 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ-VERTEX4*", "*" },
1359 		/*quirks*/DA_Q_4K
1360 	},
1361 	{
1362 		/*
1363 		 * Samsung 750 Series SSDs
1364 		 * 4k optimised & trim only works in 4k requests + 4k aligned
1365 		 */
1366 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Samsung SSD 750*", "*" },
1367 		/*quirks*/DA_Q_4K
1368 	},
1369 	{
1370 		/*
1371 		 * Samsung 830 Series SSDs
1372 		 * 4k optimised & trim only works in 4k requests + 4k aligned
1373 		 */
1374 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG SSD 830 Series*", "*" },
1375 		/*quirks*/DA_Q_4K
1376 	},
1377 	{
1378 		/*
1379 		 * Samsung 840 SSDs
1380 		 * 4k optimised & trim only works in 4k requests + 4k aligned
1381 		 */
1382 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Samsung SSD 840*", "*" },
1383 		/*quirks*/DA_Q_4K
1384 	},
1385 	{
1386 		/*
1387 		 * Samsung 845 SSDs
1388 		 * 4k optimised & trim only works in 4k requests + 4k aligned
1389 		 */
1390 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Samsung SSD 845*", "*" },
1391 		/*quirks*/DA_Q_4K
1392 	},
1393 	{
1394 		/*
1395 		 * Samsung 850 SSDs
1396 		 * 4k optimised & trim only works in 4k requests + 4k aligned
1397 		 */
1398 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Samsung SSD 850*", "*" },
1399 		/*quirks*/DA_Q_4K
1400 	},
1401 	{
1402 		/*
1403 		 * Samsung 843T Series SSDs (MZ7WD*)
1404 		 * Samsung PM851 Series SSDs (MZ7TE*)
1405 		 * Samsung PM853T Series SSDs (MZ7GE*)
1406 		 * Samsung SM863 Series SSDs (MZ7KM*)
1407 		 * 4k optimised
1408 		 */
1409 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG MZ7*", "*" },
1410 		/*quirks*/DA_Q_4K
1411 	},
1412 	{
1413 		/*
1414 		 * Same as for SAMSUNG MZ7* but enable the quirks for SSD
1415 		 * starting with MZ7* too
1416 		 */
1417 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "MZ7*", "*" },
1418 		/*quirks*/DA_Q_4K
1419 	},
1420 	{
1421 		/*
1422                  * Same as above but enable the quirks for SSD SAMSUNG MZ7*
1423                  * connected via SATA-to-SAS interposer and because of this
1424                  * starting without "ATA"
1425 		 */
1426 		{ T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "MZ7*", "*" },
1427 		/*quirks*/DA_Q_4K
1428 	},
1429 	{
1430 		/*
1431 		 * SuperTalent TeraDrive CT SSDs
1432 		 * 4k optimised & trim only works in 4k requests + 4k aligned
1433 		 */
1434 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "FTM??CT25H*", "*" },
1435 		/*quirks*/DA_Q_4K
1436 	},
1437 	{
1438 		/*
1439 		 * XceedIOPS SATA SSDs
1440 		 * 4k optimised
1441 		 */
1442 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SG9XCS2D*", "*" },
1443 		/*quirks*/DA_Q_4K
1444 	},
1445 	{
1446 		/*
1447 		 * Hama Innostor USB-Stick
1448 		 */
1449 		{ T_DIRECT, SIP_MEDIA_REMOVABLE, "Innostor", "Innostor*", "*" },
1450 		/*quirks*/DA_Q_NO_RC16
1451 	},
1452 	{
1453 		/*
1454 		 * Seagate Lamarr 8TB Shingled Magnetic Recording (SMR)
1455 		 * Drive Managed SATA hard drive.  This drive doesn't report
1456 		 * in firmware that it is a drive managed SMR drive.
1457 		 */
1458 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST8000AS000[23]*", "*" },
1459 		/*quirks*/DA_Q_SMR_DM
1460 	},
1461 	{
1462 		/*
1463 		 * MX-ES USB Drive by Mach Xtreme
1464 		 */
1465 		{ T_DIRECT, SIP_MEDIA_REMOVABLE, "MX", "MXUB3*", "*"},
1466 		/*quirks*/DA_Q_NO_RC16
1467 	},
1468 };
1469 
1470 static	disk_strategy_t	dastrategy;
1471 static	dumper_t	dadump;
1472 static	periph_init_t	dainit;
1473 static	void		daasync(void *callback_arg, u_int32_t code,
1474 				struct cam_path *path, void *arg);
1475 static	void		dasysctlinit(void *context, int pending);
1476 static	int		dasysctlsofttimeout(SYSCTL_HANDLER_ARGS);
1477 static	int		dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
1478 static	int		dadeletemethodsysctl(SYSCTL_HANDLER_ARGS);
1479 static	int		dabitsysctl(SYSCTL_HANDLER_ARGS);
1480 static	int		daflagssysctl(SYSCTL_HANDLER_ARGS);
1481 static	int		dazonemodesysctl(SYSCTL_HANDLER_ARGS);
1482 static	int		dazonesupsysctl(SYSCTL_HANDLER_ARGS);
1483 static	int		dadeletemaxsysctl(SYSCTL_HANDLER_ARGS);
1484 static	void		dadeletemethodset(struct da_softc *softc,
1485 					  da_delete_methods delete_method);
1486 static	off_t		dadeletemaxsize(struct da_softc *softc,
1487 					da_delete_methods delete_method);
1488 static	void		dadeletemethodchoose(struct da_softc *softc,
1489 					     da_delete_methods default_method);
1490 static	void		daprobedone(struct cam_periph *periph, union ccb *ccb);
1491 
1492 static	periph_ctor_t	daregister;
1493 static	periph_dtor_t	dacleanup;
1494 static	periph_start_t	dastart;
1495 static	periph_oninv_t	daoninvalidate;
1496 static	void		dazonedone(struct cam_periph *periph, union ccb *ccb);
1497 static	void		dadone(struct cam_periph *periph,
1498 			       union ccb *done_ccb);
1499 static void		dadone_probewp(struct cam_periph *periph,
1500 				       union ccb *done_ccb);
1501 static void		dadone_proberc(struct cam_periph *periph,
1502 				       union ccb *done_ccb);
1503 static void		dadone_probelbp(struct cam_periph *periph,
1504 					union ccb *done_ccb);
1505 static void		dadone_probeblklimits(struct cam_periph *periph,
1506 					      union ccb *done_ccb);
1507 static void		dadone_probebdc(struct cam_periph *periph,
1508 					union ccb *done_ccb);
1509 static void		dadone_probeata(struct cam_periph *periph,
1510 					union ccb *done_ccb);
1511 static void		dadone_probeatalogdir(struct cam_periph *periph,
1512 					      union ccb *done_ccb);
1513 static void		dadone_probeataiddir(struct cam_periph *periph,
1514 					     union ccb *done_ccb);
1515 static void		dadone_probeatasup(struct cam_periph *periph,
1516 					   union ccb *done_ccb);
1517 static void		dadone_probeatazone(struct cam_periph *periph,
1518 					    union ccb *done_ccb);
1519 static void		dadone_probezone(struct cam_periph *periph,
1520 					 union ccb *done_ccb);
1521 static void		dadone_tur(struct cam_periph *periph,
1522 				   union ccb *done_ccb);
1523 static  int		daerror(union ccb *ccb, u_int32_t cam_flags,
1524 				u_int32_t sense_flags);
1525 static void		daprevent(struct cam_periph *periph, int action);
1526 static void		dareprobe(struct cam_periph *periph);
1527 static void		dasetgeom(struct cam_periph *periph, uint32_t block_len,
1528 				  uint64_t maxsector,
1529 				  struct scsi_read_capacity_data_long *rcaplong,
1530 				  size_t rcap_size);
1531 static callout_func_t	dasendorderedtag;
1532 static void		dashutdown(void *arg, int howto);
1533 static callout_func_t	damediapoll;
1534 
1535 #ifndef	DA_DEFAULT_POLL_PERIOD
1536 #define	DA_DEFAULT_POLL_PERIOD	3
1537 #endif
1538 
1539 #ifndef DA_DEFAULT_TIMEOUT
1540 #define DA_DEFAULT_TIMEOUT 60	/* Timeout in seconds */
1541 #endif
1542 
1543 #ifndef DA_DEFAULT_SOFTTIMEOUT
1544 #define DA_DEFAULT_SOFTTIMEOUT	0
1545 #endif
1546 
1547 #ifndef	DA_DEFAULT_RETRY
1548 #define	DA_DEFAULT_RETRY	4
1549 #endif
1550 
1551 #ifndef	DA_DEFAULT_SEND_ORDERED
1552 #define	DA_DEFAULT_SEND_ORDERED	1
1553 #endif
1554 
1555 static int da_poll_period = DA_DEFAULT_POLL_PERIOD;
1556 static int da_retry_count = DA_DEFAULT_RETRY;
1557 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
1558 static sbintime_t da_default_softtimeout = DA_DEFAULT_SOFTTIMEOUT;
1559 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED;
1560 static int da_disable_wp_detection = 0;
1561 static int da_enable_biospeedup = 1;
1562 static int da_enable_uma_ccbs = 1;
1563 
1564 static SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
1565     "CAM Direct Access Disk driver");
1566 SYSCTL_INT(_kern_cam_da, OID_AUTO, poll_period, CTLFLAG_RWTUN,
1567            &da_poll_period, 0, "Media polling period in seconds");
1568 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RWTUN,
1569            &da_retry_count, 0, "Normal I/O retry count");
1570 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RWTUN,
1571            &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
1572 SYSCTL_INT(_kern_cam_da, OID_AUTO, send_ordered, CTLFLAG_RWTUN,
1573            &da_send_ordered, 0, "Send Ordered Tags");
1574 SYSCTL_INT(_kern_cam_da, OID_AUTO, disable_wp_detection, CTLFLAG_RWTUN,
1575            &da_disable_wp_detection, 0,
1576 	   "Disable detection of write-protected disks");
1577 SYSCTL_INT(_kern_cam_da, OID_AUTO, enable_biospeedup, CTLFLAG_RDTUN,
1578 	    &da_enable_biospeedup, 0, "Enable BIO_SPEEDUP processing");
1579 SYSCTL_INT(_kern_cam_da, OID_AUTO, enable_uma_ccbs, CTLFLAG_RWTUN,
1580 	    &da_enable_uma_ccbs, 0, "Use UMA for CCBs");
1581 
1582 SYSCTL_PROC(_kern_cam_da, OID_AUTO, default_softtimeout,
1583     CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, 0,
1584     dasysctlsofttimeout, "I",
1585     "Soft I/O timeout (ms)");
1586 TUNABLE_INT64("kern.cam.da.default_softtimeout", &da_default_softtimeout);
1587 
1588 /*
1589  * DA_ORDEREDTAG_INTERVAL determines how often, relative
1590  * to the default timeout, we check to see whether an ordered
1591  * tagged transaction is appropriate to prevent simple tag
1592  * starvation.  Since we'd like to ensure that there is at least
1593  * 1/2 of the timeout length left for a starved transaction to
1594  * complete after we've sent an ordered tag, we must poll at least
1595  * four times in every timeout period.  This takes care of the worst
1596  * case where a starved transaction starts during an interval that
1597  * meets the requirement "don't send an ordered tag" test so it takes
1598  * us two intervals to determine that a tag must be sent.
1599  */
1600 #ifndef DA_ORDEREDTAG_INTERVAL
1601 #define DA_ORDEREDTAG_INTERVAL 4
1602 #endif
1603 
1604 static struct periph_driver dadriver =
1605 {
1606 	dainit, "da",
1607 	TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
1608 };
1609 
1610 PERIPHDRIVER_DECLARE(da, dadriver);
1611 
1612 static MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers");
1613 
1614 /*
1615  * This driver takes out references / holds in well defined pairs, never
1616  * recursively. These macros / inline functions enforce those rules. They
1617  * are only enabled with DA_TRACK_REFS or INVARIANTS. If DA_TRACK_REFS is
1618  * defined to be 2 or larger, the tracking also includes debug printfs.
1619  */
1620 #if defined(DA_TRACK_REFS) || defined(INVARIANTS)
1621 
1622 #ifndef DA_TRACK_REFS
1623 #define DA_TRACK_REFS 1
1624 #endif
1625 
1626 #if DA_TRACK_REFS > 1
1627 static const char *da_ref_text[] = {
1628 	"bogus",
1629 	"open",
1630 	"open hold",
1631 	"close hold",
1632 	"reprobe hold",
1633 	"Test Unit Ready",
1634 	"Geom",
1635 	"sysctl",
1636 	"reprobe",
1637 	"max -- also bogus"
1638 };
1639 
1640 #define DA_PERIPH_PRINT(periph, msg, args...)		\
1641 	CAM_PERIPH_PRINT(periph, msg, ##args)
1642 #else
1643 #define DA_PERIPH_PRINT(periph, msg, args...)
1644 #endif
1645 
1646 static inline void
1647 token_sanity(da_ref_token token)
1648 {
1649 	if ((unsigned)token >= DA_REF_MAX)
1650 		panic("Bad token value passed in %d\n", token);
1651 }
1652 
1653 static inline int
1654 da_periph_hold(struct cam_periph *periph, int priority, da_ref_token token)
1655 {
1656 	int err = cam_periph_hold(periph, priority);
1657 
1658 	token_sanity(token);
1659 	DA_PERIPH_PRINT(periph, "Holding device %s (%d): %d\n",
1660 	    da_ref_text[token], token, err);
1661 	if (err == 0) {
1662 		int cnt;
1663 		struct da_softc *softc = periph->softc;
1664 
1665 		cnt = atomic_fetchadd_int(&softc->ref_flags[token], 1);
1666 		if (cnt != 0)
1667 			panic("Re-holding for reason %d, cnt = %d", token, cnt);
1668 	}
1669 	return (err);
1670 }
1671 
1672 static inline void
1673 da_periph_unhold(struct cam_periph *periph, da_ref_token token)
1674 {
1675 	int cnt;
1676 	struct da_softc *softc = periph->softc;
1677 
1678 	token_sanity(token);
1679 	DA_PERIPH_PRINT(periph, "Unholding device %s (%d)\n",
1680 	    da_ref_text[token], token);
1681 	cnt = atomic_fetchadd_int(&softc->ref_flags[token], -1);
1682 	if (cnt != 1)
1683 		panic("Unholding %d with cnt = %d", token, cnt);
1684 	cam_periph_unhold(periph);
1685 }
1686 
1687 static inline int
1688 da_periph_acquire(struct cam_periph *periph, da_ref_token token)
1689 {
1690 	int err = cam_periph_acquire(periph);
1691 
1692 	token_sanity(token);
1693 	DA_PERIPH_PRINT(periph, "acquiring device %s (%d): %d\n",
1694 	    da_ref_text[token], token, err);
1695 	if (err == 0) {
1696 		int cnt;
1697 		struct da_softc *softc = periph->softc;
1698 
1699 		cnt = atomic_fetchadd_int(&softc->ref_flags[token], 1);
1700 		if (cnt != 0)
1701 			panic("Re-refing for reason %d, cnt = %d", token, cnt);
1702 	}
1703 	return (err);
1704 }
1705 
1706 static inline void
1707 da_periph_release(struct cam_periph *periph, da_ref_token token)
1708 {
1709 	int cnt;
1710 	struct da_softc *softc = periph->softc;
1711 
1712 	token_sanity(token);
1713 	DA_PERIPH_PRINT(periph, "releasing device %s (%d)\n",
1714 	    da_ref_text[token], token);
1715 	cnt = atomic_fetchadd_int(&softc->ref_flags[token], -1);
1716 	if (cnt != 1)
1717 		panic("Releasing %d with cnt = %d", token, cnt);
1718 	cam_periph_release(periph);
1719 }
1720 
1721 static inline void
1722 da_periph_release_locked(struct cam_periph *periph, da_ref_token token)
1723 {
1724 	int cnt;
1725 	struct da_softc *softc = periph->softc;
1726 
1727 	token_sanity(token);
1728 	DA_PERIPH_PRINT(periph, "releasing device (locked) %s (%d)\n",
1729 	    da_ref_text[token], token);
1730 	cnt = atomic_fetchadd_int(&softc->ref_flags[token], -1);
1731 	if (cnt != 1)
1732 		panic("releasing (locked) %d with cnt = %d", token, cnt);
1733 	cam_periph_release_locked(periph);
1734 }
1735 
1736 #define cam_periph_hold POISON
1737 #define cam_periph_unhold POISON
1738 #define cam_periph_acquire POISON
1739 #define cam_periph_release POISON
1740 #define cam_periph_release_locked POISON
1741 
1742 #else
1743 #define	da_periph_hold(periph, prio, token)	cam_periph_hold((periph), (prio))
1744 #define da_periph_unhold(periph, token)		cam_periph_unhold((periph))
1745 #define da_periph_acquire(periph, token)	cam_periph_acquire((periph))
1746 #define da_periph_release(periph, token)	cam_periph_release((periph))
1747 #define da_periph_release_locked(periph, token)	cam_periph_release_locked((periph))
1748 #endif
1749 
1750 static int
1751 daopen(struct disk *dp)
1752 {
1753 	struct cam_periph *periph;
1754 	struct da_softc *softc;
1755 	int error;
1756 
1757 	periph = (struct cam_periph *)dp->d_drv1;
1758 	if (da_periph_acquire(periph, DA_REF_OPEN) != 0) {
1759 		return (ENXIO);
1760 	}
1761 
1762 	cam_periph_lock(periph);
1763 	if ((error = da_periph_hold(periph, PRIBIO|PCATCH, DA_REF_OPEN_HOLD)) != 0) {
1764 		cam_periph_unlock(periph);
1765 		da_periph_release(periph, DA_REF_OPEN);
1766 		return (error);
1767 	}
1768 
1769 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1770 	    ("daopen\n"));
1771 
1772 	softc = (struct da_softc *)periph->softc;
1773 	dareprobe(periph);
1774 
1775 	/* Wait for the disk size update.  */
1776 	error = cam_periph_sleep(periph, &softc->disk->d_mediasize, PRIBIO,
1777 	    "dareprobe", 0);
1778 	if (error != 0)
1779 		xpt_print(periph->path, "unable to retrieve capacity data\n");
1780 
1781 	if (periph->flags & CAM_PERIPH_INVALID)
1782 		error = ENXIO;
1783 
1784 	if (error == 0 && (softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
1785 	    (softc->quirks & DA_Q_NO_PREVENT) == 0)
1786 		daprevent(periph, PR_PREVENT);
1787 
1788 	if (error == 0) {
1789 		softc->flags &= ~DA_FLAG_PACK_INVALID;
1790 		softc->flags |= DA_FLAG_OPEN;
1791 	}
1792 
1793 	da_periph_unhold(periph, DA_REF_OPEN_HOLD);
1794 	cam_periph_unlock(periph);
1795 
1796 	if (error != 0)
1797 		da_periph_release(periph, DA_REF_OPEN);
1798 
1799 	return (error);
1800 }
1801 
1802 static int
1803 daclose(struct disk *dp)
1804 {
1805 	struct	cam_periph *periph;
1806 	struct	da_softc *softc;
1807 	union	ccb *ccb;
1808 
1809 	periph = (struct cam_periph *)dp->d_drv1;
1810 	softc = (struct da_softc *)periph->softc;
1811 	cam_periph_lock(periph);
1812 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1813 	    ("daclose\n"));
1814 
1815 	if (da_periph_hold(periph, PRIBIO, DA_REF_CLOSE_HOLD) == 0) {
1816 		/* Flush disk cache. */
1817 		if ((softc->flags & DA_FLAG_DIRTY) != 0 &&
1818 		    (softc->quirks & DA_Q_NO_SYNC_CACHE) == 0 &&
1819 		    (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
1820 			ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1821 			scsi_synchronize_cache(&ccb->csio, /*retries*/1,
1822 			    /*cbfcnp*/NULL, MSG_SIMPLE_Q_TAG,
1823 			    /*begin_lba*/0, /*lb_count*/0, SSD_FULL_SIZE,
1824 			    5 * 60 * 1000);
1825 			cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
1826 			    /*sense_flags*/SF_RETRY_UA | SF_QUIET_IR,
1827 			    softc->disk->d_devstat);
1828 			softc->flags &= ~DA_FLAG_DIRTY;
1829 			xpt_release_ccb(ccb);
1830 		}
1831 
1832 		/* Allow medium removal. */
1833 		if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
1834 		    (softc->quirks & DA_Q_NO_PREVENT) == 0)
1835 			daprevent(periph, PR_ALLOW);
1836 
1837 		da_periph_unhold(periph, DA_REF_CLOSE_HOLD);
1838 	}
1839 
1840 	/*
1841 	 * If we've got removable media, mark the blocksize as
1842 	 * unavailable, since it could change when new media is
1843 	 * inserted.
1844 	 */
1845 	if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0)
1846 		softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
1847 
1848 	softc->flags &= ~DA_FLAG_OPEN;
1849 	while (softc->refcount != 0)
1850 		cam_periph_sleep(periph, &softc->refcount, PRIBIO, "daclose", 1);
1851 	cam_periph_unlock(periph);
1852 	da_periph_release(periph, DA_REF_OPEN);
1853 	return (0);
1854 }
1855 
1856 static void
1857 daschedule(struct cam_periph *periph)
1858 {
1859 	struct da_softc *softc = (struct da_softc *)periph->softc;
1860 
1861 	if (softc->state != DA_STATE_NORMAL)
1862 		return;
1863 
1864 	cam_iosched_schedule(softc->cam_iosched, periph);
1865 }
1866 
1867 /*
1868  * Actually translate the requested transfer into one the physical driver
1869  * can understand.  The transfer is described by a buf and will include
1870  * only one physical transfer.
1871  */
1872 static void
1873 dastrategy(struct bio *bp)
1874 {
1875 	struct cam_periph *periph;
1876 	struct da_softc *softc;
1877 
1878 	periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1879 	softc = (struct da_softc *)periph->softc;
1880 
1881 	cam_periph_lock(periph);
1882 
1883 	/*
1884 	 * If the device has been made invalid, error out
1885 	 */
1886 	if ((softc->flags & DA_FLAG_PACK_INVALID)) {
1887 		cam_periph_unlock(periph);
1888 		biofinish(bp, NULL, ENXIO);
1889 		return;
1890 	}
1891 
1892 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastrategy(%p)\n", bp));
1893 
1894 	/*
1895 	 * Zone commands must be ordered, because they can depend on the
1896 	 * effects of previously issued commands, and they may affect
1897 	 * commands after them.
1898 	 */
1899 	if (bp->bio_cmd == BIO_ZONE)
1900 		bp->bio_flags |= BIO_ORDERED;
1901 
1902 	/*
1903 	 * Place it in the queue of disk activities for this disk
1904 	 */
1905 	cam_iosched_queue_work(softc->cam_iosched, bp);
1906 
1907 	/*
1908 	 * Schedule ourselves for performing the work.
1909 	 */
1910 	daschedule(periph);
1911 	cam_periph_unlock(periph);
1912 
1913 	return;
1914 }
1915 
1916 static int
1917 dadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
1918 {
1919 	struct	    cam_periph *periph;
1920 	struct	    da_softc *softc;
1921 	u_int	    secsize;
1922 	struct	    ccb_scsiio csio;
1923 	struct	    disk *dp;
1924 	int	    error = 0;
1925 
1926 	dp = arg;
1927 	periph = dp->d_drv1;
1928 	softc = (struct da_softc *)periph->softc;
1929 	secsize = softc->params.secsize;
1930 
1931 	if ((softc->flags & DA_FLAG_PACK_INVALID) != 0)
1932 		return (ENXIO);
1933 
1934 	memset(&csio, 0, sizeof(csio));
1935 	if (length > 0) {
1936 		xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1937 		csio.ccb_h.ccb_state = DA_CCB_DUMP;
1938 		scsi_read_write(&csio,
1939 				/*retries*/0,
1940 				/*cbfcnp*/NULL,
1941 				MSG_ORDERED_Q_TAG,
1942 				/*read*/SCSI_RW_WRITE,
1943 				/*byte2*/0,
1944 				/*minimum_cmd_size*/ softc->minimum_cmd_size,
1945 				offset / secsize,
1946 				length / secsize,
1947 				/*data_ptr*/(u_int8_t *) virtual,
1948 				/*dxfer_len*/length,
1949 				/*sense_len*/SSD_FULL_SIZE,
1950 				da_default_timeout * 1000);
1951 		error = cam_periph_runccb((union ccb *)&csio, cam_periph_error,
1952 		    0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1953 		if (error != 0)
1954 			printf("Aborting dump due to I/O error.\n");
1955 		return (error);
1956 	}
1957 
1958 	/*
1959 	 * Sync the disk cache contents to the physical media.
1960 	 */
1961 	if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
1962 		xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1963 		csio.ccb_h.ccb_state = DA_CCB_DUMP;
1964 		scsi_synchronize_cache(&csio,
1965 				       /*retries*/0,
1966 				       /*cbfcnp*/NULL,
1967 				       MSG_SIMPLE_Q_TAG,
1968 				       /*begin_lba*/0,/* Cover the whole disk */
1969 				       /*lb_count*/0,
1970 				       SSD_FULL_SIZE,
1971 				       5 * 1000);
1972 		error = cam_periph_runccb((union ccb *)&csio, cam_periph_error,
1973 		    0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1974 		if (error != 0)
1975 			xpt_print(periph->path, "Synchronize cache failed\n");
1976 	}
1977 	return (error);
1978 }
1979 
1980 static int
1981 dagetattr(struct bio *bp)
1982 {
1983 	int ret;
1984 	struct cam_periph *periph;
1985 
1986 	if (g_handleattr_int(bp, "GEOM::canspeedup", da_enable_biospeedup))
1987 		return (EJUSTRETURN);
1988 
1989 	periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1990 	cam_periph_lock(periph);
1991 	ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
1992 	    periph->path);
1993 	cam_periph_unlock(periph);
1994 	if (ret == 0)
1995 		bp->bio_completed = bp->bio_length;
1996 	return ret;
1997 }
1998 
1999 static void
2000 dainit(void)
2001 {
2002 	cam_status status;
2003 
2004 	da_ccb_zone = uma_zcreate("da_ccb",
2005 	    sizeof(struct ccb_scsiio), NULL, NULL, NULL, NULL,
2006 	    UMA_ALIGN_PTR, 0);
2007 
2008 	/*
2009 	 * Install a global async callback.  This callback will
2010 	 * receive async callbacks like "new device found".
2011 	 */
2012 	status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
2013 
2014 	if (status != CAM_REQ_CMP) {
2015 		printf("da: Failed to attach master async callback "
2016 		       "due to status 0x%x!\n", status);
2017 	} else if (da_send_ordered) {
2018 		/* Register our shutdown event handler */
2019 		if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
2020 					   NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
2021 		    printf("dainit: shutdown event registration failed!\n");
2022 	}
2023 }
2024 
2025 /*
2026  * Callback from GEOM, called when it has finished cleaning up its
2027  * resources.
2028  */
2029 static void
2030 dadiskgonecb(struct disk *dp)
2031 {
2032 	struct cam_periph *periph;
2033 
2034 	periph = (struct cam_periph *)dp->d_drv1;
2035 	da_periph_release(periph, DA_REF_GEOM);
2036 }
2037 
2038 static void
2039 daoninvalidate(struct cam_periph *periph)
2040 {
2041 	struct da_softc *softc;
2042 
2043 	cam_periph_assert(periph, MA_OWNED);
2044 	softc = (struct da_softc *)periph->softc;
2045 
2046 	/*
2047 	 * De-register any async callbacks.
2048 	 */
2049 	xpt_register_async(0, daasync, periph, periph->path);
2050 
2051 	softc->flags |= DA_FLAG_PACK_INVALID;
2052 #ifdef CAM_IO_STATS
2053 	softc->invalidations++;
2054 #endif
2055 
2056 	/*
2057 	 * Return all queued I/O with ENXIO.
2058 	 * XXX Handle any transactions queued to the card
2059 	 *     with XPT_ABORT_CCB.
2060 	 */
2061 	cam_iosched_flush(softc->cam_iosched, NULL, ENXIO);
2062 
2063 	/*
2064 	 * Tell GEOM that we've gone away, we'll get a callback when it is
2065 	 * done cleaning up its resources.
2066 	 */
2067 	disk_gone(softc->disk);
2068 }
2069 
2070 static void
2071 dacleanup(struct cam_periph *periph)
2072 {
2073 	struct da_softc *softc;
2074 
2075 	softc = (struct da_softc *)periph->softc;
2076 
2077 	cam_periph_unlock(periph);
2078 
2079 	cam_iosched_fini(softc->cam_iosched);
2080 
2081 	/*
2082 	 * If we can't free the sysctl tree, oh well...
2083 	 */
2084 	if ((softc->flags & DA_FLAG_SCTX_INIT) != 0) {
2085 #ifdef CAM_IO_STATS
2086 		if (sysctl_ctx_free(&softc->sysctl_stats_ctx) != 0)
2087 			xpt_print(periph->path,
2088 			    "can't remove sysctl stats context\n");
2089 #endif
2090 		if (sysctl_ctx_free(&softc->sysctl_ctx) != 0)
2091 			xpt_print(periph->path,
2092 			    "can't remove sysctl context\n");
2093 	}
2094 
2095 	callout_drain(&softc->mediapoll_c);
2096 	disk_destroy(softc->disk);
2097 	callout_drain(&softc->sendordered_c);
2098 	free(softc, M_DEVBUF);
2099 	cam_periph_lock(periph);
2100 }
2101 
2102 static void
2103 daasync(void *callback_arg, u_int32_t code,
2104 	struct cam_path *path, void *arg)
2105 {
2106 	struct cam_periph *periph;
2107 	struct da_softc *softc;
2108 
2109 	periph = (struct cam_periph *)callback_arg;
2110 	switch (code) {
2111 	case AC_FOUND_DEVICE:	/* callback to create periph, no locking yet */
2112 	{
2113 		struct ccb_getdev *cgd;
2114 		cam_status status;
2115 
2116 		cgd = (struct ccb_getdev *)arg;
2117 		if (cgd == NULL)
2118 			break;
2119 
2120 		if (cgd->protocol != PROTO_SCSI)
2121 			break;
2122 		if (SID_QUAL(&cgd->inq_data) != SID_QUAL_LU_CONNECTED)
2123 			break;
2124 		if (SID_TYPE(&cgd->inq_data) != T_DIRECT
2125 		    && SID_TYPE(&cgd->inq_data) != T_RBC
2126 		    && SID_TYPE(&cgd->inq_data) != T_OPTICAL
2127 		    && SID_TYPE(&cgd->inq_data) != T_ZBC_HM)
2128 			break;
2129 
2130 		/*
2131 		 * Allocate a peripheral instance for
2132 		 * this device and start the probe
2133 		 * process.
2134 		 */
2135 		status = cam_periph_alloc(daregister, daoninvalidate,
2136 					  dacleanup, dastart,
2137 					  "da", CAM_PERIPH_BIO,
2138 					  path, daasync,
2139 					  AC_FOUND_DEVICE, cgd);
2140 
2141 		if (status != CAM_REQ_CMP
2142 		 && status != CAM_REQ_INPROG)
2143 			printf("daasync: Unable to attach to new device "
2144 				"due to status 0x%x\n", status);
2145 		return;
2146 	}
2147 	case AC_ADVINFO_CHANGED:	/* Doesn't touch periph */
2148 	{
2149 		uintptr_t buftype;
2150 
2151 		buftype = (uintptr_t)arg;
2152 		if (buftype == CDAI_TYPE_PHYS_PATH) {
2153 			struct da_softc *softc;
2154 
2155 			softc = periph->softc;
2156 			disk_attr_changed(softc->disk, "GEOM::physpath",
2157 					  M_NOWAIT);
2158 		}
2159 		break;
2160 	}
2161 	case AC_UNIT_ATTENTION:		/* Called for this path: periph locked */
2162 	{
2163 		union ccb *ccb;
2164 		int error_code, sense_key, asc, ascq;
2165 
2166 		softc = (struct da_softc *)periph->softc;
2167 		ccb = (union ccb *)arg;
2168 
2169 		/*
2170 		 * Handle all UNIT ATTENTIONs except our own, as they will be
2171 		 * handled by daerror().
2172 		 */
2173 		if (xpt_path_periph(ccb->ccb_h.path) != periph &&
2174 		    scsi_extract_sense_ccb(ccb,
2175 		     &error_code, &sense_key, &asc, &ascq)) {
2176 			if (asc == 0x2A && ascq == 0x09) {
2177 				xpt_print(ccb->ccb_h.path,
2178 				    "Capacity data has changed\n");
2179 				cam_periph_assert(periph, MA_OWNED);
2180 				softc->flags &= ~DA_FLAG_PROBED;
2181 				dareprobe(periph);
2182 			} else if (asc == 0x28 && ascq == 0x00) {
2183 				cam_periph_assert(periph, MA_OWNED);
2184 				softc->flags &= ~DA_FLAG_PROBED;
2185 				disk_media_changed(softc->disk, M_NOWAIT);
2186 			} else if (asc == 0x3F && ascq == 0x03) {
2187 				xpt_print(ccb->ccb_h.path,
2188 				    "INQUIRY data has changed\n");
2189 				cam_periph_assert(periph, MA_OWNED);
2190 				softc->flags &= ~DA_FLAG_PROBED;
2191 				dareprobe(periph);
2192 			}
2193 		}
2194 		break;
2195 	}
2196 	case AC_SCSI_AEN:		/* Called for this path: periph locked */
2197 		/*
2198 		 * Appears to be currently unused for SCSI devices, only ata SIMs
2199 		 * generate this.
2200 		 */
2201 		cam_periph_assert(periph, MA_OWNED);
2202 		softc = (struct da_softc *)periph->softc;
2203 		if (!cam_iosched_has_work_flags(softc->cam_iosched, DA_WORK_TUR) &&
2204 		    (softc->flags & DA_FLAG_TUR_PENDING) == 0) {
2205 			if (da_periph_acquire(periph, DA_REF_TUR) == 0) {
2206 				cam_iosched_set_work_flags(softc->cam_iosched, DA_WORK_TUR);
2207 				daschedule(periph);
2208 			}
2209 		}
2210 		/* FALLTHROUGH */
2211 	case AC_SENT_BDR:		/* Called for this path: periph locked */
2212 	case AC_BUS_RESET:		/* Called for this path: periph locked */
2213 	{
2214 		struct ccb_hdr *ccbh;
2215 
2216 		cam_periph_assert(periph, MA_OWNED);
2217 		softc = (struct da_softc *)periph->softc;
2218 		/*
2219 		 * Don't fail on the expected unit attention
2220 		 * that will occur.
2221 		 */
2222 		softc->flags |= DA_FLAG_RETRY_UA;
2223 		LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
2224 			ccbh->ccb_state |= DA_CCB_RETRY_UA;
2225 		break;
2226 	}
2227 	case AC_INQ_CHANGED:		/* Called for this path: periph locked */
2228 		cam_periph_assert(periph, MA_OWNED);
2229 		softc = (struct da_softc *)periph->softc;
2230 		softc->flags &= ~DA_FLAG_PROBED;
2231 		dareprobe(periph);
2232 		break;
2233 	default:
2234 		break;
2235 	}
2236 	cam_periph_async(periph, code, path, arg);
2237 }
2238 
2239 static void
2240 dasysctlinit(void *context, int pending)
2241 {
2242 	struct cam_periph *periph;
2243 	struct da_softc *softc;
2244 	char tmpstr[32], tmpstr2[16];
2245 	struct ccb_trans_settings cts;
2246 
2247 	periph = (struct cam_periph *)context;
2248 	/*
2249 	 * periph was held for us when this task was enqueued
2250 	 */
2251 	if (periph->flags & CAM_PERIPH_INVALID) {
2252 		da_periph_release(periph, DA_REF_SYSCTL);
2253 		return;
2254 	}
2255 
2256 	softc = (struct da_softc *)periph->softc;
2257 	snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
2258 	snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
2259 
2260 	sysctl_ctx_init(&softc->sysctl_ctx);
2261 	cam_periph_lock(periph);
2262 	softc->flags |= DA_FLAG_SCTX_INIT;
2263 	cam_periph_unlock(periph);
2264 	softc->sysctl_tree = SYSCTL_ADD_NODE_WITH_LABEL(&softc->sysctl_ctx,
2265 		SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
2266 		CTLFLAG_RD | CTLFLAG_MPSAFE, 0, tmpstr, "device_index");
2267 	if (softc->sysctl_tree == NULL) {
2268 		printf("dasysctlinit: unable to allocate sysctl tree\n");
2269 		da_periph_release(periph, DA_REF_SYSCTL);
2270 		return;
2271 	}
2272 
2273 	/*
2274 	 * Now register the sysctl handler, so the user can change the value on
2275 	 * the fly.
2276 	 */
2277 	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2278 		OID_AUTO, "delete_method",
2279 		CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
2280 		softc, 0, dadeletemethodsysctl, "A",
2281 		"BIO_DELETE execution method");
2282 	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2283 		OID_AUTO, "delete_max",
2284 		CTLTYPE_U64 | CTLFLAG_RW | CTLFLAG_MPSAFE,
2285 		softc, 0, dadeletemaxsysctl, "Q",
2286 		"Maximum BIO_DELETE size");
2287 	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2288 		OID_AUTO, "minimum_cmd_size",
2289 		CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
2290 		&softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
2291 		"Minimum CDB size");
2292 	SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
2293 		SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
2294 		"trim_count", CTLFLAG_RD, &softc->trim_count,
2295 		"Total number of unmap/dsm commands sent");
2296 	SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
2297 		SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
2298 		"trim_ranges", CTLFLAG_RD, &softc->trim_ranges,
2299 		"Total number of ranges in unmap/dsm commands");
2300 	SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
2301 		SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
2302 		"trim_lbas", CTLFLAG_RD, &softc->trim_lbas,
2303 		"Total lbas in the unmap/dsm commands sent");
2304 
2305 	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2306 		OID_AUTO, "zone_mode",
2307 		CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
2308 		softc, 0, dazonemodesysctl, "A",
2309 		"Zone Mode");
2310 	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2311 		OID_AUTO, "zone_support",
2312 		CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
2313 		softc, 0, dazonesupsysctl, "A",
2314 		"Zone Support");
2315 	SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
2316 		SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
2317 		"optimal_seq_zones", CTLFLAG_RD, &softc->optimal_seq_zones,
2318 		"Optimal Number of Open Sequential Write Preferred Zones");
2319 	SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
2320 		SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
2321 		"optimal_nonseq_zones", CTLFLAG_RD,
2322 		&softc->optimal_nonseq_zones,
2323 		"Optimal Number of Non-Sequentially Written Sequential Write "
2324 		"Preferred Zones");
2325 	SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
2326 		SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
2327 		"max_seq_zones", CTLFLAG_RD, &softc->max_seq_zones,
2328 		"Maximum Number of Open Sequential Write Required Zones");
2329 
2330 	SYSCTL_ADD_INT(&softc->sysctl_ctx,
2331 		       SYSCTL_CHILDREN(softc->sysctl_tree),
2332 		       OID_AUTO,
2333 		       "error_inject",
2334 		       CTLFLAG_RW,
2335 		       &softc->error_inject,
2336 		       0,
2337 		       "error_inject leaf");
2338 
2339 	SYSCTL_ADD_INT(&softc->sysctl_ctx,
2340 		       SYSCTL_CHILDREN(softc->sysctl_tree),
2341 		       OID_AUTO,
2342 		       "p_type",
2343 		       CTLFLAG_RD,
2344 		       &softc->p_type,
2345 		       0,
2346 		       "DIF protection type");
2347 
2348 	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2349 	    OID_AUTO, "flags", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
2350 	    softc, 0, daflagssysctl, "A",
2351 	    "Flags for drive");
2352 	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2353 	    OID_AUTO, "rotating", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
2354 	    &softc->flags, (u_int)DA_FLAG_ROTATING, dabitsysctl, "I",
2355 	    "Rotating media *DEPRECATED* gone in FreeBSD 14");
2356 	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2357 	    OID_AUTO, "unmapped_io", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
2358 	    &softc->flags, (u_int)DA_FLAG_UNMAPPEDIO, dabitsysctl, "I",
2359 	    "Unmapped I/O support *DEPRECATED* gone in FreeBSD 14");
2360 
2361 #ifdef CAM_TEST_FAILURE
2362 	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2363 		OID_AUTO, "invalidate", CTLTYPE_U64 | CTLFLAG_RW | CTLFLAG_MPSAFE,
2364 		periph, 0, cam_periph_invalidate_sysctl, "I",
2365 		"Write 1 to invalidate the drive immediately");
2366 #endif
2367 
2368 	/*
2369 	 * Add some addressing info.
2370 	 */
2371 	memset(&cts, 0, sizeof (cts));
2372 	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
2373 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
2374 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
2375 	cam_periph_lock(periph);
2376 	xpt_action((union ccb *)&cts);
2377 	cam_periph_unlock(periph);
2378 	if (cts.ccb_h.status != CAM_REQ_CMP) {
2379 		da_periph_release(periph, DA_REF_SYSCTL);
2380 		return;
2381 	}
2382 	if (cts.protocol == PROTO_SCSI && cts.transport == XPORT_FC) {
2383 		struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
2384 		if (fc->valid & CTS_FC_VALID_WWPN) {
2385 			softc->wwpn = fc->wwpn;
2386 			SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
2387 			    SYSCTL_CHILDREN(softc->sysctl_tree),
2388 			    OID_AUTO, "wwpn", CTLFLAG_RD,
2389 			    &softc->wwpn, "World Wide Port Name");
2390 		}
2391 	}
2392 
2393 #ifdef CAM_IO_STATS
2394 	/*
2395 	 * Now add some useful stats.
2396 	 * XXX These should live in cam_periph and be common to all periphs
2397 	 */
2398 	softc->sysctl_stats_tree = SYSCTL_ADD_NODE(&softc->sysctl_stats_ctx,
2399 	    SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, "stats",
2400 	    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "Statistics");
2401 	SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
2402 		       SYSCTL_CHILDREN(softc->sysctl_stats_tree),
2403 		       OID_AUTO,
2404 		       "errors",
2405 		       CTLFLAG_RD,
2406 		       &softc->errors,
2407 		       0,
2408 		       "Transport errors reported by the SIM");
2409 	SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
2410 		       SYSCTL_CHILDREN(softc->sysctl_stats_tree),
2411 		       OID_AUTO,
2412 		       "timeouts",
2413 		       CTLFLAG_RD,
2414 		       &softc->timeouts,
2415 		       0,
2416 		       "Device timeouts reported by the SIM");
2417 	SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
2418 		       SYSCTL_CHILDREN(softc->sysctl_stats_tree),
2419 		       OID_AUTO,
2420 		       "pack_invalidations",
2421 		       CTLFLAG_RD,
2422 		       &softc->invalidations,
2423 		       0,
2424 		       "Device pack invalidations");
2425 #endif
2426 
2427 	cam_iosched_sysctl_init(softc->cam_iosched, &softc->sysctl_ctx,
2428 	    softc->sysctl_tree);
2429 
2430 	da_periph_release(periph, DA_REF_SYSCTL);
2431 }
2432 
2433 static int
2434 dadeletemaxsysctl(SYSCTL_HANDLER_ARGS)
2435 {
2436 	int error;
2437 	uint64_t value;
2438 	struct da_softc *softc;
2439 
2440 	softc = (struct da_softc *)arg1;
2441 
2442 	value = softc->disk->d_delmaxsize;
2443 	error = sysctl_handle_64(oidp, &value, 0, req);
2444 	if ((error != 0) || (req->newptr == NULL))
2445 		return (error);
2446 
2447 	/* only accept values smaller than the calculated value */
2448 	if (value > dadeletemaxsize(softc, softc->delete_method)) {
2449 		return (EINVAL);
2450 	}
2451 	softc->disk->d_delmaxsize = value;
2452 
2453 	return (0);
2454 }
2455 
2456 static int
2457 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
2458 {
2459 	int error, value;
2460 
2461 	value = *(int *)arg1;
2462 
2463 	error = sysctl_handle_int(oidp, &value, 0, req);
2464 
2465 	if ((error != 0)
2466 	 || (req->newptr == NULL))
2467 		return (error);
2468 
2469 	/*
2470 	 * Acceptable values here are 6, 10, 12 or 16.
2471 	 */
2472 	if (value < 6)
2473 		value = 6;
2474 	else if ((value > 6)
2475 	      && (value <= 10))
2476 		value = 10;
2477 	else if ((value > 10)
2478 	      && (value <= 12))
2479 		value = 12;
2480 	else if (value > 12)
2481 		value = 16;
2482 
2483 	*(int *)arg1 = value;
2484 
2485 	return (0);
2486 }
2487 
2488 static int
2489 dasysctlsofttimeout(SYSCTL_HANDLER_ARGS)
2490 {
2491 	sbintime_t value;
2492 	int error;
2493 
2494 	value = da_default_softtimeout / SBT_1MS;
2495 
2496 	error = sysctl_handle_int(oidp, (int *)&value, 0, req);
2497 	if ((error != 0) || (req->newptr == NULL))
2498 		return (error);
2499 
2500 	/* XXX Should clip this to a reasonable level */
2501 	if (value > da_default_timeout * 1000)
2502 		return (EINVAL);
2503 
2504 	da_default_softtimeout = value * SBT_1MS;
2505 	return (0);
2506 }
2507 
2508 static void
2509 dadeletemethodset(struct da_softc *softc, da_delete_methods delete_method)
2510 {
2511 
2512 	softc->delete_method = delete_method;
2513 	softc->disk->d_delmaxsize = dadeletemaxsize(softc, delete_method);
2514 	softc->delete_func = da_delete_functions[delete_method];
2515 
2516 	if (softc->delete_method > DA_DELETE_DISABLE)
2517 		softc->disk->d_flags |= DISKFLAG_CANDELETE;
2518 	else
2519 		softc->disk->d_flags &= ~DISKFLAG_CANDELETE;
2520 }
2521 
2522 static off_t
2523 dadeletemaxsize(struct da_softc *softc, da_delete_methods delete_method)
2524 {
2525 	off_t sectors;
2526 
2527 	switch(delete_method) {
2528 	case DA_DELETE_UNMAP:
2529 		sectors = (off_t)softc->unmap_max_lba;
2530 		break;
2531 	case DA_DELETE_ATA_TRIM:
2532 		sectors = (off_t)ATA_DSM_RANGE_MAX * softc->trim_max_ranges;
2533 		break;
2534 	case DA_DELETE_WS16:
2535 		sectors = omin(softc->ws_max_blks, WS16_MAX_BLKS);
2536 		break;
2537 	case DA_DELETE_ZERO:
2538 	case DA_DELETE_WS10:
2539 		sectors = omin(softc->ws_max_blks, WS10_MAX_BLKS);
2540 		break;
2541 	default:
2542 		return 0;
2543 	}
2544 
2545 	return (off_t)softc->params.secsize *
2546 	    omin(sectors, softc->params.sectors);
2547 }
2548 
2549 static void
2550 daprobedone(struct cam_periph *periph, union ccb *ccb)
2551 {
2552 	struct da_softc *softc;
2553 
2554 	softc = (struct da_softc *)periph->softc;
2555 
2556 	cam_periph_assert(periph, MA_OWNED);
2557 
2558 	dadeletemethodchoose(softc, DA_DELETE_NONE);
2559 
2560 	if (bootverbose && (softc->flags & DA_FLAG_ANNOUNCED) == 0) {
2561 		char buf[80];
2562 		int i, sep;
2563 
2564 		snprintf(buf, sizeof(buf), "Delete methods: <");
2565 		sep = 0;
2566 		for (i = 0; i <= DA_DELETE_MAX; i++) {
2567 			if ((softc->delete_available & (1 << i)) == 0 &&
2568 			    i != softc->delete_method)
2569 				continue;
2570 			if (sep)
2571 				strlcat(buf, ",", sizeof(buf));
2572 			strlcat(buf, da_delete_method_names[i],
2573 			    sizeof(buf));
2574 			if (i == softc->delete_method)
2575 				strlcat(buf, "(*)", sizeof(buf));
2576 			sep = 1;
2577 		}
2578 		strlcat(buf, ">", sizeof(buf));
2579 		printf("%s%d: %s\n", periph->periph_name,
2580 		    periph->unit_number, buf);
2581 	}
2582 	if ((softc->disk->d_flags & DISKFLAG_WRITE_PROTECT) != 0 &&
2583 	    (softc->flags & DA_FLAG_ANNOUNCED) == 0) {
2584 		printf("%s%d: Write Protected\n", periph->periph_name,
2585 		    periph->unit_number);
2586 	}
2587 
2588 	/*
2589 	 * Since our peripheral may be invalidated by an error
2590 	 * above or an external event, we must release our CCB
2591 	 * before releasing the probe lock on the peripheral.
2592 	 * The peripheral will only go away once the last lock
2593 	 * is removed, and we need it around for the CCB release
2594 	 * operation.
2595 	 */
2596 	xpt_release_ccb(ccb);
2597 	softc->state = DA_STATE_NORMAL;
2598 	softc->flags |= DA_FLAG_PROBED;
2599 	daschedule(periph);
2600 	wakeup(&softc->disk->d_mediasize);
2601 	if ((softc->flags & DA_FLAG_ANNOUNCED) == 0) {
2602 		softc->flags |= DA_FLAG_ANNOUNCED;
2603 		da_periph_unhold(periph, DA_REF_PROBE_HOLD);
2604 	} else
2605 		da_periph_release_locked(periph, DA_REF_REPROBE);
2606 }
2607 
2608 static void
2609 dadeletemethodchoose(struct da_softc *softc, da_delete_methods default_method)
2610 {
2611 	int i, methods;
2612 
2613 	/* If available, prefer the method requested by user. */
2614 	i = softc->delete_method_pref;
2615 	methods = softc->delete_available | (1 << DA_DELETE_DISABLE);
2616 	if (methods & (1 << i)) {
2617 		dadeletemethodset(softc, i);
2618 		return;
2619 	}
2620 
2621 	/* Use the pre-defined order to choose the best performing delete. */
2622 	for (i = DA_DELETE_MIN; i <= DA_DELETE_MAX; i++) {
2623 		if (i == DA_DELETE_ZERO)
2624 			continue;
2625 		if (softc->delete_available & (1 << i)) {
2626 			dadeletemethodset(softc, i);
2627 			return;
2628 		}
2629 	}
2630 
2631 	/* Fallback to default. */
2632 	dadeletemethodset(softc, default_method);
2633 }
2634 
2635 static int
2636 dabitsysctl(SYSCTL_HANDLER_ARGS)
2637 {
2638 	u_int *flags = arg1;
2639 	u_int test = arg2;
2640 	int tmpout, error;
2641 
2642 	tmpout = !!(*flags & test);
2643 	error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
2644 	if (error || !req->newptr)
2645 		return (error);
2646 
2647 	return (EPERM);
2648 }
2649 
2650 static int
2651 daflagssysctl(SYSCTL_HANDLER_ARGS)
2652 {
2653 	struct sbuf sbuf;
2654 	struct da_softc *softc = arg1;
2655 	int error;
2656 
2657 	sbuf_new_for_sysctl(&sbuf, NULL, 0, req);
2658 	if (softc->flags != 0)
2659 		sbuf_printf(&sbuf, "0x%b", (unsigned)softc->flags, DA_FLAG_STRING);
2660 	else
2661 		sbuf_printf(&sbuf, "0");
2662 	error = sbuf_finish(&sbuf);
2663 	sbuf_delete(&sbuf);
2664 
2665 	return (error);
2666 }
2667 
2668 static int
2669 dadeletemethodsysctl(SYSCTL_HANDLER_ARGS)
2670 {
2671 	char buf[16];
2672 	const char *p;
2673 	struct da_softc *softc;
2674 	int i, error, value;
2675 
2676 	softc = (struct da_softc *)arg1;
2677 
2678 	value = softc->delete_method;
2679 	if (value < 0 || value > DA_DELETE_MAX)
2680 		p = "UNKNOWN";
2681 	else
2682 		p = da_delete_method_names[value];
2683 	strncpy(buf, p, sizeof(buf));
2684 	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
2685 	if (error != 0 || req->newptr == NULL)
2686 		return (error);
2687 	for (i = 0; i <= DA_DELETE_MAX; i++) {
2688 		if (strcmp(buf, da_delete_method_names[i]) == 0)
2689 			break;
2690 	}
2691 	if (i > DA_DELETE_MAX)
2692 		return (EINVAL);
2693 	softc->delete_method_pref = i;
2694 	dadeletemethodchoose(softc, DA_DELETE_NONE);
2695 	return (0);
2696 }
2697 
2698 static int
2699 dazonemodesysctl(SYSCTL_HANDLER_ARGS)
2700 {
2701 	char tmpbuf[40];
2702 	struct da_softc *softc;
2703 	int error;
2704 
2705 	softc = (struct da_softc *)arg1;
2706 
2707 	switch (softc->zone_mode) {
2708 	case DA_ZONE_DRIVE_MANAGED:
2709 		snprintf(tmpbuf, sizeof(tmpbuf), "Drive Managed");
2710 		break;
2711 	case DA_ZONE_HOST_AWARE:
2712 		snprintf(tmpbuf, sizeof(tmpbuf), "Host Aware");
2713 		break;
2714 	case DA_ZONE_HOST_MANAGED:
2715 		snprintf(tmpbuf, sizeof(tmpbuf), "Host Managed");
2716 		break;
2717 	case DA_ZONE_NONE:
2718 	default:
2719 		snprintf(tmpbuf, sizeof(tmpbuf), "Not Zoned");
2720 		break;
2721 	}
2722 
2723 	error = sysctl_handle_string(oidp, tmpbuf, sizeof(tmpbuf), req);
2724 
2725 	return (error);
2726 }
2727 
2728 static int
2729 dazonesupsysctl(SYSCTL_HANDLER_ARGS)
2730 {
2731 	char tmpbuf[180];
2732 	struct da_softc *softc;
2733 	struct sbuf sb;
2734 	int error, first;
2735 	unsigned int i;
2736 
2737 	softc = (struct da_softc *)arg1;
2738 
2739 	error = 0;
2740 	first = 1;
2741 	sbuf_new(&sb, tmpbuf, sizeof(tmpbuf), 0);
2742 
2743 	for (i = 0; i < sizeof(da_zone_desc_table) /
2744 	     sizeof(da_zone_desc_table[0]); i++) {
2745 		if (softc->zone_flags & da_zone_desc_table[i].value) {
2746 			if (first == 0)
2747 				sbuf_printf(&sb, ", ");
2748 			else
2749 				first = 0;
2750 			sbuf_cat(&sb, da_zone_desc_table[i].desc);
2751 		}
2752 	}
2753 
2754 	if (first == 1)
2755 		sbuf_printf(&sb, "None");
2756 
2757 	sbuf_finish(&sb);
2758 
2759 	error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
2760 
2761 	return (error);
2762 }
2763 
2764 static cam_status
2765 daregister(struct cam_periph *periph, void *arg)
2766 {
2767 	struct da_softc *softc;
2768 	struct ccb_pathinq cpi;
2769 	struct ccb_getdev *cgd;
2770 	char tmpstr[80];
2771 	caddr_t match;
2772 	int quirks;
2773 
2774 	cgd = (struct ccb_getdev *)arg;
2775 	if (cgd == NULL) {
2776 		printf("daregister: no getdev CCB, can't register device\n");
2777 		return(CAM_REQ_CMP_ERR);
2778 	}
2779 
2780 	softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF,
2781 	    M_NOWAIT|M_ZERO);
2782 
2783 	if (softc == NULL) {
2784 		printf("daregister: Unable to probe new device. "
2785 		       "Unable to allocate softc\n");
2786 		return(CAM_REQ_CMP_ERR);
2787 	}
2788 
2789 	if (cam_iosched_init(&softc->cam_iosched, periph) != 0) {
2790 		printf("daregister: Unable to probe new device. "
2791 		       "Unable to allocate iosched memory\n");
2792 		free(softc, M_DEVBUF);
2793 		return(CAM_REQ_CMP_ERR);
2794 	}
2795 
2796 	LIST_INIT(&softc->pending_ccbs);
2797 	softc->state = DA_STATE_PROBE_WP;
2798 	bioq_init(&softc->delete_run_queue);
2799 	if (SID_IS_REMOVABLE(&cgd->inq_data))
2800 		softc->flags |= DA_FLAG_PACK_REMOVABLE;
2801 	softc->unmap_max_ranges = UNMAP_MAX_RANGES;
2802 	softc->unmap_max_lba = UNMAP_RANGE_MAX;
2803 	softc->unmap_gran = 0;
2804 	softc->unmap_gran_align = 0;
2805 	softc->ws_max_blks = WS16_MAX_BLKS;
2806 	softc->trim_max_ranges = ATA_TRIM_MAX_RANGES;
2807 	softc->flags |= DA_FLAG_ROTATING;
2808 
2809 	periph->softc = softc;
2810 
2811 	/*
2812 	 * See if this device has any quirks.
2813 	 */
2814 	match = cam_quirkmatch((caddr_t)&cgd->inq_data,
2815 			       (caddr_t)da_quirk_table,
2816 			       nitems(da_quirk_table),
2817 			       sizeof(*da_quirk_table), scsi_inquiry_match);
2818 
2819 	if (match != NULL)
2820 		softc->quirks = ((struct da_quirk_entry *)match)->quirks;
2821 	else
2822 		softc->quirks = DA_Q_NONE;
2823 
2824 	/* Check if the SIM does not want 6 byte commands */
2825 	xpt_path_inq(&cpi, periph->path);
2826 	if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
2827 		softc->quirks |= DA_Q_NO_6_BYTE;
2828 
2829 	/* Override quirks if tunable is set */
2830 	snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.quirks",
2831 		 periph->unit_number);
2832 	quirks = softc->quirks;
2833 	TUNABLE_INT_FETCH(tmpstr, &quirks);
2834 	softc->quirks = quirks;
2835 
2836 	if (SID_TYPE(&cgd->inq_data) == T_ZBC_HM)
2837 		softc->zone_mode = DA_ZONE_HOST_MANAGED;
2838 	else if (softc->quirks & DA_Q_SMR_DM)
2839 		softc->zone_mode = DA_ZONE_DRIVE_MANAGED;
2840 	else
2841 		softc->zone_mode = DA_ZONE_NONE;
2842 
2843 	if (softc->zone_mode != DA_ZONE_NONE) {
2844 		if (scsi_vpd_supported_page(periph, SVPD_ATA_INFORMATION)) {
2845 			if (scsi_vpd_supported_page(periph, SVPD_ZONED_BDC))
2846 				softc->zone_interface = DA_ZONE_IF_ATA_SAT;
2847 			else
2848 				softc->zone_interface = DA_ZONE_IF_ATA_PASS;
2849 		} else
2850 			softc->zone_interface = DA_ZONE_IF_SCSI;
2851 	}
2852 
2853 	TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
2854 
2855 	/*
2856 	 * Let XPT know we can use UMA-allocated CCBs.
2857 	 */
2858 	if (da_enable_uma_ccbs) {
2859 		KASSERT(da_ccb_zone != NULL,
2860 		    ("%s: NULL da_ccb_zone", __func__));
2861 		periph->ccb_zone = da_ccb_zone;
2862 	}
2863 
2864 	/*
2865 	 * Take an exclusive section lock on the periph while dastart is called
2866 	 * to finish the probe.  The lock will be dropped in dadone at the end
2867 	 * of probe. This locks out daopen and daclose from racing with the
2868 	 * probe.
2869 	 *
2870 	 * XXX if cam_periph_hold returns an error, we don't hold a refcount.
2871 	 */
2872 	(void)da_periph_hold(periph, PRIBIO, DA_REF_PROBE_HOLD);
2873 
2874 	/*
2875 	 * Schedule a periodic event to occasionally send an
2876 	 * ordered tag to a device.
2877 	 */
2878 	callout_init_mtx(&softc->sendordered_c, cam_periph_mtx(periph), 0);
2879 	callout_reset(&softc->sendordered_c,
2880 	    (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
2881 	    dasendorderedtag, periph);
2882 
2883 	cam_periph_unlock(periph);
2884 	/*
2885 	 * RBC devices don't have to support READ(6), only READ(10).
2886 	 */
2887 	if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
2888 		softc->minimum_cmd_size = 10;
2889 	else
2890 		softc->minimum_cmd_size = 6;
2891 
2892 	/*
2893 	 * Load the user's default, if any.
2894 	 */
2895 	snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
2896 		 periph->unit_number);
2897 	TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
2898 
2899 	/*
2900 	 * 6, 10, 12 and 16 are the currently permissible values.
2901 	 */
2902 	if (softc->minimum_cmd_size > 12)
2903 		softc->minimum_cmd_size = 16;
2904 	else if (softc->minimum_cmd_size > 10)
2905 		softc->minimum_cmd_size = 12;
2906 	else if (softc->minimum_cmd_size > 6)
2907 		softc->minimum_cmd_size = 10;
2908 	else
2909 		softc->minimum_cmd_size = 6;
2910 
2911 	/* Predict whether device may support READ CAPACITY(16). */
2912 	if (SID_ANSI_REV(&cgd->inq_data) >= SCSI_REV_SPC3 &&
2913 	    (softc->quirks & DA_Q_NO_RC16) == 0) {
2914 		softc->flags |= DA_FLAG_CAN_RC16;
2915 	}
2916 
2917 	/*
2918 	 * Register this media as a disk.
2919 	 */
2920 	softc->disk = disk_alloc();
2921 	softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
2922 			  periph->unit_number, 0,
2923 			  DEVSTAT_BS_UNAVAILABLE,
2924 			  SID_TYPE(&cgd->inq_data) |
2925 			  XPORT_DEVSTAT_TYPE(cpi.transport),
2926 			  DEVSTAT_PRIORITY_DISK);
2927 	softc->disk->d_open = daopen;
2928 	softc->disk->d_close = daclose;
2929 	softc->disk->d_strategy = dastrategy;
2930 	if (cam_sim_pollable(periph->sim))
2931 		softc->disk->d_dump = dadump;
2932 	softc->disk->d_getattr = dagetattr;
2933 	softc->disk->d_gone = dadiskgonecb;
2934 	softc->disk->d_name = "da";
2935 	softc->disk->d_drv1 = periph;
2936 	if (cpi.maxio == 0)
2937 		softc->maxio = DFLTPHYS;	/* traditional default */
2938 	else if (cpi.maxio > maxphys)
2939 		softc->maxio = maxphys;		/* for safety */
2940 	else
2941 		softc->maxio = cpi.maxio;
2942 	if (softc->quirks & DA_Q_128KB)
2943 		softc->maxio = min(softc->maxio, 128 * 1024);
2944 	softc->disk->d_maxsize = softc->maxio;
2945 	softc->disk->d_unit = periph->unit_number;
2946 	softc->disk->d_flags = DISKFLAG_DIRECT_COMPLETION | DISKFLAG_CANZONE;
2947 	if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0)
2948 		softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
2949 	if ((cpi.hba_misc & PIM_UNMAPPED) != 0) {
2950 		softc->flags |= DA_FLAG_UNMAPPEDIO;
2951 		softc->disk->d_flags |= DISKFLAG_UNMAPPED_BIO;
2952 	}
2953 	cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
2954 	    sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
2955 	strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
2956 	cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
2957 	    cgd->inq_data.product, sizeof(cgd->inq_data.product),
2958 	    sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
2959 	softc->disk->d_hba_vendor = cpi.hba_vendor;
2960 	softc->disk->d_hba_device = cpi.hba_device;
2961 	softc->disk->d_hba_subvendor = cpi.hba_subvendor;
2962 	softc->disk->d_hba_subdevice = cpi.hba_subdevice;
2963 	snprintf(softc->disk->d_attachment, sizeof(softc->disk->d_attachment),
2964 	    "%s%d", cpi.dev_name, cpi.unit_number);
2965 
2966 	/*
2967 	 * Acquire a reference to the periph before we register with GEOM.
2968 	 * We'll release this reference once GEOM calls us back (via
2969 	 * dadiskgonecb()) telling us that our provider has been freed.
2970 	 */
2971 	if (da_periph_acquire(periph, DA_REF_GEOM) != 0) {
2972 		xpt_print(periph->path, "%s: lost periph during "
2973 			  "registration!\n", __func__);
2974 		cam_periph_lock(periph);
2975 		return (CAM_REQ_CMP_ERR);
2976 	}
2977 
2978 	disk_create(softc->disk, DISK_VERSION);
2979 	cam_periph_lock(periph);
2980 
2981 	/*
2982 	 * Add async callbacks for events of interest.
2983 	 * I don't bother checking if this fails as,
2984 	 * in most cases, the system will function just
2985 	 * fine without them and the only alternative
2986 	 * would be to not attach the device on failure.
2987 	 */
2988 	xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
2989 	    AC_ADVINFO_CHANGED | AC_SCSI_AEN | AC_UNIT_ATTENTION |
2990 	    AC_INQ_CHANGED, daasync, periph, periph->path);
2991 
2992 	/*
2993 	 * Emit an attribute changed notification just in case
2994 	 * physical path information arrived before our async
2995 	 * event handler was registered, but after anyone attaching
2996 	 * to our disk device polled it.
2997 	 */
2998 	disk_attr_changed(softc->disk, "GEOM::physpath", M_NOWAIT);
2999 
3000 	/*
3001 	 * Schedule a periodic media polling events.
3002 	 */
3003 	callout_init_mtx(&softc->mediapoll_c, cam_periph_mtx(periph), 0);
3004 	if ((softc->flags & DA_FLAG_PACK_REMOVABLE) &&
3005 	    (cgd->inq_flags & SID_AEN) == 0 &&
3006 	    da_poll_period != 0)
3007 		callout_reset(&softc->mediapoll_c, da_poll_period * hz,
3008 		    damediapoll, periph);
3009 
3010 	xpt_schedule(periph, CAM_PRIORITY_DEV);
3011 
3012 	return(CAM_REQ_CMP);
3013 }
3014 
3015 static int
3016 da_zone_bio_to_scsi(int disk_zone_cmd)
3017 {
3018 	switch (disk_zone_cmd) {
3019 	case DISK_ZONE_OPEN:
3020 		return ZBC_OUT_SA_OPEN;
3021 	case DISK_ZONE_CLOSE:
3022 		return ZBC_OUT_SA_CLOSE;
3023 	case DISK_ZONE_FINISH:
3024 		return ZBC_OUT_SA_FINISH;
3025 	case DISK_ZONE_RWP:
3026 		return ZBC_OUT_SA_RWP;
3027 	}
3028 
3029 	return -1;
3030 }
3031 
3032 static int
3033 da_zone_cmd(struct cam_periph *periph, union ccb *ccb, struct bio *bp,
3034 	    int *queue_ccb)
3035 {
3036 	struct da_softc *softc;
3037 	int error;
3038 
3039 	error = 0;
3040 
3041 	if (bp->bio_cmd != BIO_ZONE) {
3042 		error = EINVAL;
3043 		goto bailout;
3044 	}
3045 
3046 	softc = periph->softc;
3047 
3048 	switch (bp->bio_zone.zone_cmd) {
3049 	case DISK_ZONE_OPEN:
3050 	case DISK_ZONE_CLOSE:
3051 	case DISK_ZONE_FINISH:
3052 	case DISK_ZONE_RWP: {
3053 		int zone_flags;
3054 		int zone_sa;
3055 		uint64_t lba;
3056 
3057 		zone_sa = da_zone_bio_to_scsi(bp->bio_zone.zone_cmd);
3058 		if (zone_sa == -1) {
3059 			xpt_print(periph->path, "Cannot translate zone "
3060 			    "cmd %#x to SCSI\n", bp->bio_zone.zone_cmd);
3061 			error = EINVAL;
3062 			goto bailout;
3063 		}
3064 
3065 		zone_flags = 0;
3066 		lba = bp->bio_zone.zone_params.rwp.id;
3067 
3068 		if (bp->bio_zone.zone_params.rwp.flags &
3069 		    DISK_ZONE_RWP_FLAG_ALL)
3070 			zone_flags |= ZBC_OUT_ALL;
3071 
3072 		if (softc->zone_interface != DA_ZONE_IF_ATA_PASS) {
3073 			scsi_zbc_out(&ccb->csio,
3074 				     /*retries*/ da_retry_count,
3075 				     /*cbfcnp*/ dadone,
3076 				     /*tag_action*/ MSG_SIMPLE_Q_TAG,
3077 				     /*service_action*/ zone_sa,
3078 				     /*zone_id*/ lba,
3079 				     /*zone_flags*/ zone_flags,
3080 				     /*data_ptr*/ NULL,
3081 				     /*dxfer_len*/ 0,
3082 				     /*sense_len*/ SSD_FULL_SIZE,
3083 				     /*timeout*/ da_default_timeout * 1000);
3084 		} else {
3085 			/*
3086 			 * Note that in this case, even though we can
3087 			 * technically use NCQ, we don't bother for several
3088 			 * reasons:
3089 			 * 1. It hasn't been tested on a SAT layer that
3090 			 *    supports it.  This is new as of SAT-4.
3091 			 * 2. Even when there is a SAT layer that supports
3092 			 *    it, that SAT layer will also probably support
3093 			 *    ZBC -> ZAC translation, since they are both
3094 			 *    in the SAT-4 spec.
3095 			 * 3. Translation will likely be preferable to ATA
3096 			 *    passthrough.  LSI / Avago at least single
3097 			 *    steps ATA passthrough commands in the HBA,
3098 			 *    regardless of protocol, so unless that
3099 			 *    changes, there is a performance penalty for
3100 			 *    doing ATA passthrough no matter whether
3101 			 *    you're using NCQ/FPDMA, DMA or PIO.
3102 			 * 4. It requires a 32-byte CDB, which at least at
3103 			 *    this point in CAM requires a CDB pointer, which
3104 			 *    would require us to allocate an additional bit
3105 			 *    of storage separate from the CCB.
3106 			 */
3107 			error = scsi_ata_zac_mgmt_out(&ccb->csio,
3108 			    /*retries*/ da_retry_count,
3109 			    /*cbfcnp*/ dadone,
3110 			    /*tag_action*/ MSG_SIMPLE_Q_TAG,
3111 			    /*use_ncq*/ 0,
3112 			    /*zm_action*/ zone_sa,
3113 			    /*zone_id*/ lba,
3114 			    /*zone_flags*/ zone_flags,
3115 			    /*data_ptr*/ NULL,
3116 			    /*dxfer_len*/ 0,
3117 			    /*cdb_storage*/ NULL,
3118 			    /*cdb_storage_len*/ 0,
3119 			    /*sense_len*/ SSD_FULL_SIZE,
3120 			    /*timeout*/ da_default_timeout * 1000);
3121 			if (error != 0) {
3122 				error = EINVAL;
3123 				xpt_print(periph->path,
3124 				    "scsi_ata_zac_mgmt_out() returned an "
3125 				    "error!");
3126 				goto bailout;
3127 			}
3128 		}
3129 		*queue_ccb = 1;
3130 
3131 		break;
3132 	}
3133 	case DISK_ZONE_REPORT_ZONES: {
3134 		uint8_t *rz_ptr;
3135 		uint32_t num_entries, alloc_size;
3136 		struct disk_zone_report *rep;
3137 
3138 		rep = &bp->bio_zone.zone_params.report;
3139 
3140 		num_entries = rep->entries_allocated;
3141 		if (num_entries == 0) {
3142 			xpt_print(periph->path, "No entries allocated for "
3143 			    "Report Zones request\n");
3144 			error = EINVAL;
3145 			goto bailout;
3146 		}
3147 		alloc_size = sizeof(struct scsi_report_zones_hdr) +
3148 		    (sizeof(struct scsi_report_zones_desc) * num_entries);
3149 		alloc_size = min(alloc_size, softc->disk->d_maxsize);
3150 		rz_ptr = malloc(alloc_size, M_SCSIDA, M_NOWAIT | M_ZERO);
3151 		if (rz_ptr == NULL) {
3152 			xpt_print(periph->path, "Unable to allocate memory "
3153 			   "for Report Zones request\n");
3154 			error = ENOMEM;
3155 			goto bailout;
3156 		}
3157 
3158 		if (softc->zone_interface != DA_ZONE_IF_ATA_PASS) {
3159 			scsi_zbc_in(&ccb->csio,
3160 				    /*retries*/ da_retry_count,
3161 				    /*cbcfnp*/ dadone,
3162 				    /*tag_action*/ MSG_SIMPLE_Q_TAG,
3163 				    /*service_action*/ ZBC_IN_SA_REPORT_ZONES,
3164 				    /*zone_start_lba*/ rep->starting_id,
3165 				    /*zone_options*/ rep->rep_options,
3166 				    /*data_ptr*/ rz_ptr,
3167 				    /*dxfer_len*/ alloc_size,
3168 				    /*sense_len*/ SSD_FULL_SIZE,
3169 				    /*timeout*/ da_default_timeout * 1000);
3170 		} else {
3171 			/*
3172 			 * Note that in this case, even though we can
3173 			 * technically use NCQ, we don't bother for several
3174 			 * reasons:
3175 			 * 1. It hasn't been tested on a SAT layer that
3176 			 *    supports it.  This is new as of SAT-4.
3177 			 * 2. Even when there is a SAT layer that supports
3178 			 *    it, that SAT layer will also probably support
3179 			 *    ZBC -> ZAC translation, since they are both
3180 			 *    in the SAT-4 spec.
3181 			 * 3. Translation will likely be preferable to ATA
3182 			 *    passthrough.  LSI / Avago at least single
3183 			 *    steps ATA passthrough commands in the HBA,
3184 			 *    regardless of protocol, so unless that
3185 			 *    changes, there is a performance penalty for
3186 			 *    doing ATA passthrough no matter whether
3187 			 *    you're using NCQ/FPDMA, DMA or PIO.
3188 			 * 4. It requires a 32-byte CDB, which at least at
3189 			 *    this point in CAM requires a CDB pointer, which
3190 			 *    would require us to allocate an additional bit
3191 			 *    of storage separate from the CCB.
3192 			 */
3193 			error = scsi_ata_zac_mgmt_in(&ccb->csio,
3194 			    /*retries*/ da_retry_count,
3195 			    /*cbcfnp*/ dadone,
3196 			    /*tag_action*/ MSG_SIMPLE_Q_TAG,
3197 			    /*use_ncq*/ 0,
3198 			    /*zm_action*/ ATA_ZM_REPORT_ZONES,
3199 			    /*zone_id*/ rep->starting_id,
3200 			    /*zone_flags*/ rep->rep_options,
3201 			    /*data_ptr*/ rz_ptr,
3202 			    /*dxfer_len*/ alloc_size,
3203 			    /*cdb_storage*/ NULL,
3204 			    /*cdb_storage_len*/ 0,
3205 			    /*sense_len*/ SSD_FULL_SIZE,
3206 			    /*timeout*/ da_default_timeout * 1000);
3207 			if (error != 0) {
3208 				error = EINVAL;
3209 				xpt_print(periph->path,
3210 				    "scsi_ata_zac_mgmt_in() returned an "
3211 				    "error!");
3212 				goto bailout;
3213 			}
3214 		}
3215 
3216 		/*
3217 		 * For BIO_ZONE, this isn't normally needed.  However, it
3218 		 * is used by devstat_end_transaction_bio() to determine
3219 		 * how much data was transferred.
3220 		 */
3221 		/*
3222 		 * XXX KDM we have a problem.  But I'm not sure how to fix
3223 		 * it.  devstat uses bio_bcount - bio_resid to calculate
3224 		 * the amount of data transferred.   The GEOM disk code
3225 		 * uses bio_length - bio_resid to calculate the amount of
3226 		 * data in bio_completed.  We have different structure
3227 		 * sizes above and below the ada(4) driver.  So, if we
3228 		 * use the sizes above, the amount transferred won't be
3229 		 * quite accurate for devstat.  If we use different sizes
3230 		 * for bio_bcount and bio_length (above and below
3231 		 * respectively), then the residual needs to match one or
3232 		 * the other.  Everything is calculated after the bio
3233 		 * leaves the driver, so changing the values around isn't
3234 		 * really an option.  For now, just set the count to the
3235 		 * passed in length.  This means that the calculations
3236 		 * above (e.g. bio_completed) will be correct, but the
3237 		 * amount of data reported to devstat will be slightly
3238 		 * under or overstated.
3239 		 */
3240 		bp->bio_bcount = bp->bio_length;
3241 
3242 		*queue_ccb = 1;
3243 
3244 		break;
3245 	}
3246 	case DISK_ZONE_GET_PARAMS: {
3247 		struct disk_zone_disk_params *params;
3248 
3249 		params = &bp->bio_zone.zone_params.disk_params;
3250 		bzero(params, sizeof(*params));
3251 
3252 		switch (softc->zone_mode) {
3253 		case DA_ZONE_DRIVE_MANAGED:
3254 			params->zone_mode = DISK_ZONE_MODE_DRIVE_MANAGED;
3255 			break;
3256 		case DA_ZONE_HOST_AWARE:
3257 			params->zone_mode = DISK_ZONE_MODE_HOST_AWARE;
3258 			break;
3259 		case DA_ZONE_HOST_MANAGED:
3260 			params->zone_mode = DISK_ZONE_MODE_HOST_MANAGED;
3261 			break;
3262 		default:
3263 		case DA_ZONE_NONE:
3264 			params->zone_mode = DISK_ZONE_MODE_NONE;
3265 			break;
3266 		}
3267 
3268 		if (softc->zone_flags & DA_ZONE_FLAG_URSWRZ)
3269 			params->flags |= DISK_ZONE_DISK_URSWRZ;
3270 
3271 		if (softc->zone_flags & DA_ZONE_FLAG_OPT_SEQ_SET) {
3272 			params->optimal_seq_zones = softc->optimal_seq_zones;
3273 			params->flags |= DISK_ZONE_OPT_SEQ_SET;
3274 		}
3275 
3276 		if (softc->zone_flags & DA_ZONE_FLAG_OPT_NONSEQ_SET) {
3277 			params->optimal_nonseq_zones =
3278 			    softc->optimal_nonseq_zones;
3279 			params->flags |= DISK_ZONE_OPT_NONSEQ_SET;
3280 		}
3281 
3282 		if (softc->zone_flags & DA_ZONE_FLAG_MAX_SEQ_SET) {
3283 			params->max_seq_zones = softc->max_seq_zones;
3284 			params->flags |= DISK_ZONE_MAX_SEQ_SET;
3285 		}
3286 		if (softc->zone_flags & DA_ZONE_FLAG_RZ_SUP)
3287 			params->flags |= DISK_ZONE_RZ_SUP;
3288 
3289 		if (softc->zone_flags & DA_ZONE_FLAG_OPEN_SUP)
3290 			params->flags |= DISK_ZONE_OPEN_SUP;
3291 
3292 		if (softc->zone_flags & DA_ZONE_FLAG_CLOSE_SUP)
3293 			params->flags |= DISK_ZONE_CLOSE_SUP;
3294 
3295 		if (softc->zone_flags & DA_ZONE_FLAG_FINISH_SUP)
3296 			params->flags |= DISK_ZONE_FINISH_SUP;
3297 
3298 		if (softc->zone_flags & DA_ZONE_FLAG_RWP_SUP)
3299 			params->flags |= DISK_ZONE_RWP_SUP;
3300 		break;
3301 	}
3302 	default:
3303 		break;
3304 	}
3305 bailout:
3306 	return (error);
3307 }
3308 
3309 static void
3310 dastart(struct cam_periph *periph, union ccb *start_ccb)
3311 {
3312 	struct da_softc *softc;
3313 
3314 	cam_periph_assert(periph, MA_OWNED);
3315 	softc = (struct da_softc *)periph->softc;
3316 
3317 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastart\n"));
3318 
3319 skipstate:
3320 	switch (softc->state) {
3321 	case DA_STATE_NORMAL:
3322 	{
3323 		struct bio *bp;
3324 		uint8_t tag_code;
3325 
3326 more:
3327 		bp = cam_iosched_next_bio(softc->cam_iosched);
3328 		if (bp == NULL) {
3329 			if (cam_iosched_has_work_flags(softc->cam_iosched,
3330 			    DA_WORK_TUR)) {
3331 				softc->flags |= DA_FLAG_TUR_PENDING;
3332 				cam_iosched_clr_work_flags(softc->cam_iosched,
3333 				    DA_WORK_TUR);
3334 				scsi_test_unit_ready(&start_ccb->csio,
3335 				     /*retries*/ da_retry_count,
3336 				     dadone_tur,
3337 				     MSG_SIMPLE_Q_TAG,
3338 				     SSD_FULL_SIZE,
3339 				     da_default_timeout * 1000);
3340 				start_ccb->ccb_h.ccb_bp = NULL;
3341 				start_ccb->ccb_h.ccb_state = DA_CCB_TUR;
3342 				xpt_action(start_ccb);
3343 			} else
3344 				xpt_release_ccb(start_ccb);
3345 			break;
3346 		}
3347 
3348 		if (bp->bio_cmd == BIO_DELETE) {
3349 			if (softc->delete_func != NULL) {
3350 				softc->delete_func(periph, start_ccb, bp);
3351 				goto out;
3352 			} else {
3353 				/*
3354 				 * Not sure this is possible, but failsafe by
3355 				 * lying and saying "sure, done."
3356 				 */
3357 				biofinish(bp, NULL, 0);
3358 				goto more;
3359 			}
3360 		}
3361 
3362 		if (cam_iosched_has_work_flags(softc->cam_iosched,
3363 		    DA_WORK_TUR)) {
3364 			cam_iosched_clr_work_flags(softc->cam_iosched,
3365 			    DA_WORK_TUR);
3366 			da_periph_release_locked(periph, DA_REF_TUR);
3367 		}
3368 
3369 		if ((bp->bio_flags & BIO_ORDERED) != 0 ||
3370 		    (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
3371 			softc->flags &= ~DA_FLAG_NEED_OTAG;
3372 			softc->flags |= DA_FLAG_WAS_OTAG;
3373 			tag_code = MSG_ORDERED_Q_TAG;
3374 		} else {
3375 			tag_code = MSG_SIMPLE_Q_TAG;
3376 		}
3377 
3378 		switch (bp->bio_cmd) {
3379 		case BIO_WRITE:
3380 		case BIO_READ:
3381 		{
3382 			void *data_ptr;
3383 			int rw_op;
3384 
3385 			biotrack(bp, __func__);
3386 
3387 			if (bp->bio_cmd == BIO_WRITE) {
3388 				softc->flags |= DA_FLAG_DIRTY;
3389 				rw_op = SCSI_RW_WRITE;
3390 			} else {
3391 				rw_op = SCSI_RW_READ;
3392 			}
3393 
3394 			data_ptr = bp->bio_data;
3395 			if ((bp->bio_flags & (BIO_UNMAPPED|BIO_VLIST)) != 0) {
3396 				rw_op |= SCSI_RW_BIO;
3397 				data_ptr = bp;
3398 			}
3399 
3400 			scsi_read_write(&start_ccb->csio,
3401 					/*retries*/da_retry_count,
3402 					/*cbfcnp*/dadone,
3403 					/*tag_action*/tag_code,
3404 					rw_op,
3405 					/*byte2*/0,
3406 					softc->minimum_cmd_size,
3407 					/*lba*/bp->bio_pblkno,
3408 					/*block_count*/bp->bio_bcount /
3409 					softc->params.secsize,
3410 					data_ptr,
3411 					/*dxfer_len*/ bp->bio_bcount,
3412 					/*sense_len*/SSD_FULL_SIZE,
3413 					da_default_timeout * 1000);
3414 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
3415 			start_ccb->csio.bio = bp;
3416 #endif
3417 			break;
3418 		}
3419 		case BIO_FLUSH:
3420 			/*
3421 			 * If we don't support sync cache, or the disk
3422 			 * isn't dirty, FLUSH is a no-op.  Use the
3423 			 * allocated CCB for the next bio if one is
3424 			 * available.
3425 			 */
3426 			if ((softc->quirks & DA_Q_NO_SYNC_CACHE) != 0 ||
3427 			    (softc->flags & DA_FLAG_DIRTY) == 0) {
3428 				biodone(bp);
3429 				goto skipstate;
3430 			}
3431 
3432 			/*
3433 			 * BIO_FLUSH doesn't currently communicate
3434 			 * range data, so we synchronize the cache
3435 			 * over the whole disk.
3436 			 */
3437 			scsi_synchronize_cache(&start_ccb->csio,
3438 					       /*retries*/1,
3439 					       /*cbfcnp*/dadone,
3440 					       /*tag_action*/tag_code,
3441 					       /*begin_lba*/0,
3442 					       /*lb_count*/0,
3443 					       SSD_FULL_SIZE,
3444 					       da_default_timeout*1000);
3445 			/*
3446 			 * Clear the dirty flag before sending the command.
3447 			 * Either this sync cache will be successful, or it
3448 			 * will fail after a retry.  If it fails, it is
3449 			 * unlikely to be successful if retried later, so
3450 			 * we'll save ourselves time by just marking the
3451 			 * device clean.
3452 			 */
3453 			softc->flags &= ~DA_FLAG_DIRTY;
3454 			break;
3455 		case BIO_ZONE: {
3456 			int error, queue_ccb;
3457 
3458 			queue_ccb = 0;
3459 
3460 			error = da_zone_cmd(periph, start_ccb, bp,&queue_ccb);
3461 			if ((error != 0)
3462 			 || (queue_ccb == 0)) {
3463 				biofinish(bp, NULL, error);
3464 				xpt_release_ccb(start_ccb);
3465 				return;
3466 			}
3467 			break;
3468 		}
3469 		default:
3470 			biofinish(bp, NULL, EOPNOTSUPP);
3471 			xpt_release_ccb(start_ccb);
3472 			return;
3473 		}
3474 		start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
3475 		start_ccb->ccb_h.flags |= CAM_UNLOCKED;
3476 		start_ccb->ccb_h.softtimeout = sbttotv(da_default_softtimeout);
3477 
3478 out:
3479 		LIST_INSERT_HEAD(&softc->pending_ccbs,
3480 				 &start_ccb->ccb_h, periph_links.le);
3481 
3482 		/* We expect a unit attention from this device */
3483 		if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
3484 			start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
3485 			softc->flags &= ~DA_FLAG_RETRY_UA;
3486 		}
3487 
3488 		start_ccb->ccb_h.ccb_bp = bp;
3489 		softc->refcount++;
3490 		cam_periph_unlock(periph);
3491 		xpt_action(start_ccb);
3492 		cam_periph_lock(periph);
3493 
3494 		/* May have more work to do, so ensure we stay scheduled */
3495 		daschedule(periph);
3496 		break;
3497 	}
3498 	case DA_STATE_PROBE_WP:
3499 	{
3500 		void  *mode_buf;
3501 		int    mode_buf_len;
3502 
3503 		if (da_disable_wp_detection) {
3504 			if ((softc->flags & DA_FLAG_CAN_RC16) != 0)
3505 				softc->state = DA_STATE_PROBE_RC16;
3506 			else
3507 				softc->state = DA_STATE_PROBE_RC;
3508 			goto skipstate;
3509 		}
3510 		mode_buf_len = 192;
3511 		mode_buf = malloc(mode_buf_len, M_SCSIDA, M_NOWAIT);
3512 		if (mode_buf == NULL) {
3513 			xpt_print(periph->path, "Unable to send mode sense - "
3514 			    "malloc failure\n");
3515 			if ((softc->flags & DA_FLAG_CAN_RC16) != 0)
3516 				softc->state = DA_STATE_PROBE_RC16;
3517 			else
3518 				softc->state = DA_STATE_PROBE_RC;
3519 			goto skipstate;
3520 		}
3521 		scsi_mode_sense_len(&start_ccb->csio,
3522 				    /*retries*/ da_retry_count,
3523 				    /*cbfcnp*/ dadone_probewp,
3524 				    /*tag_action*/ MSG_SIMPLE_Q_TAG,
3525 				    /*dbd*/ FALSE,
3526 				    /*pc*/ SMS_PAGE_CTRL_CURRENT,
3527 				    /*page*/ SMS_ALL_PAGES_PAGE,
3528 				    /*param_buf*/ mode_buf,
3529 				    /*param_len*/ mode_buf_len,
3530 				    /*minimum_cmd_size*/ softc->minimum_cmd_size,
3531 				    /*sense_len*/ SSD_FULL_SIZE,
3532 				    /*timeout*/ da_default_timeout * 1000);
3533 		start_ccb->ccb_h.ccb_bp = NULL;
3534 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_WP;
3535 		xpt_action(start_ccb);
3536 		break;
3537 	}
3538 	case DA_STATE_PROBE_RC:
3539 	{
3540 		struct scsi_read_capacity_data *rcap;
3541 
3542 		rcap = (struct scsi_read_capacity_data *)
3543 		    malloc(sizeof(*rcap), M_SCSIDA, M_NOWAIT|M_ZERO);
3544 		if (rcap == NULL) {
3545 			printf("dastart: Couldn't malloc read_capacity data\n");
3546 			/* da_free_periph??? */
3547 			break;
3548 		}
3549 		scsi_read_capacity(&start_ccb->csio,
3550 				   /*retries*/da_retry_count,
3551 				   dadone_proberc,
3552 				   MSG_SIMPLE_Q_TAG,
3553 				   rcap,
3554 				   SSD_FULL_SIZE,
3555 				   /*timeout*/5000);
3556 		start_ccb->ccb_h.ccb_bp = NULL;
3557 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC;
3558 		xpt_action(start_ccb);
3559 		break;
3560 	}
3561 	case DA_STATE_PROBE_RC16:
3562 	{
3563 		struct scsi_read_capacity_data_long *rcaplong;
3564 
3565 		rcaplong = (struct scsi_read_capacity_data_long *)
3566 			malloc(sizeof(*rcaplong), M_SCSIDA, M_NOWAIT|M_ZERO);
3567 		if (rcaplong == NULL) {
3568 			printf("dastart: Couldn't malloc read_capacity data\n");
3569 			/* da_free_periph??? */
3570 			break;
3571 		}
3572 		scsi_read_capacity_16(&start_ccb->csio,
3573 				      /*retries*/ da_retry_count,
3574 				      /*cbfcnp*/ dadone_proberc,
3575 				      /*tag_action*/ MSG_SIMPLE_Q_TAG,
3576 				      /*lba*/ 0,
3577 				      /*reladr*/ 0,
3578 				      /*pmi*/ 0,
3579 				      /*rcap_buf*/ (uint8_t *)rcaplong,
3580 				      /*rcap_buf_len*/ sizeof(*rcaplong),
3581 				      /*sense_len*/ SSD_FULL_SIZE,
3582 				      /*timeout*/ da_default_timeout * 1000);
3583 		start_ccb->ccb_h.ccb_bp = NULL;
3584 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC16;
3585 		xpt_action(start_ccb);
3586 		break;
3587 	}
3588 	case DA_STATE_PROBE_LBP:
3589 	{
3590 		struct scsi_vpd_logical_block_prov *lbp;
3591 
3592 		if (!scsi_vpd_supported_page(periph, SVPD_LBP)) {
3593 			/*
3594 			 * If we get here we don't support any SBC-3 delete
3595 			 * methods with UNMAP as the Logical Block Provisioning
3596 			 * VPD page support is required for devices which
3597 			 * support it according to T10/1799-D Revision 31
3598 			 * however older revisions of the spec don't mandate
3599 			 * this so we currently don't remove these methods
3600 			 * from the available set.
3601 			 */
3602 			softc->state = DA_STATE_PROBE_BLK_LIMITS;
3603 			goto skipstate;
3604 		}
3605 
3606 		lbp = (struct scsi_vpd_logical_block_prov *)
3607 			malloc(sizeof(*lbp), M_SCSIDA, M_NOWAIT|M_ZERO);
3608 
3609 		if (lbp == NULL) {
3610 			printf("dastart: Couldn't malloc lbp data\n");
3611 			/* da_free_periph??? */
3612 			break;
3613 		}
3614 
3615 		scsi_inquiry(&start_ccb->csio,
3616 			     /*retries*/da_retry_count,
3617 			     /*cbfcnp*/dadone_probelbp,
3618 			     /*tag_action*/MSG_SIMPLE_Q_TAG,
3619 			     /*inq_buf*/(u_int8_t *)lbp,
3620 			     /*inq_len*/sizeof(*lbp),
3621 			     /*evpd*/TRUE,
3622 			     /*page_code*/SVPD_LBP,
3623 			     /*sense_len*/SSD_MIN_SIZE,
3624 			     /*timeout*/da_default_timeout * 1000);
3625 		start_ccb->ccb_h.ccb_bp = NULL;
3626 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_LBP;
3627 		xpt_action(start_ccb);
3628 		break;
3629 	}
3630 	case DA_STATE_PROBE_BLK_LIMITS:
3631 	{
3632 		struct scsi_vpd_block_limits *block_limits;
3633 
3634 		if (!scsi_vpd_supported_page(periph, SVPD_BLOCK_LIMITS)) {
3635 			/* Not supported skip to next probe */
3636 			softc->state = DA_STATE_PROBE_BDC;
3637 			goto skipstate;
3638 		}
3639 
3640 		block_limits = (struct scsi_vpd_block_limits *)
3641 			malloc(sizeof(*block_limits), M_SCSIDA, M_NOWAIT|M_ZERO);
3642 
3643 		if (block_limits == NULL) {
3644 			printf("dastart: Couldn't malloc block_limits data\n");
3645 			/* da_free_periph??? */
3646 			break;
3647 		}
3648 
3649 		scsi_inquiry(&start_ccb->csio,
3650 			     /*retries*/da_retry_count,
3651 			     /*cbfcnp*/dadone_probeblklimits,
3652 			     /*tag_action*/MSG_SIMPLE_Q_TAG,
3653 			     /*inq_buf*/(u_int8_t *)block_limits,
3654 			     /*inq_len*/sizeof(*block_limits),
3655 			     /*evpd*/TRUE,
3656 			     /*page_code*/SVPD_BLOCK_LIMITS,
3657 			     /*sense_len*/SSD_MIN_SIZE,
3658 			     /*timeout*/da_default_timeout * 1000);
3659 		start_ccb->ccb_h.ccb_bp = NULL;
3660 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BLK_LIMITS;
3661 		xpt_action(start_ccb);
3662 		break;
3663 	}
3664 	case DA_STATE_PROBE_BDC:
3665 	{
3666 		struct scsi_vpd_block_device_characteristics *bdc;
3667 
3668 		if (!scsi_vpd_supported_page(periph, SVPD_BDC)) {
3669 			softc->state = DA_STATE_PROBE_ATA;
3670 			goto skipstate;
3671 		}
3672 
3673 		bdc = (struct scsi_vpd_block_device_characteristics *)
3674 			malloc(sizeof(*bdc), M_SCSIDA, M_NOWAIT|M_ZERO);
3675 
3676 		if (bdc == NULL) {
3677 			printf("dastart: Couldn't malloc bdc data\n");
3678 			/* da_free_periph??? */
3679 			break;
3680 		}
3681 
3682 		scsi_inquiry(&start_ccb->csio,
3683 			     /*retries*/da_retry_count,
3684 			     /*cbfcnp*/dadone_probebdc,
3685 			     /*tag_action*/MSG_SIMPLE_Q_TAG,
3686 			     /*inq_buf*/(u_int8_t *)bdc,
3687 			     /*inq_len*/sizeof(*bdc),
3688 			     /*evpd*/TRUE,
3689 			     /*page_code*/SVPD_BDC,
3690 			     /*sense_len*/SSD_MIN_SIZE,
3691 			     /*timeout*/da_default_timeout * 1000);
3692 		start_ccb->ccb_h.ccb_bp = NULL;
3693 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BDC;
3694 		xpt_action(start_ccb);
3695 		break;
3696 	}
3697 	case DA_STATE_PROBE_ATA:
3698 	{
3699 		struct ata_params *ata_params;
3700 
3701 		if (!scsi_vpd_supported_page(periph, SVPD_ATA_INFORMATION)) {
3702 			if ((softc->zone_mode == DA_ZONE_HOST_AWARE)
3703 			 || (softc->zone_mode == DA_ZONE_HOST_MANAGED)) {
3704 				/*
3705 				 * Note that if the ATA VPD page isn't
3706 				 * supported, we aren't talking to an ATA
3707 				 * device anyway.  Support for that VPD
3708 				 * page is mandatory for SCSI to ATA (SAT)
3709 				 * translation layers.
3710 				 */
3711 				softc->state = DA_STATE_PROBE_ZONE;
3712 				goto skipstate;
3713 			}
3714 			daprobedone(periph, start_ccb);
3715 			break;
3716 		}
3717 
3718 		ata_params = &periph->path->device->ident_data;
3719 
3720 		scsi_ata_identify(&start_ccb->csio,
3721 				  /*retries*/da_retry_count,
3722 				  /*cbfcnp*/dadone_probeata,
3723                                   /*tag_action*/MSG_SIMPLE_Q_TAG,
3724 				  /*data_ptr*/(u_int8_t *)ata_params,
3725 				  /*dxfer_len*/sizeof(*ata_params),
3726 				  /*sense_len*/SSD_FULL_SIZE,
3727 				  /*timeout*/da_default_timeout * 1000);
3728 		start_ccb->ccb_h.ccb_bp = NULL;
3729 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA;
3730 		xpt_action(start_ccb);
3731 		break;
3732 	}
3733 	case DA_STATE_PROBE_ATA_LOGDIR:
3734 	{
3735 		struct ata_gp_log_dir *log_dir;
3736 		int retval;
3737 
3738 		retval = 0;
3739 
3740 		if ((softc->flags & DA_FLAG_CAN_ATA_LOG) == 0) {
3741 			/*
3742 			 * If we don't have log support, not much point in
3743 			 * trying to probe zone support.
3744 			 */
3745 			daprobedone(periph, start_ccb);
3746 			break;
3747 		}
3748 
3749 		/*
3750 		 * If we have an ATA device (the SCSI ATA Information VPD
3751 		 * page should be present and the ATA identify should have
3752 		 * succeeded) and it supports logs, ask for the log directory.
3753 		 */
3754 
3755 		log_dir = malloc(sizeof(*log_dir), M_SCSIDA, M_NOWAIT|M_ZERO);
3756 		if (log_dir == NULL) {
3757 			xpt_print(periph->path, "Couldn't malloc log_dir "
3758 			    "data\n");
3759 			daprobedone(periph, start_ccb);
3760 			break;
3761 		}
3762 
3763 		retval = scsi_ata_read_log(&start_ccb->csio,
3764 		    /*retries*/ da_retry_count,
3765 		    /*cbfcnp*/ dadone_probeatalogdir,
3766 		    /*tag_action*/ MSG_SIMPLE_Q_TAG,
3767 		    /*log_address*/ ATA_LOG_DIRECTORY,
3768 		    /*page_number*/ 0,
3769 		    /*block_count*/ 1,
3770 		    /*protocol*/ softc->flags & DA_FLAG_CAN_ATA_DMA ?
3771 				 AP_PROTO_DMA : AP_PROTO_PIO_IN,
3772 		    /*data_ptr*/ (uint8_t *)log_dir,
3773 		    /*dxfer_len*/ sizeof(*log_dir),
3774 		    /*sense_len*/ SSD_FULL_SIZE,
3775 		    /*timeout*/ da_default_timeout * 1000);
3776 
3777 		if (retval != 0) {
3778 			xpt_print(periph->path, "scsi_ata_read_log() failed!");
3779 			free(log_dir, M_SCSIDA);
3780 			daprobedone(periph, start_ccb);
3781 			break;
3782 		}
3783 		start_ccb->ccb_h.ccb_bp = NULL;
3784 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA_LOGDIR;
3785 		xpt_action(start_ccb);
3786 		break;
3787 	}
3788 	case DA_STATE_PROBE_ATA_IDDIR:
3789 	{
3790 		struct ata_identify_log_pages *id_dir;
3791 		int retval;
3792 
3793 		retval = 0;
3794 
3795 		/*
3796 		 * Check here to see whether the Identify Device log is
3797 		 * supported in the directory of logs.  If so, continue
3798 		 * with requesting the log of identify device pages.
3799 		 */
3800 		if ((softc->flags & DA_FLAG_CAN_ATA_IDLOG) == 0) {
3801 			daprobedone(periph, start_ccb);
3802 			break;
3803 		}
3804 
3805 		id_dir = malloc(sizeof(*id_dir), M_SCSIDA, M_NOWAIT | M_ZERO);
3806 		if (id_dir == NULL) {
3807 			xpt_print(periph->path, "Couldn't malloc id_dir "
3808 			    "data\n");
3809 			daprobedone(periph, start_ccb);
3810 			break;
3811 		}
3812 
3813 		retval = scsi_ata_read_log(&start_ccb->csio,
3814 		    /*retries*/ da_retry_count,
3815 		    /*cbfcnp*/ dadone_probeataiddir,
3816 		    /*tag_action*/ MSG_SIMPLE_Q_TAG,
3817 		    /*log_address*/ ATA_IDENTIFY_DATA_LOG,
3818 		    /*page_number*/ ATA_IDL_PAGE_LIST,
3819 		    /*block_count*/ 1,
3820 		    /*protocol*/ softc->flags & DA_FLAG_CAN_ATA_DMA ?
3821 				 AP_PROTO_DMA : AP_PROTO_PIO_IN,
3822 		    /*data_ptr*/ (uint8_t *)id_dir,
3823 		    /*dxfer_len*/ sizeof(*id_dir),
3824 		    /*sense_len*/ SSD_FULL_SIZE,
3825 		    /*timeout*/ da_default_timeout * 1000);
3826 
3827 		if (retval != 0) {
3828 			xpt_print(periph->path, "scsi_ata_read_log() failed!");
3829 			free(id_dir, M_SCSIDA);
3830 			daprobedone(periph, start_ccb);
3831 			break;
3832 		}
3833 		start_ccb->ccb_h.ccb_bp = NULL;
3834 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA_IDDIR;
3835 		xpt_action(start_ccb);
3836 		break;
3837 	}
3838 	case DA_STATE_PROBE_ATA_SUP:
3839 	{
3840 		struct ata_identify_log_sup_cap *sup_cap;
3841 		int retval;
3842 
3843 		retval = 0;
3844 
3845 		/*
3846 		 * Check here to see whether the Supported Capabilities log
3847 		 * is in the list of Identify Device logs.
3848 		 */
3849 		if ((softc->flags & DA_FLAG_CAN_ATA_SUPCAP) == 0) {
3850 			daprobedone(periph, start_ccb);
3851 			break;
3852 		}
3853 
3854 		sup_cap = malloc(sizeof(*sup_cap), M_SCSIDA, M_NOWAIT|M_ZERO);
3855 		if (sup_cap == NULL) {
3856 			xpt_print(periph->path, "Couldn't malloc sup_cap "
3857 			    "data\n");
3858 			daprobedone(periph, start_ccb);
3859 			break;
3860 		}
3861 
3862 		retval = scsi_ata_read_log(&start_ccb->csio,
3863 		    /*retries*/ da_retry_count,
3864 		    /*cbfcnp*/ dadone_probeatasup,
3865 		    /*tag_action*/ MSG_SIMPLE_Q_TAG,
3866 		    /*log_address*/ ATA_IDENTIFY_DATA_LOG,
3867 		    /*page_number*/ ATA_IDL_SUP_CAP,
3868 		    /*block_count*/ 1,
3869 		    /*protocol*/ softc->flags & DA_FLAG_CAN_ATA_DMA ?
3870 				 AP_PROTO_DMA : AP_PROTO_PIO_IN,
3871 		    /*data_ptr*/ (uint8_t *)sup_cap,
3872 		    /*dxfer_len*/ sizeof(*sup_cap),
3873 		    /*sense_len*/ SSD_FULL_SIZE,
3874 		    /*timeout*/ da_default_timeout * 1000);
3875 
3876 		if (retval != 0) {
3877 			xpt_print(periph->path, "scsi_ata_read_log() failed!");
3878 			free(sup_cap, M_SCSIDA);
3879 			daprobedone(periph, start_ccb);
3880 			break;
3881 		}
3882 
3883 		start_ccb->ccb_h.ccb_bp = NULL;
3884 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA_SUP;
3885 		xpt_action(start_ccb);
3886 		break;
3887 	}
3888 	case DA_STATE_PROBE_ATA_ZONE:
3889 	{
3890 		struct ata_zoned_info_log *ata_zone;
3891 		int retval;
3892 
3893 		retval = 0;
3894 
3895 		/*
3896 		 * Check here to see whether the zoned device information
3897 		 * page is supported.  If so, continue on to request it.
3898 		 * If not, skip to DA_STATE_PROBE_LOG or done.
3899 		 */
3900 		if ((softc->flags & DA_FLAG_CAN_ATA_ZONE) == 0) {
3901 			daprobedone(periph, start_ccb);
3902 			break;
3903 		}
3904 		ata_zone = malloc(sizeof(*ata_zone), M_SCSIDA,
3905 				  M_NOWAIT|M_ZERO);
3906 		if (ata_zone == NULL) {
3907 			xpt_print(periph->path, "Couldn't malloc ata_zone "
3908 			    "data\n");
3909 			daprobedone(periph, start_ccb);
3910 			break;
3911 		}
3912 
3913 		retval = scsi_ata_read_log(&start_ccb->csio,
3914 		    /*retries*/ da_retry_count,
3915 		    /*cbfcnp*/ dadone_probeatazone,
3916 		    /*tag_action*/ MSG_SIMPLE_Q_TAG,
3917 		    /*log_address*/ ATA_IDENTIFY_DATA_LOG,
3918 		    /*page_number*/ ATA_IDL_ZDI,
3919 		    /*block_count*/ 1,
3920 		    /*protocol*/ softc->flags & DA_FLAG_CAN_ATA_DMA ?
3921 				 AP_PROTO_DMA : AP_PROTO_PIO_IN,
3922 		    /*data_ptr*/ (uint8_t *)ata_zone,
3923 		    /*dxfer_len*/ sizeof(*ata_zone),
3924 		    /*sense_len*/ SSD_FULL_SIZE,
3925 		    /*timeout*/ da_default_timeout * 1000);
3926 
3927 		if (retval != 0) {
3928 			xpt_print(periph->path, "scsi_ata_read_log() failed!");
3929 			free(ata_zone, M_SCSIDA);
3930 			daprobedone(periph, start_ccb);
3931 			break;
3932 		}
3933 		start_ccb->ccb_h.ccb_bp = NULL;
3934 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA_ZONE;
3935 		xpt_action(start_ccb);
3936 
3937 		break;
3938 	}
3939 	case DA_STATE_PROBE_ZONE:
3940 	{
3941 		struct scsi_vpd_zoned_bdc *bdc;
3942 
3943 		/*
3944 		 * Note that this page will be supported for SCSI protocol
3945 		 * devices that support ZBC (SMR devices), as well as ATA
3946 		 * protocol devices that are behind a SAT (SCSI to ATA
3947 		 * Translation) layer that supports converting ZBC commands
3948 		 * to their ZAC equivalents.
3949 		 */
3950 		if (!scsi_vpd_supported_page(periph, SVPD_ZONED_BDC)) {
3951 			daprobedone(periph, start_ccb);
3952 			break;
3953 		}
3954 		bdc = (struct scsi_vpd_zoned_bdc *)
3955 			malloc(sizeof(*bdc), M_SCSIDA, M_NOWAIT|M_ZERO);
3956 
3957 		if (bdc == NULL) {
3958 			xpt_release_ccb(start_ccb);
3959 			xpt_print(periph->path, "Couldn't malloc zone VPD "
3960 			    "data\n");
3961 			break;
3962 		}
3963 		scsi_inquiry(&start_ccb->csio,
3964 			     /*retries*/da_retry_count,
3965 			     /*cbfcnp*/dadone_probezone,
3966 			     /*tag_action*/MSG_SIMPLE_Q_TAG,
3967 			     /*inq_buf*/(u_int8_t *)bdc,
3968 			     /*inq_len*/sizeof(*bdc),
3969 			     /*evpd*/TRUE,
3970 			     /*page_code*/SVPD_ZONED_BDC,
3971 			     /*sense_len*/SSD_FULL_SIZE,
3972 			     /*timeout*/da_default_timeout * 1000);
3973 		start_ccb->ccb_h.ccb_bp = NULL;
3974 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ZONE;
3975 		xpt_action(start_ccb);
3976 		break;
3977 	}
3978 	}
3979 }
3980 
3981 /*
3982  * In each of the methods below, while its the caller's
3983  * responsibility to ensure the request will fit into a
3984  * single device request, we might have changed the delete
3985  * method due to the device incorrectly advertising either
3986  * its supported methods or limits.
3987  *
3988  * To prevent this causing further issues we validate the
3989  * against the methods limits, and warn which would
3990  * otherwise be unnecessary.
3991  */
3992 static void
3993 da_delete_unmap(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
3994 {
3995 	struct da_softc *softc = (struct da_softc *)periph->softc;
3996 	struct bio *bp1;
3997 	uint8_t *buf = softc->unmap_buf;
3998 	struct scsi_unmap_desc *d = (void *)&buf[UNMAP_HEAD_SIZE];
3999 	uint64_t lba, lastlba = (uint64_t)-1;
4000 	uint64_t totalcount = 0;
4001 	uint64_t count;
4002 	uint32_t c, lastcount = 0, ranges = 0;
4003 
4004 	/*
4005 	 * Currently this doesn't take the UNMAP
4006 	 * Granularity and Granularity Alignment
4007 	 * fields into account.
4008 	 *
4009 	 * This could result in both unoptimal unmap
4010 	 * requests as as well as UNMAP calls unmapping
4011 	 * fewer LBA's than requested.
4012 	 */
4013 
4014 	bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
4015 	bp1 = bp;
4016 	do {
4017 		/*
4018 		 * Note: ada and da are different in how they store the
4019 		 * pending bp's in a trim. ada stores all of them in the
4020 		 * trim_req.bps. da stores all but the first one in the
4021 		 * delete_run_queue. ada then completes all the bps in
4022 		 * its adadone() loop. da completes all the bps in the
4023 		 * delete_run_queue in dadone, and relies on the biodone
4024 		 * after to complete. This should be reconciled since there's
4025 		 * no real reason to do it differently. XXX
4026 		 */
4027 		if (bp1 != bp)
4028 			bioq_insert_tail(&softc->delete_run_queue, bp1);
4029 		lba = bp1->bio_pblkno;
4030 		count = bp1->bio_bcount / softc->params.secsize;
4031 
4032 		/* Try to extend the previous range. */
4033 		if (lba == lastlba) {
4034 			c = omin(count, UNMAP_RANGE_MAX - lastcount);
4035 			lastlba += c;
4036 			lastcount += c;
4037 			scsi_ulto4b(lastcount, d[ranges - 1].length);
4038 			count -= c;
4039 			lba += c;
4040 			totalcount += c;
4041 		} else if ((softc->quirks & DA_Q_STRICT_UNMAP) &&
4042 		    softc->unmap_gran != 0) {
4043 			/* Align length of the previous range. */
4044 			if ((c = lastcount % softc->unmap_gran) != 0) {
4045 				if (lastcount <= c) {
4046 					totalcount -= lastcount;
4047 					lastlba = (uint64_t)-1;
4048 					lastcount = 0;
4049 					ranges--;
4050 				} else {
4051 					totalcount -= c;
4052 					lastlba -= c;
4053 					lastcount -= c;
4054 					scsi_ulto4b(lastcount,
4055 					    d[ranges - 1].length);
4056 				}
4057 			}
4058 			/* Align beginning of the new range. */
4059 			c = (lba - softc->unmap_gran_align) % softc->unmap_gran;
4060 			if (c != 0) {
4061 				c = softc->unmap_gran - c;
4062 				if (count <= c) {
4063 					count = 0;
4064 				} else {
4065 					lba += c;
4066 					count -= c;
4067 				}
4068 			}
4069 		}
4070 
4071 		while (count > 0) {
4072 			c = omin(count, UNMAP_RANGE_MAX);
4073 			if (totalcount + c > softc->unmap_max_lba ||
4074 			    ranges >= softc->unmap_max_ranges) {
4075 				xpt_print(periph->path,
4076 				    "%s issuing short delete %ld > %ld"
4077 				    "|| %d >= %d",
4078 				    da_delete_method_desc[softc->delete_method],
4079 				    totalcount + c, softc->unmap_max_lba,
4080 				    ranges, softc->unmap_max_ranges);
4081 				break;
4082 			}
4083 			scsi_u64to8b(lba, d[ranges].lba);
4084 			scsi_ulto4b(c, d[ranges].length);
4085 			lba += c;
4086 			totalcount += c;
4087 			ranges++;
4088 			count -= c;
4089 			lastlba = lba;
4090 			lastcount = c;
4091 		}
4092 		bp1 = cam_iosched_next_trim(softc->cam_iosched);
4093 		if (bp1 == NULL)
4094 			break;
4095 		if (ranges >= softc->unmap_max_ranges ||
4096 		    totalcount + bp1->bio_bcount /
4097 		    softc->params.secsize > softc->unmap_max_lba) {
4098 			cam_iosched_put_back_trim(softc->cam_iosched, bp1);
4099 			break;
4100 		}
4101 	} while (1);
4102 
4103 	/* Align length of the last range. */
4104 	if ((softc->quirks & DA_Q_STRICT_UNMAP) && softc->unmap_gran != 0 &&
4105 	    (c = lastcount % softc->unmap_gran) != 0) {
4106 		if (lastcount <= c)
4107 			ranges--;
4108 		else
4109 			scsi_ulto4b(lastcount - c, d[ranges - 1].length);
4110 	}
4111 
4112 	scsi_ulto2b(ranges * 16 + 6, &buf[0]);
4113 	scsi_ulto2b(ranges * 16, &buf[2]);
4114 
4115 	scsi_unmap(&ccb->csio,
4116 		   /*retries*/da_retry_count,
4117 		   /*cbfcnp*/dadone,
4118 		   /*tag_action*/MSG_SIMPLE_Q_TAG,
4119 		   /*byte2*/0,
4120 		   /*data_ptr*/ buf,
4121 		   /*dxfer_len*/ ranges * 16 + 8,
4122 		   /*sense_len*/SSD_FULL_SIZE,
4123 		   da_default_timeout * 1000);
4124 	ccb->ccb_h.ccb_state = DA_CCB_DELETE;
4125 	ccb->ccb_h.flags |= CAM_UNLOCKED;
4126 	softc->trim_count++;
4127 	softc->trim_ranges += ranges;
4128 	softc->trim_lbas += totalcount;
4129 	cam_iosched_submit_trim(softc->cam_iosched);
4130 }
4131 
4132 static void
4133 da_delete_trim(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
4134 {
4135 	struct da_softc *softc = (struct da_softc *)periph->softc;
4136 	struct bio *bp1;
4137 	uint8_t *buf = softc->unmap_buf;
4138 	uint64_t lastlba = (uint64_t)-1;
4139 	uint64_t count;
4140 	uint64_t lba;
4141 	uint32_t lastcount = 0, c, requestcount;
4142 	int ranges = 0, off, block_count;
4143 
4144 	bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
4145 	bp1 = bp;
4146 	do {
4147 		if (bp1 != bp)//XXX imp XXX
4148 			bioq_insert_tail(&softc->delete_run_queue, bp1);
4149 		lba = bp1->bio_pblkno;
4150 		count = bp1->bio_bcount / softc->params.secsize;
4151 		requestcount = count;
4152 
4153 		/* Try to extend the previous range. */
4154 		if (lba == lastlba) {
4155 			c = omin(count, ATA_DSM_RANGE_MAX - lastcount);
4156 			lastcount += c;
4157 			off = (ranges - 1) * 8;
4158 			buf[off + 6] = lastcount & 0xff;
4159 			buf[off + 7] = (lastcount >> 8) & 0xff;
4160 			count -= c;
4161 			lba += c;
4162 		}
4163 
4164 		while (count > 0) {
4165 			c = omin(count, ATA_DSM_RANGE_MAX);
4166 			off = ranges * 8;
4167 
4168 			buf[off + 0] = lba & 0xff;
4169 			buf[off + 1] = (lba >> 8) & 0xff;
4170 			buf[off + 2] = (lba >> 16) & 0xff;
4171 			buf[off + 3] = (lba >> 24) & 0xff;
4172 			buf[off + 4] = (lba >> 32) & 0xff;
4173 			buf[off + 5] = (lba >> 40) & 0xff;
4174 			buf[off + 6] = c & 0xff;
4175 			buf[off + 7] = (c >> 8) & 0xff;
4176 			lba += c;
4177 			ranges++;
4178 			count -= c;
4179 			lastcount = c;
4180 			if (count != 0 && ranges == softc->trim_max_ranges) {
4181 				xpt_print(periph->path,
4182 				    "%s issuing short delete %ld > %ld\n",
4183 				    da_delete_method_desc[softc->delete_method],
4184 				    requestcount,
4185 				    (softc->trim_max_ranges - ranges) *
4186 				    ATA_DSM_RANGE_MAX);
4187 				break;
4188 			}
4189 		}
4190 		lastlba = lba;
4191 		bp1 = cam_iosched_next_trim(softc->cam_iosched);
4192 		if (bp1 == NULL)
4193 			break;
4194 		if (bp1->bio_bcount / softc->params.secsize >
4195 		    (softc->trim_max_ranges - ranges) * ATA_DSM_RANGE_MAX) {
4196 			cam_iosched_put_back_trim(softc->cam_iosched, bp1);
4197 			break;
4198 		}
4199 	} while (1);
4200 
4201 	block_count = howmany(ranges, ATA_DSM_BLK_RANGES);
4202 	scsi_ata_trim(&ccb->csio,
4203 		      /*retries*/da_retry_count,
4204 		      /*cbfcnp*/dadone,
4205 		      /*tag_action*/MSG_SIMPLE_Q_TAG,
4206 		      block_count,
4207 		      /*data_ptr*/buf,
4208 		      /*dxfer_len*/block_count * ATA_DSM_BLK_SIZE,
4209 		      /*sense_len*/SSD_FULL_SIZE,
4210 		      da_default_timeout * 1000);
4211 	ccb->ccb_h.ccb_state = DA_CCB_DELETE;
4212 	ccb->ccb_h.flags |= CAM_UNLOCKED;
4213 	cam_iosched_submit_trim(softc->cam_iosched);
4214 }
4215 
4216 /*
4217  * We calculate ws_max_blks here based off d_delmaxsize instead
4218  * of using softc->ws_max_blks as it is absolute max for the
4219  * device not the protocol max which may well be lower.
4220  */
4221 static void
4222 da_delete_ws(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
4223 {
4224 	struct da_softc *softc;
4225 	struct bio *bp1;
4226 	uint64_t ws_max_blks;
4227 	uint64_t lba;
4228 	uint64_t count; /* forward compat with WS32 */
4229 
4230 	softc = (struct da_softc *)periph->softc;
4231 	ws_max_blks = softc->disk->d_delmaxsize / softc->params.secsize;
4232 	lba = bp->bio_pblkno;
4233 	count = 0;
4234 	bp1 = bp;
4235 	do {
4236 		if (bp1 != bp)//XXX imp XXX
4237 			bioq_insert_tail(&softc->delete_run_queue, bp1);
4238 		count += bp1->bio_bcount / softc->params.secsize;
4239 		if (count > ws_max_blks) {
4240 			xpt_print(periph->path,
4241 			    "%s issuing short delete %ld > %ld\n",
4242 			    da_delete_method_desc[softc->delete_method],
4243 			    count, ws_max_blks);
4244 			count = omin(count, ws_max_blks);
4245 			break;
4246 		}
4247 		bp1 = cam_iosched_next_trim(softc->cam_iosched);
4248 		if (bp1 == NULL)
4249 			break;
4250 		if (lba + count != bp1->bio_pblkno ||
4251 		    count + bp1->bio_bcount /
4252 		    softc->params.secsize > ws_max_blks) {
4253 			cam_iosched_put_back_trim(softc->cam_iosched, bp1);
4254 			break;
4255 		}
4256 	} while (1);
4257 
4258 	scsi_write_same(&ccb->csio,
4259 			/*retries*/da_retry_count,
4260 			/*cbfcnp*/dadone,
4261 			/*tag_action*/MSG_SIMPLE_Q_TAG,
4262 			/*byte2*/softc->delete_method ==
4263 			    DA_DELETE_ZERO ? 0 : SWS_UNMAP,
4264 			softc->delete_method == DA_DELETE_WS16 ? 16 : 10,
4265 			/*lba*/lba,
4266 			/*block_count*/count,
4267 			/*data_ptr*/ __DECONST(void *, zero_region),
4268 			/*dxfer_len*/ softc->params.secsize,
4269 			/*sense_len*/SSD_FULL_SIZE,
4270 			da_default_timeout * 1000);
4271 	ccb->ccb_h.ccb_state = DA_CCB_DELETE;
4272 	ccb->ccb_h.flags |= CAM_UNLOCKED;
4273 	cam_iosched_submit_trim(softc->cam_iosched);
4274 }
4275 
4276 static int
4277 cmd6workaround(union ccb *ccb)
4278 {
4279 	struct scsi_rw_6 cmd6;
4280 	struct scsi_rw_10 *cmd10;
4281 	struct da_softc *softc;
4282 	u_int8_t *cdb;
4283 	struct bio *bp;
4284 	int frozen;
4285 
4286 	cdb = ccb->csio.cdb_io.cdb_bytes;
4287 	softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
4288 
4289 	if (ccb->ccb_h.ccb_state == DA_CCB_DELETE) {
4290 		da_delete_methods old_method = softc->delete_method;
4291 
4292 		/*
4293 		 * Typically there are two reasons for failure here
4294 		 * 1. Delete method was detected as supported but isn't
4295 		 * 2. Delete failed due to invalid params e.g. too big
4296 		 *
4297 		 * While we will attempt to choose an alternative delete method
4298 		 * this may result in short deletes if the existing delete
4299 		 * requests from geom are big for the new method chosen.
4300 		 *
4301 		 * This method assumes that the error which triggered this
4302 		 * will not retry the io otherwise a panic will occur
4303 		 */
4304 		dadeleteflag(softc, old_method, 0);
4305 		dadeletemethodchoose(softc, DA_DELETE_DISABLE);
4306 		if (softc->delete_method == DA_DELETE_DISABLE)
4307 			xpt_print(ccb->ccb_h.path,
4308 				  "%s failed, disabling BIO_DELETE\n",
4309 				  da_delete_method_desc[old_method]);
4310 		else
4311 			xpt_print(ccb->ccb_h.path,
4312 				  "%s failed, switching to %s BIO_DELETE\n",
4313 				  da_delete_method_desc[old_method],
4314 				  da_delete_method_desc[softc->delete_method]);
4315 
4316 		while ((bp = bioq_takefirst(&softc->delete_run_queue)) != NULL)
4317 			cam_iosched_queue_work(softc->cam_iosched, bp);
4318 		cam_iosched_queue_work(softc->cam_iosched,
4319 		    (struct bio *)ccb->ccb_h.ccb_bp);
4320 		ccb->ccb_h.ccb_bp = NULL;
4321 		return (0);
4322 	}
4323 
4324 	/* Detect unsupported PREVENT ALLOW MEDIUM REMOVAL. */
4325 	if ((ccb->ccb_h.flags & CAM_CDB_POINTER) == 0 &&
4326 	    (*cdb == PREVENT_ALLOW) &&
4327 	    (softc->quirks & DA_Q_NO_PREVENT) == 0) {
4328 		if (bootverbose)
4329 			xpt_print(ccb->ccb_h.path,
4330 			    "PREVENT ALLOW MEDIUM REMOVAL not supported.\n");
4331 		softc->quirks |= DA_Q_NO_PREVENT;
4332 		return (0);
4333 	}
4334 
4335 	/* Detect unsupported SYNCHRONIZE CACHE(10). */
4336 	if ((ccb->ccb_h.flags & CAM_CDB_POINTER) == 0 &&
4337 	    (*cdb == SYNCHRONIZE_CACHE) &&
4338 	    (softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
4339 		if (bootverbose)
4340 			xpt_print(ccb->ccb_h.path,
4341 			    "SYNCHRONIZE CACHE(10) not supported.\n");
4342 		softc->quirks |= DA_Q_NO_SYNC_CACHE;
4343 		softc->disk->d_flags &= ~DISKFLAG_CANFLUSHCACHE;
4344 		return (0);
4345 	}
4346 
4347 	/* Translation only possible if CDB is an array and cmd is R/W6 */
4348 	if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
4349 	    (*cdb != READ_6 && *cdb != WRITE_6))
4350 		return 0;
4351 
4352 	xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
4353 	    "increasing minimum_cmd_size to 10.\n");
4354 	softc->minimum_cmd_size = 10;
4355 
4356 	bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
4357 	cmd10 = (struct scsi_rw_10 *)cdb;
4358 	cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
4359 	cmd10->byte2 = 0;
4360 	scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
4361 	cmd10->reserved = 0;
4362 	scsi_ulto2b(cmd6.length, cmd10->length);
4363 	cmd10->control = cmd6.control;
4364 	ccb->csio.cdb_len = sizeof(*cmd10);
4365 
4366 	/* Requeue request, unfreezing queue if necessary */
4367 	frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
4368 	ccb->ccb_h.status = CAM_REQUEUE_REQ;
4369 	xpt_action(ccb);
4370 	if (frozen) {
4371 		cam_release_devq(ccb->ccb_h.path,
4372 				 /*relsim_flags*/0,
4373 				 /*reduction*/0,
4374 				 /*timeout*/0,
4375 				 /*getcount_only*/0);
4376 	}
4377 	return (ERESTART);
4378 }
4379 
4380 static void
4381 dazonedone(struct cam_periph *periph, union ccb *ccb)
4382 {
4383 	struct da_softc *softc;
4384 	struct bio *bp;
4385 
4386 	softc = periph->softc;
4387 	bp = (struct bio *)ccb->ccb_h.ccb_bp;
4388 
4389 	switch (bp->bio_zone.zone_cmd) {
4390 	case DISK_ZONE_OPEN:
4391 	case DISK_ZONE_CLOSE:
4392 	case DISK_ZONE_FINISH:
4393 	case DISK_ZONE_RWP:
4394 		break;
4395 	case DISK_ZONE_REPORT_ZONES: {
4396 		uint32_t avail_len;
4397 		struct disk_zone_report *rep;
4398 		struct scsi_report_zones_hdr *hdr;
4399 		struct scsi_report_zones_desc *desc;
4400 		struct disk_zone_rep_entry *entry;
4401 		uint32_t hdr_len, num_avail;
4402 		uint32_t num_to_fill, i;
4403 		int ata;
4404 
4405 		rep = &bp->bio_zone.zone_params.report;
4406 		avail_len = ccb->csio.dxfer_len - ccb->csio.resid;
4407 		/*
4408 		 * Note that bio_resid isn't normally used for zone
4409 		 * commands, but it is used by devstat_end_transaction_bio()
4410 		 * to determine how much data was transferred.  Because
4411 		 * the size of the SCSI/ATA data structures is different
4412 		 * than the size of the BIO interface structures, the
4413 		 * amount of data actually transferred from the drive will
4414 		 * be different than the amount of data transferred to
4415 		 * the user.
4416 		 */
4417 		bp->bio_resid = ccb->csio.resid;
4418 		hdr = (struct scsi_report_zones_hdr *)ccb->csio.data_ptr;
4419 		if (avail_len < sizeof(*hdr)) {
4420 			/*
4421 			 * Is there a better error than EIO here?  We asked
4422 			 * for at least the header, and we got less than
4423 			 * that.
4424 			 */
4425 			bp->bio_error = EIO;
4426 			bp->bio_flags |= BIO_ERROR;
4427 			bp->bio_resid = bp->bio_bcount;
4428 			break;
4429 		}
4430 
4431 		if (softc->zone_interface == DA_ZONE_IF_ATA_PASS)
4432 			ata = 1;
4433 		else
4434 			ata = 0;
4435 
4436 		hdr_len = ata ? le32dec(hdr->length) :
4437 				scsi_4btoul(hdr->length);
4438 		if (hdr_len > 0)
4439 			rep->entries_available = hdr_len / sizeof(*desc);
4440 		else
4441 			rep->entries_available = 0;
4442 		/*
4443 		 * NOTE: using the same values for the BIO version of the
4444 		 * same field as the SCSI/ATA values.  This means we could
4445 		 * get some additional values that aren't defined in bio.h
4446 		 * if more values of the same field are defined later.
4447 		 */
4448 		rep->header.same = hdr->byte4 & SRZ_SAME_MASK;
4449 		rep->header.maximum_lba = ata ?  le64dec(hdr->maximum_lba) :
4450 					  scsi_8btou64(hdr->maximum_lba);
4451 		/*
4452 		 * If the drive reports no entries that match the query,
4453 		 * we're done.
4454 		 */
4455 		if (hdr_len == 0) {
4456 			rep->entries_filled = 0;
4457 			break;
4458 		}
4459 
4460 		num_avail = min((avail_len - sizeof(*hdr)) / sizeof(*desc),
4461 				hdr_len / sizeof(*desc));
4462 		/*
4463 		 * If the drive didn't return any data, then we're done.
4464 		 */
4465 		if (num_avail == 0) {
4466 			rep->entries_filled = 0;
4467 			break;
4468 		}
4469 
4470 		num_to_fill = min(num_avail, rep->entries_allocated);
4471 		/*
4472 		 * If the user didn't allocate any entries for us to fill,
4473 		 * we're done.
4474 		 */
4475 		if (num_to_fill == 0) {
4476 			rep->entries_filled = 0;
4477 			break;
4478 		}
4479 
4480 		for (i = 0, desc = &hdr->desc_list[0], entry=&rep->entries[0];
4481 		     i < num_to_fill; i++, desc++, entry++) {
4482 			/*
4483 			 * NOTE: we're mapping the values here directly
4484 			 * from the SCSI/ATA bit definitions to the bio.h
4485 			 * definitions. There is also a warning in
4486 			 * disk_zone.h, but the impact is that if
4487 			 * additional values are added in the SCSI/ATA
4488 			 * specs these will be visible to consumers of
4489 			 * this interface.
4490 			 */
4491 			entry->zone_type = desc->zone_type & SRZ_TYPE_MASK;
4492 			entry->zone_condition =
4493 			    (desc->zone_flags & SRZ_ZONE_COND_MASK) >>
4494 			    SRZ_ZONE_COND_SHIFT;
4495 			entry->zone_flags |= desc->zone_flags &
4496 			    (SRZ_ZONE_NON_SEQ|SRZ_ZONE_RESET);
4497 			entry->zone_length =
4498 			    ata ? le64dec(desc->zone_length) :
4499 				  scsi_8btou64(desc->zone_length);
4500 			entry->zone_start_lba =
4501 			    ata ? le64dec(desc->zone_start_lba) :
4502 				  scsi_8btou64(desc->zone_start_lba);
4503 			entry->write_pointer_lba =
4504 			    ata ? le64dec(desc->write_pointer_lba) :
4505 				  scsi_8btou64(desc->write_pointer_lba);
4506 		}
4507 		rep->entries_filled = num_to_fill;
4508 		break;
4509 	}
4510 	case DISK_ZONE_GET_PARAMS:
4511 	default:
4512 		/*
4513 		 * In theory we should not get a GET_PARAMS bio, since it
4514 		 * should be handled without queueing the command to the
4515 		 * drive.
4516 		 */
4517 		panic("%s: Invalid zone command %d", __func__,
4518 		    bp->bio_zone.zone_cmd);
4519 		break;
4520 	}
4521 
4522 	if (bp->bio_zone.zone_cmd == DISK_ZONE_REPORT_ZONES)
4523 		free(ccb->csio.data_ptr, M_SCSIDA);
4524 }
4525 
4526 static void
4527 dadone(struct cam_periph *periph, union ccb *done_ccb)
4528 {
4529 	struct bio *bp, *bp1;
4530 	struct da_softc *softc;
4531 	struct ccb_scsiio *csio;
4532 	da_ccb_state state;
4533 
4534 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone\n"));
4535 
4536 	softc = (struct da_softc *)periph->softc;
4537 	csio = &done_ccb->csio;
4538 
4539 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
4540 	if (csio->bio != NULL)
4541 		biotrack(csio->bio, __func__);
4542 #endif
4543 	state = csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK;
4544 
4545 	cam_periph_lock(periph);
4546 	bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
4547 	if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
4548 		int error;
4549 		int sf;
4550 
4551 		if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
4552 			sf = SF_RETRY_UA;
4553 		else
4554 			sf = 0;
4555 
4556 		error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
4557 		if (error == ERESTART) {
4558 			/* A retry was scheduled, so just return. */
4559 			cam_periph_unlock(periph);
4560 			return;
4561 		}
4562 		bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
4563 		if (error != 0) {
4564 			int queued_error;
4565 
4566 			/*
4567 			 * return all queued I/O with EIO, so that
4568 			 * the client can retry these I/Os in the
4569 			 * proper order should it attempt to recover.
4570 			 */
4571 			queued_error = EIO;
4572 
4573 			if (error == ENXIO
4574 			 && (softc->flags & DA_FLAG_PACK_INVALID)== 0) {
4575 				/*
4576 				 * Catastrophic error.  Mark our pack as
4577 				 * invalid.
4578 				 *
4579 				 * XXX See if this is really a media
4580 				 * XXX change first?
4581 				 */
4582 				xpt_print(periph->path, "Invalidating pack\n");
4583 				softc->flags |= DA_FLAG_PACK_INVALID;
4584 #ifdef CAM_IO_STATS
4585 				softc->invalidations++;
4586 #endif
4587 				queued_error = ENXIO;
4588 			}
4589 			cam_iosched_flush(softc->cam_iosched, NULL,
4590 			   queued_error);
4591 			if (bp != NULL) {
4592 				bp->bio_error = error;
4593 				bp->bio_resid = bp->bio_bcount;
4594 				bp->bio_flags |= BIO_ERROR;
4595 			}
4596 		} else if (bp != NULL) {
4597 			if (state == DA_CCB_DELETE)
4598 				bp->bio_resid = 0;
4599 			else
4600 				bp->bio_resid = csio->resid;
4601 			bp->bio_error = 0;
4602 			if (bp->bio_resid != 0)
4603 				bp->bio_flags |= BIO_ERROR;
4604 		}
4605 		if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
4606 			cam_release_devq(done_ccb->ccb_h.path,
4607 					 /*relsim_flags*/0,
4608 					 /*reduction*/0,
4609 					 /*timeout*/0,
4610 					 /*getcount_only*/0);
4611 	} else if (bp != NULL) {
4612 		if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
4613 			panic("REQ_CMP with QFRZN");
4614 		if (bp->bio_cmd == BIO_ZONE)
4615 			dazonedone(periph, done_ccb);
4616 		else if (state == DA_CCB_DELETE)
4617 			bp->bio_resid = 0;
4618 		else
4619 			bp->bio_resid = csio->resid;
4620 		if ((csio->resid > 0) && (bp->bio_cmd != BIO_ZONE))
4621 			bp->bio_flags |= BIO_ERROR;
4622 		if (softc->error_inject != 0) {
4623 			bp->bio_error = softc->error_inject;
4624 			bp->bio_resid = bp->bio_bcount;
4625 			bp->bio_flags |= BIO_ERROR;
4626 			softc->error_inject = 0;
4627 		}
4628 	}
4629 
4630 	if (bp != NULL)
4631 		biotrack(bp, __func__);
4632 	LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
4633 	if (LIST_EMPTY(&softc->pending_ccbs))
4634 		softc->flags |= DA_FLAG_WAS_OTAG;
4635 
4636 	/*
4637 	 * We need to call cam_iosched before we call biodone so that we don't
4638 	 * measure any activity that happens in the completion routine, which in
4639 	 * the case of sendfile can be quite extensive. Release the periph
4640 	 * refcount taken in dastart() for each CCB.
4641 	 */
4642 	cam_iosched_bio_complete(softc->cam_iosched, bp, done_ccb);
4643 	xpt_release_ccb(done_ccb);
4644 	KASSERT(softc->refcount >= 1, ("dadone softc %p refcount %d", softc, softc->refcount));
4645 	softc->refcount--;
4646 	if (state == DA_CCB_DELETE) {
4647 		TAILQ_HEAD(, bio) queue;
4648 
4649 		TAILQ_INIT(&queue);
4650 		TAILQ_CONCAT(&queue, &softc->delete_run_queue.queue, bio_queue);
4651 		softc->delete_run_queue.insert_point = NULL;
4652 		/*
4653 		 * Normally, the xpt_release_ccb() above would make sure
4654 		 * that when we have more work to do, that work would
4655 		 * get kicked off. However, we specifically keep
4656 		 * delete_running set to 0 before the call above to
4657 		 * allow other I/O to progress when many BIO_DELETE
4658 		 * requests are pushed down. We set delete_running to 0
4659 		 * and call daschedule again so that we don't stall if
4660 		 * there are no other I/Os pending apart from BIO_DELETEs.
4661 		 */
4662 		cam_iosched_trim_done(softc->cam_iosched);
4663 		daschedule(periph);
4664 		cam_periph_unlock(periph);
4665 		while ((bp1 = TAILQ_FIRST(&queue)) != NULL) {
4666 			TAILQ_REMOVE(&queue, bp1, bio_queue);
4667 			bp1->bio_error = bp->bio_error;
4668 			if (bp->bio_flags & BIO_ERROR) {
4669 				bp1->bio_flags |= BIO_ERROR;
4670 				bp1->bio_resid = bp1->bio_bcount;
4671 			} else
4672 				bp1->bio_resid = 0;
4673 			biodone(bp1);
4674 		}
4675 	} else {
4676 		daschedule(periph);
4677 		cam_periph_unlock(periph);
4678 	}
4679 	if (bp != NULL)
4680 		biodone(bp);
4681 	return;
4682 }
4683 
4684 static void
4685 dadone_probewp(struct cam_periph *periph, union ccb *done_ccb)
4686 {
4687 	struct scsi_mode_header_6 *mode_hdr6;
4688 	struct scsi_mode_header_10 *mode_hdr10;
4689 	struct da_softc *softc;
4690 	struct ccb_scsiio *csio;
4691 	u_int32_t  priority;
4692 	uint8_t dev_spec;
4693 
4694 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone_probewp\n"));
4695 
4696 	softc = (struct da_softc *)periph->softc;
4697 	priority = done_ccb->ccb_h.pinfo.priority;
4698 	csio = &done_ccb->csio;
4699 
4700 	cam_periph_assert(periph, MA_OWNED);
4701 
4702 	KASSERT(softc->state == DA_STATE_PROBE_WP,
4703 	    ("State (%d) not PROBE_WP in dadone_probewp, periph %p ccb %p",
4704 		softc->state, periph, done_ccb));
4705         KASSERT((csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) == DA_CCB_PROBE_WP,
4706 	    ("CCB State (%lu) not PROBE_WP in dadone_probewp, periph %p ccb %p",
4707 		(unsigned long)csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK, periph,
4708 		done_ccb));
4709 
4710 	if (softc->minimum_cmd_size > 6) {
4711 		mode_hdr10 = (struct scsi_mode_header_10 *)csio->data_ptr;
4712 		dev_spec = mode_hdr10->dev_spec;
4713 	} else {
4714 		mode_hdr6 = (struct scsi_mode_header_6 *)csio->data_ptr;
4715 		dev_spec = mode_hdr6->dev_spec;
4716 	}
4717 	if (cam_ccb_status(done_ccb) == CAM_REQ_CMP) {
4718 		if ((dev_spec & 0x80) != 0)
4719 			softc->disk->d_flags |= DISKFLAG_WRITE_PROTECT;
4720 		else
4721 			softc->disk->d_flags &= ~DISKFLAG_WRITE_PROTECT;
4722 	} else {
4723 		int error;
4724 
4725 		error = daerror(done_ccb, CAM_RETRY_SELTO,
4726 				SF_RETRY_UA|SF_NO_PRINT);
4727 		if (error == ERESTART)
4728 			return;
4729 		else if (error != 0) {
4730 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
4731 				/* Don't wedge this device's queue */
4732 				cam_release_devq(done_ccb->ccb_h.path,
4733 						 /*relsim_flags*/0,
4734 						 /*reduction*/0,
4735 						 /*timeout*/0,
4736 						 /*getcount_only*/0);
4737 			}
4738 		}
4739 	}
4740 
4741 	free(csio->data_ptr, M_SCSIDA);
4742 	if ((softc->flags & DA_FLAG_CAN_RC16) != 0)
4743 		softc->state = DA_STATE_PROBE_RC16;
4744 	else
4745 		softc->state = DA_STATE_PROBE_RC;
4746 	xpt_release_ccb(done_ccb);
4747 	xpt_schedule(periph, priority);
4748 	return;
4749 }
4750 
4751 static void
4752 dadone_proberc(struct cam_periph *periph, union ccb *done_ccb)
4753 {
4754 	struct scsi_read_capacity_data *rdcap;
4755 	struct scsi_read_capacity_data_long *rcaplong;
4756 	struct da_softc *softc;
4757 	struct ccb_scsiio *csio;
4758 	da_ccb_state state;
4759 	char *announce_buf;
4760 	u_int32_t  priority;
4761 	int lbp, n;
4762 
4763 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone_proberc\n"));
4764 
4765 	softc = (struct da_softc *)periph->softc;
4766 	priority = done_ccb->ccb_h.pinfo.priority;
4767 	csio = &done_ccb->csio;
4768 	state = csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK;
4769 
4770 	KASSERT(softc->state == DA_STATE_PROBE_RC || softc->state == DA_STATE_PROBE_RC16,
4771 	    ("State (%d) not PROBE_RC* in dadone_proberc, periph %p ccb %p",
4772 		softc->state, periph, done_ccb));
4773 	KASSERT(state == DA_CCB_PROBE_RC || state == DA_CCB_PROBE_RC16,
4774 	    ("CCB State (%lu) not PROBE_RC* in dadone_probewp, periph %p ccb %p",
4775 		(unsigned long)state, periph, done_ccb));
4776 
4777 	lbp = 0;
4778 	rdcap = NULL;
4779 	rcaplong = NULL;
4780 	/* XXX TODO: can this be a malloc? */
4781 	announce_buf = softc->announce_temp;
4782 	bzero(announce_buf, DA_ANNOUNCETMP_SZ);
4783 
4784 	if (state == DA_CCB_PROBE_RC)
4785 		rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
4786 	else
4787 		rcaplong = (struct scsi_read_capacity_data_long *)
4788 			csio->data_ptr;
4789 
4790 	cam_periph_assert(periph, MA_OWNED);
4791 
4792 	if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
4793 		struct disk_params *dp;
4794 		uint32_t block_size;
4795 		uint64_t maxsector;
4796 		u_int lalba;	/* Lowest aligned LBA. */
4797 
4798 		if (state == DA_CCB_PROBE_RC) {
4799 			block_size = scsi_4btoul(rdcap->length);
4800 			maxsector = scsi_4btoul(rdcap->addr);
4801 			lalba = 0;
4802 
4803 			/*
4804 			 * According to SBC-2, if the standard 10
4805 			 * byte READ CAPACITY command returns 2^32,
4806 			 * we should issue the 16 byte version of
4807 			 * the command, since the device in question
4808 			 * has more sectors than can be represented
4809 			 * with the short version of the command.
4810 			 */
4811 			if (maxsector == 0xffffffff) {
4812 				free(rdcap, M_SCSIDA);
4813 				softc->state = DA_STATE_PROBE_RC16;
4814 				xpt_release_ccb(done_ccb);
4815 				xpt_schedule(periph, priority);
4816 				return;
4817 			}
4818 		} else {
4819 			block_size = scsi_4btoul(rcaplong->length);
4820 			maxsector = scsi_8btou64(rcaplong->addr);
4821 			lalba = scsi_2btoul(rcaplong->lalba_lbp);
4822 		}
4823 
4824 		/*
4825 		 * Because GEOM code just will panic us if we
4826 		 * give them an 'illegal' value we'll avoid that
4827 		 * here.
4828 		 */
4829 		if (block_size == 0) {
4830 			block_size = 512;
4831 			if (maxsector == 0)
4832 				maxsector = -1;
4833 		}
4834 		if (block_size >= maxphys) {
4835 			xpt_print(periph->path,
4836 			    "unsupportable block size %ju\n",
4837 			    (uintmax_t) block_size);
4838 			announce_buf = NULL;
4839 			cam_periph_invalidate(periph);
4840 		} else {
4841 			/*
4842 			 * We pass rcaplong into dasetgeom(),
4843 			 * because it will only use it if it is
4844 			 * non-NULL.
4845 			 */
4846 			dasetgeom(periph, block_size, maxsector,
4847 				  rcaplong, sizeof(*rcaplong));
4848 			lbp = (lalba & SRC16_LBPME_A);
4849 			dp = &softc->params;
4850 			n = snprintf(announce_buf, DA_ANNOUNCETMP_SZ,
4851 			    "%juMB (%ju %u byte sectors",
4852 			    ((uintmax_t)dp->secsize * dp->sectors) /
4853 			     (1024 * 1024),
4854 			    (uintmax_t)dp->sectors, dp->secsize);
4855 			if (softc->p_type != 0) {
4856 				n += snprintf(announce_buf + n,
4857 				    DA_ANNOUNCETMP_SZ - n,
4858 				    ", DIF type %d", softc->p_type);
4859 			}
4860 			snprintf(announce_buf + n, DA_ANNOUNCETMP_SZ - n, ")");
4861 		}
4862 	} else {
4863 		int error;
4864 
4865 		/*
4866 		 * Retry any UNIT ATTENTION type errors.  They
4867 		 * are expected at boot.
4868 		 */
4869 		error = daerror(done_ccb, CAM_RETRY_SELTO,
4870 				SF_RETRY_UA|SF_NO_PRINT);
4871 		if (error == ERESTART) {
4872 			/*
4873 			 * A retry was scheuled, so
4874 			 * just return.
4875 			 */
4876 			return;
4877 		} else if (error != 0) {
4878 			int asc, ascq;
4879 			int sense_key, error_code;
4880 			int have_sense;
4881 			cam_status status;
4882 			struct ccb_getdev cgd;
4883 
4884 			/* Don't wedge this device's queue */
4885 			status = done_ccb->ccb_h.status;
4886 			if ((status & CAM_DEV_QFRZN) != 0)
4887 				cam_release_devq(done_ccb->ccb_h.path,
4888 						 /*relsim_flags*/0,
4889 						 /*reduction*/0,
4890 						 /*timeout*/0,
4891 						 /*getcount_only*/0);
4892 
4893 			memset(&cgd, 0, sizeof(cgd));
4894 			xpt_setup_ccb(&cgd.ccb_h, done_ccb->ccb_h.path,
4895 				      CAM_PRIORITY_NORMAL);
4896 			cgd.ccb_h.func_code = XPT_GDEV_TYPE;
4897 			xpt_action((union ccb *)&cgd);
4898 
4899 			if (scsi_extract_sense_ccb(done_ccb,
4900 			    &error_code, &sense_key, &asc, &ascq))
4901 				have_sense = TRUE;
4902 			else
4903 				have_sense = FALSE;
4904 
4905 			/*
4906 			 * If we tried READ CAPACITY(16) and failed,
4907 			 * fallback to READ CAPACITY(10).
4908 			 */
4909 			if ((state == DA_CCB_PROBE_RC16) &&
4910 			    (softc->flags & DA_FLAG_CAN_RC16) &&
4911 			    (((csio->ccb_h.status & CAM_STATUS_MASK) ==
4912 				CAM_REQ_INVALID) ||
4913 			     ((have_sense) &&
4914 			      (error_code == SSD_CURRENT_ERROR ||
4915 			       error_code == SSD_DESC_CURRENT_ERROR) &&
4916 			      (sense_key == SSD_KEY_ILLEGAL_REQUEST)))) {
4917 				cam_periph_assert(periph, MA_OWNED);
4918 				softc->flags &= ~DA_FLAG_CAN_RC16;
4919 				free(rdcap, M_SCSIDA);
4920 				softc->state = DA_STATE_PROBE_RC;
4921 				xpt_release_ccb(done_ccb);
4922 				xpt_schedule(periph, priority);
4923 				return;
4924 			}
4925 
4926 			/*
4927 			 * Attach to anything that claims to be a
4928 			 * direct access or optical disk device,
4929 			 * as long as it doesn't return a "Logical
4930 			 * unit not supported" (0x25) error.
4931 			 * "Internal Target Failure" (0x44) is also
4932 			 * special and typically means that the
4933 			 * device is a SATA drive behind a SATL
4934 			 * translation that's fallen into a
4935 			 * terminally fatal state.
4936 			 */
4937 			if ((have_sense)
4938 			 && (asc != 0x25) && (asc != 0x44)
4939 			 && (error_code == SSD_CURRENT_ERROR
4940 			  || error_code == SSD_DESC_CURRENT_ERROR)) {
4941 				const char *sense_key_desc;
4942 				const char *asc_desc;
4943 
4944 				dasetgeom(periph, 512, -1, NULL, 0);
4945 				scsi_sense_desc(sense_key, asc, ascq,
4946 						&cgd.inq_data, &sense_key_desc,
4947 						&asc_desc);
4948 				snprintf(announce_buf, DA_ANNOUNCETMP_SZ,
4949 				    "Attempt to query device "
4950 				    "size failed: %s, %s",
4951 				    sense_key_desc, asc_desc);
4952 			} else {
4953 				if (have_sense)
4954 					scsi_sense_print(&done_ccb->csio);
4955 				else {
4956 					xpt_print(periph->path,
4957 					    "got CAM status %#x\n",
4958 					    done_ccb->ccb_h.status);
4959 				}
4960 
4961 				xpt_print(periph->path, "fatal error, "
4962 				    "failed to attach to device\n");
4963 
4964 				announce_buf = NULL;
4965 
4966 				/*
4967 				 * Free up resources.
4968 				 */
4969 				cam_periph_invalidate(periph);
4970 			}
4971 		}
4972 	}
4973 	free(csio->data_ptr, M_SCSIDA);
4974 	if (announce_buf != NULL &&
4975 	    ((softc->flags & DA_FLAG_ANNOUNCED) == 0)) {
4976 		struct sbuf sb;
4977 
4978 		sbuf_new(&sb, softc->announcebuf, DA_ANNOUNCE_SZ,
4979 		    SBUF_FIXEDLEN);
4980 		xpt_announce_periph_sbuf(periph, &sb, announce_buf);
4981 		xpt_announce_quirks_sbuf(periph, &sb, softc->quirks,
4982 		    DA_Q_BIT_STRING);
4983 		sbuf_finish(&sb);
4984 		sbuf_putbuf(&sb);
4985 
4986 		/*
4987 		 * Create our sysctl variables, now that we know
4988 		 * we have successfully attached.
4989 		 */
4990 		/* increase the refcount */
4991 		if (da_periph_acquire(periph, DA_REF_SYSCTL) == 0) {
4992 			taskqueue_enqueue(taskqueue_thread,
4993 					  &softc->sysctl_task);
4994 		} else {
4995 			/* XXX This message is useless! */
4996 			xpt_print(periph->path, "fatal error, "
4997 			    "could not acquire reference count\n");
4998 		}
4999 	}
5000 
5001 	/* We already probed the device. */
5002 	if (softc->flags & DA_FLAG_PROBED) {
5003 		daprobedone(periph, done_ccb);
5004 		return;
5005 	}
5006 
5007 	/* Ensure re-probe doesn't see old delete. */
5008 	softc->delete_available = 0;
5009 	dadeleteflag(softc, DA_DELETE_ZERO, 1);
5010 	if (lbp && (softc->quirks & DA_Q_NO_UNMAP) == 0) {
5011 		/*
5012 		 * Based on older SBC-3 spec revisions
5013 		 * any of the UNMAP methods "may" be
5014 		 * available via LBP given this flag so
5015 		 * we flag all of them as available and
5016 		 * then remove those which further
5017 		 * probes confirm aren't available
5018 		 * later.
5019 		 *
5020 		 * We could also check readcap(16) p_type
5021 		 * flag to exclude one or more invalid
5022 		 * write same (X) types here
5023 		 */
5024 		dadeleteflag(softc, DA_DELETE_WS16, 1);
5025 		dadeleteflag(softc, DA_DELETE_WS10, 1);
5026 		dadeleteflag(softc, DA_DELETE_UNMAP, 1);
5027 
5028 		softc->state = DA_STATE_PROBE_LBP;
5029 		xpt_release_ccb(done_ccb);
5030 		xpt_schedule(periph, priority);
5031 		return;
5032 	}
5033 
5034 	softc->state = DA_STATE_PROBE_BDC;
5035 	xpt_release_ccb(done_ccb);
5036 	xpt_schedule(periph, priority);
5037 	return;
5038 }
5039 
5040 static void
5041 dadone_probelbp(struct cam_periph *periph, union ccb *done_ccb)
5042 {
5043 	struct scsi_vpd_logical_block_prov *lbp;
5044 	struct da_softc *softc;
5045 	struct ccb_scsiio *csio;
5046 	u_int32_t  priority;
5047 
5048 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone_probelbp\n"));
5049 
5050 	softc = (struct da_softc *)periph->softc;
5051 	priority = done_ccb->ccb_h.pinfo.priority;
5052 	csio = &done_ccb->csio;
5053 	lbp = (struct scsi_vpd_logical_block_prov *)csio->data_ptr;
5054 
5055 	cam_periph_assert(periph, MA_OWNED);
5056 
5057 	if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5058 		/*
5059 		 * T10/1799-D Revision 31 states at least one of these
5060 		 * must be supported but we don't currently enforce this.
5061 		 */
5062 		dadeleteflag(softc, DA_DELETE_WS16,
5063 		     (lbp->flags & SVPD_LBP_WS16));
5064 		dadeleteflag(softc, DA_DELETE_WS10,
5065 			     (lbp->flags & SVPD_LBP_WS10));
5066 		dadeleteflag(softc, DA_DELETE_UNMAP,
5067 			     (lbp->flags & SVPD_LBP_UNMAP));
5068 	} else {
5069 		int error;
5070 		error = daerror(done_ccb, CAM_RETRY_SELTO,
5071 				SF_RETRY_UA|SF_NO_PRINT);
5072 		if (error == ERESTART)
5073 			return;
5074 		else if (error != 0) {
5075 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5076 				/* Don't wedge this device's queue */
5077 				cam_release_devq(done_ccb->ccb_h.path,
5078 						 /*relsim_flags*/0,
5079 						 /*reduction*/0,
5080 						 /*timeout*/0,
5081 						 /*getcount_only*/0);
5082 			}
5083 
5084 			/*
5085 			 * Failure indicates we don't support any SBC-3
5086 			 * delete methods with UNMAP
5087 			 */
5088 		}
5089 	}
5090 
5091 	free(lbp, M_SCSIDA);
5092 	softc->state = DA_STATE_PROBE_BLK_LIMITS;
5093 	xpt_release_ccb(done_ccb);
5094 	xpt_schedule(periph, priority);
5095 	return;
5096 }
5097 
5098 static void
5099 dadone_probeblklimits(struct cam_periph *periph, union ccb *done_ccb)
5100 {
5101 	struct scsi_vpd_block_limits *block_limits;
5102 	struct da_softc *softc;
5103 	struct ccb_scsiio *csio;
5104 	u_int32_t  priority;
5105 
5106 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone_probeblklimits\n"));
5107 
5108 	softc = (struct da_softc *)periph->softc;
5109 	priority = done_ccb->ccb_h.pinfo.priority;
5110 	csio = &done_ccb->csio;
5111 	block_limits = (struct scsi_vpd_block_limits *)csio->data_ptr;
5112 
5113 	cam_periph_assert(periph, MA_OWNED);
5114 
5115 	if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5116 		uint32_t max_txfer_len = scsi_4btoul(
5117 			block_limits->max_txfer_len);
5118 		uint32_t max_unmap_lba_cnt = scsi_4btoul(
5119 			block_limits->max_unmap_lba_cnt);
5120 		uint32_t max_unmap_blk_cnt = scsi_4btoul(
5121 			block_limits->max_unmap_blk_cnt);
5122 		uint32_t unmap_gran = scsi_4btoul(
5123 			block_limits->opt_unmap_grain);
5124 		uint32_t unmap_gran_align = scsi_4btoul(
5125 			block_limits->unmap_grain_align);
5126 		uint64_t ws_max_blks = scsi_8btou64(
5127 			block_limits->max_write_same_length);
5128 
5129 		if (max_txfer_len != 0) {
5130 			softc->disk->d_maxsize = MIN(softc->maxio,
5131 			    (off_t)max_txfer_len * softc->params.secsize);
5132 		}
5133 
5134 		/*
5135 		 * We should already support UNMAP but we check lba
5136 		 * and block count to be sure
5137 		 */
5138 		if (max_unmap_lba_cnt != 0x00L &&
5139 		    max_unmap_blk_cnt != 0x00L) {
5140 			softc->unmap_max_lba = max_unmap_lba_cnt;
5141 			softc->unmap_max_ranges = min(max_unmap_blk_cnt,
5142 				UNMAP_MAX_RANGES);
5143 			if (unmap_gran > 1) {
5144 				softc->unmap_gran = unmap_gran;
5145 				if (unmap_gran_align & 0x80000000) {
5146 					softc->unmap_gran_align =
5147 					    unmap_gran_align & 0x7fffffff;
5148 				}
5149 			}
5150 		} else {
5151 			/*
5152 			 * Unexpected UNMAP limits which means the
5153 			 * device doesn't actually support UNMAP
5154 			 */
5155 			dadeleteflag(softc, DA_DELETE_UNMAP, 0);
5156 		}
5157 
5158 		if (ws_max_blks != 0x00L)
5159 			softc->ws_max_blks = ws_max_blks;
5160 	} else {
5161 		int error;
5162 		error = daerror(done_ccb, CAM_RETRY_SELTO,
5163 				SF_RETRY_UA|SF_NO_PRINT);
5164 		if (error == ERESTART)
5165 			return;
5166 		else if (error != 0) {
5167 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5168 				/* Don't wedge this device's queue */
5169 				cam_release_devq(done_ccb->ccb_h.path,
5170 						 /*relsim_flags*/0,
5171 						 /*reduction*/0,
5172 						 /*timeout*/0,
5173 						 /*getcount_only*/0);
5174 			}
5175 
5176 			/*
5177 			 * Failure here doesn't mean UNMAP is not
5178 			 * supported as this is an optional page.
5179 			 */
5180 			softc->unmap_max_lba = 1;
5181 			softc->unmap_max_ranges = 1;
5182 		}
5183 	}
5184 
5185 	free(block_limits, M_SCSIDA);
5186 	softc->state = DA_STATE_PROBE_BDC;
5187 	xpt_release_ccb(done_ccb);
5188 	xpt_schedule(periph, priority);
5189 	return;
5190 }
5191 
5192 static void
5193 dadone_probebdc(struct cam_periph *periph, union ccb *done_ccb)
5194 {
5195 	struct scsi_vpd_block_device_characteristics *bdc;
5196 	struct da_softc *softc;
5197 	struct ccb_scsiio *csio;
5198 	u_int32_t  priority;
5199 
5200 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone_probebdc\n"));
5201 
5202 	softc = (struct da_softc *)periph->softc;
5203 	priority = done_ccb->ccb_h.pinfo.priority;
5204 	csio = &done_ccb->csio;
5205 	bdc = (struct scsi_vpd_block_device_characteristics *)csio->data_ptr;
5206 
5207 	cam_periph_assert(periph, MA_OWNED);
5208 
5209 	if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5210 		uint32_t valid_len;
5211 
5212 		/*
5213 		 * Disable queue sorting for non-rotational media
5214 		 * by default.
5215 		 */
5216 		u_int16_t old_rate = softc->disk->d_rotation_rate;
5217 
5218 		valid_len = csio->dxfer_len - csio->resid;
5219 		if (SBDC_IS_PRESENT(bdc, valid_len,
5220 		    medium_rotation_rate)) {
5221 			softc->disk->d_rotation_rate =
5222 				scsi_2btoul(bdc->medium_rotation_rate);
5223 			if (softc->disk->d_rotation_rate == SVPD_NON_ROTATING) {
5224 				cam_iosched_set_sort_queue(
5225 				    softc->cam_iosched, 0);
5226 				softc->flags &= ~DA_FLAG_ROTATING;
5227 			}
5228 			if (softc->disk->d_rotation_rate != old_rate) {
5229 				disk_attr_changed(softc->disk,
5230 				    "GEOM::rotation_rate", M_NOWAIT);
5231 			}
5232 		}
5233 		if ((SBDC_IS_PRESENT(bdc, valid_len, flags))
5234 		 && (softc->zone_mode == DA_ZONE_NONE)) {
5235 			int ata_proto;
5236 
5237 			if (scsi_vpd_supported_page(periph,
5238 			    SVPD_ATA_INFORMATION))
5239 				ata_proto = 1;
5240 			else
5241 				ata_proto = 0;
5242 
5243 			/*
5244 			 * The Zoned field will only be set for
5245 			 * Drive Managed and Host Aware drives.  If
5246 			 * they are Host Managed, the device type
5247 			 * in the standard INQUIRY data should be
5248 			 * set to T_ZBC_HM (0x14).
5249 			 */
5250 			if ((bdc->flags & SVPD_ZBC_MASK) ==
5251 			     SVPD_HAW_ZBC) {
5252 				softc->zone_mode = DA_ZONE_HOST_AWARE;
5253 				softc->zone_interface = (ata_proto) ?
5254 				   DA_ZONE_IF_ATA_SAT : DA_ZONE_IF_SCSI;
5255 			} else if ((bdc->flags & SVPD_ZBC_MASK) ==
5256 			     SVPD_DM_ZBC) {
5257 				softc->zone_mode =DA_ZONE_DRIVE_MANAGED;
5258 				softc->zone_interface = (ata_proto) ?
5259 				   DA_ZONE_IF_ATA_SAT : DA_ZONE_IF_SCSI;
5260 			} else if ((bdc->flags & SVPD_ZBC_MASK) !=
5261 				  SVPD_ZBC_NR) {
5262 				xpt_print(periph->path, "Unknown zoned "
5263 				    "type %#x",
5264 				    bdc->flags & SVPD_ZBC_MASK);
5265 			}
5266 		}
5267 	} else {
5268 		int error;
5269 		error = daerror(done_ccb, CAM_RETRY_SELTO,
5270 				SF_RETRY_UA|SF_NO_PRINT);
5271 		if (error == ERESTART)
5272 			return;
5273 		else if (error != 0) {
5274 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5275 				/* Don't wedge this device's queue */
5276 				cam_release_devq(done_ccb->ccb_h.path,
5277 						 /*relsim_flags*/0,
5278 						 /*reduction*/0,
5279 						 /*timeout*/0,
5280 						 /*getcount_only*/0);
5281 			}
5282 		}
5283 	}
5284 
5285 	free(bdc, M_SCSIDA);
5286 	softc->state = DA_STATE_PROBE_ATA;
5287 	xpt_release_ccb(done_ccb);
5288 	xpt_schedule(periph, priority);
5289 	return;
5290 }
5291 
5292 static void
5293 dadone_probeata(struct cam_periph *periph, union ccb *done_ccb)
5294 {
5295 	struct ata_params *ata_params;
5296 	struct ccb_scsiio *csio;
5297 	struct da_softc *softc;
5298 	u_int32_t  priority;
5299 	int continue_probe;
5300 	int error;
5301 
5302 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone_probeata\n"));
5303 
5304 	softc = (struct da_softc *)periph->softc;
5305 	priority = done_ccb->ccb_h.pinfo.priority;
5306 	csio = &done_ccb->csio;
5307 	ata_params = (struct ata_params *)csio->data_ptr;
5308 	continue_probe = 0;
5309 	error = 0;
5310 
5311 	cam_periph_assert(periph, MA_OWNED);
5312 
5313 	if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5314 		uint16_t old_rate;
5315 
5316 		ata_param_fixup(ata_params);
5317 		if (ata_params->support_dsm & ATA_SUPPORT_DSM_TRIM &&
5318 		    (softc->quirks & DA_Q_NO_UNMAP) == 0) {
5319 			dadeleteflag(softc, DA_DELETE_ATA_TRIM, 1);
5320 			if (ata_params->max_dsm_blocks != 0)
5321 				softc->trim_max_ranges = min(
5322 				  softc->trim_max_ranges,
5323 				  ata_params->max_dsm_blocks *
5324 				  ATA_DSM_BLK_RANGES);
5325 		}
5326 		/*
5327 		 * Disable queue sorting for non-rotational media
5328 		 * by default.
5329 		 */
5330 		old_rate = softc->disk->d_rotation_rate;
5331 		softc->disk->d_rotation_rate = ata_params->media_rotation_rate;
5332 		if (softc->disk->d_rotation_rate == ATA_RATE_NON_ROTATING) {
5333 			cam_iosched_set_sort_queue(softc->cam_iosched, 0);
5334 			softc->flags &= ~DA_FLAG_ROTATING;
5335 		}
5336 		if (softc->disk->d_rotation_rate != old_rate) {
5337 			disk_attr_changed(softc->disk,
5338 			    "GEOM::rotation_rate", M_NOWAIT);
5339 		}
5340 
5341 		cam_periph_assert(periph, MA_OWNED);
5342 		if (ata_params->capabilities1 & ATA_SUPPORT_DMA)
5343 			softc->flags |= DA_FLAG_CAN_ATA_DMA;
5344 
5345 		if (ata_params->support.extension & ATA_SUPPORT_GENLOG)
5346 			softc->flags |= DA_FLAG_CAN_ATA_LOG;
5347 
5348 		/*
5349 		 * At this point, if we have a SATA host aware drive,
5350 		 * we communicate via ATA passthrough unless the
5351 		 * SAT layer supports ZBC -> ZAC translation.  In
5352 		 * that case,
5353 		 *
5354 		 * XXX KDM figure out how to detect a host managed
5355 		 * SATA drive.
5356 		 */
5357 		if (softc->zone_mode == DA_ZONE_NONE) {
5358 			/*
5359 			 * Note that we don't override the zone
5360 			 * mode or interface if it has already been
5361 			 * set.  This is because it has either been
5362 			 * set as a quirk, or when we probed the
5363 			 * SCSI Block Device Characteristics page,
5364 			 * the zoned field was set.  The latter
5365 			 * means that the SAT layer supports ZBC to
5366 			 * ZAC translation, and we would prefer to
5367 			 * use that if it is available.
5368 			 */
5369 			if ((ata_params->support3 &
5370 			    ATA_SUPPORT_ZONE_MASK) ==
5371 			    ATA_SUPPORT_ZONE_HOST_AWARE) {
5372 				softc->zone_mode = DA_ZONE_HOST_AWARE;
5373 				softc->zone_interface =
5374 				    DA_ZONE_IF_ATA_PASS;
5375 			} else if ((ata_params->support3 &
5376 				    ATA_SUPPORT_ZONE_MASK) ==
5377 				    ATA_SUPPORT_ZONE_DEV_MANAGED) {
5378 				softc->zone_mode =DA_ZONE_DRIVE_MANAGED;
5379 				softc->zone_interface = DA_ZONE_IF_ATA_PASS;
5380 			}
5381 		}
5382 
5383 	} else {
5384 		error = daerror(done_ccb, CAM_RETRY_SELTO,
5385 				SF_RETRY_UA|SF_NO_PRINT);
5386 		if (error == ERESTART)
5387 			return;
5388 		else if (error != 0) {
5389 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5390 				/* Don't wedge this device's queue */
5391 				cam_release_devq(done_ccb->ccb_h.path,
5392 						 /*relsim_flags*/0,
5393 						 /*reduction*/0,
5394 						 /*timeout*/0,
5395 						 /*getcount_only*/0);
5396 			}
5397 		}
5398 	}
5399 
5400 	if ((softc->zone_mode == DA_ZONE_HOST_AWARE)
5401 	 || (softc->zone_mode == DA_ZONE_HOST_MANAGED)) {
5402 		/*
5403 		 * If the ATA IDENTIFY failed, we could be talking
5404 		 * to a SCSI drive, although that seems unlikely,
5405 		 * since the drive did report that it supported the
5406 		 * ATA Information VPD page.  If the ATA IDENTIFY
5407 		 * succeeded, and the SAT layer doesn't support
5408 		 * ZBC -> ZAC translation, continue on to get the
5409 		 * directory of ATA logs, and complete the rest of
5410 		 * the ZAC probe.  If the SAT layer does support
5411 		 * ZBC -> ZAC translation, we want to use that,
5412 		 * and we'll probe the SCSI Zoned Block Device
5413 		 * Characteristics VPD page next.
5414 		 */
5415 		if ((error == 0)
5416 		 && (softc->flags & DA_FLAG_CAN_ATA_LOG)
5417 		 && (softc->zone_interface == DA_ZONE_IF_ATA_PASS))
5418 			softc->state = DA_STATE_PROBE_ATA_LOGDIR;
5419 		else
5420 			softc->state = DA_STATE_PROBE_ZONE;
5421 		continue_probe = 1;
5422 	}
5423 	if (continue_probe != 0) {
5424 		xpt_schedule(periph, priority);
5425 		xpt_release_ccb(done_ccb);
5426 		return;
5427 	} else
5428 		daprobedone(periph, done_ccb);
5429 	return;
5430 }
5431 
5432 static void
5433 dadone_probeatalogdir(struct cam_periph *periph, union ccb *done_ccb)
5434 {
5435 	struct da_softc *softc;
5436 	struct ccb_scsiio *csio;
5437 	u_int32_t  priority;
5438 	int error;
5439 
5440 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone_probeatalogdir\n"));
5441 
5442 	softc = (struct da_softc *)periph->softc;
5443 	priority = done_ccb->ccb_h.pinfo.priority;
5444 	csio = &done_ccb->csio;
5445 
5446 	cam_periph_assert(periph, MA_OWNED);
5447 	if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5448 		error = 0;
5449 		softc->valid_logdir_len = 0;
5450 		bzero(&softc->ata_logdir, sizeof(softc->ata_logdir));
5451 		softc->valid_logdir_len = csio->dxfer_len - csio->resid;
5452 		if (softc->valid_logdir_len > 0)
5453 			bcopy(csio->data_ptr, &softc->ata_logdir,
5454 			    min(softc->valid_logdir_len,
5455 				sizeof(softc->ata_logdir)));
5456 		/*
5457 		 * Figure out whether the Identify Device log is
5458 		 * supported.  The General Purpose log directory
5459 		 * has a header, and lists the number of pages
5460 		 * available for each GP log identified by the
5461 		 * offset into the list.
5462 		 */
5463 		if ((softc->valid_logdir_len >=
5464 		    ((ATA_IDENTIFY_DATA_LOG + 1) * sizeof(uint16_t)))
5465 		 && (le16dec(softc->ata_logdir.header) ==
5466 		     ATA_GP_LOG_DIR_VERSION)
5467 		 && (le16dec(&softc->ata_logdir.num_pages[
5468 		     (ATA_IDENTIFY_DATA_LOG *
5469 		     sizeof(uint16_t)) - sizeof(uint16_t)]) > 0)){
5470 			softc->flags |= DA_FLAG_CAN_ATA_IDLOG;
5471 		} else {
5472 			softc->flags &= ~DA_FLAG_CAN_ATA_IDLOG;
5473 		}
5474 	} else {
5475 		error = daerror(done_ccb, CAM_RETRY_SELTO,
5476 				SF_RETRY_UA|SF_NO_PRINT);
5477 		if (error == ERESTART)
5478 			return;
5479 		else if (error != 0) {
5480 			/*
5481 			 * If we can't get the ATA log directory,
5482 			 * then ATA logs are effectively not
5483 			 * supported even if the bit is set in the
5484 			 * identify data.
5485 			 */
5486 			softc->flags &= ~(DA_FLAG_CAN_ATA_LOG |
5487 					  DA_FLAG_CAN_ATA_IDLOG);
5488 			if ((done_ccb->ccb_h.status &
5489 			     CAM_DEV_QFRZN) != 0) {
5490 				/* Don't wedge this device's queue */
5491 				cam_release_devq(done_ccb->ccb_h.path,
5492 						 /*relsim_flags*/0,
5493 						 /*reduction*/0,
5494 						 /*timeout*/0,
5495 						 /*getcount_only*/0);
5496 			}
5497 		}
5498 	}
5499 
5500 	free(csio->data_ptr, M_SCSIDA);
5501 
5502 	if ((error == 0)
5503 	 && (softc->flags & DA_FLAG_CAN_ATA_IDLOG)) {
5504 		softc->state = DA_STATE_PROBE_ATA_IDDIR;
5505 		xpt_release_ccb(done_ccb);
5506 		xpt_schedule(periph, priority);
5507 		return;
5508 	}
5509 	daprobedone(periph, done_ccb);
5510 	return;
5511 }
5512 
5513 static void
5514 dadone_probeataiddir(struct cam_periph *periph, union ccb *done_ccb)
5515 {
5516 	struct da_softc *softc;
5517 	struct ccb_scsiio *csio;
5518 	u_int32_t  priority;
5519 	int error;
5520 
5521 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone_probeataiddir\n"));
5522 
5523 	softc = (struct da_softc *)periph->softc;
5524 	priority = done_ccb->ccb_h.pinfo.priority;
5525 	csio = &done_ccb->csio;
5526 
5527 	cam_periph_assert(periph, MA_OWNED);
5528 
5529 	if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5530 		off_t entries_offset, max_entries;
5531 		error = 0;
5532 
5533 		softc->valid_iddir_len = 0;
5534 		bzero(&softc->ata_iddir, sizeof(softc->ata_iddir));
5535 		softc->flags &= ~(DA_FLAG_CAN_ATA_SUPCAP |
5536 				  DA_FLAG_CAN_ATA_ZONE);
5537 		softc->valid_iddir_len = csio->dxfer_len - csio->resid;
5538 		if (softc->valid_iddir_len > 0)
5539 			bcopy(csio->data_ptr, &softc->ata_iddir,
5540 			    min(softc->valid_iddir_len,
5541 				sizeof(softc->ata_iddir)));
5542 
5543 		entries_offset =
5544 		    __offsetof(struct ata_identify_log_pages,entries);
5545 		max_entries = softc->valid_iddir_len - entries_offset;
5546 		if ((softc->valid_iddir_len > (entries_offset + 1))
5547 		 && (le64dec(softc->ata_iddir.header) == ATA_IDLOG_REVISION)
5548 		 && (softc->ata_iddir.entry_count > 0)) {
5549 			int num_entries, i;
5550 
5551 			num_entries = softc->ata_iddir.entry_count;
5552 			num_entries = min(num_entries,
5553 			   softc->valid_iddir_len - entries_offset);
5554 			for (i = 0; i < num_entries && i < max_entries; i++) {
5555 				if (softc->ata_iddir.entries[i] ==
5556 				    ATA_IDL_SUP_CAP)
5557 					softc->flags |= DA_FLAG_CAN_ATA_SUPCAP;
5558 				else if (softc->ata_iddir.entries[i] ==
5559 					 ATA_IDL_ZDI)
5560 					softc->flags |= DA_FLAG_CAN_ATA_ZONE;
5561 
5562 				if ((softc->flags & DA_FLAG_CAN_ATA_SUPCAP)
5563 				 && (softc->flags & DA_FLAG_CAN_ATA_ZONE))
5564 					break;
5565 			}
5566 		}
5567 	} else {
5568 		error = daerror(done_ccb, CAM_RETRY_SELTO,
5569 				SF_RETRY_UA|SF_NO_PRINT);
5570 		if (error == ERESTART)
5571 			return;
5572 		else if (error != 0) {
5573 			/*
5574 			 * If we can't get the ATA Identify Data log
5575 			 * directory, then it effectively isn't
5576 			 * supported even if the ATA Log directory
5577 			 * a non-zero number of pages present for
5578 			 * this log.
5579 			 */
5580 			softc->flags &= ~DA_FLAG_CAN_ATA_IDLOG;
5581 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5582 				/* Don't wedge this device's queue */
5583 				cam_release_devq(done_ccb->ccb_h.path,
5584 						 /*relsim_flags*/0,
5585 						 /*reduction*/0,
5586 						 /*timeout*/0,
5587 						 /*getcount_only*/0);
5588 			}
5589 		}
5590 	}
5591 
5592 	free(csio->data_ptr, M_SCSIDA);
5593 
5594 	if ((error == 0) && (softc->flags & DA_FLAG_CAN_ATA_SUPCAP)) {
5595 		softc->state = DA_STATE_PROBE_ATA_SUP;
5596 		xpt_release_ccb(done_ccb);
5597 		xpt_schedule(periph, priority);
5598 		return;
5599 	}
5600 	daprobedone(periph, done_ccb);
5601 	return;
5602 }
5603 
5604 static void
5605 dadone_probeatasup(struct cam_periph *periph, union ccb *done_ccb)
5606 {
5607 	struct da_softc *softc;
5608 	struct ccb_scsiio *csio;
5609 	u_int32_t  priority;
5610 	int error;
5611 
5612 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone_probeatasup\n"));
5613 
5614 	softc = (struct da_softc *)periph->softc;
5615 	priority = done_ccb->ccb_h.pinfo.priority;
5616 	csio = &done_ccb->csio;
5617 
5618 	cam_periph_assert(periph, MA_OWNED);
5619 
5620 	if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5621 		uint32_t valid_len;
5622 		size_t needed_size;
5623 		struct ata_identify_log_sup_cap *sup_cap;
5624 		error = 0;
5625 
5626 		sup_cap = (struct ata_identify_log_sup_cap *)csio->data_ptr;
5627 		valid_len = csio->dxfer_len - csio->resid;
5628 		needed_size = __offsetof(struct ata_identify_log_sup_cap,
5629 		    sup_zac_cap) + 1 + sizeof(sup_cap->sup_zac_cap);
5630 		if (valid_len >= needed_size) {
5631 			uint64_t zoned, zac_cap;
5632 
5633 			zoned = le64dec(sup_cap->zoned_cap);
5634 			if (zoned & ATA_ZONED_VALID) {
5635 				/*
5636 				 * This should have already been
5637 				 * set, because this is also in the
5638 				 * ATA identify data.
5639 				 */
5640 				if ((zoned & ATA_ZONED_MASK) ==
5641 				    ATA_SUPPORT_ZONE_HOST_AWARE)
5642 					softc->zone_mode = DA_ZONE_HOST_AWARE;
5643 				else if ((zoned & ATA_ZONED_MASK) ==
5644 				    ATA_SUPPORT_ZONE_DEV_MANAGED)
5645 					softc->zone_mode =
5646 					    DA_ZONE_DRIVE_MANAGED;
5647 			}
5648 
5649 			zac_cap = le64dec(sup_cap->sup_zac_cap);
5650 			if (zac_cap & ATA_SUP_ZAC_CAP_VALID) {
5651 				if (zac_cap & ATA_REPORT_ZONES_SUP)
5652 					softc->zone_flags |=
5653 					    DA_ZONE_FLAG_RZ_SUP;
5654 				if (zac_cap & ATA_ND_OPEN_ZONE_SUP)
5655 					softc->zone_flags |=
5656 					    DA_ZONE_FLAG_OPEN_SUP;
5657 				if (zac_cap & ATA_ND_CLOSE_ZONE_SUP)
5658 					softc->zone_flags |=
5659 					    DA_ZONE_FLAG_CLOSE_SUP;
5660 				if (zac_cap & ATA_ND_FINISH_ZONE_SUP)
5661 					softc->zone_flags |=
5662 					    DA_ZONE_FLAG_FINISH_SUP;
5663 				if (zac_cap & ATA_ND_RWP_SUP)
5664 					softc->zone_flags |=
5665 					    DA_ZONE_FLAG_RWP_SUP;
5666 			} else {
5667 				/*
5668 				 * This field was introduced in
5669 				 * ACS-4, r08 on April 28th, 2015.
5670 				 * If the drive firmware was written
5671 				 * to an earlier spec, it won't have
5672 				 * the field.  So, assume all
5673 				 * commands are supported.
5674 				 */
5675 				softc->zone_flags |= DA_ZONE_FLAG_SUP_MASK;
5676 			}
5677 		}
5678 	} else {
5679 		error = daerror(done_ccb, CAM_RETRY_SELTO,
5680 				SF_RETRY_UA|SF_NO_PRINT);
5681 		if (error == ERESTART)
5682 			return;
5683 		else if (error != 0) {
5684 			/*
5685 			 * If we can't get the ATA Identify Data
5686 			 * Supported Capabilities page, clear the
5687 			 * flag...
5688 			 */
5689 			softc->flags &= ~DA_FLAG_CAN_ATA_SUPCAP;
5690 			/*
5691 			 * And clear zone capabilities.
5692 			 */
5693 			softc->zone_flags &= ~DA_ZONE_FLAG_SUP_MASK;
5694 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5695 				/* Don't wedge this device's queue */
5696 				cam_release_devq(done_ccb->ccb_h.path,
5697 						 /*relsim_flags*/0,
5698 						 /*reduction*/0,
5699 						 /*timeout*/0,
5700 						 /*getcount_only*/0);
5701 			}
5702 		}
5703 	}
5704 
5705 	free(csio->data_ptr, M_SCSIDA);
5706 
5707 	if ((error == 0) && (softc->flags & DA_FLAG_CAN_ATA_ZONE)) {
5708 		softc->state = DA_STATE_PROBE_ATA_ZONE;
5709 		xpt_release_ccb(done_ccb);
5710 		xpt_schedule(periph, priority);
5711 		return;
5712 	}
5713 	daprobedone(periph, done_ccb);
5714 	return;
5715 }
5716 
5717 static void
5718 dadone_probeatazone(struct cam_periph *periph, union ccb *done_ccb)
5719 {
5720 	struct da_softc *softc;
5721 	struct ccb_scsiio *csio;
5722 	int error;
5723 
5724 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone_probeatazone\n"));
5725 
5726 	softc = (struct da_softc *)periph->softc;
5727 	csio = &done_ccb->csio;
5728 
5729 	cam_periph_assert(periph, MA_OWNED);
5730 
5731 	if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5732 		struct ata_zoned_info_log *zi_log;
5733 		uint32_t valid_len;
5734 		size_t needed_size;
5735 
5736 		zi_log = (struct ata_zoned_info_log *)csio->data_ptr;
5737 
5738 		valid_len = csio->dxfer_len - csio->resid;
5739 		needed_size = __offsetof(struct ata_zoned_info_log,
5740 		    version_info) + 1 + sizeof(zi_log->version_info);
5741 		if (valid_len >= needed_size) {
5742 			uint64_t tmpvar;
5743 
5744 			tmpvar = le64dec(zi_log->zoned_cap);
5745 			if (tmpvar & ATA_ZDI_CAP_VALID) {
5746 				if (tmpvar & ATA_ZDI_CAP_URSWRZ)
5747 					softc->zone_flags |=
5748 					    DA_ZONE_FLAG_URSWRZ;
5749 				else
5750 					softc->zone_flags &=
5751 					    ~DA_ZONE_FLAG_URSWRZ;
5752 			}
5753 			tmpvar = le64dec(zi_log->optimal_seq_zones);
5754 			if (tmpvar & ATA_ZDI_OPT_SEQ_VALID) {
5755 				softc->zone_flags |= DA_ZONE_FLAG_OPT_SEQ_SET;
5756 				softc->optimal_seq_zones = (tmpvar &
5757 				    ATA_ZDI_OPT_SEQ_MASK);
5758 			} else {
5759 				softc->zone_flags &= ~DA_ZONE_FLAG_OPT_SEQ_SET;
5760 				softc->optimal_seq_zones = 0;
5761 			}
5762 
5763 			tmpvar =le64dec(zi_log->optimal_nonseq_zones);
5764 			if (tmpvar & ATA_ZDI_OPT_NS_VALID) {
5765 				softc->zone_flags |=
5766 				    DA_ZONE_FLAG_OPT_NONSEQ_SET;
5767 				softc->optimal_nonseq_zones =
5768 				    (tmpvar & ATA_ZDI_OPT_NS_MASK);
5769 			} else {
5770 				softc->zone_flags &=
5771 				    ~DA_ZONE_FLAG_OPT_NONSEQ_SET;
5772 				softc->optimal_nonseq_zones = 0;
5773 			}
5774 
5775 			tmpvar = le64dec(zi_log->max_seq_req_zones);
5776 			if (tmpvar & ATA_ZDI_MAX_SEQ_VALID) {
5777 				softc->zone_flags |= DA_ZONE_FLAG_MAX_SEQ_SET;
5778 				softc->max_seq_zones =
5779 				    (tmpvar & ATA_ZDI_MAX_SEQ_MASK);
5780 			} else {
5781 				softc->zone_flags &= ~DA_ZONE_FLAG_MAX_SEQ_SET;
5782 				softc->max_seq_zones = 0;
5783 			}
5784 		}
5785 	} else {
5786 		error = daerror(done_ccb, CAM_RETRY_SELTO,
5787 				SF_RETRY_UA|SF_NO_PRINT);
5788 		if (error == ERESTART)
5789 			return;
5790 		else if (error != 0) {
5791 			softc->flags &= ~DA_FLAG_CAN_ATA_ZONE;
5792 			softc->flags &= ~DA_ZONE_FLAG_SET_MASK;
5793 
5794 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5795 				/* Don't wedge this device's queue */
5796 				cam_release_devq(done_ccb->ccb_h.path,
5797 						 /*relsim_flags*/0,
5798 						 /*reduction*/0,
5799 						 /*timeout*/0,
5800 						 /*getcount_only*/0);
5801 			}
5802 		}
5803 	}
5804 
5805 	free(csio->data_ptr, M_SCSIDA);
5806 
5807 	daprobedone(periph, done_ccb);
5808 	return;
5809 }
5810 
5811 static void
5812 dadone_probezone(struct cam_periph *periph, union ccb *done_ccb)
5813 {
5814 	struct da_softc *softc;
5815 	struct ccb_scsiio *csio;
5816 	int error;
5817 
5818 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone_probezone\n"));
5819 
5820 	softc = (struct da_softc *)periph->softc;
5821 	csio = &done_ccb->csio;
5822 
5823 	cam_periph_assert(periph, MA_OWNED);
5824 
5825 	if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5826 		uint32_t valid_len;
5827 		size_t needed_len;
5828 		struct scsi_vpd_zoned_bdc *zoned_bdc;
5829 
5830 		error = 0;
5831 		zoned_bdc = (struct scsi_vpd_zoned_bdc *)csio->data_ptr;
5832 		valid_len = csio->dxfer_len - csio->resid;
5833 		needed_len = __offsetof(struct scsi_vpd_zoned_bdc,
5834 		    max_seq_req_zones) + 1 +
5835 		    sizeof(zoned_bdc->max_seq_req_zones);
5836 		if ((valid_len >= needed_len)
5837 		 && (scsi_2btoul(zoned_bdc->page_length) >= SVPD_ZBDC_PL)) {
5838 			if (zoned_bdc->flags & SVPD_ZBDC_URSWRZ)
5839 				softc->zone_flags |= DA_ZONE_FLAG_URSWRZ;
5840 			else
5841 				softc->zone_flags &= ~DA_ZONE_FLAG_URSWRZ;
5842 			softc->optimal_seq_zones =
5843 			    scsi_4btoul(zoned_bdc->optimal_seq_zones);
5844 			softc->zone_flags |= DA_ZONE_FLAG_OPT_SEQ_SET;
5845 			softc->optimal_nonseq_zones = scsi_4btoul(
5846 			    zoned_bdc->optimal_nonseq_zones);
5847 			softc->zone_flags |= DA_ZONE_FLAG_OPT_NONSEQ_SET;
5848 			softc->max_seq_zones =
5849 			    scsi_4btoul(zoned_bdc->max_seq_req_zones);
5850 			softc->zone_flags |= DA_ZONE_FLAG_MAX_SEQ_SET;
5851 		}
5852 		/*
5853 		 * All of the zone commands are mandatory for SCSI
5854 		 * devices.
5855 		 *
5856 		 * XXX KDM this is valid as of September 2015.
5857 		 * Re-check this assumption once the SAT spec is
5858 		 * updated to support SCSI ZBC to ATA ZAC mapping.
5859 		 * Since ATA allows zone commands to be reported
5860 		 * as supported or not, this may not necessarily
5861 		 * be true for an ATA device behind a SAT (SCSI to
5862 		 * ATA Translation) layer.
5863 		 */
5864 		softc->zone_flags |= DA_ZONE_FLAG_SUP_MASK;
5865 	} else {
5866 		error = daerror(done_ccb, CAM_RETRY_SELTO,
5867 				SF_RETRY_UA|SF_NO_PRINT);
5868 		if (error == ERESTART)
5869 			return;
5870 		else if (error != 0) {
5871 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5872 				/* Don't wedge this device's queue */
5873 				cam_release_devq(done_ccb->ccb_h.path,
5874 						 /*relsim_flags*/0,
5875 						 /*reduction*/0,
5876 						 /*timeout*/0,
5877 						 /*getcount_only*/0);
5878 			}
5879 		}
5880 	}
5881 
5882 	free(csio->data_ptr, M_SCSIDA);
5883 
5884 	daprobedone(periph, done_ccb);
5885 	return;
5886 }
5887 
5888 static void
5889 dadone_tur(struct cam_periph *periph, union ccb *done_ccb)
5890 {
5891 	struct da_softc *softc;
5892 
5893 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone_tur\n"));
5894 
5895 	softc = (struct da_softc *)periph->softc;
5896 
5897 	cam_periph_assert(periph, MA_OWNED);
5898 
5899 	if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5900 		if (daerror(done_ccb, CAM_RETRY_SELTO,
5901 		    SF_RETRY_UA | SF_NO_RECOVERY | SF_NO_PRINT) == ERESTART)
5902 			return;	/* Will complete again, keep reference */
5903 		if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
5904 			cam_release_devq(done_ccb->ccb_h.path,
5905 					 /*relsim_flags*/0,
5906 					 /*reduction*/0,
5907 					 /*timeout*/0,
5908 					 /*getcount_only*/0);
5909 	}
5910 	softc->flags &= ~DA_FLAG_TUR_PENDING;
5911 	xpt_release_ccb(done_ccb);
5912 	da_periph_release_locked(periph, DA_REF_TUR);
5913 	return;
5914 }
5915 
5916 static void
5917 dareprobe(struct cam_periph *periph)
5918 {
5919 	struct da_softc	  *softc;
5920 	int status __diagused;
5921 
5922 	softc = (struct da_softc *)periph->softc;
5923 
5924 	cam_periph_assert(periph, MA_OWNED);
5925 
5926 	/* Probe in progress; don't interfere. */
5927 	if (softc->state != DA_STATE_NORMAL)
5928 		return;
5929 
5930 	status = da_periph_acquire(periph, DA_REF_REPROBE);
5931 	KASSERT(status == 0, ("dareprobe: cam_periph_acquire failed"));
5932 
5933 	softc->state = DA_STATE_PROBE_WP;
5934 	xpt_schedule(periph, CAM_PRIORITY_DEV);
5935 }
5936 
5937 static int
5938 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
5939 {
5940 	struct da_softc	  *softc;
5941 	struct cam_periph *periph;
5942 	int error, error_code, sense_key, asc, ascq;
5943 
5944 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
5945 	if (ccb->csio.bio != NULL)
5946 		biotrack(ccb->csio.bio, __func__);
5947 #endif
5948 
5949 	periph = xpt_path_periph(ccb->ccb_h.path);
5950 	softc = (struct da_softc *)periph->softc;
5951 
5952 	cam_periph_assert(periph, MA_OWNED);
5953 
5954 	/*
5955 	 * Automatically detect devices that do not support
5956 	 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
5957 	 */
5958 	error = 0;
5959 	if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
5960 		error = cmd6workaround(ccb);
5961 	} else if (scsi_extract_sense_ccb(ccb,
5962 	    &error_code, &sense_key, &asc, &ascq)) {
5963 		if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
5964 			error = cmd6workaround(ccb);
5965 		/*
5966 		 * If the target replied with CAPACITY DATA HAS CHANGED UA,
5967 		 * query the capacity and notify upper layers.
5968 		 */
5969 		else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
5970 		    asc == 0x2A && ascq == 0x09) {
5971 			xpt_print(periph->path, "Capacity data has changed\n");
5972 			softc->flags &= ~DA_FLAG_PROBED;
5973 			dareprobe(periph);
5974 			sense_flags |= SF_NO_PRINT;
5975 		} else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
5976 		    asc == 0x28 && ascq == 0x00) {
5977 			softc->flags &= ~DA_FLAG_PROBED;
5978 			disk_media_changed(softc->disk, M_NOWAIT);
5979 		} else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
5980 		    asc == 0x3F && ascq == 0x03) {
5981 			xpt_print(periph->path, "INQUIRY data has changed\n");
5982 			softc->flags &= ~DA_FLAG_PROBED;
5983 			dareprobe(periph);
5984 			sense_flags |= SF_NO_PRINT;
5985 		} else if (sense_key == SSD_KEY_NOT_READY &&
5986 		    asc == 0x3a && (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
5987 			softc->flags |= DA_FLAG_PACK_INVALID;
5988 			disk_media_gone(softc->disk, M_NOWAIT);
5989 		}
5990 	}
5991 	if (error == ERESTART)
5992 		return (ERESTART);
5993 
5994 #ifdef CAM_IO_STATS
5995 	switch (ccb->ccb_h.status & CAM_STATUS_MASK) {
5996 	case CAM_CMD_TIMEOUT:
5997 		softc->timeouts++;
5998 		break;
5999 	case CAM_REQ_ABORTED:
6000 	case CAM_REQ_CMP_ERR:
6001 	case CAM_REQ_TERMIO:
6002 	case CAM_UNREC_HBA_ERROR:
6003 	case CAM_DATA_RUN_ERR:
6004 		softc->errors++;
6005 		break;
6006 	default:
6007 		break;
6008 	}
6009 #endif
6010 
6011 	/*
6012 	 * XXX
6013 	 * Until we have a better way of doing pack validation,
6014 	 * don't treat UAs as errors.
6015 	 */
6016 	sense_flags |= SF_RETRY_UA;
6017 
6018 	if (softc->quirks & DA_Q_RETRY_BUSY)
6019 		sense_flags |= SF_RETRY_BUSY;
6020 	return(cam_periph_error(ccb, cam_flags, sense_flags));
6021 }
6022 
6023 static void
6024 damediapoll(void *arg)
6025 {
6026 	struct cam_periph *periph = arg;
6027 	struct da_softc *softc = periph->softc;
6028 
6029 	if (!cam_iosched_has_work_flags(softc->cam_iosched, DA_WORK_TUR) &&
6030 	    (softc->flags & DA_FLAG_TUR_PENDING) == 0 &&
6031 	    softc->state == DA_STATE_NORMAL &&
6032 	    LIST_EMPTY(&softc->pending_ccbs)) {
6033 		if (da_periph_acquire(periph, DA_REF_TUR) == 0) {
6034 			cam_iosched_set_work_flags(softc->cam_iosched, DA_WORK_TUR);
6035 			daschedule(periph);
6036 		}
6037 	}
6038 	/* Queue us up again */
6039 	if (da_poll_period != 0)
6040 		callout_schedule(&softc->mediapoll_c, da_poll_period * hz);
6041 }
6042 
6043 static void
6044 daprevent(struct cam_periph *periph, int action)
6045 {
6046 	struct	da_softc *softc;
6047 	union	ccb *ccb;
6048 	int	error;
6049 
6050 	cam_periph_assert(periph, MA_OWNED);
6051 	softc = (struct da_softc *)periph->softc;
6052 
6053 	if (((action == PR_ALLOW)
6054 	  && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
6055 	 || ((action == PR_PREVENT)
6056 	  && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
6057 		return;
6058 	}
6059 
6060 	ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
6061 
6062 	scsi_prevent(&ccb->csio,
6063 		     /*retries*/1,
6064 		     /*cbcfp*/NULL,
6065 		     MSG_SIMPLE_Q_TAG,
6066 		     action,
6067 		     SSD_FULL_SIZE,
6068 		     5000);
6069 
6070 	error = cam_periph_runccb(ccb, daerror, CAM_RETRY_SELTO,
6071 	    SF_RETRY_UA | SF_NO_PRINT, softc->disk->d_devstat);
6072 
6073 	if (error == 0) {
6074 		if (action == PR_ALLOW)
6075 			softc->flags &= ~DA_FLAG_PACK_LOCKED;
6076 		else
6077 			softc->flags |= DA_FLAG_PACK_LOCKED;
6078 	}
6079 
6080 	xpt_release_ccb(ccb);
6081 }
6082 
6083 static void
6084 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector,
6085 	  struct scsi_read_capacity_data_long *rcaplong, size_t rcap_len)
6086 {
6087 	struct ccb_calc_geometry ccg;
6088 	struct da_softc *softc;
6089 	struct disk_params *dp;
6090 	u_int lbppbe, lalba;
6091 	int error;
6092 
6093 	softc = (struct da_softc *)periph->softc;
6094 
6095 	dp = &softc->params;
6096 	dp->secsize = block_len;
6097 	dp->sectors = maxsector + 1;
6098 	if (rcaplong != NULL) {
6099 		lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
6100 		lalba = scsi_2btoul(rcaplong->lalba_lbp);
6101 		lalba &= SRC16_LALBA_A;
6102 		if (rcaplong->prot & SRC16_PROT_EN)
6103 			softc->p_type = ((rcaplong->prot & SRC16_P_TYPE) >>
6104 			    SRC16_P_TYPE_SHIFT) + 1;
6105 		else
6106 			softc->p_type = 0;
6107 	} else {
6108 		lbppbe = 0;
6109 		lalba = 0;
6110 		softc->p_type = 0;
6111 	}
6112 
6113 	if (lbppbe > 0) {
6114 		dp->stripesize = block_len << lbppbe;
6115 		dp->stripeoffset = (dp->stripesize - block_len * lalba) %
6116 		    dp->stripesize;
6117 	} else if (softc->quirks & DA_Q_4K) {
6118 		dp->stripesize = 4096;
6119 		dp->stripeoffset = 0;
6120 	} else if (softc->unmap_gran != 0) {
6121 		dp->stripesize = block_len * softc->unmap_gran;
6122 		dp->stripeoffset = (dp->stripesize - block_len *
6123 		    softc->unmap_gran_align) % dp->stripesize;
6124 	} else {
6125 		dp->stripesize = 0;
6126 		dp->stripeoffset = 0;
6127 	}
6128 	/*
6129 	 * Have the controller provide us with a geometry
6130 	 * for this disk.  The only time the geometry
6131 	 * matters is when we boot and the controller
6132 	 * is the only one knowledgeable enough to come
6133 	 * up with something that will make this a bootable
6134 	 * device.
6135 	 */
6136 	memset(&ccg, 0, sizeof(ccg));
6137 	xpt_setup_ccb(&ccg.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
6138 	ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
6139 	ccg.block_size = dp->secsize;
6140 	ccg.volume_size = dp->sectors;
6141 	ccg.heads = 0;
6142 	ccg.secs_per_track = 0;
6143 	ccg.cylinders = 0;
6144 	xpt_action((union ccb*)&ccg);
6145 	if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
6146 		/*
6147 		 * We don't know what went wrong here- but just pick
6148 		 * a geometry so we don't have nasty things like divide
6149 		 * by zero.
6150 		 */
6151 		dp->heads = 255;
6152 		dp->secs_per_track = 255;
6153 		dp->cylinders = dp->sectors / (255 * 255);
6154 		if (dp->cylinders == 0) {
6155 			dp->cylinders = 1;
6156 		}
6157 	} else {
6158 		dp->heads = ccg.heads;
6159 		dp->secs_per_track = ccg.secs_per_track;
6160 		dp->cylinders = ccg.cylinders;
6161 	}
6162 
6163 	/*
6164 	 * If the user supplied a read capacity buffer, and if it is
6165 	 * different than the previous buffer, update the data in the EDT.
6166 	 * If it's the same, we don't bother.  This avoids sending an
6167 	 * update every time someone opens this device.
6168 	 */
6169 	if ((rcaplong != NULL)
6170 	 && (bcmp(rcaplong, &softc->rcaplong,
6171 		  min(sizeof(softc->rcaplong), rcap_len)) != 0)) {
6172 		struct ccb_dev_advinfo cdai;
6173 
6174 		memset(&cdai, 0, sizeof(cdai));
6175 		xpt_setup_ccb(&cdai.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
6176 		cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
6177 		cdai.buftype = CDAI_TYPE_RCAPLONG;
6178 		cdai.flags = CDAI_FLAG_STORE;
6179 		cdai.bufsiz = rcap_len;
6180 		cdai.buf = (uint8_t *)rcaplong;
6181 		xpt_action((union ccb *)&cdai);
6182 		if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
6183 			cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
6184 		if (cdai.ccb_h.status != CAM_REQ_CMP) {
6185 			xpt_print(periph->path, "%s: failed to set read "
6186 				  "capacity advinfo\n", __func__);
6187 			/* Use cam_error_print() to decode the status */
6188 			cam_error_print((union ccb *)&cdai, CAM_ESF_CAM_STATUS,
6189 					CAM_EPF_ALL);
6190 		} else {
6191 			bcopy(rcaplong, &softc->rcaplong,
6192 			      min(sizeof(softc->rcaplong), rcap_len));
6193 		}
6194 	}
6195 
6196 	softc->disk->d_sectorsize = softc->params.secsize;
6197 	softc->disk->d_mediasize = softc->params.secsize * (off_t)softc->params.sectors;
6198 	softc->disk->d_stripesize = softc->params.stripesize;
6199 	softc->disk->d_stripeoffset = softc->params.stripeoffset;
6200 	/* XXX: these are not actually "firmware" values, so they may be wrong */
6201 	softc->disk->d_fwsectors = softc->params.secs_per_track;
6202 	softc->disk->d_fwheads = softc->params.heads;
6203 	softc->disk->d_devstat->block_size = softc->params.secsize;
6204 	softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
6205 
6206 	error = disk_resize(softc->disk, M_NOWAIT);
6207 	if (error != 0)
6208 		xpt_print(periph->path, "disk_resize(9) failed, error = %d\n", error);
6209 }
6210 
6211 static void
6212 dasendorderedtag(void *arg)
6213 {
6214 	struct cam_periph *periph = arg;
6215 	struct da_softc *softc = periph->softc;
6216 
6217 	cam_periph_assert(periph, MA_OWNED);
6218 	if (da_send_ordered) {
6219 		if (!LIST_EMPTY(&softc->pending_ccbs)) {
6220 			if ((softc->flags & DA_FLAG_WAS_OTAG) == 0)
6221 				softc->flags |= DA_FLAG_NEED_OTAG;
6222 			softc->flags &= ~DA_FLAG_WAS_OTAG;
6223 		}
6224 	}
6225 
6226 	/* Queue us up again */
6227 	callout_reset(&softc->sendordered_c,
6228 	    (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
6229 	    dasendorderedtag, periph);
6230 }
6231 
6232 /*
6233  * Step through all DA peripheral drivers, and if the device is still open,
6234  * sync the disk cache to physical media.
6235  */
6236 static void
6237 dashutdown(void * arg, int howto)
6238 {
6239 	struct cam_periph *periph;
6240 	struct da_softc *softc;
6241 	union ccb *ccb;
6242 	int error;
6243 
6244 	if ((howto & RB_NOSYNC) != 0)
6245 		return;
6246 
6247 	CAM_PERIPH_FOREACH(periph, &dadriver) {
6248 		softc = (struct da_softc *)periph->softc;
6249 		if (SCHEDULER_STOPPED()) {
6250 			/* If we paniced with the lock held, do not recurse. */
6251 			if (!cam_periph_owned(periph) &&
6252 			    (softc->flags & DA_FLAG_OPEN)) {
6253 				dadump(softc->disk, NULL, 0, 0, 0);
6254 			}
6255 			continue;
6256 		}
6257 		cam_periph_lock(periph);
6258 
6259 		/*
6260 		 * We only sync the cache if the drive is still open, and
6261 		 * if the drive is capable of it..
6262 		 */
6263 		if (((softc->flags & DA_FLAG_OPEN) == 0)
6264 		 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
6265 			cam_periph_unlock(periph);
6266 			continue;
6267 		}
6268 
6269 		ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
6270 		scsi_synchronize_cache(&ccb->csio,
6271 				       /*retries*/0,
6272 				       /*cbfcnp*/NULL,
6273 				       MSG_SIMPLE_Q_TAG,
6274 				       /*begin_lba*/0, /* whole disk */
6275 				       /*lb_count*/0,
6276 				       SSD_FULL_SIZE,
6277 				       60 * 60 * 1000);
6278 
6279 		error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
6280 		    /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR,
6281 		    softc->disk->d_devstat);
6282 		if (error != 0)
6283 			xpt_print(periph->path, "Synchronize cache failed\n");
6284 		xpt_release_ccb(ccb);
6285 		cam_periph_unlock(periph);
6286 	}
6287 }
6288 
6289 #else /* !_KERNEL */
6290 
6291 /*
6292  * XXX These are only left out of the kernel build to silence warnings.  If,
6293  * for some reason these functions are used in the kernel, the ifdefs should
6294  * be moved so they are included both in the kernel and userland.
6295  */
6296 void
6297 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
6298 		 void (*cbfcnp)(struct cam_periph *, union ccb *),
6299 		 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
6300 		 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
6301 		 u_int32_t timeout)
6302 {
6303 	struct scsi_format_unit *scsi_cmd;
6304 
6305 	scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
6306 	scsi_cmd->opcode = FORMAT_UNIT;
6307 	scsi_cmd->byte2 = byte2;
6308 	scsi_ulto2b(ileave, scsi_cmd->interleave);
6309 
6310 	cam_fill_csio(csio,
6311 		      retries,
6312 		      cbfcnp,
6313 		      /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
6314 		      tag_action,
6315 		      data_ptr,
6316 		      dxfer_len,
6317 		      sense_len,
6318 		      sizeof(*scsi_cmd),
6319 		      timeout);
6320 }
6321 
6322 void
6323 scsi_read_defects(struct ccb_scsiio *csio, uint32_t retries,
6324 		  void (*cbfcnp)(struct cam_periph *, union ccb *),
6325 		  uint8_t tag_action, uint8_t list_format,
6326 		  uint32_t addr_desc_index, uint8_t *data_ptr,
6327 		  uint32_t dxfer_len, int minimum_cmd_size,
6328 		  uint8_t sense_len, uint32_t timeout)
6329 {
6330 	uint8_t cdb_len;
6331 
6332 	/*
6333 	 * These conditions allow using the 10 byte command.  Otherwise we
6334 	 * need to use the 12 byte command.
6335 	 */
6336 	if ((minimum_cmd_size <= 10)
6337 	 && (addr_desc_index == 0)
6338 	 && (dxfer_len <= SRDD10_MAX_LENGTH)) {
6339 		struct scsi_read_defect_data_10 *cdb10;
6340 
6341 		cdb10 = (struct scsi_read_defect_data_10 *)
6342 			&csio->cdb_io.cdb_bytes;
6343 
6344 		cdb_len = sizeof(*cdb10);
6345 		bzero(cdb10, cdb_len);
6346                 cdb10->opcode = READ_DEFECT_DATA_10;
6347                 cdb10->format = list_format;
6348                 scsi_ulto2b(dxfer_len, cdb10->alloc_length);
6349 	} else {
6350 		struct scsi_read_defect_data_12 *cdb12;
6351 
6352 		cdb12 = (struct scsi_read_defect_data_12 *)
6353 			&csio->cdb_io.cdb_bytes;
6354 
6355 		cdb_len = sizeof(*cdb12);
6356 		bzero(cdb12, cdb_len);
6357                 cdb12->opcode = READ_DEFECT_DATA_12;
6358                 cdb12->format = list_format;
6359                 scsi_ulto4b(dxfer_len, cdb12->alloc_length);
6360 		scsi_ulto4b(addr_desc_index, cdb12->address_descriptor_index);
6361 	}
6362 
6363 	cam_fill_csio(csio,
6364 		      retries,
6365 		      cbfcnp,
6366 		      /*flags*/ CAM_DIR_IN,
6367 		      tag_action,
6368 		      data_ptr,
6369 		      dxfer_len,
6370 		      sense_len,
6371 		      cdb_len,
6372 		      timeout);
6373 }
6374 
6375 void
6376 scsi_sanitize(struct ccb_scsiio *csio, u_int32_t retries,
6377 	      void (*cbfcnp)(struct cam_periph *, union ccb *),
6378 	      u_int8_t tag_action, u_int8_t byte2, u_int16_t control,
6379 	      u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
6380 	      u_int32_t timeout)
6381 {
6382 	struct scsi_sanitize *scsi_cmd;
6383 
6384 	scsi_cmd = (struct scsi_sanitize *)&csio->cdb_io.cdb_bytes;
6385 	scsi_cmd->opcode = SANITIZE;
6386 	scsi_cmd->byte2 = byte2;
6387 	scsi_cmd->control = control;
6388 	scsi_ulto2b(dxfer_len, scsi_cmd->length);
6389 
6390 	cam_fill_csio(csio,
6391 		      retries,
6392 		      cbfcnp,
6393 		      /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
6394 		      tag_action,
6395 		      data_ptr,
6396 		      dxfer_len,
6397 		      sense_len,
6398 		      sizeof(*scsi_cmd),
6399 		      timeout);
6400 }
6401 
6402 #endif /* _KERNEL */
6403 
6404 void
6405 scsi_zbc_out(struct ccb_scsiio *csio, uint32_t retries,
6406 	     void (*cbfcnp)(struct cam_periph *, union ccb *),
6407 	     uint8_t tag_action, uint8_t service_action, uint64_t zone_id,
6408 	     uint8_t zone_flags, uint8_t *data_ptr, uint32_t dxfer_len,
6409 	     uint8_t sense_len, uint32_t timeout)
6410 {
6411 	struct scsi_zbc_out *scsi_cmd;
6412 
6413 	scsi_cmd = (struct scsi_zbc_out *)&csio->cdb_io.cdb_bytes;
6414 	scsi_cmd->opcode = ZBC_OUT;
6415 	scsi_cmd->service_action = service_action;
6416 	scsi_u64to8b(zone_id, scsi_cmd->zone_id);
6417 	scsi_cmd->zone_flags = zone_flags;
6418 
6419 	cam_fill_csio(csio,
6420 		      retries,
6421 		      cbfcnp,
6422 		      /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
6423 		      tag_action,
6424 		      data_ptr,
6425 		      dxfer_len,
6426 		      sense_len,
6427 		      sizeof(*scsi_cmd),
6428 		      timeout);
6429 }
6430 
6431 void
6432 scsi_zbc_in(struct ccb_scsiio *csio, uint32_t retries,
6433 	    void (*cbfcnp)(struct cam_periph *, union ccb *),
6434 	    uint8_t tag_action, uint8_t service_action, uint64_t zone_start_lba,
6435 	    uint8_t zone_options, uint8_t *data_ptr, uint32_t dxfer_len,
6436 	    uint8_t sense_len, uint32_t timeout)
6437 {
6438 	struct scsi_zbc_in *scsi_cmd;
6439 
6440 	scsi_cmd = (struct scsi_zbc_in *)&csio->cdb_io.cdb_bytes;
6441 	scsi_cmd->opcode = ZBC_IN;
6442 	scsi_cmd->service_action = service_action;
6443 	scsi_ulto4b(dxfer_len, scsi_cmd->length);
6444 	scsi_u64to8b(zone_start_lba, scsi_cmd->zone_start_lba);
6445 	scsi_cmd->zone_options = zone_options;
6446 
6447 	cam_fill_csio(csio,
6448 		      retries,
6449 		      cbfcnp,
6450 		      /*flags*/ (dxfer_len > 0) ? CAM_DIR_IN : CAM_DIR_NONE,
6451 		      tag_action,
6452 		      data_ptr,
6453 		      dxfer_len,
6454 		      sense_len,
6455 		      sizeof(*scsi_cmd),
6456 		      timeout);
6457 
6458 }
6459 
6460 int
6461 scsi_ata_zac_mgmt_out(struct ccb_scsiio *csio, uint32_t retries,
6462 		      void (*cbfcnp)(struct cam_periph *, union ccb *),
6463 		      uint8_t tag_action, int use_ncq,
6464 		      uint8_t zm_action, uint64_t zone_id, uint8_t zone_flags,
6465 		      uint8_t *data_ptr, uint32_t dxfer_len,
6466 		      uint8_t *cdb_storage, size_t cdb_storage_len,
6467 		      uint8_t sense_len, uint32_t timeout)
6468 {
6469 	uint8_t command_out, protocol, ata_flags;
6470 	uint16_t features_out;
6471 	uint32_t sectors_out, auxiliary;
6472 	int retval;
6473 
6474 	retval = 0;
6475 
6476 	if (use_ncq == 0) {
6477 		command_out = ATA_ZAC_MANAGEMENT_OUT;
6478 		features_out = (zm_action & 0xf) | (zone_flags << 8);
6479 		ata_flags = AP_FLAG_BYT_BLOK_BLOCKS;
6480 		if (dxfer_len == 0) {
6481 			protocol = AP_PROTO_NON_DATA;
6482 			ata_flags |= AP_FLAG_TLEN_NO_DATA;
6483 			sectors_out = 0;
6484 		} else {
6485 			protocol = AP_PROTO_DMA;
6486 			ata_flags |= AP_FLAG_TLEN_SECT_CNT |
6487 				     AP_FLAG_TDIR_TO_DEV;
6488 			sectors_out = ((dxfer_len >> 9) & 0xffff);
6489 		}
6490 		auxiliary = 0;
6491 	} else {
6492 		ata_flags = AP_FLAG_BYT_BLOK_BLOCKS;
6493 		if (dxfer_len == 0) {
6494 			command_out = ATA_NCQ_NON_DATA;
6495 			features_out = ATA_NCQ_ZAC_MGMT_OUT;
6496 			/*
6497 			 * We're assuming the SCSI to ATA translation layer
6498 			 * will set the NCQ tag number in the tag field.
6499 			 * That isn't clear from the SAT-4 spec (as of rev 05).
6500 			 */
6501 			sectors_out = 0;
6502 			ata_flags |= AP_FLAG_TLEN_NO_DATA;
6503 		} else {
6504 			command_out = ATA_SEND_FPDMA_QUEUED;
6505 			/*
6506 			 * Note that we're defaulting to normal priority,
6507 			 * and assuming that the SCSI to ATA translation
6508 			 * layer will insert the NCQ tag number in the tag
6509 			 * field.  That isn't clear in the SAT-4 spec (as
6510 			 * of rev 05).
6511 			 */
6512 			sectors_out = ATA_SFPDMA_ZAC_MGMT_OUT << 8;
6513 
6514 			ata_flags |= AP_FLAG_TLEN_FEAT |
6515 				     AP_FLAG_TDIR_TO_DEV;
6516 
6517 			/*
6518 			 * For SEND FPDMA QUEUED, the transfer length is
6519 			 * encoded in the FEATURE register, and 0 means
6520 			 * that 65536 512 byte blocks are to be tranferred.
6521 			 * In practice, it seems unlikely that we'll see
6522 			 * a transfer that large, and it may confuse the
6523 			 * the SAT layer, because generally that means that
6524 			 * 0 bytes should be transferred.
6525 			 */
6526 			if (dxfer_len == (65536 * 512)) {
6527 				features_out = 0;
6528 			} else if (dxfer_len <= (65535 * 512)) {
6529 				features_out = ((dxfer_len >> 9) & 0xffff);
6530 			} else {
6531 				/* The transfer is too big. */
6532 				retval = 1;
6533 				goto bailout;
6534 			}
6535 		}
6536 
6537 		auxiliary = (zm_action & 0xf) | (zone_flags << 8);
6538 		protocol = AP_PROTO_FPDMA;
6539 	}
6540 
6541 	protocol |= AP_EXTEND;
6542 
6543 	retval = scsi_ata_pass(csio,
6544 	    retries,
6545 	    cbfcnp,
6546 	    /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
6547 	    tag_action,
6548 	    /*protocol*/ protocol,
6549 	    /*ata_flags*/ ata_flags,
6550 	    /*features*/ features_out,
6551 	    /*sector_count*/ sectors_out,
6552 	    /*lba*/ zone_id,
6553 	    /*command*/ command_out,
6554 	    /*device*/ 0,
6555 	    /*icc*/ 0,
6556 	    /*auxiliary*/ auxiliary,
6557 	    /*control*/ 0,
6558 	    /*data_ptr*/ data_ptr,
6559 	    /*dxfer_len*/ dxfer_len,
6560 	    /*cdb_storage*/ cdb_storage,
6561 	    /*cdb_storage_len*/ cdb_storage_len,
6562 	    /*minimum_cmd_size*/ 0,
6563 	    /*sense_len*/ SSD_FULL_SIZE,
6564 	    /*timeout*/ timeout);
6565 
6566 bailout:
6567 
6568 	return (retval);
6569 }
6570 
6571 int
6572 scsi_ata_zac_mgmt_in(struct ccb_scsiio *csio, uint32_t retries,
6573 		     void (*cbfcnp)(struct cam_periph *, union ccb *),
6574 		     uint8_t tag_action, int use_ncq,
6575 		     uint8_t zm_action, uint64_t zone_id, uint8_t zone_flags,
6576 		     uint8_t *data_ptr, uint32_t dxfer_len,
6577 		     uint8_t *cdb_storage, size_t cdb_storage_len,
6578 		     uint8_t sense_len, uint32_t timeout)
6579 {
6580 	uint8_t command_out, protocol;
6581 	uint16_t features_out, sectors_out;
6582 	uint32_t auxiliary;
6583 	int ata_flags;
6584 	int retval;
6585 
6586 	retval = 0;
6587 	ata_flags = AP_FLAG_TDIR_FROM_DEV | AP_FLAG_BYT_BLOK_BLOCKS;
6588 
6589 	if (use_ncq == 0) {
6590 		command_out = ATA_ZAC_MANAGEMENT_IN;
6591 		/* XXX KDM put a macro here */
6592 		features_out = (zm_action & 0xf) | (zone_flags << 8);
6593 		sectors_out = dxfer_len >> 9; /* XXX KDM macro */
6594 		protocol = AP_PROTO_DMA;
6595 		ata_flags |= AP_FLAG_TLEN_SECT_CNT;
6596 		auxiliary = 0;
6597 	} else {
6598 		ata_flags |= AP_FLAG_TLEN_FEAT;
6599 
6600 		command_out = ATA_RECV_FPDMA_QUEUED;
6601 		sectors_out = ATA_RFPDMA_ZAC_MGMT_IN << 8;
6602 
6603 		/*
6604 		 * For RECEIVE FPDMA QUEUED, the transfer length is
6605 		 * encoded in the FEATURE register, and 0 means
6606 		 * that 65536 512 byte blocks are to be tranferred.
6607 		 * In practice, it seems unlikely that we'll see
6608 		 * a transfer that large, and it may confuse the
6609 		 * the SAT layer, because generally that means that
6610 		 * 0 bytes should be transferred.
6611 		 */
6612 		if (dxfer_len == (65536 * 512)) {
6613 			features_out = 0;
6614 		} else if (dxfer_len <= (65535 * 512)) {
6615 			features_out = ((dxfer_len >> 9) & 0xffff);
6616 		} else {
6617 			/* The transfer is too big. */
6618 			retval = 1;
6619 			goto bailout;
6620 		}
6621 		auxiliary = (zm_action & 0xf) | (zone_flags << 8),
6622 		protocol = AP_PROTO_FPDMA;
6623 	}
6624 
6625 	protocol |= AP_EXTEND;
6626 
6627 	retval = scsi_ata_pass(csio,
6628 	    retries,
6629 	    cbfcnp,
6630 	    /*flags*/ CAM_DIR_IN,
6631 	    tag_action,
6632 	    /*protocol*/ protocol,
6633 	    /*ata_flags*/ ata_flags,
6634 	    /*features*/ features_out,
6635 	    /*sector_count*/ sectors_out,
6636 	    /*lba*/ zone_id,
6637 	    /*command*/ command_out,
6638 	    /*device*/ 0,
6639 	    /*icc*/ 0,
6640 	    /*auxiliary*/ auxiliary,
6641 	    /*control*/ 0,
6642 	    /*data_ptr*/ data_ptr,
6643 	    /*dxfer_len*/ (dxfer_len >> 9) * 512, /* XXX KDM */
6644 	    /*cdb_storage*/ cdb_storage,
6645 	    /*cdb_storage_len*/ cdb_storage_len,
6646 	    /*minimum_cmd_size*/ 0,
6647 	    /*sense_len*/ SSD_FULL_SIZE,
6648 	    /*timeout*/ timeout);
6649 
6650 bailout:
6651 	return (retval);
6652 }
6653