The mhi_wwan_ctrl and mhi_wwan_mbim drivers do not coordinate with runtime PM, which allows the underlying MHI controller to be runtime suspended while transmit, receive, or RX buffer refill operations are in progress. This can lead to stalled transfers or failed queueing once runtime PM is enabled in the MHI core. Enable runtime PM during probe and take explicit references around TX, RX, and buffer management operations so the controller remains active for the duration of each critical path. Signed-off-by: Krishna Chaitanya Chundru --- drivers/net/wwan/mhi_wwan_ctrl.c | 60 +++++++++++++++++++++++++++++++++++++++- drivers/net/wwan/mhi_wwan_mbim.c | 44 ++++++++++++++++++++++++++++- 2 files changed, 102 insertions(+), 2 deletions(-) diff --git a/drivers/net/wwan/mhi_wwan_ctrl.c b/drivers/net/wwan/mhi_wwan_ctrl.c index fa73861db6ad..0fe0f24c0df4 100644 --- a/drivers/net/wwan/mhi_wwan_ctrl.c +++ b/drivers/net/wwan/mhi_wwan_ctrl.c @@ -4,6 +4,7 @@ #include #include #include +#include #include /* MHI wwan flags */ @@ -79,6 +80,13 @@ static void mhi_wwan_ctrl_refill_work(struct work_struct *work) { struct mhi_wwan_dev *mhiwwan = container_of(work, struct mhi_wwan_dev, rx_refill); struct mhi_device *mhi_dev = mhiwwan->mhi_dev; + int err; + + err = pm_runtime_resume_and_get(&mhi_dev->dev); + if (err) { + dev_err(&mhi_dev->dev, "pm_runtime_resume_and_get failed %d\n", err); + return; + } while (mhi_wwan_rx_budget_dec(mhiwwan)) { struct sk_buff *skb; @@ -102,17 +110,27 @@ static void mhi_wwan_ctrl_refill_work(struct work_struct *work) break; } } + pm_runtime_put(&mhi_dev->dev); } static int mhi_wwan_ctrl_start(struct wwan_port *port) { struct mhi_wwan_dev *mhiwwan = wwan_port_get_drvdata(port); + struct mhi_device *mhi_dev = mhiwwan->mhi_dev; int ret; + ret = pm_runtime_resume_and_get(&mhi_dev->dev); + if (ret) { + dev_err(&mhi_dev->dev, "pm_runtime_resume_and_get failed %d\n", ret); + return ret; + } + /* Start mhi device's channel(s) */ ret = mhi_prepare_for_transfer(mhiwwan->mhi_dev); - if (ret) + if (ret) { + pm_runtime_put(&mhi_dev->dev); return ret; + } /* Don't allocate more buffers than MHI channel queue size */ mhiwwan->rx_budget = mhi_get_free_desc_count(mhiwwan->mhi_dev, DMA_FROM_DEVICE); @@ -123,12 +141,15 @@ static int mhi_wwan_ctrl_start(struct wwan_port *port) mhi_wwan_ctrl_refill_work(&mhiwwan->rx_refill); } + pm_runtime_put(&mhi_dev->dev); return 0; } static void mhi_wwan_ctrl_stop(struct wwan_port *port) { struct mhi_wwan_dev *mhiwwan = wwan_port_get_drvdata(port); + struct mhi_device *mhi_dev = mhiwwan->mhi_dev; + int err; spin_lock_bh(&mhiwwan->rx_lock); clear_bit(MHI_WWAN_RX_REFILL, &mhiwwan->flags); @@ -136,12 +157,20 @@ static void mhi_wwan_ctrl_stop(struct wwan_port *port) cancel_work_sync(&mhiwwan->rx_refill); + err = pm_runtime_resume_and_get(&mhi_dev->dev); + if (err) + dev_err(&mhi_dev->dev, "pm_runtime_resume_and_get failed %d\n", err); + mhi_unprepare_from_transfer(mhiwwan->mhi_dev); + + if (!err) + pm_runtime_put(&mhi_dev->dev); } static int mhi_wwan_ctrl_tx(struct wwan_port *port, struct sk_buff *skb) { struct mhi_wwan_dev *mhiwwan = wwan_port_get_drvdata(port); + struct mhi_device *mhi_dev = mhiwwan->mhi_dev; int ret; if (skb->len > mhiwwan->mtu) @@ -150,6 +179,12 @@ static int mhi_wwan_ctrl_tx(struct wwan_port *port, struct sk_buff *skb) if (!test_bit(MHI_WWAN_UL_CAP, &mhiwwan->flags)) return -EOPNOTSUPP; + ret = pm_runtime_resume_and_get(&mhi_dev->dev); + if (ret) { + dev_err(&mhi_dev->dev, "pm_runtime_resume_and_get failed %d\n", ret); + return ret; + } + /* Queue the packet for MHI transfer and check fullness of the queue */ spin_lock_bh(&mhiwwan->tx_lock); ret = mhi_queue_skb(mhiwwan->mhi_dev, DMA_TO_DEVICE, skb, skb->len, MHI_EOT); @@ -157,6 +192,9 @@ static int mhi_wwan_ctrl_tx(struct wwan_port *port, struct sk_buff *skb) wwan_port_txoff(port); spin_unlock_bh(&mhiwwan->tx_lock); + if (ret) + pm_runtime_put(&mhi_dev->dev); + return ret; } @@ -179,6 +217,8 @@ static void mhi_ul_xfer_cb(struct mhi_device *mhi_dev, /* MHI core has done with the buffer, release it */ consume_skb(skb); + pm_runtime_put(&mhi_dev->dev); + /* There is likely new slot available in the MHI queue, re-allow TX */ spin_lock_bh(&mhiwwan->tx_lock); if (!mhi_queue_is_full(mhiwwan->mhi_dev, DMA_TO_DEVICE)) @@ -217,11 +257,26 @@ static int mhi_wwan_ctrl_probe(struct mhi_device *mhi_dev, struct mhi_controller *cntrl = mhi_dev->mhi_cntrl; struct mhi_wwan_dev *mhiwwan; struct wwan_port *port; + int err; mhiwwan = kzalloc_obj(*mhiwwan); if (!mhiwwan) return -ENOMEM; + pm_runtime_no_callbacks(&mhi_dev->dev); + err = devm_pm_runtime_set_active_enabled(&mhi_dev->dev); + if (err) { + kfree(mhiwwan); + return err; + } + + err = pm_runtime_resume_and_get(&mhi_dev->dev); + if (err) { + dev_err(&mhi_dev->dev, "pm_runtime_resume_and_get failed %d\n", err); + kfree(mhiwwan); + return err; + } + mhiwwan->mhi_dev = mhi_dev; mhiwwan->mtu = MHI_WWAN_MAX_MTU; INIT_WORK(&mhiwwan->rx_refill, mhi_wwan_ctrl_refill_work); @@ -240,11 +295,14 @@ static int mhi_wwan_ctrl_probe(struct mhi_device *mhi_dev, &wwan_pops, NULL, mhiwwan); if (IS_ERR(port)) { kfree(mhiwwan); + pm_runtime_put(&mhi_dev->dev); return PTR_ERR(port); } mhiwwan->wwan_port = port; + pm_runtime_put(&mhi_dev->dev); + return 0; }; diff --git a/drivers/net/wwan/mhi_wwan_mbim.c b/drivers/net/wwan/mhi_wwan_mbim.c index 1d7e3ad900c1..56e660ecfcb4 100644 --- a/drivers/net/wwan/mhi_wwan_mbim.c +++ b/drivers/net/wwan/mhi_wwan_mbim.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -153,9 +154,18 @@ static netdev_tx_t mhi_mbim_ndo_xmit(struct sk_buff *skb, struct net_device *nde { struct mhi_mbim_link *link = wwan_netdev_drvpriv(ndev); struct mhi_mbim_context *mbim = link->mbim; + struct mhi_device *mhi_dev = mbim->mdev; unsigned long flags; int err = -ENOMEM; + err = pm_runtime_get(&mhi_dev->dev); + if (err < 0 && err != -EINPROGRESS) { + dev_err(&mhi_dev->dev, "pm_runtime_get Failed %d\n", err); + pm_runtime_put_noidle(&mhi_dev->dev); + dev_kfree_skb_any(skb); + return NETDEV_TX_OK; + } + /* Serialize MHI channel queuing and MBIM seq */ spin_lock_irqsave(&mbim->tx_lock, flags); @@ -184,6 +194,7 @@ static netdev_tx_t mhi_mbim_ndo_xmit(struct sk_buff *skb, struct net_device *nde return NETDEV_TX_OK; exit_drop: + pm_runtime_put(&mhi_dev->dev); u64_stats_update_begin(&link->tx_syncp); u64_stats_inc(&link->tx_dropped); u64_stats_update_end(&link->tx_syncp); @@ -396,6 +407,10 @@ static void mhi_net_rx_refill_work(struct work_struct *work) struct mhi_device *mdev = mbim->mdev; int err; + err = pm_runtime_get(&mdev->dev); + if (err < 0 && err != -EINPROGRESS) + dev_err(&mdev->dev, "pm_runtime_get Failed %d\n", err); + while (!mhi_queue_is_full(mdev, DMA_FROM_DEVICE)) { struct sk_buff *skb = alloc_skb(mbim->mru, GFP_KERNEL); @@ -415,6 +430,8 @@ static void mhi_net_rx_refill_work(struct work_struct *work) cond_resched(); } + pm_runtime_put(&mdev->dev); + /* If we're still starved of rx buffers, reschedule later */ if (mhi_get_free_desc_count(mdev, DMA_FROM_DEVICE) == mbim->rx_queue_sz) schedule_delayed_work(&mbim->rx_refill, HZ / 2); @@ -501,6 +518,7 @@ static void mhi_mbim_ul_callback(struct mhi_device *mhi_dev, /* MHI layer stopping/resetting the UL channel */ if (mhi_res->transaction_status == -ENOTCONN) { u64_stats_update_end(&link->tx_syncp); + pm_runtime_put(&mhi_dev->dev); return; } @@ -511,6 +529,8 @@ static void mhi_mbim_ul_callback(struct mhi_device *mhi_dev, } u64_stats_update_end(&link->tx_syncp); + pm_runtime_put(&mhi_dev->dev); + if (netif_queue_stopped(ndev) && !mhi_queue_is_full(mbim->mdev, DMA_TO_DEVICE)) netif_wake_queue(ndev); } @@ -614,6 +634,17 @@ static int mhi_mbim_probe(struct mhi_device *mhi_dev, const struct mhi_device_id if (!mbim) return -ENOMEM; + pm_runtime_no_callbacks(&mhi_dev->dev); + err = devm_pm_runtime_set_active_enabled(&mhi_dev->dev); + if (err) + return err; + + err = pm_runtime_get(&mhi_dev->dev); + if (err < 0 && err != -EINPROGRESS) { + dev_err(&mhi_dev->dev, "pm_runtime_get Failed %d\n", err); + return err; + } + spin_lock_init(&mbim->tx_lock); dev_set_drvdata(&mhi_dev->dev, mbim); mbim->mdev = mhi_dev; @@ -623,8 +654,12 @@ static int mhi_mbim_probe(struct mhi_device *mhi_dev, const struct mhi_device_id /* Start MHI channels */ err = mhi_prepare_for_transfer(mhi_dev); - if (err) + if (err) { + pm_runtime_put(&mhi_dev->dev); return err; + } + + pm_runtime_put(&mhi_dev->dev); /* Number of transfer descriptors determines size of the queue */ mbim->rx_queue_sz = mhi_get_free_desc_count(mhi_dev, DMA_FROM_DEVICE); @@ -637,12 +672,19 @@ static void mhi_mbim_remove(struct mhi_device *mhi_dev) { struct mhi_mbim_context *mbim = dev_get_drvdata(&mhi_dev->dev); struct mhi_controller *cntrl = mhi_dev->mhi_cntrl; + int err; + + err = pm_runtime_get(&mhi_dev->dev); + if (err < 0 && err != -EINPROGRESS) + dev_err(&mhi_dev->dev, "pm_runtime_get Failed %d\n", err); mhi_unprepare_from_transfer(mhi_dev); cancel_delayed_work_sync(&mbim->rx_refill); wwan_unregister_ops(&cntrl->mhi_dev->dev); kfree_skb(mbim->skbagg_head); dev_set_drvdata(&mhi_dev->dev, NULL); + + pm_runtime_put(&mhi_dev->dev); } static const struct mhi_device_id mhi_mbim_id_table[] = { -- 2.34.1