ata_xpt.c revision 199263
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 199263 2009-11-14 08:08:49Z 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{
278195534Sscottl	/* Probe the device that our peripheral driver points to */
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:
336195534Sscottl		cam_fill_ataio(ataio,
337195534Sscottl		      1,
338195534Sscottl		      probedone,
339196353Smav		      /*flags*/CAM_DIR_NONE,
340196353Smav		      0,
341196353Smav		      /*data_ptr*/NULL,
342196353Smav		      /*dxfer_len*/0,
343195534Sscottl		      30 * 1000);
344196659Smav		ata_28bit_cmd(ataio, ATA_SETFEATURES, ATA_SF_SETXFER, 0,
345195534Sscottl		    ata_max_mode(ident_buf, ATA_UDMA6, ATA_UDMA6));
346195534Sscottl		break;
347198708Smav	case PROBE_SET_MULTI:
348198708Smav	{
349198708Smav		struct ccb_trans_settings cts;
350198708Smav		u_int sectors;
351198708Smav
352198708Smav		sectors = max(1, min(ident_buf->sectors_intr & 0xff, 16));
353198708Smav
354198708Smav		/* Report bytecount to SIM. */
355198708Smav		bzero(&cts, sizeof(cts));
356198708Smav		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
357198708Smav		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
358198708Smav		cts.type = CTS_TYPE_CURRENT_SETTINGS;
359198708Smav		if (path->device->transport == XPORT_ATA) {
360198897Smav			cts.xport_specific.ata.bytecount = sectors *
361198897Smav			    ata_logical_sector_size(ident_buf);
362198708Smav			cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT;
363198708Smav		} else {
364198897Smav			cts.xport_specific.sata.bytecount = sectors *
365198897Smav			    ata_logical_sector_size(ident_buf);
366198708Smav			cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT;
367198708Smav		}
368198708Smav		xpt_action((union ccb *)&cts);
369198708Smav
370198708Smav		cam_fill_ataio(ataio,
371198708Smav		    1,
372198708Smav		    probedone,
373198708Smav		    CAM_DIR_NONE,
374198708Smav		    0,
375198708Smav		    NULL,
376198708Smav		    0,
377198708Smav		    30*1000);
378198708Smav		ata_28bit_cmd(ataio, ATA_SET_MULTI, 0, 0, sectors);
379198708Smav		break;
380195534Sscottl	}
381195534Sscottl	case PROBE_INQUIRY:
382195534Sscottl	case PROBE_FULL_INQUIRY:
383195534Sscottl	{
384195534Sscottl		u_int inquiry_len;
385195534Sscottl		struct scsi_inquiry_data *inq_buf =
386195534Sscottl		    &periph->path->device->inq_data;
387195534Sscottl
388195534Sscottl		if (softc->action == PROBE_INQUIRY)
389195534Sscottl			inquiry_len = SHORT_INQUIRY_LENGTH;
390195534Sscottl		else
391195534Sscottl			inquiry_len = SID_ADDITIONAL_LENGTH(inq_buf);
392195534Sscottl		/*
393195534Sscottl		 * Some parallel SCSI devices fail to send an
394195534Sscottl		 * ignore wide residue message when dealing with
395195534Sscottl		 * odd length inquiry requests.  Round up to be
396195534Sscottl		 * safe.
397195534Sscottl		 */
398195534Sscottl		inquiry_len = roundup2(inquiry_len, 2);
399195534Sscottl		scsi_inquiry(csio,
400195534Sscottl			     /*retries*/1,
401195534Sscottl			     probedone,
402195534Sscottl			     MSG_SIMPLE_Q_TAG,
403195534Sscottl			     (u_int8_t *)inq_buf,
404195534Sscottl			     inquiry_len,
405195534Sscottl			     /*evpd*/FALSE,
406195534Sscottl			     /*page_code*/0,
407195534Sscottl			     SSD_MIN_SIZE,
408195534Sscottl			     /*timeout*/60 * 1000);
409195534Sscottl		break;
410195534Sscottl	}
411195534Sscottl	case PROBE_PM_PID:
412195534Sscottl		cam_fill_ataio(ataio,
413195534Sscottl		      1,
414195534Sscottl		      probedone,
415195534Sscottl		      /*flags*/CAM_DIR_NONE,
416198389Smav		      0,
417195534Sscottl		      /*data_ptr*/NULL,
418195534Sscottl		      /*dxfer_len*/0,
419195534Sscottl		      10 * 1000);
420195534Sscottl		ata_pm_read_cmd(ataio, 0, 15);
421195534Sscottl		break;
422195534Sscottl	case PROBE_PM_PRV:
423195534Sscottl		cam_fill_ataio(ataio,
424195534Sscottl		      1,
425195534Sscottl		      probedone,
426195534Sscottl		      /*flags*/CAM_DIR_NONE,
427198389Smav		      0,
428195534Sscottl		      /*data_ptr*/NULL,
429195534Sscottl		      /*dxfer_len*/0,
430195534Sscottl		      10 * 1000);
431195534Sscottl		ata_pm_read_cmd(ataio, 1, 15);
432195534Sscottl		break;
433195534Sscottl	case PROBE_INVALID:
434198708Smav		CAM_DEBUG(path, CAM_DEBUG_INFO,
435195534Sscottl		    ("probestart: invalid action state\n"));
436195534Sscottl	default:
437195534Sscottl		break;
438195534Sscottl	}
439195534Sscottl	xpt_action(start_ccb);
440195534Sscottl}
441195534Sscottl#if 0
442195534Sscottlstatic void
443195534Sscottlproberequestdefaultnegotiation(struct cam_periph *periph)
444195534Sscottl{
445195534Sscottl	struct ccb_trans_settings cts;
446195534Sscottl
447198382Smav	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
448195534Sscottl	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
449195534Sscottl	cts.type = CTS_TYPE_USER_SETTINGS;
450195534Sscottl	xpt_action((union ccb *)&cts);
451195534Sscottl	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
452195534Sscottl		return;
453195534Sscottl	}
454195534Sscottl	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
455195534Sscottl	cts.type = CTS_TYPE_CURRENT_SETTINGS;
456195534Sscottl	xpt_action((union ccb *)&cts);
457195534Sscottl}
458195534Sscottl
459195534Sscottl/*
460195534Sscottl * Backoff Negotiation Code- only pertinent for SPI devices.
461195534Sscottl */
462195534Sscottlstatic int
463195534Sscottlproberequestbackoff(struct cam_periph *periph, struct cam_ed *device)
464195534Sscottl{
465195534Sscottl	struct ccb_trans_settings cts;
466195534Sscottl	struct ccb_trans_settings_spi *spi;
467195534Sscottl
468195534Sscottl	memset(&cts, 0, sizeof (cts));
469198382Smav	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
470195534Sscottl	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
471195534Sscottl	cts.type = CTS_TYPE_CURRENT_SETTINGS;
472195534Sscottl	xpt_action((union ccb *)&cts);
473195534Sscottl	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
474195534Sscottl		if (bootverbose) {
475195534Sscottl			xpt_print(periph->path,
476195534Sscottl			    "failed to get current device settings\n");
477195534Sscottl		}
478195534Sscottl		return (0);
479195534Sscottl	}
480195534Sscottl	if (cts.transport != XPORT_SPI) {
481195534Sscottl		if (bootverbose) {
482195534Sscottl			xpt_print(periph->path, "not SPI transport\n");
483195534Sscottl		}
484195534Sscottl		return (0);
485195534Sscottl	}
486195534Sscottl	spi = &cts.xport_specific.spi;
487195534Sscottl
488195534Sscottl	/*
489195534Sscottl	 * We cannot renegotiate sync rate if we don't have one.
490195534Sscottl	 */
491195534Sscottl	if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
492195534Sscottl		if (bootverbose) {
493195534Sscottl			xpt_print(periph->path, "no sync rate known\n");
494195534Sscottl		}
495195534Sscottl		return (0);
496195534Sscottl	}
497195534Sscottl
498195534Sscottl	/*
499195534Sscottl	 * We'll assert that we don't have to touch PPR options- the
500195534Sscottl	 * SIM will see what we do with period and offset and adjust
501195534Sscottl	 * the PPR options as appropriate.
502195534Sscottl	 */
503195534Sscottl
504195534Sscottl	/*
505195534Sscottl	 * A sync rate with unknown or zero offset is nonsensical.
506195534Sscottl	 * A sync period of zero means Async.
507195534Sscottl	 */
508195534Sscottl	if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0
509195534Sscottl	 || spi->sync_offset == 0 || spi->sync_period == 0) {
510195534Sscottl		if (bootverbose) {
511195534Sscottl			xpt_print(periph->path, "no sync rate available\n");
512195534Sscottl		}
513195534Sscottl		return (0);
514195534Sscottl	}
515195534Sscottl
516195534Sscottl	if (device->flags & CAM_DEV_DV_HIT_BOTTOM) {
517195534Sscottl		CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
518195534Sscottl		    ("hit async: giving up on DV\n"));
519195534Sscottl		return (0);
520195534Sscottl	}
521195534Sscottl
522195534Sscottl
523195534Sscottl	/*
524195534Sscottl	 * Jump sync_period up by one, but stop at 5MHz and fall back to Async.
525195534Sscottl	 * We don't try to remember 'last' settings to see if the SIM actually
526195534Sscottl	 * gets into the speed we want to set. We check on the SIM telling
527195534Sscottl	 * us that a requested speed is bad, but otherwise don't try and
528195534Sscottl	 * check the speed due to the asynchronous and handshake nature
529195534Sscottl	 * of speed setting.
530195534Sscottl	 */
531195534Sscottl	spi->valid = CTS_SPI_VALID_SYNC_RATE | CTS_SPI_VALID_SYNC_OFFSET;
532195534Sscottl	for (;;) {
533195534Sscottl		spi->sync_period++;
534195534Sscottl		if (spi->sync_period >= 0xf) {
535195534Sscottl			spi->sync_period = 0;
536195534Sscottl			spi->sync_offset = 0;
537195534Sscottl			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
538195534Sscottl			    ("setting to async for DV\n"));
539195534Sscottl			/*
540195534Sscottl			 * Once we hit async, we don't want to try
541195534Sscottl			 * any more settings.
542195534Sscottl			 */
543195534Sscottl			device->flags |= CAM_DEV_DV_HIT_BOTTOM;
544195534Sscottl		} else if (bootverbose) {
545195534Sscottl			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
546195534Sscottl			    ("DV: period 0x%x\n", spi->sync_period));
547195534Sscottl			printf("setting period to 0x%x\n", spi->sync_period);
548195534Sscottl		}
549195534Sscottl		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
550195534Sscottl		cts.type = CTS_TYPE_CURRENT_SETTINGS;
551195534Sscottl		xpt_action((union ccb *)&cts);
552195534Sscottl		if ((cts.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
553195534Sscottl			break;
554195534Sscottl		}
555195534Sscottl		CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
556195534Sscottl		    ("DV: failed to set period 0x%x\n", spi->sync_period));
557195534Sscottl		if (spi->sync_period == 0) {
558195534Sscottl			return (0);
559195534Sscottl		}
560195534Sscottl	}
561195534Sscottl	return (1);
562195534Sscottl}
563195534Sscottl#endif
564195534Sscottlstatic void
565195534Sscottlprobedone(struct cam_periph *periph, union ccb *done_ccb)
566195534Sscottl{
567195534Sscottl	struct ata_params *ident_buf;
568195534Sscottl	probe_softc *softc;
569195534Sscottl	struct cam_path *path;
570195534Sscottl	u_int32_t  priority;
571198389Smav	int found = 1;
572195534Sscottl
573195534Sscottl	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
574195534Sscottl
575195534Sscottl	softc = (probe_softc *)periph->softc;
576195534Sscottl	path = done_ccb->ccb_h.path;
577195534Sscottl	priority = done_ccb->ccb_h.pinfo.priority;
578195534Sscottl	ident_buf = &path->device->ident_data;
579195534Sscottl
580198708Smav	if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
581198708Smavdevice_fail:	if (cam_periph_error(done_ccb, 0, 0,
582198708Smav		    &softc->saved_ccb) == ERESTART) {
583195534Sscottl			return;
584195534Sscottl		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
585195534Sscottl			/* Don't wedge the queue */
586195534Sscottl			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
587195534Sscottl					 /*run_queue*/TRUE);
588195534Sscottl		}
589198708Smav		/* Old PIO2 devices may not support mode setting. */
590198708Smav		if (softc->action == PROBE_SETMODE &&
591198708Smav		    ata_max_pmode(ident_buf) <= ATA_PIO2 &&
592198708Smav		    (ident_buf->capabilities1 & ATA_SUPPORT_IORDY) == 0)
593198708Smav			goto noerror;
594198708Smav		/*
595198708Smav		 * If we get to this point, we got an error status back
596198708Smav		 * from the inquiry and the error status doesn't require
597198708Smav		 * automatically retrying the command.  Therefore, the
598198708Smav		 * inquiry failed.  If we had inquiry information before
599198708Smav		 * for this device, but this latest inquiry command failed,
600198708Smav		 * the device has probably gone away.  If this device isn't
601198708Smav		 * already marked unconfigured, notify the peripheral
602198708Smav		 * drivers that this device is no more.
603198708Smav		 */
604198708Smav		if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
605198708Smav			xpt_async(AC_LOST_DEVICE, path, NULL);
606198708Smav		found = 0;
607198708Smav		goto done;
608198708Smav	}
609198708Smavnoerror:
610198708Smav	switch (softc->action) {
611198708Smav	case PROBE_RESET:
612195534Sscottl	{
613198708Smav		int sign = (done_ccb->ataio.res.lba_high << 8) +
614198708Smav		    done_ccb->ataio.res.lba_mid;
615198708Smav		xpt_print(path, "SIGNATURE: %04x\n", sign);
616198708Smav		if (sign == 0x0000 &&
617198708Smav		    done_ccb->ccb_h.target_id != 15) {
618198708Smav			path->device->protocol = PROTO_ATA;
619198708Smav			PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
620198708Smav		} else if (sign == 0x9669 &&
621198708Smav		    done_ccb->ccb_h.target_id == 15) {
622198708Smav			struct ccb_trans_settings cts;
623195534Sscottl
624198708Smav				/* Report SIM that PM is present. */
625198708Smav			bzero(&cts, sizeof(cts));
626198708Smav			xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
627198708Smav			cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
628198708Smav			cts.type = CTS_TYPE_CURRENT_SETTINGS;
629198708Smav			cts.xport_specific.sata.pm_present = 1;
630198708Smav			cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
631198708Smav			xpt_action((union ccb *)&cts);
632198708Smav			path->device->protocol = PROTO_SATAPM;
633198708Smav			PROBE_SET_ACTION(softc, PROBE_PM_PID);
634198708Smav		} else if (sign == 0xeb14 &&
635198708Smav		    done_ccb->ccb_h.target_id != 15) {
636198708Smav			path->device->protocol = PROTO_SCSI;
637198708Smav			PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
638198708Smav		} else {
639198708Smav			if (done_ccb->ccb_h.target_id != 15) {
640198708Smav				xpt_print(path,
641198708Smav				    "Unexpected signature 0x%04x\n", sign);
642195534Sscottl			}
643198708Smav			goto device_fail;
644198708Smav		}
645198708Smav		xpt_release_ccb(done_ccb);
646198708Smav		xpt_schedule(periph, priority);
647198708Smav		return;
648198708Smav	}
649198708Smav	case PROBE_IDENTIFY:
650198708Smav	{
651198708Smav		int16_t *ptr;
652195534Sscottl
653198708Smav		for (ptr = (int16_t *)ident_buf;
654198708Smav		     ptr < (int16_t *)ident_buf + sizeof(struct ata_params)/2; ptr++) {
655198708Smav			*ptr = le16toh(*ptr);
656198708Smav		}
657198708Smav		if (strncmp(ident_buf->model, "FX", 2) &&
658198708Smav		    strncmp(ident_buf->model, "NEC", 3) &&
659198708Smav		    strncmp(ident_buf->model, "Pioneer", 7) &&
660198708Smav		    strncmp(ident_buf->model, "SHARP", 5)) {
661198708Smav			ata_bswap(ident_buf->model, sizeof(ident_buf->model));
662198708Smav			ata_bswap(ident_buf->revision, sizeof(ident_buf->revision));
663198708Smav			ata_bswap(ident_buf->serial, sizeof(ident_buf->serial));
664198708Smav		}
665198708Smav		ata_btrim(ident_buf->model, sizeof(ident_buf->model));
666198708Smav		ata_bpack(ident_buf->model, ident_buf->model, sizeof(ident_buf->model));
667198708Smav		ata_btrim(ident_buf->revision, sizeof(ident_buf->revision));
668198708Smav		ata_bpack(ident_buf->revision, ident_buf->revision, sizeof(ident_buf->revision));
669198708Smav		ata_btrim(ident_buf->serial, sizeof(ident_buf->serial));
670198708Smav		ata_bpack(ident_buf->serial, ident_buf->serial, sizeof(ident_buf->serial));
671195534Sscottl
672198708Smav		if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
673198708Smav			/* Check that it is the same device. */
674198708Smav			MD5_CTX context;
675198708Smav			u_int8_t digest[16];
676198708Smav
677198708Smav			MD5Init(&context);
678198708Smav			MD5Update(&context,
679198708Smav			    (unsigned char *)ident_buf->model,
680198708Smav			    sizeof(ident_buf->model));
681198708Smav			MD5Update(&context,
682198708Smav			    (unsigned char *)ident_buf->revision,
683198708Smav			    sizeof(ident_buf->revision));
684198708Smav			MD5Update(&context,
685198708Smav			    (unsigned char *)ident_buf->serial,
686198708Smav			    sizeof(ident_buf->serial));
687198708Smav			MD5Final(digest, &context);
688198708Smav			if (bcmp(digest, softc->digest, sizeof(digest))) {
689198708Smav				/* Device changed. */
690198708Smav				xpt_async(AC_LOST_DEVICE, path, NULL);
691195534Sscottl			}
692198708Smav		} else {
693195534Sscottl			/* Clean up from previous instance of this device */
694195534Sscottl			if (path->device->serial_num != NULL) {
695195534Sscottl				free(path->device->serial_num, M_CAMXPT);
696195534Sscottl				path->device->serial_num = NULL;
697195534Sscottl				path->device->serial_num_len = 0;
698195534Sscottl			}
699195534Sscottl			path->device->serial_num =
700195534Sscottl				(u_int8_t *)malloc((sizeof(ident_buf->serial) + 1),
701198708Smav					   M_CAMXPT, M_NOWAIT);
702195534Sscottl			if (path->device->serial_num != NULL) {
703195534Sscottl				bcopy(ident_buf->serial,
704195534Sscottl				      path->device->serial_num,
705195534Sscottl				      sizeof(ident_buf->serial));
706195534Sscottl				path->device->serial_num[sizeof(ident_buf->serial)]
707195534Sscottl				    = '\0';
708195534Sscottl				path->device->serial_num_len =
709195534Sscottl				    strlen(path->device->serial_num);
710195534Sscottl			}
711195534Sscottl
712198331Smav			path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
713195534Sscottl		}
714199178Smav		if (ident_buf->satacapabilities & ATA_SUPPORT_NCQ) {
715199178Smav			path->device->mintags = path->device->maxtags =
716199178Smav			    ATA_QUEUE_LEN(ident_buf->queue) + 1;
717199178Smav		}
718199178Smav		ata_find_quirk(path->device);
719199178Smav		/* XXX: If not all tags allowed, we must to tell SIM which are. */
720199178Smav		if (path->device->mintags < path->bus->sim->max_tagged_dev_openings)
721199178Smav			path->device->mintags = path->device->maxtags = 0;
722199263Smav		if (path->device->mintags != 0 &&
723199263Smav		    path->bus->sim->max_tagged_dev_openings != 0) {
724199178Smav			xpt_start_tags(path);
725199178Smav		}
726198708Smav		ata_device_transport(path);
727198708Smav		PROBE_SET_ACTION(softc, PROBE_SETMODE);
728195534Sscottl		xpt_release_ccb(done_ccb);
729198708Smav		xpt_schedule(periph, priority);
730198708Smav		return;
731195534Sscottl	}
732195534Sscottl	case PROBE_SETMODE:
733198708Smav		if (path->device->protocol == PROTO_ATA) {
734198708Smav			PROBE_SET_ACTION(softc, PROBE_SET_MULTI);
735198708Smav		} else {
736198708Smav			PROBE_SET_ACTION(softc, PROBE_INQUIRY);
737195534Sscottl		}
738198708Smav		xpt_release_ccb(done_ccb);
739198708Smav		xpt_schedule(periph, priority);
740198708Smav		return;
741198708Smav	case PROBE_SET_MULTI:
742198708Smav		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
743198708Smav			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
744198748Smav			xpt_acquire_device(path->device);
745198708Smav			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
746198708Smav			xpt_action(done_ccb);
747198708Smav			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
748198708Smav			    done_ccb);
749198708Smav		}
750198708Smav		break;
751195534Sscottl	case PROBE_INQUIRY:
752195534Sscottl	case PROBE_FULL_INQUIRY:
753195534Sscottl	{
754198708Smav		struct scsi_inquiry_data *inq_buf;
755198708Smav		u_int8_t periph_qual, len;
756195534Sscottl
757198708Smav		path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
758198708Smav		inq_buf = &path->device->inq_data;
759195534Sscottl
760198708Smav		periph_qual = SID_QUAL(inq_buf);
761195534Sscottl
762198708Smav		if (periph_qual != SID_QUAL_LU_CONNECTED)
763198708Smav			break;
764195534Sscottl
765198708Smav		/*
766198708Smav		 * We conservatively request only
767198708Smav		 * SHORT_INQUIRY_LEN bytes of inquiry
768198708Smav		 * information during our first try
769198708Smav		 * at sending an INQUIRY. If the device
770198708Smav		 * has more information to give,
771198708Smav		 * perform a second request specifying
772198708Smav		 * the amount of information the device
773198708Smav		 * is willing to give.
774198708Smav		 */
775198708Smav		len = inq_buf->additional_length
776198708Smav		    + offsetof(struct scsi_inquiry_data, additional_length) + 1;
777198708Smav		if (softc->action == PROBE_INQUIRY
778198708Smav		    && len > SHORT_INQUIRY_LENGTH) {
779198708Smav			PROBE_SET_ACTION(softc, PROBE_FULL_INQUIRY);
780198708Smav			xpt_release_ccb(done_ccb);
781198708Smav			xpt_schedule(periph, priority);
782195534Sscottl			return;
783195534Sscottl		}
784198708Smav
785198708Smav		ata_device_transport(path);
786198708Smav		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
787198708Smav			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
788198748Smav			xpt_acquire_device(path->device);
789198708Smav			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
790198708Smav			xpt_action(done_ccb);
791198708Smav			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path, done_ccb);
792198708Smav		}
793198708Smav		break;
794195534Sscottl	}
795195534Sscottl	case PROBE_PM_PID:
796198708Smav		if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) == 0)
797198708Smav			bzero(ident_buf, sizeof(*ident_buf));
798198708Smav		softc->pm_pid = (done_ccb->ataio.res.lba_high << 24) +
799198708Smav		    (done_ccb->ataio.res.lba_mid << 16) +
800198708Smav		    (done_ccb->ataio.res.lba_low << 8) +
801198708Smav		    done_ccb->ataio.res.sector_count;
802198708Smav		((uint32_t *)ident_buf)[0] = softc->pm_pid;
803198708Smav		printf("PM Product ID: %08x\n", softc->pm_pid);
804198708Smav		snprintf(ident_buf->model, sizeof(ident_buf->model),
805198708Smav		    "Port Multiplier %08x", softc->pm_pid);
806198708Smav		PROBE_SET_ACTION(softc, PROBE_PM_PRV);
807198708Smav		xpt_release_ccb(done_ccb);
808198708Smav		xpt_schedule(periph, priority);
809198708Smav		return;
810195534Sscottl	case PROBE_PM_PRV:
811198708Smav		softc->pm_prv = (done_ccb->ataio.res.lba_high << 24) +
812198708Smav		    (done_ccb->ataio.res.lba_mid << 16) +
813198708Smav		    (done_ccb->ataio.res.lba_low << 8) +
814198708Smav		    done_ccb->ataio.res.sector_count;
815198708Smav		((uint32_t *)ident_buf)[1] = softc->pm_prv;
816198708Smav		printf("PM Revision: %08x\n", softc->pm_prv);
817198708Smav		snprintf(ident_buf->revision, sizeof(ident_buf->revision),
818198708Smav		    "%04x", softc->pm_prv);
819198708Smav		path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
820198708Smav		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
821198708Smav			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
822198748Smav			xpt_acquire_device(path->device);
823198708Smav			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
824198708Smav			xpt_action(done_ccb);
825198708Smav			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
826198708Smav			    done_ccb);
827198708Smav		} else {
828198708Smav			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
829198708Smav			xpt_action(done_ccb);
830198708Smav			xpt_async(AC_SCSI_AEN, done_ccb->ccb_h.path, done_ccb);
831195534Sscottl		}
832198708Smav		break;
833195534Sscottl	case PROBE_INVALID:
834195534Sscottl		CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_INFO,
835195534Sscottl		    ("probedone: invalid action state\n"));
836195534Sscottl	default:
837195534Sscottl		break;
838195534Sscottl	}
839198708Smavdone:
840198708Smav	xpt_release_ccb(done_ccb);
841195534Sscottl	done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
842195534Sscottl	TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe);
843195534Sscottl	done_ccb->ccb_h.status = CAM_REQ_CMP;
844195534Sscottl	done_ccb->ccb_h.ppriv_field1 = found;
845195534Sscottl	xpt_done(done_ccb);
846195534Sscottl	if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
847195534Sscottl		cam_periph_invalidate(periph);
848195534Sscottl		cam_periph_release_locked(periph);
849195534Sscottl	} else {
850195534Sscottl		probeschedule(periph);
851195534Sscottl	}
852195534Sscottl}
853195534Sscottl
854195534Sscottlstatic void
855195534Sscottlprobecleanup(struct cam_periph *periph)
856195534Sscottl{
857195534Sscottl	free(periph->softc, M_CAMXPT);
858195534Sscottl}
859195534Sscottl
860195534Sscottlstatic void
861199178Smavata_find_quirk(struct cam_ed *device)
862195534Sscottl{
863199178Smav	struct ata_quirk_entry *quirk;
864195534Sscottl	caddr_t	match;
865195534Sscottl
866199178Smav	match = cam_quirkmatch((caddr_t)&device->ident_data,
867199178Smav			       (caddr_t)ata_quirk_table,
868199178Smav			       ata_quirk_table_size,
869199178Smav			       sizeof(*ata_quirk_table), ata_identify_match);
870195534Sscottl
871195534Sscottl	if (match == NULL)
872195534Sscottl		panic("xpt_find_quirk: device didn't match wildcard entry!!");
873195534Sscottl
874199178Smav	quirk = (struct ata_quirk_entry *)match;
875195534Sscottl	device->quirk = quirk;
876199178Smav	if (quirk->quirks & CAM_QUIRK_MAXTAGS)
877199178Smav		device->mintags = device->maxtags = quirk->maxtags;
878195534Sscottl}
879195534Sscottl
880195534Sscottltypedef struct {
881195534Sscottl	union	ccb *request_ccb;
882195534Sscottl	struct 	ccb_pathinq *cpi;
883195534Sscottl	int	counter;
884195534Sscottl	int	found;
885195534Sscottl} ata_scan_bus_info;
886195534Sscottl
887195534Sscottl/*
888195534Sscottl * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
889195534Sscottl * As the scan progresses, xpt_scan_bus is used as the
890195534Sscottl * callback on completion function.
891195534Sscottl */
892195534Sscottlstatic void
893195534Sscottlata_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
894195534Sscottl{
895195534Sscottl	struct	cam_path *path;
896195534Sscottl	ata_scan_bus_info *scan_info;
897195534Sscottl	union	ccb *work_ccb;
898195534Sscottl	cam_status status;
899195534Sscottl
900195534Sscottl	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
901195534Sscottl		  ("xpt_scan_bus\n"));
902195534Sscottl	switch (request_ccb->ccb_h.func_code) {
903195534Sscottl	case XPT_SCAN_BUS:
904195534Sscottl		/* Find out the characteristics of the bus */
905195534Sscottl		work_ccb = xpt_alloc_ccb_nowait();
906195534Sscottl		if (work_ccb == NULL) {
907195534Sscottl			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
908195534Sscottl			xpt_done(request_ccb);
909195534Sscottl			return;
910195534Sscottl		}
911195534Sscottl		xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
912195534Sscottl			      request_ccb->ccb_h.pinfo.priority);
913195534Sscottl		work_ccb->ccb_h.func_code = XPT_PATH_INQ;
914195534Sscottl		xpt_action(work_ccb);
915195534Sscottl		if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
916195534Sscottl			request_ccb->ccb_h.status = work_ccb->ccb_h.status;
917195534Sscottl			xpt_free_ccb(work_ccb);
918195534Sscottl			xpt_done(request_ccb);
919195534Sscottl			return;
920195534Sscottl		}
921195534Sscottl
922195534Sscottl		/* Save some state for use while we probe for devices */
923195534Sscottl		scan_info = (ata_scan_bus_info *)
924195534Sscottl		    malloc(sizeof(ata_scan_bus_info), M_CAMXPT, M_NOWAIT);
925195534Sscottl		if (scan_info == NULL) {
926195534Sscottl			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
927195534Sscottl			xpt_done(request_ccb);
928195534Sscottl			return;
929195534Sscottl		}
930195534Sscottl		scan_info->request_ccb = request_ccb;
931195534Sscottl		scan_info->cpi = &work_ccb->cpi;
932198331Smav		if (scan_info->cpi->transport == XPORT_ATA)
933198331Smav			scan_info->found = 0x0003;
934198331Smav		else
935198331Smav			scan_info->found = 0x8001;
936195534Sscottl		scan_info->counter = 0;
937195534Sscottl		/* If PM supported, probe it first. */
938195534Sscottl		if (scan_info->cpi->hba_inquiry & PI_SATAPM)
939195534Sscottl			scan_info->counter = 15;
940195534Sscottl
941195534Sscottl		work_ccb = xpt_alloc_ccb_nowait();
942195534Sscottl		if (work_ccb == NULL) {
943195534Sscottl			free(scan_info, M_CAMXPT);
944195534Sscottl			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
945195534Sscottl			xpt_done(request_ccb);
946195534Sscottl			break;
947195534Sscottl		}
948195534Sscottl		goto scan_next;
949195534Sscottl	case XPT_SCAN_LUN:
950195534Sscottl		work_ccb = request_ccb;
951195534Sscottl		/* Reuse the same CCB to query if a device was really found */
952195534Sscottl		scan_info = (ata_scan_bus_info *)work_ccb->ccb_h.ppriv_ptr0;
953195534Sscottl		/* Free the current request path- we're done with it. */
954195534Sscottl		xpt_free_path(work_ccb->ccb_h.path);
955198389Smav		/* If there is PMP... */
956195534Sscottl		if (scan_info->counter == 15) {
957195534Sscottl			if (work_ccb->ccb_h.ppriv_field1 != 0) {
958198389Smav				/* everything else willbe probed by it */
959198389Smav				scan_info->found = 0x8000;
960195534Sscottl			} else {
961195534Sscottl				struct ccb_trans_settings cts;
962195534Sscottl
963195534Sscottl				/* Report SIM that PM is absent. */
964195534Sscottl				bzero(&cts, sizeof(cts));
965195534Sscottl				xpt_setup_ccb(&cts.ccb_h,
966195534Sscottl				    scan_info->request_ccb->ccb_h.path, 1);
967195534Sscottl				cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
968195534Sscottl				cts.type = CTS_TYPE_CURRENT_SETTINGS;
969195665Smav				cts.xport_specific.sata.pm_present = 0;
970195534Sscottl				cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
971195534Sscottl				xpt_action((union ccb *)&cts);
972195534Sscottl			}
973195534Sscottl		}
974195534Sscottltake_next:
975195534Sscottl		/* Take next device. Wrap from 15 (PM) to 0. */
976195534Sscottl		scan_info->counter = (scan_info->counter + 1 ) & 0x0f;
977198322Smav		if (scan_info->counter > scan_info->cpi->max_target -
978198322Smav		    ((scan_info->cpi->hba_inquiry & PI_SATAPM) ? 1 : 0)) {
979195534Sscottl			xpt_free_ccb(work_ccb);
980195534Sscottl			xpt_free_ccb((union ccb *)scan_info->cpi);
981195534Sscottl			request_ccb = scan_info->request_ccb;
982195534Sscottl			free(scan_info, M_CAMXPT);
983195534Sscottl			request_ccb->ccb_h.status = CAM_REQ_CMP;
984195534Sscottl			xpt_done(request_ccb);
985195534Sscottl			break;
986195534Sscottl		}
987195534Sscottlscan_next:
988198389Smav		if ((scan_info->found & (1 << scan_info->counter)) == 0)
989198389Smav			goto take_next;
990195534Sscottl		status = xpt_create_path(&path, xpt_periph,
991195534Sscottl		    scan_info->request_ccb->ccb_h.path_id,
992195534Sscottl		    scan_info->counter, 0);
993195534Sscottl		if (status != CAM_REQ_CMP) {
994195534Sscottl			printf("xpt_scan_bus: xpt_create_path failed"
995195534Sscottl			    " with status %#x, bus scan halted\n",
996195534Sscottl			    status);
997195534Sscottl			xpt_free_ccb(work_ccb);
998195534Sscottl			xpt_free_ccb((union ccb *)scan_info->cpi);
999195534Sscottl			request_ccb = scan_info->request_ccb;
1000195534Sscottl			free(scan_info, M_CAMXPT);
1001195534Sscottl			request_ccb->ccb_h.status = status;
1002195534Sscottl			xpt_done(request_ccb);
1003195534Sscottl			break;
1004195534Sscottl		}
1005195534Sscottl		xpt_setup_ccb(&work_ccb->ccb_h, path,
1006195534Sscottl		    scan_info->request_ccb->ccb_h.pinfo.priority);
1007195534Sscottl		work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1008195534Sscottl		work_ccb->ccb_h.cbfcnp = ata_scan_bus;
1009195534Sscottl		work_ccb->ccb_h.ppriv_ptr0 = scan_info;
1010195534Sscottl		work_ccb->crcn.flags = scan_info->request_ccb->crcn.flags;
1011195534Sscottl		xpt_action(work_ccb);
1012195534Sscottl		break;
1013195534Sscottl	default:
1014195534Sscottl		break;
1015195534Sscottl	}
1016195534Sscottl}
1017195534Sscottl
1018195534Sscottlstatic void
1019195534Sscottlata_scan_lun(struct cam_periph *periph, struct cam_path *path,
1020195534Sscottl	     cam_flags flags, union ccb *request_ccb)
1021195534Sscottl{
1022195534Sscottl	struct ccb_pathinq cpi;
1023195534Sscottl	cam_status status;
1024195534Sscottl	struct cam_path *new_path;
1025195534Sscottl	struct cam_periph *old_periph;
1026195534Sscottl
1027195534Sscottl	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
1028195534Sscottl		  ("xpt_scan_lun\n"));
1029195534Sscottl
1030198382Smav	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
1031195534Sscottl	cpi.ccb_h.func_code = XPT_PATH_INQ;
1032195534Sscottl	xpt_action((union ccb *)&cpi);
1033195534Sscottl
1034195534Sscottl	if (cpi.ccb_h.status != CAM_REQ_CMP) {
1035195534Sscottl		if (request_ccb != NULL) {
1036195534Sscottl			request_ccb->ccb_h.status = cpi.ccb_h.status;
1037195534Sscottl			xpt_done(request_ccb);
1038195534Sscottl		}
1039195534Sscottl		return;
1040195534Sscottl	}
1041195534Sscottl
1042195534Sscottl	if (request_ccb == NULL) {
1043195534Sscottl		request_ccb = malloc(sizeof(union ccb), M_CAMXPT, M_NOWAIT);
1044195534Sscottl		if (request_ccb == NULL) {
1045195534Sscottl			xpt_print(path, "xpt_scan_lun: can't allocate CCB, "
1046195534Sscottl			    "can't continue\n");
1047195534Sscottl			return;
1048195534Sscottl		}
1049195534Sscottl		new_path = malloc(sizeof(*new_path), M_CAMXPT, M_NOWAIT);
1050195534Sscottl		if (new_path == NULL) {
1051195534Sscottl			xpt_print(path, "xpt_scan_lun: can't allocate path, "
1052195534Sscottl			    "can't continue\n");
1053195534Sscottl			free(request_ccb, M_CAMXPT);
1054195534Sscottl			return;
1055195534Sscottl		}
1056195534Sscottl		status = xpt_compile_path(new_path, xpt_periph,
1057195534Sscottl					  path->bus->path_id,
1058195534Sscottl					  path->target->target_id,
1059195534Sscottl					  path->device->lun_id);
1060195534Sscottl
1061195534Sscottl		if (status != CAM_REQ_CMP) {
1062195534Sscottl			xpt_print(path, "xpt_scan_lun: can't compile path, "
1063195534Sscottl			    "can't continue\n");
1064195534Sscottl			free(request_ccb, M_CAMXPT);
1065195534Sscottl			free(new_path, M_CAMXPT);
1066195534Sscottl			return;
1067195534Sscottl		}
1068198382Smav		xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_NORMAL);
1069195534Sscottl		request_ccb->ccb_h.cbfcnp = xptscandone;
1070195534Sscottl		request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1071195534Sscottl		request_ccb->crcn.flags = flags;
1072195534Sscottl	}
1073195534Sscottl
1074195653Smav	if ((old_periph = cam_periph_find(path, "aprobe")) != NULL) {
1075195534Sscottl		probe_softc *softc;
1076195534Sscottl
1077195534Sscottl		softc = (probe_softc *)old_periph->softc;
1078195534Sscottl		TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
1079195534Sscottl				  periph_links.tqe);
1080195534Sscottl	} else {
1081195534Sscottl		status = cam_periph_alloc(proberegister, NULL, probecleanup,
1082195653Smav					  probestart, "aprobe",
1083195534Sscottl					  CAM_PERIPH_BIO,
1084195534Sscottl					  request_ccb->ccb_h.path, NULL, 0,
1085195534Sscottl					  request_ccb);
1086195534Sscottl
1087195534Sscottl		if (status != CAM_REQ_CMP) {
1088195534Sscottl			xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
1089195534Sscottl			    "returned an error, can't continue probe\n");
1090195534Sscottl			request_ccb->ccb_h.status = status;
1091195534Sscottl			xpt_done(request_ccb);
1092195534Sscottl		}
1093195534Sscottl	}
1094195534Sscottl}
1095195534Sscottl
1096195534Sscottlstatic void
1097195534Sscottlxptscandone(struct cam_periph *periph, union ccb *done_ccb)
1098195534Sscottl{
1099195534Sscottl	xpt_release_path(done_ccb->ccb_h.path);
1100195534Sscottl	free(done_ccb->ccb_h.path, M_CAMXPT);
1101195534Sscottl	free(done_ccb, M_CAMXPT);
1102195534Sscottl}
1103195534Sscottl
1104195534Sscottlstatic struct cam_ed *
1105195534Sscottlata_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
1106195534Sscottl{
1107195534Sscottl	struct cam_path path;
1108199178Smav	struct ata_quirk_entry *quirk;
1109195534Sscottl	struct cam_ed *device;
1110195534Sscottl	struct cam_ed *cur_device;
1111195534Sscottl
1112195534Sscottl	device = xpt_alloc_device(bus, target, lun_id);
1113195534Sscottl	if (device == NULL)
1114195534Sscottl		return (NULL);
1115195534Sscottl
1116195534Sscottl	/*
1117195534Sscottl	 * Take the default quirk entry until we have inquiry
1118195534Sscottl	 * data and can determine a better quirk to use.
1119195534Sscottl	 */
1120199178Smav	quirk = &ata_quirk_table[ata_quirk_table_size - 1];
1121195534Sscottl	device->quirk = (void *)quirk;
1122199178Smav	device->mintags = 0;
1123199178Smav	device->maxtags = 0;
1124195534Sscottl	bzero(&device->inq_data, sizeof(device->inq_data));
1125195534Sscottl	device->inq_flags = 0;
1126195534Sscottl	device->queue_flags = 0;
1127195534Sscottl	device->serial_num = NULL;
1128195534Sscottl	device->serial_num_len = 0;
1129195534Sscottl
1130195534Sscottl	/*
1131195534Sscottl	 * XXX should be limited by number of CCBs this bus can
1132195534Sscottl	 * do.
1133195534Sscottl	 */
1134195534Sscottl	bus->sim->max_ccbs += device->ccbq.devq_openings;
1135195534Sscottl	/* Insertion sort into our target's device list */
1136195534Sscottl	cur_device = TAILQ_FIRST(&target->ed_entries);
1137195534Sscottl	while (cur_device != NULL && cur_device->lun_id < lun_id)
1138195534Sscottl		cur_device = TAILQ_NEXT(cur_device, links);
1139195534Sscottl	if (cur_device != NULL) {
1140195534Sscottl		TAILQ_INSERT_BEFORE(cur_device, device, links);
1141195534Sscottl	} else {
1142195534Sscottl		TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
1143195534Sscottl	}
1144195534Sscottl	target->generation++;
1145195534Sscottl	if (lun_id != CAM_LUN_WILDCARD) {
1146195534Sscottl		xpt_compile_path(&path,
1147195534Sscottl				 NULL,
1148195534Sscottl				 bus->path_id,
1149195534Sscottl				 target->target_id,
1150195534Sscottl				 lun_id);
1151195534Sscottl		ata_device_transport(&path);
1152195534Sscottl		xpt_release_path(&path);
1153195534Sscottl	}
1154195534Sscottl
1155195534Sscottl	return (device);
1156195534Sscottl}
1157195534Sscottl
1158195534Sscottlstatic void
1159195534Sscottlata_device_transport(struct cam_path *path)
1160195534Sscottl{
1161195534Sscottl	struct ccb_pathinq cpi;
1162198331Smav	struct ccb_trans_settings cts;
1163198331Smav	struct scsi_inquiry_data *inq_buf = NULL;
1164198331Smav	struct ata_params *ident_buf = NULL;
1165195534Sscottl
1166195534Sscottl	/* Get transport information from the SIM */
1167198382Smav	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
1168195534Sscottl	cpi.ccb_h.func_code = XPT_PATH_INQ;
1169195534Sscottl	xpt_action((union ccb *)&cpi);
1170195534Sscottl
1171195534Sscottl	path->device->transport = cpi.transport;
1172198331Smav	if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
1173198331Smav		inq_buf = &path->device->inq_data;
1174198331Smav	if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) != 0)
1175198331Smav		ident_buf = &path->device->ident_data;
1176198331Smav	if (path->device->protocol == PROTO_ATA) {
1177198331Smav		path->device->protocol_version = ident_buf ?
1178198331Smav		    ata_version(ident_buf->version_major) : cpi.protocol_version;
1179198331Smav	} else if (path->device->protocol == PROTO_SCSI) {
1180198331Smav		path->device->protocol_version = inq_buf ?
1181198331Smav		    SID_ANSI_REV(inq_buf) : cpi.protocol_version;
1182195534Sscottl	}
1183198331Smav	path->device->transport_version = ident_buf ?
1184198331Smav	    ata_version(ident_buf->version_major) : cpi.transport_version;
1185195534Sscottl
1186195534Sscottl	/* Tell the controller what we think */
1187198382Smav	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
1188195534Sscottl	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1189195534Sscottl	cts.type = CTS_TYPE_CURRENT_SETTINGS;
1190195534Sscottl	cts.transport = path->device->transport;
1191195534Sscottl	cts.transport_version = path->device->transport_version;
1192195534Sscottl	cts.protocol = path->device->protocol;
1193195534Sscottl	cts.protocol_version = path->device->protocol_version;
1194195534Sscottl	cts.proto_specific.valid = 0;
1195195534Sscottl	cts.xport_specific.valid = 0;
1196195534Sscottl	xpt_action((union ccb *)&cts);
1197195534Sscottl}
1198195534Sscottl
1199195534Sscottlstatic void
1200195534Sscottlata_action(union ccb *start_ccb)
1201195534Sscottl{
1202195534Sscottl
1203195534Sscottl	switch (start_ccb->ccb_h.func_code) {
1204195534Sscottl	case XPT_SET_TRAN_SETTINGS:
1205195534Sscottl	{
1206199178Smav		ata_set_transfer_settings(&start_ccb->cts,
1207195534Sscottl					   start_ccb->ccb_h.path->device,
1208195534Sscottl					   /*async_update*/FALSE);
1209195534Sscottl		break;
1210195534Sscottl	}
1211195534Sscottl	case XPT_SCAN_BUS:
1212195534Sscottl		ata_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
1213195534Sscottl		break;
1214195534Sscottl	case XPT_SCAN_LUN:
1215195534Sscottl		ata_scan_lun(start_ccb->ccb_h.path->periph,
1216195534Sscottl			      start_ccb->ccb_h.path, start_ccb->crcn.flags,
1217195534Sscottl			      start_ccb);
1218195534Sscottl		break;
1219195534Sscottl	case XPT_GET_TRAN_SETTINGS:
1220195534Sscottl	{
1221195534Sscottl		struct cam_sim *sim;
1222195534Sscottl
1223195534Sscottl		sim = start_ccb->ccb_h.path->bus->sim;
1224195534Sscottl		(*(sim->sim_action))(sim, start_ccb);
1225195534Sscottl		break;
1226195534Sscottl	}
1227195534Sscottl	default:
1228195534Sscottl		xpt_action_default(start_ccb);
1229195534Sscottl		break;
1230195534Sscottl	}
1231195534Sscottl}
1232195534Sscottl
1233195534Sscottlstatic void
1234199178Smavata_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
1235195534Sscottl			   int async_update)
1236195534Sscottl{
1237195534Sscottl	struct	ccb_pathinq cpi;
1238195534Sscottl	struct	ccb_trans_settings cur_cts;
1239195534Sscottl	struct	ccb_trans_settings_scsi *scsi;
1240195534Sscottl	struct	ccb_trans_settings_scsi *cur_scsi;
1241195534Sscottl	struct	cam_sim *sim;
1242195534Sscottl	struct	scsi_inquiry_data *inq_data;
1243195534Sscottl
1244195534Sscottl	if (device == NULL) {
1245195534Sscottl		cts->ccb_h.status = CAM_PATH_INVALID;
1246195534Sscottl		xpt_done((union ccb *)cts);
1247195534Sscottl		return;
1248195534Sscottl	}
1249195534Sscottl
1250195534Sscottl	if (cts->protocol == PROTO_UNKNOWN
1251195534Sscottl	 || cts->protocol == PROTO_UNSPECIFIED) {
1252195534Sscottl		cts->protocol = device->protocol;
1253195534Sscottl		cts->protocol_version = device->protocol_version;
1254195534Sscottl	}
1255195534Sscottl
1256195534Sscottl	if (cts->protocol_version == PROTO_VERSION_UNKNOWN
1257195534Sscottl	 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
1258195534Sscottl		cts->protocol_version = device->protocol_version;
1259195534Sscottl
1260195534Sscottl	if (cts->protocol != device->protocol) {
1261195534Sscottl		xpt_print(cts->ccb_h.path, "Uninitialized Protocol %x:%x?\n",
1262195534Sscottl		       cts->protocol, device->protocol);
1263195534Sscottl		cts->protocol = device->protocol;
1264195534Sscottl	}
1265195534Sscottl
1266195534Sscottl	if (cts->protocol_version > device->protocol_version) {
1267195534Sscottl		if (bootverbose) {
1268195534Sscottl			xpt_print(cts->ccb_h.path, "Down reving Protocol "
1269195534Sscottl			    "Version from %d to %d?\n", cts->protocol_version,
1270195534Sscottl			    device->protocol_version);
1271195534Sscottl		}
1272195534Sscottl		cts->protocol_version = device->protocol_version;
1273195534Sscottl	}
1274195534Sscottl
1275195534Sscottl	if (cts->transport == XPORT_UNKNOWN
1276195534Sscottl	 || cts->transport == XPORT_UNSPECIFIED) {
1277195534Sscottl		cts->transport = device->transport;
1278195534Sscottl		cts->transport_version = device->transport_version;
1279195534Sscottl	}
1280195534Sscottl
1281195534Sscottl	if (cts->transport_version == XPORT_VERSION_UNKNOWN
1282195534Sscottl	 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
1283195534Sscottl		cts->transport_version = device->transport_version;
1284195534Sscottl
1285195534Sscottl	if (cts->transport != device->transport) {
1286195534Sscottl		xpt_print(cts->ccb_h.path, "Uninitialized Transport %x:%x?\n",
1287195534Sscottl		    cts->transport, device->transport);
1288195534Sscottl		cts->transport = device->transport;
1289195534Sscottl	}
1290195534Sscottl
1291195534Sscottl	if (cts->transport_version > device->transport_version) {
1292195534Sscottl		if (bootverbose) {
1293195534Sscottl			xpt_print(cts->ccb_h.path, "Down reving Transport "
1294195534Sscottl			    "Version from %d to %d?\n", cts->transport_version,
1295195534Sscottl			    device->transport_version);
1296195534Sscottl		}
1297195534Sscottl		cts->transport_version = device->transport_version;
1298195534Sscottl	}
1299195534Sscottl
1300195534Sscottl	sim = cts->ccb_h.path->bus->sim;
1301195534Sscottl
1302195534Sscottl	/*
1303195534Sscottl	 * Nothing more of interest to do unless
1304195534Sscottl	 * this is a device connected via the
1305195534Sscottl	 * SCSI protocol.
1306195534Sscottl	 */
1307195534Sscottl	if (cts->protocol != PROTO_SCSI) {
1308195534Sscottl		if (async_update == FALSE)
1309195534Sscottl			(*(sim->sim_action))(sim, (union ccb *)cts);
1310195534Sscottl		return;
1311195534Sscottl	}
1312195534Sscottl
1313195534Sscottl	inq_data = &device->inq_data;
1314195534Sscottl	scsi = &cts->proto_specific.scsi;
1315198382Smav	xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NORMAL);
1316195534Sscottl	cpi.ccb_h.func_code = XPT_PATH_INQ;
1317195534Sscottl	xpt_action((union ccb *)&cpi);
1318195534Sscottl
1319195534Sscottl	/* SCSI specific sanity checking */
1320195534Sscottl	if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
1321195534Sscottl	 || (INQ_DATA_TQ_ENABLED(inq_data)) == 0
1322195534Sscottl	 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
1323195534Sscottl	 || (device->mintags == 0)) {
1324195534Sscottl		/*
1325195534Sscottl		 * Can't tag on hardware that doesn't support tags,
1326195534Sscottl		 * doesn't have it enabled, or has broken tag support.
1327195534Sscottl		 */
1328195534Sscottl		scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1329195534Sscottl	}
1330195534Sscottl
1331195534Sscottl	if (async_update == FALSE) {
1332195534Sscottl		/*
1333195534Sscottl		 * Perform sanity checking against what the
1334195534Sscottl		 * controller and device can do.
1335195534Sscottl		 */
1336198382Smav		xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NORMAL);
1337195534Sscottl		cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1338195534Sscottl		cur_cts.type = cts->type;
1339195534Sscottl		xpt_action((union ccb *)&cur_cts);
1340195534Sscottl		if ((cur_cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1341195534Sscottl			return;
1342195534Sscottl		}
1343195534Sscottl		cur_scsi = &cur_cts.proto_specific.scsi;
1344195534Sscottl		if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
1345195534Sscottl			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1346195534Sscottl			scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
1347195534Sscottl		}
1348195534Sscottl		if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
1349195534Sscottl			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1350195534Sscottl	}
1351195534Sscottl
1352195534Sscottl	if (cts->type == CTS_TYPE_CURRENT_SETTINGS
1353195534Sscottl	 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
1354195534Sscottl		int device_tagenb;
1355195534Sscottl
1356195534Sscottl		/*
1357195534Sscottl		 * If we are transitioning from tags to no-tags or
1358195534Sscottl		 * vice-versa, we need to carefully freeze and restart
1359195534Sscottl		 * the queue so that we don't overlap tagged and non-tagged
1360195534Sscottl		 * commands.  We also temporarily stop tags if there is
1361195534Sscottl		 * a change in transfer negotiation settings to allow
1362195534Sscottl		 * "tag-less" negotiation.
1363195534Sscottl		 */
1364195534Sscottl		if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
1365195534Sscottl		 || (device->inq_flags & SID_CmdQue) != 0)
1366195534Sscottl			device_tagenb = TRUE;
1367195534Sscottl		else
1368195534Sscottl			device_tagenb = FALSE;
1369195534Sscottl
1370195534Sscottl		if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
1371195534Sscottl		  && device_tagenb == FALSE)
1372195534Sscottl		 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
1373195534Sscottl		  && device_tagenb == TRUE)) {
1374195534Sscottl
1375195534Sscottl			if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
1376195534Sscottl				/*
1377195534Sscottl				 * Delay change to use tags until after a
1378195534Sscottl				 * few commands have gone to this device so
1379195534Sscottl				 * the controller has time to perform transfer
1380195534Sscottl				 * negotiations without tagged messages getting
1381195534Sscottl				 * in the way.
1382195534Sscottl				 */
1383195534Sscottl				device->tag_delay_count = CAM_TAG_DELAY_COUNT;
1384195534Sscottl				device->flags |= CAM_DEV_TAG_AFTER_COUNT;
1385195534Sscottl			} else {
1386199178Smav				xpt_stop_tags(cts->ccb_h.path);
1387195534Sscottl			}
1388195534Sscottl		}
1389195534Sscottl	}
1390195534Sscottl	if (async_update == FALSE)
1391195534Sscottl		(*(sim->sim_action))(sim, (union ccb *)cts);
1392195534Sscottl}
1393195534Sscottl
1394195534Sscottl/*
1395195534Sscottl * Handle any per-device event notifications that require action by the XPT.
1396195534Sscottl */
1397195534Sscottlstatic void
1398195534Sscottlata_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
1399195534Sscottl	      struct cam_ed *device, void *async_arg)
1400195534Sscottl{
1401195534Sscottl	cam_status status;
1402195534Sscottl	struct cam_path newpath;
1403195534Sscottl
1404195534Sscottl	/*
1405195534Sscottl	 * We only need to handle events for real devices.
1406195534Sscottl	 */
1407195534Sscottl	if (target->target_id == CAM_TARGET_WILDCARD
1408195534Sscottl	 || device->lun_id == CAM_LUN_WILDCARD)
1409195534Sscottl		return;
1410195534Sscottl
1411195534Sscottl	/*
1412195534Sscottl	 * We need our own path with wildcards expanded to
1413195534Sscottl	 * handle certain types of events.
1414195534Sscottl	 */
1415195534Sscottl	if ((async_code == AC_SENT_BDR)
1416195534Sscottl	 || (async_code == AC_BUS_RESET)
1417195534Sscottl	 || (async_code == AC_INQ_CHANGED))
1418195534Sscottl		status = xpt_compile_path(&newpath, NULL,
1419195534Sscottl					  bus->path_id,
1420195534Sscottl					  target->target_id,
1421195534Sscottl					  device->lun_id);
1422195534Sscottl	else
1423195534Sscottl		status = CAM_REQ_CMP_ERR;
1424195534Sscottl
1425195534Sscottl	if (status == CAM_REQ_CMP) {
1426195534Sscottl		if (async_code == AC_INQ_CHANGED) {
1427195534Sscottl			/*
1428195534Sscottl			 * We've sent a start unit command, or
1429195534Sscottl			 * something similar to a device that
1430195534Sscottl			 * may have caused its inquiry data to
1431195534Sscottl			 * change. So we re-scan the device to
1432195534Sscottl			 * refresh the inquiry data for it.
1433195534Sscottl			 */
1434195534Sscottl			ata_scan_lun(newpath.periph, &newpath,
1435195534Sscottl				     CAM_EXPECT_INQ_CHANGE, NULL);
1436195534Sscottl		}
1437195534Sscottl		xpt_release_path(&newpath);
1438198748Smav	} else if (async_code == AC_LOST_DEVICE &&
1439198748Smav	    (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
1440195534Sscottl		device->flags |= CAM_DEV_UNCONFIGURED;
1441198748Smav		xpt_release_device(device);
1442195534Sscottl	} else if (async_code == AC_TRANSFER_NEG) {
1443195534Sscottl		struct ccb_trans_settings *settings;
1444195534Sscottl
1445195534Sscottl		settings = (struct ccb_trans_settings *)async_arg;
1446199178Smav		ata_set_transfer_settings(settings, device,
1447195534Sscottl					  /*async_update*/TRUE);
1448195534Sscottl	}
1449195534Sscottl}
1450195534Sscottl
1451