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