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