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