Skip to content

Commit

Permalink
mt76: mt7921: fix a race between mt7921_mcu_drv_pmctrl and mt7921_mcu…
Browse files Browse the repository at this point in the history
…_fw_pmctrl

Introduce a mutex in order to avoid a race between mt7921_mcu_drv_pmctrl and
mt7921_mcu_fw_pmctrl routines since they are run two independent works

Fixes: 1d8efc7 ("mt76: mt7921: introduce Runtime PM support")
Signed-off-by: Lorenzo Bianconi <lorenzo@kernel.org>
Signed-off-by: Felix Fietkau <nbd@nbd.name>
  • Loading branch information
Lorenzo Bianconi authored and Felix Fietkau committed Apr 21, 2021
1 parent d5a2abb commit fad90e4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
1 change: 1 addition & 0 deletions drivers/net/wireless/mediatek/mt76/mt76_connac.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ struct mt76_connac_pm {

struct work_struct wake_work;
struct completion wake_cmpl;
struct mutex mutex;

struct delayed_work ps_work;
unsigned long last_activity;
Expand Down
1 change: 1 addition & 0 deletions drivers/net/wireless/mediatek/mt76/mt7921/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ int mt7921_register_device(struct mt7921_dev *dev)

INIT_DELAYED_WORK(&dev->pm.ps_work, mt7921_pm_power_save_work);
INIT_WORK(&dev->pm.wake_work, mt7921_pm_wake_work);
mutex_init(&dev->pm.mutex);
init_completion(&dev->pm.wake_cmpl);
spin_lock_init(&dev->pm.txq_lock);
set_bit(MT76_STATE_PM, &dev->mphy.state);
Expand Down
34 changes: 24 additions & 10 deletions drivers/net/wireless/mediatek/mt76/mt7921/mcu.c
Original file line number Diff line number Diff line change
Expand Up @@ -1267,9 +1267,11 @@ int mt7921_mcu_set_bss_pm(struct mt7921_dev *dev, struct ieee80211_vif *vif,
int mt7921_mcu_drv_pmctrl(struct mt7921_dev *dev)
{
struct mt76_phy *mphy = &dev->mt76.phy;
int i;
int i, err = 0;

if (!test_and_clear_bit(MT76_STATE_PM, &mphy->state))
mutex_lock(&dev->pm.mutex);

if (!test_bit(MT76_STATE_PM, &mphy->state))
goto out;

for (i = 0; i < MT7921_DRV_OWN_RETRY_COUNT; i++) {
Expand All @@ -1281,23 +1283,30 @@ int mt7921_mcu_drv_pmctrl(struct mt7921_dev *dev)

if (i == MT7921_DRV_OWN_RETRY_COUNT) {
dev_err(dev->mt76.dev, "driver own failed\n");
mt7921_reset(&dev->mt76);
return -EIO;
err = -EIO;
goto out;
}
clear_bit(MT76_STATE_PM, &mphy->state);

out:
dev->pm.last_activity = jiffies;
mutex_unlock(&dev->pm.mutex);

return 0;
if (err)
mt7921_reset(&dev->mt76);

return err;
}

int mt7921_mcu_fw_pmctrl(struct mt7921_dev *dev)
{
struct mt76_phy *mphy = &dev->mt76.phy;
int i;
int i, err = 0;

mutex_lock(&dev->pm.mutex);

if (test_and_set_bit(MT76_STATE_PM, &mphy->state))
return 0;
goto out;

for (i = 0; i < MT7921_DRV_OWN_RETRY_COUNT; i++) {
mt76_wr(dev, MT_CONN_ON_LPCTL, PCIE_LPCR_HOST_SET_OWN);
Expand All @@ -1308,11 +1317,16 @@ int mt7921_mcu_fw_pmctrl(struct mt7921_dev *dev)

if (i == MT7921_DRV_OWN_RETRY_COUNT) {
dev_err(dev->mt76.dev, "firmware own failed\n");
mt7921_reset(&dev->mt76);
return -EIO;
clear_bit(MT76_STATE_PM, &mphy->state);
err = -EIO;
}
out:
mutex_unlock(&dev->pm.mutex);

return 0;
if (err)
mt7921_reset(&dev->mt76);

return err;
}

void
Expand Down

0 comments on commit fad90e4

Please sign in to comment.