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
3232 - description: AHB peripheral clock
3333 - description: CAN bus clock
3434
35+ resets:
36+ maxItems: 1
37+
3538 required:
3639 - compatible
3740 - reg
3841 - interrupts
3942 - clocks
43+ - resets
4044
4145 additionalProperties: false
4246
@@ -46,6 +50,7 @@ examples: · 3 unmodified lines
4650 compatible = "microchip,mpfs-can";
4751 reg = <0x2010c000 0x1000>;
4852 clocks = <&clkcfg 17>, <&clkcfg 37>;
53+ resets = <&clkcfg 17>;
4954 interrupt-parent = <&plic>;
5055 interrupts = <56>;
5156 };
MAINTAINERS+3−5
@@ -15412,14 +15412,12 @@ S: Supported · 15411 unmodified lines
1541215412 F: drivers/net/phy/mxl-86110.c
1541315413 F: drivers/net/phy/mxl-gpy.c
1541415414
15415−MCAN MMIO DEVICE DRIVER
15416−M: Chandrasekar Ramakrishnan <rcsekar@samsung.com>
15415+MCAN DEVICE DRIVER
15416+M: Markus Schneider-Pargmann <msp@baylibre.com>
1541715417 L: linux-can@vger.kernel.org
1541815418 S: Maintained
1541915419 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/
1542315421
1542415422 MCBA MICROCHIP CAN BUS ANALYZER TOOL DRIVER
1542515423 R: Yasushi SHOJI <yashi@spacecubics.com>
drivers/net/can/Kconfig+17−0
@@ -124,6 +124,23 @@ config CAN_CAN327 · 123 unmodified lines
124124
125125 If this driver is built as a module, it will be called can327.
126126
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+
127144 config CAN_FLEXCAN
128145 tristate "Support for Freescale FLEXCAN based chips"
129146 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
2121 obj-$(CONFIG_CAN_CC770) += cc770/
2222 obj-$(CONFIG_CAN_C_CAN) += c_can/
2323 obj-$(CONFIG_CAN_CTUCANFD) += ctucanfd/
24+obj-$(CONFIG_CAN_DUMMY) += dummy_can.o
2425 obj-$(CONFIG_CAN_FLEXCAN) += flexcan/
2526 obj-$(CONFIG_CAN_GRCAN) += grcan.o
2627 obj-$(CONFIG_CAN_IFI_CANFD) += ifi_canfd/
drivers/net/can/dev/bittiming.c+63−0
@@ -2,6 +2,7 @@ · 1 unmodified line
22 /* Copyright (C) 2005 Marc Kleine-Budde, Pengutronix
33 * Copyright (C) 2006 Andrey Volkov, Varma Electronics
44 * Copyright (C) 2008-2009 Wolfgang Grandegger <wg@grandegger.com>
5+ * Copyright (c) 2025 Vincent Mailhol <mailhol@kernel.org>
56 */
67
78 #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
151152
152153 return -EINVAL;
153154 }
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+}
drivers/net/can/dev/calc_bittiming.c+88−26
@@ -2,6 +2,7 @@ · 1 unmodified line
22 /* Copyright (C) 2005 Marc Kleine-Budde, Pengutronix
33 * Copyright (C) 2006 Andrey Volkov, Varma Electronics
44 * Copyright (C) 2008-2009 Wolfgang Grandegger <wg@grandegger.com>
5+ * Copyright (C) 2021-2025 Vincent Mailhol <mailhol@kernel.org>
56 */
67
78 #include <linux/units.h>
@@ -9,6 +10,33 @@ · 1 unmodified line
910
1011 #define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */
1112
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+
1240 /* Bit-timing calculation derived from:
1341 *
1442 * Code based on LinCAN sources and H8S2638 project
@@ -23,7 +51,7 @@ · 8 unmodified lines
2351 */
2452 static int
2553 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,
2755 unsigned int *tseg1_ptr, unsigned int *tseg2_ptr,
2856 unsigned int *sample_point_error_ptr)
2957 {
@@ -34,7 +62,7 @@ can_update_sample_point(const struct can_bittiming_const *btc, · 4 unmodified lines
3462
3563 for (i = 0; i <= 1; i++) {
3664 tseg2 = tseg + CAN_SYNC_SEG -
37− (sample_point_nominal * (tseg + CAN_SYNC_SEG)) /
65+ (sample_point_reference * (tseg + CAN_SYNC_SEG)) /
3866 1000 - i;
3967 tseg2 = clamp(tseg2, btc->tseg2_min, btc->tseg2_max);
4068 tseg1 = tseg - tseg2;
@@ -45,9 +73,9 @@ can_update_sample_point(const struct can_bittiming_const *btc, · 4 unmodified lines
4573
4674 sample_point = 1000 * (tseg + CAN_SYNC_SEG - tseg2) /
4775 (tseg + CAN_SYNC_SEG);
48− sample_point_error = abs(sample_point_nominal - sample_point);
76+ sample_point_error = abs(sample_point_reference - sample_point);
4977
50− if (sample_point <= sample_point_nominal &&
78+ if (sample_point <= sample_point_reference &&
5179 sample_point_error < best_sample_point_error) {
5280 best_sample_point = sample_point;
5381 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
6795 {
6896 struct can_priv *priv = netdev_priv(dev);
6997 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 */
7199 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 */
73101 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 */
75103 unsigned int best_tseg = 0; /* current best value for tseg */
76104 unsigned int best_brp = 0; /* current best value for brp */
77105 unsigned int brp, tsegall, tseg, tseg1 = 0, tseg2 = 0;
78106 u64 v64;
79107 int err;
80108
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);
92116
93117 /* tseg even = round down, odd = round up */
94118 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
114138 if (bitrate_error < best_bitrate_error)
115139 best_sample_point_error = UINT_MAX;
116140
117− can_update_sample_point(btc, sample_point_nominal, tseg / 2,
141+ can_update_sample_point(btc, sample_point_reference, tseg / 2,
118142 &tseg1, &tseg2, &sample_point_error);
119143 if (sample_point_error >= best_sample_point_error)
120144 continue;
@@ -129,23 +153,26 @@ int can_calc_bittiming(const struct net_device *dev, struct can_bittiming *bt, · 8 unmodified lines
129153 }
130154
131155 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;
134158 do_div(v64, bt->bitrate);
135159 bitrate_error = (u32)v64;
160+ /* print at least 0.01% if the error is smaller */
161+ bitrate_error = max(bitrate_error, 1U);
136162 if (bitrate_error > CAN_CALC_MAX_ERROR) {
137163 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);
140167 return -EINVAL;
141168 }
142169 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);
145172 }
146173
147174 /* 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,
149176 best_tseg, &tseg1, &tseg2,
150177 NULL);
151178
@@ -198,3 +225,38 @@ void can_calc_tdco(struct can_tdc *tdc, const struct can_tdc_const *tdc_const, · 46 unmodified lines
198225 *ctrlmode |= tdc_auto;
199226 }
200227 }
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+}
drivers/net/can/dev/dev.c+29−13
@@ -92,29 +92,39 @@ const char *can_get_ctrlmode_str(u32 ctrlmode) · 91 unmodified lines
9292 {
9393 switch (ctrlmode & ~(ctrlmode - 1)) {
9494 case 0:
95− return "none";
95+ return "(none)";
9696 case CAN_CTRLMODE_LOOPBACK:
97− return "loopback";
97+ return "LOOPBACK";
9898 case CAN_CTRLMODE_LISTENONLY:
99− return "listen-only";
99+ return "LISTEN-ONLY";
100100 case CAN_CTRLMODE_3_SAMPLES:
101− return "triple-sampling";
101+ return "TRIPLE-SAMPLING";
102102 case CAN_CTRLMODE_ONE_SHOT:
103− return "one-shot";
103+ return "ONE-SHOT";
104104 case CAN_CTRLMODE_BERR_REPORTING:
105− return "berr-reporting";
105+ return "BERR-REPORTING";
106106 case CAN_CTRLMODE_FD:
107− return "fd";
107+ return "FD";
108108 case CAN_CTRLMODE_PRESUME_ACK:
109− return "presume-ack";
109+ return "PRESUME-ACK";
110110 case CAN_CTRLMODE_FD_NON_ISO:
111− return "fd-non-iso";
111+ return "FD-NON-ISO";
112112 case CAN_CTRLMODE_CC_LEN8_DLC:
113− return "cc-len8-dlc";
113+ return "CC-LEN8-DLC";
114114 case CAN_CTRLMODE_TDC_AUTO:
115− return "fd-tdc-auto";
115+ return "TDC-AUTO";
116116 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";
118128 default:
119129 return "<unknown>";
120130 }
@@ -348,7 +358,13 @@ void can_set_default_mtu(struct net_device *dev) · 227 unmodified lines
348358 {
349359 struct can_priv *priv = netdev_priv(dev);
350360
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) {
352368 dev->mtu = CANFD_MTU;
353369 dev->min_mtu = CANFD_MTU;
354370 dev->max_mtu = CANFD_MTU;
drivers/net/can/dev/netlink.c+300−19
@@ -2,7 +2,7 @@ · 1 unmodified line
22 /* Copyright (C) 2005 Marc Kleine-Budde, Pengutronix
33 * Copyright (C) 2006 Andrey Volkov, Varma Electronics
44 * 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>
66 */
77
88 #include <linux/can/dev.h>
@@ -22,6 +22,10 @@ static const struct nla_policy can_policy[IFLA_CAN_MAX + 1] = { · 13 unmodified lines
2222 [IFLA_CAN_TERMINATION] = { .type = NLA_U16 },
2323 [IFLA_CAN_TDC] = { .type = NLA_NESTED },
2424 [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 },
2529 };
2630
2731 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
3640 [IFLA_CAN_TDC_TDCF] = { .type = NLA_U32 },
3741 };
3842
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+
3955 static int can_validate_bittiming(struct nlattr *data[],
4056 struct netlink_ext_ack *extack,
4157 int ifla_can_bittiming)
@@ -70,7 +86,7 @@ static int can_validate_tdc(struct nlattr *data_tdc, · 28 unmodified lines
7086 return -EOPNOTSUPP;
7187 }
7288
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
7490 * must be set and vice-versa
7591 */
7692 if ((tdc_auto || tdc_manual) && !data_tdc) {
@@ -82,8 +98,8 @@ static int can_validate_tdc(struct nlattr *data_tdc, · 5 unmodified lines
8298 return -EOPNOTSUPP;
8399 }
84100
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
87103 */
88104 if (data_tdc) {
89105 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
116132 return 0;
117133 }
118134
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+
119169 static int can_validate_databittiming(struct nlattr *data[],
120170 struct netlink_ext_ack *extack,
121171 int ifla_can_data_bittiming, u32 flags)
@@ -126,10 +176,10 @@ static int can_validate_databittiming(struct nlattr *data[], · 4 unmodified lines
126176 bool is_on;
127177 int err;
128178
129− /* Make sure that valid CAN FD configurations always consist of
179+ /* Make sure that valid CAN FD/XL configurations always consist of
130180 * - nominal/arbitration bittiming
131181 * - data bittiming
132− * - control mode with CAN_CTRLMODE_FD set
182+ * - control mode with CAN_CTRLMODE_{FD,XL} set
133183 * - TDC parameters are coherent (details in can_validate_tdc())
134184 */
135185
@@ -139,7 +189,10 @@ static int can_validate_databittiming(struct nlattr *data[], · 3 unmodified lines
139189 is_on = flags & CAN_CTRLMODE_FD;
140190 type = "FD";
141191 } 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";
143196 }
144197
145198 if (is_on) {
@@ -175,6 +228,32 @@ static int can_validate_databittiming(struct nlattr *data[], · 29 unmodified lines
175228 return 0;
176229 }
177230
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+
178257 static int can_validate(struct nlattr *tb[], struct nlattr *data[],
179258 struct netlink_ext_ack *extack)
180259 {
@@ -188,6 +267,17 @@ static int can_validate(struct nlattr *tb[], struct nlattr *data[], · 7 unmodified lines
188267 struct can_ctrlmode *cm = nla_data(data[IFLA_CAN_CTRLMODE]);
189268
190269 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;
191281 }
192282
193283 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
199289 if (err)
200290 return err;
201291
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+
202301 return 0;
203302 }
204303
@@ -208,7 +307,7 @@ static int can_ctrlmode_changelink(struct net_device *dev, · 3 unmodified lines
208307 {
209308 struct can_priv *priv = netdev_priv(dev);
210309 struct can_ctrlmode *cm;
211− u32 ctrlstatic, maskedflags, notsupp, ctrlstatic_missing;
310+ u32 ctrlstatic, maskedflags, deactivated, notsupp, ctrlstatic_missing;
212311
213312 if (!data[IFLA_CAN_CTRLMODE])
214313 return 0;
@@ -220,6 +319,7 @@ static int can_ctrlmode_changelink(struct net_device *dev, · 5 unmodified lines
220319 cm = nla_data(data[IFLA_CAN_CTRLMODE]);
221320 ctrlstatic = can_get_static_ctrlmode(priv);
222321 maskedflags = cm->flags & cm->mask;
322+ deactivated = ~cm->flags & cm->mask;
223323 notsupp = maskedflags & ~(priv->ctrlmode_supported | ctrlstatic);
224324 ctrlstatic_missing = (maskedflags & ctrlstatic) ^ ctrlstatic;
225325
@@ -241,21 +341,40 @@ static int can_ctrlmode_changelink(struct net_device *dev, · 15 unmodified lines
241341 return -EOPNOTSUPP;
242342 }
243343
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+
244353 /* If a top dependency flag is provided, reset all its dependencies */
245354 if (cm->mask & CAN_CTRLMODE_FD)
246355 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);
247359
248360 /* clear bits to be modified and copy the flag values */
249361 priv->ctrlmode &= ~cm->mask;
250362 priv->ctrlmode |= maskedflags;
251363
252− /* Wipe potential leftovers from previous CAN FD config */
364+ /* Wipe potential leftovers from previous CAN FD/XL config */
253365 if (!(priv->ctrlmode & CAN_CTRLMODE_FD)) {
254366 memset(&priv->fd.data_bittiming, 0,
255367 sizeof(priv->fd.data_bittiming));
256368 priv->ctrlmode &= ~CAN_CTRLMODE_FD_TDC_MASK;
257369 memset(&priv->fd.tdc, 0, sizeof(priv->fd.tdc));
258370 }
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+ }
259378
260379 can_set_default_mtu(dev);
261380
@@ -330,7 +449,10 @@ static int can_dbt_changelink(struct net_device *dev, struct nlattr *data[], · 68 unmodified lines
330449 dbt_params = &priv->fd;
331450 tdc_mask = CAN_CTRLMODE_FD_TDC_MASK;
332451 } 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;
334456 }
335457
336458 if (!data_bittiming)
@@ -366,7 +488,8 @@ static int can_dbt_changelink(struct net_device *dev, struct nlattr *data[], · 29 unmodified lines
366488 if (data[IFLA_CAN_CTRLMODE]) {
367489 struct can_ctrlmode *cm = nla_data(data[IFLA_CAN_CTRLMODE]);
368490
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);
370493 }
371494 if (data_tdc) {
372495 /* 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
381504 */
382505 can_calc_tdco(&dbt_params->tdc, dbt_params->tdc_const, &dbt,
383506 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
385508 * turned off. TDC is disabled: do nothing
386509 */
387510
@@ -397,6 +520,76 @@ static int can_dbt_changelink(struct net_device *dev, struct nlattr *data[], · 9 unmodified lines
397520 return 0;
398521 }
399522
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+
400593 static int can_changelink(struct net_device *dev, struct nlattr *tb[],
401594 struct nlattr *data[],
402595 struct netlink_ext_ack *extack)
@@ -486,6 +679,14 @@ static int can_changelink(struct net_device *dev, struct nlattr *tb[], · 83 unmodified lines
486679 if (err)
487680 return err;
488681
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+
489690 if (data[IFLA_CAN_TERMINATION]) {
490691 const u16 termval = nla_get_u16(data[IFLA_CAN_TERMINATION]);
491692 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
553754 {
554755 size_t size = 0;
555756
556− if (dbt_params->data_bittiming.bitrate) /* IFLA_CAN_DATA_BITTIMING */
757+ if (dbt_params->data_bittiming.bitrate) /* IFLA_CAN_{,XL}_DATA_BITTIMING */
557758 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 */
559760 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 */
561762 size += nla_total_size(sizeof(*dbt_params->data_bitrate_const) *
562763 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 */
564765
565766 return size;
566767 }
@@ -571,6 +772,30 @@ static size_t can_ctrlmode_ext_get_size(void) · 4 unmodified lines
571772 nla_total_size(sizeof(u32)); /* IFLA_CAN_CTRLMODE_SUPPORTED */
572773 }
573774
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+
574799 static size_t can_get_size(const struct net_device *dev)
575800 {
576801 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
600825 size += can_data_bittiming_get_size(&priv->fd,
601826 priv->ctrlmode & CAN_CTRLMODE_FD_TDC_MASK);
602827
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+
603833 return size;
604834 }
605835
@@ -644,7 +874,9 @@ static int can_tdc_fill_info(struct sk_buff *skb, const struct net_device *dev, · 38 unmodified lines
644874 tdc_is_enabled = can_fd_tdc_is_enabled(priv);
645875 tdc_manual = priv->ctrlmode & CAN_CTRLMODE_TDC_MANUAL;
646876 } 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;
648880 }
649881 tdc_const = dbt_params->tdc_const;
650882 tdc = &dbt_params->tdc;
@@ -695,6 +927,42 @@ err_cancel: · 44 unmodified lines
695927 return -EMSGSIZE;
696928 }
697929
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+
698966 static int can_ctrlmode_ext_fill_info(struct sk_buff *skb,
699967 const struct can_priv *priv)
700968 {
@@ -766,9 +1034,22 @@ static int can_fill_info(struct sk_buff *skb, const struct net_device *dev) · 65 unmodified lines
7661034
7671035 can_tdc_fill_info(skb, dev, IFLA_CAN_TDC) ||
7681036
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) ||
7711041
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+ )
7721053 return -EMSGSIZE;
7731054
7741055 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+
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>");
drivers/net/can/rcar/rcar_canfd.c+168−78
@@ -468,6 +468,7 @@ struct rcar_canfd_global { · 467 unmodified lines
468468 struct platform_device *pdev; /* Respective platform device */
469469 struct clk *clkp; /* Peripheral clock */
470470 struct clk *can_clk; /* fCAN clock */
471+ struct clk *clk_ram; /* Clock RAM */
471472 unsigned long channels_mask; /* Enabled channels mask */
472473 bool extclk; /* CANFD or Ext clock */
473474 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
15691570 netif_stop_queue(ndev);
15701571 rcar_canfd_stop(ndev);
15711572 napi_disable(&priv->napi);
1572− clk_disable_unprepare(gpriv->can_clk);
15731573 close_candev(ndev);
1574+ clk_disable_unprepare(gpriv->can_clk);
15741575 phy_power_off(priv->transceiver);
15751576 return 0;
15761577 }
@@ -1960,22 +1961,120 @@ static void rcar_canfd_channel_remove(struct rcar_canfd_global *gpriv, u32 ch) · 383 unmodified lines
19601961 }
19611962 }
19621963
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+
19632064 static int rcar_canfd_probe(struct platform_device *pdev)
19642065 {
19652066 struct phy *transceivers[RCANFD_NUM_CHANNELS] = { NULL, };
19662067 const struct rcar_canfd_hw_info *info;
19672068 struct device *dev = &pdev->dev;
19682069 void __iomem *addr;
1969− u32 sts, ch, fcan_freq;
19702070 struct rcar_canfd_global *gpriv;
19712071 struct device_node *of_child;
19722072 unsigned long channels_mask = 0;
19732073 int err, ch_irq, g_irq;
19742074 int g_err_irq, g_recc_irq;
1975− u32 rule_entry = 0;
19762075 bool fdmode = true; /* CAN FD only mode - default */
19772076 char name[9] = "channelX";
1978− struct clk *clk_ram;
2077+ u32 ch, fcan_freq;
19792078 int i;
19802079
19812080 info = of_device_get_match_data(dev);
@@ -2065,10 +2164,10 @@ static int rcar_canfd_probe(struct platform_device *pdev) · 83 unmodified lines
20652164 gpriv->extclk = gpriv->info->external_clk;
20662165 }
20672166
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");
20722171
20732172 addr = devm_platform_ioremap_resource(pdev, 0);
20742173 if (IS_ERR(addr)) {
@@ -2117,59 +2216,9 @@ static int rcar_canfd_probe(struct platform_device *pdev) · 42 unmodified lines
21172216 }
21182217 }
21192218
2120− err = reset_control_reset(gpriv->rstc1);
2219+ err = rcar_canfd_global_init(gpriv);
21212220 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");
21712221 goto fail_mode;
2172− }
21732222
21742223 for_each_set_bit(ch, &gpriv->channels_mask, info->max_channels) {
21752224 err = rcar_canfd_channel_probe(gpriv, ch, fcan_freq,
@@ -2188,12 +2237,7 @@ fail_channel: · 12 unmodified lines
21882237 for_each_set_bit(ch, &gpriv->channels_mask, info->max_channels)
21892238 rcar_canfd_channel_remove(gpriv, ch);
21902239 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);
21972241 fail_dev:
21982242 return err;
21992243 }
@@ -2203,33 +2247,79 @@ static void rcar_canfd_remove(struct platform_device *pdev) · 3 unmodified lines
22032247 struct rcar_canfd_global *gpriv = platform_get_drvdata(pdev);
22042248 u32 ch;
22052249
2206− rcar_canfd_reset_controller(gpriv);
2207− rcar_canfd_disable_global_interrupts(gpriv);
2208−
22092250 for_each_set_bit(ch, &gpriv->channels_mask, gpriv->info->max_channels) {
22102251 rcar_canfd_disable_channel_interrupts(gpriv->ch[ch]);
22112252 rcar_canfd_channel_remove(gpriv, ch);
22122253 }
22132254
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);
22192256 }
22202257
2221−static int __maybe_unused rcar_canfd_suspend(struct device *dev)
2258+static int rcar_canfd_suspend(struct device *dev)
22222259 {
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+
22232286 return 0;
22242287 }
22252288
2226−static int __maybe_unused rcar_canfd_resume(struct device *dev)
2289+static int rcar_canfd_resume(struct device *dev)
22272290 {
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+
22282318 return 0;
22292319 }
22302320
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);
22332323
22342324 static const __maybe_unused struct of_device_id rcar_canfd_of_table[] = {
22352325 { .compatible = "renesas,r8a779a0-canfd", .data = &rcar_gen4_hw_info },
@@ -2246,7 +2336,7 @@ static struct platform_driver rcar_canfd_driver = { · 10 unmodified lines
22462336 .driver = {
22472337 .name = RCANFD_DRV_NAME,
22482338 .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),
22502340 },
22512341 .probe = rcar_canfd_probe,
22522342 .remove = rcar_canfd_remove,
include/linux/can/bittiming.h+76−5
@@ -1,6 +1,6 @@
11 /* SPDX-License-Identifier: GPL-2.0-only */
22 /* 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>
44 */
55
66 #ifndef _CAN_BITTIMING_H
@@ -16,10 +16,12 @@ · 9 unmodified lines
1616
1717 #define CAN_CTRLMODE_FD_TDC_MASK \
1818 (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)
1921 #define CAN_CTRLMODE_TDC_AUTO_MASK \
20− (CAN_CTRLMODE_TDC_AUTO)
22+ (CAN_CTRLMODE_TDC_AUTO | CAN_CTRLMODE_XL_TDC_AUTO)
2123 #define CAN_CTRLMODE_TDC_MANUAL_MASK \
22− (CAN_CTRLMODE_TDC_MANUAL)
24+ (CAN_CTRLMODE_TDC_MANUAL | CAN_CTRLMODE_XL_TDC_MANUAL)
2325
2426 /*
2527 * struct can_tdc - CAN FD Transmission Delay Compensation parameters
@@ -85,6 +87,11 @@ struct can_tdc { · 59 unmodified lines
8587 u32 tdcf;
8688 };
8789
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+
8895 /*
8996 * struct can_tdc_const - CAN hardware-dependent constant for
9097 * Transmission Delay Compensation
@@ -118,11 +125,48 @@ struct can_tdc_const { · 27 unmodified lines
118125 u32 tdcf_max;
119126 };
120127
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+
121161 struct data_bittiming_params {
122162 const struct can_bittiming_const *data_bittiming_const;
123163 struct can_bittiming data_bittiming;
124164 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+ };
126170 const u32 *data_bitrate_const;
127171 unsigned int data_bitrate_const_cnt;
128172 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
136180 void can_calc_tdco(struct can_tdc *tdc, const struct can_tdc_const *tdc_const,
137181 const struct can_bittiming *dbt,
138182 u32 tdc_mask, u32 *ctrlmode, u32 ctrlmode_supported);
183+
184+int can_calc_pwm(struct net_device *dev, struct netlink_ext_ack *extack);
139185 #else /* !CONFIG_CAN_CALC_BITTIMING */
140186 static inline int
141187 can_calc_bittiming(const struct net_device *dev, struct can_bittiming *bt,
142188 const struct can_bittiming_const *btc, struct netlink_ext_ack *extack)
143189 {
144− netdev_err(dev, "bit-timing calculation not available\n");
190+ NL_SET_ERR_MSG(extack, "bit-timing calculation not available\n");
145191 return -EINVAL;
146192 }
147193
@@ -151,6 +197,14 @@ can_calc_tdco(struct can_tdc *tdc, const struct can_tdc_const *tdc_const, · 3 unmodified lines
151197 u32 tdc_mask, u32 *ctrlmode, u32 ctrlmode_supported)
152198 {
153199 }
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+}
154208 #endif /* CONFIG_CAN_CALC_BITTIMING */
155209
156210 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
164218 const unsigned int bitrate_const_cnt,
165219 struct netlink_ext_ack *extack);
166220
221+int can_validate_pwm_bittiming(const struct net_device *dev,
222+ const struct can_pwm *pwm,
223+ struct netlink_ext_ack *extack);
224+
167225 /*
168226 * can_get_relative_tdco() - TDCO relative to the sample point
169227 *
@@ -206,4 +264,17 @@ static inline unsigned int can_bit_time(const struct can_bittiming *bt) · 36 unmodified lines
206264 return CAN_SYNC_SEG + bt->prop_seg + bt->phase_seg1 + bt->phase_seg2;
207265 }
208266
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+
209280 #endif /* !_CAN_BITTIMING_H */
include/linux/can/dev.h+51−17
@@ -47,7 +47,7 @@ struct can_priv { · 46 unmodified lines
4747
4848 const struct can_bittiming_const *bittiming_const;
4949 struct can_bittiming bittiming;
50− struct data_bittiming_params fd;
50+ struct data_bittiming_params fd, xl;
5151 unsigned int bitrate_const_cnt;
5252 const u32 *bitrate_const;
5353 u32 bitrate_max;
@@ -85,6 +85,11 @@ static inline bool can_fd_tdc_is_enabled(const struct can_priv *priv) · 31 unmodified lines
8585 return !!(priv->ctrlmode & CAN_CTRLMODE_FD_TDC_MASK);
8686 }
8787
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+
8893 static inline u32 can_get_static_ctrlmode(struct can_priv *priv)
8994 {
9095 return priv->ctrlmode & ~priv->ctrlmode_supported;
@@ -95,22 +100,6 @@ static inline bool can_is_canxl_dev_mtu(unsigned int mtu) · 4 unmodified lines
95100 return (mtu >= CANXL_MIN_MTU && mtu <= CANXL_MAX_MTU);
96101 }
97102
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−
114103 void can_setup(struct net_device *dev);
115104
116105 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
146135 const char *can_get_state_str(const enum can_state state);
147136 const char *can_get_ctrlmode_str(u32 ctrlmode);
148137
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+
149183 void can_state_get_by_berr_counter(const struct net_device *dev,
150184 const struct can_berr_counter *bec,
151185 enum can_state *tx_state,
include/uapi/linux/can/netlink.h+34−0
@@ -5,6 +5,7 @@ · 4 unmodified lines
55 * Definitions for the CAN netlink interface
66 *
77 * Copyright (c) 2009 Wolfgang Grandegger <wg@grandegger.com>
8+ * Copyright (c) 2021-2025 Vincent Mailhol <mailhol@kernel.org>
89 *
910 * This program is free software; you can redistribute it and/or modify
1011 * it under the terms of the version 2 of the GNU General Public License
@@ -103,6 +104,11 @@ struct can_ctrlmode { · 92 unmodified lines
103104 #define CAN_CTRLMODE_CC_LEN8_DLC 0x100 /* Classic CAN DLC option */
104105 #define CAN_CTRLMODE_TDC_AUTO 0x200 /* FD transceiver automatically calculates TDCV */
105106 #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 */
106112
107113 /*
108114 * CAN device statistics
@@ -138,6 +144,11 @@ enum { · 29 unmodified lines
138144 IFLA_CAN_BITRATE_MAX,
139145 IFLA_CAN_TDC, /* FD */
140146 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,
141152
142153 /* add new constants above here */
143154 __IFLA_CAN_MAX,
@@ -179,6 +190,29 @@ enum { · 35 unmodified lines
179190 IFLA_CAN_CTRLMODE_MAX = __IFLA_CAN_CTRLMODE - 1
180191 };
181192
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+
182216 /* u16 termination range: 1..65535 Ohms */
183217 #define CAN_TERMINATION_DISABLED 0
184218
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
892892 }
893893 }
894894
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)
896897 {
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))
899937 return CAN_MTU;
900938
901− /* CAN FD -> needs to be enabled and a CAN FD or CAN XL device */
939+ /* CAN FD */
902940 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))
904942 return CANFD_MTU;
905943
906− /* CAN XL -> needs to be enabled and a CAN XL device */
944+ /* CAN XL */
907945 if (ro->xl_frames && can_is_canxl_skb(skb) &&
908− can_is_canxl_dev_mtu(mtu))
946+ raw_dev_xl_enabled(dev, priv))
909947 return CANXL_MTU;
910948
911949 return 0;
@@ -961,7 +999,7 @@ static int raw_sendmsg(struct socket *sock, struct msghdr *msg, size_t size) · 49 unmodified lines
961999 err = -EINVAL;
9621000
9631001 /* 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);
9651003 if (!txmtu)
9661004 goto free_skb;
9671005

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