1195534Sscottl/*-
2195534Sscottl * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org>
3195534Sscottl * All rights reserved.
4195534Sscottl *
5195534Sscottl * Redistribution and use in source and binary forms, with or without
6195534Sscottl * modification, are permitted provided that the following conditions
7195534Sscottl * are met:
8195534Sscottl * 1. Redistributions of source code must retain the above copyright
9195534Sscottl *    notice, this list of conditions and the following disclaimer,
10195534Sscottl *    without modification, immediately at the beginning of the file.
11195534Sscottl * 2. Redistributions in binary form must reproduce the above copyright
12195534Sscottl *    notice, this list of conditions and the following disclaimer in the
13195534Sscottl *    documentation and/or other materials provided with the distribution.
14195534Sscottl *
15195534Sscottl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16195534Sscottl * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17195534Sscottl * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18195534Sscottl * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19195534Sscottl * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20195534Sscottl * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21195534Sscottl * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22195534Sscottl * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23195534Sscottl * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24195534Sscottl * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25195534Sscottl */
26195534Sscottl
27195534Sscottl#include <sys/cdefs.h>
28195534Sscottl__FBSDID("$FreeBSD: stable/11/sys/cam/ata/ata_da.c 358490 2020-03-01 18:03:09Z scottl $");
29195534Sscottl
30220454Smav#include "opt_ada.h"
31220454Smav
32195534Sscottl#include <sys/param.h>
33195534Sscottl
34195534Sscottl#ifdef _KERNEL
35195534Sscottl#include <sys/systm.h>
36195534Sscottl#include <sys/kernel.h>
37195534Sscottl#include <sys/bio.h>
38195534Sscottl#include <sys/sysctl.h>
39195534Sscottl#include <sys/taskqueue.h>
40195534Sscottl#include <sys/lock.h>
41195534Sscottl#include <sys/mutex.h>
42195534Sscottl#include <sys/conf.h>
43195534Sscottl#include <sys/devicestat.h>
44195534Sscottl#include <sys/eventhandler.h>
45195534Sscottl#include <sys/malloc.h>
46300207Sken#include <sys/endian.h>
47195534Sscottl#include <sys/cons.h>
48251792Smav#include <sys/proc.h>
49214279Sbrucec#include <sys/reboot.h>
50300207Sken#include <sys/sbuf.h>
51195534Sscottl#include <geom/geom_disk.h>
52195534Sscottl#endif /* _KERNEL */
53195534Sscottl
54195534Sscottl#ifndef _KERNEL
55195534Sscottl#include <stdio.h>
56195534Sscottl#include <string.h>
57195534Sscottl#endif /* _KERNEL */
58195534Sscottl
59195534Sscottl#include <cam/cam.h>
60195534Sscottl#include <cam/cam_ccb.h>
61195534Sscottl#include <cam/cam_periph.h>
62195534Sscottl#include <cam/cam_xpt_periph.h>
63300207Sken#include <cam/scsi/scsi_all.h>
64300207Sken#include <cam/scsi/scsi_da.h>
65195534Sscottl#include <cam/cam_sim.h>
66298002Simp#include <cam/cam_iosched.h>
67195534Sscottl
68195534Sscottl#include <cam/ata/ata_all.h>
69195534Sscottl
70208349Smarius#include <machine/md_var.h>	/* geometry translation */
71208349Smarius
72195534Sscottl#ifdef _KERNEL
73195534Sscottl
74195534Sscottl#define ATA_MAX_28BIT_LBA               268435455UL
75195534Sscottl
76298002Simpextern int iosched_debug;
77298002Simp
78195534Sscottltypedef enum {
79224497Smav	ADA_STATE_RAHEAD,
80220412Smav	ADA_STATE_WCACHE,
81300207Sken	ADA_STATE_LOGDIR,
82300207Sken	ADA_STATE_IDDIR,
83300207Sken	ADA_STATE_SUP_CAP,
84300207Sken	ADA_STATE_ZONE,
85198708Smav	ADA_STATE_NORMAL
86195534Sscottl} ada_state;
87195534Sscottl
88195534Sscottltypedef enum {
89300207Sken	ADA_FLAG_CAN_48BIT	= 0x00000002,
90300207Sken	ADA_FLAG_CAN_FLUSHCACHE	= 0x00000004,
91300207Sken	ADA_FLAG_CAN_NCQ	= 0x00000008,
92300207Sken	ADA_FLAG_CAN_DMA	= 0x00000010,
93300207Sken	ADA_FLAG_NEED_OTAG	= 0x00000020,
94300207Sken	ADA_FLAG_WAS_OTAG	= 0x00000040,
95300207Sken	ADA_FLAG_CAN_TRIM	= 0x00000080,
96300207Sken	ADA_FLAG_OPEN		= 0x00000100,
97300207Sken	ADA_FLAG_SCTX_INIT	= 0x00000200,
98300207Sken	ADA_FLAG_CAN_CFA        = 0x00000400,
99300207Sken	ADA_FLAG_CAN_POWERMGT   = 0x00000800,
100300207Sken	ADA_FLAG_CAN_DMA48	= 0x00001000,
101300207Sken	ADA_FLAG_CAN_LOG	= 0x00002000,
102300207Sken	ADA_FLAG_CAN_IDLOG	= 0x00004000,
103300207Sken	ADA_FLAG_CAN_SUPCAP	= 0x00008000,
104300207Sken	ADA_FLAG_CAN_ZONE	= 0x00010000,
105300207Sken	ADA_FLAG_CAN_WCACHE	= 0x00020000,
106300207Sken	ADA_FLAG_CAN_RAHEAD	= 0x00040000,
107300207Sken	ADA_FLAG_PROBED		= 0x00080000,
108300207Sken	ADA_FLAG_ANNOUNCED	= 0x00100000,
109300207Sken	ADA_FLAG_DIRTY		= 0x00200000,
110300207Sken	ADA_FLAG_CAN_NCQ_TRIM	= 0x00400000,	/* CAN_TRIM also set */
111300207Sken	ADA_FLAG_PIM_ATA_EXT	= 0x00800000
112195534Sscottl} ada_flags;
113195534Sscottl
114195534Sscottltypedef enum {
115222520Smav	ADA_Q_NONE		= 0x00,
116222520Smav	ADA_Q_4K		= 0x01,
117298002Simp	ADA_Q_NCQ_TRIM_BROKEN	= 0x02,
118300640Sken	ADA_Q_LOG_BROKEN	= 0x04,
119350765Smav	ADA_Q_SMR_DM		= 0x08,
120350767Smav	ADA_Q_NO_TRIM		= 0x10,
121350767Smav	ADA_Q_128KB		= 0x20
122195534Sscottl} ada_quirks;
123195534Sscottl
124250792Ssmh#define ADA_Q_BIT_STRING	\
125250792Ssmh	"\020"			\
126298002Simp	"\0014K"		\
127300640Sken	"\002NCQ_TRIM_BROKEN"	\
128300640Sken	"\003LOG_BROKEN"	\
129350765Smav	"\004SMR_DM"		\
130350767Smav	"\005NO_TRIM"		\
131350767Smav	"\006128KB"
132250792Ssmh
133195534Sscottltypedef enum {
134224497Smav	ADA_CCB_RAHEAD		= 0x01,
135224497Smav	ADA_CCB_WCACHE		= 0x02,
136195534Sscottl	ADA_CCB_BUFFER_IO	= 0x03,
137195534Sscottl	ADA_CCB_DUMP		= 0x05,
138201139Smav	ADA_CCB_TRIM		= 0x06,
139300207Sken	ADA_CCB_LOGDIR		= 0x07,
140300207Sken	ADA_CCB_IDDIR		= 0x08,
141300207Sken	ADA_CCB_SUP_CAP		= 0x09,
142300207Sken	ADA_CCB_ZONE		= 0x0a,
143195534Sscottl	ADA_CCB_TYPE_MASK	= 0x0F,
144195534Sscottl} ada_ccb_state;
145195534Sscottl
146300207Skentypedef enum {
147300207Sken	ADA_ZONE_NONE		= 0x00,
148300207Sken	ADA_ZONE_DRIVE_MANAGED	= 0x01,
149300207Sken	ADA_ZONE_HOST_AWARE	= 0x02,
150300207Sken	ADA_ZONE_HOST_MANAGED	= 0x03
151300207Sken} ada_zone_mode;
152300207Sken
153300207Skentypedef enum {
154300207Sken	ADA_ZONE_FLAG_RZ_SUP		= 0x0001,
155300207Sken	ADA_ZONE_FLAG_OPEN_SUP		= 0x0002,
156300207Sken	ADA_ZONE_FLAG_CLOSE_SUP		= 0x0004,
157300207Sken	ADA_ZONE_FLAG_FINISH_SUP	= 0x0008,
158300207Sken	ADA_ZONE_FLAG_RWP_SUP		= 0x0010,
159300207Sken	ADA_ZONE_FLAG_SUP_MASK		= (ADA_ZONE_FLAG_RZ_SUP |
160300207Sken					   ADA_ZONE_FLAG_OPEN_SUP |
161300207Sken					   ADA_ZONE_FLAG_CLOSE_SUP |
162300207Sken					   ADA_ZONE_FLAG_FINISH_SUP |
163300207Sken					   ADA_ZONE_FLAG_RWP_SUP),
164300207Sken	ADA_ZONE_FLAG_URSWRZ		= 0x0020,
165300207Sken	ADA_ZONE_FLAG_OPT_SEQ_SET	= 0x0040,
166300207Sken	ADA_ZONE_FLAG_OPT_NONSEQ_SET	= 0x0080,
167300207Sken	ADA_ZONE_FLAG_MAX_SEQ_SET	= 0x0100,
168300207Sken	ADA_ZONE_FLAG_SET_MASK		= (ADA_ZONE_FLAG_OPT_SEQ_SET |
169300207Sken					   ADA_ZONE_FLAG_OPT_NONSEQ_SET |
170300207Sken					   ADA_ZONE_FLAG_MAX_SEQ_SET)
171300207Sken} ada_zone_flags;
172300207Sken
173300207Skenstatic struct ada_zone_desc {
174300207Sken	ada_zone_flags value;
175300207Sken	const char *desc;
176300207Sken} ada_zone_desc_table[] = {
177300207Sken	{ADA_ZONE_FLAG_RZ_SUP, "Report Zones" },
178300207Sken	{ADA_ZONE_FLAG_OPEN_SUP, "Open" },
179300207Sken	{ADA_ZONE_FLAG_CLOSE_SUP, "Close" },
180300207Sken	{ADA_ZONE_FLAG_FINISH_SUP, "Finish" },
181300207Sken	{ADA_ZONE_FLAG_RWP_SUP, "Reset Write Pointer" },
182300207Sken};
183300207Sken
184300207Sken
185195534Sscottl/* Offsets into our private area for storing information */
186195534Sscottl#define ccb_state	ppriv_field0
187195534Sscottl#define ccb_bp		ppriv_ptr1
188195534Sscottl
189298002Simptypedef enum {
190298002Simp	ADA_DELETE_NONE,
191298002Simp	ADA_DELETE_DISABLE,
192298002Simp	ADA_DELETE_CFA_ERASE,
193298002Simp	ADA_DELETE_DSM_TRIM,
194298002Simp	ADA_DELETE_NCQ_DSM_TRIM,
195298002Simp	ADA_DELETE_MIN = ADA_DELETE_CFA_ERASE,
196298002Simp	ADA_DELETE_MAX = ADA_DELETE_NCQ_DSM_TRIM,
197298002Simp} ada_delete_methods;
198298002Simp
199298002Simpstatic const char *ada_delete_method_names[] =
200298002Simp    { "NONE", "DISABLE", "CFA_ERASE", "DSM_TRIM", "NCQ_DSM_TRIM" };
201298002Simp#if 0
202298002Simpstatic const char *ada_delete_method_desc[] =
203298002Simp    { "NONE", "DISABLED", "CFA Erase", "DSM Trim", "DSM Trim via NCQ" };
204298002Simp#endif
205298002Simp
206195534Sscottlstruct disk_params {
207195534Sscottl	u_int8_t  heads;
208198897Smav	u_int8_t  secs_per_track;
209195534Sscottl	u_int32_t cylinders;
210198897Smav	u_int32_t secsize;	/* Number of bytes/logical sector */
211198897Smav	u_int64_t sectors;	/* Total number sectors */
212195534Sscottl};
213195534Sscottl
214222643Smav#define TRIM_MAX_BLOCKS	8
215249934Ssmh#define TRIM_MAX_RANGES	(TRIM_MAX_BLOCKS * ATA_DSM_BLK_RANGES)
216201139Smavstruct trim_request {
217249934Ssmh	uint8_t		data[TRIM_MAX_RANGES * ATA_DSM_RANGE_SIZE];
218256836Smav	TAILQ_HEAD(, bio) bps;
219201139Smav};
220201139Smav
221195534Sscottlstruct ada_softc {
222298002Simp	struct   cam_iosched_softc *cam_iosched;
223257054Smav	int	 outstanding_cmds;	/* Number of active commands */
224257054Smav	int	 refcount;		/* Active xpt_action() calls */
225195534Sscottl	ada_state state;
226257054Smav	ada_flags flags;
227300207Sken	ada_zone_mode zone_mode;
228300207Sken	ada_zone_flags zone_flags;
229300207Sken	struct ata_gp_log_dir ata_logdir;
230300207Sken	int valid_logdir_len;
231300207Sken	struct ata_identify_log_pages ata_iddir;
232300207Sken	int valid_iddir_len;
233300207Sken	uint64_t optimal_seq_zones;
234300207Sken	uint64_t optimal_nonseq_zones;
235300207Sken	uint64_t max_seq_zones;
236195534Sscottl	ada_quirks quirks;
237298002Simp	ada_delete_methods delete_method;
238201139Smav	int	 trim_max_ranges;
239224497Smav	int	 read_ahead;
240220454Smav	int	 write_cache;
241298002Simp	int	 unmappedio;
242298002Simp	int	 rotating;
243220454Smav#ifdef ADA_TEST_FAILURE
244220454Smav	int      force_read_error;
245220454Smav	int      force_write_error;
246220454Smav	int      periodic_read_error;
247220454Smav	int      periodic_read_count;
248220454Smav#endif
249350805Smav	struct ccb_pathinq	cpi;
250350805Smav	struct disk_params	params;
251350805Smav	struct disk		*disk;
252195534Sscottl	struct task		sysctl_task;
253195534Sscottl	struct sysctl_ctx_list	sysctl_ctx;
254195534Sscottl	struct sysctl_oid	*sysctl_tree;
255195534Sscottl	struct callout		sendordered_c;
256201139Smav	struct trim_request	trim_req;
257298002Simp#ifdef CAM_IO_STATS
258298002Simp	struct sysctl_ctx_list	sysctl_stats_ctx;
259298002Simp	struct sysctl_oid	*sysctl_stats_tree;
260298002Simp	u_int	timeouts;
261298002Simp	u_int	errors;
262298002Simp	u_int	invalidations;
263298002Simp#endif
264195534Sscottl};
265195534Sscottl
266195534Sscottlstruct ada_quirk_entry {
267195534Sscottl	struct scsi_inquiry_pattern inq_pat;
268195534Sscottl	ada_quirks quirks;
269195534Sscottl};
270195534Sscottl
271199178Smavstatic struct ada_quirk_entry ada_quirk_table[] =
272199178Smav{
273199178Smav	{
274350767Smav		/* Sandisk X400 */
275350767Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "SanDisk?SD8SB8U1T00*", "X4162000*" },
276350767Smav		/*quirks*/ADA_Q_128KB
277350767Smav	},
278350767Smav	{
279222520Smav		/* Hitachi Advanced Format (4k) drives */
280222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Hitachi H??????????E3*", "*" },
281222520Smav		/*quirks*/ADA_Q_4K
282222520Smav	},
283222520Smav	{
284222520Smav		/* Samsung Advanced Format (4k) drives */
285228819Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD155UI*", "*" },
286228819Smav		/*quirks*/ADA_Q_4K
287228819Smav	},
288228819Smav	{
289228819Smav		/* Samsung Advanced Format (4k) drives */
290222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD204UI*", "*" },
291222520Smav		/*quirks*/ADA_Q_4K
292222520Smav	},
293222520Smav	{
294222520Smav		/* Seagate Barracuda Green Advanced Format (4k) drives */
295222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST????DL*", "*" },
296222520Smav		/*quirks*/ADA_Q_4K
297222520Smav	},
298222520Smav	{
299228819Smav		/* Seagate Barracuda Advanced Format (4k) drives */
300228819Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST???DM*", "*" },
301228819Smav		/*quirks*/ADA_Q_4K
302228819Smav	},
303228819Smav	{
304228819Smav		/* Seagate Barracuda Advanced Format (4k) drives */
305228819Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST????DM*", "*" },
306228819Smav		/*quirks*/ADA_Q_4K
307228819Smav	},
308228819Smav	{
309222520Smav		/* Seagate Momentus Advanced Format (4k) drives */
310222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9500423AS*", "*" },
311222520Smav		/*quirks*/ADA_Q_4K
312222520Smav	},
313222520Smav	{
314222520Smav		/* Seagate Momentus Advanced Format (4k) drives */
315222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9500424AS*", "*" },
316222520Smav		/*quirks*/ADA_Q_4K
317222520Smav	},
318222520Smav	{
319222520Smav		/* Seagate Momentus Advanced Format (4k) drives */
320228819Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9640423AS*", "*" },
321228819Smav		/*quirks*/ADA_Q_4K
322228819Smav	},
323228819Smav	{
324228819Smav		/* Seagate Momentus Advanced Format (4k) drives */
325228819Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9640424AS*", "*" },
326228819Smav		/*quirks*/ADA_Q_4K
327228819Smav	},
328228819Smav	{
329228819Smav		/* Seagate Momentus Advanced Format (4k) drives */
330222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750420AS*", "*" },
331222520Smav		/*quirks*/ADA_Q_4K
332222520Smav	},
333222520Smav	{
334222520Smav		/* Seagate Momentus Advanced Format (4k) drives */
335222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750422AS*", "*" },
336222520Smav		/*quirks*/ADA_Q_4K
337222520Smav	},
338222520Smav	{
339228819Smav		/* Seagate Momentus Advanced Format (4k) drives */
340228819Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750423AS*", "*" },
341228819Smav		/*quirks*/ADA_Q_4K
342228819Smav	},
343228819Smav	{
344222520Smav		/* Seagate Momentus Thin Advanced Format (4k) drives */
345222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST???LT*", "*" },
346222520Smav		/*quirks*/ADA_Q_4K
347222520Smav	},
348222520Smav	{
349280845Seadler		/* WDC Caviar Red Advanced Format (4k) drives */
350280845Seadler		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????CX*", "*" },
351280845Seadler		/*quirks*/ADA_Q_4K
352280845Seadler	},
353280845Seadler	{
354222520Smav		/* WDC Caviar Green Advanced Format (4k) drives */
355222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RS*", "*" },
356222520Smav		/*quirks*/ADA_Q_4K
357222520Smav	},
358222520Smav	{
359280845Seadler		/* WDC Caviar Green/Red Advanced Format (4k) drives */
360222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RX*", "*" },
361222520Smav		/*quirks*/ADA_Q_4K
362222520Smav	},
363222520Smav	{
364280845Seadler		/* WDC Caviar Red Advanced Format (4k) drives */
365280845Seadler		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????CX*", "*" },
366280845Seadler		/*quirks*/ADA_Q_4K
367280845Seadler	},
368280845Seadler	{
369280845Seadler		/* WDC Caviar Black Advanced Format (4k) drives */
370350763Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????AZEX*", "*" },
371280845Seadler		/*quirks*/ADA_Q_4K
372280845Seadler	},
373280845Seadler	{
374350763Smav		/* WDC Caviar Black Advanced Format (4k) drives */
375350763Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????FZEX*", "*" },
376350763Smav		/*quirks*/ADA_Q_4K
377350763Smav	},
378350763Smav	{
379222520Smav		/* WDC Caviar Green Advanced Format (4k) drives */
380222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RS*", "*" },
381222520Smav		/*quirks*/ADA_Q_4K
382222520Smav	},
383222520Smav	{
384222520Smav		/* WDC Caviar Green Advanced Format (4k) drives */
385222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RX*", "*" },
386222520Smav		/*quirks*/ADA_Q_4K
387222520Smav	},
388222520Smav	{
389222520Smav		/* WDC Scorpio Black Advanced Format (4k) drives */
390222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PKT*", "*" },
391222520Smav		/*quirks*/ADA_Q_4K
392222520Smav	},
393222520Smav	{
394222520Smav		/* WDC Scorpio Black Advanced Format (4k) drives */
395222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PKT*", "*" },
396222520Smav		/*quirks*/ADA_Q_4K
397222520Smav	},
398222520Smav	{
399222520Smav		/* WDC Scorpio Blue Advanced Format (4k) drives */
400222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PVT*", "*" },
401222520Smav		/*quirks*/ADA_Q_4K
402222520Smav	},
403222520Smav	{
404222520Smav		/* WDC Scorpio Blue Advanced Format (4k) drives */
405222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PVT*", "*" },
406222520Smav		/*quirks*/ADA_Q_4K
407222520Smav	},
408251061Ssmh	/* SSDs */
409222520Smav	{
410241784Seadler		/*
411241784Seadler		 * Corsair Force 2 SSDs
412241784Seadler		 * 4k optimised & trim only works in 4k requests + 4k aligned
413241784Seadler		 */
414241784Seadler		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair CSSD-F*", "*" },
415241784Seadler		/*quirks*/ADA_Q_4K
416241784Seadler	},
417241784Seadler	{
418241784Seadler		/*
419241784Seadler		 * Corsair Force 3 SSDs
420241784Seadler		 * 4k optimised & trim only works in 4k requests + 4k aligned
421241784Seadler		 */
422241784Seadler		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Force 3*", "*" },
423241784Seadler		/*quirks*/ADA_Q_4K
424241784Seadler	},
425241784Seadler	{
426241784Seadler		/*
427256547Ssmh		 * Corsair Neutron GTX SSDs
428256547Ssmh		 * 4k optimised & trim only works in 4k requests + 4k aligned
429256547Ssmh		 */
430256547Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Neutron GTX*", "*" },
431256547Ssmh		/*quirks*/ADA_Q_4K
432256547Ssmh	},
433256547Ssmh	{
434256547Ssmh		/*
435269974Ssmh		 * Corsair Force GT & GS SSDs
436241784Seadler		 * 4k optimised & trim only works in 4k requests + 4k aligned
437241784Seadler		 */
438269974Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Force G*", "*" },
439241784Seadler		/*quirks*/ADA_Q_4K
440241784Seadler	},
441241784Seadler	{
442241784Seadler		/*
443251061Ssmh		 * Crucial M4 SSDs
444241784Seadler		 * 4k optimised & trim only works in 4k requests + 4k aligned
445241784Seadler		 */
446251061Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "M4-CT???M4SSD2*", "*" },
447241784Seadler		/*quirks*/ADA_Q_4K
448241784Seadler	},
449241784Seadler	{
450241784Seadler		/*
451298027Simp		 * Crucial M500 SSDs MU07 firmware
452298027Simp		 * NCQ Trim works
453298002Simp		 */
454298027Simp		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Crucial CT*M500*", "MU07" },
455298002Simp		/*quirks*/0
456298002Simp	},
457298002Simp	{
458298002Simp		/*
459298002Simp		 * Crucial M500 SSDs all other firmware
460298002Simp		 * NCQ Trim doesn't work
461298002Simp		 */
462298002Simp		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Crucial CT*M500*", "*" },
463298002Simp		/*quirks*/ADA_Q_NCQ_TRIM_BROKEN
464298002Simp	},
465298002Simp	{
466298002Simp		/*
467298002Simp		 * Crucial M550 SSDs
468298002Simp		 * NCQ Trim doesn't work, but only on MU01 firmware
469298002Simp		 */
470298002Simp		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Crucial CT*M550*", "MU01" },
471298002Simp		/*quirks*/ADA_Q_NCQ_TRIM_BROKEN
472298002Simp	},
473298002Simp	{
474298002Simp		/*
475298002Simp		 * Crucial MX100 SSDs
476298002Simp		 * NCQ Trim doesn't work, but only on MU01 firmware
477298002Simp		 */
478298002Simp		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Crucial CT*MX100*", "MU01" },
479298002Simp		/*quirks*/ADA_Q_NCQ_TRIM_BROKEN
480298002Simp	},
481298002Simp	{
482298002Simp		/*
483251061Ssmh		 * Crucial RealSSD C300 SSDs
484251061Ssmh		 * 4k optimised
485251061Ssmh		 */
486251061Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "C300-CTFDDAC???MAG*",
487251061Ssmh		"*" }, /*quirks*/ADA_Q_4K
488251061Ssmh	},
489251061Ssmh	{
490251061Ssmh		/*
491298027Simp		 * FCCT M500 SSDs
492298027Simp		 * NCQ Trim doesn't work
493298027Simp		 */
494298027Simp		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "FCCT*M500*", "*" },
495298027Simp		/*quirks*/ADA_Q_NCQ_TRIM_BROKEN
496298027Simp	},
497298027Simp	{
498298027Simp		/*
499251061Ssmh		 * Intel 320 Series SSDs
500241784Seadler		 * 4k optimised & trim only works in 4k requests + 4k aligned
501241784Seadler		 */
502251061Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSA2CW*", "*" },
503241784Seadler		/*quirks*/ADA_Q_4K
504241784Seadler	},
505241784Seadler	{
506241784Seadler		/*
507251061Ssmh		 * Intel 330 Series SSDs
508241784Seadler		 * 4k optimised & trim only works in 4k requests + 4k aligned
509241784Seadler		 */
510251061Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2CT*", "*" },
511241784Seadler		/*quirks*/ADA_Q_4K
512241784Seadler	},
513241784Seadler	{
514241784Seadler		/*
515251061Ssmh		 * Intel 510 Series SSDs
516251061Ssmh		 * 4k optimised & trim only works in 4k requests + 4k aligned
517241784Seadler		 */
518251061Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2MH*", "*" },
519251061Ssmh		/*quirks*/ADA_Q_4K
520241784Seadler	},
521241784Seadler	{
522241784Seadler		/*
523251061Ssmh		 * Intel 520 Series SSDs
524251061Ssmh		 * 4k optimised & trim only works in 4k requests + 4k aligned
525241784Seadler		 */
526251061Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2BW*", "*" },
527241784Seadler		/*quirks*/ADA_Q_4K
528241784Seadler	},
529241784Seadler	{
530241784Seadler		/*
531331044Seadler		 * Intel S3610 Series SSDs
532331044Seadler		 * 4k optimised & trim only works in 4k requests + 4k aligned
533331044Seadler		 */
534331044Seadler		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2BX*", "*" },
535331044Seadler		/*quirks*/ADA_Q_4K
536331044Seadler	},
537331044Seadler	{
538331044Seadler		/*
539254329Ssmh		 * Intel X25-M Series SSDs
540254329Ssmh		 * 4k optimised & trim only works in 4k requests + 4k aligned
541254329Ssmh		 */
542254329Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSA2M*", "*" },
543254329Ssmh		/*quirks*/ADA_Q_4K
544254329Ssmh	},
545254329Ssmh	{
546254329Ssmh		/*
547350765Smav		 * KingDian S200 60GB P0921B
548350765Smav		 * Trimming crash the SSD
549350765Smav		 */
550350765Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "KingDian S200 *", "*" },
551350765Smav		/*quirks*/ADA_Q_NO_TRIM
552350765Smav	},
553350765Smav	{
554350765Smav		/*
555251061Ssmh		 * Kingston E100 Series SSDs
556250532Seadler		 * 4k optimised & trim only works in 4k requests + 4k aligned
557250532Seadler		 */
558251061Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "KINGSTON SE100S3*", "*" },
559250532Seadler		/*quirks*/ADA_Q_4K
560250532Seadler	},
561250532Seadler	{
562250532Seadler		/*
563251061Ssmh		 * Kingston HyperX 3k SSDs
564241784Seadler		 * 4k optimised & trim only works in 4k requests + 4k aligned
565241784Seadler		 */
566251061Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "KINGSTON SH103S3*", "*" },
567241784Seadler		/*quirks*/ADA_Q_4K
568241784Seadler	},
569241784Seadler	{
570241784Seadler		/*
571254329Ssmh		 * Marvell SSDs (entry taken from OpenSolaris)
572254329Ssmh		 * 4k optimised & trim only works in 4k requests + 4k aligned
573254329Ssmh		 */
574254329Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "MARVELL SD88SA02*", "*" },
575254329Ssmh		/*quirks*/ADA_Q_4K
576254329Ssmh	},
577254329Ssmh	{
578254329Ssmh		/*
579298027Simp		 * Micron M500 SSDs firmware MU07
580298002Simp		 * NCQ Trim works?
581298002Simp		 */
582298027Simp		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Micron M500*", "MU07" },
583298002Simp		/*quirks*/0
584298002Simp	},
585298002Simp	{
586298002Simp		/*
587298002Simp		 * Micron M500 SSDs all other firmware
588298002Simp		 * NCQ Trim doesn't work
589298002Simp		 */
590298002Simp		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Micron M500*", "*" },
591298002Simp		/*quirks*/ADA_Q_NCQ_TRIM_BROKEN
592298002Simp	},
593298002Simp	{
594298002Simp		/*
595298002Simp		 * Micron M5[15]0 SSDs
596298002Simp		 * NCQ Trim doesn't work, but only MU01 firmware
597298002Simp		 */
598298002Simp		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Micron M5[15]0*", "MU01" },
599298002Simp		/*quirks*/ADA_Q_NCQ_TRIM_BROKEN
600298002Simp	},
601298002Simp	{
602298002Simp		/*
603331044Seadler		 * Micron 5100 SSDs
604331044Seadler		 * 4k optimised & trim only works in 4k requests + 4k aligned
605331044Seadler		 */
606331044Seadler		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Micron 5100 MTFDDAK*", "*" },
607331044Seadler		/*quirks*/ADA_Q_4K
608331044Seadler	},
609331044Seadler	{
610331044Seadler		/*
611254329Ssmh		 * OCZ Agility 2 SSDs
612254329Ssmh		 * 4k optimised & trim only works in 4k requests + 4k aligned
613254329Ssmh		 */
614254329Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-AGILITY2*", "*" },
615254329Ssmh		/*quirks*/ADA_Q_4K
616254329Ssmh	},
617254329Ssmh	{
618254329Ssmh		/*
619251061Ssmh		 * OCZ Agility 3 SSDs
620250532Seadler		 * 4k optimised & trim only works in 4k requests + 4k aligned
621250532Seadler		 */
622251061Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-AGILITY3*", "*" },
623250532Seadler		/*quirks*/ADA_Q_4K
624250532Seadler	},
625250532Seadler	{
626250532Seadler		/*
627241784Seadler		 * OCZ Deneva R Series SSDs
628241784Seadler		 * 4k optimised & trim only works in 4k requests + 4k aligned
629241784Seadler		 */
630241784Seadler		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "DENRSTE251M45*", "*" },
631241784Seadler		/*quirks*/ADA_Q_4K
632241784Seadler	},
633241784Seadler	{
634241784Seadler		/*
635251061Ssmh		 * OCZ Vertex 2 SSDs (inc pro series)
636241784Seadler		 * 4k optimised & trim only works in 4k requests + 4k aligned
637241784Seadler		 */
638251061Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ?VERTEX2*", "*" },
639241784Seadler		/*quirks*/ADA_Q_4K
640241784Seadler	},
641241784Seadler	{
642251061Ssmh		/*
643251061Ssmh		 * OCZ Vertex 3 SSDs
644251061Ssmh		 * 4k optimised & trim only works in 4k requests + 4k aligned
645251061Ssmh		 */
646251061Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-VERTEX3*", "*" },
647251061Ssmh		/*quirks*/ADA_Q_4K
648251061Ssmh	},
649251061Ssmh	{
650251061Ssmh		/*
651253091Ssmh		 * OCZ Vertex 4 SSDs
652253091Ssmh		 * 4k optimised & trim only works in 4k requests + 4k aligned
653253091Ssmh		 */
654253091Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-VERTEX4*", "*" },
655253091Ssmh		/*quirks*/ADA_Q_4K
656253091Ssmh	},
657253091Ssmh	{
658253091Ssmh		/*
659331044Seadler		 * Samsung 750 SSDs
660331044Seadler		 * 4k optimised, NCQ TRIM seems to work
661331044Seadler		 */
662331044Seadler		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Samsung SSD 750*", "*" },
663331044Seadler		/*quirks*/ADA_Q_4K
664331044Seadler	},
665331044Seadler	{
666331044Seadler		/*
667251061Ssmh		 * Samsung 830 Series SSDs
668298002Simp		 * 4k optimised, NCQ TRIM Broken (normal TRIM is fine)
669251061Ssmh		 */
670251061Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG SSD 830 Series*", "*" },
671298002Simp		/*quirks*/ADA_Q_4K | ADA_Q_NCQ_TRIM_BROKEN
672251061Ssmh	},
673251061Ssmh	{
674251061Ssmh		/*
675269974Ssmh		 * Samsung 840 SSDs
676298002Simp		 * 4k optimised, NCQ TRIM Broken (normal TRIM is fine)
677269974Ssmh		 */
678269974Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Samsung SSD 840*", "*" },
679298002Simp		/*quirks*/ADA_Q_4K | ADA_Q_NCQ_TRIM_BROKEN
680269974Ssmh	},
681269974Ssmh	{
682269974Ssmh		/*
683331044Seadler		 * Samsung 845 SSDs
684331044Seadler		 * 4k optimised, NCQ TRIM Broken (normal TRIM is fine)
685331044Seadler		 */
686331044Seadler		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Samsung SSD 845*", "*" },
687331044Seadler		/*quirks*/ADA_Q_4K | ADA_Q_NCQ_TRIM_BROKEN
688331044Seadler	},
689331044Seadler	{
690331044Seadler		/*
691273279Sgnn		 * Samsung 850 SSDs
692298002Simp		 * 4k optimised, NCQ TRIM broken (normal TRIM fine)
693273279Sgnn		 */
694273279Sgnn		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Samsung SSD 850*", "*" },
695298002Simp		/*quirks*/ADA_Q_4K | ADA_Q_NCQ_TRIM_BROKEN
696273279Sgnn	},
697270305Ssbruno	{
698270305Ssbruno		/*
699298035Simp		 * Samsung SM863 Series SSDs (MZ7KM*)
700298035Simp		 * 4k optimised, NCQ believed to be working
701298035Simp		 */
702298035Simp		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG MZ7KM*", "*" },
703298035Simp		/*quirks*/ADA_Q_4K
704298035Simp	},
705298035Simp	{
706298035Simp		/*
707297370Sdumbbell		 * Samsung 843T Series SSDs (MZ7WD*)
708297370Sdumbbell		 * Samsung PM851 Series SSDs (MZ7TE*)
709297370Sdumbbell		 * Samsung PM853T Series SSDs (MZ7GE*)
710298035Simp		 * 4k optimised, NCQ believed to be broken since these are
711298035Simp		 * appear to be built with the same controllers as the 840/850.
712273183Ssbruno		 */
713297370Sdumbbell		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG MZ7*", "*" },
714298035Simp		/*quirks*/ADA_Q_4K | ADA_Q_NCQ_TRIM_BROKEN
715273183Ssbruno	},
716273183Ssbruno	{
717273183Ssbruno		/*
718326119Sbapt		 * Same as for SAMSUNG MZ7* but enable the quirks for SSD
719326119Sbapt		 * starting with MZ7* too
720326119Sbapt		 */
721326119Sbapt		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "MZ7*", "*" },
722326119Sbapt		/*quirks*/ADA_Q_4K | ADA_Q_NCQ_TRIM_BROKEN
723326119Sbapt	},
724326119Sbapt	{
725326119Sbapt		/*
726298137Simp		 * Samsung PM851 Series SSDs Dell OEM
727298137Simp		 * device model          "SAMSUNG SSD PM851 mSATA 256GB"
728298137Simp		 * 4k optimised, NCQ broken
729298137Simp		 */
730298137Simp		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG SSD PM851*", "*" },
731298137Simp		/*quirks*/ADA_Q_4K | ADA_Q_NCQ_TRIM_BROKEN
732298137Simp	},
733298137Simp	{
734298137Simp		/*
735251061Ssmh		 * SuperTalent TeraDrive CT SSDs
736251061Ssmh		 * 4k optimised & trim only works in 4k requests + 4k aligned
737251061Ssmh		 */
738251061Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "FTM??CT25H*", "*" },
739251061Ssmh		/*quirks*/ADA_Q_4K
740251061Ssmh	},
741251061Ssmh	{
742251061Ssmh		/*
743251061Ssmh		 * XceedIOPS SATA SSDs
744251061Ssmh		 * 4k optimised
745251061Ssmh		 */
746251061Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "SG9XCS2D*", "*" },
747251061Ssmh		/*quirks*/ADA_Q_4K
748251061Ssmh	},
749251061Ssmh	{
750300640Sken		/*
751300640Sken		 * Samsung drive that doesn't support READ LOG EXT or
752300640Sken		 * READ LOG DMA EXT, despite reporting that it does in
753300640Sken		 * ATA identify data:
754300640Sken		 * SAMSUNG HD200HJ KF100-06
755300640Sken		 */
756300640Sken		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD200*", "*" },
757300640Sken		/*quirks*/ADA_Q_LOG_BROKEN
758300640Sken	},
759300640Sken	{
760300640Sken		/*
761300640Sken		 * Samsung drive that doesn't support READ LOG EXT or
762300640Sken		 * READ LOG DMA EXT, despite reporting that it does in
763300640Sken		 * ATA identify data:
764300640Sken		 * SAMSUNG HD501LJ CR100-10
765300640Sken		 */
766300640Sken		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD501*", "*" },
767300640Sken		/*quirks*/ADA_Q_LOG_BROKEN
768300640Sken	},
769300640Sken	{
770300640Sken		/*
771300640Sken		 * Seagate Lamarr 8TB Shingled Magnetic Recording (SMR)
772300640Sken		 * Drive Managed SATA hard drive.  This drive doesn't report
773300640Sken		 * in firmware that it is a drive managed SMR drive.
774300640Sken		 */
775326825Sasomers		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST8000AS000[23]*", "*" },
776300640Sken		/*quirks*/ADA_Q_SMR_DM
777300640Sken	},
778300640Sken	{
779358490Sscottl		/* WD Green SSD */
780358490Sscottl		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WDS?????G0*", "*" },
781358490Sscottl		/*quirks*/ADA_Q_4K | ADA_Q_NCQ_TRIM_BROKEN
782358490Sscottl	},
783358490Sscottl	{
784199178Smav		/* Default */
785199178Smav		{
786199178Smav		  T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
787199178Smav		  /*vendor*/"*", /*product*/"*", /*revision*/"*"
788199178Smav		},
789199178Smav		/*quirks*/0
790199178Smav	},
791199178Smav};
792195534Sscottl
793195534Sscottlstatic	disk_strategy_t	adastrategy;
794195534Sscottlstatic	dumper_t	adadump;
795195534Sscottlstatic	periph_init_t	adainit;
796300207Skenstatic	void		adadiskgonecb(struct disk *dp);
797300207Skenstatic	periph_oninv_t	adaoninvalidate;
798300207Skenstatic	periph_dtor_t	adacleanup;
799195534Sscottlstatic	void		adaasync(void *callback_arg, u_int32_t code,
800195534Sscottl				struct cam_path *path, void *arg);
801300207Skenstatic	int		adazonemodesysctl(SYSCTL_HANDLER_ARGS);
802300207Skenstatic	int		adazonesupsysctl(SYSCTL_HANDLER_ARGS);
803195534Sscottlstatic	void		adasysctlinit(void *context, int pending);
804300207Skenstatic	int		adagetattr(struct bio *bp);
805300207Skenstatic	void		adasetflags(struct ada_softc *softc,
806300207Sken				    struct ccb_getdev *cgd);
807350805Smavstatic void		adasetgeom(struct ada_softc *softc,
808350805Smav				   struct ccb_getdev *cgd);
809195534Sscottlstatic	periph_ctor_t	adaregister;
810300207Skenstatic	void		ada_dsmtrim(struct ada_softc *softc, struct bio *bp,
811300207Sken				    struct ccb_ataio *ataio);
812300207Skenstatic	void 		ada_cfaerase(struct ada_softc *softc, struct bio *bp,
813300207Sken				     struct ccb_ataio *ataio);
814300207Skenstatic	int		ada_zone_bio_to_ata(int disk_zone_cmd);
815300207Skenstatic	int		ada_zone_cmd(struct cam_periph *periph, union ccb *ccb,
816300207Sken				     struct bio *bp, int *queue_ccb);
817195534Sscottlstatic	periph_start_t	adastart;
818300207Skenstatic	void		adaprobedone(struct cam_periph *periph, union ccb *ccb);
819300207Skenstatic	void		adazonedone(struct cam_periph *periph, union ccb *ccb);
820195534Sscottlstatic	void		adadone(struct cam_periph *periph,
821195534Sscottl			       union ccb *done_ccb);
822195534Sscottlstatic  int		adaerror(union ccb *ccb, u_int32_t cam_flags,
823195534Sscottl				u_int32_t sense_flags);
824195534Sscottlstatic timeout_t	adasendorderedtag;
825195534Sscottlstatic void		adashutdown(void *arg, int howto);
826220650Smavstatic void		adasuspend(void *arg);
827220650Smavstatic void		adaresume(void *arg);
828195534Sscottl
829221071Smav#ifndef	ADA_DEFAULT_LEGACY_ALIASES
830221071Smav#define	ADA_DEFAULT_LEGACY_ALIASES	1
831221071Smav#endif
832221071Smav
833195534Sscottl#ifndef ADA_DEFAULT_TIMEOUT
834195534Sscottl#define ADA_DEFAULT_TIMEOUT 30	/* Timeout in seconds */
835195534Sscottl#endif
836195534Sscottl
837195534Sscottl#ifndef	ADA_DEFAULT_RETRY
838195534Sscottl#define	ADA_DEFAULT_RETRY	4
839195534Sscottl#endif
840195534Sscottl
841195534Sscottl#ifndef	ADA_DEFAULT_SEND_ORDERED
842195534Sscottl#define	ADA_DEFAULT_SEND_ORDERED	1
843195534Sscottl#endif
844195534Sscottl
845214279Sbrucec#ifndef	ADA_DEFAULT_SPINDOWN_SHUTDOWN
846214279Sbrucec#define	ADA_DEFAULT_SPINDOWN_SHUTDOWN	1
847214279Sbrucec#endif
848214279Sbrucec
849220650Smav#ifndef	ADA_DEFAULT_SPINDOWN_SUSPEND
850220650Smav#define	ADA_DEFAULT_SPINDOWN_SUSPEND	1
851220650Smav#endif
852220650Smav
853224497Smav#ifndef	ADA_DEFAULT_READ_AHEAD
854224497Smav#define	ADA_DEFAULT_READ_AHEAD	1
855224497Smav#endif
856224497Smav
857220412Smav#ifndef	ADA_DEFAULT_WRITE_CACHE
858220412Smav#define	ADA_DEFAULT_WRITE_CACHE	1
859220412Smav#endif
860220412Smav
861224497Smav#define	ADA_RA	(softc->read_ahead >= 0 ? \
862224497Smav		 softc->read_ahead : ada_read_ahead)
863224497Smav#define	ADA_WC	(softc->write_cache >= 0 ? \
864224497Smav		 softc->write_cache : ada_write_cache)
865224497Smav
866208349Smarius/*
867208349Smarius * Most platforms map firmware geometry to actual, but some don't.  If
868208349Smarius * not overridden, default to nothing.
869208349Smarius */
870208349Smarius#ifndef ata_disk_firmware_geom_adjust
871208349Smarius#define	ata_disk_firmware_geom_adjust(disk)
872208349Smarius#endif
873195534Sscottl
874195534Sscottlstatic int ada_retry_count = ADA_DEFAULT_RETRY;
875195534Sscottlstatic int ada_default_timeout = ADA_DEFAULT_TIMEOUT;
876195534Sscottlstatic int ada_send_ordered = ADA_DEFAULT_SEND_ORDERED;
877214279Sbrucecstatic int ada_spindown_shutdown = ADA_DEFAULT_SPINDOWN_SHUTDOWN;
878220650Smavstatic int ada_spindown_suspend = ADA_DEFAULT_SPINDOWN_SUSPEND;
879224497Smavstatic int ada_read_ahead = ADA_DEFAULT_READ_AHEAD;
880220412Smavstatic int ada_write_cache = ADA_DEFAULT_WRITE_CACHE;
881195534Sscottl
882227309Sedstatic SYSCTL_NODE(_kern_cam, OID_AUTO, ada, CTLFLAG_RD, 0,
883195534Sscottl            "CAM Direct Access Disk driver");
884267992ShselaskySYSCTL_INT(_kern_cam_ada, OID_AUTO, retry_count, CTLFLAG_RWTUN,
885195534Sscottl           &ada_retry_count, 0, "Normal I/O retry count");
886267992ShselaskySYSCTL_INT(_kern_cam_ada, OID_AUTO, default_timeout, CTLFLAG_RWTUN,
887195534Sscottl           &ada_default_timeout, 0, "Normal I/O timeout (in seconds)");
888267992ShselaskySYSCTL_INT(_kern_cam_ada, OID_AUTO, send_ordered, CTLFLAG_RWTUN,
889195534Sscottl           &ada_send_ordered, 0, "Send Ordered Tags");
890267992ShselaskySYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_shutdown, CTLFLAG_RWTUN,
891214279Sbrucec           &ada_spindown_shutdown, 0, "Spin down upon shutdown");
892267992ShselaskySYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_suspend, CTLFLAG_RWTUN,
893220650Smav           &ada_spindown_suspend, 0, "Spin down upon suspend");
894267992ShselaskySYSCTL_INT(_kern_cam_ada, OID_AUTO, read_ahead, CTLFLAG_RWTUN,
895224497Smav           &ada_read_ahead, 0, "Enable disk read-ahead");
896267992ShselaskySYSCTL_INT(_kern_cam_ada, OID_AUTO, write_cache, CTLFLAG_RWTUN,
897220412Smav           &ada_write_cache, 0, "Enable disk write cache");
898195534Sscottl
899195534Sscottl/*
900195534Sscottl * ADA_ORDEREDTAG_INTERVAL determines how often, relative
901195534Sscottl * to the default timeout, we check to see whether an ordered
902195534Sscottl * tagged transaction is appropriate to prevent simple tag
903195534Sscottl * starvation.  Since we'd like to ensure that there is at least
904195534Sscottl * 1/2 of the timeout length left for a starved transaction to
905195534Sscottl * complete after we've sent an ordered tag, we must poll at least
906195534Sscottl * four times in every timeout period.  This takes care of the worst
907195534Sscottl * case where a starved transaction starts during an interval that
908195534Sscottl * meets the requirement "don't send an ordered tag" test so it takes
909195534Sscottl * us two intervals to determine that a tag must be sent.
910195534Sscottl */
911195534Sscottl#ifndef ADA_ORDEREDTAG_INTERVAL
912195534Sscottl#define ADA_ORDEREDTAG_INTERVAL 4
913195534Sscottl#endif
914195534Sscottl
915195534Sscottlstatic struct periph_driver adadriver =
916195534Sscottl{
917195534Sscottl	adainit, "ada",
918195534Sscottl	TAILQ_HEAD_INITIALIZER(adadriver.units), /* generation */ 0
919195534Sscottl};
920195534Sscottl
921298002Simpstatic int adadeletemethodsysctl(SYSCTL_HANDLER_ARGS);
922298002Simp
923195534SscottlPERIPHDRIVER_DECLARE(ada, adadriver);
924195534Sscottl
925300207Skenstatic MALLOC_DEFINE(M_ATADA, "ata_da", "ata_da buffers");
926300207Sken
927195534Sscottlstatic int
928195534Sscottladaopen(struct disk *dp)
929195534Sscottl{
930195534Sscottl	struct cam_periph *periph;
931195534Sscottl	struct ada_softc *softc;
932195534Sscottl	int error;
933195534Sscottl
934195534Sscottl	periph = (struct cam_periph *)dp->d_drv1;
935195534Sscottl	if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
936195534Sscottl		return(ENXIO);
937195534Sscottl	}
938195534Sscottl
939195534Sscottl	cam_periph_lock(periph);
940195534Sscottl	if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
941195534Sscottl		cam_periph_unlock(periph);
942195534Sscottl		cam_periph_release(periph);
943195534Sscottl		return (error);
944195534Sscottl	}
945195534Sscottl
946236602Smav	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
947236602Smav	    ("adaopen\n"));
948195534Sscottl
949249981Smav	softc = (struct ada_softc *)periph->softc;
950249981Smav	softc->flags |= ADA_FLAG_OPEN;
951195534Sscottl
952195534Sscottl	cam_periph_unhold(periph);
953195534Sscottl	cam_periph_unlock(periph);
954195534Sscottl	return (0);
955195534Sscottl}
956195534Sscottl
957195534Sscottlstatic int
958195534Sscottladaclose(struct disk *dp)
959195534Sscottl{
960195534Sscottl	struct	cam_periph *periph;
961195534Sscottl	struct	ada_softc *softc;
962195534Sscottl	union ccb *ccb;
963253724Smav	int error;
964195534Sscottl
965195534Sscottl	periph = (struct cam_periph *)dp->d_drv1;
966256843Smav	softc = (struct ada_softc *)periph->softc;
967195534Sscottl	cam_periph_lock(periph);
968195534Sscottl
969236602Smav	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
970236602Smav	    ("adaclose\n"));
971236602Smav
972195534Sscottl	/* We only sync the cache if the drive is capable of it. */
973253724Smav	if ((softc->flags & ADA_FLAG_DIRTY) != 0 &&
974253724Smav	    (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) != 0 &&
975256843Smav	    (periph->flags & CAM_PERIPH_INVALID) == 0 &&
976256843Smav	    cam_periph_hold(periph, PRIBIO) == 0) {
977195534Sscottl
978198382Smav		ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
979195534Sscottl		cam_fill_ataio(&ccb->ataio,
980195534Sscottl				    1,
981195534Sscottl				    adadone,
982195534Sscottl				    CAM_DIR_NONE,
983195534Sscottl				    0,
984195534Sscottl				    NULL,
985195534Sscottl				    0,
986195534Sscottl				    ada_default_timeout*1000);
987195534Sscottl
988195534Sscottl		if (softc->flags & ADA_FLAG_CAN_48BIT)
989195534Sscottl			ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0);
990195534Sscottl		else
991196659Smav			ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0);
992253724Smav		error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0,
993198328Smav		    /*sense_flags*/0, softc->disk->d_devstat);
994195534Sscottl
995253724Smav		if (error != 0)
996195534Sscottl			xpt_print(periph->path, "Synchronize cache failed\n");
997308080Smav		softc->flags &= ~ADA_FLAG_DIRTY;
998195534Sscottl		xpt_release_ccb(ccb);
999256843Smav		cam_periph_unhold(periph);
1000195534Sscottl	}
1001195534Sscottl
1002195534Sscottl	softc->flags &= ~ADA_FLAG_OPEN;
1003256843Smav
1004256843Smav	while (softc->refcount != 0)
1005256843Smav		cam_periph_sleep(periph, &softc->refcount, PRIBIO, "adaclose", 1);
1006195534Sscottl	cam_periph_unlock(periph);
1007195534Sscottl	cam_periph_release(periph);
1008195534Sscottl	return (0);
1009195534Sscottl}
1010195534Sscottl
1011201139Smavstatic void
1012201139Smavadaschedule(struct cam_periph *periph)
1013201139Smav{
1014201139Smav	struct ada_softc *softc = (struct ada_softc *)periph->softc;
1015201139Smav
1016249205Smav	if (softc->state != ADA_STATE_NORMAL)
1017249205Smav		return;
1018249205Smav
1019298002Simp	cam_iosched_schedule(softc->cam_iosched, periph);
1020201139Smav}
1021201139Smav
1022195534Sscottl/*
1023195534Sscottl * Actually translate the requested transfer into one the physical driver
1024195534Sscottl * can understand.  The transfer is described by a buf and will include
1025195534Sscottl * only one physical transfer.
1026195534Sscottl */
1027195534Sscottlstatic void
1028195534Sscottladastrategy(struct bio *bp)
1029195534Sscottl{
1030195534Sscottl	struct cam_periph *periph;
1031195534Sscottl	struct ada_softc *softc;
1032195534Sscottl
1033195534Sscottl	periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1034195534Sscottl	softc = (struct ada_softc *)periph->softc;
1035195534Sscottl
1036195534Sscottl	cam_periph_lock(periph);
1037195534Sscottl
1038236602Smav	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adastrategy(%p)\n", bp));
1039236602Smav
1040195534Sscottl	/*
1041195534Sscottl	 * If the device has been made invalid, error out
1042195534Sscottl	 */
1043249981Smav	if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
1044195534Sscottl		cam_periph_unlock(periph);
1045195534Sscottl		biofinish(bp, NULL, ENXIO);
1046195534Sscottl		return;
1047195534Sscottl	}
1048300207Sken
1049300207Sken	/*
1050300207Sken	 * Zone commands must be ordered, because they can depend on the
1051300207Sken	 * effects of previously issued commands, and they may affect
1052300207Sken	 * commands after them.
1053300207Sken	 */
1054300207Sken	if (bp->bio_cmd == BIO_ZONE)
1055300207Sken		bp->bio_flags |= BIO_ORDERED;
1056195534Sscottl
1057195534Sscottl	/*
1058195534Sscottl	 * Place it in the queue of disk activities for this disk
1059195534Sscottl	 */
1060298002Simp	cam_iosched_queue_work(softc->cam_iosched, bp);
1061195534Sscottl
1062195534Sscottl	/*
1063195534Sscottl	 * Schedule ourselves for performing the work.
1064195534Sscottl	 */
1065201139Smav	adaschedule(periph);
1066195534Sscottl	cam_periph_unlock(periph);
1067195534Sscottl
1068195534Sscottl	return;
1069195534Sscottl}
1070195534Sscottl
1071195534Sscottlstatic int
1072195534Sscottladadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
1073195534Sscottl{
1074195534Sscottl	struct	    cam_periph *periph;
1075195534Sscottl	struct	    ada_softc *softc;
1076195534Sscottl	u_int	    secsize;
1077195534Sscottl	union	    ccb ccb;
1078195534Sscottl	struct	    disk *dp;
1079195534Sscottl	uint64_t    lba;
1080195534Sscottl	uint16_t    count;
1081236814Smav	int	    error = 0;
1082195534Sscottl
1083195534Sscottl	dp = arg;
1084195534Sscottl	periph = dp->d_drv1;
1085195534Sscottl	softc = (struct ada_softc *)periph->softc;
1086195534Sscottl	cam_periph_lock(periph);
1087195534Sscottl	secsize = softc->params.secsize;
1088195534Sscottl	lba = offset / secsize;
1089195534Sscottl	count = length / secsize;
1090195534Sscottl
1091249981Smav	if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
1092195534Sscottl		cam_periph_unlock(periph);
1093195534Sscottl		return (ENXIO);
1094195534Sscottl	}
1095195534Sscottl
1096195534Sscottl	if (length > 0) {
1097198382Smav		xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1098195534Sscottl		ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
1099195534Sscottl		cam_fill_ataio(&ccb.ataio,
1100195534Sscottl		    0,
1101195534Sscottl		    adadone,
1102195534Sscottl		    CAM_DIR_OUT,
1103195534Sscottl		    0,
1104195534Sscottl		    (u_int8_t *) virtual,
1105195534Sscottl		    length,
1106195534Sscottl		    ada_default_timeout*1000);
1107195534Sscottl		if ((softc->flags & ADA_FLAG_CAN_48BIT) &&
1108195534Sscottl		    (lba + count >= ATA_MAX_28BIT_LBA ||
1109195534Sscottl		    count >= 256)) {
1110195534Sscottl			ata_48bit_cmd(&ccb.ataio, ATA_WRITE_DMA48,
1111195534Sscottl			    0, lba, count);
1112195534Sscottl		} else {
1113196659Smav			ata_28bit_cmd(&ccb.ataio, ATA_WRITE_DMA,
1114195534Sscottl			    0, lba, count);
1115195534Sscottl		}
1116195534Sscottl		xpt_polled_action(&ccb);
1117195534Sscottl
1118236814Smav		error = cam_periph_error(&ccb,
1119236814Smav		    0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1120236814Smav		if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
1121236814Smav			cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0,
1122236814Smav			    /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1123236814Smav		if (error != 0)
1124195534Sscottl			printf("Aborting dump due to I/O error.\n");
1125236814Smav
1126195534Sscottl		cam_periph_unlock(periph);
1127236814Smav		return (error);
1128195534Sscottl	}
1129195534Sscottl
1130195534Sscottl	if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) {
1131198382Smav		xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1132195534Sscottl
1133298011Simp		/*
1134298023Sngie		 * Tell the drive to flush its internal cache. if we
1135298011Simp		 * can't flush in 5s we have big problems. No need to
1136298011Simp		 * wait the default 60s to detect problems.
1137298011Simp		 */
1138195534Sscottl		ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
1139195534Sscottl		cam_fill_ataio(&ccb.ataio,
1140236814Smav				    0,
1141195534Sscottl				    adadone,
1142195534Sscottl				    CAM_DIR_NONE,
1143195534Sscottl				    0,
1144195534Sscottl				    NULL,
1145195534Sscottl				    0,
1146298002Simp				    5*1000);
1147195534Sscottl
1148195534Sscottl		if (softc->flags & ADA_FLAG_CAN_48BIT)
1149195534Sscottl			ata_48bit_cmd(&ccb.ataio, ATA_FLUSHCACHE48, 0, 0, 0);
1150195534Sscottl		else
1151196659Smav			ata_28bit_cmd(&ccb.ataio, ATA_FLUSHCACHE, 0, 0, 0);
1152195534Sscottl		xpt_polled_action(&ccb);
1153195534Sscottl
1154236814Smav		error = cam_periph_error(&ccb,
1155236814Smav		    0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1156236814Smav		if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
1157236814Smav			cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0,
1158236814Smav			    /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1159236814Smav		if (error != 0)
1160195534Sscottl			xpt_print(periph->path, "Synchronize cache failed\n");
1161195534Sscottl	}
1162195534Sscottl	cam_periph_unlock(periph);
1163236814Smav	return (error);
1164195534Sscottl}
1165195534Sscottl
1166195534Sscottlstatic void
1167195534Sscottladainit(void)
1168195534Sscottl{
1169195534Sscottl	cam_status status;
1170195534Sscottl
1171195534Sscottl	/*
1172195534Sscottl	 * Install a global async callback.  This callback will
1173195534Sscottl	 * receive async callbacks like "new device found".
1174195534Sscottl	 */
1175195534Sscottl	status = xpt_register_async(AC_FOUND_DEVICE, adaasync, NULL, NULL);
1176195534Sscottl
1177195534Sscottl	if (status != CAM_REQ_CMP) {
1178195534Sscottl		printf("ada: Failed to attach master async callback "
1179195534Sscottl		       "due to status 0x%x!\n", status);
1180195534Sscottl	} else if (ada_send_ordered) {
1181195534Sscottl
1182220650Smav		/* Register our event handlers */
1183220650Smav		if ((EVENTHANDLER_REGISTER(power_suspend, adasuspend,
1184220650Smav					   NULL, EVENTHANDLER_PRI_LAST)) == NULL)
1185220650Smav		    printf("adainit: power event registration failed!\n");
1186220650Smav		if ((EVENTHANDLER_REGISTER(power_resume, adaresume,
1187220650Smav					   NULL, EVENTHANDLER_PRI_LAST)) == NULL)
1188220650Smav		    printf("adainit: power event registration failed!\n");
1189220650Smav		if ((EVENTHANDLER_REGISTER(shutdown_post_sync, adashutdown,
1190195534Sscottl					   NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
1191195534Sscottl		    printf("adainit: shutdown event registration failed!\n");
1192195534Sscottl	}
1193195534Sscottl}
1194195534Sscottl
1195249347Sken/*
1196249347Sken * Callback from GEOM, called when it has finished cleaning up its
1197249347Sken * resources.
1198249347Sken */
1199195534Sscottlstatic void
1200249347Skenadadiskgonecb(struct disk *dp)
1201249347Sken{
1202249347Sken	struct cam_periph *periph;
1203249347Sken
1204249347Sken	periph = (struct cam_periph *)dp->d_drv1;
1205249347Sken
1206249347Sken	cam_periph_release(periph);
1207249347Sken}
1208249347Sken
1209249347Skenstatic void
1210195534Sscottladaoninvalidate(struct cam_periph *periph)
1211195534Sscottl{
1212195534Sscottl	struct ada_softc *softc;
1213195534Sscottl
1214195534Sscottl	softc = (struct ada_softc *)periph->softc;
1215195534Sscottl
1216195534Sscottl	/*
1217195534Sscottl	 * De-register any async callbacks.
1218195534Sscottl	 */
1219195534Sscottl	xpt_register_async(0, adaasync, periph, periph->path);
1220298002Simp#ifdef CAM_IO_STATS
1221298002Simp	softc->invalidations++;
1222298002Simp#endif
1223195534Sscottl
1224195534Sscottl	/*
1225195534Sscottl	 * Return all queued I/O with ENXIO.
1226195534Sscottl	 * XXX Handle any transactions queued to the card
1227195534Sscottl	 *     with XPT_ABORT_CCB.
1228195534Sscottl	 */
1229298002Simp	cam_iosched_flush(softc->cam_iosched, NULL, ENXIO);
1230195534Sscottl
1231195534Sscottl	disk_gone(softc->disk);
1232195534Sscottl}
1233195534Sscottl
1234195534Sscottlstatic void
1235195534Sscottladacleanup(struct cam_periph *periph)
1236195534Sscottl{
1237195534Sscottl	struct ada_softc *softc;
1238195534Sscottl
1239195534Sscottl	softc = (struct ada_softc *)periph->softc;
1240195534Sscottl
1241195534Sscottl	cam_periph_unlock(periph);
1242195534Sscottl
1243298002Simp	cam_iosched_fini(softc->cam_iosched);
1244298002Simp
1245195534Sscottl	/*
1246195534Sscottl	 * If we can't free the sysctl tree, oh well...
1247195534Sscottl	 */
1248298002Simp	if ((softc->flags & ADA_FLAG_SCTX_INIT) != 0) {
1249298002Simp#ifdef CAM_IO_STATS
1250298002Simp		if (sysctl_ctx_free(&softc->sysctl_stats_ctx) != 0)
1251298002Simp			xpt_print(periph->path,
1252298002Simp			    "can't remove sysctl stats context\n");
1253298002Simp#endif
1254298002Simp		if (sysctl_ctx_free(&softc->sysctl_ctx) != 0)
1255298002Simp			xpt_print(periph->path,
1256298002Simp			    "can't remove sysctl context\n");
1257195534Sscottl	}
1258195534Sscottl
1259195534Sscottl	disk_destroy(softc->disk);
1260195534Sscottl	callout_drain(&softc->sendordered_c);
1261195534Sscottl	free(softc, M_DEVBUF);
1262195534Sscottl	cam_periph_lock(periph);
1263195534Sscottl}
1264195534Sscottl
1265195534Sscottlstatic void
1266298002Simpadasetdeletemethod(struct ada_softc *softc)
1267298002Simp{
1268298002Simp
1269298002Simp	if (softc->flags & ADA_FLAG_CAN_NCQ_TRIM)
1270298002Simp		softc->delete_method = ADA_DELETE_NCQ_DSM_TRIM;
1271298002Simp	else if (softc->flags & ADA_FLAG_CAN_TRIM)
1272298002Simp		softc->delete_method = ADA_DELETE_DSM_TRIM;
1273298002Simp	else if ((softc->flags & ADA_FLAG_CAN_CFA) && !(softc->flags & ADA_FLAG_CAN_48BIT))
1274298002Simp		softc->delete_method = ADA_DELETE_CFA_ERASE;
1275298002Simp	else
1276298002Simp		softc->delete_method = ADA_DELETE_NONE;
1277298002Simp}
1278298002Simp
1279298002Simpstatic void
1280195534Sscottladaasync(void *callback_arg, u_int32_t code,
1281195534Sscottl	struct cam_path *path, void *arg)
1282195534Sscottl{
1283236393Smav	struct ccb_getdev cgd;
1284195534Sscottl	struct cam_periph *periph;
1285220412Smav	struct ada_softc *softc;
1286195534Sscottl
1287195534Sscottl	periph = (struct cam_periph *)callback_arg;
1288195534Sscottl	switch (code) {
1289195534Sscottl	case AC_FOUND_DEVICE:
1290195534Sscottl	{
1291195534Sscottl		struct ccb_getdev *cgd;
1292195534Sscottl		cam_status status;
1293195534Sscottl
1294195534Sscottl		cgd = (struct ccb_getdev *)arg;
1295195534Sscottl		if (cgd == NULL)
1296195534Sscottl			break;
1297195534Sscottl
1298195534Sscottl		if (cgd->protocol != PROTO_ATA)
1299195534Sscottl			break;
1300195534Sscottl
1301195534Sscottl		/*
1302195534Sscottl		 * Allocate a peripheral instance for
1303195534Sscottl		 * this device and start the probe
1304195534Sscottl		 * process.
1305195534Sscottl		 */
1306195534Sscottl		status = cam_periph_alloc(adaregister, adaoninvalidate,
1307195534Sscottl					  adacleanup, adastart,
1308195534Sscottl					  "ada", CAM_PERIPH_BIO,
1309256843Smav					  path, adaasync,
1310195534Sscottl					  AC_FOUND_DEVICE, cgd);
1311195534Sscottl
1312195534Sscottl		if (status != CAM_REQ_CMP
1313195534Sscottl		 && status != CAM_REQ_INPROG)
1314195534Sscottl			printf("adaasync: Unable to attach to new device "
1315195534Sscottl				"due to status 0x%x\n", status);
1316195534Sscottl		break;
1317195534Sscottl	}
1318236393Smav	case AC_GETDEV_CHANGED:
1319236393Smav	{
1320236393Smav		softc = (struct ada_softc *)periph->softc;
1321236393Smav		xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1322236393Smav		cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1323236393Smav		xpt_action((union ccb *)&cgd);
1324236393Smav
1325300207Sken		/*
1326350805Smav		 * Update our information based on the new Identify data.
1327300207Sken		 */
1328300207Sken		adasetflags(softc, &cgd);
1329350805Smav		adasetgeom(softc, &cgd);
1330350805Smav		disk_resize(softc->disk, M_NOWAIT);
1331298002Simp
1332236393Smav		cam_periph_async(periph, code, path, arg);
1333236393Smav		break;
1334236393Smav	}
1335235897Smav	case AC_ADVINFO_CHANGED:
1336235897Smav	{
1337235897Smav		uintptr_t buftype;
1338235897Smav
1339235897Smav		buftype = (uintptr_t)arg;
1340235897Smav		if (buftype == CDAI_TYPE_PHYS_PATH) {
1341235897Smav			struct ada_softc *softc;
1342235897Smav
1343235897Smav			softc = periph->softc;
1344235897Smav			disk_attr_changed(softc->disk, "GEOM::physpath",
1345235897Smav					  M_NOWAIT);
1346235897Smav		}
1347235897Smav		break;
1348235897Smav	}
1349220412Smav	case AC_SENT_BDR:
1350220412Smav	case AC_BUS_RESET:
1351220412Smav	{
1352220412Smav		softc = (struct ada_softc *)periph->softc;
1353220412Smav		cam_periph_async(periph, code, path, arg);
1354220412Smav		if (softc->state != ADA_STATE_NORMAL)
1355220412Smav			break;
1356220454Smav		xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1357220412Smav		cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1358220412Smav		xpt_action((union ccb *)&cgd);
1359300207Sken		if (ADA_RA >= 0 && softc->flags & ADA_FLAG_CAN_RAHEAD)
1360224497Smav			softc->state = ADA_STATE_RAHEAD;
1361321976Smav		else if (ADA_WC >= 0 && softc->flags & ADA_FLAG_CAN_WCACHE)
1362224497Smav			softc->state = ADA_STATE_WCACHE;
1363300640Sken		else if ((softc->flags & ADA_FLAG_CAN_LOG)
1364300640Sken		      && (softc->zone_mode != ADA_ZONE_NONE))
1365300207Sken			softc->state = ADA_STATE_LOGDIR;
1366224497Smav		else
1367224497Smav		    break;
1368256843Smav		if (cam_periph_acquire(periph) != CAM_REQ_CMP)
1369256843Smav			softc->state = ADA_STATE_NORMAL;
1370256843Smav		else
1371256843Smav			xpt_schedule(periph, CAM_PRIORITY_DEV);
1372220412Smav	}
1373195534Sscottl	default:
1374195534Sscottl		cam_periph_async(periph, code, path, arg);
1375195534Sscottl		break;
1376195534Sscottl	}
1377195534Sscottl}
1378195534Sscottl
1379300207Skenstatic int
1380300207Skenadazonemodesysctl(SYSCTL_HANDLER_ARGS)
1381300207Sken{
1382300207Sken	char tmpbuf[40];
1383300207Sken	struct ada_softc *softc;
1384300207Sken	int error;
1385300207Sken
1386300207Sken	softc = (struct ada_softc *)arg1;
1387300207Sken
1388300207Sken	switch (softc->zone_mode) {
1389300207Sken	case ADA_ZONE_DRIVE_MANAGED:
1390300207Sken		snprintf(tmpbuf, sizeof(tmpbuf), "Drive Managed");
1391300207Sken		break;
1392300207Sken	case ADA_ZONE_HOST_AWARE:
1393300207Sken		snprintf(tmpbuf, sizeof(tmpbuf), "Host Aware");
1394300207Sken		break;
1395300207Sken	case ADA_ZONE_HOST_MANAGED:
1396300207Sken		snprintf(tmpbuf, sizeof(tmpbuf), "Host Managed");
1397300207Sken		break;
1398300207Sken	case ADA_ZONE_NONE:
1399300207Sken	default:
1400300207Sken		snprintf(tmpbuf, sizeof(tmpbuf), "Not Zoned");
1401300207Sken		break;
1402300207Sken	}
1403300207Sken
1404300207Sken	error = sysctl_handle_string(oidp, tmpbuf, sizeof(tmpbuf), req);
1405300207Sken
1406300207Sken	return (error);
1407300207Sken}
1408300207Sken
1409300207Skenstatic int
1410300207Skenadazonesupsysctl(SYSCTL_HANDLER_ARGS)
1411300207Sken{
1412300207Sken	char tmpbuf[180];
1413300207Sken	struct ada_softc *softc;
1414300207Sken	struct sbuf sb;
1415300207Sken	int error, first;
1416300207Sken	unsigned int i;
1417300207Sken
1418300207Sken	softc = (struct ada_softc *)arg1;
1419300207Sken
1420300207Sken	error = 0;
1421300207Sken	first = 1;
1422300207Sken	sbuf_new(&sb, tmpbuf, sizeof(tmpbuf), 0);
1423300207Sken
1424300207Sken	for (i = 0; i < sizeof(ada_zone_desc_table) /
1425300207Sken	     sizeof(ada_zone_desc_table[0]); i++) {
1426300207Sken		if (softc->zone_flags & ada_zone_desc_table[i].value) {
1427300207Sken			if (first == 0)
1428300207Sken				sbuf_printf(&sb, ", ");
1429300207Sken			else
1430300207Sken				first = 0;
1431300207Sken			sbuf_cat(&sb, ada_zone_desc_table[i].desc);
1432300207Sken		}
1433300207Sken	}
1434300207Sken
1435300207Sken	if (first == 1)
1436300207Sken		sbuf_printf(&sb, "None");
1437300207Sken
1438300207Sken	sbuf_finish(&sb);
1439300207Sken
1440300207Sken	error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
1441300207Sken
1442300207Sken	return (error);
1443300207Sken}
1444300207Sken
1445300207Sken
1446195534Sscottlstatic void
1447195534Sscottladasysctlinit(void *context, int pending)
1448195534Sscottl{
1449195534Sscottl	struct cam_periph *periph;
1450195534Sscottl	struct ada_softc *softc;
1451327228Smav	char tmpstr[32], tmpstr2[16];
1452195534Sscottl
1453195534Sscottl	periph = (struct cam_periph *)context;
1454220454Smav
1455220454Smav	/* periph was held for us when this task was enqueued */
1456249981Smav	if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
1457220454Smav		cam_periph_release(periph);
1458195534Sscottl		return;
1459220454Smav	}
1460195534Sscottl
1461195534Sscottl	softc = (struct ada_softc *)periph->softc;
1462300207Sken	snprintf(tmpstr, sizeof(tmpstr), "CAM ADA unit %d",periph->unit_number);
1463195534Sscottl	snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1464195534Sscottl
1465195534Sscottl	sysctl_ctx_init(&softc->sysctl_ctx);
1466195534Sscottl	softc->flags |= ADA_FLAG_SCTX_INIT;
1467195534Sscottl	softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1468195534Sscottl		SYSCTL_STATIC_CHILDREN(_kern_cam_ada), OID_AUTO, tmpstr2,
1469195534Sscottl		CTLFLAG_RD, 0, tmpstr);
1470195534Sscottl	if (softc->sysctl_tree == NULL) {
1471195534Sscottl		printf("adasysctlinit: unable to allocate sysctl tree\n");
1472195534Sscottl		cam_periph_release(periph);
1473195534Sscottl		return;
1474195534Sscottl	}
1475195534Sscottl
1476298002Simp	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1477298002Simp		OID_AUTO, "delete_method", CTLTYPE_STRING | CTLFLAG_RW,
1478298002Simp		softc, 0, adadeletemethodsysctl, "A",
1479298002Simp		"BIO_DELETE execution method");
1480220454Smav	SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1481224497Smav		OID_AUTO, "read_ahead", CTLFLAG_RW | CTLFLAG_MPSAFE,
1482224497Smav		&softc->read_ahead, 0, "Enable disk read ahead.");
1483224497Smav	SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1484220454Smav		OID_AUTO, "write_cache", CTLFLAG_RW | CTLFLAG_MPSAFE,
1485220454Smav		&softc->write_cache, 0, "Enable disk write cache.");
1486248922Ssmh	SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1487298002Simp		OID_AUTO, "unmapped_io", CTLFLAG_RD | CTLFLAG_MPSAFE,
1488298002Simp		&softc->unmappedio, 0, "Unmapped I/O leaf");
1489298002Simp	SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1490298002Simp		OID_AUTO, "rotating", CTLFLAG_RD | CTLFLAG_MPSAFE,
1491298002Simp		&softc->rotating, 0, "Rotating media");
1492300207Sken	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1493300207Sken		OID_AUTO, "zone_mode", CTLTYPE_STRING | CTLFLAG_RD,
1494300207Sken		softc, 0, adazonemodesysctl, "A",
1495300207Sken		"Zone Mode");
1496300207Sken	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1497300207Sken		OID_AUTO, "zone_support", CTLTYPE_STRING | CTLFLAG_RD,
1498300207Sken		softc, 0, adazonesupsysctl, "A",
1499300207Sken		"Zone Support");
1500300207Sken	SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
1501300207Sken		SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
1502300207Sken		"optimal_seq_zones", CTLFLAG_RD, &softc->optimal_seq_zones,
1503300207Sken		"Optimal Number of Open Sequential Write Preferred Zones");
1504300207Sken	SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
1505300207Sken		SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
1506300207Sken		"optimal_nonseq_zones", CTLFLAG_RD,
1507300207Sken		&softc->optimal_nonseq_zones,
1508300207Sken		"Optimal Number of Non-Sequentially Written Sequential Write "
1509300207Sken		"Preferred Zones");
1510300207Sken	SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
1511300207Sken		SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
1512300207Sken		"max_seq_zones", CTLFLAG_RD, &softc->max_seq_zones,
1513300207Sken		"Maximum Number of Open Sequential Write Required Zones");
1514300207Sken
1515220454Smav#ifdef ADA_TEST_FAILURE
1516220454Smav	/*
1517220454Smav	 * Add a 'door bell' sysctl which allows one to set it from userland
1518220454Smav	 * and cause something bad to happen.  For the moment, we only allow
1519220454Smav	 * whacking the next read or write.
1520220454Smav	 */
1521220454Smav	SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1522220454Smav		OID_AUTO, "force_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
1523220454Smav		&softc->force_read_error, 0,
1524220454Smav		"Force a read error for the next N reads.");
1525220454Smav	SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1526220454Smav		OID_AUTO, "force_write_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
1527220454Smav		&softc->force_write_error, 0,
1528220454Smav		"Force a write error for the next N writes.");
1529220454Smav	SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1530220454Smav		OID_AUTO, "periodic_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
1531220454Smav		&softc->periodic_read_error, 0,
1532220454Smav		"Force a read error every N reads (don't set too low).");
1533220454Smav#endif
1534298002Simp
1535298002Simp#ifdef CAM_IO_STATS
1536298002Simp	softc->sysctl_stats_tree = SYSCTL_ADD_NODE(&softc->sysctl_stats_ctx,
1537298002Simp		SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, "stats",
1538298002Simp		CTLFLAG_RD, 0, "Statistics");
1539298002Simp	SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
1540298002Simp		SYSCTL_CHILDREN(softc->sysctl_stats_tree),
1541298002Simp		OID_AUTO, "timeouts", CTLFLAG_RD | CTLFLAG_MPSAFE,
1542298002Simp		&softc->timeouts, 0,
1543298002Simp		"Device timeouts reported by the SIM");
1544298002Simp	SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
1545298002Simp		SYSCTL_CHILDREN(softc->sysctl_stats_tree),
1546298002Simp		OID_AUTO, "errors", CTLFLAG_RD | CTLFLAG_MPSAFE,
1547298002Simp		&softc->errors, 0,
1548298002Simp		"Transport errors reported by the SIM.");
1549298002Simp	SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
1550298002Simp		SYSCTL_CHILDREN(softc->sysctl_stats_tree),
1551298002Simp		OID_AUTO, "pack_invalidations", CTLFLAG_RD | CTLFLAG_MPSAFE,
1552298002Simp		&softc->invalidations, 0,
1553298002Simp		"Device pack invalidations.");
1554298002Simp#endif
1555298002Simp
1556298002Simp	cam_iosched_sysctl_init(softc->cam_iosched, &softc->sysctl_ctx,
1557298002Simp	    softc->sysctl_tree);
1558298002Simp
1559195534Sscottl	cam_periph_release(periph);
1560195534Sscottl}
1561195534Sscottl
1562223089Sgibbsstatic int
1563223089Sgibbsadagetattr(struct bio *bp)
1564223089Sgibbs{
1565241485Smav	int ret;
1566223089Sgibbs	struct cam_periph *periph;
1567223089Sgibbs
1568223089Sgibbs	periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1569241485Smav	cam_periph_lock(periph);
1570223089Sgibbs	ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
1571223089Sgibbs	    periph->path);
1572241485Smav	cam_periph_unlock(periph);
1573223089Sgibbs	if (ret == 0)
1574223089Sgibbs		bp->bio_completed = bp->bio_length;
1575223089Sgibbs	return ret;
1576223089Sgibbs}
1577223089Sgibbs
1578298002Simpstatic int
1579298002Simpadadeletemethodsysctl(SYSCTL_HANDLER_ARGS)
1580298002Simp{
1581298002Simp	char buf[16];
1582298002Simp	const char *p;
1583298002Simp	struct ada_softc *softc;
1584298002Simp	int i, error, value, methods;
1585298002Simp
1586298002Simp	softc = (struct ada_softc *)arg1;
1587298002Simp
1588298002Simp	value = softc->delete_method;
1589298002Simp	if (value < 0 || value > ADA_DELETE_MAX)
1590298002Simp		p = "UNKNOWN";
1591298002Simp	else
1592298002Simp		p = ada_delete_method_names[value];
1593298002Simp	strncpy(buf, p, sizeof(buf));
1594298002Simp	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
1595298002Simp	if (error != 0 || req->newptr == NULL)
1596298002Simp		return (error);
1597298002Simp	methods = 1 << ADA_DELETE_DISABLE;
1598298002Simp	if ((softc->flags & ADA_FLAG_CAN_CFA) &&
1599298002Simp	    !(softc->flags & ADA_FLAG_CAN_48BIT))
1600298002Simp		methods |= 1 << ADA_DELETE_CFA_ERASE;
1601298002Simp	if (softc->flags & ADA_FLAG_CAN_TRIM)
1602298002Simp		methods |= 1 << ADA_DELETE_DSM_TRIM;
1603298002Simp	if (softc->flags & ADA_FLAG_CAN_NCQ_TRIM)
1604298002Simp		methods |= 1 << ADA_DELETE_NCQ_DSM_TRIM;
1605298002Simp	for (i = 0; i <= ADA_DELETE_MAX; i++) {
1606298002Simp		if (!(methods & (1 << i)) ||
1607298002Simp		    strcmp(buf, ada_delete_method_names[i]) != 0)
1608298002Simp			continue;
1609298002Simp		softc->delete_method = i;
1610298002Simp		return (0);
1611298002Simp	}
1612298002Simp	return (EINVAL);
1613298002Simp}
1614298002Simp
1615300207Skenstatic void
1616300207Skenadasetflags(struct ada_softc *softc, struct ccb_getdev *cgd)
1617300207Sken{
1618300207Sken	if ((cgd->ident_data.capabilities1 & ATA_SUPPORT_DMA) &&
1619300207Sken	    (cgd->inq_flags & SID_DMA))
1620300207Sken		softc->flags |= ADA_FLAG_CAN_DMA;
1621300207Sken	else
1622300207Sken		softc->flags &= ~ADA_FLAG_CAN_DMA;
1623300207Sken
1624300207Sken	if (cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) {
1625300207Sken		softc->flags |= ADA_FLAG_CAN_48BIT;
1626300207Sken		if (cgd->inq_flags & SID_DMA48)
1627300207Sken			softc->flags |= ADA_FLAG_CAN_DMA48;
1628300207Sken		else
1629300207Sken			softc->flags &= ~ADA_FLAG_CAN_DMA48;
1630300207Sken	} else
1631300207Sken		softc->flags &= ~(ADA_FLAG_CAN_48BIT | ADA_FLAG_CAN_DMA48);
1632300207Sken
1633300207Sken	if (cgd->ident_data.support.command2 & ATA_SUPPORT_FLUSHCACHE)
1634300207Sken		softc->flags |= ADA_FLAG_CAN_FLUSHCACHE;
1635300207Sken	else
1636300207Sken		softc->flags &= ~ADA_FLAG_CAN_FLUSHCACHE;
1637300207Sken
1638300207Sken	if (cgd->ident_data.support.command1 & ATA_SUPPORT_POWERMGT)
1639300207Sken		softc->flags |= ADA_FLAG_CAN_POWERMGT;
1640300207Sken	else
1641300207Sken		softc->flags &= ~ADA_FLAG_CAN_POWERMGT;
1642300207Sken
1643300207Sken	if ((cgd->ident_data.satacapabilities & ATA_SUPPORT_NCQ) &&
1644300207Sken	    (cgd->inq_flags & SID_DMA) && (cgd->inq_flags & SID_CmdQue))
1645300207Sken		softc->flags |= ADA_FLAG_CAN_NCQ;
1646300207Sken	else
1647300207Sken		softc->flags &= ~ADA_FLAG_CAN_NCQ;
1648300207Sken
1649300207Sken	if ((cgd->ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) &&
1650350805Smav	    (cgd->inq_flags & SID_DMA) &&
1651350805Smav	    (softc->quirks & ADA_Q_NO_TRIM) == 0) {
1652300207Sken		softc->flags |= ADA_FLAG_CAN_TRIM;
1653300207Sken		softc->trim_max_ranges = TRIM_MAX_RANGES;
1654300207Sken		if (cgd->ident_data.max_dsm_blocks != 0) {
1655300207Sken			softc->trim_max_ranges =
1656300207Sken			    min(cgd->ident_data.max_dsm_blocks *
1657300207Sken				ATA_DSM_BLK_RANGES, softc->trim_max_ranges);
1658300207Sken		}
1659300207Sken		/*
1660300207Sken		 * If we can do RCVSND_FPDMA_QUEUED commands, we may be able
1661300207Sken		 * to do NCQ trims, if we support trims at all. We also need
1662300207Sken		 * support from the SIM to do things properly. Perhaps we
1663300207Sken		 * should look at log 13 dword 0 bit 0 and dword 1 bit 0 are
1664300207Sken		 * set too...
1665300207Sken		 */
1666300207Sken		if ((softc->quirks & ADA_Q_NCQ_TRIM_BROKEN) == 0 &&
1667300207Sken		    (softc->flags & ADA_FLAG_PIM_ATA_EXT) != 0 &&
1668300207Sken		    (cgd->ident_data.satacapabilities2 &
1669300207Sken		     ATA_SUPPORT_RCVSND_FPDMA_QUEUED) != 0 &&
1670300207Sken		    (softc->flags & ADA_FLAG_CAN_TRIM) != 0)
1671300207Sken			softc->flags |= ADA_FLAG_CAN_NCQ_TRIM;
1672300207Sken		else
1673300207Sken			softc->flags &= ~ADA_FLAG_CAN_NCQ_TRIM;
1674300207Sken	} else
1675300207Sken		softc->flags &= ~(ADA_FLAG_CAN_TRIM | ADA_FLAG_CAN_NCQ_TRIM);
1676300207Sken
1677300207Sken	if (cgd->ident_data.support.command2 & ATA_SUPPORT_CFA)
1678300207Sken		softc->flags |= ADA_FLAG_CAN_CFA;
1679300207Sken	else
1680300207Sken		softc->flags &= ~ADA_FLAG_CAN_CFA;
1681300207Sken
1682300207Sken	/*
1683300207Sken	 * Now that we've set the appropriate flags, setup the delete
1684300207Sken	 * method.
1685300207Sken	 */
1686300207Sken	adasetdeletemethod(softc);
1687300207Sken
1688300640Sken	if ((cgd->ident_data.support.extension & ATA_SUPPORT_GENLOG)
1689300640Sken	 && ((softc->quirks & ADA_Q_LOG_BROKEN) == 0))
1690300207Sken		softc->flags |= ADA_FLAG_CAN_LOG;
1691300207Sken	else
1692300207Sken		softc->flags &= ~ADA_FLAG_CAN_LOG;
1693300207Sken
1694300207Sken	if ((cgd->ident_data.support3 & ATA_SUPPORT_ZONE_MASK) ==
1695300207Sken	     ATA_SUPPORT_ZONE_HOST_AWARE)
1696300207Sken		softc->zone_mode = ADA_ZONE_HOST_AWARE;
1697300640Sken	else if (((cgd->ident_data.support3 & ATA_SUPPORT_ZONE_MASK) ==
1698300640Sken		   ATA_SUPPORT_ZONE_DEV_MANAGED)
1699300640Sken	      || (softc->quirks & ADA_Q_SMR_DM))
1700300207Sken		softc->zone_mode = ADA_ZONE_DRIVE_MANAGED;
1701300207Sken	else
1702300207Sken		softc->zone_mode = ADA_ZONE_NONE;
1703300207Sken
1704300207Sken	if (cgd->ident_data.support.command1 & ATA_SUPPORT_LOOKAHEAD)
1705300207Sken		softc->flags |= ADA_FLAG_CAN_RAHEAD;
1706300207Sken	else
1707300207Sken		softc->flags &= ~ADA_FLAG_CAN_RAHEAD;
1708300207Sken
1709300207Sken	if (cgd->ident_data.support.command1 & ATA_SUPPORT_WRITECACHE)
1710300207Sken		softc->flags |= ADA_FLAG_CAN_WCACHE;
1711300207Sken	else
1712300207Sken		softc->flags &= ~ADA_FLAG_CAN_WCACHE;
1713300207Sken}
1714300207Sken
1715195534Sscottlstatic cam_status
1716195534Sscottladaregister(struct cam_periph *periph, void *arg)
1717195534Sscottl{
1718195534Sscottl	struct ada_softc *softc;
1719195534Sscottl	struct ccb_getdev *cgd;
1720289137Smav	char   announce_buf[80];
1721195534Sscottl	struct disk_params *dp;
1722195534Sscottl	caddr_t match;
1723289137Smav	int quirks;
1724195534Sscottl
1725195534Sscottl	cgd = (struct ccb_getdev *)arg;
1726195534Sscottl	if (cgd == NULL) {
1727195534Sscottl		printf("adaregister: no getdev CCB, can't register device\n");
1728195534Sscottl		return(CAM_REQ_CMP_ERR);
1729195534Sscottl	}
1730195534Sscottl
1731195534Sscottl	softc = (struct ada_softc *)malloc(sizeof(*softc), M_DEVBUF,
1732195534Sscottl	    M_NOWAIT|M_ZERO);
1733195534Sscottl
1734195534Sscottl	if (softc == NULL) {
1735195534Sscottl		printf("adaregister: Unable to probe new device. "
1736198328Smav		    "Unable to allocate softc\n");
1737195534Sscottl		return(CAM_REQ_CMP_ERR);
1738195534Sscottl	}
1739195534Sscottl
1740298002Simp	if (cam_iosched_init(&softc->cam_iosched, periph) != 0) {
1741298002Simp		printf("adaregister: Unable to probe new device. "
1742298002Simp		       "Unable to allocate iosched memory\n");
1743301568Scem		free(softc, M_DEVBUF);
1744298002Simp		return(CAM_REQ_CMP_ERR);
1745298002Simp	}
1746195534Sscottl
1747195534Sscottl	periph->softc = softc;
1748350805Smav	xpt_path_inq(&softc->cpi, periph->path);
1749195534Sscottl
1750195534Sscottl	/*
1751195534Sscottl	 * See if this device has any quirks.
1752195534Sscottl	 */
1753199178Smav	match = cam_quirkmatch((caddr_t)&cgd->ident_data,
1754199178Smav			       (caddr_t)ada_quirk_table,
1755298431Spfg			       nitems(ada_quirk_table),
1756199178Smav			       sizeof(*ada_quirk_table), ata_identify_match);
1757195534Sscottl	if (match != NULL)
1758195534Sscottl		softc->quirks = ((struct ada_quirk_entry *)match)->quirks;
1759195534Sscottl	else
1760195534Sscottl		softc->quirks = ADA_Q_NONE;
1761195534Sscottl
1762195534Sscottl	TASK_INIT(&softc->sysctl_task, 0, adasysctlinit, periph);
1763195534Sscottl
1764195534Sscottl	/*
1765195534Sscottl	 * Register this media as a disk
1766195534Sscottl	 */
1767220618Smav	(void)cam_periph_hold(periph, PRIBIO);
1768249106Smav	cam_periph_unlock(periph);
1769222520Smav	snprintf(announce_buf, sizeof(announce_buf),
1770222520Smav	    "kern.cam.ada.%d.quirks", periph->unit_number);
1771222520Smav	quirks = softc->quirks;
1772222520Smav	TUNABLE_INT_FETCH(announce_buf, &quirks);
1773222520Smav	softc->quirks = quirks;
1774224497Smav	softc->read_ahead = -1;
1775224497Smav	snprintf(announce_buf, sizeof(announce_buf),
1776224497Smav	    "kern.cam.ada.%d.read_ahead", periph->unit_number);
1777224497Smav	TUNABLE_INT_FETCH(announce_buf, &softc->read_ahead);
1778220618Smav	softc->write_cache = -1;
1779220618Smav	snprintf(announce_buf, sizeof(announce_buf),
1780220618Smav	    "kern.cam.ada.%d.write_cache", periph->unit_number);
1781220618Smav	TUNABLE_INT_FETCH(announce_buf, &softc->write_cache);
1782300532Sken
1783300532Sken	/*
1784300532Sken	 * Set support flags based on the Identify data and quirks.
1785300532Sken	 */
1786300532Sken	adasetflags(softc, cgd);
1787350805Smav	if (softc->cpi.hba_misc & PIM_ATA_EXT)
1788350805Smav		softc->flags |= ADA_FLAG_PIM_ATA_EXT;
1789300532Sken
1790250033Ssmh	/* Disable queue sorting for non-rotational media by default. */
1791298002Simp	if (cgd->ident_data.media_rotation_rate == ATA_RATE_NON_ROTATING) {
1792298002Simp		softc->rotating = 0;
1793298002Simp	} else {
1794298002Simp		softc->rotating = 1;
1795298002Simp	}
1796298002Simp	cam_iosched_set_sort_queue(softc->cam_iosched,  softc->rotating ? -1 : 0);
1797195534Sscottl	softc->disk = disk_alloc();
1798350805Smav	adasetgeom(softc, cgd);
1799220644Smav	softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
1800220644Smav			  periph->unit_number, softc->params.secsize,
1801220644Smav			  DEVSTAT_ALL_SUPPORTED,
1802220644Smav			  DEVSTAT_TYPE_DIRECT |
1803350805Smav			  XPORT_DEVSTAT_TYPE(softc->cpi.transport),
1804220644Smav			  DEVSTAT_PRIORITY_DISK);
1805195534Sscottl	softc->disk->d_open = adaopen;
1806195534Sscottl	softc->disk->d_close = adaclose;
1807195534Sscottl	softc->disk->d_strategy = adastrategy;
1808223089Sgibbs	softc->disk->d_getattr = adagetattr;
1809195534Sscottl	softc->disk->d_dump = adadump;
1810249347Sken	softc->disk->d_gone = adadiskgonecb;
1811195534Sscottl	softc->disk->d_name = "ada";
1812195534Sscottl	softc->disk->d_drv1 = periph;
1813195534Sscottl	softc->disk->d_unit = periph->unit_number;
1814195534Sscottl
1815249347Sken	/*
1816249347Sken	 * Acquire a reference to the periph before we register with GEOM.
1817249347Sken	 * We'll release this reference once GEOM calls us back (via
1818249347Sken	 * adadiskgonecb()) telling us that our provider has been freed.
1819249347Sken	 */
1820249347Sken	if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
1821249347Sken		xpt_print(periph->path, "%s: lost periph during "
1822249347Sken			  "registration!\n", __func__);
1823249347Sken		cam_periph_lock(periph);
1824249347Sken		return (CAM_REQ_CMP_ERR);
1825249347Sken	}
1826195534Sscottl	disk_create(softc->disk, DISK_VERSION);
1827249106Smav	cam_periph_lock(periph);
1828195534Sscottl
1829195534Sscottl	dp = &softc->params;
1830195534Sscottl	snprintf(announce_buf, sizeof(announce_buf),
1831289138Smav	    "%juMB (%ju %u byte sectors)",
1832289138Smav	    ((uintmax_t)dp->secsize * dp->sectors) / (1024 * 1024),
1833289138Smav	    (uintmax_t)dp->sectors, dp->secsize);
1834195534Sscottl	xpt_announce_periph(periph, announce_buf);
1835250792Ssmh	xpt_announce_quirks(periph, softc->quirks, ADA_Q_BIT_STRING);
1836220454Smav
1837195534Sscottl	/*
1838220454Smav	 * Create our sysctl variables, now that we know
1839220454Smav	 * we have successfully attached.
1840220454Smav	 */
1841256843Smav	if (cam_periph_acquire(periph) == CAM_REQ_CMP)
1842256843Smav		taskqueue_enqueue(taskqueue_thread, &softc->sysctl_task);
1843220454Smav
1844220454Smav	/*
1845195534Sscottl	 * Add async callbacks for bus reset and
1846195534Sscottl	 * bus device reset calls.  I don't bother
1847195534Sscottl	 * checking if this fails as, in most cases,
1848195534Sscottl	 * the system will function just fine without
1849195534Sscottl	 * them and the only alternative would be to
1850195534Sscottl	 * not attach the device on failure.
1851195534Sscottl	 */
1852235897Smav	xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
1853236393Smav	    AC_GETDEV_CHANGED | AC_ADVINFO_CHANGED,
1854236393Smav	    adaasync, periph, periph->path);
1855195534Sscottl
1856195534Sscottl	/*
1857195534Sscottl	 * Schedule a periodic event to occasionally send an
1858195534Sscottl	 * ordered tag to a device.
1859195534Sscottl	 */
1860256843Smav	callout_init_mtx(&softc->sendordered_c, cam_periph_mtx(periph), 0);
1861195534Sscottl	callout_reset(&softc->sendordered_c,
1862230921Smav	    (ada_default_timeout * hz) / ADA_ORDEREDTAG_INTERVAL,
1863195534Sscottl	    adasendorderedtag, softc);
1864195534Sscottl
1865300207Sken	if (ADA_RA >= 0 && softc->flags & ADA_FLAG_CAN_RAHEAD) {
1866224497Smav		softc->state = ADA_STATE_RAHEAD;
1867300207Sken	} else if (ADA_WC >= 0 && softc->flags & ADA_FLAG_CAN_WCACHE) {
1868220412Smav		softc->state = ADA_STATE_WCACHE;
1869300640Sken	} else if ((softc->flags & ADA_FLAG_CAN_LOG)
1870300640Sken		&& (softc->zone_mode != ADA_ZONE_NONE)) {
1871300207Sken		softc->state = ADA_STATE_LOGDIR;
1872256843Smav	} else {
1873300207Sken		/*
1874300207Sken		 * Nothing to probe, so we can just transition to the
1875300207Sken		 * normal state.
1876300207Sken		 */
1877300207Sken		adaprobedone(periph, NULL);
1878256843Smav		return(CAM_REQ_CMP);
1879256843Smav	}
1880300207Sken
1881300207Sken	xpt_schedule(periph, CAM_PRIORITY_DEV);
1882300207Sken
1883195534Sscottl	return(CAM_REQ_CMP);
1884195534Sscottl}
1885195534Sscottl
1886298002Simpstatic int
1887298002Simpada_dsmtrim_req_create(struct ada_softc *softc, struct bio *bp, struct trim_request *req)
1888268205Simp{
1889268205Simp	uint64_t lastlba = (uint64_t)-1;
1890268205Simp	int c, lastcount = 0, off, ranges = 0;
1891268205Simp
1892268205Simp	bzero(req, sizeof(*req));
1893268205Simp	TAILQ_INIT(&req->bps);
1894268205Simp	do {
1895268205Simp		uint64_t lba = bp->bio_pblkno;
1896268205Simp		int count = bp->bio_bcount / softc->params.secsize;
1897268205Simp
1898268205Simp		/* Try to extend the previous range. */
1899268205Simp		if (lba == lastlba) {
1900268205Simp			c = min(count, ATA_DSM_RANGE_MAX - lastcount);
1901268205Simp			lastcount += c;
1902268205Simp			off = (ranges - 1) * ATA_DSM_RANGE_SIZE;
1903268205Simp			req->data[off + 6] = lastcount & 0xff;
1904268205Simp			req->data[off + 7] =
1905268205Simp				(lastcount >> 8) & 0xff;
1906268205Simp			count -= c;
1907268205Simp			lba += c;
1908268205Simp		}
1909268205Simp
1910268205Simp		while (count > 0) {
1911268205Simp			c = min(count, ATA_DSM_RANGE_MAX);
1912268205Simp			off = ranges * ATA_DSM_RANGE_SIZE;
1913268205Simp			req->data[off + 0] = lba & 0xff;
1914268205Simp			req->data[off + 1] = (lba >> 8) & 0xff;
1915268205Simp			req->data[off + 2] = (lba >> 16) & 0xff;
1916268205Simp			req->data[off + 3] = (lba >> 24) & 0xff;
1917268205Simp			req->data[off + 4] = (lba >> 32) & 0xff;
1918268205Simp			req->data[off + 5] = (lba >> 40) & 0xff;
1919268205Simp			req->data[off + 6] = c & 0xff;
1920268205Simp			req->data[off + 7] = (c >> 8) & 0xff;
1921268205Simp			lba += c;
1922268205Simp			count -= c;
1923268205Simp			lastcount = c;
1924268205Simp			ranges++;
1925268205Simp			/*
1926268205Simp			 * Its the caller's responsibility to ensure the
1927268205Simp			 * request will fit so we don't need to check for
1928268205Simp			 * overrun here
1929268205Simp			 */
1930268205Simp		}
1931268205Simp		lastlba = lba;
1932268205Simp		TAILQ_INSERT_TAIL(&req->bps, bp, bio_queue);
1933298002Simp
1934298002Simp		bp = cam_iosched_next_trim(softc->cam_iosched);
1935298002Simp		if (bp == NULL)
1936268205Simp			break;
1937298002Simp		if (bp->bio_bcount / softc->params.secsize >
1938298002Simp		    (softc->trim_max_ranges - ranges) * ATA_DSM_RANGE_MAX) {
1939298002Simp			cam_iosched_put_back_trim(softc->cam_iosched, bp);
1940298002Simp			break;
1941298002Simp		}
1942268205Simp	} while (1);
1943298002Simp
1944298002Simp	return (ranges);
1945298002Simp}
1946298002Simp
1947298002Simpstatic void
1948298002Simpada_dsmtrim(struct ada_softc *softc, struct bio *bp, struct ccb_ataio *ataio)
1949298002Simp{
1950298002Simp	struct trim_request *req = &softc->trim_req;
1951298002Simp	int ranges;
1952298002Simp
1953298002Simp	ranges = ada_dsmtrim_req_create(softc, bp, req);
1954268205Simp	cam_fill_ataio(ataio,
1955268205Simp	    ada_retry_count,
1956268205Simp	    adadone,
1957268205Simp	    CAM_DIR_OUT,
1958268205Simp	    0,
1959268205Simp	    req->data,
1960298649Spfg	    howmany(ranges, ATA_DSM_BLK_RANGES) * ATA_DSM_BLK_SIZE,
1961268205Simp	    ada_default_timeout * 1000);
1962268205Simp	ata_48bit_cmd(ataio, ATA_DATA_SET_MANAGEMENT,
1963298649Spfg	    ATA_DSM_TRIM, 0, howmany(ranges, ATA_DSM_BLK_RANGES));
1964268205Simp}
1965268205Simp
1966268205Simpstatic void
1967298002Simpada_ncq_dsmtrim(struct ada_softc *softc, struct bio *bp, struct ccb_ataio *ataio)
1968298002Simp{
1969298002Simp	struct trim_request *req = &softc->trim_req;
1970298002Simp	int ranges;
1971298002Simp
1972298002Simp	ranges = ada_dsmtrim_req_create(softc, bp, req);
1973298002Simp	cam_fill_ataio(ataio,
1974298002Simp	    ada_retry_count,
1975298002Simp	    adadone,
1976298002Simp	    CAM_DIR_OUT,
1977298002Simp	    0,
1978298002Simp	    req->data,
1979298649Spfg	    howmany(ranges, ATA_DSM_BLK_RANGES) * ATA_DSM_BLK_SIZE,
1980298002Simp	    ada_default_timeout * 1000);
1981298002Simp	ata_ncq_cmd(ataio,
1982298002Simp	    ATA_SEND_FPDMA_QUEUED,
1983298002Simp	    0,
1984298649Spfg	    howmany(ranges, ATA_DSM_BLK_RANGES));
1985298002Simp	ataio->cmd.sector_count_exp = ATA_SFPDMA_DSM;
1986298143Simp	ataio->ata_flags |= ATA_FLAG_AUX;
1987298143Simp	ataio->aux = 1;
1988298002Simp}
1989298002Simp
1990298002Simpstatic void
1991268205Simpada_cfaerase(struct ada_softc *softc, struct bio *bp, struct ccb_ataio *ataio)
1992268205Simp{
1993273704Ssmh	struct trim_request *req = &softc->trim_req;
1994268205Simp	uint64_t lba = bp->bio_pblkno;
1995268205Simp	uint16_t count = bp->bio_bcount / softc->params.secsize;
1996268205Simp
1997273704Ssmh	bzero(req, sizeof(*req));
1998273704Ssmh	TAILQ_INIT(&req->bps);
1999273704Ssmh	TAILQ_INSERT_TAIL(&req->bps, bp, bio_queue);
2000273704Ssmh
2001268205Simp	cam_fill_ataio(ataio,
2002268205Simp	    ada_retry_count,
2003268205Simp	    adadone,
2004268205Simp	    CAM_DIR_NONE,
2005268205Simp	    0,
2006268205Simp	    NULL,
2007268205Simp	    0,
2008268205Simp	    ada_default_timeout*1000);
2009268205Simp
2010268205Simp	if (count >= 256)
2011268205Simp		count = 0;
2012268205Simp	ata_28bit_cmd(ataio, ATA_CFA_ERASE, 0, lba, count);
2013268205Simp}
2014268205Simp
2015300207Skenstatic int
2016300207Skenada_zone_bio_to_ata(int disk_zone_cmd)
2017300207Sken{
2018300207Sken	switch (disk_zone_cmd) {
2019300207Sken	case DISK_ZONE_OPEN:
2020300207Sken		return ATA_ZM_OPEN_ZONE;
2021300207Sken	case DISK_ZONE_CLOSE:
2022300207Sken		return ATA_ZM_CLOSE_ZONE;
2023300207Sken	case DISK_ZONE_FINISH:
2024300207Sken		return ATA_ZM_FINISH_ZONE;
2025300207Sken	case DISK_ZONE_RWP:
2026300207Sken		return ATA_ZM_RWP;
2027300207Sken	}
2028300207Sken
2029300207Sken	return -1;
2030300207Sken}
2031300207Sken
2032300207Skenstatic int
2033300207Skenada_zone_cmd(struct cam_periph *periph, union ccb *ccb, struct bio *bp,
2034300207Sken	     int *queue_ccb)
2035300207Sken{
2036300207Sken	struct ada_softc *softc;
2037300207Sken	int error;
2038300207Sken
2039300207Sken	error = 0;
2040300207Sken
2041300207Sken	if (bp->bio_cmd != BIO_ZONE) {
2042300207Sken		error = EINVAL;
2043300207Sken		goto bailout;
2044300207Sken	}
2045300207Sken
2046300207Sken	softc = periph->softc;
2047300207Sken
2048300207Sken	switch (bp->bio_zone.zone_cmd) {
2049300207Sken	case DISK_ZONE_OPEN:
2050300207Sken	case DISK_ZONE_CLOSE:
2051300207Sken	case DISK_ZONE_FINISH:
2052300207Sken	case DISK_ZONE_RWP: {
2053300207Sken		int zone_flags;
2054300207Sken		int zone_sa;
2055300207Sken		uint64_t lba;
2056300207Sken
2057300207Sken		zone_sa = ada_zone_bio_to_ata(bp->bio_zone.zone_cmd);
2058300207Sken		if (zone_sa == -1) {
2059300207Sken			xpt_print(periph->path, "Cannot translate zone "
2060300207Sken			    "cmd %#x to ATA\n", bp->bio_zone.zone_cmd);
2061300207Sken			error = EINVAL;
2062300207Sken			goto bailout;
2063300207Sken		}
2064300207Sken
2065300207Sken		zone_flags = 0;
2066300207Sken		lba = bp->bio_zone.zone_params.rwp.id;
2067300207Sken
2068300207Sken		if (bp->bio_zone.zone_params.rwp.flags &
2069300207Sken		    DISK_ZONE_RWP_FLAG_ALL)
2070300207Sken			zone_flags |= ZBC_OUT_ALL;
2071300207Sken
2072300207Sken		ata_zac_mgmt_out(&ccb->ataio,
2073300207Sken				 /*retries*/ ada_retry_count,
2074300207Sken				 /*cbfcnp*/ adadone,
2075300207Sken				 /*use_ncq*/ (softc->flags &
2076300207Sken					      ADA_FLAG_PIM_ATA_EXT) ? 1 : 0,
2077300207Sken				 /*zm_action*/ zone_sa,
2078300207Sken				 /*zone_id*/ lba,
2079300207Sken				 /*zone_flags*/ zone_flags,
2080300207Sken				 /*sector_count*/ 0,
2081300207Sken				 /*data_ptr*/ NULL,
2082300207Sken				 /*dxfer_len*/ 0,
2083300207Sken				 /*timeout*/ ada_default_timeout * 1000);
2084300207Sken		*queue_ccb = 1;
2085300207Sken
2086300207Sken		break;
2087300207Sken	}
2088300207Sken	case DISK_ZONE_REPORT_ZONES: {
2089300207Sken		uint8_t *rz_ptr;
2090300207Sken		uint32_t num_entries, alloc_size;
2091300207Sken		struct disk_zone_report *rep;
2092300207Sken
2093300207Sken		rep = &bp->bio_zone.zone_params.report;
2094300207Sken
2095300207Sken		num_entries = rep->entries_allocated;
2096300207Sken		if (num_entries == 0) {
2097300207Sken			xpt_print(periph->path, "No entries allocated for "
2098300207Sken			    "Report Zones request\n");
2099300207Sken			error = EINVAL;
2100300207Sken			goto bailout;
2101300207Sken		}
2102300207Sken		alloc_size = sizeof(struct scsi_report_zones_hdr) +
2103300207Sken		    (sizeof(struct scsi_report_zones_desc) * num_entries);
2104300207Sken		alloc_size = min(alloc_size, softc->disk->d_maxsize);
2105300207Sken		rz_ptr = malloc(alloc_size, M_ATADA, M_NOWAIT | M_ZERO);
2106300207Sken		if (rz_ptr == NULL) {
2107300207Sken			xpt_print(periph->path, "Unable to allocate memory "
2108300207Sken			   "for Report Zones request\n");
2109300207Sken			error = ENOMEM;
2110300207Sken			goto bailout;
2111300207Sken		}
2112300207Sken
2113300207Sken		ata_zac_mgmt_in(&ccb->ataio,
2114300207Sken				/*retries*/ ada_retry_count,
2115300207Sken				/*cbcfnp*/ adadone,
2116300207Sken				/*use_ncq*/ (softc->flags &
2117300207Sken					     ADA_FLAG_PIM_ATA_EXT) ? 1 : 0,
2118300207Sken				/*zm_action*/ ATA_ZM_REPORT_ZONES,
2119300207Sken				/*zone_id*/ rep->starting_id,
2120300207Sken				/*zone_flags*/ rep->rep_options,
2121300207Sken				/*data_ptr*/ rz_ptr,
2122300207Sken				/*dxfer_len*/ alloc_size,
2123300207Sken				/*timeout*/ ada_default_timeout * 1000);
2124300207Sken
2125300207Sken		/*
2126300207Sken		 * For BIO_ZONE, this isn't normally needed.  However, it
2127300207Sken		 * is used by devstat_end_transaction_bio() to determine
2128300207Sken		 * how much data was transferred.
2129300207Sken		 */
2130300207Sken		/*
2131300207Sken		 * XXX KDM we have a problem.  But I'm not sure how to fix
2132300207Sken		 * it.  devstat uses bio_bcount - bio_resid to calculate
2133300207Sken		 * the amount of data transferred.   The GEOM disk code
2134300207Sken		 * uses bio_length - bio_resid to calculate the amount of
2135300207Sken		 * data in bio_completed.  We have different structure
2136300207Sken		 * sizes above and below the ada(4) driver.  So, if we
2137300207Sken		 * use the sizes above, the amount transferred won't be
2138300207Sken		 * quite accurate for devstat.  If we use different sizes
2139300207Sken		 * for bio_bcount and bio_length (above and below
2140300207Sken		 * respectively), then the residual needs to match one or
2141300207Sken		 * the other.  Everything is calculated after the bio
2142300207Sken		 * leaves the driver, so changing the values around isn't
2143300207Sken		 * really an option.  For now, just set the count to the
2144300207Sken		 * passed in length.  This means that the calculations
2145300207Sken		 * above (e.g. bio_completed) will be correct, but the
2146300207Sken		 * amount of data reported to devstat will be slightly
2147300207Sken		 * under or overstated.
2148300207Sken		 */
2149300207Sken		bp->bio_bcount = bp->bio_length;
2150300207Sken
2151300207Sken		*queue_ccb = 1;
2152300207Sken
2153300207Sken		break;
2154300207Sken	}
2155300207Sken	case DISK_ZONE_GET_PARAMS: {
2156300207Sken		struct disk_zone_disk_params *params;
2157300207Sken
2158300207Sken		params = &bp->bio_zone.zone_params.disk_params;
2159300207Sken		bzero(params, sizeof(*params));
2160300207Sken
2161300207Sken		switch (softc->zone_mode) {
2162300207Sken		case ADA_ZONE_DRIVE_MANAGED:
2163300207Sken			params->zone_mode = DISK_ZONE_MODE_DRIVE_MANAGED;
2164300207Sken			break;
2165300207Sken		case ADA_ZONE_HOST_AWARE:
2166300207Sken			params->zone_mode = DISK_ZONE_MODE_HOST_AWARE;
2167300207Sken			break;
2168300207Sken		case ADA_ZONE_HOST_MANAGED:
2169300207Sken			params->zone_mode = DISK_ZONE_MODE_HOST_MANAGED;
2170300207Sken			break;
2171300207Sken		default:
2172300207Sken		case ADA_ZONE_NONE:
2173300207Sken			params->zone_mode = DISK_ZONE_MODE_NONE;
2174300207Sken			break;
2175300207Sken		}
2176300207Sken
2177300207Sken		if (softc->zone_flags & ADA_ZONE_FLAG_URSWRZ)
2178300207Sken			params->flags |= DISK_ZONE_DISK_URSWRZ;
2179300207Sken
2180300207Sken		if (softc->zone_flags & ADA_ZONE_FLAG_OPT_SEQ_SET) {
2181300207Sken			params->optimal_seq_zones = softc->optimal_seq_zones;
2182300207Sken			params->flags |= DISK_ZONE_OPT_SEQ_SET;
2183300207Sken		}
2184300207Sken
2185300207Sken		if (softc->zone_flags & ADA_ZONE_FLAG_OPT_NONSEQ_SET) {
2186300207Sken			params->optimal_nonseq_zones =
2187300207Sken			    softc->optimal_nonseq_zones;
2188300207Sken			params->flags |= DISK_ZONE_OPT_NONSEQ_SET;
2189300207Sken		}
2190300207Sken
2191300207Sken		if (softc->zone_flags & ADA_ZONE_FLAG_MAX_SEQ_SET) {
2192300207Sken			params->max_seq_zones = softc->max_seq_zones;
2193300207Sken			params->flags |= DISK_ZONE_MAX_SEQ_SET;
2194300207Sken		}
2195300207Sken		if (softc->zone_flags & ADA_ZONE_FLAG_RZ_SUP)
2196300207Sken			params->flags |= DISK_ZONE_RZ_SUP;
2197300207Sken
2198300207Sken		if (softc->zone_flags & ADA_ZONE_FLAG_OPEN_SUP)
2199300207Sken			params->flags |= DISK_ZONE_OPEN_SUP;
2200300207Sken
2201300207Sken		if (softc->zone_flags & ADA_ZONE_FLAG_CLOSE_SUP)
2202300207Sken			params->flags |= DISK_ZONE_CLOSE_SUP;
2203300207Sken
2204300207Sken		if (softc->zone_flags & ADA_ZONE_FLAG_FINISH_SUP)
2205300207Sken			params->flags |= DISK_ZONE_FINISH_SUP;
2206300207Sken
2207300207Sken		if (softc->zone_flags & ADA_ZONE_FLAG_RWP_SUP)
2208300207Sken			params->flags |= DISK_ZONE_RWP_SUP;
2209300207Sken		break;
2210300207Sken	}
2211300207Sken	default:
2212300207Sken		break;
2213300207Sken	}
2214300207Skenbailout:
2215300207Sken	return (error);
2216300207Sken}
2217300207Sken
2218268205Simpstatic void
2219195534Sscottladastart(struct cam_periph *periph, union ccb *start_ccb)
2220195534Sscottl{
2221198328Smav	struct ada_softc *softc = (struct ada_softc *)periph->softc;
2222198328Smav	struct ccb_ataio *ataio = &start_ccb->ataio;
2223195534Sscottl
2224236602Smav	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adastart\n"));
2225236602Smav
2226195534Sscottl	switch (softc->state) {
2227195534Sscottl	case ADA_STATE_NORMAL:
2228195534Sscottl	{
2229195534Sscottl		struct bio *bp;
2230201139Smav		u_int8_t tag_code;
2231195534Sscottl
2232298002Simp		bp = cam_iosched_next_bio(softc->cam_iosched);
2233201139Smav		if (bp == NULL) {
2234195534Sscottl			xpt_release_ccb(start_ccb);
2235201139Smav			break;
2236201139Smav		}
2237201139Smav
2238298002Simp		if ((bp->bio_flags & BIO_ORDERED) != 0 ||
2239298002Simp		    (bp->bio_cmd != BIO_DELETE && (softc->flags & ADA_FLAG_NEED_OTAG) != 0)) {
2240201139Smav			softc->flags &= ~ADA_FLAG_NEED_OTAG;
2241257054Smav			softc->flags |= ADA_FLAG_WAS_OTAG;
2242201139Smav			tag_code = 0;
2243195534Sscottl		} else {
2244201139Smav			tag_code = 1;
2245201139Smav		}
2246201139Smav		switch (bp->bio_cmd) {
2247253724Smav		case BIO_WRITE:
2248201139Smav		case BIO_READ:
2249201139Smav		{
2250201139Smav			uint64_t lba = bp->bio_pblkno;
2251201139Smav			uint16_t count = bp->bio_bcount / softc->params.secsize;
2252291716Sken			void *data_ptr;
2253291716Sken			int rw_op;
2254291716Sken
2255291716Sken			if (bp->bio_cmd == BIO_WRITE) {
2256291716Sken				softc->flags |= ADA_FLAG_DIRTY;
2257291716Sken				rw_op = CAM_DIR_OUT;
2258291716Sken			} else {
2259291716Sken				rw_op = CAM_DIR_IN;
2260291716Sken			}
2261291716Sken
2262291716Sken			data_ptr = bp->bio_data;
2263291716Sken			if ((bp->bio_flags & (BIO_UNMAPPED|BIO_VLIST)) != 0) {
2264291716Sken				rw_op |= CAM_DATA_BIO;
2265291716Sken				data_ptr = bp;
2266291716Sken			}
2267291716Sken
2268220454Smav#ifdef ADA_TEST_FAILURE
2269220454Smav			int fail = 0;
2270195534Sscottl
2271220454Smav			/*
2272220454Smav			 * Support the failure ioctls.  If the command is a
2273220454Smav			 * read, and there are pending forced read errors, or
2274220454Smav			 * if a write and pending write errors, then fail this
2275220454Smav			 * operation with EIO.  This is useful for testing
2276220454Smav			 * purposes.  Also, support having every Nth read fail.
2277220454Smav			 *
2278220454Smav			 * This is a rather blunt tool.
2279220454Smav			 */
2280220454Smav			if (bp->bio_cmd == BIO_READ) {
2281220454Smav				if (softc->force_read_error) {
2282220454Smav					softc->force_read_error--;
2283220454Smav					fail = 1;
2284220454Smav				}
2285220454Smav				if (softc->periodic_read_error > 0) {
2286220454Smav					if (++softc->periodic_read_count >=
2287220454Smav					    softc->periodic_read_error) {
2288220454Smav						softc->periodic_read_count = 0;
2289220454Smav						fail = 1;
2290220454Smav					}
2291220454Smav				}
2292220454Smav			} else {
2293220454Smav				if (softc->force_write_error) {
2294220454Smav					softc->force_write_error--;
2295220454Smav					fail = 1;
2296220454Smav				}
2297220454Smav			}
2298220454Smav			if (fail) {
2299287025Smav				biofinish(bp, NULL, EIO);
2300220454Smav				xpt_release_ccb(start_ccb);
2301220454Smav				adaschedule(periph);
2302220454Smav				return;
2303220454Smav			}
2304220454Smav#endif
2305248519Skib			KASSERT((bp->bio_flags & BIO_UNMAPPED) == 0 ||
2306248519Skib			    round_page(bp->bio_bcount + bp->bio_ma_offset) /
2307248519Skib			    PAGE_SIZE == bp->bio_ma_n,
2308248519Skib			    ("Short bio %p", bp));
2309201139Smav			cam_fill_ataio(ataio,
2310201139Smav			    ada_retry_count,
2311201139Smav			    adadone,
2312291716Sken			    rw_op,
2313298142Simp			    0,
2314291716Sken			    data_ptr,
2315201139Smav			    bp->bio_bcount,
2316201139Smav			    ada_default_timeout*1000);
2317195534Sscottl
2318201139Smav			if ((softc->flags & ADA_FLAG_CAN_NCQ) && tag_code) {
2319201139Smav				if (bp->bio_cmd == BIO_READ) {
2320201139Smav					ata_ncq_cmd(ataio, ATA_READ_FPDMA_QUEUED,
2321201139Smav					    lba, count);
2322201139Smav				} else {
2323201139Smav					ata_ncq_cmd(ataio, ATA_WRITE_FPDMA_QUEUED,
2324201139Smav					    lba, count);
2325201139Smav				}
2326201139Smav			} else if ((softc->flags & ADA_FLAG_CAN_48BIT) &&
2327201139Smav			    (lba + count >= ATA_MAX_28BIT_LBA ||
2328201139Smav			    count > 256)) {
2329249199Smarius				if (softc->flags & ADA_FLAG_CAN_DMA48) {
2330195534Sscottl					if (bp->bio_cmd == BIO_READ) {
2331201139Smav						ata_48bit_cmd(ataio, ATA_READ_DMA48,
2332201139Smav						    0, lba, count);
2333195534Sscottl					} else {
2334201139Smav						ata_48bit_cmd(ataio, ATA_WRITE_DMA48,
2335201139Smav						    0, lba, count);
2336195534Sscottl					}
2337201139Smav				} else {
2338201139Smav					if (bp->bio_cmd == BIO_READ) {
2339201139Smav						ata_48bit_cmd(ataio, ATA_READ_MUL48,
2340201139Smav						    0, lba, count);
2341195534Sscottl					} else {
2342201139Smav						ata_48bit_cmd(ataio, ATA_WRITE_MUL48,
2343201139Smav						    0, lba, count);
2344195534Sscottl					}
2345201139Smav				}
2346201139Smav			} else {
2347201139Smav				if (count == 256)
2348201139Smav					count = 0;
2349201139Smav				if (softc->flags & ADA_FLAG_CAN_DMA) {
2350201139Smav					if (bp->bio_cmd == BIO_READ) {
2351201139Smav						ata_28bit_cmd(ataio, ATA_READ_DMA,
2352201139Smav						    0, lba, count);
2353201139Smav					} else {
2354201139Smav						ata_28bit_cmd(ataio, ATA_WRITE_DMA,
2355201139Smav						    0, lba, count);
2356201139Smav					}
2357195534Sscottl				} else {
2358201139Smav					if (bp->bio_cmd == BIO_READ) {
2359201139Smav						ata_28bit_cmd(ataio, ATA_READ_MUL,
2360201139Smav						    0, lba, count);
2361195534Sscottl					} else {
2362201139Smav						ata_28bit_cmd(ataio, ATA_WRITE_MUL,
2363201139Smav						    0, lba, count);
2364195534Sscottl					}
2365195534Sscottl				}
2366195534Sscottl			}
2367201139Smav			break;
2368201139Smav		}
2369298002Simp		case BIO_DELETE:
2370298002Simp			switch (softc->delete_method) {
2371298002Simp			case ADA_DELETE_NCQ_DSM_TRIM:
2372298002Simp				ada_ncq_dsmtrim(softc, bp, ataio);
2373298002Simp				break;
2374298002Simp			case ADA_DELETE_DSM_TRIM:
2375298002Simp				ada_dsmtrim(softc, bp, ataio);
2376298002Simp				break;
2377298002Simp			case ADA_DELETE_CFA_ERASE:
2378298002Simp				ada_cfaerase(softc, bp, ataio);
2379298002Simp				break;
2380298002Simp			default:
2381298002Simp				biofinish(bp, NULL, EOPNOTSUPP);
2382298002Simp				xpt_release_ccb(start_ccb);
2383298002Simp				adaschedule(periph);
2384298002Simp				return;
2385298002Simp			}
2386298002Simp			start_ccb->ccb_h.ccb_state = ADA_CCB_TRIM;
2387298002Simp			start_ccb->ccb_h.flags |= CAM_UNLOCKED;
2388298002Simp			cam_iosched_submit_trim(softc->cam_iosched);
2389298002Simp			goto out;
2390201139Smav		case BIO_FLUSH:
2391201139Smav			cam_fill_ataio(ataio,
2392201139Smav			    1,
2393201139Smav			    adadone,
2394201139Smav			    CAM_DIR_NONE,
2395201139Smav			    0,
2396201139Smav			    NULL,
2397201139Smav			    0,
2398201139Smav			    ada_default_timeout*1000);
2399201139Smav
2400201139Smav			if (softc->flags & ADA_FLAG_CAN_48BIT)
2401201139Smav				ata_48bit_cmd(ataio, ATA_FLUSHCACHE48, 0, 0, 0);
2402201139Smav			else
2403201139Smav				ata_28bit_cmd(ataio, ATA_FLUSHCACHE, 0, 0, 0);
2404201139Smav			break;
2405300207Sken		case BIO_ZONE: {
2406300207Sken			int error, queue_ccb;
2407300207Sken
2408300207Sken			queue_ccb = 0;
2409300207Sken
2410300207Sken			error = ada_zone_cmd(periph, start_ccb, bp, &queue_ccb);
2411300207Sken			if ((error != 0)
2412300207Sken			 || (queue_ccb == 0)) {
2413300207Sken				biofinish(bp, NULL, error);
2414300207Sken				xpt_release_ccb(start_ccb);
2415300207Sken				return;
2416300207Sken			}
2417300207Sken			break;
2418195534Sscottl		}
2419300207Sken		}
2420201139Smav		start_ccb->ccb_h.ccb_state = ADA_CCB_BUFFER_IO;
2421256843Smav		start_ccb->ccb_h.flags |= CAM_UNLOCKED;
2422201139Smavout:
2423201139Smav		start_ccb->ccb_h.ccb_bp = bp;
2424201139Smav		softc->outstanding_cmds++;
2425256843Smav		softc->refcount++;
2426256843Smav		cam_periph_unlock(periph);
2427201139Smav		xpt_action(start_ccb);
2428256843Smav		cam_periph_lock(periph);
2429256843Smav		softc->refcount--;
2430201139Smav
2431201139Smav		/* May have more work to do, so ensure we stay scheduled */
2432201139Smav		adaschedule(periph);
2433195534Sscottl		break;
2434195534Sscottl	}
2435224497Smav	case ADA_STATE_RAHEAD:
2436220412Smav	case ADA_STATE_WCACHE:
2437220412Smav	{
2438220412Smav		cam_fill_ataio(ataio,
2439220412Smav		    1,
2440220412Smav		    adadone,
2441220412Smav		    CAM_DIR_NONE,
2442220412Smav		    0,
2443220412Smav		    NULL,
2444220412Smav		    0,
2445220412Smav		    ada_default_timeout*1000);
2446220412Smav
2447224497Smav		if (softc->state == ADA_STATE_RAHEAD) {
2448224497Smav			ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_RA ?
2449224497Smav			    ATA_SF_ENAB_RCACHE : ATA_SF_DIS_RCACHE, 0, 0);
2450224497Smav			start_ccb->ccb_h.ccb_state = ADA_CCB_RAHEAD;
2451224497Smav		} else {
2452224497Smav			ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_WC ?
2453224497Smav			    ATA_SF_ENAB_WCACHE : ATA_SF_DIS_WCACHE, 0, 0);
2454224497Smav			start_ccb->ccb_h.ccb_state = ADA_CCB_WCACHE;
2455224497Smav		}
2456249466Smav		start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2457220412Smav		xpt_action(start_ccb);
2458220412Smav		break;
2459195534Sscottl	}
2460300207Sken	case ADA_STATE_LOGDIR:
2461300207Sken	{
2462300207Sken		struct ata_gp_log_dir *log_dir;
2463300207Sken
2464300207Sken		if ((softc->flags & ADA_FLAG_CAN_LOG) == 0) {
2465300207Sken			adaprobedone(periph, start_ccb);
2466300207Sken			break;
2467300207Sken		}
2468300207Sken
2469300207Sken		log_dir = malloc(sizeof(*log_dir), M_ATADA, M_NOWAIT|M_ZERO);
2470300207Sken		if (log_dir == NULL) {
2471300207Sken			xpt_print(periph->path, "Couldn't malloc log_dir "
2472300207Sken			    "data\n");
2473300207Sken			softc->state = ADA_STATE_NORMAL;
2474300207Sken			xpt_release_ccb(start_ccb);
2475300207Sken			break;
2476300207Sken		}
2477300207Sken
2478300207Sken
2479300207Sken		ata_read_log(ataio,
2480300207Sken		    /*retries*/1,
2481300207Sken		    /*cbfcnp*/adadone,
2482300207Sken		    /*log_address*/ ATA_LOG_DIRECTORY,
2483300207Sken		    /*page_number*/ 0,
2484300207Sken		    /*block_count*/ 1,
2485300207Sken		    /*protocol*/ softc->flags & ADA_FLAG_CAN_DMA ?
2486300207Sken				 CAM_ATAIO_DMA : 0,
2487300207Sken		    /*data_ptr*/ (uint8_t *)log_dir,
2488300207Sken		    /*dxfer_len*/sizeof(*log_dir),
2489300207Sken		    /*timeout*/ada_default_timeout*1000);
2490300207Sken
2491300207Sken		start_ccb->ccb_h.ccb_state = ADA_CCB_LOGDIR;
2492300207Sken		xpt_action(start_ccb);
2493300207Sken		break;
2494220412Smav	}
2495300207Sken	case ADA_STATE_IDDIR:
2496300207Sken	{
2497300207Sken		struct ata_identify_log_pages *id_dir;
2498300207Sken
2499300207Sken		id_dir = malloc(sizeof(*id_dir), M_ATADA, M_NOWAIT | M_ZERO);
2500300207Sken		if (id_dir == NULL) {
2501300207Sken			xpt_print(periph->path, "Couldn't malloc id_dir "
2502300207Sken			    "data\n");
2503300207Sken			adaprobedone(periph, start_ccb);
2504300207Sken			break;
2505300207Sken		}
2506300207Sken
2507300207Sken		ata_read_log(ataio,
2508300207Sken		    /*retries*/1,
2509300207Sken		    /*cbfcnp*/adadone,
2510300207Sken		    /*log_address*/ ATA_IDENTIFY_DATA_LOG,
2511300207Sken		    /*page_number*/ ATA_IDL_PAGE_LIST,
2512300207Sken		    /*block_count*/ 1,
2513300207Sken		    /*protocol*/ softc->flags & ADA_FLAG_CAN_DMA ?
2514300207Sken				 CAM_ATAIO_DMA : 0,
2515300207Sken		    /*data_ptr*/ (uint8_t *)id_dir,
2516300207Sken		    /*dxfer_len*/ sizeof(*id_dir),
2517300207Sken		    /*timeout*/ada_default_timeout*1000);
2518300207Sken
2519300207Sken		start_ccb->ccb_h.ccb_state = ADA_CCB_IDDIR;
2520300207Sken		xpt_action(start_ccb);
2521300207Sken		break;
2522300207Sken	}
2523300207Sken	case ADA_STATE_SUP_CAP:
2524300207Sken	{
2525300207Sken		struct ata_identify_log_sup_cap *sup_cap;
2526300207Sken
2527300207Sken		sup_cap = malloc(sizeof(*sup_cap), M_ATADA, M_NOWAIT|M_ZERO);
2528300207Sken		if (sup_cap == NULL) {
2529300207Sken			xpt_print(periph->path, "Couldn't malloc sup_cap "
2530300207Sken			    "data\n");
2531300207Sken			adaprobedone(periph, start_ccb);
2532300207Sken			break;
2533300207Sken		}
2534300207Sken
2535300207Sken		ata_read_log(ataio,
2536300207Sken		    /*retries*/1,
2537300207Sken		    /*cbfcnp*/adadone,
2538300207Sken		    /*log_address*/ ATA_IDENTIFY_DATA_LOG,
2539300207Sken		    /*page_number*/ ATA_IDL_SUP_CAP,
2540300207Sken		    /*block_count*/ 1,
2541300207Sken		    /*protocol*/ softc->flags & ADA_FLAG_CAN_DMA ?
2542300207Sken				 CAM_ATAIO_DMA : 0,
2543300207Sken		    /*data_ptr*/ (uint8_t *)sup_cap,
2544300207Sken		    /*dxfer_len*/ sizeof(*sup_cap),
2545300207Sken		    /*timeout*/ada_default_timeout*1000);
2546300207Sken
2547300207Sken		start_ccb->ccb_h.ccb_state = ADA_CCB_SUP_CAP;
2548300207Sken		xpt_action(start_ccb);
2549300207Sken		break;
2550300207Sken	}
2551300207Sken	case ADA_STATE_ZONE:
2552300207Sken	{
2553300207Sken		struct ata_zoned_info_log *ata_zone;
2554300207Sken
2555300207Sken		ata_zone = malloc(sizeof(*ata_zone), M_ATADA, M_NOWAIT|M_ZERO);
2556300207Sken		if (ata_zone == NULL) {
2557300207Sken			xpt_print(periph->path, "Couldn't malloc ata_zone "
2558300207Sken			    "data\n");
2559300207Sken			adaprobedone(periph, start_ccb);
2560300207Sken			break;
2561300207Sken		}
2562300207Sken
2563300207Sken		ata_read_log(ataio,
2564300207Sken		    /*retries*/1,
2565300207Sken		    /*cbfcnp*/adadone,
2566300207Sken		    /*log_address*/ ATA_IDENTIFY_DATA_LOG,
2567300207Sken		    /*page_number*/ ATA_IDL_ZDI,
2568300207Sken		    /*block_count*/ 1,
2569300207Sken		    /*protocol*/ softc->flags & ADA_FLAG_CAN_DMA ?
2570300207Sken				 CAM_ATAIO_DMA : 0,
2571300207Sken		    /*data_ptr*/ (uint8_t *)ata_zone,
2572300207Sken		    /*dxfer_len*/ sizeof(*ata_zone),
2573300207Sken		    /*timeout*/ada_default_timeout*1000);
2574300207Sken
2575300207Sken		start_ccb->ccb_h.ccb_state = ADA_CCB_ZONE;
2576300207Sken		xpt_action(start_ccb);
2577300207Sken		break;
2578300207Sken	}
2579300207Sken	}
2580195534Sscottl}
2581195534Sscottl
2582195534Sscottlstatic void
2583300207Skenadaprobedone(struct cam_periph *periph, union ccb *ccb)
2584300207Sken{
2585300207Sken	struct ada_softc *softc;
2586300207Sken
2587300207Sken	softc = (struct ada_softc *)periph->softc;
2588300207Sken
2589300207Sken	if (ccb != NULL)
2590300207Sken		xpt_release_ccb(ccb);
2591300207Sken
2592300207Sken	softc->state = ADA_STATE_NORMAL;
2593300207Sken	softc->flags |= ADA_FLAG_PROBED;
2594300207Sken	adaschedule(periph);
2595300207Sken	if ((softc->flags & ADA_FLAG_ANNOUNCED) == 0) {
2596300207Sken		softc->flags |= ADA_FLAG_ANNOUNCED;
2597300207Sken		cam_periph_unhold(periph);
2598300207Sken	} else {
2599300207Sken		cam_periph_release_locked(periph);
2600300207Sken	}
2601300207Sken}
2602300207Sken
2603300207Skenstatic void
2604300207Skenadazonedone(struct cam_periph *periph, union ccb *ccb)
2605300207Sken{
2606300207Sken	struct ada_softc *softc;
2607300207Sken	struct bio *bp;
2608300207Sken
2609300207Sken	softc = periph->softc;
2610300207Sken	bp = (struct bio *)ccb->ccb_h.ccb_bp;
2611300207Sken
2612300207Sken	switch (bp->bio_zone.zone_cmd) {
2613300207Sken	case DISK_ZONE_OPEN:
2614300207Sken	case DISK_ZONE_CLOSE:
2615300207Sken	case DISK_ZONE_FINISH:
2616300207Sken	case DISK_ZONE_RWP:
2617300207Sken		break;
2618300207Sken	case DISK_ZONE_REPORT_ZONES: {
2619300207Sken		uint32_t avail_len;
2620300207Sken		struct disk_zone_report *rep;
2621300207Sken		struct scsi_report_zones_hdr *hdr;
2622300207Sken		struct scsi_report_zones_desc *desc;
2623300207Sken		struct disk_zone_rep_entry *entry;
2624300207Sken		uint32_t num_alloced, hdr_len, num_avail;
2625300207Sken		uint32_t num_to_fill, i;
2626300207Sken
2627300207Sken		rep = &bp->bio_zone.zone_params.report;
2628300207Sken		avail_len = ccb->ataio.dxfer_len - ccb->ataio.resid;
2629300207Sken		/*
2630300207Sken		 * Note that bio_resid isn't normally used for zone
2631300207Sken		 * commands, but it is used by devstat_end_transaction_bio()
2632300207Sken		 * to determine how much data was transferred.  Because
2633300207Sken		 * the size of the SCSI/ATA data structures is different
2634300207Sken		 * than the size of the BIO interface structures, the
2635300207Sken		 * amount of data actually transferred from the drive will
2636300207Sken		 * be different than the amount of data transferred to
2637300207Sken		 * the user.
2638300207Sken		 */
2639300207Sken		num_alloced = rep->entries_allocated;
2640300207Sken		hdr = (struct scsi_report_zones_hdr *)ccb->ataio.data_ptr;
2641300207Sken		if (avail_len < sizeof(*hdr)) {
2642300207Sken			/*
2643300207Sken			 * Is there a better error than EIO here?  We asked
2644300207Sken			 * for at least the header, and we got less than
2645300207Sken			 * that.
2646300207Sken			 */
2647300207Sken			bp->bio_error = EIO;
2648300207Sken			bp->bio_flags |= BIO_ERROR;
2649300207Sken			bp->bio_resid = bp->bio_bcount;
2650300207Sken			break;
2651300207Sken		}
2652300207Sken
2653300207Sken		hdr_len = le32dec(hdr->length);
2654300207Sken		if (hdr_len > 0)
2655300207Sken			rep->entries_available = hdr_len / sizeof(*desc);
2656300207Sken		else
2657300207Sken			rep->entries_available = 0;
2658300207Sken		/*
2659300207Sken		 * NOTE: using the same values for the BIO version of the
2660300207Sken		 * same field as the SCSI/ATA values.  This means we could
2661300207Sken		 * get some additional values that aren't defined in bio.h
2662300207Sken		 * if more values of the same field are defined later.
2663300207Sken		 */
2664300207Sken		rep->header.same = hdr->byte4 & SRZ_SAME_MASK;
2665300207Sken		rep->header.maximum_lba = le64dec(hdr->maximum_lba);
2666300207Sken		/*
2667300207Sken		 * If the drive reports no entries that match the query,
2668300207Sken		 * we're done.
2669300207Sken		 */
2670300207Sken		if (hdr_len == 0) {
2671300207Sken			rep->entries_filled = 0;
2672300207Sken			bp->bio_resid = bp->bio_bcount;
2673300207Sken			break;
2674300207Sken		}
2675300207Sken
2676300207Sken		num_avail = min((avail_len - sizeof(*hdr)) / sizeof(*desc),
2677300207Sken				hdr_len / sizeof(*desc));
2678300207Sken		/*
2679300207Sken		 * If the drive didn't return any data, then we're done.
2680300207Sken		 */
2681300207Sken		if (num_avail == 0) {
2682300207Sken			rep->entries_filled = 0;
2683300207Sken			bp->bio_resid = bp->bio_bcount;
2684300207Sken			break;
2685300207Sken		}
2686300207Sken
2687300207Sken		num_to_fill = min(num_avail, rep->entries_allocated);
2688300207Sken		/*
2689300207Sken		 * If the user didn't allocate any entries for us to fill,
2690300207Sken		 * we're done.
2691300207Sken		 */
2692300207Sken		if (num_to_fill == 0) {
2693300207Sken			rep->entries_filled = 0;
2694300207Sken			bp->bio_resid = bp->bio_bcount;
2695300207Sken			break;
2696300207Sken		}
2697300207Sken
2698300207Sken		for (i = 0, desc = &hdr->desc_list[0], entry=&rep->entries[0];
2699300207Sken		     i < num_to_fill; i++, desc++, entry++) {
2700300207Sken			/*
2701300207Sken			 * NOTE: we're mapping the values here directly
2702300207Sken			 * from the SCSI/ATA bit definitions to the bio.h
2703300207Sken			 * definitions.  There is also a warning in
2704300207Sken			 * disk_zone.h, but the impact is that if
2705300207Sken			 * additional values are added in the SCSI/ATA
2706300207Sken			 * specs these will be visible to consumers of
2707300207Sken			 * this interface.
2708300207Sken			 */
2709300207Sken			entry->zone_type = desc->zone_type & SRZ_TYPE_MASK;
2710300207Sken			entry->zone_condition =
2711300207Sken			    (desc->zone_flags & SRZ_ZONE_COND_MASK) >>
2712300207Sken			    SRZ_ZONE_COND_SHIFT;
2713300207Sken			entry->zone_flags |= desc->zone_flags &
2714300207Sken			    (SRZ_ZONE_NON_SEQ|SRZ_ZONE_RESET);
2715300207Sken			entry->zone_length = le64dec(desc->zone_length);
2716300207Sken			entry->zone_start_lba = le64dec(desc->zone_start_lba);
2717300207Sken			entry->write_pointer_lba =
2718300207Sken			    le64dec(desc->write_pointer_lba);
2719300207Sken		}
2720300207Sken		rep->entries_filled = num_to_fill;
2721300207Sken		/*
2722300207Sken		 * Note that this residual is accurate from the user's
2723300207Sken		 * standpoint, but the amount transferred isn't accurate
2724300207Sken		 * from the standpoint of what actually came back from the
2725300207Sken		 * drive.
2726300207Sken		 */
2727300207Sken		bp->bio_resid = bp->bio_bcount - (num_to_fill * sizeof(*entry));
2728300207Sken		break;
2729300207Sken	}
2730300207Sken	case DISK_ZONE_GET_PARAMS:
2731300207Sken	default:
2732300207Sken		/*
2733300207Sken		 * In theory we should not get a GET_PARAMS bio, since it
2734300207Sken		 * should be handled without queueing the command to the
2735300207Sken		 * drive.
2736300207Sken		 */
2737300207Sken		panic("%s: Invalid zone command %d", __func__,
2738300207Sken		    bp->bio_zone.zone_cmd);
2739300207Sken		break;
2740300207Sken	}
2741300207Sken
2742300207Sken	if (bp->bio_zone.zone_cmd == DISK_ZONE_REPORT_ZONES)
2743300207Sken		free(ccb->ataio.data_ptr, M_ATADA);
2744300207Sken}
2745300207Sken
2746300207Sken
2747300207Skenstatic void
2748195534Sscottladadone(struct cam_periph *periph, union ccb *done_ccb)
2749195534Sscottl{
2750195534Sscottl	struct ada_softc *softc;
2751195534Sscottl	struct ccb_ataio *ataio;
2752249466Smav	struct cam_path *path;
2753300207Sken	uint32_t priority;
2754253752Smav	int state;
2755195534Sscottl
2756195534Sscottl	softc = (struct ada_softc *)periph->softc;
2757195534Sscottl	ataio = &done_ccb->ataio;
2758249466Smav	path = done_ccb->ccb_h.path;
2759300207Sken	priority = done_ccb->ccb_h.pinfo.priority;
2760236602Smav
2761249466Smav	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("adadone\n"));
2762236602Smav
2763253752Smav	state = ataio->ccb_h.ccb_state & ADA_CCB_TYPE_MASK;
2764253752Smav	switch (state) {
2765195534Sscottl	case ADA_CCB_BUFFER_IO:
2766201139Smav	case ADA_CCB_TRIM:
2767195534Sscottl	{
2768195534Sscottl		struct bio *bp;
2769253752Smav		int error;
2770195534Sscottl
2771256843Smav		cam_periph_lock(periph);
2772298002Simp		bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
2773195534Sscottl		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2774198328Smav			error = adaerror(done_ccb, 0, 0);
2775195534Sscottl			if (error == ERESTART) {
2776198328Smav				/* A retry was scheduled, so just return. */
2777256843Smav				cam_periph_unlock(periph);
2778195534Sscottl				return;
2779195534Sscottl			}
2780195534Sscottl			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2781249466Smav				cam_release_devq(path,
2782195534Sscottl						 /*relsim_flags*/0,
2783195534Sscottl						 /*reduction*/0,
2784195534Sscottl						 /*timeout*/0,
2785195534Sscottl						 /*getcount_only*/0);
2786298002Simp			/*
2787298002Simp			 * If we get an error on an NCQ DSM TRIM, fall back
2788298002Simp			 * to a non-NCQ DSM TRIM forever. Please note that if
2789298002Simp			 * CAN_NCQ_TRIM is set, CAN_TRIM is necessarily set too.
2790298002Simp			 * However, for this one trim, we treat it as advisory
2791298002Simp			 * and return success up the stack.
2792298002Simp			 */
2793298002Simp			if (state == ADA_CCB_TRIM &&
2794298002Simp			    error != 0 &&
2795298002Simp			    (softc->flags & ADA_FLAG_CAN_NCQ_TRIM) != 0) {
2796298002Simp				softc->flags &= ~ADA_FLAG_CAN_NCQ_TRIM;
2797298002Simp				error = 0;
2798298002Simp				adasetdeletemethod(softc);
2799298002Simp			}
2800195534Sscottl		} else {
2801195534Sscottl			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2802195534Sscottl				panic("REQ_CMP with QFRZN");
2803300207Sken
2804253752Smav			error = 0;
2805253752Smav		}
2806253752Smav		bp->bio_error = error;
2807253752Smav		if (error != 0) {
2808253752Smav			bp->bio_resid = bp->bio_bcount;
2809253752Smav			bp->bio_flags |= BIO_ERROR;
2810253752Smav		} else {
2811300207Sken			if (bp->bio_cmd == BIO_ZONE)
2812300207Sken				adazonedone(periph, done_ccb);
2813300207Sken			else if (state == ADA_CCB_TRIM)
2814253752Smav				bp->bio_resid = 0;
2815253752Smav			else
2816253752Smav				bp->bio_resid = ataio->resid;
2817300207Sken
2818300207Sken			if ((bp->bio_resid > 0)
2819300207Sken			 && (bp->bio_cmd != BIO_ZONE))
2820195534Sscottl				bp->bio_flags |= BIO_ERROR;
2821195534Sscottl		}
2822195534Sscottl		softc->outstanding_cmds--;
2823195534Sscottl		if (softc->outstanding_cmds == 0)
2824257054Smav			softc->flags |= ADA_FLAG_WAS_OTAG;
2825298002Simp
2826298002Simp		cam_iosched_bio_complete(softc->cam_iosched, bp, done_ccb);
2827256843Smav		xpt_release_ccb(done_ccb);
2828253752Smav		if (state == ADA_CCB_TRIM) {
2829256836Smav			TAILQ_HEAD(, bio) queue;
2830256836Smav			struct bio *bp1;
2831195534Sscottl
2832256836Smav			TAILQ_INIT(&queue);
2833256836Smav			TAILQ_CONCAT(&queue, &softc->trim_req.bps, bio_queue);
2834277101Simp			/*
2835277101Simp			 * Normally, the xpt_release_ccb() above would make sure
2836277101Simp			 * that when we have more work to do, that work would
2837277101Simp			 * get kicked off. However, we specifically keep
2838277101Simp			 * trim_running set to 0 before the call above to allow
2839277101Simp			 * other I/O to progress when many BIO_DELETE requests
2840277101Simp			 * are pushed down. We set trim_running to 0 and call
2841277101Simp			 * daschedule again so that we don't stall if there are
2842277101Simp			 * no other I/Os pending apart from BIO_DELETEs.
2843277101Simp			 */
2844298002Simp			cam_iosched_trim_done(softc->cam_iosched);
2845256843Smav			adaschedule(periph);
2846256843Smav			cam_periph_unlock(periph);
2847256836Smav			while ((bp1 = TAILQ_FIRST(&queue)) != NULL) {
2848256836Smav				TAILQ_REMOVE(&queue, bp1, bio_queue);
2849256836Smav				bp1->bio_error = error;
2850256836Smav				if (error != 0) {
2851201139Smav					bp1->bio_flags |= BIO_ERROR;
2852253752Smav					bp1->bio_resid = bp1->bio_bcount;
2853253752Smav				} else
2854253752Smav					bp1->bio_resid = 0;
2855201139Smav				biodone(bp1);
2856201139Smav			}
2857256843Smav		} else {
2858298002Simp			adaschedule(periph);
2859256843Smav			cam_periph_unlock(periph);
2860201139Smav			biodone(bp);
2861256843Smav		}
2862256843Smav		return;
2863195534Sscottl	}
2864224497Smav	case ADA_CCB_RAHEAD:
2865224497Smav	{
2866224497Smav		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2867224497Smav			if (adaerror(done_ccb, 0, 0) == ERESTART) {
2868249466Smav				/* Drop freeze taken due to CAM_DEV_QFREEZE */
2869249466Smav				cam_release_devq(path, 0, 0, 0, FALSE);
2870224497Smav				return;
2871224497Smav			} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
2872249466Smav				cam_release_devq(path,
2873224497Smav				    /*relsim_flags*/0,
2874224497Smav				    /*reduction*/0,
2875224497Smav				    /*timeout*/0,
2876224497Smav				    /*getcount_only*/0);
2877224497Smav			}
2878224497Smav		}
2879224497Smav
2880224497Smav		/*
2881224497Smav		 * Since our peripheral may be invalidated by an error
2882224497Smav		 * above or an external event, we must release our CCB
2883224497Smav		 * before releasing the reference on the peripheral.
2884224497Smav		 * The peripheral will only go away once the last reference
2885224497Smav		 * is removed, and we need it around for the CCB release
2886224497Smav		 * operation.
2887224497Smav		 */
2888300207Sken
2889224497Smav		xpt_release_ccb(done_ccb);
2890300207Sken		softc->state = ADA_STATE_WCACHE;
2891300207Sken		xpt_schedule(periph, priority);
2892249466Smav		/* Drop freeze taken due to CAM_DEV_QFREEZE */
2893249466Smav		cam_release_devq(path, 0, 0, 0, FALSE);
2894224497Smav		return;
2895224497Smav	}
2896220412Smav	case ADA_CCB_WCACHE:
2897220412Smav	{
2898220412Smav		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2899220412Smav			if (adaerror(done_ccb, 0, 0) == ERESTART) {
2900300207Sken				/* Drop freeze taken due to CAM_DEV_QFREEZE */
2901300207Sken				cam_release_devq(path, 0, 0, 0, FALSE);
2902300207Sken				return;
2903220412Smav			} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
2904249466Smav				cam_release_devq(path,
2905220412Smav				    /*relsim_flags*/0,
2906220412Smav				    /*reduction*/0,
2907220412Smav				    /*timeout*/0,
2908220412Smav				    /*getcount_only*/0);
2909220412Smav			}
2910220412Smav		}
2911220412Smav
2912249466Smav		/* Drop freeze taken due to CAM_DEV_QFREEZE */
2913249466Smav		cam_release_devq(path, 0, 0, 0, FALSE);
2914300207Sken
2915300640Sken		if ((softc->flags & ADA_FLAG_CAN_LOG)
2916300640Sken		 && (softc->zone_mode != ADA_ZONE_NONE)) {
2917300207Sken			xpt_release_ccb(done_ccb);
2918300207Sken			softc->state = ADA_STATE_LOGDIR;
2919300207Sken			xpt_schedule(periph, priority);
2920300207Sken		} else {
2921300207Sken			adaprobedone(periph, done_ccb);
2922300207Sken		}
2923220412Smav		return;
2924220412Smav	}
2925300207Sken	case ADA_CCB_LOGDIR:
2926300207Sken	{
2927300207Sken		int error;
2928300207Sken
2929300207Sken		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
2930300207Sken			error = 0;
2931300207Sken			softc->valid_logdir_len = 0;
2932300207Sken			bzero(&softc->ata_logdir, sizeof(softc->ata_logdir));
2933300207Sken			softc->valid_logdir_len =
2934300207Sken				ataio->dxfer_len - ataio->resid;
2935300207Sken			if (softc->valid_logdir_len > 0)
2936300207Sken				bcopy(ataio->data_ptr, &softc->ata_logdir,
2937300207Sken				    min(softc->valid_logdir_len,
2938300207Sken					sizeof(softc->ata_logdir)));
2939300207Sken			/*
2940300207Sken			 * Figure out whether the Identify Device log is
2941300207Sken			 * supported.  The General Purpose log directory
2942300207Sken			 * has a header, and lists the number of pages
2943300207Sken			 * available for each GP log identified by the
2944300207Sken			 * offset into the list.
2945300207Sken			 */
2946300207Sken			if ((softc->valid_logdir_len >=
2947300207Sken			    ((ATA_IDENTIFY_DATA_LOG + 1) * sizeof(uint16_t)))
2948300207Sken			 && (le16dec(softc->ata_logdir.header) ==
2949300207Sken			     ATA_GP_LOG_DIR_VERSION)
2950300207Sken			 && (le16dec(&softc->ata_logdir.num_pages[
2951300207Sken			     (ATA_IDENTIFY_DATA_LOG *
2952300207Sken			     sizeof(uint16_t)) - sizeof(uint16_t)]) > 0)){
2953300207Sken				softc->flags |= ADA_FLAG_CAN_IDLOG;
2954300207Sken			} else {
2955300207Sken				softc->flags &= ~ADA_FLAG_CAN_IDLOG;
2956300207Sken			}
2957300207Sken		} else {
2958300207Sken			error = adaerror(done_ccb, CAM_RETRY_SELTO,
2959300207Sken					 SF_RETRY_UA|SF_NO_PRINT);
2960300207Sken			if (error == ERESTART)
2961300207Sken				return;
2962300207Sken			else if (error != 0) {
2963300207Sken				/*
2964300207Sken				 * If we can't get the ATA log directory,
2965300207Sken				 * then ATA logs are effectively not
2966300207Sken				 * supported even if the bit is set in the
2967300207Sken				 * identify data.
2968300207Sken				 */
2969300207Sken				softc->flags &= ~(ADA_FLAG_CAN_LOG |
2970300207Sken						  ADA_FLAG_CAN_IDLOG);
2971300207Sken				if ((done_ccb->ccb_h.status &
2972300207Sken				     CAM_DEV_QFRZN) != 0) {
2973300207Sken					/* Don't wedge this device's queue */
2974300207Sken					cam_release_devq(done_ccb->ccb_h.path,
2975300207Sken							 /*relsim_flags*/0,
2976300207Sken							 /*reduction*/0,
2977300207Sken							 /*timeout*/0,
2978300207Sken							 /*getcount_only*/0);
2979300207Sken				}
2980300207Sken			}
2981300207Sken
2982300207Sken
2983300207Sken		}
2984300207Sken
2985300207Sken		free(ataio->data_ptr, M_ATADA);
2986300207Sken
2987300207Sken		if ((error == 0)
2988300207Sken		 && (softc->flags & ADA_FLAG_CAN_IDLOG)) {
2989300207Sken			softc->state = ADA_STATE_IDDIR;
2990300207Sken			xpt_release_ccb(done_ccb);
2991300207Sken			xpt_schedule(periph, priority);
2992300207Sken		} else
2993300207Sken			adaprobedone(periph, done_ccb);
2994300207Sken
2995300207Sken		return;
2996300207Sken	}
2997300207Sken	case ADA_CCB_IDDIR: {
2998300207Sken		int error;
2999300207Sken
3000300207Sken		if ((ataio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3001300207Sken			off_t entries_offset, max_entries;
3002300207Sken			error = 0;
3003300207Sken
3004300207Sken			softc->valid_iddir_len = 0;
3005300207Sken			bzero(&softc->ata_iddir, sizeof(softc->ata_iddir));
3006300207Sken			softc->flags &= ~(ADA_FLAG_CAN_SUPCAP |
3007300207Sken					  ADA_FLAG_CAN_ZONE);
3008300207Sken			softc->valid_iddir_len =
3009300207Sken				ataio->dxfer_len - ataio->resid;
3010300207Sken			if (softc->valid_iddir_len > 0)
3011300207Sken				bcopy(ataio->data_ptr, &softc->ata_iddir,
3012300207Sken				    min(softc->valid_iddir_len,
3013300207Sken					sizeof(softc->ata_iddir)));
3014300207Sken
3015300207Sken			entries_offset =
3016300207Sken			    __offsetof(struct ata_identify_log_pages,entries);
3017300207Sken			max_entries = softc->valid_iddir_len - entries_offset;
3018300207Sken			if ((softc->valid_iddir_len > (entries_offset + 1))
3019300207Sken			 && (le64dec(softc->ata_iddir.header) ==
3020300207Sken			     ATA_IDLOG_REVISION)
3021300207Sken			 && (softc->ata_iddir.entry_count > 0)) {
3022300207Sken				int num_entries, i;
3023300207Sken
3024300207Sken				num_entries = softc->ata_iddir.entry_count;
3025300207Sken				num_entries = min(num_entries,
3026300207Sken				   softc->valid_iddir_len - entries_offset);
3027300207Sken				for (i = 0; i < num_entries &&
3028300207Sken				     i < max_entries; i++) {
3029300207Sken					if (softc->ata_iddir.entries[i] ==
3030300207Sken					    ATA_IDL_SUP_CAP)
3031300207Sken						softc->flags |=
3032300207Sken						    ADA_FLAG_CAN_SUPCAP;
3033300207Sken					else if (softc->ata_iddir.entries[i]==
3034300207Sken						 ATA_IDL_ZDI)
3035300207Sken						softc->flags |=
3036300207Sken						    ADA_FLAG_CAN_ZONE;
3037300207Sken
3038300207Sken					if ((softc->flags &
3039300207Sken					     ADA_FLAG_CAN_SUPCAP)
3040300207Sken					 && (softc->flags &
3041300207Sken					     ADA_FLAG_CAN_ZONE))
3042300207Sken						break;
3043300207Sken				}
3044300207Sken			}
3045300207Sken		} else {
3046300207Sken			error = adaerror(done_ccb, CAM_RETRY_SELTO,
3047300207Sken					 SF_RETRY_UA|SF_NO_PRINT);
3048300207Sken			if (error == ERESTART)
3049300207Sken				return;
3050300207Sken			else if (error != 0) {
3051300207Sken				/*
3052300207Sken				 * If we can't get the ATA Identify Data log
3053300207Sken				 * directory, then it effectively isn't
3054300207Sken				 * supported even if the ATA Log directory
3055300207Sken				 * a non-zero number of pages present for
3056300207Sken				 * this log.
3057300207Sken				 */
3058300207Sken				softc->flags &= ~ADA_FLAG_CAN_IDLOG;
3059300207Sken				if ((done_ccb->ccb_h.status &
3060300207Sken				     CAM_DEV_QFRZN) != 0) {
3061300207Sken					/* Don't wedge this device's queue */
3062300207Sken					cam_release_devq(done_ccb->ccb_h.path,
3063300207Sken							 /*relsim_flags*/0,
3064300207Sken							 /*reduction*/0,
3065300207Sken							 /*timeout*/0,
3066300207Sken							 /*getcount_only*/0);
3067300207Sken				}
3068300207Sken			}
3069300207Sken		}
3070300207Sken
3071300207Sken		free(ataio->data_ptr, M_ATADA);
3072300207Sken
3073300207Sken		if ((error == 0)
3074300207Sken		 && (softc->flags & ADA_FLAG_CAN_SUPCAP)) {
3075300207Sken			softc->state = ADA_STATE_SUP_CAP;
3076300207Sken			xpt_release_ccb(done_ccb);
3077300207Sken			xpt_schedule(periph, priority);
3078300207Sken		} else
3079300207Sken			adaprobedone(periph, done_ccb);
3080300207Sken		return;
3081300207Sken	}
3082300207Sken	case ADA_CCB_SUP_CAP: {
3083300207Sken		int error;
3084300207Sken
3085300207Sken		if ((ataio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3086300207Sken			uint32_t valid_len;
3087300207Sken			size_t needed_size;
3088300207Sken			struct ata_identify_log_sup_cap *sup_cap;
3089300207Sken			error = 0;
3090300207Sken
3091300207Sken			sup_cap = (struct ata_identify_log_sup_cap *)
3092300207Sken			    ataio->data_ptr;
3093300207Sken			valid_len = ataio->dxfer_len - ataio->resid;
3094300207Sken			needed_size =
3095300207Sken			    __offsetof(struct ata_identify_log_sup_cap,
3096300207Sken			    sup_zac_cap) + 1 + sizeof(sup_cap->sup_zac_cap);
3097300207Sken			if (valid_len >= needed_size) {
3098300207Sken				uint64_t zoned, zac_cap;
3099300207Sken
3100300207Sken				zoned = le64dec(sup_cap->zoned_cap);
3101300207Sken				if (zoned & ATA_ZONED_VALID) {
3102300207Sken					/*
3103300207Sken					 * This should have already been
3104300207Sken					 * set, because this is also in the
3105300207Sken					 * ATA identify data.
3106300207Sken					 */
3107300207Sken					if ((zoned & ATA_ZONED_MASK) ==
3108300207Sken					    ATA_SUPPORT_ZONE_HOST_AWARE)
3109300207Sken						softc->zone_mode =
3110300207Sken						    ADA_ZONE_HOST_AWARE;
3111300207Sken					else if ((zoned & ATA_ZONED_MASK) ==
3112300207Sken					    ATA_SUPPORT_ZONE_DEV_MANAGED)
3113300207Sken						softc->zone_mode =
3114300207Sken						    ADA_ZONE_DRIVE_MANAGED;
3115300207Sken				}
3116300207Sken
3117300207Sken				zac_cap = le64dec(sup_cap->sup_zac_cap);
3118300207Sken				if (zac_cap & ATA_SUP_ZAC_CAP_VALID) {
3119300207Sken					if (zac_cap & ATA_REPORT_ZONES_SUP)
3120300207Sken						softc->zone_flags |=
3121300207Sken						    ADA_ZONE_FLAG_RZ_SUP;
3122300207Sken					if (zac_cap & ATA_ND_OPEN_ZONE_SUP)
3123300207Sken						softc->zone_flags |=
3124300207Sken						    ADA_ZONE_FLAG_OPEN_SUP;
3125300207Sken					if (zac_cap & ATA_ND_CLOSE_ZONE_SUP)
3126300207Sken						softc->zone_flags |=
3127300207Sken						    ADA_ZONE_FLAG_CLOSE_SUP;
3128300207Sken					if (zac_cap & ATA_ND_FINISH_ZONE_SUP)
3129300207Sken						softc->zone_flags |=
3130300207Sken						    ADA_ZONE_FLAG_FINISH_SUP;
3131300207Sken					if (zac_cap & ATA_ND_RWP_SUP)
3132300207Sken						softc->zone_flags |=
3133300207Sken						    ADA_ZONE_FLAG_RWP_SUP;
3134300207Sken				} else {
3135300207Sken					/*
3136300207Sken					 * This field was introduced in
3137300207Sken					 * ACS-4, r08 on April 28th, 2015.
3138300207Sken					 * If the drive firmware was written
3139300207Sken					 * to an earlier spec, it won't have
3140300207Sken					 * the field.  So, assume all
3141300207Sken					 * commands are supported.
3142300207Sken					 */
3143300207Sken					softc->zone_flags |=
3144300207Sken					    ADA_ZONE_FLAG_SUP_MASK;
3145300207Sken				}
3146300207Sken
3147300207Sken			}
3148300207Sken		} else {
3149300207Sken			error = adaerror(done_ccb, CAM_RETRY_SELTO,
3150300207Sken					 SF_RETRY_UA|SF_NO_PRINT);
3151300207Sken			if (error == ERESTART)
3152300207Sken				return;
3153300207Sken			else if (error != 0) {
3154300207Sken				/*
3155300207Sken				 * If we can't get the ATA Identify Data
3156300207Sken				 * Supported Capabilities page, clear the
3157300207Sken				 * flag...
3158300207Sken				 */
3159300207Sken				softc->flags &= ~ADA_FLAG_CAN_SUPCAP;
3160300207Sken				/*
3161300207Sken				 * And clear zone capabilities.
3162300207Sken				 */
3163300207Sken				softc->zone_flags &= ~ADA_ZONE_FLAG_SUP_MASK;
3164300207Sken				if ((done_ccb->ccb_h.status &
3165300207Sken				     CAM_DEV_QFRZN) != 0) {
3166300207Sken					/* Don't wedge this device's queue */
3167300207Sken					cam_release_devq(done_ccb->ccb_h.path,
3168300207Sken							 /*relsim_flags*/0,
3169300207Sken							 /*reduction*/0,
3170300207Sken							 /*timeout*/0,
3171300207Sken							 /*getcount_only*/0);
3172300207Sken				}
3173300207Sken			}
3174300207Sken		}
3175300207Sken
3176300207Sken		free(ataio->data_ptr, M_ATADA);
3177300207Sken
3178300207Sken		if ((error == 0)
3179300207Sken		 && (softc->flags & ADA_FLAG_CAN_ZONE)) {
3180300207Sken			softc->state = ADA_STATE_ZONE;
3181300207Sken			xpt_release_ccb(done_ccb);
3182300207Sken			xpt_schedule(periph, priority);
3183300207Sken		} else
3184300207Sken			adaprobedone(periph, done_ccb);
3185300207Sken		return;
3186300207Sken	}
3187300207Sken	case ADA_CCB_ZONE: {
3188300207Sken		int error;
3189300207Sken
3190300207Sken		if ((ataio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3191300207Sken			struct ata_zoned_info_log *zi_log;
3192300207Sken			uint32_t valid_len;
3193300207Sken			size_t needed_size;
3194300207Sken
3195300207Sken			zi_log = (struct ata_zoned_info_log *)ataio->data_ptr;
3196300207Sken
3197300207Sken			valid_len = ataio->dxfer_len - ataio->resid;
3198300207Sken			needed_size = __offsetof(struct ata_zoned_info_log,
3199300207Sken			    version_info) + 1 + sizeof(zi_log->version_info);
3200300207Sken			if (valid_len >= needed_size) {
3201300207Sken				uint64_t tmpvar;
3202300207Sken
3203300207Sken				tmpvar = le64dec(zi_log->zoned_cap);
3204300207Sken				if (tmpvar & ATA_ZDI_CAP_VALID) {
3205300207Sken					if (tmpvar & ATA_ZDI_CAP_URSWRZ)
3206300207Sken						softc->zone_flags |=
3207300207Sken						    ADA_ZONE_FLAG_URSWRZ;
3208300207Sken					else
3209300207Sken						softc->zone_flags &=
3210300207Sken						    ~ADA_ZONE_FLAG_URSWRZ;
3211300207Sken				}
3212300207Sken				tmpvar = le64dec(zi_log->optimal_seq_zones);
3213300207Sken				if (tmpvar & ATA_ZDI_OPT_SEQ_VALID) {
3214300207Sken					softc->zone_flags |=
3215300207Sken					    ADA_ZONE_FLAG_OPT_SEQ_SET;
3216300207Sken					softc->optimal_seq_zones = (tmpvar &
3217300207Sken					    ATA_ZDI_OPT_SEQ_MASK);
3218300207Sken				} else {
3219300207Sken					softc->zone_flags &=
3220300207Sken					    ~ADA_ZONE_FLAG_OPT_SEQ_SET;
3221300207Sken					softc->optimal_seq_zones = 0;
3222300207Sken				}
3223300207Sken
3224300207Sken				tmpvar =le64dec(zi_log->optimal_nonseq_zones);
3225300207Sken				if (tmpvar & ATA_ZDI_OPT_NS_VALID) {
3226300207Sken					softc->zone_flags |=
3227300207Sken					    ADA_ZONE_FLAG_OPT_NONSEQ_SET;
3228300207Sken					softc->optimal_nonseq_zones =
3229300207Sken					    (tmpvar & ATA_ZDI_OPT_NS_MASK);
3230300207Sken				} else {
3231300207Sken					softc->zone_flags &=
3232300207Sken					    ~ADA_ZONE_FLAG_OPT_NONSEQ_SET;
3233300207Sken					softc->optimal_nonseq_zones = 0;
3234300207Sken				}
3235300207Sken
3236300207Sken				tmpvar = le64dec(zi_log->max_seq_req_zones);
3237300207Sken				if (tmpvar & ATA_ZDI_MAX_SEQ_VALID) {
3238300207Sken					softc->zone_flags |=
3239300207Sken					    ADA_ZONE_FLAG_MAX_SEQ_SET;
3240300207Sken					softc->max_seq_zones =
3241300207Sken					    (tmpvar & ATA_ZDI_MAX_SEQ_MASK);
3242300207Sken				} else {
3243300207Sken					softc->zone_flags &=
3244300207Sken					    ~ADA_ZONE_FLAG_MAX_SEQ_SET;
3245300207Sken					softc->max_seq_zones = 0;
3246300207Sken				}
3247300207Sken			}
3248300207Sken		} else {
3249300207Sken			error = adaerror(done_ccb, CAM_RETRY_SELTO,
3250300207Sken					 SF_RETRY_UA|SF_NO_PRINT);
3251300207Sken			if (error == ERESTART)
3252300207Sken				return;
3253300207Sken			else if (error != 0) {
3254300207Sken				softc->flags &= ~ADA_FLAG_CAN_ZONE;
3255300207Sken				softc->flags &= ~ADA_ZONE_FLAG_SET_MASK;
3256300207Sken
3257300207Sken				if ((done_ccb->ccb_h.status &
3258300207Sken				     CAM_DEV_QFRZN) != 0) {
3259300207Sken					/* Don't wedge this device's queue */
3260300207Sken					cam_release_devq(done_ccb->ccb_h.path,
3261300207Sken							 /*relsim_flags*/0,
3262300207Sken							 /*reduction*/0,
3263300207Sken							 /*timeout*/0,
3264300207Sken							 /*getcount_only*/0);
3265300207Sken				}
3266300207Sken			}
3267300207Sken
3268300207Sken		}
3269300207Sken		free(ataio->data_ptr, M_ATADA);
3270300207Sken
3271300207Sken		adaprobedone(periph, done_ccb);
3272300207Sken		return;
3273300207Sken	}
3274195534Sscottl	case ADA_CCB_DUMP:
3275195534Sscottl		/* No-op.  We're polling */
3276195534Sscottl		return;
3277195534Sscottl	default:
3278195534Sscottl		break;
3279195534Sscottl	}
3280195534Sscottl	xpt_release_ccb(done_ccb);
3281195534Sscottl}
3282195534Sscottl
3283195534Sscottlstatic int
3284195534Sscottladaerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
3285195534Sscottl{
3286298037Simp#ifdef CAM_IO_STATS
3287298002Simp	struct ada_softc *softc;
3288298002Simp	struct cam_periph *periph;
3289195534Sscottl
3290298002Simp	periph = xpt_path_periph(ccb->ccb_h.path);
3291298002Simp	softc = (struct ada_softc *)periph->softc;
3292298002Simp
3293298002Simp	switch (ccb->ccb_h.status & CAM_STATUS_MASK) {
3294298002Simp	case CAM_CMD_TIMEOUT:
3295298002Simp		softc->timeouts++;
3296298002Simp		break;
3297298002Simp	case CAM_REQ_ABORTED:
3298298002Simp	case CAM_REQ_CMP_ERR:
3299298002Simp	case CAM_REQ_TERMIO:
3300298002Simp	case CAM_UNREC_HBA_ERROR:
3301298002Simp	case CAM_DATA_RUN_ERR:
3302298002Simp	case CAM_ATA_STATUS_ERROR:
3303298002Simp		softc->errors++;
3304298002Simp		break;
3305298002Simp	default:
3306298002Simp		break;
3307298002Simp	}
3308298037Simp#endif
3309298002Simp
3310203385Smav	return(cam_periph_error(ccb, cam_flags, sense_flags, NULL));
3311195534Sscottl}
3312195534Sscottl
3313195534Sscottlstatic void
3314350805Smavadasetgeom(struct ada_softc *softc, struct ccb_getdev *cgd)
3315195534Sscottl{
3316195534Sscottl	struct disk_params *dp = &softc->params;
3317195534Sscottl	u_int64_t lbasize48;
3318195534Sscottl	u_int32_t lbasize;
3319350805Smav	u_int maxio, d_flags;
3320195534Sscottl
3321198897Smav	dp->secsize = ata_logical_sector_size(&cgd->ident_data);
3322195534Sscottl	if ((cgd->ident_data.atavalid & ATA_FLAG_54_58) &&
3323350805Smav	    cgd->ident_data.current_heads != 0 &&
3324350805Smav	    cgd->ident_data.current_sectors != 0) {
3325195534Sscottl		dp->heads = cgd->ident_data.current_heads;
3326195534Sscottl		dp->secs_per_track = cgd->ident_data.current_sectors;
3327195534Sscottl		dp->cylinders = cgd->ident_data.cylinders;
3328195534Sscottl		dp->sectors = (u_int32_t)cgd->ident_data.current_size_1 |
3329195534Sscottl			  ((u_int32_t)cgd->ident_data.current_size_2 << 16);
3330195534Sscottl	} else {
3331195534Sscottl		dp->heads = cgd->ident_data.heads;
3332195534Sscottl		dp->secs_per_track = cgd->ident_data.sectors;
3333195534Sscottl		dp->cylinders = cgd->ident_data.cylinders;
3334327849Sasomers		dp->sectors = cgd->ident_data.cylinders *
3335327849Sasomers			      (u_int32_t)(dp->heads * dp->secs_per_track);
3336195534Sscottl	}
3337195534Sscottl	lbasize = (u_int32_t)cgd->ident_data.lba_size_1 |
3338195534Sscottl		  ((u_int32_t)cgd->ident_data.lba_size_2 << 16);
3339195534Sscottl
3340195534Sscottl	/* use the 28bit LBA size if valid or bigger than the CHS mapping */
3341195534Sscottl	if (cgd->ident_data.cylinders == 16383 || dp->sectors < lbasize)
3342195534Sscottl		dp->sectors = lbasize;
3343195534Sscottl
3344195534Sscottl	/* use the 48bit LBA size if valid */
3345195534Sscottl	lbasize48 = ((u_int64_t)cgd->ident_data.lba_size48_1) |
3346195534Sscottl		    ((u_int64_t)cgd->ident_data.lba_size48_2 << 16) |
3347195534Sscottl		    ((u_int64_t)cgd->ident_data.lba_size48_3 << 32) |
3348195534Sscottl		    ((u_int64_t)cgd->ident_data.lba_size48_4 << 48);
3349195534Sscottl	if ((cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) &&
3350195534Sscottl	    lbasize48 > ATA_MAX_28BIT_LBA)
3351195534Sscottl		dp->sectors = lbasize48;
3352350805Smav
3353350805Smav	maxio = softc->cpi.maxio;		/* Honor max I/O size of SIM */
3354350805Smav	if (maxio == 0)
3355350805Smav		maxio = DFLTPHYS;	/* traditional default */
3356350805Smav	else if (maxio > MAXPHYS)
3357350805Smav		maxio = MAXPHYS;	/* for safety */
3358350805Smav	if (softc->flags & ADA_FLAG_CAN_48BIT)
3359350805Smav		maxio = min(maxio, 65536 * softc->params.secsize);
3360350805Smav	else					/* 28bit ATA command limit */
3361350805Smav		maxio = min(maxio, 256 * softc->params.secsize);
3362350805Smav	if (softc->quirks & ADA_Q_128KB)
3363350805Smav		maxio = min(maxio, 128 * 1024);
3364350805Smav	softc->disk->d_maxsize = maxio;
3365350805Smav	d_flags = DISKFLAG_DIRECT_COMPLETION | DISKFLAG_CANZONE;
3366350805Smav	if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE)
3367350805Smav		d_flags |= DISKFLAG_CANFLUSHCACHE;
3368350805Smav	if (softc->flags & ADA_FLAG_CAN_TRIM) {
3369350805Smav		d_flags |= DISKFLAG_CANDELETE;
3370350805Smav		softc->disk->d_delmaxsize = softc->params.secsize *
3371350805Smav		    ATA_DSM_RANGE_MAX * softc->trim_max_ranges;
3372350805Smav	} else if ((softc->flags & ADA_FLAG_CAN_CFA) &&
3373350805Smav	    !(softc->flags & ADA_FLAG_CAN_48BIT)) {
3374350805Smav		d_flags |= DISKFLAG_CANDELETE;
3375350805Smav		softc->disk->d_delmaxsize = 256 * softc->params.secsize;
3376350805Smav	} else
3377350805Smav		softc->disk->d_delmaxsize = maxio;
3378350805Smav	if ((softc->cpi.hba_misc & PIM_UNMAPPED) != 0) {
3379350805Smav		d_flags |= DISKFLAG_UNMAPPED_BIO;
3380350805Smav		softc->unmappedio = 1;
3381350805Smav	}
3382350805Smav	softc->disk->d_flags = d_flags;
3383350805Smav	strlcpy(softc->disk->d_descr, cgd->ident_data.model,
3384350805Smav	    MIN(sizeof(softc->disk->d_descr), sizeof(cgd->ident_data.model)));
3385350805Smav	strlcpy(softc->disk->d_ident, cgd->ident_data.serial,
3386350805Smav	    MIN(sizeof(softc->disk->d_ident), sizeof(cgd->ident_data.serial)));
3387350805Smav
3388350805Smav	softc->disk->d_sectorsize = softc->params.secsize;
3389350805Smav	softc->disk->d_mediasize = (off_t)softc->params.sectors *
3390350805Smav	    softc->params.secsize;
3391350805Smav	if (ata_physical_sector_size(&cgd->ident_data) !=
3392350805Smav	    softc->params.secsize) {
3393350805Smav		softc->disk->d_stripesize =
3394350805Smav		    ata_physical_sector_size(&cgd->ident_data);
3395350805Smav		softc->disk->d_stripeoffset = (softc->disk->d_stripesize -
3396350805Smav		    ata_logical_sector_offset(&cgd->ident_data)) %
3397350805Smav		    softc->disk->d_stripesize;
3398350805Smav	} else if (softc->quirks & ADA_Q_4K) {
3399350805Smav		softc->disk->d_stripesize = 4096;
3400350805Smav		softc->disk->d_stripeoffset = 0;
3401350805Smav	}
3402350805Smav	softc->disk->d_fwsectors = softc->params.secs_per_track;
3403350805Smav	softc->disk->d_fwheads = softc->params.heads;
3404350805Smav	ata_disk_firmware_geom_adjust(softc->disk);
3405350805Smav	softc->disk->d_rotation_rate = cgd->ident_data.media_rotation_rate;
3406195534Sscottl}
3407195534Sscottl
3408195534Sscottlstatic void
3409195534Sscottladasendorderedtag(void *arg)
3410195534Sscottl{
3411195534Sscottl	struct ada_softc *softc = arg;
3412195534Sscottl
3413195534Sscottl	if (ada_send_ordered) {
3414257054Smav		if (softc->outstanding_cmds > 0) {
3415257054Smav			if ((softc->flags & ADA_FLAG_WAS_OTAG) == 0)
3416257054Smav				softc->flags |= ADA_FLAG_NEED_OTAG;
3417257054Smav			softc->flags &= ~ADA_FLAG_WAS_OTAG;
3418195534Sscottl		}
3419195534Sscottl	}
3420195534Sscottl	/* Queue us up again */
3421195534Sscottl	callout_reset(&softc->sendordered_c,
3422230921Smav	    (ada_default_timeout * hz) / ADA_ORDEREDTAG_INTERVAL,
3423195534Sscottl	    adasendorderedtag, softc);
3424195534Sscottl}
3425195534Sscottl
3426195534Sscottl/*
3427195534Sscottl * Step through all ADA peripheral drivers, and if the device is still open,
3428195534Sscottl * sync the disk cache to physical media.
3429195534Sscottl */
3430195534Sscottlstatic void
3431220650Smavadaflush(void)
3432195534Sscottl{
3433195534Sscottl	struct cam_periph *periph;
3434195534Sscottl	struct ada_softc *softc;
3435248872Smav	union ccb *ccb;
3436236814Smav	int error;
3437195534Sscottl
3438248868Smav	CAM_PERIPH_FOREACH(periph, &adadriver) {
3439251792Smav		softc = (struct ada_softc *)periph->softc;
3440251792Smav		if (SCHEDULER_STOPPED()) {
3441251792Smav			/* If we paniced with the lock held, do not recurse. */
3442251792Smav			if (!cam_periph_owned(periph) &&
3443251792Smav			    (softc->flags & ADA_FLAG_OPEN)) {
3444251792Smav				adadump(softc->disk, NULL, 0, 0, 0);
3445251792Smav			}
3446200180Smav			continue;
3447251792Smav		}
3448195534Sscottl		cam_periph_lock(periph);
3449195534Sscottl		/*
3450195534Sscottl		 * We only sync the cache if the drive is still open, and
3451195534Sscottl		 * if the drive is capable of it..
3452195534Sscottl		 */
3453195534Sscottl		if (((softc->flags & ADA_FLAG_OPEN) == 0) ||
3454195534Sscottl		    (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) == 0) {
3455195534Sscottl			cam_periph_unlock(periph);
3456195534Sscottl			continue;
3457195534Sscottl		}
3458195534Sscottl
3459248872Smav		ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3460248872Smav		cam_fill_ataio(&ccb->ataio,
3461236814Smav				    0,
3462195534Sscottl				    adadone,
3463195534Sscottl				    CAM_DIR_NONE,
3464195534Sscottl				    0,
3465195534Sscottl				    NULL,
3466195534Sscottl				    0,
3467195534Sscottl				    ada_default_timeout*1000);
3468195534Sscottl		if (softc->flags & ADA_FLAG_CAN_48BIT)
3469248872Smav			ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0);
3470195534Sscottl		else
3471248872Smav			ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0);
3472195534Sscottl
3473248872Smav		error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0,
3474248872Smav		    /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY,
3475248872Smav		    softc->disk->d_devstat);
3476236814Smav		if (error != 0)
3477195534Sscottl			xpt_print(periph->path, "Synchronize cache failed\n");
3478249048Smav		xpt_release_ccb(ccb);
3479195534Sscottl		cam_periph_unlock(periph);
3480195534Sscottl	}
3481220650Smav}
3482214279Sbrucec
3483220650Smavstatic void
3484220650Smavadaspindown(uint8_t cmd, int flags)
3485220650Smav{
3486220650Smav	struct cam_periph *periph;
3487220650Smav	struct ada_softc *softc;
3488248872Smav	union ccb *ccb;
3489236814Smav	int error;
3490214279Sbrucec
3491248868Smav	CAM_PERIPH_FOREACH(periph, &adadriver) {
3492214279Sbrucec		/* If we paniced with lock held - not recurse here. */
3493214279Sbrucec		if (cam_periph_owned(periph))
3494214279Sbrucec			continue;
3495214279Sbrucec		cam_periph_lock(periph);
3496214279Sbrucec		softc = (struct ada_softc *)periph->softc;
3497214279Sbrucec		/*
3498214279Sbrucec		 * We only spin-down the drive if it is capable of it..
3499214279Sbrucec		 */
3500214279Sbrucec		if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) {
3501214279Sbrucec			cam_periph_unlock(periph);
3502214279Sbrucec			continue;
3503214279Sbrucec		}
3504214279Sbrucec
3505214279Sbrucec		if (bootverbose)
3506214279Sbrucec			xpt_print(periph->path, "spin-down\n");
3507214279Sbrucec
3508248872Smav		ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3509248872Smav		cam_fill_ataio(&ccb->ataio,
3510236814Smav				    0,
3511214279Sbrucec				    adadone,
3512220650Smav				    CAM_DIR_NONE | flags,
3513214279Sbrucec				    0,
3514214279Sbrucec				    NULL,
3515214279Sbrucec				    0,
3516214279Sbrucec				    ada_default_timeout*1000);
3517248872Smav		ata_28bit_cmd(&ccb->ataio, cmd, 0, 0, 0);
3518214279Sbrucec
3519248872Smav		error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0,
3520248872Smav		    /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY,
3521248872Smav		    softc->disk->d_devstat);
3522236814Smav		if (error != 0)
3523214279Sbrucec			xpt_print(periph->path, "Spin-down disk failed\n");
3524249048Smav		xpt_release_ccb(ccb);
3525214279Sbrucec		cam_periph_unlock(periph);
3526214279Sbrucec	}
3527195534Sscottl}
3528195534Sscottl
3529220650Smavstatic void
3530220650Smavadashutdown(void *arg, int howto)
3531220650Smav{
3532220650Smav
3533220650Smav	adaflush();
3534220650Smav	if (ada_spindown_shutdown != 0 &&
3535220650Smav	    (howto & (RB_HALT | RB_POWEROFF)) != 0)
3536220650Smav		adaspindown(ATA_STANDBY_IMMEDIATE, 0);
3537220650Smav}
3538220650Smav
3539220650Smavstatic void
3540220650Smavadasuspend(void *arg)
3541220650Smav{
3542220650Smav
3543220650Smav	adaflush();
3544220650Smav	if (ada_spindown_suspend != 0)
3545220650Smav		adaspindown(ATA_SLEEP, CAM_DEV_QFREEZE);
3546220650Smav}
3547220650Smav
3548220650Smavstatic void
3549220650Smavadaresume(void *arg)
3550220650Smav{
3551220650Smav	struct cam_periph *periph;
3552220650Smav	struct ada_softc *softc;
3553220650Smav
3554220650Smav	if (ada_spindown_suspend == 0)
3555220650Smav		return;
3556220650Smav
3557248868Smav	CAM_PERIPH_FOREACH(periph, &adadriver) {
3558220650Smav		cam_periph_lock(periph);
3559220650Smav		softc = (struct ada_softc *)periph->softc;
3560220650Smav		/*
3561220650Smav		 * We only spin-down the drive if it is capable of it..
3562220650Smav		 */
3563220650Smav		if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) {
3564220650Smav			cam_periph_unlock(periph);
3565220650Smav			continue;
3566220650Smav		}
3567220650Smav
3568220650Smav		if (bootverbose)
3569220650Smav			xpt_print(periph->path, "resume\n");
3570220650Smav
3571220650Smav		/*
3572220650Smav		 * Drop freeze taken due to CAM_DEV_QFREEZE flag set on
3573220650Smav		 * sleep request.
3574220650Smav		 */
3575220650Smav		cam_release_devq(periph->path,
3576220650Smav			 /*relsim_flags*/0,
3577220650Smav			 /*openings*/0,
3578220650Smav			 /*timeout*/0,
3579220650Smav			 /*getcount_only*/0);
3580220650Smav
3581220650Smav		cam_periph_unlock(periph);
3582220650Smav	}
3583220650Smav}
3584220650Smav
3585195534Sscottl#endif /* _KERNEL */
3586