xref: /freebsd/sys/cam/ata/ata_xpt.c (revision cf2b9a5f)
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,
908d169381SAlexander Motin 	PROBE_SETAN,
911e637ba6SAlexander Motin 	PROBE_SET_MULTI,
9252c9ce25SScott Long 	PROBE_INQUIRY,
9352c9ce25SScott Long 	PROBE_FULL_INQUIRY,
9452c9ce25SScott Long 	PROBE_PM_PID,
9552c9ce25SScott Long 	PROBE_PM_PRV,
9652c9ce25SScott Long 	PROBE_INVALID
9752c9ce25SScott Long } probe_action;
9852c9ce25SScott Long 
9952c9ce25SScott Long static char *probe_action_text[] = {
10052c9ce25SScott Long 	"PROBE_RESET",
10152c9ce25SScott Long 	"PROBE_IDENTIFY",
1024ef08dc5SAlexander Motin 	"PROBE_SPINUP",
10352c9ce25SScott Long 	"PROBE_SETMODE",
104da6808c1SAlexander Motin 	"PROBE_SETPM",
105da6808c1SAlexander Motin 	"PROBE_SETAPST",
106da6808c1SAlexander Motin 	"PROBE_SETDMAAA",
1078d169381SAlexander Motin 	"PROBE_SETAN",
1081e637ba6SAlexander Motin 	"PROBE_SET_MULTI",
10952c9ce25SScott Long 	"PROBE_INQUIRY",
11052c9ce25SScott Long 	"PROBE_FULL_INQUIRY",
11152c9ce25SScott Long 	"PROBE_PM_PID",
11252c9ce25SScott Long 	"PROBE_PM_PRV",
11352c9ce25SScott Long 	"PROBE_INVALID"
11452c9ce25SScott Long };
11552c9ce25SScott Long 
11652c9ce25SScott Long #define PROBE_SET_ACTION(softc, newaction)	\
11752c9ce25SScott Long do {									\
11852c9ce25SScott Long 	char **text;							\
11952c9ce25SScott Long 	text = probe_action_text;					\
12052c9ce25SScott Long 	CAM_DEBUG((softc)->periph->path, CAM_DEBUG_INFO,		\
12152c9ce25SScott Long 	    ("Probe %s to %s\n", text[(softc)->action],			\
12252c9ce25SScott Long 	    text[(newaction)]));					\
12352c9ce25SScott Long 	(softc)->action = (newaction);					\
12452c9ce25SScott Long } while(0)
12552c9ce25SScott Long 
12652c9ce25SScott Long typedef enum {
12752c9ce25SScott Long 	PROBE_NO_ANNOUNCE	= 0x04
12852c9ce25SScott Long } probe_flags;
12952c9ce25SScott Long 
13052c9ce25SScott Long typedef struct {
13152c9ce25SScott Long 	TAILQ_HEAD(, ccb_hdr) request_ccbs;
132a9b8edb1SAlexander Motin 	struct ata_params	ident_data;
13352c9ce25SScott Long 	probe_action	action;
13452c9ce25SScott Long 	probe_flags	flags;
13552c9ce25SScott Long 	uint32_t	pm_pid;
13652c9ce25SScott Long 	uint32_t	pm_prv;
13783c5d981SAlexander Motin 	int		restart;
1384ef08dc5SAlexander Motin 	int		spinup;
13925a519a9SAlexander Motin 	int		faults;
140da6808c1SAlexander Motin 	u_int		caps;
14152c9ce25SScott Long 	struct cam_periph *periph;
14252c9ce25SScott Long } probe_softc;
14352c9ce25SScott Long 
14430a4094fSAlexander Motin static struct ata_quirk_entry ata_quirk_table[] =
14552c9ce25SScott Long {
14652c9ce25SScott Long 	{
14752c9ce25SScott Long 		/* Default tagged queuing parameters for all devices */
14852c9ce25SScott Long 		{
14952c9ce25SScott Long 		  T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
15052c9ce25SScott Long 		  /*vendor*/"*", /*product*/"*", /*revision*/"*"
15152c9ce25SScott Long 		},
15230a4094fSAlexander Motin 		/*quirks*/0, /*maxtags*/0
15352c9ce25SScott Long 	},
15452c9ce25SScott Long };
15552c9ce25SScott Long 
15630a4094fSAlexander Motin static const int ata_quirk_table_size =
15730a4094fSAlexander Motin 	sizeof(ata_quirk_table) / sizeof(*ata_quirk_table);
15852c9ce25SScott Long 
15952c9ce25SScott Long static cam_status	proberegister(struct cam_periph *periph,
16052c9ce25SScott Long 				      void *arg);
16152c9ce25SScott Long static void	 probeschedule(struct cam_periph *probe_periph);
16252c9ce25SScott Long static void	 probestart(struct cam_periph *periph, union ccb *start_ccb);
16352c9ce25SScott Long //static void	 proberequestdefaultnegotiation(struct cam_periph *periph);
16452c9ce25SScott Long //static int       proberequestbackoff(struct cam_periph *periph,
16552c9ce25SScott Long //				     struct cam_ed *device);
16652c9ce25SScott Long static void	 probedone(struct cam_periph *periph, union ccb *done_ccb);
16752c9ce25SScott Long static void	 probecleanup(struct cam_periph *periph);
16830a4094fSAlexander Motin static void	 ata_find_quirk(struct cam_ed *device);
16952c9ce25SScott Long static void	 ata_scan_bus(struct cam_periph *periph, union ccb *ccb);
17052c9ce25SScott Long static void	 ata_scan_lun(struct cam_periph *periph,
17152c9ce25SScott Long 			       struct cam_path *path, cam_flags flags,
17252c9ce25SScott Long 			       union ccb *ccb);
17352c9ce25SScott Long static void	 xptscandone(struct cam_periph *periph, union ccb *done_ccb);
17452c9ce25SScott Long static struct cam_ed *
17552c9ce25SScott Long 		 ata_alloc_device(struct cam_eb *bus, struct cam_et *target,
17652c9ce25SScott Long 				   lun_id_t lun_id);
17752c9ce25SScott Long static void	 ata_device_transport(struct cam_path *path);
17830a4094fSAlexander Motin static void	 ata_set_transfer_settings(struct ccb_trans_settings *cts,
17952c9ce25SScott Long 					    struct cam_ed *device,
18052c9ce25SScott Long 					    int async_update);
18152c9ce25SScott Long static void	 ata_dev_async(u_int32_t async_code,
18252c9ce25SScott Long 				struct cam_eb *bus,
18352c9ce25SScott Long 				struct cam_et *target,
18452c9ce25SScott Long 				struct cam_ed *device,
18552c9ce25SScott Long 				void *async_arg);
18652c9ce25SScott Long static void	 ata_action(union ccb *start_ccb);
18757079b17SAlexander Motin static void	 ata_announce_periph(struct cam_periph *periph);
18852c9ce25SScott Long 
18952c9ce25SScott Long static struct xpt_xport ata_xport = {
19052c9ce25SScott Long 	.alloc_device = ata_alloc_device,
19152c9ce25SScott Long 	.action = ata_action,
19252c9ce25SScott Long 	.async = ata_dev_async,
19357079b17SAlexander Motin 	.announce = ata_announce_periph,
19452c9ce25SScott Long };
19552c9ce25SScott Long 
19652c9ce25SScott Long struct xpt_xport *
19752c9ce25SScott Long ata_get_xport(void)
19852c9ce25SScott Long {
19952c9ce25SScott Long 	return (&ata_xport);
20052c9ce25SScott Long }
20152c9ce25SScott Long 
20252c9ce25SScott Long static void
20352c9ce25SScott Long probe_periph_init()
20452c9ce25SScott Long {
20552c9ce25SScott Long }
20652c9ce25SScott Long 
20752c9ce25SScott Long static cam_status
20852c9ce25SScott Long proberegister(struct cam_periph *periph, void *arg)
20952c9ce25SScott Long {
21052c9ce25SScott Long 	union ccb *request_ccb;	/* CCB representing the probe request */
21152c9ce25SScott Long 	cam_status status;
21252c9ce25SScott Long 	probe_softc *softc;
21352c9ce25SScott Long 
21452c9ce25SScott Long 	request_ccb = (union ccb *)arg;
21552c9ce25SScott Long 	if (periph == NULL) {
21652c9ce25SScott Long 		printf("proberegister: periph was NULL!!\n");
21752c9ce25SScott Long 		return(CAM_REQ_CMP_ERR);
21852c9ce25SScott Long 	}
21952c9ce25SScott Long 
22052c9ce25SScott Long 	if (request_ccb == NULL) {
22152c9ce25SScott Long 		printf("proberegister: no probe CCB, "
22252c9ce25SScott Long 		       "can't register device\n");
22352c9ce25SScott Long 		return(CAM_REQ_CMP_ERR);
22452c9ce25SScott Long 	}
22552c9ce25SScott Long 
2264ef08dc5SAlexander Motin 	softc = (probe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_ZERO | M_NOWAIT);
22752c9ce25SScott Long 
22852c9ce25SScott Long 	if (softc == NULL) {
22952c9ce25SScott Long 		printf("proberegister: Unable to probe new device. "
23052c9ce25SScott Long 		       "Unable to allocate softc\n");
23152c9ce25SScott Long 		return(CAM_REQ_CMP_ERR);
23252c9ce25SScott Long 	}
23352c9ce25SScott Long 	TAILQ_INIT(&softc->request_ccbs);
23452c9ce25SScott Long 	TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
23552c9ce25SScott Long 			  periph_links.tqe);
23652c9ce25SScott Long 	softc->flags = 0;
23752c9ce25SScott Long 	periph->softc = softc;
23852c9ce25SScott Long 	softc->periph = periph;
23952c9ce25SScott Long 	softc->action = PROBE_INVALID;
24052c9ce25SScott Long 	status = cam_periph_acquire(periph);
24152c9ce25SScott Long 	if (status != CAM_REQ_CMP) {
24252c9ce25SScott Long 		return (status);
24352c9ce25SScott Long 	}
24452c9ce25SScott Long 	/*
24583c5d981SAlexander Motin 	 * Ensure nobody slip in until probe finish.
24652c9ce25SScott Long 	 */
24783c5d981SAlexander Motin 	cam_freeze_devq_arg(periph->path,
24883c5d981SAlexander Motin 	    RELSIM_RELEASE_RUNLEVEL, CAM_RL_XPT + 1);
24952c9ce25SScott Long 	probeschedule(periph);
25052c9ce25SScott Long 	return(CAM_REQ_CMP);
25152c9ce25SScott Long }
25252c9ce25SScott Long 
25352c9ce25SScott Long static void
25452c9ce25SScott Long probeschedule(struct cam_periph *periph)
25552c9ce25SScott Long {
25652c9ce25SScott Long 	union ccb *ccb;
25752c9ce25SScott Long 	probe_softc *softc;
25852c9ce25SScott Long 
25952c9ce25SScott Long 	softc = (probe_softc *)periph->softc;
26052c9ce25SScott Long 	ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
26152c9ce25SScott Long 
26265d0fb03SAlexander Motin 	if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) ||
26365d0fb03SAlexander Motin 	    periph->path->device->protocol == PROTO_SATAPM)
26452c9ce25SScott Long 		PROBE_SET_ACTION(softc, PROBE_RESET);
26552c9ce25SScott Long 	else
26652c9ce25SScott Long 		PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
26752c9ce25SScott Long 
26852c9ce25SScott Long 	if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
26952c9ce25SScott Long 		softc->flags |= PROBE_NO_ANNOUNCE;
27052c9ce25SScott Long 	else
27152c9ce25SScott Long 		softc->flags &= ~PROBE_NO_ANNOUNCE;
27252c9ce25SScott Long 
27383c5d981SAlexander Motin 	xpt_schedule(periph, CAM_PRIORITY_XPT);
27452c9ce25SScott Long }
27552c9ce25SScott Long 
27652c9ce25SScott Long static void
27752c9ce25SScott Long probestart(struct cam_periph *periph, union ccb *start_ccb)
27852c9ce25SScott Long {
279c8039fc6SAlexander Motin 	struct ccb_trans_settings cts;
28052c9ce25SScott Long 	struct ccb_ataio *ataio;
28152c9ce25SScott Long 	struct ccb_scsiio *csio;
28252c9ce25SScott Long 	probe_softc *softc;
2831e637ba6SAlexander Motin 	struct cam_path *path;
2841e637ba6SAlexander Motin 	struct ata_params *ident_buf;
28552c9ce25SScott Long 
28652c9ce25SScott Long 	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
28752c9ce25SScott Long 
28852c9ce25SScott Long 	softc = (probe_softc *)periph->softc;
2891e637ba6SAlexander Motin 	path = start_ccb->ccb_h.path;
29052c9ce25SScott Long 	ataio = &start_ccb->ataio;
29152c9ce25SScott Long 	csio = &start_ccb->csio;
2921e637ba6SAlexander Motin 	ident_buf = &periph->path->device->ident_data;
29352c9ce25SScott Long 
29483c5d981SAlexander Motin 	if (softc->restart) {
29583c5d981SAlexander Motin 		softc->restart = 0;
29683c5d981SAlexander Motin 		if ((path->device->flags & CAM_DEV_UNCONFIGURED) ||
29783c5d981SAlexander Motin 		    path->device->protocol == PROTO_SATAPM)
29883c5d981SAlexander Motin 			softc->action = PROBE_RESET;
29983c5d981SAlexander Motin 		else
30083c5d981SAlexander Motin 			softc->action = PROBE_IDENTIFY;
30183c5d981SAlexander Motin 	}
30252c9ce25SScott Long 	switch (softc->action) {
30352c9ce25SScott Long 	case PROBE_RESET:
30452c9ce25SScott Long 		cam_fill_ataio(ataio,
30552c9ce25SScott Long 		      0,
30652c9ce25SScott Long 		      probedone,
30752c9ce25SScott Long 		      /*flags*/CAM_DIR_NONE,
30865d0fb03SAlexander Motin 		      0,
30952c9ce25SScott Long 		      /*data_ptr*/NULL,
31052c9ce25SScott Long 		      /*dxfer_len*/0,
31183c5d981SAlexander Motin 		      15 * 1000);
31252c9ce25SScott Long 		ata_reset_cmd(ataio);
31352c9ce25SScott Long 		break;
31452c9ce25SScott Long 	case PROBE_IDENTIFY:
31552c9ce25SScott Long 		cam_fill_ataio(ataio,
31652c9ce25SScott Long 		      1,
31752c9ce25SScott Long 		      probedone,
31852c9ce25SScott Long 		      /*flags*/CAM_DIR_IN,
31965d0fb03SAlexander Motin 		      0,
320a9b8edb1SAlexander Motin 		      /*data_ptr*/(u_int8_t *)&softc->ident_data,
321a9b8edb1SAlexander Motin 		      /*dxfer_len*/sizeof(softc->ident_data),
32252c9ce25SScott Long 		      30 * 1000);
32352c9ce25SScott Long 		if (periph->path->device->protocol == PROTO_ATA)
3247606b445SAlexander Motin 			ata_28bit_cmd(ataio, ATA_ATA_IDENTIFY, 0, 0, 0);
32552c9ce25SScott Long 		else
3267606b445SAlexander Motin 			ata_28bit_cmd(ataio, ATA_ATAPI_IDENTIFY, 0, 0, 0);
32752c9ce25SScott Long 		break;
3284ef08dc5SAlexander Motin 	case PROBE_SPINUP:
3294ef08dc5SAlexander Motin 		if (bootverbose)
3304ef08dc5SAlexander Motin 			xpt_print(path, "Spinning up device\n");
3314ef08dc5SAlexander Motin 		cam_fill_ataio(ataio,
3324ef08dc5SAlexander Motin 		      1,
3334ef08dc5SAlexander Motin 		      probedone,
3344ef08dc5SAlexander Motin 		      /*flags*/CAM_DIR_NONE | CAM_HIGH_POWER,
3354ef08dc5SAlexander Motin 		      0,
3364ef08dc5SAlexander Motin 		      /*data_ptr*/NULL,
3374ef08dc5SAlexander Motin 		      /*dxfer_len*/0,
3384ef08dc5SAlexander Motin 		      30 * 1000);
3394ef08dc5SAlexander Motin 		ata_28bit_cmd(ataio, ATA_SETFEATURES, ATA_SF_PUIS_SPINUP, 0, 0);
3404ef08dc5SAlexander Motin 		break;
34152c9ce25SScott Long 	case PROBE_SETMODE:
342c8039fc6SAlexander Motin 	{
343c8039fc6SAlexander Motin 		int mode, wantmode;
344c8039fc6SAlexander Motin 
345c8039fc6SAlexander Motin 		mode = 0;
346c8039fc6SAlexander Motin 		/* Fetch user modes from SIM. */
347c8039fc6SAlexander Motin 		bzero(&cts, sizeof(cts));
34883c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
349c8039fc6SAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
350c8039fc6SAlexander Motin 		cts.type = CTS_TYPE_USER_SETTINGS;
351c8039fc6SAlexander Motin 		xpt_action((union ccb *)&cts);
352c8039fc6SAlexander Motin 		if (path->device->transport == XPORT_ATA) {
353c8039fc6SAlexander Motin 			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_MODE)
354c8039fc6SAlexander Motin 				mode = cts.xport_specific.ata.mode;
355c8039fc6SAlexander Motin 		} else {
3561a6f09b8SAlexander Motin 			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_MODE)
357c8039fc6SAlexander Motin 				mode = cts.xport_specific.sata.mode;
358c8039fc6SAlexander Motin 		}
359c8039fc6SAlexander Motin negotiate:
360c8039fc6SAlexander Motin 		/* Honor device capabilities. */
361c8039fc6SAlexander Motin 		wantmode = mode = ata_max_mode(ident_buf, mode);
362c8039fc6SAlexander Motin 		/* Report modes to SIM. */
363c8039fc6SAlexander Motin 		bzero(&cts, sizeof(cts));
36483c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
365c8039fc6SAlexander Motin 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
366c8039fc6SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
367c8039fc6SAlexander Motin 		if (path->device->transport == XPORT_ATA) {
368c8039fc6SAlexander Motin 			cts.xport_specific.ata.mode = mode;
369c8039fc6SAlexander Motin 			cts.xport_specific.ata.valid = CTS_ATA_VALID_MODE;
370c8039fc6SAlexander Motin 		} else {
371c8039fc6SAlexander Motin 			cts.xport_specific.sata.mode = mode;
372c8039fc6SAlexander Motin 			cts.xport_specific.sata.valid = CTS_SATA_VALID_MODE;
373c8039fc6SAlexander Motin 		}
374c8039fc6SAlexander Motin 		xpt_action((union ccb *)&cts);
375066f913aSAlexander Motin 		/* Fetch current modes from SIM. */
376c8039fc6SAlexander Motin 		bzero(&cts, sizeof(cts));
37783c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
378c8039fc6SAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
379c8039fc6SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
380c8039fc6SAlexander Motin 		xpt_action((union ccb *)&cts);
381c8039fc6SAlexander Motin 		if (path->device->transport == XPORT_ATA) {
382c8039fc6SAlexander Motin 			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_MODE)
383c8039fc6SAlexander Motin 				mode = cts.xport_specific.ata.mode;
384c8039fc6SAlexander Motin 		} else {
385c8039fc6SAlexander Motin 			if (cts.xport_specific.ata.valid & CTS_SATA_VALID_MODE)
386c8039fc6SAlexander Motin 				mode = cts.xport_specific.sata.mode;
387c8039fc6SAlexander Motin 		}
388c8039fc6SAlexander Motin 		/* If SIM disagree - renegotiate. */
389c8039fc6SAlexander Motin 		if (mode != wantmode)
390c8039fc6SAlexander Motin 			goto negotiate;
391cf2b9a5fSAlexander Motin 		/* Remember what transport thinks about DMA. */
392cf2b9a5fSAlexander Motin 		if (mode < ATA_DMA)
393cf2b9a5fSAlexander Motin 			path->device->inq_flags &= ~SID_DMA;
394cf2b9a5fSAlexander Motin 		else
395cf2b9a5fSAlexander Motin 			path->device->inq_flags |= SID_DMA;
39652c9ce25SScott Long 		cam_fill_ataio(ataio,
39752c9ce25SScott Long 		      1,
39852c9ce25SScott Long 		      probedone,
3995daa555dSAlexander Motin 		      /*flags*/CAM_DIR_NONE,
4005daa555dSAlexander Motin 		      0,
4015daa555dSAlexander Motin 		      /*data_ptr*/NULL,
4025daa555dSAlexander Motin 		      /*dxfer_len*/0,
40352c9ce25SScott Long 		      30 * 1000);
404c8039fc6SAlexander Motin 		ata_28bit_cmd(ataio, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
40552c9ce25SScott Long 		break;
406c8039fc6SAlexander Motin 	}
407da6808c1SAlexander Motin 	case PROBE_SETPM:
408da6808c1SAlexander Motin 		cam_fill_ataio(ataio,
409da6808c1SAlexander Motin 		    1,
410da6808c1SAlexander Motin 		    probedone,
411da6808c1SAlexander Motin 		    CAM_DIR_NONE,
412da6808c1SAlexander Motin 		    0,
413da6808c1SAlexander Motin 		    NULL,
414da6808c1SAlexander Motin 		    0,
415da6808c1SAlexander Motin 		    30*1000);
416da6808c1SAlexander Motin 		ata_28bit_cmd(ataio, ATA_SETFEATURES,
417da6808c1SAlexander Motin 		    (softc->caps & CTS_SATA_CAPS_H_PMREQ) ? 0x10 : 0x90,
418da6808c1SAlexander Motin 		    0, 0x03);
419da6808c1SAlexander Motin 		break;
420da6808c1SAlexander Motin 	case PROBE_SETAPST:
421da6808c1SAlexander Motin 		cam_fill_ataio(ataio,
422da6808c1SAlexander Motin 		    1,
423da6808c1SAlexander Motin 		    probedone,
424da6808c1SAlexander Motin 		    CAM_DIR_NONE,
425da6808c1SAlexander Motin 		    0,
426da6808c1SAlexander Motin 		    NULL,
427da6808c1SAlexander Motin 		    0,
428da6808c1SAlexander Motin 		    30*1000);
429da6808c1SAlexander Motin 		ata_28bit_cmd(ataio, ATA_SETFEATURES,
430da6808c1SAlexander Motin 		    (softc->caps & CTS_SATA_CAPS_H_APST) ? 0x10 : 0x90,
431da6808c1SAlexander Motin 		    0, 0x07);
432da6808c1SAlexander Motin 		break;
433da6808c1SAlexander Motin 	case PROBE_SETDMAAA:
434da6808c1SAlexander Motin 		cam_fill_ataio(ataio,
435da6808c1SAlexander Motin 		    1,
436da6808c1SAlexander Motin 		    probedone,
437da6808c1SAlexander Motin 		    CAM_DIR_NONE,
438da6808c1SAlexander Motin 		    0,
439da6808c1SAlexander Motin 		    NULL,
440da6808c1SAlexander Motin 		    0,
441da6808c1SAlexander Motin 		    30*1000);
442da6808c1SAlexander Motin 		ata_28bit_cmd(ataio, ATA_SETFEATURES,
443da6808c1SAlexander Motin 		    (softc->caps & CTS_SATA_CAPS_H_DMAAA) ? 0x10 : 0x90,
444da6808c1SAlexander Motin 		    0, 0x02);
445da6808c1SAlexander Motin 		break;
4468d169381SAlexander Motin 	case PROBE_SETAN:
4478d169381SAlexander Motin 		cam_fill_ataio(ataio,
4488d169381SAlexander Motin 		    1,
4498d169381SAlexander Motin 		    probedone,
4508d169381SAlexander Motin 		    CAM_DIR_NONE,
4518d169381SAlexander Motin 		    0,
4528d169381SAlexander Motin 		    NULL,
4538d169381SAlexander Motin 		    0,
4548d169381SAlexander Motin 		    30*1000);
4558d169381SAlexander Motin 		ata_28bit_cmd(ataio, ATA_SETFEATURES,
4568d169381SAlexander Motin 		    (softc->caps & CTS_SATA_CAPS_H_AN) ? 0x10 : 0x90,
4578d169381SAlexander Motin 		    0, 0x05);
4588d169381SAlexander Motin 		break;
4591e637ba6SAlexander Motin 	case PROBE_SET_MULTI:
4601e637ba6SAlexander Motin 	{
461066f913aSAlexander Motin 		u_int sectors, bytecount;
4621e637ba6SAlexander Motin 
463066f913aSAlexander Motin 		bytecount = 8192;	/* SATA maximum */
464066f913aSAlexander Motin 		/* Fetch user bytecount from SIM. */
465066f913aSAlexander Motin 		bzero(&cts, sizeof(cts));
46683c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
467066f913aSAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
468066f913aSAlexander Motin 		cts.type = CTS_TYPE_USER_SETTINGS;
469066f913aSAlexander Motin 		xpt_action((union ccb *)&cts);
470066f913aSAlexander Motin 		if (path->device->transport == XPORT_ATA) {
471066f913aSAlexander Motin 			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
472066f913aSAlexander Motin 				bytecount = cts.xport_specific.ata.bytecount;
473066f913aSAlexander Motin 		} else {
474066f913aSAlexander Motin 			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
475066f913aSAlexander Motin 				bytecount = cts.xport_specific.sata.bytecount;
476066f913aSAlexander Motin 		}
477066f913aSAlexander Motin 		/* Honor device capabilities. */
478066f913aSAlexander Motin 		sectors = max(1, min(ident_buf->sectors_intr & 0xff,
479066f913aSAlexander Motin 		    bytecount / ata_logical_sector_size(ident_buf)));
4801e637ba6SAlexander Motin 		/* Report bytecount to SIM. */
4811e637ba6SAlexander Motin 		bzero(&cts, sizeof(cts));
48283c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
4831e637ba6SAlexander Motin 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
4841e637ba6SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
4851e637ba6SAlexander Motin 		if (path->device->transport == XPORT_ATA) {
486c1bd46c2SAlexander Motin 			cts.xport_specific.ata.bytecount = sectors *
487c1bd46c2SAlexander Motin 			    ata_logical_sector_size(ident_buf);
4881e637ba6SAlexander Motin 			cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT;
4891e637ba6SAlexander Motin 		} else {
490c1bd46c2SAlexander Motin 			cts.xport_specific.sata.bytecount = sectors *
491c1bd46c2SAlexander Motin 			    ata_logical_sector_size(ident_buf);
4921e637ba6SAlexander Motin 			cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT;
4931e637ba6SAlexander Motin 		}
4941e637ba6SAlexander Motin 		xpt_action((union ccb *)&cts);
495066f913aSAlexander Motin 		/* Fetch current bytecount from SIM. */
496066f913aSAlexander Motin 		bzero(&cts, sizeof(cts));
49783c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
498066f913aSAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
499066f913aSAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
500066f913aSAlexander Motin 		xpt_action((union ccb *)&cts);
501066f913aSAlexander Motin 		if (path->device->transport == XPORT_ATA) {
502066f913aSAlexander Motin 			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
503066f913aSAlexander Motin 				bytecount = cts.xport_specific.ata.bytecount;
504066f913aSAlexander Motin 		} else {
505066f913aSAlexander Motin 			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
506066f913aSAlexander Motin 				bytecount = cts.xport_specific.sata.bytecount;
507066f913aSAlexander Motin 		}
508066f913aSAlexander Motin 		sectors = bytecount / ata_logical_sector_size(ident_buf);
5091e637ba6SAlexander Motin 
5101e637ba6SAlexander Motin 		cam_fill_ataio(ataio,
5111e637ba6SAlexander Motin 		    1,
5121e637ba6SAlexander Motin 		    probedone,
5131e637ba6SAlexander Motin 		    CAM_DIR_NONE,
5141e637ba6SAlexander Motin 		    0,
5151e637ba6SAlexander Motin 		    NULL,
5161e637ba6SAlexander Motin 		    0,
5171e637ba6SAlexander Motin 		    30*1000);
5181e637ba6SAlexander Motin 		ata_28bit_cmd(ataio, ATA_SET_MULTI, 0, 0, sectors);
5191e637ba6SAlexander Motin 		break;
52052c9ce25SScott Long 	}
52152c9ce25SScott Long 	case PROBE_INQUIRY:
522066f913aSAlexander Motin 	{
523066f913aSAlexander Motin 		u_int bytecount;
524066f913aSAlexander Motin 
525066f913aSAlexander Motin 		bytecount = 8192;	/* SATA maximum */
526066f913aSAlexander Motin 		/* Fetch user bytecount from 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_GET_TRAN_SETTINGS;
530066f913aSAlexander Motin 		cts.type = CTS_TYPE_USER_SETTINGS;
531066f913aSAlexander Motin 		xpt_action((union ccb *)&cts);
532066f913aSAlexander Motin 		if (path->device->transport == XPORT_ATA) {
533066f913aSAlexander Motin 			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
534066f913aSAlexander Motin 				bytecount = cts.xport_specific.ata.bytecount;
535066f913aSAlexander Motin 		} else {
536066f913aSAlexander Motin 			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
537066f913aSAlexander Motin 				bytecount = cts.xport_specific.sata.bytecount;
538066f913aSAlexander Motin 		}
539066f913aSAlexander Motin 		/* Honor device capabilities. */
540066f913aSAlexander Motin 		bytecount &= ~1;
541066f913aSAlexander Motin 		bytecount = max(2, min(65534, bytecount));
542066f913aSAlexander Motin 		if (ident_buf->satacapabilities != 0x0000 &&
543066f913aSAlexander Motin 		    ident_buf->satacapabilities != 0xffff) {
544066f913aSAlexander Motin 			bytecount = min(8192, bytecount);
545066f913aSAlexander Motin 		}
546066f913aSAlexander Motin 		/* Report bytecount to SIM. */
547066f913aSAlexander Motin 		bzero(&cts, sizeof(cts));
54883c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
549066f913aSAlexander Motin 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
550066f913aSAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
551066f913aSAlexander Motin 		if (path->device->transport == XPORT_ATA) {
552066f913aSAlexander Motin 			cts.xport_specific.ata.bytecount = bytecount;
553066f913aSAlexander Motin 			cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT;
554066f913aSAlexander Motin 		} else {
555066f913aSAlexander Motin 			cts.xport_specific.sata.bytecount = bytecount;
556066f913aSAlexander Motin 			cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT;
557066f913aSAlexander Motin 		}
558066f913aSAlexander Motin 		xpt_action((union ccb *)&cts);
559066f913aSAlexander Motin 		/* FALLTHROUGH */
560066f913aSAlexander Motin 	}
56152c9ce25SScott Long 	case PROBE_FULL_INQUIRY:
56252c9ce25SScott Long 	{
56352c9ce25SScott Long 		u_int inquiry_len;
56452c9ce25SScott Long 		struct scsi_inquiry_data *inq_buf =
56552c9ce25SScott Long 		    &periph->path->device->inq_data;
56652c9ce25SScott Long 
56752c9ce25SScott Long 		if (softc->action == PROBE_INQUIRY)
56852c9ce25SScott Long 			inquiry_len = SHORT_INQUIRY_LENGTH;
56952c9ce25SScott Long 		else
57052c9ce25SScott Long 			inquiry_len = SID_ADDITIONAL_LENGTH(inq_buf);
57152c9ce25SScott Long 		/*
57252c9ce25SScott Long 		 * Some parallel SCSI devices fail to send an
57352c9ce25SScott Long 		 * ignore wide residue message when dealing with
57452c9ce25SScott Long 		 * odd length inquiry requests.  Round up to be
57552c9ce25SScott Long 		 * safe.
57652c9ce25SScott Long 		 */
57752c9ce25SScott Long 		inquiry_len = roundup2(inquiry_len, 2);
57852c9ce25SScott Long 		scsi_inquiry(csio,
57952c9ce25SScott Long 			     /*retries*/1,
58052c9ce25SScott Long 			     probedone,
58152c9ce25SScott Long 			     MSG_SIMPLE_Q_TAG,
58252c9ce25SScott Long 			     (u_int8_t *)inq_buf,
58352c9ce25SScott Long 			     inquiry_len,
58452c9ce25SScott Long 			     /*evpd*/FALSE,
58552c9ce25SScott Long 			     /*page_code*/0,
58652c9ce25SScott Long 			     SSD_MIN_SIZE,
58752c9ce25SScott Long 			     /*timeout*/60 * 1000);
58852c9ce25SScott Long 		break;
58952c9ce25SScott Long 	}
59052c9ce25SScott Long 	case PROBE_PM_PID:
59152c9ce25SScott Long 		cam_fill_ataio(ataio,
59252c9ce25SScott Long 		      1,
59352c9ce25SScott Long 		      probedone,
59452c9ce25SScott Long 		      /*flags*/CAM_DIR_NONE,
59565d0fb03SAlexander Motin 		      0,
59652c9ce25SScott Long 		      /*data_ptr*/NULL,
59752c9ce25SScott Long 		      /*dxfer_len*/0,
59852c9ce25SScott Long 		      10 * 1000);
59952c9ce25SScott Long 		ata_pm_read_cmd(ataio, 0, 15);
60052c9ce25SScott Long 		break;
60152c9ce25SScott Long 	case PROBE_PM_PRV:
60252c9ce25SScott Long 		cam_fill_ataio(ataio,
60352c9ce25SScott Long 		      1,
60452c9ce25SScott Long 		      probedone,
60552c9ce25SScott Long 		      /*flags*/CAM_DIR_NONE,
60665d0fb03SAlexander Motin 		      0,
60752c9ce25SScott Long 		      /*data_ptr*/NULL,
60852c9ce25SScott Long 		      /*dxfer_len*/0,
60952c9ce25SScott Long 		      10 * 1000);
61052c9ce25SScott Long 		ata_pm_read_cmd(ataio, 1, 15);
61152c9ce25SScott Long 		break;
61252c9ce25SScott Long 	case PROBE_INVALID:
6131e637ba6SAlexander Motin 		CAM_DEBUG(path, CAM_DEBUG_INFO,
61452c9ce25SScott Long 		    ("probestart: invalid action state\n"));
61552c9ce25SScott Long 	default:
61652c9ce25SScott Long 		break;
61752c9ce25SScott Long 	}
61852c9ce25SScott Long 	xpt_action(start_ccb);
61952c9ce25SScott Long }
62052c9ce25SScott Long #if 0
62152c9ce25SScott Long static void
62252c9ce25SScott Long proberequestdefaultnegotiation(struct cam_periph *periph)
62352c9ce25SScott Long {
62452c9ce25SScott Long 	struct ccb_trans_settings cts;
62552c9ce25SScott Long 
62683c5d981SAlexander Motin 	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
62752c9ce25SScott Long 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
62852c9ce25SScott Long 	cts.type = CTS_TYPE_USER_SETTINGS;
62952c9ce25SScott Long 	xpt_action((union ccb *)&cts);
63052c9ce25SScott Long 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
63152c9ce25SScott Long 		return;
63252c9ce25SScott Long 	}
63352c9ce25SScott Long 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
63452c9ce25SScott Long 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
63552c9ce25SScott Long 	xpt_action((union ccb *)&cts);
63652c9ce25SScott Long }
63752c9ce25SScott Long 
63852c9ce25SScott Long /*
63952c9ce25SScott Long  * Backoff Negotiation Code- only pertinent for SPI devices.
64052c9ce25SScott Long  */
64152c9ce25SScott Long static int
64252c9ce25SScott Long proberequestbackoff(struct cam_periph *periph, struct cam_ed *device)
64352c9ce25SScott Long {
64452c9ce25SScott Long 	struct ccb_trans_settings cts;
64552c9ce25SScott Long 	struct ccb_trans_settings_spi *spi;
64652c9ce25SScott Long 
64752c9ce25SScott Long 	memset(&cts, 0, sizeof (cts));
64883c5d981SAlexander Motin 	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
64952c9ce25SScott Long 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
65052c9ce25SScott Long 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
65152c9ce25SScott Long 	xpt_action((union ccb *)&cts);
65252c9ce25SScott Long 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
65352c9ce25SScott Long 		if (bootverbose) {
65452c9ce25SScott Long 			xpt_print(periph->path,
65552c9ce25SScott Long 			    "failed to get current device settings\n");
65652c9ce25SScott Long 		}
65752c9ce25SScott Long 		return (0);
65852c9ce25SScott Long 	}
65952c9ce25SScott Long 	if (cts.transport != XPORT_SPI) {
66052c9ce25SScott Long 		if (bootverbose) {
66152c9ce25SScott Long 			xpt_print(periph->path, "not SPI transport\n");
66252c9ce25SScott Long 		}
66352c9ce25SScott Long 		return (0);
66452c9ce25SScott Long 	}
66552c9ce25SScott Long 	spi = &cts.xport_specific.spi;
66652c9ce25SScott Long 
66752c9ce25SScott Long 	/*
66852c9ce25SScott Long 	 * We cannot renegotiate sync rate if we don't have one.
66952c9ce25SScott Long 	 */
67052c9ce25SScott Long 	if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
67152c9ce25SScott Long 		if (bootverbose) {
67252c9ce25SScott Long 			xpt_print(periph->path, "no sync rate known\n");
67352c9ce25SScott Long 		}
67452c9ce25SScott Long 		return (0);
67552c9ce25SScott Long 	}
67652c9ce25SScott Long 
67752c9ce25SScott Long 	/*
67852c9ce25SScott Long 	 * We'll assert that we don't have to touch PPR options- the
67952c9ce25SScott Long 	 * SIM will see what we do with period and offset and adjust
68052c9ce25SScott Long 	 * the PPR options as appropriate.
68152c9ce25SScott Long 	 */
68252c9ce25SScott Long 
68352c9ce25SScott Long 	/*
68452c9ce25SScott Long 	 * A sync rate with unknown or zero offset is nonsensical.
68552c9ce25SScott Long 	 * A sync period of zero means Async.
68652c9ce25SScott Long 	 */
68752c9ce25SScott Long 	if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0
68852c9ce25SScott Long 	 || spi->sync_offset == 0 || spi->sync_period == 0) {
68952c9ce25SScott Long 		if (bootverbose) {
69052c9ce25SScott Long 			xpt_print(periph->path, "no sync rate available\n");
69152c9ce25SScott Long 		}
69252c9ce25SScott Long 		return (0);
69352c9ce25SScott Long 	}
69452c9ce25SScott Long 
69552c9ce25SScott Long 	if (device->flags & CAM_DEV_DV_HIT_BOTTOM) {
69652c9ce25SScott Long 		CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
69752c9ce25SScott Long 		    ("hit async: giving up on DV\n"));
69852c9ce25SScott Long 		return (0);
69952c9ce25SScott Long 	}
70052c9ce25SScott Long 
70152c9ce25SScott Long 
70252c9ce25SScott Long 	/*
70352c9ce25SScott Long 	 * Jump sync_period up by one, but stop at 5MHz and fall back to Async.
70452c9ce25SScott Long 	 * We don't try to remember 'last' settings to see if the SIM actually
70552c9ce25SScott Long 	 * gets into the speed we want to set. We check on the SIM telling
70652c9ce25SScott Long 	 * us that a requested speed is bad, but otherwise don't try and
70752c9ce25SScott Long 	 * check the speed due to the asynchronous and handshake nature
70852c9ce25SScott Long 	 * of speed setting.
70952c9ce25SScott Long 	 */
71052c9ce25SScott Long 	spi->valid = CTS_SPI_VALID_SYNC_RATE | CTS_SPI_VALID_SYNC_OFFSET;
71152c9ce25SScott Long 	for (;;) {
71252c9ce25SScott Long 		spi->sync_period++;
71352c9ce25SScott Long 		if (spi->sync_period >= 0xf) {
71452c9ce25SScott Long 			spi->sync_period = 0;
71552c9ce25SScott Long 			spi->sync_offset = 0;
71652c9ce25SScott Long 			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
71752c9ce25SScott Long 			    ("setting to async for DV\n"));
71852c9ce25SScott Long 			/*
71952c9ce25SScott Long 			 * Once we hit async, we don't want to try
72052c9ce25SScott Long 			 * any more settings.
72152c9ce25SScott Long 			 */
72252c9ce25SScott Long 			device->flags |= CAM_DEV_DV_HIT_BOTTOM;
72352c9ce25SScott Long 		} else if (bootverbose) {
72452c9ce25SScott Long 			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
72552c9ce25SScott Long 			    ("DV: period 0x%x\n", spi->sync_period));
72652c9ce25SScott Long 			printf("setting period to 0x%x\n", spi->sync_period);
72752c9ce25SScott Long 		}
72852c9ce25SScott Long 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
72952c9ce25SScott Long 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
73052c9ce25SScott Long 		xpt_action((union ccb *)&cts);
73152c9ce25SScott Long 		if ((cts.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
73252c9ce25SScott Long 			break;
73352c9ce25SScott Long 		}
73452c9ce25SScott Long 		CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
73552c9ce25SScott Long 		    ("DV: failed to set period 0x%x\n", spi->sync_period));
73652c9ce25SScott Long 		if (spi->sync_period == 0) {
73752c9ce25SScott Long 			return (0);
73852c9ce25SScott Long 		}
73952c9ce25SScott Long 	}
74052c9ce25SScott Long 	return (1);
74152c9ce25SScott Long }
74252c9ce25SScott Long #endif
74352c9ce25SScott Long static void
74452c9ce25SScott Long probedone(struct cam_periph *periph, union ccb *done_ccb)
74552c9ce25SScott Long {
746c8039fc6SAlexander Motin 	struct ccb_trans_settings cts;
74752c9ce25SScott Long 	struct ata_params *ident_buf;
74852c9ce25SScott Long 	probe_softc *softc;
74952c9ce25SScott Long 	struct cam_path *path;
75026bdaeddSAlexander Motin 	cam_status status;
75152c9ce25SScott Long 	u_int32_t  priority;
752da6808c1SAlexander Motin 	u_int caps;
75365d0fb03SAlexander Motin 	int found = 1;
75452c9ce25SScott Long 
75552c9ce25SScott Long 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
75652c9ce25SScott Long 
75752c9ce25SScott Long 	softc = (probe_softc *)periph->softc;
75852c9ce25SScott Long 	path = done_ccb->ccb_h.path;
75952c9ce25SScott Long 	priority = done_ccb->ccb_h.pinfo.priority;
76052c9ce25SScott Long 	ident_buf = &path->device->ident_data;
76152c9ce25SScott Long 
7621e637ba6SAlexander Motin 	if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
76325a519a9SAlexander Motin 		if (softc->restart) {
76425a519a9SAlexander Motin 			if (bootverbose) {
76525a519a9SAlexander Motin 				cam_error_print(done_ccb,
76625a519a9SAlexander Motin 				    CAM_ESF_ALL, CAM_EPF_ALL);
76725a519a9SAlexander Motin 			}
76825a519a9SAlexander Motin 		} else if (cam_periph_error(done_ccb, 0, 0, NULL) == ERESTART)
7691e637ba6SAlexander Motin 			return;
77025a519a9SAlexander Motin 		if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
7711e637ba6SAlexander Motin 			/* Don't wedge the queue */
7721e637ba6SAlexander Motin 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
7731e637ba6SAlexander Motin 					 /*run_queue*/TRUE);
7741e637ba6SAlexander Motin 		}
77526bdaeddSAlexander Motin 		status = done_ccb->ccb_h.status & CAM_STATUS_MASK;
77625a519a9SAlexander Motin 		if (softc->restart) {
77725a519a9SAlexander Motin 			softc->faults++;
77825a519a9SAlexander Motin 			if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) ==
77925a519a9SAlexander Motin 			    CAM_CMD_TIMEOUT)
78025a519a9SAlexander Motin 				softc->faults += 4;
78125a519a9SAlexander Motin 			if (softc->faults < 10)
78225a519a9SAlexander Motin 				goto done;
78325a519a9SAlexander Motin 			else
78425a519a9SAlexander Motin 				softc->restart = 0;
78526bdaeddSAlexander Motin 
7861e637ba6SAlexander Motin 		/* Old PIO2 devices may not support mode setting. */
78726bdaeddSAlexander Motin 		} else if (softc->action == PROBE_SETMODE &&
78826bdaeddSAlexander Motin 		    status == CAM_ATA_STATUS_ERROR &&
7891e637ba6SAlexander Motin 		    ata_max_pmode(ident_buf) <= ATA_PIO2 &&
79026bdaeddSAlexander Motin 		    (ident_buf->capabilities1 & ATA_SUPPORT_IORDY) == 0) {
7911e637ba6SAlexander Motin 			goto noerror;
79226bdaeddSAlexander Motin 
79326bdaeddSAlexander Motin 		/*
79426bdaeddSAlexander Motin 		 * Some old WD SATA disks report supported and enabled
79526bdaeddSAlexander Motin 		 * device-initiated interface power management, but return
79626bdaeddSAlexander Motin 		 * ABORT on attempt to disable it.
79726bdaeddSAlexander Motin 		 */
79826bdaeddSAlexander Motin 		} else if (softc->action == PROBE_SETPM &&
79926bdaeddSAlexander Motin 		    status == CAM_ATA_STATUS_ERROR) {
80026bdaeddSAlexander Motin 			goto noerror;
801025e2c12SAlexander Motin 
802025e2c12SAlexander Motin 		/*
803025e2c12SAlexander Motin 		 * Some HP SATA disks report supported DMA Auto-Activation,
804025e2c12SAlexander Motin 		 * but return ABORT on attempt to enable it.
805025e2c12SAlexander Motin 		 */
806025e2c12SAlexander Motin 		} else if (softc->action == PROBE_SETDMAAA &&
807025e2c12SAlexander Motin 		    status == CAM_ATA_STATUS_ERROR) {
808025e2c12SAlexander Motin 			goto noerror;
80926bdaeddSAlexander Motin 		}
81026bdaeddSAlexander Motin 
8111e637ba6SAlexander Motin 		/*
8121e637ba6SAlexander Motin 		 * If we get to this point, we got an error status back
8131e637ba6SAlexander Motin 		 * from the inquiry and the error status doesn't require
8141e637ba6SAlexander Motin 		 * automatically retrying the command.  Therefore, the
8151e637ba6SAlexander Motin 		 * inquiry failed.  If we had inquiry information before
8161e637ba6SAlexander Motin 		 * for this device, but this latest inquiry command failed,
8171e637ba6SAlexander Motin 		 * the device has probably gone away.  If this device isn't
8181e637ba6SAlexander Motin 		 * already marked unconfigured, notify the peripheral
8191e637ba6SAlexander Motin 		 * drivers that this device is no more.
8201e637ba6SAlexander Motin 		 */
82125a519a9SAlexander Motin device_fail:	if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
8221e637ba6SAlexander Motin 			xpt_async(AC_LOST_DEVICE, path, NULL);
8231e637ba6SAlexander Motin 		found = 0;
8241e637ba6SAlexander Motin 		goto done;
8251e637ba6SAlexander Motin 	}
8261e637ba6SAlexander Motin noerror:
827a9b8edb1SAlexander Motin 	if (softc->restart)
828a9b8edb1SAlexander Motin 		goto done;
82952c9ce25SScott Long 	switch (softc->action) {
83052c9ce25SScott Long 	case PROBE_RESET:
8311e637ba6SAlexander Motin 	{
83252c9ce25SScott Long 		int sign = (done_ccb->ataio.res.lba_high << 8) +
83352c9ce25SScott Long 		    done_ccb->ataio.res.lba_mid;
8347bdb664eSAlexander Motin 		if (bootverbose)
83552c9ce25SScott Long 			xpt_print(path, "SIGNATURE: %04x\n", sign);
83652c9ce25SScott Long 		if (sign == 0x0000 &&
83752c9ce25SScott Long 		    done_ccb->ccb_h.target_id != 15) {
83852c9ce25SScott Long 			path->device->protocol = PROTO_ATA;
83952c9ce25SScott Long 			PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
84052c9ce25SScott Long 		} else if (sign == 0x9669 &&
84152c9ce25SScott Long 		    done_ccb->ccb_h.target_id == 15) {
84252c9ce25SScott Long 			/* Report SIM that PM is present. */
84352c9ce25SScott Long 			bzero(&cts, sizeof(cts));
84483c5d981SAlexander Motin 			xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
84552c9ce25SScott Long 			cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
84652c9ce25SScott Long 			cts.type = CTS_TYPE_CURRENT_SETTINGS;
84752c9ce25SScott Long 			cts.xport_specific.sata.pm_present = 1;
84852c9ce25SScott Long 			cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
84952c9ce25SScott Long 			xpt_action((union ccb *)&cts);
85052c9ce25SScott Long 			path->device->protocol = PROTO_SATAPM;
85152c9ce25SScott Long 			PROBE_SET_ACTION(softc, PROBE_PM_PID);
85252c9ce25SScott Long 		} else if (sign == 0xeb14 &&
85352c9ce25SScott Long 		    done_ccb->ccb_h.target_id != 15) {
85452c9ce25SScott Long 			path->device->protocol = PROTO_SCSI;
85552c9ce25SScott Long 			PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
85652c9ce25SScott Long 		} else {
85752c9ce25SScott Long 			if (done_ccb->ccb_h.target_id != 15) {
85852c9ce25SScott Long 				xpt_print(path,
85952c9ce25SScott Long 				    "Unexpected signature 0x%04x\n", sign);
86052c9ce25SScott Long 			}
86165d0fb03SAlexander Motin 			goto device_fail;
86252c9ce25SScott Long 		}
86352c9ce25SScott Long 		xpt_release_ccb(done_ccb);
86452c9ce25SScott Long 		xpt_schedule(periph, priority);
86552c9ce25SScott Long 		return;
86652c9ce25SScott Long 	}
86752c9ce25SScott Long 	case PROBE_IDENTIFY:
86852c9ce25SScott Long 	{
869699f853bSAlexander Motin 		struct ccb_pathinq cpi;
87052c9ce25SScott Long 		int16_t *ptr;
8712eb4a8dcSAlexander Motin 		int changed = 1;
87252c9ce25SScott Long 
873a9b8edb1SAlexander Motin 		ident_buf = &softc->ident_data;
87452c9ce25SScott Long 		for (ptr = (int16_t *)ident_buf;
87552c9ce25SScott Long 		     ptr < (int16_t *)ident_buf + sizeof(struct ata_params)/2; ptr++) {
87652c9ce25SScott Long 			*ptr = le16toh(*ptr);
87752c9ce25SScott Long 		}
87852c9ce25SScott Long 		if (strncmp(ident_buf->model, "FX", 2) &&
87952c9ce25SScott Long 		    strncmp(ident_buf->model, "NEC", 3) &&
88052c9ce25SScott Long 		    strncmp(ident_buf->model, "Pioneer", 7) &&
88152c9ce25SScott Long 		    strncmp(ident_buf->model, "SHARP", 5)) {
88252c9ce25SScott Long 			ata_bswap(ident_buf->model, sizeof(ident_buf->model));
88352c9ce25SScott Long 			ata_bswap(ident_buf->revision, sizeof(ident_buf->revision));
88452c9ce25SScott Long 			ata_bswap(ident_buf->serial, sizeof(ident_buf->serial));
88552c9ce25SScott Long 		}
88652c9ce25SScott Long 		ata_btrim(ident_buf->model, sizeof(ident_buf->model));
88752c9ce25SScott Long 		ata_bpack(ident_buf->model, ident_buf->model, sizeof(ident_buf->model));
88852c9ce25SScott Long 		ata_btrim(ident_buf->revision, sizeof(ident_buf->revision));
88952c9ce25SScott Long 		ata_bpack(ident_buf->revision, ident_buf->revision, sizeof(ident_buf->revision));
89052c9ce25SScott Long 		ata_btrim(ident_buf->serial, sizeof(ident_buf->serial));
89152c9ce25SScott Long 		ata_bpack(ident_buf->serial, ident_buf->serial, sizeof(ident_buf->serial));
8924ef08dc5SAlexander Motin 		/* Device may need spin-up before IDENTIFY become valid. */
8931254b680SAlexander Motin 		if ((ident_buf->specconf == 0x37c8 ||
8941254b680SAlexander Motin 		     ident_buf->specconf == 0x738c) &&
8951254b680SAlexander Motin 		    ((ident_buf->config & ATA_RESP_INCOMPLETE) ||
8964ef08dc5SAlexander Motin 		     softc->spinup == 0)) {
8974ef08dc5SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_SPINUP);
8984ef08dc5SAlexander Motin 			xpt_release_ccb(done_ccb);
8994ef08dc5SAlexander Motin 			xpt_schedule(periph, priority);
9004ef08dc5SAlexander Motin 			return;
9014ef08dc5SAlexander Motin 		}
902a9b8edb1SAlexander Motin 		ident_buf = &path->device->ident_data;
90352c9ce25SScott Long 		if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
90452c9ce25SScott Long 			/* Check that it is the same device. */
905a9b8edb1SAlexander Motin 			if (bcmp(softc->ident_data.model, ident_buf->model,
906a9b8edb1SAlexander Motin 			     sizeof(ident_buf->model)) ||
907a9b8edb1SAlexander Motin 			    bcmp(softc->ident_data.revision, ident_buf->revision,
908a9b8edb1SAlexander Motin 			     sizeof(ident_buf->revision)) ||
909a9b8edb1SAlexander Motin 			    bcmp(softc->ident_data.serial, ident_buf->serial,
910a9b8edb1SAlexander Motin 			     sizeof(ident_buf->serial))) {
91152c9ce25SScott Long 				/* Device changed. */
91252c9ce25SScott Long 				xpt_async(AC_LOST_DEVICE, path, NULL);
9131e637ba6SAlexander Motin 			} else {
914a9b8edb1SAlexander Motin 				bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
9152eb4a8dcSAlexander Motin 				changed = 0;
9162eb4a8dcSAlexander Motin 			}
9172eb4a8dcSAlexander Motin 		}
9182eb4a8dcSAlexander Motin 		if (changed) {
9192eb4a8dcSAlexander Motin 			bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
92052c9ce25SScott Long 			/* Clean up from previous instance of this device */
92152c9ce25SScott Long 			if (path->device->serial_num != NULL) {
92252c9ce25SScott Long 				free(path->device->serial_num, M_CAMXPT);
92352c9ce25SScott Long 				path->device->serial_num = NULL;
92452c9ce25SScott Long 				path->device->serial_num_len = 0;
92552c9ce25SScott Long 			}
92652c9ce25SScott Long 			path->device->serial_num =
92752c9ce25SScott Long 				(u_int8_t *)malloc((sizeof(ident_buf->serial) + 1),
92852c9ce25SScott Long 					   M_CAMXPT, M_NOWAIT);
92952c9ce25SScott Long 			if (path->device->serial_num != NULL) {
93052c9ce25SScott Long 				bcopy(ident_buf->serial,
93152c9ce25SScott Long 				      path->device->serial_num,
93252c9ce25SScott Long 				      sizeof(ident_buf->serial));
93352c9ce25SScott Long 				path->device->serial_num[sizeof(ident_buf->serial)]
93452c9ce25SScott Long 				    = '\0';
93552c9ce25SScott Long 				path->device->serial_num_len =
93652c9ce25SScott Long 				    strlen(path->device->serial_num);
93752c9ce25SScott Long 			}
93852c9ce25SScott Long 
9394b997c49SAlexander Motin 			path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
9401e637ba6SAlexander Motin 		}
94130a4094fSAlexander Motin 		if (ident_buf->satacapabilities & ATA_SUPPORT_NCQ) {
94230a4094fSAlexander Motin 			path->device->mintags = path->device->maxtags =
94330a4094fSAlexander Motin 			    ATA_QUEUE_LEN(ident_buf->queue) + 1;
94430a4094fSAlexander Motin 		}
94530a4094fSAlexander Motin 		ata_find_quirk(path->device);
946507e5811SAlexander Motin 		if (path->device->mintags != 0 &&
947507e5811SAlexander Motin 		    path->bus->sim->max_tagged_dev_openings != 0) {
948699f853bSAlexander Motin 			/* Check if the SIM does not want queued commands. */
949699f853bSAlexander Motin 			bzero(&cpi, sizeof(cpi));
950699f853bSAlexander Motin 			xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
951699f853bSAlexander Motin 			cpi.ccb_h.func_code = XPT_PATH_INQ;
952699f853bSAlexander Motin 			xpt_action((union ccb *)&cpi);
953699f853bSAlexander Motin 			if (cpi.ccb_h.status == CAM_REQ_CMP &&
954699f853bSAlexander Motin 			    (cpi.hba_inquiry & PI_TAG_ABLE)) {
955c8039fc6SAlexander Motin 				/* Report SIM which tags are allowed. */
956c8039fc6SAlexander Motin 				bzero(&cts, sizeof(cts));
95783c5d981SAlexander Motin 				xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
958c8039fc6SAlexander Motin 				cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
959c8039fc6SAlexander Motin 				cts.type = CTS_TYPE_CURRENT_SETTINGS;
960c8039fc6SAlexander Motin 				cts.xport_specific.sata.tags = path->device->maxtags;
961c8039fc6SAlexander Motin 				cts.xport_specific.sata.valid = CTS_SATA_VALID_TAGS;
962c8039fc6SAlexander Motin 				xpt_action((union ccb *)&cts);
963c8039fc6SAlexander Motin 				/* Reconfigure queues for tagged queueing. */
96430a4094fSAlexander Motin 				xpt_start_tags(path);
96530a4094fSAlexander Motin 			}
966699f853bSAlexander Motin 		}
96752c9ce25SScott Long 		ata_device_transport(path);
96852c9ce25SScott Long 		PROBE_SET_ACTION(softc, PROBE_SETMODE);
96952c9ce25SScott Long 		xpt_release_ccb(done_ccb);
97052c9ce25SScott Long 		xpt_schedule(periph, priority);
97152c9ce25SScott Long 		return;
97252c9ce25SScott Long 	}
9734ef08dc5SAlexander Motin 	case PROBE_SPINUP:
9744ef08dc5SAlexander Motin 		if (bootverbose)
9754ef08dc5SAlexander Motin 			xpt_print(path, "Spin-up done\n");
9764ef08dc5SAlexander Motin 		softc->spinup = 1;
9774ef08dc5SAlexander Motin 		PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
9784ef08dc5SAlexander Motin 		xpt_release_ccb(done_ccb);
9794ef08dc5SAlexander Motin 		xpt_schedule(periph, priority);
9804ef08dc5SAlexander Motin 		return;
98152c9ce25SScott Long 	case PROBE_SETMODE:
982da6808c1SAlexander Motin 		if (path->device->transport != XPORT_SATA)
983da6808c1SAlexander Motin 			goto notsata;
984da6808c1SAlexander Motin 		/* Set supported bits. */
985da6808c1SAlexander Motin 		bzero(&cts, sizeof(cts));
986da6808c1SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
987da6808c1SAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
988da6808c1SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
989da6808c1SAlexander Motin 		xpt_action((union ccb *)&cts);
990da6808c1SAlexander Motin 		if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
991da6808c1SAlexander Motin 			caps = cts.xport_specific.sata.caps & CTS_SATA_CAPS_H;
992da6808c1SAlexander Motin 		else
993da6808c1SAlexander Motin 			caps = 0;
994da6808c1SAlexander Motin 		if (ident_buf->satacapabilities != 0xffff) {
995da6808c1SAlexander Motin 			if (ident_buf->satacapabilities & ATA_SUPPORT_IFPWRMNGTRCV)
996da6808c1SAlexander Motin 				caps |= CTS_SATA_CAPS_D_PMREQ;
997da6808c1SAlexander Motin 			if (ident_buf->satacapabilities & ATA_SUPPORT_HAPST)
998da6808c1SAlexander Motin 				caps |= CTS_SATA_CAPS_D_APST;
999da6808c1SAlexander Motin 		}
1000da6808c1SAlexander Motin 		/* Mask unwanted bits. */
1001da6808c1SAlexander Motin 		bzero(&cts, sizeof(cts));
1002da6808c1SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1003da6808c1SAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1004da6808c1SAlexander Motin 		cts.type = CTS_TYPE_USER_SETTINGS;
1005da6808c1SAlexander Motin 		xpt_action((union ccb *)&cts);
1006da6808c1SAlexander Motin 		if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1007da6808c1SAlexander Motin 			caps &= cts.xport_specific.sata.caps;
1008c2b82f3eSAlexander Motin 		else
1009c2b82f3eSAlexander Motin 			caps = 0;
1010da6808c1SAlexander Motin 		/* Store result to SIM. */
1011da6808c1SAlexander Motin 		bzero(&cts, sizeof(cts));
1012da6808c1SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1013da6808c1SAlexander Motin 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1014da6808c1SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
1015da6808c1SAlexander Motin 		cts.xport_specific.sata.caps = caps;
1016da6808c1SAlexander Motin 		cts.xport_specific.sata.valid = CTS_SATA_VALID_CAPS;
1017da6808c1SAlexander Motin 		xpt_action((union ccb *)&cts);
1018da6808c1SAlexander Motin 		softc->caps = caps;
1019958e4a69SAlexander Motin 		if ((ident_buf->satasupport & ATA_SUPPORT_IFPWRMNGT) &&
1020958e4a69SAlexander Motin 		    (!(softc->caps & CTS_SATA_CAPS_H_PMREQ)) !=
1021958e4a69SAlexander Motin 		    (!(ident_buf->sataenabled & ATA_SUPPORT_IFPWRMNGT))) {
1022da6808c1SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_SETPM);
1023da6808c1SAlexander Motin 			xpt_release_ccb(done_ccb);
1024da6808c1SAlexander Motin 			xpt_schedule(periph, priority);
1025da6808c1SAlexander Motin 			return;
1026da6808c1SAlexander Motin 		}
1027da6808c1SAlexander Motin 		/* FALLTHROUGH */
1028da6808c1SAlexander Motin 	case PROBE_SETPM:
1029da6808c1SAlexander Motin 		if (ident_buf->satacapabilities != 0xffff &&
1030958e4a69SAlexander Motin 		    (ident_buf->satacapabilities & ATA_SUPPORT_DAPST) &&
1031958e4a69SAlexander Motin 		    (!(softc->caps & CTS_SATA_CAPS_H_APST)) !=
1032958e4a69SAlexander Motin 		    (!(ident_buf->sataenabled & ATA_ENABLED_DAPST))) {
1033da6808c1SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_SETAPST);
1034da6808c1SAlexander Motin 			xpt_release_ccb(done_ccb);
1035da6808c1SAlexander Motin 			xpt_schedule(periph, priority);
1036da6808c1SAlexander Motin 			return;
1037da6808c1SAlexander Motin 		}
1038da6808c1SAlexander Motin 		/* FALLTHROUGH */
1039da6808c1SAlexander Motin 	case PROBE_SETAPST:
1040958e4a69SAlexander Motin 		if ((ident_buf->satasupport & ATA_SUPPORT_AUTOACTIVATE) &&
1041958e4a69SAlexander Motin 		    (!(softc->caps & CTS_SATA_CAPS_H_DMAAA)) !=
1042958e4a69SAlexander Motin 		    (!(ident_buf->sataenabled & ATA_SUPPORT_AUTOACTIVATE))) {
1043da6808c1SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_SETDMAAA);
1044da6808c1SAlexander Motin 			xpt_release_ccb(done_ccb);
1045da6808c1SAlexander Motin 			xpt_schedule(periph, priority);
1046da6808c1SAlexander Motin 			return;
1047da6808c1SAlexander Motin 		}
1048da6808c1SAlexander Motin 		/* FALLTHROUGH */
1049da6808c1SAlexander Motin 	case PROBE_SETDMAAA:
10508d169381SAlexander Motin 		if ((ident_buf->satasupport & ATA_SUPPORT_ASYNCNOTIF) &&
10518d169381SAlexander Motin 		    (!(softc->caps & CTS_SATA_CAPS_H_AN)) !=
10528d169381SAlexander Motin 		    (!(ident_buf->sataenabled & ATA_SUPPORT_ASYNCNOTIF))) {
10538d169381SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_SETAN);
10548d169381SAlexander Motin 			xpt_release_ccb(done_ccb);
10558d169381SAlexander Motin 			xpt_schedule(periph, priority);
10568d169381SAlexander Motin 			return;
10578d169381SAlexander Motin 		}
10588d169381SAlexander Motin 		/* FALLTHROUGH */
10598d169381SAlexander Motin 	case PROBE_SETAN:
1060da6808c1SAlexander Motin notsata:
10611e637ba6SAlexander Motin 		if (path->device->protocol == PROTO_ATA) {
10621e637ba6SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_SET_MULTI);
10631e637ba6SAlexander Motin 		} else {
10641e637ba6SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_INQUIRY);
10651e637ba6SAlexander Motin 		}
10661e637ba6SAlexander Motin 		xpt_release_ccb(done_ccb);
10671e637ba6SAlexander Motin 		xpt_schedule(periph, priority);
10681e637ba6SAlexander Motin 		return;
10691e637ba6SAlexander Motin 	case PROBE_SET_MULTI:
10701e637ba6SAlexander Motin 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
107152c9ce25SScott Long 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1072f98d7a47SAlexander Motin 			xpt_acquire_device(path->device);
107352c9ce25SScott Long 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
107452c9ce25SScott Long 			xpt_action(done_ccb);
107552c9ce25SScott Long 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
107652c9ce25SScott Long 			    done_ccb);
10771e637ba6SAlexander Motin 		}
107852c9ce25SScott Long 		break;
107952c9ce25SScott Long 	case PROBE_INQUIRY:
108052c9ce25SScott Long 	case PROBE_FULL_INQUIRY:
108152c9ce25SScott Long 	{
108252c9ce25SScott Long 		struct scsi_inquiry_data *inq_buf;
10831e637ba6SAlexander Motin 		u_int8_t periph_qual, len;
108452c9ce25SScott Long 
108552c9ce25SScott Long 		path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
108652c9ce25SScott Long 		inq_buf = &path->device->inq_data;
108752c9ce25SScott Long 
108852c9ce25SScott Long 		periph_qual = SID_QUAL(inq_buf);
108952c9ce25SScott Long 
10901e637ba6SAlexander Motin 		if (periph_qual != SID_QUAL_LU_CONNECTED)
10911e637ba6SAlexander Motin 			break;
109252c9ce25SScott Long 
109352c9ce25SScott Long 		/*
109452c9ce25SScott Long 		 * We conservatively request only
109552c9ce25SScott Long 		 * SHORT_INQUIRY_LEN bytes of inquiry
109652c9ce25SScott Long 		 * information during our first try
109752c9ce25SScott Long 		 * at sending an INQUIRY. If the device
109852c9ce25SScott Long 		 * has more information to give,
109952c9ce25SScott Long 		 * perform a second request specifying
110052c9ce25SScott Long 		 * the amount of information the device
110152c9ce25SScott Long 		 * is willing to give.
110252c9ce25SScott Long 		 */
110352c9ce25SScott Long 		len = inq_buf->additional_length
11041e637ba6SAlexander Motin 		    + offsetof(struct scsi_inquiry_data, additional_length) + 1;
110552c9ce25SScott Long 		if (softc->action == PROBE_INQUIRY
110652c9ce25SScott Long 		    && len > SHORT_INQUIRY_LENGTH) {
110752c9ce25SScott Long 			PROBE_SET_ACTION(softc, PROBE_FULL_INQUIRY);
110852c9ce25SScott Long 			xpt_release_ccb(done_ccb);
110952c9ce25SScott Long 			xpt_schedule(periph, priority);
111052c9ce25SScott Long 			return;
111152c9ce25SScott Long 		}
111252c9ce25SScott Long 
11134b997c49SAlexander Motin 		ata_device_transport(path);
11141e637ba6SAlexander Motin 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
111552c9ce25SScott Long 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1116f98d7a47SAlexander Motin 			xpt_acquire_device(path->device);
111752c9ce25SScott Long 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
111852c9ce25SScott Long 			xpt_action(done_ccb);
11191e637ba6SAlexander Motin 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path, done_ccb);
11201e637ba6SAlexander Motin 		}
112152c9ce25SScott Long 		break;
112252c9ce25SScott Long 	}
112352c9ce25SScott Long 	case PROBE_PM_PID:
11244b997c49SAlexander Motin 		if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) == 0)
112552c9ce25SScott Long 			bzero(ident_buf, sizeof(*ident_buf));
112652c9ce25SScott Long 		softc->pm_pid = (done_ccb->ataio.res.lba_high << 24) +
112752c9ce25SScott Long 		    (done_ccb->ataio.res.lba_mid << 16) +
112852c9ce25SScott Long 		    (done_ccb->ataio.res.lba_low << 8) +
112952c9ce25SScott Long 		    done_ccb->ataio.res.sector_count;
113065d0fb03SAlexander Motin 		((uint32_t *)ident_buf)[0] = softc->pm_pid;
113152c9ce25SScott Long 		snprintf(ident_buf->model, sizeof(ident_buf->model),
113252c9ce25SScott Long 		    "Port Multiplier %08x", softc->pm_pid);
113352c9ce25SScott Long 		PROBE_SET_ACTION(softc, PROBE_PM_PRV);
113452c9ce25SScott Long 		xpt_release_ccb(done_ccb);
113552c9ce25SScott Long 		xpt_schedule(periph, priority);
113652c9ce25SScott Long 		return;
113752c9ce25SScott Long 	case PROBE_PM_PRV:
113852c9ce25SScott Long 		softc->pm_prv = (done_ccb->ataio.res.lba_high << 24) +
113952c9ce25SScott Long 		    (done_ccb->ataio.res.lba_mid << 16) +
114052c9ce25SScott Long 		    (done_ccb->ataio.res.lba_low << 8) +
114152c9ce25SScott Long 		    done_ccb->ataio.res.sector_count;
114265d0fb03SAlexander Motin 		((uint32_t *)ident_buf)[1] = softc->pm_prv;
114352c9ce25SScott Long 		snprintf(ident_buf->revision, sizeof(ident_buf->revision),
114452c9ce25SScott Long 		    "%04x", softc->pm_prv);
11454b997c49SAlexander Motin 		path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
1146da6808c1SAlexander Motin 		/* Set supported bits. */
1147da6808c1SAlexander Motin 		bzero(&cts, sizeof(cts));
1148da6808c1SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1149da6808c1SAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1150da6808c1SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
1151da6808c1SAlexander Motin 		xpt_action((union ccb *)&cts);
1152da6808c1SAlexander Motin 		if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1153da6808c1SAlexander Motin 			caps = cts.xport_specific.sata.caps & CTS_SATA_CAPS_H;
1154da6808c1SAlexander Motin 		else
1155da6808c1SAlexander Motin 			caps = 0;
1156da6808c1SAlexander Motin 		/* All PMPs must support PM requests. */
1157da6808c1SAlexander Motin 		caps |= CTS_SATA_CAPS_D_PMREQ;
1158da6808c1SAlexander Motin 		/* Mask unwanted bits. */
1159da6808c1SAlexander Motin 		bzero(&cts, sizeof(cts));
1160da6808c1SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1161da6808c1SAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1162da6808c1SAlexander Motin 		cts.type = CTS_TYPE_USER_SETTINGS;
1163da6808c1SAlexander Motin 		xpt_action((union ccb *)&cts);
1164da6808c1SAlexander Motin 		if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1165da6808c1SAlexander Motin 			caps &= cts.xport_specific.sata.caps;
1166c2b82f3eSAlexander Motin 		else
1167c2b82f3eSAlexander Motin 			caps = 0;
1168da6808c1SAlexander Motin 		/* Store result to SIM. */
1169da6808c1SAlexander Motin 		bzero(&cts, sizeof(cts));
1170da6808c1SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1171da6808c1SAlexander Motin 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1172da6808c1SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
1173da6808c1SAlexander Motin 		cts.xport_specific.sata.caps = caps;
1174da6808c1SAlexander Motin 		cts.xport_specific.sata.valid = CTS_SATA_VALID_CAPS;
1175da6808c1SAlexander Motin 		xpt_action((union ccb *)&cts);
1176da6808c1SAlexander Motin 		softc->caps = caps;
117765d0fb03SAlexander Motin 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
117852c9ce25SScott Long 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1179f98d7a47SAlexander Motin 			xpt_acquire_device(path->device);
118052c9ce25SScott Long 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
118152c9ce25SScott Long 			xpt_action(done_ccb);
118252c9ce25SScott Long 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
118352c9ce25SScott Long 			    done_ccb);
118465d0fb03SAlexander Motin 		} else {
118565d0fb03SAlexander Motin 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
118665d0fb03SAlexander Motin 			xpt_action(done_ccb);
11871e637ba6SAlexander Motin 			xpt_async(AC_SCSI_AEN, done_ccb->ccb_h.path, done_ccb);
118852c9ce25SScott Long 		}
118952c9ce25SScott Long 		break;
119052c9ce25SScott Long 	case PROBE_INVALID:
119152c9ce25SScott Long 		CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_INFO,
119252c9ce25SScott Long 		    ("probedone: invalid action state\n"));
119352c9ce25SScott Long 	default:
119452c9ce25SScott Long 		break;
119552c9ce25SScott Long 	}
11961e637ba6SAlexander Motin done:
119783c5d981SAlexander Motin 	if (softc->restart) {
119883c5d981SAlexander Motin 		softc->restart = 0;
11991e637ba6SAlexander Motin 		xpt_release_ccb(done_ccb);
120083c5d981SAlexander Motin 		probeschedule(periph);
120183c5d981SAlexander Motin 		return;
120283c5d981SAlexander Motin 	}
120383c5d981SAlexander Motin 	xpt_release_ccb(done_ccb);
120483c5d981SAlexander Motin 	while ((done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs))) {
120583c5d981SAlexander Motin 		TAILQ_REMOVE(&softc->request_ccbs,
120683c5d981SAlexander Motin 		    &done_ccb->ccb_h, periph_links.tqe);
120783c5d981SAlexander Motin 		done_ccb->ccb_h.status = found ? CAM_REQ_CMP : CAM_REQ_CMP_ERR;
120852c9ce25SScott Long 		xpt_done(done_ccb);
120983c5d981SAlexander Motin 	}
121083c5d981SAlexander Motin 	cam_release_devq(periph->path,
121183c5d981SAlexander Motin 	    RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_XPT + 1, FALSE);
121252c9ce25SScott Long 	cam_periph_invalidate(periph);
121352c9ce25SScott Long 	cam_periph_release_locked(periph);
121452c9ce25SScott Long }
121552c9ce25SScott Long 
121652c9ce25SScott Long static void
121752c9ce25SScott Long probecleanup(struct cam_periph *periph)
121852c9ce25SScott Long {
121952c9ce25SScott Long 	free(periph->softc, M_CAMXPT);
122052c9ce25SScott Long }
122152c9ce25SScott Long 
122252c9ce25SScott Long static void
122330a4094fSAlexander Motin ata_find_quirk(struct cam_ed *device)
122452c9ce25SScott Long {
122530a4094fSAlexander Motin 	struct ata_quirk_entry *quirk;
122652c9ce25SScott Long 	caddr_t	match;
122752c9ce25SScott Long 
122830a4094fSAlexander Motin 	match = cam_quirkmatch((caddr_t)&device->ident_data,
122930a4094fSAlexander Motin 			       (caddr_t)ata_quirk_table,
123030a4094fSAlexander Motin 			       ata_quirk_table_size,
123130a4094fSAlexander Motin 			       sizeof(*ata_quirk_table), ata_identify_match);
123252c9ce25SScott Long 
123352c9ce25SScott Long 	if (match == NULL)
123452c9ce25SScott Long 		panic("xpt_find_quirk: device didn't match wildcard entry!!");
123552c9ce25SScott Long 
123630a4094fSAlexander Motin 	quirk = (struct ata_quirk_entry *)match;
123752c9ce25SScott Long 	device->quirk = quirk;
123830a4094fSAlexander Motin 	if (quirk->quirks & CAM_QUIRK_MAXTAGS)
123930a4094fSAlexander Motin 		device->mintags = device->maxtags = quirk->maxtags;
124052c9ce25SScott Long }
124152c9ce25SScott Long 
124252c9ce25SScott Long typedef struct {
124352c9ce25SScott Long 	union	ccb *request_ccb;
124452c9ce25SScott Long 	struct 	ccb_pathinq *cpi;
124552c9ce25SScott Long 	int	counter;
124652c9ce25SScott Long } ata_scan_bus_info;
124752c9ce25SScott Long 
124852c9ce25SScott Long /*
124952c9ce25SScott Long  * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
125052c9ce25SScott Long  * As the scan progresses, xpt_scan_bus is used as the
125152c9ce25SScott Long  * callback on completion function.
125252c9ce25SScott Long  */
125352c9ce25SScott Long static void
125452c9ce25SScott Long ata_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
125552c9ce25SScott Long {
125652c9ce25SScott Long 	struct	cam_path *path;
125752c9ce25SScott Long 	ata_scan_bus_info *scan_info;
125883c5d981SAlexander Motin 	union	ccb *work_ccb, *reset_ccb;
125952c9ce25SScott Long 	cam_status status;
126052c9ce25SScott Long 
126152c9ce25SScott Long 	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
126252c9ce25SScott Long 		  ("xpt_scan_bus\n"));
126352c9ce25SScott Long 	switch (request_ccb->ccb_h.func_code) {
126452c9ce25SScott Long 	case XPT_SCAN_BUS:
12650e85f214SMatt Jacob 	case XPT_SCAN_TGT:
126652c9ce25SScott Long 		/* Find out the characteristics of the bus */
126752c9ce25SScott Long 		work_ccb = xpt_alloc_ccb_nowait();
126852c9ce25SScott Long 		if (work_ccb == NULL) {
126952c9ce25SScott Long 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
127052c9ce25SScott Long 			xpt_done(request_ccb);
127152c9ce25SScott Long 			return;
127252c9ce25SScott Long 		}
127352c9ce25SScott Long 		xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
127452c9ce25SScott Long 			      request_ccb->ccb_h.pinfo.priority);
127552c9ce25SScott Long 		work_ccb->ccb_h.func_code = XPT_PATH_INQ;
127652c9ce25SScott Long 		xpt_action(work_ccb);
127752c9ce25SScott Long 		if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
127852c9ce25SScott Long 			request_ccb->ccb_h.status = work_ccb->ccb_h.status;
127952c9ce25SScott Long 			xpt_free_ccb(work_ccb);
128052c9ce25SScott Long 			xpt_done(request_ccb);
128152c9ce25SScott Long 			return;
128252c9ce25SScott Long 		}
128352c9ce25SScott Long 
128483c5d981SAlexander Motin 		/* We may need to reset bus first, if we haven't done it yet. */
128583c5d981SAlexander Motin 		if ((work_ccb->cpi.hba_inquiry &
128683c5d981SAlexander Motin 		    (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) &&
128783c5d981SAlexander Motin 		    !(work_ccb->cpi.hba_misc & PIM_NOBUSRESET) &&
128883c5d981SAlexander Motin 		    !timevalisset(&request_ccb->ccb_h.path->bus->last_reset)) {
128983c5d981SAlexander Motin 			reset_ccb = xpt_alloc_ccb_nowait();
1290f1893540SAlexander Motin 			if (reset_ccb == NULL) {
1291f1893540SAlexander Motin 				request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1292f1893540SAlexander Motin 				xpt_free_ccb(work_ccb);
1293f1893540SAlexander Motin 				xpt_done(request_ccb);
1294f1893540SAlexander Motin 				return;
1295f1893540SAlexander Motin 			}
129683c5d981SAlexander Motin 			xpt_setup_ccb(&reset_ccb->ccb_h, request_ccb->ccb_h.path,
129783c5d981SAlexander Motin 			      CAM_PRIORITY_NONE);
129883c5d981SAlexander Motin 			reset_ccb->ccb_h.func_code = XPT_RESET_BUS;
129983c5d981SAlexander Motin 			xpt_action(reset_ccb);
130083c5d981SAlexander Motin 			if (reset_ccb->ccb_h.status != CAM_REQ_CMP) {
130183c5d981SAlexander Motin 				request_ccb->ccb_h.status = reset_ccb->ccb_h.status;
130283c5d981SAlexander Motin 				xpt_free_ccb(reset_ccb);
130383c5d981SAlexander Motin 				xpt_free_ccb(work_ccb);
130483c5d981SAlexander Motin 				xpt_done(request_ccb);
130583c5d981SAlexander Motin 				return;
130683c5d981SAlexander Motin 			}
130783c5d981SAlexander Motin 			xpt_free_ccb(reset_ccb);
130883c5d981SAlexander Motin 		}
130983c5d981SAlexander Motin 
131052c9ce25SScott Long 		/* Save some state for use while we probe for devices */
131152c9ce25SScott Long 		scan_info = (ata_scan_bus_info *)
131252c9ce25SScott Long 		    malloc(sizeof(ata_scan_bus_info), M_CAMXPT, M_NOWAIT);
131352c9ce25SScott Long 		if (scan_info == NULL) {
131452c9ce25SScott Long 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1315f1893540SAlexander Motin 			xpt_free_ccb(work_ccb);
131652c9ce25SScott Long 			xpt_done(request_ccb);
131752c9ce25SScott Long 			return;
131852c9ce25SScott Long 		}
131952c9ce25SScott Long 		scan_info->request_ccb = request_ccb;
132052c9ce25SScott Long 		scan_info->cpi = &work_ccb->cpi;
132152c9ce25SScott Long 		/* If PM supported, probe it first. */
132252c9ce25SScott Long 		if (scan_info->cpi->hba_inquiry & PI_SATAPM)
13230025eb12SAlexander Motin 			scan_info->counter = scan_info->cpi->max_target;
13240025eb12SAlexander Motin 		else
13250025eb12SAlexander Motin 			scan_info->counter = 0;
132652c9ce25SScott Long 
132752c9ce25SScott Long 		work_ccb = xpt_alloc_ccb_nowait();
132852c9ce25SScott Long 		if (work_ccb == NULL) {
132952c9ce25SScott Long 			free(scan_info, M_CAMXPT);
133052c9ce25SScott Long 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
133152c9ce25SScott Long 			xpt_done(request_ccb);
133252c9ce25SScott Long 			break;
133352c9ce25SScott Long 		}
133452c9ce25SScott Long 		goto scan_next;
133552c9ce25SScott Long 	case XPT_SCAN_LUN:
133652c9ce25SScott Long 		work_ccb = request_ccb;
133752c9ce25SScott Long 		/* Reuse the same CCB to query if a device was really found */
133852c9ce25SScott Long 		scan_info = (ata_scan_bus_info *)work_ccb->ccb_h.ppriv_ptr0;
133965d0fb03SAlexander Motin 		/* If there is PMP... */
13400025eb12SAlexander Motin 		if ((scan_info->cpi->hba_inquiry & PI_SATAPM) &&
13410025eb12SAlexander Motin 		    (scan_info->counter == scan_info->cpi->max_target)) {
134283c5d981SAlexander Motin 			if (work_ccb->ccb_h.status == CAM_REQ_CMP) {
134365d0fb03SAlexander Motin 				/* everything else will be probed by it */
1344e2a75189SAlexander Motin 				/* Free the current request path- we're done with it. */
1345e2a75189SAlexander Motin 				xpt_free_path(work_ccb->ccb_h.path);
13460025eb12SAlexander Motin 				goto done;
134752c9ce25SScott Long 			} else {
134852c9ce25SScott Long 				struct ccb_trans_settings cts;
134952c9ce25SScott Long 
135052c9ce25SScott Long 				/* Report SIM that PM is absent. */
135152c9ce25SScott Long 				bzero(&cts, sizeof(cts));
135252c9ce25SScott Long 				xpt_setup_ccb(&cts.ccb_h,
1353e2a75189SAlexander Motin 				    work_ccb->ccb_h.path, CAM_PRIORITY_NONE);
135452c9ce25SScott Long 				cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
135552c9ce25SScott Long 				cts.type = CTS_TYPE_CURRENT_SETTINGS;
13563ccda2f3SAlexander Motin 				cts.xport_specific.sata.pm_present = 0;
135752c9ce25SScott Long 				cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
135852c9ce25SScott Long 				xpt_action((union ccb *)&cts);
135952c9ce25SScott Long 			}
136052c9ce25SScott Long 		}
1361e2a75189SAlexander Motin 		/* Free the current request path- we're done with it. */
1362e2a75189SAlexander Motin 		xpt_free_path(work_ccb->ccb_h.path);
13630025eb12SAlexander Motin 		if (scan_info->counter ==
13640025eb12SAlexander Motin 		    ((scan_info->cpi->hba_inquiry & PI_SATAPM) ?
13650025eb12SAlexander Motin 		    0 : scan_info->cpi->max_target)) {
13660025eb12SAlexander Motin done:
136752c9ce25SScott Long 			xpt_free_ccb(work_ccb);
136852c9ce25SScott Long 			xpt_free_ccb((union ccb *)scan_info->cpi);
136952c9ce25SScott Long 			request_ccb = scan_info->request_ccb;
137052c9ce25SScott Long 			free(scan_info, M_CAMXPT);
137152c9ce25SScott Long 			request_ccb->ccb_h.status = CAM_REQ_CMP;
137252c9ce25SScott Long 			xpt_done(request_ccb);
137352c9ce25SScott Long 			break;
137452c9ce25SScott Long 		}
13750025eb12SAlexander Motin 		/* Take next device. Wrap from max (PMP) to 0. */
13760025eb12SAlexander Motin 		scan_info->counter = (scan_info->counter + 1 ) %
13770025eb12SAlexander Motin 		    (scan_info->cpi->max_target + 1);
137852c9ce25SScott Long scan_next:
137952c9ce25SScott Long 		status = xpt_create_path(&path, xpt_periph,
138052c9ce25SScott Long 		    scan_info->request_ccb->ccb_h.path_id,
138152c9ce25SScott Long 		    scan_info->counter, 0);
138252c9ce25SScott Long 		if (status != CAM_REQ_CMP) {
138352c9ce25SScott Long 			printf("xpt_scan_bus: xpt_create_path failed"
138452c9ce25SScott Long 			    " with status %#x, bus scan halted\n",
138552c9ce25SScott Long 			    status);
138652c9ce25SScott Long 			xpt_free_ccb(work_ccb);
138752c9ce25SScott Long 			xpt_free_ccb((union ccb *)scan_info->cpi);
138852c9ce25SScott Long 			request_ccb = scan_info->request_ccb;
138952c9ce25SScott Long 			free(scan_info, M_CAMXPT);
139052c9ce25SScott Long 			request_ccb->ccb_h.status = status;
139152c9ce25SScott Long 			xpt_done(request_ccb);
139252c9ce25SScott Long 			break;
139352c9ce25SScott Long 		}
139452c9ce25SScott Long 		xpt_setup_ccb(&work_ccb->ccb_h, path,
139552c9ce25SScott Long 		    scan_info->request_ccb->ccb_h.pinfo.priority);
139652c9ce25SScott Long 		work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
139752c9ce25SScott Long 		work_ccb->ccb_h.cbfcnp = ata_scan_bus;
139852c9ce25SScott Long 		work_ccb->ccb_h.ppriv_ptr0 = scan_info;
139952c9ce25SScott Long 		work_ccb->crcn.flags = scan_info->request_ccb->crcn.flags;
140052c9ce25SScott Long 		xpt_action(work_ccb);
140152c9ce25SScott Long 		break;
140252c9ce25SScott Long 	default:
140352c9ce25SScott Long 		break;
140452c9ce25SScott Long 	}
140552c9ce25SScott Long }
140652c9ce25SScott Long 
140752c9ce25SScott Long static void
140852c9ce25SScott Long ata_scan_lun(struct cam_periph *periph, struct cam_path *path,
140952c9ce25SScott Long 	     cam_flags flags, union ccb *request_ccb)
141052c9ce25SScott Long {
141152c9ce25SScott Long 	struct ccb_pathinq cpi;
141252c9ce25SScott Long 	cam_status status;
141352c9ce25SScott Long 	struct cam_path *new_path;
141452c9ce25SScott Long 	struct cam_periph *old_periph;
141552c9ce25SScott Long 
141683c5d981SAlexander Motin 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_scan_lun\n"));
141752c9ce25SScott Long 
141883c5d981SAlexander Motin 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
141952c9ce25SScott Long 	cpi.ccb_h.func_code = XPT_PATH_INQ;
142052c9ce25SScott Long 	xpt_action((union ccb *)&cpi);
142152c9ce25SScott Long 
142252c9ce25SScott Long 	if (cpi.ccb_h.status != CAM_REQ_CMP) {
142352c9ce25SScott Long 		if (request_ccb != NULL) {
142452c9ce25SScott Long 			request_ccb->ccb_h.status = cpi.ccb_h.status;
142552c9ce25SScott Long 			xpt_done(request_ccb);
142652c9ce25SScott Long 		}
142752c9ce25SScott Long 		return;
142852c9ce25SScott Long 	}
142952c9ce25SScott Long 
143052c9ce25SScott Long 	if (request_ccb == NULL) {
143152c9ce25SScott Long 		request_ccb = malloc(sizeof(union ccb), M_CAMXPT, M_NOWAIT);
143252c9ce25SScott Long 		if (request_ccb == NULL) {
143352c9ce25SScott Long 			xpt_print(path, "xpt_scan_lun: can't allocate CCB, "
143452c9ce25SScott Long 			    "can't continue\n");
143552c9ce25SScott Long 			return;
143652c9ce25SScott Long 		}
143752c9ce25SScott Long 		new_path = malloc(sizeof(*new_path), M_CAMXPT, M_NOWAIT);
143852c9ce25SScott Long 		if (new_path == NULL) {
143952c9ce25SScott Long 			xpt_print(path, "xpt_scan_lun: can't allocate path, "
144052c9ce25SScott Long 			    "can't continue\n");
144152c9ce25SScott Long 			free(request_ccb, M_CAMXPT);
144252c9ce25SScott Long 			return;
144352c9ce25SScott Long 		}
144452c9ce25SScott Long 		status = xpt_compile_path(new_path, xpt_periph,
144552c9ce25SScott Long 					  path->bus->path_id,
144652c9ce25SScott Long 					  path->target->target_id,
144752c9ce25SScott Long 					  path->device->lun_id);
144852c9ce25SScott Long 
144952c9ce25SScott Long 		if (status != CAM_REQ_CMP) {
145052c9ce25SScott Long 			xpt_print(path, "xpt_scan_lun: can't compile path, "
145152c9ce25SScott Long 			    "can't continue\n");
145252c9ce25SScott Long 			free(request_ccb, M_CAMXPT);
145352c9ce25SScott Long 			free(new_path, M_CAMXPT);
145452c9ce25SScott Long 			return;
145552c9ce25SScott Long 		}
145683c5d981SAlexander Motin 		xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_XPT);
145752c9ce25SScott Long 		request_ccb->ccb_h.cbfcnp = xptscandone;
145852c9ce25SScott Long 		request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
145952c9ce25SScott Long 		request_ccb->crcn.flags = flags;
146052c9ce25SScott Long 	}
146152c9ce25SScott Long 
1462f09f8e3eSAlexander Motin 	if ((old_periph = cam_periph_find(path, "aprobe")) != NULL) {
146352c9ce25SScott Long 		probe_softc *softc;
146452c9ce25SScott Long 
146552c9ce25SScott Long 		softc = (probe_softc *)old_periph->softc;
146652c9ce25SScott Long 		TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
146752c9ce25SScott Long 				  periph_links.tqe);
146883c5d981SAlexander Motin 		softc->restart = 1;
146952c9ce25SScott Long 	} else {
147052c9ce25SScott Long 		status = cam_periph_alloc(proberegister, NULL, probecleanup,
1471f09f8e3eSAlexander Motin 					  probestart, "aprobe",
147252c9ce25SScott Long 					  CAM_PERIPH_BIO,
147352c9ce25SScott Long 					  request_ccb->ccb_h.path, NULL, 0,
147452c9ce25SScott Long 					  request_ccb);
147552c9ce25SScott Long 
147652c9ce25SScott Long 		if (status != CAM_REQ_CMP) {
147752c9ce25SScott Long 			xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
147852c9ce25SScott Long 			    "returned an error, can't continue probe\n");
147952c9ce25SScott Long 			request_ccb->ccb_h.status = status;
148052c9ce25SScott Long 			xpt_done(request_ccb);
148152c9ce25SScott Long 		}
148252c9ce25SScott Long 	}
148352c9ce25SScott Long }
148452c9ce25SScott Long 
148552c9ce25SScott Long static void
148652c9ce25SScott Long xptscandone(struct cam_periph *periph, union ccb *done_ccb)
148752c9ce25SScott Long {
148852c9ce25SScott Long 	xpt_release_path(done_ccb->ccb_h.path);
148952c9ce25SScott Long 	free(done_ccb->ccb_h.path, M_CAMXPT);
149052c9ce25SScott Long 	free(done_ccb, M_CAMXPT);
149152c9ce25SScott Long }
149252c9ce25SScott Long 
149352c9ce25SScott Long static struct cam_ed *
149452c9ce25SScott Long ata_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
149552c9ce25SScott Long {
149652c9ce25SScott Long 	struct cam_path path;
149730a4094fSAlexander Motin 	struct ata_quirk_entry *quirk;
149852c9ce25SScott Long 	struct cam_ed *device;
149952c9ce25SScott Long 	struct cam_ed *cur_device;
150052c9ce25SScott Long 
150152c9ce25SScott Long 	device = xpt_alloc_device(bus, target, lun_id);
150252c9ce25SScott Long 	if (device == NULL)
150352c9ce25SScott Long 		return (NULL);
150452c9ce25SScott Long 
150552c9ce25SScott Long 	/*
150652c9ce25SScott Long 	 * Take the default quirk entry until we have inquiry
150752c9ce25SScott Long 	 * data and can determine a better quirk to use.
150852c9ce25SScott Long 	 */
150930a4094fSAlexander Motin 	quirk = &ata_quirk_table[ata_quirk_table_size - 1];
151052c9ce25SScott Long 	device->quirk = (void *)quirk;
151130a4094fSAlexander Motin 	device->mintags = 0;
151230a4094fSAlexander Motin 	device->maxtags = 0;
151352c9ce25SScott Long 	bzero(&device->inq_data, sizeof(device->inq_data));
151452c9ce25SScott Long 	device->inq_flags = 0;
151552c9ce25SScott Long 	device->queue_flags = 0;
151652c9ce25SScott Long 	device->serial_num = NULL;
151752c9ce25SScott Long 	device->serial_num_len = 0;
151852c9ce25SScott Long 
151952c9ce25SScott Long 	/*
152052c9ce25SScott Long 	 * XXX should be limited by number of CCBs this bus can
152152c9ce25SScott Long 	 * do.
152252c9ce25SScott Long 	 */
152352c9ce25SScott Long 	bus->sim->max_ccbs += device->ccbq.devq_openings;
152452c9ce25SScott Long 	/* Insertion sort into our target's device list */
152552c9ce25SScott Long 	cur_device = TAILQ_FIRST(&target->ed_entries);
152652c9ce25SScott Long 	while (cur_device != NULL && cur_device->lun_id < lun_id)
152752c9ce25SScott Long 		cur_device = TAILQ_NEXT(cur_device, links);
152852c9ce25SScott Long 	if (cur_device != NULL) {
152952c9ce25SScott Long 		TAILQ_INSERT_BEFORE(cur_device, device, links);
153052c9ce25SScott Long 	} else {
153152c9ce25SScott Long 		TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
153252c9ce25SScott Long 	}
153352c9ce25SScott Long 	target->generation++;
153452c9ce25SScott Long 	if (lun_id != CAM_LUN_WILDCARD) {
153552c9ce25SScott Long 		xpt_compile_path(&path,
153652c9ce25SScott Long 				 NULL,
153752c9ce25SScott Long 				 bus->path_id,
153852c9ce25SScott Long 				 target->target_id,
153952c9ce25SScott Long 				 lun_id);
154052c9ce25SScott Long 		ata_device_transport(&path);
154152c9ce25SScott Long 		xpt_release_path(&path);
154252c9ce25SScott Long 	}
154352c9ce25SScott Long 
154452c9ce25SScott Long 	return (device);
154552c9ce25SScott Long }
154652c9ce25SScott Long 
154752c9ce25SScott Long static void
154852c9ce25SScott Long ata_device_transport(struct cam_path *path)
154952c9ce25SScott Long {
155052c9ce25SScott Long 	struct ccb_pathinq cpi;
15514b997c49SAlexander Motin 	struct ccb_trans_settings cts;
15524b997c49SAlexander Motin 	struct scsi_inquiry_data *inq_buf = NULL;
15534b997c49SAlexander Motin 	struct ata_params *ident_buf = NULL;
155452c9ce25SScott Long 
155552c9ce25SScott Long 	/* Get transport information from the SIM */
155683c5d981SAlexander Motin 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
155752c9ce25SScott Long 	cpi.ccb_h.func_code = XPT_PATH_INQ;
155852c9ce25SScott Long 	xpt_action((union ccb *)&cpi);
155952c9ce25SScott Long 
156052c9ce25SScott Long 	path->device->transport = cpi.transport;
15614b997c49SAlexander Motin 	if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
15624b997c49SAlexander Motin 		inq_buf = &path->device->inq_data;
15634b997c49SAlexander Motin 	if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) != 0)
15644b997c49SAlexander Motin 		ident_buf = &path->device->ident_data;
15654b997c49SAlexander Motin 	if (path->device->protocol == PROTO_ATA) {
15664b997c49SAlexander Motin 		path->device->protocol_version = ident_buf ?
15674b997c49SAlexander Motin 		    ata_version(ident_buf->version_major) : cpi.protocol_version;
15684b997c49SAlexander Motin 	} else if (path->device->protocol == PROTO_SCSI) {
15694b997c49SAlexander Motin 		path->device->protocol_version = inq_buf ?
15704b997c49SAlexander Motin 		    SID_ANSI_REV(inq_buf) : cpi.protocol_version;
157152c9ce25SScott Long 	}
15724b997c49SAlexander Motin 	path->device->transport_version = ident_buf ?
15734b997c49SAlexander Motin 	    ata_version(ident_buf->version_major) : cpi.transport_version;
157452c9ce25SScott Long 
157552c9ce25SScott Long 	/* Tell the controller what we think */
157683c5d981SAlexander Motin 	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
157752c9ce25SScott Long 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
157852c9ce25SScott Long 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
157952c9ce25SScott Long 	cts.transport = path->device->transport;
158052c9ce25SScott Long 	cts.transport_version = path->device->transport_version;
158152c9ce25SScott Long 	cts.protocol = path->device->protocol;
158252c9ce25SScott Long 	cts.protocol_version = path->device->protocol_version;
158352c9ce25SScott Long 	cts.proto_specific.valid = 0;
15844cca1530SAlexander Motin 	if (ident_buf) {
15854cca1530SAlexander Motin 		if (path->device->transport == XPORT_ATA) {
15864cca1530SAlexander Motin 			cts.xport_specific.ata.atapi =
15874cca1530SAlexander Motin 			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 :
15884cca1530SAlexander Motin 			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0;
15894cca1530SAlexander Motin 			cts.xport_specific.ata.valid = CTS_ATA_VALID_ATAPI;
15904cca1530SAlexander Motin 		} else {
15914cca1530SAlexander Motin 			cts.xport_specific.sata.atapi =
15924cca1530SAlexander Motin 			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 :
15934cca1530SAlexander Motin 			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0;
15944cca1530SAlexander Motin 			cts.xport_specific.sata.valid = CTS_SATA_VALID_ATAPI;
15954cca1530SAlexander Motin 		}
15964cca1530SAlexander Motin 	} else
159752c9ce25SScott Long 		cts.xport_specific.valid = 0;
159852c9ce25SScott Long 	xpt_action((union ccb *)&cts);
159952c9ce25SScott Long }
160052c9ce25SScott Long 
160152c9ce25SScott Long static void
160252c9ce25SScott Long ata_action(union ccb *start_ccb)
160352c9ce25SScott Long {
160452c9ce25SScott Long 
160552c9ce25SScott Long 	switch (start_ccb->ccb_h.func_code) {
160652c9ce25SScott Long 	case XPT_SET_TRAN_SETTINGS:
160752c9ce25SScott Long 	{
160830a4094fSAlexander Motin 		ata_set_transfer_settings(&start_ccb->cts,
160952c9ce25SScott Long 					   start_ccb->ccb_h.path->device,
161052c9ce25SScott Long 					   /*async_update*/FALSE);
161152c9ce25SScott Long 		break;
161252c9ce25SScott Long 	}
161352c9ce25SScott Long 	case XPT_SCAN_BUS:
16140e85f214SMatt Jacob 	case XPT_SCAN_TGT:
161552c9ce25SScott Long 		ata_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
161652c9ce25SScott Long 		break;
161752c9ce25SScott Long 	case XPT_SCAN_LUN:
161852c9ce25SScott Long 		ata_scan_lun(start_ccb->ccb_h.path->periph,
161952c9ce25SScott Long 			      start_ccb->ccb_h.path, start_ccb->crcn.flags,
162052c9ce25SScott Long 			      start_ccb);
162152c9ce25SScott Long 		break;
162252c9ce25SScott Long 	case XPT_GET_TRAN_SETTINGS:
162352c9ce25SScott Long 	{
162452c9ce25SScott Long 		struct cam_sim *sim;
162552c9ce25SScott Long 
162652c9ce25SScott Long 		sim = start_ccb->ccb_h.path->bus->sim;
162752c9ce25SScott Long 		(*(sim->sim_action))(sim, start_ccb);
162852c9ce25SScott Long 		break;
162952c9ce25SScott Long 	}
16304cca1530SAlexander Motin 	case XPT_SCSI_IO:
16314cca1530SAlexander Motin 	{
16324cca1530SAlexander Motin 		struct cam_ed *device;
16334cca1530SAlexander Motin 		u_int	maxlen = 0;
16344cca1530SAlexander Motin 
16354cca1530SAlexander Motin 		device = start_ccb->ccb_h.path->device;
16364cca1530SAlexander Motin 		if (device->protocol == PROTO_SCSI &&
16374cca1530SAlexander Motin 		    (device->flags & CAM_DEV_IDENTIFY_DATA_VALID)) {
16384cca1530SAlexander Motin 			uint16_t p =
16394cca1530SAlexander Motin 			    device->ident_data.config & ATA_PROTO_MASK;
16404cca1530SAlexander Motin 
16414cca1530SAlexander Motin 			maxlen = (p == ATA_PROTO_ATAPI_16) ? 16 :
16424cca1530SAlexander Motin 			    (p == ATA_PROTO_ATAPI_12) ? 12 : 0;
16434cca1530SAlexander Motin 		}
16444cca1530SAlexander Motin 		if (start_ccb->csio.cdb_len > maxlen) {
16454cca1530SAlexander Motin 			start_ccb->ccb_h.status = CAM_REQ_INVALID;
16464cca1530SAlexander Motin 			xpt_done(start_ccb);
16474cca1530SAlexander Motin 			break;
16484cca1530SAlexander Motin 		}
16494cca1530SAlexander Motin 		/* FALLTHROUGH */
16504cca1530SAlexander Motin 	}
165152c9ce25SScott Long 	default:
165252c9ce25SScott Long 		xpt_action_default(start_ccb);
165352c9ce25SScott Long 		break;
165452c9ce25SScott Long 	}
165552c9ce25SScott Long }
165652c9ce25SScott Long 
165752c9ce25SScott Long static void
165830a4094fSAlexander Motin ata_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
165952c9ce25SScott Long 			   int async_update)
166052c9ce25SScott Long {
166152c9ce25SScott Long 	struct	ccb_pathinq cpi;
166252c9ce25SScott Long 	struct	ccb_trans_settings cur_cts;
166352c9ce25SScott Long 	struct	ccb_trans_settings_scsi *scsi;
166452c9ce25SScott Long 	struct	ccb_trans_settings_scsi *cur_scsi;
166552c9ce25SScott Long 	struct	cam_sim *sim;
166652c9ce25SScott Long 	struct	scsi_inquiry_data *inq_data;
166752c9ce25SScott Long 
166852c9ce25SScott Long 	if (device == NULL) {
166952c9ce25SScott Long 		cts->ccb_h.status = CAM_PATH_INVALID;
167052c9ce25SScott Long 		xpt_done((union ccb *)cts);
167152c9ce25SScott Long 		return;
167252c9ce25SScott Long 	}
167352c9ce25SScott Long 
167452c9ce25SScott Long 	if (cts->protocol == PROTO_UNKNOWN
167552c9ce25SScott Long 	 || cts->protocol == PROTO_UNSPECIFIED) {
167652c9ce25SScott Long 		cts->protocol = device->protocol;
167752c9ce25SScott Long 		cts->protocol_version = device->protocol_version;
167852c9ce25SScott Long 	}
167952c9ce25SScott Long 
168052c9ce25SScott Long 	if (cts->protocol_version == PROTO_VERSION_UNKNOWN
168152c9ce25SScott Long 	 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
168252c9ce25SScott Long 		cts->protocol_version = device->protocol_version;
168352c9ce25SScott Long 
168452c9ce25SScott Long 	if (cts->protocol != device->protocol) {
168552c9ce25SScott Long 		xpt_print(cts->ccb_h.path, "Uninitialized Protocol %x:%x?\n",
168652c9ce25SScott Long 		       cts->protocol, device->protocol);
168752c9ce25SScott Long 		cts->protocol = device->protocol;
168852c9ce25SScott Long 	}
168952c9ce25SScott Long 
169052c9ce25SScott Long 	if (cts->protocol_version > device->protocol_version) {
169152c9ce25SScott Long 		if (bootverbose) {
169252c9ce25SScott Long 			xpt_print(cts->ccb_h.path, "Down reving Protocol "
169352c9ce25SScott Long 			    "Version from %d to %d?\n", cts->protocol_version,
169452c9ce25SScott Long 			    device->protocol_version);
169552c9ce25SScott Long 		}
169652c9ce25SScott Long 		cts->protocol_version = device->protocol_version;
169752c9ce25SScott Long 	}
169852c9ce25SScott Long 
169952c9ce25SScott Long 	if (cts->transport == XPORT_UNKNOWN
170052c9ce25SScott Long 	 || cts->transport == XPORT_UNSPECIFIED) {
170152c9ce25SScott Long 		cts->transport = device->transport;
170252c9ce25SScott Long 		cts->transport_version = device->transport_version;
170352c9ce25SScott Long 	}
170452c9ce25SScott Long 
170552c9ce25SScott Long 	if (cts->transport_version == XPORT_VERSION_UNKNOWN
170652c9ce25SScott Long 	 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
170752c9ce25SScott Long 		cts->transport_version = device->transport_version;
170852c9ce25SScott Long 
170952c9ce25SScott Long 	if (cts->transport != device->transport) {
171052c9ce25SScott Long 		xpt_print(cts->ccb_h.path, "Uninitialized Transport %x:%x?\n",
171152c9ce25SScott Long 		    cts->transport, device->transport);
171252c9ce25SScott Long 		cts->transport = device->transport;
171352c9ce25SScott Long 	}
171452c9ce25SScott Long 
171552c9ce25SScott Long 	if (cts->transport_version > device->transport_version) {
171652c9ce25SScott Long 		if (bootverbose) {
171752c9ce25SScott Long 			xpt_print(cts->ccb_h.path, "Down reving Transport "
171852c9ce25SScott Long 			    "Version from %d to %d?\n", cts->transport_version,
171952c9ce25SScott Long 			    device->transport_version);
172052c9ce25SScott Long 		}
172152c9ce25SScott Long 		cts->transport_version = device->transport_version;
172252c9ce25SScott Long 	}
172352c9ce25SScott Long 
172452c9ce25SScott Long 	sim = cts->ccb_h.path->bus->sim;
172552c9ce25SScott Long 
172652c9ce25SScott Long 	/*
172752c9ce25SScott Long 	 * Nothing more of interest to do unless
172852c9ce25SScott Long 	 * this is a device connected via the
172952c9ce25SScott Long 	 * SCSI protocol.
173052c9ce25SScott Long 	 */
173152c9ce25SScott Long 	if (cts->protocol != PROTO_SCSI) {
173252c9ce25SScott Long 		if (async_update == FALSE)
173352c9ce25SScott Long 			(*(sim->sim_action))(sim, (union ccb *)cts);
173452c9ce25SScott Long 		return;
173552c9ce25SScott Long 	}
173652c9ce25SScott Long 
173752c9ce25SScott Long 	inq_data = &device->inq_data;
173852c9ce25SScott Long 	scsi = &cts->proto_specific.scsi;
173983c5d981SAlexander Motin 	xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NONE);
174052c9ce25SScott Long 	cpi.ccb_h.func_code = XPT_PATH_INQ;
174152c9ce25SScott Long 	xpt_action((union ccb *)&cpi);
174252c9ce25SScott Long 
174352c9ce25SScott Long 	/* SCSI specific sanity checking */
174452c9ce25SScott Long 	if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
174552c9ce25SScott Long 	 || (INQ_DATA_TQ_ENABLED(inq_data)) == 0
174652c9ce25SScott Long 	 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
174752c9ce25SScott Long 	 || (device->mintags == 0)) {
174852c9ce25SScott Long 		/*
174952c9ce25SScott Long 		 * Can't tag on hardware that doesn't support tags,
175052c9ce25SScott Long 		 * doesn't have it enabled, or has broken tag support.
175152c9ce25SScott Long 		 */
175252c9ce25SScott Long 		scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
175352c9ce25SScott Long 	}
175452c9ce25SScott Long 
175552c9ce25SScott Long 	if (async_update == FALSE) {
175652c9ce25SScott Long 		/*
175752c9ce25SScott Long 		 * Perform sanity checking against what the
175852c9ce25SScott Long 		 * controller and device can do.
175952c9ce25SScott Long 		 */
176083c5d981SAlexander Motin 		xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NONE);
176152c9ce25SScott Long 		cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
176252c9ce25SScott Long 		cur_cts.type = cts->type;
176352c9ce25SScott Long 		xpt_action((union ccb *)&cur_cts);
176452c9ce25SScott Long 		if ((cur_cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
176552c9ce25SScott Long 			return;
176652c9ce25SScott Long 		}
176752c9ce25SScott Long 		cur_scsi = &cur_cts.proto_specific.scsi;
176852c9ce25SScott Long 		if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
176952c9ce25SScott Long 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
177052c9ce25SScott Long 			scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
177152c9ce25SScott Long 		}
177252c9ce25SScott Long 		if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
177352c9ce25SScott Long 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
177452c9ce25SScott Long 	}
177552c9ce25SScott Long 
177652c9ce25SScott Long 	if (cts->type == CTS_TYPE_CURRENT_SETTINGS
177752c9ce25SScott Long 	 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
177852c9ce25SScott Long 		int device_tagenb;
177952c9ce25SScott Long 
178052c9ce25SScott Long 		/*
178152c9ce25SScott Long 		 * If we are transitioning from tags to no-tags or
178252c9ce25SScott Long 		 * vice-versa, we need to carefully freeze and restart
178352c9ce25SScott Long 		 * the queue so that we don't overlap tagged and non-tagged
178452c9ce25SScott Long 		 * commands.  We also temporarily stop tags if there is
178552c9ce25SScott Long 		 * a change in transfer negotiation settings to allow
178652c9ce25SScott Long 		 * "tag-less" negotiation.
178752c9ce25SScott Long 		 */
178852c9ce25SScott Long 		if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
178952c9ce25SScott Long 		 || (device->inq_flags & SID_CmdQue) != 0)
179052c9ce25SScott Long 			device_tagenb = TRUE;
179152c9ce25SScott Long 		else
179252c9ce25SScott Long 			device_tagenb = FALSE;
179352c9ce25SScott Long 
179452c9ce25SScott Long 		if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
179552c9ce25SScott Long 		  && device_tagenb == FALSE)
179652c9ce25SScott Long 		 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
179752c9ce25SScott Long 		  && device_tagenb == TRUE)) {
179852c9ce25SScott Long 
179952c9ce25SScott Long 			if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
180052c9ce25SScott Long 				/*
180152c9ce25SScott Long 				 * Delay change to use tags until after a
180252c9ce25SScott Long 				 * few commands have gone to this device so
180352c9ce25SScott Long 				 * the controller has time to perform transfer
180452c9ce25SScott Long 				 * negotiations without tagged messages getting
180552c9ce25SScott Long 				 * in the way.
180652c9ce25SScott Long 				 */
180752c9ce25SScott Long 				device->tag_delay_count = CAM_TAG_DELAY_COUNT;
180852c9ce25SScott Long 				device->flags |= CAM_DEV_TAG_AFTER_COUNT;
180952c9ce25SScott Long 			} else {
181030a4094fSAlexander Motin 				xpt_stop_tags(cts->ccb_h.path);
181152c9ce25SScott Long 			}
181252c9ce25SScott Long 		}
181352c9ce25SScott Long 	}
181452c9ce25SScott Long 	if (async_update == FALSE)
181552c9ce25SScott Long 		(*(sim->sim_action))(sim, (union ccb *)cts);
181652c9ce25SScott Long }
181752c9ce25SScott Long 
181852c9ce25SScott Long /*
181952c9ce25SScott Long  * Handle any per-device event notifications that require action by the XPT.
182052c9ce25SScott Long  */
182152c9ce25SScott Long static void
182252c9ce25SScott Long ata_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
182352c9ce25SScott Long 	      struct cam_ed *device, void *async_arg)
182452c9ce25SScott Long {
182552c9ce25SScott Long 	cam_status status;
182652c9ce25SScott Long 	struct cam_path newpath;
182752c9ce25SScott Long 
182852c9ce25SScott Long 	/*
182952c9ce25SScott Long 	 * We only need to handle events for real devices.
183052c9ce25SScott Long 	 */
183152c9ce25SScott Long 	if (target->target_id == CAM_TARGET_WILDCARD
183252c9ce25SScott Long 	 || device->lun_id == CAM_LUN_WILDCARD)
183352c9ce25SScott Long 		return;
183452c9ce25SScott Long 
183552c9ce25SScott Long 	/*
183652c9ce25SScott Long 	 * We need our own path with wildcards expanded to
183752c9ce25SScott Long 	 * handle certain types of events.
183852c9ce25SScott Long 	 */
183952c9ce25SScott Long 	if ((async_code == AC_SENT_BDR)
184052c9ce25SScott Long 	 || (async_code == AC_BUS_RESET)
184152c9ce25SScott Long 	 || (async_code == AC_INQ_CHANGED))
184252c9ce25SScott Long 		status = xpt_compile_path(&newpath, NULL,
184352c9ce25SScott Long 					  bus->path_id,
184452c9ce25SScott Long 					  target->target_id,
184552c9ce25SScott Long 					  device->lun_id);
184652c9ce25SScott Long 	else
184752c9ce25SScott Long 		status = CAM_REQ_CMP_ERR;
184852c9ce25SScott Long 
184952c9ce25SScott Long 	if (status == CAM_REQ_CMP) {
185052c9ce25SScott Long 		if (async_code == AC_INQ_CHANGED) {
185152c9ce25SScott Long 			/*
185252c9ce25SScott Long 			 * We've sent a start unit command, or
185352c9ce25SScott Long 			 * something similar to a device that
185452c9ce25SScott Long 			 * may have caused its inquiry data to
185552c9ce25SScott Long 			 * change. So we re-scan the device to
185652c9ce25SScott Long 			 * refresh the inquiry data for it.
185752c9ce25SScott Long 			 */
185852c9ce25SScott Long 			ata_scan_lun(newpath.periph, &newpath,
185952c9ce25SScott Long 				     CAM_EXPECT_INQ_CHANGE, NULL);
186083c5d981SAlexander Motin 		} else {
186183c5d981SAlexander Motin 			/* We need to reinitialize device after reset. */
186283c5d981SAlexander Motin 			ata_scan_lun(newpath.periph, &newpath,
186383c5d981SAlexander Motin 				     0, NULL);
186452c9ce25SScott Long 		}
186552c9ce25SScott Long 		xpt_release_path(&newpath);
1866f98d7a47SAlexander Motin 	} else if (async_code == AC_LOST_DEVICE &&
1867f98d7a47SAlexander Motin 	    (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
186852c9ce25SScott Long 		device->flags |= CAM_DEV_UNCONFIGURED;
1869f98d7a47SAlexander Motin 		xpt_release_device(device);
187052c9ce25SScott Long 	} else if (async_code == AC_TRANSFER_NEG) {
187152c9ce25SScott Long 		struct ccb_trans_settings *settings;
187252c9ce25SScott Long 
187352c9ce25SScott Long 		settings = (struct ccb_trans_settings *)async_arg;
187430a4094fSAlexander Motin 		ata_set_transfer_settings(settings, device,
187552c9ce25SScott Long 					  /*async_update*/TRUE);
187652c9ce25SScott Long 	}
187752c9ce25SScott Long }
187852c9ce25SScott Long 
187957079b17SAlexander Motin static void
188057079b17SAlexander Motin ata_announce_periph(struct cam_periph *periph)
188157079b17SAlexander Motin {
188257079b17SAlexander Motin 	struct	ccb_pathinq cpi;
188357079b17SAlexander Motin 	struct	ccb_trans_settings cts;
188457079b17SAlexander Motin 	struct	cam_path *path = periph->path;
188557079b17SAlexander Motin 	u_int	speed;
188657079b17SAlexander Motin 	u_int	mb;
188757079b17SAlexander Motin 
188857079b17SAlexander Motin 	mtx_assert(periph->sim->mtx, MA_OWNED);
188957079b17SAlexander Motin 
189057079b17SAlexander Motin 	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
189157079b17SAlexander Motin 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
189257079b17SAlexander Motin 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
189357079b17SAlexander Motin 	xpt_action((union ccb*)&cts);
189457079b17SAlexander Motin 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
189557079b17SAlexander Motin 		return;
189657079b17SAlexander Motin 	/* Ask the SIM for its base transfer speed */
189757079b17SAlexander Motin 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
189857079b17SAlexander Motin 	cpi.ccb_h.func_code = XPT_PATH_INQ;
189957079b17SAlexander Motin 	xpt_action((union ccb *)&cpi);
190057079b17SAlexander Motin 	/* Report connection speed */
190157079b17SAlexander Motin 	speed = cpi.base_transfer_speed;
190257079b17SAlexander Motin 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_ATA) {
190357079b17SAlexander Motin 		struct	ccb_trans_settings_ata *ata =
190457079b17SAlexander Motin 		    &cts.xport_specific.ata;
190557079b17SAlexander Motin 
190657079b17SAlexander Motin 		if (ata->valid & CTS_ATA_VALID_MODE)
190757079b17SAlexander Motin 			speed = ata_mode2speed(ata->mode);
190857079b17SAlexander Motin 	}
190957079b17SAlexander Motin 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SATA) {
191057079b17SAlexander Motin 		struct	ccb_trans_settings_sata *sata =
191157079b17SAlexander Motin 		    &cts.xport_specific.sata;
191257079b17SAlexander Motin 
191357079b17SAlexander Motin 		if (sata->valid & CTS_SATA_VALID_REVISION)
191457079b17SAlexander Motin 			speed = ata_revision2speed(sata->revision);
191557079b17SAlexander Motin 	}
191657079b17SAlexander Motin 	mb = speed / 1000;
191757079b17SAlexander Motin 	if (mb > 0)
191857079b17SAlexander Motin 		printf("%s%d: %d.%03dMB/s transfers",
191957079b17SAlexander Motin 		       periph->periph_name, periph->unit_number,
192057079b17SAlexander Motin 		       mb, speed % 1000);
192157079b17SAlexander Motin 	else
192257079b17SAlexander Motin 		printf("%s%d: %dKB/s transfers", periph->periph_name,
192357079b17SAlexander Motin 		       periph->unit_number, speed);
192457079b17SAlexander Motin 	/* Report additional information about connection */
192557079b17SAlexander Motin 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_ATA) {
192657079b17SAlexander Motin 		struct ccb_trans_settings_ata *ata =
192757079b17SAlexander Motin 		    &cts.xport_specific.ata;
192857079b17SAlexander Motin 
192957079b17SAlexander Motin 		printf(" (");
193057079b17SAlexander Motin 		if (ata->valid & CTS_ATA_VALID_MODE)
193157079b17SAlexander Motin 			printf("%s, ", ata_mode2string(ata->mode));
193257079b17SAlexander Motin 		if ((ata->valid & CTS_ATA_VALID_ATAPI) && ata->atapi != 0)
193357079b17SAlexander Motin 			printf("ATAPI %dbytes, ", ata->atapi);
193457079b17SAlexander Motin 		if (ata->valid & CTS_ATA_VALID_BYTECOUNT)
193557079b17SAlexander Motin 			printf("PIO %dbytes", ata->bytecount);
193657079b17SAlexander Motin 		printf(")");
193757079b17SAlexander Motin 	}
193857079b17SAlexander Motin 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SATA) {
193957079b17SAlexander Motin 		struct ccb_trans_settings_sata *sata =
194057079b17SAlexander Motin 		    &cts.xport_specific.sata;
194157079b17SAlexander Motin 
194257079b17SAlexander Motin 		printf(" (");
194357079b17SAlexander Motin 		if (sata->valid & CTS_SATA_VALID_REVISION)
194457079b17SAlexander Motin 			printf("SATA %d.x, ", sata->revision);
194557079b17SAlexander Motin 		else
194657079b17SAlexander Motin 			printf("SATA, ");
194757079b17SAlexander Motin 		if (sata->valid & CTS_SATA_VALID_MODE)
194857079b17SAlexander Motin 			printf("%s, ", ata_mode2string(sata->mode));
194957079b17SAlexander Motin 		if ((sata->valid & CTS_ATA_VALID_ATAPI) && sata->atapi != 0)
195057079b17SAlexander Motin 			printf("ATAPI %dbytes, ", sata->atapi);
195157079b17SAlexander Motin 		if (sata->valid & CTS_SATA_VALID_BYTECOUNT)
195257079b17SAlexander Motin 			printf("PIO %dbytes", sata->bytecount);
195357079b17SAlexander Motin 		printf(")");
195457079b17SAlexander Motin 	}
195557079b17SAlexander Motin 	printf("\n");
195657079b17SAlexander Motin }
195757079b17SAlexander Motin 
1958