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