rx-offload.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2014 Protonic Holland,
  3. * David Jander
  4. * Copyright (C) 2014-2021 Pengutronix,
  5. * Marc Kleine-Budde <[email protected]>
  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_reschedule(&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 can_rx_offload_get_echo_skb(struct can_rx_offload *offload,
  201. unsigned int idx, u32 timestamp,
  202. unsigned int *frame_len_ptr)
  203. {
  204. struct net_device *dev = offload->dev;
  205. struct net_device_stats *stats = &dev->stats;
  206. struct sk_buff *skb;
  207. unsigned int len;
  208. int err;
  209. skb = __can_get_echo_skb(dev, idx, &len, frame_len_ptr);
  210. if (!skb)
  211. return 0;
  212. err = can_rx_offload_queue_timestamp(offload, skb, timestamp);
  213. if (err) {
  214. stats->rx_errors++;
  215. stats->tx_fifo_errors++;
  216. }
  217. return len;
  218. }
  219. EXPORT_SYMBOL_GPL(can_rx_offload_get_echo_skb);
  220. int can_rx_offload_queue_tail(struct can_rx_offload *offload,
  221. struct sk_buff *skb)
  222. {
  223. if (skb_queue_len(&offload->skb_queue) >
  224. offload->skb_queue_len_max) {
  225. dev_kfree_skb_any(skb);
  226. return -ENOBUFS;
  227. }
  228. __skb_queue_tail(&offload->skb_irq_queue, skb);
  229. return 0;
  230. }
  231. EXPORT_SYMBOL_GPL(can_rx_offload_queue_tail);
  232. void can_rx_offload_irq_finish(struct can_rx_offload *offload)
  233. {
  234. unsigned long flags;
  235. int queue_len;
  236. if (skb_queue_empty_lockless(&offload->skb_irq_queue))
  237. return;
  238. spin_lock_irqsave(&offload->skb_queue.lock, flags);
  239. skb_queue_splice_tail_init(&offload->skb_irq_queue, &offload->skb_queue);
  240. spin_unlock_irqrestore(&offload->skb_queue.lock, flags);
  241. queue_len = skb_queue_len(&offload->skb_queue);
  242. if (queue_len > offload->skb_queue_len_max / 8)
  243. netdev_dbg(offload->dev, "%s: queue_len=%d\n",
  244. __func__, queue_len);
  245. napi_schedule(&offload->napi);
  246. }
  247. EXPORT_SYMBOL_GPL(can_rx_offload_irq_finish);
  248. void can_rx_offload_threaded_irq_finish(struct can_rx_offload *offload)
  249. {
  250. unsigned long flags;
  251. int queue_len;
  252. if (skb_queue_empty_lockless(&offload->skb_irq_queue))
  253. return;
  254. spin_lock_irqsave(&offload->skb_queue.lock, flags);
  255. skb_queue_splice_tail_init(&offload->skb_irq_queue, &offload->skb_queue);
  256. spin_unlock_irqrestore(&offload->skb_queue.lock, flags);
  257. queue_len = skb_queue_len(&offload->skb_queue);
  258. if (queue_len > offload->skb_queue_len_max / 8)
  259. netdev_dbg(offload->dev, "%s: queue_len=%d\n",
  260. __func__, queue_len);
  261. local_bh_disable();
  262. napi_schedule(&offload->napi);
  263. local_bh_enable();
  264. }
  265. EXPORT_SYMBOL_GPL(can_rx_offload_threaded_irq_finish);
  266. static int can_rx_offload_init_queue(struct net_device *dev,
  267. struct can_rx_offload *offload,
  268. unsigned int weight)
  269. {
  270. offload->dev = dev;
  271. /* Limit queue len to 4x the weight (rounded to next power of two) */
  272. offload->skb_queue_len_max = 2 << fls(weight);
  273. offload->skb_queue_len_max *= 4;
  274. skb_queue_head_init(&offload->skb_queue);
  275. __skb_queue_head_init(&offload->skb_irq_queue);
  276. netif_napi_add_weight(dev, &offload->napi, can_rx_offload_napi_poll,
  277. weight);
  278. dev_dbg(dev->dev.parent, "%s: skb_queue_len_max=%d\n",
  279. __func__, offload->skb_queue_len_max);
  280. return 0;
  281. }
  282. int can_rx_offload_add_timestamp(struct net_device *dev,
  283. struct can_rx_offload *offload)
  284. {
  285. unsigned int weight;
  286. if (offload->mb_first > BITS_PER_LONG_LONG ||
  287. offload->mb_last > BITS_PER_LONG_LONG || !offload->mailbox_read)
  288. return -EINVAL;
  289. if (offload->mb_first < offload->mb_last) {
  290. offload->inc = true;
  291. weight = offload->mb_last - offload->mb_first;
  292. } else {
  293. offload->inc = false;
  294. weight = offload->mb_first - offload->mb_last;
  295. }
  296. return can_rx_offload_init_queue(dev, offload, weight);
  297. }
  298. EXPORT_SYMBOL_GPL(can_rx_offload_add_timestamp);
  299. int can_rx_offload_add_fifo(struct net_device *dev,
  300. struct can_rx_offload *offload, unsigned int weight)
  301. {
  302. if (!offload->mailbox_read)
  303. return -EINVAL;
  304. return can_rx_offload_init_queue(dev, offload, weight);
  305. }
  306. EXPORT_SYMBOL_GPL(can_rx_offload_add_fifo);
  307. int can_rx_offload_add_manual(struct net_device *dev,
  308. struct can_rx_offload *offload,
  309. unsigned int weight)
  310. {
  311. if (offload->mailbox_read)
  312. return -EINVAL;
  313. return can_rx_offload_init_queue(dev, offload, weight);
  314. }
  315. EXPORT_SYMBOL_GPL(can_rx_offload_add_manual);
  316. void can_rx_offload_enable(struct can_rx_offload *offload)
  317. {
  318. napi_enable(&offload->napi);
  319. }
  320. EXPORT_SYMBOL_GPL(can_rx_offload_enable);
  321. void can_rx_offload_del(struct can_rx_offload *offload)
  322. {
  323. netif_napi_del(&offload->napi);
  324. skb_queue_purge(&offload->skb_queue);
  325. __skb_queue_purge(&offload->skb_irq_queue);
  326. }
  327. EXPORT_SYMBOL_GPL(can_rx_offload_del);