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