Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* MHI MBIM Network driver - Network/MBIM over MHI bus
0003  *
0004  * Copyright (C) 2021 Linaro Ltd <loic.poulain@linaro.org>
0005  *
0006  * This driver copy some code from cdc_ncm, which is:
0007  * Copyright (C) ST-Ericsson 2010-2012
0008  * and cdc_mbim, which is:
0009  * Copyright (c) 2012  Smith Micro Software, Inc.
0010  * Copyright (c) 2012  Bjørn Mork <bjorn@mork.no>
0011  *
0012  */
0013 
0014 #include <linux/ethtool.h>
0015 #include <linux/if_arp.h>
0016 #include <linux/if_vlan.h>
0017 #include <linux/ip.h>
0018 #include <linux/mhi.h>
0019 #include <linux/mii.h>
0020 #include <linux/mod_devicetable.h>
0021 #include <linux/module.h>
0022 #include <linux/netdevice.h>
0023 #include <linux/skbuff.h>
0024 #include <linux/u64_stats_sync.h>
0025 #include <linux/usb.h>
0026 #include <linux/usb/cdc.h>
0027 #include <linux/usb/usbnet.h>
0028 #include <linux/usb/cdc_ncm.h>
0029 #include <linux/wwan.h>
0030 
0031 /* 3500 allows to optimize skb allocation, the skbs will basically fit in
0032  * one 4K page. Large MBIM packets will simply be split over several MHI
0033  * transfers and chained by the MHI net layer (zerocopy).
0034  */
0035 #define MHI_DEFAULT_MRU 3500
0036 
0037 #define MHI_MBIM_DEFAULT_MTU 1500
0038 #define MHI_MAX_BUF_SZ 0xffff
0039 
0040 #define MBIM_NDP16_SIGN_MASK 0x00ffffff
0041 
0042 #define MHI_MBIM_LINK_HASH_SIZE 8
0043 #define LINK_HASH(session) ((session) % MHI_MBIM_LINK_HASH_SIZE)
0044 
0045 struct mhi_mbim_link {
0046     struct mhi_mbim_context *mbim;
0047     struct net_device *ndev;
0048     unsigned int session;
0049 
0050     /* stats */
0051     u64_stats_t rx_packets;
0052     u64_stats_t rx_bytes;
0053     u64_stats_t rx_errors;
0054     u64_stats_t tx_packets;
0055     u64_stats_t tx_bytes;
0056     u64_stats_t tx_errors;
0057     u64_stats_t tx_dropped;
0058     struct u64_stats_sync tx_syncp;
0059     struct u64_stats_sync rx_syncp;
0060 
0061     struct hlist_node hlnode;
0062 };
0063 
0064 struct mhi_mbim_context {
0065     struct mhi_device *mdev;
0066     struct sk_buff *skbagg_head;
0067     struct sk_buff *skbagg_tail;
0068     unsigned int mru;
0069     u32 rx_queue_sz;
0070     u16 rx_seq;
0071     u16 tx_seq;
0072     struct delayed_work rx_refill;
0073     spinlock_t tx_lock;
0074     struct hlist_head link_list[MHI_MBIM_LINK_HASH_SIZE];
0075 };
0076 
0077 struct mbim_tx_hdr {
0078     struct usb_cdc_ncm_nth16 nth16;
0079     struct usb_cdc_ncm_ndp16 ndp16;
0080     struct usb_cdc_ncm_dpe16 dpe16[2];
0081 } __packed;
0082 
0083 static struct mhi_mbim_link *mhi_mbim_get_link_rcu(struct mhi_mbim_context *mbim,
0084                            unsigned int session)
0085 {
0086     struct mhi_mbim_link *link;
0087 
0088     hlist_for_each_entry_rcu(link, &mbim->link_list[LINK_HASH(session)], hlnode) {
0089         if (link->session == session)
0090             return link;
0091     }
0092 
0093     return NULL;
0094 }
0095 
0096 static struct sk_buff *mbim_tx_fixup(struct sk_buff *skb, unsigned int session,
0097                      u16 tx_seq)
0098 {
0099     unsigned int dgram_size = skb->len;
0100     struct usb_cdc_ncm_nth16 *nth16;
0101     struct usb_cdc_ncm_ndp16 *ndp16;
0102     struct mbim_tx_hdr *mbim_hdr;
0103 
0104     /* Only one NDP is sent, containing the IP packet (no aggregation) */
0105 
0106     /* Ensure we have enough headroom for crafting MBIM header */
0107     if (skb_cow_head(skb, sizeof(struct mbim_tx_hdr))) {
0108         dev_kfree_skb_any(skb);
0109         return NULL;
0110     }
0111 
0112     mbim_hdr = skb_push(skb, sizeof(struct mbim_tx_hdr));
0113 
0114     /* Fill NTB header */
0115     nth16 = &mbim_hdr->nth16;
0116     nth16->dwSignature = cpu_to_le32(USB_CDC_NCM_NTH16_SIGN);
0117     nth16->wHeaderLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_nth16));
0118     nth16->wSequence = cpu_to_le16(tx_seq);
0119     nth16->wBlockLength = cpu_to_le16(skb->len);
0120     nth16->wNdpIndex = cpu_to_le16(sizeof(struct usb_cdc_ncm_nth16));
0121 
0122     /* Fill the unique NDP */
0123     ndp16 = &mbim_hdr->ndp16;
0124     ndp16->dwSignature = cpu_to_le32(USB_CDC_MBIM_NDP16_IPS_SIGN | (session << 24));
0125     ndp16->wLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_ndp16)
0126                     + sizeof(struct usb_cdc_ncm_dpe16) * 2);
0127     ndp16->wNextNdpIndex = 0;
0128 
0129     /* Datagram follows the mbim header */
0130     ndp16->dpe16[0].wDatagramIndex = cpu_to_le16(sizeof(struct mbim_tx_hdr));
0131     ndp16->dpe16[0].wDatagramLength = cpu_to_le16(dgram_size);
0132 
0133     /* null termination */
0134     ndp16->dpe16[1].wDatagramIndex = 0;
0135     ndp16->dpe16[1].wDatagramLength = 0;
0136 
0137     return skb;
0138 }
0139 
0140 static netdev_tx_t mhi_mbim_ndo_xmit(struct sk_buff *skb, struct net_device *ndev)
0141 {
0142     struct mhi_mbim_link *link = wwan_netdev_drvpriv(ndev);
0143     struct mhi_mbim_context *mbim = link->mbim;
0144     unsigned long flags;
0145     int err = -ENOMEM;
0146 
0147     /* Serialize MHI channel queuing and MBIM seq */
0148     spin_lock_irqsave(&mbim->tx_lock, flags);
0149 
0150     skb = mbim_tx_fixup(skb, link->session, mbim->tx_seq);
0151     if (unlikely(!skb))
0152         goto exit_unlock;
0153 
0154     err = mhi_queue_skb(mbim->mdev, DMA_TO_DEVICE, skb, skb->len, MHI_EOT);
0155 
0156     if (mhi_queue_is_full(mbim->mdev, DMA_TO_DEVICE))
0157         netif_stop_queue(ndev);
0158 
0159     if (!err)
0160         mbim->tx_seq++;
0161 
0162 exit_unlock:
0163     spin_unlock_irqrestore(&mbim->tx_lock, flags);
0164 
0165     if (unlikely(err)) {
0166         net_err_ratelimited("%s: Failed to queue TX buf (%d)\n",
0167                     ndev->name, err);
0168         dev_kfree_skb_any(skb);
0169         goto exit_drop;
0170     }
0171 
0172     return NETDEV_TX_OK;
0173 
0174 exit_drop:
0175     u64_stats_update_begin(&link->tx_syncp);
0176     u64_stats_inc(&link->tx_dropped);
0177     u64_stats_update_end(&link->tx_syncp);
0178 
0179     return NETDEV_TX_OK;
0180 }
0181 
0182 static int mbim_rx_verify_nth16(struct mhi_mbim_context *mbim, struct sk_buff *skb)
0183 {
0184     struct usb_cdc_ncm_nth16 *nth16;
0185     int len;
0186 
0187     if (skb->len < sizeof(struct usb_cdc_ncm_nth16) +
0188             sizeof(struct usb_cdc_ncm_ndp16)) {
0189         net_err_ratelimited("frame too short\n");
0190         return -EINVAL;
0191     }
0192 
0193     nth16 = (struct usb_cdc_ncm_nth16 *)skb->data;
0194 
0195     if (nth16->dwSignature != cpu_to_le32(USB_CDC_NCM_NTH16_SIGN)) {
0196         net_err_ratelimited("invalid NTH16 signature <%#010x>\n",
0197                     le32_to_cpu(nth16->dwSignature));
0198         return -EINVAL;
0199     }
0200 
0201     /* No limit on the block length, except the size of the data pkt */
0202     len = le16_to_cpu(nth16->wBlockLength);
0203     if (len > skb->len) {
0204         net_err_ratelimited("NTB does not fit into the skb %u/%u\n",
0205                     len, skb->len);
0206         return -EINVAL;
0207     }
0208 
0209     if (mbim->rx_seq + 1 != le16_to_cpu(nth16->wSequence) &&
0210         (mbim->rx_seq || le16_to_cpu(nth16->wSequence)) &&
0211         !(mbim->rx_seq == 0xffff && !le16_to_cpu(nth16->wSequence))) {
0212         net_err_ratelimited("sequence number glitch prev=%d curr=%d\n",
0213                     mbim->rx_seq, le16_to_cpu(nth16->wSequence));
0214     }
0215     mbim->rx_seq = le16_to_cpu(nth16->wSequence);
0216 
0217     return le16_to_cpu(nth16->wNdpIndex);
0218 }
0219 
0220 static int mbim_rx_verify_ndp16(struct sk_buff *skb, struct usb_cdc_ncm_ndp16 *ndp16)
0221 {
0222     int ret;
0223 
0224     if (le16_to_cpu(ndp16->wLength) < USB_CDC_NCM_NDP16_LENGTH_MIN) {
0225         net_err_ratelimited("invalid DPT16 length <%u>\n",
0226                     le16_to_cpu(ndp16->wLength));
0227         return -EINVAL;
0228     }
0229 
0230     ret = ((le16_to_cpu(ndp16->wLength) - sizeof(struct usb_cdc_ncm_ndp16))
0231             / sizeof(struct usb_cdc_ncm_dpe16));
0232     ret--; /* Last entry is always a NULL terminator */
0233 
0234     if (sizeof(struct usb_cdc_ncm_ndp16) +
0235          ret * sizeof(struct usb_cdc_ncm_dpe16) > skb->len) {
0236         net_err_ratelimited("Invalid nframes = %d\n", ret);
0237         return -EINVAL;
0238     }
0239 
0240     return ret;
0241 }
0242 
0243 static void mhi_mbim_rx(struct mhi_mbim_context *mbim, struct sk_buff *skb)
0244 {
0245     int ndpoffset;
0246 
0247     /* Check NTB header and retrieve first NDP offset */
0248     ndpoffset = mbim_rx_verify_nth16(mbim, skb);
0249     if (ndpoffset < 0) {
0250         net_err_ratelimited("mbim: Incorrect NTB header\n");
0251         goto error;
0252     }
0253 
0254     /* Process each NDP */
0255     while (1) {
0256         struct usb_cdc_ncm_ndp16 ndp16;
0257         struct usb_cdc_ncm_dpe16 dpe16;
0258         struct mhi_mbim_link *link;
0259         int nframes, n, dpeoffset;
0260         unsigned int session;
0261 
0262         if (skb_copy_bits(skb, ndpoffset, &ndp16, sizeof(ndp16))) {
0263             net_err_ratelimited("mbim: Incorrect NDP offset (%u)\n",
0264                         ndpoffset);
0265             goto error;
0266         }
0267 
0268         /* Check NDP header and retrieve number of datagrams */
0269         nframes = mbim_rx_verify_ndp16(skb, &ndp16);
0270         if (nframes < 0) {
0271             net_err_ratelimited("mbim: Incorrect NDP16\n");
0272             goto error;
0273         }
0274 
0275          /* Only IP data type supported, no DSS in MHI context */
0276         if ((ndp16.dwSignature & cpu_to_le32(MBIM_NDP16_SIGN_MASK))
0277                 != cpu_to_le32(USB_CDC_MBIM_NDP16_IPS_SIGN)) {
0278             net_err_ratelimited("mbim: Unsupported NDP type\n");
0279             goto next_ndp;
0280         }
0281 
0282         session = (le32_to_cpu(ndp16.dwSignature) & ~MBIM_NDP16_SIGN_MASK) >> 24;
0283 
0284         rcu_read_lock();
0285 
0286         link = mhi_mbim_get_link_rcu(mbim, session);
0287         if (!link) {
0288             net_err_ratelimited("mbim: bad packet session (%u)\n", session);
0289             goto unlock;
0290         }
0291 
0292         /* de-aggregate and deliver IP packets */
0293         dpeoffset = ndpoffset + sizeof(struct usb_cdc_ncm_ndp16);
0294         for (n = 0; n < nframes; n++, dpeoffset += sizeof(dpe16)) {
0295             u16 dgram_offset, dgram_len;
0296             struct sk_buff *skbn;
0297 
0298             if (skb_copy_bits(skb, dpeoffset, &dpe16, sizeof(dpe16)))
0299                 break;
0300 
0301             dgram_offset = le16_to_cpu(dpe16.wDatagramIndex);
0302             dgram_len = le16_to_cpu(dpe16.wDatagramLength);
0303 
0304             if (!dgram_offset || !dgram_len)
0305                 break; /* null terminator */
0306 
0307             skbn = netdev_alloc_skb(link->ndev, dgram_len);
0308             if (!skbn)
0309                 continue;
0310 
0311             skb_put(skbn, dgram_len);
0312             skb_copy_bits(skb, dgram_offset, skbn->data, dgram_len);
0313 
0314             switch (skbn->data[0] & 0xf0) {
0315             case 0x40:
0316                 skbn->protocol = htons(ETH_P_IP);
0317                 break;
0318             case 0x60:
0319                 skbn->protocol = htons(ETH_P_IPV6);
0320                 break;
0321             default:
0322                 net_err_ratelimited("%s: unknown protocol\n",
0323                             link->ndev->name);
0324                 dev_kfree_skb_any(skbn);
0325                 u64_stats_update_begin(&link->rx_syncp);
0326                 u64_stats_inc(&link->rx_errors);
0327                 u64_stats_update_end(&link->rx_syncp);
0328                 continue;
0329             }
0330 
0331             u64_stats_update_begin(&link->rx_syncp);
0332             u64_stats_inc(&link->rx_packets);
0333             u64_stats_add(&link->rx_bytes, skbn->len);
0334             u64_stats_update_end(&link->rx_syncp);
0335 
0336             netif_rx(skbn);
0337         }
0338 unlock:
0339         rcu_read_unlock();
0340 next_ndp:
0341         /* Other NDP to process? */
0342         ndpoffset = (int)le16_to_cpu(ndp16.wNextNdpIndex);
0343         if (!ndpoffset)
0344             break;
0345     }
0346 
0347     /* free skb */
0348     dev_consume_skb_any(skb);
0349     return;
0350 error:
0351     dev_kfree_skb_any(skb);
0352 }
0353 
0354 static struct sk_buff *mhi_net_skb_agg(struct mhi_mbim_context *mbim,
0355                        struct sk_buff *skb)
0356 {
0357     struct sk_buff *head = mbim->skbagg_head;
0358     struct sk_buff *tail = mbim->skbagg_tail;
0359 
0360     /* This is non-paged skb chaining using frag_list */
0361     if (!head) {
0362         mbim->skbagg_head = skb;
0363         return skb;
0364     }
0365 
0366     if (!skb_shinfo(head)->frag_list)
0367         skb_shinfo(head)->frag_list = skb;
0368     else
0369         tail->next = skb;
0370 
0371     head->len += skb->len;
0372     head->data_len += skb->len;
0373     head->truesize += skb->truesize;
0374 
0375     mbim->skbagg_tail = skb;
0376 
0377     return mbim->skbagg_head;
0378 }
0379 
0380 static void mhi_net_rx_refill_work(struct work_struct *work)
0381 {
0382     struct mhi_mbim_context *mbim = container_of(work, struct mhi_mbim_context,
0383                              rx_refill.work);
0384     struct mhi_device *mdev = mbim->mdev;
0385     int err;
0386 
0387     while (!mhi_queue_is_full(mdev, DMA_FROM_DEVICE)) {
0388         struct sk_buff *skb = alloc_skb(mbim->mru, GFP_KERNEL);
0389 
0390         if (unlikely(!skb))
0391             break;
0392 
0393         err = mhi_queue_skb(mdev, DMA_FROM_DEVICE, skb,
0394                     mbim->mru, MHI_EOT);
0395         if (unlikely(err)) {
0396             kfree_skb(skb);
0397             break;
0398         }
0399 
0400         /* Do not hog the CPU if rx buffers are consumed faster than
0401          * queued (unlikely).
0402          */
0403         cond_resched();
0404     }
0405 
0406     /* If we're still starved of rx buffers, reschedule later */
0407     if (mhi_get_free_desc_count(mdev, DMA_FROM_DEVICE) == mbim->rx_queue_sz)
0408         schedule_delayed_work(&mbim->rx_refill, HZ / 2);
0409 }
0410 
0411 static void mhi_mbim_dl_callback(struct mhi_device *mhi_dev,
0412                  struct mhi_result *mhi_res)
0413 {
0414     struct mhi_mbim_context *mbim = dev_get_drvdata(&mhi_dev->dev);
0415     struct sk_buff *skb = mhi_res->buf_addr;
0416     int free_desc_count;
0417 
0418     free_desc_count = mhi_get_free_desc_count(mhi_dev, DMA_FROM_DEVICE);
0419 
0420     if (unlikely(mhi_res->transaction_status)) {
0421         switch (mhi_res->transaction_status) {
0422         case -EOVERFLOW:
0423             /* Packet has been split over multiple transfers */
0424             skb_put(skb, mhi_res->bytes_xferd);
0425             mhi_net_skb_agg(mbim, skb);
0426             break;
0427         case -ENOTCONN:
0428             /* MHI layer stopping/resetting the DL channel */
0429             dev_kfree_skb_any(skb);
0430             return;
0431         default:
0432             /* Unknown error, simply drop */
0433             dev_kfree_skb_any(skb);
0434         }
0435     } else {
0436         skb_put(skb, mhi_res->bytes_xferd);
0437 
0438         if (mbim->skbagg_head) {
0439             /* Aggregate the final fragment */
0440             skb = mhi_net_skb_agg(mbim, skb);
0441             mbim->skbagg_head = NULL;
0442         }
0443 
0444         mhi_mbim_rx(mbim, skb);
0445     }
0446 
0447     /* Refill if RX buffers queue becomes low */
0448     if (free_desc_count >= mbim->rx_queue_sz / 2)
0449         schedule_delayed_work(&mbim->rx_refill, 0);
0450 }
0451 
0452 static void mhi_mbim_ndo_get_stats64(struct net_device *ndev,
0453                      struct rtnl_link_stats64 *stats)
0454 {
0455     struct mhi_mbim_link *link = wwan_netdev_drvpriv(ndev);
0456     unsigned int start;
0457 
0458     do {
0459         start = u64_stats_fetch_begin_irq(&link->rx_syncp);
0460         stats->rx_packets = u64_stats_read(&link->rx_packets);
0461         stats->rx_bytes = u64_stats_read(&link->rx_bytes);
0462         stats->rx_errors = u64_stats_read(&link->rx_errors);
0463     } while (u64_stats_fetch_retry_irq(&link->rx_syncp, start));
0464 
0465     do {
0466         start = u64_stats_fetch_begin_irq(&link->tx_syncp);
0467         stats->tx_packets = u64_stats_read(&link->tx_packets);
0468         stats->tx_bytes = u64_stats_read(&link->tx_bytes);
0469         stats->tx_errors = u64_stats_read(&link->tx_errors);
0470         stats->tx_dropped = u64_stats_read(&link->tx_dropped);
0471     } while (u64_stats_fetch_retry_irq(&link->tx_syncp, start));
0472 }
0473 
0474 static void mhi_mbim_ul_callback(struct mhi_device *mhi_dev,
0475                  struct mhi_result *mhi_res)
0476 {
0477     struct mhi_mbim_context *mbim = dev_get_drvdata(&mhi_dev->dev);
0478     struct sk_buff *skb = mhi_res->buf_addr;
0479     struct net_device *ndev = skb->dev;
0480     struct mhi_mbim_link *link = wwan_netdev_drvpriv(ndev);
0481 
0482     /* Hardware has consumed the buffer, so free the skb (which is not
0483      * freed by the MHI stack) and perform accounting.
0484      */
0485     dev_consume_skb_any(skb);
0486 
0487     u64_stats_update_begin(&link->tx_syncp);
0488     if (unlikely(mhi_res->transaction_status)) {
0489         /* MHI layer stopping/resetting the UL channel */
0490         if (mhi_res->transaction_status == -ENOTCONN) {
0491             u64_stats_update_end(&link->tx_syncp);
0492             return;
0493         }
0494 
0495         u64_stats_inc(&link->tx_errors);
0496     } else {
0497         u64_stats_inc(&link->tx_packets);
0498         u64_stats_add(&link->tx_bytes, mhi_res->bytes_xferd);
0499     }
0500     u64_stats_update_end(&link->tx_syncp);
0501 
0502     if (netif_queue_stopped(ndev) && !mhi_queue_is_full(mbim->mdev, DMA_TO_DEVICE))
0503         netif_wake_queue(ndev);
0504 }
0505 
0506 static int mhi_mbim_ndo_open(struct net_device *ndev)
0507 {
0508     struct mhi_mbim_link *link = wwan_netdev_drvpriv(ndev);
0509 
0510     /* Feed the MHI rx buffer pool */
0511     schedule_delayed_work(&link->mbim->rx_refill, 0);
0512 
0513     /* Carrier is established via out-of-band channel (e.g. qmi) */
0514     netif_carrier_on(ndev);
0515 
0516     netif_start_queue(ndev);
0517 
0518     return 0;
0519 }
0520 
0521 static int mhi_mbim_ndo_stop(struct net_device *ndev)
0522 {
0523     netif_stop_queue(ndev);
0524     netif_carrier_off(ndev);
0525 
0526     return 0;
0527 }
0528 
0529 static const struct net_device_ops mhi_mbim_ndo = {
0530     .ndo_open = mhi_mbim_ndo_open,
0531     .ndo_stop = mhi_mbim_ndo_stop,
0532     .ndo_start_xmit = mhi_mbim_ndo_xmit,
0533     .ndo_get_stats64 = mhi_mbim_ndo_get_stats64,
0534 };
0535 
0536 static int mhi_mbim_newlink(void *ctxt, struct net_device *ndev, u32 if_id,
0537                 struct netlink_ext_ack *extack)
0538 {
0539     struct mhi_mbim_link *link = wwan_netdev_drvpriv(ndev);
0540     struct mhi_mbim_context *mbim = ctxt;
0541 
0542     link->session = if_id;
0543     link->mbim = mbim;
0544     link->ndev = ndev;
0545     u64_stats_init(&link->rx_syncp);
0546     u64_stats_init(&link->tx_syncp);
0547 
0548     rcu_read_lock();
0549     if (mhi_mbim_get_link_rcu(mbim, if_id)) {
0550         rcu_read_unlock();
0551         return -EEXIST;
0552     }
0553     rcu_read_unlock();
0554 
0555     /* Already protected by RTNL lock */
0556     hlist_add_head_rcu(&link->hlnode, &mbim->link_list[LINK_HASH(if_id)]);
0557 
0558     return register_netdevice(ndev);
0559 }
0560 
0561 static void mhi_mbim_dellink(void *ctxt, struct net_device *ndev,
0562                  struct list_head *head)
0563 {
0564     struct mhi_mbim_link *link = wwan_netdev_drvpriv(ndev);
0565 
0566     hlist_del_init_rcu(&link->hlnode);
0567     synchronize_rcu();
0568 
0569     unregister_netdevice_queue(ndev, head);
0570 }
0571 
0572 static void mhi_mbim_setup(struct net_device *ndev)
0573 {
0574     ndev->header_ops = NULL;  /* No header */
0575     ndev->type = ARPHRD_RAWIP;
0576     ndev->needed_headroom = sizeof(struct mbim_tx_hdr);
0577     ndev->hard_header_len = 0;
0578     ndev->addr_len = 0;
0579     ndev->flags = IFF_POINTOPOINT | IFF_NOARP;
0580     ndev->netdev_ops = &mhi_mbim_ndo;
0581     ndev->mtu = MHI_MBIM_DEFAULT_MTU;
0582     ndev->min_mtu = ETH_MIN_MTU;
0583     ndev->max_mtu = MHI_MAX_BUF_SZ - ndev->needed_headroom;
0584     ndev->tx_queue_len = 1000;
0585 }
0586 
0587 static const struct wwan_ops mhi_mbim_wwan_ops = {
0588     .priv_size = sizeof(struct mhi_mbim_link),
0589     .setup = mhi_mbim_setup,
0590     .newlink = mhi_mbim_newlink,
0591     .dellink = mhi_mbim_dellink,
0592 };
0593 
0594 static int mhi_mbim_probe(struct mhi_device *mhi_dev, const struct mhi_device_id *id)
0595 {
0596     struct mhi_controller *cntrl = mhi_dev->mhi_cntrl;
0597     struct mhi_mbim_context *mbim;
0598     int err;
0599 
0600     mbim = devm_kzalloc(&mhi_dev->dev, sizeof(*mbim), GFP_KERNEL);
0601     if (!mbim)
0602         return -ENOMEM;
0603 
0604     spin_lock_init(&mbim->tx_lock);
0605     dev_set_drvdata(&mhi_dev->dev, mbim);
0606     mbim->mdev = mhi_dev;
0607     mbim->mru = mhi_dev->mhi_cntrl->mru ? mhi_dev->mhi_cntrl->mru : MHI_DEFAULT_MRU;
0608 
0609     INIT_DELAYED_WORK(&mbim->rx_refill, mhi_net_rx_refill_work);
0610 
0611     /* Start MHI channels */
0612     err = mhi_prepare_for_transfer(mhi_dev);
0613     if (err)
0614         return err;
0615 
0616     /* Number of transfer descriptors determines size of the queue */
0617     mbim->rx_queue_sz = mhi_get_free_desc_count(mhi_dev, DMA_FROM_DEVICE);
0618 
0619     /* Register wwan link ops with MHI controller representing WWAN instance */
0620     return wwan_register_ops(&cntrl->mhi_dev->dev, &mhi_mbim_wwan_ops, mbim, 0);
0621 }
0622 
0623 static void mhi_mbim_remove(struct mhi_device *mhi_dev)
0624 {
0625     struct mhi_mbim_context *mbim = dev_get_drvdata(&mhi_dev->dev);
0626     struct mhi_controller *cntrl = mhi_dev->mhi_cntrl;
0627 
0628     mhi_unprepare_from_transfer(mhi_dev);
0629     cancel_delayed_work_sync(&mbim->rx_refill);
0630     wwan_unregister_ops(&cntrl->mhi_dev->dev);
0631     kfree_skb(mbim->skbagg_head);
0632     dev_set_drvdata(&mhi_dev->dev, NULL);
0633 }
0634 
0635 static const struct mhi_device_id mhi_mbim_id_table[] = {
0636     /* Hardware accelerated data PATH (to modem IPA), MBIM protocol */
0637     { .chan = "IP_HW0_MBIM", .driver_data = 0 },
0638     {}
0639 };
0640 MODULE_DEVICE_TABLE(mhi, mhi_mbim_id_table);
0641 
0642 static struct mhi_driver mhi_mbim_driver = {
0643     .probe = mhi_mbim_probe,
0644     .remove = mhi_mbim_remove,
0645     .dl_xfer_cb = mhi_mbim_dl_callback,
0646     .ul_xfer_cb = mhi_mbim_ul_callback,
0647     .id_table = mhi_mbim_id_table,
0648     .driver = {
0649         .name = "mhi_wwan_mbim",
0650         .owner = THIS_MODULE,
0651     },
0652 };
0653 
0654 module_mhi_driver(mhi_mbim_driver);
0655 
0656 MODULE_AUTHOR("Loic Poulain <loic.poulain@linaro.org>");
0657 MODULE_DESCRIPTION("Network/MBIM over MHI");
0658 MODULE_LICENSE("GPL v2");