Module Name:    src
Committed By:   riastradh
Date:           Sun Mar 13 11:30:04 UTC 2022

Modified Files:
        src/sys/dev/usb: xhci.c xhcivar.h

Log Message:
xhci(4): Serialize access to portsc registers.

Both xhci_roothub_ctrl and xhci_suspend/resume do r/m/w on them, so
use a mutex to serialize access to avoid stomping on each other.


To generate a diff of this commit:
cvs rdiff -u -r1.161 -r1.162 src/sys/dev/usb/xhci.c
cvs rdiff -u -r1.20 -r1.21 src/sys/dev/usb/xhcivar.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/usb/xhci.c
diff -u src/sys/dev/usb/xhci.c:1.161 src/sys/dev/usb/xhci.c:1.162
--- src/sys/dev/usb/xhci.c:1.161	Sun Mar 13 11:29:55 2022
+++ src/sys/dev/usb/xhci.c	Sun Mar 13 11:30:04 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: xhci.c,v 1.161 2022/03/13 11:29:55 riastradh Exp $	*/
+/*	$NetBSD: xhci.c,v 1.162 2022/03/13 11:30:04 riastradh Exp $	*/
 
 /*
  * Copyright (c) 2013 Jonathan A. Kollasch
@@ -34,7 +34,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: xhci.c,v 1.161 2022/03/13 11:29:55 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: xhci.c,v 1.162 2022/03/13 11:30:04 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_usb.h"
@@ -709,6 +709,12 @@ xhci_suspend(device_t self, const pmf_qu
 	mutex_exit(&sc->sc_lock);
 
 	/*
+	 * Block roothub xfers which might touch portsc registers until
+	 * we're done suspending.
+	 */
+	mutex_enter(&sc->sc_rhlock);
+
+	/*
 	 * xHCI Requirements Specification 1.2, May 2019, Sec. 4.23.2:
 	 * xHCI Power Management, p. 342
 	 * https://www.intel.com/content/dam/www/public/us/en/documents/technical-specifications/extensible-host-controler-interface-usb-xhci.pdf#page=342
@@ -889,7 +895,8 @@ xhci_suspend(device_t self, const pmf_qu
 	/* Success!  */
 	ok = true;
 
-out:	return ok;
+out:	mutex_exit(&sc->sc_rhlock);
+	return ok;
 }
 
 bool
@@ -906,6 +913,12 @@ xhci_resume(device_t self, const pmf_qua
 	KASSERT(sc->sc_suspender);
 
 	/*
+	 * Block roothub xfers which might touch portsc registers until
+	 * we're done resuming.
+	 */
+	mutex_enter(&sc->sc_rhlock);
+
+	/*
 	 * xHCI Requirements Specification 1.2, May 2019, Sec. 4.23.2:
 	 * xHCI Power Management, p. 343
 	 * https://www.intel.com/content/dam/www/public/us/en/documents/technical-specifications/extensible-host-controler-interface-usb-xhci.pdf#page=343
@@ -1089,7 +1102,8 @@ xhci_resume(device_t self, const pmf_qua
 	/* Success!  */
 	ok = true;
 
-out:	return ok;
+out:	mutex_exit(&sc->sc_rhlock);
+	return ok;
 }
 
 bool
@@ -1591,6 +1605,7 @@ xhci_init(struct xhci_softc *sc)
 
 	cv_init(&sc->sc_command_cv, "xhcicmd");
 	cv_init(&sc->sc_cmdbusy_cv, "xhcicmdq");
+	mutex_init(&sc->sc_rhlock, MUTEX_DEFAULT, IPL_NONE);
 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_USB);
 
@@ -3863,7 +3878,7 @@ xhci_noop(struct usbd_pipe *pipe)
  * Process root hub request.
  */
 static int
-xhci_roothub_ctrl(struct usbd_bus *bus, usb_device_request_t *req,
+xhci_roothub_ctrl_locked(struct usbd_bus *bus, usb_device_request_t *req,
     void *buf, int buflen)
 {
 	struct xhci_softc * const sc = XHCI_BUS2SC(bus);
@@ -3875,6 +3890,8 @@ xhci_roothub_ctrl(struct usbd_bus *bus, 
 
 	XHCIHIST_FUNC();
 
+	KASSERT(mutex_owned(&sc->sc_rhlock));
+
 	if (sc->sc_dying)
 		return -1;
 
@@ -4127,6 +4144,20 @@ xhci_roothub_ctrl(struct usbd_bus *bus, 
 	return totlen;
 }
 
+static int
+xhci_roothub_ctrl(struct usbd_bus *bus, usb_device_request_t *req,
+    void *buf, int buflen)
+{
+	struct xhci_softc *sc = XHCI_BUS2SC(bus);
+	int actlen;
+
+	mutex_enter(&sc->sc_rhlock);
+	actlen = xhci_roothub_ctrl_locked(bus, req, buf, buflen);
+	mutex_exit(&sc->sc_rhlock);
+
+	return actlen;
+}
+
 /* root hub interrupt */
 
 static usbd_status

Index: src/sys/dev/usb/xhcivar.h
diff -u src/sys/dev/usb/xhcivar.h:1.20 src/sys/dev/usb/xhcivar.h:1.21
--- src/sys/dev/usb/xhcivar.h:1.20	Thu Mar  3 06:09:03 2022
+++ src/sys/dev/usb/xhcivar.h	Sun Mar 13 11:30:04 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: xhcivar.h,v 1.20 2022/03/03 06:09:03 riastradh Exp $	*/
+/*	$NetBSD: xhcivar.h,v 1.21 2022/03/13 11:30:04 riastradh Exp $	*/
 
 /*
  * Copyright (c) 2013 Jonathan A. Kollasch
@@ -96,6 +96,7 @@ struct xhci_softc {
 	struct usbd_bus sc_bus;		/* USB 3 bus */
 	struct usbd_bus sc_bus2;	/* USB 2 bus */
 
+	kmutex_t sc_rhlock;
 	kmutex_t sc_lock;
 	kmutex_t sc_intr_lock;
 

Reply via email to