xref: /freebsd/sys/cam/scsi/scsi_da.c (revision f11b6ce4a3bc6d455dbec375218c01ce9b6b5dc2)
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 device has been made invalid, error out
1903 	 */
1904 	if ((softc->flags & DA_FLAG_PACK_INVALID)) {
1905 		cam_periph_unlock(periph);
1906 		biofinish(bp, NULL, ENXIO);
1907 		return;
1908 	}
1909 
1910 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastrategy(%p)\n", bp));
1911 
1912 	/*
1913 	 * Zone commands must be ordered, because they can depend on the
1914 	 * effects of previously issued commands, and they may affect
1915 	 * commands after them.
1916 	 */
1917 	if (bp->bio_cmd == BIO_ZONE)
1918 		bp->bio_flags |= BIO_ORDERED;
1919 
1920 	/*
1921 	 * Place it in the queue of disk activities for this disk
1922 	 */
1923 	cam_iosched_queue_work(softc->cam_iosched, bp);
1924 
1925 	/*
1926 	 * Schedule ourselves for performing the work.
1927 	 */
1928 	daschedule(periph);
1929 	cam_periph_unlock(periph);
1930 
1931 	return;
1932 }
1933 
1934 static int
dadump(void * arg,void * virtual,off_t offset,size_t length)1935 dadump(void *arg, void *virtual, off_t offset, size_t length)
1936 {
1937 	struct	    cam_periph *periph;
1938 	struct	    da_softc *softc;
1939 	u_int	    secsize;
1940 	struct	    ccb_scsiio csio;
1941 	struct	    disk *dp;
1942 	int	    error = 0;
1943 
1944 	dp = arg;
1945 	periph = dp->d_drv1;
1946 	softc = (struct da_softc *)periph->softc;
1947 	secsize = softc->params.secsize;
1948 
1949 	if ((softc->flags & DA_FLAG_PACK_INVALID) != 0)
1950 		return (ENXIO);
1951 
1952 	memset(&csio, 0, sizeof(csio));
1953 	if (length > 0) {
1954 		xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1955 		csio.ccb_h.ccb_state = DA_CCB_DUMP;
1956 		scsi_read_write(&csio,
1957 				/*retries*/0,
1958 				/*cbfcnp*/NULL,
1959 				MSG_ORDERED_Q_TAG,
1960 				/*read*/SCSI_RW_WRITE,
1961 				/*byte2*/0,
1962 				/*minimum_cmd_size*/ softc->minimum_cmd_size,
1963 				offset / secsize,
1964 				length / secsize,
1965 				/*data_ptr*/(uint8_t *) virtual,
1966 				/*dxfer_len*/length,
1967 				/*sense_len*/SSD_FULL_SIZE,
1968 				da_default_timeout * 1000);
1969 		error = cam_periph_runccb((union ccb *)&csio, cam_periph_error,
1970 		    0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1971 		if (error != 0)
1972 			printf("Aborting dump due to I/O error.\n");
1973 		return (error);
1974 	}
1975 
1976 	/*
1977 	 * Sync the disk cache contents to the physical media.
1978 	 */
1979 	if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
1980 		xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1981 		csio.ccb_h.ccb_state = DA_CCB_DUMP;
1982 		scsi_synchronize_cache(&csio,
1983 				       /*retries*/0,
1984 				       /*cbfcnp*/NULL,
1985 				       MSG_SIMPLE_Q_TAG,
1986 				       /*begin_lba*/0,/* Cover the whole disk */
1987 				       /*lb_count*/0,
1988 				       SSD_FULL_SIZE,
1989 				       5 * 1000);
1990 		error = cam_periph_runccb((union ccb *)&csio, cam_periph_error,
1991 		    0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1992 		if (error != 0)
1993 			xpt_print(periph->path, "Synchronize cache failed\n");
1994 	}
1995 	return (error);
1996 }
1997 
1998 static int
dagetattr(struct bio * bp)1999 dagetattr(struct bio *bp)
2000 {
2001 	int ret;
2002 	struct cam_periph *periph;
2003 
2004 	if (g_handleattr_int(bp, "GEOM::canspeedup", da_enable_biospeedup))
2005 		return (EJUSTRETURN);
2006 
2007 	periph = (struct cam_periph *)bp->bio_disk->d_drv1;
2008 	cam_periph_lock(periph);
2009 	ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
2010 	    periph->path);
2011 	cam_periph_unlock(periph);
2012 	if (ret == 0)
2013 		bp->bio_completed = bp->bio_length;
2014 	return ret;
2015 }
2016 
2017 static void
dainit(void)2018 dainit(void)
2019 {
2020 	cam_status status;
2021 
2022 	da_ccb_zone = uma_zcreate("da_ccb",
2023 	    sizeof(struct ccb_scsiio), NULL, NULL, NULL, NULL,
2024 	    UMA_ALIGN_PTR, 0);
2025 
2026 	/*
2027 	 * Install a global async callback.  This callback will
2028 	 * receive async callbacks like "new device found".
2029 	 */
2030 	status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
2031 
2032 	if (status != CAM_REQ_CMP) {
2033 		printf("da: Failed to attach master async callback "
2034 		       "due to status 0x%x!\n", status);
2035 	} else if (da_send_ordered) {
2036 		/* Register our shutdown event handler */
2037 		if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
2038 					   NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
2039 		    printf("dainit: shutdown event registration failed!\n");
2040 	}
2041 }
2042 
2043 /*
2044  * Callback from GEOM, called when it has finished cleaning up its
2045  * resources.
2046  */
2047 static void
dadiskgonecb(struct disk * dp)2048 dadiskgonecb(struct disk *dp)
2049 {
2050 	struct cam_periph *periph;
2051 
2052 	periph = (struct cam_periph *)dp->d_drv1;
2053 	da_periph_release(periph, DA_REF_GEOM);
2054 }
2055 
2056 static void
daoninvalidate(struct cam_periph * periph)2057 daoninvalidate(struct cam_periph *periph)
2058 {
2059 	struct da_softc *softc;
2060 
2061 	cam_periph_assert(periph, MA_OWNED);
2062 	softc = (struct da_softc *)periph->softc;
2063 
2064 	/*
2065 	 * De-register any async callbacks.
2066 	 */
2067 	xpt_register_async(0, daasync, periph, periph->path);
2068 
2069 	softc->flags |= DA_FLAG_PACK_INVALID;
2070 #ifdef CAM_IO_STATS
2071 	softc->invalidations++;
2072 #endif
2073 
2074 	/*
2075 	 * Return all queued I/O with ENXIO. Transactions may be queued up here
2076 	 * for retry (since we are called while there's other transactions
2077 	 * pending). Any requests in the hardware will drain before dacleanup
2078 	 * is called.
2079 	 */
2080 	cam_iosched_flush(softc->cam_iosched, NULL, ENXIO);
2081 
2082 	/*
2083 	 * Tell GEOM that we've gone away, we'll get a callback when it is
2084 	 * done cleaning up its resources.
2085 	 */
2086 	disk_gone(softc->disk);
2087 }
2088 
2089 static void
dacleanup(struct cam_periph * periph)2090 dacleanup(struct cam_periph *periph)
2091 {
2092 	struct da_softc *softc;
2093 
2094 	softc = (struct da_softc *)periph->softc;
2095 
2096 	cam_periph_unlock(periph);
2097 
2098 	cam_iosched_fini(softc->cam_iosched);
2099 
2100 	/*
2101 	 * If we can't free the sysctl tree, oh well...
2102 	 */
2103 	if ((softc->flags & DA_FLAG_SCTX_INIT) != 0) {
2104 #ifdef CAM_IO_STATS
2105 		if (sysctl_ctx_free(&softc->sysctl_stats_ctx) != 0)
2106 			xpt_print(periph->path,
2107 			    "can't remove sysctl stats context\n");
2108 #endif
2109 		if (sysctl_ctx_free(&softc->sysctl_ctx) != 0)
2110 			xpt_print(periph->path,
2111 			    "can't remove sysctl context\n");
2112 	}
2113 
2114 	callout_drain(&softc->mediapoll_c);
2115 	disk_destroy(softc->disk);
2116 	callout_drain(&softc->sendordered_c);
2117 	free(softc, M_DEVBUF);
2118 	cam_periph_lock(periph);
2119 }
2120 
2121 static void
daasync(void * callback_arg,uint32_t code,struct cam_path * path,void * arg)2122 daasync(void *callback_arg, uint32_t code,
2123 	struct cam_path *path, void *arg)
2124 {
2125 	struct cam_periph *periph;
2126 	struct da_softc *softc;
2127 
2128 	periph = (struct cam_periph *)callback_arg;
2129 	switch (code) {
2130 	case AC_FOUND_DEVICE:	/* callback to create periph, no locking yet */
2131 	{
2132 		struct ccb_getdev *cgd;
2133 		cam_status status;
2134 
2135 		cgd = (struct ccb_getdev *)arg;
2136 		if (cgd == NULL)
2137 			break;
2138 
2139 		if (cgd->protocol != PROTO_SCSI)
2140 			break;
2141 		if (SID_QUAL(&cgd->inq_data) != SID_QUAL_LU_CONNECTED)
2142 			break;
2143 		if (SID_TYPE(&cgd->inq_data) != T_DIRECT
2144 		    && SID_TYPE(&cgd->inq_data) != T_RBC
2145 		    && SID_TYPE(&cgd->inq_data) != T_OPTICAL
2146 		    && SID_TYPE(&cgd->inq_data) != T_ZBC_HM)
2147 			break;
2148 
2149 		/*
2150 		 * Allocate a peripheral instance for
2151 		 * this device and start the probe
2152 		 * process.
2153 		 */
2154 		status = cam_periph_alloc(daregister, daoninvalidate,
2155 					  dacleanup, dastart,
2156 					  "da", CAM_PERIPH_BIO,
2157 					  path, daasync,
2158 					  AC_FOUND_DEVICE, cgd);
2159 
2160 		if (status != CAM_REQ_CMP
2161 		 && status != CAM_REQ_INPROG)
2162 			printf("daasync: Unable to attach to new device "
2163 				"due to status 0x%x\n", status);
2164 		return;
2165 	}
2166 	case AC_ADVINFO_CHANGED:	/* Doesn't touch periph */
2167 	{
2168 		uintptr_t buftype;
2169 
2170 		buftype = (uintptr_t)arg;
2171 		if (buftype == CDAI_TYPE_PHYS_PATH) {
2172 			struct da_softc *softc;
2173 
2174 			softc = periph->softc;
2175 			disk_attr_changed(softc->disk, "GEOM::physpath",
2176 					  M_NOWAIT);
2177 		}
2178 		break;
2179 	}
2180 	case AC_UNIT_ATTENTION:		/* Called for this path: periph locked */
2181 	{
2182 		union ccb *ccb;
2183 		int error_code, sense_key, asc, ascq;
2184 
2185 		softc = (struct da_softc *)periph->softc;
2186 		ccb = (union ccb *)arg;
2187 
2188 		/*
2189 		 * Handle all UNIT ATTENTIONs except our own, as they will be
2190 		 * handled by daerror().
2191 		 */
2192 		if (xpt_path_periph(ccb->ccb_h.path) != periph &&
2193 		    scsi_extract_sense_ccb(ccb,
2194 		     &error_code, &sense_key, &asc, &ascq)) {
2195 			if (asc == 0x2A && ascq == 0x09) {
2196 				/* 2a/9: CAPACITY DATA HAS CHANGED */
2197 				xpt_print(ccb->ccb_h.path,
2198 				    "Capacity data has changed\n");
2199 				cam_periph_assert(periph, MA_OWNED);
2200 				softc->flags &= ~DA_FLAG_PROBED;
2201 				dareprobe(periph);
2202 			} else if (asc == 0x28 && ascq == 0x00) {
2203 				/* 28/0: NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED */
2204 				cam_periph_assert(periph, MA_OWNED);
2205 				softc->flags &= ~DA_FLAG_PROBED;
2206 				disk_media_changed(softc->disk, M_NOWAIT);
2207 			} else if (asc == 0x3F && ascq == 0x03) {
2208 				/* 3f/3: INQUIRY DATA HAS CHANGED */
2209 				xpt_print(ccb->ccb_h.path,
2210 				    "INQUIRY data has changed\n");
2211 				cam_periph_assert(periph, MA_OWNED);
2212 				softc->flags &= ~DA_FLAG_PROBED;
2213 				dareprobe(periph);
2214 			}
2215 		}
2216 		break;
2217 	}
2218 	case AC_SCSI_AEN:		/* Called for this path: periph locked */
2219 		/*
2220 		 * Appears to be currently unused for SCSI devices, only ata SIMs
2221 		 * generate this.
2222 		 */
2223 		cam_periph_assert(periph, MA_OWNED);
2224 		softc = (struct da_softc *)periph->softc;
2225 		if (!cam_iosched_has_work_flags(softc->cam_iosched, DA_WORK_TUR) &&
2226 		    (softc->flags & DA_FLAG_TUR_PENDING) == 0) {
2227 			if (da_periph_acquire(periph, DA_REF_TUR) == 0) {
2228 				cam_iosched_set_work_flags(softc->cam_iosched, DA_WORK_TUR);
2229 				daschedule(periph);
2230 			}
2231 		}
2232 		/* FALLTHROUGH */
2233 	case AC_SENT_BDR:		/* Called for this path: periph locked */
2234 	case AC_BUS_RESET:		/* Called for this path: periph locked */
2235 	{
2236 		struct ccb_hdr *ccbh;
2237 
2238 		cam_periph_assert(periph, MA_OWNED);
2239 		softc = (struct da_softc *)periph->softc;
2240 		/*
2241 		 * Don't fail on the expected unit attention
2242 		 * that will occur.
2243 		 */
2244 		softc->flags |= DA_FLAG_RETRY_UA;
2245 		LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
2246 			ccbh->ccb_state |= DA_CCB_RETRY_UA;
2247 		break;
2248 	}
2249 	case AC_INQ_CHANGED:		/* Called for this path: periph locked */
2250 		cam_periph_assert(periph, MA_OWNED);
2251 		softc = (struct da_softc *)periph->softc;
2252 		softc->flags &= ~DA_FLAG_PROBED;
2253 		dareprobe(periph);
2254 		break;
2255 	default:
2256 		break;
2257 	}
2258 	cam_periph_async(periph, code, path, arg);
2259 }
2260 
2261 static void
dasysctlinit(void * context,int pending)2262 dasysctlinit(void *context, int pending)
2263 {
2264 	struct cam_periph *periph;
2265 	struct da_softc *softc;
2266 	char tmpstr[32], tmpstr2[16];
2267 	struct ccb_trans_settings cts;
2268 
2269 	periph = (struct cam_periph *)context;
2270 	/*
2271 	 * periph was held for us when this task was enqueued
2272 	 */
2273 	if (periph->flags & CAM_PERIPH_INVALID) {
2274 		da_periph_release(periph, DA_REF_SYSCTL);
2275 		return;
2276 	}
2277 
2278 	softc = (struct da_softc *)periph->softc;
2279 	snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
2280 	snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
2281 
2282 	sysctl_ctx_init(&softc->sysctl_ctx);
2283 	cam_periph_lock(periph);
2284 	softc->flags |= DA_FLAG_SCTX_INIT;
2285 	cam_periph_unlock(periph);
2286 	softc->sysctl_tree = SYSCTL_ADD_NODE_WITH_LABEL(&softc->sysctl_ctx,
2287 		SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
2288 		CTLFLAG_RD | CTLFLAG_MPSAFE, 0, tmpstr, "device_index");
2289 	if (softc->sysctl_tree == NULL) {
2290 		printf("dasysctlinit: unable to allocate sysctl tree\n");
2291 		da_periph_release(periph, DA_REF_SYSCTL);
2292 		return;
2293 	}
2294 
2295 	/*
2296 	 * Now register the sysctl handler, so the user can change the value on
2297 	 * the fly.
2298 	 */
2299 	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2300 		OID_AUTO, "delete_method",
2301 		CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
2302 		softc, 0, dadeletemethodsysctl, "A",
2303 		"BIO_DELETE execution method");
2304 	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2305 		OID_AUTO, "delete_max",
2306 		CTLTYPE_U64 | CTLFLAG_RW | CTLFLAG_MPSAFE,
2307 		softc, 0, dadeletemaxsysctl, "Q",
2308 		"Maximum BIO_DELETE size");
2309 	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2310 		OID_AUTO, "minimum_cmd_size",
2311 		CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
2312 		&softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
2313 		"Minimum CDB size");
2314 	SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
2315 		SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
2316 		"trim_count", CTLFLAG_RD, &softc->trim_count,
2317 		"Total number of unmap/dsm commands sent");
2318 	SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
2319 		SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
2320 		"trim_ranges", CTLFLAG_RD, &softc->trim_ranges,
2321 		"Total number of ranges in unmap/dsm commands");
2322 	SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
2323 		SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
2324 		"trim_lbas", CTLFLAG_RD, &softc->trim_lbas,
2325 		"Total lbas in the unmap/dsm commands sent");
2326 
2327 	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2328 		OID_AUTO, "zone_mode",
2329 		CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
2330 		softc, 0, dazonemodesysctl, "A",
2331 		"Zone Mode");
2332 	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2333 		OID_AUTO, "zone_support",
2334 		CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
2335 		softc, 0, dazonesupsysctl, "A",
2336 		"Zone Support");
2337 	SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
2338 		SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
2339 		"optimal_seq_zones", CTLFLAG_RD, &softc->optimal_seq_zones,
2340 		"Optimal Number of Open Sequential Write Preferred Zones");
2341 	SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
2342 		SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
2343 		"optimal_nonseq_zones", CTLFLAG_RD,
2344 		&softc->optimal_nonseq_zones,
2345 		"Optimal Number of Non-Sequentially Written Sequential Write "
2346 		"Preferred Zones");
2347 	SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
2348 		SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
2349 		"max_seq_zones", CTLFLAG_RD, &softc->max_seq_zones,
2350 		"Maximum Number of Open Sequential Write Required Zones");
2351 
2352 	SYSCTL_ADD_INT(&softc->sysctl_ctx,
2353 		       SYSCTL_CHILDREN(softc->sysctl_tree),
2354 		       OID_AUTO,
2355 		       "error_inject",
2356 		       CTLFLAG_RW,
2357 		       &softc->error_inject,
2358 		       0,
2359 		       "error_inject leaf");
2360 
2361 	SYSCTL_ADD_INT(&softc->sysctl_ctx,
2362 		       SYSCTL_CHILDREN(softc->sysctl_tree),
2363 		       OID_AUTO,
2364 		       "p_type",
2365 		       CTLFLAG_RD,
2366 		       &softc->p_type,
2367 		       0,
2368 		       "DIF protection type");
2369 
2370 	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2371 	    OID_AUTO, "flags", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
2372 	    softc, 0, daflagssysctl, "A",
2373 	    "Flags for drive");
2374 	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2375 	    OID_AUTO, "rotating", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
2376 	    &softc->flags, (u_int)DA_FLAG_ROTATING, dabitsysctl, "I",
2377 	    "Rotating media *DEPRECATED* gone in FreeBSD 15");
2378 	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2379 	    OID_AUTO, "unmapped_io", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
2380 	    &softc->flags, (u_int)DA_FLAG_UNMAPPEDIO, dabitsysctl, "I",
2381 	    "Unmapped I/O support *DEPRECATED* gone in FreeBSD 15");
2382 
2383 #ifdef CAM_TEST_FAILURE
2384 	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2385 		OID_AUTO, "invalidate", CTLTYPE_U64 | CTLFLAG_RW | CTLFLAG_MPSAFE,
2386 		periph, 0, cam_periph_invalidate_sysctl, "I",
2387 		"Write 1 to invalidate the drive immediately");
2388 #endif
2389 
2390 	/*
2391 	 * Add some addressing info.
2392 	 */
2393 	memset(&cts, 0, sizeof (cts));
2394 	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
2395 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
2396 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
2397 	cam_periph_lock(periph);
2398 	xpt_action((union ccb *)&cts);
2399 	cam_periph_unlock(periph);
2400 	if (cts.ccb_h.status != CAM_REQ_CMP) {
2401 		da_periph_release(periph, DA_REF_SYSCTL);
2402 		return;
2403 	}
2404 	if (cts.protocol == PROTO_SCSI && cts.transport == XPORT_FC) {
2405 		struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
2406 		if (fc->valid & CTS_FC_VALID_WWPN) {
2407 			softc->wwpn = fc->wwpn;
2408 			SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
2409 			    SYSCTL_CHILDREN(softc->sysctl_tree),
2410 			    OID_AUTO, "wwpn", CTLFLAG_RD,
2411 			    &softc->wwpn, "World Wide Port Name");
2412 		}
2413 	}
2414 
2415 #ifdef CAM_IO_STATS
2416 	/*
2417 	 * Now add some useful stats.
2418 	 * XXX These should live in cam_periph and be common to all periphs
2419 	 */
2420 	softc->sysctl_stats_tree = SYSCTL_ADD_NODE(&softc->sysctl_stats_ctx,
2421 	    SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, "stats",
2422 	    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "Statistics");
2423 	SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
2424 		       SYSCTL_CHILDREN(softc->sysctl_stats_tree),
2425 		       OID_AUTO,
2426 		       "errors",
2427 		       CTLFLAG_RD,
2428 		       &softc->errors,
2429 		       0,
2430 		       "Transport errors reported by the SIM");
2431 	SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
2432 		       SYSCTL_CHILDREN(softc->sysctl_stats_tree),
2433 		       OID_AUTO,
2434 		       "timeouts",
2435 		       CTLFLAG_RD,
2436 		       &softc->timeouts,
2437 		       0,
2438 		       "Device timeouts reported by the SIM");
2439 	SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
2440 		       SYSCTL_CHILDREN(softc->sysctl_stats_tree),
2441 		       OID_AUTO,
2442 		       "pack_invalidations",
2443 		       CTLFLAG_RD,
2444 		       &softc->invalidations,
2445 		       0,
2446 		       "Device pack invalidations");
2447 #endif
2448 
2449 	cam_iosched_sysctl_init(softc->cam_iosched, &softc->sysctl_ctx,
2450 	    softc->sysctl_tree);
2451 
2452 	da_periph_release(periph, DA_REF_SYSCTL);
2453 }
2454 
2455 static int
dadeletemaxsysctl(SYSCTL_HANDLER_ARGS)2456 dadeletemaxsysctl(SYSCTL_HANDLER_ARGS)
2457 {
2458 	int error;
2459 	uint64_t value;
2460 	struct da_softc *softc;
2461 
2462 	softc = (struct da_softc *)arg1;
2463 
2464 	value = softc->disk->d_delmaxsize;
2465 	error = sysctl_handle_64(oidp, &value, 0, req);
2466 	if ((error != 0) || (req->newptr == NULL))
2467 		return (error);
2468 
2469 	/* only accept values smaller than the calculated value */
2470 	if (value > dadeletemaxsize(softc, softc->delete_method)) {
2471 		return (EINVAL);
2472 	}
2473 	softc->disk->d_delmaxsize = value;
2474 
2475 	return (0);
2476 }
2477 
2478 static int
dacmdsizesysctl(SYSCTL_HANDLER_ARGS)2479 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
2480 {
2481 	int error, value;
2482 
2483 	value = *(int *)arg1;
2484 
2485 	error = sysctl_handle_int(oidp, &value, 0, req);
2486 
2487 	if ((error != 0)
2488 	 || (req->newptr == NULL))
2489 		return (error);
2490 
2491 	/*
2492 	 * Acceptable values here are 6, 10, 12 or 16.
2493 	 */
2494 	if (value < 6)
2495 		value = 6;
2496 	else if ((value > 6)
2497 	      && (value <= 10))
2498 		value = 10;
2499 	else if ((value > 10)
2500 	      && (value <= 12))
2501 		value = 12;
2502 	else if (value > 12)
2503 		value = 16;
2504 
2505 	*(int *)arg1 = value;
2506 
2507 	return (0);
2508 }
2509 
2510 static int
dasysctlsofttimeout(SYSCTL_HANDLER_ARGS)2511 dasysctlsofttimeout(SYSCTL_HANDLER_ARGS)
2512 {
2513 	sbintime_t value;
2514 	int error;
2515 
2516 	value = da_default_softtimeout / SBT_1MS;
2517 
2518 	error = sysctl_handle_int(oidp, (int *)&value, 0, req);
2519 	if ((error != 0) || (req->newptr == NULL))
2520 		return (error);
2521 
2522 	/* XXX Should clip this to a reasonable level */
2523 	if (value > da_default_timeout * 1000)
2524 		return (EINVAL);
2525 
2526 	da_default_softtimeout = value * SBT_1MS;
2527 	return (0);
2528 }
2529 
2530 static void
dadeletemethodset(struct da_softc * softc,da_delete_methods delete_method)2531 dadeletemethodset(struct da_softc *softc, da_delete_methods delete_method)
2532 {
2533 
2534 	softc->delete_method = delete_method;
2535 	softc->disk->d_delmaxsize = dadeletemaxsize(softc, delete_method);
2536 	softc->delete_func = da_delete_functions[delete_method];
2537 
2538 	if (softc->delete_method > DA_DELETE_DISABLE)
2539 		softc->disk->d_flags |= DISKFLAG_CANDELETE;
2540 	else
2541 		softc->disk->d_flags &= ~DISKFLAG_CANDELETE;
2542 }
2543 
2544 static off_t
dadeletemaxsize(struct da_softc * softc,da_delete_methods delete_method)2545 dadeletemaxsize(struct da_softc *softc, da_delete_methods delete_method)
2546 {
2547 	off_t sectors;
2548 
2549 	switch(delete_method) {
2550 	case DA_DELETE_UNMAP:
2551 		sectors = (off_t)softc->unmap_max_lba;
2552 		break;
2553 	case DA_DELETE_ATA_TRIM:
2554 		sectors = (off_t)ATA_DSM_RANGE_MAX * softc->trim_max_ranges;
2555 		break;
2556 	case DA_DELETE_WS16:
2557 		sectors = omin(softc->ws_max_blks, WS16_MAX_BLKS);
2558 		break;
2559 	case DA_DELETE_ZERO:
2560 	case DA_DELETE_WS10:
2561 		sectors = omin(softc->ws_max_blks, WS10_MAX_BLKS);
2562 		break;
2563 	default:
2564 		return 0;
2565 	}
2566 
2567 	return (off_t)softc->params.secsize *
2568 	    omin(sectors, softc->params.sectors);
2569 }
2570 
2571 static void
daprobedone(struct cam_periph * periph,union ccb * ccb)2572 daprobedone(struct cam_periph *periph, union ccb *ccb)
2573 {
2574 	struct da_softc *softc;
2575 
2576 	softc = (struct da_softc *)periph->softc;
2577 
2578 	cam_periph_assert(periph, MA_OWNED);
2579 
2580 	dadeletemethodchoose(softc, DA_DELETE_NONE);
2581 
2582 	if (bootverbose && (softc->flags & DA_FLAG_ANNOUNCED) == 0) {
2583 		char buf[80];
2584 		int i, sep;
2585 
2586 		snprintf(buf, sizeof(buf), "Delete methods: <");
2587 		sep = 0;
2588 		for (i = 0; i <= DA_DELETE_MAX; i++) {
2589 			if ((softc->delete_available & (1 << i)) == 0 &&
2590 			    i != softc->delete_method)
2591 				continue;
2592 			if (sep)
2593 				strlcat(buf, ",", sizeof(buf));
2594 			strlcat(buf, da_delete_method_names[i],
2595 			    sizeof(buf));
2596 			if (i == softc->delete_method)
2597 				strlcat(buf, "(*)", sizeof(buf));
2598 			sep = 1;
2599 		}
2600 		strlcat(buf, ">", sizeof(buf));
2601 		printf("%s%d: %s\n", periph->periph_name,
2602 		    periph->unit_number, buf);
2603 	}
2604 	if ((softc->disk->d_flags & DISKFLAG_WRITE_PROTECT) != 0 &&
2605 	    (softc->flags & DA_FLAG_ANNOUNCED) == 0) {
2606 		printf("%s%d: Write Protected\n", periph->periph_name,
2607 		    periph->unit_number);
2608 	}
2609 
2610 	/*
2611 	 * Since our peripheral may be invalidated by an error
2612 	 * above or an external event, we must release our CCB
2613 	 * before releasing the probe lock on the peripheral.
2614 	 * The peripheral will only go away once the last lock
2615 	 * is removed, and we need it around for the CCB release
2616 	 * operation.
2617 	 */
2618 	xpt_release_ccb(ccb);
2619 	softc->state = DA_STATE_NORMAL;
2620 	softc->flags |= DA_FLAG_PROBED;
2621 	daschedule(periph);
2622 	wakeup(&softc->disk->d_mediasize);
2623 	if ((softc->flags & DA_FLAG_ANNOUNCED) == 0) {
2624 		softc->flags |= DA_FLAG_ANNOUNCED;
2625 
2626 		/*
2627 		 * We'll release this reference once GEOM calls us back via
2628 		 * dadiskgonecb(), telling us that our provider has been freed.
2629 		 */
2630 		if (da_periph_acquire(periph, DA_REF_GEOM) == 0)
2631 			disk_create(softc->disk, DISK_VERSION);
2632 
2633 		cam_periph_release_boot(periph);
2634 	}
2635 	da_periph_release_locked(periph, DA_REF_REPROBE);
2636 }
2637 
2638 static void
dadeletemethodchoose(struct da_softc * softc,da_delete_methods default_method)2639 dadeletemethodchoose(struct da_softc *softc, da_delete_methods default_method)
2640 {
2641 	int i, methods;
2642 
2643 	/* If available, prefer the method requested by user. */
2644 	i = softc->delete_method_pref;
2645 	methods = softc->delete_available | (1 << DA_DELETE_DISABLE);
2646 	if (methods & (1 << i)) {
2647 		dadeletemethodset(softc, i);
2648 		return;
2649 	}
2650 
2651 	/* Use the pre-defined order to choose the best performing delete. */
2652 	for (i = DA_DELETE_MIN; i <= DA_DELETE_MAX; i++) {
2653 		if (i == DA_DELETE_ZERO)
2654 			continue;
2655 		if (softc->delete_available & (1 << i)) {
2656 			dadeletemethodset(softc, i);
2657 			return;
2658 		}
2659 	}
2660 
2661 	/* Fallback to default. */
2662 	dadeletemethodset(softc, default_method);
2663 }
2664 
2665 static int
dabitsysctl(SYSCTL_HANDLER_ARGS)2666 dabitsysctl(SYSCTL_HANDLER_ARGS)
2667 {
2668 	u_int *flags = arg1;
2669 	u_int test = arg2;
2670 	int tmpout, error;
2671 
2672 	tmpout = !!(*flags & test);
2673 	error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
2674 	if (error || !req->newptr)
2675 		return (error);
2676 
2677 	return (EPERM);
2678 }
2679 
2680 static int
daflagssysctl(SYSCTL_HANDLER_ARGS)2681 daflagssysctl(SYSCTL_HANDLER_ARGS)
2682 {
2683 	struct sbuf sbuf;
2684 	struct da_softc *softc = arg1;
2685 	int error;
2686 
2687 	sbuf_new_for_sysctl(&sbuf, NULL, 0, req);
2688 	if (softc->flags != 0)
2689 		sbuf_printf(&sbuf, "0x%b", (unsigned)softc->flags, DA_FLAG_STRING);
2690 	else
2691 		sbuf_putc(&sbuf, '0');
2692 	error = sbuf_finish(&sbuf);
2693 	sbuf_delete(&sbuf);
2694 
2695 	return (error);
2696 }
2697 
2698 static int
dadeletemethodsysctl(SYSCTL_HANDLER_ARGS)2699 dadeletemethodsysctl(SYSCTL_HANDLER_ARGS)
2700 {
2701 	char buf[16];
2702 	const char *p;
2703 	struct da_softc *softc;
2704 	int i, error, value;
2705 
2706 	softc = (struct da_softc *)arg1;
2707 
2708 	value = softc->delete_method;
2709 	if (value < 0 || value > DA_DELETE_MAX)
2710 		p = "UNKNOWN";
2711 	else
2712 		p = da_delete_method_names[value];
2713 	strncpy(buf, p, sizeof(buf));
2714 	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
2715 	if (error != 0 || req->newptr == NULL)
2716 		return (error);
2717 	for (i = 0; i <= DA_DELETE_MAX; i++) {
2718 		if (strcmp(buf, da_delete_method_names[i]) == 0)
2719 			break;
2720 	}
2721 	if (i > DA_DELETE_MAX)
2722 		return (EINVAL);
2723 	softc->delete_method_pref = i;
2724 	dadeletemethodchoose(softc, DA_DELETE_NONE);
2725 	return (0);
2726 }
2727 
2728 static int
dazonemodesysctl(SYSCTL_HANDLER_ARGS)2729 dazonemodesysctl(SYSCTL_HANDLER_ARGS)
2730 {
2731 	char tmpbuf[40];
2732 	struct da_softc *softc;
2733 	int error;
2734 
2735 	softc = (struct da_softc *)arg1;
2736 
2737 	switch (softc->zone_mode) {
2738 	case DA_ZONE_DRIVE_MANAGED:
2739 		snprintf(tmpbuf, sizeof(tmpbuf), "Drive Managed");
2740 		break;
2741 	case DA_ZONE_HOST_AWARE:
2742 		snprintf(tmpbuf, sizeof(tmpbuf), "Host Aware");
2743 		break;
2744 	case DA_ZONE_HOST_MANAGED:
2745 		snprintf(tmpbuf, sizeof(tmpbuf), "Host Managed");
2746 		break;
2747 	case DA_ZONE_NONE:
2748 	default:
2749 		snprintf(tmpbuf, sizeof(tmpbuf), "Not Zoned");
2750 		break;
2751 	}
2752 
2753 	error = sysctl_handle_string(oidp, tmpbuf, sizeof(tmpbuf), req);
2754 
2755 	return (error);
2756 }
2757 
2758 static int
dazonesupsysctl(SYSCTL_HANDLER_ARGS)2759 dazonesupsysctl(SYSCTL_HANDLER_ARGS)
2760 {
2761 	struct da_softc *softc;
2762 	struct sbuf sb;
2763 	int error, first;
2764 	unsigned int i;
2765 
2766 	softc = (struct da_softc *)arg1;
2767 
2768 	first = 1;
2769 	sbuf_new_for_sysctl(&sb, NULL, 0, req);
2770 
2771 	for (i = 0; i < sizeof(da_zone_desc_table) /
2772 	     sizeof(da_zone_desc_table[0]); i++) {
2773 		if (softc->zone_flags & da_zone_desc_table[i].value) {
2774 			if (first == 0)
2775 				sbuf_cat(&sb, ", ");
2776 			else
2777 				first = 0;
2778 			sbuf_cat(&sb, da_zone_desc_table[i].desc);
2779 		}
2780 	}
2781 
2782 	if (first == 1)
2783 		sbuf_cat(&sb, "None");
2784 
2785 	error = sbuf_finish(&sb);
2786 	sbuf_delete(&sb);
2787 	return (error);
2788 }
2789 
2790 static cam_status
daregister(struct cam_periph * periph,void * arg)2791 daregister(struct cam_periph *periph, void *arg)
2792 {
2793 	struct da_softc *softc;
2794 	struct ccb_pathinq cpi;
2795 	struct ccb_getdev *cgd;
2796 	char tmpstr[80];
2797 	caddr_t match;
2798 	int quirks;
2799 
2800 	cgd = (struct ccb_getdev *)arg;
2801 	if (cgd == NULL) {
2802 		printf("daregister: no getdev CCB, can't register device\n");
2803 		return(CAM_REQ_CMP_ERR);
2804 	}
2805 
2806 	softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF,
2807 	    M_NOWAIT|M_ZERO);
2808 
2809 	if (softc == NULL) {
2810 		printf("daregister: Unable to probe new device. "
2811 		       "Unable to allocate softc\n");
2812 		return(CAM_REQ_CMP_ERR);
2813 	}
2814 
2815 	LIST_INIT(&softc->pending_ccbs);
2816 	softc->state = DA_STATE_PROBE_WP;
2817 	bioq_init(&softc->delete_run_queue);
2818 	if (SID_IS_REMOVABLE(&cgd->inq_data))
2819 		softc->flags |= DA_FLAG_PACK_REMOVABLE;
2820 	softc->unmap_max_ranges = UNMAP_MAX_RANGES;
2821 	softc->unmap_max_lba = UNMAP_RANGE_MAX;
2822 	softc->unmap_gran = 0;
2823 	softc->unmap_gran_align = 0;
2824 	softc->ws_max_blks = WS16_MAX_BLKS;
2825 	softc->trim_max_ranges = ATA_TRIM_MAX_RANGES;
2826 	softc->flags |= DA_FLAG_ROTATING;
2827 
2828 	periph->softc = softc;
2829 
2830 	/*
2831 	 * See if this device has any quirks.
2832 	 */
2833 	match = cam_quirkmatch((caddr_t)&cgd->inq_data,
2834 			       (caddr_t)da_quirk_table,
2835 			       nitems(da_quirk_table),
2836 			       sizeof(*da_quirk_table), scsi_inquiry_match);
2837 
2838 	if (match != NULL)
2839 		softc->quirks = ((struct da_quirk_entry *)match)->quirks;
2840 	else
2841 		softc->quirks = DA_Q_NONE;
2842 
2843 	/* Check if the SIM does not want 6 byte commands */
2844 	xpt_path_inq(&cpi, periph->path);
2845 	if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
2846 		softc->quirks |= DA_Q_NO_6_BYTE;
2847 
2848 	/* Override quirks if tunable is set */
2849 	snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.quirks",
2850 		 periph->unit_number);
2851 	quirks = softc->quirks;
2852 	TUNABLE_INT_FETCH(tmpstr, &quirks);
2853 	softc->quirks = quirks;
2854 
2855 	if (SID_TYPE(&cgd->inq_data) == T_ZBC_HM)
2856 		softc->zone_mode = DA_ZONE_HOST_MANAGED;
2857 	else if (softc->quirks & DA_Q_SMR_DM)
2858 		softc->zone_mode = DA_ZONE_DRIVE_MANAGED;
2859 	else
2860 		softc->zone_mode = DA_ZONE_NONE;
2861 
2862 	if (softc->zone_mode != DA_ZONE_NONE) {
2863 		if (scsi_vpd_supported_page(periph, SVPD_ATA_INFORMATION)) {
2864 			if (scsi_vpd_supported_page(periph, SVPD_ZONED_BDC))
2865 				softc->zone_interface = DA_ZONE_IF_ATA_SAT;
2866 			else
2867 				softc->zone_interface = DA_ZONE_IF_ATA_PASS;
2868 		} else
2869 			softc->zone_interface = DA_ZONE_IF_SCSI;
2870 	}
2871 
2872 	TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
2873 
2874 	/*
2875 	 * Let XPT know we can use UMA-allocated CCBs.
2876 	 */
2877 	if (da_enable_uma_ccbs) {
2878 		KASSERT(da_ccb_zone != NULL,
2879 		    ("%s: NULL da_ccb_zone", __func__));
2880 		periph->ccb_zone = da_ccb_zone;
2881 	}
2882 
2883 	/*
2884 	 * Take a reference on the periph while dastart is called to finish the
2885 	 * probe.  The reference will be dropped in dadone at the end of probe.
2886 	 */
2887 	(void)da_periph_acquire(periph, DA_REF_REPROBE);
2888 
2889 	/*
2890 	 * Schedule a periodic event to occasionally send an
2891 	 * ordered tag to a device.
2892 	 */
2893 	callout_init_mtx(&softc->sendordered_c, cam_periph_mtx(periph), 0);
2894 	callout_reset_sbt(&softc->sendordered_c,
2895 	    SBT_1S / DA_ORDEREDTAG_INTERVAL * da_default_timeout, 0,
2896 	    dasendorderedtag, periph, C_PREL(1));
2897 
2898 	cam_periph_unlock(periph);
2899 	/*
2900 	 * RBC devices don't have to support READ(6), only READ(10).
2901 	 */
2902 	if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
2903 		softc->minimum_cmd_size = 10;
2904 	else
2905 		softc->minimum_cmd_size = 6;
2906 
2907 	/*
2908 	 * Load the user's default, if any.
2909 	 */
2910 	snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
2911 		 periph->unit_number);
2912 	TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
2913 
2914 	/*
2915 	 * 6, 10, 12 and 16 are the currently permissible values.
2916 	 */
2917 	if (softc->minimum_cmd_size > 12)
2918 		softc->minimum_cmd_size = 16;
2919 	else if (softc->minimum_cmd_size > 10)
2920 		softc->minimum_cmd_size = 12;
2921 	else if (softc->minimum_cmd_size > 6)
2922 		softc->minimum_cmd_size = 10;
2923 	else
2924 		softc->minimum_cmd_size = 6;
2925 
2926 	/* On first PROBE_WP request all more pages, then adjust. */
2927 	softc->mode_page = SMS_ALL_PAGES_PAGE;
2928 
2929 	/* Predict whether device may support READ CAPACITY(16). */
2930 	if (SID_ANSI_REV(&cgd->inq_data) >= SCSI_REV_SPC3 &&
2931 	    (softc->quirks & DA_Q_NO_RC16) == 0) {
2932 		softc->flags |= DA_FLAG_CAN_RC16;
2933 	}
2934 
2935 	/*
2936 	 * Register this media as a disk.
2937 	 */
2938 	softc->disk = disk_alloc();
2939 	softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
2940 			  periph->unit_number, 0,
2941 			  DEVSTAT_BS_UNAVAILABLE,
2942 			  SID_TYPE(&cgd->inq_data) |
2943 			  XPORT_DEVSTAT_TYPE(cpi.transport),
2944 			  DEVSTAT_PRIORITY_DISK);
2945 	softc->disk->d_open = daopen;
2946 	softc->disk->d_close = daclose;
2947 	softc->disk->d_strategy = dastrategy;
2948 	if (cam_sim_pollable(periph->sim))
2949 		softc->disk->d_dump = dadump;
2950 	softc->disk->d_getattr = dagetattr;
2951 	softc->disk->d_gone = dadiskgonecb;
2952 	softc->disk->d_name = "da";
2953 	softc->disk->d_drv1 = periph;
2954 	if (cpi.maxio == 0)
2955 		softc->maxio = DFLTPHYS;	/* traditional default */
2956 	else if (cpi.maxio > maxphys)
2957 		softc->maxio = maxphys;		/* for safety */
2958 	else
2959 		softc->maxio = cpi.maxio;
2960 	if (softc->quirks & DA_Q_128KB)
2961 		softc->maxio = min(softc->maxio, 128 * 1024);
2962 	softc->disk->d_maxsize = softc->maxio;
2963 	softc->disk->d_unit = periph->unit_number;
2964 	softc->disk->d_flags = DISKFLAG_DIRECT_COMPLETION | DISKFLAG_CANZONE;
2965 	if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0)
2966 		softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
2967 	if ((cpi.hba_misc & PIM_UNMAPPED) != 0) {
2968 		softc->flags |= DA_FLAG_UNMAPPEDIO;
2969 		softc->disk->d_flags |= DISKFLAG_UNMAPPED_BIO;
2970 	}
2971 	cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
2972 	    sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
2973 	strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
2974 	cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
2975 	    cgd->inq_data.product, sizeof(cgd->inq_data.product),
2976 	    sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
2977 	softc->disk->d_hba_vendor = cpi.hba_vendor;
2978 	softc->disk->d_hba_device = cpi.hba_device;
2979 	softc->disk->d_hba_subvendor = cpi.hba_subvendor;
2980 	softc->disk->d_hba_subdevice = cpi.hba_subdevice;
2981 	snprintf(softc->disk->d_attachment, sizeof(softc->disk->d_attachment),
2982 	    "%s%d", cpi.dev_name, cpi.unit_number);
2983 
2984 	if (cam_iosched_init(&softc->cam_iosched, periph, softc->disk,
2985 	    daschedule) != 0) {
2986 		printf("daregister: Unable to probe new device. "
2987 		       "Unable to allocate iosched memory\n");
2988 		free(softc, M_DEVBUF);
2989 		return(CAM_REQ_CMP_ERR);
2990 	}
2991 
2992 	/*
2993 	 * Add async callbacks for events of interest.
2994 	 * I don't bother checking if this fails as,
2995 	 * in most cases, the system will function just
2996 	 * fine without them and the only alternative
2997 	 * would be to not attach the device on failure.
2998 	 */
2999 	cam_periph_lock(periph);
3000 	xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
3001 	    AC_ADVINFO_CHANGED | AC_SCSI_AEN | AC_UNIT_ATTENTION |
3002 	    AC_INQ_CHANGED, daasync, periph, periph->path);
3003 
3004 	/*
3005 	 * Schedule a periodic media polling events.
3006 	 */
3007 	callout_init_mtx(&softc->mediapoll_c, cam_periph_mtx(periph), 0);
3008 	if ((softc->flags & DA_FLAG_PACK_REMOVABLE) &&
3009 	    (cgd->inq_flags & SID_AEN) == 0 &&
3010 	    da_poll_period != 0) {
3011 		callout_reset_sbt(&softc->mediapoll_c, da_poll_period * SBT_1S,
3012 		    0, damediapoll, periph, C_PREL(1));
3013 	}
3014 
3015 	/* Released after probe when disk_create() call pass it to GEOM. */
3016 	cam_periph_hold_boot(periph);
3017 
3018 	xpt_schedule(periph, CAM_PRIORITY_DEV);
3019 	return(CAM_REQ_CMP);
3020 }
3021 
3022 static int
da_zone_bio_to_scsi(int disk_zone_cmd)3023 da_zone_bio_to_scsi(int disk_zone_cmd)
3024 {
3025 	switch (disk_zone_cmd) {
3026 	case DISK_ZONE_OPEN:
3027 		return ZBC_OUT_SA_OPEN;
3028 	case DISK_ZONE_CLOSE:
3029 		return ZBC_OUT_SA_CLOSE;
3030 	case DISK_ZONE_FINISH:
3031 		return ZBC_OUT_SA_FINISH;
3032 	case DISK_ZONE_RWP:
3033 		return ZBC_OUT_SA_RWP;
3034 	}
3035 
3036 	return -1;
3037 }
3038 
3039 static int
da_zone_cmd(struct cam_periph * periph,union ccb * ccb,struct bio * bp,int * queue_ccb)3040 da_zone_cmd(struct cam_periph *periph, union ccb *ccb, struct bio *bp,
3041 	    int *queue_ccb)
3042 {
3043 	struct da_softc *softc;
3044 	int error;
3045 
3046 	error = 0;
3047 
3048 	if (bp->bio_cmd != BIO_ZONE) {
3049 		error = EINVAL;
3050 		goto bailout;
3051 	}
3052 
3053 	softc = periph->softc;
3054 
3055 	switch (bp->bio_zone.zone_cmd) {
3056 	case DISK_ZONE_OPEN:
3057 	case DISK_ZONE_CLOSE:
3058 	case DISK_ZONE_FINISH:
3059 	case DISK_ZONE_RWP: {
3060 		int zone_flags;
3061 		int zone_sa;
3062 		uint64_t lba;
3063 
3064 		zone_sa = da_zone_bio_to_scsi(bp->bio_zone.zone_cmd);
3065 		if (zone_sa == -1) {
3066 			xpt_print(periph->path, "Cannot translate zone "
3067 			    "cmd %#x to SCSI\n", bp->bio_zone.zone_cmd);
3068 			error = EINVAL;
3069 			goto bailout;
3070 		}
3071 
3072 		zone_flags = 0;
3073 		lba = bp->bio_zone.zone_params.rwp.id;
3074 
3075 		if (bp->bio_zone.zone_params.rwp.flags &
3076 		    DISK_ZONE_RWP_FLAG_ALL)
3077 			zone_flags |= ZBC_OUT_ALL;
3078 
3079 		if (softc->zone_interface != DA_ZONE_IF_ATA_PASS) {
3080 			scsi_zbc_out(&ccb->csio,
3081 				     /*retries*/ da_retry_count,
3082 				     /*cbfcnp*/ dadone,
3083 				     /*tag_action*/ MSG_SIMPLE_Q_TAG,
3084 				     /*service_action*/ zone_sa,
3085 				     /*zone_id*/ lba,
3086 				     /*zone_flags*/ zone_flags,
3087 				     /*data_ptr*/ NULL,
3088 				     /*dxfer_len*/ 0,
3089 				     /*sense_len*/ SSD_FULL_SIZE,
3090 				     /*timeout*/ da_default_timeout * 1000);
3091 		} else {
3092 			/*
3093 			 * Note that in this case, even though we can
3094 			 * technically use NCQ, we don't bother for several
3095 			 * reasons:
3096 			 * 1. It hasn't been tested on a SAT layer that
3097 			 *    supports it.  This is new as of SAT-4.
3098 			 * 2. Even when there is a SAT layer that supports
3099 			 *    it, that SAT layer will also probably support
3100 			 *    ZBC -> ZAC translation, since they are both
3101 			 *    in the SAT-4 spec.
3102 			 * 3. Translation will likely be preferable to ATA
3103 			 *    passthrough.  LSI / Avago at least single
3104 			 *    steps ATA passthrough commands in the HBA,
3105 			 *    regardless of protocol, so unless that
3106 			 *    changes, there is a performance penalty for
3107 			 *    doing ATA passthrough no matter whether
3108 			 *    you're using NCQ/FPDMA, DMA or PIO.
3109 			 * 4. It requires a 32-byte CDB, which at least at
3110 			 *    this point in CAM requires a CDB pointer, which
3111 			 *    would require us to allocate an additional bit
3112 			 *    of storage separate from the CCB.
3113 			 */
3114 			error = scsi_ata_zac_mgmt_out(&ccb->csio,
3115 			    /*retries*/ da_retry_count,
3116 			    /*cbfcnp*/ dadone,
3117 			    /*tag_action*/ MSG_SIMPLE_Q_TAG,
3118 			    /*use_ncq*/ 0,
3119 			    /*zm_action*/ zone_sa,
3120 			    /*zone_id*/ lba,
3121 			    /*zone_flags*/ zone_flags,
3122 			    /*data_ptr*/ NULL,
3123 			    /*dxfer_len*/ 0,
3124 			    /*cdb_storage*/ NULL,
3125 			    /*cdb_storage_len*/ 0,
3126 			    /*sense_len*/ SSD_FULL_SIZE,
3127 			    /*timeout*/ da_default_timeout * 1000);
3128 			if (error != 0) {
3129 				error = EINVAL;
3130 				xpt_print(periph->path,
3131 				    "scsi_ata_zac_mgmt_out() returned an "
3132 				    "error!");
3133 				goto bailout;
3134 			}
3135 		}
3136 		*queue_ccb = 1;
3137 
3138 		break;
3139 	}
3140 	case DISK_ZONE_REPORT_ZONES: {
3141 		uint8_t *rz_ptr;
3142 		uint32_t num_entries, alloc_size;
3143 		struct disk_zone_report *rep;
3144 
3145 		rep = &bp->bio_zone.zone_params.report;
3146 
3147 		num_entries = rep->entries_allocated;
3148 		if (num_entries == 0) {
3149 			xpt_print(periph->path, "No entries allocated for "
3150 			    "Report Zones request\n");
3151 			error = EINVAL;
3152 			goto bailout;
3153 		}
3154 		alloc_size = sizeof(struct scsi_report_zones_hdr) +
3155 		    (sizeof(struct scsi_report_zones_desc) * num_entries);
3156 		alloc_size = min(alloc_size, softc->disk->d_maxsize);
3157 		rz_ptr = malloc(alloc_size, M_SCSIDA, M_NOWAIT | M_ZERO);
3158 		if (rz_ptr == NULL) {
3159 			xpt_print(periph->path, "Unable to allocate memory "
3160 			   "for Report Zones request\n");
3161 			error = ENOMEM;
3162 			goto bailout;
3163 		}
3164 
3165 		if (softc->zone_interface != DA_ZONE_IF_ATA_PASS) {
3166 			scsi_zbc_in(&ccb->csio,
3167 				    /*retries*/ da_retry_count,
3168 				    /*cbcfnp*/ dadone,
3169 				    /*tag_action*/ MSG_SIMPLE_Q_TAG,
3170 				    /*service_action*/ ZBC_IN_SA_REPORT_ZONES,
3171 				    /*zone_start_lba*/ rep->starting_id,
3172 				    /*zone_options*/ rep->rep_options,
3173 				    /*data_ptr*/ rz_ptr,
3174 				    /*dxfer_len*/ alloc_size,
3175 				    /*sense_len*/ SSD_FULL_SIZE,
3176 				    /*timeout*/ da_default_timeout * 1000);
3177 		} else {
3178 			/*
3179 			 * Note that in this case, even though we can
3180 			 * technically use NCQ, we don't bother for several
3181 			 * reasons:
3182 			 * 1. It hasn't been tested on a SAT layer that
3183 			 *    supports it.  This is new as of SAT-4.
3184 			 * 2. Even when there is a SAT layer that supports
3185 			 *    it, that SAT layer will also probably support
3186 			 *    ZBC -> ZAC translation, since they are both
3187 			 *    in the SAT-4 spec.
3188 			 * 3. Translation will likely be preferable to ATA
3189 			 *    passthrough.  LSI / Avago at least single
3190 			 *    steps ATA passthrough commands in the HBA,
3191 			 *    regardless of protocol, so unless that
3192 			 *    changes, there is a performance penalty for
3193 			 *    doing ATA passthrough no matter whether
3194 			 *    you're using NCQ/FPDMA, DMA or PIO.
3195 			 * 4. It requires a 32-byte CDB, which at least at
3196 			 *    this point in CAM requires a CDB pointer, which
3197 			 *    would require us to allocate an additional bit
3198 			 *    of storage separate from the CCB.
3199 			 */
3200 			error = scsi_ata_zac_mgmt_in(&ccb->csio,
3201 			    /*retries*/ da_retry_count,
3202 			    /*cbcfnp*/ dadone,
3203 			    /*tag_action*/ MSG_SIMPLE_Q_TAG,
3204 			    /*use_ncq*/ 0,
3205 			    /*zm_action*/ ATA_ZM_REPORT_ZONES,
3206 			    /*zone_id*/ rep->starting_id,
3207 			    /*zone_flags*/ rep->rep_options,
3208 			    /*data_ptr*/ rz_ptr,
3209 			    /*dxfer_len*/ alloc_size,
3210 			    /*cdb_storage*/ NULL,
3211 			    /*cdb_storage_len*/ 0,
3212 			    /*sense_len*/ SSD_FULL_SIZE,
3213 			    /*timeout*/ da_default_timeout * 1000);
3214 			if (error != 0) {
3215 				error = EINVAL;
3216 				xpt_print(periph->path,
3217 				    "scsi_ata_zac_mgmt_in() returned an "
3218 				    "error!");
3219 				goto bailout;
3220 			}
3221 		}
3222 
3223 		/*
3224 		 * For BIO_ZONE, this isn't normally needed.  However, it
3225 		 * is used by devstat_end_transaction_bio() to determine
3226 		 * how much data was transferred.
3227 		 */
3228 		/*
3229 		 * XXX KDM we have a problem.  But I'm not sure how to fix
3230 		 * it.  devstat uses bio_bcount - bio_resid to calculate
3231 		 * the amount of data transferred.   The GEOM disk code
3232 		 * uses bio_length - bio_resid to calculate the amount of
3233 		 * data in bio_completed.  We have different structure
3234 		 * sizes above and below the ada(4) driver.  So, if we
3235 		 * use the sizes above, the amount transferred won't be
3236 		 * quite accurate for devstat.  If we use different sizes
3237 		 * for bio_bcount and bio_length (above and below
3238 		 * respectively), then the residual needs to match one or
3239 		 * the other.  Everything is calculated after the bio
3240 		 * leaves the driver, so changing the values around isn't
3241 		 * really an option.  For now, just set the count to the
3242 		 * passed in length.  This means that the calculations
3243 		 * above (e.g. bio_completed) will be correct, but the
3244 		 * amount of data reported to devstat will be slightly
3245 		 * under or overstated.
3246 		 */
3247 		bp->bio_bcount = bp->bio_length;
3248 
3249 		*queue_ccb = 1;
3250 
3251 		break;
3252 	}
3253 	case DISK_ZONE_GET_PARAMS: {
3254 		struct disk_zone_disk_params *params;
3255 
3256 		params = &bp->bio_zone.zone_params.disk_params;
3257 		bzero(params, sizeof(*params));
3258 
3259 		switch (softc->zone_mode) {
3260 		case DA_ZONE_DRIVE_MANAGED:
3261 			params->zone_mode = DISK_ZONE_MODE_DRIVE_MANAGED;
3262 			break;
3263 		case DA_ZONE_HOST_AWARE:
3264 			params->zone_mode = DISK_ZONE_MODE_HOST_AWARE;
3265 			break;
3266 		case DA_ZONE_HOST_MANAGED:
3267 			params->zone_mode = DISK_ZONE_MODE_HOST_MANAGED;
3268 			break;
3269 		default:
3270 		case DA_ZONE_NONE:
3271 			params->zone_mode = DISK_ZONE_MODE_NONE;
3272 			break;
3273 		}
3274 
3275 		if (softc->zone_flags & DA_ZONE_FLAG_URSWRZ)
3276 			params->flags |= DISK_ZONE_DISK_URSWRZ;
3277 
3278 		if (softc->zone_flags & DA_ZONE_FLAG_OPT_SEQ_SET) {
3279 			params->optimal_seq_zones = softc->optimal_seq_zones;
3280 			params->flags |= DISK_ZONE_OPT_SEQ_SET;
3281 		}
3282 
3283 		if (softc->zone_flags & DA_ZONE_FLAG_OPT_NONSEQ_SET) {
3284 			params->optimal_nonseq_zones =
3285 			    softc->optimal_nonseq_zones;
3286 			params->flags |= DISK_ZONE_OPT_NONSEQ_SET;
3287 		}
3288 
3289 		if (softc->zone_flags & DA_ZONE_FLAG_MAX_SEQ_SET) {
3290 			params->max_seq_zones = softc->max_seq_zones;
3291 			params->flags |= DISK_ZONE_MAX_SEQ_SET;
3292 		}
3293 		if (softc->zone_flags & DA_ZONE_FLAG_RZ_SUP)
3294 			params->flags |= DISK_ZONE_RZ_SUP;
3295 
3296 		if (softc->zone_flags & DA_ZONE_FLAG_OPEN_SUP)
3297 			params->flags |= DISK_ZONE_OPEN_SUP;
3298 
3299 		if (softc->zone_flags & DA_ZONE_FLAG_CLOSE_SUP)
3300 			params->flags |= DISK_ZONE_CLOSE_SUP;
3301 
3302 		if (softc->zone_flags & DA_ZONE_FLAG_FINISH_SUP)
3303 			params->flags |= DISK_ZONE_FINISH_SUP;
3304 
3305 		if (softc->zone_flags & DA_ZONE_FLAG_RWP_SUP)
3306 			params->flags |= DISK_ZONE_RWP_SUP;
3307 		break;
3308 	}
3309 	default:
3310 		break;
3311 	}
3312 bailout:
3313 	return (error);
3314 }
3315 
3316 static void
dastart(struct cam_periph * periph,union ccb * start_ccb)3317 dastart(struct cam_periph *periph, union ccb *start_ccb)
3318 {
3319 	struct da_softc *softc;
3320 
3321 	cam_periph_assert(periph, MA_OWNED);
3322 	softc = (struct da_softc *)periph->softc;
3323 
3324 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastart\n"));
3325 
3326 skipstate:
3327 	switch (softc->state) {
3328 	case DA_STATE_NORMAL:
3329 	{
3330 		struct bio *bp;
3331 		uint8_t tag_code;
3332 
3333 more:
3334 		bp = cam_iosched_next_bio(softc->cam_iosched);
3335 		if (bp == NULL) {
3336 			if (cam_iosched_has_work_flags(softc->cam_iosched,
3337 			    DA_WORK_TUR)) {
3338 				softc->flags |= DA_FLAG_TUR_PENDING;
3339 				cam_iosched_clr_work_flags(softc->cam_iosched,
3340 				    DA_WORK_TUR);
3341 				scsi_test_unit_ready(&start_ccb->csio,
3342 				     /*retries*/ da_retry_count,
3343 				     dadone_tur,
3344 				     MSG_SIMPLE_Q_TAG,
3345 				     SSD_FULL_SIZE,
3346 				     da_default_timeout * 1000);
3347 				start_ccb->ccb_h.ccb_bp = NULL;
3348 				start_ccb->ccb_h.ccb_state = DA_CCB_TUR;
3349 				xpt_action(start_ccb);
3350 			} else
3351 				xpt_release_ccb(start_ccb);
3352 			break;
3353 		}
3354 
3355 		if (bp->bio_cmd == BIO_DELETE) {
3356 			if (softc->delete_func != NULL) {
3357 				softc->delete_func(periph, start_ccb, bp);
3358 				goto out;
3359 			} else {
3360 				/*
3361 				 * Not sure this is possible, but failsafe by
3362 				 * lying and saying "sure, done."
3363 				 */
3364 				biofinish(bp, NULL, 0);
3365 				goto more;
3366 			}
3367 		}
3368 
3369 		if (cam_iosched_has_work_flags(softc->cam_iosched,
3370 		    DA_WORK_TUR)) {
3371 			cam_iosched_clr_work_flags(softc->cam_iosched,
3372 			    DA_WORK_TUR);
3373 			da_periph_release_locked(periph, DA_REF_TUR);
3374 		}
3375 
3376 		if ((bp->bio_flags & BIO_ORDERED) != 0 ||
3377 		    (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
3378 			softc->flags &= ~DA_FLAG_NEED_OTAG;
3379 			softc->flags |= DA_FLAG_WAS_OTAG;
3380 			tag_code = MSG_ORDERED_Q_TAG;
3381 		} else {
3382 			tag_code = MSG_SIMPLE_Q_TAG;
3383 		}
3384 
3385 		switch (bp->bio_cmd) {
3386 		case BIO_WRITE:
3387 		case BIO_READ:
3388 		{
3389 			void *data_ptr;
3390 			int rw_op;
3391 
3392 			biotrack(bp, __func__);
3393 
3394 			if (bp->bio_cmd == BIO_WRITE) {
3395 				softc->flags |= DA_FLAG_DIRTY;
3396 				rw_op = SCSI_RW_WRITE;
3397 			} else {
3398 				rw_op = SCSI_RW_READ;
3399 			}
3400 
3401 			data_ptr = bp->bio_data;
3402 			if ((bp->bio_flags & (BIO_UNMAPPED|BIO_VLIST)) != 0) {
3403 				rw_op |= SCSI_RW_BIO;
3404 				data_ptr = bp;
3405 			}
3406 
3407 			scsi_read_write(&start_ccb->csio,
3408 					/*retries*/da_retry_count,
3409 					/*cbfcnp*/dadone,
3410 					/*tag_action*/tag_code,
3411 					rw_op,
3412 					/*byte2*/0,
3413 					softc->minimum_cmd_size,
3414 					/*lba*/bp->bio_pblkno,
3415 					/*block_count*/bp->bio_bcount /
3416 					softc->params.secsize,
3417 					data_ptr,
3418 					/*dxfer_len*/ bp->bio_bcount,
3419 					/*sense_len*/SSD_FULL_SIZE,
3420 					da_default_timeout * 1000);
3421 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
3422 			start_ccb->csio.bio = bp;
3423 #endif
3424 			break;
3425 		}
3426 		case BIO_FLUSH:
3427 			/*
3428 			 * If we don't support sync cache, or the disk
3429 			 * isn't dirty, FLUSH is a no-op.  Use the
3430 			 * allocated CCB for the next bio if one is
3431 			 * available.
3432 			 */
3433 			if ((softc->quirks & DA_Q_NO_SYNC_CACHE) != 0 ||
3434 			    (softc->flags & DA_FLAG_DIRTY) == 0) {
3435 				biodone(bp);
3436 				goto skipstate;
3437 			}
3438 
3439 			/*
3440 			 * BIO_FLUSH doesn't currently communicate
3441 			 * range data, so we synchronize the cache
3442 			 * over the whole disk.
3443 			 */
3444 			scsi_synchronize_cache(&start_ccb->csio,
3445 					       /*retries*/1,
3446 					       /*cbfcnp*/dadone,
3447 					       /*tag_action*/tag_code,
3448 					       /*begin_lba*/0,
3449 					       /*lb_count*/0,
3450 					       SSD_FULL_SIZE,
3451 					       da_default_timeout*1000);
3452 			/*
3453 			 * Clear the dirty flag before sending the command.
3454 			 * Either this sync cache will be successful, or it
3455 			 * will fail after a retry.  If it fails, it is
3456 			 * unlikely to be successful if retried later, so
3457 			 * we'll save ourselves time by just marking the
3458 			 * device clean.
3459 			 */
3460 			softc->flags &= ~DA_FLAG_DIRTY;
3461 			break;
3462 		case BIO_ZONE: {
3463 			int error, queue_ccb;
3464 
3465 			queue_ccb = 0;
3466 
3467 			error = da_zone_cmd(periph, start_ccb, bp, &queue_ccb);
3468 			if ((error != 0)
3469 			 || (queue_ccb == 0)) {
3470 				/*
3471 				 * g_io_deliver will recurisvely call start
3472 				 * routine for ENOMEM, so drop the periph
3473 				 * lock to allow that recursion.
3474 				 */
3475 				if (error == ENOMEM)
3476 					cam_periph_unlock(periph);
3477 				biofinish(bp, NULL, error);
3478 				if (error == ENOMEM)
3479 					cam_periph_lock(periph);
3480 				xpt_release_ccb(start_ccb);
3481 				return;
3482 			}
3483 			break;
3484 		}
3485 		default:
3486 			biofinish(bp, NULL, EOPNOTSUPP);
3487 			xpt_release_ccb(start_ccb);
3488 			return;
3489 		}
3490 		start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
3491 		start_ccb->ccb_h.flags |= CAM_UNLOCKED;
3492 		start_ccb->ccb_h.softtimeout = sbttotv(da_default_softtimeout);
3493 
3494 out:
3495 		LIST_INSERT_HEAD(&softc->pending_ccbs,
3496 				 &start_ccb->ccb_h, periph_links.le);
3497 
3498 		/* We expect a unit attention from this device */
3499 		if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
3500 			start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
3501 			softc->flags &= ~DA_FLAG_RETRY_UA;
3502 		}
3503 
3504 		start_ccb->ccb_h.ccb_bp = bp;
3505 		softc->refcount++;
3506 		cam_periph_unlock(periph);
3507 		xpt_action(start_ccb);
3508 		cam_periph_lock(periph);
3509 
3510 		/* May have more work to do, so ensure we stay scheduled */
3511 		daschedule(periph);
3512 		break;
3513 	}
3514 	case DA_STATE_PROBE_WP:
3515 	{
3516 		void  *mode_buf;
3517 		int    mode_buf_len;
3518 
3519 		if (da_disable_wp_detection || softc->mode_page < 0) {
3520 			if ((softc->flags & DA_FLAG_CAN_RC16) != 0)
3521 				softc->state = DA_STATE_PROBE_RC16;
3522 			else
3523 				softc->state = DA_STATE_PROBE_RC;
3524 			goto skipstate;
3525 		}
3526 		mode_buf_len = 192;
3527 		mode_buf = malloc(mode_buf_len, M_SCSIDA, M_NOWAIT);
3528 		if (mode_buf == NULL) {
3529 			xpt_print(periph->path, "Unable to send mode sense - "
3530 			    "malloc failure\n");
3531 			if ((softc->flags & DA_FLAG_CAN_RC16) != 0)
3532 				softc->state = DA_STATE_PROBE_RC16;
3533 			else
3534 				softc->state = DA_STATE_PROBE_RC;
3535 			goto skipstate;
3536 		}
3537 		scsi_mode_sense_len(&start_ccb->csio,
3538 				    /*retries*/ da_retry_count,
3539 				    /*cbfcnp*/ dadone_probewp,
3540 				    /*tag_action*/ MSG_SIMPLE_Q_TAG,
3541 				    /*dbd*/ FALSE,
3542 				    /*pc*/ SMS_PAGE_CTRL_CURRENT,
3543 				    /*page*/ softc->mode_page,
3544 				    /*param_buf*/ mode_buf,
3545 				    /*param_len*/ mode_buf_len,
3546 				    /*minimum_cmd_size*/ softc->minimum_cmd_size,
3547 				    /*sense_len*/ SSD_FULL_SIZE,
3548 				    /*timeout*/ da_default_timeout * 1000);
3549 		start_ccb->ccb_h.ccb_bp = NULL;
3550 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_WP;
3551 		xpt_action(start_ccb);
3552 		break;
3553 	}
3554 	case DA_STATE_PROBE_RC:
3555 	{
3556 		struct scsi_read_capacity_data *rcap;
3557 
3558 		rcap = (struct scsi_read_capacity_data *)
3559 		    malloc(sizeof(*rcap), M_SCSIDA, M_NOWAIT|M_ZERO);
3560 		if (rcap == NULL) {
3561 			printf("dastart: Couldn't malloc read_capacity data\n");
3562 			/* da_free_periph??? */
3563 			break;
3564 		}
3565 		scsi_read_capacity(&start_ccb->csio,
3566 				   /*retries*/da_retry_count,
3567 				   dadone_proberc,
3568 				   MSG_SIMPLE_Q_TAG,
3569 				   rcap,
3570 				   SSD_FULL_SIZE,
3571 				   /*timeout*/5000);
3572 		start_ccb->ccb_h.ccb_bp = NULL;
3573 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC;
3574 		xpt_action(start_ccb);
3575 		break;
3576 	}
3577 	case DA_STATE_PROBE_RC16:
3578 	{
3579 		struct scsi_read_capacity_data_long *rcaplong;
3580 
3581 		rcaplong = (struct scsi_read_capacity_data_long *)
3582 			malloc(sizeof(*rcaplong), M_SCSIDA, M_NOWAIT|M_ZERO);
3583 		if (rcaplong == NULL) {
3584 			printf("dastart: Couldn't malloc read_capacity data\n");
3585 			/* da_free_periph??? */
3586 			break;
3587 		}
3588 		scsi_read_capacity_16(&start_ccb->csio,
3589 				      /*retries*/ da_retry_count,
3590 				      /*cbfcnp*/ dadone_proberc,
3591 				      /*tag_action*/ MSG_SIMPLE_Q_TAG,
3592 				      /*lba*/ 0,
3593 				      /*reladr*/ 0,
3594 				      /*pmi*/ 0,
3595 				      /*rcap_buf*/ (uint8_t *)rcaplong,
3596 				      /*rcap_buf_len*/ sizeof(*rcaplong),
3597 				      /*sense_len*/ SSD_FULL_SIZE,
3598 				      /*timeout*/ da_default_timeout * 1000);
3599 		start_ccb->ccb_h.ccb_bp = NULL;
3600 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC16;
3601 		xpt_action(start_ccb);
3602 		break;
3603 	}
3604 	case DA_STATE_PROBE_LBP:
3605 	{
3606 		struct scsi_vpd_logical_block_prov *lbp;
3607 
3608 		if (!scsi_vpd_supported_page(periph, SVPD_LBP)) {
3609 			/*
3610 			 * If we get here we don't support any SBC-3 delete
3611 			 * methods with UNMAP as the Logical Block Provisioning
3612 			 * VPD page support is required for devices which
3613 			 * support it according to T10/1799-D Revision 31
3614 			 * however older revisions of the spec don't mandate
3615 			 * this so we currently don't remove these methods
3616 			 * from the available set.
3617 			 */
3618 			softc->state = DA_STATE_PROBE_BLK_LIMITS;
3619 			goto skipstate;
3620 		}
3621 
3622 		lbp = (struct scsi_vpd_logical_block_prov *)
3623 			malloc(sizeof(*lbp), M_SCSIDA, M_NOWAIT|M_ZERO);
3624 
3625 		if (lbp == NULL) {
3626 			printf("dastart: Couldn't malloc lbp data\n");
3627 			/* da_free_periph??? */
3628 			break;
3629 		}
3630 
3631 		scsi_inquiry(&start_ccb->csio,
3632 			     /*retries*/da_retry_count,
3633 			     /*cbfcnp*/dadone_probelbp,
3634 			     /*tag_action*/MSG_SIMPLE_Q_TAG,
3635 			     /*inq_buf*/(uint8_t *)lbp,
3636 			     /*inq_len*/sizeof(*lbp),
3637 			     /*evpd*/TRUE,
3638 			     /*page_code*/SVPD_LBP,
3639 			     /*sense_len*/SSD_MIN_SIZE,
3640 			     /*timeout*/da_default_timeout * 1000);
3641 		start_ccb->ccb_h.ccb_bp = NULL;
3642 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_LBP;
3643 		xpt_action(start_ccb);
3644 		break;
3645 	}
3646 	case DA_STATE_PROBE_BLK_LIMITS:
3647 	{
3648 		struct scsi_vpd_block_limits *block_limits;
3649 
3650 		if (!scsi_vpd_supported_page(periph, SVPD_BLOCK_LIMITS)) {
3651 			/* Not supported skip to next probe */
3652 			softc->state = DA_STATE_PROBE_BDC;
3653 			goto skipstate;
3654 		}
3655 
3656 		block_limits = (struct scsi_vpd_block_limits *)
3657 			malloc(sizeof(*block_limits), M_SCSIDA, M_NOWAIT|M_ZERO);
3658 
3659 		if (block_limits == NULL) {
3660 			printf("dastart: Couldn't malloc block_limits data\n");
3661 			/* da_free_periph??? */
3662 			break;
3663 		}
3664 
3665 		scsi_inquiry(&start_ccb->csio,
3666 			     /*retries*/da_retry_count,
3667 			     /*cbfcnp*/dadone_probeblklimits,
3668 			     /*tag_action*/MSG_SIMPLE_Q_TAG,
3669 			     /*inq_buf*/(uint8_t *)block_limits,
3670 			     /*inq_len*/sizeof(*block_limits),
3671 			     /*evpd*/TRUE,
3672 			     /*page_code*/SVPD_BLOCK_LIMITS,
3673 			     /*sense_len*/SSD_MIN_SIZE,
3674 			     /*timeout*/da_default_timeout * 1000);
3675 		start_ccb->ccb_h.ccb_bp = NULL;
3676 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BLK_LIMITS;
3677 		xpt_action(start_ccb);
3678 		break;
3679 	}
3680 	case DA_STATE_PROBE_BDC:
3681 	{
3682 		struct scsi_vpd_block_device_characteristics *bdc;
3683 
3684 		if (!scsi_vpd_supported_page(periph, SVPD_BDC)) {
3685 			softc->state = DA_STATE_PROBE_ATA;
3686 			goto skipstate;
3687 		}
3688 
3689 		bdc = (struct scsi_vpd_block_device_characteristics *)
3690 			malloc(sizeof(*bdc), M_SCSIDA, M_NOWAIT|M_ZERO);
3691 
3692 		if (bdc == NULL) {
3693 			printf("dastart: Couldn't malloc bdc data\n");
3694 			/* da_free_periph??? */
3695 			break;
3696 		}
3697 
3698 		scsi_inquiry(&start_ccb->csio,
3699 			     /*retries*/da_retry_count,
3700 			     /*cbfcnp*/dadone_probebdc,
3701 			     /*tag_action*/MSG_SIMPLE_Q_TAG,
3702 			     /*inq_buf*/(uint8_t *)bdc,
3703 			     /*inq_len*/sizeof(*bdc),
3704 			     /*evpd*/TRUE,
3705 			     /*page_code*/SVPD_BDC,
3706 			     /*sense_len*/SSD_MIN_SIZE,
3707 			     /*timeout*/da_default_timeout * 1000);
3708 		start_ccb->ccb_h.ccb_bp = NULL;
3709 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BDC;
3710 		xpt_action(start_ccb);
3711 		break;
3712 	}
3713 	case DA_STATE_PROBE_ATA:
3714 	{
3715 		struct ata_params *ata_params;
3716 
3717 		if (!scsi_vpd_supported_page(periph, SVPD_ATA_INFORMATION)) {
3718 			if ((softc->zone_mode == DA_ZONE_HOST_AWARE)
3719 			 || (softc->zone_mode == DA_ZONE_HOST_MANAGED)) {
3720 				/*
3721 				 * Note that if the ATA VPD page isn't
3722 				 * supported, we aren't talking to an ATA
3723 				 * device anyway.  Support for that VPD
3724 				 * page is mandatory for SCSI to ATA (SAT)
3725 				 * translation layers.
3726 				 */
3727 				softc->state = DA_STATE_PROBE_ZONE;
3728 				goto skipstate;
3729 			}
3730 			daprobedone(periph, start_ccb);
3731 			break;
3732 		}
3733 
3734 		ata_params = &periph->path->device->ident_data;
3735 
3736 		scsi_ata_identify(&start_ccb->csio,
3737 				  /*retries*/da_retry_count,
3738 				  /*cbfcnp*/dadone_probeata,
3739                                   /*tag_action*/MSG_SIMPLE_Q_TAG,
3740 				  /*data_ptr*/(uint8_t *)ata_params,
3741 				  /*dxfer_len*/sizeof(*ata_params),
3742 				  /*sense_len*/SSD_FULL_SIZE,
3743 				  /*timeout*/da_default_timeout * 1000);
3744 		start_ccb->ccb_h.ccb_bp = NULL;
3745 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA;
3746 		xpt_action(start_ccb);
3747 		break;
3748 	}
3749 	case DA_STATE_PROBE_ATA_LOGDIR:
3750 	{
3751 		struct ata_gp_log_dir *log_dir;
3752 		int retval;
3753 
3754 		retval = 0;
3755 
3756 		if ((softc->flags & DA_FLAG_CAN_ATA_LOG) == 0) {
3757 			/*
3758 			 * If we don't have log support, not much point in
3759 			 * trying to probe zone support.
3760 			 */
3761 			daprobedone(periph, start_ccb);
3762 			break;
3763 		}
3764 
3765 		/*
3766 		 * If we have an ATA device (the SCSI ATA Information VPD
3767 		 * page should be present and the ATA identify should have
3768 		 * succeeded) and it supports logs, ask for the log directory.
3769 		 */
3770 
3771 		log_dir = malloc(sizeof(*log_dir), M_SCSIDA, M_NOWAIT|M_ZERO);
3772 		if (log_dir == NULL) {
3773 			xpt_print(periph->path, "Couldn't malloc log_dir "
3774 			    "data\n");
3775 			daprobedone(periph, start_ccb);
3776 			break;
3777 		}
3778 
3779 		retval = scsi_ata_read_log(&start_ccb->csio,
3780 		    /*retries*/ da_retry_count,
3781 		    /*cbfcnp*/ dadone_probeatalogdir,
3782 		    /*tag_action*/ MSG_SIMPLE_Q_TAG,
3783 		    /*log_address*/ ATA_LOG_DIRECTORY,
3784 		    /*page_number*/ 0,
3785 		    /*block_count*/ 1,
3786 		    /*protocol*/ softc->flags & DA_FLAG_CAN_ATA_DMA ?
3787 				 AP_PROTO_DMA : AP_PROTO_PIO_IN,
3788 		    /*data_ptr*/ (uint8_t *)log_dir,
3789 		    /*dxfer_len*/ sizeof(*log_dir),
3790 		    /*sense_len*/ SSD_FULL_SIZE,
3791 		    /*timeout*/ da_default_timeout * 1000);
3792 
3793 		if (retval != 0) {
3794 			xpt_print(periph->path, "scsi_ata_read_log() failed!");
3795 			free(log_dir, M_SCSIDA);
3796 			daprobedone(periph, start_ccb);
3797 			break;
3798 		}
3799 		start_ccb->ccb_h.ccb_bp = NULL;
3800 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA_LOGDIR;
3801 		xpt_action(start_ccb);
3802 		break;
3803 	}
3804 	case DA_STATE_PROBE_ATA_IDDIR:
3805 	{
3806 		struct ata_identify_log_pages *id_dir;
3807 		int retval;
3808 
3809 		retval = 0;
3810 
3811 		/*
3812 		 * Check here to see whether the Identify Device log is
3813 		 * supported in the directory of logs.  If so, continue
3814 		 * with requesting the log of identify device pages.
3815 		 */
3816 		if ((softc->flags & DA_FLAG_CAN_ATA_IDLOG) == 0) {
3817 			daprobedone(periph, start_ccb);
3818 			break;
3819 		}
3820 
3821 		id_dir = malloc(sizeof(*id_dir), M_SCSIDA, M_NOWAIT | M_ZERO);
3822 		if (id_dir == NULL) {
3823 			xpt_print(periph->path, "Couldn't malloc id_dir "
3824 			    "data\n");
3825 			daprobedone(periph, start_ccb);
3826 			break;
3827 		}
3828 
3829 		retval = scsi_ata_read_log(&start_ccb->csio,
3830 		    /*retries*/ da_retry_count,
3831 		    /*cbfcnp*/ dadone_probeataiddir,
3832 		    /*tag_action*/ MSG_SIMPLE_Q_TAG,
3833 		    /*log_address*/ ATA_IDENTIFY_DATA_LOG,
3834 		    /*page_number*/ ATA_IDL_PAGE_LIST,
3835 		    /*block_count*/ 1,
3836 		    /*protocol*/ softc->flags & DA_FLAG_CAN_ATA_DMA ?
3837 				 AP_PROTO_DMA : AP_PROTO_PIO_IN,
3838 		    /*data_ptr*/ (uint8_t *)id_dir,
3839 		    /*dxfer_len*/ sizeof(*id_dir),
3840 		    /*sense_len*/ SSD_FULL_SIZE,
3841 		    /*timeout*/ da_default_timeout * 1000);
3842 
3843 		if (retval != 0) {
3844 			xpt_print(periph->path, "scsi_ata_read_log() failed!");
3845 			free(id_dir, M_SCSIDA);
3846 			daprobedone(periph, start_ccb);
3847 			break;
3848 		}
3849 		start_ccb->ccb_h.ccb_bp = NULL;
3850 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA_IDDIR;
3851 		xpt_action(start_ccb);
3852 		break;
3853 	}
3854 	case DA_STATE_PROBE_ATA_SUP:
3855 	{
3856 		struct ata_identify_log_sup_cap *sup_cap;
3857 		int retval;
3858 
3859 		retval = 0;
3860 
3861 		/*
3862 		 * Check here to see whether the Supported Capabilities log
3863 		 * is in the list of Identify Device logs.
3864 		 */
3865 		if ((softc->flags & DA_FLAG_CAN_ATA_SUPCAP) == 0) {
3866 			daprobedone(periph, start_ccb);
3867 			break;
3868 		}
3869 
3870 		sup_cap = malloc(sizeof(*sup_cap), M_SCSIDA, M_NOWAIT|M_ZERO);
3871 		if (sup_cap == NULL) {
3872 			xpt_print(periph->path, "Couldn't malloc sup_cap "
3873 			    "data\n");
3874 			daprobedone(periph, start_ccb);
3875 			break;
3876 		}
3877 
3878 		retval = scsi_ata_read_log(&start_ccb->csio,
3879 		    /*retries*/ da_retry_count,
3880 		    /*cbfcnp*/ dadone_probeatasup,
3881 		    /*tag_action*/ MSG_SIMPLE_Q_TAG,
3882 		    /*log_address*/ ATA_IDENTIFY_DATA_LOG,
3883 		    /*page_number*/ ATA_IDL_SUP_CAP,
3884 		    /*block_count*/ 1,
3885 		    /*protocol*/ softc->flags & DA_FLAG_CAN_ATA_DMA ?
3886 				 AP_PROTO_DMA : AP_PROTO_PIO_IN,
3887 		    /*data_ptr*/ (uint8_t *)sup_cap,
3888 		    /*dxfer_len*/ sizeof(*sup_cap),
3889 		    /*sense_len*/ SSD_FULL_SIZE,
3890 		    /*timeout*/ da_default_timeout * 1000);
3891 
3892 		if (retval != 0) {
3893 			xpt_print(periph->path, "scsi_ata_read_log() failed!");
3894 			free(sup_cap, M_SCSIDA);
3895 			daprobedone(periph, start_ccb);
3896 			break;
3897 		}
3898 
3899 		start_ccb->ccb_h.ccb_bp = NULL;
3900 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA_SUP;
3901 		xpt_action(start_ccb);
3902 		break;
3903 	}
3904 	case DA_STATE_PROBE_ATA_ZONE:
3905 	{
3906 		struct ata_zoned_info_log *ata_zone;
3907 		int retval;
3908 
3909 		retval = 0;
3910 
3911 		/*
3912 		 * Check here to see whether the zoned device information
3913 		 * page is supported.  If so, continue on to request it.
3914 		 * If not, skip to DA_STATE_PROBE_LOG or done.
3915 		 */
3916 		if ((softc->flags & DA_FLAG_CAN_ATA_ZONE) == 0) {
3917 			daprobedone(periph, start_ccb);
3918 			break;
3919 		}
3920 		ata_zone = malloc(sizeof(*ata_zone), M_SCSIDA,
3921 				  M_NOWAIT|M_ZERO);
3922 		if (ata_zone == NULL) {
3923 			xpt_print(periph->path, "Couldn't malloc ata_zone "
3924 			    "data\n");
3925 			daprobedone(periph, start_ccb);
3926 			break;
3927 		}
3928 
3929 		retval = scsi_ata_read_log(&start_ccb->csio,
3930 		    /*retries*/ da_retry_count,
3931 		    /*cbfcnp*/ dadone_probeatazone,
3932 		    /*tag_action*/ MSG_SIMPLE_Q_TAG,
3933 		    /*log_address*/ ATA_IDENTIFY_DATA_LOG,
3934 		    /*page_number*/ ATA_IDL_ZDI,
3935 		    /*block_count*/ 1,
3936 		    /*protocol*/ softc->flags & DA_FLAG_CAN_ATA_DMA ?
3937 				 AP_PROTO_DMA : AP_PROTO_PIO_IN,
3938 		    /*data_ptr*/ (uint8_t *)ata_zone,
3939 		    /*dxfer_len*/ sizeof(*ata_zone),
3940 		    /*sense_len*/ SSD_FULL_SIZE,
3941 		    /*timeout*/ da_default_timeout * 1000);
3942 
3943 		if (retval != 0) {
3944 			xpt_print(periph->path, "scsi_ata_read_log() failed!");
3945 			free(ata_zone, M_SCSIDA);
3946 			daprobedone(periph, start_ccb);
3947 			break;
3948 		}
3949 		start_ccb->ccb_h.ccb_bp = NULL;
3950 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA_ZONE;
3951 		xpt_action(start_ccb);
3952 
3953 		break;
3954 	}
3955 	case DA_STATE_PROBE_ZONE:
3956 	{
3957 		struct scsi_vpd_zoned_bdc *bdc;
3958 
3959 		/*
3960 		 * Note that this page will be supported for SCSI protocol
3961 		 * devices that support ZBC (SMR devices), as well as ATA
3962 		 * protocol devices that are behind a SAT (SCSI to ATA
3963 		 * Translation) layer that supports converting ZBC commands
3964 		 * to their ZAC equivalents.
3965 		 */
3966 		if (!scsi_vpd_supported_page(periph, SVPD_ZONED_BDC)) {
3967 			daprobedone(periph, start_ccb);
3968 			break;
3969 		}
3970 		bdc = (struct scsi_vpd_zoned_bdc *)
3971 			malloc(sizeof(*bdc), M_SCSIDA, M_NOWAIT|M_ZERO);
3972 
3973 		if (bdc == NULL) {
3974 			xpt_release_ccb(start_ccb);
3975 			xpt_print(periph->path, "Couldn't malloc zone VPD "
3976 			    "data\n");
3977 			break;
3978 		}
3979 		scsi_inquiry(&start_ccb->csio,
3980 			     /*retries*/da_retry_count,
3981 			     /*cbfcnp*/dadone_probezone,
3982 			     /*tag_action*/MSG_SIMPLE_Q_TAG,
3983 			     /*inq_buf*/(uint8_t *)bdc,
3984 			     /*inq_len*/sizeof(*bdc),
3985 			     /*evpd*/TRUE,
3986 			     /*page_code*/SVPD_ZONED_BDC,
3987 			     /*sense_len*/SSD_FULL_SIZE,
3988 			     /*timeout*/da_default_timeout * 1000);
3989 		start_ccb->ccb_h.ccb_bp = NULL;
3990 		start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ZONE;
3991 		xpt_action(start_ccb);
3992 		break;
3993 	}
3994 	}
3995 }
3996 
3997 /*
3998  * In each of the methods below, while its the caller's
3999  * responsibility to ensure the request will fit into a
4000  * single device request, we might have changed the delete
4001  * method due to the device incorrectly advertising either
4002  * its supported methods or limits.
4003  *
4004  * To prevent this causing further issues we validate the
4005  * against the methods limits, and warn which would
4006  * otherwise be unnecessary.
4007  */
4008 static void
da_delete_unmap(struct cam_periph * periph,union ccb * ccb,struct bio * bp)4009 da_delete_unmap(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
4010 {
4011 	struct da_softc *softc = (struct da_softc *)periph->softc;
4012 	struct bio *bp1;
4013 	uint8_t *buf = softc->unmap_buf;
4014 	struct scsi_unmap_desc *d = (void *)&buf[UNMAP_HEAD_SIZE];
4015 	uint64_t lba, lastlba = (uint64_t)-1;
4016 	uint64_t totalcount = 0;
4017 	uint64_t count;
4018 	uint32_t c, lastcount = 0, ranges = 0;
4019 
4020 	/*
4021 	 * Currently this doesn't take the UNMAP
4022 	 * Granularity and Granularity Alignment
4023 	 * fields into account.
4024 	 *
4025 	 * This could result in both unoptimal unmap
4026 	 * requests as as well as UNMAP calls unmapping
4027 	 * fewer LBA's than requested.
4028 	 */
4029 
4030 	bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
4031 	bp1 = bp;
4032 	do {
4033 		/*
4034 		 * Note: ada and da are different in how they store the
4035 		 * pending bp's in a trim. ada stores all of them in the
4036 		 * trim_req.bps. da stores all but the first one in the
4037 		 * delete_run_queue. ada then completes all the bps in
4038 		 * its adadone() loop. da completes all the bps in the
4039 		 * delete_run_queue in dadone, and relies on the biodone
4040 		 * after to complete. This should be reconciled since there's
4041 		 * no real reason to do it differently. XXX
4042 		 */
4043 		if (bp1 != bp)
4044 			bioq_insert_tail(&softc->delete_run_queue, bp1);
4045 		lba = bp1->bio_pblkno;
4046 		count = bp1->bio_bcount / softc->params.secsize;
4047 
4048 		/* Try to extend the previous range. */
4049 		if (lba == lastlba) {
4050 			c = omin(count, UNMAP_RANGE_MAX - lastcount);
4051 			lastlba += c;
4052 			lastcount += c;
4053 			scsi_ulto4b(lastcount, d[ranges - 1].length);
4054 			count -= c;
4055 			lba += c;
4056 			totalcount += c;
4057 		} else if ((softc->quirks & DA_Q_STRICT_UNMAP) &&
4058 		    softc->unmap_gran != 0) {
4059 			/* Align length of the previous range. */
4060 			if ((c = lastcount % softc->unmap_gran) != 0) {
4061 				if (lastcount <= c) {
4062 					totalcount -= lastcount;
4063 					lastlba = (uint64_t)-1;
4064 					lastcount = 0;
4065 					ranges--;
4066 				} else {
4067 					totalcount -= c;
4068 					lastlba -= c;
4069 					lastcount -= c;
4070 					scsi_ulto4b(lastcount,
4071 					    d[ranges - 1].length);
4072 				}
4073 			}
4074 			/* Align beginning of the new range. */
4075 			c = (lba - softc->unmap_gran_align) % softc->unmap_gran;
4076 			if (c != 0) {
4077 				c = softc->unmap_gran - c;
4078 				if (count <= c) {
4079 					count = 0;
4080 				} else {
4081 					lba += c;
4082 					count -= c;
4083 				}
4084 			}
4085 		}
4086 
4087 		while (count > 0) {
4088 			c = omin(count, UNMAP_RANGE_MAX);
4089 			if (totalcount + c > softc->unmap_max_lba ||
4090 			    ranges >= softc->unmap_max_ranges) {
4091 				xpt_print(periph->path,
4092 				    "%s issuing short delete %ld > %ld"
4093 				    "|| %d >= %d",
4094 				    da_delete_method_desc[softc->delete_method],
4095 				    totalcount + c, softc->unmap_max_lba,
4096 				    ranges, softc->unmap_max_ranges);
4097 				break;
4098 			}
4099 			scsi_u64to8b(lba, d[ranges].lba);
4100 			scsi_ulto4b(c, d[ranges].length);
4101 			lba += c;
4102 			totalcount += c;
4103 			ranges++;
4104 			count -= c;
4105 			lastlba = lba;
4106 			lastcount = c;
4107 		}
4108 		bp1 = cam_iosched_next_trim(softc->cam_iosched);
4109 		if (bp1 == NULL)
4110 			break;
4111 		if (ranges >= softc->unmap_max_ranges ||
4112 		    totalcount + bp1->bio_bcount /
4113 		    softc->params.secsize > softc->unmap_max_lba) {
4114 			cam_iosched_put_back_trim(softc->cam_iosched, bp1);
4115 			break;
4116 		}
4117 	} while (1);
4118 
4119 	/* Align length of the last range. */
4120 	if ((softc->quirks & DA_Q_STRICT_UNMAP) && softc->unmap_gran != 0 &&
4121 	    (c = lastcount % softc->unmap_gran) != 0) {
4122 		if (lastcount <= c)
4123 			ranges--;
4124 		else
4125 			scsi_ulto4b(lastcount - c, d[ranges - 1].length);
4126 	}
4127 
4128 	scsi_ulto2b(ranges * 16 + 6, &buf[0]);
4129 	scsi_ulto2b(ranges * 16, &buf[2]);
4130 
4131 	scsi_unmap(&ccb->csio,
4132 		   /*retries*/da_retry_count,
4133 		   /*cbfcnp*/dadone,
4134 		   /*tag_action*/MSG_SIMPLE_Q_TAG,
4135 		   /*byte2*/0,
4136 		   /*data_ptr*/ buf,
4137 		   /*dxfer_len*/ ranges * 16 + 8,
4138 		   /*sense_len*/SSD_FULL_SIZE,
4139 		   da_default_timeout * 1000);
4140 	ccb->ccb_h.ccb_state = DA_CCB_DELETE;
4141 	ccb->ccb_h.flags |= CAM_UNLOCKED;
4142 	softc->trim_count++;
4143 	softc->trim_ranges += ranges;
4144 	softc->trim_lbas += totalcount;
4145 	cam_iosched_submit_trim(softc->cam_iosched);
4146 }
4147 
4148 static void
da_delete_trim(struct cam_periph * periph,union ccb * ccb,struct bio * bp)4149 da_delete_trim(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
4150 {
4151 	struct da_softc *softc = (struct da_softc *)periph->softc;
4152 	struct bio *bp1;
4153 	uint8_t *buf = softc->unmap_buf;
4154 	uint64_t lastlba = (uint64_t)-1;
4155 	uint64_t count;
4156 	uint64_t lba;
4157 	uint32_t lastcount = 0, c, requestcount;
4158 	int ranges = 0, off, block_count;
4159 
4160 	bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
4161 	bp1 = bp;
4162 	do {
4163 		if (bp1 != bp)//XXX imp XXX
4164 			bioq_insert_tail(&softc->delete_run_queue, bp1);
4165 		lba = bp1->bio_pblkno;
4166 		count = bp1->bio_bcount / softc->params.secsize;
4167 		requestcount = count;
4168 
4169 		/* Try to extend the previous range. */
4170 		if (lba == lastlba) {
4171 			c = omin(count, ATA_DSM_RANGE_MAX - lastcount);
4172 			lastcount += c;
4173 			off = (ranges - 1) * 8;
4174 			buf[off + 6] = lastcount & 0xff;
4175 			buf[off + 7] = (lastcount >> 8) & 0xff;
4176 			count -= c;
4177 			lba += c;
4178 		}
4179 
4180 		while (count > 0) {
4181 			c = omin(count, ATA_DSM_RANGE_MAX);
4182 			off = ranges * 8;
4183 
4184 			buf[off + 0] = lba & 0xff;
4185 			buf[off + 1] = (lba >> 8) & 0xff;
4186 			buf[off + 2] = (lba >> 16) & 0xff;
4187 			buf[off + 3] = (lba >> 24) & 0xff;
4188 			buf[off + 4] = (lba >> 32) & 0xff;
4189 			buf[off + 5] = (lba >> 40) & 0xff;
4190 			buf[off + 6] = c & 0xff;
4191 			buf[off + 7] = (c >> 8) & 0xff;
4192 			lba += c;
4193 			ranges++;
4194 			count -= c;
4195 			lastcount = c;
4196 			if (count != 0 && ranges == softc->trim_max_ranges) {
4197 				xpt_print(periph->path,
4198 				    "%s issuing short delete %ld > %ld\n",
4199 				    da_delete_method_desc[softc->delete_method],
4200 				    requestcount,
4201 				    (softc->trim_max_ranges - ranges) *
4202 				    ATA_DSM_RANGE_MAX);
4203 				break;
4204 			}
4205 		}
4206 		lastlba = lba;
4207 		bp1 = cam_iosched_next_trim(softc->cam_iosched);
4208 		if (bp1 == NULL)
4209 			break;
4210 		if (bp1->bio_bcount / softc->params.secsize >
4211 		    (softc->trim_max_ranges - ranges) * ATA_DSM_RANGE_MAX) {
4212 			cam_iosched_put_back_trim(softc->cam_iosched, bp1);
4213 			break;
4214 		}
4215 	} while (1);
4216 
4217 	block_count = howmany(ranges, ATA_DSM_BLK_RANGES);
4218 	scsi_ata_trim(&ccb->csio,
4219 		      /*retries*/da_retry_count,
4220 		      /*cbfcnp*/dadone,
4221 		      /*tag_action*/MSG_SIMPLE_Q_TAG,
4222 		      block_count,
4223 		      /*data_ptr*/buf,
4224 		      /*dxfer_len*/block_count * ATA_DSM_BLK_SIZE,
4225 		      /*sense_len*/SSD_FULL_SIZE,
4226 		      da_default_timeout * 1000);
4227 	ccb->ccb_h.ccb_state = DA_CCB_DELETE;
4228 	ccb->ccb_h.flags |= CAM_UNLOCKED;
4229 	softc->trim_count++;
4230 	softc->trim_ranges += ranges;
4231 	softc->trim_lbas += block_count;
4232 	cam_iosched_submit_trim(softc->cam_iosched);
4233 }
4234 
4235 /*
4236  * We calculate ws_max_blks here based off d_delmaxsize instead
4237  * of using softc->ws_max_blks as it is absolute max for the
4238  * device not the protocol max which may well be lower.
4239  */
4240 static void
da_delete_ws(struct cam_periph * periph,union ccb * ccb,struct bio * bp)4241 da_delete_ws(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
4242 {
4243 	struct da_softc *softc;
4244 	struct bio *bp1;
4245 	uint64_t ws_max_blks;
4246 	uint64_t lba;
4247 	uint64_t count; /* forward compat with WS32 */
4248 
4249 	softc = (struct da_softc *)periph->softc;
4250 	ws_max_blks = softc->disk->d_delmaxsize / softc->params.secsize;
4251 	lba = bp->bio_pblkno;
4252 	count = 0;
4253 	bp1 = bp;
4254 	do {
4255 		if (bp1 != bp)//XXX imp XXX
4256 			bioq_insert_tail(&softc->delete_run_queue, bp1);
4257 		count += bp1->bio_bcount / softc->params.secsize;
4258 		if (count > ws_max_blks) {
4259 			xpt_print(periph->path,
4260 			    "%s issuing short delete %ld > %ld\n",
4261 			    da_delete_method_desc[softc->delete_method],
4262 			    count, ws_max_blks);
4263 			count = omin(count, ws_max_blks);
4264 			break;
4265 		}
4266 		bp1 = cam_iosched_next_trim(softc->cam_iosched);
4267 		if (bp1 == NULL)
4268 			break;
4269 		if (lba + count != bp1->bio_pblkno ||
4270 		    count + bp1->bio_bcount /
4271 		    softc->params.secsize > ws_max_blks) {
4272 			cam_iosched_put_back_trim(softc->cam_iosched, bp1);
4273 			break;
4274 		}
4275 	} while (1);
4276 
4277 	scsi_write_same(&ccb->csio,
4278 			/*retries*/da_retry_count,
4279 			/*cbfcnp*/dadone,
4280 			/*tag_action*/MSG_SIMPLE_Q_TAG,
4281 			/*byte2*/softc->delete_method ==
4282 			    DA_DELETE_ZERO ? 0 : SWS_UNMAP,
4283 			softc->delete_method == DA_DELETE_WS16 ? 16 : 10,
4284 			/*lba*/lba,
4285 			/*block_count*/count,
4286 			/*data_ptr*/ __DECONST(void *, zero_region),
4287 			/*dxfer_len*/ softc->params.secsize,
4288 			/*sense_len*/SSD_FULL_SIZE,
4289 			da_default_timeout * 1000);
4290 	ccb->ccb_h.ccb_state = DA_CCB_DELETE;
4291 	ccb->ccb_h.flags |= CAM_UNLOCKED;
4292 	softc->trim_count++;
4293 	softc->trim_ranges++;
4294 	softc->trim_lbas += count;
4295 	cam_iosched_submit_trim(softc->cam_iosched);
4296 }
4297 
4298 static int
cmd6workaround(union ccb * ccb)4299 cmd6workaround(union ccb *ccb)
4300 {
4301 	struct scsi_rw_6 cmd6;
4302 	struct scsi_rw_10 *cmd10;
4303 	struct da_softc *softc;
4304 	uint8_t *cdb;
4305 	struct bio *bp;
4306 	int frozen;
4307 
4308 	cdb = ccb->csio.cdb_io.cdb_bytes;
4309 	softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
4310 
4311 	if (ccb->ccb_h.ccb_state == DA_CCB_DELETE) {
4312 		da_delete_methods old_method = softc->delete_method;
4313 
4314 		/*
4315 		 * Typically there are two reasons for failure here
4316 		 * 1. Delete method was detected as supported but isn't
4317 		 * 2. Delete failed due to invalid params e.g. too big
4318 		 *
4319 		 * While we will attempt to choose an alternative delete method
4320 		 * this may result in short deletes if the existing delete
4321 		 * requests from geom are big for the new method chosen.
4322 		 *
4323 		 * This method assumes that the error which triggered this
4324 		 * will not retry the io otherwise a panic will occur
4325 		 */
4326 		dadeleteflag(softc, old_method, 0);
4327 		dadeletemethodchoose(softc, DA_DELETE_DISABLE);
4328 		if (softc->delete_method == DA_DELETE_DISABLE)
4329 			xpt_print(ccb->ccb_h.path,
4330 				  "%s failed, disabling BIO_DELETE\n",
4331 				  da_delete_method_desc[old_method]);
4332 		else
4333 			xpt_print(ccb->ccb_h.path,
4334 				  "%s failed, switching to %s BIO_DELETE\n",
4335 				  da_delete_method_desc[old_method],
4336 				  da_delete_method_desc[softc->delete_method]);
4337 
4338 		while ((bp = bioq_takefirst(&softc->delete_run_queue)) != NULL)
4339 			cam_iosched_queue_work(softc->cam_iosched, bp);
4340 		cam_iosched_queue_work(softc->cam_iosched,
4341 		    (struct bio *)ccb->ccb_h.ccb_bp);
4342 		ccb->ccb_h.ccb_bp = NULL;
4343 		return (0);
4344 	}
4345 
4346 	/* Detect unsupported PREVENT ALLOW MEDIUM REMOVAL. */
4347 	if ((ccb->ccb_h.flags & CAM_CDB_POINTER) == 0 &&
4348 	    (*cdb == PREVENT_ALLOW) &&
4349 	    (softc->quirks & DA_Q_NO_PREVENT) == 0) {
4350 		if (bootverbose)
4351 			xpt_print(ccb->ccb_h.path,
4352 			    "PREVENT ALLOW MEDIUM REMOVAL not supported.\n");
4353 		softc->quirks |= DA_Q_NO_PREVENT;
4354 		return (0);
4355 	}
4356 
4357 	/* Detect unsupported SYNCHRONIZE CACHE(10). */
4358 	if ((ccb->ccb_h.flags & CAM_CDB_POINTER) == 0 &&
4359 	    (*cdb == SYNCHRONIZE_CACHE) &&
4360 	    (softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
4361 		if (bootverbose)
4362 			xpt_print(ccb->ccb_h.path,
4363 			    "SYNCHRONIZE CACHE(10) not supported.\n");
4364 		softc->quirks |= DA_Q_NO_SYNC_CACHE;
4365 		softc->disk->d_flags &= ~DISKFLAG_CANFLUSHCACHE;
4366 		return (0);
4367 	}
4368 
4369 	/* Translation only possible if CDB is an array and cmd is R/W6 */
4370 	if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
4371 	    (*cdb != READ_6 && *cdb != WRITE_6))
4372 		return 0;
4373 
4374 	xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
4375 	    "increasing minimum_cmd_size to 10.\n");
4376 	softc->minimum_cmd_size = 10;
4377 
4378 	bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
4379 	cmd10 = (struct scsi_rw_10 *)cdb;
4380 	cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
4381 	cmd10->byte2 = 0;
4382 	scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
4383 	cmd10->reserved = 0;
4384 	scsi_ulto2b(cmd6.length, cmd10->length);
4385 	cmd10->control = cmd6.control;
4386 	ccb->csio.cdb_len = sizeof(*cmd10);
4387 
4388 	/* Requeue request, unfreezing queue if necessary */
4389 	frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
4390 	ccb->ccb_h.status = CAM_REQUEUE_REQ;
4391 	xpt_action(ccb);
4392 	if (frozen) {
4393 		cam_release_devq(ccb->ccb_h.path,
4394 				 /*relsim_flags*/0,
4395 				 /*reduction*/0,
4396 				 /*timeout*/0,
4397 				 /*getcount_only*/0);
4398 	}
4399 	return (ERESTART);
4400 }
4401 
4402 static void
dazonedone(struct cam_periph * periph,union ccb * ccb)4403 dazonedone(struct cam_periph *periph, union ccb *ccb)
4404 {
4405 	struct da_softc *softc;
4406 	struct bio *bp;
4407 
4408 	softc = periph->softc;
4409 	bp = (struct bio *)ccb->ccb_h.ccb_bp;
4410 
4411 	switch (bp->bio_zone.zone_cmd) {
4412 	case DISK_ZONE_OPEN:
4413 	case DISK_ZONE_CLOSE:
4414 	case DISK_ZONE_FINISH:
4415 	case DISK_ZONE_RWP:
4416 		break;
4417 	case DISK_ZONE_REPORT_ZONES: {
4418 		uint32_t avail_len;
4419 		struct disk_zone_report *rep;
4420 		struct scsi_report_zones_hdr *hdr;
4421 		struct scsi_report_zones_desc *desc;
4422 		struct disk_zone_rep_entry *entry;
4423 		uint32_t hdr_len, num_avail;
4424 		uint32_t num_to_fill, i;
4425 		int ata;
4426 
4427 		rep = &bp->bio_zone.zone_params.report;
4428 		avail_len = ccb->csio.dxfer_len - ccb->csio.resid;
4429 		/*
4430 		 * Note that bio_resid isn't normally used for zone
4431 		 * commands, but it is used by devstat_end_transaction_bio()
4432 		 * to determine how much data was transferred.  Because
4433 		 * the size of the SCSI/ATA data structures is different
4434 		 * than the size of the BIO interface structures, the
4435 		 * amount of data actually transferred from the drive will
4436 		 * be different than the amount of data transferred to
4437 		 * the user.
4438 		 */
4439 		bp->bio_resid = ccb->csio.resid;
4440 		hdr = (struct scsi_report_zones_hdr *)ccb->csio.data_ptr;
4441 		if (avail_len < sizeof(*hdr)) {
4442 			/*
4443 			 * Is there a better error than EIO here?  We asked
4444 			 * for at least the header, and we got less than
4445 			 * that.
4446 			 */
4447 			bp->bio_error = EIO;
4448 			bp->bio_flags |= BIO_ERROR;
4449 			bp->bio_resid = bp->bio_bcount;
4450 			break;
4451 		}
4452 
4453 		if (softc->zone_interface == DA_ZONE_IF_ATA_PASS)
4454 			ata = 1;
4455 		else
4456 			ata = 0;
4457 
4458 		hdr_len = ata ? le32dec(hdr->length) :
4459 				scsi_4btoul(hdr->length);
4460 		if (hdr_len > 0)
4461 			rep->entries_available = hdr_len / sizeof(*desc);
4462 		else
4463 			rep->entries_available = 0;
4464 		/*
4465 		 * NOTE: using the same values for the BIO version of the
4466 		 * same field as the SCSI/ATA values.  This means we could
4467 		 * get some additional values that aren't defined in bio.h
4468 		 * if more values of the same field are defined later.
4469 		 */
4470 		rep->header.same = hdr->byte4 & SRZ_SAME_MASK;
4471 		rep->header.maximum_lba = ata ?  le64dec(hdr->maximum_lba) :
4472 					  scsi_8btou64(hdr->maximum_lba);
4473 		/*
4474 		 * If the drive reports no entries that match the query,
4475 		 * we're done.
4476 		 */
4477 		if (hdr_len == 0) {
4478 			rep->entries_filled = 0;
4479 			break;
4480 		}
4481 
4482 		num_avail = min((avail_len - sizeof(*hdr)) / sizeof(*desc),
4483 				hdr_len / sizeof(*desc));
4484 		/*
4485 		 * If the drive didn't return any data, then we're done.
4486 		 */
4487 		if (num_avail == 0) {
4488 			rep->entries_filled = 0;
4489 			break;
4490 		}
4491 
4492 		num_to_fill = min(num_avail, rep->entries_allocated);
4493 		/*
4494 		 * If the user didn't allocate any entries for us to fill,
4495 		 * we're done.
4496 		 */
4497 		if (num_to_fill == 0) {
4498 			rep->entries_filled = 0;
4499 			break;
4500 		}
4501 
4502 		for (i = 0, desc = &hdr->desc_list[0], entry=&rep->entries[0];
4503 		     i < num_to_fill; i++, desc++, entry++) {
4504 			/*
4505 			 * NOTE: we're mapping the values here directly
4506 			 * from the SCSI/ATA bit definitions to the bio.h
4507 			 * definitions. There is also a warning in
4508 			 * disk_zone.h, but the impact is that if
4509 			 * additional values are added in the SCSI/ATA
4510 			 * specs these will be visible to consumers of
4511 			 * this interface.
4512 			 */
4513 			entry->zone_type = desc->zone_type & SRZ_TYPE_MASK;
4514 			entry->zone_condition =
4515 			    (desc->zone_flags & SRZ_ZONE_COND_MASK) >>
4516 			    SRZ_ZONE_COND_SHIFT;
4517 			entry->zone_flags |= desc->zone_flags &
4518 			    (SRZ_ZONE_NON_SEQ|SRZ_ZONE_RESET);
4519 			entry->zone_length =
4520 			    ata ? le64dec(desc->zone_length) :
4521 				  scsi_8btou64(desc->zone_length);
4522 			entry->zone_start_lba =
4523 			    ata ? le64dec(desc->zone_start_lba) :
4524 				  scsi_8btou64(desc->zone_start_lba);
4525 			entry->write_pointer_lba =
4526 			    ata ? le64dec(desc->write_pointer_lba) :
4527 				  scsi_8btou64(desc->write_pointer_lba);
4528 		}
4529 		rep->entries_filled = num_to_fill;
4530 		break;
4531 	}
4532 	case DISK_ZONE_GET_PARAMS:
4533 	default:
4534 		/*
4535 		 * In theory we should not get a GET_PARAMS bio, since it
4536 		 * should be handled without queueing the command to the
4537 		 * drive.
4538 		 */
4539 		panic("%s: Invalid zone command %d", __func__,
4540 		    bp->bio_zone.zone_cmd);
4541 		break;
4542 	}
4543 
4544 	if (bp->bio_zone.zone_cmd == DISK_ZONE_REPORT_ZONES)
4545 		free(ccb->csio.data_ptr, M_SCSIDA);
4546 }
4547 
4548 static void
dadone(struct cam_periph * periph,union ccb * done_ccb)4549 dadone(struct cam_periph *periph, union ccb *done_ccb)
4550 {
4551 	struct bio *bp, *bp1;
4552 	struct da_softc *softc;
4553 	struct ccb_scsiio *csio;
4554 	da_ccb_state state;
4555 
4556 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone\n"));
4557 
4558 	softc = (struct da_softc *)periph->softc;
4559 	csio = &done_ccb->csio;
4560 
4561 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
4562 	if (csio->bio != NULL)
4563 		biotrack(csio->bio, __func__);
4564 #endif
4565 	state = csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK;
4566 
4567 	cam_periph_lock(periph);
4568 	bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
4569 	if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
4570 		int error;
4571 		int sf;
4572 
4573 		if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
4574 			sf = SF_RETRY_UA;
4575 		else
4576 			sf = 0;
4577 
4578 		error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
4579 		if (error == ERESTART) {
4580 			/* A retry was scheduled, so just return. */
4581 			cam_periph_unlock(periph);
4582 			return;
4583 		}
4584 		bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
4585 		if (error != 0) {
4586 			int queued_error;
4587 
4588 			/*
4589 			 * return all queued I/O with EIO, so that
4590 			 * the client can retry these I/Os in the
4591 			 * proper order should it attempt to recover.
4592 			 */
4593 			queued_error = EIO;
4594 
4595 			if (error == ENXIO
4596 			 && (softc->flags & DA_FLAG_PACK_INVALID)== 0) {
4597 				/*
4598 				 * Catastrophic error.  Mark our pack as
4599 				 * invalid.
4600 				 *
4601 				 * XXX See if this is really a media
4602 				 * XXX change first?
4603 				 */
4604 				xpt_print(periph->path, "Invalidating pack\n");
4605 				softc->flags |= DA_FLAG_PACK_INVALID;
4606 #ifdef CAM_IO_STATS
4607 				softc->invalidations++;
4608 #endif
4609 				queued_error = ENXIO;
4610 			}
4611 			cam_iosched_flush(softc->cam_iosched, NULL,
4612 			   queued_error);
4613 			if (bp != NULL) {
4614 				bp->bio_error = error;
4615 				bp->bio_resid = bp->bio_bcount;
4616 				bp->bio_flags |= BIO_ERROR;
4617 			}
4618 		} else if (bp != NULL) {
4619 			if (state == DA_CCB_DELETE)
4620 				bp->bio_resid = 0;
4621 			else
4622 				bp->bio_resid = csio->resid;
4623 			bp->bio_error = 0;
4624 			if (bp->bio_resid != 0)
4625 				bp->bio_flags |= BIO_ERROR;
4626 		}
4627 		if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
4628 			cam_release_devq(done_ccb->ccb_h.path,
4629 					 /*relsim_flags*/0,
4630 					 /*reduction*/0,
4631 					 /*timeout*/0,
4632 					 /*getcount_only*/0);
4633 	} else if (bp != NULL) {
4634 		if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
4635 			panic("REQ_CMP with QFRZN");
4636 		if (bp->bio_cmd == BIO_ZONE)
4637 			dazonedone(periph, done_ccb);
4638 		else if (state == DA_CCB_DELETE)
4639 			bp->bio_resid = 0;
4640 		else
4641 			bp->bio_resid = csio->resid;
4642 		if ((csio->resid > 0) && (bp->bio_cmd != BIO_ZONE))
4643 			bp->bio_flags |= BIO_ERROR;
4644 		if (softc->error_inject != 0) {
4645 			bp->bio_error = softc->error_inject;
4646 			bp->bio_resid = bp->bio_bcount;
4647 			bp->bio_flags |= BIO_ERROR;
4648 			softc->error_inject = 0;
4649 		}
4650 	}
4651 
4652 	if (bp != NULL)
4653 		biotrack(bp, __func__);
4654 	LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
4655 	if (LIST_EMPTY(&softc->pending_ccbs))
4656 		softc->flags |= DA_FLAG_WAS_OTAG;
4657 
4658 	/*
4659 	 * We need to call cam_iosched before we call biodone so that we don't
4660 	 * measure any activity that happens in the completion routine, which in
4661 	 * the case of sendfile can be quite extensive. Release the periph
4662 	 * refcount taken in dastart() for each CCB.
4663 	 */
4664 	cam_iosched_bio_complete(softc->cam_iosched, bp, done_ccb);
4665 	xpt_release_ccb(done_ccb);
4666 	KASSERT(softc->refcount >= 1, ("dadone softc %p refcount %d", softc, softc->refcount));
4667 	softc->refcount--;
4668 	if (state == DA_CCB_DELETE) {
4669 		TAILQ_HEAD(, bio) queue;
4670 
4671 		TAILQ_INIT(&queue);
4672 		TAILQ_CONCAT(&queue, &softc->delete_run_queue.queue, bio_queue);
4673 		softc->delete_run_queue.insert_point = NULL;
4674 		/*
4675 		 * Normally, the xpt_release_ccb() above would make sure
4676 		 * that when we have more work to do, that work would
4677 		 * get kicked off. However, we specifically keep
4678 		 * delete_running set to 0 before the call above to
4679 		 * allow other I/O to progress when many BIO_DELETE
4680 		 * requests are pushed down. We set delete_running to 0
4681 		 * and call daschedule again so that we don't stall if
4682 		 * there are no other I/Os pending apart from BIO_DELETEs.
4683 		 */
4684 		cam_iosched_trim_done(softc->cam_iosched);
4685 		daschedule(periph);
4686 		cam_periph_unlock(periph);
4687 		while ((bp1 = TAILQ_FIRST(&queue)) != NULL) {
4688 			TAILQ_REMOVE(&queue, bp1, bio_queue);
4689 			bp1->bio_error = bp->bio_error;
4690 			if (bp->bio_flags & BIO_ERROR) {
4691 				bp1->bio_flags |= BIO_ERROR;
4692 				bp1->bio_resid = bp1->bio_bcount;
4693 			} else
4694 				bp1->bio_resid = 0;
4695 			biodone(bp1);
4696 		}
4697 	} else {
4698 		daschedule(periph);
4699 		cam_periph_unlock(periph);
4700 	}
4701 	if (bp != NULL)
4702 		biodone(bp);
4703 	return;
4704 }
4705 
4706 static void
dadone_probewp(struct cam_periph * periph,union ccb * done_ccb)4707 dadone_probewp(struct cam_periph *periph, union ccb *done_ccb)
4708 {
4709 	struct da_softc *softc;
4710 	struct ccb_scsiio *csio;
4711 	uint32_t  priority;
4712 
4713 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone_probewp\n"));
4714 
4715 	softc = (struct da_softc *)periph->softc;
4716 	priority = done_ccb->ccb_h.pinfo.priority;
4717 	csio = &done_ccb->csio;
4718 
4719 	cam_periph_assert(periph, MA_OWNED);
4720 
4721 	KASSERT(softc->state == DA_STATE_PROBE_WP,
4722 	    ("State (%d) not PROBE_WP in dadone_probewp, periph %p ccb %p",
4723 		softc->state, periph, done_ccb));
4724         KASSERT((csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) == DA_CCB_PROBE_WP,
4725 	    ("CCB State (%lu) not PROBE_WP in dadone_probewp, periph %p ccb %p",
4726 		(unsigned long)csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK, periph,
4727 		done_ccb));
4728 
4729 	if (cam_ccb_status(done_ccb) == CAM_REQ_CMP) {
4730 		int len, off;
4731 		uint8_t dev_spec;
4732 
4733 		if (csio->cdb_len > 6) {
4734 			struct scsi_mode_header_10 *mh =
4735 			    (struct scsi_mode_header_10 *)csio->data_ptr;
4736 			len = 2 + scsi_2btoul(mh->data_length);
4737 			off = sizeof(*mh) + scsi_2btoul(mh->blk_desc_len);
4738 			dev_spec = mh->dev_spec;
4739 		} else {
4740 			struct scsi_mode_header_6 *mh =
4741 			    (struct scsi_mode_header_6 *)csio->data_ptr;
4742 			len = 1 + mh->data_length;
4743 			off = sizeof(*mh) + mh->blk_desc_len;
4744 			dev_spec = mh->dev_spec;
4745 		}
4746 		if ((dev_spec & 0x80) != 0)
4747 			softc->disk->d_flags |= DISKFLAG_WRITE_PROTECT;
4748 		else
4749 			softc->disk->d_flags &= ~DISKFLAG_WRITE_PROTECT;
4750 
4751 		/* Next time request only the first of returned mode pages. */
4752 		if (off < len && off < csio->dxfer_len - csio->resid)
4753 			softc->mode_page = csio->data_ptr[off] & SMPH_PC_MASK;
4754 	} else {
4755 		int error;
4756 
4757 		error = daerror(done_ccb, CAM_RETRY_SELTO,
4758 				SF_RETRY_UA|SF_NO_PRINT);
4759 		if (error == ERESTART)
4760 			return;
4761 		else if (error != 0) {
4762 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
4763 				/* Don't wedge this device's queue */
4764 				cam_release_devq(done_ccb->ccb_h.path,
4765 						 /*relsim_flags*/0,
4766 						 /*reduction*/0,
4767 						 /*timeout*/0,
4768 						 /*getcount_only*/0);
4769 			}
4770 
4771 			/* We don't depend on it, so don't try again. */
4772 			softc->mode_page = -1;
4773 		}
4774 	}
4775 
4776 	free(csio->data_ptr, M_SCSIDA);
4777 	if ((softc->flags & DA_FLAG_CAN_RC16) != 0)
4778 		softc->state = DA_STATE_PROBE_RC16;
4779 	else
4780 		softc->state = DA_STATE_PROBE_RC;
4781 	xpt_release_ccb(done_ccb);
4782 	xpt_schedule(periph, priority);
4783 	return;
4784 }
4785 
4786 static void
dadone_proberc(struct cam_periph * periph,union ccb * done_ccb)4787 dadone_proberc(struct cam_periph *periph, union ccb *done_ccb)
4788 {
4789 	struct scsi_read_capacity_data *rdcap;
4790 	struct scsi_read_capacity_data_long *rcaplong;
4791 	struct da_softc *softc;
4792 	struct ccb_scsiio *csio;
4793 	da_ccb_state state;
4794 	char *announce_buf;
4795 	uint32_t  priority;
4796 	int lbp, n;
4797 
4798 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone_proberc\n"));
4799 
4800 	softc = (struct da_softc *)periph->softc;
4801 	priority = done_ccb->ccb_h.pinfo.priority;
4802 	csio = &done_ccb->csio;
4803 	state = csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK;
4804 
4805 	KASSERT(softc->state == DA_STATE_PROBE_RC || softc->state == DA_STATE_PROBE_RC16,
4806 	    ("State (%d) not PROBE_RC* in dadone_proberc, periph %p ccb %p",
4807 		softc->state, periph, done_ccb));
4808 	KASSERT(state == DA_CCB_PROBE_RC || state == DA_CCB_PROBE_RC16,
4809 	    ("CCB State (%lu) not PROBE_RC* in dadone_probewp, periph %p ccb %p",
4810 		(unsigned long)state, periph, done_ccb));
4811 
4812 	lbp = 0;
4813 	rdcap = NULL;
4814 	rcaplong = NULL;
4815 	/* XXX TODO: can this be a malloc? */
4816 	announce_buf = softc->announce_temp;
4817 	bzero(announce_buf, DA_ANNOUNCETMP_SZ);
4818 
4819 	if (state == DA_CCB_PROBE_RC)
4820 		rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
4821 	else
4822 		rcaplong = (struct scsi_read_capacity_data_long *)
4823 			csio->data_ptr;
4824 
4825 	cam_periph_assert(periph, MA_OWNED);
4826 
4827 	if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
4828 		struct disk_params *dp;
4829 		uint32_t block_size;
4830 		uint64_t maxsector;
4831 		u_int lalba;	/* Lowest aligned LBA. */
4832 
4833 		if (state == DA_CCB_PROBE_RC) {
4834 			block_size = scsi_4btoul(rdcap->length);
4835 			maxsector = scsi_4btoul(rdcap->addr);
4836 			lalba = 0;
4837 
4838 			/*
4839 			 * According to SBC-2, if the standard 10
4840 			 * byte READ CAPACITY command returns 2^32,
4841 			 * we should issue the 16 byte version of
4842 			 * the command, since the device in question
4843 			 * has more sectors than can be represented
4844 			 * with the short version of the command.
4845 			 */
4846 			if (maxsector == 0xffffffff) {
4847 				free(rdcap, M_SCSIDA);
4848 				softc->state = DA_STATE_PROBE_RC16;
4849 				xpt_release_ccb(done_ccb);
4850 				xpt_schedule(periph, priority);
4851 				return;
4852 			}
4853 		} else {
4854 			block_size = scsi_4btoul(rcaplong->length);
4855 			maxsector = scsi_8btou64(rcaplong->addr);
4856 			lalba = scsi_2btoul(rcaplong->lalba_lbp);
4857 		}
4858 
4859 		/*
4860 		 * Because GEOM code just will panic us if we
4861 		 * give them an 'illegal' value we'll avoid that
4862 		 * here.
4863 		 */
4864 		if (block_size == 0) {
4865 			block_size = 512;
4866 			if (maxsector == 0)
4867 				maxsector = -1;
4868 		}
4869 		if (block_size >= maxphys) {
4870 			xpt_print(periph->path,
4871 			    "unsupportable block size %ju\n",
4872 			    (uintmax_t) block_size);
4873 			announce_buf = NULL;
4874 			cam_periph_invalidate(periph);
4875 		} else {
4876 			/*
4877 			 * We pass rcaplong into dasetgeom(),
4878 			 * because it will only use it if it is
4879 			 * non-NULL.
4880 			 */
4881 			dasetgeom(periph, block_size, maxsector,
4882 				  rcaplong, sizeof(*rcaplong));
4883 			lbp = (lalba & SRC16_LBPME_A);
4884 			dp = &softc->params;
4885 			n = snprintf(announce_buf, DA_ANNOUNCETMP_SZ,
4886 			    "%juMB (%ju %u byte sectors",
4887 			    ((uintmax_t)dp->secsize * dp->sectors) /
4888 			     (1024 * 1024),
4889 			    (uintmax_t)dp->sectors, dp->secsize);
4890 			if (softc->p_type != 0) {
4891 				n += snprintf(announce_buf + n,
4892 				    DA_ANNOUNCETMP_SZ - n,
4893 				    ", DIF type %d", softc->p_type);
4894 			}
4895 			snprintf(announce_buf + n, DA_ANNOUNCETMP_SZ - n, ")");
4896 		}
4897 	} else {
4898 		int error;
4899 
4900 		/*
4901 		 * Retry any UNIT ATTENTION type errors.  They
4902 		 * are expected at boot.
4903 		 */
4904 		error = daerror(done_ccb, CAM_RETRY_SELTO,
4905 				SF_RETRY_UA|SF_NO_PRINT);
4906 		if (error == ERESTART) {
4907 			/*
4908 			 * A retry was scheuled, so
4909 			 * just return.
4910 			 */
4911 			return;
4912 		} else if (error != 0) {
4913 			int asc, ascq;
4914 			int sense_key, error_code;
4915 			int have_sense;
4916 			cam_status status;
4917 			struct ccb_getdev cgd;
4918 
4919 			/* Don't wedge this device's queue */
4920 			status = done_ccb->ccb_h.status;
4921 			if ((status & CAM_DEV_QFRZN) != 0)
4922 				cam_release_devq(done_ccb->ccb_h.path,
4923 						 /*relsim_flags*/0,
4924 						 /*reduction*/0,
4925 						 /*timeout*/0,
4926 						 /*getcount_only*/0);
4927 
4928 			memset(&cgd, 0, sizeof(cgd));
4929 			xpt_setup_ccb(&cgd.ccb_h, done_ccb->ccb_h.path,
4930 				      CAM_PRIORITY_NORMAL);
4931 			cgd.ccb_h.func_code = XPT_GDEV_TYPE;
4932 			xpt_action((union ccb *)&cgd);
4933 
4934 			if (scsi_extract_sense_ccb(done_ccb,
4935 			    &error_code, &sense_key, &asc, &ascq))
4936 				have_sense = TRUE;
4937 			else
4938 				have_sense = FALSE;
4939 
4940 			/*
4941 			 * If we tried READ CAPACITY(16) and failed,
4942 			 * fallback to READ CAPACITY(10).
4943 			 */
4944 			if ((state == DA_CCB_PROBE_RC16) &&
4945 			    (softc->flags & DA_FLAG_CAN_RC16) &&
4946 			    (((csio->ccb_h.status & CAM_STATUS_MASK) ==
4947 				CAM_REQ_INVALID) ||
4948 			     ((have_sense) &&
4949 			      (error_code == SSD_CURRENT_ERROR ||
4950 			       error_code == SSD_DESC_CURRENT_ERROR) &&
4951 			      (sense_key == SSD_KEY_ILLEGAL_REQUEST)))) {
4952 				cam_periph_assert(periph, MA_OWNED);
4953 				softc->flags &= ~DA_FLAG_CAN_RC16;
4954 				free(rdcap, M_SCSIDA);
4955 				softc->state = DA_STATE_PROBE_RC;
4956 				xpt_release_ccb(done_ccb);
4957 				xpt_schedule(periph, priority);
4958 				return;
4959 			}
4960 
4961 			/*
4962 			 * Attach to anything that claims to be a direct access
4963 			 * or optical disk device, as long as it doesn't return
4964 			 * a "Logical unit not supported" (25/0) error.
4965 			 * "Internal Target Failure" (44/0) is also special and
4966 			 * typically means that the device is a SATA drive
4967 			 * behind a SATL translation that's fallen into a
4968 			 * terminally fatal state.
4969 			 *
4970 			 * 25/0: LOGICAL UNIT NOT SUPPORTED
4971 			 * 44/0: INTERNAL TARGET FAILURE
4972 			 * 44/1: PERSISTENT RESERVATION INFORMATION LOST
4973 			 * 44/71: ATA DEVICE FAILED SET FEATURES
4974 			 */
4975 			if ((have_sense)
4976 			 && (asc != 0x25) && (asc != 0x44)
4977 			 && (error_code == SSD_CURRENT_ERROR
4978 			  || error_code == SSD_DESC_CURRENT_ERROR)) {
4979 				const char *sense_key_desc;
4980 				const char *asc_desc;
4981 
4982 				dasetgeom(periph, 512, -1, NULL, 0);
4983 				scsi_sense_desc(sense_key, asc, ascq,
4984 						&cgd.inq_data, &sense_key_desc,
4985 						&asc_desc);
4986 				snprintf(announce_buf, DA_ANNOUNCETMP_SZ,
4987 				    "Attempt to query device "
4988 				    "size failed: %s, %s",
4989 				    sense_key_desc, asc_desc);
4990 			} else {
4991 				if (have_sense)
4992 					scsi_sense_print(&done_ccb->csio);
4993 				else {
4994 					xpt_print(periph->path,
4995 					    "got CAM status %#x\n",
4996 					    done_ccb->ccb_h.status);
4997 				}
4998 
4999 				xpt_print(periph->path, "fatal error, "
5000 				    "failed to attach to device\n");
5001 
5002 				announce_buf = NULL;
5003 
5004 				/*
5005 				 * Free up resources.
5006 				 */
5007 				cam_periph_invalidate(periph);
5008 			}
5009 		}
5010 	}
5011 	free(csio->data_ptr, M_SCSIDA);
5012 	if (announce_buf != NULL &&
5013 	    ((softc->flags & DA_FLAG_ANNOUNCED) == 0)) {
5014 		struct sbuf sb;
5015 
5016 		sbuf_new(&sb, softc->announcebuf, DA_ANNOUNCE_SZ,
5017 		    SBUF_FIXEDLEN);
5018 		xpt_announce_periph_sbuf(periph, &sb, announce_buf);
5019 		xpt_announce_quirks_sbuf(periph, &sb, softc->quirks,
5020 		    DA_Q_BIT_STRING);
5021 		sbuf_finish(&sb);
5022 		sbuf_putbuf(&sb);
5023 
5024 		/*
5025 		 * Create our sysctl variables, now that we know
5026 		 * we have successfully attached.
5027 		 */
5028 		/* increase the refcount */
5029 		if (da_periph_acquire(periph, DA_REF_SYSCTL) == 0) {
5030 			taskqueue_enqueue(taskqueue_thread,
5031 					  &softc->sysctl_task);
5032 		} else {
5033 			/* XXX This message is useless! */
5034 			xpt_print(periph->path, "fatal error, "
5035 			    "could not acquire reference count\n");
5036 		}
5037 	}
5038 
5039 	/* We already probed the device. */
5040 	if (softc->flags & DA_FLAG_PROBED) {
5041 		daprobedone(periph, done_ccb);
5042 		return;
5043 	}
5044 
5045 	/* Ensure re-probe doesn't see old delete. */
5046 	softc->delete_available = 0;
5047 	dadeleteflag(softc, DA_DELETE_ZERO, 1);
5048 	if (lbp && (softc->quirks & DA_Q_NO_UNMAP) == 0) {
5049 		/*
5050 		 * Based on older SBC-3 spec revisions
5051 		 * any of the UNMAP methods "may" be
5052 		 * available via LBP given this flag so
5053 		 * we flag all of them as available and
5054 		 * then remove those which further
5055 		 * probes confirm aren't available
5056 		 * later.
5057 		 *
5058 		 * We could also check readcap(16) p_type
5059 		 * flag to exclude one or more invalid
5060 		 * write same (X) types here
5061 		 */
5062 		dadeleteflag(softc, DA_DELETE_WS16, 1);
5063 		dadeleteflag(softc, DA_DELETE_WS10, 1);
5064 		dadeleteflag(softc, DA_DELETE_UNMAP, 1);
5065 
5066 		softc->state = DA_STATE_PROBE_LBP;
5067 		xpt_release_ccb(done_ccb);
5068 		xpt_schedule(periph, priority);
5069 		return;
5070 	}
5071 
5072 	softc->state = DA_STATE_PROBE_BDC;
5073 	xpt_release_ccb(done_ccb);
5074 	xpt_schedule(periph, priority);
5075 	return;
5076 }
5077 
5078 static void
dadone_probelbp(struct cam_periph * periph,union ccb * done_ccb)5079 dadone_probelbp(struct cam_periph *periph, union ccb *done_ccb)
5080 {
5081 	struct scsi_vpd_logical_block_prov *lbp;
5082 	struct da_softc *softc;
5083 	struct ccb_scsiio *csio;
5084 	uint32_t  priority;
5085 
5086 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone_probelbp\n"));
5087 
5088 	softc = (struct da_softc *)periph->softc;
5089 	priority = done_ccb->ccb_h.pinfo.priority;
5090 	csio = &done_ccb->csio;
5091 	lbp = (struct scsi_vpd_logical_block_prov *)csio->data_ptr;
5092 
5093 	cam_periph_assert(periph, MA_OWNED);
5094 
5095 	if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5096 		/*
5097 		 * T10/1799-D Revision 31 states at least one of these
5098 		 * must be supported but we don't currently enforce this.
5099 		 */
5100 		dadeleteflag(softc, DA_DELETE_WS16,
5101 		     (lbp->flags & SVPD_LBP_WS16));
5102 		dadeleteflag(softc, DA_DELETE_WS10,
5103 			     (lbp->flags & SVPD_LBP_WS10));
5104 		dadeleteflag(softc, DA_DELETE_UNMAP,
5105 			     (lbp->flags & SVPD_LBP_UNMAP));
5106 	} else {
5107 		int error;
5108 		error = daerror(done_ccb, CAM_RETRY_SELTO,
5109 				SF_RETRY_UA|SF_NO_PRINT);
5110 		if (error == ERESTART)
5111 			return;
5112 		else if (error != 0) {
5113 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5114 				/* Don't wedge this device's queue */
5115 				cam_release_devq(done_ccb->ccb_h.path,
5116 						 /*relsim_flags*/0,
5117 						 /*reduction*/0,
5118 						 /*timeout*/0,
5119 						 /*getcount_only*/0);
5120 			}
5121 
5122 			/*
5123 			 * Failure indicates we don't support any SBC-3
5124 			 * delete methods with UNMAP
5125 			 */
5126 		}
5127 	}
5128 
5129 	free(lbp, M_SCSIDA);
5130 	softc->state = DA_STATE_PROBE_BLK_LIMITS;
5131 	xpt_release_ccb(done_ccb);
5132 	xpt_schedule(periph, priority);
5133 	return;
5134 }
5135 
5136 static void
dadone_probeblklimits(struct cam_periph * periph,union ccb * done_ccb)5137 dadone_probeblklimits(struct cam_periph *periph, union ccb *done_ccb)
5138 {
5139 	struct scsi_vpd_block_limits *block_limits;
5140 	struct da_softc *softc;
5141 	struct ccb_scsiio *csio;
5142 	uint32_t  priority;
5143 
5144 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone_probeblklimits\n"));
5145 
5146 	softc = (struct da_softc *)periph->softc;
5147 	priority = done_ccb->ccb_h.pinfo.priority;
5148 	csio = &done_ccb->csio;
5149 	block_limits = (struct scsi_vpd_block_limits *)csio->data_ptr;
5150 
5151 	cam_periph_assert(periph, MA_OWNED);
5152 
5153 	if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5154 		uint32_t max_txfer_len = scsi_4btoul(
5155 			block_limits->max_txfer_len);
5156 		uint32_t max_unmap_lba_cnt = scsi_4btoul(
5157 			block_limits->max_unmap_lba_cnt);
5158 		uint32_t max_unmap_blk_cnt = scsi_4btoul(
5159 			block_limits->max_unmap_blk_cnt);
5160 		uint32_t unmap_gran = scsi_4btoul(
5161 			block_limits->opt_unmap_grain);
5162 		uint32_t unmap_gran_align = scsi_4btoul(
5163 			block_limits->unmap_grain_align);
5164 		uint64_t ws_max_blks = scsi_8btou64(
5165 			block_limits->max_write_same_length);
5166 
5167 		if (max_txfer_len != 0) {
5168 			softc->disk->d_maxsize = MIN(softc->maxio,
5169 			    (off_t)max_txfer_len * softc->params.secsize);
5170 		}
5171 
5172 		/*
5173 		 * We should already support UNMAP but we check lba
5174 		 * and block count to be sure
5175 		 */
5176 		if (max_unmap_lba_cnt != 0x00L &&
5177 		    max_unmap_blk_cnt != 0x00L) {
5178 			softc->unmap_max_lba = max_unmap_lba_cnt;
5179 			softc->unmap_max_ranges = min(max_unmap_blk_cnt,
5180 				UNMAP_MAX_RANGES);
5181 			if (unmap_gran > 1) {
5182 				softc->unmap_gran = unmap_gran;
5183 				if (unmap_gran_align & 0x80000000) {
5184 					softc->unmap_gran_align =
5185 					    unmap_gran_align & 0x7fffffff;
5186 				}
5187 			}
5188 		} else {
5189 			/*
5190 			 * Unexpected UNMAP limits which means the
5191 			 * device doesn't actually support UNMAP
5192 			 */
5193 			dadeleteflag(softc, DA_DELETE_UNMAP, 0);
5194 		}
5195 
5196 		if (ws_max_blks != 0x00L)
5197 			softc->ws_max_blks = ws_max_blks;
5198 	} else {
5199 		int error;
5200 		error = daerror(done_ccb, CAM_RETRY_SELTO,
5201 				SF_RETRY_UA|SF_NO_PRINT);
5202 		if (error == ERESTART)
5203 			return;
5204 		else if (error != 0) {
5205 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5206 				/* Don't wedge this device's queue */
5207 				cam_release_devq(done_ccb->ccb_h.path,
5208 						 /*relsim_flags*/0,
5209 						 /*reduction*/0,
5210 						 /*timeout*/0,
5211 						 /*getcount_only*/0);
5212 			}
5213 
5214 			/*
5215 			 * Failure here doesn't mean UNMAP is not
5216 			 * supported as this is an optional page.
5217 			 */
5218 			softc->unmap_max_lba = 1;
5219 			softc->unmap_max_ranges = 1;
5220 		}
5221 	}
5222 
5223 	free(block_limits, M_SCSIDA);
5224 	softc->state = DA_STATE_PROBE_BDC;
5225 	xpt_release_ccb(done_ccb);
5226 	xpt_schedule(periph, priority);
5227 	return;
5228 }
5229 
5230 static void
dadone_probebdc(struct cam_periph * periph,union ccb * done_ccb)5231 dadone_probebdc(struct cam_periph *periph, union ccb *done_ccb)
5232 {
5233 	struct scsi_vpd_block_device_characteristics *bdc;
5234 	struct da_softc *softc;
5235 	struct ccb_scsiio *csio;
5236 	uint32_t  priority;
5237 
5238 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone_probebdc\n"));
5239 
5240 	softc = (struct da_softc *)periph->softc;
5241 	priority = done_ccb->ccb_h.pinfo.priority;
5242 	csio = &done_ccb->csio;
5243 	bdc = (struct scsi_vpd_block_device_characteristics *)csio->data_ptr;
5244 
5245 	cam_periph_assert(periph, MA_OWNED);
5246 
5247 	if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5248 		uint32_t valid_len;
5249 
5250 		/*
5251 		 * Disable queue sorting for non-rotational media
5252 		 * by default.
5253 		 */
5254 		uint16_t old_rate = softc->disk->d_rotation_rate;
5255 
5256 		valid_len = csio->dxfer_len - csio->resid;
5257 		if (SBDC_IS_PRESENT(bdc, valid_len,
5258 		    medium_rotation_rate)) {
5259 			softc->disk->d_rotation_rate =
5260 				scsi_2btoul(bdc->medium_rotation_rate);
5261 			if (softc->disk->d_rotation_rate == SVPD_NON_ROTATING) {
5262 				cam_iosched_set_sort_queue(
5263 				    softc->cam_iosched, 0);
5264 				softc->flags &= ~DA_FLAG_ROTATING;
5265 			}
5266 			if (softc->disk->d_rotation_rate != old_rate) {
5267 				disk_attr_changed(softc->disk,
5268 				    "GEOM::rotation_rate", M_NOWAIT);
5269 			}
5270 		}
5271 		if ((SBDC_IS_PRESENT(bdc, valid_len, flags))
5272 		 && (softc->zone_mode == DA_ZONE_NONE)) {
5273 			int ata_proto;
5274 
5275 			if (scsi_vpd_supported_page(periph,
5276 			    SVPD_ATA_INFORMATION))
5277 				ata_proto = 1;
5278 			else
5279 				ata_proto = 0;
5280 
5281 			/*
5282 			 * The Zoned field will only be set for
5283 			 * Drive Managed and Host Aware drives.  If
5284 			 * they are Host Managed, the device type
5285 			 * in the standard INQUIRY data should be
5286 			 * set to T_ZBC_HM (0x14).
5287 			 */
5288 			if ((bdc->flags & SVPD_ZBC_MASK) ==
5289 			     SVPD_HAW_ZBC) {
5290 				softc->zone_mode = DA_ZONE_HOST_AWARE;
5291 				softc->zone_interface = (ata_proto) ?
5292 				   DA_ZONE_IF_ATA_SAT : DA_ZONE_IF_SCSI;
5293 			} else if ((bdc->flags & SVPD_ZBC_MASK) ==
5294 			     SVPD_DM_ZBC) {
5295 				softc->zone_mode =DA_ZONE_DRIVE_MANAGED;
5296 				softc->zone_interface = (ata_proto) ?
5297 				   DA_ZONE_IF_ATA_SAT : DA_ZONE_IF_SCSI;
5298 			} else if ((bdc->flags & SVPD_ZBC_MASK) !=
5299 				  SVPD_ZBC_NR) {
5300 				xpt_print(periph->path, "Unknown zoned "
5301 				    "type %#x",
5302 				    bdc->flags & SVPD_ZBC_MASK);
5303 			}
5304 		}
5305 	} else {
5306 		int error;
5307 		error = daerror(done_ccb, CAM_RETRY_SELTO,
5308 				SF_RETRY_UA|SF_NO_PRINT);
5309 		if (error == ERESTART)
5310 			return;
5311 		else if (error != 0) {
5312 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5313 				/* Don't wedge this device's queue */
5314 				cam_release_devq(done_ccb->ccb_h.path,
5315 						 /*relsim_flags*/0,
5316 						 /*reduction*/0,
5317 						 /*timeout*/0,
5318 						 /*getcount_only*/0);
5319 			}
5320 		}
5321 	}
5322 
5323 	free(bdc, M_SCSIDA);
5324 	softc->state = DA_STATE_PROBE_ATA;
5325 	xpt_release_ccb(done_ccb);
5326 	xpt_schedule(periph, priority);
5327 	return;
5328 }
5329 
5330 static void
dadone_probeata(struct cam_periph * periph,union ccb * done_ccb)5331 dadone_probeata(struct cam_periph *periph, union ccb *done_ccb)
5332 {
5333 	struct ata_params *ata_params;
5334 	struct ccb_scsiio *csio;
5335 	struct da_softc *softc;
5336 	uint32_t  priority;
5337 	int continue_probe;
5338 	int error;
5339 
5340 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone_probeata\n"));
5341 
5342 	softc = (struct da_softc *)periph->softc;
5343 	priority = done_ccb->ccb_h.pinfo.priority;
5344 	csio = &done_ccb->csio;
5345 	ata_params = (struct ata_params *)csio->data_ptr;
5346 	continue_probe = 0;
5347 	error = 0;
5348 
5349 	cam_periph_assert(periph, MA_OWNED);
5350 
5351 	if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5352 		uint16_t old_rate;
5353 
5354 		ata_param_fixup(ata_params);
5355 		if (ata_params->support_dsm & ATA_SUPPORT_DSM_TRIM &&
5356 		    (softc->quirks & DA_Q_NO_UNMAP) == 0) {
5357 			dadeleteflag(softc, DA_DELETE_ATA_TRIM, 1);
5358 			if (ata_params->max_dsm_blocks != 0)
5359 				softc->trim_max_ranges = min(
5360 				  softc->trim_max_ranges,
5361 				  ata_params->max_dsm_blocks *
5362 				  ATA_DSM_BLK_RANGES);
5363 		}
5364 		/*
5365 		 * Disable queue sorting for non-rotational media
5366 		 * by default.
5367 		 */
5368 		old_rate = softc->disk->d_rotation_rate;
5369 		softc->disk->d_rotation_rate = ata_params->media_rotation_rate;
5370 		if (softc->disk->d_rotation_rate == ATA_RATE_NON_ROTATING) {
5371 			cam_iosched_set_sort_queue(softc->cam_iosched, 0);
5372 			softc->flags &= ~DA_FLAG_ROTATING;
5373 		}
5374 		if (softc->disk->d_rotation_rate != old_rate) {
5375 			disk_attr_changed(softc->disk,
5376 			    "GEOM::rotation_rate", M_NOWAIT);
5377 		}
5378 
5379 		cam_periph_assert(periph, MA_OWNED);
5380 		if (ata_params->capabilities1 & ATA_SUPPORT_DMA)
5381 			softc->flags |= DA_FLAG_CAN_ATA_DMA;
5382 
5383 		if (ata_params->support.extension & ATA_SUPPORT_GENLOG)
5384 			softc->flags |= DA_FLAG_CAN_ATA_LOG;
5385 
5386 		/*
5387 		 * At this point, if we have a SATA host aware drive,
5388 		 * we communicate via ATA passthrough unless the
5389 		 * SAT layer supports ZBC -> ZAC translation.  In
5390 		 * that case,
5391 		 *
5392 		 * XXX KDM figure out how to detect a host managed
5393 		 * SATA drive.
5394 		 */
5395 		if (softc->zone_mode == DA_ZONE_NONE) {
5396 			/*
5397 			 * Note that we don't override the zone
5398 			 * mode or interface if it has already been
5399 			 * set.  This is because it has either been
5400 			 * set as a quirk, or when we probed the
5401 			 * SCSI Block Device Characteristics page,
5402 			 * the zoned field was set.  The latter
5403 			 * means that the SAT layer supports ZBC to
5404 			 * ZAC translation, and we would prefer to
5405 			 * use that if it is available.
5406 			 */
5407 			if ((ata_params->support3 &
5408 			    ATA_SUPPORT_ZONE_MASK) ==
5409 			    ATA_SUPPORT_ZONE_HOST_AWARE) {
5410 				softc->zone_mode = DA_ZONE_HOST_AWARE;
5411 				softc->zone_interface =
5412 				    DA_ZONE_IF_ATA_PASS;
5413 			} else if ((ata_params->support3 &
5414 				    ATA_SUPPORT_ZONE_MASK) ==
5415 				    ATA_SUPPORT_ZONE_DEV_MANAGED) {
5416 				softc->zone_mode =DA_ZONE_DRIVE_MANAGED;
5417 				softc->zone_interface = DA_ZONE_IF_ATA_PASS;
5418 			}
5419 		}
5420 
5421 	} else {
5422 		error = daerror(done_ccb, CAM_RETRY_SELTO,
5423 				SF_RETRY_UA|SF_NO_PRINT);
5424 		if (error == ERESTART)
5425 			return;
5426 		else if (error != 0) {
5427 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5428 				/* Don't wedge this device's queue */
5429 				cam_release_devq(done_ccb->ccb_h.path,
5430 						 /*relsim_flags*/0,
5431 						 /*reduction*/0,
5432 						 /*timeout*/0,
5433 						 /*getcount_only*/0);
5434 			}
5435 		}
5436 	}
5437 
5438 	if ((softc->zone_mode == DA_ZONE_HOST_AWARE)
5439 	 || (softc->zone_mode == DA_ZONE_HOST_MANAGED)) {
5440 		/*
5441 		 * If the ATA IDENTIFY failed, we could be talking
5442 		 * to a SCSI drive, although that seems unlikely,
5443 		 * since the drive did report that it supported the
5444 		 * ATA Information VPD page.  If the ATA IDENTIFY
5445 		 * succeeded, and the SAT layer doesn't support
5446 		 * ZBC -> ZAC translation, continue on to get the
5447 		 * directory of ATA logs, and complete the rest of
5448 		 * the ZAC probe.  If the SAT layer does support
5449 		 * ZBC -> ZAC translation, we want to use that,
5450 		 * and we'll probe the SCSI Zoned Block Device
5451 		 * Characteristics VPD page next.
5452 		 */
5453 		if ((error == 0)
5454 		 && (softc->flags & DA_FLAG_CAN_ATA_LOG)
5455 		 && (softc->zone_interface == DA_ZONE_IF_ATA_PASS))
5456 			softc->state = DA_STATE_PROBE_ATA_LOGDIR;
5457 		else
5458 			softc->state = DA_STATE_PROBE_ZONE;
5459 		continue_probe = 1;
5460 	}
5461 	if (continue_probe != 0) {
5462 		xpt_schedule(periph, priority);
5463 		xpt_release_ccb(done_ccb);
5464 		return;
5465 	} else
5466 		daprobedone(periph, done_ccb);
5467 	return;
5468 }
5469 
5470 static void
dadone_probeatalogdir(struct cam_periph * periph,union ccb * done_ccb)5471 dadone_probeatalogdir(struct cam_periph *periph, union ccb *done_ccb)
5472 {
5473 	struct da_softc *softc;
5474 	struct ccb_scsiio *csio;
5475 	uint32_t  priority;
5476 	int error;
5477 
5478 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone_probeatalogdir\n"));
5479 
5480 	softc = (struct da_softc *)periph->softc;
5481 	priority = done_ccb->ccb_h.pinfo.priority;
5482 	csio = &done_ccb->csio;
5483 
5484 	cam_periph_assert(periph, MA_OWNED);
5485 	if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5486 		error = 0;
5487 		softc->valid_logdir_len = 0;
5488 		bzero(&softc->ata_logdir, sizeof(softc->ata_logdir));
5489 		softc->valid_logdir_len = csio->dxfer_len - csio->resid;
5490 		if (softc->valid_logdir_len > 0)
5491 			bcopy(csio->data_ptr, &softc->ata_logdir,
5492 			    min(softc->valid_logdir_len,
5493 				sizeof(softc->ata_logdir)));
5494 		/*
5495 		 * Figure out whether the Identify Device log is
5496 		 * supported.  The General Purpose log directory
5497 		 * has a header, and lists the number of pages
5498 		 * available for each GP log identified by the
5499 		 * offset into the list.
5500 		 */
5501 		if ((softc->valid_logdir_len >=
5502 		    ((ATA_IDENTIFY_DATA_LOG + 1) * sizeof(uint16_t)))
5503 		 && (le16dec(softc->ata_logdir.header) ==
5504 		     ATA_GP_LOG_DIR_VERSION)
5505 		 && (le16dec(&softc->ata_logdir.num_pages[
5506 		     (ATA_IDENTIFY_DATA_LOG *
5507 		     sizeof(uint16_t)) - sizeof(uint16_t)]) > 0)){
5508 			softc->flags |= DA_FLAG_CAN_ATA_IDLOG;
5509 		} else {
5510 			softc->flags &= ~DA_FLAG_CAN_ATA_IDLOG;
5511 		}
5512 	} else {
5513 		error = daerror(done_ccb, CAM_RETRY_SELTO,
5514 				SF_RETRY_UA|SF_NO_PRINT);
5515 		if (error == ERESTART)
5516 			return;
5517 		else if (error != 0) {
5518 			/*
5519 			 * If we can't get the ATA log directory,
5520 			 * then ATA logs are effectively not
5521 			 * supported even if the bit is set in the
5522 			 * identify data.
5523 			 */
5524 			softc->flags &= ~(DA_FLAG_CAN_ATA_LOG |
5525 					  DA_FLAG_CAN_ATA_IDLOG);
5526 			if ((done_ccb->ccb_h.status &
5527 			     CAM_DEV_QFRZN) != 0) {
5528 				/* Don't wedge this device's queue */
5529 				cam_release_devq(done_ccb->ccb_h.path,
5530 						 /*relsim_flags*/0,
5531 						 /*reduction*/0,
5532 						 /*timeout*/0,
5533 						 /*getcount_only*/0);
5534 			}
5535 		}
5536 	}
5537 
5538 	free(csio->data_ptr, M_SCSIDA);
5539 
5540 	if ((error == 0)
5541 	 && (softc->flags & DA_FLAG_CAN_ATA_IDLOG)) {
5542 		softc->state = DA_STATE_PROBE_ATA_IDDIR;
5543 		xpt_release_ccb(done_ccb);
5544 		xpt_schedule(periph, priority);
5545 		return;
5546 	}
5547 	daprobedone(periph, done_ccb);
5548 	return;
5549 }
5550 
5551 static void
dadone_probeataiddir(struct cam_periph * periph,union ccb * done_ccb)5552 dadone_probeataiddir(struct cam_periph *periph, union ccb *done_ccb)
5553 {
5554 	struct da_softc *softc;
5555 	struct ccb_scsiio *csio;
5556 	uint32_t  priority;
5557 	int error;
5558 
5559 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone_probeataiddir\n"));
5560 
5561 	softc = (struct da_softc *)periph->softc;
5562 	priority = done_ccb->ccb_h.pinfo.priority;
5563 	csio = &done_ccb->csio;
5564 
5565 	cam_periph_assert(periph, MA_OWNED);
5566 
5567 	if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5568 		off_t entries_offset, max_entries;
5569 		error = 0;
5570 
5571 		softc->valid_iddir_len = 0;
5572 		bzero(&softc->ata_iddir, sizeof(softc->ata_iddir));
5573 		softc->flags &= ~(DA_FLAG_CAN_ATA_SUPCAP |
5574 				  DA_FLAG_CAN_ATA_ZONE);
5575 		softc->valid_iddir_len = csio->dxfer_len - csio->resid;
5576 		if (softc->valid_iddir_len > 0)
5577 			bcopy(csio->data_ptr, &softc->ata_iddir,
5578 			    min(softc->valid_iddir_len,
5579 				sizeof(softc->ata_iddir)));
5580 
5581 		entries_offset =
5582 		    __offsetof(struct ata_identify_log_pages,entries);
5583 		max_entries = softc->valid_iddir_len - entries_offset;
5584 		if ((softc->valid_iddir_len > (entries_offset + 1))
5585 		 && (le64dec(softc->ata_iddir.header) == ATA_IDLOG_REVISION)
5586 		 && (softc->ata_iddir.entry_count > 0)) {
5587 			int num_entries, i;
5588 
5589 			num_entries = softc->ata_iddir.entry_count;
5590 			num_entries = min(num_entries,
5591 			   softc->valid_iddir_len - entries_offset);
5592 			for (i = 0; i < num_entries && i < max_entries; i++) {
5593 				if (softc->ata_iddir.entries[i] ==
5594 				    ATA_IDL_SUP_CAP)
5595 					softc->flags |= DA_FLAG_CAN_ATA_SUPCAP;
5596 				else if (softc->ata_iddir.entries[i] ==
5597 					 ATA_IDL_ZDI)
5598 					softc->flags |= DA_FLAG_CAN_ATA_ZONE;
5599 
5600 				if ((softc->flags & DA_FLAG_CAN_ATA_SUPCAP)
5601 				 && (softc->flags & DA_FLAG_CAN_ATA_ZONE))
5602 					break;
5603 			}
5604 		}
5605 	} else {
5606 		error = daerror(done_ccb, CAM_RETRY_SELTO,
5607 				SF_RETRY_UA|SF_NO_PRINT);
5608 		if (error == ERESTART)
5609 			return;
5610 		else if (error != 0) {
5611 			/*
5612 			 * If we can't get the ATA Identify Data log
5613 			 * directory, then it effectively isn't
5614 			 * supported even if the ATA Log directory
5615 			 * a non-zero number of pages present for
5616 			 * this log.
5617 			 */
5618 			softc->flags &= ~DA_FLAG_CAN_ATA_IDLOG;
5619 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5620 				/* Don't wedge this device's queue */
5621 				cam_release_devq(done_ccb->ccb_h.path,
5622 						 /*relsim_flags*/0,
5623 						 /*reduction*/0,
5624 						 /*timeout*/0,
5625 						 /*getcount_only*/0);
5626 			}
5627 		}
5628 	}
5629 
5630 	free(csio->data_ptr, M_SCSIDA);
5631 
5632 	if ((error == 0) && (softc->flags & DA_FLAG_CAN_ATA_SUPCAP)) {
5633 		softc->state = DA_STATE_PROBE_ATA_SUP;
5634 		xpt_release_ccb(done_ccb);
5635 		xpt_schedule(periph, priority);
5636 		return;
5637 	}
5638 	daprobedone(periph, done_ccb);
5639 	return;
5640 }
5641 
5642 static void
dadone_probeatasup(struct cam_periph * periph,union ccb * done_ccb)5643 dadone_probeatasup(struct cam_periph *periph, union ccb *done_ccb)
5644 {
5645 	struct da_softc *softc;
5646 	struct ccb_scsiio *csio;
5647 	uint32_t  priority;
5648 	int error;
5649 
5650 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone_probeatasup\n"));
5651 
5652 	softc = (struct da_softc *)periph->softc;
5653 	priority = done_ccb->ccb_h.pinfo.priority;
5654 	csio = &done_ccb->csio;
5655 
5656 	cam_periph_assert(periph, MA_OWNED);
5657 
5658 	if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5659 		uint32_t valid_len;
5660 		size_t needed_size;
5661 		struct ata_identify_log_sup_cap *sup_cap;
5662 		error = 0;
5663 
5664 		sup_cap = (struct ata_identify_log_sup_cap *)csio->data_ptr;
5665 		valid_len = csio->dxfer_len - csio->resid;
5666 		needed_size = __offsetof(struct ata_identify_log_sup_cap,
5667 		    sup_zac_cap) + 1 + sizeof(sup_cap->sup_zac_cap);
5668 		if (valid_len >= needed_size) {
5669 			uint64_t zoned, zac_cap;
5670 
5671 			zoned = le64dec(sup_cap->zoned_cap);
5672 			if (zoned & ATA_ZONED_VALID) {
5673 				/*
5674 				 * This should have already been
5675 				 * set, because this is also in the
5676 				 * ATA identify data.
5677 				 */
5678 				if ((zoned & ATA_ZONED_MASK) ==
5679 				    ATA_SUPPORT_ZONE_HOST_AWARE)
5680 					softc->zone_mode = DA_ZONE_HOST_AWARE;
5681 				else if ((zoned & ATA_ZONED_MASK) ==
5682 				    ATA_SUPPORT_ZONE_DEV_MANAGED)
5683 					softc->zone_mode =
5684 					    DA_ZONE_DRIVE_MANAGED;
5685 			}
5686 
5687 			zac_cap = le64dec(sup_cap->sup_zac_cap);
5688 			if (zac_cap & ATA_SUP_ZAC_CAP_VALID) {
5689 				if (zac_cap & ATA_REPORT_ZONES_SUP)
5690 					softc->zone_flags |=
5691 					    DA_ZONE_FLAG_RZ_SUP;
5692 				if (zac_cap & ATA_ND_OPEN_ZONE_SUP)
5693 					softc->zone_flags |=
5694 					    DA_ZONE_FLAG_OPEN_SUP;
5695 				if (zac_cap & ATA_ND_CLOSE_ZONE_SUP)
5696 					softc->zone_flags |=
5697 					    DA_ZONE_FLAG_CLOSE_SUP;
5698 				if (zac_cap & ATA_ND_FINISH_ZONE_SUP)
5699 					softc->zone_flags |=
5700 					    DA_ZONE_FLAG_FINISH_SUP;
5701 				if (zac_cap & ATA_ND_RWP_SUP)
5702 					softc->zone_flags |=
5703 					    DA_ZONE_FLAG_RWP_SUP;
5704 			} else {
5705 				/*
5706 				 * This field was introduced in
5707 				 * ACS-4, r08 on April 28th, 2015.
5708 				 * If the drive firmware was written
5709 				 * to an earlier spec, it won't have
5710 				 * the field.  So, assume all
5711 				 * commands are supported.
5712 				 */
5713 				softc->zone_flags |= DA_ZONE_FLAG_SUP_MASK;
5714 			}
5715 		}
5716 	} else {
5717 		error = daerror(done_ccb, CAM_RETRY_SELTO,
5718 				SF_RETRY_UA|SF_NO_PRINT);
5719 		if (error == ERESTART)
5720 			return;
5721 		else if (error != 0) {
5722 			/*
5723 			 * If we can't get the ATA Identify Data
5724 			 * Supported Capabilities page, clear the
5725 			 * flag...
5726 			 */
5727 			softc->flags &= ~DA_FLAG_CAN_ATA_SUPCAP;
5728 			/*
5729 			 * And clear zone capabilities.
5730 			 */
5731 			softc->zone_flags &= ~DA_ZONE_FLAG_SUP_MASK;
5732 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5733 				/* Don't wedge this device's queue */
5734 				cam_release_devq(done_ccb->ccb_h.path,
5735 						 /*relsim_flags*/0,
5736 						 /*reduction*/0,
5737 						 /*timeout*/0,
5738 						 /*getcount_only*/0);
5739 			}
5740 		}
5741 	}
5742 
5743 	free(csio->data_ptr, M_SCSIDA);
5744 
5745 	if ((error == 0) && (softc->flags & DA_FLAG_CAN_ATA_ZONE)) {
5746 		softc->state = DA_STATE_PROBE_ATA_ZONE;
5747 		xpt_release_ccb(done_ccb);
5748 		xpt_schedule(periph, priority);
5749 		return;
5750 	}
5751 	daprobedone(periph, done_ccb);
5752 	return;
5753 }
5754 
5755 static void
dadone_probeatazone(struct cam_periph * periph,union ccb * done_ccb)5756 dadone_probeatazone(struct cam_periph *periph, union ccb *done_ccb)
5757 {
5758 	struct da_softc *softc;
5759 	struct ccb_scsiio *csio;
5760 	int error;
5761 
5762 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone_probeatazone\n"));
5763 
5764 	softc = (struct da_softc *)periph->softc;
5765 	csio = &done_ccb->csio;
5766 
5767 	cam_periph_assert(periph, MA_OWNED);
5768 
5769 	if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5770 		struct ata_zoned_info_log *zi_log;
5771 		uint32_t valid_len;
5772 		size_t needed_size;
5773 
5774 		zi_log = (struct ata_zoned_info_log *)csio->data_ptr;
5775 
5776 		valid_len = csio->dxfer_len - csio->resid;
5777 		needed_size = __offsetof(struct ata_zoned_info_log,
5778 		    version_info) + 1 + sizeof(zi_log->version_info);
5779 		if (valid_len >= needed_size) {
5780 			uint64_t tmpvar;
5781 
5782 			tmpvar = le64dec(zi_log->zoned_cap);
5783 			if (tmpvar & ATA_ZDI_CAP_VALID) {
5784 				if (tmpvar & ATA_ZDI_CAP_URSWRZ)
5785 					softc->zone_flags |=
5786 					    DA_ZONE_FLAG_URSWRZ;
5787 				else
5788 					softc->zone_flags &=
5789 					    ~DA_ZONE_FLAG_URSWRZ;
5790 			}
5791 			tmpvar = le64dec(zi_log->optimal_seq_zones);
5792 			if (tmpvar & ATA_ZDI_OPT_SEQ_VALID) {
5793 				softc->zone_flags |= DA_ZONE_FLAG_OPT_SEQ_SET;
5794 				softc->optimal_seq_zones = (tmpvar &
5795 				    ATA_ZDI_OPT_SEQ_MASK);
5796 			} else {
5797 				softc->zone_flags &= ~DA_ZONE_FLAG_OPT_SEQ_SET;
5798 				softc->optimal_seq_zones = 0;
5799 			}
5800 
5801 			tmpvar =le64dec(zi_log->optimal_nonseq_zones);
5802 			if (tmpvar & ATA_ZDI_OPT_NS_VALID) {
5803 				softc->zone_flags |=
5804 				    DA_ZONE_FLAG_OPT_NONSEQ_SET;
5805 				softc->optimal_nonseq_zones =
5806 				    (tmpvar & ATA_ZDI_OPT_NS_MASK);
5807 			} else {
5808 				softc->zone_flags &=
5809 				    ~DA_ZONE_FLAG_OPT_NONSEQ_SET;
5810 				softc->optimal_nonseq_zones = 0;
5811 			}
5812 
5813 			tmpvar = le64dec(zi_log->max_seq_req_zones);
5814 			if (tmpvar & ATA_ZDI_MAX_SEQ_VALID) {
5815 				softc->zone_flags |= DA_ZONE_FLAG_MAX_SEQ_SET;
5816 				softc->max_seq_zones =
5817 				    (tmpvar & ATA_ZDI_MAX_SEQ_MASK);
5818 			} else {
5819 				softc->zone_flags &= ~DA_ZONE_FLAG_MAX_SEQ_SET;
5820 				softc->max_seq_zones = 0;
5821 			}
5822 		}
5823 	} else {
5824 		error = daerror(done_ccb, CAM_RETRY_SELTO,
5825 				SF_RETRY_UA|SF_NO_PRINT);
5826 		if (error == ERESTART)
5827 			return;
5828 		else if (error != 0) {
5829 			softc->flags &= ~DA_FLAG_CAN_ATA_ZONE;
5830 			softc->flags &= ~DA_ZONE_FLAG_SET_MASK;
5831 
5832 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5833 				/* Don't wedge this device's queue */
5834 				cam_release_devq(done_ccb->ccb_h.path,
5835 						 /*relsim_flags*/0,
5836 						 /*reduction*/0,
5837 						 /*timeout*/0,
5838 						 /*getcount_only*/0);
5839 			}
5840 		}
5841 	}
5842 
5843 	free(csio->data_ptr, M_SCSIDA);
5844 
5845 	daprobedone(periph, done_ccb);
5846 	return;
5847 }
5848 
5849 static void
dadone_probezone(struct cam_periph * periph,union ccb * done_ccb)5850 dadone_probezone(struct cam_periph *periph, union ccb *done_ccb)
5851 {
5852 	struct da_softc *softc;
5853 	struct ccb_scsiio *csio;
5854 	int error;
5855 
5856 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone_probezone\n"));
5857 
5858 	softc = (struct da_softc *)periph->softc;
5859 	csio = &done_ccb->csio;
5860 
5861 	cam_periph_assert(periph, MA_OWNED);
5862 
5863 	if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5864 		uint32_t valid_len;
5865 		size_t needed_len;
5866 		struct scsi_vpd_zoned_bdc *zoned_bdc;
5867 
5868 		error = 0;
5869 		zoned_bdc = (struct scsi_vpd_zoned_bdc *)csio->data_ptr;
5870 		valid_len = csio->dxfer_len - csio->resid;
5871 		needed_len = __offsetof(struct scsi_vpd_zoned_bdc,
5872 		    max_seq_req_zones) + 1 +
5873 		    sizeof(zoned_bdc->max_seq_req_zones);
5874 		if ((valid_len >= needed_len)
5875 		 && (scsi_2btoul(zoned_bdc->page_length) >= SVPD_ZBDC_PL)) {
5876 			if (zoned_bdc->flags & SVPD_ZBDC_URSWRZ)
5877 				softc->zone_flags |= DA_ZONE_FLAG_URSWRZ;
5878 			else
5879 				softc->zone_flags &= ~DA_ZONE_FLAG_URSWRZ;
5880 			softc->optimal_seq_zones =
5881 			    scsi_4btoul(zoned_bdc->optimal_seq_zones);
5882 			softc->zone_flags |= DA_ZONE_FLAG_OPT_SEQ_SET;
5883 			softc->optimal_nonseq_zones = scsi_4btoul(
5884 			    zoned_bdc->optimal_nonseq_zones);
5885 			softc->zone_flags |= DA_ZONE_FLAG_OPT_NONSEQ_SET;
5886 			softc->max_seq_zones =
5887 			    scsi_4btoul(zoned_bdc->max_seq_req_zones);
5888 			softc->zone_flags |= DA_ZONE_FLAG_MAX_SEQ_SET;
5889 		}
5890 		/*
5891 		 * All of the zone commands are mandatory for SCSI
5892 		 * devices.
5893 		 *
5894 		 * XXX KDM this is valid as of September 2015.
5895 		 * Re-check this assumption once the SAT spec is
5896 		 * updated to support SCSI ZBC to ATA ZAC mapping.
5897 		 * Since ATA allows zone commands to be reported
5898 		 * as supported or not, this may not necessarily
5899 		 * be true for an ATA device behind a SAT (SCSI to
5900 		 * ATA Translation) layer.
5901 		 */
5902 		softc->zone_flags |= DA_ZONE_FLAG_SUP_MASK;
5903 	} else {
5904 		error = daerror(done_ccb, CAM_RETRY_SELTO,
5905 				SF_RETRY_UA|SF_NO_PRINT);
5906 		if (error == ERESTART)
5907 			return;
5908 		else if (error != 0) {
5909 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5910 				/* Don't wedge this device's queue */
5911 				cam_release_devq(done_ccb->ccb_h.path,
5912 						 /*relsim_flags*/0,
5913 						 /*reduction*/0,
5914 						 /*timeout*/0,
5915 						 /*getcount_only*/0);
5916 			}
5917 		}
5918 	}
5919 
5920 	free(csio->data_ptr, M_SCSIDA);
5921 
5922 	daprobedone(periph, done_ccb);
5923 	return;
5924 }
5925 
5926 static void
dadone_tur(struct cam_periph * periph,union ccb * done_ccb)5927 dadone_tur(struct cam_periph *periph, union ccb *done_ccb)
5928 {
5929 	struct da_softc *softc;
5930 
5931 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone_tur\n"));
5932 
5933 	softc = (struct da_softc *)periph->softc;
5934 
5935 	cam_periph_assert(periph, MA_OWNED);
5936 
5937 	if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5938 		if (daerror(done_ccb, CAM_RETRY_SELTO,
5939 		    SF_RETRY_UA | SF_NO_RECOVERY | SF_NO_PRINT) == ERESTART)
5940 			return;	/* Will complete again, keep reference */
5941 		if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
5942 			cam_release_devq(done_ccb->ccb_h.path,
5943 					 /*relsim_flags*/0,
5944 					 /*reduction*/0,
5945 					 /*timeout*/0,
5946 					 /*getcount_only*/0);
5947 	}
5948 	softc->flags &= ~DA_FLAG_TUR_PENDING;
5949 	xpt_release_ccb(done_ccb);
5950 	da_periph_release_locked(periph, DA_REF_TUR);
5951 	return;
5952 }
5953 
5954 static void
dareprobe(struct cam_periph * periph)5955 dareprobe(struct cam_periph *periph)
5956 {
5957 	struct da_softc	  *softc;
5958 	int status __diagused;
5959 
5960 	softc = (struct da_softc *)periph->softc;
5961 
5962 	cam_periph_assert(periph, MA_OWNED);
5963 
5964 	/* Probe in progress; don't interfere. */
5965 	if (softc->state != DA_STATE_NORMAL)
5966 		return;
5967 
5968 	status = da_periph_acquire(periph, DA_REF_REPROBE);
5969 	KASSERT(status == 0, ("dareprobe: cam_periph_acquire failed"));
5970 
5971 	softc->state = DA_STATE_PROBE_WP;
5972 	xpt_schedule(periph, CAM_PRIORITY_DEV);
5973 }
5974 
5975 static int
daerror(union ccb * ccb,uint32_t cam_flags,uint32_t sense_flags)5976 daerror(union ccb *ccb, uint32_t cam_flags, uint32_t sense_flags)
5977 {
5978 	struct da_softc	  *softc;
5979 	struct cam_periph *periph;
5980 	int error, error_code, sense_key, asc, ascq;
5981 
5982 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
5983 	if (ccb->csio.bio != NULL)
5984 		biotrack(ccb->csio.bio, __func__);
5985 #endif
5986 
5987 	periph = xpt_path_periph(ccb->ccb_h.path);
5988 	softc = (struct da_softc *)periph->softc;
5989 
5990 	cam_periph_assert(periph, MA_OWNED);
5991 
5992 	/*
5993 	 * Automatically detect devices that do not support
5994 	 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
5995 	 */
5996 	error = 0;
5997 	if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
5998 		error = cmd6workaround(ccb);
5999 	} else if (scsi_extract_sense_ccb(ccb,
6000 	    &error_code, &sense_key, &asc, &ascq)) {
6001 		if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
6002 			error = cmd6workaround(ccb);
6003 		/*
6004 		 * If the target replied with CAPACITY DATA HAS CHANGED UA,
6005 		 * query the capacity and notify upper layers.
6006 		 */
6007 		else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
6008 		    asc == 0x2A && ascq == 0x09) {
6009 			/* 2a/9: CAPACITY DATA HAS CHANGED */
6010 			xpt_print(periph->path, "Capacity data has changed\n");
6011 			softc->flags &= ~DA_FLAG_PROBED;
6012 			dareprobe(periph);
6013 			sense_flags |= SF_NO_PRINT;
6014 		} else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
6015 		    asc == 0x28 && ascq == 0x00) {
6016 			/* 28/0: NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED */
6017 			softc->flags &= ~DA_FLAG_PROBED;
6018 			disk_media_changed(softc->disk, M_NOWAIT);
6019 		} else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
6020 		    asc == 0x3F && ascq == 0x03) {
6021 			/* 3f/3: INQUIRY DATA HAS CHANGED */
6022 			xpt_print(periph->path, "INQUIRY data has changed\n");
6023 			softc->flags &= ~DA_FLAG_PROBED;
6024 			dareprobe(periph);
6025 			sense_flags |= SF_NO_PRINT;
6026 		} else if (sense_key == SSD_KEY_NOT_READY &&
6027 		    asc == 0x3a && (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
6028 			/* 3a/0: MEDIUM NOT PRESENT */
6029 			/* 3a/1: MEDIUM NOT PRESENT - TRAY CLOSED */
6030 			/* 3a/2: MEDIUM NOT PRESENT - TRAY OPEN */
6031 			/* 3a/3: MEDIUM NOT PRESENT - LOADABLE */
6032 			/* 3a/4: MEDIUM NOT PRESENT - MEDIUM AUXILIARY MEMORY ACCESSIBLE */
6033 			softc->flags |= DA_FLAG_PACK_INVALID;
6034 			disk_media_gone(softc->disk, M_NOWAIT);
6035 		}
6036 	}
6037 	if (error == ERESTART)
6038 		return (ERESTART);
6039 
6040 #ifdef CAM_IO_STATS
6041 	switch (ccb->ccb_h.status & CAM_STATUS_MASK) {
6042 	case CAM_CMD_TIMEOUT:
6043 		softc->timeouts++;
6044 		break;
6045 	case CAM_REQ_ABORTED:
6046 	case CAM_REQ_CMP_ERR:
6047 	case CAM_REQ_TERMIO:
6048 	case CAM_UNREC_HBA_ERROR:
6049 	case CAM_DATA_RUN_ERR:
6050 	case CAM_SCSI_STATUS_ERROR:
6051 	case CAM_ATA_STATUS_ERROR:
6052 		softc->errors++;
6053 		break;
6054 	default:
6055 		break;
6056 	}
6057 #endif
6058 
6059 	/*
6060 	 * XXX
6061 	 * Until we have a better way of doing pack validation,
6062 	 * don't treat UAs as errors.
6063 	 */
6064 	sense_flags |= SF_RETRY_UA;
6065 
6066 	if (softc->quirks & DA_Q_RETRY_BUSY)
6067 		sense_flags |= SF_RETRY_BUSY;
6068 	return(cam_periph_error(ccb, cam_flags, sense_flags));
6069 }
6070 
6071 static void
damediapoll(void * arg)6072 damediapoll(void *arg)
6073 {
6074 	struct cam_periph *periph = arg;
6075 	struct da_softc *softc = periph->softc;
6076 
6077 	if (!cam_iosched_has_work_flags(softc->cam_iosched, DA_WORK_TUR) &&
6078 	    (softc->flags & DA_FLAG_TUR_PENDING) == 0 &&
6079 	    softc->state == DA_STATE_NORMAL &&
6080 	    LIST_EMPTY(&softc->pending_ccbs)) {
6081 		if (da_periph_acquire(periph, DA_REF_TUR) == 0) {
6082 			cam_iosched_set_work_flags(softc->cam_iosched, DA_WORK_TUR);
6083 			daschedule(periph);
6084 		}
6085 	}
6086 
6087 	/* Queue us up again */
6088 	if (da_poll_period != 0) {
6089 		callout_schedule_sbt(&softc->mediapoll_c,
6090 		    da_poll_period * SBT_1S, 0, C_PREL(1));
6091 	}
6092 }
6093 
6094 static void
daprevent(struct cam_periph * periph,int action)6095 daprevent(struct cam_periph *periph, int action)
6096 {
6097 	struct	da_softc *softc;
6098 	union	ccb *ccb;
6099 	int	error;
6100 
6101 	cam_periph_assert(periph, MA_OWNED);
6102 	softc = (struct da_softc *)periph->softc;
6103 
6104 	if (((action == PR_ALLOW)
6105 	  && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
6106 	 || ((action == PR_PREVENT)
6107 	  && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
6108 		return;
6109 	}
6110 
6111 	ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
6112 
6113 	scsi_prevent(&ccb->csio,
6114 		     /*retries*/1,
6115 		     /*cbcfp*/NULL,
6116 		     MSG_SIMPLE_Q_TAG,
6117 		     action,
6118 		     SSD_FULL_SIZE,
6119 		     5000);
6120 
6121 	error = cam_periph_runccb(ccb, daerror, CAM_RETRY_SELTO,
6122 	    SF_RETRY_UA | SF_NO_PRINT, softc->disk->d_devstat);
6123 
6124 	if (error == 0) {
6125 		if (action == PR_ALLOW)
6126 			softc->flags &= ~DA_FLAG_PACK_LOCKED;
6127 		else
6128 			softc->flags |= DA_FLAG_PACK_LOCKED;
6129 	}
6130 
6131 	xpt_release_ccb(ccb);
6132 }
6133 
6134 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)6135 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector,
6136 	  struct scsi_read_capacity_data_long *rcaplong, size_t rcap_len)
6137 {
6138 	struct ccb_calc_geometry ccg;
6139 	struct da_softc *softc;
6140 	struct disk_params *dp;
6141 	u_int lbppbe, lalba;
6142 	int error;
6143 
6144 	softc = (struct da_softc *)periph->softc;
6145 
6146 	dp = &softc->params;
6147 	dp->secsize = block_len;
6148 	dp->sectors = maxsector + 1;
6149 	if (rcaplong != NULL) {
6150 		lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
6151 		lalba = scsi_2btoul(rcaplong->lalba_lbp);
6152 		lalba &= SRC16_LALBA_A;
6153 		if (rcaplong->prot & SRC16_PROT_EN)
6154 			softc->p_type = ((rcaplong->prot & SRC16_P_TYPE) >>
6155 			    SRC16_P_TYPE_SHIFT) + 1;
6156 		else
6157 			softc->p_type = 0;
6158 	} else {
6159 		lbppbe = 0;
6160 		lalba = 0;
6161 		softc->p_type = 0;
6162 	}
6163 
6164 	if (lbppbe > 0) {
6165 		dp->stripesize = block_len << lbppbe;
6166 		dp->stripeoffset = (dp->stripesize - block_len * lalba) %
6167 		    dp->stripesize;
6168 	} else if (softc->quirks & DA_Q_4K) {
6169 		dp->stripesize = 4096;
6170 		dp->stripeoffset = 0;
6171 	} else if (softc->unmap_gran != 0) {
6172 		dp->stripesize = block_len * softc->unmap_gran;
6173 		dp->stripeoffset = (dp->stripesize - block_len *
6174 		    softc->unmap_gran_align) % dp->stripesize;
6175 	} else {
6176 		dp->stripesize = 0;
6177 		dp->stripeoffset = 0;
6178 	}
6179 	/*
6180 	 * Have the controller provide us with a geometry
6181 	 * for this disk.  The only time the geometry
6182 	 * matters is when we boot and the controller
6183 	 * is the only one knowledgeable enough to come
6184 	 * up with something that will make this a bootable
6185 	 * device.
6186 	 */
6187 	memset(&ccg, 0, sizeof(ccg));
6188 	xpt_setup_ccb(&ccg.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
6189 	ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
6190 	ccg.block_size = dp->secsize;
6191 	ccg.volume_size = dp->sectors;
6192 	ccg.heads = 0;
6193 	ccg.secs_per_track = 0;
6194 	ccg.cylinders = 0;
6195 	xpt_action((union ccb*)&ccg);
6196 	if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
6197 		/*
6198 		 * We don't know what went wrong here- but just pick
6199 		 * a geometry so we don't have nasty things like divide
6200 		 * by zero.
6201 		 */
6202 		dp->heads = 255;
6203 		dp->secs_per_track = 255;
6204 		dp->cylinders = dp->sectors / (255 * 255);
6205 		if (dp->cylinders == 0) {
6206 			dp->cylinders = 1;
6207 		}
6208 	} else {
6209 		dp->heads = ccg.heads;
6210 		dp->secs_per_track = ccg.secs_per_track;
6211 		dp->cylinders = ccg.cylinders;
6212 	}
6213 
6214 	/*
6215 	 * If the user supplied a read capacity buffer, and if it is
6216 	 * different than the previous buffer, update the data in the EDT.
6217 	 * If it's the same, we don't bother.  This avoids sending an
6218 	 * update every time someone opens this device.
6219 	 */
6220 	if ((rcaplong != NULL)
6221 	 && (bcmp(rcaplong, &softc->rcaplong,
6222 		  min(sizeof(softc->rcaplong), rcap_len)) != 0)) {
6223 		struct ccb_dev_advinfo cdai;
6224 
6225 		memset(&cdai, 0, sizeof(cdai));
6226 		xpt_setup_ccb(&cdai.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
6227 		cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
6228 		cdai.buftype = CDAI_TYPE_RCAPLONG;
6229 		cdai.flags = CDAI_FLAG_STORE;
6230 		cdai.bufsiz = rcap_len;
6231 		cdai.buf = (uint8_t *)rcaplong;
6232 		xpt_action((union ccb *)&cdai);
6233 		if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
6234 			cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
6235 		if (cdai.ccb_h.status != CAM_REQ_CMP) {
6236 			xpt_print(periph->path, "%s: failed to set read "
6237 				  "capacity advinfo\n", __func__);
6238 			/* Use cam_error_print() to decode the status */
6239 			cam_error_print((union ccb *)&cdai, CAM_ESF_CAM_STATUS,
6240 					CAM_EPF_ALL);
6241 		} else {
6242 			bcopy(rcaplong, &softc->rcaplong,
6243 			      min(sizeof(softc->rcaplong), rcap_len));
6244 		}
6245 	}
6246 
6247 	softc->disk->d_sectorsize = softc->params.secsize;
6248 	softc->disk->d_mediasize = softc->params.secsize * (off_t)softc->params.sectors;
6249 	softc->disk->d_stripesize = softc->params.stripesize;
6250 	softc->disk->d_stripeoffset = softc->params.stripeoffset;
6251 	/* XXX: these are not actually "firmware" values, so they may be wrong */
6252 	softc->disk->d_fwsectors = softc->params.secs_per_track;
6253 	softc->disk->d_fwheads = softc->params.heads;
6254 	softc->disk->d_devstat->block_size = softc->params.secsize;
6255 	softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
6256 
6257 	error = disk_resize(softc->disk, M_NOWAIT);
6258 	if (error != 0)
6259 		xpt_print(periph->path, "disk_resize(9) failed, error = %d\n", error);
6260 }
6261 
6262 static void
dasendorderedtag(void * arg)6263 dasendorderedtag(void *arg)
6264 {
6265 	struct cam_periph *periph = arg;
6266 	struct da_softc *softc = periph->softc;
6267 
6268 	cam_periph_assert(periph, MA_OWNED);
6269 	if (da_send_ordered) {
6270 		if (!LIST_EMPTY(&softc->pending_ccbs)) {
6271 			if ((softc->flags & DA_FLAG_WAS_OTAG) == 0)
6272 				softc->flags |= DA_FLAG_NEED_OTAG;
6273 			softc->flags &= ~DA_FLAG_WAS_OTAG;
6274 		}
6275 	}
6276 
6277 	/* Queue us up again */
6278 	callout_schedule_sbt(&softc->sendordered_c,
6279 	    SBT_1S / DA_ORDEREDTAG_INTERVAL * da_default_timeout, 0,
6280 	    C_PREL(1));
6281 }
6282 
6283 /*
6284  * Step through all DA peripheral drivers, and if the device is still open,
6285  * sync the disk cache to physical media.
6286  */
6287 static void
dashutdown(void * arg,int howto)6288 dashutdown(void * arg, int howto)
6289 {
6290 	struct cam_periph *periph;
6291 	struct da_softc *softc;
6292 	union ccb *ccb;
6293 	int error;
6294 
6295 	if ((howto & RB_NOSYNC) != 0)
6296 		return;
6297 
6298 	CAM_PERIPH_FOREACH(periph, &dadriver) {
6299 		softc = (struct da_softc *)periph->softc;
6300 		if (SCHEDULER_STOPPED()) {
6301 			/* If we paniced with the lock held, do not recurse. */
6302 			if (!cam_periph_owned(periph) &&
6303 			    (softc->flags & DA_FLAG_OPEN)) {
6304 				dadump(softc->disk, NULL, 0, 0);
6305 			}
6306 			continue;
6307 		}
6308 		cam_periph_lock(periph);
6309 
6310 		/*
6311 		 * We only sync the cache if the drive is still open, and
6312 		 * if the drive is capable of it..
6313 		 */
6314 		if (((softc->flags & DA_FLAG_OPEN) == 0)
6315 		 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
6316 			cam_periph_unlock(periph);
6317 			continue;
6318 		}
6319 
6320 		ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
6321 		scsi_synchronize_cache(&ccb->csio,
6322 				       /*retries*/0,
6323 				       /*cbfcnp*/NULL,
6324 				       MSG_SIMPLE_Q_TAG,
6325 				       /*begin_lba*/0, /* whole disk */
6326 				       /*lb_count*/0,
6327 				       SSD_FULL_SIZE,
6328 				       60 * 60 * 1000);
6329 
6330 		error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
6331 		    /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR,
6332 		    softc->disk->d_devstat);
6333 		if (error != 0)
6334 			xpt_print(periph->path, "Synchronize cache failed\n");
6335 		xpt_release_ccb(ccb);
6336 		cam_periph_unlock(periph);
6337 	}
6338 }
6339 
6340 #else /* !_KERNEL */
6341 
6342 /*
6343  * XXX These are only left out of the kernel build to silence warnings.  If,
6344  * for some reason these functions are used in the kernel, the ifdefs should
6345  * be moved so they are included both in the kernel and userland.
6346  */
6347 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)6348 scsi_format_unit(struct ccb_scsiio *csio, uint32_t retries,
6349 		 void (*cbfcnp)(struct cam_periph *, union ccb *),
6350 		 uint8_t tag_action, uint8_t byte2, uint16_t ileave,
6351 		 uint8_t *data_ptr, uint32_t dxfer_len, uint8_t sense_len,
6352 		 uint32_t timeout)
6353 {
6354 	struct scsi_format_unit *scsi_cmd;
6355 
6356 	scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
6357 	scsi_cmd->opcode = FORMAT_UNIT;
6358 	scsi_cmd->byte2 = byte2;
6359 	scsi_ulto2b(ileave, scsi_cmd->interleave);
6360 
6361 	cam_fill_csio(csio,
6362 		      retries,
6363 		      cbfcnp,
6364 		      /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
6365 		      tag_action,
6366 		      data_ptr,
6367 		      dxfer_len,
6368 		      sense_len,
6369 		      sizeof(*scsi_cmd),
6370 		      timeout);
6371 }
6372 
6373 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)6374 scsi_read_defects(struct ccb_scsiio *csio, uint32_t retries,
6375 		  void (*cbfcnp)(struct cam_periph *, union ccb *),
6376 		  uint8_t tag_action, uint8_t list_format,
6377 		  uint32_t addr_desc_index, uint8_t *data_ptr,
6378 		  uint32_t dxfer_len, int minimum_cmd_size,
6379 		  uint8_t sense_len, uint32_t timeout)
6380 {
6381 	uint8_t cdb_len;
6382 
6383 	/*
6384 	 * These conditions allow using the 10 byte command.  Otherwise we
6385 	 * need to use the 12 byte command.
6386 	 */
6387 	if ((minimum_cmd_size <= 10)
6388 	 && (addr_desc_index == 0)
6389 	 && (dxfer_len <= SRDD10_MAX_LENGTH)) {
6390 		struct scsi_read_defect_data_10 *cdb10;
6391 
6392 		cdb10 = (struct scsi_read_defect_data_10 *)
6393 			&csio->cdb_io.cdb_bytes;
6394 
6395 		cdb_len = sizeof(*cdb10);
6396 		bzero(cdb10, cdb_len);
6397                 cdb10->opcode = READ_DEFECT_DATA_10;
6398                 cdb10->format = list_format;
6399                 scsi_ulto2b(dxfer_len, cdb10->alloc_length);
6400 	} else {
6401 		struct scsi_read_defect_data_12 *cdb12;
6402 
6403 		cdb12 = (struct scsi_read_defect_data_12 *)
6404 			&csio->cdb_io.cdb_bytes;
6405 
6406 		cdb_len = sizeof(*cdb12);
6407 		bzero(cdb12, cdb_len);
6408                 cdb12->opcode = READ_DEFECT_DATA_12;
6409                 cdb12->format = list_format;
6410                 scsi_ulto4b(dxfer_len, cdb12->alloc_length);
6411 		scsi_ulto4b(addr_desc_index, cdb12->address_descriptor_index);
6412 	}
6413 
6414 	cam_fill_csio(csio,
6415 		      retries,
6416 		      cbfcnp,
6417 		      /*flags*/ CAM_DIR_IN,
6418 		      tag_action,
6419 		      data_ptr,
6420 		      dxfer_len,
6421 		      sense_len,
6422 		      cdb_len,
6423 		      timeout);
6424 }
6425 
6426 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)6427 scsi_sanitize(struct ccb_scsiio *csio, uint32_t retries,
6428 	      void (*cbfcnp)(struct cam_periph *, union ccb *),
6429 	      uint8_t tag_action, uint8_t byte2, uint16_t control,
6430 	      uint8_t *data_ptr, uint32_t dxfer_len, uint8_t sense_len,
6431 	      uint32_t timeout)
6432 {
6433 	struct scsi_sanitize *scsi_cmd;
6434 
6435 	scsi_cmd = (struct scsi_sanitize *)&csio->cdb_io.cdb_bytes;
6436 	scsi_cmd->opcode = SANITIZE;
6437 	scsi_cmd->byte2 = byte2;
6438 	scsi_cmd->control = control;
6439 	scsi_ulto2b(dxfer_len, scsi_cmd->length);
6440 
6441 	cam_fill_csio(csio,
6442 		      retries,
6443 		      cbfcnp,
6444 		      /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
6445 		      tag_action,
6446 		      data_ptr,
6447 		      dxfer_len,
6448 		      sense_len,
6449 		      sizeof(*scsi_cmd),
6450 		      timeout);
6451 }
6452 
6453 #endif /* _KERNEL */
6454 
6455 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)6456 scsi_zbc_out(struct ccb_scsiio *csio, uint32_t retries,
6457 	     void (*cbfcnp)(struct cam_periph *, union ccb *),
6458 	     uint8_t tag_action, uint8_t service_action, uint64_t zone_id,
6459 	     uint8_t zone_flags, uint8_t *data_ptr, uint32_t dxfer_len,
6460 	     uint8_t sense_len, uint32_t timeout)
6461 {
6462 	struct scsi_zbc_out *scsi_cmd;
6463 
6464 	scsi_cmd = (struct scsi_zbc_out *)&csio->cdb_io.cdb_bytes;
6465 	scsi_cmd->opcode = ZBC_OUT;
6466 	scsi_cmd->service_action = service_action;
6467 	scsi_u64to8b(zone_id, scsi_cmd->zone_id);
6468 	scsi_cmd->zone_flags = zone_flags;
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 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)6483 scsi_zbc_in(struct ccb_scsiio *csio, uint32_t retries,
6484 	    void (*cbfcnp)(struct cam_periph *, union ccb *),
6485 	    uint8_t tag_action, uint8_t service_action, uint64_t zone_start_lba,
6486 	    uint8_t zone_options, uint8_t *data_ptr, uint32_t dxfer_len,
6487 	    uint8_t sense_len, uint32_t timeout)
6488 {
6489 	struct scsi_zbc_in *scsi_cmd;
6490 
6491 	scsi_cmd = (struct scsi_zbc_in *)&csio->cdb_io.cdb_bytes;
6492 	scsi_cmd->opcode = ZBC_IN;
6493 	scsi_cmd->service_action = service_action;
6494 	scsi_ulto4b(dxfer_len, scsi_cmd->length);
6495 	scsi_u64to8b(zone_start_lba, scsi_cmd->zone_start_lba);
6496 	scsi_cmd->zone_options = zone_options;
6497 
6498 	cam_fill_csio(csio,
6499 		      retries,
6500 		      cbfcnp,
6501 		      /*flags*/ (dxfer_len > 0) ? CAM_DIR_IN : CAM_DIR_NONE,
6502 		      tag_action,
6503 		      data_ptr,
6504 		      dxfer_len,
6505 		      sense_len,
6506 		      sizeof(*scsi_cmd),
6507 		      timeout);
6508 
6509 }
6510 
6511 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)6512 scsi_ata_zac_mgmt_out(struct ccb_scsiio *csio, uint32_t retries,
6513 		      void (*cbfcnp)(struct cam_periph *, union ccb *),
6514 		      uint8_t tag_action, int use_ncq,
6515 		      uint8_t zm_action, uint64_t zone_id, uint8_t zone_flags,
6516 		      uint8_t *data_ptr, uint32_t dxfer_len,
6517 		      uint8_t *cdb_storage, size_t cdb_storage_len,
6518 		      uint8_t sense_len, uint32_t timeout)
6519 {
6520 	uint8_t command_out, protocol, ata_flags;
6521 	uint16_t features_out;
6522 	uint32_t sectors_out, auxiliary;
6523 	int retval;
6524 
6525 	retval = 0;
6526 
6527 	if (use_ncq == 0) {
6528 		command_out = ATA_ZAC_MANAGEMENT_OUT;
6529 		features_out = (zm_action & 0xf) | (zone_flags << 8);
6530 		ata_flags = AP_FLAG_BYT_BLOK_BLOCKS;
6531 		if (dxfer_len == 0) {
6532 			protocol = AP_PROTO_NON_DATA;
6533 			ata_flags |= AP_FLAG_TLEN_NO_DATA;
6534 			sectors_out = 0;
6535 		} else {
6536 			protocol = AP_PROTO_DMA;
6537 			ata_flags |= AP_FLAG_TLEN_SECT_CNT |
6538 				     AP_FLAG_TDIR_TO_DEV;
6539 			sectors_out = ((dxfer_len >> 9) & 0xffff);
6540 		}
6541 		auxiliary = 0;
6542 	} else {
6543 		ata_flags = AP_FLAG_BYT_BLOK_BLOCKS;
6544 		if (dxfer_len == 0) {
6545 			command_out = ATA_NCQ_NON_DATA;
6546 			features_out = ATA_NCQ_ZAC_MGMT_OUT;
6547 			/*
6548 			 * We're assuming the SCSI to ATA translation layer
6549 			 * will set the NCQ tag number in the tag field.
6550 			 * That isn't clear from the SAT-4 spec (as of rev 05).
6551 			 */
6552 			sectors_out = 0;
6553 			ata_flags |= AP_FLAG_TLEN_NO_DATA;
6554 		} else {
6555 			command_out = ATA_SEND_FPDMA_QUEUED;
6556 			/*
6557 			 * Note that we're defaulting to normal priority,
6558 			 * and assuming that the SCSI to ATA translation
6559 			 * layer will insert the NCQ tag number in the tag
6560 			 * field.  That isn't clear in the SAT-4 spec (as
6561 			 * of rev 05).
6562 			 */
6563 			sectors_out = ATA_SFPDMA_ZAC_MGMT_OUT << 8;
6564 
6565 			ata_flags |= AP_FLAG_TLEN_FEAT |
6566 				     AP_FLAG_TDIR_TO_DEV;
6567 
6568 			/*
6569 			 * For SEND FPDMA QUEUED, the transfer length is
6570 			 * encoded in the FEATURE register, and 0 means
6571 			 * that 65536 512 byte blocks are to be tranferred.
6572 			 * In practice, it seems unlikely that we'll see
6573 			 * a transfer that large, and it may confuse the
6574 			 * the SAT layer, because generally that means that
6575 			 * 0 bytes should be transferred.
6576 			 */
6577 			if (dxfer_len == (65536 * 512)) {
6578 				features_out = 0;
6579 			} else if (dxfer_len <= (65535 * 512)) {
6580 				features_out = ((dxfer_len >> 9) & 0xffff);
6581 			} else {
6582 				/* The transfer is too big. */
6583 				retval = 1;
6584 				goto bailout;
6585 			}
6586 		}
6587 
6588 		auxiliary = (zm_action & 0xf) | (zone_flags << 8);
6589 		protocol = AP_PROTO_FPDMA;
6590 	}
6591 
6592 	protocol |= AP_EXTEND;
6593 
6594 	retval = scsi_ata_pass(csio,
6595 	    retries,
6596 	    cbfcnp,
6597 	    /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
6598 	    tag_action,
6599 	    /*protocol*/ protocol,
6600 	    /*ata_flags*/ ata_flags,
6601 	    /*features*/ features_out,
6602 	    /*sector_count*/ sectors_out,
6603 	    /*lba*/ zone_id,
6604 	    /*command*/ command_out,
6605 	    /*device*/ 0,
6606 	    /*icc*/ 0,
6607 	    /*auxiliary*/ auxiliary,
6608 	    /*control*/ 0,
6609 	    /*data_ptr*/ data_ptr,
6610 	    /*dxfer_len*/ dxfer_len,
6611 	    /*cdb_storage*/ cdb_storage,
6612 	    /*cdb_storage_len*/ cdb_storage_len,
6613 	    /*minimum_cmd_size*/ 0,
6614 	    /*sense_len*/ SSD_FULL_SIZE,
6615 	    /*timeout*/ timeout);
6616 
6617 bailout:
6618 
6619 	return (retval);
6620 }
6621 
6622 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)6623 scsi_ata_zac_mgmt_in(struct ccb_scsiio *csio, uint32_t retries,
6624 		     void (*cbfcnp)(struct cam_periph *, union ccb *),
6625 		     uint8_t tag_action, int use_ncq,
6626 		     uint8_t zm_action, uint64_t zone_id, uint8_t zone_flags,
6627 		     uint8_t *data_ptr, uint32_t dxfer_len,
6628 		     uint8_t *cdb_storage, size_t cdb_storage_len,
6629 		     uint8_t sense_len, uint32_t timeout)
6630 {
6631 	uint8_t command_out, protocol;
6632 	uint16_t features_out, sectors_out;
6633 	uint32_t auxiliary;
6634 	int ata_flags;
6635 	int retval;
6636 
6637 	retval = 0;
6638 	ata_flags = AP_FLAG_TDIR_FROM_DEV | AP_FLAG_BYT_BLOK_BLOCKS;
6639 
6640 	if (use_ncq == 0) {
6641 		command_out = ATA_ZAC_MANAGEMENT_IN;
6642 		/* XXX KDM put a macro here */
6643 		features_out = (zm_action & 0xf) | (zone_flags << 8);
6644 		sectors_out = dxfer_len >> 9; /* XXX KDM macro */
6645 		protocol = AP_PROTO_DMA;
6646 		ata_flags |= AP_FLAG_TLEN_SECT_CNT;
6647 		auxiliary = 0;
6648 	} else {
6649 		ata_flags |= AP_FLAG_TLEN_FEAT;
6650 
6651 		command_out = ATA_RECV_FPDMA_QUEUED;
6652 		sectors_out = ATA_RFPDMA_ZAC_MGMT_IN << 8;
6653 
6654 		/*
6655 		 * For RECEIVE FPDMA QUEUED, the transfer length is
6656 		 * encoded in the FEATURE register, and 0 means
6657 		 * that 65536 512 byte blocks are to be tranferred.
6658 		 * In practice, it seems unlikely that we'll see
6659 		 * a transfer that large, and it may confuse the
6660 		 * the SAT layer, because generally that means that
6661 		 * 0 bytes should be transferred.
6662 		 */
6663 		if (dxfer_len == (65536 * 512)) {
6664 			features_out = 0;
6665 		} else if (dxfer_len <= (65535 * 512)) {
6666 			features_out = ((dxfer_len >> 9) & 0xffff);
6667 		} else {
6668 			/* The transfer is too big. */
6669 			retval = 1;
6670 			goto bailout;
6671 		}
6672 		auxiliary = (zm_action & 0xf) | (zone_flags << 8),
6673 		protocol = AP_PROTO_FPDMA;
6674 	}
6675 
6676 	protocol |= AP_EXTEND;
6677 
6678 	retval = scsi_ata_pass(csio,
6679 	    retries,
6680 	    cbfcnp,
6681 	    /*flags*/ CAM_DIR_IN,
6682 	    tag_action,
6683 	    /*protocol*/ protocol,
6684 	    /*ata_flags*/ ata_flags,
6685 	    /*features*/ features_out,
6686 	    /*sector_count*/ sectors_out,
6687 	    /*lba*/ zone_id,
6688 	    /*command*/ command_out,
6689 	    /*device*/ 0,
6690 	    /*icc*/ 0,
6691 	    /*auxiliary*/ auxiliary,
6692 	    /*control*/ 0,
6693 	    /*data_ptr*/ data_ptr,
6694 	    /*dxfer_len*/ (dxfer_len >> 9) * 512, /* XXX KDM */
6695 	    /*cdb_storage*/ cdb_storage,
6696 	    /*cdb_storage_len*/ cdb_storage_len,
6697 	    /*minimum_cmd_size*/ 0,
6698 	    /*sense_len*/ SSD_FULL_SIZE,
6699 	    /*timeout*/ timeout);
6700 
6701 bailout:
6702 	return (retval);
6703 }
6704