xref: /freebsd/sys/cam/ata/ata_xpt.c (revision 5022f21bd974c740b9052f149fb31745dc602965)
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/md5.h>
41 #include <sys/interrupt.h>
42 #include <sys/sbuf.h>
43 
44 #include <sys/lock.h>
45 #include <sys/mutex.h>
46 #include <sys/sysctl.h>
47 
48 #ifdef PC98
49 #include <pc98/pc98/pc98_machdep.h>	/* geometry translation */
50 #endif
51 
52 #include <cam/cam.h>
53 #include <cam/cam_ccb.h>
54 #include <cam/cam_queue.h>
55 #include <cam/cam_periph.h>
56 #include <cam/cam_sim.h>
57 #include <cam/cam_xpt.h>
58 #include <cam/cam_xpt_sim.h>
59 #include <cam/cam_xpt_periph.h>
60 #include <cam/cam_xpt_internal.h>
61 #include <cam/cam_debug.h>
62 
63 #include <cam/scsi/scsi_all.h>
64 #include <cam/scsi/scsi_message.h>
65 #include <cam/ata/ata_all.h>
66 #include <machine/stdarg.h>	/* for xpt_print below */
67 #include "opt_cam.h"
68 
69 struct ata_quirk_entry {
70 	struct scsi_inquiry_pattern inq_pat;
71 	u_int8_t quirks;
72 #define	CAM_QUIRK_MAXTAGS	0x01
73 	u_int maxtags;
74 };
75 
76 static periph_init_t probe_periph_init;
77 
78 static struct periph_driver probe_driver =
79 {
80 	probe_periph_init, "aprobe",
81 	TAILQ_HEAD_INITIALIZER(probe_driver.units), /* generation */ 0,
82 	CAM_PERIPH_DRV_EARLY
83 };
84 
85 PERIPHDRIVER_DECLARE(aprobe, probe_driver);
86 
87 typedef enum {
88 	PROBE_RESET,
89 	PROBE_IDENTIFY,
90 	PROBE_SETMODE,
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_SETMODE",
103 	"PROBE_SET_MULTI",
104 	"PROBE_INQUIRY",
105 	"PROBE_FULL_INQUIRY",
106 	"PROBE_PM_PID",
107 	"PROBE_PM_PRV",
108 	"PROBE_INVALID"
109 };
110 
111 #define PROBE_SET_ACTION(softc, newaction)	\
112 do {									\
113 	char **text;							\
114 	text = probe_action_text;					\
115 	CAM_DEBUG((softc)->periph->path, CAM_DEBUG_INFO,		\
116 	    ("Probe %s to %s\n", text[(softc)->action],			\
117 	    text[(newaction)]));					\
118 	(softc)->action = (newaction);					\
119 } while(0)
120 
121 typedef enum {
122 	PROBE_NO_ANNOUNCE	= 0x04
123 } probe_flags;
124 
125 typedef struct {
126 	TAILQ_HEAD(, ccb_hdr) request_ccbs;
127 	probe_action	action;
128 	union ccb	saved_ccb;
129 	probe_flags	flags;
130 	u_int8_t	digest[16];
131 	uint32_t	pm_pid;
132 	uint32_t	pm_prv;
133 	struct cam_periph *periph;
134 } probe_softc;
135 
136 static struct ata_quirk_entry ata_quirk_table[] =
137 {
138 	{
139 		/* Default tagged queuing parameters for all devices */
140 		{
141 		  T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
142 		  /*vendor*/"*", /*product*/"*", /*revision*/"*"
143 		},
144 		/*quirks*/0, /*maxtags*/0
145 	},
146 };
147 
148 static const int ata_quirk_table_size =
149 	sizeof(ata_quirk_table) / sizeof(*ata_quirk_table);
150 
151 static cam_status	proberegister(struct cam_periph *periph,
152 				      void *arg);
153 static void	 probeschedule(struct cam_periph *probe_periph);
154 static void	 probestart(struct cam_periph *periph, union ccb *start_ccb);
155 //static void	 proberequestdefaultnegotiation(struct cam_periph *periph);
156 //static int       proberequestbackoff(struct cam_periph *periph,
157 //				     struct cam_ed *device);
158 static void	 probedone(struct cam_periph *periph, union ccb *done_ccb);
159 static void	 probecleanup(struct cam_periph *periph);
160 static void	 ata_find_quirk(struct cam_ed *device);
161 static void	 ata_scan_bus(struct cam_periph *periph, union ccb *ccb);
162 static void	 ata_scan_lun(struct cam_periph *periph,
163 			       struct cam_path *path, cam_flags flags,
164 			       union ccb *ccb);
165 static void	 xptscandone(struct cam_periph *periph, union ccb *done_ccb);
166 static struct cam_ed *
167 		 ata_alloc_device(struct cam_eb *bus, struct cam_et *target,
168 				   lun_id_t lun_id);
169 static void	 ata_device_transport(struct cam_path *path);
170 static void	 ata_set_transfer_settings(struct ccb_trans_settings *cts,
171 					    struct cam_ed *device,
172 					    int async_update);
173 static void	 ata_dev_async(u_int32_t async_code,
174 				struct cam_eb *bus,
175 				struct cam_et *target,
176 				struct cam_ed *device,
177 				void *async_arg);
178 static void	 ata_action(union ccb *start_ccb);
179 
180 static struct xpt_xport ata_xport = {
181 	.alloc_device = ata_alloc_device,
182 	.action = ata_action,
183 	.async = ata_dev_async,
184 };
185 
186 struct xpt_xport *
187 ata_get_xport(void)
188 {
189 	return (&ata_xport);
190 }
191 
192 static void
193 probe_periph_init()
194 {
195 }
196 
197 static cam_status
198 proberegister(struct cam_periph *periph, void *arg)
199 {
200 	union ccb *request_ccb;	/* CCB representing the probe request */
201 	cam_status status;
202 	probe_softc *softc;
203 
204 	request_ccb = (union ccb *)arg;
205 	if (periph == NULL) {
206 		printf("proberegister: periph was NULL!!\n");
207 		return(CAM_REQ_CMP_ERR);
208 	}
209 
210 	if (request_ccb == NULL) {
211 		printf("proberegister: no probe CCB, "
212 		       "can't register device\n");
213 		return(CAM_REQ_CMP_ERR);
214 	}
215 
216 	softc = (probe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_NOWAIT);
217 
218 	if (softc == NULL) {
219 		printf("proberegister: Unable to probe new device. "
220 		       "Unable to allocate softc\n");
221 		return(CAM_REQ_CMP_ERR);
222 	}
223 	TAILQ_INIT(&softc->request_ccbs);
224 	TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
225 			  periph_links.tqe);
226 	softc->flags = 0;
227 	periph->softc = softc;
228 	softc->periph = periph;
229 	softc->action = PROBE_INVALID;
230 	status = cam_periph_acquire(periph);
231 	if (status != CAM_REQ_CMP) {
232 		return (status);
233 	}
234 
235 
236 	/*
237 	 * Ensure we've waited at least a bus settle
238 	 * delay before attempting to probe the device.
239 	 * For HBAs that don't do bus resets, this won't make a difference.
240 	 */
241 	cam_periph_freeze_after_event(periph, &periph->path->bus->last_reset,
242 				      scsi_delay);
243 	probeschedule(periph);
244 	return(CAM_REQ_CMP);
245 }
246 
247 static void
248 probeschedule(struct cam_periph *periph)
249 {
250 	struct ccb_pathinq cpi;
251 	union ccb *ccb;
252 	probe_softc *softc;
253 
254 	softc = (probe_softc *)periph->softc;
255 	ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
256 
257 	xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
258 	cpi.ccb_h.func_code = XPT_PATH_INQ;
259 	xpt_action((union ccb *)&cpi);
260 
261 	if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) ||
262 	    periph->path->device->protocol == PROTO_SATAPM)
263 		PROBE_SET_ACTION(softc, PROBE_RESET);
264 	else
265 		PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
266 
267 	if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
268 		softc->flags |= PROBE_NO_ANNOUNCE;
269 	else
270 		softc->flags &= ~PROBE_NO_ANNOUNCE;
271 
272 	xpt_schedule(periph, ccb->ccb_h.pinfo.priority);
273 }
274 
275 static void
276 probestart(struct cam_periph *periph, union ccb *start_ccb)
277 {
278 	struct ccb_trans_settings cts;
279 	struct ccb_ataio *ataio;
280 	struct ccb_scsiio *csio;
281 	probe_softc *softc;
282 	struct cam_path *path;
283 	struct ata_params *ident_buf;
284 
285 	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
286 
287 	softc = (probe_softc *)periph->softc;
288 	path = start_ccb->ccb_h.path;
289 	ataio = &start_ccb->ataio;
290 	csio = &start_ccb->csio;
291 	ident_buf = &periph->path->device->ident_data;
292 
293 	switch (softc->action) {
294 	case PROBE_RESET:
295 		cam_fill_ataio(ataio,
296 		      0,
297 		      probedone,
298 		      /*flags*/CAM_DIR_NONE,
299 		      0,
300 		      /*data_ptr*/NULL,
301 		      /*dxfer_len*/0,
302 		      (start_ccb->ccb_h.target_id == 15 ? 3 : 15) * 1000);
303 		ata_reset_cmd(ataio);
304 		break;
305 	case PROBE_IDENTIFY:
306 		if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
307 			/* Prepare check that it is the same device. */
308 			MD5_CTX context;
309 
310 			MD5Init(&context);
311 			MD5Update(&context,
312 			    (unsigned char *)ident_buf->model,
313 			    sizeof(ident_buf->model));
314 			MD5Update(&context,
315 			    (unsigned char *)ident_buf->revision,
316 			    sizeof(ident_buf->revision));
317 			MD5Update(&context,
318 			    (unsigned char *)ident_buf->serial,
319 			    sizeof(ident_buf->serial));
320 			MD5Final(softc->digest, &context);
321 		}
322 		cam_fill_ataio(ataio,
323 		      1,
324 		      probedone,
325 		      /*flags*/CAM_DIR_IN,
326 		      0,
327 		      /*data_ptr*/(u_int8_t *)ident_buf,
328 		      /*dxfer_len*/sizeof(struct ata_params),
329 		      30 * 1000);
330 		if (periph->path->device->protocol == PROTO_ATA)
331 			ata_28bit_cmd(ataio, ATA_ATA_IDENTIFY, 0, 0, 0);
332 		else
333 			ata_28bit_cmd(ataio, ATA_ATAPI_IDENTIFY, 0, 0, 0);
334 		break;
335 	case PROBE_SETMODE:
336 	{
337 		int mode, wantmode;
338 
339 		mode = 0;
340 		/* Fetch user modes from SIM. */
341 		bzero(&cts, sizeof(cts));
342 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
343 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
344 		cts.type = CTS_TYPE_USER_SETTINGS;
345 		xpt_action((union ccb *)&cts);
346 		if (path->device->transport == XPORT_ATA) {
347 			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_MODE)
348 				mode = cts.xport_specific.ata.mode;
349 		} else {
350 			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_MODE)
351 				mode = cts.xport_specific.sata.mode;
352 		}
353 negotiate:
354 		/* Honor device capabilities. */
355 		wantmode = mode = ata_max_mode(ident_buf, mode);
356 		/* Report modes to SIM. */
357 		bzero(&cts, sizeof(cts));
358 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
359 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
360 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
361 		if (path->device->transport == XPORT_ATA) {
362 			cts.xport_specific.ata.mode = mode;
363 			cts.xport_specific.ata.valid = CTS_ATA_VALID_MODE;
364 		} else {
365 			cts.xport_specific.sata.mode = mode;
366 			cts.xport_specific.sata.valid = CTS_SATA_VALID_MODE;
367 		}
368 		xpt_action((union ccb *)&cts);
369 		/* Fetch user modes from SIM. */
370 		bzero(&cts, sizeof(cts));
371 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
372 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
373 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
374 		xpt_action((union ccb *)&cts);
375 		if (path->device->transport == XPORT_ATA) {
376 			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_MODE)
377 				mode = cts.xport_specific.ata.mode;
378 		} else {
379 			if (cts.xport_specific.ata.valid & CTS_SATA_VALID_MODE)
380 				mode = cts.xport_specific.sata.mode;
381 		}
382 		/* If SIM disagree - renegotiate. */
383 		if (mode != wantmode)
384 			goto negotiate;
385 		cam_fill_ataio(ataio,
386 		      1,
387 		      probedone,
388 		      /*flags*/CAM_DIR_NONE,
389 		      0,
390 		      /*data_ptr*/NULL,
391 		      /*dxfer_len*/0,
392 		      30 * 1000);
393 		ata_28bit_cmd(ataio, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
394 		break;
395 	}
396 	case PROBE_SET_MULTI:
397 	{
398 		u_int sectors;
399 
400 		sectors = max(1, min(ident_buf->sectors_intr & 0xff, 16));
401 
402 		/* Report bytecount to SIM. */
403 		bzero(&cts, sizeof(cts));
404 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
405 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
406 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
407 		if (path->device->transport == XPORT_ATA) {
408 			cts.xport_specific.ata.bytecount = sectors *
409 			    ata_logical_sector_size(ident_buf);
410 			cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT;
411 		} else {
412 			cts.xport_specific.sata.bytecount = sectors *
413 			    ata_logical_sector_size(ident_buf);
414 			cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT;
415 		}
416 		xpt_action((union ccb *)&cts);
417 
418 		cam_fill_ataio(ataio,
419 		    1,
420 		    probedone,
421 		    CAM_DIR_NONE,
422 		    0,
423 		    NULL,
424 		    0,
425 		    30*1000);
426 		ata_28bit_cmd(ataio, ATA_SET_MULTI, 0, 0, sectors);
427 		break;
428 	}
429 	case PROBE_INQUIRY:
430 	case PROBE_FULL_INQUIRY:
431 	{
432 		u_int inquiry_len;
433 		struct scsi_inquiry_data *inq_buf =
434 		    &periph->path->device->inq_data;
435 
436 		if (softc->action == PROBE_INQUIRY)
437 			inquiry_len = SHORT_INQUIRY_LENGTH;
438 		else
439 			inquiry_len = SID_ADDITIONAL_LENGTH(inq_buf);
440 		/*
441 		 * Some parallel SCSI devices fail to send an
442 		 * ignore wide residue message when dealing with
443 		 * odd length inquiry requests.  Round up to be
444 		 * safe.
445 		 */
446 		inquiry_len = roundup2(inquiry_len, 2);
447 		scsi_inquiry(csio,
448 			     /*retries*/1,
449 			     probedone,
450 			     MSG_SIMPLE_Q_TAG,
451 			     (u_int8_t *)inq_buf,
452 			     inquiry_len,
453 			     /*evpd*/FALSE,
454 			     /*page_code*/0,
455 			     SSD_MIN_SIZE,
456 			     /*timeout*/60 * 1000);
457 		break;
458 	}
459 	case PROBE_PM_PID:
460 		cam_fill_ataio(ataio,
461 		      1,
462 		      probedone,
463 		      /*flags*/CAM_DIR_NONE,
464 		      0,
465 		      /*data_ptr*/NULL,
466 		      /*dxfer_len*/0,
467 		      10 * 1000);
468 		ata_pm_read_cmd(ataio, 0, 15);
469 		break;
470 	case PROBE_PM_PRV:
471 		cam_fill_ataio(ataio,
472 		      1,
473 		      probedone,
474 		      /*flags*/CAM_DIR_NONE,
475 		      0,
476 		      /*data_ptr*/NULL,
477 		      /*dxfer_len*/0,
478 		      10 * 1000);
479 		ata_pm_read_cmd(ataio, 1, 15);
480 		break;
481 	case PROBE_INVALID:
482 		CAM_DEBUG(path, CAM_DEBUG_INFO,
483 		    ("probestart: invalid action state\n"));
484 	default:
485 		break;
486 	}
487 	xpt_action(start_ccb);
488 }
489 #if 0
490 static void
491 proberequestdefaultnegotiation(struct cam_periph *periph)
492 {
493 	struct ccb_trans_settings cts;
494 
495 	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
496 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
497 	cts.type = CTS_TYPE_USER_SETTINGS;
498 	xpt_action((union ccb *)&cts);
499 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
500 		return;
501 	}
502 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
503 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
504 	xpt_action((union ccb *)&cts);
505 }
506 
507 /*
508  * Backoff Negotiation Code- only pertinent for SPI devices.
509  */
510 static int
511 proberequestbackoff(struct cam_periph *periph, struct cam_ed *device)
512 {
513 	struct ccb_trans_settings cts;
514 	struct ccb_trans_settings_spi *spi;
515 
516 	memset(&cts, 0, sizeof (cts));
517 	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
518 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
519 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
520 	xpt_action((union ccb *)&cts);
521 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
522 		if (bootverbose) {
523 			xpt_print(periph->path,
524 			    "failed to get current device settings\n");
525 		}
526 		return (0);
527 	}
528 	if (cts.transport != XPORT_SPI) {
529 		if (bootverbose) {
530 			xpt_print(periph->path, "not SPI transport\n");
531 		}
532 		return (0);
533 	}
534 	spi = &cts.xport_specific.spi;
535 
536 	/*
537 	 * We cannot renegotiate sync rate if we don't have one.
538 	 */
539 	if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
540 		if (bootverbose) {
541 			xpt_print(periph->path, "no sync rate known\n");
542 		}
543 		return (0);
544 	}
545 
546 	/*
547 	 * We'll assert that we don't have to touch PPR options- the
548 	 * SIM will see what we do with period and offset and adjust
549 	 * the PPR options as appropriate.
550 	 */
551 
552 	/*
553 	 * A sync rate with unknown or zero offset is nonsensical.
554 	 * A sync period of zero means Async.
555 	 */
556 	if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0
557 	 || spi->sync_offset == 0 || spi->sync_period == 0) {
558 		if (bootverbose) {
559 			xpt_print(periph->path, "no sync rate available\n");
560 		}
561 		return (0);
562 	}
563 
564 	if (device->flags & CAM_DEV_DV_HIT_BOTTOM) {
565 		CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
566 		    ("hit async: giving up on DV\n"));
567 		return (0);
568 	}
569 
570 
571 	/*
572 	 * Jump sync_period up by one, but stop at 5MHz and fall back to Async.
573 	 * We don't try to remember 'last' settings to see if the SIM actually
574 	 * gets into the speed we want to set. We check on the SIM telling
575 	 * us that a requested speed is bad, but otherwise don't try and
576 	 * check the speed due to the asynchronous and handshake nature
577 	 * of speed setting.
578 	 */
579 	spi->valid = CTS_SPI_VALID_SYNC_RATE | CTS_SPI_VALID_SYNC_OFFSET;
580 	for (;;) {
581 		spi->sync_period++;
582 		if (spi->sync_period >= 0xf) {
583 			spi->sync_period = 0;
584 			spi->sync_offset = 0;
585 			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
586 			    ("setting to async for DV\n"));
587 			/*
588 			 * Once we hit async, we don't want to try
589 			 * any more settings.
590 			 */
591 			device->flags |= CAM_DEV_DV_HIT_BOTTOM;
592 		} else if (bootverbose) {
593 			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
594 			    ("DV: period 0x%x\n", spi->sync_period));
595 			printf("setting period to 0x%x\n", spi->sync_period);
596 		}
597 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
598 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
599 		xpt_action((union ccb *)&cts);
600 		if ((cts.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
601 			break;
602 		}
603 		CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
604 		    ("DV: failed to set period 0x%x\n", spi->sync_period));
605 		if (spi->sync_period == 0) {
606 			return (0);
607 		}
608 	}
609 	return (1);
610 }
611 #endif
612 static void
613 probedone(struct cam_periph *periph, union ccb *done_ccb)
614 {
615 	struct ccb_trans_settings cts;
616 	struct ata_params *ident_buf;
617 	probe_softc *softc;
618 	struct cam_path *path;
619 	u_int32_t  priority;
620 	int found = 1;
621 
622 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
623 
624 	softc = (probe_softc *)periph->softc;
625 	path = done_ccb->ccb_h.path;
626 	priority = done_ccb->ccb_h.pinfo.priority;
627 	ident_buf = &path->device->ident_data;
628 
629 	if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
630 device_fail:	if (cam_periph_error(done_ccb, 0, 0,
631 		    &softc->saved_ccb) == ERESTART) {
632 			return;
633 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
634 			/* Don't wedge the queue */
635 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
636 					 /*run_queue*/TRUE);
637 		}
638 		/* Old PIO2 devices may not support mode setting. */
639 		if (softc->action == PROBE_SETMODE &&
640 		    ata_max_pmode(ident_buf) <= ATA_PIO2 &&
641 		    (ident_buf->capabilities1 & ATA_SUPPORT_IORDY) == 0)
642 			goto noerror;
643 		/*
644 		 * If we get to this point, we got an error status back
645 		 * from the inquiry and the error status doesn't require
646 		 * automatically retrying the command.  Therefore, the
647 		 * inquiry failed.  If we had inquiry information before
648 		 * for this device, but this latest inquiry command failed,
649 		 * the device has probably gone away.  If this device isn't
650 		 * already marked unconfigured, notify the peripheral
651 		 * drivers that this device is no more.
652 		 */
653 		if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
654 			xpt_async(AC_LOST_DEVICE, path, NULL);
655 		found = 0;
656 		goto done;
657 	}
658 noerror:
659 	switch (softc->action) {
660 	case PROBE_RESET:
661 	{
662 		int sign = (done_ccb->ataio.res.lba_high << 8) +
663 		    done_ccb->ataio.res.lba_mid;
664 		xpt_print(path, "SIGNATURE: %04x\n", sign);
665 		if (sign == 0x0000 &&
666 		    done_ccb->ccb_h.target_id != 15) {
667 			path->device->protocol = PROTO_ATA;
668 			PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
669 		} else if (sign == 0x9669 &&
670 		    done_ccb->ccb_h.target_id == 15) {
671 			/* Report SIM that PM is present. */
672 			bzero(&cts, sizeof(cts));
673 			xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
674 			cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
675 			cts.type = CTS_TYPE_CURRENT_SETTINGS;
676 			cts.xport_specific.sata.pm_present = 1;
677 			cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
678 			xpt_action((union ccb *)&cts);
679 			path->device->protocol = PROTO_SATAPM;
680 			PROBE_SET_ACTION(softc, PROBE_PM_PID);
681 		} else if (sign == 0xeb14 &&
682 		    done_ccb->ccb_h.target_id != 15) {
683 			path->device->protocol = PROTO_SCSI;
684 			PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
685 		} else {
686 			if (done_ccb->ccb_h.target_id != 15) {
687 				xpt_print(path,
688 				    "Unexpected signature 0x%04x\n", sign);
689 			}
690 			goto device_fail;
691 		}
692 		xpt_release_ccb(done_ccb);
693 		xpt_schedule(periph, priority);
694 		return;
695 	}
696 	case PROBE_IDENTIFY:
697 	{
698 		int16_t *ptr;
699 
700 		for (ptr = (int16_t *)ident_buf;
701 		     ptr < (int16_t *)ident_buf + sizeof(struct ata_params)/2; ptr++) {
702 			*ptr = le16toh(*ptr);
703 		}
704 		if (strncmp(ident_buf->model, "FX", 2) &&
705 		    strncmp(ident_buf->model, "NEC", 3) &&
706 		    strncmp(ident_buf->model, "Pioneer", 7) &&
707 		    strncmp(ident_buf->model, "SHARP", 5)) {
708 			ata_bswap(ident_buf->model, sizeof(ident_buf->model));
709 			ata_bswap(ident_buf->revision, sizeof(ident_buf->revision));
710 			ata_bswap(ident_buf->serial, sizeof(ident_buf->serial));
711 		}
712 		ata_btrim(ident_buf->model, sizeof(ident_buf->model));
713 		ata_bpack(ident_buf->model, ident_buf->model, sizeof(ident_buf->model));
714 		ata_btrim(ident_buf->revision, sizeof(ident_buf->revision));
715 		ata_bpack(ident_buf->revision, ident_buf->revision, sizeof(ident_buf->revision));
716 		ata_btrim(ident_buf->serial, sizeof(ident_buf->serial));
717 		ata_bpack(ident_buf->serial, ident_buf->serial, sizeof(ident_buf->serial));
718 
719 		if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
720 			/* Check that it is the same device. */
721 			MD5_CTX context;
722 			u_int8_t digest[16];
723 
724 			MD5Init(&context);
725 			MD5Update(&context,
726 			    (unsigned char *)ident_buf->model,
727 			    sizeof(ident_buf->model));
728 			MD5Update(&context,
729 			    (unsigned char *)ident_buf->revision,
730 			    sizeof(ident_buf->revision));
731 			MD5Update(&context,
732 			    (unsigned char *)ident_buf->serial,
733 			    sizeof(ident_buf->serial));
734 			MD5Final(digest, &context);
735 			if (bcmp(digest, softc->digest, sizeof(digest))) {
736 				/* Device changed. */
737 				xpt_async(AC_LOST_DEVICE, path, NULL);
738 			}
739 		} else {
740 			/* Clean up from previous instance of this device */
741 			if (path->device->serial_num != NULL) {
742 				free(path->device->serial_num, M_CAMXPT);
743 				path->device->serial_num = NULL;
744 				path->device->serial_num_len = 0;
745 			}
746 			path->device->serial_num =
747 				(u_int8_t *)malloc((sizeof(ident_buf->serial) + 1),
748 					   M_CAMXPT, M_NOWAIT);
749 			if (path->device->serial_num != NULL) {
750 				bcopy(ident_buf->serial,
751 				      path->device->serial_num,
752 				      sizeof(ident_buf->serial));
753 				path->device->serial_num[sizeof(ident_buf->serial)]
754 				    = '\0';
755 				path->device->serial_num_len =
756 				    strlen(path->device->serial_num);
757 			}
758 
759 			path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
760 		}
761 		if (ident_buf->satacapabilities & ATA_SUPPORT_NCQ) {
762 			path->device->mintags = path->device->maxtags =
763 			    ATA_QUEUE_LEN(ident_buf->queue) + 1;
764 		}
765 		ata_find_quirk(path->device);
766 		if (path->device->mintags != 0 &&
767 		    path->bus->sim->max_tagged_dev_openings != 0) {
768 			/* Report SIM which tags are allowed. */
769 			bzero(&cts, sizeof(cts));
770 			xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
771 			cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
772 			cts.type = CTS_TYPE_CURRENT_SETTINGS;
773 			cts.xport_specific.sata.tags = path->device->maxtags;
774 			cts.xport_specific.sata.valid = CTS_SATA_VALID_TAGS;
775 			xpt_action((union ccb *)&cts);
776 			/* Reconfigure queues for tagged queueing. */
777 			xpt_start_tags(path);
778 		}
779 		ata_device_transport(path);
780 		PROBE_SET_ACTION(softc, PROBE_SETMODE);
781 		xpt_release_ccb(done_ccb);
782 		xpt_schedule(periph, priority);
783 		return;
784 	}
785 	case PROBE_SETMODE:
786 		if (path->device->protocol == PROTO_ATA) {
787 			PROBE_SET_ACTION(softc, PROBE_SET_MULTI);
788 		} else {
789 			PROBE_SET_ACTION(softc, PROBE_INQUIRY);
790 		}
791 		xpt_release_ccb(done_ccb);
792 		xpt_schedule(periph, priority);
793 		return;
794 	case PROBE_SET_MULTI:
795 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
796 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
797 			xpt_acquire_device(path->device);
798 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
799 			xpt_action(done_ccb);
800 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
801 			    done_ccb);
802 		}
803 		break;
804 	case PROBE_INQUIRY:
805 	case PROBE_FULL_INQUIRY:
806 	{
807 		struct scsi_inquiry_data *inq_buf;
808 		u_int8_t periph_qual, len;
809 
810 		path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
811 		inq_buf = &path->device->inq_data;
812 
813 		periph_qual = SID_QUAL(inq_buf);
814 
815 		if (periph_qual != SID_QUAL_LU_CONNECTED)
816 			break;
817 
818 		/*
819 		 * We conservatively request only
820 		 * SHORT_INQUIRY_LEN bytes of inquiry
821 		 * information during our first try
822 		 * at sending an INQUIRY. If the device
823 		 * has more information to give,
824 		 * perform a second request specifying
825 		 * the amount of information the device
826 		 * is willing to give.
827 		 */
828 		len = inq_buf->additional_length
829 		    + offsetof(struct scsi_inquiry_data, additional_length) + 1;
830 		if (softc->action == PROBE_INQUIRY
831 		    && len > SHORT_INQUIRY_LENGTH) {
832 			PROBE_SET_ACTION(softc, PROBE_FULL_INQUIRY);
833 			xpt_release_ccb(done_ccb);
834 			xpt_schedule(periph, priority);
835 			return;
836 		}
837 
838 		ata_device_transport(path);
839 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
840 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
841 			xpt_acquire_device(path->device);
842 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
843 			xpt_action(done_ccb);
844 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path, done_ccb);
845 		}
846 		break;
847 	}
848 	case PROBE_PM_PID:
849 		if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) == 0)
850 			bzero(ident_buf, sizeof(*ident_buf));
851 		softc->pm_pid = (done_ccb->ataio.res.lba_high << 24) +
852 		    (done_ccb->ataio.res.lba_mid << 16) +
853 		    (done_ccb->ataio.res.lba_low << 8) +
854 		    done_ccb->ataio.res.sector_count;
855 		((uint32_t *)ident_buf)[0] = softc->pm_pid;
856 		printf("PM Product ID: %08x\n", softc->pm_pid);
857 		snprintf(ident_buf->model, sizeof(ident_buf->model),
858 		    "Port Multiplier %08x", softc->pm_pid);
859 		PROBE_SET_ACTION(softc, PROBE_PM_PRV);
860 		xpt_release_ccb(done_ccb);
861 		xpt_schedule(periph, priority);
862 		return;
863 	case PROBE_PM_PRV:
864 		softc->pm_prv = (done_ccb->ataio.res.lba_high << 24) +
865 		    (done_ccb->ataio.res.lba_mid << 16) +
866 		    (done_ccb->ataio.res.lba_low << 8) +
867 		    done_ccb->ataio.res.sector_count;
868 		((uint32_t *)ident_buf)[1] = softc->pm_prv;
869 		printf("PM Revision: %08x\n", softc->pm_prv);
870 		snprintf(ident_buf->revision, sizeof(ident_buf->revision),
871 		    "%04x", softc->pm_prv);
872 		path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
873 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
874 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
875 			xpt_acquire_device(path->device);
876 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
877 			xpt_action(done_ccb);
878 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
879 			    done_ccb);
880 		} else {
881 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
882 			xpt_action(done_ccb);
883 			xpt_async(AC_SCSI_AEN, done_ccb->ccb_h.path, done_ccb);
884 		}
885 		break;
886 	case PROBE_INVALID:
887 		CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_INFO,
888 		    ("probedone: invalid action state\n"));
889 	default:
890 		break;
891 	}
892 done:
893 	xpt_release_ccb(done_ccb);
894 	done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
895 	TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe);
896 	done_ccb->ccb_h.status = CAM_REQ_CMP;
897 	done_ccb->ccb_h.ppriv_field1 = found;
898 	xpt_done(done_ccb);
899 	if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
900 		cam_periph_invalidate(periph);
901 		cam_periph_release_locked(periph);
902 	} else {
903 		probeschedule(periph);
904 	}
905 }
906 
907 static void
908 probecleanup(struct cam_periph *periph)
909 {
910 	free(periph->softc, M_CAMXPT);
911 }
912 
913 static void
914 ata_find_quirk(struct cam_ed *device)
915 {
916 	struct ata_quirk_entry *quirk;
917 	caddr_t	match;
918 
919 	match = cam_quirkmatch((caddr_t)&device->ident_data,
920 			       (caddr_t)ata_quirk_table,
921 			       ata_quirk_table_size,
922 			       sizeof(*ata_quirk_table), ata_identify_match);
923 
924 	if (match == NULL)
925 		panic("xpt_find_quirk: device didn't match wildcard entry!!");
926 
927 	quirk = (struct ata_quirk_entry *)match;
928 	device->quirk = quirk;
929 	if (quirk->quirks & CAM_QUIRK_MAXTAGS)
930 		device->mintags = device->maxtags = quirk->maxtags;
931 }
932 
933 typedef struct {
934 	union	ccb *request_ccb;
935 	struct 	ccb_pathinq *cpi;
936 	int	counter;
937 	int	found;
938 } ata_scan_bus_info;
939 
940 /*
941  * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
942  * As the scan progresses, xpt_scan_bus is used as the
943  * callback on completion function.
944  */
945 static void
946 ata_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
947 {
948 	struct	cam_path *path;
949 	ata_scan_bus_info *scan_info;
950 	union	ccb *work_ccb;
951 	cam_status status;
952 
953 	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
954 		  ("xpt_scan_bus\n"));
955 	switch (request_ccb->ccb_h.func_code) {
956 	case XPT_SCAN_BUS:
957 		/* Find out the characteristics of the bus */
958 		work_ccb = xpt_alloc_ccb_nowait();
959 		if (work_ccb == NULL) {
960 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
961 			xpt_done(request_ccb);
962 			return;
963 		}
964 		xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
965 			      request_ccb->ccb_h.pinfo.priority);
966 		work_ccb->ccb_h.func_code = XPT_PATH_INQ;
967 		xpt_action(work_ccb);
968 		if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
969 			request_ccb->ccb_h.status = work_ccb->ccb_h.status;
970 			xpt_free_ccb(work_ccb);
971 			xpt_done(request_ccb);
972 			return;
973 		}
974 
975 		/* Save some state for use while we probe for devices */
976 		scan_info = (ata_scan_bus_info *)
977 		    malloc(sizeof(ata_scan_bus_info), M_CAMXPT, M_NOWAIT);
978 		if (scan_info == NULL) {
979 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
980 			xpt_done(request_ccb);
981 			return;
982 		}
983 		scan_info->request_ccb = request_ccb;
984 		scan_info->cpi = &work_ccb->cpi;
985 		if (scan_info->cpi->transport == XPORT_ATA)
986 			scan_info->found = 0x0003;
987 		else
988 			scan_info->found = 0x8001;
989 		scan_info->counter = 0;
990 		/* If PM supported, probe it first. */
991 		if (scan_info->cpi->hba_inquiry & PI_SATAPM)
992 			scan_info->counter = 15;
993 
994 		work_ccb = xpt_alloc_ccb_nowait();
995 		if (work_ccb == NULL) {
996 			free(scan_info, M_CAMXPT);
997 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
998 			xpt_done(request_ccb);
999 			break;
1000 		}
1001 		goto scan_next;
1002 	case XPT_SCAN_LUN:
1003 		work_ccb = request_ccb;
1004 		/* Reuse the same CCB to query if a device was really found */
1005 		scan_info = (ata_scan_bus_info *)work_ccb->ccb_h.ppriv_ptr0;
1006 		/* Free the current request path- we're done with it. */
1007 		xpt_free_path(work_ccb->ccb_h.path);
1008 		/* If there is PMP... */
1009 		if (scan_info->counter == 15) {
1010 			if (work_ccb->ccb_h.ppriv_field1 != 0) {
1011 				/* everything else willbe probed by it */
1012 				scan_info->found = 0x8000;
1013 			} else {
1014 				struct ccb_trans_settings cts;
1015 
1016 				/* Report SIM that PM is absent. */
1017 				bzero(&cts, sizeof(cts));
1018 				xpt_setup_ccb(&cts.ccb_h,
1019 				    scan_info->request_ccb->ccb_h.path, 1);
1020 				cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1021 				cts.type = CTS_TYPE_CURRENT_SETTINGS;
1022 				cts.xport_specific.sata.pm_present = 0;
1023 				cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
1024 				xpt_action((union ccb *)&cts);
1025 			}
1026 		}
1027 take_next:
1028 		/* Take next device. Wrap from 15 (PM) to 0. */
1029 		scan_info->counter = (scan_info->counter + 1 ) & 0x0f;
1030 		if (scan_info->counter > scan_info->cpi->max_target -
1031 		    ((scan_info->cpi->hba_inquiry & PI_SATAPM) ? 1 : 0)) {
1032 			xpt_free_ccb(work_ccb);
1033 			xpt_free_ccb((union ccb *)scan_info->cpi);
1034 			request_ccb = scan_info->request_ccb;
1035 			free(scan_info, M_CAMXPT);
1036 			request_ccb->ccb_h.status = CAM_REQ_CMP;
1037 			xpt_done(request_ccb);
1038 			break;
1039 		}
1040 scan_next:
1041 		if ((scan_info->found & (1 << scan_info->counter)) == 0)
1042 			goto take_next;
1043 		status = xpt_create_path(&path, xpt_periph,
1044 		    scan_info->request_ccb->ccb_h.path_id,
1045 		    scan_info->counter, 0);
1046 		if (status != CAM_REQ_CMP) {
1047 			printf("xpt_scan_bus: xpt_create_path failed"
1048 			    " with status %#x, bus scan halted\n",
1049 			    status);
1050 			xpt_free_ccb(work_ccb);
1051 			xpt_free_ccb((union ccb *)scan_info->cpi);
1052 			request_ccb = scan_info->request_ccb;
1053 			free(scan_info, M_CAMXPT);
1054 			request_ccb->ccb_h.status = status;
1055 			xpt_done(request_ccb);
1056 			break;
1057 		}
1058 		xpt_setup_ccb(&work_ccb->ccb_h, path,
1059 		    scan_info->request_ccb->ccb_h.pinfo.priority);
1060 		work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1061 		work_ccb->ccb_h.cbfcnp = ata_scan_bus;
1062 		work_ccb->ccb_h.ppriv_ptr0 = scan_info;
1063 		work_ccb->crcn.flags = scan_info->request_ccb->crcn.flags;
1064 		xpt_action(work_ccb);
1065 		break;
1066 	default:
1067 		break;
1068 	}
1069 }
1070 
1071 static void
1072 ata_scan_lun(struct cam_periph *periph, struct cam_path *path,
1073 	     cam_flags flags, union ccb *request_ccb)
1074 {
1075 	struct ccb_pathinq cpi;
1076 	cam_status status;
1077 	struct cam_path *new_path;
1078 	struct cam_periph *old_periph;
1079 
1080 	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
1081 		  ("xpt_scan_lun\n"));
1082 
1083 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
1084 	cpi.ccb_h.func_code = XPT_PATH_INQ;
1085 	xpt_action((union ccb *)&cpi);
1086 
1087 	if (cpi.ccb_h.status != CAM_REQ_CMP) {
1088 		if (request_ccb != NULL) {
1089 			request_ccb->ccb_h.status = cpi.ccb_h.status;
1090 			xpt_done(request_ccb);
1091 		}
1092 		return;
1093 	}
1094 
1095 	if (request_ccb == NULL) {
1096 		request_ccb = malloc(sizeof(union ccb), M_CAMXPT, M_NOWAIT);
1097 		if (request_ccb == NULL) {
1098 			xpt_print(path, "xpt_scan_lun: can't allocate CCB, "
1099 			    "can't continue\n");
1100 			return;
1101 		}
1102 		new_path = malloc(sizeof(*new_path), M_CAMXPT, M_NOWAIT);
1103 		if (new_path == NULL) {
1104 			xpt_print(path, "xpt_scan_lun: can't allocate path, "
1105 			    "can't continue\n");
1106 			free(request_ccb, M_CAMXPT);
1107 			return;
1108 		}
1109 		status = xpt_compile_path(new_path, xpt_periph,
1110 					  path->bus->path_id,
1111 					  path->target->target_id,
1112 					  path->device->lun_id);
1113 
1114 		if (status != CAM_REQ_CMP) {
1115 			xpt_print(path, "xpt_scan_lun: can't compile path, "
1116 			    "can't continue\n");
1117 			free(request_ccb, M_CAMXPT);
1118 			free(new_path, M_CAMXPT);
1119 			return;
1120 		}
1121 		xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_NORMAL);
1122 		request_ccb->ccb_h.cbfcnp = xptscandone;
1123 		request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1124 		request_ccb->crcn.flags = flags;
1125 	}
1126 
1127 	if ((old_periph = cam_periph_find(path, "aprobe")) != NULL) {
1128 		probe_softc *softc;
1129 
1130 		softc = (probe_softc *)old_periph->softc;
1131 		TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
1132 				  periph_links.tqe);
1133 	} else {
1134 		status = cam_periph_alloc(proberegister, NULL, probecleanup,
1135 					  probestart, "aprobe",
1136 					  CAM_PERIPH_BIO,
1137 					  request_ccb->ccb_h.path, NULL, 0,
1138 					  request_ccb);
1139 
1140 		if (status != CAM_REQ_CMP) {
1141 			xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
1142 			    "returned an error, can't continue probe\n");
1143 			request_ccb->ccb_h.status = status;
1144 			xpt_done(request_ccb);
1145 		}
1146 	}
1147 }
1148 
1149 static void
1150 xptscandone(struct cam_periph *periph, union ccb *done_ccb)
1151 {
1152 	xpt_release_path(done_ccb->ccb_h.path);
1153 	free(done_ccb->ccb_h.path, M_CAMXPT);
1154 	free(done_ccb, M_CAMXPT);
1155 }
1156 
1157 static struct cam_ed *
1158 ata_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
1159 {
1160 	struct cam_path path;
1161 	struct ata_quirk_entry *quirk;
1162 	struct cam_ed *device;
1163 	struct cam_ed *cur_device;
1164 
1165 	device = xpt_alloc_device(bus, target, lun_id);
1166 	if (device == NULL)
1167 		return (NULL);
1168 
1169 	/*
1170 	 * Take the default quirk entry until we have inquiry
1171 	 * data and can determine a better quirk to use.
1172 	 */
1173 	quirk = &ata_quirk_table[ata_quirk_table_size - 1];
1174 	device->quirk = (void *)quirk;
1175 	device->mintags = 0;
1176 	device->maxtags = 0;
1177 	bzero(&device->inq_data, sizeof(device->inq_data));
1178 	device->inq_flags = 0;
1179 	device->queue_flags = 0;
1180 	device->serial_num = NULL;
1181 	device->serial_num_len = 0;
1182 
1183 	/*
1184 	 * XXX should be limited by number of CCBs this bus can
1185 	 * do.
1186 	 */
1187 	bus->sim->max_ccbs += device->ccbq.devq_openings;
1188 	/* Insertion sort into our target's device list */
1189 	cur_device = TAILQ_FIRST(&target->ed_entries);
1190 	while (cur_device != NULL && cur_device->lun_id < lun_id)
1191 		cur_device = TAILQ_NEXT(cur_device, links);
1192 	if (cur_device != NULL) {
1193 		TAILQ_INSERT_BEFORE(cur_device, device, links);
1194 	} else {
1195 		TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
1196 	}
1197 	target->generation++;
1198 	if (lun_id != CAM_LUN_WILDCARD) {
1199 		xpt_compile_path(&path,
1200 				 NULL,
1201 				 bus->path_id,
1202 				 target->target_id,
1203 				 lun_id);
1204 		ata_device_transport(&path);
1205 		xpt_release_path(&path);
1206 	}
1207 
1208 	return (device);
1209 }
1210 
1211 static void
1212 ata_device_transport(struct cam_path *path)
1213 {
1214 	struct ccb_pathinq cpi;
1215 	struct ccb_trans_settings cts;
1216 	struct scsi_inquiry_data *inq_buf = NULL;
1217 	struct ata_params *ident_buf = NULL;
1218 
1219 	/* Get transport information from the SIM */
1220 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
1221 	cpi.ccb_h.func_code = XPT_PATH_INQ;
1222 	xpt_action((union ccb *)&cpi);
1223 
1224 	path->device->transport = cpi.transport;
1225 	if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
1226 		inq_buf = &path->device->inq_data;
1227 	if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) != 0)
1228 		ident_buf = &path->device->ident_data;
1229 	if (path->device->protocol == PROTO_ATA) {
1230 		path->device->protocol_version = ident_buf ?
1231 		    ata_version(ident_buf->version_major) : cpi.protocol_version;
1232 	} else if (path->device->protocol == PROTO_SCSI) {
1233 		path->device->protocol_version = inq_buf ?
1234 		    SID_ANSI_REV(inq_buf) : cpi.protocol_version;
1235 	}
1236 	path->device->transport_version = ident_buf ?
1237 	    ata_version(ident_buf->version_major) : cpi.transport_version;
1238 
1239 	/* Tell the controller what we think */
1240 	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
1241 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1242 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
1243 	cts.transport = path->device->transport;
1244 	cts.transport_version = path->device->transport_version;
1245 	cts.protocol = path->device->protocol;
1246 	cts.protocol_version = path->device->protocol_version;
1247 	cts.proto_specific.valid = 0;
1248 	cts.xport_specific.valid = 0;
1249 	xpt_action((union ccb *)&cts);
1250 }
1251 
1252 static void
1253 ata_action(union ccb *start_ccb)
1254 {
1255 
1256 	switch (start_ccb->ccb_h.func_code) {
1257 	case XPT_SET_TRAN_SETTINGS:
1258 	{
1259 		ata_set_transfer_settings(&start_ccb->cts,
1260 					   start_ccb->ccb_h.path->device,
1261 					   /*async_update*/FALSE);
1262 		break;
1263 	}
1264 	case XPT_SCAN_BUS:
1265 		ata_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
1266 		break;
1267 	case XPT_SCAN_LUN:
1268 		ata_scan_lun(start_ccb->ccb_h.path->periph,
1269 			      start_ccb->ccb_h.path, start_ccb->crcn.flags,
1270 			      start_ccb);
1271 		break;
1272 	case XPT_GET_TRAN_SETTINGS:
1273 	{
1274 		struct cam_sim *sim;
1275 
1276 		sim = start_ccb->ccb_h.path->bus->sim;
1277 		(*(sim->sim_action))(sim, start_ccb);
1278 		break;
1279 	}
1280 	default:
1281 		xpt_action_default(start_ccb);
1282 		break;
1283 	}
1284 }
1285 
1286 static void
1287 ata_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
1288 			   int async_update)
1289 {
1290 	struct	ccb_pathinq cpi;
1291 	struct	ccb_trans_settings cur_cts;
1292 	struct	ccb_trans_settings_scsi *scsi;
1293 	struct	ccb_trans_settings_scsi *cur_scsi;
1294 	struct	cam_sim *sim;
1295 	struct	scsi_inquiry_data *inq_data;
1296 
1297 	if (device == NULL) {
1298 		cts->ccb_h.status = CAM_PATH_INVALID;
1299 		xpt_done((union ccb *)cts);
1300 		return;
1301 	}
1302 
1303 	if (cts->protocol == PROTO_UNKNOWN
1304 	 || cts->protocol == PROTO_UNSPECIFIED) {
1305 		cts->protocol = device->protocol;
1306 		cts->protocol_version = device->protocol_version;
1307 	}
1308 
1309 	if (cts->protocol_version == PROTO_VERSION_UNKNOWN
1310 	 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
1311 		cts->protocol_version = device->protocol_version;
1312 
1313 	if (cts->protocol != device->protocol) {
1314 		xpt_print(cts->ccb_h.path, "Uninitialized Protocol %x:%x?\n",
1315 		       cts->protocol, device->protocol);
1316 		cts->protocol = device->protocol;
1317 	}
1318 
1319 	if (cts->protocol_version > device->protocol_version) {
1320 		if (bootverbose) {
1321 			xpt_print(cts->ccb_h.path, "Down reving Protocol "
1322 			    "Version from %d to %d?\n", cts->protocol_version,
1323 			    device->protocol_version);
1324 		}
1325 		cts->protocol_version = device->protocol_version;
1326 	}
1327 
1328 	if (cts->transport == XPORT_UNKNOWN
1329 	 || cts->transport == XPORT_UNSPECIFIED) {
1330 		cts->transport = device->transport;
1331 		cts->transport_version = device->transport_version;
1332 	}
1333 
1334 	if (cts->transport_version == XPORT_VERSION_UNKNOWN
1335 	 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
1336 		cts->transport_version = device->transport_version;
1337 
1338 	if (cts->transport != device->transport) {
1339 		xpt_print(cts->ccb_h.path, "Uninitialized Transport %x:%x?\n",
1340 		    cts->transport, device->transport);
1341 		cts->transport = device->transport;
1342 	}
1343 
1344 	if (cts->transport_version > device->transport_version) {
1345 		if (bootverbose) {
1346 			xpt_print(cts->ccb_h.path, "Down reving Transport "
1347 			    "Version from %d to %d?\n", cts->transport_version,
1348 			    device->transport_version);
1349 		}
1350 		cts->transport_version = device->transport_version;
1351 	}
1352 
1353 	sim = cts->ccb_h.path->bus->sim;
1354 
1355 	/*
1356 	 * Nothing more of interest to do unless
1357 	 * this is a device connected via the
1358 	 * SCSI protocol.
1359 	 */
1360 	if (cts->protocol != PROTO_SCSI) {
1361 		if (async_update == FALSE)
1362 			(*(sim->sim_action))(sim, (union ccb *)cts);
1363 		return;
1364 	}
1365 
1366 	inq_data = &device->inq_data;
1367 	scsi = &cts->proto_specific.scsi;
1368 	xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NORMAL);
1369 	cpi.ccb_h.func_code = XPT_PATH_INQ;
1370 	xpt_action((union ccb *)&cpi);
1371 
1372 	/* SCSI specific sanity checking */
1373 	if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
1374 	 || (INQ_DATA_TQ_ENABLED(inq_data)) == 0
1375 	 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
1376 	 || (device->mintags == 0)) {
1377 		/*
1378 		 * Can't tag on hardware that doesn't support tags,
1379 		 * doesn't have it enabled, or has broken tag support.
1380 		 */
1381 		scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1382 	}
1383 
1384 	if (async_update == FALSE) {
1385 		/*
1386 		 * Perform sanity checking against what the
1387 		 * controller and device can do.
1388 		 */
1389 		xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NORMAL);
1390 		cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1391 		cur_cts.type = cts->type;
1392 		xpt_action((union ccb *)&cur_cts);
1393 		if ((cur_cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1394 			return;
1395 		}
1396 		cur_scsi = &cur_cts.proto_specific.scsi;
1397 		if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
1398 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1399 			scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
1400 		}
1401 		if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
1402 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1403 	}
1404 
1405 	if (cts->type == CTS_TYPE_CURRENT_SETTINGS
1406 	 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
1407 		int device_tagenb;
1408 
1409 		/*
1410 		 * If we are transitioning from tags to no-tags or
1411 		 * vice-versa, we need to carefully freeze and restart
1412 		 * the queue so that we don't overlap tagged and non-tagged
1413 		 * commands.  We also temporarily stop tags if there is
1414 		 * a change in transfer negotiation settings to allow
1415 		 * "tag-less" negotiation.
1416 		 */
1417 		if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
1418 		 || (device->inq_flags & SID_CmdQue) != 0)
1419 			device_tagenb = TRUE;
1420 		else
1421 			device_tagenb = FALSE;
1422 
1423 		if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
1424 		  && device_tagenb == FALSE)
1425 		 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
1426 		  && device_tagenb == TRUE)) {
1427 
1428 			if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
1429 				/*
1430 				 * Delay change to use tags until after a
1431 				 * few commands have gone to this device so
1432 				 * the controller has time to perform transfer
1433 				 * negotiations without tagged messages getting
1434 				 * in the way.
1435 				 */
1436 				device->tag_delay_count = CAM_TAG_DELAY_COUNT;
1437 				device->flags |= CAM_DEV_TAG_AFTER_COUNT;
1438 			} else {
1439 				xpt_stop_tags(cts->ccb_h.path);
1440 			}
1441 		}
1442 	}
1443 	if (async_update == FALSE)
1444 		(*(sim->sim_action))(sim, (union ccb *)cts);
1445 }
1446 
1447 /*
1448  * Handle any per-device event notifications that require action by the XPT.
1449  */
1450 static void
1451 ata_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
1452 	      struct cam_ed *device, void *async_arg)
1453 {
1454 	cam_status status;
1455 	struct cam_path newpath;
1456 
1457 	/*
1458 	 * We only need to handle events for real devices.
1459 	 */
1460 	if (target->target_id == CAM_TARGET_WILDCARD
1461 	 || device->lun_id == CAM_LUN_WILDCARD)
1462 		return;
1463 
1464 	/*
1465 	 * We need our own path with wildcards expanded to
1466 	 * handle certain types of events.
1467 	 */
1468 	if ((async_code == AC_SENT_BDR)
1469 	 || (async_code == AC_BUS_RESET)
1470 	 || (async_code == AC_INQ_CHANGED))
1471 		status = xpt_compile_path(&newpath, NULL,
1472 					  bus->path_id,
1473 					  target->target_id,
1474 					  device->lun_id);
1475 	else
1476 		status = CAM_REQ_CMP_ERR;
1477 
1478 	if (status == CAM_REQ_CMP) {
1479 		if (async_code == AC_INQ_CHANGED) {
1480 			/*
1481 			 * We've sent a start unit command, or
1482 			 * something similar to a device that
1483 			 * may have caused its inquiry data to
1484 			 * change. So we re-scan the device to
1485 			 * refresh the inquiry data for it.
1486 			 */
1487 			ata_scan_lun(newpath.periph, &newpath,
1488 				     CAM_EXPECT_INQ_CHANGE, NULL);
1489 		}
1490 		xpt_release_path(&newpath);
1491 	} else if (async_code == AC_LOST_DEVICE &&
1492 	    (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
1493 		device->flags |= CAM_DEV_UNCONFIGURED;
1494 		xpt_release_device(device);
1495 	} else if (async_code == AC_TRANSFER_NEG) {
1496 		struct ccb_trans_settings *settings;
1497 
1498 		settings = (struct ccb_trans_settings *)async_arg;
1499 		ata_set_transfer_settings(settings, device,
1500 					  /*async_update*/TRUE);
1501 	}
1502 }
1503 
1504