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