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