Merge remote-tracking branch 'torvalds/master' into perf/core
[linux-2.6-microblaze.git] / drivers / net / mhi / net.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* MHI Network driver - Network over MHI bus
3  *
4  * Copyright (C) 2020 Linaro Ltd <loic.poulain@linaro.org>
5  */
6
7 #include <linux/if_arp.h>
8 #include <linux/mhi.h>
9 #include <linux/mod_devicetable.h>
10 #include <linux/module.h>
11 #include <linux/netdevice.h>
12 #include <linux/skbuff.h>
13 #include <linux/u64_stats_sync.h>
14
15 #include "mhi.h"
16
17 #define MHI_NET_MIN_MTU         ETH_MIN_MTU
18 #define MHI_NET_MAX_MTU         0xffff
19 #define MHI_NET_DEFAULT_MTU     0x4000
20
21 struct mhi_device_info {
22         const char *netname;
23         const struct mhi_net_proto *proto;
24 };
25
26 static int mhi_ndo_open(struct net_device *ndev)
27 {
28         struct mhi_net_dev *mhi_netdev = netdev_priv(ndev);
29
30         /* Feed the rx buffer pool */
31         schedule_delayed_work(&mhi_netdev->rx_refill, 0);
32
33         /* Carrier is established via out-of-band channel (e.g. qmi) */
34         netif_carrier_on(ndev);
35
36         netif_start_queue(ndev);
37
38         return 0;
39 }
40
41 static int mhi_ndo_stop(struct net_device *ndev)
42 {
43         struct mhi_net_dev *mhi_netdev = netdev_priv(ndev);
44
45         netif_stop_queue(ndev);
46         netif_carrier_off(ndev);
47         cancel_delayed_work_sync(&mhi_netdev->rx_refill);
48
49         return 0;
50 }
51
52 static netdev_tx_t mhi_ndo_xmit(struct sk_buff *skb, struct net_device *ndev)
53 {
54         struct mhi_net_dev *mhi_netdev = netdev_priv(ndev);
55         const struct mhi_net_proto *proto = mhi_netdev->proto;
56         struct mhi_device *mdev = mhi_netdev->mdev;
57         int err;
58
59         if (proto && proto->tx_fixup) {
60                 skb = proto->tx_fixup(mhi_netdev, skb);
61                 if (unlikely(!skb))
62                         goto exit_drop;
63         }
64
65         err = mhi_queue_skb(mdev, DMA_TO_DEVICE, skb, skb->len, MHI_EOT);
66         if (unlikely(err)) {
67                 net_err_ratelimited("%s: Failed to queue TX buf (%d)\n",
68                                     ndev->name, err);
69                 dev_kfree_skb_any(skb);
70                 goto exit_drop;
71         }
72
73         if (mhi_queue_is_full(mdev, DMA_TO_DEVICE))
74                 netif_stop_queue(ndev);
75
76         return NETDEV_TX_OK;
77
78 exit_drop:
79         u64_stats_update_begin(&mhi_netdev->stats.tx_syncp);
80         u64_stats_inc(&mhi_netdev->stats.tx_dropped);
81         u64_stats_update_end(&mhi_netdev->stats.tx_syncp);
82
83         return NETDEV_TX_OK;
84 }
85
86 static void mhi_ndo_get_stats64(struct net_device *ndev,
87                                 struct rtnl_link_stats64 *stats)
88 {
89         struct mhi_net_dev *mhi_netdev = netdev_priv(ndev);
90         unsigned int start;
91
92         do {
93                 start = u64_stats_fetch_begin_irq(&mhi_netdev->stats.rx_syncp);
94                 stats->rx_packets = u64_stats_read(&mhi_netdev->stats.rx_packets);
95                 stats->rx_bytes = u64_stats_read(&mhi_netdev->stats.rx_bytes);
96                 stats->rx_errors = u64_stats_read(&mhi_netdev->stats.rx_errors);
97                 stats->rx_dropped = u64_stats_read(&mhi_netdev->stats.rx_dropped);
98                 stats->rx_length_errors = u64_stats_read(&mhi_netdev->stats.rx_length_errors);
99         } while (u64_stats_fetch_retry_irq(&mhi_netdev->stats.rx_syncp, start));
100
101         do {
102                 start = u64_stats_fetch_begin_irq(&mhi_netdev->stats.tx_syncp);
103                 stats->tx_packets = u64_stats_read(&mhi_netdev->stats.tx_packets);
104                 stats->tx_bytes = u64_stats_read(&mhi_netdev->stats.tx_bytes);
105                 stats->tx_errors = u64_stats_read(&mhi_netdev->stats.tx_errors);
106                 stats->tx_dropped = u64_stats_read(&mhi_netdev->stats.tx_dropped);
107         } while (u64_stats_fetch_retry_irq(&mhi_netdev->stats.tx_syncp, start));
108 }
109
110 static const struct net_device_ops mhi_netdev_ops = {
111         .ndo_open               = mhi_ndo_open,
112         .ndo_stop               = mhi_ndo_stop,
113         .ndo_start_xmit         = mhi_ndo_xmit,
114         .ndo_get_stats64        = mhi_ndo_get_stats64,
115 };
116
117 static void mhi_net_setup(struct net_device *ndev)
118 {
119         ndev->header_ops = NULL;  /* No header */
120         ndev->type = ARPHRD_RAWIP;
121         ndev->hard_header_len = 0;
122         ndev->addr_len = 0;
123         ndev->flags = IFF_POINTOPOINT | IFF_NOARP;
124         ndev->netdev_ops = &mhi_netdev_ops;
125         ndev->mtu = MHI_NET_DEFAULT_MTU;
126         ndev->min_mtu = MHI_NET_MIN_MTU;
127         ndev->max_mtu = MHI_NET_MAX_MTU;
128         ndev->tx_queue_len = 1000;
129 }
130
131 static struct sk_buff *mhi_net_skb_agg(struct mhi_net_dev *mhi_netdev,
132                                        struct sk_buff *skb)
133 {
134         struct sk_buff *head = mhi_netdev->skbagg_head;
135         struct sk_buff *tail = mhi_netdev->skbagg_tail;
136
137         /* This is non-paged skb chaining using frag_list */
138         if (!head) {
139                 mhi_netdev->skbagg_head = skb;
140                 return skb;
141         }
142
143         if (!skb_shinfo(head)->frag_list)
144                 skb_shinfo(head)->frag_list = skb;
145         else
146                 tail->next = skb;
147
148         head->len += skb->len;
149         head->data_len += skb->len;
150         head->truesize += skb->truesize;
151
152         mhi_netdev->skbagg_tail = skb;
153
154         return mhi_netdev->skbagg_head;
155 }
156
157 static void mhi_net_dl_callback(struct mhi_device *mhi_dev,
158                                 struct mhi_result *mhi_res)
159 {
160         struct mhi_net_dev *mhi_netdev = dev_get_drvdata(&mhi_dev->dev);
161         const struct mhi_net_proto *proto = mhi_netdev->proto;
162         struct sk_buff *skb = mhi_res->buf_addr;
163         int free_desc_count;
164
165         free_desc_count = mhi_get_free_desc_count(mhi_dev, DMA_FROM_DEVICE);
166
167         if (unlikely(mhi_res->transaction_status)) {
168                 switch (mhi_res->transaction_status) {
169                 case -EOVERFLOW:
170                         /* Packet can not fit in one MHI buffer and has been
171                          * split over multiple MHI transfers, do re-aggregation.
172                          * That usually means the device side MTU is larger than
173                          * the host side MTU/MRU. Since this is not optimal,
174                          * print a warning (once).
175                          */
176                         netdev_warn_once(mhi_netdev->ndev,
177                                          "Fragmented packets received, fix MTU?\n");
178                         skb_put(skb, mhi_res->bytes_xferd);
179                         mhi_net_skb_agg(mhi_netdev, skb);
180                         break;
181                 case -ENOTCONN:
182                         /* MHI layer stopping/resetting the DL channel */
183                         dev_kfree_skb_any(skb);
184                         return;
185                 default:
186                         /* Unknown error, simply drop */
187                         dev_kfree_skb_any(skb);
188                         u64_stats_update_begin(&mhi_netdev->stats.rx_syncp);
189                         u64_stats_inc(&mhi_netdev->stats.rx_errors);
190                         u64_stats_update_end(&mhi_netdev->stats.rx_syncp);
191                 }
192         } else {
193                 skb_put(skb, mhi_res->bytes_xferd);
194
195                 if (mhi_netdev->skbagg_head) {
196                         /* Aggregate the final fragment */
197                         skb = mhi_net_skb_agg(mhi_netdev, skb);
198                         mhi_netdev->skbagg_head = NULL;
199                 }
200
201                 u64_stats_update_begin(&mhi_netdev->stats.rx_syncp);
202                 u64_stats_inc(&mhi_netdev->stats.rx_packets);
203                 u64_stats_add(&mhi_netdev->stats.rx_bytes, skb->len);
204                 u64_stats_update_end(&mhi_netdev->stats.rx_syncp);
205
206                 switch (skb->data[0] & 0xf0) {
207                 case 0x40:
208                         skb->protocol = htons(ETH_P_IP);
209                         break;
210                 case 0x60:
211                         skb->protocol = htons(ETH_P_IPV6);
212                         break;
213                 default:
214                         skb->protocol = htons(ETH_P_MAP);
215                         break;
216                 }
217
218                 if (proto && proto->rx)
219                         proto->rx(mhi_netdev, skb);
220                 else
221                         netif_rx(skb);
222         }
223
224         /* Refill if RX buffers queue becomes low */
225         if (free_desc_count >= mhi_netdev->rx_queue_sz / 2)
226                 schedule_delayed_work(&mhi_netdev->rx_refill, 0);
227 }
228
229 static void mhi_net_ul_callback(struct mhi_device *mhi_dev,
230                                 struct mhi_result *mhi_res)
231 {
232         struct mhi_net_dev *mhi_netdev = dev_get_drvdata(&mhi_dev->dev);
233         struct net_device *ndev = mhi_netdev->ndev;
234         struct mhi_device *mdev = mhi_netdev->mdev;
235         struct sk_buff *skb = mhi_res->buf_addr;
236
237         /* Hardware has consumed the buffer, so free the skb (which is not
238          * freed by the MHI stack) and perform accounting.
239          */
240         dev_consume_skb_any(skb);
241
242         u64_stats_update_begin(&mhi_netdev->stats.tx_syncp);
243         if (unlikely(mhi_res->transaction_status)) {
244
245                 /* MHI layer stopping/resetting the UL channel */
246                 if (mhi_res->transaction_status == -ENOTCONN) {
247                         u64_stats_update_end(&mhi_netdev->stats.tx_syncp);
248                         return;
249                 }
250
251                 u64_stats_inc(&mhi_netdev->stats.tx_errors);
252         } else {
253                 u64_stats_inc(&mhi_netdev->stats.tx_packets);
254                 u64_stats_add(&mhi_netdev->stats.tx_bytes, mhi_res->bytes_xferd);
255         }
256         u64_stats_update_end(&mhi_netdev->stats.tx_syncp);
257
258         if (netif_queue_stopped(ndev) && !mhi_queue_is_full(mdev, DMA_TO_DEVICE))
259                 netif_wake_queue(ndev);
260 }
261
262 static void mhi_net_rx_refill_work(struct work_struct *work)
263 {
264         struct mhi_net_dev *mhi_netdev = container_of(work, struct mhi_net_dev,
265                                                       rx_refill.work);
266         struct net_device *ndev = mhi_netdev->ndev;
267         struct mhi_device *mdev = mhi_netdev->mdev;
268         struct sk_buff *skb;
269         unsigned int size;
270         int err;
271
272         size = mhi_netdev->mru ? mhi_netdev->mru : READ_ONCE(ndev->mtu);
273
274         while (!mhi_queue_is_full(mdev, DMA_FROM_DEVICE)) {
275                 skb = netdev_alloc_skb(ndev, size);
276                 if (unlikely(!skb))
277                         break;
278
279                 err = mhi_queue_skb(mdev, DMA_FROM_DEVICE, skb, size, MHI_EOT);
280                 if (unlikely(err)) {
281                         net_err_ratelimited("%s: Failed to queue RX buf (%d)\n",
282                                             ndev->name, err);
283                         kfree_skb(skb);
284                         break;
285                 }
286
287                 /* Do not hog the CPU if rx buffers are consumed faster than
288                  * queued (unlikely).
289                  */
290                 cond_resched();
291         }
292
293         /* If we're still starved of rx buffers, reschedule later */
294         if (mhi_get_free_desc_count(mdev, DMA_FROM_DEVICE) == mhi_netdev->rx_queue_sz)
295                 schedule_delayed_work(&mhi_netdev->rx_refill, HZ / 2);
296 }
297
298 static struct device_type wwan_type = {
299         .name = "wwan",
300 };
301
302 static int mhi_net_probe(struct mhi_device *mhi_dev,
303                          const struct mhi_device_id *id)
304 {
305         const struct mhi_device_info *info = (struct mhi_device_info *)id->driver_data;
306         struct device *dev = &mhi_dev->dev;
307         struct mhi_net_dev *mhi_netdev;
308         struct net_device *ndev;
309         int err;
310
311         ndev = alloc_netdev(sizeof(*mhi_netdev), info->netname,
312                             NET_NAME_PREDICTABLE, mhi_net_setup);
313         if (!ndev)
314                 return -ENOMEM;
315
316         mhi_netdev = netdev_priv(ndev);
317         dev_set_drvdata(dev, mhi_netdev);
318         mhi_netdev->ndev = ndev;
319         mhi_netdev->mdev = mhi_dev;
320         mhi_netdev->skbagg_head = NULL;
321         mhi_netdev->proto = info->proto;
322         SET_NETDEV_DEV(ndev, &mhi_dev->dev);
323         SET_NETDEV_DEVTYPE(ndev, &wwan_type);
324
325         INIT_DELAYED_WORK(&mhi_netdev->rx_refill, mhi_net_rx_refill_work);
326         u64_stats_init(&mhi_netdev->stats.rx_syncp);
327         u64_stats_init(&mhi_netdev->stats.tx_syncp);
328
329         /* Start MHI channels */
330         err = mhi_prepare_for_transfer(mhi_dev);
331         if (err)
332                 goto out_err;
333
334         /* Number of transfer descriptors determines size of the queue */
335         mhi_netdev->rx_queue_sz = mhi_get_free_desc_count(mhi_dev, DMA_FROM_DEVICE);
336
337         err = register_netdev(ndev);
338         if (err)
339                 goto out_err;
340
341         if (mhi_netdev->proto) {
342                 err = mhi_netdev->proto->init(mhi_netdev);
343                 if (err)
344                         goto out_err_proto;
345         }
346
347         return 0;
348
349 out_err_proto:
350         unregister_netdev(ndev);
351 out_err:
352         free_netdev(ndev);
353         return err;
354 }
355
356 static void mhi_net_remove(struct mhi_device *mhi_dev)
357 {
358         struct mhi_net_dev *mhi_netdev = dev_get_drvdata(&mhi_dev->dev);
359
360         unregister_netdev(mhi_netdev->ndev);
361
362         mhi_unprepare_from_transfer(mhi_netdev->mdev);
363
364         kfree_skb(mhi_netdev->skbagg_head);
365
366         free_netdev(mhi_netdev->ndev);
367 }
368
369 static const struct mhi_device_info mhi_hwip0 = {
370         .netname = "mhi_hwip%d",
371 };
372
373 static const struct mhi_device_info mhi_swip0 = {
374         .netname = "mhi_swip%d",
375 };
376
377 static const struct mhi_device_info mhi_hwip0_mbim = {
378         .netname = "mhi_mbim%d",
379         .proto = &proto_mbim,
380 };
381
382 static const struct mhi_device_id mhi_net_id_table[] = {
383         /* Hardware accelerated data PATH (to modem IPA), protocol agnostic */
384         { .chan = "IP_HW0", .driver_data = (kernel_ulong_t)&mhi_hwip0 },
385         /* Software data PATH (to modem CPU) */
386         { .chan = "IP_SW0", .driver_data = (kernel_ulong_t)&mhi_swip0 },
387         /* Hardware accelerated data PATH (to modem IPA), MBIM protocol */
388         { .chan = "IP_HW0_MBIM", .driver_data = (kernel_ulong_t)&mhi_hwip0_mbim },
389         {}
390 };
391 MODULE_DEVICE_TABLE(mhi, mhi_net_id_table);
392
393 static struct mhi_driver mhi_net_driver = {
394         .probe = mhi_net_probe,
395         .remove = mhi_net_remove,
396         .dl_xfer_cb = mhi_net_dl_callback,
397         .ul_xfer_cb = mhi_net_ul_callback,
398         .id_table = mhi_net_id_table,
399         .driver = {
400                 .name = "mhi_net",
401                 .owner = THIS_MODULE,
402         },
403 };
404
405 module_mhi_driver(mhi_net_driver);
406
407 MODULE_AUTHOR("Loic Poulain <loic.poulain@linaro.org>");
408 MODULE_DESCRIPTION("Network over MHI");
409 MODULE_LICENSE("GPL v2");