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