1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* MHI Network driver - Network over MHI bus
4 * Copyright (C) 2021 Linaro Ltd <loic.poulain@linaro.org>
6 * This driver copy some code from cdc_ncm, which is:
7 * Copyright (C) ST-Ericsson 2010-2012
8 * and cdc_mbim, which is:
9 * Copyright (c) 2012 Smith Micro Software, Inc.
10 * Copyright (c) 2012 Bjørn Mork <bjorn@mork.no>
14 #include <linux/ethtool.h>
15 #include <linux/if_vlan.h>
17 #include <linux/mii.h>
18 #include <linux/netdevice.h>
19 #include <linux/wwan.h>
20 #include <linux/skbuff.h>
21 #include <linux/usb.h>
22 #include <linux/usb/cdc.h>
23 #include <linux/usb/usbnet.h>
24 #include <linux/usb/cdc_ncm.h>
28 #define MBIM_NDP16_SIGN_MASK 0x00ffffff
31 #define MHI_MBIM_DEFAULT_MTU 1500
33 /* 3500 allows to optimize skb allocation, the skbs will basically fit in
34 * one 4K page. Large MBIM packets will simply be split over several MHI
35 * transfers and chained by the MHI net layer (zerocopy).
37 #define MHI_MBIM_DEFAULT_MRU 3500
44 static void __mbim_length_errors_inc(struct mhi_net_dev *dev)
46 u64_stats_update_begin(&dev->stats.rx_syncp);
47 u64_stats_inc(&dev->stats.rx_length_errors);
48 u64_stats_update_end(&dev->stats.rx_syncp);
51 static void __mbim_errors_inc(struct mhi_net_dev *dev)
53 u64_stats_update_begin(&dev->stats.rx_syncp);
54 u64_stats_inc(&dev->stats.rx_errors);
55 u64_stats_update_end(&dev->stats.rx_syncp);
58 static int mbim_rx_verify_nth16(struct sk_buff *skb)
60 struct mhi_net_dev *dev = wwan_netdev_drvpriv(skb->dev);
61 struct mbim_context *ctx = dev->proto_data;
62 struct usb_cdc_ncm_nth16 *nth16;
65 if (skb->len < sizeof(struct usb_cdc_ncm_nth16) +
66 sizeof(struct usb_cdc_ncm_ndp16)) {
67 netif_dbg(dev, rx_err, dev->ndev, "frame too short\n");
68 __mbim_length_errors_inc(dev);
72 nth16 = (struct usb_cdc_ncm_nth16 *)skb->data;
74 if (nth16->dwSignature != cpu_to_le32(USB_CDC_NCM_NTH16_SIGN)) {
75 netif_dbg(dev, rx_err, dev->ndev,
76 "invalid NTH16 signature <%#010x>\n",
77 le32_to_cpu(nth16->dwSignature));
78 __mbim_errors_inc(dev);
82 /* No limit on the block length, except the size of the data pkt */
83 len = le16_to_cpu(nth16->wBlockLength);
85 netif_dbg(dev, rx_err, dev->ndev,
86 "NTB does not fit into the skb %u/%u\n", len,
88 __mbim_length_errors_inc(dev);
92 if (ctx->rx_seq + 1 != le16_to_cpu(nth16->wSequence) &&
93 (ctx->rx_seq || le16_to_cpu(nth16->wSequence)) &&
94 !(ctx->rx_seq == 0xffff && !le16_to_cpu(nth16->wSequence))) {
95 netif_dbg(dev, rx_err, dev->ndev,
96 "sequence number glitch prev=%d curr=%d\n",
97 ctx->rx_seq, le16_to_cpu(nth16->wSequence));
99 ctx->rx_seq = le16_to_cpu(nth16->wSequence);
101 return le16_to_cpu(nth16->wNdpIndex);
104 static int mbim_rx_verify_ndp16(struct sk_buff *skb, struct usb_cdc_ncm_ndp16 *ndp16)
106 struct mhi_net_dev *dev = wwan_netdev_drvpriv(skb->dev);
109 if (le16_to_cpu(ndp16->wLength) < USB_CDC_NCM_NDP16_LENGTH_MIN) {
110 netif_dbg(dev, rx_err, dev->ndev, "invalid DPT16 length <%u>\n",
111 le16_to_cpu(ndp16->wLength));
115 ret = ((le16_to_cpu(ndp16->wLength) - sizeof(struct usb_cdc_ncm_ndp16))
116 / sizeof(struct usb_cdc_ncm_dpe16));
117 ret--; /* Last entry is always a NULL terminator */
119 if (sizeof(struct usb_cdc_ncm_ndp16) +
120 ret * sizeof(struct usb_cdc_ncm_dpe16) > skb->len) {
121 netif_dbg(dev, rx_err, dev->ndev,
122 "Invalid nframes = %d\n", ret);
129 static void mbim_rx(struct mhi_net_dev *mhi_netdev, struct sk_buff *skb)
131 struct net_device *ndev = mhi_netdev->ndev;
134 /* Check NTB header and retrieve first NDP offset */
135 ndpoffset = mbim_rx_verify_nth16(skb);
137 net_err_ratelimited("%s: Incorrect NTB header\n", ndev->name);
141 /* Process each NDP */
143 struct usb_cdc_ncm_ndp16 ndp16;
144 struct usb_cdc_ncm_dpe16 dpe16;
145 int nframes, n, dpeoffset;
147 if (skb_copy_bits(skb, ndpoffset, &ndp16, sizeof(ndp16))) {
148 net_err_ratelimited("%s: Incorrect NDP offset (%u)\n",
149 ndev->name, ndpoffset);
150 __mbim_length_errors_inc(mhi_netdev);
154 /* Check NDP header and retrieve number of datagrams */
155 nframes = mbim_rx_verify_ndp16(skb, &ndp16);
157 net_err_ratelimited("%s: Incorrect NDP16\n", ndev->name);
158 __mbim_length_errors_inc(mhi_netdev);
162 /* Only IP data type supported, no DSS in MHI context */
163 if ((ndp16.dwSignature & cpu_to_le32(MBIM_NDP16_SIGN_MASK))
164 != cpu_to_le32(USB_CDC_MBIM_NDP16_IPS_SIGN)) {
165 net_err_ratelimited("%s: Unsupported NDP type\n", ndev->name);
166 __mbim_errors_inc(mhi_netdev);
170 /* Only primary IP session 0 (0x00) supported for now */
171 if (ndp16.dwSignature & ~cpu_to_le32(MBIM_NDP16_SIGN_MASK)) {
172 net_err_ratelimited("%s: bad packet session\n", ndev->name);
173 __mbim_errors_inc(mhi_netdev);
177 /* de-aggregate and deliver IP packets */
178 dpeoffset = ndpoffset + sizeof(struct usb_cdc_ncm_ndp16);
179 for (n = 0; n < nframes; n++, dpeoffset += sizeof(dpe16)) {
180 u16 dgram_offset, dgram_len;
181 struct sk_buff *skbn;
183 if (skb_copy_bits(skb, dpeoffset, &dpe16, sizeof(dpe16)))
186 dgram_offset = le16_to_cpu(dpe16.wDatagramIndex);
187 dgram_len = le16_to_cpu(dpe16.wDatagramLength);
189 if (!dgram_offset || !dgram_len)
190 break; /* null terminator */
192 skbn = netdev_alloc_skb(ndev, dgram_len);
196 skb_put(skbn, dgram_len);
197 skb_copy_bits(skb, dgram_offset, skbn->data, dgram_len);
199 switch (skbn->data[0] & 0xf0) {
201 skbn->protocol = htons(ETH_P_IP);
204 skbn->protocol = htons(ETH_P_IPV6);
207 net_err_ratelimited("%s: unknown protocol\n",
209 __mbim_errors_inc(mhi_netdev);
210 dev_kfree_skb_any(skbn);
214 u64_stats_update_begin(&mhi_netdev->stats.rx_syncp);
215 u64_stats_inc(&mhi_netdev->stats.rx_packets);
216 u64_stats_add(&mhi_netdev->stats.rx_bytes, skbn->len);
217 u64_stats_update_end(&mhi_netdev->stats.rx_syncp);
221 /* Other NDP to process? */
222 ndpoffset = (int)le16_to_cpu(ndp16.wNextNdpIndex);
228 dev_consume_skb_any(skb);
231 dev_kfree_skb_any(skb);
235 struct usb_cdc_ncm_nth16 nth16;
236 struct usb_cdc_ncm_ndp16 ndp16;
237 struct usb_cdc_ncm_dpe16 dpe16[2];
240 static struct sk_buff *mbim_tx_fixup(struct mhi_net_dev *mhi_netdev,
243 struct mbim_context *ctx = mhi_netdev->proto_data;
244 unsigned int dgram_size = skb->len;
245 struct usb_cdc_ncm_nth16 *nth16;
246 struct usb_cdc_ncm_ndp16 *ndp16;
247 struct mbim_tx_hdr *mbim_hdr;
249 /* For now, this is a partial implementation of CDC MBIM, only one NDP
250 * is sent, containing the IP packet (no aggregation).
253 /* Ensure we have enough headroom for crafting MBIM header */
254 if (skb_cow_head(skb, sizeof(struct mbim_tx_hdr))) {
255 dev_kfree_skb_any(skb);
259 mbim_hdr = skb_push(skb, sizeof(struct mbim_tx_hdr));
261 /* Fill NTB header */
262 nth16 = &mbim_hdr->nth16;
263 nth16->dwSignature = cpu_to_le32(USB_CDC_NCM_NTH16_SIGN);
264 nth16->wHeaderLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_nth16));
265 nth16->wSequence = cpu_to_le16(ctx->tx_seq++);
266 nth16->wBlockLength = cpu_to_le16(skb->len);
267 nth16->wNdpIndex = cpu_to_le16(sizeof(struct usb_cdc_ncm_nth16));
269 /* Fill the unique NDP */
270 ndp16 = &mbim_hdr->ndp16;
271 ndp16->dwSignature = cpu_to_le32(USB_CDC_MBIM_NDP16_IPS_SIGN);
272 ndp16->wLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_ndp16)
273 + sizeof(struct usb_cdc_ncm_dpe16) * 2);
274 ndp16->wNextNdpIndex = 0;
276 /* Datagram follows the mbim header */
277 ndp16->dpe16[0].wDatagramIndex = cpu_to_le16(sizeof(struct mbim_tx_hdr));
278 ndp16->dpe16[0].wDatagramLength = cpu_to_le16(dgram_size);
280 /* null termination */
281 ndp16->dpe16[1].wDatagramIndex = 0;
282 ndp16->dpe16[1].wDatagramLength = 0;
287 static int mbim_init(struct mhi_net_dev *mhi_netdev)
289 struct net_device *ndev = mhi_netdev->ndev;
291 mhi_netdev->proto_data = devm_kzalloc(&ndev->dev,
292 sizeof(struct mbim_context),
294 if (!mhi_netdev->proto_data)
297 ndev->needed_headroom = sizeof(struct mbim_tx_hdr);
298 ndev->mtu = MHI_MBIM_DEFAULT_MTU;
300 if (!mhi_netdev->mru)
301 mhi_netdev->mru = MHI_MBIM_DEFAULT_MRU;
306 const struct mhi_net_proto proto_mbim = {
309 .tx_fixup = mbim_tx_fixup,