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