xref: /linux/drivers/net/can/dev/dev.c (revision 3f0a50f345f78183f6e9b39c2f45ca5dcaa511ca)
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/gpio/consumer.h>
19 #include <linux/of.h>
20 
21 #define MOD_DESC "CAN device driver interface"
22 
23 MODULE_DESCRIPTION(MOD_DESC);
24 MODULE_LICENSE("GPL v2");
25 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
26 
27 static void can_update_state_error_stats(struct net_device *dev,
28 					 enum can_state new_state)
29 {
30 	struct can_priv *priv = netdev_priv(dev);
31 
32 	if (new_state <= priv->state)
33 		return;
34 
35 	switch (new_state) {
36 	case CAN_STATE_ERROR_WARNING:
37 		priv->can_stats.error_warning++;
38 		break;
39 	case CAN_STATE_ERROR_PASSIVE:
40 		priv->can_stats.error_passive++;
41 		break;
42 	case CAN_STATE_BUS_OFF:
43 		priv->can_stats.bus_off++;
44 		break;
45 	default:
46 		break;
47 	}
48 }
49 
50 static int can_tx_state_to_frame(struct net_device *dev, enum can_state state)
51 {
52 	switch (state) {
53 	case CAN_STATE_ERROR_ACTIVE:
54 		return CAN_ERR_CRTL_ACTIVE;
55 	case CAN_STATE_ERROR_WARNING:
56 		return CAN_ERR_CRTL_TX_WARNING;
57 	case CAN_STATE_ERROR_PASSIVE:
58 		return CAN_ERR_CRTL_TX_PASSIVE;
59 	default:
60 		return 0;
61 	}
62 }
63 
64 static int can_rx_state_to_frame(struct net_device *dev, enum can_state state)
65 {
66 	switch (state) {
67 	case CAN_STATE_ERROR_ACTIVE:
68 		return CAN_ERR_CRTL_ACTIVE;
69 	case CAN_STATE_ERROR_WARNING:
70 		return CAN_ERR_CRTL_RX_WARNING;
71 	case CAN_STATE_ERROR_PASSIVE:
72 		return CAN_ERR_CRTL_RX_PASSIVE;
73 	default:
74 		return 0;
75 	}
76 }
77 
78 const char *can_get_state_str(const enum can_state state)
79 {
80 	switch (state) {
81 	case CAN_STATE_ERROR_ACTIVE:
82 		return "Error Active";
83 	case CAN_STATE_ERROR_WARNING:
84 		return "Error Warning";
85 	case CAN_STATE_ERROR_PASSIVE:
86 		return "Error Passive";
87 	case CAN_STATE_BUS_OFF:
88 		return "Bus Off";
89 	case CAN_STATE_STOPPED:
90 		return "Stopped";
91 	case CAN_STATE_SLEEPING:
92 		return "Sleeping";
93 	default:
94 		return "<unknown>";
95 	}
96 
97 	return "<unknown>";
98 }
99 EXPORT_SYMBOL_GPL(can_get_state_str);
100 
101 void can_change_state(struct net_device *dev, struct can_frame *cf,
102 		      enum can_state tx_state, enum can_state rx_state)
103 {
104 	struct can_priv *priv = netdev_priv(dev);
105 	enum can_state new_state = max(tx_state, rx_state);
106 
107 	if (unlikely(new_state == priv->state)) {
108 		netdev_warn(dev, "%s: oops, state did not change", __func__);
109 		return;
110 	}
111 
112 	netdev_dbg(dev, "Controller changed from %s State (%d) into %s State (%d).\n",
113 		   can_get_state_str(priv->state), priv->state,
114 		   can_get_state_str(new_state), new_state);
115 
116 	can_update_state_error_stats(dev, new_state);
117 	priv->state = new_state;
118 
119 	if (!cf)
120 		return;
121 
122 	if (unlikely(new_state == CAN_STATE_BUS_OFF)) {
123 		cf->can_id |= CAN_ERR_BUSOFF;
124 		return;
125 	}
126 
127 	cf->can_id |= CAN_ERR_CRTL;
128 	cf->data[1] |= tx_state >= rx_state ?
129 		       can_tx_state_to_frame(dev, tx_state) : 0;
130 	cf->data[1] |= tx_state <= rx_state ?
131 		       can_rx_state_to_frame(dev, rx_state) : 0;
132 }
133 EXPORT_SYMBOL_GPL(can_change_state);
134 
135 /* CAN device restart for bus-off recovery */
136 static void can_restart(struct net_device *dev)
137 {
138 	struct can_priv *priv = netdev_priv(dev);
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 	netif_rx(skb);
158 
159 restart:
160 	netdev_dbg(dev, "restarted\n");
161 	priv->can_stats.restarts++;
162 
163 	/* Now restart the device */
164 	err = priv->do_set_mode(dev, CAN_MODE_START);
165 
166 	netif_carrier_on(dev);
167 	if (err)
168 		netdev_err(dev, "Error %d during restart", err);
169 }
170 
171 static void can_restart_work(struct work_struct *work)
172 {
173 	struct delayed_work *dwork = to_delayed_work(work);
174 	struct can_priv *priv = container_of(dwork, struct can_priv,
175 					     restart_work);
176 
177 	can_restart(priv->dev);
178 }
179 
180 int can_restart_now(struct net_device *dev)
181 {
182 	struct can_priv *priv = netdev_priv(dev);
183 
184 	/* A manual restart is only permitted if automatic restart is
185 	 * disabled and the device is in the bus-off state
186 	 */
187 	if (priv->restart_ms)
188 		return -EINVAL;
189 	if (priv->state != CAN_STATE_BUS_OFF)
190 		return -EBUSY;
191 
192 	cancel_delayed_work_sync(&priv->restart_work);
193 	can_restart(dev);
194 
195 	return 0;
196 }
197 
198 /* CAN bus-off
199  *
200  * This functions should be called when the device goes bus-off to
201  * tell the netif layer that no more packets can be sent or received.
202  * If enabled, a timer is started to trigger bus-off recovery.
203  */
204 void can_bus_off(struct net_device *dev)
205 {
206 	struct can_priv *priv = netdev_priv(dev);
207 
208 	if (priv->restart_ms)
209 		netdev_info(dev, "bus-off, scheduling restart in %d ms\n",
210 			    priv->restart_ms);
211 	else
212 		netdev_info(dev, "bus-off\n");
213 
214 	netif_carrier_off(dev);
215 
216 	if (priv->restart_ms)
217 		schedule_delayed_work(&priv->restart_work,
218 				      msecs_to_jiffies(priv->restart_ms));
219 }
220 EXPORT_SYMBOL_GPL(can_bus_off);
221 
222 void can_setup(struct net_device *dev)
223 {
224 	dev->type = ARPHRD_CAN;
225 	dev->mtu = CAN_MTU;
226 	dev->hard_header_len = 0;
227 	dev->addr_len = 0;
228 	dev->tx_queue_len = 10;
229 
230 	/* New-style flags. */
231 	dev->flags = IFF_NOARP;
232 	dev->features = NETIF_F_HW_CSUM;
233 }
234 
235 /* Allocate and setup space for the CAN network device */
236 struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max,
237 				    unsigned int txqs, unsigned int rxqs)
238 {
239 	struct can_ml_priv *can_ml;
240 	struct net_device *dev;
241 	struct can_priv *priv;
242 	int size;
243 
244 	/* We put the driver's priv, the CAN mid layer priv and the
245 	 * echo skb into the netdevice's priv. The memory layout for
246 	 * the netdev_priv is like this:
247 	 *
248 	 * +-------------------------+
249 	 * | driver's priv           |
250 	 * +-------------------------+
251 	 * | struct can_ml_priv      |
252 	 * +-------------------------+
253 	 * | array of struct sk_buff |
254 	 * +-------------------------+
255 	 */
256 
257 	size = ALIGN(sizeof_priv, NETDEV_ALIGN) + sizeof(struct can_ml_priv);
258 
259 	if (echo_skb_max)
260 		size = ALIGN(size, sizeof(struct sk_buff *)) +
261 			echo_skb_max * sizeof(struct sk_buff *);
262 
263 	dev = alloc_netdev_mqs(size, "can%d", NET_NAME_UNKNOWN, can_setup,
264 			       txqs, rxqs);
265 	if (!dev)
266 		return NULL;
267 
268 	priv = netdev_priv(dev);
269 	priv->dev = dev;
270 
271 	can_ml = (void *)priv + ALIGN(sizeof_priv, NETDEV_ALIGN);
272 	can_set_ml_priv(dev, can_ml);
273 
274 	if (echo_skb_max) {
275 		priv->echo_skb_max = echo_skb_max;
276 		priv->echo_skb = (void *)priv +
277 			(size - echo_skb_max * sizeof(struct sk_buff *));
278 	}
279 
280 	priv->state = CAN_STATE_STOPPED;
281 
282 	INIT_DELAYED_WORK(&priv->restart_work, can_restart_work);
283 
284 	return dev;
285 }
286 EXPORT_SYMBOL_GPL(alloc_candev_mqs);
287 
288 /* Free space of the CAN network device */
289 void free_candev(struct net_device *dev)
290 {
291 	free_netdev(dev);
292 }
293 EXPORT_SYMBOL_GPL(free_candev);
294 
295 /* changing MTU and control mode for CAN/CANFD devices */
296 int can_change_mtu(struct net_device *dev, int new_mtu)
297 {
298 	struct can_priv *priv = netdev_priv(dev);
299 	u32 ctrlmode_static = can_get_static_ctrlmode(priv);
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 (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 		    !(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 static int can_set_termination(struct net_device *ndev, u16 term)
402 {
403 	struct can_priv *priv = netdev_priv(ndev);
404 	int set;
405 
406 	if (term == priv->termination_gpio_ohms[CAN_TERMINATION_GPIO_ENABLED])
407 		set = 1;
408 	else
409 		set = 0;
410 
411 	gpiod_set_value(priv->termination_gpio, set);
412 
413 	return 0;
414 }
415 
416 static int can_get_termination(struct net_device *ndev)
417 {
418 	struct can_priv *priv = netdev_priv(ndev);
419 	struct device *dev = ndev->dev.parent;
420 	struct gpio_desc *gpio;
421 	u32 term;
422 	int ret;
423 
424 	/* Disabling termination by default is the safe choice: Else if many
425 	 * bus participants enable it, no communication is possible at all.
426 	 */
427 	gpio = devm_gpiod_get_optional(dev, "termination", GPIOD_OUT_LOW);
428 	if (IS_ERR(gpio))
429 		return dev_err_probe(dev, PTR_ERR(gpio),
430 				     "Cannot get termination-gpios\n");
431 
432 	if (!gpio)
433 		return 0;
434 
435 	ret = device_property_read_u32(dev, "termination-ohms", &term);
436 	if (ret) {
437 		netdev_err(ndev, "Cannot get termination-ohms: %pe\n",
438 			   ERR_PTR(ret));
439 		return ret;
440 	}
441 
442 	if (term > U16_MAX) {
443 		netdev_err(ndev, "Invalid termination-ohms value (%u > %u)\n",
444 			   term, U16_MAX);
445 		return -EINVAL;
446 	}
447 
448 	priv->termination_const_cnt = ARRAY_SIZE(priv->termination_gpio_ohms);
449 	priv->termination_const = priv->termination_gpio_ohms;
450 	priv->termination_gpio = gpio;
451 	priv->termination_gpio_ohms[CAN_TERMINATION_GPIO_DISABLED] =
452 		CAN_TERMINATION_DISABLED;
453 	priv->termination_gpio_ohms[CAN_TERMINATION_GPIO_ENABLED] = term;
454 	priv->do_set_termination = can_set_termination;
455 
456 	return 0;
457 }
458 
459 /* Register the CAN network device */
460 int register_candev(struct net_device *dev)
461 {
462 	struct can_priv *priv = netdev_priv(dev);
463 	int err;
464 
465 	/* Ensure termination_const, termination_const_cnt and
466 	 * do_set_termination consistency. All must be either set or
467 	 * unset.
468 	 */
469 	if ((!priv->termination_const != !priv->termination_const_cnt) ||
470 	    (!priv->termination_const != !priv->do_set_termination))
471 		return -EINVAL;
472 
473 	if (!priv->bitrate_const != !priv->bitrate_const_cnt)
474 		return -EINVAL;
475 
476 	if (!priv->data_bitrate_const != !priv->data_bitrate_const_cnt)
477 		return -EINVAL;
478 
479 	if (!priv->termination_const) {
480 		err = can_get_termination(dev);
481 		if (err)
482 			return err;
483 	}
484 
485 	dev->rtnl_link_ops = &can_link_ops;
486 	netif_carrier_off(dev);
487 
488 	return register_netdev(dev);
489 }
490 EXPORT_SYMBOL_GPL(register_candev);
491 
492 /* Unregister the CAN network device */
493 void unregister_candev(struct net_device *dev)
494 {
495 	unregister_netdev(dev);
496 }
497 EXPORT_SYMBOL_GPL(unregister_candev);
498 
499 /* Test if a network device is a candev based device
500  * and return the can_priv* if so.
501  */
502 struct can_priv *safe_candev_priv(struct net_device *dev)
503 {
504 	if (dev->type != ARPHRD_CAN || dev->rtnl_link_ops != &can_link_ops)
505 		return NULL;
506 
507 	return netdev_priv(dev);
508 }
509 EXPORT_SYMBOL_GPL(safe_candev_priv);
510 
511 static __init int can_dev_init(void)
512 {
513 	int err;
514 
515 	can_led_notifier_init();
516 
517 	err = can_netlink_register();
518 	if (!err)
519 		pr_info(MOD_DESC "\n");
520 
521 	return err;
522 }
523 module_init(can_dev_init);
524 
525 static __exit void can_dev_exit(void)
526 {
527 	can_netlink_unregister();
528 
529 	can_led_notifier_exit();
530 }
531 module_exit(can_dev_exit);
532 
533 MODULE_ALIAS_RTNL_LINK("can");
534