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