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