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