]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
wifi: mt76: mt7915: add locking for accessing mapped registers
authorShayne Chen <shayne.chen@mediatek.com>
Tue, 15 Aug 2023 09:28:30 +0000 (17:28 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Sat, 13 Apr 2024 11:07:31 +0000 (13:07 +0200)
[ Upstream commit 0937f95ab07af6e663ae932d592f630d9eb591da ]

Sicne the mapping is global, mapped register access needs to be protected
against concurrent access, otherwise a race condition might cause the reads
or writes to go towards the wrong register

Signed-off-by: Shayne Chen <shayne.chen@mediatek.com>
Signed-off-by: Peter Chiu <chui-hao.chiu@mediatek.com>
Signed-off-by: Felix Fietkau <nbd@nbd.name>
Signed-off-by: Sasha Levin <sashal@kernel.org>
drivers/net/wireless/mediatek/mt76/mt7915/mmio.c
drivers/net/wireless/mediatek/mt76/mt7915/mt7915.h

index f4ad7219f94f4d89f1358253ef9dc6530df03a93..a306a42777d789ced9182d0aedf5178c0e4a4bca 100644 (file)
@@ -490,6 +490,11 @@ static u32 __mt7915_reg_addr(struct mt7915_dev *dev, u32 addr)
                return dev->reg.map[i].maps + ofs;
        }
 
+       return 0;
+}
+
+static u32 __mt7915_reg_remap_addr(struct mt7915_dev *dev, u32 addr)
+{
        if ((addr >= MT_INFRA_BASE && addr < MT_WFSYS0_PHY_START) ||
            (addr >= MT_WFSYS0_PHY_START && addr < MT_WFSYS1_PHY_START) ||
            (addr >= MT_WFSYS1_PHY_START && addr <= MT_WFSYS1_PHY_END))
@@ -514,15 +519,30 @@ void mt7915_memcpy_fromio(struct mt7915_dev *dev, void *buf, u32 offset,
 {
        u32 addr = __mt7915_reg_addr(dev, offset);
 
-       memcpy_fromio(buf, dev->mt76.mmio.regs + addr, len);
+       if (addr) {
+               memcpy_fromio(buf, dev->mt76.mmio.regs + addr, len);
+               return;
+       }
+
+       spin_lock_bh(&dev->reg_lock);
+       memcpy_fromio(buf, dev->mt76.mmio.regs +
+                          __mt7915_reg_remap_addr(dev, offset), len);
+       spin_unlock_bh(&dev->reg_lock);
 }
 
 static u32 mt7915_rr(struct mt76_dev *mdev, u32 offset)
 {
        struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76);
-       u32 addr = __mt7915_reg_addr(dev, offset);
+       u32 addr = __mt7915_reg_addr(dev, offset), val;
 
-       return dev->bus_ops->rr(mdev, addr);
+       if (addr)
+               return dev->bus_ops->rr(mdev, addr);
+
+       spin_lock_bh(&dev->reg_lock);
+       val = dev->bus_ops->rr(mdev, __mt7915_reg_remap_addr(dev, offset));
+       spin_unlock_bh(&dev->reg_lock);
+
+       return val;
 }
 
 static void mt7915_wr(struct mt76_dev *mdev, u32 offset, u32 val)
@@ -530,7 +550,14 @@ static void mt7915_wr(struct mt76_dev *mdev, u32 offset, u32 val)
        struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76);
        u32 addr = __mt7915_reg_addr(dev, offset);
 
-       dev->bus_ops->wr(mdev, addr, val);
+       if (addr) {
+               dev->bus_ops->wr(mdev, addr, val);
+               return;
+       }
+
+       spin_lock_bh(&dev->reg_lock);
+       dev->bus_ops->wr(mdev, __mt7915_reg_remap_addr(dev, offset), val);
+       spin_unlock_bh(&dev->reg_lock);
 }
 
 static u32 mt7915_rmw(struct mt76_dev *mdev, u32 offset, u32 mask, u32 val)
@@ -538,7 +565,14 @@ static u32 mt7915_rmw(struct mt76_dev *mdev, u32 offset, u32 mask, u32 val)
        struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76);
        u32 addr = __mt7915_reg_addr(dev, offset);
 
-       return dev->bus_ops->rmw(mdev, addr, mask, val);
+       if (addr)
+               return dev->bus_ops->rmw(mdev, addr, mask, val);
+
+       spin_lock_bh(&dev->reg_lock);
+       val = dev->bus_ops->rmw(mdev, __mt7915_reg_remap_addr(dev, offset), mask, val);
+       spin_unlock_bh(&dev->reg_lock);
+
+       return val;
 }
 
 #ifdef CONFIG_NET_MEDIATEK_SOC_WED
@@ -813,6 +847,7 @@ static int mt7915_mmio_init(struct mt76_dev *mdev,
 
        dev = container_of(mdev, struct mt7915_dev, mt76);
        mt76_mmio_init(&dev->mt76, mem_base);
+       spin_lock_init(&dev->reg_lock);
 
        switch (device_id) {
        case 0x7915:
index 21984e9723709def2d61e41c08c0feb5e5c1e057..e192211d4b23eec5c271543068fa445e7b7aba8c 100644 (file)
@@ -287,6 +287,7 @@ struct mt7915_dev {
 
        struct list_head sta_rc_list;
        struct list_head twt_list;
+       spinlock_t reg_lock;
 
        u32 hw_pattern;