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