Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* MHI Network driver - Network over MHI bus
0003  *
0004  * Copyright (C) 2020 Linaro Ltd <loic.poulain@linaro.org>
0005  */
0006 
0007 #include <linux/if_arp.h>
0008 #include <linux/mhi.h>
0009 #include <linux/mod_devicetable.h>
0010 #include <linux/module.h>
0011 #include <linux/netdevice.h>
0012 #include <linux/skbuff.h>
0013 #include <linux/u64_stats_sync.h>
0014 
0015 #define MHI_NET_MIN_MTU     ETH_MIN_MTU
0016 #define MHI_NET_MAX_MTU     0xffff
0017 #define MHI_NET_DEFAULT_MTU 0x4000
0018 
0019 struct mhi_net_stats {
0020     u64_stats_t rx_packets;
0021     u64_stats_t rx_bytes;
0022     u64_stats_t rx_errors;
0023     u64_stats_t tx_packets;
0024     u64_stats_t tx_bytes;
0025     u64_stats_t tx_errors;
0026     u64_stats_t tx_dropped;
0027     struct u64_stats_sync tx_syncp;
0028     struct u64_stats_sync rx_syncp;
0029 };
0030 
0031 struct mhi_net_dev {
0032     struct mhi_device *mdev;
0033     struct net_device *ndev;
0034     struct sk_buff *skbagg_head;
0035     struct sk_buff *skbagg_tail;
0036     struct delayed_work rx_refill;
0037     struct mhi_net_stats stats;
0038     u32 rx_queue_sz;
0039     int msg_enable;
0040     unsigned int mru;
0041 };
0042 
0043 struct mhi_device_info {
0044     const char *netname;
0045 };
0046 
0047 static int mhi_ndo_open(struct net_device *ndev)
0048 {
0049     struct mhi_net_dev *mhi_netdev = netdev_priv(ndev);
0050 
0051     /* Feed the rx buffer pool */
0052     schedule_delayed_work(&mhi_netdev->rx_refill, 0);
0053 
0054     /* Carrier is established via out-of-band channel (e.g. qmi) */
0055     netif_carrier_on(ndev);
0056 
0057     netif_start_queue(ndev);
0058 
0059     return 0;
0060 }
0061 
0062 static int mhi_ndo_stop(struct net_device *ndev)
0063 {
0064     struct mhi_net_dev *mhi_netdev = netdev_priv(ndev);
0065 
0066     netif_stop_queue(ndev);
0067     netif_carrier_off(ndev);
0068     cancel_delayed_work_sync(&mhi_netdev->rx_refill);
0069 
0070     return 0;
0071 }
0072 
0073 static netdev_tx_t mhi_ndo_xmit(struct sk_buff *skb, struct net_device *ndev)
0074 {
0075     struct mhi_net_dev *mhi_netdev = netdev_priv(ndev);
0076     struct mhi_device *mdev = mhi_netdev->mdev;
0077     int err;
0078 
0079     err = mhi_queue_skb(mdev, DMA_TO_DEVICE, skb, skb->len, MHI_EOT);
0080     if (unlikely(err)) {
0081         net_err_ratelimited("%s: Failed to queue TX buf (%d)\n",
0082                     ndev->name, err);
0083         dev_kfree_skb_any(skb);
0084         goto exit_drop;
0085     }
0086 
0087     if (mhi_queue_is_full(mdev, DMA_TO_DEVICE))
0088         netif_stop_queue(ndev);
0089 
0090     return NETDEV_TX_OK;
0091 
0092 exit_drop:
0093     u64_stats_update_begin(&mhi_netdev->stats.tx_syncp);
0094     u64_stats_inc(&mhi_netdev->stats.tx_dropped);
0095     u64_stats_update_end(&mhi_netdev->stats.tx_syncp);
0096 
0097     return NETDEV_TX_OK;
0098 }
0099 
0100 static void mhi_ndo_get_stats64(struct net_device *ndev,
0101                 struct rtnl_link_stats64 *stats)
0102 {
0103     struct mhi_net_dev *mhi_netdev = netdev_priv(ndev);
0104     unsigned int start;
0105 
0106     do {
0107         start = u64_stats_fetch_begin_irq(&mhi_netdev->stats.rx_syncp);
0108         stats->rx_packets = u64_stats_read(&mhi_netdev->stats.rx_packets);
0109         stats->rx_bytes = u64_stats_read(&mhi_netdev->stats.rx_bytes);
0110         stats->rx_errors = u64_stats_read(&mhi_netdev->stats.rx_errors);
0111     } while (u64_stats_fetch_retry_irq(&mhi_netdev->stats.rx_syncp, start));
0112 
0113     do {
0114         start = u64_stats_fetch_begin_irq(&mhi_netdev->stats.tx_syncp);
0115         stats->tx_packets = u64_stats_read(&mhi_netdev->stats.tx_packets);
0116         stats->tx_bytes = u64_stats_read(&mhi_netdev->stats.tx_bytes);
0117         stats->tx_errors = u64_stats_read(&mhi_netdev->stats.tx_errors);
0118         stats->tx_dropped = u64_stats_read(&mhi_netdev->stats.tx_dropped);
0119     } while (u64_stats_fetch_retry_irq(&mhi_netdev->stats.tx_syncp, start));
0120 }
0121 
0122 static const struct net_device_ops mhi_netdev_ops = {
0123     .ndo_open               = mhi_ndo_open,
0124     .ndo_stop               = mhi_ndo_stop,
0125     .ndo_start_xmit         = mhi_ndo_xmit,
0126     .ndo_get_stats64    = mhi_ndo_get_stats64,
0127 };
0128 
0129 static void mhi_net_setup(struct net_device *ndev)
0130 {
0131     ndev->header_ops = NULL;  /* No header */
0132     ndev->type = ARPHRD_RAWIP;
0133     ndev->hard_header_len = 0;
0134     ndev->addr_len = 0;
0135     ndev->flags = IFF_POINTOPOINT | IFF_NOARP;
0136     ndev->netdev_ops = &mhi_netdev_ops;
0137     ndev->mtu = MHI_NET_DEFAULT_MTU;
0138     ndev->min_mtu = MHI_NET_MIN_MTU;
0139     ndev->max_mtu = MHI_NET_MAX_MTU;
0140     ndev->tx_queue_len = 1000;
0141 }
0142 
0143 static struct sk_buff *mhi_net_skb_agg(struct mhi_net_dev *mhi_netdev,
0144                        struct sk_buff *skb)
0145 {
0146     struct sk_buff *head = mhi_netdev->skbagg_head;
0147     struct sk_buff *tail = mhi_netdev->skbagg_tail;
0148 
0149     /* This is non-paged skb chaining using frag_list */
0150     if (!head) {
0151         mhi_netdev->skbagg_head = skb;
0152         return skb;
0153     }
0154 
0155     if (!skb_shinfo(head)->frag_list)
0156         skb_shinfo(head)->frag_list = skb;
0157     else
0158         tail->next = skb;
0159 
0160     head->len += skb->len;
0161     head->data_len += skb->len;
0162     head->truesize += skb->truesize;
0163 
0164     mhi_netdev->skbagg_tail = skb;
0165 
0166     return mhi_netdev->skbagg_head;
0167 }
0168 
0169 static void mhi_net_dl_callback(struct mhi_device *mhi_dev,
0170                 struct mhi_result *mhi_res)
0171 {
0172     struct mhi_net_dev *mhi_netdev = dev_get_drvdata(&mhi_dev->dev);
0173     struct sk_buff *skb = mhi_res->buf_addr;
0174     int free_desc_count;
0175 
0176     free_desc_count = mhi_get_free_desc_count(mhi_dev, DMA_FROM_DEVICE);
0177 
0178     if (unlikely(mhi_res->transaction_status)) {
0179         switch (mhi_res->transaction_status) {
0180         case -EOVERFLOW:
0181             /* Packet can not fit in one MHI buffer and has been
0182              * split over multiple MHI transfers, do re-aggregation.
0183              * That usually means the device side MTU is larger than
0184              * the host side MTU/MRU. Since this is not optimal,
0185              * print a warning (once).
0186              */
0187             netdev_warn_once(mhi_netdev->ndev,
0188                      "Fragmented packets received, fix MTU?\n");
0189             skb_put(skb, mhi_res->bytes_xferd);
0190             mhi_net_skb_agg(mhi_netdev, skb);
0191             break;
0192         case -ENOTCONN:
0193             /* MHI layer stopping/resetting the DL channel */
0194             dev_kfree_skb_any(skb);
0195             return;
0196         default:
0197             /* Unknown error, simply drop */
0198             dev_kfree_skb_any(skb);
0199             u64_stats_update_begin(&mhi_netdev->stats.rx_syncp);
0200             u64_stats_inc(&mhi_netdev->stats.rx_errors);
0201             u64_stats_update_end(&mhi_netdev->stats.rx_syncp);
0202         }
0203     } else {
0204         skb_put(skb, mhi_res->bytes_xferd);
0205 
0206         if (mhi_netdev->skbagg_head) {
0207             /* Aggregate the final fragment */
0208             skb = mhi_net_skb_agg(mhi_netdev, skb);
0209             mhi_netdev->skbagg_head = NULL;
0210         }
0211 
0212         switch (skb->data[0] & 0xf0) {
0213         case 0x40:
0214             skb->protocol = htons(ETH_P_IP);
0215             break;
0216         case 0x60:
0217             skb->protocol = htons(ETH_P_IPV6);
0218             break;
0219         default:
0220             skb->protocol = htons(ETH_P_MAP);
0221             break;
0222         }
0223 
0224         u64_stats_update_begin(&mhi_netdev->stats.rx_syncp);
0225         u64_stats_inc(&mhi_netdev->stats.rx_packets);
0226         u64_stats_add(&mhi_netdev->stats.rx_bytes, skb->len);
0227         u64_stats_update_end(&mhi_netdev->stats.rx_syncp);
0228         __netif_rx(skb);
0229     }
0230 
0231     /* Refill if RX buffers queue becomes low */
0232     if (free_desc_count >= mhi_netdev->rx_queue_sz / 2)
0233         schedule_delayed_work(&mhi_netdev->rx_refill, 0);
0234 }
0235 
0236 static void mhi_net_ul_callback(struct mhi_device *mhi_dev,
0237                 struct mhi_result *mhi_res)
0238 {
0239     struct mhi_net_dev *mhi_netdev = dev_get_drvdata(&mhi_dev->dev);
0240     struct net_device *ndev = mhi_netdev->ndev;
0241     struct mhi_device *mdev = mhi_netdev->mdev;
0242     struct sk_buff *skb = mhi_res->buf_addr;
0243 
0244     /* Hardware has consumed the buffer, so free the skb (which is not
0245      * freed by the MHI stack) and perform accounting.
0246      */
0247     dev_consume_skb_any(skb);
0248 
0249     u64_stats_update_begin(&mhi_netdev->stats.tx_syncp);
0250     if (unlikely(mhi_res->transaction_status)) {
0251         /* MHI layer stopping/resetting the UL channel */
0252         if (mhi_res->transaction_status == -ENOTCONN) {
0253             u64_stats_update_end(&mhi_netdev->stats.tx_syncp);
0254             return;
0255         }
0256 
0257         u64_stats_inc(&mhi_netdev->stats.tx_errors);
0258     } else {
0259         u64_stats_inc(&mhi_netdev->stats.tx_packets);
0260         u64_stats_add(&mhi_netdev->stats.tx_bytes, mhi_res->bytes_xferd);
0261     }
0262     u64_stats_update_end(&mhi_netdev->stats.tx_syncp);
0263 
0264     if (netif_queue_stopped(ndev) && !mhi_queue_is_full(mdev, DMA_TO_DEVICE))
0265         netif_wake_queue(ndev);
0266 }
0267 
0268 static void mhi_net_rx_refill_work(struct work_struct *work)
0269 {
0270     struct mhi_net_dev *mhi_netdev = container_of(work, struct mhi_net_dev,
0271                               rx_refill.work);
0272     struct net_device *ndev = mhi_netdev->ndev;
0273     struct mhi_device *mdev = mhi_netdev->mdev;
0274     struct sk_buff *skb;
0275     unsigned int size;
0276     int err;
0277 
0278     size = mhi_netdev->mru ? mhi_netdev->mru : READ_ONCE(ndev->mtu);
0279 
0280     while (!mhi_queue_is_full(mdev, DMA_FROM_DEVICE)) {
0281         skb = netdev_alloc_skb(ndev, size);
0282         if (unlikely(!skb))
0283             break;
0284 
0285         err = mhi_queue_skb(mdev, DMA_FROM_DEVICE, skb, size, MHI_EOT);
0286         if (unlikely(err)) {
0287             net_err_ratelimited("%s: Failed to queue RX buf (%d)\n",
0288                         ndev->name, err);
0289             kfree_skb(skb);
0290             break;
0291         }
0292 
0293         /* Do not hog the CPU if rx buffers are consumed faster than
0294          * queued (unlikely).
0295          */
0296         cond_resched();
0297     }
0298 
0299     /* If we're still starved of rx buffers, reschedule later */
0300     if (mhi_get_free_desc_count(mdev, DMA_FROM_DEVICE) == mhi_netdev->rx_queue_sz)
0301         schedule_delayed_work(&mhi_netdev->rx_refill, HZ / 2);
0302 }
0303 
0304 static int mhi_net_newlink(struct mhi_device *mhi_dev, struct net_device *ndev)
0305 {
0306     struct mhi_net_dev *mhi_netdev;
0307     int err;
0308 
0309     mhi_netdev = netdev_priv(ndev);
0310 
0311     dev_set_drvdata(&mhi_dev->dev, mhi_netdev);
0312     mhi_netdev->ndev = ndev;
0313     mhi_netdev->mdev = mhi_dev;
0314     mhi_netdev->skbagg_head = NULL;
0315     mhi_netdev->mru = mhi_dev->mhi_cntrl->mru;
0316 
0317     INIT_DELAYED_WORK(&mhi_netdev->rx_refill, mhi_net_rx_refill_work);
0318     u64_stats_init(&mhi_netdev->stats.rx_syncp);
0319     u64_stats_init(&mhi_netdev->stats.tx_syncp);
0320 
0321     /* Start MHI channels */
0322     err = mhi_prepare_for_transfer(mhi_dev);
0323     if (err)
0324         return err;
0325 
0326     /* Number of transfer descriptors determines size of the queue */
0327     mhi_netdev->rx_queue_sz = mhi_get_free_desc_count(mhi_dev, DMA_FROM_DEVICE);
0328 
0329     err = register_netdev(ndev);
0330     if (err)
0331         return err;
0332 
0333     return 0;
0334 }
0335 
0336 static void mhi_net_dellink(struct mhi_device *mhi_dev, struct net_device *ndev)
0337 {
0338     struct mhi_net_dev *mhi_netdev = netdev_priv(ndev);
0339 
0340     unregister_netdev(ndev);
0341 
0342     mhi_unprepare_from_transfer(mhi_dev);
0343 
0344     kfree_skb(mhi_netdev->skbagg_head);
0345 
0346     dev_set_drvdata(&mhi_dev->dev, NULL);
0347 }
0348 
0349 static int mhi_net_probe(struct mhi_device *mhi_dev,
0350              const struct mhi_device_id *id)
0351 {
0352     const struct mhi_device_info *info = (struct mhi_device_info *)id->driver_data;
0353     struct net_device *ndev;
0354     int err;
0355 
0356     ndev = alloc_netdev(sizeof(struct mhi_net_dev), info->netname,
0357                 NET_NAME_PREDICTABLE, mhi_net_setup);
0358     if (!ndev)
0359         return -ENOMEM;
0360 
0361     SET_NETDEV_DEV(ndev, &mhi_dev->dev);
0362 
0363     err = mhi_net_newlink(mhi_dev, ndev);
0364     if (err) {
0365         free_netdev(ndev);
0366         return err;
0367     }
0368 
0369     return 0;
0370 }
0371 
0372 static void mhi_net_remove(struct mhi_device *mhi_dev)
0373 {
0374     struct mhi_net_dev *mhi_netdev = dev_get_drvdata(&mhi_dev->dev);
0375 
0376     mhi_net_dellink(mhi_dev, mhi_netdev->ndev);
0377 }
0378 
0379 static const struct mhi_device_info mhi_hwip0 = {
0380     .netname = "mhi_hwip%d",
0381 };
0382 
0383 static const struct mhi_device_info mhi_swip0 = {
0384     .netname = "mhi_swip%d",
0385 };
0386 
0387 static const struct mhi_device_id mhi_net_id_table[] = {
0388     /* Hardware accelerated data PATH (to modem IPA), protocol agnostic */
0389     { .chan = "IP_HW0", .driver_data = (kernel_ulong_t)&mhi_hwip0 },
0390     /* Software data PATH (to modem CPU) */
0391     { .chan = "IP_SW0", .driver_data = (kernel_ulong_t)&mhi_swip0 },
0392     {}
0393 };
0394 MODULE_DEVICE_TABLE(mhi, mhi_net_id_table);
0395 
0396 static struct mhi_driver mhi_net_driver = {
0397     .probe = mhi_net_probe,
0398     .remove = mhi_net_remove,
0399     .dl_xfer_cb = mhi_net_dl_callback,
0400     .ul_xfer_cb = mhi_net_ul_callback,
0401     .id_table = mhi_net_id_table,
0402     .driver = {
0403         .name = "mhi_net",
0404         .owner = THIS_MODULE,
0405     },
0406 };
0407 
0408 module_mhi_driver(mhi_net_driver);
0409 
0410 MODULE_AUTHOR("Loic Poulain <loic.poulain@linaro.org>");
0411 MODULE_DESCRIPTION("Network over MHI");
0412 MODULE_LICENSE("GPL v2");