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