]> git.itanic.dy.fi Git - linux-stable/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:10:00 +0000 (13:10 +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 3039f53e224546a406a2fc4cc0b2f8e07884d456..dceb505987b19841874d35a8814c5f81216a1bf0 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
@@ -707,6 +741,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 4727d9c7b11de037f076d1c5598a60f80659d8c8..6e79bc65f5a51bd387b3129a33d3a514dec8ea9c 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;