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