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