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>
7 #include <linux/module.h>
8 #include <linux/kernel.h>
9 #include <linux/slab.h>
10 #include <linux/netdevice.h>
11 #include <linux/if_arp.h>
12 #include <linux/workqueue.h>
13 #include <linux/can.h>
14 #include <linux/can/dev.h>
15 #include <linux/can/skb.h>
16 #include <linux/can/netlink.h>
17 #include <linux/can/led.h>
19 #include <net/rtnetlink.h>
21 #define MOD_DESC "CAN device driver interface"
23 MODULE_DESCRIPTION(MOD_DESC);
24 MODULE_LICENSE("GPL v2");
25 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
27 /* CAN DLC to real data length conversion helpers */
29 static const u8 dlc2len[] = {0, 1, 2, 3, 4, 5, 6, 7,
30 8, 12, 16, 20, 24, 32, 48, 64};
32 /* get data length from can_dlc with sanitized can_dlc */
33 u8 can_dlc2len(u8 can_dlc)
35 return dlc2len[can_dlc & 0x0F];
37 EXPORT_SYMBOL_GPL(can_dlc2len);
39 static const u8 len2dlc[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, /* 0 - 8 */
40 9, 9, 9, 9, /* 9 - 12 */
41 10, 10, 10, 10, /* 13 - 16 */
42 11, 11, 11, 11, /* 17 - 20 */
43 12, 12, 12, 12, /* 21 - 24 */
44 13, 13, 13, 13, 13, 13, 13, 13, /* 25 - 32 */
45 14, 14, 14, 14, 14, 14, 14, 14, /* 33 - 40 */
46 14, 14, 14, 14, 14, 14, 14, 14, /* 41 - 48 */
47 15, 15, 15, 15, 15, 15, 15, 15, /* 49 - 56 */
48 15, 15, 15, 15, 15, 15, 15, 15}; /* 57 - 64 */
50 /* map the sanitized data length to an appropriate data length code */
51 u8 can_len2dlc(u8 len)
53 if (unlikely(len > 64))
58 EXPORT_SYMBOL_GPL(can_len2dlc);
60 #ifdef CONFIG_CAN_CALC_BITTIMING
61 #define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */
62 #define CAN_CALC_SYNC_SEG 1
64 /* Bit-timing calculation derived from:
66 * Code based on LinCAN sources and H8S2638 project
67 * Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz
68 * Copyright 2005 Stanislav Marek
69 * email: pisa@cmp.felk.cvut.cz
71 * Calculates proper bit-timing parameters for a specified bit-rate
72 * and sample-point, which can then be used to set the bit-timing
73 * registers of the CAN controller. You can find more information
74 * in the header file linux/can/netlink.h.
76 static int can_update_sample_point(const struct can_bittiming_const *btc,
77 unsigned int sample_point_nominal, unsigned int tseg,
78 unsigned int *tseg1_ptr, unsigned int *tseg2_ptr,
79 unsigned int *sample_point_error_ptr)
81 unsigned int sample_point_error, best_sample_point_error = UINT_MAX;
82 unsigned int sample_point, best_sample_point = 0;
83 unsigned int tseg1, tseg2;
86 for (i = 0; i <= 1; i++) {
87 tseg2 = tseg + CAN_CALC_SYNC_SEG - (sample_point_nominal * (tseg + CAN_CALC_SYNC_SEG)) / 1000 - i;
88 tseg2 = clamp(tseg2, btc->tseg2_min, btc->tseg2_max);
90 if (tseg1 > btc->tseg1_max) {
91 tseg1 = btc->tseg1_max;
95 sample_point = 1000 * (tseg + CAN_CALC_SYNC_SEG - tseg2) / (tseg + CAN_CALC_SYNC_SEG);
96 sample_point_error = abs(sample_point_nominal - sample_point);
98 if ((sample_point <= sample_point_nominal) && (sample_point_error < best_sample_point_error)) {
99 best_sample_point = sample_point;
100 best_sample_point_error = sample_point_error;
106 if (sample_point_error_ptr)
107 *sample_point_error_ptr = best_sample_point_error;
109 return best_sample_point;
112 static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
113 const struct can_bittiming_const *btc)
115 struct can_priv *priv = netdev_priv(dev);
116 unsigned int bitrate; /* current bitrate */
117 unsigned int bitrate_error; /* difference between current and nominal value */
118 unsigned int best_bitrate_error = UINT_MAX;
119 unsigned int sample_point_error; /* difference between current and nominal value */
120 unsigned int best_sample_point_error = UINT_MAX;
121 unsigned int sample_point_nominal; /* nominal sample point */
122 unsigned int best_tseg = 0; /* current best value for tseg */
123 unsigned int best_brp = 0; /* current best value for brp */
124 unsigned int brp, tsegall, tseg, tseg1 = 0, tseg2 = 0;
127 /* Use CiA recommended sample points */
128 if (bt->sample_point) {
129 sample_point_nominal = bt->sample_point;
131 if (bt->bitrate > 800000)
132 sample_point_nominal = 750;
133 else if (bt->bitrate > 500000)
134 sample_point_nominal = 800;
136 sample_point_nominal = 875;
139 /* tseg even = round down, odd = round up */
140 for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
141 tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
142 tsegall = CAN_CALC_SYNC_SEG + tseg / 2;
144 /* Compute all possible tseg choices (tseg=tseg1+tseg2) */
145 brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2;
147 /* choose brp step which is possible in system */
148 brp = (brp / btc->brp_inc) * btc->brp_inc;
149 if ((brp < btc->brp_min) || (brp > btc->brp_max))
152 bitrate = priv->clock.freq / (brp * tsegall);
153 bitrate_error = abs(bt->bitrate - bitrate);
155 /* tseg brp biterror */
156 if (bitrate_error > best_bitrate_error)
159 /* reset sample point error if we have a better bitrate */
160 if (bitrate_error < best_bitrate_error)
161 best_sample_point_error = UINT_MAX;
163 can_update_sample_point(btc, sample_point_nominal, tseg / 2, &tseg1, &tseg2, &sample_point_error);
164 if (sample_point_error > best_sample_point_error)
167 best_sample_point_error = sample_point_error;
168 best_bitrate_error = bitrate_error;
169 best_tseg = tseg / 2;
172 if (bitrate_error == 0 && sample_point_error == 0)
176 if (best_bitrate_error) {
177 /* Error in one-tenth of a percent */
178 v64 = (u64)best_bitrate_error * 1000;
179 do_div(v64, bt->bitrate);
180 bitrate_error = (u32)v64;
181 if (bitrate_error > CAN_CALC_MAX_ERROR) {
183 "bitrate error %d.%d%% too high\n",
184 bitrate_error / 10, bitrate_error % 10);
187 netdev_warn(dev, "bitrate error %d.%d%%\n",
188 bitrate_error / 10, bitrate_error % 10);
191 /* real sample point */
192 bt->sample_point = can_update_sample_point(btc, sample_point_nominal, best_tseg,
193 &tseg1, &tseg2, NULL);
195 v64 = (u64)best_brp * 1000 * 1000 * 1000;
196 do_div(v64, priv->clock.freq);
198 bt->prop_seg = tseg1 / 2;
199 bt->phase_seg1 = tseg1 - bt->prop_seg;
200 bt->phase_seg2 = tseg2;
202 /* check for sjw user settings */
203 if (!bt->sjw || !btc->sjw_max) {
206 /* bt->sjw is at least 1 -> sanitize upper bound to sjw_max */
207 if (bt->sjw > btc->sjw_max)
208 bt->sjw = btc->sjw_max;
209 /* bt->sjw must not be higher than tseg2 */
217 bt->bitrate = priv->clock.freq / (bt->brp * (CAN_CALC_SYNC_SEG + tseg1 + tseg2));
221 #else /* !CONFIG_CAN_CALC_BITTIMING */
222 static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
223 const struct can_bittiming_const *btc)
225 netdev_err(dev, "bit-timing calculation not available\n");
228 #endif /* CONFIG_CAN_CALC_BITTIMING */
230 /* Checks the validity of the specified bit-timing parameters prop_seg,
231 * phase_seg1, phase_seg2 and sjw and tries to determine the bitrate
232 * prescaler value brp. You can find more information in the header
233 * file linux/can/netlink.h.
235 static int can_fixup_bittiming(struct net_device *dev, struct can_bittiming *bt,
236 const struct can_bittiming_const *btc)
238 struct can_priv *priv = netdev_priv(dev);
242 tseg1 = bt->prop_seg + bt->phase_seg1;
245 if (bt->sjw > btc->sjw_max ||
246 tseg1 < btc->tseg1_min || tseg1 > btc->tseg1_max ||
247 bt->phase_seg2 < btc->tseg2_min || bt->phase_seg2 > btc->tseg2_max)
250 brp64 = (u64)priv->clock.freq * (u64)bt->tq;
251 if (btc->brp_inc > 1)
252 do_div(brp64, btc->brp_inc);
253 brp64 += 500000000UL - 1;
254 do_div(brp64, 1000000000UL); /* the practicable BRP */
255 if (btc->brp_inc > 1)
256 brp64 *= btc->brp_inc;
257 bt->brp = (u32)brp64;
259 if (bt->brp < btc->brp_min || bt->brp > btc->brp_max)
262 alltseg = bt->prop_seg + bt->phase_seg1 + bt->phase_seg2 + 1;
263 bt->bitrate = priv->clock.freq / (bt->brp * alltseg);
264 bt->sample_point = ((tseg1 + 1) * 1000) / alltseg;
269 /* Checks the validity of predefined bitrate settings */
270 static int can_validate_bitrate(struct net_device *dev, struct can_bittiming *bt,
271 const u32 *bitrate_const,
272 const unsigned int bitrate_const_cnt)
274 struct can_priv *priv = netdev_priv(dev);
277 for (i = 0; i < bitrate_const_cnt; i++) {
278 if (bt->bitrate == bitrate_const[i])
282 if (i >= priv->bitrate_const_cnt)
288 static int can_get_bittiming(struct net_device *dev, struct can_bittiming *bt,
289 const struct can_bittiming_const *btc,
290 const u32 *bitrate_const,
291 const unsigned int bitrate_const_cnt)
295 /* Depending on the given can_bittiming parameter structure the CAN
296 * timing parameters are calculated based on the provided bitrate OR
297 * alternatively the CAN timing parameters (tq, prop_seg, etc.) are
298 * provided directly which are then checked and fixed up.
300 if (!bt->tq && bt->bitrate && btc)
301 err = can_calc_bittiming(dev, bt, btc);
302 else if (bt->tq && !bt->bitrate && btc)
303 err = can_fixup_bittiming(dev, bt, btc);
304 else if (!bt->tq && bt->bitrate && bitrate_const)
305 err = can_validate_bitrate(dev, bt, bitrate_const,
313 static void can_update_state_error_stats(struct net_device *dev,
314 enum can_state new_state)
316 struct can_priv *priv = netdev_priv(dev);
318 if (new_state <= priv->state)
322 case CAN_STATE_ERROR_WARNING:
323 priv->can_stats.error_warning++;
325 case CAN_STATE_ERROR_PASSIVE:
326 priv->can_stats.error_passive++;
328 case CAN_STATE_BUS_OFF:
329 priv->can_stats.bus_off++;
336 static int can_tx_state_to_frame(struct net_device *dev, enum can_state state)
339 case CAN_STATE_ERROR_ACTIVE:
340 return CAN_ERR_CRTL_ACTIVE;
341 case CAN_STATE_ERROR_WARNING:
342 return CAN_ERR_CRTL_TX_WARNING;
343 case CAN_STATE_ERROR_PASSIVE:
344 return CAN_ERR_CRTL_TX_PASSIVE;
350 static int can_rx_state_to_frame(struct net_device *dev, enum can_state state)
353 case CAN_STATE_ERROR_ACTIVE:
354 return CAN_ERR_CRTL_ACTIVE;
355 case CAN_STATE_ERROR_WARNING:
356 return CAN_ERR_CRTL_RX_WARNING;
357 case CAN_STATE_ERROR_PASSIVE:
358 return CAN_ERR_CRTL_RX_PASSIVE;
364 void can_change_state(struct net_device *dev, struct can_frame *cf,
365 enum can_state tx_state, enum can_state rx_state)
367 struct can_priv *priv = netdev_priv(dev);
368 enum can_state new_state = max(tx_state, rx_state);
370 if (unlikely(new_state == priv->state)) {
371 netdev_warn(dev, "%s: oops, state did not change", __func__);
375 netdev_dbg(dev, "New error state: %d\n", new_state);
377 can_update_state_error_stats(dev, new_state);
378 priv->state = new_state;
383 if (unlikely(new_state == CAN_STATE_BUS_OFF)) {
384 cf->can_id |= CAN_ERR_BUSOFF;
388 cf->can_id |= CAN_ERR_CRTL;
389 cf->data[1] |= tx_state >= rx_state ?
390 can_tx_state_to_frame(dev, tx_state) : 0;
391 cf->data[1] |= tx_state <= rx_state ?
392 can_rx_state_to_frame(dev, rx_state) : 0;
394 EXPORT_SYMBOL_GPL(can_change_state);
396 /* Local echo of CAN messages
398 * CAN network devices *should* support a local echo functionality
399 * (see Documentation/networking/can.rst). To test the handling of CAN
400 * interfaces that do not support the local echo both driver types are
401 * implemented. In the case that the driver does not support the echo
402 * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core
403 * to perform the echo as a fallback solution.
405 static void can_flush_echo_skb(struct net_device *dev)
407 struct can_priv *priv = netdev_priv(dev);
408 struct net_device_stats *stats = &dev->stats;
411 for (i = 0; i < priv->echo_skb_max; i++) {
412 if (priv->echo_skb[i]) {
413 kfree_skb(priv->echo_skb[i]);
414 priv->echo_skb[i] = NULL;
416 stats->tx_aborted_errors++;
421 /* Put the skb on the stack to be looped backed locally lateron
423 * The function is typically called in the start_xmit function
424 * of the device driver. The driver must protect access to
425 * priv->echo_skb, if necessary.
427 void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
430 struct can_priv *priv = netdev_priv(dev);
432 BUG_ON(idx >= priv->echo_skb_max);
434 /* check flag whether this packet has to be looped back */
435 if (!(dev->flags & IFF_ECHO) || skb->pkt_type != PACKET_LOOPBACK ||
436 (skb->protocol != htons(ETH_P_CAN) &&
437 skb->protocol != htons(ETH_P_CANFD))) {
442 if (!priv->echo_skb[idx]) {
444 skb = can_create_echo_skb(skb);
448 /* make settings for echo to reduce code in irq context */
449 skb->pkt_type = PACKET_BROADCAST;
450 skb->ip_summed = CHECKSUM_UNNECESSARY;
453 /* save this skb for tx interrupt echo handling */
454 priv->echo_skb[idx] = skb;
456 /* locking problem with netif_stop_queue() ?? */
457 netdev_err(dev, "%s: BUG! echo_skb is occupied!\n", __func__);
461 EXPORT_SYMBOL_GPL(can_put_echo_skb);
463 struct sk_buff *__can_get_echo_skb(struct net_device *dev, unsigned int idx, u8 *len_ptr)
465 struct can_priv *priv = netdev_priv(dev);
467 if (idx >= priv->echo_skb_max) {
468 netdev_err(dev, "%s: BUG! Trying to access can_priv::echo_skb out of bounds (%u/max %u)\n",
469 __func__, idx, priv->echo_skb_max);
473 if (priv->echo_skb[idx]) {
474 /* Using "struct canfd_frame::len" for the frame
475 * length is supported on both CAN and CANFD frames.
477 struct sk_buff *skb = priv->echo_skb[idx];
478 struct canfd_frame *cf = (struct canfd_frame *)skb->data;
482 priv->echo_skb[idx] = NULL;
490 /* Get the skb from the stack and loop it back locally
492 * The function is typically called when the TX done interrupt
493 * is handled in the device driver. The driver must protect
494 * access to priv->echo_skb, if necessary.
496 unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx)
501 skb = __can_get_echo_skb(dev, idx, &len);
509 EXPORT_SYMBOL_GPL(can_get_echo_skb);
511 /* Remove the skb from the stack and free it.
513 * The function is typically called when TX failed.
515 void can_free_echo_skb(struct net_device *dev, unsigned int idx)
517 struct can_priv *priv = netdev_priv(dev);
519 BUG_ON(idx >= priv->echo_skb_max);
521 if (priv->echo_skb[idx]) {
522 dev_kfree_skb_any(priv->echo_skb[idx]);
523 priv->echo_skb[idx] = NULL;
526 EXPORT_SYMBOL_GPL(can_free_echo_skb);
528 /* CAN device restart for bus-off recovery */
529 static void can_restart(struct net_device *dev)
531 struct can_priv *priv = netdev_priv(dev);
532 struct net_device_stats *stats = &dev->stats;
534 struct can_frame *cf;
537 BUG_ON(netif_carrier_ok(dev));
539 /* No synchronization needed because the device is bus-off and
540 * no messages can come in or go out.
542 can_flush_echo_skb(dev);
544 /* send restart message upstream */
545 skb = alloc_can_err_skb(dev, &cf);
550 cf->can_id |= CAN_ERR_RESTARTED;
555 stats->rx_bytes += cf->can_dlc;
558 netdev_dbg(dev, "restarted\n");
559 priv->can_stats.restarts++;
561 /* Now restart the device */
562 err = priv->do_set_mode(dev, CAN_MODE_START);
564 netif_carrier_on(dev);
566 netdev_err(dev, "Error %d during restart", err);
569 static void can_restart_work(struct work_struct *work)
571 struct delayed_work *dwork = to_delayed_work(work);
572 struct can_priv *priv = container_of(dwork, struct can_priv, restart_work);
574 can_restart(priv->dev);
577 int can_restart_now(struct net_device *dev)
579 struct can_priv *priv = netdev_priv(dev);
581 /* A manual restart is only permitted if automatic restart is
582 * disabled and the device is in the bus-off state
584 if (priv->restart_ms)
586 if (priv->state != CAN_STATE_BUS_OFF)
589 cancel_delayed_work_sync(&priv->restart_work);
597 * This functions should be called when the device goes bus-off to
598 * tell the netif layer that no more packets can be sent or received.
599 * If enabled, a timer is started to trigger bus-off recovery.
601 void can_bus_off(struct net_device *dev)
603 struct can_priv *priv = netdev_priv(dev);
605 netdev_info(dev, "bus-off\n");
607 netif_carrier_off(dev);
609 if (priv->restart_ms)
610 schedule_delayed_work(&priv->restart_work,
611 msecs_to_jiffies(priv->restart_ms));
613 EXPORT_SYMBOL_GPL(can_bus_off);
615 static void can_setup(struct net_device *dev)
617 dev->type = ARPHRD_CAN;
619 dev->hard_header_len = 0;
621 dev->tx_queue_len = 10;
623 /* New-style flags. */
624 dev->flags = IFF_NOARP;
625 dev->features = NETIF_F_HW_CSUM;
628 struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf)
632 skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
633 sizeof(struct can_frame));
637 skb->protocol = htons(ETH_P_CAN);
638 skb->pkt_type = PACKET_BROADCAST;
639 skb->ip_summed = CHECKSUM_UNNECESSARY;
641 skb_reset_mac_header(skb);
642 skb_reset_network_header(skb);
643 skb_reset_transport_header(skb);
645 can_skb_reserve(skb);
646 can_skb_prv(skb)->ifindex = dev->ifindex;
647 can_skb_prv(skb)->skbcnt = 0;
649 *cf = skb_put_zero(skb, sizeof(struct can_frame));
653 EXPORT_SYMBOL_GPL(alloc_can_skb);
655 struct sk_buff *alloc_canfd_skb(struct net_device *dev,
656 struct canfd_frame **cfd)
660 skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
661 sizeof(struct canfd_frame));
665 skb->protocol = htons(ETH_P_CANFD);
666 skb->pkt_type = PACKET_BROADCAST;
667 skb->ip_summed = CHECKSUM_UNNECESSARY;
669 skb_reset_mac_header(skb);
670 skb_reset_network_header(skb);
671 skb_reset_transport_header(skb);
673 can_skb_reserve(skb);
674 can_skb_prv(skb)->ifindex = dev->ifindex;
675 can_skb_prv(skb)->skbcnt = 0;
677 *cfd = skb_put_zero(skb, sizeof(struct canfd_frame));
681 EXPORT_SYMBOL_GPL(alloc_canfd_skb);
683 struct sk_buff *alloc_can_err_skb(struct net_device *dev, struct can_frame **cf)
687 skb = alloc_can_skb(dev, cf);
691 (*cf)->can_id = CAN_ERR_FLAG;
692 (*cf)->can_dlc = CAN_ERR_DLC;
696 EXPORT_SYMBOL_GPL(alloc_can_err_skb);
698 /* Allocate and setup space for the CAN network device */
699 struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max,
700 unsigned int txqs, unsigned int rxqs)
702 struct net_device *dev;
703 struct can_priv *priv;
707 size = ALIGN(sizeof_priv, sizeof(struct sk_buff *)) +
708 echo_skb_max * sizeof(struct sk_buff *);
712 dev = alloc_netdev_mqs(size, "can%d", NET_NAME_UNKNOWN, can_setup,
717 priv = netdev_priv(dev);
721 priv->echo_skb_max = echo_skb_max;
722 priv->echo_skb = (void *)priv +
723 ALIGN(sizeof_priv, sizeof(struct sk_buff *));
726 priv->state = CAN_STATE_STOPPED;
728 INIT_DELAYED_WORK(&priv->restart_work, can_restart_work);
732 EXPORT_SYMBOL_GPL(alloc_candev_mqs);
734 /* Free space of the CAN network device */
735 void free_candev(struct net_device *dev)
739 EXPORT_SYMBOL_GPL(free_candev);
741 /* changing MTU and control mode for CAN/CANFD devices */
742 int can_change_mtu(struct net_device *dev, int new_mtu)
744 struct can_priv *priv = netdev_priv(dev);
746 /* Do not allow changing the MTU while running */
747 if (dev->flags & IFF_UP)
750 /* allow change of MTU according to the CANFD ability of the device */
753 /* 'CANFD-only' controllers can not switch to CAN_MTU */
754 if (priv->ctrlmode_static & CAN_CTRLMODE_FD)
757 priv->ctrlmode &= ~CAN_CTRLMODE_FD;
761 /* check for potential CANFD ability */
762 if (!(priv->ctrlmode_supported & CAN_CTRLMODE_FD) &&
763 !(priv->ctrlmode_static & CAN_CTRLMODE_FD))
766 priv->ctrlmode |= CAN_CTRLMODE_FD;
776 EXPORT_SYMBOL_GPL(can_change_mtu);
778 /* Common open function when the device gets opened.
780 * This function should be called in the open function of the device
783 int open_candev(struct net_device *dev)
785 struct can_priv *priv = netdev_priv(dev);
787 if (!priv->bittiming.bitrate) {
788 netdev_err(dev, "bit-timing not yet defined\n");
792 /* For CAN FD the data bitrate has to be >= the arbitration bitrate */
793 if ((priv->ctrlmode & CAN_CTRLMODE_FD) &&
794 (!priv->data_bittiming.bitrate ||
795 (priv->data_bittiming.bitrate < priv->bittiming.bitrate))) {
796 netdev_err(dev, "incorrect/missing data bit-timing\n");
800 /* Switch carrier on if device was stopped while in bus-off state */
801 if (!netif_carrier_ok(dev))
802 netif_carrier_on(dev);
806 EXPORT_SYMBOL_GPL(open_candev);
809 /* Common function that can be used to understand the limitation of
810 * a transceiver when it provides no means to determine these limitations
813 void of_can_transceiver(struct net_device *dev)
815 struct device_node *dn;
816 struct can_priv *priv = netdev_priv(dev);
817 struct device_node *np = dev->dev.parent->of_node;
820 dn = of_get_child_by_name(np, "can-transceiver");
824 ret = of_property_read_u32(dn, "max-bitrate", &priv->bitrate_max);
825 if ((ret && ret != -EINVAL) || (!ret && !priv->bitrate_max))
826 netdev_warn(dev, "Invalid value for transceiver max bitrate. Ignoring bitrate limit.\n");
828 EXPORT_SYMBOL_GPL(of_can_transceiver);
831 /* Common close function for cleanup before the device gets closed.
833 * This function should be called in the close function of the device
836 void close_candev(struct net_device *dev)
838 struct can_priv *priv = netdev_priv(dev);
840 cancel_delayed_work_sync(&priv->restart_work);
841 can_flush_echo_skb(dev);
843 EXPORT_SYMBOL_GPL(close_candev);
845 /* CAN netlink interface */
846 static const struct nla_policy can_policy[IFLA_CAN_MAX + 1] = {
847 [IFLA_CAN_STATE] = { .type = NLA_U32 },
848 [IFLA_CAN_CTRLMODE] = { .len = sizeof(struct can_ctrlmode) },
849 [IFLA_CAN_RESTART_MS] = { .type = NLA_U32 },
850 [IFLA_CAN_RESTART] = { .type = NLA_U32 },
851 [IFLA_CAN_BITTIMING] = { .len = sizeof(struct can_bittiming) },
852 [IFLA_CAN_BITTIMING_CONST]
853 = { .len = sizeof(struct can_bittiming_const) },
854 [IFLA_CAN_CLOCK] = { .len = sizeof(struct can_clock) },
855 [IFLA_CAN_BERR_COUNTER] = { .len = sizeof(struct can_berr_counter) },
856 [IFLA_CAN_DATA_BITTIMING]
857 = { .len = sizeof(struct can_bittiming) },
858 [IFLA_CAN_DATA_BITTIMING_CONST]
859 = { .len = sizeof(struct can_bittiming_const) },
862 static int can_validate(struct nlattr *tb[], struct nlattr *data[],
863 struct netlink_ext_ack *extack)
865 bool is_can_fd = false;
867 /* Make sure that valid CAN FD configurations always consist of
868 * - nominal/arbitration bittiming
870 * - control mode with CAN_CTRLMODE_FD set
876 if (data[IFLA_CAN_CTRLMODE]) {
877 struct can_ctrlmode *cm = nla_data(data[IFLA_CAN_CTRLMODE]);
879 is_can_fd = cm->flags & cm->mask & CAN_CTRLMODE_FD;
883 if (!data[IFLA_CAN_BITTIMING] || !data[IFLA_CAN_DATA_BITTIMING])
887 if (data[IFLA_CAN_DATA_BITTIMING]) {
888 if (!is_can_fd || !data[IFLA_CAN_BITTIMING])
895 static int can_changelink(struct net_device *dev, struct nlattr *tb[],
896 struct nlattr *data[],
897 struct netlink_ext_ack *extack)
899 struct can_priv *priv = netdev_priv(dev);
902 /* We need synchronization with dev->stop() */
905 if (data[IFLA_CAN_BITTIMING]) {
906 struct can_bittiming bt;
908 /* Do not allow changing bittiming while running */
909 if (dev->flags & IFF_UP)
912 /* Calculate bittiming parameters based on
913 * bittiming_const if set, otherwise pass bitrate
914 * directly via do_set_bitrate(). Bail out if neither
917 if (!priv->bittiming_const && !priv->do_set_bittiming)
920 memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt));
921 err = can_get_bittiming(dev, &bt,
922 priv->bittiming_const,
924 priv->bitrate_const_cnt);
928 if (priv->bitrate_max && bt.bitrate > priv->bitrate_max) {
929 netdev_err(dev, "arbitration bitrate surpasses transceiver capabilities of %d bps\n",
934 memcpy(&priv->bittiming, &bt, sizeof(bt));
936 if (priv->do_set_bittiming) {
937 /* Finally, set the bit-timing registers */
938 err = priv->do_set_bittiming(dev);
944 if (data[IFLA_CAN_CTRLMODE]) {
945 struct can_ctrlmode *cm;
949 /* Do not allow changing controller mode while running */
950 if (dev->flags & IFF_UP)
952 cm = nla_data(data[IFLA_CAN_CTRLMODE]);
953 ctrlstatic = priv->ctrlmode_static;
954 maskedflags = cm->flags & cm->mask;
956 /* check whether provided bits are allowed to be passed */
957 if (cm->mask & ~(priv->ctrlmode_supported | ctrlstatic))
960 /* do not check for static fd-non-iso if 'fd' is disabled */
961 if (!(maskedflags & CAN_CTRLMODE_FD))
962 ctrlstatic &= ~CAN_CTRLMODE_FD_NON_ISO;
964 /* make sure static options are provided by configuration */
965 if ((maskedflags & ctrlstatic) != ctrlstatic)
968 /* clear bits to be modified and copy the flag values */
969 priv->ctrlmode &= ~cm->mask;
970 priv->ctrlmode |= maskedflags;
972 /* CAN_CTRLMODE_FD can only be set when driver supports FD */
973 if (priv->ctrlmode & CAN_CTRLMODE_FD)
974 dev->mtu = CANFD_MTU;
979 if (data[IFLA_CAN_RESTART_MS]) {
980 /* Do not allow changing restart delay while running */
981 if (dev->flags & IFF_UP)
983 priv->restart_ms = nla_get_u32(data[IFLA_CAN_RESTART_MS]);
986 if (data[IFLA_CAN_RESTART]) {
987 /* Do not allow a restart while not running */
988 if (!(dev->flags & IFF_UP))
990 err = can_restart_now(dev);
995 if (data[IFLA_CAN_DATA_BITTIMING]) {
996 struct can_bittiming dbt;
998 /* Do not allow changing bittiming while running */
999 if (dev->flags & IFF_UP)
1002 /* Calculate bittiming parameters based on
1003 * data_bittiming_const if set, otherwise pass bitrate
1004 * directly via do_set_bitrate(). Bail out if neither
1007 if (!priv->data_bittiming_const && !priv->do_set_data_bittiming)
1010 memcpy(&dbt, nla_data(data[IFLA_CAN_DATA_BITTIMING]),
1012 err = can_get_bittiming(dev, &dbt,
1013 priv->data_bittiming_const,
1014 priv->data_bitrate_const,
1015 priv->data_bitrate_const_cnt);
1019 if (priv->bitrate_max && dbt.bitrate > priv->bitrate_max) {
1020 netdev_err(dev, "canfd data bitrate surpasses transceiver capabilities of %d bps\n",
1025 memcpy(&priv->data_bittiming, &dbt, sizeof(dbt));
1027 if (priv->do_set_data_bittiming) {
1028 /* Finally, set the bit-timing registers */
1029 err = priv->do_set_data_bittiming(dev);
1035 if (data[IFLA_CAN_TERMINATION]) {
1036 const u16 termval = nla_get_u16(data[IFLA_CAN_TERMINATION]);
1037 const unsigned int num_term = priv->termination_const_cnt;
1040 if (!priv->do_set_termination)
1043 /* check whether given value is supported by the interface */
1044 for (i = 0; i < num_term; i++) {
1045 if (termval == priv->termination_const[i])
1051 /* Finally, set the termination value */
1052 err = priv->do_set_termination(dev, termval);
1056 priv->termination = termval;
1062 static size_t can_get_size(const struct net_device *dev)
1064 struct can_priv *priv = netdev_priv(dev);
1067 if (priv->bittiming.bitrate) /* IFLA_CAN_BITTIMING */
1068 size += nla_total_size(sizeof(struct can_bittiming));
1069 if (priv->bittiming_const) /* IFLA_CAN_BITTIMING_CONST */
1070 size += nla_total_size(sizeof(struct can_bittiming_const));
1071 size += nla_total_size(sizeof(struct can_clock)); /* IFLA_CAN_CLOCK */
1072 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_STATE */
1073 size += nla_total_size(sizeof(struct can_ctrlmode)); /* IFLA_CAN_CTRLMODE */
1074 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_RESTART_MS */
1075 if (priv->do_get_berr_counter) /* IFLA_CAN_BERR_COUNTER */
1076 size += nla_total_size(sizeof(struct can_berr_counter));
1077 if (priv->data_bittiming.bitrate) /* IFLA_CAN_DATA_BITTIMING */
1078 size += nla_total_size(sizeof(struct can_bittiming));
1079 if (priv->data_bittiming_const) /* IFLA_CAN_DATA_BITTIMING_CONST */
1080 size += nla_total_size(sizeof(struct can_bittiming_const));
1081 if (priv->termination_const) {
1082 size += nla_total_size(sizeof(priv->termination)); /* IFLA_CAN_TERMINATION */
1083 size += nla_total_size(sizeof(*priv->termination_const) * /* IFLA_CAN_TERMINATION_CONST */
1084 priv->termination_const_cnt);
1086 if (priv->bitrate_const) /* IFLA_CAN_BITRATE_CONST */
1087 size += nla_total_size(sizeof(*priv->bitrate_const) *
1088 priv->bitrate_const_cnt);
1089 if (priv->data_bitrate_const) /* IFLA_CAN_DATA_BITRATE_CONST */
1090 size += nla_total_size(sizeof(*priv->data_bitrate_const) *
1091 priv->data_bitrate_const_cnt);
1092 size += sizeof(priv->bitrate_max); /* IFLA_CAN_BITRATE_MAX */
1097 static int can_fill_info(struct sk_buff *skb, const struct net_device *dev)
1099 struct can_priv *priv = netdev_priv(dev);
1100 struct can_ctrlmode cm = {.flags = priv->ctrlmode};
1101 struct can_berr_counter bec;
1102 enum can_state state = priv->state;
1104 if (priv->do_get_state)
1105 priv->do_get_state(dev, &state);
1107 if ((priv->bittiming.bitrate &&
1108 nla_put(skb, IFLA_CAN_BITTIMING,
1109 sizeof(priv->bittiming), &priv->bittiming)) ||
1111 (priv->bittiming_const &&
1112 nla_put(skb, IFLA_CAN_BITTIMING_CONST,
1113 sizeof(*priv->bittiming_const), priv->bittiming_const)) ||
1115 nla_put(skb, IFLA_CAN_CLOCK, sizeof(priv->clock), &priv->clock) ||
1116 nla_put_u32(skb, IFLA_CAN_STATE, state) ||
1117 nla_put(skb, IFLA_CAN_CTRLMODE, sizeof(cm), &cm) ||
1118 nla_put_u32(skb, IFLA_CAN_RESTART_MS, priv->restart_ms) ||
1120 (priv->do_get_berr_counter &&
1121 !priv->do_get_berr_counter(dev, &bec) &&
1122 nla_put(skb, IFLA_CAN_BERR_COUNTER, sizeof(bec), &bec)) ||
1124 (priv->data_bittiming.bitrate &&
1125 nla_put(skb, IFLA_CAN_DATA_BITTIMING,
1126 sizeof(priv->data_bittiming), &priv->data_bittiming)) ||
1128 (priv->data_bittiming_const &&
1129 nla_put(skb, IFLA_CAN_DATA_BITTIMING_CONST,
1130 sizeof(*priv->data_bittiming_const),
1131 priv->data_bittiming_const)) ||
1133 (priv->termination_const &&
1134 (nla_put_u16(skb, IFLA_CAN_TERMINATION, priv->termination) ||
1135 nla_put(skb, IFLA_CAN_TERMINATION_CONST,
1136 sizeof(*priv->termination_const) *
1137 priv->termination_const_cnt,
1138 priv->termination_const))) ||
1140 (priv->bitrate_const &&
1141 nla_put(skb, IFLA_CAN_BITRATE_CONST,
1142 sizeof(*priv->bitrate_const) *
1143 priv->bitrate_const_cnt,
1144 priv->bitrate_const)) ||
1146 (priv->data_bitrate_const &&
1147 nla_put(skb, IFLA_CAN_DATA_BITRATE_CONST,
1148 sizeof(*priv->data_bitrate_const) *
1149 priv->data_bitrate_const_cnt,
1150 priv->data_bitrate_const)) ||
1152 (nla_put(skb, IFLA_CAN_BITRATE_MAX,
1153 sizeof(priv->bitrate_max),
1154 &priv->bitrate_max))
1162 static size_t can_get_xstats_size(const struct net_device *dev)
1164 return sizeof(struct can_device_stats);
1167 static int can_fill_xstats(struct sk_buff *skb, const struct net_device *dev)
1169 struct can_priv *priv = netdev_priv(dev);
1171 if (nla_put(skb, IFLA_INFO_XSTATS,
1172 sizeof(priv->can_stats), &priv->can_stats))
1173 goto nla_put_failure;
1180 static int can_newlink(struct net *src_net, struct net_device *dev,
1181 struct nlattr *tb[], struct nlattr *data[],
1182 struct netlink_ext_ack *extack)
1187 static void can_dellink(struct net_device *dev, struct list_head *head)
1192 static struct rtnl_link_ops can_link_ops __read_mostly = {
1194 .maxtype = IFLA_CAN_MAX,
1195 .policy = can_policy,
1197 .validate = can_validate,
1198 .newlink = can_newlink,
1199 .changelink = can_changelink,
1200 .dellink = can_dellink,
1201 .get_size = can_get_size,
1202 .fill_info = can_fill_info,
1203 .get_xstats_size = can_get_xstats_size,
1204 .fill_xstats = can_fill_xstats,
1207 /* Register the CAN network device */
1208 int register_candev(struct net_device *dev)
1210 struct can_priv *priv = netdev_priv(dev);
1212 /* Ensure termination_const, termination_const_cnt and
1213 * do_set_termination consistency. All must be either set or
1216 if ((!priv->termination_const != !priv->termination_const_cnt) ||
1217 (!priv->termination_const != !priv->do_set_termination))
1220 if (!priv->bitrate_const != !priv->bitrate_const_cnt)
1223 if (!priv->data_bitrate_const != !priv->data_bitrate_const_cnt)
1226 dev->rtnl_link_ops = &can_link_ops;
1227 netif_carrier_off(dev);
1229 return register_netdev(dev);
1231 EXPORT_SYMBOL_GPL(register_candev);
1233 /* Unregister the CAN network device */
1234 void unregister_candev(struct net_device *dev)
1236 unregister_netdev(dev);
1238 EXPORT_SYMBOL_GPL(unregister_candev);
1240 /* Test if a network device is a candev based device
1241 * and return the can_priv* if so.
1243 struct can_priv *safe_candev_priv(struct net_device *dev)
1245 if ((dev->type != ARPHRD_CAN) || (dev->rtnl_link_ops != &can_link_ops))
1248 return netdev_priv(dev);
1250 EXPORT_SYMBOL_GPL(safe_candev_priv);
1252 static __init int can_dev_init(void)
1256 can_led_notifier_init();
1258 err = rtnl_link_register(&can_link_ops);
1260 printk(KERN_INFO MOD_DESC "\n");
1264 module_init(can_dev_init);
1266 static __exit void can_dev_exit(void)
1268 rtnl_link_unregister(&can_link_ops);
1270 can_led_notifier_exit();
1272 module_exit(can_dev_exit);
1274 MODULE_ALIAS_RTNL_LINK("can");