xref: /freebsd/sys/cam/ata/ata_xpt.c (revision 025e2c12)
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 #include <cam/cam.h>
4852c9ce25SScott Long #include <cam/cam_ccb.h>
4952c9ce25SScott Long #include <cam/cam_queue.h>
5052c9ce25SScott Long #include <cam/cam_periph.h>
5152c9ce25SScott Long #include <cam/cam_sim.h>
5252c9ce25SScott Long #include <cam/cam_xpt.h>
5352c9ce25SScott Long #include <cam/cam_xpt_sim.h>
5452c9ce25SScott Long #include <cam/cam_xpt_periph.h>
5552c9ce25SScott Long #include <cam/cam_xpt_internal.h>
5652c9ce25SScott Long #include <cam/cam_debug.h>
5752c9ce25SScott Long 
5852c9ce25SScott Long #include <cam/scsi/scsi_all.h>
5952c9ce25SScott Long #include <cam/scsi/scsi_message.h>
6052c9ce25SScott Long #include <cam/ata/ata_all.h>
6152c9ce25SScott Long #include <machine/stdarg.h>	/* for xpt_print below */
6252c9ce25SScott Long #include "opt_cam.h"
6352c9ce25SScott Long 
6430a4094fSAlexander Motin struct ata_quirk_entry {
6552c9ce25SScott Long 	struct scsi_inquiry_pattern inq_pat;
6652c9ce25SScott Long 	u_int8_t quirks;
6730a4094fSAlexander Motin #define	CAM_QUIRK_MAXTAGS	0x01
6852c9ce25SScott Long 	u_int maxtags;
6952c9ce25SScott Long };
7052c9ce25SScott Long 
7152c9ce25SScott Long static periph_init_t probe_periph_init;
7252c9ce25SScott Long 
7352c9ce25SScott Long static struct periph_driver probe_driver =
7452c9ce25SScott Long {
75f09f8e3eSAlexander Motin 	probe_periph_init, "aprobe",
761e637ba6SAlexander Motin 	TAILQ_HEAD_INITIALIZER(probe_driver.units), /* generation */ 0,
771e637ba6SAlexander Motin 	CAM_PERIPH_DRV_EARLY
7852c9ce25SScott Long };
7952c9ce25SScott Long 
80f09f8e3eSAlexander Motin PERIPHDRIVER_DECLARE(aprobe, probe_driver);
8152c9ce25SScott Long 
8252c9ce25SScott Long typedef enum {
8352c9ce25SScott Long 	PROBE_RESET,
8452c9ce25SScott Long 	PROBE_IDENTIFY,
854ef08dc5SAlexander Motin 	PROBE_SPINUP,
8652c9ce25SScott Long 	PROBE_SETMODE,
87da6808c1SAlexander Motin 	PROBE_SETPM,
88da6808c1SAlexander Motin 	PROBE_SETAPST,
89da6808c1SAlexander Motin 	PROBE_SETDMAAA,
901e637ba6SAlexander Motin 	PROBE_SET_MULTI,
9152c9ce25SScott Long 	PROBE_INQUIRY,
9252c9ce25SScott Long 	PROBE_FULL_INQUIRY,
9352c9ce25SScott Long 	PROBE_PM_PID,
9452c9ce25SScott Long 	PROBE_PM_PRV,
9552c9ce25SScott Long 	PROBE_INVALID
9652c9ce25SScott Long } probe_action;
9752c9ce25SScott Long 
9852c9ce25SScott Long static char *probe_action_text[] = {
9952c9ce25SScott Long 	"PROBE_RESET",
10052c9ce25SScott Long 	"PROBE_IDENTIFY",
1014ef08dc5SAlexander Motin 	"PROBE_SPINUP",
10252c9ce25SScott Long 	"PROBE_SETMODE",
103da6808c1SAlexander Motin 	"PROBE_SETPM",
104da6808c1SAlexander Motin 	"PROBE_SETAPST",
105da6808c1SAlexander Motin 	"PROBE_SETDMAAA",
1061e637ba6SAlexander Motin 	"PROBE_SET_MULTI",
10752c9ce25SScott Long 	"PROBE_INQUIRY",
10852c9ce25SScott Long 	"PROBE_FULL_INQUIRY",
10952c9ce25SScott Long 	"PROBE_PM_PID",
11052c9ce25SScott Long 	"PROBE_PM_PRV",
11152c9ce25SScott Long 	"PROBE_INVALID"
11252c9ce25SScott Long };
11352c9ce25SScott Long 
11452c9ce25SScott Long #define PROBE_SET_ACTION(softc, newaction)	\
11552c9ce25SScott Long do {									\
11652c9ce25SScott Long 	char **text;							\
11752c9ce25SScott Long 	text = probe_action_text;					\
11852c9ce25SScott Long 	CAM_DEBUG((softc)->periph->path, CAM_DEBUG_INFO,		\
11952c9ce25SScott Long 	    ("Probe %s to %s\n", text[(softc)->action],			\
12052c9ce25SScott Long 	    text[(newaction)]));					\
12152c9ce25SScott Long 	(softc)->action = (newaction);					\
12252c9ce25SScott Long } while(0)
12352c9ce25SScott Long 
12452c9ce25SScott Long typedef enum {
12552c9ce25SScott Long 	PROBE_NO_ANNOUNCE	= 0x04
12652c9ce25SScott Long } probe_flags;
12752c9ce25SScott Long 
12852c9ce25SScott Long typedef struct {
12952c9ce25SScott Long 	TAILQ_HEAD(, ccb_hdr) request_ccbs;
130a9b8edb1SAlexander Motin 	struct ata_params	ident_data;
13152c9ce25SScott Long 	probe_action	action;
13252c9ce25SScott Long 	probe_flags	flags;
13352c9ce25SScott Long 	uint32_t	pm_pid;
13452c9ce25SScott Long 	uint32_t	pm_prv;
13583c5d981SAlexander Motin 	int		restart;
1364ef08dc5SAlexander Motin 	int		spinup;
13725a519a9SAlexander Motin 	int		faults;
138da6808c1SAlexander Motin 	u_int		caps;
13952c9ce25SScott Long 	struct cam_periph *periph;
14052c9ce25SScott Long } probe_softc;
14152c9ce25SScott Long 
14230a4094fSAlexander Motin static struct ata_quirk_entry ata_quirk_table[] =
14352c9ce25SScott Long {
14452c9ce25SScott Long 	{
14552c9ce25SScott Long 		/* Default tagged queuing parameters for all devices */
14652c9ce25SScott Long 		{
14752c9ce25SScott Long 		  T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
14852c9ce25SScott Long 		  /*vendor*/"*", /*product*/"*", /*revision*/"*"
14952c9ce25SScott Long 		},
15030a4094fSAlexander Motin 		/*quirks*/0, /*maxtags*/0
15152c9ce25SScott Long 	},
15252c9ce25SScott Long };
15352c9ce25SScott Long 
15430a4094fSAlexander Motin static const int ata_quirk_table_size =
15530a4094fSAlexander Motin 	sizeof(ata_quirk_table) / sizeof(*ata_quirk_table);
15652c9ce25SScott Long 
15752c9ce25SScott Long static cam_status	proberegister(struct cam_periph *periph,
15852c9ce25SScott Long 				      void *arg);
15952c9ce25SScott Long static void	 probeschedule(struct cam_periph *probe_periph);
16052c9ce25SScott Long static void	 probestart(struct cam_periph *periph, union ccb *start_ccb);
16152c9ce25SScott Long //static void	 proberequestdefaultnegotiation(struct cam_periph *periph);
16252c9ce25SScott Long //static int       proberequestbackoff(struct cam_periph *periph,
16352c9ce25SScott Long //				     struct cam_ed *device);
16452c9ce25SScott Long static void	 probedone(struct cam_periph *periph, union ccb *done_ccb);
16552c9ce25SScott Long static void	 probecleanup(struct cam_periph *periph);
16630a4094fSAlexander Motin static void	 ata_find_quirk(struct cam_ed *device);
16752c9ce25SScott Long static void	 ata_scan_bus(struct cam_periph *periph, union ccb *ccb);
16852c9ce25SScott Long static void	 ata_scan_lun(struct cam_periph *periph,
16952c9ce25SScott Long 			       struct cam_path *path, cam_flags flags,
17052c9ce25SScott Long 			       union ccb *ccb);
17152c9ce25SScott Long static void	 xptscandone(struct cam_periph *periph, union ccb *done_ccb);
17252c9ce25SScott Long static struct cam_ed *
17352c9ce25SScott Long 		 ata_alloc_device(struct cam_eb *bus, struct cam_et *target,
17452c9ce25SScott Long 				   lun_id_t lun_id);
17552c9ce25SScott Long static void	 ata_device_transport(struct cam_path *path);
17630a4094fSAlexander Motin static void	 ata_set_transfer_settings(struct ccb_trans_settings *cts,
17752c9ce25SScott Long 					    struct cam_ed *device,
17852c9ce25SScott Long 					    int async_update);
17952c9ce25SScott Long static void	 ata_dev_async(u_int32_t async_code,
18052c9ce25SScott Long 				struct cam_eb *bus,
18152c9ce25SScott Long 				struct cam_et *target,
18252c9ce25SScott Long 				struct cam_ed *device,
18352c9ce25SScott Long 				void *async_arg);
18452c9ce25SScott Long static void	 ata_action(union ccb *start_ccb);
18557079b17SAlexander Motin static void	 ata_announce_periph(struct cam_periph *periph);
18652c9ce25SScott Long 
18752c9ce25SScott Long static struct xpt_xport ata_xport = {
18852c9ce25SScott Long 	.alloc_device = ata_alloc_device,
18952c9ce25SScott Long 	.action = ata_action,
19052c9ce25SScott Long 	.async = ata_dev_async,
19157079b17SAlexander Motin 	.announce = ata_announce_periph,
19252c9ce25SScott Long };
19352c9ce25SScott Long 
19452c9ce25SScott Long struct xpt_xport *
19552c9ce25SScott Long ata_get_xport(void)
19652c9ce25SScott Long {
19752c9ce25SScott Long 	return (&ata_xport);
19852c9ce25SScott Long }
19952c9ce25SScott Long 
20052c9ce25SScott Long static void
20152c9ce25SScott Long probe_periph_init()
20252c9ce25SScott Long {
20352c9ce25SScott Long }
20452c9ce25SScott Long 
20552c9ce25SScott Long static cam_status
20652c9ce25SScott Long proberegister(struct cam_periph *periph, void *arg)
20752c9ce25SScott Long {
20852c9ce25SScott Long 	union ccb *request_ccb;	/* CCB representing the probe request */
20952c9ce25SScott Long 	cam_status status;
21052c9ce25SScott Long 	probe_softc *softc;
21152c9ce25SScott Long 
21252c9ce25SScott Long 	request_ccb = (union ccb *)arg;
21352c9ce25SScott Long 	if (periph == NULL) {
21452c9ce25SScott Long 		printf("proberegister: periph was NULL!!\n");
21552c9ce25SScott Long 		return(CAM_REQ_CMP_ERR);
21652c9ce25SScott Long 	}
21752c9ce25SScott Long 
21852c9ce25SScott Long 	if (request_ccb == NULL) {
21952c9ce25SScott Long 		printf("proberegister: no probe CCB, "
22052c9ce25SScott Long 		       "can't register device\n");
22152c9ce25SScott Long 		return(CAM_REQ_CMP_ERR);
22252c9ce25SScott Long 	}
22352c9ce25SScott Long 
2244ef08dc5SAlexander Motin 	softc = (probe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_ZERO | M_NOWAIT);
22552c9ce25SScott Long 
22652c9ce25SScott Long 	if (softc == NULL) {
22752c9ce25SScott Long 		printf("proberegister: Unable to probe new device. "
22852c9ce25SScott Long 		       "Unable to allocate softc\n");
22952c9ce25SScott Long 		return(CAM_REQ_CMP_ERR);
23052c9ce25SScott Long 	}
23152c9ce25SScott Long 	TAILQ_INIT(&softc->request_ccbs);
23252c9ce25SScott Long 	TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
23352c9ce25SScott Long 			  periph_links.tqe);
23452c9ce25SScott Long 	softc->flags = 0;
23552c9ce25SScott Long 	periph->softc = softc;
23652c9ce25SScott Long 	softc->periph = periph;
23752c9ce25SScott Long 	softc->action = PROBE_INVALID;
23852c9ce25SScott Long 	status = cam_periph_acquire(periph);
23952c9ce25SScott Long 	if (status != CAM_REQ_CMP) {
24052c9ce25SScott Long 		return (status);
24152c9ce25SScott Long 	}
24252c9ce25SScott Long 	/*
24383c5d981SAlexander Motin 	 * Ensure nobody slip in until probe finish.
24452c9ce25SScott Long 	 */
24583c5d981SAlexander Motin 	cam_freeze_devq_arg(periph->path,
24683c5d981SAlexander Motin 	    RELSIM_RELEASE_RUNLEVEL, CAM_RL_XPT + 1);
24752c9ce25SScott Long 	probeschedule(periph);
24852c9ce25SScott Long 	return(CAM_REQ_CMP);
24952c9ce25SScott Long }
25052c9ce25SScott Long 
25152c9ce25SScott Long static void
25252c9ce25SScott Long probeschedule(struct cam_periph *periph)
25352c9ce25SScott Long {
25452c9ce25SScott Long 	union ccb *ccb;
25552c9ce25SScott Long 	probe_softc *softc;
25652c9ce25SScott Long 
25752c9ce25SScott Long 	softc = (probe_softc *)periph->softc;
25852c9ce25SScott Long 	ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
25952c9ce25SScott Long 
26065d0fb03SAlexander Motin 	if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) ||
26165d0fb03SAlexander Motin 	    periph->path->device->protocol == PROTO_SATAPM)
26252c9ce25SScott Long 		PROBE_SET_ACTION(softc, PROBE_RESET);
26352c9ce25SScott Long 	else
26452c9ce25SScott Long 		PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
26552c9ce25SScott Long 
26652c9ce25SScott Long 	if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
26752c9ce25SScott Long 		softc->flags |= PROBE_NO_ANNOUNCE;
26852c9ce25SScott Long 	else
26952c9ce25SScott Long 		softc->flags &= ~PROBE_NO_ANNOUNCE;
27052c9ce25SScott Long 
27183c5d981SAlexander Motin 	xpt_schedule(periph, CAM_PRIORITY_XPT);
27252c9ce25SScott Long }
27352c9ce25SScott Long 
27452c9ce25SScott Long static void
27552c9ce25SScott Long probestart(struct cam_periph *periph, union ccb *start_ccb)
27652c9ce25SScott Long {
277c8039fc6SAlexander Motin 	struct ccb_trans_settings cts;
27852c9ce25SScott Long 	struct ccb_ataio *ataio;
27952c9ce25SScott Long 	struct ccb_scsiio *csio;
28052c9ce25SScott Long 	probe_softc *softc;
2811e637ba6SAlexander Motin 	struct cam_path *path;
2821e637ba6SAlexander Motin 	struct ata_params *ident_buf;
28352c9ce25SScott Long 
28452c9ce25SScott Long 	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
28552c9ce25SScott Long 
28652c9ce25SScott Long 	softc = (probe_softc *)periph->softc;
2871e637ba6SAlexander Motin 	path = start_ccb->ccb_h.path;
28852c9ce25SScott Long 	ataio = &start_ccb->ataio;
28952c9ce25SScott Long 	csio = &start_ccb->csio;
2901e637ba6SAlexander Motin 	ident_buf = &periph->path->device->ident_data;
29152c9ce25SScott Long 
29283c5d981SAlexander Motin 	if (softc->restart) {
29383c5d981SAlexander Motin 		softc->restart = 0;
29483c5d981SAlexander Motin 		if ((path->device->flags & CAM_DEV_UNCONFIGURED) ||
29583c5d981SAlexander Motin 		    path->device->protocol == PROTO_SATAPM)
29683c5d981SAlexander Motin 			softc->action = PROBE_RESET;
29783c5d981SAlexander Motin 		else
29883c5d981SAlexander Motin 			softc->action = PROBE_IDENTIFY;
29983c5d981SAlexander Motin 	}
30052c9ce25SScott Long 	switch (softc->action) {
30152c9ce25SScott Long 	case PROBE_RESET:
30252c9ce25SScott Long 		cam_fill_ataio(ataio,
30352c9ce25SScott Long 		      0,
30452c9ce25SScott Long 		      probedone,
30552c9ce25SScott Long 		      /*flags*/CAM_DIR_NONE,
30665d0fb03SAlexander Motin 		      0,
30752c9ce25SScott Long 		      /*data_ptr*/NULL,
30852c9ce25SScott Long 		      /*dxfer_len*/0,
30983c5d981SAlexander Motin 		      15 * 1000);
31052c9ce25SScott Long 		ata_reset_cmd(ataio);
31152c9ce25SScott Long 		break;
31252c9ce25SScott Long 	case PROBE_IDENTIFY:
31352c9ce25SScott Long 		cam_fill_ataio(ataio,
31452c9ce25SScott Long 		      1,
31552c9ce25SScott Long 		      probedone,
31652c9ce25SScott Long 		      /*flags*/CAM_DIR_IN,
31765d0fb03SAlexander Motin 		      0,
318a9b8edb1SAlexander Motin 		      /*data_ptr*/(u_int8_t *)&softc->ident_data,
319a9b8edb1SAlexander Motin 		      /*dxfer_len*/sizeof(softc->ident_data),
32052c9ce25SScott Long 		      30 * 1000);
32152c9ce25SScott Long 		if (periph->path->device->protocol == PROTO_ATA)
3227606b445SAlexander Motin 			ata_28bit_cmd(ataio, ATA_ATA_IDENTIFY, 0, 0, 0);
32352c9ce25SScott Long 		else
3247606b445SAlexander Motin 			ata_28bit_cmd(ataio, ATA_ATAPI_IDENTIFY, 0, 0, 0);
32552c9ce25SScott Long 		break;
3264ef08dc5SAlexander Motin 	case PROBE_SPINUP:
3274ef08dc5SAlexander Motin 		if (bootverbose)
3284ef08dc5SAlexander Motin 			xpt_print(path, "Spinning up device\n");
3294ef08dc5SAlexander Motin 		cam_fill_ataio(ataio,
3304ef08dc5SAlexander Motin 		      1,
3314ef08dc5SAlexander Motin 		      probedone,
3324ef08dc5SAlexander Motin 		      /*flags*/CAM_DIR_NONE | CAM_HIGH_POWER,
3334ef08dc5SAlexander Motin 		      0,
3344ef08dc5SAlexander Motin 		      /*data_ptr*/NULL,
3354ef08dc5SAlexander Motin 		      /*dxfer_len*/0,
3364ef08dc5SAlexander Motin 		      30 * 1000);
3374ef08dc5SAlexander Motin 		ata_28bit_cmd(ataio, ATA_SETFEATURES, ATA_SF_PUIS_SPINUP, 0, 0);
3384ef08dc5SAlexander Motin 		break;
33952c9ce25SScott Long 	case PROBE_SETMODE:
340c8039fc6SAlexander Motin 	{
341c8039fc6SAlexander Motin 		int mode, wantmode;
342c8039fc6SAlexander Motin 
343c8039fc6SAlexander Motin 		mode = 0;
344c8039fc6SAlexander Motin 		/* Fetch user modes from SIM. */
345c8039fc6SAlexander Motin 		bzero(&cts, sizeof(cts));
34683c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
347c8039fc6SAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
348c8039fc6SAlexander Motin 		cts.type = CTS_TYPE_USER_SETTINGS;
349c8039fc6SAlexander Motin 		xpt_action((union ccb *)&cts);
350c8039fc6SAlexander Motin 		if (path->device->transport == XPORT_ATA) {
351c8039fc6SAlexander Motin 			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_MODE)
352c8039fc6SAlexander Motin 				mode = cts.xport_specific.ata.mode;
353c8039fc6SAlexander Motin 		} else {
3541a6f09b8SAlexander Motin 			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_MODE)
355c8039fc6SAlexander Motin 				mode = cts.xport_specific.sata.mode;
356c8039fc6SAlexander Motin 		}
357c8039fc6SAlexander Motin negotiate:
358c8039fc6SAlexander Motin 		/* Honor device capabilities. */
359c8039fc6SAlexander Motin 		wantmode = mode = ata_max_mode(ident_buf, mode);
360c8039fc6SAlexander Motin 		/* Report modes to SIM. */
361c8039fc6SAlexander Motin 		bzero(&cts, sizeof(cts));
36283c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
363c8039fc6SAlexander Motin 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
364c8039fc6SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
365c8039fc6SAlexander Motin 		if (path->device->transport == XPORT_ATA) {
366c8039fc6SAlexander Motin 			cts.xport_specific.ata.mode = mode;
367c8039fc6SAlexander Motin 			cts.xport_specific.ata.valid = CTS_ATA_VALID_MODE;
368c8039fc6SAlexander Motin 		} else {
369c8039fc6SAlexander Motin 			cts.xport_specific.sata.mode = mode;
370c8039fc6SAlexander Motin 			cts.xport_specific.sata.valid = CTS_SATA_VALID_MODE;
371c8039fc6SAlexander Motin 		}
372c8039fc6SAlexander Motin 		xpt_action((union ccb *)&cts);
373066f913aSAlexander Motin 		/* Fetch current modes from SIM. */
374c8039fc6SAlexander Motin 		bzero(&cts, sizeof(cts));
37583c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
376c8039fc6SAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
377c8039fc6SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
378c8039fc6SAlexander Motin 		xpt_action((union ccb *)&cts);
379c8039fc6SAlexander Motin 		if (path->device->transport == XPORT_ATA) {
380c8039fc6SAlexander Motin 			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_MODE)
381c8039fc6SAlexander Motin 				mode = cts.xport_specific.ata.mode;
382c8039fc6SAlexander Motin 		} else {
383c8039fc6SAlexander Motin 			if (cts.xport_specific.ata.valid & CTS_SATA_VALID_MODE)
384c8039fc6SAlexander Motin 				mode = cts.xport_specific.sata.mode;
385c8039fc6SAlexander Motin 		}
386c8039fc6SAlexander Motin 		/* If SIM disagree - renegotiate. */
387c8039fc6SAlexander Motin 		if (mode != wantmode)
388c8039fc6SAlexander Motin 			goto negotiate;
38952c9ce25SScott Long 		cam_fill_ataio(ataio,
39052c9ce25SScott Long 		      1,
39152c9ce25SScott Long 		      probedone,
3925daa555dSAlexander Motin 		      /*flags*/CAM_DIR_NONE,
3935daa555dSAlexander Motin 		      0,
3945daa555dSAlexander Motin 		      /*data_ptr*/NULL,
3955daa555dSAlexander Motin 		      /*dxfer_len*/0,
39652c9ce25SScott Long 		      30 * 1000);
397c8039fc6SAlexander Motin 		ata_28bit_cmd(ataio, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
39852c9ce25SScott Long 		break;
399c8039fc6SAlexander Motin 	}
400da6808c1SAlexander Motin 	case PROBE_SETPM:
401da6808c1SAlexander Motin 		cam_fill_ataio(ataio,
402da6808c1SAlexander Motin 		    1,
403da6808c1SAlexander Motin 		    probedone,
404da6808c1SAlexander Motin 		    CAM_DIR_NONE,
405da6808c1SAlexander Motin 		    0,
406da6808c1SAlexander Motin 		    NULL,
407da6808c1SAlexander Motin 		    0,
408da6808c1SAlexander Motin 		    30*1000);
409da6808c1SAlexander Motin 		ata_28bit_cmd(ataio, ATA_SETFEATURES,
410da6808c1SAlexander Motin 		    (softc->caps & CTS_SATA_CAPS_H_PMREQ) ? 0x10 : 0x90,
411da6808c1SAlexander Motin 		    0, 0x03);
412da6808c1SAlexander Motin 		break;
413da6808c1SAlexander Motin 	case PROBE_SETAPST:
414da6808c1SAlexander Motin 		cam_fill_ataio(ataio,
415da6808c1SAlexander Motin 		    1,
416da6808c1SAlexander Motin 		    probedone,
417da6808c1SAlexander Motin 		    CAM_DIR_NONE,
418da6808c1SAlexander Motin 		    0,
419da6808c1SAlexander Motin 		    NULL,
420da6808c1SAlexander Motin 		    0,
421da6808c1SAlexander Motin 		    30*1000);
422da6808c1SAlexander Motin 		ata_28bit_cmd(ataio, ATA_SETFEATURES,
423da6808c1SAlexander Motin 		    (softc->caps & CTS_SATA_CAPS_H_APST) ? 0x10 : 0x90,
424da6808c1SAlexander Motin 		    0, 0x07);
425da6808c1SAlexander Motin 		break;
426da6808c1SAlexander Motin 	case PROBE_SETDMAAA:
427da6808c1SAlexander Motin 		cam_fill_ataio(ataio,
428da6808c1SAlexander Motin 		    1,
429da6808c1SAlexander Motin 		    probedone,
430da6808c1SAlexander Motin 		    CAM_DIR_NONE,
431da6808c1SAlexander Motin 		    0,
432da6808c1SAlexander Motin 		    NULL,
433da6808c1SAlexander Motin 		    0,
434da6808c1SAlexander Motin 		    30*1000);
435da6808c1SAlexander Motin 		ata_28bit_cmd(ataio, ATA_SETFEATURES,
436da6808c1SAlexander Motin 		    (softc->caps & CTS_SATA_CAPS_H_DMAAA) ? 0x10 : 0x90,
437da6808c1SAlexander Motin 		    0, 0x02);
438da6808c1SAlexander Motin 		break;
4391e637ba6SAlexander Motin 	case PROBE_SET_MULTI:
4401e637ba6SAlexander Motin 	{
441066f913aSAlexander Motin 		u_int sectors, bytecount;
4421e637ba6SAlexander Motin 
443066f913aSAlexander Motin 		bytecount = 8192;	/* SATA maximum */
444066f913aSAlexander Motin 		/* Fetch user bytecount from SIM. */
445066f913aSAlexander Motin 		bzero(&cts, sizeof(cts));
44683c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
447066f913aSAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
448066f913aSAlexander Motin 		cts.type = CTS_TYPE_USER_SETTINGS;
449066f913aSAlexander Motin 		xpt_action((union ccb *)&cts);
450066f913aSAlexander Motin 		if (path->device->transport == XPORT_ATA) {
451066f913aSAlexander Motin 			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
452066f913aSAlexander Motin 				bytecount = cts.xport_specific.ata.bytecount;
453066f913aSAlexander Motin 		} else {
454066f913aSAlexander Motin 			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
455066f913aSAlexander Motin 				bytecount = cts.xport_specific.sata.bytecount;
456066f913aSAlexander Motin 		}
457066f913aSAlexander Motin 		/* Honor device capabilities. */
458066f913aSAlexander Motin 		sectors = max(1, min(ident_buf->sectors_intr & 0xff,
459066f913aSAlexander Motin 		    bytecount / ata_logical_sector_size(ident_buf)));
4601e637ba6SAlexander Motin 		/* Report bytecount to SIM. */
4611e637ba6SAlexander Motin 		bzero(&cts, sizeof(cts));
46283c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
4631e637ba6SAlexander Motin 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
4641e637ba6SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
4651e637ba6SAlexander Motin 		if (path->device->transport == XPORT_ATA) {
466c1bd46c2SAlexander Motin 			cts.xport_specific.ata.bytecount = sectors *
467c1bd46c2SAlexander Motin 			    ata_logical_sector_size(ident_buf);
4681e637ba6SAlexander Motin 			cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT;
4691e637ba6SAlexander Motin 		} else {
470c1bd46c2SAlexander Motin 			cts.xport_specific.sata.bytecount = sectors *
471c1bd46c2SAlexander Motin 			    ata_logical_sector_size(ident_buf);
4721e637ba6SAlexander Motin 			cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT;
4731e637ba6SAlexander Motin 		}
4741e637ba6SAlexander Motin 		xpt_action((union ccb *)&cts);
475066f913aSAlexander Motin 		/* Fetch current bytecount from SIM. */
476066f913aSAlexander Motin 		bzero(&cts, sizeof(cts));
47783c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
478066f913aSAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
479066f913aSAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
480066f913aSAlexander Motin 		xpt_action((union ccb *)&cts);
481066f913aSAlexander Motin 		if (path->device->transport == XPORT_ATA) {
482066f913aSAlexander Motin 			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
483066f913aSAlexander Motin 				bytecount = cts.xport_specific.ata.bytecount;
484066f913aSAlexander Motin 		} else {
485066f913aSAlexander Motin 			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
486066f913aSAlexander Motin 				bytecount = cts.xport_specific.sata.bytecount;
487066f913aSAlexander Motin 		}
488066f913aSAlexander Motin 		sectors = bytecount / ata_logical_sector_size(ident_buf);
4891e637ba6SAlexander Motin 
4901e637ba6SAlexander Motin 		cam_fill_ataio(ataio,
4911e637ba6SAlexander Motin 		    1,
4921e637ba6SAlexander Motin 		    probedone,
4931e637ba6SAlexander Motin 		    CAM_DIR_NONE,
4941e637ba6SAlexander Motin 		    0,
4951e637ba6SAlexander Motin 		    NULL,
4961e637ba6SAlexander Motin 		    0,
4971e637ba6SAlexander Motin 		    30*1000);
4981e637ba6SAlexander Motin 		ata_28bit_cmd(ataio, ATA_SET_MULTI, 0, 0, sectors);
4991e637ba6SAlexander Motin 		break;
50052c9ce25SScott Long 	}
50152c9ce25SScott Long 	case PROBE_INQUIRY:
502066f913aSAlexander Motin 	{
503066f913aSAlexander Motin 		u_int bytecount;
504066f913aSAlexander Motin 
505066f913aSAlexander Motin 		bytecount = 8192;	/* SATA maximum */
506066f913aSAlexander Motin 		/* Fetch user bytecount from SIM. */
507066f913aSAlexander Motin 		bzero(&cts, sizeof(cts));
50883c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
509066f913aSAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
510066f913aSAlexander Motin 		cts.type = CTS_TYPE_USER_SETTINGS;
511066f913aSAlexander Motin 		xpt_action((union ccb *)&cts);
512066f913aSAlexander Motin 		if (path->device->transport == XPORT_ATA) {
513066f913aSAlexander Motin 			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
514066f913aSAlexander Motin 				bytecount = cts.xport_specific.ata.bytecount;
515066f913aSAlexander Motin 		} else {
516066f913aSAlexander Motin 			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
517066f913aSAlexander Motin 				bytecount = cts.xport_specific.sata.bytecount;
518066f913aSAlexander Motin 		}
519066f913aSAlexander Motin 		/* Honor device capabilities. */
520066f913aSAlexander Motin 		bytecount &= ~1;
521066f913aSAlexander Motin 		bytecount = max(2, min(65534, bytecount));
522066f913aSAlexander Motin 		if (ident_buf->satacapabilities != 0x0000 &&
523066f913aSAlexander Motin 		    ident_buf->satacapabilities != 0xffff) {
524066f913aSAlexander Motin 			bytecount = min(8192, bytecount);
525066f913aSAlexander Motin 		}
526066f913aSAlexander Motin 		/* Report bytecount to SIM. */
527066f913aSAlexander Motin 		bzero(&cts, sizeof(cts));
52883c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
529066f913aSAlexander Motin 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
530066f913aSAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
531066f913aSAlexander Motin 		if (path->device->transport == XPORT_ATA) {
532066f913aSAlexander Motin 			cts.xport_specific.ata.bytecount = bytecount;
533066f913aSAlexander Motin 			cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT;
534066f913aSAlexander Motin 		} else {
535066f913aSAlexander Motin 			cts.xport_specific.sata.bytecount = bytecount;
536066f913aSAlexander Motin 			cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT;
537066f913aSAlexander Motin 		}
538066f913aSAlexander Motin 		xpt_action((union ccb *)&cts);
539066f913aSAlexander Motin 		/* FALLTHROUGH */
540066f913aSAlexander Motin 	}
54152c9ce25SScott Long 	case PROBE_FULL_INQUIRY:
54252c9ce25SScott Long 	{
54352c9ce25SScott Long 		u_int inquiry_len;
54452c9ce25SScott Long 		struct scsi_inquiry_data *inq_buf =
54552c9ce25SScott Long 		    &periph->path->device->inq_data;
54652c9ce25SScott Long 
54752c9ce25SScott Long 		if (softc->action == PROBE_INQUIRY)
54852c9ce25SScott Long 			inquiry_len = SHORT_INQUIRY_LENGTH;
54952c9ce25SScott Long 		else
55052c9ce25SScott Long 			inquiry_len = SID_ADDITIONAL_LENGTH(inq_buf);
55152c9ce25SScott Long 		/*
55252c9ce25SScott Long 		 * Some parallel SCSI devices fail to send an
55352c9ce25SScott Long 		 * ignore wide residue message when dealing with
55452c9ce25SScott Long 		 * odd length inquiry requests.  Round up to be
55552c9ce25SScott Long 		 * safe.
55652c9ce25SScott Long 		 */
55752c9ce25SScott Long 		inquiry_len = roundup2(inquiry_len, 2);
55852c9ce25SScott Long 		scsi_inquiry(csio,
55952c9ce25SScott Long 			     /*retries*/1,
56052c9ce25SScott Long 			     probedone,
56152c9ce25SScott Long 			     MSG_SIMPLE_Q_TAG,
56252c9ce25SScott Long 			     (u_int8_t *)inq_buf,
56352c9ce25SScott Long 			     inquiry_len,
56452c9ce25SScott Long 			     /*evpd*/FALSE,
56552c9ce25SScott Long 			     /*page_code*/0,
56652c9ce25SScott Long 			     SSD_MIN_SIZE,
56752c9ce25SScott Long 			     /*timeout*/60 * 1000);
56852c9ce25SScott Long 		break;
56952c9ce25SScott Long 	}
57052c9ce25SScott Long 	case PROBE_PM_PID:
57152c9ce25SScott Long 		cam_fill_ataio(ataio,
57252c9ce25SScott Long 		      1,
57352c9ce25SScott Long 		      probedone,
57452c9ce25SScott Long 		      /*flags*/CAM_DIR_NONE,
57565d0fb03SAlexander Motin 		      0,
57652c9ce25SScott Long 		      /*data_ptr*/NULL,
57752c9ce25SScott Long 		      /*dxfer_len*/0,
57852c9ce25SScott Long 		      10 * 1000);
57952c9ce25SScott Long 		ata_pm_read_cmd(ataio, 0, 15);
58052c9ce25SScott Long 		break;
58152c9ce25SScott Long 	case PROBE_PM_PRV:
58252c9ce25SScott Long 		cam_fill_ataio(ataio,
58352c9ce25SScott Long 		      1,
58452c9ce25SScott Long 		      probedone,
58552c9ce25SScott Long 		      /*flags*/CAM_DIR_NONE,
58665d0fb03SAlexander Motin 		      0,
58752c9ce25SScott Long 		      /*data_ptr*/NULL,
58852c9ce25SScott Long 		      /*dxfer_len*/0,
58952c9ce25SScott Long 		      10 * 1000);
59052c9ce25SScott Long 		ata_pm_read_cmd(ataio, 1, 15);
59152c9ce25SScott Long 		break;
59252c9ce25SScott Long 	case PROBE_INVALID:
5931e637ba6SAlexander Motin 		CAM_DEBUG(path, CAM_DEBUG_INFO,
59452c9ce25SScott Long 		    ("probestart: invalid action state\n"));
59552c9ce25SScott Long 	default:
59652c9ce25SScott Long 		break;
59752c9ce25SScott Long 	}
59852c9ce25SScott Long 	xpt_action(start_ccb);
59952c9ce25SScott Long }
60052c9ce25SScott Long #if 0
60152c9ce25SScott Long static void
60252c9ce25SScott Long proberequestdefaultnegotiation(struct cam_periph *periph)
60352c9ce25SScott Long {
60452c9ce25SScott Long 	struct ccb_trans_settings cts;
60552c9ce25SScott Long 
60683c5d981SAlexander Motin 	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
60752c9ce25SScott Long 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
60852c9ce25SScott Long 	cts.type = CTS_TYPE_USER_SETTINGS;
60952c9ce25SScott Long 	xpt_action((union ccb *)&cts);
61052c9ce25SScott Long 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
61152c9ce25SScott Long 		return;
61252c9ce25SScott Long 	}
61352c9ce25SScott Long 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
61452c9ce25SScott Long 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
61552c9ce25SScott Long 	xpt_action((union ccb *)&cts);
61652c9ce25SScott Long }
61752c9ce25SScott Long 
61852c9ce25SScott Long /*
61952c9ce25SScott Long  * Backoff Negotiation Code- only pertinent for SPI devices.
62052c9ce25SScott Long  */
62152c9ce25SScott Long static int
62252c9ce25SScott Long proberequestbackoff(struct cam_periph *periph, struct cam_ed *device)
62352c9ce25SScott Long {
62452c9ce25SScott Long 	struct ccb_trans_settings cts;
62552c9ce25SScott Long 	struct ccb_trans_settings_spi *spi;
62652c9ce25SScott Long 
62752c9ce25SScott Long 	memset(&cts, 0, sizeof (cts));
62883c5d981SAlexander Motin 	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
62952c9ce25SScott Long 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
63052c9ce25SScott Long 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
63152c9ce25SScott Long 	xpt_action((union ccb *)&cts);
63252c9ce25SScott Long 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
63352c9ce25SScott Long 		if (bootverbose) {
63452c9ce25SScott Long 			xpt_print(periph->path,
63552c9ce25SScott Long 			    "failed to get current device settings\n");
63652c9ce25SScott Long 		}
63752c9ce25SScott Long 		return (0);
63852c9ce25SScott Long 	}
63952c9ce25SScott Long 	if (cts.transport != XPORT_SPI) {
64052c9ce25SScott Long 		if (bootverbose) {
64152c9ce25SScott Long 			xpt_print(periph->path, "not SPI transport\n");
64252c9ce25SScott Long 		}
64352c9ce25SScott Long 		return (0);
64452c9ce25SScott Long 	}
64552c9ce25SScott Long 	spi = &cts.xport_specific.spi;
64652c9ce25SScott Long 
64752c9ce25SScott Long 	/*
64852c9ce25SScott Long 	 * We cannot renegotiate sync rate if we don't have one.
64952c9ce25SScott Long 	 */
65052c9ce25SScott Long 	if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
65152c9ce25SScott Long 		if (bootverbose) {
65252c9ce25SScott Long 			xpt_print(periph->path, "no sync rate known\n");
65352c9ce25SScott Long 		}
65452c9ce25SScott Long 		return (0);
65552c9ce25SScott Long 	}
65652c9ce25SScott Long 
65752c9ce25SScott Long 	/*
65852c9ce25SScott Long 	 * We'll assert that we don't have to touch PPR options- the
65952c9ce25SScott Long 	 * SIM will see what we do with period and offset and adjust
66052c9ce25SScott Long 	 * the PPR options as appropriate.
66152c9ce25SScott Long 	 */
66252c9ce25SScott Long 
66352c9ce25SScott Long 	/*
66452c9ce25SScott Long 	 * A sync rate with unknown or zero offset is nonsensical.
66552c9ce25SScott Long 	 * A sync period of zero means Async.
66652c9ce25SScott Long 	 */
66752c9ce25SScott Long 	if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0
66852c9ce25SScott Long 	 || spi->sync_offset == 0 || spi->sync_period == 0) {
66952c9ce25SScott Long 		if (bootverbose) {
67052c9ce25SScott Long 			xpt_print(periph->path, "no sync rate available\n");
67152c9ce25SScott Long 		}
67252c9ce25SScott Long 		return (0);
67352c9ce25SScott Long 	}
67452c9ce25SScott Long 
67552c9ce25SScott Long 	if (device->flags & CAM_DEV_DV_HIT_BOTTOM) {
67652c9ce25SScott Long 		CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
67752c9ce25SScott Long 		    ("hit async: giving up on DV\n"));
67852c9ce25SScott Long 		return (0);
67952c9ce25SScott Long 	}
68052c9ce25SScott Long 
68152c9ce25SScott Long 
68252c9ce25SScott Long 	/*
68352c9ce25SScott Long 	 * Jump sync_period up by one, but stop at 5MHz and fall back to Async.
68452c9ce25SScott Long 	 * We don't try to remember 'last' settings to see if the SIM actually
68552c9ce25SScott Long 	 * gets into the speed we want to set. We check on the SIM telling
68652c9ce25SScott Long 	 * us that a requested speed is bad, but otherwise don't try and
68752c9ce25SScott Long 	 * check the speed due to the asynchronous and handshake nature
68852c9ce25SScott Long 	 * of speed setting.
68952c9ce25SScott Long 	 */
69052c9ce25SScott Long 	spi->valid = CTS_SPI_VALID_SYNC_RATE | CTS_SPI_VALID_SYNC_OFFSET;
69152c9ce25SScott Long 	for (;;) {
69252c9ce25SScott Long 		spi->sync_period++;
69352c9ce25SScott Long 		if (spi->sync_period >= 0xf) {
69452c9ce25SScott Long 			spi->sync_period = 0;
69552c9ce25SScott Long 			spi->sync_offset = 0;
69652c9ce25SScott Long 			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
69752c9ce25SScott Long 			    ("setting to async for DV\n"));
69852c9ce25SScott Long 			/*
69952c9ce25SScott Long 			 * Once we hit async, we don't want to try
70052c9ce25SScott Long 			 * any more settings.
70152c9ce25SScott Long 			 */
70252c9ce25SScott Long 			device->flags |= CAM_DEV_DV_HIT_BOTTOM;
70352c9ce25SScott Long 		} else if (bootverbose) {
70452c9ce25SScott Long 			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
70552c9ce25SScott Long 			    ("DV: period 0x%x\n", spi->sync_period));
70652c9ce25SScott Long 			printf("setting period to 0x%x\n", spi->sync_period);
70752c9ce25SScott Long 		}
70852c9ce25SScott Long 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
70952c9ce25SScott Long 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
71052c9ce25SScott Long 		xpt_action((union ccb *)&cts);
71152c9ce25SScott Long 		if ((cts.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
71252c9ce25SScott Long 			break;
71352c9ce25SScott Long 		}
71452c9ce25SScott Long 		CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
71552c9ce25SScott Long 		    ("DV: failed to set period 0x%x\n", spi->sync_period));
71652c9ce25SScott Long 		if (spi->sync_period == 0) {
71752c9ce25SScott Long 			return (0);
71852c9ce25SScott Long 		}
71952c9ce25SScott Long 	}
72052c9ce25SScott Long 	return (1);
72152c9ce25SScott Long }
72252c9ce25SScott Long #endif
72352c9ce25SScott Long static void
72452c9ce25SScott Long probedone(struct cam_periph *periph, union ccb *done_ccb)
72552c9ce25SScott Long {
726c8039fc6SAlexander Motin 	struct ccb_trans_settings cts;
72752c9ce25SScott Long 	struct ata_params *ident_buf;
72852c9ce25SScott Long 	probe_softc *softc;
72952c9ce25SScott Long 	struct cam_path *path;
73026bdaeddSAlexander Motin 	cam_status status;
73152c9ce25SScott Long 	u_int32_t  priority;
732da6808c1SAlexander Motin 	u_int caps;
73365d0fb03SAlexander Motin 	int found = 1;
73452c9ce25SScott Long 
73552c9ce25SScott Long 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
73652c9ce25SScott Long 
73752c9ce25SScott Long 	softc = (probe_softc *)periph->softc;
73852c9ce25SScott Long 	path = done_ccb->ccb_h.path;
73952c9ce25SScott Long 	priority = done_ccb->ccb_h.pinfo.priority;
74052c9ce25SScott Long 	ident_buf = &path->device->ident_data;
74152c9ce25SScott Long 
7421e637ba6SAlexander Motin 	if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
74325a519a9SAlexander Motin 		if (softc->restart) {
74425a519a9SAlexander Motin 			if (bootverbose) {
74525a519a9SAlexander Motin 				cam_error_print(done_ccb,
74625a519a9SAlexander Motin 				    CAM_ESF_ALL, CAM_EPF_ALL);
74725a519a9SAlexander Motin 			}
74825a519a9SAlexander Motin 		} else if (cam_periph_error(done_ccb, 0, 0, NULL) == ERESTART)
7491e637ba6SAlexander Motin 			return;
75025a519a9SAlexander Motin 		if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
7511e637ba6SAlexander Motin 			/* Don't wedge the queue */
7521e637ba6SAlexander Motin 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
7531e637ba6SAlexander Motin 					 /*run_queue*/TRUE);
7541e637ba6SAlexander Motin 		}
75526bdaeddSAlexander Motin 		status = done_ccb->ccb_h.status & CAM_STATUS_MASK;
75625a519a9SAlexander Motin 		if (softc->restart) {
75725a519a9SAlexander Motin 			softc->faults++;
75825a519a9SAlexander Motin 			if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) ==
75925a519a9SAlexander Motin 			    CAM_CMD_TIMEOUT)
76025a519a9SAlexander Motin 				softc->faults += 4;
76125a519a9SAlexander Motin 			if (softc->faults < 10)
76225a519a9SAlexander Motin 				goto done;
76325a519a9SAlexander Motin 			else
76425a519a9SAlexander Motin 				softc->restart = 0;
76526bdaeddSAlexander Motin 
7661e637ba6SAlexander Motin 		/* Old PIO2 devices may not support mode setting. */
76726bdaeddSAlexander Motin 		} else if (softc->action == PROBE_SETMODE &&
76826bdaeddSAlexander Motin 		    status == CAM_ATA_STATUS_ERROR &&
7691e637ba6SAlexander Motin 		    ata_max_pmode(ident_buf) <= ATA_PIO2 &&
77026bdaeddSAlexander Motin 		    (ident_buf->capabilities1 & ATA_SUPPORT_IORDY) == 0) {
7711e637ba6SAlexander Motin 			goto noerror;
77226bdaeddSAlexander Motin 
77326bdaeddSAlexander Motin 		/*
77426bdaeddSAlexander Motin 		 * Some old WD SATA disks report supported and enabled
77526bdaeddSAlexander Motin 		 * device-initiated interface power management, but return
77626bdaeddSAlexander Motin 		 * ABORT on attempt to disable it.
77726bdaeddSAlexander Motin 		 */
77826bdaeddSAlexander Motin 		} else if (softc->action == PROBE_SETPM &&
77926bdaeddSAlexander Motin 		    status == CAM_ATA_STATUS_ERROR) {
78026bdaeddSAlexander Motin 			goto noerror;
781025e2c12SAlexander Motin 
782025e2c12SAlexander Motin 		/*
783025e2c12SAlexander Motin 		 * Some HP SATA disks report supported DMA Auto-Activation,
784025e2c12SAlexander Motin 		 * but return ABORT on attempt to enable it.
785025e2c12SAlexander Motin 		 */
786025e2c12SAlexander Motin 		} else if (softc->action == PROBE_SETDMAAA &&
787025e2c12SAlexander Motin 		    status == CAM_ATA_STATUS_ERROR) {
788025e2c12SAlexander Motin 			goto noerror;
78926bdaeddSAlexander Motin 		}
79026bdaeddSAlexander Motin 
7911e637ba6SAlexander Motin 		/*
7921e637ba6SAlexander Motin 		 * If we get to this point, we got an error status back
7931e637ba6SAlexander Motin 		 * from the inquiry and the error status doesn't require
7941e637ba6SAlexander Motin 		 * automatically retrying the command.  Therefore, the
7951e637ba6SAlexander Motin 		 * inquiry failed.  If we had inquiry information before
7961e637ba6SAlexander Motin 		 * for this device, but this latest inquiry command failed,
7971e637ba6SAlexander Motin 		 * the device has probably gone away.  If this device isn't
7981e637ba6SAlexander Motin 		 * already marked unconfigured, notify the peripheral
7991e637ba6SAlexander Motin 		 * drivers that this device is no more.
8001e637ba6SAlexander Motin 		 */
80125a519a9SAlexander Motin device_fail:	if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
8021e637ba6SAlexander Motin 			xpt_async(AC_LOST_DEVICE, path, NULL);
8031e637ba6SAlexander Motin 		found = 0;
8041e637ba6SAlexander Motin 		goto done;
8051e637ba6SAlexander Motin 	}
8061e637ba6SAlexander Motin noerror:
807a9b8edb1SAlexander Motin 	if (softc->restart)
808a9b8edb1SAlexander Motin 		goto done;
80952c9ce25SScott Long 	switch (softc->action) {
81052c9ce25SScott Long 	case PROBE_RESET:
8111e637ba6SAlexander Motin 	{
81252c9ce25SScott Long 		int sign = (done_ccb->ataio.res.lba_high << 8) +
81352c9ce25SScott Long 		    done_ccb->ataio.res.lba_mid;
8147bdb664eSAlexander Motin 		if (bootverbose)
81552c9ce25SScott Long 			xpt_print(path, "SIGNATURE: %04x\n", sign);
81652c9ce25SScott Long 		if (sign == 0x0000 &&
81752c9ce25SScott Long 		    done_ccb->ccb_h.target_id != 15) {
81852c9ce25SScott Long 			path->device->protocol = PROTO_ATA;
81952c9ce25SScott Long 			PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
82052c9ce25SScott Long 		} else if (sign == 0x9669 &&
82152c9ce25SScott Long 		    done_ccb->ccb_h.target_id == 15) {
82252c9ce25SScott Long 			/* Report SIM that PM is present. */
82352c9ce25SScott Long 			bzero(&cts, sizeof(cts));
82483c5d981SAlexander Motin 			xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
82552c9ce25SScott Long 			cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
82652c9ce25SScott Long 			cts.type = CTS_TYPE_CURRENT_SETTINGS;
82752c9ce25SScott Long 			cts.xport_specific.sata.pm_present = 1;
82852c9ce25SScott Long 			cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
82952c9ce25SScott Long 			xpt_action((union ccb *)&cts);
83052c9ce25SScott Long 			path->device->protocol = PROTO_SATAPM;
83152c9ce25SScott Long 			PROBE_SET_ACTION(softc, PROBE_PM_PID);
83252c9ce25SScott Long 		} else if (sign == 0xeb14 &&
83352c9ce25SScott Long 		    done_ccb->ccb_h.target_id != 15) {
83452c9ce25SScott Long 			path->device->protocol = PROTO_SCSI;
83552c9ce25SScott Long 			PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
83652c9ce25SScott Long 		} else {
83752c9ce25SScott Long 			if (done_ccb->ccb_h.target_id != 15) {
83852c9ce25SScott Long 				xpt_print(path,
83952c9ce25SScott Long 				    "Unexpected signature 0x%04x\n", sign);
84052c9ce25SScott Long 			}
84165d0fb03SAlexander Motin 			goto device_fail;
84252c9ce25SScott Long 		}
84352c9ce25SScott Long 		xpt_release_ccb(done_ccb);
84452c9ce25SScott Long 		xpt_schedule(periph, priority);
84552c9ce25SScott Long 		return;
84652c9ce25SScott Long 	}
84752c9ce25SScott Long 	case PROBE_IDENTIFY:
84852c9ce25SScott Long 	{
849699f853bSAlexander Motin 		struct ccb_pathinq cpi;
85052c9ce25SScott Long 		int16_t *ptr;
8512eb4a8dcSAlexander Motin 		int changed = 1;
85252c9ce25SScott Long 
853a9b8edb1SAlexander Motin 		ident_buf = &softc->ident_data;
85452c9ce25SScott Long 		for (ptr = (int16_t *)ident_buf;
85552c9ce25SScott Long 		     ptr < (int16_t *)ident_buf + sizeof(struct ata_params)/2; ptr++) {
85652c9ce25SScott Long 			*ptr = le16toh(*ptr);
85752c9ce25SScott Long 		}
85852c9ce25SScott Long 		if (strncmp(ident_buf->model, "FX", 2) &&
85952c9ce25SScott Long 		    strncmp(ident_buf->model, "NEC", 3) &&
86052c9ce25SScott Long 		    strncmp(ident_buf->model, "Pioneer", 7) &&
86152c9ce25SScott Long 		    strncmp(ident_buf->model, "SHARP", 5)) {
86252c9ce25SScott Long 			ata_bswap(ident_buf->model, sizeof(ident_buf->model));
86352c9ce25SScott Long 			ata_bswap(ident_buf->revision, sizeof(ident_buf->revision));
86452c9ce25SScott Long 			ata_bswap(ident_buf->serial, sizeof(ident_buf->serial));
86552c9ce25SScott Long 		}
86652c9ce25SScott Long 		ata_btrim(ident_buf->model, sizeof(ident_buf->model));
86752c9ce25SScott Long 		ata_bpack(ident_buf->model, ident_buf->model, sizeof(ident_buf->model));
86852c9ce25SScott Long 		ata_btrim(ident_buf->revision, sizeof(ident_buf->revision));
86952c9ce25SScott Long 		ata_bpack(ident_buf->revision, ident_buf->revision, sizeof(ident_buf->revision));
87052c9ce25SScott Long 		ata_btrim(ident_buf->serial, sizeof(ident_buf->serial));
87152c9ce25SScott Long 		ata_bpack(ident_buf->serial, ident_buf->serial, sizeof(ident_buf->serial));
8724ef08dc5SAlexander Motin 		/* Device may need spin-up before IDENTIFY become valid. */
8731254b680SAlexander Motin 		if ((ident_buf->specconf == 0x37c8 ||
8741254b680SAlexander Motin 		     ident_buf->specconf == 0x738c) &&
8751254b680SAlexander Motin 		    ((ident_buf->config & ATA_RESP_INCOMPLETE) ||
8764ef08dc5SAlexander Motin 		     softc->spinup == 0)) {
8774ef08dc5SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_SPINUP);
8784ef08dc5SAlexander Motin 			xpt_release_ccb(done_ccb);
8794ef08dc5SAlexander Motin 			xpt_schedule(periph, priority);
8804ef08dc5SAlexander Motin 			return;
8814ef08dc5SAlexander Motin 		}
882a9b8edb1SAlexander Motin 		ident_buf = &path->device->ident_data;
88352c9ce25SScott Long 		if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
88452c9ce25SScott Long 			/* Check that it is the same device. */
885a9b8edb1SAlexander Motin 			if (bcmp(softc->ident_data.model, ident_buf->model,
886a9b8edb1SAlexander Motin 			     sizeof(ident_buf->model)) ||
887a9b8edb1SAlexander Motin 			    bcmp(softc->ident_data.revision, ident_buf->revision,
888a9b8edb1SAlexander Motin 			     sizeof(ident_buf->revision)) ||
889a9b8edb1SAlexander Motin 			    bcmp(softc->ident_data.serial, ident_buf->serial,
890a9b8edb1SAlexander Motin 			     sizeof(ident_buf->serial))) {
89152c9ce25SScott Long 				/* Device changed. */
89252c9ce25SScott Long 				xpt_async(AC_LOST_DEVICE, path, NULL);
8931e637ba6SAlexander Motin 			} else {
894a9b8edb1SAlexander Motin 				bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
8952eb4a8dcSAlexander Motin 				changed = 0;
8962eb4a8dcSAlexander Motin 			}
8972eb4a8dcSAlexander Motin 		}
8982eb4a8dcSAlexander Motin 		if (changed) {
8992eb4a8dcSAlexander Motin 			bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
90052c9ce25SScott Long 			/* Clean up from previous instance of this device */
90152c9ce25SScott Long 			if (path->device->serial_num != NULL) {
90252c9ce25SScott Long 				free(path->device->serial_num, M_CAMXPT);
90352c9ce25SScott Long 				path->device->serial_num = NULL;
90452c9ce25SScott Long 				path->device->serial_num_len = 0;
90552c9ce25SScott Long 			}
90652c9ce25SScott Long 			path->device->serial_num =
90752c9ce25SScott Long 				(u_int8_t *)malloc((sizeof(ident_buf->serial) + 1),
90852c9ce25SScott Long 					   M_CAMXPT, M_NOWAIT);
90952c9ce25SScott Long 			if (path->device->serial_num != NULL) {
91052c9ce25SScott Long 				bcopy(ident_buf->serial,
91152c9ce25SScott Long 				      path->device->serial_num,
91252c9ce25SScott Long 				      sizeof(ident_buf->serial));
91352c9ce25SScott Long 				path->device->serial_num[sizeof(ident_buf->serial)]
91452c9ce25SScott Long 				    = '\0';
91552c9ce25SScott Long 				path->device->serial_num_len =
91652c9ce25SScott Long 				    strlen(path->device->serial_num);
91752c9ce25SScott Long 			}
91852c9ce25SScott Long 
9194b997c49SAlexander Motin 			path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
9201e637ba6SAlexander Motin 		}
92130a4094fSAlexander Motin 		if (ident_buf->satacapabilities & ATA_SUPPORT_NCQ) {
92230a4094fSAlexander Motin 			path->device->mintags = path->device->maxtags =
92330a4094fSAlexander Motin 			    ATA_QUEUE_LEN(ident_buf->queue) + 1;
92430a4094fSAlexander Motin 		}
92530a4094fSAlexander Motin 		ata_find_quirk(path->device);
926507e5811SAlexander Motin 		if (path->device->mintags != 0 &&
927507e5811SAlexander Motin 		    path->bus->sim->max_tagged_dev_openings != 0) {
928699f853bSAlexander Motin 			/* Check if the SIM does not want queued commands. */
929699f853bSAlexander Motin 			bzero(&cpi, sizeof(cpi));
930699f853bSAlexander Motin 			xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
931699f853bSAlexander Motin 			cpi.ccb_h.func_code = XPT_PATH_INQ;
932699f853bSAlexander Motin 			xpt_action((union ccb *)&cpi);
933699f853bSAlexander Motin 			if (cpi.ccb_h.status == CAM_REQ_CMP &&
934699f853bSAlexander Motin 			    (cpi.hba_inquiry & PI_TAG_ABLE)) {
935c8039fc6SAlexander Motin 				/* Report SIM which tags are allowed. */
936c8039fc6SAlexander Motin 				bzero(&cts, sizeof(cts));
93783c5d981SAlexander Motin 				xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
938c8039fc6SAlexander Motin 				cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
939c8039fc6SAlexander Motin 				cts.type = CTS_TYPE_CURRENT_SETTINGS;
940c8039fc6SAlexander Motin 				cts.xport_specific.sata.tags = path->device->maxtags;
941c8039fc6SAlexander Motin 				cts.xport_specific.sata.valid = CTS_SATA_VALID_TAGS;
942c8039fc6SAlexander Motin 				xpt_action((union ccb *)&cts);
943c8039fc6SAlexander Motin 				/* Reconfigure queues for tagged queueing. */
94430a4094fSAlexander Motin 				xpt_start_tags(path);
94530a4094fSAlexander Motin 			}
946699f853bSAlexander Motin 		}
94752c9ce25SScott Long 		ata_device_transport(path);
94852c9ce25SScott Long 		PROBE_SET_ACTION(softc, PROBE_SETMODE);
94952c9ce25SScott Long 		xpt_release_ccb(done_ccb);
95052c9ce25SScott Long 		xpt_schedule(periph, priority);
95152c9ce25SScott Long 		return;
95252c9ce25SScott Long 	}
9534ef08dc5SAlexander Motin 	case PROBE_SPINUP:
9544ef08dc5SAlexander Motin 		if (bootverbose)
9554ef08dc5SAlexander Motin 			xpt_print(path, "Spin-up done\n");
9564ef08dc5SAlexander Motin 		softc->spinup = 1;
9574ef08dc5SAlexander Motin 		PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
9584ef08dc5SAlexander Motin 		xpt_release_ccb(done_ccb);
9594ef08dc5SAlexander Motin 		xpt_schedule(periph, priority);
9604ef08dc5SAlexander Motin 		return;
96152c9ce25SScott Long 	case PROBE_SETMODE:
962da6808c1SAlexander Motin 		if (path->device->transport != XPORT_SATA)
963da6808c1SAlexander Motin 			goto notsata;
964da6808c1SAlexander Motin 		/* Set supported bits. */
965da6808c1SAlexander Motin 		bzero(&cts, sizeof(cts));
966da6808c1SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
967da6808c1SAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
968da6808c1SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
969da6808c1SAlexander Motin 		xpt_action((union ccb *)&cts);
970da6808c1SAlexander Motin 		if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
971da6808c1SAlexander Motin 			caps = cts.xport_specific.sata.caps & CTS_SATA_CAPS_H;
972da6808c1SAlexander Motin 		else
973da6808c1SAlexander Motin 			caps = 0;
974da6808c1SAlexander Motin 		if (ident_buf->satacapabilities != 0xffff) {
975da6808c1SAlexander Motin 			if (ident_buf->satacapabilities & ATA_SUPPORT_IFPWRMNGTRCV)
976da6808c1SAlexander Motin 				caps |= CTS_SATA_CAPS_D_PMREQ;
977da6808c1SAlexander Motin 			if (ident_buf->satacapabilities & ATA_SUPPORT_HAPST)
978da6808c1SAlexander Motin 				caps |= CTS_SATA_CAPS_D_APST;
979da6808c1SAlexander Motin 		}
980da6808c1SAlexander Motin 		/* Mask unwanted bits. */
981da6808c1SAlexander Motin 		bzero(&cts, sizeof(cts));
982da6808c1SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
983da6808c1SAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
984da6808c1SAlexander Motin 		cts.type = CTS_TYPE_USER_SETTINGS;
985da6808c1SAlexander Motin 		xpt_action((union ccb *)&cts);
986da6808c1SAlexander Motin 		if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
987da6808c1SAlexander Motin 			caps &= cts.xport_specific.sata.caps;
988c2b82f3eSAlexander Motin 		else
989c2b82f3eSAlexander Motin 			caps = 0;
990da6808c1SAlexander Motin 		/* Store result to SIM. */
991da6808c1SAlexander Motin 		bzero(&cts, sizeof(cts));
992da6808c1SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
993da6808c1SAlexander Motin 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
994da6808c1SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
995da6808c1SAlexander Motin 		cts.xport_specific.sata.caps = caps;
996da6808c1SAlexander Motin 		cts.xport_specific.sata.valid = CTS_SATA_VALID_CAPS;
997da6808c1SAlexander Motin 		xpt_action((union ccb *)&cts);
998da6808c1SAlexander Motin 		softc->caps = caps;
999958e4a69SAlexander Motin 		if ((ident_buf->satasupport & ATA_SUPPORT_IFPWRMNGT) &&
1000958e4a69SAlexander Motin 		    (!(softc->caps & CTS_SATA_CAPS_H_PMREQ)) !=
1001958e4a69SAlexander Motin 		    (!(ident_buf->sataenabled & ATA_SUPPORT_IFPWRMNGT))) {
1002da6808c1SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_SETPM);
1003da6808c1SAlexander Motin 			xpt_release_ccb(done_ccb);
1004da6808c1SAlexander Motin 			xpt_schedule(periph, priority);
1005da6808c1SAlexander Motin 			return;
1006da6808c1SAlexander Motin 		}
1007da6808c1SAlexander Motin 		/* FALLTHROUGH */
1008da6808c1SAlexander Motin 	case PROBE_SETPM:
1009da6808c1SAlexander Motin 		if (ident_buf->satacapabilities != 0xffff &&
1010958e4a69SAlexander Motin 		    (ident_buf->satacapabilities & ATA_SUPPORT_DAPST) &&
1011958e4a69SAlexander Motin 		    (!(softc->caps & CTS_SATA_CAPS_H_APST)) !=
1012958e4a69SAlexander Motin 		    (!(ident_buf->sataenabled & ATA_ENABLED_DAPST))) {
1013da6808c1SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_SETAPST);
1014da6808c1SAlexander Motin 			xpt_release_ccb(done_ccb);
1015da6808c1SAlexander Motin 			xpt_schedule(periph, priority);
1016da6808c1SAlexander Motin 			return;
1017da6808c1SAlexander Motin 		}
1018da6808c1SAlexander Motin 		/* FALLTHROUGH */
1019da6808c1SAlexander Motin 	case PROBE_SETAPST:
1020958e4a69SAlexander Motin 		if ((ident_buf->satasupport & ATA_SUPPORT_AUTOACTIVATE) &&
1021958e4a69SAlexander Motin 		    (!(softc->caps & CTS_SATA_CAPS_H_DMAAA)) !=
1022958e4a69SAlexander Motin 		    (!(ident_buf->sataenabled & ATA_SUPPORT_AUTOACTIVATE))) {
1023da6808c1SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_SETDMAAA);
1024da6808c1SAlexander Motin 			xpt_release_ccb(done_ccb);
1025da6808c1SAlexander Motin 			xpt_schedule(periph, priority);
1026da6808c1SAlexander Motin 			return;
1027da6808c1SAlexander Motin 		}
1028da6808c1SAlexander Motin 		/* FALLTHROUGH */
1029da6808c1SAlexander Motin 	case PROBE_SETDMAAA:
1030da6808c1SAlexander Motin notsata:
10311e637ba6SAlexander Motin 		if (path->device->protocol == PROTO_ATA) {
10321e637ba6SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_SET_MULTI);
10331e637ba6SAlexander Motin 		} else {
10341e637ba6SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_INQUIRY);
10351e637ba6SAlexander Motin 		}
10361e637ba6SAlexander Motin 		xpt_release_ccb(done_ccb);
10371e637ba6SAlexander Motin 		xpt_schedule(periph, priority);
10381e637ba6SAlexander Motin 		return;
10391e637ba6SAlexander Motin 	case PROBE_SET_MULTI:
10401e637ba6SAlexander Motin 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
104152c9ce25SScott Long 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1042f98d7a47SAlexander Motin 			xpt_acquire_device(path->device);
104352c9ce25SScott Long 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
104452c9ce25SScott Long 			xpt_action(done_ccb);
104552c9ce25SScott Long 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
104652c9ce25SScott Long 			    done_ccb);
10471e637ba6SAlexander Motin 		}
104852c9ce25SScott Long 		break;
104952c9ce25SScott Long 	case PROBE_INQUIRY:
105052c9ce25SScott Long 	case PROBE_FULL_INQUIRY:
105152c9ce25SScott Long 	{
105252c9ce25SScott Long 		struct scsi_inquiry_data *inq_buf;
10531e637ba6SAlexander Motin 		u_int8_t periph_qual, len;
105452c9ce25SScott Long 
105552c9ce25SScott Long 		path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
105652c9ce25SScott Long 		inq_buf = &path->device->inq_data;
105752c9ce25SScott Long 
105852c9ce25SScott Long 		periph_qual = SID_QUAL(inq_buf);
105952c9ce25SScott Long 
10601e637ba6SAlexander Motin 		if (periph_qual != SID_QUAL_LU_CONNECTED)
10611e637ba6SAlexander Motin 			break;
106252c9ce25SScott Long 
106352c9ce25SScott Long 		/*
106452c9ce25SScott Long 		 * We conservatively request only
106552c9ce25SScott Long 		 * SHORT_INQUIRY_LEN bytes of inquiry
106652c9ce25SScott Long 		 * information during our first try
106752c9ce25SScott Long 		 * at sending an INQUIRY. If the device
106852c9ce25SScott Long 		 * has more information to give,
106952c9ce25SScott Long 		 * perform a second request specifying
107052c9ce25SScott Long 		 * the amount of information the device
107152c9ce25SScott Long 		 * is willing to give.
107252c9ce25SScott Long 		 */
107352c9ce25SScott Long 		len = inq_buf->additional_length
10741e637ba6SAlexander Motin 		    + offsetof(struct scsi_inquiry_data, additional_length) + 1;
107552c9ce25SScott Long 		if (softc->action == PROBE_INQUIRY
107652c9ce25SScott Long 		    && len > SHORT_INQUIRY_LENGTH) {
107752c9ce25SScott Long 			PROBE_SET_ACTION(softc, PROBE_FULL_INQUIRY);
107852c9ce25SScott Long 			xpt_release_ccb(done_ccb);
107952c9ce25SScott Long 			xpt_schedule(periph, priority);
108052c9ce25SScott Long 			return;
108152c9ce25SScott Long 		}
108252c9ce25SScott Long 
10834b997c49SAlexander Motin 		ata_device_transport(path);
10841e637ba6SAlexander Motin 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
108552c9ce25SScott Long 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1086f98d7a47SAlexander Motin 			xpt_acquire_device(path->device);
108752c9ce25SScott Long 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
108852c9ce25SScott Long 			xpt_action(done_ccb);
10891e637ba6SAlexander Motin 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path, done_ccb);
10901e637ba6SAlexander Motin 		}
109152c9ce25SScott Long 		break;
109252c9ce25SScott Long 	}
109352c9ce25SScott Long 	case PROBE_PM_PID:
10944b997c49SAlexander Motin 		if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) == 0)
109552c9ce25SScott Long 			bzero(ident_buf, sizeof(*ident_buf));
109652c9ce25SScott Long 		softc->pm_pid = (done_ccb->ataio.res.lba_high << 24) +
109752c9ce25SScott Long 		    (done_ccb->ataio.res.lba_mid << 16) +
109852c9ce25SScott Long 		    (done_ccb->ataio.res.lba_low << 8) +
109952c9ce25SScott Long 		    done_ccb->ataio.res.sector_count;
110065d0fb03SAlexander Motin 		((uint32_t *)ident_buf)[0] = softc->pm_pid;
110152c9ce25SScott Long 		snprintf(ident_buf->model, sizeof(ident_buf->model),
110252c9ce25SScott Long 		    "Port Multiplier %08x", softc->pm_pid);
110352c9ce25SScott Long 		PROBE_SET_ACTION(softc, PROBE_PM_PRV);
110452c9ce25SScott Long 		xpt_release_ccb(done_ccb);
110552c9ce25SScott Long 		xpt_schedule(periph, priority);
110652c9ce25SScott Long 		return;
110752c9ce25SScott Long 	case PROBE_PM_PRV:
110852c9ce25SScott Long 		softc->pm_prv = (done_ccb->ataio.res.lba_high << 24) +
110952c9ce25SScott Long 		    (done_ccb->ataio.res.lba_mid << 16) +
111052c9ce25SScott Long 		    (done_ccb->ataio.res.lba_low << 8) +
111152c9ce25SScott Long 		    done_ccb->ataio.res.sector_count;
111265d0fb03SAlexander Motin 		((uint32_t *)ident_buf)[1] = softc->pm_prv;
111352c9ce25SScott Long 		snprintf(ident_buf->revision, sizeof(ident_buf->revision),
111452c9ce25SScott Long 		    "%04x", softc->pm_prv);
11154b997c49SAlexander Motin 		path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
1116da6808c1SAlexander Motin 		/* Set supported bits. */
1117da6808c1SAlexander Motin 		bzero(&cts, sizeof(cts));
1118da6808c1SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1119da6808c1SAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1120da6808c1SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
1121da6808c1SAlexander Motin 		xpt_action((union ccb *)&cts);
1122da6808c1SAlexander Motin 		if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1123da6808c1SAlexander Motin 			caps = cts.xport_specific.sata.caps & CTS_SATA_CAPS_H;
1124da6808c1SAlexander Motin 		else
1125da6808c1SAlexander Motin 			caps = 0;
1126da6808c1SAlexander Motin 		/* All PMPs must support PM requests. */
1127da6808c1SAlexander Motin 		caps |= CTS_SATA_CAPS_D_PMREQ;
1128da6808c1SAlexander Motin 		/* Mask unwanted bits. */
1129da6808c1SAlexander Motin 		bzero(&cts, sizeof(cts));
1130da6808c1SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1131da6808c1SAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1132da6808c1SAlexander Motin 		cts.type = CTS_TYPE_USER_SETTINGS;
1133da6808c1SAlexander Motin 		xpt_action((union ccb *)&cts);
1134da6808c1SAlexander Motin 		if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1135da6808c1SAlexander Motin 			caps &= cts.xport_specific.sata.caps;
1136c2b82f3eSAlexander Motin 		else
1137c2b82f3eSAlexander Motin 			caps = 0;
1138da6808c1SAlexander Motin 		/* Store result to SIM. */
1139da6808c1SAlexander Motin 		bzero(&cts, sizeof(cts));
1140da6808c1SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1141da6808c1SAlexander Motin 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1142da6808c1SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
1143da6808c1SAlexander Motin 		cts.xport_specific.sata.caps = caps;
1144da6808c1SAlexander Motin 		cts.xport_specific.sata.valid = CTS_SATA_VALID_CAPS;
1145da6808c1SAlexander Motin 		xpt_action((union ccb *)&cts);
1146da6808c1SAlexander Motin 		softc->caps = caps;
114765d0fb03SAlexander Motin 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
114852c9ce25SScott Long 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1149f98d7a47SAlexander Motin 			xpt_acquire_device(path->device);
115052c9ce25SScott Long 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
115152c9ce25SScott Long 			xpt_action(done_ccb);
115252c9ce25SScott Long 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
115352c9ce25SScott Long 			    done_ccb);
115465d0fb03SAlexander Motin 		} else {
115565d0fb03SAlexander Motin 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
115665d0fb03SAlexander Motin 			xpt_action(done_ccb);
11571e637ba6SAlexander Motin 			xpt_async(AC_SCSI_AEN, done_ccb->ccb_h.path, done_ccb);
115852c9ce25SScott Long 		}
115952c9ce25SScott Long 		break;
116052c9ce25SScott Long 	case PROBE_INVALID:
116152c9ce25SScott Long 		CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_INFO,
116252c9ce25SScott Long 		    ("probedone: invalid action state\n"));
116352c9ce25SScott Long 	default:
116452c9ce25SScott Long 		break;
116552c9ce25SScott Long 	}
11661e637ba6SAlexander Motin done:
116783c5d981SAlexander Motin 	if (softc->restart) {
116883c5d981SAlexander Motin 		softc->restart = 0;
11691e637ba6SAlexander Motin 		xpt_release_ccb(done_ccb);
117083c5d981SAlexander Motin 		probeschedule(periph);
117183c5d981SAlexander Motin 		return;
117283c5d981SAlexander Motin 	}
117383c5d981SAlexander Motin 	xpt_release_ccb(done_ccb);
117483c5d981SAlexander Motin 	while ((done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs))) {
117583c5d981SAlexander Motin 		TAILQ_REMOVE(&softc->request_ccbs,
117683c5d981SAlexander Motin 		    &done_ccb->ccb_h, periph_links.tqe);
117783c5d981SAlexander Motin 		done_ccb->ccb_h.status = found ? CAM_REQ_CMP : CAM_REQ_CMP_ERR;
117852c9ce25SScott Long 		xpt_done(done_ccb);
117983c5d981SAlexander Motin 	}
118083c5d981SAlexander Motin 	cam_release_devq(periph->path,
118183c5d981SAlexander Motin 	    RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_XPT + 1, FALSE);
118252c9ce25SScott Long 	cam_periph_invalidate(periph);
118352c9ce25SScott Long 	cam_periph_release_locked(periph);
118452c9ce25SScott Long }
118552c9ce25SScott Long 
118652c9ce25SScott Long static void
118752c9ce25SScott Long probecleanup(struct cam_periph *periph)
118852c9ce25SScott Long {
118952c9ce25SScott Long 	free(periph->softc, M_CAMXPT);
119052c9ce25SScott Long }
119152c9ce25SScott Long 
119252c9ce25SScott Long static void
119330a4094fSAlexander Motin ata_find_quirk(struct cam_ed *device)
119452c9ce25SScott Long {
119530a4094fSAlexander Motin 	struct ata_quirk_entry *quirk;
119652c9ce25SScott Long 	caddr_t	match;
119752c9ce25SScott Long 
119830a4094fSAlexander Motin 	match = cam_quirkmatch((caddr_t)&device->ident_data,
119930a4094fSAlexander Motin 			       (caddr_t)ata_quirk_table,
120030a4094fSAlexander Motin 			       ata_quirk_table_size,
120130a4094fSAlexander Motin 			       sizeof(*ata_quirk_table), ata_identify_match);
120252c9ce25SScott Long 
120352c9ce25SScott Long 	if (match == NULL)
120452c9ce25SScott Long 		panic("xpt_find_quirk: device didn't match wildcard entry!!");
120552c9ce25SScott Long 
120630a4094fSAlexander Motin 	quirk = (struct ata_quirk_entry *)match;
120752c9ce25SScott Long 	device->quirk = quirk;
120830a4094fSAlexander Motin 	if (quirk->quirks & CAM_QUIRK_MAXTAGS)
120930a4094fSAlexander Motin 		device->mintags = device->maxtags = quirk->maxtags;
121052c9ce25SScott Long }
121152c9ce25SScott Long 
121252c9ce25SScott Long typedef struct {
121352c9ce25SScott Long 	union	ccb *request_ccb;
121452c9ce25SScott Long 	struct 	ccb_pathinq *cpi;
121552c9ce25SScott Long 	int	counter;
121652c9ce25SScott Long } ata_scan_bus_info;
121752c9ce25SScott Long 
121852c9ce25SScott Long /*
121952c9ce25SScott Long  * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
122052c9ce25SScott Long  * As the scan progresses, xpt_scan_bus is used as the
122152c9ce25SScott Long  * callback on completion function.
122252c9ce25SScott Long  */
122352c9ce25SScott Long static void
122452c9ce25SScott Long ata_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
122552c9ce25SScott Long {
122652c9ce25SScott Long 	struct	cam_path *path;
122752c9ce25SScott Long 	ata_scan_bus_info *scan_info;
122883c5d981SAlexander Motin 	union	ccb *work_ccb, *reset_ccb;
122952c9ce25SScott Long 	cam_status status;
123052c9ce25SScott Long 
123152c9ce25SScott Long 	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
123252c9ce25SScott Long 		  ("xpt_scan_bus\n"));
123352c9ce25SScott Long 	switch (request_ccb->ccb_h.func_code) {
123452c9ce25SScott Long 	case XPT_SCAN_BUS:
12350e85f214SMatt Jacob 	case XPT_SCAN_TGT:
123652c9ce25SScott Long 		/* Find out the characteristics of the bus */
123752c9ce25SScott Long 		work_ccb = xpt_alloc_ccb_nowait();
123852c9ce25SScott Long 		if (work_ccb == NULL) {
123952c9ce25SScott Long 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
124052c9ce25SScott Long 			xpt_done(request_ccb);
124152c9ce25SScott Long 			return;
124252c9ce25SScott Long 		}
124352c9ce25SScott Long 		xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
124452c9ce25SScott Long 			      request_ccb->ccb_h.pinfo.priority);
124552c9ce25SScott Long 		work_ccb->ccb_h.func_code = XPT_PATH_INQ;
124652c9ce25SScott Long 		xpt_action(work_ccb);
124752c9ce25SScott Long 		if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
124852c9ce25SScott Long 			request_ccb->ccb_h.status = work_ccb->ccb_h.status;
124952c9ce25SScott Long 			xpt_free_ccb(work_ccb);
125052c9ce25SScott Long 			xpt_done(request_ccb);
125152c9ce25SScott Long 			return;
125252c9ce25SScott Long 		}
125352c9ce25SScott Long 
125483c5d981SAlexander Motin 		/* We may need to reset bus first, if we haven't done it yet. */
125583c5d981SAlexander Motin 		if ((work_ccb->cpi.hba_inquiry &
125683c5d981SAlexander Motin 		    (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) &&
125783c5d981SAlexander Motin 		    !(work_ccb->cpi.hba_misc & PIM_NOBUSRESET) &&
125883c5d981SAlexander Motin 		    !timevalisset(&request_ccb->ccb_h.path->bus->last_reset)) {
125983c5d981SAlexander Motin 			reset_ccb = xpt_alloc_ccb_nowait();
1260f1893540SAlexander Motin 			if (reset_ccb == NULL) {
1261f1893540SAlexander Motin 				request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1262f1893540SAlexander Motin 				xpt_free_ccb(work_ccb);
1263f1893540SAlexander Motin 				xpt_done(request_ccb);
1264f1893540SAlexander Motin 				return;
1265f1893540SAlexander Motin 			}
126683c5d981SAlexander Motin 			xpt_setup_ccb(&reset_ccb->ccb_h, request_ccb->ccb_h.path,
126783c5d981SAlexander Motin 			      CAM_PRIORITY_NONE);
126883c5d981SAlexander Motin 			reset_ccb->ccb_h.func_code = XPT_RESET_BUS;
126983c5d981SAlexander Motin 			xpt_action(reset_ccb);
127083c5d981SAlexander Motin 			if (reset_ccb->ccb_h.status != CAM_REQ_CMP) {
127183c5d981SAlexander Motin 				request_ccb->ccb_h.status = reset_ccb->ccb_h.status;
127283c5d981SAlexander Motin 				xpt_free_ccb(reset_ccb);
127383c5d981SAlexander Motin 				xpt_free_ccb(work_ccb);
127483c5d981SAlexander Motin 				xpt_done(request_ccb);
127583c5d981SAlexander Motin 				return;
127683c5d981SAlexander Motin 			}
127783c5d981SAlexander Motin 			xpt_free_ccb(reset_ccb);
127883c5d981SAlexander Motin 		}
127983c5d981SAlexander Motin 
128052c9ce25SScott Long 		/* Save some state for use while we probe for devices */
128152c9ce25SScott Long 		scan_info = (ata_scan_bus_info *)
128252c9ce25SScott Long 		    malloc(sizeof(ata_scan_bus_info), M_CAMXPT, M_NOWAIT);
128352c9ce25SScott Long 		if (scan_info == NULL) {
128452c9ce25SScott Long 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1285f1893540SAlexander Motin 			xpt_free_ccb(work_ccb);
128652c9ce25SScott Long 			xpt_done(request_ccb);
128752c9ce25SScott Long 			return;
128852c9ce25SScott Long 		}
128952c9ce25SScott Long 		scan_info->request_ccb = request_ccb;
129052c9ce25SScott Long 		scan_info->cpi = &work_ccb->cpi;
129152c9ce25SScott Long 		/* If PM supported, probe it first. */
129252c9ce25SScott Long 		if (scan_info->cpi->hba_inquiry & PI_SATAPM)
12930025eb12SAlexander Motin 			scan_info->counter = scan_info->cpi->max_target;
12940025eb12SAlexander Motin 		else
12950025eb12SAlexander Motin 			scan_info->counter = 0;
129652c9ce25SScott Long 
129752c9ce25SScott Long 		work_ccb = xpt_alloc_ccb_nowait();
129852c9ce25SScott Long 		if (work_ccb == NULL) {
129952c9ce25SScott Long 			free(scan_info, M_CAMXPT);
130052c9ce25SScott Long 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
130152c9ce25SScott Long 			xpt_done(request_ccb);
130252c9ce25SScott Long 			break;
130352c9ce25SScott Long 		}
130452c9ce25SScott Long 		goto scan_next;
130552c9ce25SScott Long 	case XPT_SCAN_LUN:
130652c9ce25SScott Long 		work_ccb = request_ccb;
130752c9ce25SScott Long 		/* Reuse the same CCB to query if a device was really found */
130852c9ce25SScott Long 		scan_info = (ata_scan_bus_info *)work_ccb->ccb_h.ppriv_ptr0;
130965d0fb03SAlexander Motin 		/* If there is PMP... */
13100025eb12SAlexander Motin 		if ((scan_info->cpi->hba_inquiry & PI_SATAPM) &&
13110025eb12SAlexander Motin 		    (scan_info->counter == scan_info->cpi->max_target)) {
131283c5d981SAlexander Motin 			if (work_ccb->ccb_h.status == CAM_REQ_CMP) {
131365d0fb03SAlexander Motin 				/* everything else will be probed by it */
1314e2a75189SAlexander Motin 				/* Free the current request path- we're done with it. */
1315e2a75189SAlexander Motin 				xpt_free_path(work_ccb->ccb_h.path);
13160025eb12SAlexander Motin 				goto done;
131752c9ce25SScott Long 			} else {
131852c9ce25SScott Long 				struct ccb_trans_settings cts;
131952c9ce25SScott Long 
132052c9ce25SScott Long 				/* Report SIM that PM is absent. */
132152c9ce25SScott Long 				bzero(&cts, sizeof(cts));
132252c9ce25SScott Long 				xpt_setup_ccb(&cts.ccb_h,
1323e2a75189SAlexander Motin 				    work_ccb->ccb_h.path, CAM_PRIORITY_NONE);
132452c9ce25SScott Long 				cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
132552c9ce25SScott Long 				cts.type = CTS_TYPE_CURRENT_SETTINGS;
13263ccda2f3SAlexander Motin 				cts.xport_specific.sata.pm_present = 0;
132752c9ce25SScott Long 				cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
132852c9ce25SScott Long 				xpt_action((union ccb *)&cts);
132952c9ce25SScott Long 			}
133052c9ce25SScott Long 		}
1331e2a75189SAlexander Motin 		/* Free the current request path- we're done with it. */
1332e2a75189SAlexander Motin 		xpt_free_path(work_ccb->ccb_h.path);
13330025eb12SAlexander Motin 		if (scan_info->counter ==
13340025eb12SAlexander Motin 		    ((scan_info->cpi->hba_inquiry & PI_SATAPM) ?
13350025eb12SAlexander Motin 		    0 : scan_info->cpi->max_target)) {
13360025eb12SAlexander Motin done:
133752c9ce25SScott Long 			xpt_free_ccb(work_ccb);
133852c9ce25SScott Long 			xpt_free_ccb((union ccb *)scan_info->cpi);
133952c9ce25SScott Long 			request_ccb = scan_info->request_ccb;
134052c9ce25SScott Long 			free(scan_info, M_CAMXPT);
134152c9ce25SScott Long 			request_ccb->ccb_h.status = CAM_REQ_CMP;
134252c9ce25SScott Long 			xpt_done(request_ccb);
134352c9ce25SScott Long 			break;
134452c9ce25SScott Long 		}
13450025eb12SAlexander Motin 		/* Take next device. Wrap from max (PMP) to 0. */
13460025eb12SAlexander Motin 		scan_info->counter = (scan_info->counter + 1 ) %
13470025eb12SAlexander Motin 		    (scan_info->cpi->max_target + 1);
134852c9ce25SScott Long scan_next:
134952c9ce25SScott Long 		status = xpt_create_path(&path, xpt_periph,
135052c9ce25SScott Long 		    scan_info->request_ccb->ccb_h.path_id,
135152c9ce25SScott Long 		    scan_info->counter, 0);
135252c9ce25SScott Long 		if (status != CAM_REQ_CMP) {
135352c9ce25SScott Long 			printf("xpt_scan_bus: xpt_create_path failed"
135452c9ce25SScott Long 			    " with status %#x, bus scan halted\n",
135552c9ce25SScott Long 			    status);
135652c9ce25SScott Long 			xpt_free_ccb(work_ccb);
135752c9ce25SScott Long 			xpt_free_ccb((union ccb *)scan_info->cpi);
135852c9ce25SScott Long 			request_ccb = scan_info->request_ccb;
135952c9ce25SScott Long 			free(scan_info, M_CAMXPT);
136052c9ce25SScott Long 			request_ccb->ccb_h.status = status;
136152c9ce25SScott Long 			xpt_done(request_ccb);
136252c9ce25SScott Long 			break;
136352c9ce25SScott Long 		}
136452c9ce25SScott Long 		xpt_setup_ccb(&work_ccb->ccb_h, path,
136552c9ce25SScott Long 		    scan_info->request_ccb->ccb_h.pinfo.priority);
136652c9ce25SScott Long 		work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
136752c9ce25SScott Long 		work_ccb->ccb_h.cbfcnp = ata_scan_bus;
136852c9ce25SScott Long 		work_ccb->ccb_h.ppriv_ptr0 = scan_info;
136952c9ce25SScott Long 		work_ccb->crcn.flags = scan_info->request_ccb->crcn.flags;
137052c9ce25SScott Long 		xpt_action(work_ccb);
137152c9ce25SScott Long 		break;
137252c9ce25SScott Long 	default:
137352c9ce25SScott Long 		break;
137452c9ce25SScott Long 	}
137552c9ce25SScott Long }
137652c9ce25SScott Long 
137752c9ce25SScott Long static void
137852c9ce25SScott Long ata_scan_lun(struct cam_periph *periph, struct cam_path *path,
137952c9ce25SScott Long 	     cam_flags flags, union ccb *request_ccb)
138052c9ce25SScott Long {
138152c9ce25SScott Long 	struct ccb_pathinq cpi;
138252c9ce25SScott Long 	cam_status status;
138352c9ce25SScott Long 	struct cam_path *new_path;
138452c9ce25SScott Long 	struct cam_periph *old_periph;
138552c9ce25SScott Long 
138683c5d981SAlexander Motin 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_scan_lun\n"));
138752c9ce25SScott Long 
138883c5d981SAlexander Motin 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
138952c9ce25SScott Long 	cpi.ccb_h.func_code = XPT_PATH_INQ;
139052c9ce25SScott Long 	xpt_action((union ccb *)&cpi);
139152c9ce25SScott Long 
139252c9ce25SScott Long 	if (cpi.ccb_h.status != CAM_REQ_CMP) {
139352c9ce25SScott Long 		if (request_ccb != NULL) {
139452c9ce25SScott Long 			request_ccb->ccb_h.status = cpi.ccb_h.status;
139552c9ce25SScott Long 			xpt_done(request_ccb);
139652c9ce25SScott Long 		}
139752c9ce25SScott Long 		return;
139852c9ce25SScott Long 	}
139952c9ce25SScott Long 
140052c9ce25SScott Long 	if (request_ccb == NULL) {
140152c9ce25SScott Long 		request_ccb = malloc(sizeof(union ccb), M_CAMXPT, M_NOWAIT);
140252c9ce25SScott Long 		if (request_ccb == NULL) {
140352c9ce25SScott Long 			xpt_print(path, "xpt_scan_lun: can't allocate CCB, "
140452c9ce25SScott Long 			    "can't continue\n");
140552c9ce25SScott Long 			return;
140652c9ce25SScott Long 		}
140752c9ce25SScott Long 		new_path = malloc(sizeof(*new_path), M_CAMXPT, M_NOWAIT);
140852c9ce25SScott Long 		if (new_path == NULL) {
140952c9ce25SScott Long 			xpt_print(path, "xpt_scan_lun: can't allocate path, "
141052c9ce25SScott Long 			    "can't continue\n");
141152c9ce25SScott Long 			free(request_ccb, M_CAMXPT);
141252c9ce25SScott Long 			return;
141352c9ce25SScott Long 		}
141452c9ce25SScott Long 		status = xpt_compile_path(new_path, xpt_periph,
141552c9ce25SScott Long 					  path->bus->path_id,
141652c9ce25SScott Long 					  path->target->target_id,
141752c9ce25SScott Long 					  path->device->lun_id);
141852c9ce25SScott Long 
141952c9ce25SScott Long 		if (status != CAM_REQ_CMP) {
142052c9ce25SScott Long 			xpt_print(path, "xpt_scan_lun: can't compile path, "
142152c9ce25SScott Long 			    "can't continue\n");
142252c9ce25SScott Long 			free(request_ccb, M_CAMXPT);
142352c9ce25SScott Long 			free(new_path, M_CAMXPT);
142452c9ce25SScott Long 			return;
142552c9ce25SScott Long 		}
142683c5d981SAlexander Motin 		xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_XPT);
142752c9ce25SScott Long 		request_ccb->ccb_h.cbfcnp = xptscandone;
142852c9ce25SScott Long 		request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
142952c9ce25SScott Long 		request_ccb->crcn.flags = flags;
143052c9ce25SScott Long 	}
143152c9ce25SScott Long 
1432f09f8e3eSAlexander Motin 	if ((old_periph = cam_periph_find(path, "aprobe")) != NULL) {
143352c9ce25SScott Long 		probe_softc *softc;
143452c9ce25SScott Long 
143552c9ce25SScott Long 		softc = (probe_softc *)old_periph->softc;
143652c9ce25SScott Long 		TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
143752c9ce25SScott Long 				  periph_links.tqe);
143883c5d981SAlexander Motin 		softc->restart = 1;
143952c9ce25SScott Long 	} else {
144052c9ce25SScott Long 		status = cam_periph_alloc(proberegister, NULL, probecleanup,
1441f09f8e3eSAlexander Motin 					  probestart, "aprobe",
144252c9ce25SScott Long 					  CAM_PERIPH_BIO,
144352c9ce25SScott Long 					  request_ccb->ccb_h.path, NULL, 0,
144452c9ce25SScott Long 					  request_ccb);
144552c9ce25SScott Long 
144652c9ce25SScott Long 		if (status != CAM_REQ_CMP) {
144752c9ce25SScott Long 			xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
144852c9ce25SScott Long 			    "returned an error, can't continue probe\n");
144952c9ce25SScott Long 			request_ccb->ccb_h.status = status;
145052c9ce25SScott Long 			xpt_done(request_ccb);
145152c9ce25SScott Long 		}
145252c9ce25SScott Long 	}
145352c9ce25SScott Long }
145452c9ce25SScott Long 
145552c9ce25SScott Long static void
145652c9ce25SScott Long xptscandone(struct cam_periph *periph, union ccb *done_ccb)
145752c9ce25SScott Long {
145852c9ce25SScott Long 	xpt_release_path(done_ccb->ccb_h.path);
145952c9ce25SScott Long 	free(done_ccb->ccb_h.path, M_CAMXPT);
146052c9ce25SScott Long 	free(done_ccb, M_CAMXPT);
146152c9ce25SScott Long }
146252c9ce25SScott Long 
146352c9ce25SScott Long static struct cam_ed *
146452c9ce25SScott Long ata_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
146552c9ce25SScott Long {
146652c9ce25SScott Long 	struct cam_path path;
146730a4094fSAlexander Motin 	struct ata_quirk_entry *quirk;
146852c9ce25SScott Long 	struct cam_ed *device;
146952c9ce25SScott Long 	struct cam_ed *cur_device;
147052c9ce25SScott Long 
147152c9ce25SScott Long 	device = xpt_alloc_device(bus, target, lun_id);
147252c9ce25SScott Long 	if (device == NULL)
147352c9ce25SScott Long 		return (NULL);
147452c9ce25SScott Long 
147552c9ce25SScott Long 	/*
147652c9ce25SScott Long 	 * Take the default quirk entry until we have inquiry
147752c9ce25SScott Long 	 * data and can determine a better quirk to use.
147852c9ce25SScott Long 	 */
147930a4094fSAlexander Motin 	quirk = &ata_quirk_table[ata_quirk_table_size - 1];
148052c9ce25SScott Long 	device->quirk = (void *)quirk;
148130a4094fSAlexander Motin 	device->mintags = 0;
148230a4094fSAlexander Motin 	device->maxtags = 0;
148352c9ce25SScott Long 	bzero(&device->inq_data, sizeof(device->inq_data));
148452c9ce25SScott Long 	device->inq_flags = 0;
148552c9ce25SScott Long 	device->queue_flags = 0;
148652c9ce25SScott Long 	device->serial_num = NULL;
148752c9ce25SScott Long 	device->serial_num_len = 0;
148852c9ce25SScott Long 
148952c9ce25SScott Long 	/*
149052c9ce25SScott Long 	 * XXX should be limited by number of CCBs this bus can
149152c9ce25SScott Long 	 * do.
149252c9ce25SScott Long 	 */
149352c9ce25SScott Long 	bus->sim->max_ccbs += device->ccbq.devq_openings;
149452c9ce25SScott Long 	/* Insertion sort into our target's device list */
149552c9ce25SScott Long 	cur_device = TAILQ_FIRST(&target->ed_entries);
149652c9ce25SScott Long 	while (cur_device != NULL && cur_device->lun_id < lun_id)
149752c9ce25SScott Long 		cur_device = TAILQ_NEXT(cur_device, links);
149852c9ce25SScott Long 	if (cur_device != NULL) {
149952c9ce25SScott Long 		TAILQ_INSERT_BEFORE(cur_device, device, links);
150052c9ce25SScott Long 	} else {
150152c9ce25SScott Long 		TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
150252c9ce25SScott Long 	}
150352c9ce25SScott Long 	target->generation++;
150452c9ce25SScott Long 	if (lun_id != CAM_LUN_WILDCARD) {
150552c9ce25SScott Long 		xpt_compile_path(&path,
150652c9ce25SScott Long 				 NULL,
150752c9ce25SScott Long 				 bus->path_id,
150852c9ce25SScott Long 				 target->target_id,
150952c9ce25SScott Long 				 lun_id);
151052c9ce25SScott Long 		ata_device_transport(&path);
151152c9ce25SScott Long 		xpt_release_path(&path);
151252c9ce25SScott Long 	}
151352c9ce25SScott Long 
151452c9ce25SScott Long 	return (device);
151552c9ce25SScott Long }
151652c9ce25SScott Long 
151752c9ce25SScott Long static void
151852c9ce25SScott Long ata_device_transport(struct cam_path *path)
151952c9ce25SScott Long {
152052c9ce25SScott Long 	struct ccb_pathinq cpi;
15214b997c49SAlexander Motin 	struct ccb_trans_settings cts;
15224b997c49SAlexander Motin 	struct scsi_inquiry_data *inq_buf = NULL;
15234b997c49SAlexander Motin 	struct ata_params *ident_buf = NULL;
152452c9ce25SScott Long 
152552c9ce25SScott Long 	/* Get transport information from the SIM */
152683c5d981SAlexander Motin 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
152752c9ce25SScott Long 	cpi.ccb_h.func_code = XPT_PATH_INQ;
152852c9ce25SScott Long 	xpt_action((union ccb *)&cpi);
152952c9ce25SScott Long 
153052c9ce25SScott Long 	path->device->transport = cpi.transport;
15314b997c49SAlexander Motin 	if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
15324b997c49SAlexander Motin 		inq_buf = &path->device->inq_data;
15334b997c49SAlexander Motin 	if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) != 0)
15344b997c49SAlexander Motin 		ident_buf = &path->device->ident_data;
15354b997c49SAlexander Motin 	if (path->device->protocol == PROTO_ATA) {
15364b997c49SAlexander Motin 		path->device->protocol_version = ident_buf ?
15374b997c49SAlexander Motin 		    ata_version(ident_buf->version_major) : cpi.protocol_version;
15384b997c49SAlexander Motin 	} else if (path->device->protocol == PROTO_SCSI) {
15394b997c49SAlexander Motin 		path->device->protocol_version = inq_buf ?
15404b997c49SAlexander Motin 		    SID_ANSI_REV(inq_buf) : cpi.protocol_version;
154152c9ce25SScott Long 	}
15424b997c49SAlexander Motin 	path->device->transport_version = ident_buf ?
15434b997c49SAlexander Motin 	    ata_version(ident_buf->version_major) : cpi.transport_version;
154452c9ce25SScott Long 
154552c9ce25SScott Long 	/* Tell the controller what we think */
154683c5d981SAlexander Motin 	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
154752c9ce25SScott Long 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
154852c9ce25SScott Long 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
154952c9ce25SScott Long 	cts.transport = path->device->transport;
155052c9ce25SScott Long 	cts.transport_version = path->device->transport_version;
155152c9ce25SScott Long 	cts.protocol = path->device->protocol;
155252c9ce25SScott Long 	cts.protocol_version = path->device->protocol_version;
155352c9ce25SScott Long 	cts.proto_specific.valid = 0;
15544cca1530SAlexander Motin 	if (ident_buf) {
15554cca1530SAlexander Motin 		if (path->device->transport == XPORT_ATA) {
15564cca1530SAlexander Motin 			cts.xport_specific.ata.atapi =
15574cca1530SAlexander Motin 			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 :
15584cca1530SAlexander Motin 			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0;
15594cca1530SAlexander Motin 			cts.xport_specific.ata.valid = CTS_ATA_VALID_ATAPI;
15604cca1530SAlexander Motin 		} else {
15614cca1530SAlexander Motin 			cts.xport_specific.sata.atapi =
15624cca1530SAlexander Motin 			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 :
15634cca1530SAlexander Motin 			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0;
15644cca1530SAlexander Motin 			cts.xport_specific.sata.valid = CTS_SATA_VALID_ATAPI;
15654cca1530SAlexander Motin 		}
15664cca1530SAlexander Motin 	} else
156752c9ce25SScott Long 		cts.xport_specific.valid = 0;
156852c9ce25SScott Long 	xpt_action((union ccb *)&cts);
156952c9ce25SScott Long }
157052c9ce25SScott Long 
157152c9ce25SScott Long static void
157252c9ce25SScott Long ata_action(union ccb *start_ccb)
157352c9ce25SScott Long {
157452c9ce25SScott Long 
157552c9ce25SScott Long 	switch (start_ccb->ccb_h.func_code) {
157652c9ce25SScott Long 	case XPT_SET_TRAN_SETTINGS:
157752c9ce25SScott Long 	{
157830a4094fSAlexander Motin 		ata_set_transfer_settings(&start_ccb->cts,
157952c9ce25SScott Long 					   start_ccb->ccb_h.path->device,
158052c9ce25SScott Long 					   /*async_update*/FALSE);
158152c9ce25SScott Long 		break;
158252c9ce25SScott Long 	}
158352c9ce25SScott Long 	case XPT_SCAN_BUS:
15840e85f214SMatt Jacob 	case XPT_SCAN_TGT:
158552c9ce25SScott Long 		ata_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
158652c9ce25SScott Long 		break;
158752c9ce25SScott Long 	case XPT_SCAN_LUN:
158852c9ce25SScott Long 		ata_scan_lun(start_ccb->ccb_h.path->periph,
158952c9ce25SScott Long 			      start_ccb->ccb_h.path, start_ccb->crcn.flags,
159052c9ce25SScott Long 			      start_ccb);
159152c9ce25SScott Long 		break;
159252c9ce25SScott Long 	case XPT_GET_TRAN_SETTINGS:
159352c9ce25SScott Long 	{
159452c9ce25SScott Long 		struct cam_sim *sim;
159552c9ce25SScott Long 
159652c9ce25SScott Long 		sim = start_ccb->ccb_h.path->bus->sim;
159752c9ce25SScott Long 		(*(sim->sim_action))(sim, start_ccb);
159852c9ce25SScott Long 		break;
159952c9ce25SScott Long 	}
16004cca1530SAlexander Motin 	case XPT_SCSI_IO:
16014cca1530SAlexander Motin 	{
16024cca1530SAlexander Motin 		struct cam_ed *device;
16034cca1530SAlexander Motin 		u_int	maxlen = 0;
16044cca1530SAlexander Motin 
16054cca1530SAlexander Motin 		device = start_ccb->ccb_h.path->device;
16064cca1530SAlexander Motin 		if (device->protocol == PROTO_SCSI &&
16074cca1530SAlexander Motin 		    (device->flags & CAM_DEV_IDENTIFY_DATA_VALID)) {
16084cca1530SAlexander Motin 			uint16_t p =
16094cca1530SAlexander Motin 			    device->ident_data.config & ATA_PROTO_MASK;
16104cca1530SAlexander Motin 
16114cca1530SAlexander Motin 			maxlen = (p == ATA_PROTO_ATAPI_16) ? 16 :
16124cca1530SAlexander Motin 			    (p == ATA_PROTO_ATAPI_12) ? 12 : 0;
16134cca1530SAlexander Motin 		}
16144cca1530SAlexander Motin 		if (start_ccb->csio.cdb_len > maxlen) {
16154cca1530SAlexander Motin 			start_ccb->ccb_h.status = CAM_REQ_INVALID;
16164cca1530SAlexander Motin 			xpt_done(start_ccb);
16174cca1530SAlexander Motin 			break;
16184cca1530SAlexander Motin 		}
16194cca1530SAlexander Motin 		/* FALLTHROUGH */
16204cca1530SAlexander Motin 	}
162152c9ce25SScott Long 	default:
162252c9ce25SScott Long 		xpt_action_default(start_ccb);
162352c9ce25SScott Long 		break;
162452c9ce25SScott Long 	}
162552c9ce25SScott Long }
162652c9ce25SScott Long 
162752c9ce25SScott Long static void
162830a4094fSAlexander Motin ata_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
162952c9ce25SScott Long 			   int async_update)
163052c9ce25SScott Long {
163152c9ce25SScott Long 	struct	ccb_pathinq cpi;
163252c9ce25SScott Long 	struct	ccb_trans_settings cur_cts;
163352c9ce25SScott Long 	struct	ccb_trans_settings_scsi *scsi;
163452c9ce25SScott Long 	struct	ccb_trans_settings_scsi *cur_scsi;
163552c9ce25SScott Long 	struct	cam_sim *sim;
163652c9ce25SScott Long 	struct	scsi_inquiry_data *inq_data;
163752c9ce25SScott Long 
163852c9ce25SScott Long 	if (device == NULL) {
163952c9ce25SScott Long 		cts->ccb_h.status = CAM_PATH_INVALID;
164052c9ce25SScott Long 		xpt_done((union ccb *)cts);
164152c9ce25SScott Long 		return;
164252c9ce25SScott Long 	}
164352c9ce25SScott Long 
164452c9ce25SScott Long 	if (cts->protocol == PROTO_UNKNOWN
164552c9ce25SScott Long 	 || cts->protocol == PROTO_UNSPECIFIED) {
164652c9ce25SScott Long 		cts->protocol = device->protocol;
164752c9ce25SScott Long 		cts->protocol_version = device->protocol_version;
164852c9ce25SScott Long 	}
164952c9ce25SScott Long 
165052c9ce25SScott Long 	if (cts->protocol_version == PROTO_VERSION_UNKNOWN
165152c9ce25SScott Long 	 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
165252c9ce25SScott Long 		cts->protocol_version = device->protocol_version;
165352c9ce25SScott Long 
165452c9ce25SScott Long 	if (cts->protocol != device->protocol) {
165552c9ce25SScott Long 		xpt_print(cts->ccb_h.path, "Uninitialized Protocol %x:%x?\n",
165652c9ce25SScott Long 		       cts->protocol, device->protocol);
165752c9ce25SScott Long 		cts->protocol = device->protocol;
165852c9ce25SScott Long 	}
165952c9ce25SScott Long 
166052c9ce25SScott Long 	if (cts->protocol_version > device->protocol_version) {
166152c9ce25SScott Long 		if (bootverbose) {
166252c9ce25SScott Long 			xpt_print(cts->ccb_h.path, "Down reving Protocol "
166352c9ce25SScott Long 			    "Version from %d to %d?\n", cts->protocol_version,
166452c9ce25SScott Long 			    device->protocol_version);
166552c9ce25SScott Long 		}
166652c9ce25SScott Long 		cts->protocol_version = device->protocol_version;
166752c9ce25SScott Long 	}
166852c9ce25SScott Long 
166952c9ce25SScott Long 	if (cts->transport == XPORT_UNKNOWN
167052c9ce25SScott Long 	 || cts->transport == XPORT_UNSPECIFIED) {
167152c9ce25SScott Long 		cts->transport = device->transport;
167252c9ce25SScott Long 		cts->transport_version = device->transport_version;
167352c9ce25SScott Long 	}
167452c9ce25SScott Long 
167552c9ce25SScott Long 	if (cts->transport_version == XPORT_VERSION_UNKNOWN
167652c9ce25SScott Long 	 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
167752c9ce25SScott Long 		cts->transport_version = device->transport_version;
167852c9ce25SScott Long 
167952c9ce25SScott Long 	if (cts->transport != device->transport) {
168052c9ce25SScott Long 		xpt_print(cts->ccb_h.path, "Uninitialized Transport %x:%x?\n",
168152c9ce25SScott Long 		    cts->transport, device->transport);
168252c9ce25SScott Long 		cts->transport = device->transport;
168352c9ce25SScott Long 	}
168452c9ce25SScott Long 
168552c9ce25SScott Long 	if (cts->transport_version > device->transport_version) {
168652c9ce25SScott Long 		if (bootverbose) {
168752c9ce25SScott Long 			xpt_print(cts->ccb_h.path, "Down reving Transport "
168852c9ce25SScott Long 			    "Version from %d to %d?\n", cts->transport_version,
168952c9ce25SScott Long 			    device->transport_version);
169052c9ce25SScott Long 		}
169152c9ce25SScott Long 		cts->transport_version = device->transport_version;
169252c9ce25SScott Long 	}
169352c9ce25SScott Long 
169452c9ce25SScott Long 	sim = cts->ccb_h.path->bus->sim;
169552c9ce25SScott Long 
169652c9ce25SScott Long 	/*
169752c9ce25SScott Long 	 * Nothing more of interest to do unless
169852c9ce25SScott Long 	 * this is a device connected via the
169952c9ce25SScott Long 	 * SCSI protocol.
170052c9ce25SScott Long 	 */
170152c9ce25SScott Long 	if (cts->protocol != PROTO_SCSI) {
170252c9ce25SScott Long 		if (async_update == FALSE)
170352c9ce25SScott Long 			(*(sim->sim_action))(sim, (union ccb *)cts);
170452c9ce25SScott Long 		return;
170552c9ce25SScott Long 	}
170652c9ce25SScott Long 
170752c9ce25SScott Long 	inq_data = &device->inq_data;
170852c9ce25SScott Long 	scsi = &cts->proto_specific.scsi;
170983c5d981SAlexander Motin 	xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NONE);
171052c9ce25SScott Long 	cpi.ccb_h.func_code = XPT_PATH_INQ;
171152c9ce25SScott Long 	xpt_action((union ccb *)&cpi);
171252c9ce25SScott Long 
171352c9ce25SScott Long 	/* SCSI specific sanity checking */
171452c9ce25SScott Long 	if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
171552c9ce25SScott Long 	 || (INQ_DATA_TQ_ENABLED(inq_data)) == 0
171652c9ce25SScott Long 	 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
171752c9ce25SScott Long 	 || (device->mintags == 0)) {
171852c9ce25SScott Long 		/*
171952c9ce25SScott Long 		 * Can't tag on hardware that doesn't support tags,
172052c9ce25SScott Long 		 * doesn't have it enabled, or has broken tag support.
172152c9ce25SScott Long 		 */
172252c9ce25SScott Long 		scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
172352c9ce25SScott Long 	}
172452c9ce25SScott Long 
172552c9ce25SScott Long 	if (async_update == FALSE) {
172652c9ce25SScott Long 		/*
172752c9ce25SScott Long 		 * Perform sanity checking against what the
172852c9ce25SScott Long 		 * controller and device can do.
172952c9ce25SScott Long 		 */
173083c5d981SAlexander Motin 		xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NONE);
173152c9ce25SScott Long 		cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
173252c9ce25SScott Long 		cur_cts.type = cts->type;
173352c9ce25SScott Long 		xpt_action((union ccb *)&cur_cts);
173452c9ce25SScott Long 		if ((cur_cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
173552c9ce25SScott Long 			return;
173652c9ce25SScott Long 		}
173752c9ce25SScott Long 		cur_scsi = &cur_cts.proto_specific.scsi;
173852c9ce25SScott Long 		if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
173952c9ce25SScott Long 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
174052c9ce25SScott Long 			scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
174152c9ce25SScott Long 		}
174252c9ce25SScott Long 		if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
174352c9ce25SScott Long 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
174452c9ce25SScott Long 	}
174552c9ce25SScott Long 
174652c9ce25SScott Long 	if (cts->type == CTS_TYPE_CURRENT_SETTINGS
174752c9ce25SScott Long 	 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
174852c9ce25SScott Long 		int device_tagenb;
174952c9ce25SScott Long 
175052c9ce25SScott Long 		/*
175152c9ce25SScott Long 		 * If we are transitioning from tags to no-tags or
175252c9ce25SScott Long 		 * vice-versa, we need to carefully freeze and restart
175352c9ce25SScott Long 		 * the queue so that we don't overlap tagged and non-tagged
175452c9ce25SScott Long 		 * commands.  We also temporarily stop tags if there is
175552c9ce25SScott Long 		 * a change in transfer negotiation settings to allow
175652c9ce25SScott Long 		 * "tag-less" negotiation.
175752c9ce25SScott Long 		 */
175852c9ce25SScott Long 		if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
175952c9ce25SScott Long 		 || (device->inq_flags & SID_CmdQue) != 0)
176052c9ce25SScott Long 			device_tagenb = TRUE;
176152c9ce25SScott Long 		else
176252c9ce25SScott Long 			device_tagenb = FALSE;
176352c9ce25SScott Long 
176452c9ce25SScott Long 		if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
176552c9ce25SScott Long 		  && device_tagenb == FALSE)
176652c9ce25SScott Long 		 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
176752c9ce25SScott Long 		  && device_tagenb == TRUE)) {
176852c9ce25SScott Long 
176952c9ce25SScott Long 			if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
177052c9ce25SScott Long 				/*
177152c9ce25SScott Long 				 * Delay change to use tags until after a
177252c9ce25SScott Long 				 * few commands have gone to this device so
177352c9ce25SScott Long 				 * the controller has time to perform transfer
177452c9ce25SScott Long 				 * negotiations without tagged messages getting
177552c9ce25SScott Long 				 * in the way.
177652c9ce25SScott Long 				 */
177752c9ce25SScott Long 				device->tag_delay_count = CAM_TAG_DELAY_COUNT;
177852c9ce25SScott Long 				device->flags |= CAM_DEV_TAG_AFTER_COUNT;
177952c9ce25SScott Long 			} else {
178030a4094fSAlexander Motin 				xpt_stop_tags(cts->ccb_h.path);
178152c9ce25SScott Long 			}
178252c9ce25SScott Long 		}
178352c9ce25SScott Long 	}
178452c9ce25SScott Long 	if (async_update == FALSE)
178552c9ce25SScott Long 		(*(sim->sim_action))(sim, (union ccb *)cts);
178652c9ce25SScott Long }
178752c9ce25SScott Long 
178852c9ce25SScott Long /*
178952c9ce25SScott Long  * Handle any per-device event notifications that require action by the XPT.
179052c9ce25SScott Long  */
179152c9ce25SScott Long static void
179252c9ce25SScott Long ata_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
179352c9ce25SScott Long 	      struct cam_ed *device, void *async_arg)
179452c9ce25SScott Long {
179552c9ce25SScott Long 	cam_status status;
179652c9ce25SScott Long 	struct cam_path newpath;
179752c9ce25SScott Long 
179852c9ce25SScott Long 	/*
179952c9ce25SScott Long 	 * We only need to handle events for real devices.
180052c9ce25SScott Long 	 */
180152c9ce25SScott Long 	if (target->target_id == CAM_TARGET_WILDCARD
180252c9ce25SScott Long 	 || device->lun_id == CAM_LUN_WILDCARD)
180352c9ce25SScott Long 		return;
180452c9ce25SScott Long 
180552c9ce25SScott Long 	/*
180652c9ce25SScott Long 	 * We need our own path with wildcards expanded to
180752c9ce25SScott Long 	 * handle certain types of events.
180852c9ce25SScott Long 	 */
180952c9ce25SScott Long 	if ((async_code == AC_SENT_BDR)
181052c9ce25SScott Long 	 || (async_code == AC_BUS_RESET)
181152c9ce25SScott Long 	 || (async_code == AC_INQ_CHANGED))
181252c9ce25SScott Long 		status = xpt_compile_path(&newpath, NULL,
181352c9ce25SScott Long 					  bus->path_id,
181452c9ce25SScott Long 					  target->target_id,
181552c9ce25SScott Long 					  device->lun_id);
181652c9ce25SScott Long 	else
181752c9ce25SScott Long 		status = CAM_REQ_CMP_ERR;
181852c9ce25SScott Long 
181952c9ce25SScott Long 	if (status == CAM_REQ_CMP) {
182052c9ce25SScott Long 		if (async_code == AC_INQ_CHANGED) {
182152c9ce25SScott Long 			/*
182252c9ce25SScott Long 			 * We've sent a start unit command, or
182352c9ce25SScott Long 			 * something similar to a device that
182452c9ce25SScott Long 			 * may have caused its inquiry data to
182552c9ce25SScott Long 			 * change. So we re-scan the device to
182652c9ce25SScott Long 			 * refresh the inquiry data for it.
182752c9ce25SScott Long 			 */
182852c9ce25SScott Long 			ata_scan_lun(newpath.periph, &newpath,
182952c9ce25SScott Long 				     CAM_EXPECT_INQ_CHANGE, NULL);
183083c5d981SAlexander Motin 		} else {
183183c5d981SAlexander Motin 			/* We need to reinitialize device after reset. */
183283c5d981SAlexander Motin 			ata_scan_lun(newpath.periph, &newpath,
183383c5d981SAlexander Motin 				     0, NULL);
183452c9ce25SScott Long 		}
183552c9ce25SScott Long 		xpt_release_path(&newpath);
1836f98d7a47SAlexander Motin 	} else if (async_code == AC_LOST_DEVICE &&
1837f98d7a47SAlexander Motin 	    (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
183852c9ce25SScott Long 		device->flags |= CAM_DEV_UNCONFIGURED;
1839f98d7a47SAlexander Motin 		xpt_release_device(device);
184052c9ce25SScott Long 	} else if (async_code == AC_TRANSFER_NEG) {
184152c9ce25SScott Long 		struct ccb_trans_settings *settings;
184252c9ce25SScott Long 
184352c9ce25SScott Long 		settings = (struct ccb_trans_settings *)async_arg;
184430a4094fSAlexander Motin 		ata_set_transfer_settings(settings, device,
184552c9ce25SScott Long 					  /*async_update*/TRUE);
184652c9ce25SScott Long 	}
184752c9ce25SScott Long }
184852c9ce25SScott Long 
184957079b17SAlexander Motin static void
185057079b17SAlexander Motin ata_announce_periph(struct cam_periph *periph)
185157079b17SAlexander Motin {
185257079b17SAlexander Motin 	struct	ccb_pathinq cpi;
185357079b17SAlexander Motin 	struct	ccb_trans_settings cts;
185457079b17SAlexander Motin 	struct	cam_path *path = periph->path;
185557079b17SAlexander Motin 	u_int	speed;
185657079b17SAlexander Motin 	u_int	mb;
185757079b17SAlexander Motin 
185857079b17SAlexander Motin 	mtx_assert(periph->sim->mtx, MA_OWNED);
185957079b17SAlexander Motin 
186057079b17SAlexander Motin 	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
186157079b17SAlexander Motin 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
186257079b17SAlexander Motin 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
186357079b17SAlexander Motin 	xpt_action((union ccb*)&cts);
186457079b17SAlexander Motin 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
186557079b17SAlexander Motin 		return;
186657079b17SAlexander Motin 	/* Ask the SIM for its base transfer speed */
186757079b17SAlexander Motin 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
186857079b17SAlexander Motin 	cpi.ccb_h.func_code = XPT_PATH_INQ;
186957079b17SAlexander Motin 	xpt_action((union ccb *)&cpi);
187057079b17SAlexander Motin 	/* Report connection speed */
187157079b17SAlexander Motin 	speed = cpi.base_transfer_speed;
187257079b17SAlexander Motin 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_ATA) {
187357079b17SAlexander Motin 		struct	ccb_trans_settings_ata *ata =
187457079b17SAlexander Motin 		    &cts.xport_specific.ata;
187557079b17SAlexander Motin 
187657079b17SAlexander Motin 		if (ata->valid & CTS_ATA_VALID_MODE)
187757079b17SAlexander Motin 			speed = ata_mode2speed(ata->mode);
187857079b17SAlexander Motin 	}
187957079b17SAlexander Motin 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SATA) {
188057079b17SAlexander Motin 		struct	ccb_trans_settings_sata *sata =
188157079b17SAlexander Motin 		    &cts.xport_specific.sata;
188257079b17SAlexander Motin 
188357079b17SAlexander Motin 		if (sata->valid & CTS_SATA_VALID_REVISION)
188457079b17SAlexander Motin 			speed = ata_revision2speed(sata->revision);
188557079b17SAlexander Motin 	}
188657079b17SAlexander Motin 	mb = speed / 1000;
188757079b17SAlexander Motin 	if (mb > 0)
188857079b17SAlexander Motin 		printf("%s%d: %d.%03dMB/s transfers",
188957079b17SAlexander Motin 		       periph->periph_name, periph->unit_number,
189057079b17SAlexander Motin 		       mb, speed % 1000);
189157079b17SAlexander Motin 	else
189257079b17SAlexander Motin 		printf("%s%d: %dKB/s transfers", periph->periph_name,
189357079b17SAlexander Motin 		       periph->unit_number, speed);
189457079b17SAlexander Motin 	/* Report additional information about connection */
189557079b17SAlexander Motin 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_ATA) {
189657079b17SAlexander Motin 		struct ccb_trans_settings_ata *ata =
189757079b17SAlexander Motin 		    &cts.xport_specific.ata;
189857079b17SAlexander Motin 
189957079b17SAlexander Motin 		printf(" (");
190057079b17SAlexander Motin 		if (ata->valid & CTS_ATA_VALID_MODE)
190157079b17SAlexander Motin 			printf("%s, ", ata_mode2string(ata->mode));
190257079b17SAlexander Motin 		if ((ata->valid & CTS_ATA_VALID_ATAPI) && ata->atapi != 0)
190357079b17SAlexander Motin 			printf("ATAPI %dbytes, ", ata->atapi);
190457079b17SAlexander Motin 		if (ata->valid & CTS_ATA_VALID_BYTECOUNT)
190557079b17SAlexander Motin 			printf("PIO %dbytes", ata->bytecount);
190657079b17SAlexander Motin 		printf(")");
190757079b17SAlexander Motin 	}
190857079b17SAlexander Motin 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SATA) {
190957079b17SAlexander Motin 		struct ccb_trans_settings_sata *sata =
191057079b17SAlexander Motin 		    &cts.xport_specific.sata;
191157079b17SAlexander Motin 
191257079b17SAlexander Motin 		printf(" (");
191357079b17SAlexander Motin 		if (sata->valid & CTS_SATA_VALID_REVISION)
191457079b17SAlexander Motin 			printf("SATA %d.x, ", sata->revision);
191557079b17SAlexander Motin 		else
191657079b17SAlexander Motin 			printf("SATA, ");
191757079b17SAlexander Motin 		if (sata->valid & CTS_SATA_VALID_MODE)
191857079b17SAlexander Motin 			printf("%s, ", ata_mode2string(sata->mode));
191957079b17SAlexander Motin 		if ((sata->valid & CTS_ATA_VALID_ATAPI) && sata->atapi != 0)
192057079b17SAlexander Motin 			printf("ATAPI %dbytes, ", sata->atapi);
192157079b17SAlexander Motin 		if (sata->valid & CTS_SATA_VALID_BYTECOUNT)
192257079b17SAlexander Motin 			printf("PIO %dbytes", sata->bytecount);
192357079b17SAlexander Motin 		printf(")");
192457079b17SAlexander Motin 	}
192557079b17SAlexander Motin 	printf("\n");
192657079b17SAlexander Motin }
192757079b17SAlexander Motin 
1928