can: rx-offload: can_rx_offload_compare(): fix typo
[linux-2.6-microblaze.git] / drivers / net / can / rx-offload.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2014      Protonic Holland,
3  *                         David Jander
4  * Copyright (C) 2014-2017 Pengutronix,
5  *                         Marc Kleine-Budde <kernel@pengutronix.de>
6  */
7
8 #include <linux/can/dev.h>
9 #include <linux/can/rx-offload.h>
10
11 struct can_rx_offload_cb {
12         u32 timestamp;
13 };
14
15 static inline struct can_rx_offload_cb *
16 can_rx_offload_get_cb(struct sk_buff *skb)
17 {
18         BUILD_BUG_ON(sizeof(struct can_rx_offload_cb) > sizeof(skb->cb));
19
20         return (struct can_rx_offload_cb *)skb->cb;
21 }
22
23 static inline bool
24 can_rx_offload_le(struct can_rx_offload *offload,
25                   unsigned int a, unsigned int b)
26 {
27         if (offload->inc)
28                 return a <= b;
29         else
30                 return a >= b;
31 }
32
33 static inline unsigned int
34 can_rx_offload_inc(struct can_rx_offload *offload, unsigned int *val)
35 {
36         if (offload->inc)
37                 return (*val)++;
38         else
39                 return (*val)--;
40 }
41
42 static int can_rx_offload_napi_poll(struct napi_struct *napi, int quota)
43 {
44         struct can_rx_offload *offload = container_of(napi,
45                                                       struct can_rx_offload,
46                                                       napi);
47         struct net_device *dev = offload->dev;
48         struct net_device_stats *stats = &dev->stats;
49         struct sk_buff *skb;
50         int work_done = 0;
51
52         while ((work_done < quota) &&
53                (skb = skb_dequeue(&offload->skb_queue))) {
54                 struct can_frame *cf = (struct can_frame *)skb->data;
55
56                 work_done++;
57                 stats->rx_packets++;
58                 stats->rx_bytes += cf->can_dlc;
59                 netif_receive_skb(skb);
60         }
61
62         if (work_done < quota) {
63                 napi_complete_done(napi, work_done);
64
65                 /* Check if there was another interrupt */
66                 if (!skb_queue_empty(&offload->skb_queue))
67                         napi_reschedule(&offload->napi);
68         }
69
70         can_led_event(offload->dev, CAN_LED_EVENT_RX);
71
72         return work_done;
73 }
74
75 static inline void
76 __skb_queue_add_sort(struct sk_buff_head *head, struct sk_buff *new,
77                      int (*compare)(struct sk_buff *a, struct sk_buff *b))
78 {
79         struct sk_buff *pos, *insert = NULL;
80
81         skb_queue_reverse_walk(head, pos) {
82                 const struct can_rx_offload_cb *cb_pos, *cb_new;
83
84                 cb_pos = can_rx_offload_get_cb(pos);
85                 cb_new = can_rx_offload_get_cb(new);
86
87                 netdev_dbg(new->dev,
88                            "%s: pos=0x%08x, new=0x%08x, diff=%10d, queue_len=%d\n",
89                            __func__,
90                            cb_pos->timestamp, cb_new->timestamp,
91                            cb_new->timestamp - cb_pos->timestamp,
92                            skb_queue_len(head));
93
94                 if (compare(pos, new) < 0)
95                         continue;
96                 insert = pos;
97                 break;
98         }
99         if (!insert)
100                 __skb_queue_head(head, new);
101         else
102                 __skb_queue_after(head, insert, new);
103 }
104
105 static int can_rx_offload_compare(struct sk_buff *a, struct sk_buff *b)
106 {
107         const struct can_rx_offload_cb *cb_a, *cb_b;
108
109         cb_a = can_rx_offload_get_cb(a);
110         cb_b = can_rx_offload_get_cb(b);
111
112         /* Subtract two u32 and return result as int, to keep
113          * difference steady around the u32 overflow.
114          */
115         return cb_b->timestamp - cb_a->timestamp;
116 }
117
118 /**
119  * can_rx_offload_offload_one() - Read one CAN frame from HW
120  * @offload: pointer to rx_offload context
121  * @n: number of mailbox to read
122  *
123  * The task of this function is to read a CAN frame from mailbox @n
124  * from the device and return the mailbox's content as a struct
125  * sk_buff.
126  *
127  * If the struct can_rx_offload::skb_queue exceeds the maximal queue
128  * length (struct can_rx_offload::skb_queue_len_max) or no skb can be
129  * allocated, the mailbox contents is discarded by reading it into an
130  * overflow buffer. This way the mailbox is marked as free by the
131  * driver.
132  *
133  * Return: A pointer to skb containing the CAN frame on success.
134  *
135  *         NULL if the mailbox @n is empty.
136  *
137  *         ERR_PTR() in case of an error
138  */
139 static struct sk_buff *
140 can_rx_offload_offload_one(struct can_rx_offload *offload, unsigned int n)
141 {
142         struct sk_buff *skb = NULL, *skb_error = NULL;
143         struct can_rx_offload_cb *cb;
144         struct can_frame *cf;
145         int ret;
146
147         if (likely(skb_queue_len(&offload->skb_queue) <
148                    offload->skb_queue_len_max)) {
149                 skb = alloc_can_skb(offload->dev, &cf);
150                 if (unlikely(!skb))
151                         skb_error = ERR_PTR(-ENOMEM);   /* skb alloc failed */
152         } else {
153                 skb_error = ERR_PTR(-ENOBUFS);          /* skb_queue is full */
154         }
155
156         /* If queue is full or skb not available, drop by reading into
157          * overflow buffer.
158          */
159         if (unlikely(skb_error)) {
160                 struct can_frame cf_overflow;
161                 u32 timestamp;
162
163                 ret = offload->mailbox_read(offload, &cf_overflow,
164                                             &timestamp, n);
165
166                 /* Mailbox was empty. */
167                 if (unlikely(!ret))
168                         return NULL;
169
170                 /* Mailbox has been read and we're dropping it or
171                  * there was a problem reading the mailbox.
172                  *
173                  * Increment error counters in any case.
174                  */
175                 offload->dev->stats.rx_dropped++;
176                 offload->dev->stats.rx_fifo_errors++;
177
178                 /* There was a problem reading the mailbox, propagate
179                  * error value.
180                  */
181                 if (unlikely(ret < 0))
182                         return ERR_PTR(ret);
183
184                 return skb_error;
185         }
186
187         cb = can_rx_offload_get_cb(skb);
188         ret = offload->mailbox_read(offload, cf, &cb->timestamp, n);
189
190         /* Mailbox was empty. */
191         if (unlikely(!ret)) {
192                 kfree_skb(skb);
193                 return NULL;
194         }
195
196         /* There was a problem reading the mailbox, propagate error value. */
197         if (unlikely(ret < 0)) {
198                 kfree_skb(skb);
199
200                 offload->dev->stats.rx_dropped++;
201                 offload->dev->stats.rx_fifo_errors++;
202
203                 return ERR_PTR(ret);
204         }
205
206         /* Mailbox was read. */
207         return skb;
208 }
209
210 int can_rx_offload_irq_offload_timestamp(struct can_rx_offload *offload,
211                                          u64 pending)
212 {
213         struct sk_buff_head skb_queue;
214         unsigned int i;
215
216         __skb_queue_head_init(&skb_queue);
217
218         for (i = offload->mb_first;
219              can_rx_offload_le(offload, i, offload->mb_last);
220              can_rx_offload_inc(offload, &i)) {
221                 struct sk_buff *skb;
222
223                 if (!(pending & BIT_ULL(i)))
224                         continue;
225
226                 skb = can_rx_offload_offload_one(offload, i);
227                 if (IS_ERR_OR_NULL(skb))
228                         continue;
229
230                 __skb_queue_add_sort(&skb_queue, skb, can_rx_offload_compare);
231         }
232
233         if (!skb_queue_empty(&skb_queue)) {
234                 unsigned long flags;
235                 u32 queue_len;
236
237                 spin_lock_irqsave(&offload->skb_queue.lock, flags);
238                 skb_queue_splice_tail(&skb_queue, &offload->skb_queue);
239                 spin_unlock_irqrestore(&offload->skb_queue.lock, flags);
240
241                 if ((queue_len = skb_queue_len(&offload->skb_queue)) >
242                     (offload->skb_queue_len_max / 8))
243                         netdev_dbg(offload->dev, "%s: queue_len=%d\n",
244                                    __func__, queue_len);
245
246                 can_rx_offload_schedule(offload);
247         }
248
249         return skb_queue_len(&skb_queue);
250 }
251 EXPORT_SYMBOL_GPL(can_rx_offload_irq_offload_timestamp);
252
253 int can_rx_offload_irq_offload_fifo(struct can_rx_offload *offload)
254 {
255         struct sk_buff *skb;
256         int received = 0;
257
258         while (1) {
259                 skb = can_rx_offload_offload_one(offload, 0);
260                 if (IS_ERR(skb))
261                         continue;
262                 if (!skb)
263                         break;
264
265                 skb_queue_tail(&offload->skb_queue, skb);
266                 received++;
267         }
268
269         if (received)
270                 can_rx_offload_schedule(offload);
271
272         return received;
273 }
274 EXPORT_SYMBOL_GPL(can_rx_offload_irq_offload_fifo);
275
276 int can_rx_offload_queue_sorted(struct can_rx_offload *offload,
277                                 struct sk_buff *skb, u32 timestamp)
278 {
279         struct can_rx_offload_cb *cb;
280         unsigned long flags;
281
282         if (skb_queue_len(&offload->skb_queue) >
283             offload->skb_queue_len_max) {
284                 kfree_skb(skb);
285                 return -ENOBUFS;
286         }
287
288         cb = can_rx_offload_get_cb(skb);
289         cb->timestamp = timestamp;
290
291         spin_lock_irqsave(&offload->skb_queue.lock, flags);
292         __skb_queue_add_sort(&offload->skb_queue, skb, can_rx_offload_compare);
293         spin_unlock_irqrestore(&offload->skb_queue.lock, flags);
294
295         can_rx_offload_schedule(offload);
296
297         return 0;
298 }
299 EXPORT_SYMBOL_GPL(can_rx_offload_queue_sorted);
300
301 unsigned int can_rx_offload_get_echo_skb(struct can_rx_offload *offload,
302                                          unsigned int idx, u32 timestamp)
303 {
304         struct net_device *dev = offload->dev;
305         struct net_device_stats *stats = &dev->stats;
306         struct sk_buff *skb;
307         u8 len;
308         int err;
309
310         skb = __can_get_echo_skb(dev, idx, &len);
311         if (!skb)
312                 return 0;
313
314         err = can_rx_offload_queue_sorted(offload, skb, timestamp);
315         if (err) {
316                 stats->rx_errors++;
317                 stats->tx_fifo_errors++;
318         }
319
320         return len;
321 }
322 EXPORT_SYMBOL_GPL(can_rx_offload_get_echo_skb);
323
324 int can_rx_offload_queue_tail(struct can_rx_offload *offload,
325                               struct sk_buff *skb)
326 {
327         if (skb_queue_len(&offload->skb_queue) >
328             offload->skb_queue_len_max) {
329                 kfree_skb(skb);
330                 return -ENOBUFS;
331         }
332
333         skb_queue_tail(&offload->skb_queue, skb);
334         can_rx_offload_schedule(offload);
335
336         return 0;
337 }
338 EXPORT_SYMBOL_GPL(can_rx_offload_queue_tail);
339
340 static int can_rx_offload_init_queue(struct net_device *dev,
341                                      struct can_rx_offload *offload,
342                                      unsigned int weight)
343 {
344         offload->dev = dev;
345
346         /* Limit queue len to 4x the weight (rounted to next power of two) */
347         offload->skb_queue_len_max = 2 << fls(weight);
348         offload->skb_queue_len_max *= 4;
349         skb_queue_head_init(&offload->skb_queue);
350
351         can_rx_offload_reset(offload);
352         netif_napi_add(dev, &offload->napi, can_rx_offload_napi_poll, weight);
353
354         dev_dbg(dev->dev.parent, "%s: skb_queue_len_max=%d\n",
355                 __func__, offload->skb_queue_len_max);
356
357         return 0;
358 }
359
360 int can_rx_offload_add_timestamp(struct net_device *dev,
361                                  struct can_rx_offload *offload)
362 {
363         unsigned int weight;
364
365         if (offload->mb_first > BITS_PER_LONG_LONG ||
366             offload->mb_last > BITS_PER_LONG_LONG || !offload->mailbox_read)
367                 return -EINVAL;
368
369         if (offload->mb_first < offload->mb_last) {
370                 offload->inc = true;
371                 weight = offload->mb_last - offload->mb_first;
372         } else {
373                 offload->inc = false;
374                 weight = offload->mb_first - offload->mb_last;
375         }
376
377         return can_rx_offload_init_queue(dev, offload, weight);
378 }
379 EXPORT_SYMBOL_GPL(can_rx_offload_add_timestamp);
380
381 int can_rx_offload_add_fifo(struct net_device *dev,
382                             struct can_rx_offload *offload, unsigned int weight)
383 {
384         if (!offload->mailbox_read)
385                 return -EINVAL;
386
387         return can_rx_offload_init_queue(dev, offload, weight);
388 }
389 EXPORT_SYMBOL_GPL(can_rx_offload_add_fifo);
390
391 void can_rx_offload_enable(struct can_rx_offload *offload)
392 {
393         can_rx_offload_reset(offload);
394         napi_enable(&offload->napi);
395 }
396 EXPORT_SYMBOL_GPL(can_rx_offload_enable);
397
398 void can_rx_offload_del(struct can_rx_offload *offload)
399 {
400         netif_napi_del(&offload->napi);
401         skb_queue_purge(&offload->skb_queue);
402 }
403 EXPORT_SYMBOL_GPL(can_rx_offload_del);
404
405 void can_rx_offload_reset(struct can_rx_offload *offload)
406 {
407 }
408 EXPORT_SYMBOL_GPL(can_rx_offload_reset);