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