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