Thu Mar 3 05:48:23 2022 UTC ()
usbnet: Fix ordering of actions in usbnet_stop.

Make sure all software activity is quiescent (callouts and tasks,
including ifmedia and mii callbacks -- anything that might trigger
register access) before asking the driver to stop the hardware.  This
way, the driver uno_stop routine is guaranteed exclusive access to
the registers.

This will also enable us to simplify the callouts and tasks so they
don't have to check the software state -- to be done in a separate
commit.


(riastradh)
diff -r1.55 -r1.56 src/sys/dev/usb/usbnet.c

cvs diff -r1.55 -r1.56 src/sys/dev/usb/usbnet.c (expand / switch to context diff)
--- src/sys/dev/usb/usbnet.c 2022/03/03 05:48:14 1.55
+++ src/sys/dev/usb/usbnet.c 2022/03/03 05:48:22 1.56
@@ -1,4 +1,4 @@
-/*	$NetBSD: usbnet.c,v 1.55 2022/03/03 05:48:14 riastradh Exp $	*/
+/*	$NetBSD: usbnet.c,v 1.56 2022/03/03 05:48:22 riastradh Exp $	*/
 
 /*
  * Copyright (c) 2019 Matthew R. Green
@@ -31,7 +31,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: usbnet.c,v 1.55 2022/03/03 05:48:14 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: usbnet.c,v 1.56 2022/03/03 05:48:22 riastradh Exp $");
 
 #include <sys/param.h>
 #include <sys/kernel.h>
@@ -1132,15 +1132,28 @@
 	mutex_exit(&unp->unp_txlock);
 	mutex_exit(&unp->unp_rxlock);
 
+	/*
+	 * Stop the timer first, then the task -- if the timer was
+	 * already firing, we stop the task or wait for it complete
+	 * only after if last fired.  Setting unp_stopping prevents the
+	 * timer task from being scheduled again.
+	 */
+	callout_halt(&unp->unp_stat_ch, &unp->unp_core_lock);
+	usb_rem_task_wait(un->un_udev, &unp->unp_ticktask, USB_TASKQ_DRIVER,
+	    &unp->unp_core_lock);
+
+	/*
+	 * Now that the software is quiescent, ask the driver to stop
+	 * the hardware.  The driver's uno_stop routine now has
+	 * exclusive access to any registers that might previously have
+	 * been used by to ifmedia, mii, or ioctl callbacks.
+	 */
 	uno_stop(un, ifp, disable);
 
+	/* Clear the watchdog timer.  */
 	mutex_enter(&unp->unp_txlock);
 	unp->unp_timer = 0;
 	mutex_exit(&unp->unp_txlock);
-
-	callout_halt(&unp->unp_stat_ch, &unp->unp_core_lock);
-	usb_rem_task_wait(un->un_udev, &unp->unp_ticktask, USB_TASKQ_DRIVER,
-	    &unp->unp_core_lock);
 
 	/* Stop transfers. */
 	usbnet_ep_stop_pipes(un);