ata_da.c revision 268815
1195534Sscottl/*-
2195534Sscottl * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org>
3195534Sscottl * All rights reserved.
4195534Sscottl *
5195534Sscottl * Redistribution and use in source and binary forms, with or without
6195534Sscottl * modification, are permitted provided that the following conditions
7195534Sscottl * are met:
8195534Sscottl * 1. Redistributions of source code must retain the above copyright
9195534Sscottl *    notice, this list of conditions and the following disclaimer,
10195534Sscottl *    without modification, immediately at the beginning of the file.
11195534Sscottl * 2. Redistributions in binary form must reproduce the above copyright
12195534Sscottl *    notice, this list of conditions and the following disclaimer in the
13195534Sscottl *    documentation and/or other materials provided with the distribution.
14195534Sscottl *
15195534Sscottl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16195534Sscottl * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17195534Sscottl * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18195534Sscottl * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19195534Sscottl * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20195534Sscottl * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21195534Sscottl * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22195534Sscottl * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23195534Sscottl * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24195534Sscottl * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25195534Sscottl */
26195534Sscottl
27195534Sscottl#include <sys/cdefs.h>
28195534Sscottl__FBSDID("$FreeBSD: stable/10/sys/cam/ata/ata_da.c 268815 2014-07-17 22:58:05Z imp $");
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,
83260387Sscottl	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];
127260387Sscottl	TAILQ_HEAD(, bio) bps;
128201139Smav};
129201139Smav
130195534Sscottlstruct ada_softc {
131195534Sscottl	struct	 bio_queue_head bio_queue;
132201139Smav	struct	 bio_queue_head trim_queue;
133260387Sscottl	int	 outstanding_cmds;	/* Number of active commands */
134260387Sscottl	int	 refcount;		/* Active xpt_action() calls */
135195534Sscottl	ada_state state;
136260387Sscottl	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		/*
294260475Smav		 * Corsair Neutron GTX SSDs
295260475Smav		 * 4k optimised & trim only works in 4k requests + 4k aligned
296260475Smav		 */
297260475Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Neutron GTX*", "*" },
298260475Smav		/*quirks*/ADA_Q_4K
299260475Smav	},
300260475Smav	{
301260475Smav		/*
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;
639260387Sscottl	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 &&
648260387Sscottl	    (periph->flags & CAM_PERIPH_INVALID) == 0 &&
649260387Sscottl	    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);
673260387Sscottl		cam_periph_unhold(periph);
674195534Sscottl	}
675195534Sscottl
676195534Sscottl	softc->flags &= ~ADA_FLAG_OPEN;
677260387Sscottl
678260387Sscottl	while (softc->refcount != 0)
679260387Sscottl		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))) {
696260387Sscottl		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	 */
730268815Simp	if (bp->bio_cmd == BIO_DELETE) {
731268815Simp		KASSERT((softc->flags & ADA_FLAG_CAN_TRIM) ||
732268815Simp			((softc->flags & ADA_FLAG_CAN_CFA) &&
733268815Simp			 !(softc->flags & ADA_FLAG_CAN_48BIT)),
734268815Simp			("BIO_DELETE but no supported TRIM method."));
735248922Ssmh		if (ADA_SIO)
736248922Ssmh		    bioq_disksort(&softc->trim_queue, bp);
737248922Ssmh		else
738248922Ssmh		    bioq_insert_tail(&softc->trim_queue, bp);
739248922Ssmh	} else {
740248922Ssmh		if (ADA_SIO)
741248922Ssmh		    bioq_disksort(&softc->bio_queue, bp);
742248922Ssmh		else
743248922Ssmh		    bioq_insert_tail(&softc->bio_queue, bp);
744248922Ssmh	}
745195534Sscottl
746195534Sscottl	/*
747195534Sscottl	 * Schedule ourselves for performing the work.
748195534Sscottl	 */
749201139Smav	adaschedule(periph);
750195534Sscottl	cam_periph_unlock(periph);
751195534Sscottl
752195534Sscottl	return;
753195534Sscottl}
754195534Sscottl
755195534Sscottlstatic int
756195534Sscottladadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
757195534Sscottl{
758195534Sscottl	struct	    cam_periph *periph;
759195534Sscottl	struct	    ada_softc *softc;
760195534Sscottl	u_int	    secsize;
761195534Sscottl	union	    ccb ccb;
762195534Sscottl	struct	    disk *dp;
763195534Sscottl	uint64_t    lba;
764195534Sscottl	uint16_t    count;
765236814Smav	int	    error = 0;
766195534Sscottl
767195534Sscottl	dp = arg;
768195534Sscottl	periph = dp->d_drv1;
769195534Sscottl	softc = (struct ada_softc *)periph->softc;
770195534Sscottl	cam_periph_lock(periph);
771195534Sscottl	secsize = softc->params.secsize;
772195534Sscottl	lba = offset / secsize;
773195534Sscottl	count = length / secsize;
774195534Sscottl
775249981Smav	if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
776195534Sscottl		cam_periph_unlock(periph);
777195534Sscottl		return (ENXIO);
778195534Sscottl	}
779195534Sscottl
780195534Sscottl	if (length > 0) {
781198382Smav		xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
782195534Sscottl		ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
783195534Sscottl		cam_fill_ataio(&ccb.ataio,
784195534Sscottl		    0,
785195534Sscottl		    adadone,
786195534Sscottl		    CAM_DIR_OUT,
787195534Sscottl		    0,
788195534Sscottl		    (u_int8_t *) virtual,
789195534Sscottl		    length,
790195534Sscottl		    ada_default_timeout*1000);
791195534Sscottl		if ((softc->flags & ADA_FLAG_CAN_48BIT) &&
792195534Sscottl		    (lba + count >= ATA_MAX_28BIT_LBA ||
793195534Sscottl		    count >= 256)) {
794195534Sscottl			ata_48bit_cmd(&ccb.ataio, ATA_WRITE_DMA48,
795195534Sscottl			    0, lba, count);
796195534Sscottl		} else {
797196659Smav			ata_28bit_cmd(&ccb.ataio, ATA_WRITE_DMA,
798195534Sscottl			    0, lba, count);
799195534Sscottl		}
800195534Sscottl		xpt_polled_action(&ccb);
801195534Sscottl
802236814Smav		error = cam_periph_error(&ccb,
803236814Smav		    0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
804236814Smav		if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
805236814Smav			cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0,
806236814Smav			    /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
807236814Smav		if (error != 0)
808195534Sscottl			printf("Aborting dump due to I/O error.\n");
809236814Smav
810195534Sscottl		cam_periph_unlock(periph);
811236814Smav		return (error);
812195534Sscottl	}
813195534Sscottl
814195534Sscottl	if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) {
815198382Smav		xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
816195534Sscottl
817195534Sscottl		ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
818195534Sscottl		cam_fill_ataio(&ccb.ataio,
819236814Smav				    0,
820195534Sscottl				    adadone,
821195534Sscottl				    CAM_DIR_NONE,
822195534Sscottl				    0,
823195534Sscottl				    NULL,
824195534Sscottl				    0,
825195534Sscottl				    ada_default_timeout*1000);
826195534Sscottl
827195534Sscottl		if (softc->flags & ADA_FLAG_CAN_48BIT)
828195534Sscottl			ata_48bit_cmd(&ccb.ataio, ATA_FLUSHCACHE48, 0, 0, 0);
829195534Sscottl		else
830196659Smav			ata_28bit_cmd(&ccb.ataio, ATA_FLUSHCACHE, 0, 0, 0);
831195534Sscottl		xpt_polled_action(&ccb);
832195534Sscottl
833236814Smav		error = cam_periph_error(&ccb,
834236814Smav		    0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
835236814Smav		if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
836236814Smav			cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0,
837236814Smav			    /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
838236814Smav		if (error != 0)
839195534Sscottl			xpt_print(periph->path, "Synchronize cache failed\n");
840195534Sscottl	}
841195534Sscottl	cam_periph_unlock(periph);
842236814Smav	return (error);
843195534Sscottl}
844195534Sscottl
845195534Sscottlstatic void
846195534Sscottladainit(void)
847195534Sscottl{
848195534Sscottl	cam_status status;
849195534Sscottl
850195534Sscottl	/*
851195534Sscottl	 * Install a global async callback.  This callback will
852195534Sscottl	 * receive async callbacks like "new device found".
853195534Sscottl	 */
854195534Sscottl	status = xpt_register_async(AC_FOUND_DEVICE, adaasync, NULL, NULL);
855195534Sscottl
856195534Sscottl	if (status != CAM_REQ_CMP) {
857195534Sscottl		printf("ada: Failed to attach master async callback "
858195534Sscottl		       "due to status 0x%x!\n", status);
859195534Sscottl	} else if (ada_send_ordered) {
860195534Sscottl
861220650Smav		/* Register our event handlers */
862220650Smav		if ((EVENTHANDLER_REGISTER(power_suspend, adasuspend,
863220650Smav					   NULL, EVENTHANDLER_PRI_LAST)) == NULL)
864220650Smav		    printf("adainit: power event registration failed!\n");
865220650Smav		if ((EVENTHANDLER_REGISTER(power_resume, adaresume,
866220650Smav					   NULL, EVENTHANDLER_PRI_LAST)) == NULL)
867220650Smav		    printf("adainit: power event registration failed!\n");
868220650Smav		if ((EVENTHANDLER_REGISTER(shutdown_post_sync, adashutdown,
869195534Sscottl					   NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
870195534Sscottl		    printf("adainit: shutdown event registration failed!\n");
871195534Sscottl	}
872195534Sscottl}
873195534Sscottl
874249347Sken/*
875249347Sken * Callback from GEOM, called when it has finished cleaning up its
876249347Sken * resources.
877249347Sken */
878195534Sscottlstatic void
879249347Skenadadiskgonecb(struct disk *dp)
880249347Sken{
881249347Sken	struct cam_periph *periph;
882249347Sken
883249347Sken	periph = (struct cam_periph *)dp->d_drv1;
884249347Sken
885249347Sken	cam_periph_release(periph);
886249347Sken}
887249347Sken
888249347Skenstatic void
889195534Sscottladaoninvalidate(struct cam_periph *periph)
890195534Sscottl{
891195534Sscottl	struct ada_softc *softc;
892195534Sscottl
893195534Sscottl	softc = (struct ada_softc *)periph->softc;
894195534Sscottl
895195534Sscottl	/*
896195534Sscottl	 * De-register any async callbacks.
897195534Sscottl	 */
898195534Sscottl	xpt_register_async(0, adaasync, periph, periph->path);
899195534Sscottl
900195534Sscottl	/*
901195534Sscottl	 * Return all queued I/O with ENXIO.
902195534Sscottl	 * XXX Handle any transactions queued to the card
903195534Sscottl	 *     with XPT_ABORT_CCB.
904195534Sscottl	 */
905195534Sscottl	bioq_flush(&softc->bio_queue, NULL, ENXIO);
906201139Smav	bioq_flush(&softc->trim_queue, NULL, ENXIO);
907195534Sscottl
908195534Sscottl	disk_gone(softc->disk);
909195534Sscottl}
910195534Sscottl
911195534Sscottlstatic void
912195534Sscottladacleanup(struct cam_periph *periph)
913195534Sscottl{
914195534Sscottl	struct ada_softc *softc;
915195534Sscottl
916195534Sscottl	softc = (struct ada_softc *)periph->softc;
917195534Sscottl
918195534Sscottl	cam_periph_unlock(periph);
919195534Sscottl
920195534Sscottl	/*
921195534Sscottl	 * If we can't free the sysctl tree, oh well...
922195534Sscottl	 */
923195534Sscottl	if ((softc->flags & ADA_FLAG_SCTX_INIT) != 0
924195534Sscottl	    && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
925195534Sscottl		xpt_print(periph->path, "can't remove sysctl context\n");
926195534Sscottl	}
927195534Sscottl
928195534Sscottl	disk_destroy(softc->disk);
929195534Sscottl	callout_drain(&softc->sendordered_c);
930195534Sscottl	free(softc, M_DEVBUF);
931195534Sscottl	cam_periph_lock(periph);
932195534Sscottl}
933195534Sscottl
934195534Sscottlstatic void
935195534Sscottladaasync(void *callback_arg, u_int32_t code,
936195534Sscottl	struct cam_path *path, void *arg)
937195534Sscottl{
938236393Smav	struct ccb_getdev cgd;
939195534Sscottl	struct cam_periph *periph;
940220412Smav	struct ada_softc *softc;
941195534Sscottl
942195534Sscottl	periph = (struct cam_periph *)callback_arg;
943195534Sscottl	switch (code) {
944195534Sscottl	case AC_FOUND_DEVICE:
945195534Sscottl	{
946195534Sscottl		struct ccb_getdev *cgd;
947195534Sscottl		cam_status status;
948195534Sscottl
949195534Sscottl		cgd = (struct ccb_getdev *)arg;
950195534Sscottl		if (cgd == NULL)
951195534Sscottl			break;
952195534Sscottl
953195534Sscottl		if (cgd->protocol != PROTO_ATA)
954195534Sscottl			break;
955195534Sscottl
956195534Sscottl		/*
957195534Sscottl		 * Allocate a peripheral instance for
958195534Sscottl		 * this device and start the probe
959195534Sscottl		 * process.
960195534Sscottl		 */
961195534Sscottl		status = cam_periph_alloc(adaregister, adaoninvalidate,
962195534Sscottl					  adacleanup, adastart,
963195534Sscottl					  "ada", CAM_PERIPH_BIO,
964260387Sscottl					  path, adaasync,
965195534Sscottl					  AC_FOUND_DEVICE, cgd);
966195534Sscottl
967195534Sscottl		if (status != CAM_REQ_CMP
968195534Sscottl		 && status != CAM_REQ_INPROG)
969195534Sscottl			printf("adaasync: Unable to attach to new device "
970195534Sscottl				"due to status 0x%x\n", status);
971195534Sscottl		break;
972195534Sscottl	}
973236393Smav	case AC_GETDEV_CHANGED:
974236393Smav	{
975236393Smav		softc = (struct ada_softc *)periph->softc;
976236393Smav		xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
977236393Smav		cgd.ccb_h.func_code = XPT_GDEV_TYPE;
978236393Smav		xpt_action((union ccb *)&cgd);
979236393Smav
980236393Smav		if ((cgd.ident_data.capabilities1 & ATA_SUPPORT_DMA) &&
981236393Smav		    (cgd.inq_flags & SID_DMA))
982236393Smav			softc->flags |= ADA_FLAG_CAN_DMA;
983236393Smav		else
984236393Smav			softc->flags &= ~ADA_FLAG_CAN_DMA;
985249199Smarius		if (cgd.ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) {
986249199Smarius			softc->flags |= ADA_FLAG_CAN_48BIT;
987249199Smarius			if (cgd.inq_flags & SID_DMA48)
988249199Smarius				softc->flags |= ADA_FLAG_CAN_DMA48;
989249199Smarius			else
990249199Smarius				softc->flags &= ~ADA_FLAG_CAN_DMA48;
991249199Smarius		} else
992249199Smarius			softc->flags &= ~(ADA_FLAG_CAN_48BIT |
993249199Smarius			    ADA_FLAG_CAN_DMA48);
994236393Smav		if ((cgd.ident_data.satacapabilities & ATA_SUPPORT_NCQ) &&
995236393Smav		    (cgd.inq_flags & SID_DMA) && (cgd.inq_flags & SID_CmdQue))
996236393Smav			softc->flags |= ADA_FLAG_CAN_NCQ;
997236393Smav		else
998236393Smav			softc->flags &= ~ADA_FLAG_CAN_NCQ;
999236393Smav		if ((cgd.ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) &&
1000236393Smav		    (cgd.inq_flags & SID_DMA))
1001236393Smav			softc->flags |= ADA_FLAG_CAN_TRIM;
1002236393Smav		else
1003236393Smav			softc->flags &= ~ADA_FLAG_CAN_TRIM;
1004236393Smav
1005236393Smav		cam_periph_async(periph, code, path, arg);
1006236393Smav		break;
1007236393Smav	}
1008235897Smav	case AC_ADVINFO_CHANGED:
1009235897Smav	{
1010235897Smav		uintptr_t buftype;
1011235897Smav
1012235897Smav		buftype = (uintptr_t)arg;
1013235897Smav		if (buftype == CDAI_TYPE_PHYS_PATH) {
1014235897Smav			struct ada_softc *softc;
1015235897Smav
1016235897Smav			softc = periph->softc;
1017235897Smav			disk_attr_changed(softc->disk, "GEOM::physpath",
1018235897Smav					  M_NOWAIT);
1019235897Smav		}
1020235897Smav		break;
1021235897Smav	}
1022220412Smav	case AC_SENT_BDR:
1023220412Smav	case AC_BUS_RESET:
1024220412Smav	{
1025220412Smav		softc = (struct ada_softc *)periph->softc;
1026220412Smav		cam_periph_async(periph, code, path, arg);
1027220412Smav		if (softc->state != ADA_STATE_NORMAL)
1028220412Smav			break;
1029220454Smav		xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1030220412Smav		cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1031220412Smav		xpt_action((union ccb *)&cgd);
1032224497Smav		if (ADA_RA >= 0 &&
1033224497Smav		    cgd.ident_data.support.command1 & ATA_SUPPORT_LOOKAHEAD)
1034224497Smav			softc->state = ADA_STATE_RAHEAD;
1035224497Smav		else if (ADA_WC >= 0 &&
1036224497Smav		    cgd.ident_data.support.command1 & ATA_SUPPORT_WRITECACHE)
1037224497Smav			softc->state = ADA_STATE_WCACHE;
1038224497Smav		else
1039224497Smav		    break;
1040260387Sscottl		if (cam_periph_acquire(periph) != CAM_REQ_CMP)
1041260387Sscottl			softc->state = ADA_STATE_NORMAL;
1042260387Sscottl		else
1043260387Sscottl			xpt_schedule(periph, CAM_PRIORITY_DEV);
1044220412Smav	}
1045195534Sscottl	default:
1046195534Sscottl		cam_periph_async(periph, code, path, arg);
1047195534Sscottl		break;
1048195534Sscottl	}
1049195534Sscottl}
1050195534Sscottl
1051195534Sscottlstatic void
1052195534Sscottladasysctlinit(void *context, int pending)
1053195534Sscottl{
1054195534Sscottl	struct cam_periph *periph;
1055195534Sscottl	struct ada_softc *softc;
1056195534Sscottl	char tmpstr[80], tmpstr2[80];
1057195534Sscottl
1058195534Sscottl	periph = (struct cam_periph *)context;
1059220454Smav
1060220454Smav	/* periph was held for us when this task was enqueued */
1061249981Smav	if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
1062220454Smav		cam_periph_release(periph);
1063195534Sscottl		return;
1064220454Smav	}
1065195534Sscottl
1066195534Sscottl	softc = (struct ada_softc *)periph->softc;
1067195534Sscottl	snprintf(tmpstr, sizeof(tmpstr), "CAM ADA unit %d", periph->unit_number);
1068195534Sscottl	snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1069195534Sscottl
1070195534Sscottl	sysctl_ctx_init(&softc->sysctl_ctx);
1071195534Sscottl	softc->flags |= ADA_FLAG_SCTX_INIT;
1072195534Sscottl	softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1073195534Sscottl		SYSCTL_STATIC_CHILDREN(_kern_cam_ada), OID_AUTO, tmpstr2,
1074195534Sscottl		CTLFLAG_RD, 0, tmpstr);
1075195534Sscottl	if (softc->sysctl_tree == NULL) {
1076195534Sscottl		printf("adasysctlinit: unable to allocate sysctl tree\n");
1077195534Sscottl		cam_periph_release(periph);
1078195534Sscottl		return;
1079195534Sscottl	}
1080195534Sscottl
1081220454Smav	SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1082224497Smav		OID_AUTO, "read_ahead", CTLFLAG_RW | CTLFLAG_MPSAFE,
1083224497Smav		&softc->read_ahead, 0, "Enable disk read ahead.");
1084224497Smav	SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1085220454Smav		OID_AUTO, "write_cache", CTLFLAG_RW | CTLFLAG_MPSAFE,
1086220454Smav		&softc->write_cache, 0, "Enable disk write cache.");
1087248922Ssmh	SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1088248922Ssmh		OID_AUTO, "sort_io_queue", CTLFLAG_RW | CTLFLAG_MPSAFE,
1089248922Ssmh		&softc->sort_io_queue, 0,
1090248922Ssmh		"Sort IO queue to try and optimise disk access patterns");
1091220454Smav#ifdef ADA_TEST_FAILURE
1092220454Smav	/*
1093220454Smav	 * Add a 'door bell' sysctl which allows one to set it from userland
1094220454Smav	 * and cause something bad to happen.  For the moment, we only allow
1095220454Smav	 * whacking the next read or write.
1096220454Smav	 */
1097220454Smav	SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1098220454Smav		OID_AUTO, "force_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
1099220454Smav		&softc->force_read_error, 0,
1100220454Smav		"Force a read error for the next N reads.");
1101220454Smav	SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1102220454Smav		OID_AUTO, "force_write_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
1103220454Smav		&softc->force_write_error, 0,
1104220454Smav		"Force a write error for the next N writes.");
1105220454Smav	SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1106220454Smav		OID_AUTO, "periodic_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
1107220454Smav		&softc->periodic_read_error, 0,
1108220454Smav		"Force a read error every N reads (don't set too low).");
1109220454Smav#endif
1110195534Sscottl	cam_periph_release(periph);
1111195534Sscottl}
1112195534Sscottl
1113223089Sgibbsstatic int
1114223089Sgibbsadagetattr(struct bio *bp)
1115223089Sgibbs{
1116241485Smav	int ret;
1117223089Sgibbs	struct cam_periph *periph;
1118223089Sgibbs
1119223089Sgibbs	periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1120241485Smav	cam_periph_lock(periph);
1121223089Sgibbs	ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
1122223089Sgibbs	    periph->path);
1123241485Smav	cam_periph_unlock(periph);
1124223089Sgibbs	if (ret == 0)
1125223089Sgibbs		bp->bio_completed = bp->bio_length;
1126223089Sgibbs	return ret;
1127223089Sgibbs}
1128223089Sgibbs
1129195534Sscottlstatic cam_status
1130195534Sscottladaregister(struct cam_periph *periph, void *arg)
1131195534Sscottl{
1132195534Sscottl	struct ada_softc *softc;
1133195534Sscottl	struct ccb_pathinq cpi;
1134195534Sscottl	struct ccb_getdev *cgd;
1135221071Smav	char   announce_buf[80], buf1[32];
1136195534Sscottl	struct disk_params *dp;
1137195534Sscottl	caddr_t match;
1138195534Sscottl	u_int maxio;
1139222520Smav	int legacy_id, quirks;
1140195534Sscottl
1141195534Sscottl	cgd = (struct ccb_getdev *)arg;
1142195534Sscottl	if (cgd == NULL) {
1143195534Sscottl		printf("adaregister: no getdev CCB, can't register device\n");
1144195534Sscottl		return(CAM_REQ_CMP_ERR);
1145195534Sscottl	}
1146195534Sscottl
1147195534Sscottl	softc = (struct ada_softc *)malloc(sizeof(*softc), M_DEVBUF,
1148195534Sscottl	    M_NOWAIT|M_ZERO);
1149195534Sscottl
1150195534Sscottl	if (softc == NULL) {
1151195534Sscottl		printf("adaregister: Unable to probe new device. "
1152198328Smav		    "Unable to allocate softc\n");
1153195534Sscottl		return(CAM_REQ_CMP_ERR);
1154195534Sscottl	}
1155195534Sscottl
1156195534Sscottl	bioq_init(&softc->bio_queue);
1157201139Smav	bioq_init(&softc->trim_queue);
1158195534Sscottl
1159236393Smav	if ((cgd->ident_data.capabilities1 & ATA_SUPPORT_DMA) &&
1160220886Smav	    (cgd->inq_flags & SID_DMA))
1161198328Smav		softc->flags |= ADA_FLAG_CAN_DMA;
1162249199Smarius	if (cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) {
1163195534Sscottl		softc->flags |= ADA_FLAG_CAN_48BIT;
1164249199Smarius		if (cgd->inq_flags & SID_DMA48)
1165249199Smarius			softc->flags |= ADA_FLAG_CAN_DMA48;
1166249199Smarius	}
1167195534Sscottl	if (cgd->ident_data.support.command2 & ATA_SUPPORT_FLUSHCACHE)
1168195534Sscottl		softc->flags |= ADA_FLAG_CAN_FLUSHCACHE;
1169214279Sbrucec	if (cgd->ident_data.support.command1 & ATA_SUPPORT_POWERMGT)
1170214279Sbrucec		softc->flags |= ADA_FLAG_CAN_POWERMGT;
1171236393Smav	if ((cgd->ident_data.satacapabilities & ATA_SUPPORT_NCQ) &&
1172220886Smav	    (cgd->inq_flags & SID_DMA) && (cgd->inq_flags & SID_CmdQue))
1173195534Sscottl		softc->flags |= ADA_FLAG_CAN_NCQ;
1174236393Smav	if ((cgd->ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) &&
1175236393Smav	    (cgd->inq_flags & SID_DMA)) {
1176201139Smav		softc->flags |= ADA_FLAG_CAN_TRIM;
1177201139Smav		softc->trim_max_ranges = TRIM_MAX_RANGES;
1178201139Smav		if (cgd->ident_data.max_dsm_blocks != 0) {
1179201139Smav			softc->trim_max_ranges =
1180249934Ssmh			    min(cgd->ident_data.max_dsm_blocks *
1181249934Ssmh				ATA_DSM_BLK_RANGES, softc->trim_max_ranges);
1182201139Smav		}
1183201139Smav	}
1184201139Smav	if (cgd->ident_data.support.command2 & ATA_SUPPORT_CFA)
1185201139Smav		softc->flags |= ADA_FLAG_CAN_CFA;
1186195534Sscottl
1187195534Sscottl	periph->softc = softc;
1188195534Sscottl
1189195534Sscottl	/*
1190195534Sscottl	 * See if this device has any quirks.
1191195534Sscottl	 */
1192199178Smav	match = cam_quirkmatch((caddr_t)&cgd->ident_data,
1193199178Smav			       (caddr_t)ada_quirk_table,
1194199178Smav			       sizeof(ada_quirk_table)/sizeof(*ada_quirk_table),
1195199178Smav			       sizeof(*ada_quirk_table), ata_identify_match);
1196195534Sscottl	if (match != NULL)
1197195534Sscottl		softc->quirks = ((struct ada_quirk_entry *)match)->quirks;
1198195534Sscottl	else
1199195534Sscottl		softc->quirks = ADA_Q_NONE;
1200195534Sscottl
1201195534Sscottl	bzero(&cpi, sizeof(cpi));
1202203108Smav	xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NONE);
1203195534Sscottl	cpi.ccb_h.func_code = XPT_PATH_INQ;
1204195534Sscottl	xpt_action((union ccb *)&cpi);
1205195534Sscottl
1206195534Sscottl	TASK_INIT(&softc->sysctl_task, 0, adasysctlinit, periph);
1207195534Sscottl
1208195534Sscottl	/*
1209195534Sscottl	 * Register this media as a disk
1210195534Sscottl	 */
1211220618Smav	(void)cam_periph_hold(periph, PRIBIO);
1212249106Smav	cam_periph_unlock(periph);
1213222520Smav	snprintf(announce_buf, sizeof(announce_buf),
1214222520Smav	    "kern.cam.ada.%d.quirks", periph->unit_number);
1215222520Smav	quirks = softc->quirks;
1216222520Smav	TUNABLE_INT_FETCH(announce_buf, &quirks);
1217222520Smav	softc->quirks = quirks;
1218224497Smav	softc->read_ahead = -1;
1219224497Smav	snprintf(announce_buf, sizeof(announce_buf),
1220224497Smav	    "kern.cam.ada.%d.read_ahead", periph->unit_number);
1221224497Smav	TUNABLE_INT_FETCH(announce_buf, &softc->read_ahead);
1222220618Smav	softc->write_cache = -1;
1223220618Smav	snprintf(announce_buf, sizeof(announce_buf),
1224220618Smav	    "kern.cam.ada.%d.write_cache", periph->unit_number);
1225220618Smav	TUNABLE_INT_FETCH(announce_buf, &softc->write_cache);
1226250033Ssmh	/* Disable queue sorting for non-rotational media by default. */
1227249941Ssmh	if (cgd->ident_data.media_rotation_rate == 1)
1228249941Ssmh		softc->sort_io_queue = 0;
1229249941Ssmh	else
1230249941Ssmh		softc->sort_io_queue = -1;
1231198897Smav	adagetparams(periph, cgd);
1232195534Sscottl	softc->disk = disk_alloc();
1233220644Smav	softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
1234220644Smav			  periph->unit_number, softc->params.secsize,
1235220644Smav			  DEVSTAT_ALL_SUPPORTED,
1236220644Smav			  DEVSTAT_TYPE_DIRECT |
1237220644Smav			  XPORT_DEVSTAT_TYPE(cpi.transport),
1238220644Smav			  DEVSTAT_PRIORITY_DISK);
1239195534Sscottl	softc->disk->d_open = adaopen;
1240195534Sscottl	softc->disk->d_close = adaclose;
1241195534Sscottl	softc->disk->d_strategy = adastrategy;
1242223089Sgibbs	softc->disk->d_getattr = adagetattr;
1243195534Sscottl	softc->disk->d_dump = adadump;
1244249347Sken	softc->disk->d_gone = adadiskgonecb;
1245195534Sscottl	softc->disk->d_name = "ada";
1246195534Sscottl	softc->disk->d_drv1 = periph;
1247195534Sscottl	maxio = cpi.maxio;		/* Honor max I/O size of SIM */
1248195534Sscottl	if (maxio == 0)
1249195534Sscottl		maxio = DFLTPHYS;	/* traditional default */
1250195534Sscottl	else if (maxio > MAXPHYS)
1251195534Sscottl		maxio = MAXPHYS;	/* for safety */
1252201139Smav	if (softc->flags & ADA_FLAG_CAN_48BIT)
1253198897Smav		maxio = min(maxio, 65536 * softc->params.secsize);
1254195534Sscottl	else					/* 28bit ATA command limit */
1255198897Smav		maxio = min(maxio, 256 * softc->params.secsize);
1256195534Sscottl	softc->disk->d_maxsize = maxio;
1257195534Sscottl	softc->disk->d_unit = periph->unit_number;
1258260385Sscottl	softc->disk->d_flags = DISKFLAG_DIRECT_COMPLETION;
1259195534Sscottl	if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE)
1260195534Sscottl		softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
1261249934Ssmh	if (softc->flags & ADA_FLAG_CAN_TRIM) {
1262201139Smav		softc->disk->d_flags |= DISKFLAG_CANDELETE;
1263249940Ssmh		softc->disk->d_delmaxsize = softc->params.secsize *
1264249940Ssmh					    ATA_DSM_RANGE_MAX *
1265249940Ssmh					    softc->trim_max_ranges;
1266249934Ssmh	} else if ((softc->flags & ADA_FLAG_CAN_CFA) &&
1267249934Ssmh	    !(softc->flags & ADA_FLAG_CAN_48BIT)) {
1268249934Ssmh		softc->disk->d_flags |= DISKFLAG_CANDELETE;
1269249940Ssmh		softc->disk->d_delmaxsize = 256 * softc->params.secsize;
1270249940Ssmh	} else
1271249940Ssmh		softc->disk->d_delmaxsize = maxio;
1272248519Skib	if ((cpi.hba_misc & PIM_UNMAPPED) != 0)
1273248519Skib		softc->disk->d_flags |= DISKFLAG_UNMAPPED_BIO;
1274219056Snwhitehorn	strlcpy(softc->disk->d_descr, cgd->ident_data.model,
1275219056Snwhitehorn	    MIN(sizeof(softc->disk->d_descr), sizeof(cgd->ident_data.model)));
1276241305Savg	strlcpy(softc->disk->d_ident, cgd->ident_data.serial,
1277241305Savg	    MIN(sizeof(softc->disk->d_ident), sizeof(cgd->ident_data.serial)));
1278210471Smav	softc->disk->d_hba_vendor = cpi.hba_vendor;
1279210471Smav	softc->disk->d_hba_device = cpi.hba_device;
1280210471Smav	softc->disk->d_hba_subvendor = cpi.hba_subvendor;
1281210471Smav	softc->disk->d_hba_subdevice = cpi.hba_subdevice;
1282195534Sscottl
1283195534Sscottl	softc->disk->d_sectorsize = softc->params.secsize;
1284198897Smav	softc->disk->d_mediasize = (off_t)softc->params.sectors *
1285198897Smav	    softc->params.secsize;
1286200969Smav	if (ata_physical_sector_size(&cgd->ident_data) !=
1287200969Smav	    softc->params.secsize) {
1288200969Smav		softc->disk->d_stripesize =
1289200969Smav		    ata_physical_sector_size(&cgd->ident_data);
1290200969Smav		softc->disk->d_stripeoffset = (softc->disk->d_stripesize -
1291200969Smav		    ata_logical_sector_offset(&cgd->ident_data)) %
1292200969Smav		    softc->disk->d_stripesize;
1293222520Smav	} else if (softc->quirks & ADA_Q_4K) {
1294222520Smav		softc->disk->d_stripesize = 4096;
1295222520Smav		softc->disk->d_stripeoffset = 0;
1296200969Smav	}
1297195534Sscottl	softc->disk->d_fwsectors = softc->params.secs_per_track;
1298195534Sscottl	softc->disk->d_fwheads = softc->params.heads;
1299208349Smarius	ata_disk_firmware_geom_adjust(softc->disk);
1300195534Sscottl
1301221071Smav	if (ada_legacy_aliases) {
1302221071Smav#ifdef ATA_STATIC_ID
1303221071Smav		legacy_id = xpt_path_legacy_ata_id(periph->path);
1304221071Smav#else
1305221071Smav		legacy_id = softc->disk->d_unit;
1306221071Smav#endif
1307221071Smav		if (legacy_id >= 0) {
1308221071Smav			snprintf(announce_buf, sizeof(announce_buf),
1309221071Smav			    "kern.devalias.%s%d",
1310221071Smav			    softc->disk->d_name, softc->disk->d_unit);
1311221071Smav			snprintf(buf1, sizeof(buf1),
1312221071Smav			    "ad%d", legacy_id);
1313221071Smav			setenv(announce_buf, buf1);
1314221071Smav		}
1315221071Smav	} else
1316221071Smav		legacy_id = -1;
1317249347Sken	/*
1318249347Sken	 * Acquire a reference to the periph before we register with GEOM.
1319249347Sken	 * We'll release this reference once GEOM calls us back (via
1320249347Sken	 * adadiskgonecb()) telling us that our provider has been freed.
1321249347Sken	 */
1322249347Sken	if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
1323249347Sken		xpt_print(periph->path, "%s: lost periph during "
1324249347Sken			  "registration!\n", __func__);
1325249347Sken		cam_periph_lock(periph);
1326249347Sken		return (CAM_REQ_CMP_ERR);
1327249347Sken	}
1328195534Sscottl	disk_create(softc->disk, DISK_VERSION);
1329249106Smav	cam_periph_lock(periph);
1330220618Smav	cam_periph_unhold(periph);
1331195534Sscottl
1332195534Sscottl	dp = &softc->params;
1333195534Sscottl	snprintf(announce_buf, sizeof(announce_buf),
1334195534Sscottl		"%juMB (%ju %u byte sectors: %dH %dS/T %dC)",
1335195534Sscottl		(uintmax_t)(((uintmax_t)dp->secsize *
1336195534Sscottl		dp->sectors) / (1024*1024)),
1337195534Sscottl		(uintmax_t)dp->sectors,
1338195534Sscottl		dp->secsize, dp->heads,
1339195534Sscottl		dp->secs_per_track, dp->cylinders);
1340195534Sscottl	xpt_announce_periph(periph, announce_buf);
1341250792Ssmh	xpt_announce_quirks(periph, softc->quirks, ADA_Q_BIT_STRING);
1342221071Smav	if (legacy_id >= 0)
1343221071Smav		printf("%s%d: Previously was known as ad%d\n",
1344221071Smav		       periph->periph_name, periph->unit_number, legacy_id);
1345220454Smav
1346195534Sscottl	/*
1347220454Smav	 * Create our sysctl variables, now that we know
1348220454Smav	 * we have successfully attached.
1349220454Smav	 */
1350260387Sscottl	if (cam_periph_acquire(periph) == CAM_REQ_CMP)
1351260387Sscottl		taskqueue_enqueue(taskqueue_thread, &softc->sysctl_task);
1352220454Smav
1353220454Smav	/*
1354195534Sscottl	 * Add async callbacks for bus reset and
1355195534Sscottl	 * bus device reset calls.  I don't bother
1356195534Sscottl	 * checking if this fails as, in most cases,
1357195534Sscottl	 * the system will function just fine without
1358195534Sscottl	 * them and the only alternative would be to
1359195534Sscottl	 * not attach the device on failure.
1360195534Sscottl	 */
1361235897Smav	xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
1362236393Smav	    AC_GETDEV_CHANGED | AC_ADVINFO_CHANGED,
1363236393Smav	    adaasync, periph, periph->path);
1364195534Sscottl
1365195534Sscottl	/*
1366195534Sscottl	 * Schedule a periodic event to occasionally send an
1367195534Sscottl	 * ordered tag to a device.
1368195534Sscottl	 */
1369260387Sscottl	callout_init_mtx(&softc->sendordered_c, cam_periph_mtx(periph), 0);
1370195534Sscottl	callout_reset(&softc->sendordered_c,
1371230921Smav	    (ada_default_timeout * hz) / ADA_ORDEREDTAG_INTERVAL,
1372195534Sscottl	    adasendorderedtag, softc);
1373195534Sscottl
1374224497Smav	if (ADA_RA >= 0 &&
1375224497Smav	    cgd->ident_data.support.command1 & ATA_SUPPORT_LOOKAHEAD) {
1376224497Smav		softc->state = ADA_STATE_RAHEAD;
1377224497Smav	} else if (ADA_WC >= 0 &&
1378220412Smav	    cgd->ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) {
1379220412Smav		softc->state = ADA_STATE_WCACHE;
1380260387Sscottl	} else {
1381260387Sscottl		softc->state = ADA_STATE_NORMAL;
1382260387Sscottl		return(CAM_REQ_CMP);
1383260387Sscottl	}
1384260387Sscottl	if (cam_periph_acquire(periph) != CAM_REQ_CMP)
1385260387Sscottl		softc->state = ADA_STATE_NORMAL;
1386260387Sscottl	else
1387220412Smav		xpt_schedule(periph, CAM_PRIORITY_DEV);
1388195534Sscottl	return(CAM_REQ_CMP);
1389195534Sscottl}
1390195534Sscottl
1391195534Sscottlstatic void
1392268815Simpada_dsmtrim(struct ada_softc *softc, struct bio *bp, struct ccb_ataio *ataio)
1393268815Simp{
1394268815Simp	struct trim_request *req = &softc->trim_req;
1395268815Simp	uint64_t lastlba = (uint64_t)-1;
1396268815Simp	int c, lastcount = 0, off, ranges = 0;
1397268815Simp
1398268815Simp	bzero(req, sizeof(*req));
1399268815Simp	TAILQ_INIT(&req->bps);
1400268815Simp	do {
1401268815Simp		uint64_t lba = bp->bio_pblkno;
1402268815Simp		int count = bp->bio_bcount / softc->params.secsize;
1403268815Simp
1404268815Simp		bioq_remove(&softc->trim_queue, bp);
1405268815Simp
1406268815Simp		/* Try to extend the previous range. */
1407268815Simp		if (lba == lastlba) {
1408268815Simp			c = min(count, ATA_DSM_RANGE_MAX - lastcount);
1409268815Simp			lastcount += c;
1410268815Simp			off = (ranges - 1) * ATA_DSM_RANGE_SIZE;
1411268815Simp			req->data[off + 6] = lastcount & 0xff;
1412268815Simp			req->data[off + 7] =
1413268815Simp				(lastcount >> 8) & 0xff;
1414268815Simp			count -= c;
1415268815Simp			lba += c;
1416268815Simp		}
1417268815Simp
1418268815Simp		while (count > 0) {
1419268815Simp			c = min(count, ATA_DSM_RANGE_MAX);
1420268815Simp			off = ranges * ATA_DSM_RANGE_SIZE;
1421268815Simp			req->data[off + 0] = lba & 0xff;
1422268815Simp			req->data[off + 1] = (lba >> 8) & 0xff;
1423268815Simp			req->data[off + 2] = (lba >> 16) & 0xff;
1424268815Simp			req->data[off + 3] = (lba >> 24) & 0xff;
1425268815Simp			req->data[off + 4] = (lba >> 32) & 0xff;
1426268815Simp			req->data[off + 5] = (lba >> 40) & 0xff;
1427268815Simp			req->data[off + 6] = c & 0xff;
1428268815Simp			req->data[off + 7] = (c >> 8) & 0xff;
1429268815Simp			lba += c;
1430268815Simp			count -= c;
1431268815Simp			lastcount = c;
1432268815Simp			ranges++;
1433268815Simp			/*
1434268815Simp			 * Its the caller's responsibility to ensure the
1435268815Simp			 * request will fit so we don't need to check for
1436268815Simp			 * overrun here
1437268815Simp			 */
1438268815Simp		}
1439268815Simp		lastlba = lba;
1440268815Simp		TAILQ_INSERT_TAIL(&req->bps, bp, bio_queue);
1441268815Simp		bp = bioq_first(&softc->trim_queue);
1442268815Simp		if (bp == NULL ||
1443268815Simp		    bp->bio_bcount / softc->params.secsize >
1444268815Simp		    (softc->trim_max_ranges - ranges) * ATA_DSM_RANGE_MAX)
1445268815Simp			break;
1446268815Simp	} while (1);
1447268815Simp	cam_fill_ataio(ataio,
1448268815Simp	    ada_retry_count,
1449268815Simp	    adadone,
1450268815Simp	    CAM_DIR_OUT,
1451268815Simp	    0,
1452268815Simp	    req->data,
1453268815Simp	    ((ranges + ATA_DSM_BLK_RANGES - 1) /
1454268815Simp	    ATA_DSM_BLK_RANGES) * ATA_DSM_BLK_SIZE,
1455268815Simp	    ada_default_timeout * 1000);
1456268815Simp	ata_48bit_cmd(ataio, ATA_DATA_SET_MANAGEMENT,
1457268815Simp	    ATA_DSM_TRIM, 0, (ranges + ATA_DSM_BLK_RANGES -
1458268815Simp	    1) / ATA_DSM_BLK_RANGES);
1459268815Simp}
1460268815Simp
1461268815Simpstatic void
1462268815Simpada_cfaerase(struct ada_softc *softc, struct bio *bp, struct ccb_ataio *ataio)
1463268815Simp{
1464268815Simp	uint64_t lba = bp->bio_pblkno;
1465268815Simp	uint16_t count = bp->bio_bcount / softc->params.secsize;
1466268815Simp
1467268815Simp	cam_fill_ataio(ataio,
1468268815Simp	    ada_retry_count,
1469268815Simp	    adadone,
1470268815Simp	    CAM_DIR_NONE,
1471268815Simp	    0,
1472268815Simp	    NULL,
1473268815Simp	    0,
1474268815Simp	    ada_default_timeout*1000);
1475268815Simp
1476268815Simp	if (count >= 256)
1477268815Simp		count = 0;
1478268815Simp	ata_28bit_cmd(ataio, ATA_CFA_ERASE, 0, lba, count);
1479268815Simp}
1480268815Simp
1481268815Simpstatic void
1482195534Sscottladastart(struct cam_periph *periph, union ccb *start_ccb)
1483195534Sscottl{
1484198328Smav	struct ada_softc *softc = (struct ada_softc *)periph->softc;
1485198328Smav	struct ccb_ataio *ataio = &start_ccb->ataio;
1486195534Sscottl
1487236602Smav	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adastart\n"));
1488236602Smav
1489195534Sscottl	switch (softc->state) {
1490195534Sscottl	case ADA_STATE_NORMAL:
1491195534Sscottl	{
1492195534Sscottl		struct bio *bp;
1493201139Smav		u_int8_t tag_code;
1494195534Sscottl
1495201139Smav		/* Run TRIM if not running yet. */
1496201139Smav		if (!softc->trim_running &&
1497201139Smav		    (bp = bioq_first(&softc->trim_queue)) != 0) {
1498268815Simp			if (softc->flags & ADA_FLAG_CAN_TRIM) {
1499268815Simp				ada_dsmtrim(softc, bp, ataio);
1500268815Simp			} else if ((softc->flags & ADA_FLAG_CAN_CFA) &&
1501268815Simp			    !(softc->flags & ADA_FLAG_CAN_48BIT)) {
1502268815Simp				ada_cfaerase(softc, bp, ataio);
1503268815Simp			} else {
1504268815Simp				panic("adastart: BIO_DELETE without method, not possible.");
1505268815Simp			}
1506201139Smav			softc->trim_running = 1;
1507201139Smav			start_ccb->ccb_h.ccb_state = ADA_CCB_TRIM;
1508260387Sscottl			start_ccb->ccb_h.flags |= CAM_UNLOCKED;
1509201139Smav			goto out;
1510201139Smav		}
1511201139Smav		/* Run regular command. */
1512201139Smav		bp = bioq_first(&softc->bio_queue);
1513201139Smav		if (bp == NULL) {
1514195534Sscottl			xpt_release_ccb(start_ccb);
1515201139Smav			break;
1516201139Smav		}
1517201139Smav		bioq_remove(&softc->bio_queue, bp);
1518201139Smav
1519212160Sgibbs		if ((bp->bio_flags & BIO_ORDERED) != 0
1520212160Sgibbs		 || (softc->flags & ADA_FLAG_NEED_OTAG) != 0) {
1521201139Smav			softc->flags &= ~ADA_FLAG_NEED_OTAG;
1522260387Sscottl			softc->flags |= ADA_FLAG_WAS_OTAG;
1523201139Smav			tag_code = 0;
1524195534Sscottl		} else {
1525201139Smav			tag_code = 1;
1526201139Smav		}
1527201139Smav		switch (bp->bio_cmd) {
1528253724Smav		case BIO_WRITE:
1529253724Smav			softc->flags |= ADA_FLAG_DIRTY;
1530253724Smav			/* FALLTHROUGH */
1531201139Smav		case BIO_READ:
1532201139Smav		{
1533201139Smav			uint64_t lba = bp->bio_pblkno;
1534201139Smav			uint16_t count = bp->bio_bcount / softc->params.secsize;
1535220454Smav#ifdef ADA_TEST_FAILURE
1536220454Smav			int fail = 0;
1537195534Sscottl
1538220454Smav			/*
1539220454Smav			 * Support the failure ioctls.  If the command is a
1540220454Smav			 * read, and there are pending forced read errors, or
1541220454Smav			 * if a write and pending write errors, then fail this
1542220454Smav			 * operation with EIO.  This is useful for testing
1543220454Smav			 * purposes.  Also, support having every Nth read fail.
1544220454Smav			 *
1545220454Smav			 * This is a rather blunt tool.
1546220454Smav			 */
1547220454Smav			if (bp->bio_cmd == BIO_READ) {
1548220454Smav				if (softc->force_read_error) {
1549220454Smav					softc->force_read_error--;
1550220454Smav					fail = 1;
1551220454Smav				}
1552220454Smav				if (softc->periodic_read_error > 0) {
1553220454Smav					if (++softc->periodic_read_count >=
1554220454Smav					    softc->periodic_read_error) {
1555220454Smav						softc->periodic_read_count = 0;
1556220454Smav						fail = 1;
1557220454Smav					}
1558220454Smav				}
1559220454Smav			} else {
1560220454Smav				if (softc->force_write_error) {
1561220454Smav					softc->force_write_error--;
1562220454Smav					fail = 1;
1563220454Smav				}
1564220454Smav			}
1565220454Smav			if (fail) {
1566220454Smav				bp->bio_error = EIO;
1567220454Smav				bp->bio_flags |= BIO_ERROR;
1568220454Smav				biodone(bp);
1569220454Smav				xpt_release_ccb(start_ccb);
1570220454Smav				adaschedule(periph);
1571220454Smav				return;
1572220454Smav			}
1573220454Smav#endif
1574248519Skib			KASSERT((bp->bio_flags & BIO_UNMAPPED) == 0 ||
1575248519Skib			    round_page(bp->bio_bcount + bp->bio_ma_offset) /
1576248519Skib			    PAGE_SIZE == bp->bio_ma_n,
1577248519Skib			    ("Short bio %p", bp));
1578201139Smav			cam_fill_ataio(ataio,
1579201139Smav			    ada_retry_count,
1580201139Smav			    adadone,
1581248519Skib			    (bp->bio_cmd == BIO_READ ? CAM_DIR_IN :
1582248519Skib				CAM_DIR_OUT) | ((bp->bio_flags & BIO_UNMAPPED)
1583248519Skib				!= 0 ? CAM_DATA_BIO : 0),
1584201139Smav			    tag_code,
1585248519Skib			    ((bp->bio_flags & BIO_UNMAPPED) != 0) ? (void *)bp :
1586248519Skib				bp->bio_data,
1587201139Smav			    bp->bio_bcount,
1588201139Smav			    ada_default_timeout*1000);
1589195534Sscottl
1590201139Smav			if ((softc->flags & ADA_FLAG_CAN_NCQ) && tag_code) {
1591201139Smav				if (bp->bio_cmd == BIO_READ) {
1592201139Smav					ata_ncq_cmd(ataio, ATA_READ_FPDMA_QUEUED,
1593201139Smav					    lba, count);
1594201139Smav				} else {
1595201139Smav					ata_ncq_cmd(ataio, ATA_WRITE_FPDMA_QUEUED,
1596201139Smav					    lba, count);
1597201139Smav				}
1598201139Smav			} else if ((softc->flags & ADA_FLAG_CAN_48BIT) &&
1599201139Smav			    (lba + count >= ATA_MAX_28BIT_LBA ||
1600201139Smav			    count > 256)) {
1601249199Smarius				if (softc->flags & ADA_FLAG_CAN_DMA48) {
1602195534Sscottl					if (bp->bio_cmd == BIO_READ) {
1603201139Smav						ata_48bit_cmd(ataio, ATA_READ_DMA48,
1604201139Smav						    0, lba, count);
1605195534Sscottl					} else {
1606201139Smav						ata_48bit_cmd(ataio, ATA_WRITE_DMA48,
1607201139Smav						    0, lba, count);
1608195534Sscottl					}
1609201139Smav				} else {
1610201139Smav					if (bp->bio_cmd == BIO_READ) {
1611201139Smav						ata_48bit_cmd(ataio, ATA_READ_MUL48,
1612201139Smav						    0, lba, count);
1613195534Sscottl					} else {
1614201139Smav						ata_48bit_cmd(ataio, ATA_WRITE_MUL48,
1615201139Smav						    0, lba, count);
1616195534Sscottl					}
1617201139Smav				}
1618201139Smav			} else {
1619201139Smav				if (count == 256)
1620201139Smav					count = 0;
1621201139Smav				if (softc->flags & ADA_FLAG_CAN_DMA) {
1622201139Smav					if (bp->bio_cmd == BIO_READ) {
1623201139Smav						ata_28bit_cmd(ataio, ATA_READ_DMA,
1624201139Smav						    0, lba, count);
1625201139Smav					} else {
1626201139Smav						ata_28bit_cmd(ataio, ATA_WRITE_DMA,
1627201139Smav						    0, lba, count);
1628201139Smav					}
1629195534Sscottl				} else {
1630201139Smav					if (bp->bio_cmd == BIO_READ) {
1631201139Smav						ata_28bit_cmd(ataio, ATA_READ_MUL,
1632201139Smav						    0, lba, count);
1633195534Sscottl					} else {
1634201139Smav						ata_28bit_cmd(ataio, ATA_WRITE_MUL,
1635201139Smav						    0, lba, count);
1636195534Sscottl					}
1637195534Sscottl				}
1638195534Sscottl			}
1639201139Smav			break;
1640201139Smav		}
1641201139Smav		case BIO_FLUSH:
1642201139Smav			cam_fill_ataio(ataio,
1643201139Smav			    1,
1644201139Smav			    adadone,
1645201139Smav			    CAM_DIR_NONE,
1646201139Smav			    0,
1647201139Smav			    NULL,
1648201139Smav			    0,
1649201139Smav			    ada_default_timeout*1000);
1650201139Smav
1651201139Smav			if (softc->flags & ADA_FLAG_CAN_48BIT)
1652201139Smav				ata_48bit_cmd(ataio, ATA_FLUSHCACHE48, 0, 0, 0);
1653201139Smav			else
1654201139Smav				ata_28bit_cmd(ataio, ATA_FLUSHCACHE, 0, 0, 0);
1655201139Smav			break;
1656195534Sscottl		}
1657201139Smav		start_ccb->ccb_h.ccb_state = ADA_CCB_BUFFER_IO;
1658260387Sscottl		start_ccb->ccb_h.flags |= CAM_UNLOCKED;
1659201139Smavout:
1660201139Smav		start_ccb->ccb_h.ccb_bp = bp;
1661201139Smav		softc->outstanding_cmds++;
1662260387Sscottl		softc->refcount++;
1663260387Sscottl		cam_periph_unlock(periph);
1664201139Smav		xpt_action(start_ccb);
1665260387Sscottl		cam_periph_lock(periph);
1666260387Sscottl		softc->refcount--;
1667201139Smav
1668201139Smav		/* May have more work to do, so ensure we stay scheduled */
1669201139Smav		adaschedule(periph);
1670195534Sscottl		break;
1671195534Sscottl	}
1672224497Smav	case ADA_STATE_RAHEAD:
1673220412Smav	case ADA_STATE_WCACHE:
1674220412Smav	{
1675220412Smav		cam_fill_ataio(ataio,
1676220412Smav		    1,
1677220412Smav		    adadone,
1678220412Smav		    CAM_DIR_NONE,
1679220412Smav		    0,
1680220412Smav		    NULL,
1681220412Smav		    0,
1682220412Smav		    ada_default_timeout*1000);
1683220412Smav
1684224497Smav		if (softc->state == ADA_STATE_RAHEAD) {
1685224497Smav			ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_RA ?
1686224497Smav			    ATA_SF_ENAB_RCACHE : ATA_SF_DIS_RCACHE, 0, 0);
1687224497Smav			start_ccb->ccb_h.ccb_state = ADA_CCB_RAHEAD;
1688224497Smav		} else {
1689224497Smav			ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_WC ?
1690224497Smav			    ATA_SF_ENAB_WCACHE : ATA_SF_DIS_WCACHE, 0, 0);
1691224497Smav			start_ccb->ccb_h.ccb_state = ADA_CCB_WCACHE;
1692224497Smav		}
1693249466Smav		start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1694220412Smav		xpt_action(start_ccb);
1695220412Smav		break;
1696195534Sscottl	}
1697220412Smav	}
1698195534Sscottl}
1699195534Sscottl
1700195534Sscottlstatic void
1701195534Sscottladadone(struct cam_periph *periph, union ccb *done_ccb)
1702195534Sscottl{
1703195534Sscottl	struct ada_softc *softc;
1704195534Sscottl	struct ccb_ataio *ataio;
1705224497Smav	struct ccb_getdev *cgd;
1706249466Smav	struct cam_path *path;
1707253752Smav	int state;
1708195534Sscottl
1709195534Sscottl	softc = (struct ada_softc *)periph->softc;
1710195534Sscottl	ataio = &done_ccb->ataio;
1711249466Smav	path = done_ccb->ccb_h.path;
1712236602Smav
1713249466Smav	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("adadone\n"));
1714236602Smav
1715253752Smav	state = ataio->ccb_h.ccb_state & ADA_CCB_TYPE_MASK;
1716253752Smav	switch (state) {
1717195534Sscottl	case ADA_CCB_BUFFER_IO:
1718201139Smav	case ADA_CCB_TRIM:
1719195534Sscottl	{
1720195534Sscottl		struct bio *bp;
1721253752Smav		int error;
1722195534Sscottl
1723260387Sscottl		cam_periph_lock(periph);
1724195534Sscottl		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1725198328Smav			error = adaerror(done_ccb, 0, 0);
1726195534Sscottl			if (error == ERESTART) {
1727198328Smav				/* A retry was scheduled, so just return. */
1728260387Sscottl				cam_periph_unlock(periph);
1729195534Sscottl				return;
1730195534Sscottl			}
1731195534Sscottl			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1732249466Smav				cam_release_devq(path,
1733195534Sscottl						 /*relsim_flags*/0,
1734195534Sscottl						 /*reduction*/0,
1735195534Sscottl						 /*timeout*/0,
1736195534Sscottl						 /*getcount_only*/0);
1737195534Sscottl		} else {
1738195534Sscottl			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1739195534Sscottl				panic("REQ_CMP with QFRZN");
1740253752Smav			error = 0;
1741253752Smav		}
1742253752Smav		bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1743253752Smav		bp->bio_error = error;
1744253752Smav		if (error != 0) {
1745253752Smav			bp->bio_resid = bp->bio_bcount;
1746253752Smav			bp->bio_flags |= BIO_ERROR;
1747253752Smav		} else {
1748253752Smav			if (state == ADA_CCB_TRIM)
1749253752Smav				bp->bio_resid = 0;
1750253752Smav			else
1751253752Smav				bp->bio_resid = ataio->resid;
1752253752Smav			if (bp->bio_resid > 0)
1753195534Sscottl				bp->bio_flags |= BIO_ERROR;
1754195534Sscottl		}
1755195534Sscottl		softc->outstanding_cmds--;
1756195534Sscottl		if (softc->outstanding_cmds == 0)
1757260387Sscottl			softc->flags |= ADA_FLAG_WAS_OTAG;
1758260387Sscottl		xpt_release_ccb(done_ccb);
1759253752Smav		if (state == ADA_CCB_TRIM) {
1760260387Sscottl			TAILQ_HEAD(, bio) queue;
1761260387Sscottl			struct bio *bp1;
1762195534Sscottl
1763260387Sscottl			TAILQ_INIT(&queue);
1764260387Sscottl			TAILQ_CONCAT(&queue, &softc->trim_req.bps, bio_queue);
1765260387Sscottl			softc->trim_running = 0;
1766260387Sscottl			adaschedule(periph);
1767260387Sscottl			cam_periph_unlock(periph);
1768260387Sscottl			while ((bp1 = TAILQ_FIRST(&queue)) != NULL) {
1769260387Sscottl				TAILQ_REMOVE(&queue, bp1, bio_queue);
1770260387Sscottl				bp1->bio_error = error;
1771260387Sscottl				if (error != 0) {
1772201139Smav					bp1->bio_flags |= BIO_ERROR;
1773253752Smav					bp1->bio_resid = bp1->bio_bcount;
1774253752Smav				} else
1775253752Smav					bp1->bio_resid = 0;
1776201139Smav				biodone(bp1);
1777201139Smav			}
1778260387Sscottl		} else {
1779260387Sscottl			cam_periph_unlock(periph);
1780201139Smav			biodone(bp);
1781260387Sscottl		}
1782260387Sscottl		return;
1783195534Sscottl	}
1784224497Smav	case ADA_CCB_RAHEAD:
1785224497Smav	{
1786224497Smav		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1787224497Smav			if (adaerror(done_ccb, 0, 0) == ERESTART) {
1788249466Smavout:
1789249466Smav				/* Drop freeze taken due to CAM_DEV_QFREEZE */
1790249466Smav				cam_release_devq(path, 0, 0, 0, FALSE);
1791224497Smav				return;
1792224497Smav			} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1793249466Smav				cam_release_devq(path,
1794224497Smav				    /*relsim_flags*/0,
1795224497Smav				    /*reduction*/0,
1796224497Smav				    /*timeout*/0,
1797224497Smav				    /*getcount_only*/0);
1798224497Smav			}
1799224497Smav		}
1800224497Smav
1801224497Smav		/*
1802224497Smav		 * Since our peripheral may be invalidated by an error
1803224497Smav		 * above or an external event, we must release our CCB
1804224497Smav		 * before releasing the reference on the peripheral.
1805224497Smav		 * The peripheral will only go away once the last reference
1806224497Smav		 * is removed, and we need it around for the CCB release
1807224497Smav		 * operation.
1808224497Smav		 */
1809224497Smav		cgd = (struct ccb_getdev *)done_ccb;
1810249466Smav		xpt_setup_ccb(&cgd->ccb_h, path, CAM_PRIORITY_NORMAL);
1811224497Smav		cgd->ccb_h.func_code = XPT_GDEV_TYPE;
1812224497Smav		xpt_action((union ccb *)cgd);
1813224497Smav		if (ADA_WC >= 0 &&
1814224497Smav		    cgd->ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) {
1815224497Smav			softc->state = ADA_STATE_WCACHE;
1816224497Smav			xpt_release_ccb(done_ccb);
1817224497Smav			xpt_schedule(periph, CAM_PRIORITY_DEV);
1818249466Smav			goto out;
1819224497Smav		}
1820224497Smav		softc->state = ADA_STATE_NORMAL;
1821224497Smav		xpt_release_ccb(done_ccb);
1822249466Smav		/* Drop freeze taken due to CAM_DEV_QFREEZE */
1823249466Smav		cam_release_devq(path, 0, 0, 0, FALSE);
1824224497Smav		adaschedule(periph);
1825224497Smav		cam_periph_release_locked(periph);
1826224497Smav		return;
1827224497Smav	}
1828220412Smav	case ADA_CCB_WCACHE:
1829220412Smav	{
1830220412Smav		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1831220412Smav			if (adaerror(done_ccb, 0, 0) == ERESTART) {
1832249466Smav				goto out;
1833220412Smav			} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1834249466Smav				cam_release_devq(path,
1835220412Smav				    /*relsim_flags*/0,
1836220412Smav				    /*reduction*/0,
1837220412Smav				    /*timeout*/0,
1838220412Smav				    /*getcount_only*/0);
1839220412Smav			}
1840220412Smav		}
1841220412Smav
1842220412Smav		softc->state = ADA_STATE_NORMAL;
1843220412Smav		/*
1844220412Smav		 * Since our peripheral may be invalidated by an error
1845220412Smav		 * above or an external event, we must release our CCB
1846220412Smav		 * before releasing the reference on the peripheral.
1847220412Smav		 * The peripheral will only go away once the last reference
1848220412Smav		 * is removed, and we need it around for the CCB release
1849220412Smav		 * operation.
1850220412Smav		 */
1851220412Smav		xpt_release_ccb(done_ccb);
1852249466Smav		/* Drop freeze taken due to CAM_DEV_QFREEZE */
1853249466Smav		cam_release_devq(path, 0, 0, 0, FALSE);
1854220412Smav		adaschedule(periph);
1855220412Smav		cam_periph_release_locked(periph);
1856220412Smav		return;
1857220412Smav	}
1858195534Sscottl	case ADA_CCB_DUMP:
1859195534Sscottl		/* No-op.  We're polling */
1860195534Sscottl		return;
1861195534Sscottl	default:
1862195534Sscottl		break;
1863195534Sscottl	}
1864195534Sscottl	xpt_release_ccb(done_ccb);
1865195534Sscottl}
1866195534Sscottl
1867195534Sscottlstatic int
1868195534Sscottladaerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1869195534Sscottl{
1870195534Sscottl
1871203385Smav	return(cam_periph_error(ccb, cam_flags, sense_flags, NULL));
1872195534Sscottl}
1873195534Sscottl
1874195534Sscottlstatic void
1875198897Smavadagetparams(struct cam_periph *periph, struct ccb_getdev *cgd)
1876195534Sscottl{
1877195534Sscottl	struct ada_softc *softc = (struct ada_softc *)periph->softc;
1878195534Sscottl	struct disk_params *dp = &softc->params;
1879195534Sscottl	u_int64_t lbasize48;
1880195534Sscottl	u_int32_t lbasize;
1881195534Sscottl
1882198897Smav	dp->secsize = ata_logical_sector_size(&cgd->ident_data);
1883195534Sscottl	if ((cgd->ident_data.atavalid & ATA_FLAG_54_58) &&
1884195534Sscottl		cgd->ident_data.current_heads && cgd->ident_data.current_sectors) {
1885195534Sscottl		dp->heads = cgd->ident_data.current_heads;
1886195534Sscottl		dp->secs_per_track = cgd->ident_data.current_sectors;
1887195534Sscottl		dp->cylinders = cgd->ident_data.cylinders;
1888195534Sscottl		dp->sectors = (u_int32_t)cgd->ident_data.current_size_1 |
1889195534Sscottl			  ((u_int32_t)cgd->ident_data.current_size_2 << 16);
1890195534Sscottl	} else {
1891195534Sscottl		dp->heads = cgd->ident_data.heads;
1892195534Sscottl		dp->secs_per_track = cgd->ident_data.sectors;
1893195534Sscottl		dp->cylinders = cgd->ident_data.cylinders;
1894195534Sscottl		dp->sectors = cgd->ident_data.cylinders * dp->heads * dp->secs_per_track;
1895195534Sscottl	}
1896195534Sscottl	lbasize = (u_int32_t)cgd->ident_data.lba_size_1 |
1897195534Sscottl		  ((u_int32_t)cgd->ident_data.lba_size_2 << 16);
1898195534Sscottl
1899195534Sscottl	/* use the 28bit LBA size if valid or bigger than the CHS mapping */
1900195534Sscottl	if (cgd->ident_data.cylinders == 16383 || dp->sectors < lbasize)
1901195534Sscottl		dp->sectors = lbasize;
1902195534Sscottl
1903195534Sscottl	/* use the 48bit LBA size if valid */
1904195534Sscottl	lbasize48 = ((u_int64_t)cgd->ident_data.lba_size48_1) |
1905195534Sscottl		    ((u_int64_t)cgd->ident_data.lba_size48_2 << 16) |
1906195534Sscottl		    ((u_int64_t)cgd->ident_data.lba_size48_3 << 32) |
1907195534Sscottl		    ((u_int64_t)cgd->ident_data.lba_size48_4 << 48);
1908195534Sscottl	if ((cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) &&
1909195534Sscottl	    lbasize48 > ATA_MAX_28BIT_LBA)
1910195534Sscottl		dp->sectors = lbasize48;
1911195534Sscottl}
1912195534Sscottl
1913195534Sscottlstatic void
1914195534Sscottladasendorderedtag(void *arg)
1915195534Sscottl{
1916195534Sscottl	struct ada_softc *softc = arg;
1917195534Sscottl
1918195534Sscottl	if (ada_send_ordered) {
1919260387Sscottl		if (softc->outstanding_cmds > 0) {
1920260387Sscottl			if ((softc->flags & ADA_FLAG_WAS_OTAG) == 0)
1921260387Sscottl				softc->flags |= ADA_FLAG_NEED_OTAG;
1922260387Sscottl			softc->flags &= ~ADA_FLAG_WAS_OTAG;
1923195534Sscottl		}
1924195534Sscottl	}
1925195534Sscottl	/* Queue us up again */
1926195534Sscottl	callout_reset(&softc->sendordered_c,
1927230921Smav	    (ada_default_timeout * hz) / ADA_ORDEREDTAG_INTERVAL,
1928195534Sscottl	    adasendorderedtag, softc);
1929195534Sscottl}
1930195534Sscottl
1931195534Sscottl/*
1932195534Sscottl * Step through all ADA peripheral drivers, and if the device is still open,
1933195534Sscottl * sync the disk cache to physical media.
1934195534Sscottl */
1935195534Sscottlstatic void
1936220650Smavadaflush(void)
1937195534Sscottl{
1938195534Sscottl	struct cam_periph *periph;
1939195534Sscottl	struct ada_softc *softc;
1940248872Smav	union ccb *ccb;
1941236814Smav	int error;
1942195534Sscottl
1943248868Smav	CAM_PERIPH_FOREACH(periph, &adadriver) {
1944251792Smav		softc = (struct ada_softc *)periph->softc;
1945251792Smav		if (SCHEDULER_STOPPED()) {
1946251792Smav			/* If we paniced with the lock held, do not recurse. */
1947251792Smav			if (!cam_periph_owned(periph) &&
1948251792Smav			    (softc->flags & ADA_FLAG_OPEN)) {
1949251792Smav				adadump(softc->disk, NULL, 0, 0, 0);
1950251792Smav			}
1951200180Smav			continue;
1952251792Smav		}
1953195534Sscottl		cam_periph_lock(periph);
1954195534Sscottl		/*
1955195534Sscottl		 * We only sync the cache if the drive is still open, and
1956195534Sscottl		 * if the drive is capable of it..
1957195534Sscottl		 */
1958195534Sscottl		if (((softc->flags & ADA_FLAG_OPEN) == 0) ||
1959195534Sscottl		    (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) == 0) {
1960195534Sscottl			cam_periph_unlock(periph);
1961195534Sscottl			continue;
1962195534Sscottl		}
1963195534Sscottl
1964248872Smav		ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1965248872Smav		cam_fill_ataio(&ccb->ataio,
1966236814Smav				    0,
1967195534Sscottl				    adadone,
1968195534Sscottl				    CAM_DIR_NONE,
1969195534Sscottl				    0,
1970195534Sscottl				    NULL,
1971195534Sscottl				    0,
1972195534Sscottl				    ada_default_timeout*1000);
1973195534Sscottl		if (softc->flags & ADA_FLAG_CAN_48BIT)
1974248872Smav			ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0);
1975195534Sscottl		else
1976248872Smav			ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0);
1977195534Sscottl
1978248872Smav		error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0,
1979248872Smav		    /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY,
1980248872Smav		    softc->disk->d_devstat);
1981236814Smav		if (error != 0)
1982195534Sscottl			xpt_print(periph->path, "Synchronize cache failed\n");
1983249048Smav		xpt_release_ccb(ccb);
1984195534Sscottl		cam_periph_unlock(periph);
1985195534Sscottl	}
1986220650Smav}
1987214279Sbrucec
1988220650Smavstatic void
1989220650Smavadaspindown(uint8_t cmd, int flags)
1990220650Smav{
1991220650Smav	struct cam_periph *periph;
1992220650Smav	struct ada_softc *softc;
1993248872Smav	union ccb *ccb;
1994236814Smav	int error;
1995214279Sbrucec
1996248868Smav	CAM_PERIPH_FOREACH(periph, &adadriver) {
1997214279Sbrucec		/* If we paniced with lock held - not recurse here. */
1998214279Sbrucec		if (cam_periph_owned(periph))
1999214279Sbrucec			continue;
2000214279Sbrucec		cam_periph_lock(periph);
2001214279Sbrucec		softc = (struct ada_softc *)periph->softc;
2002214279Sbrucec		/*
2003214279Sbrucec		 * We only spin-down the drive if it is capable of it..
2004214279Sbrucec		 */
2005214279Sbrucec		if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) {
2006214279Sbrucec			cam_periph_unlock(periph);
2007214279Sbrucec			continue;
2008214279Sbrucec		}
2009214279Sbrucec
2010214279Sbrucec		if (bootverbose)
2011214279Sbrucec			xpt_print(periph->path, "spin-down\n");
2012214279Sbrucec
2013248872Smav		ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2014248872Smav		cam_fill_ataio(&ccb->ataio,
2015236814Smav				    0,
2016214279Sbrucec				    adadone,
2017220650Smav				    CAM_DIR_NONE | flags,
2018214279Sbrucec				    0,
2019214279Sbrucec				    NULL,
2020214279Sbrucec				    0,
2021214279Sbrucec				    ada_default_timeout*1000);
2022248872Smav		ata_28bit_cmd(&ccb->ataio, cmd, 0, 0, 0);
2023214279Sbrucec
2024248872Smav		error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0,
2025248872Smav		    /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY,
2026248872Smav		    softc->disk->d_devstat);
2027236814Smav		if (error != 0)
2028214279Sbrucec			xpt_print(periph->path, "Spin-down disk failed\n");
2029249048Smav		xpt_release_ccb(ccb);
2030214279Sbrucec		cam_periph_unlock(periph);
2031214279Sbrucec	}
2032195534Sscottl}
2033195534Sscottl
2034220650Smavstatic void
2035220650Smavadashutdown(void *arg, int howto)
2036220650Smav{
2037220650Smav
2038220650Smav	adaflush();
2039220650Smav	if (ada_spindown_shutdown != 0 &&
2040220650Smav	    (howto & (RB_HALT | RB_POWEROFF)) != 0)
2041220650Smav		adaspindown(ATA_STANDBY_IMMEDIATE, 0);
2042220650Smav}
2043220650Smav
2044220650Smavstatic void
2045220650Smavadasuspend(void *arg)
2046220650Smav{
2047220650Smav
2048220650Smav	adaflush();
2049220650Smav	if (ada_spindown_suspend != 0)
2050220650Smav		adaspindown(ATA_SLEEP, CAM_DEV_QFREEZE);
2051220650Smav}
2052220650Smav
2053220650Smavstatic void
2054220650Smavadaresume(void *arg)
2055220650Smav{
2056220650Smav	struct cam_periph *periph;
2057220650Smav	struct ada_softc *softc;
2058220650Smav
2059220650Smav	if (ada_spindown_suspend == 0)
2060220650Smav		return;
2061220650Smav
2062248868Smav	CAM_PERIPH_FOREACH(periph, &adadriver) {
2063220650Smav		cam_periph_lock(periph);
2064220650Smav		softc = (struct ada_softc *)periph->softc;
2065220650Smav		/*
2066220650Smav		 * We only spin-down the drive if it is capable of it..
2067220650Smav		 */
2068220650Smav		if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) {
2069220650Smav			cam_periph_unlock(periph);
2070220650Smav			continue;
2071220650Smav		}
2072220650Smav
2073220650Smav		if (bootverbose)
2074220650Smav			xpt_print(periph->path, "resume\n");
2075220650Smav
2076220650Smav		/*
2077220650Smav		 * Drop freeze taken due to CAM_DEV_QFREEZE flag set on
2078220650Smav		 * sleep request.
2079220650Smav		 */
2080220650Smav		cam_release_devq(periph->path,
2081220650Smav			 /*relsim_flags*/0,
2082220650Smav			 /*openings*/0,
2083220650Smav			 /*timeout*/0,
2084220650Smav			 /*getcount_only*/0);
2085220650Smav
2086220650Smav		cam_periph_unlock(periph);
2087220650Smav	}
2088220650Smav}
2089220650Smav
2090195534Sscottl#endif /* _KERNEL */
2091