Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 28 Jan 2010 08:41:31 +0000 (UTC)
From:      Alexander Motin <mav@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r203108 - in head/sys: cam cam/ata cam/scsi dev/ahci dev/asr dev/ata dev/ciss dev/hptiop dev/hptrr dev/mly dev/mpt dev/ppbus dev/siis dev/trm dev/twa dev/usb/storage
Message-ID:  <201001280841.o0S8fVbP083984@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: mav
Date: Thu Jan 28 08:41:30 2010
New Revision: 203108
URL: http://svn.freebsd.org/changeset/base/203108

Log:
  MFp4: Large set of CAM inprovements.
  
  - Unify bus reset/probe sequence. Whenever bus attached at boot or later,
  CAM will automatically reset and scan it. It allows to remove duplicate
  code from many drivers.
  - Any bus, attached before CAM completed it's boot-time initialization,
  will equally join to the process, delaying boot if needed.
  - New kern.cam.boot_delay loader tunable should help controllers that
  are still unable to register their buses in time (such as slow USB/
  PCCard/ CardBus devices), by adding one more event to wait on boot.
  - To allow synchronization between different CAM levels, concept of
  requests priorities was extended. Priorities now split between several
  "run levels". Device can be freezed at specified level, allowing higher
  priority requests to pass. For example, no payload requests allowed,
  until PMP driver enable port. ATA XPT negotiate transfer parameters,
  periph driver configure caching and so on.
  - Frozen requests are no more counted by request allocation scheduler.
  It fixes deadlocks, when frozen low priority payload requests occupying
  slots, required by higher levels to manage theit execution.
  - Two last changes were holding proper ATA reinitialization and error
  recovery implementation. Now it is done: SATA controllers and Port
  Multipliers now implement automatic hot-plug and should correctly
  recover from timeouts and bus resets.
  - Improve SCSI error recovery for devices on buses without automatic sense
  reporting, such as ATAPI or USB. For example, it allows CAM to wait, while
  CD drive loads disk, instead of immediately return error status.
  - Decapitalize diagnostic messages and make them more readable and sensible.
  - Teach PMP driver to limit maximum speed on fan-out ports.
  - Make boot wait for PMP scan completes, and make rescan more reliable.
  - Fix pass driver, to return CCB to user level in case of error.
  - Increase number of retries in cd driver, as device may return several UAs.

Modified:
  head/sys/cam/ata/ata_all.c
  head/sys/cam/ata/ata_da.c
  head/sys/cam/ata/ata_pmp.c
  head/sys/cam/ata/ata_xpt.c
  head/sys/cam/cam.c
  head/sys/cam/cam.h
  head/sys/cam/cam_ccb.h
  head/sys/cam/cam_periph.c
  head/sys/cam/cam_periph.h
  head/sys/cam/cam_queue.h
  head/sys/cam/cam_sim.c
  head/sys/cam/cam_xpt.c
  head/sys/cam/cam_xpt.h
  head/sys/cam/cam_xpt_internal.h
  head/sys/cam/cam_xpt_periph.h
  head/sys/cam/cam_xpt_sim.h
  head/sys/cam/scsi/scsi_all.c
  head/sys/cam/scsi/scsi_cd.c
  head/sys/cam/scsi/scsi_ch.c
  head/sys/cam/scsi/scsi_da.c
  head/sys/cam/scsi/scsi_low.c
  head/sys/cam/scsi/scsi_pass.c
  head/sys/cam/scsi/scsi_xpt.c
  head/sys/dev/ahci/ahci.c
  head/sys/dev/ahci/ahci.h
  head/sys/dev/asr/asr.c
  head/sys/dev/ata/ata-all.c
  head/sys/dev/ata/atapi-cam.c
  head/sys/dev/ciss/ciss.c
  head/sys/dev/hptiop/hptiop.c
  head/sys/dev/hptrr/hptrr_osm_bsd.c
  head/sys/dev/hptrr/os_bsd.h
  head/sys/dev/mly/mly.c
  head/sys/dev/mpt/mpt_cam.h
  head/sys/dev/mpt/mpt_raid.c
  head/sys/dev/ppbus/vpo.c
  head/sys/dev/siis/siis.c
  head/sys/dev/trm/trm.c
  head/sys/dev/twa/tw_osl_cam.c
  head/sys/dev/usb/storage/umass.c

Modified: head/sys/cam/ata/ata_all.c
==============================================================================
--- head/sys/cam/ata/ata_all.c	Thu Jan 28 05:28:48 2010	(r203107)
+++ head/sys/cam/ata/ata_all.c	Thu Jan 28 08:41:30 2010	(r203108)
@@ -198,7 +198,7 @@ ata_command_sbuf(struct ccb_ataio *ataio
 {
 	char cmd_str[(12 * 3) + 1];
 
-	sbuf_printf(sb, "CMD: %s: %s",
+	sbuf_printf(sb, "%s. ACB: %s",
 	    ata_op_string(&ataio->cmd),
 	    ata_cmd_string(&ataio->cmd, cmd_str, sizeof(cmd_str)));
 
@@ -212,7 +212,7 @@ int
 ata_status_sbuf(struct ccb_ataio *ataio, struct sbuf *sb)
 {
 
-	sbuf_printf(sb, "ATA Status: %02x (%s%s%s%s%s%s%s%s)",
+	sbuf_printf(sb, "ATA status: %02x (%s%s%s%s%s%s%s%s)",
 	    ataio->res.status,
 	    (ataio->res.status & 0x80) ? "BSY " : "",
 	    (ataio->res.status & 0x40) ? "DRDY " : "",
@@ -223,7 +223,7 @@ ata_status_sbuf(struct ccb_ataio *ataio,
 	    (ataio->res.status & 0x02) ? "IDX " : "",
 	    (ataio->res.status & 0x01) ? "ERR" : "");
 	if (ataio->res.status & 1) {
-	    sbuf_printf(sb, ", Error: %02x (%s%s%s%s%s%s%s%s)",
+	    sbuf_printf(sb, ", error: %02x (%s%s%s%s%s%s%s%s)",
 		ataio->res.error,
 		(ataio->res.error & 0x80) ? "ICRC " : "",
 		(ataio->res.error & 0x40) ? "UNC " : "",

Modified: head/sys/cam/ata/ata_da.c
==============================================================================
--- head/sys/cam/ata/ata_da.c	Thu Jan 28 05:28:48 2010	(r203107)
+++ head/sys/cam/ata/ata_da.c	Thu Jan 28 08:41:30 2010	(r203108)
@@ -689,7 +689,7 @@ adaregister(struct cam_periph *periph, v
 
 	/* Check if the SIM does not want queued commands */
 	bzero(&cpi, sizeof(cpi));
-	xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
+	xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NONE);
 	cpi.ccb_h.func_code = XPT_PATH_INQ;
 	xpt_action((union ccb *)&cpi);
 	if (cpi.ccb_h.status != CAM_REQ_CMP ||

Modified: head/sys/cam/ata/ata_pmp.c
==============================================================================
--- head/sys/cam/ata/ata_pmp.c	Thu Jan 28 05:28:48 2010	(r203107)
+++ head/sys/cam/ata/ata_pmp.c	Thu Jan 28 08:41:30 2010	(r203108)
@@ -98,6 +98,9 @@ struct pmp_softc {
 	int			reset;
 	int			frozen;
 	int			restart;
+	int			events;
+#define PMP_EV_RESET	1
+#define PMP_EV_RESCAN	2
 	union			ccb saved_ccb;
 	struct task		sysctl_task;
 	struct sysctl_ctx_list	sysctl_ctx;
@@ -179,7 +182,8 @@ pmpfreeze(struct cam_periph *periph, int
 		    i, 0) == CAM_REQ_CMP) {
 			softc->frozen |= (1 << i);
 			xpt_acquire_device(dpath->device);
-			cam_freeze_devq(dpath);
+			cam_freeze_devq_arg(dpath,
+			    RELSIM_RELEASE_RUNLEVEL, CAM_RL_BUS + 1);
 			xpt_free_path(dpath);
 		}
 	}
@@ -200,7 +204,8 @@ pmprelease(struct cam_periph *periph, in
 		    xpt_path_path_id(periph->path),
 		    i, 0) == CAM_REQ_CMP) {
 			softc->frozen &= ~(1 << i);
-			cam_release_devq(dpath, 0, 0, 0, FALSE);
+			cam_release_devq(dpath,
+			    RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_BUS + 1, FALSE);
 			xpt_release_device(dpath->device);
 			xpt_free_path(dpath);
 		}
@@ -298,19 +303,20 @@ pmpasync(void *callback_arg, u_int32_t c
 	case AC_BUS_RESET:
 		softc = (struct pmp_softc *)periph->softc;
 		cam_periph_async(periph, code, path, arg);
-		if (code == AC_SCSI_AEN && softc->state != PMP_STATE_NORMAL &&
-		    softc->state != PMP_STATE_SCAN)
-			break;
-		if (softc->state != PMP_STATE_SCAN)
-			pmpfreeze(periph, softc->found);
+		if (code == AC_SCSI_AEN)
+			softc->events |= PMP_EV_RESCAN;
 		else
-			pmpfreeze(periph, softc->found & ~(1 << softc->pm_step));
+			softc->events |= PMP_EV_RESET;
+		if (code == AC_SCSI_AEN && softc->state != PMP_STATE_NORMAL)
+			break;
+		xpt_hold_boot();
+		pmpfreeze(periph, softc->found);
 		if (code == AC_SENT_BDR || code == AC_BUS_RESET)
 			softc->found = 0; /* We have to reset everything. */
 		if (softc->state == PMP_STATE_NORMAL) {
-			softc->state = PMP_STATE_PORTS;
+			softc->state = PMP_STATE_PRECONFIG;
 			cam_periph_acquire(periph);
-			xpt_schedule(periph, CAM_PRIORITY_BUS);
+			xpt_schedule(periph, CAM_PRIORITY_DEV);
 		} else
 			softc->restart = 1;
 		break;
@@ -353,7 +359,6 @@ static cam_status
 pmpregister(struct cam_periph *periph, void *arg)
 {
 	struct pmp_softc *softc;
-	struct ccb_pathinq cpi;
 	struct ccb_getdev *cgd;
 
 	cgd = (struct ccb_getdev *)arg;
@@ -377,16 +382,8 @@ pmpregister(struct cam_periph *periph, v
 	}
 	periph->softc = softc;
 
-	softc->state = PMP_STATE_PORTS;
 	softc->pm_pid = ((uint32_t *)&cgd->ident_data)[0];
 	softc->pm_prv = ((uint32_t *)&cgd->ident_data)[1];
-
-	/* Check if the SIM does not want queued commands */
-	bzero(&cpi, sizeof(cpi));
-	xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
-	cpi.ccb_h.func_code = XPT_PATH_INQ;
-	xpt_action((union ccb *)&cpi);
-
 	TASK_INIT(&softc->sysctl_task, 0, pmpsysctlinit, periph);
 
 	xpt_announce_periph(periph, NULL);
@@ -408,7 +405,10 @@ pmpregister(struct cam_periph *periph, v
 	 * the end of probe.
 	 */
 	(void)cam_periph_acquire(periph);
-	xpt_schedule(periph, CAM_PRIORITY_BUS);
+	xpt_hold_boot();
+	softc->state = PMP_STATE_PORTS;
+	softc->events = PMP_EV_RESCAN;
+	xpt_schedule(periph, CAM_PRIORITY_DEV);
 
 	return(CAM_REQ_CMP);
 }
@@ -416,17 +416,35 @@ pmpregister(struct cam_periph *periph, v
 static void
 pmpstart(struct cam_periph *periph, union ccb *start_ccb)
 {
+	struct ccb_trans_settings cts;
 	struct ccb_ataio *ataio;
 	struct pmp_softc *softc;
+	struct cam_path *dpath;
+	int revision = 0;
 
 	softc = (struct pmp_softc *)periph->softc;
 	ataio = &start_ccb->ataio;
 	
 	if (softc->restart) {
 		softc->restart = 0;
-		softc->state = PMP_STATE_PORTS;
+		softc->state = min(softc->state, PMP_STATE_PRECONFIG);
+	}
+	/* Fetch user wanted device speed. */
+	if (softc->state == PMP_STATE_RESET ||
+	    softc->state == PMP_STATE_CONNECT) {
+		if (xpt_create_path(&dpath, periph,
+		    xpt_path_path_id(periph->path),
+		    softc->pm_step, 0) == CAM_REQ_CMP) {
+			bzero(&cts, sizeof(cts));
+			xpt_setup_ccb(&cts.ccb_h, dpath, CAM_PRIORITY_NONE);
+			cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
+			cts.type = CTS_TYPE_USER_SETTINGS;
+			xpt_action((union ccb *)&cts);
+			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_REVISION)
+				revision = cts.xport_specific.sata.revision;
+			xpt_free_path(dpath);
+		}
 	}
-
 	switch (softc->state) {
 	case PMP_STATE_PORTS:
 		cam_fill_ataio(ataio,
@@ -460,7 +478,8 @@ pmpstart(struct cam_periph *periph, unio
 		      /*dxfer_len*/0,
 		      pmp_default_timeout * 1000);
 		ata_pm_write_cmd(ataio, 2, softc->pm_step,
-		    (softc->found & (1 << softc->pm_step)) ? 0 : 1);
+		    (revision << 4) |
+		    ((softc->found & (1 << softc->pm_step)) ? 0 : 1));
 		break;
 	case PMP_STATE_CONNECT:
 		cam_fill_ataio(ataio,
@@ -471,7 +490,8 @@ pmpstart(struct cam_periph *periph, unio
 		      /*data_ptr*/NULL,
 		      /*dxfer_len*/0,
 		      pmp_default_timeout * 1000);
-		ata_pm_write_cmd(ataio, 2, softc->pm_step, 0);
+		ata_pm_write_cmd(ataio, 2, softc->pm_step,
+		    (revision << 4));
 		break;
 	case PMP_STATE_CHECK:
 		cam_fill_ataio(ataio,
@@ -519,9 +539,9 @@ pmpdone(struct cam_periph *periph, union
 	struct ccb_trans_settings cts;
 	struct pmp_softc *softc;
 	struct ccb_ataio *ataio;
-	union ccb *work_ccb;
 	struct cam_path *path, *dpath;
 	u_int32_t  priority, res;
+	int i;
 
 	softc = (struct pmp_softc *)periph->softc;
 	ataio = &done_ccb->ataio;
@@ -547,16 +567,8 @@ pmpdone(struct cam_periph *periph, union
 
 	if (softc->restart) {
 		softc->restart = 0;
-		if (softc->state == PMP_STATE_SCAN) {
-			pmpfreeze(periph, 1 << softc->pm_step);
-			work_ccb = done_ccb;
-			done_ccb = (union ccb*)work_ccb->ccb_h.ppriv_ptr0;
-			/* Free the current request path- we're done with it. */
-		    	xpt_free_path(work_ccb->ccb_h.path);
-			xpt_free_ccb(work_ccb);
-		}
 		xpt_release_ccb(done_ccb);
-		softc->state = PMP_STATE_PORTS;
+		softc->state = min(softc->state, PMP_STATE_PRECONFIG);
 		xpt_schedule(periph, priority);
 		return;
 	}
@@ -645,7 +657,7 @@ pmpdone(struct cam_periph *periph, union
 			    xpt_path_path_id(periph->path),
 			    softc->pm_step, 0) == CAM_REQ_CMP) {
 				bzero(&cts, sizeof(cts));
-				xpt_setup_ccb(&cts.ccb_h, dpath, CAM_PRIORITY_NORMAL);
+				xpt_setup_ccb(&cts.ccb_h, dpath, CAM_PRIORITY_NONE);
 				cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
 				cts.type = CTS_TYPE_CURRENT_SETTINGS;
 				cts.xport_specific.sata.revision = (res & 0x0f0) >> 4;
@@ -705,53 +717,43 @@ pmpdone(struct cam_periph *periph, union
 		xpt_schedule(periph, priority);
 		return;
 	case PMP_STATE_CONFIG:
-		if (softc->found) {
-			softc->pm_step = 0;
-			softc->state = PMP_STATE_SCAN;
-			work_ccb = xpt_alloc_ccb_nowait();
-			if (work_ccb != NULL)
-				goto do_scan;
-			xpt_release_ccb(done_ccb);
+		for (i = 0; i < softc->pm_ports; i++) {
+			union ccb *ccb;
+
+			if ((softc->found & (1 << i)) == 0)
+				continue;
+			if (xpt_create_path(&dpath, periph,
+			    xpt_path_path_id(periph->path),
+			    i, 0) != CAM_REQ_CMP) {
+				printf("pmpdone: xpt_create_path failed"
+				    ", bus scan halted\n");
+				xpt_free_ccb(done_ccb);
+				goto done;
+			}
+			/* If we did hard reset to this device, inform XPT. */
+			if ((softc->reset & softc->found & (1 << i)) != 0)
+				xpt_async(AC_SENT_BDR, dpath, NULL);
+			/* If rescan requested, scan this device. */
+			if (softc->events & PMP_EV_RESCAN) {
+				ccb = xpt_alloc_ccb_nowait();
+				if (ccb == NULL) {
+					xpt_free_path(dpath);
+					goto done;
+				}
+				xpt_setup_ccb(&ccb->ccb_h, dpath, CAM_PRIORITY_XPT);
+				xpt_rescan(ccb);
+			} else
+				xpt_free_path(dpath);
 		}
 		break;
-	case PMP_STATE_SCAN:
-		work_ccb = done_ccb;
-		done_ccb = (union ccb*)work_ccb->ccb_h.ppriv_ptr0;
-		/* Free the current request path- we're done with it. */
-		xpt_free_path(work_ccb->ccb_h.path);
-		softc->pm_step++;
-do_scan:
-		while (softc->pm_step < softc->pm_ports &&
-		    (softc->found & (1 << softc->pm_step)) == 0) {
-			softc->pm_step++;
-		}
-		if (softc->pm_step >= softc->pm_ports) {
-			xpt_free_ccb(work_ccb);
-			break;
-		}
-		if (xpt_create_path(&dpath, periph,
-		    done_ccb->ccb_h.path_id,
-		    softc->pm_step, 0) != CAM_REQ_CMP) {
-			printf("pmpdone: xpt_create_path failed"
-			    ", bus scan halted\n");
-			xpt_free_ccb(work_ccb);
-			break;
-		}
-		xpt_setup_ccb(&work_ccb->ccb_h, dpath,
-		    done_ccb->ccb_h.pinfo.priority);
-		work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
-		work_ccb->ccb_h.cbfcnp = pmpdone;
-		work_ccb->ccb_h.ppriv_ptr0 = done_ccb;
-		work_ccb->crcn.flags = done_ccb->crcn.flags;
-		xpt_action(work_ccb);
-		pmprelease(periph, 1 << softc->pm_step);
-		return;
 	default:
 		break;
 	}
 done:
 	xpt_release_ccb(done_ccb);
 	softc->state = PMP_STATE_NORMAL;
+	softc->events = 0;
+	xpt_release_boot();
 	pmprelease(periph, -1);
 	cam_periph_release_locked(periph);
 }

Modified: head/sys/cam/ata/ata_xpt.c
==============================================================================
--- head/sys/cam/ata/ata_xpt.c	Thu Jan 28 05:28:48 2010	(r203107)
+++ head/sys/cam/ata/ata_xpt.c	Thu Jan 28 08:41:30 2010	(r203108)
@@ -130,6 +130,7 @@ typedef struct {
 	u_int8_t	digest[16];
 	uint32_t	pm_pid;
 	uint32_t	pm_prv;
+	int		restart;
 	struct cam_periph *periph;
 } probe_softc;
 
@@ -231,15 +232,11 @@ proberegister(struct cam_periph *periph,
 	if (status != CAM_REQ_CMP) {
 		return (status);
 	}
-
-
 	/*
-	 * Ensure we've waited at least a bus settle
-	 * delay before attempting to probe the device.
-	 * For HBAs that don't do bus resets, this won't make a difference.
+	 * Ensure nobody slip in until probe finish.
 	 */
-	cam_periph_freeze_after_event(periph, &periph->path->bus->last_reset,
-				      scsi_delay);
+	cam_freeze_devq_arg(periph->path,
+	    RELSIM_RELEASE_RUNLEVEL, CAM_RL_XPT + 1);
 	probeschedule(periph);
 	return(CAM_REQ_CMP);
 }
@@ -247,17 +244,12 @@ proberegister(struct cam_periph *periph,
 static void
 probeschedule(struct cam_periph *periph)
 {
-	struct ccb_pathinq cpi;
 	union ccb *ccb;
 	probe_softc *softc;
 
 	softc = (probe_softc *)periph->softc;
 	ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
 
-	xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
-	cpi.ccb_h.func_code = XPT_PATH_INQ;
-	xpt_action((union ccb *)&cpi);
-
 	if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) ||
 	    periph->path->device->protocol == PROTO_SATAPM)
 		PROBE_SET_ACTION(softc, PROBE_RESET);
@@ -269,7 +261,7 @@ probeschedule(struct cam_periph *periph)
 	else
 		softc->flags &= ~PROBE_NO_ANNOUNCE;
 
-	xpt_schedule(periph, ccb->ccb_h.pinfo.priority);
+	xpt_schedule(periph, CAM_PRIORITY_XPT);
 }
 
 static void
@@ -290,6 +282,14 @@ probestart(struct cam_periph *periph, un
 	csio = &start_ccb->csio;
 	ident_buf = &periph->path->device->ident_data;
 
+	if (softc->restart) {
+		softc->restart = 0;
+		if ((path->device->flags & CAM_DEV_UNCONFIGURED) ||
+		    path->device->protocol == PROTO_SATAPM)
+			softc->action = PROBE_RESET;
+		else
+			softc->action = PROBE_IDENTIFY;
+	}
 	switch (softc->action) {
 	case PROBE_RESET:
 		cam_fill_ataio(ataio,
@@ -299,7 +299,7 @@ probestart(struct cam_periph *periph, un
 		      0,
 		      /*data_ptr*/NULL,
 		      /*dxfer_len*/0,
-		      (start_ccb->ccb_h.target_id == 15 ? 3 : 15) * 1000);
+		      15 * 1000);
 		ata_reset_cmd(ataio);
 		break;
 	case PROBE_IDENTIFY:
@@ -339,7 +339,7 @@ probestart(struct cam_periph *periph, un
 		mode = 0;
 		/* Fetch user modes from SIM. */
 		bzero(&cts, sizeof(cts));
-		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
+		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
 		cts.type = CTS_TYPE_USER_SETTINGS;
 		xpt_action((union ccb *)&cts);
@@ -355,7 +355,7 @@ negotiate:
 		wantmode = mode = ata_max_mode(ident_buf, mode);
 		/* Report modes to SIM. */
 		bzero(&cts, sizeof(cts));
-		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
+		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
 		if (path->device->transport == XPORT_ATA) {
@@ -368,7 +368,7 @@ negotiate:
 		xpt_action((union ccb *)&cts);
 		/* Fetch current modes from SIM. */
 		bzero(&cts, sizeof(cts));
-		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
+		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
 		xpt_action((union ccb *)&cts);
@@ -400,7 +400,7 @@ negotiate:
 		bytecount = 8192;	/* SATA maximum */
 		/* Fetch user bytecount from SIM. */
 		bzero(&cts, sizeof(cts));
-		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
+		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
 		cts.type = CTS_TYPE_USER_SETTINGS;
 		xpt_action((union ccb *)&cts);
@@ -416,7 +416,7 @@ negotiate:
 		    bytecount / ata_logical_sector_size(ident_buf)));
 		/* Report bytecount to SIM. */
 		bzero(&cts, sizeof(cts));
-		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
+		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
 		if (path->device->transport == XPORT_ATA) {
@@ -431,7 +431,7 @@ negotiate:
 		xpt_action((union ccb *)&cts);
 		/* Fetch current bytecount from SIM. */
 		bzero(&cts, sizeof(cts));
-		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
+		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
 		xpt_action((union ccb *)&cts);
@@ -462,7 +462,7 @@ negotiate:
 		bytecount = 8192;	/* SATA maximum */
 		/* Fetch user bytecount from SIM. */
 		bzero(&cts, sizeof(cts));
-		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
+		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
 		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
 		cts.type = CTS_TYPE_USER_SETTINGS;
 		xpt_action((union ccb *)&cts);
@@ -482,7 +482,7 @@ negotiate:
 		}
 		/* Report bytecount to SIM. */
 		bzero(&cts, sizeof(cts));
-		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
+		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
 		if (path->device->transport == XPORT_ATA) {
@@ -560,7 +560,7 @@ proberequestdefaultnegotiation(struct ca
 {
 	struct ccb_trans_settings cts;
 
-	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
+	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
 	cts.type = CTS_TYPE_USER_SETTINGS;
 	xpt_action((union ccb *)&cts);
@@ -582,7 +582,7 @@ proberequestbackoff(struct cam_periph *p
 	struct ccb_trans_settings_spi *spi;
 
 	memset(&cts, 0, sizeof (cts));
-	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
+	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
 	xpt_action((union ccb *)&cts);
@@ -739,7 +739,7 @@ noerror:
 		    done_ccb->ccb_h.target_id == 15) {
 			/* Report SIM that PM is present. */
 			bzero(&cts, sizeof(cts));
-			xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
+			xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
 			cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
 			cts.type = CTS_TYPE_CURRENT_SETTINGS;
 			cts.xport_specific.sata.pm_present = 1;
@@ -836,7 +836,7 @@ noerror:
 		    path->bus->sim->max_tagged_dev_openings != 0) {
 			/* Report SIM which tags are allowed. */
 			bzero(&cts, sizeof(cts));
-			xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
+			xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
 			cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
 			cts.type = CTS_TYPE_CURRENT_SETTINGS;
 			cts.xport_specific.sata.tags = path->device->maxtags;
@@ -957,18 +957,23 @@ noerror:
 		break;
 	}
 done:
-	xpt_release_ccb(done_ccb);
-	done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
-	TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe);
-	done_ccb->ccb_h.status = CAM_REQ_CMP;
-	done_ccb->ccb_h.ppriv_field1 = found;
-	xpt_done(done_ccb);
-	if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
-		cam_periph_invalidate(periph);
-		cam_periph_release_locked(periph);
-	} else {
+	if (softc->restart) {
+		softc->restart = 0;
+		xpt_release_ccb(done_ccb);
 		probeschedule(periph);
+		return;
 	}
+	xpt_release_ccb(done_ccb);
+	while ((done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs))) {
+		TAILQ_REMOVE(&softc->request_ccbs,
+		    &done_ccb->ccb_h, periph_links.tqe);
+		done_ccb->ccb_h.status = found ? CAM_REQ_CMP : CAM_REQ_CMP_ERR;
+		xpt_done(done_ccb);
+	}
+	cam_release_devq(periph->path,
+	    RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_XPT + 1, FALSE);
+	cam_periph_invalidate(periph);
+	cam_periph_release_locked(periph);
 }
 
 static void
@@ -1013,7 +1018,7 @@ ata_scan_bus(struct cam_periph *periph, 
 {
 	struct	cam_path *path;
 	ata_scan_bus_info *scan_info;
-	union	ccb *work_ccb;
+	union	ccb *work_ccb, *reset_ccb;
 	cam_status status;
 
 	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
@@ -1038,6 +1043,26 @@ ata_scan_bus(struct cam_periph *periph, 
 			return;
 		}
 
+		/* We may need to reset bus first, if we haven't done it yet. */
+		if ((work_ccb->cpi.hba_inquiry &
+		    (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) &&
+		    !(work_ccb->cpi.hba_misc & PIM_NOBUSRESET) &&
+		    !timevalisset(&request_ccb->ccb_h.path->bus->last_reset)) {
+			reset_ccb = xpt_alloc_ccb_nowait();
+			xpt_setup_ccb(&reset_ccb->ccb_h, request_ccb->ccb_h.path,
+			      CAM_PRIORITY_NONE);
+			reset_ccb->ccb_h.func_code = XPT_RESET_BUS;
+			xpt_action(reset_ccb);
+			if (reset_ccb->ccb_h.status != CAM_REQ_CMP) {
+				request_ccb->ccb_h.status = reset_ccb->ccb_h.status;
+				xpt_free_ccb(reset_ccb);
+				xpt_free_ccb(work_ccb);
+				xpt_done(request_ccb);
+				return;
+			}
+			xpt_free_ccb(reset_ccb);
+		}
+
 		/* Save some state for use while we probe for devices */
 		scan_info = (ata_scan_bus_info *)
 		    malloc(sizeof(ata_scan_bus_info), M_CAMXPT, M_NOWAIT);
@@ -1071,7 +1096,7 @@ ata_scan_bus(struct cam_periph *periph, 
 		/* If there is PMP... */
 		if ((scan_info->cpi->hba_inquiry & PI_SATAPM) &&
 		    (scan_info->counter == scan_info->cpi->max_target)) {
-			if (work_ccb->ccb_h.ppriv_field1 != 0) {
+			if (work_ccb->ccb_h.status == CAM_REQ_CMP) {
 				/* everything else willbe probed by it */
 				goto done;
 			} else {
@@ -1141,10 +1166,9 @@ ata_scan_lun(struct cam_periph *periph, 
 	struct cam_path *new_path;
 	struct cam_periph *old_periph;
 
-	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
-		  ("xpt_scan_lun\n"));
+	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_scan_lun\n"));
 
-	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
+	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
 	cpi.ccb_h.func_code = XPT_PATH_INQ;
 	xpt_action((union ccb *)&cpi);
 
@@ -1182,7 +1206,7 @@ ata_scan_lun(struct cam_periph *periph, 
 			free(new_path, M_CAMXPT);
 			return;
 		}
-		xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_NORMAL);
+		xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_XPT);
 		request_ccb->ccb_h.cbfcnp = xptscandone;
 		request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
 		request_ccb->crcn.flags = flags;
@@ -1194,6 +1218,7 @@ ata_scan_lun(struct cam_periph *periph, 
 		softc = (probe_softc *)old_periph->softc;
 		TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
 				  periph_links.tqe);
+		softc->restart = 1;
 	} else {
 		status = cam_periph_alloc(proberegister, NULL, probecleanup,
 					  probestart, "aprobe",
@@ -1281,7 +1306,7 @@ ata_device_transport(struct cam_path *pa
 	struct ata_params *ident_buf = NULL;
 
 	/* Get transport information from the SIM */
-	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
+	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
 	cpi.ccb_h.func_code = XPT_PATH_INQ;
 	xpt_action((union ccb *)&cpi);
 
@@ -1301,7 +1326,7 @@ ata_device_transport(struct cam_path *pa
 	    ata_version(ident_buf->version_major) : cpi.transport_version;
 
 	/* Tell the controller what we think */
-	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
+	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
 	cts.transport = path->device->transport;
@@ -1429,7 +1454,7 @@ ata_set_transfer_settings(struct ccb_tra
 
 	inq_data = &device->inq_data;
 	scsi = &cts->proto_specific.scsi;
-	xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NORMAL);
+	xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NONE);
 	cpi.ccb_h.func_code = XPT_PATH_INQ;
 	xpt_action((union ccb *)&cpi);
 
@@ -1450,7 +1475,7 @@ ata_set_transfer_settings(struct ccb_tra
 		 * Perform sanity checking against what the
 		 * controller and device can do.
 		 */
-		xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NORMAL);
+		xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NONE);
 		cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
 		cur_cts.type = cts->type;
 		xpt_action((union ccb *)&cur_cts);
@@ -1550,6 +1575,10 @@ ata_dev_async(u_int32_t async_code, stru
 			 */
 			ata_scan_lun(newpath.periph, &newpath,
 				     CAM_EXPECT_INQ_CHANGE, NULL);
+		} else {
+			/* We need to reinitialize device after reset. */
+			ata_scan_lun(newpath.periph, &newpath,
+				     0, NULL);
 		}
 		xpt_release_path(&newpath);
 	} else if (async_code == AC_LOST_DEVICE &&

Modified: head/sys/cam/cam.c
==============================================================================
--- head/sys/cam/cam.c	Thu Jan 28 05:28:48 2010	(r203107)
+++ head/sys/cam/cam.c	Thu Jan 28 08:41:30 2010	(r203108)
@@ -305,10 +305,10 @@ cam_error_string(struct cam_device *devi
 		entry = cam_fetch_status_entry(status);
 
 		if (entry == NULL)
-			sbuf_printf(&sb, "CAM Status: Unknown (%#x)\n",
+			sbuf_printf(&sb, "CAM status: Unknown (%#x)\n",
 				    ccb->ccb_h.status);
 		else
-			sbuf_printf(&sb, "CAM Status: %s\n",
+			sbuf_printf(&sb, "CAM status: %s\n",
 				    entry->status_text);
 	}
 
@@ -338,7 +338,7 @@ cam_error_string(struct cam_device *devi
 
 			if (proto_flags & CAM_ESF_PRINT_STATUS) {
 				sbuf_cat(&sb, path_str);
-				sbuf_printf(&sb, "SCSI Status: %s\n",
+				sbuf_printf(&sb, "SCSI status: %s\n",
 					    scsi_status_string(&ccb->csio));
 			}
 

Modified: head/sys/cam/cam.h
==============================================================================
--- head/sys/cam/cam.h	Thu Jan 28 05:28:48 2010	(r203107)
+++ head/sys/cam/cam.h	Thu Jan 28 08:41:30 2010	(r203108)
@@ -60,16 +60,29 @@ typedef u_int lun_id_t;
 struct cam_periph;
 
 /*
- * Priority information for a CAM structure.  The generation number is
- * incremented everytime a new entry is entered into the queue giving round
- * robin per priority level scheduling.
+ * Priority information for a CAM structure. 
+ */
+typedef enum {
+    CAM_RL_HOST,
+    CAM_RL_BUS,
+    CAM_RL_XPT,
+    CAM_RL_DEV,
+    CAM_RL_NORMAL,
+    CAM_RL_VALUES
+} cam_rl;
+/*
+ * The generation number is incremented everytime a new entry is entered into
+ * the queue giving round robin per priority level scheduling.
  */
 typedef struct {
 	u_int32_t priority;
-#define CAM_PRIORITY_BUS	0
-#define CAM_PRIORITY_DEV	0
-#define CAM_PRIORITY_NORMAL	1
+#define CAM_PRIORITY_HOST	((CAM_RL_HOST << 8) + 0x80)
+#define CAM_PRIORITY_BUS	((CAM_RL_BUS << 8) + 0x80)
+#define CAM_PRIORITY_XPT	((CAM_RL_XPT << 8) + 0x80)
+#define CAM_PRIORITY_DEV	((CAM_RL_DEV << 8) + 0x80)
+#define CAM_PRIORITY_NORMAL	((CAM_RL_NORMAL << 8) + 0x80)
 #define CAM_PRIORITY_NONE	(u_int32_t)-1
+#define CAM_PRIORITY_TO_RL(x)	((x) >> 8)
 	u_int32_t generation;
 	int       index;
 #define CAM_UNQUEUED_INDEX	-1

Modified: head/sys/cam/cam_ccb.h
==============================================================================
--- head/sys/cam/cam_ccb.h	Thu Jan 28 05:28:48 2010	(r203107)
+++ head/sys/cam/cam_ccb.h	Thu Jan 28 08:41:30 2010	(r203108)
@@ -126,7 +126,7 @@ typedef enum {
 	XPT_PATH_INQ		= 0x04,
 				/* Path routing inquiry */
 	XPT_REL_SIMQ		= 0x05,
-				/* Release a frozen SIM queue */
+				/* Release a frozen device queue */
 	XPT_SASYNC_CB		= 0x06,
 				/* Set Asynchronous Callback Parameters */
 	XPT_SDEV_TYPE		= 0x07,
@@ -142,6 +142,8 @@ typedef enum {
 				/* Path statistics (error counts, etc.) */
 	XPT_GDEV_STATS		= 0x0c,
 				/* Device statistics (error counts, etc.) */
+	XPT_FREEZE_QUEUE	= 0x0d,
+				/* Freeze device queue */
 /* SCSI Control Functions: 0x10->0x1F */
 	XPT_ABORT		= 0x10,
 				/* Abort the specified CCB */
@@ -685,8 +687,9 @@ struct ccb_relsim {
 #define RELSIM_RELEASE_AFTER_TIMEOUT	0x02
 #define RELSIM_RELEASE_AFTER_CMDCMPLT	0x04
 #define RELSIM_RELEASE_AFTER_QEMPTY	0x08
+#define RELSIM_RELEASE_RUNLEVEL		0x10
 	u_int32_t      openings;
-	u_int32_t      release_timeout;
+	u_int32_t      release_timeout;	/* Abstract argument. */
 	u_int32_t      qfrozen_cnt;
 };
 

Modified: head/sys/cam/cam_periph.c
==============================================================================
--- head/sys/cam/cam_periph.c	Thu Jan 28 05:28:48 2010	(r203107)
+++ head/sys/cam/cam_periph.c	Thu Jan 28 08:41:30 2010	(r203108)
@@ -71,19 +71,20 @@ static  void		camperiphfree(struct cam_p
 static int		camperiphscsistatuserror(union ccb *ccb,
 						 cam_flags camflags,
 						 u_int32_t sense_flags,
-						 union ccb *save_ccb,
 						 int *openings,
 						 u_int32_t *relsim_flags,
-						 u_int32_t *timeout);
+						 u_int32_t *timeout,
+						 const char **action_string);
 static	int		camperiphscsisenseerror(union ccb *ccb,
 					        cam_flags camflags,
 					        u_int32_t sense_flags,
-					        union ccb *save_ccb,
 					        int *openings,
 					        u_int32_t *relsim_flags,
-					        u_int32_t *timeout);
+					        u_int32_t *timeout,
+					        const char **action_string);
 
 static int nperiph_drivers;
+static int initialized = 0;
 struct periph_driver **periph_drivers;
 
 MALLOC_DEFINE(M_CAMPERIPH, "CAM periph", "CAM peripheral buffers");
@@ -99,6 +100,7 @@ TUNABLE_INT("kern.cam.periph_busy_delay"
 void
 periphdriver_register(void *data)
 {
+	struct periph_driver *drv = (struct periph_driver *)data;
 	struct periph_driver **newdrivers, **old;
 	int ndrivers;
 
@@ -108,13 +110,30 @@ periphdriver_register(void *data)
 	if (periph_drivers)
 		bcopy(periph_drivers, newdrivers,
 		      sizeof(*newdrivers) * nperiph_drivers);
-	newdrivers[nperiph_drivers] = (struct periph_driver *)data;
+	newdrivers[nperiph_drivers] = drv;
 	newdrivers[nperiph_drivers + 1] = NULL;
 	old = periph_drivers;
 	periph_drivers = newdrivers;
 	if (old)
 		free(old, M_CAMPERIPH);
 	nperiph_drivers++;
+	/* If driver marked as early or it is late now, initialize it. */
+	if (((drv->flags & CAM_PERIPH_DRV_EARLY) != 0 && initialized > 0) ||
+	    initialized > 1)
+		(*drv->init)();
+}
+
+void
+periphdriver_init(int level)
+{
+	int	i, early;
+
+	initialized = max(initialized, level);
+	for (i = 0; periph_drivers[i] != NULL; i++) {
+		early = (periph_drivers[i]->flags & CAM_PERIPH_DRV_EARLY) ? 1 : 2;
+		if (early == initialized)
+			(*periph_drivers[i]->init)();
+	}
 }
 
 cam_status
@@ -915,12 +934,14 @@ cam_periph_runccb(union ccb *ccb,
 
 	} while (error == ERESTART);
           
-	if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 
+	if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
 		cam_release_devq(ccb->ccb_h.path,
 				 /* relsim_flags */0,
 				 /* openings */0,
 				 /* timeout */0,
 				 /* getcount_only */ FALSE);
+		ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
+	}
 
 	if (ds != NULL) {
 		if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
@@ -950,17 +971,26 @@ cam_periph_runccb(union ccb *ccb,
 void
 cam_freeze_devq(struct cam_path *path)
 {
-	struct ccb_hdr ccb_h;
 
-	xpt_setup_ccb(&ccb_h, path, CAM_PRIORITY_NORMAL);
-	ccb_h.func_code = XPT_NOOP;
-	ccb_h.flags = CAM_DEV_QFREEZE;
-	xpt_action((union ccb *)&ccb_h);
+	cam_freeze_devq_arg(path, 0, 0);
+}
+
+void
+cam_freeze_devq_arg(struct cam_path *path, uint32_t flags, uint32_t arg)
+{
+	struct ccb_relsim crs;
+
+	xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NONE);
+	crs.ccb_h.func_code = XPT_FREEZE_QUEUE;
+	crs.release_flags = flags;
+	crs.openings = arg;
+	crs.release_timeout = arg;
+	xpt_action((union ccb *)&crs);
 }
 
 u_int32_t
 cam_release_devq(struct cam_path *path, u_int32_t relsim_flags,
-		 u_int32_t openings, u_int32_t timeout,
+		 u_int32_t openings, u_int32_t arg,
 		 int getcount_only)
 {
 	struct ccb_relsim crs;
@@ -970,22 +1000,92 @@ cam_release_devq(struct cam_path *path, 
 	crs.ccb_h.flags = getcount_only ? CAM_DEV_QFREEZE : 0;
 	crs.release_flags = relsim_flags;
 	crs.openings = openings;
-	crs.release_timeout = timeout;
+	crs.release_timeout = arg;
 	xpt_action((union ccb *)&crs);
 	return (crs.qfrozen_cnt);
 }
 
 #define saved_ccb_ptr ppriv_ptr0
+#define recovery_depth ppriv_field1
+static void
+camperiphsensedone(struct cam_periph *periph, union ccb *done_ccb)
+{
+	union ccb      *saved_ccb = (union ccb *)done_ccb->ccb_h.saved_ccb_ptr;
+	cam_status	status;
+	int		frozen = 0;
+	u_int		sense_key;
+	int		depth = done_ccb->ccb_h.recovery_depth;
+
+	status = done_ccb->ccb_h.status;
+	if (status & CAM_DEV_QFRZN) {
+		frozen = 1;
+		/*
+		 * Clear freeze flag now for case of retry,
+		 * freeze will be dropped later.
+		 */
+		done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
+	}
+	status &= CAM_STATUS_MASK;
+	switch (status) {
+	case CAM_REQ_CMP:
+	{
+		/*
+		 * If we manually retrieved sense into a CCB and got
+		 * something other than "NO SENSE" send the updated CCB
+		 * back to the client via xpt_done() to be processed via
+		 * the error recovery code again.
+		 */
+		sense_key = saved_ccb->csio.sense_data.flags;
+		sense_key &= SSD_KEY;
+		if (sense_key != SSD_KEY_NO_SENSE) {
+			saved_ccb->ccb_h.status |=
+			    CAM_AUTOSNS_VALID;
+		} else {
+			saved_ccb->ccb_h.status &=
+			    ~CAM_STATUS_MASK;
+			saved_ccb->ccb_h.status |=
+			    CAM_AUTOSENSE_FAIL;
+		}
+		bcopy(saved_ccb, done_ccb, sizeof(union ccb));
+		xpt_free_ccb(saved_ccb);
+		break;
+	}
+	default:
+		bcopy(saved_ccb, done_ccb, sizeof(union ccb));
+		xpt_free_ccb(saved_ccb);
+		done_ccb->ccb_h.status &= ~CAM_STATUS_MASK;
+		done_ccb->ccb_h.status |= CAM_AUTOSENSE_FAIL;
+		break;
+	}
+	periph->flags &= ~CAM_PERIPH_SENSE_INPROG;
+	/*
+	 * If it is the end of recovery, drop freeze, taken due to
+	 * CAM_DEV_QFREEZE flag, set on recovery request.
+	 */
+	if (depth == 0) {
+		cam_release_devq(done_ccb->ccb_h.path,
+			 /*relsim_flags*/0,
+			 /*openings*/0,
+			 /*timeout*/0,
+			 /*getcount_only*/0);
+	}
+	/*
+	 * Copy frozen flag from recovery request if it is set there
+	 * for some reason.
+	 */
+	if (frozen != 0)
+		done_ccb->ccb_h.status |= CAM_DEV_QFRZN;
+	(*done_ccb->ccb_h.cbfcnp)(periph, done_ccb);
+}
+
 static void
 camperiphdone(struct cam_periph *periph, union ccb *done_ccb)
 {
-	union ccb      *saved_ccb;
+	union ccb      *saved_ccb, *save_ccb;
 	cam_status	status;
 	int		frozen = 0;
-	int		sense;
 	struct scsi_start_stop_unit *scsi_cmd;
 	u_int32_t	relsim_flags, timeout;
-	int		xpt_done_ccb = FALSE;
 
 	status = done_ccb->ccb_h.status;
 	if (status & CAM_DEV_QFRZN) {
@@ -996,14 +1096,12 @@ camperiphdone(struct cam_periph *periph,
 		 */
 		done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
 	}
-	sense  = (status & CAM_AUTOSNS_VALID) != 0;
-	status &= CAM_STATUS_MASK;
 
 	timeout = 0;
 	relsim_flags = 0;
 	saved_ccb = (union ccb *)done_ccb->ccb_h.saved_ccb_ptr;
 
-	switch (status) {
+	switch (status & CAM_STATUS_MASK) {
 	case CAM_REQ_CMP:
 	{
 		/*
@@ -1012,57 +1110,19 @@ camperiphdone(struct cam_periph *periph,
 		 * the inquiry information.  Many devices (mostly disks)
 		 * don't properly report their inquiry information unless
 		 * they are spun up.
-		 *
-		 * If we manually retrieved sense into a CCB and got
-		 * something other than "NO SENSE" send the updated CCB
-		 * back to the client via xpt_done() to be processed via
-		 * the error recovery code again.
 		 */
-		if (done_ccb->ccb_h.func_code == XPT_SCSI_IO) {
-			scsi_cmd = (struct scsi_start_stop_unit *)
-					&done_ccb->csio.cdb_io.cdb_bytes;
-
-		 	if (scsi_cmd->opcode == START_STOP_UNIT)
-				xpt_async(AC_INQ_CHANGED,
-					  done_ccb->ccb_h.path, NULL);

*** DIFF OUTPUT TRUNCATED AT 1000 LINES ***



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201001280841.o0S8fVbP083984>