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