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$");
29195534Sscottl
30195534Sscottl#include <sys/param.h>
31195534Sscottl#include <sys/bus.h>
32195534Sscottl#include <sys/endian.h>
33195534Sscottl#include <sys/systm.h>
34195534Sscottl#include <sys/types.h>
35195534Sscottl#include <sys/malloc.h>
36195534Sscottl#include <sys/kernel.h>
37195534Sscottl#include <sys/time.h>
38195534Sscottl#include <sys/conf.h>
39195534Sscottl#include <sys/fcntl.h>
40195534Sscottl#include <sys/interrupt.h>
41195534Sscottl#include <sys/sbuf.h>
42195534Sscottl
43195534Sscottl#include <sys/lock.h>
44195534Sscottl#include <sys/mutex.h>
45195534Sscottl#include <sys/sysctl.h>
46195534Sscottl
47195534Sscottl#include <cam/cam.h>
48195534Sscottl#include <cam/cam_ccb.h>
49195534Sscottl#include <cam/cam_queue.h>
50195534Sscottl#include <cam/cam_periph.h>
51195534Sscottl#include <cam/cam_sim.h>
52195534Sscottl#include <cam/cam_xpt.h>
53195534Sscottl#include <cam/cam_xpt_sim.h>
54195534Sscottl#include <cam/cam_xpt_periph.h>
55195534Sscottl#include <cam/cam_xpt_internal.h>
56195534Sscottl#include <cam/cam_debug.h>
57195534Sscottl
58195534Sscottl#include <cam/scsi/scsi_all.h>
59195534Sscottl#include <cam/scsi/scsi_message.h>
60195534Sscottl#include <cam/ata/ata_all.h>
61195534Sscottl#include <machine/stdarg.h>	/* for xpt_print below */
62195534Sscottl#include "opt_cam.h"
63195534Sscottl
64199178Smavstruct ata_quirk_entry {
65195534Sscottl	struct scsi_inquiry_pattern inq_pat;
66195534Sscottl	u_int8_t quirks;
67199178Smav#define	CAM_QUIRK_MAXTAGS	0x01
68236234Smav	u_int mintags;
69195534Sscottl	u_int maxtags;
70195534Sscottl};
71195534Sscottl
72195534Sscottlstatic periph_init_t probe_periph_init;
73195534Sscottl
74195534Sscottlstatic struct periph_driver probe_driver =
75195534Sscottl{
76195653Smav	probe_periph_init, "aprobe",
77198708Smav	TAILQ_HEAD_INITIALIZER(probe_driver.units), /* generation */ 0,
78198708Smav	CAM_PERIPH_DRV_EARLY
79195534Sscottl};
80195534Sscottl
81195653SmavPERIPHDRIVER_DECLARE(aprobe, probe_driver);
82195534Sscottl
83195534Sscottltypedef enum {
84195534Sscottl	PROBE_RESET,
85195534Sscottl	PROBE_IDENTIFY,
86203421Smav	PROBE_SPINUP,
87195534Sscottl	PROBE_SETMODE,
88207499Smav	PROBE_SETPM,
89207499Smav	PROBE_SETAPST,
90207499Smav	PROBE_SETDMAAA,
91220602Smav	PROBE_SETAN,
92198708Smav	PROBE_SET_MULTI,
93195534Sscottl	PROBE_INQUIRY,
94195534Sscottl	PROBE_FULL_INQUIRY,
95195534Sscottl	PROBE_PM_PID,
96195534Sscottl	PROBE_PM_PRV,
97235897Smav	PROBE_IDENTIFY_SES,
98235897Smav	PROBE_IDENTIFY_SAFTE,
99236613Smav	PROBE_DONE,
100195534Sscottl	PROBE_INVALID
101195534Sscottl} probe_action;
102195534Sscottl
103195534Sscottlstatic char *probe_action_text[] = {
104195534Sscottl	"PROBE_RESET",
105195534Sscottl	"PROBE_IDENTIFY",
106203421Smav	"PROBE_SPINUP",
107195534Sscottl	"PROBE_SETMODE",
108207499Smav	"PROBE_SETPM",
109207499Smav	"PROBE_SETAPST",
110207499Smav	"PROBE_SETDMAAA",
111220602Smav	"PROBE_SETAN",
112198708Smav	"PROBE_SET_MULTI",
113195534Sscottl	"PROBE_INQUIRY",
114195534Sscottl	"PROBE_FULL_INQUIRY",
115195534Sscottl	"PROBE_PM_PID",
116195534Sscottl	"PROBE_PM_PRV",
117235897Smav	"PROBE_IDENTIFY_SES",
118235897Smav	"PROBE_IDENTIFY_SAFTE",
119236613Smav	"PROBE_DONE",
120195534Sscottl	"PROBE_INVALID"
121195534Sscottl};
122195534Sscottl
123195534Sscottl#define PROBE_SET_ACTION(softc, newaction)	\
124195534Sscottldo {									\
125195534Sscottl	char **text;							\
126195534Sscottl	text = probe_action_text;					\
127236613Smav	CAM_DEBUG((softc)->periph->path, CAM_DEBUG_PROBE,		\
128195534Sscottl	    ("Probe %s to %s\n", text[(softc)->action],			\
129195534Sscottl	    text[(newaction)]));					\
130195534Sscottl	(softc)->action = (newaction);					\
131195534Sscottl} while(0)
132195534Sscottl
133195534Sscottltypedef enum {
134195534Sscottl	PROBE_NO_ANNOUNCE	= 0x04
135195534Sscottl} probe_flags;
136195534Sscottl
137195534Sscottltypedef struct {
138195534Sscottl	TAILQ_HEAD(, ccb_hdr) request_ccbs;
139203385Smav	struct ata_params	ident_data;
140195534Sscottl	probe_action	action;
141195534Sscottl	probe_flags	flags;
142195534Sscottl	uint32_t	pm_pid;
143195534Sscottl	uint32_t	pm_prv;
144203108Smav	int		restart;
145203421Smav	int		spinup;
146209744Smav	int		faults;
147207499Smav	u_int		caps;
148195534Sscottl	struct cam_periph *periph;
149195534Sscottl} probe_softc;
150195534Sscottl
151199178Smavstatic struct ata_quirk_entry ata_quirk_table[] =
152195534Sscottl{
153195534Sscottl	{
154195534Sscottl		/* Default tagged queuing parameters for all devices */
155195534Sscottl		{
156195534Sscottl		  T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
157195534Sscottl		  /*vendor*/"*", /*product*/"*", /*revision*/"*"
158195534Sscottl		},
159236234Smav		/*quirks*/0, /*mintags*/0, /*maxtags*/0
160195534Sscottl	},
161195534Sscottl};
162195534Sscottl
163199178Smavstatic const int ata_quirk_table_size =
164199178Smav	sizeof(ata_quirk_table) / sizeof(*ata_quirk_table);
165195534Sscottl
166195534Sscottlstatic cam_status	proberegister(struct cam_periph *periph,
167195534Sscottl				      void *arg);
168195534Sscottlstatic void	 probeschedule(struct cam_periph *probe_periph);
169195534Sscottlstatic void	 probestart(struct cam_periph *periph, union ccb *start_ccb);
170236437Smavstatic void	 proberequestdefaultnegotiation(struct cam_periph *periph);
171195534Sscottlstatic void	 probedone(struct cam_periph *periph, union ccb *done_ccb);
172195534Sscottlstatic void	 probecleanup(struct cam_periph *periph);
173199178Smavstatic void	 ata_find_quirk(struct cam_ed *device);
174195534Sscottlstatic void	 ata_scan_bus(struct cam_periph *periph, union ccb *ccb);
175195534Sscottlstatic void	 ata_scan_lun(struct cam_periph *periph,
176195534Sscottl			       struct cam_path *path, cam_flags flags,
177195534Sscottl			       union ccb *ccb);
178195534Sscottlstatic void	 xptscandone(struct cam_periph *periph, union ccb *done_ccb);
179195534Sscottlstatic struct cam_ed *
180195534Sscottl		 ata_alloc_device(struct cam_eb *bus, struct cam_et *target,
181195534Sscottl				   lun_id_t lun_id);
182195534Sscottlstatic void	 ata_device_transport(struct cam_path *path);
183236437Smavstatic void	 ata_get_transfer_settings(struct ccb_trans_settings *cts);
184199178Smavstatic void	 ata_set_transfer_settings(struct ccb_trans_settings *cts,
185195534Sscottl					    struct cam_ed *device,
186195534Sscottl					    int async_update);
187195534Sscottlstatic void	 ata_dev_async(u_int32_t async_code,
188195534Sscottl				struct cam_eb *bus,
189195534Sscottl				struct cam_et *target,
190195534Sscottl				struct cam_ed *device,
191195534Sscottl				void *async_arg);
192195534Sscottlstatic void	 ata_action(union ccb *start_ccb);
193204220Smavstatic void	 ata_announce_periph(struct cam_periph *periph);
194195534Sscottl
195230912Smavstatic int ata_dma = 1;
196230912Smavstatic int atapi_dma = 1;
197230912Smav
198230912SmavTUNABLE_INT("hw.ata.ata_dma", &ata_dma);
199230912SmavTUNABLE_INT("hw.ata.atapi_dma", &atapi_dma);
200230912Smav
201195534Sscottlstatic struct xpt_xport ata_xport = {
202195534Sscottl	.alloc_device = ata_alloc_device,
203195534Sscottl	.action = ata_action,
204195534Sscottl	.async = ata_dev_async,
205204220Smav	.announce = ata_announce_periph,
206195534Sscottl};
207195534Sscottl
208195534Sscottlstruct xpt_xport *
209195534Sscottlata_get_xport(void)
210195534Sscottl{
211195534Sscottl	return (&ata_xport);
212195534Sscottl}
213195534Sscottl
214195534Sscottlstatic void
215195534Sscottlprobe_periph_init()
216195534Sscottl{
217195534Sscottl}
218195534Sscottl
219195534Sscottlstatic cam_status
220195534Sscottlproberegister(struct cam_periph *periph, void *arg)
221195534Sscottl{
222195534Sscottl	union ccb *request_ccb;	/* CCB representing the probe request */
223195534Sscottl	cam_status status;
224195534Sscottl	probe_softc *softc;
225195534Sscottl
226195534Sscottl	request_ccb = (union ccb *)arg;
227195534Sscottl	if (request_ccb == NULL) {
228195534Sscottl		printf("proberegister: no probe CCB, "
229195534Sscottl		       "can't register device\n");
230195534Sscottl		return(CAM_REQ_CMP_ERR);
231195534Sscottl	}
232195534Sscottl
233203421Smav	softc = (probe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_ZERO | M_NOWAIT);
234195534Sscottl
235195534Sscottl	if (softc == NULL) {
236195534Sscottl		printf("proberegister: Unable to probe new device. "
237195534Sscottl		       "Unable to allocate softc\n");
238195534Sscottl		return(CAM_REQ_CMP_ERR);
239195534Sscottl	}
240195534Sscottl	TAILQ_INIT(&softc->request_ccbs);
241195534Sscottl	TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
242195534Sscottl			  periph_links.tqe);
243195534Sscottl	softc->flags = 0;
244195534Sscottl	periph->softc = softc;
245195534Sscottl	softc->periph = periph;
246195534Sscottl	softc->action = PROBE_INVALID;
247195534Sscottl	status = cam_periph_acquire(periph);
248195534Sscottl	if (status != CAM_REQ_CMP) {
249195534Sscottl		return (status);
250195534Sscottl	}
251236613Smav	CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe started\n"));
252195534Sscottl	probeschedule(periph);
253195534Sscottl	return(CAM_REQ_CMP);
254195534Sscottl}
255195534Sscottl
256195534Sscottlstatic void
257195534Sscottlprobeschedule(struct cam_periph *periph)
258195534Sscottl{
259195534Sscottl	union ccb *ccb;
260195534Sscottl	probe_softc *softc;
261195534Sscottl
262195534Sscottl	softc = (probe_softc *)periph->softc;
263195534Sscottl	ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
264195534Sscottl
265198389Smav	if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) ||
266235897Smav	    periph->path->device->protocol == PROTO_SATAPM ||
267235897Smav	    periph->path->device->protocol == PROTO_SEMB)
268195534Sscottl		PROBE_SET_ACTION(softc, PROBE_RESET);
269195534Sscottl	else
270195534Sscottl		PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
271195534Sscottl
272195534Sscottl	if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
273195534Sscottl		softc->flags |= PROBE_NO_ANNOUNCE;
274195534Sscottl	else
275195534Sscottl		softc->flags &= ~PROBE_NO_ANNOUNCE;
276195534Sscottl
277203108Smav	xpt_schedule(periph, CAM_PRIORITY_XPT);
278195534Sscottl}
279195534Sscottl
280195534Sscottlstatic void
281195534Sscottlprobestart(struct cam_periph *periph, union ccb *start_ccb)
282195534Sscottl{
283199747Smav	struct ccb_trans_settings cts;
284195534Sscottl	struct ccb_ataio *ataio;
285195534Sscottl	struct ccb_scsiio *csio;
286195534Sscottl	probe_softc *softc;
287198708Smav	struct cam_path *path;
288198708Smav	struct ata_params *ident_buf;
289195534Sscottl
290195534Sscottl	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
291195534Sscottl
292195534Sscottl	softc = (probe_softc *)periph->softc;
293198708Smav	path = start_ccb->ccb_h.path;
294195534Sscottl	ataio = &start_ccb->ataio;
295195534Sscottl	csio = &start_ccb->csio;
296198708Smav	ident_buf = &periph->path->device->ident_data;
297195534Sscottl
298203108Smav	if (softc->restart) {
299203108Smav		softc->restart = 0;
300203108Smav		if ((path->device->flags & CAM_DEV_UNCONFIGURED) ||
301235897Smav		    path->device->protocol == PROTO_SATAPM ||
302235897Smav		    path->device->protocol == PROTO_SEMB)
303203108Smav			softc->action = PROBE_RESET;
304203108Smav		else
305203108Smav			softc->action = PROBE_IDENTIFY;
306203108Smav	}
307195534Sscottl	switch (softc->action) {
308195534Sscottl	case PROBE_RESET:
309195534Sscottl		cam_fill_ataio(ataio,
310195534Sscottl		      0,
311195534Sscottl		      probedone,
312195534Sscottl		      /*flags*/CAM_DIR_NONE,
313198389Smav		      0,
314195534Sscottl		      /*data_ptr*/NULL,
315195534Sscottl		      /*dxfer_len*/0,
316203108Smav		      15 * 1000);
317195534Sscottl		ata_reset_cmd(ataio);
318195534Sscottl		break;
319195534Sscottl	case PROBE_IDENTIFY:
320195534Sscottl		cam_fill_ataio(ataio,
321195534Sscottl		      1,
322195534Sscottl		      probedone,
323195534Sscottl		      /*flags*/CAM_DIR_IN,
324198389Smav		      0,
325203385Smav		      /*data_ptr*/(u_int8_t *)&softc->ident_data,
326203385Smav		      /*dxfer_len*/sizeof(softc->ident_data),
327195534Sscottl		      30 * 1000);
328195534Sscottl		if (periph->path->device->protocol == PROTO_ATA)
329196659Smav			ata_28bit_cmd(ataio, ATA_ATA_IDENTIFY, 0, 0, 0);
330195534Sscottl		else
331196659Smav			ata_28bit_cmd(ataio, ATA_ATAPI_IDENTIFY, 0, 0, 0);
332195534Sscottl		break;
333203421Smav	case PROBE_SPINUP:
334203421Smav		if (bootverbose)
335203421Smav			xpt_print(path, "Spinning up device\n");
336203421Smav		cam_fill_ataio(ataio,
337203421Smav		      1,
338203421Smav		      probedone,
339203421Smav		      /*flags*/CAM_DIR_NONE | CAM_HIGH_POWER,
340203421Smav		      0,
341203421Smav		      /*data_ptr*/NULL,
342203421Smav		      /*dxfer_len*/0,
343203421Smav		      30 * 1000);
344203421Smav		ata_28bit_cmd(ataio, ATA_SETFEATURES, ATA_SF_PUIS_SPINUP, 0, 0);
345203421Smav		break;
346195534Sscottl	case PROBE_SETMODE:
347199747Smav	{
348199747Smav		int mode, wantmode;
349199747Smav
350199747Smav		mode = 0;
351199747Smav		/* Fetch user modes from SIM. */
352199747Smav		bzero(&cts, sizeof(cts));
353203108Smav		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
354199747Smav		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
355199747Smav		cts.type = CTS_TYPE_USER_SETTINGS;
356199747Smav		xpt_action((union ccb *)&cts);
357199747Smav		if (path->device->transport == XPORT_ATA) {
358199747Smav			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_MODE)
359199747Smav				mode = cts.xport_specific.ata.mode;
360199747Smav		} else {
361199799Smav			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_MODE)
362199747Smav				mode = cts.xport_specific.sata.mode;
363199747Smav		}
364230912Smav		if (periph->path->device->protocol == PROTO_ATA) {
365230912Smav			if (ata_dma == 0 && (mode == 0 || mode > ATA_PIO_MAX))
366230912Smav				mode = ATA_PIO_MAX;
367230912Smav		} else {
368230912Smav			if (atapi_dma == 0 && (mode == 0 || mode > ATA_PIO_MAX))
369230912Smav				mode = ATA_PIO_MAX;
370230912Smav		}
371199747Smavnegotiate:
372199747Smav		/* Honor device capabilities. */
373199747Smav		wantmode = mode = ata_max_mode(ident_buf, mode);
374199747Smav		/* Report modes to SIM. */
375199747Smav		bzero(&cts, sizeof(cts));
376203108Smav		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
377199747Smav		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
378199747Smav		cts.type = CTS_TYPE_CURRENT_SETTINGS;
379199747Smav		if (path->device->transport == XPORT_ATA) {
380199747Smav			cts.xport_specific.ata.mode = mode;
381199747Smav			cts.xport_specific.ata.valid = CTS_ATA_VALID_MODE;
382199747Smav		} else {
383199747Smav			cts.xport_specific.sata.mode = mode;
384199747Smav			cts.xport_specific.sata.valid = CTS_SATA_VALID_MODE;
385199747Smav		}
386199747Smav		xpt_action((union ccb *)&cts);
387200171Smav		/* Fetch current modes from SIM. */
388199747Smav		bzero(&cts, sizeof(cts));
389203108Smav		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
390199747Smav		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
391199747Smav		cts.type = CTS_TYPE_CURRENT_SETTINGS;
392199747Smav		xpt_action((union ccb *)&cts);
393199747Smav		if (path->device->transport == XPORT_ATA) {
394199747Smav			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_MODE)
395199747Smav				mode = cts.xport_specific.ata.mode;
396199747Smav		} else {
397199747Smav			if (cts.xport_specific.ata.valid & CTS_SATA_VALID_MODE)
398199747Smav				mode = cts.xport_specific.sata.mode;
399199747Smav		}
400199747Smav		/* If SIM disagree - renegotiate. */
401199747Smav		if (mode != wantmode)
402199747Smav			goto negotiate;
403220886Smav		/* Remember what transport thinks about DMA. */
404220886Smav		if (mode < ATA_DMA)
405220886Smav			path->device->inq_flags &= ~SID_DMA;
406220886Smav		else
407220886Smav			path->device->inq_flags |= SID_DMA;
408236393Smav		xpt_async(AC_GETDEV_CHANGED, path, NULL);
409195534Sscottl		cam_fill_ataio(ataio,
410195534Sscottl		      1,
411195534Sscottl		      probedone,
412196353Smav		      /*flags*/CAM_DIR_NONE,
413196353Smav		      0,
414196353Smav		      /*data_ptr*/NULL,
415196353Smav		      /*dxfer_len*/0,
416195534Sscottl		      30 * 1000);
417199747Smav		ata_28bit_cmd(ataio, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
418195534Sscottl		break;
419199747Smav	}
420207499Smav	case PROBE_SETPM:
421207499Smav		cam_fill_ataio(ataio,
422207499Smav		    1,
423207499Smav		    probedone,
424207499Smav		    CAM_DIR_NONE,
425207499Smav		    0,
426207499Smav		    NULL,
427207499Smav		    0,
428207499Smav		    30*1000);
429207499Smav		ata_28bit_cmd(ataio, ATA_SETFEATURES,
430207499Smav		    (softc->caps & CTS_SATA_CAPS_H_PMREQ) ? 0x10 : 0x90,
431207499Smav		    0, 0x03);
432207499Smav		break;
433207499Smav	case PROBE_SETAPST:
434207499Smav		cam_fill_ataio(ataio,
435207499Smav		    1,
436207499Smav		    probedone,
437207499Smav		    CAM_DIR_NONE,
438207499Smav		    0,
439207499Smav		    NULL,
440207499Smav		    0,
441207499Smav		    30*1000);
442207499Smav		ata_28bit_cmd(ataio, ATA_SETFEATURES,
443207499Smav		    (softc->caps & CTS_SATA_CAPS_H_APST) ? 0x10 : 0x90,
444207499Smav		    0, 0x07);
445207499Smav		break;
446207499Smav	case PROBE_SETDMAAA:
447207499Smav		cam_fill_ataio(ataio,
448207499Smav		    1,
449207499Smav		    probedone,
450207499Smav		    CAM_DIR_NONE,
451207499Smav		    0,
452207499Smav		    NULL,
453207499Smav		    0,
454207499Smav		    30*1000);
455207499Smav		ata_28bit_cmd(ataio, ATA_SETFEATURES,
456207499Smav		    (softc->caps & CTS_SATA_CAPS_H_DMAAA) ? 0x10 : 0x90,
457207499Smav		    0, 0x02);
458207499Smav		break;
459220602Smav	case PROBE_SETAN:
460238886Smav		/* Remember what transport thinks about AEN. */
461238886Smav		if (softc->caps & CTS_SATA_CAPS_H_AN)
462238886Smav			path->device->inq_flags |= SID_AEN;
463238886Smav		else
464238886Smav			path->device->inq_flags &= ~SID_AEN;
465238886Smav		xpt_async(AC_GETDEV_CHANGED, path, NULL);
466220602Smav		cam_fill_ataio(ataio,
467220602Smav		    1,
468220602Smav		    probedone,
469220602Smav		    CAM_DIR_NONE,
470220602Smav		    0,
471220602Smav		    NULL,
472220602Smav		    0,
473220602Smav		    30*1000);
474220602Smav		ata_28bit_cmd(ataio, ATA_SETFEATURES,
475220602Smav		    (softc->caps & CTS_SATA_CAPS_H_AN) ? 0x10 : 0x90,
476220602Smav		    0, 0x05);
477220602Smav		break;
478198708Smav	case PROBE_SET_MULTI:
479198708Smav	{
480200171Smav		u_int sectors, bytecount;
481198708Smav
482200171Smav		bytecount = 8192;	/* SATA maximum */
483200171Smav		/* Fetch user bytecount from SIM. */
484200171Smav		bzero(&cts, sizeof(cts));
485203108Smav		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
486200171Smav		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
487200171Smav		cts.type = CTS_TYPE_USER_SETTINGS;
488200171Smav		xpt_action((union ccb *)&cts);
489200171Smav		if (path->device->transport == XPORT_ATA) {
490200171Smav			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
491200171Smav				bytecount = cts.xport_specific.ata.bytecount;
492200171Smav		} else {
493200171Smav			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
494200171Smav				bytecount = cts.xport_specific.sata.bytecount;
495200171Smav		}
496200171Smav		/* Honor device capabilities. */
497200171Smav		sectors = max(1, min(ident_buf->sectors_intr & 0xff,
498200171Smav		    bytecount / ata_logical_sector_size(ident_buf)));
499198708Smav		/* Report bytecount to SIM. */
500198708Smav		bzero(&cts, sizeof(cts));
501203108Smav		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
502198708Smav		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
503198708Smav		cts.type = CTS_TYPE_CURRENT_SETTINGS;
504198708Smav		if (path->device->transport == XPORT_ATA) {
505198897Smav			cts.xport_specific.ata.bytecount = sectors *
506198897Smav			    ata_logical_sector_size(ident_buf);
507198708Smav			cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT;
508198708Smav		} else {
509198897Smav			cts.xport_specific.sata.bytecount = sectors *
510198897Smav			    ata_logical_sector_size(ident_buf);
511198708Smav			cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT;
512198708Smav		}
513198708Smav		xpt_action((union ccb *)&cts);
514200171Smav		/* Fetch current bytecount from SIM. */
515200171Smav		bzero(&cts, sizeof(cts));
516203108Smav		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
517200171Smav		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
518200171Smav		cts.type = CTS_TYPE_CURRENT_SETTINGS;
519200171Smav		xpt_action((union ccb *)&cts);
520200171Smav		if (path->device->transport == XPORT_ATA) {
521200171Smav			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
522200171Smav				bytecount = cts.xport_specific.ata.bytecount;
523200171Smav		} else {
524200171Smav			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
525200171Smav				bytecount = cts.xport_specific.sata.bytecount;
526200171Smav		}
527200171Smav		sectors = bytecount / ata_logical_sector_size(ident_buf);
528198708Smav
529198708Smav		cam_fill_ataio(ataio,
530198708Smav		    1,
531198708Smav		    probedone,
532198708Smav		    CAM_DIR_NONE,
533198708Smav		    0,
534198708Smav		    NULL,
535198708Smav		    0,
536198708Smav		    30*1000);
537198708Smav		ata_28bit_cmd(ataio, ATA_SET_MULTI, 0, 0, sectors);
538198708Smav		break;
539195534Sscottl	}
540195534Sscottl	case PROBE_INQUIRY:
541200171Smav	{
542200171Smav		u_int bytecount;
543200171Smav
544200171Smav		bytecount = 8192;	/* SATA maximum */
545200171Smav		/* Fetch user bytecount from SIM. */
546200171Smav		bzero(&cts, sizeof(cts));
547203108Smav		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
548200171Smav		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
549200171Smav		cts.type = CTS_TYPE_USER_SETTINGS;
550200171Smav		xpt_action((union ccb *)&cts);
551200171Smav		if (path->device->transport == XPORT_ATA) {
552200171Smav			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
553200171Smav				bytecount = cts.xport_specific.ata.bytecount;
554200171Smav		} else {
555200171Smav			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
556200171Smav				bytecount = cts.xport_specific.sata.bytecount;
557200171Smav		}
558200171Smav		/* Honor device capabilities. */
559200171Smav		bytecount &= ~1;
560200171Smav		bytecount = max(2, min(65534, bytecount));
561200171Smav		if (ident_buf->satacapabilities != 0x0000 &&
562200171Smav		    ident_buf->satacapabilities != 0xffff) {
563200171Smav			bytecount = min(8192, bytecount);
564200171Smav		}
565200171Smav		/* Report bytecount to SIM. */
566200171Smav		bzero(&cts, sizeof(cts));
567203108Smav		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
568200171Smav		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
569200171Smav		cts.type = CTS_TYPE_CURRENT_SETTINGS;
570200171Smav		if (path->device->transport == XPORT_ATA) {
571200171Smav			cts.xport_specific.ata.bytecount = bytecount;
572200171Smav			cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT;
573200171Smav		} else {
574200171Smav			cts.xport_specific.sata.bytecount = bytecount;
575200171Smav			cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT;
576200171Smav		}
577200171Smav		xpt_action((union ccb *)&cts);
578200171Smav		/* FALLTHROUGH */
579200171Smav	}
580195534Sscottl	case PROBE_FULL_INQUIRY:
581195534Sscottl	{
582195534Sscottl		u_int inquiry_len;
583195534Sscottl		struct scsi_inquiry_data *inq_buf =
584195534Sscottl		    &periph->path->device->inq_data;
585195534Sscottl
586195534Sscottl		if (softc->action == PROBE_INQUIRY)
587195534Sscottl			inquiry_len = SHORT_INQUIRY_LENGTH;
588195534Sscottl		else
589195534Sscottl			inquiry_len = SID_ADDITIONAL_LENGTH(inq_buf);
590195534Sscottl		/*
591195534Sscottl		 * Some parallel SCSI devices fail to send an
592195534Sscottl		 * ignore wide residue message when dealing with
593195534Sscottl		 * odd length inquiry requests.  Round up to be
594195534Sscottl		 * safe.
595195534Sscottl		 */
596195534Sscottl		inquiry_len = roundup2(inquiry_len, 2);
597195534Sscottl		scsi_inquiry(csio,
598195534Sscottl			     /*retries*/1,
599195534Sscottl			     probedone,
600195534Sscottl			     MSG_SIMPLE_Q_TAG,
601195534Sscottl			     (u_int8_t *)inq_buf,
602195534Sscottl			     inquiry_len,
603195534Sscottl			     /*evpd*/FALSE,
604195534Sscottl			     /*page_code*/0,
605195534Sscottl			     SSD_MIN_SIZE,
606195534Sscottl			     /*timeout*/60 * 1000);
607195534Sscottl		break;
608195534Sscottl	}
609195534Sscottl	case PROBE_PM_PID:
610195534Sscottl		cam_fill_ataio(ataio,
611195534Sscottl		      1,
612195534Sscottl		      probedone,
613195534Sscottl		      /*flags*/CAM_DIR_NONE,
614198389Smav		      0,
615195534Sscottl		      /*data_ptr*/NULL,
616195534Sscottl		      /*dxfer_len*/0,
617195534Sscottl		      10 * 1000);
618195534Sscottl		ata_pm_read_cmd(ataio, 0, 15);
619195534Sscottl		break;
620195534Sscottl	case PROBE_PM_PRV:
621195534Sscottl		cam_fill_ataio(ataio,
622195534Sscottl		      1,
623195534Sscottl		      probedone,
624195534Sscottl		      /*flags*/CAM_DIR_NONE,
625198389Smav		      0,
626195534Sscottl		      /*data_ptr*/NULL,
627195534Sscottl		      /*dxfer_len*/0,
628195534Sscottl		      10 * 1000);
629195534Sscottl		ata_pm_read_cmd(ataio, 1, 15);
630195534Sscottl		break;
631235897Smav	case PROBE_IDENTIFY_SES:
632235897Smav		cam_fill_ataio(ataio,
633235897Smav		      1,
634235897Smav		      probedone,
635235897Smav		      /*flags*/CAM_DIR_IN,
636235897Smav		      0,
637235897Smav		      /*data_ptr*/(u_int8_t *)&softc->ident_data,
638235897Smav		      /*dxfer_len*/sizeof(softc->ident_data),
639235897Smav		      30 * 1000);
640235897Smav		ata_28bit_cmd(ataio, ATA_SEP_ATTN, 0xEC, 0x02,
641235897Smav		    sizeof(softc->ident_data) / 4);
642235897Smav		break;
643235897Smav	case PROBE_IDENTIFY_SAFTE:
644235897Smav		cam_fill_ataio(ataio,
645235897Smav		      1,
646235897Smav		      probedone,
647235897Smav		      /*flags*/CAM_DIR_IN,
648235897Smav		      0,
649235897Smav		      /*data_ptr*/(u_int8_t *)&softc->ident_data,
650235897Smav		      /*dxfer_len*/sizeof(softc->ident_data),
651235897Smav		      30 * 1000);
652235897Smav		ata_28bit_cmd(ataio, ATA_SEP_ATTN, 0xEC, 0x00,
653235897Smav		    sizeof(softc->ident_data) / 4);
654235897Smav		break;
655195534Sscottl	default:
656236613Smav		panic("probestart: invalid action state 0x%x\n", softc->action);
657195534Sscottl	}
658249466Smav	start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
659195534Sscottl	xpt_action(start_ccb);
660195534Sscottl}
661236437Smav
662195534Sscottlstatic void
663195534Sscottlproberequestdefaultnegotiation(struct cam_periph *periph)
664195534Sscottl{
665195534Sscottl	struct ccb_trans_settings cts;
666195534Sscottl
667203108Smav	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
668195534Sscottl	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
669195534Sscottl	cts.type = CTS_TYPE_USER_SETTINGS;
670195534Sscottl	xpt_action((union ccb *)&cts);
671236437Smav	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
672195534Sscottl		return;
673236437Smav	cts.xport_specific.valid = 0;
674195534Sscottl	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
675195534Sscottl	cts.type = CTS_TYPE_CURRENT_SETTINGS;
676195534Sscottl	xpt_action((union ccb *)&cts);
677195534Sscottl}
678195534Sscottl
679195534Sscottlstatic void
680195534Sscottlprobedone(struct cam_periph *periph, union ccb *done_ccb)
681195534Sscottl{
682199747Smav	struct ccb_trans_settings cts;
683195534Sscottl	struct ata_params *ident_buf;
684235897Smav	struct scsi_inquiry_data *inq_buf;
685195534Sscottl	probe_softc *softc;
686195534Sscottl	struct cam_path *path;
687217444Smav	cam_status status;
688195534Sscottl	u_int32_t  priority;
689207499Smav	u_int caps;
690235897Smav	int changed = 1, found = 1;
691235897Smav	static const uint8_t fake_device_id_hdr[8] =
692235897Smav	    {0, SVPD_DEVICE_ID, 0, 12,
693235897Smav	     SVPD_ID_CODESET_BINARY, SVPD_ID_TYPE_NAA, 0, 8};
694195534Sscottl
695195534Sscottl	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
696195534Sscottl
697195534Sscottl	softc = (probe_softc *)periph->softc;
698195534Sscottl	path = done_ccb->ccb_h.path;
699195534Sscottl	priority = done_ccb->ccb_h.pinfo.priority;
700195534Sscottl	ident_buf = &path->device->ident_data;
701235897Smav	inq_buf = &path->device->inq_data;
702195534Sscottl
703198708Smav	if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
704236814Smav		if (cam_periph_error(done_ccb,
705236814Smav		    0, softc->restart ? (SF_NO_RECOVERY | SF_NO_RETRY) : 0,
706249466Smav		    NULL) == ERESTART) {
707249466Smavout:
708249466Smav			/* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
709249466Smav			cam_release_devq(path, 0, 0, 0, FALSE);
710195534Sscottl			return;
711249466Smav		}
712209744Smav		if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
713195534Sscottl			/* Don't wedge the queue */
714249466Smav			xpt_release_devq(path, /*count*/1, /*run_queue*/TRUE);
715195534Sscottl		}
716217444Smav		status = done_ccb->ccb_h.status & CAM_STATUS_MASK;
717209744Smav		if (softc->restart) {
718209744Smav			softc->faults++;
719209744Smav			if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) ==
720209744Smav			    CAM_CMD_TIMEOUT)
721209744Smav				softc->faults += 4;
722209744Smav			if (softc->faults < 10)
723209744Smav				goto done;
724209744Smav			else
725209744Smav				softc->restart = 0;
726217444Smav
727198708Smav		/* Old PIO2 devices may not support mode setting. */
728217444Smav		} else if (softc->action == PROBE_SETMODE &&
729217444Smav		    status == CAM_ATA_STATUS_ERROR &&
730198708Smav		    ata_max_pmode(ident_buf) <= ATA_PIO2 &&
731217444Smav		    (ident_buf->capabilities1 & ATA_SUPPORT_IORDY) == 0) {
732198708Smav			goto noerror;
733217444Smav
734198708Smav		/*
735217444Smav		 * Some old WD SATA disks report supported and enabled
736217444Smav		 * device-initiated interface power management, but return
737217444Smav		 * ABORT on attempt to disable it.
738217444Smav		 */
739217444Smav		} else if (softc->action == PROBE_SETPM &&
740217444Smav		    status == CAM_ATA_STATUS_ERROR) {
741217444Smav			goto noerror;
742217875Smav
743217875Smav		/*
744217875Smav		 * Some HP SATA disks report supported DMA Auto-Activation,
745217875Smav		 * but return ABORT on attempt to enable it.
746217875Smav		 */
747217875Smav		} else if (softc->action == PROBE_SETDMAAA &&
748217875Smav		    status == CAM_ATA_STATUS_ERROR) {
749217875Smav			goto noerror;
750235897Smav
751235897Smav		/*
752243571Smav		 * Some Samsung SSDs report supported Asynchronous Notification,
753243571Smav		 * but return ABORT on attempt to enable it.
754243571Smav		 */
755243571Smav		} else if (softc->action == PROBE_SETAN &&
756243571Smav		    status == CAM_ATA_STATUS_ERROR) {
757243571Smav			goto noerror;
758243571Smav
759243571Smav		/*
760235897Smav		 * SES and SAF-TE SEPs have different IDENTIFY commands,
761235897Smav		 * but SATA specification doesn't tell how to identify them.
762235897Smav		 * Until better way found, just try another if first fail.
763235897Smav		 */
764235897Smav		} else if (softc->action == PROBE_IDENTIFY_SES &&
765235897Smav		    status == CAM_ATA_STATUS_ERROR) {
766235897Smav			PROBE_SET_ACTION(softc, PROBE_IDENTIFY_SAFTE);
767235897Smav			xpt_release_ccb(done_ccb);
768235897Smav			xpt_schedule(periph, priority);
769249466Smav			goto out;
770217444Smav		}
771217444Smav
772217444Smav		/*
773198708Smav		 * If we get to this point, we got an error status back
774198708Smav		 * from the inquiry and the error status doesn't require
775198708Smav		 * automatically retrying the command.  Therefore, the
776198708Smav		 * inquiry failed.  If we had inquiry information before
777198708Smav		 * for this device, but this latest inquiry command failed,
778198708Smav		 * the device has probably gone away.  If this device isn't
779198708Smav		 * already marked unconfigured, notify the peripheral
780198708Smav		 * drivers that this device is no more.
781198708Smav		 */
782209744Smavdevice_fail:	if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
783198708Smav			xpt_async(AC_LOST_DEVICE, path, NULL);
784236613Smav		PROBE_SET_ACTION(softc, PROBE_INVALID);
785198708Smav		found = 0;
786198708Smav		goto done;
787198708Smav	}
788198708Smavnoerror:
789203385Smav	if (softc->restart)
790203385Smav		goto done;
791198708Smav	switch (softc->action) {
792198708Smav	case PROBE_RESET:
793195534Sscottl	{
794198708Smav		int sign = (done_ccb->ataio.res.lba_high << 8) +
795198708Smav		    done_ccb->ataio.res.lba_mid;
796236613Smav		CAM_DEBUG(path, CAM_DEBUG_PROBE,
797236613Smav		    ("SIGNATURE: %04x\n", sign));
798198708Smav		if (sign == 0x0000 &&
799198708Smav		    done_ccb->ccb_h.target_id != 15) {
800198708Smav			path->device->protocol = PROTO_ATA;
801198708Smav			PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
802198708Smav		} else if (sign == 0x9669 &&
803198708Smav		    done_ccb->ccb_h.target_id == 15) {
804199747Smav			/* Report SIM that PM is present. */
805198708Smav			bzero(&cts, sizeof(cts));
806203108Smav			xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
807198708Smav			cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
808198708Smav			cts.type = CTS_TYPE_CURRENT_SETTINGS;
809198708Smav			cts.xport_specific.sata.pm_present = 1;
810198708Smav			cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
811198708Smav			xpt_action((union ccb *)&cts);
812198708Smav			path->device->protocol = PROTO_SATAPM;
813198708Smav			PROBE_SET_ACTION(softc, PROBE_PM_PID);
814235897Smav		} else if (sign == 0xc33c &&
815235897Smav		    done_ccb->ccb_h.target_id != 15) {
816235897Smav			path->device->protocol = PROTO_SEMB;
817235897Smav			PROBE_SET_ACTION(softc, PROBE_IDENTIFY_SES);
818198708Smav		} else if (sign == 0xeb14 &&
819198708Smav		    done_ccb->ccb_h.target_id != 15) {
820198708Smav			path->device->protocol = PROTO_SCSI;
821198708Smav			PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
822198708Smav		} else {
823198708Smav			if (done_ccb->ccb_h.target_id != 15) {
824198708Smav				xpt_print(path,
825198708Smav				    "Unexpected signature 0x%04x\n", sign);
826195534Sscottl			}
827198708Smav			goto device_fail;
828198708Smav		}
829198708Smav		xpt_release_ccb(done_ccb);
830198708Smav		xpt_schedule(periph, priority);
831249466Smav		goto out;
832198708Smav	}
833198708Smav	case PROBE_IDENTIFY:
834198708Smav	{
835207222Smav		struct ccb_pathinq cpi;
836198708Smav		int16_t *ptr;
837195534Sscottl
838203385Smav		ident_buf = &softc->ident_data;
839198708Smav		for (ptr = (int16_t *)ident_buf;
840198708Smav		     ptr < (int16_t *)ident_buf + sizeof(struct ata_params)/2; ptr++) {
841198708Smav			*ptr = le16toh(*ptr);
842198708Smav		}
843198708Smav		if (strncmp(ident_buf->model, "FX", 2) &&
844198708Smav		    strncmp(ident_buf->model, "NEC", 3) &&
845198708Smav		    strncmp(ident_buf->model, "Pioneer", 7) &&
846198708Smav		    strncmp(ident_buf->model, "SHARP", 5)) {
847198708Smav			ata_bswap(ident_buf->model, sizeof(ident_buf->model));
848198708Smav			ata_bswap(ident_buf->revision, sizeof(ident_buf->revision));
849198708Smav			ata_bswap(ident_buf->serial, sizeof(ident_buf->serial));
850198708Smav		}
851198708Smav		ata_btrim(ident_buf->model, sizeof(ident_buf->model));
852198708Smav		ata_bpack(ident_buf->model, ident_buf->model, sizeof(ident_buf->model));
853198708Smav		ata_btrim(ident_buf->revision, sizeof(ident_buf->revision));
854198708Smav		ata_bpack(ident_buf->revision, ident_buf->revision, sizeof(ident_buf->revision));
855198708Smav		ata_btrim(ident_buf->serial, sizeof(ident_buf->serial));
856198708Smav		ata_bpack(ident_buf->serial, ident_buf->serial, sizeof(ident_buf->serial));
857203421Smav		/* Device may need spin-up before IDENTIFY become valid. */
858204354Smav		if ((ident_buf->specconf == 0x37c8 ||
859204354Smav		     ident_buf->specconf == 0x738c) &&
860204354Smav		    ((ident_buf->config & ATA_RESP_INCOMPLETE) ||
861204354Smav		     softc->spinup == 0)) {
862203421Smav			PROBE_SET_ACTION(softc, PROBE_SPINUP);
863203421Smav			xpt_release_ccb(done_ccb);
864203421Smav			xpt_schedule(periph, priority);
865249466Smav			goto out;
866203421Smav		}
867203385Smav		ident_buf = &path->device->ident_data;
868198708Smav		if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
869198708Smav			/* Check that it is the same device. */
870203385Smav			if (bcmp(softc->ident_data.model, ident_buf->model,
871203385Smav			     sizeof(ident_buf->model)) ||
872203385Smav			    bcmp(softc->ident_data.revision, ident_buf->revision,
873203385Smav			     sizeof(ident_buf->revision)) ||
874203385Smav			    bcmp(softc->ident_data.serial, ident_buf->serial,
875203385Smav			     sizeof(ident_buf->serial))) {
876198708Smav				/* Device changed. */
877198708Smav				xpt_async(AC_LOST_DEVICE, path, NULL);
878207282Smav			} else {
879203385Smav				bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
880207282Smav				changed = 0;
881207282Smav			}
882207282Smav		}
883207282Smav		if (changed) {
884203385Smav			bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
885195534Sscottl			/* Clean up from previous instance of this device */
886195534Sscottl			if (path->device->serial_num != NULL) {
887195534Sscottl				free(path->device->serial_num, M_CAMXPT);
888195534Sscottl				path->device->serial_num = NULL;
889195534Sscottl				path->device->serial_num_len = 0;
890195534Sscottl			}
891235897Smav			if (path->device->device_id != NULL) {
892235897Smav				free(path->device->device_id, M_CAMXPT);
893235897Smav				path->device->device_id = NULL;
894235897Smav				path->device->device_id_len = 0;
895235897Smav			}
896195534Sscottl			path->device->serial_num =
897195534Sscottl				(u_int8_t *)malloc((sizeof(ident_buf->serial) + 1),
898198708Smav					   M_CAMXPT, M_NOWAIT);
899195534Sscottl			if (path->device->serial_num != NULL) {
900195534Sscottl				bcopy(ident_buf->serial,
901195534Sscottl				      path->device->serial_num,
902195534Sscottl				      sizeof(ident_buf->serial));
903195534Sscottl				path->device->serial_num[sizeof(ident_buf->serial)]
904195534Sscottl				    = '\0';
905195534Sscottl				path->device->serial_num_len =
906195534Sscottl				    strlen(path->device->serial_num);
907195534Sscottl			}
908235897Smav			if (ident_buf->enabled.extension &
909235897Smav			    ATA_SUPPORT_64BITWWN) {
910235897Smav				path->device->device_id =
911235897Smav				    malloc(16, M_CAMXPT, M_NOWAIT);
912235897Smav				if (path->device->device_id != NULL) {
913235897Smav					path->device->device_id_len = 16;
914235897Smav					bcopy(&fake_device_id_hdr,
915235897Smav					    path->device->device_id, 8);
916250301Smav					bcopy(ident_buf->wwn,
917250301Smav					    path->device->device_id + 8, 8);
918250301Smav					ata_bswap(path->device->device_id + 8, 8);
919235897Smav				}
920235897Smav			}
921195534Sscottl
922198331Smav			path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
923236393Smav			xpt_async(AC_GETDEV_CHANGED, path, NULL);
924195534Sscottl		}
925199178Smav		if (ident_buf->satacapabilities & ATA_SUPPORT_NCQ) {
926236234Smav			path->device->mintags = 2;
927236234Smav			path->device->maxtags =
928199178Smav			    ATA_QUEUE_LEN(ident_buf->queue) + 1;
929199178Smav		}
930199178Smav		ata_find_quirk(path->device);
931199263Smav		if (path->device->mintags != 0 &&
932199263Smav		    path->bus->sim->max_tagged_dev_openings != 0) {
933207222Smav			/* Check if the SIM does not want queued commands. */
934207222Smav			bzero(&cpi, sizeof(cpi));
935207222Smav			xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
936207222Smav			cpi.ccb_h.func_code = XPT_PATH_INQ;
937207222Smav			xpt_action((union ccb *)&cpi);
938207222Smav			if (cpi.ccb_h.status == CAM_REQ_CMP &&
939207222Smav			    (cpi.hba_inquiry & PI_TAG_ABLE)) {
940207222Smav				/* Report SIM which tags are allowed. */
941207222Smav				bzero(&cts, sizeof(cts));
942207222Smav				xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
943207222Smav				cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
944207222Smav				cts.type = CTS_TYPE_CURRENT_SETTINGS;
945207222Smav				cts.xport_specific.sata.tags = path->device->maxtags;
946207222Smav				cts.xport_specific.sata.valid = CTS_SATA_VALID_TAGS;
947207222Smav				xpt_action((union ccb *)&cts);
948207222Smav			}
949199178Smav		}
950236666Smav		ata_device_transport(path);
951236437Smav		if (changed)
952236437Smav			proberequestdefaultnegotiation(periph);
953198708Smav		PROBE_SET_ACTION(softc, PROBE_SETMODE);
954195534Sscottl		xpt_release_ccb(done_ccb);
955198708Smav		xpt_schedule(periph, priority);
956249466Smav		goto out;
957195534Sscottl	}
958203421Smav	case PROBE_SPINUP:
959203421Smav		if (bootverbose)
960203421Smav			xpt_print(path, "Spin-up done\n");
961203421Smav		softc->spinup = 1;
962203421Smav		PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
963203421Smav		xpt_release_ccb(done_ccb);
964203421Smav		xpt_schedule(periph, priority);
965249466Smav		goto out;
966195534Sscottl	case PROBE_SETMODE:
967207499Smav		/* Set supported bits. */
968207499Smav		bzero(&cts, sizeof(cts));
969207499Smav		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
970207499Smav		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
971207499Smav		cts.type = CTS_TYPE_CURRENT_SETTINGS;
972207499Smav		xpt_action((union ccb *)&cts);
973249199Smarius		if (path->device->transport == XPORT_SATA &&
974249199Smarius		    cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
975207499Smav			caps = cts.xport_specific.sata.caps & CTS_SATA_CAPS_H;
976249199Smarius		else if (path->device->transport == XPORT_ATA &&
977249199Smarius		    cts.xport_specific.ata.valid & CTS_ATA_VALID_CAPS)
978249199Smarius			caps = cts.xport_specific.ata.caps & CTS_ATA_CAPS_H;
979207499Smav		else
980207499Smav			caps = 0;
981249199Smarius		if (path->device->transport == XPORT_SATA &&
982249199Smarius		    ident_buf->satacapabilities != 0xffff) {
983207499Smav			if (ident_buf->satacapabilities & ATA_SUPPORT_IFPWRMNGTRCV)
984207499Smav				caps |= CTS_SATA_CAPS_D_PMREQ;
985207499Smav			if (ident_buf->satacapabilities & ATA_SUPPORT_HAPST)
986207499Smav				caps |= CTS_SATA_CAPS_D_APST;
987207499Smav		}
988207499Smav		/* Mask unwanted bits. */
989207499Smav		bzero(&cts, sizeof(cts));
990207499Smav		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
991207499Smav		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
992207499Smav		cts.type = CTS_TYPE_USER_SETTINGS;
993207499Smav		xpt_action((union ccb *)&cts);
994249199Smarius		if (path->device->transport == XPORT_SATA &&
995249199Smarius		    cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
996207499Smav			caps &= cts.xport_specific.sata.caps;
997249199Smarius		else if (path->device->transport == XPORT_ATA &&
998249199Smarius		    cts.xport_specific.ata.valid & CTS_ATA_VALID_CAPS)
999249199Smarius			caps &= cts.xport_specific.ata.caps;
1000215454Smav		else
1001215454Smav			caps = 0;
1002249199Smarius		/*
1003249199Smarius		 * Remember what transport thinks about 48-bit DMA.  If
1004249199Smarius		 * capability information is not provided or transport is
1005249199Smarius		 * SATA, we take support for granted.
1006249199Smarius		 */
1007249199Smarius		if (!(path->device->inq_flags & SID_DMA) ||
1008249199Smarius		    (path->device->transport == XPORT_ATA &&
1009249199Smarius		    (cts.xport_specific.ata.valid & CTS_ATA_VALID_CAPS) &&
1010249199Smarius		    !(caps & CTS_ATA_CAPS_H_DMA48)))
1011249199Smarius			path->device->inq_flags &= ~SID_DMA48;
1012249199Smarius		else
1013249199Smarius			path->device->inq_flags |= SID_DMA48;
1014207499Smav		/* Store result to SIM. */
1015207499Smav		bzero(&cts, sizeof(cts));
1016207499Smav		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1017207499Smav		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1018207499Smav		cts.type = CTS_TYPE_CURRENT_SETTINGS;
1019249199Smarius		if (path->device->transport == XPORT_SATA) {
1020249199Smarius			cts.xport_specific.sata.caps = caps;
1021249199Smarius			cts.xport_specific.sata.valid = CTS_SATA_VALID_CAPS;
1022249199Smarius		} else {
1023249199Smarius			cts.xport_specific.ata.caps = caps;
1024249199Smarius			cts.xport_specific.ata.valid = CTS_ATA_VALID_CAPS;
1025249199Smarius		}
1026207499Smav		xpt_action((union ccb *)&cts);
1027207499Smav		softc->caps = caps;
1028249199Smarius		if (path->device->transport != XPORT_SATA)
1029249199Smarius			goto notsata;
1030217874Smav		if ((ident_buf->satasupport & ATA_SUPPORT_IFPWRMNGT) &&
1031217874Smav		    (!(softc->caps & CTS_SATA_CAPS_H_PMREQ)) !=
1032217874Smav		    (!(ident_buf->sataenabled & ATA_SUPPORT_IFPWRMNGT))) {
1033207499Smav			PROBE_SET_ACTION(softc, PROBE_SETPM);
1034207499Smav			xpt_release_ccb(done_ccb);
1035207499Smav			xpt_schedule(periph, priority);
1036249466Smav			goto out;
1037207499Smav		}
1038207499Smav		/* FALLTHROUGH */
1039207499Smav	case PROBE_SETPM:
1040207499Smav		if (ident_buf->satacapabilities != 0xffff &&
1041217874Smav		    (ident_buf->satacapabilities & ATA_SUPPORT_DAPST) &&
1042217874Smav		    (!(softc->caps & CTS_SATA_CAPS_H_APST)) !=
1043217874Smav		    (!(ident_buf->sataenabled & ATA_ENABLED_DAPST))) {
1044207499Smav			PROBE_SET_ACTION(softc, PROBE_SETAPST);
1045207499Smav			xpt_release_ccb(done_ccb);
1046207499Smav			xpt_schedule(periph, priority);
1047249466Smav			goto out;
1048207499Smav		}
1049207499Smav		/* FALLTHROUGH */
1050207499Smav	case PROBE_SETAPST:
1051217874Smav		if ((ident_buf->satasupport & ATA_SUPPORT_AUTOACTIVATE) &&
1052217874Smav		    (!(softc->caps & CTS_SATA_CAPS_H_DMAAA)) !=
1053217874Smav		    (!(ident_buf->sataenabled & ATA_SUPPORT_AUTOACTIVATE))) {
1054207499Smav			PROBE_SET_ACTION(softc, PROBE_SETDMAAA);
1055207499Smav			xpt_release_ccb(done_ccb);
1056207499Smav			xpt_schedule(periph, priority);
1057249466Smav			goto out;
1058207499Smav		}
1059207499Smav		/* FALLTHROUGH */
1060207499Smav	case PROBE_SETDMAAA:
1061220602Smav		if ((ident_buf->satasupport & ATA_SUPPORT_ASYNCNOTIF) &&
1062220602Smav		    (!(softc->caps & CTS_SATA_CAPS_H_AN)) !=
1063220602Smav		    (!(ident_buf->sataenabled & ATA_SUPPORT_ASYNCNOTIF))) {
1064220602Smav			PROBE_SET_ACTION(softc, PROBE_SETAN);
1065220602Smav			xpt_release_ccb(done_ccb);
1066220602Smav			xpt_schedule(periph, priority);
1067249466Smav			goto out;
1068220602Smav		}
1069220602Smav		/* FALLTHROUGH */
1070220602Smav	case PROBE_SETAN:
1071207499Smavnotsata:
1072198708Smav		if (path->device->protocol == PROTO_ATA) {
1073198708Smav			PROBE_SET_ACTION(softc, PROBE_SET_MULTI);
1074198708Smav		} else {
1075198708Smav			PROBE_SET_ACTION(softc, PROBE_INQUIRY);
1076195534Sscottl		}
1077198708Smav		xpt_release_ccb(done_ccb);
1078198708Smav		xpt_schedule(periph, priority);
1079249466Smav		goto out;
1080198708Smav	case PROBE_SET_MULTI:
1081198708Smav		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1082198708Smav			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1083198748Smav			xpt_acquire_device(path->device);
1084198708Smav			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1085198708Smav			xpt_action(done_ccb);
1086249466Smav			xpt_async(AC_FOUND_DEVICE, path, done_ccb);
1087198708Smav		}
1088236613Smav		PROBE_SET_ACTION(softc, PROBE_DONE);
1089198708Smav		break;
1090195534Sscottl	case PROBE_INQUIRY:
1091195534Sscottl	case PROBE_FULL_INQUIRY:
1092195534Sscottl	{
1093198708Smav		u_int8_t periph_qual, len;
1094195534Sscottl
1095198708Smav		path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
1096195534Sscottl
1097198708Smav		periph_qual = SID_QUAL(inq_buf);
1098195534Sscottl
1099198708Smav		if (periph_qual != SID_QUAL_LU_CONNECTED)
1100198708Smav			break;
1101195534Sscottl
1102198708Smav		/*
1103198708Smav		 * We conservatively request only
1104198708Smav		 * SHORT_INQUIRY_LEN bytes of inquiry
1105198708Smav		 * information during our first try
1106198708Smav		 * at sending an INQUIRY. If the device
1107198708Smav		 * has more information to give,
1108198708Smav		 * perform a second request specifying
1109198708Smav		 * the amount of information the device
1110198708Smav		 * is willing to give.
1111198708Smav		 */
1112198708Smav		len = inq_buf->additional_length
1113198708Smav		    + offsetof(struct scsi_inquiry_data, additional_length) + 1;
1114198708Smav		if (softc->action == PROBE_INQUIRY
1115198708Smav		    && len > SHORT_INQUIRY_LENGTH) {
1116198708Smav			PROBE_SET_ACTION(softc, PROBE_FULL_INQUIRY);
1117198708Smav			xpt_release_ccb(done_ccb);
1118198708Smav			xpt_schedule(periph, priority);
1119249466Smav			goto out;
1120195534Sscottl		}
1121198708Smav
1122198708Smav		ata_device_transport(path);
1123198708Smav		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1124198708Smav			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1125198748Smav			xpt_acquire_device(path->device);
1126198708Smav			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1127198708Smav			xpt_action(done_ccb);
1128249466Smav			xpt_async(AC_FOUND_DEVICE, path, done_ccb);
1129198708Smav		}
1130236613Smav		PROBE_SET_ACTION(softc, PROBE_DONE);
1131198708Smav		break;
1132195534Sscottl	}
1133195534Sscottl	case PROBE_PM_PID:
1134198708Smav		if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) == 0)
1135198708Smav			bzero(ident_buf, sizeof(*ident_buf));
1136198708Smav		softc->pm_pid = (done_ccb->ataio.res.lba_high << 24) +
1137198708Smav		    (done_ccb->ataio.res.lba_mid << 16) +
1138198708Smav		    (done_ccb->ataio.res.lba_low << 8) +
1139198708Smav		    done_ccb->ataio.res.sector_count;
1140198708Smav		((uint32_t *)ident_buf)[0] = softc->pm_pid;
1141198708Smav		snprintf(ident_buf->model, sizeof(ident_buf->model),
1142198708Smav		    "Port Multiplier %08x", softc->pm_pid);
1143198708Smav		PROBE_SET_ACTION(softc, PROBE_PM_PRV);
1144198708Smav		xpt_release_ccb(done_ccb);
1145198708Smav		xpt_schedule(periph, priority);
1146249466Smav		goto out;
1147195534Sscottl	case PROBE_PM_PRV:
1148198708Smav		softc->pm_prv = (done_ccb->ataio.res.lba_high << 24) +
1149198708Smav		    (done_ccb->ataio.res.lba_mid << 16) +
1150198708Smav		    (done_ccb->ataio.res.lba_low << 8) +
1151198708Smav		    done_ccb->ataio.res.sector_count;
1152198708Smav		((uint32_t *)ident_buf)[1] = softc->pm_prv;
1153198708Smav		snprintf(ident_buf->revision, sizeof(ident_buf->revision),
1154198708Smav		    "%04x", softc->pm_prv);
1155198708Smav		path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
1156236666Smav		ata_device_transport(path);
1157236666Smav		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED)
1158236666Smav			proberequestdefaultnegotiation(periph);
1159207499Smav		/* Set supported bits. */
1160207499Smav		bzero(&cts, sizeof(cts));
1161207499Smav		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1162207499Smav		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1163207499Smav		cts.type = CTS_TYPE_CURRENT_SETTINGS;
1164207499Smav		xpt_action((union ccb *)&cts);
1165207499Smav		if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1166207499Smav			caps = cts.xport_specific.sata.caps & CTS_SATA_CAPS_H;
1167207499Smav		else
1168207499Smav			caps = 0;
1169207499Smav		/* All PMPs must support PM requests. */
1170207499Smav		caps |= CTS_SATA_CAPS_D_PMREQ;
1171207499Smav		/* Mask unwanted bits. */
1172207499Smav		bzero(&cts, sizeof(cts));
1173207499Smav		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1174207499Smav		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1175207499Smav		cts.type = CTS_TYPE_USER_SETTINGS;
1176207499Smav		xpt_action((union ccb *)&cts);
1177207499Smav		if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1178207499Smav			caps &= cts.xport_specific.sata.caps;
1179215454Smav		else
1180215454Smav			caps = 0;
1181249199Smarius		/* Remember what transport thinks about AEN. */
1182249199Smarius		if (caps & CTS_SATA_CAPS_H_AN)
1183249199Smarius			path->device->inq_flags |= SID_AEN;
1184249199Smarius		else
1185249199Smarius			path->device->inq_flags &= ~SID_AEN;
1186207499Smav		/* Store result to SIM. */
1187207499Smav		bzero(&cts, sizeof(cts));
1188207499Smav		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1189207499Smav		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1190207499Smav		cts.type = CTS_TYPE_CURRENT_SETTINGS;
1191207499Smav		cts.xport_specific.sata.caps = caps;
1192207499Smav		cts.xport_specific.sata.valid = CTS_SATA_VALID_CAPS;
1193207499Smav		xpt_action((union ccb *)&cts);
1194207499Smav		softc->caps = caps;
1195238886Smav		xpt_async(AC_GETDEV_CHANGED, path, NULL);
1196198708Smav		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1197198708Smav			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1198198748Smav			xpt_acquire_device(path->device);
1199198708Smav			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1200198708Smav			xpt_action(done_ccb);
1201249466Smav			xpt_async(AC_FOUND_DEVICE, path, done_ccb);
1202198708Smav		} else {
1203198708Smav			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1204198708Smav			xpt_action(done_ccb);
1205249466Smav			xpt_async(AC_SCSI_AEN, path, done_ccb);
1206195534Sscottl		}
1207236613Smav		PROBE_SET_ACTION(softc, PROBE_DONE);
1208198708Smav		break;
1209235897Smav	case PROBE_IDENTIFY_SES:
1210235897Smav	case PROBE_IDENTIFY_SAFTE:
1211235897Smav		if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
1212235897Smav			/* Check that it is the same device. */
1213235897Smav			if (bcmp(&softc->ident_data, ident_buf, 53)) {
1214235897Smav				/* Device changed. */
1215235897Smav				xpt_async(AC_LOST_DEVICE, path, NULL);
1216235897Smav			} else {
1217235897Smav				bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
1218235897Smav				changed = 0;
1219235897Smav			}
1220235897Smav		}
1221235897Smav		if (changed) {
1222235897Smav			bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
1223235897Smav			/* Clean up from previous instance of this device */
1224235897Smav			if (path->device->device_id != NULL) {
1225235897Smav				free(path->device->device_id, M_CAMXPT);
1226235897Smav				path->device->device_id = NULL;
1227235897Smav				path->device->device_id_len = 0;
1228235897Smav			}
1229235897Smav			path->device->device_id =
1230235897Smav			    malloc(16, M_CAMXPT, M_NOWAIT);
1231235897Smav			if (path->device->device_id != NULL) {
1232235897Smav				path->device->device_id_len = 16;
1233235897Smav				bcopy(&fake_device_id_hdr,
1234235897Smav				    path->device->device_id, 8);
1235235897Smav				bcopy(((uint8_t*)ident_buf) + 2,
1236235897Smav				    path->device->device_id + 8, 8);
1237235897Smav			}
1238235897Smav
1239235897Smav			path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
1240235897Smav		}
1241236666Smav		ata_device_transport(path);
1242236666Smav		if (changed)
1243236666Smav			proberequestdefaultnegotiation(periph);
1244235897Smav
1245235897Smav		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1246235897Smav			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1247235897Smav			xpt_acquire_device(path->device);
1248235897Smav			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1249235897Smav			xpt_action(done_ccb);
1250249466Smav			xpt_async(AC_FOUND_DEVICE, path, done_ccb);
1251235897Smav		}
1252236613Smav		PROBE_SET_ACTION(softc, PROBE_DONE);
1253235897Smav		break;
1254195534Sscottl	default:
1255236613Smav		panic("probedone: invalid action state 0x%x\n", softc->action);
1256195534Sscottl	}
1257198708Smavdone:
1258203108Smav	if (softc->restart) {
1259203108Smav		softc->restart = 0;
1260203108Smav		xpt_release_ccb(done_ccb);
1261195534Sscottl		probeschedule(periph);
1262249466Smav		goto out;
1263195534Sscottl	}
1264203108Smav	xpt_release_ccb(done_ccb);
1265236613Smav	CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe completed\n"));
1266203108Smav	while ((done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs))) {
1267203108Smav		TAILQ_REMOVE(&softc->request_ccbs,
1268203108Smav		    &done_ccb->ccb_h, periph_links.tqe);
1269203108Smav		done_ccb->ccb_h.status = found ? CAM_REQ_CMP : CAM_REQ_CMP_ERR;
1270203108Smav		xpt_done(done_ccb);
1271203108Smav	}
1272249466Smav	/* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
1273249466Smav	cam_release_devq(path, 0, 0, 0, FALSE);
1274236228Smav	cam_periph_invalidate(periph);
1275203108Smav	cam_periph_release_locked(periph);
1276195534Sscottl}
1277195534Sscottl
1278195534Sscottlstatic void
1279195534Sscottlprobecleanup(struct cam_periph *periph)
1280195534Sscottl{
1281195534Sscottl	free(periph->softc, M_CAMXPT);
1282195534Sscottl}
1283195534Sscottl
1284195534Sscottlstatic void
1285199178Smavata_find_quirk(struct cam_ed *device)
1286195534Sscottl{
1287199178Smav	struct ata_quirk_entry *quirk;
1288195534Sscottl	caddr_t	match;
1289195534Sscottl
1290199178Smav	match = cam_quirkmatch((caddr_t)&device->ident_data,
1291199178Smav			       (caddr_t)ata_quirk_table,
1292199178Smav			       ata_quirk_table_size,
1293199178Smav			       sizeof(*ata_quirk_table), ata_identify_match);
1294195534Sscottl
1295195534Sscottl	if (match == NULL)
1296195534Sscottl		panic("xpt_find_quirk: device didn't match wildcard entry!!");
1297195534Sscottl
1298199178Smav	quirk = (struct ata_quirk_entry *)match;
1299195534Sscottl	device->quirk = quirk;
1300236234Smav	if (quirk->quirks & CAM_QUIRK_MAXTAGS) {
1301236234Smav		device->mintags = quirk->mintags;
1302236234Smav		device->maxtags = quirk->maxtags;
1303236234Smav	}
1304195534Sscottl}
1305195534Sscottl
1306195534Sscottltypedef struct {
1307195534Sscottl	union	ccb *request_ccb;
1308195534Sscottl	struct 	ccb_pathinq *cpi;
1309195534Sscottl	int	counter;
1310195534Sscottl} ata_scan_bus_info;
1311195534Sscottl
1312195534Sscottl/*
1313195534Sscottl * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
1314195534Sscottl * As the scan progresses, xpt_scan_bus is used as the
1315195534Sscottl * callback on completion function.
1316195534Sscottl */
1317195534Sscottlstatic void
1318195534Sscottlata_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
1319195534Sscottl{
1320195534Sscottl	struct	cam_path *path;
1321195534Sscottl	ata_scan_bus_info *scan_info;
1322203108Smav	union	ccb *work_ccb, *reset_ccb;
1323195534Sscottl	cam_status status;
1324195534Sscottl
1325195534Sscottl	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
1326195534Sscottl		  ("xpt_scan_bus\n"));
1327195534Sscottl	switch (request_ccb->ccb_h.func_code) {
1328195534Sscottl	case XPT_SCAN_BUS:
1329208582Smjacob	case XPT_SCAN_TGT:
1330195534Sscottl		/* Find out the characteristics of the bus */
1331195534Sscottl		work_ccb = xpt_alloc_ccb_nowait();
1332195534Sscottl		if (work_ccb == NULL) {
1333195534Sscottl			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1334195534Sscottl			xpt_done(request_ccb);
1335195534Sscottl			return;
1336195534Sscottl		}
1337195534Sscottl		xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
1338195534Sscottl			      request_ccb->ccb_h.pinfo.priority);
1339195534Sscottl		work_ccb->ccb_h.func_code = XPT_PATH_INQ;
1340195534Sscottl		xpt_action(work_ccb);
1341195534Sscottl		if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
1342195534Sscottl			request_ccb->ccb_h.status = work_ccb->ccb_h.status;
1343195534Sscottl			xpt_free_ccb(work_ccb);
1344195534Sscottl			xpt_done(request_ccb);
1345195534Sscottl			return;
1346195534Sscottl		}
1347195534Sscottl
1348203108Smav		/* We may need to reset bus first, if we haven't done it yet. */
1349203108Smav		if ((work_ccb->cpi.hba_inquiry &
1350203108Smav		    (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) &&
1351203108Smav		    !(work_ccb->cpi.hba_misc & PIM_NOBUSRESET) &&
1352203108Smav		    !timevalisset(&request_ccb->ccb_h.path->bus->last_reset)) {
1353203108Smav			reset_ccb = xpt_alloc_ccb_nowait();
1354208823Smav			if (reset_ccb == NULL) {
1355208823Smav				request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1356208823Smav				xpt_free_ccb(work_ccb);
1357208823Smav				xpt_done(request_ccb);
1358208823Smav				return;
1359208823Smav			}
1360203108Smav			xpt_setup_ccb(&reset_ccb->ccb_h, request_ccb->ccb_h.path,
1361203108Smav			      CAM_PRIORITY_NONE);
1362203108Smav			reset_ccb->ccb_h.func_code = XPT_RESET_BUS;
1363203108Smav			xpt_action(reset_ccb);
1364203108Smav			if (reset_ccb->ccb_h.status != CAM_REQ_CMP) {
1365203108Smav				request_ccb->ccb_h.status = reset_ccb->ccb_h.status;
1366203108Smav				xpt_free_ccb(reset_ccb);
1367203108Smav				xpt_free_ccb(work_ccb);
1368203108Smav				xpt_done(request_ccb);
1369203108Smav				return;
1370203108Smav			}
1371203108Smav			xpt_free_ccb(reset_ccb);
1372203108Smav		}
1373203108Smav
1374195534Sscottl		/* Save some state for use while we probe for devices */
1375195534Sscottl		scan_info = (ata_scan_bus_info *)
1376195534Sscottl		    malloc(sizeof(ata_scan_bus_info), M_CAMXPT, M_NOWAIT);
1377195534Sscottl		if (scan_info == NULL) {
1378195534Sscottl			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1379208823Smav			xpt_free_ccb(work_ccb);
1380195534Sscottl			xpt_done(request_ccb);
1381195534Sscottl			return;
1382195534Sscottl		}
1383195534Sscottl		scan_info->request_ccb = request_ccb;
1384195534Sscottl		scan_info->cpi = &work_ccb->cpi;
1385195534Sscottl		/* If PM supported, probe it first. */
1386195534Sscottl		if (scan_info->cpi->hba_inquiry & PI_SATAPM)
1387201990Smav			scan_info->counter = scan_info->cpi->max_target;
1388201990Smav		else
1389201990Smav			scan_info->counter = 0;
1390195534Sscottl
1391195534Sscottl		work_ccb = xpt_alloc_ccb_nowait();
1392195534Sscottl		if (work_ccb == NULL) {
1393195534Sscottl			free(scan_info, M_CAMXPT);
1394195534Sscottl			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1395195534Sscottl			xpt_done(request_ccb);
1396195534Sscottl			break;
1397195534Sscottl		}
1398195534Sscottl		goto scan_next;
1399195534Sscottl	case XPT_SCAN_LUN:
1400195534Sscottl		work_ccb = request_ccb;
1401195534Sscottl		/* Reuse the same CCB to query if a device was really found */
1402195534Sscottl		scan_info = (ata_scan_bus_info *)work_ccb->ccb_h.ppriv_ptr0;
1403198389Smav		/* If there is PMP... */
1404201990Smav		if ((scan_info->cpi->hba_inquiry & PI_SATAPM) &&
1405201990Smav		    (scan_info->counter == scan_info->cpi->max_target)) {
1406203108Smav			if (work_ccb->ccb_h.status == CAM_REQ_CMP) {
1407207428Smav				/* everything else will be probed by it */
1408207428Smav				/* Free the current request path- we're done with it. */
1409207428Smav				xpt_free_path(work_ccb->ccb_h.path);
1410201990Smav				goto done;
1411195534Sscottl			} else {
1412195534Sscottl				struct ccb_trans_settings cts;
1413195534Sscottl
1414195534Sscottl				/* Report SIM that PM is absent. */
1415195534Sscottl				bzero(&cts, sizeof(cts));
1416195534Sscottl				xpt_setup_ccb(&cts.ccb_h,
1417207428Smav				    work_ccb->ccb_h.path, CAM_PRIORITY_NONE);
1418195534Sscottl				cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1419195534Sscottl				cts.type = CTS_TYPE_CURRENT_SETTINGS;
1420195665Smav				cts.xport_specific.sata.pm_present = 0;
1421195534Sscottl				cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
1422195534Sscottl				xpt_action((union ccb *)&cts);
1423195534Sscottl			}
1424195534Sscottl		}
1425207428Smav		/* Free the current request path- we're done with it. */
1426207428Smav		xpt_free_path(work_ccb->ccb_h.path);
1427201990Smav		if (scan_info->counter ==
1428201990Smav		    ((scan_info->cpi->hba_inquiry & PI_SATAPM) ?
1429201990Smav		    0 : scan_info->cpi->max_target)) {
1430201990Smavdone:
1431195534Sscottl			xpt_free_ccb(work_ccb);
1432195534Sscottl			xpt_free_ccb((union ccb *)scan_info->cpi);
1433195534Sscottl			request_ccb = scan_info->request_ccb;
1434195534Sscottl			free(scan_info, M_CAMXPT);
1435195534Sscottl			request_ccb->ccb_h.status = CAM_REQ_CMP;
1436195534Sscottl			xpt_done(request_ccb);
1437195534Sscottl			break;
1438195534Sscottl		}
1439201990Smav		/* Take next device. Wrap from max (PMP) to 0. */
1440201990Smav		scan_info->counter = (scan_info->counter + 1 ) %
1441201990Smav		    (scan_info->cpi->max_target + 1);
1442195534Sscottlscan_next:
1443249468Smav		status = xpt_create_path(&path, NULL,
1444195534Sscottl		    scan_info->request_ccb->ccb_h.path_id,
1445195534Sscottl		    scan_info->counter, 0);
1446195534Sscottl		if (status != CAM_REQ_CMP) {
1447195534Sscottl			printf("xpt_scan_bus: xpt_create_path failed"
1448195534Sscottl			    " with status %#x, bus scan halted\n",
1449195534Sscottl			    status);
1450195534Sscottl			xpt_free_ccb(work_ccb);
1451195534Sscottl			xpt_free_ccb((union ccb *)scan_info->cpi);
1452195534Sscottl			request_ccb = scan_info->request_ccb;
1453195534Sscottl			free(scan_info, M_CAMXPT);
1454195534Sscottl			request_ccb->ccb_h.status = status;
1455195534Sscottl			xpt_done(request_ccb);
1456195534Sscottl			break;
1457195534Sscottl		}
1458195534Sscottl		xpt_setup_ccb(&work_ccb->ccb_h, path,
1459195534Sscottl		    scan_info->request_ccb->ccb_h.pinfo.priority);
1460195534Sscottl		work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1461195534Sscottl		work_ccb->ccb_h.cbfcnp = ata_scan_bus;
1462195534Sscottl		work_ccb->ccb_h.ppriv_ptr0 = scan_info;
1463195534Sscottl		work_ccb->crcn.flags = scan_info->request_ccb->crcn.flags;
1464195534Sscottl		xpt_action(work_ccb);
1465195534Sscottl		break;
1466195534Sscottl	default:
1467195534Sscottl		break;
1468195534Sscottl	}
1469195534Sscottl}
1470195534Sscottl
1471195534Sscottlstatic void
1472195534Sscottlata_scan_lun(struct cam_periph *periph, struct cam_path *path,
1473195534Sscottl	     cam_flags flags, union ccb *request_ccb)
1474195534Sscottl{
1475195534Sscottl	struct ccb_pathinq cpi;
1476195534Sscottl	cam_status status;
1477195534Sscottl	struct cam_path *new_path;
1478195534Sscottl	struct cam_periph *old_periph;
1479195534Sscottl
1480203108Smav	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_scan_lun\n"));
1481195534Sscottl
1482203108Smav	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
1483195534Sscottl	cpi.ccb_h.func_code = XPT_PATH_INQ;
1484195534Sscottl	xpt_action((union ccb *)&cpi);
1485195534Sscottl
1486195534Sscottl	if (cpi.ccb_h.status != CAM_REQ_CMP) {
1487195534Sscottl		if (request_ccb != NULL) {
1488195534Sscottl			request_ccb->ccb_h.status = cpi.ccb_h.status;
1489195534Sscottl			xpt_done(request_ccb);
1490195534Sscottl		}
1491195534Sscottl		return;
1492195534Sscottl	}
1493195534Sscottl
1494195534Sscottl	if (request_ccb == NULL) {
1495241455Smav		request_ccb = xpt_alloc_ccb_nowait();
1496195534Sscottl		if (request_ccb == NULL) {
1497195534Sscottl			xpt_print(path, "xpt_scan_lun: can't allocate CCB, "
1498195534Sscottl			    "can't continue\n");
1499195534Sscottl			return;
1500195534Sscottl		}
1501249468Smav		status = xpt_create_path(&new_path, NULL,
1502195534Sscottl					  path->bus->path_id,
1503195534Sscottl					  path->target->target_id,
1504195534Sscottl					  path->device->lun_id);
1505195534Sscottl		if (status != CAM_REQ_CMP) {
1506241455Smav			xpt_print(path, "xpt_scan_lun: can't create path, "
1507195534Sscottl			    "can't continue\n");
1508241455Smav			xpt_free_ccb(request_ccb);
1509195534Sscottl			return;
1510195534Sscottl		}
1511203108Smav		xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_XPT);
1512195534Sscottl		request_ccb->ccb_h.cbfcnp = xptscandone;
1513195534Sscottl		request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1514195534Sscottl		request_ccb->crcn.flags = flags;
1515195534Sscottl	}
1516195534Sscottl
1517195653Smav	if ((old_periph = cam_periph_find(path, "aprobe")) != NULL) {
1518236228Smav		if ((old_periph->flags & CAM_PERIPH_INVALID) == 0) {
1519236228Smav			probe_softc *softc;
1520195534Sscottl
1521236228Smav			softc = (probe_softc *)old_periph->softc;
1522236228Smav			TAILQ_INSERT_TAIL(&softc->request_ccbs,
1523236228Smav				&request_ccb->ccb_h, periph_links.tqe);
1524236228Smav			softc->restart = 1;
1525236228Smav		} else {
1526236228Smav			request_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1527236228Smav			xpt_done(request_ccb);
1528236228Smav		}
1529195534Sscottl	} else {
1530195534Sscottl		status = cam_periph_alloc(proberegister, NULL, probecleanup,
1531195653Smav					  probestart, "aprobe",
1532195534Sscottl					  CAM_PERIPH_BIO,
1533195534Sscottl					  request_ccb->ccb_h.path, NULL, 0,
1534195534Sscottl					  request_ccb);
1535195534Sscottl
1536195534Sscottl		if (status != CAM_REQ_CMP) {
1537195534Sscottl			xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
1538195534Sscottl			    "returned an error, can't continue probe\n");
1539195534Sscottl			request_ccb->ccb_h.status = status;
1540195534Sscottl			xpt_done(request_ccb);
1541195534Sscottl		}
1542195534Sscottl	}
1543195534Sscottl}
1544195534Sscottl
1545195534Sscottlstatic void
1546195534Sscottlxptscandone(struct cam_periph *periph, union ccb *done_ccb)
1547195534Sscottl{
1548241455Smav
1549241455Smav	xpt_free_path(done_ccb->ccb_h.path);
1550241455Smav	xpt_free_ccb(done_ccb);
1551195534Sscottl}
1552195534Sscottl
1553195534Sscottlstatic struct cam_ed *
1554195534Sscottlata_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
1555195534Sscottl{
1556195534Sscottl	struct cam_path path;
1557199178Smav	struct ata_quirk_entry *quirk;
1558195534Sscottl	struct cam_ed *device;
1559195534Sscottl
1560195534Sscottl	device = xpt_alloc_device(bus, target, lun_id);
1561195534Sscottl	if (device == NULL)
1562195534Sscottl		return (NULL);
1563195534Sscottl
1564195534Sscottl	/*
1565195534Sscottl	 * Take the default quirk entry until we have inquiry
1566195534Sscottl	 * data and can determine a better quirk to use.
1567195534Sscottl	 */
1568199178Smav	quirk = &ata_quirk_table[ata_quirk_table_size - 1];
1569195534Sscottl	device->quirk = (void *)quirk;
1570199178Smav	device->mintags = 0;
1571199178Smav	device->maxtags = 0;
1572195534Sscottl	bzero(&device->inq_data, sizeof(device->inq_data));
1573195534Sscottl	device->inq_flags = 0;
1574195534Sscottl	device->queue_flags = 0;
1575195534Sscottl	device->serial_num = NULL;
1576195534Sscottl	device->serial_num_len = 0;
1577195534Sscottl
1578195534Sscottl	/*
1579195534Sscottl	 * XXX should be limited by number of CCBs this bus can
1580195534Sscottl	 * do.
1581195534Sscottl	 */
1582195534Sscottl	bus->sim->max_ccbs += device->ccbq.devq_openings;
1583195534Sscottl	if (lun_id != CAM_LUN_WILDCARD) {
1584195534Sscottl		xpt_compile_path(&path,
1585195534Sscottl				 NULL,
1586195534Sscottl				 bus->path_id,
1587195534Sscottl				 target->target_id,
1588195534Sscottl				 lun_id);
1589195534Sscottl		ata_device_transport(&path);
1590195534Sscottl		xpt_release_path(&path);
1591195534Sscottl	}
1592195534Sscottl
1593195534Sscottl	return (device);
1594195534Sscottl}
1595195534Sscottl
1596195534Sscottlstatic void
1597195534Sscottlata_device_transport(struct cam_path *path)
1598195534Sscottl{
1599195534Sscottl	struct ccb_pathinq cpi;
1600198331Smav	struct ccb_trans_settings cts;
1601198331Smav	struct scsi_inquiry_data *inq_buf = NULL;
1602198331Smav	struct ata_params *ident_buf = NULL;
1603195534Sscottl
1604195534Sscottl	/* Get transport information from the SIM */
1605203108Smav	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
1606195534Sscottl	cpi.ccb_h.func_code = XPT_PATH_INQ;
1607195534Sscottl	xpt_action((union ccb *)&cpi);
1608195534Sscottl
1609195534Sscottl	path->device->transport = cpi.transport;
1610198331Smav	if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
1611198331Smav		inq_buf = &path->device->inq_data;
1612198331Smav	if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) != 0)
1613198331Smav		ident_buf = &path->device->ident_data;
1614198331Smav	if (path->device->protocol == PROTO_ATA) {
1615198331Smav		path->device->protocol_version = ident_buf ?
1616198331Smav		    ata_version(ident_buf->version_major) : cpi.protocol_version;
1617198331Smav	} else if (path->device->protocol == PROTO_SCSI) {
1618198331Smav		path->device->protocol_version = inq_buf ?
1619198331Smav		    SID_ANSI_REV(inq_buf) : cpi.protocol_version;
1620195534Sscottl	}
1621198331Smav	path->device->transport_version = ident_buf ?
1622198331Smav	    ata_version(ident_buf->version_major) : cpi.transport_version;
1623195534Sscottl
1624195534Sscottl	/* Tell the controller what we think */
1625203108Smav	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1626195534Sscottl	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1627195534Sscottl	cts.type = CTS_TYPE_CURRENT_SETTINGS;
1628195534Sscottl	cts.transport = path->device->transport;
1629195534Sscottl	cts.transport_version = path->device->transport_version;
1630195534Sscottl	cts.protocol = path->device->protocol;
1631195534Sscottl	cts.protocol_version = path->device->protocol_version;
1632195534Sscottl	cts.proto_specific.valid = 0;
1633203376Smav	if (ident_buf) {
1634203376Smav		if (path->device->transport == XPORT_ATA) {
1635223019Smav			cts.xport_specific.ata.atapi =
1636223019Smav			    (ident_buf->config == ATA_PROTO_CFA) ? 0 :
1637203376Smav			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 :
1638203376Smav			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0;
1639203376Smav			cts.xport_specific.ata.valid = CTS_ATA_VALID_ATAPI;
1640203376Smav		} else {
1641223019Smav			cts.xport_specific.sata.atapi =
1642223019Smav			    (ident_buf->config == ATA_PROTO_CFA) ? 0 :
1643203376Smav			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 :
1644203376Smav			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0;
1645203376Smav			cts.xport_specific.sata.valid = CTS_SATA_VALID_ATAPI;
1646203376Smav		}
1647203376Smav	} else
1648203376Smav		cts.xport_specific.valid = 0;
1649195534Sscottl	xpt_action((union ccb *)&cts);
1650195534Sscottl}
1651195534Sscottl
1652195534Sscottlstatic void
1653223443Swillata_dev_advinfo(union ccb *start_ccb)
1654223443Swill{
1655223443Swill	struct cam_ed *device;
1656223443Swill	struct ccb_dev_advinfo *cdai;
1657223443Swill	off_t amt;
1658223443Swill
1659223443Swill	start_ccb->ccb_h.status = CAM_REQ_INVALID;
1660223443Swill	device = start_ccb->ccb_h.path->device;
1661223443Swill	cdai = &start_ccb->cdai;
1662223443Swill	switch(cdai->buftype) {
1663235897Smav	case CDAI_TYPE_SCSI_DEVID:
1664235897Smav		if (cdai->flags & CDAI_FLAG_STORE)
1665235897Smav			return;
1666235897Smav		cdai->provsiz = device->device_id_len;
1667235897Smav		if (device->device_id_len == 0)
1668235897Smav			break;
1669235897Smav		amt = device->device_id_len;
1670235897Smav		if (cdai->provsiz > cdai->bufsiz)
1671235897Smav			amt = cdai->bufsiz;
1672235897Smav		memcpy(cdai->buf, device->device_id, amt);
1673235897Smav		break;
1674223443Swill	case CDAI_TYPE_SERIAL_NUM:
1675223443Swill		if (cdai->flags & CDAI_FLAG_STORE)
1676235897Smav			return;
1677223443Swill		cdai->provsiz = device->serial_num_len;
1678223443Swill		if (device->serial_num_len == 0)
1679223443Swill			break;
1680223443Swill		amt = device->serial_num_len;
1681223443Swill		if (cdai->provsiz > cdai->bufsiz)
1682223443Swill			amt = cdai->bufsiz;
1683223443Swill		memcpy(cdai->buf, device->serial_num, amt);
1684223443Swill		break;
1685235897Smav	case CDAI_TYPE_PHYS_PATH:
1686235897Smav		if (cdai->flags & CDAI_FLAG_STORE) {
1687235897Smav			if (device->physpath != NULL)
1688235897Smav				free(device->physpath, M_CAMXPT);
1689235897Smav			device->physpath_len = cdai->bufsiz;
1690235897Smav			/* Clear existing buffer if zero length */
1691235897Smav			if (cdai->bufsiz == 0)
1692235897Smav				break;
1693235897Smav			device->physpath = malloc(cdai->bufsiz, M_CAMXPT, M_NOWAIT);
1694235897Smav			if (device->physpath == NULL) {
1695235897Smav				start_ccb->ccb_h.status = CAM_REQ_ABORTED;
1696235897Smav				return;
1697235897Smav			}
1698235897Smav			memcpy(device->physpath, cdai->buf, cdai->bufsiz);
1699235897Smav		} else {
1700235897Smav			cdai->provsiz = device->physpath_len;
1701235897Smav			if (device->physpath_len == 0)
1702235897Smav				break;
1703235897Smav			amt = device->physpath_len;
1704235897Smav			if (cdai->provsiz > cdai->bufsiz)
1705235897Smav				amt = cdai->bufsiz;
1706235897Smav			memcpy(cdai->buf, device->physpath, amt);
1707235897Smav		}
1708235897Smav		break;
1709223443Swill	default:
1710235897Smav		return;
1711223443Swill	}
1712235897Smav	start_ccb->ccb_h.status = CAM_REQ_CMP;
1713235897Smav
1714235897Smav	if (cdai->flags & CDAI_FLAG_STORE) {
1715235897Smav		int owned;
1716235897Smav
1717235897Smav		owned = mtx_owned(start_ccb->ccb_h.path->bus->sim->mtx);
1718235897Smav		if (owned == 0)
1719235897Smav			mtx_lock(start_ccb->ccb_h.path->bus->sim->mtx);
1720235897Smav		xpt_async(AC_ADVINFO_CHANGED, start_ccb->ccb_h.path,
1721235897Smav			  (void *)(uintptr_t)cdai->buftype);
1722235897Smav		if (owned == 0)
1723235897Smav			mtx_unlock(start_ccb->ccb_h.path->bus->sim->mtx);
1724235897Smav	}
1725223443Swill}
1726223443Swill
1727223443Swillstatic void
1728195534Sscottlata_action(union ccb *start_ccb)
1729195534Sscottl{
1730195534Sscottl
1731195534Sscottl	switch (start_ccb->ccb_h.func_code) {
1732195534Sscottl	case XPT_SET_TRAN_SETTINGS:
1733195534Sscottl	{
1734199178Smav		ata_set_transfer_settings(&start_ccb->cts,
1735195534Sscottl					   start_ccb->ccb_h.path->device,
1736195534Sscottl					   /*async_update*/FALSE);
1737195534Sscottl		break;
1738195534Sscottl	}
1739195534Sscottl	case XPT_SCAN_BUS:
1740208582Smjacob	case XPT_SCAN_TGT:
1741195534Sscottl		ata_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
1742195534Sscottl		break;
1743195534Sscottl	case XPT_SCAN_LUN:
1744195534Sscottl		ata_scan_lun(start_ccb->ccb_h.path->periph,
1745195534Sscottl			      start_ccb->ccb_h.path, start_ccb->crcn.flags,
1746195534Sscottl			      start_ccb);
1747195534Sscottl		break;
1748195534Sscottl	case XPT_GET_TRAN_SETTINGS:
1749195534Sscottl	{
1750236437Smav		ata_get_transfer_settings(&start_ccb->cts);
1751195534Sscottl		break;
1752195534Sscottl	}
1753203376Smav	case XPT_SCSI_IO:
1754203376Smav	{
1755203376Smav		struct cam_ed *device;
1756203376Smav		u_int	maxlen = 0;
1757203376Smav
1758203376Smav		device = start_ccb->ccb_h.path->device;
1759203376Smav		if (device->protocol == PROTO_SCSI &&
1760203376Smav		    (device->flags & CAM_DEV_IDENTIFY_DATA_VALID)) {
1761203376Smav			uint16_t p =
1762203376Smav			    device->ident_data.config & ATA_PROTO_MASK;
1763203376Smav
1764223019Smav			maxlen =
1765223019Smav			    (device->ident_data.config == ATA_PROTO_CFA) ? 0 :
1766223019Smav			    (p == ATA_PROTO_ATAPI_16) ? 16 :
1767203376Smav			    (p == ATA_PROTO_ATAPI_12) ? 12 : 0;
1768203376Smav		}
1769203376Smav		if (start_ccb->csio.cdb_len > maxlen) {
1770203376Smav			start_ccb->ccb_h.status = CAM_REQ_INVALID;
1771203376Smav			xpt_done(start_ccb);
1772203376Smav			break;
1773203376Smav		}
1774223475Smav		xpt_action_default(start_ccb);
1775223475Smav		break;
1776203376Smav	}
1777223443Swill	case XPT_DEV_ADVINFO:
1778223443Swill	{
1779223443Swill		ata_dev_advinfo(start_ccb);
1780223443Swill		break;
1781223443Swill	}
1782195534Sscottl	default:
1783195534Sscottl		xpt_action_default(start_ccb);
1784195534Sscottl		break;
1785195534Sscottl	}
1786195534Sscottl}
1787195534Sscottl
1788195534Sscottlstatic void
1789236437Smavata_get_transfer_settings(struct ccb_trans_settings *cts)
1790236437Smav{
1791236437Smav	struct	ccb_trans_settings_ata *ata;
1792236437Smav	struct	ccb_trans_settings_scsi *scsi;
1793236437Smav	struct	cam_ed *device;
1794236437Smav	struct	cam_sim *sim;
1795236437Smav
1796236437Smav	device = cts->ccb_h.path->device;
1797236437Smav	sim = cts->ccb_h.path->bus->sim;
1798236437Smav	(*(sim->sim_action))(sim, (union ccb *)cts);
1799236437Smav
1800236666Smav	if (cts->protocol == PROTO_UNKNOWN ||
1801236666Smav	    cts->protocol == PROTO_UNSPECIFIED) {
1802236666Smav		cts->protocol = device->protocol;
1803236666Smav		cts->protocol_version = device->protocol_version;
1804236666Smav	}
1805236666Smav
1806236437Smav	if (cts->protocol == PROTO_ATA) {
1807236437Smav		ata = &cts->proto_specific.ata;
1808236437Smav		if ((ata->valid & CTS_ATA_VALID_TQ) == 0) {
1809236437Smav			ata->valid |= CTS_ATA_VALID_TQ;
1810236437Smav			if (cts->type == CTS_TYPE_USER_SETTINGS ||
1811236437Smav			    (device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0 ||
1812236437Smav			    (device->inq_flags & SID_CmdQue) != 0)
1813236437Smav				ata->flags |= CTS_ATA_FLAGS_TAG_ENB;
1814236437Smav		}
1815236437Smav	}
1816236437Smav	if (cts->protocol == PROTO_SCSI) {
1817236437Smav		scsi = &cts->proto_specific.scsi;
1818236437Smav		if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
1819236437Smav			scsi->valid |= CTS_SCSI_VALID_TQ;
1820236437Smav			if (cts->type == CTS_TYPE_USER_SETTINGS ||
1821236437Smav			    (device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0 ||
1822236437Smav			    (device->inq_flags & SID_CmdQue) != 0)
1823236437Smav				scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
1824236437Smav		}
1825236437Smav	}
1826236666Smav
1827236666Smav	if (cts->transport == XPORT_UNKNOWN ||
1828236666Smav	    cts->transport == XPORT_UNSPECIFIED) {
1829236666Smav		cts->transport = device->transport;
1830236666Smav		cts->transport_version = device->transport_version;
1831236666Smav	}
1832236437Smav}
1833236437Smav
1834236437Smavstatic void
1835199178Smavata_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
1836195534Sscottl			   int async_update)
1837195534Sscottl{
1838195534Sscottl	struct	ccb_pathinq cpi;
1839236437Smav	struct	ccb_trans_settings_ata *ata;
1840195534Sscottl	struct	ccb_trans_settings_scsi *scsi;
1841195534Sscottl	struct	cam_sim *sim;
1842236437Smav	struct	ata_params *ident_data;
1843195534Sscottl	struct	scsi_inquiry_data *inq_data;
1844195534Sscottl
1845195534Sscottl	if (device == NULL) {
1846195534Sscottl		cts->ccb_h.status = CAM_PATH_INVALID;
1847195534Sscottl		xpt_done((union ccb *)cts);
1848195534Sscottl		return;
1849195534Sscottl	}
1850195534Sscottl
1851195534Sscottl	if (cts->protocol == PROTO_UNKNOWN
1852195534Sscottl	 || cts->protocol == PROTO_UNSPECIFIED) {
1853195534Sscottl		cts->protocol = device->protocol;
1854195534Sscottl		cts->protocol_version = device->protocol_version;
1855195534Sscottl	}
1856195534Sscottl
1857195534Sscottl	if (cts->protocol_version == PROTO_VERSION_UNKNOWN
1858195534Sscottl	 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
1859195534Sscottl		cts->protocol_version = device->protocol_version;
1860195534Sscottl
1861195534Sscottl	if (cts->protocol != device->protocol) {
1862195534Sscottl		xpt_print(cts->ccb_h.path, "Uninitialized Protocol %x:%x?\n",
1863195534Sscottl		       cts->protocol, device->protocol);
1864195534Sscottl		cts->protocol = device->protocol;
1865195534Sscottl	}
1866195534Sscottl
1867195534Sscottl	if (cts->protocol_version > device->protocol_version) {
1868195534Sscottl		if (bootverbose) {
1869195534Sscottl			xpt_print(cts->ccb_h.path, "Down reving Protocol "
1870195534Sscottl			    "Version from %d to %d?\n", cts->protocol_version,
1871195534Sscottl			    device->protocol_version);
1872195534Sscottl		}
1873195534Sscottl		cts->protocol_version = device->protocol_version;
1874195534Sscottl	}
1875195534Sscottl
1876195534Sscottl	if (cts->transport == XPORT_UNKNOWN
1877195534Sscottl	 || cts->transport == XPORT_UNSPECIFIED) {
1878195534Sscottl		cts->transport = device->transport;
1879195534Sscottl		cts->transport_version = device->transport_version;
1880195534Sscottl	}
1881195534Sscottl
1882195534Sscottl	if (cts->transport_version == XPORT_VERSION_UNKNOWN
1883195534Sscottl	 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
1884195534Sscottl		cts->transport_version = device->transport_version;
1885195534Sscottl
1886195534Sscottl	if (cts->transport != device->transport) {
1887195534Sscottl		xpt_print(cts->ccb_h.path, "Uninitialized Transport %x:%x?\n",
1888195534Sscottl		    cts->transport, device->transport);
1889195534Sscottl		cts->transport = device->transport;
1890195534Sscottl	}
1891195534Sscottl
1892195534Sscottl	if (cts->transport_version > device->transport_version) {
1893195534Sscottl		if (bootverbose) {
1894195534Sscottl			xpt_print(cts->ccb_h.path, "Down reving Transport "
1895195534Sscottl			    "Version from %d to %d?\n", cts->transport_version,
1896195534Sscottl			    device->transport_version);
1897195534Sscottl		}
1898195534Sscottl		cts->transport_version = device->transport_version;
1899195534Sscottl	}
1900195534Sscottl
1901195534Sscottl	sim = cts->ccb_h.path->bus->sim;
1902236437Smav	ident_data = &device->ident_data;
1903195534Sscottl	inq_data = &device->inq_data;
1904236437Smav	if (cts->protocol == PROTO_ATA)
1905236437Smav		ata = &cts->proto_specific.ata;
1906236437Smav	else
1907236437Smav		ata = NULL;
1908236437Smav	if (cts->protocol == PROTO_SCSI)
1909236437Smav		scsi = &cts->proto_specific.scsi;
1910236437Smav	else
1911236437Smav		scsi = NULL;
1912203108Smav	xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NONE);
1913195534Sscottl	cpi.ccb_h.func_code = XPT_PATH_INQ;
1914195534Sscottl	xpt_action((union ccb *)&cpi);
1915195534Sscottl
1916236437Smav	/* Sanity checking */
1917195534Sscottl	if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
1918236437Smav	 || (ata && (ident_data->satacapabilities & ATA_SUPPORT_NCQ) == 0)
1919236437Smav	 || (scsi && (INQ_DATA_TQ_ENABLED(inq_data)) == 0)
1920195534Sscottl	 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
1921195534Sscottl	 || (device->mintags == 0)) {
1922195534Sscottl		/*
1923195534Sscottl		 * Can't tag on hardware that doesn't support tags,
1924195534Sscottl		 * doesn't have it enabled, or has broken tag support.
1925195534Sscottl		 */
1926236437Smav		if (ata)
1927236437Smav			ata->flags &= ~CTS_ATA_FLAGS_TAG_ENB;
1928236437Smav		if (scsi)
1929195534Sscottl			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1930195534Sscottl	}
1931195534Sscottl
1932236437Smav	/* Start/stop tags use. */
1933236437Smav	if (cts->type == CTS_TYPE_CURRENT_SETTINGS &&
1934236437Smav	    ((ata && (ata->valid & CTS_ATA_VALID_TQ) != 0) ||
1935236437Smav	     (scsi && (scsi->valid & CTS_SCSI_VALID_TQ) != 0))) {
1936236437Smav		int nowt, newt = 0;
1937195534Sscottl
1938236437Smav		nowt = ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0 ||
1939236437Smav			(device->inq_flags & SID_CmdQue) != 0);
1940236437Smav		if (ata)
1941236437Smav			newt = (ata->flags & CTS_ATA_FLAGS_TAG_ENB) != 0;
1942236437Smav		if (scsi)
1943236437Smav			newt = (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0;
1944195534Sscottl
1945236437Smav		if (newt && !nowt) {
1946236437Smav			/*
1947236437Smav			 * Delay change to use tags until after a
1948236437Smav			 * few commands have gone to this device so
1949236437Smav			 * the controller has time to perform transfer
1950236437Smav			 * negotiations without tagged messages getting
1951236437Smav			 * in the way.
1952236437Smav			 */
1953236437Smav			device->tag_delay_count = CAM_TAG_DELAY_COUNT;
1954236437Smav			device->flags |= CAM_DEV_TAG_AFTER_COUNT;
1955236437Smav		} else if (nowt && !newt)
1956236437Smav			xpt_stop_tags(cts->ccb_h.path);
1957236437Smav	}
1958195534Sscottl
1959195534Sscottl	if (async_update == FALSE)
1960195534Sscottl		(*(sim->sim_action))(sim, (union ccb *)cts);
1961195534Sscottl}
1962195534Sscottl
1963195534Sscottl/*
1964195534Sscottl * Handle any per-device event notifications that require action by the XPT.
1965195534Sscottl */
1966195534Sscottlstatic void
1967195534Sscottlata_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
1968195534Sscottl	      struct cam_ed *device, void *async_arg)
1969195534Sscottl{
1970195534Sscottl	cam_status status;
1971195534Sscottl	struct cam_path newpath;
1972195534Sscottl
1973195534Sscottl	/*
1974195534Sscottl	 * We only need to handle events for real devices.
1975195534Sscottl	 */
1976195534Sscottl	if (target->target_id == CAM_TARGET_WILDCARD
1977195534Sscottl	 || device->lun_id == CAM_LUN_WILDCARD)
1978195534Sscottl		return;
1979195534Sscottl
1980195534Sscottl	/*
1981195534Sscottl	 * We need our own path with wildcards expanded to
1982195534Sscottl	 * handle certain types of events.
1983195534Sscottl	 */
1984195534Sscottl	if ((async_code == AC_SENT_BDR)
1985195534Sscottl	 || (async_code == AC_BUS_RESET)
1986195534Sscottl	 || (async_code == AC_INQ_CHANGED))
1987195534Sscottl		status = xpt_compile_path(&newpath, NULL,
1988195534Sscottl					  bus->path_id,
1989195534Sscottl					  target->target_id,
1990195534Sscottl					  device->lun_id);
1991195534Sscottl	else
1992195534Sscottl		status = CAM_REQ_CMP_ERR;
1993195534Sscottl
1994195534Sscottl	if (status == CAM_REQ_CMP) {
1995195534Sscottl		if (async_code == AC_INQ_CHANGED) {
1996195534Sscottl			/*
1997195534Sscottl			 * We've sent a start unit command, or
1998195534Sscottl			 * something similar to a device that
1999195534Sscottl			 * may have caused its inquiry data to
2000195534Sscottl			 * change. So we re-scan the device to
2001195534Sscottl			 * refresh the inquiry data for it.
2002195534Sscottl			 */
2003195534Sscottl			ata_scan_lun(newpath.periph, &newpath,
2004195534Sscottl				     CAM_EXPECT_INQ_CHANGE, NULL);
2005203108Smav		} else {
2006203108Smav			/* We need to reinitialize device after reset. */
2007203108Smav			ata_scan_lun(newpath.periph, &newpath,
2008203108Smav				     0, NULL);
2009195534Sscottl		}
2010195534Sscottl		xpt_release_path(&newpath);
2011198748Smav	} else if (async_code == AC_LOST_DEVICE &&
2012198748Smav	    (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
2013195534Sscottl		device->flags |= CAM_DEV_UNCONFIGURED;
2014198748Smav		xpt_release_device(device);
2015195534Sscottl	} else if (async_code == AC_TRANSFER_NEG) {
2016195534Sscottl		struct ccb_trans_settings *settings;
2017195534Sscottl
2018195534Sscottl		settings = (struct ccb_trans_settings *)async_arg;
2019199178Smav		ata_set_transfer_settings(settings, device,
2020195534Sscottl					  /*async_update*/TRUE);
2021195534Sscottl	}
2022195534Sscottl}
2023195534Sscottl
2024204220Smavstatic void
2025204220Smavata_announce_periph(struct cam_periph *periph)
2026204220Smav{
2027204220Smav	struct	ccb_pathinq cpi;
2028204220Smav	struct	ccb_trans_settings cts;
2029204220Smav	struct	cam_path *path = periph->path;
2030204220Smav	u_int	speed;
2031204220Smav	u_int	mb;
2032204220Smav
2033204220Smav	mtx_assert(periph->sim->mtx, MA_OWNED);
2034204220Smav
2035204220Smav	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
2036204220Smav	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
2037204220Smav	cts.type = CTS_TYPE_CURRENT_SETTINGS;
2038204220Smav	xpt_action((union ccb*)&cts);
2039204220Smav	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
2040204220Smav		return;
2041204220Smav	/* Ask the SIM for its base transfer speed */
2042204220Smav	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
2043204220Smav	cpi.ccb_h.func_code = XPT_PATH_INQ;
2044204220Smav	xpt_action((union ccb *)&cpi);
2045204220Smav	/* Report connection speed */
2046204220Smav	speed = cpi.base_transfer_speed;
2047204220Smav	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_ATA) {
2048236437Smav		struct	ccb_trans_settings_pata *pata =
2049204220Smav		    &cts.xport_specific.ata;
2050204220Smav
2051236437Smav		if (pata->valid & CTS_ATA_VALID_MODE)
2052236437Smav			speed = ata_mode2speed(pata->mode);
2053204220Smav	}
2054204220Smav	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SATA) {
2055204220Smav		struct	ccb_trans_settings_sata *sata =
2056204220Smav		    &cts.xport_specific.sata;
2057204220Smav
2058204220Smav		if (sata->valid & CTS_SATA_VALID_REVISION)
2059204220Smav			speed = ata_revision2speed(sata->revision);
2060204220Smav	}
2061204220Smav	mb = speed / 1000;
2062204220Smav	if (mb > 0)
2063204220Smav		printf("%s%d: %d.%03dMB/s transfers",
2064204220Smav		       periph->periph_name, periph->unit_number,
2065204220Smav		       mb, speed % 1000);
2066204220Smav	else
2067204220Smav		printf("%s%d: %dKB/s transfers", periph->periph_name,
2068204220Smav		       periph->unit_number, speed);
2069204220Smav	/* Report additional information about connection */
2070204220Smav	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_ATA) {
2071236437Smav		struct ccb_trans_settings_pata *pata =
2072204220Smav		    &cts.xport_specific.ata;
2073204220Smav
2074204220Smav		printf(" (");
2075236437Smav		if (pata->valid & CTS_ATA_VALID_MODE)
2076236437Smav			printf("%s, ", ata_mode2string(pata->mode));
2077236437Smav		if ((pata->valid & CTS_ATA_VALID_ATAPI) && pata->atapi != 0)
2078236437Smav			printf("ATAPI %dbytes, ", pata->atapi);
2079236437Smav		if (pata->valid & CTS_ATA_VALID_BYTECOUNT)
2080236437Smav			printf("PIO %dbytes", pata->bytecount);
2081204220Smav		printf(")");
2082204220Smav	}
2083204220Smav	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SATA) {
2084204220Smav		struct ccb_trans_settings_sata *sata =
2085204220Smav		    &cts.xport_specific.sata;
2086204220Smav
2087204220Smav		printf(" (");
2088204220Smav		if (sata->valid & CTS_SATA_VALID_REVISION)
2089204220Smav			printf("SATA %d.x, ", sata->revision);
2090204220Smav		else
2091204220Smav			printf("SATA, ");
2092204220Smav		if (sata->valid & CTS_SATA_VALID_MODE)
2093204220Smav			printf("%s, ", ata_mode2string(sata->mode));
2094204220Smav		if ((sata->valid & CTS_ATA_VALID_ATAPI) && sata->atapi != 0)
2095204220Smav			printf("ATAPI %dbytes, ", sata->atapi);
2096204220Smav		if (sata->valid & CTS_SATA_VALID_BYTECOUNT)
2097204220Smav			printf("PIO %dbytes", sata->bytecount);
2098204220Smav		printf(")");
2099204220Smav	}
2100204220Smav	printf("\n");
2101204220Smav}
2102