rx-offload.c 10 KB

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