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