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