ata_xpt.c revision 217444
1195534Sscottl/*-
2195534Sscottl * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org>
3195534Sscottl * All rights reserved.
4195534Sscottl *
5195534Sscottl * Redistribution and use in source and binary forms, with or without
6195534Sscottl * modification, are permitted provided that the following conditions
7195534Sscottl * are met:
8195534Sscottl * 1. Redistributions of source code must retain the above copyright
9195534Sscottl *    notice, this list of conditions and the following disclaimer,
10195534Sscottl *    without modification, immediately at the beginning of the file.
11195534Sscottl * 2. Redistributions in binary form must reproduce the above copyright
12195534Sscottl *    notice, this list of conditions and the following disclaimer in the
13195534Sscottl *    documentation and/or other materials provided with the distribution.
14195534Sscottl *
15195534Sscottl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16195534Sscottl * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17195534Sscottl * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18195534Sscottl * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19195534Sscottl * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20195534Sscottl * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21195534Sscottl * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22195534Sscottl * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23195534Sscottl * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24195534Sscottl * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25195534Sscottl */
26195534Sscottl
27195534Sscottl#include <sys/cdefs.h>
28195534Sscottl__FBSDID("$FreeBSD: head/sys/cam/ata/ata_xpt.c 217444 2011-01-15 09:43:25Z mav $");
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
68195534Sscottl	u_int maxtags;
69195534Sscottl};
70195534Sscottl
71195534Sscottlstatic periph_init_t probe_periph_init;
72195534Sscottl
73195534Sscottlstatic struct periph_driver probe_driver =
74195534Sscottl{
75195653Smav	probe_periph_init, "aprobe",
76198708Smav	TAILQ_HEAD_INITIALIZER(probe_driver.units), /* generation */ 0,
77198708Smav	CAM_PERIPH_DRV_EARLY
78195534Sscottl};
79195534Sscottl
80195653SmavPERIPHDRIVER_DECLARE(aprobe, probe_driver);
81195534Sscottl
82195534Sscottltypedef enum {
83195534Sscottl	PROBE_RESET,
84195534Sscottl	PROBE_IDENTIFY,
85203421Smav	PROBE_SPINUP,
86195534Sscottl	PROBE_SETMODE,
87207499Smav	PROBE_SETPM,
88207499Smav	PROBE_SETAPST,
89207499Smav	PROBE_SETDMAAA,
90198708Smav	PROBE_SET_MULTI,
91195534Sscottl	PROBE_INQUIRY,
92195534Sscottl	PROBE_FULL_INQUIRY,
93195534Sscottl	PROBE_PM_PID,
94195534Sscottl	PROBE_PM_PRV,
95195534Sscottl	PROBE_INVALID
96195534Sscottl} probe_action;
97195534Sscottl
98195534Sscottlstatic char *probe_action_text[] = {
99195534Sscottl	"PROBE_RESET",
100195534Sscottl	"PROBE_IDENTIFY",
101203421Smav	"PROBE_SPINUP",
102195534Sscottl	"PROBE_SETMODE",
103207499Smav	"PROBE_SETPM",
104207499Smav	"PROBE_SETAPST",
105207499Smav	"PROBE_SETDMAAA",
106198708Smav	"PROBE_SET_MULTI",
107195534Sscottl	"PROBE_INQUIRY",
108195534Sscottl	"PROBE_FULL_INQUIRY",
109195534Sscottl	"PROBE_PM_PID",
110195534Sscottl	"PROBE_PM_PRV",
111195534Sscottl	"PROBE_INVALID"
112195534Sscottl};
113195534Sscottl
114195534Sscottl#define PROBE_SET_ACTION(softc, newaction)	\
115195534Sscottldo {									\
116195534Sscottl	char **text;							\
117195534Sscottl	text = probe_action_text;					\
118195534Sscottl	CAM_DEBUG((softc)->periph->path, CAM_DEBUG_INFO,		\
119195534Sscottl	    ("Probe %s to %s\n", text[(softc)->action],			\
120195534Sscottl	    text[(newaction)]));					\
121195534Sscottl	(softc)->action = (newaction);					\
122195534Sscottl} while(0)
123195534Sscottl
124195534Sscottltypedef enum {
125195534Sscottl	PROBE_NO_ANNOUNCE	= 0x04
126195534Sscottl} probe_flags;
127195534Sscottl
128195534Sscottltypedef struct {
129195534Sscottl	TAILQ_HEAD(, ccb_hdr) request_ccbs;
130203385Smav	struct ata_params	ident_data;
131195534Sscottl	probe_action	action;
132195534Sscottl	probe_flags	flags;
133195534Sscottl	uint32_t	pm_pid;
134195534Sscottl	uint32_t	pm_prv;
135203108Smav	int		restart;
136203421Smav	int		spinup;
137209744Smav	int		faults;
138207499Smav	u_int		caps;
139195534Sscottl	struct cam_periph *periph;
140195534Sscottl} probe_softc;
141195534Sscottl
142199178Smavstatic struct ata_quirk_entry ata_quirk_table[] =
143195534Sscottl{
144195534Sscottl	{
145195534Sscottl		/* Default tagged queuing parameters for all devices */
146195534Sscottl		{
147195534Sscottl		  T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
148195534Sscottl		  /*vendor*/"*", /*product*/"*", /*revision*/"*"
149195534Sscottl		},
150199178Smav		/*quirks*/0, /*maxtags*/0
151195534Sscottl	},
152195534Sscottl};
153195534Sscottl
154199178Smavstatic const int ata_quirk_table_size =
155199178Smav	sizeof(ata_quirk_table) / sizeof(*ata_quirk_table);
156195534Sscottl
157195534Sscottlstatic cam_status	proberegister(struct cam_periph *periph,
158195534Sscottl				      void *arg);
159195534Sscottlstatic void	 probeschedule(struct cam_periph *probe_periph);
160195534Sscottlstatic void	 probestart(struct cam_periph *periph, union ccb *start_ccb);
161195534Sscottl//static void	 proberequestdefaultnegotiation(struct cam_periph *periph);
162195534Sscottl//static int       proberequestbackoff(struct cam_periph *periph,
163195534Sscottl//				     struct cam_ed *device);
164195534Sscottlstatic void	 probedone(struct cam_periph *periph, union ccb *done_ccb);
165195534Sscottlstatic void	 probecleanup(struct cam_periph *periph);
166199178Smavstatic void	 ata_find_quirk(struct cam_ed *device);
167195534Sscottlstatic void	 ata_scan_bus(struct cam_periph *periph, union ccb *ccb);
168195534Sscottlstatic void	 ata_scan_lun(struct cam_periph *periph,
169195534Sscottl			       struct cam_path *path, cam_flags flags,
170195534Sscottl			       union ccb *ccb);
171195534Sscottlstatic void	 xptscandone(struct cam_periph *periph, union ccb *done_ccb);
172195534Sscottlstatic struct cam_ed *
173195534Sscottl		 ata_alloc_device(struct cam_eb *bus, struct cam_et *target,
174195534Sscottl				   lun_id_t lun_id);
175195534Sscottlstatic void	 ata_device_transport(struct cam_path *path);
176199178Smavstatic void	 ata_set_transfer_settings(struct ccb_trans_settings *cts,
177195534Sscottl					    struct cam_ed *device,
178195534Sscottl					    int async_update);
179195534Sscottlstatic void	 ata_dev_async(u_int32_t async_code,
180195534Sscottl				struct cam_eb *bus,
181195534Sscottl				struct cam_et *target,
182195534Sscottl				struct cam_ed *device,
183195534Sscottl				void *async_arg);
184195534Sscottlstatic void	 ata_action(union ccb *start_ccb);
185204220Smavstatic void	 ata_announce_periph(struct cam_periph *periph);
186195534Sscottl
187195534Sscottlstatic struct xpt_xport ata_xport = {
188195534Sscottl	.alloc_device = ata_alloc_device,
189195534Sscottl	.action = ata_action,
190195534Sscottl	.async = ata_dev_async,
191204220Smav	.announce = ata_announce_periph,
192195534Sscottl};
193195534Sscottl
194195534Sscottlstruct xpt_xport *
195195534Sscottlata_get_xport(void)
196195534Sscottl{
197195534Sscottl	return (&ata_xport);
198195534Sscottl}
199195534Sscottl
200195534Sscottlstatic void
201195534Sscottlprobe_periph_init()
202195534Sscottl{
203195534Sscottl}
204195534Sscottl
205195534Sscottlstatic cam_status
206195534Sscottlproberegister(struct cam_periph *periph, void *arg)
207195534Sscottl{
208195534Sscottl	union ccb *request_ccb;	/* CCB representing the probe request */
209195534Sscottl	cam_status status;
210195534Sscottl	probe_softc *softc;
211195534Sscottl
212195534Sscottl	request_ccb = (union ccb *)arg;
213195534Sscottl	if (periph == NULL) {
214195534Sscottl		printf("proberegister: periph was NULL!!\n");
215195534Sscottl		return(CAM_REQ_CMP_ERR);
216195534Sscottl	}
217195534Sscottl
218195534Sscottl	if (request_ccb == NULL) {
219195534Sscottl		printf("proberegister: no probe CCB, "
220195534Sscottl		       "can't register device\n");
221195534Sscottl		return(CAM_REQ_CMP_ERR);
222195534Sscottl	}
223195534Sscottl
224203421Smav	softc = (probe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_ZERO | M_NOWAIT);
225195534Sscottl
226195534Sscottl	if (softc == NULL) {
227195534Sscottl		printf("proberegister: Unable to probe new device. "
228195534Sscottl		       "Unable to allocate softc\n");
229195534Sscottl		return(CAM_REQ_CMP_ERR);
230195534Sscottl	}
231195534Sscottl	TAILQ_INIT(&softc->request_ccbs);
232195534Sscottl	TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
233195534Sscottl			  periph_links.tqe);
234195534Sscottl	softc->flags = 0;
235195534Sscottl	periph->softc = softc;
236195534Sscottl	softc->periph = periph;
237195534Sscottl	softc->action = PROBE_INVALID;
238195534Sscottl	status = cam_periph_acquire(periph);
239195534Sscottl	if (status != CAM_REQ_CMP) {
240195534Sscottl		return (status);
241195534Sscottl	}
242195534Sscottl	/*
243203108Smav	 * Ensure nobody slip in until probe finish.
244195534Sscottl	 */
245203108Smav	cam_freeze_devq_arg(periph->path,
246203108Smav	    RELSIM_RELEASE_RUNLEVEL, CAM_RL_XPT + 1);
247195534Sscottl	probeschedule(periph);
248195534Sscottl	return(CAM_REQ_CMP);
249195534Sscottl}
250195534Sscottl
251195534Sscottlstatic void
252195534Sscottlprobeschedule(struct cam_periph *periph)
253195534Sscottl{
254195534Sscottl	union ccb *ccb;
255195534Sscottl	probe_softc *softc;
256195534Sscottl
257195534Sscottl	softc = (probe_softc *)periph->softc;
258195534Sscottl	ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
259195534Sscottl
260198389Smav	if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) ||
261198389Smav	    periph->path->device->protocol == PROTO_SATAPM)
262195534Sscottl		PROBE_SET_ACTION(softc, PROBE_RESET);
263195534Sscottl	else
264195534Sscottl		PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
265195534Sscottl
266195534Sscottl	if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
267195534Sscottl		softc->flags |= PROBE_NO_ANNOUNCE;
268195534Sscottl	else
269195534Sscottl		softc->flags &= ~PROBE_NO_ANNOUNCE;
270195534Sscottl
271203108Smav	xpt_schedule(periph, CAM_PRIORITY_XPT);
272195534Sscottl}
273195534Sscottl
274195534Sscottlstatic void
275195534Sscottlprobestart(struct cam_periph *periph, union ccb *start_ccb)
276195534Sscottl{
277199747Smav	struct ccb_trans_settings cts;
278195534Sscottl	struct ccb_ataio *ataio;
279195534Sscottl	struct ccb_scsiio *csio;
280195534Sscottl	probe_softc *softc;
281198708Smav	struct cam_path *path;
282198708Smav	struct ata_params *ident_buf;
283195534Sscottl
284195534Sscottl	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
285195534Sscottl
286195534Sscottl	softc = (probe_softc *)periph->softc;
287198708Smav	path = start_ccb->ccb_h.path;
288195534Sscottl	ataio = &start_ccb->ataio;
289195534Sscottl	csio = &start_ccb->csio;
290198708Smav	ident_buf = &periph->path->device->ident_data;
291195534Sscottl
292203108Smav	if (softc->restart) {
293203108Smav		softc->restart = 0;
294203108Smav		if ((path->device->flags & CAM_DEV_UNCONFIGURED) ||
295203108Smav		    path->device->protocol == PROTO_SATAPM)
296203108Smav			softc->action = PROBE_RESET;
297203108Smav		else
298203108Smav			softc->action = PROBE_IDENTIFY;
299203108Smav	}
300195534Sscottl	switch (softc->action) {
301195534Sscottl	case PROBE_RESET:
302195534Sscottl		cam_fill_ataio(ataio,
303195534Sscottl		      0,
304195534Sscottl		      probedone,
305195534Sscottl		      /*flags*/CAM_DIR_NONE,
306198389Smav		      0,
307195534Sscottl		      /*data_ptr*/NULL,
308195534Sscottl		      /*dxfer_len*/0,
309203108Smav		      15 * 1000);
310195534Sscottl		ata_reset_cmd(ataio);
311195534Sscottl		break;
312195534Sscottl	case PROBE_IDENTIFY:
313195534Sscottl		cam_fill_ataio(ataio,
314195534Sscottl		      1,
315195534Sscottl		      probedone,
316195534Sscottl		      /*flags*/CAM_DIR_IN,
317198389Smav		      0,
318203385Smav		      /*data_ptr*/(u_int8_t *)&softc->ident_data,
319203385Smav		      /*dxfer_len*/sizeof(softc->ident_data),
320195534Sscottl		      30 * 1000);
321195534Sscottl		if (periph->path->device->protocol == PROTO_ATA)
322196659Smav			ata_28bit_cmd(ataio, ATA_ATA_IDENTIFY, 0, 0, 0);
323195534Sscottl		else
324196659Smav			ata_28bit_cmd(ataio, ATA_ATAPI_IDENTIFY, 0, 0, 0);
325195534Sscottl		break;
326203421Smav	case PROBE_SPINUP:
327203421Smav		if (bootverbose)
328203421Smav			xpt_print(path, "Spinning up device\n");
329203421Smav		cam_fill_ataio(ataio,
330203421Smav		      1,
331203421Smav		      probedone,
332203421Smav		      /*flags*/CAM_DIR_NONE | CAM_HIGH_POWER,
333203421Smav		      0,
334203421Smav		      /*data_ptr*/NULL,
335203421Smav		      /*dxfer_len*/0,
336203421Smav		      30 * 1000);
337203421Smav		ata_28bit_cmd(ataio, ATA_SETFEATURES, ATA_SF_PUIS_SPINUP, 0, 0);
338203421Smav		break;
339195534Sscottl	case PROBE_SETMODE:
340199747Smav	{
341199747Smav		int mode, wantmode;
342199747Smav
343199747Smav		mode = 0;
344199747Smav		/* Fetch user modes from SIM. */
345199747Smav		bzero(&cts, sizeof(cts));
346203108Smav		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
347199747Smav		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
348199747Smav		cts.type = CTS_TYPE_USER_SETTINGS;
349199747Smav		xpt_action((union ccb *)&cts);
350199747Smav		if (path->device->transport == XPORT_ATA) {
351199747Smav			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_MODE)
352199747Smav				mode = cts.xport_specific.ata.mode;
353199747Smav		} else {
354199799Smav			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_MODE)
355199747Smav				mode = cts.xport_specific.sata.mode;
356199747Smav		}
357199747Smavnegotiate:
358199747Smav		/* Honor device capabilities. */
359199747Smav		wantmode = mode = ata_max_mode(ident_buf, mode);
360199747Smav		/* Report modes to SIM. */
361199747Smav		bzero(&cts, sizeof(cts));
362203108Smav		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
363199747Smav		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
364199747Smav		cts.type = CTS_TYPE_CURRENT_SETTINGS;
365199747Smav		if (path->device->transport == XPORT_ATA) {
366199747Smav			cts.xport_specific.ata.mode = mode;
367199747Smav			cts.xport_specific.ata.valid = CTS_ATA_VALID_MODE;
368199747Smav		} else {
369199747Smav			cts.xport_specific.sata.mode = mode;
370199747Smav			cts.xport_specific.sata.valid = CTS_SATA_VALID_MODE;
371199747Smav		}
372199747Smav		xpt_action((union ccb *)&cts);
373200171Smav		/* Fetch current modes from SIM. */
374199747Smav		bzero(&cts, sizeof(cts));
375203108Smav		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
376199747Smav		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
377199747Smav		cts.type = CTS_TYPE_CURRENT_SETTINGS;
378199747Smav		xpt_action((union ccb *)&cts);
379199747Smav		if (path->device->transport == XPORT_ATA) {
380199747Smav			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_MODE)
381199747Smav				mode = cts.xport_specific.ata.mode;
382199747Smav		} else {
383199747Smav			if (cts.xport_specific.ata.valid & CTS_SATA_VALID_MODE)
384199747Smav				mode = cts.xport_specific.sata.mode;
385199747Smav		}
386199747Smav		/* If SIM disagree - renegotiate. */
387199747Smav		if (mode != wantmode)
388199747Smav			goto negotiate;
389195534Sscottl		cam_fill_ataio(ataio,
390195534Sscottl		      1,
391195534Sscottl		      probedone,
392196353Smav		      /*flags*/CAM_DIR_NONE,
393196353Smav		      0,
394196353Smav		      /*data_ptr*/NULL,
395196353Smav		      /*dxfer_len*/0,
396195534Sscottl		      30 * 1000);
397199747Smav		ata_28bit_cmd(ataio, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
398195534Sscottl		break;
399199747Smav	}
400207499Smav	case PROBE_SETPM:
401207499Smav		cam_fill_ataio(ataio,
402207499Smav		    1,
403207499Smav		    probedone,
404207499Smav		    CAM_DIR_NONE,
405207499Smav		    0,
406207499Smav		    NULL,
407207499Smav		    0,
408207499Smav		    30*1000);
409207499Smav		ata_28bit_cmd(ataio, ATA_SETFEATURES,
410207499Smav		    (softc->caps & CTS_SATA_CAPS_H_PMREQ) ? 0x10 : 0x90,
411207499Smav		    0, 0x03);
412207499Smav		break;
413207499Smav	case PROBE_SETAPST:
414207499Smav		cam_fill_ataio(ataio,
415207499Smav		    1,
416207499Smav		    probedone,
417207499Smav		    CAM_DIR_NONE,
418207499Smav		    0,
419207499Smav		    NULL,
420207499Smav		    0,
421207499Smav		    30*1000);
422207499Smav		ata_28bit_cmd(ataio, ATA_SETFEATURES,
423207499Smav		    (softc->caps & CTS_SATA_CAPS_H_APST) ? 0x10 : 0x90,
424207499Smav		    0, 0x07);
425207499Smav		break;
426207499Smav	case PROBE_SETDMAAA:
427207499Smav		cam_fill_ataio(ataio,
428207499Smav		    1,
429207499Smav		    probedone,
430207499Smav		    CAM_DIR_NONE,
431207499Smav		    0,
432207499Smav		    NULL,
433207499Smav		    0,
434207499Smav		    30*1000);
435207499Smav		ata_28bit_cmd(ataio, ATA_SETFEATURES,
436207499Smav		    (softc->caps & CTS_SATA_CAPS_H_DMAAA) ? 0x10 : 0x90,
437207499Smav		    0, 0x02);
438207499Smav		break;
439198708Smav	case PROBE_SET_MULTI:
440198708Smav	{
441200171Smav		u_int sectors, bytecount;
442198708Smav
443200171Smav		bytecount = 8192;	/* SATA maximum */
444200171Smav		/* Fetch user bytecount from SIM. */
445200171Smav		bzero(&cts, sizeof(cts));
446203108Smav		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
447200171Smav		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
448200171Smav		cts.type = CTS_TYPE_USER_SETTINGS;
449200171Smav		xpt_action((union ccb *)&cts);
450200171Smav		if (path->device->transport == XPORT_ATA) {
451200171Smav			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
452200171Smav				bytecount = cts.xport_specific.ata.bytecount;
453200171Smav		} else {
454200171Smav			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
455200171Smav				bytecount = cts.xport_specific.sata.bytecount;
456200171Smav		}
457200171Smav		/* Honor device capabilities. */
458200171Smav		sectors = max(1, min(ident_buf->sectors_intr & 0xff,
459200171Smav		    bytecount / ata_logical_sector_size(ident_buf)));
460198708Smav		/* Report bytecount to SIM. */
461198708Smav		bzero(&cts, sizeof(cts));
462203108Smav		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
463198708Smav		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
464198708Smav		cts.type = CTS_TYPE_CURRENT_SETTINGS;
465198708Smav		if (path->device->transport == XPORT_ATA) {
466198897Smav			cts.xport_specific.ata.bytecount = sectors *
467198897Smav			    ata_logical_sector_size(ident_buf);
468198708Smav			cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT;
469198708Smav		} else {
470198897Smav			cts.xport_specific.sata.bytecount = sectors *
471198897Smav			    ata_logical_sector_size(ident_buf);
472198708Smav			cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT;
473198708Smav		}
474198708Smav		xpt_action((union ccb *)&cts);
475200171Smav		/* Fetch current bytecount from SIM. */
476200171Smav		bzero(&cts, sizeof(cts));
477203108Smav		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
478200171Smav		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
479200171Smav		cts.type = CTS_TYPE_CURRENT_SETTINGS;
480200171Smav		xpt_action((union ccb *)&cts);
481200171Smav		if (path->device->transport == XPORT_ATA) {
482200171Smav			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
483200171Smav				bytecount = cts.xport_specific.ata.bytecount;
484200171Smav		} else {
485200171Smav			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
486200171Smav				bytecount = cts.xport_specific.sata.bytecount;
487200171Smav		}
488200171Smav		sectors = bytecount / ata_logical_sector_size(ident_buf);
489198708Smav
490198708Smav		cam_fill_ataio(ataio,
491198708Smav		    1,
492198708Smav		    probedone,
493198708Smav		    CAM_DIR_NONE,
494198708Smav		    0,
495198708Smav		    NULL,
496198708Smav		    0,
497198708Smav		    30*1000);
498198708Smav		ata_28bit_cmd(ataio, ATA_SET_MULTI, 0, 0, sectors);
499198708Smav		break;
500195534Sscottl	}
501195534Sscottl	case PROBE_INQUIRY:
502200171Smav	{
503200171Smav		u_int bytecount;
504200171Smav
505200171Smav		bytecount = 8192;	/* SATA maximum */
506200171Smav		/* Fetch user bytecount from SIM. */
507200171Smav		bzero(&cts, sizeof(cts));
508203108Smav		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
509200171Smav		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
510200171Smav		cts.type = CTS_TYPE_USER_SETTINGS;
511200171Smav		xpt_action((union ccb *)&cts);
512200171Smav		if (path->device->transport == XPORT_ATA) {
513200171Smav			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
514200171Smav				bytecount = cts.xport_specific.ata.bytecount;
515200171Smav		} else {
516200171Smav			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
517200171Smav				bytecount = cts.xport_specific.sata.bytecount;
518200171Smav		}
519200171Smav		/* Honor device capabilities. */
520200171Smav		bytecount &= ~1;
521200171Smav		bytecount = max(2, min(65534, bytecount));
522200171Smav		if (ident_buf->satacapabilities != 0x0000 &&
523200171Smav		    ident_buf->satacapabilities != 0xffff) {
524200171Smav			bytecount = min(8192, bytecount);
525200171Smav		}
526200171Smav		/* Report bytecount to SIM. */
527200171Smav		bzero(&cts, sizeof(cts));
528203108Smav		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
529200171Smav		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
530200171Smav		cts.type = CTS_TYPE_CURRENT_SETTINGS;
531200171Smav		if (path->device->transport == XPORT_ATA) {
532200171Smav			cts.xport_specific.ata.bytecount = bytecount;
533200171Smav			cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT;
534200171Smav		} else {
535200171Smav			cts.xport_specific.sata.bytecount = bytecount;
536200171Smav			cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT;
537200171Smav		}
538200171Smav		xpt_action((union ccb *)&cts);
539200171Smav		/* FALLTHROUGH */
540200171Smav	}
541195534Sscottl	case PROBE_FULL_INQUIRY:
542195534Sscottl	{
543195534Sscottl		u_int inquiry_len;
544195534Sscottl		struct scsi_inquiry_data *inq_buf =
545195534Sscottl		    &periph->path->device->inq_data;
546195534Sscottl
547195534Sscottl		if (softc->action == PROBE_INQUIRY)
548195534Sscottl			inquiry_len = SHORT_INQUIRY_LENGTH;
549195534Sscottl		else
550195534Sscottl			inquiry_len = SID_ADDITIONAL_LENGTH(inq_buf);
551195534Sscottl		/*
552195534Sscottl		 * Some parallel SCSI devices fail to send an
553195534Sscottl		 * ignore wide residue message when dealing with
554195534Sscottl		 * odd length inquiry requests.  Round up to be
555195534Sscottl		 * safe.
556195534Sscottl		 */
557195534Sscottl		inquiry_len = roundup2(inquiry_len, 2);
558195534Sscottl		scsi_inquiry(csio,
559195534Sscottl			     /*retries*/1,
560195534Sscottl			     probedone,
561195534Sscottl			     MSG_SIMPLE_Q_TAG,
562195534Sscottl			     (u_int8_t *)inq_buf,
563195534Sscottl			     inquiry_len,
564195534Sscottl			     /*evpd*/FALSE,
565195534Sscottl			     /*page_code*/0,
566195534Sscottl			     SSD_MIN_SIZE,
567195534Sscottl			     /*timeout*/60 * 1000);
568195534Sscottl		break;
569195534Sscottl	}
570195534Sscottl	case PROBE_PM_PID:
571195534Sscottl		cam_fill_ataio(ataio,
572195534Sscottl		      1,
573195534Sscottl		      probedone,
574195534Sscottl		      /*flags*/CAM_DIR_NONE,
575198389Smav		      0,
576195534Sscottl		      /*data_ptr*/NULL,
577195534Sscottl		      /*dxfer_len*/0,
578195534Sscottl		      10 * 1000);
579195534Sscottl		ata_pm_read_cmd(ataio, 0, 15);
580195534Sscottl		break;
581195534Sscottl	case PROBE_PM_PRV:
582195534Sscottl		cam_fill_ataio(ataio,
583195534Sscottl		      1,
584195534Sscottl		      probedone,
585195534Sscottl		      /*flags*/CAM_DIR_NONE,
586198389Smav		      0,
587195534Sscottl		      /*data_ptr*/NULL,
588195534Sscottl		      /*dxfer_len*/0,
589195534Sscottl		      10 * 1000);
590195534Sscottl		ata_pm_read_cmd(ataio, 1, 15);
591195534Sscottl		break;
592195534Sscottl	case PROBE_INVALID:
593198708Smav		CAM_DEBUG(path, CAM_DEBUG_INFO,
594195534Sscottl		    ("probestart: invalid action state\n"));
595195534Sscottl	default:
596195534Sscottl		break;
597195534Sscottl	}
598195534Sscottl	xpt_action(start_ccb);
599195534Sscottl}
600195534Sscottl#if 0
601195534Sscottlstatic void
602195534Sscottlproberequestdefaultnegotiation(struct cam_periph *periph)
603195534Sscottl{
604195534Sscottl	struct ccb_trans_settings cts;
605195534Sscottl
606203108Smav	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
607195534Sscottl	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
608195534Sscottl	cts.type = CTS_TYPE_USER_SETTINGS;
609195534Sscottl	xpt_action((union ccb *)&cts);
610195534Sscottl	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
611195534Sscottl		return;
612195534Sscottl	}
613195534Sscottl	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
614195534Sscottl	cts.type = CTS_TYPE_CURRENT_SETTINGS;
615195534Sscottl	xpt_action((union ccb *)&cts);
616195534Sscottl}
617195534Sscottl
618195534Sscottl/*
619195534Sscottl * Backoff Negotiation Code- only pertinent for SPI devices.
620195534Sscottl */
621195534Sscottlstatic int
622195534Sscottlproberequestbackoff(struct cam_periph *periph, struct cam_ed *device)
623195534Sscottl{
624195534Sscottl	struct ccb_trans_settings cts;
625195534Sscottl	struct ccb_trans_settings_spi *spi;
626195534Sscottl
627195534Sscottl	memset(&cts, 0, sizeof (cts));
628203108Smav	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
629195534Sscottl	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
630195534Sscottl	cts.type = CTS_TYPE_CURRENT_SETTINGS;
631195534Sscottl	xpt_action((union ccb *)&cts);
632195534Sscottl	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
633195534Sscottl		if (bootverbose) {
634195534Sscottl			xpt_print(periph->path,
635195534Sscottl			    "failed to get current device settings\n");
636195534Sscottl		}
637195534Sscottl		return (0);
638195534Sscottl	}
639195534Sscottl	if (cts.transport != XPORT_SPI) {
640195534Sscottl		if (bootverbose) {
641195534Sscottl			xpt_print(periph->path, "not SPI transport\n");
642195534Sscottl		}
643195534Sscottl		return (0);
644195534Sscottl	}
645195534Sscottl	spi = &cts.xport_specific.spi;
646195534Sscottl
647195534Sscottl	/*
648195534Sscottl	 * We cannot renegotiate sync rate if we don't have one.
649195534Sscottl	 */
650195534Sscottl	if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
651195534Sscottl		if (bootverbose) {
652195534Sscottl			xpt_print(periph->path, "no sync rate known\n");
653195534Sscottl		}
654195534Sscottl		return (0);
655195534Sscottl	}
656195534Sscottl
657195534Sscottl	/*
658195534Sscottl	 * We'll assert that we don't have to touch PPR options- the
659195534Sscottl	 * SIM will see what we do with period and offset and adjust
660195534Sscottl	 * the PPR options as appropriate.
661195534Sscottl	 */
662195534Sscottl
663195534Sscottl	/*
664195534Sscottl	 * A sync rate with unknown or zero offset is nonsensical.
665195534Sscottl	 * A sync period of zero means Async.
666195534Sscottl	 */
667195534Sscottl	if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0
668195534Sscottl	 || spi->sync_offset == 0 || spi->sync_period == 0) {
669195534Sscottl		if (bootverbose) {
670195534Sscottl			xpt_print(periph->path, "no sync rate available\n");
671195534Sscottl		}
672195534Sscottl		return (0);
673195534Sscottl	}
674195534Sscottl
675195534Sscottl	if (device->flags & CAM_DEV_DV_HIT_BOTTOM) {
676195534Sscottl		CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
677195534Sscottl		    ("hit async: giving up on DV\n"));
678195534Sscottl		return (0);
679195534Sscottl	}
680195534Sscottl
681195534Sscottl
682195534Sscottl	/*
683195534Sscottl	 * Jump sync_period up by one, but stop at 5MHz and fall back to Async.
684195534Sscottl	 * We don't try to remember 'last' settings to see if the SIM actually
685195534Sscottl	 * gets into the speed we want to set. We check on the SIM telling
686195534Sscottl	 * us that a requested speed is bad, but otherwise don't try and
687195534Sscottl	 * check the speed due to the asynchronous and handshake nature
688195534Sscottl	 * of speed setting.
689195534Sscottl	 */
690195534Sscottl	spi->valid = CTS_SPI_VALID_SYNC_RATE | CTS_SPI_VALID_SYNC_OFFSET;
691195534Sscottl	for (;;) {
692195534Sscottl		spi->sync_period++;
693195534Sscottl		if (spi->sync_period >= 0xf) {
694195534Sscottl			spi->sync_period = 0;
695195534Sscottl			spi->sync_offset = 0;
696195534Sscottl			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
697195534Sscottl			    ("setting to async for DV\n"));
698195534Sscottl			/*
699195534Sscottl			 * Once we hit async, we don't want to try
700195534Sscottl			 * any more settings.
701195534Sscottl			 */
702195534Sscottl			device->flags |= CAM_DEV_DV_HIT_BOTTOM;
703195534Sscottl		} else if (bootverbose) {
704195534Sscottl			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
705195534Sscottl			    ("DV: period 0x%x\n", spi->sync_period));
706195534Sscottl			printf("setting period to 0x%x\n", spi->sync_period);
707195534Sscottl		}
708195534Sscottl		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
709195534Sscottl		cts.type = CTS_TYPE_CURRENT_SETTINGS;
710195534Sscottl		xpt_action((union ccb *)&cts);
711195534Sscottl		if ((cts.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
712195534Sscottl			break;
713195534Sscottl		}
714195534Sscottl		CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
715195534Sscottl		    ("DV: failed to set period 0x%x\n", spi->sync_period));
716195534Sscottl		if (spi->sync_period == 0) {
717195534Sscottl			return (0);
718195534Sscottl		}
719195534Sscottl	}
720195534Sscottl	return (1);
721195534Sscottl}
722195534Sscottl#endif
723195534Sscottlstatic void
724195534Sscottlprobedone(struct cam_periph *periph, union ccb *done_ccb)
725195534Sscottl{
726199747Smav	struct ccb_trans_settings cts;
727195534Sscottl	struct ata_params *ident_buf;
728195534Sscottl	probe_softc *softc;
729195534Sscottl	struct cam_path *path;
730217444Smav	cam_status status;
731195534Sscottl	u_int32_t  priority;
732207499Smav	u_int caps;
733198389Smav	int found = 1;
734195534Sscottl
735195534Sscottl	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
736195534Sscottl
737195534Sscottl	softc = (probe_softc *)periph->softc;
738195534Sscottl	path = done_ccb->ccb_h.path;
739195534Sscottl	priority = done_ccb->ccb_h.pinfo.priority;
740195534Sscottl	ident_buf = &path->device->ident_data;
741195534Sscottl
742198708Smav	if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
743209744Smav		if (softc->restart) {
744209744Smav			if (bootverbose) {
745209744Smav				cam_error_print(done_ccb,
746209744Smav				    CAM_ESF_ALL, CAM_EPF_ALL);
747209744Smav			}
748209744Smav		} else if (cam_periph_error(done_ccb, 0, 0, NULL) == ERESTART)
749195534Sscottl			return;
750209744Smav		if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
751195534Sscottl			/* Don't wedge the queue */
752195534Sscottl			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
753195534Sscottl					 /*run_queue*/TRUE);
754195534Sscottl		}
755217444Smav		status = done_ccb->ccb_h.status & CAM_STATUS_MASK;
756209744Smav		if (softc->restart) {
757209744Smav			softc->faults++;
758209744Smav			if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) ==
759209744Smav			    CAM_CMD_TIMEOUT)
760209744Smav				softc->faults += 4;
761209744Smav			if (softc->faults < 10)
762209744Smav				goto done;
763209744Smav			else
764209744Smav				softc->restart = 0;
765217444Smav
766198708Smav		/* Old PIO2 devices may not support mode setting. */
767217444Smav		} else if (softc->action == PROBE_SETMODE &&
768217444Smav		    status == CAM_ATA_STATUS_ERROR &&
769198708Smav		    ata_max_pmode(ident_buf) <= ATA_PIO2 &&
770217444Smav		    (ident_buf->capabilities1 & ATA_SUPPORT_IORDY) == 0) {
771198708Smav			goto noerror;
772217444Smav
773198708Smav		/*
774217444Smav		 * Some old WD SATA disks report supported and enabled
775217444Smav		 * device-initiated interface power management, but return
776217444Smav		 * ABORT on attempt to disable it.
777217444Smav		 */
778217444Smav		} else if (softc->action == PROBE_SETPM &&
779217444Smav		    status == CAM_ATA_STATUS_ERROR) {
780217444Smav			goto noerror;
781217444Smav		}
782217444Smav
783217444Smav		/*
784198708Smav		 * If we get to this point, we got an error status back
785198708Smav		 * from the inquiry and the error status doesn't require
786198708Smav		 * automatically retrying the command.  Therefore, the
787198708Smav		 * inquiry failed.  If we had inquiry information before
788198708Smav		 * for this device, but this latest inquiry command failed,
789198708Smav		 * the device has probably gone away.  If this device isn't
790198708Smav		 * already marked unconfigured, notify the peripheral
791198708Smav		 * drivers that this device is no more.
792198708Smav		 */
793209744Smavdevice_fail:	if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
794198708Smav			xpt_async(AC_LOST_DEVICE, path, NULL);
795198708Smav		found = 0;
796198708Smav		goto done;
797198708Smav	}
798198708Smavnoerror:
799203385Smav	if (softc->restart)
800203385Smav		goto done;
801198708Smav	switch (softc->action) {
802198708Smav	case PROBE_RESET:
803195534Sscottl	{
804198708Smav		int sign = (done_ccb->ataio.res.lba_high << 8) +
805198708Smav		    done_ccb->ataio.res.lba_mid;
806200218Smav		if (bootverbose)
807200218Smav			xpt_print(path, "SIGNATURE: %04x\n", sign);
808198708Smav		if (sign == 0x0000 &&
809198708Smav		    done_ccb->ccb_h.target_id != 15) {
810198708Smav			path->device->protocol = PROTO_ATA;
811198708Smav			PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
812198708Smav		} else if (sign == 0x9669 &&
813198708Smav		    done_ccb->ccb_h.target_id == 15) {
814199747Smav			/* Report SIM that PM is present. */
815198708Smav			bzero(&cts, sizeof(cts));
816203108Smav			xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
817198708Smav			cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
818198708Smav			cts.type = CTS_TYPE_CURRENT_SETTINGS;
819198708Smav			cts.xport_specific.sata.pm_present = 1;
820198708Smav			cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
821198708Smav			xpt_action((union ccb *)&cts);
822198708Smav			path->device->protocol = PROTO_SATAPM;
823198708Smav			PROBE_SET_ACTION(softc, PROBE_PM_PID);
824198708Smav		} else if (sign == 0xeb14 &&
825198708Smav		    done_ccb->ccb_h.target_id != 15) {
826198708Smav			path->device->protocol = PROTO_SCSI;
827198708Smav			PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
828198708Smav		} else {
829198708Smav			if (done_ccb->ccb_h.target_id != 15) {
830198708Smav				xpt_print(path,
831198708Smav				    "Unexpected signature 0x%04x\n", sign);
832195534Sscottl			}
833198708Smav			goto device_fail;
834198708Smav		}
835198708Smav		xpt_release_ccb(done_ccb);
836198708Smav		xpt_schedule(periph, priority);
837198708Smav		return;
838198708Smav	}
839198708Smav	case PROBE_IDENTIFY:
840198708Smav	{
841207222Smav		struct ccb_pathinq cpi;
842198708Smav		int16_t *ptr;
843207282Smav		int changed = 1;
844195534Sscottl
845203385Smav		ident_buf = &softc->ident_data;
846198708Smav		for (ptr = (int16_t *)ident_buf;
847198708Smav		     ptr < (int16_t *)ident_buf + sizeof(struct ata_params)/2; ptr++) {
848198708Smav			*ptr = le16toh(*ptr);
849198708Smav		}
850198708Smav		if (strncmp(ident_buf->model, "FX", 2) &&
851198708Smav		    strncmp(ident_buf->model, "NEC", 3) &&
852198708Smav		    strncmp(ident_buf->model, "Pioneer", 7) &&
853198708Smav		    strncmp(ident_buf->model, "SHARP", 5)) {
854198708Smav			ata_bswap(ident_buf->model, sizeof(ident_buf->model));
855198708Smav			ata_bswap(ident_buf->revision, sizeof(ident_buf->revision));
856198708Smav			ata_bswap(ident_buf->serial, sizeof(ident_buf->serial));
857198708Smav		}
858198708Smav		ata_btrim(ident_buf->model, sizeof(ident_buf->model));
859198708Smav		ata_bpack(ident_buf->model, ident_buf->model, sizeof(ident_buf->model));
860198708Smav		ata_btrim(ident_buf->revision, sizeof(ident_buf->revision));
861198708Smav		ata_bpack(ident_buf->revision, ident_buf->revision, sizeof(ident_buf->revision));
862198708Smav		ata_btrim(ident_buf->serial, sizeof(ident_buf->serial));
863198708Smav		ata_bpack(ident_buf->serial, ident_buf->serial, sizeof(ident_buf->serial));
864203421Smav		/* Device may need spin-up before IDENTIFY become valid. */
865204354Smav		if ((ident_buf->specconf == 0x37c8 ||
866204354Smav		     ident_buf->specconf == 0x738c) &&
867204354Smav		    ((ident_buf->config & ATA_RESP_INCOMPLETE) ||
868204354Smav		     softc->spinup == 0)) {
869203421Smav			PROBE_SET_ACTION(softc, PROBE_SPINUP);
870203421Smav			xpt_release_ccb(done_ccb);
871203421Smav			xpt_schedule(periph, priority);
872203421Smav			return;
873203421Smav		}
874203385Smav		ident_buf = &path->device->ident_data;
875198708Smav		if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
876198708Smav			/* Check that it is the same device. */
877203385Smav			if (bcmp(softc->ident_data.model, ident_buf->model,
878203385Smav			     sizeof(ident_buf->model)) ||
879203385Smav			    bcmp(softc->ident_data.revision, ident_buf->revision,
880203385Smav			     sizeof(ident_buf->revision)) ||
881203385Smav			    bcmp(softc->ident_data.serial, ident_buf->serial,
882203385Smav			     sizeof(ident_buf->serial))) {
883198708Smav				/* Device changed. */
884198708Smav				xpt_async(AC_LOST_DEVICE, path, NULL);
885207282Smav			} else {
886203385Smav				bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
887207282Smav				changed = 0;
888207282Smav			}
889207282Smav		}
890207282Smav		if (changed) {
891203385Smav			bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
892195534Sscottl			/* Clean up from previous instance of this device */
893195534Sscottl			if (path->device->serial_num != NULL) {
894195534Sscottl				free(path->device->serial_num, M_CAMXPT);
895195534Sscottl				path->device->serial_num = NULL;
896195534Sscottl				path->device->serial_num_len = 0;
897195534Sscottl			}
898195534Sscottl			path->device->serial_num =
899195534Sscottl				(u_int8_t *)malloc((sizeof(ident_buf->serial) + 1),
900198708Smav					   M_CAMXPT, M_NOWAIT);
901195534Sscottl			if (path->device->serial_num != NULL) {
902195534Sscottl				bcopy(ident_buf->serial,
903195534Sscottl				      path->device->serial_num,
904195534Sscottl				      sizeof(ident_buf->serial));
905195534Sscottl				path->device->serial_num[sizeof(ident_buf->serial)]
906195534Sscottl				    = '\0';
907195534Sscottl				path->device->serial_num_len =
908195534Sscottl				    strlen(path->device->serial_num);
909195534Sscottl			}
910195534Sscottl
911198331Smav			path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
912195534Sscottl		}
913199178Smav		if (ident_buf->satacapabilities & ATA_SUPPORT_NCQ) {
914199178Smav			path->device->mintags = path->device->maxtags =
915199178Smav			    ATA_QUEUE_LEN(ident_buf->queue) + 1;
916199178Smav		}
917199178Smav		ata_find_quirk(path->device);
918199263Smav		if (path->device->mintags != 0 &&
919199263Smav		    path->bus->sim->max_tagged_dev_openings != 0) {
920207222Smav			/* Check if the SIM does not want queued commands. */
921207222Smav			bzero(&cpi, sizeof(cpi));
922207222Smav			xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
923207222Smav			cpi.ccb_h.func_code = XPT_PATH_INQ;
924207222Smav			xpt_action((union ccb *)&cpi);
925207222Smav			if (cpi.ccb_h.status == CAM_REQ_CMP &&
926207222Smav			    (cpi.hba_inquiry & PI_TAG_ABLE)) {
927207222Smav				/* Report SIM which tags are allowed. */
928207222Smav				bzero(&cts, sizeof(cts));
929207222Smav				xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
930207222Smav				cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
931207222Smav				cts.type = CTS_TYPE_CURRENT_SETTINGS;
932207222Smav				cts.xport_specific.sata.tags = path->device->maxtags;
933207222Smav				cts.xport_specific.sata.valid = CTS_SATA_VALID_TAGS;
934207222Smav				xpt_action((union ccb *)&cts);
935207222Smav				/* Reconfigure queues for tagged queueing. */
936207222Smav				xpt_start_tags(path);
937207222Smav			}
938199178Smav		}
939198708Smav		ata_device_transport(path);
940198708Smav		PROBE_SET_ACTION(softc, PROBE_SETMODE);
941195534Sscottl		xpt_release_ccb(done_ccb);
942198708Smav		xpt_schedule(periph, priority);
943198708Smav		return;
944195534Sscottl	}
945203421Smav	case PROBE_SPINUP:
946203421Smav		if (bootverbose)
947203421Smav			xpt_print(path, "Spin-up done\n");
948203421Smav		softc->spinup = 1;
949203421Smav		PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
950203421Smav		xpt_release_ccb(done_ccb);
951203421Smav		xpt_schedule(periph, priority);
952203421Smav		return;
953195534Sscottl	case PROBE_SETMODE:
954207499Smav		if (path->device->transport != XPORT_SATA)
955207499Smav			goto notsata;
956207499Smav		/* Set supported bits. */
957207499Smav		bzero(&cts, sizeof(cts));
958207499Smav		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
959207499Smav		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
960207499Smav		cts.type = CTS_TYPE_CURRENT_SETTINGS;
961207499Smav		xpt_action((union ccb *)&cts);
962207499Smav		if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
963207499Smav			caps = cts.xport_specific.sata.caps & CTS_SATA_CAPS_H;
964207499Smav		else
965207499Smav			caps = 0;
966207499Smav		if (ident_buf->satacapabilities != 0xffff) {
967207499Smav			if (ident_buf->satacapabilities & ATA_SUPPORT_IFPWRMNGTRCV)
968207499Smav				caps |= CTS_SATA_CAPS_D_PMREQ;
969207499Smav			if (ident_buf->satacapabilities & ATA_SUPPORT_HAPST)
970207499Smav				caps |= CTS_SATA_CAPS_D_APST;
971207499Smav		}
972207499Smav		/* Mask unwanted bits. */
973207499Smav		bzero(&cts, sizeof(cts));
974207499Smav		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
975207499Smav		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
976207499Smav		cts.type = CTS_TYPE_USER_SETTINGS;
977207499Smav		xpt_action((union ccb *)&cts);
978207499Smav		if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
979207499Smav			caps &= cts.xport_specific.sata.caps;
980215454Smav		else
981215454Smav			caps = 0;
982207499Smav		/* Store result to SIM. */
983207499Smav		bzero(&cts, sizeof(cts));
984207499Smav		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
985207499Smav		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
986207499Smav		cts.type = CTS_TYPE_CURRENT_SETTINGS;
987207499Smav		cts.xport_specific.sata.caps = caps;
988207499Smav		cts.xport_specific.sata.valid = CTS_SATA_VALID_CAPS;
989207499Smav		xpt_action((union ccb *)&cts);
990207499Smav		softc->caps = caps;
991207499Smav		if (ident_buf->satasupport & ATA_SUPPORT_IFPWRMNGT) {
992207499Smav			PROBE_SET_ACTION(softc, PROBE_SETPM);
993207499Smav			xpt_release_ccb(done_ccb);
994207499Smav			xpt_schedule(periph, priority);
995207499Smav			return;
996207499Smav		}
997207499Smav		/* FALLTHROUGH */
998207499Smav	case PROBE_SETPM:
999207499Smav		if (ident_buf->satacapabilities != 0xffff &&
1000207499Smav		    ident_buf->satacapabilities & ATA_SUPPORT_DAPST) {
1001207499Smav			PROBE_SET_ACTION(softc, PROBE_SETAPST);
1002207499Smav			xpt_release_ccb(done_ccb);
1003207499Smav			xpt_schedule(periph, priority);
1004207499Smav			return;
1005207499Smav		}
1006207499Smav		/* FALLTHROUGH */
1007207499Smav	case PROBE_SETAPST:
1008207499Smav		if (ident_buf->satasupport & ATA_SUPPORT_AUTOACTIVATE) {
1009207499Smav			PROBE_SET_ACTION(softc, PROBE_SETDMAAA);
1010207499Smav			xpt_release_ccb(done_ccb);
1011207499Smav			xpt_schedule(periph, priority);
1012207499Smav			return;
1013207499Smav		}
1014207499Smav		/* FALLTHROUGH */
1015207499Smav	case PROBE_SETDMAAA:
1016207499Smavnotsata:
1017198708Smav		if (path->device->protocol == PROTO_ATA) {
1018198708Smav			PROBE_SET_ACTION(softc, PROBE_SET_MULTI);
1019198708Smav		} else {
1020198708Smav			PROBE_SET_ACTION(softc, PROBE_INQUIRY);
1021195534Sscottl		}
1022198708Smav		xpt_release_ccb(done_ccb);
1023198708Smav		xpt_schedule(periph, priority);
1024198708Smav		return;
1025198708Smav	case PROBE_SET_MULTI:
1026198708Smav		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1027198708Smav			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1028198748Smav			xpt_acquire_device(path->device);
1029198708Smav			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1030198708Smav			xpt_action(done_ccb);
1031198708Smav			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
1032198708Smav			    done_ccb);
1033198708Smav		}
1034198708Smav		break;
1035195534Sscottl	case PROBE_INQUIRY:
1036195534Sscottl	case PROBE_FULL_INQUIRY:
1037195534Sscottl	{
1038198708Smav		struct scsi_inquiry_data *inq_buf;
1039198708Smav		u_int8_t periph_qual, len;
1040195534Sscottl
1041198708Smav		path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
1042198708Smav		inq_buf = &path->device->inq_data;
1043195534Sscottl
1044198708Smav		periph_qual = SID_QUAL(inq_buf);
1045195534Sscottl
1046198708Smav		if (periph_qual != SID_QUAL_LU_CONNECTED)
1047198708Smav			break;
1048195534Sscottl
1049198708Smav		/*
1050198708Smav		 * We conservatively request only
1051198708Smav		 * SHORT_INQUIRY_LEN bytes of inquiry
1052198708Smav		 * information during our first try
1053198708Smav		 * at sending an INQUIRY. If the device
1054198708Smav		 * has more information to give,
1055198708Smav		 * perform a second request specifying
1056198708Smav		 * the amount of information the device
1057198708Smav		 * is willing to give.
1058198708Smav		 */
1059198708Smav		len = inq_buf->additional_length
1060198708Smav		    + offsetof(struct scsi_inquiry_data, additional_length) + 1;
1061198708Smav		if (softc->action == PROBE_INQUIRY
1062198708Smav		    && len > SHORT_INQUIRY_LENGTH) {
1063198708Smav			PROBE_SET_ACTION(softc, PROBE_FULL_INQUIRY);
1064198708Smav			xpt_release_ccb(done_ccb);
1065198708Smav			xpt_schedule(periph, priority);
1066195534Sscottl			return;
1067195534Sscottl		}
1068198708Smav
1069198708Smav		ata_device_transport(path);
1070198708Smav		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1071198708Smav			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1072198748Smav			xpt_acquire_device(path->device);
1073198708Smav			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1074198708Smav			xpt_action(done_ccb);
1075198708Smav			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path, done_ccb);
1076198708Smav		}
1077198708Smav		break;
1078195534Sscottl	}
1079195534Sscottl	case PROBE_PM_PID:
1080198708Smav		if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) == 0)
1081198708Smav			bzero(ident_buf, sizeof(*ident_buf));
1082198708Smav		softc->pm_pid = (done_ccb->ataio.res.lba_high << 24) +
1083198708Smav		    (done_ccb->ataio.res.lba_mid << 16) +
1084198708Smav		    (done_ccb->ataio.res.lba_low << 8) +
1085198708Smav		    done_ccb->ataio.res.sector_count;
1086198708Smav		((uint32_t *)ident_buf)[0] = softc->pm_pid;
1087198708Smav		snprintf(ident_buf->model, sizeof(ident_buf->model),
1088198708Smav		    "Port Multiplier %08x", softc->pm_pid);
1089198708Smav		PROBE_SET_ACTION(softc, PROBE_PM_PRV);
1090198708Smav		xpt_release_ccb(done_ccb);
1091198708Smav		xpt_schedule(periph, priority);
1092198708Smav		return;
1093195534Sscottl	case PROBE_PM_PRV:
1094198708Smav		softc->pm_prv = (done_ccb->ataio.res.lba_high << 24) +
1095198708Smav		    (done_ccb->ataio.res.lba_mid << 16) +
1096198708Smav		    (done_ccb->ataio.res.lba_low << 8) +
1097198708Smav		    done_ccb->ataio.res.sector_count;
1098198708Smav		((uint32_t *)ident_buf)[1] = softc->pm_prv;
1099198708Smav		snprintf(ident_buf->revision, sizeof(ident_buf->revision),
1100198708Smav		    "%04x", softc->pm_prv);
1101198708Smav		path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
1102207499Smav		/* Set supported bits. */
1103207499Smav		bzero(&cts, sizeof(cts));
1104207499Smav		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1105207499Smav		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1106207499Smav		cts.type = CTS_TYPE_CURRENT_SETTINGS;
1107207499Smav		xpt_action((union ccb *)&cts);
1108207499Smav		if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1109207499Smav			caps = cts.xport_specific.sata.caps & CTS_SATA_CAPS_H;
1110207499Smav		else
1111207499Smav			caps = 0;
1112207499Smav		/* All PMPs must support PM requests. */
1113207499Smav		caps |= CTS_SATA_CAPS_D_PMREQ;
1114207499Smav		/* Mask unwanted bits. */
1115207499Smav		bzero(&cts, sizeof(cts));
1116207499Smav		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1117207499Smav		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1118207499Smav		cts.type = CTS_TYPE_USER_SETTINGS;
1119207499Smav		xpt_action((union ccb *)&cts);
1120207499Smav		if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1121207499Smav			caps &= cts.xport_specific.sata.caps;
1122215454Smav		else
1123215454Smav			caps = 0;
1124207499Smav		/* Store result to SIM. */
1125207499Smav		bzero(&cts, sizeof(cts));
1126207499Smav		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1127207499Smav		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1128207499Smav		cts.type = CTS_TYPE_CURRENT_SETTINGS;
1129207499Smav		cts.xport_specific.sata.caps = caps;
1130207499Smav		cts.xport_specific.sata.valid = CTS_SATA_VALID_CAPS;
1131207499Smav		xpt_action((union ccb *)&cts);
1132207499Smav		softc->caps = caps;
1133198708Smav		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1134198708Smav			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1135198748Smav			xpt_acquire_device(path->device);
1136198708Smav			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1137198708Smav			xpt_action(done_ccb);
1138198708Smav			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
1139198708Smav			    done_ccb);
1140198708Smav		} else {
1141198708Smav			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1142198708Smav			xpt_action(done_ccb);
1143198708Smav			xpt_async(AC_SCSI_AEN, done_ccb->ccb_h.path, done_ccb);
1144195534Sscottl		}
1145198708Smav		break;
1146195534Sscottl	case PROBE_INVALID:
1147195534Sscottl		CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_INFO,
1148195534Sscottl		    ("probedone: invalid action state\n"));
1149195534Sscottl	default:
1150195534Sscottl		break;
1151195534Sscottl	}
1152198708Smavdone:
1153203108Smav	if (softc->restart) {
1154203108Smav		softc->restart = 0;
1155203108Smav		xpt_release_ccb(done_ccb);
1156195534Sscottl		probeschedule(periph);
1157203108Smav		return;
1158195534Sscottl	}
1159203108Smav	xpt_release_ccb(done_ccb);
1160203108Smav	while ((done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs))) {
1161203108Smav		TAILQ_REMOVE(&softc->request_ccbs,
1162203108Smav		    &done_ccb->ccb_h, periph_links.tqe);
1163203108Smav		done_ccb->ccb_h.status = found ? CAM_REQ_CMP : CAM_REQ_CMP_ERR;
1164203108Smav		xpt_done(done_ccb);
1165203108Smav	}
1166203108Smav	cam_release_devq(periph->path,
1167203108Smav	    RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_XPT + 1, FALSE);
1168203108Smav	cam_periph_invalidate(periph);
1169203108Smav	cam_periph_release_locked(periph);
1170195534Sscottl}
1171195534Sscottl
1172195534Sscottlstatic void
1173195534Sscottlprobecleanup(struct cam_periph *periph)
1174195534Sscottl{
1175195534Sscottl	free(periph->softc, M_CAMXPT);
1176195534Sscottl}
1177195534Sscottl
1178195534Sscottlstatic void
1179199178Smavata_find_quirk(struct cam_ed *device)
1180195534Sscottl{
1181199178Smav	struct ata_quirk_entry *quirk;
1182195534Sscottl	caddr_t	match;
1183195534Sscottl
1184199178Smav	match = cam_quirkmatch((caddr_t)&device->ident_data,
1185199178Smav			       (caddr_t)ata_quirk_table,
1186199178Smav			       ata_quirk_table_size,
1187199178Smav			       sizeof(*ata_quirk_table), ata_identify_match);
1188195534Sscottl
1189195534Sscottl	if (match == NULL)
1190195534Sscottl		panic("xpt_find_quirk: device didn't match wildcard entry!!");
1191195534Sscottl
1192199178Smav	quirk = (struct ata_quirk_entry *)match;
1193195534Sscottl	device->quirk = quirk;
1194199178Smav	if (quirk->quirks & CAM_QUIRK_MAXTAGS)
1195199178Smav		device->mintags = device->maxtags = quirk->maxtags;
1196195534Sscottl}
1197195534Sscottl
1198195534Sscottltypedef struct {
1199195534Sscottl	union	ccb *request_ccb;
1200195534Sscottl	struct 	ccb_pathinq *cpi;
1201195534Sscottl	int	counter;
1202195534Sscottl} ata_scan_bus_info;
1203195534Sscottl
1204195534Sscottl/*
1205195534Sscottl * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
1206195534Sscottl * As the scan progresses, xpt_scan_bus is used as the
1207195534Sscottl * callback on completion function.
1208195534Sscottl */
1209195534Sscottlstatic void
1210195534Sscottlata_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
1211195534Sscottl{
1212195534Sscottl	struct	cam_path *path;
1213195534Sscottl	ata_scan_bus_info *scan_info;
1214203108Smav	union	ccb *work_ccb, *reset_ccb;
1215195534Sscottl	cam_status status;
1216195534Sscottl
1217195534Sscottl	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
1218195534Sscottl		  ("xpt_scan_bus\n"));
1219195534Sscottl	switch (request_ccb->ccb_h.func_code) {
1220195534Sscottl	case XPT_SCAN_BUS:
1221208582Smjacob	case XPT_SCAN_TGT:
1222195534Sscottl		/* Find out the characteristics of the bus */
1223195534Sscottl		work_ccb = xpt_alloc_ccb_nowait();
1224195534Sscottl		if (work_ccb == NULL) {
1225195534Sscottl			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1226195534Sscottl			xpt_done(request_ccb);
1227195534Sscottl			return;
1228195534Sscottl		}
1229195534Sscottl		xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
1230195534Sscottl			      request_ccb->ccb_h.pinfo.priority);
1231195534Sscottl		work_ccb->ccb_h.func_code = XPT_PATH_INQ;
1232195534Sscottl		xpt_action(work_ccb);
1233195534Sscottl		if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
1234195534Sscottl			request_ccb->ccb_h.status = work_ccb->ccb_h.status;
1235195534Sscottl			xpt_free_ccb(work_ccb);
1236195534Sscottl			xpt_done(request_ccb);
1237195534Sscottl			return;
1238195534Sscottl		}
1239195534Sscottl
1240203108Smav		/* We may need to reset bus first, if we haven't done it yet. */
1241203108Smav		if ((work_ccb->cpi.hba_inquiry &
1242203108Smav		    (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) &&
1243203108Smav		    !(work_ccb->cpi.hba_misc & PIM_NOBUSRESET) &&
1244203108Smav		    !timevalisset(&request_ccb->ccb_h.path->bus->last_reset)) {
1245203108Smav			reset_ccb = xpt_alloc_ccb_nowait();
1246208823Smav			if (reset_ccb == NULL) {
1247208823Smav				request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1248208823Smav				xpt_free_ccb(work_ccb);
1249208823Smav				xpt_done(request_ccb);
1250208823Smav				return;
1251208823Smav			}
1252203108Smav			xpt_setup_ccb(&reset_ccb->ccb_h, request_ccb->ccb_h.path,
1253203108Smav			      CAM_PRIORITY_NONE);
1254203108Smav			reset_ccb->ccb_h.func_code = XPT_RESET_BUS;
1255203108Smav			xpt_action(reset_ccb);
1256203108Smav			if (reset_ccb->ccb_h.status != CAM_REQ_CMP) {
1257203108Smav				request_ccb->ccb_h.status = reset_ccb->ccb_h.status;
1258203108Smav				xpt_free_ccb(reset_ccb);
1259203108Smav				xpt_free_ccb(work_ccb);
1260203108Smav				xpt_done(request_ccb);
1261203108Smav				return;
1262203108Smav			}
1263203108Smav			xpt_free_ccb(reset_ccb);
1264203108Smav		}
1265203108Smav
1266195534Sscottl		/* Save some state for use while we probe for devices */
1267195534Sscottl		scan_info = (ata_scan_bus_info *)
1268195534Sscottl		    malloc(sizeof(ata_scan_bus_info), M_CAMXPT, M_NOWAIT);
1269195534Sscottl		if (scan_info == NULL) {
1270195534Sscottl			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1271208823Smav			xpt_free_ccb(work_ccb);
1272195534Sscottl			xpt_done(request_ccb);
1273195534Sscottl			return;
1274195534Sscottl		}
1275195534Sscottl		scan_info->request_ccb = request_ccb;
1276195534Sscottl		scan_info->cpi = &work_ccb->cpi;
1277195534Sscottl		/* If PM supported, probe it first. */
1278195534Sscottl		if (scan_info->cpi->hba_inquiry & PI_SATAPM)
1279201990Smav			scan_info->counter = scan_info->cpi->max_target;
1280201990Smav		else
1281201990Smav			scan_info->counter = 0;
1282195534Sscottl
1283195534Sscottl		work_ccb = xpt_alloc_ccb_nowait();
1284195534Sscottl		if (work_ccb == NULL) {
1285195534Sscottl			free(scan_info, M_CAMXPT);
1286195534Sscottl			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1287195534Sscottl			xpt_done(request_ccb);
1288195534Sscottl			break;
1289195534Sscottl		}
1290195534Sscottl		goto scan_next;
1291195534Sscottl	case XPT_SCAN_LUN:
1292195534Sscottl		work_ccb = request_ccb;
1293195534Sscottl		/* Reuse the same CCB to query if a device was really found */
1294195534Sscottl		scan_info = (ata_scan_bus_info *)work_ccb->ccb_h.ppriv_ptr0;
1295198389Smav		/* If there is PMP... */
1296201990Smav		if ((scan_info->cpi->hba_inquiry & PI_SATAPM) &&
1297201990Smav		    (scan_info->counter == scan_info->cpi->max_target)) {
1298203108Smav			if (work_ccb->ccb_h.status == CAM_REQ_CMP) {
1299207428Smav				/* everything else will be probed by it */
1300207428Smav				/* Free the current request path- we're done with it. */
1301207428Smav				xpt_free_path(work_ccb->ccb_h.path);
1302201990Smav				goto done;
1303195534Sscottl			} else {
1304195534Sscottl				struct ccb_trans_settings cts;
1305195534Sscottl
1306195534Sscottl				/* Report SIM that PM is absent. */
1307195534Sscottl				bzero(&cts, sizeof(cts));
1308195534Sscottl				xpt_setup_ccb(&cts.ccb_h,
1309207428Smav				    work_ccb->ccb_h.path, CAM_PRIORITY_NONE);
1310195534Sscottl				cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1311195534Sscottl				cts.type = CTS_TYPE_CURRENT_SETTINGS;
1312195665Smav				cts.xport_specific.sata.pm_present = 0;
1313195534Sscottl				cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
1314195534Sscottl				xpt_action((union ccb *)&cts);
1315195534Sscottl			}
1316195534Sscottl		}
1317207428Smav		/* Free the current request path- we're done with it. */
1318207428Smav		xpt_free_path(work_ccb->ccb_h.path);
1319201990Smav		if (scan_info->counter ==
1320201990Smav		    ((scan_info->cpi->hba_inquiry & PI_SATAPM) ?
1321201990Smav		    0 : scan_info->cpi->max_target)) {
1322201990Smavdone:
1323195534Sscottl			xpt_free_ccb(work_ccb);
1324195534Sscottl			xpt_free_ccb((union ccb *)scan_info->cpi);
1325195534Sscottl			request_ccb = scan_info->request_ccb;
1326195534Sscottl			free(scan_info, M_CAMXPT);
1327195534Sscottl			request_ccb->ccb_h.status = CAM_REQ_CMP;
1328195534Sscottl			xpt_done(request_ccb);
1329195534Sscottl			break;
1330195534Sscottl		}
1331201990Smav		/* Take next device. Wrap from max (PMP) to 0. */
1332201990Smav		scan_info->counter = (scan_info->counter + 1 ) %
1333201990Smav		    (scan_info->cpi->max_target + 1);
1334195534Sscottlscan_next:
1335195534Sscottl		status = xpt_create_path(&path, xpt_periph,
1336195534Sscottl		    scan_info->request_ccb->ccb_h.path_id,
1337195534Sscottl		    scan_info->counter, 0);
1338195534Sscottl		if (status != CAM_REQ_CMP) {
1339195534Sscottl			printf("xpt_scan_bus: xpt_create_path failed"
1340195534Sscottl			    " with status %#x, bus scan halted\n",
1341195534Sscottl			    status);
1342195534Sscottl			xpt_free_ccb(work_ccb);
1343195534Sscottl			xpt_free_ccb((union ccb *)scan_info->cpi);
1344195534Sscottl			request_ccb = scan_info->request_ccb;
1345195534Sscottl			free(scan_info, M_CAMXPT);
1346195534Sscottl			request_ccb->ccb_h.status = status;
1347195534Sscottl			xpt_done(request_ccb);
1348195534Sscottl			break;
1349195534Sscottl		}
1350195534Sscottl		xpt_setup_ccb(&work_ccb->ccb_h, path,
1351195534Sscottl		    scan_info->request_ccb->ccb_h.pinfo.priority);
1352195534Sscottl		work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1353195534Sscottl		work_ccb->ccb_h.cbfcnp = ata_scan_bus;
1354195534Sscottl		work_ccb->ccb_h.ppriv_ptr0 = scan_info;
1355195534Sscottl		work_ccb->crcn.flags = scan_info->request_ccb->crcn.flags;
1356195534Sscottl		xpt_action(work_ccb);
1357195534Sscottl		break;
1358195534Sscottl	default:
1359195534Sscottl		break;
1360195534Sscottl	}
1361195534Sscottl}
1362195534Sscottl
1363195534Sscottlstatic void
1364195534Sscottlata_scan_lun(struct cam_periph *periph, struct cam_path *path,
1365195534Sscottl	     cam_flags flags, union ccb *request_ccb)
1366195534Sscottl{
1367195534Sscottl	struct ccb_pathinq cpi;
1368195534Sscottl	cam_status status;
1369195534Sscottl	struct cam_path *new_path;
1370195534Sscottl	struct cam_periph *old_periph;
1371195534Sscottl
1372203108Smav	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_scan_lun\n"));
1373195534Sscottl
1374203108Smav	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
1375195534Sscottl	cpi.ccb_h.func_code = XPT_PATH_INQ;
1376195534Sscottl	xpt_action((union ccb *)&cpi);
1377195534Sscottl
1378195534Sscottl	if (cpi.ccb_h.status != CAM_REQ_CMP) {
1379195534Sscottl		if (request_ccb != NULL) {
1380195534Sscottl			request_ccb->ccb_h.status = cpi.ccb_h.status;
1381195534Sscottl			xpt_done(request_ccb);
1382195534Sscottl		}
1383195534Sscottl		return;
1384195534Sscottl	}
1385195534Sscottl
1386195534Sscottl	if (request_ccb == NULL) {
1387195534Sscottl		request_ccb = malloc(sizeof(union ccb), M_CAMXPT, M_NOWAIT);
1388195534Sscottl		if (request_ccb == NULL) {
1389195534Sscottl			xpt_print(path, "xpt_scan_lun: can't allocate CCB, "
1390195534Sscottl			    "can't continue\n");
1391195534Sscottl			return;
1392195534Sscottl		}
1393195534Sscottl		new_path = malloc(sizeof(*new_path), M_CAMXPT, M_NOWAIT);
1394195534Sscottl		if (new_path == NULL) {
1395195534Sscottl			xpt_print(path, "xpt_scan_lun: can't allocate path, "
1396195534Sscottl			    "can't continue\n");
1397195534Sscottl			free(request_ccb, M_CAMXPT);
1398195534Sscottl			return;
1399195534Sscottl		}
1400195534Sscottl		status = xpt_compile_path(new_path, xpt_periph,
1401195534Sscottl					  path->bus->path_id,
1402195534Sscottl					  path->target->target_id,
1403195534Sscottl					  path->device->lun_id);
1404195534Sscottl
1405195534Sscottl		if (status != CAM_REQ_CMP) {
1406195534Sscottl			xpt_print(path, "xpt_scan_lun: can't compile path, "
1407195534Sscottl			    "can't continue\n");
1408195534Sscottl			free(request_ccb, M_CAMXPT);
1409195534Sscottl			free(new_path, M_CAMXPT);
1410195534Sscottl			return;
1411195534Sscottl		}
1412203108Smav		xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_XPT);
1413195534Sscottl		request_ccb->ccb_h.cbfcnp = xptscandone;
1414195534Sscottl		request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1415195534Sscottl		request_ccb->crcn.flags = flags;
1416195534Sscottl	}
1417195534Sscottl
1418195653Smav	if ((old_periph = cam_periph_find(path, "aprobe")) != NULL) {
1419195534Sscottl		probe_softc *softc;
1420195534Sscottl
1421195534Sscottl		softc = (probe_softc *)old_periph->softc;
1422195534Sscottl		TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
1423195534Sscottl				  periph_links.tqe);
1424203108Smav		softc->restart = 1;
1425195534Sscottl	} else {
1426195534Sscottl		status = cam_periph_alloc(proberegister, NULL, probecleanup,
1427195653Smav					  probestart, "aprobe",
1428195534Sscottl					  CAM_PERIPH_BIO,
1429195534Sscottl					  request_ccb->ccb_h.path, NULL, 0,
1430195534Sscottl					  request_ccb);
1431195534Sscottl
1432195534Sscottl		if (status != CAM_REQ_CMP) {
1433195534Sscottl			xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
1434195534Sscottl			    "returned an error, can't continue probe\n");
1435195534Sscottl			request_ccb->ccb_h.status = status;
1436195534Sscottl			xpt_done(request_ccb);
1437195534Sscottl		}
1438195534Sscottl	}
1439195534Sscottl}
1440195534Sscottl
1441195534Sscottlstatic void
1442195534Sscottlxptscandone(struct cam_periph *periph, union ccb *done_ccb)
1443195534Sscottl{
1444195534Sscottl	xpt_release_path(done_ccb->ccb_h.path);
1445195534Sscottl	free(done_ccb->ccb_h.path, M_CAMXPT);
1446195534Sscottl	free(done_ccb, M_CAMXPT);
1447195534Sscottl}
1448195534Sscottl
1449195534Sscottlstatic struct cam_ed *
1450195534Sscottlata_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
1451195534Sscottl{
1452195534Sscottl	struct cam_path path;
1453199178Smav	struct ata_quirk_entry *quirk;
1454195534Sscottl	struct cam_ed *device;
1455195534Sscottl	struct cam_ed *cur_device;
1456195534Sscottl
1457195534Sscottl	device = xpt_alloc_device(bus, target, lun_id);
1458195534Sscottl	if (device == NULL)
1459195534Sscottl		return (NULL);
1460195534Sscottl
1461195534Sscottl	/*
1462195534Sscottl	 * Take the default quirk entry until we have inquiry
1463195534Sscottl	 * data and can determine a better quirk to use.
1464195534Sscottl	 */
1465199178Smav	quirk = &ata_quirk_table[ata_quirk_table_size - 1];
1466195534Sscottl	device->quirk = (void *)quirk;
1467199178Smav	device->mintags = 0;
1468199178Smav	device->maxtags = 0;
1469195534Sscottl	bzero(&device->inq_data, sizeof(device->inq_data));
1470195534Sscottl	device->inq_flags = 0;
1471195534Sscottl	device->queue_flags = 0;
1472195534Sscottl	device->serial_num = NULL;
1473195534Sscottl	device->serial_num_len = 0;
1474195534Sscottl
1475195534Sscottl	/*
1476195534Sscottl	 * XXX should be limited by number of CCBs this bus can
1477195534Sscottl	 * do.
1478195534Sscottl	 */
1479195534Sscottl	bus->sim->max_ccbs += device->ccbq.devq_openings;
1480195534Sscottl	/* Insertion sort into our target's device list */
1481195534Sscottl	cur_device = TAILQ_FIRST(&target->ed_entries);
1482195534Sscottl	while (cur_device != NULL && cur_device->lun_id < lun_id)
1483195534Sscottl		cur_device = TAILQ_NEXT(cur_device, links);
1484195534Sscottl	if (cur_device != NULL) {
1485195534Sscottl		TAILQ_INSERT_BEFORE(cur_device, device, links);
1486195534Sscottl	} else {
1487195534Sscottl		TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
1488195534Sscottl	}
1489195534Sscottl	target->generation++;
1490195534Sscottl	if (lun_id != CAM_LUN_WILDCARD) {
1491195534Sscottl		xpt_compile_path(&path,
1492195534Sscottl				 NULL,
1493195534Sscottl				 bus->path_id,
1494195534Sscottl				 target->target_id,
1495195534Sscottl				 lun_id);
1496195534Sscottl		ata_device_transport(&path);
1497195534Sscottl		xpt_release_path(&path);
1498195534Sscottl	}
1499195534Sscottl
1500195534Sscottl	return (device);
1501195534Sscottl}
1502195534Sscottl
1503195534Sscottlstatic void
1504195534Sscottlata_device_transport(struct cam_path *path)
1505195534Sscottl{
1506195534Sscottl	struct ccb_pathinq cpi;
1507198331Smav	struct ccb_trans_settings cts;
1508198331Smav	struct scsi_inquiry_data *inq_buf = NULL;
1509198331Smav	struct ata_params *ident_buf = NULL;
1510195534Sscottl
1511195534Sscottl	/* Get transport information from the SIM */
1512203108Smav	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
1513195534Sscottl	cpi.ccb_h.func_code = XPT_PATH_INQ;
1514195534Sscottl	xpt_action((union ccb *)&cpi);
1515195534Sscottl
1516195534Sscottl	path->device->transport = cpi.transport;
1517198331Smav	if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
1518198331Smav		inq_buf = &path->device->inq_data;
1519198331Smav	if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) != 0)
1520198331Smav		ident_buf = &path->device->ident_data;
1521198331Smav	if (path->device->protocol == PROTO_ATA) {
1522198331Smav		path->device->protocol_version = ident_buf ?
1523198331Smav		    ata_version(ident_buf->version_major) : cpi.protocol_version;
1524198331Smav	} else if (path->device->protocol == PROTO_SCSI) {
1525198331Smav		path->device->protocol_version = inq_buf ?
1526198331Smav		    SID_ANSI_REV(inq_buf) : cpi.protocol_version;
1527195534Sscottl	}
1528198331Smav	path->device->transport_version = ident_buf ?
1529198331Smav	    ata_version(ident_buf->version_major) : cpi.transport_version;
1530195534Sscottl
1531195534Sscottl	/* Tell the controller what we think */
1532203108Smav	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1533195534Sscottl	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1534195534Sscottl	cts.type = CTS_TYPE_CURRENT_SETTINGS;
1535195534Sscottl	cts.transport = path->device->transport;
1536195534Sscottl	cts.transport_version = path->device->transport_version;
1537195534Sscottl	cts.protocol = path->device->protocol;
1538195534Sscottl	cts.protocol_version = path->device->protocol_version;
1539195534Sscottl	cts.proto_specific.valid = 0;
1540203376Smav	if (ident_buf) {
1541203376Smav		if (path->device->transport == XPORT_ATA) {
1542203376Smav			cts.xport_specific.ata.atapi =
1543203376Smav			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 :
1544203376Smav			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0;
1545203376Smav			cts.xport_specific.ata.valid = CTS_ATA_VALID_ATAPI;
1546203376Smav		} else {
1547203376Smav			cts.xport_specific.sata.atapi =
1548203376Smav			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 :
1549203376Smav			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0;
1550203376Smav			cts.xport_specific.sata.valid = CTS_SATA_VALID_ATAPI;
1551203376Smav		}
1552203376Smav	} else
1553203376Smav		cts.xport_specific.valid = 0;
1554195534Sscottl	xpt_action((union ccb *)&cts);
1555195534Sscottl}
1556195534Sscottl
1557195534Sscottlstatic void
1558195534Sscottlata_action(union ccb *start_ccb)
1559195534Sscottl{
1560195534Sscottl
1561195534Sscottl	switch (start_ccb->ccb_h.func_code) {
1562195534Sscottl	case XPT_SET_TRAN_SETTINGS:
1563195534Sscottl	{
1564199178Smav		ata_set_transfer_settings(&start_ccb->cts,
1565195534Sscottl					   start_ccb->ccb_h.path->device,
1566195534Sscottl					   /*async_update*/FALSE);
1567195534Sscottl		break;
1568195534Sscottl	}
1569195534Sscottl	case XPT_SCAN_BUS:
1570208582Smjacob	case XPT_SCAN_TGT:
1571195534Sscottl		ata_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
1572195534Sscottl		break;
1573195534Sscottl	case XPT_SCAN_LUN:
1574195534Sscottl		ata_scan_lun(start_ccb->ccb_h.path->periph,
1575195534Sscottl			      start_ccb->ccb_h.path, start_ccb->crcn.flags,
1576195534Sscottl			      start_ccb);
1577195534Sscottl		break;
1578195534Sscottl	case XPT_GET_TRAN_SETTINGS:
1579195534Sscottl	{
1580195534Sscottl		struct cam_sim *sim;
1581195534Sscottl
1582195534Sscottl		sim = start_ccb->ccb_h.path->bus->sim;
1583195534Sscottl		(*(sim->sim_action))(sim, start_ccb);
1584195534Sscottl		break;
1585195534Sscottl	}
1586203376Smav	case XPT_SCSI_IO:
1587203376Smav	{
1588203376Smav		struct cam_ed *device;
1589203376Smav		u_int	maxlen = 0;
1590203376Smav
1591203376Smav		device = start_ccb->ccb_h.path->device;
1592203376Smav		if (device->protocol == PROTO_SCSI &&
1593203376Smav		    (device->flags & CAM_DEV_IDENTIFY_DATA_VALID)) {
1594203376Smav			uint16_t p =
1595203376Smav			    device->ident_data.config & ATA_PROTO_MASK;
1596203376Smav
1597203376Smav			maxlen = (p == ATA_PROTO_ATAPI_16) ? 16 :
1598203376Smav			    (p == ATA_PROTO_ATAPI_12) ? 12 : 0;
1599203376Smav		}
1600203376Smav		if (start_ccb->csio.cdb_len > maxlen) {
1601203376Smav			start_ccb->ccb_h.status = CAM_REQ_INVALID;
1602203376Smav			xpt_done(start_ccb);
1603203376Smav			break;
1604203376Smav		}
1605203376Smav		/* FALLTHROUGH */
1606203376Smav	}
1607195534Sscottl	default:
1608195534Sscottl		xpt_action_default(start_ccb);
1609195534Sscottl		break;
1610195534Sscottl	}
1611195534Sscottl}
1612195534Sscottl
1613195534Sscottlstatic void
1614199178Smavata_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
1615195534Sscottl			   int async_update)
1616195534Sscottl{
1617195534Sscottl	struct	ccb_pathinq cpi;
1618195534Sscottl	struct	ccb_trans_settings cur_cts;
1619195534Sscottl	struct	ccb_trans_settings_scsi *scsi;
1620195534Sscottl	struct	ccb_trans_settings_scsi *cur_scsi;
1621195534Sscottl	struct	cam_sim *sim;
1622195534Sscottl	struct	scsi_inquiry_data *inq_data;
1623195534Sscottl
1624195534Sscottl	if (device == NULL) {
1625195534Sscottl		cts->ccb_h.status = CAM_PATH_INVALID;
1626195534Sscottl		xpt_done((union ccb *)cts);
1627195534Sscottl		return;
1628195534Sscottl	}
1629195534Sscottl
1630195534Sscottl	if (cts->protocol == PROTO_UNKNOWN
1631195534Sscottl	 || cts->protocol == PROTO_UNSPECIFIED) {
1632195534Sscottl		cts->protocol = device->protocol;
1633195534Sscottl		cts->protocol_version = device->protocol_version;
1634195534Sscottl	}
1635195534Sscottl
1636195534Sscottl	if (cts->protocol_version == PROTO_VERSION_UNKNOWN
1637195534Sscottl	 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
1638195534Sscottl		cts->protocol_version = device->protocol_version;
1639195534Sscottl
1640195534Sscottl	if (cts->protocol != device->protocol) {
1641195534Sscottl		xpt_print(cts->ccb_h.path, "Uninitialized Protocol %x:%x?\n",
1642195534Sscottl		       cts->protocol, device->protocol);
1643195534Sscottl		cts->protocol = device->protocol;
1644195534Sscottl	}
1645195534Sscottl
1646195534Sscottl	if (cts->protocol_version > device->protocol_version) {
1647195534Sscottl		if (bootverbose) {
1648195534Sscottl			xpt_print(cts->ccb_h.path, "Down reving Protocol "
1649195534Sscottl			    "Version from %d to %d?\n", cts->protocol_version,
1650195534Sscottl			    device->protocol_version);
1651195534Sscottl		}
1652195534Sscottl		cts->protocol_version = device->protocol_version;
1653195534Sscottl	}
1654195534Sscottl
1655195534Sscottl	if (cts->transport == XPORT_UNKNOWN
1656195534Sscottl	 || cts->transport == XPORT_UNSPECIFIED) {
1657195534Sscottl		cts->transport = device->transport;
1658195534Sscottl		cts->transport_version = device->transport_version;
1659195534Sscottl	}
1660195534Sscottl
1661195534Sscottl	if (cts->transport_version == XPORT_VERSION_UNKNOWN
1662195534Sscottl	 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
1663195534Sscottl		cts->transport_version = device->transport_version;
1664195534Sscottl
1665195534Sscottl	if (cts->transport != device->transport) {
1666195534Sscottl		xpt_print(cts->ccb_h.path, "Uninitialized Transport %x:%x?\n",
1667195534Sscottl		    cts->transport, device->transport);
1668195534Sscottl		cts->transport = device->transport;
1669195534Sscottl	}
1670195534Sscottl
1671195534Sscottl	if (cts->transport_version > device->transport_version) {
1672195534Sscottl		if (bootverbose) {
1673195534Sscottl			xpt_print(cts->ccb_h.path, "Down reving Transport "
1674195534Sscottl			    "Version from %d to %d?\n", cts->transport_version,
1675195534Sscottl			    device->transport_version);
1676195534Sscottl		}
1677195534Sscottl		cts->transport_version = device->transport_version;
1678195534Sscottl	}
1679195534Sscottl
1680195534Sscottl	sim = cts->ccb_h.path->bus->sim;
1681195534Sscottl
1682195534Sscottl	/*
1683195534Sscottl	 * Nothing more of interest to do unless
1684195534Sscottl	 * this is a device connected via the
1685195534Sscottl	 * SCSI protocol.
1686195534Sscottl	 */
1687195534Sscottl	if (cts->protocol != PROTO_SCSI) {
1688195534Sscottl		if (async_update == FALSE)
1689195534Sscottl			(*(sim->sim_action))(sim, (union ccb *)cts);
1690195534Sscottl		return;
1691195534Sscottl	}
1692195534Sscottl
1693195534Sscottl	inq_data = &device->inq_data;
1694195534Sscottl	scsi = &cts->proto_specific.scsi;
1695203108Smav	xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NONE);
1696195534Sscottl	cpi.ccb_h.func_code = XPT_PATH_INQ;
1697195534Sscottl	xpt_action((union ccb *)&cpi);
1698195534Sscottl
1699195534Sscottl	/* SCSI specific sanity checking */
1700195534Sscottl	if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
1701195534Sscottl	 || (INQ_DATA_TQ_ENABLED(inq_data)) == 0
1702195534Sscottl	 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
1703195534Sscottl	 || (device->mintags == 0)) {
1704195534Sscottl		/*
1705195534Sscottl		 * Can't tag on hardware that doesn't support tags,
1706195534Sscottl		 * doesn't have it enabled, or has broken tag support.
1707195534Sscottl		 */
1708195534Sscottl		scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1709195534Sscottl	}
1710195534Sscottl
1711195534Sscottl	if (async_update == FALSE) {
1712195534Sscottl		/*
1713195534Sscottl		 * Perform sanity checking against what the
1714195534Sscottl		 * controller and device can do.
1715195534Sscottl		 */
1716203108Smav		xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NONE);
1717195534Sscottl		cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1718195534Sscottl		cur_cts.type = cts->type;
1719195534Sscottl		xpt_action((union ccb *)&cur_cts);
1720195534Sscottl		if ((cur_cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1721195534Sscottl			return;
1722195534Sscottl		}
1723195534Sscottl		cur_scsi = &cur_cts.proto_specific.scsi;
1724195534Sscottl		if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
1725195534Sscottl			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1726195534Sscottl			scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
1727195534Sscottl		}
1728195534Sscottl		if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
1729195534Sscottl			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1730195534Sscottl	}
1731195534Sscottl
1732195534Sscottl	if (cts->type == CTS_TYPE_CURRENT_SETTINGS
1733195534Sscottl	 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
1734195534Sscottl		int device_tagenb;
1735195534Sscottl
1736195534Sscottl		/*
1737195534Sscottl		 * If we are transitioning from tags to no-tags or
1738195534Sscottl		 * vice-versa, we need to carefully freeze and restart
1739195534Sscottl		 * the queue so that we don't overlap tagged and non-tagged
1740195534Sscottl		 * commands.  We also temporarily stop tags if there is
1741195534Sscottl		 * a change in transfer negotiation settings to allow
1742195534Sscottl		 * "tag-less" negotiation.
1743195534Sscottl		 */
1744195534Sscottl		if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
1745195534Sscottl		 || (device->inq_flags & SID_CmdQue) != 0)
1746195534Sscottl			device_tagenb = TRUE;
1747195534Sscottl		else
1748195534Sscottl			device_tagenb = FALSE;
1749195534Sscottl
1750195534Sscottl		if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
1751195534Sscottl		  && device_tagenb == FALSE)
1752195534Sscottl		 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
1753195534Sscottl		  && device_tagenb == TRUE)) {
1754195534Sscottl
1755195534Sscottl			if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
1756195534Sscottl				/*
1757195534Sscottl				 * Delay change to use tags until after a
1758195534Sscottl				 * few commands have gone to this device so
1759195534Sscottl				 * the controller has time to perform transfer
1760195534Sscottl				 * negotiations without tagged messages getting
1761195534Sscottl				 * in the way.
1762195534Sscottl				 */
1763195534Sscottl				device->tag_delay_count = CAM_TAG_DELAY_COUNT;
1764195534Sscottl				device->flags |= CAM_DEV_TAG_AFTER_COUNT;
1765195534Sscottl			} else {
1766199178Smav				xpt_stop_tags(cts->ccb_h.path);
1767195534Sscottl			}
1768195534Sscottl		}
1769195534Sscottl	}
1770195534Sscottl	if (async_update == FALSE)
1771195534Sscottl		(*(sim->sim_action))(sim, (union ccb *)cts);
1772195534Sscottl}
1773195534Sscottl
1774195534Sscottl/*
1775195534Sscottl * Handle any per-device event notifications that require action by the XPT.
1776195534Sscottl */
1777195534Sscottlstatic void
1778195534Sscottlata_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
1779195534Sscottl	      struct cam_ed *device, void *async_arg)
1780195534Sscottl{
1781195534Sscottl	cam_status status;
1782195534Sscottl	struct cam_path newpath;
1783195534Sscottl
1784195534Sscottl	/*
1785195534Sscottl	 * We only need to handle events for real devices.
1786195534Sscottl	 */
1787195534Sscottl	if (target->target_id == CAM_TARGET_WILDCARD
1788195534Sscottl	 || device->lun_id == CAM_LUN_WILDCARD)
1789195534Sscottl		return;
1790195534Sscottl
1791195534Sscottl	/*
1792195534Sscottl	 * We need our own path with wildcards expanded to
1793195534Sscottl	 * handle certain types of events.
1794195534Sscottl	 */
1795195534Sscottl	if ((async_code == AC_SENT_BDR)
1796195534Sscottl	 || (async_code == AC_BUS_RESET)
1797195534Sscottl	 || (async_code == AC_INQ_CHANGED))
1798195534Sscottl		status = xpt_compile_path(&newpath, NULL,
1799195534Sscottl					  bus->path_id,
1800195534Sscottl					  target->target_id,
1801195534Sscottl					  device->lun_id);
1802195534Sscottl	else
1803195534Sscottl		status = CAM_REQ_CMP_ERR;
1804195534Sscottl
1805195534Sscottl	if (status == CAM_REQ_CMP) {
1806195534Sscottl		if (async_code == AC_INQ_CHANGED) {
1807195534Sscottl			/*
1808195534Sscottl			 * We've sent a start unit command, or
1809195534Sscottl			 * something similar to a device that
1810195534Sscottl			 * may have caused its inquiry data to
1811195534Sscottl			 * change. So we re-scan the device to
1812195534Sscottl			 * refresh the inquiry data for it.
1813195534Sscottl			 */
1814195534Sscottl			ata_scan_lun(newpath.periph, &newpath,
1815195534Sscottl				     CAM_EXPECT_INQ_CHANGE, NULL);
1816203108Smav		} else {
1817203108Smav			/* We need to reinitialize device after reset. */
1818203108Smav			ata_scan_lun(newpath.periph, &newpath,
1819203108Smav				     0, NULL);
1820195534Sscottl		}
1821195534Sscottl		xpt_release_path(&newpath);
1822198748Smav	} else if (async_code == AC_LOST_DEVICE &&
1823198748Smav	    (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
1824195534Sscottl		device->flags |= CAM_DEV_UNCONFIGURED;
1825198748Smav		xpt_release_device(device);
1826195534Sscottl	} else if (async_code == AC_TRANSFER_NEG) {
1827195534Sscottl		struct ccb_trans_settings *settings;
1828195534Sscottl
1829195534Sscottl		settings = (struct ccb_trans_settings *)async_arg;
1830199178Smav		ata_set_transfer_settings(settings, device,
1831195534Sscottl					  /*async_update*/TRUE);
1832195534Sscottl	}
1833195534Sscottl}
1834195534Sscottl
1835204220Smavstatic void
1836204220Smavata_announce_periph(struct cam_periph *periph)
1837204220Smav{
1838204220Smav	struct	ccb_pathinq cpi;
1839204220Smav	struct	ccb_trans_settings cts;
1840204220Smav	struct	cam_path *path = periph->path;
1841204220Smav	u_int	speed;
1842204220Smav	u_int	mb;
1843204220Smav
1844204220Smav	mtx_assert(periph->sim->mtx, MA_OWNED);
1845204220Smav
1846204220Smav	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
1847204220Smav	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1848204220Smav	cts.type = CTS_TYPE_CURRENT_SETTINGS;
1849204220Smav	xpt_action((union ccb*)&cts);
1850204220Smav	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
1851204220Smav		return;
1852204220Smav	/* Ask the SIM for its base transfer speed */
1853204220Smav	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
1854204220Smav	cpi.ccb_h.func_code = XPT_PATH_INQ;
1855204220Smav	xpt_action((union ccb *)&cpi);
1856204220Smav	/* Report connection speed */
1857204220Smav	speed = cpi.base_transfer_speed;
1858204220Smav	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_ATA) {
1859204220Smav		struct	ccb_trans_settings_ata *ata =
1860204220Smav		    &cts.xport_specific.ata;
1861204220Smav
1862204220Smav		if (ata->valid & CTS_ATA_VALID_MODE)
1863204220Smav			speed = ata_mode2speed(ata->mode);
1864204220Smav	}
1865204220Smav	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SATA) {
1866204220Smav		struct	ccb_trans_settings_sata *sata =
1867204220Smav		    &cts.xport_specific.sata;
1868204220Smav
1869204220Smav		if (sata->valid & CTS_SATA_VALID_REVISION)
1870204220Smav			speed = ata_revision2speed(sata->revision);
1871204220Smav	}
1872204220Smav	mb = speed / 1000;
1873204220Smav	if (mb > 0)
1874204220Smav		printf("%s%d: %d.%03dMB/s transfers",
1875204220Smav		       periph->periph_name, periph->unit_number,
1876204220Smav		       mb, speed % 1000);
1877204220Smav	else
1878204220Smav		printf("%s%d: %dKB/s transfers", periph->periph_name,
1879204220Smav		       periph->unit_number, speed);
1880204220Smav	/* Report additional information about connection */
1881204220Smav	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_ATA) {
1882204220Smav		struct ccb_trans_settings_ata *ata =
1883204220Smav		    &cts.xport_specific.ata;
1884204220Smav
1885204220Smav		printf(" (");
1886204220Smav		if (ata->valid & CTS_ATA_VALID_MODE)
1887204220Smav			printf("%s, ", ata_mode2string(ata->mode));
1888204220Smav		if ((ata->valid & CTS_ATA_VALID_ATAPI) && ata->atapi != 0)
1889204220Smav			printf("ATAPI %dbytes, ", ata->atapi);
1890204220Smav		if (ata->valid & CTS_ATA_VALID_BYTECOUNT)
1891204220Smav			printf("PIO %dbytes", ata->bytecount);
1892204220Smav		printf(")");
1893204220Smav	}
1894204220Smav	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SATA) {
1895204220Smav		struct ccb_trans_settings_sata *sata =
1896204220Smav		    &cts.xport_specific.sata;
1897204220Smav
1898204220Smav		printf(" (");
1899204220Smav		if (sata->valid & CTS_SATA_VALID_REVISION)
1900204220Smav			printf("SATA %d.x, ", sata->revision);
1901204220Smav		else
1902204220Smav			printf("SATA, ");
1903204220Smav		if (sata->valid & CTS_SATA_VALID_MODE)
1904204220Smav			printf("%s, ", ata_mode2string(sata->mode));
1905204220Smav		if ((sata->valid & CTS_ATA_VALID_ATAPI) && sata->atapi != 0)
1906204220Smav			printf("ATAPI %dbytes, ", sata->atapi);
1907204220Smav		if (sata->valid & CTS_SATA_VALID_BYTECOUNT)
1908204220Smav			printf("PIO %dbytes", sata->bytecount);
1909204220Smav		printf(")");
1910204220Smav	}
1911204220Smav	printf("\n");
1912204220Smav}
1913204220Smav
1914