xref: /freebsd/sys/cam/ata/ata_xpt.c (revision 4cca1530)
152c9ce25SScott Long /*-
252c9ce25SScott Long  * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org>
352c9ce25SScott Long  * All rights reserved.
452c9ce25SScott Long  *
552c9ce25SScott Long  * Redistribution and use in source and binary forms, with or without
652c9ce25SScott Long  * modification, are permitted provided that the following conditions
752c9ce25SScott Long  * are met:
852c9ce25SScott Long  * 1. Redistributions of source code must retain the above copyright
952c9ce25SScott Long  *    notice, this list of conditions and the following disclaimer,
1052c9ce25SScott Long  *    without modification, immediately at the beginning of the file.
1152c9ce25SScott Long  * 2. Redistributions in binary form must reproduce the above copyright
1252c9ce25SScott Long  *    notice, this list of conditions and the following disclaimer in the
1352c9ce25SScott Long  *    documentation and/or other materials provided with the distribution.
1452c9ce25SScott Long  *
1552c9ce25SScott Long  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1652c9ce25SScott Long  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1752c9ce25SScott Long  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1852c9ce25SScott Long  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1952c9ce25SScott Long  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2052c9ce25SScott Long  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2152c9ce25SScott Long  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2252c9ce25SScott Long  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2352c9ce25SScott Long  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2452c9ce25SScott Long  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2552c9ce25SScott Long  */
2652c9ce25SScott Long 
2752c9ce25SScott Long #include <sys/cdefs.h>
2852c9ce25SScott Long __FBSDID("$FreeBSD$");
2952c9ce25SScott Long 
3052c9ce25SScott Long #include <sys/param.h>
3152c9ce25SScott Long #include <sys/bus.h>
3252c9ce25SScott Long #include <sys/endian.h>
3352c9ce25SScott Long #include <sys/systm.h>
3452c9ce25SScott Long #include <sys/types.h>
3552c9ce25SScott Long #include <sys/malloc.h>
3652c9ce25SScott Long #include <sys/kernel.h>
3752c9ce25SScott Long #include <sys/time.h>
3852c9ce25SScott Long #include <sys/conf.h>
3952c9ce25SScott Long #include <sys/fcntl.h>
4052c9ce25SScott Long #include <sys/md5.h>
4152c9ce25SScott Long #include <sys/interrupt.h>
4252c9ce25SScott Long #include <sys/sbuf.h>
4352c9ce25SScott Long 
4452c9ce25SScott Long #include <sys/lock.h>
4552c9ce25SScott Long #include <sys/mutex.h>
4652c9ce25SScott Long #include <sys/sysctl.h>
4752c9ce25SScott Long 
4852c9ce25SScott Long #ifdef PC98
4952c9ce25SScott Long #include <pc98/pc98/pc98_machdep.h>	/* geometry translation */
5052c9ce25SScott Long #endif
5152c9ce25SScott Long 
5252c9ce25SScott Long #include <cam/cam.h>
5352c9ce25SScott Long #include <cam/cam_ccb.h>
5452c9ce25SScott Long #include <cam/cam_queue.h>
5552c9ce25SScott Long #include <cam/cam_periph.h>
5652c9ce25SScott Long #include <cam/cam_sim.h>
5752c9ce25SScott Long #include <cam/cam_xpt.h>
5852c9ce25SScott Long #include <cam/cam_xpt_sim.h>
5952c9ce25SScott Long #include <cam/cam_xpt_periph.h>
6052c9ce25SScott Long #include <cam/cam_xpt_internal.h>
6152c9ce25SScott Long #include <cam/cam_debug.h>
6252c9ce25SScott Long 
6352c9ce25SScott Long #include <cam/scsi/scsi_all.h>
6452c9ce25SScott Long #include <cam/scsi/scsi_message.h>
6552c9ce25SScott Long #include <cam/ata/ata_all.h>
6652c9ce25SScott Long #include <machine/stdarg.h>	/* for xpt_print below */
6752c9ce25SScott Long #include "opt_cam.h"
6852c9ce25SScott Long 
6930a4094fSAlexander Motin struct ata_quirk_entry {
7052c9ce25SScott Long 	struct scsi_inquiry_pattern inq_pat;
7152c9ce25SScott Long 	u_int8_t quirks;
7230a4094fSAlexander Motin #define	CAM_QUIRK_MAXTAGS	0x01
7352c9ce25SScott Long 	u_int maxtags;
7452c9ce25SScott Long };
7552c9ce25SScott Long 
7652c9ce25SScott Long static periph_init_t probe_periph_init;
7752c9ce25SScott Long 
7852c9ce25SScott Long static struct periph_driver probe_driver =
7952c9ce25SScott Long {
80f09f8e3eSAlexander Motin 	probe_periph_init, "aprobe",
811e637ba6SAlexander Motin 	TAILQ_HEAD_INITIALIZER(probe_driver.units), /* generation */ 0,
821e637ba6SAlexander Motin 	CAM_PERIPH_DRV_EARLY
8352c9ce25SScott Long };
8452c9ce25SScott Long 
85f09f8e3eSAlexander Motin PERIPHDRIVER_DECLARE(aprobe, probe_driver);
8652c9ce25SScott Long 
8752c9ce25SScott Long typedef enum {
8852c9ce25SScott Long 	PROBE_RESET,
8952c9ce25SScott Long 	PROBE_IDENTIFY,
9052c9ce25SScott Long 	PROBE_SETMODE,
911e637ba6SAlexander Motin 	PROBE_SET_MULTI,
9252c9ce25SScott Long 	PROBE_INQUIRY,
9352c9ce25SScott Long 	PROBE_FULL_INQUIRY,
9452c9ce25SScott Long 	PROBE_PM_PID,
9552c9ce25SScott Long 	PROBE_PM_PRV,
9652c9ce25SScott Long 	PROBE_INVALID
9752c9ce25SScott Long } probe_action;
9852c9ce25SScott Long 
9952c9ce25SScott Long static char *probe_action_text[] = {
10052c9ce25SScott Long 	"PROBE_RESET",
10152c9ce25SScott Long 	"PROBE_IDENTIFY",
10252c9ce25SScott Long 	"PROBE_SETMODE",
1031e637ba6SAlexander Motin 	"PROBE_SET_MULTI",
10452c9ce25SScott Long 	"PROBE_INQUIRY",
10552c9ce25SScott Long 	"PROBE_FULL_INQUIRY",
10652c9ce25SScott Long 	"PROBE_PM_PID",
10752c9ce25SScott Long 	"PROBE_PM_PRV",
10852c9ce25SScott Long 	"PROBE_INVALID"
10952c9ce25SScott Long };
11052c9ce25SScott Long 
11152c9ce25SScott Long #define PROBE_SET_ACTION(softc, newaction)	\
11252c9ce25SScott Long do {									\
11352c9ce25SScott Long 	char **text;							\
11452c9ce25SScott Long 	text = probe_action_text;					\
11552c9ce25SScott Long 	CAM_DEBUG((softc)->periph->path, CAM_DEBUG_INFO,		\
11652c9ce25SScott Long 	    ("Probe %s to %s\n", text[(softc)->action],			\
11752c9ce25SScott Long 	    text[(newaction)]));					\
11852c9ce25SScott Long 	(softc)->action = (newaction);					\
11952c9ce25SScott Long } while(0)
12052c9ce25SScott Long 
12152c9ce25SScott Long typedef enum {
12252c9ce25SScott Long 	PROBE_NO_ANNOUNCE	= 0x04
12352c9ce25SScott Long } probe_flags;
12452c9ce25SScott Long 
12552c9ce25SScott Long typedef struct {
12652c9ce25SScott Long 	TAILQ_HEAD(, ccb_hdr) request_ccbs;
12752c9ce25SScott Long 	probe_action	action;
12852c9ce25SScott Long 	union ccb	saved_ccb;
12952c9ce25SScott Long 	probe_flags	flags;
13052c9ce25SScott Long 	u_int8_t	digest[16];
13152c9ce25SScott Long 	uint32_t	pm_pid;
13252c9ce25SScott Long 	uint32_t	pm_prv;
13383c5d981SAlexander Motin 	int		restart;
13452c9ce25SScott Long 	struct cam_periph *periph;
13552c9ce25SScott Long } probe_softc;
13652c9ce25SScott Long 
13730a4094fSAlexander Motin static struct ata_quirk_entry ata_quirk_table[] =
13852c9ce25SScott Long {
13952c9ce25SScott Long 	{
14052c9ce25SScott Long 		/* Default tagged queuing parameters for all devices */
14152c9ce25SScott Long 		{
14252c9ce25SScott Long 		  T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
14352c9ce25SScott Long 		  /*vendor*/"*", /*product*/"*", /*revision*/"*"
14452c9ce25SScott Long 		},
14530a4094fSAlexander Motin 		/*quirks*/0, /*maxtags*/0
14652c9ce25SScott Long 	},
14752c9ce25SScott Long };
14852c9ce25SScott Long 
14930a4094fSAlexander Motin static const int ata_quirk_table_size =
15030a4094fSAlexander Motin 	sizeof(ata_quirk_table) / sizeof(*ata_quirk_table);
15152c9ce25SScott Long 
15252c9ce25SScott Long static cam_status	proberegister(struct cam_periph *periph,
15352c9ce25SScott Long 				      void *arg);
15452c9ce25SScott Long static void	 probeschedule(struct cam_periph *probe_periph);
15552c9ce25SScott Long static void	 probestart(struct cam_periph *periph, union ccb *start_ccb);
15652c9ce25SScott Long //static void	 proberequestdefaultnegotiation(struct cam_periph *periph);
15752c9ce25SScott Long //static int       proberequestbackoff(struct cam_periph *periph,
15852c9ce25SScott Long //				     struct cam_ed *device);
15952c9ce25SScott Long static void	 probedone(struct cam_periph *periph, union ccb *done_ccb);
16052c9ce25SScott Long static void	 probecleanup(struct cam_periph *periph);
16130a4094fSAlexander Motin static void	 ata_find_quirk(struct cam_ed *device);
16252c9ce25SScott Long static void	 ata_scan_bus(struct cam_periph *periph, union ccb *ccb);
16352c9ce25SScott Long static void	 ata_scan_lun(struct cam_periph *periph,
16452c9ce25SScott Long 			       struct cam_path *path, cam_flags flags,
16552c9ce25SScott Long 			       union ccb *ccb);
16652c9ce25SScott Long static void	 xptscandone(struct cam_periph *periph, union ccb *done_ccb);
16752c9ce25SScott Long static struct cam_ed *
16852c9ce25SScott Long 		 ata_alloc_device(struct cam_eb *bus, struct cam_et *target,
16952c9ce25SScott Long 				   lun_id_t lun_id);
17052c9ce25SScott Long static void	 ata_device_transport(struct cam_path *path);
17130a4094fSAlexander Motin static void	 ata_set_transfer_settings(struct ccb_trans_settings *cts,
17252c9ce25SScott Long 					    struct cam_ed *device,
17352c9ce25SScott Long 					    int async_update);
17452c9ce25SScott Long static void	 ata_dev_async(u_int32_t async_code,
17552c9ce25SScott Long 				struct cam_eb *bus,
17652c9ce25SScott Long 				struct cam_et *target,
17752c9ce25SScott Long 				struct cam_ed *device,
17852c9ce25SScott Long 				void *async_arg);
17952c9ce25SScott Long static void	 ata_action(union ccb *start_ccb);
18052c9ce25SScott Long 
18152c9ce25SScott Long static struct xpt_xport ata_xport = {
18252c9ce25SScott Long 	.alloc_device = ata_alloc_device,
18352c9ce25SScott Long 	.action = ata_action,
18452c9ce25SScott Long 	.async = ata_dev_async,
18552c9ce25SScott Long };
18652c9ce25SScott Long 
18752c9ce25SScott Long struct xpt_xport *
18852c9ce25SScott Long ata_get_xport(void)
18952c9ce25SScott Long {
19052c9ce25SScott Long 	return (&ata_xport);
19152c9ce25SScott Long }
19252c9ce25SScott Long 
19352c9ce25SScott Long static void
19452c9ce25SScott Long probe_periph_init()
19552c9ce25SScott Long {
19652c9ce25SScott Long }
19752c9ce25SScott Long 
19852c9ce25SScott Long static cam_status
19952c9ce25SScott Long proberegister(struct cam_periph *periph, void *arg)
20052c9ce25SScott Long {
20152c9ce25SScott Long 	union ccb *request_ccb;	/* CCB representing the probe request */
20252c9ce25SScott Long 	cam_status status;
20352c9ce25SScott Long 	probe_softc *softc;
20452c9ce25SScott Long 
20552c9ce25SScott Long 	request_ccb = (union ccb *)arg;
20652c9ce25SScott Long 	if (periph == NULL) {
20752c9ce25SScott Long 		printf("proberegister: periph was NULL!!\n");
20852c9ce25SScott Long 		return(CAM_REQ_CMP_ERR);
20952c9ce25SScott Long 	}
21052c9ce25SScott Long 
21152c9ce25SScott Long 	if (request_ccb == NULL) {
21252c9ce25SScott Long 		printf("proberegister: no probe CCB, "
21352c9ce25SScott Long 		       "can't register device\n");
21452c9ce25SScott Long 		return(CAM_REQ_CMP_ERR);
21552c9ce25SScott Long 	}
21652c9ce25SScott Long 
21752c9ce25SScott Long 	softc = (probe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_NOWAIT);
21852c9ce25SScott Long 
21952c9ce25SScott Long 	if (softc == NULL) {
22052c9ce25SScott Long 		printf("proberegister: Unable to probe new device. "
22152c9ce25SScott Long 		       "Unable to allocate softc\n");
22252c9ce25SScott Long 		return(CAM_REQ_CMP_ERR);
22352c9ce25SScott Long 	}
22452c9ce25SScott Long 	TAILQ_INIT(&softc->request_ccbs);
22552c9ce25SScott Long 	TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
22652c9ce25SScott Long 			  periph_links.tqe);
22752c9ce25SScott Long 	softc->flags = 0;
22852c9ce25SScott Long 	periph->softc = softc;
22952c9ce25SScott Long 	softc->periph = periph;
23052c9ce25SScott Long 	softc->action = PROBE_INVALID;
23152c9ce25SScott Long 	status = cam_periph_acquire(periph);
23252c9ce25SScott Long 	if (status != CAM_REQ_CMP) {
23352c9ce25SScott Long 		return (status);
23452c9ce25SScott Long 	}
23552c9ce25SScott Long 	/*
23683c5d981SAlexander Motin 	 * Ensure nobody slip in until probe finish.
23752c9ce25SScott Long 	 */
23883c5d981SAlexander Motin 	cam_freeze_devq_arg(periph->path,
23983c5d981SAlexander Motin 	    RELSIM_RELEASE_RUNLEVEL, CAM_RL_XPT + 1);
24052c9ce25SScott Long 	probeschedule(periph);
24152c9ce25SScott Long 	return(CAM_REQ_CMP);
24252c9ce25SScott Long }
24352c9ce25SScott Long 
24452c9ce25SScott Long static void
24552c9ce25SScott Long probeschedule(struct cam_periph *periph)
24652c9ce25SScott Long {
24752c9ce25SScott Long 	union ccb *ccb;
24852c9ce25SScott Long 	probe_softc *softc;
24952c9ce25SScott Long 
25052c9ce25SScott Long 	softc = (probe_softc *)periph->softc;
25152c9ce25SScott Long 	ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
25252c9ce25SScott Long 
25365d0fb03SAlexander Motin 	if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) ||
25465d0fb03SAlexander Motin 	    periph->path->device->protocol == PROTO_SATAPM)
25552c9ce25SScott Long 		PROBE_SET_ACTION(softc, PROBE_RESET);
25652c9ce25SScott Long 	else
25752c9ce25SScott Long 		PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
25852c9ce25SScott Long 
25952c9ce25SScott Long 	if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
26052c9ce25SScott Long 		softc->flags |= PROBE_NO_ANNOUNCE;
26152c9ce25SScott Long 	else
26252c9ce25SScott Long 		softc->flags &= ~PROBE_NO_ANNOUNCE;
26352c9ce25SScott Long 
26483c5d981SAlexander Motin 	xpt_schedule(periph, CAM_PRIORITY_XPT);
26552c9ce25SScott Long }
26652c9ce25SScott Long 
26752c9ce25SScott Long static void
26852c9ce25SScott Long probestart(struct cam_periph *periph, union ccb *start_ccb)
26952c9ce25SScott Long {
270c8039fc6SAlexander Motin 	struct ccb_trans_settings cts;
27152c9ce25SScott Long 	struct ccb_ataio *ataio;
27252c9ce25SScott Long 	struct ccb_scsiio *csio;
27352c9ce25SScott Long 	probe_softc *softc;
2741e637ba6SAlexander Motin 	struct cam_path *path;
2751e637ba6SAlexander Motin 	struct ata_params *ident_buf;
27652c9ce25SScott Long 
27752c9ce25SScott Long 	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
27852c9ce25SScott Long 
27952c9ce25SScott Long 	softc = (probe_softc *)periph->softc;
2801e637ba6SAlexander Motin 	path = start_ccb->ccb_h.path;
28152c9ce25SScott Long 	ataio = &start_ccb->ataio;
28252c9ce25SScott Long 	csio = &start_ccb->csio;
2831e637ba6SAlexander Motin 	ident_buf = &periph->path->device->ident_data;
28452c9ce25SScott Long 
28583c5d981SAlexander Motin 	if (softc->restart) {
28683c5d981SAlexander Motin 		softc->restart = 0;
28783c5d981SAlexander Motin 		if ((path->device->flags & CAM_DEV_UNCONFIGURED) ||
28883c5d981SAlexander Motin 		    path->device->protocol == PROTO_SATAPM)
28983c5d981SAlexander Motin 			softc->action = PROBE_RESET;
29083c5d981SAlexander Motin 		else
29183c5d981SAlexander Motin 			softc->action = PROBE_IDENTIFY;
29283c5d981SAlexander Motin 	}
29352c9ce25SScott Long 	switch (softc->action) {
29452c9ce25SScott Long 	case PROBE_RESET:
29552c9ce25SScott Long 		cam_fill_ataio(ataio,
29652c9ce25SScott Long 		      0,
29752c9ce25SScott Long 		      probedone,
29852c9ce25SScott Long 		      /*flags*/CAM_DIR_NONE,
29965d0fb03SAlexander Motin 		      0,
30052c9ce25SScott Long 		      /*data_ptr*/NULL,
30152c9ce25SScott Long 		      /*dxfer_len*/0,
30283c5d981SAlexander Motin 		      15 * 1000);
30352c9ce25SScott Long 		ata_reset_cmd(ataio);
30452c9ce25SScott Long 		break;
30552c9ce25SScott Long 	case PROBE_IDENTIFY:
30652c9ce25SScott Long 		if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
30752c9ce25SScott Long 			/* Prepare check that it is the same device. */
30852c9ce25SScott Long 			MD5_CTX context;
30952c9ce25SScott Long 
31052c9ce25SScott Long 			MD5Init(&context);
31152c9ce25SScott Long 			MD5Update(&context,
31252c9ce25SScott Long 			    (unsigned char *)ident_buf->model,
31352c9ce25SScott Long 			    sizeof(ident_buf->model));
31452c9ce25SScott Long 			MD5Update(&context,
31552c9ce25SScott Long 			    (unsigned char *)ident_buf->revision,
31652c9ce25SScott Long 			    sizeof(ident_buf->revision));
31752c9ce25SScott Long 			MD5Update(&context,
31852c9ce25SScott Long 			    (unsigned char *)ident_buf->serial,
31952c9ce25SScott Long 			    sizeof(ident_buf->serial));
32052c9ce25SScott Long 			MD5Final(softc->digest, &context);
32152c9ce25SScott Long 		}
32252c9ce25SScott Long 		cam_fill_ataio(ataio,
32352c9ce25SScott Long 		      1,
32452c9ce25SScott Long 		      probedone,
32552c9ce25SScott Long 		      /*flags*/CAM_DIR_IN,
32665d0fb03SAlexander Motin 		      0,
32752c9ce25SScott Long 		      /*data_ptr*/(u_int8_t *)ident_buf,
32852c9ce25SScott Long 		      /*dxfer_len*/sizeof(struct ata_params),
32952c9ce25SScott Long 		      30 * 1000);
33052c9ce25SScott Long 		if (periph->path->device->protocol == PROTO_ATA)
3317606b445SAlexander Motin 			ata_28bit_cmd(ataio, ATA_ATA_IDENTIFY, 0, 0, 0);
33252c9ce25SScott Long 		else
3337606b445SAlexander Motin 			ata_28bit_cmd(ataio, ATA_ATAPI_IDENTIFY, 0, 0, 0);
33452c9ce25SScott Long 		break;
33552c9ce25SScott Long 	case PROBE_SETMODE:
336c8039fc6SAlexander Motin 	{
337c8039fc6SAlexander Motin 		int mode, wantmode;
338c8039fc6SAlexander Motin 
339c8039fc6SAlexander Motin 		mode = 0;
340c8039fc6SAlexander Motin 		/* Fetch user modes from SIM. */
341c8039fc6SAlexander Motin 		bzero(&cts, sizeof(cts));
34283c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
343c8039fc6SAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
344c8039fc6SAlexander Motin 		cts.type = CTS_TYPE_USER_SETTINGS;
345c8039fc6SAlexander Motin 		xpt_action((union ccb *)&cts);
346c8039fc6SAlexander Motin 		if (path->device->transport == XPORT_ATA) {
347c8039fc6SAlexander Motin 			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_MODE)
348c8039fc6SAlexander Motin 				mode = cts.xport_specific.ata.mode;
349c8039fc6SAlexander Motin 		} else {
3501a6f09b8SAlexander Motin 			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_MODE)
351c8039fc6SAlexander Motin 				mode = cts.xport_specific.sata.mode;
352c8039fc6SAlexander Motin 		}
353c8039fc6SAlexander Motin negotiate:
354c8039fc6SAlexander Motin 		/* Honor device capabilities. */
355c8039fc6SAlexander Motin 		wantmode = mode = ata_max_mode(ident_buf, mode);
356c8039fc6SAlexander Motin 		/* Report modes to SIM. */
357c8039fc6SAlexander Motin 		bzero(&cts, sizeof(cts));
35883c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
359c8039fc6SAlexander Motin 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
360c8039fc6SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
361c8039fc6SAlexander Motin 		if (path->device->transport == XPORT_ATA) {
362c8039fc6SAlexander Motin 			cts.xport_specific.ata.mode = mode;
363c8039fc6SAlexander Motin 			cts.xport_specific.ata.valid = CTS_ATA_VALID_MODE;
364c8039fc6SAlexander Motin 		} else {
365c8039fc6SAlexander Motin 			cts.xport_specific.sata.mode = mode;
366c8039fc6SAlexander Motin 			cts.xport_specific.sata.valid = CTS_SATA_VALID_MODE;
367c8039fc6SAlexander Motin 		}
368c8039fc6SAlexander Motin 		xpt_action((union ccb *)&cts);
369066f913aSAlexander Motin 		/* Fetch current modes from SIM. */
370c8039fc6SAlexander Motin 		bzero(&cts, sizeof(cts));
37183c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
372c8039fc6SAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
373c8039fc6SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
374c8039fc6SAlexander Motin 		xpt_action((union ccb *)&cts);
375c8039fc6SAlexander Motin 		if (path->device->transport == XPORT_ATA) {
376c8039fc6SAlexander Motin 			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_MODE)
377c8039fc6SAlexander Motin 				mode = cts.xport_specific.ata.mode;
378c8039fc6SAlexander Motin 		} else {
379c8039fc6SAlexander Motin 			if (cts.xport_specific.ata.valid & CTS_SATA_VALID_MODE)
380c8039fc6SAlexander Motin 				mode = cts.xport_specific.sata.mode;
381c8039fc6SAlexander Motin 		}
382c8039fc6SAlexander Motin 		/* If SIM disagree - renegotiate. */
383c8039fc6SAlexander Motin 		if (mode != wantmode)
384c8039fc6SAlexander Motin 			goto negotiate;
38552c9ce25SScott Long 		cam_fill_ataio(ataio,
38652c9ce25SScott Long 		      1,
38752c9ce25SScott Long 		      probedone,
3885daa555dSAlexander Motin 		      /*flags*/CAM_DIR_NONE,
3895daa555dSAlexander Motin 		      0,
3905daa555dSAlexander Motin 		      /*data_ptr*/NULL,
3915daa555dSAlexander Motin 		      /*dxfer_len*/0,
39252c9ce25SScott Long 		      30 * 1000);
393c8039fc6SAlexander Motin 		ata_28bit_cmd(ataio, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
39452c9ce25SScott Long 		break;
395c8039fc6SAlexander Motin 	}
3961e637ba6SAlexander Motin 	case PROBE_SET_MULTI:
3971e637ba6SAlexander Motin 	{
398066f913aSAlexander Motin 		u_int sectors, bytecount;
3991e637ba6SAlexander Motin 
400066f913aSAlexander Motin 		bytecount = 8192;	/* SATA maximum */
401066f913aSAlexander Motin 		/* Fetch user bytecount from SIM. */
402066f913aSAlexander Motin 		bzero(&cts, sizeof(cts));
40383c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
404066f913aSAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
405066f913aSAlexander Motin 		cts.type = CTS_TYPE_USER_SETTINGS;
406066f913aSAlexander Motin 		xpt_action((union ccb *)&cts);
407066f913aSAlexander Motin 		if (path->device->transport == XPORT_ATA) {
408066f913aSAlexander Motin 			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
409066f913aSAlexander Motin 				bytecount = cts.xport_specific.ata.bytecount;
410066f913aSAlexander Motin 		} else {
411066f913aSAlexander Motin 			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
412066f913aSAlexander Motin 				bytecount = cts.xport_specific.sata.bytecount;
413066f913aSAlexander Motin 		}
414066f913aSAlexander Motin 		/* Honor device capabilities. */
415066f913aSAlexander Motin 		sectors = max(1, min(ident_buf->sectors_intr & 0xff,
416066f913aSAlexander Motin 		    bytecount / ata_logical_sector_size(ident_buf)));
4171e637ba6SAlexander Motin 		/* Report bytecount to SIM. */
4181e637ba6SAlexander Motin 		bzero(&cts, sizeof(cts));
41983c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
4201e637ba6SAlexander Motin 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
4211e637ba6SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
4221e637ba6SAlexander Motin 		if (path->device->transport == XPORT_ATA) {
423c1bd46c2SAlexander Motin 			cts.xport_specific.ata.bytecount = sectors *
424c1bd46c2SAlexander Motin 			    ata_logical_sector_size(ident_buf);
4251e637ba6SAlexander Motin 			cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT;
4261e637ba6SAlexander Motin 		} else {
427c1bd46c2SAlexander Motin 			cts.xport_specific.sata.bytecount = sectors *
428c1bd46c2SAlexander Motin 			    ata_logical_sector_size(ident_buf);
4291e637ba6SAlexander Motin 			cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT;
4301e637ba6SAlexander Motin 		}
4311e637ba6SAlexander Motin 		xpt_action((union ccb *)&cts);
432066f913aSAlexander Motin 		/* Fetch current bytecount from SIM. */
433066f913aSAlexander Motin 		bzero(&cts, sizeof(cts));
43483c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
435066f913aSAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
436066f913aSAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
437066f913aSAlexander Motin 		xpt_action((union ccb *)&cts);
438066f913aSAlexander Motin 		if (path->device->transport == XPORT_ATA) {
439066f913aSAlexander Motin 			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
440066f913aSAlexander Motin 				bytecount = cts.xport_specific.ata.bytecount;
441066f913aSAlexander Motin 		} else {
442066f913aSAlexander Motin 			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
443066f913aSAlexander Motin 				bytecount = cts.xport_specific.sata.bytecount;
444066f913aSAlexander Motin 		}
445066f913aSAlexander Motin 		sectors = bytecount / ata_logical_sector_size(ident_buf);
4461e637ba6SAlexander Motin 
4471e637ba6SAlexander Motin 		cam_fill_ataio(ataio,
4481e637ba6SAlexander Motin 		    1,
4491e637ba6SAlexander Motin 		    probedone,
4501e637ba6SAlexander Motin 		    CAM_DIR_NONE,
4511e637ba6SAlexander Motin 		    0,
4521e637ba6SAlexander Motin 		    NULL,
4531e637ba6SAlexander Motin 		    0,
4541e637ba6SAlexander Motin 		    30*1000);
4551e637ba6SAlexander Motin 		ata_28bit_cmd(ataio, ATA_SET_MULTI, 0, 0, sectors);
4561e637ba6SAlexander Motin 		break;
45752c9ce25SScott Long 	}
45852c9ce25SScott Long 	case PROBE_INQUIRY:
459066f913aSAlexander Motin 	{
460066f913aSAlexander Motin 		u_int bytecount;
461066f913aSAlexander Motin 
462066f913aSAlexander Motin 		bytecount = 8192;	/* SATA maximum */
463066f913aSAlexander Motin 		/* Fetch user bytecount from SIM. */
464066f913aSAlexander Motin 		bzero(&cts, sizeof(cts));
46583c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
466066f913aSAlexander Motin 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
467066f913aSAlexander Motin 		cts.type = CTS_TYPE_USER_SETTINGS;
468066f913aSAlexander Motin 		xpt_action((union ccb *)&cts);
469066f913aSAlexander Motin 		if (path->device->transport == XPORT_ATA) {
470066f913aSAlexander Motin 			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
471066f913aSAlexander Motin 				bytecount = cts.xport_specific.ata.bytecount;
472066f913aSAlexander Motin 		} else {
473066f913aSAlexander Motin 			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
474066f913aSAlexander Motin 				bytecount = cts.xport_specific.sata.bytecount;
475066f913aSAlexander Motin 		}
476066f913aSAlexander Motin 		/* Honor device capabilities. */
477066f913aSAlexander Motin 		bytecount &= ~1;
478066f913aSAlexander Motin 		bytecount = max(2, min(65534, bytecount));
479066f913aSAlexander Motin 		if (ident_buf->satacapabilities != 0x0000 &&
480066f913aSAlexander Motin 		    ident_buf->satacapabilities != 0xffff) {
481066f913aSAlexander Motin 			bytecount = min(8192, bytecount);
482066f913aSAlexander Motin 		}
483066f913aSAlexander Motin 		/* Report bytecount to SIM. */
484066f913aSAlexander Motin 		bzero(&cts, sizeof(cts));
48583c5d981SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
486066f913aSAlexander Motin 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
487066f913aSAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
488066f913aSAlexander Motin 		if (path->device->transport == XPORT_ATA) {
489066f913aSAlexander Motin 			cts.xport_specific.ata.bytecount = bytecount;
490066f913aSAlexander Motin 			cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT;
491066f913aSAlexander Motin 		} else {
492066f913aSAlexander Motin 			cts.xport_specific.sata.bytecount = bytecount;
493066f913aSAlexander Motin 			cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT;
494066f913aSAlexander Motin 		}
495066f913aSAlexander Motin 		xpt_action((union ccb *)&cts);
496066f913aSAlexander Motin 		/* FALLTHROUGH */
497066f913aSAlexander Motin 	}
49852c9ce25SScott Long 	case PROBE_FULL_INQUIRY:
49952c9ce25SScott Long 	{
50052c9ce25SScott Long 		u_int inquiry_len;
50152c9ce25SScott Long 		struct scsi_inquiry_data *inq_buf =
50252c9ce25SScott Long 		    &periph->path->device->inq_data;
50352c9ce25SScott Long 
50452c9ce25SScott Long 		if (softc->action == PROBE_INQUIRY)
50552c9ce25SScott Long 			inquiry_len = SHORT_INQUIRY_LENGTH;
50652c9ce25SScott Long 		else
50752c9ce25SScott Long 			inquiry_len = SID_ADDITIONAL_LENGTH(inq_buf);
50852c9ce25SScott Long 		/*
50952c9ce25SScott Long 		 * Some parallel SCSI devices fail to send an
51052c9ce25SScott Long 		 * ignore wide residue message when dealing with
51152c9ce25SScott Long 		 * odd length inquiry requests.  Round up to be
51252c9ce25SScott Long 		 * safe.
51352c9ce25SScott Long 		 */
51452c9ce25SScott Long 		inquiry_len = roundup2(inquiry_len, 2);
51552c9ce25SScott Long 		scsi_inquiry(csio,
51652c9ce25SScott Long 			     /*retries*/1,
51752c9ce25SScott Long 			     probedone,
51852c9ce25SScott Long 			     MSG_SIMPLE_Q_TAG,
51952c9ce25SScott Long 			     (u_int8_t *)inq_buf,
52052c9ce25SScott Long 			     inquiry_len,
52152c9ce25SScott Long 			     /*evpd*/FALSE,
52252c9ce25SScott Long 			     /*page_code*/0,
52352c9ce25SScott Long 			     SSD_MIN_SIZE,
52452c9ce25SScott Long 			     /*timeout*/60 * 1000);
52552c9ce25SScott Long 		break;
52652c9ce25SScott Long 	}
52752c9ce25SScott Long 	case PROBE_PM_PID:
52852c9ce25SScott Long 		cam_fill_ataio(ataio,
52952c9ce25SScott Long 		      1,
53052c9ce25SScott Long 		      probedone,
53152c9ce25SScott Long 		      /*flags*/CAM_DIR_NONE,
53265d0fb03SAlexander Motin 		      0,
53352c9ce25SScott Long 		      /*data_ptr*/NULL,
53452c9ce25SScott Long 		      /*dxfer_len*/0,
53552c9ce25SScott Long 		      10 * 1000);
53652c9ce25SScott Long 		ata_pm_read_cmd(ataio, 0, 15);
53752c9ce25SScott Long 		break;
53852c9ce25SScott Long 	case PROBE_PM_PRV:
53952c9ce25SScott Long 		cam_fill_ataio(ataio,
54052c9ce25SScott Long 		      1,
54152c9ce25SScott Long 		      probedone,
54252c9ce25SScott Long 		      /*flags*/CAM_DIR_NONE,
54365d0fb03SAlexander Motin 		      0,
54452c9ce25SScott Long 		      /*data_ptr*/NULL,
54552c9ce25SScott Long 		      /*dxfer_len*/0,
54652c9ce25SScott Long 		      10 * 1000);
54752c9ce25SScott Long 		ata_pm_read_cmd(ataio, 1, 15);
54852c9ce25SScott Long 		break;
54952c9ce25SScott Long 	case PROBE_INVALID:
5501e637ba6SAlexander Motin 		CAM_DEBUG(path, CAM_DEBUG_INFO,
55152c9ce25SScott Long 		    ("probestart: invalid action state\n"));
55252c9ce25SScott Long 	default:
55352c9ce25SScott Long 		break;
55452c9ce25SScott Long 	}
55552c9ce25SScott Long 	xpt_action(start_ccb);
55652c9ce25SScott Long }
55752c9ce25SScott Long #if 0
55852c9ce25SScott Long static void
55952c9ce25SScott Long proberequestdefaultnegotiation(struct cam_periph *periph)
56052c9ce25SScott Long {
56152c9ce25SScott Long 	struct ccb_trans_settings cts;
56252c9ce25SScott Long 
56383c5d981SAlexander Motin 	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
56452c9ce25SScott Long 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
56552c9ce25SScott Long 	cts.type = CTS_TYPE_USER_SETTINGS;
56652c9ce25SScott Long 	xpt_action((union ccb *)&cts);
56752c9ce25SScott Long 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
56852c9ce25SScott Long 		return;
56952c9ce25SScott Long 	}
57052c9ce25SScott Long 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
57152c9ce25SScott Long 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
57252c9ce25SScott Long 	xpt_action((union ccb *)&cts);
57352c9ce25SScott Long }
57452c9ce25SScott Long 
57552c9ce25SScott Long /*
57652c9ce25SScott Long  * Backoff Negotiation Code- only pertinent for SPI devices.
57752c9ce25SScott Long  */
57852c9ce25SScott Long static int
57952c9ce25SScott Long proberequestbackoff(struct cam_periph *periph, struct cam_ed *device)
58052c9ce25SScott Long {
58152c9ce25SScott Long 	struct ccb_trans_settings cts;
58252c9ce25SScott Long 	struct ccb_trans_settings_spi *spi;
58352c9ce25SScott Long 
58452c9ce25SScott Long 	memset(&cts, 0, sizeof (cts));
58583c5d981SAlexander Motin 	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
58652c9ce25SScott Long 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
58752c9ce25SScott Long 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
58852c9ce25SScott Long 	xpt_action((union ccb *)&cts);
58952c9ce25SScott Long 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
59052c9ce25SScott Long 		if (bootverbose) {
59152c9ce25SScott Long 			xpt_print(periph->path,
59252c9ce25SScott Long 			    "failed to get current device settings\n");
59352c9ce25SScott Long 		}
59452c9ce25SScott Long 		return (0);
59552c9ce25SScott Long 	}
59652c9ce25SScott Long 	if (cts.transport != XPORT_SPI) {
59752c9ce25SScott Long 		if (bootverbose) {
59852c9ce25SScott Long 			xpt_print(periph->path, "not SPI transport\n");
59952c9ce25SScott Long 		}
60052c9ce25SScott Long 		return (0);
60152c9ce25SScott Long 	}
60252c9ce25SScott Long 	spi = &cts.xport_specific.spi;
60352c9ce25SScott Long 
60452c9ce25SScott Long 	/*
60552c9ce25SScott Long 	 * We cannot renegotiate sync rate if we don't have one.
60652c9ce25SScott Long 	 */
60752c9ce25SScott Long 	if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
60852c9ce25SScott Long 		if (bootverbose) {
60952c9ce25SScott Long 			xpt_print(periph->path, "no sync rate known\n");
61052c9ce25SScott Long 		}
61152c9ce25SScott Long 		return (0);
61252c9ce25SScott Long 	}
61352c9ce25SScott Long 
61452c9ce25SScott Long 	/*
61552c9ce25SScott Long 	 * We'll assert that we don't have to touch PPR options- the
61652c9ce25SScott Long 	 * SIM will see what we do with period and offset and adjust
61752c9ce25SScott Long 	 * the PPR options as appropriate.
61852c9ce25SScott Long 	 */
61952c9ce25SScott Long 
62052c9ce25SScott Long 	/*
62152c9ce25SScott Long 	 * A sync rate with unknown or zero offset is nonsensical.
62252c9ce25SScott Long 	 * A sync period of zero means Async.
62352c9ce25SScott Long 	 */
62452c9ce25SScott Long 	if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0
62552c9ce25SScott Long 	 || spi->sync_offset == 0 || spi->sync_period == 0) {
62652c9ce25SScott Long 		if (bootverbose) {
62752c9ce25SScott Long 			xpt_print(periph->path, "no sync rate available\n");
62852c9ce25SScott Long 		}
62952c9ce25SScott Long 		return (0);
63052c9ce25SScott Long 	}
63152c9ce25SScott Long 
63252c9ce25SScott Long 	if (device->flags & CAM_DEV_DV_HIT_BOTTOM) {
63352c9ce25SScott Long 		CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
63452c9ce25SScott Long 		    ("hit async: giving up on DV\n"));
63552c9ce25SScott Long 		return (0);
63652c9ce25SScott Long 	}
63752c9ce25SScott Long 
63852c9ce25SScott Long 
63952c9ce25SScott Long 	/*
64052c9ce25SScott Long 	 * Jump sync_period up by one, but stop at 5MHz and fall back to Async.
64152c9ce25SScott Long 	 * We don't try to remember 'last' settings to see if the SIM actually
64252c9ce25SScott Long 	 * gets into the speed we want to set. We check on the SIM telling
64352c9ce25SScott Long 	 * us that a requested speed is bad, but otherwise don't try and
64452c9ce25SScott Long 	 * check the speed due to the asynchronous and handshake nature
64552c9ce25SScott Long 	 * of speed setting.
64652c9ce25SScott Long 	 */
64752c9ce25SScott Long 	spi->valid = CTS_SPI_VALID_SYNC_RATE | CTS_SPI_VALID_SYNC_OFFSET;
64852c9ce25SScott Long 	for (;;) {
64952c9ce25SScott Long 		spi->sync_period++;
65052c9ce25SScott Long 		if (spi->sync_period >= 0xf) {
65152c9ce25SScott Long 			spi->sync_period = 0;
65252c9ce25SScott Long 			spi->sync_offset = 0;
65352c9ce25SScott Long 			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
65452c9ce25SScott Long 			    ("setting to async for DV\n"));
65552c9ce25SScott Long 			/*
65652c9ce25SScott Long 			 * Once we hit async, we don't want to try
65752c9ce25SScott Long 			 * any more settings.
65852c9ce25SScott Long 			 */
65952c9ce25SScott Long 			device->flags |= CAM_DEV_DV_HIT_BOTTOM;
66052c9ce25SScott Long 		} else if (bootverbose) {
66152c9ce25SScott Long 			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
66252c9ce25SScott Long 			    ("DV: period 0x%x\n", spi->sync_period));
66352c9ce25SScott Long 			printf("setting period to 0x%x\n", spi->sync_period);
66452c9ce25SScott Long 		}
66552c9ce25SScott Long 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
66652c9ce25SScott Long 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
66752c9ce25SScott Long 		xpt_action((union ccb *)&cts);
66852c9ce25SScott Long 		if ((cts.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
66952c9ce25SScott Long 			break;
67052c9ce25SScott Long 		}
67152c9ce25SScott Long 		CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
67252c9ce25SScott Long 		    ("DV: failed to set period 0x%x\n", spi->sync_period));
67352c9ce25SScott Long 		if (spi->sync_period == 0) {
67452c9ce25SScott Long 			return (0);
67552c9ce25SScott Long 		}
67652c9ce25SScott Long 	}
67752c9ce25SScott Long 	return (1);
67852c9ce25SScott Long }
67952c9ce25SScott Long #endif
68052c9ce25SScott Long static void
68152c9ce25SScott Long probedone(struct cam_periph *periph, union ccb *done_ccb)
68252c9ce25SScott Long {
683c8039fc6SAlexander Motin 	struct ccb_trans_settings cts;
68452c9ce25SScott Long 	struct ata_params *ident_buf;
68552c9ce25SScott Long 	probe_softc *softc;
68652c9ce25SScott Long 	struct cam_path *path;
68752c9ce25SScott Long 	u_int32_t  priority;
68865d0fb03SAlexander Motin 	int found = 1;
68952c9ce25SScott Long 
69052c9ce25SScott Long 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
69152c9ce25SScott Long 
69252c9ce25SScott Long 	softc = (probe_softc *)periph->softc;
69352c9ce25SScott Long 	path = done_ccb->ccb_h.path;
69452c9ce25SScott Long 	priority = done_ccb->ccb_h.pinfo.priority;
69552c9ce25SScott Long 	ident_buf = &path->device->ident_data;
69652c9ce25SScott Long 
6971e637ba6SAlexander Motin 	if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
6981e637ba6SAlexander Motin device_fail:	if (cam_periph_error(done_ccb, 0, 0,
6991e637ba6SAlexander Motin 		    &softc->saved_ccb) == ERESTART) {
7001e637ba6SAlexander Motin 			return;
7011e637ba6SAlexander Motin 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
7021e637ba6SAlexander Motin 			/* Don't wedge the queue */
7031e637ba6SAlexander Motin 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
7041e637ba6SAlexander Motin 					 /*run_queue*/TRUE);
7051e637ba6SAlexander Motin 		}
7061e637ba6SAlexander Motin 		/* Old PIO2 devices may not support mode setting. */
7071e637ba6SAlexander Motin 		if (softc->action == PROBE_SETMODE &&
7081e637ba6SAlexander Motin 		    ata_max_pmode(ident_buf) <= ATA_PIO2 &&
7091e637ba6SAlexander Motin 		    (ident_buf->capabilities1 & ATA_SUPPORT_IORDY) == 0)
7101e637ba6SAlexander Motin 			goto noerror;
7111e637ba6SAlexander Motin 		/*
7121e637ba6SAlexander Motin 		 * If we get to this point, we got an error status back
7131e637ba6SAlexander Motin 		 * from the inquiry and the error status doesn't require
7141e637ba6SAlexander Motin 		 * automatically retrying the command.  Therefore, the
7151e637ba6SAlexander Motin 		 * inquiry failed.  If we had inquiry information before
7161e637ba6SAlexander Motin 		 * for this device, but this latest inquiry command failed,
7171e637ba6SAlexander Motin 		 * the device has probably gone away.  If this device isn't
7181e637ba6SAlexander Motin 		 * already marked unconfigured, notify the peripheral
7191e637ba6SAlexander Motin 		 * drivers that this device is no more.
7201e637ba6SAlexander Motin 		 */
7211e637ba6SAlexander Motin 		if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
7221e637ba6SAlexander Motin 			xpt_async(AC_LOST_DEVICE, path, NULL);
7231e637ba6SAlexander Motin 		found = 0;
7241e637ba6SAlexander Motin 		goto done;
7251e637ba6SAlexander Motin 	}
7261e637ba6SAlexander Motin noerror:
72752c9ce25SScott Long 	switch (softc->action) {
72852c9ce25SScott Long 	case PROBE_RESET:
7291e637ba6SAlexander Motin 	{
73052c9ce25SScott Long 		int sign = (done_ccb->ataio.res.lba_high << 8) +
73152c9ce25SScott Long 		    done_ccb->ataio.res.lba_mid;
7327bdb664eSAlexander Motin 		if (bootverbose)
73352c9ce25SScott Long 			xpt_print(path, "SIGNATURE: %04x\n", sign);
73452c9ce25SScott Long 		if (sign == 0x0000 &&
73552c9ce25SScott Long 		    done_ccb->ccb_h.target_id != 15) {
73652c9ce25SScott Long 			path->device->protocol = PROTO_ATA;
73752c9ce25SScott Long 			PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
73852c9ce25SScott Long 		} else if (sign == 0x9669 &&
73952c9ce25SScott Long 		    done_ccb->ccb_h.target_id == 15) {
74052c9ce25SScott Long 			/* Report SIM that PM is present. */
74152c9ce25SScott Long 			bzero(&cts, sizeof(cts));
74283c5d981SAlexander Motin 			xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
74352c9ce25SScott Long 			cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
74452c9ce25SScott Long 			cts.type = CTS_TYPE_CURRENT_SETTINGS;
74552c9ce25SScott Long 			cts.xport_specific.sata.pm_present = 1;
74652c9ce25SScott Long 			cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
74752c9ce25SScott Long 			xpt_action((union ccb *)&cts);
74852c9ce25SScott Long 			path->device->protocol = PROTO_SATAPM;
74952c9ce25SScott Long 			PROBE_SET_ACTION(softc, PROBE_PM_PID);
75052c9ce25SScott Long 		} else if (sign == 0xeb14 &&
75152c9ce25SScott Long 		    done_ccb->ccb_h.target_id != 15) {
75252c9ce25SScott Long 			path->device->protocol = PROTO_SCSI;
75352c9ce25SScott Long 			PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
75452c9ce25SScott Long 		} else {
75552c9ce25SScott Long 			if (done_ccb->ccb_h.target_id != 15) {
75652c9ce25SScott Long 				xpt_print(path,
75752c9ce25SScott Long 				    "Unexpected signature 0x%04x\n", sign);
75852c9ce25SScott Long 			}
75965d0fb03SAlexander Motin 			goto device_fail;
76052c9ce25SScott Long 		}
76152c9ce25SScott Long 		xpt_release_ccb(done_ccb);
76252c9ce25SScott Long 		xpt_schedule(periph, priority);
76352c9ce25SScott Long 		return;
76452c9ce25SScott Long 	}
76552c9ce25SScott Long 	case PROBE_IDENTIFY:
76652c9ce25SScott Long 	{
76752c9ce25SScott Long 		int16_t *ptr;
76852c9ce25SScott Long 
76952c9ce25SScott Long 		for (ptr = (int16_t *)ident_buf;
77052c9ce25SScott Long 		     ptr < (int16_t *)ident_buf + sizeof(struct ata_params)/2; ptr++) {
77152c9ce25SScott Long 			*ptr = le16toh(*ptr);
77252c9ce25SScott Long 		}
77352c9ce25SScott Long 		if (strncmp(ident_buf->model, "FX", 2) &&
77452c9ce25SScott Long 		    strncmp(ident_buf->model, "NEC", 3) &&
77552c9ce25SScott Long 		    strncmp(ident_buf->model, "Pioneer", 7) &&
77652c9ce25SScott Long 		    strncmp(ident_buf->model, "SHARP", 5)) {
77752c9ce25SScott Long 			ata_bswap(ident_buf->model, sizeof(ident_buf->model));
77852c9ce25SScott Long 			ata_bswap(ident_buf->revision, sizeof(ident_buf->revision));
77952c9ce25SScott Long 			ata_bswap(ident_buf->serial, sizeof(ident_buf->serial));
78052c9ce25SScott Long 		}
78152c9ce25SScott Long 		ata_btrim(ident_buf->model, sizeof(ident_buf->model));
78252c9ce25SScott Long 		ata_bpack(ident_buf->model, ident_buf->model, sizeof(ident_buf->model));
78352c9ce25SScott Long 		ata_btrim(ident_buf->revision, sizeof(ident_buf->revision));
78452c9ce25SScott Long 		ata_bpack(ident_buf->revision, ident_buf->revision, sizeof(ident_buf->revision));
78552c9ce25SScott Long 		ata_btrim(ident_buf->serial, sizeof(ident_buf->serial));
78652c9ce25SScott Long 		ata_bpack(ident_buf->serial, ident_buf->serial, sizeof(ident_buf->serial));
78752c9ce25SScott Long 
78852c9ce25SScott Long 		if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
78952c9ce25SScott Long 			/* Check that it is the same device. */
79052c9ce25SScott Long 			MD5_CTX context;
79152c9ce25SScott Long 			u_int8_t digest[16];
79252c9ce25SScott Long 
79352c9ce25SScott Long 			MD5Init(&context);
79452c9ce25SScott Long 			MD5Update(&context,
79552c9ce25SScott Long 			    (unsigned char *)ident_buf->model,
79652c9ce25SScott Long 			    sizeof(ident_buf->model));
79752c9ce25SScott Long 			MD5Update(&context,
79852c9ce25SScott Long 			    (unsigned char *)ident_buf->revision,
79952c9ce25SScott Long 			    sizeof(ident_buf->revision));
80052c9ce25SScott Long 			MD5Update(&context,
80152c9ce25SScott Long 			    (unsigned char *)ident_buf->serial,
80252c9ce25SScott Long 			    sizeof(ident_buf->serial));
80352c9ce25SScott Long 			MD5Final(digest, &context);
80452c9ce25SScott Long 			if (bcmp(digest, softc->digest, sizeof(digest))) {
80552c9ce25SScott Long 				/* Device changed. */
80652c9ce25SScott Long 				xpt_async(AC_LOST_DEVICE, path, NULL);
80752c9ce25SScott Long 			}
8081e637ba6SAlexander Motin 		} else {
80952c9ce25SScott Long 			/* Clean up from previous instance of this device */
81052c9ce25SScott Long 			if (path->device->serial_num != NULL) {
81152c9ce25SScott Long 				free(path->device->serial_num, M_CAMXPT);
81252c9ce25SScott Long 				path->device->serial_num = NULL;
81352c9ce25SScott Long 				path->device->serial_num_len = 0;
81452c9ce25SScott Long 			}
81552c9ce25SScott Long 			path->device->serial_num =
81652c9ce25SScott Long 				(u_int8_t *)malloc((sizeof(ident_buf->serial) + 1),
81752c9ce25SScott Long 					   M_CAMXPT, M_NOWAIT);
81852c9ce25SScott Long 			if (path->device->serial_num != NULL) {
81952c9ce25SScott Long 				bcopy(ident_buf->serial,
82052c9ce25SScott Long 				      path->device->serial_num,
82152c9ce25SScott Long 				      sizeof(ident_buf->serial));
82252c9ce25SScott Long 				path->device->serial_num[sizeof(ident_buf->serial)]
82352c9ce25SScott Long 				    = '\0';
82452c9ce25SScott Long 				path->device->serial_num_len =
82552c9ce25SScott Long 				    strlen(path->device->serial_num);
82652c9ce25SScott Long 			}
82752c9ce25SScott Long 
8284b997c49SAlexander Motin 			path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
8291e637ba6SAlexander Motin 		}
83030a4094fSAlexander Motin 		if (ident_buf->satacapabilities & ATA_SUPPORT_NCQ) {
83130a4094fSAlexander Motin 			path->device->mintags = path->device->maxtags =
83230a4094fSAlexander Motin 			    ATA_QUEUE_LEN(ident_buf->queue) + 1;
83330a4094fSAlexander Motin 		}
83430a4094fSAlexander Motin 		ata_find_quirk(path->device);
835507e5811SAlexander Motin 		if (path->device->mintags != 0 &&
836507e5811SAlexander Motin 		    path->bus->sim->max_tagged_dev_openings != 0) {
837c8039fc6SAlexander Motin 			/* Report SIM which tags are allowed. */
838c8039fc6SAlexander Motin 			bzero(&cts, sizeof(cts));
83983c5d981SAlexander Motin 			xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
840c8039fc6SAlexander Motin 			cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
841c8039fc6SAlexander Motin 			cts.type = CTS_TYPE_CURRENT_SETTINGS;
842c8039fc6SAlexander Motin 			cts.xport_specific.sata.tags = path->device->maxtags;
843c8039fc6SAlexander Motin 			cts.xport_specific.sata.valid = CTS_SATA_VALID_TAGS;
844c8039fc6SAlexander Motin 			xpt_action((union ccb *)&cts);
845c8039fc6SAlexander Motin 			/* Reconfigure queues for tagged queueing. */
84630a4094fSAlexander Motin 			xpt_start_tags(path);
84730a4094fSAlexander Motin 		}
84852c9ce25SScott Long 		ata_device_transport(path);
84952c9ce25SScott Long 		PROBE_SET_ACTION(softc, PROBE_SETMODE);
85052c9ce25SScott Long 		xpt_release_ccb(done_ccb);
85152c9ce25SScott Long 		xpt_schedule(periph, priority);
85252c9ce25SScott Long 		return;
85352c9ce25SScott Long 	}
85452c9ce25SScott Long 	case PROBE_SETMODE:
8551e637ba6SAlexander Motin 		if (path->device->protocol == PROTO_ATA) {
8561e637ba6SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_SET_MULTI);
8571e637ba6SAlexander Motin 		} else {
8581e637ba6SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_INQUIRY);
8591e637ba6SAlexander Motin 		}
8601e637ba6SAlexander Motin 		xpt_release_ccb(done_ccb);
8611e637ba6SAlexander Motin 		xpt_schedule(periph, priority);
8621e637ba6SAlexander Motin 		return;
8631e637ba6SAlexander Motin 	case PROBE_SET_MULTI:
8641e637ba6SAlexander Motin 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
86552c9ce25SScott Long 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
866f98d7a47SAlexander Motin 			xpt_acquire_device(path->device);
86752c9ce25SScott Long 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
86852c9ce25SScott Long 			xpt_action(done_ccb);
86952c9ce25SScott Long 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
87052c9ce25SScott Long 			    done_ccb);
8711e637ba6SAlexander Motin 		}
87252c9ce25SScott Long 		break;
87352c9ce25SScott Long 	case PROBE_INQUIRY:
87452c9ce25SScott Long 	case PROBE_FULL_INQUIRY:
87552c9ce25SScott Long 	{
87652c9ce25SScott Long 		struct scsi_inquiry_data *inq_buf;
8771e637ba6SAlexander Motin 		u_int8_t periph_qual, len;
87852c9ce25SScott Long 
87952c9ce25SScott Long 		path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
88052c9ce25SScott Long 		inq_buf = &path->device->inq_data;
88152c9ce25SScott Long 
88252c9ce25SScott Long 		periph_qual = SID_QUAL(inq_buf);
88352c9ce25SScott Long 
8841e637ba6SAlexander Motin 		if (periph_qual != SID_QUAL_LU_CONNECTED)
8851e637ba6SAlexander Motin 			break;
88652c9ce25SScott Long 
88752c9ce25SScott Long 		/*
88852c9ce25SScott Long 		 * We conservatively request only
88952c9ce25SScott Long 		 * SHORT_INQUIRY_LEN bytes of inquiry
89052c9ce25SScott Long 		 * information during our first try
89152c9ce25SScott Long 		 * at sending an INQUIRY. If the device
89252c9ce25SScott Long 		 * has more information to give,
89352c9ce25SScott Long 		 * perform a second request specifying
89452c9ce25SScott Long 		 * the amount of information the device
89552c9ce25SScott Long 		 * is willing to give.
89652c9ce25SScott Long 		 */
89752c9ce25SScott Long 		len = inq_buf->additional_length
8981e637ba6SAlexander Motin 		    + offsetof(struct scsi_inquiry_data, additional_length) + 1;
89952c9ce25SScott Long 		if (softc->action == PROBE_INQUIRY
90052c9ce25SScott Long 		    && len > SHORT_INQUIRY_LENGTH) {
90152c9ce25SScott Long 			PROBE_SET_ACTION(softc, PROBE_FULL_INQUIRY);
90252c9ce25SScott Long 			xpt_release_ccb(done_ccb);
90352c9ce25SScott Long 			xpt_schedule(periph, priority);
90452c9ce25SScott Long 			return;
90552c9ce25SScott Long 		}
90652c9ce25SScott Long 
9074b997c49SAlexander Motin 		ata_device_transport(path);
9081e637ba6SAlexander Motin 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
90952c9ce25SScott Long 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
910f98d7a47SAlexander Motin 			xpt_acquire_device(path->device);
91152c9ce25SScott Long 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
91252c9ce25SScott Long 			xpt_action(done_ccb);
9131e637ba6SAlexander Motin 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path, done_ccb);
9141e637ba6SAlexander Motin 		}
91552c9ce25SScott Long 		break;
91652c9ce25SScott Long 	}
91752c9ce25SScott Long 	case PROBE_PM_PID:
9184b997c49SAlexander Motin 		if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) == 0)
91952c9ce25SScott Long 			bzero(ident_buf, sizeof(*ident_buf));
92052c9ce25SScott Long 		softc->pm_pid = (done_ccb->ataio.res.lba_high << 24) +
92152c9ce25SScott Long 		    (done_ccb->ataio.res.lba_mid << 16) +
92252c9ce25SScott Long 		    (done_ccb->ataio.res.lba_low << 8) +
92352c9ce25SScott Long 		    done_ccb->ataio.res.sector_count;
92465d0fb03SAlexander Motin 		((uint32_t *)ident_buf)[0] = softc->pm_pid;
92552c9ce25SScott Long 		snprintf(ident_buf->model, sizeof(ident_buf->model),
92652c9ce25SScott Long 		    "Port Multiplier %08x", softc->pm_pid);
92752c9ce25SScott Long 		PROBE_SET_ACTION(softc, PROBE_PM_PRV);
92852c9ce25SScott Long 		xpt_release_ccb(done_ccb);
92952c9ce25SScott Long 		xpt_schedule(periph, priority);
93052c9ce25SScott Long 		return;
93152c9ce25SScott Long 	case PROBE_PM_PRV:
93252c9ce25SScott Long 		softc->pm_prv = (done_ccb->ataio.res.lba_high << 24) +
93352c9ce25SScott Long 		    (done_ccb->ataio.res.lba_mid << 16) +
93452c9ce25SScott Long 		    (done_ccb->ataio.res.lba_low << 8) +
93552c9ce25SScott Long 		    done_ccb->ataio.res.sector_count;
93665d0fb03SAlexander Motin 		((uint32_t *)ident_buf)[1] = softc->pm_prv;
93752c9ce25SScott Long 		snprintf(ident_buf->revision, sizeof(ident_buf->revision),
93852c9ce25SScott Long 		    "%04x", softc->pm_prv);
9394b997c49SAlexander Motin 		path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
94065d0fb03SAlexander Motin 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
94152c9ce25SScott Long 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
942f98d7a47SAlexander Motin 			xpt_acquire_device(path->device);
94352c9ce25SScott Long 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
94452c9ce25SScott Long 			xpt_action(done_ccb);
94552c9ce25SScott Long 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
94652c9ce25SScott Long 			    done_ccb);
94765d0fb03SAlexander Motin 		} else {
94865d0fb03SAlexander Motin 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
94965d0fb03SAlexander Motin 			xpt_action(done_ccb);
9501e637ba6SAlexander Motin 			xpt_async(AC_SCSI_AEN, done_ccb->ccb_h.path, done_ccb);
95152c9ce25SScott Long 		}
95252c9ce25SScott Long 		break;
95352c9ce25SScott Long 	case PROBE_INVALID:
95452c9ce25SScott Long 		CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_INFO,
95552c9ce25SScott Long 		    ("probedone: invalid action state\n"));
95652c9ce25SScott Long 	default:
95752c9ce25SScott Long 		break;
95852c9ce25SScott Long 	}
9591e637ba6SAlexander Motin done:
96083c5d981SAlexander Motin 	if (softc->restart) {
96183c5d981SAlexander Motin 		softc->restart = 0;
9621e637ba6SAlexander Motin 		xpt_release_ccb(done_ccb);
96383c5d981SAlexander Motin 		probeschedule(periph);
96483c5d981SAlexander Motin 		return;
96583c5d981SAlexander Motin 	}
96683c5d981SAlexander Motin 	xpt_release_ccb(done_ccb);
96783c5d981SAlexander Motin 	while ((done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs))) {
96883c5d981SAlexander Motin 		TAILQ_REMOVE(&softc->request_ccbs,
96983c5d981SAlexander Motin 		    &done_ccb->ccb_h, periph_links.tqe);
97083c5d981SAlexander Motin 		done_ccb->ccb_h.status = found ? CAM_REQ_CMP : CAM_REQ_CMP_ERR;
97152c9ce25SScott Long 		xpt_done(done_ccb);
97283c5d981SAlexander Motin 	}
97383c5d981SAlexander Motin 	cam_release_devq(periph->path,
97483c5d981SAlexander Motin 	    RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_XPT + 1, FALSE);
97552c9ce25SScott Long 	cam_periph_invalidate(periph);
97652c9ce25SScott Long 	cam_periph_release_locked(periph);
97752c9ce25SScott Long }
97852c9ce25SScott Long 
97952c9ce25SScott Long static void
98052c9ce25SScott Long probecleanup(struct cam_periph *periph)
98152c9ce25SScott Long {
98252c9ce25SScott Long 	free(periph->softc, M_CAMXPT);
98352c9ce25SScott Long }
98452c9ce25SScott Long 
98552c9ce25SScott Long static void
98630a4094fSAlexander Motin ata_find_quirk(struct cam_ed *device)
98752c9ce25SScott Long {
98830a4094fSAlexander Motin 	struct ata_quirk_entry *quirk;
98952c9ce25SScott Long 	caddr_t	match;
99052c9ce25SScott Long 
99130a4094fSAlexander Motin 	match = cam_quirkmatch((caddr_t)&device->ident_data,
99230a4094fSAlexander Motin 			       (caddr_t)ata_quirk_table,
99330a4094fSAlexander Motin 			       ata_quirk_table_size,
99430a4094fSAlexander Motin 			       sizeof(*ata_quirk_table), ata_identify_match);
99552c9ce25SScott Long 
99652c9ce25SScott Long 	if (match == NULL)
99752c9ce25SScott Long 		panic("xpt_find_quirk: device didn't match wildcard entry!!");
99852c9ce25SScott Long 
99930a4094fSAlexander Motin 	quirk = (struct ata_quirk_entry *)match;
100052c9ce25SScott Long 	device->quirk = quirk;
100130a4094fSAlexander Motin 	if (quirk->quirks & CAM_QUIRK_MAXTAGS)
100230a4094fSAlexander Motin 		device->mintags = device->maxtags = quirk->maxtags;
100352c9ce25SScott Long }
100452c9ce25SScott Long 
100552c9ce25SScott Long typedef struct {
100652c9ce25SScott Long 	union	ccb *request_ccb;
100752c9ce25SScott Long 	struct 	ccb_pathinq *cpi;
100852c9ce25SScott Long 	int	counter;
100952c9ce25SScott Long } ata_scan_bus_info;
101052c9ce25SScott Long 
101152c9ce25SScott Long /*
101252c9ce25SScott Long  * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
101352c9ce25SScott Long  * As the scan progresses, xpt_scan_bus is used as the
101452c9ce25SScott Long  * callback on completion function.
101552c9ce25SScott Long  */
101652c9ce25SScott Long static void
101752c9ce25SScott Long ata_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
101852c9ce25SScott Long {
101952c9ce25SScott Long 	struct	cam_path *path;
102052c9ce25SScott Long 	ata_scan_bus_info *scan_info;
102183c5d981SAlexander Motin 	union	ccb *work_ccb, *reset_ccb;
102252c9ce25SScott Long 	cam_status status;
102352c9ce25SScott Long 
102452c9ce25SScott Long 	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
102552c9ce25SScott Long 		  ("xpt_scan_bus\n"));
102652c9ce25SScott Long 	switch (request_ccb->ccb_h.func_code) {
102752c9ce25SScott Long 	case XPT_SCAN_BUS:
102852c9ce25SScott Long 		/* Find out the characteristics of the bus */
102952c9ce25SScott Long 		work_ccb = xpt_alloc_ccb_nowait();
103052c9ce25SScott Long 		if (work_ccb == NULL) {
103152c9ce25SScott Long 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
103252c9ce25SScott Long 			xpt_done(request_ccb);
103352c9ce25SScott Long 			return;
103452c9ce25SScott Long 		}
103552c9ce25SScott Long 		xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
103652c9ce25SScott Long 			      request_ccb->ccb_h.pinfo.priority);
103752c9ce25SScott Long 		work_ccb->ccb_h.func_code = XPT_PATH_INQ;
103852c9ce25SScott Long 		xpt_action(work_ccb);
103952c9ce25SScott Long 		if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
104052c9ce25SScott Long 			request_ccb->ccb_h.status = work_ccb->ccb_h.status;
104152c9ce25SScott Long 			xpt_free_ccb(work_ccb);
104252c9ce25SScott Long 			xpt_done(request_ccb);
104352c9ce25SScott Long 			return;
104452c9ce25SScott Long 		}
104552c9ce25SScott Long 
104683c5d981SAlexander Motin 		/* We may need to reset bus first, if we haven't done it yet. */
104783c5d981SAlexander Motin 		if ((work_ccb->cpi.hba_inquiry &
104883c5d981SAlexander Motin 		    (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) &&
104983c5d981SAlexander Motin 		    !(work_ccb->cpi.hba_misc & PIM_NOBUSRESET) &&
105083c5d981SAlexander Motin 		    !timevalisset(&request_ccb->ccb_h.path->bus->last_reset)) {
105183c5d981SAlexander Motin 			reset_ccb = xpt_alloc_ccb_nowait();
105283c5d981SAlexander Motin 			xpt_setup_ccb(&reset_ccb->ccb_h, request_ccb->ccb_h.path,
105383c5d981SAlexander Motin 			      CAM_PRIORITY_NONE);
105483c5d981SAlexander Motin 			reset_ccb->ccb_h.func_code = XPT_RESET_BUS;
105583c5d981SAlexander Motin 			xpt_action(reset_ccb);
105683c5d981SAlexander Motin 			if (reset_ccb->ccb_h.status != CAM_REQ_CMP) {
105783c5d981SAlexander Motin 				request_ccb->ccb_h.status = reset_ccb->ccb_h.status;
105883c5d981SAlexander Motin 				xpt_free_ccb(reset_ccb);
105983c5d981SAlexander Motin 				xpt_free_ccb(work_ccb);
106083c5d981SAlexander Motin 				xpt_done(request_ccb);
106183c5d981SAlexander Motin 				return;
106283c5d981SAlexander Motin 			}
106383c5d981SAlexander Motin 			xpt_free_ccb(reset_ccb);
106483c5d981SAlexander Motin 		}
106583c5d981SAlexander Motin 
106652c9ce25SScott Long 		/* Save some state for use while we probe for devices */
106752c9ce25SScott Long 		scan_info = (ata_scan_bus_info *)
106852c9ce25SScott Long 		    malloc(sizeof(ata_scan_bus_info), M_CAMXPT, M_NOWAIT);
106952c9ce25SScott Long 		if (scan_info == NULL) {
107052c9ce25SScott Long 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
107152c9ce25SScott Long 			xpt_done(request_ccb);
107252c9ce25SScott Long 			return;
107352c9ce25SScott Long 		}
107452c9ce25SScott Long 		scan_info->request_ccb = request_ccb;
107552c9ce25SScott Long 		scan_info->cpi = &work_ccb->cpi;
107652c9ce25SScott Long 		/* If PM supported, probe it first. */
107752c9ce25SScott Long 		if (scan_info->cpi->hba_inquiry & PI_SATAPM)
10780025eb12SAlexander Motin 			scan_info->counter = scan_info->cpi->max_target;
10790025eb12SAlexander Motin 		else
10800025eb12SAlexander Motin 			scan_info->counter = 0;
108152c9ce25SScott Long 
108252c9ce25SScott Long 		work_ccb = xpt_alloc_ccb_nowait();
108352c9ce25SScott Long 		if (work_ccb == NULL) {
108452c9ce25SScott Long 			free(scan_info, M_CAMXPT);
108552c9ce25SScott Long 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
108652c9ce25SScott Long 			xpt_done(request_ccb);
108752c9ce25SScott Long 			break;
108852c9ce25SScott Long 		}
108952c9ce25SScott Long 		goto scan_next;
109052c9ce25SScott Long 	case XPT_SCAN_LUN:
109152c9ce25SScott Long 		work_ccb = request_ccb;
109252c9ce25SScott Long 		/* Reuse the same CCB to query if a device was really found */
109352c9ce25SScott Long 		scan_info = (ata_scan_bus_info *)work_ccb->ccb_h.ppriv_ptr0;
109452c9ce25SScott Long 		/* Free the current request path- we're done with it. */
109552c9ce25SScott Long 		xpt_free_path(work_ccb->ccb_h.path);
109665d0fb03SAlexander Motin 		/* If there is PMP... */
10970025eb12SAlexander Motin 		if ((scan_info->cpi->hba_inquiry & PI_SATAPM) &&
10980025eb12SAlexander Motin 		    (scan_info->counter == scan_info->cpi->max_target)) {
109983c5d981SAlexander Motin 			if (work_ccb->ccb_h.status == CAM_REQ_CMP) {
110065d0fb03SAlexander Motin 				/* everything else willbe probed by it */
11010025eb12SAlexander Motin 				goto done;
110252c9ce25SScott Long 			} else {
110352c9ce25SScott Long 				struct ccb_trans_settings cts;
110452c9ce25SScott Long 
110552c9ce25SScott Long 				/* Report SIM that PM is absent. */
110652c9ce25SScott Long 				bzero(&cts, sizeof(cts));
110752c9ce25SScott Long 				xpt_setup_ccb(&cts.ccb_h,
110852c9ce25SScott Long 				    scan_info->request_ccb->ccb_h.path, 1);
110952c9ce25SScott Long 				cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
111052c9ce25SScott Long 				cts.type = CTS_TYPE_CURRENT_SETTINGS;
11113ccda2f3SAlexander Motin 				cts.xport_specific.sata.pm_present = 0;
111252c9ce25SScott Long 				cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
111352c9ce25SScott Long 				xpt_action((union ccb *)&cts);
111452c9ce25SScott Long 			}
111552c9ce25SScott Long 		}
11160025eb12SAlexander Motin 		if (scan_info->counter ==
11170025eb12SAlexander Motin 		    ((scan_info->cpi->hba_inquiry & PI_SATAPM) ?
11180025eb12SAlexander Motin 		    0 : scan_info->cpi->max_target)) {
11190025eb12SAlexander Motin done:
112052c9ce25SScott Long 			xpt_free_ccb(work_ccb);
112152c9ce25SScott Long 			xpt_free_ccb((union ccb *)scan_info->cpi);
112252c9ce25SScott Long 			request_ccb = scan_info->request_ccb;
112352c9ce25SScott Long 			free(scan_info, M_CAMXPT);
112452c9ce25SScott Long 			request_ccb->ccb_h.status = CAM_REQ_CMP;
112552c9ce25SScott Long 			xpt_done(request_ccb);
112652c9ce25SScott Long 			break;
112752c9ce25SScott Long 		}
11280025eb12SAlexander Motin 		/* Take next device. Wrap from max (PMP) to 0. */
11290025eb12SAlexander Motin 		scan_info->counter = (scan_info->counter + 1 ) %
11300025eb12SAlexander Motin 		    (scan_info->cpi->max_target + 1);
113152c9ce25SScott Long scan_next:
113252c9ce25SScott Long 		status = xpt_create_path(&path, xpt_periph,
113352c9ce25SScott Long 		    scan_info->request_ccb->ccb_h.path_id,
113452c9ce25SScott Long 		    scan_info->counter, 0);
113552c9ce25SScott Long 		if (status != CAM_REQ_CMP) {
113652c9ce25SScott Long 			printf("xpt_scan_bus: xpt_create_path failed"
113752c9ce25SScott Long 			    " with status %#x, bus scan halted\n",
113852c9ce25SScott Long 			    status);
113952c9ce25SScott Long 			xpt_free_ccb(work_ccb);
114052c9ce25SScott Long 			xpt_free_ccb((union ccb *)scan_info->cpi);
114152c9ce25SScott Long 			request_ccb = scan_info->request_ccb;
114252c9ce25SScott Long 			free(scan_info, M_CAMXPT);
114352c9ce25SScott Long 			request_ccb->ccb_h.status = status;
114452c9ce25SScott Long 			xpt_done(request_ccb);
114552c9ce25SScott Long 			break;
114652c9ce25SScott Long 		}
114752c9ce25SScott Long 		xpt_setup_ccb(&work_ccb->ccb_h, path,
114852c9ce25SScott Long 		    scan_info->request_ccb->ccb_h.pinfo.priority);
114952c9ce25SScott Long 		work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
115052c9ce25SScott Long 		work_ccb->ccb_h.cbfcnp = ata_scan_bus;
115152c9ce25SScott Long 		work_ccb->ccb_h.ppriv_ptr0 = scan_info;
115252c9ce25SScott Long 		work_ccb->crcn.flags = scan_info->request_ccb->crcn.flags;
115352c9ce25SScott Long 		xpt_action(work_ccb);
115452c9ce25SScott Long 		break;
115552c9ce25SScott Long 	default:
115652c9ce25SScott Long 		break;
115752c9ce25SScott Long 	}
115852c9ce25SScott Long }
115952c9ce25SScott Long 
116052c9ce25SScott Long static void
116152c9ce25SScott Long ata_scan_lun(struct cam_periph *periph, struct cam_path *path,
116252c9ce25SScott Long 	     cam_flags flags, union ccb *request_ccb)
116352c9ce25SScott Long {
116452c9ce25SScott Long 	struct ccb_pathinq cpi;
116552c9ce25SScott Long 	cam_status status;
116652c9ce25SScott Long 	struct cam_path *new_path;
116752c9ce25SScott Long 	struct cam_periph *old_periph;
116852c9ce25SScott Long 
116983c5d981SAlexander Motin 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_scan_lun\n"));
117052c9ce25SScott Long 
117183c5d981SAlexander Motin 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
117252c9ce25SScott Long 	cpi.ccb_h.func_code = XPT_PATH_INQ;
117352c9ce25SScott Long 	xpt_action((union ccb *)&cpi);
117452c9ce25SScott Long 
117552c9ce25SScott Long 	if (cpi.ccb_h.status != CAM_REQ_CMP) {
117652c9ce25SScott Long 		if (request_ccb != NULL) {
117752c9ce25SScott Long 			request_ccb->ccb_h.status = cpi.ccb_h.status;
117852c9ce25SScott Long 			xpt_done(request_ccb);
117952c9ce25SScott Long 		}
118052c9ce25SScott Long 		return;
118152c9ce25SScott Long 	}
118252c9ce25SScott Long 
118352c9ce25SScott Long 	if (request_ccb == NULL) {
118452c9ce25SScott Long 		request_ccb = malloc(sizeof(union ccb), M_CAMXPT, M_NOWAIT);
118552c9ce25SScott Long 		if (request_ccb == NULL) {
118652c9ce25SScott Long 			xpt_print(path, "xpt_scan_lun: can't allocate CCB, "
118752c9ce25SScott Long 			    "can't continue\n");
118852c9ce25SScott Long 			return;
118952c9ce25SScott Long 		}
119052c9ce25SScott Long 		new_path = malloc(sizeof(*new_path), M_CAMXPT, M_NOWAIT);
119152c9ce25SScott Long 		if (new_path == NULL) {
119252c9ce25SScott Long 			xpt_print(path, "xpt_scan_lun: can't allocate path, "
119352c9ce25SScott Long 			    "can't continue\n");
119452c9ce25SScott Long 			free(request_ccb, M_CAMXPT);
119552c9ce25SScott Long 			return;
119652c9ce25SScott Long 		}
119752c9ce25SScott Long 		status = xpt_compile_path(new_path, xpt_periph,
119852c9ce25SScott Long 					  path->bus->path_id,
119952c9ce25SScott Long 					  path->target->target_id,
120052c9ce25SScott Long 					  path->device->lun_id);
120152c9ce25SScott Long 
120252c9ce25SScott Long 		if (status != CAM_REQ_CMP) {
120352c9ce25SScott Long 			xpt_print(path, "xpt_scan_lun: can't compile path, "
120452c9ce25SScott Long 			    "can't continue\n");
120552c9ce25SScott Long 			free(request_ccb, M_CAMXPT);
120652c9ce25SScott Long 			free(new_path, M_CAMXPT);
120752c9ce25SScott Long 			return;
120852c9ce25SScott Long 		}
120983c5d981SAlexander Motin 		xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_XPT);
121052c9ce25SScott Long 		request_ccb->ccb_h.cbfcnp = xptscandone;
121152c9ce25SScott Long 		request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
121252c9ce25SScott Long 		request_ccb->crcn.flags = flags;
121352c9ce25SScott Long 	}
121452c9ce25SScott Long 
1215f09f8e3eSAlexander Motin 	if ((old_periph = cam_periph_find(path, "aprobe")) != NULL) {
121652c9ce25SScott Long 		probe_softc *softc;
121752c9ce25SScott Long 
121852c9ce25SScott Long 		softc = (probe_softc *)old_periph->softc;
121952c9ce25SScott Long 		TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
122052c9ce25SScott Long 				  periph_links.tqe);
122183c5d981SAlexander Motin 		softc->restart = 1;
122252c9ce25SScott Long 	} else {
122352c9ce25SScott Long 		status = cam_periph_alloc(proberegister, NULL, probecleanup,
1224f09f8e3eSAlexander Motin 					  probestart, "aprobe",
122552c9ce25SScott Long 					  CAM_PERIPH_BIO,
122652c9ce25SScott Long 					  request_ccb->ccb_h.path, NULL, 0,
122752c9ce25SScott Long 					  request_ccb);
122852c9ce25SScott Long 
122952c9ce25SScott Long 		if (status != CAM_REQ_CMP) {
123052c9ce25SScott Long 			xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
123152c9ce25SScott Long 			    "returned an error, can't continue probe\n");
123252c9ce25SScott Long 			request_ccb->ccb_h.status = status;
123352c9ce25SScott Long 			xpt_done(request_ccb);
123452c9ce25SScott Long 		}
123552c9ce25SScott Long 	}
123652c9ce25SScott Long }
123752c9ce25SScott Long 
123852c9ce25SScott Long static void
123952c9ce25SScott Long xptscandone(struct cam_periph *periph, union ccb *done_ccb)
124052c9ce25SScott Long {
124152c9ce25SScott Long 	xpt_release_path(done_ccb->ccb_h.path);
124252c9ce25SScott Long 	free(done_ccb->ccb_h.path, M_CAMXPT);
124352c9ce25SScott Long 	free(done_ccb, M_CAMXPT);
124452c9ce25SScott Long }
124552c9ce25SScott Long 
124652c9ce25SScott Long static struct cam_ed *
124752c9ce25SScott Long ata_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
124852c9ce25SScott Long {
124952c9ce25SScott Long 	struct cam_path path;
125030a4094fSAlexander Motin 	struct ata_quirk_entry *quirk;
125152c9ce25SScott Long 	struct cam_ed *device;
125252c9ce25SScott Long 	struct cam_ed *cur_device;
125352c9ce25SScott Long 
125452c9ce25SScott Long 	device = xpt_alloc_device(bus, target, lun_id);
125552c9ce25SScott Long 	if (device == NULL)
125652c9ce25SScott Long 		return (NULL);
125752c9ce25SScott Long 
125852c9ce25SScott Long 	/*
125952c9ce25SScott Long 	 * Take the default quirk entry until we have inquiry
126052c9ce25SScott Long 	 * data and can determine a better quirk to use.
126152c9ce25SScott Long 	 */
126230a4094fSAlexander Motin 	quirk = &ata_quirk_table[ata_quirk_table_size - 1];
126352c9ce25SScott Long 	device->quirk = (void *)quirk;
126430a4094fSAlexander Motin 	device->mintags = 0;
126530a4094fSAlexander Motin 	device->maxtags = 0;
126652c9ce25SScott Long 	bzero(&device->inq_data, sizeof(device->inq_data));
126752c9ce25SScott Long 	device->inq_flags = 0;
126852c9ce25SScott Long 	device->queue_flags = 0;
126952c9ce25SScott Long 	device->serial_num = NULL;
127052c9ce25SScott Long 	device->serial_num_len = 0;
127152c9ce25SScott Long 
127252c9ce25SScott Long 	/*
127352c9ce25SScott Long 	 * XXX should be limited by number of CCBs this bus can
127452c9ce25SScott Long 	 * do.
127552c9ce25SScott Long 	 */
127652c9ce25SScott Long 	bus->sim->max_ccbs += device->ccbq.devq_openings;
127752c9ce25SScott Long 	/* Insertion sort into our target's device list */
127852c9ce25SScott Long 	cur_device = TAILQ_FIRST(&target->ed_entries);
127952c9ce25SScott Long 	while (cur_device != NULL && cur_device->lun_id < lun_id)
128052c9ce25SScott Long 		cur_device = TAILQ_NEXT(cur_device, links);
128152c9ce25SScott Long 	if (cur_device != NULL) {
128252c9ce25SScott Long 		TAILQ_INSERT_BEFORE(cur_device, device, links);
128352c9ce25SScott Long 	} else {
128452c9ce25SScott Long 		TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
128552c9ce25SScott Long 	}
128652c9ce25SScott Long 	target->generation++;
128752c9ce25SScott Long 	if (lun_id != CAM_LUN_WILDCARD) {
128852c9ce25SScott Long 		xpt_compile_path(&path,
128952c9ce25SScott Long 				 NULL,
129052c9ce25SScott Long 				 bus->path_id,
129152c9ce25SScott Long 				 target->target_id,
129252c9ce25SScott Long 				 lun_id);
129352c9ce25SScott Long 		ata_device_transport(&path);
129452c9ce25SScott Long 		xpt_release_path(&path);
129552c9ce25SScott Long 	}
129652c9ce25SScott Long 
129752c9ce25SScott Long 	return (device);
129852c9ce25SScott Long }
129952c9ce25SScott Long 
130052c9ce25SScott Long static void
130152c9ce25SScott Long ata_device_transport(struct cam_path *path)
130252c9ce25SScott Long {
130352c9ce25SScott Long 	struct ccb_pathinq cpi;
13044b997c49SAlexander Motin 	struct ccb_trans_settings cts;
13054b997c49SAlexander Motin 	struct scsi_inquiry_data *inq_buf = NULL;
13064b997c49SAlexander Motin 	struct ata_params *ident_buf = NULL;
130752c9ce25SScott Long 
130852c9ce25SScott Long 	/* Get transport information from the SIM */
130983c5d981SAlexander Motin 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
131052c9ce25SScott Long 	cpi.ccb_h.func_code = XPT_PATH_INQ;
131152c9ce25SScott Long 	xpt_action((union ccb *)&cpi);
131252c9ce25SScott Long 
131352c9ce25SScott Long 	path->device->transport = cpi.transport;
13144b997c49SAlexander Motin 	if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
13154b997c49SAlexander Motin 		inq_buf = &path->device->inq_data;
13164b997c49SAlexander Motin 	if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) != 0)
13174b997c49SAlexander Motin 		ident_buf = &path->device->ident_data;
13184b997c49SAlexander Motin 	if (path->device->protocol == PROTO_ATA) {
13194b997c49SAlexander Motin 		path->device->protocol_version = ident_buf ?
13204b997c49SAlexander Motin 		    ata_version(ident_buf->version_major) : cpi.protocol_version;
13214b997c49SAlexander Motin 	} else if (path->device->protocol == PROTO_SCSI) {
13224b997c49SAlexander Motin 		path->device->protocol_version = inq_buf ?
13234b997c49SAlexander Motin 		    SID_ANSI_REV(inq_buf) : cpi.protocol_version;
132452c9ce25SScott Long 	}
13254b997c49SAlexander Motin 	path->device->transport_version = ident_buf ?
13264b997c49SAlexander Motin 	    ata_version(ident_buf->version_major) : cpi.transport_version;
132752c9ce25SScott Long 
132852c9ce25SScott Long 	/* Tell the controller what we think */
132983c5d981SAlexander Motin 	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
133052c9ce25SScott Long 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
133152c9ce25SScott Long 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
133252c9ce25SScott Long 	cts.transport = path->device->transport;
133352c9ce25SScott Long 	cts.transport_version = path->device->transport_version;
133452c9ce25SScott Long 	cts.protocol = path->device->protocol;
133552c9ce25SScott Long 	cts.protocol_version = path->device->protocol_version;
133652c9ce25SScott Long 	cts.proto_specific.valid = 0;
13374cca1530SAlexander Motin 	if (ident_buf) {
13384cca1530SAlexander Motin 		if (path->device->transport == XPORT_ATA) {
13394cca1530SAlexander Motin 			cts.xport_specific.ata.atapi =
13404cca1530SAlexander Motin 			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 :
13414cca1530SAlexander Motin 			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0;
13424cca1530SAlexander Motin 			cts.xport_specific.ata.valid = CTS_ATA_VALID_ATAPI;
13434cca1530SAlexander Motin 		} else {
13444cca1530SAlexander Motin 			cts.xport_specific.sata.atapi =
13454cca1530SAlexander Motin 			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 :
13464cca1530SAlexander Motin 			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0;
13474cca1530SAlexander Motin 			cts.xport_specific.sata.valid = CTS_SATA_VALID_ATAPI;
13484cca1530SAlexander Motin 		}
13494cca1530SAlexander Motin 	} else
135052c9ce25SScott Long 		cts.xport_specific.valid = 0;
135152c9ce25SScott Long 	xpt_action((union ccb *)&cts);
135252c9ce25SScott Long }
135352c9ce25SScott Long 
135452c9ce25SScott Long static void
135552c9ce25SScott Long ata_action(union ccb *start_ccb)
135652c9ce25SScott Long {
135752c9ce25SScott Long 
135852c9ce25SScott Long 	switch (start_ccb->ccb_h.func_code) {
135952c9ce25SScott Long 	case XPT_SET_TRAN_SETTINGS:
136052c9ce25SScott Long 	{
136130a4094fSAlexander Motin 		ata_set_transfer_settings(&start_ccb->cts,
136252c9ce25SScott Long 					   start_ccb->ccb_h.path->device,
136352c9ce25SScott Long 					   /*async_update*/FALSE);
136452c9ce25SScott Long 		break;
136552c9ce25SScott Long 	}
136652c9ce25SScott Long 	case XPT_SCAN_BUS:
136752c9ce25SScott Long 		ata_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
136852c9ce25SScott Long 		break;
136952c9ce25SScott Long 	case XPT_SCAN_LUN:
137052c9ce25SScott Long 		ata_scan_lun(start_ccb->ccb_h.path->periph,
137152c9ce25SScott Long 			      start_ccb->ccb_h.path, start_ccb->crcn.flags,
137252c9ce25SScott Long 			      start_ccb);
137352c9ce25SScott Long 		break;
137452c9ce25SScott Long 	case XPT_GET_TRAN_SETTINGS:
137552c9ce25SScott Long 	{
137652c9ce25SScott Long 		struct cam_sim *sim;
137752c9ce25SScott Long 
137852c9ce25SScott Long 		sim = start_ccb->ccb_h.path->bus->sim;
137952c9ce25SScott Long 		(*(sim->sim_action))(sim, start_ccb);
138052c9ce25SScott Long 		break;
138152c9ce25SScott Long 	}
13824cca1530SAlexander Motin 	case XPT_SCSI_IO:
13834cca1530SAlexander Motin 	{
13844cca1530SAlexander Motin 		struct cam_ed *device;
13854cca1530SAlexander Motin 		u_int	maxlen = 0;
13864cca1530SAlexander Motin 
13874cca1530SAlexander Motin 		device = start_ccb->ccb_h.path->device;
13884cca1530SAlexander Motin 		if (device->protocol == PROTO_SCSI &&
13894cca1530SAlexander Motin 		    (device->flags & CAM_DEV_IDENTIFY_DATA_VALID)) {
13904cca1530SAlexander Motin 			uint16_t p =
13914cca1530SAlexander Motin 			    device->ident_data.config & ATA_PROTO_MASK;
13924cca1530SAlexander Motin 
13934cca1530SAlexander Motin 			maxlen = (p == ATA_PROTO_ATAPI_16) ? 16 :
13944cca1530SAlexander Motin 			    (p == ATA_PROTO_ATAPI_12) ? 12 : 0;
13954cca1530SAlexander Motin 		}
13964cca1530SAlexander Motin 		if (start_ccb->csio.cdb_len > maxlen) {
13974cca1530SAlexander Motin 			start_ccb->ccb_h.status = CAM_REQ_INVALID;
13984cca1530SAlexander Motin 			xpt_done(start_ccb);
13994cca1530SAlexander Motin 			break;
14004cca1530SAlexander Motin 		}
14014cca1530SAlexander Motin 		/* FALLTHROUGH */
14024cca1530SAlexander Motin 	}
140352c9ce25SScott Long 	default:
140452c9ce25SScott Long 		xpt_action_default(start_ccb);
140552c9ce25SScott Long 		break;
140652c9ce25SScott Long 	}
140752c9ce25SScott Long }
140852c9ce25SScott Long 
140952c9ce25SScott Long static void
141030a4094fSAlexander Motin ata_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
141152c9ce25SScott Long 			   int async_update)
141252c9ce25SScott Long {
141352c9ce25SScott Long 	struct	ccb_pathinq cpi;
141452c9ce25SScott Long 	struct	ccb_trans_settings cur_cts;
141552c9ce25SScott Long 	struct	ccb_trans_settings_scsi *scsi;
141652c9ce25SScott Long 	struct	ccb_trans_settings_scsi *cur_scsi;
141752c9ce25SScott Long 	struct	cam_sim *sim;
141852c9ce25SScott Long 	struct	scsi_inquiry_data *inq_data;
141952c9ce25SScott Long 
142052c9ce25SScott Long 	if (device == NULL) {
142152c9ce25SScott Long 		cts->ccb_h.status = CAM_PATH_INVALID;
142252c9ce25SScott Long 		xpt_done((union ccb *)cts);
142352c9ce25SScott Long 		return;
142452c9ce25SScott Long 	}
142552c9ce25SScott Long 
142652c9ce25SScott Long 	if (cts->protocol == PROTO_UNKNOWN
142752c9ce25SScott Long 	 || cts->protocol == PROTO_UNSPECIFIED) {
142852c9ce25SScott Long 		cts->protocol = device->protocol;
142952c9ce25SScott Long 		cts->protocol_version = device->protocol_version;
143052c9ce25SScott Long 	}
143152c9ce25SScott Long 
143252c9ce25SScott Long 	if (cts->protocol_version == PROTO_VERSION_UNKNOWN
143352c9ce25SScott Long 	 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
143452c9ce25SScott Long 		cts->protocol_version = device->protocol_version;
143552c9ce25SScott Long 
143652c9ce25SScott Long 	if (cts->protocol != device->protocol) {
143752c9ce25SScott Long 		xpt_print(cts->ccb_h.path, "Uninitialized Protocol %x:%x?\n",
143852c9ce25SScott Long 		       cts->protocol, device->protocol);
143952c9ce25SScott Long 		cts->protocol = device->protocol;
144052c9ce25SScott Long 	}
144152c9ce25SScott Long 
144252c9ce25SScott Long 	if (cts->protocol_version > device->protocol_version) {
144352c9ce25SScott Long 		if (bootverbose) {
144452c9ce25SScott Long 			xpt_print(cts->ccb_h.path, "Down reving Protocol "
144552c9ce25SScott Long 			    "Version from %d to %d?\n", cts->protocol_version,
144652c9ce25SScott Long 			    device->protocol_version);
144752c9ce25SScott Long 		}
144852c9ce25SScott Long 		cts->protocol_version = device->protocol_version;
144952c9ce25SScott Long 	}
145052c9ce25SScott Long 
145152c9ce25SScott Long 	if (cts->transport == XPORT_UNKNOWN
145252c9ce25SScott Long 	 || cts->transport == XPORT_UNSPECIFIED) {
145352c9ce25SScott Long 		cts->transport = device->transport;
145452c9ce25SScott Long 		cts->transport_version = device->transport_version;
145552c9ce25SScott Long 	}
145652c9ce25SScott Long 
145752c9ce25SScott Long 	if (cts->transport_version == XPORT_VERSION_UNKNOWN
145852c9ce25SScott Long 	 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
145952c9ce25SScott Long 		cts->transport_version = device->transport_version;
146052c9ce25SScott Long 
146152c9ce25SScott Long 	if (cts->transport != device->transport) {
146252c9ce25SScott Long 		xpt_print(cts->ccb_h.path, "Uninitialized Transport %x:%x?\n",
146352c9ce25SScott Long 		    cts->transport, device->transport);
146452c9ce25SScott Long 		cts->transport = device->transport;
146552c9ce25SScott Long 	}
146652c9ce25SScott Long 
146752c9ce25SScott Long 	if (cts->transport_version > device->transport_version) {
146852c9ce25SScott Long 		if (bootverbose) {
146952c9ce25SScott Long 			xpt_print(cts->ccb_h.path, "Down reving Transport "
147052c9ce25SScott Long 			    "Version from %d to %d?\n", cts->transport_version,
147152c9ce25SScott Long 			    device->transport_version);
147252c9ce25SScott Long 		}
147352c9ce25SScott Long 		cts->transport_version = device->transport_version;
147452c9ce25SScott Long 	}
147552c9ce25SScott Long 
147652c9ce25SScott Long 	sim = cts->ccb_h.path->bus->sim;
147752c9ce25SScott Long 
147852c9ce25SScott Long 	/*
147952c9ce25SScott Long 	 * Nothing more of interest to do unless
148052c9ce25SScott Long 	 * this is a device connected via the
148152c9ce25SScott Long 	 * SCSI protocol.
148252c9ce25SScott Long 	 */
148352c9ce25SScott Long 	if (cts->protocol != PROTO_SCSI) {
148452c9ce25SScott Long 		if (async_update == FALSE)
148552c9ce25SScott Long 			(*(sim->sim_action))(sim, (union ccb *)cts);
148652c9ce25SScott Long 		return;
148752c9ce25SScott Long 	}
148852c9ce25SScott Long 
148952c9ce25SScott Long 	inq_data = &device->inq_data;
149052c9ce25SScott Long 	scsi = &cts->proto_specific.scsi;
149183c5d981SAlexander Motin 	xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NONE);
149252c9ce25SScott Long 	cpi.ccb_h.func_code = XPT_PATH_INQ;
149352c9ce25SScott Long 	xpt_action((union ccb *)&cpi);
149452c9ce25SScott Long 
149552c9ce25SScott Long 	/* SCSI specific sanity checking */
149652c9ce25SScott Long 	if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
149752c9ce25SScott Long 	 || (INQ_DATA_TQ_ENABLED(inq_data)) == 0
149852c9ce25SScott Long 	 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
149952c9ce25SScott Long 	 || (device->mintags == 0)) {
150052c9ce25SScott Long 		/*
150152c9ce25SScott Long 		 * Can't tag on hardware that doesn't support tags,
150252c9ce25SScott Long 		 * doesn't have it enabled, or has broken tag support.
150352c9ce25SScott Long 		 */
150452c9ce25SScott Long 		scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
150552c9ce25SScott Long 	}
150652c9ce25SScott Long 
150752c9ce25SScott Long 	if (async_update == FALSE) {
150852c9ce25SScott Long 		/*
150952c9ce25SScott Long 		 * Perform sanity checking against what the
151052c9ce25SScott Long 		 * controller and device can do.
151152c9ce25SScott Long 		 */
151283c5d981SAlexander Motin 		xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NONE);
151352c9ce25SScott Long 		cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
151452c9ce25SScott Long 		cur_cts.type = cts->type;
151552c9ce25SScott Long 		xpt_action((union ccb *)&cur_cts);
151652c9ce25SScott Long 		if ((cur_cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
151752c9ce25SScott Long 			return;
151852c9ce25SScott Long 		}
151952c9ce25SScott Long 		cur_scsi = &cur_cts.proto_specific.scsi;
152052c9ce25SScott Long 		if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
152152c9ce25SScott Long 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
152252c9ce25SScott Long 			scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
152352c9ce25SScott Long 		}
152452c9ce25SScott Long 		if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
152552c9ce25SScott Long 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
152652c9ce25SScott Long 	}
152752c9ce25SScott Long 
152852c9ce25SScott Long 	if (cts->type == CTS_TYPE_CURRENT_SETTINGS
152952c9ce25SScott Long 	 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
153052c9ce25SScott Long 		int device_tagenb;
153152c9ce25SScott Long 
153252c9ce25SScott Long 		/*
153352c9ce25SScott Long 		 * If we are transitioning from tags to no-tags or
153452c9ce25SScott Long 		 * vice-versa, we need to carefully freeze and restart
153552c9ce25SScott Long 		 * the queue so that we don't overlap tagged and non-tagged
153652c9ce25SScott Long 		 * commands.  We also temporarily stop tags if there is
153752c9ce25SScott Long 		 * a change in transfer negotiation settings to allow
153852c9ce25SScott Long 		 * "tag-less" negotiation.
153952c9ce25SScott Long 		 */
154052c9ce25SScott Long 		if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
154152c9ce25SScott Long 		 || (device->inq_flags & SID_CmdQue) != 0)
154252c9ce25SScott Long 			device_tagenb = TRUE;
154352c9ce25SScott Long 		else
154452c9ce25SScott Long 			device_tagenb = FALSE;
154552c9ce25SScott Long 
154652c9ce25SScott Long 		if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
154752c9ce25SScott Long 		  && device_tagenb == FALSE)
154852c9ce25SScott Long 		 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
154952c9ce25SScott Long 		  && device_tagenb == TRUE)) {
155052c9ce25SScott Long 
155152c9ce25SScott Long 			if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
155252c9ce25SScott Long 				/*
155352c9ce25SScott Long 				 * Delay change to use tags until after a
155452c9ce25SScott Long 				 * few commands have gone to this device so
155552c9ce25SScott Long 				 * the controller has time to perform transfer
155652c9ce25SScott Long 				 * negotiations without tagged messages getting
155752c9ce25SScott Long 				 * in the way.
155852c9ce25SScott Long 				 */
155952c9ce25SScott Long 				device->tag_delay_count = CAM_TAG_DELAY_COUNT;
156052c9ce25SScott Long 				device->flags |= CAM_DEV_TAG_AFTER_COUNT;
156152c9ce25SScott Long 			} else {
156230a4094fSAlexander Motin 				xpt_stop_tags(cts->ccb_h.path);
156352c9ce25SScott Long 			}
156452c9ce25SScott Long 		}
156552c9ce25SScott Long 	}
156652c9ce25SScott Long 	if (async_update == FALSE)
156752c9ce25SScott Long 		(*(sim->sim_action))(sim, (union ccb *)cts);
156852c9ce25SScott Long }
156952c9ce25SScott Long 
157052c9ce25SScott Long /*
157152c9ce25SScott Long  * Handle any per-device event notifications that require action by the XPT.
157252c9ce25SScott Long  */
157352c9ce25SScott Long static void
157452c9ce25SScott Long ata_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
157552c9ce25SScott Long 	      struct cam_ed *device, void *async_arg)
157652c9ce25SScott Long {
157752c9ce25SScott Long 	cam_status status;
157852c9ce25SScott Long 	struct cam_path newpath;
157952c9ce25SScott Long 
158052c9ce25SScott Long 	/*
158152c9ce25SScott Long 	 * We only need to handle events for real devices.
158252c9ce25SScott Long 	 */
158352c9ce25SScott Long 	if (target->target_id == CAM_TARGET_WILDCARD
158452c9ce25SScott Long 	 || device->lun_id == CAM_LUN_WILDCARD)
158552c9ce25SScott Long 		return;
158652c9ce25SScott Long 
158752c9ce25SScott Long 	/*
158852c9ce25SScott Long 	 * We need our own path with wildcards expanded to
158952c9ce25SScott Long 	 * handle certain types of events.
159052c9ce25SScott Long 	 */
159152c9ce25SScott Long 	if ((async_code == AC_SENT_BDR)
159252c9ce25SScott Long 	 || (async_code == AC_BUS_RESET)
159352c9ce25SScott Long 	 || (async_code == AC_INQ_CHANGED))
159452c9ce25SScott Long 		status = xpt_compile_path(&newpath, NULL,
159552c9ce25SScott Long 					  bus->path_id,
159652c9ce25SScott Long 					  target->target_id,
159752c9ce25SScott Long 					  device->lun_id);
159852c9ce25SScott Long 	else
159952c9ce25SScott Long 		status = CAM_REQ_CMP_ERR;
160052c9ce25SScott Long 
160152c9ce25SScott Long 	if (status == CAM_REQ_CMP) {
160252c9ce25SScott Long 		if (async_code == AC_INQ_CHANGED) {
160352c9ce25SScott Long 			/*
160452c9ce25SScott Long 			 * We've sent a start unit command, or
160552c9ce25SScott Long 			 * something similar to a device that
160652c9ce25SScott Long 			 * may have caused its inquiry data to
160752c9ce25SScott Long 			 * change. So we re-scan the device to
160852c9ce25SScott Long 			 * refresh the inquiry data for it.
160952c9ce25SScott Long 			 */
161052c9ce25SScott Long 			ata_scan_lun(newpath.periph, &newpath,
161152c9ce25SScott Long 				     CAM_EXPECT_INQ_CHANGE, NULL);
161283c5d981SAlexander Motin 		} else {
161383c5d981SAlexander Motin 			/* We need to reinitialize device after reset. */
161483c5d981SAlexander Motin 			ata_scan_lun(newpath.periph, &newpath,
161583c5d981SAlexander Motin 				     0, NULL);
161652c9ce25SScott Long 		}
161752c9ce25SScott Long 		xpt_release_path(&newpath);
1618f98d7a47SAlexander Motin 	} else if (async_code == AC_LOST_DEVICE &&
1619f98d7a47SAlexander Motin 	    (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
162052c9ce25SScott Long 		device->flags |= CAM_DEV_UNCONFIGURED;
1621f98d7a47SAlexander Motin 		xpt_release_device(device);
162252c9ce25SScott Long 	} else if (async_code == AC_TRANSFER_NEG) {
162352c9ce25SScott Long 		struct ccb_trans_settings *settings;
162452c9ce25SScott Long 
162552c9ce25SScott Long 		settings = (struct ccb_trans_settings *)async_arg;
162630a4094fSAlexander Motin 		ata_set_transfer_settings(settings, device,
162752c9ce25SScott Long 					  /*async_update*/TRUE);
162852c9ce25SScott Long 	}
162952c9ce25SScott Long }
163052c9ce25SScott Long 
1631