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