Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / net / can / dev / dev.c
blob6792c14fd7eb009d551ac22bab1f0ee2cd0f0398
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 */
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>
19 static void can_update_state_error_stats(struct net_device *dev,
20 enum can_state new_state)
22 struct can_priv *priv = netdev_priv(dev);
24 if (new_state <= priv->state)
25 return;
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;
42 static int can_tx_state_to_frame(struct net_device *dev, enum can_state state)
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;
56 static int can_rx_state_to_frame(struct net_device *dev, enum can_state state)
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;
70 const char *can_get_state_str(const enum can_state state)
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>";
89 return "<unknown>";
91 EXPORT_SYMBOL_GPL(can_get_state_str);
93 static enum can_state can_state_err_to_state(u16 err)
95 if (err < CAN_ERROR_WARNING_THRESHOLD)
96 return CAN_STATE_ERROR_ACTIVE;
97 if (err < CAN_ERROR_PASSIVE_THRESHOLD)
98 return CAN_STATE_ERROR_WARNING;
99 if (err < CAN_BUS_OFF_THRESHOLD)
100 return CAN_STATE_ERROR_PASSIVE;
102 return CAN_STATE_BUS_OFF;
105 void can_state_get_by_berr_counter(const struct net_device *dev,
106 const struct can_berr_counter *bec,
107 enum can_state *tx_state,
108 enum can_state *rx_state)
110 *tx_state = can_state_err_to_state(bec->txerr);
111 *rx_state = can_state_err_to_state(bec->rxerr);
113 EXPORT_SYMBOL_GPL(can_state_get_by_berr_counter);
115 void can_change_state(struct net_device *dev, struct can_frame *cf,
116 enum can_state tx_state, enum can_state rx_state)
118 struct can_priv *priv = netdev_priv(dev);
119 enum can_state new_state = max(tx_state, rx_state);
121 if (unlikely(new_state == priv->state)) {
122 netdev_warn(dev, "%s: oops, state did not change", __func__);
123 return;
126 netdev_dbg(dev, "Controller changed from %s State (%d) into %s State (%d).\n",
127 can_get_state_str(priv->state), priv->state,
128 can_get_state_str(new_state), new_state);
130 can_update_state_error_stats(dev, new_state);
131 priv->state = new_state;
133 if (!cf)
134 return;
136 if (unlikely(new_state == CAN_STATE_BUS_OFF)) {
137 cf->can_id |= CAN_ERR_BUSOFF;
138 return;
141 cf->can_id |= CAN_ERR_CRTL;
142 cf->data[1] |= tx_state >= rx_state ?
143 can_tx_state_to_frame(dev, tx_state) : 0;
144 cf->data[1] |= tx_state <= rx_state ?
145 can_rx_state_to_frame(dev, rx_state) : 0;
147 EXPORT_SYMBOL_GPL(can_change_state);
149 /* CAN device restart for bus-off recovery */
150 static void can_restart(struct net_device *dev)
152 struct can_priv *priv = netdev_priv(dev);
153 struct sk_buff *skb;
154 struct can_frame *cf;
155 int err;
157 if (netif_carrier_ok(dev))
158 netdev_err(dev, "Attempt to restart for bus-off recovery, but carrier is OK?\n");
160 /* No synchronization needed because the device is bus-off and
161 * no messages can come in or go out.
163 can_flush_echo_skb(dev);
165 /* send restart message upstream */
166 skb = alloc_can_err_skb(dev, &cf);
167 if (skb) {
168 cf->can_id |= CAN_ERR_RESTARTED;
169 netif_rx(skb);
172 /* Now restart the device */
173 netif_carrier_on(dev);
174 err = priv->do_set_mode(dev, CAN_MODE_START);
175 if (err) {
176 netdev_err(dev, "Restart failed, error %pe\n", ERR_PTR(err));
177 netif_carrier_off(dev);
178 } else {
179 netdev_dbg(dev, "Restarted\n");
180 priv->can_stats.restarts++;
184 static void can_restart_work(struct work_struct *work)
186 struct delayed_work *dwork = to_delayed_work(work);
187 struct can_priv *priv = container_of(dwork, struct can_priv,
188 restart_work);
190 can_restart(priv->dev);
193 int can_restart_now(struct net_device *dev)
195 struct can_priv *priv = netdev_priv(dev);
197 /* A manual restart is only permitted if automatic restart is
198 * disabled and the device is in the bus-off state
200 if (priv->restart_ms)
201 return -EINVAL;
202 if (priv->state != CAN_STATE_BUS_OFF)
203 return -EBUSY;
205 cancel_delayed_work_sync(&priv->restart_work);
206 can_restart(dev);
208 return 0;
211 /* CAN bus-off
213 * This functions should be called when the device goes bus-off to
214 * tell the netif layer that no more packets can be sent or received.
215 * If enabled, a timer is started to trigger bus-off recovery.
217 void can_bus_off(struct net_device *dev)
219 struct can_priv *priv = netdev_priv(dev);
221 if (priv->restart_ms)
222 netdev_info(dev, "bus-off, scheduling restart in %d ms\n",
223 priv->restart_ms);
224 else
225 netdev_info(dev, "bus-off\n");
227 netif_carrier_off(dev);
229 if (priv->restart_ms)
230 schedule_delayed_work(&priv->restart_work,
231 msecs_to_jiffies(priv->restart_ms));
233 EXPORT_SYMBOL_GPL(can_bus_off);
235 void can_setup(struct net_device *dev)
237 dev->type = ARPHRD_CAN;
238 dev->mtu = CAN_MTU;
239 dev->hard_header_len = 0;
240 dev->addr_len = 0;
241 dev->tx_queue_len = 10;
243 /* New-style flags. */
244 dev->flags = IFF_NOARP;
245 dev->features = NETIF_F_HW_CSUM;
248 /* Allocate and setup space for the CAN network device */
249 struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max,
250 unsigned int txqs, unsigned int rxqs)
252 struct can_ml_priv *can_ml;
253 struct net_device *dev;
254 struct can_priv *priv;
255 int size;
257 /* We put the driver's priv, the CAN mid layer priv and the
258 * echo skb into the netdevice's priv. The memory layout for
259 * the netdev_priv is like this:
261 * +-------------------------+
262 * | driver's priv |
263 * +-------------------------+
264 * | struct can_ml_priv |
265 * +-------------------------+
266 * | array of struct sk_buff |
267 * +-------------------------+
270 size = ALIGN(sizeof_priv, NETDEV_ALIGN) + sizeof(struct can_ml_priv);
272 if (echo_skb_max)
273 size = ALIGN(size, sizeof(struct sk_buff *)) +
274 echo_skb_max * sizeof(struct sk_buff *);
276 dev = alloc_netdev_mqs(size, "can%d", NET_NAME_UNKNOWN, can_setup,
277 txqs, rxqs);
278 if (!dev)
279 return NULL;
281 priv = netdev_priv(dev);
282 priv->dev = dev;
284 can_ml = (void *)priv + ALIGN(sizeof_priv, NETDEV_ALIGN);
285 can_set_ml_priv(dev, can_ml);
287 if (echo_skb_max) {
288 priv->echo_skb_max = echo_skb_max;
289 priv->echo_skb = (void *)priv +
290 (size - echo_skb_max * sizeof(struct sk_buff *));
293 priv->state = CAN_STATE_STOPPED;
295 INIT_DELAYED_WORK(&priv->restart_work, can_restart_work);
297 return dev;
299 EXPORT_SYMBOL_GPL(alloc_candev_mqs);
301 /* Free space of the CAN network device */
302 void free_candev(struct net_device *dev)
304 free_netdev(dev);
306 EXPORT_SYMBOL_GPL(free_candev);
308 /* changing MTU and control mode for CAN/CANFD devices */
309 int can_change_mtu(struct net_device *dev, int new_mtu)
311 struct can_priv *priv = netdev_priv(dev);
312 u32 ctrlmode_static = can_get_static_ctrlmode(priv);
314 /* Do not allow changing the MTU while running */
315 if (dev->flags & IFF_UP)
316 return -EBUSY;
318 /* allow change of MTU according to the CANFD ability of the device */
319 switch (new_mtu) {
320 case CAN_MTU:
321 /* 'CANFD-only' controllers can not switch to CAN_MTU */
322 if (ctrlmode_static & CAN_CTRLMODE_FD)
323 return -EINVAL;
325 priv->ctrlmode &= ~CAN_CTRLMODE_FD;
326 break;
328 case CANFD_MTU:
329 /* check for potential CANFD ability */
330 if (!(priv->ctrlmode_supported & CAN_CTRLMODE_FD) &&
331 !(ctrlmode_static & CAN_CTRLMODE_FD))
332 return -EINVAL;
334 priv->ctrlmode |= CAN_CTRLMODE_FD;
335 break;
337 default:
338 return -EINVAL;
341 WRITE_ONCE(dev->mtu, new_mtu);
342 return 0;
344 EXPORT_SYMBOL_GPL(can_change_mtu);
346 /* generic implementation of netdev_ops::ndo_eth_ioctl for CAN devices
347 * supporting hardware timestamps
349 int can_eth_ioctl_hwts(struct net_device *netdev, struct ifreq *ifr, int cmd)
351 struct hwtstamp_config hwts_cfg = { 0 };
353 switch (cmd) {
354 case SIOCSHWTSTAMP: /* set */
355 if (copy_from_user(&hwts_cfg, ifr->ifr_data, sizeof(hwts_cfg)))
356 return -EFAULT;
357 if (hwts_cfg.tx_type == HWTSTAMP_TX_ON &&
358 hwts_cfg.rx_filter == HWTSTAMP_FILTER_ALL)
359 return 0;
360 return -ERANGE;
362 case SIOCGHWTSTAMP: /* get */
363 hwts_cfg.tx_type = HWTSTAMP_TX_ON;
364 hwts_cfg.rx_filter = HWTSTAMP_FILTER_ALL;
365 if (copy_to_user(ifr->ifr_data, &hwts_cfg, sizeof(hwts_cfg)))
366 return -EFAULT;
367 return 0;
369 default:
370 return -EOPNOTSUPP;
373 EXPORT_SYMBOL(can_eth_ioctl_hwts);
375 /* generic implementation of ethtool_ops::get_ts_info for CAN devices
376 * supporting hardware timestamps
378 int can_ethtool_op_get_ts_info_hwts(struct net_device *dev,
379 struct kernel_ethtool_ts_info *info)
381 info->so_timestamping =
382 SOF_TIMESTAMPING_TX_SOFTWARE |
383 SOF_TIMESTAMPING_TX_HARDWARE |
384 SOF_TIMESTAMPING_RX_HARDWARE |
385 SOF_TIMESTAMPING_RAW_HARDWARE;
386 info->tx_types = BIT(HWTSTAMP_TX_ON);
387 info->rx_filters = BIT(HWTSTAMP_FILTER_ALL);
389 return 0;
391 EXPORT_SYMBOL(can_ethtool_op_get_ts_info_hwts);
393 /* Common open function when the device gets opened.
395 * This function should be called in the open function of the device
396 * driver.
398 int open_candev(struct net_device *dev)
400 struct can_priv *priv = netdev_priv(dev);
402 if (!priv->bittiming.bitrate) {
403 netdev_err(dev, "bit-timing not yet defined\n");
404 return -EINVAL;
407 /* For CAN FD the data bitrate has to be >= the arbitration bitrate */
408 if ((priv->ctrlmode & CAN_CTRLMODE_FD) &&
409 (!priv->data_bittiming.bitrate ||
410 priv->data_bittiming.bitrate < priv->bittiming.bitrate)) {
411 netdev_err(dev, "incorrect/missing data bit-timing\n");
412 return -EINVAL;
415 /* Switch carrier on if device was stopped while in bus-off state */
416 if (!netif_carrier_ok(dev))
417 netif_carrier_on(dev);
419 return 0;
421 EXPORT_SYMBOL_GPL(open_candev);
423 #ifdef CONFIG_OF
424 /* Common function that can be used to understand the limitation of
425 * a transceiver when it provides no means to determine these limitations
426 * at runtime.
428 void of_can_transceiver(struct net_device *dev)
430 struct device_node *dn;
431 struct can_priv *priv = netdev_priv(dev);
432 struct device_node *np = dev->dev.parent->of_node;
433 int ret;
435 dn = of_get_child_by_name(np, "can-transceiver");
436 if (!dn)
437 return;
439 ret = of_property_read_u32(dn, "max-bitrate", &priv->bitrate_max);
440 of_node_put(dn);
441 if ((ret && ret != -EINVAL) || (!ret && !priv->bitrate_max))
442 netdev_warn(dev, "Invalid value for transceiver max bitrate. Ignoring bitrate limit.\n");
444 EXPORT_SYMBOL_GPL(of_can_transceiver);
445 #endif
447 /* Common close function for cleanup before the device gets closed.
449 * This function should be called in the close function of the device
450 * driver.
452 void close_candev(struct net_device *dev)
454 struct can_priv *priv = netdev_priv(dev);
456 cancel_delayed_work_sync(&priv->restart_work);
457 can_flush_echo_skb(dev);
459 EXPORT_SYMBOL_GPL(close_candev);
461 static int can_set_termination(struct net_device *ndev, u16 term)
463 struct can_priv *priv = netdev_priv(ndev);
464 int set;
466 if (term == priv->termination_gpio_ohms[CAN_TERMINATION_GPIO_ENABLED])
467 set = 1;
468 else
469 set = 0;
471 gpiod_set_value(priv->termination_gpio, set);
473 return 0;
476 static int can_get_termination(struct net_device *ndev)
478 struct can_priv *priv = netdev_priv(ndev);
479 struct device *dev = ndev->dev.parent;
480 struct gpio_desc *gpio;
481 u32 term;
482 int ret;
484 /* Disabling termination by default is the safe choice: Else if many
485 * bus participants enable it, no communication is possible at all.
487 gpio = devm_gpiod_get_optional(dev, "termination", GPIOD_OUT_LOW);
488 if (IS_ERR(gpio))
489 return dev_err_probe(dev, PTR_ERR(gpio),
490 "Cannot get termination-gpios\n");
492 if (!gpio)
493 return 0;
495 ret = device_property_read_u32(dev, "termination-ohms", &term);
496 if (ret) {
497 netdev_err(ndev, "Cannot get termination-ohms: %pe\n",
498 ERR_PTR(ret));
499 return ret;
502 if (term > U16_MAX) {
503 netdev_err(ndev, "Invalid termination-ohms value (%u > %u)\n",
504 term, U16_MAX);
505 return -EINVAL;
508 priv->termination_const_cnt = ARRAY_SIZE(priv->termination_gpio_ohms);
509 priv->termination_const = priv->termination_gpio_ohms;
510 priv->termination_gpio = gpio;
511 priv->termination_gpio_ohms[CAN_TERMINATION_GPIO_DISABLED] =
512 CAN_TERMINATION_DISABLED;
513 priv->termination_gpio_ohms[CAN_TERMINATION_GPIO_ENABLED] = term;
514 priv->do_set_termination = can_set_termination;
516 return 0;
519 static bool
520 can_bittiming_const_valid(const struct can_bittiming_const *btc)
522 if (!btc)
523 return true;
525 if (!btc->sjw_max)
526 return false;
528 return true;
531 /* Register the CAN network device */
532 int register_candev(struct net_device *dev)
534 struct can_priv *priv = netdev_priv(dev);
535 int err;
537 /* Ensure termination_const, termination_const_cnt and
538 * do_set_termination consistency. All must be either set or
539 * unset.
541 if ((!priv->termination_const != !priv->termination_const_cnt) ||
542 (!priv->termination_const != !priv->do_set_termination))
543 return -EINVAL;
545 if (!priv->bitrate_const != !priv->bitrate_const_cnt)
546 return -EINVAL;
548 if (!priv->data_bitrate_const != !priv->data_bitrate_const_cnt)
549 return -EINVAL;
551 /* We only support either fixed bit rates or bit timing const. */
552 if ((priv->bitrate_const || priv->data_bitrate_const) &&
553 (priv->bittiming_const || priv->data_bittiming_const))
554 return -EINVAL;
556 if (!can_bittiming_const_valid(priv->bittiming_const) ||
557 !can_bittiming_const_valid(priv->data_bittiming_const))
558 return -EINVAL;
560 if (!priv->termination_const) {
561 err = can_get_termination(dev);
562 if (err)
563 return err;
566 dev->rtnl_link_ops = &can_link_ops;
567 netif_carrier_off(dev);
569 return register_netdev(dev);
571 EXPORT_SYMBOL_GPL(register_candev);
573 /* Unregister the CAN network device */
574 void unregister_candev(struct net_device *dev)
576 unregister_netdev(dev);
578 EXPORT_SYMBOL_GPL(unregister_candev);
580 /* Test if a network device is a candev based device
581 * and return the can_priv* if so.
583 struct can_priv *safe_candev_priv(struct net_device *dev)
585 if (dev->type != ARPHRD_CAN || dev->rtnl_link_ops != &can_link_ops)
586 return NULL;
588 return netdev_priv(dev);
590 EXPORT_SYMBOL_GPL(safe_candev_priv);
592 static __init int can_dev_init(void)
594 int err;
596 err = can_netlink_register();
597 if (!err)
598 pr_info("CAN device driver interface\n");
600 return err;
602 module_init(can_dev_init);
604 static __exit void can_dev_exit(void)
606 can_netlink_unregister();
608 module_exit(can_dev_exit);
610 MODULE_ALIAS_RTNL_LINK("can");