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