xref: /freebsd/sys/cam/ata/ata_xpt.c (revision 8e3e3a7ae841ccf6f6ac30a2eeab85df5d7f04bc)
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 		    NULL) == 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 			bzero(&cpi, sizeof(cpi));
1010 			xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
1011 			cpi.ccb_h.func_code = XPT_PATH_INQ;
1012 			xpt_action((union ccb *)&cpi);
1013 			if (cpi.ccb_h.status == CAM_REQ_CMP &&
1014 			    (cpi.hba_inquiry & PI_TAG_ABLE)) {
1015 				/* Report SIM which tags are allowed. */
1016 				bzero(&cts, sizeof(cts));
1017 				xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1018 				cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1019 				cts.type = CTS_TYPE_CURRENT_SETTINGS;
1020 				cts.xport_specific.sata.tags = path->device->maxtags;
1021 				cts.xport_specific.sata.valid = CTS_SATA_VALID_TAGS;
1022 				xpt_action((union ccb *)&cts);
1023 			}
1024 		}
1025 		ata_device_transport(path);
1026 		if (changed)
1027 			proberequestdefaultnegotiation(periph);
1028 		PROBE_SET_ACTION(softc, PROBE_SETMODE);
1029 		xpt_release_ccb(done_ccb);
1030 		xpt_schedule(periph, priority);
1031 		goto out;
1032 	}
1033 	case PROBE_SPINUP:
1034 		if (bootverbose)
1035 			xpt_print(path, "Spin-up done\n");
1036 		softc->spinup = 1;
1037 		PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
1038 		xpt_release_ccb(done_ccb);
1039 		xpt_schedule(periph, priority);
1040 		goto out;
1041 	case PROBE_SETMODE:
1042 		/* Set supported bits. */
1043 		bzero(&cts, sizeof(cts));
1044 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1045 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1046 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
1047 		xpt_action((union ccb *)&cts);
1048 		if (path->device->transport == XPORT_SATA &&
1049 		    cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1050 			caps = cts.xport_specific.sata.caps & CTS_SATA_CAPS_H;
1051 		else if (path->device->transport == XPORT_ATA &&
1052 		    cts.xport_specific.ata.valid & CTS_ATA_VALID_CAPS)
1053 			caps = cts.xport_specific.ata.caps & CTS_ATA_CAPS_H;
1054 		else
1055 			caps = 0;
1056 		if (path->device->transport == XPORT_SATA &&
1057 		    ident_buf->satacapabilities != 0xffff) {
1058 			if (ident_buf->satacapabilities & ATA_SUPPORT_IFPWRMNGTRCV)
1059 				caps |= CTS_SATA_CAPS_D_PMREQ;
1060 			if (ident_buf->satacapabilities & ATA_SUPPORT_HAPST)
1061 				caps |= CTS_SATA_CAPS_D_APST;
1062 		}
1063 		/* Mask unwanted bits. */
1064 		bzero(&cts, sizeof(cts));
1065 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1066 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1067 		cts.type = CTS_TYPE_USER_SETTINGS;
1068 		xpt_action((union ccb *)&cts);
1069 		if (path->device->transport == XPORT_SATA &&
1070 		    cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1071 			caps &= cts.xport_specific.sata.caps;
1072 		else if (path->device->transport == XPORT_ATA &&
1073 		    cts.xport_specific.ata.valid & CTS_ATA_VALID_CAPS)
1074 			caps &= cts.xport_specific.ata.caps;
1075 		else
1076 			caps = 0;
1077 		/*
1078 		 * Remember what transport thinks about 48-bit DMA.  If
1079 		 * capability information is not provided or transport is
1080 		 * SATA, we take support for granted.
1081 		 */
1082 		if (!(path->device->inq_flags & SID_DMA) ||
1083 		    (path->device->transport == XPORT_ATA &&
1084 		    (cts.xport_specific.ata.valid & CTS_ATA_VALID_CAPS) &&
1085 		    !(caps & CTS_ATA_CAPS_H_DMA48)))
1086 			path->device->inq_flags &= ~SID_DMA48;
1087 		else
1088 			path->device->inq_flags |= SID_DMA48;
1089 		/* Store result to SIM. */
1090 		bzero(&cts, sizeof(cts));
1091 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1092 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1093 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
1094 		if (path->device->transport == XPORT_SATA) {
1095 			cts.xport_specific.sata.caps = caps;
1096 			cts.xport_specific.sata.valid = CTS_SATA_VALID_CAPS;
1097 		} else {
1098 			cts.xport_specific.ata.caps = caps;
1099 			cts.xport_specific.ata.valid = CTS_ATA_VALID_CAPS;
1100 		}
1101 		xpt_action((union ccb *)&cts);
1102 		softc->caps = caps;
1103 		if (path->device->transport != XPORT_SATA)
1104 			goto notsata;
1105 		if ((ident_buf->satasupport & ATA_SUPPORT_IFPWRMNGT) &&
1106 		    (!(softc->caps & CTS_SATA_CAPS_H_PMREQ)) !=
1107 		    (!(ident_buf->sataenabled & ATA_SUPPORT_IFPWRMNGT))) {
1108 			PROBE_SET_ACTION(softc, PROBE_SETPM);
1109 			xpt_release_ccb(done_ccb);
1110 			xpt_schedule(periph, priority);
1111 			goto out;
1112 		}
1113 		/* FALLTHROUGH */
1114 	case PROBE_SETPM:
1115 		if (ident_buf->satacapabilities != 0xffff &&
1116 		    (ident_buf->satacapabilities & ATA_SUPPORT_DAPST) &&
1117 		    (!(softc->caps & CTS_SATA_CAPS_H_APST)) !=
1118 		    (!(ident_buf->sataenabled & ATA_ENABLED_DAPST))) {
1119 			PROBE_SET_ACTION(softc, PROBE_SETAPST);
1120 			xpt_release_ccb(done_ccb);
1121 			xpt_schedule(periph, priority);
1122 			goto out;
1123 		}
1124 		/* FALLTHROUGH */
1125 	case PROBE_SETAPST:
1126 		if ((ident_buf->satasupport & ATA_SUPPORT_AUTOACTIVATE) &&
1127 		    (!(softc->caps & CTS_SATA_CAPS_H_DMAAA)) !=
1128 		    (!(ident_buf->sataenabled & ATA_SUPPORT_AUTOACTIVATE))) {
1129 			PROBE_SET_ACTION(softc, PROBE_SETDMAAA);
1130 			xpt_release_ccb(done_ccb);
1131 			xpt_schedule(periph, priority);
1132 			goto out;
1133 		}
1134 		/* FALLTHROUGH */
1135 	case PROBE_SETDMAAA:
1136 		if (path->device->protocol != PROTO_ATA &&
1137 		    (ident_buf->satasupport & ATA_SUPPORT_ASYNCNOTIF) &&
1138 		    (!(softc->caps & CTS_SATA_CAPS_H_AN)) !=
1139 		    (!(ident_buf->sataenabled & ATA_SUPPORT_ASYNCNOTIF))) {
1140 			PROBE_SET_ACTION(softc, PROBE_SETAN);
1141 			xpt_release_ccb(done_ccb);
1142 			xpt_schedule(periph, priority);
1143 			goto out;
1144 		}
1145 		/* FALLTHROUGH */
1146 	case PROBE_SETAN:
1147 notsata:
1148 		if (path->device->protocol == PROTO_ATA) {
1149 			PROBE_SET_ACTION(softc, PROBE_SET_MULTI);
1150 		} else {
1151 			PROBE_SET_ACTION(softc, PROBE_INQUIRY);
1152 		}
1153 		xpt_release_ccb(done_ccb);
1154 		xpt_schedule(periph, priority);
1155 		goto out;
1156 	case PROBE_SET_MULTI:
1157 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1158 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1159 			xpt_acquire_device(path->device);
1160 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1161 			xpt_action(done_ccb);
1162 			xpt_async(AC_FOUND_DEVICE, path, done_ccb);
1163 		}
1164 		PROBE_SET_ACTION(softc, PROBE_DONE);
1165 		break;
1166 	case PROBE_INQUIRY:
1167 	case PROBE_FULL_INQUIRY:
1168 	{
1169 		u_int8_t periph_qual, len;
1170 
1171 		path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
1172 
1173 		periph_qual = SID_QUAL(inq_buf);
1174 
1175 		if (periph_qual != SID_QUAL_LU_CONNECTED &&
1176 		    periph_qual != SID_QUAL_LU_OFFLINE)
1177 			break;
1178 
1179 		/*
1180 		 * We conservatively request only
1181 		 * SHORT_INQUIRY_LEN bytes of inquiry
1182 		 * information during our first try
1183 		 * at sending an INQUIRY. If the device
1184 		 * has more information to give,
1185 		 * perform a second request specifying
1186 		 * the amount of information the device
1187 		 * is willing to give.
1188 		 */
1189 		len = inq_buf->additional_length
1190 		    + offsetof(struct scsi_inquiry_data, additional_length) + 1;
1191 		if (softc->action == PROBE_INQUIRY
1192 		    && len > SHORT_INQUIRY_LENGTH) {
1193 			PROBE_SET_ACTION(softc, PROBE_FULL_INQUIRY);
1194 			xpt_release_ccb(done_ccb);
1195 			xpt_schedule(periph, priority);
1196 			goto out;
1197 		}
1198 
1199 		ata_device_transport(path);
1200 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1201 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1202 			xpt_acquire_device(path->device);
1203 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1204 			xpt_action(done_ccb);
1205 			xpt_async(AC_FOUND_DEVICE, path, done_ccb);
1206 		}
1207 		PROBE_SET_ACTION(softc, PROBE_DONE);
1208 		break;
1209 	}
1210 	case PROBE_PM_PID:
1211 		if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) == 0)
1212 			bzero(ident_buf, sizeof(*ident_buf));
1213 		softc->pm_pid = (done_ccb->ataio.res.lba_high << 24) +
1214 		    (done_ccb->ataio.res.lba_mid << 16) +
1215 		    (done_ccb->ataio.res.lba_low << 8) +
1216 		    done_ccb->ataio.res.sector_count;
1217 		((uint32_t *)ident_buf)[0] = softc->pm_pid;
1218 		snprintf(ident_buf->model, sizeof(ident_buf->model),
1219 		    "Port Multiplier %08x", softc->pm_pid);
1220 		PROBE_SET_ACTION(softc, PROBE_PM_PRV);
1221 		xpt_release_ccb(done_ccb);
1222 		xpt_schedule(periph, priority);
1223 		goto out;
1224 	case PROBE_PM_PRV:
1225 		softc->pm_prv = (done_ccb->ataio.res.lba_high << 24) +
1226 		    (done_ccb->ataio.res.lba_mid << 16) +
1227 		    (done_ccb->ataio.res.lba_low << 8) +
1228 		    done_ccb->ataio.res.sector_count;
1229 		((uint32_t *)ident_buf)[1] = softc->pm_prv;
1230 		snprintf(ident_buf->revision, sizeof(ident_buf->revision),
1231 		    "%04x", softc->pm_prv);
1232 		path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
1233 		ata_device_transport(path);
1234 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED)
1235 			proberequestdefaultnegotiation(periph);
1236 		/* Set supported bits. */
1237 		bzero(&cts, sizeof(cts));
1238 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1239 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1240 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
1241 		xpt_action((union ccb *)&cts);
1242 		if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1243 			caps = cts.xport_specific.sata.caps & CTS_SATA_CAPS_H;
1244 		else
1245 			caps = 0;
1246 		/* All PMPs must support PM requests. */
1247 		caps |= CTS_SATA_CAPS_D_PMREQ;
1248 		/* Mask unwanted bits. */
1249 		bzero(&cts, sizeof(cts));
1250 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1251 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1252 		cts.type = CTS_TYPE_USER_SETTINGS;
1253 		xpt_action((union ccb *)&cts);
1254 		if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1255 			caps &= cts.xport_specific.sata.caps;
1256 		else
1257 			caps = 0;
1258 		/* Remember what transport thinks about AEN. */
1259 		if ((caps & CTS_SATA_CAPS_H_AN) && path->device->protocol != PROTO_ATA)
1260 			path->device->inq_flags |= SID_AEN;
1261 		else
1262 			path->device->inq_flags &= ~SID_AEN;
1263 		/* Store result to SIM. */
1264 		bzero(&cts, sizeof(cts));
1265 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1266 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1267 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
1268 		cts.xport_specific.sata.caps = caps;
1269 		cts.xport_specific.sata.valid = CTS_SATA_VALID_CAPS;
1270 		xpt_action((union ccb *)&cts);
1271 		softc->caps = caps;
1272 		xpt_async(AC_GETDEV_CHANGED, path, NULL);
1273 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1274 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1275 			xpt_acquire_device(path->device);
1276 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1277 			xpt_action(done_ccb);
1278 			xpt_async(AC_FOUND_DEVICE, path, done_ccb);
1279 		} else {
1280 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1281 			xpt_action(done_ccb);
1282 			xpt_async(AC_SCSI_AEN, path, done_ccb);
1283 		}
1284 		PROBE_SET_ACTION(softc, PROBE_DONE);
1285 		break;
1286 	case PROBE_IDENTIFY_SES:
1287 	case PROBE_IDENTIFY_SAFTE:
1288 		if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
1289 			/* Check that it is the same device. */
1290 			if (bcmp(&softc->ident_data, ident_buf, 53)) {
1291 				/* Device changed. */
1292 				xpt_async(AC_LOST_DEVICE, path, NULL);
1293 			} else {
1294 				bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
1295 				changed = 0;
1296 			}
1297 		}
1298 		if (changed) {
1299 			bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
1300 			/* Clean up from previous instance of this device */
1301 			if (path->device->device_id != NULL) {
1302 				free(path->device->device_id, M_CAMXPT);
1303 				path->device->device_id = NULL;
1304 				path->device->device_id_len = 0;
1305 			}
1306 			path->device->device_id =
1307 			    malloc(16, M_CAMXPT, M_NOWAIT);
1308 			if (path->device->device_id != NULL) {
1309 				path->device->device_id_len = 16;
1310 				bcopy(&fake_device_id_hdr,
1311 				    path->device->device_id, 8);
1312 				bcopy(((uint8_t*)ident_buf) + 2,
1313 				    path->device->device_id + 8, 8);
1314 			}
1315 
1316 			path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
1317 		}
1318 		ata_device_transport(path);
1319 		if (changed)
1320 			proberequestdefaultnegotiation(periph);
1321 
1322 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1323 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1324 			xpt_acquire_device(path->device);
1325 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1326 			xpt_action(done_ccb);
1327 			xpt_async(AC_FOUND_DEVICE, path, done_ccb);
1328 		}
1329 		PROBE_SET_ACTION(softc, PROBE_DONE);
1330 		break;
1331 	default:
1332 		panic("probedone: invalid action state 0x%x\n", softc->action);
1333 	}
1334 done:
1335 	if (softc->restart) {
1336 		softc->restart = 0;
1337 		xpt_release_ccb(done_ccb);
1338 		probeschedule(periph);
1339 		goto out;
1340 	}
1341 	xpt_release_ccb(done_ccb);
1342 	CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe completed\n"));
1343 	while ((done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs))) {
1344 		TAILQ_REMOVE(&softc->request_ccbs,
1345 		    &done_ccb->ccb_h, periph_links.tqe);
1346 		done_ccb->ccb_h.status = found ? CAM_REQ_CMP : CAM_REQ_CMP_ERR;
1347 		xpt_done(done_ccb);
1348 	}
1349 	/* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
1350 	cam_release_devq(path, 0, 0, 0, FALSE);
1351 	cam_periph_invalidate(periph);
1352 	cam_periph_release_locked(periph);
1353 }
1354 
1355 static void
1356 probecleanup(struct cam_periph *periph)
1357 {
1358 	free(periph->softc, M_CAMXPT);
1359 }
1360 
1361 static void
1362 ata_find_quirk(struct cam_ed *device)
1363 {
1364 	struct ata_quirk_entry *quirk;
1365 	caddr_t	match;
1366 
1367 	match = cam_quirkmatch((caddr_t)&device->ident_data,
1368 			       (caddr_t)ata_quirk_table,
1369 			       nitems(ata_quirk_table),
1370 			       sizeof(*ata_quirk_table), ata_identify_match);
1371 
1372 	if (match == NULL)
1373 		panic("xpt_find_quirk: device didn't match wildcard entry!!");
1374 
1375 	quirk = (struct ata_quirk_entry *)match;
1376 	device->quirk = quirk;
1377 	if (quirk->quirks & CAM_QUIRK_MAXTAGS) {
1378 		device->mintags = quirk->mintags;
1379 		device->maxtags = quirk->maxtags;
1380 	}
1381 }
1382 
1383 typedef struct {
1384 	union	ccb *request_ccb;
1385 	struct 	ccb_pathinq *cpi;
1386 	int	counter;
1387 } ata_scan_bus_info;
1388 
1389 /*
1390  * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
1391  * As the scan progresses, xpt_scan_bus is used as the
1392  * callback on completion function.
1393  */
1394 static void
1395 ata_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
1396 {
1397 	struct	cam_path *path;
1398 	ata_scan_bus_info *scan_info;
1399 	union	ccb *work_ccb, *reset_ccb;
1400 	struct mtx *mtx;
1401 	cam_status status;
1402 
1403 	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
1404 		  ("xpt_scan_bus\n"));
1405 	switch (request_ccb->ccb_h.func_code) {
1406 	case XPT_SCAN_BUS:
1407 	case XPT_SCAN_TGT:
1408 		/* Find out the characteristics of the bus */
1409 		work_ccb = xpt_alloc_ccb_nowait();
1410 		if (work_ccb == NULL) {
1411 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1412 			xpt_done(request_ccb);
1413 			return;
1414 		}
1415 		xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
1416 			      request_ccb->ccb_h.pinfo.priority);
1417 		work_ccb->ccb_h.func_code = XPT_PATH_INQ;
1418 		xpt_action(work_ccb);
1419 		if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
1420 			request_ccb->ccb_h.status = work_ccb->ccb_h.status;
1421 			xpt_free_ccb(work_ccb);
1422 			xpt_done(request_ccb);
1423 			return;
1424 		}
1425 
1426 		/* We may need to reset bus first, if we haven't done it yet. */
1427 		if ((work_ccb->cpi.hba_inquiry &
1428 		    (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) &&
1429 		    !(work_ccb->cpi.hba_misc & PIM_NOBUSRESET) &&
1430 		    !timevalisset(&request_ccb->ccb_h.path->bus->last_reset)) {
1431 			reset_ccb = xpt_alloc_ccb_nowait();
1432 			if (reset_ccb == NULL) {
1433 				request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1434 				xpt_free_ccb(work_ccb);
1435 				xpt_done(request_ccb);
1436 				return;
1437 			}
1438 			xpt_setup_ccb(&reset_ccb->ccb_h, request_ccb->ccb_h.path,
1439 			      CAM_PRIORITY_NONE);
1440 			reset_ccb->ccb_h.func_code = XPT_RESET_BUS;
1441 			xpt_action(reset_ccb);
1442 			if (reset_ccb->ccb_h.status != CAM_REQ_CMP) {
1443 				request_ccb->ccb_h.status = reset_ccb->ccb_h.status;
1444 				xpt_free_ccb(reset_ccb);
1445 				xpt_free_ccb(work_ccb);
1446 				xpt_done(request_ccb);
1447 				return;
1448 			}
1449 			xpt_free_ccb(reset_ccb);
1450 		}
1451 
1452 		/* Save some state for use while we probe for devices */
1453 		scan_info = (ata_scan_bus_info *)
1454 		    malloc(sizeof(ata_scan_bus_info), M_CAMXPT, M_NOWAIT);
1455 		if (scan_info == NULL) {
1456 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1457 			xpt_free_ccb(work_ccb);
1458 			xpt_done(request_ccb);
1459 			return;
1460 		}
1461 		scan_info->request_ccb = request_ccb;
1462 		scan_info->cpi = &work_ccb->cpi;
1463 		/* If PM supported, probe it first. */
1464 		if (scan_info->cpi->hba_inquiry & PI_SATAPM)
1465 			scan_info->counter = scan_info->cpi->max_target;
1466 		else
1467 			scan_info->counter = 0;
1468 
1469 		work_ccb = xpt_alloc_ccb_nowait();
1470 		if (work_ccb == NULL) {
1471 			free(scan_info, M_CAMXPT);
1472 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1473 			xpt_done(request_ccb);
1474 			break;
1475 		}
1476 		mtx = xpt_path_mtx(scan_info->request_ccb->ccb_h.path);
1477 		goto scan_next;
1478 	case XPT_SCAN_LUN:
1479 		work_ccb = request_ccb;
1480 		/* Reuse the same CCB to query if a device was really found */
1481 		scan_info = (ata_scan_bus_info *)work_ccb->ccb_h.ppriv_ptr0;
1482 		mtx = xpt_path_mtx(scan_info->request_ccb->ccb_h.path);
1483 		mtx_lock(mtx);
1484 		/* If there is PMP... */
1485 		if ((scan_info->cpi->hba_inquiry & PI_SATAPM) &&
1486 		    (scan_info->counter == scan_info->cpi->max_target)) {
1487 			if (work_ccb->ccb_h.status == CAM_REQ_CMP) {
1488 				/* everything else will be probed by it */
1489 				/* Free the current request path- we're done with it. */
1490 				xpt_free_path(work_ccb->ccb_h.path);
1491 				goto done;
1492 			} else {
1493 				struct ccb_trans_settings cts;
1494 
1495 				/* Report SIM that PM is absent. */
1496 				bzero(&cts, sizeof(cts));
1497 				xpt_setup_ccb(&cts.ccb_h,
1498 				    work_ccb->ccb_h.path, CAM_PRIORITY_NONE);
1499 				cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1500 				cts.type = CTS_TYPE_CURRENT_SETTINGS;
1501 				cts.xport_specific.sata.pm_present = 0;
1502 				cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
1503 				xpt_action((union ccb *)&cts);
1504 			}
1505 		}
1506 		/* Free the current request path- we're done with it. */
1507 		xpt_free_path(work_ccb->ccb_h.path);
1508 		if (scan_info->counter ==
1509 		    ((scan_info->cpi->hba_inquiry & PI_SATAPM) ?
1510 		    0 : scan_info->cpi->max_target)) {
1511 done:
1512 			mtx_unlock(mtx);
1513 			xpt_free_ccb(work_ccb);
1514 			xpt_free_ccb((union ccb *)scan_info->cpi);
1515 			request_ccb = scan_info->request_ccb;
1516 			free(scan_info, M_CAMXPT);
1517 			request_ccb->ccb_h.status = CAM_REQ_CMP;
1518 			xpt_done(request_ccb);
1519 			break;
1520 		}
1521 		/* Take next device. Wrap from max (PMP) to 0. */
1522 		scan_info->counter = (scan_info->counter + 1 ) %
1523 		    (scan_info->cpi->max_target + 1);
1524 scan_next:
1525 		status = xpt_create_path(&path, NULL,
1526 		    scan_info->request_ccb->ccb_h.path_id,
1527 		    scan_info->counter, 0);
1528 		if (status != CAM_REQ_CMP) {
1529 			if (request_ccb->ccb_h.func_code == XPT_SCAN_LUN)
1530 				mtx_unlock(mtx);
1531 			printf("xpt_scan_bus: xpt_create_path failed"
1532 			    " with status %#x, bus scan halted\n",
1533 			    status);
1534 			xpt_free_ccb(work_ccb);
1535 			xpt_free_ccb((union ccb *)scan_info->cpi);
1536 			request_ccb = scan_info->request_ccb;
1537 			free(scan_info, M_CAMXPT);
1538 			request_ccb->ccb_h.status = status;
1539 			xpt_done(request_ccb);
1540 			break;
1541 		}
1542 		xpt_setup_ccb(&work_ccb->ccb_h, path,
1543 		    scan_info->request_ccb->ccb_h.pinfo.priority);
1544 		work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1545 		work_ccb->ccb_h.cbfcnp = ata_scan_bus;
1546 		work_ccb->ccb_h.flags |= CAM_UNLOCKED;
1547 		work_ccb->ccb_h.ppriv_ptr0 = scan_info;
1548 		work_ccb->crcn.flags = scan_info->request_ccb->crcn.flags;
1549 		mtx_unlock(mtx);
1550 		if (request_ccb->ccb_h.func_code == XPT_SCAN_LUN)
1551 			mtx = NULL;
1552 		xpt_action(work_ccb);
1553 		if (mtx != NULL)
1554 			mtx_lock(mtx);
1555 		break;
1556 	default:
1557 		break;
1558 	}
1559 }
1560 
1561 static void
1562 ata_scan_lun(struct cam_periph *periph, struct cam_path *path,
1563 	     cam_flags flags, union ccb *request_ccb)
1564 {
1565 	struct ccb_pathinq cpi;
1566 	cam_status status;
1567 	struct cam_path *new_path;
1568 	struct cam_periph *old_periph;
1569 	int lock;
1570 
1571 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_scan_lun\n"));
1572 
1573 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
1574 	cpi.ccb_h.func_code = XPT_PATH_INQ;
1575 	xpt_action((union ccb *)&cpi);
1576 
1577 	if (cpi.ccb_h.status != CAM_REQ_CMP) {
1578 		if (request_ccb != NULL) {
1579 			request_ccb->ccb_h.status = cpi.ccb_h.status;
1580 			xpt_done(request_ccb);
1581 		}
1582 		return;
1583 	}
1584 
1585 	if (request_ccb == NULL) {
1586 		request_ccb = xpt_alloc_ccb_nowait();
1587 		if (request_ccb == NULL) {
1588 			xpt_print(path, "xpt_scan_lun: can't allocate CCB, "
1589 			    "can't continue\n");
1590 			return;
1591 		}
1592 		status = xpt_create_path(&new_path, NULL,
1593 					  path->bus->path_id,
1594 					  path->target->target_id,
1595 					  path->device->lun_id);
1596 		if (status != CAM_REQ_CMP) {
1597 			xpt_print(path, "xpt_scan_lun: can't create path, "
1598 			    "can't continue\n");
1599 			xpt_free_ccb(request_ccb);
1600 			return;
1601 		}
1602 		xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_XPT);
1603 		request_ccb->ccb_h.cbfcnp = xptscandone;
1604 		request_ccb->ccb_h.flags |= CAM_UNLOCKED;
1605 		request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1606 		request_ccb->crcn.flags = flags;
1607 	}
1608 
1609 	lock = (xpt_path_owned(path) == 0);
1610 	if (lock)
1611 		xpt_path_lock(path);
1612 	if ((old_periph = cam_periph_find(path, "aprobe")) != NULL) {
1613 		if ((old_periph->flags & CAM_PERIPH_INVALID) == 0) {
1614 			probe_softc *softc;
1615 
1616 			softc = (probe_softc *)old_periph->softc;
1617 			TAILQ_INSERT_TAIL(&softc->request_ccbs,
1618 				&request_ccb->ccb_h, periph_links.tqe);
1619 			softc->restart = 1;
1620 		} else {
1621 			request_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1622 			xpt_done(request_ccb);
1623 		}
1624 	} else {
1625 		status = cam_periph_alloc(proberegister, NULL, probecleanup,
1626 					  probestart, "aprobe",
1627 					  CAM_PERIPH_BIO,
1628 					  request_ccb->ccb_h.path, NULL, 0,
1629 					  request_ccb);
1630 
1631 		if (status != CAM_REQ_CMP) {
1632 			xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
1633 			    "returned an error, can't continue probe\n");
1634 			request_ccb->ccb_h.status = status;
1635 			xpt_done(request_ccb);
1636 		}
1637 	}
1638 	if (lock)
1639 		xpt_path_unlock(path);
1640 }
1641 
1642 static void
1643 xptscandone(struct cam_periph *periph, union ccb *done_ccb)
1644 {
1645 
1646 	xpt_free_path(done_ccb->ccb_h.path);
1647 	xpt_free_ccb(done_ccb);
1648 }
1649 
1650 static struct cam_ed *
1651 ata_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
1652 {
1653 	struct ata_quirk_entry *quirk;
1654 	struct cam_ed *device;
1655 
1656 	device = xpt_alloc_device(bus, target, lun_id);
1657 	if (device == NULL)
1658 		return (NULL);
1659 
1660 	/*
1661 	 * Take the default quirk entry until we have inquiry
1662 	 * data and can determine a better quirk to use.
1663 	 */
1664 	quirk = &ata_quirk_table[nitems(ata_quirk_table) - 1];
1665 	device->quirk = (void *)quirk;
1666 	device->mintags = 0;
1667 	device->maxtags = 0;
1668 	bzero(&device->inq_data, sizeof(device->inq_data));
1669 	device->inq_flags = 0;
1670 	device->queue_flags = 0;
1671 	device->serial_num = NULL;
1672 	device->serial_num_len = 0;
1673 	return (device);
1674 }
1675 
1676 static void
1677 ata_device_transport(struct cam_path *path)
1678 {
1679 	struct ccb_pathinq cpi;
1680 	struct ccb_trans_settings cts;
1681 	struct scsi_inquiry_data *inq_buf = NULL;
1682 	struct ata_params *ident_buf = NULL;
1683 
1684 	/* Get transport information from the SIM */
1685 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
1686 	cpi.ccb_h.func_code = XPT_PATH_INQ;
1687 	xpt_action((union ccb *)&cpi);
1688 
1689 	path->device->transport = cpi.transport;
1690 	if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
1691 		inq_buf = &path->device->inq_data;
1692 	if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) != 0)
1693 		ident_buf = &path->device->ident_data;
1694 	if (path->device->protocol == PROTO_ATA) {
1695 		path->device->protocol_version = ident_buf ?
1696 		    ata_version(ident_buf->version_major) : cpi.protocol_version;
1697 	} else if (path->device->protocol == PROTO_SCSI) {
1698 		path->device->protocol_version = inq_buf ?
1699 		    SID_ANSI_REV(inq_buf) : cpi.protocol_version;
1700 	}
1701 	path->device->transport_version = ident_buf ?
1702 	    ata_version(ident_buf->version_major) : cpi.transport_version;
1703 
1704 	/* Tell the controller what we think */
1705 	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1706 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1707 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
1708 	cts.transport = path->device->transport;
1709 	cts.transport_version = path->device->transport_version;
1710 	cts.protocol = path->device->protocol;
1711 	cts.protocol_version = path->device->protocol_version;
1712 	cts.proto_specific.valid = 0;
1713 	if (ident_buf) {
1714 		if (path->device->transport == XPORT_ATA) {
1715 			cts.xport_specific.ata.atapi =
1716 			    (ident_buf->config == ATA_PROTO_CFA) ? 0 :
1717 			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 :
1718 			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0;
1719 			cts.xport_specific.ata.valid = CTS_ATA_VALID_ATAPI;
1720 		} else {
1721 			cts.xport_specific.sata.atapi =
1722 			    (ident_buf->config == ATA_PROTO_CFA) ? 0 :
1723 			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 :
1724 			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0;
1725 			cts.xport_specific.sata.valid = CTS_SATA_VALID_ATAPI;
1726 		}
1727 	} else
1728 		cts.xport_specific.valid = 0;
1729 	xpt_action((union ccb *)&cts);
1730 }
1731 
1732 static void
1733 ata_dev_advinfo(union ccb *start_ccb)
1734 {
1735 	struct cam_ed *device;
1736 	struct ccb_dev_advinfo *cdai;
1737 	off_t amt;
1738 
1739 	start_ccb->ccb_h.status = CAM_REQ_INVALID;
1740 	device = start_ccb->ccb_h.path->device;
1741 	cdai = &start_ccb->cdai;
1742 	switch(cdai->buftype) {
1743 	case CDAI_TYPE_SCSI_DEVID:
1744 		if (cdai->flags & CDAI_FLAG_STORE)
1745 			return;
1746 		cdai->provsiz = device->device_id_len;
1747 		if (device->device_id_len == 0)
1748 			break;
1749 		amt = device->device_id_len;
1750 		if (cdai->provsiz > cdai->bufsiz)
1751 			amt = cdai->bufsiz;
1752 		memcpy(cdai->buf, device->device_id, amt);
1753 		break;
1754 	case CDAI_TYPE_SERIAL_NUM:
1755 		if (cdai->flags & CDAI_FLAG_STORE)
1756 			return;
1757 		cdai->provsiz = device->serial_num_len;
1758 		if (device->serial_num_len == 0)
1759 			break;
1760 		amt = device->serial_num_len;
1761 		if (cdai->provsiz > cdai->bufsiz)
1762 			amt = cdai->bufsiz;
1763 		memcpy(cdai->buf, device->serial_num, amt);
1764 		break;
1765 	case CDAI_TYPE_PHYS_PATH:
1766 		if (cdai->flags & CDAI_FLAG_STORE) {
1767 			if (device->physpath != NULL)
1768 				free(device->physpath, M_CAMXPT);
1769 			device->physpath_len = cdai->bufsiz;
1770 			/* Clear existing buffer if zero length */
1771 			if (cdai->bufsiz == 0)
1772 				break;
1773 			device->physpath = malloc(cdai->bufsiz, M_CAMXPT, M_NOWAIT);
1774 			if (device->physpath == NULL) {
1775 				start_ccb->ccb_h.status = CAM_REQ_ABORTED;
1776 				return;
1777 			}
1778 			memcpy(device->physpath, cdai->buf, cdai->bufsiz);
1779 		} else {
1780 			cdai->provsiz = device->physpath_len;
1781 			if (device->physpath_len == 0)
1782 				break;
1783 			amt = device->physpath_len;
1784 			if (cdai->provsiz > cdai->bufsiz)
1785 				amt = cdai->bufsiz;
1786 			memcpy(cdai->buf, device->physpath, amt);
1787 		}
1788 		break;
1789 	default:
1790 		return;
1791 	}
1792 	start_ccb->ccb_h.status = CAM_REQ_CMP;
1793 
1794 	if (cdai->flags & CDAI_FLAG_STORE) {
1795 		xpt_async(AC_ADVINFO_CHANGED, start_ccb->ccb_h.path,
1796 			  (void *)(uintptr_t)cdai->buftype);
1797 	}
1798 }
1799 
1800 static void
1801 ata_action(union ccb *start_ccb)
1802 {
1803 
1804 	switch (start_ccb->ccb_h.func_code) {
1805 	case XPT_SET_TRAN_SETTINGS:
1806 	{
1807 		ata_set_transfer_settings(&start_ccb->cts,
1808 					   start_ccb->ccb_h.path,
1809 					   /*async_update*/FALSE);
1810 		break;
1811 	}
1812 	case XPT_SCAN_BUS:
1813 	case XPT_SCAN_TGT:
1814 		ata_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
1815 		break;
1816 	case XPT_SCAN_LUN:
1817 		ata_scan_lun(start_ccb->ccb_h.path->periph,
1818 			      start_ccb->ccb_h.path, start_ccb->crcn.flags,
1819 			      start_ccb);
1820 		break;
1821 	case XPT_GET_TRAN_SETTINGS:
1822 	{
1823 		ata_get_transfer_settings(&start_ccb->cts);
1824 		break;
1825 	}
1826 	case XPT_SCSI_IO:
1827 	{
1828 		struct cam_ed *device;
1829 		u_int	maxlen = 0;
1830 
1831 		device = start_ccb->ccb_h.path->device;
1832 		if (device->protocol == PROTO_SCSI &&
1833 		    (device->flags & CAM_DEV_IDENTIFY_DATA_VALID)) {
1834 			uint16_t p =
1835 			    device->ident_data.config & ATA_PROTO_MASK;
1836 
1837 			maxlen =
1838 			    (device->ident_data.config == ATA_PROTO_CFA) ? 0 :
1839 			    (p == ATA_PROTO_ATAPI_16) ? 16 :
1840 			    (p == ATA_PROTO_ATAPI_12) ? 12 : 0;
1841 		}
1842 		if (start_ccb->csio.cdb_len > maxlen) {
1843 			start_ccb->ccb_h.status = CAM_REQ_INVALID;
1844 			xpt_done(start_ccb);
1845 			break;
1846 		}
1847 		xpt_action_default(start_ccb);
1848 		break;
1849 	}
1850 	case XPT_DEV_ADVINFO:
1851 	{
1852 		ata_dev_advinfo(start_ccb);
1853 		break;
1854 	}
1855 	default:
1856 		xpt_action_default(start_ccb);
1857 		break;
1858 	}
1859 }
1860 
1861 static void
1862 ata_get_transfer_settings(struct ccb_trans_settings *cts)
1863 {
1864 	struct	ccb_trans_settings_ata *ata;
1865 	struct	ccb_trans_settings_scsi *scsi;
1866 	struct	cam_ed *device;
1867 
1868 	device = cts->ccb_h.path->device;
1869 	xpt_action_default((union ccb *)cts);
1870 
1871 	if (cts->protocol == PROTO_UNKNOWN ||
1872 	    cts->protocol == PROTO_UNSPECIFIED) {
1873 		cts->protocol = device->protocol;
1874 		cts->protocol_version = device->protocol_version;
1875 	}
1876 
1877 	if (cts->protocol == PROTO_ATA) {
1878 		ata = &cts->proto_specific.ata;
1879 		if ((ata->valid & CTS_ATA_VALID_TQ) == 0) {
1880 			ata->valid |= CTS_ATA_VALID_TQ;
1881 			if (cts->type == CTS_TYPE_USER_SETTINGS ||
1882 			    (device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0 ||
1883 			    (device->inq_flags & SID_CmdQue) != 0)
1884 				ata->flags |= CTS_ATA_FLAGS_TAG_ENB;
1885 		}
1886 	}
1887 	if (cts->protocol == PROTO_SCSI) {
1888 		scsi = &cts->proto_specific.scsi;
1889 		if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
1890 			scsi->valid |= CTS_SCSI_VALID_TQ;
1891 			if (cts->type == CTS_TYPE_USER_SETTINGS ||
1892 			    (device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0 ||
1893 			    (device->inq_flags & SID_CmdQue) != 0)
1894 				scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
1895 		}
1896 	}
1897 
1898 	if (cts->transport == XPORT_UNKNOWN ||
1899 	    cts->transport == XPORT_UNSPECIFIED) {
1900 		cts->transport = device->transport;
1901 		cts->transport_version = device->transport_version;
1902 	}
1903 }
1904 
1905 static void
1906 ata_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_path *path,
1907 			   int async_update)
1908 {
1909 	struct	ccb_pathinq cpi;
1910 	struct	ccb_trans_settings_ata *ata;
1911 	struct	ccb_trans_settings_scsi *scsi;
1912 	struct	ata_params *ident_data;
1913 	struct	scsi_inquiry_data *inq_data;
1914 	struct	cam_ed *device;
1915 
1916 	if (path == NULL || (device = path->device) == NULL) {
1917 		cts->ccb_h.status = CAM_PATH_INVALID;
1918 		xpt_done((union ccb *)cts);
1919 		return;
1920 	}
1921 
1922 	if (cts->protocol == PROTO_UNKNOWN
1923 	 || cts->protocol == PROTO_UNSPECIFIED) {
1924 		cts->protocol = device->protocol;
1925 		cts->protocol_version = device->protocol_version;
1926 	}
1927 
1928 	if (cts->protocol_version == PROTO_VERSION_UNKNOWN
1929 	 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
1930 		cts->protocol_version = device->protocol_version;
1931 
1932 	if (cts->protocol != device->protocol) {
1933 		xpt_print(path, "Uninitialized Protocol %x:%x?\n",
1934 		       cts->protocol, device->protocol);
1935 		cts->protocol = device->protocol;
1936 	}
1937 
1938 	if (cts->protocol_version > device->protocol_version) {
1939 		if (bootverbose) {
1940 			xpt_print(path, "Down reving Protocol "
1941 			    "Version from %d to %d?\n", cts->protocol_version,
1942 			    device->protocol_version);
1943 		}
1944 		cts->protocol_version = device->protocol_version;
1945 	}
1946 
1947 	if (cts->transport == XPORT_UNKNOWN
1948 	 || cts->transport == XPORT_UNSPECIFIED) {
1949 		cts->transport = device->transport;
1950 		cts->transport_version = device->transport_version;
1951 	}
1952 
1953 	if (cts->transport_version == XPORT_VERSION_UNKNOWN
1954 	 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
1955 		cts->transport_version = device->transport_version;
1956 
1957 	if (cts->transport != device->transport) {
1958 		xpt_print(path, "Uninitialized Transport %x:%x?\n",
1959 		    cts->transport, device->transport);
1960 		cts->transport = device->transport;
1961 	}
1962 
1963 	if (cts->transport_version > device->transport_version) {
1964 		if (bootverbose) {
1965 			xpt_print(path, "Down reving Transport "
1966 			    "Version from %d to %d?\n", cts->transport_version,
1967 			    device->transport_version);
1968 		}
1969 		cts->transport_version = device->transport_version;
1970 	}
1971 
1972 	ident_data = &device->ident_data;
1973 	inq_data = &device->inq_data;
1974 	if (cts->protocol == PROTO_ATA)
1975 		ata = &cts->proto_specific.ata;
1976 	else
1977 		ata = NULL;
1978 	if (cts->protocol == PROTO_SCSI)
1979 		scsi = &cts->proto_specific.scsi;
1980 	else
1981 		scsi = NULL;
1982 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
1983 	cpi.ccb_h.func_code = XPT_PATH_INQ;
1984 	xpt_action((union ccb *)&cpi);
1985 
1986 	/* Sanity checking */
1987 	if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
1988 	 || (ata && (ident_data->satacapabilities & ATA_SUPPORT_NCQ) == 0)
1989 	 || (scsi && (INQ_DATA_TQ_ENABLED(inq_data)) == 0)
1990 	 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
1991 	 || (device->mintags == 0)) {
1992 		/*
1993 		 * Can't tag on hardware that doesn't support tags,
1994 		 * doesn't have it enabled, or has broken tag support.
1995 		 */
1996 		if (ata)
1997 			ata->flags &= ~CTS_ATA_FLAGS_TAG_ENB;
1998 		if (scsi)
1999 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2000 	}
2001 
2002 	/* Start/stop tags use. */
2003 	if (cts->type == CTS_TYPE_CURRENT_SETTINGS &&
2004 	    ((ata && (ata->valid & CTS_ATA_VALID_TQ) != 0) ||
2005 	     (scsi && (scsi->valid & CTS_SCSI_VALID_TQ) != 0))) {
2006 		int nowt, newt = 0;
2007 
2008 		nowt = ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0 ||
2009 			(device->inq_flags & SID_CmdQue) != 0);
2010 		if (ata)
2011 			newt = (ata->flags & CTS_ATA_FLAGS_TAG_ENB) != 0;
2012 		if (scsi)
2013 			newt = (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0;
2014 
2015 		if (newt && !nowt) {
2016 			/*
2017 			 * Delay change to use tags until after a
2018 			 * few commands have gone to this device so
2019 			 * the controller has time to perform transfer
2020 			 * negotiations without tagged messages getting
2021 			 * in the way.
2022 			 */
2023 			device->tag_delay_count = CAM_TAG_DELAY_COUNT;
2024 			device->flags |= CAM_DEV_TAG_AFTER_COUNT;
2025 		} else if (nowt && !newt)
2026 			xpt_stop_tags(path);
2027 	}
2028 
2029 	if (async_update == FALSE)
2030 		xpt_action_default((union ccb *)cts);
2031 }
2032 
2033 /*
2034  * Handle any per-device event notifications that require action by the XPT.
2035  */
2036 static void
2037 ata_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
2038 	      struct cam_ed *device, void *async_arg)
2039 {
2040 	cam_status status;
2041 	struct cam_path newpath;
2042 
2043 	/*
2044 	 * We only need to handle events for real devices.
2045 	 */
2046 	if (target->target_id == CAM_TARGET_WILDCARD
2047 	 || device->lun_id == CAM_LUN_WILDCARD)
2048 		return;
2049 
2050 	/*
2051 	 * We need our own path with wildcards expanded to
2052 	 * handle certain types of events.
2053 	 */
2054 	if ((async_code == AC_SENT_BDR)
2055 	 || (async_code == AC_BUS_RESET)
2056 	 || (async_code == AC_INQ_CHANGED))
2057 		status = xpt_compile_path(&newpath, NULL,
2058 					  bus->path_id,
2059 					  target->target_id,
2060 					  device->lun_id);
2061 	else
2062 		status = CAM_REQ_CMP_ERR;
2063 
2064 	if (status == CAM_REQ_CMP) {
2065 		if (async_code == AC_INQ_CHANGED) {
2066 			/*
2067 			 * We've sent a start unit command, or
2068 			 * something similar to a device that
2069 			 * may have caused its inquiry data to
2070 			 * change. So we re-scan the device to
2071 			 * refresh the inquiry data for it.
2072 			 */
2073 			ata_scan_lun(newpath.periph, &newpath,
2074 				     CAM_EXPECT_INQ_CHANGE, NULL);
2075 		} else {
2076 			/* We need to reinitialize device after reset. */
2077 			ata_scan_lun(newpath.periph, &newpath,
2078 				     0, NULL);
2079 		}
2080 		xpt_release_path(&newpath);
2081 	} else if (async_code == AC_LOST_DEVICE &&
2082 	    (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
2083 		device->flags |= CAM_DEV_UNCONFIGURED;
2084 		xpt_release_device(device);
2085 	} else if (async_code == AC_TRANSFER_NEG) {
2086 		struct ccb_trans_settings *settings;
2087 		struct cam_path path;
2088 
2089 		settings = (struct ccb_trans_settings *)async_arg;
2090 		xpt_compile_path(&path, NULL, bus->path_id, target->target_id,
2091 				 device->lun_id);
2092 		ata_set_transfer_settings(settings, &path,
2093 					  /*async_update*/TRUE);
2094 		xpt_release_path(&path);
2095 	}
2096 }
2097 
2098 static void
2099 _ata_announce_periph(struct cam_periph *periph, struct ccb_trans_settings *cts, u_int *speed)
2100 {
2101 	struct	ccb_pathinq cpi;
2102 	struct	cam_path *path = periph->path;
2103 
2104 	cam_periph_assert(periph, MA_OWNED);
2105 
2106 	xpt_setup_ccb(&cts->ccb_h, path, CAM_PRIORITY_NORMAL);
2107 	cts->ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
2108 	cts->type = CTS_TYPE_CURRENT_SETTINGS;
2109 	xpt_action((union ccb*)cts);
2110 	if ((cts->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
2111 		return;
2112 	/* Ask the SIM for its base transfer speed */
2113 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
2114 	cpi.ccb_h.func_code = XPT_PATH_INQ;
2115 	xpt_action((union ccb *)&cpi);
2116 	/* Report connection speed */
2117 	*speed = cpi.base_transfer_speed;
2118 	if (cts->transport == XPORT_ATA) {
2119 		struct	ccb_trans_settings_pata *pata =
2120 		    &cts->xport_specific.ata;
2121 
2122 		if (pata->valid & CTS_ATA_VALID_MODE)
2123 			*speed = ata_mode2speed(pata->mode);
2124 	}
2125 	if (cts->transport == XPORT_SATA) {
2126 		struct	ccb_trans_settings_sata *sata =
2127 		    &cts->xport_specific.sata;
2128 
2129 		if (sata->valid & CTS_SATA_VALID_REVISION)
2130 			*speed = ata_revision2speed(sata->revision);
2131 	}
2132 }
2133 
2134 static void
2135 ata_announce_periph(struct cam_periph *periph)
2136 {
2137 	struct ccb_trans_settings cts;
2138 	u_int speed, mb;
2139 
2140 	_ata_announce_periph(periph, &cts, &speed);
2141 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
2142 		return;
2143 
2144 	mb = speed / 1000;
2145 	if (mb > 0)
2146 		printf("%s%d: %d.%03dMB/s transfers",
2147 		       periph->periph_name, periph->unit_number,
2148 		       mb, speed % 1000);
2149 	else
2150 		printf("%s%d: %dKB/s transfers", periph->periph_name,
2151 		       periph->unit_number, speed);
2152 	/* Report additional information about connection */
2153 	if (cts.transport == XPORT_ATA) {
2154 		struct ccb_trans_settings_pata *pata =
2155 		    &cts.xport_specific.ata;
2156 
2157 		printf(" (");
2158 		if (pata->valid & CTS_ATA_VALID_MODE)
2159 			printf("%s, ", ata_mode2string(pata->mode));
2160 		if ((pata->valid & CTS_ATA_VALID_ATAPI) && pata->atapi != 0)
2161 			printf("ATAPI %dbytes, ", pata->atapi);
2162 		if (pata->valid & CTS_ATA_VALID_BYTECOUNT)
2163 			printf("PIO %dbytes", pata->bytecount);
2164 		printf(")");
2165 	}
2166 	if (cts.transport == XPORT_SATA) {
2167 		struct ccb_trans_settings_sata *sata =
2168 		    &cts.xport_specific.sata;
2169 
2170 		printf(" (");
2171 		if (sata->valid & CTS_SATA_VALID_REVISION)
2172 			printf("SATA %d.x, ", sata->revision);
2173 		else
2174 			printf("SATA, ");
2175 		if (sata->valid & CTS_SATA_VALID_MODE)
2176 			printf("%s, ", ata_mode2string(sata->mode));
2177 		if ((sata->valid & CTS_ATA_VALID_ATAPI) && sata->atapi != 0)
2178 			printf("ATAPI %dbytes, ", sata->atapi);
2179 		if (sata->valid & CTS_SATA_VALID_BYTECOUNT)
2180 			printf("PIO %dbytes", sata->bytecount);
2181 		printf(")");
2182 	}
2183 	printf("\n");
2184 }
2185 
2186 static void
2187 ata_announce_periph_sbuf(struct cam_periph *periph, struct sbuf *sb)
2188 {
2189 	struct ccb_trans_settings cts;
2190 	u_int speed, mb;
2191 
2192 	_ata_announce_periph(periph, &cts, &speed);
2193 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
2194 		return;
2195 
2196 	mb = speed / 1000;
2197 	if (mb > 0)
2198 		sbuf_printf(sb, "%s%d: %d.%03dMB/s transfers",
2199 		       periph->periph_name, periph->unit_number,
2200 		       mb, speed % 1000);
2201 	else
2202 		sbuf_printf(sb, "%s%d: %dKB/s transfers", periph->periph_name,
2203 		       periph->unit_number, speed);
2204 	/* Report additional information about connection */
2205 	if (cts.transport == XPORT_ATA) {
2206 		struct ccb_trans_settings_pata *pata =
2207 		    &cts.xport_specific.ata;
2208 
2209 		sbuf_printf(sb, " (");
2210 		if (pata->valid & CTS_ATA_VALID_MODE)
2211 			sbuf_printf(sb, "%s, ", ata_mode2string(pata->mode));
2212 		if ((pata->valid & CTS_ATA_VALID_ATAPI) && pata->atapi != 0)
2213 			sbuf_printf(sb, "ATAPI %dbytes, ", pata->atapi);
2214 		if (pata->valid & CTS_ATA_VALID_BYTECOUNT)
2215 			sbuf_printf(sb, "PIO %dbytes", pata->bytecount);
2216 		sbuf_printf(sb, ")");
2217 	}
2218 	if (cts.transport == XPORT_SATA) {
2219 		struct ccb_trans_settings_sata *sata =
2220 		    &cts.xport_specific.sata;
2221 
2222 		sbuf_printf(sb, " (");
2223 		if (sata->valid & CTS_SATA_VALID_REVISION)
2224 			sbuf_printf(sb, "SATA %d.x, ", sata->revision);
2225 		else
2226 			sbuf_printf(sb, "SATA, ");
2227 		if (sata->valid & CTS_SATA_VALID_MODE)
2228 			sbuf_printf(sb, "%s, ", ata_mode2string(sata->mode));
2229 		if ((sata->valid & CTS_ATA_VALID_ATAPI) && sata->atapi != 0)
2230 			sbuf_printf(sb, "ATAPI %dbytes, ", sata->atapi);
2231 		if (sata->valid & CTS_SATA_VALID_BYTECOUNT)
2232 			sbuf_printf(sb, "PIO %dbytes", sata->bytecount);
2233 		sbuf_printf(sb, ")");
2234 	}
2235 	sbuf_printf(sb, "\n");
2236 }
2237 
2238 static void
2239 ata_proto_announce_sbuf(struct cam_ed *device, struct sbuf *sb)
2240 {
2241 	ata_print_ident_sbuf(&device->ident_data, sb);
2242 }
2243 
2244 static void
2245 ata_proto_announce(struct cam_ed *device)
2246 {
2247 	ata_print_ident(&device->ident_data);
2248 }
2249 
2250 static void
2251 ata_proto_denounce(struct cam_ed *device)
2252 {
2253 	ata_print_ident_short(&device->ident_data);
2254 }
2255 
2256 static void
2257 ata_proto_denounce_sbuf(struct cam_ed *device, struct sbuf *sb)
2258 {
2259 	ata_print_ident_short_sbuf(&device->ident_data, sb);
2260 }
2261 
2262 static void
2263 semb_proto_announce_sbuf(struct cam_ed *device, struct sbuf *sb)
2264 {
2265 	semb_print_ident_sbuf((struct sep_identify_data *)&device->ident_data, sb);
2266 }
2267 
2268 static void
2269 semb_proto_announce(struct cam_ed *device)
2270 {
2271 	semb_print_ident((struct sep_identify_data *)&device->ident_data);
2272 }
2273 
2274 static void
2275 semb_proto_denounce(struct cam_ed *device)
2276 {
2277 	semb_print_ident_short((struct sep_identify_data *)&device->ident_data);
2278 }
2279 
2280 static void
2281 semb_proto_denounce_sbuf(struct cam_ed *device, struct sbuf *sb)
2282 {
2283 	semb_print_ident_short_sbuf((struct sep_identify_data *)&device->ident_data, sb);
2284 }
2285 
2286 static void
2287 ata_proto_debug_out(union ccb *ccb)
2288 {
2289 	char cdb_str[(sizeof(struct ata_cmd) * 3) + 1];
2290 
2291 	if (ccb->ccb_h.func_code != XPT_ATA_IO)
2292 		return;
2293 
2294 	CAM_DEBUG(ccb->ccb_h.path,
2295 	    CAM_DEBUG_CDB,("%s. ACB: %s\n", ata_op_string(&ccb->ataio.cmd),
2296 		ata_cmd_string(&ccb->ataio.cmd, cdb_str, sizeof(cdb_str))));
2297 }
2298