2 * Copyright (c) 2014 David Jander, Protonic Holland
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
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.
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/>.
18 #include <linux/can/dev.h>
19 #include <linux/can/rx-offload.h>
21 struct can_rx_offload_cb {
25 static inline struct can_rx_offload_cb *can_rx_offload_get_cb(struct sk_buff *skb)
27 BUILD_BUG_ON(sizeof(struct can_rx_offload_cb) > sizeof(skb->cb));
29 return (struct can_rx_offload_cb *)skb->cb;
32 static inline bool can_rx_offload_le(struct can_rx_offload *offload, unsigned int a, unsigned int b)
40 static inline unsigned int can_rx_offload_inc(struct can_rx_offload *offload, unsigned int *val)
48 static int can_rx_offload_napi_poll(struct napi_struct *napi, int quota)
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;
56 while ((work_done < quota) &&
57 (skb = skb_dequeue(&offload->skb_queue))) {
58 struct can_frame *cf = (struct can_frame *)skb->data;
62 stats->rx_bytes += cf->can_dlc;
63 netif_receive_skb(skb);
66 if (work_done < quota) {
67 napi_complete_done(napi, work_done);
69 /* Check if there was another interrupt */
70 if (!skb_queue_empty(&offload->skb_queue))
71 napi_reschedule(&offload->napi);
74 can_led_event(offload->dev, CAN_LED_EVENT_RX);
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))
82 struct sk_buff *pos, *insert = NULL;
84 skb_queue_reverse_walk(head, pos) {
85 const struct can_rx_offload_cb *cb_pos, *cb_new;
87 cb_pos = can_rx_offload_get_cb(pos);
88 cb_new = can_rx_offload_get_cb(new);
91 "%s: pos=0x%08x, new=0x%08x, diff=%10d, queue_len=%d\n",
93 cb_pos->timestamp, cb_new->timestamp,
94 cb_new->timestamp - cb_pos->timestamp,
97 if (compare(pos, new) < 0)
103 __skb_queue_head(head, new);
105 __skb_queue_after(head, insert, new);
108 static int can_rx_offload_compare(struct sk_buff *a, struct sk_buff *b)
110 const struct can_rx_offload_cb *cb_a, *cb_b;
112 cb_a = can_rx_offload_get_cb(a);
113 cb_b = can_rx_offload_get_cb(b);
115 /* Substract two u32 and return result as int, to keep
116 * difference steady around the u32 overflow.
118 return cb_b->timestamp - cb_a->timestamp;
121 static struct sk_buff *can_rx_offload_offload_one(struct can_rx_offload *offload, unsigned int n)
123 struct sk_buff *skb = NULL;
124 struct can_rx_offload_cb *cb;
125 struct can_frame *cf;
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);
134 struct can_frame cf_overflow;
137 ret = offload->mailbox_read(offload, &cf_overflow,
140 offload->dev->stats.rx_dropped++;
145 cb = can_rx_offload_get_cb(skb);
146 ret = offload->mailbox_read(offload, cf, &cb->timestamp, n);
155 int can_rx_offload_irq_offload_timestamp(struct can_rx_offload *offload, u64 pending)
157 struct sk_buff_head skb_queue;
160 __skb_queue_head_init(&skb_queue);
162 for (i = offload->mb_first;
163 can_rx_offload_le(offload, i, offload->mb_last);
164 can_rx_offload_inc(offload, &i)) {
167 if (!(pending & BIT_ULL(i)))
170 skb = can_rx_offload_offload_one(offload, i);
174 __skb_queue_add_sort(&skb_queue, skb, can_rx_offload_compare);
177 if (!skb_queue_empty(&skb_queue)) {
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);
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);
190 can_rx_offload_schedule(offload);
193 return skb_queue_len(&skb_queue);
195 EXPORT_SYMBOL_GPL(can_rx_offload_irq_offload_timestamp);
197 int can_rx_offload_irq_offload_fifo(struct can_rx_offload *offload)
202 while ((skb = can_rx_offload_offload_one(offload, 0))) {
203 skb_queue_tail(&offload->skb_queue, skb);
208 can_rx_offload_schedule(offload);
212 EXPORT_SYMBOL_GPL(can_rx_offload_irq_offload_fifo);
214 int can_rx_offload_irq_queue_err_skb(struct can_rx_offload *offload, struct sk_buff *skb)
216 if (skb_queue_len(&offload->skb_queue) >
217 offload->skb_queue_len_max)
220 skb_queue_tail(&offload->skb_queue, skb);
221 can_rx_offload_schedule(offload);
225 EXPORT_SYMBOL_GPL(can_rx_offload_irq_queue_err_skb);
227 static int can_rx_offload_init_queue(struct net_device *dev, struct can_rx_offload *offload, unsigned int weight)
231 /* Limit queue len to 4x the weight (rounted to next power of two) */
232 offload->skb_queue_len_max = 2 << fls(weight);
233 offload->skb_queue_len_max *= 4;
234 skb_queue_head_init(&offload->skb_queue);
236 can_rx_offload_reset(offload);
237 netif_napi_add(dev, &offload->napi, can_rx_offload_napi_poll, weight);
239 dev_dbg(dev->dev.parent, "%s: skb_queue_len_max=%d\n",
240 __func__, offload->skb_queue_len_max);
245 int can_rx_offload_add_timestamp(struct net_device *dev, struct can_rx_offload *offload)
249 if (offload->mb_first > BITS_PER_LONG_LONG ||
250 offload->mb_last > BITS_PER_LONG_LONG || !offload->mailbox_read)
253 if (offload->mb_first < offload->mb_last) {
255 weight = offload->mb_last - offload->mb_first;
257 offload->inc = false;
258 weight = offload->mb_first - offload->mb_last;
261 return can_rx_offload_init_queue(dev, offload, weight);
263 EXPORT_SYMBOL_GPL(can_rx_offload_add_timestamp);
265 int can_rx_offload_add_fifo(struct net_device *dev, struct can_rx_offload *offload, unsigned int weight)
267 if (!offload->mailbox_read)
270 return can_rx_offload_init_queue(dev, offload, weight);
272 EXPORT_SYMBOL_GPL(can_rx_offload_add_fifo);
274 void can_rx_offload_enable(struct can_rx_offload *offload)
276 can_rx_offload_reset(offload);
277 napi_enable(&offload->napi);
279 EXPORT_SYMBOL_GPL(can_rx_offload_enable);
281 void can_rx_offload_del(struct can_rx_offload *offload)
283 netif_napi_del(&offload->napi);
284 skb_queue_purge(&offload->skb_queue);
286 EXPORT_SYMBOL_GPL(can_rx_offload_del);
288 void can_rx_offload_reset(struct can_rx_offload *offload)
291 EXPORT_SYMBOL_GPL(can_rx_offload_reset);