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