xref: /openbmc/linux/drivers/net/can/dev/dev.c (revision c4e78957)
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/gpio/consumer.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 sk_buff *skb;
139 	struct can_frame *cf;
140 	int err;
141 
142 	BUG_ON(netif_carrier_ok(dev));
143 
144 	/* No synchronization needed because the device is bus-off and
145 	 * no messages can come in or go out.
146 	 */
147 	can_flush_echo_skb(dev);
148 
149 	/* send restart message upstream */
150 	skb = alloc_can_err_skb(dev, &cf);
151 	if (!skb)
152 		goto restart;
153 
154 	cf->can_id |= CAN_ERR_RESTARTED;
155 
156 	netif_rx(skb);
157 
158 restart:
159 	netdev_dbg(dev, "restarted\n");
160 	priv->can_stats.restarts++;
161 
162 	/* Now restart the device */
163 	err = priv->do_set_mode(dev, CAN_MODE_START);
164 
165 	netif_carrier_on(dev);
166 	if (err)
167 		netdev_err(dev, "Error %d during restart", err);
168 }
169 
170 static void can_restart_work(struct work_struct *work)
171 {
172 	struct delayed_work *dwork = to_delayed_work(work);
173 	struct can_priv *priv = container_of(dwork, struct can_priv,
174 					     restart_work);
175 
176 	can_restart(priv->dev);
177 }
178 
179 int can_restart_now(struct net_device *dev)
180 {
181 	struct can_priv *priv = netdev_priv(dev);
182 
183 	/* A manual restart is only permitted if automatic restart is
184 	 * disabled and the device is in the bus-off state
185 	 */
186 	if (priv->restart_ms)
187 		return -EINVAL;
188 	if (priv->state != CAN_STATE_BUS_OFF)
189 		return -EBUSY;
190 
191 	cancel_delayed_work_sync(&priv->restart_work);
192 	can_restart(dev);
193 
194 	return 0;
195 }
196 
197 /* CAN bus-off
198  *
199  * This functions should be called when the device goes bus-off to
200  * tell the netif layer that no more packets can be sent or received.
201  * If enabled, a timer is started to trigger bus-off recovery.
202  */
203 void can_bus_off(struct net_device *dev)
204 {
205 	struct can_priv *priv = netdev_priv(dev);
206 
207 	if (priv->restart_ms)
208 		netdev_info(dev, "bus-off, scheduling restart in %d ms\n",
209 			    priv->restart_ms);
210 	else
211 		netdev_info(dev, "bus-off\n");
212 
213 	netif_carrier_off(dev);
214 
215 	if (priv->restart_ms)
216 		schedule_delayed_work(&priv->restart_work,
217 				      msecs_to_jiffies(priv->restart_ms));
218 }
219 EXPORT_SYMBOL_GPL(can_bus_off);
220 
221 void can_setup(struct net_device *dev)
222 {
223 	dev->type = ARPHRD_CAN;
224 	dev->mtu = CAN_MTU;
225 	dev->hard_header_len = 0;
226 	dev->addr_len = 0;
227 	dev->tx_queue_len = 10;
228 
229 	/* New-style flags. */
230 	dev->flags = IFF_NOARP;
231 	dev->features = NETIF_F_HW_CSUM;
232 }
233 
234 /* Allocate and setup space for the CAN network device */
235 struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max,
236 				    unsigned int txqs, unsigned int rxqs)
237 {
238 	struct can_ml_priv *can_ml;
239 	struct net_device *dev;
240 	struct can_priv *priv;
241 	int size;
242 
243 	/* We put the driver's priv, the CAN mid layer priv and the
244 	 * echo skb into the netdevice's priv. The memory layout for
245 	 * the netdev_priv is like this:
246 	 *
247 	 * +-------------------------+
248 	 * | driver's priv           |
249 	 * +-------------------------+
250 	 * | struct can_ml_priv      |
251 	 * +-------------------------+
252 	 * | array of struct sk_buff |
253 	 * +-------------------------+
254 	 */
255 
256 	size = ALIGN(sizeof_priv, NETDEV_ALIGN) + sizeof(struct can_ml_priv);
257 
258 	if (echo_skb_max)
259 		size = ALIGN(size, sizeof(struct sk_buff *)) +
260 			echo_skb_max * sizeof(struct sk_buff *);
261 
262 	dev = alloc_netdev_mqs(size, "can%d", NET_NAME_UNKNOWN, can_setup,
263 			       txqs, rxqs);
264 	if (!dev)
265 		return NULL;
266 
267 	priv = netdev_priv(dev);
268 	priv->dev = dev;
269 
270 	can_ml = (void *)priv + ALIGN(sizeof_priv, NETDEV_ALIGN);
271 	can_set_ml_priv(dev, can_ml);
272 
273 	if (echo_skb_max) {
274 		priv->echo_skb_max = echo_skb_max;
275 		priv->echo_skb = (void *)priv +
276 			(size - echo_skb_max * sizeof(struct sk_buff *));
277 	}
278 
279 	priv->state = CAN_STATE_STOPPED;
280 
281 	INIT_DELAYED_WORK(&priv->restart_work, can_restart_work);
282 
283 	return dev;
284 }
285 EXPORT_SYMBOL_GPL(alloc_candev_mqs);
286 
287 /* Free space of the CAN network device */
288 void free_candev(struct net_device *dev)
289 {
290 	free_netdev(dev);
291 }
292 EXPORT_SYMBOL_GPL(free_candev);
293 
294 /* changing MTU and control mode for CAN/CANFD devices */
295 int can_change_mtu(struct net_device *dev, int new_mtu)
296 {
297 	struct can_priv *priv = netdev_priv(dev);
298 	u32 ctrlmode_static = can_get_static_ctrlmode(priv);
299 
300 	/* Do not allow changing the MTU while running */
301 	if (dev->flags & IFF_UP)
302 		return -EBUSY;
303 
304 	/* allow change of MTU according to the CANFD ability of the device */
305 	switch (new_mtu) {
306 	case CAN_MTU:
307 		/* 'CANFD-only' controllers can not switch to CAN_MTU */
308 		if (ctrlmode_static & CAN_CTRLMODE_FD)
309 			return -EINVAL;
310 
311 		priv->ctrlmode &= ~CAN_CTRLMODE_FD;
312 		break;
313 
314 	case CANFD_MTU:
315 		/* check for potential CANFD ability */
316 		if (!(priv->ctrlmode_supported & CAN_CTRLMODE_FD) &&
317 		    !(ctrlmode_static & CAN_CTRLMODE_FD))
318 			return -EINVAL;
319 
320 		priv->ctrlmode |= CAN_CTRLMODE_FD;
321 		break;
322 
323 	default:
324 		return -EINVAL;
325 	}
326 
327 	dev->mtu = new_mtu;
328 	return 0;
329 }
330 EXPORT_SYMBOL_GPL(can_change_mtu);
331 
332 /* Common open function when the device gets opened.
333  *
334  * This function should be called in the open function of the device
335  * driver.
336  */
337 int open_candev(struct net_device *dev)
338 {
339 	struct can_priv *priv = netdev_priv(dev);
340 
341 	if (!priv->bittiming.bitrate) {
342 		netdev_err(dev, "bit-timing not yet defined\n");
343 		return -EINVAL;
344 	}
345 
346 	/* For CAN FD the data bitrate has to be >= the arbitration bitrate */
347 	if ((priv->ctrlmode & CAN_CTRLMODE_FD) &&
348 	    (!priv->data_bittiming.bitrate ||
349 	     priv->data_bittiming.bitrate < priv->bittiming.bitrate)) {
350 		netdev_err(dev, "incorrect/missing data bit-timing\n");
351 		return -EINVAL;
352 	}
353 
354 	/* Switch carrier on if device was stopped while in bus-off state */
355 	if (!netif_carrier_ok(dev))
356 		netif_carrier_on(dev);
357 
358 	return 0;
359 }
360 EXPORT_SYMBOL_GPL(open_candev);
361 
362 #ifdef CONFIG_OF
363 /* Common function that can be used to understand the limitation of
364  * a transceiver when it provides no means to determine these limitations
365  * at runtime.
366  */
367 void of_can_transceiver(struct net_device *dev)
368 {
369 	struct device_node *dn;
370 	struct can_priv *priv = netdev_priv(dev);
371 	struct device_node *np = dev->dev.parent->of_node;
372 	int ret;
373 
374 	dn = of_get_child_by_name(np, "can-transceiver");
375 	if (!dn)
376 		return;
377 
378 	ret = of_property_read_u32(dn, "max-bitrate", &priv->bitrate_max);
379 	of_node_put(dn);
380 	if ((ret && ret != -EINVAL) || (!ret && !priv->bitrate_max))
381 		netdev_warn(dev, "Invalid value for transceiver max bitrate. Ignoring bitrate limit.\n");
382 }
383 EXPORT_SYMBOL_GPL(of_can_transceiver);
384 #endif
385 
386 /* Common close function for cleanup before the device gets closed.
387  *
388  * This function should be called in the close function of the device
389  * driver.
390  */
391 void close_candev(struct net_device *dev)
392 {
393 	struct can_priv *priv = netdev_priv(dev);
394 
395 	cancel_delayed_work_sync(&priv->restart_work);
396 	can_flush_echo_skb(dev);
397 }
398 EXPORT_SYMBOL_GPL(close_candev);
399 
400 static int can_set_termination(struct net_device *ndev, u16 term)
401 {
402 	struct can_priv *priv = netdev_priv(ndev);
403 	int set;
404 
405 	if (term == priv->termination_gpio_ohms[CAN_TERMINATION_GPIO_ENABLED])
406 		set = 1;
407 	else
408 		set = 0;
409 
410 	gpiod_set_value(priv->termination_gpio, set);
411 
412 	return 0;
413 }
414 
415 static int can_get_termination(struct net_device *ndev)
416 {
417 	struct can_priv *priv = netdev_priv(ndev);
418 	struct device *dev = ndev->dev.parent;
419 	struct gpio_desc *gpio;
420 	u32 term;
421 	int ret;
422 
423 	/* Disabling termination by default is the safe choice: Else if many
424 	 * bus participants enable it, no communication is possible at all.
425 	 */
426 	gpio = devm_gpiod_get_optional(dev, "termination", GPIOD_OUT_LOW);
427 	if (IS_ERR(gpio))
428 		return dev_err_probe(dev, PTR_ERR(gpio),
429 				     "Cannot get termination-gpios\n");
430 
431 	if (!gpio)
432 		return 0;
433 
434 	ret = device_property_read_u32(dev, "termination-ohms", &term);
435 	if (ret) {
436 		netdev_err(ndev, "Cannot get termination-ohms: %pe\n",
437 			   ERR_PTR(ret));
438 		return ret;
439 	}
440 
441 	if (term > U16_MAX) {
442 		netdev_err(ndev, "Invalid termination-ohms value (%u > %u)\n",
443 			   term, U16_MAX);
444 		return -EINVAL;
445 	}
446 
447 	priv->termination_const_cnt = ARRAY_SIZE(priv->termination_gpio_ohms);
448 	priv->termination_const = priv->termination_gpio_ohms;
449 	priv->termination_gpio = gpio;
450 	priv->termination_gpio_ohms[CAN_TERMINATION_GPIO_DISABLED] =
451 		CAN_TERMINATION_DISABLED;
452 	priv->termination_gpio_ohms[CAN_TERMINATION_GPIO_ENABLED] = term;
453 	priv->do_set_termination = can_set_termination;
454 
455 	return 0;
456 }
457 
458 /* Register the CAN network device */
459 int register_candev(struct net_device *dev)
460 {
461 	struct can_priv *priv = netdev_priv(dev);
462 	int err;
463 
464 	/* Ensure termination_const, termination_const_cnt and
465 	 * do_set_termination consistency. All must be either set or
466 	 * unset.
467 	 */
468 	if ((!priv->termination_const != !priv->termination_const_cnt) ||
469 	    (!priv->termination_const != !priv->do_set_termination))
470 		return -EINVAL;
471 
472 	if (!priv->bitrate_const != !priv->bitrate_const_cnt)
473 		return -EINVAL;
474 
475 	if (!priv->data_bitrate_const != !priv->data_bitrate_const_cnt)
476 		return -EINVAL;
477 
478 	if (!priv->termination_const) {
479 		err = can_get_termination(dev);
480 		if (err)
481 			return err;
482 	}
483 
484 	dev->rtnl_link_ops = &can_link_ops;
485 	netif_carrier_off(dev);
486 
487 	return register_netdev(dev);
488 }
489 EXPORT_SYMBOL_GPL(register_candev);
490 
491 /* Unregister the CAN network device */
492 void unregister_candev(struct net_device *dev)
493 {
494 	unregister_netdev(dev);
495 }
496 EXPORT_SYMBOL_GPL(unregister_candev);
497 
498 /* Test if a network device is a candev based device
499  * and return the can_priv* if so.
500  */
501 struct can_priv *safe_candev_priv(struct net_device *dev)
502 {
503 	if (dev->type != ARPHRD_CAN || dev->rtnl_link_ops != &can_link_ops)
504 		return NULL;
505 
506 	return netdev_priv(dev);
507 }
508 EXPORT_SYMBOL_GPL(safe_candev_priv);
509 
510 static __init int can_dev_init(void)
511 {
512 	int err;
513 
514 	err = can_netlink_register();
515 	if (!err)
516 		pr_info(MOD_DESC "\n");
517 
518 	return err;
519 }
520 module_init(can_dev_init);
521 
522 static __exit void can_dev_exit(void)
523 {
524 	can_netlink_unregister();
525 }
526 module_exit(can_dev_exit);
527 
528 MODULE_ALIAS_RTNL_LINK("can");
529