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