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