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