xref: /linux-6.15/include/linux/can/dev.h (revision 5a9d5ecd)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * linux/can/dev.h
4  *
5  * Definitions for the CAN network device driver interface
6  *
7  * Copyright (C) 2006 Andrey Volkov <[email protected]>
8  *               Varma Electronics Oy
9  *
10  * Copyright (C) 2008 Wolfgang Grandegger <[email protected]>
11  *
12  */
13 
14 #ifndef _CAN_DEV_H
15 #define _CAN_DEV_H
16 
17 #include <linux/can.h>
18 #include <linux/can/bittiming.h>
19 #include <linux/can/error.h>
20 #include <linux/can/led.h>
21 #include <linux/can/netlink.h>
22 #include <linux/can/skb.h>
23 #include <linux/netdevice.h>
24 
25 /*
26  * CAN mode
27  */
28 enum can_mode {
29 	CAN_MODE_STOP = 0,
30 	CAN_MODE_START,
31 	CAN_MODE_SLEEP
32 };
33 
34 /*
35  * CAN common private data
36  */
37 struct can_priv {
38 	struct net_device *dev;
39 	struct can_device_stats can_stats;
40 
41 	struct can_bittiming bittiming, data_bittiming;
42 	const struct can_bittiming_const *bittiming_const,
43 		*data_bittiming_const;
44 	const u16 *termination_const;
45 	unsigned int termination_const_cnt;
46 	u16 termination;
47 	const u32 *bitrate_const;
48 	unsigned int bitrate_const_cnt;
49 	const u32 *data_bitrate_const;
50 	unsigned int data_bitrate_const_cnt;
51 	u32 bitrate_max;
52 	struct can_clock clock;
53 
54 	enum can_state state;
55 
56 	/* CAN controller features - see include/uapi/linux/can/netlink.h */
57 	u32 ctrlmode;		/* current options setting */
58 	u32 ctrlmode_supported;	/* options that can be modified by netlink */
59 	u32 ctrlmode_static;	/* static enabled options for driver/hardware */
60 
61 	int restart_ms;
62 	struct delayed_work restart_work;
63 
64 	int (*do_set_bittiming)(struct net_device *dev);
65 	int (*do_set_data_bittiming)(struct net_device *dev);
66 	int (*do_set_mode)(struct net_device *dev, enum can_mode mode);
67 	int (*do_set_termination)(struct net_device *dev, u16 term);
68 	int (*do_get_state)(const struct net_device *dev,
69 			    enum can_state *state);
70 	int (*do_get_berr_counter)(const struct net_device *dev,
71 				   struct can_berr_counter *bec);
72 
73 	unsigned int echo_skb_max;
74 	struct sk_buff **echo_skb;
75 
76 #ifdef CONFIG_CAN_LEDS
77 	struct led_trigger *tx_led_trig;
78 	char tx_led_trig_name[CAN_LED_NAME_SZ];
79 	struct led_trigger *rx_led_trig;
80 	char rx_led_trig_name[CAN_LED_NAME_SZ];
81 	struct led_trigger *rxtx_led_trig;
82 	char rxtx_led_trig_name[CAN_LED_NAME_SZ];
83 #endif
84 };
85 
86 /*
87  * can_cc_dlc2len(value) - convert a given data length code (dlc) of a
88  * Classical CAN frame into a valid data length of max. 8 bytes.
89  *
90  * To be used in the CAN netdriver receive path to ensure conformance with
91  * ISO 11898-1 Chapter 8.4.2.3 (DLC field)
92  */
93 #define can_cc_dlc2len(dlc)	(min_t(u8, (dlc), CAN_MAX_DLEN))
94 
95 /* Check for outgoing skbs that have not been created by the CAN subsystem */
96 static inline bool can_skb_headroom_valid(struct net_device *dev,
97 					  struct sk_buff *skb)
98 {
99 	/* af_packet creates a headroom of HH_DATA_MOD bytes which is fine */
100 	if (WARN_ON_ONCE(skb_headroom(skb) < sizeof(struct can_skb_priv)))
101 		return false;
102 
103 	/* af_packet does not apply CAN skb specific settings */
104 	if (skb->ip_summed == CHECKSUM_NONE) {
105 		/* init headroom */
106 		can_skb_prv(skb)->ifindex = dev->ifindex;
107 		can_skb_prv(skb)->skbcnt = 0;
108 
109 		skb->ip_summed = CHECKSUM_UNNECESSARY;
110 
111 		/* perform proper loopback on capable devices */
112 		if (dev->flags & IFF_ECHO)
113 			skb->pkt_type = PACKET_LOOPBACK;
114 		else
115 			skb->pkt_type = PACKET_HOST;
116 
117 		skb_reset_mac_header(skb);
118 		skb_reset_network_header(skb);
119 		skb_reset_transport_header(skb);
120 	}
121 
122 	return true;
123 }
124 
125 /* Drop a given socketbuffer if it does not contain a valid CAN frame. */
126 static inline bool can_dropped_invalid_skb(struct net_device *dev,
127 					  struct sk_buff *skb)
128 {
129 	const struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
130 
131 	if (skb->protocol == htons(ETH_P_CAN)) {
132 		if (unlikely(skb->len != CAN_MTU ||
133 			     cfd->len > CAN_MAX_DLEN))
134 			goto inval_skb;
135 	} else if (skb->protocol == htons(ETH_P_CANFD)) {
136 		if (unlikely(skb->len != CANFD_MTU ||
137 			     cfd->len > CANFD_MAX_DLEN))
138 			goto inval_skb;
139 	} else
140 		goto inval_skb;
141 
142 	if (!can_skb_headroom_valid(dev, skb))
143 		goto inval_skb;
144 
145 	return false;
146 
147 inval_skb:
148 	kfree_skb(skb);
149 	dev->stats.tx_dropped++;
150 	return true;
151 }
152 
153 static inline bool can_is_canfd_skb(const struct sk_buff *skb)
154 {
155 	/* the CAN specific type of skb is identified by its data length */
156 	return skb->len == CANFD_MTU;
157 }
158 
159 /* helper to get the data length code (DLC) for Classical CAN raw DLC access */
160 static inline u8 can_get_cc_dlc(const struct can_frame *cf, const u32 ctrlmode)
161 {
162 	/* return len8_dlc as dlc value only if all conditions apply */
163 	if ((ctrlmode & CAN_CTRLMODE_CC_LEN8_DLC) &&
164 	    (cf->len == CAN_MAX_DLEN) &&
165 	    (cf->len8_dlc > CAN_MAX_DLEN && cf->len8_dlc <= CAN_MAX_RAW_DLC))
166 		return cf->len8_dlc;
167 
168 	/* return the payload length as dlc value */
169 	return cf->len;
170 }
171 
172 /* helper to set len and len8_dlc value for Classical CAN raw DLC access */
173 static inline void can_frame_set_cc_len(struct can_frame *cf, const u8 dlc,
174 					const u32 ctrlmode)
175 {
176 	/* the caller already ensured that dlc is a value from 0 .. 15 */
177 	if (ctrlmode & CAN_CTRLMODE_CC_LEN8_DLC && dlc > CAN_MAX_DLEN)
178 		cf->len8_dlc = dlc;
179 
180 	/* limit the payload length 'len' to CAN_MAX_DLEN */
181 	cf->len = can_cc_dlc2len(dlc);
182 }
183 
184 /* helper to define static CAN controller features at device creation time */
185 static inline void can_set_static_ctrlmode(struct net_device *dev,
186 					   u32 static_mode)
187 {
188 	struct can_priv *priv = netdev_priv(dev);
189 
190 	/* alloc_candev() succeeded => netdev_priv() is valid at this point */
191 	priv->ctrlmode = static_mode;
192 	priv->ctrlmode_static = static_mode;
193 
194 	/* override MTU which was set by default in can_setup()? */
195 	if (static_mode & CAN_CTRLMODE_FD)
196 		dev->mtu = CANFD_MTU;
197 }
198 
199 /* get data length from raw data length code (DLC) */
200 u8 can_fd_dlc2len(u8 dlc);
201 
202 /* map the sanitized data length to an appropriate data length code */
203 u8 can_fd_len2dlc(u8 len);
204 
205 struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max,
206 				    unsigned int txqs, unsigned int rxqs);
207 #define alloc_candev(sizeof_priv, echo_skb_max) \
208 	alloc_candev_mqs(sizeof_priv, echo_skb_max, 1, 1)
209 #define alloc_candev_mq(sizeof_priv, echo_skb_max, count) \
210 	alloc_candev_mqs(sizeof_priv, echo_skb_max, count, count)
211 void free_candev(struct net_device *dev);
212 
213 /* a candev safe wrapper around netdev_priv */
214 struct can_priv *safe_candev_priv(struct net_device *dev);
215 
216 int open_candev(struct net_device *dev);
217 void close_candev(struct net_device *dev);
218 int can_change_mtu(struct net_device *dev, int new_mtu);
219 
220 int register_candev(struct net_device *dev);
221 void unregister_candev(struct net_device *dev);
222 
223 int can_restart_now(struct net_device *dev);
224 void can_bus_off(struct net_device *dev);
225 
226 void can_change_state(struct net_device *dev, struct can_frame *cf,
227 		      enum can_state tx_state, enum can_state rx_state);
228 
229 int can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
230 		     unsigned int idx);
231 struct sk_buff *__can_get_echo_skb(struct net_device *dev, unsigned int idx,
232 				   u8 *len_ptr);
233 unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx);
234 void can_free_echo_skb(struct net_device *dev, unsigned int idx);
235 
236 #ifdef CONFIG_OF
237 void of_can_transceiver(struct net_device *dev);
238 #else
239 static inline void of_can_transceiver(struct net_device *dev) { }
240 #endif
241 
242 struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf);
243 struct sk_buff *alloc_canfd_skb(struct net_device *dev,
244 				struct canfd_frame **cfd);
245 struct sk_buff *alloc_can_err_skb(struct net_device *dev,
246 				  struct can_frame **cf);
247 
248 #endif /* !_CAN_DEV_H */
249