xref: /freebsd/sys/cam/ata/ata_xpt.c (revision 21d30ec18d673020b701750a4d498a4e75cde516)
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$");
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 
64 struct 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 
71 static periph_init_t probe_periph_init;
72 
73 static 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 
80 PERIPHDRIVER_DECLARE(aprobe, probe_driver);
81 
82 typedef 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 
98 static 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)	\
115 do {									\
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 
124 typedef enum {
125 	PROBE_NO_ANNOUNCE	= 0x04
126 } probe_flags;
127 
128 typedef 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 
141 static 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 
153 static const int ata_quirk_table_size =
154 	sizeof(ata_quirk_table) / sizeof(*ata_quirk_table);
155 
156 static cam_status	proberegister(struct cam_periph *periph,
157 				      void *arg);
158 static void	 probeschedule(struct cam_periph *probe_periph);
159 static 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);
163 static void	 probedone(struct cam_periph *periph, union ccb *done_ccb);
164 static void	 probecleanup(struct cam_periph *periph);
165 static void	 ata_find_quirk(struct cam_ed *device);
166 static void	 ata_scan_bus(struct cam_periph *periph, union ccb *ccb);
167 static void	 ata_scan_lun(struct cam_periph *periph,
168 			       struct cam_path *path, cam_flags flags,
169 			       union ccb *ccb);
170 static void	 xptscandone(struct cam_periph *periph, union ccb *done_ccb);
171 static struct cam_ed *
172 		 ata_alloc_device(struct cam_eb *bus, struct cam_et *target,
173 				   lun_id_t lun_id);
174 static void	 ata_device_transport(struct cam_path *path);
175 static void	 ata_set_transfer_settings(struct ccb_trans_settings *cts,
176 					    struct cam_ed *device,
177 					    int async_update);
178 static 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);
183 static void	 ata_action(union ccb *start_ccb);
184 static void	 ata_announce_periph(struct cam_periph *periph);
185 
186 static 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 
193 struct xpt_xport *
194 ata_get_xport(void)
195 {
196 	return (&ata_xport);
197 }
198 
199 static void
200 probe_periph_init()
201 {
202 }
203 
204 static cam_status
205 proberegister(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 
250 static void
251 probeschedule(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 
273 static void
274 probestart(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 		}
356 negotiate:
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
600 static void
601 proberequestdefaultnegotiation(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  */
620 static int
621 proberequestbackoff(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
722 static void
723 probedone(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) {
741 device_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 	}
769 noerror:
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:
985 notsata:
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 	}
1119 done:
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 
1139 static void
1140 probecleanup(struct cam_periph *periph)
1141 {
1142 	free(periph->softc, M_CAMXPT);
1143 }
1144 
1145 static void
1146 ata_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 
1165 typedef 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  */
1176 static void
1177 ata_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 	case XPT_SCAN_TGT:
1189 		/* Find out the characteristics of the bus */
1190 		work_ccb = xpt_alloc_ccb_nowait();
1191 		if (work_ccb == NULL) {
1192 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1193 			xpt_done(request_ccb);
1194 			return;
1195 		}
1196 		xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
1197 			      request_ccb->ccb_h.pinfo.priority);
1198 		work_ccb->ccb_h.func_code = XPT_PATH_INQ;
1199 		xpt_action(work_ccb);
1200 		if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
1201 			request_ccb->ccb_h.status = work_ccb->ccb_h.status;
1202 			xpt_free_ccb(work_ccb);
1203 			xpt_done(request_ccb);
1204 			return;
1205 		}
1206 
1207 		/* We may need to reset bus first, if we haven't done it yet. */
1208 		if ((work_ccb->cpi.hba_inquiry &
1209 		    (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) &&
1210 		    !(work_ccb->cpi.hba_misc & PIM_NOBUSRESET) &&
1211 		    !timevalisset(&request_ccb->ccb_h.path->bus->last_reset)) {
1212 			reset_ccb = xpt_alloc_ccb_nowait();
1213 			xpt_setup_ccb(&reset_ccb->ccb_h, request_ccb->ccb_h.path,
1214 			      CAM_PRIORITY_NONE);
1215 			reset_ccb->ccb_h.func_code = XPT_RESET_BUS;
1216 			xpt_action(reset_ccb);
1217 			if (reset_ccb->ccb_h.status != CAM_REQ_CMP) {
1218 				request_ccb->ccb_h.status = reset_ccb->ccb_h.status;
1219 				xpt_free_ccb(reset_ccb);
1220 				xpt_free_ccb(work_ccb);
1221 				xpt_done(request_ccb);
1222 				return;
1223 			}
1224 			xpt_free_ccb(reset_ccb);
1225 		}
1226 
1227 		/* Save some state for use while we probe for devices */
1228 		scan_info = (ata_scan_bus_info *)
1229 		    malloc(sizeof(ata_scan_bus_info), M_CAMXPT, M_NOWAIT);
1230 		if (scan_info == NULL) {
1231 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1232 			xpt_done(request_ccb);
1233 			return;
1234 		}
1235 		scan_info->request_ccb = request_ccb;
1236 		scan_info->cpi = &work_ccb->cpi;
1237 		/* If PM supported, probe it first. */
1238 		if (scan_info->cpi->hba_inquiry & PI_SATAPM)
1239 			scan_info->counter = scan_info->cpi->max_target;
1240 		else
1241 			scan_info->counter = 0;
1242 
1243 		work_ccb = xpt_alloc_ccb_nowait();
1244 		if (work_ccb == NULL) {
1245 			free(scan_info, M_CAMXPT);
1246 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1247 			xpt_done(request_ccb);
1248 			break;
1249 		}
1250 		goto scan_next;
1251 	case XPT_SCAN_LUN:
1252 		work_ccb = request_ccb;
1253 		/* Reuse the same CCB to query if a device was really found */
1254 		scan_info = (ata_scan_bus_info *)work_ccb->ccb_h.ppriv_ptr0;
1255 		/* If there is PMP... */
1256 		if ((scan_info->cpi->hba_inquiry & PI_SATAPM) &&
1257 		    (scan_info->counter == scan_info->cpi->max_target)) {
1258 			if (work_ccb->ccb_h.status == CAM_REQ_CMP) {
1259 				/* everything else will be probed by it */
1260 				/* Free the current request path- we're done with it. */
1261 				xpt_free_path(work_ccb->ccb_h.path);
1262 				goto done;
1263 			} else {
1264 				struct ccb_trans_settings cts;
1265 
1266 				/* Report SIM that PM is absent. */
1267 				bzero(&cts, sizeof(cts));
1268 				xpt_setup_ccb(&cts.ccb_h,
1269 				    work_ccb->ccb_h.path, CAM_PRIORITY_NONE);
1270 				cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1271 				cts.type = CTS_TYPE_CURRENT_SETTINGS;
1272 				cts.xport_specific.sata.pm_present = 0;
1273 				cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
1274 				xpt_action((union ccb *)&cts);
1275 			}
1276 		}
1277 		/* Free the current request path- we're done with it. */
1278 		xpt_free_path(work_ccb->ccb_h.path);
1279 		if (scan_info->counter ==
1280 		    ((scan_info->cpi->hba_inquiry & PI_SATAPM) ?
1281 		    0 : scan_info->cpi->max_target)) {
1282 done:
1283 			xpt_free_ccb(work_ccb);
1284 			xpt_free_ccb((union ccb *)scan_info->cpi);
1285 			request_ccb = scan_info->request_ccb;
1286 			free(scan_info, M_CAMXPT);
1287 			request_ccb->ccb_h.status = CAM_REQ_CMP;
1288 			xpt_done(request_ccb);
1289 			break;
1290 		}
1291 		/* Take next device. Wrap from max (PMP) to 0. */
1292 		scan_info->counter = (scan_info->counter + 1 ) %
1293 		    (scan_info->cpi->max_target + 1);
1294 scan_next:
1295 		status = xpt_create_path(&path, xpt_periph,
1296 		    scan_info->request_ccb->ccb_h.path_id,
1297 		    scan_info->counter, 0);
1298 		if (status != CAM_REQ_CMP) {
1299 			printf("xpt_scan_bus: xpt_create_path failed"
1300 			    " with status %#x, bus scan halted\n",
1301 			    status);
1302 			xpt_free_ccb(work_ccb);
1303 			xpt_free_ccb((union ccb *)scan_info->cpi);
1304 			request_ccb = scan_info->request_ccb;
1305 			free(scan_info, M_CAMXPT);
1306 			request_ccb->ccb_h.status = status;
1307 			xpt_done(request_ccb);
1308 			break;
1309 		}
1310 		xpt_setup_ccb(&work_ccb->ccb_h, path,
1311 		    scan_info->request_ccb->ccb_h.pinfo.priority);
1312 		work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1313 		work_ccb->ccb_h.cbfcnp = ata_scan_bus;
1314 		work_ccb->ccb_h.ppriv_ptr0 = scan_info;
1315 		work_ccb->crcn.flags = scan_info->request_ccb->crcn.flags;
1316 		xpt_action(work_ccb);
1317 		break;
1318 	default:
1319 		break;
1320 	}
1321 }
1322 
1323 static void
1324 ata_scan_lun(struct cam_periph *periph, struct cam_path *path,
1325 	     cam_flags flags, union ccb *request_ccb)
1326 {
1327 	struct ccb_pathinq cpi;
1328 	cam_status status;
1329 	struct cam_path *new_path;
1330 	struct cam_periph *old_periph;
1331 
1332 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_scan_lun\n"));
1333 
1334 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
1335 	cpi.ccb_h.func_code = XPT_PATH_INQ;
1336 	xpt_action((union ccb *)&cpi);
1337 
1338 	if (cpi.ccb_h.status != CAM_REQ_CMP) {
1339 		if (request_ccb != NULL) {
1340 			request_ccb->ccb_h.status = cpi.ccb_h.status;
1341 			xpt_done(request_ccb);
1342 		}
1343 		return;
1344 	}
1345 
1346 	if (request_ccb == NULL) {
1347 		request_ccb = malloc(sizeof(union ccb), M_CAMXPT, M_NOWAIT);
1348 		if (request_ccb == NULL) {
1349 			xpt_print(path, "xpt_scan_lun: can't allocate CCB, "
1350 			    "can't continue\n");
1351 			return;
1352 		}
1353 		new_path = malloc(sizeof(*new_path), M_CAMXPT, M_NOWAIT);
1354 		if (new_path == NULL) {
1355 			xpt_print(path, "xpt_scan_lun: can't allocate path, "
1356 			    "can't continue\n");
1357 			free(request_ccb, M_CAMXPT);
1358 			return;
1359 		}
1360 		status = xpt_compile_path(new_path, xpt_periph,
1361 					  path->bus->path_id,
1362 					  path->target->target_id,
1363 					  path->device->lun_id);
1364 
1365 		if (status != CAM_REQ_CMP) {
1366 			xpt_print(path, "xpt_scan_lun: can't compile path, "
1367 			    "can't continue\n");
1368 			free(request_ccb, M_CAMXPT);
1369 			free(new_path, M_CAMXPT);
1370 			return;
1371 		}
1372 		xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_XPT);
1373 		request_ccb->ccb_h.cbfcnp = xptscandone;
1374 		request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1375 		request_ccb->crcn.flags = flags;
1376 	}
1377 
1378 	if ((old_periph = cam_periph_find(path, "aprobe")) != NULL) {
1379 		probe_softc *softc;
1380 
1381 		softc = (probe_softc *)old_periph->softc;
1382 		TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
1383 				  periph_links.tqe);
1384 		softc->restart = 1;
1385 	} else {
1386 		status = cam_periph_alloc(proberegister, NULL, probecleanup,
1387 					  probestart, "aprobe",
1388 					  CAM_PERIPH_BIO,
1389 					  request_ccb->ccb_h.path, NULL, 0,
1390 					  request_ccb);
1391 
1392 		if (status != CAM_REQ_CMP) {
1393 			xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
1394 			    "returned an error, can't continue probe\n");
1395 			request_ccb->ccb_h.status = status;
1396 			xpt_done(request_ccb);
1397 		}
1398 	}
1399 }
1400 
1401 static void
1402 xptscandone(struct cam_periph *periph, union ccb *done_ccb)
1403 {
1404 	xpt_release_path(done_ccb->ccb_h.path);
1405 	free(done_ccb->ccb_h.path, M_CAMXPT);
1406 	free(done_ccb, M_CAMXPT);
1407 }
1408 
1409 static struct cam_ed *
1410 ata_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
1411 {
1412 	struct cam_path path;
1413 	struct ata_quirk_entry *quirk;
1414 	struct cam_ed *device;
1415 	struct cam_ed *cur_device;
1416 
1417 	device = xpt_alloc_device(bus, target, lun_id);
1418 	if (device == NULL)
1419 		return (NULL);
1420 
1421 	/*
1422 	 * Take the default quirk entry until we have inquiry
1423 	 * data and can determine a better quirk to use.
1424 	 */
1425 	quirk = &ata_quirk_table[ata_quirk_table_size - 1];
1426 	device->quirk = (void *)quirk;
1427 	device->mintags = 0;
1428 	device->maxtags = 0;
1429 	bzero(&device->inq_data, sizeof(device->inq_data));
1430 	device->inq_flags = 0;
1431 	device->queue_flags = 0;
1432 	device->serial_num = NULL;
1433 	device->serial_num_len = 0;
1434 
1435 	/*
1436 	 * XXX should be limited by number of CCBs this bus can
1437 	 * do.
1438 	 */
1439 	bus->sim->max_ccbs += device->ccbq.devq_openings;
1440 	/* Insertion sort into our target's device list */
1441 	cur_device = TAILQ_FIRST(&target->ed_entries);
1442 	while (cur_device != NULL && cur_device->lun_id < lun_id)
1443 		cur_device = TAILQ_NEXT(cur_device, links);
1444 	if (cur_device != NULL) {
1445 		TAILQ_INSERT_BEFORE(cur_device, device, links);
1446 	} else {
1447 		TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
1448 	}
1449 	target->generation++;
1450 	if (lun_id != CAM_LUN_WILDCARD) {
1451 		xpt_compile_path(&path,
1452 				 NULL,
1453 				 bus->path_id,
1454 				 target->target_id,
1455 				 lun_id);
1456 		ata_device_transport(&path);
1457 		xpt_release_path(&path);
1458 	}
1459 
1460 	return (device);
1461 }
1462 
1463 static void
1464 ata_device_transport(struct cam_path *path)
1465 {
1466 	struct ccb_pathinq cpi;
1467 	struct ccb_trans_settings cts;
1468 	struct scsi_inquiry_data *inq_buf = NULL;
1469 	struct ata_params *ident_buf = NULL;
1470 
1471 	/* Get transport information from the SIM */
1472 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
1473 	cpi.ccb_h.func_code = XPT_PATH_INQ;
1474 	xpt_action((union ccb *)&cpi);
1475 
1476 	path->device->transport = cpi.transport;
1477 	if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
1478 		inq_buf = &path->device->inq_data;
1479 	if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) != 0)
1480 		ident_buf = &path->device->ident_data;
1481 	if (path->device->protocol == PROTO_ATA) {
1482 		path->device->protocol_version = ident_buf ?
1483 		    ata_version(ident_buf->version_major) : cpi.protocol_version;
1484 	} else if (path->device->protocol == PROTO_SCSI) {
1485 		path->device->protocol_version = inq_buf ?
1486 		    SID_ANSI_REV(inq_buf) : cpi.protocol_version;
1487 	}
1488 	path->device->transport_version = ident_buf ?
1489 	    ata_version(ident_buf->version_major) : cpi.transport_version;
1490 
1491 	/* Tell the controller what we think */
1492 	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1493 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1494 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
1495 	cts.transport = path->device->transport;
1496 	cts.transport_version = path->device->transport_version;
1497 	cts.protocol = path->device->protocol;
1498 	cts.protocol_version = path->device->protocol_version;
1499 	cts.proto_specific.valid = 0;
1500 	if (ident_buf) {
1501 		if (path->device->transport == XPORT_ATA) {
1502 			cts.xport_specific.ata.atapi =
1503 			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 :
1504 			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0;
1505 			cts.xport_specific.ata.valid = CTS_ATA_VALID_ATAPI;
1506 		} else {
1507 			cts.xport_specific.sata.atapi =
1508 			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 :
1509 			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0;
1510 			cts.xport_specific.sata.valid = CTS_SATA_VALID_ATAPI;
1511 		}
1512 	} else
1513 		cts.xport_specific.valid = 0;
1514 	xpt_action((union ccb *)&cts);
1515 }
1516 
1517 static void
1518 ata_action(union ccb *start_ccb)
1519 {
1520 
1521 	switch (start_ccb->ccb_h.func_code) {
1522 	case XPT_SET_TRAN_SETTINGS:
1523 	{
1524 		ata_set_transfer_settings(&start_ccb->cts,
1525 					   start_ccb->ccb_h.path->device,
1526 					   /*async_update*/FALSE);
1527 		break;
1528 	}
1529 	case XPT_SCAN_BUS:
1530 	case XPT_SCAN_TGT:
1531 		ata_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
1532 		break;
1533 	case XPT_SCAN_LUN:
1534 		ata_scan_lun(start_ccb->ccb_h.path->periph,
1535 			      start_ccb->ccb_h.path, start_ccb->crcn.flags,
1536 			      start_ccb);
1537 		break;
1538 	case XPT_GET_TRAN_SETTINGS:
1539 	{
1540 		struct cam_sim *sim;
1541 
1542 		sim = start_ccb->ccb_h.path->bus->sim;
1543 		(*(sim->sim_action))(sim, start_ccb);
1544 		break;
1545 	}
1546 	case XPT_SCSI_IO:
1547 	{
1548 		struct cam_ed *device;
1549 		u_int	maxlen = 0;
1550 
1551 		device = start_ccb->ccb_h.path->device;
1552 		if (device->protocol == PROTO_SCSI &&
1553 		    (device->flags & CAM_DEV_IDENTIFY_DATA_VALID)) {
1554 			uint16_t p =
1555 			    device->ident_data.config & ATA_PROTO_MASK;
1556 
1557 			maxlen = (p == ATA_PROTO_ATAPI_16) ? 16 :
1558 			    (p == ATA_PROTO_ATAPI_12) ? 12 : 0;
1559 		}
1560 		if (start_ccb->csio.cdb_len > maxlen) {
1561 			start_ccb->ccb_h.status = CAM_REQ_INVALID;
1562 			xpt_done(start_ccb);
1563 			break;
1564 		}
1565 		/* FALLTHROUGH */
1566 	}
1567 	default:
1568 		xpt_action_default(start_ccb);
1569 		break;
1570 	}
1571 }
1572 
1573 static void
1574 ata_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
1575 			   int async_update)
1576 {
1577 	struct	ccb_pathinq cpi;
1578 	struct	ccb_trans_settings cur_cts;
1579 	struct	ccb_trans_settings_scsi *scsi;
1580 	struct	ccb_trans_settings_scsi *cur_scsi;
1581 	struct	cam_sim *sim;
1582 	struct	scsi_inquiry_data *inq_data;
1583 
1584 	if (device == NULL) {
1585 		cts->ccb_h.status = CAM_PATH_INVALID;
1586 		xpt_done((union ccb *)cts);
1587 		return;
1588 	}
1589 
1590 	if (cts->protocol == PROTO_UNKNOWN
1591 	 || cts->protocol == PROTO_UNSPECIFIED) {
1592 		cts->protocol = device->protocol;
1593 		cts->protocol_version = device->protocol_version;
1594 	}
1595 
1596 	if (cts->protocol_version == PROTO_VERSION_UNKNOWN
1597 	 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
1598 		cts->protocol_version = device->protocol_version;
1599 
1600 	if (cts->protocol != device->protocol) {
1601 		xpt_print(cts->ccb_h.path, "Uninitialized Protocol %x:%x?\n",
1602 		       cts->protocol, device->protocol);
1603 		cts->protocol = device->protocol;
1604 	}
1605 
1606 	if (cts->protocol_version > device->protocol_version) {
1607 		if (bootverbose) {
1608 			xpt_print(cts->ccb_h.path, "Down reving Protocol "
1609 			    "Version from %d to %d?\n", cts->protocol_version,
1610 			    device->protocol_version);
1611 		}
1612 		cts->protocol_version = device->protocol_version;
1613 	}
1614 
1615 	if (cts->transport == XPORT_UNKNOWN
1616 	 || cts->transport == XPORT_UNSPECIFIED) {
1617 		cts->transport = device->transport;
1618 		cts->transport_version = device->transport_version;
1619 	}
1620 
1621 	if (cts->transport_version == XPORT_VERSION_UNKNOWN
1622 	 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
1623 		cts->transport_version = device->transport_version;
1624 
1625 	if (cts->transport != device->transport) {
1626 		xpt_print(cts->ccb_h.path, "Uninitialized Transport %x:%x?\n",
1627 		    cts->transport, device->transport);
1628 		cts->transport = device->transport;
1629 	}
1630 
1631 	if (cts->transport_version > device->transport_version) {
1632 		if (bootverbose) {
1633 			xpt_print(cts->ccb_h.path, "Down reving Transport "
1634 			    "Version from %d to %d?\n", cts->transport_version,
1635 			    device->transport_version);
1636 		}
1637 		cts->transport_version = device->transport_version;
1638 	}
1639 
1640 	sim = cts->ccb_h.path->bus->sim;
1641 
1642 	/*
1643 	 * Nothing more of interest to do unless
1644 	 * this is a device connected via the
1645 	 * SCSI protocol.
1646 	 */
1647 	if (cts->protocol != PROTO_SCSI) {
1648 		if (async_update == FALSE)
1649 			(*(sim->sim_action))(sim, (union ccb *)cts);
1650 		return;
1651 	}
1652 
1653 	inq_data = &device->inq_data;
1654 	scsi = &cts->proto_specific.scsi;
1655 	xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NONE);
1656 	cpi.ccb_h.func_code = XPT_PATH_INQ;
1657 	xpt_action((union ccb *)&cpi);
1658 
1659 	/* SCSI specific sanity checking */
1660 	if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
1661 	 || (INQ_DATA_TQ_ENABLED(inq_data)) == 0
1662 	 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
1663 	 || (device->mintags == 0)) {
1664 		/*
1665 		 * Can't tag on hardware that doesn't support tags,
1666 		 * doesn't have it enabled, or has broken tag support.
1667 		 */
1668 		scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1669 	}
1670 
1671 	if (async_update == FALSE) {
1672 		/*
1673 		 * Perform sanity checking against what the
1674 		 * controller and device can do.
1675 		 */
1676 		xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NONE);
1677 		cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1678 		cur_cts.type = cts->type;
1679 		xpt_action((union ccb *)&cur_cts);
1680 		if ((cur_cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1681 			return;
1682 		}
1683 		cur_scsi = &cur_cts.proto_specific.scsi;
1684 		if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
1685 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1686 			scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
1687 		}
1688 		if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
1689 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1690 	}
1691 
1692 	if (cts->type == CTS_TYPE_CURRENT_SETTINGS
1693 	 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
1694 		int device_tagenb;
1695 
1696 		/*
1697 		 * If we are transitioning from tags to no-tags or
1698 		 * vice-versa, we need to carefully freeze and restart
1699 		 * the queue so that we don't overlap tagged and non-tagged
1700 		 * commands.  We also temporarily stop tags if there is
1701 		 * a change in transfer negotiation settings to allow
1702 		 * "tag-less" negotiation.
1703 		 */
1704 		if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
1705 		 || (device->inq_flags & SID_CmdQue) != 0)
1706 			device_tagenb = TRUE;
1707 		else
1708 			device_tagenb = FALSE;
1709 
1710 		if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
1711 		  && device_tagenb == FALSE)
1712 		 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
1713 		  && device_tagenb == TRUE)) {
1714 
1715 			if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
1716 				/*
1717 				 * Delay change to use tags until after a
1718 				 * few commands have gone to this device so
1719 				 * the controller has time to perform transfer
1720 				 * negotiations without tagged messages getting
1721 				 * in the way.
1722 				 */
1723 				device->tag_delay_count = CAM_TAG_DELAY_COUNT;
1724 				device->flags |= CAM_DEV_TAG_AFTER_COUNT;
1725 			} else {
1726 				xpt_stop_tags(cts->ccb_h.path);
1727 			}
1728 		}
1729 	}
1730 	if (async_update == FALSE)
1731 		(*(sim->sim_action))(sim, (union ccb *)cts);
1732 }
1733 
1734 /*
1735  * Handle any per-device event notifications that require action by the XPT.
1736  */
1737 static void
1738 ata_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
1739 	      struct cam_ed *device, void *async_arg)
1740 {
1741 	cam_status status;
1742 	struct cam_path newpath;
1743 
1744 	/*
1745 	 * We only need to handle events for real devices.
1746 	 */
1747 	if (target->target_id == CAM_TARGET_WILDCARD
1748 	 || device->lun_id == CAM_LUN_WILDCARD)
1749 		return;
1750 
1751 	/*
1752 	 * We need our own path with wildcards expanded to
1753 	 * handle certain types of events.
1754 	 */
1755 	if ((async_code == AC_SENT_BDR)
1756 	 || (async_code == AC_BUS_RESET)
1757 	 || (async_code == AC_INQ_CHANGED))
1758 		status = xpt_compile_path(&newpath, NULL,
1759 					  bus->path_id,
1760 					  target->target_id,
1761 					  device->lun_id);
1762 	else
1763 		status = CAM_REQ_CMP_ERR;
1764 
1765 	if (status == CAM_REQ_CMP) {
1766 		if (async_code == AC_INQ_CHANGED) {
1767 			/*
1768 			 * We've sent a start unit command, or
1769 			 * something similar to a device that
1770 			 * may have caused its inquiry data to
1771 			 * change. So we re-scan the device to
1772 			 * refresh the inquiry data for it.
1773 			 */
1774 			ata_scan_lun(newpath.periph, &newpath,
1775 				     CAM_EXPECT_INQ_CHANGE, NULL);
1776 		} else {
1777 			/* We need to reinitialize device after reset. */
1778 			ata_scan_lun(newpath.periph, &newpath,
1779 				     0, NULL);
1780 		}
1781 		xpt_release_path(&newpath);
1782 	} else if (async_code == AC_LOST_DEVICE &&
1783 	    (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
1784 		device->flags |= CAM_DEV_UNCONFIGURED;
1785 		xpt_release_device(device);
1786 	} else if (async_code == AC_TRANSFER_NEG) {
1787 		struct ccb_trans_settings *settings;
1788 
1789 		settings = (struct ccb_trans_settings *)async_arg;
1790 		ata_set_transfer_settings(settings, device,
1791 					  /*async_update*/TRUE);
1792 	}
1793 }
1794 
1795 static void
1796 ata_announce_periph(struct cam_periph *periph)
1797 {
1798 	struct	ccb_pathinq cpi;
1799 	struct	ccb_trans_settings cts;
1800 	struct	cam_path *path = periph->path;
1801 	u_int	speed;
1802 	u_int	mb;
1803 
1804 	mtx_assert(periph->sim->mtx, MA_OWNED);
1805 
1806 	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
1807 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1808 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
1809 	xpt_action((union ccb*)&cts);
1810 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
1811 		return;
1812 	/* Ask the SIM for its base transfer speed */
1813 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
1814 	cpi.ccb_h.func_code = XPT_PATH_INQ;
1815 	xpt_action((union ccb *)&cpi);
1816 	/* Report connection speed */
1817 	speed = cpi.base_transfer_speed;
1818 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_ATA) {
1819 		struct	ccb_trans_settings_ata *ata =
1820 		    &cts.xport_specific.ata;
1821 
1822 		if (ata->valid & CTS_ATA_VALID_MODE)
1823 			speed = ata_mode2speed(ata->mode);
1824 	}
1825 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SATA) {
1826 		struct	ccb_trans_settings_sata *sata =
1827 		    &cts.xport_specific.sata;
1828 
1829 		if (sata->valid & CTS_SATA_VALID_REVISION)
1830 			speed = ata_revision2speed(sata->revision);
1831 	}
1832 	mb = speed / 1000;
1833 	if (mb > 0)
1834 		printf("%s%d: %d.%03dMB/s transfers",
1835 		       periph->periph_name, periph->unit_number,
1836 		       mb, speed % 1000);
1837 	else
1838 		printf("%s%d: %dKB/s transfers", periph->periph_name,
1839 		       periph->unit_number, speed);
1840 	/* Report additional information about connection */
1841 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_ATA) {
1842 		struct ccb_trans_settings_ata *ata =
1843 		    &cts.xport_specific.ata;
1844 
1845 		printf(" (");
1846 		if (ata->valid & CTS_ATA_VALID_MODE)
1847 			printf("%s, ", ata_mode2string(ata->mode));
1848 		if ((ata->valid & CTS_ATA_VALID_ATAPI) && ata->atapi != 0)
1849 			printf("ATAPI %dbytes, ", ata->atapi);
1850 		if (ata->valid & CTS_ATA_VALID_BYTECOUNT)
1851 			printf("PIO %dbytes", ata->bytecount);
1852 		printf(")");
1853 	}
1854 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SATA) {
1855 		struct ccb_trans_settings_sata *sata =
1856 		    &cts.xport_specific.sata;
1857 
1858 		printf(" (");
1859 		if (sata->valid & CTS_SATA_VALID_REVISION)
1860 			printf("SATA %d.x, ", sata->revision);
1861 		else
1862 			printf("SATA, ");
1863 		if (sata->valid & CTS_SATA_VALID_MODE)
1864 			printf("%s, ", ata_mode2string(sata->mode));
1865 		if ((sata->valid & CTS_ATA_VALID_ATAPI) && sata->atapi != 0)
1866 			printf("ATAPI %dbytes, ", sata->atapi);
1867 		if (sata->valid & CTS_SATA_VALID_BYTECOUNT)
1868 			printf("PIO %dbytes", sata->bytecount);
1869 		printf(")");
1870 	}
1871 	printf("\n");
1872 }
1873 
1874