Index: sys/dev/scsipi/scsiconf.c =================================================================== RCS file: /cvsroot/src/sys/dev/scsipi/scsiconf.c,v retrieving revision 1.280 diff -u -r1.280 scsiconf.c --- sys/dev/scsipi/scsiconf.c 17 Jun 2017 22:35:50 -0000 1.280 +++ sys/dev/scsipi/scsiconf.c 6 Aug 2018 04:32:39 -0000 @@ -270,7 +270,7 @@ * 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 @@ struct scsibus_softc *sc = arg; scsibus_config(sc); + sc->sc_channel->chan_dthread = NULL; kthread_exit(0); } @@ -336,6 +337,12 @@ 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 @@ */ 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.127 diff -u -r1.127 scsipiconf.h --- sys/dev/scsipi/scsipiconf.h 4 Jul 2018 03:17:01 -0000 1.127 +++ sys/dev/scsipi/scsipiconf.h 6 Aug 2018 04:32:39 -0000 @@ -289,6 +289,7 @@ 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 */