ata_xpt.c revision 200171
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 200171 2009-12-06 00:10:13Z 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/md5.h>
41195534Sscottl#include <sys/interrupt.h>
42195534Sscottl#include <sys/sbuf.h>
43195534Sscottl
44195534Sscottl#include <sys/lock.h>
45195534Sscottl#include <sys/mutex.h>
46195534Sscottl#include <sys/sysctl.h>
47195534Sscottl
48195534Sscottl#ifdef PC98
49195534Sscottl#include <pc98/pc98/pc98_machdep.h>	/* geometry translation */
50195534Sscottl#endif
51195534Sscottl
52195534Sscottl#include <cam/cam.h>
53195534Sscottl#include <cam/cam_ccb.h>
54195534Sscottl#include <cam/cam_queue.h>
55195534Sscottl#include <cam/cam_periph.h>
56195534Sscottl#include <cam/cam_sim.h>
57195534Sscottl#include <cam/cam_xpt.h>
58195534Sscottl#include <cam/cam_xpt_sim.h>
59195534Sscottl#include <cam/cam_xpt_periph.h>
60195534Sscottl#include <cam/cam_xpt_internal.h>
61195534Sscottl#include <cam/cam_debug.h>
62195534Sscottl
63195534Sscottl#include <cam/scsi/scsi_all.h>
64195534Sscottl#include <cam/scsi/scsi_message.h>
65195534Sscottl#include <cam/ata/ata_all.h>
66195534Sscottl#include <machine/stdarg.h>	/* for xpt_print below */
67195534Sscottl#include "opt_cam.h"
68195534Sscottl
69199178Smavstruct ata_quirk_entry {
70195534Sscottl	struct scsi_inquiry_pattern inq_pat;
71195534Sscottl	u_int8_t quirks;
72199178Smav#define	CAM_QUIRK_MAXTAGS	0x01
73195534Sscottl	u_int maxtags;
74195534Sscottl};
75195534Sscottl
76195534Sscottlstatic periph_init_t probe_periph_init;
77195534Sscottl
78195534Sscottlstatic struct periph_driver probe_driver =
79195534Sscottl{
80195653Smav	probe_periph_init, "aprobe",
81198708Smav	TAILQ_HEAD_INITIALIZER(probe_driver.units), /* generation */ 0,
82198708Smav	CAM_PERIPH_DRV_EARLY
83195534Sscottl};
84195534Sscottl
85195653SmavPERIPHDRIVER_DECLARE(aprobe, probe_driver);
86195534Sscottl
87195534Sscottltypedef enum {
88195534Sscottl	PROBE_RESET,
89195534Sscottl	PROBE_IDENTIFY,
90195534Sscottl	PROBE_SETMODE,
91198708Smav	PROBE_SET_MULTI,
92195534Sscottl	PROBE_INQUIRY,
93195534Sscottl	PROBE_FULL_INQUIRY,
94195534Sscottl	PROBE_PM_PID,
95195534Sscottl	PROBE_PM_PRV,
96195534Sscottl	PROBE_INVALID
97195534Sscottl} probe_action;
98195534Sscottl
99195534Sscottlstatic char *probe_action_text[] = {
100195534Sscottl	"PROBE_RESET",
101195534Sscottl	"PROBE_IDENTIFY",
102195534Sscottl	"PROBE_SETMODE",
103198708Smav	"PROBE_SET_MULTI",
104195534Sscottl	"PROBE_INQUIRY",
105195534Sscottl	"PROBE_FULL_INQUIRY",
106195534Sscottl	"PROBE_PM_PID",
107195534Sscottl	"PROBE_PM_PRV",
108195534Sscottl	"PROBE_INVALID"
109195534Sscottl};
110195534Sscottl
111195534Sscottl#define PROBE_SET_ACTION(softc, newaction)	\
112195534Sscottldo {									\
113195534Sscottl	char **text;							\
114195534Sscottl	text = probe_action_text;					\
115195534Sscottl	CAM_DEBUG((softc)->periph->path, CAM_DEBUG_INFO,		\
116195534Sscottl	    ("Probe %s to %s\n", text[(softc)->action],			\
117195534Sscottl	    text[(newaction)]));					\
118195534Sscottl	(softc)->action = (newaction);					\
119195534Sscottl} while(0)
120195534Sscottl
121195534Sscottltypedef enum {
122195534Sscottl	PROBE_NO_ANNOUNCE	= 0x04
123195534Sscottl} probe_flags;
124195534Sscottl
125195534Sscottltypedef struct {
126195534Sscottl	TAILQ_HEAD(, ccb_hdr) request_ccbs;
127195534Sscottl	probe_action	action;
128195534Sscottl	union ccb	saved_ccb;
129195534Sscottl	probe_flags	flags;
130195534Sscottl	u_int8_t	digest[16];
131195534Sscottl	uint32_t	pm_pid;
132195534Sscottl	uint32_t	pm_prv;
133195534Sscottl	struct cam_periph *periph;
134195534Sscottl} probe_softc;
135195534Sscottl
136199178Smavstatic struct ata_quirk_entry ata_quirk_table[] =
137195534Sscottl{
138195534Sscottl	{
139195534Sscottl		/* Default tagged queuing parameters for all devices */
140195534Sscottl		{
141195534Sscottl		  T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
142195534Sscottl		  /*vendor*/"*", /*product*/"*", /*revision*/"*"
143195534Sscottl		},
144199178Smav		/*quirks*/0, /*maxtags*/0
145195534Sscottl	},
146195534Sscottl};
147195534Sscottl
148199178Smavstatic const int ata_quirk_table_size =
149199178Smav	sizeof(ata_quirk_table) / sizeof(*ata_quirk_table);
150195534Sscottl
151195534Sscottlstatic cam_status	proberegister(struct cam_periph *periph,
152195534Sscottl				      void *arg);
153195534Sscottlstatic void	 probeschedule(struct cam_periph *probe_periph);
154195534Sscottlstatic void	 probestart(struct cam_periph *periph, union ccb *start_ccb);
155195534Sscottl//static void	 proberequestdefaultnegotiation(struct cam_periph *periph);
156195534Sscottl//static int       proberequestbackoff(struct cam_periph *periph,
157195534Sscottl//				     struct cam_ed *device);
158195534Sscottlstatic void	 probedone(struct cam_periph *periph, union ccb *done_ccb);
159195534Sscottlstatic void	 probecleanup(struct cam_periph *periph);
160199178Smavstatic void	 ata_find_quirk(struct cam_ed *device);
161195534Sscottlstatic void	 ata_scan_bus(struct cam_periph *periph, union ccb *ccb);
162195534Sscottlstatic void	 ata_scan_lun(struct cam_periph *periph,
163195534Sscottl			       struct cam_path *path, cam_flags flags,
164195534Sscottl			       union ccb *ccb);
165195534Sscottlstatic void	 xptscandone(struct cam_periph *periph, union ccb *done_ccb);
166195534Sscottlstatic struct cam_ed *
167195534Sscottl		 ata_alloc_device(struct cam_eb *bus, struct cam_et *target,
168195534Sscottl				   lun_id_t lun_id);
169195534Sscottlstatic void	 ata_device_transport(struct cam_path *path);
170199178Smavstatic void	 ata_set_transfer_settings(struct ccb_trans_settings *cts,
171195534Sscottl					    struct cam_ed *device,
172195534Sscottl					    int async_update);
173195534Sscottlstatic void	 ata_dev_async(u_int32_t async_code,
174195534Sscottl				struct cam_eb *bus,
175195534Sscottl				struct cam_et *target,
176195534Sscottl				struct cam_ed *device,
177195534Sscottl				void *async_arg);
178195534Sscottlstatic void	 ata_action(union ccb *start_ccb);
179195534Sscottl
180195534Sscottlstatic struct xpt_xport ata_xport = {
181195534Sscottl	.alloc_device = ata_alloc_device,
182195534Sscottl	.action = ata_action,
183195534Sscottl	.async = ata_dev_async,
184195534Sscottl};
185195534Sscottl
186195534Sscottlstruct xpt_xport *
187195534Sscottlata_get_xport(void)
188195534Sscottl{
189195534Sscottl	return (&ata_xport);
190195534Sscottl}
191195534Sscottl
192195534Sscottlstatic void
193195534Sscottlprobe_periph_init()
194195534Sscottl{
195195534Sscottl}
196195534Sscottl
197195534Sscottlstatic cam_status
198195534Sscottlproberegister(struct cam_periph *periph, void *arg)
199195534Sscottl{
200195534Sscottl	union ccb *request_ccb;	/* CCB representing the probe request */
201195534Sscottl	cam_status status;
202195534Sscottl	probe_softc *softc;
203195534Sscottl
204195534Sscottl	request_ccb = (union ccb *)arg;
205195534Sscottl	if (periph == NULL) {
206195534Sscottl		printf("proberegister: periph was NULL!!\n");
207195534Sscottl		return(CAM_REQ_CMP_ERR);
208195534Sscottl	}
209195534Sscottl
210195534Sscottl	if (request_ccb == NULL) {
211195534Sscottl		printf("proberegister: no probe CCB, "
212195534Sscottl		       "can't register device\n");
213195534Sscottl		return(CAM_REQ_CMP_ERR);
214195534Sscottl	}
215195534Sscottl
216195534Sscottl	softc = (probe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_NOWAIT);
217195534Sscottl
218195534Sscottl	if (softc == NULL) {
219195534Sscottl		printf("proberegister: Unable to probe new device. "
220195534Sscottl		       "Unable to allocate softc\n");
221195534Sscottl		return(CAM_REQ_CMP_ERR);
222195534Sscottl	}
223195534Sscottl	TAILQ_INIT(&softc->request_ccbs);
224195534Sscottl	TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
225195534Sscottl			  periph_links.tqe);
226195534Sscottl	softc->flags = 0;
227195534Sscottl	periph->softc = softc;
228195534Sscottl	softc->periph = periph;
229195534Sscottl	softc->action = PROBE_INVALID;
230195534Sscottl	status = cam_periph_acquire(periph);
231195534Sscottl	if (status != CAM_REQ_CMP) {
232195534Sscottl		return (status);
233195534Sscottl	}
234195534Sscottl
235195534Sscottl
236195534Sscottl	/*
237195534Sscottl	 * Ensure we've waited at least a bus settle
238195534Sscottl	 * delay before attempting to probe the device.
239195534Sscottl	 * For HBAs that don't do bus resets, this won't make a difference.
240195534Sscottl	 */
241195534Sscottl	cam_periph_freeze_after_event(periph, &periph->path->bus->last_reset,
242195534Sscottl				      scsi_delay);
243195534Sscottl	probeschedule(periph);
244195534Sscottl	return(CAM_REQ_CMP);
245195534Sscottl}
246195534Sscottl
247195534Sscottlstatic void
248195534Sscottlprobeschedule(struct cam_periph *periph)
249195534Sscottl{
250195534Sscottl	struct ccb_pathinq cpi;
251195534Sscottl	union ccb *ccb;
252195534Sscottl	probe_softc *softc;
253195534Sscottl
254195534Sscottl	softc = (probe_softc *)periph->softc;
255195534Sscottl	ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
256195534Sscottl
257198382Smav	xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
258195534Sscottl	cpi.ccb_h.func_code = XPT_PATH_INQ;
259195534Sscottl	xpt_action((union ccb *)&cpi);
260195534Sscottl
261198389Smav	if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) ||
262198389Smav	    periph->path->device->protocol == PROTO_SATAPM)
263195534Sscottl		PROBE_SET_ACTION(softc, PROBE_RESET);
264195534Sscottl	else
265195534Sscottl		PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
266195534Sscottl
267195534Sscottl	if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
268195534Sscottl		softc->flags |= PROBE_NO_ANNOUNCE;
269195534Sscottl	else
270195534Sscottl		softc->flags &= ~PROBE_NO_ANNOUNCE;
271195534Sscottl
272195534Sscottl	xpt_schedule(periph, ccb->ccb_h.pinfo.priority);
273195534Sscottl}
274195534Sscottl
275195534Sscottlstatic void
276195534Sscottlprobestart(struct cam_periph *periph, union ccb *start_ccb)
277195534Sscottl{
278199747Smav	struct ccb_trans_settings cts;
279195534Sscottl	struct ccb_ataio *ataio;
280195534Sscottl	struct ccb_scsiio *csio;
281195534Sscottl	probe_softc *softc;
282198708Smav	struct cam_path *path;
283198708Smav	struct ata_params *ident_buf;
284195534Sscottl
285195534Sscottl	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
286195534Sscottl
287195534Sscottl	softc = (probe_softc *)periph->softc;
288198708Smav	path = start_ccb->ccb_h.path;
289195534Sscottl	ataio = &start_ccb->ataio;
290195534Sscottl	csio = &start_ccb->csio;
291198708Smav	ident_buf = &periph->path->device->ident_data;
292195534Sscottl
293195534Sscottl	switch (softc->action) {
294195534Sscottl	case PROBE_RESET:
295195534Sscottl		cam_fill_ataio(ataio,
296195534Sscottl		      0,
297195534Sscottl		      probedone,
298195534Sscottl		      /*flags*/CAM_DIR_NONE,
299198389Smav		      0,
300195534Sscottl		      /*data_ptr*/NULL,
301195534Sscottl		      /*dxfer_len*/0,
302195534Sscottl		      (start_ccb->ccb_h.target_id == 15 ? 3 : 15) * 1000);
303195534Sscottl		ata_reset_cmd(ataio);
304195534Sscottl		break;
305195534Sscottl	case PROBE_IDENTIFY:
306195534Sscottl		if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
307195534Sscottl			/* Prepare check that it is the same device. */
308195534Sscottl			MD5_CTX context;
309195534Sscottl
310195534Sscottl			MD5Init(&context);
311195534Sscottl			MD5Update(&context,
312195534Sscottl			    (unsigned char *)ident_buf->model,
313195534Sscottl			    sizeof(ident_buf->model));
314195534Sscottl			MD5Update(&context,
315195534Sscottl			    (unsigned char *)ident_buf->revision,
316195534Sscottl			    sizeof(ident_buf->revision));
317195534Sscottl			MD5Update(&context,
318195534Sscottl			    (unsigned char *)ident_buf->serial,
319195534Sscottl			    sizeof(ident_buf->serial));
320195534Sscottl			MD5Final(softc->digest, &context);
321195534Sscottl		}
322195534Sscottl		cam_fill_ataio(ataio,
323195534Sscottl		      1,
324195534Sscottl		      probedone,
325195534Sscottl		      /*flags*/CAM_DIR_IN,
326198389Smav		      0,
327195534Sscottl		      /*data_ptr*/(u_int8_t *)ident_buf,
328195534Sscottl		      /*dxfer_len*/sizeof(struct ata_params),
329195534Sscottl		      30 * 1000);
330195534Sscottl		if (periph->path->device->protocol == PROTO_ATA)
331196659Smav			ata_28bit_cmd(ataio, ATA_ATA_IDENTIFY, 0, 0, 0);
332195534Sscottl		else
333196659Smav			ata_28bit_cmd(ataio, ATA_ATAPI_IDENTIFY, 0, 0, 0);
334195534Sscottl		break;
335195534Sscottl	case PROBE_SETMODE:
336199747Smav	{
337199747Smav		int mode, wantmode;
338199747Smav
339199747Smav		mode = 0;
340199747Smav		/* Fetch user modes from SIM. */
341199747Smav		bzero(&cts, sizeof(cts));
342199747Smav		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
343199747Smav		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
344199747Smav		cts.type = CTS_TYPE_USER_SETTINGS;
345199747Smav		xpt_action((union ccb *)&cts);
346199747Smav		if (path->device->transport == XPORT_ATA) {
347199747Smav			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_MODE)
348199747Smav				mode = cts.xport_specific.ata.mode;
349199747Smav		} else {
350199799Smav			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_MODE)
351199747Smav				mode = cts.xport_specific.sata.mode;
352199747Smav		}
353199747Smavnegotiate:
354199747Smav		/* Honor device capabilities. */
355199747Smav		wantmode = mode = ata_max_mode(ident_buf, mode);
356199747Smav		/* Report modes to SIM. */
357199747Smav		bzero(&cts, sizeof(cts));
358199747Smav		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
359199747Smav		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
360199747Smav		cts.type = CTS_TYPE_CURRENT_SETTINGS;
361199747Smav		if (path->device->transport == XPORT_ATA) {
362199747Smav			cts.xport_specific.ata.mode = mode;
363199747Smav			cts.xport_specific.ata.valid = CTS_ATA_VALID_MODE;
364199747Smav		} else {
365199747Smav			cts.xport_specific.sata.mode = mode;
366199747Smav			cts.xport_specific.sata.valid = CTS_SATA_VALID_MODE;
367199747Smav		}
368199747Smav		xpt_action((union ccb *)&cts);
369200171Smav		/* Fetch current modes from SIM. */
370199747Smav		bzero(&cts, sizeof(cts));
371199747Smav		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
372199747Smav		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
373199747Smav		cts.type = CTS_TYPE_CURRENT_SETTINGS;
374199747Smav		xpt_action((union ccb *)&cts);
375199747Smav		if (path->device->transport == XPORT_ATA) {
376199747Smav			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_MODE)
377199747Smav				mode = cts.xport_specific.ata.mode;
378199747Smav		} else {
379199747Smav			if (cts.xport_specific.ata.valid & CTS_SATA_VALID_MODE)
380199747Smav				mode = cts.xport_specific.sata.mode;
381199747Smav		}
382199747Smav		/* If SIM disagree - renegotiate. */
383199747Smav		if (mode != wantmode)
384199747Smav			goto negotiate;
385195534Sscottl		cam_fill_ataio(ataio,
386195534Sscottl		      1,
387195534Sscottl		      probedone,
388196353Smav		      /*flags*/CAM_DIR_NONE,
389196353Smav		      0,
390196353Smav		      /*data_ptr*/NULL,
391196353Smav		      /*dxfer_len*/0,
392195534Sscottl		      30 * 1000);
393199747Smav		ata_28bit_cmd(ataio, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
394195534Sscottl		break;
395199747Smav	}
396198708Smav	case PROBE_SET_MULTI:
397198708Smav	{
398200171Smav		u_int sectors, bytecount;
399198708Smav
400200171Smav		bytecount = 8192;	/* SATA maximum */
401200171Smav		/* Fetch user bytecount from SIM. */
402200171Smav		bzero(&cts, sizeof(cts));
403200171Smav		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
404200171Smav		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
405200171Smav		cts.type = CTS_TYPE_USER_SETTINGS;
406200171Smav		xpt_action((union ccb *)&cts);
407200171Smav		if (path->device->transport == XPORT_ATA) {
408200171Smav			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
409200171Smav				bytecount = cts.xport_specific.ata.bytecount;
410200171Smav		} else {
411200171Smav			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
412200171Smav				bytecount = cts.xport_specific.sata.bytecount;
413200171Smav		}
414200171Smav		/* Honor device capabilities. */
415200171Smav		sectors = max(1, min(ident_buf->sectors_intr & 0xff,
416200171Smav		    bytecount / ata_logical_sector_size(ident_buf)));
417198708Smav		/* Report bytecount to SIM. */
418198708Smav		bzero(&cts, sizeof(cts));
419198708Smav		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
420198708Smav		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
421198708Smav		cts.type = CTS_TYPE_CURRENT_SETTINGS;
422198708Smav		if (path->device->transport == XPORT_ATA) {
423198897Smav			cts.xport_specific.ata.bytecount = sectors *
424198897Smav			    ata_logical_sector_size(ident_buf);
425198708Smav			cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT;
426198708Smav		} else {
427198897Smav			cts.xport_specific.sata.bytecount = sectors *
428198897Smav			    ata_logical_sector_size(ident_buf);
429198708Smav			cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT;
430198708Smav		}
431198708Smav		xpt_action((union ccb *)&cts);
432200171Smav		/* Fetch current bytecount from SIM. */
433200171Smav		bzero(&cts, sizeof(cts));
434200171Smav		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
435200171Smav		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
436200171Smav		cts.type = CTS_TYPE_CURRENT_SETTINGS;
437200171Smav		xpt_action((union ccb *)&cts);
438200171Smav		if (path->device->transport == XPORT_ATA) {
439200171Smav			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
440200171Smav				bytecount = cts.xport_specific.ata.bytecount;
441200171Smav		} else {
442200171Smav			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
443200171Smav				bytecount = cts.xport_specific.sata.bytecount;
444200171Smav		}
445200171Smav		sectors = bytecount / ata_logical_sector_size(ident_buf);
446198708Smav
447198708Smav		cam_fill_ataio(ataio,
448198708Smav		    1,
449198708Smav		    probedone,
450198708Smav		    CAM_DIR_NONE,
451198708Smav		    0,
452198708Smav		    NULL,
453198708Smav		    0,
454198708Smav		    30*1000);
455198708Smav		ata_28bit_cmd(ataio, ATA_SET_MULTI, 0, 0, sectors);
456198708Smav		break;
457195534Sscottl	}
458195534Sscottl	case PROBE_INQUIRY:
459200171Smav	{
460200171Smav		u_int bytecount;
461200171Smav
462200171Smav		bytecount = 8192;	/* SATA maximum */
463200171Smav		/* Fetch user bytecount from SIM. */
464200171Smav		bzero(&cts, sizeof(cts));
465200171Smav		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
466200171Smav		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
467200171Smav		cts.type = CTS_TYPE_USER_SETTINGS;
468200171Smav		xpt_action((union ccb *)&cts);
469200171Smav		if (path->device->transport == XPORT_ATA) {
470200171Smav			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
471200171Smav				bytecount = cts.xport_specific.ata.bytecount;
472200171Smav		} else {
473200171Smav			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
474200171Smav				bytecount = cts.xport_specific.sata.bytecount;
475200171Smav		}
476200171Smav		/* Honor device capabilities. */
477200171Smav		bytecount &= ~1;
478200171Smav		bytecount = max(2, min(65534, bytecount));
479200171Smav		if (ident_buf->satacapabilities != 0x0000 &&
480200171Smav		    ident_buf->satacapabilities != 0xffff) {
481200171Smav			bytecount = min(8192, bytecount);
482200171Smav		}
483200171Smav		/* Report bytecount to SIM. */
484200171Smav		bzero(&cts, sizeof(cts));
485200171Smav		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
486200171Smav		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
487200171Smav		cts.type = CTS_TYPE_CURRENT_SETTINGS;
488200171Smav		if (path->device->transport == XPORT_ATA) {
489200171Smav			cts.xport_specific.ata.bytecount = bytecount;
490200171Smav			cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT;
491200171Smav		} else {
492200171Smav			cts.xport_specific.sata.bytecount = bytecount;
493200171Smav			cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT;
494200171Smav		}
495200171Smav		xpt_action((union ccb *)&cts);
496200171Smav		/* FALLTHROUGH */
497200171Smav	}
498195534Sscottl	case PROBE_FULL_INQUIRY:
499195534Sscottl	{
500195534Sscottl		u_int inquiry_len;
501195534Sscottl		struct scsi_inquiry_data *inq_buf =
502195534Sscottl		    &periph->path->device->inq_data;
503195534Sscottl
504195534Sscottl		if (softc->action == PROBE_INQUIRY)
505195534Sscottl			inquiry_len = SHORT_INQUIRY_LENGTH;
506195534Sscottl		else
507195534Sscottl			inquiry_len = SID_ADDITIONAL_LENGTH(inq_buf);
508195534Sscottl		/*
509195534Sscottl		 * Some parallel SCSI devices fail to send an
510195534Sscottl		 * ignore wide residue message when dealing with
511195534Sscottl		 * odd length inquiry requests.  Round up to be
512195534Sscottl		 * safe.
513195534Sscottl		 */
514195534Sscottl		inquiry_len = roundup2(inquiry_len, 2);
515195534Sscottl		scsi_inquiry(csio,
516195534Sscottl			     /*retries*/1,
517195534Sscottl			     probedone,
518195534Sscottl			     MSG_SIMPLE_Q_TAG,
519195534Sscottl			     (u_int8_t *)inq_buf,
520195534Sscottl			     inquiry_len,
521195534Sscottl			     /*evpd*/FALSE,
522195534Sscottl			     /*page_code*/0,
523195534Sscottl			     SSD_MIN_SIZE,
524195534Sscottl			     /*timeout*/60 * 1000);
525195534Sscottl		break;
526195534Sscottl	}
527195534Sscottl	case PROBE_PM_PID:
528195534Sscottl		cam_fill_ataio(ataio,
529195534Sscottl		      1,
530195534Sscottl		      probedone,
531195534Sscottl		      /*flags*/CAM_DIR_NONE,
532198389Smav		      0,
533195534Sscottl		      /*data_ptr*/NULL,
534195534Sscottl		      /*dxfer_len*/0,
535195534Sscottl		      10 * 1000);
536195534Sscottl		ata_pm_read_cmd(ataio, 0, 15);
537195534Sscottl		break;
538195534Sscottl	case PROBE_PM_PRV:
539195534Sscottl		cam_fill_ataio(ataio,
540195534Sscottl		      1,
541195534Sscottl		      probedone,
542195534Sscottl		      /*flags*/CAM_DIR_NONE,
543198389Smav		      0,
544195534Sscottl		      /*data_ptr*/NULL,
545195534Sscottl		      /*dxfer_len*/0,
546195534Sscottl		      10 * 1000);
547195534Sscottl		ata_pm_read_cmd(ataio, 1, 15);
548195534Sscottl		break;
549195534Sscottl	case PROBE_INVALID:
550198708Smav		CAM_DEBUG(path, CAM_DEBUG_INFO,
551195534Sscottl		    ("probestart: invalid action state\n"));
552195534Sscottl	default:
553195534Sscottl		break;
554195534Sscottl	}
555195534Sscottl	xpt_action(start_ccb);
556195534Sscottl}
557195534Sscottl#if 0
558195534Sscottlstatic void
559195534Sscottlproberequestdefaultnegotiation(struct cam_periph *periph)
560195534Sscottl{
561195534Sscottl	struct ccb_trans_settings cts;
562195534Sscottl
563198382Smav	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
564195534Sscottl	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
565195534Sscottl	cts.type = CTS_TYPE_USER_SETTINGS;
566195534Sscottl	xpt_action((union ccb *)&cts);
567195534Sscottl	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
568195534Sscottl		return;
569195534Sscottl	}
570195534Sscottl	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
571195534Sscottl	cts.type = CTS_TYPE_CURRENT_SETTINGS;
572195534Sscottl	xpt_action((union ccb *)&cts);
573195534Sscottl}
574195534Sscottl
575195534Sscottl/*
576195534Sscottl * Backoff Negotiation Code- only pertinent for SPI devices.
577195534Sscottl */
578195534Sscottlstatic int
579195534Sscottlproberequestbackoff(struct cam_periph *periph, struct cam_ed *device)
580195534Sscottl{
581195534Sscottl	struct ccb_trans_settings cts;
582195534Sscottl	struct ccb_trans_settings_spi *spi;
583195534Sscottl
584195534Sscottl	memset(&cts, 0, sizeof (cts));
585198382Smav	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
586195534Sscottl	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
587195534Sscottl	cts.type = CTS_TYPE_CURRENT_SETTINGS;
588195534Sscottl	xpt_action((union ccb *)&cts);
589195534Sscottl	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
590195534Sscottl		if (bootverbose) {
591195534Sscottl			xpt_print(periph->path,
592195534Sscottl			    "failed to get current device settings\n");
593195534Sscottl		}
594195534Sscottl		return (0);
595195534Sscottl	}
596195534Sscottl	if (cts.transport != XPORT_SPI) {
597195534Sscottl		if (bootverbose) {
598195534Sscottl			xpt_print(periph->path, "not SPI transport\n");
599195534Sscottl		}
600195534Sscottl		return (0);
601195534Sscottl	}
602195534Sscottl	spi = &cts.xport_specific.spi;
603195534Sscottl
604195534Sscottl	/*
605195534Sscottl	 * We cannot renegotiate sync rate if we don't have one.
606195534Sscottl	 */
607195534Sscottl	if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
608195534Sscottl		if (bootverbose) {
609195534Sscottl			xpt_print(periph->path, "no sync rate known\n");
610195534Sscottl		}
611195534Sscottl		return (0);
612195534Sscottl	}
613195534Sscottl
614195534Sscottl	/*
615195534Sscottl	 * We'll assert that we don't have to touch PPR options- the
616195534Sscottl	 * SIM will see what we do with period and offset and adjust
617195534Sscottl	 * the PPR options as appropriate.
618195534Sscottl	 */
619195534Sscottl
620195534Sscottl	/*
621195534Sscottl	 * A sync rate with unknown or zero offset is nonsensical.
622195534Sscottl	 * A sync period of zero means Async.
623195534Sscottl	 */
624195534Sscottl	if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0
625195534Sscottl	 || spi->sync_offset == 0 || spi->sync_period == 0) {
626195534Sscottl		if (bootverbose) {
627195534Sscottl			xpt_print(periph->path, "no sync rate available\n");
628195534Sscottl		}
629195534Sscottl		return (0);
630195534Sscottl	}
631195534Sscottl
632195534Sscottl	if (device->flags & CAM_DEV_DV_HIT_BOTTOM) {
633195534Sscottl		CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
634195534Sscottl		    ("hit async: giving up on DV\n"));
635195534Sscottl		return (0);
636195534Sscottl	}
637195534Sscottl
638195534Sscottl
639195534Sscottl	/*
640195534Sscottl	 * Jump sync_period up by one, but stop at 5MHz and fall back to Async.
641195534Sscottl	 * We don't try to remember 'last' settings to see if the SIM actually
642195534Sscottl	 * gets into the speed we want to set. We check on the SIM telling
643195534Sscottl	 * us that a requested speed is bad, but otherwise don't try and
644195534Sscottl	 * check the speed due to the asynchronous and handshake nature
645195534Sscottl	 * of speed setting.
646195534Sscottl	 */
647195534Sscottl	spi->valid = CTS_SPI_VALID_SYNC_RATE | CTS_SPI_VALID_SYNC_OFFSET;
648195534Sscottl	for (;;) {
649195534Sscottl		spi->sync_period++;
650195534Sscottl		if (spi->sync_period >= 0xf) {
651195534Sscottl			spi->sync_period = 0;
652195534Sscottl			spi->sync_offset = 0;
653195534Sscottl			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
654195534Sscottl			    ("setting to async for DV\n"));
655195534Sscottl			/*
656195534Sscottl			 * Once we hit async, we don't want to try
657195534Sscottl			 * any more settings.
658195534Sscottl			 */
659195534Sscottl			device->flags |= CAM_DEV_DV_HIT_BOTTOM;
660195534Sscottl		} else if (bootverbose) {
661195534Sscottl			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
662195534Sscottl			    ("DV: period 0x%x\n", spi->sync_period));
663195534Sscottl			printf("setting period to 0x%x\n", spi->sync_period);
664195534Sscottl		}
665195534Sscottl		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
666195534Sscottl		cts.type = CTS_TYPE_CURRENT_SETTINGS;
667195534Sscottl		xpt_action((union ccb *)&cts);
668195534Sscottl		if ((cts.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
669195534Sscottl			break;
670195534Sscottl		}
671195534Sscottl		CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
672195534Sscottl		    ("DV: failed to set period 0x%x\n", spi->sync_period));
673195534Sscottl		if (spi->sync_period == 0) {
674195534Sscottl			return (0);
675195534Sscottl		}
676195534Sscottl	}
677195534Sscottl	return (1);
678195534Sscottl}
679195534Sscottl#endif
680195534Sscottlstatic void
681195534Sscottlprobedone(struct cam_periph *periph, union ccb *done_ccb)
682195534Sscottl{
683199747Smav	struct ccb_trans_settings cts;
684195534Sscottl	struct ata_params *ident_buf;
685195534Sscottl	probe_softc *softc;
686195534Sscottl	struct cam_path *path;
687195534Sscottl	u_int32_t  priority;
688198389Smav	int found = 1;
689195534Sscottl
690195534Sscottl	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
691195534Sscottl
692195534Sscottl	softc = (probe_softc *)periph->softc;
693195534Sscottl	path = done_ccb->ccb_h.path;
694195534Sscottl	priority = done_ccb->ccb_h.pinfo.priority;
695195534Sscottl	ident_buf = &path->device->ident_data;
696195534Sscottl
697198708Smav	if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
698198708Smavdevice_fail:	if (cam_periph_error(done_ccb, 0, 0,
699198708Smav		    &softc->saved_ccb) == ERESTART) {
700195534Sscottl			return;
701195534Sscottl		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
702195534Sscottl			/* Don't wedge the queue */
703195534Sscottl			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
704195534Sscottl					 /*run_queue*/TRUE);
705195534Sscottl		}
706198708Smav		/* Old PIO2 devices may not support mode setting. */
707198708Smav		if (softc->action == PROBE_SETMODE &&
708198708Smav		    ata_max_pmode(ident_buf) <= ATA_PIO2 &&
709198708Smav		    (ident_buf->capabilities1 & ATA_SUPPORT_IORDY) == 0)
710198708Smav			goto noerror;
711198708Smav		/*
712198708Smav		 * If we get to this point, we got an error status back
713198708Smav		 * from the inquiry and the error status doesn't require
714198708Smav		 * automatically retrying the command.  Therefore, the
715198708Smav		 * inquiry failed.  If we had inquiry information before
716198708Smav		 * for this device, but this latest inquiry command failed,
717198708Smav		 * the device has probably gone away.  If this device isn't
718198708Smav		 * already marked unconfigured, notify the peripheral
719198708Smav		 * drivers that this device is no more.
720198708Smav		 */
721198708Smav		if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
722198708Smav			xpt_async(AC_LOST_DEVICE, path, NULL);
723198708Smav		found = 0;
724198708Smav		goto done;
725198708Smav	}
726198708Smavnoerror:
727198708Smav	switch (softc->action) {
728198708Smav	case PROBE_RESET:
729195534Sscottl	{
730198708Smav		int sign = (done_ccb->ataio.res.lba_high << 8) +
731198708Smav		    done_ccb->ataio.res.lba_mid;
732198708Smav		xpt_print(path, "SIGNATURE: %04x\n", sign);
733198708Smav		if (sign == 0x0000 &&
734198708Smav		    done_ccb->ccb_h.target_id != 15) {
735198708Smav			path->device->protocol = PROTO_ATA;
736198708Smav			PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
737198708Smav		} else if (sign == 0x9669 &&
738198708Smav		    done_ccb->ccb_h.target_id == 15) {
739199747Smav			/* Report SIM that PM is present. */
740198708Smav			bzero(&cts, sizeof(cts));
741198708Smav			xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
742198708Smav			cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
743198708Smav			cts.type = CTS_TYPE_CURRENT_SETTINGS;
744198708Smav			cts.xport_specific.sata.pm_present = 1;
745198708Smav			cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
746198708Smav			xpt_action((union ccb *)&cts);
747198708Smav			path->device->protocol = PROTO_SATAPM;
748198708Smav			PROBE_SET_ACTION(softc, PROBE_PM_PID);
749198708Smav		} else if (sign == 0xeb14 &&
750198708Smav		    done_ccb->ccb_h.target_id != 15) {
751198708Smav			path->device->protocol = PROTO_SCSI;
752198708Smav			PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
753198708Smav		} else {
754198708Smav			if (done_ccb->ccb_h.target_id != 15) {
755198708Smav				xpt_print(path,
756198708Smav				    "Unexpected signature 0x%04x\n", sign);
757195534Sscottl			}
758198708Smav			goto device_fail;
759198708Smav		}
760198708Smav		xpt_release_ccb(done_ccb);
761198708Smav		xpt_schedule(periph, priority);
762198708Smav		return;
763198708Smav	}
764198708Smav	case PROBE_IDENTIFY:
765198708Smav	{
766198708Smav		int16_t *ptr;
767195534Sscottl
768198708Smav		for (ptr = (int16_t *)ident_buf;
769198708Smav		     ptr < (int16_t *)ident_buf + sizeof(struct ata_params)/2; ptr++) {
770198708Smav			*ptr = le16toh(*ptr);
771198708Smav		}
772198708Smav		if (strncmp(ident_buf->model, "FX", 2) &&
773198708Smav		    strncmp(ident_buf->model, "NEC", 3) &&
774198708Smav		    strncmp(ident_buf->model, "Pioneer", 7) &&
775198708Smav		    strncmp(ident_buf->model, "SHARP", 5)) {
776198708Smav			ata_bswap(ident_buf->model, sizeof(ident_buf->model));
777198708Smav			ata_bswap(ident_buf->revision, sizeof(ident_buf->revision));
778198708Smav			ata_bswap(ident_buf->serial, sizeof(ident_buf->serial));
779198708Smav		}
780198708Smav		ata_btrim(ident_buf->model, sizeof(ident_buf->model));
781198708Smav		ata_bpack(ident_buf->model, ident_buf->model, sizeof(ident_buf->model));
782198708Smav		ata_btrim(ident_buf->revision, sizeof(ident_buf->revision));
783198708Smav		ata_bpack(ident_buf->revision, ident_buf->revision, sizeof(ident_buf->revision));
784198708Smav		ata_btrim(ident_buf->serial, sizeof(ident_buf->serial));
785198708Smav		ata_bpack(ident_buf->serial, ident_buf->serial, sizeof(ident_buf->serial));
786195534Sscottl
787198708Smav		if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
788198708Smav			/* Check that it is the same device. */
789198708Smav			MD5_CTX context;
790198708Smav			u_int8_t digest[16];
791198708Smav
792198708Smav			MD5Init(&context);
793198708Smav			MD5Update(&context,
794198708Smav			    (unsigned char *)ident_buf->model,
795198708Smav			    sizeof(ident_buf->model));
796198708Smav			MD5Update(&context,
797198708Smav			    (unsigned char *)ident_buf->revision,
798198708Smav			    sizeof(ident_buf->revision));
799198708Smav			MD5Update(&context,
800198708Smav			    (unsigned char *)ident_buf->serial,
801198708Smav			    sizeof(ident_buf->serial));
802198708Smav			MD5Final(digest, &context);
803198708Smav			if (bcmp(digest, softc->digest, sizeof(digest))) {
804198708Smav				/* Device changed. */
805198708Smav				xpt_async(AC_LOST_DEVICE, path, NULL);
806195534Sscottl			}
807198708Smav		} else {
808195534Sscottl			/* Clean up from previous instance of this device */
809195534Sscottl			if (path->device->serial_num != NULL) {
810195534Sscottl				free(path->device->serial_num, M_CAMXPT);
811195534Sscottl				path->device->serial_num = NULL;
812195534Sscottl				path->device->serial_num_len = 0;
813195534Sscottl			}
814195534Sscottl			path->device->serial_num =
815195534Sscottl				(u_int8_t *)malloc((sizeof(ident_buf->serial) + 1),
816198708Smav					   M_CAMXPT, M_NOWAIT);
817195534Sscottl			if (path->device->serial_num != NULL) {
818195534Sscottl				bcopy(ident_buf->serial,
819195534Sscottl				      path->device->serial_num,
820195534Sscottl				      sizeof(ident_buf->serial));
821195534Sscottl				path->device->serial_num[sizeof(ident_buf->serial)]
822195534Sscottl				    = '\0';
823195534Sscottl				path->device->serial_num_len =
824195534Sscottl				    strlen(path->device->serial_num);
825195534Sscottl			}
826195534Sscottl
827198331Smav			path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
828195534Sscottl		}
829199178Smav		if (ident_buf->satacapabilities & ATA_SUPPORT_NCQ) {
830199178Smav			path->device->mintags = path->device->maxtags =
831199178Smav			    ATA_QUEUE_LEN(ident_buf->queue) + 1;
832199178Smav		}
833199178Smav		ata_find_quirk(path->device);
834199263Smav		if (path->device->mintags != 0 &&
835199263Smav		    path->bus->sim->max_tagged_dev_openings != 0) {
836199747Smav			/* Report SIM which tags are allowed. */
837199747Smav			bzero(&cts, sizeof(cts));
838199747Smav			xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
839199747Smav			cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
840199747Smav			cts.type = CTS_TYPE_CURRENT_SETTINGS;
841199747Smav			cts.xport_specific.sata.tags = path->device->maxtags;
842199747Smav			cts.xport_specific.sata.valid = CTS_SATA_VALID_TAGS;
843199747Smav			xpt_action((union ccb *)&cts);
844199747Smav			/* Reconfigure queues for tagged queueing. */
845199178Smav			xpt_start_tags(path);
846199178Smav		}
847198708Smav		ata_device_transport(path);
848198708Smav		PROBE_SET_ACTION(softc, PROBE_SETMODE);
849195534Sscottl		xpt_release_ccb(done_ccb);
850198708Smav		xpt_schedule(periph, priority);
851198708Smav		return;
852195534Sscottl	}
853195534Sscottl	case PROBE_SETMODE:
854198708Smav		if (path->device->protocol == PROTO_ATA) {
855198708Smav			PROBE_SET_ACTION(softc, PROBE_SET_MULTI);
856198708Smav		} else {
857198708Smav			PROBE_SET_ACTION(softc, PROBE_INQUIRY);
858195534Sscottl		}
859198708Smav		xpt_release_ccb(done_ccb);
860198708Smav		xpt_schedule(periph, priority);
861198708Smav		return;
862198708Smav	case PROBE_SET_MULTI:
863198708Smav		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
864198708Smav			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
865198748Smav			xpt_acquire_device(path->device);
866198708Smav			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
867198708Smav			xpt_action(done_ccb);
868198708Smav			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
869198708Smav			    done_ccb);
870198708Smav		}
871198708Smav		break;
872195534Sscottl	case PROBE_INQUIRY:
873195534Sscottl	case PROBE_FULL_INQUIRY:
874195534Sscottl	{
875198708Smav		struct scsi_inquiry_data *inq_buf;
876198708Smav		u_int8_t periph_qual, len;
877195534Sscottl
878198708Smav		path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
879198708Smav		inq_buf = &path->device->inq_data;
880195534Sscottl
881198708Smav		periph_qual = SID_QUAL(inq_buf);
882195534Sscottl
883198708Smav		if (periph_qual != SID_QUAL_LU_CONNECTED)
884198708Smav			break;
885195534Sscottl
886198708Smav		/*
887198708Smav		 * We conservatively request only
888198708Smav		 * SHORT_INQUIRY_LEN bytes of inquiry
889198708Smav		 * information during our first try
890198708Smav		 * at sending an INQUIRY. If the device
891198708Smav		 * has more information to give,
892198708Smav		 * perform a second request specifying
893198708Smav		 * the amount of information the device
894198708Smav		 * is willing to give.
895198708Smav		 */
896198708Smav		len = inq_buf->additional_length
897198708Smav		    + offsetof(struct scsi_inquiry_data, additional_length) + 1;
898198708Smav		if (softc->action == PROBE_INQUIRY
899198708Smav		    && len > SHORT_INQUIRY_LENGTH) {
900198708Smav			PROBE_SET_ACTION(softc, PROBE_FULL_INQUIRY);
901198708Smav			xpt_release_ccb(done_ccb);
902198708Smav			xpt_schedule(periph, priority);
903195534Sscottl			return;
904195534Sscottl		}
905198708Smav
906198708Smav		ata_device_transport(path);
907198708Smav		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
908198708Smav			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
909198748Smav			xpt_acquire_device(path->device);
910198708Smav			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
911198708Smav			xpt_action(done_ccb);
912198708Smav			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path, done_ccb);
913198708Smav		}
914198708Smav		break;
915195534Sscottl	}
916195534Sscottl	case PROBE_PM_PID:
917198708Smav		if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) == 0)
918198708Smav			bzero(ident_buf, sizeof(*ident_buf));
919198708Smav		softc->pm_pid = (done_ccb->ataio.res.lba_high << 24) +
920198708Smav		    (done_ccb->ataio.res.lba_mid << 16) +
921198708Smav		    (done_ccb->ataio.res.lba_low << 8) +
922198708Smav		    done_ccb->ataio.res.sector_count;
923198708Smav		((uint32_t *)ident_buf)[0] = softc->pm_pid;
924198708Smav		printf("PM Product ID: %08x\n", softc->pm_pid);
925198708Smav		snprintf(ident_buf->model, sizeof(ident_buf->model),
926198708Smav		    "Port Multiplier %08x", softc->pm_pid);
927198708Smav		PROBE_SET_ACTION(softc, PROBE_PM_PRV);
928198708Smav		xpt_release_ccb(done_ccb);
929198708Smav		xpt_schedule(periph, priority);
930198708Smav		return;
931195534Sscottl	case PROBE_PM_PRV:
932198708Smav		softc->pm_prv = (done_ccb->ataio.res.lba_high << 24) +
933198708Smav		    (done_ccb->ataio.res.lba_mid << 16) +
934198708Smav		    (done_ccb->ataio.res.lba_low << 8) +
935198708Smav		    done_ccb->ataio.res.sector_count;
936198708Smav		((uint32_t *)ident_buf)[1] = softc->pm_prv;
937198708Smav		printf("PM Revision: %08x\n", softc->pm_prv);
938198708Smav		snprintf(ident_buf->revision, sizeof(ident_buf->revision),
939198708Smav		    "%04x", softc->pm_prv);
940198708Smav		path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
941198708Smav		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
942198708Smav			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
943198748Smav			xpt_acquire_device(path->device);
944198708Smav			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
945198708Smav			xpt_action(done_ccb);
946198708Smav			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
947198708Smav			    done_ccb);
948198708Smav		} else {
949198708Smav			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
950198708Smav			xpt_action(done_ccb);
951198708Smav			xpt_async(AC_SCSI_AEN, done_ccb->ccb_h.path, done_ccb);
952195534Sscottl		}
953198708Smav		break;
954195534Sscottl	case PROBE_INVALID:
955195534Sscottl		CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_INFO,
956195534Sscottl		    ("probedone: invalid action state\n"));
957195534Sscottl	default:
958195534Sscottl		break;
959195534Sscottl	}
960198708Smavdone:
961198708Smav	xpt_release_ccb(done_ccb);
962195534Sscottl	done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
963195534Sscottl	TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe);
964195534Sscottl	done_ccb->ccb_h.status = CAM_REQ_CMP;
965195534Sscottl	done_ccb->ccb_h.ppriv_field1 = found;
966195534Sscottl	xpt_done(done_ccb);
967195534Sscottl	if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
968195534Sscottl		cam_periph_invalidate(periph);
969195534Sscottl		cam_periph_release_locked(periph);
970195534Sscottl	} else {
971195534Sscottl		probeschedule(periph);
972195534Sscottl	}
973195534Sscottl}
974195534Sscottl
975195534Sscottlstatic void
976195534Sscottlprobecleanup(struct cam_periph *periph)
977195534Sscottl{
978195534Sscottl	free(periph->softc, M_CAMXPT);
979195534Sscottl}
980195534Sscottl
981195534Sscottlstatic void
982199178Smavata_find_quirk(struct cam_ed *device)
983195534Sscottl{
984199178Smav	struct ata_quirk_entry *quirk;
985195534Sscottl	caddr_t	match;
986195534Sscottl
987199178Smav	match = cam_quirkmatch((caddr_t)&device->ident_data,
988199178Smav			       (caddr_t)ata_quirk_table,
989199178Smav			       ata_quirk_table_size,
990199178Smav			       sizeof(*ata_quirk_table), ata_identify_match);
991195534Sscottl
992195534Sscottl	if (match == NULL)
993195534Sscottl		panic("xpt_find_quirk: device didn't match wildcard entry!!");
994195534Sscottl
995199178Smav	quirk = (struct ata_quirk_entry *)match;
996195534Sscottl	device->quirk = quirk;
997199178Smav	if (quirk->quirks & CAM_QUIRK_MAXTAGS)
998199178Smav		device->mintags = device->maxtags = quirk->maxtags;
999195534Sscottl}
1000195534Sscottl
1001195534Sscottltypedef struct {
1002195534Sscottl	union	ccb *request_ccb;
1003195534Sscottl	struct 	ccb_pathinq *cpi;
1004195534Sscottl	int	counter;
1005195534Sscottl	int	found;
1006195534Sscottl} ata_scan_bus_info;
1007195534Sscottl
1008195534Sscottl/*
1009195534Sscottl * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
1010195534Sscottl * As the scan progresses, xpt_scan_bus is used as the
1011195534Sscottl * callback on completion function.
1012195534Sscottl */
1013195534Sscottlstatic void
1014195534Sscottlata_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
1015195534Sscottl{
1016195534Sscottl	struct	cam_path *path;
1017195534Sscottl	ata_scan_bus_info *scan_info;
1018195534Sscottl	union	ccb *work_ccb;
1019195534Sscottl	cam_status status;
1020195534Sscottl
1021195534Sscottl	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
1022195534Sscottl		  ("xpt_scan_bus\n"));
1023195534Sscottl	switch (request_ccb->ccb_h.func_code) {
1024195534Sscottl	case XPT_SCAN_BUS:
1025195534Sscottl		/* Find out the characteristics of the bus */
1026195534Sscottl		work_ccb = xpt_alloc_ccb_nowait();
1027195534Sscottl		if (work_ccb == NULL) {
1028195534Sscottl			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1029195534Sscottl			xpt_done(request_ccb);
1030195534Sscottl			return;
1031195534Sscottl		}
1032195534Sscottl		xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
1033195534Sscottl			      request_ccb->ccb_h.pinfo.priority);
1034195534Sscottl		work_ccb->ccb_h.func_code = XPT_PATH_INQ;
1035195534Sscottl		xpt_action(work_ccb);
1036195534Sscottl		if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
1037195534Sscottl			request_ccb->ccb_h.status = work_ccb->ccb_h.status;
1038195534Sscottl			xpt_free_ccb(work_ccb);
1039195534Sscottl			xpt_done(request_ccb);
1040195534Sscottl			return;
1041195534Sscottl		}
1042195534Sscottl
1043195534Sscottl		/* Save some state for use while we probe for devices */
1044195534Sscottl		scan_info = (ata_scan_bus_info *)
1045195534Sscottl		    malloc(sizeof(ata_scan_bus_info), M_CAMXPT, M_NOWAIT);
1046195534Sscottl		if (scan_info == NULL) {
1047195534Sscottl			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1048195534Sscottl			xpt_done(request_ccb);
1049195534Sscottl			return;
1050195534Sscottl		}
1051195534Sscottl		scan_info->request_ccb = request_ccb;
1052195534Sscottl		scan_info->cpi = &work_ccb->cpi;
1053198331Smav		if (scan_info->cpi->transport == XPORT_ATA)
1054198331Smav			scan_info->found = 0x0003;
1055198331Smav		else
1056198331Smav			scan_info->found = 0x8001;
1057195534Sscottl		scan_info->counter = 0;
1058195534Sscottl		/* If PM supported, probe it first. */
1059195534Sscottl		if (scan_info->cpi->hba_inquiry & PI_SATAPM)
1060195534Sscottl			scan_info->counter = 15;
1061195534Sscottl
1062195534Sscottl		work_ccb = xpt_alloc_ccb_nowait();
1063195534Sscottl		if (work_ccb == NULL) {
1064195534Sscottl			free(scan_info, M_CAMXPT);
1065195534Sscottl			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1066195534Sscottl			xpt_done(request_ccb);
1067195534Sscottl			break;
1068195534Sscottl		}
1069195534Sscottl		goto scan_next;
1070195534Sscottl	case XPT_SCAN_LUN:
1071195534Sscottl		work_ccb = request_ccb;
1072195534Sscottl		/* Reuse the same CCB to query if a device was really found */
1073195534Sscottl		scan_info = (ata_scan_bus_info *)work_ccb->ccb_h.ppriv_ptr0;
1074195534Sscottl		/* Free the current request path- we're done with it. */
1075195534Sscottl		xpt_free_path(work_ccb->ccb_h.path);
1076198389Smav		/* If there is PMP... */
1077195534Sscottl		if (scan_info->counter == 15) {
1078195534Sscottl			if (work_ccb->ccb_h.ppriv_field1 != 0) {
1079198389Smav				/* everything else willbe probed by it */
1080198389Smav				scan_info->found = 0x8000;
1081195534Sscottl			} else {
1082195534Sscottl				struct ccb_trans_settings cts;
1083195534Sscottl
1084195534Sscottl				/* Report SIM that PM is absent. */
1085195534Sscottl				bzero(&cts, sizeof(cts));
1086195534Sscottl				xpt_setup_ccb(&cts.ccb_h,
1087195534Sscottl				    scan_info->request_ccb->ccb_h.path, 1);
1088195534Sscottl				cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1089195534Sscottl				cts.type = CTS_TYPE_CURRENT_SETTINGS;
1090195665Smav				cts.xport_specific.sata.pm_present = 0;
1091195534Sscottl				cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
1092195534Sscottl				xpt_action((union ccb *)&cts);
1093195534Sscottl			}
1094195534Sscottl		}
1095195534Sscottltake_next:
1096195534Sscottl		/* Take next device. Wrap from 15 (PM) to 0. */
1097195534Sscottl		scan_info->counter = (scan_info->counter + 1 ) & 0x0f;
1098198322Smav		if (scan_info->counter > scan_info->cpi->max_target -
1099198322Smav		    ((scan_info->cpi->hba_inquiry & PI_SATAPM) ? 1 : 0)) {
1100195534Sscottl			xpt_free_ccb(work_ccb);
1101195534Sscottl			xpt_free_ccb((union ccb *)scan_info->cpi);
1102195534Sscottl			request_ccb = scan_info->request_ccb;
1103195534Sscottl			free(scan_info, M_CAMXPT);
1104195534Sscottl			request_ccb->ccb_h.status = CAM_REQ_CMP;
1105195534Sscottl			xpt_done(request_ccb);
1106195534Sscottl			break;
1107195534Sscottl		}
1108195534Sscottlscan_next:
1109198389Smav		if ((scan_info->found & (1 << scan_info->counter)) == 0)
1110198389Smav			goto take_next;
1111195534Sscottl		status = xpt_create_path(&path, xpt_periph,
1112195534Sscottl		    scan_info->request_ccb->ccb_h.path_id,
1113195534Sscottl		    scan_info->counter, 0);
1114195534Sscottl		if (status != CAM_REQ_CMP) {
1115195534Sscottl			printf("xpt_scan_bus: xpt_create_path failed"
1116195534Sscottl			    " with status %#x, bus scan halted\n",
1117195534Sscottl			    status);
1118195534Sscottl			xpt_free_ccb(work_ccb);
1119195534Sscottl			xpt_free_ccb((union ccb *)scan_info->cpi);
1120195534Sscottl			request_ccb = scan_info->request_ccb;
1121195534Sscottl			free(scan_info, M_CAMXPT);
1122195534Sscottl			request_ccb->ccb_h.status = status;
1123195534Sscottl			xpt_done(request_ccb);
1124195534Sscottl			break;
1125195534Sscottl		}
1126195534Sscottl		xpt_setup_ccb(&work_ccb->ccb_h, path,
1127195534Sscottl		    scan_info->request_ccb->ccb_h.pinfo.priority);
1128195534Sscottl		work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1129195534Sscottl		work_ccb->ccb_h.cbfcnp = ata_scan_bus;
1130195534Sscottl		work_ccb->ccb_h.ppriv_ptr0 = scan_info;
1131195534Sscottl		work_ccb->crcn.flags = scan_info->request_ccb->crcn.flags;
1132195534Sscottl		xpt_action(work_ccb);
1133195534Sscottl		break;
1134195534Sscottl	default:
1135195534Sscottl		break;
1136195534Sscottl	}
1137195534Sscottl}
1138195534Sscottl
1139195534Sscottlstatic void
1140195534Sscottlata_scan_lun(struct cam_periph *periph, struct cam_path *path,
1141195534Sscottl	     cam_flags flags, union ccb *request_ccb)
1142195534Sscottl{
1143195534Sscottl	struct ccb_pathinq cpi;
1144195534Sscottl	cam_status status;
1145195534Sscottl	struct cam_path *new_path;
1146195534Sscottl	struct cam_periph *old_periph;
1147195534Sscottl
1148195534Sscottl	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
1149195534Sscottl		  ("xpt_scan_lun\n"));
1150195534Sscottl
1151198382Smav	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
1152195534Sscottl	cpi.ccb_h.func_code = XPT_PATH_INQ;
1153195534Sscottl	xpt_action((union ccb *)&cpi);
1154195534Sscottl
1155195534Sscottl	if (cpi.ccb_h.status != CAM_REQ_CMP) {
1156195534Sscottl		if (request_ccb != NULL) {
1157195534Sscottl			request_ccb->ccb_h.status = cpi.ccb_h.status;
1158195534Sscottl			xpt_done(request_ccb);
1159195534Sscottl		}
1160195534Sscottl		return;
1161195534Sscottl	}
1162195534Sscottl
1163195534Sscottl	if (request_ccb == NULL) {
1164195534Sscottl		request_ccb = malloc(sizeof(union ccb), M_CAMXPT, M_NOWAIT);
1165195534Sscottl		if (request_ccb == NULL) {
1166195534Sscottl			xpt_print(path, "xpt_scan_lun: can't allocate CCB, "
1167195534Sscottl			    "can't continue\n");
1168195534Sscottl			return;
1169195534Sscottl		}
1170195534Sscottl		new_path = malloc(sizeof(*new_path), M_CAMXPT, M_NOWAIT);
1171195534Sscottl		if (new_path == NULL) {
1172195534Sscottl			xpt_print(path, "xpt_scan_lun: can't allocate path, "
1173195534Sscottl			    "can't continue\n");
1174195534Sscottl			free(request_ccb, M_CAMXPT);
1175195534Sscottl			return;
1176195534Sscottl		}
1177195534Sscottl		status = xpt_compile_path(new_path, xpt_periph,
1178195534Sscottl					  path->bus->path_id,
1179195534Sscottl					  path->target->target_id,
1180195534Sscottl					  path->device->lun_id);
1181195534Sscottl
1182195534Sscottl		if (status != CAM_REQ_CMP) {
1183195534Sscottl			xpt_print(path, "xpt_scan_lun: can't compile path, "
1184195534Sscottl			    "can't continue\n");
1185195534Sscottl			free(request_ccb, M_CAMXPT);
1186195534Sscottl			free(new_path, M_CAMXPT);
1187195534Sscottl			return;
1188195534Sscottl		}
1189198382Smav		xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_NORMAL);
1190195534Sscottl		request_ccb->ccb_h.cbfcnp = xptscandone;
1191195534Sscottl		request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1192195534Sscottl		request_ccb->crcn.flags = flags;
1193195534Sscottl	}
1194195534Sscottl
1195195653Smav	if ((old_periph = cam_periph_find(path, "aprobe")) != NULL) {
1196195534Sscottl		probe_softc *softc;
1197195534Sscottl
1198195534Sscottl		softc = (probe_softc *)old_periph->softc;
1199195534Sscottl		TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
1200195534Sscottl				  periph_links.tqe);
1201195534Sscottl	} else {
1202195534Sscottl		status = cam_periph_alloc(proberegister, NULL, probecleanup,
1203195653Smav					  probestart, "aprobe",
1204195534Sscottl					  CAM_PERIPH_BIO,
1205195534Sscottl					  request_ccb->ccb_h.path, NULL, 0,
1206195534Sscottl					  request_ccb);
1207195534Sscottl
1208195534Sscottl		if (status != CAM_REQ_CMP) {
1209195534Sscottl			xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
1210195534Sscottl			    "returned an error, can't continue probe\n");
1211195534Sscottl			request_ccb->ccb_h.status = status;
1212195534Sscottl			xpt_done(request_ccb);
1213195534Sscottl		}
1214195534Sscottl	}
1215195534Sscottl}
1216195534Sscottl
1217195534Sscottlstatic void
1218195534Sscottlxptscandone(struct cam_periph *periph, union ccb *done_ccb)
1219195534Sscottl{
1220195534Sscottl	xpt_release_path(done_ccb->ccb_h.path);
1221195534Sscottl	free(done_ccb->ccb_h.path, M_CAMXPT);
1222195534Sscottl	free(done_ccb, M_CAMXPT);
1223195534Sscottl}
1224195534Sscottl
1225195534Sscottlstatic struct cam_ed *
1226195534Sscottlata_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
1227195534Sscottl{
1228195534Sscottl	struct cam_path path;
1229199178Smav	struct ata_quirk_entry *quirk;
1230195534Sscottl	struct cam_ed *device;
1231195534Sscottl	struct cam_ed *cur_device;
1232195534Sscottl
1233195534Sscottl	device = xpt_alloc_device(bus, target, lun_id);
1234195534Sscottl	if (device == NULL)
1235195534Sscottl		return (NULL);
1236195534Sscottl
1237195534Sscottl	/*
1238195534Sscottl	 * Take the default quirk entry until we have inquiry
1239195534Sscottl	 * data and can determine a better quirk to use.
1240195534Sscottl	 */
1241199178Smav	quirk = &ata_quirk_table[ata_quirk_table_size - 1];
1242195534Sscottl	device->quirk = (void *)quirk;
1243199178Smav	device->mintags = 0;
1244199178Smav	device->maxtags = 0;
1245195534Sscottl	bzero(&device->inq_data, sizeof(device->inq_data));
1246195534Sscottl	device->inq_flags = 0;
1247195534Sscottl	device->queue_flags = 0;
1248195534Sscottl	device->serial_num = NULL;
1249195534Sscottl	device->serial_num_len = 0;
1250195534Sscottl
1251195534Sscottl	/*
1252195534Sscottl	 * XXX should be limited by number of CCBs this bus can
1253195534Sscottl	 * do.
1254195534Sscottl	 */
1255195534Sscottl	bus->sim->max_ccbs += device->ccbq.devq_openings;
1256195534Sscottl	/* Insertion sort into our target's device list */
1257195534Sscottl	cur_device = TAILQ_FIRST(&target->ed_entries);
1258195534Sscottl	while (cur_device != NULL && cur_device->lun_id < lun_id)
1259195534Sscottl		cur_device = TAILQ_NEXT(cur_device, links);
1260195534Sscottl	if (cur_device != NULL) {
1261195534Sscottl		TAILQ_INSERT_BEFORE(cur_device, device, links);
1262195534Sscottl	} else {
1263195534Sscottl		TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
1264195534Sscottl	}
1265195534Sscottl	target->generation++;
1266195534Sscottl	if (lun_id != CAM_LUN_WILDCARD) {
1267195534Sscottl		xpt_compile_path(&path,
1268195534Sscottl				 NULL,
1269195534Sscottl				 bus->path_id,
1270195534Sscottl				 target->target_id,
1271195534Sscottl				 lun_id);
1272195534Sscottl		ata_device_transport(&path);
1273195534Sscottl		xpt_release_path(&path);
1274195534Sscottl	}
1275195534Sscottl
1276195534Sscottl	return (device);
1277195534Sscottl}
1278195534Sscottl
1279195534Sscottlstatic void
1280195534Sscottlata_device_transport(struct cam_path *path)
1281195534Sscottl{
1282195534Sscottl	struct ccb_pathinq cpi;
1283198331Smav	struct ccb_trans_settings cts;
1284198331Smav	struct scsi_inquiry_data *inq_buf = NULL;
1285198331Smav	struct ata_params *ident_buf = NULL;
1286195534Sscottl
1287195534Sscottl	/* Get transport information from the SIM */
1288198382Smav	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
1289195534Sscottl	cpi.ccb_h.func_code = XPT_PATH_INQ;
1290195534Sscottl	xpt_action((union ccb *)&cpi);
1291195534Sscottl
1292195534Sscottl	path->device->transport = cpi.transport;
1293198331Smav	if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
1294198331Smav		inq_buf = &path->device->inq_data;
1295198331Smav	if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) != 0)
1296198331Smav		ident_buf = &path->device->ident_data;
1297198331Smav	if (path->device->protocol == PROTO_ATA) {
1298198331Smav		path->device->protocol_version = ident_buf ?
1299198331Smav		    ata_version(ident_buf->version_major) : cpi.protocol_version;
1300198331Smav	} else if (path->device->protocol == PROTO_SCSI) {
1301198331Smav		path->device->protocol_version = inq_buf ?
1302198331Smav		    SID_ANSI_REV(inq_buf) : cpi.protocol_version;
1303195534Sscottl	}
1304198331Smav	path->device->transport_version = ident_buf ?
1305198331Smav	    ata_version(ident_buf->version_major) : cpi.transport_version;
1306195534Sscottl
1307195534Sscottl	/* Tell the controller what we think */
1308198382Smav	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
1309195534Sscottl	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1310195534Sscottl	cts.type = CTS_TYPE_CURRENT_SETTINGS;
1311195534Sscottl	cts.transport = path->device->transport;
1312195534Sscottl	cts.transport_version = path->device->transport_version;
1313195534Sscottl	cts.protocol = path->device->protocol;
1314195534Sscottl	cts.protocol_version = path->device->protocol_version;
1315195534Sscottl	cts.proto_specific.valid = 0;
1316195534Sscottl	cts.xport_specific.valid = 0;
1317195534Sscottl	xpt_action((union ccb *)&cts);
1318195534Sscottl}
1319195534Sscottl
1320195534Sscottlstatic void
1321195534Sscottlata_action(union ccb *start_ccb)
1322195534Sscottl{
1323195534Sscottl
1324195534Sscottl	switch (start_ccb->ccb_h.func_code) {
1325195534Sscottl	case XPT_SET_TRAN_SETTINGS:
1326195534Sscottl	{
1327199178Smav		ata_set_transfer_settings(&start_ccb->cts,
1328195534Sscottl					   start_ccb->ccb_h.path->device,
1329195534Sscottl					   /*async_update*/FALSE);
1330195534Sscottl		break;
1331195534Sscottl	}
1332195534Sscottl	case XPT_SCAN_BUS:
1333195534Sscottl		ata_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
1334195534Sscottl		break;
1335195534Sscottl	case XPT_SCAN_LUN:
1336195534Sscottl		ata_scan_lun(start_ccb->ccb_h.path->periph,
1337195534Sscottl			      start_ccb->ccb_h.path, start_ccb->crcn.flags,
1338195534Sscottl			      start_ccb);
1339195534Sscottl		break;
1340195534Sscottl	case XPT_GET_TRAN_SETTINGS:
1341195534Sscottl	{
1342195534Sscottl		struct cam_sim *sim;
1343195534Sscottl
1344195534Sscottl		sim = start_ccb->ccb_h.path->bus->sim;
1345195534Sscottl		(*(sim->sim_action))(sim, start_ccb);
1346195534Sscottl		break;
1347195534Sscottl	}
1348195534Sscottl	default:
1349195534Sscottl		xpt_action_default(start_ccb);
1350195534Sscottl		break;
1351195534Sscottl	}
1352195534Sscottl}
1353195534Sscottl
1354195534Sscottlstatic void
1355199178Smavata_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
1356195534Sscottl			   int async_update)
1357195534Sscottl{
1358195534Sscottl	struct	ccb_pathinq cpi;
1359195534Sscottl	struct	ccb_trans_settings cur_cts;
1360195534Sscottl	struct	ccb_trans_settings_scsi *scsi;
1361195534Sscottl	struct	ccb_trans_settings_scsi *cur_scsi;
1362195534Sscottl	struct	cam_sim *sim;
1363195534Sscottl	struct	scsi_inquiry_data *inq_data;
1364195534Sscottl
1365195534Sscottl	if (device == NULL) {
1366195534Sscottl		cts->ccb_h.status = CAM_PATH_INVALID;
1367195534Sscottl		xpt_done((union ccb *)cts);
1368195534Sscottl		return;
1369195534Sscottl	}
1370195534Sscottl
1371195534Sscottl	if (cts->protocol == PROTO_UNKNOWN
1372195534Sscottl	 || cts->protocol == PROTO_UNSPECIFIED) {
1373195534Sscottl		cts->protocol = device->protocol;
1374195534Sscottl		cts->protocol_version = device->protocol_version;
1375195534Sscottl	}
1376195534Sscottl
1377195534Sscottl	if (cts->protocol_version == PROTO_VERSION_UNKNOWN
1378195534Sscottl	 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
1379195534Sscottl		cts->protocol_version = device->protocol_version;
1380195534Sscottl
1381195534Sscottl	if (cts->protocol != device->protocol) {
1382195534Sscottl		xpt_print(cts->ccb_h.path, "Uninitialized Protocol %x:%x?\n",
1383195534Sscottl		       cts->protocol, device->protocol);
1384195534Sscottl		cts->protocol = device->protocol;
1385195534Sscottl	}
1386195534Sscottl
1387195534Sscottl	if (cts->protocol_version > device->protocol_version) {
1388195534Sscottl		if (bootverbose) {
1389195534Sscottl			xpt_print(cts->ccb_h.path, "Down reving Protocol "
1390195534Sscottl			    "Version from %d to %d?\n", cts->protocol_version,
1391195534Sscottl			    device->protocol_version);
1392195534Sscottl		}
1393195534Sscottl		cts->protocol_version = device->protocol_version;
1394195534Sscottl	}
1395195534Sscottl
1396195534Sscottl	if (cts->transport == XPORT_UNKNOWN
1397195534Sscottl	 || cts->transport == XPORT_UNSPECIFIED) {
1398195534Sscottl		cts->transport = device->transport;
1399195534Sscottl		cts->transport_version = device->transport_version;
1400195534Sscottl	}
1401195534Sscottl
1402195534Sscottl	if (cts->transport_version == XPORT_VERSION_UNKNOWN
1403195534Sscottl	 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
1404195534Sscottl		cts->transport_version = device->transport_version;
1405195534Sscottl
1406195534Sscottl	if (cts->transport != device->transport) {
1407195534Sscottl		xpt_print(cts->ccb_h.path, "Uninitialized Transport %x:%x?\n",
1408195534Sscottl		    cts->transport, device->transport);
1409195534Sscottl		cts->transport = device->transport;
1410195534Sscottl	}
1411195534Sscottl
1412195534Sscottl	if (cts->transport_version > device->transport_version) {
1413195534Sscottl		if (bootverbose) {
1414195534Sscottl			xpt_print(cts->ccb_h.path, "Down reving Transport "
1415195534Sscottl			    "Version from %d to %d?\n", cts->transport_version,
1416195534Sscottl			    device->transport_version);
1417195534Sscottl		}
1418195534Sscottl		cts->transport_version = device->transport_version;
1419195534Sscottl	}
1420195534Sscottl
1421195534Sscottl	sim = cts->ccb_h.path->bus->sim;
1422195534Sscottl
1423195534Sscottl	/*
1424195534Sscottl	 * Nothing more of interest to do unless
1425195534Sscottl	 * this is a device connected via the
1426195534Sscottl	 * SCSI protocol.
1427195534Sscottl	 */
1428195534Sscottl	if (cts->protocol != PROTO_SCSI) {
1429195534Sscottl		if (async_update == FALSE)
1430195534Sscottl			(*(sim->sim_action))(sim, (union ccb *)cts);
1431195534Sscottl		return;
1432195534Sscottl	}
1433195534Sscottl
1434195534Sscottl	inq_data = &device->inq_data;
1435195534Sscottl	scsi = &cts->proto_specific.scsi;
1436198382Smav	xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NORMAL);
1437195534Sscottl	cpi.ccb_h.func_code = XPT_PATH_INQ;
1438195534Sscottl	xpt_action((union ccb *)&cpi);
1439195534Sscottl
1440195534Sscottl	/* SCSI specific sanity checking */
1441195534Sscottl	if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
1442195534Sscottl	 || (INQ_DATA_TQ_ENABLED(inq_data)) == 0
1443195534Sscottl	 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
1444195534Sscottl	 || (device->mintags == 0)) {
1445195534Sscottl		/*
1446195534Sscottl		 * Can't tag on hardware that doesn't support tags,
1447195534Sscottl		 * doesn't have it enabled, or has broken tag support.
1448195534Sscottl		 */
1449195534Sscottl		scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1450195534Sscottl	}
1451195534Sscottl
1452195534Sscottl	if (async_update == FALSE) {
1453195534Sscottl		/*
1454195534Sscottl		 * Perform sanity checking against what the
1455195534Sscottl		 * controller and device can do.
1456195534Sscottl		 */
1457198382Smav		xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NORMAL);
1458195534Sscottl		cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1459195534Sscottl		cur_cts.type = cts->type;
1460195534Sscottl		xpt_action((union ccb *)&cur_cts);
1461195534Sscottl		if ((cur_cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1462195534Sscottl			return;
1463195534Sscottl		}
1464195534Sscottl		cur_scsi = &cur_cts.proto_specific.scsi;
1465195534Sscottl		if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
1466195534Sscottl			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1467195534Sscottl			scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
1468195534Sscottl		}
1469195534Sscottl		if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
1470195534Sscottl			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1471195534Sscottl	}
1472195534Sscottl
1473195534Sscottl	if (cts->type == CTS_TYPE_CURRENT_SETTINGS
1474195534Sscottl	 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
1475195534Sscottl		int device_tagenb;
1476195534Sscottl
1477195534Sscottl		/*
1478195534Sscottl		 * If we are transitioning from tags to no-tags or
1479195534Sscottl		 * vice-versa, we need to carefully freeze and restart
1480195534Sscottl		 * the queue so that we don't overlap tagged and non-tagged
1481195534Sscottl		 * commands.  We also temporarily stop tags if there is
1482195534Sscottl		 * a change in transfer negotiation settings to allow
1483195534Sscottl		 * "tag-less" negotiation.
1484195534Sscottl		 */
1485195534Sscottl		if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
1486195534Sscottl		 || (device->inq_flags & SID_CmdQue) != 0)
1487195534Sscottl			device_tagenb = TRUE;
1488195534Sscottl		else
1489195534Sscottl			device_tagenb = FALSE;
1490195534Sscottl
1491195534Sscottl		if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
1492195534Sscottl		  && device_tagenb == FALSE)
1493195534Sscottl		 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
1494195534Sscottl		  && device_tagenb == TRUE)) {
1495195534Sscottl
1496195534Sscottl			if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
1497195534Sscottl				/*
1498195534Sscottl				 * Delay change to use tags until after a
1499195534Sscottl				 * few commands have gone to this device so
1500195534Sscottl				 * the controller has time to perform transfer
1501195534Sscottl				 * negotiations without tagged messages getting
1502195534Sscottl				 * in the way.
1503195534Sscottl				 */
1504195534Sscottl				device->tag_delay_count = CAM_TAG_DELAY_COUNT;
1505195534Sscottl				device->flags |= CAM_DEV_TAG_AFTER_COUNT;
1506195534Sscottl			} else {
1507199178Smav				xpt_stop_tags(cts->ccb_h.path);
1508195534Sscottl			}
1509195534Sscottl		}
1510195534Sscottl	}
1511195534Sscottl	if (async_update == FALSE)
1512195534Sscottl		(*(sim->sim_action))(sim, (union ccb *)cts);
1513195534Sscottl}
1514195534Sscottl
1515195534Sscottl/*
1516195534Sscottl * Handle any per-device event notifications that require action by the XPT.
1517195534Sscottl */
1518195534Sscottlstatic void
1519195534Sscottlata_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
1520195534Sscottl	      struct cam_ed *device, void *async_arg)
1521195534Sscottl{
1522195534Sscottl	cam_status status;
1523195534Sscottl	struct cam_path newpath;
1524195534Sscottl
1525195534Sscottl	/*
1526195534Sscottl	 * We only need to handle events for real devices.
1527195534Sscottl	 */
1528195534Sscottl	if (target->target_id == CAM_TARGET_WILDCARD
1529195534Sscottl	 || device->lun_id == CAM_LUN_WILDCARD)
1530195534Sscottl		return;
1531195534Sscottl
1532195534Sscottl	/*
1533195534Sscottl	 * We need our own path with wildcards expanded to
1534195534Sscottl	 * handle certain types of events.
1535195534Sscottl	 */
1536195534Sscottl	if ((async_code == AC_SENT_BDR)
1537195534Sscottl	 || (async_code == AC_BUS_RESET)
1538195534Sscottl	 || (async_code == AC_INQ_CHANGED))
1539195534Sscottl		status = xpt_compile_path(&newpath, NULL,
1540195534Sscottl					  bus->path_id,
1541195534Sscottl					  target->target_id,
1542195534Sscottl					  device->lun_id);
1543195534Sscottl	else
1544195534Sscottl		status = CAM_REQ_CMP_ERR;
1545195534Sscottl
1546195534Sscottl	if (status == CAM_REQ_CMP) {
1547195534Sscottl		if (async_code == AC_INQ_CHANGED) {
1548195534Sscottl			/*
1549195534Sscottl			 * We've sent a start unit command, or
1550195534Sscottl			 * something similar to a device that
1551195534Sscottl			 * may have caused its inquiry data to
1552195534Sscottl			 * change. So we re-scan the device to
1553195534Sscottl			 * refresh the inquiry data for it.
1554195534Sscottl			 */
1555195534Sscottl			ata_scan_lun(newpath.periph, &newpath,
1556195534Sscottl				     CAM_EXPECT_INQ_CHANGE, NULL);
1557195534Sscottl		}
1558195534Sscottl		xpt_release_path(&newpath);
1559198748Smav	} else if (async_code == AC_LOST_DEVICE &&
1560198748Smav	    (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
1561195534Sscottl		device->flags |= CAM_DEV_UNCONFIGURED;
1562198748Smav		xpt_release_device(device);
1563195534Sscottl	} else if (async_code == AC_TRANSFER_NEG) {
1564195534Sscottl		struct ccb_trans_settings *settings;
1565195534Sscottl
1566195534Sscottl		settings = (struct ccb_trans_settings *)async_arg;
1567199178Smav		ata_set_transfer_settings(settings, device,
1568195534Sscottl					  /*async_update*/TRUE);
1569195534Sscottl	}
1570195534Sscottl}
1571195534Sscottl
1572