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