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