xref: /freebsd/sys/cam/ata/ata_xpt.c (revision e3514747256465c52c3b2aedc9795f52c0d3efe9)
1 /*-
2  * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification, immediately at the beginning of the file.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/bus.h>
32 #include <sys/endian.h>
33 #include <sys/systm.h>
34 #include <sys/types.h>
35 #include <sys/malloc.h>
36 #include <sys/kernel.h>
37 #include <sys/time.h>
38 #include <sys/conf.h>
39 #include <sys/fcntl.h>
40 #include <sys/interrupt.h>
41 #include <sys/sbuf.h>
42 
43 #include <sys/eventhandler.h>
44 #include <sys/lock.h>
45 #include <sys/mutex.h>
46 #include <sys/sysctl.h>
47 
48 #include <cam/cam.h>
49 #include <cam/cam_ccb.h>
50 #include <cam/cam_queue.h>
51 #include <cam/cam_periph.h>
52 #include <cam/cam_sim.h>
53 #include <cam/cam_xpt.h>
54 #include <cam/cam_xpt_sim.h>
55 #include <cam/cam_xpt_periph.h>
56 #include <cam/cam_xpt_internal.h>
57 #include <cam/cam_debug.h>
58 
59 #include <cam/scsi/scsi_all.h>
60 #include <cam/scsi/scsi_message.h>
61 #include <cam/ata/ata_all.h>
62 #include <machine/stdarg.h>	/* for xpt_print below */
63 #include "opt_cam.h"
64 
65 struct ata_quirk_entry {
66 	struct scsi_inquiry_pattern inq_pat;
67 	u_int8_t quirks;
68 #define	CAM_QUIRK_MAXTAGS	0x01
69 	u_int mintags;
70 	u_int maxtags;
71 };
72 
73 static periph_init_t probe_periph_init;
74 
75 static struct periph_driver probe_driver =
76 {
77 	probe_periph_init, "aprobe",
78 	TAILQ_HEAD_INITIALIZER(probe_driver.units), /* generation */ 0,
79 	CAM_PERIPH_DRV_EARLY
80 };
81 
82 PERIPHDRIVER_DECLARE(aprobe, probe_driver);
83 
84 typedef enum {
85 	PROBE_RESET,
86 	PROBE_IDENTIFY,
87 	PROBE_SPINUP,
88 	PROBE_SETMODE,
89 	PROBE_SETPM,
90 	PROBE_SETAPST,
91 	PROBE_SETDMAAA,
92 	PROBE_SETAN,
93 	PROBE_SET_MULTI,
94 	PROBE_INQUIRY,
95 	PROBE_FULL_INQUIRY,
96 	PROBE_PM_PID,
97 	PROBE_PM_PRV,
98 	PROBE_IDENTIFY_SES,
99 	PROBE_IDENTIFY_SAFTE,
100 	PROBE_DONE,
101 	PROBE_INVALID
102 } probe_action;
103 
104 static char *probe_action_text[] = {
105 	"PROBE_RESET",
106 	"PROBE_IDENTIFY",
107 	"PROBE_SPINUP",
108 	"PROBE_SETMODE",
109 	"PROBE_SETPM",
110 	"PROBE_SETAPST",
111 	"PROBE_SETDMAAA",
112 	"PROBE_SETAN",
113 	"PROBE_SET_MULTI",
114 	"PROBE_INQUIRY",
115 	"PROBE_FULL_INQUIRY",
116 	"PROBE_PM_PID",
117 	"PROBE_PM_PRV",
118 	"PROBE_IDENTIFY_SES",
119 	"PROBE_IDENTIFY_SAFTE",
120 	"PROBE_DONE",
121 	"PROBE_INVALID"
122 };
123 
124 #define PROBE_SET_ACTION(softc, newaction)	\
125 do {									\
126 	char **text;							\
127 	text = probe_action_text;					\
128 	CAM_DEBUG((softc)->periph->path, CAM_DEBUG_PROBE,		\
129 	    ("Probe %s to %s\n", text[(softc)->action],			\
130 	    text[(newaction)]));					\
131 	(softc)->action = (newaction);					\
132 } while(0)
133 
134 typedef enum {
135 	PROBE_NO_ANNOUNCE	= 0x04
136 } probe_flags;
137 
138 typedef struct {
139 	TAILQ_HEAD(, ccb_hdr) request_ccbs;
140 	struct ata_params	ident_data;
141 	probe_action	action;
142 	probe_flags	flags;
143 	uint32_t	pm_pid;
144 	uint32_t	pm_prv;
145 	int		restart;
146 	int		spinup;
147 	int		faults;
148 	u_int		caps;
149 	struct cam_periph *periph;
150 } probe_softc;
151 
152 static struct ata_quirk_entry ata_quirk_table[] =
153 {
154 	{
155 		/* Default tagged queuing parameters for all devices */
156 		{
157 		  T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
158 		  /*vendor*/"*", /*product*/"*", /*revision*/"*"
159 		},
160 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
161 	},
162 };
163 
164 static cam_status	proberegister(struct cam_periph *periph,
165 				      void *arg);
166 static void	 probeschedule(struct cam_periph *probe_periph);
167 static void	 probestart(struct cam_periph *periph, union ccb *start_ccb);
168 static void	 proberequestdefaultnegotiation(struct cam_periph *periph);
169 static void	 probedone(struct cam_periph *periph, union ccb *done_ccb);
170 static void	 probecleanup(struct cam_periph *periph);
171 static void	 ata_find_quirk(struct cam_ed *device);
172 static void	 ata_scan_bus(struct cam_periph *periph, union ccb *ccb);
173 static void	 ata_scan_lun(struct cam_periph *periph,
174 			       struct cam_path *path, cam_flags flags,
175 			       union ccb *ccb);
176 static void	 xptscandone(struct cam_periph *periph, union ccb *done_ccb);
177 static struct cam_ed *
178 		 ata_alloc_device(struct cam_eb *bus, struct cam_et *target,
179 				   lun_id_t lun_id);
180 static void	 ata_device_transport(struct cam_path *path);
181 static void	 ata_get_transfer_settings(struct ccb_trans_settings *cts);
182 static void	 ata_set_transfer_settings(struct ccb_trans_settings *cts,
183 					    struct cam_path *path,
184 					    int async_update);
185 static void	 ata_dev_async(u_int32_t async_code,
186 				struct cam_eb *bus,
187 				struct cam_et *target,
188 				struct cam_ed *device,
189 				void *async_arg);
190 static void	 ata_action(union ccb *start_ccb);
191 static void	 ata_announce_periph(struct cam_periph *periph);
192 static void	 ata_announce_periph_sbuf(struct cam_periph *periph, struct sbuf *sb);
193 static void	 ata_proto_announce(struct cam_ed *device);
194 static void	 ata_proto_announce_sbuf(struct cam_ed *device, struct sbuf *sb);
195 static void	 ata_proto_denounce(struct cam_ed *device);
196 static void	 ata_proto_denounce_sbuf(struct cam_ed *device, struct sbuf *sb);
197 static void	 ata_proto_debug_out(union ccb *ccb);
198 static void	 semb_proto_announce(struct cam_ed *device);
199 static void	 semb_proto_announce_sbuf(struct cam_ed *device, struct sbuf *sb);
200 static void	 semb_proto_denounce(struct cam_ed *device);
201 static void	 semb_proto_denounce_sbuf(struct cam_ed *device, struct sbuf *sb);
202 
203 static int ata_dma = 1;
204 static int atapi_dma = 1;
205 
206 TUNABLE_INT("hw.ata.ata_dma", &ata_dma);
207 TUNABLE_INT("hw.ata.atapi_dma", &atapi_dma);
208 
209 static struct xpt_xport_ops ata_xport_ops = {
210 	.alloc_device = ata_alloc_device,
211 	.action = ata_action,
212 	.async = ata_dev_async,
213 	.announce = ata_announce_periph,
214 	.announce_sbuf = ata_announce_periph_sbuf,
215 };
216 #define ATA_XPT_XPORT(x, X)			\
217 static struct xpt_xport ata_xport_ ## x = {	\
218 	.xport = XPORT_ ## X,			\
219 	.name = #x,				\
220 	.ops = &ata_xport_ops,			\
221 };						\
222 CAM_XPT_XPORT(ata_xport_ ## x);
223 
224 ATA_XPT_XPORT(ata, ATA);
225 ATA_XPT_XPORT(sata, SATA);
226 
227 #undef ATA_XPORT_XPORT
228 
229 static struct xpt_proto_ops ata_proto_ops_ata = {
230 	.announce = ata_proto_announce,
231 	.announce_sbuf = ata_proto_announce_sbuf,
232 	.denounce = ata_proto_denounce,
233 	.denounce_sbuf = ata_proto_denounce_sbuf,
234 	.debug_out = ata_proto_debug_out,
235 };
236 static struct xpt_proto ata_proto_ata = {
237 	.proto = PROTO_ATA,
238 	.name = "ata",
239 	.ops = &ata_proto_ops_ata,
240 };
241 
242 static struct xpt_proto_ops ata_proto_ops_satapm = {
243 	.announce = ata_proto_announce,
244 	.announce_sbuf = ata_proto_announce_sbuf,
245 	.denounce = ata_proto_denounce,
246 	.denounce_sbuf = ata_proto_denounce_sbuf,
247 	.debug_out = ata_proto_debug_out,
248 };
249 static struct xpt_proto ata_proto_satapm = {
250 	.proto = PROTO_SATAPM,
251 	.name = "satapm",
252 	.ops = &ata_proto_ops_satapm,
253 };
254 
255 static struct xpt_proto_ops ata_proto_ops_semb = {
256 	.announce = semb_proto_announce,
257 	.announce_sbuf = semb_proto_announce_sbuf,
258 	.denounce = semb_proto_denounce,
259 	.denounce_sbuf = semb_proto_denounce_sbuf,
260 	.debug_out = ata_proto_debug_out,
261 };
262 static struct xpt_proto ata_proto_semb = {
263 	.proto = PROTO_SEMB,
264 	.name = "semb",
265 	.ops = &ata_proto_ops_semb,
266 };
267 
268 CAM_XPT_PROTO(ata_proto_ata);
269 CAM_XPT_PROTO(ata_proto_satapm);
270 CAM_XPT_PROTO(ata_proto_semb);
271 
272 static void
273 probe_periph_init()
274 {
275 }
276 
277 static cam_status
278 proberegister(struct cam_periph *periph, void *arg)
279 {
280 	union ccb *request_ccb;	/* CCB representing the probe request */
281 	cam_status status;
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 	status = cam_periph_acquire(periph);
306 	if (status != CAM_REQ_CMP) {
307 		return (status);
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 		    NULL) == 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 HP SATA disks report supported DMA Auto-Activation,
804 		 * but return ABORT on attempt to enable it.
805 		 */
806 		} else if (softc->action == PROBE_SETDMAAA &&
807 		    status == CAM_ATA_STATUS_ERROR) {
808 			goto noerror;
809 
810 		/*
811 		 * SES and SAF-TE SEPs have different IDENTIFY commands,
812 		 * but SATA specification doesn't tell how to identify them.
813 		 * Until better way found, just try another if first fail.
814 		 */
815 		} else if (softc->action == PROBE_IDENTIFY_SES &&
816 		    status == CAM_ATA_STATUS_ERROR) {
817 			PROBE_SET_ACTION(softc, PROBE_IDENTIFY_SAFTE);
818 			xpt_release_ccb(done_ccb);
819 			xpt_schedule(periph, priority);
820 			goto out;
821 		}
822 
823 		/*
824 		 * If we get to this point, we got an error status back
825 		 * from the inquiry and the error status doesn't require
826 		 * automatically retrying the command.  Therefore, the
827 		 * inquiry failed.  If we had inquiry information before
828 		 * for this device, but this latest inquiry command failed,
829 		 * the device has probably gone away.  If this device isn't
830 		 * already marked unconfigured, notify the peripheral
831 		 * drivers that this device is no more.
832 		 */
833 device_fail:	if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
834 			xpt_async(AC_LOST_DEVICE, path, NULL);
835 		PROBE_SET_ACTION(softc, PROBE_INVALID);
836 		found = 0;
837 		goto done;
838 	}
839 noerror:
840 	if (softc->restart)
841 		goto done;
842 	switch (softc->action) {
843 	case PROBE_RESET:
844 	{
845 		int sign = (done_ccb->ataio.res.lba_high << 8) +
846 		    done_ccb->ataio.res.lba_mid;
847 		CAM_DEBUG(path, CAM_DEBUG_PROBE,
848 		    ("SIGNATURE: %04x\n", sign));
849 		if (sign == 0x0000 &&
850 		    done_ccb->ccb_h.target_id != 15) {
851 			path->device->protocol = PROTO_ATA;
852 			PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
853 		} else if (sign == 0x9669 &&
854 		    done_ccb->ccb_h.target_id == 15) {
855 			/* Report SIM that PM is present. */
856 			bzero(&cts, sizeof(cts));
857 			xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
858 			cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
859 			cts.type = CTS_TYPE_CURRENT_SETTINGS;
860 			cts.xport_specific.sata.pm_present = 1;
861 			cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
862 			xpt_action((union ccb *)&cts);
863 			path->device->protocol = PROTO_SATAPM;
864 			PROBE_SET_ACTION(softc, PROBE_PM_PID);
865 		} else if (sign == 0xc33c &&
866 		    done_ccb->ccb_h.target_id != 15) {
867 			path->device->protocol = PROTO_SEMB;
868 			PROBE_SET_ACTION(softc, PROBE_IDENTIFY_SES);
869 		} else if (sign == 0xeb14 &&
870 		    done_ccb->ccb_h.target_id != 15) {
871 			path->device->protocol = PROTO_SCSI;
872 			PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
873 		} else {
874 			if (done_ccb->ccb_h.target_id != 15) {
875 				xpt_print(path,
876 				    "Unexpected signature 0x%04x\n", sign);
877 			}
878 			goto device_fail;
879 		}
880 		xpt_release_ccb(done_ccb);
881 		xpt_schedule(periph, priority);
882 		goto out;
883 	}
884 	case PROBE_IDENTIFY:
885 	{
886 		struct ccb_pathinq cpi;
887 		int16_t *ptr;
888 		int veto = 0;
889 
890 		ident_buf = &softc->ident_data;
891 		for (ptr = (int16_t *)ident_buf;
892 		     ptr < (int16_t *)ident_buf + sizeof(struct ata_params)/2; ptr++) {
893 			*ptr = le16toh(*ptr);
894 		}
895 
896 		/*
897 		 * Allow others to veto this ATA disk attachment.  This
898 		 * is mainly used by VMs, whose disk controllers may
899 		 * share the disks with the simulated ATA controllers.
900 		 */
901 		EVENTHANDLER_INVOKE(ada_probe_veto, path, ident_buf, &veto);
902 		if (veto) {
903 			goto device_fail;
904 		}
905 
906 		if (strncmp(ident_buf->model, "FX", 2) &&
907 		    strncmp(ident_buf->model, "NEC", 3) &&
908 		    strncmp(ident_buf->model, "Pioneer", 7) &&
909 		    strncmp(ident_buf->model, "SHARP", 5)) {
910 			ata_bswap(ident_buf->model, sizeof(ident_buf->model));
911 			ata_bswap(ident_buf->revision, sizeof(ident_buf->revision));
912 			ata_bswap(ident_buf->serial, sizeof(ident_buf->serial));
913 		}
914 		ata_btrim(ident_buf->model, sizeof(ident_buf->model));
915 		ata_bpack(ident_buf->model, ident_buf->model, sizeof(ident_buf->model));
916 		ata_btrim(ident_buf->revision, sizeof(ident_buf->revision));
917 		ata_bpack(ident_buf->revision, ident_buf->revision, sizeof(ident_buf->revision));
918 		ata_btrim(ident_buf->serial, sizeof(ident_buf->serial));
919 		ata_bpack(ident_buf->serial, ident_buf->serial, sizeof(ident_buf->serial));
920 		/* Device may need spin-up before IDENTIFY become valid. */
921 		if ((ident_buf->specconf == 0x37c8 ||
922 		     ident_buf->specconf == 0x738c) &&
923 		    ((ident_buf->config & ATA_RESP_INCOMPLETE) ||
924 		     softc->spinup == 0)) {
925 			PROBE_SET_ACTION(softc, PROBE_SPINUP);
926 			xpt_release_ccb(done_ccb);
927 			xpt_schedule(periph, priority);
928 			goto out;
929 		}
930 		ident_buf = &path->device->ident_data;
931 		if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
932 			/* Check that it is the same device. */
933 			if (bcmp(softc->ident_data.model, ident_buf->model,
934 			     sizeof(ident_buf->model)) ||
935 			    bcmp(softc->ident_data.revision, ident_buf->revision,
936 			     sizeof(ident_buf->revision)) ||
937 			    bcmp(softc->ident_data.serial, ident_buf->serial,
938 			     sizeof(ident_buf->serial))) {
939 				/* Device changed. */
940 				xpt_async(AC_LOST_DEVICE, path, NULL);
941 			} else {
942 				bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
943 				changed = 0;
944 			}
945 		}
946 		if (changed) {
947 			bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
948 			/* Clean up from previous instance of this device */
949 			if (path->device->serial_num != NULL) {
950 				free(path->device->serial_num, M_CAMXPT);
951 				path->device->serial_num = NULL;
952 				path->device->serial_num_len = 0;
953 			}
954 			if (path->device->device_id != NULL) {
955 				free(path->device->device_id, M_CAMXPT);
956 				path->device->device_id = NULL;
957 				path->device->device_id_len = 0;
958 			}
959 			path->device->serial_num =
960 				(u_int8_t *)malloc((sizeof(ident_buf->serial) + 1),
961 					   M_CAMXPT, M_NOWAIT);
962 			if (path->device->serial_num != NULL) {
963 				bcopy(ident_buf->serial,
964 				      path->device->serial_num,
965 				      sizeof(ident_buf->serial));
966 				path->device->serial_num[sizeof(ident_buf->serial)]
967 				    = '\0';
968 				path->device->serial_num_len =
969 				    strlen(path->device->serial_num);
970 			}
971 			if (ident_buf->enabled.extension &
972 			    ATA_SUPPORT_64BITWWN) {
973 				path->device->device_id =
974 				    malloc(16, M_CAMXPT, M_NOWAIT);
975 				if (path->device->device_id != NULL) {
976 					path->device->device_id_len = 16;
977 					bcopy(&fake_device_id_hdr,
978 					    path->device->device_id, 8);
979 					bcopy(ident_buf->wwn,
980 					    path->device->device_id + 8, 8);
981 					ata_bswap(path->device->device_id + 8, 8);
982 				}
983 			}
984 
985 			path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
986 			xpt_async(AC_GETDEV_CHANGED, path, NULL);
987 		}
988 		if (ident_buf->satacapabilities & ATA_SUPPORT_NCQ) {
989 			path->device->mintags = 2;
990 			path->device->maxtags =
991 			    ATA_QUEUE_LEN(ident_buf->queue) + 1;
992 		}
993 		ata_find_quirk(path->device);
994 		if (path->device->mintags != 0 &&
995 		    path->bus->sim->max_tagged_dev_openings != 0) {
996 			/* Check if the SIM does not want queued commands. */
997 			bzero(&cpi, sizeof(cpi));
998 			xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
999 			cpi.ccb_h.func_code = XPT_PATH_INQ;
1000 			xpt_action((union ccb *)&cpi);
1001 			if (cpi.ccb_h.status == CAM_REQ_CMP &&
1002 			    (cpi.hba_inquiry & PI_TAG_ABLE)) {
1003 				/* Report SIM which tags are allowed. */
1004 				bzero(&cts, sizeof(cts));
1005 				xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1006 				cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1007 				cts.type = CTS_TYPE_CURRENT_SETTINGS;
1008 				cts.xport_specific.sata.tags = path->device->maxtags;
1009 				cts.xport_specific.sata.valid = CTS_SATA_VALID_TAGS;
1010 				xpt_action((union ccb *)&cts);
1011 			}
1012 		}
1013 		ata_device_transport(path);
1014 		if (changed)
1015 			proberequestdefaultnegotiation(periph);
1016 		PROBE_SET_ACTION(softc, PROBE_SETMODE);
1017 		xpt_release_ccb(done_ccb);
1018 		xpt_schedule(periph, priority);
1019 		goto out;
1020 	}
1021 	case PROBE_SPINUP:
1022 		if (bootverbose)
1023 			xpt_print(path, "Spin-up done\n");
1024 		softc->spinup = 1;
1025 		PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
1026 		xpt_release_ccb(done_ccb);
1027 		xpt_schedule(periph, priority);
1028 		goto out;
1029 	case PROBE_SETMODE:
1030 		/* Set supported bits. */
1031 		bzero(&cts, sizeof(cts));
1032 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1033 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1034 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
1035 		xpt_action((union ccb *)&cts);
1036 		if (path->device->transport == XPORT_SATA &&
1037 		    cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1038 			caps = cts.xport_specific.sata.caps & CTS_SATA_CAPS_H;
1039 		else if (path->device->transport == XPORT_ATA &&
1040 		    cts.xport_specific.ata.valid & CTS_ATA_VALID_CAPS)
1041 			caps = cts.xport_specific.ata.caps & CTS_ATA_CAPS_H;
1042 		else
1043 			caps = 0;
1044 		if (path->device->transport == XPORT_SATA &&
1045 		    ident_buf->satacapabilities != 0xffff) {
1046 			if (ident_buf->satacapabilities & ATA_SUPPORT_IFPWRMNGTRCV)
1047 				caps |= CTS_SATA_CAPS_D_PMREQ;
1048 			if (ident_buf->satacapabilities & ATA_SUPPORT_HAPST)
1049 				caps |= CTS_SATA_CAPS_D_APST;
1050 		}
1051 		/* Mask unwanted bits. */
1052 		bzero(&cts, sizeof(cts));
1053 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1054 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1055 		cts.type = CTS_TYPE_USER_SETTINGS;
1056 		xpt_action((union ccb *)&cts);
1057 		if (path->device->transport == XPORT_SATA &&
1058 		    cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1059 			caps &= cts.xport_specific.sata.caps;
1060 		else if (path->device->transport == XPORT_ATA &&
1061 		    cts.xport_specific.ata.valid & CTS_ATA_VALID_CAPS)
1062 			caps &= cts.xport_specific.ata.caps;
1063 		else
1064 			caps = 0;
1065 		/*
1066 		 * Remember what transport thinks about 48-bit DMA.  If
1067 		 * capability information is not provided or transport is
1068 		 * SATA, we take support for granted.
1069 		 */
1070 		if (!(path->device->inq_flags & SID_DMA) ||
1071 		    (path->device->transport == XPORT_ATA &&
1072 		    (cts.xport_specific.ata.valid & CTS_ATA_VALID_CAPS) &&
1073 		    !(caps & CTS_ATA_CAPS_H_DMA48)))
1074 			path->device->inq_flags &= ~SID_DMA48;
1075 		else
1076 			path->device->inq_flags |= SID_DMA48;
1077 		/* Store result to SIM. */
1078 		bzero(&cts, sizeof(cts));
1079 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1080 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1081 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
1082 		if (path->device->transport == XPORT_SATA) {
1083 			cts.xport_specific.sata.caps = caps;
1084 			cts.xport_specific.sata.valid = CTS_SATA_VALID_CAPS;
1085 		} else {
1086 			cts.xport_specific.ata.caps = caps;
1087 			cts.xport_specific.ata.valid = CTS_ATA_VALID_CAPS;
1088 		}
1089 		xpt_action((union ccb *)&cts);
1090 		softc->caps = caps;
1091 		if (path->device->transport != XPORT_SATA)
1092 			goto notsata;
1093 		if ((ident_buf->satasupport & ATA_SUPPORT_IFPWRMNGT) &&
1094 		    (!(softc->caps & CTS_SATA_CAPS_H_PMREQ)) !=
1095 		    (!(ident_buf->sataenabled & ATA_SUPPORT_IFPWRMNGT))) {
1096 			PROBE_SET_ACTION(softc, PROBE_SETPM);
1097 			xpt_release_ccb(done_ccb);
1098 			xpt_schedule(periph, priority);
1099 			goto out;
1100 		}
1101 		/* FALLTHROUGH */
1102 	case PROBE_SETPM:
1103 		if (ident_buf->satacapabilities != 0xffff &&
1104 		    (ident_buf->satacapabilities & ATA_SUPPORT_DAPST) &&
1105 		    (!(softc->caps & CTS_SATA_CAPS_H_APST)) !=
1106 		    (!(ident_buf->sataenabled & ATA_ENABLED_DAPST))) {
1107 			PROBE_SET_ACTION(softc, PROBE_SETAPST);
1108 			xpt_release_ccb(done_ccb);
1109 			xpt_schedule(periph, priority);
1110 			goto out;
1111 		}
1112 		/* FALLTHROUGH */
1113 	case PROBE_SETAPST:
1114 		if ((ident_buf->satasupport & ATA_SUPPORT_AUTOACTIVATE) &&
1115 		    (!(softc->caps & CTS_SATA_CAPS_H_DMAAA)) !=
1116 		    (!(ident_buf->sataenabled & ATA_SUPPORT_AUTOACTIVATE))) {
1117 			PROBE_SET_ACTION(softc, PROBE_SETDMAAA);
1118 			xpt_release_ccb(done_ccb);
1119 			xpt_schedule(periph, priority);
1120 			goto out;
1121 		}
1122 		/* FALLTHROUGH */
1123 	case PROBE_SETDMAAA:
1124 		if (path->device->protocol != PROTO_ATA &&
1125 		    (ident_buf->satasupport & ATA_SUPPORT_ASYNCNOTIF) &&
1126 		    (!(softc->caps & CTS_SATA_CAPS_H_AN)) !=
1127 		    (!(ident_buf->sataenabled & ATA_SUPPORT_ASYNCNOTIF))) {
1128 			PROBE_SET_ACTION(softc, PROBE_SETAN);
1129 			xpt_release_ccb(done_ccb);
1130 			xpt_schedule(periph, priority);
1131 			goto out;
1132 		}
1133 		/* FALLTHROUGH */
1134 	case PROBE_SETAN:
1135 notsata:
1136 		if (path->device->protocol == PROTO_ATA) {
1137 			PROBE_SET_ACTION(softc, PROBE_SET_MULTI);
1138 		} else {
1139 			PROBE_SET_ACTION(softc, PROBE_INQUIRY);
1140 		}
1141 		xpt_release_ccb(done_ccb);
1142 		xpt_schedule(periph, priority);
1143 		goto out;
1144 	case PROBE_SET_MULTI:
1145 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1146 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1147 			xpt_acquire_device(path->device);
1148 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1149 			xpt_action(done_ccb);
1150 			xpt_async(AC_FOUND_DEVICE, path, done_ccb);
1151 		}
1152 		PROBE_SET_ACTION(softc, PROBE_DONE);
1153 		break;
1154 	case PROBE_INQUIRY:
1155 	case PROBE_FULL_INQUIRY:
1156 	{
1157 		u_int8_t periph_qual, len;
1158 
1159 		path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
1160 
1161 		periph_qual = SID_QUAL(inq_buf);
1162 
1163 		if (periph_qual != SID_QUAL_LU_CONNECTED &&
1164 		    periph_qual != SID_QUAL_LU_OFFLINE)
1165 			break;
1166 
1167 		/*
1168 		 * We conservatively request only
1169 		 * SHORT_INQUIRY_LEN bytes of inquiry
1170 		 * information during our first try
1171 		 * at sending an INQUIRY. If the device
1172 		 * has more information to give,
1173 		 * perform a second request specifying
1174 		 * the amount of information the device
1175 		 * is willing to give.
1176 		 */
1177 		len = inq_buf->additional_length
1178 		    + offsetof(struct scsi_inquiry_data, additional_length) + 1;
1179 		if (softc->action == PROBE_INQUIRY
1180 		    && len > SHORT_INQUIRY_LENGTH) {
1181 			PROBE_SET_ACTION(softc, PROBE_FULL_INQUIRY);
1182 			xpt_release_ccb(done_ccb);
1183 			xpt_schedule(periph, priority);
1184 			goto out;
1185 		}
1186 
1187 		ata_device_transport(path);
1188 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1189 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1190 			xpt_acquire_device(path->device);
1191 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1192 			xpt_action(done_ccb);
1193 			xpt_async(AC_FOUND_DEVICE, path, done_ccb);
1194 		}
1195 		PROBE_SET_ACTION(softc, PROBE_DONE);
1196 		break;
1197 	}
1198 	case PROBE_PM_PID:
1199 		if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) == 0)
1200 			bzero(ident_buf, sizeof(*ident_buf));
1201 		softc->pm_pid = (done_ccb->ataio.res.lba_high << 24) +
1202 		    (done_ccb->ataio.res.lba_mid << 16) +
1203 		    (done_ccb->ataio.res.lba_low << 8) +
1204 		    done_ccb->ataio.res.sector_count;
1205 		((uint32_t *)ident_buf)[0] = softc->pm_pid;
1206 		snprintf(ident_buf->model, sizeof(ident_buf->model),
1207 		    "Port Multiplier %08x", softc->pm_pid);
1208 		PROBE_SET_ACTION(softc, PROBE_PM_PRV);
1209 		xpt_release_ccb(done_ccb);
1210 		xpt_schedule(periph, priority);
1211 		goto out;
1212 	case PROBE_PM_PRV:
1213 		softc->pm_prv = (done_ccb->ataio.res.lba_high << 24) +
1214 		    (done_ccb->ataio.res.lba_mid << 16) +
1215 		    (done_ccb->ataio.res.lba_low << 8) +
1216 		    done_ccb->ataio.res.sector_count;
1217 		((uint32_t *)ident_buf)[1] = softc->pm_prv;
1218 		snprintf(ident_buf->revision, sizeof(ident_buf->revision),
1219 		    "%04x", softc->pm_prv);
1220 		path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
1221 		ata_device_transport(path);
1222 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED)
1223 			proberequestdefaultnegotiation(periph);
1224 		/* Set supported bits. */
1225 		bzero(&cts, sizeof(cts));
1226 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1227 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1228 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
1229 		xpt_action((union ccb *)&cts);
1230 		if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1231 			caps = cts.xport_specific.sata.caps & CTS_SATA_CAPS_H;
1232 		else
1233 			caps = 0;
1234 		/* All PMPs must support PM requests. */
1235 		caps |= CTS_SATA_CAPS_D_PMREQ;
1236 		/* Mask unwanted bits. */
1237 		bzero(&cts, sizeof(cts));
1238 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1239 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1240 		cts.type = CTS_TYPE_USER_SETTINGS;
1241 		xpt_action((union ccb *)&cts);
1242 		if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1243 			caps &= cts.xport_specific.sata.caps;
1244 		else
1245 			caps = 0;
1246 		/* Remember what transport thinks about AEN. */
1247 		if ((caps & CTS_SATA_CAPS_H_AN) && path->device->protocol != PROTO_ATA)
1248 			path->device->inq_flags |= SID_AEN;
1249 		else
1250 			path->device->inq_flags &= ~SID_AEN;
1251 		/* Store result to SIM. */
1252 		bzero(&cts, sizeof(cts));
1253 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1254 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1255 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
1256 		cts.xport_specific.sata.caps = caps;
1257 		cts.xport_specific.sata.valid = CTS_SATA_VALID_CAPS;
1258 		xpt_action((union ccb *)&cts);
1259 		softc->caps = caps;
1260 		xpt_async(AC_GETDEV_CHANGED, path, NULL);
1261 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1262 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1263 			xpt_acquire_device(path->device);
1264 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1265 			xpt_action(done_ccb);
1266 			xpt_async(AC_FOUND_DEVICE, path, done_ccb);
1267 		} else {
1268 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1269 			xpt_action(done_ccb);
1270 			xpt_async(AC_SCSI_AEN, path, done_ccb);
1271 		}
1272 		PROBE_SET_ACTION(softc, PROBE_DONE);
1273 		break;
1274 	case PROBE_IDENTIFY_SES:
1275 	case PROBE_IDENTIFY_SAFTE:
1276 		if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
1277 			/* Check that it is the same device. */
1278 			if (bcmp(&softc->ident_data, ident_buf, 53)) {
1279 				/* Device changed. */
1280 				xpt_async(AC_LOST_DEVICE, path, NULL);
1281 			} else {
1282 				bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
1283 				changed = 0;
1284 			}
1285 		}
1286 		if (changed) {
1287 			bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
1288 			/* Clean up from previous instance of this device */
1289 			if (path->device->device_id != NULL) {
1290 				free(path->device->device_id, M_CAMXPT);
1291 				path->device->device_id = NULL;
1292 				path->device->device_id_len = 0;
1293 			}
1294 			path->device->device_id =
1295 			    malloc(16, M_CAMXPT, M_NOWAIT);
1296 			if (path->device->device_id != NULL) {
1297 				path->device->device_id_len = 16;
1298 				bcopy(&fake_device_id_hdr,
1299 				    path->device->device_id, 8);
1300 				bcopy(((uint8_t*)ident_buf) + 2,
1301 				    path->device->device_id + 8, 8);
1302 			}
1303 
1304 			path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
1305 		}
1306 		ata_device_transport(path);
1307 		if (changed)
1308 			proberequestdefaultnegotiation(periph);
1309 
1310 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1311 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1312 			xpt_acquire_device(path->device);
1313 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1314 			xpt_action(done_ccb);
1315 			xpt_async(AC_FOUND_DEVICE, path, done_ccb);
1316 		}
1317 		PROBE_SET_ACTION(softc, PROBE_DONE);
1318 		break;
1319 	default:
1320 		panic("probedone: invalid action state 0x%x\n", softc->action);
1321 	}
1322 done:
1323 	if (softc->restart) {
1324 		softc->restart = 0;
1325 		xpt_release_ccb(done_ccb);
1326 		probeschedule(periph);
1327 		goto out;
1328 	}
1329 	xpt_release_ccb(done_ccb);
1330 	CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe completed\n"));
1331 	while ((done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs))) {
1332 		TAILQ_REMOVE(&softc->request_ccbs,
1333 		    &done_ccb->ccb_h, periph_links.tqe);
1334 		done_ccb->ccb_h.status = found ? CAM_REQ_CMP : CAM_REQ_CMP_ERR;
1335 		xpt_done(done_ccb);
1336 	}
1337 	/* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
1338 	cam_release_devq(path, 0, 0, 0, FALSE);
1339 	cam_periph_invalidate(periph);
1340 	cam_periph_release_locked(periph);
1341 }
1342 
1343 static void
1344 probecleanup(struct cam_periph *periph)
1345 {
1346 	free(periph->softc, M_CAMXPT);
1347 }
1348 
1349 static void
1350 ata_find_quirk(struct cam_ed *device)
1351 {
1352 	struct ata_quirk_entry *quirk;
1353 	caddr_t	match;
1354 
1355 	match = cam_quirkmatch((caddr_t)&device->ident_data,
1356 			       (caddr_t)ata_quirk_table,
1357 			       nitems(ata_quirk_table),
1358 			       sizeof(*ata_quirk_table), ata_identify_match);
1359 
1360 	if (match == NULL)
1361 		panic("xpt_find_quirk: device didn't match wildcard entry!!");
1362 
1363 	quirk = (struct ata_quirk_entry *)match;
1364 	device->quirk = quirk;
1365 	if (quirk->quirks & CAM_QUIRK_MAXTAGS) {
1366 		device->mintags = quirk->mintags;
1367 		device->maxtags = quirk->maxtags;
1368 	}
1369 }
1370 
1371 typedef struct {
1372 	union	ccb *request_ccb;
1373 	struct 	ccb_pathinq *cpi;
1374 	int	counter;
1375 } ata_scan_bus_info;
1376 
1377 /*
1378  * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
1379  * As the scan progresses, xpt_scan_bus is used as the
1380  * callback on completion function.
1381  */
1382 static void
1383 ata_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
1384 {
1385 	struct	cam_path *path;
1386 	ata_scan_bus_info *scan_info;
1387 	union	ccb *work_ccb, *reset_ccb;
1388 	struct mtx *mtx;
1389 	cam_status status;
1390 
1391 	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
1392 		  ("xpt_scan_bus\n"));
1393 	switch (request_ccb->ccb_h.func_code) {
1394 	case XPT_SCAN_BUS:
1395 	case XPT_SCAN_TGT:
1396 		/* Find out the characteristics of the bus */
1397 		work_ccb = xpt_alloc_ccb_nowait();
1398 		if (work_ccb == NULL) {
1399 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1400 			xpt_done(request_ccb);
1401 			return;
1402 		}
1403 		xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
1404 			      request_ccb->ccb_h.pinfo.priority);
1405 		work_ccb->ccb_h.func_code = XPT_PATH_INQ;
1406 		xpt_action(work_ccb);
1407 		if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
1408 			request_ccb->ccb_h.status = work_ccb->ccb_h.status;
1409 			xpt_free_ccb(work_ccb);
1410 			xpt_done(request_ccb);
1411 			return;
1412 		}
1413 
1414 		/* We may need to reset bus first, if we haven't done it yet. */
1415 		if ((work_ccb->cpi.hba_inquiry &
1416 		    (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) &&
1417 		    !(work_ccb->cpi.hba_misc & PIM_NOBUSRESET) &&
1418 		    !timevalisset(&request_ccb->ccb_h.path->bus->last_reset)) {
1419 			reset_ccb = xpt_alloc_ccb_nowait();
1420 			if (reset_ccb == NULL) {
1421 				request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1422 				xpt_free_ccb(work_ccb);
1423 				xpt_done(request_ccb);
1424 				return;
1425 			}
1426 			xpt_setup_ccb(&reset_ccb->ccb_h, request_ccb->ccb_h.path,
1427 			      CAM_PRIORITY_NONE);
1428 			reset_ccb->ccb_h.func_code = XPT_RESET_BUS;
1429 			xpt_action(reset_ccb);
1430 			if (reset_ccb->ccb_h.status != CAM_REQ_CMP) {
1431 				request_ccb->ccb_h.status = reset_ccb->ccb_h.status;
1432 				xpt_free_ccb(reset_ccb);
1433 				xpt_free_ccb(work_ccb);
1434 				xpt_done(request_ccb);
1435 				return;
1436 			}
1437 			xpt_free_ccb(reset_ccb);
1438 		}
1439 
1440 		/* Save some state for use while we probe for devices */
1441 		scan_info = (ata_scan_bus_info *)
1442 		    malloc(sizeof(ata_scan_bus_info), M_CAMXPT, M_NOWAIT);
1443 		if (scan_info == NULL) {
1444 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1445 			xpt_free_ccb(work_ccb);
1446 			xpt_done(request_ccb);
1447 			return;
1448 		}
1449 		scan_info->request_ccb = request_ccb;
1450 		scan_info->cpi = &work_ccb->cpi;
1451 		/* If PM supported, probe it first. */
1452 		if (scan_info->cpi->hba_inquiry & PI_SATAPM)
1453 			scan_info->counter = scan_info->cpi->max_target;
1454 		else
1455 			scan_info->counter = 0;
1456 
1457 		work_ccb = xpt_alloc_ccb_nowait();
1458 		if (work_ccb == NULL) {
1459 			free(scan_info, M_CAMXPT);
1460 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1461 			xpt_done(request_ccb);
1462 			break;
1463 		}
1464 		mtx = xpt_path_mtx(scan_info->request_ccb->ccb_h.path);
1465 		goto scan_next;
1466 	case XPT_SCAN_LUN:
1467 		work_ccb = request_ccb;
1468 		/* Reuse the same CCB to query if a device was really found */
1469 		scan_info = (ata_scan_bus_info *)work_ccb->ccb_h.ppriv_ptr0;
1470 		mtx = xpt_path_mtx(scan_info->request_ccb->ccb_h.path);
1471 		mtx_lock(mtx);
1472 		/* If there is PMP... */
1473 		if ((scan_info->cpi->hba_inquiry & PI_SATAPM) &&
1474 		    (scan_info->counter == scan_info->cpi->max_target)) {
1475 			if (work_ccb->ccb_h.status == CAM_REQ_CMP) {
1476 				/* everything else will be probed by it */
1477 				/* Free the current request path- we're done with it. */
1478 				xpt_free_path(work_ccb->ccb_h.path);
1479 				goto done;
1480 			} else {
1481 				struct ccb_trans_settings cts;
1482 
1483 				/* Report SIM that PM is absent. */
1484 				bzero(&cts, sizeof(cts));
1485 				xpt_setup_ccb(&cts.ccb_h,
1486 				    work_ccb->ccb_h.path, CAM_PRIORITY_NONE);
1487 				cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1488 				cts.type = CTS_TYPE_CURRENT_SETTINGS;
1489 				cts.xport_specific.sata.pm_present = 0;
1490 				cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
1491 				xpt_action((union ccb *)&cts);
1492 			}
1493 		}
1494 		/* Free the current request path- we're done with it. */
1495 		xpt_free_path(work_ccb->ccb_h.path);
1496 		if (scan_info->counter ==
1497 		    ((scan_info->cpi->hba_inquiry & PI_SATAPM) ?
1498 		    0 : scan_info->cpi->max_target)) {
1499 done:
1500 			mtx_unlock(mtx);
1501 			xpt_free_ccb(work_ccb);
1502 			xpt_free_ccb((union ccb *)scan_info->cpi);
1503 			request_ccb = scan_info->request_ccb;
1504 			free(scan_info, M_CAMXPT);
1505 			request_ccb->ccb_h.status = CAM_REQ_CMP;
1506 			xpt_done(request_ccb);
1507 			break;
1508 		}
1509 		/* Take next device. Wrap from max (PMP) to 0. */
1510 		scan_info->counter = (scan_info->counter + 1 ) %
1511 		    (scan_info->cpi->max_target + 1);
1512 scan_next:
1513 		status = xpt_create_path(&path, NULL,
1514 		    scan_info->request_ccb->ccb_h.path_id,
1515 		    scan_info->counter, 0);
1516 		if (status != CAM_REQ_CMP) {
1517 			if (request_ccb->ccb_h.func_code == XPT_SCAN_LUN)
1518 				mtx_unlock(mtx);
1519 			printf("xpt_scan_bus: xpt_create_path failed"
1520 			    " with status %#x, bus scan halted\n",
1521 			    status);
1522 			xpt_free_ccb(work_ccb);
1523 			xpt_free_ccb((union ccb *)scan_info->cpi);
1524 			request_ccb = scan_info->request_ccb;
1525 			free(scan_info, M_CAMXPT);
1526 			request_ccb->ccb_h.status = status;
1527 			xpt_done(request_ccb);
1528 			break;
1529 		}
1530 		xpt_setup_ccb(&work_ccb->ccb_h, path,
1531 		    scan_info->request_ccb->ccb_h.pinfo.priority);
1532 		work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1533 		work_ccb->ccb_h.cbfcnp = ata_scan_bus;
1534 		work_ccb->ccb_h.flags |= CAM_UNLOCKED;
1535 		work_ccb->ccb_h.ppriv_ptr0 = scan_info;
1536 		work_ccb->crcn.flags = scan_info->request_ccb->crcn.flags;
1537 		mtx_unlock(mtx);
1538 		if (request_ccb->ccb_h.func_code == XPT_SCAN_LUN)
1539 			mtx = NULL;
1540 		xpt_action(work_ccb);
1541 		if (mtx != NULL)
1542 			mtx_lock(mtx);
1543 		break;
1544 	default:
1545 		break;
1546 	}
1547 }
1548 
1549 static void
1550 ata_scan_lun(struct cam_periph *periph, struct cam_path *path,
1551 	     cam_flags flags, union ccb *request_ccb)
1552 {
1553 	struct ccb_pathinq cpi;
1554 	cam_status status;
1555 	struct cam_path *new_path;
1556 	struct cam_periph *old_periph;
1557 	int lock;
1558 
1559 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_scan_lun\n"));
1560 
1561 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
1562 	cpi.ccb_h.func_code = XPT_PATH_INQ;
1563 	xpt_action((union ccb *)&cpi);
1564 
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_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
1674 	cpi.ccb_h.func_code = XPT_PATH_INQ;
1675 	xpt_action((union ccb *)&cpi);
1676 
1677 	path->device->transport = cpi.transport;
1678 	if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
1679 		inq_buf = &path->device->inq_data;
1680 	if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) != 0)
1681 		ident_buf = &path->device->ident_data;
1682 	if (path->device->protocol == PROTO_ATA) {
1683 		path->device->protocol_version = ident_buf ?
1684 		    ata_version(ident_buf->version_major) : cpi.protocol_version;
1685 	} else if (path->device->protocol == PROTO_SCSI) {
1686 		path->device->protocol_version = inq_buf ?
1687 		    SID_ANSI_REV(inq_buf) : cpi.protocol_version;
1688 	}
1689 	path->device->transport_version = ident_buf ?
1690 	    ata_version(ident_buf->version_major) : cpi.transport_version;
1691 
1692 	/* Tell the controller what we think */
1693 	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1694 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1695 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
1696 	cts.transport = path->device->transport;
1697 	cts.transport_version = path->device->transport_version;
1698 	cts.protocol = path->device->protocol;
1699 	cts.protocol_version = path->device->protocol_version;
1700 	cts.proto_specific.valid = 0;
1701 	if (ident_buf) {
1702 		if (path->device->transport == XPORT_ATA) {
1703 			cts.xport_specific.ata.atapi =
1704 			    (ident_buf->config == ATA_PROTO_CFA) ? 0 :
1705 			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 :
1706 			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0;
1707 			cts.xport_specific.ata.valid = CTS_ATA_VALID_ATAPI;
1708 		} else {
1709 			cts.xport_specific.sata.atapi =
1710 			    (ident_buf->config == ATA_PROTO_CFA) ? 0 :
1711 			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 :
1712 			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0;
1713 			cts.xport_specific.sata.valid = CTS_SATA_VALID_ATAPI;
1714 		}
1715 	} else
1716 		cts.xport_specific.valid = 0;
1717 	xpt_action((union ccb *)&cts);
1718 }
1719 
1720 static void
1721 ata_dev_advinfo(union ccb *start_ccb)
1722 {
1723 	struct cam_ed *device;
1724 	struct ccb_dev_advinfo *cdai;
1725 	off_t amt;
1726 
1727 	start_ccb->ccb_h.status = CAM_REQ_INVALID;
1728 	device = start_ccb->ccb_h.path->device;
1729 	cdai = &start_ccb->cdai;
1730 	switch(cdai->buftype) {
1731 	case CDAI_TYPE_SCSI_DEVID:
1732 		if (cdai->flags & CDAI_FLAG_STORE)
1733 			return;
1734 		cdai->provsiz = device->device_id_len;
1735 		if (device->device_id_len == 0)
1736 			break;
1737 		amt = device->device_id_len;
1738 		if (cdai->provsiz > cdai->bufsiz)
1739 			amt = cdai->bufsiz;
1740 		memcpy(cdai->buf, device->device_id, amt);
1741 		break;
1742 	case CDAI_TYPE_SERIAL_NUM:
1743 		if (cdai->flags & CDAI_FLAG_STORE)
1744 			return;
1745 		cdai->provsiz = device->serial_num_len;
1746 		if (device->serial_num_len == 0)
1747 			break;
1748 		amt = device->serial_num_len;
1749 		if (cdai->provsiz > cdai->bufsiz)
1750 			amt = cdai->bufsiz;
1751 		memcpy(cdai->buf, device->serial_num, amt);
1752 		break;
1753 	case CDAI_TYPE_PHYS_PATH:
1754 		if (cdai->flags & CDAI_FLAG_STORE) {
1755 			if (device->physpath != NULL)
1756 				free(device->physpath, M_CAMXPT);
1757 			device->physpath_len = cdai->bufsiz;
1758 			/* Clear existing buffer if zero length */
1759 			if (cdai->bufsiz == 0)
1760 				break;
1761 			device->physpath = malloc(cdai->bufsiz, M_CAMXPT, M_NOWAIT);
1762 			if (device->physpath == NULL) {
1763 				start_ccb->ccb_h.status = CAM_REQ_ABORTED;
1764 				return;
1765 			}
1766 			memcpy(device->physpath, cdai->buf, cdai->bufsiz);
1767 		} else {
1768 			cdai->provsiz = device->physpath_len;
1769 			if (device->physpath_len == 0)
1770 				break;
1771 			amt = device->physpath_len;
1772 			if (cdai->provsiz > cdai->bufsiz)
1773 				amt = cdai->bufsiz;
1774 			memcpy(cdai->buf, device->physpath, amt);
1775 		}
1776 		break;
1777 	default:
1778 		return;
1779 	}
1780 	start_ccb->ccb_h.status = CAM_REQ_CMP;
1781 
1782 	if (cdai->flags & CDAI_FLAG_STORE) {
1783 		xpt_async(AC_ADVINFO_CHANGED, start_ccb->ccb_h.path,
1784 			  (void *)(uintptr_t)cdai->buftype);
1785 	}
1786 }
1787 
1788 static void
1789 ata_action(union ccb *start_ccb)
1790 {
1791 
1792 	switch (start_ccb->ccb_h.func_code) {
1793 	case XPT_SET_TRAN_SETTINGS:
1794 	{
1795 		ata_set_transfer_settings(&start_ccb->cts,
1796 					   start_ccb->ccb_h.path,
1797 					   /*async_update*/FALSE);
1798 		break;
1799 	}
1800 	case XPT_SCAN_BUS:
1801 	case XPT_SCAN_TGT:
1802 		ata_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
1803 		break;
1804 	case XPT_SCAN_LUN:
1805 		ata_scan_lun(start_ccb->ccb_h.path->periph,
1806 			      start_ccb->ccb_h.path, start_ccb->crcn.flags,
1807 			      start_ccb);
1808 		break;
1809 	case XPT_GET_TRAN_SETTINGS:
1810 	{
1811 		ata_get_transfer_settings(&start_ccb->cts);
1812 		break;
1813 	}
1814 	case XPT_SCSI_IO:
1815 	{
1816 		struct cam_ed *device;
1817 		u_int	maxlen = 0;
1818 
1819 		device = start_ccb->ccb_h.path->device;
1820 		if (device->protocol == PROTO_SCSI &&
1821 		    (device->flags & CAM_DEV_IDENTIFY_DATA_VALID)) {
1822 			uint16_t p =
1823 			    device->ident_data.config & ATA_PROTO_MASK;
1824 
1825 			maxlen =
1826 			    (device->ident_data.config == ATA_PROTO_CFA) ? 0 :
1827 			    (p == ATA_PROTO_ATAPI_16) ? 16 :
1828 			    (p == ATA_PROTO_ATAPI_12) ? 12 : 0;
1829 		}
1830 		if (start_ccb->csio.cdb_len > maxlen) {
1831 			start_ccb->ccb_h.status = CAM_REQ_INVALID;
1832 			xpt_done(start_ccb);
1833 			break;
1834 		}
1835 		xpt_action_default(start_ccb);
1836 		break;
1837 	}
1838 	case XPT_DEV_ADVINFO:
1839 	{
1840 		ata_dev_advinfo(start_ccb);
1841 		break;
1842 	}
1843 	default:
1844 		xpt_action_default(start_ccb);
1845 		break;
1846 	}
1847 }
1848 
1849 static void
1850 ata_get_transfer_settings(struct ccb_trans_settings *cts)
1851 {
1852 	struct	ccb_trans_settings_ata *ata;
1853 	struct	ccb_trans_settings_scsi *scsi;
1854 	struct	cam_ed *device;
1855 
1856 	device = cts->ccb_h.path->device;
1857 	xpt_action_default((union ccb *)cts);
1858 
1859 	if (cts->protocol == PROTO_UNKNOWN ||
1860 	    cts->protocol == PROTO_UNSPECIFIED) {
1861 		cts->protocol = device->protocol;
1862 		cts->protocol_version = device->protocol_version;
1863 	}
1864 
1865 	if (cts->protocol == PROTO_ATA) {
1866 		ata = &cts->proto_specific.ata;
1867 		if ((ata->valid & CTS_ATA_VALID_TQ) == 0) {
1868 			ata->valid |= CTS_ATA_VALID_TQ;
1869 			if (cts->type == CTS_TYPE_USER_SETTINGS ||
1870 			    (device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0 ||
1871 			    (device->inq_flags & SID_CmdQue) != 0)
1872 				ata->flags |= CTS_ATA_FLAGS_TAG_ENB;
1873 		}
1874 	}
1875 	if (cts->protocol == PROTO_SCSI) {
1876 		scsi = &cts->proto_specific.scsi;
1877 		if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
1878 			scsi->valid |= CTS_SCSI_VALID_TQ;
1879 			if (cts->type == CTS_TYPE_USER_SETTINGS ||
1880 			    (device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0 ||
1881 			    (device->inq_flags & SID_CmdQue) != 0)
1882 				scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
1883 		}
1884 	}
1885 
1886 	if (cts->transport == XPORT_UNKNOWN ||
1887 	    cts->transport == XPORT_UNSPECIFIED) {
1888 		cts->transport = device->transport;
1889 		cts->transport_version = device->transport_version;
1890 	}
1891 }
1892 
1893 static void
1894 ata_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_path *path,
1895 			   int async_update)
1896 {
1897 	struct	ccb_pathinq cpi;
1898 	struct	ccb_trans_settings_ata *ata;
1899 	struct	ccb_trans_settings_scsi *scsi;
1900 	struct	ata_params *ident_data;
1901 	struct	scsi_inquiry_data *inq_data;
1902 	struct	cam_ed *device;
1903 
1904 	if (path == NULL || (device = path->device) == NULL) {
1905 		cts->ccb_h.status = CAM_PATH_INVALID;
1906 		xpt_done((union ccb *)cts);
1907 		return;
1908 	}
1909 
1910 	if (cts->protocol == PROTO_UNKNOWN
1911 	 || cts->protocol == PROTO_UNSPECIFIED) {
1912 		cts->protocol = device->protocol;
1913 		cts->protocol_version = device->protocol_version;
1914 	}
1915 
1916 	if (cts->protocol_version == PROTO_VERSION_UNKNOWN
1917 	 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
1918 		cts->protocol_version = device->protocol_version;
1919 
1920 	if (cts->protocol != device->protocol) {
1921 		xpt_print(path, "Uninitialized Protocol %x:%x?\n",
1922 		       cts->protocol, device->protocol);
1923 		cts->protocol = device->protocol;
1924 	}
1925 
1926 	if (cts->protocol_version > device->protocol_version) {
1927 		if (bootverbose) {
1928 			xpt_print(path, "Down reving Protocol "
1929 			    "Version from %d to %d?\n", cts->protocol_version,
1930 			    device->protocol_version);
1931 		}
1932 		cts->protocol_version = device->protocol_version;
1933 	}
1934 
1935 	if (cts->transport == XPORT_UNKNOWN
1936 	 || cts->transport == XPORT_UNSPECIFIED) {
1937 		cts->transport = device->transport;
1938 		cts->transport_version = device->transport_version;
1939 	}
1940 
1941 	if (cts->transport_version == XPORT_VERSION_UNKNOWN
1942 	 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
1943 		cts->transport_version = device->transport_version;
1944 
1945 	if (cts->transport != device->transport) {
1946 		xpt_print(path, "Uninitialized Transport %x:%x?\n",
1947 		    cts->transport, device->transport);
1948 		cts->transport = device->transport;
1949 	}
1950 
1951 	if (cts->transport_version > device->transport_version) {
1952 		if (bootverbose) {
1953 			xpt_print(path, "Down reving Transport "
1954 			    "Version from %d to %d?\n", cts->transport_version,
1955 			    device->transport_version);
1956 		}
1957 		cts->transport_version = device->transport_version;
1958 	}
1959 
1960 	ident_data = &device->ident_data;
1961 	inq_data = &device->inq_data;
1962 	if (cts->protocol == PROTO_ATA)
1963 		ata = &cts->proto_specific.ata;
1964 	else
1965 		ata = NULL;
1966 	if (cts->protocol == PROTO_SCSI)
1967 		scsi = &cts->proto_specific.scsi;
1968 	else
1969 		scsi = NULL;
1970 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
1971 	cpi.ccb_h.func_code = XPT_PATH_INQ;
1972 	xpt_action((union ccb *)&cpi);
1973 
1974 	/* Sanity checking */
1975 	if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
1976 	 || (ata && (ident_data->satacapabilities & ATA_SUPPORT_NCQ) == 0)
1977 	 || (scsi && (INQ_DATA_TQ_ENABLED(inq_data)) == 0)
1978 	 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
1979 	 || (device->mintags == 0)) {
1980 		/*
1981 		 * Can't tag on hardware that doesn't support tags,
1982 		 * doesn't have it enabled, or has broken tag support.
1983 		 */
1984 		if (ata)
1985 			ata->flags &= ~CTS_ATA_FLAGS_TAG_ENB;
1986 		if (scsi)
1987 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1988 	}
1989 
1990 	/* Start/stop tags use. */
1991 	if (cts->type == CTS_TYPE_CURRENT_SETTINGS &&
1992 	    ((ata && (ata->valid & CTS_ATA_VALID_TQ) != 0) ||
1993 	     (scsi && (scsi->valid & CTS_SCSI_VALID_TQ) != 0))) {
1994 		int nowt, newt = 0;
1995 
1996 		nowt = ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0 ||
1997 			(device->inq_flags & SID_CmdQue) != 0);
1998 		if (ata)
1999 			newt = (ata->flags & CTS_ATA_FLAGS_TAG_ENB) != 0;
2000 		if (scsi)
2001 			newt = (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0;
2002 
2003 		if (newt && !nowt) {
2004 			/*
2005 			 * Delay change to use tags until after a
2006 			 * few commands have gone to this device so
2007 			 * the controller has time to perform transfer
2008 			 * negotiations without tagged messages getting
2009 			 * in the way.
2010 			 */
2011 			device->tag_delay_count = CAM_TAG_DELAY_COUNT;
2012 			device->flags |= CAM_DEV_TAG_AFTER_COUNT;
2013 		} else if (nowt && !newt)
2014 			xpt_stop_tags(path);
2015 	}
2016 
2017 	if (async_update == FALSE)
2018 		xpt_action_default((union ccb *)cts);
2019 }
2020 
2021 /*
2022  * Handle any per-device event notifications that require action by the XPT.
2023  */
2024 static void
2025 ata_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
2026 	      struct cam_ed *device, void *async_arg)
2027 {
2028 	cam_status status;
2029 	struct cam_path newpath;
2030 
2031 	/*
2032 	 * We only need to handle events for real devices.
2033 	 */
2034 	if (target->target_id == CAM_TARGET_WILDCARD
2035 	 || device->lun_id == CAM_LUN_WILDCARD)
2036 		return;
2037 
2038 	/*
2039 	 * We need our own path with wildcards expanded to
2040 	 * handle certain types of events.
2041 	 */
2042 	if ((async_code == AC_SENT_BDR)
2043 	 || (async_code == AC_BUS_RESET)
2044 	 || (async_code == AC_INQ_CHANGED))
2045 		status = xpt_compile_path(&newpath, NULL,
2046 					  bus->path_id,
2047 					  target->target_id,
2048 					  device->lun_id);
2049 	else
2050 		status = CAM_REQ_CMP_ERR;
2051 
2052 	if (status == CAM_REQ_CMP) {
2053 		if (async_code == AC_INQ_CHANGED) {
2054 			/*
2055 			 * We've sent a start unit command, or
2056 			 * something similar to a device that
2057 			 * may have caused its inquiry data to
2058 			 * change. So we re-scan the device to
2059 			 * refresh the inquiry data for it.
2060 			 */
2061 			ata_scan_lun(newpath.periph, &newpath,
2062 				     CAM_EXPECT_INQ_CHANGE, NULL);
2063 		} else {
2064 			/* We need to reinitialize device after reset. */
2065 			ata_scan_lun(newpath.periph, &newpath,
2066 				     0, NULL);
2067 		}
2068 		xpt_release_path(&newpath);
2069 	} else if (async_code == AC_LOST_DEVICE &&
2070 	    (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
2071 		device->flags |= CAM_DEV_UNCONFIGURED;
2072 		xpt_release_device(device);
2073 	} else if (async_code == AC_TRANSFER_NEG) {
2074 		struct ccb_trans_settings *settings;
2075 		struct cam_path path;
2076 
2077 		settings = (struct ccb_trans_settings *)async_arg;
2078 		xpt_compile_path(&path, NULL, bus->path_id, target->target_id,
2079 				 device->lun_id);
2080 		ata_set_transfer_settings(settings, &path,
2081 					  /*async_update*/TRUE);
2082 		xpt_release_path(&path);
2083 	}
2084 }
2085 
2086 static void
2087 _ata_announce_periph(struct cam_periph *periph, struct ccb_trans_settings *cts, u_int *speed)
2088 {
2089 	struct	ccb_pathinq cpi;
2090 	struct	cam_path *path = periph->path;
2091 
2092 	cam_periph_assert(periph, MA_OWNED);
2093 
2094 	xpt_setup_ccb(&cts->ccb_h, path, CAM_PRIORITY_NORMAL);
2095 	cts->ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
2096 	cts->type = CTS_TYPE_CURRENT_SETTINGS;
2097 	xpt_action((union ccb*)cts);
2098 	if ((cts->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
2099 		return;
2100 	/* Ask the SIM for its base transfer speed */
2101 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
2102 	cpi.ccb_h.func_code = XPT_PATH_INQ;
2103 	xpt_action((union ccb *)&cpi);
2104 	/* Report connection speed */
2105 	*speed = cpi.base_transfer_speed;
2106 	if (cts->transport == XPORT_ATA) {
2107 		struct	ccb_trans_settings_pata *pata =
2108 		    &cts->xport_specific.ata;
2109 
2110 		if (pata->valid & CTS_ATA_VALID_MODE)
2111 			*speed = ata_mode2speed(pata->mode);
2112 	}
2113 	if (cts->transport == XPORT_SATA) {
2114 		struct	ccb_trans_settings_sata *sata =
2115 		    &cts->xport_specific.sata;
2116 
2117 		if (sata->valid & CTS_SATA_VALID_REVISION)
2118 			*speed = ata_revision2speed(sata->revision);
2119 	}
2120 }
2121 
2122 static void
2123 ata_announce_periph(struct cam_periph *periph)
2124 {
2125 	struct ccb_trans_settings cts;
2126 	u_int speed, mb;
2127 
2128 	_ata_announce_periph(periph, &cts, &speed);
2129 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
2130 		return;
2131 
2132 	mb = speed / 1000;
2133 	if (mb > 0)
2134 		printf("%s%d: %d.%03dMB/s transfers",
2135 		       periph->periph_name, periph->unit_number,
2136 		       mb, speed % 1000);
2137 	else
2138 		printf("%s%d: %dKB/s transfers", periph->periph_name,
2139 		       periph->unit_number, speed);
2140 	/* Report additional information about connection */
2141 	if (cts.transport == XPORT_ATA) {
2142 		struct ccb_trans_settings_pata *pata =
2143 		    &cts.xport_specific.ata;
2144 
2145 		printf(" (");
2146 		if (pata->valid & CTS_ATA_VALID_MODE)
2147 			printf("%s, ", ata_mode2string(pata->mode));
2148 		if ((pata->valid & CTS_ATA_VALID_ATAPI) && pata->atapi != 0)
2149 			printf("ATAPI %dbytes, ", pata->atapi);
2150 		if (pata->valid & CTS_ATA_VALID_BYTECOUNT)
2151 			printf("PIO %dbytes", pata->bytecount);
2152 		printf(")");
2153 	}
2154 	if (cts.transport == XPORT_SATA) {
2155 		struct ccb_trans_settings_sata *sata =
2156 		    &cts.xport_specific.sata;
2157 
2158 		printf(" (");
2159 		if (sata->valid & CTS_SATA_VALID_REVISION)
2160 			printf("SATA %d.x, ", sata->revision);
2161 		else
2162 			printf("SATA, ");
2163 		if (sata->valid & CTS_SATA_VALID_MODE)
2164 			printf("%s, ", ata_mode2string(sata->mode));
2165 		if ((sata->valid & CTS_ATA_VALID_ATAPI) && sata->atapi != 0)
2166 			printf("ATAPI %dbytes, ", sata->atapi);
2167 		if (sata->valid & CTS_SATA_VALID_BYTECOUNT)
2168 			printf("PIO %dbytes", sata->bytecount);
2169 		printf(")");
2170 	}
2171 	printf("\n");
2172 }
2173 
2174 static void
2175 ata_announce_periph_sbuf(struct cam_periph *periph, struct sbuf *sb)
2176 {
2177 	struct ccb_trans_settings cts;
2178 	u_int speed, mb;
2179 
2180 	_ata_announce_periph(periph, &cts, &speed);
2181 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
2182 		return;
2183 
2184 	mb = speed / 1000;
2185 	if (mb > 0)
2186 		sbuf_printf(sb, "%s%d: %d.%03dMB/s transfers",
2187 		       periph->periph_name, periph->unit_number,
2188 		       mb, speed % 1000);
2189 	else
2190 		sbuf_printf(sb, "%s%d: %dKB/s transfers", periph->periph_name,
2191 		       periph->unit_number, speed);
2192 	/* Report additional information about connection */
2193 	if (cts.transport == XPORT_ATA) {
2194 		struct ccb_trans_settings_pata *pata =
2195 		    &cts.xport_specific.ata;
2196 
2197 		sbuf_printf(sb, " (");
2198 		if (pata->valid & CTS_ATA_VALID_MODE)
2199 			sbuf_printf(sb, "%s, ", ata_mode2string(pata->mode));
2200 		if ((pata->valid & CTS_ATA_VALID_ATAPI) && pata->atapi != 0)
2201 			sbuf_printf(sb, "ATAPI %dbytes, ", pata->atapi);
2202 		if (pata->valid & CTS_ATA_VALID_BYTECOUNT)
2203 			sbuf_printf(sb, "PIO %dbytes", pata->bytecount);
2204 		sbuf_printf(sb, ")");
2205 	}
2206 	if (cts.transport == XPORT_SATA) {
2207 		struct ccb_trans_settings_sata *sata =
2208 		    &cts.xport_specific.sata;
2209 
2210 		sbuf_printf(sb, " (");
2211 		if (sata->valid & CTS_SATA_VALID_REVISION)
2212 			sbuf_printf(sb, "SATA %d.x, ", sata->revision);
2213 		else
2214 			sbuf_printf(sb, "SATA, ");
2215 		if (sata->valid & CTS_SATA_VALID_MODE)
2216 			sbuf_printf(sb, "%s, ", ata_mode2string(sata->mode));
2217 		if ((sata->valid & CTS_ATA_VALID_ATAPI) && sata->atapi != 0)
2218 			sbuf_printf(sb, "ATAPI %dbytes, ", sata->atapi);
2219 		if (sata->valid & CTS_SATA_VALID_BYTECOUNT)
2220 			sbuf_printf(sb, "PIO %dbytes", sata->bytecount);
2221 		sbuf_printf(sb, ")");
2222 	}
2223 	sbuf_printf(sb, "\n");
2224 }
2225 
2226 static void
2227 ata_proto_announce_sbuf(struct cam_ed *device, struct sbuf *sb)
2228 {
2229 	ata_print_ident_sbuf(&device->ident_data, sb);
2230 }
2231 
2232 static void
2233 ata_proto_announce(struct cam_ed *device)
2234 {
2235 	ata_print_ident(&device->ident_data);
2236 }
2237 
2238 static void
2239 ata_proto_denounce(struct cam_ed *device)
2240 {
2241 	ata_print_ident_short(&device->ident_data);
2242 }
2243 
2244 static void
2245 ata_proto_denounce_sbuf(struct cam_ed *device, struct sbuf *sb)
2246 {
2247 	ata_print_ident_short_sbuf(&device->ident_data, sb);
2248 }
2249 
2250 static void
2251 semb_proto_announce_sbuf(struct cam_ed *device, struct sbuf *sb)
2252 {
2253 	semb_print_ident_sbuf((struct sep_identify_data *)&device->ident_data, sb);
2254 }
2255 
2256 static void
2257 semb_proto_announce(struct cam_ed *device)
2258 {
2259 	semb_print_ident((struct sep_identify_data *)&device->ident_data);
2260 }
2261 
2262 static void
2263 semb_proto_denounce(struct cam_ed *device)
2264 {
2265 	semb_print_ident_short((struct sep_identify_data *)&device->ident_data);
2266 }
2267 
2268 static void
2269 semb_proto_denounce_sbuf(struct cam_ed *device, struct sbuf *sb)
2270 {
2271 	semb_print_ident_short_sbuf((struct sep_identify_data *)&device->ident_data, sb);
2272 }
2273 
2274 static void
2275 ata_proto_debug_out(union ccb *ccb)
2276 {
2277 	char cdb_str[(sizeof(struct ata_cmd) * 3) + 1];
2278 
2279 	if (ccb->ccb_h.func_code != XPT_ATA_IO)
2280 		return;
2281 
2282 	CAM_DEBUG(ccb->ccb_h.path,
2283 	    CAM_DEBUG_CDB,("%s. ACB: %s\n", ata_op_string(&ccb->ataio.cmd),
2284 		ata_cmd_string(&ccb->ataio.cmd, cdb_str, sizeof(cdb_str))));
2285 }
2286