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