ata_da.c revision 257054
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 257054 2013-10-24 14:05:44Z 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,
83257054Smav	ADA_FLAG_WAS_OTAG	= 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;
133257054Smav	int	 outstanding_cmds;	/* Number of active commands */
134257054Smav	int	 refcount;		/* Active xpt_action() calls */
135195534Sscottl	ada_state state;
136257054Smav	ada_flags flags;
137195534Sscottl	ada_quirks quirks;
138248922Ssmh	int	 sort_io_queue;
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;
156195534Sscottl};
157195534Sscottl
158195534Sscottlstruct ada_quirk_entry {
159195534Sscottl	struct scsi_inquiry_pattern inq_pat;
160195534Sscottl	ada_quirks quirks;
161195534Sscottl};
162195534Sscottl
163199178Smavstatic struct ada_quirk_entry ada_quirk_table[] =
164199178Smav{
165199178Smav	{
166222520Smav		/* Hitachi Advanced Format (4k) drives */
167222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Hitachi H??????????E3*", "*" },
168222520Smav		/*quirks*/ADA_Q_4K
169222520Smav	},
170222520Smav	{
171222520Smav		/* Samsung Advanced Format (4k) drives */
172228819Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD155UI*", "*" },
173228819Smav		/*quirks*/ADA_Q_4K
174228819Smav	},
175228819Smav	{
176228819Smav		/* Samsung Advanced Format (4k) drives */
177222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD204UI*", "*" },
178222520Smav		/*quirks*/ADA_Q_4K
179222520Smav	},
180222520Smav	{
181222520Smav		/* Seagate Barracuda Green Advanced Format (4k) drives */
182222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST????DL*", "*" },
183222520Smav		/*quirks*/ADA_Q_4K
184222520Smav	},
185222520Smav	{
186228819Smav		/* Seagate Barracuda Advanced Format (4k) drives */
187228819Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST???DM*", "*" },
188228819Smav		/*quirks*/ADA_Q_4K
189228819Smav	},
190228819Smav	{
191228819Smav		/* Seagate Barracuda Advanced Format (4k) drives */
192228819Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST????DM*", "*" },
193228819Smav		/*quirks*/ADA_Q_4K
194228819Smav	},
195228819Smav	{
196222520Smav		/* Seagate Momentus Advanced Format (4k) drives */
197222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9500423AS*", "*" },
198222520Smav		/*quirks*/ADA_Q_4K
199222520Smav	},
200222520Smav	{
201222520Smav		/* Seagate Momentus Advanced Format (4k) drives */
202222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9500424AS*", "*" },
203222520Smav		/*quirks*/ADA_Q_4K
204222520Smav	},
205222520Smav	{
206222520Smav		/* Seagate Momentus Advanced Format (4k) drives */
207228819Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9640423AS*", "*" },
208228819Smav		/*quirks*/ADA_Q_4K
209228819Smav	},
210228819Smav	{
211228819Smav		/* Seagate Momentus Advanced Format (4k) drives */
212228819Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9640424AS*", "*" },
213228819Smav		/*quirks*/ADA_Q_4K
214228819Smav	},
215228819Smav	{
216228819Smav		/* Seagate Momentus Advanced Format (4k) drives */
217222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750420AS*", "*" },
218222520Smav		/*quirks*/ADA_Q_4K
219222520Smav	},
220222520Smav	{
221222520Smav		/* Seagate Momentus Advanced Format (4k) drives */
222222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750422AS*", "*" },
223222520Smav		/*quirks*/ADA_Q_4K
224222520Smav	},
225222520Smav	{
226228819Smav		/* Seagate Momentus Advanced Format (4k) drives */
227228819Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750423AS*", "*" },
228228819Smav		/*quirks*/ADA_Q_4K
229228819Smav	},
230228819Smav	{
231222520Smav		/* Seagate Momentus Thin Advanced Format (4k) drives */
232222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST???LT*", "*" },
233222520Smav		/*quirks*/ADA_Q_4K
234222520Smav	},
235222520Smav	{
236222520Smav		/* WDC Caviar Green Advanced Format (4k) drives */
237222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RS*", "*" },
238222520Smav		/*quirks*/ADA_Q_4K
239222520Smav	},
240222520Smav	{
241222520Smav		/* WDC Caviar Green Advanced Format (4k) drives */
242222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RX*", "*" },
243222520Smav		/*quirks*/ADA_Q_4K
244222520Smav	},
245222520Smav	{
246222520Smav		/* WDC Caviar Green Advanced Format (4k) drives */
247222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RS*", "*" },
248222520Smav		/*quirks*/ADA_Q_4K
249222520Smav	},
250222520Smav	{
251222520Smav		/* WDC Caviar Green Advanced Format (4k) drives */
252222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RX*", "*" },
253222520Smav		/*quirks*/ADA_Q_4K
254222520Smav	},
255222520Smav	{
256222520Smav		/* WDC Scorpio Black Advanced Format (4k) drives */
257222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PKT*", "*" },
258222520Smav		/*quirks*/ADA_Q_4K
259222520Smav	},
260222520Smav	{
261222520Smav		/* WDC Scorpio Black Advanced Format (4k) drives */
262222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PKT*", "*" },
263222520Smav		/*quirks*/ADA_Q_4K
264222520Smav	},
265222520Smav	{
266222520Smav		/* WDC Scorpio Blue Advanced Format (4k) drives */
267222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PVT*", "*" },
268222520Smav		/*quirks*/ADA_Q_4K
269222520Smav	},
270222520Smav	{
271222520Smav		/* WDC Scorpio Blue Advanced Format (4k) drives */
272222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PVT*", "*" },
273222520Smav		/*quirks*/ADA_Q_4K
274222520Smav	},
275251061Ssmh	/* SSDs */
276222520Smav	{
277241784Seadler		/*
278241784Seadler		 * Corsair Force 2 SSDs
279241784Seadler		 * 4k optimised & trim only works in 4k requests + 4k aligned
280241784Seadler		 */
281241784Seadler		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair CSSD-F*", "*" },
282241784Seadler		/*quirks*/ADA_Q_4K
283241784Seadler	},
284241784Seadler	{
285241784Seadler		/*
286241784Seadler		 * Corsair Force 3 SSDs
287241784Seadler		 * 4k optimised & trim only works in 4k requests + 4k aligned
288241784Seadler		 */
289241784Seadler		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Force 3*", "*" },
290241784Seadler		/*quirks*/ADA_Q_4K
291241784Seadler	},
292241784Seadler	{
293241784Seadler		/*
294256547Ssmh		 * Corsair Neutron GTX SSDs
295256547Ssmh		 * 4k optimised & trim only works in 4k requests + 4k aligned
296256547Ssmh		 */
297256547Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Neutron GTX*", "*" },
298256547Ssmh		/*quirks*/ADA_Q_4K
299256547Ssmh	},
300256547Ssmh	{
301256547Ssmh		/*
302251061Ssmh		 * Corsair Force GT SSDs
303241784Seadler		 * 4k optimised & trim only works in 4k requests + 4k aligned
304241784Seadler		 */
305251061Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Force GT*", "*" },
306241784Seadler		/*quirks*/ADA_Q_4K
307241784Seadler	},
308241784Seadler	{
309241784Seadler		/*
310251061Ssmh		 * Crucial M4 SSDs
311241784Seadler		 * 4k optimised & trim only works in 4k requests + 4k aligned
312241784Seadler		 */
313251061Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "M4-CT???M4SSD2*", "*" },
314241784Seadler		/*quirks*/ADA_Q_4K
315241784Seadler	},
316241784Seadler	{
317241784Seadler		/*
318251061Ssmh		 * Crucial RealSSD C300 SSDs
319251061Ssmh		 * 4k optimised
320251061Ssmh		 */
321251061Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "C300-CTFDDAC???MAG*",
322251061Ssmh		"*" }, /*quirks*/ADA_Q_4K
323251061Ssmh	},
324251061Ssmh	{
325251061Ssmh		/*
326251061Ssmh		 * Intel 320 Series SSDs
327241784Seadler		 * 4k optimised & trim only works in 4k requests + 4k aligned
328241784Seadler		 */
329251061Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSA2CW*", "*" },
330241784Seadler		/*quirks*/ADA_Q_4K
331241784Seadler	},
332241784Seadler	{
333241784Seadler		/*
334251061Ssmh		 * Intel 330 Series SSDs
335241784Seadler		 * 4k optimised & trim only works in 4k requests + 4k aligned
336241784Seadler		 */
337251061Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2CT*", "*" },
338241784Seadler		/*quirks*/ADA_Q_4K
339241784Seadler	},
340241784Seadler	{
341241784Seadler		/*
342251061Ssmh		 * Intel 510 Series SSDs
343251061Ssmh		 * 4k optimised & trim only works in 4k requests + 4k aligned
344241784Seadler		 */
345251061Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2MH*", "*" },
346251061Ssmh		/*quirks*/ADA_Q_4K
347241784Seadler	},
348241784Seadler	{
349241784Seadler		/*
350251061Ssmh		 * Intel 520 Series SSDs
351251061Ssmh		 * 4k optimised & trim only works in 4k requests + 4k aligned
352241784Seadler		 */
353251061Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2BW*", "*" },
354241784Seadler		/*quirks*/ADA_Q_4K
355241784Seadler	},
356241784Seadler	{
357241784Seadler		/*
358254329Ssmh		 * Intel X25-M Series SSDs
359254329Ssmh		 * 4k optimised & trim only works in 4k requests + 4k aligned
360254329Ssmh		 */
361254329Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSA2M*", "*" },
362254329Ssmh		/*quirks*/ADA_Q_4K
363254329Ssmh	},
364254329Ssmh	{
365254329Ssmh		/*
366251061Ssmh		 * Kingston E100 Series SSDs
367250532Seadler		 * 4k optimised & trim only works in 4k requests + 4k aligned
368250532Seadler		 */
369251061Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "KINGSTON SE100S3*", "*" },
370250532Seadler		/*quirks*/ADA_Q_4K
371250532Seadler	},
372250532Seadler	{
373250532Seadler		/*
374251061Ssmh		 * Kingston HyperX 3k SSDs
375241784Seadler		 * 4k optimised & trim only works in 4k requests + 4k aligned
376241784Seadler		 */
377251061Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "KINGSTON SH103S3*", "*" },
378241784Seadler		/*quirks*/ADA_Q_4K
379241784Seadler	},
380241784Seadler	{
381241784Seadler		/*
382254329Ssmh		 * Marvell SSDs (entry taken from OpenSolaris)
383254329Ssmh		 * 4k optimised & trim only works in 4k requests + 4k aligned
384254329Ssmh		 */
385254329Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "MARVELL SD88SA02*", "*" },
386254329Ssmh		/*quirks*/ADA_Q_4K
387254329Ssmh	},
388254329Ssmh	{
389254329Ssmh		/*
390254329Ssmh		 * OCZ Agility 2 SSDs
391254329Ssmh		 * 4k optimised & trim only works in 4k requests + 4k aligned
392254329Ssmh		 */
393254329Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-AGILITY2*", "*" },
394254329Ssmh		/*quirks*/ADA_Q_4K
395254329Ssmh	},
396254329Ssmh	{
397254329Ssmh		/*
398251061Ssmh		 * OCZ Agility 3 SSDs
399250532Seadler		 * 4k optimised & trim only works in 4k requests + 4k aligned
400250532Seadler		 */
401251061Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-AGILITY3*", "*" },
402250532Seadler		/*quirks*/ADA_Q_4K
403250532Seadler	},
404250532Seadler	{
405250532Seadler		/*
406241784Seadler		 * OCZ Deneva R Series SSDs
407241784Seadler		 * 4k optimised & trim only works in 4k requests + 4k aligned
408241784Seadler		 */
409241784Seadler		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "DENRSTE251M45*", "*" },
410241784Seadler		/*quirks*/ADA_Q_4K
411241784Seadler	},
412241784Seadler	{
413241784Seadler		/*
414251061Ssmh		 * OCZ Vertex 2 SSDs (inc pro series)
415241784Seadler		 * 4k optimised & trim only works in 4k requests + 4k aligned
416241784Seadler		 */
417251061Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ?VERTEX2*", "*" },
418241784Seadler		/*quirks*/ADA_Q_4K
419241784Seadler	},
420241784Seadler	{
421251061Ssmh		/*
422251061Ssmh		 * OCZ Vertex 3 SSDs
423251061Ssmh		 * 4k optimised & trim only works in 4k requests + 4k aligned
424251061Ssmh		 */
425251061Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-VERTEX3*", "*" },
426251061Ssmh		/*quirks*/ADA_Q_4K
427251061Ssmh	},
428251061Ssmh	{
429251061Ssmh		/*
430253091Ssmh		 * OCZ Vertex 4 SSDs
431253091Ssmh		 * 4k optimised & trim only works in 4k requests + 4k aligned
432253091Ssmh		 */
433253091Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-VERTEX4*", "*" },
434253091Ssmh		/*quirks*/ADA_Q_4K
435253091Ssmh	},
436253091Ssmh	{
437253091Ssmh		/*
438251061Ssmh		 * Samsung 830 Series SSDs
439251061Ssmh		 * 4k optimised
440251061Ssmh		 */
441251061Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG SSD 830 Series*", "*" },
442251061Ssmh		/*quirks*/ADA_Q_4K
443251061Ssmh	},
444251061Ssmh	{
445251061Ssmh		/*
446251061Ssmh		 * SuperTalent TeraDrive CT SSDs
447251061Ssmh		 * 4k optimised & trim only works in 4k requests + 4k aligned
448251061Ssmh		 */
449251061Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "FTM??CT25H*", "*" },
450251061Ssmh		/*quirks*/ADA_Q_4K
451251061Ssmh	},
452251061Ssmh	{
453251061Ssmh		/*
454251061Ssmh		 * XceedIOPS SATA SSDs
455251061Ssmh		 * 4k optimised
456251061Ssmh		 */
457251061Ssmh		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "SG9XCS2D*", "*" },
458251061Ssmh		/*quirks*/ADA_Q_4K
459251061Ssmh	},
460251061Ssmh	{
461199178Smav		/* Default */
462199178Smav		{
463199178Smav		  T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
464199178Smav		  /*vendor*/"*", /*product*/"*", /*revision*/"*"
465199178Smav		},
466199178Smav		/*quirks*/0
467199178Smav	},
468199178Smav};
469195534Sscottl
470195534Sscottlstatic	disk_strategy_t	adastrategy;
471195534Sscottlstatic	dumper_t	adadump;
472195534Sscottlstatic	periph_init_t	adainit;
473195534Sscottlstatic	void		adaasync(void *callback_arg, u_int32_t code,
474195534Sscottl				struct cam_path *path, void *arg);
475195534Sscottlstatic	void		adasysctlinit(void *context, int pending);
476195534Sscottlstatic	periph_ctor_t	adaregister;
477195534Sscottlstatic	periph_dtor_t	adacleanup;
478195534Sscottlstatic	periph_start_t	adastart;
479195534Sscottlstatic	periph_oninv_t	adaoninvalidate;
480195534Sscottlstatic	void		adadone(struct cam_periph *periph,
481195534Sscottl			       union ccb *done_ccb);
482195534Sscottlstatic  int		adaerror(union ccb *ccb, u_int32_t cam_flags,
483195534Sscottl				u_int32_t sense_flags);
484198897Smavstatic void		adagetparams(struct cam_periph *periph,
485195534Sscottl				struct ccb_getdev *cgd);
486195534Sscottlstatic timeout_t	adasendorderedtag;
487195534Sscottlstatic void		adashutdown(void *arg, int howto);
488220650Smavstatic void		adasuspend(void *arg);
489220650Smavstatic void		adaresume(void *arg);
490195534Sscottl
491221071Smav#ifndef	ADA_DEFAULT_LEGACY_ALIASES
492221071Smav#define	ADA_DEFAULT_LEGACY_ALIASES	1
493221071Smav#endif
494221071Smav
495195534Sscottl#ifndef ADA_DEFAULT_TIMEOUT
496195534Sscottl#define ADA_DEFAULT_TIMEOUT 30	/* Timeout in seconds */
497195534Sscottl#endif
498195534Sscottl
499195534Sscottl#ifndef	ADA_DEFAULT_RETRY
500195534Sscottl#define	ADA_DEFAULT_RETRY	4
501195534Sscottl#endif
502195534Sscottl
503195534Sscottl#ifndef	ADA_DEFAULT_SEND_ORDERED
504195534Sscottl#define	ADA_DEFAULT_SEND_ORDERED	1
505195534Sscottl#endif
506195534Sscottl
507214279Sbrucec#ifndef	ADA_DEFAULT_SPINDOWN_SHUTDOWN
508214279Sbrucec#define	ADA_DEFAULT_SPINDOWN_SHUTDOWN	1
509214279Sbrucec#endif
510214279Sbrucec
511220650Smav#ifndef	ADA_DEFAULT_SPINDOWN_SUSPEND
512220650Smav#define	ADA_DEFAULT_SPINDOWN_SUSPEND	1
513220650Smav#endif
514220650Smav
515224497Smav#ifndef	ADA_DEFAULT_READ_AHEAD
516224497Smav#define	ADA_DEFAULT_READ_AHEAD	1
517224497Smav#endif
518224497Smav
519220412Smav#ifndef	ADA_DEFAULT_WRITE_CACHE
520220412Smav#define	ADA_DEFAULT_WRITE_CACHE	1
521220412Smav#endif
522220412Smav
523224497Smav#define	ADA_RA	(softc->read_ahead >= 0 ? \
524224497Smav		 softc->read_ahead : ada_read_ahead)
525224497Smav#define	ADA_WC	(softc->write_cache >= 0 ? \
526224497Smav		 softc->write_cache : ada_write_cache)
527248922Ssmh#define	ADA_SIO	(softc->sort_io_queue >= 0 ? \
528248922Ssmh		 softc->sort_io_queue : cam_sort_io_queues)
529224497Smav
530208349Smarius/*
531208349Smarius * Most platforms map firmware geometry to actual, but some don't.  If
532208349Smarius * not overridden, default to nothing.
533208349Smarius */
534208349Smarius#ifndef ata_disk_firmware_geom_adjust
535208349Smarius#define	ata_disk_firmware_geom_adjust(disk)
536208349Smarius#endif
537195534Sscottl
538221071Smavstatic int ada_legacy_aliases = ADA_DEFAULT_LEGACY_ALIASES;
539195534Sscottlstatic int ada_retry_count = ADA_DEFAULT_RETRY;
540195534Sscottlstatic int ada_default_timeout = ADA_DEFAULT_TIMEOUT;
541195534Sscottlstatic int ada_send_ordered = ADA_DEFAULT_SEND_ORDERED;
542214279Sbrucecstatic int ada_spindown_shutdown = ADA_DEFAULT_SPINDOWN_SHUTDOWN;
543220650Smavstatic int ada_spindown_suspend = ADA_DEFAULT_SPINDOWN_SUSPEND;
544224497Smavstatic int ada_read_ahead = ADA_DEFAULT_READ_AHEAD;
545220412Smavstatic int ada_write_cache = ADA_DEFAULT_WRITE_CACHE;
546195534Sscottl
547227309Sedstatic SYSCTL_NODE(_kern_cam, OID_AUTO, ada, CTLFLAG_RD, 0,
548195534Sscottl            "CAM Direct Access Disk driver");
549221071SmavSYSCTL_INT(_kern_cam_ada, OID_AUTO, legacy_aliases, CTLFLAG_RW,
550221071Smav           &ada_legacy_aliases, 0, "Create legacy-like device aliases");
551221071SmavTUNABLE_INT("kern.cam.ada.legacy_aliases", &ada_legacy_aliases);
552195534SscottlSYSCTL_INT(_kern_cam_ada, OID_AUTO, retry_count, CTLFLAG_RW,
553195534Sscottl           &ada_retry_count, 0, "Normal I/O retry count");
554195534SscottlTUNABLE_INT("kern.cam.ada.retry_count", &ada_retry_count);
555195534SscottlSYSCTL_INT(_kern_cam_ada, OID_AUTO, default_timeout, CTLFLAG_RW,
556195534Sscottl           &ada_default_timeout, 0, "Normal I/O timeout (in seconds)");
557195534SscottlTUNABLE_INT("kern.cam.ada.default_timeout", &ada_default_timeout);
558238382SbruefferSYSCTL_INT(_kern_cam_ada, OID_AUTO, send_ordered, CTLFLAG_RW,
559195534Sscottl           &ada_send_ordered, 0, "Send Ordered Tags");
560238382SbruefferTUNABLE_INT("kern.cam.ada.send_ordered", &ada_send_ordered);
561214279SbrucecSYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_shutdown, CTLFLAG_RW,
562214279Sbrucec           &ada_spindown_shutdown, 0, "Spin down upon shutdown");
563214279SbrucecTUNABLE_INT("kern.cam.ada.spindown_shutdown", &ada_spindown_shutdown);
564220650SmavSYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_suspend, CTLFLAG_RW,
565220650Smav           &ada_spindown_suspend, 0, "Spin down upon suspend");
566220650SmavTUNABLE_INT("kern.cam.ada.spindown_suspend", &ada_spindown_suspend);
567224497SmavSYSCTL_INT(_kern_cam_ada, OID_AUTO, read_ahead, CTLFLAG_RW,
568224497Smav           &ada_read_ahead, 0, "Enable disk read-ahead");
569224497SmavTUNABLE_INT("kern.cam.ada.read_ahead", &ada_read_ahead);
570220412SmavSYSCTL_INT(_kern_cam_ada, OID_AUTO, write_cache, CTLFLAG_RW,
571220412Smav           &ada_write_cache, 0, "Enable disk write cache");
572220412SmavTUNABLE_INT("kern.cam.ada.write_cache", &ada_write_cache);
573195534Sscottl
574195534Sscottl/*
575195534Sscottl * ADA_ORDEREDTAG_INTERVAL determines how often, relative
576195534Sscottl * to the default timeout, we check to see whether an ordered
577195534Sscottl * tagged transaction is appropriate to prevent simple tag
578195534Sscottl * starvation.  Since we'd like to ensure that there is at least
579195534Sscottl * 1/2 of the timeout length left for a starved transaction to
580195534Sscottl * complete after we've sent an ordered tag, we must poll at least
581195534Sscottl * four times in every timeout period.  This takes care of the worst
582195534Sscottl * case where a starved transaction starts during an interval that
583195534Sscottl * meets the requirement "don't send an ordered tag" test so it takes
584195534Sscottl * us two intervals to determine that a tag must be sent.
585195534Sscottl */
586195534Sscottl#ifndef ADA_ORDEREDTAG_INTERVAL
587195534Sscottl#define ADA_ORDEREDTAG_INTERVAL 4
588195534Sscottl#endif
589195534Sscottl
590195534Sscottlstatic struct periph_driver adadriver =
591195534Sscottl{
592195534Sscottl	adainit, "ada",
593195534Sscottl	TAILQ_HEAD_INITIALIZER(adadriver.units), /* generation */ 0
594195534Sscottl};
595195534Sscottl
596195534SscottlPERIPHDRIVER_DECLARE(ada, adadriver);
597195534Sscottl
598227293Sedstatic MALLOC_DEFINE(M_ATADA, "ata_da", "ata_da buffers");
599195534Sscottl
600195534Sscottlstatic int
601195534Sscottladaopen(struct disk *dp)
602195534Sscottl{
603195534Sscottl	struct cam_periph *periph;
604195534Sscottl	struct ada_softc *softc;
605195534Sscottl	int error;
606195534Sscottl
607195534Sscottl	periph = (struct cam_periph *)dp->d_drv1;
608195534Sscottl	if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
609195534Sscottl		return(ENXIO);
610195534Sscottl	}
611195534Sscottl
612195534Sscottl	cam_periph_lock(periph);
613195534Sscottl	if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
614195534Sscottl		cam_periph_unlock(periph);
615195534Sscottl		cam_periph_release(periph);
616195534Sscottl		return (error);
617195534Sscottl	}
618195534Sscottl
619236602Smav	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
620236602Smav	    ("adaopen\n"));
621195534Sscottl
622249981Smav	softc = (struct ada_softc *)periph->softc;
623249981Smav	softc->flags |= ADA_FLAG_OPEN;
624195534Sscottl
625195534Sscottl	cam_periph_unhold(periph);
626195534Sscottl	cam_periph_unlock(periph);
627195534Sscottl	return (0);
628195534Sscottl}
629195534Sscottl
630195534Sscottlstatic int
631195534Sscottladaclose(struct disk *dp)
632195534Sscottl{
633195534Sscottl	struct	cam_periph *periph;
634195534Sscottl	struct	ada_softc *softc;
635195534Sscottl	union ccb *ccb;
636253724Smav	int error;
637195534Sscottl
638195534Sscottl	periph = (struct cam_periph *)dp->d_drv1;
639256843Smav	softc = (struct ada_softc *)periph->softc;
640195534Sscottl	cam_periph_lock(periph);
641195534Sscottl
642236602Smav	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
643236602Smav	    ("adaclose\n"));
644236602Smav
645195534Sscottl	/* We only sync the cache if the drive is capable of it. */
646253724Smav	if ((softc->flags & ADA_FLAG_DIRTY) != 0 &&
647253724Smav	    (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) != 0 &&
648256843Smav	    (periph->flags & CAM_PERIPH_INVALID) == 0 &&
649256843Smav	    cam_periph_hold(periph, PRIBIO) == 0) {
650195534Sscottl
651198382Smav		ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
652195534Sscottl		cam_fill_ataio(&ccb->ataio,
653195534Sscottl				    1,
654195534Sscottl				    adadone,
655195534Sscottl				    CAM_DIR_NONE,
656195534Sscottl				    0,
657195534Sscottl				    NULL,
658195534Sscottl				    0,
659195534Sscottl				    ada_default_timeout*1000);
660195534Sscottl
661195534Sscottl		if (softc->flags & ADA_FLAG_CAN_48BIT)
662195534Sscottl			ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0);
663195534Sscottl		else
664196659Smav			ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0);
665253724Smav		error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0,
666198328Smav		    /*sense_flags*/0, softc->disk->d_devstat);
667195534Sscottl
668253724Smav		if (error != 0)
669195534Sscottl			xpt_print(periph->path, "Synchronize cache failed\n");
670253724Smav		else
671253724Smav			softc->flags &= ~ADA_FLAG_DIRTY;
672195534Sscottl		xpt_release_ccb(ccb);
673256843Smav		cam_periph_unhold(periph);
674195534Sscottl	}
675195534Sscottl
676195534Sscottl	softc->flags &= ~ADA_FLAG_OPEN;
677256843Smav
678256843Smav	while (softc->refcount != 0)
679256843Smav		cam_periph_sleep(periph, &softc->refcount, PRIBIO, "adaclose", 1);
680195534Sscottl	cam_periph_unlock(periph);
681195534Sscottl	cam_periph_release(periph);
682195534Sscottl	return (0);
683195534Sscottl}
684195534Sscottl
685201139Smavstatic void
686201139Smavadaschedule(struct cam_periph *periph)
687201139Smav{
688201139Smav	struct ada_softc *softc = (struct ada_softc *)periph->softc;
689201139Smav
690249205Smav	if (softc->state != ADA_STATE_NORMAL)
691249205Smav		return;
692249205Smav
693224531Smav	/* Check if we have more work to do. */
694201139Smav	if (bioq_first(&softc->bio_queue) ||
695201139Smav	    (!softc->trim_running && bioq_first(&softc->trim_queue))) {
696256843Smav		xpt_schedule(periph, CAM_PRIORITY_NORMAL);
697201139Smav	}
698201139Smav}
699201139Smav
700195534Sscottl/*
701195534Sscottl * Actually translate the requested transfer into one the physical driver
702195534Sscottl * can understand.  The transfer is described by a buf and will include
703195534Sscottl * only one physical transfer.
704195534Sscottl */
705195534Sscottlstatic void
706195534Sscottladastrategy(struct bio *bp)
707195534Sscottl{
708195534Sscottl	struct cam_periph *periph;
709195534Sscottl	struct ada_softc *softc;
710195534Sscottl
711195534Sscottl	periph = (struct cam_periph *)bp->bio_disk->d_drv1;
712195534Sscottl	softc = (struct ada_softc *)periph->softc;
713195534Sscottl
714195534Sscottl	cam_periph_lock(periph);
715195534Sscottl
716236602Smav	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adastrategy(%p)\n", bp));
717236602Smav
718195534Sscottl	/*
719195534Sscottl	 * If the device has been made invalid, error out
720195534Sscottl	 */
721249981Smav	if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
722195534Sscottl		cam_periph_unlock(periph);
723195534Sscottl		biofinish(bp, NULL, ENXIO);
724195534Sscottl		return;
725195534Sscottl	}
726195534Sscottl
727195534Sscottl	/*
728195534Sscottl	 * Place it in the queue of disk activities for this disk
729195534Sscottl	 */
730201139Smav	if (bp->bio_cmd == BIO_DELETE &&
731248922Ssmh	    (softc->flags & ADA_FLAG_CAN_TRIM)) {
732248922Ssmh		if (ADA_SIO)
733248922Ssmh		    bioq_disksort(&softc->trim_queue, bp);
734248922Ssmh		else
735248922Ssmh		    bioq_insert_tail(&softc->trim_queue, bp);
736248922Ssmh	} else {
737248922Ssmh		if (ADA_SIO)
738248922Ssmh		    bioq_disksort(&softc->bio_queue, bp);
739248922Ssmh		else
740248922Ssmh		    bioq_insert_tail(&softc->bio_queue, bp);
741248922Ssmh	}
742195534Sscottl
743195534Sscottl	/*
744195534Sscottl	 * Schedule ourselves for performing the work.
745195534Sscottl	 */
746201139Smav	adaschedule(periph);
747195534Sscottl	cam_periph_unlock(periph);
748195534Sscottl
749195534Sscottl	return;
750195534Sscottl}
751195534Sscottl
752195534Sscottlstatic int
753195534Sscottladadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
754195534Sscottl{
755195534Sscottl	struct	    cam_periph *periph;
756195534Sscottl	struct	    ada_softc *softc;
757195534Sscottl	u_int	    secsize;
758195534Sscottl	union	    ccb ccb;
759195534Sscottl	struct	    disk *dp;
760195534Sscottl	uint64_t    lba;
761195534Sscottl	uint16_t    count;
762236814Smav	int	    error = 0;
763195534Sscottl
764195534Sscottl	dp = arg;
765195534Sscottl	periph = dp->d_drv1;
766195534Sscottl	softc = (struct ada_softc *)periph->softc;
767195534Sscottl	cam_periph_lock(periph);
768195534Sscottl	secsize = softc->params.secsize;
769195534Sscottl	lba = offset / secsize;
770195534Sscottl	count = length / secsize;
771195534Sscottl
772249981Smav	if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
773195534Sscottl		cam_periph_unlock(periph);
774195534Sscottl		return (ENXIO);
775195534Sscottl	}
776195534Sscottl
777195534Sscottl	if (length > 0) {
778198382Smav		xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
779195534Sscottl		ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
780195534Sscottl		cam_fill_ataio(&ccb.ataio,
781195534Sscottl		    0,
782195534Sscottl		    adadone,
783195534Sscottl		    CAM_DIR_OUT,
784195534Sscottl		    0,
785195534Sscottl		    (u_int8_t *) virtual,
786195534Sscottl		    length,
787195534Sscottl		    ada_default_timeout*1000);
788195534Sscottl		if ((softc->flags & ADA_FLAG_CAN_48BIT) &&
789195534Sscottl		    (lba + count >= ATA_MAX_28BIT_LBA ||
790195534Sscottl		    count >= 256)) {
791195534Sscottl			ata_48bit_cmd(&ccb.ataio, ATA_WRITE_DMA48,
792195534Sscottl			    0, lba, count);
793195534Sscottl		} else {
794196659Smav			ata_28bit_cmd(&ccb.ataio, ATA_WRITE_DMA,
795195534Sscottl			    0, lba, count);
796195534Sscottl		}
797195534Sscottl		xpt_polled_action(&ccb);
798195534Sscottl
799236814Smav		error = cam_periph_error(&ccb,
800236814Smav		    0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
801236814Smav		if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
802236814Smav			cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0,
803236814Smav			    /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
804236814Smav		if (error != 0)
805195534Sscottl			printf("Aborting dump due to I/O error.\n");
806236814Smav
807195534Sscottl		cam_periph_unlock(periph);
808236814Smav		return (error);
809195534Sscottl	}
810195534Sscottl
811195534Sscottl	if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) {
812198382Smav		xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
813195534Sscottl
814195534Sscottl		ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
815195534Sscottl		cam_fill_ataio(&ccb.ataio,
816236814Smav				    0,
817195534Sscottl				    adadone,
818195534Sscottl				    CAM_DIR_NONE,
819195534Sscottl				    0,
820195534Sscottl				    NULL,
821195534Sscottl				    0,
822195534Sscottl				    ada_default_timeout*1000);
823195534Sscottl
824195534Sscottl		if (softc->flags & ADA_FLAG_CAN_48BIT)
825195534Sscottl			ata_48bit_cmd(&ccb.ataio, ATA_FLUSHCACHE48, 0, 0, 0);
826195534Sscottl		else
827196659Smav			ata_28bit_cmd(&ccb.ataio, ATA_FLUSHCACHE, 0, 0, 0);
828195534Sscottl		xpt_polled_action(&ccb);
829195534Sscottl
830236814Smav		error = cam_periph_error(&ccb,
831236814Smav		    0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
832236814Smav		if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
833236814Smav			cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0,
834236814Smav			    /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
835236814Smav		if (error != 0)
836195534Sscottl			xpt_print(periph->path, "Synchronize cache failed\n");
837195534Sscottl	}
838195534Sscottl	cam_periph_unlock(periph);
839236814Smav	return (error);
840195534Sscottl}
841195534Sscottl
842195534Sscottlstatic void
843195534Sscottladainit(void)
844195534Sscottl{
845195534Sscottl	cam_status status;
846195534Sscottl
847195534Sscottl	/*
848195534Sscottl	 * Install a global async callback.  This callback will
849195534Sscottl	 * receive async callbacks like "new device found".
850195534Sscottl	 */
851195534Sscottl	status = xpt_register_async(AC_FOUND_DEVICE, adaasync, NULL, NULL);
852195534Sscottl
853195534Sscottl	if (status != CAM_REQ_CMP) {
854195534Sscottl		printf("ada: Failed to attach master async callback "
855195534Sscottl		       "due to status 0x%x!\n", status);
856195534Sscottl	} else if (ada_send_ordered) {
857195534Sscottl
858220650Smav		/* Register our event handlers */
859220650Smav		if ((EVENTHANDLER_REGISTER(power_suspend, adasuspend,
860220650Smav					   NULL, EVENTHANDLER_PRI_LAST)) == NULL)
861220650Smav		    printf("adainit: power event registration failed!\n");
862220650Smav		if ((EVENTHANDLER_REGISTER(power_resume, adaresume,
863220650Smav					   NULL, EVENTHANDLER_PRI_LAST)) == NULL)
864220650Smav		    printf("adainit: power event registration failed!\n");
865220650Smav		if ((EVENTHANDLER_REGISTER(shutdown_post_sync, adashutdown,
866195534Sscottl					   NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
867195534Sscottl		    printf("adainit: shutdown event registration failed!\n");
868195534Sscottl	}
869195534Sscottl}
870195534Sscottl
871249347Sken/*
872249347Sken * Callback from GEOM, called when it has finished cleaning up its
873249347Sken * resources.
874249347Sken */
875195534Sscottlstatic void
876249347Skenadadiskgonecb(struct disk *dp)
877249347Sken{
878249347Sken	struct cam_periph *periph;
879249347Sken
880249347Sken	periph = (struct cam_periph *)dp->d_drv1;
881249347Sken
882249347Sken	cam_periph_release(periph);
883249347Sken}
884249347Sken
885249347Skenstatic void
886195534Sscottladaoninvalidate(struct cam_periph *periph)
887195534Sscottl{
888195534Sscottl	struct ada_softc *softc;
889195534Sscottl
890195534Sscottl	softc = (struct ada_softc *)periph->softc;
891195534Sscottl
892195534Sscottl	/*
893195534Sscottl	 * De-register any async callbacks.
894195534Sscottl	 */
895195534Sscottl	xpt_register_async(0, adaasync, periph, periph->path);
896195534Sscottl
897195534Sscottl	/*
898195534Sscottl	 * Return all queued I/O with ENXIO.
899195534Sscottl	 * XXX Handle any transactions queued to the card
900195534Sscottl	 *     with XPT_ABORT_CCB.
901195534Sscottl	 */
902195534Sscottl	bioq_flush(&softc->bio_queue, NULL, ENXIO);
903201139Smav	bioq_flush(&softc->trim_queue, NULL, ENXIO);
904195534Sscottl
905195534Sscottl	disk_gone(softc->disk);
906195534Sscottl}
907195534Sscottl
908195534Sscottlstatic void
909195534Sscottladacleanup(struct cam_periph *periph)
910195534Sscottl{
911195534Sscottl	struct ada_softc *softc;
912195534Sscottl
913195534Sscottl	softc = (struct ada_softc *)periph->softc;
914195534Sscottl
915195534Sscottl	cam_periph_unlock(periph);
916195534Sscottl
917195534Sscottl	/*
918195534Sscottl	 * If we can't free the sysctl tree, oh well...
919195534Sscottl	 */
920195534Sscottl	if ((softc->flags & ADA_FLAG_SCTX_INIT) != 0
921195534Sscottl	    && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
922195534Sscottl		xpt_print(periph->path, "can't remove sysctl context\n");
923195534Sscottl	}
924195534Sscottl
925195534Sscottl	disk_destroy(softc->disk);
926195534Sscottl	callout_drain(&softc->sendordered_c);
927195534Sscottl	free(softc, M_DEVBUF);
928195534Sscottl	cam_periph_lock(periph);
929195534Sscottl}
930195534Sscottl
931195534Sscottlstatic void
932195534Sscottladaasync(void *callback_arg, u_int32_t code,
933195534Sscottl	struct cam_path *path, void *arg)
934195534Sscottl{
935236393Smav	struct ccb_getdev cgd;
936195534Sscottl	struct cam_periph *periph;
937220412Smav	struct ada_softc *softc;
938195534Sscottl
939195534Sscottl	periph = (struct cam_periph *)callback_arg;
940195534Sscottl	switch (code) {
941195534Sscottl	case AC_FOUND_DEVICE:
942195534Sscottl	{
943195534Sscottl		struct ccb_getdev *cgd;
944195534Sscottl		cam_status status;
945195534Sscottl
946195534Sscottl		cgd = (struct ccb_getdev *)arg;
947195534Sscottl		if (cgd == NULL)
948195534Sscottl			break;
949195534Sscottl
950195534Sscottl		if (cgd->protocol != PROTO_ATA)
951195534Sscottl			break;
952195534Sscottl
953195534Sscottl		/*
954195534Sscottl		 * Allocate a peripheral instance for
955195534Sscottl		 * this device and start the probe
956195534Sscottl		 * process.
957195534Sscottl		 */
958195534Sscottl		status = cam_periph_alloc(adaregister, adaoninvalidate,
959195534Sscottl					  adacleanup, adastart,
960195534Sscottl					  "ada", CAM_PERIPH_BIO,
961256843Smav					  path, adaasync,
962195534Sscottl					  AC_FOUND_DEVICE, cgd);
963195534Sscottl
964195534Sscottl		if (status != CAM_REQ_CMP
965195534Sscottl		 && status != CAM_REQ_INPROG)
966195534Sscottl			printf("adaasync: Unable to attach to new device "
967195534Sscottl				"due to status 0x%x\n", status);
968195534Sscottl		break;
969195534Sscottl	}
970236393Smav	case AC_GETDEV_CHANGED:
971236393Smav	{
972236393Smav		softc = (struct ada_softc *)periph->softc;
973236393Smav		xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
974236393Smav		cgd.ccb_h.func_code = XPT_GDEV_TYPE;
975236393Smav		xpt_action((union ccb *)&cgd);
976236393Smav
977236393Smav		if ((cgd.ident_data.capabilities1 & ATA_SUPPORT_DMA) &&
978236393Smav		    (cgd.inq_flags & SID_DMA))
979236393Smav			softc->flags |= ADA_FLAG_CAN_DMA;
980236393Smav		else
981236393Smav			softc->flags &= ~ADA_FLAG_CAN_DMA;
982249199Smarius		if (cgd.ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) {
983249199Smarius			softc->flags |= ADA_FLAG_CAN_48BIT;
984249199Smarius			if (cgd.inq_flags & SID_DMA48)
985249199Smarius				softc->flags |= ADA_FLAG_CAN_DMA48;
986249199Smarius			else
987249199Smarius				softc->flags &= ~ADA_FLAG_CAN_DMA48;
988249199Smarius		} else
989249199Smarius			softc->flags &= ~(ADA_FLAG_CAN_48BIT |
990249199Smarius			    ADA_FLAG_CAN_DMA48);
991236393Smav		if ((cgd.ident_data.satacapabilities & ATA_SUPPORT_NCQ) &&
992236393Smav		    (cgd.inq_flags & SID_DMA) && (cgd.inq_flags & SID_CmdQue))
993236393Smav			softc->flags |= ADA_FLAG_CAN_NCQ;
994236393Smav		else
995236393Smav			softc->flags &= ~ADA_FLAG_CAN_NCQ;
996236393Smav		if ((cgd.ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) &&
997236393Smav		    (cgd.inq_flags & SID_DMA))
998236393Smav			softc->flags |= ADA_FLAG_CAN_TRIM;
999236393Smav		else
1000236393Smav			softc->flags &= ~ADA_FLAG_CAN_TRIM;
1001236393Smav
1002236393Smav		cam_periph_async(periph, code, path, arg);
1003236393Smav		break;
1004236393Smav	}
1005235897Smav	case AC_ADVINFO_CHANGED:
1006235897Smav	{
1007235897Smav		uintptr_t buftype;
1008235897Smav
1009235897Smav		buftype = (uintptr_t)arg;
1010235897Smav		if (buftype == CDAI_TYPE_PHYS_PATH) {
1011235897Smav			struct ada_softc *softc;
1012235897Smav
1013235897Smav			softc = periph->softc;
1014235897Smav			disk_attr_changed(softc->disk, "GEOM::physpath",
1015235897Smav					  M_NOWAIT);
1016235897Smav		}
1017235897Smav		break;
1018235897Smav	}
1019220412Smav	case AC_SENT_BDR:
1020220412Smav	case AC_BUS_RESET:
1021220412Smav	{
1022220412Smav		softc = (struct ada_softc *)periph->softc;
1023220412Smav		cam_periph_async(periph, code, path, arg);
1024220412Smav		if (softc->state != ADA_STATE_NORMAL)
1025220412Smav			break;
1026220454Smav		xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1027220412Smav		cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1028220412Smav		xpt_action((union ccb *)&cgd);
1029224497Smav		if (ADA_RA >= 0 &&
1030224497Smav		    cgd.ident_data.support.command1 & ATA_SUPPORT_LOOKAHEAD)
1031224497Smav			softc->state = ADA_STATE_RAHEAD;
1032224497Smav		else if (ADA_WC >= 0 &&
1033224497Smav		    cgd.ident_data.support.command1 & ATA_SUPPORT_WRITECACHE)
1034224497Smav			softc->state = ADA_STATE_WCACHE;
1035224497Smav		else
1036224497Smav		    break;
1037256843Smav		if (cam_periph_acquire(periph) != CAM_REQ_CMP)
1038256843Smav			softc->state = ADA_STATE_NORMAL;
1039256843Smav		else
1040256843Smav			xpt_schedule(periph, CAM_PRIORITY_DEV);
1041220412Smav	}
1042195534Sscottl	default:
1043195534Sscottl		cam_periph_async(periph, code, path, arg);
1044195534Sscottl		break;
1045195534Sscottl	}
1046195534Sscottl}
1047195534Sscottl
1048195534Sscottlstatic void
1049195534Sscottladasysctlinit(void *context, int pending)
1050195534Sscottl{
1051195534Sscottl	struct cam_periph *periph;
1052195534Sscottl	struct ada_softc *softc;
1053195534Sscottl	char tmpstr[80], tmpstr2[80];
1054195534Sscottl
1055195534Sscottl	periph = (struct cam_periph *)context;
1056220454Smav
1057220454Smav	/* periph was held for us when this task was enqueued */
1058249981Smav	if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
1059220454Smav		cam_periph_release(periph);
1060195534Sscottl		return;
1061220454Smav	}
1062195534Sscottl
1063195534Sscottl	softc = (struct ada_softc *)periph->softc;
1064195534Sscottl	snprintf(tmpstr, sizeof(tmpstr), "CAM ADA unit %d", periph->unit_number);
1065195534Sscottl	snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1066195534Sscottl
1067195534Sscottl	sysctl_ctx_init(&softc->sysctl_ctx);
1068195534Sscottl	softc->flags |= ADA_FLAG_SCTX_INIT;
1069195534Sscottl	softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1070195534Sscottl		SYSCTL_STATIC_CHILDREN(_kern_cam_ada), OID_AUTO, tmpstr2,
1071195534Sscottl		CTLFLAG_RD, 0, tmpstr);
1072195534Sscottl	if (softc->sysctl_tree == NULL) {
1073195534Sscottl		printf("adasysctlinit: unable to allocate sysctl tree\n");
1074195534Sscottl		cam_periph_release(periph);
1075195534Sscottl		return;
1076195534Sscottl	}
1077195534Sscottl
1078220454Smav	SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1079224497Smav		OID_AUTO, "read_ahead", CTLFLAG_RW | CTLFLAG_MPSAFE,
1080224497Smav		&softc->read_ahead, 0, "Enable disk read ahead.");
1081224497Smav	SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1082220454Smav		OID_AUTO, "write_cache", CTLFLAG_RW | CTLFLAG_MPSAFE,
1083220454Smav		&softc->write_cache, 0, "Enable disk write cache.");
1084248922Ssmh	SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1085248922Ssmh		OID_AUTO, "sort_io_queue", CTLFLAG_RW | CTLFLAG_MPSAFE,
1086248922Ssmh		&softc->sort_io_queue, 0,
1087248922Ssmh		"Sort IO queue to try and optimise disk access patterns");
1088220454Smav#ifdef ADA_TEST_FAILURE
1089220454Smav	/*
1090220454Smav	 * Add a 'door bell' sysctl which allows one to set it from userland
1091220454Smav	 * and cause something bad to happen.  For the moment, we only allow
1092220454Smav	 * whacking the next read or write.
1093220454Smav	 */
1094220454Smav	SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1095220454Smav		OID_AUTO, "force_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
1096220454Smav		&softc->force_read_error, 0,
1097220454Smav		"Force a read error for the next N reads.");
1098220454Smav	SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1099220454Smav		OID_AUTO, "force_write_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
1100220454Smav		&softc->force_write_error, 0,
1101220454Smav		"Force a write error for the next N writes.");
1102220454Smav	SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1103220454Smav		OID_AUTO, "periodic_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
1104220454Smav		&softc->periodic_read_error, 0,
1105220454Smav		"Force a read error every N reads (don't set too low).");
1106220454Smav#endif
1107195534Sscottl	cam_periph_release(periph);
1108195534Sscottl}
1109195534Sscottl
1110223089Sgibbsstatic int
1111223089Sgibbsadagetattr(struct bio *bp)
1112223089Sgibbs{
1113241485Smav	int ret;
1114223089Sgibbs	struct cam_periph *periph;
1115223089Sgibbs
1116223089Sgibbs	periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1117241485Smav	cam_periph_lock(periph);
1118223089Sgibbs	ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
1119223089Sgibbs	    periph->path);
1120241485Smav	cam_periph_unlock(periph);
1121223089Sgibbs	if (ret == 0)
1122223089Sgibbs		bp->bio_completed = bp->bio_length;
1123223089Sgibbs	return ret;
1124223089Sgibbs}
1125223089Sgibbs
1126195534Sscottlstatic cam_status
1127195534Sscottladaregister(struct cam_periph *periph, void *arg)
1128195534Sscottl{
1129195534Sscottl	struct ada_softc *softc;
1130195534Sscottl	struct ccb_pathinq cpi;
1131195534Sscottl	struct ccb_getdev *cgd;
1132221071Smav	char   announce_buf[80], buf1[32];
1133195534Sscottl	struct disk_params *dp;
1134195534Sscottl	caddr_t match;
1135195534Sscottl	u_int maxio;
1136222520Smav	int legacy_id, quirks;
1137195534Sscottl
1138195534Sscottl	cgd = (struct ccb_getdev *)arg;
1139195534Sscottl	if (cgd == NULL) {
1140195534Sscottl		printf("adaregister: no getdev CCB, can't register device\n");
1141195534Sscottl		return(CAM_REQ_CMP_ERR);
1142195534Sscottl	}
1143195534Sscottl
1144195534Sscottl	softc = (struct ada_softc *)malloc(sizeof(*softc), M_DEVBUF,
1145195534Sscottl	    M_NOWAIT|M_ZERO);
1146195534Sscottl
1147195534Sscottl	if (softc == NULL) {
1148195534Sscottl		printf("adaregister: Unable to probe new device. "
1149198328Smav		    "Unable to allocate softc\n");
1150195534Sscottl		return(CAM_REQ_CMP_ERR);
1151195534Sscottl	}
1152195534Sscottl
1153195534Sscottl	bioq_init(&softc->bio_queue);
1154201139Smav	bioq_init(&softc->trim_queue);
1155195534Sscottl
1156236393Smav	if ((cgd->ident_data.capabilities1 & ATA_SUPPORT_DMA) &&
1157220886Smav	    (cgd->inq_flags & SID_DMA))
1158198328Smav		softc->flags |= ADA_FLAG_CAN_DMA;
1159249199Smarius	if (cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) {
1160195534Sscottl		softc->flags |= ADA_FLAG_CAN_48BIT;
1161249199Smarius		if (cgd->inq_flags & SID_DMA48)
1162249199Smarius			softc->flags |= ADA_FLAG_CAN_DMA48;
1163249199Smarius	}
1164195534Sscottl	if (cgd->ident_data.support.command2 & ATA_SUPPORT_FLUSHCACHE)
1165195534Sscottl		softc->flags |= ADA_FLAG_CAN_FLUSHCACHE;
1166214279Sbrucec	if (cgd->ident_data.support.command1 & ATA_SUPPORT_POWERMGT)
1167214279Sbrucec		softc->flags |= ADA_FLAG_CAN_POWERMGT;
1168236393Smav	if ((cgd->ident_data.satacapabilities & ATA_SUPPORT_NCQ) &&
1169220886Smav	    (cgd->inq_flags & SID_DMA) && (cgd->inq_flags & SID_CmdQue))
1170195534Sscottl		softc->flags |= ADA_FLAG_CAN_NCQ;
1171236393Smav	if ((cgd->ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) &&
1172236393Smav	    (cgd->inq_flags & SID_DMA)) {
1173201139Smav		softc->flags |= ADA_FLAG_CAN_TRIM;
1174201139Smav		softc->trim_max_ranges = TRIM_MAX_RANGES;
1175201139Smav		if (cgd->ident_data.max_dsm_blocks != 0) {
1176201139Smav			softc->trim_max_ranges =
1177249934Ssmh			    min(cgd->ident_data.max_dsm_blocks *
1178249934Ssmh				ATA_DSM_BLK_RANGES, softc->trim_max_ranges);
1179201139Smav		}
1180201139Smav	}
1181201139Smav	if (cgd->ident_data.support.command2 & ATA_SUPPORT_CFA)
1182201139Smav		softc->flags |= ADA_FLAG_CAN_CFA;
1183195534Sscottl
1184195534Sscottl	periph->softc = softc;
1185195534Sscottl
1186195534Sscottl	/*
1187195534Sscottl	 * See if this device has any quirks.
1188195534Sscottl	 */
1189199178Smav	match = cam_quirkmatch((caddr_t)&cgd->ident_data,
1190199178Smav			       (caddr_t)ada_quirk_table,
1191199178Smav			       sizeof(ada_quirk_table)/sizeof(*ada_quirk_table),
1192199178Smav			       sizeof(*ada_quirk_table), ata_identify_match);
1193195534Sscottl	if (match != NULL)
1194195534Sscottl		softc->quirks = ((struct ada_quirk_entry *)match)->quirks;
1195195534Sscottl	else
1196195534Sscottl		softc->quirks = ADA_Q_NONE;
1197195534Sscottl
1198195534Sscottl	bzero(&cpi, sizeof(cpi));
1199203108Smav	xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NONE);
1200195534Sscottl	cpi.ccb_h.func_code = XPT_PATH_INQ;
1201195534Sscottl	xpt_action((union ccb *)&cpi);
1202195534Sscottl
1203195534Sscottl	TASK_INIT(&softc->sysctl_task, 0, adasysctlinit, periph);
1204195534Sscottl
1205195534Sscottl	/*
1206195534Sscottl	 * Register this media as a disk
1207195534Sscottl	 */
1208220618Smav	(void)cam_periph_hold(periph, PRIBIO);
1209249106Smav	cam_periph_unlock(periph);
1210222520Smav	snprintf(announce_buf, sizeof(announce_buf),
1211222520Smav	    "kern.cam.ada.%d.quirks", periph->unit_number);
1212222520Smav	quirks = softc->quirks;
1213222520Smav	TUNABLE_INT_FETCH(announce_buf, &quirks);
1214222520Smav	softc->quirks = quirks;
1215224497Smav	softc->read_ahead = -1;
1216224497Smav	snprintf(announce_buf, sizeof(announce_buf),
1217224497Smav	    "kern.cam.ada.%d.read_ahead", periph->unit_number);
1218224497Smav	TUNABLE_INT_FETCH(announce_buf, &softc->read_ahead);
1219220618Smav	softc->write_cache = -1;
1220220618Smav	snprintf(announce_buf, sizeof(announce_buf),
1221220618Smav	    "kern.cam.ada.%d.write_cache", periph->unit_number);
1222220618Smav	TUNABLE_INT_FETCH(announce_buf, &softc->write_cache);
1223250033Ssmh	/* Disable queue sorting for non-rotational media by default. */
1224256956Ssmh	if (cgd->ident_data.media_rotation_rate == ATA_RATE_NON_ROTATING)
1225249941Ssmh		softc->sort_io_queue = 0;
1226249941Ssmh	else
1227249941Ssmh		softc->sort_io_queue = -1;
1228198897Smav	adagetparams(periph, cgd);
1229195534Sscottl	softc->disk = disk_alloc();
1230256956Ssmh	softc->disk->d_rotation_rate = cgd->ident_data.media_rotation_rate;
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;
1256256880Smav	softc->disk->d_flags = DISKFLAG_DIRECT_COMPLETION;
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;
1491257054Smav			softc->flags |= ADA_FLAG_WAS_OTAG;
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)
1745257054Smav			softc->flags |= ADA_FLAG_WAS_OTAG;
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) {
1907257054Smav		if (softc->outstanding_cmds > 0) {
1908257054Smav			if ((softc->flags & ADA_FLAG_WAS_OTAG) == 0)
1909257054Smav				softc->flags |= ADA_FLAG_NEED_OTAG;
1910257054Smav			softc->flags &= ~ADA_FLAG_WAS_OTAG;
1911195534Sscottl		}
1912195534Sscottl	}
1913195534Sscottl	/* Queue us up again */
1914195534Sscottl	callout_reset(&softc->sendordered_c,
1915230921Smav	    (ada_default_timeout * hz) / ADA_ORDEREDTAG_INTERVAL,
1916195534Sscottl	    adasendorderedtag, softc);
1917195534Sscottl}
1918195534Sscottl
1919195534Sscottl/*
1920195534Sscottl * Step through all ADA peripheral drivers, and if the device is still open,
1921195534Sscottl * sync the disk cache to physical media.
1922195534Sscottl */
1923195534Sscottlstatic void
1924220650Smavadaflush(void)
1925195534Sscottl{
1926195534Sscottl	struct cam_periph *periph;
1927195534Sscottl	struct ada_softc *softc;
1928248872Smav	union ccb *ccb;
1929236814Smav	int error;
1930195534Sscottl
1931248868Smav	CAM_PERIPH_FOREACH(periph, &adadriver) {
1932251792Smav		softc = (struct ada_softc *)periph->softc;
1933251792Smav		if (SCHEDULER_STOPPED()) {
1934251792Smav			/* If we paniced with the lock held, do not recurse. */
1935251792Smav			if (!cam_periph_owned(periph) &&
1936251792Smav			    (softc->flags & ADA_FLAG_OPEN)) {
1937251792Smav				adadump(softc->disk, NULL, 0, 0, 0);
1938251792Smav			}
1939200180Smav			continue;
1940251792Smav		}
1941195534Sscottl		cam_periph_lock(periph);
1942195534Sscottl		/*
1943195534Sscottl		 * We only sync the cache if the drive is still open, and
1944195534Sscottl		 * if the drive is capable of it..
1945195534Sscottl		 */
1946195534Sscottl		if (((softc->flags & ADA_FLAG_OPEN) == 0) ||
1947195534Sscottl		    (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) == 0) {
1948195534Sscottl			cam_periph_unlock(periph);
1949195534Sscottl			continue;
1950195534Sscottl		}
1951195534Sscottl
1952248872Smav		ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1953248872Smav		cam_fill_ataio(&ccb->ataio,
1954236814Smav				    0,
1955195534Sscottl				    adadone,
1956195534Sscottl				    CAM_DIR_NONE,
1957195534Sscottl				    0,
1958195534Sscottl				    NULL,
1959195534Sscottl				    0,
1960195534Sscottl				    ada_default_timeout*1000);
1961195534Sscottl		if (softc->flags & ADA_FLAG_CAN_48BIT)
1962248872Smav			ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0);
1963195534Sscottl		else
1964248872Smav			ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0);
1965195534Sscottl
1966248872Smav		error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0,
1967248872Smav		    /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY,
1968248872Smav		    softc->disk->d_devstat);
1969236814Smav		if (error != 0)
1970195534Sscottl			xpt_print(periph->path, "Synchronize cache failed\n");
1971249048Smav		xpt_release_ccb(ccb);
1972195534Sscottl		cam_periph_unlock(periph);
1973195534Sscottl	}
1974220650Smav}
1975214279Sbrucec
1976220650Smavstatic void
1977220650Smavadaspindown(uint8_t cmd, int flags)
1978220650Smav{
1979220650Smav	struct cam_periph *periph;
1980220650Smav	struct ada_softc *softc;
1981248872Smav	union ccb *ccb;
1982236814Smav	int error;
1983214279Sbrucec
1984248868Smav	CAM_PERIPH_FOREACH(periph, &adadriver) {
1985214279Sbrucec		/* If we paniced with lock held - not recurse here. */
1986214279Sbrucec		if (cam_periph_owned(periph))
1987214279Sbrucec			continue;
1988214279Sbrucec		cam_periph_lock(periph);
1989214279Sbrucec		softc = (struct ada_softc *)periph->softc;
1990214279Sbrucec		/*
1991214279Sbrucec		 * We only spin-down the drive if it is capable of it..
1992214279Sbrucec		 */
1993214279Sbrucec		if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) {
1994214279Sbrucec			cam_periph_unlock(periph);
1995214279Sbrucec			continue;
1996214279Sbrucec		}
1997214279Sbrucec
1998214279Sbrucec		if (bootverbose)
1999214279Sbrucec			xpt_print(periph->path, "spin-down\n");
2000214279Sbrucec
2001248872Smav		ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2002248872Smav		cam_fill_ataio(&ccb->ataio,
2003236814Smav				    0,
2004214279Sbrucec				    adadone,
2005220650Smav				    CAM_DIR_NONE | flags,
2006214279Sbrucec				    0,
2007214279Sbrucec				    NULL,
2008214279Sbrucec				    0,
2009214279Sbrucec				    ada_default_timeout*1000);
2010248872Smav		ata_28bit_cmd(&ccb->ataio, cmd, 0, 0, 0);
2011214279Sbrucec
2012248872Smav		error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0,
2013248872Smav		    /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY,
2014248872Smav		    softc->disk->d_devstat);
2015236814Smav		if (error != 0)
2016214279Sbrucec			xpt_print(periph->path, "Spin-down disk failed\n");
2017249048Smav		xpt_release_ccb(ccb);
2018214279Sbrucec		cam_periph_unlock(periph);
2019214279Sbrucec	}
2020195534Sscottl}
2021195534Sscottl
2022220650Smavstatic void
2023220650Smavadashutdown(void *arg, int howto)
2024220650Smav{
2025220650Smav
2026220650Smav	adaflush();
2027220650Smav	if (ada_spindown_shutdown != 0 &&
2028220650Smav	    (howto & (RB_HALT | RB_POWEROFF)) != 0)
2029220650Smav		adaspindown(ATA_STANDBY_IMMEDIATE, 0);
2030220650Smav}
2031220650Smav
2032220650Smavstatic void
2033220650Smavadasuspend(void *arg)
2034220650Smav{
2035220650Smav
2036220650Smav	adaflush();
2037220650Smav	if (ada_spindown_suspend != 0)
2038220650Smav		adaspindown(ATA_SLEEP, CAM_DEV_QFREEZE);
2039220650Smav}
2040220650Smav
2041220650Smavstatic void
2042220650Smavadaresume(void *arg)
2043220650Smav{
2044220650Smav	struct cam_periph *periph;
2045220650Smav	struct ada_softc *softc;
2046220650Smav
2047220650Smav	if (ada_spindown_suspend == 0)
2048220650Smav		return;
2049220650Smav
2050248868Smav	CAM_PERIPH_FOREACH(periph, &adadriver) {
2051220650Smav		cam_periph_lock(periph);
2052220650Smav		softc = (struct ada_softc *)periph->softc;
2053220650Smav		/*
2054220650Smav		 * We only spin-down the drive if it is capable of it..
2055220650Smav		 */
2056220650Smav		if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) {
2057220650Smav			cam_periph_unlock(periph);
2058220650Smav			continue;
2059220650Smav		}
2060220650Smav
2061220650Smav		if (bootverbose)
2062220650Smav			xpt_print(periph->path, "resume\n");
2063220650Smav
2064220650Smav		/*
2065220650Smav		 * Drop freeze taken due to CAM_DEV_QFREEZE flag set on
2066220650Smav		 * sleep request.
2067220650Smav		 */
2068220650Smav		cam_release_devq(periph->path,
2069220650Smav			 /*relsim_flags*/0,
2070220650Smav			 /*openings*/0,
2071220650Smav			 /*timeout*/0,
2072220650Smav			 /*getcount_only*/0);
2073220650Smav
2074220650Smav		cam_periph_unlock(periph);
2075220650Smav	}
2076220650Smav}
2077220650Smav
2078195534Sscottl#endif /* _KERNEL */
2079