xref: /freebsd/sys/cam/ata/ata_xpt.c (revision da6808c1)
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/interrupt.h>
4152c9ce25SScott Long #include <sys/sbuf.h>
4252c9ce25SScott Long 
4352c9ce25SScott Long #include <sys/lock.h>
4452c9ce25SScott Long #include <sys/mutex.h>
4552c9ce25SScott Long #include <sys/sysctl.h>
4652c9ce25SScott Long 
4752c9ce25SScott Long #ifdef PC98
4852c9ce25SScott Long #include <pc98/pc98/pc98_machdep.h>	/* geometry translation */
4952c9ce25SScott Long #endif
5052c9ce25SScott Long 
5152c9ce25SScott Long #include <cam/cam.h>
5252c9ce25SScott Long #include <cam/cam_ccb.h>
5352c9ce25SScott Long #include <cam/cam_queue.h>
5452c9ce25SScott Long #include <cam/cam_periph.h>
5552c9ce25SScott Long #include <cam/cam_sim.h>
5652c9ce25SScott Long #include <cam/cam_xpt.h>
5752c9ce25SScott Long #include <cam/cam_xpt_sim.h>
5852c9ce25SScott Long #include <cam/cam_xpt_periph.h>
5952c9ce25SScott Long #include <cam/cam_xpt_internal.h>
6052c9ce25SScott Long #include <cam/cam_debug.h>
6152c9ce25SScott Long 
6252c9ce25SScott Long #include <cam/scsi/scsi_all.h>
6352c9ce25SScott Long #include <cam/scsi/scsi_message.h>
6452c9ce25SScott Long #include <cam/ata/ata_all.h>
6552c9ce25SScott Long #include <machine/stdarg.h>	/* for xpt_print below */
6652c9ce25SScott Long #include "opt_cam.h"
6752c9ce25SScott Long 
6830a4094fSAlexander Motin struct ata_quirk_entry {
6952c9ce25SScott Long 	struct scsi_inquiry_pattern inq_pat;
7052c9ce25SScott Long 	u_int8_t quirks;
7130a4094fSAlexander Motin #define	CAM_QUIRK_MAXTAGS	0x01
7252c9ce25SScott Long 	u_int maxtags;
7352c9ce25SScott Long };
7452c9ce25SScott Long 
7552c9ce25SScott Long static periph_init_t probe_periph_init;
7652c9ce25SScott Long 
7752c9ce25SScott Long static struct periph_driver probe_driver =
7852c9ce25SScott Long {
79f09f8e3eSAlexander Motin 	probe_periph_init, "aprobe",
801e637ba6SAlexander Motin 	TAILQ_HEAD_INITIALIZER(probe_driver.units), /* generation */ 0,
811e637ba6SAlexander Motin 	CAM_PERIPH_DRV_EARLY
8252c9ce25SScott Long };
8352c9ce25SScott Long 
84f09f8e3eSAlexander Motin PERIPHDRIVER_DECLARE(aprobe, probe_driver);
8552c9ce25SScott Long 
8652c9ce25SScott Long typedef enum {
8752c9ce25SScott Long 	PROBE_RESET,
8852c9ce25SScott Long 	PROBE_IDENTIFY,
894ef08dc5SAlexander Motin 	PROBE_SPINUP,
9052c9ce25SScott Long 	PROBE_SETMODE,
91da6808c1SAlexander Motin 	PROBE_SETPM,
92da6808c1SAlexander Motin 	PROBE_SETAPST,
93da6808c1SAlexander Motin 	PROBE_SETDMAAA,
941e637ba6SAlexander Motin 	PROBE_SET_MULTI,
9552c9ce25SScott Long 	PROBE_INQUIRY,
9652c9ce25SScott Long 	PROBE_FULL_INQUIRY,
9752c9ce25SScott Long 	PROBE_PM_PID,
9852c9ce25SScott Long 	PROBE_PM_PRV,
9952c9ce25SScott Long 	PROBE_INVALID
10052c9ce25SScott Long } probe_action;
10152c9ce25SScott Long 
10252c9ce25SScott Long static char *probe_action_text[] = {
10352c9ce25SScott Long 	"PROBE_RESET",
10452c9ce25SScott Long 	"PROBE_IDENTIFY",
1054ef08dc5SAlexander Motin 	"PROBE_SPINUP",
10652c9ce25SScott Long 	"PROBE_SETMODE",
107da6808c1SAlexander Motin 	"PROBE_SETPM",
108da6808c1SAlexander Motin 	"PROBE_SETAPST",
109da6808c1SAlexander Motin 	"PROBE_SETDMAAA",
1101e637ba6SAlexander Motin 	"PROBE_SET_MULTI",
11152c9ce25SScott Long 	"PROBE_INQUIRY",
11252c9ce25SScott Long 	"PROBE_FULL_INQUIRY",
11352c9ce25SScott Long 	"PROBE_PM_PID",
11452c9ce25SScott Long 	"PROBE_PM_PRV",
11552c9ce25SScott Long 	"PROBE_INVALID"
11652c9ce25SScott Long };
11752c9ce25SScott Long 
11852c9ce25SScott Long #define PROBE_SET_ACTION(softc, newaction)	\
11952c9ce25SScott Long do {									\
12052c9ce25SScott Long 	char **text;							\
12152c9ce25SScott Long 	text = probe_action_text;					\
12252c9ce25SScott Long 	CAM_DEBUG((softc)->periph->path, CAM_DEBUG_INFO,		\
12352c9ce25SScott Long 	    ("Probe %s to %s\n", text[(softc)->action],			\
12452c9ce25SScott Long 	    text[(newaction)]));					\
12552c9ce25SScott Long 	(softc)->action = (newaction);					\
12652c9ce25SScott Long } while(0)
12752c9ce25SScott Long 
12852c9ce25SScott Long typedef enum {
12952c9ce25SScott Long 	PROBE_NO_ANNOUNCE	= 0x04
13052c9ce25SScott Long } probe_flags;
13152c9ce25SScott Long 
13252c9ce25SScott Long typedef struct {
13352c9ce25SScott Long 	TAILQ_HEAD(, ccb_hdr) request_ccbs;
134a9b8edb1SAlexander Motin 	struct ata_params	ident_data;
13552c9ce25SScott Long 	probe_action	action;
13652c9ce25SScott Long 	probe_flags	flags;
13752c9ce25SScott Long 	uint32_t	pm_pid;
13852c9ce25SScott Long 	uint32_t	pm_prv;
13983c5d981SAlexander Motin 	int		restart;
1404ef08dc5SAlexander Motin 	int		spinup;
141da6808c1SAlexander Motin 	u_int		caps;
14252c9ce25SScott Long 	struct cam_periph *periph;
14352c9ce25SScott Long } probe_softc;
14452c9ce25SScott Long 
14530a4094fSAlexander Motin static struct ata_quirk_entry ata_quirk_table[] =
14652c9ce25SScott Long {
14752c9ce25SScott Long 	{
14852c9ce25SScott Long 		/* Default tagged queuing parameters for all devices */
14952c9ce25SScott Long 		{
15052c9ce25SScott Long 		  T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
15152c9ce25SScott Long 		  /*vendor*/"*", /*product*/"*", /*revision*/"*"
15252c9ce25SScott Long 		},
15330a4094fSAlexander Motin 		/*quirks*/0, /*maxtags*/0
15452c9ce25SScott Long 	},
15552c9ce25SScott Long };
15652c9ce25SScott Long 
15730a4094fSAlexander Motin static const int ata_quirk_table_size =
15830a4094fSAlexander Motin 	sizeof(ata_quirk_table) / sizeof(*ata_quirk_table);
15952c9ce25SScott Long 
16052c9ce25SScott Long static cam_status	proberegister(struct cam_periph *periph,
16152c9ce25SScott Long 				      void *arg);
16252c9ce25SScott Long static void	 probeschedule(struct cam_periph *probe_periph);
16352c9ce25SScott Long static void	 probestart(struct cam_periph *periph, union ccb *start_ccb);
16452c9ce25SScott Long //static void	 proberequestdefaultnegotiation(struct cam_periph *periph);
16552c9ce25SScott Long //static int       proberequestbackoff(struct cam_periph *periph,
16652c9ce25SScott Long //				     struct cam_ed *device);
16752c9ce25SScott Long static void	 probedone(struct cam_periph *periph, union ccb *done_ccb);
16852c9ce25SScott Long static void	 probecleanup(struct cam_periph *periph);
16930a4094fSAlexander Motin static void	 ata_find_quirk(struct cam_ed *device);
17052c9ce25SScott Long static void	 ata_scan_bus(struct cam_periph *periph, union ccb *ccb);
17152c9ce25SScott Long static void	 ata_scan_lun(struct cam_periph *periph,
17252c9ce25SScott Long 			       struct cam_path *path, cam_flags flags,
17352c9ce25SScott Long 			       union ccb *ccb);
17452c9ce25SScott Long static void	 xptscandone(struct cam_periph *periph, union ccb *done_ccb);
17552c9ce25SScott Long static struct cam_ed *
17652c9ce25SScott Long 		 ata_alloc_device(struct cam_eb *bus, struct cam_et *target,
17752c9ce25SScott Long 				   lun_id_t lun_id);
17852c9ce25SScott Long static void	 ata_device_transport(struct cam_path *path);
17930a4094fSAlexander Motin static void	 ata_set_transfer_settings(struct ccb_trans_settings *cts,
18052c9ce25SScott Long 					    struct cam_ed *device,
18152c9ce25SScott Long 					    int async_update);
18252c9ce25SScott Long static void	 ata_dev_async(u_int32_t async_code,
18352c9ce25SScott Long 				struct cam_eb *bus,
18452c9ce25SScott Long 				struct cam_et *target,
18552c9ce25SScott Long 				struct cam_ed *device,
18652c9ce25SScott Long 				void *async_arg);
18752c9ce25SScott Long static void	 ata_action(union ccb *start_ccb);
18857079b17SAlexander Motin static void	 ata_announce_periph(struct cam_periph *periph);
18952c9ce25SScott Long 
19052c9ce25SScott Long static struct xpt_xport ata_xport = {
19152c9ce25SScott Long 	.alloc_device = ata_alloc_device,
19252c9ce25SScott Long 	.action = ata_action,
19352c9ce25SScott Long 	.async = ata_dev_async,
19457079b17SAlexander Motin 	.announce = ata_announce_periph,
19552c9ce25SScott Long };
19652c9ce25SScott Long 
19752c9ce25SScott Long struct xpt_xport *
19852c9ce25SScott Long ata_get_xport(void)
19952c9ce25SScott Long {
20052c9ce25SScott Long 	return (&ata_xport);
20152c9ce25SScott Long }
20252c9ce25SScott Long 
20352c9ce25SScott Long static void
20452c9ce25SScott Long probe_periph_init()
20552c9ce25SScott Long {
20652c9ce25SScott Long }
20752c9ce25SScott Long 
20852c9ce25SScott Long static cam_status
20952c9ce25SScott Long proberegister(struct cam_periph *periph, void *arg)
21052c9ce25SScott Long {
21152c9ce25SScott Long 	union ccb *request_ccb;	/* CCB representing the probe request */
21252c9ce25SScott Long 	cam_status status;
21352c9ce25SScott Long 	probe_softc *softc;
21452c9ce25SScott Long 
21552c9ce25SScott Long 	request_ccb = (union ccb *)arg;
21652c9ce25SScott Long 	if (periph == NULL) {
21752c9ce25SScott Long 		printf("proberegister: periph was NULL!!\n");
21852c9ce25SScott Long 		return(CAM_REQ_CMP_ERR);
21952c9ce25SScott Long 	}
22052c9ce25SScott Long 
22152c9ce25SScott Long 	if (request_ccb == NULL) {
22252c9ce25SScott Long 		printf("proberegister: no probe CCB, "
22352c9ce25SScott Long 		       "can't register device\n");
22452c9ce25SScott Long 		return(CAM_REQ_CMP_ERR);
22552c9ce25SScott Long 	}
22652c9ce25SScott Long 
2274ef08dc5SAlexander Motin 	softc = (probe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_ZERO | M_NOWAIT);
22852c9ce25SScott Long 
22952c9ce25SScott Long 	if (softc == NULL) {
23052c9ce25SScott Long 		printf("proberegister: Unable to probe new device. "
23152c9ce25SScott Long 		       "Unable to allocate softc\n");
23252c9ce25SScott Long 		return(CAM_REQ_CMP_ERR);
23352c9ce25SScott Long 	}
23452c9ce25SScott Long 	TAILQ_INIT(&softc->request_ccbs);
23552c9ce25SScott Long 	TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
23652c9ce25SScott Long 			  periph_links.tqe);
23752c9ce25SScott Long 	softc->flags = 0;
23852c9ce25SScott Long 	periph->softc = softc;
23952c9ce25SScott Long 	softc->periph = periph;
24052c9ce25SScott Long 	softc->action = PROBE_INVALID;
24152c9ce25SScott Long 	status = cam_periph_acquire(periph);
24252c9ce25SScott Long 	if (status != CAM_REQ_CMP) {
24352c9ce25SScott Long 		return (status);
24452c9ce25SScott Long 	}
24552c9ce25SScott Long 	/*
24683c5d981SAlexander Motin 	 * Ensure nobody slip in until probe finish.
24752c9ce25SScott Long 	 */
24883c5d981SAlexander Motin 	cam_freeze_devq_arg(periph->path,
24983c5d981SAlexander Motin 	    RELSIM_RELEASE_RUNLEVEL, CAM_RL_XPT + 1);
25052c9ce25SScott Long 	probeschedule(periph);
25152c9ce25SScott Long 	return(CAM_REQ_CMP);
25252c9ce25SScott Long }
25352c9ce25SScott Long 
25452c9ce25SScott Long static void
25552c9ce25SScott Long probeschedule(struct cam_periph *periph)
25652c9ce25SScott Long {
25752c9ce25SScott Long 	union ccb *ccb;
25852c9ce25SScott Long 	probe_softc *softc;
25952c9ce25SScott Long 
26052c9ce25SScott Long 	softc = (probe_softc *)periph->softc;
26152c9ce25SScott Long 	ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
26252c9ce25SScott Long 
26365d0fb03SAlexander Motin 	if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) ||
26465d0fb03SAlexander Motin 	    periph->path->device->protocol == PROTO_SATAPM)
26552c9ce25SScott Long 		PROBE_SET_ACTION(softc, PROBE_RESET);
26652c9ce25SScott Long 	else
26752c9ce25SScott Long 		PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
26852c9ce25SScott Long 
26952c9ce25SScott Long 	if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
27052c9ce25SScott Long 		softc->flags |= PROBE_NO_ANNOUNCE;
27152c9ce25SScott Long 	else
27252c9ce25SScott Long 		softc->flags &= ~PROBE_NO_ANNOUNCE;
27352c9ce25SScott Long 
27483c5d981SAlexander Motin 	xpt_schedule(periph, CAM_PRIORITY_XPT);
27552c9ce25SScott Long }
27652c9ce25SScott Long 
27752c9ce25SScott Long static void
27852c9ce25SScott Long probestart(struct cam_periph *periph, union ccb *start_ccb)
27952c9ce25SScott Long {
280c8039fc6SAlexander Motin 	struct ccb_trans_settings cts;
28152c9ce25SScott Long 	struct ccb_ataio *ataio;
28252c9ce25SScott Long 	struct ccb_scsiio *csio;
28352c9ce25SScott Long 	probe_softc *softc;
2841e637ba6SAlexander Motin 	struct cam_path *path;
2851e637ba6SAlexander Motin 	struct ata_params *ident_buf;
28652c9ce25SScott Long 
28752c9ce25SScott Long 	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
28852c9ce25SScott Long 
28952c9ce25SScott Long 	softc = (probe_softc *)periph->softc;
2901e637ba6SAlexander Motin 	path = start_ccb->ccb_h.path;
29152c9ce25SScott Long 	ataio = &start_ccb->ataio;
29252c9ce25SScott Long 	csio = &start_ccb->csio;
2931e637ba6SAlexander Motin 	ident_buf = &periph->path->device->ident_data;
29452c9ce25SScott Long 
29583c5d981SAlexander Motin 	if (softc->restart) {
29683c5d981SAlexander Motin 		softc->restart = 0;
29783c5d981SAlexander Motin 		if ((path->device->flags & CAM_DEV_UNCONFIGURED) ||
29883c5d981SAlexander Motin 		    path->device->protocol == PROTO_SATAPM)
29983c5d981SAlexander Motin 			softc->action = PROBE_RESET;
30083c5d981SAlexander Motin 		else
30183c5d981SAlexander Motin 			softc->action = PROBE_IDENTIFY;
30283c5d981SAlexander Motin 	}
30352c9ce25SScott Long 	switch (softc->action) {
30452c9ce25SScott Long 	case PROBE_RESET:
30552c9ce25SScott Long 		cam_fill_ataio(ataio,
30652c9ce25SScott Long 		      0,
30752c9ce25SScott Long 		      probedone,
30852c9ce25SScott Long 		      /*flags*/CAM_DIR_NONE,
30965d0fb03SAlexander Motin 		      0,
31052c9ce25SScott Long 		      /*data_ptr*/NULL,
31152c9ce25SScott Long 		      /*dxfer_len*/0,
31283c5d981SAlexander Motin 		      15 * 1000);
31352c9ce25SScott Long 		ata_reset_cmd(ataio);
31452c9ce25SScott Long 		break;
31552c9ce25SScott Long 	case PROBE_IDENTIFY:
31652c9ce25SScott Long 		cam_fill_ataio(ataio,
31752c9ce25SScott Long 		      1,
31852c9ce25SScott Long 		      probedone,
31952c9ce25SScott Long 		      /*flags*/CAM_DIR_IN,
32065d0fb03SAlexander Motin 		      0,
321a9b8edb1SAlexander Motin 		      /*data_ptr*/(u_int8_t *)&softc->ident_data,
322a9b8edb1SAlexander Motin 		      /*dxfer_len*/sizeof(softc->ident_data),
32352c9ce25SScott Long 		      30 * 1000);
32452c9ce25SScott Long 		if (periph->path->device->protocol == PROTO_ATA)
3257606b445SAlexander Motin 			ata_28bit_cmd(ataio, ATA_ATA_IDENTIFY, 0, 0, 0);
32652c9ce25SScott Long 		else
3277606b445SAlexander Motin 			ata_28bit_cmd(ataio, ATA_ATAPI_IDENTIFY, 0, 0, 0);
32852c9ce25SScott Long 		break;
3294ef08dc5SAlexander Motin 	case PROBE_SPINUP:
3304ef08dc5SAlexander Motin 		if (bootverbose)
3314ef08dc5SAlexander Motin 			xpt_print(path, "Spinning up device\n");
3324ef08dc5SAlexander Motin 		cam_fill_ataio(ataio,
3334ef08dc5SAlexander Motin 		      1,
3344ef08dc5SAlexander Motin 		      probedone,
3354ef08dc5SAlexander Motin 		      /*flags*/CAM_DIR_NONE | CAM_HIGH_POWER,
3364ef08dc5SAlexander Motin 		      0,
3374ef08dc5SAlexander Motin 		      /*data_ptr*/NULL,
3384ef08dc5SAlexander Motin 		      /*dxfer_len*/0,
3394ef08dc5SAlexander Motin 		      30 * 1000);
3404ef08dc5SAlexander Motin 		ata_28bit_cmd(ataio, ATA_SETFEATURES, ATA_SF_PUIS_SPINUP, 0, 0);
3414ef08dc5SAlexander Motin 		break;
34252c9ce25SScott Long 	case PROBE_SETMODE:
343c8039fc6SAlexander Motin 	{
344c8039fc6SAlexander Motin 		int mode, wantmode;
345c8039fc6SAlexander Motin 
346c8039fc6SAlexander Motin 		mode = 0;
347c8039fc6SAlexander Motin 		/* Fetch user modes from SIM. */
348c8039fc6SAlexander Motin 		bzero(&cts, sizeof(cts));
34983c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
350c8039fc6SAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
351c8039fc6SAlexander Motin 		cts.type = CTS_TYPE_USER_SETTINGS;
352c8039fc6SAlexander Motin 		xpt_action((union ccb *)&cts);
353c8039fc6SAlexander Motin 		if (path->device->transport == XPORT_ATA) {
354c8039fc6SAlexander Motin 			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_MODE)
355c8039fc6SAlexander Motin 				mode = cts.xport_specific.ata.mode;
356c8039fc6SAlexander Motin 		} else {
3571a6f09b8SAlexander Motin 			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_MODE)
358c8039fc6SAlexander Motin 				mode = cts.xport_specific.sata.mode;
359c8039fc6SAlexander Motin 		}
360c8039fc6SAlexander Motin negotiate:
361c8039fc6SAlexander Motin 		/* Honor device capabilities. */
362c8039fc6SAlexander Motin 		wantmode = mode = ata_max_mode(ident_buf, mode);
363c8039fc6SAlexander Motin 		/* Report modes to SIM. */
364c8039fc6SAlexander Motin 		bzero(&cts, sizeof(cts));
36583c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
366c8039fc6SAlexander Motin 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
367c8039fc6SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
368c8039fc6SAlexander Motin 		if (path->device->transport == XPORT_ATA) {
369c8039fc6SAlexander Motin 			cts.xport_specific.ata.mode = mode;
370c8039fc6SAlexander Motin 			cts.xport_specific.ata.valid = CTS_ATA_VALID_MODE;
371c8039fc6SAlexander Motin 		} else {
372c8039fc6SAlexander Motin 			cts.xport_specific.sata.mode = mode;
373c8039fc6SAlexander Motin 			cts.xport_specific.sata.valid = CTS_SATA_VALID_MODE;
374c8039fc6SAlexander Motin 		}
375c8039fc6SAlexander Motin 		xpt_action((union ccb *)&cts);
376066f913aSAlexander Motin 		/* Fetch current modes from SIM. */
377c8039fc6SAlexander Motin 		bzero(&cts, sizeof(cts));
37883c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
379c8039fc6SAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
380c8039fc6SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
381c8039fc6SAlexander Motin 		xpt_action((union ccb *)&cts);
382c8039fc6SAlexander Motin 		if (path->device->transport == XPORT_ATA) {
383c8039fc6SAlexander Motin 			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_MODE)
384c8039fc6SAlexander Motin 				mode = cts.xport_specific.ata.mode;
385c8039fc6SAlexander Motin 		} else {
386c8039fc6SAlexander Motin 			if (cts.xport_specific.ata.valid & CTS_SATA_VALID_MODE)
387c8039fc6SAlexander Motin 				mode = cts.xport_specific.sata.mode;
388c8039fc6SAlexander Motin 		}
389c8039fc6SAlexander Motin 		/* If SIM disagree - renegotiate. */
390c8039fc6SAlexander Motin 		if (mode != wantmode)
391c8039fc6SAlexander Motin 			goto negotiate;
39252c9ce25SScott Long 		cam_fill_ataio(ataio,
39352c9ce25SScott Long 		      1,
39452c9ce25SScott Long 		      probedone,
3955daa555dSAlexander Motin 		      /*flags*/CAM_DIR_NONE,
3965daa555dSAlexander Motin 		      0,
3975daa555dSAlexander Motin 		      /*data_ptr*/NULL,
3985daa555dSAlexander Motin 		      /*dxfer_len*/0,
39952c9ce25SScott Long 		      30 * 1000);
400c8039fc6SAlexander Motin 		ata_28bit_cmd(ataio, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
40152c9ce25SScott Long 		break;
402c8039fc6SAlexander Motin 	}
403da6808c1SAlexander Motin 	case PROBE_SETPM:
404da6808c1SAlexander Motin 		cam_fill_ataio(ataio,
405da6808c1SAlexander Motin 		    1,
406da6808c1SAlexander Motin 		    probedone,
407da6808c1SAlexander Motin 		    CAM_DIR_NONE,
408da6808c1SAlexander Motin 		    0,
409da6808c1SAlexander Motin 		    NULL,
410da6808c1SAlexander Motin 		    0,
411da6808c1SAlexander Motin 		    30*1000);
412da6808c1SAlexander Motin 		ata_28bit_cmd(ataio, ATA_SETFEATURES,
413da6808c1SAlexander Motin 		    (softc->caps & CTS_SATA_CAPS_H_PMREQ) ? 0x10 : 0x90,
414da6808c1SAlexander Motin 		    0, 0x03);
415da6808c1SAlexander Motin 		break;
416da6808c1SAlexander Motin 	case PROBE_SETAPST:
417da6808c1SAlexander Motin 		cam_fill_ataio(ataio,
418da6808c1SAlexander Motin 		    1,
419da6808c1SAlexander Motin 		    probedone,
420da6808c1SAlexander Motin 		    CAM_DIR_NONE,
421da6808c1SAlexander Motin 		    0,
422da6808c1SAlexander Motin 		    NULL,
423da6808c1SAlexander Motin 		    0,
424da6808c1SAlexander Motin 		    30*1000);
425da6808c1SAlexander Motin 		ata_28bit_cmd(ataio, ATA_SETFEATURES,
426da6808c1SAlexander Motin 		    (softc->caps & CTS_SATA_CAPS_H_APST) ? 0x10 : 0x90,
427da6808c1SAlexander Motin 		    0, 0x07);
428da6808c1SAlexander Motin 		break;
429da6808c1SAlexander Motin 	case PROBE_SETDMAAA:
430da6808c1SAlexander Motin 		cam_fill_ataio(ataio,
431da6808c1SAlexander Motin 		    1,
432da6808c1SAlexander Motin 		    probedone,
433da6808c1SAlexander Motin 		    CAM_DIR_NONE,
434da6808c1SAlexander Motin 		    0,
435da6808c1SAlexander Motin 		    NULL,
436da6808c1SAlexander Motin 		    0,
437da6808c1SAlexander Motin 		    30*1000);
438da6808c1SAlexander Motin 		ata_28bit_cmd(ataio, ATA_SETFEATURES,
439da6808c1SAlexander Motin 		    (softc->caps & CTS_SATA_CAPS_H_DMAAA) ? 0x10 : 0x90,
440da6808c1SAlexander Motin 		    0, 0x02);
441da6808c1SAlexander Motin 		break;
4421e637ba6SAlexander Motin 	case PROBE_SET_MULTI:
4431e637ba6SAlexander Motin 	{
444066f913aSAlexander Motin 		u_int sectors, bytecount;
4451e637ba6SAlexander Motin 
446066f913aSAlexander Motin 		bytecount = 8192;	/* SATA maximum */
447066f913aSAlexander Motin 		/* Fetch user bytecount from SIM. */
448066f913aSAlexander Motin 		bzero(&cts, sizeof(cts));
44983c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
450066f913aSAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
451066f913aSAlexander Motin 		cts.type = CTS_TYPE_USER_SETTINGS;
452066f913aSAlexander Motin 		xpt_action((union ccb *)&cts);
453066f913aSAlexander Motin 		if (path->device->transport == XPORT_ATA) {
454066f913aSAlexander Motin 			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
455066f913aSAlexander Motin 				bytecount = cts.xport_specific.ata.bytecount;
456066f913aSAlexander Motin 		} else {
457066f913aSAlexander Motin 			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
458066f913aSAlexander Motin 				bytecount = cts.xport_specific.sata.bytecount;
459066f913aSAlexander Motin 		}
460066f913aSAlexander Motin 		/* Honor device capabilities. */
461066f913aSAlexander Motin 		sectors = max(1, min(ident_buf->sectors_intr & 0xff,
462066f913aSAlexander Motin 		    bytecount / ata_logical_sector_size(ident_buf)));
4631e637ba6SAlexander Motin 		/* Report bytecount to SIM. */
4641e637ba6SAlexander Motin 		bzero(&cts, sizeof(cts));
46583c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
4661e637ba6SAlexander Motin 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
4671e637ba6SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
4681e637ba6SAlexander Motin 		if (path->device->transport == XPORT_ATA) {
469c1bd46c2SAlexander Motin 			cts.xport_specific.ata.bytecount = sectors *
470c1bd46c2SAlexander Motin 			    ata_logical_sector_size(ident_buf);
4711e637ba6SAlexander Motin 			cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT;
4721e637ba6SAlexander Motin 		} else {
473c1bd46c2SAlexander Motin 			cts.xport_specific.sata.bytecount = sectors *
474c1bd46c2SAlexander Motin 			    ata_logical_sector_size(ident_buf);
4751e637ba6SAlexander Motin 			cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT;
4761e637ba6SAlexander Motin 		}
4771e637ba6SAlexander Motin 		xpt_action((union ccb *)&cts);
478066f913aSAlexander Motin 		/* Fetch current bytecount from SIM. */
479066f913aSAlexander Motin 		bzero(&cts, sizeof(cts));
48083c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
481066f913aSAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
482066f913aSAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
483066f913aSAlexander Motin 		xpt_action((union ccb *)&cts);
484066f913aSAlexander Motin 		if (path->device->transport == XPORT_ATA) {
485066f913aSAlexander Motin 			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
486066f913aSAlexander Motin 				bytecount = cts.xport_specific.ata.bytecount;
487066f913aSAlexander Motin 		} else {
488066f913aSAlexander Motin 			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
489066f913aSAlexander Motin 				bytecount = cts.xport_specific.sata.bytecount;
490066f913aSAlexander Motin 		}
491066f913aSAlexander Motin 		sectors = bytecount / ata_logical_sector_size(ident_buf);
4921e637ba6SAlexander Motin 
4931e637ba6SAlexander Motin 		cam_fill_ataio(ataio,
4941e637ba6SAlexander Motin 		    1,
4951e637ba6SAlexander Motin 		    probedone,
4961e637ba6SAlexander Motin 		    CAM_DIR_NONE,
4971e637ba6SAlexander Motin 		    0,
4981e637ba6SAlexander Motin 		    NULL,
4991e637ba6SAlexander Motin 		    0,
5001e637ba6SAlexander Motin 		    30*1000);
5011e637ba6SAlexander Motin 		ata_28bit_cmd(ataio, ATA_SET_MULTI, 0, 0, sectors);
5021e637ba6SAlexander Motin 		break;
50352c9ce25SScott Long 	}
50452c9ce25SScott Long 	case PROBE_INQUIRY:
505066f913aSAlexander Motin 	{
506066f913aSAlexander Motin 		u_int bytecount;
507066f913aSAlexander Motin 
508066f913aSAlexander Motin 		bytecount = 8192;	/* SATA maximum */
509066f913aSAlexander Motin 		/* Fetch user bytecount from SIM. */
510066f913aSAlexander Motin 		bzero(&cts, sizeof(cts));
51183c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
512066f913aSAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
513066f913aSAlexander Motin 		cts.type = CTS_TYPE_USER_SETTINGS;
514066f913aSAlexander Motin 		xpt_action((union ccb *)&cts);
515066f913aSAlexander Motin 		if (path->device->transport == XPORT_ATA) {
516066f913aSAlexander Motin 			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
517066f913aSAlexander Motin 				bytecount = cts.xport_specific.ata.bytecount;
518066f913aSAlexander Motin 		} else {
519066f913aSAlexander Motin 			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
520066f913aSAlexander Motin 				bytecount = cts.xport_specific.sata.bytecount;
521066f913aSAlexander Motin 		}
522066f913aSAlexander Motin 		/* Honor device capabilities. */
523066f913aSAlexander Motin 		bytecount &= ~1;
524066f913aSAlexander Motin 		bytecount = max(2, min(65534, bytecount));
525066f913aSAlexander Motin 		if (ident_buf->satacapabilities != 0x0000 &&
526066f913aSAlexander Motin 		    ident_buf->satacapabilities != 0xffff) {
527066f913aSAlexander Motin 			bytecount = min(8192, bytecount);
528066f913aSAlexander Motin 		}
529066f913aSAlexander Motin 		/* Report bytecount to SIM. */
530066f913aSAlexander Motin 		bzero(&cts, sizeof(cts));
53183c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
532066f913aSAlexander Motin 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
533066f913aSAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
534066f913aSAlexander Motin 		if (path->device->transport == XPORT_ATA) {
535066f913aSAlexander Motin 			cts.xport_specific.ata.bytecount = bytecount;
536066f913aSAlexander Motin 			cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT;
537066f913aSAlexander Motin 		} else {
538066f913aSAlexander Motin 			cts.xport_specific.sata.bytecount = bytecount;
539066f913aSAlexander Motin 			cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT;
540066f913aSAlexander Motin 		}
541066f913aSAlexander Motin 		xpt_action((union ccb *)&cts);
542066f913aSAlexander Motin 		/* FALLTHROUGH */
543066f913aSAlexander Motin 	}
54452c9ce25SScott Long 	case PROBE_FULL_INQUIRY:
54552c9ce25SScott Long 	{
54652c9ce25SScott Long 		u_int inquiry_len;
54752c9ce25SScott Long 		struct scsi_inquiry_data *inq_buf =
54852c9ce25SScott Long 		    &periph->path->device->inq_data;
54952c9ce25SScott Long 
55052c9ce25SScott Long 		if (softc->action == PROBE_INQUIRY)
55152c9ce25SScott Long 			inquiry_len = SHORT_INQUIRY_LENGTH;
55252c9ce25SScott Long 		else
55352c9ce25SScott Long 			inquiry_len = SID_ADDITIONAL_LENGTH(inq_buf);
55452c9ce25SScott Long 		/*
55552c9ce25SScott Long 		 * Some parallel SCSI devices fail to send an
55652c9ce25SScott Long 		 * ignore wide residue message when dealing with
55752c9ce25SScott Long 		 * odd length inquiry requests.  Round up to be
55852c9ce25SScott Long 		 * safe.
55952c9ce25SScott Long 		 */
56052c9ce25SScott Long 		inquiry_len = roundup2(inquiry_len, 2);
56152c9ce25SScott Long 		scsi_inquiry(csio,
56252c9ce25SScott Long 			     /*retries*/1,
56352c9ce25SScott Long 			     probedone,
56452c9ce25SScott Long 			     MSG_SIMPLE_Q_TAG,
56552c9ce25SScott Long 			     (u_int8_t *)inq_buf,
56652c9ce25SScott Long 			     inquiry_len,
56752c9ce25SScott Long 			     /*evpd*/FALSE,
56852c9ce25SScott Long 			     /*page_code*/0,
56952c9ce25SScott Long 			     SSD_MIN_SIZE,
57052c9ce25SScott Long 			     /*timeout*/60 * 1000);
57152c9ce25SScott Long 		break;
57252c9ce25SScott Long 	}
57352c9ce25SScott Long 	case PROBE_PM_PID:
57452c9ce25SScott Long 		cam_fill_ataio(ataio,
57552c9ce25SScott Long 		      1,
57652c9ce25SScott Long 		      probedone,
57752c9ce25SScott Long 		      /*flags*/CAM_DIR_NONE,
57865d0fb03SAlexander Motin 		      0,
57952c9ce25SScott Long 		      /*data_ptr*/NULL,
58052c9ce25SScott Long 		      /*dxfer_len*/0,
58152c9ce25SScott Long 		      10 * 1000);
58252c9ce25SScott Long 		ata_pm_read_cmd(ataio, 0, 15);
58352c9ce25SScott Long 		break;
58452c9ce25SScott Long 	case PROBE_PM_PRV:
58552c9ce25SScott Long 		cam_fill_ataio(ataio,
58652c9ce25SScott Long 		      1,
58752c9ce25SScott Long 		      probedone,
58852c9ce25SScott Long 		      /*flags*/CAM_DIR_NONE,
58965d0fb03SAlexander Motin 		      0,
59052c9ce25SScott Long 		      /*data_ptr*/NULL,
59152c9ce25SScott Long 		      /*dxfer_len*/0,
59252c9ce25SScott Long 		      10 * 1000);
59352c9ce25SScott Long 		ata_pm_read_cmd(ataio, 1, 15);
59452c9ce25SScott Long 		break;
59552c9ce25SScott Long 	case PROBE_INVALID:
5961e637ba6SAlexander Motin 		CAM_DEBUG(path, CAM_DEBUG_INFO,
59752c9ce25SScott Long 		    ("probestart: invalid action state\n"));
59852c9ce25SScott Long 	default:
59952c9ce25SScott Long 		break;
60052c9ce25SScott Long 	}
60152c9ce25SScott Long 	xpt_action(start_ccb);
60252c9ce25SScott Long }
60352c9ce25SScott Long #if 0
60452c9ce25SScott Long static void
60552c9ce25SScott Long proberequestdefaultnegotiation(struct cam_periph *periph)
60652c9ce25SScott Long {
60752c9ce25SScott Long 	struct ccb_trans_settings cts;
60852c9ce25SScott Long 
60983c5d981SAlexander Motin 	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
61052c9ce25SScott Long 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
61152c9ce25SScott Long 	cts.type = CTS_TYPE_USER_SETTINGS;
61252c9ce25SScott Long 	xpt_action((union ccb *)&cts);
61352c9ce25SScott Long 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
61452c9ce25SScott Long 		return;
61552c9ce25SScott Long 	}
61652c9ce25SScott Long 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
61752c9ce25SScott Long 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
61852c9ce25SScott Long 	xpt_action((union ccb *)&cts);
61952c9ce25SScott Long }
62052c9ce25SScott Long 
62152c9ce25SScott Long /*
62252c9ce25SScott Long  * Backoff Negotiation Code- only pertinent for SPI devices.
62352c9ce25SScott Long  */
62452c9ce25SScott Long static int
62552c9ce25SScott Long proberequestbackoff(struct cam_periph *periph, struct cam_ed *device)
62652c9ce25SScott Long {
62752c9ce25SScott Long 	struct ccb_trans_settings cts;
62852c9ce25SScott Long 	struct ccb_trans_settings_spi *spi;
62952c9ce25SScott Long 
63052c9ce25SScott Long 	memset(&cts, 0, sizeof (cts));
63183c5d981SAlexander Motin 	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
63252c9ce25SScott Long 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
63352c9ce25SScott Long 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
63452c9ce25SScott Long 	xpt_action((union ccb *)&cts);
63552c9ce25SScott Long 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
63652c9ce25SScott Long 		if (bootverbose) {
63752c9ce25SScott Long 			xpt_print(periph->path,
63852c9ce25SScott Long 			    "failed to get current device settings\n");
63952c9ce25SScott Long 		}
64052c9ce25SScott Long 		return (0);
64152c9ce25SScott Long 	}
64252c9ce25SScott Long 	if (cts.transport != XPORT_SPI) {
64352c9ce25SScott Long 		if (bootverbose) {
64452c9ce25SScott Long 			xpt_print(periph->path, "not SPI transport\n");
64552c9ce25SScott Long 		}
64652c9ce25SScott Long 		return (0);
64752c9ce25SScott Long 	}
64852c9ce25SScott Long 	spi = &cts.xport_specific.spi;
64952c9ce25SScott Long 
65052c9ce25SScott Long 	/*
65152c9ce25SScott Long 	 * We cannot renegotiate sync rate if we don't have one.
65252c9ce25SScott Long 	 */
65352c9ce25SScott Long 	if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
65452c9ce25SScott Long 		if (bootverbose) {
65552c9ce25SScott Long 			xpt_print(periph->path, "no sync rate known\n");
65652c9ce25SScott Long 		}
65752c9ce25SScott Long 		return (0);
65852c9ce25SScott Long 	}
65952c9ce25SScott Long 
66052c9ce25SScott Long 	/*
66152c9ce25SScott Long 	 * We'll assert that we don't have to touch PPR options- the
66252c9ce25SScott Long 	 * SIM will see what we do with period and offset and adjust
66352c9ce25SScott Long 	 * the PPR options as appropriate.
66452c9ce25SScott Long 	 */
66552c9ce25SScott Long 
66652c9ce25SScott Long 	/*
66752c9ce25SScott Long 	 * A sync rate with unknown or zero offset is nonsensical.
66852c9ce25SScott Long 	 * A sync period of zero means Async.
66952c9ce25SScott Long 	 */
67052c9ce25SScott Long 	if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0
67152c9ce25SScott Long 	 || spi->sync_offset == 0 || spi->sync_period == 0) {
67252c9ce25SScott Long 		if (bootverbose) {
67352c9ce25SScott Long 			xpt_print(periph->path, "no sync rate available\n");
67452c9ce25SScott Long 		}
67552c9ce25SScott Long 		return (0);
67652c9ce25SScott Long 	}
67752c9ce25SScott Long 
67852c9ce25SScott Long 	if (device->flags & CAM_DEV_DV_HIT_BOTTOM) {
67952c9ce25SScott Long 		CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
68052c9ce25SScott Long 		    ("hit async: giving up on DV\n"));
68152c9ce25SScott Long 		return (0);
68252c9ce25SScott Long 	}
68352c9ce25SScott Long 
68452c9ce25SScott Long 
68552c9ce25SScott Long 	/*
68652c9ce25SScott Long 	 * Jump sync_period up by one, but stop at 5MHz and fall back to Async.
68752c9ce25SScott Long 	 * We don't try to remember 'last' settings to see if the SIM actually
68852c9ce25SScott Long 	 * gets into the speed we want to set. We check on the SIM telling
68952c9ce25SScott Long 	 * us that a requested speed is bad, but otherwise don't try and
69052c9ce25SScott Long 	 * check the speed due to the asynchronous and handshake nature
69152c9ce25SScott Long 	 * of speed setting.
69252c9ce25SScott Long 	 */
69352c9ce25SScott Long 	spi->valid = CTS_SPI_VALID_SYNC_RATE | CTS_SPI_VALID_SYNC_OFFSET;
69452c9ce25SScott Long 	for (;;) {
69552c9ce25SScott Long 		spi->sync_period++;
69652c9ce25SScott Long 		if (spi->sync_period >= 0xf) {
69752c9ce25SScott Long 			spi->sync_period = 0;
69852c9ce25SScott Long 			spi->sync_offset = 0;
69952c9ce25SScott Long 			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
70052c9ce25SScott Long 			    ("setting to async for DV\n"));
70152c9ce25SScott Long 			/*
70252c9ce25SScott Long 			 * Once we hit async, we don't want to try
70352c9ce25SScott Long 			 * any more settings.
70452c9ce25SScott Long 			 */
70552c9ce25SScott Long 			device->flags |= CAM_DEV_DV_HIT_BOTTOM;
70652c9ce25SScott Long 		} else if (bootverbose) {
70752c9ce25SScott Long 			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
70852c9ce25SScott Long 			    ("DV: period 0x%x\n", spi->sync_period));
70952c9ce25SScott Long 			printf("setting period to 0x%x\n", spi->sync_period);
71052c9ce25SScott Long 		}
71152c9ce25SScott Long 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
71252c9ce25SScott Long 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
71352c9ce25SScott Long 		xpt_action((union ccb *)&cts);
71452c9ce25SScott Long 		if ((cts.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
71552c9ce25SScott Long 			break;
71652c9ce25SScott Long 		}
71752c9ce25SScott Long 		CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
71852c9ce25SScott Long 		    ("DV: failed to set period 0x%x\n", spi->sync_period));
71952c9ce25SScott Long 		if (spi->sync_period == 0) {
72052c9ce25SScott Long 			return (0);
72152c9ce25SScott Long 		}
72252c9ce25SScott Long 	}
72352c9ce25SScott Long 	return (1);
72452c9ce25SScott Long }
72552c9ce25SScott Long #endif
72652c9ce25SScott Long static void
72752c9ce25SScott Long probedone(struct cam_periph *periph, union ccb *done_ccb)
72852c9ce25SScott Long {
729c8039fc6SAlexander Motin 	struct ccb_trans_settings cts;
73052c9ce25SScott Long 	struct ata_params *ident_buf;
73152c9ce25SScott Long 	probe_softc *softc;
73252c9ce25SScott Long 	struct cam_path *path;
73352c9ce25SScott Long 	u_int32_t  priority;
734da6808c1SAlexander Motin 	u_int caps;
73565d0fb03SAlexander Motin 	int found = 1;
73652c9ce25SScott Long 
73752c9ce25SScott Long 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
73852c9ce25SScott Long 
73952c9ce25SScott Long 	softc = (probe_softc *)periph->softc;
74052c9ce25SScott Long 	path = done_ccb->ccb_h.path;
74152c9ce25SScott Long 	priority = done_ccb->ccb_h.pinfo.priority;
74252c9ce25SScott Long 	ident_buf = &path->device->ident_data;
74352c9ce25SScott Long 
7441e637ba6SAlexander Motin 	if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
745639c2d4fSAlexander Motin device_fail:	if ((!softc->restart) &&
746639c2d4fSAlexander Motin 		    cam_periph_error(done_ccb, 0, 0, NULL) == ERESTART) {
7471e637ba6SAlexander Motin 			return;
7481e637ba6SAlexander Motin 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
7491e637ba6SAlexander Motin 			/* Don't wedge the queue */
7501e637ba6SAlexander Motin 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
7511e637ba6SAlexander Motin 					 /*run_queue*/TRUE);
7521e637ba6SAlexander Motin 		}
7531e637ba6SAlexander Motin 		/* Old PIO2 devices may not support mode setting. */
7541e637ba6SAlexander Motin 		if (softc->action == PROBE_SETMODE &&
7551e637ba6SAlexander Motin 		    ata_max_pmode(ident_buf) <= ATA_PIO2 &&
7561e637ba6SAlexander Motin 		    (ident_buf->capabilities1 & ATA_SUPPORT_IORDY) == 0)
7571e637ba6SAlexander Motin 			goto noerror;
7581e637ba6SAlexander Motin 		/*
7591e637ba6SAlexander Motin 		 * If we get to this point, we got an error status back
7601e637ba6SAlexander Motin 		 * from the inquiry and the error status doesn't require
7611e637ba6SAlexander Motin 		 * automatically retrying the command.  Therefore, the
7621e637ba6SAlexander Motin 		 * inquiry failed.  If we had inquiry information before
7631e637ba6SAlexander Motin 		 * for this device, but this latest inquiry command failed,
7641e637ba6SAlexander Motin 		 * the device has probably gone away.  If this device isn't
7651e637ba6SAlexander Motin 		 * already marked unconfigured, notify the peripheral
7661e637ba6SAlexander Motin 		 * drivers that this device is no more.
7671e637ba6SAlexander Motin 		 */
7681e637ba6SAlexander Motin 		if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
7691e637ba6SAlexander Motin 			xpt_async(AC_LOST_DEVICE, path, NULL);
7701e637ba6SAlexander Motin 		found = 0;
7711e637ba6SAlexander Motin 		goto done;
7721e637ba6SAlexander Motin 	}
7731e637ba6SAlexander Motin noerror:
774a9b8edb1SAlexander Motin 	if (softc->restart)
775a9b8edb1SAlexander Motin 		goto done;
77652c9ce25SScott Long 	switch (softc->action) {
77752c9ce25SScott Long 	case PROBE_RESET:
7781e637ba6SAlexander Motin 	{
77952c9ce25SScott Long 		int sign = (done_ccb->ataio.res.lba_high << 8) +
78052c9ce25SScott Long 		    done_ccb->ataio.res.lba_mid;
7817bdb664eSAlexander Motin 		if (bootverbose)
78252c9ce25SScott Long 			xpt_print(path, "SIGNATURE: %04x\n", sign);
78352c9ce25SScott Long 		if (sign == 0x0000 &&
78452c9ce25SScott Long 		    done_ccb->ccb_h.target_id != 15) {
78552c9ce25SScott Long 			path->device->protocol = PROTO_ATA;
78652c9ce25SScott Long 			PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
78752c9ce25SScott Long 		} else if (sign == 0x9669 &&
78852c9ce25SScott Long 		    done_ccb->ccb_h.target_id == 15) {
78952c9ce25SScott Long 			/* Report SIM that PM is present. */
79052c9ce25SScott Long 			bzero(&cts, sizeof(cts));
79183c5d981SAlexander Motin 			xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
79252c9ce25SScott Long 			cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
79352c9ce25SScott Long 			cts.type = CTS_TYPE_CURRENT_SETTINGS;
79452c9ce25SScott Long 			cts.xport_specific.sata.pm_present = 1;
79552c9ce25SScott Long 			cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
79652c9ce25SScott Long 			xpt_action((union ccb *)&cts);
79752c9ce25SScott Long 			path->device->protocol = PROTO_SATAPM;
79852c9ce25SScott Long 			PROBE_SET_ACTION(softc, PROBE_PM_PID);
79952c9ce25SScott Long 		} else if (sign == 0xeb14 &&
80052c9ce25SScott Long 		    done_ccb->ccb_h.target_id != 15) {
80152c9ce25SScott Long 			path->device->protocol = PROTO_SCSI;
80252c9ce25SScott Long 			PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
80352c9ce25SScott Long 		} else {
80452c9ce25SScott Long 			if (done_ccb->ccb_h.target_id != 15) {
80552c9ce25SScott Long 				xpt_print(path,
80652c9ce25SScott Long 				    "Unexpected signature 0x%04x\n", sign);
80752c9ce25SScott Long 			}
80865d0fb03SAlexander Motin 			goto device_fail;
80952c9ce25SScott Long 		}
81052c9ce25SScott Long 		xpt_release_ccb(done_ccb);
81152c9ce25SScott Long 		xpt_schedule(periph, priority);
81252c9ce25SScott Long 		return;
81352c9ce25SScott Long 	}
81452c9ce25SScott Long 	case PROBE_IDENTIFY:
81552c9ce25SScott Long 	{
816699f853bSAlexander Motin 		struct ccb_pathinq cpi;
81752c9ce25SScott Long 		int16_t *ptr;
8182eb4a8dcSAlexander Motin 		int changed = 1;
81952c9ce25SScott Long 
820a9b8edb1SAlexander Motin 		ident_buf = &softc->ident_data;
82152c9ce25SScott Long 		for (ptr = (int16_t *)ident_buf;
82252c9ce25SScott Long 		     ptr < (int16_t *)ident_buf + sizeof(struct ata_params)/2; ptr++) {
82352c9ce25SScott Long 			*ptr = le16toh(*ptr);
82452c9ce25SScott Long 		}
82552c9ce25SScott Long 		if (strncmp(ident_buf->model, "FX", 2) &&
82652c9ce25SScott Long 		    strncmp(ident_buf->model, "NEC", 3) &&
82752c9ce25SScott Long 		    strncmp(ident_buf->model, "Pioneer", 7) &&
82852c9ce25SScott Long 		    strncmp(ident_buf->model, "SHARP", 5)) {
82952c9ce25SScott Long 			ata_bswap(ident_buf->model, sizeof(ident_buf->model));
83052c9ce25SScott Long 			ata_bswap(ident_buf->revision, sizeof(ident_buf->revision));
83152c9ce25SScott Long 			ata_bswap(ident_buf->serial, sizeof(ident_buf->serial));
83252c9ce25SScott Long 		}
83352c9ce25SScott Long 		ata_btrim(ident_buf->model, sizeof(ident_buf->model));
83452c9ce25SScott Long 		ata_bpack(ident_buf->model, ident_buf->model, sizeof(ident_buf->model));
83552c9ce25SScott Long 		ata_btrim(ident_buf->revision, sizeof(ident_buf->revision));
83652c9ce25SScott Long 		ata_bpack(ident_buf->revision, ident_buf->revision, sizeof(ident_buf->revision));
83752c9ce25SScott Long 		ata_btrim(ident_buf->serial, sizeof(ident_buf->serial));
83852c9ce25SScott Long 		ata_bpack(ident_buf->serial, ident_buf->serial, sizeof(ident_buf->serial));
8394ef08dc5SAlexander Motin 		/* Device may need spin-up before IDENTIFY become valid. */
8401254b680SAlexander Motin 		if ((ident_buf->specconf == 0x37c8 ||
8411254b680SAlexander Motin 		     ident_buf->specconf == 0x738c) &&
8421254b680SAlexander Motin 		    ((ident_buf->config & ATA_RESP_INCOMPLETE) ||
8434ef08dc5SAlexander Motin 		     softc->spinup == 0)) {
8444ef08dc5SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_SPINUP);
8454ef08dc5SAlexander Motin 			xpt_release_ccb(done_ccb);
8464ef08dc5SAlexander Motin 			xpt_schedule(periph, priority);
8474ef08dc5SAlexander Motin 			return;
8484ef08dc5SAlexander Motin 		}
849a9b8edb1SAlexander Motin 		ident_buf = &path->device->ident_data;
85052c9ce25SScott Long 		if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
85152c9ce25SScott Long 			/* Check that it is the same device. */
852a9b8edb1SAlexander Motin 			if (bcmp(softc->ident_data.model, ident_buf->model,
853a9b8edb1SAlexander Motin 			     sizeof(ident_buf->model)) ||
854a9b8edb1SAlexander Motin 			    bcmp(softc->ident_data.revision, ident_buf->revision,
855a9b8edb1SAlexander Motin 			     sizeof(ident_buf->revision)) ||
856a9b8edb1SAlexander Motin 			    bcmp(softc->ident_data.serial, ident_buf->serial,
857a9b8edb1SAlexander Motin 			     sizeof(ident_buf->serial))) {
85852c9ce25SScott Long 				/* Device changed. */
85952c9ce25SScott Long 				xpt_async(AC_LOST_DEVICE, path, NULL);
8601e637ba6SAlexander Motin 			} else {
861a9b8edb1SAlexander Motin 				bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
8622eb4a8dcSAlexander Motin 				changed = 0;
8632eb4a8dcSAlexander Motin 			}
8642eb4a8dcSAlexander Motin 		}
8652eb4a8dcSAlexander Motin 		if (changed) {
8662eb4a8dcSAlexander Motin 			bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
86752c9ce25SScott Long 			/* Clean up from previous instance of this device */
86852c9ce25SScott Long 			if (path->device->serial_num != NULL) {
86952c9ce25SScott Long 				free(path->device->serial_num, M_CAMXPT);
87052c9ce25SScott Long 				path->device->serial_num = NULL;
87152c9ce25SScott Long 				path->device->serial_num_len = 0;
87252c9ce25SScott Long 			}
87352c9ce25SScott Long 			path->device->serial_num =
87452c9ce25SScott Long 				(u_int8_t *)malloc((sizeof(ident_buf->serial) + 1),
87552c9ce25SScott Long 					   M_CAMXPT, M_NOWAIT);
87652c9ce25SScott Long 			if (path->device->serial_num != NULL) {
87752c9ce25SScott Long 				bcopy(ident_buf->serial,
87852c9ce25SScott Long 				      path->device->serial_num,
87952c9ce25SScott Long 				      sizeof(ident_buf->serial));
88052c9ce25SScott Long 				path->device->serial_num[sizeof(ident_buf->serial)]
88152c9ce25SScott Long 				    = '\0';
88252c9ce25SScott Long 				path->device->serial_num_len =
88352c9ce25SScott Long 				    strlen(path->device->serial_num);
88452c9ce25SScott Long 			}
88552c9ce25SScott Long 
8864b997c49SAlexander Motin 			path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
8871e637ba6SAlexander Motin 		}
88830a4094fSAlexander Motin 		if (ident_buf->satacapabilities & ATA_SUPPORT_NCQ) {
88930a4094fSAlexander Motin 			path->device->mintags = path->device->maxtags =
89030a4094fSAlexander Motin 			    ATA_QUEUE_LEN(ident_buf->queue) + 1;
89130a4094fSAlexander Motin 		}
89230a4094fSAlexander Motin 		ata_find_quirk(path->device);
893507e5811SAlexander Motin 		if (path->device->mintags != 0 &&
894507e5811SAlexander Motin 		    path->bus->sim->max_tagged_dev_openings != 0) {
895699f853bSAlexander Motin 			/* Check if the SIM does not want queued commands. */
896699f853bSAlexander Motin 			bzero(&cpi, sizeof(cpi));
897699f853bSAlexander Motin 			xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
898699f853bSAlexander Motin 			cpi.ccb_h.func_code = XPT_PATH_INQ;
899699f853bSAlexander Motin 			xpt_action((union ccb *)&cpi);
900699f853bSAlexander Motin 			if (cpi.ccb_h.status == CAM_REQ_CMP &&
901699f853bSAlexander Motin 			    (cpi.hba_inquiry & PI_TAG_ABLE)) {
902c8039fc6SAlexander Motin 				/* Report SIM which tags are allowed. */
903c8039fc6SAlexander Motin 				bzero(&cts, sizeof(cts));
90483c5d981SAlexander Motin 				xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
905c8039fc6SAlexander Motin 				cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
906c8039fc6SAlexander Motin 				cts.type = CTS_TYPE_CURRENT_SETTINGS;
907c8039fc6SAlexander Motin 				cts.xport_specific.sata.tags = path->device->maxtags;
908c8039fc6SAlexander Motin 				cts.xport_specific.sata.valid = CTS_SATA_VALID_TAGS;
909c8039fc6SAlexander Motin 				xpt_action((union ccb *)&cts);
910c8039fc6SAlexander Motin 				/* Reconfigure queues for tagged queueing. */
91130a4094fSAlexander Motin 				xpt_start_tags(path);
91230a4094fSAlexander Motin 			}
913699f853bSAlexander Motin 		}
91452c9ce25SScott Long 		ata_device_transport(path);
91552c9ce25SScott Long 		PROBE_SET_ACTION(softc, PROBE_SETMODE);
91652c9ce25SScott Long 		xpt_release_ccb(done_ccb);
91752c9ce25SScott Long 		xpt_schedule(periph, priority);
91852c9ce25SScott Long 		return;
91952c9ce25SScott Long 	}
9204ef08dc5SAlexander Motin 	case PROBE_SPINUP:
9214ef08dc5SAlexander Motin 		if (bootverbose)
9224ef08dc5SAlexander Motin 			xpt_print(path, "Spin-up done\n");
9234ef08dc5SAlexander Motin 		softc->spinup = 1;
9244ef08dc5SAlexander Motin 		PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
9254ef08dc5SAlexander Motin 		xpt_release_ccb(done_ccb);
9264ef08dc5SAlexander Motin 		xpt_schedule(periph, priority);
9274ef08dc5SAlexander Motin 		return;
92852c9ce25SScott Long 	case PROBE_SETMODE:
929da6808c1SAlexander Motin 		if (path->device->transport != XPORT_SATA)
930da6808c1SAlexander Motin 			goto notsata;
931da6808c1SAlexander Motin 		/* Set supported bits. */
932da6808c1SAlexander Motin 		bzero(&cts, sizeof(cts));
933da6808c1SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
934da6808c1SAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
935da6808c1SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
936da6808c1SAlexander Motin 		xpt_action((union ccb *)&cts);
937da6808c1SAlexander Motin 		if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
938da6808c1SAlexander Motin 			caps = cts.xport_specific.sata.caps & CTS_SATA_CAPS_H;
939da6808c1SAlexander Motin 		else
940da6808c1SAlexander Motin 			caps = 0;
941da6808c1SAlexander Motin 		if (ident_buf->satacapabilities != 0xffff) {
942da6808c1SAlexander Motin 			if (ident_buf->satacapabilities & ATA_SUPPORT_IFPWRMNGTRCV)
943da6808c1SAlexander Motin 				caps |= CTS_SATA_CAPS_D_PMREQ;
944da6808c1SAlexander Motin 			if (ident_buf->satacapabilities & ATA_SUPPORT_HAPST)
945da6808c1SAlexander Motin 				caps |= CTS_SATA_CAPS_D_APST;
946da6808c1SAlexander Motin 		}
947da6808c1SAlexander Motin 		/* Mask unwanted bits. */
948da6808c1SAlexander Motin 		bzero(&cts, sizeof(cts));
949da6808c1SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
950da6808c1SAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
951da6808c1SAlexander Motin 		cts.type = CTS_TYPE_USER_SETTINGS;
952da6808c1SAlexander Motin 		xpt_action((union ccb *)&cts);
953da6808c1SAlexander Motin 		if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
954da6808c1SAlexander Motin 			caps &= cts.xport_specific.sata.caps;
955da6808c1SAlexander Motin 		/* Store result to SIM. */
956da6808c1SAlexander Motin 		bzero(&cts, sizeof(cts));
957da6808c1SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
958da6808c1SAlexander Motin 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
959da6808c1SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
960da6808c1SAlexander Motin 		cts.xport_specific.sata.caps = caps;
961da6808c1SAlexander Motin 		cts.xport_specific.sata.valid = CTS_SATA_VALID_CAPS;
962da6808c1SAlexander Motin 		xpt_action((union ccb *)&cts);
963da6808c1SAlexander Motin 		softc->caps = caps;
964da6808c1SAlexander Motin 		if (ident_buf->satasupport & ATA_SUPPORT_IFPWRMNGT) {
965da6808c1SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_SETPM);
966da6808c1SAlexander Motin 			xpt_release_ccb(done_ccb);
967da6808c1SAlexander Motin 			xpt_schedule(periph, priority);
968da6808c1SAlexander Motin 			return;
969da6808c1SAlexander Motin 		}
970da6808c1SAlexander Motin 		/* FALLTHROUGH */
971da6808c1SAlexander Motin 	case PROBE_SETPM:
972da6808c1SAlexander Motin 		if (ident_buf->satacapabilities != 0xffff &&
973da6808c1SAlexander Motin 		    ident_buf->satacapabilities & ATA_SUPPORT_DAPST) {
974da6808c1SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_SETAPST);
975da6808c1SAlexander Motin 			xpt_release_ccb(done_ccb);
976da6808c1SAlexander Motin 			xpt_schedule(periph, priority);
977da6808c1SAlexander Motin 			return;
978da6808c1SAlexander Motin 		}
979da6808c1SAlexander Motin 		/* FALLTHROUGH */
980da6808c1SAlexander Motin 	case PROBE_SETAPST:
981da6808c1SAlexander Motin 		if (ident_buf->satasupport & ATA_SUPPORT_AUTOACTIVATE) {
982da6808c1SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_SETDMAAA);
983da6808c1SAlexander Motin 			xpt_release_ccb(done_ccb);
984da6808c1SAlexander Motin 			xpt_schedule(periph, priority);
985da6808c1SAlexander Motin 			return;
986da6808c1SAlexander Motin 		}
987da6808c1SAlexander Motin 		/* FALLTHROUGH */
988da6808c1SAlexander Motin 	case PROBE_SETDMAAA:
989da6808c1SAlexander Motin notsata:
9901e637ba6SAlexander Motin 		if (path->device->protocol == PROTO_ATA) {
9911e637ba6SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_SET_MULTI);
9921e637ba6SAlexander Motin 		} else {
9931e637ba6SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_INQUIRY);
9941e637ba6SAlexander Motin 		}
9951e637ba6SAlexander Motin 		xpt_release_ccb(done_ccb);
9961e637ba6SAlexander Motin 		xpt_schedule(periph, priority);
9971e637ba6SAlexander Motin 		return;
9981e637ba6SAlexander Motin 	case PROBE_SET_MULTI:
9991e637ba6SAlexander Motin 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
100052c9ce25SScott Long 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1001f98d7a47SAlexander Motin 			xpt_acquire_device(path->device);
100252c9ce25SScott Long 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
100352c9ce25SScott Long 			xpt_action(done_ccb);
100452c9ce25SScott Long 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
100552c9ce25SScott Long 			    done_ccb);
10061e637ba6SAlexander Motin 		}
100752c9ce25SScott Long 		break;
100852c9ce25SScott Long 	case PROBE_INQUIRY:
100952c9ce25SScott Long 	case PROBE_FULL_INQUIRY:
101052c9ce25SScott Long 	{
101152c9ce25SScott Long 		struct scsi_inquiry_data *inq_buf;
10121e637ba6SAlexander Motin 		u_int8_t periph_qual, len;
101352c9ce25SScott Long 
101452c9ce25SScott Long 		path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
101552c9ce25SScott Long 		inq_buf = &path->device->inq_data;
101652c9ce25SScott Long 
101752c9ce25SScott Long 		periph_qual = SID_QUAL(inq_buf);
101852c9ce25SScott Long 
10191e637ba6SAlexander Motin 		if (periph_qual != SID_QUAL_LU_CONNECTED)
10201e637ba6SAlexander Motin 			break;
102152c9ce25SScott Long 
102252c9ce25SScott Long 		/*
102352c9ce25SScott Long 		 * We conservatively request only
102452c9ce25SScott Long 		 * SHORT_INQUIRY_LEN bytes of inquiry
102552c9ce25SScott Long 		 * information during our first try
102652c9ce25SScott Long 		 * at sending an INQUIRY. If the device
102752c9ce25SScott Long 		 * has more information to give,
102852c9ce25SScott Long 		 * perform a second request specifying
102952c9ce25SScott Long 		 * the amount of information the device
103052c9ce25SScott Long 		 * is willing to give.
103152c9ce25SScott Long 		 */
103252c9ce25SScott Long 		len = inq_buf->additional_length
10331e637ba6SAlexander Motin 		    + offsetof(struct scsi_inquiry_data, additional_length) + 1;
103452c9ce25SScott Long 		if (softc->action == PROBE_INQUIRY
103552c9ce25SScott Long 		    && len > SHORT_INQUIRY_LENGTH) {
103652c9ce25SScott Long 			PROBE_SET_ACTION(softc, PROBE_FULL_INQUIRY);
103752c9ce25SScott Long 			xpt_release_ccb(done_ccb);
103852c9ce25SScott Long 			xpt_schedule(periph, priority);
103952c9ce25SScott Long 			return;
104052c9ce25SScott Long 		}
104152c9ce25SScott Long 
10424b997c49SAlexander Motin 		ata_device_transport(path);
10431e637ba6SAlexander Motin 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
104452c9ce25SScott Long 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1045f98d7a47SAlexander Motin 			xpt_acquire_device(path->device);
104652c9ce25SScott Long 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
104752c9ce25SScott Long 			xpt_action(done_ccb);
10481e637ba6SAlexander Motin 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path, done_ccb);
10491e637ba6SAlexander Motin 		}
105052c9ce25SScott Long 		break;
105152c9ce25SScott Long 	}
105252c9ce25SScott Long 	case PROBE_PM_PID:
10534b997c49SAlexander Motin 		if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) == 0)
105452c9ce25SScott Long 			bzero(ident_buf, sizeof(*ident_buf));
105552c9ce25SScott Long 		softc->pm_pid = (done_ccb->ataio.res.lba_high << 24) +
105652c9ce25SScott Long 		    (done_ccb->ataio.res.lba_mid << 16) +
105752c9ce25SScott Long 		    (done_ccb->ataio.res.lba_low << 8) +
105852c9ce25SScott Long 		    done_ccb->ataio.res.sector_count;
105965d0fb03SAlexander Motin 		((uint32_t *)ident_buf)[0] = softc->pm_pid;
106052c9ce25SScott Long 		snprintf(ident_buf->model, sizeof(ident_buf->model),
106152c9ce25SScott Long 		    "Port Multiplier %08x", softc->pm_pid);
106252c9ce25SScott Long 		PROBE_SET_ACTION(softc, PROBE_PM_PRV);
106352c9ce25SScott Long 		xpt_release_ccb(done_ccb);
106452c9ce25SScott Long 		xpt_schedule(periph, priority);
106552c9ce25SScott Long 		return;
106652c9ce25SScott Long 	case PROBE_PM_PRV:
106752c9ce25SScott Long 		softc->pm_prv = (done_ccb->ataio.res.lba_high << 24) +
106852c9ce25SScott Long 		    (done_ccb->ataio.res.lba_mid << 16) +
106952c9ce25SScott Long 		    (done_ccb->ataio.res.lba_low << 8) +
107052c9ce25SScott Long 		    done_ccb->ataio.res.sector_count;
107165d0fb03SAlexander Motin 		((uint32_t *)ident_buf)[1] = softc->pm_prv;
107252c9ce25SScott Long 		snprintf(ident_buf->revision, sizeof(ident_buf->revision),
107352c9ce25SScott Long 		    "%04x", softc->pm_prv);
10744b997c49SAlexander Motin 		path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
1075da6808c1SAlexander Motin 		/* Set supported bits. */
1076da6808c1SAlexander Motin 		bzero(&cts, sizeof(cts));
1077da6808c1SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1078da6808c1SAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1079da6808c1SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
1080da6808c1SAlexander Motin 		xpt_action((union ccb *)&cts);
1081da6808c1SAlexander Motin 		if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1082da6808c1SAlexander Motin 			caps = cts.xport_specific.sata.caps & CTS_SATA_CAPS_H;
1083da6808c1SAlexander Motin 		else
1084da6808c1SAlexander Motin 			caps = 0;
1085da6808c1SAlexander Motin 		/* All PMPs must support PM requests. */
1086da6808c1SAlexander Motin 		caps |= CTS_SATA_CAPS_D_PMREQ;
1087da6808c1SAlexander Motin 		/* Mask unwanted bits. */
1088da6808c1SAlexander Motin 		bzero(&cts, sizeof(cts));
1089da6808c1SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1090da6808c1SAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1091da6808c1SAlexander Motin 		cts.type = CTS_TYPE_USER_SETTINGS;
1092da6808c1SAlexander Motin 		xpt_action((union ccb *)&cts);
1093da6808c1SAlexander Motin 		if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1094da6808c1SAlexander Motin 			caps &= cts.xport_specific.sata.caps;
1095da6808c1SAlexander Motin 		/* Store result to SIM. */
1096da6808c1SAlexander Motin 		bzero(&cts, sizeof(cts));
1097da6808c1SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1098da6808c1SAlexander Motin 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1099da6808c1SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
1100da6808c1SAlexander Motin 		cts.xport_specific.sata.caps = caps;
1101da6808c1SAlexander Motin 		cts.xport_specific.sata.valid = CTS_SATA_VALID_CAPS;
1102da6808c1SAlexander Motin 		xpt_action((union ccb *)&cts);
1103da6808c1SAlexander Motin 		softc->caps = caps;
110465d0fb03SAlexander Motin 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
110552c9ce25SScott Long 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1106f98d7a47SAlexander Motin 			xpt_acquire_device(path->device);
110752c9ce25SScott Long 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
110852c9ce25SScott Long 			xpt_action(done_ccb);
110952c9ce25SScott Long 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
111052c9ce25SScott Long 			    done_ccb);
111165d0fb03SAlexander Motin 		} else {
111265d0fb03SAlexander Motin 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
111365d0fb03SAlexander Motin 			xpt_action(done_ccb);
11141e637ba6SAlexander Motin 			xpt_async(AC_SCSI_AEN, done_ccb->ccb_h.path, done_ccb);
111552c9ce25SScott Long 		}
111652c9ce25SScott Long 		break;
111752c9ce25SScott Long 	case PROBE_INVALID:
111852c9ce25SScott Long 		CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_INFO,
111952c9ce25SScott Long 		    ("probedone: invalid action state\n"));
112052c9ce25SScott Long 	default:
112152c9ce25SScott Long 		break;
112252c9ce25SScott Long 	}
11231e637ba6SAlexander Motin done:
112483c5d981SAlexander Motin 	if (softc->restart) {
112583c5d981SAlexander Motin 		softc->restart = 0;
11261e637ba6SAlexander Motin 		xpt_release_ccb(done_ccb);
112783c5d981SAlexander Motin 		probeschedule(periph);
112883c5d981SAlexander Motin 		return;
112983c5d981SAlexander Motin 	}
113083c5d981SAlexander Motin 	xpt_release_ccb(done_ccb);
113183c5d981SAlexander Motin 	while ((done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs))) {
113283c5d981SAlexander Motin 		TAILQ_REMOVE(&softc->request_ccbs,
113383c5d981SAlexander Motin 		    &done_ccb->ccb_h, periph_links.tqe);
113483c5d981SAlexander Motin 		done_ccb->ccb_h.status = found ? CAM_REQ_CMP : CAM_REQ_CMP_ERR;
113552c9ce25SScott Long 		xpt_done(done_ccb);
113683c5d981SAlexander Motin 	}
113783c5d981SAlexander Motin 	cam_release_devq(periph->path,
113883c5d981SAlexander Motin 	    RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_XPT + 1, FALSE);
113952c9ce25SScott Long 	cam_periph_invalidate(periph);
114052c9ce25SScott Long 	cam_periph_release_locked(periph);
114152c9ce25SScott Long }
114252c9ce25SScott Long 
114352c9ce25SScott Long static void
114452c9ce25SScott Long probecleanup(struct cam_periph *periph)
114552c9ce25SScott Long {
114652c9ce25SScott Long 	free(periph->softc, M_CAMXPT);
114752c9ce25SScott Long }
114852c9ce25SScott Long 
114952c9ce25SScott Long static void
115030a4094fSAlexander Motin ata_find_quirk(struct cam_ed *device)
115152c9ce25SScott Long {
115230a4094fSAlexander Motin 	struct ata_quirk_entry *quirk;
115352c9ce25SScott Long 	caddr_t	match;
115452c9ce25SScott Long 
115530a4094fSAlexander Motin 	match = cam_quirkmatch((caddr_t)&device->ident_data,
115630a4094fSAlexander Motin 			       (caddr_t)ata_quirk_table,
115730a4094fSAlexander Motin 			       ata_quirk_table_size,
115830a4094fSAlexander Motin 			       sizeof(*ata_quirk_table), ata_identify_match);
115952c9ce25SScott Long 
116052c9ce25SScott Long 	if (match == NULL)
116152c9ce25SScott Long 		panic("xpt_find_quirk: device didn't match wildcard entry!!");
116252c9ce25SScott Long 
116330a4094fSAlexander Motin 	quirk = (struct ata_quirk_entry *)match;
116452c9ce25SScott Long 	device->quirk = quirk;
116530a4094fSAlexander Motin 	if (quirk->quirks & CAM_QUIRK_MAXTAGS)
116630a4094fSAlexander Motin 		device->mintags = device->maxtags = quirk->maxtags;
116752c9ce25SScott Long }
116852c9ce25SScott Long 
116952c9ce25SScott Long typedef struct {
117052c9ce25SScott Long 	union	ccb *request_ccb;
117152c9ce25SScott Long 	struct 	ccb_pathinq *cpi;
117252c9ce25SScott Long 	int	counter;
117352c9ce25SScott Long } ata_scan_bus_info;
117452c9ce25SScott Long 
117552c9ce25SScott Long /*
117652c9ce25SScott Long  * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
117752c9ce25SScott Long  * As the scan progresses, xpt_scan_bus is used as the
117852c9ce25SScott Long  * callback on completion function.
117952c9ce25SScott Long  */
118052c9ce25SScott Long static void
118152c9ce25SScott Long ata_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
118252c9ce25SScott Long {
118352c9ce25SScott Long 	struct	cam_path *path;
118452c9ce25SScott Long 	ata_scan_bus_info *scan_info;
118583c5d981SAlexander Motin 	union	ccb *work_ccb, *reset_ccb;
118652c9ce25SScott Long 	cam_status status;
118752c9ce25SScott Long 
118852c9ce25SScott Long 	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
118952c9ce25SScott Long 		  ("xpt_scan_bus\n"));
119052c9ce25SScott Long 	switch (request_ccb->ccb_h.func_code) {
119152c9ce25SScott Long 	case XPT_SCAN_BUS:
119252c9ce25SScott Long 		/* Find out the characteristics of the bus */
119352c9ce25SScott Long 		work_ccb = xpt_alloc_ccb_nowait();
119452c9ce25SScott Long 		if (work_ccb == NULL) {
119552c9ce25SScott Long 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
119652c9ce25SScott Long 			xpt_done(request_ccb);
119752c9ce25SScott Long 			return;
119852c9ce25SScott Long 		}
119952c9ce25SScott Long 		xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
120052c9ce25SScott Long 			      request_ccb->ccb_h.pinfo.priority);
120152c9ce25SScott Long 		work_ccb->ccb_h.func_code = XPT_PATH_INQ;
120252c9ce25SScott Long 		xpt_action(work_ccb);
120352c9ce25SScott Long 		if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
120452c9ce25SScott Long 			request_ccb->ccb_h.status = work_ccb->ccb_h.status;
120552c9ce25SScott Long 			xpt_free_ccb(work_ccb);
120652c9ce25SScott Long 			xpt_done(request_ccb);
120752c9ce25SScott Long 			return;
120852c9ce25SScott Long 		}
120952c9ce25SScott Long 
121083c5d981SAlexander Motin 		/* We may need to reset bus first, if we haven't done it yet. */
121183c5d981SAlexander Motin 		if ((work_ccb->cpi.hba_inquiry &
121283c5d981SAlexander Motin 		    (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) &&
121383c5d981SAlexander Motin 		    !(work_ccb->cpi.hba_misc & PIM_NOBUSRESET) &&
121483c5d981SAlexander Motin 		    !timevalisset(&request_ccb->ccb_h.path->bus->last_reset)) {
121583c5d981SAlexander Motin 			reset_ccb = xpt_alloc_ccb_nowait();
121683c5d981SAlexander Motin 			xpt_setup_ccb(&reset_ccb->ccb_h, request_ccb->ccb_h.path,
121783c5d981SAlexander Motin 			      CAM_PRIORITY_NONE);
121883c5d981SAlexander Motin 			reset_ccb->ccb_h.func_code = XPT_RESET_BUS;
121983c5d981SAlexander Motin 			xpt_action(reset_ccb);
122083c5d981SAlexander Motin 			if (reset_ccb->ccb_h.status != CAM_REQ_CMP) {
122183c5d981SAlexander Motin 				request_ccb->ccb_h.status = reset_ccb->ccb_h.status;
122283c5d981SAlexander Motin 				xpt_free_ccb(reset_ccb);
122383c5d981SAlexander Motin 				xpt_free_ccb(work_ccb);
122483c5d981SAlexander Motin 				xpt_done(request_ccb);
122583c5d981SAlexander Motin 				return;
122683c5d981SAlexander Motin 			}
122783c5d981SAlexander Motin 			xpt_free_ccb(reset_ccb);
122883c5d981SAlexander Motin 		}
122983c5d981SAlexander Motin 
123052c9ce25SScott Long 		/* Save some state for use while we probe for devices */
123152c9ce25SScott Long 		scan_info = (ata_scan_bus_info *)
123252c9ce25SScott Long 		    malloc(sizeof(ata_scan_bus_info), M_CAMXPT, M_NOWAIT);
123352c9ce25SScott Long 		if (scan_info == NULL) {
123452c9ce25SScott Long 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
123552c9ce25SScott Long 			xpt_done(request_ccb);
123652c9ce25SScott Long 			return;
123752c9ce25SScott Long 		}
123852c9ce25SScott Long 		scan_info->request_ccb = request_ccb;
123952c9ce25SScott Long 		scan_info->cpi = &work_ccb->cpi;
124052c9ce25SScott Long 		/* If PM supported, probe it first. */
124152c9ce25SScott Long 		if (scan_info->cpi->hba_inquiry & PI_SATAPM)
12420025eb12SAlexander Motin 			scan_info->counter = scan_info->cpi->max_target;
12430025eb12SAlexander Motin 		else
12440025eb12SAlexander Motin 			scan_info->counter = 0;
124552c9ce25SScott Long 
124652c9ce25SScott Long 		work_ccb = xpt_alloc_ccb_nowait();
124752c9ce25SScott Long 		if (work_ccb == NULL) {
124852c9ce25SScott Long 			free(scan_info, M_CAMXPT);
124952c9ce25SScott Long 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
125052c9ce25SScott Long 			xpt_done(request_ccb);
125152c9ce25SScott Long 			break;
125252c9ce25SScott Long 		}
125352c9ce25SScott Long 		goto scan_next;
125452c9ce25SScott Long 	case XPT_SCAN_LUN:
125552c9ce25SScott Long 		work_ccb = request_ccb;
125652c9ce25SScott Long 		/* Reuse the same CCB to query if a device was really found */
125752c9ce25SScott Long 		scan_info = (ata_scan_bus_info *)work_ccb->ccb_h.ppriv_ptr0;
125865d0fb03SAlexander Motin 		/* If there is PMP... */
12590025eb12SAlexander Motin 		if ((scan_info->cpi->hba_inquiry & PI_SATAPM) &&
12600025eb12SAlexander Motin 		    (scan_info->counter == scan_info->cpi->max_target)) {
126183c5d981SAlexander Motin 			if (work_ccb->ccb_h.status == CAM_REQ_CMP) {
126265d0fb03SAlexander Motin 				/* everything else will be probed by it */
1263e2a75189SAlexander Motin 				/* Free the current request path- we're done with it. */
1264e2a75189SAlexander Motin 				xpt_free_path(work_ccb->ccb_h.path);
12650025eb12SAlexander Motin 				goto done;
126652c9ce25SScott Long 			} else {
126752c9ce25SScott Long 				struct ccb_trans_settings cts;
126852c9ce25SScott Long 
126952c9ce25SScott Long 				/* Report SIM that PM is absent. */
127052c9ce25SScott Long 				bzero(&cts, sizeof(cts));
127152c9ce25SScott Long 				xpt_setup_ccb(&cts.ccb_h,
1272e2a75189SAlexander Motin 				    work_ccb->ccb_h.path, CAM_PRIORITY_NONE);
127352c9ce25SScott Long 				cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
127452c9ce25SScott Long 				cts.type = CTS_TYPE_CURRENT_SETTINGS;
12753ccda2f3SAlexander Motin 				cts.xport_specific.sata.pm_present = 0;
127652c9ce25SScott Long 				cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
127752c9ce25SScott Long 				xpt_action((union ccb *)&cts);
127852c9ce25SScott Long 			}
127952c9ce25SScott Long 		}
1280e2a75189SAlexander Motin 		/* Free the current request path- we're done with it. */
1281e2a75189SAlexander Motin 		xpt_free_path(work_ccb->ccb_h.path);
12820025eb12SAlexander Motin 		if (scan_info->counter ==
12830025eb12SAlexander Motin 		    ((scan_info->cpi->hba_inquiry & PI_SATAPM) ?
12840025eb12SAlexander Motin 		    0 : scan_info->cpi->max_target)) {
12850025eb12SAlexander Motin done:
128652c9ce25SScott Long 			xpt_free_ccb(work_ccb);
128752c9ce25SScott Long 			xpt_free_ccb((union ccb *)scan_info->cpi);
128852c9ce25SScott Long 			request_ccb = scan_info->request_ccb;
128952c9ce25SScott Long 			free(scan_info, M_CAMXPT);
129052c9ce25SScott Long 			request_ccb->ccb_h.status = CAM_REQ_CMP;
129152c9ce25SScott Long 			xpt_done(request_ccb);
129252c9ce25SScott Long 			break;
129352c9ce25SScott Long 		}
12940025eb12SAlexander Motin 		/* Take next device. Wrap from max (PMP) to 0. */
12950025eb12SAlexander Motin 		scan_info->counter = (scan_info->counter + 1 ) %
12960025eb12SAlexander Motin 		    (scan_info->cpi->max_target + 1);
129752c9ce25SScott Long scan_next:
129852c9ce25SScott Long 		status = xpt_create_path(&path, xpt_periph,
129952c9ce25SScott Long 		    scan_info->request_ccb->ccb_h.path_id,
130052c9ce25SScott Long 		    scan_info->counter, 0);
130152c9ce25SScott Long 		if (status != CAM_REQ_CMP) {
130252c9ce25SScott Long 			printf("xpt_scan_bus: xpt_create_path failed"
130352c9ce25SScott Long 			    " with status %#x, bus scan halted\n",
130452c9ce25SScott Long 			    status);
130552c9ce25SScott Long 			xpt_free_ccb(work_ccb);
130652c9ce25SScott Long 			xpt_free_ccb((union ccb *)scan_info->cpi);
130752c9ce25SScott Long 			request_ccb = scan_info->request_ccb;
130852c9ce25SScott Long 			free(scan_info, M_CAMXPT);
130952c9ce25SScott Long 			request_ccb->ccb_h.status = status;
131052c9ce25SScott Long 			xpt_done(request_ccb);
131152c9ce25SScott Long 			break;
131252c9ce25SScott Long 		}
131352c9ce25SScott Long 		xpt_setup_ccb(&work_ccb->ccb_h, path,
131452c9ce25SScott Long 		    scan_info->request_ccb->ccb_h.pinfo.priority);
131552c9ce25SScott Long 		work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
131652c9ce25SScott Long 		work_ccb->ccb_h.cbfcnp = ata_scan_bus;
131752c9ce25SScott Long 		work_ccb->ccb_h.ppriv_ptr0 = scan_info;
131852c9ce25SScott Long 		work_ccb->crcn.flags = scan_info->request_ccb->crcn.flags;
131952c9ce25SScott Long 		xpt_action(work_ccb);
132052c9ce25SScott Long 		break;
132152c9ce25SScott Long 	default:
132252c9ce25SScott Long 		break;
132352c9ce25SScott Long 	}
132452c9ce25SScott Long }
132552c9ce25SScott Long 
132652c9ce25SScott Long static void
132752c9ce25SScott Long ata_scan_lun(struct cam_periph *periph, struct cam_path *path,
132852c9ce25SScott Long 	     cam_flags flags, union ccb *request_ccb)
132952c9ce25SScott Long {
133052c9ce25SScott Long 	struct ccb_pathinq cpi;
133152c9ce25SScott Long 	cam_status status;
133252c9ce25SScott Long 	struct cam_path *new_path;
133352c9ce25SScott Long 	struct cam_periph *old_periph;
133452c9ce25SScott Long 
133583c5d981SAlexander Motin 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_scan_lun\n"));
133652c9ce25SScott Long 
133783c5d981SAlexander Motin 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
133852c9ce25SScott Long 	cpi.ccb_h.func_code = XPT_PATH_INQ;
133952c9ce25SScott Long 	xpt_action((union ccb *)&cpi);
134052c9ce25SScott Long 
134152c9ce25SScott Long 	if (cpi.ccb_h.status != CAM_REQ_CMP) {
134252c9ce25SScott Long 		if (request_ccb != NULL) {
134352c9ce25SScott Long 			request_ccb->ccb_h.status = cpi.ccb_h.status;
134452c9ce25SScott Long 			xpt_done(request_ccb);
134552c9ce25SScott Long 		}
134652c9ce25SScott Long 		return;
134752c9ce25SScott Long 	}
134852c9ce25SScott Long 
134952c9ce25SScott Long 	if (request_ccb == NULL) {
135052c9ce25SScott Long 		request_ccb = malloc(sizeof(union ccb), M_CAMXPT, M_NOWAIT);
135152c9ce25SScott Long 		if (request_ccb == NULL) {
135252c9ce25SScott Long 			xpt_print(path, "xpt_scan_lun: can't allocate CCB, "
135352c9ce25SScott Long 			    "can't continue\n");
135452c9ce25SScott Long 			return;
135552c9ce25SScott Long 		}
135652c9ce25SScott Long 		new_path = malloc(sizeof(*new_path), M_CAMXPT, M_NOWAIT);
135752c9ce25SScott Long 		if (new_path == NULL) {
135852c9ce25SScott Long 			xpt_print(path, "xpt_scan_lun: can't allocate path, "
135952c9ce25SScott Long 			    "can't continue\n");
136052c9ce25SScott Long 			free(request_ccb, M_CAMXPT);
136152c9ce25SScott Long 			return;
136252c9ce25SScott Long 		}
136352c9ce25SScott Long 		status = xpt_compile_path(new_path, xpt_periph,
136452c9ce25SScott Long 					  path->bus->path_id,
136552c9ce25SScott Long 					  path->target->target_id,
136652c9ce25SScott Long 					  path->device->lun_id);
136752c9ce25SScott Long 
136852c9ce25SScott Long 		if (status != CAM_REQ_CMP) {
136952c9ce25SScott Long 			xpt_print(path, "xpt_scan_lun: can't compile path, "
137052c9ce25SScott Long 			    "can't continue\n");
137152c9ce25SScott Long 			free(request_ccb, M_CAMXPT);
137252c9ce25SScott Long 			free(new_path, M_CAMXPT);
137352c9ce25SScott Long 			return;
137452c9ce25SScott Long 		}
137583c5d981SAlexander Motin 		xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_XPT);
137652c9ce25SScott Long 		request_ccb->ccb_h.cbfcnp = xptscandone;
137752c9ce25SScott Long 		request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
137852c9ce25SScott Long 		request_ccb->crcn.flags = flags;
137952c9ce25SScott Long 	}
138052c9ce25SScott Long 
1381f09f8e3eSAlexander Motin 	if ((old_periph = cam_periph_find(path, "aprobe")) != NULL) {
138252c9ce25SScott Long 		probe_softc *softc;
138352c9ce25SScott Long 
138452c9ce25SScott Long 		softc = (probe_softc *)old_periph->softc;
138552c9ce25SScott Long 		TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
138652c9ce25SScott Long 				  periph_links.tqe);
138783c5d981SAlexander Motin 		softc->restart = 1;
138852c9ce25SScott Long 	} else {
138952c9ce25SScott Long 		status = cam_periph_alloc(proberegister, NULL, probecleanup,
1390f09f8e3eSAlexander Motin 					  probestart, "aprobe",
139152c9ce25SScott Long 					  CAM_PERIPH_BIO,
139252c9ce25SScott Long 					  request_ccb->ccb_h.path, NULL, 0,
139352c9ce25SScott Long 					  request_ccb);
139452c9ce25SScott Long 
139552c9ce25SScott Long 		if (status != CAM_REQ_CMP) {
139652c9ce25SScott Long 			xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
139752c9ce25SScott Long 			    "returned an error, can't continue probe\n");
139852c9ce25SScott Long 			request_ccb->ccb_h.status = status;
139952c9ce25SScott Long 			xpt_done(request_ccb);
140052c9ce25SScott Long 		}
140152c9ce25SScott Long 	}
140252c9ce25SScott Long }
140352c9ce25SScott Long 
140452c9ce25SScott Long static void
140552c9ce25SScott Long xptscandone(struct cam_periph *periph, union ccb *done_ccb)
140652c9ce25SScott Long {
140752c9ce25SScott Long 	xpt_release_path(done_ccb->ccb_h.path);
140852c9ce25SScott Long 	free(done_ccb->ccb_h.path, M_CAMXPT);
140952c9ce25SScott Long 	free(done_ccb, M_CAMXPT);
141052c9ce25SScott Long }
141152c9ce25SScott Long 
141252c9ce25SScott Long static struct cam_ed *
141352c9ce25SScott Long ata_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
141452c9ce25SScott Long {
141552c9ce25SScott Long 	struct cam_path path;
141630a4094fSAlexander Motin 	struct ata_quirk_entry *quirk;
141752c9ce25SScott Long 	struct cam_ed *device;
141852c9ce25SScott Long 	struct cam_ed *cur_device;
141952c9ce25SScott Long 
142052c9ce25SScott Long 	device = xpt_alloc_device(bus, target, lun_id);
142152c9ce25SScott Long 	if (device == NULL)
142252c9ce25SScott Long 		return (NULL);
142352c9ce25SScott Long 
142452c9ce25SScott Long 	/*
142552c9ce25SScott Long 	 * Take the default quirk entry until we have inquiry
142652c9ce25SScott Long 	 * data and can determine a better quirk to use.
142752c9ce25SScott Long 	 */
142830a4094fSAlexander Motin 	quirk = &ata_quirk_table[ata_quirk_table_size - 1];
142952c9ce25SScott Long 	device->quirk = (void *)quirk;
143030a4094fSAlexander Motin 	device->mintags = 0;
143130a4094fSAlexander Motin 	device->maxtags = 0;
143252c9ce25SScott Long 	bzero(&device->inq_data, sizeof(device->inq_data));
143352c9ce25SScott Long 	device->inq_flags = 0;
143452c9ce25SScott Long 	device->queue_flags = 0;
143552c9ce25SScott Long 	device->serial_num = NULL;
143652c9ce25SScott Long 	device->serial_num_len = 0;
143752c9ce25SScott Long 
143852c9ce25SScott Long 	/*
143952c9ce25SScott Long 	 * XXX should be limited by number of CCBs this bus can
144052c9ce25SScott Long 	 * do.
144152c9ce25SScott Long 	 */
144252c9ce25SScott Long 	bus->sim->max_ccbs += device->ccbq.devq_openings;
144352c9ce25SScott Long 	/* Insertion sort into our target's device list */
144452c9ce25SScott Long 	cur_device = TAILQ_FIRST(&target->ed_entries);
144552c9ce25SScott Long 	while (cur_device != NULL && cur_device->lun_id < lun_id)
144652c9ce25SScott Long 		cur_device = TAILQ_NEXT(cur_device, links);
144752c9ce25SScott Long 	if (cur_device != NULL) {
144852c9ce25SScott Long 		TAILQ_INSERT_BEFORE(cur_device, device, links);
144952c9ce25SScott Long 	} else {
145052c9ce25SScott Long 		TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
145152c9ce25SScott Long 	}
145252c9ce25SScott Long 	target->generation++;
145352c9ce25SScott Long 	if (lun_id != CAM_LUN_WILDCARD) {
145452c9ce25SScott Long 		xpt_compile_path(&path,
145552c9ce25SScott Long 				 NULL,
145652c9ce25SScott Long 				 bus->path_id,
145752c9ce25SScott Long 				 target->target_id,
145852c9ce25SScott Long 				 lun_id);
145952c9ce25SScott Long 		ata_device_transport(&path);
146052c9ce25SScott Long 		xpt_release_path(&path);
146152c9ce25SScott Long 	}
146252c9ce25SScott Long 
146352c9ce25SScott Long 	return (device);
146452c9ce25SScott Long }
146552c9ce25SScott Long 
146652c9ce25SScott Long static void
146752c9ce25SScott Long ata_device_transport(struct cam_path *path)
146852c9ce25SScott Long {
146952c9ce25SScott Long 	struct ccb_pathinq cpi;
14704b997c49SAlexander Motin 	struct ccb_trans_settings cts;
14714b997c49SAlexander Motin 	struct scsi_inquiry_data *inq_buf = NULL;
14724b997c49SAlexander Motin 	struct ata_params *ident_buf = NULL;
147352c9ce25SScott Long 
147452c9ce25SScott Long 	/* Get transport information from the SIM */
147583c5d981SAlexander Motin 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
147652c9ce25SScott Long 	cpi.ccb_h.func_code = XPT_PATH_INQ;
147752c9ce25SScott Long 	xpt_action((union ccb *)&cpi);
147852c9ce25SScott Long 
147952c9ce25SScott Long 	path->device->transport = cpi.transport;
14804b997c49SAlexander Motin 	if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
14814b997c49SAlexander Motin 		inq_buf = &path->device->inq_data;
14824b997c49SAlexander Motin 	if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) != 0)
14834b997c49SAlexander Motin 		ident_buf = &path->device->ident_data;
14844b997c49SAlexander Motin 	if (path->device->protocol == PROTO_ATA) {
14854b997c49SAlexander Motin 		path->device->protocol_version = ident_buf ?
14864b997c49SAlexander Motin 		    ata_version(ident_buf->version_major) : cpi.protocol_version;
14874b997c49SAlexander Motin 	} else if (path->device->protocol == PROTO_SCSI) {
14884b997c49SAlexander Motin 		path->device->protocol_version = inq_buf ?
14894b997c49SAlexander Motin 		    SID_ANSI_REV(inq_buf) : cpi.protocol_version;
149052c9ce25SScott Long 	}
14914b997c49SAlexander Motin 	path->device->transport_version = ident_buf ?
14924b997c49SAlexander Motin 	    ata_version(ident_buf->version_major) : cpi.transport_version;
149352c9ce25SScott Long 
149452c9ce25SScott Long 	/* Tell the controller what we think */
149583c5d981SAlexander Motin 	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
149652c9ce25SScott Long 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
149752c9ce25SScott Long 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
149852c9ce25SScott Long 	cts.transport = path->device->transport;
149952c9ce25SScott Long 	cts.transport_version = path->device->transport_version;
150052c9ce25SScott Long 	cts.protocol = path->device->protocol;
150152c9ce25SScott Long 	cts.protocol_version = path->device->protocol_version;
150252c9ce25SScott Long 	cts.proto_specific.valid = 0;
15034cca1530SAlexander Motin 	if (ident_buf) {
15044cca1530SAlexander Motin 		if (path->device->transport == XPORT_ATA) {
15054cca1530SAlexander Motin 			cts.xport_specific.ata.atapi =
15064cca1530SAlexander Motin 			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 :
15074cca1530SAlexander Motin 			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0;
15084cca1530SAlexander Motin 			cts.xport_specific.ata.valid = CTS_ATA_VALID_ATAPI;
15094cca1530SAlexander Motin 		} else {
15104cca1530SAlexander Motin 			cts.xport_specific.sata.atapi =
15114cca1530SAlexander Motin 			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 :
15124cca1530SAlexander Motin 			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0;
15134cca1530SAlexander Motin 			cts.xport_specific.sata.valid = CTS_SATA_VALID_ATAPI;
15144cca1530SAlexander Motin 		}
15154cca1530SAlexander Motin 	} else
151652c9ce25SScott Long 		cts.xport_specific.valid = 0;
151752c9ce25SScott Long 	xpt_action((union ccb *)&cts);
151852c9ce25SScott Long }
151952c9ce25SScott Long 
152052c9ce25SScott Long static void
152152c9ce25SScott Long ata_action(union ccb *start_ccb)
152252c9ce25SScott Long {
152352c9ce25SScott Long 
152452c9ce25SScott Long 	switch (start_ccb->ccb_h.func_code) {
152552c9ce25SScott Long 	case XPT_SET_TRAN_SETTINGS:
152652c9ce25SScott Long 	{
152730a4094fSAlexander Motin 		ata_set_transfer_settings(&start_ccb->cts,
152852c9ce25SScott Long 					   start_ccb->ccb_h.path->device,
152952c9ce25SScott Long 					   /*async_update*/FALSE);
153052c9ce25SScott Long 		break;
153152c9ce25SScott Long 	}
153252c9ce25SScott Long 	case XPT_SCAN_BUS:
153352c9ce25SScott Long 		ata_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
153452c9ce25SScott Long 		break;
153552c9ce25SScott Long 	case XPT_SCAN_LUN:
153652c9ce25SScott Long 		ata_scan_lun(start_ccb->ccb_h.path->periph,
153752c9ce25SScott Long 			      start_ccb->ccb_h.path, start_ccb->crcn.flags,
153852c9ce25SScott Long 			      start_ccb);
153952c9ce25SScott Long 		break;
154052c9ce25SScott Long 	case XPT_GET_TRAN_SETTINGS:
154152c9ce25SScott Long 	{
154252c9ce25SScott Long 		struct cam_sim *sim;
154352c9ce25SScott Long 
154452c9ce25SScott Long 		sim = start_ccb->ccb_h.path->bus->sim;
154552c9ce25SScott Long 		(*(sim->sim_action))(sim, start_ccb);
154652c9ce25SScott Long 		break;
154752c9ce25SScott Long 	}
15484cca1530SAlexander Motin 	case XPT_SCSI_IO:
15494cca1530SAlexander Motin 	{
15504cca1530SAlexander Motin 		struct cam_ed *device;
15514cca1530SAlexander Motin 		u_int	maxlen = 0;
15524cca1530SAlexander Motin 
15534cca1530SAlexander Motin 		device = start_ccb->ccb_h.path->device;
15544cca1530SAlexander Motin 		if (device->protocol == PROTO_SCSI &&
15554cca1530SAlexander Motin 		    (device->flags & CAM_DEV_IDENTIFY_DATA_VALID)) {
15564cca1530SAlexander Motin 			uint16_t p =
15574cca1530SAlexander Motin 			    device->ident_data.config & ATA_PROTO_MASK;
15584cca1530SAlexander Motin 
15594cca1530SAlexander Motin 			maxlen = (p == ATA_PROTO_ATAPI_16) ? 16 :
15604cca1530SAlexander Motin 			    (p == ATA_PROTO_ATAPI_12) ? 12 : 0;
15614cca1530SAlexander Motin 		}
15624cca1530SAlexander Motin 		if (start_ccb->csio.cdb_len > maxlen) {
15634cca1530SAlexander Motin 			start_ccb->ccb_h.status = CAM_REQ_INVALID;
15644cca1530SAlexander Motin 			xpt_done(start_ccb);
15654cca1530SAlexander Motin 			break;
15664cca1530SAlexander Motin 		}
15674cca1530SAlexander Motin 		/* FALLTHROUGH */
15684cca1530SAlexander Motin 	}
156952c9ce25SScott Long 	default:
157052c9ce25SScott Long 		xpt_action_default(start_ccb);
157152c9ce25SScott Long 		break;
157252c9ce25SScott Long 	}
157352c9ce25SScott Long }
157452c9ce25SScott Long 
157552c9ce25SScott Long static void
157630a4094fSAlexander Motin ata_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
157752c9ce25SScott Long 			   int async_update)
157852c9ce25SScott Long {
157952c9ce25SScott Long 	struct	ccb_pathinq cpi;
158052c9ce25SScott Long 	struct	ccb_trans_settings cur_cts;
158152c9ce25SScott Long 	struct	ccb_trans_settings_scsi *scsi;
158252c9ce25SScott Long 	struct	ccb_trans_settings_scsi *cur_scsi;
158352c9ce25SScott Long 	struct	cam_sim *sim;
158452c9ce25SScott Long 	struct	scsi_inquiry_data *inq_data;
158552c9ce25SScott Long 
158652c9ce25SScott Long 	if (device == NULL) {
158752c9ce25SScott Long 		cts->ccb_h.status = CAM_PATH_INVALID;
158852c9ce25SScott Long 		xpt_done((union ccb *)cts);
158952c9ce25SScott Long 		return;
159052c9ce25SScott Long 	}
159152c9ce25SScott Long 
159252c9ce25SScott Long 	if (cts->protocol == PROTO_UNKNOWN
159352c9ce25SScott Long 	 || cts->protocol == PROTO_UNSPECIFIED) {
159452c9ce25SScott Long 		cts->protocol = device->protocol;
159552c9ce25SScott Long 		cts->protocol_version = device->protocol_version;
159652c9ce25SScott Long 	}
159752c9ce25SScott Long 
159852c9ce25SScott Long 	if (cts->protocol_version == PROTO_VERSION_UNKNOWN
159952c9ce25SScott Long 	 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
160052c9ce25SScott Long 		cts->protocol_version = device->protocol_version;
160152c9ce25SScott Long 
160252c9ce25SScott Long 	if (cts->protocol != device->protocol) {
160352c9ce25SScott Long 		xpt_print(cts->ccb_h.path, "Uninitialized Protocol %x:%x?\n",
160452c9ce25SScott Long 		       cts->protocol, device->protocol);
160552c9ce25SScott Long 		cts->protocol = device->protocol;
160652c9ce25SScott Long 	}
160752c9ce25SScott Long 
160852c9ce25SScott Long 	if (cts->protocol_version > device->protocol_version) {
160952c9ce25SScott Long 		if (bootverbose) {
161052c9ce25SScott Long 			xpt_print(cts->ccb_h.path, "Down reving Protocol "
161152c9ce25SScott Long 			    "Version from %d to %d?\n", cts->protocol_version,
161252c9ce25SScott Long 			    device->protocol_version);
161352c9ce25SScott Long 		}
161452c9ce25SScott Long 		cts->protocol_version = device->protocol_version;
161552c9ce25SScott Long 	}
161652c9ce25SScott Long 
161752c9ce25SScott Long 	if (cts->transport == XPORT_UNKNOWN
161852c9ce25SScott Long 	 || cts->transport == XPORT_UNSPECIFIED) {
161952c9ce25SScott Long 		cts->transport = device->transport;
162052c9ce25SScott Long 		cts->transport_version = device->transport_version;
162152c9ce25SScott Long 	}
162252c9ce25SScott Long 
162352c9ce25SScott Long 	if (cts->transport_version == XPORT_VERSION_UNKNOWN
162452c9ce25SScott Long 	 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
162552c9ce25SScott Long 		cts->transport_version = device->transport_version;
162652c9ce25SScott Long 
162752c9ce25SScott Long 	if (cts->transport != device->transport) {
162852c9ce25SScott Long 		xpt_print(cts->ccb_h.path, "Uninitialized Transport %x:%x?\n",
162952c9ce25SScott Long 		    cts->transport, device->transport);
163052c9ce25SScott Long 		cts->transport = device->transport;
163152c9ce25SScott Long 	}
163252c9ce25SScott Long 
163352c9ce25SScott Long 	if (cts->transport_version > device->transport_version) {
163452c9ce25SScott Long 		if (bootverbose) {
163552c9ce25SScott Long 			xpt_print(cts->ccb_h.path, "Down reving Transport "
163652c9ce25SScott Long 			    "Version from %d to %d?\n", cts->transport_version,
163752c9ce25SScott Long 			    device->transport_version);
163852c9ce25SScott Long 		}
163952c9ce25SScott Long 		cts->transport_version = device->transport_version;
164052c9ce25SScott Long 	}
164152c9ce25SScott Long 
164252c9ce25SScott Long 	sim = cts->ccb_h.path->bus->sim;
164352c9ce25SScott Long 
164452c9ce25SScott Long 	/*
164552c9ce25SScott Long 	 * Nothing more of interest to do unless
164652c9ce25SScott Long 	 * this is a device connected via the
164752c9ce25SScott Long 	 * SCSI protocol.
164852c9ce25SScott Long 	 */
164952c9ce25SScott Long 	if (cts->protocol != PROTO_SCSI) {
165052c9ce25SScott Long 		if (async_update == FALSE)
165152c9ce25SScott Long 			(*(sim->sim_action))(sim, (union ccb *)cts);
165252c9ce25SScott Long 		return;
165352c9ce25SScott Long 	}
165452c9ce25SScott Long 
165552c9ce25SScott Long 	inq_data = &device->inq_data;
165652c9ce25SScott Long 	scsi = &cts->proto_specific.scsi;
165783c5d981SAlexander Motin 	xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NONE);
165852c9ce25SScott Long 	cpi.ccb_h.func_code = XPT_PATH_INQ;
165952c9ce25SScott Long 	xpt_action((union ccb *)&cpi);
166052c9ce25SScott Long 
166152c9ce25SScott Long 	/* SCSI specific sanity checking */
166252c9ce25SScott Long 	if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
166352c9ce25SScott Long 	 || (INQ_DATA_TQ_ENABLED(inq_data)) == 0
166452c9ce25SScott Long 	 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
166552c9ce25SScott Long 	 || (device->mintags == 0)) {
166652c9ce25SScott Long 		/*
166752c9ce25SScott Long 		 * Can't tag on hardware that doesn't support tags,
166852c9ce25SScott Long 		 * doesn't have it enabled, or has broken tag support.
166952c9ce25SScott Long 		 */
167052c9ce25SScott Long 		scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
167152c9ce25SScott Long 	}
167252c9ce25SScott Long 
167352c9ce25SScott Long 	if (async_update == FALSE) {
167452c9ce25SScott Long 		/*
167552c9ce25SScott Long 		 * Perform sanity checking against what the
167652c9ce25SScott Long 		 * controller and device can do.
167752c9ce25SScott Long 		 */
167883c5d981SAlexander Motin 		xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NONE);
167952c9ce25SScott Long 		cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
168052c9ce25SScott Long 		cur_cts.type = cts->type;
168152c9ce25SScott Long 		xpt_action((union ccb *)&cur_cts);
168252c9ce25SScott Long 		if ((cur_cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
168352c9ce25SScott Long 			return;
168452c9ce25SScott Long 		}
168552c9ce25SScott Long 		cur_scsi = &cur_cts.proto_specific.scsi;
168652c9ce25SScott Long 		if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
168752c9ce25SScott Long 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
168852c9ce25SScott Long 			scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
168952c9ce25SScott Long 		}
169052c9ce25SScott Long 		if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
169152c9ce25SScott Long 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
169252c9ce25SScott Long 	}
169352c9ce25SScott Long 
169452c9ce25SScott Long 	if (cts->type == CTS_TYPE_CURRENT_SETTINGS
169552c9ce25SScott Long 	 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
169652c9ce25SScott Long 		int device_tagenb;
169752c9ce25SScott Long 
169852c9ce25SScott Long 		/*
169952c9ce25SScott Long 		 * If we are transitioning from tags to no-tags or
170052c9ce25SScott Long 		 * vice-versa, we need to carefully freeze and restart
170152c9ce25SScott Long 		 * the queue so that we don't overlap tagged and non-tagged
170252c9ce25SScott Long 		 * commands.  We also temporarily stop tags if there is
170352c9ce25SScott Long 		 * a change in transfer negotiation settings to allow
170452c9ce25SScott Long 		 * "tag-less" negotiation.
170552c9ce25SScott Long 		 */
170652c9ce25SScott Long 		if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
170752c9ce25SScott Long 		 || (device->inq_flags & SID_CmdQue) != 0)
170852c9ce25SScott Long 			device_tagenb = TRUE;
170952c9ce25SScott Long 		else
171052c9ce25SScott Long 			device_tagenb = FALSE;
171152c9ce25SScott Long 
171252c9ce25SScott Long 		if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
171352c9ce25SScott Long 		  && device_tagenb == FALSE)
171452c9ce25SScott Long 		 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
171552c9ce25SScott Long 		  && device_tagenb == TRUE)) {
171652c9ce25SScott Long 
171752c9ce25SScott Long 			if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
171852c9ce25SScott Long 				/*
171952c9ce25SScott Long 				 * Delay change to use tags until after a
172052c9ce25SScott Long 				 * few commands have gone to this device so
172152c9ce25SScott Long 				 * the controller has time to perform transfer
172252c9ce25SScott Long 				 * negotiations without tagged messages getting
172352c9ce25SScott Long 				 * in the way.
172452c9ce25SScott Long 				 */
172552c9ce25SScott Long 				device->tag_delay_count = CAM_TAG_DELAY_COUNT;
172652c9ce25SScott Long 				device->flags |= CAM_DEV_TAG_AFTER_COUNT;
172752c9ce25SScott Long 			} else {
172830a4094fSAlexander Motin 				xpt_stop_tags(cts->ccb_h.path);
172952c9ce25SScott Long 			}
173052c9ce25SScott Long 		}
173152c9ce25SScott Long 	}
173252c9ce25SScott Long 	if (async_update == FALSE)
173352c9ce25SScott Long 		(*(sim->sim_action))(sim, (union ccb *)cts);
173452c9ce25SScott Long }
173552c9ce25SScott Long 
173652c9ce25SScott Long /*
173752c9ce25SScott Long  * Handle any per-device event notifications that require action by the XPT.
173852c9ce25SScott Long  */
173952c9ce25SScott Long static void
174052c9ce25SScott Long ata_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
174152c9ce25SScott Long 	      struct cam_ed *device, void *async_arg)
174252c9ce25SScott Long {
174352c9ce25SScott Long 	cam_status status;
174452c9ce25SScott Long 	struct cam_path newpath;
174552c9ce25SScott Long 
174652c9ce25SScott Long 	/*
174752c9ce25SScott Long 	 * We only need to handle events for real devices.
174852c9ce25SScott Long 	 */
174952c9ce25SScott Long 	if (target->target_id == CAM_TARGET_WILDCARD
175052c9ce25SScott Long 	 || device->lun_id == CAM_LUN_WILDCARD)
175152c9ce25SScott Long 		return;
175252c9ce25SScott Long 
175352c9ce25SScott Long 	/*
175452c9ce25SScott Long 	 * We need our own path with wildcards expanded to
175552c9ce25SScott Long 	 * handle certain types of events.
175652c9ce25SScott Long 	 */
175752c9ce25SScott Long 	if ((async_code == AC_SENT_BDR)
175852c9ce25SScott Long 	 || (async_code == AC_BUS_RESET)
175952c9ce25SScott Long 	 || (async_code == AC_INQ_CHANGED))
176052c9ce25SScott Long 		status = xpt_compile_path(&newpath, NULL,
176152c9ce25SScott Long 					  bus->path_id,
176252c9ce25SScott Long 					  target->target_id,
176352c9ce25SScott Long 					  device->lun_id);
176452c9ce25SScott Long 	else
176552c9ce25SScott Long 		status = CAM_REQ_CMP_ERR;
176652c9ce25SScott Long 
176752c9ce25SScott Long 	if (status == CAM_REQ_CMP) {
176852c9ce25SScott Long 		if (async_code == AC_INQ_CHANGED) {
176952c9ce25SScott Long 			/*
177052c9ce25SScott Long 			 * We've sent a start unit command, or
177152c9ce25SScott Long 			 * something similar to a device that
177252c9ce25SScott Long 			 * may have caused its inquiry data to
177352c9ce25SScott Long 			 * change. So we re-scan the device to
177452c9ce25SScott Long 			 * refresh the inquiry data for it.
177552c9ce25SScott Long 			 */
177652c9ce25SScott Long 			ata_scan_lun(newpath.periph, &newpath,
177752c9ce25SScott Long 				     CAM_EXPECT_INQ_CHANGE, NULL);
177883c5d981SAlexander Motin 		} else {
177983c5d981SAlexander Motin 			/* We need to reinitialize device after reset. */
178083c5d981SAlexander Motin 			ata_scan_lun(newpath.periph, &newpath,
178183c5d981SAlexander Motin 				     0, NULL);
178252c9ce25SScott Long 		}
178352c9ce25SScott Long 		xpt_release_path(&newpath);
1784f98d7a47SAlexander Motin 	} else if (async_code == AC_LOST_DEVICE &&
1785f98d7a47SAlexander Motin 	    (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
178652c9ce25SScott Long 		device->flags |= CAM_DEV_UNCONFIGURED;
1787f98d7a47SAlexander Motin 		xpt_release_device(device);
178852c9ce25SScott Long 	} else if (async_code == AC_TRANSFER_NEG) {
178952c9ce25SScott Long 		struct ccb_trans_settings *settings;
179052c9ce25SScott Long 
179152c9ce25SScott Long 		settings = (struct ccb_trans_settings *)async_arg;
179230a4094fSAlexander Motin 		ata_set_transfer_settings(settings, device,
179352c9ce25SScott Long 					  /*async_update*/TRUE);
179452c9ce25SScott Long 	}
179552c9ce25SScott Long }
179652c9ce25SScott Long 
179757079b17SAlexander Motin static void
179857079b17SAlexander Motin ata_announce_periph(struct cam_periph *periph)
179957079b17SAlexander Motin {
180057079b17SAlexander Motin 	struct	ccb_pathinq cpi;
180157079b17SAlexander Motin 	struct	ccb_trans_settings cts;
180257079b17SAlexander Motin 	struct	cam_path *path = periph->path;
180357079b17SAlexander Motin 	u_int	speed;
180457079b17SAlexander Motin 	u_int	mb;
180557079b17SAlexander Motin 
180657079b17SAlexander Motin 	mtx_assert(periph->sim->mtx, MA_OWNED);
180757079b17SAlexander Motin 
180857079b17SAlexander Motin 	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
180957079b17SAlexander Motin 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
181057079b17SAlexander Motin 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
181157079b17SAlexander Motin 	xpt_action((union ccb*)&cts);
181257079b17SAlexander Motin 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
181357079b17SAlexander Motin 		return;
181457079b17SAlexander Motin 	/* Ask the SIM for its base transfer speed */
181557079b17SAlexander Motin 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
181657079b17SAlexander Motin 	cpi.ccb_h.func_code = XPT_PATH_INQ;
181757079b17SAlexander Motin 	xpt_action((union ccb *)&cpi);
181857079b17SAlexander Motin 	/* Report connection speed */
181957079b17SAlexander Motin 	speed = cpi.base_transfer_speed;
182057079b17SAlexander Motin 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_ATA) {
182157079b17SAlexander Motin 		struct	ccb_trans_settings_ata *ata =
182257079b17SAlexander Motin 		    &cts.xport_specific.ata;
182357079b17SAlexander Motin 
182457079b17SAlexander Motin 		if (ata->valid & CTS_ATA_VALID_MODE)
182557079b17SAlexander Motin 			speed = ata_mode2speed(ata->mode);
182657079b17SAlexander Motin 	}
182757079b17SAlexander Motin 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SATA) {
182857079b17SAlexander Motin 		struct	ccb_trans_settings_sata *sata =
182957079b17SAlexander Motin 		    &cts.xport_specific.sata;
183057079b17SAlexander Motin 
183157079b17SAlexander Motin 		if (sata->valid & CTS_SATA_VALID_REVISION)
183257079b17SAlexander Motin 			speed = ata_revision2speed(sata->revision);
183357079b17SAlexander Motin 	}
183457079b17SAlexander Motin 	mb = speed / 1000;
183557079b17SAlexander Motin 	if (mb > 0)
183657079b17SAlexander Motin 		printf("%s%d: %d.%03dMB/s transfers",
183757079b17SAlexander Motin 		       periph->periph_name, periph->unit_number,
183857079b17SAlexander Motin 		       mb, speed % 1000);
183957079b17SAlexander Motin 	else
184057079b17SAlexander Motin 		printf("%s%d: %dKB/s transfers", periph->periph_name,
184157079b17SAlexander Motin 		       periph->unit_number, speed);
184257079b17SAlexander Motin 	/* Report additional information about connection */
184357079b17SAlexander Motin 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_ATA) {
184457079b17SAlexander Motin 		struct ccb_trans_settings_ata *ata =
184557079b17SAlexander Motin 		    &cts.xport_specific.ata;
184657079b17SAlexander Motin 
184757079b17SAlexander Motin 		printf(" (");
184857079b17SAlexander Motin 		if (ata->valid & CTS_ATA_VALID_MODE)
184957079b17SAlexander Motin 			printf("%s, ", ata_mode2string(ata->mode));
185057079b17SAlexander Motin 		if ((ata->valid & CTS_ATA_VALID_ATAPI) && ata->atapi != 0)
185157079b17SAlexander Motin 			printf("ATAPI %dbytes, ", ata->atapi);
185257079b17SAlexander Motin 		if (ata->valid & CTS_ATA_VALID_BYTECOUNT)
185357079b17SAlexander Motin 			printf("PIO %dbytes", ata->bytecount);
185457079b17SAlexander Motin 		printf(")");
185557079b17SAlexander Motin 	}
185657079b17SAlexander Motin 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SATA) {
185757079b17SAlexander Motin 		struct ccb_trans_settings_sata *sata =
185857079b17SAlexander Motin 		    &cts.xport_specific.sata;
185957079b17SAlexander Motin 
186057079b17SAlexander Motin 		printf(" (");
186157079b17SAlexander Motin 		if (sata->valid & CTS_SATA_VALID_REVISION)
186257079b17SAlexander Motin 			printf("SATA %d.x, ", sata->revision);
186357079b17SAlexander Motin 		else
186457079b17SAlexander Motin 			printf("SATA, ");
186557079b17SAlexander Motin 		if (sata->valid & CTS_SATA_VALID_MODE)
186657079b17SAlexander Motin 			printf("%s, ", ata_mode2string(sata->mode));
186757079b17SAlexander Motin 		if ((sata->valid & CTS_ATA_VALID_ATAPI) && sata->atapi != 0)
186857079b17SAlexander Motin 			printf("ATAPI %dbytes, ", sata->atapi);
186957079b17SAlexander Motin 		if (sata->valid & CTS_SATA_VALID_BYTECOUNT)
187057079b17SAlexander Motin 			printf("PIO %dbytes", sata->bytecount);
187157079b17SAlexander Motin 		printf(")");
187257079b17SAlexander Motin 	}
187357079b17SAlexander Motin 	printf("\n");
187457079b17SAlexander Motin }
187557079b17SAlexander Motin 
1876