xref: /openbmc/linux/drivers/net/can/dev/dev.c (revision 26a9630c72ebac7c564db305a6aee54a8edde70e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2005 Marc Kleine-Budde, Pengutronix
3  * Copyright (C) 2006 Andrey Volkov, Varma Electronics
4  * Copyright (C) 2008-2009 Wolfgang Grandegger <wg@grandegger.com>
5  */
6 
7 #include <linux/module.h>
8 #include <linux/kernel.h>
9 #include <linux/slab.h>
10 #include <linux/netdevice.h>
11 #include <linux/if_arp.h>
12 #include <linux/workqueue.h>
13 #include <linux/can.h>
14 #include <linux/can/can-ml.h>
15 #include <linux/can/dev.h>
16 #include <linux/can/skb.h>
17 #include <linux/can/led.h>
18 #include <linux/of.h>
19 
20 #define MOD_DESC "CAN device driver interface"
21 
22 MODULE_DESCRIPTION(MOD_DESC);
23 MODULE_LICENSE("GPL v2");
24 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
25 
26 static void can_update_state_error_stats(struct net_device *dev,
27 					 enum can_state new_state)
28 {
29 	struct can_priv *priv = netdev_priv(dev);
30 
31 	if (new_state <= priv->state)
32 		return;
33 
34 	switch (new_state) {
35 	case CAN_STATE_ERROR_WARNING:
36 		priv->can_stats.error_warning++;
37 		break;
38 	case CAN_STATE_ERROR_PASSIVE:
39 		priv->can_stats.error_passive++;
40 		break;
41 	case CAN_STATE_BUS_OFF:
42 		priv->can_stats.bus_off++;
43 		break;
44 	default:
45 		break;
46 	}
47 }
48 
49 static int can_tx_state_to_frame(struct net_device *dev, enum can_state state)
50 {
51 	switch (state) {
52 	case CAN_STATE_ERROR_ACTIVE:
53 		return CAN_ERR_CRTL_ACTIVE;
54 	case CAN_STATE_ERROR_WARNING:
55 		return CAN_ERR_CRTL_TX_WARNING;
56 	case CAN_STATE_ERROR_PASSIVE:
57 		return CAN_ERR_CRTL_TX_PASSIVE;
58 	default:
59 		return 0;
60 	}
61 }
62 
63 static int can_rx_state_to_frame(struct net_device *dev, enum can_state state)
64 {
65 	switch (state) {
66 	case CAN_STATE_ERROR_ACTIVE:
67 		return CAN_ERR_CRTL_ACTIVE;
68 	case CAN_STATE_ERROR_WARNING:
69 		return CAN_ERR_CRTL_RX_WARNING;
70 	case CAN_STATE_ERROR_PASSIVE:
71 		return CAN_ERR_CRTL_RX_PASSIVE;
72 	default:
73 		return 0;
74 	}
75 }
76 
77 const char *can_get_state_str(const enum can_state state)
78 {
79 	switch (state) {
80 	case CAN_STATE_ERROR_ACTIVE:
81 		return "Error Active";
82 	case CAN_STATE_ERROR_WARNING:
83 		return "Error Warning";
84 	case CAN_STATE_ERROR_PASSIVE:
85 		return "Error Passive";
86 	case CAN_STATE_BUS_OFF:
87 		return "Bus Off";
88 	case CAN_STATE_STOPPED:
89 		return "Stopped";
90 	case CAN_STATE_SLEEPING:
91 		return "Sleeping";
92 	default:
93 		return "<unknown>";
94 	}
95 
96 	return "<unknown>";
97 }
98 EXPORT_SYMBOL_GPL(can_get_state_str);
99 
100 void can_change_state(struct net_device *dev, struct can_frame *cf,
101 		      enum can_state tx_state, enum can_state rx_state)
102 {
103 	struct can_priv *priv = netdev_priv(dev);
104 	enum can_state new_state = max(tx_state, rx_state);
105 
106 	if (unlikely(new_state == priv->state)) {
107 		netdev_warn(dev, "%s: oops, state did not change", __func__);
108 		return;
109 	}
110 
111 	netdev_dbg(dev, "Controller changed from %s State (%d) into %s State (%d).\n",
112 		   can_get_state_str(priv->state), priv->state,
113 		   can_get_state_str(new_state), new_state);
114 
115 	can_update_state_error_stats(dev, new_state);
116 	priv->state = new_state;
117 
118 	if (!cf)
119 		return;
120 
121 	if (unlikely(new_state == CAN_STATE_BUS_OFF)) {
122 		cf->can_id |= CAN_ERR_BUSOFF;
123 		return;
124 	}
125 
126 	cf->can_id |= CAN_ERR_CRTL;
127 	cf->data[1] |= tx_state >= rx_state ?
128 		       can_tx_state_to_frame(dev, tx_state) : 0;
129 	cf->data[1] |= tx_state <= rx_state ?
130 		       can_rx_state_to_frame(dev, rx_state) : 0;
131 }
132 EXPORT_SYMBOL_GPL(can_change_state);
133 
134 /* CAN device restart for bus-off recovery */
135 static void can_restart(struct net_device *dev)
136 {
137 	struct can_priv *priv = netdev_priv(dev);
138 	struct net_device_stats *stats = &dev->stats;
139 	struct sk_buff *skb;
140 	struct can_frame *cf;
141 	int err;
142 
143 	BUG_ON(netif_carrier_ok(dev));
144 
145 	/* No synchronization needed because the device is bus-off and
146 	 * no messages can come in or go out.
147 	 */
148 	can_flush_echo_skb(dev);
149 
150 	/* send restart message upstream */
151 	skb = alloc_can_err_skb(dev, &cf);
152 	if (!skb)
153 		goto restart;
154 
155 	cf->can_id |= CAN_ERR_RESTARTED;
156 
157 	stats->rx_packets++;
158 	stats->rx_bytes += cf->len;
159 
160 	netif_rx_ni(skb);
161 
162 restart:
163 	netdev_dbg(dev, "restarted\n");
164 	priv->can_stats.restarts++;
165 
166 	/* Now restart the device */
167 	err = priv->do_set_mode(dev, CAN_MODE_START);
168 
169 	netif_carrier_on(dev);
170 	if (err)
171 		netdev_err(dev, "Error %d during restart", err);
172 }
173 
174 static void can_restart_work(struct work_struct *work)
175 {
176 	struct delayed_work *dwork = to_delayed_work(work);
177 	struct can_priv *priv = container_of(dwork, struct can_priv,
178 					     restart_work);
179 
180 	can_restart(priv->dev);
181 }
182 
183 int can_restart_now(struct net_device *dev)
184 {
185 	struct can_priv *priv = netdev_priv(dev);
186 
187 	/* A manual restart is only permitted if automatic restart is
188 	 * disabled and the device is in the bus-off state
189 	 */
190 	if (priv->restart_ms)
191 		return -EINVAL;
192 	if (priv->state != CAN_STATE_BUS_OFF)
193 		return -EBUSY;
194 
195 	cancel_delayed_work_sync(&priv->restart_work);
196 	can_restart(dev);
197 
198 	return 0;
199 }
200 
201 /* CAN bus-off
202  *
203  * This functions should be called when the device goes bus-off to
204  * tell the netif layer that no more packets can be sent or received.
205  * If enabled, a timer is started to trigger bus-off recovery.
206  */
207 void can_bus_off(struct net_device *dev)
208 {
209 	struct can_priv *priv = netdev_priv(dev);
210 
211 	if (priv->restart_ms)
212 		netdev_info(dev, "bus-off, scheduling restart in %d ms\n",
213 			    priv->restart_ms);
214 	else
215 		netdev_info(dev, "bus-off\n");
216 
217 	netif_carrier_off(dev);
218 
219 	if (priv->restart_ms)
220 		schedule_delayed_work(&priv->restart_work,
221 				      msecs_to_jiffies(priv->restart_ms));
222 }
223 EXPORT_SYMBOL_GPL(can_bus_off);
224 
225 void can_setup(struct net_device *dev)
226 {
227 	dev->type = ARPHRD_CAN;
228 	dev->mtu = CAN_MTU;
229 	dev->hard_header_len = 0;
230 	dev->addr_len = 0;
231 	dev->tx_queue_len = 10;
232 
233 	/* New-style flags. */
234 	dev->flags = IFF_NOARP;
235 	dev->features = NETIF_F_HW_CSUM;
236 }
237 
238 /* Allocate and setup space for the CAN network device */
239 struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max,
240 				    unsigned int txqs, unsigned int rxqs)
241 {
242 	struct net_device *dev;
243 	struct can_priv *priv;
244 	int size;
245 
246 	/* We put the driver's priv, the CAN mid layer priv and the
247 	 * echo skb into the netdevice's priv. The memory layout for
248 	 * the netdev_priv is like this:
249 	 *
250 	 * +-------------------------+
251 	 * | driver's priv           |
252 	 * +-------------------------+
253 	 * | struct can_ml_priv      |
254 	 * +-------------------------+
255 	 * | array of struct sk_buff |
256 	 * +-------------------------+
257 	 */
258 
259 	size = ALIGN(sizeof_priv, NETDEV_ALIGN) + sizeof(struct can_ml_priv);
260 
261 	if (echo_skb_max)
262 		size = ALIGN(size, sizeof(struct sk_buff *)) +
263 			echo_skb_max * sizeof(struct sk_buff *);
264 
265 	dev = alloc_netdev_mqs(size, "can%d", NET_NAME_UNKNOWN, can_setup,
266 			       txqs, rxqs);
267 	if (!dev)
268 		return NULL;
269 
270 	priv = netdev_priv(dev);
271 	priv->dev = dev;
272 
273 	dev->ml_priv = (void *)priv + ALIGN(sizeof_priv, NETDEV_ALIGN);
274 
275 	if (echo_skb_max) {
276 		priv->echo_skb_max = echo_skb_max;
277 		priv->echo_skb = (void *)priv +
278 			(size - echo_skb_max * sizeof(struct sk_buff *));
279 	}
280 
281 	priv->state = CAN_STATE_STOPPED;
282 
283 	INIT_DELAYED_WORK(&priv->restart_work, can_restart_work);
284 
285 	return dev;
286 }
287 EXPORT_SYMBOL_GPL(alloc_candev_mqs);
288 
289 /* Free space of the CAN network device */
290 void free_candev(struct net_device *dev)
291 {
292 	free_netdev(dev);
293 }
294 EXPORT_SYMBOL_GPL(free_candev);
295 
296 /* changing MTU and control mode for CAN/CANFD devices */
297 int can_change_mtu(struct net_device *dev, int new_mtu)
298 {
299 	struct can_priv *priv = netdev_priv(dev);
300 
301 	/* Do not allow changing the MTU while running */
302 	if (dev->flags & IFF_UP)
303 		return -EBUSY;
304 
305 	/* allow change of MTU according to the CANFD ability of the device */
306 	switch (new_mtu) {
307 	case CAN_MTU:
308 		/* 'CANFD-only' controllers can not switch to CAN_MTU */
309 		if (priv->ctrlmode_static & CAN_CTRLMODE_FD)
310 			return -EINVAL;
311 
312 		priv->ctrlmode &= ~CAN_CTRLMODE_FD;
313 		break;
314 
315 	case CANFD_MTU:
316 		/* check for potential CANFD ability */
317 		if (!(priv->ctrlmode_supported & CAN_CTRLMODE_FD) &&
318 		    !(priv->ctrlmode_static & CAN_CTRLMODE_FD))
319 			return -EINVAL;
320 
321 		priv->ctrlmode |= CAN_CTRLMODE_FD;
322 		break;
323 
324 	default:
325 		return -EINVAL;
326 	}
327 
328 	dev->mtu = new_mtu;
329 	return 0;
330 }
331 EXPORT_SYMBOL_GPL(can_change_mtu);
332 
333 /* Common open function when the device gets opened.
334  *
335  * This function should be called in the open function of the device
336  * driver.
337  */
338 int open_candev(struct net_device *dev)
339 {
340 	struct can_priv *priv = netdev_priv(dev);
341 
342 	if (!priv->bittiming.bitrate) {
343 		netdev_err(dev, "bit-timing not yet defined\n");
344 		return -EINVAL;
345 	}
346 
347 	/* For CAN FD the data bitrate has to be >= the arbitration bitrate */
348 	if ((priv->ctrlmode & CAN_CTRLMODE_FD) &&
349 	    (!priv->data_bittiming.bitrate ||
350 	     priv->data_bittiming.bitrate < priv->bittiming.bitrate)) {
351 		netdev_err(dev, "incorrect/missing data bit-timing\n");
352 		return -EINVAL;
353 	}
354 
355 	/* Switch carrier on if device was stopped while in bus-off state */
356 	if (!netif_carrier_ok(dev))
357 		netif_carrier_on(dev);
358 
359 	return 0;
360 }
361 EXPORT_SYMBOL_GPL(open_candev);
362 
363 #ifdef CONFIG_OF
364 /* Common function that can be used to understand the limitation of
365  * a transceiver when it provides no means to determine these limitations
366  * at runtime.
367  */
368 void of_can_transceiver(struct net_device *dev)
369 {
370 	struct device_node *dn;
371 	struct can_priv *priv = netdev_priv(dev);
372 	struct device_node *np = dev->dev.parent->of_node;
373 	int ret;
374 
375 	dn = of_get_child_by_name(np, "can-transceiver");
376 	if (!dn)
377 		return;
378 
379 	ret = of_property_read_u32(dn, "max-bitrate", &priv->bitrate_max);
380 	of_node_put(dn);
381 	if ((ret && ret != -EINVAL) || (!ret && !priv->bitrate_max))
382 		netdev_warn(dev, "Invalid value for transceiver max bitrate. Ignoring bitrate limit.\n");
383 }
384 EXPORT_SYMBOL_GPL(of_can_transceiver);
385 #endif
386 
387 /* Common close function for cleanup before the device gets closed.
388  *
389  * This function should be called in the close function of the device
390  * driver.
391  */
392 void close_candev(struct net_device *dev)
393 {
394 	struct can_priv *priv = netdev_priv(dev);
395 
396 	cancel_delayed_work_sync(&priv->restart_work);
397 	can_flush_echo_skb(dev);
398 }
399 EXPORT_SYMBOL_GPL(close_candev);
400 
401 /* Register the CAN network device */
402 int register_candev(struct net_device *dev)
403 {
404 	struct can_priv *priv = netdev_priv(dev);
405 
406 	/* Ensure termination_const, termination_const_cnt and
407 	 * do_set_termination consistency. All must be either set or
408 	 * unset.
409 	 */
410 	if ((!priv->termination_const != !priv->termination_const_cnt) ||
411 	    (!priv->termination_const != !priv->do_set_termination))
412 		return -EINVAL;
413 
414 	if (!priv->bitrate_const != !priv->bitrate_const_cnt)
415 		return -EINVAL;
416 
417 	if (!priv->data_bitrate_const != !priv->data_bitrate_const_cnt)
418 		return -EINVAL;
419 
420 	dev->rtnl_link_ops = &can_link_ops;
421 	netif_carrier_off(dev);
422 
423 	return register_netdev(dev);
424 }
425 EXPORT_SYMBOL_GPL(register_candev);
426 
427 /* Unregister the CAN network device */
428 void unregister_candev(struct net_device *dev)
429 {
430 	unregister_netdev(dev);
431 }
432 EXPORT_SYMBOL_GPL(unregister_candev);
433 
434 /* Test if a network device is a candev based device
435  * and return the can_priv* if so.
436  */
437 struct can_priv *safe_candev_priv(struct net_device *dev)
438 {
439 	if (dev->type != ARPHRD_CAN || dev->rtnl_link_ops != &can_link_ops)
440 		return NULL;
441 
442 	return netdev_priv(dev);
443 }
444 EXPORT_SYMBOL_GPL(safe_candev_priv);
445 
446 static __init int can_dev_init(void)
447 {
448 	int err;
449 
450 	can_led_notifier_init();
451 
452 	err = can_netlink_register();
453 	if (!err)
454 		pr_info(MOD_DESC "\n");
455 
456 	return err;
457 }
458 module_init(can_dev_init);
459 
460 static __exit void can_dev_exit(void)
461 {
462 	can_netlink_unregister();
463 
464 	can_led_notifier_exit();
465 }
466 module_exit(can_dev_exit);
467 
468 MODULE_ALIAS_RTNL_LINK("can");
469