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