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