ata_da.c revision 256843
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: head/sys/cam/ata/ata_da.c 256843 2013-10-21 12:00:26Z mav $");
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>
46195534Sscottl#include <sys/cons.h>
47251792Smav#include <sys/proc.h>
48214279Sbrucec#include <sys/reboot.h>
49195534Sscottl#include <geom/geom_disk.h>
50195534Sscottl#endif /* _KERNEL */
51195534Sscottl
52195534Sscottl#ifndef _KERNEL
53195534Sscottl#include <stdio.h>
54195534Sscottl#include <string.h>
55195534Sscottl#endif /* _KERNEL */
56195534Sscottl
57195534Sscottl#include <cam/cam.h>
58195534Sscottl#include <cam/cam_ccb.h>
59195534Sscottl#include <cam/cam_periph.h>
60195534Sscottl#include <cam/cam_xpt_periph.h>
61195534Sscottl#include <cam/cam_sim.h>
62195534Sscottl
63195534Sscottl#include <cam/ata/ata_all.h>
64195534Sscottl
65208349Smarius#include <machine/md_var.h>	/* geometry translation */
66208349Smarius
67195534Sscottl#ifdef _KERNEL
68195534Sscottl
69195534Sscottl#define ATA_MAX_28BIT_LBA               268435455UL
70195534Sscottl
71195534Sscottltypedef enum {
72224497Smav	ADA_STATE_RAHEAD,
73220412Smav	ADA_STATE_WCACHE,
74198708Smav	ADA_STATE_NORMAL
75195534Sscottl} ada_state;
76195534Sscottl
77195534Sscottltypedef enum {
78249199Smarius	ADA_FLAG_CAN_48BIT	= 0x0002,
79249199Smarius	ADA_FLAG_CAN_FLUSHCACHE	= 0x0004,
80249199Smarius	ADA_FLAG_CAN_NCQ	= 0x0008,
81249199Smarius	ADA_FLAG_CAN_DMA	= 0x0010,
82249199Smarius	ADA_FLAG_NEED_OTAG	= 0x0020,
83249199Smarius	ADA_FLAG_WENT_IDLE	= 0x0040,
84249199Smarius	ADA_FLAG_CAN_TRIM	= 0x0080,
85249199Smarius	ADA_FLAG_OPEN		= 0x0100,
86249199Smarius	ADA_FLAG_SCTX_INIT	= 0x0200,
87249199Smarius	ADA_FLAG_CAN_CFA        = 0x0400,
88249199Smarius	ADA_FLAG_CAN_POWERMGT   = 0x0800,
89253724Smav	ADA_FLAG_CAN_DMA48	= 0x1000,
90253724Smav	ADA_FLAG_DIRTY		= 0x2000
91195534Sscottl} ada_flags;
92195534Sscottl
93195534Sscottltypedef enum {
94222520Smav	ADA_Q_NONE		= 0x00,
95222520Smav	ADA_Q_4K		= 0x01,
96195534Sscottl} ada_quirks;
97195534Sscottl
98250792Ssmh#define ADA_Q_BIT_STRING	\
99250792Ssmh	"\020"			\
100250792Ssmh	"\0014K"
101250792Ssmh
102195534Sscottltypedef enum {
103224497Smav	ADA_CCB_RAHEAD		= 0x01,
104224497Smav	ADA_CCB_WCACHE		= 0x02,
105195534Sscottl	ADA_CCB_BUFFER_IO	= 0x03,
106195534Sscottl	ADA_CCB_DUMP		= 0x05,
107201139Smav	ADA_CCB_TRIM		= 0x06,
108195534Sscottl	ADA_CCB_TYPE_MASK	= 0x0F,
109195534Sscottl} ada_ccb_state;
110195534Sscottl
111195534Sscottl/* Offsets into our private area for storing information */
112195534Sscottl#define ccb_state	ppriv_field0
113195534Sscottl#define ccb_bp		ppriv_ptr1
114195534Sscottl
115195534Sscottlstruct disk_params {
116195534Sscottl	u_int8_t  heads;
117198897Smav	u_int8_t  secs_per_track;
118195534Sscottl	u_int32_t cylinders;
119198897Smav	u_int32_t secsize;	/* Number of bytes/logical sector */
120198897Smav	u_int64_t sectors;	/* Total number sectors */
121195534Sscottl};
122195534Sscottl
123222643Smav#define TRIM_MAX_BLOCKS	8
124249934Ssmh#define TRIM_MAX_RANGES	(TRIM_MAX_BLOCKS * ATA_DSM_BLK_RANGES)
125201139Smavstruct trim_request {
126249934Ssmh	uint8_t		data[TRIM_MAX_RANGES * ATA_DSM_RANGE_SIZE];
127256836Smav	TAILQ_HEAD(, bio) bps;
128201139Smav};
129201139Smav
130195534Sscottlstruct ada_softc {
131195534Sscottl	struct	 bio_queue_head bio_queue;
132201139Smav	struct	 bio_queue_head trim_queue;
133195534Sscottl	ada_state state;
134195534Sscottl	ada_flags flags;
135195534Sscottl	ada_quirks quirks;
136248922Ssmh	int	 sort_io_queue;
137195534Sscottl	int	 ordered_tag_count;
138195534Sscottl	int	 outstanding_cmds;
139201139Smav	int	 trim_max_ranges;
140201139Smav	int	 trim_running;
141224497Smav	int	 read_ahead;
142220454Smav	int	 write_cache;
143220454Smav#ifdef ADA_TEST_FAILURE
144220454Smav	int      force_read_error;
145220454Smav	int      force_write_error;
146220454Smav	int      periodic_read_error;
147220454Smav	int      periodic_read_count;
148220454Smav#endif
149195534Sscottl	struct	 disk_params params;
150195534Sscottl	struct	 disk *disk;
151195534Sscottl	struct task		sysctl_task;
152195534Sscottl	struct sysctl_ctx_list	sysctl_ctx;
153195534Sscottl	struct sysctl_oid	*sysctl_tree;
154195534Sscottl	struct callout		sendordered_c;
155201139Smav	struct trim_request	trim_req;
156256843Smav	int	refcount;
157195534Sscottl};
158195534Sscottl
159195534Sscottlstruct ada_quirk_entry {
160195534Sscottl	struct scsi_inquiry_pattern inq_pat;
161195534Sscottl	ada_quirks quirks;
162195534Sscottl};
163195534Sscottl
164199178Smavstatic struct ada_quirk_entry ada_quirk_table[] =
165199178Smav{
166199178Smav	{
167222520Smav		/* Hitachi Advanced Format (4k) drives */
168222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Hitachi H??????????E3*", "*" },
169222520Smav		/*quirks*/ADA_Q_4K
170222520Smav	},
171222520Smav	{
172222520Smav		/* Samsung Advanced Format (4k) drives */
173228819Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD155UI*", "*" },
174228819Smav		/*quirks*/ADA_Q_4K
175228819Smav	},
176228819Smav	{
177228819Smav		/* Samsung Advanced Format (4k) drives */
178222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD204UI*", "*" },
179222520Smav		/*quirks*/ADA_Q_4K
180222520Smav	},
181222520Smav	{
182222520Smav		/* Seagate Barracuda Green Advanced Format (4k) drives */
183222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST????DL*", "*" },
184222520Smav		/*quirks*/ADA_Q_4K
185222520Smav	},
186222520Smav	{
187228819Smav		/* Seagate Barracuda Advanced Format (4k) drives */
188228819Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST???DM*", "*" },
189228819Smav		/*quirks*/ADA_Q_4K
190228819Smav	},
191228819Smav	{
192228819Smav		/* Seagate Barracuda Advanced Format (4k) drives */
193228819Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST????DM*", "*" },
194228819Smav		/*quirks*/ADA_Q_4K
195228819Smav	},
196228819Smav	{
197222520Smav		/* Seagate Momentus Advanced Format (4k) drives */
198222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9500423AS*", "*" },
199222520Smav		/*quirks*/ADA_Q_4K
200222520Smav	},
201222520Smav	{
202222520Smav		/* Seagate Momentus Advanced Format (4k) drives */
203222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9500424AS*", "*" },
204222520Smav		/*quirks*/ADA_Q_4K
205222520Smav	},
206222520Smav	{
207222520Smav		/* Seagate Momentus Advanced Format (4k) drives */
208228819Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9640423AS*", "*" },
209228819Smav		/*quirks*/ADA_Q_4K
210228819Smav	},
211228819Smav	{
212228819Smav		/* Seagate Momentus Advanced Format (4k) drives */
213228819Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9640424AS*", "*" },
214228819Smav		/*quirks*/ADA_Q_4K
215228819Smav	},
216228819Smav	{
217228819Smav		/* Seagate Momentus Advanced Format (4k) drives */
218222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750420AS*", "*" },
219222520Smav		/*quirks*/ADA_Q_4K
220222520Smav	},
221222520Smav	{
222222520Smav		/* Seagate Momentus Advanced Format (4k) drives */
223222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750422AS*", "*" },
224222520Smav		/*quirks*/ADA_Q_4K
225222520Smav	},
226222520Smav	{
227228819Smav		/* Seagate Momentus Advanced Format (4k) drives */
228228819Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750423AS*", "*" },
229228819Smav		/*quirks*/ADA_Q_4K
230228819Smav	},
231228819Smav	{
232222520Smav		/* Seagate Momentus Thin Advanced Format (4k) drives */
233222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST???LT*", "*" },
234222520Smav		/*quirks*/ADA_Q_4K
235222520Smav	},
236222520Smav	{
237222520Smav		/* WDC Caviar Green Advanced Format (4k) drives */
238222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RS*", "*" },
239222520Smav		/*quirks*/ADA_Q_4K
240222520Smav	},
241222520Smav	{
242222520Smav		/* WDC Caviar Green Advanced Format (4k) drives */
243222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RX*", "*" },
244222520Smav		/*quirks*/ADA_Q_4K
245222520Smav	},
246222520Smav	{
247222520Smav		/* WDC Caviar Green Advanced Format (4k) drives */
248222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RS*", "*" },
249222520Smav		/*quirks*/ADA_Q_4K
250222520Smav	},
251222520Smav	{
252222520Smav		/* WDC Caviar Green Advanced Format (4k) drives */
253222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RX*", "*" },
254222520Smav		/*quirks*/ADA_Q_4K
255222520Smav	},
256222520Smav	{
257222520Smav		/* WDC Scorpio Black Advanced Format (4k) drives */
258222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PKT*", "*" },
259222520Smav		/*quirks*/ADA_Q_4K
260222520Smav	},
261222520Smav	{
262222520Smav		/* WDC Scorpio Black Advanced Format (4k) drives */
263222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PKT*", "*" },
264222520Smav		/*quirks*/ADA_Q_4K
265222520Smav	},
266222520Smav	{
267222520Smav		/* WDC Scorpio Blue Advanced Format (4k) drives */
268222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PVT*", "*" },
269222520Smav		/*quirks*/ADA_Q_4K
270222520Smav	},
271222520Smav	{
272222520Smav		/* WDC Scorpio Blue Advanced Format (4k) drives */
273222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PVT*", "*" },
274222520Smav		/*quirks*/ADA_Q_4K
275222520Smav	},
276251061Ssmh	/* SSDs */
277222520Smav	{
278241784Seadler		/*
279241784Seadler		 * Corsair Force 2 SSDs
280241784Seadler		 * 4k optimised & trim only works in 4k requests + 4k aligned
281241784Seadler		 */
282241784Seadler		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair CSSD-F*", "*" },
283241784Seadler		/*quirks*/ADA_Q_4K
284241784Seadler	},
285241784Seadler	{
286241784Seadler		/*
287241784Seadler		 * Corsair Force 3 SSDs
288241784Seadler		 * 4k optimised & trim only works in 4k requests + 4k aligned
289241784Seadler		 */
290241784Seadler		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Force 3*", "*" },
291241784Seadler		/*quirks*/ADA_Q_4K
292241784Seadler	},
293241784Seadler	{
294241784Seadler		/*
295256547Ssmh		 * Corsair Neutron GTX SSDs
296256547Ssmh		 * 4k optimised & trim only works in 4k requests + 4k aligned
297256547Ssmh		 */
298256547Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Neutron GTX*", "*" },
299256547Ssmh		/*quirks*/ADA_Q_4K
300256547Ssmh	},
301256547Ssmh	{
302256547Ssmh		/*
303251061Ssmh		 * Corsair Force GT SSDs
304241784Seadler		 * 4k optimised & trim only works in 4k requests + 4k aligned
305241784Seadler		 */
306251061Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Force GT*", "*" },
307241784Seadler		/*quirks*/ADA_Q_4K
308241784Seadler	},
309241784Seadler	{
310241784Seadler		/*
311251061Ssmh		 * Crucial M4 SSDs
312241784Seadler		 * 4k optimised & trim only works in 4k requests + 4k aligned
313241784Seadler		 */
314251061Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "M4-CT???M4SSD2*", "*" },
315241784Seadler		/*quirks*/ADA_Q_4K
316241784Seadler	},
317241784Seadler	{
318241784Seadler		/*
319251061Ssmh		 * Crucial RealSSD C300 SSDs
320251061Ssmh		 * 4k optimised
321251061Ssmh		 */
322251061Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "C300-CTFDDAC???MAG*",
323251061Ssmh		"*" }, /*quirks*/ADA_Q_4K
324251061Ssmh	},
325251061Ssmh	{
326251061Ssmh		/*
327251061Ssmh		 * Intel 320 Series SSDs
328241784Seadler		 * 4k optimised & trim only works in 4k requests + 4k aligned
329241784Seadler		 */
330251061Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSA2CW*", "*" },
331241784Seadler		/*quirks*/ADA_Q_4K
332241784Seadler	},
333241784Seadler	{
334241784Seadler		/*
335251061Ssmh		 * Intel 330 Series SSDs
336241784Seadler		 * 4k optimised & trim only works in 4k requests + 4k aligned
337241784Seadler		 */
338251061Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2CT*", "*" },
339241784Seadler		/*quirks*/ADA_Q_4K
340241784Seadler	},
341241784Seadler	{
342241784Seadler		/*
343251061Ssmh		 * Intel 510 Series SSDs
344251061Ssmh		 * 4k optimised & trim only works in 4k requests + 4k aligned
345241784Seadler		 */
346251061Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2MH*", "*" },
347251061Ssmh		/*quirks*/ADA_Q_4K
348241784Seadler	},
349241784Seadler	{
350241784Seadler		/*
351251061Ssmh		 * Intel 520 Series SSDs
352251061Ssmh		 * 4k optimised & trim only works in 4k requests + 4k aligned
353241784Seadler		 */
354251061Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2BW*", "*" },
355241784Seadler		/*quirks*/ADA_Q_4K
356241784Seadler	},
357241784Seadler	{
358241784Seadler		/*
359254329Ssmh		 * Intel X25-M Series SSDs
360254329Ssmh		 * 4k optimised & trim only works in 4k requests + 4k aligned
361254329Ssmh		 */
362254329Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSA2M*", "*" },
363254329Ssmh		/*quirks*/ADA_Q_4K
364254329Ssmh	},
365254329Ssmh	{
366254329Ssmh		/*
367251061Ssmh		 * Kingston E100 Series SSDs
368250532Seadler		 * 4k optimised & trim only works in 4k requests + 4k aligned
369250532Seadler		 */
370251061Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "KINGSTON SE100S3*", "*" },
371250532Seadler		/*quirks*/ADA_Q_4K
372250532Seadler	},
373250532Seadler	{
374250532Seadler		/*
375251061Ssmh		 * Kingston HyperX 3k SSDs
376241784Seadler		 * 4k optimised & trim only works in 4k requests + 4k aligned
377241784Seadler		 */
378251061Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "KINGSTON SH103S3*", "*" },
379241784Seadler		/*quirks*/ADA_Q_4K
380241784Seadler	},
381241784Seadler	{
382241784Seadler		/*
383254329Ssmh		 * Marvell SSDs (entry taken from OpenSolaris)
384254329Ssmh		 * 4k optimised & trim only works in 4k requests + 4k aligned
385254329Ssmh		 */
386254329Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "MARVELL SD88SA02*", "*" },
387254329Ssmh		/*quirks*/ADA_Q_4K
388254329Ssmh	},
389254329Ssmh	{
390254329Ssmh		/*
391254329Ssmh		 * OCZ Agility 2 SSDs
392254329Ssmh		 * 4k optimised & trim only works in 4k requests + 4k aligned
393254329Ssmh		 */
394254329Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-AGILITY2*", "*" },
395254329Ssmh		/*quirks*/ADA_Q_4K
396254329Ssmh	},
397254329Ssmh	{
398254329Ssmh		/*
399251061Ssmh		 * OCZ Agility 3 SSDs
400250532Seadler		 * 4k optimised & trim only works in 4k requests + 4k aligned
401250532Seadler		 */
402251061Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-AGILITY3*", "*" },
403250532Seadler		/*quirks*/ADA_Q_4K
404250532Seadler	},
405250532Seadler	{
406250532Seadler		/*
407241784Seadler		 * OCZ Deneva R Series SSDs
408241784Seadler		 * 4k optimised & trim only works in 4k requests + 4k aligned
409241784Seadler		 */
410241784Seadler		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "DENRSTE251M45*", "*" },
411241784Seadler		/*quirks*/ADA_Q_4K
412241784Seadler	},
413241784Seadler	{
414241784Seadler		/*
415251061Ssmh		 * OCZ Vertex 2 SSDs (inc pro series)
416241784Seadler		 * 4k optimised & trim only works in 4k requests + 4k aligned
417241784Seadler		 */
418251061Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ?VERTEX2*", "*" },
419241784Seadler		/*quirks*/ADA_Q_4K
420241784Seadler	},
421241784Seadler	{
422251061Ssmh		/*
423251061Ssmh		 * OCZ Vertex 3 SSDs
424251061Ssmh		 * 4k optimised & trim only works in 4k requests + 4k aligned
425251061Ssmh		 */
426251061Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-VERTEX3*", "*" },
427251061Ssmh		/*quirks*/ADA_Q_4K
428251061Ssmh	},
429251061Ssmh	{
430251061Ssmh		/*
431253091Ssmh		 * OCZ Vertex 4 SSDs
432253091Ssmh		 * 4k optimised & trim only works in 4k requests + 4k aligned
433253091Ssmh		 */
434253091Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-VERTEX4*", "*" },
435253091Ssmh		/*quirks*/ADA_Q_4K
436253091Ssmh	},
437253091Ssmh	{
438253091Ssmh		/*
439251061Ssmh		 * Samsung 830 Series SSDs
440251061Ssmh		 * 4k optimised
441251061Ssmh		 */
442251061Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG SSD 830 Series*", "*" },
443251061Ssmh		/*quirks*/ADA_Q_4K
444251061Ssmh	},
445251061Ssmh	{
446251061Ssmh		/*
447251061Ssmh		 * SuperTalent TeraDrive CT SSDs
448251061Ssmh		 * 4k optimised & trim only works in 4k requests + 4k aligned
449251061Ssmh		 */
450251061Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "FTM??CT25H*", "*" },
451251061Ssmh		/*quirks*/ADA_Q_4K
452251061Ssmh	},
453251061Ssmh	{
454251061Ssmh		/*
455251061Ssmh		 * XceedIOPS SATA SSDs
456251061Ssmh		 * 4k optimised
457251061Ssmh		 */
458251061Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "SG9XCS2D*", "*" },
459251061Ssmh		/*quirks*/ADA_Q_4K
460251061Ssmh	},
461251061Ssmh	{
462199178Smav		/* Default */
463199178Smav		{
464199178Smav		  T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
465199178Smav		  /*vendor*/"*", /*product*/"*", /*revision*/"*"
466199178Smav		},
467199178Smav		/*quirks*/0
468199178Smav	},
469199178Smav};
470195534Sscottl
471195534Sscottlstatic	disk_strategy_t	adastrategy;
472195534Sscottlstatic	dumper_t	adadump;
473195534Sscottlstatic	periph_init_t	adainit;
474195534Sscottlstatic	void		adaasync(void *callback_arg, u_int32_t code,
475195534Sscottl				struct cam_path *path, void *arg);
476195534Sscottlstatic	void		adasysctlinit(void *context, int pending);
477195534Sscottlstatic	periph_ctor_t	adaregister;
478195534Sscottlstatic	periph_dtor_t	adacleanup;
479195534Sscottlstatic	periph_start_t	adastart;
480195534Sscottlstatic	periph_oninv_t	adaoninvalidate;
481195534Sscottlstatic	void		adadone(struct cam_periph *periph,
482195534Sscottl			       union ccb *done_ccb);
483195534Sscottlstatic  int		adaerror(union ccb *ccb, u_int32_t cam_flags,
484195534Sscottl				u_int32_t sense_flags);
485198897Smavstatic void		adagetparams(struct cam_periph *periph,
486195534Sscottl				struct ccb_getdev *cgd);
487195534Sscottlstatic timeout_t	adasendorderedtag;
488195534Sscottlstatic void		adashutdown(void *arg, int howto);
489220650Smavstatic void		adasuspend(void *arg);
490220650Smavstatic void		adaresume(void *arg);
491195534Sscottl
492221071Smav#ifndef	ADA_DEFAULT_LEGACY_ALIASES
493221071Smav#define	ADA_DEFAULT_LEGACY_ALIASES	1
494221071Smav#endif
495221071Smav
496195534Sscottl#ifndef ADA_DEFAULT_TIMEOUT
497195534Sscottl#define ADA_DEFAULT_TIMEOUT 30	/* Timeout in seconds */
498195534Sscottl#endif
499195534Sscottl
500195534Sscottl#ifndef	ADA_DEFAULT_RETRY
501195534Sscottl#define	ADA_DEFAULT_RETRY	4
502195534Sscottl#endif
503195534Sscottl
504195534Sscottl#ifndef	ADA_DEFAULT_SEND_ORDERED
505195534Sscottl#define	ADA_DEFAULT_SEND_ORDERED	1
506195534Sscottl#endif
507195534Sscottl
508214279Sbrucec#ifndef	ADA_DEFAULT_SPINDOWN_SHUTDOWN
509214279Sbrucec#define	ADA_DEFAULT_SPINDOWN_SHUTDOWN	1
510214279Sbrucec#endif
511214279Sbrucec
512220650Smav#ifndef	ADA_DEFAULT_SPINDOWN_SUSPEND
513220650Smav#define	ADA_DEFAULT_SPINDOWN_SUSPEND	1
514220650Smav#endif
515220650Smav
516224497Smav#ifndef	ADA_DEFAULT_READ_AHEAD
517224497Smav#define	ADA_DEFAULT_READ_AHEAD	1
518224497Smav#endif
519224497Smav
520220412Smav#ifndef	ADA_DEFAULT_WRITE_CACHE
521220412Smav#define	ADA_DEFAULT_WRITE_CACHE	1
522220412Smav#endif
523220412Smav
524224497Smav#define	ADA_RA	(softc->read_ahead >= 0 ? \
525224497Smav		 softc->read_ahead : ada_read_ahead)
526224497Smav#define	ADA_WC	(softc->write_cache >= 0 ? \
527224497Smav		 softc->write_cache : ada_write_cache)
528248922Ssmh#define	ADA_SIO	(softc->sort_io_queue >= 0 ? \
529248922Ssmh		 softc->sort_io_queue : cam_sort_io_queues)
530224497Smav
531208349Smarius/*
532208349Smarius * Most platforms map firmware geometry to actual, but some don't.  If
533208349Smarius * not overridden, default to nothing.
534208349Smarius */
535208349Smarius#ifndef ata_disk_firmware_geom_adjust
536208349Smarius#define	ata_disk_firmware_geom_adjust(disk)
537208349Smarius#endif
538195534Sscottl
539221071Smavstatic int ada_legacy_aliases = ADA_DEFAULT_LEGACY_ALIASES;
540195534Sscottlstatic int ada_retry_count = ADA_DEFAULT_RETRY;
541195534Sscottlstatic int ada_default_timeout = ADA_DEFAULT_TIMEOUT;
542195534Sscottlstatic int ada_send_ordered = ADA_DEFAULT_SEND_ORDERED;
543214279Sbrucecstatic int ada_spindown_shutdown = ADA_DEFAULT_SPINDOWN_SHUTDOWN;
544220650Smavstatic int ada_spindown_suspend = ADA_DEFAULT_SPINDOWN_SUSPEND;
545224497Smavstatic int ada_read_ahead = ADA_DEFAULT_READ_AHEAD;
546220412Smavstatic int ada_write_cache = ADA_DEFAULT_WRITE_CACHE;
547195534Sscottl
548227309Sedstatic SYSCTL_NODE(_kern_cam, OID_AUTO, ada, CTLFLAG_RD, 0,
549195534Sscottl            "CAM Direct Access Disk driver");
550221071SmavSYSCTL_INT(_kern_cam_ada, OID_AUTO, legacy_aliases, CTLFLAG_RW,
551221071Smav           &ada_legacy_aliases, 0, "Create legacy-like device aliases");
552221071SmavTUNABLE_INT("kern.cam.ada.legacy_aliases", &ada_legacy_aliases);
553195534SscottlSYSCTL_INT(_kern_cam_ada, OID_AUTO, retry_count, CTLFLAG_RW,
554195534Sscottl           &ada_retry_count, 0, "Normal I/O retry count");
555195534SscottlTUNABLE_INT("kern.cam.ada.retry_count", &ada_retry_count);
556195534SscottlSYSCTL_INT(_kern_cam_ada, OID_AUTO, default_timeout, CTLFLAG_RW,
557195534Sscottl           &ada_default_timeout, 0, "Normal I/O timeout (in seconds)");
558195534SscottlTUNABLE_INT("kern.cam.ada.default_timeout", &ada_default_timeout);
559238382SbruefferSYSCTL_INT(_kern_cam_ada, OID_AUTO, send_ordered, CTLFLAG_RW,
560195534Sscottl           &ada_send_ordered, 0, "Send Ordered Tags");
561238382SbruefferTUNABLE_INT("kern.cam.ada.send_ordered", &ada_send_ordered);
562214279SbrucecSYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_shutdown, CTLFLAG_RW,
563214279Sbrucec           &ada_spindown_shutdown, 0, "Spin down upon shutdown");
564214279SbrucecTUNABLE_INT("kern.cam.ada.spindown_shutdown", &ada_spindown_shutdown);
565220650SmavSYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_suspend, CTLFLAG_RW,
566220650Smav           &ada_spindown_suspend, 0, "Spin down upon suspend");
567220650SmavTUNABLE_INT("kern.cam.ada.spindown_suspend", &ada_spindown_suspend);
568224497SmavSYSCTL_INT(_kern_cam_ada, OID_AUTO, read_ahead, CTLFLAG_RW,
569224497Smav           &ada_read_ahead, 0, "Enable disk read-ahead");
570224497SmavTUNABLE_INT("kern.cam.ada.read_ahead", &ada_read_ahead);
571220412SmavSYSCTL_INT(_kern_cam_ada, OID_AUTO, write_cache, CTLFLAG_RW,
572220412Smav           &ada_write_cache, 0, "Enable disk write cache");
573220412SmavTUNABLE_INT("kern.cam.ada.write_cache", &ada_write_cache);
574195534Sscottl
575195534Sscottl/*
576195534Sscottl * ADA_ORDEREDTAG_INTERVAL determines how often, relative
577195534Sscottl * to the default timeout, we check to see whether an ordered
578195534Sscottl * tagged transaction is appropriate to prevent simple tag
579195534Sscottl * starvation.  Since we'd like to ensure that there is at least
580195534Sscottl * 1/2 of the timeout length left for a starved transaction to
581195534Sscottl * complete after we've sent an ordered tag, we must poll at least
582195534Sscottl * four times in every timeout period.  This takes care of the worst
583195534Sscottl * case where a starved transaction starts during an interval that
584195534Sscottl * meets the requirement "don't send an ordered tag" test so it takes
585195534Sscottl * us two intervals to determine that a tag must be sent.
586195534Sscottl */
587195534Sscottl#ifndef ADA_ORDEREDTAG_INTERVAL
588195534Sscottl#define ADA_ORDEREDTAG_INTERVAL 4
589195534Sscottl#endif
590195534Sscottl
591195534Sscottlstatic struct periph_driver adadriver =
592195534Sscottl{
593195534Sscottl	adainit, "ada",
594195534Sscottl	TAILQ_HEAD_INITIALIZER(adadriver.units), /* generation */ 0
595195534Sscottl};
596195534Sscottl
597195534SscottlPERIPHDRIVER_DECLARE(ada, adadriver);
598195534Sscottl
599227293Sedstatic MALLOC_DEFINE(M_ATADA, "ata_da", "ata_da buffers");
600195534Sscottl
601195534Sscottlstatic int
602195534Sscottladaopen(struct disk *dp)
603195534Sscottl{
604195534Sscottl	struct cam_periph *periph;
605195534Sscottl	struct ada_softc *softc;
606195534Sscottl	int error;
607195534Sscottl
608195534Sscottl	periph = (struct cam_periph *)dp->d_drv1;
609195534Sscottl	if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
610195534Sscottl		return(ENXIO);
611195534Sscottl	}
612195534Sscottl
613195534Sscottl	cam_periph_lock(periph);
614195534Sscottl	if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
615195534Sscottl		cam_periph_unlock(periph);
616195534Sscottl		cam_periph_release(periph);
617195534Sscottl		return (error);
618195534Sscottl	}
619195534Sscottl
620236602Smav	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
621236602Smav	    ("adaopen\n"));
622195534Sscottl
623249981Smav	softc = (struct ada_softc *)periph->softc;
624249981Smav	softc->flags |= ADA_FLAG_OPEN;
625195534Sscottl
626195534Sscottl	cam_periph_unhold(periph);
627195534Sscottl	cam_periph_unlock(periph);
628195534Sscottl	return (0);
629195534Sscottl}
630195534Sscottl
631195534Sscottlstatic int
632195534Sscottladaclose(struct disk *dp)
633195534Sscottl{
634195534Sscottl	struct	cam_periph *periph;
635195534Sscottl	struct	ada_softc *softc;
636195534Sscottl	union ccb *ccb;
637253724Smav	int error;
638195534Sscottl
639195534Sscottl	periph = (struct cam_periph *)dp->d_drv1;
640256843Smav	softc = (struct ada_softc *)periph->softc;
641195534Sscottl	cam_periph_lock(periph);
642195534Sscottl
643236602Smav	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
644236602Smav	    ("adaclose\n"));
645236602Smav
646195534Sscottl	/* We only sync the cache if the drive is capable of it. */
647253724Smav	if ((softc->flags & ADA_FLAG_DIRTY) != 0 &&
648253724Smav	    (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) != 0 &&
649256843Smav	    (periph->flags & CAM_PERIPH_INVALID) == 0 &&
650256843Smav	    cam_periph_hold(periph, PRIBIO) == 0) {
651195534Sscottl
652198382Smav		ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
653195534Sscottl		cam_fill_ataio(&ccb->ataio,
654195534Sscottl				    1,
655195534Sscottl				    adadone,
656195534Sscottl				    CAM_DIR_NONE,
657195534Sscottl				    0,
658195534Sscottl				    NULL,
659195534Sscottl				    0,
660195534Sscottl				    ada_default_timeout*1000);
661195534Sscottl
662195534Sscottl		if (softc->flags & ADA_FLAG_CAN_48BIT)
663195534Sscottl			ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0);
664195534Sscottl		else
665196659Smav			ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0);
666253724Smav		error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0,
667198328Smav		    /*sense_flags*/0, softc->disk->d_devstat);
668195534Sscottl
669253724Smav		if (error != 0)
670195534Sscottl			xpt_print(periph->path, "Synchronize cache failed\n");
671253724Smav		else
672253724Smav			softc->flags &= ~ADA_FLAG_DIRTY;
673195534Sscottl		xpt_release_ccb(ccb);
674256843Smav		cam_periph_unhold(periph);
675195534Sscottl	}
676195534Sscottl
677195534Sscottl	softc->flags &= ~ADA_FLAG_OPEN;
678256843Smav
679256843Smav	while (softc->refcount != 0)
680256843Smav		cam_periph_sleep(periph, &softc->refcount, PRIBIO, "adaclose", 1);
681195534Sscottl	cam_periph_unlock(periph);
682195534Sscottl	cam_periph_release(periph);
683195534Sscottl	return (0);
684195534Sscottl}
685195534Sscottl
686201139Smavstatic void
687201139Smavadaschedule(struct cam_periph *periph)
688201139Smav{
689201139Smav	struct ada_softc *softc = (struct ada_softc *)periph->softc;
690201139Smav
691249205Smav	if (softc->state != ADA_STATE_NORMAL)
692249205Smav		return;
693249205Smav
694224531Smav	/* Check if we have more work to do. */
695201139Smav	if (bioq_first(&softc->bio_queue) ||
696201139Smav	    (!softc->trim_running && bioq_first(&softc->trim_queue))) {
697256843Smav		xpt_schedule(periph, CAM_PRIORITY_NORMAL);
698201139Smav	}
699201139Smav}
700201139Smav
701195534Sscottl/*
702195534Sscottl * Actually translate the requested transfer into one the physical driver
703195534Sscottl * can understand.  The transfer is described by a buf and will include
704195534Sscottl * only one physical transfer.
705195534Sscottl */
706195534Sscottlstatic void
707195534Sscottladastrategy(struct bio *bp)
708195534Sscottl{
709195534Sscottl	struct cam_periph *periph;
710195534Sscottl	struct ada_softc *softc;
711195534Sscottl
712195534Sscottl	periph = (struct cam_periph *)bp->bio_disk->d_drv1;
713195534Sscottl	softc = (struct ada_softc *)periph->softc;
714195534Sscottl
715195534Sscottl	cam_periph_lock(periph);
716195534Sscottl
717236602Smav	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adastrategy(%p)\n", bp));
718236602Smav
719195534Sscottl	/*
720195534Sscottl	 * If the device has been made invalid, error out
721195534Sscottl	 */
722249981Smav	if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
723195534Sscottl		cam_periph_unlock(periph);
724195534Sscottl		biofinish(bp, NULL, ENXIO);
725195534Sscottl		return;
726195534Sscottl	}
727195534Sscottl
728195534Sscottl	/*
729195534Sscottl	 * Place it in the queue of disk activities for this disk
730195534Sscottl	 */
731201139Smav	if (bp->bio_cmd == BIO_DELETE &&
732248922Ssmh	    (softc->flags & ADA_FLAG_CAN_TRIM)) {
733248922Ssmh		if (ADA_SIO)
734248922Ssmh		    bioq_disksort(&softc->trim_queue, bp);
735248922Ssmh		else
736248922Ssmh		    bioq_insert_tail(&softc->trim_queue, bp);
737248922Ssmh	} else {
738248922Ssmh		if (ADA_SIO)
739248922Ssmh		    bioq_disksort(&softc->bio_queue, bp);
740248922Ssmh		else
741248922Ssmh		    bioq_insert_tail(&softc->bio_queue, bp);
742248922Ssmh	}
743195534Sscottl
744195534Sscottl	/*
745195534Sscottl	 * Schedule ourselves for performing the work.
746195534Sscottl	 */
747201139Smav	adaschedule(periph);
748195534Sscottl	cam_periph_unlock(periph);
749195534Sscottl
750195534Sscottl	return;
751195534Sscottl}
752195534Sscottl
753195534Sscottlstatic int
754195534Sscottladadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
755195534Sscottl{
756195534Sscottl	struct	    cam_periph *periph;
757195534Sscottl	struct	    ada_softc *softc;
758195534Sscottl	u_int	    secsize;
759195534Sscottl	union	    ccb ccb;
760195534Sscottl	struct	    disk *dp;
761195534Sscottl	uint64_t    lba;
762195534Sscottl	uint16_t    count;
763236814Smav	int	    error = 0;
764195534Sscottl
765195534Sscottl	dp = arg;
766195534Sscottl	periph = dp->d_drv1;
767195534Sscottl	softc = (struct ada_softc *)periph->softc;
768195534Sscottl	cam_periph_lock(periph);
769195534Sscottl	secsize = softc->params.secsize;
770195534Sscottl	lba = offset / secsize;
771195534Sscottl	count = length / secsize;
772195534Sscottl
773249981Smav	if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
774195534Sscottl		cam_periph_unlock(periph);
775195534Sscottl		return (ENXIO);
776195534Sscottl	}
777195534Sscottl
778195534Sscottl	if (length > 0) {
779198382Smav		xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
780195534Sscottl		ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
781195534Sscottl		cam_fill_ataio(&ccb.ataio,
782195534Sscottl		    0,
783195534Sscottl		    adadone,
784195534Sscottl		    CAM_DIR_OUT,
785195534Sscottl		    0,
786195534Sscottl		    (u_int8_t *) virtual,
787195534Sscottl		    length,
788195534Sscottl		    ada_default_timeout*1000);
789195534Sscottl		if ((softc->flags & ADA_FLAG_CAN_48BIT) &&
790195534Sscottl		    (lba + count >= ATA_MAX_28BIT_LBA ||
791195534Sscottl		    count >= 256)) {
792195534Sscottl			ata_48bit_cmd(&ccb.ataio, ATA_WRITE_DMA48,
793195534Sscottl			    0, lba, count);
794195534Sscottl		} else {
795196659Smav			ata_28bit_cmd(&ccb.ataio, ATA_WRITE_DMA,
796195534Sscottl			    0, lba, count);
797195534Sscottl		}
798195534Sscottl		xpt_polled_action(&ccb);
799195534Sscottl
800236814Smav		error = cam_periph_error(&ccb,
801236814Smav		    0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
802236814Smav		if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
803236814Smav			cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0,
804236814Smav			    /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
805236814Smav		if (error != 0)
806195534Sscottl			printf("Aborting dump due to I/O error.\n");
807236814Smav
808195534Sscottl		cam_periph_unlock(periph);
809236814Smav		return (error);
810195534Sscottl	}
811195534Sscottl
812195534Sscottl	if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) {
813198382Smav		xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
814195534Sscottl
815195534Sscottl		ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
816195534Sscottl		cam_fill_ataio(&ccb.ataio,
817236814Smav				    0,
818195534Sscottl				    adadone,
819195534Sscottl				    CAM_DIR_NONE,
820195534Sscottl				    0,
821195534Sscottl				    NULL,
822195534Sscottl				    0,
823195534Sscottl				    ada_default_timeout*1000);
824195534Sscottl
825195534Sscottl		if (softc->flags & ADA_FLAG_CAN_48BIT)
826195534Sscottl			ata_48bit_cmd(&ccb.ataio, ATA_FLUSHCACHE48, 0, 0, 0);
827195534Sscottl		else
828196659Smav			ata_28bit_cmd(&ccb.ataio, ATA_FLUSHCACHE, 0, 0, 0);
829195534Sscottl		xpt_polled_action(&ccb);
830195534Sscottl
831236814Smav		error = cam_periph_error(&ccb,
832236814Smav		    0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
833236814Smav		if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
834236814Smav			cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0,
835236814Smav			    /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
836236814Smav		if (error != 0)
837195534Sscottl			xpt_print(periph->path, "Synchronize cache failed\n");
838195534Sscottl	}
839195534Sscottl	cam_periph_unlock(periph);
840236814Smav	return (error);
841195534Sscottl}
842195534Sscottl
843195534Sscottlstatic void
844195534Sscottladainit(void)
845195534Sscottl{
846195534Sscottl	cam_status status;
847195534Sscottl
848195534Sscottl	/*
849195534Sscottl	 * Install a global async callback.  This callback will
850195534Sscottl	 * receive async callbacks like "new device found".
851195534Sscottl	 */
852195534Sscottl	status = xpt_register_async(AC_FOUND_DEVICE, adaasync, NULL, NULL);
853195534Sscottl
854195534Sscottl	if (status != CAM_REQ_CMP) {
855195534Sscottl		printf("ada: Failed to attach master async callback "
856195534Sscottl		       "due to status 0x%x!\n", status);
857195534Sscottl	} else if (ada_send_ordered) {
858195534Sscottl
859220650Smav		/* Register our event handlers */
860220650Smav		if ((EVENTHANDLER_REGISTER(power_suspend, adasuspend,
861220650Smav					   NULL, EVENTHANDLER_PRI_LAST)) == NULL)
862220650Smav		    printf("adainit: power event registration failed!\n");
863220650Smav		if ((EVENTHANDLER_REGISTER(power_resume, adaresume,
864220650Smav					   NULL, EVENTHANDLER_PRI_LAST)) == NULL)
865220650Smav		    printf("adainit: power event registration failed!\n");
866220650Smav		if ((EVENTHANDLER_REGISTER(shutdown_post_sync, adashutdown,
867195534Sscottl					   NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
868195534Sscottl		    printf("adainit: shutdown event registration failed!\n");
869195534Sscottl	}
870195534Sscottl}
871195534Sscottl
872249347Sken/*
873249347Sken * Callback from GEOM, called when it has finished cleaning up its
874249347Sken * resources.
875249347Sken */
876195534Sscottlstatic void
877249347Skenadadiskgonecb(struct disk *dp)
878249347Sken{
879249347Sken	struct cam_periph *periph;
880249347Sken
881249347Sken	periph = (struct cam_periph *)dp->d_drv1;
882249347Sken
883249347Sken	cam_periph_release(periph);
884249347Sken}
885249347Sken
886249347Skenstatic void
887195534Sscottladaoninvalidate(struct cam_periph *periph)
888195534Sscottl{
889195534Sscottl	struct ada_softc *softc;
890195534Sscottl
891195534Sscottl	softc = (struct ada_softc *)periph->softc;
892195534Sscottl
893195534Sscottl	/*
894195534Sscottl	 * De-register any async callbacks.
895195534Sscottl	 */
896195534Sscottl	xpt_register_async(0, adaasync, periph, periph->path);
897195534Sscottl
898195534Sscottl	/*
899195534Sscottl	 * Return all queued I/O with ENXIO.
900195534Sscottl	 * XXX Handle any transactions queued to the card
901195534Sscottl	 *     with XPT_ABORT_CCB.
902195534Sscottl	 */
903195534Sscottl	bioq_flush(&softc->bio_queue, NULL, ENXIO);
904201139Smav	bioq_flush(&softc->trim_queue, NULL, ENXIO);
905195534Sscottl
906195534Sscottl	disk_gone(softc->disk);
907195534Sscottl}
908195534Sscottl
909195534Sscottlstatic void
910195534Sscottladacleanup(struct cam_periph *periph)
911195534Sscottl{
912195534Sscottl	struct ada_softc *softc;
913195534Sscottl
914195534Sscottl	softc = (struct ada_softc *)periph->softc;
915195534Sscottl
916195534Sscottl	cam_periph_unlock(periph);
917195534Sscottl
918195534Sscottl	/*
919195534Sscottl	 * If we can't free the sysctl tree, oh well...
920195534Sscottl	 */
921195534Sscottl	if ((softc->flags & ADA_FLAG_SCTX_INIT) != 0
922195534Sscottl	    && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
923195534Sscottl		xpt_print(periph->path, "can't remove sysctl context\n");
924195534Sscottl	}
925195534Sscottl
926195534Sscottl	disk_destroy(softc->disk);
927195534Sscottl	callout_drain(&softc->sendordered_c);
928195534Sscottl	free(softc, M_DEVBUF);
929195534Sscottl	cam_periph_lock(periph);
930195534Sscottl}
931195534Sscottl
932195534Sscottlstatic void
933195534Sscottladaasync(void *callback_arg, u_int32_t code,
934195534Sscottl	struct cam_path *path, void *arg)
935195534Sscottl{
936236393Smav	struct ccb_getdev cgd;
937195534Sscottl	struct cam_periph *periph;
938220412Smav	struct ada_softc *softc;
939195534Sscottl
940195534Sscottl	periph = (struct cam_periph *)callback_arg;
941195534Sscottl	switch (code) {
942195534Sscottl	case AC_FOUND_DEVICE:
943195534Sscottl	{
944195534Sscottl		struct ccb_getdev *cgd;
945195534Sscottl		cam_status status;
946195534Sscottl
947195534Sscottl		cgd = (struct ccb_getdev *)arg;
948195534Sscottl		if (cgd == NULL)
949195534Sscottl			break;
950195534Sscottl
951195534Sscottl		if (cgd->protocol != PROTO_ATA)
952195534Sscottl			break;
953195534Sscottl
954195534Sscottl		/*
955195534Sscottl		 * Allocate a peripheral instance for
956195534Sscottl		 * this device and start the probe
957195534Sscottl		 * process.
958195534Sscottl		 */
959195534Sscottl		status = cam_periph_alloc(adaregister, adaoninvalidate,
960195534Sscottl					  adacleanup, adastart,
961195534Sscottl					  "ada", CAM_PERIPH_BIO,
962256843Smav					  path, adaasync,
963195534Sscottl					  AC_FOUND_DEVICE, cgd);
964195534Sscottl
965195534Sscottl		if (status != CAM_REQ_CMP
966195534Sscottl		 && status != CAM_REQ_INPROG)
967195534Sscottl			printf("adaasync: Unable to attach to new device "
968195534Sscottl				"due to status 0x%x\n", status);
969195534Sscottl		break;
970195534Sscottl	}
971236393Smav	case AC_GETDEV_CHANGED:
972236393Smav	{
973236393Smav		softc = (struct ada_softc *)periph->softc;
974236393Smav		xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
975236393Smav		cgd.ccb_h.func_code = XPT_GDEV_TYPE;
976236393Smav		xpt_action((union ccb *)&cgd);
977236393Smav
978236393Smav		if ((cgd.ident_data.capabilities1 & ATA_SUPPORT_DMA) &&
979236393Smav		    (cgd.inq_flags & SID_DMA))
980236393Smav			softc->flags |= ADA_FLAG_CAN_DMA;
981236393Smav		else
982236393Smav			softc->flags &= ~ADA_FLAG_CAN_DMA;
983249199Smarius		if (cgd.ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) {
984249199Smarius			softc->flags |= ADA_FLAG_CAN_48BIT;
985249199Smarius			if (cgd.inq_flags & SID_DMA48)
986249199Smarius				softc->flags |= ADA_FLAG_CAN_DMA48;
987249199Smarius			else
988249199Smarius				softc->flags &= ~ADA_FLAG_CAN_DMA48;
989249199Smarius		} else
990249199Smarius			softc->flags &= ~(ADA_FLAG_CAN_48BIT |
991249199Smarius			    ADA_FLAG_CAN_DMA48);
992236393Smav		if ((cgd.ident_data.satacapabilities & ATA_SUPPORT_NCQ) &&
993236393Smav		    (cgd.inq_flags & SID_DMA) && (cgd.inq_flags & SID_CmdQue))
994236393Smav			softc->flags |= ADA_FLAG_CAN_NCQ;
995236393Smav		else
996236393Smav			softc->flags &= ~ADA_FLAG_CAN_NCQ;
997236393Smav		if ((cgd.ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) &&
998236393Smav		    (cgd.inq_flags & SID_DMA))
999236393Smav			softc->flags |= ADA_FLAG_CAN_TRIM;
1000236393Smav		else
1001236393Smav			softc->flags &= ~ADA_FLAG_CAN_TRIM;
1002236393Smav
1003236393Smav		cam_periph_async(periph, code, path, arg);
1004236393Smav		break;
1005236393Smav	}
1006235897Smav	case AC_ADVINFO_CHANGED:
1007235897Smav	{
1008235897Smav		uintptr_t buftype;
1009235897Smav
1010235897Smav		buftype = (uintptr_t)arg;
1011235897Smav		if (buftype == CDAI_TYPE_PHYS_PATH) {
1012235897Smav			struct ada_softc *softc;
1013235897Smav
1014235897Smav			softc = periph->softc;
1015235897Smav			disk_attr_changed(softc->disk, "GEOM::physpath",
1016235897Smav					  M_NOWAIT);
1017235897Smav		}
1018235897Smav		break;
1019235897Smav	}
1020220412Smav	case AC_SENT_BDR:
1021220412Smav	case AC_BUS_RESET:
1022220412Smav	{
1023220412Smav		softc = (struct ada_softc *)periph->softc;
1024220412Smav		cam_periph_async(periph, code, path, arg);
1025220412Smav		if (softc->state != ADA_STATE_NORMAL)
1026220412Smav			break;
1027220454Smav		xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1028220412Smav		cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1029220412Smav		xpt_action((union ccb *)&cgd);
1030224497Smav		if (ADA_RA >= 0 &&
1031224497Smav		    cgd.ident_data.support.command1 & ATA_SUPPORT_LOOKAHEAD)
1032224497Smav			softc->state = ADA_STATE_RAHEAD;
1033224497Smav		else if (ADA_WC >= 0 &&
1034224497Smav		    cgd.ident_data.support.command1 & ATA_SUPPORT_WRITECACHE)
1035224497Smav			softc->state = ADA_STATE_WCACHE;
1036224497Smav		else
1037224497Smav		    break;
1038256843Smav		if (cam_periph_acquire(periph) != CAM_REQ_CMP)
1039256843Smav			softc->state = ADA_STATE_NORMAL;
1040256843Smav		else
1041256843Smav			xpt_schedule(periph, CAM_PRIORITY_DEV);
1042220412Smav	}
1043195534Sscottl	default:
1044195534Sscottl		cam_periph_async(periph, code, path, arg);
1045195534Sscottl		break;
1046195534Sscottl	}
1047195534Sscottl}
1048195534Sscottl
1049195534Sscottlstatic void
1050195534Sscottladasysctlinit(void *context, int pending)
1051195534Sscottl{
1052195534Sscottl	struct cam_periph *periph;
1053195534Sscottl	struct ada_softc *softc;
1054195534Sscottl	char tmpstr[80], tmpstr2[80];
1055195534Sscottl
1056195534Sscottl	periph = (struct cam_periph *)context;
1057220454Smav
1058220454Smav	/* periph was held for us when this task was enqueued */
1059249981Smav	if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
1060220454Smav		cam_periph_release(periph);
1061195534Sscottl		return;
1062220454Smav	}
1063195534Sscottl
1064195534Sscottl	softc = (struct ada_softc *)periph->softc;
1065195534Sscottl	snprintf(tmpstr, sizeof(tmpstr), "CAM ADA unit %d", periph->unit_number);
1066195534Sscottl	snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1067195534Sscottl
1068195534Sscottl	sysctl_ctx_init(&softc->sysctl_ctx);
1069195534Sscottl	softc->flags |= ADA_FLAG_SCTX_INIT;
1070195534Sscottl	softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1071195534Sscottl		SYSCTL_STATIC_CHILDREN(_kern_cam_ada), OID_AUTO, tmpstr2,
1072195534Sscottl		CTLFLAG_RD, 0, tmpstr);
1073195534Sscottl	if (softc->sysctl_tree == NULL) {
1074195534Sscottl		printf("adasysctlinit: unable to allocate sysctl tree\n");
1075195534Sscottl		cam_periph_release(periph);
1076195534Sscottl		return;
1077195534Sscottl	}
1078195534Sscottl
1079220454Smav	SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1080224497Smav		OID_AUTO, "read_ahead", CTLFLAG_RW | CTLFLAG_MPSAFE,
1081224497Smav		&softc->read_ahead, 0, "Enable disk read ahead.");
1082224497Smav	SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1083220454Smav		OID_AUTO, "write_cache", CTLFLAG_RW | CTLFLAG_MPSAFE,
1084220454Smav		&softc->write_cache, 0, "Enable disk write cache.");
1085248922Ssmh	SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1086248922Ssmh		OID_AUTO, "sort_io_queue", CTLFLAG_RW | CTLFLAG_MPSAFE,
1087248922Ssmh		&softc->sort_io_queue, 0,
1088248922Ssmh		"Sort IO queue to try and optimise disk access patterns");
1089220454Smav#ifdef ADA_TEST_FAILURE
1090220454Smav	/*
1091220454Smav	 * Add a 'door bell' sysctl which allows one to set it from userland
1092220454Smav	 * and cause something bad to happen.  For the moment, we only allow
1093220454Smav	 * whacking the next read or write.
1094220454Smav	 */
1095220454Smav	SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1096220454Smav		OID_AUTO, "force_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
1097220454Smav		&softc->force_read_error, 0,
1098220454Smav		"Force a read error for the next N reads.");
1099220454Smav	SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1100220454Smav		OID_AUTO, "force_write_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
1101220454Smav		&softc->force_write_error, 0,
1102220454Smav		"Force a write error for the next N writes.");
1103220454Smav	SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1104220454Smav		OID_AUTO, "periodic_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
1105220454Smav		&softc->periodic_read_error, 0,
1106220454Smav		"Force a read error every N reads (don't set too low).");
1107220454Smav#endif
1108195534Sscottl	cam_periph_release(periph);
1109195534Sscottl}
1110195534Sscottl
1111223089Sgibbsstatic int
1112223089Sgibbsadagetattr(struct bio *bp)
1113223089Sgibbs{
1114241485Smav	int ret;
1115223089Sgibbs	struct cam_periph *periph;
1116223089Sgibbs
1117223089Sgibbs	periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1118241485Smav	cam_periph_lock(periph);
1119223089Sgibbs	ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
1120223089Sgibbs	    periph->path);
1121241485Smav	cam_periph_unlock(periph);
1122223089Sgibbs	if (ret == 0)
1123223089Sgibbs		bp->bio_completed = bp->bio_length;
1124223089Sgibbs	return ret;
1125223089Sgibbs}
1126223089Sgibbs
1127195534Sscottlstatic cam_status
1128195534Sscottladaregister(struct cam_periph *periph, void *arg)
1129195534Sscottl{
1130195534Sscottl	struct ada_softc *softc;
1131195534Sscottl	struct ccb_pathinq cpi;
1132195534Sscottl	struct ccb_getdev *cgd;
1133221071Smav	char   announce_buf[80], buf1[32];
1134195534Sscottl	struct disk_params *dp;
1135195534Sscottl	caddr_t match;
1136195534Sscottl	u_int maxio;
1137222520Smav	int legacy_id, quirks;
1138195534Sscottl
1139195534Sscottl	cgd = (struct ccb_getdev *)arg;
1140195534Sscottl	if (cgd == NULL) {
1141195534Sscottl		printf("adaregister: no getdev CCB, can't register device\n");
1142195534Sscottl		return(CAM_REQ_CMP_ERR);
1143195534Sscottl	}
1144195534Sscottl
1145195534Sscottl	softc = (struct ada_softc *)malloc(sizeof(*softc), M_DEVBUF,
1146195534Sscottl	    M_NOWAIT|M_ZERO);
1147195534Sscottl
1148195534Sscottl	if (softc == NULL) {
1149195534Sscottl		printf("adaregister: Unable to probe new device. "
1150198328Smav		    "Unable to allocate softc\n");
1151195534Sscottl		return(CAM_REQ_CMP_ERR);
1152195534Sscottl	}
1153195534Sscottl
1154195534Sscottl	bioq_init(&softc->bio_queue);
1155201139Smav	bioq_init(&softc->trim_queue);
1156195534Sscottl
1157236393Smav	if ((cgd->ident_data.capabilities1 & ATA_SUPPORT_DMA) &&
1158220886Smav	    (cgd->inq_flags & SID_DMA))
1159198328Smav		softc->flags |= ADA_FLAG_CAN_DMA;
1160249199Smarius	if (cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) {
1161195534Sscottl		softc->flags |= ADA_FLAG_CAN_48BIT;
1162249199Smarius		if (cgd->inq_flags & SID_DMA48)
1163249199Smarius			softc->flags |= ADA_FLAG_CAN_DMA48;
1164249199Smarius	}
1165195534Sscottl	if (cgd->ident_data.support.command2 & ATA_SUPPORT_FLUSHCACHE)
1166195534Sscottl		softc->flags |= ADA_FLAG_CAN_FLUSHCACHE;
1167214279Sbrucec	if (cgd->ident_data.support.command1 & ATA_SUPPORT_POWERMGT)
1168214279Sbrucec		softc->flags |= ADA_FLAG_CAN_POWERMGT;
1169236393Smav	if ((cgd->ident_data.satacapabilities & ATA_SUPPORT_NCQ) &&
1170220886Smav	    (cgd->inq_flags & SID_DMA) && (cgd->inq_flags & SID_CmdQue))
1171195534Sscottl		softc->flags |= ADA_FLAG_CAN_NCQ;
1172236393Smav	if ((cgd->ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) &&
1173236393Smav	    (cgd->inq_flags & SID_DMA)) {
1174201139Smav		softc->flags |= ADA_FLAG_CAN_TRIM;
1175201139Smav		softc->trim_max_ranges = TRIM_MAX_RANGES;
1176201139Smav		if (cgd->ident_data.max_dsm_blocks != 0) {
1177201139Smav			softc->trim_max_ranges =
1178249934Ssmh			    min(cgd->ident_data.max_dsm_blocks *
1179249934Ssmh				ATA_DSM_BLK_RANGES, softc->trim_max_ranges);
1180201139Smav		}
1181201139Smav	}
1182201139Smav	if (cgd->ident_data.support.command2 & ATA_SUPPORT_CFA)
1183201139Smav		softc->flags |= ADA_FLAG_CAN_CFA;
1184195534Sscottl
1185195534Sscottl	periph->softc = softc;
1186195534Sscottl
1187195534Sscottl	/*
1188195534Sscottl	 * See if this device has any quirks.
1189195534Sscottl	 */
1190199178Smav	match = cam_quirkmatch((caddr_t)&cgd->ident_data,
1191199178Smav			       (caddr_t)ada_quirk_table,
1192199178Smav			       sizeof(ada_quirk_table)/sizeof(*ada_quirk_table),
1193199178Smav			       sizeof(*ada_quirk_table), ata_identify_match);
1194195534Sscottl	if (match != NULL)
1195195534Sscottl		softc->quirks = ((struct ada_quirk_entry *)match)->quirks;
1196195534Sscottl	else
1197195534Sscottl		softc->quirks = ADA_Q_NONE;
1198195534Sscottl
1199195534Sscottl	bzero(&cpi, sizeof(cpi));
1200203108Smav	xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NONE);
1201195534Sscottl	cpi.ccb_h.func_code = XPT_PATH_INQ;
1202195534Sscottl	xpt_action((union ccb *)&cpi);
1203195534Sscottl
1204195534Sscottl	TASK_INIT(&softc->sysctl_task, 0, adasysctlinit, periph);
1205195534Sscottl
1206195534Sscottl	/*
1207195534Sscottl	 * Register this media as a disk
1208195534Sscottl	 */
1209220618Smav	(void)cam_periph_hold(periph, PRIBIO);
1210249106Smav	cam_periph_unlock(periph);
1211222520Smav	snprintf(announce_buf, sizeof(announce_buf),
1212222520Smav	    "kern.cam.ada.%d.quirks", periph->unit_number);
1213222520Smav	quirks = softc->quirks;
1214222520Smav	TUNABLE_INT_FETCH(announce_buf, &quirks);
1215222520Smav	softc->quirks = quirks;
1216224497Smav	softc->read_ahead = -1;
1217224497Smav	snprintf(announce_buf, sizeof(announce_buf),
1218224497Smav	    "kern.cam.ada.%d.read_ahead", periph->unit_number);
1219224497Smav	TUNABLE_INT_FETCH(announce_buf, &softc->read_ahead);
1220220618Smav	softc->write_cache = -1;
1221220618Smav	snprintf(announce_buf, sizeof(announce_buf),
1222220618Smav	    "kern.cam.ada.%d.write_cache", periph->unit_number);
1223220618Smav	TUNABLE_INT_FETCH(announce_buf, &softc->write_cache);
1224250033Ssmh	/* Disable queue sorting for non-rotational media by default. */
1225249941Ssmh	if (cgd->ident_data.media_rotation_rate == 1)
1226249941Ssmh		softc->sort_io_queue = 0;
1227249941Ssmh	else
1228249941Ssmh		softc->sort_io_queue = -1;
1229198897Smav	adagetparams(periph, cgd);
1230195534Sscottl	softc->disk = disk_alloc();
1231220644Smav	softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
1232220644Smav			  periph->unit_number, softc->params.secsize,
1233220644Smav			  DEVSTAT_ALL_SUPPORTED,
1234220644Smav			  DEVSTAT_TYPE_DIRECT |
1235220644Smav			  XPORT_DEVSTAT_TYPE(cpi.transport),
1236220644Smav			  DEVSTAT_PRIORITY_DISK);
1237195534Sscottl	softc->disk->d_open = adaopen;
1238195534Sscottl	softc->disk->d_close = adaclose;
1239195534Sscottl	softc->disk->d_strategy = adastrategy;
1240223089Sgibbs	softc->disk->d_getattr = adagetattr;
1241195534Sscottl	softc->disk->d_dump = adadump;
1242249347Sken	softc->disk->d_gone = adadiskgonecb;
1243195534Sscottl	softc->disk->d_name = "ada";
1244195534Sscottl	softc->disk->d_drv1 = periph;
1245195534Sscottl	maxio = cpi.maxio;		/* Honor max I/O size of SIM */
1246195534Sscottl	if (maxio == 0)
1247195534Sscottl		maxio = DFLTPHYS;	/* traditional default */
1248195534Sscottl	else if (maxio > MAXPHYS)
1249195534Sscottl		maxio = MAXPHYS;	/* for safety */
1250201139Smav	if (softc->flags & ADA_FLAG_CAN_48BIT)
1251198897Smav		maxio = min(maxio, 65536 * softc->params.secsize);
1252195534Sscottl	else					/* 28bit ATA command limit */
1253198897Smav		maxio = min(maxio, 256 * softc->params.secsize);
1254195534Sscottl	softc->disk->d_maxsize = maxio;
1255195534Sscottl	softc->disk->d_unit = periph->unit_number;
1256195534Sscottl	softc->disk->d_flags = 0;
1257195534Sscottl	if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE)
1258195534Sscottl		softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
1259249934Ssmh	if (softc->flags & ADA_FLAG_CAN_TRIM) {
1260201139Smav		softc->disk->d_flags |= DISKFLAG_CANDELETE;
1261249940Ssmh		softc->disk->d_delmaxsize = softc->params.secsize *
1262249940Ssmh					    ATA_DSM_RANGE_MAX *
1263249940Ssmh					    softc->trim_max_ranges;
1264249934Ssmh	} else if ((softc->flags & ADA_FLAG_CAN_CFA) &&
1265249934Ssmh	    !(softc->flags & ADA_FLAG_CAN_48BIT)) {
1266249934Ssmh		softc->disk->d_flags |= DISKFLAG_CANDELETE;
1267249940Ssmh		softc->disk->d_delmaxsize = 256 * softc->params.secsize;
1268249940Ssmh	} else
1269249940Ssmh		softc->disk->d_delmaxsize = maxio;
1270248519Skib	if ((cpi.hba_misc & PIM_UNMAPPED) != 0)
1271248519Skib		softc->disk->d_flags |= DISKFLAG_UNMAPPED_BIO;
1272219056Snwhitehorn	strlcpy(softc->disk->d_descr, cgd->ident_data.model,
1273219056Snwhitehorn	    MIN(sizeof(softc->disk->d_descr), sizeof(cgd->ident_data.model)));
1274241305Savg	strlcpy(softc->disk->d_ident, cgd->ident_data.serial,
1275241305Savg	    MIN(sizeof(softc->disk->d_ident), sizeof(cgd->ident_data.serial)));
1276210471Smav	softc->disk->d_hba_vendor = cpi.hba_vendor;
1277210471Smav	softc->disk->d_hba_device = cpi.hba_device;
1278210471Smav	softc->disk->d_hba_subvendor = cpi.hba_subvendor;
1279210471Smav	softc->disk->d_hba_subdevice = cpi.hba_subdevice;
1280195534Sscottl
1281195534Sscottl	softc->disk->d_sectorsize = softc->params.secsize;
1282198897Smav	softc->disk->d_mediasize = (off_t)softc->params.sectors *
1283198897Smav	    softc->params.secsize;
1284200969Smav	if (ata_physical_sector_size(&cgd->ident_data) !=
1285200969Smav	    softc->params.secsize) {
1286200969Smav		softc->disk->d_stripesize =
1287200969Smav		    ata_physical_sector_size(&cgd->ident_data);
1288200969Smav		softc->disk->d_stripeoffset = (softc->disk->d_stripesize -
1289200969Smav		    ata_logical_sector_offset(&cgd->ident_data)) %
1290200969Smav		    softc->disk->d_stripesize;
1291222520Smav	} else if (softc->quirks & ADA_Q_4K) {
1292222520Smav		softc->disk->d_stripesize = 4096;
1293222520Smav		softc->disk->d_stripeoffset = 0;
1294200969Smav	}
1295195534Sscottl	softc->disk->d_fwsectors = softc->params.secs_per_track;
1296195534Sscottl	softc->disk->d_fwheads = softc->params.heads;
1297208349Smarius	ata_disk_firmware_geom_adjust(softc->disk);
1298195534Sscottl
1299221071Smav	if (ada_legacy_aliases) {
1300221071Smav#ifdef ATA_STATIC_ID
1301221071Smav		legacy_id = xpt_path_legacy_ata_id(periph->path);
1302221071Smav#else
1303221071Smav		legacy_id = softc->disk->d_unit;
1304221071Smav#endif
1305221071Smav		if (legacy_id >= 0) {
1306221071Smav			snprintf(announce_buf, sizeof(announce_buf),
1307221071Smav			    "kern.devalias.%s%d",
1308221071Smav			    softc->disk->d_name, softc->disk->d_unit);
1309221071Smav			snprintf(buf1, sizeof(buf1),
1310221071Smav			    "ad%d", legacy_id);
1311221071Smav			setenv(announce_buf, buf1);
1312221071Smav		}
1313221071Smav	} else
1314221071Smav		legacy_id = -1;
1315249347Sken	/*
1316249347Sken	 * Acquire a reference to the periph before we register with GEOM.
1317249347Sken	 * We'll release this reference once GEOM calls us back (via
1318249347Sken	 * adadiskgonecb()) telling us that our provider has been freed.
1319249347Sken	 */
1320249347Sken	if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
1321249347Sken		xpt_print(periph->path, "%s: lost periph during "
1322249347Sken			  "registration!\n", __func__);
1323249347Sken		cam_periph_lock(periph);
1324249347Sken		return (CAM_REQ_CMP_ERR);
1325249347Sken	}
1326195534Sscottl	disk_create(softc->disk, DISK_VERSION);
1327249106Smav	cam_periph_lock(periph);
1328220618Smav	cam_periph_unhold(periph);
1329195534Sscottl
1330195534Sscottl	dp = &softc->params;
1331195534Sscottl	snprintf(announce_buf, sizeof(announce_buf),
1332195534Sscottl		"%juMB (%ju %u byte sectors: %dH %dS/T %dC)",
1333195534Sscottl		(uintmax_t)(((uintmax_t)dp->secsize *
1334195534Sscottl		dp->sectors) / (1024*1024)),
1335195534Sscottl		(uintmax_t)dp->sectors,
1336195534Sscottl		dp->secsize, dp->heads,
1337195534Sscottl		dp->secs_per_track, dp->cylinders);
1338195534Sscottl	xpt_announce_periph(periph, announce_buf);
1339250792Ssmh	xpt_announce_quirks(periph, softc->quirks, ADA_Q_BIT_STRING);
1340221071Smav	if (legacy_id >= 0)
1341221071Smav		printf("%s%d: Previously was known as ad%d\n",
1342221071Smav		       periph->periph_name, periph->unit_number, legacy_id);
1343220454Smav
1344195534Sscottl	/*
1345220454Smav	 * Create our sysctl variables, now that we know
1346220454Smav	 * we have successfully attached.
1347220454Smav	 */
1348256843Smav	if (cam_periph_acquire(periph) == CAM_REQ_CMP)
1349256843Smav		taskqueue_enqueue(taskqueue_thread, &softc->sysctl_task);
1350220454Smav
1351220454Smav	/*
1352195534Sscottl	 * Add async callbacks for bus reset and
1353195534Sscottl	 * bus device reset calls.  I don't bother
1354195534Sscottl	 * checking if this fails as, in most cases,
1355195534Sscottl	 * the system will function just fine without
1356195534Sscottl	 * them and the only alternative would be to
1357195534Sscottl	 * not attach the device on failure.
1358195534Sscottl	 */
1359235897Smav	xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
1360236393Smav	    AC_GETDEV_CHANGED | AC_ADVINFO_CHANGED,
1361236393Smav	    adaasync, periph, periph->path);
1362195534Sscottl
1363195534Sscottl	/*
1364195534Sscottl	 * Schedule a periodic event to occasionally send an
1365195534Sscottl	 * ordered tag to a device.
1366195534Sscottl	 */
1367256843Smav	callout_init_mtx(&softc->sendordered_c, cam_periph_mtx(periph), 0);
1368195534Sscottl	callout_reset(&softc->sendordered_c,
1369230921Smav	    (ada_default_timeout * hz) / ADA_ORDEREDTAG_INTERVAL,
1370195534Sscottl	    adasendorderedtag, softc);
1371195534Sscottl
1372224497Smav	if (ADA_RA >= 0 &&
1373224497Smav	    cgd->ident_data.support.command1 & ATA_SUPPORT_LOOKAHEAD) {
1374224497Smav		softc->state = ADA_STATE_RAHEAD;
1375224497Smav	} else if (ADA_WC >= 0 &&
1376220412Smav	    cgd->ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) {
1377220412Smav		softc->state = ADA_STATE_WCACHE;
1378256843Smav	} else {
1379256843Smav		softc->state = ADA_STATE_NORMAL;
1380256843Smav		return(CAM_REQ_CMP);
1381256843Smav	}
1382256843Smav	if (cam_periph_acquire(periph) != CAM_REQ_CMP)
1383256843Smav		softc->state = ADA_STATE_NORMAL;
1384256843Smav	else
1385220412Smav		xpt_schedule(periph, CAM_PRIORITY_DEV);
1386195534Sscottl	return(CAM_REQ_CMP);
1387195534Sscottl}
1388195534Sscottl
1389195534Sscottlstatic void
1390195534Sscottladastart(struct cam_periph *periph, union ccb *start_ccb)
1391195534Sscottl{
1392198328Smav	struct ada_softc *softc = (struct ada_softc *)periph->softc;
1393198328Smav	struct ccb_ataio *ataio = &start_ccb->ataio;
1394195534Sscottl
1395236602Smav	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adastart\n"));
1396236602Smav
1397195534Sscottl	switch (softc->state) {
1398195534Sscottl	case ADA_STATE_NORMAL:
1399195534Sscottl	{
1400195534Sscottl		struct bio *bp;
1401201139Smav		u_int8_t tag_code;
1402195534Sscottl
1403201139Smav		/* Run TRIM if not running yet. */
1404201139Smav		if (!softc->trim_running &&
1405201139Smav		    (bp = bioq_first(&softc->trim_queue)) != 0) {
1406201139Smav			struct trim_request *req = &softc->trim_req;
1407201139Smav			struct bio *bp1;
1408222628Smav			uint64_t lastlba = (uint64_t)-1;
1409256836Smav			int c, lastcount = 0, off, ranges = 0;
1410201139Smav
1411201139Smav			softc->trim_running = 1;
1412201139Smav			bzero(req, sizeof(*req));
1413256836Smav			TAILQ_INIT(&req->bps);
1414201139Smav			bp1 = bp;
1415201139Smav			do {
1416201139Smav				uint64_t lba = bp1->bio_pblkno;
1417201139Smav				int count = bp1->bio_bcount /
1418201139Smav				    softc->params.secsize;
1419201139Smav
1420201139Smav				bioq_remove(&softc->trim_queue, bp1);
1421222628Smav
1422222628Smav				/* Try to extend the previous range. */
1423222628Smav				if (lba == lastlba) {
1424249934Ssmh					c = min(count, ATA_DSM_RANGE_MAX - lastcount);
1425222628Smav					lastcount += c;
1426249934Ssmh					off = (ranges - 1) * ATA_DSM_RANGE_SIZE;
1427222628Smav					req->data[off + 6] = lastcount & 0xff;
1428222628Smav					req->data[off + 7] =
1429222628Smav					    (lastcount >> 8) & 0xff;
1430222628Smav					count -= c;
1431222628Smav					lba += c;
1432222628Smav				}
1433222628Smav
1434201139Smav				while (count > 0) {
1435249934Ssmh					c = min(count, ATA_DSM_RANGE_MAX);
1436249934Ssmh					off = ranges * ATA_DSM_RANGE_SIZE;
1437201139Smav					req->data[off + 0] = lba & 0xff;
1438201139Smav					req->data[off + 1] = (lba >> 8) & 0xff;
1439201139Smav					req->data[off + 2] = (lba >> 16) & 0xff;
1440201139Smav					req->data[off + 3] = (lba >> 24) & 0xff;
1441201139Smav					req->data[off + 4] = (lba >> 32) & 0xff;
1442201139Smav					req->data[off + 5] = (lba >> 40) & 0xff;
1443201139Smav					req->data[off + 6] = c & 0xff;
1444201139Smav					req->data[off + 7] = (c >> 8) & 0xff;
1445201139Smav					lba += c;
1446201139Smav					count -= c;
1447222628Smav					lastcount = c;
1448201139Smav					ranges++;
1449249934Ssmh					/*
1450249934Ssmh					 * Its the caller's responsibility to ensure the
1451249934Ssmh					 * request will fit so we don't need to check for
1452249934Ssmh					 * overrun here
1453249934Ssmh					 */
1454201139Smav				}
1455222628Smav				lastlba = lba;
1456256836Smav				TAILQ_INSERT_TAIL(&req->bps, bp1, bio_queue);
1457201139Smav				bp1 = bioq_first(&softc->trim_queue);
1458256836Smav				if (bp1 == NULL ||
1459201139Smav				    bp1->bio_bcount / softc->params.secsize >
1460249934Ssmh				    (softc->trim_max_ranges - ranges) *
1461249934Ssmh				    ATA_DSM_RANGE_MAX)
1462201139Smav					break;
1463201139Smav			} while (1);
1464201139Smav			cam_fill_ataio(ataio,
1465201139Smav			    ada_retry_count,
1466201139Smav			    adadone,
1467201139Smav			    CAM_DIR_OUT,
1468201139Smav			    0,
1469201139Smav			    req->data,
1470249934Ssmh			    ((ranges + ATA_DSM_BLK_RANGES - 1) /
1471249934Ssmh			        ATA_DSM_BLK_RANGES) * ATA_DSM_BLK_SIZE,
1472201139Smav			    ada_default_timeout * 1000);
1473201139Smav			ata_48bit_cmd(ataio, ATA_DATA_SET_MANAGEMENT,
1474249934Ssmh			    ATA_DSM_TRIM, 0, (ranges + ATA_DSM_BLK_RANGES -
1475249934Ssmh			    1) / ATA_DSM_BLK_RANGES);
1476201139Smav			start_ccb->ccb_h.ccb_state = ADA_CCB_TRIM;
1477256843Smav			start_ccb->ccb_h.flags |= CAM_UNLOCKED;
1478201139Smav			goto out;
1479201139Smav		}
1480201139Smav		/* Run regular command. */
1481201139Smav		bp = bioq_first(&softc->bio_queue);
1482201139Smav		if (bp == NULL) {
1483195534Sscottl			xpt_release_ccb(start_ccb);
1484201139Smav			break;
1485201139Smav		}
1486201139Smav		bioq_remove(&softc->bio_queue, bp);
1487201139Smav
1488212160Sgibbs		if ((bp->bio_flags & BIO_ORDERED) != 0
1489212160Sgibbs		 || (softc->flags & ADA_FLAG_NEED_OTAG) != 0) {
1490201139Smav			softc->flags &= ~ADA_FLAG_NEED_OTAG;
1491201139Smav			softc->ordered_tag_count++;
1492201139Smav			tag_code = 0;
1493195534Sscottl		} else {
1494201139Smav			tag_code = 1;
1495201139Smav		}
1496201139Smav		switch (bp->bio_cmd) {
1497253724Smav		case BIO_WRITE:
1498253724Smav			softc->flags |= ADA_FLAG_DIRTY;
1499253724Smav			/* FALLTHROUGH */
1500201139Smav		case BIO_READ:
1501201139Smav		{
1502201139Smav			uint64_t lba = bp->bio_pblkno;
1503201139Smav			uint16_t count = bp->bio_bcount / softc->params.secsize;
1504220454Smav#ifdef ADA_TEST_FAILURE
1505220454Smav			int fail = 0;
1506195534Sscottl
1507220454Smav			/*
1508220454Smav			 * Support the failure ioctls.  If the command is a
1509220454Smav			 * read, and there are pending forced read errors, or
1510220454Smav			 * if a write and pending write errors, then fail this
1511220454Smav			 * operation with EIO.  This is useful for testing
1512220454Smav			 * purposes.  Also, support having every Nth read fail.
1513220454Smav			 *
1514220454Smav			 * This is a rather blunt tool.
1515220454Smav			 */
1516220454Smav			if (bp->bio_cmd == BIO_READ) {
1517220454Smav				if (softc->force_read_error) {
1518220454Smav					softc->force_read_error--;
1519220454Smav					fail = 1;
1520220454Smav				}
1521220454Smav				if (softc->periodic_read_error > 0) {
1522220454Smav					if (++softc->periodic_read_count >=
1523220454Smav					    softc->periodic_read_error) {
1524220454Smav						softc->periodic_read_count = 0;
1525220454Smav						fail = 1;
1526220454Smav					}
1527220454Smav				}
1528220454Smav			} else {
1529220454Smav				if (softc->force_write_error) {
1530220454Smav					softc->force_write_error--;
1531220454Smav					fail = 1;
1532220454Smav				}
1533220454Smav			}
1534220454Smav			if (fail) {
1535220454Smav				bp->bio_error = EIO;
1536220454Smav				bp->bio_flags |= BIO_ERROR;
1537220454Smav				biodone(bp);
1538220454Smav				xpt_release_ccb(start_ccb);
1539220454Smav				adaschedule(periph);
1540220454Smav				return;
1541220454Smav			}
1542220454Smav#endif
1543248519Skib			KASSERT((bp->bio_flags & BIO_UNMAPPED) == 0 ||
1544248519Skib			    round_page(bp->bio_bcount + bp->bio_ma_offset) /
1545248519Skib			    PAGE_SIZE == bp->bio_ma_n,
1546248519Skib			    ("Short bio %p", bp));
1547201139Smav			cam_fill_ataio(ataio,
1548201139Smav			    ada_retry_count,
1549201139Smav			    adadone,
1550248519Skib			    (bp->bio_cmd == BIO_READ ? CAM_DIR_IN :
1551248519Skib				CAM_DIR_OUT) | ((bp->bio_flags & BIO_UNMAPPED)
1552248519Skib				!= 0 ? CAM_DATA_BIO : 0),
1553201139Smav			    tag_code,
1554248519Skib			    ((bp->bio_flags & BIO_UNMAPPED) != 0) ? (void *)bp :
1555248519Skib				bp->bio_data,
1556201139Smav			    bp->bio_bcount,
1557201139Smav			    ada_default_timeout*1000);
1558195534Sscottl
1559201139Smav			if ((softc->flags & ADA_FLAG_CAN_NCQ) && tag_code) {
1560201139Smav				if (bp->bio_cmd == BIO_READ) {
1561201139Smav					ata_ncq_cmd(ataio, ATA_READ_FPDMA_QUEUED,
1562201139Smav					    lba, count);
1563201139Smav				} else {
1564201139Smav					ata_ncq_cmd(ataio, ATA_WRITE_FPDMA_QUEUED,
1565201139Smav					    lba, count);
1566201139Smav				}
1567201139Smav			} else if ((softc->flags & ADA_FLAG_CAN_48BIT) &&
1568201139Smav			    (lba + count >= ATA_MAX_28BIT_LBA ||
1569201139Smav			    count > 256)) {
1570249199Smarius				if (softc->flags & ADA_FLAG_CAN_DMA48) {
1571195534Sscottl					if (bp->bio_cmd == BIO_READ) {
1572201139Smav						ata_48bit_cmd(ataio, ATA_READ_DMA48,
1573201139Smav						    0, lba, count);
1574195534Sscottl					} else {
1575201139Smav						ata_48bit_cmd(ataio, ATA_WRITE_DMA48,
1576201139Smav						    0, lba, count);
1577195534Sscottl					}
1578201139Smav				} else {
1579201139Smav					if (bp->bio_cmd == BIO_READ) {
1580201139Smav						ata_48bit_cmd(ataio, ATA_READ_MUL48,
1581201139Smav						    0, lba, count);
1582195534Sscottl					} else {
1583201139Smav						ata_48bit_cmd(ataio, ATA_WRITE_MUL48,
1584201139Smav						    0, lba, count);
1585195534Sscottl					}
1586201139Smav				}
1587201139Smav			} else {
1588201139Smav				if (count == 256)
1589201139Smav					count = 0;
1590201139Smav				if (softc->flags & ADA_FLAG_CAN_DMA) {
1591201139Smav					if (bp->bio_cmd == BIO_READ) {
1592201139Smav						ata_28bit_cmd(ataio, ATA_READ_DMA,
1593201139Smav						    0, lba, count);
1594201139Smav					} else {
1595201139Smav						ata_28bit_cmd(ataio, ATA_WRITE_DMA,
1596201139Smav						    0, lba, count);
1597201139Smav					}
1598195534Sscottl				} else {
1599201139Smav					if (bp->bio_cmd == BIO_READ) {
1600201139Smav						ata_28bit_cmd(ataio, ATA_READ_MUL,
1601201139Smav						    0, lba, count);
1602195534Sscottl					} else {
1603201139Smav						ata_28bit_cmd(ataio, ATA_WRITE_MUL,
1604201139Smav						    0, lba, count);
1605195534Sscottl					}
1606195534Sscottl				}
1607195534Sscottl			}
1608201139Smav			break;
1609201139Smav		}
1610201139Smav		case BIO_DELETE:
1611201139Smav		{
1612201139Smav			uint64_t lba = bp->bio_pblkno;
1613201139Smav			uint16_t count = bp->bio_bcount / softc->params.secsize;
1614195534Sscottl
1615201139Smav			cam_fill_ataio(ataio,
1616201139Smav			    ada_retry_count,
1617201139Smav			    adadone,
1618201139Smav			    CAM_DIR_NONE,
1619201139Smav			    0,
1620201139Smav			    NULL,
1621201139Smav			    0,
1622201139Smav			    ada_default_timeout*1000);
1623201139Smav
1624201139Smav			if (count >= 256)
1625201139Smav				count = 0;
1626201139Smav			ata_28bit_cmd(ataio, ATA_CFA_ERASE, 0, lba, count);
1627201139Smav			break;
1628195534Sscottl		}
1629201139Smav		case BIO_FLUSH:
1630201139Smav			cam_fill_ataio(ataio,
1631201139Smav			    1,
1632201139Smav			    adadone,
1633201139Smav			    CAM_DIR_NONE,
1634201139Smav			    0,
1635201139Smav			    NULL,
1636201139Smav			    0,
1637201139Smav			    ada_default_timeout*1000);
1638201139Smav
1639201139Smav			if (softc->flags & ADA_FLAG_CAN_48BIT)
1640201139Smav				ata_48bit_cmd(ataio, ATA_FLUSHCACHE48, 0, 0, 0);
1641201139Smav			else
1642201139Smav				ata_28bit_cmd(ataio, ATA_FLUSHCACHE, 0, 0, 0);
1643201139Smav			break;
1644195534Sscottl		}
1645201139Smav		start_ccb->ccb_h.ccb_state = ADA_CCB_BUFFER_IO;
1646256843Smav		start_ccb->ccb_h.flags |= CAM_UNLOCKED;
1647201139Smavout:
1648201139Smav		start_ccb->ccb_h.ccb_bp = bp;
1649201139Smav		softc->outstanding_cmds++;
1650256843Smav		softc->refcount++;
1651256843Smav		cam_periph_unlock(periph);
1652201139Smav		xpt_action(start_ccb);
1653256843Smav		cam_periph_lock(periph);
1654256843Smav		softc->refcount--;
1655201139Smav
1656201139Smav		/* May have more work to do, so ensure we stay scheduled */
1657201139Smav		adaschedule(periph);
1658195534Sscottl		break;
1659195534Sscottl	}
1660224497Smav	case ADA_STATE_RAHEAD:
1661220412Smav	case ADA_STATE_WCACHE:
1662220412Smav	{
1663220412Smav		cam_fill_ataio(ataio,
1664220412Smav		    1,
1665220412Smav		    adadone,
1666220412Smav		    CAM_DIR_NONE,
1667220412Smav		    0,
1668220412Smav		    NULL,
1669220412Smav		    0,
1670220412Smav		    ada_default_timeout*1000);
1671220412Smav
1672224497Smav		if (softc->state == ADA_STATE_RAHEAD) {
1673224497Smav			ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_RA ?
1674224497Smav			    ATA_SF_ENAB_RCACHE : ATA_SF_DIS_RCACHE, 0, 0);
1675224497Smav			start_ccb->ccb_h.ccb_state = ADA_CCB_RAHEAD;
1676224497Smav		} else {
1677224497Smav			ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_WC ?
1678224497Smav			    ATA_SF_ENAB_WCACHE : ATA_SF_DIS_WCACHE, 0, 0);
1679224497Smav			start_ccb->ccb_h.ccb_state = ADA_CCB_WCACHE;
1680224497Smav		}
1681249466Smav		start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1682220412Smav		xpt_action(start_ccb);
1683220412Smav		break;
1684195534Sscottl	}
1685220412Smav	}
1686195534Sscottl}
1687195534Sscottl
1688195534Sscottlstatic void
1689195534Sscottladadone(struct cam_periph *periph, union ccb *done_ccb)
1690195534Sscottl{
1691195534Sscottl	struct ada_softc *softc;
1692195534Sscottl	struct ccb_ataio *ataio;
1693224497Smav	struct ccb_getdev *cgd;
1694249466Smav	struct cam_path *path;
1695253752Smav	int state;
1696195534Sscottl
1697195534Sscottl	softc = (struct ada_softc *)periph->softc;
1698195534Sscottl	ataio = &done_ccb->ataio;
1699249466Smav	path = done_ccb->ccb_h.path;
1700236602Smav
1701249466Smav	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("adadone\n"));
1702236602Smav
1703253752Smav	state = ataio->ccb_h.ccb_state & ADA_CCB_TYPE_MASK;
1704253752Smav	switch (state) {
1705195534Sscottl	case ADA_CCB_BUFFER_IO:
1706201139Smav	case ADA_CCB_TRIM:
1707195534Sscottl	{
1708195534Sscottl		struct bio *bp;
1709253752Smav		int error;
1710195534Sscottl
1711256843Smav		cam_periph_lock(periph);
1712195534Sscottl		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1713198328Smav			error = adaerror(done_ccb, 0, 0);
1714195534Sscottl			if (error == ERESTART) {
1715198328Smav				/* A retry was scheduled, so just return. */
1716256843Smav				cam_periph_unlock(periph);
1717195534Sscottl				return;
1718195534Sscottl			}
1719195534Sscottl			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1720249466Smav				cam_release_devq(path,
1721195534Sscottl						 /*relsim_flags*/0,
1722195534Sscottl						 /*reduction*/0,
1723195534Sscottl						 /*timeout*/0,
1724195534Sscottl						 /*getcount_only*/0);
1725195534Sscottl		} else {
1726195534Sscottl			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1727195534Sscottl				panic("REQ_CMP with QFRZN");
1728253752Smav			error = 0;
1729253752Smav		}
1730253752Smav		bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1731253752Smav		bp->bio_error = error;
1732253752Smav		if (error != 0) {
1733253752Smav			bp->bio_resid = bp->bio_bcount;
1734253752Smav			bp->bio_flags |= BIO_ERROR;
1735253752Smav		} else {
1736253752Smav			if (state == ADA_CCB_TRIM)
1737253752Smav				bp->bio_resid = 0;
1738253752Smav			else
1739253752Smav				bp->bio_resid = ataio->resid;
1740253752Smav			if (bp->bio_resid > 0)
1741195534Sscottl				bp->bio_flags |= BIO_ERROR;
1742195534Sscottl		}
1743195534Sscottl		softc->outstanding_cmds--;
1744195534Sscottl		if (softc->outstanding_cmds == 0)
1745195534Sscottl			softc->flags |= ADA_FLAG_WENT_IDLE;
1746256843Smav		xpt_release_ccb(done_ccb);
1747253752Smav		if (state == ADA_CCB_TRIM) {
1748256836Smav			TAILQ_HEAD(, bio) queue;
1749256836Smav			struct bio *bp1;
1750195534Sscottl
1751256836Smav			TAILQ_INIT(&queue);
1752256836Smav			TAILQ_CONCAT(&queue, &softc->trim_req.bps, bio_queue);
1753256836Smav			softc->trim_running = 0;
1754256843Smav			adaschedule(periph);
1755256843Smav			cam_periph_unlock(periph);
1756256836Smav			while ((bp1 = TAILQ_FIRST(&queue)) != NULL) {
1757256836Smav				TAILQ_REMOVE(&queue, bp1, bio_queue);
1758256836Smav				bp1->bio_error = error;
1759256836Smav				if (error != 0) {
1760201139Smav					bp1->bio_flags |= BIO_ERROR;
1761253752Smav					bp1->bio_resid = bp1->bio_bcount;
1762253752Smav				} else
1763253752Smav					bp1->bio_resid = 0;
1764201139Smav				biodone(bp1);
1765201139Smav			}
1766256843Smav		} else {
1767256843Smav			cam_periph_unlock(periph);
1768201139Smav			biodone(bp);
1769256843Smav		}
1770256843Smav		return;
1771195534Sscottl	}
1772224497Smav	case ADA_CCB_RAHEAD:
1773224497Smav	{
1774224497Smav		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1775224497Smav			if (adaerror(done_ccb, 0, 0) == ERESTART) {
1776249466Smavout:
1777249466Smav				/* Drop freeze taken due to CAM_DEV_QFREEZE */
1778249466Smav				cam_release_devq(path, 0, 0, 0, FALSE);
1779224497Smav				return;
1780224497Smav			} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1781249466Smav				cam_release_devq(path,
1782224497Smav				    /*relsim_flags*/0,
1783224497Smav				    /*reduction*/0,
1784224497Smav				    /*timeout*/0,
1785224497Smav				    /*getcount_only*/0);
1786224497Smav			}
1787224497Smav		}
1788224497Smav
1789224497Smav		/*
1790224497Smav		 * Since our peripheral may be invalidated by an error
1791224497Smav		 * above or an external event, we must release our CCB
1792224497Smav		 * before releasing the reference on the peripheral.
1793224497Smav		 * The peripheral will only go away once the last reference
1794224497Smav		 * is removed, and we need it around for the CCB release
1795224497Smav		 * operation.
1796224497Smav		 */
1797224497Smav		cgd = (struct ccb_getdev *)done_ccb;
1798249466Smav		xpt_setup_ccb(&cgd->ccb_h, path, CAM_PRIORITY_NORMAL);
1799224497Smav		cgd->ccb_h.func_code = XPT_GDEV_TYPE;
1800224497Smav		xpt_action((union ccb *)cgd);
1801224497Smav		if (ADA_WC >= 0 &&
1802224497Smav		    cgd->ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) {
1803224497Smav			softc->state = ADA_STATE_WCACHE;
1804224497Smav			xpt_release_ccb(done_ccb);
1805224497Smav			xpt_schedule(periph, CAM_PRIORITY_DEV);
1806249466Smav			goto out;
1807224497Smav		}
1808224497Smav		softc->state = ADA_STATE_NORMAL;
1809224497Smav		xpt_release_ccb(done_ccb);
1810249466Smav		/* Drop freeze taken due to CAM_DEV_QFREEZE */
1811249466Smav		cam_release_devq(path, 0, 0, 0, FALSE);
1812224497Smav		adaschedule(periph);
1813224497Smav		cam_periph_release_locked(periph);
1814224497Smav		return;
1815224497Smav	}
1816220412Smav	case ADA_CCB_WCACHE:
1817220412Smav	{
1818220412Smav		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1819220412Smav			if (adaerror(done_ccb, 0, 0) == ERESTART) {
1820249466Smav				goto out;
1821220412Smav			} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1822249466Smav				cam_release_devq(path,
1823220412Smav				    /*relsim_flags*/0,
1824220412Smav				    /*reduction*/0,
1825220412Smav				    /*timeout*/0,
1826220412Smav				    /*getcount_only*/0);
1827220412Smav			}
1828220412Smav		}
1829220412Smav
1830220412Smav		softc->state = ADA_STATE_NORMAL;
1831220412Smav		/*
1832220412Smav		 * Since our peripheral may be invalidated by an error
1833220412Smav		 * above or an external event, we must release our CCB
1834220412Smav		 * before releasing the reference on the peripheral.
1835220412Smav		 * The peripheral will only go away once the last reference
1836220412Smav		 * is removed, and we need it around for the CCB release
1837220412Smav		 * operation.
1838220412Smav		 */
1839220412Smav		xpt_release_ccb(done_ccb);
1840249466Smav		/* Drop freeze taken due to CAM_DEV_QFREEZE */
1841249466Smav		cam_release_devq(path, 0, 0, 0, FALSE);
1842220412Smav		adaschedule(periph);
1843220412Smav		cam_periph_release_locked(periph);
1844220412Smav		return;
1845220412Smav	}
1846195534Sscottl	case ADA_CCB_DUMP:
1847195534Sscottl		/* No-op.  We're polling */
1848195534Sscottl		return;
1849195534Sscottl	default:
1850195534Sscottl		break;
1851195534Sscottl	}
1852195534Sscottl	xpt_release_ccb(done_ccb);
1853195534Sscottl}
1854195534Sscottl
1855195534Sscottlstatic int
1856195534Sscottladaerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1857195534Sscottl{
1858195534Sscottl
1859203385Smav	return(cam_periph_error(ccb, cam_flags, sense_flags, NULL));
1860195534Sscottl}
1861195534Sscottl
1862195534Sscottlstatic void
1863198897Smavadagetparams(struct cam_periph *periph, struct ccb_getdev *cgd)
1864195534Sscottl{
1865195534Sscottl	struct ada_softc *softc = (struct ada_softc *)periph->softc;
1866195534Sscottl	struct disk_params *dp = &softc->params;
1867195534Sscottl	u_int64_t lbasize48;
1868195534Sscottl	u_int32_t lbasize;
1869195534Sscottl
1870198897Smav	dp->secsize = ata_logical_sector_size(&cgd->ident_data);
1871195534Sscottl	if ((cgd->ident_data.atavalid & ATA_FLAG_54_58) &&
1872195534Sscottl		cgd->ident_data.current_heads && cgd->ident_data.current_sectors) {
1873195534Sscottl		dp->heads = cgd->ident_data.current_heads;
1874195534Sscottl		dp->secs_per_track = cgd->ident_data.current_sectors;
1875195534Sscottl		dp->cylinders = cgd->ident_data.cylinders;
1876195534Sscottl		dp->sectors = (u_int32_t)cgd->ident_data.current_size_1 |
1877195534Sscottl			  ((u_int32_t)cgd->ident_data.current_size_2 << 16);
1878195534Sscottl	} else {
1879195534Sscottl		dp->heads = cgd->ident_data.heads;
1880195534Sscottl		dp->secs_per_track = cgd->ident_data.sectors;
1881195534Sscottl		dp->cylinders = cgd->ident_data.cylinders;
1882195534Sscottl		dp->sectors = cgd->ident_data.cylinders * dp->heads * dp->secs_per_track;
1883195534Sscottl	}
1884195534Sscottl	lbasize = (u_int32_t)cgd->ident_data.lba_size_1 |
1885195534Sscottl		  ((u_int32_t)cgd->ident_data.lba_size_2 << 16);
1886195534Sscottl
1887195534Sscottl	/* use the 28bit LBA size if valid or bigger than the CHS mapping */
1888195534Sscottl	if (cgd->ident_data.cylinders == 16383 || dp->sectors < lbasize)
1889195534Sscottl		dp->sectors = lbasize;
1890195534Sscottl
1891195534Sscottl	/* use the 48bit LBA size if valid */
1892195534Sscottl	lbasize48 = ((u_int64_t)cgd->ident_data.lba_size48_1) |
1893195534Sscottl		    ((u_int64_t)cgd->ident_data.lba_size48_2 << 16) |
1894195534Sscottl		    ((u_int64_t)cgd->ident_data.lba_size48_3 << 32) |
1895195534Sscottl		    ((u_int64_t)cgd->ident_data.lba_size48_4 << 48);
1896195534Sscottl	if ((cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) &&
1897195534Sscottl	    lbasize48 > ATA_MAX_28BIT_LBA)
1898195534Sscottl		dp->sectors = lbasize48;
1899195534Sscottl}
1900195534Sscottl
1901195534Sscottlstatic void
1902195534Sscottladasendorderedtag(void *arg)
1903195534Sscottl{
1904195534Sscottl	struct ada_softc *softc = arg;
1905195534Sscottl
1906195534Sscottl	if (ada_send_ordered) {
1907195534Sscottl		if ((softc->ordered_tag_count == 0)
1908195534Sscottl		 && ((softc->flags & ADA_FLAG_WENT_IDLE) == 0)) {
1909195534Sscottl			softc->flags |= ADA_FLAG_NEED_OTAG;
1910195534Sscottl		}
1911195534Sscottl		if (softc->outstanding_cmds > 0)
1912195534Sscottl			softc->flags &= ~ADA_FLAG_WENT_IDLE;
1913195534Sscottl
1914195534Sscottl		softc->ordered_tag_count = 0;
1915195534Sscottl	}
1916195534Sscottl	/* Queue us up again */
1917195534Sscottl	callout_reset(&softc->sendordered_c,
1918230921Smav	    (ada_default_timeout * hz) / ADA_ORDEREDTAG_INTERVAL,
1919195534Sscottl	    adasendorderedtag, softc);
1920195534Sscottl}
1921195534Sscottl
1922195534Sscottl/*
1923195534Sscottl * Step through all ADA peripheral drivers, and if the device is still open,
1924195534Sscottl * sync the disk cache to physical media.
1925195534Sscottl */
1926195534Sscottlstatic void
1927220650Smavadaflush(void)
1928195534Sscottl{
1929195534Sscottl	struct cam_periph *periph;
1930195534Sscottl	struct ada_softc *softc;
1931248872Smav	union ccb *ccb;
1932236814Smav	int error;
1933195534Sscottl
1934248868Smav	CAM_PERIPH_FOREACH(periph, &adadriver) {
1935251792Smav		softc = (struct ada_softc *)periph->softc;
1936251792Smav		if (SCHEDULER_STOPPED()) {
1937251792Smav			/* If we paniced with the lock held, do not recurse. */
1938251792Smav			if (!cam_periph_owned(periph) &&
1939251792Smav			    (softc->flags & ADA_FLAG_OPEN)) {
1940251792Smav				adadump(softc->disk, NULL, 0, 0, 0);
1941251792Smav			}
1942200180Smav			continue;
1943251792Smav		}
1944195534Sscottl		cam_periph_lock(periph);
1945195534Sscottl		/*
1946195534Sscottl		 * We only sync the cache if the drive is still open, and
1947195534Sscottl		 * if the drive is capable of it..
1948195534Sscottl		 */
1949195534Sscottl		if (((softc->flags & ADA_FLAG_OPEN) == 0) ||
1950195534Sscottl		    (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) == 0) {
1951195534Sscottl			cam_periph_unlock(periph);
1952195534Sscottl			continue;
1953195534Sscottl		}
1954195534Sscottl
1955248872Smav		ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1956248872Smav		cam_fill_ataio(&ccb->ataio,
1957236814Smav				    0,
1958195534Sscottl				    adadone,
1959195534Sscottl				    CAM_DIR_NONE,
1960195534Sscottl				    0,
1961195534Sscottl				    NULL,
1962195534Sscottl				    0,
1963195534Sscottl				    ada_default_timeout*1000);
1964195534Sscottl		if (softc->flags & ADA_FLAG_CAN_48BIT)
1965248872Smav			ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0);
1966195534Sscottl		else
1967248872Smav			ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0);
1968195534Sscottl
1969248872Smav		error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0,
1970248872Smav		    /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY,
1971248872Smav		    softc->disk->d_devstat);
1972236814Smav		if (error != 0)
1973195534Sscottl			xpt_print(periph->path, "Synchronize cache failed\n");
1974249048Smav		xpt_release_ccb(ccb);
1975195534Sscottl		cam_periph_unlock(periph);
1976195534Sscottl	}
1977220650Smav}
1978214279Sbrucec
1979220650Smavstatic void
1980220650Smavadaspindown(uint8_t cmd, int flags)
1981220650Smav{
1982220650Smav	struct cam_periph *periph;
1983220650Smav	struct ada_softc *softc;
1984248872Smav	union ccb *ccb;
1985236814Smav	int error;
1986214279Sbrucec
1987248868Smav	CAM_PERIPH_FOREACH(periph, &adadriver) {
1988214279Sbrucec		/* If we paniced with lock held - not recurse here. */
1989214279Sbrucec		if (cam_periph_owned(periph))
1990214279Sbrucec			continue;
1991214279Sbrucec		cam_periph_lock(periph);
1992214279Sbrucec		softc = (struct ada_softc *)periph->softc;
1993214279Sbrucec		/*
1994214279Sbrucec		 * We only spin-down the drive if it is capable of it..
1995214279Sbrucec		 */
1996214279Sbrucec		if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) {
1997214279Sbrucec			cam_periph_unlock(periph);
1998214279Sbrucec			continue;
1999214279Sbrucec		}
2000214279Sbrucec
2001214279Sbrucec		if (bootverbose)
2002214279Sbrucec			xpt_print(periph->path, "spin-down\n");
2003214279Sbrucec
2004248872Smav		ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2005248872Smav		cam_fill_ataio(&ccb->ataio,
2006236814Smav				    0,
2007214279Sbrucec				    adadone,
2008220650Smav				    CAM_DIR_NONE | flags,
2009214279Sbrucec				    0,
2010214279Sbrucec				    NULL,
2011214279Sbrucec				    0,
2012214279Sbrucec				    ada_default_timeout*1000);
2013248872Smav		ata_28bit_cmd(&ccb->ataio, cmd, 0, 0, 0);
2014214279Sbrucec
2015248872Smav		error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0,
2016248872Smav		    /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY,
2017248872Smav		    softc->disk->d_devstat);
2018236814Smav		if (error != 0)
2019214279Sbrucec			xpt_print(periph->path, "Spin-down disk failed\n");
2020249048Smav		xpt_release_ccb(ccb);
2021214279Sbrucec		cam_periph_unlock(periph);
2022214279Sbrucec	}
2023195534Sscottl}
2024195534Sscottl
2025220650Smavstatic void
2026220650Smavadashutdown(void *arg, int howto)
2027220650Smav{
2028220650Smav
2029220650Smav	adaflush();
2030220650Smav	if (ada_spindown_shutdown != 0 &&
2031220650Smav	    (howto & (RB_HALT | RB_POWEROFF)) != 0)
2032220650Smav		adaspindown(ATA_STANDBY_IMMEDIATE, 0);
2033220650Smav}
2034220650Smav
2035220650Smavstatic void
2036220650Smavadasuspend(void *arg)
2037220650Smav{
2038220650Smav
2039220650Smav	adaflush();
2040220650Smav	if (ada_spindown_suspend != 0)
2041220650Smav		adaspindown(ATA_SLEEP, CAM_DEV_QFREEZE);
2042220650Smav}
2043220650Smav
2044220650Smavstatic void
2045220650Smavadaresume(void *arg)
2046220650Smav{
2047220650Smav	struct cam_periph *periph;
2048220650Smav	struct ada_softc *softc;
2049220650Smav
2050220650Smav	if (ada_spindown_suspend == 0)
2051220650Smav		return;
2052220650Smav
2053248868Smav	CAM_PERIPH_FOREACH(periph, &adadriver) {
2054220650Smav		cam_periph_lock(periph);
2055220650Smav		softc = (struct ada_softc *)periph->softc;
2056220650Smav		/*
2057220650Smav		 * We only spin-down the drive if it is capable of it..
2058220650Smav		 */
2059220650Smav		if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) {
2060220650Smav			cam_periph_unlock(periph);
2061220650Smav			continue;
2062220650Smav		}
2063220650Smav
2064220650Smav		if (bootverbose)
2065220650Smav			xpt_print(periph->path, "resume\n");
2066220650Smav
2067220650Smav		/*
2068220650Smav		 * Drop freeze taken due to CAM_DEV_QFREEZE flag set on
2069220650Smav		 * sleep request.
2070220650Smav		 */
2071220650Smav		cam_release_devq(periph->path,
2072220650Smav			 /*relsim_flags*/0,
2073220650Smav			 /*openings*/0,
2074220650Smav			 /*timeout*/0,
2075220650Smav			 /*getcount_only*/0);
2076220650Smav
2077220650Smav		cam_periph_unlock(periph);
2078220650Smav	}
2079220650Smav}
2080220650Smav
2081195534Sscottl#endif /* _KERNEL */
2082