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