xref: /freebsd/sys/cam/ata/ata_da.c (revision 349cc55c9796c4596a5b9904cd3281af295f878f)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer,
12  *    without modification, immediately at the beginning of the file.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include "opt_ada.h"
33 
34 #include <sys/param.h>
35 
36 #ifdef _KERNEL
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/bio.h>
40 #include <sys/sysctl.h>
41 #include <sys/taskqueue.h>
42 #include <sys/lock.h>
43 #include <sys/mutex.h>
44 #include <sys/conf.h>
45 #include <sys/devicestat.h>
46 #include <sys/eventhandler.h>
47 #include <sys/malloc.h>
48 #include <sys/endian.h>
49 #include <sys/cons.h>
50 #include <sys/proc.h>
51 #include <sys/reboot.h>
52 #include <sys/sbuf.h>
53 #include <geom/geom.h>
54 #include <geom/geom_disk.h>
55 #endif /* _KERNEL */
56 
57 #ifndef _KERNEL
58 #include <stdio.h>
59 #include <string.h>
60 #endif /* _KERNEL */
61 
62 #include <cam/cam.h>
63 #include <cam/cam_ccb.h>
64 #include <cam/cam_periph.h>
65 #include <cam/cam_xpt_periph.h>
66 #include <cam/scsi/scsi_all.h>
67 #include <cam/scsi/scsi_da.h>
68 #include <cam/cam_sim.h>
69 #include <cam/cam_iosched.h>
70 
71 #include <cam/ata/ata_all.h>
72 
73 #ifdef _KERNEL
74 
75 #define ATA_MAX_28BIT_LBA               268435455UL
76 
77 extern int iosched_debug;
78 
79 typedef enum {
80 	ADA_STATE_RAHEAD,
81 	ADA_STATE_WCACHE,
82 	ADA_STATE_LOGDIR,
83 	ADA_STATE_IDDIR,
84 	ADA_STATE_SUP_CAP,
85 	ADA_STATE_ZONE,
86 	ADA_STATE_NORMAL
87 } ada_state;
88 
89 typedef enum {
90 	ADA_FLAG_CAN_48BIT	= 0x00000002,
91 	ADA_FLAG_CAN_FLUSHCACHE	= 0x00000004,
92 	ADA_FLAG_CAN_NCQ	= 0x00000008,
93 	ADA_FLAG_CAN_DMA	= 0x00000010,
94 	ADA_FLAG_NEED_OTAG	= 0x00000020,
95 	ADA_FLAG_WAS_OTAG	= 0x00000040,
96 	ADA_FLAG_CAN_TRIM	= 0x00000080,
97 	ADA_FLAG_OPEN		= 0x00000100,
98 	ADA_FLAG_SCTX_INIT	= 0x00000200,
99 	ADA_FLAG_CAN_CFA        = 0x00000400,
100 	ADA_FLAG_CAN_POWERMGT   = 0x00000800,
101 	ADA_FLAG_CAN_DMA48	= 0x00001000,
102 	ADA_FLAG_CAN_LOG	= 0x00002000,
103 	ADA_FLAG_CAN_IDLOG	= 0x00004000,
104 	ADA_FLAG_CAN_SUPCAP	= 0x00008000,
105 	ADA_FLAG_CAN_ZONE	= 0x00010000,
106 	ADA_FLAG_CAN_WCACHE	= 0x00020000,
107 	ADA_FLAG_CAN_RAHEAD	= 0x00040000,
108 	ADA_FLAG_PROBED		= 0x00080000,
109 	ADA_FLAG_ANNOUNCED	= 0x00100000,
110 	ADA_FLAG_DIRTY		= 0x00200000,
111 	ADA_FLAG_CAN_NCQ_TRIM	= 0x00400000,	/* CAN_TRIM also set */
112 	ADA_FLAG_PIM_ATA_EXT	= 0x00800000,
113 	ADA_FLAG_UNMAPPEDIO	= 0x01000000,
114 	ADA_FLAG_ROTATING	= 0x02000000
115 } ada_flags;
116 #define ADA_FLAG_STRING		\
117 	"\020"			\
118 	"\002CAN_48BIT"		\
119 	"\003CAN_FLUSHCACHE"	\
120 	"\004CAN_NCQ"		\
121 	"\005CAN_DMA"		\
122 	"\006NEED_OTAG"		\
123 	"\007WAS_OTAG"		\
124 	"\010CAN_TRIM"		\
125 	"\011OPEN"		\
126 	"\012SCTX_INIT"		\
127 	"\013CAN_CFA"		\
128 	"\014CAN_POWERMGT"	\
129 	"\015CAN_DMA48"		\
130 	"\016CAN_LOG"		\
131 	"\017CAN_IDLOG"		\
132 	"\020CAN_SUPCAP"	\
133 	"\021CAN_ZONE"		\
134 	"\022CAN_WCACHE"	\
135 	"\023CAN_RAHEAD"	\
136 	"\024PROBED"		\
137 	"\025ANNOUNCED"		\
138 	"\026DIRTY"		\
139 	"\027CAN_NCQ_TRIM"	\
140 	"\030PIM_ATA_EXT"	\
141 	"\031UNMAPPEDIO"	\
142 	"\032ROTATING"
143 
144 typedef enum {
145 	ADA_Q_NONE		= 0x00,
146 	ADA_Q_4K		= 0x01,
147 	ADA_Q_NCQ_TRIM_BROKEN	= 0x02,
148 	ADA_Q_LOG_BROKEN	= 0x04,
149 	ADA_Q_SMR_DM		= 0x08,
150 	ADA_Q_NO_TRIM		= 0x10,
151 	ADA_Q_128KB		= 0x20
152 } ada_quirks;
153 
154 #define ADA_Q_BIT_STRING	\
155 	"\020"			\
156 	"\0014K"		\
157 	"\002NCQ_TRIM_BROKEN"	\
158 	"\003LOG_BROKEN"	\
159 	"\004SMR_DM"		\
160 	"\005NO_TRIM"		\
161 	"\006128KB"
162 
163 typedef enum {
164 	ADA_CCB_RAHEAD		= 0x01,
165 	ADA_CCB_WCACHE		= 0x02,
166 	ADA_CCB_BUFFER_IO	= 0x03,
167 	ADA_CCB_DUMP		= 0x05,
168 	ADA_CCB_TRIM		= 0x06,
169 	ADA_CCB_LOGDIR		= 0x07,
170 	ADA_CCB_IDDIR		= 0x08,
171 	ADA_CCB_SUP_CAP		= 0x09,
172 	ADA_CCB_ZONE		= 0x0a,
173 	ADA_CCB_TYPE_MASK	= 0x0F,
174 } ada_ccb_state;
175 
176 typedef enum {
177 	ADA_ZONE_NONE		= 0x00,
178 	ADA_ZONE_DRIVE_MANAGED	= 0x01,
179 	ADA_ZONE_HOST_AWARE	= 0x02,
180 	ADA_ZONE_HOST_MANAGED	= 0x03
181 } ada_zone_mode;
182 
183 typedef enum {
184 	ADA_ZONE_FLAG_RZ_SUP		= 0x0001,
185 	ADA_ZONE_FLAG_OPEN_SUP		= 0x0002,
186 	ADA_ZONE_FLAG_CLOSE_SUP		= 0x0004,
187 	ADA_ZONE_FLAG_FINISH_SUP	= 0x0008,
188 	ADA_ZONE_FLAG_RWP_SUP		= 0x0010,
189 	ADA_ZONE_FLAG_SUP_MASK		= (ADA_ZONE_FLAG_RZ_SUP |
190 					   ADA_ZONE_FLAG_OPEN_SUP |
191 					   ADA_ZONE_FLAG_CLOSE_SUP |
192 					   ADA_ZONE_FLAG_FINISH_SUP |
193 					   ADA_ZONE_FLAG_RWP_SUP),
194 	ADA_ZONE_FLAG_URSWRZ		= 0x0020,
195 	ADA_ZONE_FLAG_OPT_SEQ_SET	= 0x0040,
196 	ADA_ZONE_FLAG_OPT_NONSEQ_SET	= 0x0080,
197 	ADA_ZONE_FLAG_MAX_SEQ_SET	= 0x0100,
198 	ADA_ZONE_FLAG_SET_MASK		= (ADA_ZONE_FLAG_OPT_SEQ_SET |
199 					   ADA_ZONE_FLAG_OPT_NONSEQ_SET |
200 					   ADA_ZONE_FLAG_MAX_SEQ_SET)
201 } ada_zone_flags;
202 
203 static struct ada_zone_desc {
204 	ada_zone_flags value;
205 	const char *desc;
206 } ada_zone_desc_table[] = {
207 	{ADA_ZONE_FLAG_RZ_SUP, "Report Zones" },
208 	{ADA_ZONE_FLAG_OPEN_SUP, "Open" },
209 	{ADA_ZONE_FLAG_CLOSE_SUP, "Close" },
210 	{ADA_ZONE_FLAG_FINISH_SUP, "Finish" },
211 	{ADA_ZONE_FLAG_RWP_SUP, "Reset Write Pointer" },
212 };
213 
214 /* Offsets into our private area for storing information */
215 #define ccb_state	ppriv_field0
216 #define ccb_bp		ppriv_ptr1
217 
218 typedef enum {
219 	ADA_DELETE_NONE,
220 	ADA_DELETE_DISABLE,
221 	ADA_DELETE_CFA_ERASE,
222 	ADA_DELETE_DSM_TRIM,
223 	ADA_DELETE_NCQ_DSM_TRIM,
224 	ADA_DELETE_MIN = ADA_DELETE_CFA_ERASE,
225 	ADA_DELETE_MAX = ADA_DELETE_NCQ_DSM_TRIM,
226 } ada_delete_methods;
227 
228 static const char *ada_delete_method_names[] =
229     { "NONE", "DISABLE", "CFA_ERASE", "DSM_TRIM", "NCQ_DSM_TRIM" };
230 #if 0
231 static const char *ada_delete_method_desc[] =
232     { "NONE", "DISABLED", "CFA Erase", "DSM Trim", "DSM Trim via NCQ" };
233 #endif
234 
235 struct disk_params {
236 	u_int8_t  heads;
237 	u_int8_t  secs_per_track;
238 	u_int32_t cylinders;
239 	u_int32_t secsize;	/* Number of bytes/logical sector */
240 	u_int64_t sectors;	/* Total number sectors */
241 };
242 
243 #define TRIM_MAX_BLOCKS	8
244 #define TRIM_MAX_RANGES	(TRIM_MAX_BLOCKS * ATA_DSM_BLK_RANGES)
245 struct trim_request {
246 	uint8_t		data[TRIM_MAX_RANGES * ATA_DSM_RANGE_SIZE];
247 	TAILQ_HEAD(, bio) bps;
248 };
249 
250 struct ada_softc {
251 	struct   cam_iosched_softc *cam_iosched;
252 	int	 outstanding_cmds;	/* Number of active commands */
253 	int	 refcount;		/* Active xpt_action() calls */
254 	ada_state state;
255 	ada_flags flags;
256 	ada_zone_mode zone_mode;
257 	ada_zone_flags zone_flags;
258 	struct ata_gp_log_dir ata_logdir;
259 	int valid_logdir_len;
260 	struct ata_identify_log_pages ata_iddir;
261 	int valid_iddir_len;
262 	uint64_t optimal_seq_zones;
263 	uint64_t optimal_nonseq_zones;
264 	uint64_t max_seq_zones;
265 	ada_quirks quirks;
266 	ada_delete_methods delete_method;
267 	int	 trim_max_ranges;
268 	int	 read_ahead;
269 	int	 write_cache;
270 #ifdef CAM_TEST_FAILURE
271 	int      force_read_error;
272 	int      force_write_error;
273 	int      periodic_read_error;
274 	int      periodic_read_count;
275 #endif
276 	struct ccb_pathinq	cpi;
277 	struct disk_params	params;
278 	struct disk		*disk;
279 	struct task		sysctl_task;
280 	struct sysctl_ctx_list	sysctl_ctx;
281 	struct sysctl_oid	*sysctl_tree;
282 	struct callout		sendordered_c;
283 	struct trim_request	trim_req;
284 	uint64_t		trim_count;
285 	uint64_t		trim_ranges;
286 	uint64_t		trim_lbas;
287 #ifdef CAM_IO_STATS
288 	struct sysctl_ctx_list	sysctl_stats_ctx;
289 	struct sysctl_oid	*sysctl_stats_tree;
290 	u_int	timeouts;
291 	u_int	errors;
292 	u_int	invalidations;
293 #endif
294 #define ADA_ANNOUNCETMP_SZ 80
295 	char	announce_temp[ADA_ANNOUNCETMP_SZ];
296 #define ADA_ANNOUNCE_SZ 400
297 	char	announce_buffer[ADA_ANNOUNCE_SZ];
298 };
299 
300 static uma_zone_t ada_ccb_zone;
301 
302 struct ada_quirk_entry {
303 	struct scsi_inquiry_pattern inq_pat;
304 	ada_quirks quirks;
305 };
306 
307 static struct ada_quirk_entry ada_quirk_table[] =
308 {
309 	{
310 		/* Sandisk X400 */
311 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "SanDisk?SD8SB8U1T00*", "X4162000*" },
312 		/*quirks*/ADA_Q_128KB
313 	},
314 	{
315 		/* Hitachi Advanced Format (4k) drives */
316 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Hitachi H??????????E3*", "*" },
317 		/*quirks*/ADA_Q_4K
318 	},
319 	{
320 		/* Samsung Advanced Format (4k) drives */
321 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD155UI*", "*" },
322 		/*quirks*/ADA_Q_4K
323 	},
324 	{
325 		/* Samsung Advanced Format (4k) drives */
326 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD204UI*", "*" },
327 		/*quirks*/ADA_Q_4K
328 	},
329 	{
330 		/* Seagate Barracuda Green Advanced Format (4k) drives */
331 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST????DL*", "*" },
332 		/*quirks*/ADA_Q_4K
333 	},
334 	{
335 		/* Seagate Barracuda Advanced Format (4k) drives */
336 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST???DM*", "*" },
337 		/*quirks*/ADA_Q_4K
338 	},
339 	{
340 		/* Seagate Barracuda Advanced Format (4k) drives */
341 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST????DM*", "*" },
342 		/*quirks*/ADA_Q_4K
343 	},
344 	{
345 		/* Seagate Momentus Advanced Format (4k) drives */
346 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9500423AS*", "*" },
347 		/*quirks*/ADA_Q_4K
348 	},
349 	{
350 		/* Seagate Momentus Advanced Format (4k) drives */
351 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9500424AS*", "*" },
352 		/*quirks*/ADA_Q_4K
353 	},
354 	{
355 		/* Seagate Momentus Advanced Format (4k) drives */
356 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9640423AS*", "*" },
357 		/*quirks*/ADA_Q_4K
358 	},
359 	{
360 		/* Seagate Momentus Advanced Format (4k) drives */
361 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9640424AS*", "*" },
362 		/*quirks*/ADA_Q_4K
363 	},
364 	{
365 		/* Seagate Momentus Advanced Format (4k) drives */
366 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750420AS*", "*" },
367 		/*quirks*/ADA_Q_4K
368 	},
369 	{
370 		/* Seagate Momentus Advanced Format (4k) drives */
371 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750422AS*", "*" },
372 		/*quirks*/ADA_Q_4K
373 	},
374 	{
375 		/* Seagate Momentus Advanced Format (4k) drives */
376 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750423AS*", "*" },
377 		/*quirks*/ADA_Q_4K
378 	},
379 	{
380 		/* Seagate Momentus Thin Advanced Format (4k) drives */
381 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST???LT*", "*" },
382 		/*quirks*/ADA_Q_4K
383 	},
384 	{
385 		/* WDC Caviar Red Advanced Format (4k) drives */
386 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????CX*", "*" },
387 		/*quirks*/ADA_Q_4K
388 	},
389 	{
390 		/* WDC Caviar Green Advanced Format (4k) drives */
391 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RS*", "*" },
392 		/*quirks*/ADA_Q_4K
393 	},
394 	{
395 		/* WDC Caviar Green/Red Advanced Format (4k) drives */
396 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RX*", "*" },
397 		/*quirks*/ADA_Q_4K
398 	},
399 	{
400 		/* WDC Caviar Red Advanced Format (4k) drives */
401 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????CX*", "*" },
402 		/*quirks*/ADA_Q_4K
403 	},
404 	{
405 		/* WDC Caviar Black Advanced Format (4k) drives */
406 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????AZEX*", "*" },
407 		/*quirks*/ADA_Q_4K
408 	},
409 	{
410 		/* WDC Caviar Black Advanced Format (4k) drives */
411 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????FZEX*", "*" },
412 		/*quirks*/ADA_Q_4K
413 	},
414 	{
415 		/* WDC Caviar Green Advanced Format (4k) drives */
416 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RS*", "*" },
417 		/*quirks*/ADA_Q_4K
418 	},
419 	{
420 		/* WDC Caviar Green Advanced Format (4k) drives */
421 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RX*", "*" },
422 		/*quirks*/ADA_Q_4K
423 	},
424 	{
425 		/* WDC Scorpio Black Advanced Format (4k) drives */
426 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PKT*", "*" },
427 		/*quirks*/ADA_Q_4K
428 	},
429 	{
430 		/* WDC Scorpio Black Advanced Format (4k) drives */
431 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PKT*", "*" },
432 		/*quirks*/ADA_Q_4K
433 	},
434 	{
435 		/* WDC Scorpio Blue Advanced Format (4k) drives */
436 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PVT*", "*" },
437 		/*quirks*/ADA_Q_4K
438 	},
439 	{
440 		/* WDC Scorpio Blue Advanced Format (4k) drives */
441 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PVT*", "*" },
442 		/*quirks*/ADA_Q_4K
443 	},
444 	/* SSDs */
445 	{
446 		/*
447 		 * Corsair Force 2 SSDs
448 		 * 4k optimised & trim only works in 4k requests + 4k aligned
449 		 */
450 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair CSSD-F*", "*" },
451 		/*quirks*/ADA_Q_4K
452 	},
453 	{
454 		/*
455 		 * Corsair Force 3 SSDs
456 		 * 4k optimised & trim only works in 4k requests + 4k aligned
457 		 */
458 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Force 3*", "*" },
459 		/*quirks*/ADA_Q_4K
460 	},
461 	{
462 		/*
463 		 * Corsair Neutron GTX SSDs
464 		 * 4k optimised & trim only works in 4k requests + 4k aligned
465 		 */
466 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Neutron GTX*", "*" },
467 		/*quirks*/ADA_Q_4K
468 	},
469 	{
470 		/*
471 		 * Corsair Force GT & GS SSDs
472 		 * 4k optimised & trim only works in 4k requests + 4k aligned
473 		 */
474 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Force G*", "*" },
475 		/*quirks*/ADA_Q_4K
476 	},
477 	{
478 		/*
479 		 * Crucial M4 SSDs
480 		 * 4k optimised & trim only works in 4k requests + 4k aligned
481 		 */
482 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "M4-CT???M4SSD2*", "*" },
483 		/*quirks*/ADA_Q_4K
484 	},
485 	{
486 		/*
487 		 * Crucial M500 SSDs MU07 firmware
488 		 * NCQ Trim works
489 		 */
490 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Crucial CT*M500*", "MU07" },
491 		/*quirks*/0
492 	},
493 	{
494 		/*
495 		 * Crucial M500 SSDs all other firmware
496 		 * NCQ Trim doesn't work
497 		 */
498 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Crucial CT*M500*", "*" },
499 		/*quirks*/ADA_Q_NCQ_TRIM_BROKEN
500 	},
501 	{
502 		/*
503 		 * Crucial M550 SSDs
504 		 * NCQ Trim doesn't work, but only on MU01 firmware
505 		 */
506 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Crucial CT*M550*", "MU01" },
507 		/*quirks*/ADA_Q_NCQ_TRIM_BROKEN
508 	},
509 	{
510 		/*
511 		 * Crucial MX100 SSDs
512 		 * NCQ Trim doesn't work, but only on MU01 firmware
513 		 */
514 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Crucial CT*MX100*", "MU01" },
515 		/*quirks*/ADA_Q_NCQ_TRIM_BROKEN
516 	},
517 	{
518 		/*
519 		 * Crucial RealSSD C300 SSDs
520 		 * 4k optimised
521 		 */
522 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "C300-CTFDDAC???MAG*",
523 		"*" }, /*quirks*/ADA_Q_4K
524 	},
525 	{
526 		/*
527 		 * FCCT M500 SSDs
528 		 * NCQ Trim doesn't work
529 		 */
530 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "FCCT*M500*", "*" },
531 		/*quirks*/ADA_Q_NCQ_TRIM_BROKEN
532 	},
533 	{
534 		/*
535 		 * Intel 320 Series SSDs
536 		 * 4k optimised & trim only works in 4k requests + 4k aligned
537 		 */
538 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSA2CW*", "*" },
539 		/*quirks*/ADA_Q_4K
540 	},
541 	{
542 		/*
543 		 * Intel 330 Series SSDs
544 		 * 4k optimised & trim only works in 4k requests + 4k aligned
545 		 */
546 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2CT*", "*" },
547 		/*quirks*/ADA_Q_4K
548 	},
549 	{
550 		/*
551 		 * Intel 510 Series SSDs
552 		 * 4k optimised & trim only works in 4k requests + 4k aligned
553 		 */
554 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2MH*", "*" },
555 		/*quirks*/ADA_Q_4K
556 	},
557 	{
558 		/*
559 		 * Intel 520 Series SSDs
560 		 * 4k optimised & trim only works in 4k requests + 4k aligned
561 		 */
562 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2BW*", "*" },
563 		/*quirks*/ADA_Q_4K
564 	},
565 	{
566 		/*
567 		 * Intel S3610 Series SSDs
568 		 * 4k optimised & trim only works in 4k requests + 4k aligned
569 		 */
570 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2BX*", "*" },
571 		/*quirks*/ADA_Q_4K
572 	},
573 	{
574 		/*
575 		 * Intel X25-M Series SSDs
576 		 * 4k optimised & trim only works in 4k requests + 4k aligned
577 		 */
578 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSA2M*", "*" },
579 		/*quirks*/ADA_Q_4K
580 	},
581 	{
582 		/*
583 		 * KingDian S200 60GB P0921B
584 		 * Trimming crash the SSD
585 		 */
586 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "KingDian S200 *", "*" },
587 		/*quirks*/ADA_Q_NO_TRIM
588 	},
589 	{
590 		/*
591 		 * Kingston E100 Series SSDs
592 		 * 4k optimised & trim only works in 4k requests + 4k aligned
593 		 */
594 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "KINGSTON SE100S3*", "*" },
595 		/*quirks*/ADA_Q_4K
596 	},
597 	{
598 		/*
599 		 * Kingston HyperX 3k SSDs
600 		 * 4k optimised & trim only works in 4k requests + 4k aligned
601 		 */
602 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "KINGSTON SH103S3*", "*" },
603 		/*quirks*/ADA_Q_4K
604 	},
605 	{
606 		/*
607 		 * Marvell SSDs (entry taken from OpenSolaris)
608 		 * 4k optimised & trim only works in 4k requests + 4k aligned
609 		 */
610 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "MARVELL SD88SA02*", "*" },
611 		/*quirks*/ADA_Q_4K
612 	},
613 	{
614 		/*
615 		 * Micron M500 SSDs firmware MU07
616 		 * NCQ Trim works?
617 		 */
618 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Micron M500*", "MU07" },
619 		/*quirks*/0
620 	},
621 	{
622 		/*
623 		 * Micron M500 SSDs all other firmware
624 		 * NCQ Trim doesn't work
625 		 */
626 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Micron M500*", "*" },
627 		/*quirks*/ADA_Q_NCQ_TRIM_BROKEN
628 	},
629 	{
630 		/*
631 		 * Micron M5[15]0 SSDs
632 		 * NCQ Trim doesn't work, but only MU01 firmware
633 		 */
634 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Micron M5[15]0*", "MU01" },
635 		/*quirks*/ADA_Q_NCQ_TRIM_BROKEN
636 	},
637 	{
638 		/*
639 		 * Micron 5100 SSDs
640 		 * 4k optimised & trim only works in 4k requests + 4k aligned
641 		 */
642 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Micron 5100 MTFDDAK*", "*" },
643 		/*quirks*/ADA_Q_4K
644 	},
645 	{
646 		/*
647 		 * OCZ Agility 2 SSDs
648 		 * 4k optimised & trim only works in 4k requests + 4k aligned
649 		 */
650 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-AGILITY2*", "*" },
651 		/*quirks*/ADA_Q_4K
652 	},
653 	{
654 		/*
655 		 * OCZ Agility 3 SSDs
656 		 * 4k optimised & trim only works in 4k requests + 4k aligned
657 		 */
658 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-AGILITY3*", "*" },
659 		/*quirks*/ADA_Q_4K
660 	},
661 	{
662 		/*
663 		 * OCZ Deneva R Series SSDs
664 		 * 4k optimised & trim only works in 4k requests + 4k aligned
665 		 */
666 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "DENRSTE251M45*", "*" },
667 		/*quirks*/ADA_Q_4K
668 	},
669 	{
670 		/*
671 		 * OCZ Vertex 2 SSDs (inc pro series)
672 		 * 4k optimised & trim only works in 4k requests + 4k aligned
673 		 */
674 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ?VERTEX2*", "*" },
675 		/*quirks*/ADA_Q_4K
676 	},
677 	{
678 		/*
679 		 * OCZ Vertex 3 SSDs
680 		 * 4k optimised & trim only works in 4k requests + 4k aligned
681 		 */
682 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-VERTEX3*", "*" },
683 		/*quirks*/ADA_Q_4K
684 	},
685 	{
686 		/*
687 		 * OCZ Vertex 4 SSDs
688 		 * 4k optimised & trim only works in 4k requests + 4k aligned
689 		 */
690 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-VERTEX4*", "*" },
691 		/*quirks*/ADA_Q_4K
692 	},
693 	{
694 		/*
695 		 * Samsung 750 SSDs
696 		 * 4k optimised, NCQ TRIM seems to work
697 		 */
698 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Samsung SSD 750*", "*" },
699 		/*quirks*/ADA_Q_4K
700 	},
701 	{
702 		/*
703 		 * Samsung 830 Series SSDs
704 		 * 4k optimised, NCQ TRIM Broken (normal TRIM is fine)
705 		 */
706 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG SSD 830 Series*", "*" },
707 		/*quirks*/ADA_Q_4K | ADA_Q_NCQ_TRIM_BROKEN
708 	},
709 	{
710 		/*
711 		 * Samsung 840 SSDs
712 		 * 4k optimised, NCQ TRIM Broken (normal TRIM is fine)
713 		 */
714 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Samsung SSD 840*", "*" },
715 		/*quirks*/ADA_Q_4K | ADA_Q_NCQ_TRIM_BROKEN
716 	},
717 	{
718 		/*
719 		 * Samsung 845 SSDs
720 		 * 4k optimised, NCQ TRIM Broken (normal TRIM is fine)
721 		 */
722 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Samsung SSD 845*", "*" },
723 		/*quirks*/ADA_Q_4K | ADA_Q_NCQ_TRIM_BROKEN
724 	},
725 	{
726 		/*
727 		 * Samsung 850 SSDs
728 		 * 4k optimised, NCQ TRIM broken (normal TRIM fine)
729 		 */
730 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Samsung SSD 850*", "*" },
731 		/*quirks*/ADA_Q_4K | ADA_Q_NCQ_TRIM_BROKEN
732 	},
733 	{
734 		/*
735 		 * Samsung SM863 Series SSDs (MZ7KM*)
736 		 * 4k optimised, NCQ believed to be working
737 		 */
738 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG MZ7KM*", "*" },
739 		/*quirks*/ADA_Q_4K
740 	},
741 	{
742 		/*
743 		 * Samsung 843T Series SSDs (MZ7WD*)
744 		 * Samsung PM851 Series SSDs (MZ7TE*)
745 		 * Samsung PM853T Series SSDs (MZ7GE*)
746 		 * 4k optimised, NCQ believed to be broken since these are
747 		 * appear to be built with the same controllers as the 840/850.
748 		 */
749 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG MZ7*", "*" },
750 		/*quirks*/ADA_Q_4K | ADA_Q_NCQ_TRIM_BROKEN
751 	},
752 	{
753 		/*
754 		 * Same as for SAMSUNG MZ7* but enable the quirks for SSD
755 		 * starting with MZ7* too
756 		 */
757 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "MZ7*", "*" },
758 		/*quirks*/ADA_Q_4K | ADA_Q_NCQ_TRIM_BROKEN
759 	},
760 	{
761 		/*
762 		 * Samsung PM851 Series SSDs Dell OEM
763 		 * device model          "SAMSUNG SSD PM851 mSATA 256GB"
764 		 * 4k optimised, NCQ broken
765 		 */
766 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG SSD PM851*", "*" },
767 		/*quirks*/ADA_Q_4K | ADA_Q_NCQ_TRIM_BROKEN
768 	},
769 	{
770 		/*
771 		 * SuperTalent TeraDrive CT SSDs
772 		 * 4k optimised & trim only works in 4k requests + 4k aligned
773 		 */
774 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "FTM??CT25H*", "*" },
775 		/*quirks*/ADA_Q_4K
776 	},
777 	{
778 		/*
779 		 * XceedIOPS SATA SSDs
780 		 * 4k optimised
781 		 */
782 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "SG9XCS2D*", "*" },
783 		/*quirks*/ADA_Q_4K
784 	},
785 	{
786 		/*
787 		 * Samsung drive that doesn't support READ LOG EXT or
788 		 * READ LOG DMA EXT, despite reporting that it does in
789 		 * ATA identify data:
790 		 * SAMSUNG HD200HJ KF100-06
791 		 */
792 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD200*", "*" },
793 		/*quirks*/ADA_Q_LOG_BROKEN
794 	},
795 	{
796 		/*
797 		 * Samsung drive that doesn't support READ LOG EXT or
798 		 * READ LOG DMA EXT, despite reporting that it does in
799 		 * ATA identify data:
800 		 * SAMSUNG HD501LJ CR100-10
801 		 */
802 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD501*", "*" },
803 		/*quirks*/ADA_Q_LOG_BROKEN
804 	},
805 	{
806 		/*
807 		 * Seagate Lamarr 8TB Shingled Magnetic Recording (SMR)
808 		 * Drive Managed SATA hard drive.  This drive doesn't report
809 		 * in firmware that it is a drive managed SMR drive.
810 		 */
811 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST8000AS000[23]*", "*" },
812 		/*quirks*/ADA_Q_SMR_DM
813 	},
814 	{
815 		/* WD Green SSD */
816 		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WDS?????G0*", "*" },
817 		/*quirks*/ADA_Q_4K | ADA_Q_NCQ_TRIM_BROKEN
818 	},
819 	{
820 		/* Default */
821 		{
822 		  T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
823 		  /*vendor*/"*", /*product*/"*", /*revision*/"*"
824 		},
825 		/*quirks*/0
826 	},
827 };
828 
829 static	disk_strategy_t	adastrategy;
830 static	dumper_t	adadump;
831 static	periph_init_t	adainit;
832 static	void		adadiskgonecb(struct disk *dp);
833 static	periph_oninv_t	adaoninvalidate;
834 static	periph_dtor_t	adacleanup;
835 static	void		adaasync(void *callback_arg, u_int32_t code,
836 				struct cam_path *path, void *arg);
837 static	int		adabitsysctl(SYSCTL_HANDLER_ARGS);
838 static	int		adaflagssysctl(SYSCTL_HANDLER_ARGS);
839 static	int		adazonesupsysctl(SYSCTL_HANDLER_ARGS);
840 static	void		adasysctlinit(void *context, int pending);
841 static	int		adagetattr(struct bio *bp);
842 static	void		adasetflags(struct ada_softc *softc,
843 				    struct ccb_getdev *cgd);
844 static void		adasetgeom(struct ada_softc *softc,
845 				   struct ccb_getdev *cgd);
846 static	periph_ctor_t	adaregister;
847 static	void		ada_dsmtrim(struct ada_softc *softc, struct bio *bp,
848 				    struct ccb_ataio *ataio);
849 static	void 		ada_cfaerase(struct ada_softc *softc, struct bio *bp,
850 				     struct ccb_ataio *ataio);
851 static	int		ada_zone_bio_to_ata(int disk_zone_cmd);
852 static	int		ada_zone_cmd(struct cam_periph *periph, union ccb *ccb,
853 				     struct bio *bp, int *queue_ccb);
854 static	periph_start_t	adastart;
855 static	void		adaprobedone(struct cam_periph *periph, union ccb *ccb);
856 static	void		adazonedone(struct cam_periph *periph, union ccb *ccb);
857 static	void		adadone(struct cam_periph *periph,
858 			       union ccb *done_ccb);
859 static  int		adaerror(union ccb *ccb, u_int32_t cam_flags,
860 				u_int32_t sense_flags);
861 static callout_func_t	adasendorderedtag;
862 static void		adashutdown(void *arg, int howto);
863 static void		adasuspend(void *arg);
864 static void		adaresume(void *arg);
865 
866 #ifndef ADA_DEFAULT_TIMEOUT
867 #define ADA_DEFAULT_TIMEOUT 30	/* Timeout in seconds */
868 #endif
869 
870 #ifndef	ADA_DEFAULT_RETRY
871 #define	ADA_DEFAULT_RETRY	4
872 #endif
873 
874 #ifndef	ADA_DEFAULT_SEND_ORDERED
875 #define	ADA_DEFAULT_SEND_ORDERED	1
876 #endif
877 
878 #ifndef	ADA_DEFAULT_SPINDOWN_SHUTDOWN
879 #define	ADA_DEFAULT_SPINDOWN_SHUTDOWN	1
880 #endif
881 
882 #ifndef	ADA_DEFAULT_SPINDOWN_SUSPEND
883 #define	ADA_DEFAULT_SPINDOWN_SUSPEND	1
884 #endif
885 
886 #ifndef	ADA_DEFAULT_READ_AHEAD
887 #define	ADA_DEFAULT_READ_AHEAD	1
888 #endif
889 
890 #ifndef	ADA_DEFAULT_WRITE_CACHE
891 #define	ADA_DEFAULT_WRITE_CACHE	1
892 #endif
893 
894 #define	ADA_RA	(softc->read_ahead >= 0 ? \
895 		 softc->read_ahead : ada_read_ahead)
896 #define	ADA_WC	(softc->write_cache >= 0 ? \
897 		 softc->write_cache : ada_write_cache)
898 
899 static int ada_retry_count = ADA_DEFAULT_RETRY;
900 static int ada_default_timeout = ADA_DEFAULT_TIMEOUT;
901 static int ada_send_ordered = ADA_DEFAULT_SEND_ORDERED;
902 static int ada_spindown_shutdown = ADA_DEFAULT_SPINDOWN_SHUTDOWN;
903 static int ada_spindown_suspend = ADA_DEFAULT_SPINDOWN_SUSPEND;
904 static int ada_read_ahead = ADA_DEFAULT_READ_AHEAD;
905 static int ada_write_cache = ADA_DEFAULT_WRITE_CACHE;
906 static int ada_enable_biospeedup = 1;
907 static int ada_enable_uma_ccbs = 1;
908 
909 static SYSCTL_NODE(_kern_cam, OID_AUTO, ada, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
910     "CAM Direct Access Disk driver");
911 SYSCTL_INT(_kern_cam_ada, OID_AUTO, retry_count, CTLFLAG_RWTUN,
912            &ada_retry_count, 0, "Normal I/O retry count");
913 SYSCTL_INT(_kern_cam_ada, OID_AUTO, default_timeout, CTLFLAG_RWTUN,
914            &ada_default_timeout, 0, "Normal I/O timeout (in seconds)");
915 SYSCTL_INT(_kern_cam_ada, OID_AUTO, send_ordered, CTLFLAG_RWTUN,
916            &ada_send_ordered, 0, "Send Ordered Tags");
917 SYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_shutdown, CTLFLAG_RWTUN,
918            &ada_spindown_shutdown, 0, "Spin down upon shutdown");
919 SYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_suspend, CTLFLAG_RWTUN,
920            &ada_spindown_suspend, 0, "Spin down upon suspend");
921 SYSCTL_INT(_kern_cam_ada, OID_AUTO, read_ahead, CTLFLAG_RWTUN,
922            &ada_read_ahead, 0, "Enable disk read-ahead");
923 SYSCTL_INT(_kern_cam_ada, OID_AUTO, write_cache, CTLFLAG_RWTUN,
924            &ada_write_cache, 0, "Enable disk write cache");
925 SYSCTL_INT(_kern_cam_ada, OID_AUTO, enable_biospeedup, CTLFLAG_RDTUN,
926 	   &ada_enable_biospeedup, 0, "Enable BIO_SPEEDUP processing");
927 SYSCTL_INT(_kern_cam_ada, OID_AUTO, enable_uma_ccbs, CTLFLAG_RWTUN,
928 	    &ada_enable_uma_ccbs, 0, "Use UMA for CCBs");
929 
930 /*
931  * ADA_ORDEREDTAG_INTERVAL determines how often, relative
932  * to the default timeout, we check to see whether an ordered
933  * tagged transaction is appropriate to prevent simple tag
934  * starvation.  Since we'd like to ensure that there is at least
935  * 1/2 of the timeout length left for a starved transaction to
936  * complete after we've sent an ordered tag, we must poll at least
937  * four times in every timeout period.  This takes care of the worst
938  * case where a starved transaction starts during an interval that
939  * meets the requirement "don't send an ordered tag" test so it takes
940  * us two intervals to determine that a tag must be sent.
941  */
942 #ifndef ADA_ORDEREDTAG_INTERVAL
943 #define ADA_ORDEREDTAG_INTERVAL 4
944 #endif
945 
946 static struct periph_driver adadriver =
947 {
948 	adainit, "ada",
949 	TAILQ_HEAD_INITIALIZER(adadriver.units), /* generation */ 0
950 };
951 
952 static int adadeletemethodsysctl(SYSCTL_HANDLER_ARGS);
953 
954 PERIPHDRIVER_DECLARE(ada, adadriver);
955 
956 static MALLOC_DEFINE(M_ATADA, "ata_da", "ata_da buffers");
957 
958 static int
959 adaopen(struct disk *dp)
960 {
961 	struct cam_periph *periph;
962 	struct ada_softc *softc;
963 	int error;
964 
965 	periph = (struct cam_periph *)dp->d_drv1;
966 	if (cam_periph_acquire(periph) != 0) {
967 		return(ENXIO);
968 	}
969 
970 	cam_periph_lock(periph);
971 	if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
972 		cam_periph_unlock(periph);
973 		cam_periph_release(periph);
974 		return (error);
975 	}
976 
977 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
978 	    ("adaopen\n"));
979 
980 	softc = (struct ada_softc *)periph->softc;
981 	softc->flags |= ADA_FLAG_OPEN;
982 
983 	cam_periph_unhold(periph);
984 	cam_periph_unlock(periph);
985 	return (0);
986 }
987 
988 static int
989 adaclose(struct disk *dp)
990 {
991 	struct	cam_periph *periph;
992 	struct	ada_softc *softc;
993 	union ccb *ccb;
994 	int error;
995 
996 	periph = (struct cam_periph *)dp->d_drv1;
997 	softc = (struct ada_softc *)periph->softc;
998 	cam_periph_lock(periph);
999 
1000 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1001 	    ("adaclose\n"));
1002 
1003 	/* We only sync the cache if the drive is capable of it. */
1004 	if ((softc->flags & ADA_FLAG_DIRTY) != 0 &&
1005 	    (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) != 0 &&
1006 	    (periph->flags & CAM_PERIPH_INVALID) == 0 &&
1007 	    cam_periph_hold(periph, PRIBIO) == 0) {
1008 		ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1009 		cam_fill_ataio(&ccb->ataio,
1010 				    1,
1011 				    NULL,
1012 				    CAM_DIR_NONE,
1013 				    0,
1014 				    NULL,
1015 				    0,
1016 				    ada_default_timeout*1000);
1017 
1018 		if (softc->flags & ADA_FLAG_CAN_48BIT)
1019 			ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0);
1020 		else
1021 			ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0);
1022 		error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0,
1023 		    /*sense_flags*/0, softc->disk->d_devstat);
1024 
1025 		if (error != 0)
1026 			xpt_print(periph->path, "Synchronize cache failed\n");
1027 		softc->flags &= ~ADA_FLAG_DIRTY;
1028 		xpt_release_ccb(ccb);
1029 		cam_periph_unhold(periph);
1030 	}
1031 
1032 	softc->flags &= ~ADA_FLAG_OPEN;
1033 
1034 	while (softc->refcount != 0)
1035 		cam_periph_sleep(periph, &softc->refcount, PRIBIO, "adaclose", 1);
1036 	cam_periph_unlock(periph);
1037 	cam_periph_release(periph);
1038 	return (0);
1039 }
1040 
1041 static void
1042 adaschedule(struct cam_periph *periph)
1043 {
1044 	struct ada_softc *softc = (struct ada_softc *)periph->softc;
1045 
1046 	if (softc->state != ADA_STATE_NORMAL)
1047 		return;
1048 
1049 	cam_iosched_schedule(softc->cam_iosched, periph);
1050 }
1051 
1052 /*
1053  * Actually translate the requested transfer into one the physical driver
1054  * can understand.  The transfer is described by a buf and will include
1055  * only one physical transfer.
1056  */
1057 static void
1058 adastrategy(struct bio *bp)
1059 {
1060 	struct cam_periph *periph;
1061 	struct ada_softc *softc;
1062 
1063 	periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1064 	softc = (struct ada_softc *)periph->softc;
1065 
1066 	cam_periph_lock(periph);
1067 
1068 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adastrategy(%p)\n", bp));
1069 
1070 	/*
1071 	 * If the device has been made invalid, error out
1072 	 */
1073 	if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
1074 		cam_periph_unlock(periph);
1075 		biofinish(bp, NULL, ENXIO);
1076 		return;
1077 	}
1078 
1079 	/*
1080 	 * Zone commands must be ordered, because they can depend on the
1081 	 * effects of previously issued commands, and they may affect
1082 	 * commands after them.
1083 	 */
1084 	if (bp->bio_cmd == BIO_ZONE)
1085 		bp->bio_flags |= BIO_ORDERED;
1086 
1087 	/*
1088 	 * Place it in the queue of disk activities for this disk
1089 	 */
1090 	cam_iosched_queue_work(softc->cam_iosched, bp);
1091 
1092 	/*
1093 	 * Schedule ourselves for performing the work.
1094 	 */
1095 	adaschedule(periph);
1096 	cam_periph_unlock(periph);
1097 
1098 	return;
1099 }
1100 
1101 static int
1102 adadump(void *arg, void *virtual, off_t offset, size_t length)
1103 {
1104 	struct	    cam_periph *periph;
1105 	struct	    ada_softc *softc;
1106 	u_int	    secsize;
1107 	struct	    ccb_ataio ataio;
1108 	struct	    disk *dp;
1109 	uint64_t    lba;
1110 	uint16_t    count;
1111 	int	    error = 0;
1112 
1113 	dp = arg;
1114 	periph = dp->d_drv1;
1115 	softc = (struct ada_softc *)periph->softc;
1116 	secsize = softc->params.secsize;
1117 	lba = offset / secsize;
1118 	count = length / secsize;
1119 	if ((periph->flags & CAM_PERIPH_INVALID) != 0)
1120 		return (ENXIO);
1121 
1122 	memset(&ataio, 0, sizeof(ataio));
1123 	if (length > 0) {
1124 		xpt_setup_ccb(&ataio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1125 		ataio.ccb_h.ccb_state = ADA_CCB_DUMP;
1126 		cam_fill_ataio(&ataio,
1127 		    0,
1128 		    NULL,
1129 		    CAM_DIR_OUT,
1130 		    0,
1131 		    (u_int8_t *) virtual,
1132 		    length,
1133 		    ada_default_timeout*1000);
1134 		if ((softc->flags & ADA_FLAG_CAN_48BIT) &&
1135 		    (lba + count >= ATA_MAX_28BIT_LBA ||
1136 		    count >= 256)) {
1137 			ata_48bit_cmd(&ataio, ATA_WRITE_DMA48,
1138 			    0, lba, count);
1139 		} else {
1140 			ata_28bit_cmd(&ataio, ATA_WRITE_DMA,
1141 			    0, lba, count);
1142 		}
1143 		error = cam_periph_runccb((union ccb *)&ataio, adaerror,
1144 		    0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1145 		if (error != 0)
1146 			printf("Aborting dump due to I/O error.\n");
1147 
1148 		return (error);
1149 	}
1150 
1151 	if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) {
1152 		xpt_setup_ccb(&ataio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1153 
1154 		/*
1155 		 * Tell the drive to flush its internal cache. if we
1156 		 * can't flush in 5s we have big problems. No need to
1157 		 * wait the default 60s to detect problems.
1158 		 */
1159 		ataio.ccb_h.ccb_state = ADA_CCB_DUMP;
1160 		cam_fill_ataio(&ataio,
1161 				    0,
1162 				    NULL,
1163 				    CAM_DIR_NONE,
1164 				    0,
1165 				    NULL,
1166 				    0,
1167 				    5*1000);
1168 
1169 		if (softc->flags & ADA_FLAG_CAN_48BIT)
1170 			ata_48bit_cmd(&ataio, ATA_FLUSHCACHE48, 0, 0, 0);
1171 		else
1172 			ata_28bit_cmd(&ataio, ATA_FLUSHCACHE, 0, 0, 0);
1173 		error = cam_periph_runccb((union ccb *)&ataio, adaerror,
1174 		    0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1175 		if (error != 0)
1176 			xpt_print(periph->path, "Synchronize cache failed\n");
1177 	}
1178 	return (error);
1179 }
1180 
1181 static void
1182 adainit(void)
1183 {
1184 	cam_status status;
1185 
1186 	ada_ccb_zone = uma_zcreate("ada_ccb",
1187 	    sizeof(struct ccb_ataio), NULL, NULL, NULL, NULL,
1188 	    UMA_ALIGN_PTR, 0);
1189 
1190 	/*
1191 	 * Install a global async callback.  This callback will
1192 	 * receive async callbacks like "new device found".
1193 	 */
1194 	status = xpt_register_async(AC_FOUND_DEVICE, adaasync, NULL, NULL);
1195 
1196 	if (status != CAM_REQ_CMP) {
1197 		printf("ada: Failed to attach master async callback "
1198 		       "due to status 0x%x!\n", status);
1199 	} else if (ada_send_ordered) {
1200 		/* Register our event handlers */
1201 		if ((EVENTHANDLER_REGISTER(power_suspend, adasuspend,
1202 					   NULL, EVENTHANDLER_PRI_LAST)) == NULL)
1203 		    printf("adainit: power event registration failed!\n");
1204 		if ((EVENTHANDLER_REGISTER(power_resume, adaresume,
1205 					   NULL, EVENTHANDLER_PRI_LAST)) == NULL)
1206 		    printf("adainit: power event registration failed!\n");
1207 		if ((EVENTHANDLER_REGISTER(shutdown_post_sync, adashutdown,
1208 					   NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
1209 		    printf("adainit: shutdown event registration failed!\n");
1210 	}
1211 }
1212 
1213 /*
1214  * Callback from GEOM, called when it has finished cleaning up its
1215  * resources.
1216  */
1217 static void
1218 adadiskgonecb(struct disk *dp)
1219 {
1220 	struct cam_periph *periph;
1221 
1222 	periph = (struct cam_periph *)dp->d_drv1;
1223 
1224 	cam_periph_release(periph);
1225 }
1226 
1227 static void
1228 adaoninvalidate(struct cam_periph *periph)
1229 {
1230 	struct ada_softc *softc;
1231 
1232 	softc = (struct ada_softc *)periph->softc;
1233 
1234 	/*
1235 	 * De-register any async callbacks.
1236 	 */
1237 	xpt_register_async(0, adaasync, periph, periph->path);
1238 #ifdef CAM_IO_STATS
1239 	softc->invalidations++;
1240 #endif
1241 
1242 	/*
1243 	 * Return all queued I/O with ENXIO. Transactions may be queued up here
1244 	 * for retry (since we are called while there's other transactions
1245 	 * pending). Any requests in the hardware will drain before ndacleanup
1246 	 * is called.
1247 	 */
1248 	cam_iosched_flush(softc->cam_iosched, NULL, ENXIO);
1249 
1250 	/*
1251 	 * Tell GEOM that we've gone away, we'll get a callback when it is
1252 	 * done cleaning up its resources.
1253 	 */
1254 	disk_gone(softc->disk);
1255 }
1256 
1257 static void
1258 adacleanup(struct cam_periph *periph)
1259 {
1260 	struct ada_softc *softc;
1261 
1262 	softc = (struct ada_softc *)periph->softc;
1263 
1264 	cam_periph_unlock(periph);
1265 
1266 	cam_iosched_fini(softc->cam_iosched);
1267 
1268 	/*
1269 	 * If we can't free the sysctl tree, oh well...
1270 	 */
1271 	if ((softc->flags & ADA_FLAG_SCTX_INIT) != 0) {
1272 #ifdef CAM_IO_STATS
1273 		if (sysctl_ctx_free(&softc->sysctl_stats_ctx) != 0)
1274 			xpt_print(periph->path,
1275 			    "can't remove sysctl stats context\n");
1276 #endif
1277 		if (sysctl_ctx_free(&softc->sysctl_ctx) != 0)
1278 			xpt_print(periph->path,
1279 			    "can't remove sysctl context\n");
1280 	}
1281 
1282 	disk_destroy(softc->disk);
1283 	callout_drain(&softc->sendordered_c);
1284 	free(softc, M_DEVBUF);
1285 	cam_periph_lock(periph);
1286 }
1287 
1288 static void
1289 adasetdeletemethod(struct ada_softc *softc)
1290 {
1291 
1292 	if (softc->flags & ADA_FLAG_CAN_NCQ_TRIM)
1293 		softc->delete_method = ADA_DELETE_NCQ_DSM_TRIM;
1294 	else if (softc->flags & ADA_FLAG_CAN_TRIM)
1295 		softc->delete_method = ADA_DELETE_DSM_TRIM;
1296 	else if ((softc->flags & ADA_FLAG_CAN_CFA) && !(softc->flags & ADA_FLAG_CAN_48BIT))
1297 		softc->delete_method = ADA_DELETE_CFA_ERASE;
1298 	else
1299 		softc->delete_method = ADA_DELETE_NONE;
1300 }
1301 
1302 static void
1303 adaasync(void *callback_arg, u_int32_t code,
1304 	struct cam_path *path, void *arg)
1305 {
1306 	struct ccb_getdev cgd;
1307 	struct cam_periph *periph;
1308 	struct ada_softc *softc;
1309 
1310 	periph = (struct cam_periph *)callback_arg;
1311 	switch (code) {
1312 	case AC_FOUND_DEVICE:
1313 	{
1314 		struct ccb_getdev *cgd;
1315 		cam_status status;
1316 
1317 		cgd = (struct ccb_getdev *)arg;
1318 		if (cgd == NULL)
1319 			break;
1320 
1321 		if (cgd->protocol != PROTO_ATA)
1322 			break;
1323 
1324 		/*
1325 		 * Allocate a peripheral instance for
1326 		 * this device and start the probe
1327 		 * process.
1328 		 */
1329 		status = cam_periph_alloc(adaregister, adaoninvalidate,
1330 					  adacleanup, adastart,
1331 					  "ada", CAM_PERIPH_BIO,
1332 					  path, adaasync,
1333 					  AC_FOUND_DEVICE, cgd);
1334 
1335 		if (status != CAM_REQ_CMP
1336 		 && status != CAM_REQ_INPROG)
1337 			printf("adaasync: Unable to attach to new device "
1338 				"due to status 0x%x\n", status);
1339 		break;
1340 	}
1341 	case AC_GETDEV_CHANGED:
1342 	{
1343 		softc = (struct ada_softc *)periph->softc;
1344 		memset(&cgd, 0, sizeof(cgd));
1345 		xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1346 		cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1347 		xpt_action((union ccb *)&cgd);
1348 
1349 		/*
1350 		 * Update our information based on the new Identify data.
1351 		 */
1352 		adasetflags(softc, &cgd);
1353 		adasetgeom(softc, &cgd);
1354 		disk_resize(softc->disk, M_NOWAIT);
1355 		break;
1356 	}
1357 	case AC_ADVINFO_CHANGED:
1358 	{
1359 		uintptr_t buftype;
1360 
1361 		buftype = (uintptr_t)arg;
1362 		if (buftype == CDAI_TYPE_PHYS_PATH) {
1363 			struct ada_softc *softc;
1364 
1365 			softc = periph->softc;
1366 			disk_attr_changed(softc->disk, "GEOM::physpath",
1367 					  M_NOWAIT);
1368 		}
1369 		break;
1370 	}
1371 	case AC_SENT_BDR:
1372 	case AC_BUS_RESET:
1373 	{
1374 		softc = (struct ada_softc *)periph->softc;
1375 		if (softc->state != ADA_STATE_NORMAL)
1376 			break;
1377 		if (ADA_RA >= 0 && softc->flags & ADA_FLAG_CAN_RAHEAD)
1378 			softc->state = ADA_STATE_RAHEAD;
1379 		else if (ADA_WC >= 0 && softc->flags & ADA_FLAG_CAN_WCACHE)
1380 			softc->state = ADA_STATE_WCACHE;
1381 		else if ((softc->flags & ADA_FLAG_CAN_LOG)
1382 		      && (softc->zone_mode != ADA_ZONE_NONE))
1383 			softc->state = ADA_STATE_LOGDIR;
1384 		else
1385 			break;
1386 		if (cam_periph_acquire(periph) != 0)
1387 			softc->state = ADA_STATE_NORMAL;
1388 		else
1389 			xpt_schedule(periph, CAM_PRIORITY_DEV);
1390 		break;
1391 	}
1392 	default:
1393 		break;
1394 	}
1395 	cam_periph_async(periph, code, path, arg);
1396 }
1397 
1398 static int
1399 adazonemodesysctl(SYSCTL_HANDLER_ARGS)
1400 {
1401 	char tmpbuf[40];
1402 	struct ada_softc *softc;
1403 	int error;
1404 
1405 	softc = (struct ada_softc *)arg1;
1406 
1407 	switch (softc->zone_mode) {
1408 	case ADA_ZONE_DRIVE_MANAGED:
1409 		snprintf(tmpbuf, sizeof(tmpbuf), "Drive Managed");
1410 		break;
1411 	case ADA_ZONE_HOST_AWARE:
1412 		snprintf(tmpbuf, sizeof(tmpbuf), "Host Aware");
1413 		break;
1414 	case ADA_ZONE_HOST_MANAGED:
1415 		snprintf(tmpbuf, sizeof(tmpbuf), "Host Managed");
1416 		break;
1417 	case ADA_ZONE_NONE:
1418 	default:
1419 		snprintf(tmpbuf, sizeof(tmpbuf), "Not Zoned");
1420 		break;
1421 	}
1422 
1423 	error = sysctl_handle_string(oidp, tmpbuf, sizeof(tmpbuf), req);
1424 
1425 	return (error);
1426 }
1427 
1428 static int
1429 adazonesupsysctl(SYSCTL_HANDLER_ARGS)
1430 {
1431 	char tmpbuf[180];
1432 	struct ada_softc *softc;
1433 	struct sbuf sb;
1434 	int error, first;
1435 	unsigned int i;
1436 
1437 	softc = (struct ada_softc *)arg1;
1438 
1439 	error = 0;
1440 	first = 1;
1441 	sbuf_new(&sb, tmpbuf, sizeof(tmpbuf), 0);
1442 
1443 	for (i = 0; i < sizeof(ada_zone_desc_table) /
1444 	     sizeof(ada_zone_desc_table[0]); i++) {
1445 		if (softc->zone_flags & ada_zone_desc_table[i].value) {
1446 			if (first == 0)
1447 				sbuf_printf(&sb, ", ");
1448 			else
1449 				first = 0;
1450 			sbuf_cat(&sb, ada_zone_desc_table[i].desc);
1451 		}
1452 	}
1453 
1454 	if (first == 1)
1455 		sbuf_printf(&sb, "None");
1456 
1457 	sbuf_finish(&sb);
1458 
1459 	error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
1460 
1461 	return (error);
1462 }
1463 
1464 static void
1465 adasysctlinit(void *context, int pending)
1466 {
1467 	struct cam_periph *periph;
1468 	struct ada_softc *softc;
1469 	char tmpstr[32], tmpstr2[16];
1470 
1471 	periph = (struct cam_periph *)context;
1472 
1473 	/* periph was held for us when this task was enqueued */
1474 	if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
1475 		cam_periph_release(periph);
1476 		return;
1477 	}
1478 
1479 	softc = (struct ada_softc *)periph->softc;
1480 	snprintf(tmpstr, sizeof(tmpstr), "CAM ADA unit %d",periph->unit_number);
1481 	snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1482 
1483 	sysctl_ctx_init(&softc->sysctl_ctx);
1484 	softc->flags |= ADA_FLAG_SCTX_INIT;
1485 	softc->sysctl_tree = SYSCTL_ADD_NODE_WITH_LABEL(&softc->sysctl_ctx,
1486 		SYSCTL_STATIC_CHILDREN(_kern_cam_ada), OID_AUTO, tmpstr2,
1487 		CTLFLAG_RD | CTLFLAG_MPSAFE, 0, tmpstr, "device_index");
1488 	if (softc->sysctl_tree == NULL) {
1489 		printf("adasysctlinit: unable to allocate sysctl tree\n");
1490 		cam_periph_release(periph);
1491 		return;
1492 	}
1493 
1494 	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1495 		OID_AUTO, "delete_method",
1496 		CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
1497 		softc, 0, adadeletemethodsysctl, "A",
1498 		"BIO_DELETE execution method");
1499 	SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
1500 		SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
1501 		"trim_count", CTLFLAG_RD, &softc->trim_count,
1502 		"Total number of dsm commands sent");
1503 	SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
1504 		SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
1505 		"trim_ranges", CTLFLAG_RD, &softc->trim_ranges,
1506 		"Total number of ranges in dsm commands");
1507 	SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
1508 		SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
1509 		"trim_lbas", CTLFLAG_RD, &softc->trim_lbas,
1510 		"Total lbas in the dsm commands sent");
1511 	SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1512 		OID_AUTO, "read_ahead", CTLFLAG_RW | CTLFLAG_MPSAFE,
1513 		&softc->read_ahead, 0, "Enable disk read ahead.");
1514 	SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1515 		OID_AUTO, "write_cache", CTLFLAG_RW | CTLFLAG_MPSAFE,
1516 		&softc->write_cache, 0, "Enable disk write cache.");
1517 	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1518 		OID_AUTO, "zone_mode",
1519 		CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
1520 		softc, 0, adazonemodesysctl, "A",
1521 		"Zone Mode");
1522 	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1523 		OID_AUTO, "zone_support",
1524 		CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
1525 		softc, 0, adazonesupsysctl, "A",
1526 		"Zone Support");
1527 	SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
1528 		SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
1529 		"optimal_seq_zones", CTLFLAG_RD, &softc->optimal_seq_zones,
1530 		"Optimal Number of Open Sequential Write Preferred Zones");
1531 	SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
1532 		SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
1533 		"optimal_nonseq_zones", CTLFLAG_RD,
1534 		&softc->optimal_nonseq_zones,
1535 		"Optimal Number of Non-Sequentially Written Sequential Write "
1536 		"Preferred Zones");
1537 	SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
1538 		SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
1539 		"max_seq_zones", CTLFLAG_RD, &softc->max_seq_zones,
1540 		"Maximum Number of Open Sequential Write Required Zones");
1541 	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1542 	    OID_AUTO, "flags", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
1543 	    softc, 0, adaflagssysctl, "A",
1544 	    "Flags for drive");
1545 	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1546 	    OID_AUTO, "unmapped_io", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
1547 	    &softc->flags, (u_int)ADA_FLAG_UNMAPPEDIO, adabitsysctl, "I",
1548 	    "Unmapped I/O support *DEPRECATED* gone in FreeBSD 14");
1549 	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1550 	    OID_AUTO, "rotating", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
1551 	    &softc->flags, (u_int)ADA_FLAG_ROTATING, adabitsysctl, "I",
1552 	    "Rotating media *DEPRECATED* gone in FreeBSD 14");
1553 
1554 #ifdef CAM_TEST_FAILURE
1555 	/*
1556 	 * Add a 'door bell' sysctl which allows one to set it from userland
1557 	 * and cause something bad to happen.  For the moment, we only allow
1558 	 * whacking the next read or write.
1559 	 */
1560 	SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1561 		OID_AUTO, "force_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
1562 		&softc->force_read_error, 0,
1563 		"Force a read error for the next N reads.");
1564 	SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1565 		OID_AUTO, "force_write_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
1566 		&softc->force_write_error, 0,
1567 		"Force a write error for the next N writes.");
1568 	SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1569 		OID_AUTO, "periodic_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
1570 		&softc->periodic_read_error, 0,
1571 		"Force a read error every N reads (don't set too low).");
1572 	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1573 		OID_AUTO, "invalidate", CTLTYPE_U64 | CTLFLAG_RW | CTLFLAG_MPSAFE,
1574 		periph, 0, cam_periph_invalidate_sysctl, "I",
1575 		"Write 1 to invalidate the drive immediately");
1576 #endif
1577 
1578 #ifdef CAM_IO_STATS
1579 	softc->sysctl_stats_tree = SYSCTL_ADD_NODE(&softc->sysctl_stats_ctx,
1580 		SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, "stats",
1581 		CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "Statistics");
1582 	SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
1583 		SYSCTL_CHILDREN(softc->sysctl_stats_tree),
1584 		OID_AUTO, "timeouts", CTLFLAG_RD | CTLFLAG_MPSAFE,
1585 		&softc->timeouts, 0,
1586 		"Device timeouts reported by the SIM");
1587 	SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
1588 		SYSCTL_CHILDREN(softc->sysctl_stats_tree),
1589 		OID_AUTO, "errors", CTLFLAG_RD | CTLFLAG_MPSAFE,
1590 		&softc->errors, 0,
1591 		"Transport errors reported by the SIM.");
1592 	SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
1593 		SYSCTL_CHILDREN(softc->sysctl_stats_tree),
1594 		OID_AUTO, "pack_invalidations", CTLFLAG_RD | CTLFLAG_MPSAFE,
1595 		&softc->invalidations, 0,
1596 		"Device pack invalidations.");
1597 #endif
1598 
1599 	cam_iosched_sysctl_init(softc->cam_iosched, &softc->sysctl_ctx,
1600 	    softc->sysctl_tree);
1601 
1602 	cam_periph_release(periph);
1603 }
1604 
1605 static int
1606 adagetattr(struct bio *bp)
1607 {
1608 	int ret;
1609 	struct cam_periph *periph;
1610 
1611 	if (g_handleattr_int(bp, "GEOM::canspeedup", ada_enable_biospeedup))
1612 		return (EJUSTRETURN);
1613 
1614 	periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1615 	cam_periph_lock(periph);
1616 	ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
1617 	    periph->path);
1618 	cam_periph_unlock(periph);
1619 	if (ret == 0)
1620 		bp->bio_completed = bp->bio_length;
1621 	return ret;
1622 }
1623 
1624 static int
1625 adadeletemethodsysctl(SYSCTL_HANDLER_ARGS)
1626 {
1627 	char buf[16];
1628 	const char *p;
1629 	struct ada_softc *softc;
1630 	int i, error, value, methods;
1631 
1632 	softc = (struct ada_softc *)arg1;
1633 
1634 	value = softc->delete_method;
1635 	if (value < 0 || value > ADA_DELETE_MAX)
1636 		p = "UNKNOWN";
1637 	else
1638 		p = ada_delete_method_names[value];
1639 	strncpy(buf, p, sizeof(buf));
1640 	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
1641 	if (error != 0 || req->newptr == NULL)
1642 		return (error);
1643 	methods = 1 << ADA_DELETE_DISABLE;
1644 	if ((softc->flags & ADA_FLAG_CAN_CFA) &&
1645 	    !(softc->flags & ADA_FLAG_CAN_48BIT))
1646 		methods |= 1 << ADA_DELETE_CFA_ERASE;
1647 	if (softc->flags & ADA_FLAG_CAN_TRIM)
1648 		methods |= 1 << ADA_DELETE_DSM_TRIM;
1649 	if (softc->flags & ADA_FLAG_CAN_NCQ_TRIM)
1650 		methods |= 1 << ADA_DELETE_NCQ_DSM_TRIM;
1651 	for (i = 0; i <= ADA_DELETE_MAX; i++) {
1652 		if (!(methods & (1 << i)) ||
1653 		    strcmp(buf, ada_delete_method_names[i]) != 0)
1654 			continue;
1655 		softc->delete_method = i;
1656 		return (0);
1657 	}
1658 	return (EINVAL);
1659 }
1660 
1661 static int
1662 adabitsysctl(SYSCTL_HANDLER_ARGS)
1663 {
1664 	u_int *flags = arg1;
1665 	u_int test = arg2;
1666 	int tmpout, error;
1667 
1668 	tmpout = !!(*flags & test);
1669 	error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1670 	if (error || !req->newptr)
1671 		return (error);
1672 
1673 	return (EPERM);
1674 }
1675 
1676 static int
1677 adaflagssysctl(SYSCTL_HANDLER_ARGS)
1678 {
1679 	struct sbuf sbuf;
1680 	struct ada_softc *softc = arg1;
1681 	int error;
1682 
1683 	sbuf_new_for_sysctl(&sbuf, NULL, 0, req);
1684 	if (softc->flags != 0)
1685 		sbuf_printf(&sbuf, "0x%b", (unsigned)softc->flags, ADA_FLAG_STRING);
1686 	else
1687 		sbuf_printf(&sbuf, "0");
1688 	error = sbuf_finish(&sbuf);
1689 	sbuf_delete(&sbuf);
1690 
1691 	return (error);
1692 }
1693 
1694 static void
1695 adasetflags(struct ada_softc *softc, struct ccb_getdev *cgd)
1696 {
1697 	if ((cgd->ident_data.capabilities1 & ATA_SUPPORT_DMA) &&
1698 	    (cgd->inq_flags & SID_DMA))
1699 		softc->flags |= ADA_FLAG_CAN_DMA;
1700 	else
1701 		softc->flags &= ~ADA_FLAG_CAN_DMA;
1702 
1703 	if (cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) {
1704 		softc->flags |= ADA_FLAG_CAN_48BIT;
1705 		if (cgd->inq_flags & SID_DMA48)
1706 			softc->flags |= ADA_FLAG_CAN_DMA48;
1707 		else
1708 			softc->flags &= ~ADA_FLAG_CAN_DMA48;
1709 	} else
1710 		softc->flags &= ~(ADA_FLAG_CAN_48BIT | ADA_FLAG_CAN_DMA48);
1711 
1712 	if (cgd->ident_data.support.command2 & ATA_SUPPORT_FLUSHCACHE)
1713 		softc->flags |= ADA_FLAG_CAN_FLUSHCACHE;
1714 	else
1715 		softc->flags &= ~ADA_FLAG_CAN_FLUSHCACHE;
1716 
1717 	if (cgd->ident_data.support.command1 & ATA_SUPPORT_POWERMGT)
1718 		softc->flags |= ADA_FLAG_CAN_POWERMGT;
1719 	else
1720 		softc->flags &= ~ADA_FLAG_CAN_POWERMGT;
1721 
1722 	if ((cgd->ident_data.satacapabilities & ATA_SUPPORT_NCQ) &&
1723 	    (cgd->inq_flags & SID_DMA) && (cgd->inq_flags & SID_CmdQue))
1724 		softc->flags |= ADA_FLAG_CAN_NCQ;
1725 	else
1726 		softc->flags &= ~ADA_FLAG_CAN_NCQ;
1727 
1728 	if ((cgd->ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) &&
1729 	    (cgd->inq_flags & SID_DMA) &&
1730 	    (softc->quirks & ADA_Q_NO_TRIM) == 0) {
1731 		softc->flags |= ADA_FLAG_CAN_TRIM;
1732 		softc->trim_max_ranges = TRIM_MAX_RANGES;
1733 		if (cgd->ident_data.max_dsm_blocks != 0) {
1734 			softc->trim_max_ranges =
1735 			    min(cgd->ident_data.max_dsm_blocks *
1736 				ATA_DSM_BLK_RANGES, softc->trim_max_ranges);
1737 		}
1738 		/*
1739 		 * If we can do RCVSND_FPDMA_QUEUED commands, we may be able
1740 		 * to do NCQ trims, if we support trims at all. We also need
1741 		 * support from the SIM to do things properly. Perhaps we
1742 		 * should look at log 13 dword 0 bit 0 and dword 1 bit 0 are
1743 		 * set too...
1744 		 */
1745 		if ((softc->quirks & ADA_Q_NCQ_TRIM_BROKEN) == 0 &&
1746 		    (softc->flags & ADA_FLAG_PIM_ATA_EXT) != 0 &&
1747 		    (cgd->ident_data.satacapabilities2 &
1748 		     ATA_SUPPORT_RCVSND_FPDMA_QUEUED) != 0 &&
1749 		    (softc->flags & ADA_FLAG_CAN_TRIM) != 0)
1750 			softc->flags |= ADA_FLAG_CAN_NCQ_TRIM;
1751 		else
1752 			softc->flags &= ~ADA_FLAG_CAN_NCQ_TRIM;
1753 	} else
1754 		softc->flags &= ~(ADA_FLAG_CAN_TRIM | ADA_FLAG_CAN_NCQ_TRIM);
1755 
1756 	if (cgd->ident_data.support.command2 & ATA_SUPPORT_CFA)
1757 		softc->flags |= ADA_FLAG_CAN_CFA;
1758 	else
1759 		softc->flags &= ~ADA_FLAG_CAN_CFA;
1760 
1761 	/*
1762 	 * Now that we've set the appropriate flags, setup the delete
1763 	 * method.
1764 	 */
1765 	adasetdeletemethod(softc);
1766 
1767 	if ((cgd->ident_data.support.extension & ATA_SUPPORT_GENLOG)
1768 	 && ((softc->quirks & ADA_Q_LOG_BROKEN) == 0))
1769 		softc->flags |= ADA_FLAG_CAN_LOG;
1770 	else
1771 		softc->flags &= ~ADA_FLAG_CAN_LOG;
1772 
1773 	if ((cgd->ident_data.support3 & ATA_SUPPORT_ZONE_MASK) ==
1774 	     ATA_SUPPORT_ZONE_HOST_AWARE)
1775 		softc->zone_mode = ADA_ZONE_HOST_AWARE;
1776 	else if (((cgd->ident_data.support3 & ATA_SUPPORT_ZONE_MASK) ==
1777 		   ATA_SUPPORT_ZONE_DEV_MANAGED)
1778 	      || (softc->quirks & ADA_Q_SMR_DM))
1779 		softc->zone_mode = ADA_ZONE_DRIVE_MANAGED;
1780 	else
1781 		softc->zone_mode = ADA_ZONE_NONE;
1782 
1783 	if (cgd->ident_data.support.command1 & ATA_SUPPORT_LOOKAHEAD)
1784 		softc->flags |= ADA_FLAG_CAN_RAHEAD;
1785 	else
1786 		softc->flags &= ~ADA_FLAG_CAN_RAHEAD;
1787 
1788 	if (cgd->ident_data.support.command1 & ATA_SUPPORT_WRITECACHE)
1789 		softc->flags |= ADA_FLAG_CAN_WCACHE;
1790 	else
1791 		softc->flags &= ~ADA_FLAG_CAN_WCACHE;
1792 }
1793 
1794 static cam_status
1795 adaregister(struct cam_periph *periph, void *arg)
1796 {
1797 	struct ada_softc *softc;
1798 	struct ccb_getdev *cgd;
1799 	struct disk_params *dp;
1800 	struct sbuf sb;
1801 	char   *announce_buf;
1802 	caddr_t match;
1803 	int quirks;
1804 
1805 	cgd = (struct ccb_getdev *)arg;
1806 	if (cgd == NULL) {
1807 		printf("adaregister: no getdev CCB, can't register device\n");
1808 		return(CAM_REQ_CMP_ERR);
1809 	}
1810 
1811 	softc = (struct ada_softc *)malloc(sizeof(*softc), M_DEVBUF,
1812 	    M_NOWAIT|M_ZERO);
1813 
1814 	if (softc == NULL) {
1815 		printf("adaregister: Unable to probe new device. "
1816 		    "Unable to allocate softc\n");
1817 		return(CAM_REQ_CMP_ERR);
1818 	}
1819 
1820 	announce_buf = softc->announce_temp;
1821 	bzero(announce_buf, ADA_ANNOUNCETMP_SZ);
1822 
1823 	if (cam_iosched_init(&softc->cam_iosched, periph) != 0) {
1824 		printf("adaregister: Unable to probe new device. "
1825 		       "Unable to allocate iosched memory\n");
1826 		free(softc, M_DEVBUF);
1827 		return(CAM_REQ_CMP_ERR);
1828 	}
1829 
1830 	periph->softc = softc;
1831 	xpt_path_inq(&softc->cpi, periph->path);
1832 
1833 	/*
1834 	 * See if this device has any quirks.
1835 	 */
1836 	match = cam_quirkmatch((caddr_t)&cgd->ident_data,
1837 			       (caddr_t)ada_quirk_table,
1838 			       nitems(ada_quirk_table),
1839 			       sizeof(*ada_quirk_table), ata_identify_match);
1840 	if (match != NULL)
1841 		softc->quirks = ((struct ada_quirk_entry *)match)->quirks;
1842 	else
1843 		softc->quirks = ADA_Q_NONE;
1844 
1845 	TASK_INIT(&softc->sysctl_task, 0, adasysctlinit, periph);
1846 
1847 	/*
1848 	 * Register this media as a disk
1849 	 */
1850 	(void)cam_periph_hold(periph, PRIBIO);
1851 	cam_periph_unlock(periph);
1852 	snprintf(announce_buf, ADA_ANNOUNCETMP_SZ,
1853 	    "kern.cam.ada.%d.quirks", periph->unit_number);
1854 	quirks = softc->quirks;
1855 	TUNABLE_INT_FETCH(announce_buf, &quirks);
1856 	softc->quirks = quirks;
1857 	softc->read_ahead = -1;
1858 	snprintf(announce_buf, ADA_ANNOUNCETMP_SZ,
1859 	    "kern.cam.ada.%d.read_ahead", periph->unit_number);
1860 	TUNABLE_INT_FETCH(announce_buf, &softc->read_ahead);
1861 	softc->write_cache = -1;
1862 	snprintf(announce_buf, ADA_ANNOUNCETMP_SZ,
1863 	    "kern.cam.ada.%d.write_cache", periph->unit_number);
1864 	TUNABLE_INT_FETCH(announce_buf, &softc->write_cache);
1865 
1866 	/*
1867 	 * Let XPT know we can use UMA-allocated CCBs.
1868 	 */
1869 	if (ada_enable_uma_ccbs) {
1870 		KASSERT(ada_ccb_zone != NULL,
1871 		    ("%s: NULL ada_ccb_zone", __func__));
1872 		periph->ccb_zone = ada_ccb_zone;
1873 	}
1874 
1875 	/*
1876 	 * Set support flags based on the Identify data and quirks.
1877 	 */
1878 	adasetflags(softc, cgd);
1879 	if (softc->cpi.hba_misc & PIM_ATA_EXT)
1880 		softc->flags |= ADA_FLAG_PIM_ATA_EXT;
1881 
1882 	/* Disable queue sorting for non-rotational media by default. */
1883 	if (cgd->ident_data.media_rotation_rate == ATA_RATE_NON_ROTATING) {
1884 		softc->flags &= ~ADA_FLAG_ROTATING;
1885 	} else {
1886 		softc->flags |= ADA_FLAG_ROTATING;
1887 	}
1888 	cam_iosched_set_sort_queue(softc->cam_iosched,
1889 	    (softc->flags & ADA_FLAG_ROTATING) ? -1 : 0);
1890 	softc->disk = disk_alloc();
1891 	adasetgeom(softc, cgd);
1892 	softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
1893 			  periph->unit_number, softc->params.secsize,
1894 			  DEVSTAT_ALL_SUPPORTED,
1895 			  DEVSTAT_TYPE_DIRECT |
1896 			  XPORT_DEVSTAT_TYPE(softc->cpi.transport),
1897 			  DEVSTAT_PRIORITY_DISK);
1898 	softc->disk->d_open = adaopen;
1899 	softc->disk->d_close = adaclose;
1900 	softc->disk->d_strategy = adastrategy;
1901 	softc->disk->d_getattr = adagetattr;
1902 	if (cam_sim_pollable(periph->sim))
1903 		softc->disk->d_dump = adadump;
1904 	softc->disk->d_gone = adadiskgonecb;
1905 	softc->disk->d_name = "ada";
1906 	softc->disk->d_drv1 = periph;
1907 	softc->disk->d_unit = periph->unit_number;
1908 
1909 	/*
1910 	 * Acquire a reference to the periph before we register with GEOM.
1911 	 * We'll release this reference once GEOM calls us back (via
1912 	 * adadiskgonecb()) telling us that our provider has been freed.
1913 	 */
1914 	if (cam_periph_acquire(periph) != 0) {
1915 		xpt_print(periph->path, "%s: lost periph during "
1916 			  "registration!\n", __func__);
1917 		cam_periph_lock(periph);
1918 		return (CAM_REQ_CMP_ERR);
1919 	}
1920 	disk_create(softc->disk, DISK_VERSION);
1921 	cam_periph_lock(periph);
1922 
1923 	dp = &softc->params;
1924 	snprintf(announce_buf, ADA_ANNOUNCETMP_SZ,
1925 	    "%juMB (%ju %u byte sectors)",
1926 	    ((uintmax_t)dp->secsize * dp->sectors) / (1024 * 1024),
1927 	    (uintmax_t)dp->sectors, dp->secsize);
1928 
1929 	sbuf_new(&sb, softc->announce_buffer, ADA_ANNOUNCE_SZ, SBUF_FIXEDLEN);
1930 	xpt_announce_periph_sbuf(periph, &sb, announce_buf);
1931 	xpt_announce_quirks_sbuf(periph, &sb, softc->quirks, ADA_Q_BIT_STRING);
1932 	sbuf_finish(&sb);
1933 	sbuf_putbuf(&sb);
1934 
1935 	/*
1936 	 * Create our sysctl variables, now that we know
1937 	 * we have successfully attached.
1938 	 */
1939 	if (cam_periph_acquire(periph) == 0)
1940 		taskqueue_enqueue(taskqueue_thread, &softc->sysctl_task);
1941 
1942 	/*
1943 	 * Add async callbacks for bus reset and
1944 	 * bus device reset calls.  I don't bother
1945 	 * checking if this fails as, in most cases,
1946 	 * the system will function just fine without
1947 	 * them and the only alternative would be to
1948 	 * not attach the device on failure.
1949 	 */
1950 	xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
1951 	    AC_GETDEV_CHANGED | AC_ADVINFO_CHANGED,
1952 	    adaasync, periph, periph->path);
1953 
1954 	/*
1955 	 * Schedule a periodic event to occasionally send an
1956 	 * ordered tag to a device.
1957 	 */
1958 	callout_init_mtx(&softc->sendordered_c, cam_periph_mtx(periph), 0);
1959 	callout_reset_sbt(&softc->sendordered_c,
1960 	    SBT_1S / ADA_ORDEREDTAG_INTERVAL * ada_default_timeout, 0,
1961 	    adasendorderedtag, softc, C_PREL(1));
1962 
1963 	if (ADA_RA >= 0 && softc->flags & ADA_FLAG_CAN_RAHEAD) {
1964 		softc->state = ADA_STATE_RAHEAD;
1965 	} else if (ADA_WC >= 0 && softc->flags & ADA_FLAG_CAN_WCACHE) {
1966 		softc->state = ADA_STATE_WCACHE;
1967 	} else if ((softc->flags & ADA_FLAG_CAN_LOG)
1968 		&& (softc->zone_mode != ADA_ZONE_NONE)) {
1969 		softc->state = ADA_STATE_LOGDIR;
1970 	} else {
1971 		/*
1972 		 * Nothing to probe, so we can just transition to the
1973 		 * normal state.
1974 		 */
1975 		adaprobedone(periph, NULL);
1976 		return(CAM_REQ_CMP);
1977 	}
1978 
1979 	xpt_schedule(periph, CAM_PRIORITY_DEV);
1980 
1981 	return(CAM_REQ_CMP);
1982 }
1983 
1984 static int
1985 ada_dsmtrim_req_create(struct ada_softc *softc, struct bio *bp, struct trim_request *req)
1986 {
1987 	uint64_t lastlba = (uint64_t)-1, lbas = 0;
1988 	int c, lastcount = 0, off, ranges = 0;
1989 
1990 	bzero(req, sizeof(*req));
1991 	TAILQ_INIT(&req->bps);
1992 	do {
1993 		uint64_t lba = bp->bio_pblkno;
1994 		int count = bp->bio_bcount / softc->params.secsize;
1995 
1996 		/* Try to extend the previous range. */
1997 		if (lba == lastlba) {
1998 			c = min(count, ATA_DSM_RANGE_MAX - lastcount);
1999 			lastcount += c;
2000 			off = (ranges - 1) * ATA_DSM_RANGE_SIZE;
2001 			req->data[off + 6] = lastcount & 0xff;
2002 			req->data[off + 7] =
2003 				(lastcount >> 8) & 0xff;
2004 			count -= c;
2005 			lba += c;
2006 			lbas += c;
2007 		}
2008 
2009 		while (count > 0) {
2010 			c = min(count, ATA_DSM_RANGE_MAX);
2011 			off = ranges * ATA_DSM_RANGE_SIZE;
2012 			req->data[off + 0] = lba & 0xff;
2013 			req->data[off + 1] = (lba >> 8) & 0xff;
2014 			req->data[off + 2] = (lba >> 16) & 0xff;
2015 			req->data[off + 3] = (lba >> 24) & 0xff;
2016 			req->data[off + 4] = (lba >> 32) & 0xff;
2017 			req->data[off + 5] = (lba >> 40) & 0xff;
2018 			req->data[off + 6] = c & 0xff;
2019 			req->data[off + 7] = (c >> 8) & 0xff;
2020 			lba += c;
2021 			lbas += c;
2022 			count -= c;
2023 			lastcount = c;
2024 			ranges++;
2025 			/*
2026 			 * Its the caller's responsibility to ensure the
2027 			 * request will fit so we don't need to check for
2028 			 * overrun here
2029 			 */
2030 		}
2031 		lastlba = lba;
2032 		TAILQ_INSERT_TAIL(&req->bps, bp, bio_queue);
2033 
2034 		bp = cam_iosched_next_trim(softc->cam_iosched);
2035 		if (bp == NULL)
2036 			break;
2037 		if (bp->bio_bcount / softc->params.secsize >
2038 		    (softc->trim_max_ranges - ranges) * ATA_DSM_RANGE_MAX) {
2039 			cam_iosched_put_back_trim(softc->cam_iosched, bp);
2040 			break;
2041 		}
2042 	} while (1);
2043 	softc->trim_count++;
2044 	softc->trim_ranges += ranges;
2045 	softc->trim_lbas += lbas;
2046 
2047 	return (ranges);
2048 }
2049 
2050 static void
2051 ada_dsmtrim(struct ada_softc *softc, struct bio *bp, struct ccb_ataio *ataio)
2052 {
2053 	struct trim_request *req = &softc->trim_req;
2054 	int ranges;
2055 
2056 	ranges = ada_dsmtrim_req_create(softc, bp, req);
2057 	cam_fill_ataio(ataio,
2058 	    ada_retry_count,
2059 	    adadone,
2060 	    CAM_DIR_OUT,
2061 	    0,
2062 	    req->data,
2063 	    howmany(ranges, ATA_DSM_BLK_RANGES) * ATA_DSM_BLK_SIZE,
2064 	    ada_default_timeout * 1000);
2065 	ata_48bit_cmd(ataio, ATA_DATA_SET_MANAGEMENT,
2066 	    ATA_DSM_TRIM, 0, howmany(ranges, ATA_DSM_BLK_RANGES));
2067 }
2068 
2069 static void
2070 ada_ncq_dsmtrim(struct ada_softc *softc, struct bio *bp, struct ccb_ataio *ataio)
2071 {
2072 	struct trim_request *req = &softc->trim_req;
2073 	int ranges;
2074 
2075 	ranges = ada_dsmtrim_req_create(softc, bp, req);
2076 	cam_fill_ataio(ataio,
2077 	    ada_retry_count,
2078 	    adadone,
2079 	    CAM_DIR_OUT,
2080 	    0,
2081 	    req->data,
2082 	    howmany(ranges, ATA_DSM_BLK_RANGES) * ATA_DSM_BLK_SIZE,
2083 	    ada_default_timeout * 1000);
2084 	ata_ncq_cmd(ataio,
2085 	    ATA_SEND_FPDMA_QUEUED,
2086 	    0,
2087 	    howmany(ranges, ATA_DSM_BLK_RANGES));
2088 	ataio->cmd.sector_count_exp = ATA_SFPDMA_DSM;
2089 	ataio->ata_flags |= ATA_FLAG_AUX;
2090 	ataio->aux = 1;
2091 }
2092 
2093 static void
2094 ada_cfaerase(struct ada_softc *softc, struct bio *bp, struct ccb_ataio *ataio)
2095 {
2096 	struct trim_request *req = &softc->trim_req;
2097 	uint64_t lba = bp->bio_pblkno;
2098 	uint16_t count = bp->bio_bcount / softc->params.secsize;
2099 
2100 	bzero(req, sizeof(*req));
2101 	TAILQ_INIT(&req->bps);
2102 	TAILQ_INSERT_TAIL(&req->bps, bp, bio_queue);
2103 
2104 	cam_fill_ataio(ataio,
2105 	    ada_retry_count,
2106 	    adadone,
2107 	    CAM_DIR_NONE,
2108 	    0,
2109 	    NULL,
2110 	    0,
2111 	    ada_default_timeout*1000);
2112 
2113 	if (count >= 256)
2114 		count = 0;
2115 	ata_28bit_cmd(ataio, ATA_CFA_ERASE, 0, lba, count);
2116 }
2117 
2118 static int
2119 ada_zone_bio_to_ata(int disk_zone_cmd)
2120 {
2121 	switch (disk_zone_cmd) {
2122 	case DISK_ZONE_OPEN:
2123 		return ATA_ZM_OPEN_ZONE;
2124 	case DISK_ZONE_CLOSE:
2125 		return ATA_ZM_CLOSE_ZONE;
2126 	case DISK_ZONE_FINISH:
2127 		return ATA_ZM_FINISH_ZONE;
2128 	case DISK_ZONE_RWP:
2129 		return ATA_ZM_RWP;
2130 	}
2131 
2132 	return -1;
2133 }
2134 
2135 static int
2136 ada_zone_cmd(struct cam_periph *periph, union ccb *ccb, struct bio *bp,
2137 	     int *queue_ccb)
2138 {
2139 	struct ada_softc *softc;
2140 	int error;
2141 
2142 	error = 0;
2143 
2144 	if (bp->bio_cmd != BIO_ZONE) {
2145 		error = EINVAL;
2146 		goto bailout;
2147 	}
2148 
2149 	softc = periph->softc;
2150 
2151 	switch (bp->bio_zone.zone_cmd) {
2152 	case DISK_ZONE_OPEN:
2153 	case DISK_ZONE_CLOSE:
2154 	case DISK_ZONE_FINISH:
2155 	case DISK_ZONE_RWP: {
2156 		int zone_flags;
2157 		int zone_sa;
2158 		uint64_t lba;
2159 
2160 		zone_sa = ada_zone_bio_to_ata(bp->bio_zone.zone_cmd);
2161 		if (zone_sa == -1) {
2162 			xpt_print(periph->path, "Cannot translate zone "
2163 			    "cmd %#x to ATA\n", bp->bio_zone.zone_cmd);
2164 			error = EINVAL;
2165 			goto bailout;
2166 		}
2167 
2168 		zone_flags = 0;
2169 		lba = bp->bio_zone.zone_params.rwp.id;
2170 
2171 		if (bp->bio_zone.zone_params.rwp.flags &
2172 		    DISK_ZONE_RWP_FLAG_ALL)
2173 			zone_flags |= ZBC_OUT_ALL;
2174 
2175 		ata_zac_mgmt_out(&ccb->ataio,
2176 				 /*retries*/ ada_retry_count,
2177 				 /*cbfcnp*/ adadone,
2178 				 /*use_ncq*/ (softc->flags &
2179 					      ADA_FLAG_PIM_ATA_EXT) ? 1 : 0,
2180 				 /*zm_action*/ zone_sa,
2181 				 /*zone_id*/ lba,
2182 				 /*zone_flags*/ zone_flags,
2183 				 /*sector_count*/ 0,
2184 				 /*data_ptr*/ NULL,
2185 				 /*dxfer_len*/ 0,
2186 				 /*timeout*/ ada_default_timeout * 1000);
2187 		*queue_ccb = 1;
2188 
2189 		break;
2190 	}
2191 	case DISK_ZONE_REPORT_ZONES: {
2192 		uint8_t *rz_ptr;
2193 		uint32_t num_entries, alloc_size;
2194 		struct disk_zone_report *rep;
2195 
2196 		rep = &bp->bio_zone.zone_params.report;
2197 
2198 		num_entries = rep->entries_allocated;
2199 		if (num_entries == 0) {
2200 			xpt_print(periph->path, "No entries allocated for "
2201 			    "Report Zones request\n");
2202 			error = EINVAL;
2203 			goto bailout;
2204 		}
2205 		alloc_size = sizeof(struct scsi_report_zones_hdr) +
2206 		    (sizeof(struct scsi_report_zones_desc) * num_entries);
2207 		alloc_size = min(alloc_size, softc->disk->d_maxsize);
2208 		rz_ptr = malloc(alloc_size, M_ATADA, M_NOWAIT | M_ZERO);
2209 		if (rz_ptr == NULL) {
2210 			xpt_print(periph->path, "Unable to allocate memory "
2211 			   "for Report Zones request\n");
2212 			error = ENOMEM;
2213 			goto bailout;
2214 		}
2215 
2216 		ata_zac_mgmt_in(&ccb->ataio,
2217 				/*retries*/ ada_retry_count,
2218 				/*cbcfnp*/ adadone,
2219 				/*use_ncq*/ (softc->flags &
2220 					     ADA_FLAG_PIM_ATA_EXT) ? 1 : 0,
2221 				/*zm_action*/ ATA_ZM_REPORT_ZONES,
2222 				/*zone_id*/ rep->starting_id,
2223 				/*zone_flags*/ rep->rep_options,
2224 				/*data_ptr*/ rz_ptr,
2225 				/*dxfer_len*/ alloc_size,
2226 				/*timeout*/ ada_default_timeout * 1000);
2227 
2228 		/*
2229 		 * For BIO_ZONE, this isn't normally needed.  However, it
2230 		 * is used by devstat_end_transaction_bio() to determine
2231 		 * how much data was transferred.
2232 		 */
2233 		/*
2234 		 * XXX KDM we have a problem.  But I'm not sure how to fix
2235 		 * it.  devstat uses bio_bcount - bio_resid to calculate
2236 		 * the amount of data transferred.   The GEOM disk code
2237 		 * uses bio_length - bio_resid to calculate the amount of
2238 		 * data in bio_completed.  We have different structure
2239 		 * sizes above and below the ada(4) driver.  So, if we
2240 		 * use the sizes above, the amount transferred won't be
2241 		 * quite accurate for devstat.  If we use different sizes
2242 		 * for bio_bcount and bio_length (above and below
2243 		 * respectively), then the residual needs to match one or
2244 		 * the other.  Everything is calculated after the bio
2245 		 * leaves the driver, so changing the values around isn't
2246 		 * really an option.  For now, just set the count to the
2247 		 * passed in length.  This means that the calculations
2248 		 * above (e.g. bio_completed) will be correct, but the
2249 		 * amount of data reported to devstat will be slightly
2250 		 * under or overstated.
2251 		 */
2252 		bp->bio_bcount = bp->bio_length;
2253 
2254 		*queue_ccb = 1;
2255 
2256 		break;
2257 	}
2258 	case DISK_ZONE_GET_PARAMS: {
2259 		struct disk_zone_disk_params *params;
2260 
2261 		params = &bp->bio_zone.zone_params.disk_params;
2262 		bzero(params, sizeof(*params));
2263 
2264 		switch (softc->zone_mode) {
2265 		case ADA_ZONE_DRIVE_MANAGED:
2266 			params->zone_mode = DISK_ZONE_MODE_DRIVE_MANAGED;
2267 			break;
2268 		case ADA_ZONE_HOST_AWARE:
2269 			params->zone_mode = DISK_ZONE_MODE_HOST_AWARE;
2270 			break;
2271 		case ADA_ZONE_HOST_MANAGED:
2272 			params->zone_mode = DISK_ZONE_MODE_HOST_MANAGED;
2273 			break;
2274 		default:
2275 		case ADA_ZONE_NONE:
2276 			params->zone_mode = DISK_ZONE_MODE_NONE;
2277 			break;
2278 		}
2279 
2280 		if (softc->zone_flags & ADA_ZONE_FLAG_URSWRZ)
2281 			params->flags |= DISK_ZONE_DISK_URSWRZ;
2282 
2283 		if (softc->zone_flags & ADA_ZONE_FLAG_OPT_SEQ_SET) {
2284 			params->optimal_seq_zones = softc->optimal_seq_zones;
2285 			params->flags |= DISK_ZONE_OPT_SEQ_SET;
2286 		}
2287 
2288 		if (softc->zone_flags & ADA_ZONE_FLAG_OPT_NONSEQ_SET) {
2289 			params->optimal_nonseq_zones =
2290 			    softc->optimal_nonseq_zones;
2291 			params->flags |= DISK_ZONE_OPT_NONSEQ_SET;
2292 		}
2293 
2294 		if (softc->zone_flags & ADA_ZONE_FLAG_MAX_SEQ_SET) {
2295 			params->max_seq_zones = softc->max_seq_zones;
2296 			params->flags |= DISK_ZONE_MAX_SEQ_SET;
2297 		}
2298 		if (softc->zone_flags & ADA_ZONE_FLAG_RZ_SUP)
2299 			params->flags |= DISK_ZONE_RZ_SUP;
2300 
2301 		if (softc->zone_flags & ADA_ZONE_FLAG_OPEN_SUP)
2302 			params->flags |= DISK_ZONE_OPEN_SUP;
2303 
2304 		if (softc->zone_flags & ADA_ZONE_FLAG_CLOSE_SUP)
2305 			params->flags |= DISK_ZONE_CLOSE_SUP;
2306 
2307 		if (softc->zone_flags & ADA_ZONE_FLAG_FINISH_SUP)
2308 			params->flags |= DISK_ZONE_FINISH_SUP;
2309 
2310 		if (softc->zone_flags & ADA_ZONE_FLAG_RWP_SUP)
2311 			params->flags |= DISK_ZONE_RWP_SUP;
2312 		break;
2313 	}
2314 	default:
2315 		break;
2316 	}
2317 bailout:
2318 	return (error);
2319 }
2320 
2321 static void
2322 adastart(struct cam_periph *periph, union ccb *start_ccb)
2323 {
2324 	struct ada_softc *softc = (struct ada_softc *)periph->softc;
2325 	struct ccb_ataio *ataio = &start_ccb->ataio;
2326 
2327 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adastart\n"));
2328 
2329 	switch (softc->state) {
2330 	case ADA_STATE_NORMAL:
2331 	{
2332 		struct bio *bp;
2333 		u_int8_t tag_code;
2334 
2335 		bp = cam_iosched_next_bio(softc->cam_iosched);
2336 		if (bp == NULL) {
2337 			xpt_release_ccb(start_ccb);
2338 			break;
2339 		}
2340 
2341 		if ((bp->bio_flags & BIO_ORDERED) != 0 ||
2342 		    (bp->bio_cmd != BIO_DELETE && (softc->flags & ADA_FLAG_NEED_OTAG) != 0)) {
2343 			softc->flags &= ~ADA_FLAG_NEED_OTAG;
2344 			softc->flags |= ADA_FLAG_WAS_OTAG;
2345 			tag_code = 0;
2346 		} else {
2347 			tag_code = 1;
2348 		}
2349 		switch (bp->bio_cmd) {
2350 		case BIO_WRITE:
2351 		case BIO_READ:
2352 		{
2353 			uint64_t lba = bp->bio_pblkno;
2354 			uint16_t count = bp->bio_bcount / softc->params.secsize;
2355 			void *data_ptr;
2356 			int rw_op;
2357 
2358 			if (bp->bio_cmd == BIO_WRITE) {
2359 				softc->flags |= ADA_FLAG_DIRTY;
2360 				rw_op = CAM_DIR_OUT;
2361 			} else {
2362 				rw_op = CAM_DIR_IN;
2363 			}
2364 
2365 			data_ptr = bp->bio_data;
2366 			if ((bp->bio_flags & (BIO_UNMAPPED|BIO_VLIST)) != 0) {
2367 				rw_op |= CAM_DATA_BIO;
2368 				data_ptr = bp;
2369 			}
2370 
2371 #ifdef CAM_TEST_FAILURE
2372 			int fail = 0;
2373 
2374 			/*
2375 			 * Support the failure ioctls.  If the command is a
2376 			 * read, and there are pending forced read errors, or
2377 			 * if a write and pending write errors, then fail this
2378 			 * operation with EIO.  This is useful for testing
2379 			 * purposes.  Also, support having every Nth read fail.
2380 			 *
2381 			 * This is a rather blunt tool.
2382 			 */
2383 			if (bp->bio_cmd == BIO_READ) {
2384 				if (softc->force_read_error) {
2385 					softc->force_read_error--;
2386 					fail = 1;
2387 				}
2388 				if (softc->periodic_read_error > 0) {
2389 					if (++softc->periodic_read_count >=
2390 					    softc->periodic_read_error) {
2391 						softc->periodic_read_count = 0;
2392 						fail = 1;
2393 					}
2394 				}
2395 			} else {
2396 				if (softc->force_write_error) {
2397 					softc->force_write_error--;
2398 					fail = 1;
2399 				}
2400 			}
2401 			if (fail) {
2402 				biofinish(bp, NULL, EIO);
2403 				xpt_release_ccb(start_ccb);
2404 				adaschedule(periph);
2405 				return;
2406 			}
2407 #endif
2408 			KASSERT((bp->bio_flags & BIO_UNMAPPED) == 0 ||
2409 			    round_page(bp->bio_bcount + bp->bio_ma_offset) /
2410 			    PAGE_SIZE == bp->bio_ma_n,
2411 			    ("Short bio %p", bp));
2412 			cam_fill_ataio(ataio,
2413 			    ada_retry_count,
2414 			    adadone,
2415 			    rw_op,
2416 			    0,
2417 			    data_ptr,
2418 			    bp->bio_bcount,
2419 			    ada_default_timeout*1000);
2420 
2421 			if ((softc->flags & ADA_FLAG_CAN_NCQ) && tag_code) {
2422 				if (bp->bio_cmd == BIO_READ) {
2423 					ata_ncq_cmd(ataio, ATA_READ_FPDMA_QUEUED,
2424 					    lba, count);
2425 				} else {
2426 					ata_ncq_cmd(ataio, ATA_WRITE_FPDMA_QUEUED,
2427 					    lba, count);
2428 				}
2429 			} else if ((softc->flags & ADA_FLAG_CAN_48BIT) &&
2430 			    (lba + count >= ATA_MAX_28BIT_LBA ||
2431 			    count > 256)) {
2432 				if (softc->flags & ADA_FLAG_CAN_DMA48) {
2433 					if (bp->bio_cmd == BIO_READ) {
2434 						ata_48bit_cmd(ataio, ATA_READ_DMA48,
2435 						    0, lba, count);
2436 					} else {
2437 						ata_48bit_cmd(ataio, ATA_WRITE_DMA48,
2438 						    0, lba, count);
2439 					}
2440 				} else {
2441 					if (bp->bio_cmd == BIO_READ) {
2442 						ata_48bit_cmd(ataio, ATA_READ_MUL48,
2443 						    0, lba, count);
2444 					} else {
2445 						ata_48bit_cmd(ataio, ATA_WRITE_MUL48,
2446 						    0, lba, count);
2447 					}
2448 				}
2449 			} else {
2450 				if (count == 256)
2451 					count = 0;
2452 				if (softc->flags & ADA_FLAG_CAN_DMA) {
2453 					if (bp->bio_cmd == BIO_READ) {
2454 						ata_28bit_cmd(ataio, ATA_READ_DMA,
2455 						    0, lba, count);
2456 					} else {
2457 						ata_28bit_cmd(ataio, ATA_WRITE_DMA,
2458 						    0, lba, count);
2459 					}
2460 				} else {
2461 					if (bp->bio_cmd == BIO_READ) {
2462 						ata_28bit_cmd(ataio, ATA_READ_MUL,
2463 						    0, lba, count);
2464 					} else {
2465 						ata_28bit_cmd(ataio, ATA_WRITE_MUL,
2466 						    0, lba, count);
2467 					}
2468 				}
2469 			}
2470 			break;
2471 		}
2472 		case BIO_DELETE:
2473 			switch (softc->delete_method) {
2474 			case ADA_DELETE_NCQ_DSM_TRIM:
2475 				ada_ncq_dsmtrim(softc, bp, ataio);
2476 				break;
2477 			case ADA_DELETE_DSM_TRIM:
2478 				ada_dsmtrim(softc, bp, ataio);
2479 				break;
2480 			case ADA_DELETE_CFA_ERASE:
2481 				ada_cfaerase(softc, bp, ataio);
2482 				break;
2483 			default:
2484 				biofinish(bp, NULL, EOPNOTSUPP);
2485 				xpt_release_ccb(start_ccb);
2486 				adaschedule(periph);
2487 				return;
2488 			}
2489 			start_ccb->ccb_h.ccb_state = ADA_CCB_TRIM;
2490 			start_ccb->ccb_h.flags |= CAM_UNLOCKED;
2491 			cam_iosched_submit_trim(softc->cam_iosched);
2492 			goto out;
2493 		case BIO_FLUSH:
2494 			cam_fill_ataio(ataio,
2495 			    1,
2496 			    adadone,
2497 			    CAM_DIR_NONE,
2498 			    0,
2499 			    NULL,
2500 			    0,
2501 			    ada_default_timeout*1000);
2502 
2503 			if (softc->flags & ADA_FLAG_CAN_48BIT)
2504 				ata_48bit_cmd(ataio, ATA_FLUSHCACHE48, 0, 0, 0);
2505 			else
2506 				ata_28bit_cmd(ataio, ATA_FLUSHCACHE, 0, 0, 0);
2507 			break;
2508 		case BIO_ZONE: {
2509 			int error, queue_ccb;
2510 
2511 			queue_ccb = 0;
2512 
2513 			error = ada_zone_cmd(periph, start_ccb, bp, &queue_ccb);
2514 			if ((error != 0)
2515 			 || (queue_ccb == 0)) {
2516 				biofinish(bp, NULL, error);
2517 				xpt_release_ccb(start_ccb);
2518 				return;
2519 			}
2520 			break;
2521 		}
2522 		default:
2523 			biofinish(bp, NULL, EOPNOTSUPP);
2524 			xpt_release_ccb(start_ccb);
2525 			return;
2526 		}
2527 		start_ccb->ccb_h.ccb_state = ADA_CCB_BUFFER_IO;
2528 		start_ccb->ccb_h.flags |= CAM_UNLOCKED;
2529 out:
2530 		start_ccb->ccb_h.ccb_bp = bp;
2531 		softc->outstanding_cmds++;
2532 		softc->refcount++;
2533 		cam_periph_unlock(periph);
2534 		xpt_action(start_ccb);
2535 		cam_periph_lock(periph);
2536 
2537 		/* May have more work to do, so ensure we stay scheduled */
2538 		adaschedule(periph);
2539 		break;
2540 	}
2541 	case ADA_STATE_RAHEAD:
2542 	case ADA_STATE_WCACHE:
2543 	{
2544 		cam_fill_ataio(ataio,
2545 		    1,
2546 		    adadone,
2547 		    CAM_DIR_NONE,
2548 		    0,
2549 		    NULL,
2550 		    0,
2551 		    ada_default_timeout*1000);
2552 
2553 		if (softc->state == ADA_STATE_RAHEAD) {
2554 			ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_RA ?
2555 			    ATA_SF_ENAB_RCACHE : ATA_SF_DIS_RCACHE, 0, 0);
2556 			start_ccb->ccb_h.ccb_state = ADA_CCB_RAHEAD;
2557 		} else {
2558 			ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_WC ?
2559 			    ATA_SF_ENAB_WCACHE : ATA_SF_DIS_WCACHE, 0, 0);
2560 			start_ccb->ccb_h.ccb_state = ADA_CCB_WCACHE;
2561 		}
2562 		start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2563 		xpt_action(start_ccb);
2564 		break;
2565 	}
2566 	case ADA_STATE_LOGDIR:
2567 	{
2568 		struct ata_gp_log_dir *log_dir;
2569 
2570 		if ((softc->flags & ADA_FLAG_CAN_LOG) == 0) {
2571 			adaprobedone(periph, start_ccb);
2572 			break;
2573 		}
2574 
2575 		log_dir = malloc(sizeof(*log_dir), M_ATADA, M_NOWAIT|M_ZERO);
2576 		if (log_dir == NULL) {
2577 			xpt_print(periph->path, "Couldn't malloc log_dir "
2578 			    "data\n");
2579 			softc->state = ADA_STATE_NORMAL;
2580 			xpt_release_ccb(start_ccb);
2581 			break;
2582 		}
2583 
2584 		ata_read_log(ataio,
2585 		    /*retries*/1,
2586 		    /*cbfcnp*/adadone,
2587 		    /*log_address*/ ATA_LOG_DIRECTORY,
2588 		    /*page_number*/ 0,
2589 		    /*block_count*/ 1,
2590 		    /*protocol*/ softc->flags & ADA_FLAG_CAN_DMA ?
2591 				 CAM_ATAIO_DMA : 0,
2592 		    /*data_ptr*/ (uint8_t *)log_dir,
2593 		    /*dxfer_len*/sizeof(*log_dir),
2594 		    /*timeout*/ada_default_timeout*1000);
2595 
2596 		start_ccb->ccb_h.ccb_state = ADA_CCB_LOGDIR;
2597 		xpt_action(start_ccb);
2598 		break;
2599 	}
2600 	case ADA_STATE_IDDIR:
2601 	{
2602 		struct ata_identify_log_pages *id_dir;
2603 
2604 		id_dir = malloc(sizeof(*id_dir), M_ATADA, M_NOWAIT | M_ZERO);
2605 		if (id_dir == NULL) {
2606 			xpt_print(periph->path, "Couldn't malloc id_dir "
2607 			    "data\n");
2608 			adaprobedone(periph, start_ccb);
2609 			break;
2610 		}
2611 
2612 		ata_read_log(ataio,
2613 		    /*retries*/1,
2614 		    /*cbfcnp*/adadone,
2615 		    /*log_address*/ ATA_IDENTIFY_DATA_LOG,
2616 		    /*page_number*/ ATA_IDL_PAGE_LIST,
2617 		    /*block_count*/ 1,
2618 		    /*protocol*/ softc->flags & ADA_FLAG_CAN_DMA ?
2619 				 CAM_ATAIO_DMA : 0,
2620 		    /*data_ptr*/ (uint8_t *)id_dir,
2621 		    /*dxfer_len*/ sizeof(*id_dir),
2622 		    /*timeout*/ada_default_timeout*1000);
2623 
2624 		start_ccb->ccb_h.ccb_state = ADA_CCB_IDDIR;
2625 		xpt_action(start_ccb);
2626 		break;
2627 	}
2628 	case ADA_STATE_SUP_CAP:
2629 	{
2630 		struct ata_identify_log_sup_cap *sup_cap;
2631 
2632 		sup_cap = malloc(sizeof(*sup_cap), M_ATADA, M_NOWAIT|M_ZERO);
2633 		if (sup_cap == NULL) {
2634 			xpt_print(periph->path, "Couldn't malloc sup_cap "
2635 			    "data\n");
2636 			adaprobedone(periph, start_ccb);
2637 			break;
2638 		}
2639 
2640 		ata_read_log(ataio,
2641 		    /*retries*/1,
2642 		    /*cbfcnp*/adadone,
2643 		    /*log_address*/ ATA_IDENTIFY_DATA_LOG,
2644 		    /*page_number*/ ATA_IDL_SUP_CAP,
2645 		    /*block_count*/ 1,
2646 		    /*protocol*/ softc->flags & ADA_FLAG_CAN_DMA ?
2647 				 CAM_ATAIO_DMA : 0,
2648 		    /*data_ptr*/ (uint8_t *)sup_cap,
2649 		    /*dxfer_len*/ sizeof(*sup_cap),
2650 		    /*timeout*/ada_default_timeout*1000);
2651 
2652 		start_ccb->ccb_h.ccb_state = ADA_CCB_SUP_CAP;
2653 		xpt_action(start_ccb);
2654 		break;
2655 	}
2656 	case ADA_STATE_ZONE:
2657 	{
2658 		struct ata_zoned_info_log *ata_zone;
2659 
2660 		ata_zone = malloc(sizeof(*ata_zone), M_ATADA, M_NOWAIT|M_ZERO);
2661 		if (ata_zone == NULL) {
2662 			xpt_print(periph->path, "Couldn't malloc ata_zone "
2663 			    "data\n");
2664 			adaprobedone(periph, start_ccb);
2665 			break;
2666 		}
2667 
2668 		ata_read_log(ataio,
2669 		    /*retries*/1,
2670 		    /*cbfcnp*/adadone,
2671 		    /*log_address*/ ATA_IDENTIFY_DATA_LOG,
2672 		    /*page_number*/ ATA_IDL_ZDI,
2673 		    /*block_count*/ 1,
2674 		    /*protocol*/ softc->flags & ADA_FLAG_CAN_DMA ?
2675 				 CAM_ATAIO_DMA : 0,
2676 		    /*data_ptr*/ (uint8_t *)ata_zone,
2677 		    /*dxfer_len*/ sizeof(*ata_zone),
2678 		    /*timeout*/ada_default_timeout*1000);
2679 
2680 		start_ccb->ccb_h.ccb_state = ADA_CCB_ZONE;
2681 		xpt_action(start_ccb);
2682 		break;
2683 	}
2684 	}
2685 }
2686 
2687 static void
2688 adaprobedone(struct cam_periph *periph, union ccb *ccb)
2689 {
2690 	struct ada_softc *softc;
2691 
2692 	softc = (struct ada_softc *)periph->softc;
2693 
2694 	/*
2695 	 * Since our peripheral may be invalidated by an error we must release
2696 	 * our CCB before releasing the reference on the peripheral.  The
2697 	 * peripheral will only go away once the last reference is removed, and
2698 	 * we need it around for the CCB release operation.
2699 	 */
2700 	if (ccb != NULL)
2701 		xpt_release_ccb(ccb);
2702 
2703 	softc->state = ADA_STATE_NORMAL;
2704 	softc->flags |= ADA_FLAG_PROBED;
2705 	adaschedule(periph);
2706 	if ((softc->flags & ADA_FLAG_ANNOUNCED) == 0) {
2707 		softc->flags |= ADA_FLAG_ANNOUNCED;
2708 		cam_periph_unhold(periph);
2709 	} else {
2710 		cam_periph_release_locked(periph);
2711 	}
2712 }
2713 
2714 static void
2715 adazonedone(struct cam_periph *periph, union ccb *ccb)
2716 {
2717 	struct bio *bp;
2718 
2719 	bp = (struct bio *)ccb->ccb_h.ccb_bp;
2720 
2721 	switch (bp->bio_zone.zone_cmd) {
2722 	case DISK_ZONE_OPEN:
2723 	case DISK_ZONE_CLOSE:
2724 	case DISK_ZONE_FINISH:
2725 	case DISK_ZONE_RWP:
2726 		break;
2727 	case DISK_ZONE_REPORT_ZONES: {
2728 		uint32_t avail_len;
2729 		struct disk_zone_report *rep;
2730 		struct scsi_report_zones_hdr *hdr;
2731 		struct scsi_report_zones_desc *desc;
2732 		struct disk_zone_rep_entry *entry;
2733 		uint32_t hdr_len, num_avail;
2734 		uint32_t num_to_fill, i;
2735 
2736 		rep = &bp->bio_zone.zone_params.report;
2737 		avail_len = ccb->ataio.dxfer_len - ccb->ataio.resid;
2738 		/*
2739 		 * Note that bio_resid isn't normally used for zone
2740 		 * commands, but it is used by devstat_end_transaction_bio()
2741 		 * to determine how much data was transferred.  Because
2742 		 * the size of the SCSI/ATA data structures is different
2743 		 * than the size of the BIO interface structures, the
2744 		 * amount of data actually transferred from the drive will
2745 		 * be different than the amount of data transferred to
2746 		 * the user.
2747 		 */
2748 		hdr = (struct scsi_report_zones_hdr *)ccb->ataio.data_ptr;
2749 		if (avail_len < sizeof(*hdr)) {
2750 			/*
2751 			 * Is there a better error than EIO here?  We asked
2752 			 * for at least the header, and we got less than
2753 			 * that.
2754 			 */
2755 			bp->bio_error = EIO;
2756 			bp->bio_flags |= BIO_ERROR;
2757 			bp->bio_resid = bp->bio_bcount;
2758 			break;
2759 		}
2760 
2761 		hdr_len = le32dec(hdr->length);
2762 		if (hdr_len > 0)
2763 			rep->entries_available = hdr_len / sizeof(*desc);
2764 		else
2765 			rep->entries_available = 0;
2766 		/*
2767 		 * NOTE: using the same values for the BIO version of the
2768 		 * same field as the SCSI/ATA values.  This means we could
2769 		 * get some additional values that aren't defined in bio.h
2770 		 * if more values of the same field are defined later.
2771 		 */
2772 		rep->header.same = hdr->byte4 & SRZ_SAME_MASK;
2773 		rep->header.maximum_lba = le64dec(hdr->maximum_lba);
2774 		/*
2775 		 * If the drive reports no entries that match the query,
2776 		 * we're done.
2777 		 */
2778 		if (hdr_len == 0) {
2779 			rep->entries_filled = 0;
2780 			bp->bio_resid = bp->bio_bcount;
2781 			break;
2782 		}
2783 
2784 		num_avail = min((avail_len - sizeof(*hdr)) / sizeof(*desc),
2785 				hdr_len / sizeof(*desc));
2786 		/*
2787 		 * If the drive didn't return any data, then we're done.
2788 		 */
2789 		if (num_avail == 0) {
2790 			rep->entries_filled = 0;
2791 			bp->bio_resid = bp->bio_bcount;
2792 			break;
2793 		}
2794 
2795 		num_to_fill = min(num_avail, rep->entries_allocated);
2796 		/*
2797 		 * If the user didn't allocate any entries for us to fill,
2798 		 * we're done.
2799 		 */
2800 		if (num_to_fill == 0) {
2801 			rep->entries_filled = 0;
2802 			bp->bio_resid = bp->bio_bcount;
2803 			break;
2804 		}
2805 
2806 		for (i = 0, desc = &hdr->desc_list[0], entry=&rep->entries[0];
2807 		     i < num_to_fill; i++, desc++, entry++) {
2808 			/*
2809 			 * NOTE: we're mapping the values here directly
2810 			 * from the SCSI/ATA bit definitions to the bio.h
2811 			 * definitions.  There is also a warning in
2812 			 * disk_zone.h, but the impact is that if
2813 			 * additional values are added in the SCSI/ATA
2814 			 * specs these will be visible to consumers of
2815 			 * this interface.
2816 			 */
2817 			entry->zone_type = desc->zone_type & SRZ_TYPE_MASK;
2818 			entry->zone_condition =
2819 			    (desc->zone_flags & SRZ_ZONE_COND_MASK) >>
2820 			    SRZ_ZONE_COND_SHIFT;
2821 			entry->zone_flags |= desc->zone_flags &
2822 			    (SRZ_ZONE_NON_SEQ|SRZ_ZONE_RESET);
2823 			entry->zone_length = le64dec(desc->zone_length);
2824 			entry->zone_start_lba = le64dec(desc->zone_start_lba);
2825 			entry->write_pointer_lba =
2826 			    le64dec(desc->write_pointer_lba);
2827 		}
2828 		rep->entries_filled = num_to_fill;
2829 		/*
2830 		 * Note that this residual is accurate from the user's
2831 		 * standpoint, but the amount transferred isn't accurate
2832 		 * from the standpoint of what actually came back from the
2833 		 * drive.
2834 		 */
2835 		bp->bio_resid = bp->bio_bcount - (num_to_fill * sizeof(*entry));
2836 		break;
2837 	}
2838 	case DISK_ZONE_GET_PARAMS:
2839 	default:
2840 		/*
2841 		 * In theory we should not get a GET_PARAMS bio, since it
2842 		 * should be handled without queueing the command to the
2843 		 * drive.
2844 		 */
2845 		panic("%s: Invalid zone command %d", __func__,
2846 		    bp->bio_zone.zone_cmd);
2847 		break;
2848 	}
2849 
2850 	if (bp->bio_zone.zone_cmd == DISK_ZONE_REPORT_ZONES)
2851 		free(ccb->ataio.data_ptr, M_ATADA);
2852 }
2853 
2854 static void
2855 adadone(struct cam_periph *periph, union ccb *done_ccb)
2856 {
2857 	struct ada_softc *softc;
2858 	struct ccb_ataio *ataio;
2859 	struct cam_path *path;
2860 	uint32_t priority;
2861 	int state;
2862 
2863 	softc = (struct ada_softc *)periph->softc;
2864 	ataio = &done_ccb->ataio;
2865 	path = done_ccb->ccb_h.path;
2866 	priority = done_ccb->ccb_h.pinfo.priority;
2867 
2868 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("adadone\n"));
2869 
2870 	state = ataio->ccb_h.ccb_state & ADA_CCB_TYPE_MASK;
2871 	switch (state) {
2872 	case ADA_CCB_BUFFER_IO:
2873 	case ADA_CCB_TRIM:
2874 	{
2875 		struct bio *bp;
2876 		int error;
2877 
2878 		cam_periph_lock(periph);
2879 		bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
2880 		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2881 			error = adaerror(done_ccb, CAM_RETRY_SELTO, 0);
2882 			if (error == ERESTART) {
2883 				/* A retry was scheduled, so just return. */
2884 				cam_periph_unlock(periph);
2885 				return;
2886 			}
2887 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2888 				cam_release_devq(path,
2889 						 /*relsim_flags*/0,
2890 						 /*reduction*/0,
2891 						 /*timeout*/0,
2892 						 /*getcount_only*/0);
2893 			/*
2894 			 * If we get an error on an NCQ DSM TRIM, fall back
2895 			 * to a non-NCQ DSM TRIM forever. Please note that if
2896 			 * CAN_NCQ_TRIM is set, CAN_TRIM is necessarily set too.
2897 			 * However, for this one trim, we treat it as advisory
2898 			 * and return success up the stack.
2899 			 */
2900 			if (state == ADA_CCB_TRIM &&
2901 			    error != 0 &&
2902 			    (softc->flags & ADA_FLAG_CAN_NCQ_TRIM) != 0) {
2903 				softc->flags &= ~ADA_FLAG_CAN_NCQ_TRIM;
2904 				error = 0;
2905 				adasetdeletemethod(softc);
2906 			}
2907 		} else {
2908 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2909 				panic("REQ_CMP with QFRZN");
2910 
2911 			error = 0;
2912 		}
2913 		bp->bio_error = error;
2914 		if (error != 0) {
2915 			bp->bio_resid = bp->bio_bcount;
2916 			bp->bio_flags |= BIO_ERROR;
2917 		} else {
2918 			if (bp->bio_cmd == BIO_ZONE)
2919 				adazonedone(periph, done_ccb);
2920 			else if (state == ADA_CCB_TRIM)
2921 				bp->bio_resid = 0;
2922 			else
2923 				bp->bio_resid = ataio->resid;
2924 
2925 			if ((bp->bio_resid > 0)
2926 			 && (bp->bio_cmd != BIO_ZONE))
2927 				bp->bio_flags |= BIO_ERROR;
2928 		}
2929 		softc->outstanding_cmds--;
2930 		if (softc->outstanding_cmds == 0)
2931 			softc->flags |= ADA_FLAG_WAS_OTAG;
2932 
2933 		/*
2934 		 * We need to call cam_iosched before we call biodone so that we
2935 		 * don't measure any activity that happens in the completion
2936 		 * routine, which in the case of sendfile can be quite
2937 		 * extensive.  Release the periph refcount taken in adastart()
2938 		 * for each CCB.
2939 		 */
2940 		cam_iosched_bio_complete(softc->cam_iosched, bp, done_ccb);
2941 		xpt_release_ccb(done_ccb);
2942 		KASSERT(softc->refcount >= 1, ("adadone softc %p refcount %d", softc, softc->refcount));
2943 		softc->refcount--;
2944 		if (state == ADA_CCB_TRIM) {
2945 			TAILQ_HEAD(, bio) queue;
2946 			struct bio *bp1;
2947 
2948 			TAILQ_INIT(&queue);
2949 			TAILQ_CONCAT(&queue, &softc->trim_req.bps, bio_queue);
2950 			/*
2951 			 * Normally, the xpt_release_ccb() above would make sure
2952 			 * that when we have more work to do, that work would
2953 			 * get kicked off. However, we specifically keep
2954 			 * trim_running set to 0 before the call above to allow
2955 			 * other I/O to progress when many BIO_DELETE requests
2956 			 * are pushed down. We set trim_running to 0 and call
2957 			 * daschedule again so that we don't stall if there are
2958 			 * no other I/Os pending apart from BIO_DELETEs.
2959 			 */
2960 			cam_iosched_trim_done(softc->cam_iosched);
2961 			adaschedule(periph);
2962 			cam_periph_unlock(periph);
2963 			while ((bp1 = TAILQ_FIRST(&queue)) != NULL) {
2964 				TAILQ_REMOVE(&queue, bp1, bio_queue);
2965 				bp1->bio_error = error;
2966 				if (error != 0) {
2967 					bp1->bio_flags |= BIO_ERROR;
2968 					bp1->bio_resid = bp1->bio_bcount;
2969 				} else
2970 					bp1->bio_resid = 0;
2971 				biodone(bp1);
2972 			}
2973 		} else {
2974 			adaschedule(periph);
2975 			cam_periph_unlock(periph);
2976 			biodone(bp);
2977 		}
2978 		return;
2979 	}
2980 	case ADA_CCB_RAHEAD:
2981 	{
2982 		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2983 			if (adaerror(done_ccb, 0, 0) == ERESTART) {
2984 				/* Drop freeze taken due to CAM_DEV_QFREEZE */
2985 				cam_release_devq(path, 0, 0, 0, FALSE);
2986 				return;
2987 			} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
2988 				cam_release_devq(path,
2989 				    /*relsim_flags*/0,
2990 				    /*reduction*/0,
2991 				    /*timeout*/0,
2992 				    /*getcount_only*/0);
2993 			}
2994 		}
2995 
2996 		xpt_release_ccb(done_ccb);
2997 		softc->state = ADA_STATE_WCACHE;
2998 		xpt_schedule(periph, priority);
2999 		/* Drop freeze taken due to CAM_DEV_QFREEZE */
3000 		cam_release_devq(path, 0, 0, 0, FALSE);
3001 		return;
3002 	}
3003 	case ADA_CCB_WCACHE:
3004 	{
3005 		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3006 			if (adaerror(done_ccb, 0, 0) == ERESTART) {
3007 				/* Drop freeze taken due to CAM_DEV_QFREEZE */
3008 				cam_release_devq(path, 0, 0, 0, FALSE);
3009 				return;
3010 			} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3011 				cam_release_devq(path,
3012 				    /*relsim_flags*/0,
3013 				    /*reduction*/0,
3014 				    /*timeout*/0,
3015 				    /*getcount_only*/0);
3016 			}
3017 		}
3018 
3019 		/* Drop freeze taken due to CAM_DEV_QFREEZE */
3020 		cam_release_devq(path, 0, 0, 0, FALSE);
3021 
3022 		if ((softc->flags & ADA_FLAG_CAN_LOG)
3023 		 && (softc->zone_mode != ADA_ZONE_NONE)) {
3024 			xpt_release_ccb(done_ccb);
3025 			softc->state = ADA_STATE_LOGDIR;
3026 			xpt_schedule(periph, priority);
3027 		} else {
3028 			adaprobedone(periph, done_ccb);
3029 		}
3030 		return;
3031 	}
3032 	case ADA_CCB_LOGDIR:
3033 	{
3034 		int error;
3035 
3036 		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3037 			error = 0;
3038 			softc->valid_logdir_len = 0;
3039 			bzero(&softc->ata_logdir, sizeof(softc->ata_logdir));
3040 			softc->valid_logdir_len =
3041 				ataio->dxfer_len - ataio->resid;
3042 			if (softc->valid_logdir_len > 0)
3043 				bcopy(ataio->data_ptr, &softc->ata_logdir,
3044 				    min(softc->valid_logdir_len,
3045 					sizeof(softc->ata_logdir)));
3046 			/*
3047 			 * Figure out whether the Identify Device log is
3048 			 * supported.  The General Purpose log directory
3049 			 * has a header, and lists the number of pages
3050 			 * available for each GP log identified by the
3051 			 * offset into the list.
3052 			 */
3053 			if ((softc->valid_logdir_len >=
3054 			    ((ATA_IDENTIFY_DATA_LOG + 1) * sizeof(uint16_t)))
3055 			 && (le16dec(softc->ata_logdir.header) ==
3056 			     ATA_GP_LOG_DIR_VERSION)
3057 			 && (le16dec(&softc->ata_logdir.num_pages[
3058 			     (ATA_IDENTIFY_DATA_LOG *
3059 			     sizeof(uint16_t)) - sizeof(uint16_t)]) > 0)){
3060 				softc->flags |= ADA_FLAG_CAN_IDLOG;
3061 			} else {
3062 				softc->flags &= ~ADA_FLAG_CAN_IDLOG;
3063 			}
3064 		} else {
3065 			error = adaerror(done_ccb, CAM_RETRY_SELTO,
3066 					 SF_RETRY_UA|SF_NO_PRINT);
3067 			if (error == ERESTART)
3068 				return;
3069 			else if (error != 0) {
3070 				/*
3071 				 * If we can't get the ATA log directory,
3072 				 * then ATA logs are effectively not
3073 				 * supported even if the bit is set in the
3074 				 * identify data.
3075 				 */
3076 				softc->flags &= ~(ADA_FLAG_CAN_LOG |
3077 						  ADA_FLAG_CAN_IDLOG);
3078 				if ((done_ccb->ccb_h.status &
3079 				     CAM_DEV_QFRZN) != 0) {
3080 					/* Don't wedge this device's queue */
3081 					cam_release_devq(done_ccb->ccb_h.path,
3082 							 /*relsim_flags*/0,
3083 							 /*reduction*/0,
3084 							 /*timeout*/0,
3085 							 /*getcount_only*/0);
3086 				}
3087 			}
3088 		}
3089 
3090 		free(ataio->data_ptr, M_ATADA);
3091 
3092 		if ((error == 0)
3093 		 && (softc->flags & ADA_FLAG_CAN_IDLOG)) {
3094 			softc->state = ADA_STATE_IDDIR;
3095 			xpt_release_ccb(done_ccb);
3096 			xpt_schedule(periph, priority);
3097 		} else
3098 			adaprobedone(periph, done_ccb);
3099 
3100 		return;
3101 	}
3102 	case ADA_CCB_IDDIR: {
3103 		int error;
3104 
3105 		if ((ataio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3106 			off_t entries_offset, max_entries;
3107 			error = 0;
3108 
3109 			softc->valid_iddir_len = 0;
3110 			bzero(&softc->ata_iddir, sizeof(softc->ata_iddir));
3111 			softc->flags &= ~(ADA_FLAG_CAN_SUPCAP |
3112 					  ADA_FLAG_CAN_ZONE);
3113 			softc->valid_iddir_len =
3114 				ataio->dxfer_len - ataio->resid;
3115 			if (softc->valid_iddir_len > 0)
3116 				bcopy(ataio->data_ptr, &softc->ata_iddir,
3117 				    min(softc->valid_iddir_len,
3118 					sizeof(softc->ata_iddir)));
3119 
3120 			entries_offset =
3121 			    __offsetof(struct ata_identify_log_pages,entries);
3122 			max_entries = softc->valid_iddir_len - entries_offset;
3123 			if ((softc->valid_iddir_len > (entries_offset + 1))
3124 			 && (le64dec(softc->ata_iddir.header) ==
3125 			     ATA_IDLOG_REVISION)
3126 			 && (softc->ata_iddir.entry_count > 0)) {
3127 				int num_entries, i;
3128 
3129 				num_entries = softc->ata_iddir.entry_count;
3130 				num_entries = min(num_entries,
3131 				   softc->valid_iddir_len - entries_offset);
3132 				for (i = 0; i < num_entries &&
3133 				     i < max_entries; i++) {
3134 					if (softc->ata_iddir.entries[i] ==
3135 					    ATA_IDL_SUP_CAP)
3136 						softc->flags |=
3137 						    ADA_FLAG_CAN_SUPCAP;
3138 					else if (softc->ata_iddir.entries[i]==
3139 						 ATA_IDL_ZDI)
3140 						softc->flags |=
3141 						    ADA_FLAG_CAN_ZONE;
3142 
3143 					if ((softc->flags &
3144 					     ADA_FLAG_CAN_SUPCAP)
3145 					 && (softc->flags &
3146 					     ADA_FLAG_CAN_ZONE))
3147 						break;
3148 				}
3149 			}
3150 		} else {
3151 			error = adaerror(done_ccb, CAM_RETRY_SELTO,
3152 					 SF_RETRY_UA|SF_NO_PRINT);
3153 			if (error == ERESTART)
3154 				return;
3155 			else if (error != 0) {
3156 				/*
3157 				 * If we can't get the ATA Identify Data log
3158 				 * directory, then it effectively isn't
3159 				 * supported even if the ATA Log directory
3160 				 * a non-zero number of pages present for
3161 				 * this log.
3162 				 */
3163 				softc->flags &= ~ADA_FLAG_CAN_IDLOG;
3164 				if ((done_ccb->ccb_h.status &
3165 				     CAM_DEV_QFRZN) != 0) {
3166 					/* Don't wedge this device's queue */
3167 					cam_release_devq(done_ccb->ccb_h.path,
3168 							 /*relsim_flags*/0,
3169 							 /*reduction*/0,
3170 							 /*timeout*/0,
3171 							 /*getcount_only*/0);
3172 				}
3173 			}
3174 		}
3175 
3176 		free(ataio->data_ptr, M_ATADA);
3177 
3178 		if ((error == 0)
3179 		 && (softc->flags & ADA_FLAG_CAN_SUPCAP)) {
3180 			softc->state = ADA_STATE_SUP_CAP;
3181 			xpt_release_ccb(done_ccb);
3182 			xpt_schedule(periph, priority);
3183 		} else
3184 			adaprobedone(periph, done_ccb);
3185 		return;
3186 	}
3187 	case ADA_CCB_SUP_CAP: {
3188 		int error;
3189 
3190 		if ((ataio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3191 			uint32_t valid_len;
3192 			size_t needed_size;
3193 			struct ata_identify_log_sup_cap *sup_cap;
3194 			error = 0;
3195 
3196 			sup_cap = (struct ata_identify_log_sup_cap *)
3197 			    ataio->data_ptr;
3198 			valid_len = ataio->dxfer_len - ataio->resid;
3199 			needed_size =
3200 			    __offsetof(struct ata_identify_log_sup_cap,
3201 			    sup_zac_cap) + 1 + sizeof(sup_cap->sup_zac_cap);
3202 			if (valid_len >= needed_size) {
3203 				uint64_t zoned, zac_cap;
3204 
3205 				zoned = le64dec(sup_cap->zoned_cap);
3206 				if (zoned & ATA_ZONED_VALID) {
3207 					/*
3208 					 * This should have already been
3209 					 * set, because this is also in the
3210 					 * ATA identify data.
3211 					 */
3212 					if ((zoned & ATA_ZONED_MASK) ==
3213 					    ATA_SUPPORT_ZONE_HOST_AWARE)
3214 						softc->zone_mode =
3215 						    ADA_ZONE_HOST_AWARE;
3216 					else if ((zoned & ATA_ZONED_MASK) ==
3217 					    ATA_SUPPORT_ZONE_DEV_MANAGED)
3218 						softc->zone_mode =
3219 						    ADA_ZONE_DRIVE_MANAGED;
3220 				}
3221 
3222 				zac_cap = le64dec(sup_cap->sup_zac_cap);
3223 				if (zac_cap & ATA_SUP_ZAC_CAP_VALID) {
3224 					if (zac_cap & ATA_REPORT_ZONES_SUP)
3225 						softc->zone_flags |=
3226 						    ADA_ZONE_FLAG_RZ_SUP;
3227 					if (zac_cap & ATA_ND_OPEN_ZONE_SUP)
3228 						softc->zone_flags |=
3229 						    ADA_ZONE_FLAG_OPEN_SUP;
3230 					if (zac_cap & ATA_ND_CLOSE_ZONE_SUP)
3231 						softc->zone_flags |=
3232 						    ADA_ZONE_FLAG_CLOSE_SUP;
3233 					if (zac_cap & ATA_ND_FINISH_ZONE_SUP)
3234 						softc->zone_flags |=
3235 						    ADA_ZONE_FLAG_FINISH_SUP;
3236 					if (zac_cap & ATA_ND_RWP_SUP)
3237 						softc->zone_flags |=
3238 						    ADA_ZONE_FLAG_RWP_SUP;
3239 				} else {
3240 					/*
3241 					 * This field was introduced in
3242 					 * ACS-4, r08 on April 28th, 2015.
3243 					 * If the drive firmware was written
3244 					 * to an earlier spec, it won't have
3245 					 * the field.  So, assume all
3246 					 * commands are supported.
3247 					 */
3248 					softc->zone_flags |=
3249 					    ADA_ZONE_FLAG_SUP_MASK;
3250 				}
3251 			}
3252 		} else {
3253 			error = adaerror(done_ccb, CAM_RETRY_SELTO,
3254 					 SF_RETRY_UA|SF_NO_PRINT);
3255 			if (error == ERESTART)
3256 				return;
3257 			else if (error != 0) {
3258 				/*
3259 				 * If we can't get the ATA Identify Data
3260 				 * Supported Capabilities page, clear the
3261 				 * flag...
3262 				 */
3263 				softc->flags &= ~ADA_FLAG_CAN_SUPCAP;
3264 				/*
3265 				 * And clear zone capabilities.
3266 				 */
3267 				softc->zone_flags &= ~ADA_ZONE_FLAG_SUP_MASK;
3268 				if ((done_ccb->ccb_h.status &
3269 				     CAM_DEV_QFRZN) != 0) {
3270 					/* Don't wedge this device's queue */
3271 					cam_release_devq(done_ccb->ccb_h.path,
3272 							 /*relsim_flags*/0,
3273 							 /*reduction*/0,
3274 							 /*timeout*/0,
3275 							 /*getcount_only*/0);
3276 				}
3277 			}
3278 		}
3279 
3280 		free(ataio->data_ptr, M_ATADA);
3281 
3282 		if ((error == 0)
3283 		 && (softc->flags & ADA_FLAG_CAN_ZONE)) {
3284 			softc->state = ADA_STATE_ZONE;
3285 			xpt_release_ccb(done_ccb);
3286 			xpt_schedule(periph, priority);
3287 		} else
3288 			adaprobedone(periph, done_ccb);
3289 		return;
3290 	}
3291 	case ADA_CCB_ZONE: {
3292 		int error;
3293 
3294 		if ((ataio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3295 			struct ata_zoned_info_log *zi_log;
3296 			uint32_t valid_len;
3297 			size_t needed_size;
3298 
3299 			zi_log = (struct ata_zoned_info_log *)ataio->data_ptr;
3300 
3301 			valid_len = ataio->dxfer_len - ataio->resid;
3302 			needed_size = __offsetof(struct ata_zoned_info_log,
3303 			    version_info) + 1 + sizeof(zi_log->version_info);
3304 			if (valid_len >= needed_size) {
3305 				uint64_t tmpvar;
3306 
3307 				tmpvar = le64dec(zi_log->zoned_cap);
3308 				if (tmpvar & ATA_ZDI_CAP_VALID) {
3309 					if (tmpvar & ATA_ZDI_CAP_URSWRZ)
3310 						softc->zone_flags |=
3311 						    ADA_ZONE_FLAG_URSWRZ;
3312 					else
3313 						softc->zone_flags &=
3314 						    ~ADA_ZONE_FLAG_URSWRZ;
3315 				}
3316 				tmpvar = le64dec(zi_log->optimal_seq_zones);
3317 				if (tmpvar & ATA_ZDI_OPT_SEQ_VALID) {
3318 					softc->zone_flags |=
3319 					    ADA_ZONE_FLAG_OPT_SEQ_SET;
3320 					softc->optimal_seq_zones = (tmpvar &
3321 					    ATA_ZDI_OPT_SEQ_MASK);
3322 				} else {
3323 					softc->zone_flags &=
3324 					    ~ADA_ZONE_FLAG_OPT_SEQ_SET;
3325 					softc->optimal_seq_zones = 0;
3326 				}
3327 
3328 				tmpvar =le64dec(zi_log->optimal_nonseq_zones);
3329 				if (tmpvar & ATA_ZDI_OPT_NS_VALID) {
3330 					softc->zone_flags |=
3331 					    ADA_ZONE_FLAG_OPT_NONSEQ_SET;
3332 					softc->optimal_nonseq_zones =
3333 					    (tmpvar & ATA_ZDI_OPT_NS_MASK);
3334 				} else {
3335 					softc->zone_flags &=
3336 					    ~ADA_ZONE_FLAG_OPT_NONSEQ_SET;
3337 					softc->optimal_nonseq_zones = 0;
3338 				}
3339 
3340 				tmpvar = le64dec(zi_log->max_seq_req_zones);
3341 				if (tmpvar & ATA_ZDI_MAX_SEQ_VALID) {
3342 					softc->zone_flags |=
3343 					    ADA_ZONE_FLAG_MAX_SEQ_SET;
3344 					softc->max_seq_zones =
3345 					    (tmpvar & ATA_ZDI_MAX_SEQ_MASK);
3346 				} else {
3347 					softc->zone_flags &=
3348 					    ~ADA_ZONE_FLAG_MAX_SEQ_SET;
3349 					softc->max_seq_zones = 0;
3350 				}
3351 			}
3352 		} else {
3353 			error = adaerror(done_ccb, CAM_RETRY_SELTO,
3354 					 SF_RETRY_UA|SF_NO_PRINT);
3355 			if (error == ERESTART)
3356 				return;
3357 			else if (error != 0) {
3358 				softc->flags &= ~ADA_FLAG_CAN_ZONE;
3359 				softc->flags &= ~ADA_ZONE_FLAG_SET_MASK;
3360 
3361 				if ((done_ccb->ccb_h.status &
3362 				     CAM_DEV_QFRZN) != 0) {
3363 					/* Don't wedge this device's queue */
3364 					cam_release_devq(done_ccb->ccb_h.path,
3365 							 /*relsim_flags*/0,
3366 							 /*reduction*/0,
3367 							 /*timeout*/0,
3368 							 /*getcount_only*/0);
3369 				}
3370 			}
3371 		}
3372 		free(ataio->data_ptr, M_ATADA);
3373 
3374 		adaprobedone(periph, done_ccb);
3375 		return;
3376 	}
3377 	case ADA_CCB_DUMP:
3378 		/* No-op.  We're polling */
3379 		return;
3380 	default:
3381 		break;
3382 	}
3383 	xpt_release_ccb(done_ccb);
3384 }
3385 
3386 static int
3387 adaerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
3388 {
3389 #ifdef CAM_IO_STATS
3390 	struct ada_softc *softc;
3391 	struct cam_periph *periph;
3392 
3393 	periph = xpt_path_periph(ccb->ccb_h.path);
3394 	softc = (struct ada_softc *)periph->softc;
3395 
3396 	switch (ccb->ccb_h.status & CAM_STATUS_MASK) {
3397 	case CAM_CMD_TIMEOUT:
3398 		softc->timeouts++;
3399 		break;
3400 	case CAM_REQ_ABORTED:
3401 	case CAM_REQ_CMP_ERR:
3402 	case CAM_REQ_TERMIO:
3403 	case CAM_UNREC_HBA_ERROR:
3404 	case CAM_DATA_RUN_ERR:
3405 	case CAM_ATA_STATUS_ERROR:
3406 		softc->errors++;
3407 		break;
3408 	default:
3409 		break;
3410 	}
3411 #endif
3412 
3413 	return(cam_periph_error(ccb, cam_flags, sense_flags));
3414 }
3415 
3416 static void
3417 adasetgeom(struct ada_softc *softc, struct ccb_getdev *cgd)
3418 {
3419 	struct disk_params *dp = &softc->params;
3420 	u_int64_t lbasize48;
3421 	u_int32_t lbasize;
3422 	u_int maxio, d_flags;
3423 	size_t tmpsize;
3424 
3425 	dp->secsize = ata_logical_sector_size(&cgd->ident_data);
3426 	if ((cgd->ident_data.atavalid & ATA_FLAG_54_58) &&
3427 	    cgd->ident_data.current_heads != 0 &&
3428 	    cgd->ident_data.current_sectors != 0) {
3429 		dp->heads = cgd->ident_data.current_heads;
3430 		dp->secs_per_track = cgd->ident_data.current_sectors;
3431 		dp->cylinders = cgd->ident_data.cylinders;
3432 		dp->sectors = (u_int32_t)cgd->ident_data.current_size_1 |
3433 			  ((u_int32_t)cgd->ident_data.current_size_2 << 16);
3434 	} else {
3435 		dp->heads = cgd->ident_data.heads;
3436 		dp->secs_per_track = cgd->ident_data.sectors;
3437 		dp->cylinders = cgd->ident_data.cylinders;
3438 		dp->sectors = cgd->ident_data.cylinders *
3439 			      (u_int32_t)(dp->heads * dp->secs_per_track);
3440 	}
3441 	lbasize = (u_int32_t)cgd->ident_data.lba_size_1 |
3442 		  ((u_int32_t)cgd->ident_data.lba_size_2 << 16);
3443 
3444 	/* use the 28bit LBA size if valid or bigger than the CHS mapping */
3445 	if (cgd->ident_data.cylinders == 16383 || dp->sectors < lbasize)
3446 		dp->sectors = lbasize;
3447 
3448 	/* use the 48bit LBA size if valid */
3449 	lbasize48 = ((u_int64_t)cgd->ident_data.lba_size48_1) |
3450 		    ((u_int64_t)cgd->ident_data.lba_size48_2 << 16) |
3451 		    ((u_int64_t)cgd->ident_data.lba_size48_3 << 32) |
3452 		    ((u_int64_t)cgd->ident_data.lba_size48_4 << 48);
3453 	if ((cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) &&
3454 	    lbasize48 > ATA_MAX_28BIT_LBA)
3455 		dp->sectors = lbasize48;
3456 
3457 	maxio = softc->cpi.maxio;		/* Honor max I/O size of SIM */
3458 	if (maxio == 0)
3459 		maxio = DFLTPHYS;	/* traditional default */
3460 	else if (maxio > maxphys)
3461 		maxio = maxphys;	/* for safety */
3462 	if (softc->flags & ADA_FLAG_CAN_48BIT)
3463 		maxio = min(maxio, 65536 * softc->params.secsize);
3464 	else					/* 28bit ATA command limit */
3465 		maxio = min(maxio, 256 * softc->params.secsize);
3466 	if (softc->quirks & ADA_Q_128KB)
3467 		maxio = min(maxio, 128 * 1024);
3468 	softc->disk->d_maxsize = maxio;
3469 	d_flags = DISKFLAG_DIRECT_COMPLETION | DISKFLAG_CANZONE;
3470 	if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE)
3471 		d_flags |= DISKFLAG_CANFLUSHCACHE;
3472 	if (softc->flags & ADA_FLAG_CAN_TRIM) {
3473 		d_flags |= DISKFLAG_CANDELETE;
3474 		softc->disk->d_delmaxsize = softc->params.secsize *
3475 		    ATA_DSM_RANGE_MAX * softc->trim_max_ranges;
3476 	} else if ((softc->flags & ADA_FLAG_CAN_CFA) &&
3477 	    !(softc->flags & ADA_FLAG_CAN_48BIT)) {
3478 		d_flags |= DISKFLAG_CANDELETE;
3479 		softc->disk->d_delmaxsize = 256 * softc->params.secsize;
3480 	} else
3481 		softc->disk->d_delmaxsize = maxio;
3482 	if ((softc->cpi.hba_misc & PIM_UNMAPPED) != 0) {
3483 		d_flags |= DISKFLAG_UNMAPPED_BIO;
3484 		softc->flags |= ADA_FLAG_UNMAPPEDIO;
3485 	}
3486 	softc->disk->d_flags = d_flags;
3487 
3488 	/*
3489 	 * ata_param_fixup will strip trailing padding spaces and add a NUL,
3490 	 * but if the field has no padding (as is common for serial numbers)
3491 	 * there will still be no NUL terminator. We cannot use strlcpy, since
3492 	 * it keeps reading src until it finds a NUL in order to compute the
3493 	 * return value (and will truncate the final character due to having a
3494 	 * single dsize rather than separate ssize and dsize), and strncpy does
3495 	 * not add a NUL to the destination if it reaches the character limit.
3496 	 */
3497 	tmpsize = MIN(sizeof(softc->disk->d_descr) - 1,
3498 	    sizeof(cgd->ident_data.model));
3499 	memcpy(softc->disk->d_descr, cgd->ident_data.model, tmpsize);
3500 	softc->disk->d_descr[tmpsize] = '\0';
3501 
3502 	tmpsize = MIN(sizeof(softc->disk->d_ident) - 1,
3503 	    sizeof(cgd->ident_data.serial));
3504 	memcpy(softc->disk->d_ident, cgd->ident_data.serial, tmpsize);
3505 	softc->disk->d_ident[tmpsize] = '\0';
3506 
3507 	softc->disk->d_sectorsize = softc->params.secsize;
3508 	softc->disk->d_mediasize = (off_t)softc->params.sectors *
3509 	    softc->params.secsize;
3510 	if (ata_physical_sector_size(&cgd->ident_data) !=
3511 	    softc->params.secsize) {
3512 		softc->disk->d_stripesize =
3513 		    ata_physical_sector_size(&cgd->ident_data);
3514 		softc->disk->d_stripeoffset = (softc->disk->d_stripesize -
3515 		    ata_logical_sector_offset(&cgd->ident_data)) %
3516 		    softc->disk->d_stripesize;
3517 	} else if (softc->quirks & ADA_Q_4K) {
3518 		softc->disk->d_stripesize = 4096;
3519 		softc->disk->d_stripeoffset = 0;
3520 	}
3521 	softc->disk->d_fwsectors = softc->params.secs_per_track;
3522 	softc->disk->d_fwheads = softc->params.heads;
3523 	softc->disk->d_rotation_rate = cgd->ident_data.media_rotation_rate;
3524 	snprintf(softc->disk->d_attachment, sizeof(softc->disk->d_attachment),
3525 	    "%s%d", softc->cpi.dev_name, softc->cpi.unit_number);
3526 }
3527 
3528 static void
3529 adasendorderedtag(void *arg)
3530 {
3531 	struct ada_softc *softc = arg;
3532 
3533 	if (ada_send_ordered) {
3534 		if (softc->outstanding_cmds > 0) {
3535 			if ((softc->flags & ADA_FLAG_WAS_OTAG) == 0)
3536 				softc->flags |= ADA_FLAG_NEED_OTAG;
3537 			softc->flags &= ~ADA_FLAG_WAS_OTAG;
3538 		}
3539 	}
3540 
3541 	/* Queue us up again */
3542 	callout_schedule_sbt(&softc->sendordered_c,
3543 	    SBT_1S / ADA_ORDEREDTAG_INTERVAL * ada_default_timeout, 0,
3544 	    C_PREL(1));
3545 }
3546 
3547 /*
3548  * Step through all ADA peripheral drivers, and if the device is still open,
3549  * sync the disk cache to physical media.
3550  */
3551 static void
3552 adaflush(void)
3553 {
3554 	struct cam_periph *periph;
3555 	struct ada_softc *softc;
3556 	union ccb *ccb;
3557 	int error;
3558 
3559 	CAM_PERIPH_FOREACH(periph, &adadriver) {
3560 		softc = (struct ada_softc *)periph->softc;
3561 		if (SCHEDULER_STOPPED()) {
3562 			/* If we panicked with the lock held, do not recurse. */
3563 			if (!cam_periph_owned(periph) &&
3564 			    (softc->flags & ADA_FLAG_OPEN)) {
3565 				adadump(softc->disk, NULL, 0, 0);
3566 			}
3567 			continue;
3568 		}
3569 		cam_periph_lock(periph);
3570 		/*
3571 		 * We only sync the cache if the drive is still open, and
3572 		 * if the drive is capable of it..
3573 		 */
3574 		if (((softc->flags & ADA_FLAG_OPEN) == 0) ||
3575 		    (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) == 0) {
3576 			cam_periph_unlock(periph);
3577 			continue;
3578 		}
3579 
3580 		ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3581 		cam_fill_ataio(&ccb->ataio,
3582 				    0,
3583 				    NULL,
3584 				    CAM_DIR_NONE,
3585 				    0,
3586 				    NULL,
3587 				    0,
3588 				    ada_default_timeout*1000);
3589 		if (softc->flags & ADA_FLAG_CAN_48BIT)
3590 			ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0);
3591 		else
3592 			ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0);
3593 
3594 		error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0,
3595 		    /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY,
3596 		    softc->disk->d_devstat);
3597 		if (error != 0)
3598 			xpt_print(periph->path, "Synchronize cache failed\n");
3599 		xpt_release_ccb(ccb);
3600 		cam_periph_unlock(periph);
3601 	}
3602 }
3603 
3604 static void
3605 adaspindown(uint8_t cmd, int flags)
3606 {
3607 	struct cam_periph *periph;
3608 	struct ada_softc *softc;
3609 	struct ccb_ataio local_ccb;
3610 	int error;
3611 	int mode;
3612 
3613 	CAM_PERIPH_FOREACH(periph, &adadriver) {
3614 		/* If we panicked with lock held - not recurse here. */
3615 		if (cam_periph_owned(periph))
3616 			continue;
3617 		cam_periph_lock(periph);
3618 		softc = (struct ada_softc *)periph->softc;
3619 		/*
3620 		 * We only spin-down the drive if it is capable of it..
3621 		 */
3622 		if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) {
3623 			cam_periph_unlock(periph);
3624 			continue;
3625 		}
3626 
3627 		/*
3628 		 * Additionally check if we would spin up the drive instead of
3629 		 * spinning it down.
3630 		 */
3631 		if (cmd == ATA_IDLE_IMMEDIATE) {
3632 			memset(&local_ccb, 0, sizeof(local_ccb));
3633 			xpt_setup_ccb(&local_ccb.ccb_h, periph->path,
3634 			    CAM_PRIORITY_NORMAL);
3635 			local_ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
3636 
3637 			cam_fill_ataio(&local_ccb, 0, NULL, CAM_DIR_NONE,
3638 			    0, NULL, 0, ada_default_timeout * 1000);
3639 			ata_28bit_cmd(&local_ccb, ATA_CHECK_POWER_MODE,
3640 			    0, 0, 0);
3641 			local_ccb.cmd.flags |= CAM_ATAIO_NEEDRESULT;
3642 
3643 			error = cam_periph_runccb((union ccb *)&local_ccb,
3644 			    adaerror, /*cam_flags*/0,
3645 			    /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY,
3646 			    softc->disk->d_devstat);
3647 			if (error != 0) {
3648 				xpt_print(periph->path,
3649 				    "Failed to read current power mode\n");
3650 			} else {
3651 				mode = local_ccb.res.sector_count;
3652 #ifdef DIAGNOSTIC
3653 				if (bootverbose) {
3654 					xpt_print(periph->path,
3655 					    "disk power mode 0x%02x\n", mode);
3656 				}
3657 #endif
3658 				switch (mode) {
3659 				case ATA_PM_STANDBY:
3660 				case ATA_PM_STANDBY_Y:
3661 					if (bootverbose) {
3662 						xpt_print(periph->path,
3663 						    "already spun down\n");
3664 					}
3665 					cam_periph_unlock(periph);
3666 					continue;
3667 				default:
3668 					break;
3669 				}
3670 			}
3671 		}
3672 
3673 		if (bootverbose)
3674 			xpt_print(periph->path, "spin-down\n");
3675 
3676 		memset(&local_ccb, 0, sizeof(local_ccb));
3677 		xpt_setup_ccb(&local_ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
3678 		local_ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
3679 
3680 		cam_fill_ataio(&local_ccb,
3681 				    0,
3682 				    NULL,
3683 				    CAM_DIR_NONE | flags,
3684 				    0,
3685 				    NULL,
3686 				    0,
3687 				    ada_default_timeout*1000);
3688 		ata_28bit_cmd(&local_ccb, cmd, 0, 0, 0);
3689 		error = cam_periph_runccb((union ccb *)&local_ccb, adaerror,
3690 		    /*cam_flags*/0, /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY,
3691 		    softc->disk->d_devstat);
3692 		if (error != 0)
3693 			xpt_print(periph->path, "Spin-down disk failed\n");
3694 		cam_periph_unlock(periph);
3695 	}
3696 }
3697 
3698 static void
3699 adashutdown(void *arg, int howto)
3700 {
3701 	int how;
3702 
3703 	if ((howto & RB_NOSYNC) != 0)
3704 		return;
3705 
3706 	adaflush();
3707 
3708 	/*
3709 	 * STANDBY IMMEDIATE saves any volatile data to the drive. It also spins
3710 	 * down hard drives. IDLE IMMEDIATE also saves the volatile data without
3711 	 * a spindown. We send the former when we expect to lose power soon. For
3712 	 * a warm boot, we send the latter to avoid a thundering herd of spinups
3713 	 * just after the kernel loads while probing. We have to do something to
3714 	 * flush the data because the BIOS in many systems resets the HBA
3715 	 * causing a COMINIT/COMRESET negotiation, which some drives interpret
3716 	 * as license to toss the volatile data, and others count as unclean
3717 	 * shutdown when in the Active PM state in SMART attributes.
3718 	 *
3719 	 * adaspindown will ensure that we don't send this to a drive that
3720 	 * doesn't support it.
3721 	 */
3722 	if (ada_spindown_shutdown != 0) {
3723 		how = (howto & (RB_HALT | RB_POWEROFF | RB_POWERCYCLE)) ?
3724 		    ATA_STANDBY_IMMEDIATE : ATA_IDLE_IMMEDIATE;
3725 		adaspindown(how, 0);
3726 	}
3727 }
3728 
3729 static void
3730 adasuspend(void *arg)
3731 {
3732 
3733 	adaflush();
3734 	/*
3735 	 * SLEEP also fushes any volatile data, like STANDBY IMEDIATE,
3736 	 * so we don't need to send it as well.
3737 	 */
3738 	if (ada_spindown_suspend != 0)
3739 		adaspindown(ATA_SLEEP, CAM_DEV_QFREEZE);
3740 }
3741 
3742 static void
3743 adaresume(void *arg)
3744 {
3745 	struct cam_periph *periph;
3746 	struct ada_softc *softc;
3747 
3748 	if (ada_spindown_suspend == 0)
3749 		return;
3750 
3751 	CAM_PERIPH_FOREACH(periph, &adadriver) {
3752 		cam_periph_lock(periph);
3753 		softc = (struct ada_softc *)periph->softc;
3754 		/*
3755 		 * We only spin-down the drive if it is capable of it..
3756 		 */
3757 		if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) {
3758 			cam_periph_unlock(periph);
3759 			continue;
3760 		}
3761 
3762 		if (bootverbose)
3763 			xpt_print(periph->path, "resume\n");
3764 
3765 		/*
3766 		 * Drop freeze taken due to CAM_DEV_QFREEZE flag set on
3767 		 * sleep request.
3768 		 */
3769 		cam_release_devq(periph->path,
3770 			 /*relsim_flags*/0,
3771 			 /*openings*/0,
3772 			 /*timeout*/0,
3773 			 /*getcount_only*/0);
3774 
3775 		cam_periph_unlock(periph);
3776 	}
3777 }
3778 
3779 #endif /* _KERNEL */
3780