Marc Kleine-Budde says: ==================== pull-request: can-next 2025-11-26 this is a pull request of 27 patches for net-next/main. The first 17 patches are by Vincent Mailhol and Oliver Hartkopp and add CAN XL support to the CAN netlink interface. Geert Uytterhoeven and Biju Das provide 7 patches for the rcar_canfd driver to add suspend/resume support. The next 2 patches are by Markus Schneider-Pargmann and add them as the m_can maintainer. Conor Dooley's patch updates the mpfs-can DT bindungs. linux-can-next-for-6.19-20251126 * tag 'linux-can-next-for-6.19-20251126' of git://git.kernel.org/pub/scm/linux/kernel/git/mkl/linux-can-next: (27 commits) dt-bindings: can: mpfs: document resets MAINTAINERS: Simplify m_can section MAINTAINERS: Add myself as m_can maintainer can: rcar_canfd: Add suspend/resume support can: rcar_canfd: Convert to DEFINE_SIMPLE_DEV_PM_OPS() can: rcar_canfd: Invert CAN clock and close_candev() order can: rcar_canfd: Extract rcar_canfd_global_{,de}init() can: rcar_canfd: Use devm_clk_get_optional() for RAM clk can: rcar_canfd: Invert global vs. channel teardown can: rcar_canfd: Invert reset assert order can: dev: print bitrate error with two decimal digits can: raw: instantly reject unsupported CAN frames can: add dummy_can driver can: calc_bittiming: add can_calc_sample_point_pwm() can: calc_bittiming: add can_calc_sample_point_nrz() can: calc_bittiming: replace misleading "nominal" by "reference" can: netlink: add PWM netlink interface can: calc_bittiming: add PWM calculation can: bittiming: add PWM validation can: bittiming: add PWM parameters ... ====================
Unified · Split
| @@ -32,11 +32,15 @@ properties: · 31 unmodified lines | ||
| 32 | 32 | - description: AHB peripheral clock |
| 33 | 33 | - description: CAN bus clock |
| 34 | 34 | |
| 35 | + resets: | |
| 36 | + maxItems: 1 | |
| 37 | + | |
| 35 | 38 | required: |
| 36 | 39 | - compatible |
| 37 | 40 | - reg |
| 38 | 41 | - interrupts |
| 39 | 42 | - clocks |
| 43 | + - resets | |
| 40 | 44 | |
| 41 | 45 | additionalProperties: false |
| 42 | 46 | |
| @@ -46,6 +50,7 @@ examples: · 3 unmodified lines | ||
| 46 | 50 | compatible = "microchip,mpfs-can"; |
| 47 | 51 | reg = <0x2010c000 0x1000>; |
| 48 | 52 | clocks = <&clkcfg 17>, <&clkcfg 37>; |
| 53 | + resets = <&clkcfg 17>; | |
| 49 | 54 | interrupt-parent = <&plic>; |
| 50 | 55 | interrupts = <56>; |
| 51 | 56 | }; |
| @@ -15412,14 +15412,12 @@ S: Supported · 15411 unmodified lines | ||
| 15412 | 15412 | F: drivers/net/phy/mxl-86110.c |
| 15413 | 15413 | F: drivers/net/phy/mxl-gpy.c |
| 15414 | 15414 | |
| 15415 | −MCAN MMIO DEVICE DRIVER | |
| 15416 | −M: Chandrasekar Ramakrishnan <rcsekar@samsung.com> | |
| 15415 | +MCAN DEVICE DRIVER | |
| 15416 | +M: Markus Schneider-Pargmann <msp@baylibre.com> | |
| 15417 | 15417 | L: linux-can@vger.kernel.org |
| 15418 | 15418 | S: Maintained |
| 15419 | 15419 | F: Documentation/devicetree/bindings/net/can/bosch,m_can.yaml |
| 15420 | −F: drivers/net/can/m_can/m_can.c | |
| 15421 | −F: drivers/net/can/m_can/m_can.h | |
| 15422 | −F: drivers/net/can/m_can/m_can_platform.c | |
| 15420 | +F: drivers/net/can/m_can/ | |
| 15423 | 15421 | |
| 15424 | 15422 | MCBA MICROCHIP CAN BUS ANALYZER TOOL DRIVER |
| 15425 | 15423 | R: Yasushi SHOJI <yashi@spacecubics.com> |
| @@ -124,6 +124,23 @@ config CAN_CAN327 · 123 unmodified lines | ||
| 124 | 124 | |
| 125 | 125 | If this driver is built as a module, it will be called can327. |
| 126 | 126 | |
| 127 | +config CAN_DUMMY | |
| 128 | + tristate "Dummy CAN" | |
| 129 | + help | |
| 130 | + A dummy CAN module supporting Classical CAN, CAN FD and CAN XL. It | |
| 131 | + exposes bittiming values which can be configured through the netlink | |
| 132 | + interface. | |
| 133 | + | |
| 134 | + The module will simply echo any frame sent to it. If debug messages | |
| 135 | + are activated, it prints all the CAN bittiming information in the | |
| 136 | + kernel log. Aside from that it does nothing. | |
| 137 | + | |
| 138 | + This is convenient for testing the CAN netlink interface. Most of the | |
| 139 | + users will never need this. If unsure, say NO. | |
| 140 | + | |
| 141 | + To compile this driver as a module, choose M here: the module will be | |
| 142 | + called dummy-can. | |
| 143 | + | |
| 127 | 144 | config CAN_FLEXCAN |
| 128 | 145 | tristate "Support for Freescale FLEXCAN based chips" |
| 129 | 146 | depends on OF || COLDFIRE || COMPILE_TEST |
| @@ -21,6 +21,7 @@ obj-$(CONFIG_CAN_CAN327) += can327.o · 20 unmodified lines | ||
| 21 | 21 | obj-$(CONFIG_CAN_CC770) += cc770/ |
| 22 | 22 | obj-$(CONFIG_CAN_C_CAN) += c_can/ |
| 23 | 23 | obj-$(CONFIG_CAN_CTUCANFD) += ctucanfd/ |
| 24 | +obj-$(CONFIG_CAN_DUMMY) += dummy_can.o | |
| 24 | 25 | obj-$(CONFIG_CAN_FLEXCAN) += flexcan/ |
| 25 | 26 | obj-$(CONFIG_CAN_GRCAN) += grcan.o |
| 26 | 27 | obj-$(CONFIG_CAN_IFI_CANFD) += ifi_canfd/ |
| @@ -2,6 +2,7 @@ · 1 unmodified line | ||
| 2 | 2 | /* Copyright (C) 2005 Marc Kleine-Budde, Pengutronix |
| 3 | 3 | * Copyright (C) 2006 Andrey Volkov, Varma Electronics |
| 4 | 4 | * Copyright (C) 2008-2009 Wolfgang Grandegger <wg@grandegger.com> |
| 5 | + * Copyright (c) 2025 Vincent Mailhol <mailhol@kernel.org> | |
| 5 | 6 | */ |
| 6 | 7 | |
| 7 | 8 | #include <linux/can/dev.h> |
| @@ -151,3 +152,65 @@ int can_get_bittiming(const struct net_device *dev, struct can_bittiming *bt, · 143 unmodified lines | ||
| 151 | 152 | |
| 152 | 153 | return -EINVAL; |
| 153 | 154 | } |
| 155 | + | |
| 156 | +int can_validate_pwm_bittiming(const struct net_device *dev, | |
| 157 | + const struct can_pwm *pwm, | |
| 158 | + struct netlink_ext_ack *extack) | |
| 159 | +{ | |
| 160 | + const struct can_priv *priv = netdev_priv(dev); | |
| 161 | + u32 xl_bit_time_tqmin = can_bit_time_tqmin(&priv->xl.data_bittiming); | |
| 162 | + u32 nom_bit_time_tqmin = can_bit_time_tqmin(&priv->bittiming); | |
| 163 | + u32 pwms_ns = can_tqmin_to_ns(pwm->pwms, priv->clock.freq); | |
| 164 | + u32 pwml_ns = can_tqmin_to_ns(pwm->pwml, priv->clock.freq); | |
| 165 | + | |
| 166 | + if (pwms_ns + pwml_ns > CAN_PWM_NS_MAX) { | |
| 167 | + NL_SET_ERR_MSG_FMT(extack, | |
| 168 | + "The PWM symbol duration: %u ns may not exceed %u ns", | |
| 169 | + pwms_ns + pwml_ns, CAN_PWM_NS_MAX); | |
| 170 | + return -EINVAL; | |
| 171 | + } | |
| 172 | + | |
| 173 | + if (pwms_ns < CAN_PWM_DECODE_NS) { | |
| 174 | + NL_SET_ERR_MSG_FMT(extack, | |
| 175 | + "PWMS: %u ns shall be at least %u ns", | |
| 176 | + pwms_ns, CAN_PWM_DECODE_NS); | |
| 177 | + return -EINVAL; | |
| 178 | + } | |
| 179 | + | |
| 180 | + if (pwm->pwms >= pwm->pwml) { | |
| 181 | + NL_SET_ERR_MSG_FMT(extack, | |
| 182 | + "PWMS: %u tqmin shall be smaller than PWML: %u tqmin", | |
| 183 | + pwm->pwms, pwm->pwml); | |
| 184 | + return -EINVAL; | |
| 185 | + } | |
| 186 | + | |
| 187 | + if (pwml_ns - pwms_ns < 2 * CAN_PWM_DECODE_NS) { | |
| 188 | + NL_SET_ERR_MSG_FMT(extack, | |
| 189 | + "At least %u ns shall separate PWMS: %u ns from PMWL: %u ns", | |
| 190 | + 2 * CAN_PWM_DECODE_NS, pwms_ns, pwml_ns); | |
| 191 | + return -EINVAL; | |
| 192 | + } | |
| 193 | + | |
| 194 | + if (xl_bit_time_tqmin % (pwm->pwms + pwm->pwml) != 0) { | |
| 195 | + NL_SET_ERR_MSG_FMT(extack, | |
| 196 | + "PWM duration: %u tqmin does not divide XL's bit time: %u tqmin", | |
| 197 | + pwm->pwms + pwm->pwml, xl_bit_time_tqmin); | |
| 198 | + return -EINVAL; | |
| 199 | + } | |
| 200 | + | |
| 201 | + if (pwm->pwmo >= pwm->pwms + pwm->pwml) { | |
| 202 | + NL_SET_ERR_MSG_FMT(extack, | |
| 203 | + "PWMO: %u tqmin can not be greater than PWMS + PWML: %u tqmin", | |
| 204 | + pwm->pwmo, pwm->pwms + pwm->pwml); | |
| 205 | + return -EINVAL; | |
| 206 | + } | |
| 207 | + | |
| 208 | + if (nom_bit_time_tqmin % (pwm->pwms + pwm->pwml) != pwm->pwmo) { | |
| 209 | + NL_SET_ERR_MSG_FMT(extack, | |
| 210 | + "Can not assemble nominal bit time: %u tqmin out of PWMS + PMWL and PWMO", | |
| 211 | + nom_bit_time_tqmin); | |
| 212 | + return -EINVAL; | |
| 213 | + } | |
| 214 | + | |
| 215 | + return 0; | |
| 216 | +} | |
| @@ -2,6 +2,7 @@ · 1 unmodified line | ||
| 2 | 2 | /* Copyright (C) 2005 Marc Kleine-Budde, Pengutronix |
| 3 | 3 | * Copyright (C) 2006 Andrey Volkov, Varma Electronics |
| 4 | 4 | * Copyright (C) 2008-2009 Wolfgang Grandegger <wg@grandegger.com> |
| 5 | + * Copyright (C) 2021-2025 Vincent Mailhol <mailhol@kernel.org> | |
| 5 | 6 | */ |
| 6 | 7 | |
| 7 | 8 | #include <linux/units.h> |
| @@ -9,6 +10,33 @@ · 1 unmodified line | ||
| 9 | 10 | |
| 10 | 11 | #define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */ |
| 11 | 12 | |
| 13 | +/* CiA recommended sample points for Non Return to Zero encoding. */ | |
| 14 | +static int can_calc_sample_point_nrz(const struct can_bittiming *bt) | |
| 15 | +{ | |
| 16 | + if (bt->bitrate > 800 * KILO /* BPS */) | |
| 17 | + return 750; | |
| 18 | + | |
| 19 | + if (bt->bitrate > 500 * KILO /* BPS */) | |
| 20 | + return 800; | |
| 21 | + | |
| 22 | + return 875; | |
| 23 | +} | |
| 24 | + | |
| 25 | +/* Sample points for Pulse-Width Modulation encoding. */ | |
| 26 | +static int can_calc_sample_point_pwm(const struct can_bittiming *bt) | |
| 27 | +{ | |
| 28 | + if (bt->bitrate > 15 * MEGA /* BPS */) | |
| 29 | + return 625; | |
| 30 | + | |
| 31 | + if (bt->bitrate > 9 * MEGA /* BPS */) | |
| 32 | + return 600; | |
| 33 | + | |
| 34 | + if (bt->bitrate > 4 * MEGA /* BPS */) | |
| 35 | + return 560; | |
| 36 | + | |
| 37 | + return 520; | |
| 38 | +} | |
| 39 | + | |
| 12 | 40 | /* Bit-timing calculation derived from: |
| 13 | 41 | * |
| 14 | 42 | * Code based on LinCAN sources and H8S2638 project |
| @@ -23,7 +51,7 @@ · 8 unmodified lines | ||
| 23 | 51 | */ |
| 24 | 52 | static int |
| 25 | 53 | can_update_sample_point(const struct can_bittiming_const *btc, |
| 26 | − const unsigned int sample_point_nominal, const unsigned int tseg, | |
| 54 | + const unsigned int sample_point_reference, const unsigned int tseg, | |
| 27 | 55 | unsigned int *tseg1_ptr, unsigned int *tseg2_ptr, |
| 28 | 56 | unsigned int *sample_point_error_ptr) |
| 29 | 57 | { |
| @@ -34,7 +62,7 @@ can_update_sample_point(const struct can_bittiming_const *btc, · 4 unmodified lines | ||
| 34 | 62 | |
| 35 | 63 | for (i = 0; i <= 1; i++) { |
| 36 | 64 | tseg2 = tseg + CAN_SYNC_SEG - |
| 37 | − (sample_point_nominal * (tseg + CAN_SYNC_SEG)) / | |
| 65 | + (sample_point_reference * (tseg + CAN_SYNC_SEG)) / | |
| 38 | 66 | 1000 - i; |
| 39 | 67 | tseg2 = clamp(tseg2, btc->tseg2_min, btc->tseg2_max); |
| 40 | 68 | tseg1 = tseg - tseg2; |
| @@ -45,9 +73,9 @@ can_update_sample_point(const struct can_bittiming_const *btc, · 4 unmodified lines | ||
| 45 | 73 | |
| 46 | 74 | sample_point = 1000 * (tseg + CAN_SYNC_SEG - tseg2) / |
| 47 | 75 | (tseg + CAN_SYNC_SEG); |
| 48 | − sample_point_error = abs(sample_point_nominal - sample_point); | |
| 76 | + sample_point_error = abs(sample_point_reference - sample_point); | |
| 49 | 77 | |
| 50 | − if (sample_point <= sample_point_nominal && | |
| 78 | + if (sample_point <= sample_point_reference && | |
| 51 | 79 | sample_point_error < best_sample_point_error) { |
| 52 | 80 | best_sample_point = sample_point; |
| 53 | 81 | best_sample_point_error = sample_point_error; |
| @@ -67,28 +95,24 @@ int can_calc_bittiming(const struct net_device *dev, struct can_bittiming *bt, · 13 unmodified lines | ||
| 67 | 95 | { |
| 68 | 96 | struct can_priv *priv = netdev_priv(dev); |
| 69 | 97 | unsigned int bitrate; /* current bitrate */ |
| 70 | − unsigned int bitrate_error; /* difference between current and nominal value */ | |
| 98 | + unsigned int bitrate_error; /* diff between calculated and reference value */ | |
| 71 | 99 | unsigned int best_bitrate_error = UINT_MAX; |
| 72 | − unsigned int sample_point_error; /* difference between current and nominal value */ | |
| 100 | + unsigned int sample_point_error; /* diff between calculated and reference value */ | |
| 73 | 101 | unsigned int best_sample_point_error = UINT_MAX; |
| 74 | − unsigned int sample_point_nominal; /* nominal sample point */ | |
| 102 | + unsigned int sample_point_reference; /* reference sample point */ | |
| 75 | 103 | unsigned int best_tseg = 0; /* current best value for tseg */ |
| 76 | 104 | unsigned int best_brp = 0; /* current best value for brp */ |
| 77 | 105 | unsigned int brp, tsegall, tseg, tseg1 = 0, tseg2 = 0; |
| 78 | 106 | u64 v64; |
| 79 | 107 | int err; |
| 80 | 108 | |
| 81 | − /* Use CiA recommended sample points */ | |
| 82 | − if (bt->sample_point) { | |
| 83 | − sample_point_nominal = bt->sample_point; | |
| 84 | − } else { | |
| 85 | − if (bt->bitrate > 800 * KILO /* BPS */) | |
| 86 | − sample_point_nominal = 750; | |
| 87 | − else if (bt->bitrate > 500 * KILO /* BPS */) | |
| 88 | − sample_point_nominal = 800; | |
| 89 | − else | |
| 90 | − sample_point_nominal = 875; | |
| 91 | − } | |
| 109 | + if (bt->sample_point) | |
| 110 | + sample_point_reference = bt->sample_point; | |
| 111 | + else if (btc == priv->xl.data_bittiming_const && | |
| 112 | + (priv->ctrlmode & CAN_CTRLMODE_XL_TMS)) | |
| 113 | + sample_point_reference = can_calc_sample_point_pwm(bt); | |
| 114 | + else | |
| 115 | + sample_point_reference = can_calc_sample_point_nrz(bt); | |
| 92 | 116 | |
| 93 | 117 | /* tseg even = round down, odd = round up */ |
| 94 | 118 | for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1; |
| @@ -114,7 +138,7 @@ int can_calc_bittiming(const struct net_device *dev, struct can_bittiming *bt, · 19 unmodified lines | ||
| 114 | 138 | if (bitrate_error < best_bitrate_error) |
| 115 | 139 | best_sample_point_error = UINT_MAX; |
| 116 | 140 | |
| 117 | − can_update_sample_point(btc, sample_point_nominal, tseg / 2, | |
| 141 | + can_update_sample_point(btc, sample_point_reference, tseg / 2, | |
| 118 | 142 | &tseg1, &tseg2, &sample_point_error); |
| 119 | 143 | if (sample_point_error >= best_sample_point_error) |
| 120 | 144 | continue; |
| @@ -129,23 +153,26 @@ int can_calc_bittiming(const struct net_device *dev, struct can_bittiming *bt, · 8 unmodified lines | ||
| 129 | 153 | } |
| 130 | 154 | |
| 131 | 155 | if (best_bitrate_error) { |
| 132 | − /* Error in one-tenth of a percent */ | |
| 133 | − v64 = (u64)best_bitrate_error * 1000; | |
| 156 | + /* Error in one-hundredth of a percent */ | |
| 157 | + v64 = (u64)best_bitrate_error * 10000; | |
| 134 | 158 | do_div(v64, bt->bitrate); |
| 135 | 159 | bitrate_error = (u32)v64; |
| 160 | + /* print at least 0.01% if the error is smaller */ | |
| 161 | + bitrate_error = max(bitrate_error, 1U); | |
| 136 | 162 | if (bitrate_error > CAN_CALC_MAX_ERROR) { |
| 137 | 163 | NL_SET_ERR_MSG_FMT(extack, |
| 138 | − "bitrate error: %u.%u%% too high", | |
| 139 | − bitrate_error / 10, bitrate_error % 10); | |
| 164 | + "bitrate error: %u.%02u%% too high", | |
| 165 | + bitrate_error / 100, | |
| 166 | + bitrate_error % 100); | |
| 140 | 167 | return -EINVAL; |
| 141 | 168 | } |
| 142 | 169 | NL_SET_ERR_MSG_FMT(extack, |
| 143 | − "bitrate error: %u.%u%%", | |
| 144 | − bitrate_error / 10, bitrate_error % 10); | |
| 170 | + "bitrate error: %u.%02u%%", | |
| 171 | + bitrate_error / 100, bitrate_error % 100); | |
| 145 | 172 | } |
| 146 | 173 | |
| 147 | 174 | /* real sample point */ |
| 148 | − bt->sample_point = can_update_sample_point(btc, sample_point_nominal, | |
| 175 | + bt->sample_point = can_update_sample_point(btc, sample_point_reference, | |
| 149 | 176 | best_tseg, &tseg1, &tseg2, |
| 150 | 177 | NULL); |
| 151 | 178 | |
| @@ -198,3 +225,38 @@ void can_calc_tdco(struct can_tdc *tdc, const struct can_tdc_const *tdc_const, · 46 unmodified lines | ||
| 198 | 225 | *ctrlmode |= tdc_auto; |
| 199 | 226 | } |
| 200 | 227 | } |
| 228 | + | |
| 229 | +int can_calc_pwm(struct net_device *dev, struct netlink_ext_ack *extack) | |
| 230 | +{ | |
| 231 | + struct can_priv *priv = netdev_priv(dev); | |
| 232 | + const struct can_pwm_const *pwm_const = priv->xl.pwm_const; | |
| 233 | + struct can_pwm *pwm = &priv->xl.pwm; | |
| 234 | + u32 xl_tqmin = can_bit_time_tqmin(&priv->xl.data_bittiming); | |
| 235 | + u32 xl_ns = can_tqmin_to_ns(xl_tqmin, priv->clock.freq); | |
| 236 | + u32 nom_tqmin = can_bit_time_tqmin(&priv->bittiming); | |
| 237 | + int pwm_per_bit_max = xl_tqmin / (pwm_const->pwms_min + pwm_const->pwml_min); | |
| 238 | + int pwm_per_bit; | |
| 239 | + u32 pwm_tqmin; | |
| 240 | + | |
| 241 | + /* For 5 MB/s databitrate or greater, xl_ns < CAN_PWM_NS_MAX | |
| 242 | + * giving us a pwm_per_bit of 1 and the loop immediately breaks | |
| 243 | + */ | |
| 244 | + for (pwm_per_bit = DIV_ROUND_UP(xl_ns, CAN_PWM_NS_MAX); | |
| 245 | + pwm_per_bit <= pwm_per_bit_max; pwm_per_bit++) | |
| 246 | + if (xl_tqmin % pwm_per_bit == 0) | |
| 247 | + break; | |
| 248 | + | |
| 249 | + if (pwm_per_bit > pwm_per_bit_max) { | |
| 250 | + NL_SET_ERR_MSG_FMT(extack, | |
| 251 | + "Can not divide the XL data phase's bit time: %u tqmin into multiple PWM symbols", | |
| 252 | + xl_tqmin); | |
| 253 | + return -EINVAL; | |
| 254 | + } | |
| 255 | + | |
| 256 | + pwm_tqmin = xl_tqmin / pwm_per_bit; | |
| 257 | + pwm->pwms = DIV_ROUND_UP_POW2(pwm_tqmin, 4); | |
| 258 | + pwm->pwml = pwm_tqmin - pwm->pwms; | |
| 259 | + pwm->pwmo = nom_tqmin % pwm_tqmin; | |
| 260 | + | |
| 261 | + return 0; | |
| 262 | +} | |
| @@ -92,29 +92,39 @@ const char *can_get_ctrlmode_str(u32 ctrlmode) · 91 unmodified lines | ||
| 92 | 92 | { |
| 93 | 93 | switch (ctrlmode & ~(ctrlmode - 1)) { |
| 94 | 94 | case 0: |
| 95 | − return "none"; | |
| 95 | + return "(none)"; | |
| 96 | 96 | case CAN_CTRLMODE_LOOPBACK: |
| 97 | − return "loopback"; | |
| 97 | + return "LOOPBACK"; | |
| 98 | 98 | case CAN_CTRLMODE_LISTENONLY: |
| 99 | − return "listen-only"; | |
| 99 | + return "LISTEN-ONLY"; | |
| 100 | 100 | case CAN_CTRLMODE_3_SAMPLES: |
| 101 | − return "triple-sampling"; | |
| 101 | + return "TRIPLE-SAMPLING"; | |
| 102 | 102 | case CAN_CTRLMODE_ONE_SHOT: |
| 103 | − return "one-shot"; | |
| 103 | + return "ONE-SHOT"; | |
| 104 | 104 | case CAN_CTRLMODE_BERR_REPORTING: |
| 105 | − return "berr-reporting"; | |
| 105 | + return "BERR-REPORTING"; | |
| 106 | 106 | case CAN_CTRLMODE_FD: |
| 107 | − return "fd"; | |
| 107 | + return "FD"; | |
| 108 | 108 | case CAN_CTRLMODE_PRESUME_ACK: |
| 109 | − return "presume-ack"; | |
| 109 | + return "PRESUME-ACK"; | |
| 110 | 110 | case CAN_CTRLMODE_FD_NON_ISO: |
| 111 | − return "fd-non-iso"; | |
| 111 | + return "FD-NON-ISO"; | |
| 112 | 112 | case CAN_CTRLMODE_CC_LEN8_DLC: |
| 113 | − return "cc-len8-dlc"; | |
| 113 | + return "CC-LEN8-DLC"; | |
| 114 | 114 | case CAN_CTRLMODE_TDC_AUTO: |
| 115 | − return "fd-tdc-auto"; | |
| 115 | + return "TDC-AUTO"; | |
| 116 | 116 | case CAN_CTRLMODE_TDC_MANUAL: |
| 117 | − return "fd-tdc-manual"; | |
| 117 | + return "TDC-MANUAL"; | |
| 118 | + case CAN_CTRLMODE_RESTRICTED: | |
| 119 | + return "RESTRICTED"; | |
| 120 | + case CAN_CTRLMODE_XL: | |
| 121 | + return "XL"; | |
| 122 | + case CAN_CTRLMODE_XL_TDC_AUTO: | |
| 123 | + return "XL-TDC-AUTO"; | |
| 124 | + case CAN_CTRLMODE_XL_TDC_MANUAL: | |
| 125 | + return "XL-TDC-MANUAL"; | |
| 126 | + case CAN_CTRLMODE_XL_TMS: | |
| 127 | + return "TMS"; | |
| 118 | 128 | default: |
| 119 | 129 | return "<unknown>"; |
| 120 | 130 | } |
| @@ -348,7 +358,13 @@ void can_set_default_mtu(struct net_device *dev) · 227 unmodified lines | ||
| 348 | 358 | { |
| 349 | 359 | struct can_priv *priv = netdev_priv(dev); |
| 350 | 360 | |
| 351 | − if (priv->ctrlmode & CAN_CTRLMODE_FD) { | |
| 361 | + if (priv->ctrlmode & CAN_CTRLMODE_XL) { | |
| 362 | + if (can_is_canxl_dev_mtu(dev->mtu)) | |
| 363 | + return; | |
| 364 | + dev->mtu = CANXL_MTU; | |
| 365 | + dev->min_mtu = CANXL_MIN_MTU; | |
| 366 | + dev->max_mtu = CANXL_MAX_MTU; | |
| 367 | + } else if (priv->ctrlmode & CAN_CTRLMODE_FD) { | |
| 352 | 368 | dev->mtu = CANFD_MTU; |
| 353 | 369 | dev->min_mtu = CANFD_MTU; |
| 354 | 370 | dev->max_mtu = CANFD_MTU; |
| @@ -2,7 +2,7 @@ · 1 unmodified line | ||
| 2 | 2 | /* Copyright (C) 2005 Marc Kleine-Budde, Pengutronix |
| 3 | 3 | * Copyright (C) 2006 Andrey Volkov, Varma Electronics |
| 4 | 4 | * Copyright (C) 2008-2009 Wolfgang Grandegger <wg@grandegger.com> |
| 5 | − * Copyright (C) 2021 Vincent Mailhol <mailhol.vincent@wanadoo.fr> | |
| 5 | + * Copyright (C) 2021-2025 Vincent Mailhol <mailhol@kernel.org> | |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | #include <linux/can/dev.h> |
| @@ -22,6 +22,10 @@ static const struct nla_policy can_policy[IFLA_CAN_MAX + 1] = { · 13 unmodified lines | ||
| 22 | 22 | [IFLA_CAN_TERMINATION] = { .type = NLA_U16 }, |
| 23 | 23 | [IFLA_CAN_TDC] = { .type = NLA_NESTED }, |
| 24 | 24 | [IFLA_CAN_CTRLMODE_EXT] = { .type = NLA_NESTED }, |
| 25 | + [IFLA_CAN_XL_DATA_BITTIMING] = { .len = sizeof(struct can_bittiming) }, | |
| 26 | + [IFLA_CAN_XL_DATA_BITTIMING_CONST] = { .len = sizeof(struct can_bittiming_const) }, | |
| 27 | + [IFLA_CAN_XL_TDC] = { .type = NLA_NESTED }, | |
| 28 | + [IFLA_CAN_XL_PWM] = { .type = NLA_NESTED }, | |
| 25 | 29 | }; |
| 26 | 30 | |
| 27 | 31 | static const struct nla_policy can_tdc_policy[IFLA_CAN_TDC_MAX + 1] = { |
| @@ -36,6 +40,18 @@ static const struct nla_policy can_tdc_policy[IFLA_CAN_TDC_MAX + 1] = { · 8 unmodified lines | ||
| 36 | 40 | [IFLA_CAN_TDC_TDCF] = { .type = NLA_U32 }, |
| 37 | 41 | }; |
| 38 | 42 | |
| 43 | +static const struct nla_policy can_pwm_policy[IFLA_CAN_PWM_MAX + 1] = { | |
| 44 | + [IFLA_CAN_PWM_PWMS_MIN] = { .type = NLA_U32 }, | |
| 45 | + [IFLA_CAN_PWM_PWMS_MAX] = { .type = NLA_U32 }, | |
| 46 | + [IFLA_CAN_PWM_PWML_MIN] = { .type = NLA_U32 }, | |
| 47 | + [IFLA_CAN_PWM_PWML_MAX] = { .type = NLA_U32 }, | |
| 48 | + [IFLA_CAN_PWM_PWMO_MIN] = { .type = NLA_U32 }, | |
| 49 | + [IFLA_CAN_PWM_PWMO_MAX] = { .type = NLA_U32 }, | |
| 50 | + [IFLA_CAN_PWM_PWMS] = { .type = NLA_U32 }, | |
| 51 | + [IFLA_CAN_PWM_PWML] = { .type = NLA_U32 }, | |
| 52 | + [IFLA_CAN_PWM_PWMO] = { .type = NLA_U32 }, | |
| 53 | +}; | |
| 54 | + | |
| 39 | 55 | static int can_validate_bittiming(struct nlattr *data[], |
| 40 | 56 | struct netlink_ext_ack *extack, |
| 41 | 57 | int ifla_can_bittiming) |
| @@ -70,7 +86,7 @@ static int can_validate_tdc(struct nlattr *data_tdc, · 28 unmodified lines | ||
| 70 | 86 | return -EOPNOTSUPP; |
| 71 | 87 | } |
| 72 | 88 | |
| 73 | − /* If one of the CAN_CTRLMODE_TDC_* flag is set then TDC | |
| 89 | + /* If one of the CAN_CTRLMODE_{,XL}_TDC_* flags is set then TDC | |
| 74 | 90 | * must be set and vice-versa |
| 75 | 91 | */ |
| 76 | 92 | if ((tdc_auto || tdc_manual) && !data_tdc) { |
| @@ -82,8 +98,8 @@ static int can_validate_tdc(struct nlattr *data_tdc, · 5 unmodified lines | ||
| 82 | 98 | return -EOPNOTSUPP; |
| 83 | 99 | } |
| 84 | 100 | |
| 85 | − /* If providing TDC parameters, at least TDCO is needed. TDCV | |
| 86 | − * is needed if and only if CAN_CTRLMODE_TDC_MANUAL is set | |
| 101 | + /* If providing TDC parameters, at least TDCO is needed. TDCV is | |
| 102 | + * needed if and only if CAN_CTRLMODE_{,XL}_TDC_MANUAL is set | |
| 87 | 103 | */ |
| 88 | 104 | if (data_tdc) { |
| 89 | 105 | struct nlattr *tb_tdc[IFLA_CAN_TDC_MAX + 1]; |
| @@ -116,6 +132,40 @@ static int can_validate_tdc(struct nlattr *data_tdc, · 26 unmodified lines | ||
| 116 | 132 | return 0; |
| 117 | 133 | } |
| 118 | 134 | |
| 135 | +static int can_validate_pwm(struct nlattr *data[], | |
| 136 | + struct netlink_ext_ack *extack, u32 flags) | |
| 137 | +{ | |
| 138 | + struct nlattr *tb_pwm[IFLA_CAN_PWM_MAX + 1]; | |
| 139 | + int err; | |
| 140 | + | |
| 141 | + if (!data[IFLA_CAN_XL_PWM]) | |
| 142 | + return 0; | |
| 143 | + | |
| 144 | + if (!(flags & CAN_CTRLMODE_XL_TMS)) { | |
| 145 | + NL_SET_ERR_MSG(extack, "PWM requires TMS"); | |
| 146 | + return -EOPNOTSUPP; | |
| 147 | + } | |
| 148 | + | |
| 149 | + err = nla_parse_nested(tb_pwm, IFLA_CAN_PWM_MAX, data[IFLA_CAN_XL_PWM], | |
| 150 | + can_pwm_policy, extack); | |
| 151 | + if (err) | |
| 152 | + return err; | |
| 153 | + | |
| 154 | + if (!tb_pwm[IFLA_CAN_PWM_PWMS] != !tb_pwm[IFLA_CAN_PWM_PWML]) { | |
| 155 | + NL_SET_ERR_MSG(extack, | |
| 156 | + "Provide either both PWMS and PWML, or none for automatic calculation"); | |
| 157 | + return -EOPNOTSUPP; | |
| 158 | + } | |
| 159 | + | |
| 160 | + if (tb_pwm[IFLA_CAN_PWM_PWMO] && | |
| 161 | + (!tb_pwm[IFLA_CAN_PWM_PWMS] || !tb_pwm[IFLA_CAN_PWM_PWML])) { | |
| 162 | + NL_SET_ERR_MSG(extack, "PWMO requires both PWMS and PWML"); | |
| 163 | + return -EOPNOTSUPP; | |
| 164 | + } | |
| 165 | + | |
| 166 | + return 0; | |
| 167 | +} | |
| 168 | + | |
| 119 | 169 | static int can_validate_databittiming(struct nlattr *data[], |
| 120 | 170 | struct netlink_ext_ack *extack, |
| 121 | 171 | int ifla_can_data_bittiming, u32 flags) |
| @@ -126,10 +176,10 @@ static int can_validate_databittiming(struct nlattr *data[], · 4 unmodified lines | ||
| 126 | 176 | bool is_on; |
| 127 | 177 | int err; |
| 128 | 178 | |
| 129 | − /* Make sure that valid CAN FD configurations always consist of | |
| 179 | + /* Make sure that valid CAN FD/XL configurations always consist of | |
| 130 | 180 | * - nominal/arbitration bittiming |
| 131 | 181 | * - data bittiming |
| 132 | − * - control mode with CAN_CTRLMODE_FD set | |
| 182 | + * - control mode with CAN_CTRLMODE_{FD,XL} set | |
| 133 | 183 | * - TDC parameters are coherent (details in can_validate_tdc()) |
| 134 | 184 | */ |
| 135 | 185 | |
| @@ -139,7 +189,10 @@ static int can_validate_databittiming(struct nlattr *data[], · 3 unmodified lines | ||
| 139 | 189 | is_on = flags & CAN_CTRLMODE_FD; |
| 140 | 190 | type = "FD"; |
| 141 | 191 | } else { |
| 142 | − return -EOPNOTSUPP; /* Place holder for CAN XL */ | |
| 192 | + data_tdc = data[IFLA_CAN_XL_TDC]; | |
| 193 | + tdc_flags = flags & CAN_CTRLMODE_XL_TDC_MASK; | |
| 194 | + is_on = flags & CAN_CTRLMODE_XL; | |
| 195 | + type = "XL"; | |
| 143 | 196 | } |
| 144 | 197 | |
| 145 | 198 | if (is_on) { |
| @@ -175,6 +228,32 @@ static int can_validate_databittiming(struct nlattr *data[], · 29 unmodified lines | ||
| 175 | 228 | return 0; |
| 176 | 229 | } |
| 177 | 230 | |
| 231 | +static int can_validate_xl_flags(struct netlink_ext_ack *extack, | |
| 232 | + u32 masked_flags, u32 mask) | |
| 233 | +{ | |
| 234 | + if (masked_flags & CAN_CTRLMODE_XL) { | |
| 235 | + if (masked_flags & CAN_CTRLMODE_XL_TMS) { | |
| 236 | + const u32 tms_conflicts_mask = CAN_CTRLMODE_FD | | |
| 237 | + CAN_CTRLMODE_XL_TDC_MASK; | |
| 238 | + u32 tms_conflicts = masked_flags & tms_conflicts_mask; | |
| 239 | + | |
| 240 | + if (tms_conflicts) { | |
| 241 | + NL_SET_ERR_MSG_FMT(extack, | |
| 242 | + "TMS and %s are mutually exclusive", | |
| 243 | + can_get_ctrlmode_str(tms_conflicts)); | |
| 244 | + return -EOPNOTSUPP; | |
| 245 | + } | |
| 246 | + } | |
| 247 | + } else { | |
| 248 | + if (mask & CAN_CTRLMODE_XL_TMS) { | |
| 249 | + NL_SET_ERR_MSG(extack, "TMS requires CAN XL"); | |
| 250 | + return -EOPNOTSUPP; | |
| 251 | + } | |
| 252 | + } | |
| 253 | + | |
| 254 | + return 0; | |
| 255 | +} | |
| 256 | + | |
| 178 | 257 | static int can_validate(struct nlattr *tb[], struct nlattr *data[], |
| 179 | 258 | struct netlink_ext_ack *extack) |
| 180 | 259 | { |
| @@ -188,6 +267,17 @@ static int can_validate(struct nlattr *tb[], struct nlattr *data[], · 7 unmodified lines | ||
| 188 | 267 | struct can_ctrlmode *cm = nla_data(data[IFLA_CAN_CTRLMODE]); |
| 189 | 268 | |
| 190 | 269 | flags = cm->flags & cm->mask; |
| 270 | + | |
| 271 | + if ((flags & CAN_CTRLMODE_LISTENONLY) && | |
| 272 | + (flags & CAN_CTRLMODE_RESTRICTED)) { | |
| 273 | + NL_SET_ERR_MSG(extack, | |
| 274 | + "LISTEN-ONLY and RESTRICTED modes are mutually exclusive"); | |
| 275 | + return -EOPNOTSUPP; | |
| 276 | + } | |
| 277 | + | |
| 278 | + err = can_validate_xl_flags(extack, flags, cm->mask); | |
| 279 | + if (err) | |
| 280 | + return err; | |
| 191 | 281 | } |
| 192 | 282 | |
| 193 | 283 | err = can_validate_bittiming(data, extack, IFLA_CAN_BITTIMING); |
| @@ -199,6 +289,15 @@ static int can_validate(struct nlattr *tb[], struct nlattr *data[], · 5 unmodified lines | ||
| 199 | 289 | if (err) |
| 200 | 290 | return err; |
| 201 | 291 | |
| 292 | + err = can_validate_databittiming(data, extack, | |
| 293 | + IFLA_CAN_XL_DATA_BITTIMING, flags); | |
| 294 | + if (err) | |
| 295 | + return err; | |
| 296 | + | |
| 297 | + err = can_validate_pwm(data, extack, flags); | |
| 298 | + if (err) | |
| 299 | + return err; | |
| 300 | + | |
| 202 | 301 | return 0; |
| 203 | 302 | } |
| 204 | 303 | |
| @@ -208,7 +307,7 @@ static int can_ctrlmode_changelink(struct net_device *dev, · 3 unmodified lines | ||
| 208 | 307 | { |
| 209 | 308 | struct can_priv *priv = netdev_priv(dev); |
| 210 | 309 | struct can_ctrlmode *cm; |
| 211 | − u32 ctrlstatic, maskedflags, notsupp, ctrlstatic_missing; | |
| 310 | + u32 ctrlstatic, maskedflags, deactivated, notsupp, ctrlstatic_missing; | |
| 212 | 311 | |
| 213 | 312 | if (!data[IFLA_CAN_CTRLMODE]) |
| 214 | 313 | return 0; |
| @@ -220,6 +319,7 @@ static int can_ctrlmode_changelink(struct net_device *dev, · 5 unmodified lines | ||
| 220 | 319 | cm = nla_data(data[IFLA_CAN_CTRLMODE]); |
| 221 | 320 | ctrlstatic = can_get_static_ctrlmode(priv); |
| 222 | 321 | maskedflags = cm->flags & cm->mask; |
| 322 | + deactivated = ~cm->flags & cm->mask; | |
| 223 | 323 | notsupp = maskedflags & ~(priv->ctrlmode_supported | ctrlstatic); |
| 224 | 324 | ctrlstatic_missing = (maskedflags & ctrlstatic) ^ ctrlstatic; |
| 225 | 325 | |
| @@ -241,21 +341,40 @@ static int can_ctrlmode_changelink(struct net_device *dev, · 15 unmodified lines | ||
| 241 | 341 | return -EOPNOTSUPP; |
| 242 | 342 | } |
| 243 | 343 | |
| 344 | + /* If FD was active and is not turned off, check for XL conflicts */ | |
| 345 | + if (priv->ctrlmode & CAN_CTRLMODE_FD & ~deactivated) { | |
| 346 | + if (maskedflags & CAN_CTRLMODE_XL_TMS) { | |
| 347 | + NL_SET_ERR_MSG(extack, | |
| 348 | + "TMS can not be activated while CAN FD is on"); | |
| 349 | + return -EOPNOTSUPP; | |
| 350 | + } | |
| 351 | + } | |
| 352 | + | |
| 244 | 353 | /* If a top dependency flag is provided, reset all its dependencies */ |
| 245 | 354 | if (cm->mask & CAN_CTRLMODE_FD) |
| 246 | 355 | priv->ctrlmode &= ~CAN_CTRLMODE_FD_TDC_MASK; |
| 356 | + if (cm->mask & CAN_CTRLMODE_XL) | |
| 357 | + priv->ctrlmode &= ~(CAN_CTRLMODE_XL_TDC_MASK | | |
| 358 | + CAN_CTRLMODE_XL_TMS); | |
| 247 | 359 | |
| 248 | 360 | /* clear bits to be modified and copy the flag values */ |
| 249 | 361 | priv->ctrlmode &= ~cm->mask; |
| 250 | 362 | priv->ctrlmode |= maskedflags; |
| 251 | 363 | |
| 252 | − /* Wipe potential leftovers from previous CAN FD config */ | |
| 364 | + /* Wipe potential leftovers from previous CAN FD/XL config */ | |
| 253 | 365 | if (!(priv->ctrlmode & CAN_CTRLMODE_FD)) { |
| 254 | 366 | memset(&priv->fd.data_bittiming, 0, |
| 255 | 367 | sizeof(priv->fd.data_bittiming)); |
| 256 | 368 | priv->ctrlmode &= ~CAN_CTRLMODE_FD_TDC_MASK; |
| 257 | 369 | memset(&priv->fd.tdc, 0, sizeof(priv->fd.tdc)); |
| 258 | 370 | } |
| 371 | + if (!(priv->ctrlmode & CAN_CTRLMODE_XL)) { | |
| 372 | + memset(&priv->xl.data_bittiming, 0, | |
| 373 | + sizeof(priv->fd.data_bittiming)); | |
| 374 | + priv->ctrlmode &= ~CAN_CTRLMODE_XL_TDC_MASK; | |
| 375 | + memset(&priv->xl.tdc, 0, sizeof(priv->xl.tdc)); | |
| 376 | + memset(&priv->xl.pwm, 0, sizeof(priv->xl.pwm)); | |
| 377 | + } | |
| 259 | 378 | |
| 260 | 379 | can_set_default_mtu(dev); |
| 261 | 380 | |
| @@ -330,7 +449,10 @@ static int can_dbt_changelink(struct net_device *dev, struct nlattr *data[], · 68 unmodified lines | ||
| 330 | 449 | dbt_params = &priv->fd; |
| 331 | 450 | tdc_mask = CAN_CTRLMODE_FD_TDC_MASK; |
| 332 | 451 | } else { |
| 333 | − return -EOPNOTSUPP; /* Place holder for CAN XL */ | |
| 452 | + data_bittiming = data[IFLA_CAN_XL_DATA_BITTIMING]; | |
| 453 | + data_tdc = data[IFLA_CAN_XL_TDC]; | |
| 454 | + dbt_params = &priv->xl; | |
| 455 | + tdc_mask = CAN_CTRLMODE_XL_TDC_MASK; | |
| 334 | 456 | } |
| 335 | 457 | |
| 336 | 458 | if (!data_bittiming) |
| @@ -366,7 +488,8 @@ static int can_dbt_changelink(struct net_device *dev, struct nlattr *data[], · 29 unmodified lines | ||
| 366 | 488 | if (data[IFLA_CAN_CTRLMODE]) { |
| 367 | 489 | struct can_ctrlmode *cm = nla_data(data[IFLA_CAN_CTRLMODE]); |
| 368 | 490 | |
| 369 | − need_tdc_calc = !(cm->mask & tdc_mask); | |
| 491 | + if (fd || !(priv->ctrlmode & CAN_CTRLMODE_XL_TMS)) | |
| 492 | + need_tdc_calc = !(cm->mask & tdc_mask); | |
| 370 | 493 | } |
| 371 | 494 | if (data_tdc) { |
| 372 | 495 | /* TDC parameters are provided: use them */ |
| @@ -381,7 +504,7 @@ static int can_dbt_changelink(struct net_device *dev, struct nlattr *data[], · 8 unmodified lines | ||
| 381 | 504 | */ |
| 382 | 505 | can_calc_tdco(&dbt_params->tdc, dbt_params->tdc_const, &dbt, |
| 383 | 506 | tdc_mask, &priv->ctrlmode, priv->ctrlmode_supported); |
| 384 | − } /* else: both CAN_CTRLMODE_TDC_{AUTO,MANUAL} are explicitly | |
| 507 | + } /* else: both CAN_CTRLMODE_{,XL}_TDC_{AUTO,MANUAL} are explicitly | |
| 385 | 508 | * turned off. TDC is disabled: do nothing |
| 386 | 509 | */ |
| 387 | 510 | |
| @@ -397,6 +520,76 @@ static int can_dbt_changelink(struct net_device *dev, struct nlattr *data[], · 9 unmodified lines | ||
| 397 | 520 | return 0; |
| 398 | 521 | } |
| 399 | 522 | |
| 523 | +static int can_pwm_changelink(struct net_device *dev, | |
| 524 | + const struct nlattr *pwm_nla, | |
| 525 | + struct netlink_ext_ack *extack) | |
| 526 | +{ | |
| 527 | + struct can_priv *priv = netdev_priv(dev); | |
| 528 | + const struct can_pwm_const *pwm_const = priv->xl.pwm_const; | |
| 529 | + struct nlattr *tb_pwm[IFLA_CAN_PWM_MAX + 1]; | |
| 530 | + struct can_pwm pwm = { 0 }; | |
| 531 | + int err; | |
| 532 | + | |
| 533 | + if (!(priv->ctrlmode & CAN_CTRLMODE_XL_TMS)) | |
| 534 | + return 0; | |
| 535 | + | |
| 536 | + if (!pwm_const) { | |
| 537 | + NL_SET_ERR_MSG(extack, "The device does not support PWM"); | |
| 538 | + return -EOPNOTSUPP; | |
| 539 | + } | |
| 540 | + | |
| 541 | + if (!pwm_nla) | |
| 542 | + return can_calc_pwm(dev, extack); | |
| 543 | + | |
| 544 | + err = nla_parse_nested(tb_pwm, IFLA_CAN_PWM_MAX, pwm_nla, | |
| 545 | + can_pwm_policy, extack); | |
| 546 | + if (err) | |
| 547 | + return err; | |
| 548 | + | |
| 549 | + if (tb_pwm[IFLA_CAN_PWM_PWMS]) { | |
| 550 | + pwm.pwms = nla_get_u32(tb_pwm[IFLA_CAN_PWM_PWMS]); | |
| 551 | + if (pwm.pwms < pwm_const->pwms_min || | |
| 552 | + pwm.pwms > pwm_const->pwms_max) { | |
| 553 | + NL_SET_ERR_MSG_FMT(extack, | |
| 554 | + "PWMS: %u tqmin is out of range: %u...%u", | |
| 555 | + pwm.pwms, pwm_const->pwms_min, | |
| 556 | + pwm_const->pwms_max); | |
| 557 | + return -EINVAL; | |
| 558 | + } | |
| 559 | + } | |
| 560 | + | |
| 561 | + if (tb_pwm[IFLA_CAN_PWM_PWML]) { | |
| 562 | + pwm.pwml = nla_get_u32(tb_pwm[IFLA_CAN_PWM_PWML]); | |
| 563 | + if (pwm.pwml < pwm_const->pwml_min || | |
| 564 | + pwm.pwml > pwm_const->pwml_max) { | |
| 565 | + NL_SET_ERR_MSG_FMT(extack, | |
| 566 | + "PWML: %u tqmin is out of range: %u...%u", | |
| 567 | + pwm.pwml, pwm_const->pwml_min, | |
| 568 | + pwm_const->pwml_max); | |
| 569 | + return -EINVAL; | |
| 570 | + } | |
| 571 | + } | |
| 572 | + | |
| 573 | + if (tb_pwm[IFLA_CAN_PWM_PWMO]) { | |
| 574 | + pwm.pwmo = nla_get_u32(tb_pwm[IFLA_CAN_PWM_PWMO]); | |
| 575 | + if (pwm.pwmo < pwm_const->pwmo_min || | |
| 576 | + pwm.pwmo > pwm_const->pwmo_max) { | |
| 577 | + NL_SET_ERR_MSG_FMT(extack, | |
| 578 | + "PWMO: %u tqmin is out of range: %u...%u", | |
| 579 | + pwm.pwmo, pwm_const->pwmo_min, | |
| 580 | + pwm_const->pwmo_max); | |
| 581 | + return -EINVAL; | |
| 582 | + } | |
| 583 | + } | |
| 584 | + | |
| 585 | + err = can_validate_pwm_bittiming(dev, &pwm, extack); | |
| 586 | + if (err) | |
| 587 | + return err; | |
| 588 | + | |
| 589 | + priv->xl.pwm = pwm; | |
| 590 | + return 0; | |
| 591 | +} | |
| 592 | + | |
| 400 | 593 | static int can_changelink(struct net_device *dev, struct nlattr *tb[], |
| 401 | 594 | struct nlattr *data[], |
| 402 | 595 | struct netlink_ext_ack *extack) |
| @@ -486,6 +679,14 @@ static int can_changelink(struct net_device *dev, struct nlattr *tb[], · 83 unmodified lines | ||
| 486 | 679 | if (err) |
| 487 | 680 | return err; |
| 488 | 681 | |
| 682 | + /* CAN XL */ | |
| 683 | + err = can_dbt_changelink(dev, data, false, extack); | |
| 684 | + if (err) | |
| 685 | + return err; | |
| 686 | + err = can_pwm_changelink(dev, data[IFLA_CAN_XL_PWM], extack); | |
| 687 | + if (err) | |
| 688 | + return err; | |
| 689 | + | |
| 489 | 690 | if (data[IFLA_CAN_TERMINATION]) { |
| 490 | 691 | const u16 termval = nla_get_u16(data[IFLA_CAN_TERMINATION]); |
| 491 | 692 | const unsigned int num_term = priv->termination_const_cnt; |
| @@ -553,14 +754,14 @@ static size_t can_data_bittiming_get_size(struct data_bittiming_params *dbt_para · 61 unmodified lines | ||
| 553 | 754 | { |
| 554 | 755 | size_t size = 0; |
| 555 | 756 | |
| 556 | − if (dbt_params->data_bittiming.bitrate) /* IFLA_CAN_DATA_BITTIMING */ | |
| 757 | + if (dbt_params->data_bittiming.bitrate) /* IFLA_CAN_{,XL}_DATA_BITTIMING */ | |
| 557 | 758 | size += nla_total_size(sizeof(dbt_params->data_bittiming)); |
| 558 | − if (dbt_params->data_bittiming_const) /* IFLA_CAN_DATA_BITTIMING_CONST */ | |
| 759 | + if (dbt_params->data_bittiming_const) /* IFLA_CAN_{,XL}_DATA_BITTIMING_CONST */ | |
| 559 | 760 | size += nla_total_size(sizeof(*dbt_params->data_bittiming_const)); |
| 560 | − if (dbt_params->data_bitrate_const) /* IFLA_CAN_DATA_BITRATE_CONST */ | |
| 761 | + if (dbt_params->data_bitrate_const) /* IFLA_CAN_{,XL}_DATA_BITRATE_CONST */ | |
| 561 | 762 | size += nla_total_size(sizeof(*dbt_params->data_bitrate_const) * |
| 562 | 763 | dbt_params->data_bitrate_const_cnt); |
| 563 | − size += can_tdc_get_size(dbt_params, tdc_flags);/* IFLA_CAN_TDC */ | |
| 764 | + size += can_tdc_get_size(dbt_params, tdc_flags);/* IFLA_CAN_{,XL}_TDC */ | |
| 564 | 765 | |
| 565 | 766 | return size; |
| 566 | 767 | } |
| @@ -571,6 +772,30 @@ static size_t can_ctrlmode_ext_get_size(void) · 4 unmodified lines | ||
| 571 | 772 | nla_total_size(sizeof(u32)); /* IFLA_CAN_CTRLMODE_SUPPORTED */ |
| 572 | 773 | } |
| 573 | 774 | |
| 775 | +static size_t can_pwm_get_size(const struct can_pwm_const *pwm_const, | |
| 776 | + bool pwm_on) | |
| 777 | +{ | |
| 778 | + size_t size; | |
| 779 | + | |
| 780 | + if (!pwm_const || !pwm_on) | |
| 781 | + return 0; | |
| 782 | + | |
| 783 | + size = nla_total_size(0); /* nest IFLA_CAN_PWM */ | |
| 784 | + | |
| 785 | + size += nla_total_size(sizeof(u32)); /* IFLA_CAN_PWM_PWMS_MIN */ | |
| 786 | + size += nla_total_size(sizeof(u32)); /* IFLA_CAN_PWM_PWMS_MAX */ | |
| 787 | + size += nla_total_size(sizeof(u32)); /* IFLA_CAN_PWM_PWML_MIN */ | |
| 788 | + size += nla_total_size(sizeof(u32)); /* IFLA_CAN_PWM_PWML_MAX */ | |
| 789 | + size += nla_total_size(sizeof(u32)); /* IFLA_CAN_PWM_PWMO_MIN */ | |
| 790 | + size += nla_total_size(sizeof(u32)); /* IFLA_CAN_PWM_PWMO_MAX */ | |
| 791 | + | |
| 792 | + size += nla_total_size(sizeof(u32)); /* IFLA_CAN_PWM_PWMS */ | |
| 793 | + size += nla_total_size(sizeof(u32)); /* IFLA_CAN_PWM_PWML */ | |
| 794 | + size += nla_total_size(sizeof(u32)); /* IFLA_CAN_PWM_PWMO */ | |
| 795 | + | |
| 796 | + return size; | |
| 797 | +} | |
| 798 | + | |
| 574 | 799 | static size_t can_get_size(const struct net_device *dev) |
| 575 | 800 | { |
| 576 | 801 | struct can_priv *priv = netdev_priv(dev); |
| @@ -600,6 +825,11 @@ static size_t can_get_size(const struct net_device *dev) · 23 unmodified lines | ||
| 600 | 825 | size += can_data_bittiming_get_size(&priv->fd, |
| 601 | 826 | priv->ctrlmode & CAN_CTRLMODE_FD_TDC_MASK); |
| 602 | 827 | |
| 828 | + size += can_data_bittiming_get_size(&priv->xl, | |
| 829 | + priv->ctrlmode & CAN_CTRLMODE_XL_TDC_MASK); | |
| 830 | + size += can_pwm_get_size(priv->xl.pwm_const, /* IFLA_CAN_XL_PWM */ | |
| 831 | + priv->ctrlmode & CAN_CTRLMODE_XL_TMS); | |
| 832 | + | |
| 603 | 833 | return size; |
| 604 | 834 | } |
| 605 | 835 | |
| @@ -644,7 +874,9 @@ static int can_tdc_fill_info(struct sk_buff *skb, const struct net_device *dev, · 38 unmodified lines | ||
| 644 | 874 | tdc_is_enabled = can_fd_tdc_is_enabled(priv); |
| 645 | 875 | tdc_manual = priv->ctrlmode & CAN_CTRLMODE_TDC_MANUAL; |
| 646 | 876 | } else { |
| 647 | − return -EOPNOTSUPP; /* Place holder for CAN XL */ | |
| 877 | + dbt_params = &priv->xl; | |
| 878 | + tdc_is_enabled = can_xl_tdc_is_enabled(priv); | |
| 879 | + tdc_manual = priv->ctrlmode & CAN_CTRLMODE_XL_TDC_MANUAL; | |
| 648 | 880 | } |
| 649 | 881 | tdc_const = dbt_params->tdc_const; |
| 650 | 882 | tdc = &dbt_params->tdc; |
| @@ -695,6 +927,42 @@ err_cancel: · 44 unmodified lines | ||
| 695 | 927 | return -EMSGSIZE; |
| 696 | 928 | } |
| 697 | 929 | |
| 930 | +static int can_pwm_fill_info(struct sk_buff *skb, const struct can_priv *priv) | |
| 931 | +{ | |
| 932 | + const struct can_pwm_const *pwm_const = priv->xl.pwm_const; | |
| 933 | + const struct can_pwm *pwm = &priv->xl.pwm; | |
| 934 | + struct nlattr *nest; | |
| 935 | + | |
| 936 | + if (!pwm_const) | |
| 937 | + return 0; | |
| 938 | + | |
| 939 | + nest = nla_nest_start(skb, IFLA_CAN_XL_PWM); | |
| 940 | + if (!nest) | |
| 941 | + return -EMSGSIZE; | |
| 942 | + | |
| 943 | + if (nla_put_u32(skb, IFLA_CAN_PWM_PWMS_MIN, pwm_const->pwms_min) || | |
| 944 | + nla_put_u32(skb, IFLA_CAN_PWM_PWMS_MAX, pwm_const->pwms_max) || | |
| 945 | + nla_put_u32(skb, IFLA_CAN_PWM_PWML_MIN, pwm_const->pwml_min) || | |
| 946 | + nla_put_u32(skb, IFLA_CAN_PWM_PWML_MAX, pwm_const->pwml_max) || | |
| 947 | + nla_put_u32(skb, IFLA_CAN_PWM_PWMO_MIN, pwm_const->pwmo_min) || | |
| 948 | + nla_put_u32(skb, IFLA_CAN_PWM_PWMO_MAX, pwm_const->pwmo_max)) | |
| 949 | + goto err_cancel; | |
| 950 | + | |
| 951 | + if (priv->ctrlmode & CAN_CTRLMODE_XL_TMS) { | |
| 952 | + if (nla_put_u32(skb, IFLA_CAN_PWM_PWMS, pwm->pwms) || | |
| 953 | + nla_put_u32(skb, IFLA_CAN_PWM_PWML, pwm->pwml) || | |
| 954 | + nla_put_u32(skb, IFLA_CAN_PWM_PWMO, pwm->pwmo)) | |
| 955 | + goto err_cancel; | |
| 956 | + } | |
| 957 | + | |
| 958 | + nla_nest_end(skb, nest); | |
| 959 | + return 0; | |
| 960 | + | |
| 961 | +err_cancel: | |
| 962 | + nla_nest_cancel(skb, nest); | |
| 963 | + return -EMSGSIZE; | |
| 964 | +} | |
| 965 | + | |
| 698 | 966 | static int can_ctrlmode_ext_fill_info(struct sk_buff *skb, |
| 699 | 967 | const struct can_priv *priv) |
| 700 | 968 | { |
| @@ -766,9 +1034,22 @@ static int can_fill_info(struct sk_buff *skb, const struct net_device *dev) · 65 unmodified lines | ||
| 766 | 1034 | |
| 767 | 1035 | can_tdc_fill_info(skb, dev, IFLA_CAN_TDC) || |
| 768 | 1036 | |
| 769 | − can_ctrlmode_ext_fill_info(skb, priv) | |
| 770 | − ) | |
| 1037 | + can_ctrlmode_ext_fill_info(skb, priv) || | |
| 1038 | + | |
| 1039 | + can_bittiming_fill_info(skb, IFLA_CAN_XL_DATA_BITTIMING, | |
| 1040 | + &priv->xl.data_bittiming) || | |
| 771 | 1041 | |
| 1042 | + can_bittiming_const_fill_info(skb, IFLA_CAN_XL_DATA_BITTIMING_CONST, | |
| 1043 | + priv->xl.data_bittiming_const) || | |
| 1044 | + | |
| 1045 | + can_bitrate_const_fill_info(skb, IFLA_CAN_XL_DATA_BITRATE_CONST, | |
| 1046 | + priv->xl.data_bitrate_const, | |
| 1047 | + priv->xl.data_bitrate_const_cnt) || | |
| 1048 | + | |
| 1049 | + can_tdc_fill_info(skb, dev, IFLA_CAN_XL_TDC) || | |
| 1050 | + | |
| 1051 | + can_pwm_fill_info(skb, priv) | |
| 1052 | + ) | |
| 772 | 1053 | return -EMSGSIZE; |
| 773 | 1054 | |
| 774 | 1055 | return 0; |
| @@ -0,0 +1,285 @@ | ||
| 1 | +// SPDX-License-Identifier: GPL-2.0 | |
| 2 | + | |
| 3 | +/* Copyright (c) 2025 Vincent Mailhol <mailhol@kernel.org> */ | |
| 4 | + | |
| 5 | +#include <linux/array_size.h> | |
| 6 | +#include <linux/errno.h> | |
| 7 | +#include <linux/init.h> | |
| 8 | +#include <linux/kernel.h> | |
| 9 | +#include <linux/module.h> | |
| 10 | +#include <linux/netdevice.h> | |
| 11 | +#include <linux/units.h> | |
| 12 | +#include <linux/string_choices.h> | |
| 13 | + | |
| 14 | +#include <linux/can.h> | |
| 15 | +#include <linux/can/bittiming.h> | |
| 16 | +#include <linux/can/dev.h> | |
| 17 | +#include <linux/can/skb.h> | |
| 18 | + | |
| 19 | +struct dummy_can { | |
| 20 | + struct can_priv can; | |
| 21 | + struct net_device *dev; | |
| 22 | +}; | |
| 23 | + | |
| 24 | +static struct dummy_can *dummy_can; | |
| 25 | + | |
| 26 | +static const struct can_bittiming_const dummy_can_bittiming_const = { | |
| 27 | + .name = "dummy_can CC", | |
| 28 | + .tseg1_min = 2, | |
| 29 | + .tseg1_max = 256, | |
| 30 | + .tseg2_min = 2, | |
| 31 | + .tseg2_max = 128, | |
| 32 | + .sjw_max = 128, | |
| 33 | + .brp_min = 1, | |
| 34 | + .brp_max = 512, | |
| 35 | + .brp_inc = 1 | |
| 36 | +}; | |
| 37 | + | |
| 38 | +static const struct can_bittiming_const dummy_can_fd_databittiming_const = { | |
| 39 | + .name = "dummy_can FD", | |
| 40 | + .tseg1_min = 2, | |
| 41 | + .tseg1_max = 256, | |
| 42 | + .tseg2_min = 2, | |
| 43 | + .tseg2_max = 128, | |
| 44 | + .sjw_max = 128, | |
| 45 | + .brp_min = 1, | |
| 46 | + .brp_max = 512, | |
| 47 | + .brp_inc = 1 | |
| 48 | +}; | |
| 49 | + | |
| 50 | +static const struct can_tdc_const dummy_can_fd_tdc_const = { | |
| 51 | + .tdcv_min = 0, | |
| 52 | + .tdcv_max = 0, /* Manual mode not supported. */ | |
| 53 | + .tdco_min = 0, | |
| 54 | + .tdco_max = 127, | |
| 55 | + .tdcf_min = 0, | |
| 56 | + .tdcf_max = 127 | |
| 57 | +}; | |
| 58 | + | |
| 59 | +static const struct can_bittiming_const dummy_can_xl_databittiming_const = { | |
| 60 | + .name = "dummy_can XL", | |
| 61 | + .tseg1_min = 2, | |
| 62 | + .tseg1_max = 256, | |
| 63 | + .tseg2_min = 2, | |
| 64 | + .tseg2_max = 128, | |
| 65 | + .sjw_max = 128, | |
| 66 | + .brp_min = 1, | |
| 67 | + .brp_max = 512, | |
| 68 | + .brp_inc = 1 | |
| 69 | +}; | |
| 70 | + | |
| 71 | +static const struct can_tdc_const dummy_can_xl_tdc_const = { | |
| 72 | + .tdcv_min = 0, | |
| 73 | + .tdcv_max = 0, /* Manual mode not supported. */ | |
| 74 | + .tdco_min = 0, | |
| 75 | + .tdco_max = 127, | |
| 76 | + .tdcf_min = 0, | |
| 77 | + .tdcf_max = 127 | |
| 78 | +}; | |
| 79 | + | |
| 80 | +static const struct can_pwm_const dummy_can_pwm_const = { | |
| 81 | + .pwms_min = 1, | |
| 82 | + .pwms_max = 8, | |
| 83 | + .pwml_min = 2, | |
| 84 | + .pwml_max = 24, | |
| 85 | + .pwmo_min = 0, | |
| 86 | + .pwmo_max = 16, | |
| 87 | +}; | |
| 88 | + | |
| 89 | +static void dummy_can_print_bittiming(struct net_device *dev, | |
| 90 | + struct can_bittiming *bt) | |
| 91 | +{ | |
| 92 | + netdev_dbg(dev, "\tbitrate: %u\n", bt->bitrate); | |
| 93 | + netdev_dbg(dev, "\tsample_point: %u\n", bt->sample_point); | |
| 94 | + netdev_dbg(dev, "\ttq: %u\n", bt->tq); | |
| 95 | + netdev_dbg(dev, "\tprop_seg: %u\n", bt->prop_seg); | |
| 96 | + netdev_dbg(dev, "\tphase_seg1: %u\n", bt->phase_seg1); | |
| 97 | + netdev_dbg(dev, "\tphase_seg2: %u\n", bt->phase_seg2); | |
| 98 | + netdev_dbg(dev, "\tsjw: %u\n", bt->sjw); | |
| 99 | + netdev_dbg(dev, "\tbrp: %u\n", bt->brp); | |
| 100 | +} | |
| 101 | + | |
| 102 | +static void dummy_can_print_tdc(struct net_device *dev, struct can_tdc *tdc) | |
| 103 | +{ | |
| 104 | + netdev_dbg(dev, "\t\ttdcv: %u\n", tdc->tdcv); | |
| 105 | + netdev_dbg(dev, "\t\ttdco: %u\n", tdc->tdco); | |
| 106 | + netdev_dbg(dev, "\t\ttdcf: %u\n", tdc->tdcf); | |
| 107 | +} | |
| 108 | + | |
| 109 | +static void dummy_can_print_pwm(struct net_device *dev, struct can_pwm *pwm, | |
| 110 | + struct can_bittiming *dbt) | |
| 111 | +{ | |
| 112 | + netdev_dbg(dev, "\t\tpwms: %u\n", pwm->pwms); | |
| 113 | + netdev_dbg(dev, "\t\tpwml: %u\n", pwm->pwml); | |
| 114 | + netdev_dbg(dev, "\t\tpwmo: %u\n", pwm->pwmo); | |
| 115 | +} | |
| 116 | + | |
| 117 | +static void dummy_can_print_ctrlmode(struct net_device *dev) | |
| 118 | +{ | |
| 119 | + struct dummy_can *priv = netdev_priv(dev); | |
| 120 | + struct can_priv *can_priv = &priv->can; | |
| 121 | + unsigned long supported = can_priv->ctrlmode_supported; | |
| 122 | + u32 enabled = can_priv->ctrlmode; | |
| 123 | + | |
| 124 | + netdev_dbg(dev, "Control modes:\n"); | |
| 125 | + netdev_dbg(dev, "\tsupported: 0x%08x\n", (u32)supported); | |
| 126 | + netdev_dbg(dev, "\tenabled: 0x%08x\n", enabled); | |
| 127 | + | |
| 128 | + if (supported) { | |
| 129 | + int idx; | |
| 130 | + | |
| 131 | + netdev_dbg(dev, "\tlist:"); | |
| 132 | + for_each_set_bit(idx, &supported, BITS_PER_TYPE(u32)) | |
| 133 | + netdev_dbg(dev, "\t\t%s: %s\n", | |
| 134 | + can_get_ctrlmode_str(BIT(idx)), | |
| 135 | + enabled & BIT(idx) ? "on" : "off"); | |
| 136 | + } | |
| 137 | +} | |
| 138 | + | |
| 139 | +static void dummy_can_print_bittiming_info(struct net_device *dev) | |
| 140 | +{ | |
| 141 | + struct dummy_can *priv = netdev_priv(dev); | |
| 142 | + struct can_priv *can_priv = &priv->can; | |
| 143 | + | |
| 144 | + netdev_dbg(dev, "Clock frequency: %u\n", can_priv->clock.freq); | |
| 145 | + netdev_dbg(dev, "Maximum bitrate: %u\n", can_priv->bitrate_max); | |
| 146 | + netdev_dbg(dev, "MTU: %u\n", dev->mtu); | |
| 147 | + netdev_dbg(dev, "\n"); | |
| 148 | + | |
| 149 | + dummy_can_print_ctrlmode(dev); | |
| 150 | + netdev_dbg(dev, "\n"); | |
| 151 | + | |
| 152 | + netdev_dbg(dev, "Classical CAN nominal bittiming:\n"); | |
| 153 | + dummy_can_print_bittiming(dev, &can_priv->bittiming); | |
| 154 | + netdev_dbg(dev, "\n"); | |
| 155 | + | |
| 156 | + if (can_priv->ctrlmode & CAN_CTRLMODE_FD) { | |
| 157 | + netdev_dbg(dev, "CAN FD databittiming:\n"); | |
| 158 | + dummy_can_print_bittiming(dev, &can_priv->fd.data_bittiming); | |
| 159 | + if (can_fd_tdc_is_enabled(can_priv)) { | |
| 160 | + netdev_dbg(dev, "\tCAN FD TDC:\n"); | |
| 161 | + dummy_can_print_tdc(dev, &can_priv->fd.tdc); | |
| 162 | + } | |
| 163 | + } | |
| 164 | + netdev_dbg(dev, "\n"); | |
| 165 | + | |
| 166 | + if (can_priv->ctrlmode & CAN_CTRLMODE_XL) { | |
| 167 | + netdev_dbg(dev, "CAN XL databittiming:\n"); | |
| 168 | + dummy_can_print_bittiming(dev, &can_priv->xl.data_bittiming); | |
| 169 | + if (can_xl_tdc_is_enabled(can_priv)) { | |
| 170 | + netdev_dbg(dev, "\tCAN XL TDC:\n"); | |
| 171 | + dummy_can_print_tdc(dev, &can_priv->xl.tdc); | |
| 172 | + } | |
| 173 | + if (can_priv->ctrlmode & CAN_CTRLMODE_XL_TMS) { | |
| 174 | + netdev_dbg(dev, "\tCAN XL PWM:\n"); | |
| 175 | + dummy_can_print_pwm(dev, &can_priv->xl.pwm, | |
| 176 | + &can_priv->xl.data_bittiming); | |
| 177 | + } | |
| 178 | + } | |
| 179 | + netdev_dbg(dev, "\n"); | |
| 180 | +} | |
| 181 | + | |
| 182 | +static int dummy_can_netdev_open(struct net_device *dev) | |
| 183 | +{ | |
| 184 | + int ret; | |
| 185 | + struct can_priv *priv = netdev_priv(dev); | |
| 186 | + | |
| 187 | + dummy_can_print_bittiming_info(dev); | |
| 188 | + netdev_dbg(dev, "error-signalling is %s\n", | |
| 189 | + str_enabled_disabled(!can_dev_in_xl_only_mode(priv))); | |
| 190 | + | |
| 191 | + ret = open_candev(dev); | |
| 192 | + if (ret) | |
| 193 | + return ret; | |
| 194 | + netif_start_queue(dev); | |
| 195 | + netdev_dbg(dev, "dummy-can is up\n"); | |
| 196 | + | |
| 197 | + return 0; | |
| 198 | +} | |
| 199 | + | |
| 200 | +static int dummy_can_netdev_close(struct net_device *dev) | |
| 201 | +{ | |
| 202 | + netif_stop_queue(dev); | |
| 203 | + close_candev(dev); | |
| 204 | + netdev_dbg(dev, "dummy-can is down\n"); | |
| 205 | + | |
| 206 | + return 0; | |
| 207 | +} | |
| 208 | + | |
| 209 | +static netdev_tx_t dummy_can_start_xmit(struct sk_buff *skb, | |
| 210 | + struct net_device *dev) | |
| 211 | +{ | |
| 212 | + if (can_dev_dropped_skb(dev, skb)) | |
| 213 | + return NETDEV_TX_OK; | |
| 214 | + | |
| 215 | + can_put_echo_skb(skb, dev, 0, 0); | |
| 216 | + dev->stats.tx_packets++; | |
| 217 | + dev->stats.tx_bytes += can_get_echo_skb(dev, 0, NULL); | |
| 218 | + | |
| 219 | + return NETDEV_TX_OK; | |
| 220 | +} | |
| 221 | + | |
| 222 | +static const struct net_device_ops dummy_can_netdev_ops = { | |
| 223 | + .ndo_open = dummy_can_netdev_open, | |
| 224 | + .ndo_stop = dummy_can_netdev_close, | |
| 225 | + .ndo_start_xmit = dummy_can_start_xmit, | |
| 226 | +}; | |
| 227 | + | |
| 228 | +static const struct ethtool_ops dummy_can_ethtool_ops = { | |
| 229 | + .get_ts_info = ethtool_op_get_ts_info, | |
| 230 | +}; | |
| 231 | + | |
| 232 | +static int __init dummy_can_init(void) | |
| 233 | +{ | |
| 234 | + struct net_device *dev; | |
| 235 | + struct dummy_can *priv; | |
| 236 | + int ret; | |
| 237 | + | |
| 238 | + dev = alloc_candev(sizeof(*priv), 1); | |
| 239 | + if (!dev) | |
| 240 | + return -ENOMEM; | |
| 241 | + | |
| 242 | + dev->netdev_ops = &dummy_can_netdev_ops; | |
| 243 | + dev->ethtool_ops = &dummy_can_ethtool_ops; | |
| 244 | + priv = netdev_priv(dev); | |
| 245 | + priv->can.bittiming_const = &dummy_can_bittiming_const; | |
| 246 | + priv->can.bitrate_max = 20 * MEGA /* BPS */; | |
| 247 | + priv->can.clock.freq = 160 * MEGA /* Hz */; | |
| 248 | + priv->can.fd.data_bittiming_const = &dummy_can_fd_databittiming_const; | |
| 249 | + priv->can.fd.tdc_const = &dummy_can_fd_tdc_const; | |
| 250 | + priv->can.xl.data_bittiming_const = &dummy_can_xl_databittiming_const; | |
| 251 | + priv->can.xl.tdc_const = &dummy_can_xl_tdc_const; | |
| 252 | + priv->can.xl.pwm_const = &dummy_can_pwm_const; | |
| 253 | + priv->can.ctrlmode_supported = CAN_CTRLMODE_LISTENONLY | | |
| 254 | + CAN_CTRLMODE_FD | CAN_CTRLMODE_TDC_AUTO | | |
| 255 | + CAN_CTRLMODE_RESTRICTED | CAN_CTRLMODE_XL | | |
| 256 | + CAN_CTRLMODE_XL_TDC_AUTO | CAN_CTRLMODE_XL_TMS; | |
| 257 | + priv->dev = dev; | |
| 258 | + | |
| 259 | + ret = register_candev(priv->dev); | |
| 260 | + if (ret) { | |
| 261 | + free_candev(priv->dev); | |
| 262 | + return ret; | |
| 263 | + } | |
| 264 | + | |
| 265 | + dummy_can = priv; | |
| 266 | + netdev_dbg(dev, "dummy-can ready\n"); | |
| 267 | + | |
| 268 | + return 0; | |
| 269 | +} | |
| 270 | + | |
| 271 | +static void __exit dummy_can_exit(void) | |
| 272 | +{ | |
| 273 | + struct net_device *dev = dummy_can->dev; | |
| 274 | + | |
| 275 | + netdev_dbg(dev, "dummy-can bye bye\n"); | |
| 276 | + unregister_candev(dev); | |
| 277 | + free_candev(dev); | |
| 278 | +} | |
| 279 | + | |
| 280 | +module_init(dummy_can_init); | |
| 281 | +module_exit(dummy_can_exit); | |
| 282 | + | |
| 283 | +MODULE_DESCRIPTION("A dummy CAN driver, mainly to test the netlink interface"); | |
| 284 | +MODULE_LICENSE("GPL"); | |
| 285 | +MODULE_AUTHOR("Vincent Mailhol <mailhol@kernel.org>"); | |
| @@ -468,6 +468,7 @@ struct rcar_canfd_global { · 467 unmodified lines | ||
| 468 | 468 | struct platform_device *pdev; /* Respective platform device */ |
| 469 | 469 | struct clk *clkp; /* Peripheral clock */ |
| 470 | 470 | struct clk *can_clk; /* fCAN clock */ |
| 471 | + struct clk *clk_ram; /* Clock RAM */ | |
| 471 | 472 | unsigned long channels_mask; /* Enabled channels mask */ |
| 472 | 473 | bool extclk; /* CANFD or Ext clock */ |
| 473 | 474 | bool fdmode; /* CAN FD or Classical CAN only mode */ |
| @@ -1569,8 +1570,8 @@ static int rcar_canfd_close(struct net_device *ndev) · 1095 unmodified lines | ||
| 1569 | 1570 | netif_stop_queue(ndev); |
| 1570 | 1571 | rcar_canfd_stop(ndev); |
| 1571 | 1572 | napi_disable(&priv->napi); |
| 1572 | − clk_disable_unprepare(gpriv->can_clk); | |
| 1573 | 1573 | close_candev(ndev); |
| 1574 | + clk_disable_unprepare(gpriv->can_clk); | |
| 1574 | 1575 | phy_power_off(priv->transceiver); |
| 1575 | 1576 | return 0; |
| 1576 | 1577 | } |
| @@ -1960,22 +1961,120 @@ static void rcar_canfd_channel_remove(struct rcar_canfd_global *gpriv, u32 ch) · 383 unmodified lines | ||
| 1960 | 1961 | } |
| 1961 | 1962 | } |
| 1962 | 1963 | |
| 1964 | +static int rcar_canfd_global_init(struct rcar_canfd_global *gpriv) | |
| 1965 | +{ | |
| 1966 | + struct device *dev = &gpriv->pdev->dev; | |
| 1967 | + u32 rule_entry = 0; | |
| 1968 | + u32 ch, sts; | |
| 1969 | + int err; | |
| 1970 | + | |
| 1971 | + err = reset_control_reset(gpriv->rstc1); | |
| 1972 | + if (err) | |
| 1973 | + return err; | |
| 1974 | + | |
| 1975 | + err = reset_control_reset(gpriv->rstc2); | |
| 1976 | + if (err) | |
| 1977 | + goto fail_reset1; | |
| 1978 | + | |
| 1979 | + /* Enable peripheral clock for register access */ | |
| 1980 | + err = clk_prepare_enable(gpriv->clkp); | |
| 1981 | + if (err) { | |
| 1982 | + dev_err(dev, "failed to enable peripheral clock: %pe\n", | |
| 1983 | + ERR_PTR(err)); | |
| 1984 | + goto fail_reset2; | |
| 1985 | + } | |
| 1986 | + | |
| 1987 | + /* Enable RAM clock */ | |
| 1988 | + err = clk_prepare_enable(gpriv->clk_ram); | |
| 1989 | + if (err) { | |
| 1990 | + dev_err(dev, | |
| 1991 | + "failed to enable RAM clock, error %d\n", err); | |
| 1992 | + goto fail_clk; | |
| 1993 | + } | |
| 1994 | + | |
| 1995 | + err = rcar_canfd_reset_controller(gpriv); | |
| 1996 | + if (err) { | |
| 1997 | + dev_err(dev, "reset controller failed: %pe\n", ERR_PTR(err)); | |
| 1998 | + goto fail_ram_clk; | |
| 1999 | + } | |
| 2000 | + | |
| 2001 | + /* Controller in Global reset & Channel reset mode */ | |
| 2002 | + rcar_canfd_configure_controller(gpriv); | |
| 2003 | + | |
| 2004 | + /* Configure per channel attributes */ | |
| 2005 | + for_each_set_bit(ch, &gpriv->channels_mask, gpriv->info->max_channels) { | |
| 2006 | + /* Configure Channel's Rx fifo */ | |
| 2007 | + rcar_canfd_configure_rx(gpriv, ch); | |
| 2008 | + | |
| 2009 | + /* Configure Channel's Tx (Common) fifo */ | |
| 2010 | + rcar_canfd_configure_tx(gpriv, ch); | |
| 2011 | + | |
| 2012 | + /* Configure receive rules */ | |
| 2013 | + rcar_canfd_configure_afl_rules(gpriv, ch, rule_entry); | |
| 2014 | + rule_entry += RCANFD_CHANNEL_NUMRULES; | |
| 2015 | + } | |
| 2016 | + | |
| 2017 | + /* Configure common interrupts */ | |
| 2018 | + rcar_canfd_enable_global_interrupts(gpriv); | |
| 2019 | + | |
| 2020 | + /* Start Global operation mode */ | |
| 2021 | + rcar_canfd_update_bit(gpriv->base, RCANFD_GCTR, RCANFD_GCTR_GMDC_MASK, | |
| 2022 | + RCANFD_GCTR_GMDC_GOPM); | |
| 2023 | + | |
| 2024 | + /* Verify mode change */ | |
| 2025 | + err = readl_poll_timeout((gpriv->base + RCANFD_GSTS), sts, | |
| 2026 | + !(sts & RCANFD_GSTS_GNOPM), 2, 500000); | |
| 2027 | + if (err) { | |
| 2028 | + dev_err(dev, "global operational mode failed\n"); | |
| 2029 | + goto fail_mode; | |
| 2030 | + } | |
| 2031 | + | |
| 2032 | + return 0; | |
| 2033 | + | |
| 2034 | +fail_mode: | |
| 2035 | + rcar_canfd_disable_global_interrupts(gpriv); | |
| 2036 | +fail_ram_clk: | |
| 2037 | + clk_disable_unprepare(gpriv->clk_ram); | |
| 2038 | +fail_clk: | |
| 2039 | + clk_disable_unprepare(gpriv->clkp); | |
| 2040 | +fail_reset2: | |
| 2041 | + reset_control_assert(gpriv->rstc2); | |
| 2042 | +fail_reset1: | |
| 2043 | + reset_control_assert(gpriv->rstc1); | |
| 2044 | + return err; | |
| 2045 | +} | |
| 2046 | + | |
| 2047 | +static void rcar_canfd_global_deinit(struct rcar_canfd_global *gpriv, bool full) | |
| 2048 | +{ | |
| 2049 | + rcar_canfd_disable_global_interrupts(gpriv); | |
| 2050 | + | |
| 2051 | + if (full) { | |
| 2052 | + rcar_canfd_reset_controller(gpriv); | |
| 2053 | + | |
| 2054 | + /* Enter global sleep mode */ | |
| 2055 | + rcar_canfd_set_bit(gpriv->base, RCANFD_GCTR, RCANFD_GCTR_GSLPR); | |
| 2056 | + } | |
| 2057 | + | |
| 2058 | + clk_disable_unprepare(gpriv->clk_ram); | |
| 2059 | + clk_disable_unprepare(gpriv->clkp); | |
| 2060 | + reset_control_assert(gpriv->rstc2); | |
| 2061 | + reset_control_assert(gpriv->rstc1); | |
| 2062 | +} | |
| 2063 | + | |
| 1963 | 2064 | static int rcar_canfd_probe(struct platform_device *pdev) |
| 1964 | 2065 | { |
| 1965 | 2066 | struct phy *transceivers[RCANFD_NUM_CHANNELS] = { NULL, }; |
| 1966 | 2067 | const struct rcar_canfd_hw_info *info; |
| 1967 | 2068 | struct device *dev = &pdev->dev; |
| 1968 | 2069 | void __iomem *addr; |
| 1969 | − u32 sts, ch, fcan_freq; | |
| 1970 | 2070 | struct rcar_canfd_global *gpriv; |
| 1971 | 2071 | struct device_node *of_child; |
| 1972 | 2072 | unsigned long channels_mask = 0; |
| 1973 | 2073 | int err, ch_irq, g_irq; |
| 1974 | 2074 | int g_err_irq, g_recc_irq; |
| 1975 | − u32 rule_entry = 0; | |
| 1976 | 2075 | bool fdmode = true; /* CAN FD only mode - default */ |
| 1977 | 2076 | char name[9] = "channelX"; |
| 1978 | − struct clk *clk_ram; | |
| 2077 | + u32 ch, fcan_freq; | |
| 1979 | 2078 | int i; |
| 1980 | 2079 | |
| 1981 | 2080 | info = of_device_get_match_data(dev); |
| @@ -2065,10 +2164,10 @@ static int rcar_canfd_probe(struct platform_device *pdev) · 83 unmodified lines | ||
| 2065 | 2164 | gpriv->extclk = gpriv->info->external_clk; |
| 2066 | 2165 | } |
| 2067 | 2166 | |
| 2068 | − clk_ram = devm_clk_get_optional_enabled(dev, "ram_clk"); | |
| 2069 | − if (IS_ERR(clk_ram)) | |
| 2070 | − return dev_err_probe(dev, PTR_ERR(clk_ram), | |
| 2071 | − "cannot get enabled ram clock\n"); | |
| 2167 | + gpriv->clk_ram = devm_clk_get_optional(dev, "ram_clk"); | |
| 2168 | + if (IS_ERR(gpriv->clk_ram)) | |
| 2169 | + return dev_err_probe(dev, PTR_ERR(gpriv->clk_ram), | |
| 2170 | + "cannot get ram clock\n"); | |
| 2072 | 2171 | |
| 2073 | 2172 | addr = devm_platform_ioremap_resource(pdev, 0); |
| 2074 | 2173 | if (IS_ERR(addr)) { |
| @@ -2117,59 +2216,9 @@ static int rcar_canfd_probe(struct platform_device *pdev) · 42 unmodified lines | ||
| 2117 | 2216 | } |
| 2118 | 2217 | } |
| 2119 | 2218 | |
| 2120 | − err = reset_control_reset(gpriv->rstc1); | |
| 2219 | + err = rcar_canfd_global_init(gpriv); | |
| 2121 | 2220 | if (err) |
| 2122 | − goto fail_dev; | |
| 2123 | − err = reset_control_reset(gpriv->rstc2); | |
| 2124 | − if (err) { | |
| 2125 | − reset_control_assert(gpriv->rstc1); | |
| 2126 | − goto fail_dev; | |
| 2127 | − } | |
| 2128 | − | |
| 2129 | − /* Enable peripheral clock for register access */ | |
| 2130 | − err = clk_prepare_enable(gpriv->clkp); | |
| 2131 | − if (err) { | |
| 2132 | − dev_err(dev, "failed to enable peripheral clock: %pe\n", | |
| 2133 | − ERR_PTR(err)); | |
| 2134 | − goto fail_reset; | |
| 2135 | − } | |
| 2136 | − | |
| 2137 | − err = rcar_canfd_reset_controller(gpriv); | |
| 2138 | − if (err) { | |
| 2139 | − dev_err(dev, "reset controller failed: %pe\n", ERR_PTR(err)); | |
| 2140 | − goto fail_clk; | |
| 2141 | − } | |
| 2142 | − | |
| 2143 | − /* Controller in Global reset & Channel reset mode */ | |
| 2144 | − rcar_canfd_configure_controller(gpriv); | |
| 2145 | − | |
| 2146 | − /* Configure per channel attributes */ | |
| 2147 | − for_each_set_bit(ch, &gpriv->channels_mask, info->max_channels) { | |
| 2148 | − /* Configure Channel's Rx fifo */ | |
| 2149 | − rcar_canfd_configure_rx(gpriv, ch); | |
| 2150 | − | |
| 2151 | − /* Configure Channel's Tx (Common) fifo */ | |
| 2152 | − rcar_canfd_configure_tx(gpriv, ch); | |
| 2153 | − | |
| 2154 | − /* Configure receive rules */ | |
| 2155 | − rcar_canfd_configure_afl_rules(gpriv, ch, rule_entry); | |
| 2156 | − rule_entry += RCANFD_CHANNEL_NUMRULES; | |
| 2157 | − } | |
| 2158 | − | |
| 2159 | − /* Configure common interrupts */ | |
| 2160 | − rcar_canfd_enable_global_interrupts(gpriv); | |
| 2161 | − | |
| 2162 | − /* Start Global operation mode */ | |
| 2163 | − rcar_canfd_update_bit(gpriv->base, RCANFD_GCTR, RCANFD_GCTR_GMDC_MASK, | |
| 2164 | − RCANFD_GCTR_GMDC_GOPM); | |
| 2165 | − | |
| 2166 | − /* Verify mode change */ | |
| 2167 | − err = readl_poll_timeout((gpriv->base + RCANFD_GSTS), sts, | |
| 2168 | − !(sts & RCANFD_GSTS_GNOPM), 2, 500000); | |
| 2169 | − if (err) { | |
| 2170 | − dev_err(dev, "global operational mode failed\n"); | |
| 2171 | 2221 | goto fail_mode; |
| 2172 | − } | |
| 2173 | 2222 | |
| 2174 | 2223 | for_each_set_bit(ch, &gpriv->channels_mask, info->max_channels) { |
| 2175 | 2224 | err = rcar_canfd_channel_probe(gpriv, ch, fcan_freq, |
| @@ -2188,12 +2237,7 @@ fail_channel: · 12 unmodified lines | ||
| 2188 | 2237 | for_each_set_bit(ch, &gpriv->channels_mask, info->max_channels) |
| 2189 | 2238 | rcar_canfd_channel_remove(gpriv, ch); |
| 2190 | 2239 | fail_mode: |
| 2191 | − rcar_canfd_disable_global_interrupts(gpriv); | |
| 2192 | −fail_clk: | |
| 2193 | − clk_disable_unprepare(gpriv->clkp); | |
| 2194 | −fail_reset: | |
| 2195 | − reset_control_assert(gpriv->rstc1); | |
| 2196 | − reset_control_assert(gpriv->rstc2); | |
| 2240 | + rcar_canfd_global_deinit(gpriv, false); | |
| 2197 | 2241 | fail_dev: |
| 2198 | 2242 | return err; |
| 2199 | 2243 | } |
| @@ -2203,33 +2247,79 @@ static void rcar_canfd_remove(struct platform_device *pdev) · 3 unmodified lines | ||
| 2203 | 2247 | struct rcar_canfd_global *gpriv = platform_get_drvdata(pdev); |
| 2204 | 2248 | u32 ch; |
| 2205 | 2249 | |
| 2206 | − rcar_canfd_reset_controller(gpriv); | |
| 2207 | − rcar_canfd_disable_global_interrupts(gpriv); | |
| 2208 | − | |
| 2209 | 2250 | for_each_set_bit(ch, &gpriv->channels_mask, gpriv->info->max_channels) { |
| 2210 | 2251 | rcar_canfd_disable_channel_interrupts(gpriv->ch[ch]); |
| 2211 | 2252 | rcar_canfd_channel_remove(gpriv, ch); |
| 2212 | 2253 | } |
| 2213 | 2254 | |
| 2214 | − /* Enter global sleep mode */ | |
| 2215 | − rcar_canfd_set_bit(gpriv->base, RCANFD_GCTR, RCANFD_GCTR_GSLPR); | |
| 2216 | − clk_disable_unprepare(gpriv->clkp); | |
| 2217 | − reset_control_assert(gpriv->rstc1); | |
| 2218 | − reset_control_assert(gpriv->rstc2); | |
| 2255 | + rcar_canfd_global_deinit(gpriv, true); | |
| 2219 | 2256 | } |
| 2220 | 2257 | |
| 2221 | −static int __maybe_unused rcar_canfd_suspend(struct device *dev) | |
| 2258 | +static int rcar_canfd_suspend(struct device *dev) | |
| 2222 | 2259 | { |
| 2260 | + struct rcar_canfd_global *gpriv = dev_get_drvdata(dev); | |
| 2261 | + int err; | |
| 2262 | + u32 ch; | |
| 2263 | + | |
| 2264 | + for_each_set_bit(ch, &gpriv->channels_mask, gpriv->info->max_channels) { | |
| 2265 | + struct rcar_canfd_channel *priv = gpriv->ch[ch]; | |
| 2266 | + struct net_device *ndev = priv->ndev; | |
| 2267 | + | |
| 2268 | + if (!netif_running(ndev)) | |
| 2269 | + continue; | |
| 2270 | + | |
| 2271 | + netif_device_detach(ndev); | |
| 2272 | + | |
| 2273 | + err = rcar_canfd_close(ndev); | |
| 2274 | + if (err) { | |
| 2275 | + netdev_err(ndev, "rcar_canfd_close() failed %pe\n", | |
| 2276 | + ERR_PTR(err)); | |
| 2277 | + return err; | |
| 2278 | + } | |
| 2279 | + | |
| 2280 | + priv->can.state = CAN_STATE_SLEEPING; | |
| 2281 | + } | |
| 2282 | + | |
| 2283 | + /* TODO Skip if wake-up (which is not yet supported) is enabled */ | |
| 2284 | + rcar_canfd_global_deinit(gpriv, false); | |
| 2285 | + | |
| 2223 | 2286 | return 0; |
| 2224 | 2287 | } |
| 2225 | 2288 | |
| 2226 | −static int __maybe_unused rcar_canfd_resume(struct device *dev) | |
| 2289 | +static int rcar_canfd_resume(struct device *dev) | |
| 2227 | 2290 | { |
| 2291 | + struct rcar_canfd_global *gpriv = dev_get_drvdata(dev); | |
| 2292 | + int err; | |
| 2293 | + u32 ch; | |
| 2294 | + | |
| 2295 | + err = rcar_canfd_global_init(gpriv); | |
| 2296 | + if (err) { | |
| 2297 | + dev_err(dev, "rcar_canfd_global_init() failed %pe\n", ERR_PTR(err)); | |
| 2298 | + return err; | |
| 2299 | + } | |
| 2300 | + | |
| 2301 | + for_each_set_bit(ch, &gpriv->channels_mask, gpriv->info->max_channels) { | |
| 2302 | + struct rcar_canfd_channel *priv = gpriv->ch[ch]; | |
| 2303 | + struct net_device *ndev = priv->ndev; | |
| 2304 | + | |
| 2305 | + if (!netif_running(ndev)) | |
| 2306 | + continue; | |
| 2307 | + | |
| 2308 | + err = rcar_canfd_open(ndev); | |
| 2309 | + if (err) { | |
| 2310 | + netdev_err(ndev, "rcar_canfd_open() failed %pe\n", | |
| 2311 | + ERR_PTR(err)); | |
| 2312 | + return err; | |
| 2313 | + } | |
| 2314 | + | |
| 2315 | + netif_device_attach(ndev); | |
| 2316 | + } | |
| 2317 | + | |
| 2228 | 2318 | return 0; |
| 2229 | 2319 | } |
| 2230 | 2320 | |
| 2231 | −static SIMPLE_DEV_PM_OPS(rcar_canfd_pm_ops, rcar_canfd_suspend, | |
| 2232 | − rcar_canfd_resume); | |
| 2321 | +static DEFINE_SIMPLE_DEV_PM_OPS(rcar_canfd_pm_ops, rcar_canfd_suspend, | |
| 2322 | + rcar_canfd_resume); | |
| 2233 | 2323 | |
| 2234 | 2324 | static const __maybe_unused struct of_device_id rcar_canfd_of_table[] = { |
| 2235 | 2325 | { .compatible = "renesas,r8a779a0-canfd", .data = &rcar_gen4_hw_info }, |
| @@ -2246,7 +2336,7 @@ static struct platform_driver rcar_canfd_driver = { · 10 unmodified lines | ||
| 2246 | 2336 | .driver = { |
| 2247 | 2337 | .name = RCANFD_DRV_NAME, |
| 2248 | 2338 | .of_match_table = of_match_ptr(rcar_canfd_of_table), |
| 2249 | − .pm = &rcar_canfd_pm_ops, | |
| 2339 | + .pm = pm_sleep_ptr(&rcar_canfd_pm_ops), | |
| 2250 | 2340 | }, |
| 2251 | 2341 | .probe = rcar_canfd_probe, |
| 2252 | 2342 | .remove = rcar_canfd_remove, |
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | 2 | /* Copyright (c) 2020 Pengutronix, Marc Kleine-Budde <kernel@pengutronix.de> |
| 3 | − * Copyright (c) 2021 Vincent Mailhol <mailhol.vincent@wanadoo.fr> | |
| 3 | + * Copyright (c) 2021-2025 Vincent Mailhol <mailhol@kernel.org> | |
| 4 | 4 | */ |
| 5 | 5 | |
| 6 | 6 | #ifndef _CAN_BITTIMING_H |
| @@ -16,10 +16,12 @@ · 9 unmodified lines | ||
| 16 | 16 | |
| 17 | 17 | #define CAN_CTRLMODE_FD_TDC_MASK \ |
| 18 | 18 | (CAN_CTRLMODE_TDC_AUTO | CAN_CTRLMODE_TDC_MANUAL) |
| 19 | +#define CAN_CTRLMODE_XL_TDC_MASK \ | |
| 20 | + (CAN_CTRLMODE_XL_TDC_AUTO | CAN_CTRLMODE_XL_TDC_MANUAL) | |
| 19 | 21 | #define CAN_CTRLMODE_TDC_AUTO_MASK \ |
| 20 | − (CAN_CTRLMODE_TDC_AUTO) | |
| 22 | + (CAN_CTRLMODE_TDC_AUTO | CAN_CTRLMODE_XL_TDC_AUTO) | |
| 21 | 23 | #define CAN_CTRLMODE_TDC_MANUAL_MASK \ |
| 22 | − (CAN_CTRLMODE_TDC_MANUAL) | |
| 24 | + (CAN_CTRLMODE_TDC_MANUAL | CAN_CTRLMODE_XL_TDC_MANUAL) | |
| 23 | 25 | |
| 24 | 26 | /* |
| 25 | 27 | * struct can_tdc - CAN FD Transmission Delay Compensation parameters |
| @@ -85,6 +87,11 @@ struct can_tdc { · 59 unmodified lines | ||
| 85 | 87 | u32 tdcf; |
| 86 | 88 | }; |
| 87 | 89 | |
| 90 | +/* The transceiver decoding margin corresponds to t_Decode in ISO 11898-2 */ | |
| 91 | +#define CAN_PWM_DECODE_NS 5 | |
| 92 | +/* Maximum PWM symbol duration. Corresponds to t_SymbolNom_MAX - t_Decode */ | |
| 93 | +#define CAN_PWM_NS_MAX (205 - CAN_PWM_DECODE_NS) | |
| 94 | + | |
| 88 | 95 | /* |
| 89 | 96 | * struct can_tdc_const - CAN hardware-dependent constant for |
| 90 | 97 | * Transmission Delay Compensation |
| @@ -118,11 +125,48 @@ struct can_tdc_const { · 27 unmodified lines | ||
| 118 | 125 | u32 tdcf_max; |
| 119 | 126 | }; |
| 120 | 127 | |
| 128 | +/* | |
| 129 | + * struct can_pwm - CAN Pulse-Width Modulation (PWM) parameters | |
| 130 | + * | |
| 131 | + * @pwms: pulse width modulation short phase | |
| 132 | + * @pwml: pulse width modulation long phase | |
| 133 | + * @pwmo: pulse width modulation offset | |
| 134 | + */ | |
| 135 | +struct can_pwm { | |
| 136 | + u32 pwms; | |
| 137 | + u32 pwml; | |
| 138 | + u32 pwmo; | |
| 139 | +}; | |
| 140 | + | |
| 141 | +/* | |
| 142 | + * struct can_pwm - CAN hardware-dependent constants for Pulse-Width | |
| 143 | + * Modulation (PWM) | |
| 144 | + * | |
| 145 | + * @pwms_min: PWM short phase minimum value. Must be at least 1. | |
| 146 | + * @pwms_max: PWM short phase maximum value | |
| 147 | + * @pwml_min: PWM long phase minimum value. Must be at least 1. | |
| 148 | + * @pwml_max: PWM long phase maximum value | |
| 149 | + * @pwmo_min: PWM offset phase minimum value | |
| 150 | + * @pwmo_max: PWM offset phase maximum value | |
| 151 | + */ | |
| 152 | +struct can_pwm_const { | |
| 153 | + u32 pwms_min; | |
| 154 | + u32 pwms_max; | |
| 155 | + u32 pwml_min; | |
| 156 | + u32 pwml_max; | |
| 157 | + u32 pwmo_min; | |
| 158 | + u32 pwmo_max; | |
| 159 | +}; | |
| 160 | + | |
| 121 | 161 | struct data_bittiming_params { |
| 122 | 162 | const struct can_bittiming_const *data_bittiming_const; |
| 123 | 163 | struct can_bittiming data_bittiming; |
| 124 | 164 | const struct can_tdc_const *tdc_const; |
| 125 | − struct can_tdc tdc; | |
| 165 | + const struct can_pwm_const *pwm_const; | |
| 166 | + union { | |
| 167 | + struct can_tdc tdc; | |
| 168 | + struct can_pwm pwm; | |
| 169 | + }; | |
| 126 | 170 | const u32 *data_bitrate_const; |
| 127 | 171 | unsigned int data_bitrate_const_cnt; |
| 128 | 172 | int (*do_set_data_bittiming)(struct net_device *dev); |
| @@ -136,12 +180,14 @@ int can_calc_bittiming(const struct net_device *dev, struct can_bittiming *bt, · 7 unmodified lines | ||
| 136 | 180 | void can_calc_tdco(struct can_tdc *tdc, const struct can_tdc_const *tdc_const, |
| 137 | 181 | const struct can_bittiming *dbt, |
| 138 | 182 | u32 tdc_mask, u32 *ctrlmode, u32 ctrlmode_supported); |
| 183 | + | |
| 184 | +int can_calc_pwm(struct net_device *dev, struct netlink_ext_ack *extack); | |
| 139 | 185 | #else /* !CONFIG_CAN_CALC_BITTIMING */ |
| 140 | 186 | static inline int |
| 141 | 187 | can_calc_bittiming(const struct net_device *dev, struct can_bittiming *bt, |
| 142 | 188 | const struct can_bittiming_const *btc, struct netlink_ext_ack *extack) |
| 143 | 189 | { |
| 144 | − netdev_err(dev, "bit-timing calculation not available\n"); | |
| 190 | + NL_SET_ERR_MSG(extack, "bit-timing calculation not available\n"); | |
| 145 | 191 | return -EINVAL; |
| 146 | 192 | } |
| 147 | 193 | |
| @@ -151,6 +197,14 @@ can_calc_tdco(struct can_tdc *tdc, const struct can_tdc_const *tdc_const, · 3 unmodified lines | ||
| 151 | 197 | u32 tdc_mask, u32 *ctrlmode, u32 ctrlmode_supported) |
| 152 | 198 | { |
| 153 | 199 | } |
| 200 | + | |
| 201 | +static inline int | |
| 202 | +can_calc_pwm(struct net_device *dev, struct netlink_ext_ack *extack) | |
| 203 | +{ | |
| 204 | + NL_SET_ERR_MSG(extack, | |
| 205 | + "bit-timing calculation not available: manually provide PWML and PWMS\n"); | |
| 206 | + return -EINVAL; | |
| 207 | +} | |
| 154 | 208 | #endif /* CONFIG_CAN_CALC_BITTIMING */ |
| 155 | 209 | |
| 156 | 210 | void can_sjw_set_default(struct can_bittiming *bt); |
| @@ -164,6 +218,10 @@ int can_get_bittiming(const struct net_device *dev, struct can_bittiming *bt, · 7 unmodified lines | ||
| 164 | 218 | const unsigned int bitrate_const_cnt, |
| 165 | 219 | struct netlink_ext_ack *extack); |
| 166 | 220 | |
| 221 | +int can_validate_pwm_bittiming(const struct net_device *dev, | |
| 222 | + const struct can_pwm *pwm, | |
| 223 | + struct netlink_ext_ack *extack); | |
| 224 | + | |
| 167 | 225 | /* |
| 168 | 226 | * can_get_relative_tdco() - TDCO relative to the sample point |
| 169 | 227 | * |
| @@ -206,4 +264,17 @@ static inline unsigned int can_bit_time(const struct can_bittiming *bt) · 36 unmodified lines | ||
| 206 | 264 | return CAN_SYNC_SEG + bt->prop_seg + bt->phase_seg1 + bt->phase_seg2; |
| 207 | 265 | } |
| 208 | 266 | |
| 267 | +/* Duration of one bit in minimum time quantum */ | |
| 268 | +static inline unsigned int can_bit_time_tqmin(const struct can_bittiming *bt) | |
| 269 | +{ | |
| 270 | + return can_bit_time(bt) * bt->brp; | |
| 271 | +} | |
| 272 | + | |
| 273 | +/* Convert a duration from minimum a minimum time quantum to nano seconds */ | |
| 274 | +static inline u32 can_tqmin_to_ns(u32 tqmin, u32 clock_freq) | |
| 275 | +{ | |
| 276 | + return DIV_U64_ROUND_CLOSEST(mul_u32_u32(tqmin, NSEC_PER_SEC), | |
| 277 | + clock_freq); | |
| 278 | +} | |
| 279 | + | |
| 209 | 280 | #endif /* !_CAN_BITTIMING_H */ |
| @@ -47,7 +47,7 @@ struct can_priv { · 46 unmodified lines | ||
| 47 | 47 | |
| 48 | 48 | const struct can_bittiming_const *bittiming_const; |
| 49 | 49 | struct can_bittiming bittiming; |
| 50 | − struct data_bittiming_params fd; | |
| 50 | + struct data_bittiming_params fd, xl; | |
| 51 | 51 | unsigned int bitrate_const_cnt; |
| 52 | 52 | const u32 *bitrate_const; |
| 53 | 53 | u32 bitrate_max; |
| @@ -85,6 +85,11 @@ static inline bool can_fd_tdc_is_enabled(const struct can_priv *priv) · 31 unmodified lines | ||
| 85 | 85 | return !!(priv->ctrlmode & CAN_CTRLMODE_FD_TDC_MASK); |
| 86 | 86 | } |
| 87 | 87 | |
| 88 | +static inline bool can_xl_tdc_is_enabled(const struct can_priv *priv) | |
| 89 | +{ | |
| 90 | + return !!(priv->ctrlmode & CAN_CTRLMODE_XL_TDC_MASK); | |
| 91 | +} | |
| 92 | + | |
| 88 | 93 | static inline u32 can_get_static_ctrlmode(struct can_priv *priv) |
| 89 | 94 | { |
| 90 | 95 | return priv->ctrlmode & ~priv->ctrlmode_supported; |
| @@ -95,22 +100,6 @@ static inline bool can_is_canxl_dev_mtu(unsigned int mtu) · 4 unmodified lines | ||
| 95 | 100 | return (mtu >= CANXL_MIN_MTU && mtu <= CANXL_MAX_MTU); |
| 96 | 101 | } |
| 97 | 102 | |
| 98 | −/* drop skb if it does not contain a valid CAN frame for sending */ | |
| 99 | −static inline bool can_dev_dropped_skb(struct net_device *dev, struct sk_buff *skb) | |
| 100 | −{ | |
| 101 | − struct can_priv *priv = netdev_priv(dev); | |
| 102 | − | |
| 103 | − if (priv->ctrlmode & CAN_CTRLMODE_LISTENONLY) { | |
| 104 | − netdev_info_once(dev, | |
| 105 | − "interface in listen only mode, dropping skb\n"); | |
| 106 | − kfree_skb(skb); | |
| 107 | − dev->stats.tx_dropped++; | |
| 108 | − return true; | |
| 109 | − } | |
| 110 | − | |
| 111 | − return can_dropped_invalid_skb(dev, skb); | |
| 112 | −} | |
| 113 | − | |
| 114 | 103 | void can_setup(struct net_device *dev); |
| 115 | 104 | |
| 116 | 105 | struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max, |
| @@ -146,6 +135,51 @@ void can_bus_off(struct net_device *dev); · 29 unmodified lines | ||
| 146 | 135 | const char *can_get_state_str(const enum can_state state); |
| 147 | 136 | const char *can_get_ctrlmode_str(u32 ctrlmode); |
| 148 | 137 | |
| 138 | +static inline bool can_dev_in_xl_only_mode(struct can_priv *priv) | |
| 139 | +{ | |
| 140 | + const u32 mixed_mode = CAN_CTRLMODE_FD | CAN_CTRLMODE_XL; | |
| 141 | + | |
| 142 | + /* When CAN XL is enabled but FD is disabled we are running in | |
| 143 | + * the so-called 'CANXL-only mode' where the error signalling is | |
| 144 | + * disabled. This helper function determines the required value | |
| 145 | + * to disable error signalling in the CAN XL controller. | |
| 146 | + * The so-called CC/FD/XL 'mixed mode' requires error signalling. | |
| 147 | + */ | |
| 148 | + return ((priv->ctrlmode & mixed_mode) == CAN_CTRLMODE_XL); | |
| 149 | +} | |
| 150 | + | |
| 151 | +/* drop skb if it does not contain a valid CAN frame for sending */ | |
| 152 | +static inline bool can_dev_dropped_skb(struct net_device *dev, struct sk_buff *skb) | |
| 153 | +{ | |
| 154 | + struct can_priv *priv = netdev_priv(dev); | |
| 155 | + u32 silent_mode = priv->ctrlmode & (CAN_CTRLMODE_LISTENONLY | | |
| 156 | + CAN_CTRLMODE_RESTRICTED); | |
| 157 | + | |
| 158 | + if (silent_mode) { | |
| 159 | + netdev_info_once(dev, "interface in %s mode, dropping skb\n", | |
| 160 | + can_get_ctrlmode_str(silent_mode)); | |
| 161 | + goto invalid_skb; | |
| 162 | + } | |
| 163 | + | |
| 164 | + if (!(priv->ctrlmode & CAN_CTRLMODE_FD) && can_is_canfd_skb(skb)) { | |
| 165 | + netdev_info_once(dev, "CAN FD is disabled, dropping skb\n"); | |
| 166 | + goto invalid_skb; | |
| 167 | + } | |
| 168 | + | |
| 169 | + if (can_dev_in_xl_only_mode(priv) && !can_is_canxl_skb(skb)) { | |
| 170 | + netdev_info_once(dev, | |
| 171 | + "Error signaling is disabled, dropping skb\n"); | |
| 172 | + goto invalid_skb; | |
| 173 | + } | |
| 174 | + | |
| 175 | + return can_dropped_invalid_skb(dev, skb); | |
| 176 | + | |
| 177 | +invalid_skb: | |
| 178 | + kfree_skb(skb); | |
| 179 | + dev->stats.tx_dropped++; | |
| 180 | + return true; | |
| 181 | +} | |
| 182 | + | |
| 149 | 183 | void can_state_get_by_berr_counter(const struct net_device *dev, |
| 150 | 184 | const struct can_berr_counter *bec, |
| 151 | 185 | enum can_state *tx_state, |
| @@ -5,6 +5,7 @@ · 4 unmodified lines | ||
| 5 | 5 | * Definitions for the CAN netlink interface |
| 6 | 6 | * |
| 7 | 7 | * Copyright (c) 2009 Wolfgang Grandegger <wg@grandegger.com> |
| 8 | + * Copyright (c) 2021-2025 Vincent Mailhol <mailhol@kernel.org> | |
| 8 | 9 | * |
| 9 | 10 | * This program is free software; you can redistribute it and/or modify |
| 10 | 11 | * it under the terms of the version 2 of the GNU General Public License |
| @@ -103,6 +104,11 @@ struct can_ctrlmode { · 92 unmodified lines | ||
| 103 | 104 | #define CAN_CTRLMODE_CC_LEN8_DLC 0x100 /* Classic CAN DLC option */ |
| 104 | 105 | #define CAN_CTRLMODE_TDC_AUTO 0x200 /* FD transceiver automatically calculates TDCV */ |
| 105 | 106 | #define CAN_CTRLMODE_TDC_MANUAL 0x400 /* FD TDCV is manually set up by user */ |
| 107 | +#define CAN_CTRLMODE_RESTRICTED 0x800 /* Restricted operation mode */ | |
| 108 | +#define CAN_CTRLMODE_XL 0x1000 /* CAN XL mode */ | |
| 109 | +#define CAN_CTRLMODE_XL_TDC_AUTO 0x2000 /* XL transceiver automatically calculates TDCV */ | |
| 110 | +#define CAN_CTRLMODE_XL_TDC_MANUAL 0x4000 /* XL TDCV is manually set up by user */ | |
| 111 | +#define CAN_CTRLMODE_XL_TMS 0x8000 /* Transceiver Mode Switching */ | |
| 106 | 112 | |
| 107 | 113 | /* |
| 108 | 114 | * CAN device statistics |
| @@ -138,6 +144,11 @@ enum { · 29 unmodified lines | ||
| 138 | 144 | IFLA_CAN_BITRATE_MAX, |
| 139 | 145 | IFLA_CAN_TDC, /* FD */ |
| 140 | 146 | IFLA_CAN_CTRLMODE_EXT, |
| 147 | + IFLA_CAN_XL_DATA_BITTIMING, | |
| 148 | + IFLA_CAN_XL_DATA_BITTIMING_CONST, | |
| 149 | + IFLA_CAN_XL_DATA_BITRATE_CONST, | |
| 150 | + IFLA_CAN_XL_TDC, | |
| 151 | + IFLA_CAN_XL_PWM, | |
| 141 | 152 | |
| 142 | 153 | /* add new constants above here */ |
| 143 | 154 | __IFLA_CAN_MAX, |
| @@ -179,6 +190,29 @@ enum { · 35 unmodified lines | ||
| 179 | 190 | IFLA_CAN_CTRLMODE_MAX = __IFLA_CAN_CTRLMODE - 1 |
| 180 | 191 | }; |
| 181 | 192 | |
| 193 | +/* | |
| 194 | + * CAN FD/XL Pulse-Width Modulation (PWM) | |
| 195 | + * | |
| 196 | + * Please refer to struct can_pwm_const and can_pwm in | |
| 197 | + * include/linux/can/bittiming.h for further details. | |
| 198 | + */ | |
| 199 | +enum { | |
| 200 | + IFLA_CAN_PWM_UNSPEC, | |
| 201 | + IFLA_CAN_PWM_PWMS_MIN, /* u32 */ | |
| 202 | + IFLA_CAN_PWM_PWMS_MAX, /* u32 */ | |
| 203 | + IFLA_CAN_PWM_PWML_MIN, /* u32 */ | |
| 204 | + IFLA_CAN_PWM_PWML_MAX, /* u32 */ | |
| 205 | + IFLA_CAN_PWM_PWMO_MIN, /* u32 */ | |
| 206 | + IFLA_CAN_PWM_PWMO_MAX, /* u32 */ | |
| 207 | + IFLA_CAN_PWM_PWMS, /* u32 */ | |
| 208 | + IFLA_CAN_PWM_PWML, /* u32 */ | |
| 209 | + IFLA_CAN_PWM_PWMO, /* u32 */ | |
| 210 | + | |
| 211 | + /* add new constants above here */ | |
| 212 | + __IFLA_CAN_PWM, | |
| 213 | + IFLA_CAN_PWM_MAX = __IFLA_CAN_PWM - 1 | |
| 214 | +}; | |
| 215 | + | |
| 182 | 216 | /* u16 termination range: 1..65535 Ohms */ |
| 183 | 217 | #define CAN_TERMINATION_DISABLED 0 |
| 184 | 218 | |
| @@ -892,20 +892,58 @@ static void raw_put_canxl_vcid(struct raw_sock *ro, struct sk_buff *skb) · 891 unmodified lines | ||
| 892 | 892 | } |
| 893 | 893 | } |
| 894 | 894 | |
| 895 | −static unsigned int raw_check_txframe(struct raw_sock *ro, struct sk_buff *skb, int mtu) | |
| 895 | +static inline bool raw_dev_cc_enabled(struct net_device *dev, | |
| 896 | + struct can_priv *priv) | |
| 896 | 897 | { |
| 897 | − /* Classical CAN -> no checks for flags and device capabilities */ | |
| 898 | − if (can_is_can_skb(skb)) | |
| 898 | + /* The CANXL-only mode disables error-signalling on the CAN bus | |
| 899 | + * which is needed to send CAN CC/FD frames | |
| 900 | + */ | |
| 901 | + if (priv) | |
| 902 | + return !can_dev_in_xl_only_mode(priv); | |
| 903 | + | |
| 904 | + /* virtual CAN interfaces always support CAN CC */ | |
| 905 | + return true; | |
| 906 | +} | |
| 907 | + | |
| 908 | +static inline bool raw_dev_fd_enabled(struct net_device *dev, | |
| 909 | + struct can_priv *priv) | |
| 910 | +{ | |
| 911 | + /* check FD ctrlmode on real CAN interfaces */ | |
| 912 | + if (priv) | |
| 913 | + return (priv->ctrlmode & CAN_CTRLMODE_FD); | |
| 914 | + | |
| 915 | + /* check MTU for virtual CAN FD interfaces */ | |
| 916 | + return (READ_ONCE(dev->mtu) >= CANFD_MTU); | |
| 917 | +} | |
| 918 | + | |
| 919 | +static inline bool raw_dev_xl_enabled(struct net_device *dev, | |
| 920 | + struct can_priv *priv) | |
| 921 | +{ | |
| 922 | + /* check XL ctrlmode on real CAN interfaces */ | |
| 923 | + if (priv) | |
| 924 | + return (priv->ctrlmode & CAN_CTRLMODE_XL); | |
| 925 | + | |
| 926 | + /* check MTU for virtual CAN XL interfaces */ | |
| 927 | + return can_is_canxl_dev_mtu(READ_ONCE(dev->mtu)); | |
| 928 | +} | |
| 929 | + | |
| 930 | +static unsigned int raw_check_txframe(struct raw_sock *ro, struct sk_buff *skb, | |
| 931 | + struct net_device *dev) | |
| 932 | +{ | |
| 933 | + struct can_priv *priv = safe_candev_priv(dev); | |
| 934 | + | |
| 935 | + /* Classical CAN */ | |
| 936 | + if (can_is_can_skb(skb) && raw_dev_cc_enabled(dev, priv)) | |
| 899 | 937 | return CAN_MTU; |
| 900 | 938 | |
| 901 | − /* CAN FD -> needs to be enabled and a CAN FD or CAN XL device */ | |
| 939 | + /* CAN FD */ | |
| 902 | 940 | if (ro->fd_frames && can_is_canfd_skb(skb) && |
| 903 | − (mtu == CANFD_MTU || can_is_canxl_dev_mtu(mtu))) | |
| 941 | + raw_dev_fd_enabled(dev, priv)) | |
| 904 | 942 | return CANFD_MTU; |
| 905 | 943 | |
| 906 | − /* CAN XL -> needs to be enabled and a CAN XL device */ | |
| 944 | + /* CAN XL */ | |
| 907 | 945 | if (ro->xl_frames && can_is_canxl_skb(skb) && |
| 908 | − can_is_canxl_dev_mtu(mtu)) | |
| 946 | + raw_dev_xl_enabled(dev, priv)) | |
| 909 | 947 | return CANXL_MTU; |
| 910 | 948 | |
| 911 | 949 | return 0; |
| @@ -961,7 +999,7 @@ static int raw_sendmsg(struct socket *sock, struct msghdr *msg, size_t size) · 49 unmodified lines | ||
| 961 | 999 | err = -EINVAL; |
| 962 | 1000 | |
| 963 | 1001 | /* check for valid CAN (CC/FD/XL) frame content */ |
| 964 | − txmtu = raw_check_txframe(ro, skb, READ_ONCE(dev->mtu)); | |
| 1002 | + txmtu = raw_check_txframe(ro, skb, dev); | |
| 965 | 1003 | if (!txmtu) |
| 966 | 1004 | goto free_skb; |
| 967 | 1005 | |