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