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