xref: /freebsd/sys/cam/ata/ata_da.c (revision bd001d86d679e10d179ef00b9866f0e65b6fa7fd)
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, vm_offset_t physical, 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 	if (ccb != NULL)
2695 		xpt_release_ccb(ccb);
2696 
2697 	softc->state = ADA_STATE_NORMAL;
2698 	softc->flags |= ADA_FLAG_PROBED;
2699 	adaschedule(periph);
2700 	if ((softc->flags & ADA_FLAG_ANNOUNCED) == 0) {
2701 		softc->flags |= ADA_FLAG_ANNOUNCED;
2702 		cam_periph_unhold(periph);
2703 	} else {
2704 		cam_periph_release_locked(periph);
2705 	}
2706 }
2707 
2708 static void
2709 adazonedone(struct cam_periph *periph, union ccb *ccb)
2710 {
2711 	struct bio *bp;
2712 
2713 	bp = (struct bio *)ccb->ccb_h.ccb_bp;
2714 
2715 	switch (bp->bio_zone.zone_cmd) {
2716 	case DISK_ZONE_OPEN:
2717 	case DISK_ZONE_CLOSE:
2718 	case DISK_ZONE_FINISH:
2719 	case DISK_ZONE_RWP:
2720 		break;
2721 	case DISK_ZONE_REPORT_ZONES: {
2722 		uint32_t avail_len;
2723 		struct disk_zone_report *rep;
2724 		struct scsi_report_zones_hdr *hdr;
2725 		struct scsi_report_zones_desc *desc;
2726 		struct disk_zone_rep_entry *entry;
2727 		uint32_t hdr_len, num_avail;
2728 		uint32_t num_to_fill, i;
2729 
2730 		rep = &bp->bio_zone.zone_params.report;
2731 		avail_len = ccb->ataio.dxfer_len - ccb->ataio.resid;
2732 		/*
2733 		 * Note that bio_resid isn't normally used for zone
2734 		 * commands, but it is used by devstat_end_transaction_bio()
2735 		 * to determine how much data was transferred.  Because
2736 		 * the size of the SCSI/ATA data structures is different
2737 		 * than the size of the BIO interface structures, the
2738 		 * amount of data actually transferred from the drive will
2739 		 * be different than the amount of data transferred to
2740 		 * the user.
2741 		 */
2742 		hdr = (struct scsi_report_zones_hdr *)ccb->ataio.data_ptr;
2743 		if (avail_len < sizeof(*hdr)) {
2744 			/*
2745 			 * Is there a better error than EIO here?  We asked
2746 			 * for at least the header, and we got less than
2747 			 * that.
2748 			 */
2749 			bp->bio_error = EIO;
2750 			bp->bio_flags |= BIO_ERROR;
2751 			bp->bio_resid = bp->bio_bcount;
2752 			break;
2753 		}
2754 
2755 		hdr_len = le32dec(hdr->length);
2756 		if (hdr_len > 0)
2757 			rep->entries_available = hdr_len / sizeof(*desc);
2758 		else
2759 			rep->entries_available = 0;
2760 		/*
2761 		 * NOTE: using the same values for the BIO version of the
2762 		 * same field as the SCSI/ATA values.  This means we could
2763 		 * get some additional values that aren't defined in bio.h
2764 		 * if more values of the same field are defined later.
2765 		 */
2766 		rep->header.same = hdr->byte4 & SRZ_SAME_MASK;
2767 		rep->header.maximum_lba = le64dec(hdr->maximum_lba);
2768 		/*
2769 		 * If the drive reports no entries that match the query,
2770 		 * we're done.
2771 		 */
2772 		if (hdr_len == 0) {
2773 			rep->entries_filled = 0;
2774 			bp->bio_resid = bp->bio_bcount;
2775 			break;
2776 		}
2777 
2778 		num_avail = min((avail_len - sizeof(*hdr)) / sizeof(*desc),
2779 				hdr_len / sizeof(*desc));
2780 		/*
2781 		 * If the drive didn't return any data, then we're done.
2782 		 */
2783 		if (num_avail == 0) {
2784 			rep->entries_filled = 0;
2785 			bp->bio_resid = bp->bio_bcount;
2786 			break;
2787 		}
2788 
2789 		num_to_fill = min(num_avail, rep->entries_allocated);
2790 		/*
2791 		 * If the user didn't allocate any entries for us to fill,
2792 		 * we're done.
2793 		 */
2794 		if (num_to_fill == 0) {
2795 			rep->entries_filled = 0;
2796 			bp->bio_resid = bp->bio_bcount;
2797 			break;
2798 		}
2799 
2800 		for (i = 0, desc = &hdr->desc_list[0], entry=&rep->entries[0];
2801 		     i < num_to_fill; i++, desc++, entry++) {
2802 			/*
2803 			 * NOTE: we're mapping the values here directly
2804 			 * from the SCSI/ATA bit definitions to the bio.h
2805 			 * definitions.  There is also a warning in
2806 			 * disk_zone.h, but the impact is that if
2807 			 * additional values are added in the SCSI/ATA
2808 			 * specs these will be visible to consumers of
2809 			 * this interface.
2810 			 */
2811 			entry->zone_type = desc->zone_type & SRZ_TYPE_MASK;
2812 			entry->zone_condition =
2813 			    (desc->zone_flags & SRZ_ZONE_COND_MASK) >>
2814 			    SRZ_ZONE_COND_SHIFT;
2815 			entry->zone_flags |= desc->zone_flags &
2816 			    (SRZ_ZONE_NON_SEQ|SRZ_ZONE_RESET);
2817 			entry->zone_length = le64dec(desc->zone_length);
2818 			entry->zone_start_lba = le64dec(desc->zone_start_lba);
2819 			entry->write_pointer_lba =
2820 			    le64dec(desc->write_pointer_lba);
2821 		}
2822 		rep->entries_filled = num_to_fill;
2823 		/*
2824 		 * Note that this residual is accurate from the user's
2825 		 * standpoint, but the amount transferred isn't accurate
2826 		 * from the standpoint of what actually came back from the
2827 		 * drive.
2828 		 */
2829 		bp->bio_resid = bp->bio_bcount - (num_to_fill * sizeof(*entry));
2830 		break;
2831 	}
2832 	case DISK_ZONE_GET_PARAMS:
2833 	default:
2834 		/*
2835 		 * In theory we should not get a GET_PARAMS bio, since it
2836 		 * should be handled without queueing the command to the
2837 		 * drive.
2838 		 */
2839 		panic("%s: Invalid zone command %d", __func__,
2840 		    bp->bio_zone.zone_cmd);
2841 		break;
2842 	}
2843 
2844 	if (bp->bio_zone.zone_cmd == DISK_ZONE_REPORT_ZONES)
2845 		free(ccb->ataio.data_ptr, M_ATADA);
2846 }
2847 
2848 static void
2849 adadone(struct cam_periph *periph, union ccb *done_ccb)
2850 {
2851 	struct ada_softc *softc;
2852 	struct ccb_ataio *ataio;
2853 	struct cam_path *path;
2854 	uint32_t priority;
2855 	int state;
2856 
2857 	softc = (struct ada_softc *)periph->softc;
2858 	ataio = &done_ccb->ataio;
2859 	path = done_ccb->ccb_h.path;
2860 	priority = done_ccb->ccb_h.pinfo.priority;
2861 
2862 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("adadone\n"));
2863 
2864 	state = ataio->ccb_h.ccb_state & ADA_CCB_TYPE_MASK;
2865 	switch (state) {
2866 	case ADA_CCB_BUFFER_IO:
2867 	case ADA_CCB_TRIM:
2868 	{
2869 		struct bio *bp;
2870 		int error;
2871 
2872 		cam_periph_lock(periph);
2873 		bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
2874 		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2875 			error = adaerror(done_ccb, 0, 0);
2876 			if (error == ERESTART) {
2877 				/* A retry was scheduled, so just return. */
2878 				cam_periph_unlock(periph);
2879 				return;
2880 			}
2881 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2882 				cam_release_devq(path,
2883 						 /*relsim_flags*/0,
2884 						 /*reduction*/0,
2885 						 /*timeout*/0,
2886 						 /*getcount_only*/0);
2887 			/*
2888 			 * If we get an error on an NCQ DSM TRIM, fall back
2889 			 * to a non-NCQ DSM TRIM forever. Please note that if
2890 			 * CAN_NCQ_TRIM is set, CAN_TRIM is necessarily set too.
2891 			 * However, for this one trim, we treat it as advisory
2892 			 * and return success up the stack.
2893 			 */
2894 			if (state == ADA_CCB_TRIM &&
2895 			    error != 0 &&
2896 			    (softc->flags & ADA_FLAG_CAN_NCQ_TRIM) != 0) {
2897 				softc->flags &= ~ADA_FLAG_CAN_NCQ_TRIM;
2898 				error = 0;
2899 				adasetdeletemethod(softc);
2900 			}
2901 		} else {
2902 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2903 				panic("REQ_CMP with QFRZN");
2904 
2905 			error = 0;
2906 		}
2907 		bp->bio_error = error;
2908 		if (error != 0) {
2909 			bp->bio_resid = bp->bio_bcount;
2910 			bp->bio_flags |= BIO_ERROR;
2911 		} else {
2912 			if (bp->bio_cmd == BIO_ZONE)
2913 				adazonedone(periph, done_ccb);
2914 			else if (state == ADA_CCB_TRIM)
2915 				bp->bio_resid = 0;
2916 			else
2917 				bp->bio_resid = ataio->resid;
2918 
2919 			if ((bp->bio_resid > 0)
2920 			 && (bp->bio_cmd != BIO_ZONE))
2921 				bp->bio_flags |= BIO_ERROR;
2922 		}
2923 		softc->outstanding_cmds--;
2924 		if (softc->outstanding_cmds == 0)
2925 			softc->flags |= ADA_FLAG_WAS_OTAG;
2926 
2927 		/*
2928 		 * We need to call cam_iosched before we call biodone so that we
2929 		 * don't measure any activity that happens in the completion
2930 		 * routine, which in the case of sendfile can be quite
2931 		 * extensive.  Release the periph refcount taken in adastart()
2932 		 * for each CCB.
2933 		 */
2934 		cam_iosched_bio_complete(softc->cam_iosched, bp, done_ccb);
2935 		xpt_release_ccb(done_ccb);
2936 		KASSERT(softc->refcount >= 1, ("adadone softc %p refcount %d", softc, softc->refcount));
2937 		softc->refcount--;
2938 		if (state == ADA_CCB_TRIM) {
2939 			TAILQ_HEAD(, bio) queue;
2940 			struct bio *bp1;
2941 
2942 			TAILQ_INIT(&queue);
2943 			TAILQ_CONCAT(&queue, &softc->trim_req.bps, bio_queue);
2944 			/*
2945 			 * Normally, the xpt_release_ccb() above would make sure
2946 			 * that when we have more work to do, that work would
2947 			 * get kicked off. However, we specifically keep
2948 			 * trim_running set to 0 before the call above to allow
2949 			 * other I/O to progress when many BIO_DELETE requests
2950 			 * are pushed down. We set trim_running to 0 and call
2951 			 * daschedule again so that we don't stall if there are
2952 			 * no other I/Os pending apart from BIO_DELETEs.
2953 			 */
2954 			cam_iosched_trim_done(softc->cam_iosched);
2955 			adaschedule(periph);
2956 			cam_periph_unlock(periph);
2957 			while ((bp1 = TAILQ_FIRST(&queue)) != NULL) {
2958 				TAILQ_REMOVE(&queue, bp1, bio_queue);
2959 				bp1->bio_error = error;
2960 				if (error != 0) {
2961 					bp1->bio_flags |= BIO_ERROR;
2962 					bp1->bio_resid = bp1->bio_bcount;
2963 				} else
2964 					bp1->bio_resid = 0;
2965 				biodone(bp1);
2966 			}
2967 		} else {
2968 			adaschedule(periph);
2969 			cam_periph_unlock(periph);
2970 			biodone(bp);
2971 		}
2972 		return;
2973 	}
2974 	case ADA_CCB_RAHEAD:
2975 	{
2976 		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2977 			if (adaerror(done_ccb, 0, 0) == ERESTART) {
2978 				/* Drop freeze taken due to CAM_DEV_QFREEZE */
2979 				cam_release_devq(path, 0, 0, 0, FALSE);
2980 				return;
2981 			} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
2982 				cam_release_devq(path,
2983 				    /*relsim_flags*/0,
2984 				    /*reduction*/0,
2985 				    /*timeout*/0,
2986 				    /*getcount_only*/0);
2987 			}
2988 		}
2989 
2990 		/*
2991 		 * Since our peripheral may be invalidated by an error
2992 		 * above or an external event, we must release our CCB
2993 		 * before releasing the reference on the peripheral.
2994 		 * The peripheral will only go away once the last reference
2995 		 * is removed, and we need it around for the CCB release
2996 		 * operation.
2997 		 */
2998 
2999 		xpt_release_ccb(done_ccb);
3000 		softc->state = ADA_STATE_WCACHE;
3001 		xpt_schedule(periph, priority);
3002 		/* Drop freeze taken due to CAM_DEV_QFREEZE */
3003 		cam_release_devq(path, 0, 0, 0, FALSE);
3004 		return;
3005 	}
3006 	case ADA_CCB_WCACHE:
3007 	{
3008 		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3009 			if (adaerror(done_ccb, 0, 0) == ERESTART) {
3010 				/* Drop freeze taken due to CAM_DEV_QFREEZE */
3011 				cam_release_devq(path, 0, 0, 0, FALSE);
3012 				return;
3013 			} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3014 				cam_release_devq(path,
3015 				    /*relsim_flags*/0,
3016 				    /*reduction*/0,
3017 				    /*timeout*/0,
3018 				    /*getcount_only*/0);
3019 			}
3020 		}
3021 
3022 		/* Drop freeze taken due to CAM_DEV_QFREEZE */
3023 		cam_release_devq(path, 0, 0, 0, FALSE);
3024 
3025 		if ((softc->flags & ADA_FLAG_CAN_LOG)
3026 		 && (softc->zone_mode != ADA_ZONE_NONE)) {
3027 			xpt_release_ccb(done_ccb);
3028 			softc->state = ADA_STATE_LOGDIR;
3029 			xpt_schedule(periph, priority);
3030 		} else {
3031 			adaprobedone(periph, done_ccb);
3032 		}
3033 		return;
3034 	}
3035 	case ADA_CCB_LOGDIR:
3036 	{
3037 		int error;
3038 
3039 		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3040 			error = 0;
3041 			softc->valid_logdir_len = 0;
3042 			bzero(&softc->ata_logdir, sizeof(softc->ata_logdir));
3043 			softc->valid_logdir_len =
3044 				ataio->dxfer_len - ataio->resid;
3045 			if (softc->valid_logdir_len > 0)
3046 				bcopy(ataio->data_ptr, &softc->ata_logdir,
3047 				    min(softc->valid_logdir_len,
3048 					sizeof(softc->ata_logdir)));
3049 			/*
3050 			 * Figure out whether the Identify Device log is
3051 			 * supported.  The General Purpose log directory
3052 			 * has a header, and lists the number of pages
3053 			 * available for each GP log identified by the
3054 			 * offset into the list.
3055 			 */
3056 			if ((softc->valid_logdir_len >=
3057 			    ((ATA_IDENTIFY_DATA_LOG + 1) * sizeof(uint16_t)))
3058 			 && (le16dec(softc->ata_logdir.header) ==
3059 			     ATA_GP_LOG_DIR_VERSION)
3060 			 && (le16dec(&softc->ata_logdir.num_pages[
3061 			     (ATA_IDENTIFY_DATA_LOG *
3062 			     sizeof(uint16_t)) - sizeof(uint16_t)]) > 0)){
3063 				softc->flags |= ADA_FLAG_CAN_IDLOG;
3064 			} else {
3065 				softc->flags &= ~ADA_FLAG_CAN_IDLOG;
3066 			}
3067 		} else {
3068 			error = adaerror(done_ccb, CAM_RETRY_SELTO,
3069 					 SF_RETRY_UA|SF_NO_PRINT);
3070 			if (error == ERESTART)
3071 				return;
3072 			else if (error != 0) {
3073 				/*
3074 				 * If we can't get the ATA log directory,
3075 				 * then ATA logs are effectively not
3076 				 * supported even if the bit is set in the
3077 				 * identify data.
3078 				 */
3079 				softc->flags &= ~(ADA_FLAG_CAN_LOG |
3080 						  ADA_FLAG_CAN_IDLOG);
3081 				if ((done_ccb->ccb_h.status &
3082 				     CAM_DEV_QFRZN) != 0) {
3083 					/* Don't wedge this device's queue */
3084 					cam_release_devq(done_ccb->ccb_h.path,
3085 							 /*relsim_flags*/0,
3086 							 /*reduction*/0,
3087 							 /*timeout*/0,
3088 							 /*getcount_only*/0);
3089 				}
3090 			}
3091 		}
3092 
3093 		free(ataio->data_ptr, M_ATADA);
3094 
3095 		if ((error == 0)
3096 		 && (softc->flags & ADA_FLAG_CAN_IDLOG)) {
3097 			softc->state = ADA_STATE_IDDIR;
3098 			xpt_release_ccb(done_ccb);
3099 			xpt_schedule(periph, priority);
3100 		} else
3101 			adaprobedone(periph, done_ccb);
3102 
3103 		return;
3104 	}
3105 	case ADA_CCB_IDDIR: {
3106 		int error;
3107 
3108 		if ((ataio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3109 			off_t entries_offset, max_entries;
3110 			error = 0;
3111 
3112 			softc->valid_iddir_len = 0;
3113 			bzero(&softc->ata_iddir, sizeof(softc->ata_iddir));
3114 			softc->flags &= ~(ADA_FLAG_CAN_SUPCAP |
3115 					  ADA_FLAG_CAN_ZONE);
3116 			softc->valid_iddir_len =
3117 				ataio->dxfer_len - ataio->resid;
3118 			if (softc->valid_iddir_len > 0)
3119 				bcopy(ataio->data_ptr, &softc->ata_iddir,
3120 				    min(softc->valid_iddir_len,
3121 					sizeof(softc->ata_iddir)));
3122 
3123 			entries_offset =
3124 			    __offsetof(struct ata_identify_log_pages,entries);
3125 			max_entries = softc->valid_iddir_len - entries_offset;
3126 			if ((softc->valid_iddir_len > (entries_offset + 1))
3127 			 && (le64dec(softc->ata_iddir.header) ==
3128 			     ATA_IDLOG_REVISION)
3129 			 && (softc->ata_iddir.entry_count > 0)) {
3130 				int num_entries, i;
3131 
3132 				num_entries = softc->ata_iddir.entry_count;
3133 				num_entries = min(num_entries,
3134 				   softc->valid_iddir_len - entries_offset);
3135 				for (i = 0; i < num_entries &&
3136 				     i < max_entries; i++) {
3137 					if (softc->ata_iddir.entries[i] ==
3138 					    ATA_IDL_SUP_CAP)
3139 						softc->flags |=
3140 						    ADA_FLAG_CAN_SUPCAP;
3141 					else if (softc->ata_iddir.entries[i]==
3142 						 ATA_IDL_ZDI)
3143 						softc->flags |=
3144 						    ADA_FLAG_CAN_ZONE;
3145 
3146 					if ((softc->flags &
3147 					     ADA_FLAG_CAN_SUPCAP)
3148 					 && (softc->flags &
3149 					     ADA_FLAG_CAN_ZONE))
3150 						break;
3151 				}
3152 			}
3153 		} else {
3154 			error = adaerror(done_ccb, CAM_RETRY_SELTO,
3155 					 SF_RETRY_UA|SF_NO_PRINT);
3156 			if (error == ERESTART)
3157 				return;
3158 			else if (error != 0) {
3159 				/*
3160 				 * If we can't get the ATA Identify Data log
3161 				 * directory, then it effectively isn't
3162 				 * supported even if the ATA Log directory
3163 				 * a non-zero number of pages present for
3164 				 * this log.
3165 				 */
3166 				softc->flags &= ~ADA_FLAG_CAN_IDLOG;
3167 				if ((done_ccb->ccb_h.status &
3168 				     CAM_DEV_QFRZN) != 0) {
3169 					/* Don't wedge this device's queue */
3170 					cam_release_devq(done_ccb->ccb_h.path,
3171 							 /*relsim_flags*/0,
3172 							 /*reduction*/0,
3173 							 /*timeout*/0,
3174 							 /*getcount_only*/0);
3175 				}
3176 			}
3177 		}
3178 
3179 		free(ataio->data_ptr, M_ATADA);
3180 
3181 		if ((error == 0)
3182 		 && (softc->flags & ADA_FLAG_CAN_SUPCAP)) {
3183 			softc->state = ADA_STATE_SUP_CAP;
3184 			xpt_release_ccb(done_ccb);
3185 			xpt_schedule(periph, priority);
3186 		} else
3187 			adaprobedone(periph, done_ccb);
3188 		return;
3189 	}
3190 	case ADA_CCB_SUP_CAP: {
3191 		int error;
3192 
3193 		if ((ataio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3194 			uint32_t valid_len;
3195 			size_t needed_size;
3196 			struct ata_identify_log_sup_cap *sup_cap;
3197 			error = 0;
3198 
3199 			sup_cap = (struct ata_identify_log_sup_cap *)
3200 			    ataio->data_ptr;
3201 			valid_len = ataio->dxfer_len - ataio->resid;
3202 			needed_size =
3203 			    __offsetof(struct ata_identify_log_sup_cap,
3204 			    sup_zac_cap) + 1 + sizeof(sup_cap->sup_zac_cap);
3205 			if (valid_len >= needed_size) {
3206 				uint64_t zoned, zac_cap;
3207 
3208 				zoned = le64dec(sup_cap->zoned_cap);
3209 				if (zoned & ATA_ZONED_VALID) {
3210 					/*
3211 					 * This should have already been
3212 					 * set, because this is also in the
3213 					 * ATA identify data.
3214 					 */
3215 					if ((zoned & ATA_ZONED_MASK) ==
3216 					    ATA_SUPPORT_ZONE_HOST_AWARE)
3217 						softc->zone_mode =
3218 						    ADA_ZONE_HOST_AWARE;
3219 					else if ((zoned & ATA_ZONED_MASK) ==
3220 					    ATA_SUPPORT_ZONE_DEV_MANAGED)
3221 						softc->zone_mode =
3222 						    ADA_ZONE_DRIVE_MANAGED;
3223 				}
3224 
3225 				zac_cap = le64dec(sup_cap->sup_zac_cap);
3226 				if (zac_cap & ATA_SUP_ZAC_CAP_VALID) {
3227 					if (zac_cap & ATA_REPORT_ZONES_SUP)
3228 						softc->zone_flags |=
3229 						    ADA_ZONE_FLAG_RZ_SUP;
3230 					if (zac_cap & ATA_ND_OPEN_ZONE_SUP)
3231 						softc->zone_flags |=
3232 						    ADA_ZONE_FLAG_OPEN_SUP;
3233 					if (zac_cap & ATA_ND_CLOSE_ZONE_SUP)
3234 						softc->zone_flags |=
3235 						    ADA_ZONE_FLAG_CLOSE_SUP;
3236 					if (zac_cap & ATA_ND_FINISH_ZONE_SUP)
3237 						softc->zone_flags |=
3238 						    ADA_ZONE_FLAG_FINISH_SUP;
3239 					if (zac_cap & ATA_ND_RWP_SUP)
3240 						softc->zone_flags |=
3241 						    ADA_ZONE_FLAG_RWP_SUP;
3242 				} else {
3243 					/*
3244 					 * This field was introduced in
3245 					 * ACS-4, r08 on April 28th, 2015.
3246 					 * If the drive firmware was written
3247 					 * to an earlier spec, it won't have
3248 					 * the field.  So, assume all
3249 					 * commands are supported.
3250 					 */
3251 					softc->zone_flags |=
3252 					    ADA_ZONE_FLAG_SUP_MASK;
3253 				}
3254 			}
3255 		} else {
3256 			error = adaerror(done_ccb, CAM_RETRY_SELTO,
3257 					 SF_RETRY_UA|SF_NO_PRINT);
3258 			if (error == ERESTART)
3259 				return;
3260 			else if (error != 0) {
3261 				/*
3262 				 * If we can't get the ATA Identify Data
3263 				 * Supported Capabilities page, clear the
3264 				 * flag...
3265 				 */
3266 				softc->flags &= ~ADA_FLAG_CAN_SUPCAP;
3267 				/*
3268 				 * And clear zone capabilities.
3269 				 */
3270 				softc->zone_flags &= ~ADA_ZONE_FLAG_SUP_MASK;
3271 				if ((done_ccb->ccb_h.status &
3272 				     CAM_DEV_QFRZN) != 0) {
3273 					/* Don't wedge this device's queue */
3274 					cam_release_devq(done_ccb->ccb_h.path,
3275 							 /*relsim_flags*/0,
3276 							 /*reduction*/0,
3277 							 /*timeout*/0,
3278 							 /*getcount_only*/0);
3279 				}
3280 			}
3281 		}
3282 
3283 		free(ataio->data_ptr, M_ATADA);
3284 
3285 		if ((error == 0)
3286 		 && (softc->flags & ADA_FLAG_CAN_ZONE)) {
3287 			softc->state = ADA_STATE_ZONE;
3288 			xpt_release_ccb(done_ccb);
3289 			xpt_schedule(periph, priority);
3290 		} else
3291 			adaprobedone(periph, done_ccb);
3292 		return;
3293 	}
3294 	case ADA_CCB_ZONE: {
3295 		int error;
3296 
3297 		if ((ataio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3298 			struct ata_zoned_info_log *zi_log;
3299 			uint32_t valid_len;
3300 			size_t needed_size;
3301 
3302 			zi_log = (struct ata_zoned_info_log *)ataio->data_ptr;
3303 
3304 			valid_len = ataio->dxfer_len - ataio->resid;
3305 			needed_size = __offsetof(struct ata_zoned_info_log,
3306 			    version_info) + 1 + sizeof(zi_log->version_info);
3307 			if (valid_len >= needed_size) {
3308 				uint64_t tmpvar;
3309 
3310 				tmpvar = le64dec(zi_log->zoned_cap);
3311 				if (tmpvar & ATA_ZDI_CAP_VALID) {
3312 					if (tmpvar & ATA_ZDI_CAP_URSWRZ)
3313 						softc->zone_flags |=
3314 						    ADA_ZONE_FLAG_URSWRZ;
3315 					else
3316 						softc->zone_flags &=
3317 						    ~ADA_ZONE_FLAG_URSWRZ;
3318 				}
3319 				tmpvar = le64dec(zi_log->optimal_seq_zones);
3320 				if (tmpvar & ATA_ZDI_OPT_SEQ_VALID) {
3321 					softc->zone_flags |=
3322 					    ADA_ZONE_FLAG_OPT_SEQ_SET;
3323 					softc->optimal_seq_zones = (tmpvar &
3324 					    ATA_ZDI_OPT_SEQ_MASK);
3325 				} else {
3326 					softc->zone_flags &=
3327 					    ~ADA_ZONE_FLAG_OPT_SEQ_SET;
3328 					softc->optimal_seq_zones = 0;
3329 				}
3330 
3331 				tmpvar =le64dec(zi_log->optimal_nonseq_zones);
3332 				if (tmpvar & ATA_ZDI_OPT_NS_VALID) {
3333 					softc->zone_flags |=
3334 					    ADA_ZONE_FLAG_OPT_NONSEQ_SET;
3335 					softc->optimal_nonseq_zones =
3336 					    (tmpvar & ATA_ZDI_OPT_NS_MASK);
3337 				} else {
3338 					softc->zone_flags &=
3339 					    ~ADA_ZONE_FLAG_OPT_NONSEQ_SET;
3340 					softc->optimal_nonseq_zones = 0;
3341 				}
3342 
3343 				tmpvar = le64dec(zi_log->max_seq_req_zones);
3344 				if (tmpvar & ATA_ZDI_MAX_SEQ_VALID) {
3345 					softc->zone_flags |=
3346 					    ADA_ZONE_FLAG_MAX_SEQ_SET;
3347 					softc->max_seq_zones =
3348 					    (tmpvar & ATA_ZDI_MAX_SEQ_MASK);
3349 				} else {
3350 					softc->zone_flags &=
3351 					    ~ADA_ZONE_FLAG_MAX_SEQ_SET;
3352 					softc->max_seq_zones = 0;
3353 				}
3354 			}
3355 		} else {
3356 			error = adaerror(done_ccb, CAM_RETRY_SELTO,
3357 					 SF_RETRY_UA|SF_NO_PRINT);
3358 			if (error == ERESTART)
3359 				return;
3360 			else if (error != 0) {
3361 				softc->flags &= ~ADA_FLAG_CAN_ZONE;
3362 				softc->flags &= ~ADA_ZONE_FLAG_SET_MASK;
3363 
3364 				if ((done_ccb->ccb_h.status &
3365 				     CAM_DEV_QFRZN) != 0) {
3366 					/* Don't wedge this device's queue */
3367 					cam_release_devq(done_ccb->ccb_h.path,
3368 							 /*relsim_flags*/0,
3369 							 /*reduction*/0,
3370 							 /*timeout*/0,
3371 							 /*getcount_only*/0);
3372 				}
3373 			}
3374 		}
3375 		free(ataio->data_ptr, M_ATADA);
3376 
3377 		adaprobedone(periph, done_ccb);
3378 		return;
3379 	}
3380 	case ADA_CCB_DUMP:
3381 		/* No-op.  We're polling */
3382 		return;
3383 	default:
3384 		break;
3385 	}
3386 	xpt_release_ccb(done_ccb);
3387 }
3388 
3389 static int
3390 adaerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
3391 {
3392 #ifdef CAM_IO_STATS
3393 	struct ada_softc *softc;
3394 	struct cam_periph *periph;
3395 
3396 	periph = xpt_path_periph(ccb->ccb_h.path);
3397 	softc = (struct ada_softc *)periph->softc;
3398 
3399 	switch (ccb->ccb_h.status & CAM_STATUS_MASK) {
3400 	case CAM_CMD_TIMEOUT:
3401 		softc->timeouts++;
3402 		break;
3403 	case CAM_REQ_ABORTED:
3404 	case CAM_REQ_CMP_ERR:
3405 	case CAM_REQ_TERMIO:
3406 	case CAM_UNREC_HBA_ERROR:
3407 	case CAM_DATA_RUN_ERR:
3408 	case CAM_ATA_STATUS_ERROR:
3409 		softc->errors++;
3410 		break;
3411 	default:
3412 		break;
3413 	}
3414 #endif
3415 
3416 	return(cam_periph_error(ccb, cam_flags, sense_flags));
3417 }
3418 
3419 static void
3420 adasetgeom(struct ada_softc *softc, struct ccb_getdev *cgd)
3421 {
3422 	struct disk_params *dp = &softc->params;
3423 	u_int64_t lbasize48;
3424 	u_int32_t lbasize;
3425 	u_int maxio, d_flags;
3426 	size_t tmpsize;
3427 
3428 	dp->secsize = ata_logical_sector_size(&cgd->ident_data);
3429 	if ((cgd->ident_data.atavalid & ATA_FLAG_54_58) &&
3430 	    cgd->ident_data.current_heads != 0 &&
3431 	    cgd->ident_data.current_sectors != 0) {
3432 		dp->heads = cgd->ident_data.current_heads;
3433 		dp->secs_per_track = cgd->ident_data.current_sectors;
3434 		dp->cylinders = cgd->ident_data.cylinders;
3435 		dp->sectors = (u_int32_t)cgd->ident_data.current_size_1 |
3436 			  ((u_int32_t)cgd->ident_data.current_size_2 << 16);
3437 	} else {
3438 		dp->heads = cgd->ident_data.heads;
3439 		dp->secs_per_track = cgd->ident_data.sectors;
3440 		dp->cylinders = cgd->ident_data.cylinders;
3441 		dp->sectors = cgd->ident_data.cylinders *
3442 			      (u_int32_t)(dp->heads * dp->secs_per_track);
3443 	}
3444 	lbasize = (u_int32_t)cgd->ident_data.lba_size_1 |
3445 		  ((u_int32_t)cgd->ident_data.lba_size_2 << 16);
3446 
3447 	/* use the 28bit LBA size if valid or bigger than the CHS mapping */
3448 	if (cgd->ident_data.cylinders == 16383 || dp->sectors < lbasize)
3449 		dp->sectors = lbasize;
3450 
3451 	/* use the 48bit LBA size if valid */
3452 	lbasize48 = ((u_int64_t)cgd->ident_data.lba_size48_1) |
3453 		    ((u_int64_t)cgd->ident_data.lba_size48_2 << 16) |
3454 		    ((u_int64_t)cgd->ident_data.lba_size48_3 << 32) |
3455 		    ((u_int64_t)cgd->ident_data.lba_size48_4 << 48);
3456 	if ((cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) &&
3457 	    lbasize48 > ATA_MAX_28BIT_LBA)
3458 		dp->sectors = lbasize48;
3459 
3460 	maxio = softc->cpi.maxio;		/* Honor max I/O size of SIM */
3461 	if (maxio == 0)
3462 		maxio = DFLTPHYS;	/* traditional default */
3463 	else if (maxio > maxphys)
3464 		maxio = maxphys;	/* for safety */
3465 	if (softc->flags & ADA_FLAG_CAN_48BIT)
3466 		maxio = min(maxio, 65536 * softc->params.secsize);
3467 	else					/* 28bit ATA command limit */
3468 		maxio = min(maxio, 256 * softc->params.secsize);
3469 	if (softc->quirks & ADA_Q_128KB)
3470 		maxio = min(maxio, 128 * 1024);
3471 	softc->disk->d_maxsize = maxio;
3472 	d_flags = DISKFLAG_DIRECT_COMPLETION | DISKFLAG_CANZONE;
3473 	if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE)
3474 		d_flags |= DISKFLAG_CANFLUSHCACHE;
3475 	if (softc->flags & ADA_FLAG_CAN_TRIM) {
3476 		d_flags |= DISKFLAG_CANDELETE;
3477 		softc->disk->d_delmaxsize = softc->params.secsize *
3478 		    ATA_DSM_RANGE_MAX * softc->trim_max_ranges;
3479 	} else if ((softc->flags & ADA_FLAG_CAN_CFA) &&
3480 	    !(softc->flags & ADA_FLAG_CAN_48BIT)) {
3481 		d_flags |= DISKFLAG_CANDELETE;
3482 		softc->disk->d_delmaxsize = 256 * softc->params.secsize;
3483 	} else
3484 		softc->disk->d_delmaxsize = maxio;
3485 	if ((softc->cpi.hba_misc & PIM_UNMAPPED) != 0) {
3486 		d_flags |= DISKFLAG_UNMAPPED_BIO;
3487 		softc->flags |= ADA_FLAG_UNMAPPEDIO;
3488 	}
3489 	softc->disk->d_flags = d_flags;
3490 
3491 	/*
3492 	 * ata_param_fixup will strip trailing padding spaces and add a NUL,
3493 	 * but if the field has no padding (as is common for serial numbers)
3494 	 * there will still be no NUL terminator. We cannot use strlcpy, since
3495 	 * it keeps reading src until it finds a NUL in order to compute the
3496 	 * return value (and will truncate the final character due to having a
3497 	 * single dsize rather than separate ssize and dsize), and strncpy does
3498 	 * not add a NUL to the destination if it reaches the character limit.
3499 	 */
3500 	tmpsize = MIN(sizeof(softc->disk->d_descr) - 1,
3501 	    sizeof(cgd->ident_data.model));
3502 	memcpy(softc->disk->d_descr, cgd->ident_data.model, tmpsize);
3503 	softc->disk->d_descr[tmpsize] = '\0';
3504 
3505 	tmpsize = MIN(sizeof(softc->disk->d_ident) - 1,
3506 	    sizeof(cgd->ident_data.serial));
3507 	memcpy(softc->disk->d_ident, cgd->ident_data.serial, tmpsize);
3508 	softc->disk->d_ident[tmpsize] = '\0';
3509 
3510 	softc->disk->d_sectorsize = softc->params.secsize;
3511 	softc->disk->d_mediasize = (off_t)softc->params.sectors *
3512 	    softc->params.secsize;
3513 	if (ata_physical_sector_size(&cgd->ident_data) !=
3514 	    softc->params.secsize) {
3515 		softc->disk->d_stripesize =
3516 		    ata_physical_sector_size(&cgd->ident_data);
3517 		softc->disk->d_stripeoffset = (softc->disk->d_stripesize -
3518 		    ata_logical_sector_offset(&cgd->ident_data)) %
3519 		    softc->disk->d_stripesize;
3520 	} else if (softc->quirks & ADA_Q_4K) {
3521 		softc->disk->d_stripesize = 4096;
3522 		softc->disk->d_stripeoffset = 0;
3523 	}
3524 	softc->disk->d_fwsectors = softc->params.secs_per_track;
3525 	softc->disk->d_fwheads = softc->params.heads;
3526 	softc->disk->d_rotation_rate = cgd->ident_data.media_rotation_rate;
3527 	snprintf(softc->disk->d_attachment, sizeof(softc->disk->d_attachment),
3528 	    "%s%d", softc->cpi.dev_name, softc->cpi.unit_number);
3529 }
3530 
3531 static void
3532 adasendorderedtag(void *arg)
3533 {
3534 	struct ada_softc *softc = arg;
3535 
3536 	if (ada_send_ordered) {
3537 		if (softc->outstanding_cmds > 0) {
3538 			if ((softc->flags & ADA_FLAG_WAS_OTAG) == 0)
3539 				softc->flags |= ADA_FLAG_NEED_OTAG;
3540 			softc->flags &= ~ADA_FLAG_WAS_OTAG;
3541 		}
3542 	}
3543 
3544 	/* Queue us up again */
3545 	callout_schedule_sbt(&softc->sendordered_c,
3546 	    SBT_1S / ADA_ORDEREDTAG_INTERVAL * ada_default_timeout, 0,
3547 	    C_PREL(1));
3548 }
3549 
3550 /*
3551  * Step through all ADA peripheral drivers, and if the device is still open,
3552  * sync the disk cache to physical media.
3553  */
3554 static void
3555 adaflush(void)
3556 {
3557 	struct cam_periph *periph;
3558 	struct ada_softc *softc;
3559 	union ccb *ccb;
3560 	int error;
3561 
3562 	CAM_PERIPH_FOREACH(periph, &adadriver) {
3563 		softc = (struct ada_softc *)periph->softc;
3564 		if (SCHEDULER_STOPPED()) {
3565 			/* If we panicked with the lock held, do not recurse. */
3566 			if (!cam_periph_owned(periph) &&
3567 			    (softc->flags & ADA_FLAG_OPEN)) {
3568 				adadump(softc->disk, NULL, 0, 0, 0);
3569 			}
3570 			continue;
3571 		}
3572 		cam_periph_lock(periph);
3573 		/*
3574 		 * We only sync the cache if the drive is still open, and
3575 		 * if the drive is capable of it..
3576 		 */
3577 		if (((softc->flags & ADA_FLAG_OPEN) == 0) ||
3578 		    (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) == 0) {
3579 			cam_periph_unlock(periph);
3580 			continue;
3581 		}
3582 
3583 		ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3584 		cam_fill_ataio(&ccb->ataio,
3585 				    0,
3586 				    NULL,
3587 				    CAM_DIR_NONE,
3588 				    0,
3589 				    NULL,
3590 				    0,
3591 				    ada_default_timeout*1000);
3592 		if (softc->flags & ADA_FLAG_CAN_48BIT)
3593 			ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0);
3594 		else
3595 			ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0);
3596 
3597 		error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0,
3598 		    /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY,
3599 		    softc->disk->d_devstat);
3600 		if (error != 0)
3601 			xpt_print(periph->path, "Synchronize cache failed\n");
3602 		xpt_release_ccb(ccb);
3603 		cam_periph_unlock(periph);
3604 	}
3605 }
3606 
3607 static void
3608 adaspindown(uint8_t cmd, int flags)
3609 {
3610 	struct cam_periph *periph;
3611 	struct ada_softc *softc;
3612 	struct ccb_ataio local_ccb;
3613 	int error;
3614 	int mode;
3615 
3616 	CAM_PERIPH_FOREACH(periph, &adadriver) {
3617 		/* If we panicked with lock held - not recurse here. */
3618 		if (cam_periph_owned(periph))
3619 			continue;
3620 		cam_periph_lock(periph);
3621 		softc = (struct ada_softc *)periph->softc;
3622 		/*
3623 		 * We only spin-down the drive if it is capable of it..
3624 		 */
3625 		if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) {
3626 			cam_periph_unlock(periph);
3627 			continue;
3628 		}
3629 
3630 		/*
3631 		 * Additionally check if we would spin up the drive instead of
3632 		 * spinning it down.
3633 		 */
3634 		if (cmd == ATA_IDLE_IMMEDIATE) {
3635 			memset(&local_ccb, 0, sizeof(local_ccb));
3636 			xpt_setup_ccb(&local_ccb.ccb_h, periph->path,
3637 			    CAM_PRIORITY_NORMAL);
3638 			local_ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
3639 
3640 			cam_fill_ataio(&local_ccb, 0, NULL, CAM_DIR_NONE,
3641 			    0, NULL, 0, ada_default_timeout * 1000);
3642 			ata_28bit_cmd(&local_ccb, ATA_CHECK_POWER_MODE,
3643 			    0, 0, 0);
3644 			local_ccb.cmd.flags |= CAM_ATAIO_NEEDRESULT;
3645 
3646 			error = cam_periph_runccb((union ccb *)&local_ccb,
3647 			    adaerror, /*cam_flags*/0,
3648 			    /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY,
3649 			    softc->disk->d_devstat);
3650 			if (error != 0) {
3651 				xpt_print(periph->path,
3652 				    "Failed to read current power mode\n");
3653 			} else {
3654 				mode = local_ccb.res.sector_count;
3655 #ifdef DIAGNOSTIC
3656 				if (bootverbose) {
3657 					xpt_print(periph->path,
3658 					    "disk power mode 0x%02x\n", mode);
3659 				}
3660 #endif
3661 				switch (mode) {
3662 				case ATA_PM_STANDBY:
3663 				case ATA_PM_STANDBY_Y:
3664 					if (bootverbose) {
3665 						xpt_print(periph->path,
3666 						    "already spun down\n");
3667 					}
3668 					cam_periph_unlock(periph);
3669 					continue;
3670 				default:
3671 					break;
3672 				}
3673 			}
3674 		}
3675 
3676 		if (bootverbose)
3677 			xpt_print(periph->path, "spin-down\n");
3678 
3679 		memset(&local_ccb, 0, sizeof(local_ccb));
3680 		xpt_setup_ccb(&local_ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
3681 		local_ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
3682 
3683 		cam_fill_ataio(&local_ccb,
3684 				    0,
3685 				    NULL,
3686 				    CAM_DIR_NONE | flags,
3687 				    0,
3688 				    NULL,
3689 				    0,
3690 				    ada_default_timeout*1000);
3691 		ata_28bit_cmd(&local_ccb, cmd, 0, 0, 0);
3692 		error = cam_periph_runccb((union ccb *)&local_ccb, adaerror,
3693 		    /*cam_flags*/0, /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY,
3694 		    softc->disk->d_devstat);
3695 		if (error != 0)
3696 			xpt_print(periph->path, "Spin-down disk failed\n");
3697 		cam_periph_unlock(periph);
3698 	}
3699 }
3700 
3701 static void
3702 adashutdown(void *arg, int howto)
3703 {
3704 	int how;
3705 
3706 	if ((howto & RB_NOSYNC) != 0)
3707 		return;
3708 
3709 	adaflush();
3710 
3711 	/*
3712 	 * STANDBY IMMEDIATE saves any volatile data to the drive. It also spins
3713 	 * down hard drives. IDLE IMMEDIATE also saves the volatile data without
3714 	 * a spindown. We send the former when we expect to lose power soon. For
3715 	 * a warm boot, we send the latter to avoid a thundering herd of spinups
3716 	 * just after the kernel loads while probing. We have to do something to
3717 	 * flush the data because the BIOS in many systems resets the HBA
3718 	 * causing a COMINIT/COMRESET negotiation, which some drives interpret
3719 	 * as license to toss the volatile data, and others count as unclean
3720 	 * shutdown when in the Active PM state in SMART attributes.
3721 	 *
3722 	 * adaspindown will ensure that we don't send this to a drive that
3723 	 * doesn't support it.
3724 	 */
3725 	if (ada_spindown_shutdown != 0) {
3726 		how = (howto & (RB_HALT | RB_POWEROFF | RB_POWERCYCLE)) ?
3727 		    ATA_STANDBY_IMMEDIATE : ATA_IDLE_IMMEDIATE;
3728 		adaspindown(how, 0);
3729 	}
3730 }
3731 
3732 static void
3733 adasuspend(void *arg)
3734 {
3735 
3736 	adaflush();
3737 	/*
3738 	 * SLEEP also fushes any volatile data, like STANDBY IMEDIATE,
3739 	 * so we don't need to send it as well.
3740 	 */
3741 	if (ada_spindown_suspend != 0)
3742 		adaspindown(ATA_SLEEP, CAM_DEV_QFREEZE);
3743 }
3744 
3745 static void
3746 adaresume(void *arg)
3747 {
3748 	struct cam_periph *periph;
3749 	struct ada_softc *softc;
3750 
3751 	if (ada_spindown_suspend == 0)
3752 		return;
3753 
3754 	CAM_PERIPH_FOREACH(periph, &adadriver) {
3755 		cam_periph_lock(periph);
3756 		softc = (struct ada_softc *)periph->softc;
3757 		/*
3758 		 * We only spin-down the drive if it is capable of it..
3759 		 */
3760 		if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) {
3761 			cam_periph_unlock(periph);
3762 			continue;
3763 		}
3764 
3765 		if (bootverbose)
3766 			xpt_print(periph->path, "resume\n");
3767 
3768 		/*
3769 		 * Drop freeze taken due to CAM_DEV_QFREEZE flag set on
3770 		 * sleep request.
3771 		 */
3772 		cam_release_devq(periph->path,
3773 			 /*relsim_flags*/0,
3774 			 /*openings*/0,
3775 			 /*timeout*/0,
3776 			 /*getcount_only*/0);
3777 
3778 		cam_periph_unlock(periph);
3779 	}
3780 }
3781 
3782 #endif /* _KERNEL */
3783