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