xref: /freebsd/sys/cam/ata/ata_xpt.c (revision c8039fc6)
152c9ce25SScott Long /*-
252c9ce25SScott Long  * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org>
352c9ce25SScott Long  * All rights reserved.
452c9ce25SScott Long  *
552c9ce25SScott Long  * Redistribution and use in source and binary forms, with or without
652c9ce25SScott Long  * modification, are permitted provided that the following conditions
752c9ce25SScott Long  * are met:
852c9ce25SScott Long  * 1. Redistributions of source code must retain the above copyright
952c9ce25SScott Long  *    notice, this list of conditions and the following disclaimer,
1052c9ce25SScott Long  *    without modification, immediately at the beginning of the file.
1152c9ce25SScott Long  * 2. Redistributions in binary form must reproduce the above copyright
1252c9ce25SScott Long  *    notice, this list of conditions and the following disclaimer in the
1352c9ce25SScott Long  *    documentation and/or other materials provided with the distribution.
1452c9ce25SScott Long  *
1552c9ce25SScott Long  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1652c9ce25SScott Long  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1752c9ce25SScott Long  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1852c9ce25SScott Long  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1952c9ce25SScott Long  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2052c9ce25SScott Long  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2152c9ce25SScott Long  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2252c9ce25SScott Long  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2352c9ce25SScott Long  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2452c9ce25SScott Long  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2552c9ce25SScott Long  */
2652c9ce25SScott Long 
2752c9ce25SScott Long #include <sys/cdefs.h>
2852c9ce25SScott Long __FBSDID("$FreeBSD$");
2952c9ce25SScott Long 
3052c9ce25SScott Long #include <sys/param.h>
3152c9ce25SScott Long #include <sys/bus.h>
3252c9ce25SScott Long #include <sys/endian.h>
3352c9ce25SScott Long #include <sys/systm.h>
3452c9ce25SScott Long #include <sys/types.h>
3552c9ce25SScott Long #include <sys/malloc.h>
3652c9ce25SScott Long #include <sys/kernel.h>
3752c9ce25SScott Long #include <sys/time.h>
3852c9ce25SScott Long #include <sys/conf.h>
3952c9ce25SScott Long #include <sys/fcntl.h>
4052c9ce25SScott Long #include <sys/md5.h>
4152c9ce25SScott Long #include <sys/interrupt.h>
4252c9ce25SScott Long #include <sys/sbuf.h>
4352c9ce25SScott Long 
4452c9ce25SScott Long #include <sys/lock.h>
4552c9ce25SScott Long #include <sys/mutex.h>
4652c9ce25SScott Long #include <sys/sysctl.h>
4752c9ce25SScott Long 
4852c9ce25SScott Long #ifdef PC98
4952c9ce25SScott Long #include <pc98/pc98/pc98_machdep.h>	/* geometry translation */
5052c9ce25SScott Long #endif
5152c9ce25SScott Long 
5252c9ce25SScott Long #include <cam/cam.h>
5352c9ce25SScott Long #include <cam/cam_ccb.h>
5452c9ce25SScott Long #include <cam/cam_queue.h>
5552c9ce25SScott Long #include <cam/cam_periph.h>
5652c9ce25SScott Long #include <cam/cam_sim.h>
5752c9ce25SScott Long #include <cam/cam_xpt.h>
5852c9ce25SScott Long #include <cam/cam_xpt_sim.h>
5952c9ce25SScott Long #include <cam/cam_xpt_periph.h>
6052c9ce25SScott Long #include <cam/cam_xpt_internal.h>
6152c9ce25SScott Long #include <cam/cam_debug.h>
6252c9ce25SScott Long 
6352c9ce25SScott Long #include <cam/scsi/scsi_all.h>
6452c9ce25SScott Long #include <cam/scsi/scsi_message.h>
6552c9ce25SScott Long #include <cam/ata/ata_all.h>
6652c9ce25SScott Long #include <machine/stdarg.h>	/* for xpt_print below */
6752c9ce25SScott Long #include "opt_cam.h"
6852c9ce25SScott Long 
6930a4094fSAlexander Motin struct ata_quirk_entry {
7052c9ce25SScott Long 	struct scsi_inquiry_pattern inq_pat;
7152c9ce25SScott Long 	u_int8_t quirks;
7230a4094fSAlexander Motin #define	CAM_QUIRK_MAXTAGS	0x01
7352c9ce25SScott Long 	u_int maxtags;
7452c9ce25SScott Long };
7552c9ce25SScott Long 
7652c9ce25SScott Long static periph_init_t probe_periph_init;
7752c9ce25SScott Long 
7852c9ce25SScott Long static struct periph_driver probe_driver =
7952c9ce25SScott Long {
80f09f8e3eSAlexander Motin 	probe_periph_init, "aprobe",
811e637ba6SAlexander Motin 	TAILQ_HEAD_INITIALIZER(probe_driver.units), /* generation */ 0,
821e637ba6SAlexander Motin 	CAM_PERIPH_DRV_EARLY
8352c9ce25SScott Long };
8452c9ce25SScott Long 
85f09f8e3eSAlexander Motin PERIPHDRIVER_DECLARE(aprobe, probe_driver);
8652c9ce25SScott Long 
8752c9ce25SScott Long typedef enum {
8852c9ce25SScott Long 	PROBE_RESET,
8952c9ce25SScott Long 	PROBE_IDENTIFY,
9052c9ce25SScott Long 	PROBE_SETMODE,
911e637ba6SAlexander Motin 	PROBE_SET_MULTI,
9252c9ce25SScott Long 	PROBE_INQUIRY,
9352c9ce25SScott Long 	PROBE_FULL_INQUIRY,
9452c9ce25SScott Long 	PROBE_PM_PID,
9552c9ce25SScott Long 	PROBE_PM_PRV,
9652c9ce25SScott Long 	PROBE_INVALID
9752c9ce25SScott Long } probe_action;
9852c9ce25SScott Long 
9952c9ce25SScott Long static char *probe_action_text[] = {
10052c9ce25SScott Long 	"PROBE_RESET",
10152c9ce25SScott Long 	"PROBE_IDENTIFY",
10252c9ce25SScott Long 	"PROBE_SETMODE",
1031e637ba6SAlexander Motin 	"PROBE_SET_MULTI",
10452c9ce25SScott Long 	"PROBE_INQUIRY",
10552c9ce25SScott Long 	"PROBE_FULL_INQUIRY",
10652c9ce25SScott Long 	"PROBE_PM_PID",
10752c9ce25SScott Long 	"PROBE_PM_PRV",
10852c9ce25SScott Long 	"PROBE_INVALID"
10952c9ce25SScott Long };
11052c9ce25SScott Long 
11152c9ce25SScott Long #define PROBE_SET_ACTION(softc, newaction)	\
11252c9ce25SScott Long do {									\
11352c9ce25SScott Long 	char **text;							\
11452c9ce25SScott Long 	text = probe_action_text;					\
11552c9ce25SScott Long 	CAM_DEBUG((softc)->periph->path, CAM_DEBUG_INFO,		\
11652c9ce25SScott Long 	    ("Probe %s to %s\n", text[(softc)->action],			\
11752c9ce25SScott Long 	    text[(newaction)]));					\
11852c9ce25SScott Long 	(softc)->action = (newaction);					\
11952c9ce25SScott Long } while(0)
12052c9ce25SScott Long 
12152c9ce25SScott Long typedef enum {
12252c9ce25SScott Long 	PROBE_NO_ANNOUNCE	= 0x04
12352c9ce25SScott Long } probe_flags;
12452c9ce25SScott Long 
12552c9ce25SScott Long typedef struct {
12652c9ce25SScott Long 	TAILQ_HEAD(, ccb_hdr) request_ccbs;
12752c9ce25SScott Long 	probe_action	action;
12852c9ce25SScott Long 	union ccb	saved_ccb;
12952c9ce25SScott Long 	probe_flags	flags;
13052c9ce25SScott Long 	u_int8_t	digest[16];
13152c9ce25SScott Long 	uint32_t	pm_pid;
13252c9ce25SScott Long 	uint32_t	pm_prv;
13352c9ce25SScott Long 	struct cam_periph *periph;
13452c9ce25SScott Long } probe_softc;
13552c9ce25SScott Long 
13630a4094fSAlexander Motin static struct ata_quirk_entry ata_quirk_table[] =
13752c9ce25SScott Long {
13852c9ce25SScott Long 	{
13952c9ce25SScott Long 		/* Default tagged queuing parameters for all devices */
14052c9ce25SScott Long 		{
14152c9ce25SScott Long 		  T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
14252c9ce25SScott Long 		  /*vendor*/"*", /*product*/"*", /*revision*/"*"
14352c9ce25SScott Long 		},
14430a4094fSAlexander Motin 		/*quirks*/0, /*maxtags*/0
14552c9ce25SScott Long 	},
14652c9ce25SScott Long };
14752c9ce25SScott Long 
14830a4094fSAlexander Motin static const int ata_quirk_table_size =
14930a4094fSAlexander Motin 	sizeof(ata_quirk_table) / sizeof(*ata_quirk_table);
15052c9ce25SScott Long 
15152c9ce25SScott Long static cam_status	proberegister(struct cam_periph *periph,
15252c9ce25SScott Long 				      void *arg);
15352c9ce25SScott Long static void	 probeschedule(struct cam_periph *probe_periph);
15452c9ce25SScott Long static void	 probestart(struct cam_periph *periph, union ccb *start_ccb);
15552c9ce25SScott Long //static void	 proberequestdefaultnegotiation(struct cam_periph *periph);
15652c9ce25SScott Long //static int       proberequestbackoff(struct cam_periph *periph,
15752c9ce25SScott Long //				     struct cam_ed *device);
15852c9ce25SScott Long static void	 probedone(struct cam_periph *periph, union ccb *done_ccb);
15952c9ce25SScott Long static void	 probecleanup(struct cam_periph *periph);
16030a4094fSAlexander Motin static void	 ata_find_quirk(struct cam_ed *device);
16152c9ce25SScott Long static void	 ata_scan_bus(struct cam_periph *periph, union ccb *ccb);
16252c9ce25SScott Long static void	 ata_scan_lun(struct cam_periph *periph,
16352c9ce25SScott Long 			       struct cam_path *path, cam_flags flags,
16452c9ce25SScott Long 			       union ccb *ccb);
16552c9ce25SScott Long static void	 xptscandone(struct cam_periph *periph, union ccb *done_ccb);
16652c9ce25SScott Long static struct cam_ed *
16752c9ce25SScott Long 		 ata_alloc_device(struct cam_eb *bus, struct cam_et *target,
16852c9ce25SScott Long 				   lun_id_t lun_id);
16952c9ce25SScott Long static void	 ata_device_transport(struct cam_path *path);
17030a4094fSAlexander Motin static void	 ata_set_transfer_settings(struct ccb_trans_settings *cts,
17152c9ce25SScott Long 					    struct cam_ed *device,
17252c9ce25SScott Long 					    int async_update);
17352c9ce25SScott Long static void	 ata_dev_async(u_int32_t async_code,
17452c9ce25SScott Long 				struct cam_eb *bus,
17552c9ce25SScott Long 				struct cam_et *target,
17652c9ce25SScott Long 				struct cam_ed *device,
17752c9ce25SScott Long 				void *async_arg);
17852c9ce25SScott Long static void	 ata_action(union ccb *start_ccb);
17952c9ce25SScott Long 
18052c9ce25SScott Long static struct xpt_xport ata_xport = {
18152c9ce25SScott Long 	.alloc_device = ata_alloc_device,
18252c9ce25SScott Long 	.action = ata_action,
18352c9ce25SScott Long 	.async = ata_dev_async,
18452c9ce25SScott Long };
18552c9ce25SScott Long 
18652c9ce25SScott Long struct xpt_xport *
18752c9ce25SScott Long ata_get_xport(void)
18852c9ce25SScott Long {
18952c9ce25SScott Long 	return (&ata_xport);
19052c9ce25SScott Long }
19152c9ce25SScott Long 
19252c9ce25SScott Long static void
19352c9ce25SScott Long probe_periph_init()
19452c9ce25SScott Long {
19552c9ce25SScott Long }
19652c9ce25SScott Long 
19752c9ce25SScott Long static cam_status
19852c9ce25SScott Long proberegister(struct cam_periph *periph, void *arg)
19952c9ce25SScott Long {
20052c9ce25SScott Long 	union ccb *request_ccb;	/* CCB representing the probe request */
20152c9ce25SScott Long 	cam_status status;
20252c9ce25SScott Long 	probe_softc *softc;
20352c9ce25SScott Long 
20452c9ce25SScott Long 	request_ccb = (union ccb *)arg;
20552c9ce25SScott Long 	if (periph == NULL) {
20652c9ce25SScott Long 		printf("proberegister: periph was NULL!!\n");
20752c9ce25SScott Long 		return(CAM_REQ_CMP_ERR);
20852c9ce25SScott Long 	}
20952c9ce25SScott Long 
21052c9ce25SScott Long 	if (request_ccb == NULL) {
21152c9ce25SScott Long 		printf("proberegister: no probe CCB, "
21252c9ce25SScott Long 		       "can't register device\n");
21352c9ce25SScott Long 		return(CAM_REQ_CMP_ERR);
21452c9ce25SScott Long 	}
21552c9ce25SScott Long 
21652c9ce25SScott Long 	softc = (probe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_NOWAIT);
21752c9ce25SScott Long 
21852c9ce25SScott Long 	if (softc == NULL) {
21952c9ce25SScott Long 		printf("proberegister: Unable to probe new device. "
22052c9ce25SScott Long 		       "Unable to allocate softc\n");
22152c9ce25SScott Long 		return(CAM_REQ_CMP_ERR);
22252c9ce25SScott Long 	}
22352c9ce25SScott Long 	TAILQ_INIT(&softc->request_ccbs);
22452c9ce25SScott Long 	TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
22552c9ce25SScott Long 			  periph_links.tqe);
22652c9ce25SScott Long 	softc->flags = 0;
22752c9ce25SScott Long 	periph->softc = softc;
22852c9ce25SScott Long 	softc->periph = periph;
22952c9ce25SScott Long 	softc->action = PROBE_INVALID;
23052c9ce25SScott Long 	status = cam_periph_acquire(periph);
23152c9ce25SScott Long 	if (status != CAM_REQ_CMP) {
23252c9ce25SScott Long 		return (status);
23352c9ce25SScott Long 	}
23452c9ce25SScott Long 
23552c9ce25SScott Long 
23652c9ce25SScott Long 	/*
23752c9ce25SScott Long 	 * Ensure we've waited at least a bus settle
23852c9ce25SScott Long 	 * delay before attempting to probe the device.
23952c9ce25SScott Long 	 * For HBAs that don't do bus resets, this won't make a difference.
24052c9ce25SScott Long 	 */
24152c9ce25SScott Long 	cam_periph_freeze_after_event(periph, &periph->path->bus->last_reset,
24252c9ce25SScott Long 				      scsi_delay);
24352c9ce25SScott Long 	probeschedule(periph);
24452c9ce25SScott Long 	return(CAM_REQ_CMP);
24552c9ce25SScott Long }
24652c9ce25SScott Long 
24752c9ce25SScott Long static void
24852c9ce25SScott Long probeschedule(struct cam_periph *periph)
24952c9ce25SScott Long {
25052c9ce25SScott Long 	struct ccb_pathinq cpi;
25152c9ce25SScott Long 	union ccb *ccb;
25252c9ce25SScott Long 	probe_softc *softc;
25352c9ce25SScott Long 
25452c9ce25SScott Long 	softc = (probe_softc *)periph->softc;
25552c9ce25SScott Long 	ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
25652c9ce25SScott Long 
257bbfa4aa1SAlexander Motin 	xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
25852c9ce25SScott Long 	cpi.ccb_h.func_code = XPT_PATH_INQ;
25952c9ce25SScott Long 	xpt_action((union ccb *)&cpi);
26052c9ce25SScott Long 
26165d0fb03SAlexander Motin 	if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) ||
26265d0fb03SAlexander Motin 	    periph->path->device->protocol == PROTO_SATAPM)
26352c9ce25SScott Long 		PROBE_SET_ACTION(softc, PROBE_RESET);
26452c9ce25SScott Long 	else
26552c9ce25SScott Long 		PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
26652c9ce25SScott Long 
26752c9ce25SScott Long 	if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
26852c9ce25SScott Long 		softc->flags |= PROBE_NO_ANNOUNCE;
26952c9ce25SScott Long 	else
27052c9ce25SScott Long 		softc->flags &= ~PROBE_NO_ANNOUNCE;
27152c9ce25SScott Long 
27252c9ce25SScott Long 	xpt_schedule(periph, ccb->ccb_h.pinfo.priority);
27352c9ce25SScott Long }
27452c9ce25SScott Long 
27552c9ce25SScott Long static void
27652c9ce25SScott Long probestart(struct cam_periph *periph, union ccb *start_ccb)
27752c9ce25SScott Long {
278c8039fc6SAlexander Motin 	struct ccb_trans_settings cts;
27952c9ce25SScott Long 	struct ccb_ataio *ataio;
28052c9ce25SScott Long 	struct ccb_scsiio *csio;
28152c9ce25SScott Long 	probe_softc *softc;
2821e637ba6SAlexander Motin 	struct cam_path *path;
2831e637ba6SAlexander Motin 	struct ata_params *ident_buf;
28452c9ce25SScott Long 
28552c9ce25SScott Long 	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
28652c9ce25SScott Long 
28752c9ce25SScott Long 	softc = (probe_softc *)periph->softc;
2881e637ba6SAlexander Motin 	path = start_ccb->ccb_h.path;
28952c9ce25SScott Long 	ataio = &start_ccb->ataio;
29052c9ce25SScott Long 	csio = &start_ccb->csio;
2911e637ba6SAlexander Motin 	ident_buf = &periph->path->device->ident_data;
29252c9ce25SScott Long 
29352c9ce25SScott Long 	switch (softc->action) {
29452c9ce25SScott Long 	case PROBE_RESET:
29552c9ce25SScott Long 		cam_fill_ataio(ataio,
29652c9ce25SScott Long 		      0,
29752c9ce25SScott Long 		      probedone,
29852c9ce25SScott Long 		      /*flags*/CAM_DIR_NONE,
29965d0fb03SAlexander Motin 		      0,
30052c9ce25SScott Long 		      /*data_ptr*/NULL,
30152c9ce25SScott Long 		      /*dxfer_len*/0,
30252c9ce25SScott Long 		      (start_ccb->ccb_h.target_id == 15 ? 3 : 15) * 1000);
30352c9ce25SScott Long 		ata_reset_cmd(ataio);
30452c9ce25SScott Long 		break;
30552c9ce25SScott Long 	case PROBE_IDENTIFY:
30652c9ce25SScott Long 		if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
30752c9ce25SScott Long 			/* Prepare check that it is the same device. */
30852c9ce25SScott Long 			MD5_CTX context;
30952c9ce25SScott Long 
31052c9ce25SScott Long 			MD5Init(&context);
31152c9ce25SScott Long 			MD5Update(&context,
31252c9ce25SScott Long 			    (unsigned char *)ident_buf->model,
31352c9ce25SScott Long 			    sizeof(ident_buf->model));
31452c9ce25SScott Long 			MD5Update(&context,
31552c9ce25SScott Long 			    (unsigned char *)ident_buf->revision,
31652c9ce25SScott Long 			    sizeof(ident_buf->revision));
31752c9ce25SScott Long 			MD5Update(&context,
31852c9ce25SScott Long 			    (unsigned char *)ident_buf->serial,
31952c9ce25SScott Long 			    sizeof(ident_buf->serial));
32052c9ce25SScott Long 			MD5Final(softc->digest, &context);
32152c9ce25SScott Long 		}
32252c9ce25SScott Long 		cam_fill_ataio(ataio,
32352c9ce25SScott Long 		      1,
32452c9ce25SScott Long 		      probedone,
32552c9ce25SScott Long 		      /*flags*/CAM_DIR_IN,
32665d0fb03SAlexander Motin 		      0,
32752c9ce25SScott Long 		      /*data_ptr*/(u_int8_t *)ident_buf,
32852c9ce25SScott Long 		      /*dxfer_len*/sizeof(struct ata_params),
32952c9ce25SScott Long 		      30 * 1000);
33052c9ce25SScott Long 		if (periph->path->device->protocol == PROTO_ATA)
3317606b445SAlexander Motin 			ata_28bit_cmd(ataio, ATA_ATA_IDENTIFY, 0, 0, 0);
33252c9ce25SScott Long 		else
3337606b445SAlexander Motin 			ata_28bit_cmd(ataio, ATA_ATAPI_IDENTIFY, 0, 0, 0);
33452c9ce25SScott Long 		break;
33552c9ce25SScott Long 	case PROBE_SETMODE:
336c8039fc6SAlexander Motin 	{
337c8039fc6SAlexander Motin 		int mode, wantmode;
338c8039fc6SAlexander Motin 
339c8039fc6SAlexander Motin 		mode = 0;
340c8039fc6SAlexander Motin 		/* Fetch user modes from SIM. */
341c8039fc6SAlexander Motin 		bzero(&cts, sizeof(cts));
342c8039fc6SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
343c8039fc6SAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
344c8039fc6SAlexander Motin 		cts.type = CTS_TYPE_USER_SETTINGS;
345c8039fc6SAlexander Motin 		xpt_action((union ccb *)&cts);
346c8039fc6SAlexander Motin 		if (path->device->transport == XPORT_ATA) {
347c8039fc6SAlexander Motin 			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_MODE)
348c8039fc6SAlexander Motin 				mode = cts.xport_specific.ata.mode;
349c8039fc6SAlexander Motin 		} else {
350c8039fc6SAlexander Motin 			if (cts.xport_specific.ata.valid & CTS_SATA_VALID_MODE)
351c8039fc6SAlexander Motin 				mode = cts.xport_specific.sata.mode;
352c8039fc6SAlexander Motin 		}
353c8039fc6SAlexander Motin negotiate:
354c8039fc6SAlexander Motin 		/* Honor device capabilities. */
355c8039fc6SAlexander Motin 		wantmode = mode = ata_max_mode(ident_buf, mode);
356c8039fc6SAlexander Motin 		/* Report modes to SIM. */
357c8039fc6SAlexander Motin 		bzero(&cts, sizeof(cts));
358c8039fc6SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
359c8039fc6SAlexander Motin 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
360c8039fc6SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
361c8039fc6SAlexander Motin 		if (path->device->transport == XPORT_ATA) {
362c8039fc6SAlexander Motin 			cts.xport_specific.ata.mode = mode;
363c8039fc6SAlexander Motin 			cts.xport_specific.ata.valid = CTS_ATA_VALID_MODE;
364c8039fc6SAlexander Motin 		} else {
365c8039fc6SAlexander Motin 			cts.xport_specific.sata.mode = mode;
366c8039fc6SAlexander Motin 			cts.xport_specific.sata.valid = CTS_SATA_VALID_MODE;
367c8039fc6SAlexander Motin 		}
368c8039fc6SAlexander Motin 		xpt_action((union ccb *)&cts);
369c8039fc6SAlexander Motin 		/* Fetch user modes from SIM. */
370c8039fc6SAlexander Motin 		bzero(&cts, sizeof(cts));
371c8039fc6SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
372c8039fc6SAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
373c8039fc6SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
374c8039fc6SAlexander Motin 		xpt_action((union ccb *)&cts);
375c8039fc6SAlexander Motin 		if (path->device->transport == XPORT_ATA) {
376c8039fc6SAlexander Motin 			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_MODE)
377c8039fc6SAlexander Motin 				mode = cts.xport_specific.ata.mode;
378c8039fc6SAlexander Motin 		} else {
379c8039fc6SAlexander Motin 			if (cts.xport_specific.ata.valid & CTS_SATA_VALID_MODE)
380c8039fc6SAlexander Motin 				mode = cts.xport_specific.sata.mode;
381c8039fc6SAlexander Motin 		}
382c8039fc6SAlexander Motin 		/* If SIM disagree - renegotiate. */
383c8039fc6SAlexander Motin 		if (mode != wantmode)
384c8039fc6SAlexander Motin 			goto negotiate;
38552c9ce25SScott Long 		cam_fill_ataio(ataio,
38652c9ce25SScott Long 		      1,
38752c9ce25SScott Long 		      probedone,
3885daa555dSAlexander Motin 		      /*flags*/CAM_DIR_NONE,
3895daa555dSAlexander Motin 		      0,
3905daa555dSAlexander Motin 		      /*data_ptr*/NULL,
3915daa555dSAlexander Motin 		      /*dxfer_len*/0,
39252c9ce25SScott Long 		      30 * 1000);
393c8039fc6SAlexander Motin 		ata_28bit_cmd(ataio, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
39452c9ce25SScott Long 		break;
395c8039fc6SAlexander Motin 	}
3961e637ba6SAlexander Motin 	case PROBE_SET_MULTI:
3971e637ba6SAlexander Motin 	{
3981e637ba6SAlexander Motin 		u_int sectors;
3991e637ba6SAlexander Motin 
4001e637ba6SAlexander Motin 		sectors = max(1, min(ident_buf->sectors_intr & 0xff, 16));
4011e637ba6SAlexander Motin 
4021e637ba6SAlexander Motin 		/* Report bytecount to SIM. */
4031e637ba6SAlexander Motin 		bzero(&cts, sizeof(cts));
4041e637ba6SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
4051e637ba6SAlexander Motin 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
4061e637ba6SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
4071e637ba6SAlexander Motin 		if (path->device->transport == XPORT_ATA) {
408c1bd46c2SAlexander Motin 			cts.xport_specific.ata.bytecount = sectors *
409c1bd46c2SAlexander Motin 			    ata_logical_sector_size(ident_buf);
4101e637ba6SAlexander Motin 			cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT;
4111e637ba6SAlexander Motin 		} else {
412c1bd46c2SAlexander Motin 			cts.xport_specific.sata.bytecount = sectors *
413c1bd46c2SAlexander Motin 			    ata_logical_sector_size(ident_buf);
4141e637ba6SAlexander Motin 			cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT;
4151e637ba6SAlexander Motin 		}
4161e637ba6SAlexander Motin 		xpt_action((union ccb *)&cts);
4171e637ba6SAlexander Motin 
4181e637ba6SAlexander Motin 		cam_fill_ataio(ataio,
4191e637ba6SAlexander Motin 		    1,
4201e637ba6SAlexander Motin 		    probedone,
4211e637ba6SAlexander Motin 		    CAM_DIR_NONE,
4221e637ba6SAlexander Motin 		    0,
4231e637ba6SAlexander Motin 		    NULL,
4241e637ba6SAlexander Motin 		    0,
4251e637ba6SAlexander Motin 		    30*1000);
4261e637ba6SAlexander Motin 		ata_28bit_cmd(ataio, ATA_SET_MULTI, 0, 0, sectors);
4271e637ba6SAlexander Motin 		break;
42852c9ce25SScott Long 	}
42952c9ce25SScott Long 	case PROBE_INQUIRY:
43052c9ce25SScott Long 	case PROBE_FULL_INQUIRY:
43152c9ce25SScott Long 	{
43252c9ce25SScott Long 		u_int inquiry_len;
43352c9ce25SScott Long 		struct scsi_inquiry_data *inq_buf =
43452c9ce25SScott Long 		    &periph->path->device->inq_data;
43552c9ce25SScott Long 
43652c9ce25SScott Long 		if (softc->action == PROBE_INQUIRY)
43752c9ce25SScott Long 			inquiry_len = SHORT_INQUIRY_LENGTH;
43852c9ce25SScott Long 		else
43952c9ce25SScott Long 			inquiry_len = SID_ADDITIONAL_LENGTH(inq_buf);
44052c9ce25SScott Long 		/*
44152c9ce25SScott Long 		 * Some parallel SCSI devices fail to send an
44252c9ce25SScott Long 		 * ignore wide residue message when dealing with
44352c9ce25SScott Long 		 * odd length inquiry requests.  Round up to be
44452c9ce25SScott Long 		 * safe.
44552c9ce25SScott Long 		 */
44652c9ce25SScott Long 		inquiry_len = roundup2(inquiry_len, 2);
44752c9ce25SScott Long 		scsi_inquiry(csio,
44852c9ce25SScott Long 			     /*retries*/1,
44952c9ce25SScott Long 			     probedone,
45052c9ce25SScott Long 			     MSG_SIMPLE_Q_TAG,
45152c9ce25SScott Long 			     (u_int8_t *)inq_buf,
45252c9ce25SScott Long 			     inquiry_len,
45352c9ce25SScott Long 			     /*evpd*/FALSE,
45452c9ce25SScott Long 			     /*page_code*/0,
45552c9ce25SScott Long 			     SSD_MIN_SIZE,
45652c9ce25SScott Long 			     /*timeout*/60 * 1000);
45752c9ce25SScott Long 		break;
45852c9ce25SScott Long 	}
45952c9ce25SScott Long 	case PROBE_PM_PID:
46052c9ce25SScott Long 		cam_fill_ataio(ataio,
46152c9ce25SScott Long 		      1,
46252c9ce25SScott Long 		      probedone,
46352c9ce25SScott Long 		      /*flags*/CAM_DIR_NONE,
46465d0fb03SAlexander Motin 		      0,
46552c9ce25SScott Long 		      /*data_ptr*/NULL,
46652c9ce25SScott Long 		      /*dxfer_len*/0,
46752c9ce25SScott Long 		      10 * 1000);
46852c9ce25SScott Long 		ata_pm_read_cmd(ataio, 0, 15);
46952c9ce25SScott Long 		break;
47052c9ce25SScott Long 	case PROBE_PM_PRV:
47152c9ce25SScott Long 		cam_fill_ataio(ataio,
47252c9ce25SScott Long 		      1,
47352c9ce25SScott Long 		      probedone,
47452c9ce25SScott Long 		      /*flags*/CAM_DIR_NONE,
47565d0fb03SAlexander Motin 		      0,
47652c9ce25SScott Long 		      /*data_ptr*/NULL,
47752c9ce25SScott Long 		      /*dxfer_len*/0,
47852c9ce25SScott Long 		      10 * 1000);
47952c9ce25SScott Long 		ata_pm_read_cmd(ataio, 1, 15);
48052c9ce25SScott Long 		break;
48152c9ce25SScott Long 	case PROBE_INVALID:
4821e637ba6SAlexander Motin 		CAM_DEBUG(path, CAM_DEBUG_INFO,
48352c9ce25SScott Long 		    ("probestart: invalid action state\n"));
48452c9ce25SScott Long 	default:
48552c9ce25SScott Long 		break;
48652c9ce25SScott Long 	}
48752c9ce25SScott Long 	xpt_action(start_ccb);
48852c9ce25SScott Long }
48952c9ce25SScott Long #if 0
49052c9ce25SScott Long static void
49152c9ce25SScott Long proberequestdefaultnegotiation(struct cam_periph *periph)
49252c9ce25SScott Long {
49352c9ce25SScott Long 	struct ccb_trans_settings cts;
49452c9ce25SScott Long 
495bbfa4aa1SAlexander Motin 	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
49652c9ce25SScott Long 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
49752c9ce25SScott Long 	cts.type = CTS_TYPE_USER_SETTINGS;
49852c9ce25SScott Long 	xpt_action((union ccb *)&cts);
49952c9ce25SScott Long 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
50052c9ce25SScott Long 		return;
50152c9ce25SScott Long 	}
50252c9ce25SScott Long 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
50352c9ce25SScott Long 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
50452c9ce25SScott Long 	xpt_action((union ccb *)&cts);
50552c9ce25SScott Long }
50652c9ce25SScott Long 
50752c9ce25SScott Long /*
50852c9ce25SScott Long  * Backoff Negotiation Code- only pertinent for SPI devices.
50952c9ce25SScott Long  */
51052c9ce25SScott Long static int
51152c9ce25SScott Long proberequestbackoff(struct cam_periph *periph, struct cam_ed *device)
51252c9ce25SScott Long {
51352c9ce25SScott Long 	struct ccb_trans_settings cts;
51452c9ce25SScott Long 	struct ccb_trans_settings_spi *spi;
51552c9ce25SScott Long 
51652c9ce25SScott Long 	memset(&cts, 0, sizeof (cts));
517bbfa4aa1SAlexander Motin 	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
51852c9ce25SScott Long 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
51952c9ce25SScott Long 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
52052c9ce25SScott Long 	xpt_action((union ccb *)&cts);
52152c9ce25SScott Long 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
52252c9ce25SScott Long 		if (bootverbose) {
52352c9ce25SScott Long 			xpt_print(periph->path,
52452c9ce25SScott Long 			    "failed to get current device settings\n");
52552c9ce25SScott Long 		}
52652c9ce25SScott Long 		return (0);
52752c9ce25SScott Long 	}
52852c9ce25SScott Long 	if (cts.transport != XPORT_SPI) {
52952c9ce25SScott Long 		if (bootverbose) {
53052c9ce25SScott Long 			xpt_print(periph->path, "not SPI transport\n");
53152c9ce25SScott Long 		}
53252c9ce25SScott Long 		return (0);
53352c9ce25SScott Long 	}
53452c9ce25SScott Long 	spi = &cts.xport_specific.spi;
53552c9ce25SScott Long 
53652c9ce25SScott Long 	/*
53752c9ce25SScott Long 	 * We cannot renegotiate sync rate if we don't have one.
53852c9ce25SScott Long 	 */
53952c9ce25SScott Long 	if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
54052c9ce25SScott Long 		if (bootverbose) {
54152c9ce25SScott Long 			xpt_print(periph->path, "no sync rate known\n");
54252c9ce25SScott Long 		}
54352c9ce25SScott Long 		return (0);
54452c9ce25SScott Long 	}
54552c9ce25SScott Long 
54652c9ce25SScott Long 	/*
54752c9ce25SScott Long 	 * We'll assert that we don't have to touch PPR options- the
54852c9ce25SScott Long 	 * SIM will see what we do with period and offset and adjust
54952c9ce25SScott Long 	 * the PPR options as appropriate.
55052c9ce25SScott Long 	 */
55152c9ce25SScott Long 
55252c9ce25SScott Long 	/*
55352c9ce25SScott Long 	 * A sync rate with unknown or zero offset is nonsensical.
55452c9ce25SScott Long 	 * A sync period of zero means Async.
55552c9ce25SScott Long 	 */
55652c9ce25SScott Long 	if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0
55752c9ce25SScott Long 	 || spi->sync_offset == 0 || spi->sync_period == 0) {
55852c9ce25SScott Long 		if (bootverbose) {
55952c9ce25SScott Long 			xpt_print(periph->path, "no sync rate available\n");
56052c9ce25SScott Long 		}
56152c9ce25SScott Long 		return (0);
56252c9ce25SScott Long 	}
56352c9ce25SScott Long 
56452c9ce25SScott Long 	if (device->flags & CAM_DEV_DV_HIT_BOTTOM) {
56552c9ce25SScott Long 		CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
56652c9ce25SScott Long 		    ("hit async: giving up on DV\n"));
56752c9ce25SScott Long 		return (0);
56852c9ce25SScott Long 	}
56952c9ce25SScott Long 
57052c9ce25SScott Long 
57152c9ce25SScott Long 	/*
57252c9ce25SScott Long 	 * Jump sync_period up by one, but stop at 5MHz and fall back to Async.
57352c9ce25SScott Long 	 * We don't try to remember 'last' settings to see if the SIM actually
57452c9ce25SScott Long 	 * gets into the speed we want to set. We check on the SIM telling
57552c9ce25SScott Long 	 * us that a requested speed is bad, but otherwise don't try and
57652c9ce25SScott Long 	 * check the speed due to the asynchronous and handshake nature
57752c9ce25SScott Long 	 * of speed setting.
57852c9ce25SScott Long 	 */
57952c9ce25SScott Long 	spi->valid = CTS_SPI_VALID_SYNC_RATE | CTS_SPI_VALID_SYNC_OFFSET;
58052c9ce25SScott Long 	for (;;) {
58152c9ce25SScott Long 		spi->sync_period++;
58252c9ce25SScott Long 		if (spi->sync_period >= 0xf) {
58352c9ce25SScott Long 			spi->sync_period = 0;
58452c9ce25SScott Long 			spi->sync_offset = 0;
58552c9ce25SScott Long 			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
58652c9ce25SScott Long 			    ("setting to async for DV\n"));
58752c9ce25SScott Long 			/*
58852c9ce25SScott Long 			 * Once we hit async, we don't want to try
58952c9ce25SScott Long 			 * any more settings.
59052c9ce25SScott Long 			 */
59152c9ce25SScott Long 			device->flags |= CAM_DEV_DV_HIT_BOTTOM;
59252c9ce25SScott Long 		} else if (bootverbose) {
59352c9ce25SScott Long 			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
59452c9ce25SScott Long 			    ("DV: period 0x%x\n", spi->sync_period));
59552c9ce25SScott Long 			printf("setting period to 0x%x\n", spi->sync_period);
59652c9ce25SScott Long 		}
59752c9ce25SScott Long 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
59852c9ce25SScott Long 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
59952c9ce25SScott Long 		xpt_action((union ccb *)&cts);
60052c9ce25SScott Long 		if ((cts.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
60152c9ce25SScott Long 			break;
60252c9ce25SScott Long 		}
60352c9ce25SScott Long 		CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
60452c9ce25SScott Long 		    ("DV: failed to set period 0x%x\n", spi->sync_period));
60552c9ce25SScott Long 		if (spi->sync_period == 0) {
60652c9ce25SScott Long 			return (0);
60752c9ce25SScott Long 		}
60852c9ce25SScott Long 	}
60952c9ce25SScott Long 	return (1);
61052c9ce25SScott Long }
61152c9ce25SScott Long #endif
61252c9ce25SScott Long static void
61352c9ce25SScott Long probedone(struct cam_periph *periph, union ccb *done_ccb)
61452c9ce25SScott Long {
615c8039fc6SAlexander Motin 	struct ccb_trans_settings cts;
61652c9ce25SScott Long 	struct ata_params *ident_buf;
61752c9ce25SScott Long 	probe_softc *softc;
61852c9ce25SScott Long 	struct cam_path *path;
61952c9ce25SScott Long 	u_int32_t  priority;
62065d0fb03SAlexander Motin 	int found = 1;
62152c9ce25SScott Long 
62252c9ce25SScott Long 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
62352c9ce25SScott Long 
62452c9ce25SScott Long 	softc = (probe_softc *)periph->softc;
62552c9ce25SScott Long 	path = done_ccb->ccb_h.path;
62652c9ce25SScott Long 	priority = done_ccb->ccb_h.pinfo.priority;
62752c9ce25SScott Long 	ident_buf = &path->device->ident_data;
62852c9ce25SScott Long 
6291e637ba6SAlexander Motin 	if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
6301e637ba6SAlexander Motin device_fail:	if (cam_periph_error(done_ccb, 0, 0,
6311e637ba6SAlexander Motin 		    &softc->saved_ccb) == ERESTART) {
6321e637ba6SAlexander Motin 			return;
6331e637ba6SAlexander Motin 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
6341e637ba6SAlexander Motin 			/* Don't wedge the queue */
6351e637ba6SAlexander Motin 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
6361e637ba6SAlexander Motin 					 /*run_queue*/TRUE);
6371e637ba6SAlexander Motin 		}
6381e637ba6SAlexander Motin 		/* Old PIO2 devices may not support mode setting. */
6391e637ba6SAlexander Motin 		if (softc->action == PROBE_SETMODE &&
6401e637ba6SAlexander Motin 		    ata_max_pmode(ident_buf) <= ATA_PIO2 &&
6411e637ba6SAlexander Motin 		    (ident_buf->capabilities1 & ATA_SUPPORT_IORDY) == 0)
6421e637ba6SAlexander Motin 			goto noerror;
6431e637ba6SAlexander Motin 		/*
6441e637ba6SAlexander Motin 		 * If we get to this point, we got an error status back
6451e637ba6SAlexander Motin 		 * from the inquiry and the error status doesn't require
6461e637ba6SAlexander Motin 		 * automatically retrying the command.  Therefore, the
6471e637ba6SAlexander Motin 		 * inquiry failed.  If we had inquiry information before
6481e637ba6SAlexander Motin 		 * for this device, but this latest inquiry command failed,
6491e637ba6SAlexander Motin 		 * the device has probably gone away.  If this device isn't
6501e637ba6SAlexander Motin 		 * already marked unconfigured, notify the peripheral
6511e637ba6SAlexander Motin 		 * drivers that this device is no more.
6521e637ba6SAlexander Motin 		 */
6531e637ba6SAlexander Motin 		if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
6541e637ba6SAlexander Motin 			xpt_async(AC_LOST_DEVICE, path, NULL);
6551e637ba6SAlexander Motin 		found = 0;
6561e637ba6SAlexander Motin 		goto done;
6571e637ba6SAlexander Motin 	}
6581e637ba6SAlexander Motin noerror:
65952c9ce25SScott Long 	switch (softc->action) {
66052c9ce25SScott Long 	case PROBE_RESET:
6611e637ba6SAlexander Motin 	{
66252c9ce25SScott Long 		int sign = (done_ccb->ataio.res.lba_high << 8) +
66352c9ce25SScott Long 		    done_ccb->ataio.res.lba_mid;
66452c9ce25SScott Long 		xpt_print(path, "SIGNATURE: %04x\n", sign);
66552c9ce25SScott Long 		if (sign == 0x0000 &&
66652c9ce25SScott Long 		    done_ccb->ccb_h.target_id != 15) {
66752c9ce25SScott Long 			path->device->protocol = PROTO_ATA;
66852c9ce25SScott Long 			PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
66952c9ce25SScott Long 		} else if (sign == 0x9669 &&
67052c9ce25SScott Long 		    done_ccb->ccb_h.target_id == 15) {
67152c9ce25SScott Long 			/* Report SIM that PM is present. */
67252c9ce25SScott Long 			bzero(&cts, sizeof(cts));
673bbfa4aa1SAlexander Motin 			xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
67452c9ce25SScott Long 			cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
67552c9ce25SScott Long 			cts.type = CTS_TYPE_CURRENT_SETTINGS;
67652c9ce25SScott Long 			cts.xport_specific.sata.pm_present = 1;
67752c9ce25SScott Long 			cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
67852c9ce25SScott Long 			xpt_action((union ccb *)&cts);
67952c9ce25SScott Long 			path->device->protocol = PROTO_SATAPM;
68052c9ce25SScott Long 			PROBE_SET_ACTION(softc, PROBE_PM_PID);
68152c9ce25SScott Long 		} else if (sign == 0xeb14 &&
68252c9ce25SScott Long 		    done_ccb->ccb_h.target_id != 15) {
68352c9ce25SScott Long 			path->device->protocol = PROTO_SCSI;
68452c9ce25SScott Long 			PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
68552c9ce25SScott Long 		} else {
68652c9ce25SScott Long 			if (done_ccb->ccb_h.target_id != 15) {
68752c9ce25SScott Long 				xpt_print(path,
68852c9ce25SScott Long 				    "Unexpected signature 0x%04x\n", sign);
68952c9ce25SScott Long 			}
69065d0fb03SAlexander Motin 			goto device_fail;
69152c9ce25SScott Long 		}
69252c9ce25SScott Long 		xpt_release_ccb(done_ccb);
69352c9ce25SScott Long 		xpt_schedule(periph, priority);
69452c9ce25SScott Long 		return;
69552c9ce25SScott Long 	}
69652c9ce25SScott Long 	case PROBE_IDENTIFY:
69752c9ce25SScott Long 	{
69852c9ce25SScott Long 		int16_t *ptr;
69952c9ce25SScott Long 
70052c9ce25SScott Long 		for (ptr = (int16_t *)ident_buf;
70152c9ce25SScott Long 		     ptr < (int16_t *)ident_buf + sizeof(struct ata_params)/2; ptr++) {
70252c9ce25SScott Long 			*ptr = le16toh(*ptr);
70352c9ce25SScott Long 		}
70452c9ce25SScott Long 		if (strncmp(ident_buf->model, "FX", 2) &&
70552c9ce25SScott Long 		    strncmp(ident_buf->model, "NEC", 3) &&
70652c9ce25SScott Long 		    strncmp(ident_buf->model, "Pioneer", 7) &&
70752c9ce25SScott Long 		    strncmp(ident_buf->model, "SHARP", 5)) {
70852c9ce25SScott Long 			ata_bswap(ident_buf->model, sizeof(ident_buf->model));
70952c9ce25SScott Long 			ata_bswap(ident_buf->revision, sizeof(ident_buf->revision));
71052c9ce25SScott Long 			ata_bswap(ident_buf->serial, sizeof(ident_buf->serial));
71152c9ce25SScott Long 		}
71252c9ce25SScott Long 		ata_btrim(ident_buf->model, sizeof(ident_buf->model));
71352c9ce25SScott Long 		ata_bpack(ident_buf->model, ident_buf->model, sizeof(ident_buf->model));
71452c9ce25SScott Long 		ata_btrim(ident_buf->revision, sizeof(ident_buf->revision));
71552c9ce25SScott Long 		ata_bpack(ident_buf->revision, ident_buf->revision, sizeof(ident_buf->revision));
71652c9ce25SScott Long 		ata_btrim(ident_buf->serial, sizeof(ident_buf->serial));
71752c9ce25SScott Long 		ata_bpack(ident_buf->serial, ident_buf->serial, sizeof(ident_buf->serial));
71852c9ce25SScott Long 
71952c9ce25SScott Long 		if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
72052c9ce25SScott Long 			/* Check that it is the same device. */
72152c9ce25SScott Long 			MD5_CTX context;
72252c9ce25SScott Long 			u_int8_t digest[16];
72352c9ce25SScott Long 
72452c9ce25SScott Long 			MD5Init(&context);
72552c9ce25SScott Long 			MD5Update(&context,
72652c9ce25SScott Long 			    (unsigned char *)ident_buf->model,
72752c9ce25SScott Long 			    sizeof(ident_buf->model));
72852c9ce25SScott Long 			MD5Update(&context,
72952c9ce25SScott Long 			    (unsigned char *)ident_buf->revision,
73052c9ce25SScott Long 			    sizeof(ident_buf->revision));
73152c9ce25SScott Long 			MD5Update(&context,
73252c9ce25SScott Long 			    (unsigned char *)ident_buf->serial,
73352c9ce25SScott Long 			    sizeof(ident_buf->serial));
73452c9ce25SScott Long 			MD5Final(digest, &context);
73552c9ce25SScott Long 			if (bcmp(digest, softc->digest, sizeof(digest))) {
73652c9ce25SScott Long 				/* Device changed. */
73752c9ce25SScott Long 				xpt_async(AC_LOST_DEVICE, path, NULL);
73852c9ce25SScott Long 			}
7391e637ba6SAlexander Motin 		} else {
74052c9ce25SScott Long 			/* Clean up from previous instance of this device */
74152c9ce25SScott Long 			if (path->device->serial_num != NULL) {
74252c9ce25SScott Long 				free(path->device->serial_num, M_CAMXPT);
74352c9ce25SScott Long 				path->device->serial_num = NULL;
74452c9ce25SScott Long 				path->device->serial_num_len = 0;
74552c9ce25SScott Long 			}
74652c9ce25SScott Long 			path->device->serial_num =
74752c9ce25SScott Long 				(u_int8_t *)malloc((sizeof(ident_buf->serial) + 1),
74852c9ce25SScott Long 					   M_CAMXPT, M_NOWAIT);
74952c9ce25SScott Long 			if (path->device->serial_num != NULL) {
75052c9ce25SScott Long 				bcopy(ident_buf->serial,
75152c9ce25SScott Long 				      path->device->serial_num,
75252c9ce25SScott Long 				      sizeof(ident_buf->serial));
75352c9ce25SScott Long 				path->device->serial_num[sizeof(ident_buf->serial)]
75452c9ce25SScott Long 				    = '\0';
75552c9ce25SScott Long 				path->device->serial_num_len =
75652c9ce25SScott Long 				    strlen(path->device->serial_num);
75752c9ce25SScott Long 			}
75852c9ce25SScott Long 
7594b997c49SAlexander Motin 			path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
7601e637ba6SAlexander Motin 		}
76130a4094fSAlexander Motin 		if (ident_buf->satacapabilities & ATA_SUPPORT_NCQ) {
76230a4094fSAlexander Motin 			path->device->mintags = path->device->maxtags =
76330a4094fSAlexander Motin 			    ATA_QUEUE_LEN(ident_buf->queue) + 1;
76430a4094fSAlexander Motin 		}
76530a4094fSAlexander Motin 		ata_find_quirk(path->device);
766507e5811SAlexander Motin 		if (path->device->mintags != 0 &&
767507e5811SAlexander Motin 		    path->bus->sim->max_tagged_dev_openings != 0) {
768c8039fc6SAlexander Motin 			/* Report SIM which tags are allowed. */
769c8039fc6SAlexander Motin 			bzero(&cts, sizeof(cts));
770c8039fc6SAlexander Motin 			xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
771c8039fc6SAlexander Motin 			cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
772c8039fc6SAlexander Motin 			cts.type = CTS_TYPE_CURRENT_SETTINGS;
773c8039fc6SAlexander Motin 			cts.xport_specific.sata.tags = path->device->maxtags;
774c8039fc6SAlexander Motin 			cts.xport_specific.sata.valid = CTS_SATA_VALID_TAGS;
775c8039fc6SAlexander Motin 			xpt_action((union ccb *)&cts);
776c8039fc6SAlexander Motin 			/* Reconfigure queues for tagged queueing. */
77730a4094fSAlexander Motin 			xpt_start_tags(path);
77830a4094fSAlexander Motin 		}
77952c9ce25SScott Long 		ata_device_transport(path);
78052c9ce25SScott Long 		PROBE_SET_ACTION(softc, PROBE_SETMODE);
78152c9ce25SScott Long 		xpt_release_ccb(done_ccb);
78252c9ce25SScott Long 		xpt_schedule(periph, priority);
78352c9ce25SScott Long 		return;
78452c9ce25SScott Long 	}
78552c9ce25SScott Long 	case PROBE_SETMODE:
7861e637ba6SAlexander Motin 		if (path->device->protocol == PROTO_ATA) {
7871e637ba6SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_SET_MULTI);
7881e637ba6SAlexander Motin 		} else {
7891e637ba6SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_INQUIRY);
7901e637ba6SAlexander Motin 		}
7911e637ba6SAlexander Motin 		xpt_release_ccb(done_ccb);
7921e637ba6SAlexander Motin 		xpt_schedule(periph, priority);
7931e637ba6SAlexander Motin 		return;
7941e637ba6SAlexander Motin 	case PROBE_SET_MULTI:
7951e637ba6SAlexander Motin 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
79652c9ce25SScott Long 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
797f98d7a47SAlexander Motin 			xpt_acquire_device(path->device);
79852c9ce25SScott Long 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
79952c9ce25SScott Long 			xpt_action(done_ccb);
80052c9ce25SScott Long 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
80152c9ce25SScott Long 			    done_ccb);
8021e637ba6SAlexander Motin 		}
80352c9ce25SScott Long 		break;
80452c9ce25SScott Long 	case PROBE_INQUIRY:
80552c9ce25SScott Long 	case PROBE_FULL_INQUIRY:
80652c9ce25SScott Long 	{
80752c9ce25SScott Long 		struct scsi_inquiry_data *inq_buf;
8081e637ba6SAlexander Motin 		u_int8_t periph_qual, len;
80952c9ce25SScott Long 
81052c9ce25SScott Long 		path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
81152c9ce25SScott Long 		inq_buf = &path->device->inq_data;
81252c9ce25SScott Long 
81352c9ce25SScott Long 		periph_qual = SID_QUAL(inq_buf);
81452c9ce25SScott Long 
8151e637ba6SAlexander Motin 		if (periph_qual != SID_QUAL_LU_CONNECTED)
8161e637ba6SAlexander Motin 			break;
81752c9ce25SScott Long 
81852c9ce25SScott Long 		/*
81952c9ce25SScott Long 		 * We conservatively request only
82052c9ce25SScott Long 		 * SHORT_INQUIRY_LEN bytes of inquiry
82152c9ce25SScott Long 		 * information during our first try
82252c9ce25SScott Long 		 * at sending an INQUIRY. If the device
82352c9ce25SScott Long 		 * has more information to give,
82452c9ce25SScott Long 		 * perform a second request specifying
82552c9ce25SScott Long 		 * the amount of information the device
82652c9ce25SScott Long 		 * is willing to give.
82752c9ce25SScott Long 		 */
82852c9ce25SScott Long 		len = inq_buf->additional_length
8291e637ba6SAlexander Motin 		    + offsetof(struct scsi_inquiry_data, additional_length) + 1;
83052c9ce25SScott Long 		if (softc->action == PROBE_INQUIRY
83152c9ce25SScott Long 		    && len > SHORT_INQUIRY_LENGTH) {
83252c9ce25SScott Long 			PROBE_SET_ACTION(softc, PROBE_FULL_INQUIRY);
83352c9ce25SScott Long 			xpt_release_ccb(done_ccb);
83452c9ce25SScott Long 			xpt_schedule(periph, priority);
83552c9ce25SScott Long 			return;
83652c9ce25SScott Long 		}
83752c9ce25SScott Long 
8384b997c49SAlexander Motin 		ata_device_transport(path);
8391e637ba6SAlexander Motin 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
84052c9ce25SScott Long 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
841f98d7a47SAlexander Motin 			xpt_acquire_device(path->device);
84252c9ce25SScott Long 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
84352c9ce25SScott Long 			xpt_action(done_ccb);
8441e637ba6SAlexander Motin 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path, done_ccb);
8451e637ba6SAlexander Motin 		}
84652c9ce25SScott Long 		break;
84752c9ce25SScott Long 	}
84852c9ce25SScott Long 	case PROBE_PM_PID:
8494b997c49SAlexander Motin 		if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) == 0)
85052c9ce25SScott Long 			bzero(ident_buf, sizeof(*ident_buf));
85152c9ce25SScott Long 		softc->pm_pid = (done_ccb->ataio.res.lba_high << 24) +
85252c9ce25SScott Long 		    (done_ccb->ataio.res.lba_mid << 16) +
85352c9ce25SScott Long 		    (done_ccb->ataio.res.lba_low << 8) +
85452c9ce25SScott Long 		    done_ccb->ataio.res.sector_count;
85565d0fb03SAlexander Motin 		((uint32_t *)ident_buf)[0] = softc->pm_pid;
85652c9ce25SScott Long 		printf("PM Product ID: %08x\n", softc->pm_pid);
85752c9ce25SScott Long 		snprintf(ident_buf->model, sizeof(ident_buf->model),
85852c9ce25SScott Long 		    "Port Multiplier %08x", softc->pm_pid);
85952c9ce25SScott Long 		PROBE_SET_ACTION(softc, PROBE_PM_PRV);
86052c9ce25SScott Long 		xpt_release_ccb(done_ccb);
86152c9ce25SScott Long 		xpt_schedule(periph, priority);
86252c9ce25SScott Long 		return;
86352c9ce25SScott Long 	case PROBE_PM_PRV:
86452c9ce25SScott Long 		softc->pm_prv = (done_ccb->ataio.res.lba_high << 24) +
86552c9ce25SScott Long 		    (done_ccb->ataio.res.lba_mid << 16) +
86652c9ce25SScott Long 		    (done_ccb->ataio.res.lba_low << 8) +
86752c9ce25SScott Long 		    done_ccb->ataio.res.sector_count;
86865d0fb03SAlexander Motin 		((uint32_t *)ident_buf)[1] = softc->pm_prv;
86952c9ce25SScott Long 		printf("PM Revision: %08x\n", softc->pm_prv);
87052c9ce25SScott Long 		snprintf(ident_buf->revision, sizeof(ident_buf->revision),
87152c9ce25SScott Long 		    "%04x", softc->pm_prv);
8724b997c49SAlexander Motin 		path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
87365d0fb03SAlexander Motin 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
87452c9ce25SScott Long 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
875f98d7a47SAlexander Motin 			xpt_acquire_device(path->device);
87652c9ce25SScott Long 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
87752c9ce25SScott Long 			xpt_action(done_ccb);
87852c9ce25SScott Long 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
87952c9ce25SScott Long 			    done_ccb);
88065d0fb03SAlexander Motin 		} else {
88165d0fb03SAlexander Motin 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
88265d0fb03SAlexander Motin 			xpt_action(done_ccb);
8831e637ba6SAlexander Motin 			xpt_async(AC_SCSI_AEN, done_ccb->ccb_h.path, done_ccb);
88452c9ce25SScott Long 		}
88552c9ce25SScott Long 		break;
88652c9ce25SScott Long 	case PROBE_INVALID:
88752c9ce25SScott Long 		CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_INFO,
88852c9ce25SScott Long 		    ("probedone: invalid action state\n"));
88952c9ce25SScott Long 	default:
89052c9ce25SScott Long 		break;
89152c9ce25SScott Long 	}
8921e637ba6SAlexander Motin done:
8931e637ba6SAlexander Motin 	xpt_release_ccb(done_ccb);
89452c9ce25SScott Long 	done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
89552c9ce25SScott Long 	TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe);
89652c9ce25SScott Long 	done_ccb->ccb_h.status = CAM_REQ_CMP;
89752c9ce25SScott Long 	done_ccb->ccb_h.ppriv_field1 = found;
89852c9ce25SScott Long 	xpt_done(done_ccb);
89952c9ce25SScott Long 	if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
90052c9ce25SScott Long 		cam_periph_invalidate(periph);
90152c9ce25SScott Long 		cam_periph_release_locked(periph);
90252c9ce25SScott Long 	} else {
90352c9ce25SScott Long 		probeschedule(periph);
90452c9ce25SScott Long 	}
90552c9ce25SScott Long }
90652c9ce25SScott Long 
90752c9ce25SScott Long static void
90852c9ce25SScott Long probecleanup(struct cam_periph *periph)
90952c9ce25SScott Long {
91052c9ce25SScott Long 	free(periph->softc, M_CAMXPT);
91152c9ce25SScott Long }
91252c9ce25SScott Long 
91352c9ce25SScott Long static void
91430a4094fSAlexander Motin ata_find_quirk(struct cam_ed *device)
91552c9ce25SScott Long {
91630a4094fSAlexander Motin 	struct ata_quirk_entry *quirk;
91752c9ce25SScott Long 	caddr_t	match;
91852c9ce25SScott Long 
91930a4094fSAlexander Motin 	match = cam_quirkmatch((caddr_t)&device->ident_data,
92030a4094fSAlexander Motin 			       (caddr_t)ata_quirk_table,
92130a4094fSAlexander Motin 			       ata_quirk_table_size,
92230a4094fSAlexander Motin 			       sizeof(*ata_quirk_table), ata_identify_match);
92352c9ce25SScott Long 
92452c9ce25SScott Long 	if (match == NULL)
92552c9ce25SScott Long 		panic("xpt_find_quirk: device didn't match wildcard entry!!");
92652c9ce25SScott Long 
92730a4094fSAlexander Motin 	quirk = (struct ata_quirk_entry *)match;
92852c9ce25SScott Long 	device->quirk = quirk;
92930a4094fSAlexander Motin 	if (quirk->quirks & CAM_QUIRK_MAXTAGS)
93030a4094fSAlexander Motin 		device->mintags = device->maxtags = quirk->maxtags;
93152c9ce25SScott Long }
93252c9ce25SScott Long 
93352c9ce25SScott Long typedef struct {
93452c9ce25SScott Long 	union	ccb *request_ccb;
93552c9ce25SScott Long 	struct 	ccb_pathinq *cpi;
93652c9ce25SScott Long 	int	counter;
93752c9ce25SScott Long 	int	found;
93852c9ce25SScott Long } ata_scan_bus_info;
93952c9ce25SScott Long 
94052c9ce25SScott Long /*
94152c9ce25SScott Long  * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
94252c9ce25SScott Long  * As the scan progresses, xpt_scan_bus is used as the
94352c9ce25SScott Long  * callback on completion function.
94452c9ce25SScott Long  */
94552c9ce25SScott Long static void
94652c9ce25SScott Long ata_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
94752c9ce25SScott Long {
94852c9ce25SScott Long 	struct	cam_path *path;
94952c9ce25SScott Long 	ata_scan_bus_info *scan_info;
95052c9ce25SScott Long 	union	ccb *work_ccb;
95152c9ce25SScott Long 	cam_status status;
95252c9ce25SScott Long 
95352c9ce25SScott Long 	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
95452c9ce25SScott Long 		  ("xpt_scan_bus\n"));
95552c9ce25SScott Long 	switch (request_ccb->ccb_h.func_code) {
95652c9ce25SScott Long 	case XPT_SCAN_BUS:
95752c9ce25SScott Long 		/* Find out the characteristics of the bus */
95852c9ce25SScott Long 		work_ccb = xpt_alloc_ccb_nowait();
95952c9ce25SScott Long 		if (work_ccb == NULL) {
96052c9ce25SScott Long 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
96152c9ce25SScott Long 			xpt_done(request_ccb);
96252c9ce25SScott Long 			return;
96352c9ce25SScott Long 		}
96452c9ce25SScott Long 		xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
96552c9ce25SScott Long 			      request_ccb->ccb_h.pinfo.priority);
96652c9ce25SScott Long 		work_ccb->ccb_h.func_code = XPT_PATH_INQ;
96752c9ce25SScott Long 		xpt_action(work_ccb);
96852c9ce25SScott Long 		if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
96952c9ce25SScott Long 			request_ccb->ccb_h.status = work_ccb->ccb_h.status;
97052c9ce25SScott Long 			xpt_free_ccb(work_ccb);
97152c9ce25SScott Long 			xpt_done(request_ccb);
97252c9ce25SScott Long 			return;
97352c9ce25SScott Long 		}
97452c9ce25SScott Long 
97552c9ce25SScott Long 		/* Save some state for use while we probe for devices */
97652c9ce25SScott Long 		scan_info = (ata_scan_bus_info *)
97752c9ce25SScott Long 		    malloc(sizeof(ata_scan_bus_info), M_CAMXPT, M_NOWAIT);
97852c9ce25SScott Long 		if (scan_info == NULL) {
97952c9ce25SScott Long 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
98052c9ce25SScott Long 			xpt_done(request_ccb);
98152c9ce25SScott Long 			return;
98252c9ce25SScott Long 		}
98352c9ce25SScott Long 		scan_info->request_ccb = request_ccb;
98452c9ce25SScott Long 		scan_info->cpi = &work_ccb->cpi;
9854b997c49SAlexander Motin 		if (scan_info->cpi->transport == XPORT_ATA)
9864b997c49SAlexander Motin 			scan_info->found = 0x0003;
9874b997c49SAlexander Motin 		else
98852c9ce25SScott Long 			scan_info->found = 0x8001;
98952c9ce25SScott Long 		scan_info->counter = 0;
99052c9ce25SScott Long 		/* If PM supported, probe it first. */
99152c9ce25SScott Long 		if (scan_info->cpi->hba_inquiry & PI_SATAPM)
99252c9ce25SScott Long 			scan_info->counter = 15;
99352c9ce25SScott Long 
99452c9ce25SScott Long 		work_ccb = xpt_alloc_ccb_nowait();
99552c9ce25SScott Long 		if (work_ccb == NULL) {
99652c9ce25SScott Long 			free(scan_info, M_CAMXPT);
99752c9ce25SScott Long 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
99852c9ce25SScott Long 			xpt_done(request_ccb);
99952c9ce25SScott Long 			break;
100052c9ce25SScott Long 		}
100152c9ce25SScott Long 		goto scan_next;
100252c9ce25SScott Long 	case XPT_SCAN_LUN:
100352c9ce25SScott Long 		work_ccb = request_ccb;
100452c9ce25SScott Long 		/* Reuse the same CCB to query if a device was really found */
100552c9ce25SScott Long 		scan_info = (ata_scan_bus_info *)work_ccb->ccb_h.ppriv_ptr0;
100652c9ce25SScott Long 		/* Free the current request path- we're done with it. */
100752c9ce25SScott Long 		xpt_free_path(work_ccb->ccb_h.path);
100865d0fb03SAlexander Motin 		/* If there is PMP... */
100952c9ce25SScott Long 		if (scan_info->counter == 15) {
101052c9ce25SScott Long 			if (work_ccb->ccb_h.ppriv_field1 != 0) {
101165d0fb03SAlexander Motin 				/* everything else willbe probed by it */
101265d0fb03SAlexander Motin 				scan_info->found = 0x8000;
101352c9ce25SScott Long 			} else {
101452c9ce25SScott Long 				struct ccb_trans_settings cts;
101552c9ce25SScott Long 
101652c9ce25SScott Long 				/* Report SIM that PM is absent. */
101752c9ce25SScott Long 				bzero(&cts, sizeof(cts));
101852c9ce25SScott Long 				xpt_setup_ccb(&cts.ccb_h,
101952c9ce25SScott Long 				    scan_info->request_ccb->ccb_h.path, 1);
102052c9ce25SScott Long 				cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
102152c9ce25SScott Long 				cts.type = CTS_TYPE_CURRENT_SETTINGS;
10223ccda2f3SAlexander Motin 				cts.xport_specific.sata.pm_present = 0;
102352c9ce25SScott Long 				cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
102452c9ce25SScott Long 				xpt_action((union ccb *)&cts);
102552c9ce25SScott Long 			}
102652c9ce25SScott Long 		}
102752c9ce25SScott Long take_next:
102852c9ce25SScott Long 		/* Take next device. Wrap from 15 (PM) to 0. */
102952c9ce25SScott Long 		scan_info->counter = (scan_info->counter + 1 ) & 0x0f;
10308e7cccb3SAlexander Motin 		if (scan_info->counter > scan_info->cpi->max_target -
10318e7cccb3SAlexander Motin 		    ((scan_info->cpi->hba_inquiry & PI_SATAPM) ? 1 : 0)) {
103252c9ce25SScott Long 			xpt_free_ccb(work_ccb);
103352c9ce25SScott Long 			xpt_free_ccb((union ccb *)scan_info->cpi);
103452c9ce25SScott Long 			request_ccb = scan_info->request_ccb;
103552c9ce25SScott Long 			free(scan_info, M_CAMXPT);
103652c9ce25SScott Long 			request_ccb->ccb_h.status = CAM_REQ_CMP;
103752c9ce25SScott Long 			xpt_done(request_ccb);
103852c9ce25SScott Long 			break;
103952c9ce25SScott Long 		}
104052c9ce25SScott Long scan_next:
104165d0fb03SAlexander Motin 		if ((scan_info->found & (1 << scan_info->counter)) == 0)
104265d0fb03SAlexander Motin 			goto take_next;
104352c9ce25SScott Long 		status = xpt_create_path(&path, xpt_periph,
104452c9ce25SScott Long 		    scan_info->request_ccb->ccb_h.path_id,
104552c9ce25SScott Long 		    scan_info->counter, 0);
104652c9ce25SScott Long 		if (status != CAM_REQ_CMP) {
104752c9ce25SScott Long 			printf("xpt_scan_bus: xpt_create_path failed"
104852c9ce25SScott Long 			    " with status %#x, bus scan halted\n",
104952c9ce25SScott Long 			    status);
105052c9ce25SScott Long 			xpt_free_ccb(work_ccb);
105152c9ce25SScott Long 			xpt_free_ccb((union ccb *)scan_info->cpi);
105252c9ce25SScott Long 			request_ccb = scan_info->request_ccb;
105352c9ce25SScott Long 			free(scan_info, M_CAMXPT);
105452c9ce25SScott Long 			request_ccb->ccb_h.status = status;
105552c9ce25SScott Long 			xpt_done(request_ccb);
105652c9ce25SScott Long 			break;
105752c9ce25SScott Long 		}
105852c9ce25SScott Long 		xpt_setup_ccb(&work_ccb->ccb_h, path,
105952c9ce25SScott Long 		    scan_info->request_ccb->ccb_h.pinfo.priority);
106052c9ce25SScott Long 		work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
106152c9ce25SScott Long 		work_ccb->ccb_h.cbfcnp = ata_scan_bus;
106252c9ce25SScott Long 		work_ccb->ccb_h.ppriv_ptr0 = scan_info;
106352c9ce25SScott Long 		work_ccb->crcn.flags = scan_info->request_ccb->crcn.flags;
106452c9ce25SScott Long 		xpt_action(work_ccb);
106552c9ce25SScott Long 		break;
106652c9ce25SScott Long 	default:
106752c9ce25SScott Long 		break;
106852c9ce25SScott Long 	}
106952c9ce25SScott Long }
107052c9ce25SScott Long 
107152c9ce25SScott Long static void
107252c9ce25SScott Long ata_scan_lun(struct cam_periph *periph, struct cam_path *path,
107352c9ce25SScott Long 	     cam_flags flags, union ccb *request_ccb)
107452c9ce25SScott Long {
107552c9ce25SScott Long 	struct ccb_pathinq cpi;
107652c9ce25SScott Long 	cam_status status;
107752c9ce25SScott Long 	struct cam_path *new_path;
107852c9ce25SScott Long 	struct cam_periph *old_periph;
107952c9ce25SScott Long 
108052c9ce25SScott Long 	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
108152c9ce25SScott Long 		  ("xpt_scan_lun\n"));
108252c9ce25SScott Long 
1083bbfa4aa1SAlexander Motin 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
108452c9ce25SScott Long 	cpi.ccb_h.func_code = XPT_PATH_INQ;
108552c9ce25SScott Long 	xpt_action((union ccb *)&cpi);
108652c9ce25SScott Long 
108752c9ce25SScott Long 	if (cpi.ccb_h.status != CAM_REQ_CMP) {
108852c9ce25SScott Long 		if (request_ccb != NULL) {
108952c9ce25SScott Long 			request_ccb->ccb_h.status = cpi.ccb_h.status;
109052c9ce25SScott Long 			xpt_done(request_ccb);
109152c9ce25SScott Long 		}
109252c9ce25SScott Long 		return;
109352c9ce25SScott Long 	}
109452c9ce25SScott Long 
109552c9ce25SScott Long 	if (request_ccb == NULL) {
109652c9ce25SScott Long 		request_ccb = malloc(sizeof(union ccb), M_CAMXPT, M_NOWAIT);
109752c9ce25SScott Long 		if (request_ccb == NULL) {
109852c9ce25SScott Long 			xpt_print(path, "xpt_scan_lun: can't allocate CCB, "
109952c9ce25SScott Long 			    "can't continue\n");
110052c9ce25SScott Long 			return;
110152c9ce25SScott Long 		}
110252c9ce25SScott Long 		new_path = malloc(sizeof(*new_path), M_CAMXPT, M_NOWAIT);
110352c9ce25SScott Long 		if (new_path == NULL) {
110452c9ce25SScott Long 			xpt_print(path, "xpt_scan_lun: can't allocate path, "
110552c9ce25SScott Long 			    "can't continue\n");
110652c9ce25SScott Long 			free(request_ccb, M_CAMXPT);
110752c9ce25SScott Long 			return;
110852c9ce25SScott Long 		}
110952c9ce25SScott Long 		status = xpt_compile_path(new_path, xpt_periph,
111052c9ce25SScott Long 					  path->bus->path_id,
111152c9ce25SScott Long 					  path->target->target_id,
111252c9ce25SScott Long 					  path->device->lun_id);
111352c9ce25SScott Long 
111452c9ce25SScott Long 		if (status != CAM_REQ_CMP) {
111552c9ce25SScott Long 			xpt_print(path, "xpt_scan_lun: can't compile path, "
111652c9ce25SScott Long 			    "can't continue\n");
111752c9ce25SScott Long 			free(request_ccb, M_CAMXPT);
111852c9ce25SScott Long 			free(new_path, M_CAMXPT);
111952c9ce25SScott Long 			return;
112052c9ce25SScott Long 		}
1121bbfa4aa1SAlexander Motin 		xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_NORMAL);
112252c9ce25SScott Long 		request_ccb->ccb_h.cbfcnp = xptscandone;
112352c9ce25SScott Long 		request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
112452c9ce25SScott Long 		request_ccb->crcn.flags = flags;
112552c9ce25SScott Long 	}
112652c9ce25SScott Long 
1127f09f8e3eSAlexander Motin 	if ((old_periph = cam_periph_find(path, "aprobe")) != NULL) {
112852c9ce25SScott Long 		probe_softc *softc;
112952c9ce25SScott Long 
113052c9ce25SScott Long 		softc = (probe_softc *)old_periph->softc;
113152c9ce25SScott Long 		TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
113252c9ce25SScott Long 				  periph_links.tqe);
113352c9ce25SScott Long 	} else {
113452c9ce25SScott Long 		status = cam_periph_alloc(proberegister, NULL, probecleanup,
1135f09f8e3eSAlexander Motin 					  probestart, "aprobe",
113652c9ce25SScott Long 					  CAM_PERIPH_BIO,
113752c9ce25SScott Long 					  request_ccb->ccb_h.path, NULL, 0,
113852c9ce25SScott Long 					  request_ccb);
113952c9ce25SScott Long 
114052c9ce25SScott Long 		if (status != CAM_REQ_CMP) {
114152c9ce25SScott Long 			xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
114252c9ce25SScott Long 			    "returned an error, can't continue probe\n");
114352c9ce25SScott Long 			request_ccb->ccb_h.status = status;
114452c9ce25SScott Long 			xpt_done(request_ccb);
114552c9ce25SScott Long 		}
114652c9ce25SScott Long 	}
114752c9ce25SScott Long }
114852c9ce25SScott Long 
114952c9ce25SScott Long static void
115052c9ce25SScott Long xptscandone(struct cam_periph *periph, union ccb *done_ccb)
115152c9ce25SScott Long {
115252c9ce25SScott Long 	xpt_release_path(done_ccb->ccb_h.path);
115352c9ce25SScott Long 	free(done_ccb->ccb_h.path, M_CAMXPT);
115452c9ce25SScott Long 	free(done_ccb, M_CAMXPT);
115552c9ce25SScott Long }
115652c9ce25SScott Long 
115752c9ce25SScott Long static struct cam_ed *
115852c9ce25SScott Long ata_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
115952c9ce25SScott Long {
116052c9ce25SScott Long 	struct cam_path path;
116130a4094fSAlexander Motin 	struct ata_quirk_entry *quirk;
116252c9ce25SScott Long 	struct cam_ed *device;
116352c9ce25SScott Long 	struct cam_ed *cur_device;
116452c9ce25SScott Long 
116552c9ce25SScott Long 	device = xpt_alloc_device(bus, target, lun_id);
116652c9ce25SScott Long 	if (device == NULL)
116752c9ce25SScott Long 		return (NULL);
116852c9ce25SScott Long 
116952c9ce25SScott Long 	/*
117052c9ce25SScott Long 	 * Take the default quirk entry until we have inquiry
117152c9ce25SScott Long 	 * data and can determine a better quirk to use.
117252c9ce25SScott Long 	 */
117330a4094fSAlexander Motin 	quirk = &ata_quirk_table[ata_quirk_table_size - 1];
117452c9ce25SScott Long 	device->quirk = (void *)quirk;
117530a4094fSAlexander Motin 	device->mintags = 0;
117630a4094fSAlexander Motin 	device->maxtags = 0;
117752c9ce25SScott Long 	bzero(&device->inq_data, sizeof(device->inq_data));
117852c9ce25SScott Long 	device->inq_flags = 0;
117952c9ce25SScott Long 	device->queue_flags = 0;
118052c9ce25SScott Long 	device->serial_num = NULL;
118152c9ce25SScott Long 	device->serial_num_len = 0;
118252c9ce25SScott Long 
118352c9ce25SScott Long 	/*
118452c9ce25SScott Long 	 * XXX should be limited by number of CCBs this bus can
118552c9ce25SScott Long 	 * do.
118652c9ce25SScott Long 	 */
118752c9ce25SScott Long 	bus->sim->max_ccbs += device->ccbq.devq_openings;
118852c9ce25SScott Long 	/* Insertion sort into our target's device list */
118952c9ce25SScott Long 	cur_device = TAILQ_FIRST(&target->ed_entries);
119052c9ce25SScott Long 	while (cur_device != NULL && cur_device->lun_id < lun_id)
119152c9ce25SScott Long 		cur_device = TAILQ_NEXT(cur_device, links);
119252c9ce25SScott Long 	if (cur_device != NULL) {
119352c9ce25SScott Long 		TAILQ_INSERT_BEFORE(cur_device, device, links);
119452c9ce25SScott Long 	} else {
119552c9ce25SScott Long 		TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
119652c9ce25SScott Long 	}
119752c9ce25SScott Long 	target->generation++;
119852c9ce25SScott Long 	if (lun_id != CAM_LUN_WILDCARD) {
119952c9ce25SScott Long 		xpt_compile_path(&path,
120052c9ce25SScott Long 				 NULL,
120152c9ce25SScott Long 				 bus->path_id,
120252c9ce25SScott Long 				 target->target_id,
120352c9ce25SScott Long 				 lun_id);
120452c9ce25SScott Long 		ata_device_transport(&path);
120552c9ce25SScott Long 		xpt_release_path(&path);
120652c9ce25SScott Long 	}
120752c9ce25SScott Long 
120852c9ce25SScott Long 	return (device);
120952c9ce25SScott Long }
121052c9ce25SScott Long 
121152c9ce25SScott Long static void
121252c9ce25SScott Long ata_device_transport(struct cam_path *path)
121352c9ce25SScott Long {
121452c9ce25SScott Long 	struct ccb_pathinq cpi;
12154b997c49SAlexander Motin 	struct ccb_trans_settings cts;
12164b997c49SAlexander Motin 	struct scsi_inquiry_data *inq_buf = NULL;
12174b997c49SAlexander Motin 	struct ata_params *ident_buf = NULL;
121852c9ce25SScott Long 
121952c9ce25SScott Long 	/* Get transport information from the SIM */
1220bbfa4aa1SAlexander Motin 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
122152c9ce25SScott Long 	cpi.ccb_h.func_code = XPT_PATH_INQ;
122252c9ce25SScott Long 	xpt_action((union ccb *)&cpi);
122352c9ce25SScott Long 
122452c9ce25SScott Long 	path->device->transport = cpi.transport;
12254b997c49SAlexander Motin 	if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
12264b997c49SAlexander Motin 		inq_buf = &path->device->inq_data;
12274b997c49SAlexander Motin 	if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) != 0)
12284b997c49SAlexander Motin 		ident_buf = &path->device->ident_data;
12294b997c49SAlexander Motin 	if (path->device->protocol == PROTO_ATA) {
12304b997c49SAlexander Motin 		path->device->protocol_version = ident_buf ?
12314b997c49SAlexander Motin 		    ata_version(ident_buf->version_major) : cpi.protocol_version;
12324b997c49SAlexander Motin 	} else if (path->device->protocol == PROTO_SCSI) {
12334b997c49SAlexander Motin 		path->device->protocol_version = inq_buf ?
12344b997c49SAlexander Motin 		    SID_ANSI_REV(inq_buf) : cpi.protocol_version;
123552c9ce25SScott Long 	}
12364b997c49SAlexander Motin 	path->device->transport_version = ident_buf ?
12374b997c49SAlexander Motin 	    ata_version(ident_buf->version_major) : cpi.transport_version;
123852c9ce25SScott Long 
123952c9ce25SScott Long 	/* Tell the controller what we think */
1240bbfa4aa1SAlexander Motin 	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
124152c9ce25SScott Long 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
124252c9ce25SScott Long 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
124352c9ce25SScott Long 	cts.transport = path->device->transport;
124452c9ce25SScott Long 	cts.transport_version = path->device->transport_version;
124552c9ce25SScott Long 	cts.protocol = path->device->protocol;
124652c9ce25SScott Long 	cts.protocol_version = path->device->protocol_version;
124752c9ce25SScott Long 	cts.proto_specific.valid = 0;
124852c9ce25SScott Long 	cts.xport_specific.valid = 0;
124952c9ce25SScott Long 	xpt_action((union ccb *)&cts);
125052c9ce25SScott Long }
125152c9ce25SScott Long 
125252c9ce25SScott Long static void
125352c9ce25SScott Long ata_action(union ccb *start_ccb)
125452c9ce25SScott Long {
125552c9ce25SScott Long 
125652c9ce25SScott Long 	switch (start_ccb->ccb_h.func_code) {
125752c9ce25SScott Long 	case XPT_SET_TRAN_SETTINGS:
125852c9ce25SScott Long 	{
125930a4094fSAlexander Motin 		ata_set_transfer_settings(&start_ccb->cts,
126052c9ce25SScott Long 					   start_ccb->ccb_h.path->device,
126152c9ce25SScott Long 					   /*async_update*/FALSE);
126252c9ce25SScott Long 		break;
126352c9ce25SScott Long 	}
126452c9ce25SScott Long 	case XPT_SCAN_BUS:
126552c9ce25SScott Long 		ata_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
126652c9ce25SScott Long 		break;
126752c9ce25SScott Long 	case XPT_SCAN_LUN:
126852c9ce25SScott Long 		ata_scan_lun(start_ccb->ccb_h.path->periph,
126952c9ce25SScott Long 			      start_ccb->ccb_h.path, start_ccb->crcn.flags,
127052c9ce25SScott Long 			      start_ccb);
127152c9ce25SScott Long 		break;
127252c9ce25SScott Long 	case XPT_GET_TRAN_SETTINGS:
127352c9ce25SScott Long 	{
127452c9ce25SScott Long 		struct cam_sim *sim;
127552c9ce25SScott Long 
127652c9ce25SScott Long 		sim = start_ccb->ccb_h.path->bus->sim;
127752c9ce25SScott Long 		(*(sim->sim_action))(sim, start_ccb);
127852c9ce25SScott Long 		break;
127952c9ce25SScott Long 	}
128052c9ce25SScott Long 	default:
128152c9ce25SScott Long 		xpt_action_default(start_ccb);
128252c9ce25SScott Long 		break;
128352c9ce25SScott Long 	}
128452c9ce25SScott Long }
128552c9ce25SScott Long 
128652c9ce25SScott Long static void
128730a4094fSAlexander Motin ata_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
128852c9ce25SScott Long 			   int async_update)
128952c9ce25SScott Long {
129052c9ce25SScott Long 	struct	ccb_pathinq cpi;
129152c9ce25SScott Long 	struct	ccb_trans_settings cur_cts;
129252c9ce25SScott Long 	struct	ccb_trans_settings_scsi *scsi;
129352c9ce25SScott Long 	struct	ccb_trans_settings_scsi *cur_scsi;
129452c9ce25SScott Long 	struct	cam_sim *sim;
129552c9ce25SScott Long 	struct	scsi_inquiry_data *inq_data;
129652c9ce25SScott Long 
129752c9ce25SScott Long 	if (device == NULL) {
129852c9ce25SScott Long 		cts->ccb_h.status = CAM_PATH_INVALID;
129952c9ce25SScott Long 		xpt_done((union ccb *)cts);
130052c9ce25SScott Long 		return;
130152c9ce25SScott Long 	}
130252c9ce25SScott Long 
130352c9ce25SScott Long 	if (cts->protocol == PROTO_UNKNOWN
130452c9ce25SScott Long 	 || cts->protocol == PROTO_UNSPECIFIED) {
130552c9ce25SScott Long 		cts->protocol = device->protocol;
130652c9ce25SScott Long 		cts->protocol_version = device->protocol_version;
130752c9ce25SScott Long 	}
130852c9ce25SScott Long 
130952c9ce25SScott Long 	if (cts->protocol_version == PROTO_VERSION_UNKNOWN
131052c9ce25SScott Long 	 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
131152c9ce25SScott Long 		cts->protocol_version = device->protocol_version;
131252c9ce25SScott Long 
131352c9ce25SScott Long 	if (cts->protocol != device->protocol) {
131452c9ce25SScott Long 		xpt_print(cts->ccb_h.path, "Uninitialized Protocol %x:%x?\n",
131552c9ce25SScott Long 		       cts->protocol, device->protocol);
131652c9ce25SScott Long 		cts->protocol = device->protocol;
131752c9ce25SScott Long 	}
131852c9ce25SScott Long 
131952c9ce25SScott Long 	if (cts->protocol_version > device->protocol_version) {
132052c9ce25SScott Long 		if (bootverbose) {
132152c9ce25SScott Long 			xpt_print(cts->ccb_h.path, "Down reving Protocol "
132252c9ce25SScott Long 			    "Version from %d to %d?\n", cts->protocol_version,
132352c9ce25SScott Long 			    device->protocol_version);
132452c9ce25SScott Long 		}
132552c9ce25SScott Long 		cts->protocol_version = device->protocol_version;
132652c9ce25SScott Long 	}
132752c9ce25SScott Long 
132852c9ce25SScott Long 	if (cts->transport == XPORT_UNKNOWN
132952c9ce25SScott Long 	 || cts->transport == XPORT_UNSPECIFIED) {
133052c9ce25SScott Long 		cts->transport = device->transport;
133152c9ce25SScott Long 		cts->transport_version = device->transport_version;
133252c9ce25SScott Long 	}
133352c9ce25SScott Long 
133452c9ce25SScott Long 	if (cts->transport_version == XPORT_VERSION_UNKNOWN
133552c9ce25SScott Long 	 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
133652c9ce25SScott Long 		cts->transport_version = device->transport_version;
133752c9ce25SScott Long 
133852c9ce25SScott Long 	if (cts->transport != device->transport) {
133952c9ce25SScott Long 		xpt_print(cts->ccb_h.path, "Uninitialized Transport %x:%x?\n",
134052c9ce25SScott Long 		    cts->transport, device->transport);
134152c9ce25SScott Long 		cts->transport = device->transport;
134252c9ce25SScott Long 	}
134352c9ce25SScott Long 
134452c9ce25SScott Long 	if (cts->transport_version > device->transport_version) {
134552c9ce25SScott Long 		if (bootverbose) {
134652c9ce25SScott Long 			xpt_print(cts->ccb_h.path, "Down reving Transport "
134752c9ce25SScott Long 			    "Version from %d to %d?\n", cts->transport_version,
134852c9ce25SScott Long 			    device->transport_version);
134952c9ce25SScott Long 		}
135052c9ce25SScott Long 		cts->transport_version = device->transport_version;
135152c9ce25SScott Long 	}
135252c9ce25SScott Long 
135352c9ce25SScott Long 	sim = cts->ccb_h.path->bus->sim;
135452c9ce25SScott Long 
135552c9ce25SScott Long 	/*
135652c9ce25SScott Long 	 * Nothing more of interest to do unless
135752c9ce25SScott Long 	 * this is a device connected via the
135852c9ce25SScott Long 	 * SCSI protocol.
135952c9ce25SScott Long 	 */
136052c9ce25SScott Long 	if (cts->protocol != PROTO_SCSI) {
136152c9ce25SScott Long 		if (async_update == FALSE)
136252c9ce25SScott Long 			(*(sim->sim_action))(sim, (union ccb *)cts);
136352c9ce25SScott Long 		return;
136452c9ce25SScott Long 	}
136552c9ce25SScott Long 
136652c9ce25SScott Long 	inq_data = &device->inq_data;
136752c9ce25SScott Long 	scsi = &cts->proto_specific.scsi;
1368bbfa4aa1SAlexander Motin 	xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NORMAL);
136952c9ce25SScott Long 	cpi.ccb_h.func_code = XPT_PATH_INQ;
137052c9ce25SScott Long 	xpt_action((union ccb *)&cpi);
137152c9ce25SScott Long 
137252c9ce25SScott Long 	/* SCSI specific sanity checking */
137352c9ce25SScott Long 	if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
137452c9ce25SScott Long 	 || (INQ_DATA_TQ_ENABLED(inq_data)) == 0
137552c9ce25SScott Long 	 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
137652c9ce25SScott Long 	 || (device->mintags == 0)) {
137752c9ce25SScott Long 		/*
137852c9ce25SScott Long 		 * Can't tag on hardware that doesn't support tags,
137952c9ce25SScott Long 		 * doesn't have it enabled, or has broken tag support.
138052c9ce25SScott Long 		 */
138152c9ce25SScott Long 		scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
138252c9ce25SScott Long 	}
138352c9ce25SScott Long 
138452c9ce25SScott Long 	if (async_update == FALSE) {
138552c9ce25SScott Long 		/*
138652c9ce25SScott Long 		 * Perform sanity checking against what the
138752c9ce25SScott Long 		 * controller and device can do.
138852c9ce25SScott Long 		 */
1389bbfa4aa1SAlexander Motin 		xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NORMAL);
139052c9ce25SScott Long 		cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
139152c9ce25SScott Long 		cur_cts.type = cts->type;
139252c9ce25SScott Long 		xpt_action((union ccb *)&cur_cts);
139352c9ce25SScott Long 		if ((cur_cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
139452c9ce25SScott Long 			return;
139552c9ce25SScott Long 		}
139652c9ce25SScott Long 		cur_scsi = &cur_cts.proto_specific.scsi;
139752c9ce25SScott Long 		if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
139852c9ce25SScott Long 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
139952c9ce25SScott Long 			scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
140052c9ce25SScott Long 		}
140152c9ce25SScott Long 		if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
140252c9ce25SScott Long 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
140352c9ce25SScott Long 	}
140452c9ce25SScott Long 
140552c9ce25SScott Long 	if (cts->type == CTS_TYPE_CURRENT_SETTINGS
140652c9ce25SScott Long 	 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
140752c9ce25SScott Long 		int device_tagenb;
140852c9ce25SScott Long 
140952c9ce25SScott Long 		/*
141052c9ce25SScott Long 		 * If we are transitioning from tags to no-tags or
141152c9ce25SScott Long 		 * vice-versa, we need to carefully freeze and restart
141252c9ce25SScott Long 		 * the queue so that we don't overlap tagged and non-tagged
141352c9ce25SScott Long 		 * commands.  We also temporarily stop tags if there is
141452c9ce25SScott Long 		 * a change in transfer negotiation settings to allow
141552c9ce25SScott Long 		 * "tag-less" negotiation.
141652c9ce25SScott Long 		 */
141752c9ce25SScott Long 		if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
141852c9ce25SScott Long 		 || (device->inq_flags & SID_CmdQue) != 0)
141952c9ce25SScott Long 			device_tagenb = TRUE;
142052c9ce25SScott Long 		else
142152c9ce25SScott Long 			device_tagenb = FALSE;
142252c9ce25SScott Long 
142352c9ce25SScott Long 		if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
142452c9ce25SScott Long 		  && device_tagenb == FALSE)
142552c9ce25SScott Long 		 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
142652c9ce25SScott Long 		  && device_tagenb == TRUE)) {
142752c9ce25SScott Long 
142852c9ce25SScott Long 			if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
142952c9ce25SScott Long 				/*
143052c9ce25SScott Long 				 * Delay change to use tags until after a
143152c9ce25SScott Long 				 * few commands have gone to this device so
143252c9ce25SScott Long 				 * the controller has time to perform transfer
143352c9ce25SScott Long 				 * negotiations without tagged messages getting
143452c9ce25SScott Long 				 * in the way.
143552c9ce25SScott Long 				 */
143652c9ce25SScott Long 				device->tag_delay_count = CAM_TAG_DELAY_COUNT;
143752c9ce25SScott Long 				device->flags |= CAM_DEV_TAG_AFTER_COUNT;
143852c9ce25SScott Long 			} else {
143930a4094fSAlexander Motin 				xpt_stop_tags(cts->ccb_h.path);
144052c9ce25SScott Long 			}
144152c9ce25SScott Long 		}
144252c9ce25SScott Long 	}
144352c9ce25SScott Long 	if (async_update == FALSE)
144452c9ce25SScott Long 		(*(sim->sim_action))(sim, (union ccb *)cts);
144552c9ce25SScott Long }
144652c9ce25SScott Long 
144752c9ce25SScott Long /*
144852c9ce25SScott Long  * Handle any per-device event notifications that require action by the XPT.
144952c9ce25SScott Long  */
145052c9ce25SScott Long static void
145152c9ce25SScott Long ata_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
145252c9ce25SScott Long 	      struct cam_ed *device, void *async_arg)
145352c9ce25SScott Long {
145452c9ce25SScott Long 	cam_status status;
145552c9ce25SScott Long 	struct cam_path newpath;
145652c9ce25SScott Long 
145752c9ce25SScott Long 	/*
145852c9ce25SScott Long 	 * We only need to handle events for real devices.
145952c9ce25SScott Long 	 */
146052c9ce25SScott Long 	if (target->target_id == CAM_TARGET_WILDCARD
146152c9ce25SScott Long 	 || device->lun_id == CAM_LUN_WILDCARD)
146252c9ce25SScott Long 		return;
146352c9ce25SScott Long 
146452c9ce25SScott Long 	/*
146552c9ce25SScott Long 	 * We need our own path with wildcards expanded to
146652c9ce25SScott Long 	 * handle certain types of events.
146752c9ce25SScott Long 	 */
146852c9ce25SScott Long 	if ((async_code == AC_SENT_BDR)
146952c9ce25SScott Long 	 || (async_code == AC_BUS_RESET)
147052c9ce25SScott Long 	 || (async_code == AC_INQ_CHANGED))
147152c9ce25SScott Long 		status = xpt_compile_path(&newpath, NULL,
147252c9ce25SScott Long 					  bus->path_id,
147352c9ce25SScott Long 					  target->target_id,
147452c9ce25SScott Long 					  device->lun_id);
147552c9ce25SScott Long 	else
147652c9ce25SScott Long 		status = CAM_REQ_CMP_ERR;
147752c9ce25SScott Long 
147852c9ce25SScott Long 	if (status == CAM_REQ_CMP) {
147952c9ce25SScott Long 		if (async_code == AC_INQ_CHANGED) {
148052c9ce25SScott Long 			/*
148152c9ce25SScott Long 			 * We've sent a start unit command, or
148252c9ce25SScott Long 			 * something similar to a device that
148352c9ce25SScott Long 			 * may have caused its inquiry data to
148452c9ce25SScott Long 			 * change. So we re-scan the device to
148552c9ce25SScott Long 			 * refresh the inquiry data for it.
148652c9ce25SScott Long 			 */
148752c9ce25SScott Long 			ata_scan_lun(newpath.periph, &newpath,
148852c9ce25SScott Long 				     CAM_EXPECT_INQ_CHANGE, NULL);
148952c9ce25SScott Long 		}
149052c9ce25SScott Long 		xpt_release_path(&newpath);
1491f98d7a47SAlexander Motin 	} else if (async_code == AC_LOST_DEVICE &&
1492f98d7a47SAlexander Motin 	    (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
149352c9ce25SScott Long 		device->flags |= CAM_DEV_UNCONFIGURED;
1494f98d7a47SAlexander Motin 		xpt_release_device(device);
149552c9ce25SScott Long 	} else if (async_code == AC_TRANSFER_NEG) {
149652c9ce25SScott Long 		struct ccb_trans_settings *settings;
149752c9ce25SScott Long 
149852c9ce25SScott Long 		settings = (struct ccb_trans_settings *)async_arg;
149930a4094fSAlexander Motin 		ata_set_transfer_settings(settings, device,
150052c9ce25SScott Long 					  /*async_update*/TRUE);
150152c9ce25SScott Long 	}
150252c9ce25SScott Long }
150352c9ce25SScott Long 
1504