diff options
Diffstat (limited to 'drivers/net/can/dev.c')
-rw-r--r-- | drivers/net/can/dev.c | 183 |
1 files changed, 148 insertions, 35 deletions
diff --git a/drivers/net/can/dev.c b/drivers/net/can/dev.c index fc59bc6f040b..c7a260478749 100644 --- a/drivers/net/can/dev.c +++ b/drivers/net/can/dev.c | |||
@@ -99,10 +99,10 @@ static int can_update_spt(const struct can_bittiming_const *btc, | |||
99 | return 1000 * (tseg + 1 - *tseg2) / (tseg + 1); | 99 | return 1000 * (tseg + 1 - *tseg2) / (tseg + 1); |
100 | } | 100 | } |
101 | 101 | ||
102 | static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt) | 102 | static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt, |
103 | const struct can_bittiming_const *btc) | ||
103 | { | 104 | { |
104 | struct can_priv *priv = netdev_priv(dev); | 105 | struct can_priv *priv = netdev_priv(dev); |
105 | const struct can_bittiming_const *btc = priv->bittiming_const; | ||
106 | long rate, best_rate = 0; | 106 | long rate, best_rate = 0; |
107 | long best_error = 1000000000, error = 0; | 107 | long best_error = 1000000000, error = 0; |
108 | int best_tseg = 0, best_brp = 0, brp = 0; | 108 | int best_tseg = 0, best_brp = 0, brp = 0; |
@@ -110,9 +110,6 @@ static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt) | |||
110 | int spt_error = 1000, spt = 0, sampl_pt; | 110 | int spt_error = 1000, spt = 0, sampl_pt; |
111 | u64 v64; | 111 | u64 v64; |
112 | 112 | ||
113 | if (!priv->bittiming_const) | ||
114 | return -ENOTSUPP; | ||
115 | |||
116 | /* Use CIA recommended sample points */ | 113 | /* Use CIA recommended sample points */ |
117 | if (bt->sample_point) { | 114 | if (bt->sample_point) { |
118 | sampl_pt = bt->sample_point; | 115 | sampl_pt = bt->sample_point; |
@@ -204,7 +201,8 @@ static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt) | |||
204 | return 0; | 201 | return 0; |
205 | } | 202 | } |
206 | #else /* !CONFIG_CAN_CALC_BITTIMING */ | 203 | #else /* !CONFIG_CAN_CALC_BITTIMING */ |
207 | static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt) | 204 | static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt, |
205 | const struct can_bittiming_const *btc) | ||
208 | { | 206 | { |
209 | netdev_err(dev, "bit-timing calculation not available\n"); | 207 | netdev_err(dev, "bit-timing calculation not available\n"); |
210 | return -EINVAL; | 208 | return -EINVAL; |
@@ -217,16 +215,13 @@ static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt) | |||
217 | * prescaler value brp. You can find more information in the header | 215 | * prescaler value brp. You can find more information in the header |
218 | * file linux/can/netlink.h. | 216 | * file linux/can/netlink.h. |
219 | */ | 217 | */ |
220 | static int can_fixup_bittiming(struct net_device *dev, struct can_bittiming *bt) | 218 | static int can_fixup_bittiming(struct net_device *dev, struct can_bittiming *bt, |
219 | const struct can_bittiming_const *btc) | ||
221 | { | 220 | { |
222 | struct can_priv *priv = netdev_priv(dev); | 221 | struct can_priv *priv = netdev_priv(dev); |
223 | const struct can_bittiming_const *btc = priv->bittiming_const; | ||
224 | int tseg1, alltseg; | 222 | int tseg1, alltseg; |
225 | u64 brp64; | 223 | u64 brp64; |
226 | 224 | ||
227 | if (!priv->bittiming_const) | ||
228 | return -ENOTSUPP; | ||
229 | |||
230 | tseg1 = bt->prop_seg + bt->phase_seg1; | 225 | tseg1 = bt->prop_seg + bt->phase_seg1; |
231 | if (!bt->sjw) | 226 | if (!bt->sjw) |
232 | bt->sjw = 1; | 227 | bt->sjw = 1; |
@@ -254,26 +249,29 @@ static int can_fixup_bittiming(struct net_device *dev, struct can_bittiming *bt) | |||
254 | return 0; | 249 | return 0; |
255 | } | 250 | } |
256 | 251 | ||
257 | static int can_get_bittiming(struct net_device *dev, struct can_bittiming *bt) | 252 | static int can_get_bittiming(struct net_device *dev, struct can_bittiming *bt, |
253 | const struct can_bittiming_const *btc) | ||
258 | { | 254 | { |
259 | struct can_priv *priv = netdev_priv(dev); | ||
260 | int err; | 255 | int err; |
261 | 256 | ||
262 | /* Check if the CAN device has bit-timing parameters */ | 257 | /* Check if the CAN device has bit-timing parameters */ |
263 | if (priv->bittiming_const) { | 258 | if (!btc) |
259 | return -ENOTSUPP; | ||
264 | 260 | ||
265 | /* Non-expert mode? Check if the bitrate has been pre-defined */ | 261 | /* |
266 | if (!bt->tq) | 262 | * Depending on the given can_bittiming parameter structure the CAN |
267 | /* Determine bit-timing parameters */ | 263 | * timing parameters are calculated based on the provided bitrate OR |
268 | err = can_calc_bittiming(dev, bt); | 264 | * alternatively the CAN timing parameters (tq, prop_seg, etc.) are |
269 | else | 265 | * provided directly which are then checked and fixed up. |
270 | /* Check bit-timing params and calculate proper brp */ | 266 | */ |
271 | err = can_fixup_bittiming(dev, bt); | 267 | if (!bt->tq && bt->bitrate) |
272 | if (err) | 268 | err = can_calc_bittiming(dev, bt, btc); |
273 | return err; | 269 | else if (bt->tq && !bt->bitrate) |
274 | } | 270 | err = can_fixup_bittiming(dev, bt, btc); |
271 | else | ||
272 | err = -EINVAL; | ||
275 | 273 | ||
276 | return 0; | 274 | return err; |
277 | } | 275 | } |
278 | 276 | ||
279 | /* | 277 | /* |
@@ -317,7 +315,9 @@ void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev, | |||
317 | BUG_ON(idx >= priv->echo_skb_max); | 315 | BUG_ON(idx >= priv->echo_skb_max); |
318 | 316 | ||
319 | /* check flag whether this packet has to be looped back */ | 317 | /* check flag whether this packet has to be looped back */ |
320 | if (!(dev->flags & IFF_ECHO) || skb->pkt_type != PACKET_LOOPBACK) { | 318 | if (!(dev->flags & IFF_ECHO) || skb->pkt_type != PACKET_LOOPBACK || |
319 | (skb->protocol != htons(ETH_P_CAN) && | ||
320 | skb->protocol != htons(ETH_P_CANFD))) { | ||
321 | kfree_skb(skb); | 321 | kfree_skb(skb); |
322 | return; | 322 | return; |
323 | } | 323 | } |
@@ -329,7 +329,6 @@ void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev, | |||
329 | return; | 329 | return; |
330 | 330 | ||
331 | /* make settings for echo to reduce code in irq context */ | 331 | /* make settings for echo to reduce code in irq context */ |
332 | skb->protocol = htons(ETH_P_CAN); | ||
333 | skb->pkt_type = PACKET_BROADCAST; | 332 | skb->pkt_type = PACKET_BROADCAST; |
334 | skb->ip_summed = CHECKSUM_UNNECESSARY; | 333 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
335 | skb->dev = dev; | 334 | skb->dev = dev; |
@@ -512,6 +511,30 @@ struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf) | |||
512 | } | 511 | } |
513 | EXPORT_SYMBOL_GPL(alloc_can_skb); | 512 | EXPORT_SYMBOL_GPL(alloc_can_skb); |
514 | 513 | ||
514 | struct sk_buff *alloc_canfd_skb(struct net_device *dev, | ||
515 | struct canfd_frame **cfd) | ||
516 | { | ||
517 | struct sk_buff *skb; | ||
518 | |||
519 | skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) + | ||
520 | sizeof(struct canfd_frame)); | ||
521 | if (unlikely(!skb)) | ||
522 | return NULL; | ||
523 | |||
524 | skb->protocol = htons(ETH_P_CANFD); | ||
525 | skb->pkt_type = PACKET_BROADCAST; | ||
526 | skb->ip_summed = CHECKSUM_UNNECESSARY; | ||
527 | |||
528 | can_skb_reserve(skb); | ||
529 | can_skb_prv(skb)->ifindex = dev->ifindex; | ||
530 | |||
531 | *cfd = (struct canfd_frame *)skb_put(skb, sizeof(struct canfd_frame)); | ||
532 | memset(*cfd, 0, sizeof(struct canfd_frame)); | ||
533 | |||
534 | return skb; | ||
535 | } | ||
536 | EXPORT_SYMBOL_GPL(alloc_canfd_skb); | ||
537 | |||
515 | struct sk_buff *alloc_can_err_skb(struct net_device *dev, struct can_frame **cf) | 538 | struct sk_buff *alloc_can_err_skb(struct net_device *dev, struct can_frame **cf) |
516 | { | 539 | { |
517 | struct sk_buff *skb; | 540 | struct sk_buff *skb; |
@@ -572,6 +595,39 @@ void free_candev(struct net_device *dev) | |||
572 | EXPORT_SYMBOL_GPL(free_candev); | 595 | EXPORT_SYMBOL_GPL(free_candev); |
573 | 596 | ||
574 | /* | 597 | /* |
598 | * changing MTU and control mode for CAN/CANFD devices | ||
599 | */ | ||
600 | int can_change_mtu(struct net_device *dev, int new_mtu) | ||
601 | { | ||
602 | struct can_priv *priv = netdev_priv(dev); | ||
603 | |||
604 | /* Do not allow changing the MTU while running */ | ||
605 | if (dev->flags & IFF_UP) | ||
606 | return -EBUSY; | ||
607 | |||
608 | /* allow change of MTU according to the CANFD ability of the device */ | ||
609 | switch (new_mtu) { | ||
610 | case CAN_MTU: | ||
611 | priv->ctrlmode &= ~CAN_CTRLMODE_FD; | ||
612 | break; | ||
613 | |||
614 | case CANFD_MTU: | ||
615 | if (!(priv->ctrlmode_supported & CAN_CTRLMODE_FD)) | ||
616 | return -EINVAL; | ||
617 | |||
618 | priv->ctrlmode |= CAN_CTRLMODE_FD; | ||
619 | break; | ||
620 | |||
621 | default: | ||
622 | return -EINVAL; | ||
623 | } | ||
624 | |||
625 | dev->mtu = new_mtu; | ||
626 | return 0; | ||
627 | } | ||
628 | EXPORT_SYMBOL_GPL(can_change_mtu); | ||
629 | |||
630 | /* | ||
575 | * Common open function when the device gets opened. | 631 | * Common open function when the device gets opened. |
576 | * | 632 | * |
577 | * This function should be called in the open function of the device | 633 | * This function should be called in the open function of the device |
@@ -581,11 +637,19 @@ int open_candev(struct net_device *dev) | |||
581 | { | 637 | { |
582 | struct can_priv *priv = netdev_priv(dev); | 638 | struct can_priv *priv = netdev_priv(dev); |
583 | 639 | ||
584 | if (!priv->bittiming.tq && !priv->bittiming.bitrate) { | 640 | if (!priv->bittiming.bitrate) { |
585 | netdev_err(dev, "bit-timing not yet defined\n"); | 641 | netdev_err(dev, "bit-timing not yet defined\n"); |
586 | return -EINVAL; | 642 | return -EINVAL; |
587 | } | 643 | } |
588 | 644 | ||
645 | /* For CAN FD the data bitrate has to be >= the arbitration bitrate */ | ||
646 | if ((priv->ctrlmode & CAN_CTRLMODE_FD) && | ||
647 | (!priv->data_bittiming.bitrate || | ||
648 | (priv->data_bittiming.bitrate < priv->bittiming.bitrate))) { | ||
649 | netdev_err(dev, "incorrect/missing data bit-timing\n"); | ||
650 | return -EINVAL; | ||
651 | } | ||
652 | |||
589 | /* Switch carrier on if device was stopped while in bus-off state */ | 653 | /* Switch carrier on if device was stopped while in bus-off state */ |
590 | if (!netif_carrier_ok(dev)) | 654 | if (!netif_carrier_ok(dev)) |
591 | netif_carrier_on(dev); | 655 | netif_carrier_on(dev); |
@@ -624,6 +688,10 @@ static const struct nla_policy can_policy[IFLA_CAN_MAX + 1] = { | |||
624 | = { .len = sizeof(struct can_bittiming_const) }, | 688 | = { .len = sizeof(struct can_bittiming_const) }, |
625 | [IFLA_CAN_CLOCK] = { .len = sizeof(struct can_clock) }, | 689 | [IFLA_CAN_CLOCK] = { .len = sizeof(struct can_clock) }, |
626 | [IFLA_CAN_BERR_COUNTER] = { .len = sizeof(struct can_berr_counter) }, | 690 | [IFLA_CAN_BERR_COUNTER] = { .len = sizeof(struct can_berr_counter) }, |
691 | [IFLA_CAN_DATA_BITTIMING] | ||
692 | = { .len = sizeof(struct can_bittiming) }, | ||
693 | [IFLA_CAN_DATA_BITTIMING_CONST] | ||
694 | = { .len = sizeof(struct can_bittiming_const) }, | ||
627 | }; | 695 | }; |
628 | 696 | ||
629 | static int can_changelink(struct net_device *dev, | 697 | static int can_changelink(struct net_device *dev, |
@@ -642,9 +710,7 @@ static int can_changelink(struct net_device *dev, | |||
642 | if (dev->flags & IFF_UP) | 710 | if (dev->flags & IFF_UP) |
643 | return -EBUSY; | 711 | return -EBUSY; |
644 | memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt)); | 712 | memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt)); |
645 | if ((!bt.bitrate && !bt.tq) || (bt.bitrate && bt.tq)) | 713 | err = can_get_bittiming(dev, &bt, priv->bittiming_const); |
646 | return -EINVAL; | ||
647 | err = can_get_bittiming(dev, &bt); | ||
648 | if (err) | 714 | if (err) |
649 | return err; | 715 | return err; |
650 | memcpy(&priv->bittiming, &bt, sizeof(bt)); | 716 | memcpy(&priv->bittiming, &bt, sizeof(bt)); |
@@ -668,6 +734,12 @@ static int can_changelink(struct net_device *dev, | |||
668 | return -EOPNOTSUPP; | 734 | return -EOPNOTSUPP; |
669 | priv->ctrlmode &= ~cm->mask; | 735 | priv->ctrlmode &= ~cm->mask; |
670 | priv->ctrlmode |= cm->flags; | 736 | priv->ctrlmode |= cm->flags; |
737 | |||
738 | /* CAN_CTRLMODE_FD can only be set when driver supports FD */ | ||
739 | if (priv->ctrlmode & CAN_CTRLMODE_FD) | ||
740 | dev->mtu = CANFD_MTU; | ||
741 | else | ||
742 | dev->mtu = CAN_MTU; | ||
671 | } | 743 | } |
672 | 744 | ||
673 | if (data[IFLA_CAN_RESTART_MS]) { | 745 | if (data[IFLA_CAN_RESTART_MS]) { |
@@ -686,6 +758,27 @@ static int can_changelink(struct net_device *dev, | |||
686 | return err; | 758 | return err; |
687 | } | 759 | } |
688 | 760 | ||
761 | if (data[IFLA_CAN_DATA_BITTIMING]) { | ||
762 | struct can_bittiming dbt; | ||
763 | |||
764 | /* Do not allow changing bittiming while running */ | ||
765 | if (dev->flags & IFF_UP) | ||
766 | return -EBUSY; | ||
767 | memcpy(&dbt, nla_data(data[IFLA_CAN_DATA_BITTIMING]), | ||
768 | sizeof(dbt)); | ||
769 | err = can_get_bittiming(dev, &dbt, priv->data_bittiming_const); | ||
770 | if (err) | ||
771 | return err; | ||
772 | memcpy(&priv->data_bittiming, &dbt, sizeof(dbt)); | ||
773 | |||
774 | if (priv->do_set_data_bittiming) { | ||
775 | /* Finally, set the bit-timing registers */ | ||
776 | err = priv->do_set_data_bittiming(dev); | ||
777 | if (err) | ||
778 | return err; | ||
779 | } | ||
780 | } | ||
781 | |||
689 | return 0; | 782 | return 0; |
690 | } | 783 | } |
691 | 784 | ||
@@ -694,7 +787,8 @@ static size_t can_get_size(const struct net_device *dev) | |||
694 | struct can_priv *priv = netdev_priv(dev); | 787 | struct can_priv *priv = netdev_priv(dev); |
695 | size_t size = 0; | 788 | size_t size = 0; |
696 | 789 | ||
697 | size += nla_total_size(sizeof(struct can_bittiming)); /* IFLA_CAN_BITTIMING */ | 790 | if (priv->bittiming.bitrate) /* IFLA_CAN_BITTIMING */ |
791 | size += nla_total_size(sizeof(struct can_bittiming)); | ||
698 | if (priv->bittiming_const) /* IFLA_CAN_BITTIMING_CONST */ | 792 | if (priv->bittiming_const) /* IFLA_CAN_BITTIMING_CONST */ |
699 | size += nla_total_size(sizeof(struct can_bittiming_const)); | 793 | size += nla_total_size(sizeof(struct can_bittiming_const)); |
700 | size += nla_total_size(sizeof(struct can_clock)); /* IFLA_CAN_CLOCK */ | 794 | size += nla_total_size(sizeof(struct can_clock)); /* IFLA_CAN_CLOCK */ |
@@ -703,6 +797,10 @@ static size_t can_get_size(const struct net_device *dev) | |||
703 | size += nla_total_size(sizeof(u32)); /* IFLA_CAN_RESTART_MS */ | 797 | size += nla_total_size(sizeof(u32)); /* IFLA_CAN_RESTART_MS */ |
704 | if (priv->do_get_berr_counter) /* IFLA_CAN_BERR_COUNTER */ | 798 | if (priv->do_get_berr_counter) /* IFLA_CAN_BERR_COUNTER */ |
705 | size += nla_total_size(sizeof(struct can_berr_counter)); | 799 | size += nla_total_size(sizeof(struct can_berr_counter)); |
800 | if (priv->data_bittiming.bitrate) /* IFLA_CAN_DATA_BITTIMING */ | ||
801 | size += nla_total_size(sizeof(struct can_bittiming)); | ||
802 | if (priv->data_bittiming_const) /* IFLA_CAN_DATA_BITTIMING_CONST */ | ||
803 | size += nla_total_size(sizeof(struct can_bittiming_const)); | ||
706 | 804 | ||
707 | return size; | 805 | return size; |
708 | } | 806 | } |
@@ -716,19 +814,34 @@ static int can_fill_info(struct sk_buff *skb, const struct net_device *dev) | |||
716 | 814 | ||
717 | if (priv->do_get_state) | 815 | if (priv->do_get_state) |
718 | priv->do_get_state(dev, &state); | 816 | priv->do_get_state(dev, &state); |
719 | if (nla_put(skb, IFLA_CAN_BITTIMING, | 817 | |
720 | sizeof(priv->bittiming), &priv->bittiming) || | 818 | if ((priv->bittiming.bitrate && |
819 | nla_put(skb, IFLA_CAN_BITTIMING, | ||
820 | sizeof(priv->bittiming), &priv->bittiming)) || | ||
821 | |||
721 | (priv->bittiming_const && | 822 | (priv->bittiming_const && |
722 | nla_put(skb, IFLA_CAN_BITTIMING_CONST, | 823 | nla_put(skb, IFLA_CAN_BITTIMING_CONST, |
723 | sizeof(*priv->bittiming_const), priv->bittiming_const)) || | 824 | sizeof(*priv->bittiming_const), priv->bittiming_const)) || |
825 | |||
724 | nla_put(skb, IFLA_CAN_CLOCK, sizeof(cm), &priv->clock) || | 826 | nla_put(skb, IFLA_CAN_CLOCK, sizeof(cm), &priv->clock) || |
725 | nla_put_u32(skb, IFLA_CAN_STATE, state) || | 827 | nla_put_u32(skb, IFLA_CAN_STATE, state) || |
726 | nla_put(skb, IFLA_CAN_CTRLMODE, sizeof(cm), &cm) || | 828 | nla_put(skb, IFLA_CAN_CTRLMODE, sizeof(cm), &cm) || |
727 | nla_put_u32(skb, IFLA_CAN_RESTART_MS, priv->restart_ms) || | 829 | nla_put_u32(skb, IFLA_CAN_RESTART_MS, priv->restart_ms) || |
830 | |||
728 | (priv->do_get_berr_counter && | 831 | (priv->do_get_berr_counter && |
729 | !priv->do_get_berr_counter(dev, &bec) && | 832 | !priv->do_get_berr_counter(dev, &bec) && |
730 | nla_put(skb, IFLA_CAN_BERR_COUNTER, sizeof(bec), &bec))) | 833 | nla_put(skb, IFLA_CAN_BERR_COUNTER, sizeof(bec), &bec)) || |
834 | |||
835 | (priv->data_bittiming.bitrate && | ||
836 | nla_put(skb, IFLA_CAN_DATA_BITTIMING, | ||
837 | sizeof(priv->data_bittiming), &priv->data_bittiming)) || | ||
838 | |||
839 | (priv->data_bittiming_const && | ||
840 | nla_put(skb, IFLA_CAN_DATA_BITTIMING_CONST, | ||
841 | sizeof(*priv->data_bittiming_const), | ||
842 | priv->data_bittiming_const))) | ||
731 | return -EMSGSIZE; | 843 | return -EMSGSIZE; |
844 | |||
732 | return 0; | 845 | return 0; |
733 | } | 846 | } |
734 | 847 | ||