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