Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/dev ehci(4): Serialize access to portsc registers.



details:   https://anonhg.NetBSD.org/src/rev/8d8190cf2b71
branches:  trunk
changeset: 363466:8d8190cf2b71
user:      riastradh <riastradh%NetBSD.org@localhost>
date:      Sun Mar 13 11:29:21 2022 +0000

description:
ehci(4): Serialize access to portsc registers.

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

diffstat:

 sys/dev/pci/ehci_pci.c |   5 +++--
 sys/dev/usb/ehci.c     |  41 ++++++++++++++++++++++++++++++-----------
 sys/dev/usb/ehcivar.h  |   3 ++-
 3 files changed, 35 insertions(+), 14 deletions(-)

diffs (175 lines):

diff -r 2865cf28909f -r 8d8190cf2b71 sys/dev/pci/ehci_pci.c
--- a/sys/dev/pci/ehci_pci.c    Sun Mar 13 11:29:10 2022 +0000
+++ b/sys/dev/pci/ehci_pci.c    Sun Mar 13 11:29:21 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ehci_pci.c,v 1.73 2021/12/22 21:45:02 skrll Exp $      */
+/*     $NetBSD: ehci_pci.c,v 1.74 2022/03/13 11:29:21 riastradh Exp $  */
 
 /*
  * Copyright (c) 2001, 2002 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ehci_pci.c,v 1.73 2021/12/22 21:45:02 skrll Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ehci_pci.c,v 1.74 2022/03/13 11:29:21 riastradh Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -325,6 +325,7 @@
 #if 1
        /* XXX created in ehci.c */
        if (sc->sc_init_state >= EHCI_INIT_INITED) {
+               mutex_destroy(&sc->sc.sc_rhlock);
                mutex_destroy(&sc->sc.sc_lock);
                mutex_destroy(&sc->sc.sc_intr_lock);
                softint_disestablish(sc->sc.sc_doorbell_si);
diff -r 2865cf28909f -r 8d8190cf2b71 sys/dev/usb/ehci.c
--- a/sys/dev/usb/ehci.c        Sun Mar 13 11:29:10 2022 +0000
+++ b/sys/dev/usb/ehci.c        Sun Mar 13 11:29:21 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ehci.c,v 1.308 2022/03/13 11:29:10 riastradh Exp $ */
+/*     $NetBSD: ehci.c,v 1.309 2022/03/13 11:29:21 riastradh Exp $ */
 
 /*
  * Copyright (c) 2004-2012,2016,2020 The NetBSD Foundation, Inc.
@@ -54,7 +54,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ehci.c,v 1.308 2022/03/13 11:29:10 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ehci.c,v 1.309 2022/03/13 11:29:21 riastradh Exp $");
 
 #include "ohci.h"
 #include "uhci.h"
@@ -412,6 +412,7 @@
        theehci = sc;
 #endif
 
+       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);
        cv_init(&sc->sc_doorbell, "ehcidb");
@@ -703,6 +704,7 @@
 fail1:
        softint_disestablish(sc->sc_doorbell_si);
        softint_disestablish(sc->sc_pcd_si);
+       mutex_destroy(&sc->sc_rhlock);
        mutex_destroy(&sc->sc_lock);
        mutex_destroy(&sc->sc_intr_lock);
 
@@ -1411,6 +1413,7 @@
        /* XXX destroyed in ehci_pci.c as it controls ehci_intr access */
        softint_disestablish(sc->sc_doorbell_si);
        softint_disestablish(sc->sc_pcd_si);
+       mutex_destroy(&sc->sc_rhlock);
        mutex_destroy(&sc->sc_lock);
        mutex_destroy(&sc->sc_intr_lock);
 #endif
@@ -1441,9 +1444,6 @@
  *
  * Note that this power handler isn't to be registered directly; the
  * bus glue needs to call out to it.
- *
- * XXX This should be serialized with ehci_roothub_ctrl's access to the
- * portsc registers.
  */
 bool
 ehci_suspend(device_t dv, const pmf_qual_t *qual)
@@ -1454,6 +1454,8 @@
 
        EHCIHIST_FUNC(); EHCIHIST_CALLED();
 
+       mutex_enter(&sc->sc_rhlock);
+
        for (i = 1; i <= sc->sc_noport; i++) {
                cmd = EOREAD4(sc, EHCI_PORTSC(i)) & ~EHCI_PS_CLEAR;
                if ((cmd & EHCI_PS_PO) == 0 && (cmd & EHCI_PS_PE) == EHCI_PS_PE)
@@ -1488,6 +1490,8 @@
        if (hcr != EHCI_STS_HCH)
                printf("%s: config timeout\n", device_xname(dv));
 
+       mutex_exit(&sc->sc_rhlock);
+
        return true;
 }
 
@@ -1500,6 +1504,8 @@
 
        EHCIHIST_FUNC(); EHCIHIST_CALLED();
 
+       mutex_enter(&sc->sc_rhlock);
+
        /* restore things in case the bios sucks */
        EOWRITE4(sc, EHCI_CTRLDSSEGMENT, 0);
        EOWRITE4(sc, EHCI_PERIODICLISTBASE, DMAADDR(&sc->sc_fldma, 0));
@@ -1545,6 +1551,8 @@
        if (hcr == EHCI_STS_HCH)
                printf("%s: config timeout\n", device_xname(dv));
 
+       mutex_exit(&sc->sc_rhlock);
+
        return true;
 }
 
@@ -2375,8 +2383,8 @@
 
 /***********/
 
-Static int
-ehci_roothub_ctrl(struct usbd_bus *bus, usb_device_request_t *req,
+static int
+ehci_roothub_ctrl_locked(struct usbd_bus *bus, usb_device_request_t *req,
     void *buf, int buflen)
 {
        ehci_softc_t *sc = EHCI_BUS2SC(bus);
@@ -2389,10 +2397,7 @@
 
        EHCIHIST_FUNC(); EHCIHIST_CALLED();
 
-       /*
-        * XXX This should be serialized with ehci_suspend/resume's
-        * access to the portsc registers.
-        */
+       KASSERT(mutex_owned(&sc->sc_rhlock));
 
        if (sc->sc_dying)
                return -1;
@@ -2667,6 +2672,20 @@
        return totlen;
 }
 
+Static int
+ehci_roothub_ctrl(struct usbd_bus *bus, usb_device_request_t *req,
+    void *buf, int buflen)
+{
+       struct ehci_softc *sc = EHCI_BUS2SC(bus);
+       int actlen;
+
+       mutex_enter(&sc->sc_rhlock);
+       actlen = ehci_roothub_ctrl_locked(bus, req, buf, buflen);
+       mutex_exit(&sc->sc_rhlock);
+
+       return actlen;
+}
+
 /*
  * Handle ehci hand-off in early boot vs RB_ASKNAME/RB_SINGLE.
  *
diff -r 2865cf28909f -r 8d8190cf2b71 sys/dev/usb/ehcivar.h
--- a/sys/dev/usb/ehcivar.h     Sun Mar 13 11:29:10 2022 +0000
+++ b/sys/dev/usb/ehcivar.h     Sun Mar 13 11:29:21 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ehcivar.h,v 1.50 2022/03/13 11:29:10 riastradh Exp $ */
+/*     $NetBSD: ehcivar.h,v 1.51 2022/03/13 11:29:21 riastradh Exp $ */
 
 /*
  * Copyright (c) 2001 The NetBSD Foundation, Inc.
@@ -162,6 +162,7 @@
 
 typedef struct ehci_softc {
        device_t sc_dev;
+       kmutex_t sc_rhlock;
        kmutex_t sc_lock;
        kmutex_t sc_intr_lock;
        kcondvar_t sc_doorbell;



Home | Main Index | Thread Index | Old Index