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