1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2005 Marc Kleine-Budde, Pengutronix
3 * Copyright (C) 2006 Andrey Volkov, Varma Electronics
7 #include <linux/can/dev.h>
9 /* Local echo of CAN messages
11 * CAN network devices *should* support a local echo functionality
12 * (see Documentation/networking/can.rst). To test the handling of CAN
13 * interfaces that do not support the local echo both driver types are
14 * implemented. In the case that the driver does not support the echo
15 * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core
16 * to perform the echo as a fallback solution.
18 void can_flush_echo_skb(struct net_device *dev)
20 struct can_priv *priv = netdev_priv(dev);
21 struct net_device_stats *stats = &dev->stats;
24 for (i = 0; i < priv->echo_skb_max; i++) {
25 if (priv->echo_skb[i]) {
26 kfree_skb(priv->echo_skb[i]);
27 priv->echo_skb[i] = NULL;
29 stats->tx_aborted_errors++;
34 /* Put the skb on the stack to be looped backed locally lateron
36 * The function is typically called in the start_xmit function
37 * of the device driver. The driver must protect access to
38 * priv->echo_skb, if necessary.
40 int can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
41 unsigned int idx, unsigned int frame_len)
43 struct can_priv *priv = netdev_priv(dev);
45 BUG_ON(idx >= priv->echo_skb_max);
47 /* check flag whether this packet has to be looped back */
48 if (!(dev->flags & IFF_ECHO) ||
49 (skb->protocol != htons(ETH_P_CAN) &&
50 skb->protocol != htons(ETH_P_CANFD))) {
55 if (!priv->echo_skb[idx]) {
56 skb = can_create_echo_skb(skb);
60 /* make settings for echo to reduce code in irq context */
61 skb->ip_summed = CHECKSUM_UNNECESSARY;
64 /* save frame_len to reuse it when transmission is completed */
65 can_skb_prv(skb)->frame_len = frame_len;
67 skb_tx_timestamp(skb);
69 /* save this skb for tx interrupt echo handling */
70 priv->echo_skb[idx] = skb;
72 /* locking problem with netif_stop_queue() ?? */
73 netdev_err(dev, "%s: BUG! echo_skb %d is occupied!\n", __func__, idx);
80 EXPORT_SYMBOL_GPL(can_put_echo_skb);
83 __can_get_echo_skb(struct net_device *dev, unsigned int idx, u8 *len_ptr,
84 unsigned int *frame_len_ptr)
86 struct can_priv *priv = netdev_priv(dev);
88 if (idx >= priv->echo_skb_max) {
89 netdev_err(dev, "%s: BUG! Trying to access can_priv::echo_skb out of bounds (%u/max %u)\n",
90 __func__, idx, priv->echo_skb_max);
94 if (priv->echo_skb[idx]) {
95 /* Using "struct canfd_frame::len" for the frame
96 * length is supported on both CAN and CANFD frames.
98 struct sk_buff *skb = priv->echo_skb[idx];
99 struct can_skb_priv *can_skb_priv = can_skb_prv(skb);
100 struct canfd_frame *cf = (struct canfd_frame *)skb->data;
102 /* get the real payload length for netdev statistics */
103 if (cf->can_id & CAN_RTR_FLAG)
109 *frame_len_ptr = can_skb_priv->frame_len;
111 priv->echo_skb[idx] = NULL;
113 if (skb->pkt_type == PACKET_LOOPBACK) {
114 skb->pkt_type = PACKET_BROADCAST;
116 dev_consume_skb_any(skb);
126 /* Get the skb from the stack and loop it back locally
128 * The function is typically called when the TX done interrupt
129 * is handled in the device driver. The driver must protect
130 * access to priv->echo_skb, if necessary.
132 unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx,
133 unsigned int *frame_len_ptr)
138 skb = __can_get_echo_skb(dev, idx, &len, frame_len_ptr);
143 if (netif_rx(skb) == NET_RX_SUCCESS)
144 dev_consume_skb_any(skb);
146 dev_kfree_skb_any(skb);
150 EXPORT_SYMBOL_GPL(can_get_echo_skb);
152 /* Remove the skb from the stack and free it.
154 * The function is typically called when TX failed.
156 void can_free_echo_skb(struct net_device *dev, unsigned int idx,
157 unsigned int *frame_len_ptr)
159 struct can_priv *priv = netdev_priv(dev);
161 if (idx >= priv->echo_skb_max) {
162 netdev_err(dev, "%s: BUG! Trying to access can_priv::echo_skb out of bounds (%u/max %u)\n",
163 __func__, idx, priv->echo_skb_max);
167 if (priv->echo_skb[idx]) {
168 struct sk_buff *skb = priv->echo_skb[idx];
169 struct can_skb_priv *can_skb_priv = can_skb_prv(skb);
172 *frame_len_ptr = can_skb_priv->frame_len;
174 dev_kfree_skb_any(skb);
175 priv->echo_skb[idx] = NULL;
178 EXPORT_SYMBOL_GPL(can_free_echo_skb);
180 struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf)
184 skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
185 sizeof(struct can_frame));
186 if (unlikely(!skb)) {
192 skb->protocol = htons(ETH_P_CAN);
193 skb->pkt_type = PACKET_BROADCAST;
194 skb->ip_summed = CHECKSUM_UNNECESSARY;
196 skb_reset_mac_header(skb);
197 skb_reset_network_header(skb);
198 skb_reset_transport_header(skb);
200 can_skb_reserve(skb);
201 can_skb_prv(skb)->ifindex = dev->ifindex;
202 can_skb_prv(skb)->skbcnt = 0;
204 *cf = skb_put_zero(skb, sizeof(struct can_frame));
208 EXPORT_SYMBOL_GPL(alloc_can_skb);
210 struct sk_buff *alloc_canfd_skb(struct net_device *dev,
211 struct canfd_frame **cfd)
215 skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
216 sizeof(struct canfd_frame));
217 if (unlikely(!skb)) {
223 skb->protocol = htons(ETH_P_CANFD);
224 skb->pkt_type = PACKET_BROADCAST;
225 skb->ip_summed = CHECKSUM_UNNECESSARY;
227 skb_reset_mac_header(skb);
228 skb_reset_network_header(skb);
229 skb_reset_transport_header(skb);
231 can_skb_reserve(skb);
232 can_skb_prv(skb)->ifindex = dev->ifindex;
233 can_skb_prv(skb)->skbcnt = 0;
235 *cfd = skb_put_zero(skb, sizeof(struct canfd_frame));
239 EXPORT_SYMBOL_GPL(alloc_canfd_skb);
241 struct sk_buff *alloc_can_err_skb(struct net_device *dev, struct can_frame **cf)
245 skb = alloc_can_skb(dev, cf);
249 (*cf)->can_id = CAN_ERR_FLAG;
250 (*cf)->len = CAN_ERR_DLC;
254 EXPORT_SYMBOL_GPL(alloc_can_err_skb);