xref: /freebsd/sys/cam/ata/ata_xpt.c (revision 1e637ba6)
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 
6952c9ce25SScott Long struct scsi_quirk_entry {
7052c9ce25SScott Long 	struct scsi_inquiry_pattern inq_pat;
7152c9ce25SScott Long 	u_int8_t quirks;
7252c9ce25SScott Long #define	CAM_QUIRK_NOLUNS	0x01
7352c9ce25SScott Long #define	CAM_QUIRK_NOSERIAL	0x02
7452c9ce25SScott Long #define	CAM_QUIRK_HILUNS	0x04
7552c9ce25SScott Long #define	CAM_QUIRK_NOHILUNS	0x08
7652c9ce25SScott Long 	u_int mintags;
7752c9ce25SScott Long 	u_int maxtags;
7852c9ce25SScott Long };
7952c9ce25SScott Long #define SCSI_QUIRK(dev)	((struct scsi_quirk_entry *)((dev)->quirk))
8052c9ce25SScott Long 
8152c9ce25SScott Long static periph_init_t probe_periph_init;
8252c9ce25SScott Long 
8352c9ce25SScott Long static struct periph_driver probe_driver =
8452c9ce25SScott Long {
85f09f8e3eSAlexander Motin 	probe_periph_init, "aprobe",
861e637ba6SAlexander Motin 	TAILQ_HEAD_INITIALIZER(probe_driver.units), /* generation */ 0,
871e637ba6SAlexander Motin 	CAM_PERIPH_DRV_EARLY
8852c9ce25SScott Long };
8952c9ce25SScott Long 
90f09f8e3eSAlexander Motin PERIPHDRIVER_DECLARE(aprobe, probe_driver);
9152c9ce25SScott Long 
9252c9ce25SScott Long typedef enum {
9352c9ce25SScott Long 	PROBE_RESET,
9452c9ce25SScott Long 	PROBE_IDENTIFY,
9552c9ce25SScott Long 	PROBE_SETMODE,
961e637ba6SAlexander Motin 	PROBE_SET_MULTI,
9752c9ce25SScott Long 	PROBE_INQUIRY,
9852c9ce25SScott Long 	PROBE_FULL_INQUIRY,
9952c9ce25SScott Long 	PROBE_PM_PID,
10052c9ce25SScott Long 	PROBE_PM_PRV,
10152c9ce25SScott Long 	PROBE_INVALID
10252c9ce25SScott Long } probe_action;
10352c9ce25SScott Long 
10452c9ce25SScott Long static char *probe_action_text[] = {
10552c9ce25SScott Long 	"PROBE_RESET",
10652c9ce25SScott Long 	"PROBE_IDENTIFY",
10752c9ce25SScott Long 	"PROBE_SETMODE",
1081e637ba6SAlexander Motin 	"PROBE_SET_MULTI",
10952c9ce25SScott Long 	"PROBE_INQUIRY",
11052c9ce25SScott Long 	"PROBE_FULL_INQUIRY",
11152c9ce25SScott Long 	"PROBE_PM_PID",
11252c9ce25SScott Long 	"PROBE_PM_PRV",
11352c9ce25SScott Long 	"PROBE_INVALID"
11452c9ce25SScott Long };
11552c9ce25SScott Long 
11652c9ce25SScott Long #define PROBE_SET_ACTION(softc, newaction)	\
11752c9ce25SScott Long do {									\
11852c9ce25SScott Long 	char **text;							\
11952c9ce25SScott Long 	text = probe_action_text;					\
12052c9ce25SScott Long 	CAM_DEBUG((softc)->periph->path, CAM_DEBUG_INFO,		\
12152c9ce25SScott Long 	    ("Probe %s to %s\n", text[(softc)->action],			\
12252c9ce25SScott Long 	    text[(newaction)]));					\
12352c9ce25SScott Long 	(softc)->action = (newaction);					\
12452c9ce25SScott Long } while(0)
12552c9ce25SScott Long 
12652c9ce25SScott Long typedef enum {
12752c9ce25SScott Long 	PROBE_NO_ANNOUNCE	= 0x04
12852c9ce25SScott Long } probe_flags;
12952c9ce25SScott Long 
13052c9ce25SScott Long typedef struct {
13152c9ce25SScott Long 	TAILQ_HEAD(, ccb_hdr) request_ccbs;
13252c9ce25SScott Long 	probe_action	action;
13352c9ce25SScott Long 	union ccb	saved_ccb;
13452c9ce25SScott Long 	probe_flags	flags;
13552c9ce25SScott Long 	u_int8_t	digest[16];
13652c9ce25SScott Long 	uint32_t	pm_pid;
13752c9ce25SScott Long 	uint32_t	pm_prv;
13852c9ce25SScott Long 	struct cam_periph *periph;
13952c9ce25SScott Long } probe_softc;
14052c9ce25SScott Long 
14152c9ce25SScott Long static struct scsi_quirk_entry scsi_quirk_table[] =
14252c9ce25SScott Long {
14352c9ce25SScott Long 	{
14452c9ce25SScott Long 		/* Default tagged queuing parameters for all devices */
14552c9ce25SScott Long 		{
14652c9ce25SScott Long 		  T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
14752c9ce25SScott Long 		  /*vendor*/"*", /*product*/"*", /*revision*/"*"
14852c9ce25SScott Long 		},
14952c9ce25SScott Long 		/*quirks*/0, /*mintags*/2, /*maxtags*/32
15052c9ce25SScott Long 	},
15152c9ce25SScott Long };
15252c9ce25SScott Long 
15352c9ce25SScott Long static const int scsi_quirk_table_size =
15452c9ce25SScott Long 	sizeof(scsi_quirk_table) / sizeof(*scsi_quirk_table);
15552c9ce25SScott Long 
15652c9ce25SScott Long static cam_status	proberegister(struct cam_periph *periph,
15752c9ce25SScott Long 				      void *arg);
15852c9ce25SScott Long static void	 probeschedule(struct cam_periph *probe_periph);
15952c9ce25SScott Long static void	 probestart(struct cam_periph *periph, union ccb *start_ccb);
16052c9ce25SScott Long //static void	 proberequestdefaultnegotiation(struct cam_periph *periph);
16152c9ce25SScott Long //static int       proberequestbackoff(struct cam_periph *periph,
16252c9ce25SScott Long //				     struct cam_ed *device);
16352c9ce25SScott Long static void	 probedone(struct cam_periph *periph, union ccb *done_ccb);
16452c9ce25SScott Long static void	 probecleanup(struct cam_periph *periph);
16552c9ce25SScott Long static void	 scsi_find_quirk(struct cam_ed *device);
16652c9ce25SScott Long static void	 ata_scan_bus(struct cam_periph *periph, union ccb *ccb);
16752c9ce25SScott Long static void	 ata_scan_lun(struct cam_periph *periph,
16852c9ce25SScott Long 			       struct cam_path *path, cam_flags flags,
16952c9ce25SScott Long 			       union ccb *ccb);
17052c9ce25SScott Long static void	 xptscandone(struct cam_periph *periph, union ccb *done_ccb);
17152c9ce25SScott Long static struct cam_ed *
17252c9ce25SScott Long 		 ata_alloc_device(struct cam_eb *bus, struct cam_et *target,
17352c9ce25SScott Long 				   lun_id_t lun_id);
17452c9ce25SScott Long static void	 ata_device_transport(struct cam_path *path);
17552c9ce25SScott Long static void	 scsi_set_transfer_settings(struct ccb_trans_settings *cts,
17652c9ce25SScott Long 					    struct cam_ed *device,
17752c9ce25SScott Long 					    int async_update);
17852c9ce25SScott Long static void	 scsi_toggle_tags(struct cam_path *path);
17952c9ce25SScott Long static void	 ata_dev_async(u_int32_t async_code,
18052c9ce25SScott Long 				struct cam_eb *bus,
18152c9ce25SScott Long 				struct cam_et *target,
18252c9ce25SScott Long 				struct cam_ed *device,
18352c9ce25SScott Long 				void *async_arg);
18452c9ce25SScott Long static void	 ata_action(union ccb *start_ccb);
18552c9ce25SScott Long 
18652c9ce25SScott Long static struct xpt_xport ata_xport = {
18752c9ce25SScott Long 	.alloc_device = ata_alloc_device,
18852c9ce25SScott Long 	.action = ata_action,
18952c9ce25SScott Long 	.async = ata_dev_async,
19052c9ce25SScott Long };
19152c9ce25SScott Long 
19252c9ce25SScott Long struct xpt_xport *
19352c9ce25SScott Long ata_get_xport(void)
19452c9ce25SScott Long {
19552c9ce25SScott Long 	return (&ata_xport);
19652c9ce25SScott Long }
19752c9ce25SScott Long 
19852c9ce25SScott Long static void
19952c9ce25SScott Long probe_periph_init()
20052c9ce25SScott Long {
20152c9ce25SScott Long }
20252c9ce25SScott Long 
20352c9ce25SScott Long static cam_status
20452c9ce25SScott Long proberegister(struct cam_periph *periph, void *arg)
20552c9ce25SScott Long {
20652c9ce25SScott Long 	union ccb *request_ccb;	/* CCB representing the probe request */
20752c9ce25SScott Long 	cam_status status;
20852c9ce25SScott Long 	probe_softc *softc;
20952c9ce25SScott Long 
21052c9ce25SScott Long 	request_ccb = (union ccb *)arg;
21152c9ce25SScott Long 	if (periph == NULL) {
21252c9ce25SScott Long 		printf("proberegister: periph was NULL!!\n");
21352c9ce25SScott Long 		return(CAM_REQ_CMP_ERR);
21452c9ce25SScott Long 	}
21552c9ce25SScott Long 
21652c9ce25SScott Long 	if (request_ccb == NULL) {
21752c9ce25SScott Long 		printf("proberegister: no probe CCB, "
21852c9ce25SScott Long 		       "can't register device\n");
21952c9ce25SScott Long 		return(CAM_REQ_CMP_ERR);
22052c9ce25SScott Long 	}
22152c9ce25SScott Long 
22252c9ce25SScott Long 	softc = (probe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_NOWAIT);
22352c9ce25SScott Long 
22452c9ce25SScott Long 	if (softc == NULL) {
22552c9ce25SScott Long 		printf("proberegister: Unable to probe new device. "
22652c9ce25SScott Long 		       "Unable to allocate softc\n");
22752c9ce25SScott Long 		return(CAM_REQ_CMP_ERR);
22852c9ce25SScott Long 	}
22952c9ce25SScott Long 	TAILQ_INIT(&softc->request_ccbs);
23052c9ce25SScott Long 	TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
23152c9ce25SScott Long 			  periph_links.tqe);
23252c9ce25SScott Long 	softc->flags = 0;
23352c9ce25SScott Long 	periph->softc = softc;
23452c9ce25SScott Long 	softc->periph = periph;
23552c9ce25SScott Long 	softc->action = PROBE_INVALID;
23652c9ce25SScott Long 	status = cam_periph_acquire(periph);
23752c9ce25SScott Long 	if (status != CAM_REQ_CMP) {
23852c9ce25SScott Long 		return (status);
23952c9ce25SScott Long 	}
24052c9ce25SScott Long 
24152c9ce25SScott Long 
24252c9ce25SScott Long 	/*
24352c9ce25SScott Long 	 * Ensure we've waited at least a bus settle
24452c9ce25SScott Long 	 * delay before attempting to probe the device.
24552c9ce25SScott Long 	 * For HBAs that don't do bus resets, this won't make a difference.
24652c9ce25SScott Long 	 */
24752c9ce25SScott Long 	cam_periph_freeze_after_event(periph, &periph->path->bus->last_reset,
24852c9ce25SScott Long 				      scsi_delay);
24952c9ce25SScott Long 	probeschedule(periph);
25052c9ce25SScott Long 	return(CAM_REQ_CMP);
25152c9ce25SScott Long }
25252c9ce25SScott Long 
25352c9ce25SScott Long static void
25452c9ce25SScott Long probeschedule(struct cam_periph *periph)
25552c9ce25SScott Long {
25652c9ce25SScott Long 	struct ccb_pathinq cpi;
25752c9ce25SScott Long 	union ccb *ccb;
25852c9ce25SScott Long 	probe_softc *softc;
25952c9ce25SScott Long 
26052c9ce25SScott Long 	softc = (probe_softc *)periph->softc;
26152c9ce25SScott Long 	ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
26252c9ce25SScott Long 
263bbfa4aa1SAlexander Motin 	xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
26452c9ce25SScott Long 	cpi.ccb_h.func_code = XPT_PATH_INQ;
26552c9ce25SScott Long 	xpt_action((union ccb *)&cpi);
26652c9ce25SScott Long 
26765d0fb03SAlexander Motin 	if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) ||
26865d0fb03SAlexander Motin 	    periph->path->device->protocol == PROTO_SATAPM)
26952c9ce25SScott Long 		PROBE_SET_ACTION(softc, PROBE_RESET);
27052c9ce25SScott Long 	else
27152c9ce25SScott Long 		PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
27252c9ce25SScott Long 
27352c9ce25SScott Long 	if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
27452c9ce25SScott Long 		softc->flags |= PROBE_NO_ANNOUNCE;
27552c9ce25SScott Long 	else
27652c9ce25SScott Long 		softc->flags &= ~PROBE_NO_ANNOUNCE;
27752c9ce25SScott Long 
27852c9ce25SScott Long 	xpt_schedule(periph, ccb->ccb_h.pinfo.priority);
27952c9ce25SScott Long }
28052c9ce25SScott Long 
28152c9ce25SScott Long static void
28252c9ce25SScott Long probestart(struct cam_periph *periph, union ccb *start_ccb)
28352c9ce25SScott Long {
28452c9ce25SScott Long 	/* Probe the device that our peripheral driver points to */
28552c9ce25SScott Long 	struct ccb_ataio *ataio;
28652c9ce25SScott Long 	struct ccb_scsiio *csio;
28752c9ce25SScott Long 	probe_softc *softc;
2881e637ba6SAlexander Motin 	struct cam_path *path;
2891e637ba6SAlexander Motin 	struct ata_params *ident_buf;
29052c9ce25SScott Long 
29152c9ce25SScott Long 	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
29252c9ce25SScott Long 
29352c9ce25SScott Long 	softc = (probe_softc *)periph->softc;
2941e637ba6SAlexander Motin 	path = start_ccb->ccb_h.path;
29552c9ce25SScott Long 	ataio = &start_ccb->ataio;
29652c9ce25SScott Long 	csio = &start_ccb->csio;
2971e637ba6SAlexander Motin 	ident_buf = &periph->path->device->ident_data;
29852c9ce25SScott Long 
29952c9ce25SScott Long 	switch (softc->action) {
30052c9ce25SScott Long 	case PROBE_RESET:
30152c9ce25SScott Long 		cam_fill_ataio(ataio,
30252c9ce25SScott Long 		      0,
30352c9ce25SScott Long 		      probedone,
30452c9ce25SScott Long 		      /*flags*/CAM_DIR_NONE,
30565d0fb03SAlexander Motin 		      0,
30652c9ce25SScott Long 		      /*data_ptr*/NULL,
30752c9ce25SScott Long 		      /*dxfer_len*/0,
30852c9ce25SScott Long 		      (start_ccb->ccb_h.target_id == 15 ? 3 : 15) * 1000);
30952c9ce25SScott Long 		ata_reset_cmd(ataio);
31052c9ce25SScott Long 		break;
31152c9ce25SScott Long 	case PROBE_IDENTIFY:
31252c9ce25SScott Long 		if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
31352c9ce25SScott Long 			/* Prepare check that it is the same device. */
31452c9ce25SScott Long 			MD5_CTX context;
31552c9ce25SScott Long 
31652c9ce25SScott Long 			MD5Init(&context);
31752c9ce25SScott Long 			MD5Update(&context,
31852c9ce25SScott Long 			    (unsigned char *)ident_buf->model,
31952c9ce25SScott Long 			    sizeof(ident_buf->model));
32052c9ce25SScott Long 			MD5Update(&context,
32152c9ce25SScott Long 			    (unsigned char *)ident_buf->revision,
32252c9ce25SScott Long 			    sizeof(ident_buf->revision));
32352c9ce25SScott Long 			MD5Update(&context,
32452c9ce25SScott Long 			    (unsigned char *)ident_buf->serial,
32552c9ce25SScott Long 			    sizeof(ident_buf->serial));
32652c9ce25SScott Long 			MD5Final(softc->digest, &context);
32752c9ce25SScott Long 		}
32852c9ce25SScott Long 		cam_fill_ataio(ataio,
32952c9ce25SScott Long 		      1,
33052c9ce25SScott Long 		      probedone,
33152c9ce25SScott Long 		      /*flags*/CAM_DIR_IN,
33265d0fb03SAlexander Motin 		      0,
33352c9ce25SScott Long 		      /*data_ptr*/(u_int8_t *)ident_buf,
33452c9ce25SScott Long 		      /*dxfer_len*/sizeof(struct ata_params),
33552c9ce25SScott Long 		      30 * 1000);
33652c9ce25SScott Long 		if (periph->path->device->protocol == PROTO_ATA)
3377606b445SAlexander Motin 			ata_28bit_cmd(ataio, ATA_ATA_IDENTIFY, 0, 0, 0);
33852c9ce25SScott Long 		else
3397606b445SAlexander Motin 			ata_28bit_cmd(ataio, ATA_ATAPI_IDENTIFY, 0, 0, 0);
34052c9ce25SScott Long 		break;
34152c9ce25SScott Long 	case PROBE_SETMODE:
34252c9ce25SScott Long 		cam_fill_ataio(ataio,
34352c9ce25SScott Long 		      1,
34452c9ce25SScott Long 		      probedone,
3455daa555dSAlexander Motin 		      /*flags*/CAM_DIR_NONE,
3465daa555dSAlexander Motin 		      0,
3475daa555dSAlexander Motin 		      /*data_ptr*/NULL,
3485daa555dSAlexander Motin 		      /*dxfer_len*/0,
34952c9ce25SScott Long 		      30 * 1000);
3507606b445SAlexander Motin 		ata_28bit_cmd(ataio, ATA_SETFEATURES, ATA_SF_SETXFER, 0,
35152c9ce25SScott Long 		    ata_max_mode(ident_buf, ATA_UDMA6, ATA_UDMA6));
35252c9ce25SScott Long 		break;
3531e637ba6SAlexander Motin 	case PROBE_SET_MULTI:
3541e637ba6SAlexander Motin 	{
3551e637ba6SAlexander Motin 		struct ccb_trans_settings cts;
3561e637ba6SAlexander Motin 		u_int sectors;
3571e637ba6SAlexander Motin 
3581e637ba6SAlexander Motin 		sectors = max(1, min(ident_buf->sectors_intr & 0xff, 16));
3591e637ba6SAlexander Motin 
3601e637ba6SAlexander Motin 		/* Report bytecount to SIM. */
3611e637ba6SAlexander Motin 		bzero(&cts, sizeof(cts));
3621e637ba6SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
3631e637ba6SAlexander Motin 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
3641e637ba6SAlexander Motin 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
3651e637ba6SAlexander Motin 		if (path->device->transport == XPORT_ATA) {
3661e637ba6SAlexander Motin 			cts.xport_specific.ata.bytecount = sectors * 512;
3671e637ba6SAlexander Motin 			cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT;
3681e637ba6SAlexander Motin 		} else {
3691e637ba6SAlexander Motin 			cts.xport_specific.sata.bytecount = sectors * 512;
3701e637ba6SAlexander Motin 			cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT;
3711e637ba6SAlexander Motin 		}
3721e637ba6SAlexander Motin 		xpt_action((union ccb *)&cts);
3731e637ba6SAlexander Motin 
3741e637ba6SAlexander Motin 		cam_fill_ataio(ataio,
3751e637ba6SAlexander Motin 		    1,
3761e637ba6SAlexander Motin 		    probedone,
3771e637ba6SAlexander Motin 		    CAM_DIR_NONE,
3781e637ba6SAlexander Motin 		    0,
3791e637ba6SAlexander Motin 		    NULL,
3801e637ba6SAlexander Motin 		    0,
3811e637ba6SAlexander Motin 		    30*1000);
3821e637ba6SAlexander Motin 		ata_28bit_cmd(ataio, ATA_SET_MULTI, 0, 0, sectors);
3831e637ba6SAlexander Motin 		break;
38452c9ce25SScott Long 	}
38552c9ce25SScott Long 	case PROBE_INQUIRY:
38652c9ce25SScott Long 	case PROBE_FULL_INQUIRY:
38752c9ce25SScott Long 	{
38852c9ce25SScott Long 		u_int inquiry_len;
38952c9ce25SScott Long 		struct scsi_inquiry_data *inq_buf =
39052c9ce25SScott Long 		    &periph->path->device->inq_data;
39152c9ce25SScott Long 
39252c9ce25SScott Long 		if (softc->action == PROBE_INQUIRY)
39352c9ce25SScott Long 			inquiry_len = SHORT_INQUIRY_LENGTH;
39452c9ce25SScott Long 		else
39552c9ce25SScott Long 			inquiry_len = SID_ADDITIONAL_LENGTH(inq_buf);
39652c9ce25SScott Long 		/*
39752c9ce25SScott Long 		 * Some parallel SCSI devices fail to send an
39852c9ce25SScott Long 		 * ignore wide residue message when dealing with
39952c9ce25SScott Long 		 * odd length inquiry requests.  Round up to be
40052c9ce25SScott Long 		 * safe.
40152c9ce25SScott Long 		 */
40252c9ce25SScott Long 		inquiry_len = roundup2(inquiry_len, 2);
40352c9ce25SScott Long 		scsi_inquiry(csio,
40452c9ce25SScott Long 			     /*retries*/1,
40552c9ce25SScott Long 			     probedone,
40652c9ce25SScott Long 			     MSG_SIMPLE_Q_TAG,
40752c9ce25SScott Long 			     (u_int8_t *)inq_buf,
40852c9ce25SScott Long 			     inquiry_len,
40952c9ce25SScott Long 			     /*evpd*/FALSE,
41052c9ce25SScott Long 			     /*page_code*/0,
41152c9ce25SScott Long 			     SSD_MIN_SIZE,
41252c9ce25SScott Long 			     /*timeout*/60 * 1000);
41352c9ce25SScott Long 		break;
41452c9ce25SScott Long 	}
41552c9ce25SScott Long 	case PROBE_PM_PID:
41652c9ce25SScott Long 		cam_fill_ataio(ataio,
41752c9ce25SScott Long 		      1,
41852c9ce25SScott Long 		      probedone,
41952c9ce25SScott Long 		      /*flags*/CAM_DIR_NONE,
42065d0fb03SAlexander Motin 		      0,
42152c9ce25SScott Long 		      /*data_ptr*/NULL,
42252c9ce25SScott Long 		      /*dxfer_len*/0,
42352c9ce25SScott Long 		      10 * 1000);
42452c9ce25SScott Long 		ata_pm_read_cmd(ataio, 0, 15);
42552c9ce25SScott Long 		break;
42652c9ce25SScott Long 	case PROBE_PM_PRV:
42752c9ce25SScott Long 		cam_fill_ataio(ataio,
42852c9ce25SScott Long 		      1,
42952c9ce25SScott Long 		      probedone,
43052c9ce25SScott Long 		      /*flags*/CAM_DIR_NONE,
43165d0fb03SAlexander Motin 		      0,
43252c9ce25SScott Long 		      /*data_ptr*/NULL,
43352c9ce25SScott Long 		      /*dxfer_len*/0,
43452c9ce25SScott Long 		      10 * 1000);
43552c9ce25SScott Long 		ata_pm_read_cmd(ataio, 1, 15);
43652c9ce25SScott Long 		break;
43752c9ce25SScott Long 	case PROBE_INVALID:
4381e637ba6SAlexander Motin 		CAM_DEBUG(path, CAM_DEBUG_INFO,
43952c9ce25SScott Long 		    ("probestart: invalid action state\n"));
44052c9ce25SScott Long 	default:
44152c9ce25SScott Long 		break;
44252c9ce25SScott Long 	}
44352c9ce25SScott Long 	xpt_action(start_ccb);
44452c9ce25SScott Long }
44552c9ce25SScott Long #if 0
44652c9ce25SScott Long static void
44752c9ce25SScott Long proberequestdefaultnegotiation(struct cam_periph *periph)
44852c9ce25SScott Long {
44952c9ce25SScott Long 	struct ccb_trans_settings cts;
45052c9ce25SScott Long 
451bbfa4aa1SAlexander Motin 	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
45252c9ce25SScott Long 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
45352c9ce25SScott Long 	cts.type = CTS_TYPE_USER_SETTINGS;
45452c9ce25SScott Long 	xpt_action((union ccb *)&cts);
45552c9ce25SScott Long 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
45652c9ce25SScott Long 		return;
45752c9ce25SScott Long 	}
45852c9ce25SScott Long 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
45952c9ce25SScott Long 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
46052c9ce25SScott Long 	xpt_action((union ccb *)&cts);
46152c9ce25SScott Long }
46252c9ce25SScott Long 
46352c9ce25SScott Long /*
46452c9ce25SScott Long  * Backoff Negotiation Code- only pertinent for SPI devices.
46552c9ce25SScott Long  */
46652c9ce25SScott Long static int
46752c9ce25SScott Long proberequestbackoff(struct cam_periph *periph, struct cam_ed *device)
46852c9ce25SScott Long {
46952c9ce25SScott Long 	struct ccb_trans_settings cts;
47052c9ce25SScott Long 	struct ccb_trans_settings_spi *spi;
47152c9ce25SScott Long 
47252c9ce25SScott Long 	memset(&cts, 0, sizeof (cts));
473bbfa4aa1SAlexander Motin 	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
47452c9ce25SScott Long 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
47552c9ce25SScott Long 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
47652c9ce25SScott Long 	xpt_action((union ccb *)&cts);
47752c9ce25SScott Long 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
47852c9ce25SScott Long 		if (bootverbose) {
47952c9ce25SScott Long 			xpt_print(periph->path,
48052c9ce25SScott Long 			    "failed to get current device settings\n");
48152c9ce25SScott Long 		}
48252c9ce25SScott Long 		return (0);
48352c9ce25SScott Long 	}
48452c9ce25SScott Long 	if (cts.transport != XPORT_SPI) {
48552c9ce25SScott Long 		if (bootverbose) {
48652c9ce25SScott Long 			xpt_print(periph->path, "not SPI transport\n");
48752c9ce25SScott Long 		}
48852c9ce25SScott Long 		return (0);
48952c9ce25SScott Long 	}
49052c9ce25SScott Long 	spi = &cts.xport_specific.spi;
49152c9ce25SScott Long 
49252c9ce25SScott Long 	/*
49352c9ce25SScott Long 	 * We cannot renegotiate sync rate if we don't have one.
49452c9ce25SScott Long 	 */
49552c9ce25SScott Long 	if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
49652c9ce25SScott Long 		if (bootverbose) {
49752c9ce25SScott Long 			xpt_print(periph->path, "no sync rate known\n");
49852c9ce25SScott Long 		}
49952c9ce25SScott Long 		return (0);
50052c9ce25SScott Long 	}
50152c9ce25SScott Long 
50252c9ce25SScott Long 	/*
50352c9ce25SScott Long 	 * We'll assert that we don't have to touch PPR options- the
50452c9ce25SScott Long 	 * SIM will see what we do with period and offset and adjust
50552c9ce25SScott Long 	 * the PPR options as appropriate.
50652c9ce25SScott Long 	 */
50752c9ce25SScott Long 
50852c9ce25SScott Long 	/*
50952c9ce25SScott Long 	 * A sync rate with unknown or zero offset is nonsensical.
51052c9ce25SScott Long 	 * A sync period of zero means Async.
51152c9ce25SScott Long 	 */
51252c9ce25SScott Long 	if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0
51352c9ce25SScott Long 	 || spi->sync_offset == 0 || spi->sync_period == 0) {
51452c9ce25SScott Long 		if (bootverbose) {
51552c9ce25SScott Long 			xpt_print(periph->path, "no sync rate available\n");
51652c9ce25SScott Long 		}
51752c9ce25SScott Long 		return (0);
51852c9ce25SScott Long 	}
51952c9ce25SScott Long 
52052c9ce25SScott Long 	if (device->flags & CAM_DEV_DV_HIT_BOTTOM) {
52152c9ce25SScott Long 		CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
52252c9ce25SScott Long 		    ("hit async: giving up on DV\n"));
52352c9ce25SScott Long 		return (0);
52452c9ce25SScott Long 	}
52552c9ce25SScott Long 
52652c9ce25SScott Long 
52752c9ce25SScott Long 	/*
52852c9ce25SScott Long 	 * Jump sync_period up by one, but stop at 5MHz and fall back to Async.
52952c9ce25SScott Long 	 * We don't try to remember 'last' settings to see if the SIM actually
53052c9ce25SScott Long 	 * gets into the speed we want to set. We check on the SIM telling
53152c9ce25SScott Long 	 * us that a requested speed is bad, but otherwise don't try and
53252c9ce25SScott Long 	 * check the speed due to the asynchronous and handshake nature
53352c9ce25SScott Long 	 * of speed setting.
53452c9ce25SScott Long 	 */
53552c9ce25SScott Long 	spi->valid = CTS_SPI_VALID_SYNC_RATE | CTS_SPI_VALID_SYNC_OFFSET;
53652c9ce25SScott Long 	for (;;) {
53752c9ce25SScott Long 		spi->sync_period++;
53852c9ce25SScott Long 		if (spi->sync_period >= 0xf) {
53952c9ce25SScott Long 			spi->sync_period = 0;
54052c9ce25SScott Long 			spi->sync_offset = 0;
54152c9ce25SScott Long 			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
54252c9ce25SScott Long 			    ("setting to async for DV\n"));
54352c9ce25SScott Long 			/*
54452c9ce25SScott Long 			 * Once we hit async, we don't want to try
54552c9ce25SScott Long 			 * any more settings.
54652c9ce25SScott Long 			 */
54752c9ce25SScott Long 			device->flags |= CAM_DEV_DV_HIT_BOTTOM;
54852c9ce25SScott Long 		} else if (bootverbose) {
54952c9ce25SScott Long 			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
55052c9ce25SScott Long 			    ("DV: period 0x%x\n", spi->sync_period));
55152c9ce25SScott Long 			printf("setting period to 0x%x\n", spi->sync_period);
55252c9ce25SScott Long 		}
55352c9ce25SScott Long 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
55452c9ce25SScott Long 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
55552c9ce25SScott Long 		xpt_action((union ccb *)&cts);
55652c9ce25SScott Long 		if ((cts.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
55752c9ce25SScott Long 			break;
55852c9ce25SScott Long 		}
55952c9ce25SScott Long 		CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
56052c9ce25SScott Long 		    ("DV: failed to set period 0x%x\n", spi->sync_period));
56152c9ce25SScott Long 		if (spi->sync_period == 0) {
56252c9ce25SScott Long 			return (0);
56352c9ce25SScott Long 		}
56452c9ce25SScott Long 	}
56552c9ce25SScott Long 	return (1);
56652c9ce25SScott Long }
56752c9ce25SScott Long #endif
56852c9ce25SScott Long static void
56952c9ce25SScott Long probedone(struct cam_periph *periph, union ccb *done_ccb)
57052c9ce25SScott Long {
57152c9ce25SScott Long 	struct ata_params *ident_buf;
57252c9ce25SScott Long 	probe_softc *softc;
57352c9ce25SScott Long 	struct cam_path *path;
57452c9ce25SScott Long 	u_int32_t  priority;
57565d0fb03SAlexander Motin 	int found = 1;
57652c9ce25SScott Long 
57752c9ce25SScott Long 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
57852c9ce25SScott Long 
57952c9ce25SScott Long 	softc = (probe_softc *)periph->softc;
58052c9ce25SScott Long 	path = done_ccb->ccb_h.path;
58152c9ce25SScott Long 	priority = done_ccb->ccb_h.pinfo.priority;
58252c9ce25SScott Long 	ident_buf = &path->device->ident_data;
58352c9ce25SScott Long 
5841e637ba6SAlexander Motin 	if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5851e637ba6SAlexander Motin device_fail:	if (cam_periph_error(done_ccb, 0, 0,
5861e637ba6SAlexander Motin 		    &softc->saved_ccb) == ERESTART) {
5871e637ba6SAlexander Motin 			return;
5881e637ba6SAlexander Motin 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5891e637ba6SAlexander Motin 			/* Don't wedge the queue */
5901e637ba6SAlexander Motin 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
5911e637ba6SAlexander Motin 					 /*run_queue*/TRUE);
5921e637ba6SAlexander Motin 		}
5931e637ba6SAlexander Motin 		/* Old PIO2 devices may not support mode setting. */
5941e637ba6SAlexander Motin 		if (softc->action == PROBE_SETMODE &&
5951e637ba6SAlexander Motin 		    ata_max_pmode(ident_buf) <= ATA_PIO2 &&
5961e637ba6SAlexander Motin 		    (ident_buf->capabilities1 & ATA_SUPPORT_IORDY) == 0)
5971e637ba6SAlexander Motin 			goto noerror;
5981e637ba6SAlexander Motin 		/*
5991e637ba6SAlexander Motin 		 * If we get to this point, we got an error status back
6001e637ba6SAlexander Motin 		 * from the inquiry and the error status doesn't require
6011e637ba6SAlexander Motin 		 * automatically retrying the command.  Therefore, the
6021e637ba6SAlexander Motin 		 * inquiry failed.  If we had inquiry information before
6031e637ba6SAlexander Motin 		 * for this device, but this latest inquiry command failed,
6041e637ba6SAlexander Motin 		 * the device has probably gone away.  If this device isn't
6051e637ba6SAlexander Motin 		 * already marked unconfigured, notify the peripheral
6061e637ba6SAlexander Motin 		 * drivers that this device is no more.
6071e637ba6SAlexander Motin 		 */
6081e637ba6SAlexander Motin 		if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
6091e637ba6SAlexander Motin 			xpt_async(AC_LOST_DEVICE, path, NULL);
6101e637ba6SAlexander Motin 		found = 0;
6111e637ba6SAlexander Motin 		goto done;
6121e637ba6SAlexander Motin 	}
6131e637ba6SAlexander Motin noerror:
61452c9ce25SScott Long 	switch (softc->action) {
61552c9ce25SScott Long 	case PROBE_RESET:
6161e637ba6SAlexander Motin 	{
61752c9ce25SScott Long 		int sign = (done_ccb->ataio.res.lba_high << 8) +
61852c9ce25SScott Long 		    done_ccb->ataio.res.lba_mid;
61952c9ce25SScott Long 		xpt_print(path, "SIGNATURE: %04x\n", sign);
62052c9ce25SScott Long 		if (sign == 0x0000 &&
62152c9ce25SScott Long 		    done_ccb->ccb_h.target_id != 15) {
62252c9ce25SScott Long 			path->device->protocol = PROTO_ATA;
62352c9ce25SScott Long 			PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
62452c9ce25SScott Long 		} else if (sign == 0x9669 &&
62552c9ce25SScott Long 		    done_ccb->ccb_h.target_id == 15) {
62652c9ce25SScott Long 			struct ccb_trans_settings cts;
62752c9ce25SScott Long 
62852c9ce25SScott Long 				/* Report SIM that PM is present. */
62952c9ce25SScott Long 			bzero(&cts, sizeof(cts));
630bbfa4aa1SAlexander Motin 			xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
63152c9ce25SScott Long 			cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
63252c9ce25SScott Long 			cts.type = CTS_TYPE_CURRENT_SETTINGS;
63352c9ce25SScott Long 			cts.xport_specific.sata.pm_present = 1;
63452c9ce25SScott Long 			cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
63552c9ce25SScott Long 			xpt_action((union ccb *)&cts);
63652c9ce25SScott Long 			path->device->protocol = PROTO_SATAPM;
63752c9ce25SScott Long 			PROBE_SET_ACTION(softc, PROBE_PM_PID);
63852c9ce25SScott Long 		} else if (sign == 0xeb14 &&
63952c9ce25SScott Long 		    done_ccb->ccb_h.target_id != 15) {
64052c9ce25SScott Long 			path->device->protocol = PROTO_SCSI;
64152c9ce25SScott Long 			PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
64252c9ce25SScott Long 		} else {
64352c9ce25SScott Long 			if (done_ccb->ccb_h.target_id != 15) {
64452c9ce25SScott Long 				xpt_print(path,
64552c9ce25SScott Long 				    "Unexpected signature 0x%04x\n", sign);
64652c9ce25SScott Long 			}
64765d0fb03SAlexander Motin 			goto device_fail;
64852c9ce25SScott Long 		}
64952c9ce25SScott Long 		xpt_release_ccb(done_ccb);
65052c9ce25SScott Long 		xpt_schedule(periph, priority);
65152c9ce25SScott Long 		return;
65252c9ce25SScott Long 	}
65352c9ce25SScott Long 	case PROBE_IDENTIFY:
65452c9ce25SScott Long 	{
65552c9ce25SScott Long 		int16_t *ptr;
65652c9ce25SScott Long 
65752c9ce25SScott Long 		for (ptr = (int16_t *)ident_buf;
65852c9ce25SScott Long 		     ptr < (int16_t *)ident_buf + sizeof(struct ata_params)/2; ptr++) {
65952c9ce25SScott Long 			*ptr = le16toh(*ptr);
66052c9ce25SScott Long 		}
66152c9ce25SScott Long 		if (strncmp(ident_buf->model, "FX", 2) &&
66252c9ce25SScott Long 		    strncmp(ident_buf->model, "NEC", 3) &&
66352c9ce25SScott Long 		    strncmp(ident_buf->model, "Pioneer", 7) &&
66452c9ce25SScott Long 		    strncmp(ident_buf->model, "SHARP", 5)) {
66552c9ce25SScott Long 			ata_bswap(ident_buf->model, sizeof(ident_buf->model));
66652c9ce25SScott Long 			ata_bswap(ident_buf->revision, sizeof(ident_buf->revision));
66752c9ce25SScott Long 			ata_bswap(ident_buf->serial, sizeof(ident_buf->serial));
66852c9ce25SScott Long 		}
66952c9ce25SScott Long 		ata_btrim(ident_buf->model, sizeof(ident_buf->model));
67052c9ce25SScott Long 		ata_bpack(ident_buf->model, ident_buf->model, sizeof(ident_buf->model));
67152c9ce25SScott Long 		ata_btrim(ident_buf->revision, sizeof(ident_buf->revision));
67252c9ce25SScott Long 		ata_bpack(ident_buf->revision, ident_buf->revision, sizeof(ident_buf->revision));
67352c9ce25SScott Long 		ata_btrim(ident_buf->serial, sizeof(ident_buf->serial));
67452c9ce25SScott Long 		ata_bpack(ident_buf->serial, ident_buf->serial, sizeof(ident_buf->serial));
67552c9ce25SScott Long 
67652c9ce25SScott Long 		if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
67752c9ce25SScott Long 			/* Check that it is the same device. */
67852c9ce25SScott Long 			MD5_CTX context;
67952c9ce25SScott Long 			u_int8_t digest[16];
68052c9ce25SScott Long 
68152c9ce25SScott Long 			MD5Init(&context);
68252c9ce25SScott Long 			MD5Update(&context,
68352c9ce25SScott Long 			    (unsigned char *)ident_buf->model,
68452c9ce25SScott Long 			    sizeof(ident_buf->model));
68552c9ce25SScott Long 			MD5Update(&context,
68652c9ce25SScott Long 			    (unsigned char *)ident_buf->revision,
68752c9ce25SScott Long 			    sizeof(ident_buf->revision));
68852c9ce25SScott Long 			MD5Update(&context,
68952c9ce25SScott Long 			    (unsigned char *)ident_buf->serial,
69052c9ce25SScott Long 			    sizeof(ident_buf->serial));
69152c9ce25SScott Long 			MD5Final(digest, &context);
69252c9ce25SScott Long 			if (bcmp(digest, softc->digest, sizeof(digest))) {
69352c9ce25SScott Long 				/* Device changed. */
69452c9ce25SScott Long 				xpt_async(AC_LOST_DEVICE, path, NULL);
69552c9ce25SScott Long 			}
6961e637ba6SAlexander Motin 		} else {
69752c9ce25SScott Long 			/* Clean up from previous instance of this device */
69852c9ce25SScott Long 			if (path->device->serial_num != NULL) {
69952c9ce25SScott Long 				free(path->device->serial_num, M_CAMXPT);
70052c9ce25SScott Long 				path->device->serial_num = NULL;
70152c9ce25SScott Long 				path->device->serial_num_len = 0;
70252c9ce25SScott Long 			}
70352c9ce25SScott Long 			path->device->serial_num =
70452c9ce25SScott Long 				(u_int8_t *)malloc((sizeof(ident_buf->serial) + 1),
70552c9ce25SScott Long 					   M_CAMXPT, M_NOWAIT);
70652c9ce25SScott Long 			if (path->device->serial_num != NULL) {
70752c9ce25SScott Long 				bcopy(ident_buf->serial,
70852c9ce25SScott Long 				      path->device->serial_num,
70952c9ce25SScott Long 				      sizeof(ident_buf->serial));
71052c9ce25SScott Long 				path->device->serial_num[sizeof(ident_buf->serial)]
71152c9ce25SScott Long 				    = '\0';
71252c9ce25SScott Long 				path->device->serial_num_len =
71352c9ce25SScott Long 				    strlen(path->device->serial_num);
71452c9ce25SScott Long 			}
71552c9ce25SScott Long 
7164b997c49SAlexander Motin 			path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
7171e637ba6SAlexander Motin 		}
71852c9ce25SScott Long 		ata_device_transport(path);
71952c9ce25SScott Long 		PROBE_SET_ACTION(softc, PROBE_SETMODE);
72052c9ce25SScott Long 		xpt_release_ccb(done_ccb);
72152c9ce25SScott Long 		xpt_schedule(periph, priority);
72252c9ce25SScott Long 		return;
72352c9ce25SScott Long 	}
72452c9ce25SScott Long 	case PROBE_SETMODE:
7251e637ba6SAlexander Motin 		if (path->device->protocol == PROTO_ATA) {
7261e637ba6SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_SET_MULTI);
7271e637ba6SAlexander Motin 		} else {
7281e637ba6SAlexander Motin 			PROBE_SET_ACTION(softc, PROBE_INQUIRY);
7291e637ba6SAlexander Motin 		}
7301e637ba6SAlexander Motin 		xpt_release_ccb(done_ccb);
7311e637ba6SAlexander Motin 		xpt_schedule(periph, priority);
7321e637ba6SAlexander Motin 		return;
7331e637ba6SAlexander Motin 	case PROBE_SET_MULTI:
7341e637ba6SAlexander Motin 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
73552c9ce25SScott Long 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
73652c9ce25SScott Long 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
73752c9ce25SScott Long 			xpt_action(done_ccb);
73852c9ce25SScott Long 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
73952c9ce25SScott Long 			    done_ccb);
7401e637ba6SAlexander Motin 		}
74152c9ce25SScott Long 		break;
74252c9ce25SScott Long 	case PROBE_INQUIRY:
74352c9ce25SScott Long 	case PROBE_FULL_INQUIRY:
74452c9ce25SScott Long 	{
74552c9ce25SScott Long 		struct scsi_inquiry_data *inq_buf;
7461e637ba6SAlexander Motin 		u_int8_t periph_qual, len;
74752c9ce25SScott Long 
74852c9ce25SScott Long 		path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
74952c9ce25SScott Long 		inq_buf = &path->device->inq_data;
75052c9ce25SScott Long 
75152c9ce25SScott Long 		periph_qual = SID_QUAL(inq_buf);
75252c9ce25SScott Long 
7531e637ba6SAlexander Motin 		if (periph_qual != SID_QUAL_LU_CONNECTED)
7541e637ba6SAlexander Motin 			break;
75552c9ce25SScott Long 
75652c9ce25SScott Long 		/*
75752c9ce25SScott Long 		 * We conservatively request only
75852c9ce25SScott Long 		 * SHORT_INQUIRY_LEN bytes of inquiry
75952c9ce25SScott Long 		 * information during our first try
76052c9ce25SScott Long 		 * at sending an INQUIRY. If the device
76152c9ce25SScott Long 		 * has more information to give,
76252c9ce25SScott Long 		 * perform a second request specifying
76352c9ce25SScott Long 		 * the amount of information the device
76452c9ce25SScott Long 		 * is willing to give.
76552c9ce25SScott Long 		 */
76652c9ce25SScott Long 		len = inq_buf->additional_length
7671e637ba6SAlexander Motin 		    + offsetof(struct scsi_inquiry_data, additional_length) + 1;
76852c9ce25SScott Long 		if (softc->action == PROBE_INQUIRY
76952c9ce25SScott Long 		    && len > SHORT_INQUIRY_LENGTH) {
77052c9ce25SScott Long 			PROBE_SET_ACTION(softc, PROBE_FULL_INQUIRY);
77152c9ce25SScott Long 			xpt_release_ccb(done_ccb);
77252c9ce25SScott Long 			xpt_schedule(periph, priority);
77352c9ce25SScott Long 			return;
77452c9ce25SScott Long 		}
77552c9ce25SScott Long 
77652c9ce25SScott Long 		scsi_find_quirk(path->device);
7774b997c49SAlexander Motin 		ata_device_transport(path);
7781e637ba6SAlexander Motin 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
77952c9ce25SScott Long 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
78052c9ce25SScott Long 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
78152c9ce25SScott Long 			xpt_action(done_ccb);
7821e637ba6SAlexander Motin 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path, done_ccb);
7831e637ba6SAlexander Motin 		}
78452c9ce25SScott Long 		break;
78552c9ce25SScott Long 	}
78652c9ce25SScott Long 	case PROBE_PM_PID:
7874b997c49SAlexander Motin 		if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) == 0)
78852c9ce25SScott Long 			bzero(ident_buf, sizeof(*ident_buf));
78952c9ce25SScott Long 		softc->pm_pid = (done_ccb->ataio.res.lba_high << 24) +
79052c9ce25SScott Long 		    (done_ccb->ataio.res.lba_mid << 16) +
79152c9ce25SScott Long 		    (done_ccb->ataio.res.lba_low << 8) +
79252c9ce25SScott Long 		    done_ccb->ataio.res.sector_count;
79365d0fb03SAlexander Motin 		((uint32_t *)ident_buf)[0] = softc->pm_pid;
79452c9ce25SScott Long 		printf("PM Product ID: %08x\n", softc->pm_pid);
79552c9ce25SScott Long 		snprintf(ident_buf->model, sizeof(ident_buf->model),
79652c9ce25SScott Long 		    "Port Multiplier %08x", softc->pm_pid);
79752c9ce25SScott Long 		PROBE_SET_ACTION(softc, PROBE_PM_PRV);
79852c9ce25SScott Long 		xpt_release_ccb(done_ccb);
79952c9ce25SScott Long 		xpt_schedule(periph, priority);
80052c9ce25SScott Long 		return;
80152c9ce25SScott Long 	case PROBE_PM_PRV:
80252c9ce25SScott Long 		softc->pm_prv = (done_ccb->ataio.res.lba_high << 24) +
80352c9ce25SScott Long 		    (done_ccb->ataio.res.lba_mid << 16) +
80452c9ce25SScott Long 		    (done_ccb->ataio.res.lba_low << 8) +
80552c9ce25SScott Long 		    done_ccb->ataio.res.sector_count;
80665d0fb03SAlexander Motin 		((uint32_t *)ident_buf)[1] = softc->pm_prv;
80752c9ce25SScott Long 		printf("PM Revision: %08x\n", softc->pm_prv);
80852c9ce25SScott Long 		snprintf(ident_buf->revision, sizeof(ident_buf->revision),
80952c9ce25SScott Long 		    "%04x", softc->pm_prv);
8104b997c49SAlexander Motin 		path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
81165d0fb03SAlexander Motin 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
81252c9ce25SScott Long 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
81352c9ce25SScott Long 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
81452c9ce25SScott Long 			xpt_action(done_ccb);
81552c9ce25SScott Long 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
81652c9ce25SScott Long 			    done_ccb);
81765d0fb03SAlexander Motin 		} else {
81865d0fb03SAlexander Motin 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
81965d0fb03SAlexander Motin 			xpt_action(done_ccb);
8201e637ba6SAlexander Motin 			xpt_async(AC_SCSI_AEN, done_ccb->ccb_h.path, done_ccb);
82152c9ce25SScott Long 		}
82252c9ce25SScott Long 		break;
82352c9ce25SScott Long 	case PROBE_INVALID:
82452c9ce25SScott Long 		CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_INFO,
82552c9ce25SScott Long 		    ("probedone: invalid action state\n"));
82652c9ce25SScott Long 	default:
82752c9ce25SScott Long 		break;
82852c9ce25SScott Long 	}
8291e637ba6SAlexander Motin done:
8301e637ba6SAlexander Motin 	xpt_release_ccb(done_ccb);
83152c9ce25SScott Long 	done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
83252c9ce25SScott Long 	TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe);
83352c9ce25SScott Long 	done_ccb->ccb_h.status = CAM_REQ_CMP;
83452c9ce25SScott Long 	done_ccb->ccb_h.ppriv_field1 = found;
83552c9ce25SScott Long 	xpt_done(done_ccb);
83652c9ce25SScott Long 	if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
83752c9ce25SScott Long 		cam_periph_invalidate(periph);
83852c9ce25SScott Long 		cam_periph_release_locked(periph);
83952c9ce25SScott Long 	} else {
84052c9ce25SScott Long 		probeschedule(periph);
84152c9ce25SScott Long 	}
84252c9ce25SScott Long }
84352c9ce25SScott Long 
84452c9ce25SScott Long static void
84552c9ce25SScott Long probecleanup(struct cam_periph *periph)
84652c9ce25SScott Long {
84752c9ce25SScott Long 	free(periph->softc, M_CAMXPT);
84852c9ce25SScott Long }
84952c9ce25SScott Long 
85052c9ce25SScott Long static void
85152c9ce25SScott Long scsi_find_quirk(struct cam_ed *device)
85252c9ce25SScott Long {
85352c9ce25SScott Long 	struct scsi_quirk_entry *quirk;
85452c9ce25SScott Long 	caddr_t	match;
85552c9ce25SScott Long 
85652c9ce25SScott Long 	match = cam_quirkmatch((caddr_t)&device->inq_data,
85752c9ce25SScott Long 			       (caddr_t)scsi_quirk_table,
85852c9ce25SScott Long 			       sizeof(scsi_quirk_table) /
85952c9ce25SScott Long 			       sizeof(*scsi_quirk_table),
86052c9ce25SScott Long 			       sizeof(*scsi_quirk_table), scsi_inquiry_match);
86152c9ce25SScott Long 
86252c9ce25SScott Long 	if (match == NULL)
86352c9ce25SScott Long 		panic("xpt_find_quirk: device didn't match wildcard entry!!");
86452c9ce25SScott Long 
86552c9ce25SScott Long 	quirk = (struct scsi_quirk_entry *)match;
86652c9ce25SScott Long 	device->quirk = quirk;
86752c9ce25SScott Long 	device->mintags = quirk->mintags;
86852c9ce25SScott Long 	device->maxtags = quirk->maxtags;
86952c9ce25SScott Long }
87052c9ce25SScott Long 
87152c9ce25SScott Long typedef struct {
87252c9ce25SScott Long 	union	ccb *request_ccb;
87352c9ce25SScott Long 	struct 	ccb_pathinq *cpi;
87452c9ce25SScott Long 	int	counter;
87552c9ce25SScott Long 	int	found;
87652c9ce25SScott Long } ata_scan_bus_info;
87752c9ce25SScott Long 
87852c9ce25SScott Long /*
87952c9ce25SScott Long  * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
88052c9ce25SScott Long  * As the scan progresses, xpt_scan_bus is used as the
88152c9ce25SScott Long  * callback on completion function.
88252c9ce25SScott Long  */
88352c9ce25SScott Long static void
88452c9ce25SScott Long ata_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
88552c9ce25SScott Long {
88652c9ce25SScott Long 	struct	cam_path *path;
88752c9ce25SScott Long 	ata_scan_bus_info *scan_info;
88852c9ce25SScott Long 	union	ccb *work_ccb;
88952c9ce25SScott Long 	cam_status status;
89052c9ce25SScott Long 
89152c9ce25SScott Long 	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
89252c9ce25SScott Long 		  ("xpt_scan_bus\n"));
89352c9ce25SScott Long 	switch (request_ccb->ccb_h.func_code) {
89452c9ce25SScott Long 	case XPT_SCAN_BUS:
89552c9ce25SScott Long 		/* Find out the characteristics of the bus */
89652c9ce25SScott Long 		work_ccb = xpt_alloc_ccb_nowait();
89752c9ce25SScott Long 		if (work_ccb == NULL) {
89852c9ce25SScott Long 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
89952c9ce25SScott Long 			xpt_done(request_ccb);
90052c9ce25SScott Long 			return;
90152c9ce25SScott Long 		}
90252c9ce25SScott Long 		xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
90352c9ce25SScott Long 			      request_ccb->ccb_h.pinfo.priority);
90452c9ce25SScott Long 		work_ccb->ccb_h.func_code = XPT_PATH_INQ;
90552c9ce25SScott Long 		xpt_action(work_ccb);
90652c9ce25SScott Long 		if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
90752c9ce25SScott Long 			request_ccb->ccb_h.status = work_ccb->ccb_h.status;
90852c9ce25SScott Long 			xpt_free_ccb(work_ccb);
90952c9ce25SScott Long 			xpt_done(request_ccb);
91052c9ce25SScott Long 			return;
91152c9ce25SScott Long 		}
91252c9ce25SScott Long 
91352c9ce25SScott Long 		/* Save some state for use while we probe for devices */
91452c9ce25SScott Long 		scan_info = (ata_scan_bus_info *)
91552c9ce25SScott Long 		    malloc(sizeof(ata_scan_bus_info), M_CAMXPT, M_NOWAIT);
91652c9ce25SScott Long 		if (scan_info == NULL) {
91752c9ce25SScott Long 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
91852c9ce25SScott Long 			xpt_done(request_ccb);
91952c9ce25SScott Long 			return;
92052c9ce25SScott Long 		}
92152c9ce25SScott Long 		scan_info->request_ccb = request_ccb;
92252c9ce25SScott Long 		scan_info->cpi = &work_ccb->cpi;
9234b997c49SAlexander Motin 		if (scan_info->cpi->transport == XPORT_ATA)
9244b997c49SAlexander Motin 			scan_info->found = 0x0003;
9254b997c49SAlexander Motin 		else
92652c9ce25SScott Long 			scan_info->found = 0x8001;
92752c9ce25SScott Long 		scan_info->counter = 0;
92852c9ce25SScott Long 		/* If PM supported, probe it first. */
92952c9ce25SScott Long 		if (scan_info->cpi->hba_inquiry & PI_SATAPM)
93052c9ce25SScott Long 			scan_info->counter = 15;
93152c9ce25SScott Long 
93252c9ce25SScott Long 		work_ccb = xpt_alloc_ccb_nowait();
93352c9ce25SScott Long 		if (work_ccb == NULL) {
93452c9ce25SScott Long 			free(scan_info, M_CAMXPT);
93552c9ce25SScott Long 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
93652c9ce25SScott Long 			xpt_done(request_ccb);
93752c9ce25SScott Long 			break;
93852c9ce25SScott Long 		}
93952c9ce25SScott Long 		goto scan_next;
94052c9ce25SScott Long 	case XPT_SCAN_LUN:
94152c9ce25SScott Long 		work_ccb = request_ccb;
94252c9ce25SScott Long 		/* Reuse the same CCB to query if a device was really found */
94352c9ce25SScott Long 		scan_info = (ata_scan_bus_info *)work_ccb->ccb_h.ppriv_ptr0;
94452c9ce25SScott Long 		/* Free the current request path- we're done with it. */
94552c9ce25SScott Long 		xpt_free_path(work_ccb->ccb_h.path);
94665d0fb03SAlexander Motin 		/* If there is PMP... */
94752c9ce25SScott Long 		if (scan_info->counter == 15) {
94852c9ce25SScott Long 			if (work_ccb->ccb_h.ppriv_field1 != 0) {
94965d0fb03SAlexander Motin 				/* everything else willbe probed by it */
95065d0fb03SAlexander Motin 				scan_info->found = 0x8000;
95152c9ce25SScott Long 			} else {
95252c9ce25SScott Long 				struct ccb_trans_settings cts;
95352c9ce25SScott Long 
95452c9ce25SScott Long 				/* Report SIM that PM is absent. */
95552c9ce25SScott Long 				bzero(&cts, sizeof(cts));
95652c9ce25SScott Long 				xpt_setup_ccb(&cts.ccb_h,
95752c9ce25SScott Long 				    scan_info->request_ccb->ccb_h.path, 1);
95852c9ce25SScott Long 				cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
95952c9ce25SScott Long 				cts.type = CTS_TYPE_CURRENT_SETTINGS;
9603ccda2f3SAlexander Motin 				cts.xport_specific.sata.pm_present = 0;
96152c9ce25SScott Long 				cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
96252c9ce25SScott Long 				xpt_action((union ccb *)&cts);
96352c9ce25SScott Long 			}
96452c9ce25SScott Long 		}
96552c9ce25SScott Long take_next:
96652c9ce25SScott Long 		/* Take next device. Wrap from 15 (PM) to 0. */
96752c9ce25SScott Long 		scan_info->counter = (scan_info->counter + 1 ) & 0x0f;
9688e7cccb3SAlexander Motin 		if (scan_info->counter > scan_info->cpi->max_target -
9698e7cccb3SAlexander Motin 		    ((scan_info->cpi->hba_inquiry & PI_SATAPM) ? 1 : 0)) {
97052c9ce25SScott Long 			xpt_free_ccb(work_ccb);
97152c9ce25SScott Long 			xpt_free_ccb((union ccb *)scan_info->cpi);
97252c9ce25SScott Long 			request_ccb = scan_info->request_ccb;
97352c9ce25SScott Long 			free(scan_info, M_CAMXPT);
97452c9ce25SScott Long 			request_ccb->ccb_h.status = CAM_REQ_CMP;
97552c9ce25SScott Long 			xpt_done(request_ccb);
97652c9ce25SScott Long 			break;
97752c9ce25SScott Long 		}
97852c9ce25SScott Long scan_next:
97965d0fb03SAlexander Motin 		if ((scan_info->found & (1 << scan_info->counter)) == 0)
98065d0fb03SAlexander Motin 			goto take_next;
98152c9ce25SScott Long 		status = xpt_create_path(&path, xpt_periph,
98252c9ce25SScott Long 		    scan_info->request_ccb->ccb_h.path_id,
98352c9ce25SScott Long 		    scan_info->counter, 0);
98452c9ce25SScott Long 		if (status != CAM_REQ_CMP) {
98552c9ce25SScott Long 			printf("xpt_scan_bus: xpt_create_path failed"
98652c9ce25SScott Long 			    " with status %#x, bus scan halted\n",
98752c9ce25SScott Long 			    status);
98852c9ce25SScott Long 			xpt_free_ccb(work_ccb);
98952c9ce25SScott Long 			xpt_free_ccb((union ccb *)scan_info->cpi);
99052c9ce25SScott Long 			request_ccb = scan_info->request_ccb;
99152c9ce25SScott Long 			free(scan_info, M_CAMXPT);
99252c9ce25SScott Long 			request_ccb->ccb_h.status = status;
99352c9ce25SScott Long 			xpt_done(request_ccb);
99452c9ce25SScott Long 			break;
99552c9ce25SScott Long 		}
99652c9ce25SScott Long 		xpt_setup_ccb(&work_ccb->ccb_h, path,
99752c9ce25SScott Long 		    scan_info->request_ccb->ccb_h.pinfo.priority);
99852c9ce25SScott Long 		work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
99952c9ce25SScott Long 		work_ccb->ccb_h.cbfcnp = ata_scan_bus;
100052c9ce25SScott Long 		work_ccb->ccb_h.ppriv_ptr0 = scan_info;
100152c9ce25SScott Long 		work_ccb->crcn.flags = scan_info->request_ccb->crcn.flags;
100252c9ce25SScott Long 		xpt_action(work_ccb);
100352c9ce25SScott Long 		break;
100452c9ce25SScott Long 	default:
100552c9ce25SScott Long 		break;
100652c9ce25SScott Long 	}
100752c9ce25SScott Long }
100852c9ce25SScott Long 
100952c9ce25SScott Long static void
101052c9ce25SScott Long ata_scan_lun(struct cam_periph *periph, struct cam_path *path,
101152c9ce25SScott Long 	     cam_flags flags, union ccb *request_ccb)
101252c9ce25SScott Long {
101352c9ce25SScott Long 	struct ccb_pathinq cpi;
101452c9ce25SScott Long 	cam_status status;
101552c9ce25SScott Long 	struct cam_path *new_path;
101652c9ce25SScott Long 	struct cam_periph *old_periph;
101752c9ce25SScott Long 
101852c9ce25SScott Long 	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
101952c9ce25SScott Long 		  ("xpt_scan_lun\n"));
102052c9ce25SScott Long 
1021bbfa4aa1SAlexander Motin 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
102252c9ce25SScott Long 	cpi.ccb_h.func_code = XPT_PATH_INQ;
102352c9ce25SScott Long 	xpt_action((union ccb *)&cpi);
102452c9ce25SScott Long 
102552c9ce25SScott Long 	if (cpi.ccb_h.status != CAM_REQ_CMP) {
102652c9ce25SScott Long 		if (request_ccb != NULL) {
102752c9ce25SScott Long 			request_ccb->ccb_h.status = cpi.ccb_h.status;
102852c9ce25SScott Long 			xpt_done(request_ccb);
102952c9ce25SScott Long 		}
103052c9ce25SScott Long 		return;
103152c9ce25SScott Long 	}
103252c9ce25SScott Long 
103352c9ce25SScott Long 	if (request_ccb == NULL) {
103452c9ce25SScott Long 		request_ccb = malloc(sizeof(union ccb), M_CAMXPT, M_NOWAIT);
103552c9ce25SScott Long 		if (request_ccb == NULL) {
103652c9ce25SScott Long 			xpt_print(path, "xpt_scan_lun: can't allocate CCB, "
103752c9ce25SScott Long 			    "can't continue\n");
103852c9ce25SScott Long 			return;
103952c9ce25SScott Long 		}
104052c9ce25SScott Long 		new_path = malloc(sizeof(*new_path), M_CAMXPT, M_NOWAIT);
104152c9ce25SScott Long 		if (new_path == NULL) {
104252c9ce25SScott Long 			xpt_print(path, "xpt_scan_lun: can't allocate path, "
104352c9ce25SScott Long 			    "can't continue\n");
104452c9ce25SScott Long 			free(request_ccb, M_CAMXPT);
104552c9ce25SScott Long 			return;
104652c9ce25SScott Long 		}
104752c9ce25SScott Long 		status = xpt_compile_path(new_path, xpt_periph,
104852c9ce25SScott Long 					  path->bus->path_id,
104952c9ce25SScott Long 					  path->target->target_id,
105052c9ce25SScott Long 					  path->device->lun_id);
105152c9ce25SScott Long 
105252c9ce25SScott Long 		if (status != CAM_REQ_CMP) {
105352c9ce25SScott Long 			xpt_print(path, "xpt_scan_lun: can't compile path, "
105452c9ce25SScott Long 			    "can't continue\n");
105552c9ce25SScott Long 			free(request_ccb, M_CAMXPT);
105652c9ce25SScott Long 			free(new_path, M_CAMXPT);
105752c9ce25SScott Long 			return;
105852c9ce25SScott Long 		}
1059bbfa4aa1SAlexander Motin 		xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_NORMAL);
106052c9ce25SScott Long 		request_ccb->ccb_h.cbfcnp = xptscandone;
106152c9ce25SScott Long 		request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
106252c9ce25SScott Long 		request_ccb->crcn.flags = flags;
106352c9ce25SScott Long 	}
106452c9ce25SScott Long 
1065f09f8e3eSAlexander Motin 	if ((old_periph = cam_periph_find(path, "aprobe")) != NULL) {
106652c9ce25SScott Long 		probe_softc *softc;
106752c9ce25SScott Long 
106852c9ce25SScott Long 		softc = (probe_softc *)old_periph->softc;
106952c9ce25SScott Long 		TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
107052c9ce25SScott Long 				  periph_links.tqe);
107152c9ce25SScott Long 	} else {
107252c9ce25SScott Long 		status = cam_periph_alloc(proberegister, NULL, probecleanup,
1073f09f8e3eSAlexander Motin 					  probestart, "aprobe",
107452c9ce25SScott Long 					  CAM_PERIPH_BIO,
107552c9ce25SScott Long 					  request_ccb->ccb_h.path, NULL, 0,
107652c9ce25SScott Long 					  request_ccb);
107752c9ce25SScott Long 
107852c9ce25SScott Long 		if (status != CAM_REQ_CMP) {
107952c9ce25SScott Long 			xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
108052c9ce25SScott Long 			    "returned an error, can't continue probe\n");
108152c9ce25SScott Long 			request_ccb->ccb_h.status = status;
108252c9ce25SScott Long 			xpt_done(request_ccb);
108352c9ce25SScott Long 		}
108452c9ce25SScott Long 	}
108552c9ce25SScott Long }
108652c9ce25SScott Long 
108752c9ce25SScott Long static void
108852c9ce25SScott Long xptscandone(struct cam_periph *periph, union ccb *done_ccb)
108952c9ce25SScott Long {
109052c9ce25SScott Long 	xpt_release_path(done_ccb->ccb_h.path);
109152c9ce25SScott Long 	free(done_ccb->ccb_h.path, M_CAMXPT);
109252c9ce25SScott Long 	free(done_ccb, M_CAMXPT);
109352c9ce25SScott Long }
109452c9ce25SScott Long 
109552c9ce25SScott Long static struct cam_ed *
109652c9ce25SScott Long ata_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
109752c9ce25SScott Long {
109852c9ce25SScott Long 	struct cam_path path;
109952c9ce25SScott Long 	struct scsi_quirk_entry *quirk;
110052c9ce25SScott Long 	struct cam_ed *device;
110152c9ce25SScott Long 	struct cam_ed *cur_device;
110252c9ce25SScott Long 
110352c9ce25SScott Long 	device = xpt_alloc_device(bus, target, lun_id);
110452c9ce25SScott Long 	if (device == NULL)
110552c9ce25SScott Long 		return (NULL);
110652c9ce25SScott Long 
110752c9ce25SScott Long 	/*
110852c9ce25SScott Long 	 * Take the default quirk entry until we have inquiry
110952c9ce25SScott Long 	 * data and can determine a better quirk to use.
111052c9ce25SScott Long 	 */
111152c9ce25SScott Long 	quirk = &scsi_quirk_table[scsi_quirk_table_size - 1];
111252c9ce25SScott Long 	device->quirk = (void *)quirk;
111352c9ce25SScott Long 	device->mintags = quirk->mintags;
111452c9ce25SScott Long 	device->maxtags = quirk->maxtags;
111552c9ce25SScott Long 	bzero(&device->inq_data, sizeof(device->inq_data));
111652c9ce25SScott Long 	device->inq_flags = 0;
111752c9ce25SScott Long 	device->queue_flags = 0;
111852c9ce25SScott Long 	device->serial_num = NULL;
111952c9ce25SScott Long 	device->serial_num_len = 0;
112052c9ce25SScott Long 
112152c9ce25SScott Long 	/*
112252c9ce25SScott Long 	 * XXX should be limited by number of CCBs this bus can
112352c9ce25SScott Long 	 * do.
112452c9ce25SScott Long 	 */
112552c9ce25SScott Long 	bus->sim->max_ccbs += device->ccbq.devq_openings;
112652c9ce25SScott Long 	/* Insertion sort into our target's device list */
112752c9ce25SScott Long 	cur_device = TAILQ_FIRST(&target->ed_entries);
112852c9ce25SScott Long 	while (cur_device != NULL && cur_device->lun_id < lun_id)
112952c9ce25SScott Long 		cur_device = TAILQ_NEXT(cur_device, links);
113052c9ce25SScott Long 	if (cur_device != NULL) {
113152c9ce25SScott Long 		TAILQ_INSERT_BEFORE(cur_device, device, links);
113252c9ce25SScott Long 	} else {
113352c9ce25SScott Long 		TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
113452c9ce25SScott Long 	}
113552c9ce25SScott Long 	target->generation++;
113652c9ce25SScott Long 	if (lun_id != CAM_LUN_WILDCARD) {
113752c9ce25SScott Long 		xpt_compile_path(&path,
113852c9ce25SScott Long 				 NULL,
113952c9ce25SScott Long 				 bus->path_id,
114052c9ce25SScott Long 				 target->target_id,
114152c9ce25SScott Long 				 lun_id);
114252c9ce25SScott Long 		ata_device_transport(&path);
114352c9ce25SScott Long 		xpt_release_path(&path);
114452c9ce25SScott Long 	}
114552c9ce25SScott Long 
114652c9ce25SScott Long 	return (device);
114752c9ce25SScott Long }
114852c9ce25SScott Long 
114952c9ce25SScott Long static void
115052c9ce25SScott Long ata_device_transport(struct cam_path *path)
115152c9ce25SScott Long {
115252c9ce25SScott Long 	struct ccb_pathinq cpi;
11534b997c49SAlexander Motin 	struct ccb_trans_settings cts;
11544b997c49SAlexander Motin 	struct scsi_inquiry_data *inq_buf = NULL;
11554b997c49SAlexander Motin 	struct ata_params *ident_buf = NULL;
115652c9ce25SScott Long 
115752c9ce25SScott Long 	/* Get transport information from the SIM */
1158bbfa4aa1SAlexander Motin 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
115952c9ce25SScott Long 	cpi.ccb_h.func_code = XPT_PATH_INQ;
116052c9ce25SScott Long 	xpt_action((union ccb *)&cpi);
116152c9ce25SScott Long 
116252c9ce25SScott Long 	path->device->transport = cpi.transport;
11634b997c49SAlexander Motin 	if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
11644b997c49SAlexander Motin 		inq_buf = &path->device->inq_data;
11654b997c49SAlexander Motin 	if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) != 0)
11664b997c49SAlexander Motin 		ident_buf = &path->device->ident_data;
11674b997c49SAlexander Motin 	if (path->device->protocol == PROTO_ATA) {
11684b997c49SAlexander Motin 		path->device->protocol_version = ident_buf ?
11694b997c49SAlexander Motin 		    ata_version(ident_buf->version_major) : cpi.protocol_version;
11704b997c49SAlexander Motin 	} else if (path->device->protocol == PROTO_SCSI) {
11714b997c49SAlexander Motin 		path->device->protocol_version = inq_buf ?
11724b997c49SAlexander Motin 		    SID_ANSI_REV(inq_buf) : cpi.protocol_version;
117352c9ce25SScott Long 	}
11744b997c49SAlexander Motin 	path->device->transport_version = ident_buf ?
11754b997c49SAlexander Motin 	    ata_version(ident_buf->version_major) : cpi.transport_version;
117652c9ce25SScott Long 
117752c9ce25SScott Long 	/* Tell the controller what we think */
1178bbfa4aa1SAlexander Motin 	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
117952c9ce25SScott Long 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
118052c9ce25SScott Long 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
118152c9ce25SScott Long 	cts.transport = path->device->transport;
118252c9ce25SScott Long 	cts.transport_version = path->device->transport_version;
118352c9ce25SScott Long 	cts.protocol = path->device->protocol;
118452c9ce25SScott Long 	cts.protocol_version = path->device->protocol_version;
118552c9ce25SScott Long 	cts.proto_specific.valid = 0;
118652c9ce25SScott Long 	cts.xport_specific.valid = 0;
118752c9ce25SScott Long 	xpt_action((union ccb *)&cts);
118852c9ce25SScott Long }
118952c9ce25SScott Long 
119052c9ce25SScott Long static void
119152c9ce25SScott Long ata_action(union ccb *start_ccb)
119252c9ce25SScott Long {
119352c9ce25SScott Long 
119452c9ce25SScott Long 	switch (start_ccb->ccb_h.func_code) {
119552c9ce25SScott Long 	case XPT_SET_TRAN_SETTINGS:
119652c9ce25SScott Long 	{
119752c9ce25SScott Long 		scsi_set_transfer_settings(&start_ccb->cts,
119852c9ce25SScott Long 					   start_ccb->ccb_h.path->device,
119952c9ce25SScott Long 					   /*async_update*/FALSE);
120052c9ce25SScott Long 		break;
120152c9ce25SScott Long 	}
120252c9ce25SScott Long 	case XPT_SCAN_BUS:
120352c9ce25SScott Long 		ata_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
120452c9ce25SScott Long 		break;
120552c9ce25SScott Long 	case XPT_SCAN_LUN:
120652c9ce25SScott Long 		ata_scan_lun(start_ccb->ccb_h.path->periph,
120752c9ce25SScott Long 			      start_ccb->ccb_h.path, start_ccb->crcn.flags,
120852c9ce25SScott Long 			      start_ccb);
120952c9ce25SScott Long 		break;
121052c9ce25SScott Long 	case XPT_GET_TRAN_SETTINGS:
121152c9ce25SScott Long 	{
121252c9ce25SScott Long 		struct cam_sim *sim;
121352c9ce25SScott Long 
121452c9ce25SScott Long 		sim = start_ccb->ccb_h.path->bus->sim;
121552c9ce25SScott Long 		(*(sim->sim_action))(sim, start_ccb);
121652c9ce25SScott Long 		break;
121752c9ce25SScott Long 	}
121852c9ce25SScott Long 	default:
121952c9ce25SScott Long 		xpt_action_default(start_ccb);
122052c9ce25SScott Long 		break;
122152c9ce25SScott Long 	}
122252c9ce25SScott Long }
122352c9ce25SScott Long 
122452c9ce25SScott Long static void
122552c9ce25SScott Long scsi_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
122652c9ce25SScott Long 			   int async_update)
122752c9ce25SScott Long {
122852c9ce25SScott Long 	struct	ccb_pathinq cpi;
122952c9ce25SScott Long 	struct	ccb_trans_settings cur_cts;
123052c9ce25SScott Long 	struct	ccb_trans_settings_scsi *scsi;
123152c9ce25SScott Long 	struct	ccb_trans_settings_scsi *cur_scsi;
123252c9ce25SScott Long 	struct	cam_sim *sim;
123352c9ce25SScott Long 	struct	scsi_inquiry_data *inq_data;
123452c9ce25SScott Long 
123552c9ce25SScott Long 	if (device == NULL) {
123652c9ce25SScott Long 		cts->ccb_h.status = CAM_PATH_INVALID;
123752c9ce25SScott Long 		xpt_done((union ccb *)cts);
123852c9ce25SScott Long 		return;
123952c9ce25SScott Long 	}
124052c9ce25SScott Long 
124152c9ce25SScott Long 	if (cts->protocol == PROTO_UNKNOWN
124252c9ce25SScott Long 	 || cts->protocol == PROTO_UNSPECIFIED) {
124352c9ce25SScott Long 		cts->protocol = device->protocol;
124452c9ce25SScott Long 		cts->protocol_version = device->protocol_version;
124552c9ce25SScott Long 	}
124652c9ce25SScott Long 
124752c9ce25SScott Long 	if (cts->protocol_version == PROTO_VERSION_UNKNOWN
124852c9ce25SScott Long 	 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
124952c9ce25SScott Long 		cts->protocol_version = device->protocol_version;
125052c9ce25SScott Long 
125152c9ce25SScott Long 	if (cts->protocol != device->protocol) {
125252c9ce25SScott Long 		xpt_print(cts->ccb_h.path, "Uninitialized Protocol %x:%x?\n",
125352c9ce25SScott Long 		       cts->protocol, device->protocol);
125452c9ce25SScott Long 		cts->protocol = device->protocol;
125552c9ce25SScott Long 	}
125652c9ce25SScott Long 
125752c9ce25SScott Long 	if (cts->protocol_version > device->protocol_version) {
125852c9ce25SScott Long 		if (bootverbose) {
125952c9ce25SScott Long 			xpt_print(cts->ccb_h.path, "Down reving Protocol "
126052c9ce25SScott Long 			    "Version from %d to %d?\n", cts->protocol_version,
126152c9ce25SScott Long 			    device->protocol_version);
126252c9ce25SScott Long 		}
126352c9ce25SScott Long 		cts->protocol_version = device->protocol_version;
126452c9ce25SScott Long 	}
126552c9ce25SScott Long 
126652c9ce25SScott Long 	if (cts->transport == XPORT_UNKNOWN
126752c9ce25SScott Long 	 || cts->transport == XPORT_UNSPECIFIED) {
126852c9ce25SScott Long 		cts->transport = device->transport;
126952c9ce25SScott Long 		cts->transport_version = device->transport_version;
127052c9ce25SScott Long 	}
127152c9ce25SScott Long 
127252c9ce25SScott Long 	if (cts->transport_version == XPORT_VERSION_UNKNOWN
127352c9ce25SScott Long 	 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
127452c9ce25SScott Long 		cts->transport_version = device->transport_version;
127552c9ce25SScott Long 
127652c9ce25SScott Long 	if (cts->transport != device->transport) {
127752c9ce25SScott Long 		xpt_print(cts->ccb_h.path, "Uninitialized Transport %x:%x?\n",
127852c9ce25SScott Long 		    cts->transport, device->transport);
127952c9ce25SScott Long 		cts->transport = device->transport;
128052c9ce25SScott Long 	}
128152c9ce25SScott Long 
128252c9ce25SScott Long 	if (cts->transport_version > device->transport_version) {
128352c9ce25SScott Long 		if (bootverbose) {
128452c9ce25SScott Long 			xpt_print(cts->ccb_h.path, "Down reving Transport "
128552c9ce25SScott Long 			    "Version from %d to %d?\n", cts->transport_version,
128652c9ce25SScott Long 			    device->transport_version);
128752c9ce25SScott Long 		}
128852c9ce25SScott Long 		cts->transport_version = device->transport_version;
128952c9ce25SScott Long 	}
129052c9ce25SScott Long 
129152c9ce25SScott Long 	sim = cts->ccb_h.path->bus->sim;
129252c9ce25SScott Long 
129352c9ce25SScott Long 	/*
129452c9ce25SScott Long 	 * Nothing more of interest to do unless
129552c9ce25SScott Long 	 * this is a device connected via the
129652c9ce25SScott Long 	 * SCSI protocol.
129752c9ce25SScott Long 	 */
129852c9ce25SScott Long 	if (cts->protocol != PROTO_SCSI) {
129952c9ce25SScott Long 		if (async_update == FALSE)
130052c9ce25SScott Long 			(*(sim->sim_action))(sim, (union ccb *)cts);
130152c9ce25SScott Long 		return;
130252c9ce25SScott Long 	}
130352c9ce25SScott Long 
130452c9ce25SScott Long 	inq_data = &device->inq_data;
130552c9ce25SScott Long 	scsi = &cts->proto_specific.scsi;
1306bbfa4aa1SAlexander Motin 	xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NORMAL);
130752c9ce25SScott Long 	cpi.ccb_h.func_code = XPT_PATH_INQ;
130852c9ce25SScott Long 	xpt_action((union ccb *)&cpi);
130952c9ce25SScott Long 
131052c9ce25SScott Long 	/* SCSI specific sanity checking */
131152c9ce25SScott Long 	if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
131252c9ce25SScott Long 	 || (INQ_DATA_TQ_ENABLED(inq_data)) == 0
131352c9ce25SScott Long 	 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
131452c9ce25SScott Long 	 || (device->mintags == 0)) {
131552c9ce25SScott Long 		/*
131652c9ce25SScott Long 		 * Can't tag on hardware that doesn't support tags,
131752c9ce25SScott Long 		 * doesn't have it enabled, or has broken tag support.
131852c9ce25SScott Long 		 */
131952c9ce25SScott Long 		scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
132052c9ce25SScott Long 	}
132152c9ce25SScott Long 
132252c9ce25SScott Long 	if (async_update == FALSE) {
132352c9ce25SScott Long 		/*
132452c9ce25SScott Long 		 * Perform sanity checking against what the
132552c9ce25SScott Long 		 * controller and device can do.
132652c9ce25SScott Long 		 */
1327bbfa4aa1SAlexander Motin 		xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NORMAL);
132852c9ce25SScott Long 		cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
132952c9ce25SScott Long 		cur_cts.type = cts->type;
133052c9ce25SScott Long 		xpt_action((union ccb *)&cur_cts);
133152c9ce25SScott Long 		if ((cur_cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
133252c9ce25SScott Long 			return;
133352c9ce25SScott Long 		}
133452c9ce25SScott Long 		cur_scsi = &cur_cts.proto_specific.scsi;
133552c9ce25SScott Long 		if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
133652c9ce25SScott Long 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
133752c9ce25SScott Long 			scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
133852c9ce25SScott Long 		}
133952c9ce25SScott Long 		if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
134052c9ce25SScott Long 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
134152c9ce25SScott Long 	}
134252c9ce25SScott Long 
134352c9ce25SScott Long 	if (cts->type == CTS_TYPE_CURRENT_SETTINGS
134452c9ce25SScott Long 	 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
134552c9ce25SScott Long 		int device_tagenb;
134652c9ce25SScott Long 
134752c9ce25SScott Long 		/*
134852c9ce25SScott Long 		 * If we are transitioning from tags to no-tags or
134952c9ce25SScott Long 		 * vice-versa, we need to carefully freeze and restart
135052c9ce25SScott Long 		 * the queue so that we don't overlap tagged and non-tagged
135152c9ce25SScott Long 		 * commands.  We also temporarily stop tags if there is
135252c9ce25SScott Long 		 * a change in transfer negotiation settings to allow
135352c9ce25SScott Long 		 * "tag-less" negotiation.
135452c9ce25SScott Long 		 */
135552c9ce25SScott Long 		if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
135652c9ce25SScott Long 		 || (device->inq_flags & SID_CmdQue) != 0)
135752c9ce25SScott Long 			device_tagenb = TRUE;
135852c9ce25SScott Long 		else
135952c9ce25SScott Long 			device_tagenb = FALSE;
136052c9ce25SScott Long 
136152c9ce25SScott Long 		if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
136252c9ce25SScott Long 		  && device_tagenb == FALSE)
136352c9ce25SScott Long 		 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
136452c9ce25SScott Long 		  && device_tagenb == TRUE)) {
136552c9ce25SScott Long 
136652c9ce25SScott Long 			if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
136752c9ce25SScott Long 				/*
136852c9ce25SScott Long 				 * Delay change to use tags until after a
136952c9ce25SScott Long 				 * few commands have gone to this device so
137052c9ce25SScott Long 				 * the controller has time to perform transfer
137152c9ce25SScott Long 				 * negotiations without tagged messages getting
137252c9ce25SScott Long 				 * in the way.
137352c9ce25SScott Long 				 */
137452c9ce25SScott Long 				device->tag_delay_count = CAM_TAG_DELAY_COUNT;
137552c9ce25SScott Long 				device->flags |= CAM_DEV_TAG_AFTER_COUNT;
137652c9ce25SScott Long 			} else {
137752c9ce25SScott Long 				struct ccb_relsim crs;
137852c9ce25SScott Long 
137952c9ce25SScott Long 				xpt_freeze_devq(cts->ccb_h.path, /*count*/1);
138052c9ce25SScott Long 		  		device->inq_flags &= ~SID_CmdQue;
138152c9ce25SScott Long 				xpt_dev_ccbq_resize(cts->ccb_h.path,
138252c9ce25SScott Long 						    sim->max_dev_openings);
138352c9ce25SScott Long 				device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
138452c9ce25SScott Long 				device->tag_delay_count = 0;
138552c9ce25SScott Long 
138652c9ce25SScott Long 				xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path,
1387bbfa4aa1SAlexander Motin 				    CAM_PRIORITY_NORMAL);
138852c9ce25SScott Long 				crs.ccb_h.func_code = XPT_REL_SIMQ;
138952c9ce25SScott Long 				crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
139052c9ce25SScott Long 				crs.openings
139152c9ce25SScott Long 				    = crs.release_timeout
139252c9ce25SScott Long 				    = crs.qfrozen_cnt
139352c9ce25SScott Long 				    = 0;
139452c9ce25SScott Long 				xpt_action((union ccb *)&crs);
139552c9ce25SScott Long 			}
139652c9ce25SScott Long 		}
139752c9ce25SScott Long 	}
139852c9ce25SScott Long 	if (async_update == FALSE)
139952c9ce25SScott Long 		(*(sim->sim_action))(sim, (union ccb *)cts);
140052c9ce25SScott Long }
140152c9ce25SScott Long 
140252c9ce25SScott Long static void
140352c9ce25SScott Long scsi_toggle_tags(struct cam_path *path)
140452c9ce25SScott Long {
140552c9ce25SScott Long 	struct cam_ed *dev;
140652c9ce25SScott Long 
140752c9ce25SScott Long 	/*
140852c9ce25SScott Long 	 * Give controllers a chance to renegotiate
140952c9ce25SScott Long 	 * before starting tag operations.  We
141052c9ce25SScott Long 	 * "toggle" tagged queuing off then on
141152c9ce25SScott Long 	 * which causes the tag enable command delay
141252c9ce25SScott Long 	 * counter to come into effect.
141352c9ce25SScott Long 	 */
141452c9ce25SScott Long 	dev = path->device;
141552c9ce25SScott Long 	if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
141652c9ce25SScott Long 	 || ((dev->inq_flags & SID_CmdQue) != 0
141752c9ce25SScott Long  	  && (dev->inq_flags & (SID_Sync|SID_WBus16|SID_WBus32)) != 0)) {
141852c9ce25SScott Long 		struct ccb_trans_settings cts;
141952c9ce25SScott Long 
1420bbfa4aa1SAlexander Motin 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
142152c9ce25SScott Long 		cts.protocol = PROTO_SCSI;
142252c9ce25SScott Long 		cts.protocol_version = PROTO_VERSION_UNSPECIFIED;
142352c9ce25SScott Long 		cts.transport = XPORT_UNSPECIFIED;
142452c9ce25SScott Long 		cts.transport_version = XPORT_VERSION_UNSPECIFIED;
142552c9ce25SScott Long 		cts.proto_specific.scsi.flags = 0;
142652c9ce25SScott Long 		cts.proto_specific.scsi.valid = CTS_SCSI_VALID_TQ;
142752c9ce25SScott Long 		scsi_set_transfer_settings(&cts, path->device,
142852c9ce25SScott Long 					  /*async_update*/TRUE);
142952c9ce25SScott Long 		cts.proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB;
143052c9ce25SScott Long 		scsi_set_transfer_settings(&cts, path->device,
143152c9ce25SScott Long 					  /*async_update*/TRUE);
143252c9ce25SScott Long 	}
143352c9ce25SScott Long }
143452c9ce25SScott Long 
143552c9ce25SScott Long /*
143652c9ce25SScott Long  * Handle any per-device event notifications that require action by the XPT.
143752c9ce25SScott Long  */
143852c9ce25SScott Long static void
143952c9ce25SScott Long ata_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
144052c9ce25SScott Long 	      struct cam_ed *device, void *async_arg)
144152c9ce25SScott Long {
144252c9ce25SScott Long 	cam_status status;
144352c9ce25SScott Long 	struct cam_path newpath;
144452c9ce25SScott Long 
144552c9ce25SScott Long 	/*
144652c9ce25SScott Long 	 * We only need to handle events for real devices.
144752c9ce25SScott Long 	 */
144852c9ce25SScott Long 	if (target->target_id == CAM_TARGET_WILDCARD
144952c9ce25SScott Long 	 || device->lun_id == CAM_LUN_WILDCARD)
145052c9ce25SScott Long 		return;
145152c9ce25SScott Long 
145252c9ce25SScott Long 	/*
145352c9ce25SScott Long 	 * We need our own path with wildcards expanded to
145452c9ce25SScott Long 	 * handle certain types of events.
145552c9ce25SScott Long 	 */
145652c9ce25SScott Long 	if ((async_code == AC_SENT_BDR)
145752c9ce25SScott Long 	 || (async_code == AC_BUS_RESET)
145852c9ce25SScott Long 	 || (async_code == AC_INQ_CHANGED))
145952c9ce25SScott Long 		status = xpt_compile_path(&newpath, NULL,
146052c9ce25SScott Long 					  bus->path_id,
146152c9ce25SScott Long 					  target->target_id,
146252c9ce25SScott Long 					  device->lun_id);
146352c9ce25SScott Long 	else
146452c9ce25SScott Long 		status = CAM_REQ_CMP_ERR;
146552c9ce25SScott Long 
146652c9ce25SScott Long 	if (status == CAM_REQ_CMP) {
146752c9ce25SScott Long 
146852c9ce25SScott Long 		/*
146952c9ce25SScott Long 		 * Allow transfer negotiation to occur in a
147052c9ce25SScott Long 		 * tag free environment.
147152c9ce25SScott Long 		 */
147252c9ce25SScott Long 		if (async_code == AC_SENT_BDR
147352c9ce25SScott Long 		 || async_code == AC_BUS_RESET)
147452c9ce25SScott Long 			scsi_toggle_tags(&newpath);
147552c9ce25SScott Long 
147652c9ce25SScott Long 		if (async_code == AC_INQ_CHANGED) {
147752c9ce25SScott Long 			/*
147852c9ce25SScott Long 			 * We've sent a start unit command, or
147952c9ce25SScott Long 			 * something similar to a device that
148052c9ce25SScott Long 			 * may have caused its inquiry data to
148152c9ce25SScott Long 			 * change. So we re-scan the device to
148252c9ce25SScott Long 			 * refresh the inquiry data for it.
148352c9ce25SScott Long 			 */
148452c9ce25SScott Long 			ata_scan_lun(newpath.periph, &newpath,
148552c9ce25SScott Long 				     CAM_EXPECT_INQ_CHANGE, NULL);
148652c9ce25SScott Long 		}
148752c9ce25SScott Long 		xpt_release_path(&newpath);
148852c9ce25SScott Long 	} else if (async_code == AC_LOST_DEVICE) {
148952c9ce25SScott Long 		device->flags |= CAM_DEV_UNCONFIGURED;
149052c9ce25SScott Long 	} else if (async_code == AC_TRANSFER_NEG) {
149152c9ce25SScott Long 		struct ccb_trans_settings *settings;
149252c9ce25SScott Long 
149352c9ce25SScott Long 		settings = (struct ccb_trans_settings *)async_arg;
149452c9ce25SScott Long 		scsi_set_transfer_settings(settings, device,
149552c9ce25SScott Long 					  /*async_update*/TRUE);
149652c9ce25SScott Long 	}
149752c9ce25SScott Long }
149852c9ce25SScott Long 
1499