Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 
0003 /* Copyright (c) 2014-2018, The Linux Foundation. All rights reserved.
0004  * Copyright (C) 2018-2021 Linaro Ltd.
0005  */
0006 
0007 #include <linux/errno.h>
0008 #include <linux/if_arp.h>
0009 #include <linux/netdevice.h>
0010 #include <linux/skbuff.h>
0011 #include <linux/if_rmnet.h>
0012 #include <linux/etherdevice.h>
0013 #include <net/pkt_sched.h>
0014 #include <linux/pm_runtime.h>
0015 #include <linux/remoteproc/qcom_rproc.h>
0016 
0017 #include "ipa.h"
0018 #include "ipa_data.h"
0019 #include "ipa_endpoint.h"
0020 #include "ipa_table.h"
0021 #include "ipa_mem.h"
0022 #include "ipa_modem.h"
0023 #include "ipa_smp2p.h"
0024 #include "ipa_qmi.h"
0025 #include "ipa_uc.h"
0026 #include "ipa_power.h"
0027 
0028 #define IPA_NETDEV_NAME     "rmnet_ipa%d"
0029 #define IPA_NETDEV_TAILROOM 0   /* for padding by mux layer */
0030 #define IPA_NETDEV_TIMEOUT  10  /* seconds */
0031 
0032 enum ipa_modem_state {
0033     IPA_MODEM_STATE_STOPPED = 0,
0034     IPA_MODEM_STATE_STARTING,
0035     IPA_MODEM_STATE_RUNNING,
0036     IPA_MODEM_STATE_STOPPING,
0037 };
0038 
0039 /**
0040  * struct ipa_priv - IPA network device private data
0041  * @ipa:    IPA pointer
0042  * @work:   Work structure used to wake the modem netdev TX queue
0043  */
0044 struct ipa_priv {
0045     struct ipa *ipa;
0046     struct work_struct work;
0047 };
0048 
0049 /** ipa_open() - Opens the modem network interface */
0050 static int ipa_open(struct net_device *netdev)
0051 {
0052     struct ipa_priv *priv = netdev_priv(netdev);
0053     struct ipa *ipa = priv->ipa;
0054     struct device *dev;
0055     int ret;
0056 
0057     dev = &ipa->pdev->dev;
0058     ret = pm_runtime_get_sync(dev);
0059     if (ret < 0)
0060         goto err_power_put;
0061 
0062     ret = ipa_endpoint_enable_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]);
0063     if (ret)
0064         goto err_power_put;
0065 
0066     ret = ipa_endpoint_enable_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_RX]);
0067     if (ret)
0068         goto err_disable_tx;
0069 
0070     netif_start_queue(netdev);
0071 
0072     pm_runtime_mark_last_busy(dev);
0073     (void)pm_runtime_put_autosuspend(dev);
0074 
0075     return 0;
0076 
0077 err_disable_tx:
0078     ipa_endpoint_disable_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]);
0079 err_power_put:
0080     pm_runtime_put_noidle(dev);
0081 
0082     return ret;
0083 }
0084 
0085 /** ipa_stop() - Stops the modem network interface. */
0086 static int ipa_stop(struct net_device *netdev)
0087 {
0088     struct ipa_priv *priv = netdev_priv(netdev);
0089     struct ipa *ipa = priv->ipa;
0090     struct device *dev;
0091     int ret;
0092 
0093     dev = &ipa->pdev->dev;
0094     ret = pm_runtime_get_sync(dev);
0095     if (ret < 0)
0096         goto out_power_put;
0097 
0098     netif_stop_queue(netdev);
0099 
0100     ipa_endpoint_disable_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_RX]);
0101     ipa_endpoint_disable_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]);
0102 out_power_put:
0103     pm_runtime_mark_last_busy(dev);
0104     (void)pm_runtime_put_autosuspend(dev);
0105 
0106     return 0;
0107 }
0108 
0109 /** ipa_start_xmit() - Transmits an skb.
0110  * @skb: skb to be transmitted
0111  * @dev: network device
0112  *
0113  * Return codes:
0114  * NETDEV_TX_OK: Success
0115  * NETDEV_TX_BUSY: Error while transmitting the skb. Try again later
0116  */
0117 static netdev_tx_t
0118 ipa_start_xmit(struct sk_buff *skb, struct net_device *netdev)
0119 {
0120     struct net_device_stats *stats = &netdev->stats;
0121     struct ipa_priv *priv = netdev_priv(netdev);
0122     struct ipa_endpoint *endpoint;
0123     struct ipa *ipa = priv->ipa;
0124     u32 skb_len = skb->len;
0125     struct device *dev;
0126     int ret;
0127 
0128     if (!skb_len)
0129         goto err_drop_skb;
0130 
0131     endpoint = ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX];
0132     if (endpoint->config.qmap && skb->protocol != htons(ETH_P_MAP))
0133         goto err_drop_skb;
0134 
0135     /* The hardware must be powered for us to transmit */
0136     dev = &ipa->pdev->dev;
0137     ret = pm_runtime_get(dev);
0138     if (ret < 1) {
0139         /* If a resume won't happen, just drop the packet */
0140         if (ret < 0 && ret != -EINPROGRESS) {
0141             ipa_power_modem_queue_active(ipa);
0142             pm_runtime_put_noidle(dev);
0143             goto err_drop_skb;
0144         }
0145 
0146         /* No power (yet).  Stop the network stack from transmitting
0147          * until we're resumed; ipa_modem_resume() arranges for the
0148          * TX queue to be started again.
0149          */
0150         ipa_power_modem_queue_stop(ipa);
0151 
0152         pm_runtime_put_noidle(dev);
0153 
0154         return NETDEV_TX_BUSY;
0155     }
0156 
0157     ipa_power_modem_queue_active(ipa);
0158 
0159     ret = ipa_endpoint_skb_tx(endpoint, skb);
0160 
0161     pm_runtime_mark_last_busy(dev);
0162     (void)pm_runtime_put_autosuspend(dev);
0163 
0164     if (ret) {
0165         if (ret != -E2BIG)
0166             return NETDEV_TX_BUSY;
0167         goto err_drop_skb;
0168     }
0169 
0170     stats->tx_packets++;
0171     stats->tx_bytes += skb_len;
0172 
0173     return NETDEV_TX_OK;
0174 
0175 err_drop_skb:
0176     dev_kfree_skb_any(skb);
0177     stats->tx_dropped++;
0178 
0179     return NETDEV_TX_OK;
0180 }
0181 
0182 void ipa_modem_skb_rx(struct net_device *netdev, struct sk_buff *skb)
0183 {
0184     struct net_device_stats *stats = &netdev->stats;
0185 
0186     if (skb) {
0187         skb->dev = netdev;
0188         skb->protocol = htons(ETH_P_MAP);
0189         stats->rx_packets++;
0190         stats->rx_bytes += skb->len;
0191 
0192         (void)netif_receive_skb(skb);
0193     } else {
0194         stats->rx_dropped++;
0195     }
0196 }
0197 
0198 static const struct net_device_ops ipa_modem_ops = {
0199     .ndo_open   = ipa_open,
0200     .ndo_stop   = ipa_stop,
0201     .ndo_start_xmit = ipa_start_xmit,
0202 };
0203 
0204 /** ipa_modem_netdev_setup() - netdev setup function for the modem */
0205 static void ipa_modem_netdev_setup(struct net_device *netdev)
0206 {
0207     netdev->netdev_ops = &ipa_modem_ops;
0208 
0209     netdev->header_ops = NULL;
0210     netdev->type = ARPHRD_RAWIP;
0211     netdev->hard_header_len = 0;
0212     netdev->min_header_len = ETH_HLEN;
0213     netdev->min_mtu = ETH_MIN_MTU;
0214     netdev->max_mtu = IPA_MTU;
0215     netdev->mtu = netdev->max_mtu;
0216     netdev->addr_len = 0;
0217     netdev->tx_queue_len = DEFAULT_TX_QUEUE_LEN;
0218     netdev->flags &= ~(IFF_BROADCAST | IFF_MULTICAST);
0219     netdev->priv_flags |= IFF_TX_SKB_SHARING;
0220     eth_broadcast_addr(netdev->broadcast);
0221 
0222     /* The endpoint is configured for QMAP */
0223     netdev->needed_headroom = sizeof(struct rmnet_map_header);
0224     netdev->needed_tailroom = IPA_NETDEV_TAILROOM;
0225     netdev->watchdog_timeo = IPA_NETDEV_TIMEOUT * HZ;
0226     netdev->hw_features = NETIF_F_SG;
0227 }
0228 
0229 /** ipa_modem_suspend() - suspend callback
0230  * @netdev: Network device
0231  *
0232  * Suspend the modem's endpoints.
0233  */
0234 void ipa_modem_suspend(struct net_device *netdev)
0235 {
0236     struct ipa_priv *priv = netdev_priv(netdev);
0237     struct ipa *ipa = priv->ipa;
0238 
0239     if (!(netdev->flags & IFF_UP))
0240         return;
0241 
0242     ipa_endpoint_suspend_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_RX]);
0243     ipa_endpoint_suspend_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]);
0244 }
0245 
0246 /**
0247  * ipa_modem_wake_queue_work() - enable modem netdev queue
0248  * @work:   Work structure
0249  *
0250  * Re-enable transmit on the modem network device.  This is called
0251  * in (power management) work queue context, scheduled when resuming
0252  * the modem.  We can't enable the queue directly in ipa_modem_resume()
0253  * because transmits restart the instant the queue is awakened; but the
0254  * device power state won't be ACTIVE until *after* ipa_modem_resume()
0255  * returns.
0256  */
0257 static void ipa_modem_wake_queue_work(struct work_struct *work)
0258 {
0259     struct ipa_priv *priv = container_of(work, struct ipa_priv, work);
0260 
0261     ipa_power_modem_queue_wake(priv->ipa);
0262 }
0263 
0264 /** ipa_modem_resume() - resume callback for runtime_pm
0265  * @dev: pointer to device
0266  *
0267  * Resume the modem's endpoints.
0268  */
0269 void ipa_modem_resume(struct net_device *netdev)
0270 {
0271     struct ipa_priv *priv = netdev_priv(netdev);
0272     struct ipa *ipa = priv->ipa;
0273 
0274     if (!(netdev->flags & IFF_UP))
0275         return;
0276 
0277     ipa_endpoint_resume_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]);
0278     ipa_endpoint_resume_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_RX]);
0279 
0280     /* Arrange for the TX queue to be restarted */
0281     (void)queue_pm_work(&priv->work);
0282 }
0283 
0284 int ipa_modem_start(struct ipa *ipa)
0285 {
0286     enum ipa_modem_state state;
0287     struct net_device *netdev;
0288     struct ipa_priv *priv;
0289     int ret;
0290 
0291     /* Only attempt to start the modem if it's stopped */
0292     state = atomic_cmpxchg(&ipa->modem_state, IPA_MODEM_STATE_STOPPED,
0293                    IPA_MODEM_STATE_STARTING);
0294 
0295     /* Silently ignore attempts when running, or when changing state */
0296     if (state != IPA_MODEM_STATE_STOPPED)
0297         return 0;
0298 
0299     netdev = alloc_netdev(sizeof(struct ipa_priv), IPA_NETDEV_NAME,
0300                   NET_NAME_UNKNOWN, ipa_modem_netdev_setup);
0301     if (!netdev) {
0302         ret = -ENOMEM;
0303         goto out_set_state;
0304     }
0305 
0306     SET_NETDEV_DEV(netdev, &ipa->pdev->dev);
0307     priv = netdev_priv(netdev);
0308     priv->ipa = ipa;
0309     INIT_WORK(&priv->work, ipa_modem_wake_queue_work);
0310     ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]->netdev = netdev;
0311     ipa->name_map[IPA_ENDPOINT_AP_MODEM_RX]->netdev = netdev;
0312     ipa->modem_netdev = netdev;
0313 
0314     ret = register_netdev(netdev);
0315     if (ret) {
0316         ipa->modem_netdev = NULL;
0317         ipa->name_map[IPA_ENDPOINT_AP_MODEM_RX]->netdev = NULL;
0318         ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]->netdev = NULL;
0319         free_netdev(netdev);
0320     }
0321 
0322 out_set_state:
0323     if (ret)
0324         atomic_set(&ipa->modem_state, IPA_MODEM_STATE_STOPPED);
0325     else
0326         atomic_set(&ipa->modem_state, IPA_MODEM_STATE_RUNNING);
0327     smp_mb__after_atomic();
0328 
0329     return ret;
0330 }
0331 
0332 int ipa_modem_stop(struct ipa *ipa)
0333 {
0334     struct net_device *netdev = ipa->modem_netdev;
0335     enum ipa_modem_state state;
0336 
0337     /* Only attempt to stop the modem if it's running */
0338     state = atomic_cmpxchg(&ipa->modem_state, IPA_MODEM_STATE_RUNNING,
0339                    IPA_MODEM_STATE_STOPPING);
0340 
0341     /* Silently ignore attempts when already stopped */
0342     if (state == IPA_MODEM_STATE_STOPPED)
0343         return 0;
0344 
0345     /* If we're somewhere between stopped and starting, we're busy */
0346     if (state != IPA_MODEM_STATE_RUNNING)
0347         return -EBUSY;
0348 
0349     /* Clean up the netdev and endpoints if it was started */
0350     if (netdev) {
0351         struct ipa_priv *priv = netdev_priv(netdev);
0352 
0353         cancel_work_sync(&priv->work);
0354         /* If it was opened, stop it first */
0355         if (netdev->flags & IFF_UP)
0356             (void)ipa_stop(netdev);
0357         unregister_netdev(netdev);
0358         ipa->modem_netdev = NULL;
0359         ipa->name_map[IPA_ENDPOINT_AP_MODEM_RX]->netdev = NULL;
0360         ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]->netdev = NULL;
0361         free_netdev(netdev);
0362     }
0363 
0364     atomic_set(&ipa->modem_state, IPA_MODEM_STATE_STOPPED);
0365     smp_mb__after_atomic();
0366 
0367     return 0;
0368 }
0369 
0370 /* Treat a "clean" modem stop the same as a crash */
0371 static void ipa_modem_crashed(struct ipa *ipa)
0372 {
0373     struct device *dev = &ipa->pdev->dev;
0374     int ret;
0375 
0376     /* Prevent the modem from triggering a call to ipa_setup() */
0377     ipa_smp2p_irq_disable_setup(ipa);
0378 
0379     ret = pm_runtime_get_sync(dev);
0380     if (ret < 0) {
0381         dev_err(dev, "error %d getting power to handle crash\n", ret);
0382         goto out_power_put;
0383     }
0384 
0385     ipa_endpoint_modem_pause_all(ipa, true);
0386 
0387     ipa_endpoint_modem_hol_block_clear_all(ipa);
0388 
0389     ipa_table_reset(ipa, true);
0390 
0391     ret = ipa_table_hash_flush(ipa);
0392     if (ret)
0393         dev_err(dev, "error %d flushing hash caches\n", ret);
0394 
0395     ret = ipa_endpoint_modem_exception_reset_all(ipa);
0396     if (ret)
0397         dev_err(dev, "error %d resetting exception endpoint\n", ret);
0398 
0399     ipa_endpoint_modem_pause_all(ipa, false);
0400 
0401     ret = ipa_modem_stop(ipa);
0402     if (ret)
0403         dev_err(dev, "error %d stopping modem\n", ret);
0404 
0405     /* Now prepare for the next modem boot */
0406     ret = ipa_mem_zero_modem(ipa);
0407     if (ret)
0408         dev_err(dev, "error %d zeroing modem memory regions\n", ret);
0409 
0410 out_power_put:
0411     pm_runtime_mark_last_busy(dev);
0412     (void)pm_runtime_put_autosuspend(dev);
0413 }
0414 
0415 static int ipa_modem_notify(struct notifier_block *nb, unsigned long action,
0416                 void *data)
0417 {
0418     struct ipa *ipa = container_of(nb, struct ipa, nb);
0419     struct qcom_ssr_notify_data *notify_data = data;
0420     struct device *dev = &ipa->pdev->dev;
0421 
0422     switch (action) {
0423     case QCOM_SSR_BEFORE_POWERUP:
0424         dev_info(dev, "received modem starting event\n");
0425         ipa_uc_power(ipa);
0426         ipa_smp2p_notify_reset(ipa);
0427         break;
0428 
0429     case QCOM_SSR_AFTER_POWERUP:
0430         dev_info(dev, "received modem running event\n");
0431         break;
0432 
0433     case QCOM_SSR_BEFORE_SHUTDOWN:
0434         dev_info(dev, "received modem %s event\n",
0435              notify_data->crashed ? "crashed" : "stopping");
0436         if (ipa->setup_complete)
0437             ipa_modem_crashed(ipa);
0438         break;
0439 
0440     case QCOM_SSR_AFTER_SHUTDOWN:
0441         dev_info(dev, "received modem offline event\n");
0442         break;
0443 
0444     default:
0445         dev_err(dev, "received unrecognized event %lu\n", action);
0446         break;
0447     }
0448 
0449     return NOTIFY_OK;
0450 }
0451 
0452 int ipa_modem_config(struct ipa *ipa)
0453 {
0454     void *notifier;
0455 
0456     ipa->nb.notifier_call = ipa_modem_notify;
0457 
0458     notifier = qcom_register_ssr_notifier("mpss", &ipa->nb);
0459     if (IS_ERR(notifier))
0460         return PTR_ERR(notifier);
0461 
0462     ipa->notifier = notifier;
0463 
0464     return 0;
0465 }
0466 
0467 void ipa_modem_deconfig(struct ipa *ipa)
0468 {
0469     struct device *dev = &ipa->pdev->dev;
0470     int ret;
0471 
0472     ret = qcom_unregister_ssr_notifier(ipa->notifier, &ipa->nb);
0473     if (ret)
0474         dev_err(dev, "error %d unregistering notifier", ret);
0475 
0476     ipa->notifier = NULL;
0477     memset(&ipa->nb, 0, sizeof(ipa->nb));
0478 }