xref: /freebsd/sys/cam/ata/ata_xpt.c (revision 26bdaedd)
152c9ce25SScott Long /*-
252c9ce25SScott Long  * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org>
352c9ce25SScott Long  * All rights reserved.
452c9ce25SScott Long  *
552c9ce25SScott Long  * Redistribution and use in source and binary forms, with or without
652c9ce25SScott Long  * modification, are permitted provided that the following conditions
752c9ce25SScott Long  * are met:
852c9ce25SScott Long  * 1. Redistributions of source code must retain the above copyright
952c9ce25SScott Long  *    notice, this list of conditions and the following disclaimer,
1052c9ce25SScott Long  *    without modification, immediately at the beginning of the file.
1152c9ce25SScott Long  * 2. Redistributions in binary form must reproduce the above copyright
1252c9ce25SScott Long  *    notice, this list of conditions and the following disclaimer in the
1352c9ce25SScott Long  *    documentation and/or other materials provided with the distribution.
1452c9ce25SScott Long  *
1552c9ce25SScott Long  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1652c9ce25SScott Long  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1752c9ce25SScott Long  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1852c9ce25SScott Long  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1952c9ce25SScott Long  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2052c9ce25SScott Long  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2152c9ce25SScott Long  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2252c9ce25SScott Long  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2352c9ce25SScott Long  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2452c9ce25SScott Long  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2552c9ce25SScott Long  */
2652c9ce25SScott Long 
2752c9ce25SScott Long #include <sys/cdefs.h>
2852c9ce25SScott Long __FBSDID("$FreeBSD$");
2952c9ce25SScott Long 
3052c9ce25SScott Long #include <sys/param.h>
3152c9ce25SScott Long #include <sys/bus.h>
3252c9ce25SScott Long #include <sys/endian.h>
3352c9ce25SScott Long #include <sys/systm.h>
3452c9ce25SScott Long #include <sys/types.h>
3552c9ce25SScott Long #include <sys/malloc.h>
3652c9ce25SScott Long #include <sys/kernel.h>
3752c9ce25SScott Long #include <sys/time.h>
3852c9ce25SScott Long #include <sys/conf.h>
3952c9ce25SScott Long #include <sys/fcntl.h>
4052c9ce25SScott Long #include <sys/interrupt.h>
4152c9ce25SScott Long #include <sys/sbuf.h>
4252c9ce25SScott Long 
4352c9ce25SScott Long #include <sys/lock.h>
4452c9ce25SScott Long #include <sys/mutex.h>
4552c9ce25SScott Long #include <sys/sysctl.h>
4652c9ce25SScott Long 
4752c9ce25SScott Long #include <cam/cam.h>
4852c9ce25SScott Long #include <cam/cam_ccb.h>
4952c9ce25SScott Long #include <cam/cam_queue.h>
5052c9ce25SScott Long #include <cam/cam_periph.h>
5152c9ce25SScott Long #include <cam/cam_sim.h>
5252c9ce25SScott Long #include <cam/cam_xpt.h>
5352c9ce25SScott Long #include <cam/cam_xpt_sim.h>
5452c9ce25SScott Long #include <cam/cam_xpt_periph.h>
5552c9ce25SScott Long #include <cam/cam_xpt_internal.h>
5652c9ce25SScott Long #include <cam/cam_debug.h>
5752c9ce25SScott Long 
5852c9ce25SScott Long #include <cam/scsi/scsi_all.h>
5952c9ce25SScott Long #include <cam/scsi/scsi_message.h>
6052c9ce25SScott Long #include <cam/ata/ata_all.h>
6152c9ce25SScott Long #include <machine/stdarg.h>	/* for xpt_print below */
6252c9ce25SScott Long #include "opt_cam.h"
6352c9ce25SScott Long 
6430a4094fSAlexander Motin struct ata_quirk_entry {
6552c9ce25SScott Long 	struct scsi_inquiry_pattern inq_pat;
6652c9ce25SScott Long 	u_int8_t quirks;
6730a4094fSAlexander Motin #define	CAM_QUIRK_MAXTAGS	0x01
6852c9ce25SScott Long 	u_int maxtags;
6952c9ce25SScott Long };
7052c9ce25SScott Long 
7152c9ce25SScott Long static periph_init_t probe_periph_init;
7252c9ce25SScott Long 
7352c9ce25SScott Long static struct periph_driver probe_driver =
7452c9ce25SScott Long {
75f09f8e3eSAlexander Motin 	probe_periph_init, "aprobe",
761e637ba6SAlexander Motin 	TAILQ_HEAD_INITIALIZER(probe_driver.units), /* generation */ 0,
771e637ba6SAlexander Motin 	CAM_PERIPH_DRV_EARLY
7852c9ce25SScott Long };
7952c9ce25SScott Long 
80f09f8e3eSAlexander Motin PERIPHDRIVER_DECLARE(aprobe, probe_driver);
8152c9ce25SScott Long 
8252c9ce25SScott Long typedef enum {
8352c9ce25SScott Long 	PROBE_RESET,
8452c9ce25SScott Long 	PROBE_IDENTIFY,
854ef08dc5SAlexander Motin 	PROBE_SPINUP,
8652c9ce25SScott Long 	PROBE_SETMODE,
87da6808c1SAlexander Motin 	PROBE_SETPM,
88da6808c1SAlexander Motin 	PROBE_SETAPST,
89da6808c1SAlexander Motin 	PROBE_SETDMAAA,
901e637ba6SAlexander Motin 	PROBE_SET_MULTI,
9152c9ce25SScott Long 	PROBE_INQUIRY,
9252c9ce25SScott Long 	PROBE_FULL_INQUIRY,
9352c9ce25SScott Long 	PROBE_PM_PID,
9452c9ce25SScott Long 	PROBE_PM_PRV,
9552c9ce25SScott Long 	PROBE_INVALID
9652c9ce25SScott Long } probe_action;
9752c9ce25SScott Long 
9852c9ce25SScott Long static char *probe_action_text[] = {
9952c9ce25SScott Long 	"PROBE_RESET",
10052c9ce25SScott Long 	"PROBE_IDENTIFY",
1014ef08dc5SAlexander Motin 	"PROBE_SPINUP",
10252c9ce25SScott Long 	"PROBE_SETMODE",
103da6808c1SAlexander Motin 	"PROBE_SETPM",
104da6808c1SAlexander Motin 	"PROBE_SETAPST",
105da6808c1SAlexander Motin 	"PROBE_SETDMAAA",
1061e637ba6SAlexander Motin 	"PROBE_SET_MULTI",
10752c9ce25SScott Long 	"PROBE_INQUIRY",
10852c9ce25SScott Long 	"PROBE_FULL_INQUIRY",
10952c9ce25SScott Long 	"PROBE_PM_PID",
11052c9ce25SScott Long 	"PROBE_PM_PRV",
11152c9ce25SScott Long 	"PROBE_INVALID"
11252c9ce25SScott Long };
11352c9ce25SScott Long 
11452c9ce25SScott Long #define PROBE_SET_ACTION(softc, newaction)	\
11552c9ce25SScott Long do {									\
11652c9ce25SScott Long 	char **text;							\
11752c9ce25SScott Long 	text = probe_action_text;					\
11852c9ce25SScott Long 	CAM_DEBUG((softc)->periph->path, CAM_DEBUG_INFO,		\
11952c9ce25SScott Long 	    ("Probe %s to %s\n", text[(softc)->action],			\
12052c9ce25SScott Long 	    text[(newaction)]));					\
12152c9ce25SScott Long 	(softc)->action = (newaction);					\
12252c9ce25SScott Long } while(0)
12352c9ce25SScott Long 
12452c9ce25SScott Long typedef enum {
12552c9ce25SScott Long 	PROBE_NO_ANNOUNCE	= 0x04
12652c9ce25SScott Long } probe_flags;
12752c9ce25SScott Long 
12852c9ce25SScott Long typedef struct {
12952c9ce25SScott Long 	TAILQ_HEAD(, ccb_hdr) request_ccbs;
130a9b8edb1SAlexander Motin 	struct ata_params	ident_data;
13152c9ce25SScott Long 	probe_action	action;
13252c9ce25SScott Long 	probe_flags	flags;
13352c9ce25SScott Long 	uint32_t	pm_pid;
13452c9ce25SScott Long 	uint32_t	pm_prv;
13583c5d981SAlexander Motin 	int		restart;
1364ef08dc5SAlexander Motin 	int		spinup;
13725a519a9SAlexander Motin 	int		faults;
138da6808c1SAlexander Motin 	u_int		caps;
13952c9ce25SScott Long 	struct cam_periph *periph;
14052c9ce25SScott Long } probe_softc;
14152c9ce25SScott Long 
14230a4094fSAlexander Motin static struct ata_quirk_entry ata_quirk_table[] =
14352c9ce25SScott Long {
14452c9ce25SScott Long 	{
14552c9ce25SScott Long 		/* Default tagged queuing parameters for all devices */
14652c9ce25SScott Long 		{
14752c9ce25SScott Long 		  T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
14852c9ce25SScott Long 		  /*vendor*/"*", /*product*/"*", /*revision*/"*"
14952c9ce25SScott Long 		},
15030a4094fSAlexander Motin 		/*quirks*/0, /*maxtags*/0
15152c9ce25SScott Long 	},
15252c9ce25SScott Long };
15352c9ce25SScott Long 
15430a4094fSAlexander Motin static const int ata_quirk_table_size =
15530a4094fSAlexander Motin 	sizeof(ata_quirk_table) / sizeof(*ata_quirk_table);
15652c9ce25SScott Long 
15752c9ce25SScott Long static cam_status	proberegister(struct cam_periph *periph,
15852c9ce25SScott Long 				      void *arg);
15952c9ce25SScott Long static void	 probeschedule(struct cam_periph *probe_periph);
16052c9ce25SScott Long static void	 probestart(struct cam_periph *periph, union ccb *start_ccb);
16152c9ce25SScott Long //static void	 proberequestdefaultnegotiation(struct cam_periph *periph);
16252c9ce25SScott Long //static int       proberequestbackoff(struct cam_periph *periph,
16352c9ce25SScott Long //				     struct cam_ed *device);
16452c9ce25SScott Long static void	 probedone(struct cam_periph *periph, union ccb *done_ccb);
16552c9ce25SScott Long static void	 probecleanup(struct cam_periph *periph);
16630a4094fSAlexander Motin static void	 ata_find_quirk(struct cam_ed *device);
16752c9ce25SScott Long static void	 ata_scan_bus(struct cam_periph *periph, union ccb *ccb);
16852c9ce25SScott Long static void	 ata_scan_lun(struct cam_periph *periph,
16952c9ce25SScott Long 			       struct cam_path *path, cam_flags flags,
17052c9ce25SScott Long 			       union ccb *ccb);
17152c9ce25SScott Long static void	 xptscandone(struct cam_periph *periph, union ccb *done_ccb);
17252c9ce25SScott Long static struct cam_ed *
17352c9ce25SScott Long 		 ata_alloc_device(struct cam_eb *bus, struct cam_et *target,
17452c9ce25SScott Long 				   lun_id_t lun_id);
17552c9ce25SScott Long static void	 ata_device_transport(struct cam_path *path);
17630a4094fSAlexander Motin static void	 ata_set_transfer_settings(struct ccb_trans_settings *cts,
17752c9ce25SScott Long 					    struct cam_ed *device,
17852c9ce25SScott Long 					    int async_update);
17952c9ce25SScott Long static void	 ata_dev_async(u_int32_t async_code,
18052c9ce25SScott Long 				struct cam_eb *bus,
18152c9ce25SScott Long 				struct cam_et *target,
18252c9ce25SScott Long 				struct cam_ed *device,
18352c9ce25SScott Long 				void *async_arg);
18452c9ce25SScott Long static void	 ata_action(union ccb *start_ccb);
18557079b17SAlexander Motin static void	 ata_announce_periph(struct cam_periph *periph);
18652c9ce25SScott Long 
18752c9ce25SScott Long static struct xpt_xport ata_xport = {
18852c9ce25SScott Long 	.alloc_device = ata_alloc_device,
18952c9ce25SScott Long 	.action = ata_action,
19052c9ce25SScott Long 	.async = ata_dev_async,
19157079b17SAlexander Motin 	.announce = ata_announce_periph,
19252c9ce25SScott Long };
19352c9ce25SScott Long 
19452c9ce25SScott Long struct xpt_xport *
19552c9ce25SScott Long ata_get_xport(void)
19652c9ce25SScott Long {
19752c9ce25SScott Long 	return (&ata_xport);
19852c9ce25SScott Long }
19952c9ce25SScott Long 
20052c9ce25SScott Long static void
20152c9ce25SScott Long probe_periph_init()
20252c9ce25SScott Long {
20352c9ce25SScott Long }
20452c9ce25SScott Long 
20552c9ce25SScott Long static cam_status
20652c9ce25SScott Long proberegister(struct cam_periph *periph, void *arg)
20752c9ce25SScott Long {
20852c9ce25SScott Long 	union ccb *request_ccb;	/* CCB representing the probe request */
20952c9ce25SScott Long 	cam_status status;
21052c9ce25SScott Long 	probe_softc *softc;
21152c9ce25SScott Long 
21252c9ce25SScott Long 	request_ccb = (union ccb *)arg;
21352c9ce25SScott Long 	if (periph == NULL) {
21452c9ce25SScott Long 		printf("proberegister: periph was NULL!!\n");
21552c9ce25SScott Long 		return(CAM_REQ_CMP_ERR);
21652c9ce25SScott Long 	}
21752c9ce25SScott Long 
21852c9ce25SScott Long 	if (request_ccb == NULL) {
21952c9ce25SScott Long 		printf("proberegister: no probe CCB, "
22052c9ce25SScott Long 		       "can't register device\n");
22152c9ce25SScott Long 		return(CAM_REQ_CMP_ERR);
22252c9ce25SScott Long 	}
22352c9ce25SScott Long 
2244ef08dc5SAlexander Motin 	softc = (probe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_ZERO | M_NOWAIT);
22552c9ce25SScott Long 
22652c9ce25SScott Long 	if (softc == NULL) {
22752c9ce25SScott Long 		printf("proberegister: Unable to probe new device. "
22852c9ce25SScott Long 		       "Unable to allocate softc\n");
22952c9ce25SScott Long 		return(CAM_REQ_CMP_ERR);
23052c9ce25SScott Long 	}
23152c9ce25SScott Long 	TAILQ_INIT(&softc->request_ccbs);
23252c9ce25SScott Long 	TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
23352c9ce25SScott Long 			  periph_links.tqe);
23452c9ce25SScott Long 	softc->flags = 0;
23552c9ce25SScott Long 	periph->softc = softc;
23652c9ce25SScott Long 	softc->periph = periph;
23752c9ce25SScott Long 	softc->action = PROBE_INVALID;
23852c9ce25SScott Long 	status = cam_periph_acquire(periph);
23952c9ce25SScott Long 	if (status != CAM_REQ_CMP) {
24052c9ce25SScott Long 		return (status);
24152c9ce25SScott Long 	}
24252c9ce25SScott Long 	/*
24383c5d981SAlexander Motin 	 * Ensure nobody slip in until probe finish.
24452c9ce25SScott Long 	 */
24583c5d981SAlexander Motin 	cam_freeze_devq_arg(periph->path,
24683c5d981SAlexander Motin 	    RELSIM_RELEASE_RUNLEVEL, CAM_RL_XPT + 1);
24752c9ce25SScott Long 	probeschedule(periph);
24852c9ce25SScott Long 	return(CAM_REQ_CMP);
24952c9ce25SScott Long }
25052c9ce25SScott Long 
25152c9ce25SScott Long static void
25252c9ce25SScott Long probeschedule(struct cam_periph *periph)
25352c9ce25SScott Long {
25452c9ce25SScott Long 	union ccb *ccb;
25552c9ce25SScott Long 	probe_softc *softc;
25652c9ce25SScott Long 
25752c9ce25SScott Long 	softc = (probe_softc *)periph->softc;
25852c9ce25SScott Long 	ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
25952c9ce25SScott Long 
26065d0fb03SAlexander Motin 	if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) ||
26165d0fb03SAlexander Motin 	    periph->path->device->protocol == PROTO_SATAPM)
26252c9ce25SScott Long 		PROBE_SET_ACTION(softc, PROBE_RESET);
26352c9ce25SScott Long 	else
26452c9ce25SScott Long 		PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
26552c9ce25SScott Long 
26652c9ce25SScott Long 	if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
26752c9ce25SScott Long 		softc->flags |= PROBE_NO_ANNOUNCE;
26852c9ce25SScott Long 	else
26952c9ce25SScott Long 		softc->flags &= ~PROBE_NO_ANNOUNCE;
27052c9ce25SScott Long 
27183c5d981SAlexander Motin 	xpt_schedule(periph, CAM_PRIORITY_XPT);
27252c9ce25SScott Long }
27352c9ce25SScott Long 
27452c9ce25SScott Long static void
27552c9ce25SScott Long probestart(struct cam_periph *periph, union ccb *start_ccb)
27652c9ce25SScott Long {
277c8039fc6SAlexander Motin 	struct ccb_trans_settings cts;
27852c9ce25SScott Long 	struct ccb_ataio *ataio;
27952c9ce25SScott Long 	struct ccb_scsiio *csio;
28052c9ce25SScott Long 	probe_softc *softc;
2811e637ba6SAlexander Motin 	struct cam_path *path;
2821e637ba6SAlexander Motin 	struct ata_params *ident_buf;
28352c9ce25SScott Long 
28452c9ce25SScott Long 	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
28552c9ce25SScott Long 
28652c9ce25SScott Long 	softc = (probe_softc *)periph->softc;
2871e637ba6SAlexander Motin 	path = start_ccb->ccb_h.path;
28852c9ce25SScott Long 	ataio = &start_ccb->ataio;
28952c9ce25SScott Long 	csio = &start_ccb->csio;
2901e637ba6SAlexander Motin 	ident_buf = &periph->path->device->ident_data;
29152c9ce25SScott Long 
29283c5d981SAlexander Motin 	if (softc->restart) {
29383c5d981SAlexander Motin 		softc->restart = 0;
29483c5d981SAlexander Motin 		if ((path->device->flags & CAM_DEV_UNCONFIGURED) ||
29583c5d981SAlexander Motin 		    path->device->protocol == PROTO_SATAPM)
29683c5d981SAlexander Motin 			softc->action = PROBE_RESET;
29783c5d981SAlexander Motin 		else
29883c5d981SAlexander Motin 			softc->action = PROBE_IDENTIFY;
29983c5d981SAlexander Motin 	}
30052c9ce25SScott Long 	switch (softc->action) {
30152c9ce25SScott Long 	case PROBE_RESET:
30252c9ce25SScott Long 		cam_fill_ataio(ataio,
30352c9ce25SScott Long 		      0,
30452c9ce25SScott Long 		      probedone,
30552c9ce25SScott Long 		      /*flags*/CAM_DIR_NONE,
30665d0fb03SAlexander Motin 		      0,
30752c9ce25SScott Long 		      /*data_ptr*/NULL,
30852c9ce25SScott Long 		      /*dxfer_len*/0,
30983c5d981SAlexander Motin 		      15 * 1000);
31052c9ce25SScott Long 		ata_reset_cmd(ataio);
31152c9ce25SScott Long 		break;
31252c9ce25SScott Long 	case PROBE_IDENTIFY:
31352c9ce25SScott Long 		cam_fill_ataio(ataio,
31452c9ce25SScott Long 		      1,
31552c9ce25SScott Long 		      probedone,
31652c9ce25SScott Long 		      /*flags*/CAM_DIR_IN,
31765d0fb03SAlexander Motin 		      0,
318a9b8edb1SAlexander Motin 		      /*data_ptr*/(u_int8_t *)&softc->ident_data,
319a9b8edb1SAlexander Motin 		      /*dxfer_len*/sizeof(softc->ident_data),
32052c9ce25SScott Long 		      30 * 1000);
32152c9ce25SScott Long 		if (periph->path->device->protocol == PROTO_ATA)
3227606b445SAlexander Motin 			ata_28bit_cmd(ataio, ATA_ATA_IDENTIFY, 0, 0, 0);
32352c9ce25SScott Long 		else
3247606b445SAlexander Motin 			ata_28bit_cmd(ataio, ATA_ATAPI_IDENTIFY, 0, 0, 0);
32552c9ce25SScott Long 		break;
3264ef08dc5SAlexander Motin 	case PROBE_SPINUP:
3274ef08dc5SAlexander Motin 		if (bootverbose)
3284ef08dc5SAlexander Motin 			xpt_print(path, "Spinning up device\n");
3294ef08dc5SAlexander Motin 		cam_fill_ataio(ataio,
3304ef08dc5SAlexander Motin 		      1,
3314ef08dc5SAlexander Motin 		      probedone,
3324ef08dc5SAlexander Motin 		      /*flags*/CAM_DIR_NONE | CAM_HIGH_POWER,
3334ef08dc5SAlexander Motin 		      0,
3344ef08dc5SAlexander Motin 		      /*data_ptr*/NULL,
3354ef08dc5SAlexander Motin 		      /*dxfer_len*/0,
3364ef08dc5SAlexander Motin 		      30 * 1000);
3374ef08dc5SAlexander Motin 		ata_28bit_cmd(ataio, ATA_SETFEATURES, ATA_SF_PUIS_SPINUP, 0, 0);
3384ef08dc5SAlexander Motin 		break;
33952c9ce25SScott Long 	case PROBE_SETMODE:
340c8039fc6SAlexander Motin 	{
341c8039fc6SAlexander Motin 		int mode, wantmode;
342c8039fc6SAlexander Motin 
343c8039fc6SAlexander Motin 		mode = 0;
344c8039fc6SAlexander Motin 		/* Fetch user modes from SIM. */
345c8039fc6SAlexander Motin 		bzero(&cts, sizeof(cts));
34683c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
347c8039fc6SAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
348c8039fc6SAlexander Motin 		cts.type = CTS_TYPE_USER_SETTINGS;
349c8039fc6SAlexander Motin 		xpt_action((union ccb *)&cts);
350c8039fc6SAlexander Motin 		if (path->device->transport == XPORT_ATA) {
351c8039fc6SAlexander Motin 			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_MODE)
352c8039fc6SAlexander Motin 				mode = cts.xport_specific.ata.mode;
353c8039fc6SAlexander Motin 		} else {
3541a6f09b8SAlexander Motin 			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_MODE)
355c8039fc6SAlexander Motin 				mode = cts.xport_specific.sata.mode;
356c8039fc6SAlexander Motin 		}
357c8039fc6SAlexander Motin negotiate:
358c8039fc6SAlexander Motin 		/* Honor device capabilities. */
359c8039fc6SAlexander Motin 		wantmode = mode = ata_max_mode(ident_buf, mode);
360c8039fc6SAlexander Motin 		/* Report modes to SIM. */
361c8039fc6SAlexander Motin 		bzero(&cts, sizeof(cts));
36283c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
363c8039fc6SAlexander Motin 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
364c8039fc6SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
365c8039fc6SAlexander Motin 		if (path->device->transport == XPORT_ATA) {
366c8039fc6SAlexander Motin 			cts.xport_specific.ata.mode = mode;
367c8039fc6SAlexander Motin 			cts.xport_specific.ata.valid = CTS_ATA_VALID_MODE;
368c8039fc6SAlexander Motin 		} else {
369c8039fc6SAlexander Motin 			cts.xport_specific.sata.mode = mode;
370c8039fc6SAlexander Motin 			cts.xport_specific.sata.valid = CTS_SATA_VALID_MODE;
371c8039fc6SAlexander Motin 		}
372c8039fc6SAlexander Motin 		xpt_action((union ccb *)&cts);
373066f913aSAlexander Motin 		/* Fetch current modes from SIM. */
374c8039fc6SAlexander Motin 		bzero(&cts, sizeof(cts));
37583c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
376c8039fc6SAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
377c8039fc6SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
378c8039fc6SAlexander Motin 		xpt_action((union ccb *)&cts);
379c8039fc6SAlexander Motin 		if (path->device->transport == XPORT_ATA) {
380c8039fc6SAlexander Motin 			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_MODE)
381c8039fc6SAlexander Motin 				mode = cts.xport_specific.ata.mode;
382c8039fc6SAlexander Motin 		} else {
383c8039fc6SAlexander Motin 			if (cts.xport_specific.ata.valid & CTS_SATA_VALID_MODE)
384c8039fc6SAlexander Motin 				mode = cts.xport_specific.sata.mode;
385c8039fc6SAlexander Motin 		}
386c8039fc6SAlexander Motin 		/* If SIM disagree - renegotiate. */
387c8039fc6SAlexander Motin 		if (mode != wantmode)
388c8039fc6SAlexander Motin 			goto negotiate;
38952c9ce25SScott Long 		cam_fill_ataio(ataio,
39052c9ce25SScott Long 		      1,
39152c9ce25SScott Long 		      probedone,
3925daa555dSAlexander Motin 		      /*flags*/CAM_DIR_NONE,
3935daa555dSAlexander Motin 		      0,
3945daa555dSAlexander Motin 		      /*data_ptr*/NULL,
3955daa555dSAlexander Motin 		      /*dxfer_len*/0,
39652c9ce25SScott Long 		      30 * 1000);
397c8039fc6SAlexander Motin 		ata_28bit_cmd(ataio, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
39852c9ce25SScott Long 		break;
399c8039fc6SAlexander Motin 	}
400da6808c1SAlexander Motin 	case PROBE_SETPM:
401da6808c1SAlexander Motin 		cam_fill_ataio(ataio,
402da6808c1SAlexander Motin 		    1,
403da6808c1SAlexander Motin 		    probedone,
404da6808c1SAlexander Motin 		    CAM_DIR_NONE,
405da6808c1SAlexander Motin 		    0,
406da6808c1SAlexander Motin 		    NULL,
407da6808c1SAlexander Motin 		    0,
408da6808c1SAlexander Motin 		    30*1000);
409da6808c1SAlexander Motin 		ata_28bit_cmd(ataio, ATA_SETFEATURES,
410da6808c1SAlexander Motin 		    (softc->caps & CTS_SATA_CAPS_H_PMREQ) ? 0x10 : 0x90,
411da6808c1SAlexander Motin 		    0, 0x03);
412da6808c1SAlexander Motin 		break;
413da6808c1SAlexander Motin 	case PROBE_SETAPST:
414da6808c1SAlexander Motin 		cam_fill_ataio(ataio,
415da6808c1SAlexander Motin 		    1,
416da6808c1SAlexander Motin 		    probedone,
417da6808c1SAlexander Motin 		    CAM_DIR_NONE,
418da6808c1SAlexander Motin 		    0,
419da6808c1SAlexander Motin 		    NULL,
420da6808c1SAlexander Motin 		    0,
421da6808c1SAlexander Motin 		    30*1000);
422da6808c1SAlexander Motin 		ata_28bit_cmd(ataio, ATA_SETFEATURES,
423da6808c1SAlexander Motin 		    (softc->caps & CTS_SATA_CAPS_H_APST) ? 0x10 : 0x90,
424da6808c1SAlexander Motin 		    0, 0x07);
425da6808c1SAlexander Motin 		break;
426da6808c1SAlexander Motin 	case PROBE_SETDMAAA:
427da6808c1SAlexander Motin 		cam_fill_ataio(ataio,
428da6808c1SAlexander Motin 		    1,
429da6808c1SAlexander Motin 		    probedone,
430da6808c1SAlexander Motin 		    CAM_DIR_NONE,
431da6808c1SAlexander Motin 		    0,
432da6808c1SAlexander Motin 		    NULL,
433da6808c1SAlexander Motin 		    0,
434da6808c1SAlexander Motin 		    30*1000);
435da6808c1SAlexander Motin 		ata_28bit_cmd(ataio, ATA_SETFEATURES,
436da6808c1SAlexander Motin 		    (softc->caps & CTS_SATA_CAPS_H_DMAAA) ? 0x10 : 0x90,
437da6808c1SAlexander Motin 		    0, 0x02);
438da6808c1SAlexander Motin 		break;
4391e637ba6SAlexander Motin 	case PROBE_SET_MULTI:
4401e637ba6SAlexander Motin 	{
441066f913aSAlexander Motin 		u_int sectors, bytecount;
4421e637ba6SAlexander Motin 
443066f913aSAlexander Motin 		bytecount = 8192;	/* SATA maximum */
444066f913aSAlexander Motin 		/* Fetch user bytecount from SIM. */
445066f913aSAlexander Motin 		bzero(&cts, sizeof(cts));
44683c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
447066f913aSAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
448066f913aSAlexander Motin 		cts.type = CTS_TYPE_USER_SETTINGS;
449066f913aSAlexander Motin 		xpt_action((union ccb *)&cts);
450066f913aSAlexander Motin 		if (path->device->transport == XPORT_ATA) {
451066f913aSAlexander Motin 			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
452066f913aSAlexander Motin 				bytecount = cts.xport_specific.ata.bytecount;
453066f913aSAlexander Motin 		} else {
454066f913aSAlexander Motin 			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
455066f913aSAlexander Motin 				bytecount = cts.xport_specific.sata.bytecount;
456066f913aSAlexander Motin 		}
457066f913aSAlexander Motin 		/* Honor device capabilities. */
458066f913aSAlexander Motin 		sectors = max(1, min(ident_buf->sectors_intr & 0xff,
459066f913aSAlexander Motin 		    bytecount / ata_logical_sector_size(ident_buf)));
4601e637ba6SAlexander Motin 		/* Report bytecount to SIM. */
4611e637ba6SAlexander Motin 		bzero(&cts, sizeof(cts));
46283c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
4631e637ba6SAlexander Motin 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
4641e637ba6SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
4651e637ba6SAlexander Motin 		if (path->device->transport == XPORT_ATA) {
466c1bd46c2SAlexander Motin 			cts.xport_specific.ata.bytecount = sectors *
467c1bd46c2SAlexander Motin 			    ata_logical_sector_size(ident_buf);
4681e637ba6SAlexander Motin 			cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT;
4691e637ba6SAlexander Motin 		} else {
470c1bd46c2SAlexander Motin 			cts.xport_specific.sata.bytecount = sectors *
471c1bd46c2SAlexander Motin 			    ata_logical_sector_size(ident_buf);
4721e637ba6SAlexander Motin 			cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT;
4731e637ba6SAlexander Motin 		}
4741e637ba6SAlexander Motin 		xpt_action((union ccb *)&cts);
475066f913aSAlexander Motin 		/* Fetch current bytecount from SIM. */
476066f913aSAlexander Motin 		bzero(&cts, sizeof(cts));
47783c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
478066f913aSAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
479066f913aSAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
480066f913aSAlexander Motin 		xpt_action((union ccb *)&cts);
481066f913aSAlexander Motin 		if (path->device->transport == XPORT_ATA) {
482066f913aSAlexander Motin 			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
483066f913aSAlexander Motin 				bytecount = cts.xport_specific.ata.bytecount;
484066f913aSAlexander Motin 		} else {
485066f913aSAlexander Motin 			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
486066f913aSAlexander Motin 				bytecount = cts.xport_specific.sata.bytecount;
487066f913aSAlexander Motin 		}
488066f913aSAlexander Motin 		sectors = bytecount / ata_logical_sector_size(ident_buf);
4891e637ba6SAlexander Motin 
4901e637ba6SAlexander Motin 		cam_fill_ataio(ataio,
4911e637ba6SAlexander Motin 		    1,
4921e637ba6SAlexander Motin 		    probedone,
4931e637ba6SAlexander Motin 		    CAM_DIR_NONE,
4941e637ba6SAlexander Motin 		    0,
4951e637ba6SAlexander Motin 		    NULL,
4961e637ba6SAlexander Motin 		    0,
4971e637ba6SAlexander Motin 		    30*1000);
4981e637ba6SAlexander Motin 		ata_28bit_cmd(ataio, ATA_SET_MULTI, 0, 0, sectors);
4991e637ba6SAlexander Motin 		break;
50052c9ce25SScott Long 	}
50152c9ce25SScott Long 	case PROBE_INQUIRY:
502066f913aSAlexander Motin 	{
503066f913aSAlexander Motin 		u_int bytecount;
504066f913aSAlexander Motin 
505066f913aSAlexander Motin 		bytecount = 8192;	/* SATA maximum */
506066f913aSAlexander Motin 		/* Fetch user bytecount from SIM. */
507066f913aSAlexander Motin 		bzero(&cts, sizeof(cts));
50883c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
509066f913aSAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
510066f913aSAlexander Motin 		cts.type = CTS_TYPE_USER_SETTINGS;
511066f913aSAlexander Motin 		xpt_action((union ccb *)&cts);
512066f913aSAlexander Motin 		if (path->device->transport == XPORT_ATA) {
513066f913aSAlexander Motin 			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
514066f913aSAlexander Motin 				bytecount = cts.xport_specific.ata.bytecount;
515066f913aSAlexander Motin 		} else {
516066f913aSAlexander Motin 			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
517066f913aSAlexander Motin 				bytecount = cts.xport_specific.sata.bytecount;
518066f913aSAlexander Motin 		}
519066f913aSAlexander Motin 		/* Honor device capabilities. */
520066f913aSAlexander Motin 		bytecount &= ~1;
521066f913aSAlexander Motin 		bytecount = max(2, min(65534, bytecount));
522066f913aSAlexander Motin 		if (ident_buf->satacapabilities != 0x0000 &&
523066f913aSAlexander Motin 		    ident_buf->satacapabilities != 0xffff) {
524066f913aSAlexander Motin 			bytecount = min(8192, bytecount);
525066f913aSAlexander Motin 		}
526066f913aSAlexander Motin 		/* Report bytecount to SIM. */
527066f913aSAlexander Motin 		bzero(&cts, sizeof(cts));
52883c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
529066f913aSAlexander Motin 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
530066f913aSAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
531066f913aSAlexander Motin 		if (path->device->transport == XPORT_ATA) {
532066f913aSAlexander Motin 			cts.xport_specific.ata.bytecount = bytecount;
533066f913aSAlexander Motin 			cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT;
534066f913aSAlexander Motin 		} else {
535066f913aSAlexander Motin 			cts.xport_specific.sata.bytecount = bytecount;
536066f913aSAlexander Motin 			cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT;
537066f913aSAlexander Motin 		}
538066f913aSAlexander Motin 		xpt_action((union ccb *)&cts);
539066f913aSAlexander Motin 		/* FALLTHROUGH */
540066f913aSAlexander Motin 	}
54152c9ce25SScott Long 	case PROBE_FULL_INQUIRY:
54252c9ce25SScott Long 	{
54352c9ce25SScott Long 		u_int inquiry_len;
54452c9ce25SScott Long 		struct scsi_inquiry_data *inq_buf =
54552c9ce25SScott Long 		    &periph->path->device->inq_data;
54652c9ce25SScott Long 
54752c9ce25SScott Long 		if (softc->action == PROBE_INQUIRY)
54852c9ce25SScott Long 			inquiry_len = SHORT_INQUIRY_LENGTH;
54952c9ce25SScott Long 		else
55052c9ce25SScott Long 			inquiry_len = SID_ADDITIONAL_LENGTH(inq_buf);
55152c9ce25SScott Long 		/*
55252c9ce25SScott Long 		 * Some parallel SCSI devices fail to send an
55352c9ce25SScott Long 		 * ignore wide residue message when dealing with
55452c9ce25SScott Long 		 * odd length inquiry requests.  Round up to be
55552c9ce25SScott Long 		 * safe.
55652c9ce25SScott Long 		 */
55752c9ce25SScott Long 		inquiry_len = roundup2(inquiry_len, 2);
55852c9ce25SScott Long 		scsi_inquiry(csio,
55952c9ce25SScott Long 			     /*retries*/1,
56052c9ce25SScott Long 			     probedone,
56152c9ce25SScott Long 			     MSG_SIMPLE_Q_TAG,
56252c9ce25SScott Long 			     (u_int8_t *)inq_buf,
56352c9ce25SScott Long 			     inquiry_len,
56452c9ce25SScott Long 			     /*evpd*/FALSE,
56552c9ce25SScott Long 			     /*page_code*/0,
56652c9ce25SScott Long 			     SSD_MIN_SIZE,
56752c9ce25SScott Long 			     /*timeout*/60 * 1000);
56852c9ce25SScott Long 		break;
56952c9ce25SScott Long 	}
57052c9ce25SScott Long 	case PROBE_PM_PID:
57152c9ce25SScott Long 		cam_fill_ataio(ataio,
57252c9ce25SScott Long 		      1,
57352c9ce25SScott Long 		      probedone,
57452c9ce25SScott Long 		      /*flags*/CAM_DIR_NONE,
57565d0fb03SAlexander Motin 		      0,
57652c9ce25SScott Long 		      /*data_ptr*/NULL,
57752c9ce25SScott Long 		      /*dxfer_len*/0,
57852c9ce25SScott Long 		      10 * 1000);
57952c9ce25SScott Long 		ata_pm_read_cmd(ataio, 0, 15);
58052c9ce25SScott Long 		break;
58152c9ce25SScott Long 	case PROBE_PM_PRV:
58252c9ce25SScott Long 		cam_fill_ataio(ataio,
58352c9ce25SScott Long 		      1,
58452c9ce25SScott Long 		      probedone,
58552c9ce25SScott Long 		      /*flags*/CAM_DIR_NONE,
58665d0fb03SAlexander Motin 		      0,
58752c9ce25SScott Long 		      /*data_ptr*/NULL,
58852c9ce25SScott Long 		      /*dxfer_len*/0,
58952c9ce25SScott Long 		      10 * 1000);
59052c9ce25SScott Long 		ata_pm_read_cmd(ataio, 1, 15);
59152c9ce25SScott Long 		break;
59252c9ce25SScott Long 	case PROBE_INVALID:
5931e637ba6SAlexander Motin 		CAM_DEBUG(path, CAM_DEBUG_INFO,
59452c9ce25SScott Long 		    ("probestart: invalid action state\n"));
59552c9ce25SScott Long 	default:
59652c9ce25SScott Long 		break;
59752c9ce25SScott Long 	}
59852c9ce25SScott Long 	xpt_action(start_ccb);
59952c9ce25SScott Long }
60052c9ce25SScott Long #if 0
60152c9ce25SScott Long static void
60252c9ce25SScott Long proberequestdefaultnegotiation(struct cam_periph *periph)
60352c9ce25SScott Long {
60452c9ce25SScott Long 	struct ccb_trans_settings cts;
60552c9ce25SScott Long 
60683c5d981SAlexander Motin 	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
60752c9ce25SScott Long 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
60852c9ce25SScott Long 	cts.type = CTS_TYPE_USER_SETTINGS;
60952c9ce25SScott Long 	xpt_action((union ccb *)&cts);
61052c9ce25SScott Long 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
61152c9ce25SScott Long 		return;
61252c9ce25SScott Long 	}
61352c9ce25SScott Long 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
61452c9ce25SScott Long 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
61552c9ce25SScott Long 	xpt_action((union ccb *)&cts);
61652c9ce25SScott Long }
61752c9ce25SScott Long 
61852c9ce25SScott Long /*
61952c9ce25SScott Long  * Backoff Negotiation Code- only pertinent for SPI devices.
62052c9ce25SScott Long  */
62152c9ce25SScott Long static int
62252c9ce25SScott Long proberequestbackoff(struct cam_periph *periph, struct cam_ed *device)
62352c9ce25SScott Long {
62452c9ce25SScott Long 	struct ccb_trans_settings cts;
62552c9ce25SScott Long 	struct ccb_trans_settings_spi *spi;
62652c9ce25SScott Long 
62752c9ce25SScott Long 	memset(&cts, 0, sizeof (cts));
62883c5d981SAlexander Motin 	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
62952c9ce25SScott Long 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
63052c9ce25SScott Long 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
63152c9ce25SScott Long 	xpt_action((union ccb *)&cts);
63252c9ce25SScott Long 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
63352c9ce25SScott Long 		if (bootverbose) {
63452c9ce25SScott Long 			xpt_print(periph->path,
63552c9ce25SScott Long 			    "failed to get current device settings\n");
63652c9ce25SScott Long 		}
63752c9ce25SScott Long 		return (0);
63852c9ce25SScott Long 	}
63952c9ce25SScott Long 	if (cts.transport != XPORT_SPI) {
64052c9ce25SScott Long 		if (bootverbose) {
64152c9ce25SScott Long 			xpt_print(periph->path, "not SPI transport\n");
64252c9ce25SScott Long 		}
64352c9ce25SScott Long 		return (0);
64452c9ce25SScott Long 	}
64552c9ce25SScott Long 	spi = &cts.xport_specific.spi;
64652c9ce25SScott Long 
64752c9ce25SScott Long 	/*
64852c9ce25SScott Long 	 * We cannot renegotiate sync rate if we don't have one.
64952c9ce25SScott Long 	 */
65052c9ce25SScott Long 	if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
65152c9ce25SScott Long 		if (bootverbose) {
65252c9ce25SScott Long 			xpt_print(periph->path, "no sync rate known\n");
65352c9ce25SScott Long 		}
65452c9ce25SScott Long 		return (0);
65552c9ce25SScott Long 	}
65652c9ce25SScott Long 
65752c9ce25SScott Long 	/*
65852c9ce25SScott Long 	 * We'll assert that we don't have to touch PPR options- the
65952c9ce25SScott Long 	 * SIM will see what we do with period and offset and adjust
66052c9ce25SScott Long 	 * the PPR options as appropriate.
66152c9ce25SScott Long 	 */
66252c9ce25SScott Long 
66352c9ce25SScott Long 	/*
66452c9ce25SScott Long 	 * A sync rate with unknown or zero offset is nonsensical.
66552c9ce25SScott Long 	 * A sync period of zero means Async.
66652c9ce25SScott Long 	 */
66752c9ce25SScott Long 	if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0
66852c9ce25SScott Long 	 || spi->sync_offset == 0 || spi->sync_period == 0) {
66952c9ce25SScott Long 		if (bootverbose) {
67052c9ce25SScott Long 			xpt_print(periph->path, "no sync rate available\n");
67152c9ce25SScott Long 		}
67252c9ce25SScott Long 		return (0);
67352c9ce25SScott Long 	}
67452c9ce25SScott Long 
67552c9ce25SScott Long 	if (device->flags & CAM_DEV_DV_HIT_BOTTOM) {
67652c9ce25SScott Long 		CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
67752c9ce25SScott Long 		    ("hit async: giving up on DV\n"));
67852c9ce25SScott Long 		return (0);
67952c9ce25SScott Long 	}
68052c9ce25SScott Long 
68152c9ce25SScott Long 
68252c9ce25SScott Long 	/*
68352c9ce25SScott Long 	 * Jump sync_period up by one, but stop at 5MHz and fall back to Async.
68452c9ce25SScott Long 	 * We don't try to remember 'last' settings to see if the SIM actually
68552c9ce25SScott Long 	 * gets into the speed we want to set. We check on the SIM telling
68652c9ce25SScott Long 	 * us that a requested speed is bad, but otherwise don't try and
68752c9ce25SScott Long 	 * check the speed due to the asynchronous and handshake nature
68852c9ce25SScott Long 	 * of speed setting.
68952c9ce25SScott Long 	 */
69052c9ce25SScott Long 	spi->valid = CTS_SPI_VALID_SYNC_RATE | CTS_SPI_VALID_SYNC_OFFSET;
69152c9ce25SScott Long 	for (;;) {
69252c9ce25SScott Long 		spi->sync_period++;
69352c9ce25SScott Long 		if (spi->sync_period >= 0xf) {
69452c9ce25SScott Long 			spi->sync_period = 0;
69552c9ce25SScott Long 			spi->sync_offset = 0;
69652c9ce25SScott Long 			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
69752c9ce25SScott Long 			    ("setting to async for DV\n"));
69852c9ce25SScott Long 			/*
69952c9ce25SScott Long 			 * Once we hit async, we don't want to try
70052c9ce25SScott Long 			 * any more settings.
70152c9ce25SScott Long 			 */
70252c9ce25SScott Long 			device->flags |= CAM_DEV_DV_HIT_BOTTOM;
70352c9ce25SScott Long 		} else if (bootverbose) {
70452c9ce25SScott Long 			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
70552c9ce25SScott Long 			    ("DV: period 0x%x\n", spi->sync_period));
70652c9ce25SScott Long 			printf("setting period to 0x%x\n", spi->sync_period);
70752c9ce25SScott Long 		}
70852c9ce25SScott Long 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
70952c9ce25SScott Long 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
71052c9ce25SScott Long 		xpt_action((union ccb *)&cts);
71152c9ce25SScott Long 		if ((cts.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
71252c9ce25SScott Long 			break;
71352c9ce25SScott Long 		}
71452c9ce25SScott Long 		CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
71552c9ce25SScott Long 		    ("DV: failed to set period 0x%x\n", spi->sync_period));
71652c9ce25SScott Long 		if (spi->sync_period == 0) {
71752c9ce25SScott Long 			return (0);
71852c9ce25SScott Long 		}
71952c9ce25SScott Long 	}
72052c9ce25SScott Long 	return (1);
72152c9ce25SScott Long }
72252c9ce25SScott Long #endif
72352c9ce25SScott Long static void
72452c9ce25SScott Long probedone(struct cam_periph *periph, union ccb *done_ccb)
72552c9ce25SScott Long {
726c8039fc6SAlexander Motin 	struct ccb_trans_settings cts;
72752c9ce25SScott Long 	struct ata_params *ident_buf;
72852c9ce25SScott Long 	probe_softc *softc;
72952c9ce25SScott Long 	struct cam_path *path;
73026bdaeddSAlexander Motin 	cam_status status;
73152c9ce25SScott Long 	u_int32_t  priority;
732da6808c1SAlexander Motin 	u_int caps;
73365d0fb03SAlexander Motin 	int found = 1;
73452c9ce25SScott Long 
73552c9ce25SScott Long 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
73652c9ce25SScott Long 
73752c9ce25SScott Long 	softc = (probe_softc *)periph->softc;
73852c9ce25SScott Long 	path = done_ccb->ccb_h.path;
73952c9ce25SScott Long 	priority = done_ccb->ccb_h.pinfo.priority;
74052c9ce25SScott Long 	ident_buf = &path->device->ident_data;
74152c9ce25SScott Long 
7421e637ba6SAlexander Motin 	if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
74325a519a9SAlexander Motin 		if (softc->restart) {
74425a519a9SAlexander Motin 			if (bootverbose) {
74525a519a9SAlexander Motin 				cam_error_print(done_ccb,
74625a519a9SAlexander Motin 				    CAM_ESF_ALL, CAM_EPF_ALL);
74725a519a9SAlexander Motin 			}
74825a519a9SAlexander Motin 		} else if (cam_periph_error(done_ccb, 0, 0, NULL) == ERESTART)
7491e637ba6SAlexander Motin 			return;
75025a519a9SAlexander Motin 		if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
7511e637ba6SAlexander Motin 			/* Don't wedge the queue */
7521e637ba6SAlexander Motin 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
7531e637ba6SAlexander Motin 					 /*run_queue*/TRUE);
7541e637ba6SAlexander Motin 		}
75526bdaeddSAlexander Motin 		status = done_ccb->ccb_h.status & CAM_STATUS_MASK;
75625a519a9SAlexander Motin 		if (softc->restart) {
75725a519a9SAlexander Motin 			softc->faults++;
75825a519a9SAlexander Motin 			if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) ==
75925a519a9SAlexander Motin 			    CAM_CMD_TIMEOUT)
76025a519a9SAlexander Motin 				softc->faults += 4;
76125a519a9SAlexander Motin 			if (softc->faults < 10)
76225a519a9SAlexander Motin 				goto done;
76325a519a9SAlexander Motin 			else
76425a519a9SAlexander Motin 				softc->restart = 0;
76526bdaeddSAlexander Motin 
7661e637ba6SAlexander Motin 		/* Old PIO2 devices may not support mode setting. */
76726bdaeddSAlexander Motin 		} else if (softc->action == PROBE_SETMODE &&
76826bdaeddSAlexander Motin 		    status == CAM_ATA_STATUS_ERROR &&
7691e637ba6SAlexander Motin 		    ata_max_pmode(ident_buf) <= ATA_PIO2 &&
77026bdaeddSAlexander Motin 		    (ident_buf->capabilities1 & ATA_SUPPORT_IORDY) == 0) {
7711e637ba6SAlexander Motin 			goto noerror;
77226bdaeddSAlexander Motin 
77326bdaeddSAlexander Motin 		/*
77426bdaeddSAlexander Motin 		 * Some old WD SATA disks report supported and enabled
77526bdaeddSAlexander Motin 		 * device-initiated interface power management, but return
77626bdaeddSAlexander Motin 		 * ABORT on attempt to disable it.
77726bdaeddSAlexander Motin 		 */
77826bdaeddSAlexander Motin 		} else if (softc->action == PROBE_SETPM &&
77926bdaeddSAlexander Motin 		    status == CAM_ATA_STATUS_ERROR) {
78026bdaeddSAlexander Motin 			goto noerror;
78126bdaeddSAlexander Motin 		}
78226bdaeddSAlexander Motin 
7831e637ba6SAlexander Motin 		/*
7841e637ba6SAlexander Motin 		 * If we get to this point, we got an error status back
7851e637ba6SAlexander Motin 		 * from the inquiry and the error status doesn't require
7861e637ba6SAlexander Motin 		 * automatically retrying the command.  Therefore, the
7871e637ba6SAlexander Motin 		 * inquiry failed.  If we had inquiry information before
7881e637ba6SAlexander Motin 		 * for this device, but this latest inquiry command failed,
7891e637ba6SAlexander Motin 		 * the device has probably gone away.  If this device isn't
7901e637ba6SAlexander Motin 		 * already marked unconfigured, notify the peripheral
7911e637ba6SAlexander Motin 		 * drivers that this device is no more.
7921e637ba6SAlexander Motin 		 */
79325a519a9SAlexander Motin device_fail:	if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
7941e637ba6SAlexander Motin 			xpt_async(AC_LOST_DEVICE, path, NULL);
7951e637ba6SAlexander Motin 		found = 0;
7961e637ba6SAlexander Motin 		goto done;
7971e637ba6SAlexander Motin 	}
7981e637ba6SAlexander Motin noerror:
799a9b8edb1SAlexander Motin 	if (softc->restart)
800a9b8edb1SAlexander Motin 		goto done;
80152c9ce25SScott Long 	switch (softc->action) {
80252c9ce25SScott Long 	case PROBE_RESET:
8031e637ba6SAlexander Motin 	{
80452c9ce25SScott Long 		int sign = (done_ccb->ataio.res.lba_high << 8) +
80552c9ce25SScott Long 		    done_ccb->ataio.res.lba_mid;
8067bdb664eSAlexander Motin 		if (bootverbose)
80752c9ce25SScott Long 			xpt_print(path, "SIGNATURE: %04x\n", sign);
80852c9ce25SScott Long 		if (sign == 0x0000 &&
80952c9ce25SScott Long 		    done_ccb->ccb_h.target_id != 15) {
81052c9ce25SScott Long 			path->device->protocol = PROTO_ATA;
81152c9ce25SScott Long 			PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
81252c9ce25SScott Long 		} else if (sign == 0x9669 &&
81352c9ce25SScott Long 		    done_ccb->ccb_h.target_id == 15) {
81452c9ce25SScott Long 			/* Report SIM that PM is present. */
81552c9ce25SScott Long 			bzero(&cts, sizeof(cts));
81683c5d981SAlexander Motin 			xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
81752c9ce25SScott Long 			cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
81852c9ce25SScott Long 			cts.type = CTS_TYPE_CURRENT_SETTINGS;
81952c9ce25SScott Long 			cts.xport_specific.sata.pm_present = 1;
82052c9ce25SScott Long 			cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
82152c9ce25SScott Long 			xpt_action((union ccb *)&cts);
82252c9ce25SScott Long 			path->device->protocol = PROTO_SATAPM;
82352c9ce25SScott Long 			PROBE_SET_ACTION(softc, PROBE_PM_PID);
82452c9ce25SScott Long 		} else if (sign == 0xeb14 &&
82552c9ce25SScott Long 		    done_ccb->ccb_h.target_id != 15) {
82652c9ce25SScott Long 			path->device->protocol = PROTO_SCSI;
82752c9ce25SScott Long 			PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
82852c9ce25SScott Long 		} else {
82952c9ce25SScott Long 			if (done_ccb->ccb_h.target_id != 15) {
83052c9ce25SScott Long 				xpt_print(path,
83152c9ce25SScott Long 				    "Unexpected signature 0x%04x\n", sign);
83252c9ce25SScott Long 			}
83365d0fb03SAlexander Motin 			goto device_fail;
83452c9ce25SScott Long 		}
83552c9ce25SScott Long 		xpt_release_ccb(done_ccb);
83652c9ce25SScott Long 		xpt_schedule(periph, priority);
83752c9ce25SScott Long 		return;
83852c9ce25SScott Long 	}
83952c9ce25SScott Long 	case PROBE_IDENTIFY:
84052c9ce25SScott Long 	{
841699f853bSAlexander Motin 		struct ccb_pathinq cpi;
84252c9ce25SScott Long 		int16_t *ptr;
8432eb4a8dcSAlexander Motin 		int changed = 1;
84452c9ce25SScott Long 
845a9b8edb1SAlexander Motin 		ident_buf = &softc->ident_data;
84652c9ce25SScott Long 		for (ptr = (int16_t *)ident_buf;
84752c9ce25SScott Long 		     ptr < (int16_t *)ident_buf + sizeof(struct ata_params)/2; ptr++) {
84852c9ce25SScott Long 			*ptr = le16toh(*ptr);
84952c9ce25SScott Long 		}
85052c9ce25SScott Long 		if (strncmp(ident_buf->model, "FX", 2) &&
85152c9ce25SScott Long 		    strncmp(ident_buf->model, "NEC", 3) &&
85252c9ce25SScott Long 		    strncmp(ident_buf->model, "Pioneer", 7) &&
85352c9ce25SScott Long 		    strncmp(ident_buf->model, "SHARP", 5)) {
85452c9ce25SScott Long 			ata_bswap(ident_buf->model, sizeof(ident_buf->model));
85552c9ce25SScott Long 			ata_bswap(ident_buf->revision, sizeof(ident_buf->revision));
85652c9ce25SScott Long 			ata_bswap(ident_buf->serial, sizeof(ident_buf->serial));
85752c9ce25SScott Long 		}
85852c9ce25SScott Long 		ata_btrim(ident_buf->model, sizeof(ident_buf->model));
85952c9ce25SScott Long 		ata_bpack(ident_buf->model, ident_buf->model, sizeof(ident_buf->model));
86052c9ce25SScott Long 		ata_btrim(ident_buf->revision, sizeof(ident_buf->revision));
86152c9ce25SScott Long 		ata_bpack(ident_buf->revision, ident_buf->revision, sizeof(ident_buf->revision));
86252c9ce25SScott Long 		ata_btrim(ident_buf->serial, sizeof(ident_buf->serial));
86352c9ce25SScott Long 		ata_bpack(ident_buf->serial, ident_buf->serial, sizeof(ident_buf->serial));
8644ef08dc5SAlexander Motin 		/* Device may need spin-up before IDENTIFY become valid. */
8651254b680SAlexander Motin 		if ((ident_buf->specconf == 0x37c8 ||
8661254b680SAlexander Motin 		     ident_buf->specconf == 0x738c) &&
8671254b680SAlexander Motin 		    ((ident_buf->config & ATA_RESP_INCOMPLETE) ||
8684ef08dc5SAlexander Motin 		     softc->spinup == 0)) {
8694ef08dc5SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_SPINUP);
8704ef08dc5SAlexander Motin 			xpt_release_ccb(done_ccb);
8714ef08dc5SAlexander Motin 			xpt_schedule(periph, priority);
8724ef08dc5SAlexander Motin 			return;
8734ef08dc5SAlexander Motin 		}
874a9b8edb1SAlexander Motin 		ident_buf = &path->device->ident_data;
87552c9ce25SScott Long 		if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
87652c9ce25SScott Long 			/* Check that it is the same device. */
877a9b8edb1SAlexander Motin 			if (bcmp(softc->ident_data.model, ident_buf->model,
878a9b8edb1SAlexander Motin 			     sizeof(ident_buf->model)) ||
879a9b8edb1SAlexander Motin 			    bcmp(softc->ident_data.revision, ident_buf->revision,
880a9b8edb1SAlexander Motin 			     sizeof(ident_buf->revision)) ||
881a9b8edb1SAlexander Motin 			    bcmp(softc->ident_data.serial, ident_buf->serial,
882a9b8edb1SAlexander Motin 			     sizeof(ident_buf->serial))) {
88352c9ce25SScott Long 				/* Device changed. */
88452c9ce25SScott Long 				xpt_async(AC_LOST_DEVICE, path, NULL);
8851e637ba6SAlexander Motin 			} else {
886a9b8edb1SAlexander Motin 				bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
8872eb4a8dcSAlexander Motin 				changed = 0;
8882eb4a8dcSAlexander Motin 			}
8892eb4a8dcSAlexander Motin 		}
8902eb4a8dcSAlexander Motin 		if (changed) {
8912eb4a8dcSAlexander Motin 			bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
89252c9ce25SScott Long 			/* Clean up from previous instance of this device */
89352c9ce25SScott Long 			if (path->device->serial_num != NULL) {
89452c9ce25SScott Long 				free(path->device->serial_num, M_CAMXPT);
89552c9ce25SScott Long 				path->device->serial_num = NULL;
89652c9ce25SScott Long 				path->device->serial_num_len = 0;
89752c9ce25SScott Long 			}
89852c9ce25SScott Long 			path->device->serial_num =
89952c9ce25SScott Long 				(u_int8_t *)malloc((sizeof(ident_buf->serial) + 1),
90052c9ce25SScott Long 					   M_CAMXPT, M_NOWAIT);
90152c9ce25SScott Long 			if (path->device->serial_num != NULL) {
90252c9ce25SScott Long 				bcopy(ident_buf->serial,
90352c9ce25SScott Long 				      path->device->serial_num,
90452c9ce25SScott Long 				      sizeof(ident_buf->serial));
90552c9ce25SScott Long 				path->device->serial_num[sizeof(ident_buf->serial)]
90652c9ce25SScott Long 				    = '\0';
90752c9ce25SScott Long 				path->device->serial_num_len =
90852c9ce25SScott Long 				    strlen(path->device->serial_num);
90952c9ce25SScott Long 			}
91052c9ce25SScott Long 
9114b997c49SAlexander Motin 			path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
9121e637ba6SAlexander Motin 		}
91330a4094fSAlexander Motin 		if (ident_buf->satacapabilities & ATA_SUPPORT_NCQ) {
91430a4094fSAlexander Motin 			path->device->mintags = path->device->maxtags =
91530a4094fSAlexander Motin 			    ATA_QUEUE_LEN(ident_buf->queue) + 1;
91630a4094fSAlexander Motin 		}
91730a4094fSAlexander Motin 		ata_find_quirk(path->device);
918507e5811SAlexander Motin 		if (path->device->mintags != 0 &&
919507e5811SAlexander Motin 		    path->bus->sim->max_tagged_dev_openings != 0) {
920699f853bSAlexander Motin 			/* Check if the SIM does not want queued commands. */
921699f853bSAlexander Motin 			bzero(&cpi, sizeof(cpi));
922699f853bSAlexander Motin 			xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
923699f853bSAlexander Motin 			cpi.ccb_h.func_code = XPT_PATH_INQ;
924699f853bSAlexander Motin 			xpt_action((union ccb *)&cpi);
925699f853bSAlexander Motin 			if (cpi.ccb_h.status == CAM_REQ_CMP &&
926699f853bSAlexander Motin 			    (cpi.hba_inquiry & PI_TAG_ABLE)) {
927c8039fc6SAlexander Motin 				/* Report SIM which tags are allowed. */
928c8039fc6SAlexander Motin 				bzero(&cts, sizeof(cts));
92983c5d981SAlexander Motin 				xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
930c8039fc6SAlexander Motin 				cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
931c8039fc6SAlexander Motin 				cts.type = CTS_TYPE_CURRENT_SETTINGS;
932c8039fc6SAlexander Motin 				cts.xport_specific.sata.tags = path->device->maxtags;
933c8039fc6SAlexander Motin 				cts.xport_specific.sata.valid = CTS_SATA_VALID_TAGS;
934c8039fc6SAlexander Motin 				xpt_action((union ccb *)&cts);
935c8039fc6SAlexander Motin 				/* Reconfigure queues for tagged queueing. */
93630a4094fSAlexander Motin 				xpt_start_tags(path);
93730a4094fSAlexander Motin 			}
938699f853bSAlexander Motin 		}
93952c9ce25SScott Long 		ata_device_transport(path);
94052c9ce25SScott Long 		PROBE_SET_ACTION(softc, PROBE_SETMODE);
94152c9ce25SScott Long 		xpt_release_ccb(done_ccb);
94252c9ce25SScott Long 		xpt_schedule(periph, priority);
94352c9ce25SScott Long 		return;
94452c9ce25SScott Long 	}
9454ef08dc5SAlexander Motin 	case PROBE_SPINUP:
9464ef08dc5SAlexander Motin 		if (bootverbose)
9474ef08dc5SAlexander Motin 			xpt_print(path, "Spin-up done\n");
9484ef08dc5SAlexander Motin 		softc->spinup = 1;
9494ef08dc5SAlexander Motin 		PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
9504ef08dc5SAlexander Motin 		xpt_release_ccb(done_ccb);
9514ef08dc5SAlexander Motin 		xpt_schedule(periph, priority);
9524ef08dc5SAlexander Motin 		return;
95352c9ce25SScott Long 	case PROBE_SETMODE:
954da6808c1SAlexander Motin 		if (path->device->transport != XPORT_SATA)
955da6808c1SAlexander Motin 			goto notsata;
956da6808c1SAlexander Motin 		/* Set supported bits. */
957da6808c1SAlexander Motin 		bzero(&cts, sizeof(cts));
958da6808c1SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
959da6808c1SAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
960da6808c1SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
961da6808c1SAlexander Motin 		xpt_action((union ccb *)&cts);
962da6808c1SAlexander Motin 		if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
963da6808c1SAlexander Motin 			caps = cts.xport_specific.sata.caps & CTS_SATA_CAPS_H;
964da6808c1SAlexander Motin 		else
965da6808c1SAlexander Motin 			caps = 0;
966da6808c1SAlexander Motin 		if (ident_buf->satacapabilities != 0xffff) {
967da6808c1SAlexander Motin 			if (ident_buf->satacapabilities & ATA_SUPPORT_IFPWRMNGTRCV)
968da6808c1SAlexander Motin 				caps |= CTS_SATA_CAPS_D_PMREQ;
969da6808c1SAlexander Motin 			if (ident_buf->satacapabilities & ATA_SUPPORT_HAPST)
970da6808c1SAlexander Motin 				caps |= CTS_SATA_CAPS_D_APST;
971da6808c1SAlexander Motin 		}
972da6808c1SAlexander Motin 		/* Mask unwanted bits. */
973da6808c1SAlexander Motin 		bzero(&cts, sizeof(cts));
974da6808c1SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
975da6808c1SAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
976da6808c1SAlexander Motin 		cts.type = CTS_TYPE_USER_SETTINGS;
977da6808c1SAlexander Motin 		xpt_action((union ccb *)&cts);
978da6808c1SAlexander Motin 		if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
979da6808c1SAlexander Motin 			caps &= cts.xport_specific.sata.caps;
980c2b82f3eSAlexander Motin 		else
981c2b82f3eSAlexander Motin 			caps = 0;
982da6808c1SAlexander Motin 		/* Store result to SIM. */
983da6808c1SAlexander Motin 		bzero(&cts, sizeof(cts));
984da6808c1SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
985da6808c1SAlexander Motin 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
986da6808c1SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
987da6808c1SAlexander Motin 		cts.xport_specific.sata.caps = caps;
988da6808c1SAlexander Motin 		cts.xport_specific.sata.valid = CTS_SATA_VALID_CAPS;
989da6808c1SAlexander Motin 		xpt_action((union ccb *)&cts);
990da6808c1SAlexander Motin 		softc->caps = caps;
991da6808c1SAlexander Motin 		if (ident_buf->satasupport & ATA_SUPPORT_IFPWRMNGT) {
992da6808c1SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_SETPM);
993da6808c1SAlexander Motin 			xpt_release_ccb(done_ccb);
994da6808c1SAlexander Motin 			xpt_schedule(periph, priority);
995da6808c1SAlexander Motin 			return;
996da6808c1SAlexander Motin 		}
997da6808c1SAlexander Motin 		/* FALLTHROUGH */
998da6808c1SAlexander Motin 	case PROBE_SETPM:
999da6808c1SAlexander Motin 		if (ident_buf->satacapabilities != 0xffff &&
1000da6808c1SAlexander Motin 		    ident_buf->satacapabilities & ATA_SUPPORT_DAPST) {
1001da6808c1SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_SETAPST);
1002da6808c1SAlexander Motin 			xpt_release_ccb(done_ccb);
1003da6808c1SAlexander Motin 			xpt_schedule(periph, priority);
1004da6808c1SAlexander Motin 			return;
1005da6808c1SAlexander Motin 		}
1006da6808c1SAlexander Motin 		/* FALLTHROUGH */
1007da6808c1SAlexander Motin 	case PROBE_SETAPST:
1008da6808c1SAlexander Motin 		if (ident_buf->satasupport & ATA_SUPPORT_AUTOACTIVATE) {
1009da6808c1SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_SETDMAAA);
1010da6808c1SAlexander Motin 			xpt_release_ccb(done_ccb);
1011da6808c1SAlexander Motin 			xpt_schedule(periph, priority);
1012da6808c1SAlexander Motin 			return;
1013da6808c1SAlexander Motin 		}
1014da6808c1SAlexander Motin 		/* FALLTHROUGH */
1015da6808c1SAlexander Motin 	case PROBE_SETDMAAA:
1016da6808c1SAlexander Motin notsata:
10171e637ba6SAlexander Motin 		if (path->device->protocol == PROTO_ATA) {
10181e637ba6SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_SET_MULTI);
10191e637ba6SAlexander Motin 		} else {
10201e637ba6SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_INQUIRY);
10211e637ba6SAlexander Motin 		}
10221e637ba6SAlexander Motin 		xpt_release_ccb(done_ccb);
10231e637ba6SAlexander Motin 		xpt_schedule(periph, priority);
10241e637ba6SAlexander Motin 		return;
10251e637ba6SAlexander Motin 	case PROBE_SET_MULTI:
10261e637ba6SAlexander Motin 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
102752c9ce25SScott Long 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1028f98d7a47SAlexander Motin 			xpt_acquire_device(path->device);
102952c9ce25SScott Long 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
103052c9ce25SScott Long 			xpt_action(done_ccb);
103152c9ce25SScott Long 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
103252c9ce25SScott Long 			    done_ccb);
10331e637ba6SAlexander Motin 		}
103452c9ce25SScott Long 		break;
103552c9ce25SScott Long 	case PROBE_INQUIRY:
103652c9ce25SScott Long 	case PROBE_FULL_INQUIRY:
103752c9ce25SScott Long 	{
103852c9ce25SScott Long 		struct scsi_inquiry_data *inq_buf;
10391e637ba6SAlexander Motin 		u_int8_t periph_qual, len;
104052c9ce25SScott Long 
104152c9ce25SScott Long 		path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
104252c9ce25SScott Long 		inq_buf = &path->device->inq_data;
104352c9ce25SScott Long 
104452c9ce25SScott Long 		periph_qual = SID_QUAL(inq_buf);
104552c9ce25SScott Long 
10461e637ba6SAlexander Motin 		if (periph_qual != SID_QUAL_LU_CONNECTED)
10471e637ba6SAlexander Motin 			break;
104852c9ce25SScott Long 
104952c9ce25SScott Long 		/*
105052c9ce25SScott Long 		 * We conservatively request only
105152c9ce25SScott Long 		 * SHORT_INQUIRY_LEN bytes of inquiry
105252c9ce25SScott Long 		 * information during our first try
105352c9ce25SScott Long 		 * at sending an INQUIRY. If the device
105452c9ce25SScott Long 		 * has more information to give,
105552c9ce25SScott Long 		 * perform a second request specifying
105652c9ce25SScott Long 		 * the amount of information the device
105752c9ce25SScott Long 		 * is willing to give.
105852c9ce25SScott Long 		 */
105952c9ce25SScott Long 		len = inq_buf->additional_length
10601e637ba6SAlexander Motin 		    + offsetof(struct scsi_inquiry_data, additional_length) + 1;
106152c9ce25SScott Long 		if (softc->action == PROBE_INQUIRY
106252c9ce25SScott Long 		    && len > SHORT_INQUIRY_LENGTH) {
106352c9ce25SScott Long 			PROBE_SET_ACTION(softc, PROBE_FULL_INQUIRY);
106452c9ce25SScott Long 			xpt_release_ccb(done_ccb);
106552c9ce25SScott Long 			xpt_schedule(periph, priority);
106652c9ce25SScott Long 			return;
106752c9ce25SScott Long 		}
106852c9ce25SScott Long 
10694b997c49SAlexander Motin 		ata_device_transport(path);
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);
10751e637ba6SAlexander Motin 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path, done_ccb);
10761e637ba6SAlexander Motin 		}
107752c9ce25SScott Long 		break;
107852c9ce25SScott Long 	}
107952c9ce25SScott Long 	case PROBE_PM_PID:
10804b997c49SAlexander Motin 		if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) == 0)
108152c9ce25SScott Long 			bzero(ident_buf, sizeof(*ident_buf));
108252c9ce25SScott Long 		softc->pm_pid = (done_ccb->ataio.res.lba_high << 24) +
108352c9ce25SScott Long 		    (done_ccb->ataio.res.lba_mid << 16) +
108452c9ce25SScott Long 		    (done_ccb->ataio.res.lba_low << 8) +
108552c9ce25SScott Long 		    done_ccb->ataio.res.sector_count;
108665d0fb03SAlexander Motin 		((uint32_t *)ident_buf)[0] = softc->pm_pid;
108752c9ce25SScott Long 		snprintf(ident_buf->model, sizeof(ident_buf->model),
108852c9ce25SScott Long 		    "Port Multiplier %08x", softc->pm_pid);
108952c9ce25SScott Long 		PROBE_SET_ACTION(softc, PROBE_PM_PRV);
109052c9ce25SScott Long 		xpt_release_ccb(done_ccb);
109152c9ce25SScott Long 		xpt_schedule(periph, priority);
109252c9ce25SScott Long 		return;
109352c9ce25SScott Long 	case PROBE_PM_PRV:
109452c9ce25SScott Long 		softc->pm_prv = (done_ccb->ataio.res.lba_high << 24) +
109552c9ce25SScott Long 		    (done_ccb->ataio.res.lba_mid << 16) +
109652c9ce25SScott Long 		    (done_ccb->ataio.res.lba_low << 8) +
109752c9ce25SScott Long 		    done_ccb->ataio.res.sector_count;
109865d0fb03SAlexander Motin 		((uint32_t *)ident_buf)[1] = softc->pm_prv;
109952c9ce25SScott Long 		snprintf(ident_buf->revision, sizeof(ident_buf->revision),
110052c9ce25SScott Long 		    "%04x", softc->pm_prv);
11014b997c49SAlexander Motin 		path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
1102da6808c1SAlexander Motin 		/* Set supported bits. */
1103da6808c1SAlexander Motin 		bzero(&cts, sizeof(cts));
1104da6808c1SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1105da6808c1SAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1106da6808c1SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
1107da6808c1SAlexander Motin 		xpt_action((union ccb *)&cts);
1108da6808c1SAlexander Motin 		if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1109da6808c1SAlexander Motin 			caps = cts.xport_specific.sata.caps & CTS_SATA_CAPS_H;
1110da6808c1SAlexander Motin 		else
1111da6808c1SAlexander Motin 			caps = 0;
1112da6808c1SAlexander Motin 		/* All PMPs must support PM requests. */
1113da6808c1SAlexander Motin 		caps |= CTS_SATA_CAPS_D_PMREQ;
1114da6808c1SAlexander Motin 		/* Mask unwanted bits. */
1115da6808c1SAlexander Motin 		bzero(&cts, sizeof(cts));
1116da6808c1SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1117da6808c1SAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1118da6808c1SAlexander Motin 		cts.type = CTS_TYPE_USER_SETTINGS;
1119da6808c1SAlexander Motin 		xpt_action((union ccb *)&cts);
1120da6808c1SAlexander Motin 		if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1121da6808c1SAlexander Motin 			caps &= cts.xport_specific.sata.caps;
1122c2b82f3eSAlexander Motin 		else
1123c2b82f3eSAlexander Motin 			caps = 0;
1124da6808c1SAlexander Motin 		/* Store result to SIM. */
1125da6808c1SAlexander Motin 		bzero(&cts, sizeof(cts));
1126da6808c1SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1127da6808c1SAlexander Motin 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1128da6808c1SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
1129da6808c1SAlexander Motin 		cts.xport_specific.sata.caps = caps;
1130da6808c1SAlexander Motin 		cts.xport_specific.sata.valid = CTS_SATA_VALID_CAPS;
1131da6808c1SAlexander Motin 		xpt_action((union ccb *)&cts);
1132da6808c1SAlexander Motin 		softc->caps = caps;
113365d0fb03SAlexander Motin 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
113452c9ce25SScott Long 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1135f98d7a47SAlexander Motin 			xpt_acquire_device(path->device);
113652c9ce25SScott Long 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
113752c9ce25SScott Long 			xpt_action(done_ccb);
113852c9ce25SScott Long 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
113952c9ce25SScott Long 			    done_ccb);
114065d0fb03SAlexander Motin 		} else {
114165d0fb03SAlexander Motin 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
114265d0fb03SAlexander Motin 			xpt_action(done_ccb);
11431e637ba6SAlexander Motin 			xpt_async(AC_SCSI_AEN, done_ccb->ccb_h.path, done_ccb);
114452c9ce25SScott Long 		}
114552c9ce25SScott Long 		break;
114652c9ce25SScott Long 	case PROBE_INVALID:
114752c9ce25SScott Long 		CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_INFO,
114852c9ce25SScott Long 		    ("probedone: invalid action state\n"));
114952c9ce25SScott Long 	default:
115052c9ce25SScott Long 		break;
115152c9ce25SScott Long 	}
11521e637ba6SAlexander Motin done:
115383c5d981SAlexander Motin 	if (softc->restart) {
115483c5d981SAlexander Motin 		softc->restart = 0;
11551e637ba6SAlexander Motin 		xpt_release_ccb(done_ccb);
115683c5d981SAlexander Motin 		probeschedule(periph);
115783c5d981SAlexander Motin 		return;
115883c5d981SAlexander Motin 	}
115983c5d981SAlexander Motin 	xpt_release_ccb(done_ccb);
116083c5d981SAlexander Motin 	while ((done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs))) {
116183c5d981SAlexander Motin 		TAILQ_REMOVE(&softc->request_ccbs,
116283c5d981SAlexander Motin 		    &done_ccb->ccb_h, periph_links.tqe);
116383c5d981SAlexander Motin 		done_ccb->ccb_h.status = found ? CAM_REQ_CMP : CAM_REQ_CMP_ERR;
116452c9ce25SScott Long 		xpt_done(done_ccb);
116583c5d981SAlexander Motin 	}
116683c5d981SAlexander Motin 	cam_release_devq(periph->path,
116783c5d981SAlexander Motin 	    RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_XPT + 1, FALSE);
116852c9ce25SScott Long 	cam_periph_invalidate(periph);
116952c9ce25SScott Long 	cam_periph_release_locked(periph);
117052c9ce25SScott Long }
117152c9ce25SScott Long 
117252c9ce25SScott Long static void
117352c9ce25SScott Long probecleanup(struct cam_periph *periph)
117452c9ce25SScott Long {
117552c9ce25SScott Long 	free(periph->softc, M_CAMXPT);
117652c9ce25SScott Long }
117752c9ce25SScott Long 
117852c9ce25SScott Long static void
117930a4094fSAlexander Motin ata_find_quirk(struct cam_ed *device)
118052c9ce25SScott Long {
118130a4094fSAlexander Motin 	struct ata_quirk_entry *quirk;
118252c9ce25SScott Long 	caddr_t	match;
118352c9ce25SScott Long 
118430a4094fSAlexander Motin 	match = cam_quirkmatch((caddr_t)&device->ident_data,
118530a4094fSAlexander Motin 			       (caddr_t)ata_quirk_table,
118630a4094fSAlexander Motin 			       ata_quirk_table_size,
118730a4094fSAlexander Motin 			       sizeof(*ata_quirk_table), ata_identify_match);
118852c9ce25SScott Long 
118952c9ce25SScott Long 	if (match == NULL)
119052c9ce25SScott Long 		panic("xpt_find_quirk: device didn't match wildcard entry!!");
119152c9ce25SScott Long 
119230a4094fSAlexander Motin 	quirk = (struct ata_quirk_entry *)match;
119352c9ce25SScott Long 	device->quirk = quirk;
119430a4094fSAlexander Motin 	if (quirk->quirks & CAM_QUIRK_MAXTAGS)
119530a4094fSAlexander Motin 		device->mintags = device->maxtags = quirk->maxtags;
119652c9ce25SScott Long }
119752c9ce25SScott Long 
119852c9ce25SScott Long typedef struct {
119952c9ce25SScott Long 	union	ccb *request_ccb;
120052c9ce25SScott Long 	struct 	ccb_pathinq *cpi;
120152c9ce25SScott Long 	int	counter;
120252c9ce25SScott Long } ata_scan_bus_info;
120352c9ce25SScott Long 
120452c9ce25SScott Long /*
120552c9ce25SScott Long  * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
120652c9ce25SScott Long  * As the scan progresses, xpt_scan_bus is used as the
120752c9ce25SScott Long  * callback on completion function.
120852c9ce25SScott Long  */
120952c9ce25SScott Long static void
121052c9ce25SScott Long ata_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
121152c9ce25SScott Long {
121252c9ce25SScott Long 	struct	cam_path *path;
121352c9ce25SScott Long 	ata_scan_bus_info *scan_info;
121483c5d981SAlexander Motin 	union	ccb *work_ccb, *reset_ccb;
121552c9ce25SScott Long 	cam_status status;
121652c9ce25SScott Long 
121752c9ce25SScott Long 	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
121852c9ce25SScott Long 		  ("xpt_scan_bus\n"));
121952c9ce25SScott Long 	switch (request_ccb->ccb_h.func_code) {
122052c9ce25SScott Long 	case XPT_SCAN_BUS:
12210e85f214SMatt Jacob 	case XPT_SCAN_TGT:
122252c9ce25SScott Long 		/* Find out the characteristics of the bus */
122352c9ce25SScott Long 		work_ccb = xpt_alloc_ccb_nowait();
122452c9ce25SScott Long 		if (work_ccb == NULL) {
122552c9ce25SScott Long 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
122652c9ce25SScott Long 			xpt_done(request_ccb);
122752c9ce25SScott Long 			return;
122852c9ce25SScott Long 		}
122952c9ce25SScott Long 		xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
123052c9ce25SScott Long 			      request_ccb->ccb_h.pinfo.priority);
123152c9ce25SScott Long 		work_ccb->ccb_h.func_code = XPT_PATH_INQ;
123252c9ce25SScott Long 		xpt_action(work_ccb);
123352c9ce25SScott Long 		if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
123452c9ce25SScott Long 			request_ccb->ccb_h.status = work_ccb->ccb_h.status;
123552c9ce25SScott Long 			xpt_free_ccb(work_ccb);
123652c9ce25SScott Long 			xpt_done(request_ccb);
123752c9ce25SScott Long 			return;
123852c9ce25SScott Long 		}
123952c9ce25SScott Long 
124083c5d981SAlexander Motin 		/* We may need to reset bus first, if we haven't done it yet. */
124183c5d981SAlexander Motin 		if ((work_ccb->cpi.hba_inquiry &
124283c5d981SAlexander Motin 		    (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) &&
124383c5d981SAlexander Motin 		    !(work_ccb->cpi.hba_misc & PIM_NOBUSRESET) &&
124483c5d981SAlexander Motin 		    !timevalisset(&request_ccb->ccb_h.path->bus->last_reset)) {
124583c5d981SAlexander Motin 			reset_ccb = xpt_alloc_ccb_nowait();
1246f1893540SAlexander Motin 			if (reset_ccb == NULL) {
1247f1893540SAlexander Motin 				request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1248f1893540SAlexander Motin 				xpt_free_ccb(work_ccb);
1249f1893540SAlexander Motin 				xpt_done(request_ccb);
1250f1893540SAlexander Motin 				return;
1251f1893540SAlexander Motin 			}
125283c5d981SAlexander Motin 			xpt_setup_ccb(&reset_ccb->ccb_h, request_ccb->ccb_h.path,
125383c5d981SAlexander Motin 			      CAM_PRIORITY_NONE);
125483c5d981SAlexander Motin 			reset_ccb->ccb_h.func_code = XPT_RESET_BUS;
125583c5d981SAlexander Motin 			xpt_action(reset_ccb);
125683c5d981SAlexander Motin 			if (reset_ccb->ccb_h.status != CAM_REQ_CMP) {
125783c5d981SAlexander Motin 				request_ccb->ccb_h.status = reset_ccb->ccb_h.status;
125883c5d981SAlexander Motin 				xpt_free_ccb(reset_ccb);
125983c5d981SAlexander Motin 				xpt_free_ccb(work_ccb);
126083c5d981SAlexander Motin 				xpt_done(request_ccb);
126183c5d981SAlexander Motin 				return;
126283c5d981SAlexander Motin 			}
126383c5d981SAlexander Motin 			xpt_free_ccb(reset_ccb);
126483c5d981SAlexander Motin 		}
126583c5d981SAlexander Motin 
126652c9ce25SScott Long 		/* Save some state for use while we probe for devices */
126752c9ce25SScott Long 		scan_info = (ata_scan_bus_info *)
126852c9ce25SScott Long 		    malloc(sizeof(ata_scan_bus_info), M_CAMXPT, M_NOWAIT);
126952c9ce25SScott Long 		if (scan_info == NULL) {
127052c9ce25SScott Long 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1271f1893540SAlexander Motin 			xpt_free_ccb(work_ccb);
127252c9ce25SScott Long 			xpt_done(request_ccb);
127352c9ce25SScott Long 			return;
127452c9ce25SScott Long 		}
127552c9ce25SScott Long 		scan_info->request_ccb = request_ccb;
127652c9ce25SScott Long 		scan_info->cpi = &work_ccb->cpi;
127752c9ce25SScott Long 		/* If PM supported, probe it first. */
127852c9ce25SScott Long 		if (scan_info->cpi->hba_inquiry & PI_SATAPM)
12790025eb12SAlexander Motin 			scan_info->counter = scan_info->cpi->max_target;
12800025eb12SAlexander Motin 		else
12810025eb12SAlexander Motin 			scan_info->counter = 0;
128252c9ce25SScott Long 
128352c9ce25SScott Long 		work_ccb = xpt_alloc_ccb_nowait();
128452c9ce25SScott Long 		if (work_ccb == NULL) {
128552c9ce25SScott Long 			free(scan_info, M_CAMXPT);
128652c9ce25SScott Long 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
128752c9ce25SScott Long 			xpt_done(request_ccb);
128852c9ce25SScott Long 			break;
128952c9ce25SScott Long 		}
129052c9ce25SScott Long 		goto scan_next;
129152c9ce25SScott Long 	case XPT_SCAN_LUN:
129252c9ce25SScott Long 		work_ccb = request_ccb;
129352c9ce25SScott Long 		/* Reuse the same CCB to query if a device was really found */
129452c9ce25SScott Long 		scan_info = (ata_scan_bus_info *)work_ccb->ccb_h.ppriv_ptr0;
129565d0fb03SAlexander Motin 		/* If there is PMP... */
12960025eb12SAlexander Motin 		if ((scan_info->cpi->hba_inquiry & PI_SATAPM) &&
12970025eb12SAlexander Motin 		    (scan_info->counter == scan_info->cpi->max_target)) {
129883c5d981SAlexander Motin 			if (work_ccb->ccb_h.status == CAM_REQ_CMP) {
129965d0fb03SAlexander Motin 				/* everything else will be probed by it */
1300e2a75189SAlexander Motin 				/* Free the current request path- we're done with it. */
1301e2a75189SAlexander Motin 				xpt_free_path(work_ccb->ccb_h.path);
13020025eb12SAlexander Motin 				goto done;
130352c9ce25SScott Long 			} else {
130452c9ce25SScott Long 				struct ccb_trans_settings cts;
130552c9ce25SScott Long 
130652c9ce25SScott Long 				/* Report SIM that PM is absent. */
130752c9ce25SScott Long 				bzero(&cts, sizeof(cts));
130852c9ce25SScott Long 				xpt_setup_ccb(&cts.ccb_h,
1309e2a75189SAlexander Motin 				    work_ccb->ccb_h.path, CAM_PRIORITY_NONE);
131052c9ce25SScott Long 				cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
131152c9ce25SScott Long 				cts.type = CTS_TYPE_CURRENT_SETTINGS;
13123ccda2f3SAlexander Motin 				cts.xport_specific.sata.pm_present = 0;
131352c9ce25SScott Long 				cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
131452c9ce25SScott Long 				xpt_action((union ccb *)&cts);
131552c9ce25SScott Long 			}
131652c9ce25SScott Long 		}
1317e2a75189SAlexander Motin 		/* Free the current request path- we're done with it. */
1318e2a75189SAlexander Motin 		xpt_free_path(work_ccb->ccb_h.path);
13190025eb12SAlexander Motin 		if (scan_info->counter ==
13200025eb12SAlexander Motin 		    ((scan_info->cpi->hba_inquiry & PI_SATAPM) ?
13210025eb12SAlexander Motin 		    0 : scan_info->cpi->max_target)) {
13220025eb12SAlexander Motin done:
132352c9ce25SScott Long 			xpt_free_ccb(work_ccb);
132452c9ce25SScott Long 			xpt_free_ccb((union ccb *)scan_info->cpi);
132552c9ce25SScott Long 			request_ccb = scan_info->request_ccb;
132652c9ce25SScott Long 			free(scan_info, M_CAMXPT);
132752c9ce25SScott Long 			request_ccb->ccb_h.status = CAM_REQ_CMP;
132852c9ce25SScott Long 			xpt_done(request_ccb);
132952c9ce25SScott Long 			break;
133052c9ce25SScott Long 		}
13310025eb12SAlexander Motin 		/* Take next device. Wrap from max (PMP) to 0. */
13320025eb12SAlexander Motin 		scan_info->counter = (scan_info->counter + 1 ) %
13330025eb12SAlexander Motin 		    (scan_info->cpi->max_target + 1);
133452c9ce25SScott Long scan_next:
133552c9ce25SScott Long 		status = xpt_create_path(&path, xpt_periph,
133652c9ce25SScott Long 		    scan_info->request_ccb->ccb_h.path_id,
133752c9ce25SScott Long 		    scan_info->counter, 0);
133852c9ce25SScott Long 		if (status != CAM_REQ_CMP) {
133952c9ce25SScott Long 			printf("xpt_scan_bus: xpt_create_path failed"
134052c9ce25SScott Long 			    " with status %#x, bus scan halted\n",
134152c9ce25SScott Long 			    status);
134252c9ce25SScott Long 			xpt_free_ccb(work_ccb);
134352c9ce25SScott Long 			xpt_free_ccb((union ccb *)scan_info->cpi);
134452c9ce25SScott Long 			request_ccb = scan_info->request_ccb;
134552c9ce25SScott Long 			free(scan_info, M_CAMXPT);
134652c9ce25SScott Long 			request_ccb->ccb_h.status = status;
134752c9ce25SScott Long 			xpt_done(request_ccb);
134852c9ce25SScott Long 			break;
134952c9ce25SScott Long 		}
135052c9ce25SScott Long 		xpt_setup_ccb(&work_ccb->ccb_h, path,
135152c9ce25SScott Long 		    scan_info->request_ccb->ccb_h.pinfo.priority);
135252c9ce25SScott Long 		work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
135352c9ce25SScott Long 		work_ccb->ccb_h.cbfcnp = ata_scan_bus;
135452c9ce25SScott Long 		work_ccb->ccb_h.ppriv_ptr0 = scan_info;
135552c9ce25SScott Long 		work_ccb->crcn.flags = scan_info->request_ccb->crcn.flags;
135652c9ce25SScott Long 		xpt_action(work_ccb);
135752c9ce25SScott Long 		break;
135852c9ce25SScott Long 	default:
135952c9ce25SScott Long 		break;
136052c9ce25SScott Long 	}
136152c9ce25SScott Long }
136252c9ce25SScott Long 
136352c9ce25SScott Long static void
136452c9ce25SScott Long ata_scan_lun(struct cam_periph *periph, struct cam_path *path,
136552c9ce25SScott Long 	     cam_flags flags, union ccb *request_ccb)
136652c9ce25SScott Long {
136752c9ce25SScott Long 	struct ccb_pathinq cpi;
136852c9ce25SScott Long 	cam_status status;
136952c9ce25SScott Long 	struct cam_path *new_path;
137052c9ce25SScott Long 	struct cam_periph *old_periph;
137152c9ce25SScott Long 
137283c5d981SAlexander Motin 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_scan_lun\n"));
137352c9ce25SScott Long 
137483c5d981SAlexander Motin 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
137552c9ce25SScott Long 	cpi.ccb_h.func_code = XPT_PATH_INQ;
137652c9ce25SScott Long 	xpt_action((union ccb *)&cpi);
137752c9ce25SScott Long 
137852c9ce25SScott Long 	if (cpi.ccb_h.status != CAM_REQ_CMP) {
137952c9ce25SScott Long 		if (request_ccb != NULL) {
138052c9ce25SScott Long 			request_ccb->ccb_h.status = cpi.ccb_h.status;
138152c9ce25SScott Long 			xpt_done(request_ccb);
138252c9ce25SScott Long 		}
138352c9ce25SScott Long 		return;
138452c9ce25SScott Long 	}
138552c9ce25SScott Long 
138652c9ce25SScott Long 	if (request_ccb == NULL) {
138752c9ce25SScott Long 		request_ccb = malloc(sizeof(union ccb), M_CAMXPT, M_NOWAIT);
138852c9ce25SScott Long 		if (request_ccb == NULL) {
138952c9ce25SScott Long 			xpt_print(path, "xpt_scan_lun: can't allocate CCB, "
139052c9ce25SScott Long 			    "can't continue\n");
139152c9ce25SScott Long 			return;
139252c9ce25SScott Long 		}
139352c9ce25SScott Long 		new_path = malloc(sizeof(*new_path), M_CAMXPT, M_NOWAIT);
139452c9ce25SScott Long 		if (new_path == NULL) {
139552c9ce25SScott Long 			xpt_print(path, "xpt_scan_lun: can't allocate path, "
139652c9ce25SScott Long 			    "can't continue\n");
139752c9ce25SScott Long 			free(request_ccb, M_CAMXPT);
139852c9ce25SScott Long 			return;
139952c9ce25SScott Long 		}
140052c9ce25SScott Long 		status = xpt_compile_path(new_path, xpt_periph,
140152c9ce25SScott Long 					  path->bus->path_id,
140252c9ce25SScott Long 					  path->target->target_id,
140352c9ce25SScott Long 					  path->device->lun_id);
140452c9ce25SScott Long 
140552c9ce25SScott Long 		if (status != CAM_REQ_CMP) {
140652c9ce25SScott Long 			xpt_print(path, "xpt_scan_lun: can't compile path, "
140752c9ce25SScott Long 			    "can't continue\n");
140852c9ce25SScott Long 			free(request_ccb, M_CAMXPT);
140952c9ce25SScott Long 			free(new_path, M_CAMXPT);
141052c9ce25SScott Long 			return;
141152c9ce25SScott Long 		}
141283c5d981SAlexander Motin 		xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_XPT);
141352c9ce25SScott Long 		request_ccb->ccb_h.cbfcnp = xptscandone;
141452c9ce25SScott Long 		request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
141552c9ce25SScott Long 		request_ccb->crcn.flags = flags;
141652c9ce25SScott Long 	}
141752c9ce25SScott Long 
1418f09f8e3eSAlexander Motin 	if ((old_periph = cam_periph_find(path, "aprobe")) != NULL) {
141952c9ce25SScott Long 		probe_softc *softc;
142052c9ce25SScott Long 
142152c9ce25SScott Long 		softc = (probe_softc *)old_periph->softc;
142252c9ce25SScott Long 		TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
142352c9ce25SScott Long 				  periph_links.tqe);
142483c5d981SAlexander Motin 		softc->restart = 1;
142552c9ce25SScott Long 	} else {
142652c9ce25SScott Long 		status = cam_periph_alloc(proberegister, NULL, probecleanup,
1427f09f8e3eSAlexander Motin 					  probestart, "aprobe",
142852c9ce25SScott Long 					  CAM_PERIPH_BIO,
142952c9ce25SScott Long 					  request_ccb->ccb_h.path, NULL, 0,
143052c9ce25SScott Long 					  request_ccb);
143152c9ce25SScott Long 
143252c9ce25SScott Long 		if (status != CAM_REQ_CMP) {
143352c9ce25SScott Long 			xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
143452c9ce25SScott Long 			    "returned an error, can't continue probe\n");
143552c9ce25SScott Long 			request_ccb->ccb_h.status = status;
143652c9ce25SScott Long 			xpt_done(request_ccb);
143752c9ce25SScott Long 		}
143852c9ce25SScott Long 	}
143952c9ce25SScott Long }
144052c9ce25SScott Long 
144152c9ce25SScott Long static void
144252c9ce25SScott Long xptscandone(struct cam_periph *periph, union ccb *done_ccb)
144352c9ce25SScott Long {
144452c9ce25SScott Long 	xpt_release_path(done_ccb->ccb_h.path);
144552c9ce25SScott Long 	free(done_ccb->ccb_h.path, M_CAMXPT);
144652c9ce25SScott Long 	free(done_ccb, M_CAMXPT);
144752c9ce25SScott Long }
144852c9ce25SScott Long 
144952c9ce25SScott Long static struct cam_ed *
145052c9ce25SScott Long ata_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
145152c9ce25SScott Long {
145252c9ce25SScott Long 	struct cam_path path;
145330a4094fSAlexander Motin 	struct ata_quirk_entry *quirk;
145452c9ce25SScott Long 	struct cam_ed *device;
145552c9ce25SScott Long 	struct cam_ed *cur_device;
145652c9ce25SScott Long 
145752c9ce25SScott Long 	device = xpt_alloc_device(bus, target, lun_id);
145852c9ce25SScott Long 	if (device == NULL)
145952c9ce25SScott Long 		return (NULL);
146052c9ce25SScott Long 
146152c9ce25SScott Long 	/*
146252c9ce25SScott Long 	 * Take the default quirk entry until we have inquiry
146352c9ce25SScott Long 	 * data and can determine a better quirk to use.
146452c9ce25SScott Long 	 */
146530a4094fSAlexander Motin 	quirk = &ata_quirk_table[ata_quirk_table_size - 1];
146652c9ce25SScott Long 	device->quirk = (void *)quirk;
146730a4094fSAlexander Motin 	device->mintags = 0;
146830a4094fSAlexander Motin 	device->maxtags = 0;
146952c9ce25SScott Long 	bzero(&device->inq_data, sizeof(device->inq_data));
147052c9ce25SScott Long 	device->inq_flags = 0;
147152c9ce25SScott Long 	device->queue_flags = 0;
147252c9ce25SScott Long 	device->serial_num = NULL;
147352c9ce25SScott Long 	device->serial_num_len = 0;
147452c9ce25SScott Long 
147552c9ce25SScott Long 	/*
147652c9ce25SScott Long 	 * XXX should be limited by number of CCBs this bus can
147752c9ce25SScott Long 	 * do.
147852c9ce25SScott Long 	 */
147952c9ce25SScott Long 	bus->sim->max_ccbs += device->ccbq.devq_openings;
148052c9ce25SScott Long 	/* Insertion sort into our target's device list */
148152c9ce25SScott Long 	cur_device = TAILQ_FIRST(&target->ed_entries);
148252c9ce25SScott Long 	while (cur_device != NULL && cur_device->lun_id < lun_id)
148352c9ce25SScott Long 		cur_device = TAILQ_NEXT(cur_device, links);
148452c9ce25SScott Long 	if (cur_device != NULL) {
148552c9ce25SScott Long 		TAILQ_INSERT_BEFORE(cur_device, device, links);
148652c9ce25SScott Long 	} else {
148752c9ce25SScott Long 		TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
148852c9ce25SScott Long 	}
148952c9ce25SScott Long 	target->generation++;
149052c9ce25SScott Long 	if (lun_id != CAM_LUN_WILDCARD) {
149152c9ce25SScott Long 		xpt_compile_path(&path,
149252c9ce25SScott Long 				 NULL,
149352c9ce25SScott Long 				 bus->path_id,
149452c9ce25SScott Long 				 target->target_id,
149552c9ce25SScott Long 				 lun_id);
149652c9ce25SScott Long 		ata_device_transport(&path);
149752c9ce25SScott Long 		xpt_release_path(&path);
149852c9ce25SScott Long 	}
149952c9ce25SScott Long 
150052c9ce25SScott Long 	return (device);
150152c9ce25SScott Long }
150252c9ce25SScott Long 
150352c9ce25SScott Long static void
150452c9ce25SScott Long ata_device_transport(struct cam_path *path)
150552c9ce25SScott Long {
150652c9ce25SScott Long 	struct ccb_pathinq cpi;
15074b997c49SAlexander Motin 	struct ccb_trans_settings cts;
15084b997c49SAlexander Motin 	struct scsi_inquiry_data *inq_buf = NULL;
15094b997c49SAlexander Motin 	struct ata_params *ident_buf = NULL;
151052c9ce25SScott Long 
151152c9ce25SScott Long 	/* Get transport information from the SIM */
151283c5d981SAlexander Motin 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
151352c9ce25SScott Long 	cpi.ccb_h.func_code = XPT_PATH_INQ;
151452c9ce25SScott Long 	xpt_action((union ccb *)&cpi);
151552c9ce25SScott Long 
151652c9ce25SScott Long 	path->device->transport = cpi.transport;
15174b997c49SAlexander Motin 	if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
15184b997c49SAlexander Motin 		inq_buf = &path->device->inq_data;
15194b997c49SAlexander Motin 	if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) != 0)
15204b997c49SAlexander Motin 		ident_buf = &path->device->ident_data;
15214b997c49SAlexander Motin 	if (path->device->protocol == PROTO_ATA) {
15224b997c49SAlexander Motin 		path->device->protocol_version = ident_buf ?
15234b997c49SAlexander Motin 		    ata_version(ident_buf->version_major) : cpi.protocol_version;
15244b997c49SAlexander Motin 	} else if (path->device->protocol == PROTO_SCSI) {
15254b997c49SAlexander Motin 		path->device->protocol_version = inq_buf ?
15264b997c49SAlexander Motin 		    SID_ANSI_REV(inq_buf) : cpi.protocol_version;
152752c9ce25SScott Long 	}
15284b997c49SAlexander Motin 	path->device->transport_version = ident_buf ?
15294b997c49SAlexander Motin 	    ata_version(ident_buf->version_major) : cpi.transport_version;
153052c9ce25SScott Long 
153152c9ce25SScott Long 	/* Tell the controller what we think */
153283c5d981SAlexander Motin 	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
153352c9ce25SScott Long 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
153452c9ce25SScott Long 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
153552c9ce25SScott Long 	cts.transport = path->device->transport;
153652c9ce25SScott Long 	cts.transport_version = path->device->transport_version;
153752c9ce25SScott Long 	cts.protocol = path->device->protocol;
153852c9ce25SScott Long 	cts.protocol_version = path->device->protocol_version;
153952c9ce25SScott Long 	cts.proto_specific.valid = 0;
15404cca1530SAlexander Motin 	if (ident_buf) {
15414cca1530SAlexander Motin 		if (path->device->transport == XPORT_ATA) {
15424cca1530SAlexander Motin 			cts.xport_specific.ata.atapi =
15434cca1530SAlexander Motin 			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 :
15444cca1530SAlexander Motin 			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0;
15454cca1530SAlexander Motin 			cts.xport_specific.ata.valid = CTS_ATA_VALID_ATAPI;
15464cca1530SAlexander Motin 		} else {
15474cca1530SAlexander Motin 			cts.xport_specific.sata.atapi =
15484cca1530SAlexander Motin 			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 :
15494cca1530SAlexander Motin 			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0;
15504cca1530SAlexander Motin 			cts.xport_specific.sata.valid = CTS_SATA_VALID_ATAPI;
15514cca1530SAlexander Motin 		}
15524cca1530SAlexander Motin 	} else
155352c9ce25SScott Long 		cts.xport_specific.valid = 0;
155452c9ce25SScott Long 	xpt_action((union ccb *)&cts);
155552c9ce25SScott Long }
155652c9ce25SScott Long 
155752c9ce25SScott Long static void
155852c9ce25SScott Long ata_action(union ccb *start_ccb)
155952c9ce25SScott Long {
156052c9ce25SScott Long 
156152c9ce25SScott Long 	switch (start_ccb->ccb_h.func_code) {
156252c9ce25SScott Long 	case XPT_SET_TRAN_SETTINGS:
156352c9ce25SScott Long 	{
156430a4094fSAlexander Motin 		ata_set_transfer_settings(&start_ccb->cts,
156552c9ce25SScott Long 					   start_ccb->ccb_h.path->device,
156652c9ce25SScott Long 					   /*async_update*/FALSE);
156752c9ce25SScott Long 		break;
156852c9ce25SScott Long 	}
156952c9ce25SScott Long 	case XPT_SCAN_BUS:
15700e85f214SMatt Jacob 	case XPT_SCAN_TGT:
157152c9ce25SScott Long 		ata_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
157252c9ce25SScott Long 		break;
157352c9ce25SScott Long 	case XPT_SCAN_LUN:
157452c9ce25SScott Long 		ata_scan_lun(start_ccb->ccb_h.path->periph,
157552c9ce25SScott Long 			      start_ccb->ccb_h.path, start_ccb->crcn.flags,
157652c9ce25SScott Long 			      start_ccb);
157752c9ce25SScott Long 		break;
157852c9ce25SScott Long 	case XPT_GET_TRAN_SETTINGS:
157952c9ce25SScott Long 	{
158052c9ce25SScott Long 		struct cam_sim *sim;
158152c9ce25SScott Long 
158252c9ce25SScott Long 		sim = start_ccb->ccb_h.path->bus->sim;
158352c9ce25SScott Long 		(*(sim->sim_action))(sim, start_ccb);
158452c9ce25SScott Long 		break;
158552c9ce25SScott Long 	}
15864cca1530SAlexander Motin 	case XPT_SCSI_IO:
15874cca1530SAlexander Motin 	{
15884cca1530SAlexander Motin 		struct cam_ed *device;
15894cca1530SAlexander Motin 		u_int	maxlen = 0;
15904cca1530SAlexander Motin 
15914cca1530SAlexander Motin 		device = start_ccb->ccb_h.path->device;
15924cca1530SAlexander Motin 		if (device->protocol == PROTO_SCSI &&
15934cca1530SAlexander Motin 		    (device->flags & CAM_DEV_IDENTIFY_DATA_VALID)) {
15944cca1530SAlexander Motin 			uint16_t p =
15954cca1530SAlexander Motin 			    device->ident_data.config & ATA_PROTO_MASK;
15964cca1530SAlexander Motin 
15974cca1530SAlexander Motin 			maxlen = (p == ATA_PROTO_ATAPI_16) ? 16 :
15984cca1530SAlexander Motin 			    (p == ATA_PROTO_ATAPI_12) ? 12 : 0;
15994cca1530SAlexander Motin 		}
16004cca1530SAlexander Motin 		if (start_ccb->csio.cdb_len > maxlen) {
16014cca1530SAlexander Motin 			start_ccb->ccb_h.status = CAM_REQ_INVALID;
16024cca1530SAlexander Motin 			xpt_done(start_ccb);
16034cca1530SAlexander Motin 			break;
16044cca1530SAlexander Motin 		}
16054cca1530SAlexander Motin 		/* FALLTHROUGH */
16064cca1530SAlexander Motin 	}
160752c9ce25SScott Long 	default:
160852c9ce25SScott Long 		xpt_action_default(start_ccb);
160952c9ce25SScott Long 		break;
161052c9ce25SScott Long 	}
161152c9ce25SScott Long }
161252c9ce25SScott Long 
161352c9ce25SScott Long static void
161430a4094fSAlexander Motin ata_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
161552c9ce25SScott Long 			   int async_update)
161652c9ce25SScott Long {
161752c9ce25SScott Long 	struct	ccb_pathinq cpi;
161852c9ce25SScott Long 	struct	ccb_trans_settings cur_cts;
161952c9ce25SScott Long 	struct	ccb_trans_settings_scsi *scsi;
162052c9ce25SScott Long 	struct	ccb_trans_settings_scsi *cur_scsi;
162152c9ce25SScott Long 	struct	cam_sim *sim;
162252c9ce25SScott Long 	struct	scsi_inquiry_data *inq_data;
162352c9ce25SScott Long 
162452c9ce25SScott Long 	if (device == NULL) {
162552c9ce25SScott Long 		cts->ccb_h.status = CAM_PATH_INVALID;
162652c9ce25SScott Long 		xpt_done((union ccb *)cts);
162752c9ce25SScott Long 		return;
162852c9ce25SScott Long 	}
162952c9ce25SScott Long 
163052c9ce25SScott Long 	if (cts->protocol == PROTO_UNKNOWN
163152c9ce25SScott Long 	 || cts->protocol == PROTO_UNSPECIFIED) {
163252c9ce25SScott Long 		cts->protocol = device->protocol;
163352c9ce25SScott Long 		cts->protocol_version = device->protocol_version;
163452c9ce25SScott Long 	}
163552c9ce25SScott Long 
163652c9ce25SScott Long 	if (cts->protocol_version == PROTO_VERSION_UNKNOWN
163752c9ce25SScott Long 	 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
163852c9ce25SScott Long 		cts->protocol_version = device->protocol_version;
163952c9ce25SScott Long 
164052c9ce25SScott Long 	if (cts->protocol != device->protocol) {
164152c9ce25SScott Long 		xpt_print(cts->ccb_h.path, "Uninitialized Protocol %x:%x?\n",
164252c9ce25SScott Long 		       cts->protocol, device->protocol);
164352c9ce25SScott Long 		cts->protocol = device->protocol;
164452c9ce25SScott Long 	}
164552c9ce25SScott Long 
164652c9ce25SScott Long 	if (cts->protocol_version > device->protocol_version) {
164752c9ce25SScott Long 		if (bootverbose) {
164852c9ce25SScott Long 			xpt_print(cts->ccb_h.path, "Down reving Protocol "
164952c9ce25SScott Long 			    "Version from %d to %d?\n", cts->protocol_version,
165052c9ce25SScott Long 			    device->protocol_version);
165152c9ce25SScott Long 		}
165252c9ce25SScott Long 		cts->protocol_version = device->protocol_version;
165352c9ce25SScott Long 	}
165452c9ce25SScott Long 
165552c9ce25SScott Long 	if (cts->transport == XPORT_UNKNOWN
165652c9ce25SScott Long 	 || cts->transport == XPORT_UNSPECIFIED) {
165752c9ce25SScott Long 		cts->transport = device->transport;
165852c9ce25SScott Long 		cts->transport_version = device->transport_version;
165952c9ce25SScott Long 	}
166052c9ce25SScott Long 
166152c9ce25SScott Long 	if (cts->transport_version == XPORT_VERSION_UNKNOWN
166252c9ce25SScott Long 	 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
166352c9ce25SScott Long 		cts->transport_version = device->transport_version;
166452c9ce25SScott Long 
166552c9ce25SScott Long 	if (cts->transport != device->transport) {
166652c9ce25SScott Long 		xpt_print(cts->ccb_h.path, "Uninitialized Transport %x:%x?\n",
166752c9ce25SScott Long 		    cts->transport, device->transport);
166852c9ce25SScott Long 		cts->transport = device->transport;
166952c9ce25SScott Long 	}
167052c9ce25SScott Long 
167152c9ce25SScott Long 	if (cts->transport_version > device->transport_version) {
167252c9ce25SScott Long 		if (bootverbose) {
167352c9ce25SScott Long 			xpt_print(cts->ccb_h.path, "Down reving Transport "
167452c9ce25SScott Long 			    "Version from %d to %d?\n", cts->transport_version,
167552c9ce25SScott Long 			    device->transport_version);
167652c9ce25SScott Long 		}
167752c9ce25SScott Long 		cts->transport_version = device->transport_version;
167852c9ce25SScott Long 	}
167952c9ce25SScott Long 
168052c9ce25SScott Long 	sim = cts->ccb_h.path->bus->sim;
168152c9ce25SScott Long 
168252c9ce25SScott Long 	/*
168352c9ce25SScott Long 	 * Nothing more of interest to do unless
168452c9ce25SScott Long 	 * this is a device connected via the
168552c9ce25SScott Long 	 * SCSI protocol.
168652c9ce25SScott Long 	 */
168752c9ce25SScott Long 	if (cts->protocol != PROTO_SCSI) {
168852c9ce25SScott Long 		if (async_update == FALSE)
168952c9ce25SScott Long 			(*(sim->sim_action))(sim, (union ccb *)cts);
169052c9ce25SScott Long 		return;
169152c9ce25SScott Long 	}
169252c9ce25SScott Long 
169352c9ce25SScott Long 	inq_data = &device->inq_data;
169452c9ce25SScott Long 	scsi = &cts->proto_specific.scsi;
169583c5d981SAlexander Motin 	xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NONE);
169652c9ce25SScott Long 	cpi.ccb_h.func_code = XPT_PATH_INQ;
169752c9ce25SScott Long 	xpt_action((union ccb *)&cpi);
169852c9ce25SScott Long 
169952c9ce25SScott Long 	/* SCSI specific sanity checking */
170052c9ce25SScott Long 	if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
170152c9ce25SScott Long 	 || (INQ_DATA_TQ_ENABLED(inq_data)) == 0
170252c9ce25SScott Long 	 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
170352c9ce25SScott Long 	 || (device->mintags == 0)) {
170452c9ce25SScott Long 		/*
170552c9ce25SScott Long 		 * Can't tag on hardware that doesn't support tags,
170652c9ce25SScott Long 		 * doesn't have it enabled, or has broken tag support.
170752c9ce25SScott Long 		 */
170852c9ce25SScott Long 		scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
170952c9ce25SScott Long 	}
171052c9ce25SScott Long 
171152c9ce25SScott Long 	if (async_update == FALSE) {
171252c9ce25SScott Long 		/*
171352c9ce25SScott Long 		 * Perform sanity checking against what the
171452c9ce25SScott Long 		 * controller and device can do.
171552c9ce25SScott Long 		 */
171683c5d981SAlexander Motin 		xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NONE);
171752c9ce25SScott Long 		cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
171852c9ce25SScott Long 		cur_cts.type = cts->type;
171952c9ce25SScott Long 		xpt_action((union ccb *)&cur_cts);
172052c9ce25SScott Long 		if ((cur_cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
172152c9ce25SScott Long 			return;
172252c9ce25SScott Long 		}
172352c9ce25SScott Long 		cur_scsi = &cur_cts.proto_specific.scsi;
172452c9ce25SScott Long 		if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
172552c9ce25SScott Long 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
172652c9ce25SScott Long 			scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
172752c9ce25SScott Long 		}
172852c9ce25SScott Long 		if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
172952c9ce25SScott Long 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
173052c9ce25SScott Long 	}
173152c9ce25SScott Long 
173252c9ce25SScott Long 	if (cts->type == CTS_TYPE_CURRENT_SETTINGS
173352c9ce25SScott Long 	 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
173452c9ce25SScott Long 		int device_tagenb;
173552c9ce25SScott Long 
173652c9ce25SScott Long 		/*
173752c9ce25SScott Long 		 * If we are transitioning from tags to no-tags or
173852c9ce25SScott Long 		 * vice-versa, we need to carefully freeze and restart
173952c9ce25SScott Long 		 * the queue so that we don't overlap tagged and non-tagged
174052c9ce25SScott Long 		 * commands.  We also temporarily stop tags if there is
174152c9ce25SScott Long 		 * a change in transfer negotiation settings to allow
174252c9ce25SScott Long 		 * "tag-less" negotiation.
174352c9ce25SScott Long 		 */
174452c9ce25SScott Long 		if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
174552c9ce25SScott Long 		 || (device->inq_flags & SID_CmdQue) != 0)
174652c9ce25SScott Long 			device_tagenb = TRUE;
174752c9ce25SScott Long 		else
174852c9ce25SScott Long 			device_tagenb = FALSE;
174952c9ce25SScott Long 
175052c9ce25SScott Long 		if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
175152c9ce25SScott Long 		  && device_tagenb == FALSE)
175252c9ce25SScott Long 		 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
175352c9ce25SScott Long 		  && device_tagenb == TRUE)) {
175452c9ce25SScott Long 
175552c9ce25SScott Long 			if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
175652c9ce25SScott Long 				/*
175752c9ce25SScott Long 				 * Delay change to use tags until after a
175852c9ce25SScott Long 				 * few commands have gone to this device so
175952c9ce25SScott Long 				 * the controller has time to perform transfer
176052c9ce25SScott Long 				 * negotiations without tagged messages getting
176152c9ce25SScott Long 				 * in the way.
176252c9ce25SScott Long 				 */
176352c9ce25SScott Long 				device->tag_delay_count = CAM_TAG_DELAY_COUNT;
176452c9ce25SScott Long 				device->flags |= CAM_DEV_TAG_AFTER_COUNT;
176552c9ce25SScott Long 			} else {
176630a4094fSAlexander Motin 				xpt_stop_tags(cts->ccb_h.path);
176752c9ce25SScott Long 			}
176852c9ce25SScott Long 		}
176952c9ce25SScott Long 	}
177052c9ce25SScott Long 	if (async_update == FALSE)
177152c9ce25SScott Long 		(*(sim->sim_action))(sim, (union ccb *)cts);
177252c9ce25SScott Long }
177352c9ce25SScott Long 
177452c9ce25SScott Long /*
177552c9ce25SScott Long  * Handle any per-device event notifications that require action by the XPT.
177652c9ce25SScott Long  */
177752c9ce25SScott Long static void
177852c9ce25SScott Long ata_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
177952c9ce25SScott Long 	      struct cam_ed *device, void *async_arg)
178052c9ce25SScott Long {
178152c9ce25SScott Long 	cam_status status;
178252c9ce25SScott Long 	struct cam_path newpath;
178352c9ce25SScott Long 
178452c9ce25SScott Long 	/*
178552c9ce25SScott Long 	 * We only need to handle events for real devices.
178652c9ce25SScott Long 	 */
178752c9ce25SScott Long 	if (target->target_id == CAM_TARGET_WILDCARD
178852c9ce25SScott Long 	 || device->lun_id == CAM_LUN_WILDCARD)
178952c9ce25SScott Long 		return;
179052c9ce25SScott Long 
179152c9ce25SScott Long 	/*
179252c9ce25SScott Long 	 * We need our own path with wildcards expanded to
179352c9ce25SScott Long 	 * handle certain types of events.
179452c9ce25SScott Long 	 */
179552c9ce25SScott Long 	if ((async_code == AC_SENT_BDR)
179652c9ce25SScott Long 	 || (async_code == AC_BUS_RESET)
179752c9ce25SScott Long 	 || (async_code == AC_INQ_CHANGED))
179852c9ce25SScott Long 		status = xpt_compile_path(&newpath, NULL,
179952c9ce25SScott Long 					  bus->path_id,
180052c9ce25SScott Long 					  target->target_id,
180152c9ce25SScott Long 					  device->lun_id);
180252c9ce25SScott Long 	else
180352c9ce25SScott Long 		status = CAM_REQ_CMP_ERR;
180452c9ce25SScott Long 
180552c9ce25SScott Long 	if (status == CAM_REQ_CMP) {
180652c9ce25SScott Long 		if (async_code == AC_INQ_CHANGED) {
180752c9ce25SScott Long 			/*
180852c9ce25SScott Long 			 * We've sent a start unit command, or
180952c9ce25SScott Long 			 * something similar to a device that
181052c9ce25SScott Long 			 * may have caused its inquiry data to
181152c9ce25SScott Long 			 * change. So we re-scan the device to
181252c9ce25SScott Long 			 * refresh the inquiry data for it.
181352c9ce25SScott Long 			 */
181452c9ce25SScott Long 			ata_scan_lun(newpath.periph, &newpath,
181552c9ce25SScott Long 				     CAM_EXPECT_INQ_CHANGE, NULL);
181683c5d981SAlexander Motin 		} else {
181783c5d981SAlexander Motin 			/* We need to reinitialize device after reset. */
181883c5d981SAlexander Motin 			ata_scan_lun(newpath.periph, &newpath,
181983c5d981SAlexander Motin 				     0, NULL);
182052c9ce25SScott Long 		}
182152c9ce25SScott Long 		xpt_release_path(&newpath);
1822f98d7a47SAlexander Motin 	} else if (async_code == AC_LOST_DEVICE &&
1823f98d7a47SAlexander Motin 	    (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
182452c9ce25SScott Long 		device->flags |= CAM_DEV_UNCONFIGURED;
1825f98d7a47SAlexander Motin 		xpt_release_device(device);
182652c9ce25SScott Long 	} else if (async_code == AC_TRANSFER_NEG) {
182752c9ce25SScott Long 		struct ccb_trans_settings *settings;
182852c9ce25SScott Long 
182952c9ce25SScott Long 		settings = (struct ccb_trans_settings *)async_arg;
183030a4094fSAlexander Motin 		ata_set_transfer_settings(settings, device,
183152c9ce25SScott Long 					  /*async_update*/TRUE);
183252c9ce25SScott Long 	}
183352c9ce25SScott Long }
183452c9ce25SScott Long 
183557079b17SAlexander Motin static void
183657079b17SAlexander Motin ata_announce_periph(struct cam_periph *periph)
183757079b17SAlexander Motin {
183857079b17SAlexander Motin 	struct	ccb_pathinq cpi;
183957079b17SAlexander Motin 	struct	ccb_trans_settings cts;
184057079b17SAlexander Motin 	struct	cam_path *path = periph->path;
184157079b17SAlexander Motin 	u_int	speed;
184257079b17SAlexander Motin 	u_int	mb;
184357079b17SAlexander Motin 
184457079b17SAlexander Motin 	mtx_assert(periph->sim->mtx, MA_OWNED);
184557079b17SAlexander Motin 
184657079b17SAlexander Motin 	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
184757079b17SAlexander Motin 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
184857079b17SAlexander Motin 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
184957079b17SAlexander Motin 	xpt_action((union ccb*)&cts);
185057079b17SAlexander Motin 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
185157079b17SAlexander Motin 		return;
185257079b17SAlexander Motin 	/* Ask the SIM for its base transfer speed */
185357079b17SAlexander Motin 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
185457079b17SAlexander Motin 	cpi.ccb_h.func_code = XPT_PATH_INQ;
185557079b17SAlexander Motin 	xpt_action((union ccb *)&cpi);
185657079b17SAlexander Motin 	/* Report connection speed */
185757079b17SAlexander Motin 	speed = cpi.base_transfer_speed;
185857079b17SAlexander Motin 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_ATA) {
185957079b17SAlexander Motin 		struct	ccb_trans_settings_ata *ata =
186057079b17SAlexander Motin 		    &cts.xport_specific.ata;
186157079b17SAlexander Motin 
186257079b17SAlexander Motin 		if (ata->valid & CTS_ATA_VALID_MODE)
186357079b17SAlexander Motin 			speed = ata_mode2speed(ata->mode);
186457079b17SAlexander Motin 	}
186557079b17SAlexander Motin 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SATA) {
186657079b17SAlexander Motin 		struct	ccb_trans_settings_sata *sata =
186757079b17SAlexander Motin 		    &cts.xport_specific.sata;
186857079b17SAlexander Motin 
186957079b17SAlexander Motin 		if (sata->valid & CTS_SATA_VALID_REVISION)
187057079b17SAlexander Motin 			speed = ata_revision2speed(sata->revision);
187157079b17SAlexander Motin 	}
187257079b17SAlexander Motin 	mb = speed / 1000;
187357079b17SAlexander Motin 	if (mb > 0)
187457079b17SAlexander Motin 		printf("%s%d: %d.%03dMB/s transfers",
187557079b17SAlexander Motin 		       periph->periph_name, periph->unit_number,
187657079b17SAlexander Motin 		       mb, speed % 1000);
187757079b17SAlexander Motin 	else
187857079b17SAlexander Motin 		printf("%s%d: %dKB/s transfers", periph->periph_name,
187957079b17SAlexander Motin 		       periph->unit_number, speed);
188057079b17SAlexander Motin 	/* Report additional information about connection */
188157079b17SAlexander Motin 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_ATA) {
188257079b17SAlexander Motin 		struct ccb_trans_settings_ata *ata =
188357079b17SAlexander Motin 		    &cts.xport_specific.ata;
188457079b17SAlexander Motin 
188557079b17SAlexander Motin 		printf(" (");
188657079b17SAlexander Motin 		if (ata->valid & CTS_ATA_VALID_MODE)
188757079b17SAlexander Motin 			printf("%s, ", ata_mode2string(ata->mode));
188857079b17SAlexander Motin 		if ((ata->valid & CTS_ATA_VALID_ATAPI) && ata->atapi != 0)
188957079b17SAlexander Motin 			printf("ATAPI %dbytes, ", ata->atapi);
189057079b17SAlexander Motin 		if (ata->valid & CTS_ATA_VALID_BYTECOUNT)
189157079b17SAlexander Motin 			printf("PIO %dbytes", ata->bytecount);
189257079b17SAlexander Motin 		printf(")");
189357079b17SAlexander Motin 	}
189457079b17SAlexander Motin 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SATA) {
189557079b17SAlexander Motin 		struct ccb_trans_settings_sata *sata =
189657079b17SAlexander Motin 		    &cts.xport_specific.sata;
189757079b17SAlexander Motin 
189857079b17SAlexander Motin 		printf(" (");
189957079b17SAlexander Motin 		if (sata->valid & CTS_SATA_VALID_REVISION)
190057079b17SAlexander Motin 			printf("SATA %d.x, ", sata->revision);
190157079b17SAlexander Motin 		else
190257079b17SAlexander Motin 			printf("SATA, ");
190357079b17SAlexander Motin 		if (sata->valid & CTS_SATA_VALID_MODE)
190457079b17SAlexander Motin 			printf("%s, ", ata_mode2string(sata->mode));
190557079b17SAlexander Motin 		if ((sata->valid & CTS_ATA_VALID_ATAPI) && sata->atapi != 0)
190657079b17SAlexander Motin 			printf("ATAPI %dbytes, ", sata->atapi);
190757079b17SAlexander Motin 		if (sata->valid & CTS_SATA_VALID_BYTECOUNT)
190857079b17SAlexander Motin 			printf("PIO %dbytes", sata->bytecount);
190957079b17SAlexander Motin 		printf(")");
191057079b17SAlexander Motin 	}
191157079b17SAlexander Motin 	printf("\n");
191257079b17SAlexander Motin }
191357079b17SAlexander Motin 
1914