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