Index: sys/dev/scsipi/scsiconf.c
===================================================================
RCS file: /cvsroot/src/sys/dev/scsipi/scsiconf.c,v
retrieving revision 1.280
diff -p -u -r1.280 scsiconf.c
--- sys/dev/scsipi/scsiconf.c	17 Jun 2017 22:35:50 -0000	1.280
+++ sys/dev/scsipi/scsiconf.c	4 May 2018 00:31:18 -0000
@@ -270,7 +270,7 @@ scsibusattach(device_t parent, device_t 
          * Create the discover thread
          */
         if (kthread_create(PRI_NONE, 0, NULL, scsibus_discover_thread, sc,
-            NULL, "%s-d", chan->chan_name)) {
+            &chan->chan_dthread, "%s-d", chan->chan_name)) {
                 aprint_error_dev(sc->sc_dev, "unable to create discovery "
 		    "thread for channel %d\n", chan->chan_channel);
                 return;
@@ -283,6 +283,7 @@ scsibus_discover_thread(void *arg)
 	struct scsibus_softc *sc = arg;
 
 	scsibus_config(sc);
+	sc->sc_channel->chan_dthread = NULL;
 	kthread_exit(0);
 }
 
@@ -336,6 +337,12 @@ scsibusdetach(device_t self, int flags)
 	int error;
 
 	/*
+	 * Defer while discovery thread is running
+	 */
+	while (chan->chan_dthread != NULL)
+		kpause("scsibusdet", false, hz, NULL);
+
+	/*
 	 * Detach all of the periphs.
 	 */
 	error = scsipi_target_detach(chan, -1, -1, flags);
@@ -415,6 +422,7 @@ scsi_probe_bus(struct scsibus_softc *sc,
 		 */
 		scsipi_set_xfer_mode(chan, target, 1);
 	}
+
 	scsipi_adapter_delref(chan->chan_adapter);
 ret:
 	return (error);
Index: sys/dev/scsipi/scsipiconf.h
===================================================================
RCS file: /cvsroot/src/sys/dev/scsipi/scsipiconf.h,v
retrieving revision 1.126
diff -p -u -r1.126 scsipiconf.h
--- sys/dev/scsipi/scsipiconf.h	29 Nov 2016 03:23:00 -0000	1.126
+++ sys/dev/scsipi/scsipiconf.h	4 May 2018 00:31:18 -0000
@@ -289,6 +289,7 @@ struct scsipi_channel {
 
 	int	chan_defquirks;		/* default device's quirks */
 
+	struct lwp *chan_dthread;	/* discovery thread */
 	struct lwp *chan_thread;	/* completion thread */
 	int	chan_tflags;		/* flags for the completion thread */
 
