From 6d011946c556fcea72449f09bb3d95502db6b3b6 Mon Sep 17 00:00:00 2001 From: Kristof Provost Date: Sun, 15 Jan 2017 10:21:25 +0000 Subject: [PATCH] arswitch: Ensure the lock is always held when calling arswitch_modifyreg() arswitch_setled() and a number of _global_setup functions did not acquire the lock before calling arswitch_modifyreg(). With WITNESS enabled this would instantly panic. Discovered on a TPLink-3600: ("panic: mutex arswitch not owned at sys/dev/etherswitch/arswitch/arswitch_reg.c:236") Reviewed by: adrian, kan Differential Revision: https://reviews.freebsd.org/D9187 --- sys/dev/etherswitch/arswitch/arswitch.c | 8 +++++++- sys/dev/etherswitch/arswitch/arswitch_7240.c | 4 ++++ sys/dev/etherswitch/arswitch/arswitch_8316.c | 3 +++ sys/dev/etherswitch/arswitch/arswitch_8327.c | 3 +++ sys/dev/etherswitch/arswitch/arswitch_9340.c | 4 ++++ 5 files changed, 21 insertions(+), 1 deletion(-) diff --git a/sys/dev/etherswitch/arswitch/arswitch.c b/sys/dev/etherswitch/arswitch/arswitch.c index 09055632da0e..f98d84acaafc 100644 --- a/sys/dev/etherswitch/arswitch/arswitch.c +++ b/sys/dev/etherswitch/arswitch/arswitch.c @@ -845,6 +845,7 @@ static int arswitch_setled(struct arswitch_softc *sc, int phy, int led, int style) { int shift; + int err; if (phy < 0 || phy > sc->numphys) return EINVAL; @@ -852,10 +853,15 @@ arswitch_setled(struct arswitch_softc *sc, int phy, int led, int style) if (style < 0 || style > ETHERSWITCH_PORT_LED_MAX) return (EINVAL); + ARSWITCH_LOCK(sc); + shift = ar8327_led_mapping[phy][led].shift; - return (arswitch_modifyreg(sc->sc_dev, + err = (arswitch_modifyreg(sc->sc_dev, ar8327_led_mapping[phy][led].reg, 0x03 << shift, led_pattern_table[style] << shift)); + ARSWITCH_UNLOCK(sc); + + return (err); } static void diff --git a/sys/dev/etherswitch/arswitch/arswitch_7240.c b/sys/dev/etherswitch/arswitch/arswitch_7240.c index 01ea9b3e9c1b..d8f51c2784d1 100644 --- a/sys/dev/etherswitch/arswitch/arswitch_7240.c +++ b/sys/dev/etherswitch/arswitch/arswitch_7240.c @@ -81,6 +81,8 @@ static int ar7240_hw_global_setup(struct arswitch_softc *sc) { + ARSWITCH_LOCK(sc); + /* Enable CPU port; disable mirror port */ arswitch_writereg(sc->sc_dev, AR8X16_REG_CPU_PORT, AR8X16_CPU_PORT_EN | AR8X16_CPU_MIRROR_DIS); @@ -103,6 +105,8 @@ ar7240_hw_global_setup(struct arswitch_softc *sc) arswitch_modifyreg(sc->sc_dev, AR8X16_REG_SERVICE_TAG, AR8X16_SERVICE_TAG_MASK, 0); + ARSWITCH_UNLOCK(sc); + return (0); } diff --git a/sys/dev/etherswitch/arswitch/arswitch_8316.c b/sys/dev/etherswitch/arswitch/arswitch_8316.c index 54d6618bd805..bd46657f8859 100644 --- a/sys/dev/etherswitch/arswitch/arswitch_8316.c +++ b/sys/dev/etherswitch/arswitch/arswitch_8316.c @@ -127,6 +127,8 @@ static int ar8316_hw_global_setup(struct arswitch_softc *sc) { + ARSWITCH_LOCK(sc); + arswitch_writereg(sc->sc_dev, 0x38, AR8X16_MAGIC); /* Enable CPU port and disable mirror port. */ @@ -156,6 +158,7 @@ ar8316_hw_global_setup(struct arswitch_softc *sc) arswitch_modifyreg(sc->sc_dev, AR8X16_REG_SERVICE_TAG, AR8X16_SERVICE_TAG_MASK, 0); + ARSWITCH_UNLOCK(sc); return (0); } diff --git a/sys/dev/etherswitch/arswitch/arswitch_8327.c b/sys/dev/etherswitch/arswitch/arswitch_8327.c index 013688f1cce4..ab26707e300e 100644 --- a/sys/dev/etherswitch/arswitch/arswitch_8327.c +++ b/sys/dev/etherswitch/arswitch/arswitch_8327.c @@ -708,6 +708,8 @@ ar8327_hw_global_setup(struct arswitch_softc *sc) { uint32_t t; + ARSWITCH_LOCK(sc); + /* enable CPU port and disable mirror port */ t = AR8327_FWD_CTRL0_CPU_PORT_EN | AR8327_FWD_CTRL0_MIRROR_PORT; @@ -741,6 +743,7 @@ ar8327_hw_global_setup(struct arswitch_softc *sc) /* GMAC0 (CPU), GMAC1..5 (PHYs), GMAC6 (CPU) */ sc->info.es_nports = 7; + ARSWITCH_UNLOCK(sc); return (0); } diff --git a/sys/dev/etherswitch/arswitch/arswitch_9340.c b/sys/dev/etherswitch/arswitch/arswitch_9340.c index 89f6caa58c28..fd3ab223843f 100644 --- a/sys/dev/etherswitch/arswitch/arswitch_9340.c +++ b/sys/dev/etherswitch/arswitch/arswitch_9340.c @@ -81,6 +81,8 @@ static int ar9340_hw_global_setup(struct arswitch_softc *sc) { + ARSWITCH_LOCK(sc); + /* Enable CPU port; disable mirror port */ arswitch_writereg(sc->sc_dev, AR8X16_REG_CPU_PORT, AR8X16_CPU_PORT_EN | AR8X16_CPU_MIRROR_DIS); @@ -142,6 +144,7 @@ ar9340_hw_global_setup(struct arswitch_softc *sc) } else { device_printf(sc->sc_dev, "%s: need is_gmii or is_mii set\n", __func__); + ARSWITCH_UNLOCK(sc); return (ENXIO); } @@ -163,6 +166,7 @@ ar9340_hw_global_setup(struct arswitch_softc *sc) /* Settle time */ DELAY(1000); + ARSWITCH_UNLOCK(sc); return (0); }