1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2014 David Jander, Protonic Holland
4 * Copyright (C) 2014-2017 Pengutronix, Marc Kleine-Budde <kernel@pengutronix.de>
7 #include <linux/can/dev.h>
8 #include <linux/can/rx-offload.h>
10 struct can_rx_offload_cb {
14 static inline struct can_rx_offload_cb *can_rx_offload_get_cb(struct sk_buff *skb)
16 BUILD_BUG_ON(sizeof(struct can_rx_offload_cb) > sizeof(skb->cb));
18 return (struct can_rx_offload_cb *)skb->cb;
21 static inline bool can_rx_offload_le(struct can_rx_offload *offload, unsigned int a, unsigned int b)
29 static inline unsigned int can_rx_offload_inc(struct can_rx_offload *offload, unsigned int *val)
37 static int can_rx_offload_napi_poll(struct napi_struct *napi, int quota)
39 struct can_rx_offload *offload = container_of(napi, struct can_rx_offload, napi);
40 struct net_device *dev = offload->dev;
41 struct net_device_stats *stats = &dev->stats;
45 while ((work_done < quota) &&
46 (skb = skb_dequeue(&offload->skb_queue))) {
47 struct can_frame *cf = (struct can_frame *)skb->data;
51 stats->rx_bytes += cf->can_dlc;
52 netif_receive_skb(skb);
55 if (work_done < quota) {
56 napi_complete_done(napi, work_done);
58 /* Check if there was another interrupt */
59 if (!skb_queue_empty(&offload->skb_queue))
60 napi_reschedule(&offload->napi);
63 can_led_event(offload->dev, CAN_LED_EVENT_RX);
68 static inline void __skb_queue_add_sort(struct sk_buff_head *head, struct sk_buff *new,
69 int (*compare)(struct sk_buff *a, struct sk_buff *b))
71 struct sk_buff *pos, *insert = NULL;
73 skb_queue_reverse_walk(head, pos) {
74 const struct can_rx_offload_cb *cb_pos, *cb_new;
76 cb_pos = can_rx_offload_get_cb(pos);
77 cb_new = can_rx_offload_get_cb(new);
80 "%s: pos=0x%08x, new=0x%08x, diff=%10d, queue_len=%d\n",
82 cb_pos->timestamp, cb_new->timestamp,
83 cb_new->timestamp - cb_pos->timestamp,
86 if (compare(pos, new) < 0)
92 __skb_queue_head(head, new);
94 __skb_queue_after(head, insert, new);
97 static int can_rx_offload_compare(struct sk_buff *a, struct sk_buff *b)
99 const struct can_rx_offload_cb *cb_a, *cb_b;
101 cb_a = can_rx_offload_get_cb(a);
102 cb_b = can_rx_offload_get_cb(b);
104 /* Substract two u32 and return result as int, to keep
105 * difference steady around the u32 overflow.
107 return cb_b->timestamp - cb_a->timestamp;
111 * can_rx_offload_offload_one() - Read one CAN frame from HW
112 * @offload: pointer to rx_offload context
113 * @n: number of mailbox to read
115 * The task of this function is to read a CAN frame from mailbox @n
116 * from the device and return the mailbox's content as a struct
119 * If the struct can_rx_offload::skb_queue exceeds the maximal queue
120 * length (struct can_rx_offload::skb_queue_len_max) or no skb can be
121 * allocated, the mailbox contents is discarded by reading it into an
122 * overflow buffer. This way the mailbox is marked as free by the
125 * Return: A pointer to skb containing the CAN frame on success.
127 * NULL if the mailbox @n is empty.
129 * ERR_PTR() in case of an error
131 static struct sk_buff *
132 can_rx_offload_offload_one(struct can_rx_offload *offload, unsigned int n)
134 struct sk_buff *skb = NULL, *skb_error = NULL;
135 struct can_rx_offload_cb *cb;
136 struct can_frame *cf;
139 if (likely(skb_queue_len(&offload->skb_queue) <
140 offload->skb_queue_len_max)) {
141 skb = alloc_can_skb(offload->dev, &cf);
143 skb_error = ERR_PTR(-ENOMEM); /* skb alloc failed */
145 skb_error = ERR_PTR(-ENOBUFS); /* skb_queue is full */
148 /* If queue is full or skb not available, drop by reading into
151 if (unlikely(skb_error)) {
152 struct can_frame cf_overflow;
155 ret = offload->mailbox_read(offload, &cf_overflow,
158 /* Mailbox was empty. */
162 /* Mailbox has been read and we're dropping it or
163 * there was a problem reading the mailbox.
165 * Increment error counters in any case.
167 offload->dev->stats.rx_dropped++;
168 offload->dev->stats.rx_fifo_errors++;
170 /* There was a problem reading the mailbox, propagate
173 if (unlikely(ret < 0))
179 cb = can_rx_offload_get_cb(skb);
180 ret = offload->mailbox_read(offload, cf, &cb->timestamp, n);
182 /* Mailbox was empty. */
183 if (unlikely(!ret)) {
188 /* There was a problem reading the mailbox, propagate error value. */
189 if (unlikely(ret < 0)) {
192 offload->dev->stats.rx_dropped++;
193 offload->dev->stats.rx_fifo_errors++;
198 /* Mailbox was read. */
202 int can_rx_offload_irq_offload_timestamp(struct can_rx_offload *offload, u64 pending)
204 struct sk_buff_head skb_queue;
207 __skb_queue_head_init(&skb_queue);
209 for (i = offload->mb_first;
210 can_rx_offload_le(offload, i, offload->mb_last);
211 can_rx_offload_inc(offload, &i)) {
214 if (!(pending & BIT_ULL(i)))
217 skb = can_rx_offload_offload_one(offload, i);
218 if (IS_ERR_OR_NULL(skb))
221 __skb_queue_add_sort(&skb_queue, skb, can_rx_offload_compare);
224 if (!skb_queue_empty(&skb_queue)) {
228 spin_lock_irqsave(&offload->skb_queue.lock, flags);
229 skb_queue_splice_tail(&skb_queue, &offload->skb_queue);
230 spin_unlock_irqrestore(&offload->skb_queue.lock, flags);
232 if ((queue_len = skb_queue_len(&offload->skb_queue)) >
233 (offload->skb_queue_len_max / 8))
234 netdev_dbg(offload->dev, "%s: queue_len=%d\n",
235 __func__, queue_len);
237 can_rx_offload_schedule(offload);
240 return skb_queue_len(&skb_queue);
242 EXPORT_SYMBOL_GPL(can_rx_offload_irq_offload_timestamp);
244 int can_rx_offload_irq_offload_fifo(struct can_rx_offload *offload)
250 skb = can_rx_offload_offload_one(offload, 0);
256 skb_queue_tail(&offload->skb_queue, skb);
261 can_rx_offload_schedule(offload);
265 EXPORT_SYMBOL_GPL(can_rx_offload_irq_offload_fifo);
267 int can_rx_offload_queue_sorted(struct can_rx_offload *offload,
268 struct sk_buff *skb, u32 timestamp)
270 struct can_rx_offload_cb *cb;
273 if (skb_queue_len(&offload->skb_queue) >
274 offload->skb_queue_len_max) {
279 cb = can_rx_offload_get_cb(skb);
280 cb->timestamp = timestamp;
282 spin_lock_irqsave(&offload->skb_queue.lock, flags);
283 __skb_queue_add_sort(&offload->skb_queue, skb, can_rx_offload_compare);
284 spin_unlock_irqrestore(&offload->skb_queue.lock, flags);
286 can_rx_offload_schedule(offload);
290 EXPORT_SYMBOL_GPL(can_rx_offload_queue_sorted);
292 unsigned int can_rx_offload_get_echo_skb(struct can_rx_offload *offload,
293 unsigned int idx, u32 timestamp)
295 struct net_device *dev = offload->dev;
296 struct net_device_stats *stats = &dev->stats;
301 skb = __can_get_echo_skb(dev, idx, &len);
305 err = can_rx_offload_queue_sorted(offload, skb, timestamp);
308 stats->tx_fifo_errors++;
313 EXPORT_SYMBOL_GPL(can_rx_offload_get_echo_skb);
315 int can_rx_offload_queue_tail(struct can_rx_offload *offload,
318 if (skb_queue_len(&offload->skb_queue) >
319 offload->skb_queue_len_max) {
324 skb_queue_tail(&offload->skb_queue, skb);
325 can_rx_offload_schedule(offload);
329 EXPORT_SYMBOL_GPL(can_rx_offload_queue_tail);
331 static int can_rx_offload_init_queue(struct net_device *dev, struct can_rx_offload *offload, unsigned int weight)
335 /* Limit queue len to 4x the weight (rounted to next power of two) */
336 offload->skb_queue_len_max = 2 << fls(weight);
337 offload->skb_queue_len_max *= 4;
338 skb_queue_head_init(&offload->skb_queue);
340 can_rx_offload_reset(offload);
341 netif_napi_add(dev, &offload->napi, can_rx_offload_napi_poll, weight);
343 dev_dbg(dev->dev.parent, "%s: skb_queue_len_max=%d\n",
344 __func__, offload->skb_queue_len_max);
349 int can_rx_offload_add_timestamp(struct net_device *dev, struct can_rx_offload *offload)
353 if (offload->mb_first > BITS_PER_LONG_LONG ||
354 offload->mb_last > BITS_PER_LONG_LONG || !offload->mailbox_read)
357 if (offload->mb_first < offload->mb_last) {
359 weight = offload->mb_last - offload->mb_first;
361 offload->inc = false;
362 weight = offload->mb_first - offload->mb_last;
365 return can_rx_offload_init_queue(dev, offload, weight);
367 EXPORT_SYMBOL_GPL(can_rx_offload_add_timestamp);
369 int can_rx_offload_add_fifo(struct net_device *dev, struct can_rx_offload *offload, unsigned int weight)
371 if (!offload->mailbox_read)
374 return can_rx_offload_init_queue(dev, offload, weight);
376 EXPORT_SYMBOL_GPL(can_rx_offload_add_fifo);
378 void can_rx_offload_enable(struct can_rx_offload *offload)
380 can_rx_offload_reset(offload);
381 napi_enable(&offload->napi);
383 EXPORT_SYMBOL_GPL(can_rx_offload_enable);
385 void can_rx_offload_del(struct can_rx_offload *offload)
387 netif_napi_del(&offload->napi);
388 skb_queue_purge(&offload->skb_queue);
390 EXPORT_SYMBOL_GPL(can_rx_offload_del);
392 void can_rx_offload_reset(struct can_rx_offload *offload)
395 EXPORT_SYMBOL_GPL(can_rx_offload_reset);