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