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