ata_xpt.c revision 207499
1275218Sbapt/*-
2275218Sbapt * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org>
3275218Sbapt * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer,
10 *    without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/cam/ata/ata_xpt.c 207499 2010-05-02 12:07:47Z mav $");
29
30#include <sys/param.h>
31#include <sys/bus.h>
32#include <sys/endian.h>
33#include <sys/systm.h>
34#include <sys/types.h>
35#include <sys/malloc.h>
36#include <sys/kernel.h>
37#include <sys/time.h>
38#include <sys/conf.h>
39#include <sys/fcntl.h>
40#include <sys/interrupt.h>
41#include <sys/sbuf.h>
42
43#include <sys/lock.h>
44#include <sys/mutex.h>
45#include <sys/sysctl.h>
46
47#ifdef PC98
48#include <pc98/pc98/pc98_machdep.h>	/* geometry translation */
49#endif
50
51#include <cam/cam.h>
52#include <cam/cam_ccb.h>
53#include <cam/cam_queue.h>
54#include <cam/cam_periph.h>
55#include <cam/cam_sim.h>
56#include <cam/cam_xpt.h>
57#include <cam/cam_xpt_sim.h>
58#include <cam/cam_xpt_periph.h>
59#include <cam/cam_xpt_internal.h>
60#include <cam/cam_debug.h>
61
62#include <cam/scsi/scsi_all.h>
63#include <cam/scsi/scsi_message.h>
64#include <cam/ata/ata_all.h>
65#include <machine/stdarg.h>	/* for xpt_print below */
66#include "opt_cam.h"
67
68struct ata_quirk_entry {
69	struct scsi_inquiry_pattern inq_pat;
70	u_int8_t quirks;
71#define	CAM_QUIRK_MAXTAGS	0x01
72	u_int maxtags;
73};
74
75static periph_init_t probe_periph_init;
76
77static struct periph_driver probe_driver =
78{
79	probe_periph_init, "aprobe",
80	TAILQ_HEAD_INITIALIZER(probe_driver.units), /* generation */ 0,
81	CAM_PERIPH_DRV_EARLY
82};
83
84PERIPHDRIVER_DECLARE(aprobe, probe_driver);
85
86typedef enum {
87	PROBE_RESET,
88	PROBE_IDENTIFY,
89	PROBE_SPINUP,
90	PROBE_SETMODE,
91	PROBE_SETPM,
92	PROBE_SETAPST,
93	PROBE_SETDMAAA,
94	PROBE_SET_MULTI,
95	PROBE_INQUIRY,
96	PROBE_FULL_INQUIRY,
97	PROBE_PM_PID,
98	PROBE_PM_PRV,
99	PROBE_INVALID
100} probe_action;
101
102static char *probe_action_text[] = {
103	"PROBE_RESET",
104	"PROBE_IDENTIFY",
105	"PROBE_SPINUP",
106	"PROBE_SETMODE",
107	"PROBE_SETPM",
108	"PROBE_SETAPST",
109	"PROBE_SETDMAAA",
110	"PROBE_SET_MULTI",
111	"PROBE_INQUIRY",
112	"PROBE_FULL_INQUIRY",
113	"PROBE_PM_PID",
114	"PROBE_PM_PRV",
115	"PROBE_INVALID"
116};
117
118#define PROBE_SET_ACTION(softc, newaction)	\
119do {									\
120	char **text;							\
121	text = probe_action_text;					\
122	CAM_DEBUG((softc)->periph->path, CAM_DEBUG_INFO,		\
123	    ("Probe %s to %s\n", text[(softc)->action],			\
124	    text[(newaction)]));					\
125	(softc)->action = (newaction);					\
126} while(0)
127
128typedef enum {
129	PROBE_NO_ANNOUNCE	= 0x04
130} probe_flags;
131
132typedef struct {
133	TAILQ_HEAD(, ccb_hdr) request_ccbs;
134	struct ata_params	ident_data;
135	probe_action	action;
136	probe_flags	flags;
137	uint32_t	pm_pid;
138	uint32_t	pm_prv;
139	int		restart;
140	int		spinup;
141	u_int		caps;
142	struct cam_periph *periph;
143} probe_softc;
144
145static struct ata_quirk_entry ata_quirk_table[] =
146{
147	{
148		/* Default tagged queuing parameters for all devices */
149		{
150		  T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
151		  /*vendor*/"*", /*product*/"*", /*revision*/"*"
152		},
153		/*quirks*/0, /*maxtags*/0
154	},
155};
156
157static const int ata_quirk_table_size =
158	sizeof(ata_quirk_table) / sizeof(*ata_quirk_table);
159
160static cam_status	proberegister(struct cam_periph *periph,
161				      void *arg);
162static void	 probeschedule(struct cam_periph *probe_periph);
163static void	 probestart(struct cam_periph *periph, union ccb *start_ccb);
164//static void	 proberequestdefaultnegotiation(struct cam_periph *periph);
165//static int       proberequestbackoff(struct cam_periph *periph,
166//				     struct cam_ed *device);
167static void	 probedone(struct cam_periph *periph, union ccb *done_ccb);
168static void	 probecleanup(struct cam_periph *periph);
169static void	 ata_find_quirk(struct cam_ed *device);
170static void	 ata_scan_bus(struct cam_periph *periph, union ccb *ccb);
171static void	 ata_scan_lun(struct cam_periph *periph,
172			       struct cam_path *path, cam_flags flags,
173			       union ccb *ccb);
174static void	 xptscandone(struct cam_periph *periph, union ccb *done_ccb);
175static struct cam_ed *
176		 ata_alloc_device(struct cam_eb *bus, struct cam_et *target,
177				   lun_id_t lun_id);
178static void	 ata_device_transport(struct cam_path *path);
179static void	 ata_set_transfer_settings(struct ccb_trans_settings *cts,
180					    struct cam_ed *device,
181					    int async_update);
182static void	 ata_dev_async(u_int32_t async_code,
183				struct cam_eb *bus,
184				struct cam_et *target,
185				struct cam_ed *device,
186				void *async_arg);
187static void	 ata_action(union ccb *start_ccb);
188static void	 ata_announce_periph(struct cam_periph *periph);
189
190static struct xpt_xport ata_xport = {
191	.alloc_device = ata_alloc_device,
192	.action = ata_action,
193	.async = ata_dev_async,
194	.announce = ata_announce_periph,
195};
196
197struct xpt_xport *
198ata_get_xport(void)
199{
200	return (&ata_xport);
201}
202
203static void
204probe_periph_init()
205{
206}
207
208static cam_status
209proberegister(struct cam_periph *periph, void *arg)
210{
211	union ccb *request_ccb;	/* CCB representing the probe request */
212	cam_status status;
213	probe_softc *softc;
214
215	request_ccb = (union ccb *)arg;
216	if (periph == NULL) {
217		printf("proberegister: periph was NULL!!\n");
218		return(CAM_REQ_CMP_ERR);
219	}
220
221	if (request_ccb == NULL) {
222		printf("proberegister: no probe CCB, "
223		       "can't register device\n");
224		return(CAM_REQ_CMP_ERR);
225	}
226
227	softc = (probe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_ZERO | M_NOWAIT);
228
229	if (softc == NULL) {
230		printf("proberegister: Unable to probe new device. "
231		       "Unable to allocate softc\n");
232		return(CAM_REQ_CMP_ERR);
233	}
234	TAILQ_INIT(&softc->request_ccbs);
235	TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
236			  periph_links.tqe);
237	softc->flags = 0;
238	periph->softc = softc;
239	softc->periph = periph;
240	softc->action = PROBE_INVALID;
241	status = cam_periph_acquire(periph);
242	if (status != CAM_REQ_CMP) {
243		return (status);
244	}
245	/*
246	 * Ensure nobody slip in until probe finish.
247	 */
248	cam_freeze_devq_arg(periph->path,
249	    RELSIM_RELEASE_RUNLEVEL, CAM_RL_XPT + 1);
250	probeschedule(periph);
251	return(CAM_REQ_CMP);
252}
253
254static void
255probeschedule(struct cam_periph *periph)
256{
257	union ccb *ccb;
258	probe_softc *softc;
259
260	softc = (probe_softc *)periph->softc;
261	ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
262
263	if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) ||
264	    periph->path->device->protocol == PROTO_SATAPM)
265		PROBE_SET_ACTION(softc, PROBE_RESET);
266	else
267		PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
268
269	if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
270		softc->flags |= PROBE_NO_ANNOUNCE;
271	else
272		softc->flags &= ~PROBE_NO_ANNOUNCE;
273
274	xpt_schedule(periph, CAM_PRIORITY_XPT);
275}
276
277static void
278probestart(struct cam_periph *periph, union ccb *start_ccb)
279{
280	struct ccb_trans_settings cts;
281	struct ccb_ataio *ataio;
282	struct ccb_scsiio *csio;
283	probe_softc *softc;
284	struct cam_path *path;
285	struct ata_params *ident_buf;
286
287	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
288
289	softc = (probe_softc *)periph->softc;
290	path = start_ccb->ccb_h.path;
291	ataio = &start_ccb->ataio;
292	csio = &start_ccb->csio;
293	ident_buf = &periph->path->device->ident_data;
294
295	if (softc->restart) {
296		softc->restart = 0;
297		if ((path->device->flags & CAM_DEV_UNCONFIGURED) ||
298		    path->device->protocol == PROTO_SATAPM)
299			softc->action = PROBE_RESET;
300		else
301			softc->action = PROBE_IDENTIFY;
302	}
303	switch (softc->action) {
304	case PROBE_RESET:
305		cam_fill_ataio(ataio,
306		      0,
307		      probedone,
308		      /*flags*/CAM_DIR_NONE,
309		      0,
310		      /*data_ptr*/NULL,
311		      /*dxfer_len*/0,
312		      15 * 1000);
313		ata_reset_cmd(ataio);
314		break;
315	case PROBE_IDENTIFY:
316		cam_fill_ataio(ataio,
317		      1,
318		      probedone,
319		      /*flags*/CAM_DIR_IN,
320		      0,
321		      /*data_ptr*/(u_int8_t *)&softc->ident_data,
322		      /*dxfer_len*/sizeof(softc->ident_data),
323		      30 * 1000);
324		if (periph->path->device->protocol == PROTO_ATA)
325			ata_28bit_cmd(ataio, ATA_ATA_IDENTIFY, 0, 0, 0);
326		else
327			ata_28bit_cmd(ataio, ATA_ATAPI_IDENTIFY, 0, 0, 0);
328		break;
329	case PROBE_SPINUP:
330		if (bootverbose)
331			xpt_print(path, "Spinning up device\n");
332		cam_fill_ataio(ataio,
333		      1,
334		      probedone,
335		      /*flags*/CAM_DIR_NONE | CAM_HIGH_POWER,
336		      0,
337		      /*data_ptr*/NULL,
338		      /*dxfer_len*/0,
339		      30 * 1000);
340		ata_28bit_cmd(ataio, ATA_SETFEATURES, ATA_SF_PUIS_SPINUP, 0, 0);
341		break;
342	case PROBE_SETMODE:
343	{
344		int mode, wantmode;
345
346		mode = 0;
347		/* Fetch user modes from SIM. */
348		bzero(&cts, sizeof(cts));
349		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
350		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
351		cts.type = CTS_TYPE_USER_SETTINGS;
352		xpt_action((union ccb *)&cts);
353		if (path->device->transport == XPORT_ATA) {
354			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_MODE)
355				mode = cts.xport_specific.ata.mode;
356		} else {
357			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_MODE)
358				mode = cts.xport_specific.sata.mode;
359		}
360negotiate:
361		/* Honor device capabilities. */
362		wantmode = mode = ata_max_mode(ident_buf, mode);
363		/* Report modes to SIM. */
364		bzero(&cts, sizeof(cts));
365		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
366		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
367		cts.type = CTS_TYPE_CURRENT_SETTINGS;
368		if (path->device->transport == XPORT_ATA) {
369			cts.xport_specific.ata.mode = mode;
370			cts.xport_specific.ata.valid = CTS_ATA_VALID_MODE;
371		} else {
372			cts.xport_specific.sata.mode = mode;
373			cts.xport_specific.sata.valid = CTS_SATA_VALID_MODE;
374		}
375		xpt_action((union ccb *)&cts);
376		/* Fetch current modes from SIM. */
377		bzero(&cts, sizeof(cts));
378		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
379		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
380		cts.type = CTS_TYPE_CURRENT_SETTINGS;
381		xpt_action((union ccb *)&cts);
382		if (path->device->transport == XPORT_ATA) {
383			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_MODE)
384				mode = cts.xport_specific.ata.mode;
385		} else {
386			if (cts.xport_specific.ata.valid & CTS_SATA_VALID_MODE)
387				mode = cts.xport_specific.sata.mode;
388		}
389		/* If SIM disagree - renegotiate. */
390		if (mode != wantmode)
391			goto negotiate;
392		cam_fill_ataio(ataio,
393		      1,
394		      probedone,
395		      /*flags*/CAM_DIR_NONE,
396		      0,
397		      /*data_ptr*/NULL,
398		      /*dxfer_len*/0,
399		      30 * 1000);
400		ata_28bit_cmd(ataio, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
401		break;
402	}
403	case PROBE_SETPM:
404		cam_fill_ataio(ataio,
405		    1,
406		    probedone,
407		    CAM_DIR_NONE,
408		    0,
409		    NULL,
410		    0,
411		    30*1000);
412		ata_28bit_cmd(ataio, ATA_SETFEATURES,
413		    (softc->caps & CTS_SATA_CAPS_H_PMREQ) ? 0x10 : 0x90,
414		    0, 0x03);
415		break;
416	case PROBE_SETAPST:
417		cam_fill_ataio(ataio,
418		    1,
419		    probedone,
420		    CAM_DIR_NONE,
421		    0,
422		    NULL,
423		    0,
424		    30*1000);
425		ata_28bit_cmd(ataio, ATA_SETFEATURES,
426		    (softc->caps & CTS_SATA_CAPS_H_APST) ? 0x10 : 0x90,
427		    0, 0x07);
428		break;
429	case PROBE_SETDMAAA:
430		cam_fill_ataio(ataio,
431		    1,
432		    probedone,
433		    CAM_DIR_NONE,
434		    0,
435		    NULL,
436		    0,
437		    30*1000);
438		ata_28bit_cmd(ataio, ATA_SETFEATURES,
439		    (softc->caps & CTS_SATA_CAPS_H_DMAAA) ? 0x10 : 0x90,
440		    0, 0x02);
441		break;
442	case PROBE_SET_MULTI:
443	{
444		u_int sectors, bytecount;
445
446		bytecount = 8192;	/* SATA maximum */
447		/* Fetch user bytecount from SIM. */
448		bzero(&cts, sizeof(cts));
449		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
450		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
451		cts.type = CTS_TYPE_USER_SETTINGS;
452		xpt_action((union ccb *)&cts);
453		if (path->device->transport == XPORT_ATA) {
454			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
455				bytecount = cts.xport_specific.ata.bytecount;
456		} else {
457			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
458				bytecount = cts.xport_specific.sata.bytecount;
459		}
460		/* Honor device capabilities. */
461		sectors = max(1, min(ident_buf->sectors_intr & 0xff,
462		    bytecount / ata_logical_sector_size(ident_buf)));
463		/* Report bytecount to SIM. */
464		bzero(&cts, sizeof(cts));
465		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
466		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
467		cts.type = CTS_TYPE_CURRENT_SETTINGS;
468		if (path->device->transport == XPORT_ATA) {
469			cts.xport_specific.ata.bytecount = sectors *
470			    ata_logical_sector_size(ident_buf);
471			cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT;
472		} else {
473			cts.xport_specific.sata.bytecount = sectors *
474			    ata_logical_sector_size(ident_buf);
475			cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT;
476		}
477		xpt_action((union ccb *)&cts);
478		/* Fetch current bytecount from SIM. */
479		bzero(&cts, sizeof(cts));
480		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
481		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
482		cts.type = CTS_TYPE_CURRENT_SETTINGS;
483		xpt_action((union ccb *)&cts);
484		if (path->device->transport == XPORT_ATA) {
485			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
486				bytecount = cts.xport_specific.ata.bytecount;
487		} else {
488			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
489				bytecount = cts.xport_specific.sata.bytecount;
490		}
491		sectors = bytecount / ata_logical_sector_size(ident_buf);
492
493		cam_fill_ataio(ataio,
494		    1,
495		    probedone,
496		    CAM_DIR_NONE,
497		    0,
498		    NULL,
499		    0,
500		    30*1000);
501		ata_28bit_cmd(ataio, ATA_SET_MULTI, 0, 0, sectors);
502		break;
503	}
504	case PROBE_INQUIRY:
505	{
506		u_int bytecount;
507
508		bytecount = 8192;	/* SATA maximum */
509		/* Fetch user bytecount from SIM. */
510		bzero(&cts, sizeof(cts));
511		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
512		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
513		cts.type = CTS_TYPE_USER_SETTINGS;
514		xpt_action((union ccb *)&cts);
515		if (path->device->transport == XPORT_ATA) {
516			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
517				bytecount = cts.xport_specific.ata.bytecount;
518		} else {
519			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
520				bytecount = cts.xport_specific.sata.bytecount;
521		}
522		/* Honor device capabilities. */
523		bytecount &= ~1;
524		bytecount = max(2, min(65534, bytecount));
525		if (ident_buf->satacapabilities != 0x0000 &&
526		    ident_buf->satacapabilities != 0xffff) {
527			bytecount = min(8192, bytecount);
528		}
529		/* Report bytecount to SIM. */
530		bzero(&cts, sizeof(cts));
531		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
532		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
533		cts.type = CTS_TYPE_CURRENT_SETTINGS;
534		if (path->device->transport == XPORT_ATA) {
535			cts.xport_specific.ata.bytecount = bytecount;
536			cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT;
537		} else {
538			cts.xport_specific.sata.bytecount = bytecount;
539			cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT;
540		}
541		xpt_action((union ccb *)&cts);
542		/* FALLTHROUGH */
543	}
544	case PROBE_FULL_INQUIRY:
545	{
546		u_int inquiry_len;
547		struct scsi_inquiry_data *inq_buf =
548		    &periph->path->device->inq_data;
549
550		if (softc->action == PROBE_INQUIRY)
551			inquiry_len = SHORT_INQUIRY_LENGTH;
552		else
553			inquiry_len = SID_ADDITIONAL_LENGTH(inq_buf);
554		/*
555		 * Some parallel SCSI devices fail to send an
556		 * ignore wide residue message when dealing with
557		 * odd length inquiry requests.  Round up to be
558		 * safe.
559		 */
560		inquiry_len = roundup2(inquiry_len, 2);
561		scsi_inquiry(csio,
562			     /*retries*/1,
563			     probedone,
564			     MSG_SIMPLE_Q_TAG,
565			     (u_int8_t *)inq_buf,
566			     inquiry_len,
567			     /*evpd*/FALSE,
568			     /*page_code*/0,
569			     SSD_MIN_SIZE,
570			     /*timeout*/60 * 1000);
571		break;
572	}
573	case PROBE_PM_PID:
574		cam_fill_ataio(ataio,
575		      1,
576		      probedone,
577		      /*flags*/CAM_DIR_NONE,
578		      0,
579		      /*data_ptr*/NULL,
580		      /*dxfer_len*/0,
581		      10 * 1000);
582		ata_pm_read_cmd(ataio, 0, 15);
583		break;
584	case PROBE_PM_PRV:
585		cam_fill_ataio(ataio,
586		      1,
587		      probedone,
588		      /*flags*/CAM_DIR_NONE,
589		      0,
590		      /*data_ptr*/NULL,
591		      /*dxfer_len*/0,
592		      10 * 1000);
593		ata_pm_read_cmd(ataio, 1, 15);
594		break;
595	case PROBE_INVALID:
596		CAM_DEBUG(path, CAM_DEBUG_INFO,
597		    ("probestart: invalid action state\n"));
598	default:
599		break;
600	}
601	xpt_action(start_ccb);
602}
603#if 0
604static void
605proberequestdefaultnegotiation(struct cam_periph *periph)
606{
607	struct ccb_trans_settings cts;
608
609	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
610	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
611	cts.type = CTS_TYPE_USER_SETTINGS;
612	xpt_action((union ccb *)&cts);
613	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
614		return;
615	}
616	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
617	cts.type = CTS_TYPE_CURRENT_SETTINGS;
618	xpt_action((union ccb *)&cts);
619}
620
621/*
622 * Backoff Negotiation Code- only pertinent for SPI devices.
623 */
624static int
625proberequestbackoff(struct cam_periph *periph, struct cam_ed *device)
626{
627	struct ccb_trans_settings cts;
628	struct ccb_trans_settings_spi *spi;
629
630	memset(&cts, 0, sizeof (cts));
631	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
632	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
633	cts.type = CTS_TYPE_CURRENT_SETTINGS;
634	xpt_action((union ccb *)&cts);
635	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
636		if (bootverbose) {
637			xpt_print(periph->path,
638			    "failed to get current device settings\n");
639		}
640		return (0);
641	}
642	if (cts.transport != XPORT_SPI) {
643		if (bootverbose) {
644			xpt_print(periph->path, "not SPI transport\n");
645		}
646		return (0);
647	}
648	spi = &cts.xport_specific.spi;
649
650	/*
651	 * We cannot renegotiate sync rate if we don't have one.
652	 */
653	if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
654		if (bootverbose) {
655			xpt_print(periph->path, "no sync rate known\n");
656		}
657		return (0);
658	}
659
660	/*
661	 * We'll assert that we don't have to touch PPR options- the
662	 * SIM will see what we do with period and offset and adjust
663	 * the PPR options as appropriate.
664	 */
665
666	/*
667	 * A sync rate with unknown or zero offset is nonsensical.
668	 * A sync period of zero means Async.
669	 */
670	if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0
671	 || spi->sync_offset == 0 || spi->sync_period == 0) {
672		if (bootverbose) {
673			xpt_print(periph->path, "no sync rate available\n");
674		}
675		return (0);
676	}
677
678	if (device->flags & CAM_DEV_DV_HIT_BOTTOM) {
679		CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
680		    ("hit async: giving up on DV\n"));
681		return (0);
682	}
683
684
685	/*
686	 * Jump sync_period up by one, but stop at 5MHz and fall back to Async.
687	 * We don't try to remember 'last' settings to see if the SIM actually
688	 * gets into the speed we want to set. We check on the SIM telling
689	 * us that a requested speed is bad, but otherwise don't try and
690	 * check the speed due to the asynchronous and handshake nature
691	 * of speed setting.
692	 */
693	spi->valid = CTS_SPI_VALID_SYNC_RATE | CTS_SPI_VALID_SYNC_OFFSET;
694	for (;;) {
695		spi->sync_period++;
696		if (spi->sync_period >= 0xf) {
697			spi->sync_period = 0;
698			spi->sync_offset = 0;
699			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
700			    ("setting to async for DV\n"));
701			/*
702			 * Once we hit async, we don't want to try
703			 * any more settings.
704			 */
705			device->flags |= CAM_DEV_DV_HIT_BOTTOM;
706		} else if (bootverbose) {
707			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
708			    ("DV: period 0x%x\n", spi->sync_period));
709			printf("setting period to 0x%x\n", spi->sync_period);
710		}
711		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
712		cts.type = CTS_TYPE_CURRENT_SETTINGS;
713		xpt_action((union ccb *)&cts);
714		if ((cts.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
715			break;
716		}
717		CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
718		    ("DV: failed to set period 0x%x\n", spi->sync_period));
719		if (spi->sync_period == 0) {
720			return (0);
721		}
722	}
723	return (1);
724}
725#endif
726static void
727probedone(struct cam_periph *periph, union ccb *done_ccb)
728{
729	struct ccb_trans_settings cts;
730	struct ata_params *ident_buf;
731	probe_softc *softc;
732	struct cam_path *path;
733	u_int32_t  priority;
734	u_int caps;
735	int found = 1;
736
737	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
738
739	softc = (probe_softc *)periph->softc;
740	path = done_ccb->ccb_h.path;
741	priority = done_ccb->ccb_h.pinfo.priority;
742	ident_buf = &path->device->ident_data;
743
744	if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
745device_fail:	if ((!softc->restart) &&
746		    cam_periph_error(done_ccb, 0, 0, NULL) == ERESTART) {
747			return;
748		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
749			/* Don't wedge the queue */
750			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
751					 /*run_queue*/TRUE);
752		}
753		/* Old PIO2 devices may not support mode setting. */
754		if (softc->action == PROBE_SETMODE &&
755		    ata_max_pmode(ident_buf) <= ATA_PIO2 &&
756		    (ident_buf->capabilities1 & ATA_SUPPORT_IORDY) == 0)
757			goto noerror;
758		/*
759		 * If we get to this point, we got an error status back
760		 * from the inquiry and the error status doesn't require
761		 * automatically retrying the command.  Therefore, the
762		 * inquiry failed.  If we had inquiry information before
763		 * for this device, but this latest inquiry command failed,
764		 * the device has probably gone away.  If this device isn't
765		 * already marked unconfigured, notify the peripheral
766		 * drivers that this device is no more.
767		 */
768		if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
769			xpt_async(AC_LOST_DEVICE, path, NULL);
770		found = 0;
771		goto done;
772	}
773noerror:
774	if (softc->restart)
775		goto done;
776	switch (softc->action) {
777	case PROBE_RESET:
778	{
779		int sign = (done_ccb->ataio.res.lba_high << 8) +
780		    done_ccb->ataio.res.lba_mid;
781		if (bootverbose)
782			xpt_print(path, "SIGNATURE: %04x\n", sign);
783		if (sign == 0x0000 &&
784		    done_ccb->ccb_h.target_id != 15) {
785			path->device->protocol = PROTO_ATA;
786			PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
787		} else if (sign == 0x9669 &&
788		    done_ccb->ccb_h.target_id == 15) {
789			/* Report SIM that PM is present. */
790			bzero(&cts, sizeof(cts));
791			xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
792			cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
793			cts.type = CTS_TYPE_CURRENT_SETTINGS;
794			cts.xport_specific.sata.pm_present = 1;
795			cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
796			xpt_action((union ccb *)&cts);
797			path->device->protocol = PROTO_SATAPM;
798			PROBE_SET_ACTION(softc, PROBE_PM_PID);
799		} else if (sign == 0xeb14 &&
800		    done_ccb->ccb_h.target_id != 15) {
801			path->device->protocol = PROTO_SCSI;
802			PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
803		} else {
804			if (done_ccb->ccb_h.target_id != 15) {
805				xpt_print(path,
806				    "Unexpected signature 0x%04x\n", sign);
807			}
808			goto device_fail;
809		}
810		xpt_release_ccb(done_ccb);
811		xpt_schedule(periph, priority);
812		return;
813	}
814	case PROBE_IDENTIFY:
815	{
816		struct ccb_pathinq cpi;
817		int16_t *ptr;
818		int changed = 1;
819
820		ident_buf = &softc->ident_data;
821		for (ptr = (int16_t *)ident_buf;
822		     ptr < (int16_t *)ident_buf + sizeof(struct ata_params)/2; ptr++) {
823			*ptr = le16toh(*ptr);
824		}
825		if (strncmp(ident_buf->model, "FX", 2) &&
826		    strncmp(ident_buf->model, "NEC", 3) &&
827		    strncmp(ident_buf->model, "Pioneer", 7) &&
828		    strncmp(ident_buf->model, "SHARP", 5)) {
829			ata_bswap(ident_buf->model, sizeof(ident_buf->model));
830			ata_bswap(ident_buf->revision, sizeof(ident_buf->revision));
831			ata_bswap(ident_buf->serial, sizeof(ident_buf->serial));
832		}
833		ata_btrim(ident_buf->model, sizeof(ident_buf->model));
834		ata_bpack(ident_buf->model, ident_buf->model, sizeof(ident_buf->model));
835		ata_btrim(ident_buf->revision, sizeof(ident_buf->revision));
836		ata_bpack(ident_buf->revision, ident_buf->revision, sizeof(ident_buf->revision));
837		ata_btrim(ident_buf->serial, sizeof(ident_buf->serial));
838		ata_bpack(ident_buf->serial, ident_buf->serial, sizeof(ident_buf->serial));
839		/* Device may need spin-up before IDENTIFY become valid. */
840		if ((ident_buf->specconf == 0x37c8 ||
841		     ident_buf->specconf == 0x738c) &&
842		    ((ident_buf->config & ATA_RESP_INCOMPLETE) ||
843		     softc->spinup == 0)) {
844			PROBE_SET_ACTION(softc, PROBE_SPINUP);
845			xpt_release_ccb(done_ccb);
846			xpt_schedule(periph, priority);
847			return;
848		}
849		ident_buf = &path->device->ident_data;
850		if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
851			/* Check that it is the same device. */
852			if (bcmp(softc->ident_data.model, ident_buf->model,
853			     sizeof(ident_buf->model)) ||
854			    bcmp(softc->ident_data.revision, ident_buf->revision,
855			     sizeof(ident_buf->revision)) ||
856			    bcmp(softc->ident_data.serial, ident_buf->serial,
857			     sizeof(ident_buf->serial))) {
858				/* Device changed. */
859				xpt_async(AC_LOST_DEVICE, path, NULL);
860			} else {
861				bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
862				changed = 0;
863			}
864		}
865		if (changed) {
866			bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
867			/* Clean up from previous instance of this device */
868			if (path->device->serial_num != NULL) {
869				free(path->device->serial_num, M_CAMXPT);
870				path->device->serial_num = NULL;
871				path->device->serial_num_len = 0;
872			}
873			path->device->serial_num =
874				(u_int8_t *)malloc((sizeof(ident_buf->serial) + 1),
875					   M_CAMXPT, M_NOWAIT);
876			if (path->device->serial_num != NULL) {
877				bcopy(ident_buf->serial,
878				      path->device->serial_num,
879				      sizeof(ident_buf->serial));
880				path->device->serial_num[sizeof(ident_buf->serial)]
881				    = '\0';
882				path->device->serial_num_len =
883				    strlen(path->device->serial_num);
884			}
885
886			path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
887		}
888		if (ident_buf->satacapabilities & ATA_SUPPORT_NCQ) {
889			path->device->mintags = path->device->maxtags =
890			    ATA_QUEUE_LEN(ident_buf->queue) + 1;
891		}
892		ata_find_quirk(path->device);
893		if (path->device->mintags != 0 &&
894		    path->bus->sim->max_tagged_dev_openings != 0) {
895			/* Check if the SIM does not want queued commands. */
896			bzero(&cpi, sizeof(cpi));
897			xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
898			cpi.ccb_h.func_code = XPT_PATH_INQ;
899			xpt_action((union ccb *)&cpi);
900			if (cpi.ccb_h.status == CAM_REQ_CMP &&
901			    (cpi.hba_inquiry & PI_TAG_ABLE)) {
902				/* Report SIM which tags are allowed. */
903				bzero(&cts, sizeof(cts));
904				xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
905				cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
906				cts.type = CTS_TYPE_CURRENT_SETTINGS;
907				cts.xport_specific.sata.tags = path->device->maxtags;
908				cts.xport_specific.sata.valid = CTS_SATA_VALID_TAGS;
909				xpt_action((union ccb *)&cts);
910				/* Reconfigure queues for tagged queueing. */
911				xpt_start_tags(path);
912			}
913		}
914		ata_device_transport(path);
915		PROBE_SET_ACTION(softc, PROBE_SETMODE);
916		xpt_release_ccb(done_ccb);
917		xpt_schedule(periph, priority);
918		return;
919	}
920	case PROBE_SPINUP:
921		if (bootverbose)
922			xpt_print(path, "Spin-up done\n");
923		softc->spinup = 1;
924		PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
925		xpt_release_ccb(done_ccb);
926		xpt_schedule(periph, priority);
927		return;
928	case PROBE_SETMODE:
929		if (path->device->transport != XPORT_SATA)
930			goto notsata;
931		/* Set supported bits. */
932		bzero(&cts, sizeof(cts));
933		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
934		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
935		cts.type = CTS_TYPE_CURRENT_SETTINGS;
936		xpt_action((union ccb *)&cts);
937		if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
938			caps = cts.xport_specific.sata.caps & CTS_SATA_CAPS_H;
939		else
940			caps = 0;
941		if (ident_buf->satacapabilities != 0xffff) {
942			if (ident_buf->satacapabilities & ATA_SUPPORT_IFPWRMNGTRCV)
943				caps |= CTS_SATA_CAPS_D_PMREQ;
944			if (ident_buf->satacapabilities & ATA_SUPPORT_HAPST)
945				caps |= CTS_SATA_CAPS_D_APST;
946		}
947		/* Mask unwanted bits. */
948		bzero(&cts, sizeof(cts));
949		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
950		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
951		cts.type = CTS_TYPE_USER_SETTINGS;
952		xpt_action((union ccb *)&cts);
953		if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
954			caps &= cts.xport_specific.sata.caps;
955		/* Store result to SIM. */
956		bzero(&cts, sizeof(cts));
957		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
958		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
959		cts.type = CTS_TYPE_CURRENT_SETTINGS;
960		cts.xport_specific.sata.caps = caps;
961		cts.xport_specific.sata.valid = CTS_SATA_VALID_CAPS;
962		xpt_action((union ccb *)&cts);
963		softc->caps = caps;
964		if (ident_buf->satasupport & ATA_SUPPORT_IFPWRMNGT) {
965			PROBE_SET_ACTION(softc, PROBE_SETPM);
966			xpt_release_ccb(done_ccb);
967			xpt_schedule(periph, priority);
968			return;
969		}
970		/* FALLTHROUGH */
971	case PROBE_SETPM:
972		if (ident_buf->satacapabilities != 0xffff &&
973		    ident_buf->satacapabilities & ATA_SUPPORT_DAPST) {
974			PROBE_SET_ACTION(softc, PROBE_SETAPST);
975			xpt_release_ccb(done_ccb);
976			xpt_schedule(periph, priority);
977			return;
978		}
979		/* FALLTHROUGH */
980	case PROBE_SETAPST:
981		if (ident_buf->satasupport & ATA_SUPPORT_AUTOACTIVATE) {
982			PROBE_SET_ACTION(softc, PROBE_SETDMAAA);
983			xpt_release_ccb(done_ccb);
984			xpt_schedule(periph, priority);
985			return;
986		}
987		/* FALLTHROUGH */
988	case PROBE_SETDMAAA:
989notsata:
990		if (path->device->protocol == PROTO_ATA) {
991			PROBE_SET_ACTION(softc, PROBE_SET_MULTI);
992		} else {
993			PROBE_SET_ACTION(softc, PROBE_INQUIRY);
994		}
995		xpt_release_ccb(done_ccb);
996		xpt_schedule(periph, priority);
997		return;
998	case PROBE_SET_MULTI:
999		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1000			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1001			xpt_acquire_device(path->device);
1002			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1003			xpt_action(done_ccb);
1004			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
1005			    done_ccb);
1006		}
1007		break;
1008	case PROBE_INQUIRY:
1009	case PROBE_FULL_INQUIRY:
1010	{
1011		struct scsi_inquiry_data *inq_buf;
1012		u_int8_t periph_qual, len;
1013
1014		path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
1015		inq_buf = &path->device->inq_data;
1016
1017		periph_qual = SID_QUAL(inq_buf);
1018
1019		if (periph_qual != SID_QUAL_LU_CONNECTED)
1020			break;
1021
1022		/*
1023		 * We conservatively request only
1024		 * SHORT_INQUIRY_LEN bytes of inquiry
1025		 * information during our first try
1026		 * at sending an INQUIRY. If the device
1027		 * has more information to give,
1028		 * perform a second request specifying
1029		 * the amount of information the device
1030		 * is willing to give.
1031		 */
1032		len = inq_buf->additional_length
1033		    + offsetof(struct scsi_inquiry_data, additional_length) + 1;
1034		if (softc->action == PROBE_INQUIRY
1035		    && len > SHORT_INQUIRY_LENGTH) {
1036			PROBE_SET_ACTION(softc, PROBE_FULL_INQUIRY);
1037			xpt_release_ccb(done_ccb);
1038			xpt_schedule(periph, priority);
1039			return;
1040		}
1041
1042		ata_device_transport(path);
1043		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1044			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1045			xpt_acquire_device(path->device);
1046			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1047			xpt_action(done_ccb);
1048			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path, done_ccb);
1049		}
1050		break;
1051	}
1052	case PROBE_PM_PID:
1053		if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) == 0)
1054			bzero(ident_buf, sizeof(*ident_buf));
1055		softc->pm_pid = (done_ccb->ataio.res.lba_high << 24) +
1056		    (done_ccb->ataio.res.lba_mid << 16) +
1057		    (done_ccb->ataio.res.lba_low << 8) +
1058		    done_ccb->ataio.res.sector_count;
1059		((uint32_t *)ident_buf)[0] = softc->pm_pid;
1060		snprintf(ident_buf->model, sizeof(ident_buf->model),
1061		    "Port Multiplier %08x", softc->pm_pid);
1062		PROBE_SET_ACTION(softc, PROBE_PM_PRV);
1063		xpt_release_ccb(done_ccb);
1064		xpt_schedule(periph, priority);
1065		return;
1066	case PROBE_PM_PRV:
1067		softc->pm_prv = (done_ccb->ataio.res.lba_high << 24) +
1068		    (done_ccb->ataio.res.lba_mid << 16) +
1069		    (done_ccb->ataio.res.lba_low << 8) +
1070		    done_ccb->ataio.res.sector_count;
1071		((uint32_t *)ident_buf)[1] = softc->pm_prv;
1072		snprintf(ident_buf->revision, sizeof(ident_buf->revision),
1073		    "%04x", softc->pm_prv);
1074		path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
1075		/* Set supported bits. */
1076		bzero(&cts, sizeof(cts));
1077		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1078		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1079		cts.type = CTS_TYPE_CURRENT_SETTINGS;
1080		xpt_action((union ccb *)&cts);
1081		if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1082			caps = cts.xport_specific.sata.caps & CTS_SATA_CAPS_H;
1083		else
1084			caps = 0;
1085		/* All PMPs must support PM requests. */
1086		caps |= CTS_SATA_CAPS_D_PMREQ;
1087		/* Mask unwanted bits. */
1088		bzero(&cts, sizeof(cts));
1089		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1090		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1091		cts.type = CTS_TYPE_USER_SETTINGS;
1092		xpt_action((union ccb *)&cts);
1093		if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1094			caps &= cts.xport_specific.sata.caps;
1095		/* Store result to SIM. */
1096		bzero(&cts, sizeof(cts));
1097		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1098		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1099		cts.type = CTS_TYPE_CURRENT_SETTINGS;
1100		cts.xport_specific.sata.caps = caps;
1101		cts.xport_specific.sata.valid = CTS_SATA_VALID_CAPS;
1102		xpt_action((union ccb *)&cts);
1103		softc->caps = caps;
1104		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1105			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1106			xpt_acquire_device(path->device);
1107			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1108			xpt_action(done_ccb);
1109			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
1110			    done_ccb);
1111		} else {
1112			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1113			xpt_action(done_ccb);
1114			xpt_async(AC_SCSI_AEN, done_ccb->ccb_h.path, done_ccb);
1115		}
1116		break;
1117	case PROBE_INVALID:
1118		CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_INFO,
1119		    ("probedone: invalid action state\n"));
1120	default:
1121		break;
1122	}
1123done:
1124	if (softc->restart) {
1125		softc->restart = 0;
1126		xpt_release_ccb(done_ccb);
1127		probeschedule(periph);
1128		return;
1129	}
1130	xpt_release_ccb(done_ccb);
1131	while ((done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs))) {
1132		TAILQ_REMOVE(&softc->request_ccbs,
1133		    &done_ccb->ccb_h, periph_links.tqe);
1134		done_ccb->ccb_h.status = found ? CAM_REQ_CMP : CAM_REQ_CMP_ERR;
1135		xpt_done(done_ccb);
1136	}
1137	cam_release_devq(periph->path,
1138	    RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_XPT + 1, FALSE);
1139	cam_periph_invalidate(periph);
1140	cam_periph_release_locked(periph);
1141}
1142
1143static void
1144probecleanup(struct cam_periph *periph)
1145{
1146	free(periph->softc, M_CAMXPT);
1147}
1148
1149static void
1150ata_find_quirk(struct cam_ed *device)
1151{
1152	struct ata_quirk_entry *quirk;
1153	caddr_t	match;
1154
1155	match = cam_quirkmatch((caddr_t)&device->ident_data,
1156			       (caddr_t)ata_quirk_table,
1157			       ata_quirk_table_size,
1158			       sizeof(*ata_quirk_table), ata_identify_match);
1159
1160	if (match == NULL)
1161		panic("xpt_find_quirk: device didn't match wildcard entry!!");
1162
1163	quirk = (struct ata_quirk_entry *)match;
1164	device->quirk = quirk;
1165	if (quirk->quirks & CAM_QUIRK_MAXTAGS)
1166		device->mintags = device->maxtags = quirk->maxtags;
1167}
1168
1169typedef struct {
1170	union	ccb *request_ccb;
1171	struct 	ccb_pathinq *cpi;
1172	int	counter;
1173} ata_scan_bus_info;
1174
1175/*
1176 * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
1177 * As the scan progresses, xpt_scan_bus is used as the
1178 * callback on completion function.
1179 */
1180static void
1181ata_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
1182{
1183	struct	cam_path *path;
1184	ata_scan_bus_info *scan_info;
1185	union	ccb *work_ccb, *reset_ccb;
1186	cam_status status;
1187
1188	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
1189		  ("xpt_scan_bus\n"));
1190	switch (request_ccb->ccb_h.func_code) {
1191	case XPT_SCAN_BUS:
1192		/* Find out the characteristics of the bus */
1193		work_ccb = xpt_alloc_ccb_nowait();
1194		if (work_ccb == NULL) {
1195			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1196			xpt_done(request_ccb);
1197			return;
1198		}
1199		xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
1200			      request_ccb->ccb_h.pinfo.priority);
1201		work_ccb->ccb_h.func_code = XPT_PATH_INQ;
1202		xpt_action(work_ccb);
1203		if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
1204			request_ccb->ccb_h.status = work_ccb->ccb_h.status;
1205			xpt_free_ccb(work_ccb);
1206			xpt_done(request_ccb);
1207			return;
1208		}
1209
1210		/* We may need to reset bus first, if we haven't done it yet. */
1211		if ((work_ccb->cpi.hba_inquiry &
1212		    (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) &&
1213		    !(work_ccb->cpi.hba_misc & PIM_NOBUSRESET) &&
1214		    !timevalisset(&request_ccb->ccb_h.path->bus->last_reset)) {
1215			reset_ccb = xpt_alloc_ccb_nowait();
1216			xpt_setup_ccb(&reset_ccb->ccb_h, request_ccb->ccb_h.path,
1217			      CAM_PRIORITY_NONE);
1218			reset_ccb->ccb_h.func_code = XPT_RESET_BUS;
1219			xpt_action(reset_ccb);
1220			if (reset_ccb->ccb_h.status != CAM_REQ_CMP) {
1221				request_ccb->ccb_h.status = reset_ccb->ccb_h.status;
1222				xpt_free_ccb(reset_ccb);
1223				xpt_free_ccb(work_ccb);
1224				xpt_done(request_ccb);
1225				return;
1226			}
1227			xpt_free_ccb(reset_ccb);
1228		}
1229
1230		/* Save some state for use while we probe for devices */
1231		scan_info = (ata_scan_bus_info *)
1232		    malloc(sizeof(ata_scan_bus_info), M_CAMXPT, M_NOWAIT);
1233		if (scan_info == NULL) {
1234			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1235			xpt_done(request_ccb);
1236			return;
1237		}
1238		scan_info->request_ccb = request_ccb;
1239		scan_info->cpi = &work_ccb->cpi;
1240		/* If PM supported, probe it first. */
1241		if (scan_info->cpi->hba_inquiry & PI_SATAPM)
1242			scan_info->counter = scan_info->cpi->max_target;
1243		else
1244			scan_info->counter = 0;
1245
1246		work_ccb = xpt_alloc_ccb_nowait();
1247		if (work_ccb == NULL) {
1248			free(scan_info, M_CAMXPT);
1249			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1250			xpt_done(request_ccb);
1251			break;
1252		}
1253		goto scan_next;
1254	case XPT_SCAN_LUN:
1255		work_ccb = request_ccb;
1256		/* Reuse the same CCB to query if a device was really found */
1257		scan_info = (ata_scan_bus_info *)work_ccb->ccb_h.ppriv_ptr0;
1258		/* If there is PMP... */
1259		if ((scan_info->cpi->hba_inquiry & PI_SATAPM) &&
1260		    (scan_info->counter == scan_info->cpi->max_target)) {
1261			if (work_ccb->ccb_h.status == CAM_REQ_CMP) {
1262				/* everything else will be probed by it */
1263				/* Free the current request path- we're done with it. */
1264				xpt_free_path(work_ccb->ccb_h.path);
1265				goto done;
1266			} else {
1267				struct ccb_trans_settings cts;
1268
1269				/* Report SIM that PM is absent. */
1270				bzero(&cts, sizeof(cts));
1271				xpt_setup_ccb(&cts.ccb_h,
1272				    work_ccb->ccb_h.path, CAM_PRIORITY_NONE);
1273				cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1274				cts.type = CTS_TYPE_CURRENT_SETTINGS;
1275				cts.xport_specific.sata.pm_present = 0;
1276				cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
1277				xpt_action((union ccb *)&cts);
1278			}
1279		}
1280		/* Free the current request path- we're done with it. */
1281		xpt_free_path(work_ccb->ccb_h.path);
1282		if (scan_info->counter ==
1283		    ((scan_info->cpi->hba_inquiry & PI_SATAPM) ?
1284		    0 : scan_info->cpi->max_target)) {
1285done:
1286			xpt_free_ccb(work_ccb);
1287			xpt_free_ccb((union ccb *)scan_info->cpi);
1288			request_ccb = scan_info->request_ccb;
1289			free(scan_info, M_CAMXPT);
1290			request_ccb->ccb_h.status = CAM_REQ_CMP;
1291			xpt_done(request_ccb);
1292			break;
1293		}
1294		/* Take next device. Wrap from max (PMP) to 0. */
1295		scan_info->counter = (scan_info->counter + 1 ) %
1296		    (scan_info->cpi->max_target + 1);
1297scan_next:
1298		status = xpt_create_path(&path, xpt_periph,
1299		    scan_info->request_ccb->ccb_h.path_id,
1300		    scan_info->counter, 0);
1301		if (status != CAM_REQ_CMP) {
1302			printf("xpt_scan_bus: xpt_create_path failed"
1303			    " with status %#x, bus scan halted\n",
1304			    status);
1305			xpt_free_ccb(work_ccb);
1306			xpt_free_ccb((union ccb *)scan_info->cpi);
1307			request_ccb = scan_info->request_ccb;
1308			free(scan_info, M_CAMXPT);
1309			request_ccb->ccb_h.status = status;
1310			xpt_done(request_ccb);
1311			break;
1312		}
1313		xpt_setup_ccb(&work_ccb->ccb_h, path,
1314		    scan_info->request_ccb->ccb_h.pinfo.priority);
1315		work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1316		work_ccb->ccb_h.cbfcnp = ata_scan_bus;
1317		work_ccb->ccb_h.ppriv_ptr0 = scan_info;
1318		work_ccb->crcn.flags = scan_info->request_ccb->crcn.flags;
1319		xpt_action(work_ccb);
1320		break;
1321	default:
1322		break;
1323	}
1324}
1325
1326static void
1327ata_scan_lun(struct cam_periph *periph, struct cam_path *path,
1328	     cam_flags flags, union ccb *request_ccb)
1329{
1330	struct ccb_pathinq cpi;
1331	cam_status status;
1332	struct cam_path *new_path;
1333	struct cam_periph *old_periph;
1334
1335	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_scan_lun\n"));
1336
1337	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
1338	cpi.ccb_h.func_code = XPT_PATH_INQ;
1339	xpt_action((union ccb *)&cpi);
1340
1341	if (cpi.ccb_h.status != CAM_REQ_CMP) {
1342		if (request_ccb != NULL) {
1343			request_ccb->ccb_h.status = cpi.ccb_h.status;
1344			xpt_done(request_ccb);
1345		}
1346		return;
1347	}
1348
1349	if (request_ccb == NULL) {
1350		request_ccb = malloc(sizeof(union ccb), M_CAMXPT, M_NOWAIT);
1351		if (request_ccb == NULL) {
1352			xpt_print(path, "xpt_scan_lun: can't allocate CCB, "
1353			    "can't continue\n");
1354			return;
1355		}
1356		new_path = malloc(sizeof(*new_path), M_CAMXPT, M_NOWAIT);
1357		if (new_path == NULL) {
1358			xpt_print(path, "xpt_scan_lun: can't allocate path, "
1359			    "can't continue\n");
1360			free(request_ccb, M_CAMXPT);
1361			return;
1362		}
1363		status = xpt_compile_path(new_path, xpt_periph,
1364					  path->bus->path_id,
1365					  path->target->target_id,
1366					  path->device->lun_id);
1367
1368		if (status != CAM_REQ_CMP) {
1369			xpt_print(path, "xpt_scan_lun: can't compile path, "
1370			    "can't continue\n");
1371			free(request_ccb, M_CAMXPT);
1372			free(new_path, M_CAMXPT);
1373			return;
1374		}
1375		xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_XPT);
1376		request_ccb->ccb_h.cbfcnp = xptscandone;
1377		request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1378		request_ccb->crcn.flags = flags;
1379	}
1380
1381	if ((old_periph = cam_periph_find(path, "aprobe")) != NULL) {
1382		probe_softc *softc;
1383
1384		softc = (probe_softc *)old_periph->softc;
1385		TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
1386				  periph_links.tqe);
1387		softc->restart = 1;
1388	} else {
1389		status = cam_periph_alloc(proberegister, NULL, probecleanup,
1390					  probestart, "aprobe",
1391					  CAM_PERIPH_BIO,
1392					  request_ccb->ccb_h.path, NULL, 0,
1393					  request_ccb);
1394
1395		if (status != CAM_REQ_CMP) {
1396			xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
1397			    "returned an error, can't continue probe\n");
1398			request_ccb->ccb_h.status = status;
1399			xpt_done(request_ccb);
1400		}
1401	}
1402}
1403
1404static void
1405xptscandone(struct cam_periph *periph, union ccb *done_ccb)
1406{
1407	xpt_release_path(done_ccb->ccb_h.path);
1408	free(done_ccb->ccb_h.path, M_CAMXPT);
1409	free(done_ccb, M_CAMXPT);
1410}
1411
1412static struct cam_ed *
1413ata_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
1414{
1415	struct cam_path path;
1416	struct ata_quirk_entry *quirk;
1417	struct cam_ed *device;
1418	struct cam_ed *cur_device;
1419
1420	device = xpt_alloc_device(bus, target, lun_id);
1421	if (device == NULL)
1422		return (NULL);
1423
1424	/*
1425	 * Take the default quirk entry until we have inquiry
1426	 * data and can determine a better quirk to use.
1427	 */
1428	quirk = &ata_quirk_table[ata_quirk_table_size - 1];
1429	device->quirk = (void *)quirk;
1430	device->mintags = 0;
1431	device->maxtags = 0;
1432	bzero(&device->inq_data, sizeof(device->inq_data));
1433	device->inq_flags = 0;
1434	device->queue_flags = 0;
1435	device->serial_num = NULL;
1436	device->serial_num_len = 0;
1437
1438	/*
1439	 * XXX should be limited by number of CCBs this bus can
1440	 * do.
1441	 */
1442	bus->sim->max_ccbs += device->ccbq.devq_openings;
1443	/* Insertion sort into our target's device list */
1444	cur_device = TAILQ_FIRST(&target->ed_entries);
1445	while (cur_device != NULL && cur_device->lun_id < lun_id)
1446		cur_device = TAILQ_NEXT(cur_device, links);
1447	if (cur_device != NULL) {
1448		TAILQ_INSERT_BEFORE(cur_device, device, links);
1449	} else {
1450		TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
1451	}
1452	target->generation++;
1453	if (lun_id != CAM_LUN_WILDCARD) {
1454		xpt_compile_path(&path,
1455				 NULL,
1456				 bus->path_id,
1457				 target->target_id,
1458				 lun_id);
1459		ata_device_transport(&path);
1460		xpt_release_path(&path);
1461	}
1462
1463	return (device);
1464}
1465
1466static void
1467ata_device_transport(struct cam_path *path)
1468{
1469	struct ccb_pathinq cpi;
1470	struct ccb_trans_settings cts;
1471	struct scsi_inquiry_data *inq_buf = NULL;
1472	struct ata_params *ident_buf = NULL;
1473
1474	/* Get transport information from the SIM */
1475	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
1476	cpi.ccb_h.func_code = XPT_PATH_INQ;
1477	xpt_action((union ccb *)&cpi);
1478
1479	path->device->transport = cpi.transport;
1480	if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
1481		inq_buf = &path->device->inq_data;
1482	if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) != 0)
1483		ident_buf = &path->device->ident_data;
1484	if (path->device->protocol == PROTO_ATA) {
1485		path->device->protocol_version = ident_buf ?
1486		    ata_version(ident_buf->version_major) : cpi.protocol_version;
1487	} else if (path->device->protocol == PROTO_SCSI) {
1488		path->device->protocol_version = inq_buf ?
1489		    SID_ANSI_REV(inq_buf) : cpi.protocol_version;
1490	}
1491	path->device->transport_version = ident_buf ?
1492	    ata_version(ident_buf->version_major) : cpi.transport_version;
1493
1494	/* Tell the controller what we think */
1495	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1496	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1497	cts.type = CTS_TYPE_CURRENT_SETTINGS;
1498	cts.transport = path->device->transport;
1499	cts.transport_version = path->device->transport_version;
1500	cts.protocol = path->device->protocol;
1501	cts.protocol_version = path->device->protocol_version;
1502	cts.proto_specific.valid = 0;
1503	if (ident_buf) {
1504		if (path->device->transport == XPORT_ATA) {
1505			cts.xport_specific.ata.atapi =
1506			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 :
1507			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0;
1508			cts.xport_specific.ata.valid = CTS_ATA_VALID_ATAPI;
1509		} else {
1510			cts.xport_specific.sata.atapi =
1511			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 :
1512			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0;
1513			cts.xport_specific.sata.valid = CTS_SATA_VALID_ATAPI;
1514		}
1515	} else
1516		cts.xport_specific.valid = 0;
1517	xpt_action((union ccb *)&cts);
1518}
1519
1520static void
1521ata_action(union ccb *start_ccb)
1522{
1523
1524	switch (start_ccb->ccb_h.func_code) {
1525	case XPT_SET_TRAN_SETTINGS:
1526	{
1527		ata_set_transfer_settings(&start_ccb->cts,
1528					   start_ccb->ccb_h.path->device,
1529					   /*async_update*/FALSE);
1530		break;
1531	}
1532	case XPT_SCAN_BUS:
1533		ata_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
1534		break;
1535	case XPT_SCAN_LUN:
1536		ata_scan_lun(start_ccb->ccb_h.path->periph,
1537			      start_ccb->ccb_h.path, start_ccb->crcn.flags,
1538			      start_ccb);
1539		break;
1540	case XPT_GET_TRAN_SETTINGS:
1541	{
1542		struct cam_sim *sim;
1543
1544		sim = start_ccb->ccb_h.path->bus->sim;
1545		(*(sim->sim_action))(sim, start_ccb);
1546		break;
1547	}
1548	case XPT_SCSI_IO:
1549	{
1550		struct cam_ed *device;
1551		u_int	maxlen = 0;
1552
1553		device = start_ccb->ccb_h.path->device;
1554		if (device->protocol == PROTO_SCSI &&
1555		    (device->flags & CAM_DEV_IDENTIFY_DATA_VALID)) {
1556			uint16_t p =
1557			    device->ident_data.config & ATA_PROTO_MASK;
1558
1559			maxlen = (p == ATA_PROTO_ATAPI_16) ? 16 :
1560			    (p == ATA_PROTO_ATAPI_12) ? 12 : 0;
1561		}
1562		if (start_ccb->csio.cdb_len > maxlen) {
1563			start_ccb->ccb_h.status = CAM_REQ_INVALID;
1564			xpt_done(start_ccb);
1565			break;
1566		}
1567		/* FALLTHROUGH */
1568	}
1569	default:
1570		xpt_action_default(start_ccb);
1571		break;
1572	}
1573}
1574
1575static void
1576ata_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
1577			   int async_update)
1578{
1579	struct	ccb_pathinq cpi;
1580	struct	ccb_trans_settings cur_cts;
1581	struct	ccb_trans_settings_scsi *scsi;
1582	struct	ccb_trans_settings_scsi *cur_scsi;
1583	struct	cam_sim *sim;
1584	struct	scsi_inquiry_data *inq_data;
1585
1586	if (device == NULL) {
1587		cts->ccb_h.status = CAM_PATH_INVALID;
1588		xpt_done((union ccb *)cts);
1589		return;
1590	}
1591
1592	if (cts->protocol == PROTO_UNKNOWN
1593	 || cts->protocol == PROTO_UNSPECIFIED) {
1594		cts->protocol = device->protocol;
1595		cts->protocol_version = device->protocol_version;
1596	}
1597
1598	if (cts->protocol_version == PROTO_VERSION_UNKNOWN
1599	 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
1600		cts->protocol_version = device->protocol_version;
1601
1602	if (cts->protocol != device->protocol) {
1603		xpt_print(cts->ccb_h.path, "Uninitialized Protocol %x:%x?\n",
1604		       cts->protocol, device->protocol);
1605		cts->protocol = device->protocol;
1606	}
1607
1608	if (cts->protocol_version > device->protocol_version) {
1609		if (bootverbose) {
1610			xpt_print(cts->ccb_h.path, "Down reving Protocol "
1611			    "Version from %d to %d?\n", cts->protocol_version,
1612			    device->protocol_version);
1613		}
1614		cts->protocol_version = device->protocol_version;
1615	}
1616
1617	if (cts->transport == XPORT_UNKNOWN
1618	 || cts->transport == XPORT_UNSPECIFIED) {
1619		cts->transport = device->transport;
1620		cts->transport_version = device->transport_version;
1621	}
1622
1623	if (cts->transport_version == XPORT_VERSION_UNKNOWN
1624	 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
1625		cts->transport_version = device->transport_version;
1626
1627	if (cts->transport != device->transport) {
1628		xpt_print(cts->ccb_h.path, "Uninitialized Transport %x:%x?\n",
1629		    cts->transport, device->transport);
1630		cts->transport = device->transport;
1631	}
1632
1633	if (cts->transport_version > device->transport_version) {
1634		if (bootverbose) {
1635			xpt_print(cts->ccb_h.path, "Down reving Transport "
1636			    "Version from %d to %d?\n", cts->transport_version,
1637			    device->transport_version);
1638		}
1639		cts->transport_version = device->transport_version;
1640	}
1641
1642	sim = cts->ccb_h.path->bus->sim;
1643
1644	/*
1645	 * Nothing more of interest to do unless
1646	 * this is a device connected via the
1647	 * SCSI protocol.
1648	 */
1649	if (cts->protocol != PROTO_SCSI) {
1650		if (async_update == FALSE)
1651			(*(sim->sim_action))(sim, (union ccb *)cts);
1652		return;
1653	}
1654
1655	inq_data = &device->inq_data;
1656	scsi = &cts->proto_specific.scsi;
1657	xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NONE);
1658	cpi.ccb_h.func_code = XPT_PATH_INQ;
1659	xpt_action((union ccb *)&cpi);
1660
1661	/* SCSI specific sanity checking */
1662	if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
1663	 || (INQ_DATA_TQ_ENABLED(inq_data)) == 0
1664	 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
1665	 || (device->mintags == 0)) {
1666		/*
1667		 * Can't tag on hardware that doesn't support tags,
1668		 * doesn't have it enabled, or has broken tag support.
1669		 */
1670		scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1671	}
1672
1673	if (async_update == FALSE) {
1674		/*
1675		 * Perform sanity checking against what the
1676		 * controller and device can do.
1677		 */
1678		xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NONE);
1679		cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1680		cur_cts.type = cts->type;
1681		xpt_action((union ccb *)&cur_cts);
1682		if ((cur_cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1683			return;
1684		}
1685		cur_scsi = &cur_cts.proto_specific.scsi;
1686		if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
1687			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1688			scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
1689		}
1690		if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
1691			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1692	}
1693
1694	if (cts->type == CTS_TYPE_CURRENT_SETTINGS
1695	 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
1696		int device_tagenb;
1697
1698		/*
1699		 * If we are transitioning from tags to no-tags or
1700		 * vice-versa, we need to carefully freeze and restart
1701		 * the queue so that we don't overlap tagged and non-tagged
1702		 * commands.  We also temporarily stop tags if there is
1703		 * a change in transfer negotiation settings to allow
1704		 * "tag-less" negotiation.
1705		 */
1706		if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
1707		 || (device->inq_flags & SID_CmdQue) != 0)
1708			device_tagenb = TRUE;
1709		else
1710			device_tagenb = FALSE;
1711
1712		if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
1713		  && device_tagenb == FALSE)
1714		 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
1715		  && device_tagenb == TRUE)) {
1716
1717			if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
1718				/*
1719				 * Delay change to use tags until after a
1720				 * few commands have gone to this device so
1721				 * the controller has time to perform transfer
1722				 * negotiations without tagged messages getting
1723				 * in the way.
1724				 */
1725				device->tag_delay_count = CAM_TAG_DELAY_COUNT;
1726				device->flags |= CAM_DEV_TAG_AFTER_COUNT;
1727			} else {
1728				xpt_stop_tags(cts->ccb_h.path);
1729			}
1730		}
1731	}
1732	if (async_update == FALSE)
1733		(*(sim->sim_action))(sim, (union ccb *)cts);
1734}
1735
1736/*
1737 * Handle any per-device event notifications that require action by the XPT.
1738 */
1739static void
1740ata_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
1741	      struct cam_ed *device, void *async_arg)
1742{
1743	cam_status status;
1744	struct cam_path newpath;
1745
1746	/*
1747	 * We only need to handle events for real devices.
1748	 */
1749	if (target->target_id == CAM_TARGET_WILDCARD
1750	 || device->lun_id == CAM_LUN_WILDCARD)
1751		return;
1752
1753	/*
1754	 * We need our own path with wildcards expanded to
1755	 * handle certain types of events.
1756	 */
1757	if ((async_code == AC_SENT_BDR)
1758	 || (async_code == AC_BUS_RESET)
1759	 || (async_code == AC_INQ_CHANGED))
1760		status = xpt_compile_path(&newpath, NULL,
1761					  bus->path_id,
1762					  target->target_id,
1763					  device->lun_id);
1764	else
1765		status = CAM_REQ_CMP_ERR;
1766
1767	if (status == CAM_REQ_CMP) {
1768		if (async_code == AC_INQ_CHANGED) {
1769			/*
1770			 * We've sent a start unit command, or
1771			 * something similar to a device that
1772			 * may have caused its inquiry data to
1773			 * change. So we re-scan the device to
1774			 * refresh the inquiry data for it.
1775			 */
1776			ata_scan_lun(newpath.periph, &newpath,
1777				     CAM_EXPECT_INQ_CHANGE, NULL);
1778		} else {
1779			/* We need to reinitialize device after reset. */
1780			ata_scan_lun(newpath.periph, &newpath,
1781				     0, NULL);
1782		}
1783		xpt_release_path(&newpath);
1784	} else if (async_code == AC_LOST_DEVICE &&
1785	    (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
1786		device->flags |= CAM_DEV_UNCONFIGURED;
1787		xpt_release_device(device);
1788	} else if (async_code == AC_TRANSFER_NEG) {
1789		struct ccb_trans_settings *settings;
1790
1791		settings = (struct ccb_trans_settings *)async_arg;
1792		ata_set_transfer_settings(settings, device,
1793					  /*async_update*/TRUE);
1794	}
1795}
1796
1797static void
1798ata_announce_periph(struct cam_periph *periph)
1799{
1800	struct	ccb_pathinq cpi;
1801	struct	ccb_trans_settings cts;
1802	struct	cam_path *path = periph->path;
1803	u_int	speed;
1804	u_int	mb;
1805
1806	mtx_assert(periph->sim->mtx, MA_OWNED);
1807
1808	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
1809	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1810	cts.type = CTS_TYPE_CURRENT_SETTINGS;
1811	xpt_action((union ccb*)&cts);
1812	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
1813		return;
1814	/* Ask the SIM for its base transfer speed */
1815	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
1816	cpi.ccb_h.func_code = XPT_PATH_INQ;
1817	xpt_action((union ccb *)&cpi);
1818	/* Report connection speed */
1819	speed = cpi.base_transfer_speed;
1820	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_ATA) {
1821		struct	ccb_trans_settings_ata *ata =
1822		    &cts.xport_specific.ata;
1823
1824		if (ata->valid & CTS_ATA_VALID_MODE)
1825			speed = ata_mode2speed(ata->mode);
1826	}
1827	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SATA) {
1828		struct	ccb_trans_settings_sata *sata =
1829		    &cts.xport_specific.sata;
1830
1831		if (sata->valid & CTS_SATA_VALID_REVISION)
1832			speed = ata_revision2speed(sata->revision);
1833	}
1834	mb = speed / 1000;
1835	if (mb > 0)
1836		printf("%s%d: %d.%03dMB/s transfers",
1837		       periph->periph_name, periph->unit_number,
1838		       mb, speed % 1000);
1839	else
1840		printf("%s%d: %dKB/s transfers", periph->periph_name,
1841		       periph->unit_number, speed);
1842	/* Report additional information about connection */
1843	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_ATA) {
1844		struct ccb_trans_settings_ata *ata =
1845		    &cts.xport_specific.ata;
1846
1847		printf(" (");
1848		if (ata->valid & CTS_ATA_VALID_MODE)
1849			printf("%s, ", ata_mode2string(ata->mode));
1850		if ((ata->valid & CTS_ATA_VALID_ATAPI) && ata->atapi != 0)
1851			printf("ATAPI %dbytes, ", ata->atapi);
1852		if (ata->valid & CTS_ATA_VALID_BYTECOUNT)
1853			printf("PIO %dbytes", ata->bytecount);
1854		printf(")");
1855	}
1856	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SATA) {
1857		struct ccb_trans_settings_sata *sata =
1858		    &cts.xport_specific.sata;
1859
1860		printf(" (");
1861		if (sata->valid & CTS_SATA_VALID_REVISION)
1862			printf("SATA %d.x, ", sata->revision);
1863		else
1864			printf("SATA, ");
1865		if (sata->valid & CTS_SATA_VALID_MODE)
1866			printf("%s, ", ata_mode2string(sata->mode));
1867		if ((sata->valid & CTS_ATA_VALID_ATAPI) && sata->atapi != 0)
1868			printf("ATAPI %dbytes, ", sata->atapi);
1869		if (sata->valid & CTS_SATA_VALID_BYTECOUNT)
1870			printf("PIO %dbytes", sata->bytecount);
1871		printf(")");
1872	}
1873	printf("\n");
1874}
1875
1876