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