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
can_rx_offload_get_cb(struct sk_buff * skb)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
can_rx_offload_le(struct can_rx_offload * offload,unsigned int a,unsigned int b)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
can_rx_offload_inc(struct can_rx_offload * offload,unsigned int * val)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
can_rx_offload_napi_poll(struct napi_struct * napi,int quota)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
__skb_queue_add_sort(struct sk_buff_head * head,struct sk_buff * new,int (* compare)(struct sk_buff * a,struct sk_buff * b))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 = (struct sk_buff *)head;
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
103 __skb_queue_after(head, insert, new);
104 }
105
can_rx_offload_compare(struct sk_buff * a,struct sk_buff * b)106 static int can_rx_offload_compare(struct sk_buff *a, struct sk_buff *b)
107 {
108 const struct can_rx_offload_cb *cb_a, *cb_b;
109
110 cb_a = can_rx_offload_get_cb(a);
111 cb_b = can_rx_offload_get_cb(b);
112
113 /* Substract two u32 and return result as int, to keep
114 * difference steady around the u32 overflow.
115 */
116 return cb_b->timestamp - cb_a->timestamp;
117 }
118
119 /**
120 * can_rx_offload_offload_one() - Read one CAN frame from HW
121 * @offload: pointer to rx_offload context
122 * @n: number of mailbox to read
123 *
124 * The task of this function is to read a CAN frame from mailbox @n
125 * from the device and return the mailbox's content as a struct
126 * sk_buff.
127 *
128 * If the struct can_rx_offload::skb_queue exceeds the maximal queue
129 * length (struct can_rx_offload::skb_queue_len_max) or no skb can be
130 * allocated, the mailbox contents is discarded by reading it into an
131 * overflow buffer. This way the mailbox is marked as free by the
132 * driver.
133 *
134 * Return: A pointer to skb containing the CAN frame on success.
135 *
136 * NULL if the mailbox @n is empty.
137 *
138 * ERR_PTR() in case of an error
139 */
140 static struct sk_buff *
can_rx_offload_offload_one(struct can_rx_offload * offload,unsigned int n)141 can_rx_offload_offload_one(struct can_rx_offload *offload, unsigned int n)
142 {
143 struct sk_buff *skb = NULL, *skb_error = NULL;
144 struct can_rx_offload_cb *cb;
145 struct can_frame *cf;
146 int ret;
147
148 if (likely(skb_queue_len(&offload->skb_queue) <
149 offload->skb_queue_len_max)) {
150 skb = alloc_can_skb(offload->dev, &cf);
151 if (unlikely(!skb))
152 skb_error = ERR_PTR(-ENOMEM); /* skb alloc failed */
153 } else {
154 skb_error = ERR_PTR(-ENOBUFS); /* skb_queue is full */
155 }
156
157 /* If queue is full or skb not available, drop by reading into
158 * overflow buffer.
159 */
160 if (unlikely(skb_error)) {
161 struct can_frame cf_overflow;
162 u32 timestamp;
163
164 ret = offload->mailbox_read(offload, &cf_overflow,
165 ×tamp, n);
166
167 /* Mailbox was empty. */
168 if (unlikely(!ret))
169 return NULL;
170
171 /* Mailbox has been read and we're dropping it or
172 * there was a problem reading the mailbox.
173 *
174 * Increment error counters in any case.
175 */
176 offload->dev->stats.rx_dropped++;
177 offload->dev->stats.rx_fifo_errors++;
178
179 /* There was a problem reading the mailbox, propagate
180 * error value.
181 */
182 if (unlikely(ret < 0))
183 return ERR_PTR(ret);
184
185 return skb_error;
186 }
187
188 cb = can_rx_offload_get_cb(skb);
189 ret = offload->mailbox_read(offload, cf, &cb->timestamp, n);
190
191 /* Mailbox was empty. */
192 if (unlikely(!ret)) {
193 kfree_skb(skb);
194 return NULL;
195 }
196
197 /* There was a problem reading the mailbox, propagate error value. */
198 if (unlikely(ret < 0)) {
199 kfree_skb(skb);
200
201 offload->dev->stats.rx_dropped++;
202 offload->dev->stats.rx_fifo_errors++;
203
204 return ERR_PTR(ret);
205 }
206
207 /* Mailbox was read. */
208 return skb;
209 }
210
can_rx_offload_irq_offload_timestamp(struct can_rx_offload * offload,u64 pending)211 int can_rx_offload_irq_offload_timestamp(struct can_rx_offload *offload, 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
can_rx_offload_irq_offload_fifo(struct can_rx_offload * offload)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
can_rx_offload_queue_sorted(struct can_rx_offload * offload,struct sk_buff * skb,u32 timestamp)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
can_rx_offload_get_echo_skb(struct can_rx_offload * offload,unsigned int idx,u32 timestamp)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
can_rx_offload_queue_tail(struct can_rx_offload * offload,struct sk_buff * skb)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
can_rx_offload_init_queue(struct net_device * dev,struct can_rx_offload * offload,unsigned int weight)340 static int can_rx_offload_init_queue(struct net_device *dev, struct can_rx_offload *offload, unsigned int weight)
341 {
342 offload->dev = dev;
343
344 /* Limit queue len to 4x the weight (rounted to next power of two) */
345 offload->skb_queue_len_max = 2 << fls(weight);
346 offload->skb_queue_len_max *= 4;
347 skb_queue_head_init(&offload->skb_queue);
348
349 can_rx_offload_reset(offload);
350 netif_napi_add(dev, &offload->napi, can_rx_offload_napi_poll, weight);
351
352 dev_dbg(dev->dev.parent, "%s: skb_queue_len_max=%d\n",
353 __func__, offload->skb_queue_len_max);
354
355 return 0;
356 }
357
can_rx_offload_add_timestamp(struct net_device * dev,struct can_rx_offload * offload)358 int can_rx_offload_add_timestamp(struct net_device *dev, struct can_rx_offload *offload)
359 {
360 unsigned int weight;
361
362 if (offload->mb_first > BITS_PER_LONG_LONG ||
363 offload->mb_last > BITS_PER_LONG_LONG || !offload->mailbox_read)
364 return -EINVAL;
365
366 if (offload->mb_first < offload->mb_last) {
367 offload->inc = true;
368 weight = offload->mb_last - offload->mb_first;
369 } else {
370 offload->inc = false;
371 weight = offload->mb_first - offload->mb_last;
372 }
373
374 return can_rx_offload_init_queue(dev, offload, weight);
375 }
376 EXPORT_SYMBOL_GPL(can_rx_offload_add_timestamp);
377
can_rx_offload_add_fifo(struct net_device * dev,struct can_rx_offload * offload,unsigned int weight)378 int can_rx_offload_add_fifo(struct net_device *dev, struct can_rx_offload *offload, unsigned int weight)
379 {
380 if (!offload->mailbox_read)
381 return -EINVAL;
382
383 return can_rx_offload_init_queue(dev, offload, weight);
384 }
385 EXPORT_SYMBOL_GPL(can_rx_offload_add_fifo);
386
can_rx_offload_enable(struct can_rx_offload * offload)387 void can_rx_offload_enable(struct can_rx_offload *offload)
388 {
389 can_rx_offload_reset(offload);
390 napi_enable(&offload->napi);
391 }
392 EXPORT_SYMBOL_GPL(can_rx_offload_enable);
393
can_rx_offload_del(struct can_rx_offload * offload)394 void can_rx_offload_del(struct can_rx_offload *offload)
395 {
396 netif_napi_del(&offload->napi);
397 skb_queue_purge(&offload->skb_queue);
398 }
399 EXPORT_SYMBOL_GPL(can_rx_offload_del);
400
can_rx_offload_reset(struct can_rx_offload * offload)401 void can_rx_offload_reset(struct can_rx_offload *offload)
402 {
403 }
404 EXPORT_SYMBOL_GPL(can_rx_offload_reset);
405