ata_da.c revision 222628
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 222628 2011-06-02 20:56:42Z mav $");
29195534Sscottl
30220454Smav#include "opt_ada.h"
31221071Smav#include "opt_ata.h"
32220454Smav
33195534Sscottl#include <sys/param.h>
34195534Sscottl
35195534Sscottl#ifdef _KERNEL
36195534Sscottl#include <sys/systm.h>
37195534Sscottl#include <sys/kernel.h>
38195534Sscottl#include <sys/bio.h>
39195534Sscottl#include <sys/sysctl.h>
40195534Sscottl#include <sys/taskqueue.h>
41195534Sscottl#include <sys/lock.h>
42195534Sscottl#include <sys/mutex.h>
43195534Sscottl#include <sys/conf.h>
44195534Sscottl#include <sys/devicestat.h>
45195534Sscottl#include <sys/eventhandler.h>
46195534Sscottl#include <sys/malloc.h>
47195534Sscottl#include <sys/cons.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 {
72220412Smav	ADA_STATE_WCACHE,
73198708Smav	ADA_STATE_NORMAL
74195534Sscottl} ada_state;
75195534Sscottl
76195534Sscottltypedef enum {
77195534Sscottl	ADA_FLAG_PACK_INVALID	= 0x001,
78195534Sscottl	ADA_FLAG_CAN_48BIT	= 0x002,
79195534Sscottl	ADA_FLAG_CAN_FLUSHCACHE	= 0x004,
80198328Smav	ADA_FLAG_CAN_NCQ	= 0x008,
81198328Smav	ADA_FLAG_CAN_DMA	= 0x010,
82195534Sscottl	ADA_FLAG_NEED_OTAG	= 0x020,
83195534Sscottl	ADA_FLAG_WENT_IDLE	= 0x040,
84201139Smav	ADA_FLAG_CAN_TRIM	= 0x080,
85195534Sscottl	ADA_FLAG_OPEN		= 0x100,
86201139Smav	ADA_FLAG_SCTX_INIT	= 0x200,
87214279Sbrucec	ADA_FLAG_CAN_CFA        = 0x400,
88214279Sbrucec	ADA_FLAG_CAN_POWERMGT   = 0x800
89195534Sscottl} ada_flags;
90195534Sscottl
91195534Sscottltypedef enum {
92222520Smav	ADA_Q_NONE		= 0x00,
93222520Smav	ADA_Q_4K		= 0x01,
94195534Sscottl} ada_quirks;
95195534Sscottl
96195534Sscottltypedef enum {
97220412Smav	ADA_CCB_WCACHE		= 0x01,
98195534Sscottl	ADA_CCB_BUFFER_IO	= 0x03,
99195534Sscottl	ADA_CCB_WAITING		= 0x04,
100195534Sscottl	ADA_CCB_DUMP		= 0x05,
101201139Smav	ADA_CCB_TRIM		= 0x06,
102195534Sscottl	ADA_CCB_TYPE_MASK	= 0x0F,
103195534Sscottl} ada_ccb_state;
104195534Sscottl
105195534Sscottl/* Offsets into our private area for storing information */
106195534Sscottl#define ccb_state	ppriv_field0
107195534Sscottl#define ccb_bp		ppriv_ptr1
108195534Sscottl
109195534Sscottlstruct disk_params {
110195534Sscottl	u_int8_t  heads;
111198897Smav	u_int8_t  secs_per_track;
112195534Sscottl	u_int32_t cylinders;
113198897Smav	u_int32_t secsize;	/* Number of bytes/logical sector */
114198897Smav	u_int64_t sectors;	/* Total number sectors */
115195534Sscottl};
116195534Sscottl
117201139Smav#define TRIM_MAX_BLOCKS	4
118222628Smav#define TRIM_MAX_RANGES	(TRIM_MAX_BLOCKS * 64)
119222628Smav#define TRIM_MAX_BIOS	(TRIM_MAX_RANGES * 8)
120201139Smavstruct trim_request {
121201139Smav	uint8_t		data[TRIM_MAX_RANGES * 8];
122222628Smav	struct bio	*bps[TRIM_MAX_BIOS];
123201139Smav};
124201139Smav
125195534Sscottlstruct ada_softc {
126195534Sscottl	struct	 bio_queue_head bio_queue;
127201139Smav	struct	 bio_queue_head trim_queue;
128195534Sscottl	ada_state state;
129195534Sscottl	ada_flags flags;
130195534Sscottl	ada_quirks quirks;
131195534Sscottl	int	 ordered_tag_count;
132195534Sscottl	int	 outstanding_cmds;
133201139Smav	int	 trim_max_ranges;
134201139Smav	int	 trim_running;
135220454Smav	int	 write_cache;
136220454Smav#ifdef ADA_TEST_FAILURE
137220454Smav	int      force_read_error;
138220454Smav	int      force_write_error;
139220454Smav	int      periodic_read_error;
140220454Smav	int      periodic_read_count;
141220454Smav#endif
142195534Sscottl	struct	 disk_params params;
143195534Sscottl	struct	 disk *disk;
144195534Sscottl	struct task		sysctl_task;
145195534Sscottl	struct sysctl_ctx_list	sysctl_ctx;
146195534Sscottl	struct sysctl_oid	*sysctl_tree;
147195534Sscottl	struct callout		sendordered_c;
148201139Smav	struct trim_request	trim_req;
149195534Sscottl};
150195534Sscottl
151195534Sscottlstruct ada_quirk_entry {
152195534Sscottl	struct scsi_inquiry_pattern inq_pat;
153195534Sscottl	ada_quirks quirks;
154195534Sscottl};
155195534Sscottl
156199178Smavstatic struct ada_quirk_entry ada_quirk_table[] =
157199178Smav{
158199178Smav	{
159222520Smav		/* Hitachi Advanced Format (4k) drives */
160222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "Hitachi H??????????E3*", "*" },
161222520Smav		/*quirks*/ADA_Q_4K
162222520Smav	},
163222520Smav	{
164222520Smav		/* Samsung Advanced Format (4k) drives */
165222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD204UI*", "*" },
166222520Smav		/*quirks*/ADA_Q_4K
167222520Smav	},
168222520Smav	{
169222520Smav		/* Seagate Barracuda Green Advanced Format (4k) drives */
170222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST????DL*", "*" },
171222520Smav		/*quirks*/ADA_Q_4K
172222520Smav	},
173222520Smav	{
174222520Smav		/* Seagate Momentus Advanced Format (4k) drives */
175222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9500423AS*", "*" },
176222520Smav		/*quirks*/ADA_Q_4K
177222520Smav	},
178222520Smav	{
179222520Smav		/* Seagate Momentus Advanced Format (4k) drives */
180222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9500424AS*", "*" },
181222520Smav		/*quirks*/ADA_Q_4K
182222520Smav	},
183222520Smav	{
184222520Smav		/* Seagate Momentus Advanced Format (4k) drives */
185222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750420AS*", "*" },
186222520Smav		/*quirks*/ADA_Q_4K
187222520Smav	},
188222520Smav	{
189222520Smav		/* Seagate Momentus Advanced Format (4k) drives */
190222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750422AS*", "*" },
191222520Smav		/*quirks*/ADA_Q_4K
192222520Smav	},
193222520Smav	{
194222520Smav		/* Seagate Momentus Thin Advanced Format (4k) drives */
195222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "ST???LT*", "*" },
196222520Smav		/*quirks*/ADA_Q_4K
197222520Smav	},
198222520Smav	{
199222520Smav		/* WDC Caviar Green Advanced Format (4k) drives */
200222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RS*", "*" },
201222520Smav		/*quirks*/ADA_Q_4K
202222520Smav	},
203222520Smav	{
204222520Smav		/* WDC Caviar Green Advanced Format (4k) drives */
205222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RX*", "*" },
206222520Smav		/*quirks*/ADA_Q_4K
207222520Smav	},
208222520Smav	{
209222520Smav		/* WDC Caviar Green Advanced Format (4k) drives */
210222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RS*", "*" },
211222520Smav		/*quirks*/ADA_Q_4K
212222520Smav	},
213222520Smav	{
214222520Smav		/* WDC Caviar Green Advanced Format (4k) drives */
215222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RX*", "*" },
216222520Smav		/*quirks*/ADA_Q_4K
217222520Smav	},
218222520Smav	{
219222520Smav		/* WDC Scorpio Black Advanced Format (4k) drives */
220222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PKT*", "*" },
221222520Smav		/*quirks*/ADA_Q_4K
222222520Smav	},
223222520Smav	{
224222520Smav		/* WDC Scorpio Black Advanced Format (4k) drives */
225222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PKT*", "*" },
226222520Smav		/*quirks*/ADA_Q_4K
227222520Smav	},
228222520Smav	{
229222520Smav		/* WDC Scorpio Blue Advanced Format (4k) drives */
230222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PVT*", "*" },
231222520Smav		/*quirks*/ADA_Q_4K
232222520Smav	},
233222520Smav	{
234222520Smav		/* WDC Scorpio Blue Advanced Format (4k) drives */
235222520Smav		{ T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PVT*", "*" },
236222520Smav		/*quirks*/ADA_Q_4K
237222520Smav	},
238222520Smav	{
239199178Smav		/* Default */
240199178Smav		{
241199178Smav		  T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
242199178Smav		  /*vendor*/"*", /*product*/"*", /*revision*/"*"
243199178Smav		},
244199178Smav		/*quirks*/0
245199178Smav	},
246199178Smav};
247195534Sscottl
248195534Sscottlstatic	disk_strategy_t	adastrategy;
249195534Sscottlstatic	dumper_t	adadump;
250195534Sscottlstatic	periph_init_t	adainit;
251195534Sscottlstatic	void		adaasync(void *callback_arg, u_int32_t code,
252195534Sscottl				struct cam_path *path, void *arg);
253195534Sscottlstatic	void		adasysctlinit(void *context, int pending);
254195534Sscottlstatic	periph_ctor_t	adaregister;
255195534Sscottlstatic	periph_dtor_t	adacleanup;
256195534Sscottlstatic	periph_start_t	adastart;
257195534Sscottlstatic	periph_oninv_t	adaoninvalidate;
258195534Sscottlstatic	void		adadone(struct cam_periph *periph,
259195534Sscottl			       union ccb *done_ccb);
260195534Sscottlstatic  int		adaerror(union ccb *ccb, u_int32_t cam_flags,
261195534Sscottl				u_int32_t sense_flags);
262198897Smavstatic void		adagetparams(struct cam_periph *periph,
263195534Sscottl				struct ccb_getdev *cgd);
264195534Sscottlstatic timeout_t	adasendorderedtag;
265195534Sscottlstatic void		adashutdown(void *arg, int howto);
266220650Smavstatic void		adasuspend(void *arg);
267220650Smavstatic void		adaresume(void *arg);
268195534Sscottl
269221071Smav#ifndef	ADA_DEFAULT_LEGACY_ALIASES
270221071Smav#ifdef ATA_CAM
271221071Smav#define	ADA_DEFAULT_LEGACY_ALIASES	1
272221071Smav#else
273221071Smav#define	ADA_DEFAULT_LEGACY_ALIASES	0
274221071Smav#endif
275221071Smav#endif
276221071Smav
277195534Sscottl#ifndef ADA_DEFAULT_TIMEOUT
278195534Sscottl#define ADA_DEFAULT_TIMEOUT 30	/* Timeout in seconds */
279195534Sscottl#endif
280195534Sscottl
281195534Sscottl#ifndef	ADA_DEFAULT_RETRY
282195534Sscottl#define	ADA_DEFAULT_RETRY	4
283195534Sscottl#endif
284195534Sscottl
285195534Sscottl#ifndef	ADA_DEFAULT_SEND_ORDERED
286195534Sscottl#define	ADA_DEFAULT_SEND_ORDERED	1
287195534Sscottl#endif
288195534Sscottl
289214279Sbrucec#ifndef	ADA_DEFAULT_SPINDOWN_SHUTDOWN
290214279Sbrucec#define	ADA_DEFAULT_SPINDOWN_SHUTDOWN	1
291214279Sbrucec#endif
292214279Sbrucec
293220650Smav#ifndef	ADA_DEFAULT_SPINDOWN_SUSPEND
294220650Smav#define	ADA_DEFAULT_SPINDOWN_SUSPEND	1
295220650Smav#endif
296220650Smav
297220412Smav#ifndef	ADA_DEFAULT_WRITE_CACHE
298220412Smav#define	ADA_DEFAULT_WRITE_CACHE	1
299220412Smav#endif
300220412Smav
301208349Smarius/*
302208349Smarius * Most platforms map firmware geometry to actual, but some don't.  If
303208349Smarius * not overridden, default to nothing.
304208349Smarius */
305208349Smarius#ifndef ata_disk_firmware_geom_adjust
306208349Smarius#define	ata_disk_firmware_geom_adjust(disk)
307208349Smarius#endif
308195534Sscottl
309221071Smavstatic int ada_legacy_aliases = ADA_DEFAULT_LEGACY_ALIASES;
310195534Sscottlstatic int ada_retry_count = ADA_DEFAULT_RETRY;
311195534Sscottlstatic int ada_default_timeout = ADA_DEFAULT_TIMEOUT;
312195534Sscottlstatic int ada_send_ordered = ADA_DEFAULT_SEND_ORDERED;
313214279Sbrucecstatic int ada_spindown_shutdown = ADA_DEFAULT_SPINDOWN_SHUTDOWN;
314220650Smavstatic int ada_spindown_suspend = ADA_DEFAULT_SPINDOWN_SUSPEND;
315220412Smavstatic int ada_write_cache = ADA_DEFAULT_WRITE_CACHE;
316195534Sscottl
317195534SscottlSYSCTL_NODE(_kern_cam, OID_AUTO, ada, CTLFLAG_RD, 0,
318195534Sscottl            "CAM Direct Access Disk driver");
319221071SmavSYSCTL_INT(_kern_cam_ada, OID_AUTO, legacy_aliases, CTLFLAG_RW,
320221071Smav           &ada_legacy_aliases, 0, "Create legacy-like device aliases");
321221071SmavTUNABLE_INT("kern.cam.ada.legacy_aliases", &ada_legacy_aliases);
322195534SscottlSYSCTL_INT(_kern_cam_ada, OID_AUTO, retry_count, CTLFLAG_RW,
323195534Sscottl           &ada_retry_count, 0, "Normal I/O retry count");
324195534SscottlTUNABLE_INT("kern.cam.ada.retry_count", &ada_retry_count);
325195534SscottlSYSCTL_INT(_kern_cam_ada, OID_AUTO, default_timeout, CTLFLAG_RW,
326195534Sscottl           &ada_default_timeout, 0, "Normal I/O timeout (in seconds)");
327195534SscottlTUNABLE_INT("kern.cam.ada.default_timeout", &ada_default_timeout);
328195534SscottlSYSCTL_INT(_kern_cam_ada, OID_AUTO, ada_send_ordered, CTLFLAG_RW,
329195534Sscottl           &ada_send_ordered, 0, "Send Ordered Tags");
330195534SscottlTUNABLE_INT("kern.cam.ada.ada_send_ordered", &ada_send_ordered);
331214279SbrucecSYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_shutdown, CTLFLAG_RW,
332214279Sbrucec           &ada_spindown_shutdown, 0, "Spin down upon shutdown");
333214279SbrucecTUNABLE_INT("kern.cam.ada.spindown_shutdown", &ada_spindown_shutdown);
334220650SmavSYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_suspend, CTLFLAG_RW,
335220650Smav           &ada_spindown_suspend, 0, "Spin down upon suspend");
336220650SmavTUNABLE_INT("kern.cam.ada.spindown_suspend", &ada_spindown_suspend);
337220412SmavSYSCTL_INT(_kern_cam_ada, OID_AUTO, write_cache, CTLFLAG_RW,
338220412Smav           &ada_write_cache, 0, "Enable disk write cache");
339220412SmavTUNABLE_INT("kern.cam.ada.write_cache", &ada_write_cache);
340195534Sscottl
341195534Sscottl/*
342195534Sscottl * ADA_ORDEREDTAG_INTERVAL determines how often, relative
343195534Sscottl * to the default timeout, we check to see whether an ordered
344195534Sscottl * tagged transaction is appropriate to prevent simple tag
345195534Sscottl * starvation.  Since we'd like to ensure that there is at least
346195534Sscottl * 1/2 of the timeout length left for a starved transaction to
347195534Sscottl * complete after we've sent an ordered tag, we must poll at least
348195534Sscottl * four times in every timeout period.  This takes care of the worst
349195534Sscottl * case where a starved transaction starts during an interval that
350195534Sscottl * meets the requirement "don't send an ordered tag" test so it takes
351195534Sscottl * us two intervals to determine that a tag must be sent.
352195534Sscottl */
353195534Sscottl#ifndef ADA_ORDEREDTAG_INTERVAL
354195534Sscottl#define ADA_ORDEREDTAG_INTERVAL 4
355195534Sscottl#endif
356195534Sscottl
357195534Sscottlstatic struct periph_driver adadriver =
358195534Sscottl{
359195534Sscottl	adainit, "ada",
360195534Sscottl	TAILQ_HEAD_INITIALIZER(adadriver.units), /* generation */ 0
361195534Sscottl};
362195534Sscottl
363195534SscottlPERIPHDRIVER_DECLARE(ada, adadriver);
364195534Sscottl
365195534SscottlMALLOC_DEFINE(M_ATADA, "ata_da", "ata_da buffers");
366195534Sscottl
367195534Sscottlstatic int
368195534Sscottladaopen(struct disk *dp)
369195534Sscottl{
370195534Sscottl	struct cam_periph *periph;
371195534Sscottl	struct ada_softc *softc;
372195534Sscottl	int error;
373195534Sscottl
374195534Sscottl	periph = (struct cam_periph *)dp->d_drv1;
375195534Sscottl	if (periph == NULL) {
376195534Sscottl		return (ENXIO);
377195534Sscottl	}
378195534Sscottl
379195534Sscottl	if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
380195534Sscottl		return(ENXIO);
381195534Sscottl	}
382195534Sscottl
383195534Sscottl	cam_periph_lock(periph);
384195534Sscottl	if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
385195534Sscottl		cam_periph_unlock(periph);
386195534Sscottl		cam_periph_release(periph);
387195534Sscottl		return (error);
388195534Sscottl	}
389195534Sscottl
390195534Sscottl	softc = (struct ada_softc *)periph->softc;
391195534Sscottl	softc->flags |= ADA_FLAG_OPEN;
392195534Sscottl
393195534Sscottl	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
394195534Sscottl	    ("adaopen: disk=%s%d (unit %d)\n", dp->d_name, dp->d_unit,
395220778Smav	     periph->unit_number));
396195534Sscottl
397195534Sscottl	if ((softc->flags & ADA_FLAG_PACK_INVALID) != 0) {
398195534Sscottl		/* Invalidate our pack information. */
399195534Sscottl		softc->flags &= ~ADA_FLAG_PACK_INVALID;
400195534Sscottl	}
401195534Sscottl
402195534Sscottl	cam_periph_unhold(periph);
403195534Sscottl	cam_periph_unlock(periph);
404195534Sscottl	return (0);
405195534Sscottl}
406195534Sscottl
407195534Sscottlstatic int
408195534Sscottladaclose(struct disk *dp)
409195534Sscottl{
410195534Sscottl	struct	cam_periph *periph;
411195534Sscottl	struct	ada_softc *softc;
412195534Sscottl	union ccb *ccb;
413195534Sscottl	int error;
414195534Sscottl
415195534Sscottl	periph = (struct cam_periph *)dp->d_drv1;
416195534Sscottl	if (periph == NULL)
417195534Sscottl		return (ENXIO);
418195534Sscottl
419195534Sscottl	cam_periph_lock(periph);
420195534Sscottl	if ((error = cam_periph_hold(periph, PRIBIO)) != 0) {
421195534Sscottl		cam_periph_unlock(periph);
422195534Sscottl		cam_periph_release(periph);
423195534Sscottl		return (error);
424195534Sscottl	}
425195534Sscottl
426195534Sscottl	softc = (struct ada_softc *)periph->softc;
427195534Sscottl	/* We only sync the cache if the drive is capable of it. */
428195534Sscottl	if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) {
429195534Sscottl
430198382Smav		ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
431195534Sscottl		cam_fill_ataio(&ccb->ataio,
432195534Sscottl				    1,
433195534Sscottl				    adadone,
434195534Sscottl				    CAM_DIR_NONE,
435195534Sscottl				    0,
436195534Sscottl				    NULL,
437195534Sscottl				    0,
438195534Sscottl				    ada_default_timeout*1000);
439195534Sscottl
440195534Sscottl		if (softc->flags & ADA_FLAG_CAN_48BIT)
441195534Sscottl			ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0);
442195534Sscottl		else
443196659Smav			ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0);
444195748Smav		cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
445198328Smav		    /*sense_flags*/0, softc->disk->d_devstat);
446195534Sscottl
447195534Sscottl		if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
448195534Sscottl			xpt_print(periph->path, "Synchronize cache failed\n");
449195534Sscottl
450195534Sscottl		if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
451195534Sscottl			cam_release_devq(ccb->ccb_h.path,
452195534Sscottl					 /*relsim_flags*/0,
453195534Sscottl					 /*reduction*/0,
454195534Sscottl					 /*timeout*/0,
455195534Sscottl					 /*getcount_only*/0);
456195534Sscottl		xpt_release_ccb(ccb);
457195534Sscottl	}
458195534Sscottl
459195534Sscottl	softc->flags &= ~ADA_FLAG_OPEN;
460195534Sscottl	cam_periph_unhold(periph);
461195534Sscottl	cam_periph_unlock(periph);
462195534Sscottl	cam_periph_release(periph);
463195534Sscottl	return (0);
464195534Sscottl}
465195534Sscottl
466201139Smavstatic void
467201139Smavadaschedule(struct cam_periph *periph)
468201139Smav{
469201139Smav	struct ada_softc *softc = (struct ada_softc *)periph->softc;
470201139Smav
471201139Smav	if (bioq_first(&softc->bio_queue) ||
472201139Smav	    (!softc->trim_running && bioq_first(&softc->trim_queue))) {
473201139Smav		/* Have more work to do, so ensure we stay scheduled */
474201139Smav		xpt_schedule(periph, CAM_PRIORITY_NORMAL);
475201139Smav	}
476201139Smav}
477201139Smav
478195534Sscottl/*
479195534Sscottl * Actually translate the requested transfer into one the physical driver
480195534Sscottl * can understand.  The transfer is described by a buf and will include
481195534Sscottl * only one physical transfer.
482195534Sscottl */
483195534Sscottlstatic void
484195534Sscottladastrategy(struct bio *bp)
485195534Sscottl{
486195534Sscottl	struct cam_periph *periph;
487195534Sscottl	struct ada_softc *softc;
488195534Sscottl
489195534Sscottl	periph = (struct cam_periph *)bp->bio_disk->d_drv1;
490195534Sscottl	if (periph == NULL) {
491195534Sscottl		biofinish(bp, NULL, ENXIO);
492195534Sscottl		return;
493195534Sscottl	}
494195534Sscottl	softc = (struct ada_softc *)periph->softc;
495195534Sscottl
496195534Sscottl	cam_periph_lock(periph);
497195534Sscottl
498195534Sscottl	/*
499195534Sscottl	 * If the device has been made invalid, error out
500195534Sscottl	 */
501195534Sscottl	if ((softc->flags & ADA_FLAG_PACK_INVALID)) {
502195534Sscottl		cam_periph_unlock(periph);
503195534Sscottl		biofinish(bp, NULL, ENXIO);
504195534Sscottl		return;
505195534Sscottl	}
506195534Sscottl
507195534Sscottl	/*
508195534Sscottl	 * Place it in the queue of disk activities for this disk
509195534Sscottl	 */
510201139Smav	if (bp->bio_cmd == BIO_DELETE &&
511201139Smav	    (softc->flags & ADA_FLAG_CAN_TRIM))
512201139Smav		bioq_disksort(&softc->trim_queue, bp);
513201139Smav	else
514201139Smav		bioq_disksort(&softc->bio_queue, bp);
515195534Sscottl
516195534Sscottl	/*
517195534Sscottl	 * Schedule ourselves for performing the work.
518195534Sscottl	 */
519201139Smav	adaschedule(periph);
520195534Sscottl	cam_periph_unlock(periph);
521195534Sscottl
522195534Sscottl	return;
523195534Sscottl}
524195534Sscottl
525195534Sscottlstatic int
526195534Sscottladadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
527195534Sscottl{
528195534Sscottl	struct	    cam_periph *periph;
529195534Sscottl	struct	    ada_softc *softc;
530195534Sscottl	u_int	    secsize;
531195534Sscottl	union	    ccb ccb;
532195534Sscottl	struct	    disk *dp;
533195534Sscottl	uint64_t    lba;
534195534Sscottl	uint16_t    count;
535195534Sscottl
536195534Sscottl	dp = arg;
537195534Sscottl	periph = dp->d_drv1;
538195534Sscottl	if (periph == NULL)
539195534Sscottl		return (ENXIO);
540195534Sscottl	softc = (struct ada_softc *)periph->softc;
541195534Sscottl	cam_periph_lock(periph);
542195534Sscottl	secsize = softc->params.secsize;
543195534Sscottl	lba = offset / secsize;
544195534Sscottl	count = length / secsize;
545195534Sscottl
546195534Sscottl	if ((softc->flags & ADA_FLAG_PACK_INVALID) != 0) {
547195534Sscottl		cam_periph_unlock(periph);
548195534Sscottl		return (ENXIO);
549195534Sscottl	}
550195534Sscottl
551195534Sscottl	if (length > 0) {
552198382Smav		xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
553195534Sscottl		ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
554195534Sscottl		cam_fill_ataio(&ccb.ataio,
555195534Sscottl		    0,
556195534Sscottl		    adadone,
557195534Sscottl		    CAM_DIR_OUT,
558195534Sscottl		    0,
559195534Sscottl		    (u_int8_t *) virtual,
560195534Sscottl		    length,
561195534Sscottl		    ada_default_timeout*1000);
562195534Sscottl		if ((softc->flags & ADA_FLAG_CAN_48BIT) &&
563195534Sscottl		    (lba + count >= ATA_MAX_28BIT_LBA ||
564195534Sscottl		    count >= 256)) {
565195534Sscottl			ata_48bit_cmd(&ccb.ataio, ATA_WRITE_DMA48,
566195534Sscottl			    0, lba, count);
567195534Sscottl		} else {
568196659Smav			ata_28bit_cmd(&ccb.ataio, ATA_WRITE_DMA,
569195534Sscottl			    0, lba, count);
570195534Sscottl		}
571195534Sscottl		xpt_polled_action(&ccb);
572195534Sscottl
573195534Sscottl		if ((ccb.ataio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
574195534Sscottl			printf("Aborting dump due to I/O error.\n");
575195534Sscottl			cam_periph_unlock(periph);
576195534Sscottl			return(EIO);
577195534Sscottl		}
578195534Sscottl		cam_periph_unlock(periph);
579195534Sscottl		return(0);
580195534Sscottl	}
581195534Sscottl
582195534Sscottl	if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) {
583198382Smav		xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
584195534Sscottl
585195534Sscottl		ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
586195534Sscottl		cam_fill_ataio(&ccb.ataio,
587195534Sscottl				    1,
588195534Sscottl				    adadone,
589195534Sscottl				    CAM_DIR_NONE,
590195534Sscottl				    0,
591195534Sscottl				    NULL,
592195534Sscottl				    0,
593195534Sscottl				    ada_default_timeout*1000);
594195534Sscottl
595195534Sscottl		if (softc->flags & ADA_FLAG_CAN_48BIT)
596195534Sscottl			ata_48bit_cmd(&ccb.ataio, ATA_FLUSHCACHE48, 0, 0, 0);
597195534Sscottl		else
598196659Smav			ata_28bit_cmd(&ccb.ataio, ATA_FLUSHCACHE, 0, 0, 0);
599195534Sscottl		xpt_polled_action(&ccb);
600195534Sscottl
601195534Sscottl		if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
602195534Sscottl			xpt_print(periph->path, "Synchronize cache failed\n");
603195534Sscottl
604195534Sscottl		if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
605195534Sscottl			cam_release_devq(ccb.ccb_h.path,
606195534Sscottl					 /*relsim_flags*/0,
607195534Sscottl					 /*reduction*/0,
608195534Sscottl					 /*timeout*/0,
609195534Sscottl					 /*getcount_only*/0);
610195534Sscottl	}
611195534Sscottl	cam_periph_unlock(periph);
612195534Sscottl	return (0);
613195534Sscottl}
614195534Sscottl
615195534Sscottlstatic void
616195534Sscottladainit(void)
617195534Sscottl{
618195534Sscottl	cam_status status;
619195534Sscottl
620195534Sscottl	/*
621195534Sscottl	 * Install a global async callback.  This callback will
622195534Sscottl	 * receive async callbacks like "new device found".
623195534Sscottl	 */
624195534Sscottl	status = xpt_register_async(AC_FOUND_DEVICE, adaasync, NULL, NULL);
625195534Sscottl
626195534Sscottl	if (status != CAM_REQ_CMP) {
627195534Sscottl		printf("ada: Failed to attach master async callback "
628195534Sscottl		       "due to status 0x%x!\n", status);
629195534Sscottl	} else if (ada_send_ordered) {
630195534Sscottl
631220650Smav		/* Register our event handlers */
632220650Smav		if ((EVENTHANDLER_REGISTER(power_suspend, adasuspend,
633220650Smav					   NULL, EVENTHANDLER_PRI_LAST)) == NULL)
634220650Smav		    printf("adainit: power event registration failed!\n");
635220650Smav		if ((EVENTHANDLER_REGISTER(power_resume, adaresume,
636220650Smav					   NULL, EVENTHANDLER_PRI_LAST)) == NULL)
637220650Smav		    printf("adainit: power event registration failed!\n");
638220650Smav		if ((EVENTHANDLER_REGISTER(shutdown_post_sync, adashutdown,
639195534Sscottl					   NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
640195534Sscottl		    printf("adainit: shutdown event registration failed!\n");
641195534Sscottl	}
642195534Sscottl}
643195534Sscottl
644195534Sscottlstatic void
645195534Sscottladaoninvalidate(struct cam_periph *periph)
646195534Sscottl{
647195534Sscottl	struct ada_softc *softc;
648195534Sscottl
649195534Sscottl	softc = (struct ada_softc *)periph->softc;
650195534Sscottl
651195534Sscottl	/*
652195534Sscottl	 * De-register any async callbacks.
653195534Sscottl	 */
654195534Sscottl	xpt_register_async(0, adaasync, periph, periph->path);
655195534Sscottl
656195534Sscottl	softc->flags |= ADA_FLAG_PACK_INVALID;
657195534Sscottl
658195534Sscottl	/*
659195534Sscottl	 * Return all queued I/O with ENXIO.
660195534Sscottl	 * XXX Handle any transactions queued to the card
661195534Sscottl	 *     with XPT_ABORT_CCB.
662195534Sscottl	 */
663195534Sscottl	bioq_flush(&softc->bio_queue, NULL, ENXIO);
664201139Smav	bioq_flush(&softc->trim_queue, NULL, ENXIO);
665195534Sscottl
666195534Sscottl	disk_gone(softc->disk);
667195534Sscottl	xpt_print(periph->path, "lost device\n");
668195534Sscottl}
669195534Sscottl
670195534Sscottlstatic void
671195534Sscottladacleanup(struct cam_periph *periph)
672195534Sscottl{
673195534Sscottl	struct ada_softc *softc;
674195534Sscottl
675195534Sscottl	softc = (struct ada_softc *)periph->softc;
676195534Sscottl
677195534Sscottl	xpt_print(periph->path, "removing device entry\n");
678195534Sscottl	cam_periph_unlock(periph);
679195534Sscottl
680195534Sscottl	/*
681195534Sscottl	 * If we can't free the sysctl tree, oh well...
682195534Sscottl	 */
683195534Sscottl	if ((softc->flags & ADA_FLAG_SCTX_INIT) != 0
684195534Sscottl	    && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
685195534Sscottl		xpt_print(periph->path, "can't remove sysctl context\n");
686195534Sscottl	}
687195534Sscottl
688195534Sscottl	disk_destroy(softc->disk);
689195534Sscottl	callout_drain(&softc->sendordered_c);
690195534Sscottl	free(softc, M_DEVBUF);
691195534Sscottl	cam_periph_lock(periph);
692195534Sscottl}
693195534Sscottl
694195534Sscottlstatic void
695195534Sscottladaasync(void *callback_arg, u_int32_t code,
696195534Sscottl	struct cam_path *path, void *arg)
697195534Sscottl{
698195534Sscottl	struct cam_periph *periph;
699220412Smav	struct ada_softc *softc;
700195534Sscottl
701195534Sscottl	periph = (struct cam_periph *)callback_arg;
702195534Sscottl	switch (code) {
703195534Sscottl	case AC_FOUND_DEVICE:
704195534Sscottl	{
705195534Sscottl		struct ccb_getdev *cgd;
706195534Sscottl		cam_status status;
707195534Sscottl
708195534Sscottl		cgd = (struct ccb_getdev *)arg;
709195534Sscottl		if (cgd == NULL)
710195534Sscottl			break;
711195534Sscottl
712195534Sscottl		if (cgd->protocol != PROTO_ATA)
713195534Sscottl			break;
714195534Sscottl
715195534Sscottl		/*
716195534Sscottl		 * Allocate a peripheral instance for
717195534Sscottl		 * this device and start the probe
718195534Sscottl		 * process.
719195534Sscottl		 */
720195534Sscottl		status = cam_periph_alloc(adaregister, adaoninvalidate,
721195534Sscottl					  adacleanup, adastart,
722195534Sscottl					  "ada", CAM_PERIPH_BIO,
723195534Sscottl					  cgd->ccb_h.path, adaasync,
724195534Sscottl					  AC_FOUND_DEVICE, cgd);
725195534Sscottl
726195534Sscottl		if (status != CAM_REQ_CMP
727195534Sscottl		 && status != CAM_REQ_INPROG)
728195534Sscottl			printf("adaasync: Unable to attach to new device "
729195534Sscottl				"due to status 0x%x\n", status);
730195534Sscottl		break;
731195534Sscottl	}
732220412Smav	case AC_SENT_BDR:
733220412Smav	case AC_BUS_RESET:
734220412Smav	{
735220412Smav		struct ccb_getdev cgd;
736220412Smav
737220412Smav		softc = (struct ada_softc *)periph->softc;
738220412Smav		cam_periph_async(periph, code, path, arg);
739220454Smav		if (ada_write_cache < 0 && softc->write_cache < 0)
740220412Smav			break;
741220412Smav		if (softc->state != ADA_STATE_NORMAL)
742220412Smav			break;
743220454Smav		xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
744220412Smav		cgd.ccb_h.func_code = XPT_GDEV_TYPE;
745220412Smav		xpt_action((union ccb *)&cgd);
746220412Smav		if ((cgd.ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) == 0)
747220412Smav			break;
748220412Smav		softc->state = ADA_STATE_WCACHE;
749220412Smav		cam_periph_acquire(periph);
750220412Smav		cam_freeze_devq_arg(periph->path,
751220412Smav		    RELSIM_RELEASE_RUNLEVEL, CAM_RL_DEV + 1);
752220412Smav		xpt_schedule(periph, CAM_PRIORITY_DEV);
753220412Smav	}
754195534Sscottl	default:
755195534Sscottl		cam_periph_async(periph, code, path, arg);
756195534Sscottl		break;
757195534Sscottl	}
758195534Sscottl}
759195534Sscottl
760195534Sscottlstatic void
761195534Sscottladasysctlinit(void *context, int pending)
762195534Sscottl{
763195534Sscottl	struct cam_periph *periph;
764195534Sscottl	struct ada_softc *softc;
765195534Sscottl	char tmpstr[80], tmpstr2[80];
766195534Sscottl
767195534Sscottl	periph = (struct cam_periph *)context;
768220454Smav
769220454Smav	/* periph was held for us when this task was enqueued */
770220454Smav	if (periph->flags & CAM_PERIPH_INVALID) {
771220454Smav		cam_periph_release(periph);
772195534Sscottl		return;
773220454Smav	}
774195534Sscottl
775195534Sscottl	softc = (struct ada_softc *)periph->softc;
776195534Sscottl	snprintf(tmpstr, sizeof(tmpstr), "CAM ADA unit %d", periph->unit_number);
777195534Sscottl	snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
778195534Sscottl
779195534Sscottl	sysctl_ctx_init(&softc->sysctl_ctx);
780195534Sscottl	softc->flags |= ADA_FLAG_SCTX_INIT;
781195534Sscottl	softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
782195534Sscottl		SYSCTL_STATIC_CHILDREN(_kern_cam_ada), OID_AUTO, tmpstr2,
783195534Sscottl		CTLFLAG_RD, 0, tmpstr);
784195534Sscottl	if (softc->sysctl_tree == NULL) {
785195534Sscottl		printf("adasysctlinit: unable to allocate sysctl tree\n");
786195534Sscottl		cam_periph_release(periph);
787195534Sscottl		return;
788195534Sscottl	}
789195534Sscottl
790220454Smav	SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
791220454Smav		OID_AUTO, "write_cache", CTLFLAG_RW | CTLFLAG_MPSAFE,
792220454Smav		&softc->write_cache, 0, "Enable disk write cache.");
793220454Smav#ifdef ADA_TEST_FAILURE
794220454Smav	/*
795220454Smav	 * Add a 'door bell' sysctl which allows one to set it from userland
796220454Smav	 * and cause something bad to happen.  For the moment, we only allow
797220454Smav	 * whacking the next read or write.
798220454Smav	 */
799220454Smav	SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
800220454Smav		OID_AUTO, "force_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
801220454Smav		&softc->force_read_error, 0,
802220454Smav		"Force a read error for the next N reads.");
803220454Smav	SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
804220454Smav		OID_AUTO, "force_write_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
805220454Smav		&softc->force_write_error, 0,
806220454Smav		"Force a write error for the next N writes.");
807220454Smav	SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
808220454Smav		OID_AUTO, "periodic_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
809220454Smav		&softc->periodic_read_error, 0,
810220454Smav		"Force a read error every N reads (don't set too low).");
811220454Smav#endif
812195534Sscottl	cam_periph_release(periph);
813195534Sscottl}
814195534Sscottl
815195534Sscottlstatic cam_status
816195534Sscottladaregister(struct cam_periph *periph, void *arg)
817195534Sscottl{
818195534Sscottl	struct ada_softc *softc;
819195534Sscottl	struct ccb_pathinq cpi;
820195534Sscottl	struct ccb_getdev *cgd;
821221071Smav	char   announce_buf[80], buf1[32];
822195534Sscottl	struct disk_params *dp;
823195534Sscottl	caddr_t match;
824195534Sscottl	u_int maxio;
825222520Smav	int legacy_id, quirks;
826195534Sscottl
827195534Sscottl	cgd = (struct ccb_getdev *)arg;
828195534Sscottl	if (periph == NULL) {
829195534Sscottl		printf("adaregister: periph was NULL!!\n");
830195534Sscottl		return(CAM_REQ_CMP_ERR);
831195534Sscottl	}
832195534Sscottl
833195534Sscottl	if (cgd == NULL) {
834195534Sscottl		printf("adaregister: no getdev CCB, can't register device\n");
835195534Sscottl		return(CAM_REQ_CMP_ERR);
836195534Sscottl	}
837195534Sscottl
838195534Sscottl	softc = (struct ada_softc *)malloc(sizeof(*softc), M_DEVBUF,
839195534Sscottl	    M_NOWAIT|M_ZERO);
840195534Sscottl
841195534Sscottl	if (softc == NULL) {
842195534Sscottl		printf("adaregister: Unable to probe new device. "
843198328Smav		    "Unable to allocate softc\n");
844195534Sscottl		return(CAM_REQ_CMP_ERR);
845195534Sscottl	}
846195534Sscottl
847195534Sscottl	bioq_init(&softc->bio_queue);
848201139Smav	bioq_init(&softc->trim_queue);
849195534Sscottl
850220886Smav	if (cgd->ident_data.capabilities1 & ATA_SUPPORT_DMA &&
851220886Smav	    (cgd->inq_flags & SID_DMA))
852198328Smav		softc->flags |= ADA_FLAG_CAN_DMA;
853195534Sscottl	if (cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48)
854195534Sscottl		softc->flags |= ADA_FLAG_CAN_48BIT;
855195534Sscottl	if (cgd->ident_data.support.command2 & ATA_SUPPORT_FLUSHCACHE)
856195534Sscottl		softc->flags |= ADA_FLAG_CAN_FLUSHCACHE;
857214279Sbrucec	if (cgd->ident_data.support.command1 & ATA_SUPPORT_POWERMGT)
858214279Sbrucec		softc->flags |= ADA_FLAG_CAN_POWERMGT;
859195534Sscottl	if (cgd->ident_data.satacapabilities & ATA_SUPPORT_NCQ &&
860220886Smav	    (cgd->inq_flags & SID_DMA) && (cgd->inq_flags & SID_CmdQue))
861195534Sscottl		softc->flags |= ADA_FLAG_CAN_NCQ;
862201139Smav	if (cgd->ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) {
863201139Smav		softc->flags |= ADA_FLAG_CAN_TRIM;
864201139Smav		softc->trim_max_ranges = TRIM_MAX_RANGES;
865201139Smav		if (cgd->ident_data.max_dsm_blocks != 0) {
866201139Smav			softc->trim_max_ranges =
867201139Smav			    min(cgd->ident_data.max_dsm_blocks * 64,
868201139Smav				softc->trim_max_ranges);
869201139Smav		}
870201139Smav	}
871201139Smav	if (cgd->ident_data.support.command2 & ATA_SUPPORT_CFA)
872201139Smav		softc->flags |= ADA_FLAG_CAN_CFA;
873195534Sscottl
874195534Sscottl	periph->softc = softc;
875195534Sscottl
876195534Sscottl	/*
877195534Sscottl	 * See if this device has any quirks.
878195534Sscottl	 */
879199178Smav	match = cam_quirkmatch((caddr_t)&cgd->ident_data,
880199178Smav			       (caddr_t)ada_quirk_table,
881199178Smav			       sizeof(ada_quirk_table)/sizeof(*ada_quirk_table),
882199178Smav			       sizeof(*ada_quirk_table), ata_identify_match);
883195534Sscottl	if (match != NULL)
884195534Sscottl		softc->quirks = ((struct ada_quirk_entry *)match)->quirks;
885195534Sscottl	else
886195534Sscottl		softc->quirks = ADA_Q_NONE;
887195534Sscottl
888195534Sscottl	bzero(&cpi, sizeof(cpi));
889203108Smav	xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NONE);
890195534Sscottl	cpi.ccb_h.func_code = XPT_PATH_INQ;
891195534Sscottl	xpt_action((union ccb *)&cpi);
892195534Sscottl
893195534Sscottl	TASK_INIT(&softc->sysctl_task, 0, adasysctlinit, periph);
894195534Sscottl
895195534Sscottl	/*
896195534Sscottl	 * Register this media as a disk
897195534Sscottl	 */
898220618Smav	(void)cam_periph_hold(periph, PRIBIO);
899195534Sscottl	mtx_unlock(periph->sim->mtx);
900222520Smav	snprintf(announce_buf, sizeof(announce_buf),
901222520Smav	    "kern.cam.ada.%d.quirks", periph->unit_number);
902222520Smav	quirks = softc->quirks;
903222520Smav	TUNABLE_INT_FETCH(announce_buf, &quirks);
904222520Smav	softc->quirks = quirks;
905220618Smav	softc->write_cache = -1;
906220618Smav	snprintf(announce_buf, sizeof(announce_buf),
907220618Smav	    "kern.cam.ada.%d.write_cache", periph->unit_number);
908220618Smav	TUNABLE_INT_FETCH(announce_buf, &softc->write_cache);
909198897Smav	adagetparams(periph, cgd);
910195534Sscottl	softc->disk = disk_alloc();
911220644Smav	softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
912220644Smav			  periph->unit_number, softc->params.secsize,
913220644Smav			  DEVSTAT_ALL_SUPPORTED,
914220644Smav			  DEVSTAT_TYPE_DIRECT |
915220644Smav			  XPORT_DEVSTAT_TYPE(cpi.transport),
916220644Smav			  DEVSTAT_PRIORITY_DISK);
917195534Sscottl	softc->disk->d_open = adaopen;
918195534Sscottl	softc->disk->d_close = adaclose;
919195534Sscottl	softc->disk->d_strategy = adastrategy;
920195534Sscottl	softc->disk->d_dump = adadump;
921195534Sscottl	softc->disk->d_name = "ada";
922195534Sscottl	softc->disk->d_drv1 = periph;
923195534Sscottl	maxio = cpi.maxio;		/* Honor max I/O size of SIM */
924195534Sscottl	if (maxio == 0)
925195534Sscottl		maxio = DFLTPHYS;	/* traditional default */
926195534Sscottl	else if (maxio > MAXPHYS)
927195534Sscottl		maxio = MAXPHYS;	/* for safety */
928201139Smav	if (softc->flags & ADA_FLAG_CAN_48BIT)
929198897Smav		maxio = min(maxio, 65536 * softc->params.secsize);
930195534Sscottl	else					/* 28bit ATA command limit */
931198897Smav		maxio = min(maxio, 256 * softc->params.secsize);
932195534Sscottl	softc->disk->d_maxsize = maxio;
933195534Sscottl	softc->disk->d_unit = periph->unit_number;
934195534Sscottl	softc->disk->d_flags = 0;
935195534Sscottl	if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE)
936195534Sscottl		softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
937201139Smav	if ((softc->flags & ADA_FLAG_CAN_TRIM) ||
938201139Smav	    ((softc->flags & ADA_FLAG_CAN_CFA) &&
939201139Smav	    !(softc->flags & ADA_FLAG_CAN_48BIT)))
940201139Smav		softc->disk->d_flags |= DISKFLAG_CANDELETE;
941197896Spjd	strlcpy(softc->disk->d_ident, cgd->serial_num,
942197896Spjd	    MIN(sizeof(softc->disk->d_ident), cgd->serial_num_len + 1));
943219056Snwhitehorn	strlcpy(softc->disk->d_descr, cgd->ident_data.model,
944219056Snwhitehorn	    MIN(sizeof(softc->disk->d_descr), sizeof(cgd->ident_data.model)));
945210471Smav	softc->disk->d_hba_vendor = cpi.hba_vendor;
946210471Smav	softc->disk->d_hba_device = cpi.hba_device;
947210471Smav	softc->disk->d_hba_subvendor = cpi.hba_subvendor;
948210471Smav	softc->disk->d_hba_subdevice = cpi.hba_subdevice;
949195534Sscottl
950195534Sscottl	softc->disk->d_sectorsize = softc->params.secsize;
951198897Smav	softc->disk->d_mediasize = (off_t)softc->params.sectors *
952198897Smav	    softc->params.secsize;
953200969Smav	if (ata_physical_sector_size(&cgd->ident_data) !=
954200969Smav	    softc->params.secsize) {
955200969Smav		softc->disk->d_stripesize =
956200969Smav		    ata_physical_sector_size(&cgd->ident_data);
957200969Smav		softc->disk->d_stripeoffset = (softc->disk->d_stripesize -
958200969Smav		    ata_logical_sector_offset(&cgd->ident_data)) %
959200969Smav		    softc->disk->d_stripesize;
960222520Smav	} else if (softc->quirks & ADA_Q_4K) {
961222520Smav		softc->disk->d_stripesize = 4096;
962222520Smav		softc->disk->d_stripeoffset = 0;
963200969Smav	}
964195534Sscottl	softc->disk->d_fwsectors = softc->params.secs_per_track;
965195534Sscottl	softc->disk->d_fwheads = softc->params.heads;
966208349Smarius	ata_disk_firmware_geom_adjust(softc->disk);
967195534Sscottl
968221071Smav	if (ada_legacy_aliases) {
969221071Smav#ifdef ATA_STATIC_ID
970221071Smav		legacy_id = xpt_path_legacy_ata_id(periph->path);
971221071Smav#else
972221071Smav		legacy_id = softc->disk->d_unit;
973221071Smav#endif
974221071Smav		if (legacy_id >= 0) {
975221071Smav			snprintf(announce_buf, sizeof(announce_buf),
976221071Smav			    "kern.devalias.%s%d",
977221071Smav			    softc->disk->d_name, softc->disk->d_unit);
978221071Smav			snprintf(buf1, sizeof(buf1),
979221071Smav			    "ad%d", legacy_id);
980221071Smav			setenv(announce_buf, buf1);
981221071Smav		}
982221071Smav	} else
983221071Smav		legacy_id = -1;
984195534Sscottl	disk_create(softc->disk, DISK_VERSION);
985195534Sscottl	mtx_lock(periph->sim->mtx);
986220618Smav	cam_periph_unhold(periph);
987195534Sscottl
988195534Sscottl	dp = &softc->params;
989195534Sscottl	snprintf(announce_buf, sizeof(announce_buf),
990195534Sscottl		"%juMB (%ju %u byte sectors: %dH %dS/T %dC)",
991195534Sscottl		(uintmax_t)(((uintmax_t)dp->secsize *
992195534Sscottl		dp->sectors) / (1024*1024)),
993195534Sscottl		(uintmax_t)dp->sectors,
994195534Sscottl		dp->secsize, dp->heads,
995195534Sscottl		dp->secs_per_track, dp->cylinders);
996195534Sscottl	xpt_announce_periph(periph, announce_buf);
997221071Smav	if (legacy_id >= 0)
998221071Smav		printf("%s%d: Previously was known as ad%d\n",
999221071Smav		       periph->periph_name, periph->unit_number, legacy_id);
1000220454Smav
1001195534Sscottl	/*
1002220454Smav	 * Create our sysctl variables, now that we know
1003220454Smav	 * we have successfully attached.
1004220454Smav	 */
1005220454Smav	cam_periph_acquire(periph);
1006220454Smav	taskqueue_enqueue(taskqueue_thread, &softc->sysctl_task);
1007220454Smav
1008220454Smav	/*
1009195534Sscottl	 * Add async callbacks for bus reset and
1010195534Sscottl	 * bus device reset calls.  I don't bother
1011195534Sscottl	 * checking if this fails as, in most cases,
1012195534Sscottl	 * the system will function just fine without
1013195534Sscottl	 * them and the only alternative would be to
1014195534Sscottl	 * not attach the device on failure.
1015195534Sscottl	 */
1016220412Smav	xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE,
1017195534Sscottl			   adaasync, periph, periph->path);
1018195534Sscottl
1019195534Sscottl	/*
1020195534Sscottl	 * Schedule a periodic event to occasionally send an
1021195534Sscottl	 * ordered tag to a device.
1022195534Sscottl	 */
1023195534Sscottl	callout_init_mtx(&softc->sendordered_c, periph->sim->mtx, 0);
1024195534Sscottl	callout_reset(&softc->sendordered_c,
1025195534Sscottl	    (ADA_DEFAULT_TIMEOUT * hz) / ADA_ORDEREDTAG_INTERVAL,
1026195534Sscottl	    adasendorderedtag, softc);
1027195534Sscottl
1028220454Smav	if ((ada_write_cache >= 0 || softc->write_cache >= 0) &&
1029220412Smav	    cgd->ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) {
1030220412Smav		softc->state = ADA_STATE_WCACHE;
1031220412Smav		cam_periph_acquire(periph);
1032220412Smav		cam_freeze_devq_arg(periph->path,
1033220412Smav		    RELSIM_RELEASE_RUNLEVEL, CAM_RL_DEV + 1);
1034220412Smav		xpt_schedule(periph, CAM_PRIORITY_DEV);
1035220412Smav	} else
1036220412Smav		softc->state = ADA_STATE_NORMAL;
1037220412Smav
1038195534Sscottl	return(CAM_REQ_CMP);
1039195534Sscottl}
1040195534Sscottl
1041195534Sscottlstatic void
1042195534Sscottladastart(struct cam_periph *periph, union ccb *start_ccb)
1043195534Sscottl{
1044198328Smav	struct ada_softc *softc = (struct ada_softc *)periph->softc;
1045198328Smav	struct ccb_ataio *ataio = &start_ccb->ataio;
1046195534Sscottl
1047195534Sscottl	switch (softc->state) {
1048195534Sscottl	case ADA_STATE_NORMAL:
1049195534Sscottl	{
1050195534Sscottl		struct bio *bp;
1051201139Smav		u_int8_t tag_code;
1052195534Sscottl
1053201139Smav		/* Execute immediate CCB if waiting. */
1054195534Sscottl		if (periph->immediate_priority <= periph->pinfo.priority) {
1055195534Sscottl			CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1056195534Sscottl					("queuing for immediate ccb\n"));
1057195534Sscottl			start_ccb->ccb_h.ccb_state = ADA_CCB_WAITING;
1058195534Sscottl			SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1059195534Sscottl					  periph_links.sle);
1060195534Sscottl			periph->immediate_priority = CAM_PRIORITY_NONE;
1061195534Sscottl			wakeup(&periph->ccb_list);
1062201139Smav			/* Have more work to do, so ensure we stay scheduled */
1063201139Smav			adaschedule(periph);
1064201139Smav			break;
1065201139Smav		}
1066201139Smav		/* Run TRIM if not running yet. */
1067201139Smav		if (!softc->trim_running &&
1068201139Smav		    (bp = bioq_first(&softc->trim_queue)) != 0) {
1069201139Smav			struct trim_request *req = &softc->trim_req;
1070201139Smav			struct bio *bp1;
1071222628Smav			uint64_t lastlba = (uint64_t)-1;
1072222628Smav			int bps = 0, c, lastcount = 0, off, ranges = 0;
1073201139Smav
1074201139Smav			softc->trim_running = 1;
1075201139Smav			bzero(req, sizeof(*req));
1076201139Smav			bp1 = bp;
1077201139Smav			do {
1078201139Smav				uint64_t lba = bp1->bio_pblkno;
1079201139Smav				int count = bp1->bio_bcount /
1080201139Smav				    softc->params.secsize;
1081201139Smav
1082201139Smav				bioq_remove(&softc->trim_queue, bp1);
1083222628Smav
1084222628Smav				/* Try to extend the previous range. */
1085222628Smav				if (lba == lastlba) {
1086222628Smav					c = min(count, 0xffff - lastcount);
1087222628Smav					lastcount += c;
1088222628Smav					off = (ranges - 1) * 8;
1089222628Smav					req->data[off + 6] = lastcount & 0xff;
1090222628Smav					req->data[off + 7] =
1091222628Smav					    (lastcount >> 8) & 0xff;
1092222628Smav					count -= c;
1093222628Smav					lba += c;
1094222628Smav				}
1095222628Smav
1096201139Smav				while (count > 0) {
1097222628Smav					c = min(count, 0xffff);
1098222628Smav					off = ranges * 8;
1099201139Smav					req->data[off + 0] = lba & 0xff;
1100201139Smav					req->data[off + 1] = (lba >> 8) & 0xff;
1101201139Smav					req->data[off + 2] = (lba >> 16) & 0xff;
1102201139Smav					req->data[off + 3] = (lba >> 24) & 0xff;
1103201139Smav					req->data[off + 4] = (lba >> 32) & 0xff;
1104201139Smav					req->data[off + 5] = (lba >> 40) & 0xff;
1105201139Smav					req->data[off + 6] = c & 0xff;
1106201139Smav					req->data[off + 7] = (c >> 8) & 0xff;
1107201139Smav					lba += c;
1108201139Smav					count -= c;
1109222628Smav					lastcount = c;
1110201139Smav					ranges++;
1111201139Smav				}
1112222628Smav				lastlba = lba;
1113201139Smav				req->bps[bps++] = bp1;
1114201139Smav				bp1 = bioq_first(&softc->trim_queue);
1115222628Smav				if (bps >= TRIM_MAX_BIOS ||
1116222628Smav				    bp1 == NULL ||
1117201139Smav				    bp1->bio_bcount / softc->params.secsize >
1118201139Smav				    (softc->trim_max_ranges - ranges) * 0xffff)
1119201139Smav					break;
1120201139Smav			} while (1);
1121201139Smav			cam_fill_ataio(ataio,
1122201139Smav			    ada_retry_count,
1123201139Smav			    adadone,
1124201139Smav			    CAM_DIR_OUT,
1125201139Smav			    0,
1126201139Smav			    req->data,
1127201139Smav			    ((ranges + 63) / 64) * 512,
1128201139Smav			    ada_default_timeout * 1000);
1129201139Smav			ata_48bit_cmd(ataio, ATA_DATA_SET_MANAGEMENT,
1130201139Smav			    ATA_DSM_TRIM, 0, (ranges + 63) / 64);
1131201139Smav			start_ccb->ccb_h.ccb_state = ADA_CCB_TRIM;
1132201139Smav			goto out;
1133201139Smav		}
1134201139Smav		/* Run regular command. */
1135201139Smav		bp = bioq_first(&softc->bio_queue);
1136201139Smav		if (bp == NULL) {
1137195534Sscottl			xpt_release_ccb(start_ccb);
1138201139Smav			break;
1139201139Smav		}
1140201139Smav		bioq_remove(&softc->bio_queue, bp);
1141201139Smav
1142212160Sgibbs		if ((bp->bio_flags & BIO_ORDERED) != 0
1143212160Sgibbs		 || (softc->flags & ADA_FLAG_NEED_OTAG) != 0) {
1144201139Smav			softc->flags &= ~ADA_FLAG_NEED_OTAG;
1145201139Smav			softc->ordered_tag_count++;
1146201139Smav			tag_code = 0;
1147195534Sscottl		} else {
1148201139Smav			tag_code = 1;
1149201139Smav		}
1150201139Smav		switch (bp->bio_cmd) {
1151201139Smav		case BIO_READ:
1152201139Smav		case BIO_WRITE:
1153201139Smav		{
1154201139Smav			uint64_t lba = bp->bio_pblkno;
1155201139Smav			uint16_t count = bp->bio_bcount / softc->params.secsize;
1156220454Smav#ifdef ADA_TEST_FAILURE
1157220454Smav			int fail = 0;
1158195534Sscottl
1159220454Smav			/*
1160220454Smav			 * Support the failure ioctls.  If the command is a
1161220454Smav			 * read, and there are pending forced read errors, or
1162220454Smav			 * if a write and pending write errors, then fail this
1163220454Smav			 * operation with EIO.  This is useful for testing
1164220454Smav			 * purposes.  Also, support having every Nth read fail.
1165220454Smav			 *
1166220454Smav			 * This is a rather blunt tool.
1167220454Smav			 */
1168220454Smav			if (bp->bio_cmd == BIO_READ) {
1169220454Smav				if (softc->force_read_error) {
1170220454Smav					softc->force_read_error--;
1171220454Smav					fail = 1;
1172220454Smav				}
1173220454Smav				if (softc->periodic_read_error > 0) {
1174220454Smav					if (++softc->periodic_read_count >=
1175220454Smav					    softc->periodic_read_error) {
1176220454Smav						softc->periodic_read_count = 0;
1177220454Smav						fail = 1;
1178220454Smav					}
1179220454Smav				}
1180220454Smav			} else {
1181220454Smav				if (softc->force_write_error) {
1182220454Smav					softc->force_write_error--;
1183220454Smav					fail = 1;
1184220454Smav				}
1185220454Smav			}
1186220454Smav			if (fail) {
1187220454Smav				bp->bio_error = EIO;
1188220454Smav				bp->bio_flags |= BIO_ERROR;
1189220454Smav				biodone(bp);
1190220454Smav				xpt_release_ccb(start_ccb);
1191220454Smav				adaschedule(periph);
1192220454Smav				return;
1193220454Smav			}
1194220454Smav#endif
1195201139Smav			cam_fill_ataio(ataio,
1196201139Smav			    ada_retry_count,
1197201139Smav			    adadone,
1198201139Smav			    bp->bio_cmd == BIO_READ ?
1199201139Smav			        CAM_DIR_IN : CAM_DIR_OUT,
1200201139Smav			    tag_code,
1201201139Smav			    bp->bio_data,
1202201139Smav			    bp->bio_bcount,
1203201139Smav			    ada_default_timeout*1000);
1204195534Sscottl
1205201139Smav			if ((softc->flags & ADA_FLAG_CAN_NCQ) && tag_code) {
1206201139Smav				if (bp->bio_cmd == BIO_READ) {
1207201139Smav					ata_ncq_cmd(ataio, ATA_READ_FPDMA_QUEUED,
1208201139Smav					    lba, count);
1209201139Smav				} else {
1210201139Smav					ata_ncq_cmd(ataio, ATA_WRITE_FPDMA_QUEUED,
1211201139Smav					    lba, count);
1212201139Smav				}
1213201139Smav			} else if ((softc->flags & ADA_FLAG_CAN_48BIT) &&
1214201139Smav			    (lba + count >= ATA_MAX_28BIT_LBA ||
1215201139Smav			    count > 256)) {
1216201139Smav				if (softc->flags & ADA_FLAG_CAN_DMA) {
1217195534Sscottl					if (bp->bio_cmd == BIO_READ) {
1218201139Smav						ata_48bit_cmd(ataio, ATA_READ_DMA48,
1219201139Smav						    0, lba, count);
1220195534Sscottl					} else {
1221201139Smav						ata_48bit_cmd(ataio, ATA_WRITE_DMA48,
1222201139Smav						    0, lba, count);
1223195534Sscottl					}
1224201139Smav				} else {
1225201139Smav					if (bp->bio_cmd == BIO_READ) {
1226201139Smav						ata_48bit_cmd(ataio, ATA_READ_MUL48,
1227201139Smav						    0, lba, count);
1228195534Sscottl					} else {
1229201139Smav						ata_48bit_cmd(ataio, ATA_WRITE_MUL48,
1230201139Smav						    0, lba, count);
1231195534Sscottl					}
1232201139Smav				}
1233201139Smav			} else {
1234201139Smav				if (count == 256)
1235201139Smav					count = 0;
1236201139Smav				if (softc->flags & ADA_FLAG_CAN_DMA) {
1237201139Smav					if (bp->bio_cmd == BIO_READ) {
1238201139Smav						ata_28bit_cmd(ataio, ATA_READ_DMA,
1239201139Smav						    0, lba, count);
1240201139Smav					} else {
1241201139Smav						ata_28bit_cmd(ataio, ATA_WRITE_DMA,
1242201139Smav						    0, lba, count);
1243201139Smav					}
1244195534Sscottl				} else {
1245201139Smav					if (bp->bio_cmd == BIO_READ) {
1246201139Smav						ata_28bit_cmd(ataio, ATA_READ_MUL,
1247201139Smav						    0, lba, count);
1248195534Sscottl					} else {
1249201139Smav						ata_28bit_cmd(ataio, ATA_WRITE_MUL,
1250201139Smav						    0, lba, count);
1251195534Sscottl					}
1252195534Sscottl				}
1253195534Sscottl			}
1254201139Smav			break;
1255201139Smav		}
1256201139Smav		case BIO_DELETE:
1257201139Smav		{
1258201139Smav			uint64_t lba = bp->bio_pblkno;
1259201139Smav			uint16_t count = bp->bio_bcount / softc->params.secsize;
1260195534Sscottl
1261201139Smav			cam_fill_ataio(ataio,
1262201139Smav			    ada_retry_count,
1263201139Smav			    adadone,
1264201139Smav			    CAM_DIR_NONE,
1265201139Smav			    0,
1266201139Smav			    NULL,
1267201139Smav			    0,
1268201139Smav			    ada_default_timeout*1000);
1269201139Smav
1270201139Smav			if (count >= 256)
1271201139Smav				count = 0;
1272201139Smav			ata_28bit_cmd(ataio, ATA_CFA_ERASE, 0, lba, count);
1273201139Smav			break;
1274195534Sscottl		}
1275201139Smav		case BIO_FLUSH:
1276201139Smav			cam_fill_ataio(ataio,
1277201139Smav			    1,
1278201139Smav			    adadone,
1279201139Smav			    CAM_DIR_NONE,
1280201139Smav			    0,
1281201139Smav			    NULL,
1282201139Smav			    0,
1283201139Smav			    ada_default_timeout*1000);
1284201139Smav
1285201139Smav			if (softc->flags & ADA_FLAG_CAN_48BIT)
1286201139Smav				ata_48bit_cmd(ataio, ATA_FLUSHCACHE48, 0, 0, 0);
1287201139Smav			else
1288201139Smav				ata_28bit_cmd(ataio, ATA_FLUSHCACHE, 0, 0, 0);
1289201139Smav			break;
1290195534Sscottl		}
1291201139Smav		start_ccb->ccb_h.ccb_state = ADA_CCB_BUFFER_IO;
1292201139Smavout:
1293201139Smav		start_ccb->ccb_h.ccb_bp = bp;
1294201139Smav		softc->outstanding_cmds++;
1295201139Smav		xpt_action(start_ccb);
1296201139Smav
1297201139Smav		/* May have more work to do, so ensure we stay scheduled */
1298201139Smav		adaschedule(periph);
1299195534Sscottl		break;
1300195534Sscottl	}
1301220412Smav	case ADA_STATE_WCACHE:
1302220412Smav	{
1303220412Smav		cam_fill_ataio(ataio,
1304220412Smav		    1,
1305220412Smav		    adadone,
1306220412Smav		    CAM_DIR_NONE,
1307220412Smav		    0,
1308220412Smav		    NULL,
1309220412Smav		    0,
1310220412Smav		    ada_default_timeout*1000);
1311220412Smav
1312220454Smav		ata_28bit_cmd(ataio, ATA_SETFEATURES, (softc->write_cache > 0 ||
1313220454Smav		     (softc->write_cache < 0 && ada_write_cache)) ?
1314220412Smav		    ATA_SF_ENAB_WCACHE : ATA_SF_DIS_WCACHE, 0, 0);
1315220412Smav		start_ccb->ccb_h.ccb_state = ADA_CCB_WCACHE;
1316220412Smav		xpt_action(start_ccb);
1317220412Smav		break;
1318195534Sscottl	}
1319220412Smav	}
1320195534Sscottl}
1321195534Sscottl
1322195534Sscottlstatic void
1323195534Sscottladadone(struct cam_periph *periph, union ccb *done_ccb)
1324195534Sscottl{
1325195534Sscottl	struct ada_softc *softc;
1326195534Sscottl	struct ccb_ataio *ataio;
1327195534Sscottl
1328195534Sscottl	softc = (struct ada_softc *)periph->softc;
1329195534Sscottl	ataio = &done_ccb->ataio;
1330195534Sscottl	switch (ataio->ccb_h.ccb_state & ADA_CCB_TYPE_MASK) {
1331195534Sscottl	case ADA_CCB_BUFFER_IO:
1332201139Smav	case ADA_CCB_TRIM:
1333195534Sscottl	{
1334195534Sscottl		struct bio *bp;
1335195534Sscottl
1336195534Sscottl		bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1337195534Sscottl		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1338195534Sscottl			int error;
1339195534Sscottl
1340198328Smav			error = adaerror(done_ccb, 0, 0);
1341195534Sscottl			if (error == ERESTART) {
1342198328Smav				/* A retry was scheduled, so just return. */
1343195534Sscottl				return;
1344195534Sscottl			}
1345195534Sscottl			if (error != 0) {
1346195534Sscottl				if (error == ENXIO) {
1347195534Sscottl					/*
1348195534Sscottl					 * Catastrophic error.  Mark our pack as
1349195534Sscottl					 * invalid.
1350195534Sscottl					 */
1351195534Sscottl					/*
1352195534Sscottl					 * XXX See if this is really a media
1353195534Sscottl					 * XXX change first?
1354195534Sscottl					 */
1355195534Sscottl					xpt_print(periph->path,
1356195534Sscottl					    "Invalidating pack\n");
1357195534Sscottl					softc->flags |= ADA_FLAG_PACK_INVALID;
1358195534Sscottl				}
1359195534Sscottl				bp->bio_error = error;
1360195534Sscottl				bp->bio_resid = bp->bio_bcount;
1361195534Sscottl				bp->bio_flags |= BIO_ERROR;
1362195534Sscottl			} else {
1363195534Sscottl				bp->bio_resid = ataio->resid;
1364195534Sscottl				bp->bio_error = 0;
1365195534Sscottl				if (bp->bio_resid != 0)
1366195534Sscottl					bp->bio_flags |= BIO_ERROR;
1367195534Sscottl			}
1368195534Sscottl			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1369195534Sscottl				cam_release_devq(done_ccb->ccb_h.path,
1370195534Sscottl						 /*relsim_flags*/0,
1371195534Sscottl						 /*reduction*/0,
1372195534Sscottl						 /*timeout*/0,
1373195534Sscottl						 /*getcount_only*/0);
1374195534Sscottl		} else {
1375195534Sscottl			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1376195534Sscottl				panic("REQ_CMP with QFRZN");
1377195534Sscottl			bp->bio_resid = ataio->resid;
1378195534Sscottl			if (ataio->resid > 0)
1379195534Sscottl				bp->bio_flags |= BIO_ERROR;
1380195534Sscottl		}
1381195534Sscottl		softc->outstanding_cmds--;
1382195534Sscottl		if (softc->outstanding_cmds == 0)
1383195534Sscottl			softc->flags |= ADA_FLAG_WENT_IDLE;
1384201139Smav		if ((ataio->ccb_h.ccb_state & ADA_CCB_TYPE_MASK) ==
1385201139Smav		    ADA_CCB_TRIM) {
1386201139Smav			struct trim_request *req =
1387201139Smav			    (struct trim_request *)ataio->data_ptr;
1388201139Smav			int i;
1389195534Sscottl
1390222628Smav			for (i = 1; i < TRIM_MAX_BIOS && req->bps[i]; i++) {
1391201139Smav				struct bio *bp1 = req->bps[i];
1392201139Smav
1393201139Smav				bp1->bio_resid = bp->bio_resid;
1394201139Smav				bp1->bio_error = bp->bio_error;
1395201139Smav				if (bp->bio_flags & BIO_ERROR)
1396201139Smav					bp1->bio_flags |= BIO_ERROR;
1397201139Smav				biodone(bp1);
1398201139Smav			}
1399201139Smav			softc->trim_running = 0;
1400201139Smav			biodone(bp);
1401201139Smav			adaschedule(periph);
1402201139Smav		} else
1403201139Smav			biodone(bp);
1404195534Sscottl		break;
1405195534Sscottl	}
1406220412Smav	case ADA_CCB_WCACHE:
1407220412Smav	{
1408220412Smav		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1409220412Smav			if (adaerror(done_ccb, 0, 0) == ERESTART) {
1410220412Smav				return;
1411220412Smav			} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1412220412Smav				cam_release_devq(done_ccb->ccb_h.path,
1413220412Smav				    /*relsim_flags*/0,
1414220412Smav				    /*reduction*/0,
1415220412Smav				    /*timeout*/0,
1416220412Smav				    /*getcount_only*/0);
1417220412Smav			}
1418220412Smav		}
1419220412Smav
1420220412Smav		softc->state = ADA_STATE_NORMAL;
1421220412Smav		/*
1422220412Smav		 * Since our peripheral may be invalidated by an error
1423220412Smav		 * above or an external event, we must release our CCB
1424220412Smav		 * before releasing the reference on the peripheral.
1425220412Smav		 * The peripheral will only go away once the last reference
1426220412Smav		 * is removed, and we need it around for the CCB release
1427220412Smav		 * operation.
1428220412Smav		 */
1429220412Smav		xpt_release_ccb(done_ccb);
1430220412Smav		cam_release_devq(periph->path,
1431220412Smav		    RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_DEV + 1, FALSE);
1432220412Smav		adaschedule(periph);
1433220412Smav		cam_periph_release_locked(periph);
1434220412Smav		return;
1435220412Smav	}
1436195534Sscottl	case ADA_CCB_WAITING:
1437195534Sscottl	{
1438195534Sscottl		/* Caller will release the CCB */
1439195534Sscottl		wakeup(&done_ccb->ccb_h.cbfcnp);
1440195534Sscottl		return;
1441195534Sscottl	}
1442195534Sscottl	case ADA_CCB_DUMP:
1443195534Sscottl		/* No-op.  We're polling */
1444195534Sscottl		return;
1445195534Sscottl	default:
1446195534Sscottl		break;
1447195534Sscottl	}
1448195534Sscottl	xpt_release_ccb(done_ccb);
1449195534Sscottl}
1450195534Sscottl
1451195534Sscottlstatic int
1452195534Sscottladaerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1453195534Sscottl{
1454195534Sscottl
1455203385Smav	return(cam_periph_error(ccb, cam_flags, sense_flags, NULL));
1456195534Sscottl}
1457195534Sscottl
1458195534Sscottlstatic void
1459198897Smavadagetparams(struct cam_periph *periph, struct ccb_getdev *cgd)
1460195534Sscottl{
1461195534Sscottl	struct ada_softc *softc = (struct ada_softc *)periph->softc;
1462195534Sscottl	struct disk_params *dp = &softc->params;
1463195534Sscottl	u_int64_t lbasize48;
1464195534Sscottl	u_int32_t lbasize;
1465195534Sscottl
1466198897Smav	dp->secsize = ata_logical_sector_size(&cgd->ident_data);
1467195534Sscottl	if ((cgd->ident_data.atavalid & ATA_FLAG_54_58) &&
1468195534Sscottl		cgd->ident_data.current_heads && cgd->ident_data.current_sectors) {
1469195534Sscottl		dp->heads = cgd->ident_data.current_heads;
1470195534Sscottl		dp->secs_per_track = cgd->ident_data.current_sectors;
1471195534Sscottl		dp->cylinders = cgd->ident_data.cylinders;
1472195534Sscottl		dp->sectors = (u_int32_t)cgd->ident_data.current_size_1 |
1473195534Sscottl			  ((u_int32_t)cgd->ident_data.current_size_2 << 16);
1474195534Sscottl	} else {
1475195534Sscottl		dp->heads = cgd->ident_data.heads;
1476195534Sscottl		dp->secs_per_track = cgd->ident_data.sectors;
1477195534Sscottl		dp->cylinders = cgd->ident_data.cylinders;
1478195534Sscottl		dp->sectors = cgd->ident_data.cylinders * dp->heads * dp->secs_per_track;
1479195534Sscottl	}
1480195534Sscottl	lbasize = (u_int32_t)cgd->ident_data.lba_size_1 |
1481195534Sscottl		  ((u_int32_t)cgd->ident_data.lba_size_2 << 16);
1482195534Sscottl
1483195534Sscottl	/* use the 28bit LBA size if valid or bigger than the CHS mapping */
1484195534Sscottl	if (cgd->ident_data.cylinders == 16383 || dp->sectors < lbasize)
1485195534Sscottl		dp->sectors = lbasize;
1486195534Sscottl
1487195534Sscottl	/* use the 48bit LBA size if valid */
1488195534Sscottl	lbasize48 = ((u_int64_t)cgd->ident_data.lba_size48_1) |
1489195534Sscottl		    ((u_int64_t)cgd->ident_data.lba_size48_2 << 16) |
1490195534Sscottl		    ((u_int64_t)cgd->ident_data.lba_size48_3 << 32) |
1491195534Sscottl		    ((u_int64_t)cgd->ident_data.lba_size48_4 << 48);
1492195534Sscottl	if ((cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) &&
1493195534Sscottl	    lbasize48 > ATA_MAX_28BIT_LBA)
1494195534Sscottl		dp->sectors = lbasize48;
1495195534Sscottl}
1496195534Sscottl
1497195534Sscottlstatic void
1498195534Sscottladasendorderedtag(void *arg)
1499195534Sscottl{
1500195534Sscottl	struct ada_softc *softc = arg;
1501195534Sscottl
1502195534Sscottl	if (ada_send_ordered) {
1503195534Sscottl		if ((softc->ordered_tag_count == 0)
1504195534Sscottl		 && ((softc->flags & ADA_FLAG_WENT_IDLE) == 0)) {
1505195534Sscottl			softc->flags |= ADA_FLAG_NEED_OTAG;
1506195534Sscottl		}
1507195534Sscottl		if (softc->outstanding_cmds > 0)
1508195534Sscottl			softc->flags &= ~ADA_FLAG_WENT_IDLE;
1509195534Sscottl
1510195534Sscottl		softc->ordered_tag_count = 0;
1511195534Sscottl	}
1512195534Sscottl	/* Queue us up again */
1513195534Sscottl	callout_reset(&softc->sendordered_c,
1514195534Sscottl	    (ADA_DEFAULT_TIMEOUT * hz) / ADA_ORDEREDTAG_INTERVAL,
1515195534Sscottl	    adasendorderedtag, softc);
1516195534Sscottl}
1517195534Sscottl
1518195534Sscottl/*
1519195534Sscottl * Step through all ADA peripheral drivers, and if the device is still open,
1520195534Sscottl * sync the disk cache to physical media.
1521195534Sscottl */
1522195534Sscottlstatic void
1523220650Smavadaflush(void)
1524195534Sscottl{
1525195534Sscottl	struct cam_periph *periph;
1526195534Sscottl	struct ada_softc *softc;
1527195534Sscottl
1528195534Sscottl	TAILQ_FOREACH(periph, &adadriver.units, unit_links) {
1529195534Sscottl		union ccb ccb;
1530195534Sscottl
1531200180Smav		/* If we paniced with lock held - not recurse here. */
1532200180Smav		if (cam_periph_owned(periph))
1533200180Smav			continue;
1534195534Sscottl		cam_periph_lock(periph);
1535195534Sscottl		softc = (struct ada_softc *)periph->softc;
1536195534Sscottl		/*
1537195534Sscottl		 * We only sync the cache if the drive is still open, and
1538195534Sscottl		 * if the drive is capable of it..
1539195534Sscottl		 */
1540195534Sscottl		if (((softc->flags & ADA_FLAG_OPEN) == 0) ||
1541195534Sscottl		    (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) == 0) {
1542195534Sscottl			cam_periph_unlock(periph);
1543195534Sscottl			continue;
1544195534Sscottl		}
1545195534Sscottl
1546198382Smav		xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1547195534Sscottl
1548195534Sscottl		ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
1549195534Sscottl		cam_fill_ataio(&ccb.ataio,
1550195534Sscottl				    1,
1551195534Sscottl				    adadone,
1552195534Sscottl				    CAM_DIR_NONE,
1553195534Sscottl				    0,
1554195534Sscottl				    NULL,
1555195534Sscottl				    0,
1556195534Sscottl				    ada_default_timeout*1000);
1557195534Sscottl
1558195534Sscottl		if (softc->flags & ADA_FLAG_CAN_48BIT)
1559195534Sscottl			ata_48bit_cmd(&ccb.ataio, ATA_FLUSHCACHE48, 0, 0, 0);
1560195534Sscottl		else
1561196659Smav			ata_28bit_cmd(&ccb.ataio, ATA_FLUSHCACHE, 0, 0, 0);
1562195534Sscottl		xpt_polled_action(&ccb);
1563195534Sscottl
1564195534Sscottl		if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
1565195534Sscottl			xpt_print(periph->path, "Synchronize cache failed\n");
1566195534Sscottl
1567195534Sscottl		if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
1568195534Sscottl			cam_release_devq(ccb.ccb_h.path,
1569195534Sscottl					 /*relsim_flags*/0,
1570195534Sscottl					 /*reduction*/0,
1571195534Sscottl					 /*timeout*/0,
1572195534Sscottl					 /*getcount_only*/0);
1573195534Sscottl		cam_periph_unlock(periph);
1574195534Sscottl	}
1575220650Smav}
1576214279Sbrucec
1577220650Smavstatic void
1578220650Smavadaspindown(uint8_t cmd, int flags)
1579220650Smav{
1580220650Smav	struct cam_periph *periph;
1581220650Smav	struct ada_softc *softc;
1582214279Sbrucec
1583214279Sbrucec	TAILQ_FOREACH(periph, &adadriver.units, unit_links) {
1584214279Sbrucec		union ccb ccb;
1585214279Sbrucec
1586214279Sbrucec		/* If we paniced with lock held - not recurse here. */
1587214279Sbrucec		if (cam_periph_owned(periph))
1588214279Sbrucec			continue;
1589214279Sbrucec		cam_periph_lock(periph);
1590214279Sbrucec		softc = (struct ada_softc *)periph->softc;
1591214279Sbrucec		/*
1592214279Sbrucec		 * We only spin-down the drive if it is capable of it..
1593214279Sbrucec		 */
1594214279Sbrucec		if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) {
1595214279Sbrucec			cam_periph_unlock(periph);
1596214279Sbrucec			continue;
1597214279Sbrucec		}
1598214279Sbrucec
1599214279Sbrucec		if (bootverbose)
1600214279Sbrucec			xpt_print(periph->path, "spin-down\n");
1601214279Sbrucec
1602214279Sbrucec		xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1603214279Sbrucec
1604214279Sbrucec		ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
1605214279Sbrucec		cam_fill_ataio(&ccb.ataio,
1606214279Sbrucec				    1,
1607214279Sbrucec				    adadone,
1608220650Smav				    CAM_DIR_NONE | flags,
1609214279Sbrucec				    0,
1610214279Sbrucec				    NULL,
1611214279Sbrucec				    0,
1612214279Sbrucec				    ada_default_timeout*1000);
1613214279Sbrucec
1614220650Smav		ata_28bit_cmd(&ccb.ataio, cmd, 0, 0, 0);
1615214279Sbrucec		xpt_polled_action(&ccb);
1616214279Sbrucec
1617214279Sbrucec		if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
1618214279Sbrucec			xpt_print(periph->path, "Spin-down disk failed\n");
1619214279Sbrucec
1620214279Sbrucec		if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
1621214279Sbrucec			cam_release_devq(ccb.ccb_h.path,
1622214279Sbrucec					 /*relsim_flags*/0,
1623214279Sbrucec					 /*reduction*/0,
1624214279Sbrucec					 /*timeout*/0,
1625214279Sbrucec					 /*getcount_only*/0);
1626214279Sbrucec		cam_periph_unlock(periph);
1627214279Sbrucec	}
1628195534Sscottl}
1629195534Sscottl
1630220650Smavstatic void
1631220650Smavadashutdown(void *arg, int howto)
1632220650Smav{
1633220650Smav
1634220650Smav	adaflush();
1635220650Smav	if (ada_spindown_shutdown != 0 &&
1636220650Smav	    (howto & (RB_HALT | RB_POWEROFF)) != 0)
1637220650Smav		adaspindown(ATA_STANDBY_IMMEDIATE, 0);
1638220650Smav}
1639220650Smav
1640220650Smavstatic void
1641220650Smavadasuspend(void *arg)
1642220650Smav{
1643220650Smav
1644220650Smav	adaflush();
1645220650Smav	if (ada_spindown_suspend != 0)
1646220650Smav		adaspindown(ATA_SLEEP, CAM_DEV_QFREEZE);
1647220650Smav}
1648220650Smav
1649220650Smavstatic void
1650220650Smavadaresume(void *arg)
1651220650Smav{
1652220650Smav	struct cam_periph *periph;
1653220650Smav	struct ada_softc *softc;
1654220650Smav
1655220650Smav	if (ada_spindown_suspend == 0)
1656220650Smav		return;
1657220650Smav
1658220650Smav	TAILQ_FOREACH(periph, &adadriver.units, unit_links) {
1659220650Smav		cam_periph_lock(periph);
1660220650Smav		softc = (struct ada_softc *)periph->softc;
1661220650Smav		/*
1662220650Smav		 * We only spin-down the drive if it is capable of it..
1663220650Smav		 */
1664220650Smav		if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) {
1665220650Smav			cam_periph_unlock(periph);
1666220650Smav			continue;
1667220650Smav		}
1668220650Smav
1669220650Smav		if (bootverbose)
1670220650Smav			xpt_print(periph->path, "resume\n");
1671220650Smav
1672220650Smav		/*
1673220650Smav		 * Drop freeze taken due to CAM_DEV_QFREEZE flag set on
1674220650Smav		 * sleep request.
1675220650Smav		 */
1676220650Smav		cam_release_devq(periph->path,
1677220650Smav			 /*relsim_flags*/0,
1678220650Smav			 /*openings*/0,
1679220650Smav			 /*timeout*/0,
1680220650Smav			 /*getcount_only*/0);
1681220650Smav
1682220650Smav		cam_periph_unlock(periph);
1683220650Smav	}
1684220650Smav}
1685220650Smav
1686195534Sscottl#endif /* _KERNEL */
1687