xref: /freebsd/sys/cam/scsi/scsi_da.c (revision c8e7f78a3d28ff6e6223ed136ada8e1e2f34965e)
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 	"\016ANNOUCNED"		\
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 843T Series SSDs (MZ7WD*)
1401 		 * Samsung PM851 Series SSDs (MZ7TE*)
1402 		 * Samsung PM853T Series SSDs (MZ7GE*)
1403 		 * Samsung SM863 Series SSDs (MZ7KM*)
1404 		 * 4k optimised
1405 		 */
1406 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG MZ7*", "*" },
1407 		/*quirks*/DA_Q_4K
1408 	},
1409 	{
1410 		/*
1411 		 * Same as for SAMSUNG MZ7* but enable the quirks for SSD
1412 		 * starting with MZ7* too
1413 		 */
1414 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "MZ7*", "*" },
1415 		/*quirks*/DA_Q_4K
1416 	},
1417 	{
1418 		/*
1419                  * Same as above but enable the quirks for SSD SAMSUNG MZ7*
1420                  * connected via SATA-to-SAS interposer and because of this
1421                  * starting without "ATA"
1422 		 */
1423 		{ T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "MZ7*", "*" },
1424 		/*quirks*/DA_Q_4K
1425 	},
1426 	{
1427 		/*
1428 		 * SuperTalent TeraDrive CT SSDs
1429 		 * 4k optimised & trim only works in 4k requests + 4k aligned
1430 		 */
1431 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "FTM??CT25H*", "*" },
1432 		/*quirks*/DA_Q_4K
1433 	},
1434 	{
1435 		/*
1436 		 * XceedIOPS SATA SSDs
1437 		 * 4k optimised
1438 		 */
1439 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SG9XCS2D*", "*" },
1440 		/*quirks*/DA_Q_4K
1441 	},
1442 	{
1443 		/*
1444 		 * Hama Innostor USB-Stick
1445 		 */
1446 		{ T_DIRECT, SIP_MEDIA_REMOVABLE, "Innostor", "Innostor*", "*" },
1447 		/*quirks*/DA_Q_NO_RC16
1448 	},
1449 	{
1450 		/*
1451 		 * Seagate Lamarr 8TB Shingled Magnetic Recording (SMR)
1452 		 * Drive Managed SATA hard drive.  This drive doesn't report
1453 		 * in firmware that it is a drive managed SMR drive.
1454 		 */
1455 		{ T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST8000AS000[23]*", "*" },
1456 		/*quirks*/DA_Q_SMR_DM
1457 	},
1458 	{
1459 		/*
1460 		 * MX-ES USB Drive by Mach Xtreme
1461 		 */
1462 		{ T_DIRECT, SIP_MEDIA_REMOVABLE, "MX", "MXUB3*", "*"},
1463 		/*quirks*/DA_Q_NO_RC16
1464 	},
1465 };
1466 
1467 static	disk_strategy_t	dastrategy;
1468 static	dumper_t	dadump;
1469 static	periph_init_t	dainit;
1470 static	void		daasync(void *callback_arg, uint32_t code,
1471 				struct cam_path *path, void *arg);
1472 static	void		dasysctlinit(void *context, int pending);
1473 static	int		dasysctlsofttimeout(SYSCTL_HANDLER_ARGS);
1474 static	int		dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
1475 static	int		dadeletemethodsysctl(SYSCTL_HANDLER_ARGS);
1476 static	int		dabitsysctl(SYSCTL_HANDLER_ARGS);
1477 static	int		daflagssysctl(SYSCTL_HANDLER_ARGS);
1478 static	int		dazonemodesysctl(SYSCTL_HANDLER_ARGS);
1479 static	int		dazonesupsysctl(SYSCTL_HANDLER_ARGS);
1480 static	int		dadeletemaxsysctl(SYSCTL_HANDLER_ARGS);
1481 static	void		dadeletemethodset(struct da_softc *softc,
1482 					  da_delete_methods delete_method);
1483 static	off_t		dadeletemaxsize(struct da_softc *softc,
1484 					da_delete_methods delete_method);
1485 static	void		dadeletemethodchoose(struct da_softc *softc,
1486 					     da_delete_methods default_method);
1487 static	void		daprobedone(struct cam_periph *periph, union ccb *ccb);
1488 
1489 static	periph_ctor_t	daregister;
1490 static	periph_dtor_t	dacleanup;
1491 static	periph_start_t	dastart;
1492 static	periph_oninv_t	daoninvalidate;
1493 static	void		dazonedone(struct cam_periph *periph, union ccb *ccb);
1494 static	void		dadone(struct cam_periph *periph,
1495 			       union ccb *done_ccb);
1496 static void		dadone_probewp(struct cam_periph *periph,
1497 				       union ccb *done_ccb);
1498 static void		dadone_proberc(struct cam_periph *periph,
1499 				       union ccb *done_ccb);
1500 static void		dadone_probelbp(struct cam_periph *periph,
1501 					union ccb *done_ccb);
1502 static void		dadone_probeblklimits(struct cam_periph *periph,
1503 					      union ccb *done_ccb);
1504 static void		dadone_probebdc(struct cam_periph *periph,
1505 					union ccb *done_ccb);
1506 static void		dadone_probeata(struct cam_periph *periph,
1507 					union ccb *done_ccb);
1508 static void		dadone_probeatalogdir(struct cam_periph *periph,
1509 					      union ccb *done_ccb);
1510 static void		dadone_probeataiddir(struct cam_periph *periph,
1511 					     union ccb *done_ccb);
1512 static void		dadone_probeatasup(struct cam_periph *periph,
1513 					   union ccb *done_ccb);
1514 static void		dadone_probeatazone(struct cam_periph *periph,
1515 					    union ccb *done_ccb);
1516 static void		dadone_probezone(struct cam_periph *periph,
1517 					 union ccb *done_ccb);
1518 static void		dadone_tur(struct cam_periph *periph,
1519 				   union ccb *done_ccb);
1520 static  int		daerror(union ccb *ccb, uint32_t cam_flags,
1521 				uint32_t sense_flags);
1522 static void		daprevent(struct cam_periph *periph, int action);
1523 static void		dareprobe(struct cam_periph *periph);
1524 static void		dasetgeom(struct cam_periph *periph, uint32_t block_len,
1525 				  uint64_t maxsector,
1526 				  struct scsi_read_capacity_data_long *rcaplong,
1527 				  size_t rcap_size);
1528 static callout_func_t	dasendorderedtag;
1529 static void		dashutdown(void *arg, int howto);
1530 static callout_func_t	damediapoll;
1531 
1532 #ifndef	DA_DEFAULT_POLL_PERIOD
1533 #define	DA_DEFAULT_POLL_PERIOD	3
1534 #endif
1535 
1536 #ifndef DA_DEFAULT_TIMEOUT
1537 #define DA_DEFAULT_TIMEOUT 60	/* Timeout in seconds */
1538 #endif
1539 
1540 #ifndef DA_DEFAULT_SOFTTIMEOUT
1541 #define DA_DEFAULT_SOFTTIMEOUT	0
1542 #endif
1543 
1544 #ifndef	DA_DEFAULT_RETRY
1545 #define	DA_DEFAULT_RETRY	4
1546 #endif
1547 
1548 #ifndef	DA_DEFAULT_SEND_ORDERED
1549 #define	DA_DEFAULT_SEND_ORDERED	1
1550 #endif
1551 
1552 static int da_poll_period = DA_DEFAULT_POLL_PERIOD;
1553 static int da_retry_count = DA_DEFAULT_RETRY;
1554 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
1555 static sbintime_t da_default_softtimeout = DA_DEFAULT_SOFTTIMEOUT;
1556 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED;
1557 static int da_disable_wp_detection = 0;
1558 static int da_enable_biospeedup = 1;
1559 static int da_enable_uma_ccbs = 1;
1560 
1561 static SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
1562     "CAM Direct Access Disk driver");
1563 SYSCTL_INT(_kern_cam_da, OID_AUTO, poll_period, CTLFLAG_RWTUN,
1564            &da_poll_period, 0, "Media polling period in seconds");
1565 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RWTUN,
1566            &da_retry_count, 0, "Normal I/O retry count");
1567 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RWTUN,
1568            &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
1569 SYSCTL_INT(_kern_cam_da, OID_AUTO, send_ordered, CTLFLAG_RWTUN,
1570            &da_send_ordered, 0, "Send Ordered Tags");
1571 SYSCTL_INT(_kern_cam_da, OID_AUTO, disable_wp_detection, CTLFLAG_RWTUN,
1572            &da_disable_wp_detection, 0,
1573 	   "Disable detection of write-protected disks");
1574 SYSCTL_INT(_kern_cam_da, OID_AUTO, enable_biospeedup, CTLFLAG_RDTUN,
1575 	    &da_enable_biospeedup, 0, "Enable BIO_SPEEDUP processing");
1576 SYSCTL_INT(_kern_cam_da, OID_AUTO, enable_uma_ccbs, CTLFLAG_RWTUN,
1577 	    &da_enable_uma_ccbs, 0, "Use UMA for CCBs");
1578 
1579 SYSCTL_PROC(_kern_cam_da, OID_AUTO, default_softtimeout,
1580     CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, 0,
1581     dasysctlsofttimeout, "I",
1582     "Soft I/O timeout (ms)");
1583 TUNABLE_INT64("kern.cam.da.default_softtimeout", &da_default_softtimeout);
1584 
1585 /*
1586  * DA_ORDEREDTAG_INTERVAL determines how often, relative
1587  * to the default timeout, we check to see whether an ordered
1588  * tagged transaction is appropriate to prevent simple tag
1589  * starvation.  Since we'd like to ensure that there is at least
1590  * 1/2 of the timeout length left for a starved transaction to
1591  * complete after we've sent an ordered tag, we must poll at least
1592  * four times in every timeout period.  This takes care of the worst
1593  * case where a starved transaction starts during an interval that
1594  * meets the requirement "don't send an ordered tag" test so it takes
1595  * us two intervals to determine that a tag must be sent.
1596  */
1597 #ifndef DA_ORDEREDTAG_INTERVAL
1598 #define DA_ORDEREDTAG_INTERVAL 4
1599 #endif
1600 
1601 static struct periph_driver dadriver =
1602 {
1603 	dainit, "da",
1604 	TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
1605 };
1606 
1607 PERIPHDRIVER_DECLARE(da, dadriver);
1608 
1609 static MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers");
1610 
1611 /*
1612  * This driver takes out references / holds in well defined pairs, never
1613  * recursively. These macros / inline functions enforce those rules. They
1614  * are only enabled with DA_TRACK_REFS or INVARIANTS. If DA_TRACK_REFS is
1615  * defined to be 2 or larger, the tracking also includes debug printfs.
1616  */
1617 #if defined(DA_TRACK_REFS) || defined(INVARIANTS)
1618 
1619 #ifndef DA_TRACK_REFS
1620 #define DA_TRACK_REFS 1
1621 #endif
1622 
1623 #if DA_TRACK_REFS > 1
1624 static const char *da_ref_text[] = {
1625 	"bogus",
1626 	"open",
1627 	"open hold",
1628 	"close hold",
1629 	"reprobe hold",
1630 	"Test Unit Ready",
1631 	"Geom",
1632 	"sysctl",
1633 	"reprobe",
1634 	"max -- also bogus"
1635 };
1636 
1637 #define DA_PERIPH_PRINT(periph, msg, args...)		\
1638 	CAM_PERIPH_PRINT(periph, msg, ##args)
1639 #else
1640 #define DA_PERIPH_PRINT(periph, msg, args...)
1641 #endif
1642 
1643 static inline void
1644 token_sanity(da_ref_token token)
1645 {
1646 	if ((unsigned)token >= DA_REF_MAX)
1647 		panic("Bad token value passed in %d\n", token);
1648 }
1649 
1650 static inline int
1651 da_periph_hold(struct cam_periph *periph, int priority, da_ref_token token)
1652 {
1653 	int err = cam_periph_hold(periph, priority);
1654 
1655 	token_sanity(token);
1656 	DA_PERIPH_PRINT(periph, "Holding device %s (%d): %d\n",
1657 	    da_ref_text[token], token, err);
1658 	if (err == 0) {
1659 		int cnt;
1660 		struct da_softc *softc = periph->softc;
1661 
1662 		cnt = atomic_fetchadd_int(&softc->ref_flags[token], 1);
1663 		if (cnt != 0)
1664 			panic("Re-holding for reason %d, cnt = %d", token, cnt);
1665 	}
1666 	return (err);
1667 }
1668 
1669 static inline void
1670 da_periph_unhold(struct cam_periph *periph, da_ref_token token)
1671 {
1672 	int cnt;
1673 	struct da_softc *softc = periph->softc;
1674 
1675 	token_sanity(token);
1676 	DA_PERIPH_PRINT(periph, "Unholding device %s (%d)\n",
1677 	    da_ref_text[token], token);
1678 	cnt = atomic_fetchadd_int(&softc->ref_flags[token], -1);
1679 	if (cnt != 1)
1680 		panic("Unholding %d with cnt = %d", token, cnt);
1681 	cam_periph_unhold(periph);
1682 }
1683 
1684 static inline int
1685 da_periph_acquire(struct cam_periph *periph, da_ref_token token)
1686 {
1687 	int err = cam_periph_acquire(periph);
1688 
1689 	token_sanity(token);
1690 	DA_PERIPH_PRINT(periph, "acquiring device %s (%d): %d\n",
1691 	    da_ref_text[token], token, err);
1692 	if (err == 0) {
1693 		int cnt;
1694 		struct da_softc *softc = periph->softc;
1695 
1696 		cnt = atomic_fetchadd_int(&softc->ref_flags[token], 1);
1697 		if (cnt != 0)
1698 			panic("Re-refing for reason %d, cnt = %d", token, cnt);
1699 	}
1700 	return (err);
1701 }
1702 
1703 static inline void
1704 da_periph_release(struct cam_periph *periph, da_ref_token token)
1705 {
1706 	int cnt;
1707 	struct da_softc *softc = periph->softc;
1708 
1709 	token_sanity(token);
1710 	DA_PERIPH_PRINT(periph, "releasing device %s (%d)\n",
1711 	    da_ref_text[token], token);
1712 	cnt = atomic_fetchadd_int(&softc->ref_flags[token], -1);
1713 	if (cnt != 1)
1714 		panic("Releasing %d with cnt = %d", token, cnt);
1715 	cam_periph_release(periph);
1716 }
1717 
1718 static inline void
1719 da_periph_release_locked(struct cam_periph *periph, da_ref_token token)
1720 {
1721 	int cnt;
1722 	struct da_softc *softc = periph->softc;
1723 
1724 	token_sanity(token);
1725 	DA_PERIPH_PRINT(periph, "releasing device (locked) %s (%d)\n",
1726 	    da_ref_text[token], token);
1727 	cnt = atomic_fetchadd_int(&softc->ref_flags[token], -1);
1728 	if (cnt != 1)
1729 		panic("releasing (locked) %d with cnt = %d", token, cnt);
1730 	cam_periph_release_locked(periph);
1731 }
1732 
1733 #define cam_periph_hold POISON
1734 #define cam_periph_unhold POISON
1735 #define cam_periph_acquire POISON
1736 #define cam_periph_release POISON
1737 #define cam_periph_release_locked POISON
1738 
1739 #else
1740 #define	da_periph_hold(periph, prio, token)	cam_periph_hold((periph), (prio))
1741 #define da_periph_unhold(periph, token)		cam_periph_unhold((periph))
1742 #define da_periph_acquire(periph, token)	cam_periph_acquire((periph))
1743 #define da_periph_release(periph, token)	cam_periph_release((periph))
1744 #define da_periph_release_locked(periph, token)	cam_periph_release_locked((periph))
1745 #endif
1746 
1747 static int
1748 daopen(struct disk *dp)
1749 {
1750 	struct cam_periph *periph;
1751 	struct da_softc *softc;
1752 	int error;
1753 
1754 	periph = (struct cam_periph *)dp->d_drv1;
1755 	if (da_periph_acquire(periph, DA_REF_OPEN) != 0) {
1756 		return (ENXIO);
1757 	}
1758 
1759 	cam_periph_lock(periph);
1760 	if ((error = da_periph_hold(periph, PRIBIO|PCATCH, DA_REF_OPEN_HOLD)) != 0) {
1761 		cam_periph_unlock(periph);
1762 		da_periph_release(periph, DA_REF_OPEN);
1763 		return (error);
1764 	}
1765 
1766 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1767 	    ("daopen\n"));
1768 
1769 	softc = (struct da_softc *)periph->softc;
1770 	dareprobe(periph);
1771 
1772 	/* Wait for the disk size update.  */
1773 	error = cam_periph_sleep(periph, &softc->disk->d_mediasize, PRIBIO,
1774 	    "dareprobe", 0);
1775 	if (error != 0)
1776 		xpt_print(periph->path, "unable to retrieve capacity data\n");
1777 
1778 	if (periph->flags & CAM_PERIPH_INVALID)
1779 		error = ENXIO;
1780 
1781 	if (error == 0 && (softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
1782 	    (softc->quirks & DA_Q_NO_PREVENT) == 0)
1783 		daprevent(periph, PR_PREVENT);
1784 
1785 	if (error == 0) {
1786 		softc->flags &= ~DA_FLAG_PACK_INVALID;
1787 		softc->flags |= DA_FLAG_OPEN;
1788 	}
1789 
1790 	da_periph_unhold(periph, DA_REF_OPEN_HOLD);
1791 	cam_periph_unlock(periph);
1792 
1793 	if (error != 0)
1794 		da_periph_release(periph, DA_REF_OPEN);
1795 
1796 	return (error);
1797 }
1798 
1799 static int
1800 daclose(struct disk *dp)
1801 {
1802 	struct	cam_periph *periph;
1803 	struct	da_softc *softc;
1804 	union	ccb *ccb;
1805 
1806 	periph = (struct cam_periph *)dp->d_drv1;
1807 	softc = (struct da_softc *)periph->softc;
1808 	cam_periph_lock(periph);
1809 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1810 	    ("daclose\n"));
1811 
1812 	if (da_periph_hold(periph, PRIBIO, DA_REF_CLOSE_HOLD) == 0) {
1813 		/* Flush disk cache. */
1814 		if ((softc->flags & DA_FLAG_DIRTY) != 0 &&
1815 		    (softc->quirks & DA_Q_NO_SYNC_CACHE) == 0 &&
1816 		    (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
1817 			ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1818 			scsi_synchronize_cache(&ccb->csio, /*retries*/1,
1819 			    /*cbfcnp*/NULL, MSG_SIMPLE_Q_TAG,
1820 			    /*begin_lba*/0, /*lb_count*/0, SSD_FULL_SIZE,
1821 			    5 * 60 * 1000);
1822 			cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
1823 			    /*sense_flags*/SF_RETRY_UA | SF_QUIET_IR,
1824 			    softc->disk->d_devstat);
1825 			softc->flags &= ~DA_FLAG_DIRTY;
1826 			xpt_release_ccb(ccb);
1827 		}
1828 
1829 		/* Allow medium removal. */
1830 		if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
1831 		    (softc->quirks & DA_Q_NO_PREVENT) == 0)
1832 			daprevent(periph, PR_ALLOW);
1833 
1834 		da_periph_unhold(periph, DA_REF_CLOSE_HOLD);
1835 	}
1836 
1837 	/*
1838 	 * If we've got removable media, mark the blocksize as
1839 	 * unavailable, since it could change when new media is
1840 	 * inserted.
1841 	 */
1842 	if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0)
1843 		softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
1844 
1845 	softc->flags &= ~DA_FLAG_OPEN;
1846 	while (softc->refcount != 0)
1847 		cam_periph_sleep(periph, &softc->refcount, PRIBIO, "daclose", 1);
1848 	cam_periph_unlock(periph);
1849 	da_periph_release(periph, DA_REF_OPEN);
1850 	return (0);
1851 }
1852 
1853 static void
1854 daschedule(struct cam_periph *periph)
1855 {
1856 	struct da_softc *softc = (struct da_softc *)periph->softc;
1857 
1858 	if (softc->state != DA_STATE_NORMAL)
1859 		return;
1860 
1861 	cam_iosched_schedule(softc->cam_iosched, periph);
1862 }
1863 
1864 /*
1865  * Actually translate the requested transfer into one the physical driver
1866  * can understand.  The transfer is described by a buf and will include
1867  * only one physical transfer.
1868  */
1869 static void
1870 dastrategy(struct bio *bp)
1871 {
1872 	struct cam_periph *periph;
1873 	struct da_softc *softc;
1874 
1875 	periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1876 	softc = (struct da_softc *)periph->softc;
1877 
1878 	cam_periph_lock(periph);
1879 
1880 	/*
1881 	 * If the device has been made invalid, error out
1882 	 */
1883 	if ((softc->flags & DA_FLAG_PACK_INVALID)) {
1884 		cam_periph_unlock(periph);
1885 		biofinish(bp, NULL, ENXIO);
1886 		return;
1887 	}
1888 
1889 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastrategy(%p)\n", bp));
1890 
1891 	/*
1892 	 * Zone commands must be ordered, because they can depend on the
1893 	 * effects of previously issued commands, and they may affect
1894 	 * commands after them.
1895 	 */
1896 	if (bp->bio_cmd == BIO_ZONE)
1897 		bp->bio_flags |= BIO_ORDERED;
1898 
1899 	/*
1900 	 * Place it in the queue of disk activities for this disk
1901 	 */
1902 	cam_iosched_queue_work(softc->cam_iosched, bp);
1903 
1904 	/*
1905 	 * Schedule ourselves for performing the work.
1906 	 */
1907 	daschedule(periph);
1908 	cam_periph_unlock(periph);
1909 
1910 	return;
1911 }
1912 
1913 static int
1914 dadump(void *arg, void *virtual, off_t offset, size_t length)
1915 {
1916 	struct	    cam_periph *periph;
1917 	struct	    da_softc *softc;
1918 	u_int	    secsize;
1919 	struct	    ccb_scsiio csio;
1920 	struct	    disk *dp;
1921 	int	    error = 0;
1922 
1923 	dp = arg;
1924 	periph = dp->d_drv1;
1925 	softc = (struct da_softc *)periph->softc;
1926 	secsize = softc->params.secsize;
1927 
1928 	if ((softc->flags & DA_FLAG_PACK_INVALID) != 0)
1929 		return (ENXIO);
1930 
1931 	memset(&csio, 0, sizeof(csio));
1932 	if (length > 0) {
1933 		xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1934 		csio.ccb_h.ccb_state = DA_CCB_DUMP;
1935 		scsi_read_write(&csio,
1936 				/*retries*/0,
1937 				/*cbfcnp*/NULL,
1938 				MSG_ORDERED_Q_TAG,
1939 				/*read*/SCSI_RW_WRITE,
1940 				/*byte2*/0,
1941 				/*minimum_cmd_size*/ softc->minimum_cmd_size,
1942 				offset / secsize,
1943 				length / secsize,
1944 				/*data_ptr*/(uint8_t *) virtual,
1945 				/*dxfer_len*/length,
1946 				/*sense_len*/SSD_FULL_SIZE,
1947 				da_default_timeout * 1000);
1948 		error = cam_periph_runccb((union ccb *)&csio, cam_periph_error,
1949 		    0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1950 		if (error != 0)
1951 			printf("Aborting dump due to I/O error.\n");
1952 		return (error);
1953 	}
1954 
1955 	/*
1956 	 * Sync the disk cache contents to the physical media.
1957 	 */
1958 	if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
1959 		xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1960 		csio.ccb_h.ccb_state = DA_CCB_DUMP;
1961 		scsi_synchronize_cache(&csio,
1962 				       /*retries*/0,
1963 				       /*cbfcnp*/NULL,
1964 				       MSG_SIMPLE_Q_TAG,
1965 				       /*begin_lba*/0,/* Cover the whole disk */
1966 				       /*lb_count*/0,
1967 				       SSD_FULL_SIZE,
1968 				       5 * 1000);
1969 		error = cam_periph_runccb((union ccb *)&csio, cam_periph_error,
1970 		    0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1971 		if (error != 0)
1972 			xpt_print(periph->path, "Synchronize cache failed\n");
1973 	}
1974 	return (error);
1975 }
1976 
1977 static int
1978 dagetattr(struct bio *bp)
1979 {
1980 	int ret;
1981 	struct cam_periph *periph;
1982 
1983 	if (g_handleattr_int(bp, "GEOM::canspeedup", da_enable_biospeedup))
1984 		return (EJUSTRETURN);
1985 
1986 	periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1987 	cam_periph_lock(periph);
1988 	ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
1989 	    periph->path);
1990 	cam_periph_unlock(periph);
1991 	if (ret == 0)
1992 		bp->bio_completed = bp->bio_length;
1993 	return ret;
1994 }
1995 
1996 static void
1997 dainit(void)
1998 {
1999 	cam_status status;
2000 
2001 	da_ccb_zone = uma_zcreate("da_ccb",
2002 	    sizeof(struct ccb_scsiio), NULL, NULL, NULL, NULL,
2003 	    UMA_ALIGN_PTR, 0);
2004 
2005 	/*
2006 	 * Install a global async callback.  This callback will
2007 	 * receive async callbacks like "new device found".
2008 	 */
2009 	status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
2010 
2011 	if (status != CAM_REQ_CMP) {
2012 		printf("da: Failed to attach master async callback "
2013 		       "due to status 0x%x!\n", status);
2014 	} else if (da_send_ordered) {
2015 		/* Register our shutdown event handler */
2016 		if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
2017 					   NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
2018 		    printf("dainit: shutdown event registration failed!\n");
2019 	}
2020 }
2021 
2022 /*
2023  * Callback from GEOM, called when it has finished cleaning up its
2024  * resources.
2025  */
2026 static void
2027 dadiskgonecb(struct disk *dp)
2028 {
2029 	struct cam_periph *periph;
2030 
2031 	periph = (struct cam_periph *)dp->d_drv1;
2032 	da_periph_release(periph, DA_REF_GEOM);
2033 }
2034 
2035 static void
2036 daoninvalidate(struct cam_periph *periph)
2037 {
2038 	struct da_softc *softc;
2039 
2040 	cam_periph_assert(periph, MA_OWNED);
2041 	softc = (struct da_softc *)periph->softc;
2042 
2043 	/*
2044 	 * De-register any async callbacks.
2045 	 */
2046 	xpt_register_async(0, daasync, periph, periph->path);
2047 
2048 	softc->flags |= DA_FLAG_PACK_INVALID;
2049 #ifdef CAM_IO_STATS
2050 	softc->invalidations++;
2051 #endif
2052 
2053 	/*
2054 	 * Return all queued I/O with ENXIO. Transactions may be queued up here
2055 	 * for retry (since we are called while there's other transactions
2056 	 * pending). Any requests in the hardware will drain before dacleanup
2057 	 * is called.
2058 	 */
2059 	cam_iosched_flush(softc->cam_iosched, NULL, ENXIO);
2060 
2061 	/*
2062 	 * Tell GEOM that we've gone away, we'll get a callback when it is
2063 	 * done cleaning up its resources.
2064 	 */
2065 	disk_gone(softc->disk);
2066 }
2067 
2068 static void
2069 dacleanup(struct cam_periph *periph)
2070 {
2071 	struct da_softc *softc;
2072 
2073 	softc = (struct da_softc *)periph->softc;
2074 
2075 	cam_periph_unlock(periph);
2076 
2077 	cam_iosched_fini(softc->cam_iosched);
2078 
2079 	/*
2080 	 * If we can't free the sysctl tree, oh well...
2081 	 */
2082 	if ((softc->flags & DA_FLAG_SCTX_INIT) != 0) {
2083 #ifdef CAM_IO_STATS
2084 		if (sysctl_ctx_free(&softc->sysctl_stats_ctx) != 0)
2085 			xpt_print(periph->path,
2086 			    "can't remove sysctl stats context\n");
2087 #endif
2088 		if (sysctl_ctx_free(&softc->sysctl_ctx) != 0)
2089 			xpt_print(periph->path,
2090 			    "can't remove sysctl context\n");
2091 	}
2092 
2093 	callout_drain(&softc->mediapoll_c);
2094 	disk_destroy(softc->disk);
2095 	callout_drain(&softc->sendordered_c);
2096 	free(softc, M_DEVBUF);
2097 	cam_periph_lock(periph);
2098 }
2099 
2100 static void
2101 daasync(void *callback_arg, uint32_t code,
2102 	struct cam_path *path, void *arg)
2103 {
2104 	struct cam_periph *periph;
2105 	struct da_softc *softc;
2106 
2107 	periph = (struct cam_periph *)callback_arg;
2108 	switch (code) {
2109 	case AC_FOUND_DEVICE:	/* callback to create periph, no locking yet */
2110 	{
2111 		struct ccb_getdev *cgd;
2112 		cam_status status;
2113 
2114 		cgd = (struct ccb_getdev *)arg;
2115 		if (cgd == NULL)
2116 			break;
2117 
2118 		if (cgd->protocol != PROTO_SCSI)
2119 			break;
2120 		if (SID_QUAL(&cgd->inq_data) != SID_QUAL_LU_CONNECTED)
2121 			break;
2122 		if (SID_TYPE(&cgd->inq_data) != T_DIRECT
2123 		    && SID_TYPE(&cgd->inq_data) != T_RBC
2124 		    && SID_TYPE(&cgd->inq_data) != T_OPTICAL
2125 		    && SID_TYPE(&cgd->inq_data) != T_ZBC_HM)
2126 			break;
2127 
2128 		/*
2129 		 * Allocate a peripheral instance for
2130 		 * this device and start the probe
2131 		 * process.
2132 		 */
2133 		status = cam_periph_alloc(daregister, daoninvalidate,
2134 					  dacleanup, dastart,
2135 					  "da", CAM_PERIPH_BIO,
2136 					  path, daasync,
2137 					  AC_FOUND_DEVICE, cgd);
2138 
2139 		if (status != CAM_REQ_CMP
2140 		 && status != CAM_REQ_INPROG)
2141 			printf("daasync: Unable to attach to new device "
2142 				"due to status 0x%x\n", status);
2143 		return;
2144 	}
2145 	case AC_ADVINFO_CHANGED:	/* Doesn't touch periph */
2146 	{
2147 		uintptr_t buftype;
2148 
2149 		buftype = (uintptr_t)arg;
2150 		if (buftype == CDAI_TYPE_PHYS_PATH) {
2151 			struct da_softc *softc;
2152 
2153 			softc = periph->softc;
2154 			disk_attr_changed(softc->disk, "GEOM::physpath",
2155 					  M_NOWAIT);
2156 		}
2157 		break;
2158 	}
2159 	case AC_UNIT_ATTENTION:		/* Called for this path: periph locked */
2160 	{
2161 		union ccb *ccb;
2162 		int error_code, sense_key, asc, ascq;
2163 
2164 		softc = (struct da_softc *)periph->softc;
2165 		ccb = (union ccb *)arg;
2166 
2167 		/*
2168 		 * Handle all UNIT ATTENTIONs except our own, as they will be
2169 		 * handled by daerror().
2170 		 */
2171 		if (xpt_path_periph(ccb->ccb_h.path) != periph &&
2172 		    scsi_extract_sense_ccb(ccb,
2173 		     &error_code, &sense_key, &asc, &ascq)) {
2174 			if (asc == 0x2A && ascq == 0x09) {
2175 				xpt_print(ccb->ccb_h.path,
2176 				    "Capacity data has changed\n");
2177 				cam_periph_assert(periph, MA_OWNED);
2178 				softc->flags &= ~DA_FLAG_PROBED;
2179 				dareprobe(periph);
2180 			} else if (asc == 0x28 && ascq == 0x00) {
2181 				cam_periph_assert(periph, MA_OWNED);
2182 				softc->flags &= ~DA_FLAG_PROBED;
2183 				disk_media_changed(softc->disk, M_NOWAIT);
2184 			} else if (asc == 0x3F && ascq == 0x03) {
2185 				xpt_print(ccb->ccb_h.path,
2186 				    "INQUIRY data has changed\n");
2187 				cam_periph_assert(periph, MA_OWNED);
2188 				softc->flags &= ~DA_FLAG_PROBED;
2189 				dareprobe(periph);
2190 			}
2191 		}
2192 		break;
2193 	}
2194 	case AC_SCSI_AEN:		/* Called for this path: periph locked */
2195 		/*
2196 		 * Appears to be currently unused for SCSI devices, only ata SIMs
2197 		 * generate this.
2198 		 */
2199 		cam_periph_assert(periph, MA_OWNED);
2200 		softc = (struct da_softc *)periph->softc;
2201 		if (!cam_iosched_has_work_flags(softc->cam_iosched, DA_WORK_TUR) &&
2202 		    (softc->flags & DA_FLAG_TUR_PENDING) == 0) {
2203 			if (da_periph_acquire(periph, DA_REF_TUR) == 0) {
2204 				cam_iosched_set_work_flags(softc->cam_iosched, DA_WORK_TUR);
2205 				daschedule(periph);
2206 			}
2207 		}
2208 		/* FALLTHROUGH */
2209 	case AC_SENT_BDR:		/* Called for this path: periph locked */
2210 	case AC_BUS_RESET:		/* Called for this path: periph locked */
2211 	{
2212 		struct ccb_hdr *ccbh;
2213 
2214 		cam_periph_assert(periph, MA_OWNED);
2215 		softc = (struct da_softc *)periph->softc;
2216 		/*
2217 		 * Don't fail on the expected unit attention
2218 		 * that will occur.
2219 		 */
2220 		softc->flags |= DA_FLAG_RETRY_UA;
2221 		LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
2222 			ccbh->ccb_state |= DA_CCB_RETRY_UA;
2223 		break;
2224 	}
2225 	case AC_INQ_CHANGED:		/* Called for this path: periph locked */
2226 		cam_periph_assert(periph, MA_OWNED);
2227 		softc = (struct da_softc *)periph->softc;
2228 		softc->flags &= ~DA_FLAG_PROBED;
2229 		dareprobe(periph);
2230 		break;
2231 	default:
2232 		break;
2233 	}
2234 	cam_periph_async(periph, code, path, arg);
2235 }
2236 
2237 static void
2238 dasysctlinit(void *context, int pending)
2239 {
2240 	struct cam_periph *periph;
2241 	struct da_softc *softc;
2242 	char tmpstr[32], tmpstr2[16];
2243 	struct ccb_trans_settings cts;
2244 
2245 	periph = (struct cam_periph *)context;
2246 	/*
2247 	 * periph was held for us when this task was enqueued
2248 	 */
2249 	if (periph->flags & CAM_PERIPH_INVALID) {
2250 		da_periph_release(periph, DA_REF_SYSCTL);
2251 		return;
2252 	}
2253 
2254 	softc = (struct da_softc *)periph->softc;
2255 	snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
2256 	snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
2257 
2258 	sysctl_ctx_init(&softc->sysctl_ctx);
2259 	cam_periph_lock(periph);
2260 	softc->flags |= DA_FLAG_SCTX_INIT;
2261 	cam_periph_unlock(periph);
2262 	softc->sysctl_tree = SYSCTL_ADD_NODE_WITH_LABEL(&softc->sysctl_ctx,
2263 		SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
2264 		CTLFLAG_RD | CTLFLAG_MPSAFE, 0, tmpstr, "device_index");
2265 	if (softc->sysctl_tree == NULL) {
2266 		printf("dasysctlinit: unable to allocate sysctl tree\n");
2267 		da_periph_release(periph, DA_REF_SYSCTL);
2268 		return;
2269 	}
2270 
2271 	/*
2272 	 * Now register the sysctl handler, so the user can change the value on
2273 	 * the fly.
2274 	 */
2275 	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2276 		OID_AUTO, "delete_method",
2277 		CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
2278 		softc, 0, dadeletemethodsysctl, "A",
2279 		"BIO_DELETE execution method");
2280 	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2281 		OID_AUTO, "delete_max",
2282 		CTLTYPE_U64 | CTLFLAG_RW | CTLFLAG_MPSAFE,
2283 		softc, 0, dadeletemaxsysctl, "Q",
2284 		"Maximum BIO_DELETE size");
2285 	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2286 		OID_AUTO, "minimum_cmd_size",
2287 		CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
2288 		&softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
2289 		"Minimum CDB size");
2290 	SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
2291 		SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
2292 		"trim_count", CTLFLAG_RD, &softc->trim_count,
2293 		"Total number of unmap/dsm commands sent");
2294 	SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
2295 		SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
2296 		"trim_ranges", CTLFLAG_RD, &softc->trim_ranges,
2297 		"Total number of ranges in unmap/dsm commands");
2298 	SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
2299 		SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
2300 		"trim_lbas", CTLFLAG_RD, &softc->trim_lbas,
2301 		"Total lbas in the unmap/dsm commands sent");
2302 
2303 	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2304 		OID_AUTO, "zone_mode",
2305 		CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
2306 		softc, 0, dazonemodesysctl, "A",
2307 		"Zone Mode");
2308 	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2309 		OID_AUTO, "zone_support",
2310 		CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
2311 		softc, 0, dazonesupsysctl, "A",
2312 		"Zone Support");
2313 	SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
2314 		SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
2315 		"optimal_seq_zones", CTLFLAG_RD, &softc->optimal_seq_zones,
2316 		"Optimal Number of Open Sequential Write Preferred Zones");
2317 	SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
2318 		SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
2319 		"optimal_nonseq_zones", CTLFLAG_RD,
2320 		&softc->optimal_nonseq_zones,
2321 		"Optimal Number of Non-Sequentially Written Sequential Write "
2322 		"Preferred Zones");
2323 	SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
2324 		SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
2325 		"max_seq_zones", CTLFLAG_RD, &softc->max_seq_zones,
2326 		"Maximum Number of Open Sequential Write Required Zones");
2327 
2328 	SYSCTL_ADD_INT(&softc->sysctl_ctx,
2329 		       SYSCTL_CHILDREN(softc->sysctl_tree),
2330 		       OID_AUTO,
2331 		       "error_inject",
2332 		       CTLFLAG_RW,
2333 		       &softc->error_inject,
2334 		       0,
2335 		       "error_inject leaf");
2336 
2337 	SYSCTL_ADD_INT(&softc->sysctl_ctx,
2338 		       SYSCTL_CHILDREN(softc->sysctl_tree),
2339 		       OID_AUTO,
2340 		       "p_type",
2341 		       CTLFLAG_RD,
2342 		       &softc->p_type,
2343 		       0,
2344 		       "DIF protection type");
2345 
2346 	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2347 	    OID_AUTO, "flags", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
2348 	    softc, 0, daflagssysctl, "A",
2349 	    "Flags for drive");
2350 	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2351 	    OID_AUTO, "rotating", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
2352 	    &softc->flags, (u_int)DA_FLAG_ROTATING, dabitsysctl, "I",
2353 	    "Rotating media *DEPRECATED* gone in FreeBSD 15");
2354 	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2355 	    OID_AUTO, "unmapped_io", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
2356 	    &softc->flags, (u_int)DA_FLAG_UNMAPPEDIO, dabitsysctl, "I",
2357 	    "Unmapped I/O support *DEPRECATED* gone in FreeBSD 15");
2358 
2359 #ifdef CAM_TEST_FAILURE
2360 	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2361 		OID_AUTO, "invalidate", CTLTYPE_U64 | CTLFLAG_RW | CTLFLAG_MPSAFE,
2362 		periph, 0, cam_periph_invalidate_sysctl, "I",
2363 		"Write 1 to invalidate the drive immediately");
2364 #endif
2365 
2366 	/*
2367 	 * Add some addressing info.
2368 	 */
2369 	memset(&cts, 0, sizeof (cts));
2370 	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
2371 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
2372 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
2373 	cam_periph_lock(periph);
2374 	xpt_action((union ccb *)&cts);
2375 	cam_periph_unlock(periph);
2376 	if (cts.ccb_h.status != CAM_REQ_CMP) {
2377 		da_periph_release(periph, DA_REF_SYSCTL);
2378 		return;
2379 	}
2380 	if (cts.protocol == PROTO_SCSI && cts.transport == XPORT_FC) {
2381 		struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
2382 		if (fc->valid & CTS_FC_VALID_WWPN) {
2383 			softc->wwpn = fc->wwpn;
2384 			SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
2385 			    SYSCTL_CHILDREN(softc->sysctl_tree),
2386 			    OID_AUTO, "wwpn", CTLFLAG_RD,
2387 			    &softc->wwpn, "World Wide Port Name");
2388 		}
2389 	}
2390 
2391 #ifdef CAM_IO_STATS
2392 	/*
2393 	 * Now add some useful stats.
2394 	 * XXX These should live in cam_periph and be common to all periphs
2395 	 */
2396 	softc->sysctl_stats_tree = SYSCTL_ADD_NODE(&softc->sysctl_stats_ctx,
2397 	    SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, "stats",
2398 	    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "Statistics");
2399 	SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
2400 		       SYSCTL_CHILDREN(softc->sysctl_stats_tree),
2401 		       OID_AUTO,
2402 		       "errors",
2403 		       CTLFLAG_RD,
2404 		       &softc->errors,
2405 		       0,
2406 		       "Transport errors reported by the SIM");
2407 	SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
2408 		       SYSCTL_CHILDREN(softc->sysctl_stats_tree),
2409 		       OID_AUTO,
2410 		       "timeouts",
2411 		       CTLFLAG_RD,
2412 		       &softc->timeouts,
2413 		       0,
2414 		       "Device timeouts reported by the SIM");
2415 	SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
2416 		       SYSCTL_CHILDREN(softc->sysctl_stats_tree),
2417 		       OID_AUTO,
2418 		       "pack_invalidations",
2419 		       CTLFLAG_RD,
2420 		       &softc->invalidations,
2421 		       0,
2422 		       "Device pack invalidations");
2423 #endif
2424 
2425 	cam_iosched_sysctl_init(softc->cam_iosched, &softc->sysctl_ctx,
2426 	    softc->sysctl_tree);
2427 
2428 	da_periph_release(periph, DA_REF_SYSCTL);
2429 }
2430 
2431 static int
2432 dadeletemaxsysctl(SYSCTL_HANDLER_ARGS)
2433 {
2434 	int error;
2435 	uint64_t value;
2436 	struct da_softc *softc;
2437 
2438 	softc = (struct da_softc *)arg1;
2439 
2440 	value = softc->disk->d_delmaxsize;
2441 	error = sysctl_handle_64(oidp, &value, 0, req);
2442 	if ((error != 0) || (req->newptr == NULL))
2443 		return (error);
2444 
2445 	/* only accept values smaller than the calculated value */
2446 	if (value > dadeletemaxsize(softc, softc->delete_method)) {
2447 		return (EINVAL);
2448 	}
2449 	softc->disk->d_delmaxsize = value;
2450 
2451 	return (0);
2452 }
2453 
2454 static int
2455 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
2456 {
2457 	int error, value;
2458 
2459 	value = *(int *)arg1;
2460 
2461 	error = sysctl_handle_int(oidp, &value, 0, req);
2462 
2463 	if ((error != 0)
2464 	 || (req->newptr == NULL))
2465 		return (error);
2466 
2467 	/*
2468 	 * Acceptable values here are 6, 10, 12 or 16.
2469 	 */
2470 	if (value < 6)
2471 		value = 6;
2472 	else if ((value > 6)
2473 	      && (value <= 10))
2474 		value = 10;
2475 	else if ((value > 10)
2476 	      && (value <= 12))
2477 		value = 12;
2478 	else if (value > 12)
2479 		value = 16;
2480 
2481 	*(int *)arg1 = value;
2482 
2483 	return (0);
2484 }
2485 
2486 static int
2487 dasysctlsofttimeout(SYSCTL_HANDLER_ARGS)
2488 {
2489 	sbintime_t value;
2490 	int error;
2491 
2492 	value = da_default_softtimeout / SBT_1MS;
2493 
2494 	error = sysctl_handle_int(oidp, (int *)&value, 0, req);
2495 	if ((error != 0) || (req->newptr == NULL))
2496 		return (error);
2497 
2498 	/* XXX Should clip this to a reasonable level */
2499 	if (value > da_default_timeout * 1000)
2500 		return (EINVAL);
2501 
2502 	da_default_softtimeout = value * SBT_1MS;
2503 	return (0);
2504 }
2505 
2506 static void
2507 dadeletemethodset(struct da_softc *softc, da_delete_methods delete_method)
2508 {
2509 
2510 	softc->delete_method = delete_method;
2511 	softc->disk->d_delmaxsize = dadeletemaxsize(softc, delete_method);
2512 	softc->delete_func = da_delete_functions[delete_method];
2513 
2514 	if (softc->delete_method > DA_DELETE_DISABLE)
2515 		softc->disk->d_flags |= DISKFLAG_CANDELETE;
2516 	else
2517 		softc->disk->d_flags &= ~DISKFLAG_CANDELETE;
2518 }
2519 
2520 static off_t
2521 dadeletemaxsize(struct da_softc *softc, da_delete_methods delete_method)
2522 {
2523 	off_t sectors;
2524 
2525 	switch(delete_method) {
2526 	case DA_DELETE_UNMAP:
2527 		sectors = (off_t)softc->unmap_max_lba;
2528 		break;
2529 	case DA_DELETE_ATA_TRIM:
2530 		sectors = (off_t)ATA_DSM_RANGE_MAX * softc->trim_max_ranges;
2531 		break;
2532 	case DA_DELETE_WS16:
2533 		sectors = omin(softc->ws_max_blks, WS16_MAX_BLKS);
2534 		break;
2535 	case DA_DELETE_ZERO:
2536 	case DA_DELETE_WS10:
2537 		sectors = omin(softc->ws_max_blks, WS10_MAX_BLKS);
2538 		break;
2539 	default:
2540 		return 0;
2541 	}
2542 
2543 	return (off_t)softc->params.secsize *
2544 	    omin(sectors, softc->params.sectors);
2545 }
2546 
2547 static void
2548 daprobedone(struct cam_periph *periph, union ccb *ccb)
2549 {
2550 	struct da_softc *softc;
2551 
2552 	softc = (struct da_softc *)periph->softc;
2553 
2554 	cam_periph_assert(periph, MA_OWNED);
2555 
2556 	dadeletemethodchoose(softc, DA_DELETE_NONE);
2557 
2558 	if (bootverbose && (softc->flags & DA_FLAG_ANNOUNCED) == 0) {
2559 		char buf[80];
2560 		int i, sep;
2561 
2562 		snprintf(buf, sizeof(buf), "Delete methods: <");
2563 		sep = 0;
2564 		for (i = 0; i <= DA_DELETE_MAX; i++) {
2565 			if ((softc->delete_available & (1 << i)) == 0 &&
2566 			    i != softc->delete_method)
2567 				continue;
2568 			if (sep)
2569 				strlcat(buf, ",", sizeof(buf));
2570 			strlcat(buf, da_delete_method_names[i],
2571 			    sizeof(buf));
2572 			if (i == softc->delete_method)
2573 				strlcat(buf, "(*)", sizeof(buf));
2574 			sep = 1;
2575 		}
2576 		strlcat(buf, ">", sizeof(buf));
2577 		printf("%s%d: %s\n", periph->periph_name,
2578 		    periph->unit_number, buf);
2579 	}
2580 	if ((softc->disk->d_flags & DISKFLAG_WRITE_PROTECT) != 0 &&
2581 	    (softc->flags & DA_FLAG_ANNOUNCED) == 0) {
2582 		printf("%s%d: Write Protected\n", periph->periph_name,
2583 		    periph->unit_number);
2584 	}
2585 
2586 	/*
2587 	 * Since our peripheral may be invalidated by an error
2588 	 * above or an external event, we must release our CCB
2589 	 * before releasing the probe lock on the peripheral.
2590 	 * The peripheral will only go away once the last lock
2591 	 * is removed, and we need it around for the CCB release
2592 	 * operation.
2593 	 */
2594 	xpt_release_ccb(ccb);
2595 	softc->state = DA_STATE_NORMAL;
2596 	softc->flags |= DA_FLAG_PROBED;
2597 	daschedule(periph);
2598 	wakeup(&softc->disk->d_mediasize);
2599 	if ((softc->flags & DA_FLAG_ANNOUNCED) == 0) {
2600 		softc->flags |= DA_FLAG_ANNOUNCED;
2601 
2602 		/*
2603 		 * We'll release this reference once GEOM calls us back via
2604 		 * dadiskgonecb(), telling us that our provider has been freed.
2605 		 */
2606 		if (da_periph_acquire(periph, DA_REF_GEOM) == 0)
2607 			disk_create(softc->disk, DISK_VERSION);
2608 
2609 		cam_periph_release_boot(periph);
2610 	}
2611 	da_periph_release_locked(periph, DA_REF_REPROBE);
2612 }
2613 
2614 static void
2615 dadeletemethodchoose(struct da_softc *softc, da_delete_methods default_method)
2616 {
2617 	int i, methods;
2618 
2619 	/* If available, prefer the method requested by user. */
2620 	i = softc->delete_method_pref;
2621 	methods = softc->delete_available | (1 << DA_DELETE_DISABLE);
2622 	if (methods & (1 << i)) {
2623 		dadeletemethodset(softc, i);
2624 		return;
2625 	}
2626 
2627 	/* Use the pre-defined order to choose the best performing delete. */
2628 	for (i = DA_DELETE_MIN; i <= DA_DELETE_MAX; i++) {
2629 		if (i == DA_DELETE_ZERO)
2630 			continue;
2631 		if (softc->delete_available & (1 << i)) {
2632 			dadeletemethodset(softc, i);
2633 			return;
2634 		}
2635 	}
2636 
2637 	/* Fallback to default. */
2638 	dadeletemethodset(softc, default_method);
2639 }
2640 
2641 static int
2642 dabitsysctl(SYSCTL_HANDLER_ARGS)
2643 {
2644 	u_int *flags = arg1;
2645 	u_int test = arg2;
2646 	int tmpout, error;
2647 
2648 	tmpout = !!(*flags & test);
2649 	error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
2650 	if (error || !req->newptr)
2651 		return (error);
2652 
2653 	return (EPERM);
2654 }
2655 
2656 static int
2657 daflagssysctl(SYSCTL_HANDLER_ARGS)
2658 {
2659 	struct sbuf sbuf;
2660 	struct da_softc *softc = arg1;
2661 	int error;
2662 
2663 	sbuf_new_for_sysctl(&sbuf, NULL, 0, req);
2664 	if (softc->flags != 0)
2665 		sbuf_printf(&sbuf, "0x%b", (unsigned)softc->flags, DA_FLAG_STRING);
2666 	else
2667 		sbuf_putc(&sbuf, '0');
2668 	error = sbuf_finish(&sbuf);
2669 	sbuf_delete(&sbuf);
2670 
2671 	return (error);
2672 }
2673 
2674 static int
2675 dadeletemethodsysctl(SYSCTL_HANDLER_ARGS)
2676 {
2677 	char buf[16];
2678 	const char *p;
2679 	struct da_softc *softc;
2680 	int i, error, value;
2681 
2682 	softc = (struct da_softc *)arg1;
2683 
2684 	value = softc->delete_method;
2685 	if (value < 0 || value > DA_DELETE_MAX)
2686 		p = "UNKNOWN";
2687 	else
2688 		p = da_delete_method_names[value];
2689 	strncpy(buf, p, sizeof(buf));
2690 	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
2691 	if (error != 0 || req->newptr == NULL)
2692 		return (error);
2693 	for (i = 0; i <= DA_DELETE_MAX; i++) {
2694 		if (strcmp(buf, da_delete_method_names[i]) == 0)
2695 			break;
2696 	}
2697 	if (i > DA_DELETE_MAX)
2698 		return (EINVAL);
2699 	softc->delete_method_pref = i;
2700 	dadeletemethodchoose(softc, DA_DELETE_NONE);
2701 	return (0);
2702 }
2703 
2704 static int
2705 dazonemodesysctl(SYSCTL_HANDLER_ARGS)
2706 {
2707 	char tmpbuf[40];
2708 	struct da_softc *softc;
2709 	int error;
2710 
2711 	softc = (struct da_softc *)arg1;
2712 
2713 	switch (softc->zone_mode) {
2714 	case DA_ZONE_DRIVE_MANAGED:
2715 		snprintf(tmpbuf, sizeof(tmpbuf), "Drive Managed");
2716 		break;
2717 	case DA_ZONE_HOST_AWARE:
2718 		snprintf(tmpbuf, sizeof(tmpbuf), "Host Aware");
2719 		break;
2720 	case DA_ZONE_HOST_MANAGED:
2721 		snprintf(tmpbuf, sizeof(tmpbuf), "Host Managed");
2722 		break;
2723 	case DA_ZONE_NONE:
2724 	default:
2725 		snprintf(tmpbuf, sizeof(tmpbuf), "Not Zoned");
2726 		break;
2727 	}
2728 
2729 	error = sysctl_handle_string(oidp, tmpbuf, sizeof(tmpbuf), req);
2730 
2731 	return (error);
2732 }
2733 
2734 static int
2735 dazonesupsysctl(SYSCTL_HANDLER_ARGS)
2736 {
2737 	struct da_softc *softc;
2738 	struct sbuf sb;
2739 	int error, first;
2740 	unsigned int i;
2741 
2742 	softc = (struct da_softc *)arg1;
2743 
2744 	first = 1;
2745 	sbuf_new_for_sysctl(&sb, NULL, 0, req);
2746 
2747 	for (i = 0; i < sizeof(da_zone_desc_table) /
2748 	     sizeof(da_zone_desc_table[0]); i++) {
2749 		if (softc->zone_flags & da_zone_desc_table[i].value) {
2750 			if (first == 0)
2751 				sbuf_cat(&sb, ", ");
2752 			else
2753 				first = 0;
2754 			sbuf_cat(&sb, da_zone_desc_table[i].desc);
2755 		}
2756 	}
2757 
2758 	if (first == 1)
2759 		sbuf_cat(&sb, "None");
2760 
2761 	error = sbuf_finish(&sb);
2762 	sbuf_delete(&sb);
2763 	return (error);
2764 }
2765 
2766 static cam_status
2767 daregister(struct cam_periph *periph, void *arg)
2768 {
2769 	struct da_softc *softc;
2770 	struct ccb_pathinq cpi;
2771 	struct ccb_getdev *cgd;
2772 	char tmpstr[80];
2773 	caddr_t match;
2774 	int quirks;
2775 
2776 	cgd = (struct ccb_getdev *)arg;
2777 	if (cgd == NULL) {
2778 		printf("daregister: no getdev CCB, can't register device\n");
2779 		return(CAM_REQ_CMP_ERR);
2780 	}
2781 
2782 	softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF,
2783 	    M_NOWAIT|M_ZERO);
2784 
2785 	if (softc == NULL) {
2786 		printf("daregister: Unable to probe new device. "
2787 		       "Unable to allocate softc\n");
2788 		return(CAM_REQ_CMP_ERR);
2789 	}
2790 
2791 	if (cam_iosched_init(&softc->cam_iosched, periph) != 0) {
2792 		printf("daregister: Unable to probe new device. "
2793 		       "Unable to allocate iosched memory\n");
2794 		free(softc, M_DEVBUF);
2795 		return(CAM_REQ_CMP_ERR);
2796 	}
2797 
2798 	LIST_INIT(&softc->pending_ccbs);
2799 	softc->state = DA_STATE_PROBE_WP;
2800 	bioq_init(&softc->delete_run_queue);
2801 	if (SID_IS_REMOVABLE(&cgd->inq_data))
2802 		softc->flags |= DA_FLAG_PACK_REMOVABLE;
2803 	softc->unmap_max_ranges = UNMAP_MAX_RANGES;
2804 	softc->unmap_max_lba = UNMAP_RANGE_MAX;
2805 	softc->unmap_gran = 0;
2806 	softc->unmap_gran_align = 0;
2807 	softc->ws_max_blks = WS16_MAX_BLKS;
2808 	softc->trim_max_ranges = ATA_TRIM_MAX_RANGES;
2809 	softc->flags |= DA_FLAG_ROTATING;
2810 
2811 	periph->softc = softc;
2812 
2813 	/*
2814 	 * See if this device has any quirks.
2815 	 */
2816 	match = cam_quirkmatch((caddr_t)&cgd->inq_data,
2817 			       (caddr_t)da_quirk_table,
2818 			       nitems(da_quirk_table),
2819 			       sizeof(*da_quirk_table), scsi_inquiry_match);
2820 
2821 	if (match != NULL)
2822 		softc->quirks = ((struct da_quirk_entry *)match)->quirks;
2823 	else
2824 		softc->quirks = DA_Q_NONE;
2825 
2826 	/* Check if the SIM does not want 6 byte commands */
2827 	xpt_path_inq(&cpi, periph->path);
2828 	if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
2829 		softc->quirks |= DA_Q_NO_6_BYTE;
2830 
2831 	/* Override quirks if tunable is set */
2832 	snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.quirks",
2833 		 periph->unit_number);
2834 	quirks = softc->quirks;
2835 	TUNABLE_INT_FETCH(tmpstr, &quirks);
2836 	softc->quirks = quirks;
2837 
2838 	if (SID_TYPE(&cgd->inq_data) == T_ZBC_HM)
2839 		softc->zone_mode = DA_ZONE_HOST_MANAGED;
2840 	else if (softc->quirks & DA_Q_SMR_DM)
2841 		softc->zone_mode = DA_ZONE_DRIVE_MANAGED;
2842 	else
2843 		softc->zone_mode = DA_ZONE_NONE;
2844 
2845 	if (softc->zone_mode != DA_ZONE_NONE) {
2846 		if (scsi_vpd_supported_page(periph, SVPD_ATA_INFORMATION)) {
2847 			if (scsi_vpd_supported_page(periph, SVPD_ZONED_BDC))
2848 				softc->zone_interface = DA_ZONE_IF_ATA_SAT;
2849 			else
2850 				softc->zone_interface = DA_ZONE_IF_ATA_PASS;
2851 		} else
2852 			softc->zone_interface = DA_ZONE_IF_SCSI;
2853 	}
2854 
2855 	TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
2856 
2857 	/*
2858 	 * Let XPT know we can use UMA-allocated CCBs.
2859 	 */
2860 	if (da_enable_uma_ccbs) {
2861 		KASSERT(da_ccb_zone != NULL,
2862 		    ("%s: NULL da_ccb_zone", __func__));
2863 		periph->ccb_zone = da_ccb_zone;
2864 	}
2865 
2866 	/*
2867 	 * Take a reference on the periph while dastart is called to finish the
2868 	 * probe.  The reference will be dropped in dadone at the end of probe.
2869 	 */
2870 	(void)da_periph_acquire(periph, DA_REF_REPROBE);
2871 
2872 	/*
2873 	 * Schedule a periodic event to occasionally send an
2874 	 * ordered tag to a device.
2875 	 */
2876 	callout_init_mtx(&softc->sendordered_c, cam_periph_mtx(periph), 0);
2877 	callout_reset_sbt(&softc->sendordered_c,
2878 	    SBT_1S / DA_ORDEREDTAG_INTERVAL * da_default_timeout, 0,
2879 	    dasendorderedtag, periph, C_PREL(1));
2880 
2881 	cam_periph_unlock(periph);
2882 	/*
2883 	 * RBC devices don't have to support READ(6), only READ(10).
2884 	 */
2885 	if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
2886 		softc->minimum_cmd_size = 10;
2887 	else
2888 		softc->minimum_cmd_size = 6;
2889 
2890 	/*
2891 	 * Load the user's default, if any.
2892 	 */
2893 	snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
2894 		 periph->unit_number);
2895 	TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
2896 
2897 	/*
2898 	 * 6, 10, 12 and 16 are the currently permissible values.
2899 	 */
2900 	if (softc->minimum_cmd_size > 12)
2901 		softc->minimum_cmd_size = 16;
2902 	else if (softc->minimum_cmd_size > 10)
2903 		softc->minimum_cmd_size = 12;
2904 	else if (softc->minimum_cmd_size > 6)
2905 		softc->minimum_cmd_size = 10;
2906 	else
2907 		softc->minimum_cmd_size = 6;
2908 
2909 	/* On first PROBE_WP request all more pages, then adjust. */
2910 	softc->mode_page = SMS_ALL_PAGES_PAGE;
2911 
2912 	/* Predict whether device may support READ CAPACITY(16). */
2913 	if (SID_ANSI_REV(&cgd->inq_data) >= SCSI_REV_SPC3 &&
2914 	    (softc->quirks & DA_Q_NO_RC16) == 0) {
2915 		softc->flags |= DA_FLAG_CAN_RC16;
2916 	}
2917 
2918 	/*
2919 	 * Register this media as a disk.
2920 	 */
2921 	softc->disk = disk_alloc();
2922 	softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
2923 			  periph->unit_number, 0,
2924 			  DEVSTAT_BS_UNAVAILABLE,
2925 			  SID_TYPE(&cgd->inq_data) |
2926 			  XPORT_DEVSTAT_TYPE(cpi.transport),
2927 			  DEVSTAT_PRIORITY_DISK);
2928 	softc->disk->d_open = daopen;
2929 	softc->disk->d_close = daclose;
2930 	softc->disk->d_strategy = dastrategy;
2931 	if (cam_sim_pollable(periph->sim))
2932 		softc->disk->d_dump = dadump;
2933 	softc->disk->d_getattr = dagetattr;
2934 	softc->disk->d_gone = dadiskgonecb;
2935 	softc->disk->d_name = "da";
2936 	softc->disk->d_drv1 = periph;
2937 	if (cpi.maxio == 0)
2938 		softc->maxio = DFLTPHYS;	/* traditional default */
2939 	else if (cpi.maxio > maxphys)
2940 		softc->maxio = maxphys;		/* for safety */
2941 	else
2942 		softc->maxio = cpi.maxio;
2943 	if (softc->quirks & DA_Q_128KB)
2944 		softc->maxio = min(softc->maxio, 128 * 1024);
2945 	softc->disk->d_maxsize = softc->maxio;
2946 	softc->disk->d_unit = periph->unit_number;
2947 	softc->disk->d_flags = DISKFLAG_DIRECT_COMPLETION | DISKFLAG_CANZONE;
2948 	if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0)
2949 		softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
2950 	if ((cpi.hba_misc & PIM_UNMAPPED) != 0) {
2951 		softc->flags |= DA_FLAG_UNMAPPEDIO;
2952 		softc->disk->d_flags |= DISKFLAG_UNMAPPED_BIO;
2953 	}
2954 	cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
2955 	    sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
2956 	strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
2957 	cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
2958 	    cgd->inq_data.product, sizeof(cgd->inq_data.product),
2959 	    sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
2960 	softc->disk->d_hba_vendor = cpi.hba_vendor;
2961 	softc->disk->d_hba_device = cpi.hba_device;
2962 	softc->disk->d_hba_subvendor = cpi.hba_subvendor;
2963 	softc->disk->d_hba_subdevice = cpi.hba_subdevice;
2964 	snprintf(softc->disk->d_attachment, sizeof(softc->disk->d_attachment),
2965 	    "%s%d", cpi.dev_name, cpi.unit_number);
2966 	cam_periph_lock(periph);
2967 
2968 	/*
2969 	 * Add async callbacks for events of interest.
2970 	 * I don't bother checking if this fails as,
2971 	 * in most cases, the system will function just
2972 	 * fine without them and the only alternative
2973 	 * would be to not attach the device on failure.
2974 	 */
2975 	xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
2976 	    AC_ADVINFO_CHANGED | AC_SCSI_AEN | AC_UNIT_ATTENTION |
2977 	    AC_INQ_CHANGED, daasync, periph, periph->path);
2978 
2979 	/*
2980 	 * Schedule a periodic media polling events.
2981 	 */
2982 	callout_init_mtx(&softc->mediapoll_c, cam_periph_mtx(periph), 0);
2983 	if ((softc->flags & DA_FLAG_PACK_REMOVABLE) &&
2984 	    (cgd->inq_flags & SID_AEN) == 0 &&
2985 	    da_poll_period != 0) {
2986 		callout_reset_sbt(&softc->mediapoll_c, da_poll_period * SBT_1S,
2987 		    0, damediapoll, periph, C_PREL(1));
2988 	}
2989 
2990 	/* Released after probe when disk_create() call pass it to GEOM. */
2991 	cam_periph_hold_boot(periph);
2992 
2993 	xpt_schedule(periph, CAM_PRIORITY_DEV);
2994 	return(CAM_REQ_CMP);
2995 }
2996 
2997 static int
2998 da_zone_bio_to_scsi(int disk_zone_cmd)
2999 {
3000 	switch (disk_zone_cmd) {
3001 	case DISK_ZONE_OPEN:
3002 		return ZBC_OUT_SA_OPEN;
3003 	case DISK_ZONE_CLOSE:
3004 		return ZBC_OUT_SA_CLOSE;
3005 	case DISK_ZONE_FINISH:
3006 		return ZBC_OUT_SA_FINISH;
3007 	case DISK_ZONE_RWP:
3008 		return ZBC_OUT_SA_RWP;
3009 	}
3010 
3011 	return -1;
3012 }
3013 
3014 static int
3015 da_zone_cmd(struct cam_periph *periph, union ccb *ccb, struct bio *bp,
3016 	    int *queue_ccb)
3017 {
3018 	struct da_softc *softc;
3019 	int error;
3020 
3021 	error = 0;
3022 
3023 	if (bp->bio_cmd != BIO_ZONE) {
3024 		error = EINVAL;
3025 		goto bailout;
3026 	}
3027 
3028 	softc = periph->softc;
3029 
3030 	switch (bp->bio_zone.zone_cmd) {
3031 	case DISK_ZONE_OPEN:
3032 	case DISK_ZONE_CLOSE:
3033 	case DISK_ZONE_FINISH:
3034 	case DISK_ZONE_RWP: {
3035 		int zone_flags;
3036 		int zone_sa;
3037 		uint64_t lba;
3038 
3039 		zone_sa = da_zone_bio_to_scsi(bp->bio_zone.zone_cmd);
3040 		if (zone_sa == -1) {
3041 			xpt_print(periph->path, "Cannot translate zone "
3042 			    "cmd %#x to SCSI\n", bp->bio_zone.zone_cmd);
3043 			error = EINVAL;
3044 			goto bailout;
3045 		}
3046 
3047 		zone_flags = 0;
3048 		lba = bp->bio_zone.zone_params.rwp.id;
3049 
3050 		if (bp->bio_zone.zone_params.rwp.flags &
3051 		    DISK_ZONE_RWP_FLAG_ALL)
3052 			zone_flags |= ZBC_OUT_ALL;
3053 
3054 		if (softc->zone_interface != DA_ZONE_IF_ATA_PASS) {
3055 			scsi_zbc_out(&ccb->csio,
3056 				     /*retries*/ da_retry_count,
3057 				     /*cbfcnp*/ dadone,
3058 				     /*tag_action*/ MSG_SIMPLE_Q_TAG,
3059 				     /*service_action*/ zone_sa,
3060 				     /*zone_id*/ lba,
3061 				     /*zone_flags*/ zone_flags,
3062 				     /*data_ptr*/ NULL,
3063 				     /*dxfer_len*/ 0,
3064 				     /*sense_len*/ SSD_FULL_SIZE,
3065 				     /*timeout*/ da_default_timeout * 1000);
3066 		} else {
3067 			/*
3068 			 * Note that in this case, even though we can
3069 			 * technically use NCQ, we don't bother for several
3070 			 * reasons:
3071 			 * 1. It hasn't been tested on a SAT layer that
3072 			 *    supports it.  This is new as of SAT-4.
3073 			 * 2. Even when there is a SAT layer that supports
3074 			 *    it, that SAT layer will also probably support
3075 			 *    ZBC -> ZAC translation, since they are both
3076 			 *    in the SAT-4 spec.
3077 			 * 3. Translation will likely be preferable to ATA
3078 			 *    passthrough.  LSI / Avago at least single
3079 			 *    steps ATA passthrough commands in the HBA,
3080 			 *    regardless of protocol, so unless that
3081 			 *    changes, there is a performance penalty for
3082 			 *    doing ATA passthrough no matter whether
3083 			 *    you're using NCQ/FPDMA, DMA or PIO.
3084 			 * 4. It requires a 32-byte CDB, which at least at
3085 			 *    this point in CAM requires a CDB pointer, which
3086 			 *    would require us to allocate an additional bit
3087 			 *    of storage separate from the CCB.
3088 			 */
3089 			error = scsi_ata_zac_mgmt_out(&ccb->csio,
3090 			    /*retries*/ da_retry_count,
3091 			    /*cbfcnp*/ dadone,
3092 			    /*tag_action*/ MSG_SIMPLE_Q_TAG,
3093 			    /*use_ncq*/ 0,
3094 			    /*zm_action*/ zone_sa,
3095 			    /*zone_id*/ lba,
3096 			    /*zone_flags*/ zone_flags,
3097 			    /*data_ptr*/ NULL,
3098 			    /*dxfer_len*/ 0,
3099 			    /*cdb_storage*/ NULL,
3100 			    /*cdb_storage_len*/ 0,
3101 			    /*sense_len*/ SSD_FULL_SIZE,
3102 			    /*timeout*/ da_default_timeout * 1000);
3103 			if (error != 0) {
3104 				error = EINVAL;
3105 				xpt_print(periph->path,
3106 				    "scsi_ata_zac_mgmt_out() returned an "
3107 				    "error!");
3108 				goto bailout;
3109 			}
3110 		}
3111 		*queue_ccb = 1;
3112 
3113 		break;
3114 	}
3115 	case DISK_ZONE_REPORT_ZONES: {
3116 		uint8_t *rz_ptr;
3117 		uint32_t num_entries, alloc_size;
3118 		struct disk_zone_report *rep;
3119 
3120 		rep = &bp->bio_zone.zone_params.report;
3121 
3122 		num_entries = rep->entries_allocated;
3123 		if (num_entries == 0) {
3124 			xpt_print(periph->path, "No entries allocated for "
3125 			    "Report Zones request\n");
3126 			error = EINVAL;
3127 			goto bailout;
3128 		}
3129 		alloc_size = sizeof(struct scsi_report_zones_hdr) +
3130 		    (sizeof(struct scsi_report_zones_desc) * num_entries);
3131 		alloc_size = min(alloc_size, softc->disk->d_maxsize);
3132 		rz_ptr = malloc(alloc_size, M_SCSIDA, M_NOWAIT | M_ZERO);
3133 		if (rz_ptr == NULL) {
3134 			xpt_print(periph->path, "Unable to allocate memory "
3135 			   "for Report Zones request\n");
3136 			error = ENOMEM;
3137 			goto bailout;
3138 		}
3139 
3140 		if (softc->zone_interface != DA_ZONE_IF_ATA_PASS) {
3141 			scsi_zbc_in(&ccb->csio,
3142 				    /*retries*/ da_retry_count,
3143 				    /*cbcfnp*/ dadone,
3144 				    /*tag_action*/ MSG_SIMPLE_Q_TAG,
3145 				    /*service_action*/ ZBC_IN_SA_REPORT_ZONES,
3146 				    /*zone_start_lba*/ rep->starting_id,
3147 				    /*zone_options*/ rep->rep_options,
3148 				    /*data_ptr*/ rz_ptr,
3149 				    /*dxfer_len*/ alloc_size,
3150 				    /*sense_len*/ SSD_FULL_SIZE,
3151 				    /*timeout*/ da_default_timeout * 1000);
3152 		} else {
3153 			/*
3154 			 * Note that in this case, even though we can
3155 			 * technically use NCQ, we don't bother for several
3156 			 * reasons:
3157 			 * 1. It hasn't been tested on a SAT layer that
3158 			 *    supports it.  This is new as of SAT-4.
3159 			 * 2. Even when there is a SAT layer that supports
3160 			 *    it, that SAT layer will also probably support
3161 			 *    ZBC -> ZAC translation, since they are both
3162 			 *    in the SAT-4 spec.
3163 			 * 3. Translation will likely be preferable to ATA
3164 			 *    passthrough.  LSI / Avago at least single
3165 			 *    steps ATA passthrough commands in the HBA,
3166 			 *    regardless of protocol, so unless that
3167 			 *    changes, there is a performance penalty for
3168 			 *    doing ATA passthrough no matter whether
3169 			 *    you're using NCQ/FPDMA, DMA or PIO.
3170 			 * 4. It requires a 32-byte CDB, which at least at
3171 			 *    this point in CAM requires a CDB pointer, which
3172 			 *    would require us to allocate an additional bit
3173 			 *    of storage separate from the CCB.
3174 			 */
3175 			error = scsi_ata_zac_mgmt_in(&ccb->csio,
3176 			    /*retries*/ da_retry_count,
3177 			    /*cbcfnp*/ dadone,
3178 			    /*tag_action*/ MSG_SIMPLE_Q_TAG,
3179 			    /*use_ncq*/ 0,
3180 			    /*zm_action*/ ATA_ZM_REPORT_ZONES,
3181 			    /*zone_id*/ rep->starting_id,
3182 			    /*zone_flags*/ rep->rep_options,
3183 			    /*data_ptr*/ rz_ptr,
3184 			    /*dxfer_len*/ alloc_size,
3185 			    /*cdb_storage*/ NULL,
3186 			    /*cdb_storage_len*/ 0,
3187 			    /*sense_len*/ SSD_FULL_SIZE,
3188 			    /*timeout*/ da_default_timeout * 1000);
3189 			if (error != 0) {
3190 				error = EINVAL;
3191 				xpt_print(periph->path,
3192 				    "scsi_ata_zac_mgmt_in() returned an "
3193 				    "error!");
3194 				goto bailout;
3195 			}
3196 		}
3197 
3198 		/*
3199 		 * For BIO_ZONE, this isn't normally needed.  However, it
3200 		 * is used by devstat_end_transaction_bio() to determine
3201 		 * how much data was transferred.
3202 		 */
3203 		/*
3204 		 * XXX KDM we have a problem.  But I'm not sure how to fix
3205 		 * it.  devstat uses bio_bcount - bio_resid to calculate
3206 		 * the amount of data transferred.   The GEOM disk code
3207 		 * uses bio_length - bio_resid to calculate the amount of
3208 		 * data in bio_completed.  We have different structure
3209 		 * sizes above and below the ada(4) driver.  So, if we
3210 		 * use the sizes above, the amount transferred won't be
3211 		 * quite accurate for devstat.  If we use different sizes
3212 		 * for bio_bcount and bio_length (above and below
3213 		 * respectively), then the residual needs to match one or
3214 		 * the other.  Everything is calculated after the bio
3215 		 * leaves the driver, so changing the values around isn't
3216 		 * really an option.  For now, just set the count to the
3217 		 * passed in length.  This means that the calculations
3218 		 * above (e.g. bio_completed) will be correct, but the
3219 		 * amount of data reported to devstat will be slightly
3220 		 * under or overstated.
3221 		 */
3222 		bp->bio_bcount = bp->bio_length;
3223 
3224 		*queue_ccb = 1;
3225 
3226 		break;
3227 	}
3228 	case DISK_ZONE_GET_PARAMS: {
3229 		struct disk_zone_disk_params *params;
3230 
3231 		params = &bp->bio_zone.zone_params.disk_params;
3232 		bzero(params, sizeof(*params));
3233 
3234 		switch (softc->zone_mode) {
3235 		case DA_ZONE_DRIVE_MANAGED:
3236 			params->zone_mode = DISK_ZONE_MODE_DRIVE_MANAGED;
3237 			break;
3238 		case DA_ZONE_HOST_AWARE:
3239 			params->zone_mode = DISK_ZONE_MODE_HOST_AWARE;
3240 			break;
3241 		case DA_ZONE_HOST_MANAGED:
3242 			params->zone_mode = DISK_ZONE_MODE_HOST_MANAGED;
3243 			break;
3244 		default:
3245 		case DA_ZONE_NONE:
3246 			params->zone_mode = DISK_ZONE_MODE_NONE;
3247 			break;
3248 		}
3249 
3250 		if (softc->zone_flags & DA_ZONE_FLAG_URSWRZ)
3251 			params->flags |= DISK_ZONE_DISK_URSWRZ;
3252 
3253 		if (softc->zone_flags & DA_ZONE_FLAG_OPT_SEQ_SET) {
3254 			params->optimal_seq_zones = softc->optimal_seq_zones;
3255 			params->flags |= DISK_ZONE_OPT_SEQ_SET;
3256 		}
3257 
3258 		if (softc->zone_flags & DA_ZONE_FLAG_OPT_NONSEQ_SET) {
3259 			params->optimal_nonseq_zones =
3260 			    softc->optimal_nonseq_zones;
3261 			params->flags |= DISK_ZONE_OPT_NONSEQ_SET;
3262 		}
3263 
3264 		if (softc->zone_flags & DA_ZONE_FLAG_MAX_SEQ_SET) {
3265 			params->max_seq_zones = softc->max_seq_zones;
3266 			params->flags |= DISK_ZONE_MAX_SEQ_SET;
3267 		}
3268 		if (softc->zone_flags & DA_ZONE_FLAG_RZ_SUP)
3269 			params->flags |= DISK_ZONE_RZ_SUP;
3270 
3271 		if (softc->zone_flags & DA_ZONE_FLAG_OPEN_SUP)
3272 			params->flags |= DISK_ZONE_OPEN_SUP;
3273 
3274 		if (softc->zone_flags & DA_ZONE_FLAG_CLOSE_SUP)
3275 			params->flags |= DISK_ZONE_CLOSE_SUP;
3276 
3277 		if (softc->zone_flags & DA_ZONE_FLAG_FINISH_SUP)
3278 			params->flags |= DISK_ZONE_FINISH_SUP;
3279 
3280 		if (softc->zone_flags & DA_ZONE_FLAG_RWP_SUP)
3281 			params->flags |= DISK_ZONE_RWP_SUP;
3282 		break;
3283 	}
3284 	default:
3285 		break;
3286 	}
3287 bailout:
3288 	return (error);
3289 }
3290 
3291 static void
3292 dastart(struct cam_periph *periph, union ccb *start_ccb)
3293 {
3294 	struct da_softc *softc;
3295 
3296 	cam_periph_assert(periph, MA_OWNED);
3297 	softc = (struct da_softc *)periph->softc;
3298 
3299 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastart\n"));
3300 
3301 skipstate:
3302 	switch (softc->state) {
3303 	case DA_STATE_NORMAL:
3304 	{
3305 		struct bio *bp;
3306 		uint8_t tag_code;
3307 
3308 more:
3309 		bp = cam_iosched_next_bio(softc->cam_iosched);
3310 		if (bp == NULL) {
3311 			if (cam_iosched_has_work_flags(softc->cam_iosched,
3312 			    DA_WORK_TUR)) {
3313 				softc->flags |= DA_FLAG_TUR_PENDING;
3314 				cam_iosched_clr_work_flags(softc->cam_iosched,
3315 				    DA_WORK_TUR);
3316 				scsi_test_unit_ready(&start_ccb->csio,
3317 				     /*retries*/ da_retry_count,
3318 				     dadone_tur,
3319 				     MSG_SIMPLE_Q_TAG,
3320 				     SSD_FULL_SIZE,
3321 				     da_default_timeout * 1000);
3322 				start_ccb->ccb_h.ccb_bp = NULL;
3323 				start_ccb->ccb_h.ccb_state = DA_CCB_TUR;
3324 				xpt_action(start_ccb);
3325 			} else
3326 				xpt_release_ccb(start_ccb);
3327 			break;
3328 		}
3329 
3330 		if (bp->bio_cmd == BIO_DELETE) {
3331 			if (softc->delete_func != NULL) {
3332 				softc->delete_func(periph, start_ccb, bp);
3333 				goto out;
3334 			} else {
3335 				/*
3336 				 * Not sure this is possible, but failsafe by
3337 				 * lying and saying "sure, done."
3338 				 */
3339 				biofinish(bp, NULL, 0);
3340 				goto more;
3341 			}
3342 		}
3343 
3344 		if (cam_iosched_has_work_flags(softc->cam_iosched,
3345 		    DA_WORK_TUR)) {
3346 			cam_iosched_clr_work_flags(softc->cam_iosched,
3347 			    DA_WORK_TUR);
3348 			da_periph_release_locked(periph, DA_REF_TUR);
3349 		}
3350 
3351 		if ((bp->bio_flags & BIO_ORDERED) != 0 ||
3352 		    (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
3353 			softc->flags &= ~DA_FLAG_NEED_OTAG;
3354 			softc->flags |= DA_FLAG_WAS_OTAG;
3355 			tag_code = MSG_ORDERED_Q_TAG;
3356 		} else {
3357 			tag_code = MSG_SIMPLE_Q_TAG;
3358 		}
3359 
3360 		switch (bp->bio_cmd) {
3361 		case BIO_WRITE:
3362 		case BIO_READ:
3363 		{
3364 			void *data_ptr;
3365 			int rw_op;
3366 
3367 			biotrack(bp, __func__);
3368 
3369 			if (bp->bio_cmd == BIO_WRITE) {
3370 				softc->flags |= DA_FLAG_DIRTY;
3371 				rw_op = SCSI_RW_WRITE;
3372 			} else {
3373 				rw_op = SCSI_RW_READ;
3374 			}
3375 
3376 			data_ptr = bp->bio_data;
3377 			if ((bp->bio_flags & (BIO_UNMAPPED|BIO_VLIST)) != 0) {
3378 				rw_op |= SCSI_RW_BIO;
3379 				data_ptr = bp;
3380 			}
3381 
3382 			scsi_read_write(&start_ccb->csio,
3383 					/*retries*/da_retry_count,
3384 					/*cbfcnp*/dadone,
3385 					/*tag_action*/tag_code,
3386 					rw_op,
3387 					/*byte2*/0,
3388 					softc->minimum_cmd_size,
3389 					/*lba*/bp->bio_pblkno,
3390 					/*block_count*/bp->bio_bcount /
3391 					softc->params.secsize,
3392 					data_ptr,
3393 					/*dxfer_len*/ bp->bio_bcount,
3394 					/*sense_len*/SSD_FULL_SIZE,
3395 					da_default_timeout * 1000);
3396 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
3397 			start_ccb->csio.bio = bp;
3398 #endif
3399 			break;
3400 		}
3401 		case BIO_FLUSH:
3402 			/*
3403 			 * If we don't support sync cache, or the disk
3404 			 * isn't dirty, FLUSH is a no-op.  Use the
3405 			 * allocated CCB for the next bio if one is
3406 			 * available.
3407 			 */
3408 			if ((softc->quirks & DA_Q_NO_SYNC_CACHE) != 0 ||
3409 			    (softc->flags & DA_FLAG_DIRTY) == 0) {
3410 				biodone(bp);
3411 				goto skipstate;
3412 			}
3413 
3414 			/*
3415 			 * BIO_FLUSH doesn't currently communicate
3416 			 * range data, so we synchronize the cache
3417 			 * over the whole disk.
3418 			 */
3419 			scsi_synchronize_cache(&start_ccb->csio,
3420 					       /*retries*/1,
3421 					       /*cbfcnp*/dadone,
3422 					       /*tag_action*/tag_code,
3423 					       /*begin_lba*/0,
3424 					       /*lb_count*/0,
3425 					       SSD_FULL_SIZE,
3426 					       da_default_timeout*1000);
3427 			/*
3428 			 * Clear the dirty flag before sending the command.
3429 			 * Either this sync cache will be successful, or it
3430 			 * will fail after a retry.  If it fails, it is
3431 			 * unlikely to be successful if retried later, so
3432 			 * we'll save ourselves time by just marking the
3433 			 * device clean.
3434 			 */
3435 			softc->flags &= ~DA_FLAG_DIRTY;
3436 			break;
3437 		case BIO_ZONE: {
3438 			int error, queue_ccb;
3439 
3440 			queue_ccb = 0;
3441 
3442 			error = da_zone_cmd(periph, start_ccb, bp,&queue_ccb);
3443 			if ((error != 0)
3444 			 || (queue_ccb == 0)) {
3445 				biofinish(bp, NULL, error);
3446 				xpt_release_ccb(start_ccb);
3447 				return;
3448 			}
3449 			break;
3450 		}
3451 		default:
3452 			biofinish(bp, NULL, EOPNOTSUPP);
3453 			xpt_release_ccb(start_ccb);
3454 			return;
3455 		}
3456 		start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
3457 		start_ccb->ccb_h.flags |= CAM_UNLOCKED;
3458 		start_ccb->ccb_h.softtimeout = sbttotv(da_default_softtimeout);
3459 
3460 out:
3461 		LIST_INSERT_HEAD(&softc->pending_ccbs,
3462 				 &start_ccb->ccb_h, periph_links.le);
3463 
3464 		/* We expect a unit attention from this device */
3465 		if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
3466 			start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
3467 			softc->flags &= ~DA_FLAG_RETRY_UA;
3468 		}
3469 
3470 		start_ccb->ccb_h.ccb_bp = bp;
3471 		softc->refcount++;
3472 		cam_periph_unlock(periph);
3473 		xpt_action(start_ccb);
3474 		cam_periph_lock(periph);
3475 
3476 		/* May have more work to do, so ensure we stay scheduled */
3477 		daschedule(periph);
3478 		break;
3479 	}
3480 	case DA_STATE_PROBE_WP:
3481 	{
3482 		void  *mode_buf;
3483 		int    mode_buf_len;
3484 
3485 		if (da_disable_wp_detection || softc->mode_page < 0) {
3486 			if ((softc->flags & DA_FLAG_CAN_RC16) != 0)
3487 				softc->state = DA_STATE_PROBE_RC16;
3488 			else
3489 				softc->state = DA_STATE_PROBE_RC;
3490 			goto skipstate;
3491 		}
3492 		mode_buf_len = 192;
3493 		mode_buf = malloc(mode_buf_len, M_SCSIDA, M_NOWAIT);
3494 		if (mode_buf == NULL) {
3495 			xpt_print(periph->path, "Unable to send mode sense - "
3496 			    "malloc failure\n");
3497 			if ((softc->flags & DA_FLAG_CAN_RC16) != 0)
3498 				softc->state = DA_STATE_PROBE_RC16;
3499 			else
3500 				softc->state = DA_STATE_PROBE_RC;
3501 			goto skipstate;
3502 		}
3503 		scsi_mode_sense_len(&start_ccb->csio,
3504 				    /*retries*/ da_retry_count,
3505 				    /*cbfcnp*/ dadone_probewp,
3506 				    /*tag_action*/ MSG_SIMPLE_Q_TAG,
3507 				    /*dbd*/ FALSE,
3508 				    /*pc*/ SMS_PAGE_CTRL_CURRENT,
3509 				    /*page*/ softc->mode_page,
3510 				    /*param_buf*/ mode_buf,
3511 				    /*param_len*/ mode_buf_len,
3512 				    /*minimum_cmd_size*/ softc->minimum_cmd_size,
3513 				    /*sense_len*/ SSD_FULL_SIZE,
3514 				    /*timeout*/ da_default_timeout * 1000);
3515 		start_ccb->ccb_h.ccb_bp = NULL;
3516 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_WP;
3517 		xpt_action(start_ccb);
3518 		break;
3519 	}
3520 	case DA_STATE_PROBE_RC:
3521 	{
3522 		struct scsi_read_capacity_data *rcap;
3523 
3524 		rcap = (struct scsi_read_capacity_data *)
3525 		    malloc(sizeof(*rcap), M_SCSIDA, M_NOWAIT|M_ZERO);
3526 		if (rcap == NULL) {
3527 			printf("dastart: Couldn't malloc read_capacity data\n");
3528 			/* da_free_periph??? */
3529 			break;
3530 		}
3531 		scsi_read_capacity(&start_ccb->csio,
3532 				   /*retries*/da_retry_count,
3533 				   dadone_proberc,
3534 				   MSG_SIMPLE_Q_TAG,
3535 				   rcap,
3536 				   SSD_FULL_SIZE,
3537 				   /*timeout*/5000);
3538 		start_ccb->ccb_h.ccb_bp = NULL;
3539 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC;
3540 		xpt_action(start_ccb);
3541 		break;
3542 	}
3543 	case DA_STATE_PROBE_RC16:
3544 	{
3545 		struct scsi_read_capacity_data_long *rcaplong;
3546 
3547 		rcaplong = (struct scsi_read_capacity_data_long *)
3548 			malloc(sizeof(*rcaplong), M_SCSIDA, M_NOWAIT|M_ZERO);
3549 		if (rcaplong == NULL) {
3550 			printf("dastart: Couldn't malloc read_capacity data\n");
3551 			/* da_free_periph??? */
3552 			break;
3553 		}
3554 		scsi_read_capacity_16(&start_ccb->csio,
3555 				      /*retries*/ da_retry_count,
3556 				      /*cbfcnp*/ dadone_proberc,
3557 				      /*tag_action*/ MSG_SIMPLE_Q_TAG,
3558 				      /*lba*/ 0,
3559 				      /*reladr*/ 0,
3560 				      /*pmi*/ 0,
3561 				      /*rcap_buf*/ (uint8_t *)rcaplong,
3562 				      /*rcap_buf_len*/ sizeof(*rcaplong),
3563 				      /*sense_len*/ SSD_FULL_SIZE,
3564 				      /*timeout*/ da_default_timeout * 1000);
3565 		start_ccb->ccb_h.ccb_bp = NULL;
3566 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC16;
3567 		xpt_action(start_ccb);
3568 		break;
3569 	}
3570 	case DA_STATE_PROBE_LBP:
3571 	{
3572 		struct scsi_vpd_logical_block_prov *lbp;
3573 
3574 		if (!scsi_vpd_supported_page(periph, SVPD_LBP)) {
3575 			/*
3576 			 * If we get here we don't support any SBC-3 delete
3577 			 * methods with UNMAP as the Logical Block Provisioning
3578 			 * VPD page support is required for devices which
3579 			 * support it according to T10/1799-D Revision 31
3580 			 * however older revisions of the spec don't mandate
3581 			 * this so we currently don't remove these methods
3582 			 * from the available set.
3583 			 */
3584 			softc->state = DA_STATE_PROBE_BLK_LIMITS;
3585 			goto skipstate;
3586 		}
3587 
3588 		lbp = (struct scsi_vpd_logical_block_prov *)
3589 			malloc(sizeof(*lbp), M_SCSIDA, M_NOWAIT|M_ZERO);
3590 
3591 		if (lbp == NULL) {
3592 			printf("dastart: Couldn't malloc lbp data\n");
3593 			/* da_free_periph??? */
3594 			break;
3595 		}
3596 
3597 		scsi_inquiry(&start_ccb->csio,
3598 			     /*retries*/da_retry_count,
3599 			     /*cbfcnp*/dadone_probelbp,
3600 			     /*tag_action*/MSG_SIMPLE_Q_TAG,
3601 			     /*inq_buf*/(uint8_t *)lbp,
3602 			     /*inq_len*/sizeof(*lbp),
3603 			     /*evpd*/TRUE,
3604 			     /*page_code*/SVPD_LBP,
3605 			     /*sense_len*/SSD_MIN_SIZE,
3606 			     /*timeout*/da_default_timeout * 1000);
3607 		start_ccb->ccb_h.ccb_bp = NULL;
3608 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_LBP;
3609 		xpt_action(start_ccb);
3610 		break;
3611 	}
3612 	case DA_STATE_PROBE_BLK_LIMITS:
3613 	{
3614 		struct scsi_vpd_block_limits *block_limits;
3615 
3616 		if (!scsi_vpd_supported_page(periph, SVPD_BLOCK_LIMITS)) {
3617 			/* Not supported skip to next probe */
3618 			softc->state = DA_STATE_PROBE_BDC;
3619 			goto skipstate;
3620 		}
3621 
3622 		block_limits = (struct scsi_vpd_block_limits *)
3623 			malloc(sizeof(*block_limits), M_SCSIDA, M_NOWAIT|M_ZERO);
3624 
3625 		if (block_limits == NULL) {
3626 			printf("dastart: Couldn't malloc block_limits data\n");
3627 			/* da_free_periph??? */
3628 			break;
3629 		}
3630 
3631 		scsi_inquiry(&start_ccb->csio,
3632 			     /*retries*/da_retry_count,
3633 			     /*cbfcnp*/dadone_probeblklimits,
3634 			     /*tag_action*/MSG_SIMPLE_Q_TAG,
3635 			     /*inq_buf*/(uint8_t *)block_limits,
3636 			     /*inq_len*/sizeof(*block_limits),
3637 			     /*evpd*/TRUE,
3638 			     /*page_code*/SVPD_BLOCK_LIMITS,
3639 			     /*sense_len*/SSD_MIN_SIZE,
3640 			     /*timeout*/da_default_timeout * 1000);
3641 		start_ccb->ccb_h.ccb_bp = NULL;
3642 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BLK_LIMITS;
3643 		xpt_action(start_ccb);
3644 		break;
3645 	}
3646 	case DA_STATE_PROBE_BDC:
3647 	{
3648 		struct scsi_vpd_block_device_characteristics *bdc;
3649 
3650 		if (!scsi_vpd_supported_page(periph, SVPD_BDC)) {
3651 			softc->state = DA_STATE_PROBE_ATA;
3652 			goto skipstate;
3653 		}
3654 
3655 		bdc = (struct scsi_vpd_block_device_characteristics *)
3656 			malloc(sizeof(*bdc), M_SCSIDA, M_NOWAIT|M_ZERO);
3657 
3658 		if (bdc == NULL) {
3659 			printf("dastart: Couldn't malloc bdc data\n");
3660 			/* da_free_periph??? */
3661 			break;
3662 		}
3663 
3664 		scsi_inquiry(&start_ccb->csio,
3665 			     /*retries*/da_retry_count,
3666 			     /*cbfcnp*/dadone_probebdc,
3667 			     /*tag_action*/MSG_SIMPLE_Q_TAG,
3668 			     /*inq_buf*/(uint8_t *)bdc,
3669 			     /*inq_len*/sizeof(*bdc),
3670 			     /*evpd*/TRUE,
3671 			     /*page_code*/SVPD_BDC,
3672 			     /*sense_len*/SSD_MIN_SIZE,
3673 			     /*timeout*/da_default_timeout * 1000);
3674 		start_ccb->ccb_h.ccb_bp = NULL;
3675 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BDC;
3676 		xpt_action(start_ccb);
3677 		break;
3678 	}
3679 	case DA_STATE_PROBE_ATA:
3680 	{
3681 		struct ata_params *ata_params;
3682 
3683 		if (!scsi_vpd_supported_page(periph, SVPD_ATA_INFORMATION)) {
3684 			if ((softc->zone_mode == DA_ZONE_HOST_AWARE)
3685 			 || (softc->zone_mode == DA_ZONE_HOST_MANAGED)) {
3686 				/*
3687 				 * Note that if the ATA VPD page isn't
3688 				 * supported, we aren't talking to an ATA
3689 				 * device anyway.  Support for that VPD
3690 				 * page is mandatory for SCSI to ATA (SAT)
3691 				 * translation layers.
3692 				 */
3693 				softc->state = DA_STATE_PROBE_ZONE;
3694 				goto skipstate;
3695 			}
3696 			daprobedone(periph, start_ccb);
3697 			break;
3698 		}
3699 
3700 		ata_params = &periph->path->device->ident_data;
3701 
3702 		scsi_ata_identify(&start_ccb->csio,
3703 				  /*retries*/da_retry_count,
3704 				  /*cbfcnp*/dadone_probeata,
3705                                   /*tag_action*/MSG_SIMPLE_Q_TAG,
3706 				  /*data_ptr*/(uint8_t *)ata_params,
3707 				  /*dxfer_len*/sizeof(*ata_params),
3708 				  /*sense_len*/SSD_FULL_SIZE,
3709 				  /*timeout*/da_default_timeout * 1000);
3710 		start_ccb->ccb_h.ccb_bp = NULL;
3711 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA;
3712 		xpt_action(start_ccb);
3713 		break;
3714 	}
3715 	case DA_STATE_PROBE_ATA_LOGDIR:
3716 	{
3717 		struct ata_gp_log_dir *log_dir;
3718 		int retval;
3719 
3720 		retval = 0;
3721 
3722 		if ((softc->flags & DA_FLAG_CAN_ATA_LOG) == 0) {
3723 			/*
3724 			 * If we don't have log support, not much point in
3725 			 * trying to probe zone support.
3726 			 */
3727 			daprobedone(periph, start_ccb);
3728 			break;
3729 		}
3730 
3731 		/*
3732 		 * If we have an ATA device (the SCSI ATA Information VPD
3733 		 * page should be present and the ATA identify should have
3734 		 * succeeded) and it supports logs, ask for the log directory.
3735 		 */
3736 
3737 		log_dir = malloc(sizeof(*log_dir), M_SCSIDA, M_NOWAIT|M_ZERO);
3738 		if (log_dir == NULL) {
3739 			xpt_print(periph->path, "Couldn't malloc log_dir "
3740 			    "data\n");
3741 			daprobedone(periph, start_ccb);
3742 			break;
3743 		}
3744 
3745 		retval = scsi_ata_read_log(&start_ccb->csio,
3746 		    /*retries*/ da_retry_count,
3747 		    /*cbfcnp*/ dadone_probeatalogdir,
3748 		    /*tag_action*/ MSG_SIMPLE_Q_TAG,
3749 		    /*log_address*/ ATA_LOG_DIRECTORY,
3750 		    /*page_number*/ 0,
3751 		    /*block_count*/ 1,
3752 		    /*protocol*/ softc->flags & DA_FLAG_CAN_ATA_DMA ?
3753 				 AP_PROTO_DMA : AP_PROTO_PIO_IN,
3754 		    /*data_ptr*/ (uint8_t *)log_dir,
3755 		    /*dxfer_len*/ sizeof(*log_dir),
3756 		    /*sense_len*/ SSD_FULL_SIZE,
3757 		    /*timeout*/ da_default_timeout * 1000);
3758 
3759 		if (retval != 0) {
3760 			xpt_print(periph->path, "scsi_ata_read_log() failed!");
3761 			free(log_dir, M_SCSIDA);
3762 			daprobedone(periph, start_ccb);
3763 			break;
3764 		}
3765 		start_ccb->ccb_h.ccb_bp = NULL;
3766 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA_LOGDIR;
3767 		xpt_action(start_ccb);
3768 		break;
3769 	}
3770 	case DA_STATE_PROBE_ATA_IDDIR:
3771 	{
3772 		struct ata_identify_log_pages *id_dir;
3773 		int retval;
3774 
3775 		retval = 0;
3776 
3777 		/*
3778 		 * Check here to see whether the Identify Device log is
3779 		 * supported in the directory of logs.  If so, continue
3780 		 * with requesting the log of identify device pages.
3781 		 */
3782 		if ((softc->flags & DA_FLAG_CAN_ATA_IDLOG) == 0) {
3783 			daprobedone(periph, start_ccb);
3784 			break;
3785 		}
3786 
3787 		id_dir = malloc(sizeof(*id_dir), M_SCSIDA, M_NOWAIT | M_ZERO);
3788 		if (id_dir == NULL) {
3789 			xpt_print(periph->path, "Couldn't malloc id_dir "
3790 			    "data\n");
3791 			daprobedone(periph, start_ccb);
3792 			break;
3793 		}
3794 
3795 		retval = scsi_ata_read_log(&start_ccb->csio,
3796 		    /*retries*/ da_retry_count,
3797 		    /*cbfcnp*/ dadone_probeataiddir,
3798 		    /*tag_action*/ MSG_SIMPLE_Q_TAG,
3799 		    /*log_address*/ ATA_IDENTIFY_DATA_LOG,
3800 		    /*page_number*/ ATA_IDL_PAGE_LIST,
3801 		    /*block_count*/ 1,
3802 		    /*protocol*/ softc->flags & DA_FLAG_CAN_ATA_DMA ?
3803 				 AP_PROTO_DMA : AP_PROTO_PIO_IN,
3804 		    /*data_ptr*/ (uint8_t *)id_dir,
3805 		    /*dxfer_len*/ sizeof(*id_dir),
3806 		    /*sense_len*/ SSD_FULL_SIZE,
3807 		    /*timeout*/ da_default_timeout * 1000);
3808 
3809 		if (retval != 0) {
3810 			xpt_print(periph->path, "scsi_ata_read_log() failed!");
3811 			free(id_dir, M_SCSIDA);
3812 			daprobedone(periph, start_ccb);
3813 			break;
3814 		}
3815 		start_ccb->ccb_h.ccb_bp = NULL;
3816 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA_IDDIR;
3817 		xpt_action(start_ccb);
3818 		break;
3819 	}
3820 	case DA_STATE_PROBE_ATA_SUP:
3821 	{
3822 		struct ata_identify_log_sup_cap *sup_cap;
3823 		int retval;
3824 
3825 		retval = 0;
3826 
3827 		/*
3828 		 * Check here to see whether the Supported Capabilities log
3829 		 * is in the list of Identify Device logs.
3830 		 */
3831 		if ((softc->flags & DA_FLAG_CAN_ATA_SUPCAP) == 0) {
3832 			daprobedone(periph, start_ccb);
3833 			break;
3834 		}
3835 
3836 		sup_cap = malloc(sizeof(*sup_cap), M_SCSIDA, M_NOWAIT|M_ZERO);
3837 		if (sup_cap == NULL) {
3838 			xpt_print(periph->path, "Couldn't malloc sup_cap "
3839 			    "data\n");
3840 			daprobedone(periph, start_ccb);
3841 			break;
3842 		}
3843 
3844 		retval = scsi_ata_read_log(&start_ccb->csio,
3845 		    /*retries*/ da_retry_count,
3846 		    /*cbfcnp*/ dadone_probeatasup,
3847 		    /*tag_action*/ MSG_SIMPLE_Q_TAG,
3848 		    /*log_address*/ ATA_IDENTIFY_DATA_LOG,
3849 		    /*page_number*/ ATA_IDL_SUP_CAP,
3850 		    /*block_count*/ 1,
3851 		    /*protocol*/ softc->flags & DA_FLAG_CAN_ATA_DMA ?
3852 				 AP_PROTO_DMA : AP_PROTO_PIO_IN,
3853 		    /*data_ptr*/ (uint8_t *)sup_cap,
3854 		    /*dxfer_len*/ sizeof(*sup_cap),
3855 		    /*sense_len*/ SSD_FULL_SIZE,
3856 		    /*timeout*/ da_default_timeout * 1000);
3857 
3858 		if (retval != 0) {
3859 			xpt_print(periph->path, "scsi_ata_read_log() failed!");
3860 			free(sup_cap, M_SCSIDA);
3861 			daprobedone(periph, start_ccb);
3862 			break;
3863 		}
3864 
3865 		start_ccb->ccb_h.ccb_bp = NULL;
3866 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA_SUP;
3867 		xpt_action(start_ccb);
3868 		break;
3869 	}
3870 	case DA_STATE_PROBE_ATA_ZONE:
3871 	{
3872 		struct ata_zoned_info_log *ata_zone;
3873 		int retval;
3874 
3875 		retval = 0;
3876 
3877 		/*
3878 		 * Check here to see whether the zoned device information
3879 		 * page is supported.  If so, continue on to request it.
3880 		 * If not, skip to DA_STATE_PROBE_LOG or done.
3881 		 */
3882 		if ((softc->flags & DA_FLAG_CAN_ATA_ZONE) == 0) {
3883 			daprobedone(periph, start_ccb);
3884 			break;
3885 		}
3886 		ata_zone = malloc(sizeof(*ata_zone), M_SCSIDA,
3887 				  M_NOWAIT|M_ZERO);
3888 		if (ata_zone == NULL) {
3889 			xpt_print(periph->path, "Couldn't malloc ata_zone "
3890 			    "data\n");
3891 			daprobedone(periph, start_ccb);
3892 			break;
3893 		}
3894 
3895 		retval = scsi_ata_read_log(&start_ccb->csio,
3896 		    /*retries*/ da_retry_count,
3897 		    /*cbfcnp*/ dadone_probeatazone,
3898 		    /*tag_action*/ MSG_SIMPLE_Q_TAG,
3899 		    /*log_address*/ ATA_IDENTIFY_DATA_LOG,
3900 		    /*page_number*/ ATA_IDL_ZDI,
3901 		    /*block_count*/ 1,
3902 		    /*protocol*/ softc->flags & DA_FLAG_CAN_ATA_DMA ?
3903 				 AP_PROTO_DMA : AP_PROTO_PIO_IN,
3904 		    /*data_ptr*/ (uint8_t *)ata_zone,
3905 		    /*dxfer_len*/ sizeof(*ata_zone),
3906 		    /*sense_len*/ SSD_FULL_SIZE,
3907 		    /*timeout*/ da_default_timeout * 1000);
3908 
3909 		if (retval != 0) {
3910 			xpt_print(periph->path, "scsi_ata_read_log() failed!");
3911 			free(ata_zone, M_SCSIDA);
3912 			daprobedone(periph, start_ccb);
3913 			break;
3914 		}
3915 		start_ccb->ccb_h.ccb_bp = NULL;
3916 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA_ZONE;
3917 		xpt_action(start_ccb);
3918 
3919 		break;
3920 	}
3921 	case DA_STATE_PROBE_ZONE:
3922 	{
3923 		struct scsi_vpd_zoned_bdc *bdc;
3924 
3925 		/*
3926 		 * Note that this page will be supported for SCSI protocol
3927 		 * devices that support ZBC (SMR devices), as well as ATA
3928 		 * protocol devices that are behind a SAT (SCSI to ATA
3929 		 * Translation) layer that supports converting ZBC commands
3930 		 * to their ZAC equivalents.
3931 		 */
3932 		if (!scsi_vpd_supported_page(periph, SVPD_ZONED_BDC)) {
3933 			daprobedone(periph, start_ccb);
3934 			break;
3935 		}
3936 		bdc = (struct scsi_vpd_zoned_bdc *)
3937 			malloc(sizeof(*bdc), M_SCSIDA, M_NOWAIT|M_ZERO);
3938 
3939 		if (bdc == NULL) {
3940 			xpt_release_ccb(start_ccb);
3941 			xpt_print(periph->path, "Couldn't malloc zone VPD "
3942 			    "data\n");
3943 			break;
3944 		}
3945 		scsi_inquiry(&start_ccb->csio,
3946 			     /*retries*/da_retry_count,
3947 			     /*cbfcnp*/dadone_probezone,
3948 			     /*tag_action*/MSG_SIMPLE_Q_TAG,
3949 			     /*inq_buf*/(uint8_t *)bdc,
3950 			     /*inq_len*/sizeof(*bdc),
3951 			     /*evpd*/TRUE,
3952 			     /*page_code*/SVPD_ZONED_BDC,
3953 			     /*sense_len*/SSD_FULL_SIZE,
3954 			     /*timeout*/da_default_timeout * 1000);
3955 		start_ccb->ccb_h.ccb_bp = NULL;
3956 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ZONE;
3957 		xpt_action(start_ccb);
3958 		break;
3959 	}
3960 	}
3961 }
3962 
3963 /*
3964  * In each of the methods below, while its the caller's
3965  * responsibility to ensure the request will fit into a
3966  * single device request, we might have changed the delete
3967  * method due to the device incorrectly advertising either
3968  * its supported methods or limits.
3969  *
3970  * To prevent this causing further issues we validate the
3971  * against the methods limits, and warn which would
3972  * otherwise be unnecessary.
3973  */
3974 static void
3975 da_delete_unmap(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
3976 {
3977 	struct da_softc *softc = (struct da_softc *)periph->softc;
3978 	struct bio *bp1;
3979 	uint8_t *buf = softc->unmap_buf;
3980 	struct scsi_unmap_desc *d = (void *)&buf[UNMAP_HEAD_SIZE];
3981 	uint64_t lba, lastlba = (uint64_t)-1;
3982 	uint64_t totalcount = 0;
3983 	uint64_t count;
3984 	uint32_t c, lastcount = 0, ranges = 0;
3985 
3986 	/*
3987 	 * Currently this doesn't take the UNMAP
3988 	 * Granularity and Granularity Alignment
3989 	 * fields into account.
3990 	 *
3991 	 * This could result in both unoptimal unmap
3992 	 * requests as as well as UNMAP calls unmapping
3993 	 * fewer LBA's than requested.
3994 	 */
3995 
3996 	bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
3997 	bp1 = bp;
3998 	do {
3999 		/*
4000 		 * Note: ada and da are different in how they store the
4001 		 * pending bp's in a trim. ada stores all of them in the
4002 		 * trim_req.bps. da stores all but the first one in the
4003 		 * delete_run_queue. ada then completes all the bps in
4004 		 * its adadone() loop. da completes all the bps in the
4005 		 * delete_run_queue in dadone, and relies on the biodone
4006 		 * after to complete. This should be reconciled since there's
4007 		 * no real reason to do it differently. XXX
4008 		 */
4009 		if (bp1 != bp)
4010 			bioq_insert_tail(&softc->delete_run_queue, bp1);
4011 		lba = bp1->bio_pblkno;
4012 		count = bp1->bio_bcount / softc->params.secsize;
4013 
4014 		/* Try to extend the previous range. */
4015 		if (lba == lastlba) {
4016 			c = omin(count, UNMAP_RANGE_MAX - lastcount);
4017 			lastlba += c;
4018 			lastcount += c;
4019 			scsi_ulto4b(lastcount, d[ranges - 1].length);
4020 			count -= c;
4021 			lba += c;
4022 			totalcount += c;
4023 		} else if ((softc->quirks & DA_Q_STRICT_UNMAP) &&
4024 		    softc->unmap_gran != 0) {
4025 			/* Align length of the previous range. */
4026 			if ((c = lastcount % softc->unmap_gran) != 0) {
4027 				if (lastcount <= c) {
4028 					totalcount -= lastcount;
4029 					lastlba = (uint64_t)-1;
4030 					lastcount = 0;
4031 					ranges--;
4032 				} else {
4033 					totalcount -= c;
4034 					lastlba -= c;
4035 					lastcount -= c;
4036 					scsi_ulto4b(lastcount,
4037 					    d[ranges - 1].length);
4038 				}
4039 			}
4040 			/* Align beginning of the new range. */
4041 			c = (lba - softc->unmap_gran_align) % softc->unmap_gran;
4042 			if (c != 0) {
4043 				c = softc->unmap_gran - c;
4044 				if (count <= c) {
4045 					count = 0;
4046 				} else {
4047 					lba += c;
4048 					count -= c;
4049 				}
4050 			}
4051 		}
4052 
4053 		while (count > 0) {
4054 			c = omin(count, UNMAP_RANGE_MAX);
4055 			if (totalcount + c > softc->unmap_max_lba ||
4056 			    ranges >= softc->unmap_max_ranges) {
4057 				xpt_print(periph->path,
4058 				    "%s issuing short delete %ld > %ld"
4059 				    "|| %d >= %d",
4060 				    da_delete_method_desc[softc->delete_method],
4061 				    totalcount + c, softc->unmap_max_lba,
4062 				    ranges, softc->unmap_max_ranges);
4063 				break;
4064 			}
4065 			scsi_u64to8b(lba, d[ranges].lba);
4066 			scsi_ulto4b(c, d[ranges].length);
4067 			lba += c;
4068 			totalcount += c;
4069 			ranges++;
4070 			count -= c;
4071 			lastlba = lba;
4072 			lastcount = c;
4073 		}
4074 		bp1 = cam_iosched_next_trim(softc->cam_iosched);
4075 		if (bp1 == NULL)
4076 			break;
4077 		if (ranges >= softc->unmap_max_ranges ||
4078 		    totalcount + bp1->bio_bcount /
4079 		    softc->params.secsize > softc->unmap_max_lba) {
4080 			cam_iosched_put_back_trim(softc->cam_iosched, bp1);
4081 			break;
4082 		}
4083 	} while (1);
4084 
4085 	/* Align length of the last range. */
4086 	if ((softc->quirks & DA_Q_STRICT_UNMAP) && softc->unmap_gran != 0 &&
4087 	    (c = lastcount % softc->unmap_gran) != 0) {
4088 		if (lastcount <= c)
4089 			ranges--;
4090 		else
4091 			scsi_ulto4b(lastcount - c, d[ranges - 1].length);
4092 	}
4093 
4094 	scsi_ulto2b(ranges * 16 + 6, &buf[0]);
4095 	scsi_ulto2b(ranges * 16, &buf[2]);
4096 
4097 	scsi_unmap(&ccb->csio,
4098 		   /*retries*/da_retry_count,
4099 		   /*cbfcnp*/dadone,
4100 		   /*tag_action*/MSG_SIMPLE_Q_TAG,
4101 		   /*byte2*/0,
4102 		   /*data_ptr*/ buf,
4103 		   /*dxfer_len*/ ranges * 16 + 8,
4104 		   /*sense_len*/SSD_FULL_SIZE,
4105 		   da_default_timeout * 1000);
4106 	ccb->ccb_h.ccb_state = DA_CCB_DELETE;
4107 	ccb->ccb_h.flags |= CAM_UNLOCKED;
4108 	softc->trim_count++;
4109 	softc->trim_ranges += ranges;
4110 	softc->trim_lbas += totalcount;
4111 	cam_iosched_submit_trim(softc->cam_iosched);
4112 }
4113 
4114 static void
4115 da_delete_trim(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
4116 {
4117 	struct da_softc *softc = (struct da_softc *)periph->softc;
4118 	struct bio *bp1;
4119 	uint8_t *buf = softc->unmap_buf;
4120 	uint64_t lastlba = (uint64_t)-1;
4121 	uint64_t count;
4122 	uint64_t lba;
4123 	uint32_t lastcount = 0, c, requestcount;
4124 	int ranges = 0, off, block_count;
4125 
4126 	bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
4127 	bp1 = bp;
4128 	do {
4129 		if (bp1 != bp)//XXX imp XXX
4130 			bioq_insert_tail(&softc->delete_run_queue, bp1);
4131 		lba = bp1->bio_pblkno;
4132 		count = bp1->bio_bcount / softc->params.secsize;
4133 		requestcount = count;
4134 
4135 		/* Try to extend the previous range. */
4136 		if (lba == lastlba) {
4137 			c = omin(count, ATA_DSM_RANGE_MAX - lastcount);
4138 			lastcount += c;
4139 			off = (ranges - 1) * 8;
4140 			buf[off + 6] = lastcount & 0xff;
4141 			buf[off + 7] = (lastcount >> 8) & 0xff;
4142 			count -= c;
4143 			lba += c;
4144 		}
4145 
4146 		while (count > 0) {
4147 			c = omin(count, ATA_DSM_RANGE_MAX);
4148 			off = ranges * 8;
4149 
4150 			buf[off + 0] = lba & 0xff;
4151 			buf[off + 1] = (lba >> 8) & 0xff;
4152 			buf[off + 2] = (lba >> 16) & 0xff;
4153 			buf[off + 3] = (lba >> 24) & 0xff;
4154 			buf[off + 4] = (lba >> 32) & 0xff;
4155 			buf[off + 5] = (lba >> 40) & 0xff;
4156 			buf[off + 6] = c & 0xff;
4157 			buf[off + 7] = (c >> 8) & 0xff;
4158 			lba += c;
4159 			ranges++;
4160 			count -= c;
4161 			lastcount = c;
4162 			if (count != 0 && ranges == softc->trim_max_ranges) {
4163 				xpt_print(periph->path,
4164 				    "%s issuing short delete %ld > %ld\n",
4165 				    da_delete_method_desc[softc->delete_method],
4166 				    requestcount,
4167 				    (softc->trim_max_ranges - ranges) *
4168 				    ATA_DSM_RANGE_MAX);
4169 				break;
4170 			}
4171 		}
4172 		lastlba = lba;
4173 		bp1 = cam_iosched_next_trim(softc->cam_iosched);
4174 		if (bp1 == NULL)
4175 			break;
4176 		if (bp1->bio_bcount / softc->params.secsize >
4177 		    (softc->trim_max_ranges - ranges) * ATA_DSM_RANGE_MAX) {
4178 			cam_iosched_put_back_trim(softc->cam_iosched, bp1);
4179 			break;
4180 		}
4181 	} while (1);
4182 
4183 	block_count = howmany(ranges, ATA_DSM_BLK_RANGES);
4184 	scsi_ata_trim(&ccb->csio,
4185 		      /*retries*/da_retry_count,
4186 		      /*cbfcnp*/dadone,
4187 		      /*tag_action*/MSG_SIMPLE_Q_TAG,
4188 		      block_count,
4189 		      /*data_ptr*/buf,
4190 		      /*dxfer_len*/block_count * ATA_DSM_BLK_SIZE,
4191 		      /*sense_len*/SSD_FULL_SIZE,
4192 		      da_default_timeout * 1000);
4193 	ccb->ccb_h.ccb_state = DA_CCB_DELETE;
4194 	ccb->ccb_h.flags |= CAM_UNLOCKED;
4195 	cam_iosched_submit_trim(softc->cam_iosched);
4196 }
4197 
4198 /*
4199  * We calculate ws_max_blks here based off d_delmaxsize instead
4200  * of using softc->ws_max_blks as it is absolute max for the
4201  * device not the protocol max which may well be lower.
4202  */
4203 static void
4204 da_delete_ws(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
4205 {
4206 	struct da_softc *softc;
4207 	struct bio *bp1;
4208 	uint64_t ws_max_blks;
4209 	uint64_t lba;
4210 	uint64_t count; /* forward compat with WS32 */
4211 
4212 	softc = (struct da_softc *)periph->softc;
4213 	ws_max_blks = softc->disk->d_delmaxsize / softc->params.secsize;
4214 	lba = bp->bio_pblkno;
4215 	count = 0;
4216 	bp1 = bp;
4217 	do {
4218 		if (bp1 != bp)//XXX imp XXX
4219 			bioq_insert_tail(&softc->delete_run_queue, bp1);
4220 		count += bp1->bio_bcount / softc->params.secsize;
4221 		if (count > ws_max_blks) {
4222 			xpt_print(periph->path,
4223 			    "%s issuing short delete %ld > %ld\n",
4224 			    da_delete_method_desc[softc->delete_method],
4225 			    count, ws_max_blks);
4226 			count = omin(count, ws_max_blks);
4227 			break;
4228 		}
4229 		bp1 = cam_iosched_next_trim(softc->cam_iosched);
4230 		if (bp1 == NULL)
4231 			break;
4232 		if (lba + count != bp1->bio_pblkno ||
4233 		    count + bp1->bio_bcount /
4234 		    softc->params.secsize > ws_max_blks) {
4235 			cam_iosched_put_back_trim(softc->cam_iosched, bp1);
4236 			break;
4237 		}
4238 	} while (1);
4239 
4240 	scsi_write_same(&ccb->csio,
4241 			/*retries*/da_retry_count,
4242 			/*cbfcnp*/dadone,
4243 			/*tag_action*/MSG_SIMPLE_Q_TAG,
4244 			/*byte2*/softc->delete_method ==
4245 			    DA_DELETE_ZERO ? 0 : SWS_UNMAP,
4246 			softc->delete_method == DA_DELETE_WS16 ? 16 : 10,
4247 			/*lba*/lba,
4248 			/*block_count*/count,
4249 			/*data_ptr*/ __DECONST(void *, zero_region),
4250 			/*dxfer_len*/ softc->params.secsize,
4251 			/*sense_len*/SSD_FULL_SIZE,
4252 			da_default_timeout * 1000);
4253 	ccb->ccb_h.ccb_state = DA_CCB_DELETE;
4254 	ccb->ccb_h.flags |= CAM_UNLOCKED;
4255 	cam_iosched_submit_trim(softc->cam_iosched);
4256 }
4257 
4258 static int
4259 cmd6workaround(union ccb *ccb)
4260 {
4261 	struct scsi_rw_6 cmd6;
4262 	struct scsi_rw_10 *cmd10;
4263 	struct da_softc *softc;
4264 	uint8_t *cdb;
4265 	struct bio *bp;
4266 	int frozen;
4267 
4268 	cdb = ccb->csio.cdb_io.cdb_bytes;
4269 	softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
4270 
4271 	if (ccb->ccb_h.ccb_state == DA_CCB_DELETE) {
4272 		da_delete_methods old_method = softc->delete_method;
4273 
4274 		/*
4275 		 * Typically there are two reasons for failure here
4276 		 * 1. Delete method was detected as supported but isn't
4277 		 * 2. Delete failed due to invalid params e.g. too big
4278 		 *
4279 		 * While we will attempt to choose an alternative delete method
4280 		 * this may result in short deletes if the existing delete
4281 		 * requests from geom are big for the new method chosen.
4282 		 *
4283 		 * This method assumes that the error which triggered this
4284 		 * will not retry the io otherwise a panic will occur
4285 		 */
4286 		dadeleteflag(softc, old_method, 0);
4287 		dadeletemethodchoose(softc, DA_DELETE_DISABLE);
4288 		if (softc->delete_method == DA_DELETE_DISABLE)
4289 			xpt_print(ccb->ccb_h.path,
4290 				  "%s failed, disabling BIO_DELETE\n",
4291 				  da_delete_method_desc[old_method]);
4292 		else
4293 			xpt_print(ccb->ccb_h.path,
4294 				  "%s failed, switching to %s BIO_DELETE\n",
4295 				  da_delete_method_desc[old_method],
4296 				  da_delete_method_desc[softc->delete_method]);
4297 
4298 		while ((bp = bioq_takefirst(&softc->delete_run_queue)) != NULL)
4299 			cam_iosched_queue_work(softc->cam_iosched, bp);
4300 		cam_iosched_queue_work(softc->cam_iosched,
4301 		    (struct bio *)ccb->ccb_h.ccb_bp);
4302 		ccb->ccb_h.ccb_bp = NULL;
4303 		return (0);
4304 	}
4305 
4306 	/* Detect unsupported PREVENT ALLOW MEDIUM REMOVAL. */
4307 	if ((ccb->ccb_h.flags & CAM_CDB_POINTER) == 0 &&
4308 	    (*cdb == PREVENT_ALLOW) &&
4309 	    (softc->quirks & DA_Q_NO_PREVENT) == 0) {
4310 		if (bootverbose)
4311 			xpt_print(ccb->ccb_h.path,
4312 			    "PREVENT ALLOW MEDIUM REMOVAL not supported.\n");
4313 		softc->quirks |= DA_Q_NO_PREVENT;
4314 		return (0);
4315 	}
4316 
4317 	/* Detect unsupported SYNCHRONIZE CACHE(10). */
4318 	if ((ccb->ccb_h.flags & CAM_CDB_POINTER) == 0 &&
4319 	    (*cdb == SYNCHRONIZE_CACHE) &&
4320 	    (softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
4321 		if (bootverbose)
4322 			xpt_print(ccb->ccb_h.path,
4323 			    "SYNCHRONIZE CACHE(10) not supported.\n");
4324 		softc->quirks |= DA_Q_NO_SYNC_CACHE;
4325 		softc->disk->d_flags &= ~DISKFLAG_CANFLUSHCACHE;
4326 		return (0);
4327 	}
4328 
4329 	/* Translation only possible if CDB is an array and cmd is R/W6 */
4330 	if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
4331 	    (*cdb != READ_6 && *cdb != WRITE_6))
4332 		return 0;
4333 
4334 	xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
4335 	    "increasing minimum_cmd_size to 10.\n");
4336 	softc->minimum_cmd_size = 10;
4337 
4338 	bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
4339 	cmd10 = (struct scsi_rw_10 *)cdb;
4340 	cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
4341 	cmd10->byte2 = 0;
4342 	scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
4343 	cmd10->reserved = 0;
4344 	scsi_ulto2b(cmd6.length, cmd10->length);
4345 	cmd10->control = cmd6.control;
4346 	ccb->csio.cdb_len = sizeof(*cmd10);
4347 
4348 	/* Requeue request, unfreezing queue if necessary */
4349 	frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
4350 	ccb->ccb_h.status = CAM_REQUEUE_REQ;
4351 	xpt_action(ccb);
4352 	if (frozen) {
4353 		cam_release_devq(ccb->ccb_h.path,
4354 				 /*relsim_flags*/0,
4355 				 /*reduction*/0,
4356 				 /*timeout*/0,
4357 				 /*getcount_only*/0);
4358 	}
4359 	return (ERESTART);
4360 }
4361 
4362 static void
4363 dazonedone(struct cam_periph *periph, union ccb *ccb)
4364 {
4365 	struct da_softc *softc;
4366 	struct bio *bp;
4367 
4368 	softc = periph->softc;
4369 	bp = (struct bio *)ccb->ccb_h.ccb_bp;
4370 
4371 	switch (bp->bio_zone.zone_cmd) {
4372 	case DISK_ZONE_OPEN:
4373 	case DISK_ZONE_CLOSE:
4374 	case DISK_ZONE_FINISH:
4375 	case DISK_ZONE_RWP:
4376 		break;
4377 	case DISK_ZONE_REPORT_ZONES: {
4378 		uint32_t avail_len;
4379 		struct disk_zone_report *rep;
4380 		struct scsi_report_zones_hdr *hdr;
4381 		struct scsi_report_zones_desc *desc;
4382 		struct disk_zone_rep_entry *entry;
4383 		uint32_t hdr_len, num_avail;
4384 		uint32_t num_to_fill, i;
4385 		int ata;
4386 
4387 		rep = &bp->bio_zone.zone_params.report;
4388 		avail_len = ccb->csio.dxfer_len - ccb->csio.resid;
4389 		/*
4390 		 * Note that bio_resid isn't normally used for zone
4391 		 * commands, but it is used by devstat_end_transaction_bio()
4392 		 * to determine how much data was transferred.  Because
4393 		 * the size of the SCSI/ATA data structures is different
4394 		 * than the size of the BIO interface structures, the
4395 		 * amount of data actually transferred from the drive will
4396 		 * be different than the amount of data transferred to
4397 		 * the user.
4398 		 */
4399 		bp->bio_resid = ccb->csio.resid;
4400 		hdr = (struct scsi_report_zones_hdr *)ccb->csio.data_ptr;
4401 		if (avail_len < sizeof(*hdr)) {
4402 			/*
4403 			 * Is there a better error than EIO here?  We asked
4404 			 * for at least the header, and we got less than
4405 			 * that.
4406 			 */
4407 			bp->bio_error = EIO;
4408 			bp->bio_flags |= BIO_ERROR;
4409 			bp->bio_resid = bp->bio_bcount;
4410 			break;
4411 		}
4412 
4413 		if (softc->zone_interface == DA_ZONE_IF_ATA_PASS)
4414 			ata = 1;
4415 		else
4416 			ata = 0;
4417 
4418 		hdr_len = ata ? le32dec(hdr->length) :
4419 				scsi_4btoul(hdr->length);
4420 		if (hdr_len > 0)
4421 			rep->entries_available = hdr_len / sizeof(*desc);
4422 		else
4423 			rep->entries_available = 0;
4424 		/*
4425 		 * NOTE: using the same values for the BIO version of the
4426 		 * same field as the SCSI/ATA values.  This means we could
4427 		 * get some additional values that aren't defined in bio.h
4428 		 * if more values of the same field are defined later.
4429 		 */
4430 		rep->header.same = hdr->byte4 & SRZ_SAME_MASK;
4431 		rep->header.maximum_lba = ata ?  le64dec(hdr->maximum_lba) :
4432 					  scsi_8btou64(hdr->maximum_lba);
4433 		/*
4434 		 * If the drive reports no entries that match the query,
4435 		 * we're done.
4436 		 */
4437 		if (hdr_len == 0) {
4438 			rep->entries_filled = 0;
4439 			break;
4440 		}
4441 
4442 		num_avail = min((avail_len - sizeof(*hdr)) / sizeof(*desc),
4443 				hdr_len / sizeof(*desc));
4444 		/*
4445 		 * If the drive didn't return any data, then we're done.
4446 		 */
4447 		if (num_avail == 0) {
4448 			rep->entries_filled = 0;
4449 			break;
4450 		}
4451 
4452 		num_to_fill = min(num_avail, rep->entries_allocated);
4453 		/*
4454 		 * If the user didn't allocate any entries for us to fill,
4455 		 * we're done.
4456 		 */
4457 		if (num_to_fill == 0) {
4458 			rep->entries_filled = 0;
4459 			break;
4460 		}
4461 
4462 		for (i = 0, desc = &hdr->desc_list[0], entry=&rep->entries[0];
4463 		     i < num_to_fill; i++, desc++, entry++) {
4464 			/*
4465 			 * NOTE: we're mapping the values here directly
4466 			 * from the SCSI/ATA bit definitions to the bio.h
4467 			 * definitions. There is also a warning in
4468 			 * disk_zone.h, but the impact is that if
4469 			 * additional values are added in the SCSI/ATA
4470 			 * specs these will be visible to consumers of
4471 			 * this interface.
4472 			 */
4473 			entry->zone_type = desc->zone_type & SRZ_TYPE_MASK;
4474 			entry->zone_condition =
4475 			    (desc->zone_flags & SRZ_ZONE_COND_MASK) >>
4476 			    SRZ_ZONE_COND_SHIFT;
4477 			entry->zone_flags |= desc->zone_flags &
4478 			    (SRZ_ZONE_NON_SEQ|SRZ_ZONE_RESET);
4479 			entry->zone_length =
4480 			    ata ? le64dec(desc->zone_length) :
4481 				  scsi_8btou64(desc->zone_length);
4482 			entry->zone_start_lba =
4483 			    ata ? le64dec(desc->zone_start_lba) :
4484 				  scsi_8btou64(desc->zone_start_lba);
4485 			entry->write_pointer_lba =
4486 			    ata ? le64dec(desc->write_pointer_lba) :
4487 				  scsi_8btou64(desc->write_pointer_lba);
4488 		}
4489 		rep->entries_filled = num_to_fill;
4490 		break;
4491 	}
4492 	case DISK_ZONE_GET_PARAMS:
4493 	default:
4494 		/*
4495 		 * In theory we should not get a GET_PARAMS bio, since it
4496 		 * should be handled without queueing the command to the
4497 		 * drive.
4498 		 */
4499 		panic("%s: Invalid zone command %d", __func__,
4500 		    bp->bio_zone.zone_cmd);
4501 		break;
4502 	}
4503 
4504 	if (bp->bio_zone.zone_cmd == DISK_ZONE_REPORT_ZONES)
4505 		free(ccb->csio.data_ptr, M_SCSIDA);
4506 }
4507 
4508 static void
4509 dadone(struct cam_periph *periph, union ccb *done_ccb)
4510 {
4511 	struct bio *bp, *bp1;
4512 	struct da_softc *softc;
4513 	struct ccb_scsiio *csio;
4514 	da_ccb_state state;
4515 
4516 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone\n"));
4517 
4518 	softc = (struct da_softc *)periph->softc;
4519 	csio = &done_ccb->csio;
4520 
4521 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
4522 	if (csio->bio != NULL)
4523 		biotrack(csio->bio, __func__);
4524 #endif
4525 	state = csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK;
4526 
4527 	cam_periph_lock(periph);
4528 	bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
4529 	if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
4530 		int error;
4531 		int sf;
4532 
4533 		if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
4534 			sf = SF_RETRY_UA;
4535 		else
4536 			sf = 0;
4537 
4538 		error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
4539 		if (error == ERESTART) {
4540 			/* A retry was scheduled, so just return. */
4541 			cam_periph_unlock(periph);
4542 			return;
4543 		}
4544 		bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
4545 		if (error != 0) {
4546 			int queued_error;
4547 
4548 			/*
4549 			 * return all queued I/O with EIO, so that
4550 			 * the client can retry these I/Os in the
4551 			 * proper order should it attempt to recover.
4552 			 */
4553 			queued_error = EIO;
4554 
4555 			if (error == ENXIO
4556 			 && (softc->flags & DA_FLAG_PACK_INVALID)== 0) {
4557 				/*
4558 				 * Catastrophic error.  Mark our pack as
4559 				 * invalid.
4560 				 *
4561 				 * XXX See if this is really a media
4562 				 * XXX change first?
4563 				 */
4564 				xpt_print(periph->path, "Invalidating pack\n");
4565 				softc->flags |= DA_FLAG_PACK_INVALID;
4566 #ifdef CAM_IO_STATS
4567 				softc->invalidations++;
4568 #endif
4569 				queued_error = ENXIO;
4570 			}
4571 			cam_iosched_flush(softc->cam_iosched, NULL,
4572 			   queued_error);
4573 			if (bp != NULL) {
4574 				bp->bio_error = error;
4575 				bp->bio_resid = bp->bio_bcount;
4576 				bp->bio_flags |= BIO_ERROR;
4577 			}
4578 		} else if (bp != NULL) {
4579 			if (state == DA_CCB_DELETE)
4580 				bp->bio_resid = 0;
4581 			else
4582 				bp->bio_resid = csio->resid;
4583 			bp->bio_error = 0;
4584 			if (bp->bio_resid != 0)
4585 				bp->bio_flags |= BIO_ERROR;
4586 		}
4587 		if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
4588 			cam_release_devq(done_ccb->ccb_h.path,
4589 					 /*relsim_flags*/0,
4590 					 /*reduction*/0,
4591 					 /*timeout*/0,
4592 					 /*getcount_only*/0);
4593 	} else if (bp != NULL) {
4594 		if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
4595 			panic("REQ_CMP with QFRZN");
4596 		if (bp->bio_cmd == BIO_ZONE)
4597 			dazonedone(periph, done_ccb);
4598 		else if (state == DA_CCB_DELETE)
4599 			bp->bio_resid = 0;
4600 		else
4601 			bp->bio_resid = csio->resid;
4602 		if ((csio->resid > 0) && (bp->bio_cmd != BIO_ZONE))
4603 			bp->bio_flags |= BIO_ERROR;
4604 		if (softc->error_inject != 0) {
4605 			bp->bio_error = softc->error_inject;
4606 			bp->bio_resid = bp->bio_bcount;
4607 			bp->bio_flags |= BIO_ERROR;
4608 			softc->error_inject = 0;
4609 		}
4610 	}
4611 
4612 	if (bp != NULL)
4613 		biotrack(bp, __func__);
4614 	LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
4615 	if (LIST_EMPTY(&softc->pending_ccbs))
4616 		softc->flags |= DA_FLAG_WAS_OTAG;
4617 
4618 	/*
4619 	 * We need to call cam_iosched before we call biodone so that we don't
4620 	 * measure any activity that happens in the completion routine, which in
4621 	 * the case of sendfile can be quite extensive. Release the periph
4622 	 * refcount taken in dastart() for each CCB.
4623 	 */
4624 	cam_iosched_bio_complete(softc->cam_iosched, bp, done_ccb);
4625 	xpt_release_ccb(done_ccb);
4626 	KASSERT(softc->refcount >= 1, ("dadone softc %p refcount %d", softc, softc->refcount));
4627 	softc->refcount--;
4628 	if (state == DA_CCB_DELETE) {
4629 		TAILQ_HEAD(, bio) queue;
4630 
4631 		TAILQ_INIT(&queue);
4632 		TAILQ_CONCAT(&queue, &softc->delete_run_queue.queue, bio_queue);
4633 		softc->delete_run_queue.insert_point = NULL;
4634 		/*
4635 		 * Normally, the xpt_release_ccb() above would make sure
4636 		 * that when we have more work to do, that work would
4637 		 * get kicked off. However, we specifically keep
4638 		 * delete_running set to 0 before the call above to
4639 		 * allow other I/O to progress when many BIO_DELETE
4640 		 * requests are pushed down. We set delete_running to 0
4641 		 * and call daschedule again so that we don't stall if
4642 		 * there are no other I/Os pending apart from BIO_DELETEs.
4643 		 */
4644 		cam_iosched_trim_done(softc->cam_iosched);
4645 		daschedule(periph);
4646 		cam_periph_unlock(periph);
4647 		while ((bp1 = TAILQ_FIRST(&queue)) != NULL) {
4648 			TAILQ_REMOVE(&queue, bp1, bio_queue);
4649 			bp1->bio_error = bp->bio_error;
4650 			if (bp->bio_flags & BIO_ERROR) {
4651 				bp1->bio_flags |= BIO_ERROR;
4652 				bp1->bio_resid = bp1->bio_bcount;
4653 			} else
4654 				bp1->bio_resid = 0;
4655 			biodone(bp1);
4656 		}
4657 	} else {
4658 		daschedule(periph);
4659 		cam_periph_unlock(periph);
4660 	}
4661 	if (bp != NULL)
4662 		biodone(bp);
4663 	return;
4664 }
4665 
4666 static void
4667 dadone_probewp(struct cam_periph *periph, union ccb *done_ccb)
4668 {
4669 	struct da_softc *softc;
4670 	struct ccb_scsiio *csio;
4671 	uint32_t  priority;
4672 
4673 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone_probewp\n"));
4674 
4675 	softc = (struct da_softc *)periph->softc;
4676 	priority = done_ccb->ccb_h.pinfo.priority;
4677 	csio = &done_ccb->csio;
4678 
4679 	cam_periph_assert(periph, MA_OWNED);
4680 
4681 	KASSERT(softc->state == DA_STATE_PROBE_WP,
4682 	    ("State (%d) not PROBE_WP in dadone_probewp, periph %p ccb %p",
4683 		softc->state, periph, done_ccb));
4684         KASSERT((csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) == DA_CCB_PROBE_WP,
4685 	    ("CCB State (%lu) not PROBE_WP in dadone_probewp, periph %p ccb %p",
4686 		(unsigned long)csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK, periph,
4687 		done_ccb));
4688 
4689 	if (cam_ccb_status(done_ccb) == CAM_REQ_CMP) {
4690 		int len, off;
4691 		uint8_t dev_spec;
4692 
4693 		if (csio->cdb_len > 6) {
4694 			struct scsi_mode_header_10 *mh =
4695 			    (struct scsi_mode_header_10 *)csio->data_ptr;
4696 			len = 2 + scsi_2btoul(mh->data_length);
4697 			off = sizeof(*mh) + scsi_2btoul(mh->blk_desc_len);
4698 			dev_spec = mh->dev_spec;
4699 		} else {
4700 			struct scsi_mode_header_6 *mh =
4701 			    (struct scsi_mode_header_6 *)csio->data_ptr;
4702 			len = 1 + mh->data_length;
4703 			off = sizeof(*mh) + mh->blk_desc_len;
4704 			dev_spec = mh->dev_spec;
4705 		}
4706 		if ((dev_spec & 0x80) != 0)
4707 			softc->disk->d_flags |= DISKFLAG_WRITE_PROTECT;
4708 		else
4709 			softc->disk->d_flags &= ~DISKFLAG_WRITE_PROTECT;
4710 
4711 		/* Next time request only the first of returned mode pages. */
4712 		if (off < len && off < csio->dxfer_len - csio->resid)
4713 			softc->mode_page = csio->data_ptr[off] & SMPH_PC_MASK;
4714 	} else {
4715 		int error;
4716 
4717 		error = daerror(done_ccb, CAM_RETRY_SELTO,
4718 				SF_RETRY_UA|SF_NO_PRINT);
4719 		if (error == ERESTART)
4720 			return;
4721 		else if (error != 0) {
4722 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
4723 				/* Don't wedge this device's queue */
4724 				cam_release_devq(done_ccb->ccb_h.path,
4725 						 /*relsim_flags*/0,
4726 						 /*reduction*/0,
4727 						 /*timeout*/0,
4728 						 /*getcount_only*/0);
4729 			}
4730 
4731 			/* We don't depend on it, so don't try again. */
4732 			softc->mode_page = -1;
4733 		}
4734 	}
4735 
4736 	free(csio->data_ptr, M_SCSIDA);
4737 	if ((softc->flags & DA_FLAG_CAN_RC16) != 0)
4738 		softc->state = DA_STATE_PROBE_RC16;
4739 	else
4740 		softc->state = DA_STATE_PROBE_RC;
4741 	xpt_release_ccb(done_ccb);
4742 	xpt_schedule(periph, priority);
4743 	return;
4744 }
4745 
4746 static void
4747 dadone_proberc(struct cam_periph *periph, union ccb *done_ccb)
4748 {
4749 	struct scsi_read_capacity_data *rdcap;
4750 	struct scsi_read_capacity_data_long *rcaplong;
4751 	struct da_softc *softc;
4752 	struct ccb_scsiio *csio;
4753 	da_ccb_state state;
4754 	char *announce_buf;
4755 	uint32_t  priority;
4756 	int lbp, n;
4757 
4758 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone_proberc\n"));
4759 
4760 	softc = (struct da_softc *)periph->softc;
4761 	priority = done_ccb->ccb_h.pinfo.priority;
4762 	csio = &done_ccb->csio;
4763 	state = csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK;
4764 
4765 	KASSERT(softc->state == DA_STATE_PROBE_RC || softc->state == DA_STATE_PROBE_RC16,
4766 	    ("State (%d) not PROBE_RC* in dadone_proberc, periph %p ccb %p",
4767 		softc->state, periph, done_ccb));
4768 	KASSERT(state == DA_CCB_PROBE_RC || state == DA_CCB_PROBE_RC16,
4769 	    ("CCB State (%lu) not PROBE_RC* in dadone_probewp, periph %p ccb %p",
4770 		(unsigned long)state, periph, done_ccb));
4771 
4772 	lbp = 0;
4773 	rdcap = NULL;
4774 	rcaplong = NULL;
4775 	/* XXX TODO: can this be a malloc? */
4776 	announce_buf = softc->announce_temp;
4777 	bzero(announce_buf, DA_ANNOUNCETMP_SZ);
4778 
4779 	if (state == DA_CCB_PROBE_RC)
4780 		rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
4781 	else
4782 		rcaplong = (struct scsi_read_capacity_data_long *)
4783 			csio->data_ptr;
4784 
4785 	cam_periph_assert(periph, MA_OWNED);
4786 
4787 	if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
4788 		struct disk_params *dp;
4789 		uint32_t block_size;
4790 		uint64_t maxsector;
4791 		u_int lalba;	/* Lowest aligned LBA. */
4792 
4793 		if (state == DA_CCB_PROBE_RC) {
4794 			block_size = scsi_4btoul(rdcap->length);
4795 			maxsector = scsi_4btoul(rdcap->addr);
4796 			lalba = 0;
4797 
4798 			/*
4799 			 * According to SBC-2, if the standard 10
4800 			 * byte READ CAPACITY command returns 2^32,
4801 			 * we should issue the 16 byte version of
4802 			 * the command, since the device in question
4803 			 * has more sectors than can be represented
4804 			 * with the short version of the command.
4805 			 */
4806 			if (maxsector == 0xffffffff) {
4807 				free(rdcap, M_SCSIDA);
4808 				softc->state = DA_STATE_PROBE_RC16;
4809 				xpt_release_ccb(done_ccb);
4810 				xpt_schedule(periph, priority);
4811 				return;
4812 			}
4813 		} else {
4814 			block_size = scsi_4btoul(rcaplong->length);
4815 			maxsector = scsi_8btou64(rcaplong->addr);
4816 			lalba = scsi_2btoul(rcaplong->lalba_lbp);
4817 		}
4818 
4819 		/*
4820 		 * Because GEOM code just will panic us if we
4821 		 * give them an 'illegal' value we'll avoid that
4822 		 * here.
4823 		 */
4824 		if (block_size == 0) {
4825 			block_size = 512;
4826 			if (maxsector == 0)
4827 				maxsector = -1;
4828 		}
4829 		if (block_size >= maxphys) {
4830 			xpt_print(periph->path,
4831 			    "unsupportable block size %ju\n",
4832 			    (uintmax_t) block_size);
4833 			announce_buf = NULL;
4834 			cam_periph_invalidate(periph);
4835 		} else {
4836 			/*
4837 			 * We pass rcaplong into dasetgeom(),
4838 			 * because it will only use it if it is
4839 			 * non-NULL.
4840 			 */
4841 			dasetgeom(periph, block_size, maxsector,
4842 				  rcaplong, sizeof(*rcaplong));
4843 			lbp = (lalba & SRC16_LBPME_A);
4844 			dp = &softc->params;
4845 			n = snprintf(announce_buf, DA_ANNOUNCETMP_SZ,
4846 			    "%juMB (%ju %u byte sectors",
4847 			    ((uintmax_t)dp->secsize * dp->sectors) /
4848 			     (1024 * 1024),
4849 			    (uintmax_t)dp->sectors, dp->secsize);
4850 			if (softc->p_type != 0) {
4851 				n += snprintf(announce_buf + n,
4852 				    DA_ANNOUNCETMP_SZ - n,
4853 				    ", DIF type %d", softc->p_type);
4854 			}
4855 			snprintf(announce_buf + n, DA_ANNOUNCETMP_SZ - n, ")");
4856 		}
4857 	} else {
4858 		int error;
4859 
4860 		/*
4861 		 * Retry any UNIT ATTENTION type errors.  They
4862 		 * are expected at boot.
4863 		 */
4864 		error = daerror(done_ccb, CAM_RETRY_SELTO,
4865 				SF_RETRY_UA|SF_NO_PRINT);
4866 		if (error == ERESTART) {
4867 			/*
4868 			 * A retry was scheuled, so
4869 			 * just return.
4870 			 */
4871 			return;
4872 		} else if (error != 0) {
4873 			int asc, ascq;
4874 			int sense_key, error_code;
4875 			int have_sense;
4876 			cam_status status;
4877 			struct ccb_getdev cgd;
4878 
4879 			/* Don't wedge this device's queue */
4880 			status = done_ccb->ccb_h.status;
4881 			if ((status & CAM_DEV_QFRZN) != 0)
4882 				cam_release_devq(done_ccb->ccb_h.path,
4883 						 /*relsim_flags*/0,
4884 						 /*reduction*/0,
4885 						 /*timeout*/0,
4886 						 /*getcount_only*/0);
4887 
4888 			memset(&cgd, 0, sizeof(cgd));
4889 			xpt_setup_ccb(&cgd.ccb_h, done_ccb->ccb_h.path,
4890 				      CAM_PRIORITY_NORMAL);
4891 			cgd.ccb_h.func_code = XPT_GDEV_TYPE;
4892 			xpt_action((union ccb *)&cgd);
4893 
4894 			if (scsi_extract_sense_ccb(done_ccb,
4895 			    &error_code, &sense_key, &asc, &ascq))
4896 				have_sense = TRUE;
4897 			else
4898 				have_sense = FALSE;
4899 
4900 			/*
4901 			 * If we tried READ CAPACITY(16) and failed,
4902 			 * fallback to READ CAPACITY(10).
4903 			 */
4904 			if ((state == DA_CCB_PROBE_RC16) &&
4905 			    (softc->flags & DA_FLAG_CAN_RC16) &&
4906 			    (((csio->ccb_h.status & CAM_STATUS_MASK) ==
4907 				CAM_REQ_INVALID) ||
4908 			     ((have_sense) &&
4909 			      (error_code == SSD_CURRENT_ERROR ||
4910 			       error_code == SSD_DESC_CURRENT_ERROR) &&
4911 			      (sense_key == SSD_KEY_ILLEGAL_REQUEST)))) {
4912 				cam_periph_assert(periph, MA_OWNED);
4913 				softc->flags &= ~DA_FLAG_CAN_RC16;
4914 				free(rdcap, M_SCSIDA);
4915 				softc->state = DA_STATE_PROBE_RC;
4916 				xpt_release_ccb(done_ccb);
4917 				xpt_schedule(periph, priority);
4918 				return;
4919 			}
4920 
4921 			/*
4922 			 * Attach to anything that claims to be a
4923 			 * direct access or optical disk device,
4924 			 * as long as it doesn't return a "Logical
4925 			 * unit not supported" (0x25) error.
4926 			 * "Internal Target Failure" (0x44) is also
4927 			 * special and typically means that the
4928 			 * device is a SATA drive behind a SATL
4929 			 * translation that's fallen into a
4930 			 * terminally fatal state.
4931 			 */
4932 			if ((have_sense)
4933 			 && (asc != 0x25) && (asc != 0x44)
4934 			 && (error_code == SSD_CURRENT_ERROR
4935 			  || error_code == SSD_DESC_CURRENT_ERROR)) {
4936 				const char *sense_key_desc;
4937 				const char *asc_desc;
4938 
4939 				dasetgeom(periph, 512, -1, NULL, 0);
4940 				scsi_sense_desc(sense_key, asc, ascq,
4941 						&cgd.inq_data, &sense_key_desc,
4942 						&asc_desc);
4943 				snprintf(announce_buf, DA_ANNOUNCETMP_SZ,
4944 				    "Attempt to query device "
4945 				    "size failed: %s, %s",
4946 				    sense_key_desc, asc_desc);
4947 			} else {
4948 				if (have_sense)
4949 					scsi_sense_print(&done_ccb->csio);
4950 				else {
4951 					xpt_print(periph->path,
4952 					    "got CAM status %#x\n",
4953 					    done_ccb->ccb_h.status);
4954 				}
4955 
4956 				xpt_print(periph->path, "fatal error, "
4957 				    "failed to attach to device\n");
4958 
4959 				announce_buf = NULL;
4960 
4961 				/*
4962 				 * Free up resources.
4963 				 */
4964 				cam_periph_invalidate(periph);
4965 			}
4966 		}
4967 	}
4968 	free(csio->data_ptr, M_SCSIDA);
4969 	if (announce_buf != NULL &&
4970 	    ((softc->flags & DA_FLAG_ANNOUNCED) == 0)) {
4971 		struct sbuf sb;
4972 
4973 		sbuf_new(&sb, softc->announcebuf, DA_ANNOUNCE_SZ,
4974 		    SBUF_FIXEDLEN);
4975 		xpt_announce_periph_sbuf(periph, &sb, announce_buf);
4976 		xpt_announce_quirks_sbuf(periph, &sb, softc->quirks,
4977 		    DA_Q_BIT_STRING);
4978 		sbuf_finish(&sb);
4979 		sbuf_putbuf(&sb);
4980 
4981 		/*
4982 		 * Create our sysctl variables, now that we know
4983 		 * we have successfully attached.
4984 		 */
4985 		/* increase the refcount */
4986 		if (da_periph_acquire(periph, DA_REF_SYSCTL) == 0) {
4987 			taskqueue_enqueue(taskqueue_thread,
4988 					  &softc->sysctl_task);
4989 		} else {
4990 			/* XXX This message is useless! */
4991 			xpt_print(periph->path, "fatal error, "
4992 			    "could not acquire reference count\n");
4993 		}
4994 	}
4995 
4996 	/* We already probed the device. */
4997 	if (softc->flags & DA_FLAG_PROBED) {
4998 		daprobedone(periph, done_ccb);
4999 		return;
5000 	}
5001 
5002 	/* Ensure re-probe doesn't see old delete. */
5003 	softc->delete_available = 0;
5004 	dadeleteflag(softc, DA_DELETE_ZERO, 1);
5005 	if (lbp && (softc->quirks & DA_Q_NO_UNMAP) == 0) {
5006 		/*
5007 		 * Based on older SBC-3 spec revisions
5008 		 * any of the UNMAP methods "may" be
5009 		 * available via LBP given this flag so
5010 		 * we flag all of them as available and
5011 		 * then remove those which further
5012 		 * probes confirm aren't available
5013 		 * later.
5014 		 *
5015 		 * We could also check readcap(16) p_type
5016 		 * flag to exclude one or more invalid
5017 		 * write same (X) types here
5018 		 */
5019 		dadeleteflag(softc, DA_DELETE_WS16, 1);
5020 		dadeleteflag(softc, DA_DELETE_WS10, 1);
5021 		dadeleteflag(softc, DA_DELETE_UNMAP, 1);
5022 
5023 		softc->state = DA_STATE_PROBE_LBP;
5024 		xpt_release_ccb(done_ccb);
5025 		xpt_schedule(periph, priority);
5026 		return;
5027 	}
5028 
5029 	softc->state = DA_STATE_PROBE_BDC;
5030 	xpt_release_ccb(done_ccb);
5031 	xpt_schedule(periph, priority);
5032 	return;
5033 }
5034 
5035 static void
5036 dadone_probelbp(struct cam_periph *periph, union ccb *done_ccb)
5037 {
5038 	struct scsi_vpd_logical_block_prov *lbp;
5039 	struct da_softc *softc;
5040 	struct ccb_scsiio *csio;
5041 	uint32_t  priority;
5042 
5043 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone_probelbp\n"));
5044 
5045 	softc = (struct da_softc *)periph->softc;
5046 	priority = done_ccb->ccb_h.pinfo.priority;
5047 	csio = &done_ccb->csio;
5048 	lbp = (struct scsi_vpd_logical_block_prov *)csio->data_ptr;
5049 
5050 	cam_periph_assert(periph, MA_OWNED);
5051 
5052 	if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5053 		/*
5054 		 * T10/1799-D Revision 31 states at least one of these
5055 		 * must be supported but we don't currently enforce this.
5056 		 */
5057 		dadeleteflag(softc, DA_DELETE_WS16,
5058 		     (lbp->flags & SVPD_LBP_WS16));
5059 		dadeleteflag(softc, DA_DELETE_WS10,
5060 			     (lbp->flags & SVPD_LBP_WS10));
5061 		dadeleteflag(softc, DA_DELETE_UNMAP,
5062 			     (lbp->flags & SVPD_LBP_UNMAP));
5063 	} else {
5064 		int error;
5065 		error = daerror(done_ccb, CAM_RETRY_SELTO,
5066 				SF_RETRY_UA|SF_NO_PRINT);
5067 		if (error == ERESTART)
5068 			return;
5069 		else if (error != 0) {
5070 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5071 				/* Don't wedge this device's queue */
5072 				cam_release_devq(done_ccb->ccb_h.path,
5073 						 /*relsim_flags*/0,
5074 						 /*reduction*/0,
5075 						 /*timeout*/0,
5076 						 /*getcount_only*/0);
5077 			}
5078 
5079 			/*
5080 			 * Failure indicates we don't support any SBC-3
5081 			 * delete methods with UNMAP
5082 			 */
5083 		}
5084 	}
5085 
5086 	free(lbp, M_SCSIDA);
5087 	softc->state = DA_STATE_PROBE_BLK_LIMITS;
5088 	xpt_release_ccb(done_ccb);
5089 	xpt_schedule(periph, priority);
5090 	return;
5091 }
5092 
5093 static void
5094 dadone_probeblklimits(struct cam_periph *periph, union ccb *done_ccb)
5095 {
5096 	struct scsi_vpd_block_limits *block_limits;
5097 	struct da_softc *softc;
5098 	struct ccb_scsiio *csio;
5099 	uint32_t  priority;
5100 
5101 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone_probeblklimits\n"));
5102 
5103 	softc = (struct da_softc *)periph->softc;
5104 	priority = done_ccb->ccb_h.pinfo.priority;
5105 	csio = &done_ccb->csio;
5106 	block_limits = (struct scsi_vpd_block_limits *)csio->data_ptr;
5107 
5108 	cam_periph_assert(periph, MA_OWNED);
5109 
5110 	if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5111 		uint32_t max_txfer_len = scsi_4btoul(
5112 			block_limits->max_txfer_len);
5113 		uint32_t max_unmap_lba_cnt = scsi_4btoul(
5114 			block_limits->max_unmap_lba_cnt);
5115 		uint32_t max_unmap_blk_cnt = scsi_4btoul(
5116 			block_limits->max_unmap_blk_cnt);
5117 		uint32_t unmap_gran = scsi_4btoul(
5118 			block_limits->opt_unmap_grain);
5119 		uint32_t unmap_gran_align = scsi_4btoul(
5120 			block_limits->unmap_grain_align);
5121 		uint64_t ws_max_blks = scsi_8btou64(
5122 			block_limits->max_write_same_length);
5123 
5124 		if (max_txfer_len != 0) {
5125 			softc->disk->d_maxsize = MIN(softc->maxio,
5126 			    (off_t)max_txfer_len * softc->params.secsize);
5127 		}
5128 
5129 		/*
5130 		 * We should already support UNMAP but we check lba
5131 		 * and block count to be sure
5132 		 */
5133 		if (max_unmap_lba_cnt != 0x00L &&
5134 		    max_unmap_blk_cnt != 0x00L) {
5135 			softc->unmap_max_lba = max_unmap_lba_cnt;
5136 			softc->unmap_max_ranges = min(max_unmap_blk_cnt,
5137 				UNMAP_MAX_RANGES);
5138 			if (unmap_gran > 1) {
5139 				softc->unmap_gran = unmap_gran;
5140 				if (unmap_gran_align & 0x80000000) {
5141 					softc->unmap_gran_align =
5142 					    unmap_gran_align & 0x7fffffff;
5143 				}
5144 			}
5145 		} else {
5146 			/*
5147 			 * Unexpected UNMAP limits which means the
5148 			 * device doesn't actually support UNMAP
5149 			 */
5150 			dadeleteflag(softc, DA_DELETE_UNMAP, 0);
5151 		}
5152 
5153 		if (ws_max_blks != 0x00L)
5154 			softc->ws_max_blks = ws_max_blks;
5155 	} else {
5156 		int error;
5157 		error = daerror(done_ccb, CAM_RETRY_SELTO,
5158 				SF_RETRY_UA|SF_NO_PRINT);
5159 		if (error == ERESTART)
5160 			return;
5161 		else if (error != 0) {
5162 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5163 				/* Don't wedge this device's queue */
5164 				cam_release_devq(done_ccb->ccb_h.path,
5165 						 /*relsim_flags*/0,
5166 						 /*reduction*/0,
5167 						 /*timeout*/0,
5168 						 /*getcount_only*/0);
5169 			}
5170 
5171 			/*
5172 			 * Failure here doesn't mean UNMAP is not
5173 			 * supported as this is an optional page.
5174 			 */
5175 			softc->unmap_max_lba = 1;
5176 			softc->unmap_max_ranges = 1;
5177 		}
5178 	}
5179 
5180 	free(block_limits, M_SCSIDA);
5181 	softc->state = DA_STATE_PROBE_BDC;
5182 	xpt_release_ccb(done_ccb);
5183 	xpt_schedule(periph, priority);
5184 	return;
5185 }
5186 
5187 static void
5188 dadone_probebdc(struct cam_periph *periph, union ccb *done_ccb)
5189 {
5190 	struct scsi_vpd_block_device_characteristics *bdc;
5191 	struct da_softc *softc;
5192 	struct ccb_scsiio *csio;
5193 	uint32_t  priority;
5194 
5195 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone_probebdc\n"));
5196 
5197 	softc = (struct da_softc *)periph->softc;
5198 	priority = done_ccb->ccb_h.pinfo.priority;
5199 	csio = &done_ccb->csio;
5200 	bdc = (struct scsi_vpd_block_device_characteristics *)csio->data_ptr;
5201 
5202 	cam_periph_assert(periph, MA_OWNED);
5203 
5204 	if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5205 		uint32_t valid_len;
5206 
5207 		/*
5208 		 * Disable queue sorting for non-rotational media
5209 		 * by default.
5210 		 */
5211 		uint16_t old_rate = softc->disk->d_rotation_rate;
5212 
5213 		valid_len = csio->dxfer_len - csio->resid;
5214 		if (SBDC_IS_PRESENT(bdc, valid_len,
5215 		    medium_rotation_rate)) {
5216 			softc->disk->d_rotation_rate =
5217 				scsi_2btoul(bdc->medium_rotation_rate);
5218 			if (softc->disk->d_rotation_rate == SVPD_NON_ROTATING) {
5219 				cam_iosched_set_sort_queue(
5220 				    softc->cam_iosched, 0);
5221 				softc->flags &= ~DA_FLAG_ROTATING;
5222 			}
5223 			if (softc->disk->d_rotation_rate != old_rate) {
5224 				disk_attr_changed(softc->disk,
5225 				    "GEOM::rotation_rate", M_NOWAIT);
5226 			}
5227 		}
5228 		if ((SBDC_IS_PRESENT(bdc, valid_len, flags))
5229 		 && (softc->zone_mode == DA_ZONE_NONE)) {
5230 			int ata_proto;
5231 
5232 			if (scsi_vpd_supported_page(periph,
5233 			    SVPD_ATA_INFORMATION))
5234 				ata_proto = 1;
5235 			else
5236 				ata_proto = 0;
5237 
5238 			/*
5239 			 * The Zoned field will only be set for
5240 			 * Drive Managed and Host Aware drives.  If
5241 			 * they are Host Managed, the device type
5242 			 * in the standard INQUIRY data should be
5243 			 * set to T_ZBC_HM (0x14).
5244 			 */
5245 			if ((bdc->flags & SVPD_ZBC_MASK) ==
5246 			     SVPD_HAW_ZBC) {
5247 				softc->zone_mode = DA_ZONE_HOST_AWARE;
5248 				softc->zone_interface = (ata_proto) ?
5249 				   DA_ZONE_IF_ATA_SAT : DA_ZONE_IF_SCSI;
5250 			} else if ((bdc->flags & SVPD_ZBC_MASK) ==
5251 			     SVPD_DM_ZBC) {
5252 				softc->zone_mode =DA_ZONE_DRIVE_MANAGED;
5253 				softc->zone_interface = (ata_proto) ?
5254 				   DA_ZONE_IF_ATA_SAT : DA_ZONE_IF_SCSI;
5255 			} else if ((bdc->flags & SVPD_ZBC_MASK) !=
5256 				  SVPD_ZBC_NR) {
5257 				xpt_print(periph->path, "Unknown zoned "
5258 				    "type %#x",
5259 				    bdc->flags & SVPD_ZBC_MASK);
5260 			}
5261 		}
5262 	} else {
5263 		int error;
5264 		error = daerror(done_ccb, CAM_RETRY_SELTO,
5265 				SF_RETRY_UA|SF_NO_PRINT);
5266 		if (error == ERESTART)
5267 			return;
5268 		else if (error != 0) {
5269 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5270 				/* Don't wedge this device's queue */
5271 				cam_release_devq(done_ccb->ccb_h.path,
5272 						 /*relsim_flags*/0,
5273 						 /*reduction*/0,
5274 						 /*timeout*/0,
5275 						 /*getcount_only*/0);
5276 			}
5277 		}
5278 	}
5279 
5280 	free(bdc, M_SCSIDA);
5281 	softc->state = DA_STATE_PROBE_ATA;
5282 	xpt_release_ccb(done_ccb);
5283 	xpt_schedule(periph, priority);
5284 	return;
5285 }
5286 
5287 static void
5288 dadone_probeata(struct cam_periph *periph, union ccb *done_ccb)
5289 {
5290 	struct ata_params *ata_params;
5291 	struct ccb_scsiio *csio;
5292 	struct da_softc *softc;
5293 	uint32_t  priority;
5294 	int continue_probe;
5295 	int error;
5296 
5297 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone_probeata\n"));
5298 
5299 	softc = (struct da_softc *)periph->softc;
5300 	priority = done_ccb->ccb_h.pinfo.priority;
5301 	csio = &done_ccb->csio;
5302 	ata_params = (struct ata_params *)csio->data_ptr;
5303 	continue_probe = 0;
5304 	error = 0;
5305 
5306 	cam_periph_assert(periph, MA_OWNED);
5307 
5308 	if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5309 		uint16_t old_rate;
5310 
5311 		ata_param_fixup(ata_params);
5312 		if (ata_params->support_dsm & ATA_SUPPORT_DSM_TRIM &&
5313 		    (softc->quirks & DA_Q_NO_UNMAP) == 0) {
5314 			dadeleteflag(softc, DA_DELETE_ATA_TRIM, 1);
5315 			if (ata_params->max_dsm_blocks != 0)
5316 				softc->trim_max_ranges = min(
5317 				  softc->trim_max_ranges,
5318 				  ata_params->max_dsm_blocks *
5319 				  ATA_DSM_BLK_RANGES);
5320 		}
5321 		/*
5322 		 * Disable queue sorting for non-rotational media
5323 		 * by default.
5324 		 */
5325 		old_rate = softc->disk->d_rotation_rate;
5326 		softc->disk->d_rotation_rate = ata_params->media_rotation_rate;
5327 		if (softc->disk->d_rotation_rate == ATA_RATE_NON_ROTATING) {
5328 			cam_iosched_set_sort_queue(softc->cam_iosched, 0);
5329 			softc->flags &= ~DA_FLAG_ROTATING;
5330 		}
5331 		if (softc->disk->d_rotation_rate != old_rate) {
5332 			disk_attr_changed(softc->disk,
5333 			    "GEOM::rotation_rate", M_NOWAIT);
5334 		}
5335 
5336 		cam_periph_assert(periph, MA_OWNED);
5337 		if (ata_params->capabilities1 & ATA_SUPPORT_DMA)
5338 			softc->flags |= DA_FLAG_CAN_ATA_DMA;
5339 
5340 		if (ata_params->support.extension & ATA_SUPPORT_GENLOG)
5341 			softc->flags |= DA_FLAG_CAN_ATA_LOG;
5342 
5343 		/*
5344 		 * At this point, if we have a SATA host aware drive,
5345 		 * we communicate via ATA passthrough unless the
5346 		 * SAT layer supports ZBC -> ZAC translation.  In
5347 		 * that case,
5348 		 *
5349 		 * XXX KDM figure out how to detect a host managed
5350 		 * SATA drive.
5351 		 */
5352 		if (softc->zone_mode == DA_ZONE_NONE) {
5353 			/*
5354 			 * Note that we don't override the zone
5355 			 * mode or interface if it has already been
5356 			 * set.  This is because it has either been
5357 			 * set as a quirk, or when we probed the
5358 			 * SCSI Block Device Characteristics page,
5359 			 * the zoned field was set.  The latter
5360 			 * means that the SAT layer supports ZBC to
5361 			 * ZAC translation, and we would prefer to
5362 			 * use that if it is available.
5363 			 */
5364 			if ((ata_params->support3 &
5365 			    ATA_SUPPORT_ZONE_MASK) ==
5366 			    ATA_SUPPORT_ZONE_HOST_AWARE) {
5367 				softc->zone_mode = DA_ZONE_HOST_AWARE;
5368 				softc->zone_interface =
5369 				    DA_ZONE_IF_ATA_PASS;
5370 			} else if ((ata_params->support3 &
5371 				    ATA_SUPPORT_ZONE_MASK) ==
5372 				    ATA_SUPPORT_ZONE_DEV_MANAGED) {
5373 				softc->zone_mode =DA_ZONE_DRIVE_MANAGED;
5374 				softc->zone_interface = DA_ZONE_IF_ATA_PASS;
5375 			}
5376 		}
5377 
5378 	} else {
5379 		error = daerror(done_ccb, CAM_RETRY_SELTO,
5380 				SF_RETRY_UA|SF_NO_PRINT);
5381 		if (error == ERESTART)
5382 			return;
5383 		else if (error != 0) {
5384 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5385 				/* Don't wedge this device's queue */
5386 				cam_release_devq(done_ccb->ccb_h.path,
5387 						 /*relsim_flags*/0,
5388 						 /*reduction*/0,
5389 						 /*timeout*/0,
5390 						 /*getcount_only*/0);
5391 			}
5392 		}
5393 	}
5394 
5395 	if ((softc->zone_mode == DA_ZONE_HOST_AWARE)
5396 	 || (softc->zone_mode == DA_ZONE_HOST_MANAGED)) {
5397 		/*
5398 		 * If the ATA IDENTIFY failed, we could be talking
5399 		 * to a SCSI drive, although that seems unlikely,
5400 		 * since the drive did report that it supported the
5401 		 * ATA Information VPD page.  If the ATA IDENTIFY
5402 		 * succeeded, and the SAT layer doesn't support
5403 		 * ZBC -> ZAC translation, continue on to get the
5404 		 * directory of ATA logs, and complete the rest of
5405 		 * the ZAC probe.  If the SAT layer does support
5406 		 * ZBC -> ZAC translation, we want to use that,
5407 		 * and we'll probe the SCSI Zoned Block Device
5408 		 * Characteristics VPD page next.
5409 		 */
5410 		if ((error == 0)
5411 		 && (softc->flags & DA_FLAG_CAN_ATA_LOG)
5412 		 && (softc->zone_interface == DA_ZONE_IF_ATA_PASS))
5413 			softc->state = DA_STATE_PROBE_ATA_LOGDIR;
5414 		else
5415 			softc->state = DA_STATE_PROBE_ZONE;
5416 		continue_probe = 1;
5417 	}
5418 	if (continue_probe != 0) {
5419 		xpt_schedule(periph, priority);
5420 		xpt_release_ccb(done_ccb);
5421 		return;
5422 	} else
5423 		daprobedone(periph, done_ccb);
5424 	return;
5425 }
5426 
5427 static void
5428 dadone_probeatalogdir(struct cam_periph *periph, union ccb *done_ccb)
5429 {
5430 	struct da_softc *softc;
5431 	struct ccb_scsiio *csio;
5432 	uint32_t  priority;
5433 	int error;
5434 
5435 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone_probeatalogdir\n"));
5436 
5437 	softc = (struct da_softc *)periph->softc;
5438 	priority = done_ccb->ccb_h.pinfo.priority;
5439 	csio = &done_ccb->csio;
5440 
5441 	cam_periph_assert(periph, MA_OWNED);
5442 	if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5443 		error = 0;
5444 		softc->valid_logdir_len = 0;
5445 		bzero(&softc->ata_logdir, sizeof(softc->ata_logdir));
5446 		softc->valid_logdir_len = csio->dxfer_len - csio->resid;
5447 		if (softc->valid_logdir_len > 0)
5448 			bcopy(csio->data_ptr, &softc->ata_logdir,
5449 			    min(softc->valid_logdir_len,
5450 				sizeof(softc->ata_logdir)));
5451 		/*
5452 		 * Figure out whether the Identify Device log is
5453 		 * supported.  The General Purpose log directory
5454 		 * has a header, and lists the number of pages
5455 		 * available for each GP log identified by the
5456 		 * offset into the list.
5457 		 */
5458 		if ((softc->valid_logdir_len >=
5459 		    ((ATA_IDENTIFY_DATA_LOG + 1) * sizeof(uint16_t)))
5460 		 && (le16dec(softc->ata_logdir.header) ==
5461 		     ATA_GP_LOG_DIR_VERSION)
5462 		 && (le16dec(&softc->ata_logdir.num_pages[
5463 		     (ATA_IDENTIFY_DATA_LOG *
5464 		     sizeof(uint16_t)) - sizeof(uint16_t)]) > 0)){
5465 			softc->flags |= DA_FLAG_CAN_ATA_IDLOG;
5466 		} else {
5467 			softc->flags &= ~DA_FLAG_CAN_ATA_IDLOG;
5468 		}
5469 	} else {
5470 		error = daerror(done_ccb, CAM_RETRY_SELTO,
5471 				SF_RETRY_UA|SF_NO_PRINT);
5472 		if (error == ERESTART)
5473 			return;
5474 		else if (error != 0) {
5475 			/*
5476 			 * If we can't get the ATA log directory,
5477 			 * then ATA logs are effectively not
5478 			 * supported even if the bit is set in the
5479 			 * identify data.
5480 			 */
5481 			softc->flags &= ~(DA_FLAG_CAN_ATA_LOG |
5482 					  DA_FLAG_CAN_ATA_IDLOG);
5483 			if ((done_ccb->ccb_h.status &
5484 			     CAM_DEV_QFRZN) != 0) {
5485 				/* Don't wedge this device's queue */
5486 				cam_release_devq(done_ccb->ccb_h.path,
5487 						 /*relsim_flags*/0,
5488 						 /*reduction*/0,
5489 						 /*timeout*/0,
5490 						 /*getcount_only*/0);
5491 			}
5492 		}
5493 	}
5494 
5495 	free(csio->data_ptr, M_SCSIDA);
5496 
5497 	if ((error == 0)
5498 	 && (softc->flags & DA_FLAG_CAN_ATA_IDLOG)) {
5499 		softc->state = DA_STATE_PROBE_ATA_IDDIR;
5500 		xpt_release_ccb(done_ccb);
5501 		xpt_schedule(periph, priority);
5502 		return;
5503 	}
5504 	daprobedone(periph, done_ccb);
5505 	return;
5506 }
5507 
5508 static void
5509 dadone_probeataiddir(struct cam_periph *periph, union ccb *done_ccb)
5510 {
5511 	struct da_softc *softc;
5512 	struct ccb_scsiio *csio;
5513 	uint32_t  priority;
5514 	int error;
5515 
5516 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone_probeataiddir\n"));
5517 
5518 	softc = (struct da_softc *)periph->softc;
5519 	priority = done_ccb->ccb_h.pinfo.priority;
5520 	csio = &done_ccb->csio;
5521 
5522 	cam_periph_assert(periph, MA_OWNED);
5523 
5524 	if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5525 		off_t entries_offset, max_entries;
5526 		error = 0;
5527 
5528 		softc->valid_iddir_len = 0;
5529 		bzero(&softc->ata_iddir, sizeof(softc->ata_iddir));
5530 		softc->flags &= ~(DA_FLAG_CAN_ATA_SUPCAP |
5531 				  DA_FLAG_CAN_ATA_ZONE);
5532 		softc->valid_iddir_len = csio->dxfer_len - csio->resid;
5533 		if (softc->valid_iddir_len > 0)
5534 			bcopy(csio->data_ptr, &softc->ata_iddir,
5535 			    min(softc->valid_iddir_len,
5536 				sizeof(softc->ata_iddir)));
5537 
5538 		entries_offset =
5539 		    __offsetof(struct ata_identify_log_pages,entries);
5540 		max_entries = softc->valid_iddir_len - entries_offset;
5541 		if ((softc->valid_iddir_len > (entries_offset + 1))
5542 		 && (le64dec(softc->ata_iddir.header) == ATA_IDLOG_REVISION)
5543 		 && (softc->ata_iddir.entry_count > 0)) {
5544 			int num_entries, i;
5545 
5546 			num_entries = softc->ata_iddir.entry_count;
5547 			num_entries = min(num_entries,
5548 			   softc->valid_iddir_len - entries_offset);
5549 			for (i = 0; i < num_entries && i < max_entries; i++) {
5550 				if (softc->ata_iddir.entries[i] ==
5551 				    ATA_IDL_SUP_CAP)
5552 					softc->flags |= DA_FLAG_CAN_ATA_SUPCAP;
5553 				else if (softc->ata_iddir.entries[i] ==
5554 					 ATA_IDL_ZDI)
5555 					softc->flags |= DA_FLAG_CAN_ATA_ZONE;
5556 
5557 				if ((softc->flags & DA_FLAG_CAN_ATA_SUPCAP)
5558 				 && (softc->flags & DA_FLAG_CAN_ATA_ZONE))
5559 					break;
5560 			}
5561 		}
5562 	} else {
5563 		error = daerror(done_ccb, CAM_RETRY_SELTO,
5564 				SF_RETRY_UA|SF_NO_PRINT);
5565 		if (error == ERESTART)
5566 			return;
5567 		else if (error != 0) {
5568 			/*
5569 			 * If we can't get the ATA Identify Data log
5570 			 * directory, then it effectively isn't
5571 			 * supported even if the ATA Log directory
5572 			 * a non-zero number of pages present for
5573 			 * this log.
5574 			 */
5575 			softc->flags &= ~DA_FLAG_CAN_ATA_IDLOG;
5576 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5577 				/* Don't wedge this device's queue */
5578 				cam_release_devq(done_ccb->ccb_h.path,
5579 						 /*relsim_flags*/0,
5580 						 /*reduction*/0,
5581 						 /*timeout*/0,
5582 						 /*getcount_only*/0);
5583 			}
5584 		}
5585 	}
5586 
5587 	free(csio->data_ptr, M_SCSIDA);
5588 
5589 	if ((error == 0) && (softc->flags & DA_FLAG_CAN_ATA_SUPCAP)) {
5590 		softc->state = DA_STATE_PROBE_ATA_SUP;
5591 		xpt_release_ccb(done_ccb);
5592 		xpt_schedule(periph, priority);
5593 		return;
5594 	}
5595 	daprobedone(periph, done_ccb);
5596 	return;
5597 }
5598 
5599 static void
5600 dadone_probeatasup(struct cam_periph *periph, union ccb *done_ccb)
5601 {
5602 	struct da_softc *softc;
5603 	struct ccb_scsiio *csio;
5604 	uint32_t  priority;
5605 	int error;
5606 
5607 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone_probeatasup\n"));
5608 
5609 	softc = (struct da_softc *)periph->softc;
5610 	priority = done_ccb->ccb_h.pinfo.priority;
5611 	csio = &done_ccb->csio;
5612 
5613 	cam_periph_assert(periph, MA_OWNED);
5614 
5615 	if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5616 		uint32_t valid_len;
5617 		size_t needed_size;
5618 		struct ata_identify_log_sup_cap *sup_cap;
5619 		error = 0;
5620 
5621 		sup_cap = (struct ata_identify_log_sup_cap *)csio->data_ptr;
5622 		valid_len = csio->dxfer_len - csio->resid;
5623 		needed_size = __offsetof(struct ata_identify_log_sup_cap,
5624 		    sup_zac_cap) + 1 + sizeof(sup_cap->sup_zac_cap);
5625 		if (valid_len >= needed_size) {
5626 			uint64_t zoned, zac_cap;
5627 
5628 			zoned = le64dec(sup_cap->zoned_cap);
5629 			if (zoned & ATA_ZONED_VALID) {
5630 				/*
5631 				 * This should have already been
5632 				 * set, because this is also in the
5633 				 * ATA identify data.
5634 				 */
5635 				if ((zoned & ATA_ZONED_MASK) ==
5636 				    ATA_SUPPORT_ZONE_HOST_AWARE)
5637 					softc->zone_mode = DA_ZONE_HOST_AWARE;
5638 				else if ((zoned & ATA_ZONED_MASK) ==
5639 				    ATA_SUPPORT_ZONE_DEV_MANAGED)
5640 					softc->zone_mode =
5641 					    DA_ZONE_DRIVE_MANAGED;
5642 			}
5643 
5644 			zac_cap = le64dec(sup_cap->sup_zac_cap);
5645 			if (zac_cap & ATA_SUP_ZAC_CAP_VALID) {
5646 				if (zac_cap & ATA_REPORT_ZONES_SUP)
5647 					softc->zone_flags |=
5648 					    DA_ZONE_FLAG_RZ_SUP;
5649 				if (zac_cap & ATA_ND_OPEN_ZONE_SUP)
5650 					softc->zone_flags |=
5651 					    DA_ZONE_FLAG_OPEN_SUP;
5652 				if (zac_cap & ATA_ND_CLOSE_ZONE_SUP)
5653 					softc->zone_flags |=
5654 					    DA_ZONE_FLAG_CLOSE_SUP;
5655 				if (zac_cap & ATA_ND_FINISH_ZONE_SUP)
5656 					softc->zone_flags |=
5657 					    DA_ZONE_FLAG_FINISH_SUP;
5658 				if (zac_cap & ATA_ND_RWP_SUP)
5659 					softc->zone_flags |=
5660 					    DA_ZONE_FLAG_RWP_SUP;
5661 			} else {
5662 				/*
5663 				 * This field was introduced in
5664 				 * ACS-4, r08 on April 28th, 2015.
5665 				 * If the drive firmware was written
5666 				 * to an earlier spec, it won't have
5667 				 * the field.  So, assume all
5668 				 * commands are supported.
5669 				 */
5670 				softc->zone_flags |= DA_ZONE_FLAG_SUP_MASK;
5671 			}
5672 		}
5673 	} else {
5674 		error = daerror(done_ccb, CAM_RETRY_SELTO,
5675 				SF_RETRY_UA|SF_NO_PRINT);
5676 		if (error == ERESTART)
5677 			return;
5678 		else if (error != 0) {
5679 			/*
5680 			 * If we can't get the ATA Identify Data
5681 			 * Supported Capabilities page, clear the
5682 			 * flag...
5683 			 */
5684 			softc->flags &= ~DA_FLAG_CAN_ATA_SUPCAP;
5685 			/*
5686 			 * And clear zone capabilities.
5687 			 */
5688 			softc->zone_flags &= ~DA_ZONE_FLAG_SUP_MASK;
5689 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5690 				/* Don't wedge this device's queue */
5691 				cam_release_devq(done_ccb->ccb_h.path,
5692 						 /*relsim_flags*/0,
5693 						 /*reduction*/0,
5694 						 /*timeout*/0,
5695 						 /*getcount_only*/0);
5696 			}
5697 		}
5698 	}
5699 
5700 	free(csio->data_ptr, M_SCSIDA);
5701 
5702 	if ((error == 0) && (softc->flags & DA_FLAG_CAN_ATA_ZONE)) {
5703 		softc->state = DA_STATE_PROBE_ATA_ZONE;
5704 		xpt_release_ccb(done_ccb);
5705 		xpt_schedule(periph, priority);
5706 		return;
5707 	}
5708 	daprobedone(periph, done_ccb);
5709 	return;
5710 }
5711 
5712 static void
5713 dadone_probeatazone(struct cam_periph *periph, union ccb *done_ccb)
5714 {
5715 	struct da_softc *softc;
5716 	struct ccb_scsiio *csio;
5717 	int error;
5718 
5719 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone_probeatazone\n"));
5720 
5721 	softc = (struct da_softc *)periph->softc;
5722 	csio = &done_ccb->csio;
5723 
5724 	cam_periph_assert(periph, MA_OWNED);
5725 
5726 	if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5727 		struct ata_zoned_info_log *zi_log;
5728 		uint32_t valid_len;
5729 		size_t needed_size;
5730 
5731 		zi_log = (struct ata_zoned_info_log *)csio->data_ptr;
5732 
5733 		valid_len = csio->dxfer_len - csio->resid;
5734 		needed_size = __offsetof(struct ata_zoned_info_log,
5735 		    version_info) + 1 + sizeof(zi_log->version_info);
5736 		if (valid_len >= needed_size) {
5737 			uint64_t tmpvar;
5738 
5739 			tmpvar = le64dec(zi_log->zoned_cap);
5740 			if (tmpvar & ATA_ZDI_CAP_VALID) {
5741 				if (tmpvar & ATA_ZDI_CAP_URSWRZ)
5742 					softc->zone_flags |=
5743 					    DA_ZONE_FLAG_URSWRZ;
5744 				else
5745 					softc->zone_flags &=
5746 					    ~DA_ZONE_FLAG_URSWRZ;
5747 			}
5748 			tmpvar = le64dec(zi_log->optimal_seq_zones);
5749 			if (tmpvar & ATA_ZDI_OPT_SEQ_VALID) {
5750 				softc->zone_flags |= DA_ZONE_FLAG_OPT_SEQ_SET;
5751 				softc->optimal_seq_zones = (tmpvar &
5752 				    ATA_ZDI_OPT_SEQ_MASK);
5753 			} else {
5754 				softc->zone_flags &= ~DA_ZONE_FLAG_OPT_SEQ_SET;
5755 				softc->optimal_seq_zones = 0;
5756 			}
5757 
5758 			tmpvar =le64dec(zi_log->optimal_nonseq_zones);
5759 			if (tmpvar & ATA_ZDI_OPT_NS_VALID) {
5760 				softc->zone_flags |=
5761 				    DA_ZONE_FLAG_OPT_NONSEQ_SET;
5762 				softc->optimal_nonseq_zones =
5763 				    (tmpvar & ATA_ZDI_OPT_NS_MASK);
5764 			} else {
5765 				softc->zone_flags &=
5766 				    ~DA_ZONE_FLAG_OPT_NONSEQ_SET;
5767 				softc->optimal_nonseq_zones = 0;
5768 			}
5769 
5770 			tmpvar = le64dec(zi_log->max_seq_req_zones);
5771 			if (tmpvar & ATA_ZDI_MAX_SEQ_VALID) {
5772 				softc->zone_flags |= DA_ZONE_FLAG_MAX_SEQ_SET;
5773 				softc->max_seq_zones =
5774 				    (tmpvar & ATA_ZDI_MAX_SEQ_MASK);
5775 			} else {
5776 				softc->zone_flags &= ~DA_ZONE_FLAG_MAX_SEQ_SET;
5777 				softc->max_seq_zones = 0;
5778 			}
5779 		}
5780 	} else {
5781 		error = daerror(done_ccb, CAM_RETRY_SELTO,
5782 				SF_RETRY_UA|SF_NO_PRINT);
5783 		if (error == ERESTART)
5784 			return;
5785 		else if (error != 0) {
5786 			softc->flags &= ~DA_FLAG_CAN_ATA_ZONE;
5787 			softc->flags &= ~DA_ZONE_FLAG_SET_MASK;
5788 
5789 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5790 				/* Don't wedge this device's queue */
5791 				cam_release_devq(done_ccb->ccb_h.path,
5792 						 /*relsim_flags*/0,
5793 						 /*reduction*/0,
5794 						 /*timeout*/0,
5795 						 /*getcount_only*/0);
5796 			}
5797 		}
5798 	}
5799 
5800 	free(csio->data_ptr, M_SCSIDA);
5801 
5802 	daprobedone(periph, done_ccb);
5803 	return;
5804 }
5805 
5806 static void
5807 dadone_probezone(struct cam_periph *periph, union ccb *done_ccb)
5808 {
5809 	struct da_softc *softc;
5810 	struct ccb_scsiio *csio;
5811 	int error;
5812 
5813 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone_probezone\n"));
5814 
5815 	softc = (struct da_softc *)periph->softc;
5816 	csio = &done_ccb->csio;
5817 
5818 	cam_periph_assert(periph, MA_OWNED);
5819 
5820 	if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5821 		uint32_t valid_len;
5822 		size_t needed_len;
5823 		struct scsi_vpd_zoned_bdc *zoned_bdc;
5824 
5825 		error = 0;
5826 		zoned_bdc = (struct scsi_vpd_zoned_bdc *)csio->data_ptr;
5827 		valid_len = csio->dxfer_len - csio->resid;
5828 		needed_len = __offsetof(struct scsi_vpd_zoned_bdc,
5829 		    max_seq_req_zones) + 1 +
5830 		    sizeof(zoned_bdc->max_seq_req_zones);
5831 		if ((valid_len >= needed_len)
5832 		 && (scsi_2btoul(zoned_bdc->page_length) >= SVPD_ZBDC_PL)) {
5833 			if (zoned_bdc->flags & SVPD_ZBDC_URSWRZ)
5834 				softc->zone_flags |= DA_ZONE_FLAG_URSWRZ;
5835 			else
5836 				softc->zone_flags &= ~DA_ZONE_FLAG_URSWRZ;
5837 			softc->optimal_seq_zones =
5838 			    scsi_4btoul(zoned_bdc->optimal_seq_zones);
5839 			softc->zone_flags |= DA_ZONE_FLAG_OPT_SEQ_SET;
5840 			softc->optimal_nonseq_zones = scsi_4btoul(
5841 			    zoned_bdc->optimal_nonseq_zones);
5842 			softc->zone_flags |= DA_ZONE_FLAG_OPT_NONSEQ_SET;
5843 			softc->max_seq_zones =
5844 			    scsi_4btoul(zoned_bdc->max_seq_req_zones);
5845 			softc->zone_flags |= DA_ZONE_FLAG_MAX_SEQ_SET;
5846 		}
5847 		/*
5848 		 * All of the zone commands are mandatory for SCSI
5849 		 * devices.
5850 		 *
5851 		 * XXX KDM this is valid as of September 2015.
5852 		 * Re-check this assumption once the SAT spec is
5853 		 * updated to support SCSI ZBC to ATA ZAC mapping.
5854 		 * Since ATA allows zone commands to be reported
5855 		 * as supported or not, this may not necessarily
5856 		 * be true for an ATA device behind a SAT (SCSI to
5857 		 * ATA Translation) layer.
5858 		 */
5859 		softc->zone_flags |= DA_ZONE_FLAG_SUP_MASK;
5860 	} else {
5861 		error = daerror(done_ccb, CAM_RETRY_SELTO,
5862 				SF_RETRY_UA|SF_NO_PRINT);
5863 		if (error == ERESTART)
5864 			return;
5865 		else if (error != 0) {
5866 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5867 				/* Don't wedge this device's queue */
5868 				cam_release_devq(done_ccb->ccb_h.path,
5869 						 /*relsim_flags*/0,
5870 						 /*reduction*/0,
5871 						 /*timeout*/0,
5872 						 /*getcount_only*/0);
5873 			}
5874 		}
5875 	}
5876 
5877 	free(csio->data_ptr, M_SCSIDA);
5878 
5879 	daprobedone(periph, done_ccb);
5880 	return;
5881 }
5882 
5883 static void
5884 dadone_tur(struct cam_periph *periph, union ccb *done_ccb)
5885 {
5886 	struct da_softc *softc;
5887 
5888 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone_tur\n"));
5889 
5890 	softc = (struct da_softc *)periph->softc;
5891 
5892 	cam_periph_assert(periph, MA_OWNED);
5893 
5894 	if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5895 		if (daerror(done_ccb, CAM_RETRY_SELTO,
5896 		    SF_RETRY_UA | SF_NO_RECOVERY | SF_NO_PRINT) == ERESTART)
5897 			return;	/* Will complete again, keep reference */
5898 		if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
5899 			cam_release_devq(done_ccb->ccb_h.path,
5900 					 /*relsim_flags*/0,
5901 					 /*reduction*/0,
5902 					 /*timeout*/0,
5903 					 /*getcount_only*/0);
5904 	}
5905 	softc->flags &= ~DA_FLAG_TUR_PENDING;
5906 	xpt_release_ccb(done_ccb);
5907 	da_periph_release_locked(periph, DA_REF_TUR);
5908 	return;
5909 }
5910 
5911 static void
5912 dareprobe(struct cam_periph *periph)
5913 {
5914 	struct da_softc	  *softc;
5915 	int status __diagused;
5916 
5917 	softc = (struct da_softc *)periph->softc;
5918 
5919 	cam_periph_assert(periph, MA_OWNED);
5920 
5921 	/* Probe in progress; don't interfere. */
5922 	if (softc->state != DA_STATE_NORMAL)
5923 		return;
5924 
5925 	status = da_periph_acquire(periph, DA_REF_REPROBE);
5926 	KASSERT(status == 0, ("dareprobe: cam_periph_acquire failed"));
5927 
5928 	softc->state = DA_STATE_PROBE_WP;
5929 	xpt_schedule(periph, CAM_PRIORITY_DEV);
5930 }
5931 
5932 static int
5933 daerror(union ccb *ccb, uint32_t cam_flags, uint32_t sense_flags)
5934 {
5935 	struct da_softc	  *softc;
5936 	struct cam_periph *periph;
5937 	int error, error_code, sense_key, asc, ascq;
5938 
5939 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
5940 	if (ccb->csio.bio != NULL)
5941 		biotrack(ccb->csio.bio, __func__);
5942 #endif
5943 
5944 	periph = xpt_path_periph(ccb->ccb_h.path);
5945 	softc = (struct da_softc *)periph->softc;
5946 
5947 	cam_periph_assert(periph, MA_OWNED);
5948 
5949 	/*
5950 	 * Automatically detect devices that do not support
5951 	 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
5952 	 */
5953 	error = 0;
5954 	if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
5955 		error = cmd6workaround(ccb);
5956 	} else if (scsi_extract_sense_ccb(ccb,
5957 	    &error_code, &sense_key, &asc, &ascq)) {
5958 		if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
5959 			error = cmd6workaround(ccb);
5960 		/*
5961 		 * If the target replied with CAPACITY DATA HAS CHANGED UA,
5962 		 * query the capacity and notify upper layers.
5963 		 */
5964 		else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
5965 		    asc == 0x2A && ascq == 0x09) {
5966 			xpt_print(periph->path, "Capacity data has changed\n");
5967 			softc->flags &= ~DA_FLAG_PROBED;
5968 			dareprobe(periph);
5969 			sense_flags |= SF_NO_PRINT;
5970 		} else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
5971 		    asc == 0x28 && ascq == 0x00) {
5972 			softc->flags &= ~DA_FLAG_PROBED;
5973 			disk_media_changed(softc->disk, M_NOWAIT);
5974 		} else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
5975 		    asc == 0x3F && ascq == 0x03) {
5976 			xpt_print(periph->path, "INQUIRY data has changed\n");
5977 			softc->flags &= ~DA_FLAG_PROBED;
5978 			dareprobe(periph);
5979 			sense_flags |= SF_NO_PRINT;
5980 		} else if (sense_key == SSD_KEY_NOT_READY &&
5981 		    asc == 0x3a && (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
5982 			softc->flags |= DA_FLAG_PACK_INVALID;
5983 			disk_media_gone(softc->disk, M_NOWAIT);
5984 		}
5985 	}
5986 	if (error == ERESTART)
5987 		return (ERESTART);
5988 
5989 #ifdef CAM_IO_STATS
5990 	switch (ccb->ccb_h.status & CAM_STATUS_MASK) {
5991 	case CAM_CMD_TIMEOUT:
5992 		softc->timeouts++;
5993 		break;
5994 	case CAM_REQ_ABORTED:
5995 	case CAM_REQ_CMP_ERR:
5996 	case CAM_REQ_TERMIO:
5997 	case CAM_UNREC_HBA_ERROR:
5998 	case CAM_DATA_RUN_ERR:
5999 	case CAM_SCSI_STATUS_ERROR:
6000 	case CAM_ATA_STATUS_ERROR:
6001 		softc->errors++;
6002 		break;
6003 	default:
6004 		break;
6005 	}
6006 #endif
6007 
6008 	/*
6009 	 * XXX
6010 	 * Until we have a better way of doing pack validation,
6011 	 * don't treat UAs as errors.
6012 	 */
6013 	sense_flags |= SF_RETRY_UA;
6014 
6015 	if (softc->quirks & DA_Q_RETRY_BUSY)
6016 		sense_flags |= SF_RETRY_BUSY;
6017 	return(cam_periph_error(ccb, cam_flags, sense_flags));
6018 }
6019 
6020 static void
6021 damediapoll(void *arg)
6022 {
6023 	struct cam_periph *periph = arg;
6024 	struct da_softc *softc = periph->softc;
6025 
6026 	if (!cam_iosched_has_work_flags(softc->cam_iosched, DA_WORK_TUR) &&
6027 	    (softc->flags & DA_FLAG_TUR_PENDING) == 0 &&
6028 	    softc->state == DA_STATE_NORMAL &&
6029 	    LIST_EMPTY(&softc->pending_ccbs)) {
6030 		if (da_periph_acquire(periph, DA_REF_TUR) == 0) {
6031 			cam_iosched_set_work_flags(softc->cam_iosched, DA_WORK_TUR);
6032 			daschedule(periph);
6033 		}
6034 	}
6035 
6036 	/* Queue us up again */
6037 	if (da_poll_period != 0) {
6038 		callout_schedule_sbt(&softc->mediapoll_c,
6039 		    da_poll_period * SBT_1S, 0, C_PREL(1));
6040 	}
6041 }
6042 
6043 static void
6044 daprevent(struct cam_periph *periph, int action)
6045 {
6046 	struct	da_softc *softc;
6047 	union	ccb *ccb;
6048 	int	error;
6049 
6050 	cam_periph_assert(periph, MA_OWNED);
6051 	softc = (struct da_softc *)periph->softc;
6052 
6053 	if (((action == PR_ALLOW)
6054 	  && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
6055 	 || ((action == PR_PREVENT)
6056 	  && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
6057 		return;
6058 	}
6059 
6060 	ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
6061 
6062 	scsi_prevent(&ccb->csio,
6063 		     /*retries*/1,
6064 		     /*cbcfp*/NULL,
6065 		     MSG_SIMPLE_Q_TAG,
6066 		     action,
6067 		     SSD_FULL_SIZE,
6068 		     5000);
6069 
6070 	error = cam_periph_runccb(ccb, daerror, CAM_RETRY_SELTO,
6071 	    SF_RETRY_UA | SF_NO_PRINT, softc->disk->d_devstat);
6072 
6073 	if (error == 0) {
6074 		if (action == PR_ALLOW)
6075 			softc->flags &= ~DA_FLAG_PACK_LOCKED;
6076 		else
6077 			softc->flags |= DA_FLAG_PACK_LOCKED;
6078 	}
6079 
6080 	xpt_release_ccb(ccb);
6081 }
6082 
6083 static void
6084 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector,
6085 	  struct scsi_read_capacity_data_long *rcaplong, size_t rcap_len)
6086 {
6087 	struct ccb_calc_geometry ccg;
6088 	struct da_softc *softc;
6089 	struct disk_params *dp;
6090 	u_int lbppbe, lalba;
6091 	int error;
6092 
6093 	softc = (struct da_softc *)periph->softc;
6094 
6095 	dp = &softc->params;
6096 	dp->secsize = block_len;
6097 	dp->sectors = maxsector + 1;
6098 	if (rcaplong != NULL) {
6099 		lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
6100 		lalba = scsi_2btoul(rcaplong->lalba_lbp);
6101 		lalba &= SRC16_LALBA_A;
6102 		if (rcaplong->prot & SRC16_PROT_EN)
6103 			softc->p_type = ((rcaplong->prot & SRC16_P_TYPE) >>
6104 			    SRC16_P_TYPE_SHIFT) + 1;
6105 		else
6106 			softc->p_type = 0;
6107 	} else {
6108 		lbppbe = 0;
6109 		lalba = 0;
6110 		softc->p_type = 0;
6111 	}
6112 
6113 	if (lbppbe > 0) {
6114 		dp->stripesize = block_len << lbppbe;
6115 		dp->stripeoffset = (dp->stripesize - block_len * lalba) %
6116 		    dp->stripesize;
6117 	} else if (softc->quirks & DA_Q_4K) {
6118 		dp->stripesize = 4096;
6119 		dp->stripeoffset = 0;
6120 	} else if (softc->unmap_gran != 0) {
6121 		dp->stripesize = block_len * softc->unmap_gran;
6122 		dp->stripeoffset = (dp->stripesize - block_len *
6123 		    softc->unmap_gran_align) % dp->stripesize;
6124 	} else {
6125 		dp->stripesize = 0;
6126 		dp->stripeoffset = 0;
6127 	}
6128 	/*
6129 	 * Have the controller provide us with a geometry
6130 	 * for this disk.  The only time the geometry
6131 	 * matters is when we boot and the controller
6132 	 * is the only one knowledgeable enough to come
6133 	 * up with something that will make this a bootable
6134 	 * device.
6135 	 */
6136 	memset(&ccg, 0, sizeof(ccg));
6137 	xpt_setup_ccb(&ccg.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
6138 	ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
6139 	ccg.block_size = dp->secsize;
6140 	ccg.volume_size = dp->sectors;
6141 	ccg.heads = 0;
6142 	ccg.secs_per_track = 0;
6143 	ccg.cylinders = 0;
6144 	xpt_action((union ccb*)&ccg);
6145 	if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
6146 		/*
6147 		 * We don't know what went wrong here- but just pick
6148 		 * a geometry so we don't have nasty things like divide
6149 		 * by zero.
6150 		 */
6151 		dp->heads = 255;
6152 		dp->secs_per_track = 255;
6153 		dp->cylinders = dp->sectors / (255 * 255);
6154 		if (dp->cylinders == 0) {
6155 			dp->cylinders = 1;
6156 		}
6157 	} else {
6158 		dp->heads = ccg.heads;
6159 		dp->secs_per_track = ccg.secs_per_track;
6160 		dp->cylinders = ccg.cylinders;
6161 	}
6162 
6163 	/*
6164 	 * If the user supplied a read capacity buffer, and if it is
6165 	 * different than the previous buffer, update the data in the EDT.
6166 	 * If it's the same, we don't bother.  This avoids sending an
6167 	 * update every time someone opens this device.
6168 	 */
6169 	if ((rcaplong != NULL)
6170 	 && (bcmp(rcaplong, &softc->rcaplong,
6171 		  min(sizeof(softc->rcaplong), rcap_len)) != 0)) {
6172 		struct ccb_dev_advinfo cdai;
6173 
6174 		memset(&cdai, 0, sizeof(cdai));
6175 		xpt_setup_ccb(&cdai.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
6176 		cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
6177 		cdai.buftype = CDAI_TYPE_RCAPLONG;
6178 		cdai.flags = CDAI_FLAG_STORE;
6179 		cdai.bufsiz = rcap_len;
6180 		cdai.buf = (uint8_t *)rcaplong;
6181 		xpt_action((union ccb *)&cdai);
6182 		if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
6183 			cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
6184 		if (cdai.ccb_h.status != CAM_REQ_CMP) {
6185 			xpt_print(periph->path, "%s: failed to set read "
6186 				  "capacity advinfo\n", __func__);
6187 			/* Use cam_error_print() to decode the status */
6188 			cam_error_print((union ccb *)&cdai, CAM_ESF_CAM_STATUS,
6189 					CAM_EPF_ALL);
6190 		} else {
6191 			bcopy(rcaplong, &softc->rcaplong,
6192 			      min(sizeof(softc->rcaplong), rcap_len));
6193 		}
6194 	}
6195 
6196 	softc->disk->d_sectorsize = softc->params.secsize;
6197 	softc->disk->d_mediasize = softc->params.secsize * (off_t)softc->params.sectors;
6198 	softc->disk->d_stripesize = softc->params.stripesize;
6199 	softc->disk->d_stripeoffset = softc->params.stripeoffset;
6200 	/* XXX: these are not actually "firmware" values, so they may be wrong */
6201 	softc->disk->d_fwsectors = softc->params.secs_per_track;
6202 	softc->disk->d_fwheads = softc->params.heads;
6203 	softc->disk->d_devstat->block_size = softc->params.secsize;
6204 	softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
6205 
6206 	error = disk_resize(softc->disk, M_NOWAIT);
6207 	if (error != 0)
6208 		xpt_print(periph->path, "disk_resize(9) failed, error = %d\n", error);
6209 }
6210 
6211 static void
6212 dasendorderedtag(void *arg)
6213 {
6214 	struct cam_periph *periph = arg;
6215 	struct da_softc *softc = periph->softc;
6216 
6217 	cam_periph_assert(periph, MA_OWNED);
6218 	if (da_send_ordered) {
6219 		if (!LIST_EMPTY(&softc->pending_ccbs)) {
6220 			if ((softc->flags & DA_FLAG_WAS_OTAG) == 0)
6221 				softc->flags |= DA_FLAG_NEED_OTAG;
6222 			softc->flags &= ~DA_FLAG_WAS_OTAG;
6223 		}
6224 	}
6225 
6226 	/* Queue us up again */
6227 	callout_schedule_sbt(&softc->sendordered_c,
6228 	    SBT_1S / DA_ORDEREDTAG_INTERVAL * da_default_timeout, 0,
6229 	    C_PREL(1));
6230 }
6231 
6232 /*
6233  * Step through all DA peripheral drivers, and if the device is still open,
6234  * sync the disk cache to physical media.
6235  */
6236 static void
6237 dashutdown(void * arg, int howto)
6238 {
6239 	struct cam_periph *periph;
6240 	struct da_softc *softc;
6241 	union ccb *ccb;
6242 	int error;
6243 
6244 	if ((howto & RB_NOSYNC) != 0)
6245 		return;
6246 
6247 	CAM_PERIPH_FOREACH(periph, &dadriver) {
6248 		softc = (struct da_softc *)periph->softc;
6249 		if (SCHEDULER_STOPPED()) {
6250 			/* If we paniced with the lock held, do not recurse. */
6251 			if (!cam_periph_owned(periph) &&
6252 			    (softc->flags & DA_FLAG_OPEN)) {
6253 				dadump(softc->disk, NULL, 0, 0);
6254 			}
6255 			continue;
6256 		}
6257 		cam_periph_lock(periph);
6258 
6259 		/*
6260 		 * We only sync the cache if the drive is still open, and
6261 		 * if the drive is capable of it..
6262 		 */
6263 		if (((softc->flags & DA_FLAG_OPEN) == 0)
6264 		 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
6265 			cam_periph_unlock(periph);
6266 			continue;
6267 		}
6268 
6269 		ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
6270 		scsi_synchronize_cache(&ccb->csio,
6271 				       /*retries*/0,
6272 				       /*cbfcnp*/NULL,
6273 				       MSG_SIMPLE_Q_TAG,
6274 				       /*begin_lba*/0, /* whole disk */
6275 				       /*lb_count*/0,
6276 				       SSD_FULL_SIZE,
6277 				       60 * 60 * 1000);
6278 
6279 		error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
6280 		    /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR,
6281 		    softc->disk->d_devstat);
6282 		if (error != 0)
6283 			xpt_print(periph->path, "Synchronize cache failed\n");
6284 		xpt_release_ccb(ccb);
6285 		cam_periph_unlock(periph);
6286 	}
6287 }
6288 
6289 #else /* !_KERNEL */
6290 
6291 /*
6292  * XXX These are only left out of the kernel build to silence warnings.  If,
6293  * for some reason these functions are used in the kernel, the ifdefs should
6294  * be moved so they are included both in the kernel and userland.
6295  */
6296 void
6297 scsi_format_unit(struct ccb_scsiio *csio, uint32_t retries,
6298 		 void (*cbfcnp)(struct cam_periph *, union ccb *),
6299 		 uint8_t tag_action, uint8_t byte2, uint16_t ileave,
6300 		 uint8_t *data_ptr, uint32_t dxfer_len, uint8_t sense_len,
6301 		 uint32_t timeout)
6302 {
6303 	struct scsi_format_unit *scsi_cmd;
6304 
6305 	scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
6306 	scsi_cmd->opcode = FORMAT_UNIT;
6307 	scsi_cmd->byte2 = byte2;
6308 	scsi_ulto2b(ileave, scsi_cmd->interleave);
6309 
6310 	cam_fill_csio(csio,
6311 		      retries,
6312 		      cbfcnp,
6313 		      /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
6314 		      tag_action,
6315 		      data_ptr,
6316 		      dxfer_len,
6317 		      sense_len,
6318 		      sizeof(*scsi_cmd),
6319 		      timeout);
6320 }
6321 
6322 void
6323 scsi_read_defects(struct ccb_scsiio *csio, uint32_t retries,
6324 		  void (*cbfcnp)(struct cam_periph *, union ccb *),
6325 		  uint8_t tag_action, uint8_t list_format,
6326 		  uint32_t addr_desc_index, uint8_t *data_ptr,
6327 		  uint32_t dxfer_len, int minimum_cmd_size,
6328 		  uint8_t sense_len, uint32_t timeout)
6329 {
6330 	uint8_t cdb_len;
6331 
6332 	/*
6333 	 * These conditions allow using the 10 byte command.  Otherwise we
6334 	 * need to use the 12 byte command.
6335 	 */
6336 	if ((minimum_cmd_size <= 10)
6337 	 && (addr_desc_index == 0)
6338 	 && (dxfer_len <= SRDD10_MAX_LENGTH)) {
6339 		struct scsi_read_defect_data_10 *cdb10;
6340 
6341 		cdb10 = (struct scsi_read_defect_data_10 *)
6342 			&csio->cdb_io.cdb_bytes;
6343 
6344 		cdb_len = sizeof(*cdb10);
6345 		bzero(cdb10, cdb_len);
6346                 cdb10->opcode = READ_DEFECT_DATA_10;
6347                 cdb10->format = list_format;
6348                 scsi_ulto2b(dxfer_len, cdb10->alloc_length);
6349 	} else {
6350 		struct scsi_read_defect_data_12 *cdb12;
6351 
6352 		cdb12 = (struct scsi_read_defect_data_12 *)
6353 			&csio->cdb_io.cdb_bytes;
6354 
6355 		cdb_len = sizeof(*cdb12);
6356 		bzero(cdb12, cdb_len);
6357                 cdb12->opcode = READ_DEFECT_DATA_12;
6358                 cdb12->format = list_format;
6359                 scsi_ulto4b(dxfer_len, cdb12->alloc_length);
6360 		scsi_ulto4b(addr_desc_index, cdb12->address_descriptor_index);
6361 	}
6362 
6363 	cam_fill_csio(csio,
6364 		      retries,
6365 		      cbfcnp,
6366 		      /*flags*/ CAM_DIR_IN,
6367 		      tag_action,
6368 		      data_ptr,
6369 		      dxfer_len,
6370 		      sense_len,
6371 		      cdb_len,
6372 		      timeout);
6373 }
6374 
6375 void
6376 scsi_sanitize(struct ccb_scsiio *csio, uint32_t retries,
6377 	      void (*cbfcnp)(struct cam_periph *, union ccb *),
6378 	      uint8_t tag_action, uint8_t byte2, uint16_t control,
6379 	      uint8_t *data_ptr, uint32_t dxfer_len, uint8_t sense_len,
6380 	      uint32_t timeout)
6381 {
6382 	struct scsi_sanitize *scsi_cmd;
6383 
6384 	scsi_cmd = (struct scsi_sanitize *)&csio->cdb_io.cdb_bytes;
6385 	scsi_cmd->opcode = SANITIZE;
6386 	scsi_cmd->byte2 = byte2;
6387 	scsi_cmd->control = control;
6388 	scsi_ulto2b(dxfer_len, scsi_cmd->length);
6389 
6390 	cam_fill_csio(csio,
6391 		      retries,
6392 		      cbfcnp,
6393 		      /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
6394 		      tag_action,
6395 		      data_ptr,
6396 		      dxfer_len,
6397 		      sense_len,
6398 		      sizeof(*scsi_cmd),
6399 		      timeout);
6400 }
6401 
6402 #endif /* _KERNEL */
6403 
6404 void
6405 scsi_zbc_out(struct ccb_scsiio *csio, uint32_t retries,
6406 	     void (*cbfcnp)(struct cam_periph *, union ccb *),
6407 	     uint8_t tag_action, uint8_t service_action, uint64_t zone_id,
6408 	     uint8_t zone_flags, uint8_t *data_ptr, uint32_t dxfer_len,
6409 	     uint8_t sense_len, uint32_t timeout)
6410 {
6411 	struct scsi_zbc_out *scsi_cmd;
6412 
6413 	scsi_cmd = (struct scsi_zbc_out *)&csio->cdb_io.cdb_bytes;
6414 	scsi_cmd->opcode = ZBC_OUT;
6415 	scsi_cmd->service_action = service_action;
6416 	scsi_u64to8b(zone_id, scsi_cmd->zone_id);
6417 	scsi_cmd->zone_flags = zone_flags;
6418 
6419 	cam_fill_csio(csio,
6420 		      retries,
6421 		      cbfcnp,
6422 		      /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
6423 		      tag_action,
6424 		      data_ptr,
6425 		      dxfer_len,
6426 		      sense_len,
6427 		      sizeof(*scsi_cmd),
6428 		      timeout);
6429 }
6430 
6431 void
6432 scsi_zbc_in(struct ccb_scsiio *csio, uint32_t retries,
6433 	    void (*cbfcnp)(struct cam_periph *, union ccb *),
6434 	    uint8_t tag_action, uint8_t service_action, uint64_t zone_start_lba,
6435 	    uint8_t zone_options, uint8_t *data_ptr, uint32_t dxfer_len,
6436 	    uint8_t sense_len, uint32_t timeout)
6437 {
6438 	struct scsi_zbc_in *scsi_cmd;
6439 
6440 	scsi_cmd = (struct scsi_zbc_in *)&csio->cdb_io.cdb_bytes;
6441 	scsi_cmd->opcode = ZBC_IN;
6442 	scsi_cmd->service_action = service_action;
6443 	scsi_ulto4b(dxfer_len, scsi_cmd->length);
6444 	scsi_u64to8b(zone_start_lba, scsi_cmd->zone_start_lba);
6445 	scsi_cmd->zone_options = zone_options;
6446 
6447 	cam_fill_csio(csio,
6448 		      retries,
6449 		      cbfcnp,
6450 		      /*flags*/ (dxfer_len > 0) ? CAM_DIR_IN : CAM_DIR_NONE,
6451 		      tag_action,
6452 		      data_ptr,
6453 		      dxfer_len,
6454 		      sense_len,
6455 		      sizeof(*scsi_cmd),
6456 		      timeout);
6457 
6458 }
6459 
6460 int
6461 scsi_ata_zac_mgmt_out(struct ccb_scsiio *csio, uint32_t retries,
6462 		      void (*cbfcnp)(struct cam_periph *, union ccb *),
6463 		      uint8_t tag_action, int use_ncq,
6464 		      uint8_t zm_action, uint64_t zone_id, uint8_t zone_flags,
6465 		      uint8_t *data_ptr, uint32_t dxfer_len,
6466 		      uint8_t *cdb_storage, size_t cdb_storage_len,
6467 		      uint8_t sense_len, uint32_t timeout)
6468 {
6469 	uint8_t command_out, protocol, ata_flags;
6470 	uint16_t features_out;
6471 	uint32_t sectors_out, auxiliary;
6472 	int retval;
6473 
6474 	retval = 0;
6475 
6476 	if (use_ncq == 0) {
6477 		command_out = ATA_ZAC_MANAGEMENT_OUT;
6478 		features_out = (zm_action & 0xf) | (zone_flags << 8);
6479 		ata_flags = AP_FLAG_BYT_BLOK_BLOCKS;
6480 		if (dxfer_len == 0) {
6481 			protocol = AP_PROTO_NON_DATA;
6482 			ata_flags |= AP_FLAG_TLEN_NO_DATA;
6483 			sectors_out = 0;
6484 		} else {
6485 			protocol = AP_PROTO_DMA;
6486 			ata_flags |= AP_FLAG_TLEN_SECT_CNT |
6487 				     AP_FLAG_TDIR_TO_DEV;
6488 			sectors_out = ((dxfer_len >> 9) & 0xffff);
6489 		}
6490 		auxiliary = 0;
6491 	} else {
6492 		ata_flags = AP_FLAG_BYT_BLOK_BLOCKS;
6493 		if (dxfer_len == 0) {
6494 			command_out = ATA_NCQ_NON_DATA;
6495 			features_out = ATA_NCQ_ZAC_MGMT_OUT;
6496 			/*
6497 			 * We're assuming the SCSI to ATA translation layer
6498 			 * will set the NCQ tag number in the tag field.
6499 			 * That isn't clear from the SAT-4 spec (as of rev 05).
6500 			 */
6501 			sectors_out = 0;
6502 			ata_flags |= AP_FLAG_TLEN_NO_DATA;
6503 		} else {
6504 			command_out = ATA_SEND_FPDMA_QUEUED;
6505 			/*
6506 			 * Note that we're defaulting to normal priority,
6507 			 * and assuming that the SCSI to ATA translation
6508 			 * layer will insert the NCQ tag number in the tag
6509 			 * field.  That isn't clear in the SAT-4 spec (as
6510 			 * of rev 05).
6511 			 */
6512 			sectors_out = ATA_SFPDMA_ZAC_MGMT_OUT << 8;
6513 
6514 			ata_flags |= AP_FLAG_TLEN_FEAT |
6515 				     AP_FLAG_TDIR_TO_DEV;
6516 
6517 			/*
6518 			 * For SEND FPDMA QUEUED, the transfer length is
6519 			 * encoded in the FEATURE register, and 0 means
6520 			 * that 65536 512 byte blocks are to be tranferred.
6521 			 * In practice, it seems unlikely that we'll see
6522 			 * a transfer that large, and it may confuse the
6523 			 * the SAT layer, because generally that means that
6524 			 * 0 bytes should be transferred.
6525 			 */
6526 			if (dxfer_len == (65536 * 512)) {
6527 				features_out = 0;
6528 			} else if (dxfer_len <= (65535 * 512)) {
6529 				features_out = ((dxfer_len >> 9) & 0xffff);
6530 			} else {
6531 				/* The transfer is too big. */
6532 				retval = 1;
6533 				goto bailout;
6534 			}
6535 		}
6536 
6537 		auxiliary = (zm_action & 0xf) | (zone_flags << 8);
6538 		protocol = AP_PROTO_FPDMA;
6539 	}
6540 
6541 	protocol |= AP_EXTEND;
6542 
6543 	retval = scsi_ata_pass(csio,
6544 	    retries,
6545 	    cbfcnp,
6546 	    /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
6547 	    tag_action,
6548 	    /*protocol*/ protocol,
6549 	    /*ata_flags*/ ata_flags,
6550 	    /*features*/ features_out,
6551 	    /*sector_count*/ sectors_out,
6552 	    /*lba*/ zone_id,
6553 	    /*command*/ command_out,
6554 	    /*device*/ 0,
6555 	    /*icc*/ 0,
6556 	    /*auxiliary*/ auxiliary,
6557 	    /*control*/ 0,
6558 	    /*data_ptr*/ data_ptr,
6559 	    /*dxfer_len*/ dxfer_len,
6560 	    /*cdb_storage*/ cdb_storage,
6561 	    /*cdb_storage_len*/ cdb_storage_len,
6562 	    /*minimum_cmd_size*/ 0,
6563 	    /*sense_len*/ SSD_FULL_SIZE,
6564 	    /*timeout*/ timeout);
6565 
6566 bailout:
6567 
6568 	return (retval);
6569 }
6570 
6571 int
6572 scsi_ata_zac_mgmt_in(struct ccb_scsiio *csio, uint32_t retries,
6573 		     void (*cbfcnp)(struct cam_periph *, union ccb *),
6574 		     uint8_t tag_action, int use_ncq,
6575 		     uint8_t zm_action, uint64_t zone_id, uint8_t zone_flags,
6576 		     uint8_t *data_ptr, uint32_t dxfer_len,
6577 		     uint8_t *cdb_storage, size_t cdb_storage_len,
6578 		     uint8_t sense_len, uint32_t timeout)
6579 {
6580 	uint8_t command_out, protocol;
6581 	uint16_t features_out, sectors_out;
6582 	uint32_t auxiliary;
6583 	int ata_flags;
6584 	int retval;
6585 
6586 	retval = 0;
6587 	ata_flags = AP_FLAG_TDIR_FROM_DEV | AP_FLAG_BYT_BLOK_BLOCKS;
6588 
6589 	if (use_ncq == 0) {
6590 		command_out = ATA_ZAC_MANAGEMENT_IN;
6591 		/* XXX KDM put a macro here */
6592 		features_out = (zm_action & 0xf) | (zone_flags << 8);
6593 		sectors_out = dxfer_len >> 9; /* XXX KDM macro */
6594 		protocol = AP_PROTO_DMA;
6595 		ata_flags |= AP_FLAG_TLEN_SECT_CNT;
6596 		auxiliary = 0;
6597 	} else {
6598 		ata_flags |= AP_FLAG_TLEN_FEAT;
6599 
6600 		command_out = ATA_RECV_FPDMA_QUEUED;
6601 		sectors_out = ATA_RFPDMA_ZAC_MGMT_IN << 8;
6602 
6603 		/*
6604 		 * For RECEIVE FPDMA QUEUED, the transfer length is
6605 		 * encoded in the FEATURE register, and 0 means
6606 		 * that 65536 512 byte blocks are to be tranferred.
6607 		 * In practice, it seems unlikely that we'll see
6608 		 * a transfer that large, and it may confuse the
6609 		 * the SAT layer, because generally that means that
6610 		 * 0 bytes should be transferred.
6611 		 */
6612 		if (dxfer_len == (65536 * 512)) {
6613 			features_out = 0;
6614 		} else if (dxfer_len <= (65535 * 512)) {
6615 			features_out = ((dxfer_len >> 9) & 0xffff);
6616 		} else {
6617 			/* The transfer is too big. */
6618 			retval = 1;
6619 			goto bailout;
6620 		}
6621 		auxiliary = (zm_action & 0xf) | (zone_flags << 8),
6622 		protocol = AP_PROTO_FPDMA;
6623 	}
6624 
6625 	protocol |= AP_EXTEND;
6626 
6627 	retval = scsi_ata_pass(csio,
6628 	    retries,
6629 	    cbfcnp,
6630 	    /*flags*/ CAM_DIR_IN,
6631 	    tag_action,
6632 	    /*protocol*/ protocol,
6633 	    /*ata_flags*/ ata_flags,
6634 	    /*features*/ features_out,
6635 	    /*sector_count*/ sectors_out,
6636 	    /*lba*/ zone_id,
6637 	    /*command*/ command_out,
6638 	    /*device*/ 0,
6639 	    /*icc*/ 0,
6640 	    /*auxiliary*/ auxiliary,
6641 	    /*control*/ 0,
6642 	    /*data_ptr*/ data_ptr,
6643 	    /*dxfer_len*/ (dxfer_len >> 9) * 512, /* XXX KDM */
6644 	    /*cdb_storage*/ cdb_storage,
6645 	    /*cdb_storage_len*/ cdb_storage_len,
6646 	    /*minimum_cmd_size*/ 0,
6647 	    /*sense_len*/ SSD_FULL_SIZE,
6648 	    /*timeout*/ timeout);
6649 
6650 bailout:
6651 	return (retval);
6652 }
6653