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