Thomas Gleixner | 35e62ae | 2019-05-29 16:57:58 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2005 Marc Kleine-Budde, Pengutronix |
| 4 | * Copyright (C) 2006 Andrey Volkov, Varma Electronics |
| 5 | * Copyright (C) 2008-2009 Wolfgang Grandegger <wg@grandegger.com> |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 10 | #include <linux/slab.h> |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 11 | #include <linux/netdevice.h> |
| 12 | #include <linux/if_arp.h> |
Sergei Miroshnichenko | 9abefcb | 2016-09-07 16:51:12 +0300 | [diff] [blame] | 13 | #include <linux/workqueue.h> |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 14 | #include <linux/can.h> |
| 15 | #include <linux/can/dev.h> |
Oliver Hartkopp | 156c2bb | 2013-01-17 18:43:39 +0100 | [diff] [blame] | 16 | #include <linux/can/skb.h> |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 17 | #include <linux/can/netlink.h> |
Kurt Van Dijck | a1ef7bd | 2012-12-18 18:50:57 +0100 | [diff] [blame] | 18 | #include <linux/can/led.h> |
Franklin S Cooper Jr | 2290aef | 2018-01-10 16:25:18 +0530 | [diff] [blame] | 19 | #include <linux/of.h> |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 20 | #include <net/rtnetlink.h> |
| 21 | |
| 22 | #define MOD_DESC "CAN device driver interface" |
| 23 | |
| 24 | MODULE_DESCRIPTION(MOD_DESC); |
| 25 | MODULE_LICENSE("GPL v2"); |
| 26 | MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>"); |
| 27 | |
Oliver Hartkopp | 1e0625f | 2012-06-13 20:48:21 +0200 | [diff] [blame] | 28 | /* CAN DLC to real data length conversion helpers */ |
| 29 | |
| 30 | static const u8 dlc2len[] = {0, 1, 2, 3, 4, 5, 6, 7, |
| 31 | 8, 12, 16, 20, 24, 32, 48, 64}; |
| 32 | |
| 33 | /* get data length from can_dlc with sanitized can_dlc */ |
| 34 | u8 can_dlc2len(u8 can_dlc) |
| 35 | { |
| 36 | return dlc2len[can_dlc & 0x0F]; |
| 37 | } |
| 38 | EXPORT_SYMBOL_GPL(can_dlc2len); |
| 39 | |
| 40 | static const u8 len2dlc[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, /* 0 - 8 */ |
| 41 | 9, 9, 9, 9, /* 9 - 12 */ |
| 42 | 10, 10, 10, 10, /* 13 - 16 */ |
| 43 | 11, 11, 11, 11, /* 17 - 20 */ |
| 44 | 12, 12, 12, 12, /* 21 - 24 */ |
| 45 | 13, 13, 13, 13, 13, 13, 13, 13, /* 25 - 32 */ |
| 46 | 14, 14, 14, 14, 14, 14, 14, 14, /* 33 - 40 */ |
| 47 | 14, 14, 14, 14, 14, 14, 14, 14, /* 41 - 48 */ |
| 48 | 15, 15, 15, 15, 15, 15, 15, 15, /* 49 - 56 */ |
| 49 | 15, 15, 15, 15, 15, 15, 15, 15}; /* 57 - 64 */ |
| 50 | |
| 51 | /* map the sanitized data length to an appropriate data length code */ |
| 52 | u8 can_len2dlc(u8 len) |
| 53 | { |
| 54 | if (unlikely(len > 64)) |
| 55 | return 0xF; |
| 56 | |
| 57 | return len2dlc[len]; |
| 58 | } |
| 59 | EXPORT_SYMBOL_GPL(can_len2dlc); |
| 60 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 61 | #ifdef CONFIG_CAN_CALC_BITTIMING |
| 62 | #define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */ |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 63 | #define CAN_CALC_SYNC_SEG 1 |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 64 | |
| 65 | /* |
| 66 | * Bit-timing calculation derived from: |
| 67 | * |
| 68 | * Code based on LinCAN sources and H8S2638 project |
| 69 | * Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz |
| 70 | * Copyright 2005 Stanislav Marek |
| 71 | * email: pisa@cmp.felk.cvut.cz |
| 72 | * |
| 73 | * Calculates proper bit-timing parameters for a specified bit-rate |
| 74 | * and sample-point, which can then be used to set the bit-timing |
| 75 | * registers of the CAN controller. You can find more information |
| 76 | * in the header file linux/can/netlink.h. |
| 77 | */ |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 78 | static int can_update_sample_point(const struct can_bittiming_const *btc, |
| 79 | unsigned int sample_point_nominal, unsigned int tseg, |
| 80 | unsigned int *tseg1_ptr, unsigned int *tseg2_ptr, |
| 81 | unsigned int *sample_point_error_ptr) |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 82 | { |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 83 | unsigned int sample_point_error, best_sample_point_error = UINT_MAX; |
| 84 | unsigned int sample_point, best_sample_point = 0; |
| 85 | unsigned int tseg1, tseg2; |
| 86 | int i; |
| 87 | |
| 88 | for (i = 0; i <= 1; i++) { |
| 89 | tseg2 = tseg + CAN_CALC_SYNC_SEG - (sample_point_nominal * (tseg + CAN_CALC_SYNC_SEG)) / 1000 - i; |
| 90 | tseg2 = clamp(tseg2, btc->tseg2_min, btc->tseg2_max); |
| 91 | tseg1 = tseg - tseg2; |
| 92 | if (tseg1 > btc->tseg1_max) { |
| 93 | tseg1 = btc->tseg1_max; |
| 94 | tseg2 = tseg - tseg1; |
| 95 | } |
| 96 | |
| 97 | sample_point = 1000 * (tseg + CAN_CALC_SYNC_SEG - tseg2) / (tseg + CAN_CALC_SYNC_SEG); |
| 98 | sample_point_error = abs(sample_point_nominal - sample_point); |
| 99 | |
| 100 | if ((sample_point <= sample_point_nominal) && (sample_point_error < best_sample_point_error)) { |
| 101 | best_sample_point = sample_point; |
| 102 | best_sample_point_error = sample_point_error; |
| 103 | *tseg1_ptr = tseg1; |
| 104 | *tseg2_ptr = tseg2; |
| 105 | } |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 106 | } |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 107 | |
| 108 | if (sample_point_error_ptr) |
| 109 | *sample_point_error_ptr = best_sample_point_error; |
| 110 | |
| 111 | return best_sample_point; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Oliver Hartkopp | 08da7da | 2014-02-28 16:36:22 +0100 | [diff] [blame] | 114 | static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt, |
| 115 | const struct can_bittiming_const *btc) |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 116 | { |
| 117 | struct can_priv *priv = netdev_priv(dev); |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 118 | unsigned int bitrate; /* current bitrate */ |
| 119 | unsigned int bitrate_error; /* difference between current and nominal value */ |
| 120 | unsigned int best_bitrate_error = UINT_MAX; |
| 121 | unsigned int sample_point_error; /* difference between current and nominal value */ |
| 122 | unsigned int best_sample_point_error = UINT_MAX; |
| 123 | unsigned int sample_point_nominal; /* nominal sample point */ |
| 124 | unsigned int best_tseg = 0; /* current best value for tseg */ |
| 125 | unsigned int best_brp = 0; /* current best value for brp */ |
| 126 | unsigned int brp, tsegall, tseg, tseg1 = 0, tseg2 = 0; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 127 | u64 v64; |
| 128 | |
Roman Fietze | 67b5909 | 2014-10-20 10:32:42 +0200 | [diff] [blame] | 129 | /* Use CiA recommended sample points */ |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 130 | if (bt->sample_point) { |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 131 | sample_point_nominal = bt->sample_point; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 132 | } else { |
| 133 | if (bt->bitrate > 800000) |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 134 | sample_point_nominal = 750; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 135 | else if (bt->bitrate > 500000) |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 136 | sample_point_nominal = 800; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 137 | else |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 138 | sample_point_nominal = 875; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /* tseg even = round down, odd = round up */ |
| 142 | for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1; |
| 143 | tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) { |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 144 | tsegall = CAN_CALC_SYNC_SEG + tseg / 2; |
| 145 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 146 | /* Compute all possible tseg choices (tseg=tseg1+tseg2) */ |
| 147 | brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2; |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 148 | |
| 149 | /* choose brp step which is possible in system */ |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 150 | brp = (brp / btc->brp_inc) * btc->brp_inc; |
| 151 | if ((brp < btc->brp_min) || (brp > btc->brp_max)) |
| 152 | continue; |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 153 | |
| 154 | bitrate = priv->clock.freq / (brp * tsegall); |
| 155 | bitrate_error = abs(bt->bitrate - bitrate); |
| 156 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 157 | /* tseg brp biterror */ |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 158 | if (bitrate_error > best_bitrate_error) |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 159 | continue; |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 160 | |
| 161 | /* reset sample point error if we have a better bitrate */ |
| 162 | if (bitrate_error < best_bitrate_error) |
| 163 | best_sample_point_error = UINT_MAX; |
| 164 | |
| 165 | can_update_sample_point(btc, sample_point_nominal, tseg / 2, &tseg1, &tseg2, &sample_point_error); |
| 166 | if (sample_point_error > best_sample_point_error) |
| 167 | continue; |
| 168 | |
| 169 | best_sample_point_error = sample_point_error; |
| 170 | best_bitrate_error = bitrate_error; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 171 | best_tseg = tseg / 2; |
| 172 | best_brp = brp; |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 173 | |
| 174 | if (bitrate_error == 0 && sample_point_error == 0) |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 175 | break; |
| 176 | } |
| 177 | |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 178 | if (best_bitrate_error) { |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 179 | /* Error in one-tenth of a percent */ |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 180 | v64 = (u64)best_bitrate_error * 1000; |
| 181 | do_div(v64, bt->bitrate); |
| 182 | bitrate_error = (u32)v64; |
| 183 | if (bitrate_error > CAN_CALC_MAX_ERROR) { |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 184 | netdev_err(dev, |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 185 | "bitrate error %d.%d%% too high\n", |
| 186 | bitrate_error / 10, bitrate_error % 10); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 187 | return -EDOM; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 188 | } |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 189 | netdev_warn(dev, "bitrate error %d.%d%%\n", |
| 190 | bitrate_error / 10, bitrate_error % 10); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /* real sample point */ |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 194 | bt->sample_point = can_update_sample_point(btc, sample_point_nominal, best_tseg, |
| 195 | &tseg1, &tseg2, NULL); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 196 | |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 197 | v64 = (u64)best_brp * 1000 * 1000 * 1000; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 198 | do_div(v64, priv->clock.freq); |
| 199 | bt->tq = (u32)v64; |
| 200 | bt->prop_seg = tseg1 / 2; |
| 201 | bt->phase_seg1 = tseg1 - bt->prop_seg; |
| 202 | bt->phase_seg2 = tseg2; |
Oliver Hartkopp | 2e11437 | 2011-09-28 02:50:11 +0000 | [diff] [blame] | 203 | |
| 204 | /* check for sjw user settings */ |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 205 | if (!bt->sjw || !btc->sjw_max) { |
Oliver Hartkopp | 2e11437 | 2011-09-28 02:50:11 +0000 | [diff] [blame] | 206 | bt->sjw = 1; |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 207 | } else { |
Oliver Hartkopp | 2e11437 | 2011-09-28 02:50:11 +0000 | [diff] [blame] | 208 | /* bt->sjw is at least 1 -> sanitize upper bound to sjw_max */ |
| 209 | if (bt->sjw > btc->sjw_max) |
| 210 | bt->sjw = btc->sjw_max; |
| 211 | /* bt->sjw must not be higher than tseg2 */ |
| 212 | if (tseg2 < bt->sjw) |
| 213 | bt->sjw = tseg2; |
| 214 | } |
| 215 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 216 | bt->brp = best_brp; |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 217 | |
| 218 | /* real bitrate */ |
| 219 | bt->bitrate = priv->clock.freq / (bt->brp * (CAN_CALC_SYNC_SEG + tseg1 + tseg2)); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | #else /* !CONFIG_CAN_CALC_BITTIMING */ |
Oliver Hartkopp | 08da7da | 2014-02-28 16:36:22 +0100 | [diff] [blame] | 224 | static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt, |
| 225 | const struct can_bittiming_const *btc) |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 226 | { |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 227 | netdev_err(dev, "bit-timing calculation not available\n"); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 228 | return -EINVAL; |
| 229 | } |
| 230 | #endif /* CONFIG_CAN_CALC_BITTIMING */ |
| 231 | |
| 232 | /* |
| 233 | * Checks the validity of the specified bit-timing parameters prop_seg, |
| 234 | * phase_seg1, phase_seg2 and sjw and tries to determine the bitrate |
| 235 | * prescaler value brp. You can find more information in the header |
| 236 | * file linux/can/netlink.h. |
| 237 | */ |
Oliver Hartkopp | 08da7da | 2014-02-28 16:36:22 +0100 | [diff] [blame] | 238 | static int can_fixup_bittiming(struct net_device *dev, struct can_bittiming *bt, |
| 239 | const struct can_bittiming_const *btc) |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 240 | { |
| 241 | struct can_priv *priv = netdev_priv(dev); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 242 | int tseg1, alltseg; |
| 243 | u64 brp64; |
| 244 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 245 | tseg1 = bt->prop_seg + bt->phase_seg1; |
| 246 | if (!bt->sjw) |
| 247 | bt->sjw = 1; |
| 248 | if (bt->sjw > btc->sjw_max || |
| 249 | tseg1 < btc->tseg1_min || tseg1 > btc->tseg1_max || |
| 250 | bt->phase_seg2 < btc->tseg2_min || bt->phase_seg2 > btc->tseg2_max) |
| 251 | return -ERANGE; |
| 252 | |
| 253 | brp64 = (u64)priv->clock.freq * (u64)bt->tq; |
| 254 | if (btc->brp_inc > 1) |
| 255 | do_div(brp64, btc->brp_inc); |
| 256 | brp64 += 500000000UL - 1; |
| 257 | do_div(brp64, 1000000000UL); /* the practicable BRP */ |
| 258 | if (btc->brp_inc > 1) |
| 259 | brp64 *= btc->brp_inc; |
| 260 | bt->brp = (u32)brp64; |
| 261 | |
| 262 | if (bt->brp < btc->brp_min || bt->brp > btc->brp_max) |
| 263 | return -EINVAL; |
| 264 | |
| 265 | alltseg = bt->prop_seg + bt->phase_seg1 + bt->phase_seg2 + 1; |
| 266 | bt->bitrate = priv->clock.freq / (bt->brp * alltseg); |
| 267 | bt->sample_point = ((tseg1 + 1) * 1000) / alltseg; |
| 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | |
Marc Kleine-Budde | 431af77 | 2017-01-11 17:05:35 +0100 | [diff] [blame] | 272 | /* Checks the validity of predefined bitrate settings */ |
| 273 | static int can_validate_bitrate(struct net_device *dev, struct can_bittiming *bt, |
| 274 | const u32 *bitrate_const, |
| 275 | const unsigned int bitrate_const_cnt) |
| 276 | { |
| 277 | struct can_priv *priv = netdev_priv(dev); |
| 278 | unsigned int i; |
| 279 | |
| 280 | for (i = 0; i < bitrate_const_cnt; i++) { |
| 281 | if (bt->bitrate == bitrate_const[i]) |
| 282 | break; |
| 283 | } |
| 284 | |
| 285 | if (i >= priv->bitrate_const_cnt) |
| 286 | return -EINVAL; |
| 287 | |
| 288 | return 0; |
| 289 | } |
| 290 | |
Oliver Hartkopp | 08da7da | 2014-02-28 16:36:22 +0100 | [diff] [blame] | 291 | static int can_get_bittiming(struct net_device *dev, struct can_bittiming *bt, |
Marc Kleine-Budde | 431af77 | 2017-01-11 17:05:35 +0100 | [diff] [blame] | 292 | const struct can_bittiming_const *btc, |
| 293 | const u32 *bitrate_const, |
| 294 | const unsigned int bitrate_const_cnt) |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 295 | { |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 296 | int err; |
| 297 | |
Oliver Hartkopp | d5298df | 2014-02-28 16:36:21 +0100 | [diff] [blame] | 298 | /* |
| 299 | * Depending on the given can_bittiming parameter structure the CAN |
| 300 | * timing parameters are calculated based on the provided bitrate OR |
| 301 | * alternatively the CAN timing parameters (tq, prop_seg, etc.) are |
| 302 | * provided directly which are then checked and fixed up. |
| 303 | */ |
Marc Kleine-Budde | 431af77 | 2017-01-11 17:05:35 +0100 | [diff] [blame] | 304 | if (!bt->tq && bt->bitrate && btc) |
Oliver Hartkopp | 08da7da | 2014-02-28 16:36:22 +0100 | [diff] [blame] | 305 | err = can_calc_bittiming(dev, bt, btc); |
Marc Kleine-Budde | 431af77 | 2017-01-11 17:05:35 +0100 | [diff] [blame] | 306 | else if (bt->tq && !bt->bitrate && btc) |
Oliver Hartkopp | 08da7da | 2014-02-28 16:36:22 +0100 | [diff] [blame] | 307 | err = can_fixup_bittiming(dev, bt, btc); |
Marc Kleine-Budde | 431af77 | 2017-01-11 17:05:35 +0100 | [diff] [blame] | 308 | else if (!bt->tq && bt->bitrate && bitrate_const) |
| 309 | err = can_validate_bitrate(dev, bt, bitrate_const, |
| 310 | bitrate_const_cnt); |
Oliver Hartkopp | d5298df | 2014-02-28 16:36:21 +0100 | [diff] [blame] | 311 | else |
| 312 | err = -EINVAL; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 313 | |
Oliver Hartkopp | d5298df | 2014-02-28 16:36:21 +0100 | [diff] [blame] | 314 | return err; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Andri Yngvason | bac78aa | 2014-12-03 17:54:13 +0000 | [diff] [blame] | 317 | static void can_update_state_error_stats(struct net_device *dev, |
| 318 | enum can_state new_state) |
| 319 | { |
| 320 | struct can_priv *priv = netdev_priv(dev); |
| 321 | |
| 322 | if (new_state <= priv->state) |
| 323 | return; |
| 324 | |
| 325 | switch (new_state) { |
| 326 | case CAN_STATE_ERROR_WARNING: |
| 327 | priv->can_stats.error_warning++; |
| 328 | break; |
| 329 | case CAN_STATE_ERROR_PASSIVE: |
| 330 | priv->can_stats.error_passive++; |
| 331 | break; |
| 332 | case CAN_STATE_BUS_OFF: |
Andri Yngvason | be38a6f | 2015-01-16 14:30:28 +0000 | [diff] [blame] | 333 | priv->can_stats.bus_off++; |
| 334 | break; |
Andri Yngvason | bac78aa | 2014-12-03 17:54:13 +0000 | [diff] [blame] | 335 | default: |
| 336 | break; |
kbuild test robot | 5b5ba2a | 2015-01-16 16:52:21 +0800 | [diff] [blame] | 337 | } |
Andri Yngvason | bac78aa | 2014-12-03 17:54:13 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | static int can_tx_state_to_frame(struct net_device *dev, enum can_state state) |
| 341 | { |
| 342 | switch (state) { |
| 343 | case CAN_STATE_ERROR_ACTIVE: |
| 344 | return CAN_ERR_CRTL_ACTIVE; |
| 345 | case CAN_STATE_ERROR_WARNING: |
| 346 | return CAN_ERR_CRTL_TX_WARNING; |
| 347 | case CAN_STATE_ERROR_PASSIVE: |
| 348 | return CAN_ERR_CRTL_TX_PASSIVE; |
| 349 | default: |
| 350 | return 0; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | static int can_rx_state_to_frame(struct net_device *dev, enum can_state state) |
| 355 | { |
| 356 | switch (state) { |
| 357 | case CAN_STATE_ERROR_ACTIVE: |
| 358 | return CAN_ERR_CRTL_ACTIVE; |
| 359 | case CAN_STATE_ERROR_WARNING: |
| 360 | return CAN_ERR_CRTL_RX_WARNING; |
| 361 | case CAN_STATE_ERROR_PASSIVE: |
| 362 | return CAN_ERR_CRTL_RX_PASSIVE; |
| 363 | default: |
| 364 | return 0; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | void can_change_state(struct net_device *dev, struct can_frame *cf, |
| 369 | enum can_state tx_state, enum can_state rx_state) |
| 370 | { |
| 371 | struct can_priv *priv = netdev_priv(dev); |
| 372 | enum can_state new_state = max(tx_state, rx_state); |
| 373 | |
| 374 | if (unlikely(new_state == priv->state)) { |
| 375 | netdev_warn(dev, "%s: oops, state did not change", __func__); |
| 376 | return; |
| 377 | } |
| 378 | |
| 379 | netdev_dbg(dev, "New error state: %d\n", new_state); |
| 380 | |
| 381 | can_update_state_error_stats(dev, new_state); |
| 382 | priv->state = new_state; |
| 383 | |
Marc Kleine-Budde | ff3416f | 2017-05-18 10:22:22 +0200 | [diff] [blame] | 384 | if (!cf) |
| 385 | return; |
| 386 | |
Andri Yngvason | bac78aa | 2014-12-03 17:54:13 +0000 | [diff] [blame] | 387 | if (unlikely(new_state == CAN_STATE_BUS_OFF)) { |
| 388 | cf->can_id |= CAN_ERR_BUSOFF; |
| 389 | return; |
| 390 | } |
| 391 | |
| 392 | cf->can_id |= CAN_ERR_CRTL; |
| 393 | cf->data[1] |= tx_state >= rx_state ? |
| 394 | can_tx_state_to_frame(dev, tx_state) : 0; |
| 395 | cf->data[1] |= tx_state <= rx_state ? |
| 396 | can_rx_state_to_frame(dev, rx_state) : 0; |
| 397 | } |
| 398 | EXPORT_SYMBOL_GPL(can_change_state); |
| 399 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 400 | /* |
| 401 | * Local echo of CAN messages |
| 402 | * |
| 403 | * CAN network devices *should* support a local echo functionality |
Robert Schwebel | 7d59773 | 2018-01-24 11:19:11 +0100 | [diff] [blame] | 404 | * (see Documentation/networking/can.rst). To test the handling of CAN |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 405 | * interfaces that do not support the local echo both driver types are |
| 406 | * implemented. In the case that the driver does not support the echo |
| 407 | * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core |
| 408 | * to perform the echo as a fallback solution. |
| 409 | */ |
| 410 | static void can_flush_echo_skb(struct net_device *dev) |
| 411 | { |
| 412 | struct can_priv *priv = netdev_priv(dev); |
| 413 | struct net_device_stats *stats = &dev->stats; |
| 414 | int i; |
| 415 | |
Wolfgang Grandegger | a6e4bc5 | 2009-10-08 22:17:11 +0000 | [diff] [blame] | 416 | for (i = 0; i < priv->echo_skb_max; i++) { |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 417 | if (priv->echo_skb[i]) { |
| 418 | kfree_skb(priv->echo_skb[i]); |
| 419 | priv->echo_skb[i] = NULL; |
| 420 | stats->tx_dropped++; |
| 421 | stats->tx_aborted_errors++; |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | /* |
| 427 | * Put the skb on the stack to be looped backed locally lateron |
| 428 | * |
| 429 | * The function is typically called in the start_xmit function |
| 430 | * of the device driver. The driver must protect access to |
| 431 | * priv->echo_skb, if necessary. |
| 432 | */ |
Wolfgang Grandegger | a6e4bc5 | 2009-10-08 22:17:11 +0000 | [diff] [blame] | 433 | void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev, |
| 434 | unsigned int idx) |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 435 | { |
| 436 | struct can_priv *priv = netdev_priv(dev); |
| 437 | |
Wolfgang Grandegger | a6e4bc5 | 2009-10-08 22:17:11 +0000 | [diff] [blame] | 438 | BUG_ON(idx >= priv->echo_skb_max); |
| 439 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 440 | /* check flag whether this packet has to be looped back */ |
Oliver Hartkopp | a94bc9c | 2014-02-28 16:36:19 +0100 | [diff] [blame] | 441 | if (!(dev->flags & IFF_ECHO) || skb->pkt_type != PACKET_LOOPBACK || |
| 442 | (skb->protocol != htons(ETH_P_CAN) && |
| 443 | skb->protocol != htons(ETH_P_CANFD))) { |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 444 | kfree_skb(skb); |
| 445 | return; |
| 446 | } |
| 447 | |
| 448 | if (!priv->echo_skb[idx]) { |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 449 | |
Oliver Hartkopp | 0ae89be | 2014-01-30 10:11:28 +0100 | [diff] [blame] | 450 | skb = can_create_echo_skb(skb); |
| 451 | if (!skb) |
| 452 | return; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 453 | |
| 454 | /* make settings for echo to reduce code in irq context */ |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 455 | skb->pkt_type = PACKET_BROADCAST; |
| 456 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 457 | skb->dev = dev; |
| 458 | |
| 459 | /* save this skb for tx interrupt echo handling */ |
| 460 | priv->echo_skb[idx] = skb; |
| 461 | } else { |
| 462 | /* locking problem with netif_stop_queue() ?? */ |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 463 | netdev_err(dev, "%s: BUG! echo_skb is occupied!\n", __func__); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 464 | kfree_skb(skb); |
| 465 | } |
| 466 | } |
| 467 | EXPORT_SYMBOL_GPL(can_put_echo_skb); |
| 468 | |
Marc Kleine-Budde | a4310fa | 2018-10-31 10:37:46 +0100 | [diff] [blame] | 469 | struct sk_buff *__can_get_echo_skb(struct net_device *dev, unsigned int idx, u8 *len_ptr) |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 470 | { |
| 471 | struct can_priv *priv = netdev_priv(dev); |
| 472 | |
Marc Kleine-Budde | e7a6994 | 2018-10-31 14:05:26 +0100 | [diff] [blame] | 473 | if (idx >= priv->echo_skb_max) { |
| 474 | netdev_err(dev, "%s: BUG! Trying to access can_priv::echo_skb out of bounds (%u/max %u)\n", |
| 475 | __func__, idx, priv->echo_skb_max); |
| 476 | return NULL; |
| 477 | } |
Wolfgang Grandegger | a6e4bc5 | 2009-10-08 22:17:11 +0000 | [diff] [blame] | 478 | |
Manfred Schlaegl | 7b12c81 | 2018-12-19 19:39:58 +0100 | [diff] [blame] | 479 | if (priv->echo_skb[idx]) { |
| 480 | /* Using "struct canfd_frame::len" for the frame |
| 481 | * length is supported on both CAN and CANFD frames. |
| 482 | */ |
| 483 | struct sk_buff *skb = priv->echo_skb[idx]; |
| 484 | struct canfd_frame *cf = (struct canfd_frame *)skb->data; |
| 485 | u8 len = cf->len; |
| 486 | |
| 487 | *len_ptr = len; |
| 488 | priv->echo_skb[idx] = NULL; |
| 489 | |
| 490 | return skb; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 491 | } |
Marc Kleine-Budde | cf5046b | 2011-10-10 23:43:53 +0200 | [diff] [blame] | 492 | |
Manfred Schlaegl | 7b12c81 | 2018-12-19 19:39:58 +0100 | [diff] [blame] | 493 | return NULL; |
Marc Kleine-Budde | a4310fa | 2018-10-31 10:37:46 +0100 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | /* |
| 497 | * Get the skb from the stack and loop it back locally |
| 498 | * |
| 499 | * The function is typically called when the TX done interrupt |
| 500 | * is handled in the device driver. The driver must protect |
| 501 | * access to priv->echo_skb, if necessary. |
| 502 | */ |
| 503 | unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx) |
| 504 | { |
| 505 | struct sk_buff *skb; |
| 506 | u8 len; |
| 507 | |
| 508 | skb = __can_get_echo_skb(dev, idx, &len); |
| 509 | if (!skb) |
| 510 | return 0; |
| 511 | |
| 512 | netif_rx(skb); |
| 513 | |
| 514 | return len; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 515 | } |
| 516 | EXPORT_SYMBOL_GPL(can_get_echo_skb); |
| 517 | |
| 518 | /* |
Wolfgang Grandegger | 39e3ab6 | 2009-09-01 05:26:12 +0000 | [diff] [blame] | 519 | * Remove the skb from the stack and free it. |
| 520 | * |
| 521 | * The function is typically called when TX failed. |
| 522 | */ |
Wolfgang Grandegger | a6e4bc5 | 2009-10-08 22:17:11 +0000 | [diff] [blame] | 523 | void can_free_echo_skb(struct net_device *dev, unsigned int idx) |
Wolfgang Grandegger | 39e3ab6 | 2009-09-01 05:26:12 +0000 | [diff] [blame] | 524 | { |
| 525 | struct can_priv *priv = netdev_priv(dev); |
| 526 | |
Wolfgang Grandegger | a6e4bc5 | 2009-10-08 22:17:11 +0000 | [diff] [blame] | 527 | BUG_ON(idx >= priv->echo_skb_max); |
| 528 | |
Wolfgang Grandegger | 39e3ab6 | 2009-09-01 05:26:12 +0000 | [diff] [blame] | 529 | if (priv->echo_skb[idx]) { |
Thomas Körper | 5247a58 | 2014-10-31 07:33:54 +0100 | [diff] [blame] | 530 | dev_kfree_skb_any(priv->echo_skb[idx]); |
Wolfgang Grandegger | 39e3ab6 | 2009-09-01 05:26:12 +0000 | [diff] [blame] | 531 | priv->echo_skb[idx] = NULL; |
| 532 | } |
| 533 | } |
| 534 | EXPORT_SYMBOL_GPL(can_free_echo_skb); |
| 535 | |
| 536 | /* |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 537 | * CAN device restart for bus-off recovery |
| 538 | */ |
Sergei Miroshnichenko | 9abefcb | 2016-09-07 16:51:12 +0300 | [diff] [blame] | 539 | static void can_restart(struct net_device *dev) |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 540 | { |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 541 | struct can_priv *priv = netdev_priv(dev); |
| 542 | struct net_device_stats *stats = &dev->stats; |
| 543 | struct sk_buff *skb; |
| 544 | struct can_frame *cf; |
| 545 | int err; |
| 546 | |
| 547 | BUG_ON(netif_carrier_ok(dev)); |
| 548 | |
| 549 | /* |
| 550 | * No synchronization needed because the device is bus-off and |
| 551 | * no messages can come in or go out. |
| 552 | */ |
| 553 | can_flush_echo_skb(dev); |
| 554 | |
| 555 | /* send restart message upstream */ |
Wolfgang Grandegger | 7b6856a0 | 2009-10-20 00:08:01 -0700 | [diff] [blame] | 556 | skb = alloc_can_err_skb(dev, &cf); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 557 | if (skb == NULL) { |
| 558 | err = -ENOMEM; |
Wolfgang Grandegger | b3d0df7 | 2009-07-20 04:06:40 +0000 | [diff] [blame] | 559 | goto restart; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 560 | } |
Wolfgang Grandegger | 7b6856a0 | 2009-10-20 00:08:01 -0700 | [diff] [blame] | 561 | cf->can_id |= CAN_ERR_RESTARTED; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 562 | |
| 563 | netif_rx(skb); |
| 564 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 565 | stats->rx_packets++; |
| 566 | stats->rx_bytes += cf->can_dlc; |
| 567 | |
Wolfgang Grandegger | b3d0df7 | 2009-07-20 04:06:40 +0000 | [diff] [blame] | 568 | restart: |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 569 | netdev_dbg(dev, "restarted\n"); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 570 | priv->can_stats.restarts++; |
| 571 | |
| 572 | /* Now restart the device */ |
| 573 | err = priv->do_set_mode(dev, CAN_MODE_START); |
| 574 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 575 | netif_carrier_on(dev); |
| 576 | if (err) |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 577 | netdev_err(dev, "Error %d during restart", err); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 578 | } |
| 579 | |
Sergei Miroshnichenko | 9abefcb | 2016-09-07 16:51:12 +0300 | [diff] [blame] | 580 | static void can_restart_work(struct work_struct *work) |
| 581 | { |
| 582 | struct delayed_work *dwork = to_delayed_work(work); |
| 583 | struct can_priv *priv = container_of(dwork, struct can_priv, restart_work); |
| 584 | |
| 585 | can_restart(priv->dev); |
| 586 | } |
| 587 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 588 | int can_restart_now(struct net_device *dev) |
| 589 | { |
| 590 | struct can_priv *priv = netdev_priv(dev); |
| 591 | |
| 592 | /* |
| 593 | * A manual restart is only permitted if automatic restart is |
| 594 | * disabled and the device is in the bus-off state |
| 595 | */ |
| 596 | if (priv->restart_ms) |
| 597 | return -EINVAL; |
| 598 | if (priv->state != CAN_STATE_BUS_OFF) |
| 599 | return -EBUSY; |
| 600 | |
Sergei Miroshnichenko | 9abefcb | 2016-09-07 16:51:12 +0300 | [diff] [blame] | 601 | cancel_delayed_work_sync(&priv->restart_work); |
| 602 | can_restart(dev); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 603 | |
| 604 | return 0; |
| 605 | } |
| 606 | |
| 607 | /* |
| 608 | * CAN bus-off |
| 609 | * |
| 610 | * This functions should be called when the device goes bus-off to |
| 611 | * tell the netif layer that no more packets can be sent or received. |
| 612 | * If enabled, a timer is started to trigger bus-off recovery. |
| 613 | */ |
| 614 | void can_bus_off(struct net_device *dev) |
| 615 | { |
| 616 | struct can_priv *priv = netdev_priv(dev); |
| 617 | |
Jakob Unterwurzacher | 71c23a8 | 2018-04-18 16:10:03 +0200 | [diff] [blame] | 618 | netdev_info(dev, "bus-off\n"); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 619 | |
| 620 | netif_carrier_off(dev); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 621 | |
| 622 | if (priv->restart_ms) |
Sergei Miroshnichenko | 9abefcb | 2016-09-07 16:51:12 +0300 | [diff] [blame] | 623 | schedule_delayed_work(&priv->restart_work, |
| 624 | msecs_to_jiffies(priv->restart_ms)); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 625 | } |
| 626 | EXPORT_SYMBOL_GPL(can_bus_off); |
| 627 | |
| 628 | static void can_setup(struct net_device *dev) |
| 629 | { |
| 630 | dev->type = ARPHRD_CAN; |
Oliver Hartkopp | 1e0625f | 2012-06-13 20:48:21 +0200 | [diff] [blame] | 631 | dev->mtu = CAN_MTU; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 632 | dev->hard_header_len = 0; |
| 633 | dev->addr_len = 0; |
| 634 | dev->tx_queue_len = 10; |
| 635 | |
| 636 | /* New-style flags. */ |
| 637 | dev->flags = IFF_NOARP; |
Michał Mirosław | 34324dc | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 638 | dev->features = NETIF_F_HW_CSUM; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 639 | } |
| 640 | |
Wolfgang Grandegger | 7b6856a0 | 2009-10-20 00:08:01 -0700 | [diff] [blame] | 641 | struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf) |
| 642 | { |
| 643 | struct sk_buff *skb; |
| 644 | |
Oliver Hartkopp | 156c2bb | 2013-01-17 18:43:39 +0100 | [diff] [blame] | 645 | skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) + |
| 646 | sizeof(struct can_frame)); |
Wolfgang Grandegger | 7b6856a0 | 2009-10-20 00:08:01 -0700 | [diff] [blame] | 647 | if (unlikely(!skb)) |
| 648 | return NULL; |
| 649 | |
| 650 | skb->protocol = htons(ETH_P_CAN); |
| 651 | skb->pkt_type = PACKET_BROADCAST; |
| 652 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
Oliver Hartkopp | 156c2bb | 2013-01-17 18:43:39 +0100 | [diff] [blame] | 653 | |
Oliver Hartkopp | 969439016 | 2015-02-23 20:37:54 +0100 | [diff] [blame] | 654 | skb_reset_mac_header(skb); |
| 655 | skb_reset_network_header(skb); |
| 656 | skb_reset_transport_header(skb); |
| 657 | |
Oliver Hartkopp | 2bf3440 | 2013-01-28 08:33:33 +0000 | [diff] [blame] | 658 | can_skb_reserve(skb); |
| 659 | can_skb_prv(skb)->ifindex = dev->ifindex; |
Oliver Hartkopp | d3b58c4 | 2015-06-26 11:58:19 +0200 | [diff] [blame] | 660 | can_skb_prv(skb)->skbcnt = 0; |
Oliver Hartkopp | 156c2bb | 2013-01-17 18:43:39 +0100 | [diff] [blame] | 661 | |
YueHaibing | 8551e71 | 2018-04-28 15:49:57 +0800 | [diff] [blame] | 662 | *cf = skb_put_zero(skb, sizeof(struct can_frame)); |
Wolfgang Grandegger | 7b6856a0 | 2009-10-20 00:08:01 -0700 | [diff] [blame] | 663 | |
| 664 | return skb; |
| 665 | } |
| 666 | EXPORT_SYMBOL_GPL(alloc_can_skb); |
| 667 | |
Stephane Grosjean | cb2518c | 2014-01-15 09:50:13 +0100 | [diff] [blame] | 668 | struct sk_buff *alloc_canfd_skb(struct net_device *dev, |
| 669 | struct canfd_frame **cfd) |
| 670 | { |
| 671 | struct sk_buff *skb; |
| 672 | |
| 673 | skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) + |
| 674 | sizeof(struct canfd_frame)); |
| 675 | if (unlikely(!skb)) |
| 676 | return NULL; |
| 677 | |
| 678 | skb->protocol = htons(ETH_P_CANFD); |
| 679 | skb->pkt_type = PACKET_BROADCAST; |
| 680 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 681 | |
Oliver Hartkopp | 969439016 | 2015-02-23 20:37:54 +0100 | [diff] [blame] | 682 | skb_reset_mac_header(skb); |
| 683 | skb_reset_network_header(skb); |
| 684 | skb_reset_transport_header(skb); |
| 685 | |
Stephane Grosjean | cb2518c | 2014-01-15 09:50:13 +0100 | [diff] [blame] | 686 | can_skb_reserve(skb); |
| 687 | can_skb_prv(skb)->ifindex = dev->ifindex; |
Oliver Hartkopp | d3b58c4 | 2015-06-26 11:58:19 +0200 | [diff] [blame] | 688 | can_skb_prv(skb)->skbcnt = 0; |
Stephane Grosjean | cb2518c | 2014-01-15 09:50:13 +0100 | [diff] [blame] | 689 | |
YueHaibing | 8551e71 | 2018-04-28 15:49:57 +0800 | [diff] [blame] | 690 | *cfd = skb_put_zero(skb, sizeof(struct canfd_frame)); |
Stephane Grosjean | cb2518c | 2014-01-15 09:50:13 +0100 | [diff] [blame] | 691 | |
| 692 | return skb; |
| 693 | } |
| 694 | EXPORT_SYMBOL_GPL(alloc_canfd_skb); |
| 695 | |
Wolfgang Grandegger | 7b6856a0 | 2009-10-20 00:08:01 -0700 | [diff] [blame] | 696 | struct sk_buff *alloc_can_err_skb(struct net_device *dev, struct can_frame **cf) |
| 697 | { |
| 698 | struct sk_buff *skb; |
| 699 | |
| 700 | skb = alloc_can_skb(dev, cf); |
| 701 | if (unlikely(!skb)) |
| 702 | return NULL; |
| 703 | |
| 704 | (*cf)->can_id = CAN_ERR_FLAG; |
| 705 | (*cf)->can_dlc = CAN_ERR_DLC; |
| 706 | |
| 707 | return skb; |
| 708 | } |
| 709 | EXPORT_SYMBOL_GPL(alloc_can_err_skb); |
| 710 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 711 | /* |
| 712 | * Allocate and setup space for the CAN network device |
| 713 | */ |
Zhu Yi | 0387090 | 2018-06-13 16:37:17 +0200 | [diff] [blame] | 714 | struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max, |
| 715 | unsigned int txqs, unsigned int rxqs) |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 716 | { |
| 717 | struct net_device *dev; |
| 718 | struct can_priv *priv; |
Wolfgang Grandegger | a6e4bc5 | 2009-10-08 22:17:11 +0000 | [diff] [blame] | 719 | int size; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 720 | |
Wolfgang Grandegger | a6e4bc5 | 2009-10-08 22:17:11 +0000 | [diff] [blame] | 721 | if (echo_skb_max) |
| 722 | size = ALIGN(sizeof_priv, sizeof(struct sk_buff *)) + |
| 723 | echo_skb_max * sizeof(struct sk_buff *); |
| 724 | else |
| 725 | size = sizeof_priv; |
| 726 | |
Zhu Yi | 0387090 | 2018-06-13 16:37:17 +0200 | [diff] [blame] | 727 | dev = alloc_netdev_mqs(size, "can%d", NET_NAME_UNKNOWN, can_setup, |
| 728 | txqs, rxqs); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 729 | if (!dev) |
| 730 | return NULL; |
| 731 | |
| 732 | priv = netdev_priv(dev); |
Sergei Miroshnichenko | 9abefcb | 2016-09-07 16:51:12 +0300 | [diff] [blame] | 733 | priv->dev = dev; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 734 | |
Wolfgang Grandegger | a6e4bc5 | 2009-10-08 22:17:11 +0000 | [diff] [blame] | 735 | if (echo_skb_max) { |
| 736 | priv->echo_skb_max = echo_skb_max; |
| 737 | priv->echo_skb = (void *)priv + |
| 738 | ALIGN(sizeof_priv, sizeof(struct sk_buff *)); |
| 739 | } |
| 740 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 741 | priv->state = CAN_STATE_STOPPED; |
| 742 | |
Sergei Miroshnichenko | 9abefcb | 2016-09-07 16:51:12 +0300 | [diff] [blame] | 743 | INIT_DELAYED_WORK(&priv->restart_work, can_restart_work); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 744 | |
| 745 | return dev; |
| 746 | } |
Zhu Yi | 0387090 | 2018-06-13 16:37:17 +0200 | [diff] [blame] | 747 | EXPORT_SYMBOL_GPL(alloc_candev_mqs); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 748 | |
| 749 | /* |
| 750 | * Free space of the CAN network device |
| 751 | */ |
| 752 | void free_candev(struct net_device *dev) |
| 753 | { |
| 754 | free_netdev(dev); |
| 755 | } |
| 756 | EXPORT_SYMBOL_GPL(free_candev); |
| 757 | |
| 758 | /* |
Oliver Hartkopp | bc05a89 | 2014-02-28 16:36:24 +0100 | [diff] [blame] | 759 | * changing MTU and control mode for CAN/CANFD devices |
| 760 | */ |
| 761 | int can_change_mtu(struct net_device *dev, int new_mtu) |
| 762 | { |
| 763 | struct can_priv *priv = netdev_priv(dev); |
| 764 | |
| 765 | /* Do not allow changing the MTU while running */ |
| 766 | if (dev->flags & IFF_UP) |
| 767 | return -EBUSY; |
| 768 | |
| 769 | /* allow change of MTU according to the CANFD ability of the device */ |
| 770 | switch (new_mtu) { |
| 771 | case CAN_MTU: |
Oliver Hartkopp | bb208f1 | 2016-03-21 20:18:21 +0100 | [diff] [blame] | 772 | /* 'CANFD-only' controllers can not switch to CAN_MTU */ |
| 773 | if (priv->ctrlmode_static & CAN_CTRLMODE_FD) |
| 774 | return -EINVAL; |
| 775 | |
Oliver Hartkopp | bc05a89 | 2014-02-28 16:36:24 +0100 | [diff] [blame] | 776 | priv->ctrlmode &= ~CAN_CTRLMODE_FD; |
| 777 | break; |
| 778 | |
| 779 | case CANFD_MTU: |
Oliver Hartkopp | bb208f1 | 2016-03-21 20:18:21 +0100 | [diff] [blame] | 780 | /* check for potential CANFD ability */ |
| 781 | if (!(priv->ctrlmode_supported & CAN_CTRLMODE_FD) && |
| 782 | !(priv->ctrlmode_static & CAN_CTRLMODE_FD)) |
Oliver Hartkopp | bc05a89 | 2014-02-28 16:36:24 +0100 | [diff] [blame] | 783 | return -EINVAL; |
| 784 | |
| 785 | priv->ctrlmode |= CAN_CTRLMODE_FD; |
| 786 | break; |
| 787 | |
| 788 | default: |
| 789 | return -EINVAL; |
| 790 | } |
| 791 | |
| 792 | dev->mtu = new_mtu; |
| 793 | return 0; |
| 794 | } |
| 795 | EXPORT_SYMBOL_GPL(can_change_mtu); |
| 796 | |
| 797 | /* |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 798 | * Common open function when the device gets opened. |
| 799 | * |
| 800 | * This function should be called in the open function of the device |
| 801 | * driver. |
| 802 | */ |
| 803 | int open_candev(struct net_device *dev) |
| 804 | { |
| 805 | struct can_priv *priv = netdev_priv(dev); |
| 806 | |
Oliver Hartkopp | b30749f | 2014-02-28 16:36:20 +0100 | [diff] [blame] | 807 | if (!priv->bittiming.bitrate) { |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 808 | netdev_err(dev, "bit-timing not yet defined\n"); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 809 | return -EINVAL; |
| 810 | } |
| 811 | |
Oliver Hartkopp | dd22586 | 2014-02-28 16:36:25 +0100 | [diff] [blame] | 812 | /* For CAN FD the data bitrate has to be >= the arbitration bitrate */ |
| 813 | if ((priv->ctrlmode & CAN_CTRLMODE_FD) && |
| 814 | (!priv->data_bittiming.bitrate || |
| 815 | (priv->data_bittiming.bitrate < priv->bittiming.bitrate))) { |
| 816 | netdev_err(dev, "incorrect/missing data bit-timing\n"); |
| 817 | return -EINVAL; |
| 818 | } |
| 819 | |
Wolfgang Grandegger | 1b0d922 | 2009-07-20 04:06:41 +0000 | [diff] [blame] | 820 | /* Switch carrier on if device was stopped while in bus-off state */ |
| 821 | if (!netif_carrier_ok(dev)) |
| 822 | netif_carrier_on(dev); |
| 823 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 824 | return 0; |
| 825 | } |
Wolfgang Grandegger | 128ced8 | 2009-05-30 07:55:48 +0000 | [diff] [blame] | 826 | EXPORT_SYMBOL_GPL(open_candev); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 827 | |
Franklin S Cooper Jr | 2290aef | 2018-01-10 16:25:18 +0530 | [diff] [blame] | 828 | #ifdef CONFIG_OF |
| 829 | /* Common function that can be used to understand the limitation of |
| 830 | * a transceiver when it provides no means to determine these limitations |
| 831 | * at runtime. |
| 832 | */ |
| 833 | void of_can_transceiver(struct net_device *dev) |
| 834 | { |
| 835 | struct device_node *dn; |
| 836 | struct can_priv *priv = netdev_priv(dev); |
| 837 | struct device_node *np = dev->dev.parent->of_node; |
| 838 | int ret; |
| 839 | |
| 840 | dn = of_get_child_by_name(np, "can-transceiver"); |
| 841 | if (!dn) |
| 842 | return; |
| 843 | |
| 844 | ret = of_property_read_u32(dn, "max-bitrate", &priv->bitrate_max); |
| 845 | if ((ret && ret != -EINVAL) || (!ret && !priv->bitrate_max)) |
| 846 | netdev_warn(dev, "Invalid value for transceiver max bitrate. Ignoring bitrate limit.\n"); |
| 847 | } |
| 848 | EXPORT_SYMBOL_GPL(of_can_transceiver); |
| 849 | #endif |
| 850 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 851 | /* |
| 852 | * Common close function for cleanup before the device gets closed. |
| 853 | * |
| 854 | * This function should be called in the close function of the device |
| 855 | * driver. |
| 856 | */ |
| 857 | void close_candev(struct net_device *dev) |
| 858 | { |
| 859 | struct can_priv *priv = netdev_priv(dev); |
| 860 | |
Sergei Miroshnichenko | 9abefcb | 2016-09-07 16:51:12 +0300 | [diff] [blame] | 861 | cancel_delayed_work_sync(&priv->restart_work); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 862 | can_flush_echo_skb(dev); |
| 863 | } |
| 864 | EXPORT_SYMBOL_GPL(close_candev); |
| 865 | |
| 866 | /* |
| 867 | * CAN netlink interface |
| 868 | */ |
| 869 | static const struct nla_policy can_policy[IFLA_CAN_MAX + 1] = { |
| 870 | [IFLA_CAN_STATE] = { .type = NLA_U32 }, |
| 871 | [IFLA_CAN_CTRLMODE] = { .len = sizeof(struct can_ctrlmode) }, |
| 872 | [IFLA_CAN_RESTART_MS] = { .type = NLA_U32 }, |
| 873 | [IFLA_CAN_RESTART] = { .type = NLA_U32 }, |
| 874 | [IFLA_CAN_BITTIMING] = { .len = sizeof(struct can_bittiming) }, |
| 875 | [IFLA_CAN_BITTIMING_CONST] |
| 876 | = { .len = sizeof(struct can_bittiming_const) }, |
| 877 | [IFLA_CAN_CLOCK] = { .len = sizeof(struct can_clock) }, |
Wolfgang Grandegger | 52c793f | 2010-02-22 22:21:17 +0000 | [diff] [blame] | 878 | [IFLA_CAN_BERR_COUNTER] = { .len = sizeof(struct can_berr_counter) }, |
Oliver Hartkopp | 9859ccd | 2014-02-28 16:36:23 +0100 | [diff] [blame] | 879 | [IFLA_CAN_DATA_BITTIMING] |
| 880 | = { .len = sizeof(struct can_bittiming) }, |
| 881 | [IFLA_CAN_DATA_BITTIMING_CONST] |
| 882 | = { .len = sizeof(struct can_bittiming_const) }, |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 883 | }; |
| 884 | |
Matthias Schiffer | a8b8a889 | 2017-06-25 23:56:01 +0200 | [diff] [blame] | 885 | static int can_validate(struct nlattr *tb[], struct nlattr *data[], |
| 886 | struct netlink_ext_ack *extack) |
Oliver Hartkopp | bb208f1 | 2016-03-21 20:18:21 +0100 | [diff] [blame] | 887 | { |
| 888 | bool is_can_fd = false; |
| 889 | |
| 890 | /* Make sure that valid CAN FD configurations always consist of |
| 891 | * - nominal/arbitration bittiming |
| 892 | * - data bittiming |
| 893 | * - control mode with CAN_CTRLMODE_FD set |
| 894 | */ |
| 895 | |
Oliver Hartkopp | bce271f | 2016-06-21 12:14:07 +0200 | [diff] [blame] | 896 | if (!data) |
| 897 | return 0; |
| 898 | |
Oliver Hartkopp | bb208f1 | 2016-03-21 20:18:21 +0100 | [diff] [blame] | 899 | if (data[IFLA_CAN_CTRLMODE]) { |
| 900 | struct can_ctrlmode *cm = nla_data(data[IFLA_CAN_CTRLMODE]); |
| 901 | |
| 902 | is_can_fd = cm->flags & cm->mask & CAN_CTRLMODE_FD; |
| 903 | } |
| 904 | |
| 905 | if (is_can_fd) { |
| 906 | if (!data[IFLA_CAN_BITTIMING] || !data[IFLA_CAN_DATA_BITTIMING]) |
| 907 | return -EOPNOTSUPP; |
| 908 | } |
| 909 | |
| 910 | if (data[IFLA_CAN_DATA_BITTIMING]) { |
| 911 | if (!is_can_fd || !data[IFLA_CAN_BITTIMING]) |
| 912 | return -EOPNOTSUPP; |
| 913 | } |
| 914 | |
| 915 | return 0; |
| 916 | } |
| 917 | |
Matthias Schiffer | ad744b2 | 2017-06-25 23:56:00 +0200 | [diff] [blame] | 918 | static int can_changelink(struct net_device *dev, struct nlattr *tb[], |
| 919 | struct nlattr *data[], |
| 920 | struct netlink_ext_ack *extack) |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 921 | { |
| 922 | struct can_priv *priv = netdev_priv(dev); |
| 923 | int err; |
| 924 | |
| 925 | /* We need synchronization with dev->stop() */ |
| 926 | ASSERT_RTNL(); |
| 927 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 928 | if (data[IFLA_CAN_BITTIMING]) { |
| 929 | struct can_bittiming bt; |
| 930 | |
| 931 | /* Do not allow changing bittiming while running */ |
| 932 | if (dev->flags & IFF_UP) |
| 933 | return -EBUSY; |
Marc Kleine-Budde | c3606d4 | 2017-01-06 11:12:41 +0100 | [diff] [blame] | 934 | |
| 935 | /* Calculate bittiming parameters based on |
| 936 | * bittiming_const if set, otherwise pass bitrate |
| 937 | * directly via do_set_bitrate(). Bail out if neither |
| 938 | * is given. |
| 939 | */ |
| 940 | if (!priv->bittiming_const && !priv->do_set_bittiming) |
| 941 | return -EOPNOTSUPP; |
| 942 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 943 | memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt)); |
Marc Kleine-Budde | 431af77 | 2017-01-11 17:05:35 +0100 | [diff] [blame] | 944 | err = can_get_bittiming(dev, &bt, |
| 945 | priv->bittiming_const, |
| 946 | priv->bitrate_const, |
| 947 | priv->bitrate_const_cnt); |
| 948 | if (err) |
| 949 | return err; |
Franklin S Cooper Jr | 2290aef | 2018-01-10 16:25:18 +0530 | [diff] [blame] | 950 | |
| 951 | if (priv->bitrate_max && bt.bitrate > priv->bitrate_max) { |
| 952 | netdev_err(dev, "arbitration bitrate surpasses transceiver capabilities of %d bps\n", |
| 953 | priv->bitrate_max); |
| 954 | return -EINVAL; |
| 955 | } |
| 956 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 957 | memcpy(&priv->bittiming, &bt, sizeof(bt)); |
| 958 | |
| 959 | if (priv->do_set_bittiming) { |
| 960 | /* Finally, set the bit-timing registers */ |
| 961 | err = priv->do_set_bittiming(dev); |
| 962 | if (err) |
| 963 | return err; |
| 964 | } |
| 965 | } |
| 966 | |
Marc Kleine-Budde | 49cb5c0 | 2013-10-04 15:40:22 +0200 | [diff] [blame] | 967 | if (data[IFLA_CAN_CTRLMODE]) { |
| 968 | struct can_ctrlmode *cm; |
Oliver Hartkopp | bb208f1 | 2016-03-21 20:18:21 +0100 | [diff] [blame] | 969 | u32 ctrlstatic; |
| 970 | u32 maskedflags; |
Marc Kleine-Budde | 49cb5c0 | 2013-10-04 15:40:22 +0200 | [diff] [blame] | 971 | |
| 972 | /* Do not allow changing controller mode while running */ |
| 973 | if (dev->flags & IFF_UP) |
| 974 | return -EBUSY; |
| 975 | cm = nla_data(data[IFLA_CAN_CTRLMODE]); |
Oliver Hartkopp | bb208f1 | 2016-03-21 20:18:21 +0100 | [diff] [blame] | 976 | ctrlstatic = priv->ctrlmode_static; |
| 977 | maskedflags = cm->flags & cm->mask; |
Oliver Hartkopp | 9b1087a | 2015-01-05 18:40:15 +0100 | [diff] [blame] | 978 | |
Oliver Hartkopp | bb208f1 | 2016-03-21 20:18:21 +0100 | [diff] [blame] | 979 | /* check whether provided bits are allowed to be passed */ |
| 980 | if (cm->mask & ~(priv->ctrlmode_supported | ctrlstatic)) |
| 981 | return -EOPNOTSUPP; |
| 982 | |
| 983 | /* do not check for static fd-non-iso if 'fd' is disabled */ |
| 984 | if (!(maskedflags & CAN_CTRLMODE_FD)) |
| 985 | ctrlstatic &= ~CAN_CTRLMODE_FD_NON_ISO; |
| 986 | |
| 987 | /* make sure static options are provided by configuration */ |
| 988 | if ((maskedflags & ctrlstatic) != ctrlstatic) |
Marc Kleine-Budde | 49cb5c0 | 2013-10-04 15:40:22 +0200 | [diff] [blame] | 989 | return -EOPNOTSUPP; |
Oliver Hartkopp | 9b1087a | 2015-01-05 18:40:15 +0100 | [diff] [blame] | 990 | |
| 991 | /* clear bits to be modified and copy the flag values */ |
Marc Kleine-Budde | 49cb5c0 | 2013-10-04 15:40:22 +0200 | [diff] [blame] | 992 | priv->ctrlmode &= ~cm->mask; |
Oliver Hartkopp | bb208f1 | 2016-03-21 20:18:21 +0100 | [diff] [blame] | 993 | priv->ctrlmode |= maskedflags; |
Oliver Hartkopp | bc05a89 | 2014-02-28 16:36:24 +0100 | [diff] [blame] | 994 | |
| 995 | /* CAN_CTRLMODE_FD can only be set when driver supports FD */ |
| 996 | if (priv->ctrlmode & CAN_CTRLMODE_FD) |
| 997 | dev->mtu = CANFD_MTU; |
| 998 | else |
| 999 | dev->mtu = CAN_MTU; |
Marc Kleine-Budde | 49cb5c0 | 2013-10-04 15:40:22 +0200 | [diff] [blame] | 1000 | } |
| 1001 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 1002 | if (data[IFLA_CAN_RESTART_MS]) { |
| 1003 | /* Do not allow changing restart delay while running */ |
| 1004 | if (dev->flags & IFF_UP) |
| 1005 | return -EBUSY; |
| 1006 | priv->restart_ms = nla_get_u32(data[IFLA_CAN_RESTART_MS]); |
| 1007 | } |
| 1008 | |
| 1009 | if (data[IFLA_CAN_RESTART]) { |
| 1010 | /* Do not allow a restart while not running */ |
| 1011 | if (!(dev->flags & IFF_UP)) |
| 1012 | return -EINVAL; |
| 1013 | err = can_restart_now(dev); |
| 1014 | if (err) |
| 1015 | return err; |
| 1016 | } |
| 1017 | |
Oliver Hartkopp | 9859ccd | 2014-02-28 16:36:23 +0100 | [diff] [blame] | 1018 | if (data[IFLA_CAN_DATA_BITTIMING]) { |
| 1019 | struct can_bittiming dbt; |
| 1020 | |
| 1021 | /* Do not allow changing bittiming while running */ |
| 1022 | if (dev->flags & IFF_UP) |
| 1023 | return -EBUSY; |
Marc Kleine-Budde | c3606d4 | 2017-01-06 11:12:41 +0100 | [diff] [blame] | 1024 | |
| 1025 | /* Calculate bittiming parameters based on |
| 1026 | * data_bittiming_const if set, otherwise pass bitrate |
| 1027 | * directly via do_set_bitrate(). Bail out if neither |
| 1028 | * is given. |
| 1029 | */ |
| 1030 | if (!priv->data_bittiming_const && !priv->do_set_data_bittiming) |
| 1031 | return -EOPNOTSUPP; |
| 1032 | |
Oliver Hartkopp | 9859ccd | 2014-02-28 16:36:23 +0100 | [diff] [blame] | 1033 | memcpy(&dbt, nla_data(data[IFLA_CAN_DATA_BITTIMING]), |
| 1034 | sizeof(dbt)); |
Marc Kleine-Budde | 431af77 | 2017-01-11 17:05:35 +0100 | [diff] [blame] | 1035 | err = can_get_bittiming(dev, &dbt, |
| 1036 | priv->data_bittiming_const, |
| 1037 | priv->data_bitrate_const, |
| 1038 | priv->data_bitrate_const_cnt); |
| 1039 | if (err) |
| 1040 | return err; |
Franklin S Cooper Jr | 2290aef | 2018-01-10 16:25:18 +0530 | [diff] [blame] | 1041 | |
| 1042 | if (priv->bitrate_max && dbt.bitrate > priv->bitrate_max) { |
| 1043 | netdev_err(dev, "canfd data bitrate surpasses transceiver capabilities of %d bps\n", |
| 1044 | priv->bitrate_max); |
| 1045 | return -EINVAL; |
| 1046 | } |
| 1047 | |
Oliver Hartkopp | 9859ccd | 2014-02-28 16:36:23 +0100 | [diff] [blame] | 1048 | memcpy(&priv->data_bittiming, &dbt, sizeof(dbt)); |
| 1049 | |
| 1050 | if (priv->do_set_data_bittiming) { |
| 1051 | /* Finally, set the bit-timing registers */ |
| 1052 | err = priv->do_set_data_bittiming(dev); |
| 1053 | if (err) |
| 1054 | return err; |
| 1055 | } |
| 1056 | } |
| 1057 | |
Oliver Hartkopp | 12a6075 | 2017-01-10 18:52:06 +0100 | [diff] [blame] | 1058 | if (data[IFLA_CAN_TERMINATION]) { |
| 1059 | const u16 termval = nla_get_u16(data[IFLA_CAN_TERMINATION]); |
| 1060 | const unsigned int num_term = priv->termination_const_cnt; |
| 1061 | unsigned int i; |
| 1062 | |
| 1063 | if (!priv->do_set_termination) |
| 1064 | return -EOPNOTSUPP; |
| 1065 | |
| 1066 | /* check whether given value is supported by the interface */ |
| 1067 | for (i = 0; i < num_term; i++) { |
| 1068 | if (termval == priv->termination_const[i]) |
| 1069 | break; |
| 1070 | } |
| 1071 | if (i >= num_term) |
| 1072 | return -EINVAL; |
| 1073 | |
| 1074 | /* Finally, set the termination value */ |
| 1075 | err = priv->do_set_termination(dev, termval); |
| 1076 | if (err) |
| 1077 | return err; |
| 1078 | |
| 1079 | priv->termination = termval; |
| 1080 | } |
| 1081 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 1082 | return 0; |
| 1083 | } |
| 1084 | |
Wolfgang Grandegger | 53a0ef86 | 2009-11-06 23:53:13 +0000 | [diff] [blame] | 1085 | static size_t can_get_size(const struct net_device *dev) |
| 1086 | { |
| 1087 | struct can_priv *priv = netdev_priv(dev); |
Marc Kleine-Budde | c13c64d | 2013-10-05 20:34:47 +0200 | [diff] [blame] | 1088 | size_t size = 0; |
Wolfgang Grandegger | 53a0ef86 | 2009-11-06 23:53:13 +0000 | [diff] [blame] | 1089 | |
Oliver Hartkopp | b30749f | 2014-02-28 16:36:20 +0100 | [diff] [blame] | 1090 | if (priv->bittiming.bitrate) /* IFLA_CAN_BITTIMING */ |
| 1091 | size += nla_total_size(sizeof(struct can_bittiming)); |
Marc Kleine-Budde | c13c64d | 2013-10-05 20:34:47 +0200 | [diff] [blame] | 1092 | if (priv->bittiming_const) /* IFLA_CAN_BITTIMING_CONST */ |
Marc Kleine-Budde | fe119a0 | 2013-10-05 21:25:17 +0200 | [diff] [blame] | 1093 | size += nla_total_size(sizeof(struct can_bittiming_const)); |
Marc Kleine-Budde | c13c64d | 2013-10-05 20:34:47 +0200 | [diff] [blame] | 1094 | size += nla_total_size(sizeof(struct can_clock)); /* IFLA_CAN_CLOCK */ |
| 1095 | size += nla_total_size(sizeof(u32)); /* IFLA_CAN_STATE */ |
| 1096 | size += nla_total_size(sizeof(struct can_ctrlmode)); /* IFLA_CAN_CTRLMODE */ |
| 1097 | size += nla_total_size(sizeof(u32)); /* IFLA_CAN_RESTART_MS */ |
| 1098 | if (priv->do_get_berr_counter) /* IFLA_CAN_BERR_COUNTER */ |
| 1099 | size += nla_total_size(sizeof(struct can_berr_counter)); |
Oliver Hartkopp | 9859ccd | 2014-02-28 16:36:23 +0100 | [diff] [blame] | 1100 | if (priv->data_bittiming.bitrate) /* IFLA_CAN_DATA_BITTIMING */ |
| 1101 | size += nla_total_size(sizeof(struct can_bittiming)); |
| 1102 | if (priv->data_bittiming_const) /* IFLA_CAN_DATA_BITTIMING_CONST */ |
| 1103 | size += nla_total_size(sizeof(struct can_bittiming_const)); |
Oliver Hartkopp | 12a6075 | 2017-01-10 18:52:06 +0100 | [diff] [blame] | 1104 | if (priv->termination_const) { |
| 1105 | size += nla_total_size(sizeof(priv->termination)); /* IFLA_CAN_TERMINATION */ |
| 1106 | size += nla_total_size(sizeof(*priv->termination_const) * /* IFLA_CAN_TERMINATION_CONST */ |
| 1107 | priv->termination_const_cnt); |
| 1108 | } |
Marc Kleine-Budde | 431af77 | 2017-01-11 17:05:35 +0100 | [diff] [blame] | 1109 | if (priv->bitrate_const) /* IFLA_CAN_BITRATE_CONST */ |
| 1110 | size += nla_total_size(sizeof(*priv->bitrate_const) * |
| 1111 | priv->bitrate_const_cnt); |
| 1112 | if (priv->data_bitrate_const) /* IFLA_CAN_DATA_BITRATE_CONST */ |
| 1113 | size += nla_total_size(sizeof(*priv->data_bitrate_const) * |
| 1114 | priv->data_bitrate_const_cnt); |
Franklin S Cooper Jr | 2290aef | 2018-01-10 16:25:18 +0530 | [diff] [blame] | 1115 | size += sizeof(priv->bitrate_max); /* IFLA_CAN_BITRATE_MAX */ |
Wolfgang Grandegger | 53a0ef86 | 2009-11-06 23:53:13 +0000 | [diff] [blame] | 1116 | |
| 1117 | return size; |
| 1118 | } |
| 1119 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 1120 | static int can_fill_info(struct sk_buff *skb, const struct net_device *dev) |
| 1121 | { |
| 1122 | struct can_priv *priv = netdev_priv(dev); |
| 1123 | struct can_ctrlmode cm = {.flags = priv->ctrlmode}; |
Wolfgang Grandegger | 52c793f | 2010-02-22 22:21:17 +0000 | [diff] [blame] | 1124 | struct can_berr_counter bec; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 1125 | enum can_state state = priv->state; |
| 1126 | |
| 1127 | if (priv->do_get_state) |
| 1128 | priv->do_get_state(dev, &state); |
Oliver Hartkopp | 9859ccd | 2014-02-28 16:36:23 +0100 | [diff] [blame] | 1129 | |
Oliver Hartkopp | b30749f | 2014-02-28 16:36:20 +0100 | [diff] [blame] | 1130 | if ((priv->bittiming.bitrate && |
| 1131 | nla_put(skb, IFLA_CAN_BITTIMING, |
| 1132 | sizeof(priv->bittiming), &priv->bittiming)) || |
Oliver Hartkopp | 9859ccd | 2014-02-28 16:36:23 +0100 | [diff] [blame] | 1133 | |
David S. Miller | 31e0e32 | 2012-04-01 20:21:11 -0400 | [diff] [blame] | 1134 | (priv->bittiming_const && |
| 1135 | nla_put(skb, IFLA_CAN_BITTIMING_CONST, |
Marc Kleine-Budde | 57a59b9 | 2013-10-04 13:35:57 +0200 | [diff] [blame] | 1136 | sizeof(*priv->bittiming_const), priv->bittiming_const)) || |
Oliver Hartkopp | 9859ccd | 2014-02-28 16:36:23 +0100 | [diff] [blame] | 1137 | |
Marek Vasut | 562b103 | 2015-10-30 13:48:19 +0100 | [diff] [blame] | 1138 | nla_put(skb, IFLA_CAN_CLOCK, sizeof(priv->clock), &priv->clock) || |
Marc Kleine-Budde | 57a59b9 | 2013-10-04 13:35:57 +0200 | [diff] [blame] | 1139 | nla_put_u32(skb, IFLA_CAN_STATE, state) || |
| 1140 | nla_put(skb, IFLA_CAN_CTRLMODE, sizeof(cm), &cm) || |
| 1141 | nla_put_u32(skb, IFLA_CAN_RESTART_MS, priv->restart_ms) || |
Oliver Hartkopp | 9859ccd | 2014-02-28 16:36:23 +0100 | [diff] [blame] | 1142 | |
Marc Kleine-Budde | 57a59b9 | 2013-10-04 13:35:57 +0200 | [diff] [blame] | 1143 | (priv->do_get_berr_counter && |
| 1144 | !priv->do_get_berr_counter(dev, &bec) && |
Oliver Hartkopp | 9859ccd | 2014-02-28 16:36:23 +0100 | [diff] [blame] | 1145 | nla_put(skb, IFLA_CAN_BERR_COUNTER, sizeof(bec), &bec)) || |
| 1146 | |
| 1147 | (priv->data_bittiming.bitrate && |
| 1148 | nla_put(skb, IFLA_CAN_DATA_BITTIMING, |
| 1149 | sizeof(priv->data_bittiming), &priv->data_bittiming)) || |
| 1150 | |
| 1151 | (priv->data_bittiming_const && |
| 1152 | nla_put(skb, IFLA_CAN_DATA_BITTIMING_CONST, |
| 1153 | sizeof(*priv->data_bittiming_const), |
Oliver Hartkopp | 12a6075 | 2017-01-10 18:52:06 +0100 | [diff] [blame] | 1154 | priv->data_bittiming_const)) || |
| 1155 | |
| 1156 | (priv->termination_const && |
| 1157 | (nla_put_u16(skb, IFLA_CAN_TERMINATION, priv->termination) || |
| 1158 | nla_put(skb, IFLA_CAN_TERMINATION_CONST, |
| 1159 | sizeof(*priv->termination_const) * |
| 1160 | priv->termination_const_cnt, |
Marc Kleine-Budde | 431af77 | 2017-01-11 17:05:35 +0100 | [diff] [blame] | 1161 | priv->termination_const))) || |
| 1162 | |
| 1163 | (priv->bitrate_const && |
| 1164 | nla_put(skb, IFLA_CAN_BITRATE_CONST, |
| 1165 | sizeof(*priv->bitrate_const) * |
| 1166 | priv->bitrate_const_cnt, |
| 1167 | priv->bitrate_const)) || |
| 1168 | |
| 1169 | (priv->data_bitrate_const && |
| 1170 | nla_put(skb, IFLA_CAN_DATA_BITRATE_CONST, |
| 1171 | sizeof(*priv->data_bitrate_const) * |
| 1172 | priv->data_bitrate_const_cnt, |
Franklin S Cooper Jr | 2290aef | 2018-01-10 16:25:18 +0530 | [diff] [blame] | 1173 | priv->data_bitrate_const)) || |
| 1174 | |
| 1175 | (nla_put(skb, IFLA_CAN_BITRATE_MAX, |
| 1176 | sizeof(priv->bitrate_max), |
| 1177 | &priv->bitrate_max)) |
Marc Kleine-Budde | 431af77 | 2017-01-11 17:05:35 +0100 | [diff] [blame] | 1178 | ) |
Oliver Hartkopp | 12a6075 | 2017-01-10 18:52:06 +0100 | [diff] [blame] | 1179 | |
Marc Kleine-Budde | 57a59b9 | 2013-10-04 13:35:57 +0200 | [diff] [blame] | 1180 | return -EMSGSIZE; |
Oliver Hartkopp | 9859ccd | 2014-02-28 16:36:23 +0100 | [diff] [blame] | 1181 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 1182 | return 0; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
Wolfgang Grandegger | 55369c0 | 2009-11-12 05:34:05 +0000 | [diff] [blame] | 1185 | static size_t can_get_xstats_size(const struct net_device *dev) |
| 1186 | { |
| 1187 | return sizeof(struct can_device_stats); |
| 1188 | } |
| 1189 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 1190 | static int can_fill_xstats(struct sk_buff *skb, const struct net_device *dev) |
| 1191 | { |
| 1192 | struct can_priv *priv = netdev_priv(dev); |
| 1193 | |
David S. Miller | 31e0e32 | 2012-04-01 20:21:11 -0400 | [diff] [blame] | 1194 | if (nla_put(skb, IFLA_INFO_XSTATS, |
| 1195 | sizeof(priv->can_stats), &priv->can_stats)) |
| 1196 | goto nla_put_failure; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 1197 | return 0; |
| 1198 | |
| 1199 | nla_put_failure: |
| 1200 | return -EMSGSIZE; |
| 1201 | } |
| 1202 | |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 1203 | static int can_newlink(struct net *src_net, struct net_device *dev, |
Matthias Schiffer | 7a3f4a1 | 2017-06-25 23:55:59 +0200 | [diff] [blame] | 1204 | struct nlattr *tb[], struct nlattr *data[], |
| 1205 | struct netlink_ext_ack *extack) |
Oliver Hartkopp | 993e6f2 | 2009-08-11 02:41:24 +0000 | [diff] [blame] | 1206 | { |
| 1207 | return -EOPNOTSUPP; |
| 1208 | } |
| 1209 | |
Oliver Hartkopp | 25e1ed6 | 2016-06-21 15:45:47 +0200 | [diff] [blame] | 1210 | static void can_dellink(struct net_device *dev, struct list_head *head) |
| 1211 | { |
| 1212 | return; |
| 1213 | } |
| 1214 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 1215 | static struct rtnl_link_ops can_link_ops __read_mostly = { |
| 1216 | .kind = "can", |
| 1217 | .maxtype = IFLA_CAN_MAX, |
| 1218 | .policy = can_policy, |
| 1219 | .setup = can_setup, |
Oliver Hartkopp | bb208f1 | 2016-03-21 20:18:21 +0100 | [diff] [blame] | 1220 | .validate = can_validate, |
Oliver Hartkopp | 993e6f2 | 2009-08-11 02:41:24 +0000 | [diff] [blame] | 1221 | .newlink = can_newlink, |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 1222 | .changelink = can_changelink, |
Oliver Hartkopp | 25e1ed6 | 2016-06-21 15:45:47 +0200 | [diff] [blame] | 1223 | .dellink = can_dellink, |
Wolfgang Grandegger | 53a0ef86 | 2009-11-06 23:53:13 +0000 | [diff] [blame] | 1224 | .get_size = can_get_size, |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 1225 | .fill_info = can_fill_info, |
Wolfgang Grandegger | 55369c0 | 2009-11-12 05:34:05 +0000 | [diff] [blame] | 1226 | .get_xstats_size = can_get_xstats_size, |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 1227 | .fill_xstats = can_fill_xstats, |
| 1228 | }; |
| 1229 | |
| 1230 | /* |
| 1231 | * Register the CAN network device |
| 1232 | */ |
| 1233 | int register_candev(struct net_device *dev) |
| 1234 | { |
Oliver Hartkopp | 12a6075 | 2017-01-10 18:52:06 +0100 | [diff] [blame] | 1235 | struct can_priv *priv = netdev_priv(dev); |
| 1236 | |
| 1237 | /* Ensure termination_const, termination_const_cnt and |
| 1238 | * do_set_termination consistency. All must be either set or |
| 1239 | * unset. |
| 1240 | */ |
| 1241 | if ((!priv->termination_const != !priv->termination_const_cnt) || |
| 1242 | (!priv->termination_const != !priv->do_set_termination)) |
| 1243 | return -EINVAL; |
| 1244 | |
Marc Kleine-Budde | 431af77 | 2017-01-11 17:05:35 +0100 | [diff] [blame] | 1245 | if (!priv->bitrate_const != !priv->bitrate_const_cnt) |
| 1246 | return -EINVAL; |
| 1247 | |
| 1248 | if (!priv->data_bitrate_const != !priv->data_bitrate_const_cnt) |
| 1249 | return -EINVAL; |
| 1250 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 1251 | dev->rtnl_link_ops = &can_link_ops; |
| 1252 | return register_netdev(dev); |
| 1253 | } |
| 1254 | EXPORT_SYMBOL_GPL(register_candev); |
| 1255 | |
| 1256 | /* |
| 1257 | * Unregister the CAN network device |
| 1258 | */ |
| 1259 | void unregister_candev(struct net_device *dev) |
| 1260 | { |
| 1261 | unregister_netdev(dev); |
| 1262 | } |
| 1263 | EXPORT_SYMBOL_GPL(unregister_candev); |
| 1264 | |
Kurt Van Dijck | bf03a53 | 2012-12-18 18:50:56 +0100 | [diff] [blame] | 1265 | /* |
| 1266 | * Test if a network device is a candev based device |
| 1267 | * and return the can_priv* if so. |
| 1268 | */ |
| 1269 | struct can_priv *safe_candev_priv(struct net_device *dev) |
| 1270 | { |
| 1271 | if ((dev->type != ARPHRD_CAN) || (dev->rtnl_link_ops != &can_link_ops)) |
| 1272 | return NULL; |
| 1273 | |
| 1274 | return netdev_priv(dev); |
| 1275 | } |
| 1276 | EXPORT_SYMBOL_GPL(safe_candev_priv); |
| 1277 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 1278 | static __init int can_dev_init(void) |
| 1279 | { |
| 1280 | int err; |
| 1281 | |
Kurt Van Dijck | a1ef7bd | 2012-12-18 18:50:57 +0100 | [diff] [blame] | 1282 | can_led_notifier_init(); |
| 1283 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 1284 | err = rtnl_link_register(&can_link_ops); |
| 1285 | if (!err) |
| 1286 | printk(KERN_INFO MOD_DESC "\n"); |
| 1287 | |
| 1288 | return err; |
| 1289 | } |
| 1290 | module_init(can_dev_init); |
| 1291 | |
| 1292 | static __exit void can_dev_exit(void) |
| 1293 | { |
| 1294 | rtnl_link_unregister(&can_link_ops); |
Kurt Van Dijck | a1ef7bd | 2012-12-18 18:50:57 +0100 | [diff] [blame] | 1295 | |
| 1296 | can_led_notifier_exit(); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 1297 | } |
| 1298 | module_exit(can_dev_exit); |
| 1299 | |
| 1300 | MODULE_ALIAS_RTNL_LINK("can"); |