Merge branch 'core-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[sfrench/cifs-2.6.git] / drivers / net / can / rx-offload.c
1 /*
2  * Copyright (c) 2014 David Jander, Protonic Holland
3  * Copyright (C) 2014-2017 Pengutronix, Marc Kleine-Budde <kernel@pengutronix.de>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the version 2 of the GNU General Public License
7  * as published by the Free Software Foundation
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <linux/can/dev.h>
19 #include <linux/can/rx-offload.h>
20
21 struct can_rx_offload_cb {
22         u32 timestamp;
23 };
24
25 static inline struct can_rx_offload_cb *can_rx_offload_get_cb(struct sk_buff *skb)
26 {
27         BUILD_BUG_ON(sizeof(struct can_rx_offload_cb) > sizeof(skb->cb));
28
29         return (struct can_rx_offload_cb *)skb->cb;
30 }
31
32 static inline bool can_rx_offload_le(struct can_rx_offload *offload, unsigned int a, unsigned int b)
33 {
34         if (offload->inc)
35                 return a <= b;
36         else
37                 return a >= b;
38 }
39
40 static inline unsigned int can_rx_offload_inc(struct can_rx_offload *offload, unsigned int *val)
41 {
42         if (offload->inc)
43                 return (*val)++;
44         else
45                 return (*val)--;
46 }
47
48 static int can_rx_offload_napi_poll(struct napi_struct *napi, int quota)
49 {
50         struct can_rx_offload *offload = container_of(napi, struct can_rx_offload, napi);
51         struct net_device *dev = offload->dev;
52         struct net_device_stats *stats = &dev->stats;
53         struct sk_buff *skb;
54         int work_done = 0;
55
56         while ((work_done < quota) &&
57                (skb = skb_dequeue(&offload->skb_queue))) {
58                 struct can_frame *cf = (struct can_frame *)skb->data;
59
60                 work_done++;
61                 stats->rx_packets++;
62                 stats->rx_bytes += cf->can_dlc;
63                 netif_receive_skb(skb);
64         }
65
66         if (work_done < quota) {
67                 napi_complete_done(napi, work_done);
68
69                 /* Check if there was another interrupt */
70                 if (!skb_queue_empty(&offload->skb_queue))
71                         napi_reschedule(&offload->napi);
72         }
73
74         can_led_event(offload->dev, CAN_LED_EVENT_RX);
75
76         return work_done;
77 }
78
79 static inline void __skb_queue_add_sort(struct sk_buff_head *head, struct sk_buff *new,
80                                         int (*compare)(struct sk_buff *a, struct sk_buff *b))
81 {
82         struct sk_buff *pos, *insert = NULL;
83
84         skb_queue_reverse_walk(head, pos) {
85                 const struct can_rx_offload_cb *cb_pos, *cb_new;
86
87                 cb_pos = can_rx_offload_get_cb(pos);
88                 cb_new = can_rx_offload_get_cb(new);
89
90                 netdev_dbg(new->dev,
91                            "%s: pos=0x%08x, new=0x%08x, diff=%10d, queue_len=%d\n",
92                            __func__,
93                            cb_pos->timestamp, cb_new->timestamp,
94                            cb_new->timestamp - cb_pos->timestamp,
95                            skb_queue_len(head));
96
97                 if (compare(pos, new) < 0)
98                         continue;
99                 insert = pos;
100                 break;
101         }
102         if (!insert)
103                 __skb_queue_head(head, new);
104         else
105                 __skb_queue_after(head, insert, new);
106 }
107
108 static int can_rx_offload_compare(struct sk_buff *a, struct sk_buff *b)
109 {
110         const struct can_rx_offload_cb *cb_a, *cb_b;
111
112         cb_a = can_rx_offload_get_cb(a);
113         cb_b = can_rx_offload_get_cb(b);
114
115         /* Substract two u32 and return result as int, to keep
116          * difference steady around the u32 overflow.
117          */
118         return cb_b->timestamp - cb_a->timestamp;
119 }
120
121 static struct sk_buff *can_rx_offload_offload_one(struct can_rx_offload *offload, unsigned int n)
122 {
123         struct sk_buff *skb = NULL;
124         struct can_rx_offload_cb *cb;
125         struct can_frame *cf;
126         int ret;
127
128         /* If queue is full or skb not available, read to discard mailbox */
129         if (likely(skb_queue_len(&offload->skb_queue) <=
130                    offload->skb_queue_len_max))
131                 skb = alloc_can_skb(offload->dev, &cf);
132
133         if (!skb) {
134                 struct can_frame cf_overflow;
135                 u32 timestamp;
136
137                 ret = offload->mailbox_read(offload, &cf_overflow,
138                                             &timestamp, n);
139                 if (ret)
140                         offload->dev->stats.rx_dropped++;
141
142                 return NULL;
143         }
144
145         cb = can_rx_offload_get_cb(skb);
146         ret = offload->mailbox_read(offload, cf, &cb->timestamp, n);
147         if (!ret) {
148                 kfree_skb(skb);
149                 return NULL;
150         }
151
152         return skb;
153 }
154
155 int can_rx_offload_irq_offload_timestamp(struct can_rx_offload *offload, u64 pending)
156 {
157         struct sk_buff_head skb_queue;
158         unsigned int i;
159
160         __skb_queue_head_init(&skb_queue);
161
162         for (i = offload->mb_first;
163              can_rx_offload_le(offload, i, offload->mb_last);
164              can_rx_offload_inc(offload, &i)) {
165                 struct sk_buff *skb;
166
167                 if (!(pending & BIT_ULL(i)))
168                         continue;
169
170                 skb = can_rx_offload_offload_one(offload, i);
171                 if (!skb)
172                         break;
173
174                 __skb_queue_add_sort(&skb_queue, skb, can_rx_offload_compare);
175         }
176
177         if (!skb_queue_empty(&skb_queue)) {
178                 unsigned long flags;
179                 u32 queue_len;
180
181                 spin_lock_irqsave(&offload->skb_queue.lock, flags);
182                 skb_queue_splice_tail(&skb_queue, &offload->skb_queue);
183                 spin_unlock_irqrestore(&offload->skb_queue.lock, flags);
184
185                 if ((queue_len = skb_queue_len(&offload->skb_queue)) >
186                     (offload->skb_queue_len_max / 8))
187                         netdev_dbg(offload->dev, "%s: queue_len=%d\n",
188                                    __func__, queue_len);
189
190                 can_rx_offload_schedule(offload);
191         }
192
193         return skb_queue_len(&skb_queue);
194 }
195 EXPORT_SYMBOL_GPL(can_rx_offload_irq_offload_timestamp);
196
197 int can_rx_offload_irq_offload_fifo(struct can_rx_offload *offload)
198 {
199         struct sk_buff *skb;
200         int received = 0;
201
202         while ((skb = can_rx_offload_offload_one(offload, 0))) {
203                 skb_queue_tail(&offload->skb_queue, skb);
204                 received++;
205         }
206
207         if (received)
208                 can_rx_offload_schedule(offload);
209
210         return received;
211 }
212 EXPORT_SYMBOL_GPL(can_rx_offload_irq_offload_fifo);
213
214 int can_rx_offload_queue_sorted(struct can_rx_offload *offload,
215                                 struct sk_buff *skb, u32 timestamp)
216 {
217         struct can_rx_offload_cb *cb;
218         unsigned long flags;
219
220         if (skb_queue_len(&offload->skb_queue) >
221             offload->skb_queue_len_max)
222                 return -ENOMEM;
223
224         cb = can_rx_offload_get_cb(skb);
225         cb->timestamp = timestamp;
226
227         spin_lock_irqsave(&offload->skb_queue.lock, flags);
228         __skb_queue_add_sort(&offload->skb_queue, skb, can_rx_offload_compare);
229         spin_unlock_irqrestore(&offload->skb_queue.lock, flags);
230
231         can_rx_offload_schedule(offload);
232
233         return 0;
234 }
235 EXPORT_SYMBOL_GPL(can_rx_offload_queue_sorted);
236
237 unsigned int can_rx_offload_get_echo_skb(struct can_rx_offload *offload,
238                                          unsigned int idx, u32 timestamp)
239 {
240         struct net_device *dev = offload->dev;
241         struct net_device_stats *stats = &dev->stats;
242         struct sk_buff *skb;
243         u8 len;
244         int err;
245
246         skb = __can_get_echo_skb(dev, idx, &len);
247         if (!skb)
248                 return 0;
249
250         err = can_rx_offload_queue_sorted(offload, skb, timestamp);
251         if (err) {
252                 stats->rx_errors++;
253                 stats->tx_fifo_errors++;
254         }
255
256         return len;
257 }
258 EXPORT_SYMBOL_GPL(can_rx_offload_get_echo_skb);
259
260 int can_rx_offload_queue_tail(struct can_rx_offload *offload,
261                               struct sk_buff *skb)
262 {
263         if (skb_queue_len(&offload->skb_queue) >
264             offload->skb_queue_len_max)
265                 return -ENOMEM;
266
267         skb_queue_tail(&offload->skb_queue, skb);
268         can_rx_offload_schedule(offload);
269
270         return 0;
271 }
272 EXPORT_SYMBOL_GPL(can_rx_offload_queue_tail);
273
274 static int can_rx_offload_init_queue(struct net_device *dev, struct can_rx_offload *offload, unsigned int weight)
275 {
276         offload->dev = dev;
277
278         /* Limit queue len to 4x the weight (rounted to next power of two) */
279         offload->skb_queue_len_max = 2 << fls(weight);
280         offload->skb_queue_len_max *= 4;
281         skb_queue_head_init(&offload->skb_queue);
282
283         can_rx_offload_reset(offload);
284         netif_napi_add(dev, &offload->napi, can_rx_offload_napi_poll, weight);
285
286         dev_dbg(dev->dev.parent, "%s: skb_queue_len_max=%d\n",
287                 __func__, offload->skb_queue_len_max);
288
289         return 0;
290 }
291
292 int can_rx_offload_add_timestamp(struct net_device *dev, struct can_rx_offload *offload)
293 {
294         unsigned int weight;
295
296         if (offload->mb_first > BITS_PER_LONG_LONG ||
297             offload->mb_last > BITS_PER_LONG_LONG || !offload->mailbox_read)
298                 return -EINVAL;
299
300         if (offload->mb_first < offload->mb_last) {
301                 offload->inc = true;
302                 weight = offload->mb_last - offload->mb_first;
303         } else {
304                 offload->inc = false;
305                 weight = offload->mb_first - offload->mb_last;
306         }
307
308         return can_rx_offload_init_queue(dev, offload, weight);
309 }
310 EXPORT_SYMBOL_GPL(can_rx_offload_add_timestamp);
311
312 int can_rx_offload_add_fifo(struct net_device *dev, struct can_rx_offload *offload, unsigned int weight)
313 {
314         if (!offload->mailbox_read)
315                 return -EINVAL;
316
317         return can_rx_offload_init_queue(dev, offload, weight);
318 }
319 EXPORT_SYMBOL_GPL(can_rx_offload_add_fifo);
320
321 void can_rx_offload_enable(struct can_rx_offload *offload)
322 {
323         can_rx_offload_reset(offload);
324         napi_enable(&offload->napi);
325 }
326 EXPORT_SYMBOL_GPL(can_rx_offload_enable);
327
328 void can_rx_offload_del(struct can_rx_offload *offload)
329 {
330         netif_napi_del(&offload->napi);
331         skb_queue_purge(&offload->skb_queue);
332 }
333 EXPORT_SYMBOL_GPL(can_rx_offload_del);
334
335 void can_rx_offload_reset(struct can_rx_offload *offload)
336 {
337 }
338 EXPORT_SYMBOL_GPL(can_rx_offload_reset);