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