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