xref: /freebsd/sys/cam/ata/ata_xpt.c (revision c2b82f3e)
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;
73052c9ce25SScott Long 	u_int32_t  priority;
731da6808c1SAlexander Motin 	u_int caps;
73265d0fb03SAlexander Motin 	int found = 1;
73352c9ce25SScott Long 
73452c9ce25SScott Long 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
73552c9ce25SScott Long 
73652c9ce25SScott Long 	softc = (probe_softc *)periph->softc;
73752c9ce25SScott Long 	path = done_ccb->ccb_h.path;
73852c9ce25SScott Long 	priority = done_ccb->ccb_h.pinfo.priority;
73952c9ce25SScott Long 	ident_buf = &path->device->ident_data;
74052c9ce25SScott Long 
7411e637ba6SAlexander Motin 	if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
74225a519a9SAlexander Motin 		if (softc->restart) {
74325a519a9SAlexander Motin 			if (bootverbose) {
74425a519a9SAlexander Motin 				cam_error_print(done_ccb,
74525a519a9SAlexander Motin 				    CAM_ESF_ALL, CAM_EPF_ALL);
74625a519a9SAlexander Motin 			}
74725a519a9SAlexander Motin 		} else if (cam_periph_error(done_ccb, 0, 0, NULL) == ERESTART)
7481e637ba6SAlexander Motin 			return;
74925a519a9SAlexander Motin 		if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
7501e637ba6SAlexander Motin 			/* Don't wedge the queue */
7511e637ba6SAlexander Motin 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
7521e637ba6SAlexander Motin 					 /*run_queue*/TRUE);
7531e637ba6SAlexander Motin 		}
75425a519a9SAlexander Motin 		if (softc->restart) {
75525a519a9SAlexander Motin 			softc->faults++;
75625a519a9SAlexander Motin 			if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) ==
75725a519a9SAlexander Motin 			    CAM_CMD_TIMEOUT)
75825a519a9SAlexander Motin 				softc->faults += 4;
75925a519a9SAlexander Motin 			if (softc->faults < 10)
76025a519a9SAlexander Motin 				goto done;
76125a519a9SAlexander Motin 			else
76225a519a9SAlexander Motin 				softc->restart = 0;
76325a519a9SAlexander Motin 		} else
7641e637ba6SAlexander Motin 		/* Old PIO2 devices may not support mode setting. */
7651e637ba6SAlexander Motin 		if (softc->action == PROBE_SETMODE &&
7661e637ba6SAlexander Motin 		    ata_max_pmode(ident_buf) <= ATA_PIO2 &&
7671e637ba6SAlexander Motin 		    (ident_buf->capabilities1 & ATA_SUPPORT_IORDY) == 0)
7681e637ba6SAlexander Motin 			goto noerror;
7691e637ba6SAlexander Motin 		/*
7701e637ba6SAlexander Motin 		 * If we get to this point, we got an error status back
7711e637ba6SAlexander Motin 		 * from the inquiry and the error status doesn't require
7721e637ba6SAlexander Motin 		 * automatically retrying the command.  Therefore, the
7731e637ba6SAlexander Motin 		 * inquiry failed.  If we had inquiry information before
7741e637ba6SAlexander Motin 		 * for this device, but this latest inquiry command failed,
7751e637ba6SAlexander Motin 		 * the device has probably gone away.  If this device isn't
7761e637ba6SAlexander Motin 		 * already marked unconfigured, notify the peripheral
7771e637ba6SAlexander Motin 		 * drivers that this device is no more.
7781e637ba6SAlexander Motin 		 */
77925a519a9SAlexander Motin device_fail:	if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
7801e637ba6SAlexander Motin 			xpt_async(AC_LOST_DEVICE, path, NULL);
7811e637ba6SAlexander Motin 		found = 0;
7821e637ba6SAlexander Motin 		goto done;
7831e637ba6SAlexander Motin 	}
7841e637ba6SAlexander Motin noerror:
785a9b8edb1SAlexander Motin 	if (softc->restart)
786a9b8edb1SAlexander Motin 		goto done;
78752c9ce25SScott Long 	switch (softc->action) {
78852c9ce25SScott Long 	case PROBE_RESET:
7891e637ba6SAlexander Motin 	{
79052c9ce25SScott Long 		int sign = (done_ccb->ataio.res.lba_high << 8) +
79152c9ce25SScott Long 		    done_ccb->ataio.res.lba_mid;
7927bdb664eSAlexander Motin 		if (bootverbose)
79352c9ce25SScott Long 			xpt_print(path, "SIGNATURE: %04x\n", sign);
79452c9ce25SScott Long 		if (sign == 0x0000 &&
79552c9ce25SScott Long 		    done_ccb->ccb_h.target_id != 15) {
79652c9ce25SScott Long 			path->device->protocol = PROTO_ATA;
79752c9ce25SScott Long 			PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
79852c9ce25SScott Long 		} else if (sign == 0x9669 &&
79952c9ce25SScott Long 		    done_ccb->ccb_h.target_id == 15) {
80052c9ce25SScott Long 			/* Report SIM that PM is present. */
80152c9ce25SScott Long 			bzero(&cts, sizeof(cts));
80283c5d981SAlexander Motin 			xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
80352c9ce25SScott Long 			cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
80452c9ce25SScott Long 			cts.type = CTS_TYPE_CURRENT_SETTINGS;
80552c9ce25SScott Long 			cts.xport_specific.sata.pm_present = 1;
80652c9ce25SScott Long 			cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
80752c9ce25SScott Long 			xpt_action((union ccb *)&cts);
80852c9ce25SScott Long 			path->device->protocol = PROTO_SATAPM;
80952c9ce25SScott Long 			PROBE_SET_ACTION(softc, PROBE_PM_PID);
81052c9ce25SScott Long 		} else if (sign == 0xeb14 &&
81152c9ce25SScott Long 		    done_ccb->ccb_h.target_id != 15) {
81252c9ce25SScott Long 			path->device->protocol = PROTO_SCSI;
81352c9ce25SScott Long 			PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
81452c9ce25SScott Long 		} else {
81552c9ce25SScott Long 			if (done_ccb->ccb_h.target_id != 15) {
81652c9ce25SScott Long 				xpt_print(path,
81752c9ce25SScott Long 				    "Unexpected signature 0x%04x\n", sign);
81852c9ce25SScott Long 			}
81965d0fb03SAlexander Motin 			goto device_fail;
82052c9ce25SScott Long 		}
82152c9ce25SScott Long 		xpt_release_ccb(done_ccb);
82252c9ce25SScott Long 		xpt_schedule(periph, priority);
82352c9ce25SScott Long 		return;
82452c9ce25SScott Long 	}
82552c9ce25SScott Long 	case PROBE_IDENTIFY:
82652c9ce25SScott Long 	{
827699f853bSAlexander Motin 		struct ccb_pathinq cpi;
82852c9ce25SScott Long 		int16_t *ptr;
8292eb4a8dcSAlexander Motin 		int changed = 1;
83052c9ce25SScott Long 
831a9b8edb1SAlexander Motin 		ident_buf = &softc->ident_data;
83252c9ce25SScott Long 		for (ptr = (int16_t *)ident_buf;
83352c9ce25SScott Long 		     ptr < (int16_t *)ident_buf + sizeof(struct ata_params)/2; ptr++) {
83452c9ce25SScott Long 			*ptr = le16toh(*ptr);
83552c9ce25SScott Long 		}
83652c9ce25SScott Long 		if (strncmp(ident_buf->model, "FX", 2) &&
83752c9ce25SScott Long 		    strncmp(ident_buf->model, "NEC", 3) &&
83852c9ce25SScott Long 		    strncmp(ident_buf->model, "Pioneer", 7) &&
83952c9ce25SScott Long 		    strncmp(ident_buf->model, "SHARP", 5)) {
84052c9ce25SScott Long 			ata_bswap(ident_buf->model, sizeof(ident_buf->model));
84152c9ce25SScott Long 			ata_bswap(ident_buf->revision, sizeof(ident_buf->revision));
84252c9ce25SScott Long 			ata_bswap(ident_buf->serial, sizeof(ident_buf->serial));
84352c9ce25SScott Long 		}
84452c9ce25SScott Long 		ata_btrim(ident_buf->model, sizeof(ident_buf->model));
84552c9ce25SScott Long 		ata_bpack(ident_buf->model, ident_buf->model, sizeof(ident_buf->model));
84652c9ce25SScott Long 		ata_btrim(ident_buf->revision, sizeof(ident_buf->revision));
84752c9ce25SScott Long 		ata_bpack(ident_buf->revision, ident_buf->revision, sizeof(ident_buf->revision));
84852c9ce25SScott Long 		ata_btrim(ident_buf->serial, sizeof(ident_buf->serial));
84952c9ce25SScott Long 		ata_bpack(ident_buf->serial, ident_buf->serial, sizeof(ident_buf->serial));
8504ef08dc5SAlexander Motin 		/* Device may need spin-up before IDENTIFY become valid. */
8511254b680SAlexander Motin 		if ((ident_buf->specconf == 0x37c8 ||
8521254b680SAlexander Motin 		     ident_buf->specconf == 0x738c) &&
8531254b680SAlexander Motin 		    ((ident_buf->config & ATA_RESP_INCOMPLETE) ||
8544ef08dc5SAlexander Motin 		     softc->spinup == 0)) {
8554ef08dc5SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_SPINUP);
8564ef08dc5SAlexander Motin 			xpt_release_ccb(done_ccb);
8574ef08dc5SAlexander Motin 			xpt_schedule(periph, priority);
8584ef08dc5SAlexander Motin 			return;
8594ef08dc5SAlexander Motin 		}
860a9b8edb1SAlexander Motin 		ident_buf = &path->device->ident_data;
86152c9ce25SScott Long 		if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
86252c9ce25SScott Long 			/* Check that it is the same device. */
863a9b8edb1SAlexander Motin 			if (bcmp(softc->ident_data.model, ident_buf->model,
864a9b8edb1SAlexander Motin 			     sizeof(ident_buf->model)) ||
865a9b8edb1SAlexander Motin 			    bcmp(softc->ident_data.revision, ident_buf->revision,
866a9b8edb1SAlexander Motin 			     sizeof(ident_buf->revision)) ||
867a9b8edb1SAlexander Motin 			    bcmp(softc->ident_data.serial, ident_buf->serial,
868a9b8edb1SAlexander Motin 			     sizeof(ident_buf->serial))) {
86952c9ce25SScott Long 				/* Device changed. */
87052c9ce25SScott Long 				xpt_async(AC_LOST_DEVICE, path, NULL);
8711e637ba6SAlexander Motin 			} else {
872a9b8edb1SAlexander Motin 				bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
8732eb4a8dcSAlexander Motin 				changed = 0;
8742eb4a8dcSAlexander Motin 			}
8752eb4a8dcSAlexander Motin 		}
8762eb4a8dcSAlexander Motin 		if (changed) {
8772eb4a8dcSAlexander Motin 			bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
87852c9ce25SScott Long 			/* Clean up from previous instance of this device */
87952c9ce25SScott Long 			if (path->device->serial_num != NULL) {
88052c9ce25SScott Long 				free(path->device->serial_num, M_CAMXPT);
88152c9ce25SScott Long 				path->device->serial_num = NULL;
88252c9ce25SScott Long 				path->device->serial_num_len = 0;
88352c9ce25SScott Long 			}
88452c9ce25SScott Long 			path->device->serial_num =
88552c9ce25SScott Long 				(u_int8_t *)malloc((sizeof(ident_buf->serial) + 1),
88652c9ce25SScott Long 					   M_CAMXPT, M_NOWAIT);
88752c9ce25SScott Long 			if (path->device->serial_num != NULL) {
88852c9ce25SScott Long 				bcopy(ident_buf->serial,
88952c9ce25SScott Long 				      path->device->serial_num,
89052c9ce25SScott Long 				      sizeof(ident_buf->serial));
89152c9ce25SScott Long 				path->device->serial_num[sizeof(ident_buf->serial)]
89252c9ce25SScott Long 				    = '\0';
89352c9ce25SScott Long 				path->device->serial_num_len =
89452c9ce25SScott Long 				    strlen(path->device->serial_num);
89552c9ce25SScott Long 			}
89652c9ce25SScott Long 
8974b997c49SAlexander Motin 			path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
8981e637ba6SAlexander Motin 		}
89930a4094fSAlexander Motin 		if (ident_buf->satacapabilities & ATA_SUPPORT_NCQ) {
90030a4094fSAlexander Motin 			path->device->mintags = path->device->maxtags =
90130a4094fSAlexander Motin 			    ATA_QUEUE_LEN(ident_buf->queue) + 1;
90230a4094fSAlexander Motin 		}
90330a4094fSAlexander Motin 		ata_find_quirk(path->device);
904507e5811SAlexander Motin 		if (path->device->mintags != 0 &&
905507e5811SAlexander Motin 		    path->bus->sim->max_tagged_dev_openings != 0) {
906699f853bSAlexander Motin 			/* Check if the SIM does not want queued commands. */
907699f853bSAlexander Motin 			bzero(&cpi, sizeof(cpi));
908699f853bSAlexander Motin 			xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
909699f853bSAlexander Motin 			cpi.ccb_h.func_code = XPT_PATH_INQ;
910699f853bSAlexander Motin 			xpt_action((union ccb *)&cpi);
911699f853bSAlexander Motin 			if (cpi.ccb_h.status == CAM_REQ_CMP &&
912699f853bSAlexander Motin 			    (cpi.hba_inquiry & PI_TAG_ABLE)) {
913c8039fc6SAlexander Motin 				/* Report SIM which tags are allowed. */
914c8039fc6SAlexander Motin 				bzero(&cts, sizeof(cts));
91583c5d981SAlexander Motin 				xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
916c8039fc6SAlexander Motin 				cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
917c8039fc6SAlexander Motin 				cts.type = CTS_TYPE_CURRENT_SETTINGS;
918c8039fc6SAlexander Motin 				cts.xport_specific.sata.tags = path->device->maxtags;
919c8039fc6SAlexander Motin 				cts.xport_specific.sata.valid = CTS_SATA_VALID_TAGS;
920c8039fc6SAlexander Motin 				xpt_action((union ccb *)&cts);
921c8039fc6SAlexander Motin 				/* Reconfigure queues for tagged queueing. */
92230a4094fSAlexander Motin 				xpt_start_tags(path);
92330a4094fSAlexander Motin 			}
924699f853bSAlexander Motin 		}
92552c9ce25SScott Long 		ata_device_transport(path);
92652c9ce25SScott Long 		PROBE_SET_ACTION(softc, PROBE_SETMODE);
92752c9ce25SScott Long 		xpt_release_ccb(done_ccb);
92852c9ce25SScott Long 		xpt_schedule(periph, priority);
92952c9ce25SScott Long 		return;
93052c9ce25SScott Long 	}
9314ef08dc5SAlexander Motin 	case PROBE_SPINUP:
9324ef08dc5SAlexander Motin 		if (bootverbose)
9334ef08dc5SAlexander Motin 			xpt_print(path, "Spin-up done\n");
9344ef08dc5SAlexander Motin 		softc->spinup = 1;
9354ef08dc5SAlexander Motin 		PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
9364ef08dc5SAlexander Motin 		xpt_release_ccb(done_ccb);
9374ef08dc5SAlexander Motin 		xpt_schedule(periph, priority);
9384ef08dc5SAlexander Motin 		return;
93952c9ce25SScott Long 	case PROBE_SETMODE:
940da6808c1SAlexander Motin 		if (path->device->transport != XPORT_SATA)
941da6808c1SAlexander Motin 			goto notsata;
942da6808c1SAlexander Motin 		/* Set supported bits. */
943da6808c1SAlexander Motin 		bzero(&cts, sizeof(cts));
944da6808c1SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
945da6808c1SAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
946da6808c1SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
947da6808c1SAlexander Motin 		xpt_action((union ccb *)&cts);
948da6808c1SAlexander Motin 		if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
949da6808c1SAlexander Motin 			caps = cts.xport_specific.sata.caps & CTS_SATA_CAPS_H;
950da6808c1SAlexander Motin 		else
951da6808c1SAlexander Motin 			caps = 0;
952da6808c1SAlexander Motin 		if (ident_buf->satacapabilities != 0xffff) {
953da6808c1SAlexander Motin 			if (ident_buf->satacapabilities & ATA_SUPPORT_IFPWRMNGTRCV)
954da6808c1SAlexander Motin 				caps |= CTS_SATA_CAPS_D_PMREQ;
955da6808c1SAlexander Motin 			if (ident_buf->satacapabilities & ATA_SUPPORT_HAPST)
956da6808c1SAlexander Motin 				caps |= CTS_SATA_CAPS_D_APST;
957da6808c1SAlexander Motin 		}
958da6808c1SAlexander Motin 		/* Mask unwanted bits. */
959da6808c1SAlexander Motin 		bzero(&cts, sizeof(cts));
960da6808c1SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
961da6808c1SAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
962da6808c1SAlexander Motin 		cts.type = CTS_TYPE_USER_SETTINGS;
963da6808c1SAlexander Motin 		xpt_action((union ccb *)&cts);
964da6808c1SAlexander Motin 		if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
965da6808c1SAlexander Motin 			caps &= cts.xport_specific.sata.caps;
966c2b82f3eSAlexander Motin 		else
967c2b82f3eSAlexander Motin 			caps = 0;
968da6808c1SAlexander Motin 		/* Store result to SIM. */
969da6808c1SAlexander Motin 		bzero(&cts, sizeof(cts));
970da6808c1SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
971da6808c1SAlexander Motin 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
972da6808c1SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
973da6808c1SAlexander Motin 		cts.xport_specific.sata.caps = caps;
974da6808c1SAlexander Motin 		cts.xport_specific.sata.valid = CTS_SATA_VALID_CAPS;
975da6808c1SAlexander Motin 		xpt_action((union ccb *)&cts);
976da6808c1SAlexander Motin 		softc->caps = caps;
977da6808c1SAlexander Motin 		if (ident_buf->satasupport & ATA_SUPPORT_IFPWRMNGT) {
978da6808c1SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_SETPM);
979da6808c1SAlexander Motin 			xpt_release_ccb(done_ccb);
980da6808c1SAlexander Motin 			xpt_schedule(periph, priority);
981da6808c1SAlexander Motin 			return;
982da6808c1SAlexander Motin 		}
983da6808c1SAlexander Motin 		/* FALLTHROUGH */
984da6808c1SAlexander Motin 	case PROBE_SETPM:
985da6808c1SAlexander Motin 		if (ident_buf->satacapabilities != 0xffff &&
986da6808c1SAlexander Motin 		    ident_buf->satacapabilities & ATA_SUPPORT_DAPST) {
987da6808c1SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_SETAPST);
988da6808c1SAlexander Motin 			xpt_release_ccb(done_ccb);
989da6808c1SAlexander Motin 			xpt_schedule(periph, priority);
990da6808c1SAlexander Motin 			return;
991da6808c1SAlexander Motin 		}
992da6808c1SAlexander Motin 		/* FALLTHROUGH */
993da6808c1SAlexander Motin 	case PROBE_SETAPST:
994da6808c1SAlexander Motin 		if (ident_buf->satasupport & ATA_SUPPORT_AUTOACTIVATE) {
995da6808c1SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_SETDMAAA);
996da6808c1SAlexander Motin 			xpt_release_ccb(done_ccb);
997da6808c1SAlexander Motin 			xpt_schedule(periph, priority);
998da6808c1SAlexander Motin 			return;
999da6808c1SAlexander Motin 		}
1000da6808c1SAlexander Motin 		/* FALLTHROUGH */
1001da6808c1SAlexander Motin 	case PROBE_SETDMAAA:
1002da6808c1SAlexander Motin notsata:
10031e637ba6SAlexander Motin 		if (path->device->protocol == PROTO_ATA) {
10041e637ba6SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_SET_MULTI);
10051e637ba6SAlexander Motin 		} else {
10061e637ba6SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_INQUIRY);
10071e637ba6SAlexander Motin 		}
10081e637ba6SAlexander Motin 		xpt_release_ccb(done_ccb);
10091e637ba6SAlexander Motin 		xpt_schedule(periph, priority);
10101e637ba6SAlexander Motin 		return;
10111e637ba6SAlexander Motin 	case PROBE_SET_MULTI:
10121e637ba6SAlexander Motin 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
101352c9ce25SScott Long 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1014f98d7a47SAlexander Motin 			xpt_acquire_device(path->device);
101552c9ce25SScott Long 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
101652c9ce25SScott Long 			xpt_action(done_ccb);
101752c9ce25SScott Long 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
101852c9ce25SScott Long 			    done_ccb);
10191e637ba6SAlexander Motin 		}
102052c9ce25SScott Long 		break;
102152c9ce25SScott Long 	case PROBE_INQUIRY:
102252c9ce25SScott Long 	case PROBE_FULL_INQUIRY:
102352c9ce25SScott Long 	{
102452c9ce25SScott Long 		struct scsi_inquiry_data *inq_buf;
10251e637ba6SAlexander Motin 		u_int8_t periph_qual, len;
102652c9ce25SScott Long 
102752c9ce25SScott Long 		path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
102852c9ce25SScott Long 		inq_buf = &path->device->inq_data;
102952c9ce25SScott Long 
103052c9ce25SScott Long 		periph_qual = SID_QUAL(inq_buf);
103152c9ce25SScott Long 
10321e637ba6SAlexander Motin 		if (periph_qual != SID_QUAL_LU_CONNECTED)
10331e637ba6SAlexander Motin 			break;
103452c9ce25SScott Long 
103552c9ce25SScott Long 		/*
103652c9ce25SScott Long 		 * We conservatively request only
103752c9ce25SScott Long 		 * SHORT_INQUIRY_LEN bytes of inquiry
103852c9ce25SScott Long 		 * information during our first try
103952c9ce25SScott Long 		 * at sending an INQUIRY. If the device
104052c9ce25SScott Long 		 * has more information to give,
104152c9ce25SScott Long 		 * perform a second request specifying
104252c9ce25SScott Long 		 * the amount of information the device
104352c9ce25SScott Long 		 * is willing to give.
104452c9ce25SScott Long 		 */
104552c9ce25SScott Long 		len = inq_buf->additional_length
10461e637ba6SAlexander Motin 		    + offsetof(struct scsi_inquiry_data, additional_length) + 1;
104752c9ce25SScott Long 		if (softc->action == PROBE_INQUIRY
104852c9ce25SScott Long 		    && len > SHORT_INQUIRY_LENGTH) {
104952c9ce25SScott Long 			PROBE_SET_ACTION(softc, PROBE_FULL_INQUIRY);
105052c9ce25SScott Long 			xpt_release_ccb(done_ccb);
105152c9ce25SScott Long 			xpt_schedule(periph, priority);
105252c9ce25SScott Long 			return;
105352c9ce25SScott Long 		}
105452c9ce25SScott Long 
10554b997c49SAlexander Motin 		ata_device_transport(path);
10561e637ba6SAlexander Motin 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
105752c9ce25SScott Long 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1058f98d7a47SAlexander Motin 			xpt_acquire_device(path->device);
105952c9ce25SScott Long 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
106052c9ce25SScott Long 			xpt_action(done_ccb);
10611e637ba6SAlexander Motin 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path, done_ccb);
10621e637ba6SAlexander Motin 		}
106352c9ce25SScott Long 		break;
106452c9ce25SScott Long 	}
106552c9ce25SScott Long 	case PROBE_PM_PID:
10664b997c49SAlexander Motin 		if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) == 0)
106752c9ce25SScott Long 			bzero(ident_buf, sizeof(*ident_buf));
106852c9ce25SScott Long 		softc->pm_pid = (done_ccb->ataio.res.lba_high << 24) +
106952c9ce25SScott Long 		    (done_ccb->ataio.res.lba_mid << 16) +
107052c9ce25SScott Long 		    (done_ccb->ataio.res.lba_low << 8) +
107152c9ce25SScott Long 		    done_ccb->ataio.res.sector_count;
107265d0fb03SAlexander Motin 		((uint32_t *)ident_buf)[0] = softc->pm_pid;
107352c9ce25SScott Long 		snprintf(ident_buf->model, sizeof(ident_buf->model),
107452c9ce25SScott Long 		    "Port Multiplier %08x", softc->pm_pid);
107552c9ce25SScott Long 		PROBE_SET_ACTION(softc, PROBE_PM_PRV);
107652c9ce25SScott Long 		xpt_release_ccb(done_ccb);
107752c9ce25SScott Long 		xpt_schedule(periph, priority);
107852c9ce25SScott Long 		return;
107952c9ce25SScott Long 	case PROBE_PM_PRV:
108052c9ce25SScott Long 		softc->pm_prv = (done_ccb->ataio.res.lba_high << 24) +
108152c9ce25SScott Long 		    (done_ccb->ataio.res.lba_mid << 16) +
108252c9ce25SScott Long 		    (done_ccb->ataio.res.lba_low << 8) +
108352c9ce25SScott Long 		    done_ccb->ataio.res.sector_count;
108465d0fb03SAlexander Motin 		((uint32_t *)ident_buf)[1] = softc->pm_prv;
108552c9ce25SScott Long 		snprintf(ident_buf->revision, sizeof(ident_buf->revision),
108652c9ce25SScott Long 		    "%04x", softc->pm_prv);
10874b997c49SAlexander Motin 		path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
1088da6808c1SAlexander Motin 		/* Set supported bits. */
1089da6808c1SAlexander Motin 		bzero(&cts, sizeof(cts));
1090da6808c1SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1091da6808c1SAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1092da6808c1SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
1093da6808c1SAlexander Motin 		xpt_action((union ccb *)&cts);
1094da6808c1SAlexander Motin 		if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1095da6808c1SAlexander Motin 			caps = cts.xport_specific.sata.caps & CTS_SATA_CAPS_H;
1096da6808c1SAlexander Motin 		else
1097da6808c1SAlexander Motin 			caps = 0;
1098da6808c1SAlexander Motin 		/* All PMPs must support PM requests. */
1099da6808c1SAlexander Motin 		caps |= CTS_SATA_CAPS_D_PMREQ;
1100da6808c1SAlexander Motin 		/* Mask unwanted bits. */
1101da6808c1SAlexander Motin 		bzero(&cts, sizeof(cts));
1102da6808c1SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1103da6808c1SAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1104da6808c1SAlexander Motin 		cts.type = CTS_TYPE_USER_SETTINGS;
1105da6808c1SAlexander Motin 		xpt_action((union ccb *)&cts);
1106da6808c1SAlexander Motin 		if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1107da6808c1SAlexander Motin 			caps &= cts.xport_specific.sata.caps;
1108c2b82f3eSAlexander Motin 		else
1109c2b82f3eSAlexander Motin 			caps = 0;
1110da6808c1SAlexander Motin 		/* Store result to SIM. */
1111da6808c1SAlexander Motin 		bzero(&cts, sizeof(cts));
1112da6808c1SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1113da6808c1SAlexander Motin 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1114da6808c1SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
1115da6808c1SAlexander Motin 		cts.xport_specific.sata.caps = caps;
1116da6808c1SAlexander Motin 		cts.xport_specific.sata.valid = CTS_SATA_VALID_CAPS;
1117da6808c1SAlexander Motin 		xpt_action((union ccb *)&cts);
1118da6808c1SAlexander Motin 		softc->caps = caps;
111965d0fb03SAlexander Motin 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
112052c9ce25SScott Long 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1121f98d7a47SAlexander Motin 			xpt_acquire_device(path->device);
112252c9ce25SScott Long 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
112352c9ce25SScott Long 			xpt_action(done_ccb);
112452c9ce25SScott Long 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
112552c9ce25SScott Long 			    done_ccb);
112665d0fb03SAlexander Motin 		} else {
112765d0fb03SAlexander Motin 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
112865d0fb03SAlexander Motin 			xpt_action(done_ccb);
11291e637ba6SAlexander Motin 			xpt_async(AC_SCSI_AEN, done_ccb->ccb_h.path, done_ccb);
113052c9ce25SScott Long 		}
113152c9ce25SScott Long 		break;
113252c9ce25SScott Long 	case PROBE_INVALID:
113352c9ce25SScott Long 		CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_INFO,
113452c9ce25SScott Long 		    ("probedone: invalid action state\n"));
113552c9ce25SScott Long 	default:
113652c9ce25SScott Long 		break;
113752c9ce25SScott Long 	}
11381e637ba6SAlexander Motin done:
113983c5d981SAlexander Motin 	if (softc->restart) {
114083c5d981SAlexander Motin 		softc->restart = 0;
11411e637ba6SAlexander Motin 		xpt_release_ccb(done_ccb);
114283c5d981SAlexander Motin 		probeschedule(periph);
114383c5d981SAlexander Motin 		return;
114483c5d981SAlexander Motin 	}
114583c5d981SAlexander Motin 	xpt_release_ccb(done_ccb);
114683c5d981SAlexander Motin 	while ((done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs))) {
114783c5d981SAlexander Motin 		TAILQ_REMOVE(&softc->request_ccbs,
114883c5d981SAlexander Motin 		    &done_ccb->ccb_h, periph_links.tqe);
114983c5d981SAlexander Motin 		done_ccb->ccb_h.status = found ? CAM_REQ_CMP : CAM_REQ_CMP_ERR;
115052c9ce25SScott Long 		xpt_done(done_ccb);
115183c5d981SAlexander Motin 	}
115283c5d981SAlexander Motin 	cam_release_devq(periph->path,
115383c5d981SAlexander Motin 	    RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_XPT + 1, FALSE);
115452c9ce25SScott Long 	cam_periph_invalidate(periph);
115552c9ce25SScott Long 	cam_periph_release_locked(periph);
115652c9ce25SScott Long }
115752c9ce25SScott Long 
115852c9ce25SScott Long static void
115952c9ce25SScott Long probecleanup(struct cam_periph *periph)
116052c9ce25SScott Long {
116152c9ce25SScott Long 	free(periph->softc, M_CAMXPT);
116252c9ce25SScott Long }
116352c9ce25SScott Long 
116452c9ce25SScott Long static void
116530a4094fSAlexander Motin ata_find_quirk(struct cam_ed *device)
116652c9ce25SScott Long {
116730a4094fSAlexander Motin 	struct ata_quirk_entry *quirk;
116852c9ce25SScott Long 	caddr_t	match;
116952c9ce25SScott Long 
117030a4094fSAlexander Motin 	match = cam_quirkmatch((caddr_t)&device->ident_data,
117130a4094fSAlexander Motin 			       (caddr_t)ata_quirk_table,
117230a4094fSAlexander Motin 			       ata_quirk_table_size,
117330a4094fSAlexander Motin 			       sizeof(*ata_quirk_table), ata_identify_match);
117452c9ce25SScott Long 
117552c9ce25SScott Long 	if (match == NULL)
117652c9ce25SScott Long 		panic("xpt_find_quirk: device didn't match wildcard entry!!");
117752c9ce25SScott Long 
117830a4094fSAlexander Motin 	quirk = (struct ata_quirk_entry *)match;
117952c9ce25SScott Long 	device->quirk = quirk;
118030a4094fSAlexander Motin 	if (quirk->quirks & CAM_QUIRK_MAXTAGS)
118130a4094fSAlexander Motin 		device->mintags = device->maxtags = quirk->maxtags;
118252c9ce25SScott Long }
118352c9ce25SScott Long 
118452c9ce25SScott Long typedef struct {
118552c9ce25SScott Long 	union	ccb *request_ccb;
118652c9ce25SScott Long 	struct 	ccb_pathinq *cpi;
118752c9ce25SScott Long 	int	counter;
118852c9ce25SScott Long } ata_scan_bus_info;
118952c9ce25SScott Long 
119052c9ce25SScott Long /*
119152c9ce25SScott Long  * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
119252c9ce25SScott Long  * As the scan progresses, xpt_scan_bus is used as the
119352c9ce25SScott Long  * callback on completion function.
119452c9ce25SScott Long  */
119552c9ce25SScott Long static void
119652c9ce25SScott Long ata_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
119752c9ce25SScott Long {
119852c9ce25SScott Long 	struct	cam_path *path;
119952c9ce25SScott Long 	ata_scan_bus_info *scan_info;
120083c5d981SAlexander Motin 	union	ccb *work_ccb, *reset_ccb;
120152c9ce25SScott Long 	cam_status status;
120252c9ce25SScott Long 
120352c9ce25SScott Long 	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
120452c9ce25SScott Long 		  ("xpt_scan_bus\n"));
120552c9ce25SScott Long 	switch (request_ccb->ccb_h.func_code) {
120652c9ce25SScott Long 	case XPT_SCAN_BUS:
12070e85f214SMatt Jacob 	case XPT_SCAN_TGT:
120852c9ce25SScott Long 		/* Find out the characteristics of the bus */
120952c9ce25SScott Long 		work_ccb = xpt_alloc_ccb_nowait();
121052c9ce25SScott Long 		if (work_ccb == NULL) {
121152c9ce25SScott Long 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
121252c9ce25SScott Long 			xpt_done(request_ccb);
121352c9ce25SScott Long 			return;
121452c9ce25SScott Long 		}
121552c9ce25SScott Long 		xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
121652c9ce25SScott Long 			      request_ccb->ccb_h.pinfo.priority);
121752c9ce25SScott Long 		work_ccb->ccb_h.func_code = XPT_PATH_INQ;
121852c9ce25SScott Long 		xpt_action(work_ccb);
121952c9ce25SScott Long 		if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
122052c9ce25SScott Long 			request_ccb->ccb_h.status = work_ccb->ccb_h.status;
122152c9ce25SScott Long 			xpt_free_ccb(work_ccb);
122252c9ce25SScott Long 			xpt_done(request_ccb);
122352c9ce25SScott Long 			return;
122452c9ce25SScott Long 		}
122552c9ce25SScott Long 
122683c5d981SAlexander Motin 		/* We may need to reset bus first, if we haven't done it yet. */
122783c5d981SAlexander Motin 		if ((work_ccb->cpi.hba_inquiry &
122883c5d981SAlexander Motin 		    (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) &&
122983c5d981SAlexander Motin 		    !(work_ccb->cpi.hba_misc & PIM_NOBUSRESET) &&
123083c5d981SAlexander Motin 		    !timevalisset(&request_ccb->ccb_h.path->bus->last_reset)) {
123183c5d981SAlexander Motin 			reset_ccb = xpt_alloc_ccb_nowait();
1232f1893540SAlexander Motin 			if (reset_ccb == NULL) {
1233f1893540SAlexander Motin 				request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1234f1893540SAlexander Motin 				xpt_free_ccb(work_ccb);
1235f1893540SAlexander Motin 				xpt_done(request_ccb);
1236f1893540SAlexander Motin 				return;
1237f1893540SAlexander Motin 			}
123883c5d981SAlexander Motin 			xpt_setup_ccb(&reset_ccb->ccb_h, request_ccb->ccb_h.path,
123983c5d981SAlexander Motin 			      CAM_PRIORITY_NONE);
124083c5d981SAlexander Motin 			reset_ccb->ccb_h.func_code = XPT_RESET_BUS;
124183c5d981SAlexander Motin 			xpt_action(reset_ccb);
124283c5d981SAlexander Motin 			if (reset_ccb->ccb_h.status != CAM_REQ_CMP) {
124383c5d981SAlexander Motin 				request_ccb->ccb_h.status = reset_ccb->ccb_h.status;
124483c5d981SAlexander Motin 				xpt_free_ccb(reset_ccb);
124583c5d981SAlexander Motin 				xpt_free_ccb(work_ccb);
124683c5d981SAlexander Motin 				xpt_done(request_ccb);
124783c5d981SAlexander Motin 				return;
124883c5d981SAlexander Motin 			}
124983c5d981SAlexander Motin 			xpt_free_ccb(reset_ccb);
125083c5d981SAlexander Motin 		}
125183c5d981SAlexander Motin 
125252c9ce25SScott Long 		/* Save some state for use while we probe for devices */
125352c9ce25SScott Long 		scan_info = (ata_scan_bus_info *)
125452c9ce25SScott Long 		    malloc(sizeof(ata_scan_bus_info), M_CAMXPT, M_NOWAIT);
125552c9ce25SScott Long 		if (scan_info == NULL) {
125652c9ce25SScott Long 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1257f1893540SAlexander Motin 			xpt_free_ccb(work_ccb);
125852c9ce25SScott Long 			xpt_done(request_ccb);
125952c9ce25SScott Long 			return;
126052c9ce25SScott Long 		}
126152c9ce25SScott Long 		scan_info->request_ccb = request_ccb;
126252c9ce25SScott Long 		scan_info->cpi = &work_ccb->cpi;
126352c9ce25SScott Long 		/* If PM supported, probe it first. */
126452c9ce25SScott Long 		if (scan_info->cpi->hba_inquiry & PI_SATAPM)
12650025eb12SAlexander Motin 			scan_info->counter = scan_info->cpi->max_target;
12660025eb12SAlexander Motin 		else
12670025eb12SAlexander Motin 			scan_info->counter = 0;
126852c9ce25SScott Long 
126952c9ce25SScott Long 		work_ccb = xpt_alloc_ccb_nowait();
127052c9ce25SScott Long 		if (work_ccb == NULL) {
127152c9ce25SScott Long 			free(scan_info, M_CAMXPT);
127252c9ce25SScott Long 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
127352c9ce25SScott Long 			xpt_done(request_ccb);
127452c9ce25SScott Long 			break;
127552c9ce25SScott Long 		}
127652c9ce25SScott Long 		goto scan_next;
127752c9ce25SScott Long 	case XPT_SCAN_LUN:
127852c9ce25SScott Long 		work_ccb = request_ccb;
127952c9ce25SScott Long 		/* Reuse the same CCB to query if a device was really found */
128052c9ce25SScott Long 		scan_info = (ata_scan_bus_info *)work_ccb->ccb_h.ppriv_ptr0;
128165d0fb03SAlexander Motin 		/* If there is PMP... */
12820025eb12SAlexander Motin 		if ((scan_info->cpi->hba_inquiry & PI_SATAPM) &&
12830025eb12SAlexander Motin 		    (scan_info->counter == scan_info->cpi->max_target)) {
128483c5d981SAlexander Motin 			if (work_ccb->ccb_h.status == CAM_REQ_CMP) {
128565d0fb03SAlexander Motin 				/* everything else will be probed by it */
1286e2a75189SAlexander Motin 				/* Free the current request path- we're done with it. */
1287e2a75189SAlexander Motin 				xpt_free_path(work_ccb->ccb_h.path);
12880025eb12SAlexander Motin 				goto done;
128952c9ce25SScott Long 			} else {
129052c9ce25SScott Long 				struct ccb_trans_settings cts;
129152c9ce25SScott Long 
129252c9ce25SScott Long 				/* Report SIM that PM is absent. */
129352c9ce25SScott Long 				bzero(&cts, sizeof(cts));
129452c9ce25SScott Long 				xpt_setup_ccb(&cts.ccb_h,
1295e2a75189SAlexander Motin 				    work_ccb->ccb_h.path, CAM_PRIORITY_NONE);
129652c9ce25SScott Long 				cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
129752c9ce25SScott Long 				cts.type = CTS_TYPE_CURRENT_SETTINGS;
12983ccda2f3SAlexander Motin 				cts.xport_specific.sata.pm_present = 0;
129952c9ce25SScott Long 				cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
130052c9ce25SScott Long 				xpt_action((union ccb *)&cts);
130152c9ce25SScott Long 			}
130252c9ce25SScott Long 		}
1303e2a75189SAlexander Motin 		/* Free the current request path- we're done with it. */
1304e2a75189SAlexander Motin 		xpt_free_path(work_ccb->ccb_h.path);
13050025eb12SAlexander Motin 		if (scan_info->counter ==
13060025eb12SAlexander Motin 		    ((scan_info->cpi->hba_inquiry & PI_SATAPM) ?
13070025eb12SAlexander Motin 		    0 : scan_info->cpi->max_target)) {
13080025eb12SAlexander Motin done:
130952c9ce25SScott Long 			xpt_free_ccb(work_ccb);
131052c9ce25SScott Long 			xpt_free_ccb((union ccb *)scan_info->cpi);
131152c9ce25SScott Long 			request_ccb = scan_info->request_ccb;
131252c9ce25SScott Long 			free(scan_info, M_CAMXPT);
131352c9ce25SScott Long 			request_ccb->ccb_h.status = CAM_REQ_CMP;
131452c9ce25SScott Long 			xpt_done(request_ccb);
131552c9ce25SScott Long 			break;
131652c9ce25SScott Long 		}
13170025eb12SAlexander Motin 		/* Take next device. Wrap from max (PMP) to 0. */
13180025eb12SAlexander Motin 		scan_info->counter = (scan_info->counter + 1 ) %
13190025eb12SAlexander Motin 		    (scan_info->cpi->max_target + 1);
132052c9ce25SScott Long scan_next:
132152c9ce25SScott Long 		status = xpt_create_path(&path, xpt_periph,
132252c9ce25SScott Long 		    scan_info->request_ccb->ccb_h.path_id,
132352c9ce25SScott Long 		    scan_info->counter, 0);
132452c9ce25SScott Long 		if (status != CAM_REQ_CMP) {
132552c9ce25SScott Long 			printf("xpt_scan_bus: xpt_create_path failed"
132652c9ce25SScott Long 			    " with status %#x, bus scan halted\n",
132752c9ce25SScott Long 			    status);
132852c9ce25SScott Long 			xpt_free_ccb(work_ccb);
132952c9ce25SScott Long 			xpt_free_ccb((union ccb *)scan_info->cpi);
133052c9ce25SScott Long 			request_ccb = scan_info->request_ccb;
133152c9ce25SScott Long 			free(scan_info, M_CAMXPT);
133252c9ce25SScott Long 			request_ccb->ccb_h.status = status;
133352c9ce25SScott Long 			xpt_done(request_ccb);
133452c9ce25SScott Long 			break;
133552c9ce25SScott Long 		}
133652c9ce25SScott Long 		xpt_setup_ccb(&work_ccb->ccb_h, path,
133752c9ce25SScott Long 		    scan_info->request_ccb->ccb_h.pinfo.priority);
133852c9ce25SScott Long 		work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
133952c9ce25SScott Long 		work_ccb->ccb_h.cbfcnp = ata_scan_bus;
134052c9ce25SScott Long 		work_ccb->ccb_h.ppriv_ptr0 = scan_info;
134152c9ce25SScott Long 		work_ccb->crcn.flags = scan_info->request_ccb->crcn.flags;
134252c9ce25SScott Long 		xpt_action(work_ccb);
134352c9ce25SScott Long 		break;
134452c9ce25SScott Long 	default:
134552c9ce25SScott Long 		break;
134652c9ce25SScott Long 	}
134752c9ce25SScott Long }
134852c9ce25SScott Long 
134952c9ce25SScott Long static void
135052c9ce25SScott Long ata_scan_lun(struct cam_periph *periph, struct cam_path *path,
135152c9ce25SScott Long 	     cam_flags flags, union ccb *request_ccb)
135252c9ce25SScott Long {
135352c9ce25SScott Long 	struct ccb_pathinq cpi;
135452c9ce25SScott Long 	cam_status status;
135552c9ce25SScott Long 	struct cam_path *new_path;
135652c9ce25SScott Long 	struct cam_periph *old_periph;
135752c9ce25SScott Long 
135883c5d981SAlexander Motin 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_scan_lun\n"));
135952c9ce25SScott Long 
136083c5d981SAlexander Motin 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
136152c9ce25SScott Long 	cpi.ccb_h.func_code = XPT_PATH_INQ;
136252c9ce25SScott Long 	xpt_action((union ccb *)&cpi);
136352c9ce25SScott Long 
136452c9ce25SScott Long 	if (cpi.ccb_h.status != CAM_REQ_CMP) {
136552c9ce25SScott Long 		if (request_ccb != NULL) {
136652c9ce25SScott Long 			request_ccb->ccb_h.status = cpi.ccb_h.status;
136752c9ce25SScott Long 			xpt_done(request_ccb);
136852c9ce25SScott Long 		}
136952c9ce25SScott Long 		return;
137052c9ce25SScott Long 	}
137152c9ce25SScott Long 
137252c9ce25SScott Long 	if (request_ccb == NULL) {
137352c9ce25SScott Long 		request_ccb = malloc(sizeof(union ccb), M_CAMXPT, M_NOWAIT);
137452c9ce25SScott Long 		if (request_ccb == NULL) {
137552c9ce25SScott Long 			xpt_print(path, "xpt_scan_lun: can't allocate CCB, "
137652c9ce25SScott Long 			    "can't continue\n");
137752c9ce25SScott Long 			return;
137852c9ce25SScott Long 		}
137952c9ce25SScott Long 		new_path = malloc(sizeof(*new_path), M_CAMXPT, M_NOWAIT);
138052c9ce25SScott Long 		if (new_path == NULL) {
138152c9ce25SScott Long 			xpt_print(path, "xpt_scan_lun: can't allocate path, "
138252c9ce25SScott Long 			    "can't continue\n");
138352c9ce25SScott Long 			free(request_ccb, M_CAMXPT);
138452c9ce25SScott Long 			return;
138552c9ce25SScott Long 		}
138652c9ce25SScott Long 		status = xpt_compile_path(new_path, xpt_periph,
138752c9ce25SScott Long 					  path->bus->path_id,
138852c9ce25SScott Long 					  path->target->target_id,
138952c9ce25SScott Long 					  path->device->lun_id);
139052c9ce25SScott Long 
139152c9ce25SScott Long 		if (status != CAM_REQ_CMP) {
139252c9ce25SScott Long 			xpt_print(path, "xpt_scan_lun: can't compile path, "
139352c9ce25SScott Long 			    "can't continue\n");
139452c9ce25SScott Long 			free(request_ccb, M_CAMXPT);
139552c9ce25SScott Long 			free(new_path, M_CAMXPT);
139652c9ce25SScott Long 			return;
139752c9ce25SScott Long 		}
139883c5d981SAlexander Motin 		xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_XPT);
139952c9ce25SScott Long 		request_ccb->ccb_h.cbfcnp = xptscandone;
140052c9ce25SScott Long 		request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
140152c9ce25SScott Long 		request_ccb->crcn.flags = flags;
140252c9ce25SScott Long 	}
140352c9ce25SScott Long 
1404f09f8e3eSAlexander Motin 	if ((old_periph = cam_periph_find(path, "aprobe")) != NULL) {
140552c9ce25SScott Long 		probe_softc *softc;
140652c9ce25SScott Long 
140752c9ce25SScott Long 		softc = (probe_softc *)old_periph->softc;
140852c9ce25SScott Long 		TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
140952c9ce25SScott Long 				  periph_links.tqe);
141083c5d981SAlexander Motin 		softc->restart = 1;
141152c9ce25SScott Long 	} else {
141252c9ce25SScott Long 		status = cam_periph_alloc(proberegister, NULL, probecleanup,
1413f09f8e3eSAlexander Motin 					  probestart, "aprobe",
141452c9ce25SScott Long 					  CAM_PERIPH_BIO,
141552c9ce25SScott Long 					  request_ccb->ccb_h.path, NULL, 0,
141652c9ce25SScott Long 					  request_ccb);
141752c9ce25SScott Long 
141852c9ce25SScott Long 		if (status != CAM_REQ_CMP) {
141952c9ce25SScott Long 			xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
142052c9ce25SScott Long 			    "returned an error, can't continue probe\n");
142152c9ce25SScott Long 			request_ccb->ccb_h.status = status;
142252c9ce25SScott Long 			xpt_done(request_ccb);
142352c9ce25SScott Long 		}
142452c9ce25SScott Long 	}
142552c9ce25SScott Long }
142652c9ce25SScott Long 
142752c9ce25SScott Long static void
142852c9ce25SScott Long xptscandone(struct cam_periph *periph, union ccb *done_ccb)
142952c9ce25SScott Long {
143052c9ce25SScott Long 	xpt_release_path(done_ccb->ccb_h.path);
143152c9ce25SScott Long 	free(done_ccb->ccb_h.path, M_CAMXPT);
143252c9ce25SScott Long 	free(done_ccb, M_CAMXPT);
143352c9ce25SScott Long }
143452c9ce25SScott Long 
143552c9ce25SScott Long static struct cam_ed *
143652c9ce25SScott Long ata_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
143752c9ce25SScott Long {
143852c9ce25SScott Long 	struct cam_path path;
143930a4094fSAlexander Motin 	struct ata_quirk_entry *quirk;
144052c9ce25SScott Long 	struct cam_ed *device;
144152c9ce25SScott Long 	struct cam_ed *cur_device;
144252c9ce25SScott Long 
144352c9ce25SScott Long 	device = xpt_alloc_device(bus, target, lun_id);
144452c9ce25SScott Long 	if (device == NULL)
144552c9ce25SScott Long 		return (NULL);
144652c9ce25SScott Long 
144752c9ce25SScott Long 	/*
144852c9ce25SScott Long 	 * Take the default quirk entry until we have inquiry
144952c9ce25SScott Long 	 * data and can determine a better quirk to use.
145052c9ce25SScott Long 	 */
145130a4094fSAlexander Motin 	quirk = &ata_quirk_table[ata_quirk_table_size - 1];
145252c9ce25SScott Long 	device->quirk = (void *)quirk;
145330a4094fSAlexander Motin 	device->mintags = 0;
145430a4094fSAlexander Motin 	device->maxtags = 0;
145552c9ce25SScott Long 	bzero(&device->inq_data, sizeof(device->inq_data));
145652c9ce25SScott Long 	device->inq_flags = 0;
145752c9ce25SScott Long 	device->queue_flags = 0;
145852c9ce25SScott Long 	device->serial_num = NULL;
145952c9ce25SScott Long 	device->serial_num_len = 0;
146052c9ce25SScott Long 
146152c9ce25SScott Long 	/*
146252c9ce25SScott Long 	 * XXX should be limited by number of CCBs this bus can
146352c9ce25SScott Long 	 * do.
146452c9ce25SScott Long 	 */
146552c9ce25SScott Long 	bus->sim->max_ccbs += device->ccbq.devq_openings;
146652c9ce25SScott Long 	/* Insertion sort into our target's device list */
146752c9ce25SScott Long 	cur_device = TAILQ_FIRST(&target->ed_entries);
146852c9ce25SScott Long 	while (cur_device != NULL && cur_device->lun_id < lun_id)
146952c9ce25SScott Long 		cur_device = TAILQ_NEXT(cur_device, links);
147052c9ce25SScott Long 	if (cur_device != NULL) {
147152c9ce25SScott Long 		TAILQ_INSERT_BEFORE(cur_device, device, links);
147252c9ce25SScott Long 	} else {
147352c9ce25SScott Long 		TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
147452c9ce25SScott Long 	}
147552c9ce25SScott Long 	target->generation++;
147652c9ce25SScott Long 	if (lun_id != CAM_LUN_WILDCARD) {
147752c9ce25SScott Long 		xpt_compile_path(&path,
147852c9ce25SScott Long 				 NULL,
147952c9ce25SScott Long 				 bus->path_id,
148052c9ce25SScott Long 				 target->target_id,
148152c9ce25SScott Long 				 lun_id);
148252c9ce25SScott Long 		ata_device_transport(&path);
148352c9ce25SScott Long 		xpt_release_path(&path);
148452c9ce25SScott Long 	}
148552c9ce25SScott Long 
148652c9ce25SScott Long 	return (device);
148752c9ce25SScott Long }
148852c9ce25SScott Long 
148952c9ce25SScott Long static void
149052c9ce25SScott Long ata_device_transport(struct cam_path *path)
149152c9ce25SScott Long {
149252c9ce25SScott Long 	struct ccb_pathinq cpi;
14934b997c49SAlexander Motin 	struct ccb_trans_settings cts;
14944b997c49SAlexander Motin 	struct scsi_inquiry_data *inq_buf = NULL;
14954b997c49SAlexander Motin 	struct ata_params *ident_buf = NULL;
149652c9ce25SScott Long 
149752c9ce25SScott Long 	/* Get transport information from the SIM */
149883c5d981SAlexander Motin 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
149952c9ce25SScott Long 	cpi.ccb_h.func_code = XPT_PATH_INQ;
150052c9ce25SScott Long 	xpt_action((union ccb *)&cpi);
150152c9ce25SScott Long 
150252c9ce25SScott Long 	path->device->transport = cpi.transport;
15034b997c49SAlexander Motin 	if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
15044b997c49SAlexander Motin 		inq_buf = &path->device->inq_data;
15054b997c49SAlexander Motin 	if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) != 0)
15064b997c49SAlexander Motin 		ident_buf = &path->device->ident_data;
15074b997c49SAlexander Motin 	if (path->device->protocol == PROTO_ATA) {
15084b997c49SAlexander Motin 		path->device->protocol_version = ident_buf ?
15094b997c49SAlexander Motin 		    ata_version(ident_buf->version_major) : cpi.protocol_version;
15104b997c49SAlexander Motin 	} else if (path->device->protocol == PROTO_SCSI) {
15114b997c49SAlexander Motin 		path->device->protocol_version = inq_buf ?
15124b997c49SAlexander Motin 		    SID_ANSI_REV(inq_buf) : cpi.protocol_version;
151352c9ce25SScott Long 	}
15144b997c49SAlexander Motin 	path->device->transport_version = ident_buf ?
15154b997c49SAlexander Motin 	    ata_version(ident_buf->version_major) : cpi.transport_version;
151652c9ce25SScott Long 
151752c9ce25SScott Long 	/* Tell the controller what we think */
151883c5d981SAlexander Motin 	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
151952c9ce25SScott Long 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
152052c9ce25SScott Long 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
152152c9ce25SScott Long 	cts.transport = path->device->transport;
152252c9ce25SScott Long 	cts.transport_version = path->device->transport_version;
152352c9ce25SScott Long 	cts.protocol = path->device->protocol;
152452c9ce25SScott Long 	cts.protocol_version = path->device->protocol_version;
152552c9ce25SScott Long 	cts.proto_specific.valid = 0;
15264cca1530SAlexander Motin 	if (ident_buf) {
15274cca1530SAlexander Motin 		if (path->device->transport == XPORT_ATA) {
15284cca1530SAlexander Motin 			cts.xport_specific.ata.atapi =
15294cca1530SAlexander Motin 			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 :
15304cca1530SAlexander Motin 			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0;
15314cca1530SAlexander Motin 			cts.xport_specific.ata.valid = CTS_ATA_VALID_ATAPI;
15324cca1530SAlexander Motin 		} else {
15334cca1530SAlexander Motin 			cts.xport_specific.sata.atapi =
15344cca1530SAlexander Motin 			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 :
15354cca1530SAlexander Motin 			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0;
15364cca1530SAlexander Motin 			cts.xport_specific.sata.valid = CTS_SATA_VALID_ATAPI;
15374cca1530SAlexander Motin 		}
15384cca1530SAlexander Motin 	} else
153952c9ce25SScott Long 		cts.xport_specific.valid = 0;
154052c9ce25SScott Long 	xpt_action((union ccb *)&cts);
154152c9ce25SScott Long }
154252c9ce25SScott Long 
154352c9ce25SScott Long static void
154452c9ce25SScott Long ata_action(union ccb *start_ccb)
154552c9ce25SScott Long {
154652c9ce25SScott Long 
154752c9ce25SScott Long 	switch (start_ccb->ccb_h.func_code) {
154852c9ce25SScott Long 	case XPT_SET_TRAN_SETTINGS:
154952c9ce25SScott Long 	{
155030a4094fSAlexander Motin 		ata_set_transfer_settings(&start_ccb->cts,
155152c9ce25SScott Long 					   start_ccb->ccb_h.path->device,
155252c9ce25SScott Long 					   /*async_update*/FALSE);
155352c9ce25SScott Long 		break;
155452c9ce25SScott Long 	}
155552c9ce25SScott Long 	case XPT_SCAN_BUS:
15560e85f214SMatt Jacob 	case XPT_SCAN_TGT:
155752c9ce25SScott Long 		ata_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
155852c9ce25SScott Long 		break;
155952c9ce25SScott Long 	case XPT_SCAN_LUN:
156052c9ce25SScott Long 		ata_scan_lun(start_ccb->ccb_h.path->periph,
156152c9ce25SScott Long 			      start_ccb->ccb_h.path, start_ccb->crcn.flags,
156252c9ce25SScott Long 			      start_ccb);
156352c9ce25SScott Long 		break;
156452c9ce25SScott Long 	case XPT_GET_TRAN_SETTINGS:
156552c9ce25SScott Long 	{
156652c9ce25SScott Long 		struct cam_sim *sim;
156752c9ce25SScott Long 
156852c9ce25SScott Long 		sim = start_ccb->ccb_h.path->bus->sim;
156952c9ce25SScott Long 		(*(sim->sim_action))(sim, start_ccb);
157052c9ce25SScott Long 		break;
157152c9ce25SScott Long 	}
15724cca1530SAlexander Motin 	case XPT_SCSI_IO:
15734cca1530SAlexander Motin 	{
15744cca1530SAlexander Motin 		struct cam_ed *device;
15754cca1530SAlexander Motin 		u_int	maxlen = 0;
15764cca1530SAlexander Motin 
15774cca1530SAlexander Motin 		device = start_ccb->ccb_h.path->device;
15784cca1530SAlexander Motin 		if (device->protocol == PROTO_SCSI &&
15794cca1530SAlexander Motin 		    (device->flags & CAM_DEV_IDENTIFY_DATA_VALID)) {
15804cca1530SAlexander Motin 			uint16_t p =
15814cca1530SAlexander Motin 			    device->ident_data.config & ATA_PROTO_MASK;
15824cca1530SAlexander Motin 
15834cca1530SAlexander Motin 			maxlen = (p == ATA_PROTO_ATAPI_16) ? 16 :
15844cca1530SAlexander Motin 			    (p == ATA_PROTO_ATAPI_12) ? 12 : 0;
15854cca1530SAlexander Motin 		}
15864cca1530SAlexander Motin 		if (start_ccb->csio.cdb_len > maxlen) {
15874cca1530SAlexander Motin 			start_ccb->ccb_h.status = CAM_REQ_INVALID;
15884cca1530SAlexander Motin 			xpt_done(start_ccb);
15894cca1530SAlexander Motin 			break;
15904cca1530SAlexander Motin 		}
15914cca1530SAlexander Motin 		/* FALLTHROUGH */
15924cca1530SAlexander Motin 	}
159352c9ce25SScott Long 	default:
159452c9ce25SScott Long 		xpt_action_default(start_ccb);
159552c9ce25SScott Long 		break;
159652c9ce25SScott Long 	}
159752c9ce25SScott Long }
159852c9ce25SScott Long 
159952c9ce25SScott Long static void
160030a4094fSAlexander Motin ata_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
160152c9ce25SScott Long 			   int async_update)
160252c9ce25SScott Long {
160352c9ce25SScott Long 	struct	ccb_pathinq cpi;
160452c9ce25SScott Long 	struct	ccb_trans_settings cur_cts;
160552c9ce25SScott Long 	struct	ccb_trans_settings_scsi *scsi;
160652c9ce25SScott Long 	struct	ccb_trans_settings_scsi *cur_scsi;
160752c9ce25SScott Long 	struct	cam_sim *sim;
160852c9ce25SScott Long 	struct	scsi_inquiry_data *inq_data;
160952c9ce25SScott Long 
161052c9ce25SScott Long 	if (device == NULL) {
161152c9ce25SScott Long 		cts->ccb_h.status = CAM_PATH_INVALID;
161252c9ce25SScott Long 		xpt_done((union ccb *)cts);
161352c9ce25SScott Long 		return;
161452c9ce25SScott Long 	}
161552c9ce25SScott Long 
161652c9ce25SScott Long 	if (cts->protocol == PROTO_UNKNOWN
161752c9ce25SScott Long 	 || cts->protocol == PROTO_UNSPECIFIED) {
161852c9ce25SScott Long 		cts->protocol = device->protocol;
161952c9ce25SScott Long 		cts->protocol_version = device->protocol_version;
162052c9ce25SScott Long 	}
162152c9ce25SScott Long 
162252c9ce25SScott Long 	if (cts->protocol_version == PROTO_VERSION_UNKNOWN
162352c9ce25SScott Long 	 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
162452c9ce25SScott Long 		cts->protocol_version = device->protocol_version;
162552c9ce25SScott Long 
162652c9ce25SScott Long 	if (cts->protocol != device->protocol) {
162752c9ce25SScott Long 		xpt_print(cts->ccb_h.path, "Uninitialized Protocol %x:%x?\n",
162852c9ce25SScott Long 		       cts->protocol, device->protocol);
162952c9ce25SScott Long 		cts->protocol = device->protocol;
163052c9ce25SScott Long 	}
163152c9ce25SScott Long 
163252c9ce25SScott Long 	if (cts->protocol_version > device->protocol_version) {
163352c9ce25SScott Long 		if (bootverbose) {
163452c9ce25SScott Long 			xpt_print(cts->ccb_h.path, "Down reving Protocol "
163552c9ce25SScott Long 			    "Version from %d to %d?\n", cts->protocol_version,
163652c9ce25SScott Long 			    device->protocol_version);
163752c9ce25SScott Long 		}
163852c9ce25SScott Long 		cts->protocol_version = device->protocol_version;
163952c9ce25SScott Long 	}
164052c9ce25SScott Long 
164152c9ce25SScott Long 	if (cts->transport == XPORT_UNKNOWN
164252c9ce25SScott Long 	 || cts->transport == XPORT_UNSPECIFIED) {
164352c9ce25SScott Long 		cts->transport = device->transport;
164452c9ce25SScott Long 		cts->transport_version = device->transport_version;
164552c9ce25SScott Long 	}
164652c9ce25SScott Long 
164752c9ce25SScott Long 	if (cts->transport_version == XPORT_VERSION_UNKNOWN
164852c9ce25SScott Long 	 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
164952c9ce25SScott Long 		cts->transport_version = device->transport_version;
165052c9ce25SScott Long 
165152c9ce25SScott Long 	if (cts->transport != device->transport) {
165252c9ce25SScott Long 		xpt_print(cts->ccb_h.path, "Uninitialized Transport %x:%x?\n",
165352c9ce25SScott Long 		    cts->transport, device->transport);
165452c9ce25SScott Long 		cts->transport = device->transport;
165552c9ce25SScott Long 	}
165652c9ce25SScott Long 
165752c9ce25SScott Long 	if (cts->transport_version > device->transport_version) {
165852c9ce25SScott Long 		if (bootverbose) {
165952c9ce25SScott Long 			xpt_print(cts->ccb_h.path, "Down reving Transport "
166052c9ce25SScott Long 			    "Version from %d to %d?\n", cts->transport_version,
166152c9ce25SScott Long 			    device->transport_version);
166252c9ce25SScott Long 		}
166352c9ce25SScott Long 		cts->transport_version = device->transport_version;
166452c9ce25SScott Long 	}
166552c9ce25SScott Long 
166652c9ce25SScott Long 	sim = cts->ccb_h.path->bus->sim;
166752c9ce25SScott Long 
166852c9ce25SScott Long 	/*
166952c9ce25SScott Long 	 * Nothing more of interest to do unless
167052c9ce25SScott Long 	 * this is a device connected via the
167152c9ce25SScott Long 	 * SCSI protocol.
167252c9ce25SScott Long 	 */
167352c9ce25SScott Long 	if (cts->protocol != PROTO_SCSI) {
167452c9ce25SScott Long 		if (async_update == FALSE)
167552c9ce25SScott Long 			(*(sim->sim_action))(sim, (union ccb *)cts);
167652c9ce25SScott Long 		return;
167752c9ce25SScott Long 	}
167852c9ce25SScott Long 
167952c9ce25SScott Long 	inq_data = &device->inq_data;
168052c9ce25SScott Long 	scsi = &cts->proto_specific.scsi;
168183c5d981SAlexander Motin 	xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NONE);
168252c9ce25SScott Long 	cpi.ccb_h.func_code = XPT_PATH_INQ;
168352c9ce25SScott Long 	xpt_action((union ccb *)&cpi);
168452c9ce25SScott Long 
168552c9ce25SScott Long 	/* SCSI specific sanity checking */
168652c9ce25SScott Long 	if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
168752c9ce25SScott Long 	 || (INQ_DATA_TQ_ENABLED(inq_data)) == 0
168852c9ce25SScott Long 	 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
168952c9ce25SScott Long 	 || (device->mintags == 0)) {
169052c9ce25SScott Long 		/*
169152c9ce25SScott Long 		 * Can't tag on hardware that doesn't support tags,
169252c9ce25SScott Long 		 * doesn't have it enabled, or has broken tag support.
169352c9ce25SScott Long 		 */
169452c9ce25SScott Long 		scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
169552c9ce25SScott Long 	}
169652c9ce25SScott Long 
169752c9ce25SScott Long 	if (async_update == FALSE) {
169852c9ce25SScott Long 		/*
169952c9ce25SScott Long 		 * Perform sanity checking against what the
170052c9ce25SScott Long 		 * controller and device can do.
170152c9ce25SScott Long 		 */
170283c5d981SAlexander Motin 		xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NONE);
170352c9ce25SScott Long 		cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
170452c9ce25SScott Long 		cur_cts.type = cts->type;
170552c9ce25SScott Long 		xpt_action((union ccb *)&cur_cts);
170652c9ce25SScott Long 		if ((cur_cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
170752c9ce25SScott Long 			return;
170852c9ce25SScott Long 		}
170952c9ce25SScott Long 		cur_scsi = &cur_cts.proto_specific.scsi;
171052c9ce25SScott Long 		if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
171152c9ce25SScott Long 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
171252c9ce25SScott Long 			scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
171352c9ce25SScott Long 		}
171452c9ce25SScott Long 		if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
171552c9ce25SScott Long 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
171652c9ce25SScott Long 	}
171752c9ce25SScott Long 
171852c9ce25SScott Long 	if (cts->type == CTS_TYPE_CURRENT_SETTINGS
171952c9ce25SScott Long 	 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
172052c9ce25SScott Long 		int device_tagenb;
172152c9ce25SScott Long 
172252c9ce25SScott Long 		/*
172352c9ce25SScott Long 		 * If we are transitioning from tags to no-tags or
172452c9ce25SScott Long 		 * vice-versa, we need to carefully freeze and restart
172552c9ce25SScott Long 		 * the queue so that we don't overlap tagged and non-tagged
172652c9ce25SScott Long 		 * commands.  We also temporarily stop tags if there is
172752c9ce25SScott Long 		 * a change in transfer negotiation settings to allow
172852c9ce25SScott Long 		 * "tag-less" negotiation.
172952c9ce25SScott Long 		 */
173052c9ce25SScott Long 		if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
173152c9ce25SScott Long 		 || (device->inq_flags & SID_CmdQue) != 0)
173252c9ce25SScott Long 			device_tagenb = TRUE;
173352c9ce25SScott Long 		else
173452c9ce25SScott Long 			device_tagenb = FALSE;
173552c9ce25SScott Long 
173652c9ce25SScott Long 		if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
173752c9ce25SScott Long 		  && device_tagenb == FALSE)
173852c9ce25SScott Long 		 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
173952c9ce25SScott Long 		  && device_tagenb == TRUE)) {
174052c9ce25SScott Long 
174152c9ce25SScott Long 			if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
174252c9ce25SScott Long 				/*
174352c9ce25SScott Long 				 * Delay change to use tags until after a
174452c9ce25SScott Long 				 * few commands have gone to this device so
174552c9ce25SScott Long 				 * the controller has time to perform transfer
174652c9ce25SScott Long 				 * negotiations without tagged messages getting
174752c9ce25SScott Long 				 * in the way.
174852c9ce25SScott Long 				 */
174952c9ce25SScott Long 				device->tag_delay_count = CAM_TAG_DELAY_COUNT;
175052c9ce25SScott Long 				device->flags |= CAM_DEV_TAG_AFTER_COUNT;
175152c9ce25SScott Long 			} else {
175230a4094fSAlexander Motin 				xpt_stop_tags(cts->ccb_h.path);
175352c9ce25SScott Long 			}
175452c9ce25SScott Long 		}
175552c9ce25SScott Long 	}
175652c9ce25SScott Long 	if (async_update == FALSE)
175752c9ce25SScott Long 		(*(sim->sim_action))(sim, (union ccb *)cts);
175852c9ce25SScott Long }
175952c9ce25SScott Long 
176052c9ce25SScott Long /*
176152c9ce25SScott Long  * Handle any per-device event notifications that require action by the XPT.
176252c9ce25SScott Long  */
176352c9ce25SScott Long static void
176452c9ce25SScott Long ata_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
176552c9ce25SScott Long 	      struct cam_ed *device, void *async_arg)
176652c9ce25SScott Long {
176752c9ce25SScott Long 	cam_status status;
176852c9ce25SScott Long 	struct cam_path newpath;
176952c9ce25SScott Long 
177052c9ce25SScott Long 	/*
177152c9ce25SScott Long 	 * We only need to handle events for real devices.
177252c9ce25SScott Long 	 */
177352c9ce25SScott Long 	if (target->target_id == CAM_TARGET_WILDCARD
177452c9ce25SScott Long 	 || device->lun_id == CAM_LUN_WILDCARD)
177552c9ce25SScott Long 		return;
177652c9ce25SScott Long 
177752c9ce25SScott Long 	/*
177852c9ce25SScott Long 	 * We need our own path with wildcards expanded to
177952c9ce25SScott Long 	 * handle certain types of events.
178052c9ce25SScott Long 	 */
178152c9ce25SScott Long 	if ((async_code == AC_SENT_BDR)
178252c9ce25SScott Long 	 || (async_code == AC_BUS_RESET)
178352c9ce25SScott Long 	 || (async_code == AC_INQ_CHANGED))
178452c9ce25SScott Long 		status = xpt_compile_path(&newpath, NULL,
178552c9ce25SScott Long 					  bus->path_id,
178652c9ce25SScott Long 					  target->target_id,
178752c9ce25SScott Long 					  device->lun_id);
178852c9ce25SScott Long 	else
178952c9ce25SScott Long 		status = CAM_REQ_CMP_ERR;
179052c9ce25SScott Long 
179152c9ce25SScott Long 	if (status == CAM_REQ_CMP) {
179252c9ce25SScott Long 		if (async_code == AC_INQ_CHANGED) {
179352c9ce25SScott Long 			/*
179452c9ce25SScott Long 			 * We've sent a start unit command, or
179552c9ce25SScott Long 			 * something similar to a device that
179652c9ce25SScott Long 			 * may have caused its inquiry data to
179752c9ce25SScott Long 			 * change. So we re-scan the device to
179852c9ce25SScott Long 			 * refresh the inquiry data for it.
179952c9ce25SScott Long 			 */
180052c9ce25SScott Long 			ata_scan_lun(newpath.periph, &newpath,
180152c9ce25SScott Long 				     CAM_EXPECT_INQ_CHANGE, NULL);
180283c5d981SAlexander Motin 		} else {
180383c5d981SAlexander Motin 			/* We need to reinitialize device after reset. */
180483c5d981SAlexander Motin 			ata_scan_lun(newpath.periph, &newpath,
180583c5d981SAlexander Motin 				     0, NULL);
180652c9ce25SScott Long 		}
180752c9ce25SScott Long 		xpt_release_path(&newpath);
1808f98d7a47SAlexander Motin 	} else if (async_code == AC_LOST_DEVICE &&
1809f98d7a47SAlexander Motin 	    (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
181052c9ce25SScott Long 		device->flags |= CAM_DEV_UNCONFIGURED;
1811f98d7a47SAlexander Motin 		xpt_release_device(device);
181252c9ce25SScott Long 	} else if (async_code == AC_TRANSFER_NEG) {
181352c9ce25SScott Long 		struct ccb_trans_settings *settings;
181452c9ce25SScott Long 
181552c9ce25SScott Long 		settings = (struct ccb_trans_settings *)async_arg;
181630a4094fSAlexander Motin 		ata_set_transfer_settings(settings, device,
181752c9ce25SScott Long 					  /*async_update*/TRUE);
181852c9ce25SScott Long 	}
181952c9ce25SScott Long }
182052c9ce25SScott Long 
182157079b17SAlexander Motin static void
182257079b17SAlexander Motin ata_announce_periph(struct cam_periph *periph)
182357079b17SAlexander Motin {
182457079b17SAlexander Motin 	struct	ccb_pathinq cpi;
182557079b17SAlexander Motin 	struct	ccb_trans_settings cts;
182657079b17SAlexander Motin 	struct	cam_path *path = periph->path;
182757079b17SAlexander Motin 	u_int	speed;
182857079b17SAlexander Motin 	u_int	mb;
182957079b17SAlexander Motin 
183057079b17SAlexander Motin 	mtx_assert(periph->sim->mtx, MA_OWNED);
183157079b17SAlexander Motin 
183257079b17SAlexander Motin 	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
183357079b17SAlexander Motin 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
183457079b17SAlexander Motin 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
183557079b17SAlexander Motin 	xpt_action((union ccb*)&cts);
183657079b17SAlexander Motin 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
183757079b17SAlexander Motin 		return;
183857079b17SAlexander Motin 	/* Ask the SIM for its base transfer speed */
183957079b17SAlexander Motin 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
184057079b17SAlexander Motin 	cpi.ccb_h.func_code = XPT_PATH_INQ;
184157079b17SAlexander Motin 	xpt_action((union ccb *)&cpi);
184257079b17SAlexander Motin 	/* Report connection speed */
184357079b17SAlexander Motin 	speed = cpi.base_transfer_speed;
184457079b17SAlexander Motin 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_ATA) {
184557079b17SAlexander Motin 		struct	ccb_trans_settings_ata *ata =
184657079b17SAlexander Motin 		    &cts.xport_specific.ata;
184757079b17SAlexander Motin 
184857079b17SAlexander Motin 		if (ata->valid & CTS_ATA_VALID_MODE)
184957079b17SAlexander Motin 			speed = ata_mode2speed(ata->mode);
185057079b17SAlexander Motin 	}
185157079b17SAlexander Motin 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SATA) {
185257079b17SAlexander Motin 		struct	ccb_trans_settings_sata *sata =
185357079b17SAlexander Motin 		    &cts.xport_specific.sata;
185457079b17SAlexander Motin 
185557079b17SAlexander Motin 		if (sata->valid & CTS_SATA_VALID_REVISION)
185657079b17SAlexander Motin 			speed = ata_revision2speed(sata->revision);
185757079b17SAlexander Motin 	}
185857079b17SAlexander Motin 	mb = speed / 1000;
185957079b17SAlexander Motin 	if (mb > 0)
186057079b17SAlexander Motin 		printf("%s%d: %d.%03dMB/s transfers",
186157079b17SAlexander Motin 		       periph->periph_name, periph->unit_number,
186257079b17SAlexander Motin 		       mb, speed % 1000);
186357079b17SAlexander Motin 	else
186457079b17SAlexander Motin 		printf("%s%d: %dKB/s transfers", periph->periph_name,
186557079b17SAlexander Motin 		       periph->unit_number, speed);
186657079b17SAlexander Motin 	/* Report additional information about connection */
186757079b17SAlexander Motin 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_ATA) {
186857079b17SAlexander Motin 		struct ccb_trans_settings_ata *ata =
186957079b17SAlexander Motin 		    &cts.xport_specific.ata;
187057079b17SAlexander Motin 
187157079b17SAlexander Motin 		printf(" (");
187257079b17SAlexander Motin 		if (ata->valid & CTS_ATA_VALID_MODE)
187357079b17SAlexander Motin 			printf("%s, ", ata_mode2string(ata->mode));
187457079b17SAlexander Motin 		if ((ata->valid & CTS_ATA_VALID_ATAPI) && ata->atapi != 0)
187557079b17SAlexander Motin 			printf("ATAPI %dbytes, ", ata->atapi);
187657079b17SAlexander Motin 		if (ata->valid & CTS_ATA_VALID_BYTECOUNT)
187757079b17SAlexander Motin 			printf("PIO %dbytes", ata->bytecount);
187857079b17SAlexander Motin 		printf(")");
187957079b17SAlexander Motin 	}
188057079b17SAlexander Motin 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SATA) {
188157079b17SAlexander Motin 		struct ccb_trans_settings_sata *sata =
188257079b17SAlexander Motin 		    &cts.xport_specific.sata;
188357079b17SAlexander Motin 
188457079b17SAlexander Motin 		printf(" (");
188557079b17SAlexander Motin 		if (sata->valid & CTS_SATA_VALID_REVISION)
188657079b17SAlexander Motin 			printf("SATA %d.x, ", sata->revision);
188757079b17SAlexander Motin 		else
188857079b17SAlexander Motin 			printf("SATA, ");
188957079b17SAlexander Motin 		if (sata->valid & CTS_SATA_VALID_MODE)
189057079b17SAlexander Motin 			printf("%s, ", ata_mode2string(sata->mode));
189157079b17SAlexander Motin 		if ((sata->valid & CTS_ATA_VALID_ATAPI) && sata->atapi != 0)
189257079b17SAlexander Motin 			printf("ATAPI %dbytes, ", sata->atapi);
189357079b17SAlexander Motin 		if (sata->valid & CTS_SATA_VALID_BYTECOUNT)
189457079b17SAlexander Motin 			printf("PIO %dbytes", sata->bytecount);
189557079b17SAlexander Motin 		printf(")");
189657079b17SAlexander Motin 	}
189757079b17SAlexander Motin 	printf("\n");
189857079b17SAlexander Motin }
189957079b17SAlexander Motin 
1900