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