xref: /freebsd/sys/cam/ata/ata_xpt.c (revision 519b24f0)
152c9ce25SScott Long /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3bec9534dSPedro F. Giffuni  *
452c9ce25SScott Long  * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org>
552c9ce25SScott Long  * All rights reserved.
652c9ce25SScott Long  *
752c9ce25SScott Long  * Redistribution and use in source and binary forms, with or without
852c9ce25SScott Long  * modification, are permitted provided that the following conditions
952c9ce25SScott Long  * are met:
1052c9ce25SScott Long  * 1. Redistributions of source code must retain the above copyright
1152c9ce25SScott Long  *    notice, this list of conditions and the following disclaimer,
1252c9ce25SScott Long  *    without modification, immediately at the beginning of the file.
1352c9ce25SScott Long  * 2. Redistributions in binary form must reproduce the above copyright
1452c9ce25SScott Long  *    notice, this list of conditions and the following disclaimer in the
1552c9ce25SScott Long  *    documentation and/or other materials provided with the distribution.
1652c9ce25SScott Long  *
1752c9ce25SScott Long  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1852c9ce25SScott Long  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1952c9ce25SScott Long  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2052c9ce25SScott Long  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2152c9ce25SScott Long  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2252c9ce25SScott Long  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2352c9ce25SScott Long  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2452c9ce25SScott Long  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2552c9ce25SScott Long  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2652c9ce25SScott Long  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2752c9ce25SScott Long  */
2852c9ce25SScott Long 
2952c9ce25SScott Long #include <sys/param.h>
3052c9ce25SScott Long #include <sys/bus.h>
3152c9ce25SScott Long #include <sys/endian.h>
3252c9ce25SScott Long #include <sys/systm.h>
3352c9ce25SScott Long #include <sys/types.h>
3452c9ce25SScott Long #include <sys/malloc.h>
3552c9ce25SScott Long #include <sys/kernel.h>
3652c9ce25SScott Long #include <sys/time.h>
3752c9ce25SScott Long #include <sys/conf.h>
3852c9ce25SScott Long #include <sys/fcntl.h>
3952c9ce25SScott Long #include <sys/sbuf.h>
4052c9ce25SScott Long 
41a11463fdSSepherosa Ziehau #include <sys/eventhandler.h>
4252c9ce25SScott Long #include <sys/lock.h>
4352c9ce25SScott Long #include <sys/mutex.h>
4452c9ce25SScott Long #include <sys/sysctl.h>
4552c9ce25SScott Long 
4652c9ce25SScott Long #include <cam/cam.h>
4752c9ce25SScott Long #include <cam/cam_ccb.h>
4852c9ce25SScott Long #include <cam/cam_queue.h>
4952c9ce25SScott Long #include <cam/cam_periph.h>
5052c9ce25SScott Long #include <cam/cam_sim.h>
5152c9ce25SScott Long #include <cam/cam_xpt.h>
5252c9ce25SScott Long #include <cam/cam_xpt_sim.h>
5352c9ce25SScott Long #include <cam/cam_xpt_periph.h>
5452c9ce25SScott Long #include <cam/cam_xpt_internal.h>
5552c9ce25SScott Long #include <cam/cam_debug.h>
5652c9ce25SScott Long 
5752c9ce25SScott Long #include <cam/scsi/scsi_all.h>
5852c9ce25SScott Long #include <cam/scsi/scsi_message.h>
5952c9ce25SScott Long #include <cam/ata/ata_all.h>
6052c9ce25SScott Long #include <machine/stdarg.h>	/* for xpt_print below */
6152c9ce25SScott Long 
6230a4094fSAlexander Motin struct ata_quirk_entry {
6352c9ce25SScott Long 	struct scsi_inquiry_pattern inq_pat;
649db2db6bSWarner Losh 	uint8_t quirks;
6530a4094fSAlexander Motin #define	CAM_QUIRK_MAXTAGS	0x01
667dc3213dSAlexander Motin 	u_int mintags;
6752c9ce25SScott Long 	u_int maxtags;
6852c9ce25SScott Long };
6952c9ce25SScott Long 
700f280cbdSWarner Losh static periph_init_t aprobe_periph_init;
7152c9ce25SScott Long 
720f280cbdSWarner Losh static struct periph_driver aprobe_driver =
7352c9ce25SScott Long {
740f280cbdSWarner Losh 	aprobe_periph_init, "aprobe",
750f280cbdSWarner Losh 	TAILQ_HEAD_INITIALIZER(aprobe_driver.units), /* generation */ 0,
761e637ba6SAlexander Motin 	CAM_PERIPH_DRV_EARLY
7752c9ce25SScott Long };
7852c9ce25SScott Long 
790f280cbdSWarner Losh PERIPHDRIVER_DECLARE(aprobe, aprobe_driver);
8052c9ce25SScott Long 
8152c9ce25SScott Long typedef enum {
8252c9ce25SScott Long 	PROBE_RESET,
8352c9ce25SScott Long 	PROBE_IDENTIFY,
844ef08dc5SAlexander Motin 	PROBE_SPINUP,
8552c9ce25SScott Long 	PROBE_SETMODE,
86da6808c1SAlexander Motin 	PROBE_SETPM,
87da6808c1SAlexander Motin 	PROBE_SETAPST,
88da6808c1SAlexander Motin 	PROBE_SETDMAAA,
898d169381SAlexander Motin 	PROBE_SETAN,
901e637ba6SAlexander Motin 	PROBE_SET_MULTI,
9152c9ce25SScott Long 	PROBE_INQUIRY,
9252c9ce25SScott Long 	PROBE_FULL_INQUIRY,
9352c9ce25SScott Long 	PROBE_PM_PID,
9452c9ce25SScott Long 	PROBE_PM_PRV,
953089bb2eSAlexander Motin 	PROBE_IDENTIFY_SES,
963089bb2eSAlexander Motin 	PROBE_IDENTIFY_SAFTE,
97a4d953c4SAlexander Motin 	PROBE_DONE,
9852c9ce25SScott Long 	PROBE_INVALID
990f280cbdSWarner Losh } aprobe_action;
10052c9ce25SScott Long 
10152c9ce25SScott Long static char *probe_action_text[] = {
10252c9ce25SScott Long 	"PROBE_RESET",
10352c9ce25SScott Long 	"PROBE_IDENTIFY",
1044ef08dc5SAlexander Motin 	"PROBE_SPINUP",
10552c9ce25SScott Long 	"PROBE_SETMODE",
106da6808c1SAlexander Motin 	"PROBE_SETPM",
107da6808c1SAlexander Motin 	"PROBE_SETAPST",
108da6808c1SAlexander Motin 	"PROBE_SETDMAAA",
1098d169381SAlexander Motin 	"PROBE_SETAN",
1101e637ba6SAlexander Motin 	"PROBE_SET_MULTI",
11152c9ce25SScott Long 	"PROBE_INQUIRY",
11252c9ce25SScott Long 	"PROBE_FULL_INQUIRY",
11352c9ce25SScott Long 	"PROBE_PM_PID",
11452c9ce25SScott Long 	"PROBE_PM_PRV",
1153089bb2eSAlexander Motin 	"PROBE_IDENTIFY_SES",
1163089bb2eSAlexander Motin 	"PROBE_IDENTIFY_SAFTE",
117a4d953c4SAlexander Motin 	"PROBE_DONE",
11852c9ce25SScott Long 	"PROBE_INVALID"
11952c9ce25SScott Long };
12052c9ce25SScott Long 
12152c9ce25SScott Long #define PROBE_SET_ACTION(softc, newaction)	\
12252c9ce25SScott Long do {									\
12352c9ce25SScott Long 	char **text;							\
12452c9ce25SScott Long 	text = probe_action_text;					\
125a4d953c4SAlexander Motin 	CAM_DEBUG((softc)->periph->path, CAM_DEBUG_PROBE,		\
12652c9ce25SScott Long 	    ("Probe %s to %s\n", text[(softc)->action],			\
12752c9ce25SScott Long 	    text[(newaction)]));					\
12852c9ce25SScott Long 	(softc)->action = (newaction);					\
12952c9ce25SScott Long } while(0)
13052c9ce25SScott Long 
13152c9ce25SScott Long typedef enum {
13252c9ce25SScott Long 	PROBE_NO_ANNOUNCE	= 0x04
1330f280cbdSWarner Losh } aprobe_flags;
13452c9ce25SScott Long 
13552c9ce25SScott Long typedef struct {
13652c9ce25SScott Long 	TAILQ_HEAD(, ccb_hdr) request_ccbs;
137a9b8edb1SAlexander Motin 	struct ata_params	ident_data;
1380f280cbdSWarner Losh 	aprobe_action	action;
1390f280cbdSWarner Losh 	aprobe_flags	flags;
14052c9ce25SScott Long 	uint32_t	pm_pid;
14152c9ce25SScott Long 	uint32_t	pm_prv;
14283c5d981SAlexander Motin 	int		restart;
1434ef08dc5SAlexander Motin 	int		spinup;
14425a519a9SAlexander Motin 	int		faults;
145da6808c1SAlexander Motin 	u_int		caps;
14652c9ce25SScott Long 	struct cam_periph *periph;
1474762aa57SWarner Losh } aprobe_softc;
14852c9ce25SScott Long 
14930a4094fSAlexander Motin static struct ata_quirk_entry ata_quirk_table[] =
15052c9ce25SScott Long {
15152c9ce25SScott Long 	{
15252c9ce25SScott Long 		/* Default tagged queuing parameters for all devices */
15352c9ce25SScott Long 		{
15452c9ce25SScott Long 		  T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
15552c9ce25SScott Long 		  /*vendor*/"*", /*product*/"*", /*revision*/"*"
15652c9ce25SScott Long 		},
1577dc3213dSAlexander Motin 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
15852c9ce25SScott Long 	},
15952c9ce25SScott Long };
16052c9ce25SScott Long 
1610f280cbdSWarner Losh static cam_status	aproberegister(struct cam_periph *periph, void *arg);
1620f280cbdSWarner Losh static void	 aprobeschedule(struct cam_periph *probe_periph);
1630f280cbdSWarner Losh static void	 aprobestart(struct cam_periph *periph, union ccb *start_ccb);
1640f280cbdSWarner Losh static void	 aproberequestdefaultnegotiation(struct cam_periph *periph);
1650f280cbdSWarner Losh static void	 aprobedone(struct cam_periph *periph, union ccb *done_ccb);
1660f280cbdSWarner Losh static void	 aprobecleanup(struct cam_periph *periph);
16730a4094fSAlexander Motin static void	 ata_find_quirk(struct cam_ed *device);
16852c9ce25SScott Long static void	 ata_scan_bus(struct cam_periph *periph, union ccb *ccb);
16952c9ce25SScott Long static void	 ata_scan_lun(struct cam_periph *periph,
17052c9ce25SScott Long 			       struct cam_path *path, cam_flags flags,
17152c9ce25SScott Long 			       union ccb *ccb);
1720f280cbdSWarner Losh static void	 axptscandone(struct cam_periph *periph, union ccb *done_ccb);
17352c9ce25SScott Long static struct cam_ed *
17452c9ce25SScott Long 		 ata_alloc_device(struct cam_eb *bus, struct cam_et *target,
17552c9ce25SScott Long 				   lun_id_t lun_id);
17652c9ce25SScott Long static void	 ata_device_transport(struct cam_path *path);
177b9c473b2SAlexander Motin static void	 ata_get_transfer_settings(struct ccb_trans_settings *cts);
17830a4094fSAlexander Motin static void	 ata_set_transfer_settings(struct ccb_trans_settings *cts,
179227d67aaSAlexander Motin 					    struct cam_path *path,
18052c9ce25SScott Long 					    int async_update);
1819db2db6bSWarner Losh static void	 ata_dev_async(uint32_t async_code,
18252c9ce25SScott Long 				struct cam_eb *bus,
18352c9ce25SScott Long 				struct cam_et *target,
18452c9ce25SScott Long 				struct cam_ed *device,
18552c9ce25SScott Long 				void *async_arg);
18652c9ce25SScott Long static void	 ata_action(union ccb *start_ccb);
1875d01277fSScott Long static void	 ata_announce_periph_sbuf(struct cam_periph *periph, struct sbuf *sb);
1885d01277fSScott Long static void	 ata_proto_announce_sbuf(struct cam_ed *device, struct sbuf *sb);
1895d01277fSScott Long static void	 ata_proto_denounce_sbuf(struct cam_ed *device, struct sbuf *sb);
19008f13879SWarner Losh static void	 ata_proto_debug_out(union ccb *ccb);
1915d01277fSScott Long static void	 semb_proto_announce_sbuf(struct cam_ed *device, struct sbuf *sb);
1925d01277fSScott Long static void	 semb_proto_denounce_sbuf(struct cam_ed *device, struct sbuf *sb);
19352c9ce25SScott Long 
1942121d8a5SAlexander Motin static int ata_dma = 1;
1952121d8a5SAlexander Motin static int atapi_dma = 1;
1962121d8a5SAlexander Motin 
1972121d8a5SAlexander Motin TUNABLE_INT("hw.ata.ata_dma", &ata_dma);
1982121d8a5SAlexander Motin TUNABLE_INT("hw.ata.atapi_dma", &atapi_dma);
1992121d8a5SAlexander Motin 
200ded2b706SWarner Losh static struct xpt_xport_ops ata_xport_ops = {
20152c9ce25SScott Long 	.alloc_device = ata_alloc_device,
20252c9ce25SScott Long 	.action = ata_action,
20352c9ce25SScott Long 	.async = ata_dev_async,
2045d01277fSScott Long 	.announce_sbuf = ata_announce_periph_sbuf,
20552c9ce25SScott Long };
206ded2b706SWarner Losh #define ATA_XPT_XPORT(x, X)			\
207ded2b706SWarner Losh static struct xpt_xport ata_xport_ ## x = {	\
208ded2b706SWarner Losh 	.xport = XPORT_ ## X,			\
209ded2b706SWarner Losh 	.name = #x,				\
210ded2b706SWarner Losh 	.ops = &ata_xport_ops,			\
211ded2b706SWarner Losh };						\
212ded2b706SWarner Losh CAM_XPT_XPORT(ata_xport_ ## x);
21352c9ce25SScott Long 
214ded2b706SWarner Losh ATA_XPT_XPORT(ata, ATA);
215ded2b706SWarner Losh ATA_XPT_XPORT(sata, SATA);
216ded2b706SWarner Losh 
217ded2b706SWarner Losh #undef ATA_XPORT_XPORT
21852c9ce25SScott Long 
21908f13879SWarner Losh static struct xpt_proto_ops ata_proto_ops_ata = {
2205d01277fSScott Long 	.announce_sbuf = ata_proto_announce_sbuf,
2215d01277fSScott Long 	.denounce_sbuf = ata_proto_denounce_sbuf,
22208f13879SWarner Losh 	.debug_out = ata_proto_debug_out,
22308f13879SWarner Losh };
22408f13879SWarner Losh static struct xpt_proto ata_proto_ata = {
22508f13879SWarner Losh 	.proto = PROTO_ATA,
22608f13879SWarner Losh 	.name = "ata",
22708f13879SWarner Losh 	.ops = &ata_proto_ops_ata,
22808f13879SWarner Losh };
22908f13879SWarner Losh 
23008f13879SWarner Losh static struct xpt_proto_ops ata_proto_ops_satapm = {
2315d01277fSScott Long 	.announce_sbuf = ata_proto_announce_sbuf,
2325d01277fSScott Long 	.denounce_sbuf = ata_proto_denounce_sbuf,
23308f13879SWarner Losh 	.debug_out = ata_proto_debug_out,
23408f13879SWarner Losh };
23508f13879SWarner Losh static struct xpt_proto ata_proto_satapm = {
23608f13879SWarner Losh 	.proto = PROTO_SATAPM,
23708f13879SWarner Losh 	.name = "satapm",
23808f13879SWarner Losh 	.ops = &ata_proto_ops_satapm,
23908f13879SWarner Losh };
24008f13879SWarner Losh 
24108f13879SWarner Losh static struct xpt_proto_ops ata_proto_ops_semb = {
2425d01277fSScott Long 	.announce_sbuf = semb_proto_announce_sbuf,
2435d01277fSScott Long 	.denounce_sbuf = semb_proto_denounce_sbuf,
24408f13879SWarner Losh 	.debug_out = ata_proto_debug_out,
24508f13879SWarner Losh };
24608f13879SWarner Losh static struct xpt_proto ata_proto_semb = {
24708f13879SWarner Losh 	.proto = PROTO_SEMB,
24808f13879SWarner Losh 	.name = "semb",
24908f13879SWarner Losh 	.ops = &ata_proto_ops_semb,
25008f13879SWarner Losh };
25108f13879SWarner Losh 
25208f13879SWarner Losh CAM_XPT_PROTO(ata_proto_ata);
25308f13879SWarner Losh CAM_XPT_PROTO(ata_proto_satapm);
25408f13879SWarner Losh CAM_XPT_PROTO(ata_proto_semb);
25508f13879SWarner Losh 
25652c9ce25SScott Long static void
aprobe_periph_init(void)2579982b3eeSConrad Meyer aprobe_periph_init(void)
25852c9ce25SScott Long {
25952c9ce25SScott Long }
26052c9ce25SScott Long 
26152c9ce25SScott Long static cam_status
aproberegister(struct cam_periph * periph,void * arg)2620f280cbdSWarner Losh aproberegister(struct cam_periph *periph, void *arg)
26352c9ce25SScott Long {
26452c9ce25SScott Long 	union ccb *request_ccb;	/* CCB representing the probe request */
2654762aa57SWarner Losh 	aprobe_softc *softc;
26652c9ce25SScott Long 
26752c9ce25SScott Long 	request_ccb = (union ccb *)arg;
26852c9ce25SScott Long 	if (request_ccb == NULL) {
26952c9ce25SScott Long 		printf("proberegister: no probe CCB, "
27052c9ce25SScott Long 		       "can't register device\n");
27152c9ce25SScott Long 		return(CAM_REQ_CMP_ERR);
27252c9ce25SScott Long 	}
27352c9ce25SScott Long 
2744762aa57SWarner Losh 	softc = (aprobe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_ZERO | M_NOWAIT);
27552c9ce25SScott Long 
27652c9ce25SScott Long 	if (softc == NULL) {
27752c9ce25SScott Long 		printf("proberegister: Unable to probe new device. "
27852c9ce25SScott Long 		       "Unable to allocate softc\n");
27952c9ce25SScott Long 		return(CAM_REQ_CMP_ERR);
28052c9ce25SScott Long 	}
28152c9ce25SScott Long 	TAILQ_INIT(&softc->request_ccbs);
28252c9ce25SScott Long 	TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
28352c9ce25SScott Long 			  periph_links.tqe);
28452c9ce25SScott Long 	softc->flags = 0;
28552c9ce25SScott Long 	periph->softc = softc;
28652c9ce25SScott Long 	softc->periph = periph;
28752c9ce25SScott Long 	softc->action = PROBE_INVALID;
28899e7a4adSScott Long 	if (cam_periph_acquire(periph) != 0)
28999e7a4adSScott Long 		return (CAM_REQ_CMP_ERR);
29099e7a4adSScott Long 
291a4d953c4SAlexander Motin 	CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe started\n"));
292227d67aaSAlexander Motin 	ata_device_transport(periph->path);
2930f280cbdSWarner Losh 	aprobeschedule(periph);
29452c9ce25SScott Long 	return(CAM_REQ_CMP);
29552c9ce25SScott Long }
29652c9ce25SScott Long 
29752c9ce25SScott Long static void
aprobeschedule(struct cam_periph * periph)2980f280cbdSWarner Losh aprobeschedule(struct cam_periph *periph)
29952c9ce25SScott Long {
30052c9ce25SScott Long 	union ccb *ccb;
3014762aa57SWarner Losh 	aprobe_softc *softc;
30252c9ce25SScott Long 
3034762aa57SWarner Losh 	softc = (aprobe_softc *)periph->softc;
30452c9ce25SScott Long 	ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
30552c9ce25SScott Long 
30665d0fb03SAlexander Motin 	if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) ||
3073089bb2eSAlexander Motin 	    periph->path->device->protocol == PROTO_SATAPM ||
3083089bb2eSAlexander Motin 	    periph->path->device->protocol == PROTO_SEMB)
30952c9ce25SScott Long 		PROBE_SET_ACTION(softc, PROBE_RESET);
31052c9ce25SScott Long 	else
31152c9ce25SScott Long 		PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
31252c9ce25SScott Long 
31352c9ce25SScott Long 	if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
31452c9ce25SScott Long 		softc->flags |= PROBE_NO_ANNOUNCE;
31552c9ce25SScott Long 	else
31652c9ce25SScott Long 		softc->flags &= ~PROBE_NO_ANNOUNCE;
31752c9ce25SScott Long 
31883c5d981SAlexander Motin 	xpt_schedule(periph, CAM_PRIORITY_XPT);
31952c9ce25SScott Long }
32052c9ce25SScott Long 
32152c9ce25SScott Long static void
aprobestart(struct cam_periph * periph,union ccb * start_ccb)3220f280cbdSWarner Losh aprobestart(struct cam_periph *periph, union ccb *start_ccb)
32352c9ce25SScott Long {
324c8039fc6SAlexander Motin 	struct ccb_trans_settings cts;
32552c9ce25SScott Long 	struct ccb_ataio *ataio;
32652c9ce25SScott Long 	struct ccb_scsiio *csio;
3274762aa57SWarner Losh 	aprobe_softc *softc;
3281e637ba6SAlexander Motin 	struct cam_path *path;
3291e637ba6SAlexander Motin 	struct ata_params *ident_buf;
33076d843daSAlexander Motin 	u_int oif;
33152c9ce25SScott Long 
3320f280cbdSWarner Losh 	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("aprobestart\n"));
33352c9ce25SScott Long 
3344762aa57SWarner Losh 	softc = (aprobe_softc *)periph->softc;
3351e637ba6SAlexander Motin 	path = start_ccb->ccb_h.path;
33652c9ce25SScott Long 	ataio = &start_ccb->ataio;
33752c9ce25SScott Long 	csio = &start_ccb->csio;
3381e637ba6SAlexander Motin 	ident_buf = &periph->path->device->ident_data;
33952c9ce25SScott Long 
34083c5d981SAlexander Motin 	if (softc->restart) {
34183c5d981SAlexander Motin 		softc->restart = 0;
34283c5d981SAlexander Motin 		if ((path->device->flags & CAM_DEV_UNCONFIGURED) ||
3433089bb2eSAlexander Motin 		    path->device->protocol == PROTO_SATAPM ||
3443089bb2eSAlexander Motin 		    path->device->protocol == PROTO_SEMB)
34583c5d981SAlexander Motin 			softc->action = PROBE_RESET;
34683c5d981SAlexander Motin 		else
34783c5d981SAlexander Motin 			softc->action = PROBE_IDENTIFY;
34883c5d981SAlexander Motin 	}
34952c9ce25SScott Long 	switch (softc->action) {
35052c9ce25SScott Long 	case PROBE_RESET:
35152c9ce25SScott Long 		cam_fill_ataio(ataio,
35252c9ce25SScott Long 		      0,
3530f280cbdSWarner Losh 		      aprobedone,
35452c9ce25SScott Long 		      /*flags*/CAM_DIR_NONE,
35565d0fb03SAlexander Motin 		      0,
35652c9ce25SScott Long 		      /*data_ptr*/NULL,
35752c9ce25SScott Long 		      /*dxfer_len*/0,
35883c5d981SAlexander Motin 		      15 * 1000);
35952c9ce25SScott Long 		ata_reset_cmd(ataio);
36052c9ce25SScott Long 		break;
36152c9ce25SScott Long 	case PROBE_IDENTIFY:
36252c9ce25SScott Long 		cam_fill_ataio(ataio,
36352c9ce25SScott Long 		      1,
3640f280cbdSWarner Losh 		      aprobedone,
36552c9ce25SScott Long 		      /*flags*/CAM_DIR_IN,
36665d0fb03SAlexander Motin 		      0,
3679db2db6bSWarner Losh 		      /*data_ptr*/(uint8_t *)&softc->ident_data,
368a9b8edb1SAlexander Motin 		      /*dxfer_len*/sizeof(softc->ident_data),
36952c9ce25SScott Long 		      30 * 1000);
37076d843daSAlexander Motin 		if (path->device->protocol == PROTO_ATA)
3717606b445SAlexander Motin 			ata_28bit_cmd(ataio, ATA_ATA_IDENTIFY, 0, 0, 0);
37252c9ce25SScott Long 		else
3737606b445SAlexander Motin 			ata_28bit_cmd(ataio, ATA_ATAPI_IDENTIFY, 0, 0, 0);
37452c9ce25SScott Long 		break;
3754ef08dc5SAlexander Motin 	case PROBE_SPINUP:
3764ef08dc5SAlexander Motin 		if (bootverbose)
3774ef08dc5SAlexander Motin 			xpt_print(path, "Spinning up device\n");
3784ef08dc5SAlexander Motin 		cam_fill_ataio(ataio,
3794ef08dc5SAlexander Motin 		      1,
3800f280cbdSWarner Losh 		      aprobedone,
3814ef08dc5SAlexander Motin 		      /*flags*/CAM_DIR_NONE | CAM_HIGH_POWER,
3824ef08dc5SAlexander Motin 		      0,
3834ef08dc5SAlexander Motin 		      /*data_ptr*/NULL,
3844ef08dc5SAlexander Motin 		      /*dxfer_len*/0,
3854ef08dc5SAlexander Motin 		      30 * 1000);
3864ef08dc5SAlexander Motin 		ata_28bit_cmd(ataio, ATA_SETFEATURES, ATA_SF_PUIS_SPINUP, 0, 0);
3874ef08dc5SAlexander Motin 		break;
38852c9ce25SScott Long 	case PROBE_SETMODE:
389c8039fc6SAlexander Motin 	{
390c8039fc6SAlexander Motin 		int mode, wantmode;
391c8039fc6SAlexander Motin 
392c8039fc6SAlexander Motin 		mode = 0;
393c8039fc6SAlexander Motin 		/* Fetch user 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_USER_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 {
4031a6f09b8SAlexander Motin 			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_MODE)
404c8039fc6SAlexander Motin 				mode = cts.xport_specific.sata.mode;
405c8039fc6SAlexander Motin 		}
40676d843daSAlexander Motin 		if (path->device->protocol == PROTO_ATA) {
4072121d8a5SAlexander Motin 			if (ata_dma == 0 && (mode == 0 || mode > ATA_PIO_MAX))
4082121d8a5SAlexander Motin 				mode = ATA_PIO_MAX;
4092121d8a5SAlexander Motin 		} else {
4102121d8a5SAlexander Motin 			if (atapi_dma == 0 && (mode == 0 || mode > ATA_PIO_MAX))
4112121d8a5SAlexander Motin 				mode = ATA_PIO_MAX;
4122121d8a5SAlexander Motin 		}
413c8039fc6SAlexander Motin negotiate:
414c8039fc6SAlexander Motin 		/* Honor device capabilities. */
415c8039fc6SAlexander Motin 		wantmode = mode = ata_max_mode(ident_buf, mode);
416c8039fc6SAlexander Motin 		/* Report modes to SIM. */
417c8039fc6SAlexander Motin 		bzero(&cts, sizeof(cts));
41883c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
419c8039fc6SAlexander Motin 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
420c8039fc6SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
421c8039fc6SAlexander Motin 		if (path->device->transport == XPORT_ATA) {
422c8039fc6SAlexander Motin 			cts.xport_specific.ata.mode = mode;
423c8039fc6SAlexander Motin 			cts.xport_specific.ata.valid = CTS_ATA_VALID_MODE;
424c8039fc6SAlexander Motin 		} else {
425c8039fc6SAlexander Motin 			cts.xport_specific.sata.mode = mode;
426c8039fc6SAlexander Motin 			cts.xport_specific.sata.valid = CTS_SATA_VALID_MODE;
427c8039fc6SAlexander Motin 		}
428c8039fc6SAlexander Motin 		xpt_action((union ccb *)&cts);
429066f913aSAlexander Motin 		/* Fetch current modes from SIM. */
430c8039fc6SAlexander Motin 		bzero(&cts, sizeof(cts));
43183c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
432c8039fc6SAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
433c8039fc6SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
434c8039fc6SAlexander Motin 		xpt_action((union ccb *)&cts);
435c8039fc6SAlexander Motin 		if (path->device->transport == XPORT_ATA) {
436c8039fc6SAlexander Motin 			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_MODE)
437c8039fc6SAlexander Motin 				mode = cts.xport_specific.ata.mode;
438c8039fc6SAlexander Motin 		} else {
439c7bacdccSBrooks Davis 			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_MODE)
440c8039fc6SAlexander Motin 				mode = cts.xport_specific.sata.mode;
441c8039fc6SAlexander Motin 		}
442c8039fc6SAlexander Motin 		/* If SIM disagree - renegotiate. */
443c8039fc6SAlexander Motin 		if (mode != wantmode)
444c8039fc6SAlexander Motin 			goto negotiate;
445cf2b9a5fSAlexander Motin 		/* Remember what transport thinks about DMA. */
44676d843daSAlexander Motin 		oif = path->device->inq_flags;
447cf2b9a5fSAlexander Motin 		if (mode < ATA_DMA)
448cf2b9a5fSAlexander Motin 			path->device->inq_flags &= ~SID_DMA;
449cf2b9a5fSAlexander Motin 		else
450cf2b9a5fSAlexander Motin 			path->device->inq_flags |= SID_DMA;
45176d843daSAlexander Motin 		if (path->device->inq_flags != oif)
452581b2e78SAlexander Motin 			xpt_async(AC_GETDEV_CHANGED, path, NULL);
45352c9ce25SScott Long 		cam_fill_ataio(ataio,
45452c9ce25SScott Long 		      1,
4550f280cbdSWarner Losh 		      aprobedone,
4565daa555dSAlexander Motin 		      /*flags*/CAM_DIR_NONE,
4575daa555dSAlexander Motin 		      0,
4585daa555dSAlexander Motin 		      /*data_ptr*/NULL,
4595daa555dSAlexander Motin 		      /*dxfer_len*/0,
46052c9ce25SScott Long 		      30 * 1000);
461c8039fc6SAlexander Motin 		ata_28bit_cmd(ataio, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
46252c9ce25SScott Long 		break;
463c8039fc6SAlexander Motin 	}
464da6808c1SAlexander Motin 	case PROBE_SETPM:
465da6808c1SAlexander Motin 		cam_fill_ataio(ataio,
466da6808c1SAlexander Motin 		    1,
4670f280cbdSWarner Losh 		    aprobedone,
468da6808c1SAlexander Motin 		    CAM_DIR_NONE,
469da6808c1SAlexander Motin 		    0,
470da6808c1SAlexander Motin 		    NULL,
471da6808c1SAlexander Motin 		    0,
472da6808c1SAlexander Motin 		    30*1000);
473da6808c1SAlexander Motin 		ata_28bit_cmd(ataio, ATA_SETFEATURES,
474da6808c1SAlexander Motin 		    (softc->caps & CTS_SATA_CAPS_H_PMREQ) ? 0x10 : 0x90,
475da6808c1SAlexander Motin 		    0, 0x03);
476da6808c1SAlexander Motin 		break;
477da6808c1SAlexander Motin 	case PROBE_SETAPST:
478da6808c1SAlexander Motin 		cam_fill_ataio(ataio,
479da6808c1SAlexander Motin 		    1,
4800f280cbdSWarner Losh 		    aprobedone,
481da6808c1SAlexander Motin 		    CAM_DIR_NONE,
482da6808c1SAlexander Motin 		    0,
483da6808c1SAlexander Motin 		    NULL,
484da6808c1SAlexander Motin 		    0,
485da6808c1SAlexander Motin 		    30*1000);
486da6808c1SAlexander Motin 		ata_28bit_cmd(ataio, ATA_SETFEATURES,
487da6808c1SAlexander Motin 		    (softc->caps & CTS_SATA_CAPS_H_APST) ? 0x10 : 0x90,
488da6808c1SAlexander Motin 		    0, 0x07);
489da6808c1SAlexander Motin 		break;
490da6808c1SAlexander Motin 	case PROBE_SETDMAAA:
491da6808c1SAlexander Motin 		cam_fill_ataio(ataio,
492da6808c1SAlexander Motin 		    1,
4930f280cbdSWarner Losh 		    aprobedone,
494da6808c1SAlexander Motin 		    CAM_DIR_NONE,
495da6808c1SAlexander Motin 		    0,
496da6808c1SAlexander Motin 		    NULL,
497da6808c1SAlexander Motin 		    0,
498da6808c1SAlexander Motin 		    30*1000);
499da6808c1SAlexander Motin 		ata_28bit_cmd(ataio, ATA_SETFEATURES,
500da6808c1SAlexander Motin 		    (softc->caps & CTS_SATA_CAPS_H_DMAAA) ? 0x10 : 0x90,
501da6808c1SAlexander Motin 		    0, 0x02);
502da6808c1SAlexander Motin 		break;
5038d169381SAlexander Motin 	case PROBE_SETAN:
504e4bed0b4SWarner Losh 		/* Remember what transport thinks about AEN. */
50576d843daSAlexander Motin 		oif = path->device->inq_flags;
506e4bed0b4SWarner Losh 		if (softc->caps & CTS_SATA_CAPS_H_AN)
5073631c638SAlexander Motin 			path->device->inq_flags |= SID_AEN;
5083631c638SAlexander Motin 		else
5093631c638SAlexander Motin 			path->device->inq_flags &= ~SID_AEN;
51076d843daSAlexander Motin 		if (path->device->inq_flags != oif)
5113631c638SAlexander Motin 			xpt_async(AC_GETDEV_CHANGED, path, NULL);
5128d169381SAlexander Motin 		cam_fill_ataio(ataio,
5138d169381SAlexander Motin 		    1,
5140f280cbdSWarner Losh 		    aprobedone,
5158d169381SAlexander Motin 		    CAM_DIR_NONE,
5168d169381SAlexander Motin 		    0,
5178d169381SAlexander Motin 		    NULL,
5188d169381SAlexander Motin 		    0,
5198d169381SAlexander Motin 		    30*1000);
5208d169381SAlexander Motin 		ata_28bit_cmd(ataio, ATA_SETFEATURES,
5218d169381SAlexander Motin 		    (softc->caps & CTS_SATA_CAPS_H_AN) ? 0x10 : 0x90,
5228d169381SAlexander Motin 		    0, 0x05);
5238d169381SAlexander Motin 		break;
5241e637ba6SAlexander Motin 	case PROBE_SET_MULTI:
5251e637ba6SAlexander Motin 	{
526066f913aSAlexander Motin 		u_int sectors, bytecount;
5271e637ba6SAlexander Motin 
528066f913aSAlexander Motin 		bytecount = 8192;	/* SATA maximum */
529066f913aSAlexander Motin 		/* Fetch user bytecount from SIM. */
530066f913aSAlexander Motin 		bzero(&cts, sizeof(cts));
53183c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
532066f913aSAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
533066f913aSAlexander Motin 		cts.type = CTS_TYPE_USER_SETTINGS;
534066f913aSAlexander Motin 		xpt_action((union ccb *)&cts);
535066f913aSAlexander Motin 		if (path->device->transport == XPORT_ATA) {
536066f913aSAlexander Motin 			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
537066f913aSAlexander Motin 				bytecount = cts.xport_specific.ata.bytecount;
538066f913aSAlexander Motin 		} else {
539066f913aSAlexander Motin 			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
540066f913aSAlexander Motin 				bytecount = cts.xport_specific.sata.bytecount;
541066f913aSAlexander Motin 		}
542066f913aSAlexander Motin 		/* Honor device capabilities. */
543066f913aSAlexander Motin 		sectors = max(1, min(ident_buf->sectors_intr & 0xff,
544066f913aSAlexander Motin 		    bytecount / ata_logical_sector_size(ident_buf)));
5451e637ba6SAlexander Motin 		/* Report bytecount to SIM. */
5461e637ba6SAlexander Motin 		bzero(&cts, sizeof(cts));
54783c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
5481e637ba6SAlexander Motin 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
5491e637ba6SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
5501e637ba6SAlexander Motin 		if (path->device->transport == XPORT_ATA) {
551c1bd46c2SAlexander Motin 			cts.xport_specific.ata.bytecount = sectors *
552c1bd46c2SAlexander Motin 			    ata_logical_sector_size(ident_buf);
5531e637ba6SAlexander Motin 			cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT;
5541e637ba6SAlexander Motin 		} else {
555c1bd46c2SAlexander Motin 			cts.xport_specific.sata.bytecount = sectors *
556c1bd46c2SAlexander Motin 			    ata_logical_sector_size(ident_buf);
5571e637ba6SAlexander Motin 			cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT;
5581e637ba6SAlexander Motin 		}
5591e637ba6SAlexander Motin 		xpt_action((union ccb *)&cts);
560066f913aSAlexander Motin 		/* Fetch current bytecount from SIM. */
561066f913aSAlexander Motin 		bzero(&cts, sizeof(cts));
56283c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
563066f913aSAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
564066f913aSAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
565066f913aSAlexander Motin 		xpt_action((union ccb *)&cts);
566066f913aSAlexander Motin 		if (path->device->transport == XPORT_ATA) {
567066f913aSAlexander Motin 			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
568066f913aSAlexander Motin 				bytecount = cts.xport_specific.ata.bytecount;
569066f913aSAlexander Motin 		} else {
570066f913aSAlexander Motin 			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
571066f913aSAlexander Motin 				bytecount = cts.xport_specific.sata.bytecount;
572066f913aSAlexander Motin 		}
573066f913aSAlexander Motin 		sectors = bytecount / ata_logical_sector_size(ident_buf);
5741e637ba6SAlexander Motin 
5751e637ba6SAlexander Motin 		cam_fill_ataio(ataio,
5761e637ba6SAlexander Motin 		    1,
5770f280cbdSWarner Losh 		    aprobedone,
5781e637ba6SAlexander Motin 		    CAM_DIR_NONE,
5791e637ba6SAlexander Motin 		    0,
5801e637ba6SAlexander Motin 		    NULL,
5811e637ba6SAlexander Motin 		    0,
5821e637ba6SAlexander Motin 		    30*1000);
5831e637ba6SAlexander Motin 		ata_28bit_cmd(ataio, ATA_SET_MULTI, 0, 0, sectors);
5841e637ba6SAlexander Motin 		break;
58552c9ce25SScott Long 	}
58652c9ce25SScott Long 	case PROBE_INQUIRY:
587066f913aSAlexander Motin 	{
588066f913aSAlexander Motin 		u_int bytecount;
589066f913aSAlexander Motin 
590066f913aSAlexander Motin 		bytecount = 8192;	/* SATA maximum */
591066f913aSAlexander Motin 		/* Fetch user bytecount from SIM. */
592066f913aSAlexander Motin 		bzero(&cts, sizeof(cts));
59383c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
594066f913aSAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
595066f913aSAlexander Motin 		cts.type = CTS_TYPE_USER_SETTINGS;
596066f913aSAlexander Motin 		xpt_action((union ccb *)&cts);
597066f913aSAlexander Motin 		if (path->device->transport == XPORT_ATA) {
598066f913aSAlexander Motin 			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
599066f913aSAlexander Motin 				bytecount = cts.xport_specific.ata.bytecount;
600066f913aSAlexander Motin 		} else {
601066f913aSAlexander Motin 			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
602066f913aSAlexander Motin 				bytecount = cts.xport_specific.sata.bytecount;
603066f913aSAlexander Motin 		}
604066f913aSAlexander Motin 		/* Honor device capabilities. */
605066f913aSAlexander Motin 		bytecount &= ~1;
606066f913aSAlexander Motin 		bytecount = max(2, min(65534, bytecount));
607066f913aSAlexander Motin 		if (ident_buf->satacapabilities != 0x0000 &&
608066f913aSAlexander Motin 		    ident_buf->satacapabilities != 0xffff) {
609066f913aSAlexander Motin 			bytecount = min(8192, bytecount);
610066f913aSAlexander Motin 		}
611066f913aSAlexander Motin 		/* Report bytecount to SIM. */
612066f913aSAlexander Motin 		bzero(&cts, sizeof(cts));
61383c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
614066f913aSAlexander Motin 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
615066f913aSAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
616066f913aSAlexander Motin 		if (path->device->transport == XPORT_ATA) {
617066f913aSAlexander Motin 			cts.xport_specific.ata.bytecount = bytecount;
618066f913aSAlexander Motin 			cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT;
619066f913aSAlexander Motin 		} else {
620066f913aSAlexander Motin 			cts.xport_specific.sata.bytecount = bytecount;
621066f913aSAlexander Motin 			cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT;
622066f913aSAlexander Motin 		}
623066f913aSAlexander Motin 		xpt_action((union ccb *)&cts);
624066f913aSAlexander Motin 		/* FALLTHROUGH */
625066f913aSAlexander Motin 	}
62652c9ce25SScott Long 	case PROBE_FULL_INQUIRY:
62752c9ce25SScott Long 	{
62852c9ce25SScott Long 		u_int inquiry_len;
62952c9ce25SScott Long 		struct scsi_inquiry_data *inq_buf =
63076d843daSAlexander Motin 		    &path->device->inq_data;
63152c9ce25SScott Long 
63252c9ce25SScott Long 		if (softc->action == PROBE_INQUIRY)
63352c9ce25SScott Long 			inquiry_len = SHORT_INQUIRY_LENGTH;
63452c9ce25SScott Long 		else
63552c9ce25SScott Long 			inquiry_len = SID_ADDITIONAL_LENGTH(inq_buf);
63652c9ce25SScott Long 		/*
63752c9ce25SScott Long 		 * Some parallel SCSI devices fail to send an
63852c9ce25SScott Long 		 * ignore wide residue message when dealing with
63952c9ce25SScott Long 		 * odd length inquiry requests.  Round up to be
64052c9ce25SScott Long 		 * safe.
64152c9ce25SScott Long 		 */
64252c9ce25SScott Long 		inquiry_len = roundup2(inquiry_len, 2);
64352c9ce25SScott Long 		scsi_inquiry(csio,
64452c9ce25SScott Long 			     /*retries*/1,
6450f280cbdSWarner Losh 			     aprobedone,
64652c9ce25SScott Long 			     MSG_SIMPLE_Q_TAG,
6479db2db6bSWarner Losh 			     (uint8_t *)inq_buf,
64852c9ce25SScott Long 			     inquiry_len,
64952c9ce25SScott Long 			     /*evpd*/FALSE,
65052c9ce25SScott Long 			     /*page_code*/0,
65152c9ce25SScott Long 			     SSD_MIN_SIZE,
65252c9ce25SScott Long 			     /*timeout*/60 * 1000);
65352c9ce25SScott Long 		break;
65452c9ce25SScott Long 	}
65552c9ce25SScott Long 	case PROBE_PM_PID:
65652c9ce25SScott Long 		cam_fill_ataio(ataio,
65752c9ce25SScott Long 		      1,
6580f280cbdSWarner Losh 		      aprobedone,
65952c9ce25SScott Long 		      /*flags*/CAM_DIR_NONE,
66065d0fb03SAlexander Motin 		      0,
66152c9ce25SScott Long 		      /*data_ptr*/NULL,
66252c9ce25SScott Long 		      /*dxfer_len*/0,
66352c9ce25SScott Long 		      10 * 1000);
66452c9ce25SScott Long 		ata_pm_read_cmd(ataio, 0, 15);
66552c9ce25SScott Long 		break;
66652c9ce25SScott Long 	case PROBE_PM_PRV:
66752c9ce25SScott Long 		cam_fill_ataio(ataio,
66852c9ce25SScott Long 		      1,
6690f280cbdSWarner Losh 		      aprobedone,
67052c9ce25SScott Long 		      /*flags*/CAM_DIR_NONE,
67165d0fb03SAlexander Motin 		      0,
67252c9ce25SScott Long 		      /*data_ptr*/NULL,
67352c9ce25SScott Long 		      /*dxfer_len*/0,
67452c9ce25SScott Long 		      10 * 1000);
67552c9ce25SScott Long 		ata_pm_read_cmd(ataio, 1, 15);
67652c9ce25SScott Long 		break;
6773089bb2eSAlexander Motin 	case PROBE_IDENTIFY_SES:
6783089bb2eSAlexander Motin 		cam_fill_ataio(ataio,
6793089bb2eSAlexander Motin 		      1,
6800f280cbdSWarner Losh 		      aprobedone,
6813089bb2eSAlexander Motin 		      /*flags*/CAM_DIR_IN,
6823089bb2eSAlexander Motin 		      0,
6839db2db6bSWarner Losh 		      /*data_ptr*/(uint8_t *)&softc->ident_data,
6843089bb2eSAlexander Motin 		      /*dxfer_len*/sizeof(softc->ident_data),
6853089bb2eSAlexander Motin 		      30 * 1000);
6863089bb2eSAlexander Motin 		ata_28bit_cmd(ataio, ATA_SEP_ATTN, 0xEC, 0x02,
6873089bb2eSAlexander Motin 		    sizeof(softc->ident_data) / 4);
6883089bb2eSAlexander Motin 		break;
6893089bb2eSAlexander Motin 	case PROBE_IDENTIFY_SAFTE:
6903089bb2eSAlexander Motin 		cam_fill_ataio(ataio,
6913089bb2eSAlexander Motin 		      1,
6920f280cbdSWarner Losh 		      aprobedone,
6933089bb2eSAlexander Motin 		      /*flags*/CAM_DIR_IN,
6943089bb2eSAlexander Motin 		      0,
6959db2db6bSWarner Losh 		      /*data_ptr*/(uint8_t *)&softc->ident_data,
6963089bb2eSAlexander Motin 		      /*dxfer_len*/sizeof(softc->ident_data),
6973089bb2eSAlexander Motin 		      30 * 1000);
6983089bb2eSAlexander Motin 		ata_28bit_cmd(ataio, ATA_SEP_ATTN, 0xEC, 0x00,
6993089bb2eSAlexander Motin 		    sizeof(softc->ident_data) / 4);
7003089bb2eSAlexander Motin 		break;
70152c9ce25SScott Long 	default:
7020f280cbdSWarner Losh 		panic("aprobestart: invalid action state 0x%x\n", softc->action);
70352c9ce25SScott Long 	}
704cccf4220SAlexander Motin 	start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
70552c9ce25SScott Long 	xpt_action(start_ccb);
70652c9ce25SScott Long }
707b9c473b2SAlexander Motin 
70852c9ce25SScott Long static void
aproberequestdefaultnegotiation(struct cam_periph * periph)7090f280cbdSWarner Losh aproberequestdefaultnegotiation(struct cam_periph *periph)
71052c9ce25SScott Long {
71152c9ce25SScott Long 	struct ccb_trans_settings cts;
71252c9ce25SScott Long 
713ec5325dbSEdward Tomasz Napierala 	bzero(&cts, sizeof(cts));
71483c5d981SAlexander Motin 	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
71552c9ce25SScott Long 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
71652c9ce25SScott Long 	cts.type = CTS_TYPE_USER_SETTINGS;
71752c9ce25SScott Long 	xpt_action((union ccb *)&cts);
718b9c473b2SAlexander Motin 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
71952c9ce25SScott Long 		return;
720b9c473b2SAlexander Motin 	cts.xport_specific.valid = 0;
72152c9ce25SScott Long 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
72252c9ce25SScott Long 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
72352c9ce25SScott Long 	xpt_action((union ccb *)&cts);
72452c9ce25SScott Long }
72552c9ce25SScott Long 
72652c9ce25SScott Long static void
aprobedone(struct cam_periph * periph,union ccb * done_ccb)7270f280cbdSWarner Losh aprobedone(struct cam_periph *periph, union ccb *done_ccb)
72852c9ce25SScott Long {
729c8039fc6SAlexander Motin 	struct ccb_trans_settings cts;
73052c9ce25SScott Long 	struct ata_params *ident_buf;
7313089bb2eSAlexander Motin 	struct scsi_inquiry_data *inq_buf;
7324762aa57SWarner Losh 	aprobe_softc *softc;
73352c9ce25SScott Long 	struct cam_path *path;
73426bdaeddSAlexander Motin 	cam_status status;
7359db2db6bSWarner Losh 	uint32_t  priority;
73676d843daSAlexander Motin 	u_int caps, oif;
73776d843daSAlexander Motin 	int changed, found = 1;
7383089bb2eSAlexander Motin 	static const uint8_t fake_device_id_hdr[8] =
7393089bb2eSAlexander Motin 	    {0, SVPD_DEVICE_ID, 0, 12,
7403089bb2eSAlexander Motin 	     SVPD_ID_CODESET_BINARY, SVPD_ID_TYPE_NAA, 0, 8};
74152c9ce25SScott Long 
7420f280cbdSWarner Losh 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("aprobedone\n"));
74352c9ce25SScott Long 
7444762aa57SWarner Losh 	softc = (aprobe_softc *)periph->softc;
74552c9ce25SScott Long 	path = done_ccb->ccb_h.path;
74652c9ce25SScott Long 	priority = done_ccb->ccb_h.pinfo.priority;
74752c9ce25SScott Long 	ident_buf = &path->device->ident_data;
7483089bb2eSAlexander Motin 	inq_buf = &path->device->inq_data;
74952c9ce25SScott Long 
7501e637ba6SAlexander Motin 	if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
7510191d9b3SAlexander Motin 		if (cam_periph_error(done_ccb,
752553484aeSWarner Losh 			0, softc->restart ? (SF_NO_RECOVERY | SF_NO_RETRY) : 0
753553484aeSWarner Losh 		    ) == ERESTART) {
754cccf4220SAlexander Motin out:
755cccf4220SAlexander Motin 			/* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
756cccf4220SAlexander Motin 			cam_release_devq(path, 0, 0, 0, FALSE);
7571e637ba6SAlexander Motin 			return;
758cccf4220SAlexander Motin 		}
75925a519a9SAlexander Motin 		if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
7601e637ba6SAlexander Motin 			/* Don't wedge the queue */
761cccf4220SAlexander Motin 			xpt_release_devq(path, /*count*/1, /*run_queue*/TRUE);
7621e637ba6SAlexander Motin 		}
76326bdaeddSAlexander Motin 		status = done_ccb->ccb_h.status & CAM_STATUS_MASK;
76425a519a9SAlexander Motin 		if (softc->restart) {
76525a519a9SAlexander Motin 			softc->faults++;
76625a519a9SAlexander Motin 			if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) ==
76725a519a9SAlexander Motin 			    CAM_CMD_TIMEOUT)
76825a519a9SAlexander Motin 				softc->faults += 4;
76925a519a9SAlexander Motin 			if (softc->faults < 10)
77025a519a9SAlexander Motin 				goto done;
77125a519a9SAlexander Motin 			else
77225a519a9SAlexander Motin 				softc->restart = 0;
77326bdaeddSAlexander Motin 
7741e637ba6SAlexander Motin 		/* Old PIO2 devices may not support mode setting. */
77526bdaeddSAlexander Motin 		} else if (softc->action == PROBE_SETMODE &&
77626bdaeddSAlexander Motin 		    status == CAM_ATA_STATUS_ERROR &&
7771e637ba6SAlexander Motin 		    ata_max_pmode(ident_buf) <= ATA_PIO2 &&
77826bdaeddSAlexander Motin 		    (ident_buf->capabilities1 & ATA_SUPPORT_IORDY) == 0) {
7791e637ba6SAlexander Motin 			goto noerror;
78026bdaeddSAlexander Motin 
78126bdaeddSAlexander Motin 		/*
78226bdaeddSAlexander Motin 		 * Some old WD SATA disks report supported and enabled
78326bdaeddSAlexander Motin 		 * device-initiated interface power management, but return
78426bdaeddSAlexander Motin 		 * ABORT on attempt to disable it.
78526bdaeddSAlexander Motin 		 */
78626bdaeddSAlexander Motin 		} else if (softc->action == PROBE_SETPM &&
78726bdaeddSAlexander Motin 		    status == CAM_ATA_STATUS_ERROR) {
78826bdaeddSAlexander Motin 			goto noerror;
789025e2c12SAlexander Motin 
790025e2c12SAlexander Motin 		/*
791b5617df5SAndriy Gapon 		 * Some old WD SATA disks have broken SPINUP handling.
792b5617df5SAndriy Gapon 		 * If we really fail to spin up the disk, then there will be
793b5617df5SAndriy Gapon 		 * some media access errors later on, but at least we will
794b5617df5SAndriy Gapon 		 * have a device to interact with for recovery attempts.
795b5617df5SAndriy Gapon 		 */
796b5617df5SAndriy Gapon 		} else if (softc->action == PROBE_SPINUP &&
797b5617df5SAndriy Gapon 		    status == CAM_ATA_STATUS_ERROR) {
798b5617df5SAndriy Gapon 			goto noerror;
799b5617df5SAndriy Gapon 
800b5617df5SAndriy Gapon 		/*
801025e2c12SAlexander Motin 		 * Some HP SATA disks report supported DMA Auto-Activation,
802025e2c12SAlexander Motin 		 * but return ABORT on attempt to enable it.
803025e2c12SAlexander Motin 		 */
804025e2c12SAlexander Motin 		} else if (softc->action == PROBE_SETDMAAA &&
805025e2c12SAlexander Motin 		    status == CAM_ATA_STATUS_ERROR) {
806025e2c12SAlexander Motin 			goto noerror;
8073089bb2eSAlexander Motin 
8083089bb2eSAlexander Motin 		/*
8093089bb2eSAlexander Motin 		 * SES and SAF-TE SEPs have different IDENTIFY commands,
8103089bb2eSAlexander Motin 		 * but SATA specification doesn't tell how to identify them.
8113089bb2eSAlexander Motin 		 * Until better way found, just try another if first fail.
8123089bb2eSAlexander Motin 		 */
8133089bb2eSAlexander Motin 		} else if (softc->action == PROBE_IDENTIFY_SES &&
8143089bb2eSAlexander Motin 		    status == CAM_ATA_STATUS_ERROR) {
8153089bb2eSAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_IDENTIFY_SAFTE);
8163089bb2eSAlexander Motin 			xpt_release_ccb(done_ccb);
8173089bb2eSAlexander Motin 			xpt_schedule(periph, priority);
818cccf4220SAlexander Motin 			goto out;
81926bdaeddSAlexander Motin 		}
82026bdaeddSAlexander Motin 
8211e637ba6SAlexander Motin 		/*
8221e637ba6SAlexander Motin 		 * If we get to this point, we got an error status back
8231e637ba6SAlexander Motin 		 * from the inquiry and the error status doesn't require
8241e637ba6SAlexander Motin 		 * automatically retrying the command.  Therefore, the
8251e637ba6SAlexander Motin 		 * inquiry failed.  If we had inquiry information before
8261e637ba6SAlexander Motin 		 * for this device, but this latest inquiry command failed,
8271e637ba6SAlexander Motin 		 * the device has probably gone away.  If this device isn't
8281e637ba6SAlexander Motin 		 * already marked unconfigured, notify the peripheral
8291e637ba6SAlexander Motin 		 * drivers that this device is no more.
8301e637ba6SAlexander Motin 		 */
83125a519a9SAlexander Motin device_fail:	if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
8321e637ba6SAlexander Motin 			xpt_async(AC_LOST_DEVICE, path, NULL);
833a4d953c4SAlexander Motin 		PROBE_SET_ACTION(softc, PROBE_INVALID);
8341e637ba6SAlexander Motin 		found = 0;
8351e637ba6SAlexander Motin 		goto done;
8361e637ba6SAlexander Motin 	}
8371e637ba6SAlexander Motin noerror:
838a9b8edb1SAlexander Motin 	if (softc->restart)
839a9b8edb1SAlexander Motin 		goto done;
84052c9ce25SScott Long 	switch (softc->action) {
84152c9ce25SScott Long 	case PROBE_RESET:
8421e637ba6SAlexander Motin 	{
84352c9ce25SScott Long 		int sign = (done_ccb->ataio.res.lba_high << 8) +
84452c9ce25SScott Long 		    done_ccb->ataio.res.lba_mid;
845a4d953c4SAlexander Motin 		CAM_DEBUG(path, CAM_DEBUG_PROBE,
846a4d953c4SAlexander Motin 		    ("SIGNATURE: %04x\n", sign));
84752c9ce25SScott Long 		if (sign == 0x0000 &&
84852c9ce25SScott Long 		    done_ccb->ccb_h.target_id != 15) {
84952c9ce25SScott Long 			path->device->protocol = PROTO_ATA;
85052c9ce25SScott Long 			PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
85152c9ce25SScott Long 		} else if (sign == 0x9669 &&
85252c9ce25SScott Long 		    done_ccb->ccb_h.target_id == 15) {
85352c9ce25SScott Long 			/* Report SIM that PM is present. */
85452c9ce25SScott Long 			bzero(&cts, sizeof(cts));
85583c5d981SAlexander Motin 			xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
85652c9ce25SScott Long 			cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
85752c9ce25SScott Long 			cts.type = CTS_TYPE_CURRENT_SETTINGS;
85852c9ce25SScott Long 			cts.xport_specific.sata.pm_present = 1;
85952c9ce25SScott Long 			cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
86052c9ce25SScott Long 			xpt_action((union ccb *)&cts);
86152c9ce25SScott Long 			path->device->protocol = PROTO_SATAPM;
86252c9ce25SScott Long 			PROBE_SET_ACTION(softc, PROBE_PM_PID);
8633089bb2eSAlexander Motin 		} else if (sign == 0xc33c &&
8643089bb2eSAlexander Motin 		    done_ccb->ccb_h.target_id != 15) {
8653089bb2eSAlexander Motin 			path->device->protocol = PROTO_SEMB;
8663089bb2eSAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_IDENTIFY_SES);
86752c9ce25SScott Long 		} else if (sign == 0xeb14 &&
86852c9ce25SScott Long 		    done_ccb->ccb_h.target_id != 15) {
86952c9ce25SScott Long 			path->device->protocol = PROTO_SCSI;
87052c9ce25SScott Long 			PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
87152c9ce25SScott Long 		} else {
87252c9ce25SScott Long 			if (done_ccb->ccb_h.target_id != 15) {
87352c9ce25SScott Long 				xpt_print(path,
87452c9ce25SScott Long 				    "Unexpected signature 0x%04x\n", sign);
87552c9ce25SScott Long 			}
87665d0fb03SAlexander Motin 			goto device_fail;
87752c9ce25SScott Long 		}
87852c9ce25SScott Long 		xpt_release_ccb(done_ccb);
87952c9ce25SScott Long 		xpt_schedule(periph, priority);
880cccf4220SAlexander Motin 		goto out;
88152c9ce25SScott Long 	}
88252c9ce25SScott Long 	case PROBE_IDENTIFY:
88352c9ce25SScott Long 	{
884699f853bSAlexander Motin 		struct ccb_pathinq cpi;
885a11463fdSSepherosa Ziehau 		int veto = 0;
88652c9ce25SScott Long 
88776769dc1SWarner Losh 		/*
88876769dc1SWarner Losh 		 * Convert to host byte order, and fix the strings.
88976769dc1SWarner Losh 		 */
890a9b8edb1SAlexander Motin 		ident_buf = &softc->ident_data;
891296218d4SWarner Losh 		ata_param_fixup(ident_buf);
89276769dc1SWarner Losh 
89376769dc1SWarner Losh 		/*
89476769dc1SWarner Losh 		 * Allow others to veto this ATA disk attachment.  This
89576769dc1SWarner Losh 		 * is mainly used by VMs, whose disk controllers may
89676769dc1SWarner Losh 		 * share the disks with the simulated ATA controllers.
89776769dc1SWarner Losh 		 */
89876769dc1SWarner Losh 		EVENTHANDLER_INVOKE(ada_probe_veto, path, ident_buf, &veto);
89976769dc1SWarner Losh 		if (veto) {
90076769dc1SWarner Losh 			goto device_fail;
90176769dc1SWarner Losh 		}
90276769dc1SWarner Losh 
9034ef08dc5SAlexander Motin 		/* Device may need spin-up before IDENTIFY become valid. */
9041254b680SAlexander Motin 		if ((ident_buf->specconf == 0x37c8 ||
9051254b680SAlexander Motin 		     ident_buf->specconf == 0x738c) &&
9061254b680SAlexander Motin 		    ((ident_buf->config & ATA_RESP_INCOMPLETE) ||
9074ef08dc5SAlexander Motin 		     softc->spinup == 0)) {
9084ef08dc5SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_SPINUP);
9094ef08dc5SAlexander Motin 			xpt_release_ccb(done_ccb);
9104ef08dc5SAlexander Motin 			xpt_schedule(periph, priority);
911cccf4220SAlexander Motin 			goto out;
9124ef08dc5SAlexander Motin 		}
913a9b8edb1SAlexander Motin 		ident_buf = &path->device->ident_data;
91476d843daSAlexander Motin 
91576d843daSAlexander Motin 		/* Check that it is the same device as we know. */
91652c9ce25SScott Long 		if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
917a9b8edb1SAlexander Motin 			if (bcmp(softc->ident_data.model, ident_buf->model,
918a9b8edb1SAlexander Motin 			     sizeof(ident_buf->model)) ||
919a9b8edb1SAlexander Motin 			    bcmp(softc->ident_data.serial, ident_buf->serial,
920a9b8edb1SAlexander Motin 			     sizeof(ident_buf->serial))) {
92176d843daSAlexander Motin 				/* The device was replaced. */
92276d843daSAlexander Motin 				changed = 2;
92352c9ce25SScott Long 				xpt_async(AC_LOST_DEVICE, path, NULL);
92476d843daSAlexander Motin 			} else if (bcmp(&softc->ident_data, ident_buf,
92576d843daSAlexander Motin 			     sizeof(*ident_buf))) {
92676d843daSAlexander Motin 				/* The device is the same, but has changed. */
92776d843daSAlexander Motin 				changed = 1;
9281e637ba6SAlexander Motin 			} else {
92976d843daSAlexander Motin 				/* Nothing has changed. */
9302eb4a8dcSAlexander Motin 				changed = 0;
9312eb4a8dcSAlexander Motin 			}
93276d843daSAlexander Motin 		} else {
93376d843daSAlexander Motin 			/* This is a new device. */
93476d843daSAlexander Motin 			changed = 2;
9352eb4a8dcSAlexander Motin 		}
93676d843daSAlexander Motin 
93776d843daSAlexander Motin 		if (changed != 0)
9382eb4a8dcSAlexander Motin 			bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
93976d843daSAlexander Motin 		if (changed == 2) {
94052c9ce25SScott Long 			/* Clean up from previous instance of this device */
94152c9ce25SScott Long 			if (path->device->serial_num != NULL) {
94252c9ce25SScott Long 				free(path->device->serial_num, M_CAMXPT);
94352c9ce25SScott Long 				path->device->serial_num = NULL;
94452c9ce25SScott Long 				path->device->serial_num_len = 0;
94552c9ce25SScott Long 			}
9463089bb2eSAlexander Motin 			if (path->device->device_id != NULL) {
9473089bb2eSAlexander Motin 				free(path->device->device_id, M_CAMXPT);
9483089bb2eSAlexander Motin 				path->device->device_id = NULL;
9493089bb2eSAlexander Motin 				path->device->device_id_len = 0;
9503089bb2eSAlexander Motin 			}
95152c9ce25SScott Long 			path->device->serial_num =
9529db2db6bSWarner Losh 				(uint8_t *)malloc((sizeof(ident_buf->serial) + 1),
95352c9ce25SScott Long 					   M_CAMXPT, M_NOWAIT);
95452c9ce25SScott Long 			if (path->device->serial_num != NULL) {
95552c9ce25SScott Long 				bcopy(ident_buf->serial,
95652c9ce25SScott Long 				      path->device->serial_num,
95752c9ce25SScott Long 				      sizeof(ident_buf->serial));
95852c9ce25SScott Long 				path->device->serial_num[sizeof(ident_buf->serial)]
95952c9ce25SScott Long 				    = '\0';
96052c9ce25SScott Long 				path->device->serial_num_len =
96152c9ce25SScott Long 				    strlen(path->device->serial_num);
96252c9ce25SScott Long 			}
9633089bb2eSAlexander Motin 			if (ident_buf->enabled.extension &
9643089bb2eSAlexander Motin 			    ATA_SUPPORT_64BITWWN) {
9653089bb2eSAlexander Motin 				path->device->device_id =
9663089bb2eSAlexander Motin 				    malloc(16, M_CAMXPT, M_NOWAIT);
9673089bb2eSAlexander Motin 				if (path->device->device_id != NULL) {
9683089bb2eSAlexander Motin 					path->device->device_id_len = 16;
9693089bb2eSAlexander Motin 					bcopy(&fake_device_id_hdr,
9703089bb2eSAlexander Motin 					    path->device->device_id, 8);
9713d6dd54eSAlexander Motin 					bcopy(ident_buf->wwn,
9723d6dd54eSAlexander Motin 					    path->device->device_id + 8, 8);
9733d6dd54eSAlexander Motin 					ata_bswap(path->device->device_id + 8, 8);
9743089bb2eSAlexander Motin 				}
9753089bb2eSAlexander Motin 			}
9764b997c49SAlexander Motin 			path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
9771e637ba6SAlexander Motin 		}
97876d843daSAlexander Motin 		if (changed == 1)
97976d843daSAlexander Motin 			xpt_async(AC_GETDEV_CHANGED, path, NULL);
98030a4094fSAlexander Motin 		if (ident_buf->satacapabilities & ATA_SUPPORT_NCQ) {
9817dc3213dSAlexander Motin 			path->device->mintags = 2;
9827dc3213dSAlexander Motin 			path->device->maxtags =
98330a4094fSAlexander Motin 			    ATA_QUEUE_LEN(ident_buf->queue) + 1;
98430a4094fSAlexander Motin 		}
98530a4094fSAlexander Motin 		ata_find_quirk(path->device);
986507e5811SAlexander Motin 		if (path->device->mintags != 0 &&
987507e5811SAlexander Motin 		    path->bus->sim->max_tagged_dev_openings != 0) {
988699f853bSAlexander Motin 			/* Check if the SIM does not want queued commands. */
989762a7f4fSWarner Losh 			xpt_path_inq(&cpi, path);
990fd02926aSWarner Losh 			if (cam_ccb_success((union ccb *)&cpi) &&
991699f853bSAlexander Motin 			    (cpi.hba_inquiry & PI_TAG_ABLE)) {
992c8039fc6SAlexander Motin 				/* Report SIM which tags are allowed. */
993c8039fc6SAlexander Motin 				bzero(&cts, sizeof(cts));
99483c5d981SAlexander Motin 				xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
995c8039fc6SAlexander Motin 				cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
996c8039fc6SAlexander Motin 				cts.type = CTS_TYPE_CURRENT_SETTINGS;
997c8039fc6SAlexander Motin 				cts.xport_specific.sata.tags = path->device->maxtags;
998c8039fc6SAlexander Motin 				cts.xport_specific.sata.valid = CTS_SATA_VALID_TAGS;
999c8039fc6SAlexander Motin 				xpt_action((union ccb *)&cts);
100030a4094fSAlexander Motin 			}
1001699f853bSAlexander Motin 		}
1002bc1bf6e8SAlexander Motin 		ata_device_transport(path);
100376d843daSAlexander Motin 		if (changed == 2)
10040f280cbdSWarner Losh 			aproberequestdefaultnegotiation(periph);
100552c9ce25SScott Long 		PROBE_SET_ACTION(softc, PROBE_SETMODE);
100652c9ce25SScott Long 		xpt_release_ccb(done_ccb);
100752c9ce25SScott Long 		xpt_schedule(periph, priority);
1008cccf4220SAlexander Motin 		goto out;
100952c9ce25SScott Long 	}
10104ef08dc5SAlexander Motin 	case PROBE_SPINUP:
10114ef08dc5SAlexander Motin 		if (bootverbose)
10124ef08dc5SAlexander Motin 			xpt_print(path, "Spin-up done\n");
10134ef08dc5SAlexander Motin 		softc->spinup = 1;
10144ef08dc5SAlexander Motin 		PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
10154ef08dc5SAlexander Motin 		xpt_release_ccb(done_ccb);
10164ef08dc5SAlexander Motin 		xpt_schedule(periph, priority);
1017cccf4220SAlexander Motin 		goto out;
101852c9ce25SScott Long 	case PROBE_SETMODE:
1019da6808c1SAlexander Motin 		/* Set supported bits. */
1020da6808c1SAlexander Motin 		bzero(&cts, sizeof(cts));
1021da6808c1SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1022da6808c1SAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1023da6808c1SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
1024da6808c1SAlexander Motin 		xpt_action((union ccb *)&cts);
10252e1eb332SMarius Strobl 		if (path->device->transport == XPORT_SATA &&
10262e1eb332SMarius Strobl 		    cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1027da6808c1SAlexander Motin 			caps = cts.xport_specific.sata.caps & CTS_SATA_CAPS_H;
10282e1eb332SMarius Strobl 		else if (path->device->transport == XPORT_ATA &&
10292e1eb332SMarius Strobl 		    cts.xport_specific.ata.valid & CTS_ATA_VALID_CAPS)
10302e1eb332SMarius Strobl 			caps = cts.xport_specific.ata.caps & CTS_ATA_CAPS_H;
1031da6808c1SAlexander Motin 		else
1032da6808c1SAlexander Motin 			caps = 0;
10332e1eb332SMarius Strobl 		if (path->device->transport == XPORT_SATA &&
10342e1eb332SMarius Strobl 		    ident_buf->satacapabilities != 0xffff) {
1035da6808c1SAlexander Motin 			if (ident_buf->satacapabilities & ATA_SUPPORT_IFPWRMNGTRCV)
1036da6808c1SAlexander Motin 				caps |= CTS_SATA_CAPS_D_PMREQ;
1037da6808c1SAlexander Motin 			if (ident_buf->satacapabilities & ATA_SUPPORT_HAPST)
1038da6808c1SAlexander Motin 				caps |= CTS_SATA_CAPS_D_APST;
1039da6808c1SAlexander Motin 		}
1040da6808c1SAlexander Motin 		/* Mask unwanted bits. */
1041da6808c1SAlexander Motin 		bzero(&cts, sizeof(cts));
1042da6808c1SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1043da6808c1SAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1044da6808c1SAlexander Motin 		cts.type = CTS_TYPE_USER_SETTINGS;
1045da6808c1SAlexander Motin 		xpt_action((union ccb *)&cts);
10462e1eb332SMarius Strobl 		if (path->device->transport == XPORT_SATA &&
10472e1eb332SMarius Strobl 		    cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1048da6808c1SAlexander Motin 			caps &= cts.xport_specific.sata.caps;
10492e1eb332SMarius Strobl 		else if (path->device->transport == XPORT_ATA &&
10502e1eb332SMarius Strobl 		    cts.xport_specific.ata.valid & CTS_ATA_VALID_CAPS)
10512e1eb332SMarius Strobl 			caps &= cts.xport_specific.ata.caps;
1052c2b82f3eSAlexander Motin 		else
1053c2b82f3eSAlexander Motin 			caps = 0;
10542e1eb332SMarius Strobl 		/*
10552e1eb332SMarius Strobl 		 * Remember what transport thinks about 48-bit DMA.  If
10562e1eb332SMarius Strobl 		 * capability information is not provided or transport is
10572e1eb332SMarius Strobl 		 * SATA, we take support for granted.
10582e1eb332SMarius Strobl 		 */
105976d843daSAlexander Motin 		oif = path->device->inq_flags;
10602e1eb332SMarius Strobl 		if (!(path->device->inq_flags & SID_DMA) ||
10612e1eb332SMarius Strobl 		    (path->device->transport == XPORT_ATA &&
10622e1eb332SMarius Strobl 		    (cts.xport_specific.ata.valid & CTS_ATA_VALID_CAPS) &&
10632e1eb332SMarius Strobl 		    !(caps & CTS_ATA_CAPS_H_DMA48)))
10642e1eb332SMarius Strobl 			path->device->inq_flags &= ~SID_DMA48;
10652e1eb332SMarius Strobl 		else
10662e1eb332SMarius Strobl 			path->device->inq_flags |= SID_DMA48;
106776d843daSAlexander Motin 		if (path->device->inq_flags != oif)
106876d843daSAlexander Motin 			xpt_async(AC_GETDEV_CHANGED, path, NULL);
1069da6808c1SAlexander Motin 		/* Store result to SIM. */
1070da6808c1SAlexander Motin 		bzero(&cts, sizeof(cts));
1071da6808c1SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1072da6808c1SAlexander Motin 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1073da6808c1SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
10742e1eb332SMarius Strobl 		if (path->device->transport == XPORT_SATA) {
1075da6808c1SAlexander Motin 			cts.xport_specific.sata.caps = caps;
1076da6808c1SAlexander Motin 			cts.xport_specific.sata.valid = CTS_SATA_VALID_CAPS;
10772e1eb332SMarius Strobl 		} else {
10782e1eb332SMarius Strobl 			cts.xport_specific.ata.caps = caps;
10792e1eb332SMarius Strobl 			cts.xport_specific.ata.valid = CTS_ATA_VALID_CAPS;
10802e1eb332SMarius Strobl 		}
1081da6808c1SAlexander Motin 		xpt_action((union ccb *)&cts);
1082da6808c1SAlexander Motin 		softc->caps = caps;
10832e1eb332SMarius Strobl 		if (path->device->transport != XPORT_SATA)
10842e1eb332SMarius Strobl 			goto notsata;
1085958e4a69SAlexander Motin 		if ((ident_buf->satasupport & ATA_SUPPORT_IFPWRMNGT) &&
1086958e4a69SAlexander Motin 		    (!(softc->caps & CTS_SATA_CAPS_H_PMREQ)) !=
1087958e4a69SAlexander Motin 		    (!(ident_buf->sataenabled & ATA_SUPPORT_IFPWRMNGT))) {
1088da6808c1SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_SETPM);
1089da6808c1SAlexander Motin 			xpt_release_ccb(done_ccb);
1090da6808c1SAlexander Motin 			xpt_schedule(periph, priority);
1091cccf4220SAlexander Motin 			goto out;
1092da6808c1SAlexander Motin 		}
1093da6808c1SAlexander Motin 		/* FALLTHROUGH */
1094da6808c1SAlexander Motin 	case PROBE_SETPM:
1095da6808c1SAlexander Motin 		if (ident_buf->satacapabilities != 0xffff &&
1096958e4a69SAlexander Motin 		    (ident_buf->satacapabilities & ATA_SUPPORT_DAPST) &&
1097958e4a69SAlexander Motin 		    (!(softc->caps & CTS_SATA_CAPS_H_APST)) !=
1098958e4a69SAlexander Motin 		    (!(ident_buf->sataenabled & ATA_ENABLED_DAPST))) {
1099da6808c1SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_SETAPST);
1100da6808c1SAlexander Motin 			xpt_release_ccb(done_ccb);
1101da6808c1SAlexander Motin 			xpt_schedule(periph, priority);
1102cccf4220SAlexander Motin 			goto out;
1103da6808c1SAlexander Motin 		}
1104da6808c1SAlexander Motin 		/* FALLTHROUGH */
1105da6808c1SAlexander Motin 	case PROBE_SETAPST:
1106958e4a69SAlexander Motin 		if ((ident_buf->satasupport & ATA_SUPPORT_AUTOACTIVATE) &&
1107958e4a69SAlexander Motin 		    (!(softc->caps & CTS_SATA_CAPS_H_DMAAA)) !=
1108958e4a69SAlexander Motin 		    (!(ident_buf->sataenabled & ATA_SUPPORT_AUTOACTIVATE))) {
1109da6808c1SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_SETDMAAA);
1110da6808c1SAlexander Motin 			xpt_release_ccb(done_ccb);
1111da6808c1SAlexander Motin 			xpt_schedule(periph, priority);
1112cccf4220SAlexander Motin 			goto out;
1113da6808c1SAlexander Motin 		}
1114da6808c1SAlexander Motin 		/* FALLTHROUGH */
1115da6808c1SAlexander Motin 	case PROBE_SETDMAAA:
1116e4bed0b4SWarner Losh 		if (path->device->protocol != PROTO_ATA &&
1117e4bed0b4SWarner Losh 		    (ident_buf->satasupport & ATA_SUPPORT_ASYNCNOTIF) &&
11188d169381SAlexander Motin 		    (!(softc->caps & CTS_SATA_CAPS_H_AN)) !=
11198d169381SAlexander Motin 		    (!(ident_buf->sataenabled & ATA_SUPPORT_ASYNCNOTIF))) {
11208d169381SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_SETAN);
11218d169381SAlexander Motin 			xpt_release_ccb(done_ccb);
11228d169381SAlexander Motin 			xpt_schedule(periph, priority);
1123cccf4220SAlexander Motin 			goto out;
11248d169381SAlexander Motin 		}
11258d169381SAlexander Motin 		/* FALLTHROUGH */
11268d169381SAlexander Motin 	case PROBE_SETAN:
1127da6808c1SAlexander Motin notsata:
11281e637ba6SAlexander Motin 		if (path->device->protocol == PROTO_ATA) {
11291e637ba6SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_SET_MULTI);
11301e637ba6SAlexander Motin 		} else {
11311e637ba6SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_INQUIRY);
11321e637ba6SAlexander Motin 		}
11331e637ba6SAlexander Motin 		xpt_release_ccb(done_ccb);
11341e637ba6SAlexander Motin 		xpt_schedule(periph, priority);
1135cccf4220SAlexander Motin 		goto out;
11361e637ba6SAlexander Motin 	case PROBE_SET_MULTI:
11371e637ba6SAlexander Motin 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
113852c9ce25SScott Long 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1139f98d7a47SAlexander Motin 			xpt_acquire_device(path->device);
114052c9ce25SScott Long 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
114152c9ce25SScott Long 			xpt_action(done_ccb);
1142cccf4220SAlexander Motin 			xpt_async(AC_FOUND_DEVICE, path, done_ccb);
11431e637ba6SAlexander Motin 		}
1144a4d953c4SAlexander Motin 		PROBE_SET_ACTION(softc, PROBE_DONE);
114552c9ce25SScott Long 		break;
114652c9ce25SScott Long 	case PROBE_INQUIRY:
114752c9ce25SScott Long 	case PROBE_FULL_INQUIRY:
114852c9ce25SScott Long 	{
11499db2db6bSWarner Losh 		uint8_t periph_qual, len;
115052c9ce25SScott Long 
115152c9ce25SScott Long 		path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
115252c9ce25SScott Long 
115352c9ce25SScott Long 		periph_qual = SID_QUAL(inq_buf);
115452c9ce25SScott Long 
115592024858SAlexander Motin 		if (periph_qual != SID_QUAL_LU_CONNECTED &&
115692024858SAlexander Motin 		    periph_qual != SID_QUAL_LU_OFFLINE)
11571e637ba6SAlexander Motin 			break;
115852c9ce25SScott Long 
115952c9ce25SScott Long 		/*
116052c9ce25SScott Long 		 * We conservatively request only
116152c9ce25SScott Long 		 * SHORT_INQUIRY_LEN bytes of inquiry
116252c9ce25SScott Long 		 * information during our first try
116352c9ce25SScott Long 		 * at sending an INQUIRY. If the device
116452c9ce25SScott Long 		 * has more information to give,
116552c9ce25SScott Long 		 * perform a second request specifying
116652c9ce25SScott Long 		 * the amount of information the device
116752c9ce25SScott Long 		 * is willing to give.
116852c9ce25SScott Long 		 */
116952c9ce25SScott Long 		len = inq_buf->additional_length
11701e637ba6SAlexander Motin 		    + offsetof(struct scsi_inquiry_data, additional_length) + 1;
117152c9ce25SScott Long 		if (softc->action == PROBE_INQUIRY
117252c9ce25SScott Long 		    && len > SHORT_INQUIRY_LENGTH) {
117352c9ce25SScott Long 			PROBE_SET_ACTION(softc, PROBE_FULL_INQUIRY);
117452c9ce25SScott Long 			xpt_release_ccb(done_ccb);
117552c9ce25SScott Long 			xpt_schedule(periph, priority);
1176cccf4220SAlexander Motin 			goto out;
117752c9ce25SScott Long 		}
117852c9ce25SScott Long 
11794b997c49SAlexander Motin 		ata_device_transport(path);
11801e637ba6SAlexander Motin 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
118152c9ce25SScott Long 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1182f98d7a47SAlexander Motin 			xpt_acquire_device(path->device);
118352c9ce25SScott Long 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
118452c9ce25SScott Long 			xpt_action(done_ccb);
1185cccf4220SAlexander Motin 			xpt_async(AC_FOUND_DEVICE, path, done_ccb);
11861e637ba6SAlexander Motin 		}
1187a4d953c4SAlexander Motin 		PROBE_SET_ACTION(softc, PROBE_DONE);
118852c9ce25SScott Long 		break;
118952c9ce25SScott Long 	}
119052c9ce25SScott Long 	case PROBE_PM_PID:
11914b997c49SAlexander Motin 		if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) == 0)
119252c9ce25SScott Long 			bzero(ident_buf, sizeof(*ident_buf));
119352c9ce25SScott Long 		softc->pm_pid = (done_ccb->ataio.res.lba_high << 24) +
119452c9ce25SScott Long 		    (done_ccb->ataio.res.lba_mid << 16) +
119552c9ce25SScott Long 		    (done_ccb->ataio.res.lba_low << 8) +
119652c9ce25SScott Long 		    done_ccb->ataio.res.sector_count;
119765d0fb03SAlexander Motin 		((uint32_t *)ident_buf)[0] = softc->pm_pid;
119852c9ce25SScott Long 		snprintf(ident_buf->model, sizeof(ident_buf->model),
119952c9ce25SScott Long 		    "Port Multiplier %08x", softc->pm_pid);
120052c9ce25SScott Long 		PROBE_SET_ACTION(softc, PROBE_PM_PRV);
120152c9ce25SScott Long 		xpt_release_ccb(done_ccb);
120252c9ce25SScott Long 		xpt_schedule(periph, priority);
1203cccf4220SAlexander Motin 		goto out;
120452c9ce25SScott Long 	case PROBE_PM_PRV:
120552c9ce25SScott Long 		softc->pm_prv = (done_ccb->ataio.res.lba_high << 24) +
120652c9ce25SScott Long 		    (done_ccb->ataio.res.lba_mid << 16) +
120752c9ce25SScott Long 		    (done_ccb->ataio.res.lba_low << 8) +
120852c9ce25SScott Long 		    done_ccb->ataio.res.sector_count;
120965d0fb03SAlexander Motin 		((uint32_t *)ident_buf)[1] = softc->pm_prv;
121052c9ce25SScott Long 		snprintf(ident_buf->revision, sizeof(ident_buf->revision),
121152c9ce25SScott Long 		    "%04x", softc->pm_prv);
12124b997c49SAlexander Motin 		path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
1213bc1bf6e8SAlexander Motin 		ata_device_transport(path);
1214bc1bf6e8SAlexander Motin 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED)
12150f280cbdSWarner Losh 			aproberequestdefaultnegotiation(periph);
1216da6808c1SAlexander Motin 		/* Set supported bits. */
1217da6808c1SAlexander Motin 		bzero(&cts, sizeof(cts));
1218da6808c1SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1219da6808c1SAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1220da6808c1SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
1221da6808c1SAlexander Motin 		xpt_action((union ccb *)&cts);
1222da6808c1SAlexander Motin 		if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1223da6808c1SAlexander Motin 			caps = cts.xport_specific.sata.caps & CTS_SATA_CAPS_H;
1224da6808c1SAlexander Motin 		else
1225da6808c1SAlexander Motin 			caps = 0;
1226da6808c1SAlexander Motin 		/* All PMPs must support PM requests. */
1227da6808c1SAlexander Motin 		caps |= CTS_SATA_CAPS_D_PMREQ;
1228da6808c1SAlexander Motin 		/* Mask unwanted bits. */
1229da6808c1SAlexander Motin 		bzero(&cts, sizeof(cts));
1230da6808c1SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1231da6808c1SAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1232da6808c1SAlexander Motin 		cts.type = CTS_TYPE_USER_SETTINGS;
1233da6808c1SAlexander Motin 		xpt_action((union ccb *)&cts);
1234da6808c1SAlexander Motin 		if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1235da6808c1SAlexander Motin 			caps &= cts.xport_specific.sata.caps;
1236c2b82f3eSAlexander Motin 		else
1237c2b82f3eSAlexander Motin 			caps = 0;
12382e1eb332SMarius Strobl 		/* Remember what transport thinks about AEN. */
123976d843daSAlexander Motin 		oif = path->device->inq_flags;
1240e4bed0b4SWarner Losh 		if ((caps & CTS_SATA_CAPS_H_AN) && path->device->protocol != PROTO_ATA)
12412e1eb332SMarius Strobl 			path->device->inq_flags |= SID_AEN;
12422e1eb332SMarius Strobl 		else
12432e1eb332SMarius Strobl 			path->device->inq_flags &= ~SID_AEN;
1244da6808c1SAlexander Motin 		/* Store result to SIM. */
1245da6808c1SAlexander Motin 		bzero(&cts, sizeof(cts));
1246da6808c1SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1247da6808c1SAlexander Motin 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1248da6808c1SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
1249da6808c1SAlexander Motin 		cts.xport_specific.sata.caps = caps;
1250da6808c1SAlexander Motin 		cts.xport_specific.sata.valid = CTS_SATA_VALID_CAPS;
1251da6808c1SAlexander Motin 		xpt_action((union ccb *)&cts);
1252da6808c1SAlexander Motin 		softc->caps = caps;
125365d0fb03SAlexander Motin 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
125452c9ce25SScott Long 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1255f98d7a47SAlexander Motin 			xpt_acquire_device(path->device);
125652c9ce25SScott Long 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
125752c9ce25SScott Long 			xpt_action(done_ccb);
1258cccf4220SAlexander Motin 			xpt_async(AC_FOUND_DEVICE, path, done_ccb);
125965d0fb03SAlexander Motin 		} else {
126076d843daSAlexander Motin 			if (path->device->inq_flags != oif)
126176d843daSAlexander Motin 				xpt_async(AC_GETDEV_CHANGED, path, NULL);
126265d0fb03SAlexander Motin 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
126365d0fb03SAlexander Motin 			xpt_action(done_ccb);
1264cccf4220SAlexander Motin 			xpt_async(AC_SCSI_AEN, path, done_ccb);
126552c9ce25SScott Long 		}
1266a4d953c4SAlexander Motin 		PROBE_SET_ACTION(softc, PROBE_DONE);
126752c9ce25SScott Long 		break;
12683089bb2eSAlexander Motin 	case PROBE_IDENTIFY_SES:
12693089bb2eSAlexander Motin 	case PROBE_IDENTIFY_SAFTE:
12703089bb2eSAlexander Motin 		if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
12713089bb2eSAlexander Motin 			/* Check that it is the same device. */
12723089bb2eSAlexander Motin 			if (bcmp(&softc->ident_data, ident_buf, 53)) {
12733089bb2eSAlexander Motin 				/* Device changed. */
127476d843daSAlexander Motin 				changed = 2;
12753089bb2eSAlexander Motin 				xpt_async(AC_LOST_DEVICE, path, NULL);
12763089bb2eSAlexander Motin 			} else {
12773089bb2eSAlexander Motin 				bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
12783089bb2eSAlexander Motin 				changed = 0;
12793089bb2eSAlexander Motin 			}
128076d843daSAlexander Motin 		} else
128176d843daSAlexander Motin 			changed = 2;
12823089bb2eSAlexander Motin 		if (changed) {
12833089bb2eSAlexander Motin 			bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
12843089bb2eSAlexander Motin 			/* Clean up from previous instance of this device */
12853089bb2eSAlexander Motin 			if (path->device->device_id != NULL) {
12863089bb2eSAlexander Motin 				free(path->device->device_id, M_CAMXPT);
12873089bb2eSAlexander Motin 				path->device->device_id = NULL;
12883089bb2eSAlexander Motin 				path->device->device_id_len = 0;
12893089bb2eSAlexander Motin 			}
12903089bb2eSAlexander Motin 			path->device->device_id =
12913089bb2eSAlexander Motin 			    malloc(16, M_CAMXPT, M_NOWAIT);
12923089bb2eSAlexander Motin 			if (path->device->device_id != NULL) {
12933089bb2eSAlexander Motin 				path->device->device_id_len = 16;
12943089bb2eSAlexander Motin 				bcopy(&fake_device_id_hdr,
12953089bb2eSAlexander Motin 				    path->device->device_id, 8);
12963089bb2eSAlexander Motin 				bcopy(((uint8_t*)ident_buf) + 2,
12973089bb2eSAlexander Motin 				    path->device->device_id + 8, 8);
12983089bb2eSAlexander Motin 			}
12993089bb2eSAlexander Motin 
13003089bb2eSAlexander Motin 			path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
13013089bb2eSAlexander Motin 		}
1302bc1bf6e8SAlexander Motin 		ata_device_transport(path);
1303bc1bf6e8SAlexander Motin 		if (changed)
13040f280cbdSWarner Losh 			aproberequestdefaultnegotiation(periph);
13053089bb2eSAlexander Motin 
13063089bb2eSAlexander Motin 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
13073089bb2eSAlexander Motin 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
13083089bb2eSAlexander Motin 			xpt_acquire_device(path->device);
13093089bb2eSAlexander Motin 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
13103089bb2eSAlexander Motin 			xpt_action(done_ccb);
1311cccf4220SAlexander Motin 			xpt_async(AC_FOUND_DEVICE, path, done_ccb);
13123089bb2eSAlexander Motin 		}
1313a4d953c4SAlexander Motin 		PROBE_SET_ACTION(softc, PROBE_DONE);
13143089bb2eSAlexander Motin 		break;
131552c9ce25SScott Long 	default:
13160f280cbdSWarner Losh 		panic("aprobedone: invalid action state 0x%x\n", softc->action);
131752c9ce25SScott Long 	}
13181e637ba6SAlexander Motin done:
131983c5d981SAlexander Motin 	if (softc->restart) {
132083c5d981SAlexander Motin 		softc->restart = 0;
13211e637ba6SAlexander Motin 		xpt_release_ccb(done_ccb);
13220f280cbdSWarner Losh 		aprobeschedule(periph);
1323cccf4220SAlexander Motin 		goto out;
132483c5d981SAlexander Motin 	}
132583c5d981SAlexander Motin 	xpt_release_ccb(done_ccb);
1326a4d953c4SAlexander Motin 	CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe completed\n"));
132783c5d981SAlexander Motin 	while ((done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs))) {
132883c5d981SAlexander Motin 		TAILQ_REMOVE(&softc->request_ccbs,
132983c5d981SAlexander Motin 		    &done_ccb->ccb_h, periph_links.tqe);
133083c5d981SAlexander Motin 		done_ccb->ccb_h.status = found ? CAM_REQ_CMP : CAM_REQ_CMP_ERR;
133152c9ce25SScott Long 		xpt_done(done_ccb);
133283c5d981SAlexander Motin 	}
1333cccf4220SAlexander Motin 	/* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
1334cccf4220SAlexander Motin 	cam_release_devq(path, 0, 0, 0, FALSE);
13352e3f592bSAlexander Motin 	cam_periph_invalidate(periph);
133652c9ce25SScott Long 	cam_periph_release_locked(periph);
133752c9ce25SScott Long }
133852c9ce25SScott Long 
133952c9ce25SScott Long static void
aprobecleanup(struct cam_periph * periph)13400f280cbdSWarner Losh aprobecleanup(struct cam_periph *periph)
134152c9ce25SScott Long {
134252c9ce25SScott Long 	free(periph->softc, M_CAMXPT);
134352c9ce25SScott Long }
134452c9ce25SScott Long 
134552c9ce25SScott Long static void
ata_find_quirk(struct cam_ed * device)134630a4094fSAlexander Motin ata_find_quirk(struct cam_ed *device)
134752c9ce25SScott Long {
134830a4094fSAlexander Motin 	struct ata_quirk_entry *quirk;
134952c9ce25SScott Long 	caddr_t	match;
135052c9ce25SScott Long 
135130a4094fSAlexander Motin 	match = cam_quirkmatch((caddr_t)&device->ident_data,
135230a4094fSAlexander Motin 			       (caddr_t)ata_quirk_table,
13538dfea464SPedro F. Giffuni 			       nitems(ata_quirk_table),
135430a4094fSAlexander Motin 			       sizeof(*ata_quirk_table), ata_identify_match);
135552c9ce25SScott Long 
135652c9ce25SScott Long 	if (match == NULL)
135752c9ce25SScott Long 		panic("xpt_find_quirk: device didn't match wildcard entry!!");
135852c9ce25SScott Long 
135930a4094fSAlexander Motin 	quirk = (struct ata_quirk_entry *)match;
136052c9ce25SScott Long 	device->quirk = quirk;
13617dc3213dSAlexander Motin 	if (quirk->quirks & CAM_QUIRK_MAXTAGS) {
13627dc3213dSAlexander Motin 		device->mintags = quirk->mintags;
13637dc3213dSAlexander Motin 		device->maxtags = quirk->maxtags;
13647dc3213dSAlexander Motin 	}
136552c9ce25SScott Long }
136652c9ce25SScott Long 
136752c9ce25SScott Long typedef struct {
136852c9ce25SScott Long 	union	ccb *request_ccb;
136952c9ce25SScott Long 	struct 	ccb_pathinq *cpi;
137052c9ce25SScott Long 	int	counter;
137152c9ce25SScott Long } ata_scan_bus_info;
137252c9ce25SScott Long 
137352c9ce25SScott Long /*
137452c9ce25SScott Long  * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
137552c9ce25SScott Long  * As the scan progresses, xpt_scan_bus is used as the
137652c9ce25SScott Long  * callback on completion function.
137752c9ce25SScott Long  */
137852c9ce25SScott Long static void
ata_scan_bus(struct cam_periph * periph,union ccb * request_ccb)137952c9ce25SScott Long ata_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
138052c9ce25SScott Long {
138152c9ce25SScott Long 	struct	cam_path *path;
138252c9ce25SScott Long 	ata_scan_bus_info *scan_info;
138383c5d981SAlexander Motin 	union	ccb *work_ccb, *reset_ccb;
1384227d67aaSAlexander Motin 	struct mtx *mtx;
138552c9ce25SScott Long 	cam_status status;
138652c9ce25SScott Long 
138752c9ce25SScott Long 	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
138852c9ce25SScott Long 		  ("xpt_scan_bus\n"));
138952c9ce25SScott Long 	switch (request_ccb->ccb_h.func_code) {
139052c9ce25SScott Long 	case XPT_SCAN_BUS:
13910e85f214SMatt Jacob 	case XPT_SCAN_TGT:
139252c9ce25SScott Long 		/* Find out the characteristics of the bus */
139352c9ce25SScott Long 		work_ccb = xpt_alloc_ccb_nowait();
139452c9ce25SScott Long 		if (work_ccb == NULL) {
139552c9ce25SScott Long 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
139652c9ce25SScott Long 			xpt_done(request_ccb);
139752c9ce25SScott Long 			return;
139852c9ce25SScott Long 		}
1399762a7f4fSWarner Losh 		xpt_path_inq(&work_ccb->cpi, request_ccb->ccb_h.path);
140052c9ce25SScott Long 		if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
140152c9ce25SScott Long 			request_ccb->ccb_h.status = work_ccb->ccb_h.status;
140252c9ce25SScott Long 			xpt_free_ccb(work_ccb);
140352c9ce25SScott Long 			xpt_done(request_ccb);
140452c9ce25SScott Long 			return;
140552c9ce25SScott Long 		}
140652c9ce25SScott Long 
140783c5d981SAlexander Motin 		/* We may need to reset bus first, if we haven't done it yet. */
140883c5d981SAlexander Motin 		if ((work_ccb->cpi.hba_inquiry &
140983c5d981SAlexander Motin 		    (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) &&
141083c5d981SAlexander Motin 		    !(work_ccb->cpi.hba_misc & PIM_NOBUSRESET) &&
141183c5d981SAlexander Motin 		    !timevalisset(&request_ccb->ccb_h.path->bus->last_reset)) {
141283c5d981SAlexander Motin 			reset_ccb = xpt_alloc_ccb_nowait();
1413f1893540SAlexander Motin 			if (reset_ccb == NULL) {
1414f1893540SAlexander Motin 				request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1415f1893540SAlexander Motin 				xpt_free_ccb(work_ccb);
1416f1893540SAlexander Motin 				xpt_done(request_ccb);
1417f1893540SAlexander Motin 				return;
1418f1893540SAlexander Motin 			}
141983c5d981SAlexander Motin 			xpt_setup_ccb(&reset_ccb->ccb_h, request_ccb->ccb_h.path,
142083c5d981SAlexander Motin 			      CAM_PRIORITY_NONE);
142183c5d981SAlexander Motin 			reset_ccb->ccb_h.func_code = XPT_RESET_BUS;
142283c5d981SAlexander Motin 			xpt_action(reset_ccb);
142383c5d981SAlexander Motin 			if (reset_ccb->ccb_h.status != CAM_REQ_CMP) {
142483c5d981SAlexander Motin 				request_ccb->ccb_h.status = reset_ccb->ccb_h.status;
142583c5d981SAlexander Motin 				xpt_free_ccb(reset_ccb);
142683c5d981SAlexander Motin 				xpt_free_ccb(work_ccb);
142783c5d981SAlexander Motin 				xpt_done(request_ccb);
142883c5d981SAlexander Motin 				return;
142983c5d981SAlexander Motin 			}
143083c5d981SAlexander Motin 			xpt_free_ccb(reset_ccb);
143183c5d981SAlexander Motin 		}
143283c5d981SAlexander Motin 
143352c9ce25SScott Long 		/* Save some state for use while we probe for devices */
143452c9ce25SScott Long 		scan_info = (ata_scan_bus_info *)
143552c9ce25SScott Long 		    malloc(sizeof(ata_scan_bus_info), M_CAMXPT, M_NOWAIT);
143652c9ce25SScott Long 		if (scan_info == NULL) {
143752c9ce25SScott Long 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1438f1893540SAlexander Motin 			xpt_free_ccb(work_ccb);
143952c9ce25SScott Long 			xpt_done(request_ccb);
144052c9ce25SScott Long 			return;
144152c9ce25SScott Long 		}
144252c9ce25SScott Long 		scan_info->request_ccb = request_ccb;
144352c9ce25SScott Long 		scan_info->cpi = &work_ccb->cpi;
144452c9ce25SScott Long 		/* If PM supported, probe it first. */
144552c9ce25SScott Long 		if (scan_info->cpi->hba_inquiry & PI_SATAPM)
14460025eb12SAlexander Motin 			scan_info->counter = scan_info->cpi->max_target;
14470025eb12SAlexander Motin 		else
14480025eb12SAlexander Motin 			scan_info->counter = 0;
144952c9ce25SScott Long 
145052c9ce25SScott Long 		work_ccb = xpt_alloc_ccb_nowait();
145152c9ce25SScott Long 		if (work_ccb == NULL) {
145252c9ce25SScott Long 			free(scan_info, M_CAMXPT);
145352c9ce25SScott Long 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
145452c9ce25SScott Long 			xpt_done(request_ccb);
145552c9ce25SScott Long 			break;
145652c9ce25SScott Long 		}
1457227d67aaSAlexander Motin 		mtx = xpt_path_mtx(scan_info->request_ccb->ccb_h.path);
145852c9ce25SScott Long 		goto scan_next;
145952c9ce25SScott Long 	case XPT_SCAN_LUN:
146052c9ce25SScott Long 		work_ccb = request_ccb;
146152c9ce25SScott Long 		/* Reuse the same CCB to query if a device was really found */
146252c9ce25SScott Long 		scan_info = (ata_scan_bus_info *)work_ccb->ccb_h.ppriv_ptr0;
1463227d67aaSAlexander Motin 		mtx = xpt_path_mtx(scan_info->request_ccb->ccb_h.path);
1464227d67aaSAlexander Motin 		mtx_lock(mtx);
146565d0fb03SAlexander Motin 		/* If there is PMP... */
14660025eb12SAlexander Motin 		if ((scan_info->cpi->hba_inquiry & PI_SATAPM) &&
14670025eb12SAlexander Motin 		    (scan_info->counter == scan_info->cpi->max_target)) {
1468fd02926aSWarner Losh 			if (cam_ccb_success(work_ccb)) {
146965d0fb03SAlexander Motin 				/* everything else will be probed by it */
1470e2a75189SAlexander Motin 				/* Free the current request path- we're done with it. */
1471e2a75189SAlexander Motin 				xpt_free_path(work_ccb->ccb_h.path);
14720025eb12SAlexander Motin 				goto done;
147352c9ce25SScott Long 			} else {
147452c9ce25SScott Long 				struct ccb_trans_settings cts;
147552c9ce25SScott Long 
147652c9ce25SScott Long 				/* Report SIM that PM is absent. */
147752c9ce25SScott Long 				bzero(&cts, sizeof(cts));
147852c9ce25SScott Long 				xpt_setup_ccb(&cts.ccb_h,
1479e2a75189SAlexander Motin 				    work_ccb->ccb_h.path, CAM_PRIORITY_NONE);
148052c9ce25SScott Long 				cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
148152c9ce25SScott Long 				cts.type = CTS_TYPE_CURRENT_SETTINGS;
14823ccda2f3SAlexander Motin 				cts.xport_specific.sata.pm_present = 0;
148352c9ce25SScott Long 				cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
148452c9ce25SScott Long 				xpt_action((union ccb *)&cts);
148552c9ce25SScott Long 			}
148652c9ce25SScott Long 		}
1487e2a75189SAlexander Motin 		/* Free the current request path- we're done with it. */
1488e2a75189SAlexander Motin 		xpt_free_path(work_ccb->ccb_h.path);
14890025eb12SAlexander Motin 		if (scan_info->counter ==
14900025eb12SAlexander Motin 		    ((scan_info->cpi->hba_inquiry & PI_SATAPM) ?
14910025eb12SAlexander Motin 		    0 : scan_info->cpi->max_target)) {
14920025eb12SAlexander Motin done:
1493227d67aaSAlexander Motin 			mtx_unlock(mtx);
149452c9ce25SScott Long 			xpt_free_ccb(work_ccb);
149552c9ce25SScott Long 			xpt_free_ccb((union ccb *)scan_info->cpi);
149652c9ce25SScott Long 			request_ccb = scan_info->request_ccb;
149752c9ce25SScott Long 			free(scan_info, M_CAMXPT);
149852c9ce25SScott Long 			request_ccb->ccb_h.status = CAM_REQ_CMP;
149952c9ce25SScott Long 			xpt_done(request_ccb);
150052c9ce25SScott Long 			break;
150152c9ce25SScott Long 		}
15020025eb12SAlexander Motin 		/* Take next device. Wrap from max (PMP) to 0. */
15030025eb12SAlexander Motin 		scan_info->counter = (scan_info->counter + 1 ) %
15040025eb12SAlexander Motin 		    (scan_info->cpi->max_target + 1);
150552c9ce25SScott Long scan_next:
1506e5dfa058SAlexander Motin 		status = xpt_create_path(&path, NULL,
150752c9ce25SScott Long 		    scan_info->request_ccb->ccb_h.path_id,
150852c9ce25SScott Long 		    scan_info->counter, 0);
150952c9ce25SScott Long 		if (status != CAM_REQ_CMP) {
1510227d67aaSAlexander Motin 			if (request_ccb->ccb_h.func_code == XPT_SCAN_LUN)
1511227d67aaSAlexander Motin 				mtx_unlock(mtx);
151252c9ce25SScott Long 			printf("xpt_scan_bus: xpt_create_path failed"
151352c9ce25SScott Long 			    " with status %#x, bus scan halted\n",
151452c9ce25SScott Long 			    status);
151552c9ce25SScott Long 			xpt_free_ccb(work_ccb);
151652c9ce25SScott Long 			xpt_free_ccb((union ccb *)scan_info->cpi);
151752c9ce25SScott Long 			request_ccb = scan_info->request_ccb;
151852c9ce25SScott Long 			free(scan_info, M_CAMXPT);
151952c9ce25SScott Long 			request_ccb->ccb_h.status = status;
152052c9ce25SScott Long 			xpt_done(request_ccb);
152152c9ce25SScott Long 			break;
152252c9ce25SScott Long 		}
152352c9ce25SScott Long 		xpt_setup_ccb(&work_ccb->ccb_h, path,
152452c9ce25SScott Long 		    scan_info->request_ccb->ccb_h.pinfo.priority);
152552c9ce25SScott Long 		work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
152652c9ce25SScott Long 		work_ccb->ccb_h.cbfcnp = ata_scan_bus;
1527227d67aaSAlexander Motin 		work_ccb->ccb_h.flags |= CAM_UNLOCKED;
152852c9ce25SScott Long 		work_ccb->ccb_h.ppriv_ptr0 = scan_info;
152952c9ce25SScott Long 		work_ccb->crcn.flags = scan_info->request_ccb->crcn.flags;
1530227d67aaSAlexander Motin 		mtx_unlock(mtx);
1531227d67aaSAlexander Motin 		if (request_ccb->ccb_h.func_code == XPT_SCAN_LUN)
1532227d67aaSAlexander Motin 			mtx = NULL;
153352c9ce25SScott Long 		xpt_action(work_ccb);
1534227d67aaSAlexander Motin 		if (mtx != NULL)
1535227d67aaSAlexander Motin 			mtx_lock(mtx);
153652c9ce25SScott Long 		break;
153752c9ce25SScott Long 	default:
153852c9ce25SScott Long 		break;
153952c9ce25SScott Long 	}
154052c9ce25SScott Long }
154152c9ce25SScott Long 
154252c9ce25SScott Long static void
ata_scan_lun(struct cam_periph * periph,struct cam_path * path,cam_flags flags,union ccb * request_ccb)154352c9ce25SScott Long ata_scan_lun(struct cam_periph *periph, struct cam_path *path,
154452c9ce25SScott Long 	     cam_flags flags, union ccb *request_ccb)
154552c9ce25SScott Long {
154652c9ce25SScott Long 	struct ccb_pathinq cpi;
154752c9ce25SScott Long 	cam_status status;
154852c9ce25SScott Long 	struct cam_path *new_path;
154952c9ce25SScott Long 	struct cam_periph *old_periph;
1550227d67aaSAlexander Motin 	int lock;
155152c9ce25SScott Long 
155283c5d981SAlexander Motin 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_scan_lun\n"));
155352c9ce25SScott Long 
1554762a7f4fSWarner Losh 	xpt_path_inq(&cpi, path);
155552c9ce25SScott Long 	if (cpi.ccb_h.status != CAM_REQ_CMP) {
155652c9ce25SScott Long 		if (request_ccb != NULL) {
155752c9ce25SScott Long 			request_ccb->ccb_h.status = cpi.ccb_h.status;
155852c9ce25SScott Long 			xpt_done(request_ccb);
155952c9ce25SScott Long 		}
156052c9ce25SScott Long 		return;
156152c9ce25SScott Long 	}
156252c9ce25SScott Long 
156352c9ce25SScott Long 	if (request_ccb == NULL) {
156432aa80a6SAlexander Motin 		request_ccb = xpt_alloc_ccb_nowait();
156552c9ce25SScott Long 		if (request_ccb == NULL) {
156652c9ce25SScott Long 			xpt_print(path, "xpt_scan_lun: can't allocate CCB, "
156752c9ce25SScott Long 			    "can't continue\n");
156852c9ce25SScott Long 			return;
156952c9ce25SScott Long 		}
1570e5dfa058SAlexander Motin 		status = xpt_create_path(&new_path, NULL,
157152c9ce25SScott Long 					  path->bus->path_id,
157252c9ce25SScott Long 					  path->target->target_id,
157352c9ce25SScott Long 					  path->device->lun_id);
157452c9ce25SScott Long 		if (status != CAM_REQ_CMP) {
157532aa80a6SAlexander Motin 			xpt_print(path, "xpt_scan_lun: can't create path, "
157652c9ce25SScott Long 			    "can't continue\n");
157732aa80a6SAlexander Motin 			xpt_free_ccb(request_ccb);
157852c9ce25SScott Long 			return;
157952c9ce25SScott Long 		}
158083c5d981SAlexander Motin 		xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_XPT);
15810f280cbdSWarner Losh 		request_ccb->ccb_h.cbfcnp = axptscandone;
1582227d67aaSAlexander Motin 		request_ccb->ccb_h.flags |= CAM_UNLOCKED;
158352c9ce25SScott Long 		request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
158452c9ce25SScott Long 		request_ccb->crcn.flags = flags;
158552c9ce25SScott Long 	}
158652c9ce25SScott Long 
1587227d67aaSAlexander Motin 	lock = (xpt_path_owned(path) == 0);
1588227d67aaSAlexander Motin 	if (lock)
1589227d67aaSAlexander Motin 		xpt_path_lock(path);
1590f09f8e3eSAlexander Motin 	if ((old_periph = cam_periph_find(path, "aprobe")) != NULL) {
15912e3f592bSAlexander Motin 		if ((old_periph->flags & CAM_PERIPH_INVALID) == 0) {
15924762aa57SWarner Losh 			aprobe_softc *softc;
159352c9ce25SScott Long 
15944762aa57SWarner Losh 			softc = (aprobe_softc *)old_periph->softc;
15952e3f592bSAlexander Motin 			TAILQ_INSERT_TAIL(&softc->request_ccbs,
15962e3f592bSAlexander Motin 				&request_ccb->ccb_h, periph_links.tqe);
159783c5d981SAlexander Motin 			softc->restart = 1;
159852c9ce25SScott Long 		} else {
15992e3f592bSAlexander Motin 			request_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
16002e3f592bSAlexander Motin 			xpt_done(request_ccb);
16012e3f592bSAlexander Motin 		}
16022e3f592bSAlexander Motin 	} else {
16030f280cbdSWarner Losh 		status = cam_periph_alloc(aproberegister, NULL, aprobecleanup,
16040f280cbdSWarner Losh 					  aprobestart, "aprobe",
160552c9ce25SScott Long 					  CAM_PERIPH_BIO,
160652c9ce25SScott Long 					  request_ccb->ccb_h.path, NULL, 0,
160752c9ce25SScott Long 					  request_ccb);
160852c9ce25SScott Long 
160952c9ce25SScott Long 		if (status != CAM_REQ_CMP) {
161052c9ce25SScott Long 			xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
161152c9ce25SScott Long 			    "returned an error, can't continue probe\n");
161252c9ce25SScott Long 			request_ccb->ccb_h.status = status;
161352c9ce25SScott Long 			xpt_done(request_ccb);
161452c9ce25SScott Long 		}
161552c9ce25SScott Long 	}
1616227d67aaSAlexander Motin 	if (lock)
1617227d67aaSAlexander Motin 		xpt_path_unlock(path);
161852c9ce25SScott Long }
161952c9ce25SScott Long 
162052c9ce25SScott Long static void
axptscandone(struct cam_periph * periph,union ccb * done_ccb)16210f280cbdSWarner Losh axptscandone(struct cam_periph *periph, union ccb *done_ccb)
162252c9ce25SScott Long {
162332aa80a6SAlexander Motin 
162432aa80a6SAlexander Motin 	xpt_free_path(done_ccb->ccb_h.path);
162532aa80a6SAlexander Motin 	xpt_free_ccb(done_ccb);
162652c9ce25SScott Long }
162752c9ce25SScott Long 
162852c9ce25SScott Long static struct cam_ed *
ata_alloc_device(struct cam_eb * bus,struct cam_et * target,lun_id_t lun_id)162952c9ce25SScott Long ata_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
163052c9ce25SScott Long {
163130a4094fSAlexander Motin 	struct ata_quirk_entry *quirk;
163252c9ce25SScott Long 	struct cam_ed *device;
163352c9ce25SScott Long 
163452c9ce25SScott Long 	device = xpt_alloc_device(bus, target, lun_id);
163552c9ce25SScott Long 	if (device == NULL)
163652c9ce25SScott Long 		return (NULL);
163752c9ce25SScott Long 
163852c9ce25SScott Long 	/*
163952c9ce25SScott Long 	 * Take the default quirk entry until we have inquiry
164052c9ce25SScott Long 	 * data and can determine a better quirk to use.
164152c9ce25SScott Long 	 */
16428dfea464SPedro F. Giffuni 	quirk = &ata_quirk_table[nitems(ata_quirk_table) - 1];
164352c9ce25SScott Long 	device->quirk = (void *)quirk;
164430a4094fSAlexander Motin 	device->mintags = 0;
164530a4094fSAlexander Motin 	device->maxtags = 0;
164652c9ce25SScott Long 	bzero(&device->inq_data, sizeof(device->inq_data));
164752c9ce25SScott Long 	device->inq_flags = 0;
164852c9ce25SScott Long 	device->queue_flags = 0;
164952c9ce25SScott Long 	device->serial_num = NULL;
165052c9ce25SScott Long 	device->serial_num_len = 0;
165152c9ce25SScott Long 	return (device);
165252c9ce25SScott Long }
165352c9ce25SScott Long 
165452c9ce25SScott Long static void
ata_device_transport(struct cam_path * path)165552c9ce25SScott Long ata_device_transport(struct cam_path *path)
165652c9ce25SScott Long {
165752c9ce25SScott Long 	struct ccb_pathinq cpi;
16584b997c49SAlexander Motin 	struct ccb_trans_settings cts;
16594b997c49SAlexander Motin 	struct scsi_inquiry_data *inq_buf = NULL;
16604b997c49SAlexander Motin 	struct ata_params *ident_buf = NULL;
166152c9ce25SScott Long 
166252c9ce25SScott Long 	/* Get transport information from the SIM */
1663762a7f4fSWarner Losh 	xpt_path_inq(&cpi, path);
166452c9ce25SScott Long 
166552c9ce25SScott Long 	path->device->transport = cpi.transport;
16664b997c49SAlexander Motin 	if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
16674b997c49SAlexander Motin 		inq_buf = &path->device->inq_data;
16684b997c49SAlexander Motin 	if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) != 0)
16694b997c49SAlexander Motin 		ident_buf = &path->device->ident_data;
16704b997c49SAlexander Motin 	if (path->device->protocol == PROTO_ATA) {
16714b997c49SAlexander Motin 		path->device->protocol_version = ident_buf ?
16724b997c49SAlexander Motin 		    ata_version(ident_buf->version_major) : cpi.protocol_version;
16734b997c49SAlexander Motin 	} else if (path->device->protocol == PROTO_SCSI) {
16744b997c49SAlexander Motin 		path->device->protocol_version = inq_buf ?
16754b997c49SAlexander Motin 		    SID_ANSI_REV(inq_buf) : cpi.protocol_version;
167652c9ce25SScott Long 	}
16774b997c49SAlexander Motin 	path->device->transport_version = ident_buf ?
16784b997c49SAlexander Motin 	    ata_version(ident_buf->version_major) : cpi.transport_version;
167952c9ce25SScott Long 
168052c9ce25SScott Long 	/* Tell the controller what we think */
1681ec5325dbSEdward Tomasz Napierala 	bzero(&cts, sizeof(cts));
168283c5d981SAlexander Motin 	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
168352c9ce25SScott Long 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
168452c9ce25SScott Long 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
168552c9ce25SScott Long 	cts.transport = path->device->transport;
168652c9ce25SScott Long 	cts.transport_version = path->device->transport_version;
168752c9ce25SScott Long 	cts.protocol = path->device->protocol;
168852c9ce25SScott Long 	cts.protocol_version = path->device->protocol_version;
168952c9ce25SScott Long 	cts.proto_specific.valid = 0;
16904cca1530SAlexander Motin 	if (ident_buf) {
16914cca1530SAlexander Motin 		if (path->device->transport == XPORT_ATA) {
16924cca1530SAlexander Motin 			cts.xport_specific.ata.atapi =
16938e6cab54SAlexander Motin 			    (ident_buf->config == ATA_PROTO_CFA) ? 0 :
16944cca1530SAlexander Motin 			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 :
16954cca1530SAlexander Motin 			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0;
16964cca1530SAlexander Motin 			cts.xport_specific.ata.valid = CTS_ATA_VALID_ATAPI;
16974cca1530SAlexander Motin 		} else {
16984cca1530SAlexander Motin 			cts.xport_specific.sata.atapi =
16998e6cab54SAlexander Motin 			    (ident_buf->config == ATA_PROTO_CFA) ? 0 :
17004cca1530SAlexander Motin 			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 :
17014cca1530SAlexander Motin 			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0;
17024cca1530SAlexander Motin 			cts.xport_specific.sata.valid = CTS_SATA_VALID_ATAPI;
17034cca1530SAlexander Motin 		}
17044cca1530SAlexander Motin 	} else
170552c9ce25SScott Long 		cts.xport_specific.valid = 0;
170652c9ce25SScott Long 	xpt_action((union ccb *)&cts);
170752c9ce25SScott Long }
170852c9ce25SScott Long 
170952c9ce25SScott Long static void
ata_dev_advinfo(union ccb * start_ccb)171014f900e2SWill Andrews ata_dev_advinfo(union ccb *start_ccb)
171114f900e2SWill Andrews {
171214f900e2SWill Andrews 	struct cam_ed *device;
171314f900e2SWill Andrews 	struct ccb_dev_advinfo *cdai;
171414f900e2SWill Andrews 	off_t amt;
171514f900e2SWill Andrews 
17166a216c0bSAlexander Motin 	xpt_path_assert(start_ccb->ccb_h.path, MA_OWNED);
171714f900e2SWill Andrews 	start_ccb->ccb_h.status = CAM_REQ_INVALID;
171814f900e2SWill Andrews 	device = start_ccb->ccb_h.path->device;
171914f900e2SWill Andrews 	cdai = &start_ccb->cdai;
172014f900e2SWill Andrews 	switch(cdai->buftype) {
17213089bb2eSAlexander Motin 	case CDAI_TYPE_SCSI_DEVID:
17223089bb2eSAlexander Motin 		if (cdai->flags & CDAI_FLAG_STORE)
17233089bb2eSAlexander Motin 			return;
17243089bb2eSAlexander Motin 		cdai->provsiz = device->device_id_len;
17253089bb2eSAlexander Motin 		if (device->device_id_len == 0)
17263089bb2eSAlexander Motin 			break;
17273089bb2eSAlexander Motin 		amt = device->device_id_len;
17283089bb2eSAlexander Motin 		if (cdai->provsiz > cdai->bufsiz)
17293089bb2eSAlexander Motin 			amt = cdai->bufsiz;
17303089bb2eSAlexander Motin 		memcpy(cdai->buf, device->device_id, amt);
17313089bb2eSAlexander Motin 		break;
173214f900e2SWill Andrews 	case CDAI_TYPE_SERIAL_NUM:
173314f900e2SWill Andrews 		if (cdai->flags & CDAI_FLAG_STORE)
17343089bb2eSAlexander Motin 			return;
173514f900e2SWill Andrews 		cdai->provsiz = device->serial_num_len;
173614f900e2SWill Andrews 		if (device->serial_num_len == 0)
173714f900e2SWill Andrews 			break;
173814f900e2SWill Andrews 		amt = device->serial_num_len;
173914f900e2SWill Andrews 		if (cdai->provsiz > cdai->bufsiz)
174014f900e2SWill Andrews 			amt = cdai->bufsiz;
174114f900e2SWill Andrews 		memcpy(cdai->buf, device->serial_num, amt);
174214f900e2SWill Andrews 		break;
17433089bb2eSAlexander Motin 	case CDAI_TYPE_PHYS_PATH:
17443089bb2eSAlexander Motin 		if (cdai->flags & CDAI_FLAG_STORE) {
1745431ddd94SYoung Xiao 			if (device->physpath != NULL) {
17463089bb2eSAlexander Motin 				free(device->physpath, M_CAMXPT);
1747431ddd94SYoung Xiao 				device->physpath = NULL;
1748431ddd94SYoung Xiao 				device->physpath_len = 0;
1749431ddd94SYoung Xiao 			}
17503089bb2eSAlexander Motin 			/* Clear existing buffer if zero length */
17513089bb2eSAlexander Motin 			if (cdai->bufsiz == 0)
175214f900e2SWill Andrews 				break;
17533089bb2eSAlexander Motin 			device->physpath = malloc(cdai->bufsiz, M_CAMXPT, M_NOWAIT);
17543089bb2eSAlexander Motin 			if (device->physpath == NULL) {
17553089bb2eSAlexander Motin 				start_ccb->ccb_h.status = CAM_REQ_ABORTED;
17563089bb2eSAlexander Motin 				return;
17573089bb2eSAlexander Motin 			}
1758431ddd94SYoung Xiao 			device->physpath_len = cdai->bufsiz;
17593089bb2eSAlexander Motin 			memcpy(device->physpath, cdai->buf, cdai->bufsiz);
17603089bb2eSAlexander Motin 		} else {
17613089bb2eSAlexander Motin 			cdai->provsiz = device->physpath_len;
17623089bb2eSAlexander Motin 			if (device->physpath_len == 0)
17633089bb2eSAlexander Motin 				break;
17643089bb2eSAlexander Motin 			amt = device->physpath_len;
17653089bb2eSAlexander Motin 			if (cdai->provsiz > cdai->bufsiz)
17663089bb2eSAlexander Motin 				amt = cdai->bufsiz;
17673089bb2eSAlexander Motin 			memcpy(cdai->buf, device->physpath, amt);
17683089bb2eSAlexander Motin 		}
17693089bb2eSAlexander Motin 		break;
17703089bb2eSAlexander Motin 	default:
17713089bb2eSAlexander Motin 		return;
17723089bb2eSAlexander Motin 	}
17733089bb2eSAlexander Motin 	start_ccb->ccb_h.status = CAM_REQ_CMP;
17743089bb2eSAlexander Motin 
17753089bb2eSAlexander Motin 	if (cdai->flags & CDAI_FLAG_STORE) {
17763089bb2eSAlexander Motin 		xpt_async(AC_ADVINFO_CHANGED, start_ccb->ccb_h.path,
17773089bb2eSAlexander Motin 			  (void *)(uintptr_t)cdai->buftype);
177814f900e2SWill Andrews 	}
177914f900e2SWill Andrews }
178014f900e2SWill Andrews 
178114f900e2SWill Andrews static void
ata_action(union ccb * start_ccb)178252c9ce25SScott Long ata_action(union ccb *start_ccb)
178352c9ce25SScott Long {
178452c9ce25SScott Long 
17853394d423SEdward Tomasz Napierala 	if (start_ccb->ccb_h.func_code != XPT_ATA_IO) {
17863394d423SEdward Tomasz Napierala 		KASSERT((start_ccb->ccb_h.alloc_flags & CAM_CCB_FROM_UMA) == 0,
17873394d423SEdward Tomasz Napierala 		    ("%s: ccb %p, func_code %#x should not be allocated "
17883394d423SEdward Tomasz Napierala 		    "from UMA zone\n",
17893394d423SEdward Tomasz Napierala 		    __func__, start_ccb, start_ccb->ccb_h.func_code));
17903394d423SEdward Tomasz Napierala 	}
17913394d423SEdward Tomasz Napierala 
179252c9ce25SScott Long 	switch (start_ccb->ccb_h.func_code) {
179352c9ce25SScott Long 	case XPT_SET_TRAN_SETTINGS:
179452c9ce25SScott Long 	{
179530a4094fSAlexander Motin 		ata_set_transfer_settings(&start_ccb->cts,
1796227d67aaSAlexander Motin 					   start_ccb->ccb_h.path,
179752c9ce25SScott Long 					   /*async_update*/FALSE);
179852c9ce25SScott Long 		break;
179952c9ce25SScott Long 	}
180052c9ce25SScott Long 	case XPT_SCAN_BUS:
18010e85f214SMatt Jacob 	case XPT_SCAN_TGT:
180252c9ce25SScott Long 		ata_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
180352c9ce25SScott Long 		break;
180452c9ce25SScott Long 	case XPT_SCAN_LUN:
180552c9ce25SScott Long 		ata_scan_lun(start_ccb->ccb_h.path->periph,
180652c9ce25SScott Long 			      start_ccb->ccb_h.path, start_ccb->crcn.flags,
180752c9ce25SScott Long 			      start_ccb);
180852c9ce25SScott Long 		break;
180952c9ce25SScott Long 	case XPT_GET_TRAN_SETTINGS:
181052c9ce25SScott Long 	{
1811b9c473b2SAlexander Motin 		ata_get_transfer_settings(&start_ccb->cts);
181252c9ce25SScott Long 		break;
181352c9ce25SScott Long 	}
18144cca1530SAlexander Motin 	case XPT_SCSI_IO:
18154cca1530SAlexander Motin 	{
18164cca1530SAlexander Motin 		struct cam_ed *device;
18174cca1530SAlexander Motin 		u_int	maxlen = 0;
18184cca1530SAlexander Motin 
18194cca1530SAlexander Motin 		device = start_ccb->ccb_h.path->device;
18204cca1530SAlexander Motin 		if (device->protocol == PROTO_SCSI &&
18214cca1530SAlexander Motin 		    (device->flags & CAM_DEV_IDENTIFY_DATA_VALID)) {
18224cca1530SAlexander Motin 			uint16_t p =
18234cca1530SAlexander Motin 			    device->ident_data.config & ATA_PROTO_MASK;
18244cca1530SAlexander Motin 
18258e6cab54SAlexander Motin 			maxlen =
18268e6cab54SAlexander Motin 			    (device->ident_data.config == ATA_PROTO_CFA) ? 0 :
18278e6cab54SAlexander Motin 			    (p == ATA_PROTO_ATAPI_16) ? 16 :
18284cca1530SAlexander Motin 			    (p == ATA_PROTO_ATAPI_12) ? 12 : 0;
18294cca1530SAlexander Motin 		}
18304cca1530SAlexander Motin 		if (start_ccb->csio.cdb_len > maxlen) {
18314cca1530SAlexander Motin 			start_ccb->ccb_h.status = CAM_REQ_INVALID;
18324cca1530SAlexander Motin 			xpt_done(start_ccb);
18334cca1530SAlexander Motin 			break;
18344cca1530SAlexander Motin 		}
1835ee2b236bSAlexander Motin 		xpt_action_default(start_ccb);
1836ee2b236bSAlexander Motin 		break;
18374cca1530SAlexander Motin 	}
183814f900e2SWill Andrews 	case XPT_DEV_ADVINFO:
183914f900e2SWill Andrews 	{
184014f900e2SWill Andrews 		ata_dev_advinfo(start_ccb);
184114f900e2SWill Andrews 		break;
184214f900e2SWill Andrews 	}
184352c9ce25SScott Long 	default:
184452c9ce25SScott Long 		xpt_action_default(start_ccb);
184552c9ce25SScott Long 		break;
184652c9ce25SScott Long 	}
184752c9ce25SScott Long }
184852c9ce25SScott Long 
184952c9ce25SScott Long static void
ata_get_transfer_settings(struct ccb_trans_settings * cts)1850b9c473b2SAlexander Motin ata_get_transfer_settings(struct ccb_trans_settings *cts)
1851b9c473b2SAlexander Motin {
1852b9c473b2SAlexander Motin 	struct	ccb_trans_settings_ata *ata;
1853b9c473b2SAlexander Motin 	struct	ccb_trans_settings_scsi *scsi;
1854b9c473b2SAlexander Motin 	struct	cam_ed *device;
1855b9c473b2SAlexander Motin 
1856b9c473b2SAlexander Motin 	device = cts->ccb_h.path->device;
1857227d67aaSAlexander Motin 	xpt_action_default((union ccb *)cts);
1858b9c473b2SAlexander Motin 
1859bc1bf6e8SAlexander Motin 	if (cts->protocol == PROTO_UNKNOWN ||
1860bc1bf6e8SAlexander Motin 	    cts->protocol == PROTO_UNSPECIFIED) {
1861bc1bf6e8SAlexander Motin 		cts->protocol = device->protocol;
1862bc1bf6e8SAlexander Motin 		cts->protocol_version = device->protocol_version;
1863bc1bf6e8SAlexander Motin 	}
1864bc1bf6e8SAlexander Motin 
1865b9c473b2SAlexander Motin 	if (cts->protocol == PROTO_ATA) {
1866b9c473b2SAlexander Motin 		ata = &cts->proto_specific.ata;
1867b9c473b2SAlexander Motin 		if ((ata->valid & CTS_ATA_VALID_TQ) == 0) {
1868b9c473b2SAlexander Motin 			ata->valid |= CTS_ATA_VALID_TQ;
1869b9c473b2SAlexander Motin 			if (cts->type == CTS_TYPE_USER_SETTINGS ||
1870b9c473b2SAlexander Motin 			    (device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0 ||
1871b9c473b2SAlexander Motin 			    (device->inq_flags & SID_CmdQue) != 0)
1872b9c473b2SAlexander Motin 				ata->flags |= CTS_ATA_FLAGS_TAG_ENB;
1873b9c473b2SAlexander Motin 		}
1874b9c473b2SAlexander Motin 	}
1875b9c473b2SAlexander Motin 	if (cts->protocol == PROTO_SCSI) {
1876b9c473b2SAlexander Motin 		scsi = &cts->proto_specific.scsi;
1877b9c473b2SAlexander Motin 		if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
1878b9c473b2SAlexander Motin 			scsi->valid |= CTS_SCSI_VALID_TQ;
1879b9c473b2SAlexander Motin 			if (cts->type == CTS_TYPE_USER_SETTINGS ||
1880b9c473b2SAlexander Motin 			    (device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0 ||
1881b9c473b2SAlexander Motin 			    (device->inq_flags & SID_CmdQue) != 0)
1882b9c473b2SAlexander Motin 				scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
1883b9c473b2SAlexander Motin 		}
1884b9c473b2SAlexander Motin 	}
1885bc1bf6e8SAlexander Motin 
1886bc1bf6e8SAlexander Motin 	if (cts->transport == XPORT_UNKNOWN ||
1887bc1bf6e8SAlexander Motin 	    cts->transport == XPORT_UNSPECIFIED) {
1888bc1bf6e8SAlexander Motin 		cts->transport = device->transport;
1889bc1bf6e8SAlexander Motin 		cts->transport_version = device->transport_version;
1890bc1bf6e8SAlexander Motin 	}
1891b9c473b2SAlexander Motin }
1892b9c473b2SAlexander Motin 
1893b9c473b2SAlexander Motin static void
ata_set_transfer_settings(struct ccb_trans_settings * cts,struct cam_path * path,int async_update)1894227d67aaSAlexander Motin ata_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_path *path,
189552c9ce25SScott Long 			   int async_update)
189652c9ce25SScott Long {
189752c9ce25SScott Long 	struct	ccb_pathinq cpi;
1898b9c473b2SAlexander Motin 	struct	ccb_trans_settings_ata *ata;
189952c9ce25SScott Long 	struct	ccb_trans_settings_scsi *scsi;
1900b9c473b2SAlexander Motin 	struct	ata_params *ident_data;
190152c9ce25SScott Long 	struct	scsi_inquiry_data *inq_data;
1902227d67aaSAlexander Motin 	struct	cam_ed *device;
190352c9ce25SScott Long 
1904227d67aaSAlexander Motin 	if (path == NULL || (device = path->device) == NULL) {
190552c9ce25SScott Long 		cts->ccb_h.status = CAM_PATH_INVALID;
190652c9ce25SScott Long 		xpt_done((union ccb *)cts);
190752c9ce25SScott Long 		return;
190852c9ce25SScott Long 	}
190952c9ce25SScott Long 
191052c9ce25SScott Long 	if (cts->protocol == PROTO_UNKNOWN
191152c9ce25SScott Long 	 || cts->protocol == PROTO_UNSPECIFIED) {
191252c9ce25SScott Long 		cts->protocol = device->protocol;
191352c9ce25SScott Long 		cts->protocol_version = device->protocol_version;
191452c9ce25SScott Long 	}
191552c9ce25SScott Long 
191652c9ce25SScott Long 	if (cts->protocol_version == PROTO_VERSION_UNKNOWN
191752c9ce25SScott Long 	 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
191852c9ce25SScott Long 		cts->protocol_version = device->protocol_version;
191952c9ce25SScott Long 
192052c9ce25SScott Long 	if (cts->protocol != device->protocol) {
1921227d67aaSAlexander Motin 		xpt_print(path, "Uninitialized Protocol %x:%x?\n",
192252c9ce25SScott Long 		       cts->protocol, device->protocol);
192352c9ce25SScott Long 		cts->protocol = device->protocol;
192452c9ce25SScott Long 	}
192552c9ce25SScott Long 
192652c9ce25SScott Long 	if (cts->protocol_version > device->protocol_version) {
192752c9ce25SScott Long 		if (bootverbose) {
1928227d67aaSAlexander Motin 			xpt_print(path, "Down reving Protocol "
192952c9ce25SScott Long 			    "Version from %d to %d?\n", cts->protocol_version,
193052c9ce25SScott Long 			    device->protocol_version);
193152c9ce25SScott Long 		}
193252c9ce25SScott Long 		cts->protocol_version = device->protocol_version;
193352c9ce25SScott Long 	}
193452c9ce25SScott Long 
193552c9ce25SScott Long 	if (cts->transport == XPORT_UNKNOWN
193652c9ce25SScott Long 	 || cts->transport == XPORT_UNSPECIFIED) {
193752c9ce25SScott Long 		cts->transport = device->transport;
193852c9ce25SScott Long 		cts->transport_version = device->transport_version;
193952c9ce25SScott Long 	}
194052c9ce25SScott Long 
194152c9ce25SScott Long 	if (cts->transport_version == XPORT_VERSION_UNKNOWN
194252c9ce25SScott Long 	 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
194352c9ce25SScott Long 		cts->transport_version = device->transport_version;
194452c9ce25SScott Long 
194552c9ce25SScott Long 	if (cts->transport != device->transport) {
1946227d67aaSAlexander Motin 		xpt_print(path, "Uninitialized Transport %x:%x?\n",
194752c9ce25SScott Long 		    cts->transport, device->transport);
194852c9ce25SScott Long 		cts->transport = device->transport;
194952c9ce25SScott Long 	}
195052c9ce25SScott Long 
195152c9ce25SScott Long 	if (cts->transport_version > device->transport_version) {
195252c9ce25SScott Long 		if (bootverbose) {
1953227d67aaSAlexander Motin 			xpt_print(path, "Down reving Transport "
195452c9ce25SScott Long 			    "Version from %d to %d?\n", cts->transport_version,
195552c9ce25SScott Long 			    device->transport_version);
195652c9ce25SScott Long 		}
195752c9ce25SScott Long 		cts->transport_version = device->transport_version;
195852c9ce25SScott Long 	}
195952c9ce25SScott Long 
1960b9c473b2SAlexander Motin 	ident_data = &device->ident_data;
196152c9ce25SScott Long 	inq_data = &device->inq_data;
1962b9c473b2SAlexander Motin 	if (cts->protocol == PROTO_ATA)
1963b9c473b2SAlexander Motin 		ata = &cts->proto_specific.ata;
1964b9c473b2SAlexander Motin 	else
1965b9c473b2SAlexander Motin 		ata = NULL;
1966b9c473b2SAlexander Motin 	if (cts->protocol == PROTO_SCSI)
196752c9ce25SScott Long 		scsi = &cts->proto_specific.scsi;
1968b9c473b2SAlexander Motin 	else
1969b9c473b2SAlexander Motin 		scsi = NULL;
1970762a7f4fSWarner Losh 	xpt_path_inq(&cpi, path);
197152c9ce25SScott Long 
1972b9c473b2SAlexander Motin 	/* Sanity checking */
197352c9ce25SScott Long 	if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
1974b9c473b2SAlexander Motin 	 || (ata && (ident_data->satacapabilities & ATA_SUPPORT_NCQ) == 0)
1975b9c473b2SAlexander Motin 	 || (scsi && (INQ_DATA_TQ_ENABLED(inq_data)) == 0)
197652c9ce25SScott Long 	 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
197752c9ce25SScott Long 	 || (device->mintags == 0)) {
197852c9ce25SScott Long 		/*
197952c9ce25SScott Long 		 * Can't tag on hardware that doesn't support tags,
198052c9ce25SScott Long 		 * doesn't have it enabled, or has broken tag support.
198152c9ce25SScott Long 		 */
1982b9c473b2SAlexander Motin 		if (ata)
1983b9c473b2SAlexander Motin 			ata->flags &= ~CTS_ATA_FLAGS_TAG_ENB;
1984b9c473b2SAlexander Motin 		if (scsi)
198552c9ce25SScott Long 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
198652c9ce25SScott Long 	}
198752c9ce25SScott Long 
1988b9c473b2SAlexander Motin 	/* Start/stop tags use. */
1989b9c473b2SAlexander Motin 	if (cts->type == CTS_TYPE_CURRENT_SETTINGS &&
1990b9c473b2SAlexander Motin 	    ((ata && (ata->valid & CTS_ATA_VALID_TQ) != 0) ||
1991b9c473b2SAlexander Motin 	     (scsi && (scsi->valid & CTS_SCSI_VALID_TQ) != 0))) {
1992b9c473b2SAlexander Motin 		int nowt, newt = 0;
199352c9ce25SScott Long 
1994b9c473b2SAlexander Motin 		nowt = ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0 ||
1995b9c473b2SAlexander Motin 			(device->inq_flags & SID_CmdQue) != 0);
1996b9c473b2SAlexander Motin 		if (ata)
1997b9c473b2SAlexander Motin 			newt = (ata->flags & CTS_ATA_FLAGS_TAG_ENB) != 0;
1998b9c473b2SAlexander Motin 		if (scsi)
1999b9c473b2SAlexander Motin 			newt = (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0;
200052c9ce25SScott Long 
2001b9c473b2SAlexander Motin 		if (newt && !nowt) {
200252c9ce25SScott Long 			/*
200352c9ce25SScott Long 			 * Delay change to use tags until after a
200452c9ce25SScott Long 			 * few commands have gone to this device so
200552c9ce25SScott Long 			 * the controller has time to perform transfer
200652c9ce25SScott Long 			 * negotiations without tagged messages getting
200752c9ce25SScott Long 			 * in the way.
200852c9ce25SScott Long 			 */
200952c9ce25SScott Long 			device->tag_delay_count = CAM_TAG_DELAY_COUNT;
201052c9ce25SScott Long 			device->flags |= CAM_DEV_TAG_AFTER_COUNT;
2011b9c473b2SAlexander Motin 		} else if (nowt && !newt)
2012227d67aaSAlexander Motin 			xpt_stop_tags(path);
201352c9ce25SScott Long 	}
2014b9c473b2SAlexander Motin 
201552c9ce25SScott Long 	if (async_update == FALSE)
2016227d67aaSAlexander Motin 		xpt_action_default((union ccb *)cts);
201752c9ce25SScott Long }
201852c9ce25SScott Long 
201952c9ce25SScott Long /*
202052c9ce25SScott Long  * Handle any per-device event notifications that require action by the XPT.
202152c9ce25SScott Long  */
202252c9ce25SScott Long static void
ata_dev_async(uint32_t async_code,struct cam_eb * bus,struct cam_et * target,struct cam_ed * device,void * async_arg)20239db2db6bSWarner Losh ata_dev_async(uint32_t async_code, struct cam_eb *bus, struct cam_et *target,
202452c9ce25SScott Long 	      struct cam_ed *device, void *async_arg)
202552c9ce25SScott Long {
202652c9ce25SScott Long 	/*
202752c9ce25SScott Long 	 * We only need to handle events for real devices.
202852c9ce25SScott Long 	 */
20299d899bbcSWarner Losh 	if (target->target_id == CAM_TARGET_WILDCARD ||
20309d899bbcSWarner Losh 	    device->lun_id == CAM_LUN_WILDCARD)
203152c9ce25SScott Long 		return;
203252c9ce25SScott Long 
20339d899bbcSWarner Losh 	switch (async_code) {
20349d899bbcSWarner Losh 	case AC_SENT_BDR:
20359d899bbcSWarner Losh 	case AC_BUS_RESET:
20369d899bbcSWarner Losh 	case AC_INQ_CHANGED: {
20379d899bbcSWarner Losh 		cam_status status;
20389d899bbcSWarner Losh 		struct cam_path newpath;
20399d899bbcSWarner Losh 		cam_flags flags;
20409d899bbcSWarner Losh 
204152c9ce25SScott Long 		/*
20429d899bbcSWarner Losh 		 * We need our own path with wildcards expanded to handle these
20439d899bbcSWarner Losh 		 * events.
204452c9ce25SScott Long 		 */
204552c9ce25SScott Long 		status = xpt_compile_path(&newpath, NULL,
204652c9ce25SScott Long 					  bus->path_id,
204752c9ce25SScott Long 					  target->target_id,
204852c9ce25SScott Long 					  device->lun_id);
20499d899bbcSWarner Losh 		if (status != CAM_REQ_CMP)
20509d899bbcSWarner Losh 			break; /* fail safe and just drop it */
205152c9ce25SScott Long 
205252c9ce25SScott Long 		/*
20539d899bbcSWarner Losh 		 * For AC_INQ_CHANGED, we've sent a start unit command, or
20549d899bbcSWarner Losh 		 * something similar to a device that may have caused its
20559d899bbcSWarner Losh 		 * inquiry data to change. So we re-scan the device to refresh
20569d899bbcSWarner Losh 		 * the inquiry data for it, allowing changes. Otherwise we rescan
20579d899bbcSWarner Losh 		 * without allowing changes to respond to the reset, not allowing
20589d899bbcSWarner Losh 		 * changes.
205952c9ce25SScott Long 		 */
20609d899bbcSWarner Losh 		flags = async_code == AC_INQ_CHANGED ? CAM_EXPECT_INQ_CHANGE : 0;
20619d899bbcSWarner Losh 		ata_scan_lun(newpath.periph, &newpath, flags, NULL);
206252c9ce25SScott Long 		xpt_release_path(&newpath);
20639d899bbcSWarner Losh 		break;
20649d899bbcSWarner Losh 	}
20659d899bbcSWarner Losh 	case AC_TRANSFER_NEG: {
206652c9ce25SScott Long 		struct ccb_trans_settings *settings;
2067227d67aaSAlexander Motin 		struct cam_path path;
206852c9ce25SScott Long 
206952c9ce25SScott Long 		settings = (struct ccb_trans_settings *)async_arg;
2070227d67aaSAlexander Motin 		xpt_compile_path(&path, NULL, bus->path_id, target->target_id,
2071227d67aaSAlexander Motin 				 device->lun_id);
2072227d67aaSAlexander Motin 		ata_set_transfer_settings(settings, &path,
207352c9ce25SScott Long 					  /*async_update*/TRUE);
2074227d67aaSAlexander Motin 		xpt_release_path(&path);
20759d899bbcSWarner Losh 		break;
20769d899bbcSWarner Losh 	}
20779d899bbcSWarner Losh 	case AC_LOST_DEVICE:
20789d899bbcSWarner Losh 		if ((device->flags & CAM_DEV_UNCONFIGURED) == 0) {
20799d899bbcSWarner Losh 			device->flags |= CAM_DEV_UNCONFIGURED;
20809d899bbcSWarner Losh 			xpt_release_device(device);
20819d899bbcSWarner Losh 		}
20829d899bbcSWarner Losh 		break;
208352c9ce25SScott Long 	}
208452c9ce25SScott Long }
208552c9ce25SScott Long 
208657079b17SAlexander Motin static void
_ata_announce_periph(struct cam_periph * periph,struct ccb_trans_settings * cts,u_int * speed)20875d01277fSScott Long _ata_announce_periph(struct cam_periph *periph, struct ccb_trans_settings *cts, u_int *speed)
208857079b17SAlexander Motin {
208957079b17SAlexander Motin 	struct	ccb_pathinq cpi;
209057079b17SAlexander Motin 	struct	cam_path *path = periph->path;
209157079b17SAlexander Motin 
2092227d67aaSAlexander Motin 	cam_periph_assert(periph, MA_OWNED);
209357079b17SAlexander Motin 
20945d01277fSScott Long 	xpt_setup_ccb(&cts->ccb_h, path, CAM_PRIORITY_NORMAL);
20955d01277fSScott Long 	cts->ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
20965d01277fSScott Long 	cts->type = CTS_TYPE_CURRENT_SETTINGS;
20975d01277fSScott Long 	xpt_action((union ccb*)cts);
20985d01277fSScott Long 	if ((cts->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
209957079b17SAlexander Motin 		return;
210057079b17SAlexander Motin 	/* Ask the SIM for its base transfer speed */
2101762a7f4fSWarner Losh 	xpt_path_inq(&cpi, path);
210257079b17SAlexander Motin 	/* Report connection speed */
21035d01277fSScott Long 	*speed = cpi.base_transfer_speed;
21045d01277fSScott Long 	if (cts->transport == XPORT_ATA) {
2105b9c473b2SAlexander Motin 		struct	ccb_trans_settings_pata *pata =
21065d01277fSScott Long 		    &cts->xport_specific.ata;
210757079b17SAlexander Motin 
2108b9c473b2SAlexander Motin 		if (pata->valid & CTS_ATA_VALID_MODE)
21095d01277fSScott Long 			*speed = ata_mode2speed(pata->mode);
211057079b17SAlexander Motin 	}
21115d01277fSScott Long 	if (cts->transport == XPORT_SATA) {
211257079b17SAlexander Motin 		struct	ccb_trans_settings_sata *sata =
21135d01277fSScott Long 		    &cts->xport_specific.sata;
211457079b17SAlexander Motin 
211557079b17SAlexander Motin 		if (sata->valid & CTS_SATA_VALID_REVISION)
21165d01277fSScott Long 			*speed = ata_revision2speed(sata->revision);
211757079b17SAlexander Motin 	}
21185d01277fSScott Long }
21195d01277fSScott Long 
21205d01277fSScott Long static void
ata_announce_periph_sbuf(struct cam_periph * periph,struct sbuf * sb)21215d01277fSScott Long ata_announce_periph_sbuf(struct cam_periph *periph, struct sbuf *sb)
21225d01277fSScott Long {
21235d01277fSScott Long 	struct ccb_trans_settings cts;
21245d01277fSScott Long 	u_int speed, mb;
21255d01277fSScott Long 
21260f206cc9SEdward Tomasz Napierala 	bzero(&cts, sizeof(cts));
21275d01277fSScott Long 	_ata_announce_periph(periph, &cts, &speed);
21285d01277fSScott Long 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
21295d01277fSScott Long 		return;
21305d01277fSScott Long 
21315d01277fSScott Long 	mb = speed / 1000;
21325d01277fSScott Long 	if (mb > 0)
21335d01277fSScott Long 		sbuf_printf(sb, "%s%d: %d.%03dMB/s transfers",
21345d01277fSScott Long 		       periph->periph_name, periph->unit_number,
21355d01277fSScott Long 		       mb, speed % 1000);
21365d01277fSScott Long 	else
21375d01277fSScott Long 		sbuf_printf(sb, "%s%d: %dKB/s transfers", periph->periph_name,
21385d01277fSScott Long 		       periph->unit_number, speed);
21395d01277fSScott Long 	/* Report additional information about connection */
21405d01277fSScott Long 	if (cts.transport == XPORT_ATA) {
21415d01277fSScott Long 		struct ccb_trans_settings_pata *pata =
21425d01277fSScott Long 		    &cts.xport_specific.ata;
21435d01277fSScott Long 
2144519b24f0SAlexander Motin 		sbuf_cat(sb, " (");
21455d01277fSScott Long 		if (pata->valid & CTS_ATA_VALID_MODE)
21465d01277fSScott Long 			sbuf_printf(sb, "%s, ", ata_mode2string(pata->mode));
21475d01277fSScott Long 		if ((pata->valid & CTS_ATA_VALID_ATAPI) && pata->atapi != 0)
21485d01277fSScott Long 			sbuf_printf(sb, "ATAPI %dbytes, ", pata->atapi);
21495d01277fSScott Long 		if (pata->valid & CTS_ATA_VALID_BYTECOUNT)
21505d01277fSScott Long 			sbuf_printf(sb, "PIO %dbytes", pata->bytecount);
2151519b24f0SAlexander Motin 		sbuf_putc(sb, ')');
21525d01277fSScott Long 	}
21535d01277fSScott Long 	if (cts.transport == XPORT_SATA) {
21545d01277fSScott Long 		struct ccb_trans_settings_sata *sata =
21555d01277fSScott Long 		    &cts.xport_specific.sata;
21565d01277fSScott Long 
2157519b24f0SAlexander Motin 		sbuf_cat(sb, " (");
21585d01277fSScott Long 		if (sata->valid & CTS_SATA_VALID_REVISION)
21595d01277fSScott Long 			sbuf_printf(sb, "SATA %d.x, ", sata->revision);
21605d01277fSScott Long 		else
2161519b24f0SAlexander Motin 			sbuf_cat(sb, "SATA, ");
21625d01277fSScott Long 		if (sata->valid & CTS_SATA_VALID_MODE)
21635d01277fSScott Long 			sbuf_printf(sb, "%s, ", ata_mode2string(sata->mode));
21645d01277fSScott Long 		if ((sata->valid & CTS_ATA_VALID_ATAPI) && sata->atapi != 0)
21655d01277fSScott Long 			sbuf_printf(sb, "ATAPI %dbytes, ", sata->atapi);
21665d01277fSScott Long 		if (sata->valid & CTS_SATA_VALID_BYTECOUNT)
21675d01277fSScott Long 			sbuf_printf(sb, "PIO %dbytes", sata->bytecount);
2168519b24f0SAlexander Motin 		sbuf_putc(sb, ')');
21695d01277fSScott Long 	}
2170519b24f0SAlexander Motin 	sbuf_putc(sb, '\n');
21715d01277fSScott Long }
21725d01277fSScott Long 
21735d01277fSScott Long static void
ata_proto_announce_sbuf(struct cam_ed * device,struct sbuf * sb)21745d01277fSScott Long ata_proto_announce_sbuf(struct cam_ed *device, struct sbuf *sb)
21755d01277fSScott Long {
21765d01277fSScott Long 	ata_print_ident_sbuf(&device->ident_data, sb);
21775d01277fSScott Long }
21785d01277fSScott Long 
21795d01277fSScott Long static void
ata_proto_denounce_sbuf(struct cam_ed * device,struct sbuf * sb)21805d01277fSScott Long ata_proto_denounce_sbuf(struct cam_ed *device, struct sbuf *sb)
21815d01277fSScott Long {
21825d01277fSScott Long 	ata_print_ident_short_sbuf(&device->ident_data, sb);
21835d01277fSScott Long }
21845d01277fSScott Long 
21855d01277fSScott Long static void
semb_proto_announce_sbuf(struct cam_ed * device,struct sbuf * sb)21865d01277fSScott Long semb_proto_announce_sbuf(struct cam_ed *device, struct sbuf *sb)
21875d01277fSScott Long {
21885d01277fSScott Long 	semb_print_ident_sbuf((struct sep_identify_data *)&device->ident_data, sb);
21895d01277fSScott Long }
21905d01277fSScott Long 
21915d01277fSScott Long static void
semb_proto_denounce_sbuf(struct cam_ed * device,struct sbuf * sb)21925d01277fSScott Long semb_proto_denounce_sbuf(struct cam_ed *device, struct sbuf *sb)
21935d01277fSScott Long {
21945d01277fSScott Long 	semb_print_ident_short_sbuf((struct sep_identify_data *)&device->ident_data, sb);
21955d01277fSScott Long }
21965d01277fSScott Long 
21975d01277fSScott Long static void
ata_proto_debug_out(union ccb * ccb)219808f13879SWarner Losh ata_proto_debug_out(union ccb *ccb)
219908f13879SWarner Losh {
220008f13879SWarner Losh 	char cdb_str[(sizeof(struct ata_cmd) * 3) + 1];
220108f13879SWarner Losh 
220208f13879SWarner Losh 	if (ccb->ccb_h.func_code != XPT_ATA_IO)
220308f13879SWarner Losh 		return;
220408f13879SWarner Losh 
220508f13879SWarner Losh 	CAM_DEBUG(ccb->ccb_h.path,
220608f13879SWarner Losh 	    CAM_DEBUG_CDB,("%s. ACB: %s\n", ata_op_string(&ccb->ataio.cmd),
220708f13879SWarner Losh 		ata_cmd_string(&ccb->ataio.cmd, cdb_str, sizeof(cdb_str))));
220808f13879SWarner Losh }
2209