1aa730a99SLoic Poulain // SPDX-License-Identifier: GPL-2.0-or-later
2aa730a99SLoic Poulain /* MHI MBIM Network driver - Network/MBIM over MHI bus
3aa730a99SLoic Poulain *
4aa730a99SLoic Poulain * Copyright (C) 2021 Linaro Ltd <loic.poulain@linaro.org>
5aa730a99SLoic Poulain *
6aa730a99SLoic Poulain * This driver copy some code from cdc_ncm, which is:
7aa730a99SLoic Poulain * Copyright (C) ST-Ericsson 2010-2012
8aa730a99SLoic Poulain * and cdc_mbim, which is:
9aa730a99SLoic Poulain * Copyright (c) 2012 Smith Micro Software, Inc.
10aa730a99SLoic Poulain * Copyright (c) 2012 Bjørn Mork <bjorn@mork.no>
11aa730a99SLoic Poulain *
12aa730a99SLoic Poulain */
13aa730a99SLoic Poulain
14aa730a99SLoic Poulain #include <linux/ethtool.h>
15aa730a99SLoic Poulain #include <linux/if_arp.h>
16aa730a99SLoic Poulain #include <linux/if_vlan.h>
17aa730a99SLoic Poulain #include <linux/ip.h>
18aa730a99SLoic Poulain #include <linux/mhi.h>
19aa730a99SLoic Poulain #include <linux/mii.h>
20aa730a99SLoic Poulain #include <linux/mod_devicetable.h>
21aa730a99SLoic Poulain #include <linux/module.h>
22aa730a99SLoic Poulain #include <linux/netdevice.h>
23aa730a99SLoic Poulain #include <linux/skbuff.h>
24aa730a99SLoic Poulain #include <linux/u64_stats_sync.h>
25aa730a99SLoic Poulain #include <linux/usb.h>
26aa730a99SLoic Poulain #include <linux/usb/cdc.h>
27aa730a99SLoic Poulain #include <linux/usb/usbnet.h>
28aa730a99SLoic Poulain #include <linux/usb/cdc_ncm.h>
29aa730a99SLoic Poulain #include <linux/wwan.h>
30aa730a99SLoic Poulain
31aa730a99SLoic Poulain /* 3500 allows to optimize skb allocation, the skbs will basically fit in
32aa730a99SLoic Poulain * one 4K page. Large MBIM packets will simply be split over several MHI
33aa730a99SLoic Poulain * transfers and chained by the MHI net layer (zerocopy).
34aa730a99SLoic Poulain */
35aa730a99SLoic Poulain #define MHI_DEFAULT_MRU 3500
36aa730a99SLoic Poulain
37aa730a99SLoic Poulain #define MHI_MBIM_DEFAULT_MTU 1500
38aa730a99SLoic Poulain #define MHI_MAX_BUF_SZ 0xffff
39aa730a99SLoic Poulain
40aa730a99SLoic Poulain #define MBIM_NDP16_SIGN_MASK 0x00ffffff
41aa730a99SLoic Poulain
42aa730a99SLoic Poulain #define MHI_MBIM_LINK_HASH_SIZE 8
43aa730a99SLoic Poulain #define LINK_HASH(session) ((session) % MHI_MBIM_LINK_HASH_SIZE)
44aa730a99SLoic Poulain
45aa730a99SLoic Poulain struct mhi_mbim_link {
46aa730a99SLoic Poulain struct mhi_mbim_context *mbim;
47aa730a99SLoic Poulain struct net_device *ndev;
48aa730a99SLoic Poulain unsigned int session;
49aa730a99SLoic Poulain
50aa730a99SLoic Poulain /* stats */
51aa730a99SLoic Poulain u64_stats_t rx_packets;
52aa730a99SLoic Poulain u64_stats_t rx_bytes;
53aa730a99SLoic Poulain u64_stats_t rx_errors;
54aa730a99SLoic Poulain u64_stats_t tx_packets;
55aa730a99SLoic Poulain u64_stats_t tx_bytes;
56aa730a99SLoic Poulain u64_stats_t tx_errors;
57aa730a99SLoic Poulain u64_stats_t tx_dropped;
58aa730a99SLoic Poulain struct u64_stats_sync tx_syncp;
59aa730a99SLoic Poulain struct u64_stats_sync rx_syncp;
60aa730a99SLoic Poulain
61aa730a99SLoic Poulain struct hlist_node hlnode;
62aa730a99SLoic Poulain };
63aa730a99SLoic Poulain
64aa730a99SLoic Poulain struct mhi_mbim_context {
65aa730a99SLoic Poulain struct mhi_device *mdev;
66aa730a99SLoic Poulain struct sk_buff *skbagg_head;
67aa730a99SLoic Poulain struct sk_buff *skbagg_tail;
68aa730a99SLoic Poulain unsigned int mru;
69aa730a99SLoic Poulain u32 rx_queue_sz;
70aa730a99SLoic Poulain u16 rx_seq;
71aa730a99SLoic Poulain u16 tx_seq;
72aa730a99SLoic Poulain struct delayed_work rx_refill;
73aa730a99SLoic Poulain spinlock_t tx_lock;
74aa730a99SLoic Poulain struct hlist_head link_list[MHI_MBIM_LINK_HASH_SIZE];
75aa730a99SLoic Poulain };
76aa730a99SLoic Poulain
77aa730a99SLoic Poulain struct mbim_tx_hdr {
78aa730a99SLoic Poulain struct usb_cdc_ncm_nth16 nth16;
79aa730a99SLoic Poulain struct usb_cdc_ncm_ndp16 ndp16;
80aa730a99SLoic Poulain struct usb_cdc_ncm_dpe16 dpe16[2];
81aa730a99SLoic Poulain } __packed;
82aa730a99SLoic Poulain
mhi_mbim_get_link_rcu(struct mhi_mbim_context * mbim,unsigned int session)83aa730a99SLoic Poulain static struct mhi_mbim_link *mhi_mbim_get_link_rcu(struct mhi_mbim_context *mbim,
84aa730a99SLoic Poulain unsigned int session)
85aa730a99SLoic Poulain {
86aa730a99SLoic Poulain struct mhi_mbim_link *link;
87aa730a99SLoic Poulain
88aa730a99SLoic Poulain hlist_for_each_entry_rcu(link, &mbim->link_list[LINK_HASH(session)], hlnode) {
89aa730a99SLoic Poulain if (link->session == session)
90aa730a99SLoic Poulain return link;
91aa730a99SLoic Poulain }
92aa730a99SLoic Poulain
93aa730a99SLoic Poulain return NULL;
94aa730a99SLoic Poulain }
95aa730a99SLoic Poulain
mbim_tx_fixup(struct sk_buff * skb,unsigned int session,u16 tx_seq)96aa730a99SLoic Poulain static struct sk_buff *mbim_tx_fixup(struct sk_buff *skb, unsigned int session,
97aa730a99SLoic Poulain u16 tx_seq)
98aa730a99SLoic Poulain {
99aa730a99SLoic Poulain unsigned int dgram_size = skb->len;
100aa730a99SLoic Poulain struct usb_cdc_ncm_nth16 *nth16;
101aa730a99SLoic Poulain struct usb_cdc_ncm_ndp16 *ndp16;
102aa730a99SLoic Poulain struct mbim_tx_hdr *mbim_hdr;
103aa730a99SLoic Poulain
104aa730a99SLoic Poulain /* Only one NDP is sent, containing the IP packet (no aggregation) */
105aa730a99SLoic Poulain
106aa730a99SLoic Poulain /* Ensure we have enough headroom for crafting MBIM header */
107aa730a99SLoic Poulain if (skb_cow_head(skb, sizeof(struct mbim_tx_hdr))) {
108aa730a99SLoic Poulain dev_kfree_skb_any(skb);
109aa730a99SLoic Poulain return NULL;
110aa730a99SLoic Poulain }
111aa730a99SLoic Poulain
112aa730a99SLoic Poulain mbim_hdr = skb_push(skb, sizeof(struct mbim_tx_hdr));
113aa730a99SLoic Poulain
114aa730a99SLoic Poulain /* Fill NTB header */
115aa730a99SLoic Poulain nth16 = &mbim_hdr->nth16;
116aa730a99SLoic Poulain nth16->dwSignature = cpu_to_le32(USB_CDC_NCM_NTH16_SIGN);
117aa730a99SLoic Poulain nth16->wHeaderLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_nth16));
118aa730a99SLoic Poulain nth16->wSequence = cpu_to_le16(tx_seq);
119aa730a99SLoic Poulain nth16->wBlockLength = cpu_to_le16(skb->len);
120aa730a99SLoic Poulain nth16->wNdpIndex = cpu_to_le16(sizeof(struct usb_cdc_ncm_nth16));
121aa730a99SLoic Poulain
122aa730a99SLoic Poulain /* Fill the unique NDP */
123aa730a99SLoic Poulain ndp16 = &mbim_hdr->ndp16;
124aa730a99SLoic Poulain ndp16->dwSignature = cpu_to_le32(USB_CDC_MBIM_NDP16_IPS_SIGN | (session << 24));
125aa730a99SLoic Poulain ndp16->wLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_ndp16)
126aa730a99SLoic Poulain + sizeof(struct usb_cdc_ncm_dpe16) * 2);
127aa730a99SLoic Poulain ndp16->wNextNdpIndex = 0;
128aa730a99SLoic Poulain
129aa730a99SLoic Poulain /* Datagram follows the mbim header */
130aa730a99SLoic Poulain ndp16->dpe16[0].wDatagramIndex = cpu_to_le16(sizeof(struct mbim_tx_hdr));
131aa730a99SLoic Poulain ndp16->dpe16[0].wDatagramLength = cpu_to_le16(dgram_size);
132aa730a99SLoic Poulain
133aa730a99SLoic Poulain /* null termination */
134aa730a99SLoic Poulain ndp16->dpe16[1].wDatagramIndex = 0;
135aa730a99SLoic Poulain ndp16->dpe16[1].wDatagramLength = 0;
136aa730a99SLoic Poulain
137aa730a99SLoic Poulain return skb;
138aa730a99SLoic Poulain }
139aa730a99SLoic Poulain
mhi_mbim_ndo_xmit(struct sk_buff * skb,struct net_device * ndev)140aa730a99SLoic Poulain static netdev_tx_t mhi_mbim_ndo_xmit(struct sk_buff *skb, struct net_device *ndev)
141aa730a99SLoic Poulain {
142aa730a99SLoic Poulain struct mhi_mbim_link *link = wwan_netdev_drvpriv(ndev);
143aa730a99SLoic Poulain struct mhi_mbim_context *mbim = link->mbim;
144aa730a99SLoic Poulain unsigned long flags;
145aa730a99SLoic Poulain int err = -ENOMEM;
146aa730a99SLoic Poulain
147aa730a99SLoic Poulain /* Serialize MHI channel queuing and MBIM seq */
148aa730a99SLoic Poulain spin_lock_irqsave(&mbim->tx_lock, flags);
149aa730a99SLoic Poulain
150aa730a99SLoic Poulain skb = mbim_tx_fixup(skb, link->session, mbim->tx_seq);
151aa730a99SLoic Poulain if (unlikely(!skb))
152aa730a99SLoic Poulain goto exit_unlock;
153aa730a99SLoic Poulain
154aa730a99SLoic Poulain err = mhi_queue_skb(mbim->mdev, DMA_TO_DEVICE, skb, skb->len, MHI_EOT);
155aa730a99SLoic Poulain
156aa730a99SLoic Poulain if (mhi_queue_is_full(mbim->mdev, DMA_TO_DEVICE))
157aa730a99SLoic Poulain netif_stop_queue(ndev);
158aa730a99SLoic Poulain
159aa730a99SLoic Poulain if (!err)
160aa730a99SLoic Poulain mbim->tx_seq++;
161aa730a99SLoic Poulain
162aa730a99SLoic Poulain exit_unlock:
163aa730a99SLoic Poulain spin_unlock_irqrestore(&mbim->tx_lock, flags);
164aa730a99SLoic Poulain
165aa730a99SLoic Poulain if (unlikely(err)) {
166aa730a99SLoic Poulain net_err_ratelimited("%s: Failed to queue TX buf (%d)\n",
167aa730a99SLoic Poulain ndev->name, err);
168aa730a99SLoic Poulain dev_kfree_skb_any(skb);
169aa730a99SLoic Poulain goto exit_drop;
170aa730a99SLoic Poulain }
171aa730a99SLoic Poulain
172aa730a99SLoic Poulain return NETDEV_TX_OK;
173aa730a99SLoic Poulain
174aa730a99SLoic Poulain exit_drop:
175aa730a99SLoic Poulain u64_stats_update_begin(&link->tx_syncp);
176aa730a99SLoic Poulain u64_stats_inc(&link->tx_dropped);
177aa730a99SLoic Poulain u64_stats_update_end(&link->tx_syncp);
178aa730a99SLoic Poulain
179aa730a99SLoic Poulain return NETDEV_TX_OK;
180aa730a99SLoic Poulain }
181aa730a99SLoic Poulain
mbim_rx_verify_nth16(struct mhi_mbim_context * mbim,struct sk_buff * skb)182aa730a99SLoic Poulain static int mbim_rx_verify_nth16(struct mhi_mbim_context *mbim, struct sk_buff *skb)
183aa730a99SLoic Poulain {
184aa730a99SLoic Poulain struct usb_cdc_ncm_nth16 *nth16;
185aa730a99SLoic Poulain int len;
186aa730a99SLoic Poulain
187aa730a99SLoic Poulain if (skb->len < sizeof(struct usb_cdc_ncm_nth16) +
188aa730a99SLoic Poulain sizeof(struct usb_cdc_ncm_ndp16)) {
189aa730a99SLoic Poulain net_err_ratelimited("frame too short\n");
190aa730a99SLoic Poulain return -EINVAL;
191aa730a99SLoic Poulain }
192aa730a99SLoic Poulain
193aa730a99SLoic Poulain nth16 = (struct usb_cdc_ncm_nth16 *)skb->data;
194aa730a99SLoic Poulain
195aa730a99SLoic Poulain if (nth16->dwSignature != cpu_to_le32(USB_CDC_NCM_NTH16_SIGN)) {
196aa730a99SLoic Poulain net_err_ratelimited("invalid NTH16 signature <%#010x>\n",
197aa730a99SLoic Poulain le32_to_cpu(nth16->dwSignature));
198aa730a99SLoic Poulain return -EINVAL;
199aa730a99SLoic Poulain }
200aa730a99SLoic Poulain
201aa730a99SLoic Poulain /* No limit on the block length, except the size of the data pkt */
202aa730a99SLoic Poulain len = le16_to_cpu(nth16->wBlockLength);
203aa730a99SLoic Poulain if (len > skb->len) {
204aa730a99SLoic Poulain net_err_ratelimited("NTB does not fit into the skb %u/%u\n",
205aa730a99SLoic Poulain len, skb->len);
206aa730a99SLoic Poulain return -EINVAL;
207aa730a99SLoic Poulain }
208aa730a99SLoic Poulain
209aa730a99SLoic Poulain if (mbim->rx_seq + 1 != le16_to_cpu(nth16->wSequence) &&
210aa730a99SLoic Poulain (mbim->rx_seq || le16_to_cpu(nth16->wSequence)) &&
211aa730a99SLoic Poulain !(mbim->rx_seq == 0xffff && !le16_to_cpu(nth16->wSequence))) {
212aa730a99SLoic Poulain net_err_ratelimited("sequence number glitch prev=%d curr=%d\n",
213aa730a99SLoic Poulain mbim->rx_seq, le16_to_cpu(nth16->wSequence));
214aa730a99SLoic Poulain }
215aa730a99SLoic Poulain mbim->rx_seq = le16_to_cpu(nth16->wSequence);
216aa730a99SLoic Poulain
217aa730a99SLoic Poulain return le16_to_cpu(nth16->wNdpIndex);
218aa730a99SLoic Poulain }
219aa730a99SLoic Poulain
mbim_rx_verify_ndp16(struct sk_buff * skb,struct usb_cdc_ncm_ndp16 * ndp16)220aa730a99SLoic Poulain static int mbim_rx_verify_ndp16(struct sk_buff *skb, struct usb_cdc_ncm_ndp16 *ndp16)
221aa730a99SLoic Poulain {
222aa730a99SLoic Poulain int ret;
223aa730a99SLoic Poulain
224aa730a99SLoic Poulain if (le16_to_cpu(ndp16->wLength) < USB_CDC_NCM_NDP16_LENGTH_MIN) {
225aa730a99SLoic Poulain net_err_ratelimited("invalid DPT16 length <%u>\n",
226aa730a99SLoic Poulain le16_to_cpu(ndp16->wLength));
227aa730a99SLoic Poulain return -EINVAL;
228aa730a99SLoic Poulain }
229aa730a99SLoic Poulain
230aa730a99SLoic Poulain ret = ((le16_to_cpu(ndp16->wLength) - sizeof(struct usb_cdc_ncm_ndp16))
231aa730a99SLoic Poulain / sizeof(struct usb_cdc_ncm_dpe16));
232aa730a99SLoic Poulain ret--; /* Last entry is always a NULL terminator */
233aa730a99SLoic Poulain
234aa730a99SLoic Poulain if (sizeof(struct usb_cdc_ncm_ndp16) +
235aa730a99SLoic Poulain ret * sizeof(struct usb_cdc_ncm_dpe16) > skb->len) {
236aa730a99SLoic Poulain net_err_ratelimited("Invalid nframes = %d\n", ret);
237aa730a99SLoic Poulain return -EINVAL;
238aa730a99SLoic Poulain }
239aa730a99SLoic Poulain
240aa730a99SLoic Poulain return ret;
241aa730a99SLoic Poulain }
242aa730a99SLoic Poulain
mhi_mbim_rx(struct mhi_mbim_context * mbim,struct sk_buff * skb)243aa730a99SLoic Poulain static void mhi_mbim_rx(struct mhi_mbim_context *mbim, struct sk_buff *skb)
244aa730a99SLoic Poulain {
245aa730a99SLoic Poulain int ndpoffset;
246aa730a99SLoic Poulain
247aa730a99SLoic Poulain /* Check NTB header and retrieve first NDP offset */
248aa730a99SLoic Poulain ndpoffset = mbim_rx_verify_nth16(mbim, skb);
249aa730a99SLoic Poulain if (ndpoffset < 0) {
250aa730a99SLoic Poulain net_err_ratelimited("mbim: Incorrect NTB header\n");
251aa730a99SLoic Poulain goto error;
252aa730a99SLoic Poulain }
253aa730a99SLoic Poulain
254aa730a99SLoic Poulain /* Process each NDP */
255aa730a99SLoic Poulain while (1) {
256aa730a99SLoic Poulain struct usb_cdc_ncm_ndp16 ndp16;
257aa730a99SLoic Poulain struct usb_cdc_ncm_dpe16 dpe16;
258aa730a99SLoic Poulain struct mhi_mbim_link *link;
259aa730a99SLoic Poulain int nframes, n, dpeoffset;
260aa730a99SLoic Poulain unsigned int session;
261aa730a99SLoic Poulain
262aa730a99SLoic Poulain if (skb_copy_bits(skb, ndpoffset, &ndp16, sizeof(ndp16))) {
263aa730a99SLoic Poulain net_err_ratelimited("mbim: Incorrect NDP offset (%u)\n",
264aa730a99SLoic Poulain ndpoffset);
265aa730a99SLoic Poulain goto error;
266aa730a99SLoic Poulain }
267aa730a99SLoic Poulain
268aa730a99SLoic Poulain /* Check NDP header and retrieve number of datagrams */
269aa730a99SLoic Poulain nframes = mbim_rx_verify_ndp16(skb, &ndp16);
270aa730a99SLoic Poulain if (nframes < 0) {
271aa730a99SLoic Poulain net_err_ratelimited("mbim: Incorrect NDP16\n");
272aa730a99SLoic Poulain goto error;
273aa730a99SLoic Poulain }
274aa730a99SLoic Poulain
275aa730a99SLoic Poulain /* Only IP data type supported, no DSS in MHI context */
276aa730a99SLoic Poulain if ((ndp16.dwSignature & cpu_to_le32(MBIM_NDP16_SIGN_MASK))
277aa730a99SLoic Poulain != cpu_to_le32(USB_CDC_MBIM_NDP16_IPS_SIGN)) {
278aa730a99SLoic Poulain net_err_ratelimited("mbim: Unsupported NDP type\n");
279aa730a99SLoic Poulain goto next_ndp;
280aa730a99SLoic Poulain }
281aa730a99SLoic Poulain
282aa730a99SLoic Poulain session = (le32_to_cpu(ndp16.dwSignature) & ~MBIM_NDP16_SIGN_MASK) >> 24;
283aa730a99SLoic Poulain
284aa730a99SLoic Poulain rcu_read_lock();
285aa730a99SLoic Poulain
286aa730a99SLoic Poulain link = mhi_mbim_get_link_rcu(mbim, session);
287aa730a99SLoic Poulain if (!link) {
288aa730a99SLoic Poulain net_err_ratelimited("mbim: bad packet session (%u)\n", session);
289aa730a99SLoic Poulain goto unlock;
290aa730a99SLoic Poulain }
291aa730a99SLoic Poulain
292aa730a99SLoic Poulain /* de-aggregate and deliver IP packets */
293aa730a99SLoic Poulain dpeoffset = ndpoffset + sizeof(struct usb_cdc_ncm_ndp16);
294aa730a99SLoic Poulain for (n = 0; n < nframes; n++, dpeoffset += sizeof(dpe16)) {
295aa730a99SLoic Poulain u16 dgram_offset, dgram_len;
296aa730a99SLoic Poulain struct sk_buff *skbn;
297aa730a99SLoic Poulain
298aa730a99SLoic Poulain if (skb_copy_bits(skb, dpeoffset, &dpe16, sizeof(dpe16)))
299aa730a99SLoic Poulain break;
300aa730a99SLoic Poulain
301aa730a99SLoic Poulain dgram_offset = le16_to_cpu(dpe16.wDatagramIndex);
302aa730a99SLoic Poulain dgram_len = le16_to_cpu(dpe16.wDatagramLength);
303aa730a99SLoic Poulain
304aa730a99SLoic Poulain if (!dgram_offset || !dgram_len)
305aa730a99SLoic Poulain break; /* null terminator */
306aa730a99SLoic Poulain
307aa730a99SLoic Poulain skbn = netdev_alloc_skb(link->ndev, dgram_len);
308aa730a99SLoic Poulain if (!skbn)
309aa730a99SLoic Poulain continue;
310aa730a99SLoic Poulain
311aa730a99SLoic Poulain skb_put(skbn, dgram_len);
312aa730a99SLoic Poulain skb_copy_bits(skb, dgram_offset, skbn->data, dgram_len);
313aa730a99SLoic Poulain
314aa730a99SLoic Poulain switch (skbn->data[0] & 0xf0) {
315aa730a99SLoic Poulain case 0x40:
316aa730a99SLoic Poulain skbn->protocol = htons(ETH_P_IP);
317aa730a99SLoic Poulain break;
318aa730a99SLoic Poulain case 0x60:
319aa730a99SLoic Poulain skbn->protocol = htons(ETH_P_IPV6);
320aa730a99SLoic Poulain break;
321aa730a99SLoic Poulain default:
322aa730a99SLoic Poulain net_err_ratelimited("%s: unknown protocol\n",
323aa730a99SLoic Poulain link->ndev->name);
324aa730a99SLoic Poulain dev_kfree_skb_any(skbn);
325aa730a99SLoic Poulain u64_stats_update_begin(&link->rx_syncp);
326aa730a99SLoic Poulain u64_stats_inc(&link->rx_errors);
327aa730a99SLoic Poulain u64_stats_update_end(&link->rx_syncp);
328aa730a99SLoic Poulain continue;
329aa730a99SLoic Poulain }
330aa730a99SLoic Poulain
331aa730a99SLoic Poulain u64_stats_update_begin(&link->rx_syncp);
332aa730a99SLoic Poulain u64_stats_inc(&link->rx_packets);
333aa730a99SLoic Poulain u64_stats_add(&link->rx_bytes, skbn->len);
334aa730a99SLoic Poulain u64_stats_update_end(&link->rx_syncp);
335aa730a99SLoic Poulain
336aa730a99SLoic Poulain netif_rx(skbn);
337aa730a99SLoic Poulain }
338aa730a99SLoic Poulain unlock:
339aa730a99SLoic Poulain rcu_read_unlock();
340aa730a99SLoic Poulain next_ndp:
341aa730a99SLoic Poulain /* Other NDP to process? */
342aa730a99SLoic Poulain ndpoffset = (int)le16_to_cpu(ndp16.wNextNdpIndex);
343aa730a99SLoic Poulain if (!ndpoffset)
344aa730a99SLoic Poulain break;
345aa730a99SLoic Poulain }
346aa730a99SLoic Poulain
347aa730a99SLoic Poulain /* free skb */
348aa730a99SLoic Poulain dev_consume_skb_any(skb);
349aa730a99SLoic Poulain return;
350aa730a99SLoic Poulain error:
351aa730a99SLoic Poulain dev_kfree_skb_any(skb);
352aa730a99SLoic Poulain }
353aa730a99SLoic Poulain
mhi_net_skb_agg(struct mhi_mbim_context * mbim,struct sk_buff * skb)354aa730a99SLoic Poulain static struct sk_buff *mhi_net_skb_agg(struct mhi_mbim_context *mbim,
355aa730a99SLoic Poulain struct sk_buff *skb)
356aa730a99SLoic Poulain {
357aa730a99SLoic Poulain struct sk_buff *head = mbim->skbagg_head;
358aa730a99SLoic Poulain struct sk_buff *tail = mbim->skbagg_tail;
359aa730a99SLoic Poulain
360aa730a99SLoic Poulain /* This is non-paged skb chaining using frag_list */
361aa730a99SLoic Poulain if (!head) {
362aa730a99SLoic Poulain mbim->skbagg_head = skb;
363aa730a99SLoic Poulain return skb;
364aa730a99SLoic Poulain }
365aa730a99SLoic Poulain
366aa730a99SLoic Poulain if (!skb_shinfo(head)->frag_list)
367aa730a99SLoic Poulain skb_shinfo(head)->frag_list = skb;
368aa730a99SLoic Poulain else
369aa730a99SLoic Poulain tail->next = skb;
370aa730a99SLoic Poulain
371aa730a99SLoic Poulain head->len += skb->len;
372aa730a99SLoic Poulain head->data_len += skb->len;
373aa730a99SLoic Poulain head->truesize += skb->truesize;
374aa730a99SLoic Poulain
375aa730a99SLoic Poulain mbim->skbagg_tail = skb;
376aa730a99SLoic Poulain
377aa730a99SLoic Poulain return mbim->skbagg_head;
378aa730a99SLoic Poulain }
379aa730a99SLoic Poulain
mhi_net_rx_refill_work(struct work_struct * work)380aa730a99SLoic Poulain static void mhi_net_rx_refill_work(struct work_struct *work)
381aa730a99SLoic Poulain {
382aa730a99SLoic Poulain struct mhi_mbim_context *mbim = container_of(work, struct mhi_mbim_context,
383aa730a99SLoic Poulain rx_refill.work);
384aa730a99SLoic Poulain struct mhi_device *mdev = mbim->mdev;
385aa730a99SLoic Poulain int err;
386aa730a99SLoic Poulain
387aa730a99SLoic Poulain while (!mhi_queue_is_full(mdev, DMA_FROM_DEVICE)) {
388f542cdfaSSlark Xiao struct sk_buff *skb = alloc_skb(mbim->mru, GFP_KERNEL);
389aa730a99SLoic Poulain
390aa730a99SLoic Poulain if (unlikely(!skb))
391aa730a99SLoic Poulain break;
392aa730a99SLoic Poulain
393aa730a99SLoic Poulain err = mhi_queue_skb(mdev, DMA_FROM_DEVICE, skb,
394f542cdfaSSlark Xiao mbim->mru, MHI_EOT);
395aa730a99SLoic Poulain if (unlikely(err)) {
396aa730a99SLoic Poulain kfree_skb(skb);
397aa730a99SLoic Poulain break;
398aa730a99SLoic Poulain }
399aa730a99SLoic Poulain
400aa730a99SLoic Poulain /* Do not hog the CPU if rx buffers are consumed faster than
401aa730a99SLoic Poulain * queued (unlikely).
402aa730a99SLoic Poulain */
403aa730a99SLoic Poulain cond_resched();
404aa730a99SLoic Poulain }
405aa730a99SLoic Poulain
406aa730a99SLoic Poulain /* If we're still starved of rx buffers, reschedule later */
407aa730a99SLoic Poulain if (mhi_get_free_desc_count(mdev, DMA_FROM_DEVICE) == mbim->rx_queue_sz)
408aa730a99SLoic Poulain schedule_delayed_work(&mbim->rx_refill, HZ / 2);
409aa730a99SLoic Poulain }
410aa730a99SLoic Poulain
mhi_mbim_dl_callback(struct mhi_device * mhi_dev,struct mhi_result * mhi_res)411aa730a99SLoic Poulain static void mhi_mbim_dl_callback(struct mhi_device *mhi_dev,
412aa730a99SLoic Poulain struct mhi_result *mhi_res)
413aa730a99SLoic Poulain {
414aa730a99SLoic Poulain struct mhi_mbim_context *mbim = dev_get_drvdata(&mhi_dev->dev);
415aa730a99SLoic Poulain struct sk_buff *skb = mhi_res->buf_addr;
416aa730a99SLoic Poulain int free_desc_count;
417aa730a99SLoic Poulain
418aa730a99SLoic Poulain free_desc_count = mhi_get_free_desc_count(mhi_dev, DMA_FROM_DEVICE);
419aa730a99SLoic Poulain
420aa730a99SLoic Poulain if (unlikely(mhi_res->transaction_status)) {
421aa730a99SLoic Poulain switch (mhi_res->transaction_status) {
422aa730a99SLoic Poulain case -EOVERFLOW:
423aa730a99SLoic Poulain /* Packet has been split over multiple transfers */
424aa730a99SLoic Poulain skb_put(skb, mhi_res->bytes_xferd);
425aa730a99SLoic Poulain mhi_net_skb_agg(mbim, skb);
426aa730a99SLoic Poulain break;
427aa730a99SLoic Poulain case -ENOTCONN:
428aa730a99SLoic Poulain /* MHI layer stopping/resetting the DL channel */
429aa730a99SLoic Poulain dev_kfree_skb_any(skb);
430aa730a99SLoic Poulain return;
431aa730a99SLoic Poulain default:
432aa730a99SLoic Poulain /* Unknown error, simply drop */
433aa730a99SLoic Poulain dev_kfree_skb_any(skb);
434aa730a99SLoic Poulain }
435aa730a99SLoic Poulain } else {
436aa730a99SLoic Poulain skb_put(skb, mhi_res->bytes_xferd);
437aa730a99SLoic Poulain
438aa730a99SLoic Poulain if (mbim->skbagg_head) {
439aa730a99SLoic Poulain /* Aggregate the final fragment */
440aa730a99SLoic Poulain skb = mhi_net_skb_agg(mbim, skb);
441aa730a99SLoic Poulain mbim->skbagg_head = NULL;
442aa730a99SLoic Poulain }
443aa730a99SLoic Poulain
444aa730a99SLoic Poulain mhi_mbim_rx(mbim, skb);
445aa730a99SLoic Poulain }
446aa730a99SLoic Poulain
447aa730a99SLoic Poulain /* Refill if RX buffers queue becomes low */
448aa730a99SLoic Poulain if (free_desc_count >= mbim->rx_queue_sz / 2)
449aa730a99SLoic Poulain schedule_delayed_work(&mbim->rx_refill, 0);
450aa730a99SLoic Poulain }
451aa730a99SLoic Poulain
mhi_mbim_ndo_get_stats64(struct net_device * ndev,struct rtnl_link_stats64 * stats)452aa730a99SLoic Poulain static void mhi_mbim_ndo_get_stats64(struct net_device *ndev,
453aa730a99SLoic Poulain struct rtnl_link_stats64 *stats)
454aa730a99SLoic Poulain {
455aa730a99SLoic Poulain struct mhi_mbim_link *link = wwan_netdev_drvpriv(ndev);
456aa730a99SLoic Poulain unsigned int start;
457aa730a99SLoic Poulain
458aa730a99SLoic Poulain do {
459068c38adSThomas Gleixner start = u64_stats_fetch_begin(&link->rx_syncp);
460aa730a99SLoic Poulain stats->rx_packets = u64_stats_read(&link->rx_packets);
461aa730a99SLoic Poulain stats->rx_bytes = u64_stats_read(&link->rx_bytes);
462aa730a99SLoic Poulain stats->rx_errors = u64_stats_read(&link->rx_errors);
463068c38adSThomas Gleixner } while (u64_stats_fetch_retry(&link->rx_syncp, start));
464aa730a99SLoic Poulain
465aa730a99SLoic Poulain do {
466068c38adSThomas Gleixner start = u64_stats_fetch_begin(&link->tx_syncp);
467aa730a99SLoic Poulain stats->tx_packets = u64_stats_read(&link->tx_packets);
468aa730a99SLoic Poulain stats->tx_bytes = u64_stats_read(&link->tx_bytes);
469aa730a99SLoic Poulain stats->tx_errors = u64_stats_read(&link->tx_errors);
470aa730a99SLoic Poulain stats->tx_dropped = u64_stats_read(&link->tx_dropped);
471068c38adSThomas Gleixner } while (u64_stats_fetch_retry(&link->tx_syncp, start));
472aa730a99SLoic Poulain }
473aa730a99SLoic Poulain
mhi_mbim_ul_callback(struct mhi_device * mhi_dev,struct mhi_result * mhi_res)474aa730a99SLoic Poulain static void mhi_mbim_ul_callback(struct mhi_device *mhi_dev,
475aa730a99SLoic Poulain struct mhi_result *mhi_res)
476aa730a99SLoic Poulain {
477aa730a99SLoic Poulain struct mhi_mbim_context *mbim = dev_get_drvdata(&mhi_dev->dev);
478aa730a99SLoic Poulain struct sk_buff *skb = mhi_res->buf_addr;
479aa730a99SLoic Poulain struct net_device *ndev = skb->dev;
480aa730a99SLoic Poulain struct mhi_mbim_link *link = wwan_netdev_drvpriv(ndev);
481aa730a99SLoic Poulain
482aa730a99SLoic Poulain /* Hardware has consumed the buffer, so free the skb (which is not
483aa730a99SLoic Poulain * freed by the MHI stack) and perform accounting.
484aa730a99SLoic Poulain */
485aa730a99SLoic Poulain dev_consume_skb_any(skb);
486aa730a99SLoic Poulain
487aa730a99SLoic Poulain u64_stats_update_begin(&link->tx_syncp);
488aa730a99SLoic Poulain if (unlikely(mhi_res->transaction_status)) {
489aa730a99SLoic Poulain /* MHI layer stopping/resetting the UL channel */
490aa730a99SLoic Poulain if (mhi_res->transaction_status == -ENOTCONN) {
491aa730a99SLoic Poulain u64_stats_update_end(&link->tx_syncp);
492aa730a99SLoic Poulain return;
493aa730a99SLoic Poulain }
494aa730a99SLoic Poulain
495aa730a99SLoic Poulain u64_stats_inc(&link->tx_errors);
496aa730a99SLoic Poulain } else {
497aa730a99SLoic Poulain u64_stats_inc(&link->tx_packets);
498aa730a99SLoic Poulain u64_stats_add(&link->tx_bytes, mhi_res->bytes_xferd);
499aa730a99SLoic Poulain }
500aa730a99SLoic Poulain u64_stats_update_end(&link->tx_syncp);
501aa730a99SLoic Poulain
502aa730a99SLoic Poulain if (netif_queue_stopped(ndev) && !mhi_queue_is_full(mbim->mdev, DMA_TO_DEVICE))
503aa730a99SLoic Poulain netif_wake_queue(ndev);
504aa730a99SLoic Poulain }
505aa730a99SLoic Poulain
mhi_mbim_ndo_open(struct net_device * ndev)506aa730a99SLoic Poulain static int mhi_mbim_ndo_open(struct net_device *ndev)
507aa730a99SLoic Poulain {
508aa730a99SLoic Poulain struct mhi_mbim_link *link = wwan_netdev_drvpriv(ndev);
509aa730a99SLoic Poulain
510aa730a99SLoic Poulain /* Feed the MHI rx buffer pool */
511aa730a99SLoic Poulain schedule_delayed_work(&link->mbim->rx_refill, 0);
512aa730a99SLoic Poulain
513aa730a99SLoic Poulain /* Carrier is established via out-of-band channel (e.g. qmi) */
514aa730a99SLoic Poulain netif_carrier_on(ndev);
515aa730a99SLoic Poulain
516aa730a99SLoic Poulain netif_start_queue(ndev);
517aa730a99SLoic Poulain
518aa730a99SLoic Poulain return 0;
519aa730a99SLoic Poulain }
520aa730a99SLoic Poulain
mhi_mbim_ndo_stop(struct net_device * ndev)521aa730a99SLoic Poulain static int mhi_mbim_ndo_stop(struct net_device *ndev)
522aa730a99SLoic Poulain {
523aa730a99SLoic Poulain netif_stop_queue(ndev);
524aa730a99SLoic Poulain netif_carrier_off(ndev);
525aa730a99SLoic Poulain
526aa730a99SLoic Poulain return 0;
527aa730a99SLoic Poulain }
528aa730a99SLoic Poulain
529aa730a99SLoic Poulain static const struct net_device_ops mhi_mbim_ndo = {
530aa730a99SLoic Poulain .ndo_open = mhi_mbim_ndo_open,
531aa730a99SLoic Poulain .ndo_stop = mhi_mbim_ndo_stop,
532aa730a99SLoic Poulain .ndo_start_xmit = mhi_mbim_ndo_xmit,
533aa730a99SLoic Poulain .ndo_get_stats64 = mhi_mbim_ndo_get_stats64,
534aa730a99SLoic Poulain };
535aa730a99SLoic Poulain
mhi_mbim_newlink(void * ctxt,struct net_device * ndev,u32 if_id,struct netlink_ext_ack * extack)536aa730a99SLoic Poulain static int mhi_mbim_newlink(void *ctxt, struct net_device *ndev, u32 if_id,
537aa730a99SLoic Poulain struct netlink_ext_ack *extack)
538aa730a99SLoic Poulain {
539aa730a99SLoic Poulain struct mhi_mbim_link *link = wwan_netdev_drvpriv(ndev);
540aa730a99SLoic Poulain struct mhi_mbim_context *mbim = ctxt;
541aa730a99SLoic Poulain
542aa730a99SLoic Poulain link->session = if_id;
543aa730a99SLoic Poulain link->mbim = mbim;
544aa730a99SLoic Poulain link->ndev = ndev;
545aa730a99SLoic Poulain u64_stats_init(&link->rx_syncp);
546aa730a99SLoic Poulain u64_stats_init(&link->tx_syncp);
547aa730a99SLoic Poulain
548aa730a99SLoic Poulain rcu_read_lock();
549aa730a99SLoic Poulain if (mhi_mbim_get_link_rcu(mbim, if_id)) {
550aa730a99SLoic Poulain rcu_read_unlock();
551aa730a99SLoic Poulain return -EEXIST;
552aa730a99SLoic Poulain }
553aa730a99SLoic Poulain rcu_read_unlock();
554aa730a99SLoic Poulain
555aa730a99SLoic Poulain /* Already protected by RTNL lock */
556aa730a99SLoic Poulain hlist_add_head_rcu(&link->hlnode, &mbim->link_list[LINK_HASH(if_id)]);
557aa730a99SLoic Poulain
558aa730a99SLoic Poulain return register_netdevice(ndev);
559aa730a99SLoic Poulain }
560aa730a99SLoic Poulain
mhi_mbim_dellink(void * ctxt,struct net_device * ndev,struct list_head * head)561aa730a99SLoic Poulain static void mhi_mbim_dellink(void *ctxt, struct net_device *ndev,
562aa730a99SLoic Poulain struct list_head *head)
563aa730a99SLoic Poulain {
564aa730a99SLoic Poulain struct mhi_mbim_link *link = wwan_netdev_drvpriv(ndev);
565aa730a99SLoic Poulain
566aa730a99SLoic Poulain hlist_del_init_rcu(&link->hlnode);
567aa730a99SLoic Poulain synchronize_rcu();
568aa730a99SLoic Poulain
569aa730a99SLoic Poulain unregister_netdevice_queue(ndev, head);
570aa730a99SLoic Poulain }
571aa730a99SLoic Poulain
mhi_mbim_setup(struct net_device * ndev)572aa730a99SLoic Poulain static void mhi_mbim_setup(struct net_device *ndev)
573aa730a99SLoic Poulain {
574aa730a99SLoic Poulain ndev->header_ops = NULL; /* No header */
575aa730a99SLoic Poulain ndev->type = ARPHRD_RAWIP;
576aa730a99SLoic Poulain ndev->needed_headroom = sizeof(struct mbim_tx_hdr);
577aa730a99SLoic Poulain ndev->hard_header_len = 0;
578aa730a99SLoic Poulain ndev->addr_len = 0;
579aa730a99SLoic Poulain ndev->flags = IFF_POINTOPOINT | IFF_NOARP;
580aa730a99SLoic Poulain ndev->netdev_ops = &mhi_mbim_ndo;
581aa730a99SLoic Poulain ndev->mtu = MHI_MBIM_DEFAULT_MTU;
582aa730a99SLoic Poulain ndev->min_mtu = ETH_MIN_MTU;
583aa730a99SLoic Poulain ndev->max_mtu = MHI_MAX_BUF_SZ - ndev->needed_headroom;
584aa730a99SLoic Poulain ndev->tx_queue_len = 1000;
585*668205b9SHW He ndev->needs_free_netdev = true;
586aa730a99SLoic Poulain }
587aa730a99SLoic Poulain
588aa730a99SLoic Poulain static const struct wwan_ops mhi_mbim_wwan_ops = {
589aa730a99SLoic Poulain .priv_size = sizeof(struct mhi_mbim_link),
590aa730a99SLoic Poulain .setup = mhi_mbim_setup,
591aa730a99SLoic Poulain .newlink = mhi_mbim_newlink,
592aa730a99SLoic Poulain .dellink = mhi_mbim_dellink,
593aa730a99SLoic Poulain };
594aa730a99SLoic Poulain
mhi_mbim_probe(struct mhi_device * mhi_dev,const struct mhi_device_id * id)595aa730a99SLoic Poulain static int mhi_mbim_probe(struct mhi_device *mhi_dev, const struct mhi_device_id *id)
596aa730a99SLoic Poulain {
597aa730a99SLoic Poulain struct mhi_controller *cntrl = mhi_dev->mhi_cntrl;
598aa730a99SLoic Poulain struct mhi_mbim_context *mbim;
599aa730a99SLoic Poulain int err;
600aa730a99SLoic Poulain
601aa730a99SLoic Poulain mbim = devm_kzalloc(&mhi_dev->dev, sizeof(*mbim), GFP_KERNEL);
602aa730a99SLoic Poulain if (!mbim)
603aa730a99SLoic Poulain return -ENOMEM;
604aa730a99SLoic Poulain
60594c0a6fbSWei Yongjun spin_lock_init(&mbim->tx_lock);
606aa730a99SLoic Poulain dev_set_drvdata(&mhi_dev->dev, mbim);
607aa730a99SLoic Poulain mbim->mdev = mhi_dev;
608aa730a99SLoic Poulain mbim->mru = mhi_dev->mhi_cntrl->mru ? mhi_dev->mhi_cntrl->mru : MHI_DEFAULT_MRU;
609aa730a99SLoic Poulain
610aa730a99SLoic Poulain INIT_DELAYED_WORK(&mbim->rx_refill, mhi_net_rx_refill_work);
611aa730a99SLoic Poulain
612aa730a99SLoic Poulain /* Start MHI channels */
61397c78d0aSJakub Kicinski err = mhi_prepare_for_transfer(mhi_dev);
614aa730a99SLoic Poulain if (err)
615aa730a99SLoic Poulain return err;
616aa730a99SLoic Poulain
617aa730a99SLoic Poulain /* Number of transfer descriptors determines size of the queue */
618aa730a99SLoic Poulain mbim->rx_queue_sz = mhi_get_free_desc_count(mhi_dev, DMA_FROM_DEVICE);
619aa730a99SLoic Poulain
620aa730a99SLoic Poulain /* Register wwan link ops with MHI controller representing WWAN instance */
621aa730a99SLoic Poulain return wwan_register_ops(&cntrl->mhi_dev->dev, &mhi_mbim_wwan_ops, mbim, 0);
622aa730a99SLoic Poulain }
623aa730a99SLoic Poulain
mhi_mbim_remove(struct mhi_device * mhi_dev)624aa730a99SLoic Poulain static void mhi_mbim_remove(struct mhi_device *mhi_dev)
625aa730a99SLoic Poulain {
626aa730a99SLoic Poulain struct mhi_mbim_context *mbim = dev_get_drvdata(&mhi_dev->dev);
627aa730a99SLoic Poulain struct mhi_controller *cntrl = mhi_dev->mhi_cntrl;
628aa730a99SLoic Poulain
629aa730a99SLoic Poulain mhi_unprepare_from_transfer(mhi_dev);
630aa730a99SLoic Poulain cancel_delayed_work_sync(&mbim->rx_refill);
631aa730a99SLoic Poulain wwan_unregister_ops(&cntrl->mhi_dev->dev);
632aa730a99SLoic Poulain kfree_skb(mbim->skbagg_head);
633aa730a99SLoic Poulain dev_set_drvdata(&mhi_dev->dev, NULL);
634aa730a99SLoic Poulain }
635aa730a99SLoic Poulain
636aa730a99SLoic Poulain static const struct mhi_device_id mhi_mbim_id_table[] = {
637aa730a99SLoic Poulain /* Hardware accelerated data PATH (to modem IPA), MBIM protocol */
638aa730a99SLoic Poulain { .chan = "IP_HW0_MBIM", .driver_data = 0 },
639aa730a99SLoic Poulain {}
640aa730a99SLoic Poulain };
641aa730a99SLoic Poulain MODULE_DEVICE_TABLE(mhi, mhi_mbim_id_table);
642aa730a99SLoic Poulain
643aa730a99SLoic Poulain static struct mhi_driver mhi_mbim_driver = {
644aa730a99SLoic Poulain .probe = mhi_mbim_probe,
645aa730a99SLoic Poulain .remove = mhi_mbim_remove,
646aa730a99SLoic Poulain .dl_xfer_cb = mhi_mbim_dl_callback,
647aa730a99SLoic Poulain .ul_xfer_cb = mhi_mbim_ul_callback,
648aa730a99SLoic Poulain .id_table = mhi_mbim_id_table,
649aa730a99SLoic Poulain .driver = {
650aa730a99SLoic Poulain .name = "mhi_wwan_mbim",
651aa730a99SLoic Poulain .owner = THIS_MODULE,
652aa730a99SLoic Poulain },
653aa730a99SLoic Poulain };
654aa730a99SLoic Poulain
655aa730a99SLoic Poulain module_mhi_driver(mhi_mbim_driver);
656aa730a99SLoic Poulain
657aa730a99SLoic Poulain MODULE_AUTHOR("Loic Poulain <loic.poulain@linaro.org>");
658aa730a99SLoic Poulain MODULE_DESCRIPTION("Network/MBIM over MHI");
659aa730a99SLoic Poulain MODULE_LICENSE("GPL v2");
660