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