Index: sys/dev/mpr/mpr_sas.h =================================================================== --- sys/dev/mpr/mpr_sas.h +++ sys/dev/mpr/mpr_sas.h @@ -64,6 +64,7 @@ SLIST_HEAD(, mprsas_lun) luns; TAILQ_HEAD(, mpr_command) commands; struct mpr_command *tm; + struct mpr_command *pending_remove_tm; TAILQ_HEAD(, mpr_command) timedout_commands; uint16_t exp_dev_handle; uint16_t phy_num; Index: sys/dev/mpr/mpr_sas.c =================================================================== --- sys/dev/mpr/mpr_sas.c +++ sys/dev/mpr/mpr_sas.c @@ -559,7 +559,6 @@ MPI2_SCSI_TASK_MANAGE_REPLY *reply; MPI2_SAS_IOUNIT_CONTROL_REQUEST *req; struct mprsas_target *targ; - struct mpr_command *next_cm; uint16_t handle; MPR_FUNCTRACE(sc); @@ -609,6 +608,19 @@ tm->cm_complete = mprsas_remove_complete; tm->cm_complete_data = (void *)(uintptr_t)handle; + /* + * Wait to send the REMOVE_DEVICE until all the commands have cleared. + * They should be aborted or time out and we'll kick thus off there + * if so. + */ + if (TAILQ_FIRST(&targ->commands) == NULL) { + mpr_dprint(sc, MPR_INFO, "No pending commands: starting remove_device\n"); + mpr_map_command(sc, tm); + targ->pending_remove_tm = NULL; + } else { + targ->pending_remove_tm = tm; + } + mpr_map_command(sc, tm); mpr_dprint(sc, MPR_INFO, "clearing target %u handle 0x%04x\n", @@ -618,15 +630,6 @@ "connector name (%4s)\n", targ->encl_level, targ->encl_slot, targ->connector_name); } - TAILQ_FOREACH_SAFE(tm, &targ->commands, cm_link, next_cm) { - union ccb *ccb; - - mpr_dprint(sc, MPR_XINFO, "Completing missed command %p\n", tm); - ccb = tm->cm_complete_data; - mprsas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE); - tm->cm_state = MPR_CM_STATE_BUSY; - mprsas_scsiio_complete(sc, tm); - } } static void @@ -642,6 +645,15 @@ reply = (MPI2_SAS_IOUNIT_CONTROL_REPLY *)tm->cm_reply; handle = (uint16_t)(uintptr_t)tm->cm_complete_data; + targ = tm->cm_targ; + + /* + * At this point, we should have no pending commands for the target. + * The remove target has just completed. + */ + KASSERT(TAILQ_FIRST(&targ->commands) == NULL, + ("%s: no commands should be pending\n", __func__)); + /* * Currently there should be no way we can hit this case. It only * happens when we have a failure to allocate chain frames, and @@ -674,7 +686,6 @@ */ if ((le16toh(reply->IOCStatus) & MPI2_IOCSTATUS_MASK) == MPI2_IOCSTATUS_SUCCESS) { - targ = tm->cm_targ; targ->handle = 0x0; targ->encl_handle = 0x0; targ->encl_level_valid = 0x0; @@ -1964,13 +1975,17 @@ /* * If devinfo is 0 this will be a volume. In that case don't tell CAM * that the volume has timed out. We want volumes to be enumerated - * until they are deleted/removed, not just failed. + * until they are deleted/removed, not just failed. In either event, + * we're removing the target due to a firmware event telling us + * the device is now gone (as opposed to some transient event). Since + * we're opting to remove failed devices from the OS's view, we need + * to propagate that status up the stack. */ if (targ->flags & MPRSAS_TARGET_INREMOVAL) { if (targ->devinfo == 0) mprsas_set_ccbstatus(ccb, CAM_REQ_CMP); else - mprsas_set_ccbstatus(ccb, CAM_SEL_TIMEOUT); + mprsas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE); xpt_done(ccb); return; } @@ -2900,6 +2915,21 @@ xpt_freeze_devq(ccb->ccb_h.path, /*count*/ 1); } + /* + * Check to see if we're removing the device. If so, and this is the + * last command on the queue, proceed with the deferred removal of the + * device. Note, for removing a volume, this won't trigger because + * pending_remove_tm will be NULL. + */ + if (cm->cm_targ->flags & MPRSAS_TARGET_INREMOVAL) { + if (TAILQ_FIRST(&cm->cm_targ->commands) == NULL && + cm->cm_targ->pending_remove_tm != NULL) { + mpr_dprint(sc, MPR_INFO, "Last pending command complete: starting remove_device\n"); + mpr_map_command(sc, cm->cm_targ->pending_remove_tm); + cm->cm_targ->pending_remove_tm = NULL; + } + } + mpr_free_command(sc, cm); xpt_done(ccb); }