History

Merge tag 'linux-can-next-for-6.19-20251126' of git://git.kernel.org/pub/scm/linux/kernel/git/mkl/linux-can-next

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 ... ====================

Paolo Abeni committed 11 months ago

Commit 73f784b2 parents 45d100e 9aea35e14 files · +1166 −171Browse files

Unified · Split

14 files
  1. Documentation/devicetree/bindings/net/can/microchip,mpfs-can.yaml +5 −0
  2. MAINTAINERS +3 −5
  3. drivers/net/can/Kconfig +17 −0
  4. drivers/net/can/Makefile +1 −0
  5. drivers/net/can/dev/bittiming.c +63 −0
  6. drivers/net/can/dev/calc_bittiming.c +88 −26
  7. drivers/net/can/dev/dev.c +29 −13
  8. drivers/net/can/dev/netlink.c +300 −19
  9. drivers/net/can/dummy_can.c +285 −0
  10. drivers/net/can/rcar/rcar_canfd.c +168 −78
  11. include/linux/can/bittiming.h +76 −5
  12. include/linux/can/dev.h +51 −17
  13. include/uapi/linux/can/netlink.h +34 −0
  14. net/can/raw.c +46 −8
Documentation/devicetree/bindings/net/can/microchip,mpfs-can.yaml+5−0
@@ -32,11 +32,15 @@ properties: · 31 unmodified lines
32 - description: AHB peripheral clock32 - description: AHB peripheral clock
33 - description: CAN bus clock33 - description: CAN bus clock
34 34
35 resets:
36 maxItems: 1
37
35required:38required:
36 - compatible39 - compatible
37 - reg40 - reg
38 - interrupts41 - interrupts
39 - clocks42 - clocks
43 - resets
40 44
41additionalProperties: false45additionalProperties: false
42 46
@@ -46,6 +50,7 @@ examples: · 3 unmodified lines
46 compatible = "microchip,mpfs-can";50 compatible = "microchip,mpfs-can";
47 reg = <0x2010c000 0x1000>;51 reg = <0x2010c000 0x1000>;
48 clocks = <&clkcfg 17>, <&clkcfg 37>;52 clocks = <&clkcfg 17>, <&clkcfg 37>;
53 resets = <&clkcfg 17>;
49 interrupt-parent = <&plic>;54 interrupt-parent = <&plic>;
50 interrupts = <56>;55 interrupts = <56>;
51 };56 };
MAINTAINERS+3−5
@@ -15412,14 +15412,12 @@ S: Supported · 15411 unmodified lines
15412F: drivers/net/phy/mxl-86110.c15412F: drivers/net/phy/mxl-86110.c
15413F: drivers/net/phy/mxl-gpy.c15413F: drivers/net/phy/mxl-gpy.c
15414 15414
15415MCAN MMIO DEVICE DRIVER15415MCAN DEVICE DRIVER
15416M: Chandrasekar Ramakrishnan <rcsekar@samsung.com>15416M: Markus Schneider-Pargmann <msp@baylibre.com>
15417L: linux-can@vger.kernel.org15417L: linux-can@vger.kernel.org
15418S: Maintained15418S: Maintained
15419F: Documentation/devicetree/bindings/net/can/bosch,m_can.yaml15419F: Documentation/devicetree/bindings/net/can/bosch,m_can.yaml
15420F: drivers/net/can/m_can/m_can.c15420F: drivers/net/can/m_can/
15421F: drivers/net/can/m_can/m_can.h
15422F: drivers/net/can/m_can/m_can_platform.c
15423 15421
15424MCBA MICROCHIP CAN BUS ANALYZER TOOL DRIVER15422MCBA MICROCHIP CAN BUS ANALYZER TOOL DRIVER
15425R: Yasushi SHOJI <yashi@spacecubics.com>15423R: Yasushi SHOJI <yashi@spacecubics.com>
drivers/net/can/Kconfig+17−0
@@ -124,6 +124,23 @@ config CAN_CAN327 · 123 unmodified lines
124 124
125 If this driver is built as a module, it will be called can327.125 If this driver is built as a module, it will be called can327.
126 126
127config 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
127config CAN_FLEXCAN144config CAN_FLEXCAN
128 tristate "Support for Freescale FLEXCAN based chips"145 tristate "Support for Freescale FLEXCAN based chips"
129 depends on OF || COLDFIRE || COMPILE_TEST146 depends on OF || COLDFIRE || COMPILE_TEST
drivers/net/can/Makefile+1−0
@@ -21,6 +21,7 @@ obj-$(CONFIG_CAN_CAN327) += can327.o · 20 unmodified lines
21obj-$(CONFIG_CAN_CC770) += cc770/21obj-$(CONFIG_CAN_CC770) += cc770/
22obj-$(CONFIG_CAN_C_CAN) += c_can/22obj-$(CONFIG_CAN_C_CAN) += c_can/
23obj-$(CONFIG_CAN_CTUCANFD) += ctucanfd/23obj-$(CONFIG_CAN_CTUCANFD) += ctucanfd/
24obj-$(CONFIG_CAN_DUMMY) += dummy_can.o
24obj-$(CONFIG_CAN_FLEXCAN) += flexcan/25obj-$(CONFIG_CAN_FLEXCAN) += flexcan/
25obj-$(CONFIG_CAN_GRCAN) += grcan.o26obj-$(CONFIG_CAN_GRCAN) += grcan.o
26obj-$(CONFIG_CAN_IFI_CANFD) += ifi_canfd/27obj-$(CONFIG_CAN_IFI_CANFD) += ifi_canfd/
drivers/net/can/dev/bittiming.c+63−0
@@ -2,6 +2,7 @@ · 1 unmodified line
2/* Copyright (C) 2005 Marc Kleine-Budde, Pengutronix2/* Copyright (C) 2005 Marc Kleine-Budde, Pengutronix
3 * Copyright (C) 2006 Andrey Volkov, Varma Electronics3 * Copyright (C) 2006 Andrey Volkov, Varma Electronics
4 * Copyright (C) 2008-2009 Wolfgang Grandegger <wg@grandegger.com>4 * Copyright (C) 2008-2009 Wolfgang Grandegger <wg@grandegger.com>
5 * Copyright (c) 2025 Vincent Mailhol <mailhol@kernel.org>
5 */6 */
6 7
7#include <linux/can/dev.h>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 return -EINVAL;153 return -EINVAL;
153}154}
155
156int 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}
drivers/net/can/dev/calc_bittiming.c+88−26
@@ -2,6 +2,7 @@ · 1 unmodified line
2/* Copyright (C) 2005 Marc Kleine-Budde, Pengutronix2/* Copyright (C) 2005 Marc Kleine-Budde, Pengutronix
3 * Copyright (C) 2006 Andrey Volkov, Varma Electronics3 * Copyright (C) 2006 Andrey Volkov, Varma Electronics
4 * Copyright (C) 2008-2009 Wolfgang Grandegger <wg@grandegger.com>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#include <linux/units.h>8#include <linux/units.h>
@@ -9,6 +10,33 @@ · 1 unmodified line
9 10
10#define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */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. */
14static 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. */
26static 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/* Bit-timing calculation derived from:40/* Bit-timing calculation derived from:
13 *41 *
14 * Code based on LinCAN sources and H8S2638 project42 * Code based on LinCAN sources and H8S2638 project
@@ -23,7 +51,7 @@ · 8 unmodified lines
23 */51 */
24static int52static int
25can_update_sample_point(const struct can_bittiming_const *btc,53can_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 unsigned int *tseg1_ptr, unsigned int *tseg2_ptr,55 unsigned int *tseg1_ptr, unsigned int *tseg2_ptr,
28 unsigned int *sample_point_error_ptr)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 for (i = 0; i <= 1; i++) {63 for (i = 0; i <= 1; i++) {
36 tseg2 = tseg + CAN_SYNC_SEG -64 tseg2 = tseg + CAN_SYNC_SEG -
37 (sample_point_nominal * (tseg + CAN_SYNC_SEG)) /65 (sample_point_reference * (tseg + CAN_SYNC_SEG)) /
38 1000 - i;66 1000 - i;
39 tseg2 = clamp(tseg2, btc->tseg2_min, btc->tseg2_max);67 tseg2 = clamp(tseg2, btc->tseg2_min, btc->tseg2_max);
40 tseg1 = tseg - tseg2;68 tseg1 = tseg - tseg2;
@@ -45,9 +73,9 @@ can_update_sample_point(const struct can_bittiming_const *btc, · 4 unmodified lines
45 73
46 sample_point = 1000 * (tseg + CAN_SYNC_SEG - tseg2) /74 sample_point = 1000 * (tseg + CAN_SYNC_SEG - tseg2) /
47 (tseg + CAN_SYNC_SEG);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 sample_point_error < best_sample_point_error) {79 sample_point_error < best_sample_point_error) {
52 best_sample_point = sample_point;80 best_sample_point = sample_point;
53 best_sample_point_error = sample_point_error;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 struct can_priv *priv = netdev_priv(dev);96 struct can_priv *priv = netdev_priv(dev);
69 unsigned int bitrate; /* current bitrate */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 unsigned int best_bitrate_error = UINT_MAX;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 unsigned int best_sample_point_error = UINT_MAX;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 unsigned int best_tseg = 0; /* current best value for tseg */103 unsigned int best_tseg = 0; /* current best value for tseg */
76 unsigned int best_brp = 0; /* current best value for brp */104 unsigned int best_brp = 0; /* current best value for brp */
77 unsigned int brp, tsegall, tseg, tseg1 = 0, tseg2 = 0;105 unsigned int brp, tsegall, tseg, tseg1 = 0, tseg2 = 0;
78 u64 v64;106 u64 v64;
79 int err;107 int err;
80 108
81 /* Use CiA recommended sample points */109 if (bt->sample_point)
82 if (bt->sample_point) {110 sample_point_reference = bt->sample_point;
83 sample_point_nominal = bt->sample_point;111 else if (btc == priv->xl.data_bittiming_const &&
84 } else {112 (priv->ctrlmode & CAN_CTRLMODE_XL_TMS))
85 if (bt->bitrate > 800 * KILO /* BPS */)113 sample_point_reference = can_calc_sample_point_pwm(bt);
86 sample_point_nominal = 750;114 else
87 else if (bt->bitrate > 500 * KILO /* BPS */)115 sample_point_reference = can_calc_sample_point_nrz(bt);
88 sample_point_nominal = 800;
89 else
90 sample_point_nominal = 875;
91 }
92 116
93 /* tseg even = round down, odd = round up */117 /* tseg even = round down, odd = round up */
94 for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;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 if (bitrate_error < best_bitrate_error)138 if (bitrate_error < best_bitrate_error)
115 best_sample_point_error = UINT_MAX;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 &tseg1, &tseg2, &sample_point_error);142 &tseg1, &tseg2, &sample_point_error);
119 if (sample_point_error >= best_sample_point_error)143 if (sample_point_error >= best_sample_point_error)
120 continue;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 if (best_bitrate_error) {155 if (best_bitrate_error) {
132 /* Error in one-tenth of a percent */156 /* Error in one-hundredth of a percent */
133 v64 = (u64)best_bitrate_error * 1000;157 v64 = (u64)best_bitrate_error * 10000;
134 do_div(v64, bt->bitrate);158 do_div(v64, bt->bitrate);
135 bitrate_error = (u32)v64;159 bitrate_error = (u32)v64;
160 /* print at least 0.01% if the error is smaller */
161 bitrate_error = max(bitrate_error, 1U);
136 if (bitrate_error > CAN_CALC_MAX_ERROR) {162 if (bitrate_error > CAN_CALC_MAX_ERROR) {
137 NL_SET_ERR_MSG_FMT(extack,163 NL_SET_ERR_MSG_FMT(extack,
138 "bitrate error: %u.%u%% too high",164 "bitrate error: %u.%02u%% too high",
139 bitrate_error / 10, bitrate_error % 10);165 bitrate_error / 100,
166 bitrate_error % 100);
140 return -EINVAL;167 return -EINVAL;
141 }168 }
142 NL_SET_ERR_MSG_FMT(extack,169 NL_SET_ERR_MSG_FMT(extack,
143 "bitrate error: %u.%u%%",170 "bitrate error: %u.%02u%%",
144 bitrate_error / 10, bitrate_error % 10);171 bitrate_error / 100, bitrate_error % 100);
145 }172 }
146 173
147 /* real sample point */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 best_tseg, &tseg1, &tseg2,176 best_tseg, &tseg1, &tseg2,
150 NULL);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 *ctrlmode |= tdc_auto;225 *ctrlmode |= tdc_auto;
199 }226 }
200}227}
228
229int 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}
drivers/net/can/dev/dev.c+29−13
@@ -92,29 +92,39 @@ const char *can_get_ctrlmode_str(u32 ctrlmode) · 91 unmodified lines
92{92{
93 switch (ctrlmode & ~(ctrlmode - 1)) {93 switch (ctrlmode & ~(ctrlmode - 1)) {
94 case 0:94 case 0:
95 return "none";95 return "(none)";
96 case CAN_CTRLMODE_LOOPBACK:96 case CAN_CTRLMODE_LOOPBACK:
97 return "loopback";97 return "LOOPBACK";
98 case CAN_CTRLMODE_LISTENONLY:98 case CAN_CTRLMODE_LISTENONLY:
99 return "listen-only";99 return "LISTEN-ONLY";
100 case CAN_CTRLMODE_3_SAMPLES:100 case CAN_CTRLMODE_3_SAMPLES:
101 return "triple-sampling";101 return "TRIPLE-SAMPLING";
102 case CAN_CTRLMODE_ONE_SHOT:102 case CAN_CTRLMODE_ONE_SHOT:
103 return "one-shot";103 return "ONE-SHOT";
104 case CAN_CTRLMODE_BERR_REPORTING:104 case CAN_CTRLMODE_BERR_REPORTING:
105 return "berr-reporting";105 return "BERR-REPORTING";
106 case CAN_CTRLMODE_FD:106 case CAN_CTRLMODE_FD:
107 return "fd";107 return "FD";
108 case CAN_CTRLMODE_PRESUME_ACK:108 case CAN_CTRLMODE_PRESUME_ACK:
109 return "presume-ack";109 return "PRESUME-ACK";
110 case CAN_CTRLMODE_FD_NON_ISO:110 case CAN_CTRLMODE_FD_NON_ISO:
111 return "fd-non-iso";111 return "FD-NON-ISO";
112 case CAN_CTRLMODE_CC_LEN8_DLC:112 case CAN_CTRLMODE_CC_LEN8_DLC:
113 return "cc-len8-dlc";113 return "CC-LEN8-DLC";
114 case CAN_CTRLMODE_TDC_AUTO:114 case CAN_CTRLMODE_TDC_AUTO:
115 return "fd-tdc-auto";115 return "TDC-AUTO";
116 case CAN_CTRLMODE_TDC_MANUAL: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 default:128 default:
119 return "<unknown>";129 return "<unknown>";
120 }130 }
@@ -348,7 +358,13 @@ void can_set_default_mtu(struct net_device *dev) · 227 unmodified lines
348{358{
349 struct can_priv *priv = netdev_priv(dev);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 dev->mtu = CANFD_MTU;368 dev->mtu = CANFD_MTU;
353 dev->min_mtu = CANFD_MTU;369 dev->min_mtu = CANFD_MTU;
354 dev->max_mtu = CANFD_MTU;370 dev->max_mtu = CANFD_MTU;
drivers/net/can/dev/netlink.c+300−19
@@ -2,7 +2,7 @@ · 1 unmodified line
2/* Copyright (C) 2005 Marc Kleine-Budde, Pengutronix2/* Copyright (C) 2005 Marc Kleine-Budde, Pengutronix
3 * Copyright (C) 2006 Andrey Volkov, Varma Electronics3 * Copyright (C) 2006 Andrey Volkov, Varma Electronics
4 * Copyright (C) 2008-2009 Wolfgang Grandegger <wg@grandegger.com>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#include <linux/can/dev.h>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 [IFLA_CAN_TERMINATION] = { .type = NLA_U16 },22 [IFLA_CAN_TERMINATION] = { .type = NLA_U16 },
23 [IFLA_CAN_TDC] = { .type = NLA_NESTED },23 [IFLA_CAN_TDC] = { .type = NLA_NESTED },
24 [IFLA_CAN_CTRLMODE_EXT] = { .type = NLA_NESTED },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
27static const struct nla_policy can_tdc_policy[IFLA_CAN_TDC_MAX + 1] = {31static 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 [IFLA_CAN_TDC_TDCF] = { .type = NLA_U32 },40 [IFLA_CAN_TDC_TDCF] = { .type = NLA_U32 },
37};41};
38 42
43static 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
39static int can_validate_bittiming(struct nlattr *data[],55static int can_validate_bittiming(struct nlattr *data[],
40 struct netlink_ext_ack *extack,56 struct netlink_ext_ack *extack,
41 int ifla_can_bittiming)57 int ifla_can_bittiming)
@@ -70,7 +86,7 @@ static int can_validate_tdc(struct nlattr *data_tdc, · 28 unmodified lines
70 return -EOPNOTSUPP;86 return -EOPNOTSUPP;
71 }87 }
72 88
73 /* If one of the CAN_CTRLMODE_TDC_* flag is set then TDC89 /* If one of the CAN_CTRLMODE_{,XL}_TDC_* flags is set then TDC
74 * must be set and vice-versa90 * must be set and vice-versa
75 */91 */
76 if ((tdc_auto || tdc_manual) && !data_tdc) {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 return -EOPNOTSUPP;98 return -EOPNOTSUPP;
83 }99 }
84 100
85 /* If providing TDC parameters, at least TDCO is needed. TDCV101 /* If providing TDC parameters, at least TDCO is needed. TDCV is
86 * is needed if and only if CAN_CTRLMODE_TDC_MANUAL is set102 * needed if and only if CAN_CTRLMODE_{,XL}_TDC_MANUAL is set
87 */103 */
88 if (data_tdc) {104 if (data_tdc) {
89 struct nlattr *tb_tdc[IFLA_CAN_TDC_MAX + 1];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 return 0;132 return 0;
117}133}
118 134
135static 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
119static int can_validate_databittiming(struct nlattr *data[],169static int can_validate_databittiming(struct nlattr *data[],
120 struct netlink_ext_ack *extack,170 struct netlink_ext_ack *extack,
121 int ifla_can_data_bittiming, u32 flags)171 int ifla_can_data_bittiming, u32 flags)
@@ -126,10 +176,10 @@ static int can_validate_databittiming(struct nlattr *data[], · 4 unmodified lines
126 bool is_on;176 bool is_on;
127 int err;177 int err;
128 178
129 /* Make sure that valid CAN FD configurations always consist of179 /* Make sure that valid CAN FD/XL configurations always consist of
130 * - nominal/arbitration bittiming180 * - nominal/arbitration bittiming
131 * - data bittiming181 * - data bittiming
132 * - control mode with CAN_CTRLMODE_FD set182 * - control mode with CAN_CTRLMODE_{FD,XL} set
133 * - TDC parameters are coherent (details in can_validate_tdc())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 is_on = flags & CAN_CTRLMODE_FD;189 is_on = flags & CAN_CTRLMODE_FD;
140 type = "FD";190 type = "FD";
141 } else {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 if (is_on) {198 if (is_on) {
@@ -175,6 +228,32 @@ static int can_validate_databittiming(struct nlattr *data[], · 29 unmodified lines
175 return 0;228 return 0;
176}229}
177 230
231static 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
178static int can_validate(struct nlattr *tb[], struct nlattr *data[],257static int can_validate(struct nlattr *tb[], struct nlattr *data[],
179 struct netlink_ext_ack *extack)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 struct can_ctrlmode *cm = nla_data(data[IFLA_CAN_CTRLMODE]);267 struct can_ctrlmode *cm = nla_data(data[IFLA_CAN_CTRLMODE]);
189 268
190 flags = cm->flags & cm->mask;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 err = can_validate_bittiming(data, extack, IFLA_CAN_BITTIMING);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 if (err)289 if (err)
200 return err;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 return 0;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 struct can_priv *priv = netdev_priv(dev);308 struct can_priv *priv = netdev_priv(dev);
210 struct can_ctrlmode *cm;309 struct can_ctrlmode *cm;
211 u32 ctrlstatic, maskedflags, notsupp, ctrlstatic_missing;310 u32 ctrlstatic, maskedflags, deactivated, notsupp, ctrlstatic_missing;
212 311
213 if (!data[IFLA_CAN_CTRLMODE])312 if (!data[IFLA_CAN_CTRLMODE])
214 return 0;313 return 0;
@@ -220,6 +319,7 @@ static int can_ctrlmode_changelink(struct net_device *dev, · 5 unmodified lines
220 cm = nla_data(data[IFLA_CAN_CTRLMODE]);319 cm = nla_data(data[IFLA_CAN_CTRLMODE]);
221 ctrlstatic = can_get_static_ctrlmode(priv);320 ctrlstatic = can_get_static_ctrlmode(priv);
222 maskedflags = cm->flags & cm->mask;321 maskedflags = cm->flags & cm->mask;
322 deactivated = ~cm->flags & cm->mask;
223 notsupp = maskedflags & ~(priv->ctrlmode_supported | ctrlstatic);323 notsupp = maskedflags & ~(priv->ctrlmode_supported | ctrlstatic);
224 ctrlstatic_missing = (maskedflags & ctrlstatic) ^ ctrlstatic;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 return -EOPNOTSUPP;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 /* If a top dependency flag is provided, reset all its dependencies */353 /* If a top dependency flag is provided, reset all its dependencies */
245 if (cm->mask & CAN_CTRLMODE_FD)354 if (cm->mask & CAN_CTRLMODE_FD)
246 priv->ctrlmode &= ~CAN_CTRLMODE_FD_TDC_MASK;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 /* clear bits to be modified and copy the flag values */360 /* clear bits to be modified and copy the flag values */
249 priv->ctrlmode &= ~cm->mask;361 priv->ctrlmode &= ~cm->mask;
250 priv->ctrlmode |= maskedflags;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 if (!(priv->ctrlmode & CAN_CTRLMODE_FD)) {365 if (!(priv->ctrlmode & CAN_CTRLMODE_FD)) {
254 memset(&priv->fd.data_bittiming, 0,366 memset(&priv->fd.data_bittiming, 0,
255 sizeof(priv->fd.data_bittiming));367 sizeof(priv->fd.data_bittiming));
256 priv->ctrlmode &= ~CAN_CTRLMODE_FD_TDC_MASK;368 priv->ctrlmode &= ~CAN_CTRLMODE_FD_TDC_MASK;
257 memset(&priv->fd.tdc, 0, sizeof(priv->fd.tdc));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 can_set_default_mtu(dev);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 dbt_params = &priv->fd;449 dbt_params = &priv->fd;
331 tdc_mask = CAN_CTRLMODE_FD_TDC_MASK;450 tdc_mask = CAN_CTRLMODE_FD_TDC_MASK;
332 } else {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 if (!data_bittiming)458 if (!data_bittiming)
@@ -366,7 +488,8 @@ static int can_dbt_changelink(struct net_device *dev, struct nlattr *data[], · 29 unmodified lines
366 if (data[IFLA_CAN_CTRLMODE]) {488 if (data[IFLA_CAN_CTRLMODE]) {
367 struct can_ctrlmode *cm = nla_data(data[IFLA_CAN_CTRLMODE]);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 if (data_tdc) {494 if (data_tdc) {
372 /* TDC parameters are provided: use them */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 can_calc_tdco(&dbt_params->tdc, dbt_params->tdc_const, &dbt,505 can_calc_tdco(&dbt_params->tdc, dbt_params->tdc_const, &dbt,
383 tdc_mask, &priv->ctrlmode, priv->ctrlmode_supported);506 tdc_mask, &priv->ctrlmode, priv->ctrlmode_supported);
384 } /* else: both CAN_CTRLMODE_TDC_{AUTO,MANUAL} are explicitly507 } /* else: both CAN_CTRLMODE_{,XL}_TDC_{AUTO,MANUAL} are explicitly
385 * turned off. TDC is disabled: do nothing508 * 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 return 0;520 return 0;
398}521}
399 522
523static 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
400static int can_changelink(struct net_device *dev, struct nlattr *tb[],593static int can_changelink(struct net_device *dev, struct nlattr *tb[],
401 struct nlattr *data[],594 struct nlattr *data[],
402 struct netlink_ext_ack *extack)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 if (err)679 if (err)
487 return err;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 if (data[IFLA_CAN_TERMINATION]) {690 if (data[IFLA_CAN_TERMINATION]) {
490 const u16 termval = nla_get_u16(data[IFLA_CAN_TERMINATION]);691 const u16 termval = nla_get_u16(data[IFLA_CAN_TERMINATION]);
491 const unsigned int num_term = priv->termination_const_cnt;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 size_t size = 0;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 size += nla_total_size(sizeof(dbt_params->data_bittiming));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 size += nla_total_size(sizeof(*dbt_params->data_bittiming_const));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 size += nla_total_size(sizeof(*dbt_params->data_bitrate_const) *762 size += nla_total_size(sizeof(*dbt_params->data_bitrate_const) *
562 dbt_params->data_bitrate_const_cnt);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 return size;766 return size;
566}767}
@@ -571,6 +772,30 @@ static size_t can_ctrlmode_ext_get_size(void) · 4 unmodified lines
571 nla_total_size(sizeof(u32)); /* IFLA_CAN_CTRLMODE_SUPPORTED */772 nla_total_size(sizeof(u32)); /* IFLA_CAN_CTRLMODE_SUPPORTED */
572}773}
573 774
775static 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
574static size_t can_get_size(const struct net_device *dev)799static size_t can_get_size(const struct net_device *dev)
575{800{
576 struct can_priv *priv = netdev_priv(dev);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 size += can_data_bittiming_get_size(&priv->fd,825 size += can_data_bittiming_get_size(&priv->fd,
601 priv->ctrlmode & CAN_CTRLMODE_FD_TDC_MASK);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 return size;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 tdc_is_enabled = can_fd_tdc_is_enabled(priv);874 tdc_is_enabled = can_fd_tdc_is_enabled(priv);
645 tdc_manual = priv->ctrlmode & CAN_CTRLMODE_TDC_MANUAL;875 tdc_manual = priv->ctrlmode & CAN_CTRLMODE_TDC_MANUAL;
646 } else {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 tdc_const = dbt_params->tdc_const;881 tdc_const = dbt_params->tdc_const;
650 tdc = &dbt_params->tdc;882 tdc = &dbt_params->tdc;
@@ -695,6 +927,42 @@ err_cancel: · 44 unmodified lines
695 return -EMSGSIZE;927 return -EMSGSIZE;
696}928}
697 929
930static 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
961err_cancel:
962 nla_nest_cancel(skb, nest);
963 return -EMSGSIZE;
964}
965
698static int can_ctrlmode_ext_fill_info(struct sk_buff *skb,966static int can_ctrlmode_ext_fill_info(struct sk_buff *skb,
699 const struct can_priv *priv)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 can_tdc_fill_info(skb, dev, IFLA_CAN_TDC) ||1035 can_tdc_fill_info(skb, dev, IFLA_CAN_TDC) ||
768 1036
769 can_ctrlmode_ext_fill_info(skb, priv)1037 can_ctrlmode_ext_fill_info(skb, priv) ||
770 )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 return -EMSGSIZE;1053 return -EMSGSIZE;
773 1054
774 return 0;1055 return 0;
drivers/net/can/dummy_can.c+285−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
19struct dummy_can {
20 struct can_priv can;
21 struct net_device *dev;
22};
23
24static struct dummy_can *dummy_can;
25
26static 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
38static 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
50static 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
59static 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
71static 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
80static 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
89static 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
102static 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
109static 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
117static 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
139static 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
182static 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
200static 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
209static 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
222static 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
228static const struct ethtool_ops dummy_can_ethtool_ops = {
229 .get_ts_info = ethtool_op_get_ts_info,
230};
231
232static 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
271static 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
280module_init(dummy_can_init);
281module_exit(dummy_can_exit);
282
283MODULE_DESCRIPTION("A dummy CAN driver, mainly to test the netlink interface");
284MODULE_LICENSE("GPL");
285MODULE_AUTHOR("Vincent Mailhol <mailhol@kernel.org>");
drivers/net/can/rcar/rcar_canfd.c+168−78
@@ -468,6 +468,7 @@ struct rcar_canfd_global { · 467 unmodified lines
468 struct platform_device *pdev; /* Respective platform device */468 struct platform_device *pdev; /* Respective platform device */
469 struct clk *clkp; /* Peripheral clock */469 struct clk *clkp; /* Peripheral clock */
470 struct clk *can_clk; /* fCAN clock */470 struct clk *can_clk; /* fCAN clock */
471 struct clk *clk_ram; /* Clock RAM */
471 unsigned long channels_mask; /* Enabled channels mask */472 unsigned long channels_mask; /* Enabled channels mask */
472 bool extclk; /* CANFD or Ext clock */473 bool extclk; /* CANFD or Ext clock */
473 bool fdmode; /* CAN FD or Classical CAN only mode */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 netif_stop_queue(ndev);1570 netif_stop_queue(ndev);
1570 rcar_canfd_stop(ndev);1571 rcar_canfd_stop(ndev);
1571 napi_disable(&priv->napi);1572 napi_disable(&priv->napi);
1572 clk_disable_unprepare(gpriv->can_clk);
1573 close_candev(ndev);1573 close_candev(ndev);
1574 clk_disable_unprepare(gpriv->can_clk);
1574 phy_power_off(priv->transceiver);1575 phy_power_off(priv->transceiver);
1575 return 0;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
1964static 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
2034fail_mode:
2035 rcar_canfd_disable_global_interrupts(gpriv);
2036fail_ram_clk:
2037 clk_disable_unprepare(gpriv->clk_ram);
2038fail_clk:
2039 clk_disable_unprepare(gpriv->clkp);
2040fail_reset2:
2041 reset_control_assert(gpriv->rstc2);
2042fail_reset1:
2043 reset_control_assert(gpriv->rstc1);
2044 return err;
2045}
2046
2047static 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
1963static int rcar_canfd_probe(struct platform_device *pdev)2064static int rcar_canfd_probe(struct platform_device *pdev)
1964{2065{
1965 struct phy *transceivers[RCANFD_NUM_CHANNELS] = { NULL, };2066 struct phy *transceivers[RCANFD_NUM_CHANNELS] = { NULL, };
1966 const struct rcar_canfd_hw_info *info;2067 const struct rcar_canfd_hw_info *info;
1967 struct device *dev = &pdev->dev;2068 struct device *dev = &pdev->dev;
1968 void __iomem *addr;2069 void __iomem *addr;
1969 u32 sts, ch, fcan_freq;
1970 struct rcar_canfd_global *gpriv;2070 struct rcar_canfd_global *gpriv;
1971 struct device_node *of_child;2071 struct device_node *of_child;
1972 unsigned long channels_mask = 0;2072 unsigned long channels_mask = 0;
1973 int err, ch_irq, g_irq;2073 int err, ch_irq, g_irq;
1974 int g_err_irq, g_recc_irq;2074 int g_err_irq, g_recc_irq;
1975 u32 rule_entry = 0;
1976 bool fdmode = true; /* CAN FD only mode - default */2075 bool fdmode = true; /* CAN FD only mode - default */
1977 char name[9] = "channelX";2076 char name[9] = "channelX";
1978 struct clk *clk_ram;2077 u32 ch, fcan_freq;
1979 int i;2078 int i;
1980 2079
1981 info = of_device_get_match_data(dev);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 gpriv->extclk = gpriv->info->external_clk;2164 gpriv->extclk = gpriv->info->external_clk;
2066 }2165 }
2067 2166
2068 clk_ram = devm_clk_get_optional_enabled(dev, "ram_clk");2167 gpriv->clk_ram = devm_clk_get_optional(dev, "ram_clk");
2069 if (IS_ERR(clk_ram))2168 if (IS_ERR(gpriv->clk_ram))
2070 return dev_err_probe(dev, PTR_ERR(clk_ram),2169 return dev_err_probe(dev, PTR_ERR(gpriv->clk_ram),
2071 "cannot get enabled ram clock\n");2170 "cannot get ram clock\n");
2072 2171
2073 addr = devm_platform_ioremap_resource(pdev, 0);2172 addr = devm_platform_ioremap_resource(pdev, 0);
2074 if (IS_ERR(addr)) {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 if (err)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 goto fail_mode;2221 goto fail_mode;
2172 }
2173 2222
2174 for_each_set_bit(ch, &gpriv->channels_mask, info->max_channels) {2223 for_each_set_bit(ch, &gpriv->channels_mask, info->max_channels) {
2175 err = rcar_canfd_channel_probe(gpriv, ch, fcan_freq,2224 err = rcar_canfd_channel_probe(gpriv, ch, fcan_freq,
@@ -2188,12 +2237,7 @@ fail_channel: · 12 unmodified lines
2188 for_each_set_bit(ch, &gpriv->channels_mask, info->max_channels)2237 for_each_set_bit(ch, &gpriv->channels_mask, info->max_channels)
2189 rcar_canfd_channel_remove(gpriv, ch);2238 rcar_canfd_channel_remove(gpriv, ch);
2190fail_mode:2239fail_mode:
2191 rcar_canfd_disable_global_interrupts(gpriv);2240 rcar_canfd_global_deinit(gpriv, false);
2192fail_clk:
2193 clk_disable_unprepare(gpriv->clkp);
2194fail_reset:
2195 reset_control_assert(gpriv->rstc1);
2196 reset_control_assert(gpriv->rstc2);
2197fail_dev:2241fail_dev:
2198 return err;2242 return err;
2199}2243}
@@ -2203,33 +2247,79 @@ static void rcar_canfd_remove(struct platform_device *pdev) · 3 unmodified lines
2203 struct rcar_canfd_global *gpriv = platform_get_drvdata(pdev);2247 struct rcar_canfd_global *gpriv = platform_get_drvdata(pdev);
2204 u32 ch;2248 u32 ch;
2205 2249
2206 rcar_canfd_reset_controller(gpriv);
2207 rcar_canfd_disable_global_interrupts(gpriv);
2208
2209 for_each_set_bit(ch, &gpriv->channels_mask, gpriv->info->max_channels) {2250 for_each_set_bit(ch, &gpriv->channels_mask, gpriv->info->max_channels) {
2210 rcar_canfd_disable_channel_interrupts(gpriv->ch[ch]);2251 rcar_canfd_disable_channel_interrupts(gpriv->ch[ch]);
2211 rcar_canfd_channel_remove(gpriv, ch);2252 rcar_canfd_channel_remove(gpriv, ch);
2212 }2253 }
2213 2254
2214 /* Enter global sleep mode */2255 rcar_canfd_global_deinit(gpriv, true);
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);
2219}2256}
2220 2257
2221static int __maybe_unused rcar_canfd_suspend(struct device *dev)2258static 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 return 0;2286 return 0;
2224}2287}
2225 2288
2226static int __maybe_unused rcar_canfd_resume(struct device *dev)2289static 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 return 0;2318 return 0;
2229}2319}
2230 2320
2231static SIMPLE_DEV_PM_OPS(rcar_canfd_pm_ops, rcar_canfd_suspend,2321static DEFINE_SIMPLE_DEV_PM_OPS(rcar_canfd_pm_ops, rcar_canfd_suspend,
2232 rcar_canfd_resume);2322 rcar_canfd_resume);
2233 2323
2234static const __maybe_unused struct of_device_id rcar_canfd_of_table[] = {2324static const __maybe_unused struct of_device_id rcar_canfd_of_table[] = {
2235 { .compatible = "renesas,r8a779a0-canfd", .data = &rcar_gen4_hw_info },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 .driver = {2336 .driver = {
2247 .name = RCANFD_DRV_NAME,2337 .name = RCANFD_DRV_NAME,
2248 .of_match_table = of_match_ptr(rcar_canfd_of_table),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 .probe = rcar_canfd_probe,2341 .probe = rcar_canfd_probe,
2252 .remove = rcar_canfd_remove,2342 .remove = rcar_canfd_remove,
include/linux/can/bittiming.h+76−5
@@ -1,6 +1,6 @@
1/* SPDX-License-Identifier: GPL-2.0-only */1/* SPDX-License-Identifier: GPL-2.0-only */
2/* Copyright (c) 2020 Pengutronix, Marc Kleine-Budde <kernel@pengutronix.de>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#ifndef _CAN_BITTIMING_H6#ifndef _CAN_BITTIMING_H
@@ -16,10 +16,12 @@ · 9 unmodified lines
16 16
17#define CAN_CTRLMODE_FD_TDC_MASK \17#define CAN_CTRLMODE_FD_TDC_MASK \
18 (CAN_CTRLMODE_TDC_AUTO | CAN_CTRLMODE_TDC_MANUAL)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#define CAN_CTRLMODE_TDC_AUTO_MASK \21#define CAN_CTRLMODE_TDC_AUTO_MASK \
20 (CAN_CTRLMODE_TDC_AUTO)22 (CAN_CTRLMODE_TDC_AUTO | CAN_CTRLMODE_XL_TDC_AUTO)
21#define CAN_CTRLMODE_TDC_MANUAL_MASK \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 * struct can_tdc - CAN FD Transmission Delay Compensation parameters27 * struct can_tdc - CAN FD Transmission Delay Compensation parameters
@@ -85,6 +87,11 @@ struct can_tdc { · 59 unmodified lines
85 u32 tdcf;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 * struct can_tdc_const - CAN hardware-dependent constant for96 * struct can_tdc_const - CAN hardware-dependent constant for
90 * Transmission Delay Compensation97 * Transmission Delay Compensation
@@ -118,11 +125,48 @@ struct can_tdc_const { · 27 unmodified lines
118 u32 tdcf_max;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 */
135struct 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 */
152struct 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
121struct data_bittiming_params {161struct data_bittiming_params {
122 const struct can_bittiming_const *data_bittiming_const;162 const struct can_bittiming_const *data_bittiming_const;
123 struct can_bittiming data_bittiming;163 struct can_bittiming data_bittiming;
124 const struct can_tdc_const *tdc_const;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 const u32 *data_bitrate_const;170 const u32 *data_bitrate_const;
127 unsigned int data_bitrate_const_cnt;171 unsigned int data_bitrate_const_cnt;
128 int (*do_set_data_bittiming)(struct net_device *dev);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
136void can_calc_tdco(struct can_tdc *tdc, const struct can_tdc_const *tdc_const,180void can_calc_tdco(struct can_tdc *tdc, const struct can_tdc_const *tdc_const,
137 const struct can_bittiming *dbt,181 const struct can_bittiming *dbt,
138 u32 tdc_mask, u32 *ctrlmode, u32 ctrlmode_supported);182 u32 tdc_mask, u32 *ctrlmode, u32 ctrlmode_supported);
183
184int can_calc_pwm(struct net_device *dev, struct netlink_ext_ack *extack);
139#else /* !CONFIG_CAN_CALC_BITTIMING */185#else /* !CONFIG_CAN_CALC_BITTIMING */
140static inline int186static inline int
141can_calc_bittiming(const struct net_device *dev, struct can_bittiming *bt,187can_calc_bittiming(const struct net_device *dev, struct can_bittiming *bt,
142 const struct can_bittiming_const *btc, struct netlink_ext_ack *extack)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 return -EINVAL;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 u32 tdc_mask, u32 *ctrlmode, u32 ctrlmode_supported)197 u32 tdc_mask, u32 *ctrlmode, u32 ctrlmode_supported)
152{198{
153}199}
200
201static inline int
202can_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#endif /* CONFIG_CAN_CALC_BITTIMING */208#endif /* CONFIG_CAN_CALC_BITTIMING */
155 209
156void can_sjw_set_default(struct can_bittiming *bt);210void 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 const unsigned int bitrate_const_cnt,218 const unsigned int bitrate_const_cnt,
165 struct netlink_ext_ack *extack);219 struct netlink_ext_ack *extack);
166 220
221int 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 * can_get_relative_tdco() - TDCO relative to the sample point226 * 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 return CAN_SYNC_SEG + bt->prop_seg + bt->phase_seg1 + bt->phase_seg2;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 */
268static 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 */
274static 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#endif /* !_CAN_BITTIMING_H */280#endif /* !_CAN_BITTIMING_H */
include/linux/can/dev.h+51−17
@@ -47,7 +47,7 @@ struct can_priv { · 46 unmodified lines
47 47
48 const struct can_bittiming_const *bittiming_const;48 const struct can_bittiming_const *bittiming_const;
49 struct can_bittiming bittiming;49 struct can_bittiming bittiming;
50 struct data_bittiming_params fd;50 struct data_bittiming_params fd, xl;
51 unsigned int bitrate_const_cnt;51 unsigned int bitrate_const_cnt;
52 const u32 *bitrate_const;52 const u32 *bitrate_const;
53 u32 bitrate_max;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 return !!(priv->ctrlmode & CAN_CTRLMODE_FD_TDC_MASK);85 return !!(priv->ctrlmode & CAN_CTRLMODE_FD_TDC_MASK);
86}86}
87 87
88static inline bool can_xl_tdc_is_enabled(const struct can_priv *priv)
89{
90 return !!(priv->ctrlmode & CAN_CTRLMODE_XL_TDC_MASK);
91}
92
88static inline u32 can_get_static_ctrlmode(struct can_priv *priv)93static inline u32 can_get_static_ctrlmode(struct can_priv *priv)
89{94{
90 return priv->ctrlmode & ~priv->ctrlmode_supported;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 return (mtu >= CANXL_MIN_MTU && mtu <= CANXL_MAX_MTU);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 */
99static 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
114void can_setup(struct net_device *dev);103void can_setup(struct net_device *dev);
115 104
116struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max,105struct 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
146const char *can_get_state_str(const enum can_state state);135const char *can_get_state_str(const enum can_state state);
147const char *can_get_ctrlmode_str(u32 ctrlmode);136const char *can_get_ctrlmode_str(u32 ctrlmode);
148 137
138static 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 */
152static 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
177invalid_skb:
178 kfree_skb(skb);
179 dev->stats.tx_dropped++;
180 return true;
181}
182
149void can_state_get_by_berr_counter(const struct net_device *dev,183void can_state_get_by_berr_counter(const struct net_device *dev,
150 const struct can_berr_counter *bec,184 const struct can_berr_counter *bec,
151 enum can_state *tx_state,185 enum can_state *tx_state,
include/uapi/linux/can/netlink.h+34−0
@@ -5,6 +5,7 @@ · 4 unmodified lines
5 * Definitions for the CAN netlink interface5 * Definitions for the CAN netlink interface
6 *6 *
7 * Copyright (c) 2009 Wolfgang Grandegger <wg@grandegger.com>7 * Copyright (c) 2009 Wolfgang Grandegger <wg@grandegger.com>
8 * Copyright (c) 2021-2025 Vincent Mailhol <mailhol@kernel.org>
8 *9 *
9 * This program is free software; you can redistribute it and/or modify10 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the version 2 of the GNU General Public License11 * 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#define CAN_CTRLMODE_CC_LEN8_DLC 0x100 /* Classic CAN DLC option */104#define CAN_CTRLMODE_CC_LEN8_DLC 0x100 /* Classic CAN DLC option */
104#define CAN_CTRLMODE_TDC_AUTO 0x200 /* FD transceiver automatically calculates TDCV */105#define CAN_CTRLMODE_TDC_AUTO 0x200 /* FD transceiver automatically calculates TDCV */
105#define CAN_CTRLMODE_TDC_MANUAL 0x400 /* FD TDCV is manually set up by user */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 * CAN device statistics114 * CAN device statistics
@@ -138,6 +144,11 @@ enum { · 29 unmodified lines
138 IFLA_CAN_BITRATE_MAX,144 IFLA_CAN_BITRATE_MAX,
139 IFLA_CAN_TDC, /* FD */145 IFLA_CAN_TDC, /* FD */
140 IFLA_CAN_CTRLMODE_EXT,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 /* add new constants above here */153 /* add new constants above here */
143 __IFLA_CAN_MAX,154 __IFLA_CAN_MAX,
@@ -179,6 +190,29 @@ enum { · 35 unmodified lines
179 IFLA_CAN_CTRLMODE_MAX = __IFLA_CAN_CTRLMODE - 1190 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 */
199enum {
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/* u16 termination range: 1..65535 Ohms */216/* u16 termination range: 1..65535 Ohms */
183#define CAN_TERMINATION_DISABLED 0217#define CAN_TERMINATION_DISABLED 0
184 218
net/can/raw.c+46−8
@@ -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
895static unsigned int raw_check_txframe(struct raw_sock *ro, struct sk_buff *skb, int mtu)895static 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 /* The CANXL-only mode disables error-signalling on the CAN bus
898 if (can_is_can_skb(skb))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
908static 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
919static 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
930static 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 return CAN_MTU;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 if (ro->fd_frames && can_is_canfd_skb(skb) &&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 return CANFD_MTU;942 return CANFD_MTU;
905 943
906 /* CAN XL -> needs to be enabled and a CAN XL device */944 /* CAN XL */
907 if (ro->xl_frames && can_is_canxl_skb(skb) &&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 return CANXL_MTU;947 return CANXL_MTU;
910 948
911 return 0;949 return 0;
@@ -961,7 +999,7 @@ static int raw_sendmsg(struct socket *sock, struct msghdr *msg, size_t size) · 49 unmodified lines
961 err = -EINVAL;999 err = -EINVAL;
962 1000
963 /* check for valid CAN (CC/FD/XL) frame content */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 if (!txmtu)1003 if (!txmtu)
966 goto free_skb;1004 goto free_skb;
967 1005

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t