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