xref: /freebsd/sys/cam/ata/ata_xpt.c (revision 2e1eb332)
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
687dc3213dSAlexander Motin 	u_int mintags;
6952c9ce25SScott Long 	u_int maxtags;
7052c9ce25SScott Long };
7152c9ce25SScott Long 
7252c9ce25SScott Long static periph_init_t probe_periph_init;
7352c9ce25SScott Long 
7452c9ce25SScott Long static struct periph_driver probe_driver =
7552c9ce25SScott Long {
76f09f8e3eSAlexander Motin 	probe_periph_init, "aprobe",
771e637ba6SAlexander Motin 	TAILQ_HEAD_INITIALIZER(probe_driver.units), /* generation */ 0,
781e637ba6SAlexander Motin 	CAM_PERIPH_DRV_EARLY
7952c9ce25SScott Long };
8052c9ce25SScott Long 
81f09f8e3eSAlexander Motin PERIPHDRIVER_DECLARE(aprobe, probe_driver);
8252c9ce25SScott Long 
8352c9ce25SScott Long typedef enum {
8452c9ce25SScott Long 	PROBE_RESET,
8552c9ce25SScott Long 	PROBE_IDENTIFY,
864ef08dc5SAlexander Motin 	PROBE_SPINUP,
8752c9ce25SScott Long 	PROBE_SETMODE,
88da6808c1SAlexander Motin 	PROBE_SETPM,
89da6808c1SAlexander Motin 	PROBE_SETAPST,
90da6808c1SAlexander Motin 	PROBE_SETDMAAA,
918d169381SAlexander Motin 	PROBE_SETAN,
921e637ba6SAlexander Motin 	PROBE_SET_MULTI,
9352c9ce25SScott Long 	PROBE_INQUIRY,
9452c9ce25SScott Long 	PROBE_FULL_INQUIRY,
9552c9ce25SScott Long 	PROBE_PM_PID,
9652c9ce25SScott Long 	PROBE_PM_PRV,
973089bb2eSAlexander Motin 	PROBE_IDENTIFY_SES,
983089bb2eSAlexander Motin 	PROBE_IDENTIFY_SAFTE,
99a4d953c4SAlexander Motin 	PROBE_DONE,
10052c9ce25SScott Long 	PROBE_INVALID
10152c9ce25SScott Long } probe_action;
10252c9ce25SScott Long 
10352c9ce25SScott Long static char *probe_action_text[] = {
10452c9ce25SScott Long 	"PROBE_RESET",
10552c9ce25SScott Long 	"PROBE_IDENTIFY",
1064ef08dc5SAlexander Motin 	"PROBE_SPINUP",
10752c9ce25SScott Long 	"PROBE_SETMODE",
108da6808c1SAlexander Motin 	"PROBE_SETPM",
109da6808c1SAlexander Motin 	"PROBE_SETAPST",
110da6808c1SAlexander Motin 	"PROBE_SETDMAAA",
1118d169381SAlexander Motin 	"PROBE_SETAN",
1121e637ba6SAlexander Motin 	"PROBE_SET_MULTI",
11352c9ce25SScott Long 	"PROBE_INQUIRY",
11452c9ce25SScott Long 	"PROBE_FULL_INQUIRY",
11552c9ce25SScott Long 	"PROBE_PM_PID",
11652c9ce25SScott Long 	"PROBE_PM_PRV",
1173089bb2eSAlexander Motin 	"PROBE_IDENTIFY_SES",
1183089bb2eSAlexander Motin 	"PROBE_IDENTIFY_SAFTE",
119a4d953c4SAlexander Motin 	"PROBE_DONE",
12052c9ce25SScott Long 	"PROBE_INVALID"
12152c9ce25SScott Long };
12252c9ce25SScott Long 
12352c9ce25SScott Long #define PROBE_SET_ACTION(softc, newaction)	\
12452c9ce25SScott Long do {									\
12552c9ce25SScott Long 	char **text;							\
12652c9ce25SScott Long 	text = probe_action_text;					\
127a4d953c4SAlexander Motin 	CAM_DEBUG((softc)->periph->path, CAM_DEBUG_PROBE,		\
12852c9ce25SScott Long 	    ("Probe %s to %s\n", text[(softc)->action],			\
12952c9ce25SScott Long 	    text[(newaction)]));					\
13052c9ce25SScott Long 	(softc)->action = (newaction);					\
13152c9ce25SScott Long } while(0)
13252c9ce25SScott Long 
13352c9ce25SScott Long typedef enum {
13452c9ce25SScott Long 	PROBE_NO_ANNOUNCE	= 0x04
13552c9ce25SScott Long } probe_flags;
13652c9ce25SScott Long 
13752c9ce25SScott Long typedef struct {
13852c9ce25SScott Long 	TAILQ_HEAD(, ccb_hdr) request_ccbs;
139a9b8edb1SAlexander Motin 	struct ata_params	ident_data;
14052c9ce25SScott Long 	probe_action	action;
14152c9ce25SScott Long 	probe_flags	flags;
14252c9ce25SScott Long 	uint32_t	pm_pid;
14352c9ce25SScott Long 	uint32_t	pm_prv;
14483c5d981SAlexander Motin 	int		restart;
1454ef08dc5SAlexander Motin 	int		spinup;
14625a519a9SAlexander Motin 	int		faults;
147da6808c1SAlexander Motin 	u_int		caps;
14852c9ce25SScott Long 	struct cam_periph *periph;
14952c9ce25SScott Long } probe_softc;
15052c9ce25SScott Long 
15130a4094fSAlexander Motin static struct ata_quirk_entry ata_quirk_table[] =
15252c9ce25SScott Long {
15352c9ce25SScott Long 	{
15452c9ce25SScott Long 		/* Default tagged queuing parameters for all devices */
15552c9ce25SScott Long 		{
15652c9ce25SScott Long 		  T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
15752c9ce25SScott Long 		  /*vendor*/"*", /*product*/"*", /*revision*/"*"
15852c9ce25SScott Long 		},
1597dc3213dSAlexander Motin 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
16052c9ce25SScott Long 	},
16152c9ce25SScott Long };
16252c9ce25SScott Long 
16330a4094fSAlexander Motin static const int ata_quirk_table_size =
16430a4094fSAlexander Motin 	sizeof(ata_quirk_table) / sizeof(*ata_quirk_table);
16552c9ce25SScott Long 
16652c9ce25SScott Long static cam_status	proberegister(struct cam_periph *periph,
16752c9ce25SScott Long 				      void *arg);
16852c9ce25SScott Long static void	 probeschedule(struct cam_periph *probe_periph);
16952c9ce25SScott Long static void	 probestart(struct cam_periph *periph, union ccb *start_ccb);
170b9c473b2SAlexander Motin static void	 proberequestdefaultnegotiation(struct cam_periph *periph);
17152c9ce25SScott Long static void	 probedone(struct cam_periph *periph, union ccb *done_ccb);
17252c9ce25SScott Long static void	 probecleanup(struct cam_periph *periph);
17330a4094fSAlexander Motin static void	 ata_find_quirk(struct cam_ed *device);
17452c9ce25SScott Long static void	 ata_scan_bus(struct cam_periph *periph, union ccb *ccb);
17552c9ce25SScott Long static void	 ata_scan_lun(struct cam_periph *periph,
17652c9ce25SScott Long 			       struct cam_path *path, cam_flags flags,
17752c9ce25SScott Long 			       union ccb *ccb);
17852c9ce25SScott Long static void	 xptscandone(struct cam_periph *periph, union ccb *done_ccb);
17952c9ce25SScott Long static struct cam_ed *
18052c9ce25SScott Long 		 ata_alloc_device(struct cam_eb *bus, struct cam_et *target,
18152c9ce25SScott Long 				   lun_id_t lun_id);
18252c9ce25SScott Long static void	 ata_device_transport(struct cam_path *path);
183b9c473b2SAlexander Motin static void	 ata_get_transfer_settings(struct ccb_trans_settings *cts);
18430a4094fSAlexander Motin static void	 ata_set_transfer_settings(struct ccb_trans_settings *cts,
18552c9ce25SScott Long 					    struct cam_ed *device,
18652c9ce25SScott Long 					    int async_update);
18752c9ce25SScott Long static void	 ata_dev_async(u_int32_t async_code,
18852c9ce25SScott Long 				struct cam_eb *bus,
18952c9ce25SScott Long 				struct cam_et *target,
19052c9ce25SScott Long 				struct cam_ed *device,
19152c9ce25SScott Long 				void *async_arg);
19252c9ce25SScott Long static void	 ata_action(union ccb *start_ccb);
19357079b17SAlexander Motin static void	 ata_announce_periph(struct cam_periph *periph);
19452c9ce25SScott Long 
1952121d8a5SAlexander Motin static int ata_dma = 1;
1962121d8a5SAlexander Motin static int atapi_dma = 1;
1972121d8a5SAlexander Motin 
1982121d8a5SAlexander Motin TUNABLE_INT("hw.ata.ata_dma", &ata_dma);
1992121d8a5SAlexander Motin TUNABLE_INT("hw.ata.atapi_dma", &atapi_dma);
2002121d8a5SAlexander Motin 
20152c9ce25SScott Long static struct xpt_xport ata_xport = {
20252c9ce25SScott Long 	.alloc_device = ata_alloc_device,
20352c9ce25SScott Long 	.action = ata_action,
20452c9ce25SScott Long 	.async = ata_dev_async,
20557079b17SAlexander Motin 	.announce = ata_announce_periph,
20652c9ce25SScott Long };
20752c9ce25SScott Long 
20852c9ce25SScott Long struct xpt_xport *
20952c9ce25SScott Long ata_get_xport(void)
21052c9ce25SScott Long {
21152c9ce25SScott Long 	return (&ata_xport);
21252c9ce25SScott Long }
21352c9ce25SScott Long 
21452c9ce25SScott Long static void
21552c9ce25SScott Long probe_periph_init()
21652c9ce25SScott Long {
21752c9ce25SScott Long }
21852c9ce25SScott Long 
21952c9ce25SScott Long static cam_status
22052c9ce25SScott Long proberegister(struct cam_periph *periph, void *arg)
22152c9ce25SScott Long {
22252c9ce25SScott Long 	union ccb *request_ccb;	/* CCB representing the probe request */
22352c9ce25SScott Long 	cam_status status;
22452c9ce25SScott Long 	probe_softc *softc;
22552c9ce25SScott Long 
22652c9ce25SScott Long 	request_ccb = (union ccb *)arg;
22752c9ce25SScott Long 	if (request_ccb == NULL) {
22852c9ce25SScott Long 		printf("proberegister: no probe CCB, "
22952c9ce25SScott Long 		       "can't register device\n");
23052c9ce25SScott Long 		return(CAM_REQ_CMP_ERR);
23152c9ce25SScott Long 	}
23252c9ce25SScott Long 
2334ef08dc5SAlexander Motin 	softc = (probe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_ZERO | M_NOWAIT);
23452c9ce25SScott Long 
23552c9ce25SScott Long 	if (softc == NULL) {
23652c9ce25SScott Long 		printf("proberegister: Unable to probe new device. "
23752c9ce25SScott Long 		       "Unable to allocate softc\n");
23852c9ce25SScott Long 		return(CAM_REQ_CMP_ERR);
23952c9ce25SScott Long 	}
24052c9ce25SScott Long 	TAILQ_INIT(&softc->request_ccbs);
24152c9ce25SScott Long 	TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
24252c9ce25SScott Long 			  periph_links.tqe);
24352c9ce25SScott Long 	softc->flags = 0;
24452c9ce25SScott Long 	periph->softc = softc;
24552c9ce25SScott Long 	softc->periph = periph;
24652c9ce25SScott Long 	softc->action = PROBE_INVALID;
24752c9ce25SScott Long 	status = cam_periph_acquire(periph);
24852c9ce25SScott Long 	if (status != CAM_REQ_CMP) {
24952c9ce25SScott Long 		return (status);
25052c9ce25SScott Long 	}
251a4d953c4SAlexander Motin 	CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe started\n"));
252a4d953c4SAlexander Motin 
25352c9ce25SScott Long 	/*
25483c5d981SAlexander Motin 	 * Ensure nobody slip in until probe finish.
25552c9ce25SScott Long 	 */
25683c5d981SAlexander Motin 	cam_freeze_devq_arg(periph->path,
25783c5d981SAlexander Motin 	    RELSIM_RELEASE_RUNLEVEL, CAM_RL_XPT + 1);
25852c9ce25SScott Long 	probeschedule(periph);
25952c9ce25SScott Long 	return(CAM_REQ_CMP);
26052c9ce25SScott Long }
26152c9ce25SScott Long 
26252c9ce25SScott Long static void
26352c9ce25SScott Long probeschedule(struct cam_periph *periph)
26452c9ce25SScott Long {
26552c9ce25SScott Long 	union ccb *ccb;
26652c9ce25SScott Long 	probe_softc *softc;
26752c9ce25SScott Long 
26852c9ce25SScott Long 	softc = (probe_softc *)periph->softc;
26952c9ce25SScott Long 	ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
27052c9ce25SScott Long 
27165d0fb03SAlexander Motin 	if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) ||
2723089bb2eSAlexander Motin 	    periph->path->device->protocol == PROTO_SATAPM ||
2733089bb2eSAlexander Motin 	    periph->path->device->protocol == PROTO_SEMB)
27452c9ce25SScott Long 		PROBE_SET_ACTION(softc, PROBE_RESET);
27552c9ce25SScott Long 	else
27652c9ce25SScott Long 		PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
27752c9ce25SScott Long 
27852c9ce25SScott Long 	if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
27952c9ce25SScott Long 		softc->flags |= PROBE_NO_ANNOUNCE;
28052c9ce25SScott Long 	else
28152c9ce25SScott Long 		softc->flags &= ~PROBE_NO_ANNOUNCE;
28252c9ce25SScott Long 
28383c5d981SAlexander Motin 	xpt_schedule(periph, CAM_PRIORITY_XPT);
28452c9ce25SScott Long }
28552c9ce25SScott Long 
28652c9ce25SScott Long static void
28752c9ce25SScott Long probestart(struct cam_periph *periph, union ccb *start_ccb)
28852c9ce25SScott Long {
289c8039fc6SAlexander Motin 	struct ccb_trans_settings cts;
29052c9ce25SScott Long 	struct ccb_ataio *ataio;
29152c9ce25SScott Long 	struct ccb_scsiio *csio;
29252c9ce25SScott Long 	probe_softc *softc;
2931e637ba6SAlexander Motin 	struct cam_path *path;
2941e637ba6SAlexander Motin 	struct ata_params *ident_buf;
29552c9ce25SScott Long 
29652c9ce25SScott Long 	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
29752c9ce25SScott Long 
29852c9ce25SScott Long 	softc = (probe_softc *)periph->softc;
2991e637ba6SAlexander Motin 	path = start_ccb->ccb_h.path;
30052c9ce25SScott Long 	ataio = &start_ccb->ataio;
30152c9ce25SScott Long 	csio = &start_ccb->csio;
3021e637ba6SAlexander Motin 	ident_buf = &periph->path->device->ident_data;
30352c9ce25SScott Long 
30483c5d981SAlexander Motin 	if (softc->restart) {
30583c5d981SAlexander Motin 		softc->restart = 0;
30683c5d981SAlexander Motin 		if ((path->device->flags & CAM_DEV_UNCONFIGURED) ||
3073089bb2eSAlexander Motin 		    path->device->protocol == PROTO_SATAPM ||
3083089bb2eSAlexander Motin 		    path->device->protocol == PROTO_SEMB)
30983c5d981SAlexander Motin 			softc->action = PROBE_RESET;
31083c5d981SAlexander Motin 		else
31183c5d981SAlexander Motin 			softc->action = PROBE_IDENTIFY;
31283c5d981SAlexander Motin 	}
31352c9ce25SScott Long 	switch (softc->action) {
31452c9ce25SScott Long 	case PROBE_RESET:
31552c9ce25SScott Long 		cam_fill_ataio(ataio,
31652c9ce25SScott Long 		      0,
31752c9ce25SScott Long 		      probedone,
31852c9ce25SScott Long 		      /*flags*/CAM_DIR_NONE,
31965d0fb03SAlexander Motin 		      0,
32052c9ce25SScott Long 		      /*data_ptr*/NULL,
32152c9ce25SScott Long 		      /*dxfer_len*/0,
32283c5d981SAlexander Motin 		      15 * 1000);
32352c9ce25SScott Long 		ata_reset_cmd(ataio);
32452c9ce25SScott Long 		break;
32552c9ce25SScott Long 	case PROBE_IDENTIFY:
32652c9ce25SScott Long 		cam_fill_ataio(ataio,
32752c9ce25SScott Long 		      1,
32852c9ce25SScott Long 		      probedone,
32952c9ce25SScott Long 		      /*flags*/CAM_DIR_IN,
33065d0fb03SAlexander Motin 		      0,
331a9b8edb1SAlexander Motin 		      /*data_ptr*/(u_int8_t *)&softc->ident_data,
332a9b8edb1SAlexander Motin 		      /*dxfer_len*/sizeof(softc->ident_data),
33352c9ce25SScott Long 		      30 * 1000);
33452c9ce25SScott Long 		if (periph->path->device->protocol == PROTO_ATA)
3357606b445SAlexander Motin 			ata_28bit_cmd(ataio, ATA_ATA_IDENTIFY, 0, 0, 0);
33652c9ce25SScott Long 		else
3377606b445SAlexander Motin 			ata_28bit_cmd(ataio, ATA_ATAPI_IDENTIFY, 0, 0, 0);
33852c9ce25SScott Long 		break;
3394ef08dc5SAlexander Motin 	case PROBE_SPINUP:
3404ef08dc5SAlexander Motin 		if (bootverbose)
3414ef08dc5SAlexander Motin 			xpt_print(path, "Spinning up device\n");
3424ef08dc5SAlexander Motin 		cam_fill_ataio(ataio,
3434ef08dc5SAlexander Motin 		      1,
3444ef08dc5SAlexander Motin 		      probedone,
3454ef08dc5SAlexander Motin 		      /*flags*/CAM_DIR_NONE | CAM_HIGH_POWER,
3464ef08dc5SAlexander Motin 		      0,
3474ef08dc5SAlexander Motin 		      /*data_ptr*/NULL,
3484ef08dc5SAlexander Motin 		      /*dxfer_len*/0,
3494ef08dc5SAlexander Motin 		      30 * 1000);
3504ef08dc5SAlexander Motin 		ata_28bit_cmd(ataio, ATA_SETFEATURES, ATA_SF_PUIS_SPINUP, 0, 0);
3514ef08dc5SAlexander Motin 		break;
35252c9ce25SScott Long 	case PROBE_SETMODE:
353c8039fc6SAlexander Motin 	{
354c8039fc6SAlexander Motin 		int mode, wantmode;
355c8039fc6SAlexander Motin 
356c8039fc6SAlexander Motin 		mode = 0;
357c8039fc6SAlexander Motin 		/* Fetch user modes from SIM. */
358c8039fc6SAlexander Motin 		bzero(&cts, sizeof(cts));
35983c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
360c8039fc6SAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
361c8039fc6SAlexander Motin 		cts.type = CTS_TYPE_USER_SETTINGS;
362c8039fc6SAlexander Motin 		xpt_action((union ccb *)&cts);
363c8039fc6SAlexander Motin 		if (path->device->transport == XPORT_ATA) {
364c8039fc6SAlexander Motin 			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_MODE)
365c8039fc6SAlexander Motin 				mode = cts.xport_specific.ata.mode;
366c8039fc6SAlexander Motin 		} else {
3671a6f09b8SAlexander Motin 			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_MODE)
368c8039fc6SAlexander Motin 				mode = cts.xport_specific.sata.mode;
369c8039fc6SAlexander Motin 		}
3702121d8a5SAlexander Motin 		if (periph->path->device->protocol == PROTO_ATA) {
3712121d8a5SAlexander Motin 			if (ata_dma == 0 && (mode == 0 || mode > ATA_PIO_MAX))
3722121d8a5SAlexander Motin 				mode = ATA_PIO_MAX;
3732121d8a5SAlexander Motin 		} else {
3742121d8a5SAlexander Motin 			if (atapi_dma == 0 && (mode == 0 || mode > ATA_PIO_MAX))
3752121d8a5SAlexander Motin 				mode = ATA_PIO_MAX;
3762121d8a5SAlexander Motin 		}
377c8039fc6SAlexander Motin negotiate:
378c8039fc6SAlexander Motin 		/* Honor device capabilities. */
379c8039fc6SAlexander Motin 		wantmode = mode = ata_max_mode(ident_buf, mode);
380c8039fc6SAlexander Motin 		/* Report modes to SIM. */
381c8039fc6SAlexander Motin 		bzero(&cts, sizeof(cts));
38283c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
383c8039fc6SAlexander Motin 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
384c8039fc6SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
385c8039fc6SAlexander Motin 		if (path->device->transport == XPORT_ATA) {
386c8039fc6SAlexander Motin 			cts.xport_specific.ata.mode = mode;
387c8039fc6SAlexander Motin 			cts.xport_specific.ata.valid = CTS_ATA_VALID_MODE;
388c8039fc6SAlexander Motin 		} else {
389c8039fc6SAlexander Motin 			cts.xport_specific.sata.mode = mode;
390c8039fc6SAlexander Motin 			cts.xport_specific.sata.valid = CTS_SATA_VALID_MODE;
391c8039fc6SAlexander Motin 		}
392c8039fc6SAlexander Motin 		xpt_action((union ccb *)&cts);
393066f913aSAlexander Motin 		/* Fetch current modes from SIM. */
394c8039fc6SAlexander Motin 		bzero(&cts, sizeof(cts));
39583c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
396c8039fc6SAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
397c8039fc6SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
398c8039fc6SAlexander Motin 		xpt_action((union ccb *)&cts);
399c8039fc6SAlexander Motin 		if (path->device->transport == XPORT_ATA) {
400c8039fc6SAlexander Motin 			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_MODE)
401c8039fc6SAlexander Motin 				mode = cts.xport_specific.ata.mode;
402c8039fc6SAlexander Motin 		} else {
403c8039fc6SAlexander Motin 			if (cts.xport_specific.ata.valid & CTS_SATA_VALID_MODE)
404c8039fc6SAlexander Motin 				mode = cts.xport_specific.sata.mode;
405c8039fc6SAlexander Motin 		}
406c8039fc6SAlexander Motin 		/* If SIM disagree - renegotiate. */
407c8039fc6SAlexander Motin 		if (mode != wantmode)
408c8039fc6SAlexander Motin 			goto negotiate;
409cf2b9a5fSAlexander Motin 		/* Remember what transport thinks about DMA. */
410cf2b9a5fSAlexander Motin 		if (mode < ATA_DMA)
411cf2b9a5fSAlexander Motin 			path->device->inq_flags &= ~SID_DMA;
412cf2b9a5fSAlexander Motin 		else
413cf2b9a5fSAlexander Motin 			path->device->inq_flags |= SID_DMA;
414581b2e78SAlexander Motin 		xpt_async(AC_GETDEV_CHANGED, path, NULL);
41552c9ce25SScott Long 		cam_fill_ataio(ataio,
41652c9ce25SScott Long 		      1,
41752c9ce25SScott Long 		      probedone,
4185daa555dSAlexander Motin 		      /*flags*/CAM_DIR_NONE,
4195daa555dSAlexander Motin 		      0,
4205daa555dSAlexander Motin 		      /*data_ptr*/NULL,
4215daa555dSAlexander Motin 		      /*dxfer_len*/0,
42252c9ce25SScott Long 		      30 * 1000);
423c8039fc6SAlexander Motin 		ata_28bit_cmd(ataio, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
42452c9ce25SScott Long 		break;
425c8039fc6SAlexander Motin 	}
426da6808c1SAlexander Motin 	case PROBE_SETPM:
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_PMREQ) ? 0x10 : 0x90,
437da6808c1SAlexander Motin 		    0, 0x03);
438da6808c1SAlexander Motin 		break;
439da6808c1SAlexander Motin 	case PROBE_SETAPST:
440da6808c1SAlexander Motin 		cam_fill_ataio(ataio,
441da6808c1SAlexander Motin 		    1,
442da6808c1SAlexander Motin 		    probedone,
443da6808c1SAlexander Motin 		    CAM_DIR_NONE,
444da6808c1SAlexander Motin 		    0,
445da6808c1SAlexander Motin 		    NULL,
446da6808c1SAlexander Motin 		    0,
447da6808c1SAlexander Motin 		    30*1000);
448da6808c1SAlexander Motin 		ata_28bit_cmd(ataio, ATA_SETFEATURES,
449da6808c1SAlexander Motin 		    (softc->caps & CTS_SATA_CAPS_H_APST) ? 0x10 : 0x90,
450da6808c1SAlexander Motin 		    0, 0x07);
451da6808c1SAlexander Motin 		break;
452da6808c1SAlexander Motin 	case PROBE_SETDMAAA:
453da6808c1SAlexander Motin 		cam_fill_ataio(ataio,
454da6808c1SAlexander Motin 		    1,
455da6808c1SAlexander Motin 		    probedone,
456da6808c1SAlexander Motin 		    CAM_DIR_NONE,
457da6808c1SAlexander Motin 		    0,
458da6808c1SAlexander Motin 		    NULL,
459da6808c1SAlexander Motin 		    0,
460da6808c1SAlexander Motin 		    30*1000);
461da6808c1SAlexander Motin 		ata_28bit_cmd(ataio, ATA_SETFEATURES,
462da6808c1SAlexander Motin 		    (softc->caps & CTS_SATA_CAPS_H_DMAAA) ? 0x10 : 0x90,
463da6808c1SAlexander Motin 		    0, 0x02);
464da6808c1SAlexander Motin 		break;
4658d169381SAlexander Motin 	case PROBE_SETAN:
4663631c638SAlexander Motin 		/* Remember what transport thinks about AEN. */
4673631c638SAlexander Motin 		if (softc->caps & CTS_SATA_CAPS_H_AN)
4683631c638SAlexander Motin 			path->device->inq_flags |= SID_AEN;
4693631c638SAlexander Motin 		else
4703631c638SAlexander Motin 			path->device->inq_flags &= ~SID_AEN;
4713631c638SAlexander Motin 		xpt_async(AC_GETDEV_CHANGED, path, NULL);
4728d169381SAlexander Motin 		cam_fill_ataio(ataio,
4738d169381SAlexander Motin 		    1,
4748d169381SAlexander Motin 		    probedone,
4758d169381SAlexander Motin 		    CAM_DIR_NONE,
4768d169381SAlexander Motin 		    0,
4778d169381SAlexander Motin 		    NULL,
4788d169381SAlexander Motin 		    0,
4798d169381SAlexander Motin 		    30*1000);
4808d169381SAlexander Motin 		ata_28bit_cmd(ataio, ATA_SETFEATURES,
4818d169381SAlexander Motin 		    (softc->caps & CTS_SATA_CAPS_H_AN) ? 0x10 : 0x90,
4828d169381SAlexander Motin 		    0, 0x05);
4838d169381SAlexander Motin 		break;
4841e637ba6SAlexander Motin 	case PROBE_SET_MULTI:
4851e637ba6SAlexander Motin 	{
486066f913aSAlexander Motin 		u_int sectors, bytecount;
4871e637ba6SAlexander Motin 
488066f913aSAlexander Motin 		bytecount = 8192;	/* SATA maximum */
489066f913aSAlexander Motin 		/* Fetch user bytecount from SIM. */
490066f913aSAlexander Motin 		bzero(&cts, sizeof(cts));
49183c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
492066f913aSAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
493066f913aSAlexander Motin 		cts.type = CTS_TYPE_USER_SETTINGS;
494066f913aSAlexander Motin 		xpt_action((union ccb *)&cts);
495066f913aSAlexander Motin 		if (path->device->transport == XPORT_ATA) {
496066f913aSAlexander Motin 			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
497066f913aSAlexander Motin 				bytecount = cts.xport_specific.ata.bytecount;
498066f913aSAlexander Motin 		} else {
499066f913aSAlexander Motin 			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
500066f913aSAlexander Motin 				bytecount = cts.xport_specific.sata.bytecount;
501066f913aSAlexander Motin 		}
502066f913aSAlexander Motin 		/* Honor device capabilities. */
503066f913aSAlexander Motin 		sectors = max(1, min(ident_buf->sectors_intr & 0xff,
504066f913aSAlexander Motin 		    bytecount / ata_logical_sector_size(ident_buf)));
5051e637ba6SAlexander Motin 		/* Report bytecount to SIM. */
5061e637ba6SAlexander Motin 		bzero(&cts, sizeof(cts));
50783c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
5081e637ba6SAlexander Motin 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
5091e637ba6SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
5101e637ba6SAlexander Motin 		if (path->device->transport == XPORT_ATA) {
511c1bd46c2SAlexander Motin 			cts.xport_specific.ata.bytecount = sectors *
512c1bd46c2SAlexander Motin 			    ata_logical_sector_size(ident_buf);
5131e637ba6SAlexander Motin 			cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT;
5141e637ba6SAlexander Motin 		} else {
515c1bd46c2SAlexander Motin 			cts.xport_specific.sata.bytecount = sectors *
516c1bd46c2SAlexander Motin 			    ata_logical_sector_size(ident_buf);
5171e637ba6SAlexander Motin 			cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT;
5181e637ba6SAlexander Motin 		}
5191e637ba6SAlexander Motin 		xpt_action((union ccb *)&cts);
520066f913aSAlexander Motin 		/* Fetch current bytecount from SIM. */
521066f913aSAlexander Motin 		bzero(&cts, sizeof(cts));
52283c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
523066f913aSAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
524066f913aSAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
525066f913aSAlexander Motin 		xpt_action((union ccb *)&cts);
526066f913aSAlexander Motin 		if (path->device->transport == XPORT_ATA) {
527066f913aSAlexander Motin 			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
528066f913aSAlexander Motin 				bytecount = cts.xport_specific.ata.bytecount;
529066f913aSAlexander Motin 		} else {
530066f913aSAlexander Motin 			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
531066f913aSAlexander Motin 				bytecount = cts.xport_specific.sata.bytecount;
532066f913aSAlexander Motin 		}
533066f913aSAlexander Motin 		sectors = bytecount / ata_logical_sector_size(ident_buf);
5341e637ba6SAlexander Motin 
5351e637ba6SAlexander Motin 		cam_fill_ataio(ataio,
5361e637ba6SAlexander Motin 		    1,
5371e637ba6SAlexander Motin 		    probedone,
5381e637ba6SAlexander Motin 		    CAM_DIR_NONE,
5391e637ba6SAlexander Motin 		    0,
5401e637ba6SAlexander Motin 		    NULL,
5411e637ba6SAlexander Motin 		    0,
5421e637ba6SAlexander Motin 		    30*1000);
5431e637ba6SAlexander Motin 		ata_28bit_cmd(ataio, ATA_SET_MULTI, 0, 0, sectors);
5441e637ba6SAlexander Motin 		break;
54552c9ce25SScott Long 	}
54652c9ce25SScott Long 	case PROBE_INQUIRY:
547066f913aSAlexander Motin 	{
548066f913aSAlexander Motin 		u_int bytecount;
549066f913aSAlexander Motin 
550066f913aSAlexander Motin 		bytecount = 8192;	/* SATA maximum */
551066f913aSAlexander Motin 		/* Fetch user bytecount from SIM. */
552066f913aSAlexander Motin 		bzero(&cts, sizeof(cts));
55383c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
554066f913aSAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
555066f913aSAlexander Motin 		cts.type = CTS_TYPE_USER_SETTINGS;
556066f913aSAlexander Motin 		xpt_action((union ccb *)&cts);
557066f913aSAlexander Motin 		if (path->device->transport == XPORT_ATA) {
558066f913aSAlexander Motin 			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
559066f913aSAlexander Motin 				bytecount = cts.xport_specific.ata.bytecount;
560066f913aSAlexander Motin 		} else {
561066f913aSAlexander Motin 			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
562066f913aSAlexander Motin 				bytecount = cts.xport_specific.sata.bytecount;
563066f913aSAlexander Motin 		}
564066f913aSAlexander Motin 		/* Honor device capabilities. */
565066f913aSAlexander Motin 		bytecount &= ~1;
566066f913aSAlexander Motin 		bytecount = max(2, min(65534, bytecount));
567066f913aSAlexander Motin 		if (ident_buf->satacapabilities != 0x0000 &&
568066f913aSAlexander Motin 		    ident_buf->satacapabilities != 0xffff) {
569066f913aSAlexander Motin 			bytecount = min(8192, bytecount);
570066f913aSAlexander Motin 		}
571066f913aSAlexander Motin 		/* Report bytecount to SIM. */
572066f913aSAlexander Motin 		bzero(&cts, sizeof(cts));
57383c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
574066f913aSAlexander Motin 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
575066f913aSAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
576066f913aSAlexander Motin 		if (path->device->transport == XPORT_ATA) {
577066f913aSAlexander Motin 			cts.xport_specific.ata.bytecount = bytecount;
578066f913aSAlexander Motin 			cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT;
579066f913aSAlexander Motin 		} else {
580066f913aSAlexander Motin 			cts.xport_specific.sata.bytecount = bytecount;
581066f913aSAlexander Motin 			cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT;
582066f913aSAlexander Motin 		}
583066f913aSAlexander Motin 		xpt_action((union ccb *)&cts);
584066f913aSAlexander Motin 		/* FALLTHROUGH */
585066f913aSAlexander Motin 	}
58652c9ce25SScott Long 	case PROBE_FULL_INQUIRY:
58752c9ce25SScott Long 	{
58852c9ce25SScott Long 		u_int inquiry_len;
58952c9ce25SScott Long 		struct scsi_inquiry_data *inq_buf =
59052c9ce25SScott Long 		    &periph->path->device->inq_data;
59152c9ce25SScott Long 
59252c9ce25SScott Long 		if (softc->action == PROBE_INQUIRY)
59352c9ce25SScott Long 			inquiry_len = SHORT_INQUIRY_LENGTH;
59452c9ce25SScott Long 		else
59552c9ce25SScott Long 			inquiry_len = SID_ADDITIONAL_LENGTH(inq_buf);
59652c9ce25SScott Long 		/*
59752c9ce25SScott Long 		 * Some parallel SCSI devices fail to send an
59852c9ce25SScott Long 		 * ignore wide residue message when dealing with
59952c9ce25SScott Long 		 * odd length inquiry requests.  Round up to be
60052c9ce25SScott Long 		 * safe.
60152c9ce25SScott Long 		 */
60252c9ce25SScott Long 		inquiry_len = roundup2(inquiry_len, 2);
60352c9ce25SScott Long 		scsi_inquiry(csio,
60452c9ce25SScott Long 			     /*retries*/1,
60552c9ce25SScott Long 			     probedone,
60652c9ce25SScott Long 			     MSG_SIMPLE_Q_TAG,
60752c9ce25SScott Long 			     (u_int8_t *)inq_buf,
60852c9ce25SScott Long 			     inquiry_len,
60952c9ce25SScott Long 			     /*evpd*/FALSE,
61052c9ce25SScott Long 			     /*page_code*/0,
61152c9ce25SScott Long 			     SSD_MIN_SIZE,
61252c9ce25SScott Long 			     /*timeout*/60 * 1000);
61352c9ce25SScott Long 		break;
61452c9ce25SScott Long 	}
61552c9ce25SScott Long 	case PROBE_PM_PID:
61652c9ce25SScott Long 		cam_fill_ataio(ataio,
61752c9ce25SScott Long 		      1,
61852c9ce25SScott Long 		      probedone,
61952c9ce25SScott Long 		      /*flags*/CAM_DIR_NONE,
62065d0fb03SAlexander Motin 		      0,
62152c9ce25SScott Long 		      /*data_ptr*/NULL,
62252c9ce25SScott Long 		      /*dxfer_len*/0,
62352c9ce25SScott Long 		      10 * 1000);
62452c9ce25SScott Long 		ata_pm_read_cmd(ataio, 0, 15);
62552c9ce25SScott Long 		break;
62652c9ce25SScott Long 	case PROBE_PM_PRV:
62752c9ce25SScott Long 		cam_fill_ataio(ataio,
62852c9ce25SScott Long 		      1,
62952c9ce25SScott Long 		      probedone,
63052c9ce25SScott Long 		      /*flags*/CAM_DIR_NONE,
63165d0fb03SAlexander Motin 		      0,
63252c9ce25SScott Long 		      /*data_ptr*/NULL,
63352c9ce25SScott Long 		      /*dxfer_len*/0,
63452c9ce25SScott Long 		      10 * 1000);
63552c9ce25SScott Long 		ata_pm_read_cmd(ataio, 1, 15);
63652c9ce25SScott Long 		break;
6373089bb2eSAlexander Motin 	case PROBE_IDENTIFY_SES:
6383089bb2eSAlexander Motin 		cam_fill_ataio(ataio,
6393089bb2eSAlexander Motin 		      1,
6403089bb2eSAlexander Motin 		      probedone,
6413089bb2eSAlexander Motin 		      /*flags*/CAM_DIR_IN,
6423089bb2eSAlexander Motin 		      0,
6433089bb2eSAlexander Motin 		      /*data_ptr*/(u_int8_t *)&softc->ident_data,
6443089bb2eSAlexander Motin 		      /*dxfer_len*/sizeof(softc->ident_data),
6453089bb2eSAlexander Motin 		      30 * 1000);
6463089bb2eSAlexander Motin 		ata_28bit_cmd(ataio, ATA_SEP_ATTN, 0xEC, 0x02,
6473089bb2eSAlexander Motin 		    sizeof(softc->ident_data) / 4);
6483089bb2eSAlexander Motin 		break;
6493089bb2eSAlexander Motin 	case PROBE_IDENTIFY_SAFTE:
6503089bb2eSAlexander Motin 		cam_fill_ataio(ataio,
6513089bb2eSAlexander Motin 		      1,
6523089bb2eSAlexander Motin 		      probedone,
6533089bb2eSAlexander Motin 		      /*flags*/CAM_DIR_IN,
6543089bb2eSAlexander Motin 		      0,
6553089bb2eSAlexander Motin 		      /*data_ptr*/(u_int8_t *)&softc->ident_data,
6563089bb2eSAlexander Motin 		      /*dxfer_len*/sizeof(softc->ident_data),
6573089bb2eSAlexander Motin 		      30 * 1000);
6583089bb2eSAlexander Motin 		ata_28bit_cmd(ataio, ATA_SEP_ATTN, 0xEC, 0x00,
6593089bb2eSAlexander Motin 		    sizeof(softc->ident_data) / 4);
6603089bb2eSAlexander Motin 		break;
66152c9ce25SScott Long 	default:
662a4d953c4SAlexander Motin 		panic("probestart: invalid action state 0x%x\n", softc->action);
66352c9ce25SScott Long 	}
66452c9ce25SScott Long 	xpt_action(start_ccb);
66552c9ce25SScott Long }
666b9c473b2SAlexander Motin 
66752c9ce25SScott Long static void
66852c9ce25SScott Long proberequestdefaultnegotiation(struct cam_periph *periph)
66952c9ce25SScott Long {
67052c9ce25SScott Long 	struct ccb_trans_settings cts;
67152c9ce25SScott Long 
67283c5d981SAlexander Motin 	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
67352c9ce25SScott Long 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
67452c9ce25SScott Long 	cts.type = CTS_TYPE_USER_SETTINGS;
67552c9ce25SScott Long 	xpt_action((union ccb *)&cts);
676b9c473b2SAlexander Motin 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
67752c9ce25SScott Long 		return;
678b9c473b2SAlexander Motin 	cts.xport_specific.valid = 0;
67952c9ce25SScott Long 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
68052c9ce25SScott Long 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
68152c9ce25SScott Long 	xpt_action((union ccb *)&cts);
68252c9ce25SScott Long }
68352c9ce25SScott Long 
68452c9ce25SScott Long static void
68552c9ce25SScott Long probedone(struct cam_periph *periph, union ccb *done_ccb)
68652c9ce25SScott Long {
687c8039fc6SAlexander Motin 	struct ccb_trans_settings cts;
68852c9ce25SScott Long 	struct ata_params *ident_buf;
6893089bb2eSAlexander Motin 	struct scsi_inquiry_data *inq_buf;
69052c9ce25SScott Long 	probe_softc *softc;
69152c9ce25SScott Long 	struct cam_path *path;
69226bdaeddSAlexander Motin 	cam_status status;
69352c9ce25SScott Long 	u_int32_t  priority;
694da6808c1SAlexander Motin 	u_int caps;
6953089bb2eSAlexander Motin 	int changed = 1, found = 1;
6963089bb2eSAlexander Motin 	static const uint8_t fake_device_id_hdr[8] =
6973089bb2eSAlexander Motin 	    {0, SVPD_DEVICE_ID, 0, 12,
6983089bb2eSAlexander Motin 	     SVPD_ID_CODESET_BINARY, SVPD_ID_TYPE_NAA, 0, 8};
69952c9ce25SScott Long 
70052c9ce25SScott Long 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
70152c9ce25SScott Long 
70252c9ce25SScott Long 	softc = (probe_softc *)periph->softc;
70352c9ce25SScott Long 	path = done_ccb->ccb_h.path;
70452c9ce25SScott Long 	priority = done_ccb->ccb_h.pinfo.priority;
70552c9ce25SScott Long 	ident_buf = &path->device->ident_data;
7063089bb2eSAlexander Motin 	inq_buf = &path->device->inq_data;
70752c9ce25SScott Long 
7081e637ba6SAlexander Motin 	if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
7090191d9b3SAlexander Motin 		if (cam_periph_error(done_ccb,
7100191d9b3SAlexander Motin 		    0, softc->restart ? (SF_NO_RECOVERY | SF_NO_RETRY) : 0,
7110191d9b3SAlexander Motin 		    NULL) == ERESTART)
7121e637ba6SAlexander Motin 			return;
71325a519a9SAlexander Motin 		if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
7141e637ba6SAlexander Motin 			/* Don't wedge the queue */
7151e637ba6SAlexander Motin 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
7161e637ba6SAlexander Motin 					 /*run_queue*/TRUE);
7171e637ba6SAlexander Motin 		}
71826bdaeddSAlexander Motin 		status = done_ccb->ccb_h.status & CAM_STATUS_MASK;
71925a519a9SAlexander Motin 		if (softc->restart) {
72025a519a9SAlexander Motin 			softc->faults++;
72125a519a9SAlexander Motin 			if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) ==
72225a519a9SAlexander Motin 			    CAM_CMD_TIMEOUT)
72325a519a9SAlexander Motin 				softc->faults += 4;
72425a519a9SAlexander Motin 			if (softc->faults < 10)
72525a519a9SAlexander Motin 				goto done;
72625a519a9SAlexander Motin 			else
72725a519a9SAlexander Motin 				softc->restart = 0;
72826bdaeddSAlexander Motin 
7291e637ba6SAlexander Motin 		/* Old PIO2 devices may not support mode setting. */
73026bdaeddSAlexander Motin 		} else if (softc->action == PROBE_SETMODE &&
73126bdaeddSAlexander Motin 		    status == CAM_ATA_STATUS_ERROR &&
7321e637ba6SAlexander Motin 		    ata_max_pmode(ident_buf) <= ATA_PIO2 &&
73326bdaeddSAlexander Motin 		    (ident_buf->capabilities1 & ATA_SUPPORT_IORDY) == 0) {
7341e637ba6SAlexander Motin 			goto noerror;
73526bdaeddSAlexander Motin 
73626bdaeddSAlexander Motin 		/*
73726bdaeddSAlexander Motin 		 * Some old WD SATA disks report supported and enabled
73826bdaeddSAlexander Motin 		 * device-initiated interface power management, but return
73926bdaeddSAlexander Motin 		 * ABORT on attempt to disable it.
74026bdaeddSAlexander Motin 		 */
74126bdaeddSAlexander Motin 		} else if (softc->action == PROBE_SETPM &&
74226bdaeddSAlexander Motin 		    status == CAM_ATA_STATUS_ERROR) {
74326bdaeddSAlexander Motin 			goto noerror;
744025e2c12SAlexander Motin 
745025e2c12SAlexander Motin 		/*
746025e2c12SAlexander Motin 		 * Some HP SATA disks report supported DMA Auto-Activation,
747025e2c12SAlexander Motin 		 * but return ABORT on attempt to enable it.
748025e2c12SAlexander Motin 		 */
749025e2c12SAlexander Motin 		} else if (softc->action == PROBE_SETDMAAA &&
750025e2c12SAlexander Motin 		    status == CAM_ATA_STATUS_ERROR) {
751025e2c12SAlexander Motin 			goto noerror;
7523089bb2eSAlexander Motin 
7533089bb2eSAlexander Motin 		/*
75409dff101SAlexander Motin 		 * Some Samsung SSDs report supported Asynchronous Notification,
75509dff101SAlexander Motin 		 * but return ABORT on attempt to enable it.
75609dff101SAlexander Motin 		 */
75709dff101SAlexander Motin 		} else if (softc->action == PROBE_SETAN &&
75809dff101SAlexander Motin 		    status == CAM_ATA_STATUS_ERROR) {
75909dff101SAlexander Motin 			goto noerror;
76009dff101SAlexander Motin 
76109dff101SAlexander Motin 		/*
7623089bb2eSAlexander Motin 		 * SES and SAF-TE SEPs have different IDENTIFY commands,
7633089bb2eSAlexander Motin 		 * but SATA specification doesn't tell how to identify them.
7643089bb2eSAlexander Motin 		 * Until better way found, just try another if first fail.
7653089bb2eSAlexander Motin 		 */
7663089bb2eSAlexander Motin 		} else if (softc->action == PROBE_IDENTIFY_SES &&
7673089bb2eSAlexander Motin 		    status == CAM_ATA_STATUS_ERROR) {
7683089bb2eSAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_IDENTIFY_SAFTE);
7693089bb2eSAlexander Motin 			xpt_release_ccb(done_ccb);
7703089bb2eSAlexander Motin 			xpt_schedule(periph, priority);
7713089bb2eSAlexander Motin 			return;
77226bdaeddSAlexander Motin 		}
77326bdaeddSAlexander Motin 
7741e637ba6SAlexander Motin 		/*
7751e637ba6SAlexander Motin 		 * If we get to this point, we got an error status back
7761e637ba6SAlexander Motin 		 * from the inquiry and the error status doesn't require
7771e637ba6SAlexander Motin 		 * automatically retrying the command.  Therefore, the
7781e637ba6SAlexander Motin 		 * inquiry failed.  If we had inquiry information before
7791e637ba6SAlexander Motin 		 * for this device, but this latest inquiry command failed,
7801e637ba6SAlexander Motin 		 * the device has probably gone away.  If this device isn't
7811e637ba6SAlexander Motin 		 * already marked unconfigured, notify the peripheral
7821e637ba6SAlexander Motin 		 * drivers that this device is no more.
7831e637ba6SAlexander Motin 		 */
78425a519a9SAlexander Motin device_fail:	if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
7851e637ba6SAlexander Motin 			xpt_async(AC_LOST_DEVICE, path, NULL);
786a4d953c4SAlexander Motin 		PROBE_SET_ACTION(softc, PROBE_INVALID);
7871e637ba6SAlexander Motin 		found = 0;
7881e637ba6SAlexander Motin 		goto done;
7891e637ba6SAlexander Motin 	}
7901e637ba6SAlexander Motin noerror:
791a9b8edb1SAlexander Motin 	if (softc->restart)
792a9b8edb1SAlexander Motin 		goto done;
79352c9ce25SScott Long 	switch (softc->action) {
79452c9ce25SScott Long 	case PROBE_RESET:
7951e637ba6SAlexander Motin 	{
79652c9ce25SScott Long 		int sign = (done_ccb->ataio.res.lba_high << 8) +
79752c9ce25SScott Long 		    done_ccb->ataio.res.lba_mid;
798a4d953c4SAlexander Motin 		CAM_DEBUG(path, CAM_DEBUG_PROBE,
799a4d953c4SAlexander Motin 		    ("SIGNATURE: %04x\n", sign));
80052c9ce25SScott Long 		if (sign == 0x0000 &&
80152c9ce25SScott Long 		    done_ccb->ccb_h.target_id != 15) {
80252c9ce25SScott Long 			path->device->protocol = PROTO_ATA;
80352c9ce25SScott Long 			PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
80452c9ce25SScott Long 		} else if (sign == 0x9669 &&
80552c9ce25SScott Long 		    done_ccb->ccb_h.target_id == 15) {
80652c9ce25SScott Long 			/* Report SIM that PM is present. */
80752c9ce25SScott Long 			bzero(&cts, sizeof(cts));
80883c5d981SAlexander Motin 			xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
80952c9ce25SScott Long 			cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
81052c9ce25SScott Long 			cts.type = CTS_TYPE_CURRENT_SETTINGS;
81152c9ce25SScott Long 			cts.xport_specific.sata.pm_present = 1;
81252c9ce25SScott Long 			cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
81352c9ce25SScott Long 			xpt_action((union ccb *)&cts);
81452c9ce25SScott Long 			path->device->protocol = PROTO_SATAPM;
81552c9ce25SScott Long 			PROBE_SET_ACTION(softc, PROBE_PM_PID);
8163089bb2eSAlexander Motin 		} else if (sign == 0xc33c &&
8173089bb2eSAlexander Motin 		    done_ccb->ccb_h.target_id != 15) {
8183089bb2eSAlexander Motin 			path->device->protocol = PROTO_SEMB;
8193089bb2eSAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_IDENTIFY_SES);
82052c9ce25SScott Long 		} else if (sign == 0xeb14 &&
82152c9ce25SScott Long 		    done_ccb->ccb_h.target_id != 15) {
82252c9ce25SScott Long 			path->device->protocol = PROTO_SCSI;
82352c9ce25SScott Long 			PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
82452c9ce25SScott Long 		} else {
82552c9ce25SScott Long 			if (done_ccb->ccb_h.target_id != 15) {
82652c9ce25SScott Long 				xpt_print(path,
82752c9ce25SScott Long 				    "Unexpected signature 0x%04x\n", sign);
82852c9ce25SScott Long 			}
82965d0fb03SAlexander Motin 			goto device_fail;
83052c9ce25SScott Long 		}
83152c9ce25SScott Long 		xpt_release_ccb(done_ccb);
83252c9ce25SScott Long 		xpt_schedule(periph, priority);
83352c9ce25SScott Long 		return;
83452c9ce25SScott Long 	}
83552c9ce25SScott Long 	case PROBE_IDENTIFY:
83652c9ce25SScott Long 	{
837699f853bSAlexander Motin 		struct ccb_pathinq cpi;
83852c9ce25SScott Long 		int16_t *ptr;
83952c9ce25SScott Long 
840a9b8edb1SAlexander Motin 		ident_buf = &softc->ident_data;
84152c9ce25SScott Long 		for (ptr = (int16_t *)ident_buf;
84252c9ce25SScott Long 		     ptr < (int16_t *)ident_buf + sizeof(struct ata_params)/2; ptr++) {
84352c9ce25SScott Long 			*ptr = le16toh(*ptr);
84452c9ce25SScott Long 		}
84552c9ce25SScott Long 		if (strncmp(ident_buf->model, "FX", 2) &&
84652c9ce25SScott Long 		    strncmp(ident_buf->model, "NEC", 3) &&
84752c9ce25SScott Long 		    strncmp(ident_buf->model, "Pioneer", 7) &&
84852c9ce25SScott Long 		    strncmp(ident_buf->model, "SHARP", 5)) {
84952c9ce25SScott Long 			ata_bswap(ident_buf->model, sizeof(ident_buf->model));
85052c9ce25SScott Long 			ata_bswap(ident_buf->revision, sizeof(ident_buf->revision));
85152c9ce25SScott Long 			ata_bswap(ident_buf->serial, sizeof(ident_buf->serial));
85252c9ce25SScott Long 		}
85352c9ce25SScott Long 		ata_btrim(ident_buf->model, sizeof(ident_buf->model));
85452c9ce25SScott Long 		ata_bpack(ident_buf->model, ident_buf->model, sizeof(ident_buf->model));
85552c9ce25SScott Long 		ata_btrim(ident_buf->revision, sizeof(ident_buf->revision));
85652c9ce25SScott Long 		ata_bpack(ident_buf->revision, ident_buf->revision, sizeof(ident_buf->revision));
85752c9ce25SScott Long 		ata_btrim(ident_buf->serial, sizeof(ident_buf->serial));
85852c9ce25SScott Long 		ata_bpack(ident_buf->serial, ident_buf->serial, sizeof(ident_buf->serial));
8594ef08dc5SAlexander Motin 		/* Device may need spin-up before IDENTIFY become valid. */
8601254b680SAlexander Motin 		if ((ident_buf->specconf == 0x37c8 ||
8611254b680SAlexander Motin 		     ident_buf->specconf == 0x738c) &&
8621254b680SAlexander Motin 		    ((ident_buf->config & ATA_RESP_INCOMPLETE) ||
8634ef08dc5SAlexander Motin 		     softc->spinup == 0)) {
8644ef08dc5SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_SPINUP);
8654ef08dc5SAlexander Motin 			xpt_release_ccb(done_ccb);
8664ef08dc5SAlexander Motin 			xpt_schedule(periph, priority);
8674ef08dc5SAlexander Motin 			return;
8684ef08dc5SAlexander Motin 		}
869a9b8edb1SAlexander Motin 		ident_buf = &path->device->ident_data;
87052c9ce25SScott Long 		if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
87152c9ce25SScott Long 			/* Check that it is the same device. */
872a9b8edb1SAlexander Motin 			if (bcmp(softc->ident_data.model, ident_buf->model,
873a9b8edb1SAlexander Motin 			     sizeof(ident_buf->model)) ||
874a9b8edb1SAlexander Motin 			    bcmp(softc->ident_data.revision, ident_buf->revision,
875a9b8edb1SAlexander Motin 			     sizeof(ident_buf->revision)) ||
876a9b8edb1SAlexander Motin 			    bcmp(softc->ident_data.serial, ident_buf->serial,
877a9b8edb1SAlexander Motin 			     sizeof(ident_buf->serial))) {
87852c9ce25SScott Long 				/* Device changed. */
87952c9ce25SScott Long 				xpt_async(AC_LOST_DEVICE, path, NULL);
8801e637ba6SAlexander Motin 			} else {
881a9b8edb1SAlexander Motin 				bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
8822eb4a8dcSAlexander Motin 				changed = 0;
8832eb4a8dcSAlexander Motin 			}
8842eb4a8dcSAlexander Motin 		}
8852eb4a8dcSAlexander Motin 		if (changed) {
8862eb4a8dcSAlexander Motin 			bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
88752c9ce25SScott Long 			/* Clean up from previous instance of this device */
88852c9ce25SScott Long 			if (path->device->serial_num != NULL) {
88952c9ce25SScott Long 				free(path->device->serial_num, M_CAMXPT);
89052c9ce25SScott Long 				path->device->serial_num = NULL;
89152c9ce25SScott Long 				path->device->serial_num_len = 0;
89252c9ce25SScott Long 			}
8933089bb2eSAlexander Motin 			if (path->device->device_id != NULL) {
8943089bb2eSAlexander Motin 				free(path->device->device_id, M_CAMXPT);
8953089bb2eSAlexander Motin 				path->device->device_id = NULL;
8963089bb2eSAlexander Motin 				path->device->device_id_len = 0;
8973089bb2eSAlexander Motin 			}
89852c9ce25SScott Long 			path->device->serial_num =
89952c9ce25SScott Long 				(u_int8_t *)malloc((sizeof(ident_buf->serial) + 1),
90052c9ce25SScott Long 					   M_CAMXPT, M_NOWAIT);
90152c9ce25SScott Long 			if (path->device->serial_num != NULL) {
90252c9ce25SScott Long 				bcopy(ident_buf->serial,
90352c9ce25SScott Long 				      path->device->serial_num,
90452c9ce25SScott Long 				      sizeof(ident_buf->serial));
90552c9ce25SScott Long 				path->device->serial_num[sizeof(ident_buf->serial)]
90652c9ce25SScott Long 				    = '\0';
90752c9ce25SScott Long 				path->device->serial_num_len =
90852c9ce25SScott Long 				    strlen(path->device->serial_num);
90952c9ce25SScott Long 			}
9103089bb2eSAlexander Motin 			if (ident_buf->enabled.extension &
9113089bb2eSAlexander Motin 			    ATA_SUPPORT_64BITWWN) {
9123089bb2eSAlexander Motin 				path->device->device_id =
9133089bb2eSAlexander Motin 				    malloc(16, M_CAMXPT, M_NOWAIT);
9143089bb2eSAlexander Motin 				if (path->device->device_id != NULL) {
9153089bb2eSAlexander Motin 					path->device->device_id_len = 16;
9163089bb2eSAlexander Motin 					bcopy(&fake_device_id_hdr,
9173089bb2eSAlexander Motin 					    path->device->device_id, 8);
9183089bb2eSAlexander Motin 					bcopy(ident_buf->wwn,
9193089bb2eSAlexander Motin 					    path->device->device_id + 8, 8);
9203089bb2eSAlexander Motin 				}
9213089bb2eSAlexander Motin 			}
92252c9ce25SScott Long 
9234b997c49SAlexander Motin 			path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
924581b2e78SAlexander Motin 			xpt_async(AC_GETDEV_CHANGED, path, NULL);
9251e637ba6SAlexander Motin 		}
92630a4094fSAlexander Motin 		if (ident_buf->satacapabilities & ATA_SUPPORT_NCQ) {
9277dc3213dSAlexander Motin 			path->device->mintags = 2;
9287dc3213dSAlexander Motin 			path->device->maxtags =
92930a4094fSAlexander Motin 			    ATA_QUEUE_LEN(ident_buf->queue) + 1;
93030a4094fSAlexander Motin 		}
93130a4094fSAlexander Motin 		ata_find_quirk(path->device);
932507e5811SAlexander Motin 		if (path->device->mintags != 0 &&
933507e5811SAlexander Motin 		    path->bus->sim->max_tagged_dev_openings != 0) {
934699f853bSAlexander Motin 			/* Check if the SIM does not want queued commands. */
935699f853bSAlexander Motin 			bzero(&cpi, sizeof(cpi));
936699f853bSAlexander Motin 			xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
937699f853bSAlexander Motin 			cpi.ccb_h.func_code = XPT_PATH_INQ;
938699f853bSAlexander Motin 			xpt_action((union ccb *)&cpi);
939699f853bSAlexander Motin 			if (cpi.ccb_h.status == CAM_REQ_CMP &&
940699f853bSAlexander Motin 			    (cpi.hba_inquiry & PI_TAG_ABLE)) {
941c8039fc6SAlexander Motin 				/* Report SIM which tags are allowed. */
942c8039fc6SAlexander Motin 				bzero(&cts, sizeof(cts));
94383c5d981SAlexander Motin 				xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
944c8039fc6SAlexander Motin 				cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
945c8039fc6SAlexander Motin 				cts.type = CTS_TYPE_CURRENT_SETTINGS;
946c8039fc6SAlexander Motin 				cts.xport_specific.sata.tags = path->device->maxtags;
947c8039fc6SAlexander Motin 				cts.xport_specific.sata.valid = CTS_SATA_VALID_TAGS;
948c8039fc6SAlexander Motin 				xpt_action((union ccb *)&cts);
94930a4094fSAlexander Motin 			}
950699f853bSAlexander Motin 		}
951bc1bf6e8SAlexander Motin 		ata_device_transport(path);
952b9c473b2SAlexander Motin 		if (changed)
953b9c473b2SAlexander Motin 			proberequestdefaultnegotiation(periph);
95452c9ce25SScott Long 		PROBE_SET_ACTION(softc, PROBE_SETMODE);
95552c9ce25SScott Long 		xpt_release_ccb(done_ccb);
95652c9ce25SScott Long 		xpt_schedule(periph, priority);
95752c9ce25SScott Long 		return;
95852c9ce25SScott Long 	}
9594ef08dc5SAlexander Motin 	case PROBE_SPINUP:
9604ef08dc5SAlexander Motin 		if (bootverbose)
9614ef08dc5SAlexander Motin 			xpt_print(path, "Spin-up done\n");
9624ef08dc5SAlexander Motin 		softc->spinup = 1;
9634ef08dc5SAlexander Motin 		PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
9644ef08dc5SAlexander Motin 		xpt_release_ccb(done_ccb);
9654ef08dc5SAlexander Motin 		xpt_schedule(periph, priority);
9664ef08dc5SAlexander Motin 		return;
96752c9ce25SScott Long 	case PROBE_SETMODE:
968da6808c1SAlexander Motin 		/* Set supported bits. */
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_GET_TRAN_SETTINGS;
972da6808c1SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
973da6808c1SAlexander Motin 		xpt_action((union ccb *)&cts);
9742e1eb332SMarius Strobl 		if (path->device->transport == XPORT_SATA &&
9752e1eb332SMarius Strobl 		    cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
976da6808c1SAlexander Motin 			caps = cts.xport_specific.sata.caps & CTS_SATA_CAPS_H;
9772e1eb332SMarius Strobl 		else if (path->device->transport == XPORT_ATA &&
9782e1eb332SMarius Strobl 		    cts.xport_specific.ata.valid & CTS_ATA_VALID_CAPS)
9792e1eb332SMarius Strobl 			caps = cts.xport_specific.ata.caps & CTS_ATA_CAPS_H;
980da6808c1SAlexander Motin 		else
981da6808c1SAlexander Motin 			caps = 0;
9822e1eb332SMarius Strobl 		if (path->device->transport == XPORT_SATA &&
9832e1eb332SMarius Strobl 		    ident_buf->satacapabilities != 0xffff) {
984da6808c1SAlexander Motin 			if (ident_buf->satacapabilities & ATA_SUPPORT_IFPWRMNGTRCV)
985da6808c1SAlexander Motin 				caps |= CTS_SATA_CAPS_D_PMREQ;
986da6808c1SAlexander Motin 			if (ident_buf->satacapabilities & ATA_SUPPORT_HAPST)
987da6808c1SAlexander Motin 				caps |= CTS_SATA_CAPS_D_APST;
988da6808c1SAlexander Motin 		}
989da6808c1SAlexander Motin 		/* Mask unwanted bits. */
990da6808c1SAlexander Motin 		bzero(&cts, sizeof(cts));
991da6808c1SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
992da6808c1SAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
993da6808c1SAlexander Motin 		cts.type = CTS_TYPE_USER_SETTINGS;
994da6808c1SAlexander Motin 		xpt_action((union ccb *)&cts);
9952e1eb332SMarius Strobl 		if (path->device->transport == XPORT_SATA &&
9962e1eb332SMarius Strobl 		    cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
997da6808c1SAlexander Motin 			caps &= cts.xport_specific.sata.caps;
9982e1eb332SMarius Strobl 		else if (path->device->transport == XPORT_ATA &&
9992e1eb332SMarius Strobl 		    cts.xport_specific.ata.valid & CTS_ATA_VALID_CAPS)
10002e1eb332SMarius Strobl 			caps &= cts.xport_specific.ata.caps;
1001c2b82f3eSAlexander Motin 		else
1002c2b82f3eSAlexander Motin 			caps = 0;
10032e1eb332SMarius Strobl 		/*
10042e1eb332SMarius Strobl 		 * Remember what transport thinks about 48-bit DMA.  If
10052e1eb332SMarius Strobl 		 * capability information is not provided or transport is
10062e1eb332SMarius Strobl 		 * SATA, we take support for granted.
10072e1eb332SMarius Strobl 		 */
10082e1eb332SMarius Strobl 		if (!(path->device->inq_flags & SID_DMA) ||
10092e1eb332SMarius Strobl 		    (path->device->transport == XPORT_ATA &&
10102e1eb332SMarius Strobl 		    (cts.xport_specific.ata.valid & CTS_ATA_VALID_CAPS) &&
10112e1eb332SMarius Strobl 		    !(caps & CTS_ATA_CAPS_H_DMA48)))
10122e1eb332SMarius Strobl 			path->device->inq_flags &= ~SID_DMA48;
10132e1eb332SMarius Strobl 		else
10142e1eb332SMarius Strobl 			path->device->inq_flags |= SID_DMA48;
1015da6808c1SAlexander Motin 		/* Store result to SIM. */
1016da6808c1SAlexander Motin 		bzero(&cts, sizeof(cts));
1017da6808c1SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1018da6808c1SAlexander Motin 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1019da6808c1SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
10202e1eb332SMarius Strobl 		if (path->device->transport == XPORT_SATA) {
1021da6808c1SAlexander Motin 			cts.xport_specific.sata.caps = caps;
1022da6808c1SAlexander Motin 			cts.xport_specific.sata.valid = CTS_SATA_VALID_CAPS;
10232e1eb332SMarius Strobl 		} else {
10242e1eb332SMarius Strobl 			cts.xport_specific.ata.caps = caps;
10252e1eb332SMarius Strobl 			cts.xport_specific.ata.valid = CTS_ATA_VALID_CAPS;
10262e1eb332SMarius Strobl 		}
1027da6808c1SAlexander Motin 		xpt_action((union ccb *)&cts);
1028da6808c1SAlexander Motin 		softc->caps = caps;
10292e1eb332SMarius Strobl 		if (path->device->transport != XPORT_SATA)
10302e1eb332SMarius Strobl 			goto notsata;
1031958e4a69SAlexander Motin 		if ((ident_buf->satasupport & ATA_SUPPORT_IFPWRMNGT) &&
1032958e4a69SAlexander Motin 		    (!(softc->caps & CTS_SATA_CAPS_H_PMREQ)) !=
1033958e4a69SAlexander Motin 		    (!(ident_buf->sataenabled & ATA_SUPPORT_IFPWRMNGT))) {
1034da6808c1SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_SETPM);
1035da6808c1SAlexander Motin 			xpt_release_ccb(done_ccb);
1036da6808c1SAlexander Motin 			xpt_schedule(periph, priority);
1037da6808c1SAlexander Motin 			return;
1038da6808c1SAlexander Motin 		}
1039da6808c1SAlexander Motin 		/* FALLTHROUGH */
1040da6808c1SAlexander Motin 	case PROBE_SETPM:
1041da6808c1SAlexander Motin 		if (ident_buf->satacapabilities != 0xffff &&
1042958e4a69SAlexander Motin 		    (ident_buf->satacapabilities & ATA_SUPPORT_DAPST) &&
1043958e4a69SAlexander Motin 		    (!(softc->caps & CTS_SATA_CAPS_H_APST)) !=
1044958e4a69SAlexander Motin 		    (!(ident_buf->sataenabled & ATA_ENABLED_DAPST))) {
1045da6808c1SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_SETAPST);
1046da6808c1SAlexander Motin 			xpt_release_ccb(done_ccb);
1047da6808c1SAlexander Motin 			xpt_schedule(periph, priority);
1048da6808c1SAlexander Motin 			return;
1049da6808c1SAlexander Motin 		}
1050da6808c1SAlexander Motin 		/* FALLTHROUGH */
1051da6808c1SAlexander Motin 	case PROBE_SETAPST:
1052958e4a69SAlexander Motin 		if ((ident_buf->satasupport & ATA_SUPPORT_AUTOACTIVATE) &&
1053958e4a69SAlexander Motin 		    (!(softc->caps & CTS_SATA_CAPS_H_DMAAA)) !=
1054958e4a69SAlexander Motin 		    (!(ident_buf->sataenabled & ATA_SUPPORT_AUTOACTIVATE))) {
1055da6808c1SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_SETDMAAA);
1056da6808c1SAlexander Motin 			xpt_release_ccb(done_ccb);
1057da6808c1SAlexander Motin 			xpt_schedule(periph, priority);
1058da6808c1SAlexander Motin 			return;
1059da6808c1SAlexander Motin 		}
1060da6808c1SAlexander Motin 		/* FALLTHROUGH */
1061da6808c1SAlexander Motin 	case PROBE_SETDMAAA:
10628d169381SAlexander Motin 		if ((ident_buf->satasupport & ATA_SUPPORT_ASYNCNOTIF) &&
10638d169381SAlexander Motin 		    (!(softc->caps & CTS_SATA_CAPS_H_AN)) !=
10648d169381SAlexander Motin 		    (!(ident_buf->sataenabled & ATA_SUPPORT_ASYNCNOTIF))) {
10658d169381SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_SETAN);
10668d169381SAlexander Motin 			xpt_release_ccb(done_ccb);
10678d169381SAlexander Motin 			xpt_schedule(periph, priority);
10688d169381SAlexander Motin 			return;
10698d169381SAlexander Motin 		}
10708d169381SAlexander Motin 		/* FALLTHROUGH */
10718d169381SAlexander Motin 	case PROBE_SETAN:
1072da6808c1SAlexander Motin notsata:
10731e637ba6SAlexander Motin 		if (path->device->protocol == PROTO_ATA) {
10741e637ba6SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_SET_MULTI);
10751e637ba6SAlexander Motin 		} else {
10761e637ba6SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_INQUIRY);
10771e637ba6SAlexander Motin 		}
10781e637ba6SAlexander Motin 		xpt_release_ccb(done_ccb);
10791e637ba6SAlexander Motin 		xpt_schedule(periph, priority);
10801e637ba6SAlexander Motin 		return;
10811e637ba6SAlexander Motin 	case PROBE_SET_MULTI:
10821e637ba6SAlexander Motin 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
108352c9ce25SScott Long 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1084f98d7a47SAlexander Motin 			xpt_acquire_device(path->device);
108552c9ce25SScott Long 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
108652c9ce25SScott Long 			xpt_action(done_ccb);
108752c9ce25SScott Long 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
108852c9ce25SScott Long 			    done_ccb);
10891e637ba6SAlexander Motin 		}
1090a4d953c4SAlexander Motin 		PROBE_SET_ACTION(softc, PROBE_DONE);
109152c9ce25SScott Long 		break;
109252c9ce25SScott Long 	case PROBE_INQUIRY:
109352c9ce25SScott Long 	case PROBE_FULL_INQUIRY:
109452c9ce25SScott Long 	{
10951e637ba6SAlexander Motin 		u_int8_t periph_qual, len;
109652c9ce25SScott Long 
109752c9ce25SScott Long 		path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
109852c9ce25SScott Long 
109952c9ce25SScott Long 		periph_qual = SID_QUAL(inq_buf);
110052c9ce25SScott Long 
11011e637ba6SAlexander Motin 		if (periph_qual != SID_QUAL_LU_CONNECTED)
11021e637ba6SAlexander Motin 			break;
110352c9ce25SScott Long 
110452c9ce25SScott Long 		/*
110552c9ce25SScott Long 		 * We conservatively request only
110652c9ce25SScott Long 		 * SHORT_INQUIRY_LEN bytes of inquiry
110752c9ce25SScott Long 		 * information during our first try
110852c9ce25SScott Long 		 * at sending an INQUIRY. If the device
110952c9ce25SScott Long 		 * has more information to give,
111052c9ce25SScott Long 		 * perform a second request specifying
111152c9ce25SScott Long 		 * the amount of information the device
111252c9ce25SScott Long 		 * is willing to give.
111352c9ce25SScott Long 		 */
111452c9ce25SScott Long 		len = inq_buf->additional_length
11151e637ba6SAlexander Motin 		    + offsetof(struct scsi_inquiry_data, additional_length) + 1;
111652c9ce25SScott Long 		if (softc->action == PROBE_INQUIRY
111752c9ce25SScott Long 		    && len > SHORT_INQUIRY_LENGTH) {
111852c9ce25SScott Long 			PROBE_SET_ACTION(softc, PROBE_FULL_INQUIRY);
111952c9ce25SScott Long 			xpt_release_ccb(done_ccb);
112052c9ce25SScott Long 			xpt_schedule(periph, priority);
112152c9ce25SScott Long 			return;
112252c9ce25SScott Long 		}
112352c9ce25SScott Long 
11244b997c49SAlexander Motin 		ata_device_transport(path);
11251e637ba6SAlexander Motin 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
112652c9ce25SScott Long 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1127f98d7a47SAlexander Motin 			xpt_acquire_device(path->device);
112852c9ce25SScott Long 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
112952c9ce25SScott Long 			xpt_action(done_ccb);
11301e637ba6SAlexander Motin 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path, done_ccb);
11311e637ba6SAlexander Motin 		}
1132a4d953c4SAlexander Motin 		PROBE_SET_ACTION(softc, PROBE_DONE);
113352c9ce25SScott Long 		break;
113452c9ce25SScott Long 	}
113552c9ce25SScott Long 	case PROBE_PM_PID:
11364b997c49SAlexander Motin 		if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) == 0)
113752c9ce25SScott Long 			bzero(ident_buf, sizeof(*ident_buf));
113852c9ce25SScott Long 		softc->pm_pid = (done_ccb->ataio.res.lba_high << 24) +
113952c9ce25SScott Long 		    (done_ccb->ataio.res.lba_mid << 16) +
114052c9ce25SScott Long 		    (done_ccb->ataio.res.lba_low << 8) +
114152c9ce25SScott Long 		    done_ccb->ataio.res.sector_count;
114265d0fb03SAlexander Motin 		((uint32_t *)ident_buf)[0] = softc->pm_pid;
114352c9ce25SScott Long 		snprintf(ident_buf->model, sizeof(ident_buf->model),
114452c9ce25SScott Long 		    "Port Multiplier %08x", softc->pm_pid);
114552c9ce25SScott Long 		PROBE_SET_ACTION(softc, PROBE_PM_PRV);
114652c9ce25SScott Long 		xpt_release_ccb(done_ccb);
114752c9ce25SScott Long 		xpt_schedule(periph, priority);
114852c9ce25SScott Long 		return;
114952c9ce25SScott Long 	case PROBE_PM_PRV:
115052c9ce25SScott Long 		softc->pm_prv = (done_ccb->ataio.res.lba_high << 24) +
115152c9ce25SScott Long 		    (done_ccb->ataio.res.lba_mid << 16) +
115252c9ce25SScott Long 		    (done_ccb->ataio.res.lba_low << 8) +
115352c9ce25SScott Long 		    done_ccb->ataio.res.sector_count;
115465d0fb03SAlexander Motin 		((uint32_t *)ident_buf)[1] = softc->pm_prv;
115552c9ce25SScott Long 		snprintf(ident_buf->revision, sizeof(ident_buf->revision),
115652c9ce25SScott Long 		    "%04x", softc->pm_prv);
11574b997c49SAlexander Motin 		path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
1158bc1bf6e8SAlexander Motin 		ata_device_transport(path);
1159bc1bf6e8SAlexander Motin 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED)
1160bc1bf6e8SAlexander Motin 			proberequestdefaultnegotiation(periph);
1161da6808c1SAlexander Motin 		/* Set supported bits. */
1162da6808c1SAlexander Motin 		bzero(&cts, sizeof(cts));
1163da6808c1SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1164da6808c1SAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1165da6808c1SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
1166da6808c1SAlexander Motin 		xpt_action((union ccb *)&cts);
1167da6808c1SAlexander Motin 		if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1168da6808c1SAlexander Motin 			caps = cts.xport_specific.sata.caps & CTS_SATA_CAPS_H;
1169da6808c1SAlexander Motin 		else
1170da6808c1SAlexander Motin 			caps = 0;
1171da6808c1SAlexander Motin 		/* All PMPs must support PM requests. */
1172da6808c1SAlexander Motin 		caps |= CTS_SATA_CAPS_D_PMREQ;
1173da6808c1SAlexander Motin 		/* Mask unwanted bits. */
1174da6808c1SAlexander Motin 		bzero(&cts, sizeof(cts));
1175da6808c1SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1176da6808c1SAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1177da6808c1SAlexander Motin 		cts.type = CTS_TYPE_USER_SETTINGS;
1178da6808c1SAlexander Motin 		xpt_action((union ccb *)&cts);
1179da6808c1SAlexander Motin 		if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1180da6808c1SAlexander Motin 			caps &= cts.xport_specific.sata.caps;
1181c2b82f3eSAlexander Motin 		else
1182c2b82f3eSAlexander Motin 			caps = 0;
11832e1eb332SMarius Strobl 		/* Remember what transport thinks about AEN. */
11842e1eb332SMarius Strobl 		if (caps & CTS_SATA_CAPS_H_AN)
11852e1eb332SMarius Strobl 			path->device->inq_flags |= SID_AEN;
11862e1eb332SMarius Strobl 		else
11872e1eb332SMarius Strobl 			path->device->inq_flags &= ~SID_AEN;
1188da6808c1SAlexander Motin 		/* Store result to SIM. */
1189da6808c1SAlexander Motin 		bzero(&cts, sizeof(cts));
1190da6808c1SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1191da6808c1SAlexander Motin 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1192da6808c1SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
1193da6808c1SAlexander Motin 		cts.xport_specific.sata.caps = caps;
1194da6808c1SAlexander Motin 		cts.xport_specific.sata.valid = CTS_SATA_VALID_CAPS;
1195da6808c1SAlexander Motin 		xpt_action((union ccb *)&cts);
1196da6808c1SAlexander Motin 		softc->caps = caps;
11973631c638SAlexander Motin 		xpt_async(AC_GETDEV_CHANGED, path, NULL);
119865d0fb03SAlexander Motin 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
119952c9ce25SScott Long 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1200f98d7a47SAlexander Motin 			xpt_acquire_device(path->device);
120152c9ce25SScott Long 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
120252c9ce25SScott Long 			xpt_action(done_ccb);
120352c9ce25SScott Long 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
120452c9ce25SScott Long 			    done_ccb);
120565d0fb03SAlexander Motin 		} else {
120665d0fb03SAlexander Motin 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
120765d0fb03SAlexander Motin 			xpt_action(done_ccb);
12081e637ba6SAlexander Motin 			xpt_async(AC_SCSI_AEN, done_ccb->ccb_h.path, done_ccb);
120952c9ce25SScott Long 		}
1210a4d953c4SAlexander Motin 		PROBE_SET_ACTION(softc, PROBE_DONE);
121152c9ce25SScott Long 		break;
12123089bb2eSAlexander Motin 	case PROBE_IDENTIFY_SES:
12133089bb2eSAlexander Motin 	case PROBE_IDENTIFY_SAFTE:
12143089bb2eSAlexander Motin 		if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
12153089bb2eSAlexander Motin 			/* Check that it is the same device. */
12163089bb2eSAlexander Motin 			if (bcmp(&softc->ident_data, ident_buf, 53)) {
12173089bb2eSAlexander Motin 				/* Device changed. */
12183089bb2eSAlexander Motin 				xpt_async(AC_LOST_DEVICE, path, NULL);
12193089bb2eSAlexander Motin 			} else {
12203089bb2eSAlexander Motin 				bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
12213089bb2eSAlexander Motin 				changed = 0;
12223089bb2eSAlexander Motin 			}
12233089bb2eSAlexander Motin 		}
12243089bb2eSAlexander Motin 		if (changed) {
12253089bb2eSAlexander Motin 			bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
12263089bb2eSAlexander Motin 			/* Clean up from previous instance of this device */
12273089bb2eSAlexander Motin 			if (path->device->device_id != NULL) {
12283089bb2eSAlexander Motin 				free(path->device->device_id, M_CAMXPT);
12293089bb2eSAlexander Motin 				path->device->device_id = NULL;
12303089bb2eSAlexander Motin 				path->device->device_id_len = 0;
12313089bb2eSAlexander Motin 			}
12323089bb2eSAlexander Motin 			path->device->device_id =
12333089bb2eSAlexander Motin 			    malloc(16, M_CAMXPT, M_NOWAIT);
12343089bb2eSAlexander Motin 			if (path->device->device_id != NULL) {
12353089bb2eSAlexander Motin 				path->device->device_id_len = 16;
12363089bb2eSAlexander Motin 				bcopy(&fake_device_id_hdr,
12373089bb2eSAlexander Motin 				    path->device->device_id, 8);
12383089bb2eSAlexander Motin 				bcopy(((uint8_t*)ident_buf) + 2,
12393089bb2eSAlexander Motin 				    path->device->device_id + 8, 8);
12403089bb2eSAlexander Motin 			}
12413089bb2eSAlexander Motin 
12423089bb2eSAlexander Motin 			path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
12433089bb2eSAlexander Motin 		}
1244bc1bf6e8SAlexander Motin 		ata_device_transport(path);
1245bc1bf6e8SAlexander Motin 		if (changed)
1246bc1bf6e8SAlexander Motin 			proberequestdefaultnegotiation(periph);
12473089bb2eSAlexander Motin 
12483089bb2eSAlexander Motin 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
12493089bb2eSAlexander Motin 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
12503089bb2eSAlexander Motin 			xpt_acquire_device(path->device);
12513089bb2eSAlexander Motin 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
12523089bb2eSAlexander Motin 			xpt_action(done_ccb);
12533089bb2eSAlexander Motin 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
12543089bb2eSAlexander Motin 			    done_ccb);
12553089bb2eSAlexander Motin 		}
1256a4d953c4SAlexander Motin 		PROBE_SET_ACTION(softc, PROBE_DONE);
12573089bb2eSAlexander Motin 		break;
125852c9ce25SScott Long 	default:
1259a4d953c4SAlexander Motin 		panic("probedone: invalid action state 0x%x\n", softc->action);
126052c9ce25SScott Long 	}
12611e637ba6SAlexander Motin done:
126283c5d981SAlexander Motin 	if (softc->restart) {
126383c5d981SAlexander Motin 		softc->restart = 0;
12641e637ba6SAlexander Motin 		xpt_release_ccb(done_ccb);
126583c5d981SAlexander Motin 		probeschedule(periph);
126683c5d981SAlexander Motin 		return;
126783c5d981SAlexander Motin 	}
126883c5d981SAlexander Motin 	xpt_release_ccb(done_ccb);
1269a4d953c4SAlexander Motin 	CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe completed\n"));
127083c5d981SAlexander Motin 	while ((done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs))) {
127183c5d981SAlexander Motin 		TAILQ_REMOVE(&softc->request_ccbs,
127283c5d981SAlexander Motin 		    &done_ccb->ccb_h, periph_links.tqe);
127383c5d981SAlexander Motin 		done_ccb->ccb_h.status = found ? CAM_REQ_CMP : CAM_REQ_CMP_ERR;
127452c9ce25SScott Long 		xpt_done(done_ccb);
127583c5d981SAlexander Motin 	}
12762e3f592bSAlexander Motin 	cam_periph_invalidate(periph);
127783c5d981SAlexander Motin 	cam_release_devq(periph->path,
127883c5d981SAlexander Motin 	    RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_XPT + 1, FALSE);
127952c9ce25SScott Long 	cam_periph_release_locked(periph);
128052c9ce25SScott Long }
128152c9ce25SScott Long 
128252c9ce25SScott Long static void
128352c9ce25SScott Long probecleanup(struct cam_periph *periph)
128452c9ce25SScott Long {
128552c9ce25SScott Long 	free(periph->softc, M_CAMXPT);
128652c9ce25SScott Long }
128752c9ce25SScott Long 
128852c9ce25SScott Long static void
128930a4094fSAlexander Motin ata_find_quirk(struct cam_ed *device)
129052c9ce25SScott Long {
129130a4094fSAlexander Motin 	struct ata_quirk_entry *quirk;
129252c9ce25SScott Long 	caddr_t	match;
129352c9ce25SScott Long 
129430a4094fSAlexander Motin 	match = cam_quirkmatch((caddr_t)&device->ident_data,
129530a4094fSAlexander Motin 			       (caddr_t)ata_quirk_table,
129630a4094fSAlexander Motin 			       ata_quirk_table_size,
129730a4094fSAlexander Motin 			       sizeof(*ata_quirk_table), ata_identify_match);
129852c9ce25SScott Long 
129952c9ce25SScott Long 	if (match == NULL)
130052c9ce25SScott Long 		panic("xpt_find_quirk: device didn't match wildcard entry!!");
130152c9ce25SScott Long 
130230a4094fSAlexander Motin 	quirk = (struct ata_quirk_entry *)match;
130352c9ce25SScott Long 	device->quirk = quirk;
13047dc3213dSAlexander Motin 	if (quirk->quirks & CAM_QUIRK_MAXTAGS) {
13057dc3213dSAlexander Motin 		device->mintags = quirk->mintags;
13067dc3213dSAlexander Motin 		device->maxtags = quirk->maxtags;
13077dc3213dSAlexander Motin 	}
130852c9ce25SScott Long }
130952c9ce25SScott Long 
131052c9ce25SScott Long typedef struct {
131152c9ce25SScott Long 	union	ccb *request_ccb;
131252c9ce25SScott Long 	struct 	ccb_pathinq *cpi;
131352c9ce25SScott Long 	int	counter;
131452c9ce25SScott Long } ata_scan_bus_info;
131552c9ce25SScott Long 
131652c9ce25SScott Long /*
131752c9ce25SScott Long  * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
131852c9ce25SScott Long  * As the scan progresses, xpt_scan_bus is used as the
131952c9ce25SScott Long  * callback on completion function.
132052c9ce25SScott Long  */
132152c9ce25SScott Long static void
132252c9ce25SScott Long ata_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
132352c9ce25SScott Long {
132452c9ce25SScott Long 	struct	cam_path *path;
132552c9ce25SScott Long 	ata_scan_bus_info *scan_info;
132683c5d981SAlexander Motin 	union	ccb *work_ccb, *reset_ccb;
132752c9ce25SScott Long 	cam_status status;
132852c9ce25SScott Long 
132952c9ce25SScott Long 	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
133052c9ce25SScott Long 		  ("xpt_scan_bus\n"));
133152c9ce25SScott Long 	switch (request_ccb->ccb_h.func_code) {
133252c9ce25SScott Long 	case XPT_SCAN_BUS:
13330e85f214SMatt Jacob 	case XPT_SCAN_TGT:
133452c9ce25SScott Long 		/* Find out the characteristics of the bus */
133552c9ce25SScott Long 		work_ccb = xpt_alloc_ccb_nowait();
133652c9ce25SScott Long 		if (work_ccb == NULL) {
133752c9ce25SScott Long 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
133852c9ce25SScott Long 			xpt_done(request_ccb);
133952c9ce25SScott Long 			return;
134052c9ce25SScott Long 		}
134152c9ce25SScott Long 		xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
134252c9ce25SScott Long 			      request_ccb->ccb_h.pinfo.priority);
134352c9ce25SScott Long 		work_ccb->ccb_h.func_code = XPT_PATH_INQ;
134452c9ce25SScott Long 		xpt_action(work_ccb);
134552c9ce25SScott Long 		if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
134652c9ce25SScott Long 			request_ccb->ccb_h.status = work_ccb->ccb_h.status;
134752c9ce25SScott Long 			xpt_free_ccb(work_ccb);
134852c9ce25SScott Long 			xpt_done(request_ccb);
134952c9ce25SScott Long 			return;
135052c9ce25SScott Long 		}
135152c9ce25SScott Long 
135283c5d981SAlexander Motin 		/* We may need to reset bus first, if we haven't done it yet. */
135383c5d981SAlexander Motin 		if ((work_ccb->cpi.hba_inquiry &
135483c5d981SAlexander Motin 		    (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) &&
135583c5d981SAlexander Motin 		    !(work_ccb->cpi.hba_misc & PIM_NOBUSRESET) &&
135683c5d981SAlexander Motin 		    !timevalisset(&request_ccb->ccb_h.path->bus->last_reset)) {
135783c5d981SAlexander Motin 			reset_ccb = xpt_alloc_ccb_nowait();
1358f1893540SAlexander Motin 			if (reset_ccb == NULL) {
1359f1893540SAlexander Motin 				request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1360f1893540SAlexander Motin 				xpt_free_ccb(work_ccb);
1361f1893540SAlexander Motin 				xpt_done(request_ccb);
1362f1893540SAlexander Motin 				return;
1363f1893540SAlexander Motin 			}
136483c5d981SAlexander Motin 			xpt_setup_ccb(&reset_ccb->ccb_h, request_ccb->ccb_h.path,
136583c5d981SAlexander Motin 			      CAM_PRIORITY_NONE);
136683c5d981SAlexander Motin 			reset_ccb->ccb_h.func_code = XPT_RESET_BUS;
136783c5d981SAlexander Motin 			xpt_action(reset_ccb);
136883c5d981SAlexander Motin 			if (reset_ccb->ccb_h.status != CAM_REQ_CMP) {
136983c5d981SAlexander Motin 				request_ccb->ccb_h.status = reset_ccb->ccb_h.status;
137083c5d981SAlexander Motin 				xpt_free_ccb(reset_ccb);
137183c5d981SAlexander Motin 				xpt_free_ccb(work_ccb);
137283c5d981SAlexander Motin 				xpt_done(request_ccb);
137383c5d981SAlexander Motin 				return;
137483c5d981SAlexander Motin 			}
137583c5d981SAlexander Motin 			xpt_free_ccb(reset_ccb);
137683c5d981SAlexander Motin 		}
137783c5d981SAlexander Motin 
137852c9ce25SScott Long 		/* Save some state for use while we probe for devices */
137952c9ce25SScott Long 		scan_info = (ata_scan_bus_info *)
138052c9ce25SScott Long 		    malloc(sizeof(ata_scan_bus_info), M_CAMXPT, M_NOWAIT);
138152c9ce25SScott Long 		if (scan_info == NULL) {
138252c9ce25SScott Long 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1383f1893540SAlexander Motin 			xpt_free_ccb(work_ccb);
138452c9ce25SScott Long 			xpt_done(request_ccb);
138552c9ce25SScott Long 			return;
138652c9ce25SScott Long 		}
138752c9ce25SScott Long 		scan_info->request_ccb = request_ccb;
138852c9ce25SScott Long 		scan_info->cpi = &work_ccb->cpi;
138952c9ce25SScott Long 		/* If PM supported, probe it first. */
139052c9ce25SScott Long 		if (scan_info->cpi->hba_inquiry & PI_SATAPM)
13910025eb12SAlexander Motin 			scan_info->counter = scan_info->cpi->max_target;
13920025eb12SAlexander Motin 		else
13930025eb12SAlexander Motin 			scan_info->counter = 0;
139452c9ce25SScott Long 
139552c9ce25SScott Long 		work_ccb = xpt_alloc_ccb_nowait();
139652c9ce25SScott Long 		if (work_ccb == NULL) {
139752c9ce25SScott Long 			free(scan_info, M_CAMXPT);
139852c9ce25SScott Long 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
139952c9ce25SScott Long 			xpt_done(request_ccb);
140052c9ce25SScott Long 			break;
140152c9ce25SScott Long 		}
140252c9ce25SScott Long 		goto scan_next;
140352c9ce25SScott Long 	case XPT_SCAN_LUN:
140452c9ce25SScott Long 		work_ccb = request_ccb;
140552c9ce25SScott Long 		/* Reuse the same CCB to query if a device was really found */
140652c9ce25SScott Long 		scan_info = (ata_scan_bus_info *)work_ccb->ccb_h.ppriv_ptr0;
140765d0fb03SAlexander Motin 		/* If there is PMP... */
14080025eb12SAlexander Motin 		if ((scan_info->cpi->hba_inquiry & PI_SATAPM) &&
14090025eb12SAlexander Motin 		    (scan_info->counter == scan_info->cpi->max_target)) {
141083c5d981SAlexander Motin 			if (work_ccb->ccb_h.status == CAM_REQ_CMP) {
141165d0fb03SAlexander Motin 				/* everything else will be probed by it */
1412e2a75189SAlexander Motin 				/* Free the current request path- we're done with it. */
1413e2a75189SAlexander Motin 				xpt_free_path(work_ccb->ccb_h.path);
14140025eb12SAlexander Motin 				goto done;
141552c9ce25SScott Long 			} else {
141652c9ce25SScott Long 				struct ccb_trans_settings cts;
141752c9ce25SScott Long 
141852c9ce25SScott Long 				/* Report SIM that PM is absent. */
141952c9ce25SScott Long 				bzero(&cts, sizeof(cts));
142052c9ce25SScott Long 				xpt_setup_ccb(&cts.ccb_h,
1421e2a75189SAlexander Motin 				    work_ccb->ccb_h.path, CAM_PRIORITY_NONE);
142252c9ce25SScott Long 				cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
142352c9ce25SScott Long 				cts.type = CTS_TYPE_CURRENT_SETTINGS;
14243ccda2f3SAlexander Motin 				cts.xport_specific.sata.pm_present = 0;
142552c9ce25SScott Long 				cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
142652c9ce25SScott Long 				xpt_action((union ccb *)&cts);
142752c9ce25SScott Long 			}
142852c9ce25SScott Long 		}
1429e2a75189SAlexander Motin 		/* Free the current request path- we're done with it. */
1430e2a75189SAlexander Motin 		xpt_free_path(work_ccb->ccb_h.path);
14310025eb12SAlexander Motin 		if (scan_info->counter ==
14320025eb12SAlexander Motin 		    ((scan_info->cpi->hba_inquiry & PI_SATAPM) ?
14330025eb12SAlexander Motin 		    0 : scan_info->cpi->max_target)) {
14340025eb12SAlexander Motin done:
143552c9ce25SScott Long 			xpt_free_ccb(work_ccb);
143652c9ce25SScott Long 			xpt_free_ccb((union ccb *)scan_info->cpi);
143752c9ce25SScott Long 			request_ccb = scan_info->request_ccb;
143852c9ce25SScott Long 			free(scan_info, M_CAMXPT);
143952c9ce25SScott Long 			request_ccb->ccb_h.status = CAM_REQ_CMP;
144052c9ce25SScott Long 			xpt_done(request_ccb);
144152c9ce25SScott Long 			break;
144252c9ce25SScott Long 		}
14430025eb12SAlexander Motin 		/* Take next device. Wrap from max (PMP) to 0. */
14440025eb12SAlexander Motin 		scan_info->counter = (scan_info->counter + 1 ) %
14450025eb12SAlexander Motin 		    (scan_info->cpi->max_target + 1);
144652c9ce25SScott Long scan_next:
144752c9ce25SScott Long 		status = xpt_create_path(&path, xpt_periph,
144852c9ce25SScott Long 		    scan_info->request_ccb->ccb_h.path_id,
144952c9ce25SScott Long 		    scan_info->counter, 0);
145052c9ce25SScott Long 		if (status != CAM_REQ_CMP) {
145152c9ce25SScott Long 			printf("xpt_scan_bus: xpt_create_path failed"
145252c9ce25SScott Long 			    " with status %#x, bus scan halted\n",
145352c9ce25SScott Long 			    status);
145452c9ce25SScott Long 			xpt_free_ccb(work_ccb);
145552c9ce25SScott Long 			xpt_free_ccb((union ccb *)scan_info->cpi);
145652c9ce25SScott Long 			request_ccb = scan_info->request_ccb;
145752c9ce25SScott Long 			free(scan_info, M_CAMXPT);
145852c9ce25SScott Long 			request_ccb->ccb_h.status = status;
145952c9ce25SScott Long 			xpt_done(request_ccb);
146052c9ce25SScott Long 			break;
146152c9ce25SScott Long 		}
146252c9ce25SScott Long 		xpt_setup_ccb(&work_ccb->ccb_h, path,
146352c9ce25SScott Long 		    scan_info->request_ccb->ccb_h.pinfo.priority);
146452c9ce25SScott Long 		work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
146552c9ce25SScott Long 		work_ccb->ccb_h.cbfcnp = ata_scan_bus;
146652c9ce25SScott Long 		work_ccb->ccb_h.ppriv_ptr0 = scan_info;
146752c9ce25SScott Long 		work_ccb->crcn.flags = scan_info->request_ccb->crcn.flags;
146852c9ce25SScott Long 		xpt_action(work_ccb);
146952c9ce25SScott Long 		break;
147052c9ce25SScott Long 	default:
147152c9ce25SScott Long 		break;
147252c9ce25SScott Long 	}
147352c9ce25SScott Long }
147452c9ce25SScott Long 
147552c9ce25SScott Long static void
147652c9ce25SScott Long ata_scan_lun(struct cam_periph *periph, struct cam_path *path,
147752c9ce25SScott Long 	     cam_flags flags, union ccb *request_ccb)
147852c9ce25SScott Long {
147952c9ce25SScott Long 	struct ccb_pathinq cpi;
148052c9ce25SScott Long 	cam_status status;
148152c9ce25SScott Long 	struct cam_path *new_path;
148252c9ce25SScott Long 	struct cam_periph *old_periph;
148352c9ce25SScott Long 
148483c5d981SAlexander Motin 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_scan_lun\n"));
148552c9ce25SScott Long 
148683c5d981SAlexander Motin 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
148752c9ce25SScott Long 	cpi.ccb_h.func_code = XPT_PATH_INQ;
148852c9ce25SScott Long 	xpt_action((union ccb *)&cpi);
148952c9ce25SScott Long 
149052c9ce25SScott Long 	if (cpi.ccb_h.status != CAM_REQ_CMP) {
149152c9ce25SScott Long 		if (request_ccb != NULL) {
149252c9ce25SScott Long 			request_ccb->ccb_h.status = cpi.ccb_h.status;
149352c9ce25SScott Long 			xpt_done(request_ccb);
149452c9ce25SScott Long 		}
149552c9ce25SScott Long 		return;
149652c9ce25SScott Long 	}
149752c9ce25SScott Long 
149852c9ce25SScott Long 	if (request_ccb == NULL) {
149932aa80a6SAlexander Motin 		request_ccb = xpt_alloc_ccb_nowait();
150052c9ce25SScott Long 		if (request_ccb == NULL) {
150152c9ce25SScott Long 			xpt_print(path, "xpt_scan_lun: can't allocate CCB, "
150252c9ce25SScott Long 			    "can't continue\n");
150352c9ce25SScott Long 			return;
150452c9ce25SScott Long 		}
150532aa80a6SAlexander Motin 		status = xpt_create_path(&new_path, xpt_periph,
150652c9ce25SScott Long 					  path->bus->path_id,
150752c9ce25SScott Long 					  path->target->target_id,
150852c9ce25SScott Long 					  path->device->lun_id);
150952c9ce25SScott Long 		if (status != CAM_REQ_CMP) {
151032aa80a6SAlexander Motin 			xpt_print(path, "xpt_scan_lun: can't create path, "
151152c9ce25SScott Long 			    "can't continue\n");
151232aa80a6SAlexander Motin 			xpt_free_ccb(request_ccb);
151352c9ce25SScott Long 			return;
151452c9ce25SScott Long 		}
151583c5d981SAlexander Motin 		xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_XPT);
151652c9ce25SScott Long 		request_ccb->ccb_h.cbfcnp = xptscandone;
151752c9ce25SScott Long 		request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
151852c9ce25SScott Long 		request_ccb->crcn.flags = flags;
151952c9ce25SScott Long 	}
152052c9ce25SScott Long 
1521f09f8e3eSAlexander Motin 	if ((old_periph = cam_periph_find(path, "aprobe")) != NULL) {
15222e3f592bSAlexander Motin 		if ((old_periph->flags & CAM_PERIPH_INVALID) == 0) {
152352c9ce25SScott Long 			probe_softc *softc;
152452c9ce25SScott Long 
152552c9ce25SScott Long 			softc = (probe_softc *)old_periph->softc;
15262e3f592bSAlexander Motin 			TAILQ_INSERT_TAIL(&softc->request_ccbs,
15272e3f592bSAlexander Motin 				&request_ccb->ccb_h, periph_links.tqe);
152883c5d981SAlexander Motin 			softc->restart = 1;
152952c9ce25SScott Long 		} else {
15302e3f592bSAlexander Motin 			request_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
15312e3f592bSAlexander Motin 			xpt_done(request_ccb);
15322e3f592bSAlexander Motin 		}
15332e3f592bSAlexander Motin 	} else {
153452c9ce25SScott Long 		status = cam_periph_alloc(proberegister, NULL, probecleanup,
1535f09f8e3eSAlexander Motin 					  probestart, "aprobe",
153652c9ce25SScott Long 					  CAM_PERIPH_BIO,
153752c9ce25SScott Long 					  request_ccb->ccb_h.path, NULL, 0,
153852c9ce25SScott Long 					  request_ccb);
153952c9ce25SScott Long 
154052c9ce25SScott Long 		if (status != CAM_REQ_CMP) {
154152c9ce25SScott Long 			xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
154252c9ce25SScott Long 			    "returned an error, can't continue probe\n");
154352c9ce25SScott Long 			request_ccb->ccb_h.status = status;
154452c9ce25SScott Long 			xpt_done(request_ccb);
154552c9ce25SScott Long 		}
154652c9ce25SScott Long 	}
154752c9ce25SScott Long }
154852c9ce25SScott Long 
154952c9ce25SScott Long static void
155052c9ce25SScott Long xptscandone(struct cam_periph *periph, union ccb *done_ccb)
155152c9ce25SScott Long {
155232aa80a6SAlexander Motin 
155332aa80a6SAlexander Motin 	xpt_free_path(done_ccb->ccb_h.path);
155432aa80a6SAlexander Motin 	xpt_free_ccb(done_ccb);
155552c9ce25SScott Long }
155652c9ce25SScott Long 
155752c9ce25SScott Long static struct cam_ed *
155852c9ce25SScott Long ata_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
155952c9ce25SScott Long {
156052c9ce25SScott Long 	struct cam_path path;
156130a4094fSAlexander Motin 	struct ata_quirk_entry *quirk;
156252c9ce25SScott Long 	struct cam_ed *device;
156352c9ce25SScott Long 
156452c9ce25SScott Long 	device = xpt_alloc_device(bus, target, lun_id);
156552c9ce25SScott Long 	if (device == NULL)
156652c9ce25SScott Long 		return (NULL);
156752c9ce25SScott Long 
156852c9ce25SScott Long 	/*
156952c9ce25SScott Long 	 * Take the default quirk entry until we have inquiry
157052c9ce25SScott Long 	 * data and can determine a better quirk to use.
157152c9ce25SScott Long 	 */
157230a4094fSAlexander Motin 	quirk = &ata_quirk_table[ata_quirk_table_size - 1];
157352c9ce25SScott Long 	device->quirk = (void *)quirk;
157430a4094fSAlexander Motin 	device->mintags = 0;
157530a4094fSAlexander Motin 	device->maxtags = 0;
157652c9ce25SScott Long 	bzero(&device->inq_data, sizeof(device->inq_data));
157752c9ce25SScott Long 	device->inq_flags = 0;
157852c9ce25SScott Long 	device->queue_flags = 0;
157952c9ce25SScott Long 	device->serial_num = NULL;
158052c9ce25SScott Long 	device->serial_num_len = 0;
158152c9ce25SScott Long 
158252c9ce25SScott Long 	/*
158352c9ce25SScott Long 	 * XXX should be limited by number of CCBs this bus can
158452c9ce25SScott Long 	 * do.
158552c9ce25SScott Long 	 */
158652c9ce25SScott Long 	bus->sim->max_ccbs += device->ccbq.devq_openings;
158752c9ce25SScott Long 	if (lun_id != CAM_LUN_WILDCARD) {
158852c9ce25SScott Long 		xpt_compile_path(&path,
158952c9ce25SScott Long 				 NULL,
159052c9ce25SScott Long 				 bus->path_id,
159152c9ce25SScott Long 				 target->target_id,
159252c9ce25SScott Long 				 lun_id);
159352c9ce25SScott Long 		ata_device_transport(&path);
159452c9ce25SScott Long 		xpt_release_path(&path);
159552c9ce25SScott Long 	}
159652c9ce25SScott Long 
159752c9ce25SScott Long 	return (device);
159852c9ce25SScott Long }
159952c9ce25SScott Long 
160052c9ce25SScott Long static void
160152c9ce25SScott Long ata_device_transport(struct cam_path *path)
160252c9ce25SScott Long {
160352c9ce25SScott Long 	struct ccb_pathinq cpi;
16044b997c49SAlexander Motin 	struct ccb_trans_settings cts;
16054b997c49SAlexander Motin 	struct scsi_inquiry_data *inq_buf = NULL;
16064b997c49SAlexander Motin 	struct ata_params *ident_buf = NULL;
160752c9ce25SScott Long 
160852c9ce25SScott Long 	/* Get transport information from the SIM */
160983c5d981SAlexander Motin 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
161052c9ce25SScott Long 	cpi.ccb_h.func_code = XPT_PATH_INQ;
161152c9ce25SScott Long 	xpt_action((union ccb *)&cpi);
161252c9ce25SScott Long 
161352c9ce25SScott Long 	path->device->transport = cpi.transport;
16144b997c49SAlexander Motin 	if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
16154b997c49SAlexander Motin 		inq_buf = &path->device->inq_data;
16164b997c49SAlexander Motin 	if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) != 0)
16174b997c49SAlexander Motin 		ident_buf = &path->device->ident_data;
16184b997c49SAlexander Motin 	if (path->device->protocol == PROTO_ATA) {
16194b997c49SAlexander Motin 		path->device->protocol_version = ident_buf ?
16204b997c49SAlexander Motin 		    ata_version(ident_buf->version_major) : cpi.protocol_version;
16214b997c49SAlexander Motin 	} else if (path->device->protocol == PROTO_SCSI) {
16224b997c49SAlexander Motin 		path->device->protocol_version = inq_buf ?
16234b997c49SAlexander Motin 		    SID_ANSI_REV(inq_buf) : cpi.protocol_version;
162452c9ce25SScott Long 	}
16254b997c49SAlexander Motin 	path->device->transport_version = ident_buf ?
16264b997c49SAlexander Motin 	    ata_version(ident_buf->version_major) : cpi.transport_version;
162752c9ce25SScott Long 
162852c9ce25SScott Long 	/* Tell the controller what we think */
162983c5d981SAlexander Motin 	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
163052c9ce25SScott Long 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
163152c9ce25SScott Long 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
163252c9ce25SScott Long 	cts.transport = path->device->transport;
163352c9ce25SScott Long 	cts.transport_version = path->device->transport_version;
163452c9ce25SScott Long 	cts.protocol = path->device->protocol;
163552c9ce25SScott Long 	cts.protocol_version = path->device->protocol_version;
163652c9ce25SScott Long 	cts.proto_specific.valid = 0;
16374cca1530SAlexander Motin 	if (ident_buf) {
16384cca1530SAlexander Motin 		if (path->device->transport == XPORT_ATA) {
16394cca1530SAlexander Motin 			cts.xport_specific.ata.atapi =
16408e6cab54SAlexander Motin 			    (ident_buf->config == ATA_PROTO_CFA) ? 0 :
16414cca1530SAlexander Motin 			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 :
16424cca1530SAlexander Motin 			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0;
16434cca1530SAlexander Motin 			cts.xport_specific.ata.valid = CTS_ATA_VALID_ATAPI;
16444cca1530SAlexander Motin 		} else {
16454cca1530SAlexander Motin 			cts.xport_specific.sata.atapi =
16468e6cab54SAlexander Motin 			    (ident_buf->config == ATA_PROTO_CFA) ? 0 :
16474cca1530SAlexander Motin 			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 :
16484cca1530SAlexander Motin 			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0;
16494cca1530SAlexander Motin 			cts.xport_specific.sata.valid = CTS_SATA_VALID_ATAPI;
16504cca1530SAlexander Motin 		}
16514cca1530SAlexander Motin 	} else
165252c9ce25SScott Long 		cts.xport_specific.valid = 0;
165352c9ce25SScott Long 	xpt_action((union ccb *)&cts);
165452c9ce25SScott Long }
165552c9ce25SScott Long 
165652c9ce25SScott Long static void
165714f900e2SWill Andrews ata_dev_advinfo(union ccb *start_ccb)
165814f900e2SWill Andrews {
165914f900e2SWill Andrews 	struct cam_ed *device;
166014f900e2SWill Andrews 	struct ccb_dev_advinfo *cdai;
166114f900e2SWill Andrews 	off_t amt;
166214f900e2SWill Andrews 
166314f900e2SWill Andrews 	start_ccb->ccb_h.status = CAM_REQ_INVALID;
166414f900e2SWill Andrews 	device = start_ccb->ccb_h.path->device;
166514f900e2SWill Andrews 	cdai = &start_ccb->cdai;
166614f900e2SWill Andrews 	switch(cdai->buftype) {
16673089bb2eSAlexander Motin 	case CDAI_TYPE_SCSI_DEVID:
16683089bb2eSAlexander Motin 		if (cdai->flags & CDAI_FLAG_STORE)
16693089bb2eSAlexander Motin 			return;
16703089bb2eSAlexander Motin 		cdai->provsiz = device->device_id_len;
16713089bb2eSAlexander Motin 		if (device->device_id_len == 0)
16723089bb2eSAlexander Motin 			break;
16733089bb2eSAlexander Motin 		amt = device->device_id_len;
16743089bb2eSAlexander Motin 		if (cdai->provsiz > cdai->bufsiz)
16753089bb2eSAlexander Motin 			amt = cdai->bufsiz;
16763089bb2eSAlexander Motin 		memcpy(cdai->buf, device->device_id, amt);
16773089bb2eSAlexander Motin 		break;
167814f900e2SWill Andrews 	case CDAI_TYPE_SERIAL_NUM:
167914f900e2SWill Andrews 		if (cdai->flags & CDAI_FLAG_STORE)
16803089bb2eSAlexander Motin 			return;
168114f900e2SWill Andrews 		cdai->provsiz = device->serial_num_len;
168214f900e2SWill Andrews 		if (device->serial_num_len == 0)
168314f900e2SWill Andrews 			break;
168414f900e2SWill Andrews 		amt = device->serial_num_len;
168514f900e2SWill Andrews 		if (cdai->provsiz > cdai->bufsiz)
168614f900e2SWill Andrews 			amt = cdai->bufsiz;
168714f900e2SWill Andrews 		memcpy(cdai->buf, device->serial_num, amt);
168814f900e2SWill Andrews 		break;
16893089bb2eSAlexander Motin 	case CDAI_TYPE_PHYS_PATH:
16903089bb2eSAlexander Motin 		if (cdai->flags & CDAI_FLAG_STORE) {
16913089bb2eSAlexander Motin 			if (device->physpath != NULL)
16923089bb2eSAlexander Motin 				free(device->physpath, M_CAMXPT);
16933089bb2eSAlexander Motin 			device->physpath_len = cdai->bufsiz;
16943089bb2eSAlexander Motin 			/* Clear existing buffer if zero length */
16953089bb2eSAlexander Motin 			if (cdai->bufsiz == 0)
169614f900e2SWill Andrews 				break;
16973089bb2eSAlexander Motin 			device->physpath = malloc(cdai->bufsiz, M_CAMXPT, M_NOWAIT);
16983089bb2eSAlexander Motin 			if (device->physpath == NULL) {
16993089bb2eSAlexander Motin 				start_ccb->ccb_h.status = CAM_REQ_ABORTED;
17003089bb2eSAlexander Motin 				return;
17013089bb2eSAlexander Motin 			}
17023089bb2eSAlexander Motin 			memcpy(device->physpath, cdai->buf, cdai->bufsiz);
17033089bb2eSAlexander Motin 		} else {
17043089bb2eSAlexander Motin 			cdai->provsiz = device->physpath_len;
17053089bb2eSAlexander Motin 			if (device->physpath_len == 0)
17063089bb2eSAlexander Motin 				break;
17073089bb2eSAlexander Motin 			amt = device->physpath_len;
17083089bb2eSAlexander Motin 			if (cdai->provsiz > cdai->bufsiz)
17093089bb2eSAlexander Motin 				amt = cdai->bufsiz;
17103089bb2eSAlexander Motin 			memcpy(cdai->buf, device->physpath, amt);
17113089bb2eSAlexander Motin 		}
17123089bb2eSAlexander Motin 		break;
17133089bb2eSAlexander Motin 	default:
17143089bb2eSAlexander Motin 		return;
17153089bb2eSAlexander Motin 	}
17163089bb2eSAlexander Motin 	start_ccb->ccb_h.status = CAM_REQ_CMP;
17173089bb2eSAlexander Motin 
17183089bb2eSAlexander Motin 	if (cdai->flags & CDAI_FLAG_STORE) {
17193089bb2eSAlexander Motin 		int owned;
17203089bb2eSAlexander Motin 
17213089bb2eSAlexander Motin 		owned = mtx_owned(start_ccb->ccb_h.path->bus->sim->mtx);
17223089bb2eSAlexander Motin 		if (owned == 0)
17233089bb2eSAlexander Motin 			mtx_lock(start_ccb->ccb_h.path->bus->sim->mtx);
17243089bb2eSAlexander Motin 		xpt_async(AC_ADVINFO_CHANGED, start_ccb->ccb_h.path,
17253089bb2eSAlexander Motin 			  (void *)(uintptr_t)cdai->buftype);
17263089bb2eSAlexander Motin 		if (owned == 0)
17273089bb2eSAlexander Motin 			mtx_unlock(start_ccb->ccb_h.path->bus->sim->mtx);
172814f900e2SWill Andrews 	}
172914f900e2SWill Andrews }
173014f900e2SWill Andrews 
173114f900e2SWill Andrews static void
173252c9ce25SScott Long ata_action(union ccb *start_ccb)
173352c9ce25SScott Long {
173452c9ce25SScott Long 
173552c9ce25SScott Long 	switch (start_ccb->ccb_h.func_code) {
173652c9ce25SScott Long 	case XPT_SET_TRAN_SETTINGS:
173752c9ce25SScott Long 	{
173830a4094fSAlexander Motin 		ata_set_transfer_settings(&start_ccb->cts,
173952c9ce25SScott Long 					   start_ccb->ccb_h.path->device,
174052c9ce25SScott Long 					   /*async_update*/FALSE);
174152c9ce25SScott Long 		break;
174252c9ce25SScott Long 	}
174352c9ce25SScott Long 	case XPT_SCAN_BUS:
17440e85f214SMatt Jacob 	case XPT_SCAN_TGT:
174552c9ce25SScott Long 		ata_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
174652c9ce25SScott Long 		break;
174752c9ce25SScott Long 	case XPT_SCAN_LUN:
174852c9ce25SScott Long 		ata_scan_lun(start_ccb->ccb_h.path->periph,
174952c9ce25SScott Long 			      start_ccb->ccb_h.path, start_ccb->crcn.flags,
175052c9ce25SScott Long 			      start_ccb);
175152c9ce25SScott Long 		break;
175252c9ce25SScott Long 	case XPT_GET_TRAN_SETTINGS:
175352c9ce25SScott Long 	{
1754b9c473b2SAlexander Motin 		ata_get_transfer_settings(&start_ccb->cts);
175552c9ce25SScott Long 		break;
175652c9ce25SScott Long 	}
17574cca1530SAlexander Motin 	case XPT_SCSI_IO:
17584cca1530SAlexander Motin 	{
17594cca1530SAlexander Motin 		struct cam_ed *device;
17604cca1530SAlexander Motin 		u_int	maxlen = 0;
17614cca1530SAlexander Motin 
17624cca1530SAlexander Motin 		device = start_ccb->ccb_h.path->device;
17634cca1530SAlexander Motin 		if (device->protocol == PROTO_SCSI &&
17644cca1530SAlexander Motin 		    (device->flags & CAM_DEV_IDENTIFY_DATA_VALID)) {
17654cca1530SAlexander Motin 			uint16_t p =
17664cca1530SAlexander Motin 			    device->ident_data.config & ATA_PROTO_MASK;
17674cca1530SAlexander Motin 
17688e6cab54SAlexander Motin 			maxlen =
17698e6cab54SAlexander Motin 			    (device->ident_data.config == ATA_PROTO_CFA) ? 0 :
17708e6cab54SAlexander Motin 			    (p == ATA_PROTO_ATAPI_16) ? 16 :
17714cca1530SAlexander Motin 			    (p == ATA_PROTO_ATAPI_12) ? 12 : 0;
17724cca1530SAlexander Motin 		}
17734cca1530SAlexander Motin 		if (start_ccb->csio.cdb_len > maxlen) {
17744cca1530SAlexander Motin 			start_ccb->ccb_h.status = CAM_REQ_INVALID;
17754cca1530SAlexander Motin 			xpt_done(start_ccb);
17764cca1530SAlexander Motin 			break;
17774cca1530SAlexander Motin 		}
1778ee2b236bSAlexander Motin 		xpt_action_default(start_ccb);
1779ee2b236bSAlexander Motin 		break;
17804cca1530SAlexander Motin 	}
178114f900e2SWill Andrews 	case XPT_DEV_ADVINFO:
178214f900e2SWill Andrews 	{
178314f900e2SWill Andrews 		ata_dev_advinfo(start_ccb);
178414f900e2SWill Andrews 		break;
178514f900e2SWill Andrews 	}
178652c9ce25SScott Long 	default:
178752c9ce25SScott Long 		xpt_action_default(start_ccb);
178852c9ce25SScott Long 		break;
178952c9ce25SScott Long 	}
179052c9ce25SScott Long }
179152c9ce25SScott Long 
179252c9ce25SScott Long static void
1793b9c473b2SAlexander Motin ata_get_transfer_settings(struct ccb_trans_settings *cts)
1794b9c473b2SAlexander Motin {
1795b9c473b2SAlexander Motin 	struct	ccb_trans_settings_ata *ata;
1796b9c473b2SAlexander Motin 	struct	ccb_trans_settings_scsi *scsi;
1797b9c473b2SAlexander Motin 	struct	cam_ed *device;
1798b9c473b2SAlexander Motin 	struct	cam_sim *sim;
1799b9c473b2SAlexander Motin 
1800b9c473b2SAlexander Motin 	device = cts->ccb_h.path->device;
1801b9c473b2SAlexander Motin 	sim = cts->ccb_h.path->bus->sim;
1802b9c473b2SAlexander Motin 	(*(sim->sim_action))(sim, (union ccb *)cts);
1803b9c473b2SAlexander Motin 
1804bc1bf6e8SAlexander Motin 	if (cts->protocol == PROTO_UNKNOWN ||
1805bc1bf6e8SAlexander Motin 	    cts->protocol == PROTO_UNSPECIFIED) {
1806bc1bf6e8SAlexander Motin 		cts->protocol = device->protocol;
1807bc1bf6e8SAlexander Motin 		cts->protocol_version = device->protocol_version;
1808bc1bf6e8SAlexander Motin 	}
1809bc1bf6e8SAlexander Motin 
1810b9c473b2SAlexander Motin 	if (cts->protocol == PROTO_ATA) {
1811b9c473b2SAlexander Motin 		ata = &cts->proto_specific.ata;
1812b9c473b2SAlexander Motin 		if ((ata->valid & CTS_ATA_VALID_TQ) == 0) {
1813b9c473b2SAlexander Motin 			ata->valid |= CTS_ATA_VALID_TQ;
1814b9c473b2SAlexander Motin 			if (cts->type == CTS_TYPE_USER_SETTINGS ||
1815b9c473b2SAlexander Motin 			    (device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0 ||
1816b9c473b2SAlexander Motin 			    (device->inq_flags & SID_CmdQue) != 0)
1817b9c473b2SAlexander Motin 				ata->flags |= CTS_ATA_FLAGS_TAG_ENB;
1818b9c473b2SAlexander Motin 		}
1819b9c473b2SAlexander Motin 	}
1820b9c473b2SAlexander Motin 	if (cts->protocol == PROTO_SCSI) {
1821b9c473b2SAlexander Motin 		scsi = &cts->proto_specific.scsi;
1822b9c473b2SAlexander Motin 		if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
1823b9c473b2SAlexander Motin 			scsi->valid |= CTS_SCSI_VALID_TQ;
1824b9c473b2SAlexander Motin 			if (cts->type == CTS_TYPE_USER_SETTINGS ||
1825b9c473b2SAlexander Motin 			    (device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0 ||
1826b9c473b2SAlexander Motin 			    (device->inq_flags & SID_CmdQue) != 0)
1827b9c473b2SAlexander Motin 				scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
1828b9c473b2SAlexander Motin 		}
1829b9c473b2SAlexander Motin 	}
1830bc1bf6e8SAlexander Motin 
1831bc1bf6e8SAlexander Motin 	if (cts->transport == XPORT_UNKNOWN ||
1832bc1bf6e8SAlexander Motin 	    cts->transport == XPORT_UNSPECIFIED) {
1833bc1bf6e8SAlexander Motin 		cts->transport = device->transport;
1834bc1bf6e8SAlexander Motin 		cts->transport_version = device->transport_version;
1835bc1bf6e8SAlexander Motin 	}
1836b9c473b2SAlexander Motin }
1837b9c473b2SAlexander Motin 
1838b9c473b2SAlexander Motin static void
183930a4094fSAlexander Motin ata_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
184052c9ce25SScott Long 			   int async_update)
184152c9ce25SScott Long {
184252c9ce25SScott Long 	struct	ccb_pathinq cpi;
1843b9c473b2SAlexander Motin 	struct	ccb_trans_settings_ata *ata;
184452c9ce25SScott Long 	struct	ccb_trans_settings_scsi *scsi;
184552c9ce25SScott Long 	struct	cam_sim *sim;
1846b9c473b2SAlexander Motin 	struct	ata_params *ident_data;
184752c9ce25SScott Long 	struct	scsi_inquiry_data *inq_data;
184852c9ce25SScott Long 
184952c9ce25SScott Long 	if (device == NULL) {
185052c9ce25SScott Long 		cts->ccb_h.status = CAM_PATH_INVALID;
185152c9ce25SScott Long 		xpt_done((union ccb *)cts);
185252c9ce25SScott Long 		return;
185352c9ce25SScott Long 	}
185452c9ce25SScott Long 
185552c9ce25SScott Long 	if (cts->protocol == PROTO_UNKNOWN
185652c9ce25SScott Long 	 || cts->protocol == PROTO_UNSPECIFIED) {
185752c9ce25SScott Long 		cts->protocol = device->protocol;
185852c9ce25SScott Long 		cts->protocol_version = device->protocol_version;
185952c9ce25SScott Long 	}
186052c9ce25SScott Long 
186152c9ce25SScott Long 	if (cts->protocol_version == PROTO_VERSION_UNKNOWN
186252c9ce25SScott Long 	 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
186352c9ce25SScott Long 		cts->protocol_version = device->protocol_version;
186452c9ce25SScott Long 
186552c9ce25SScott Long 	if (cts->protocol != device->protocol) {
186652c9ce25SScott Long 		xpt_print(cts->ccb_h.path, "Uninitialized Protocol %x:%x?\n",
186752c9ce25SScott Long 		       cts->protocol, device->protocol);
186852c9ce25SScott Long 		cts->protocol = device->protocol;
186952c9ce25SScott Long 	}
187052c9ce25SScott Long 
187152c9ce25SScott Long 	if (cts->protocol_version > device->protocol_version) {
187252c9ce25SScott Long 		if (bootverbose) {
187352c9ce25SScott Long 			xpt_print(cts->ccb_h.path, "Down reving Protocol "
187452c9ce25SScott Long 			    "Version from %d to %d?\n", cts->protocol_version,
187552c9ce25SScott Long 			    device->protocol_version);
187652c9ce25SScott Long 		}
187752c9ce25SScott Long 		cts->protocol_version = device->protocol_version;
187852c9ce25SScott Long 	}
187952c9ce25SScott Long 
188052c9ce25SScott Long 	if (cts->transport == XPORT_UNKNOWN
188152c9ce25SScott Long 	 || cts->transport == XPORT_UNSPECIFIED) {
188252c9ce25SScott Long 		cts->transport = device->transport;
188352c9ce25SScott Long 		cts->transport_version = device->transport_version;
188452c9ce25SScott Long 	}
188552c9ce25SScott Long 
188652c9ce25SScott Long 	if (cts->transport_version == XPORT_VERSION_UNKNOWN
188752c9ce25SScott Long 	 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
188852c9ce25SScott Long 		cts->transport_version = device->transport_version;
188952c9ce25SScott Long 
189052c9ce25SScott Long 	if (cts->transport != device->transport) {
189152c9ce25SScott Long 		xpt_print(cts->ccb_h.path, "Uninitialized Transport %x:%x?\n",
189252c9ce25SScott Long 		    cts->transport, device->transport);
189352c9ce25SScott Long 		cts->transport = device->transport;
189452c9ce25SScott Long 	}
189552c9ce25SScott Long 
189652c9ce25SScott Long 	if (cts->transport_version > device->transport_version) {
189752c9ce25SScott Long 		if (bootverbose) {
189852c9ce25SScott Long 			xpt_print(cts->ccb_h.path, "Down reving Transport "
189952c9ce25SScott Long 			    "Version from %d to %d?\n", cts->transport_version,
190052c9ce25SScott Long 			    device->transport_version);
190152c9ce25SScott Long 		}
190252c9ce25SScott Long 		cts->transport_version = device->transport_version;
190352c9ce25SScott Long 	}
190452c9ce25SScott Long 
190552c9ce25SScott Long 	sim = cts->ccb_h.path->bus->sim;
1906b9c473b2SAlexander Motin 	ident_data = &device->ident_data;
190752c9ce25SScott Long 	inq_data = &device->inq_data;
1908b9c473b2SAlexander Motin 	if (cts->protocol == PROTO_ATA)
1909b9c473b2SAlexander Motin 		ata = &cts->proto_specific.ata;
1910b9c473b2SAlexander Motin 	else
1911b9c473b2SAlexander Motin 		ata = NULL;
1912b9c473b2SAlexander Motin 	if (cts->protocol == PROTO_SCSI)
191352c9ce25SScott Long 		scsi = &cts->proto_specific.scsi;
1914b9c473b2SAlexander Motin 	else
1915b9c473b2SAlexander Motin 		scsi = NULL;
191683c5d981SAlexander Motin 	xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NONE);
191752c9ce25SScott Long 	cpi.ccb_h.func_code = XPT_PATH_INQ;
191852c9ce25SScott Long 	xpt_action((union ccb *)&cpi);
191952c9ce25SScott Long 
1920b9c473b2SAlexander Motin 	/* Sanity checking */
192152c9ce25SScott Long 	if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
1922b9c473b2SAlexander Motin 	 || (ata && (ident_data->satacapabilities & ATA_SUPPORT_NCQ) == 0)
1923b9c473b2SAlexander Motin 	 || (scsi && (INQ_DATA_TQ_ENABLED(inq_data)) == 0)
192452c9ce25SScott Long 	 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
192552c9ce25SScott Long 	 || (device->mintags == 0)) {
192652c9ce25SScott Long 		/*
192752c9ce25SScott Long 		 * Can't tag on hardware that doesn't support tags,
192852c9ce25SScott Long 		 * doesn't have it enabled, or has broken tag support.
192952c9ce25SScott Long 		 */
1930b9c473b2SAlexander Motin 		if (ata)
1931b9c473b2SAlexander Motin 			ata->flags &= ~CTS_ATA_FLAGS_TAG_ENB;
1932b9c473b2SAlexander Motin 		if (scsi)
193352c9ce25SScott Long 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
193452c9ce25SScott Long 	}
193552c9ce25SScott Long 
1936b9c473b2SAlexander Motin 	/* Start/stop tags use. */
1937b9c473b2SAlexander Motin 	if (cts->type == CTS_TYPE_CURRENT_SETTINGS &&
1938b9c473b2SAlexander Motin 	    ((ata && (ata->valid & CTS_ATA_VALID_TQ) != 0) ||
1939b9c473b2SAlexander Motin 	     (scsi && (scsi->valid & CTS_SCSI_VALID_TQ) != 0))) {
1940b9c473b2SAlexander Motin 		int nowt, newt = 0;
194152c9ce25SScott Long 
1942b9c473b2SAlexander Motin 		nowt = ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0 ||
1943b9c473b2SAlexander Motin 			(device->inq_flags & SID_CmdQue) != 0);
1944b9c473b2SAlexander Motin 		if (ata)
1945b9c473b2SAlexander Motin 			newt = (ata->flags & CTS_ATA_FLAGS_TAG_ENB) != 0;
1946b9c473b2SAlexander Motin 		if (scsi)
1947b9c473b2SAlexander Motin 			newt = (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0;
194852c9ce25SScott Long 
1949b9c473b2SAlexander Motin 		if (newt && !nowt) {
195052c9ce25SScott Long 			/*
195152c9ce25SScott Long 			 * Delay change to use tags until after a
195252c9ce25SScott Long 			 * few commands have gone to this device so
195352c9ce25SScott Long 			 * the controller has time to perform transfer
195452c9ce25SScott Long 			 * negotiations without tagged messages getting
195552c9ce25SScott Long 			 * in the way.
195652c9ce25SScott Long 			 */
195752c9ce25SScott Long 			device->tag_delay_count = CAM_TAG_DELAY_COUNT;
195852c9ce25SScott Long 			device->flags |= CAM_DEV_TAG_AFTER_COUNT;
1959b9c473b2SAlexander Motin 		} else if (nowt && !newt)
196030a4094fSAlexander Motin 			xpt_stop_tags(cts->ccb_h.path);
196152c9ce25SScott Long 	}
1962b9c473b2SAlexander Motin 
196352c9ce25SScott Long 	if (async_update == FALSE)
196452c9ce25SScott Long 		(*(sim->sim_action))(sim, (union ccb *)cts);
196552c9ce25SScott Long }
196652c9ce25SScott Long 
196752c9ce25SScott Long /*
196852c9ce25SScott Long  * Handle any per-device event notifications that require action by the XPT.
196952c9ce25SScott Long  */
197052c9ce25SScott Long static void
197152c9ce25SScott Long ata_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
197252c9ce25SScott Long 	      struct cam_ed *device, void *async_arg)
197352c9ce25SScott Long {
197452c9ce25SScott Long 	cam_status status;
197552c9ce25SScott Long 	struct cam_path newpath;
197652c9ce25SScott Long 
197752c9ce25SScott Long 	/*
197852c9ce25SScott Long 	 * We only need to handle events for real devices.
197952c9ce25SScott Long 	 */
198052c9ce25SScott Long 	if (target->target_id == CAM_TARGET_WILDCARD
198152c9ce25SScott Long 	 || device->lun_id == CAM_LUN_WILDCARD)
198252c9ce25SScott Long 		return;
198352c9ce25SScott Long 
198452c9ce25SScott Long 	/*
198552c9ce25SScott Long 	 * We need our own path with wildcards expanded to
198652c9ce25SScott Long 	 * handle certain types of events.
198752c9ce25SScott Long 	 */
198852c9ce25SScott Long 	if ((async_code == AC_SENT_BDR)
198952c9ce25SScott Long 	 || (async_code == AC_BUS_RESET)
199052c9ce25SScott Long 	 || (async_code == AC_INQ_CHANGED))
199152c9ce25SScott Long 		status = xpt_compile_path(&newpath, NULL,
199252c9ce25SScott Long 					  bus->path_id,
199352c9ce25SScott Long 					  target->target_id,
199452c9ce25SScott Long 					  device->lun_id);
199552c9ce25SScott Long 	else
199652c9ce25SScott Long 		status = CAM_REQ_CMP_ERR;
199752c9ce25SScott Long 
199852c9ce25SScott Long 	if (status == CAM_REQ_CMP) {
199952c9ce25SScott Long 		if (async_code == AC_INQ_CHANGED) {
200052c9ce25SScott Long 			/*
200152c9ce25SScott Long 			 * We've sent a start unit command, or
200252c9ce25SScott Long 			 * something similar to a device that
200352c9ce25SScott Long 			 * may have caused its inquiry data to
200452c9ce25SScott Long 			 * change. So we re-scan the device to
200552c9ce25SScott Long 			 * refresh the inquiry data for it.
200652c9ce25SScott Long 			 */
200752c9ce25SScott Long 			ata_scan_lun(newpath.periph, &newpath,
200852c9ce25SScott Long 				     CAM_EXPECT_INQ_CHANGE, NULL);
200983c5d981SAlexander Motin 		} else {
201083c5d981SAlexander Motin 			/* We need to reinitialize device after reset. */
201183c5d981SAlexander Motin 			ata_scan_lun(newpath.periph, &newpath,
201283c5d981SAlexander Motin 				     0, NULL);
201352c9ce25SScott Long 		}
201452c9ce25SScott Long 		xpt_release_path(&newpath);
2015f98d7a47SAlexander Motin 	} else if (async_code == AC_LOST_DEVICE &&
2016f98d7a47SAlexander Motin 	    (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
201752c9ce25SScott Long 		device->flags |= CAM_DEV_UNCONFIGURED;
2018f98d7a47SAlexander Motin 		xpt_release_device(device);
201952c9ce25SScott Long 	} else if (async_code == AC_TRANSFER_NEG) {
202052c9ce25SScott Long 		struct ccb_trans_settings *settings;
202152c9ce25SScott Long 
202252c9ce25SScott Long 		settings = (struct ccb_trans_settings *)async_arg;
202330a4094fSAlexander Motin 		ata_set_transfer_settings(settings, device,
202452c9ce25SScott Long 					  /*async_update*/TRUE);
202552c9ce25SScott Long 	}
202652c9ce25SScott Long }
202752c9ce25SScott Long 
202857079b17SAlexander Motin static void
202957079b17SAlexander Motin ata_announce_periph(struct cam_periph *periph)
203057079b17SAlexander Motin {
203157079b17SAlexander Motin 	struct	ccb_pathinq cpi;
203257079b17SAlexander Motin 	struct	ccb_trans_settings cts;
203357079b17SAlexander Motin 	struct	cam_path *path = periph->path;
203457079b17SAlexander Motin 	u_int	speed;
203557079b17SAlexander Motin 	u_int	mb;
203657079b17SAlexander Motin 
203757079b17SAlexander Motin 	mtx_assert(periph->sim->mtx, MA_OWNED);
203857079b17SAlexander Motin 
203957079b17SAlexander Motin 	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
204057079b17SAlexander Motin 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
204157079b17SAlexander Motin 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
204257079b17SAlexander Motin 	xpt_action((union ccb*)&cts);
204357079b17SAlexander Motin 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
204457079b17SAlexander Motin 		return;
204557079b17SAlexander Motin 	/* Ask the SIM for its base transfer speed */
204657079b17SAlexander Motin 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
204757079b17SAlexander Motin 	cpi.ccb_h.func_code = XPT_PATH_INQ;
204857079b17SAlexander Motin 	xpt_action((union ccb *)&cpi);
204957079b17SAlexander Motin 	/* Report connection speed */
205057079b17SAlexander Motin 	speed = cpi.base_transfer_speed;
205157079b17SAlexander Motin 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_ATA) {
2052b9c473b2SAlexander Motin 		struct	ccb_trans_settings_pata *pata =
205357079b17SAlexander Motin 		    &cts.xport_specific.ata;
205457079b17SAlexander Motin 
2055b9c473b2SAlexander Motin 		if (pata->valid & CTS_ATA_VALID_MODE)
2056b9c473b2SAlexander Motin 			speed = ata_mode2speed(pata->mode);
205757079b17SAlexander Motin 	}
205857079b17SAlexander Motin 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SATA) {
205957079b17SAlexander Motin 		struct	ccb_trans_settings_sata *sata =
206057079b17SAlexander Motin 		    &cts.xport_specific.sata;
206157079b17SAlexander Motin 
206257079b17SAlexander Motin 		if (sata->valid & CTS_SATA_VALID_REVISION)
206357079b17SAlexander Motin 			speed = ata_revision2speed(sata->revision);
206457079b17SAlexander Motin 	}
206557079b17SAlexander Motin 	mb = speed / 1000;
206657079b17SAlexander Motin 	if (mb > 0)
206757079b17SAlexander Motin 		printf("%s%d: %d.%03dMB/s transfers",
206857079b17SAlexander Motin 		       periph->periph_name, periph->unit_number,
206957079b17SAlexander Motin 		       mb, speed % 1000);
207057079b17SAlexander Motin 	else
207157079b17SAlexander Motin 		printf("%s%d: %dKB/s transfers", periph->periph_name,
207257079b17SAlexander Motin 		       periph->unit_number, speed);
207357079b17SAlexander Motin 	/* Report additional information about connection */
207457079b17SAlexander Motin 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_ATA) {
2075b9c473b2SAlexander Motin 		struct ccb_trans_settings_pata *pata =
207657079b17SAlexander Motin 		    &cts.xport_specific.ata;
207757079b17SAlexander Motin 
207857079b17SAlexander Motin 		printf(" (");
2079b9c473b2SAlexander Motin 		if (pata->valid & CTS_ATA_VALID_MODE)
2080b9c473b2SAlexander Motin 			printf("%s, ", ata_mode2string(pata->mode));
2081b9c473b2SAlexander Motin 		if ((pata->valid & CTS_ATA_VALID_ATAPI) && pata->atapi != 0)
2082b9c473b2SAlexander Motin 			printf("ATAPI %dbytes, ", pata->atapi);
2083b9c473b2SAlexander Motin 		if (pata->valid & CTS_ATA_VALID_BYTECOUNT)
2084b9c473b2SAlexander Motin 			printf("PIO %dbytes", pata->bytecount);
208557079b17SAlexander Motin 		printf(")");
208657079b17SAlexander Motin 	}
208757079b17SAlexander Motin 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SATA) {
208857079b17SAlexander Motin 		struct ccb_trans_settings_sata *sata =
208957079b17SAlexander Motin 		    &cts.xport_specific.sata;
209057079b17SAlexander Motin 
209157079b17SAlexander Motin 		printf(" (");
209257079b17SAlexander Motin 		if (sata->valid & CTS_SATA_VALID_REVISION)
209357079b17SAlexander Motin 			printf("SATA %d.x, ", sata->revision);
209457079b17SAlexander Motin 		else
209557079b17SAlexander Motin 			printf("SATA, ");
209657079b17SAlexander Motin 		if (sata->valid & CTS_SATA_VALID_MODE)
209757079b17SAlexander Motin 			printf("%s, ", ata_mode2string(sata->mode));
209857079b17SAlexander Motin 		if ((sata->valid & CTS_ATA_VALID_ATAPI) && sata->atapi != 0)
209957079b17SAlexander Motin 			printf("ATAPI %dbytes, ", sata->atapi);
210057079b17SAlexander Motin 		if (sata->valid & CTS_SATA_VALID_BYTECOUNT)
210157079b17SAlexander Motin 			printf("PIO %dbytes", sata->bytecount);
210257079b17SAlexander Motin 		printf(")");
210357079b17SAlexander Motin 	}
210457079b17SAlexander Motin 	printf("\n");
210557079b17SAlexander Motin }
2106