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