Skip to contentSkip to conversation

add the netlink interface for CAN-FD Transmitter Delay Compensation (TDC)

PATCH In review Vincent Mailhol 2 participants 11 messages Sep 18, 2021 – Jun 2, 2025

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v6 cover add the netlink interface for CAN-FD Transmitter Delay Compensation (TDC)

The main goal of this series is to add a netlink interface for the TDC parameters using netlink nested attributes. The series also contains a few fix on the current TDC implementation.

This series completes the implementation of the Transmitter Delay Compensation (TDC) in the kernel which started in March with those two patches:
- commit 289ea9e4ae59 ("can: add new CAN FD bittiming parameters:
Transmitter Delay Compensation (TDC)")
- commit c25cc7993243 ("can: bittiming: add calculation for CAN FD Transmitter Delay Compensation (TDC)")

The netlink interface was missing from this series because the initial patch needed rework in order to make it more flexible for future changes.

At that time, Marc suggested to take inspiration from the recently released ethtool-netlink interface (Ref [1]).

ethtool uses nested attributes (c.f. NLA_NESTED type in validation policy). A bit of trivia: the NLA_NESTED type was introduced in version 2.6.15 of the kernel and thus actually predates Socket CAN.
Ref: commit bfa83a9e03cf ("[NETLINK]: Type-safe netlink messages/attributes interface")

Since then, Stephan shared additional remarks for improvement which are addressed in revision v4 of this series [2].

The first patch allows TDCV and TDCO to be zero. Previously, those zero values were assigned a special meaning. The replace that special meaning, two additional ctrmode flags are added: CAN_CTRLMODE_TDC_AUTO and CAN_CTRLMODE_TDC_MANUAL.

The second patch fixes the unit of the TDC parameters. In fact, those should be measured in clock period (a.k.a. minimum time quantum) and not in time quantum as it is done in current implementation.

The third patch modifies the prototype of can_calc_tdco() so that the data bittiming struct is passed as an argument instead of retrieving it from priv. This will prevent inconsistent configuration from occurring in case of early return.

The fourth patch is the real thing: the TDC netlink interface.

The fifth patch allows to retrieve TDCV from the device when in TDC_AUTO mode. This is done through a callback function.

The sixth and last patch addresses the concerns of Marc and Stefan that some devices which use a TDCO value relative to the Sample Point (so far, the TDC implementation used the formula SSP = TDCV + TDCO). To do so, an helper function to convert TDCO from an absolute value to a value relative to the sample point is added.

I prepared another patch series for iproute2-next. The series is available at [3] and can be helpful for tests.

[1] https://lore.kernel.org/linux-can/[email protected]/
[2] https://lore.kernel.org/linux-can/[email protected]/
[3] https://lore.kernel.org/linux-can/[email protected]

** Changelog **

v5 -> v6:

- fix a typo (double space) in include/linux-can/dev.h:can_get_relative_tdco().
- clear garbage values in struct can_tdc. Under certain conditions, some of the TDC parameters are irrelevant (example: can_tdc::tdcv when in auto mode). In the previous patch, those parameters were left untouched. We now clear any of the parameters which become irrelevant.
- fix a bug which allowed to have both CAN_CTRLMODE_TDC_AUTO and CAN_CTRLMODE_TDC_MANUAL to be set at the same time. Now, each time one of those flag is provided, we make sure to turn the other one off.
- in drivers/net/can/netlink.c:can_tdc_changelink(), store all the TDC parameters in a temporary structure and copy them all at once at the end of the function. This prevent TDC to be in an inconsistent state if the function had to return early due to out of bands parameters.
- similarly to above fix: clear the TDC flags (CAN_CTRLMODE_TDC_{AUTO,MANUAL}) if drivers/net/can/netlink.c:can_tdc_changelink() return an error code. This way, we are not left with inconsistent value.
- removed the first and last patch of the series because those have already been accepted in below pull request:
https://lore.kernel.org/linux-can/162939960964.18233.15653984142298230559.git-patchwork-notify@kernel.org/T/#t
- do not modify the TDC parameters if the databittiming parameters are not provided. The new logic is now as follow:
* data bittiming not provided: TDC parameters unchanged
* data bittiming provided: (unchanged from current behavior)
+ tdc-mode not provided: do can_calc_tdco (fully automated)
+ tdc-mode auto and tdco provided: TDC_AUTO
+ tdc-mode manual and both of tdcv and tdco provided: TDC_MANUAL

v4 -> v5:
- move to can_validate() all of the checks on TDC parameters that do not need access to priv.
- in can_tdc_get_size(), field IFLA_CAN_TDCV was not taken in account for size calculation when in automatic mode.
- if can_tdc_is_enabled(priv) returns true, we know that one of either CAN_CTRLMODE_TDC_AUTO or CAN_CTRLMODE_TDC_MANUAL is set. After confirming that CAN_CTRLMODE_TDC_MANUAL if off, we then implicitly know that CAN_CTRLMODE_TDC_AUTO is set and do not need to check it again. Remove such redundant check in can_tdc_fill_info().

v3 -> v4:
- add a patch to the series to change the unit from time quantum to clock period (a.k.a. minimum time quantum).
- add a callback function to retrieve TDCV from the device.

v2 -> v3:
- allows both TDCV and TDCO to be zero.
- introduce the can_tdc_get_relative_tdco() helper function
- other path of the series modified accordingly to above changes.

RFC v1 -> v2:
The v2 fixes several issue in can_tdc_get_size() and can_tdc_fill_info(). Namely: can_tdc_get_size() returned an incorrect size if TDC was not implemented and can_tdc_fill_info() did not include a fail path with nla_nest_cancel().

Vincent Mailhol (6):
can: bittiming: allow TDC{V,O} to be zero and add

    can_tdc_const::tdc{v,o,f}_min
  can: bittiming: change unit of TDC parameters to clock periods
  can: bittiming: change can_calc_tdco()'s prototype to not directly
    modify priv
  can: netlink: add interface for CAN-FD Transmitter Delay Compensation
    (TDC)

can: netlink: add can_priv::do_get_auto_tdcv() to retrieve tdcv from
device
can: dev: add can_tdc_get_relative_tdco() helper function

 drivers/net/can/dev/bittiming.c           |  28 +--
 drivers/net/can/dev/netlink.c             | 221 +++++++++++++++++++++-
 drivers/net/can/usb/etas_es58x/es58x_fd.c |   7 +-
 include/linux/can/bittiming.h             |  89 ++++++---
 include/linux/can/dev.h                   |  34 ++++
 include/uapi/linux/can/netlink.h          |  31 ++-
 6 files changed, 365 insertions(+), 45 deletions(-)
Signature · 1 line
2.32.0
#1 lore raw

PATCH v6 1/6 can: bittiming: allow TDC{V,O} to be zero and add can_tdc_const::tdc{v,o,f}_min

ISO 11898-1 specifies in section 11.3.3 "Transmitter delay compensation" that "the configuration range for [the] SSP position shall be at least 0 to 63 minimum time quanta."

Because SSP = TDCV + TDCO, it means that we should allow both TDCV and TDCO to hold zero value in order to honor SSP's minimum possible value.

However, current implementation assigned special meaning to TDCV and TDCO's zero values:
* TDCV = 0 -> TDCV is automatically measured by the transceiver.
* TDCO = 0 -> TDC is off.

In order to allow for those values to really be zero and to maintain current features, we introduce two new flags:
* CAN_CTRLMODE_TDC_AUTO indicates that the controller support automatic measurement of TDCV.
* CAN_CTRLMODE_TDC_MANUAL indicates that the controller support manual configuration of TDCV. N.B.: current implementation failed to provide an option for the driver to indicate that only manual mode was supported.

TDC is disabled if both CAN_CTRLMODE_TDC_AUTO and CAN_CTRLMODE_TDC_MANUAL flags are off, c.f. the helper function can_tdc_is_enabled() which is also introduced in this patch.

Also, this patch adds three fields: tdcv_min, tdco_min and tdcf_min to struct can_tdc_const. While we are not convinced that those three fields could be anything else than zero, we can imagine that some controllers might specify a lower bound on these. Thus, those minimums are really added "just in case".

Comments of struct can_tdc and can_tdc_const are updated accordingly.

Finally, the changes are applied to the etas_es58x driver.

---
 drivers/net/can/dev/bittiming.c           | 10 ++--
 drivers/net/can/usb/etas_es58x/es58x_fd.c |  7 ++-
 include/linux/can/bittiming.h             | 64 +++++++++++++++++------
 include/linux/can/dev.h                   |  4 ++
 include/uapi/linux/can/netlink.h          |  2 +
 5 files changed, 65 insertions(+), 22 deletions(-)
drivers/net/can/dev/bittiming.c +7 -3
index f49170eadd54..7dbda411915a 100644
@@ -182,9 +182,12 @@ void can_calc_tdco(struct net_device *dev)
 	struct can_tdc *tdc = &priv->tdc;
 	const struct can_tdc_const *tdc_const = priv->tdc_const;
 
-	if (!tdc_const)
+	if (!tdc_const ||
+	    !(priv->ctrlmode_supported & CAN_CTRLMODE_TDC_AUTO))
 		return;
 
+	priv->ctrlmode &= ~CAN_CTRLMODE_TDC_MASK;
+
 	/* As specified in ISO 11898-1 section 11.3.3 "Transmitter
 	 * delay compensation" (TDC) is only applicable if data BRP is
 	 * one or two.
@@ -193,9 +196,10 @@ void can_calc_tdco(struct net_device *dev)
 		/* Reuse "normal" sample point and convert it to time quanta */
 		u32 sample_point_in_tq = can_bit_time(dbt) * dbt->sample_point / 1000;
 
+		if (sample_point_in_tq < tdc_const->tdco_min)
+			return;
 		tdc->tdco = min(sample_point_in_tq, tdc_const->tdco_max);
-	} else {
-		tdc->tdco = 0;
+		priv->ctrlmode |= CAN_CTRLMODE_TDC_AUTO;
 	}
 }
 #endif /* CONFIG_CAN_CALC_BITTIMING */
drivers/net/can/usb/etas_es58x/es58x_fd.c +5 -2
index af042aa55f59..4f0cae29f4d8 100644
@@ -428,7 +428,7 @@ static int es58x_fd_enable_channel(struct es58x_priv *priv)
 		es58x_fd_convert_bittiming(&tx_conf_msg.data_bittiming,
 					   &priv->can.data_bittiming);
 
-		if (priv->can.tdc.tdco) {
+		if (can_tdc_is_enabled(&priv->can)) {
 			tx_conf_msg.tdc_enabled = 1;
 			tx_conf_msg.tdco = cpu_to_le16(priv->can.tdc.tdco);
 			tx_conf_msg.tdcf = cpu_to_le16(priv->can.tdc.tdcf);
@@ -505,8 +505,11 @@ static const struct can_bittiming_const es58x_fd_data_bittiming_const = {
  * Register" from Microchip.
  */
 static const struct can_tdc_const es58x_tdc_const = {
+	.tdcv_min = 0,
 	.tdcv_max = 0, /* Manual mode not supported. */
+	.tdco_min = 0,
 	.tdco_max = 127,
+	.tdcf_min = 0,
 	.tdcf_max = 127
 };
 
@@ -523,7 +526,7 @@ const struct es58x_parameters es58x_fd_param = {
 	.clock = {.freq = 80 * CAN_MHZ},
 	.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK | CAN_CTRLMODE_LISTENONLY |
 	    CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_FD | CAN_CTRLMODE_FD_NON_ISO |
-	    CAN_CTRLMODE_CC_LEN8_DLC,
+	    CAN_CTRLMODE_CC_LEN8_DLC | CAN_CTRLMODE_TDC_AUTO,
 	.tx_start_of_frame = 0xCEFA,	/* FACE in little endian */
 	.rx_start_of_frame = 0xFECA,	/* CAFE in little endian */
 	.tx_urb_cmd_max_len = ES58X_FD_TX_URB_CMD_MAX_LEN,
include/linux/can/bittiming.h +47 -17

101 lines not on this page · raw message

include/linux/can/dev.h +4 -0
index 2413253e54c7..6dacbbb41e68 100644
@@ -96,6 +96,10 @@ struct can_priv {
 #endif
 };
 
+static inline bool can_tdc_is_enabled(const struct can_priv *priv)
+{
+	return !!(priv->ctrlmode & CAN_CTRLMODE_TDC_MASK);
+}
 
 /* helper to define static CAN controller features at device creation time */
 static inline void can_set_static_ctrlmode(struct net_device *dev,
include/uapi/linux/can/netlink.h +2 -0
index f730d443b918..004cd09a7d49 100644
@@ -101,6 +101,8 @@ struct can_ctrlmode {
 #define CAN_CTRLMODE_PRESUME_ACK	0x40	/* Ignore missing CAN ACKs */
 #define CAN_CTRLMODE_FD_NON_ISO		0x80	/* CAN FD in non-ISO mode */
 #define CAN_CTRLMODE_CC_LEN8_DLC	0x100	/* Classic CAN DLC option */
+#define CAN_CTRLMODE_TDC_AUTO		0x200	/* CAN transiver automatically calculates TDCV */
+#define CAN_CTRLMODE_TDC_MANUAL		0x400	/* TDCV is manually set up by user */
 
 /*
  * CAN device statistics
Signature · 1 line
2.32.0
#2 lore raw

PATCH v6 2/6 can: bittiming: change unit of TDC parameters to clock periods

In reply to Vincent Mailhol

In the current implementation, all Transmission Delay Compensation (TDC) parameters are expressed in time quantum. However, ISO 11898-1 actually specifies that these should be expressed in *minimum* time quantum.

Furthermore, the minimum time quantum is specified to be "one node clock period long" (c.f. paragraph 11.3.1.1 "Bit time"). For sake of simplicity, we prefer to use the "clock period" term instead of "minimum time quantum" because we believe that it is more broadly understood.

This patch fixes that discrepancy by updating the documentation and the formula for TDCO calculation.

N.B. In can_calc_tdco(), the sample point (in time quantum) was calculated using a division, thus introducing a risk of rounding and truncation errors. On top of changing the unit to clock period, we also modified the formula to use only additions.

---
 drivers/net/can/dev/bittiming.c |  9 +++++----
 include/linux/can/bittiming.h   | 28 +++++++++++++++++-----------
 2 files changed, 22 insertions(+), 15 deletions(-)
drivers/net/can/dev/bittiming.c +5 -4
index 7dbda411915a..582463bc15f7 100644
@@ -193,12 +193,13 @@ void can_calc_tdco(struct net_device *dev)
 	 * one or two.
 	 */
 	if (dbt->brp == 1 || dbt->brp == 2) {
-		/* Reuse "normal" sample point and convert it to time quanta */
-		u32 sample_point_in_tq = can_bit_time(dbt) * dbt->sample_point / 1000;
+		/* Sample point in clock periods */
+		u32 sample_point_in_tc = (CAN_SYNC_SEG + dbt->prop_seg +
+					  dbt->phase_seg1) * dbt->brp;
 
-		if (sample_point_in_tq < tdc_const->tdco_min)
+		if (sample_point_in_tc < tdc_const->tdco_min)
 			return;
-		tdc->tdco = min(sample_point_in_tq, tdc_const->tdco_max);
+		tdc->tdco = min(sample_point_in_tc, tdc_const->tdco_max);
 		priv->ctrlmode |= CAN_CTRLMODE_TDC_AUTO;
 	}
 }
include/linux/can/bittiming.h +17 -11
index 9e20260611cc..aebbe65dab7e 100644
@@ -31,8 +31,8 @@
  *
  * To solve this issue, ISO 11898-1 introduces in section 11.3.3
  * "Transmitter delay compensation" a SSP (Secondary Sample Point)
- * equal to the distance, in time quanta, from the start of the bit
- * time on the TX pin to the actual measurement on the RX pin.
+ * equal to the distance from the start of the bit time on the TX pin
+ * to the actual measurement on the RX pin.
  *
  * This structure contains the parameters to calculate that SSP.
  *
@@ -44,8 +44,13 @@
  *                           |<------- TDCO ------->|
  *  |<----------- Secondary Sample Point ---------->|
  *
+ * To increase precision, contrary to the other bittiming parameters
+ * which are measured in time quanta, the TDC parameters are measured
+ * in clock periods (also referred as "minimum time quantum" in ISO
+ * 11898-1).
+ *
  * @tdcv: Transmitter Delay Compensation Value. The time needed for
- *	the signal to propagate, i.e. the distance, in time quanta,
+ *	the signal to propagate, i.e. the distance, in clock periods,
  *	from the start of the bit on the TX pin to when it is received
  *	on the RX pin. @tdcv depends on the controller modes:
  *
@@ -62,17 +67,18 @@
  *	TDC is disabled and all the values of this structure should be
  *	ignored.
  *
- * @tdco: Transmitter Delay Compensation Offset. Offset value, in time
- *	quanta, defining the distance between the start of the bit
- *	reception on the RX pin of the transceiver and the SSP
+ * @tdco: Transmitter Delay Compensation Offset. Offset value, in
+ *	clock periods, defining the distance between the start of the
+ *	bit reception on the RX pin of the transceiver and the SSP
  *	position such that SSP = @tdcv + @tdco.
  *
  * @tdcf: Transmitter Delay Compensation Filter window. Defines the
- *	minimum value for the SSP position in time quanta. If the SSP
- *	position is less than @tdcf, then no delay compensations occur
- *	and the normal sampling point is used instead. The feature is
- *	enabled if and only if @tdcv is set to zero (automatic mode)
- *	and @tdcf is configured to a value greater than @tdco.
+ *	minimum value for the SSP position in clock periods. If the
+ *	SSP position is less than @tdcf, then no delay compensations
+ *	occur and the normal sampling point is used instead. The
+ *	feature is enabled if and only if @tdcv is set to zero
+ *	(automatic mode) and @tdcf is configured to a value greater
+ *	than @tdco.
  */
 struct can_tdc {
 	u32 tdcv;
Signature · 1 line
2.32.0
#3 lore raw

PATCH v6 3/6 can: bittiming: change can_calc_tdco()'s prototype to not directly modify priv

In reply to Vincent Mailhol

The function can_calc_tdco() directly retrieves can_priv from the net_device and directly modifies it.

This is annoying for the upcoming patch. In drivers/net/can/dev/netlink.c:can_changelink(), the data bittiming are written to a temporary structure and memcpyed to can_priv only after everything succeeded. In the next patch, where we will introduce the netlink interface for TDC parameters, we will add a new TDC block which can potentially fail. For this reason, the data bittiming temporary structure has to be copied after that to-be-introduced TDC block. However, TDC also needs to access data bittiming information.

We change the prototype so that the data bittiming structure is passed to can_calc_tdco() as an argument instead of retrieving it from priv. This way can_calc_tdco() can access the data bittiming before it gets memcpyed to priv.

---
 drivers/net/can/dev/bittiming.c | 17 +++++++----------
 drivers/net/can/dev/netlink.c   |  3 ++-
 include/linux/can/bittiming.h   |  9 +++++++--
 3 files changed, 16 insertions(+), 13 deletions(-)
drivers/net/can/dev/bittiming.c +7 -10
index 582463bc15f7..d439ea676e6e 100644
@@ -175,18 +175,15 @@ int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
 	return 0;
 }
 
-void can_calc_tdco(struct net_device *dev)
-{
-	struct can_priv *priv = netdev_priv(dev);
-	const struct can_bittiming *dbt = &priv->data_bittiming;
-	struct can_tdc *tdc = &priv->tdc;
-	const struct can_tdc_const *tdc_const = priv->tdc_const;
+void can_calc_tdco(struct can_tdc *tdc, const struct can_tdc_const *tdc_const,
+		   const struct can_bittiming *dbt,
+		   u32 *ctrlmode, u32 ctrlmode_supported)
 
-	if (!tdc_const ||
-	    !(priv->ctrlmode_supported & CAN_CTRLMODE_TDC_AUTO))
+{
+	if (!tdc_const || !(ctrlmode_supported & CAN_CTRLMODE_TDC_AUTO))
 		return;
 
-	priv->ctrlmode &= ~CAN_CTRLMODE_TDC_MASK;
+	*ctrlmode &= ~CAN_CTRLMODE_TDC_MASK;
 
 	/* As specified in ISO 11898-1 section 11.3.3 "Transmitter
 	 * delay compensation" (TDC) is only applicable if data BRP is
@@ -200,7 +197,7 @@ void can_calc_tdco(struct net_device *dev)
 		if (sample_point_in_tc < tdc_const->tdco_min)
 			return;
 		tdc->tdco = min(sample_point_in_tc, tdc_const->tdco_max);
-		priv->ctrlmode |= CAN_CTRLMODE_TDC_AUTO;
+		*ctrlmode |= CAN_CTRLMODE_TDC_AUTO;
 	}
 }
 #endif /* CONFIG_CAN_CALC_BITTIMING */
drivers/net/can/dev/netlink.c +2 -1
index 80425636049d..e79c9a2ffbfc 100644
@@ -189,7 +189,8 @@ static int can_changelink(struct net_device *dev, struct nlattr *tb[],
 
 		memcpy(&priv->data_bittiming, &dbt, sizeof(dbt));
 
-		can_calc_tdco(dev);
+		can_calc_tdco(&priv->tdc, priv->tdc_const, &priv->data_bittiming,
+			      &priv->ctrlmode, priv->ctrlmode_supported);
 
 		if (priv->do_set_data_bittiming) {
 			/* Finally, set the bit-timing registers */
include/linux/can/bittiming.h +7 -2
index aebbe65dab7e..20b50baf3a02 100644
@@ -123,7 +123,9 @@ struct can_tdc_const {
 int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
 		       const struct can_bittiming_const *btc);
 
-void can_calc_tdco(struct net_device *dev);
+void can_calc_tdco(struct can_tdc *tdc, const struct can_tdc_const *tdc_const,
+		   const struct can_bittiming *dbt,
+		   u32 *ctrlmode, u32 ctrlmode_supported);
 #else /* !CONFIG_CAN_CALC_BITTIMING */
 static inline int
 can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
@@ -133,7 +135,10 @@ can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
 	return -EINVAL;
 }
 
-static inline void can_calc_tdco(struct net_device *dev)
+static inline void
+can_calc_tdco(struct can_tdc *tdc, const struct can_tdc_const *tdc_const,
+	      const struct can_bittiming *dbt,
+	      u32 *ctrlmode, u32 ctrlmode_supported)
 {
 }
 #endif /* CONFIG_CAN_CALC_BITTIMING */
Signature · 1 line
2.32.0
#4 lore raw

PATCH v6 4/6 can: netlink: add interface for CAN-FD Transmitter Delay Compensation (TDC)

In reply to Vincent Mailhol

Add the netlink interface for TDC parameters of struct can_tdc_const and can_tdc.

Contrary to the can_bittiming(_const) structures for which there is just a single IFLA_CAN(_DATA)_BITTMING(_CONST) entry per structure, here, we create a nested entry IFLA_CAN_TDC. Within this nested entry, additional IFLA_CAN_TDC_TDC* entries are added for each of the TDC parameters of the newly introduced struct can_tdc_const and struct can_tdc.

For struct can_tdc_const, these are:
IFLA_CAN_TDC_TDCV_MIN
IFLA_CAN_TDC_TDCV_MAX
IFLA_CAN_TDC_TDCO_MIN
IFLA_CAN_TDC_TDCO_MAX
IFLA_CAN_TDC_TDCF_MIN
IFLA_CAN_TDC_TDCF_MAX

For struct can_tdc, these are:
IFLA_CAN_TDC_TDCV
IFLA_CAN_TDC_TDCO
IFLA_CAN_TDC_TDCF

This is done so that changes can be applied in the future to the structures without breaking the netlink interface.

The TDC netlink logic works as follow:

* CAN_CTRLMODE_FD is not provided:
- if any TDC parameters are provided: error.

- TDC parameters not provided: TDC parameters unchanged.

* CAN_CTRLMODE_FD is provided and is false:
- TDC is deactivated: both the structure and the CAN_CTRLMODE_TDC_{AUTO,MANUAL} flags are flushed.

* CAN_CTRLMODE_FD provided and is true:
- CAN_CTRLMODE_TDC_{AUTO,MANUAL} and tdc{v,o,f} not provided: call can_calc_tdco() to automatically decide whether TDC should be activated and, if so, set CAN_CTRLMODE_TDC_AUTO and uses the calculated tdco value.

- CAN_CTRLMODE_TDC_AUTO and tdco provided: set CAN_CTRLMODE_TDC_AUTO and use the provided tdco value. Here, tdcv is illegal and tdcf is optional.

- CAN_CTRLMODE_TDC_MANUAL and both of tdcv and tdco provided: set CAN_CTRLMODE_TDC_MANUAL and use the provided tdcv and tdco value. Here, tdcf is optional.

- CAN_CTRLMODE_TDC_{AUTO,MANUAL} are mutually exclusive. Whenever one flag is turned on, the other will automatically be turned off. Providing both returns an error.

- Combination other than the one listed above are illegal and will return an error.

N.B. above rules mean that whenever CAN_CTRLMODE_FD is provided, the previous TDC values will be overwritten. The only option to reuse previous TDC value is to not provide CAN_CTRLMODE_FD.

All the new parameters are defined as u32. This arbitrary choice is done to mimic the other bittiming values with are also all of type u32. An u16 would have been sufficient to hold the TDC values.

This patch completes below series (c.f. [1]):
- commit 289ea9e4ae59 ("can: add new CAN FD bittiming parameters:
Transmitter Delay Compensation (TDC)")
- commit c25cc7993243 ("can: bittiming: add calculation for CAN FD Transmitter Delay Compensation (TDC)")

[1] https://lore.kernel.org/linux-can/[email protected]/T/#t

---
 drivers/net/can/dev/netlink.c    | 213 ++++++++++++++++++++++++++++++-
 include/uapi/linux/can/netlink.h |  29 ++++-
 2 files changed, 235 insertions(+), 7 deletions(-)
drivers/net/can/dev/netlink.c +208 -5
index e79c9a2ffbfc..c77cc6ae88b6 100644
@@ -2,6 +2,7 @@
 /* Copyright (C) 2005 Marc Kleine-Budde, Pengutronix
  * Copyright (C) 2006 Andrey Volkov, Varma Electronics
  * Copyright (C) 2008-2009 Wolfgang Grandegger <[email protected]>
+ * Copyright (C) 2021 Vincent Mailhol <[email protected]>
  */
 
 #include <linux/can/dev.h>
@@ -19,6 +20,19 @@ static const struct nla_policy can_policy[IFLA_CAN_MAX + 1] = {
 	[IFLA_CAN_DATA_BITTIMING] = { .len = sizeof(struct can_bittiming) },
 	[IFLA_CAN_DATA_BITTIMING_CONST]	= { .len = sizeof(struct can_bittiming_const) },
 	[IFLA_CAN_TERMINATION] = { .type = NLA_U16 },
+	[IFLA_CAN_TDC] = { .type = NLA_NESTED },
+};
+
+static const struct nla_policy can_tdc_policy[IFLA_CAN_TDC_MAX + 1] = {
+	[IFLA_CAN_TDC_TDCV_MIN] = { .type = NLA_U32 },
+	[IFLA_CAN_TDC_TDCV_MAX] = { .type = NLA_U32 },
+	[IFLA_CAN_TDC_TDCO_MIN] = { .type = NLA_U32 },
+	[IFLA_CAN_TDC_TDCO_MAX] = { .type = NLA_U32 },
+	[IFLA_CAN_TDC_TDCF_MIN] = { .type = NLA_U32 },
+	[IFLA_CAN_TDC_TDCF_MAX] = { .type = NLA_U32 },
+	[IFLA_CAN_TDC_TDCV] = { .type = NLA_U32 },
+	[IFLA_CAN_TDC_TDCO] = { .type = NLA_U32 },
+	[IFLA_CAN_TDC_TDCF] = { .type = NLA_U32 },
 };
 
 static int can_validate(struct nlattr *tb[], struct nlattr *data[],
@@ -30,6 +44,7 @@ static int can_validate(struct nlattr *tb[], struct nlattr *data[],
 	 * - nominal/arbitration bittiming
 	 * - data bittiming
 	 * - control mode with CAN_CTRLMODE_FD set
+	 * - TDC parameters are coherent (details below)
 	 */
 
 	if (!data)
@@ -37,8 +52,43 @@ static int can_validate(struct nlattr *tb[], struct nlattr *data[],
 
 	if (data[IFLA_CAN_CTRLMODE]) {
 		struct can_ctrlmode *cm = nla_data(data[IFLA_CAN_CTRLMODE]);
+		u32 tdc_flags = cm->flags & CAN_CTRLMODE_TDC_MASK;
 
 		is_can_fd = cm->flags & cm->mask & CAN_CTRLMODE_FD;
+
+		/* CAN_CTRLMODE_TDC_{AUTO,MANUAL} are mutually exclusive */
+		if (tdc_flags == CAN_CTRLMODE_TDC_MASK)
+			return -EOPNOTSUPP;
+		/* If one of the CAN_CTRLMODE_TDC_* flag is set then
+		 * TDC must be set and vice-versa
+		 */
+		if (!!tdc_flags != !!data[IFLA_CAN_TDC])
+			return -EOPNOTSUPP;
+		/* If providing TDC parameters, at least TDCO is
+		 * needed. TDCV is needed if and only if
+		 * CAN_CTRLMODE_TDC_MANUAL is set
+		 */
+		if (data[IFLA_CAN_TDC]) {
+			struct nlattr *tb_tdc[IFLA_CAN_TDC_MAX + 1];
+			int err;
+
+			err = nla_parse_nested(tb_tdc, IFLA_CAN_TDC_MAX,
+					       data[IFLA_CAN_TDC],
+					       can_tdc_policy, extack);
+			if (err)
+				return err;
+
+			if (tb_tdc[IFLA_CAN_TDC_TDCV]) {
+				if (tdc_flags & CAN_CTRLMODE_TDC_AUTO)
+					return -EOPNOTSUPP;
+			} else {
+				if (tdc_flags & CAN_CTRLMODE_TDC_MANUAL)
+					return -EOPNOTSUPP;
+			}
+
+			if (!tb_tdc[IFLA_CAN_TDC_TDCO])
+				return -EOPNOTSUPP;
+		}
 	}
 
 	if (is_can_fd) {
@@ -46,7 +96,7 @@ static int can_validate(struct nlattr *tb[], struct nlattr *data[],
 			return -EOPNOTSUPP;
 	}
 
-	if (data[IFLA_CAN_DATA_BITTIMING]) {
+	if (data[IFLA_CAN_DATA_BITTIMING] || data[IFLA_CAN_TDC]) {
 		if (!is_can_fd)
 			return -EOPNOTSUPP;
 	}
@@ -54,11 +104,60 @@ static int can_validate(struct nlattr *tb[], struct nlattr *data[],
 	return 0;
 }
 
+static int can_tdc_changelink(struct can_priv *priv, const struct nlattr *nla,
+			      struct netlink_ext_ack *extack)
+{
+	struct nlattr *tb_tdc[IFLA_CAN_TDC_MAX + 1];
+	struct can_tdc tdc = { 0 };
+	const struct can_tdc_const *tdc_const = priv->tdc_const;
+	int err;
+
+	if (!tdc_const || !can_tdc_is_enabled(priv))
+		return -EOPNOTSUPP;
+
+	err = nla_parse_nested(tb_tdc, IFLA_CAN_TDC_MAX, nla,
+			       can_tdc_policy, extack);
+	if (err)
+		return err;
+
+	if (tb_tdc[IFLA_CAN_TDC_TDCV]) {
+		u32 tdcv = nla_get_u32(tb_tdc[IFLA_CAN_TDC_TDCV]);
+
+		if (tdcv < tdc_const->tdcv_min || tdcv > tdc_const->tdcv_max)
+			return -EINVAL;
+
+		tdc.tdcv = tdcv;
+	}
+
+	if (tb_tdc[IFLA_CAN_TDC_TDCO]) {
+		u32 tdco = nla_get_u32(tb_tdc[IFLA_CAN_TDC_TDCO]);
+
+		if (tdco < tdc_const->tdco_min || tdco > tdc_const->tdco_max)
+			return -EINVAL;
+
+		tdc.tdco = tdco;
+	}
+
+	if (tb_tdc[IFLA_CAN_TDC_TDCF]) {
+		u32 tdcf = nla_get_u32(tb_tdc[IFLA_CAN_TDC_TDCF]);
+
+		if (tdcf < tdc_const->tdcf_min || tdcf > tdc_const->tdcf_max)
+			return -EINVAL;
+
+		tdc.tdcf = tdcf;
+	}
+
+	priv->tdc = tdc;
+
+	return 0;
+}
+
 static int can_changelink(struct net_device *dev, struct nlattr *tb[],
 			  struct nlattr *data[],
 			  struct netlink_ext_ack *extack)
 {
 	struct can_priv *priv = netdev_priv(dev);
+	u32 tdc_mask = 0;
 	int err;
 
 	/* We need synchronization with dev->stop() */
@@ -138,7 +237,16 @@ static int can_changelink(struct net_device *dev, struct nlattr *tb[],
 			dev->mtu = CAN_MTU;
 			memset(&priv->data_bittiming, 0,
 			       sizeof(priv->data_bittiming));
+			priv->ctrlmode &= ~CAN_CTRLMODE_TDC_MASK;
+			memset(&priv->tdc, 0, sizeof(priv->tdc));
 		}
+
+		tdc_mask = cm->mask & CAN_CTRLMODE_TDC_MASK;
+		/* CAN_CTRLMODE_TDC_{AUTO,MANUAL} are mutually
+		 * exclusive: make sure to turn the other one off
+		 */
+		if (tdc_mask)
+			priv->ctrlmode &= cm->flags | ~CAN_CTRLMODE_TDC_MASK;
 	}
 
 	if (data[IFLA_CAN_RESTART_MS]) {
@@ -187,10 +295,26 @@ static int can_changelink(struct net_device *dev, struct nlattr *tb[],
 			return -EINVAL;
 		}
 
-		memcpy(&priv->data_bittiming, &dbt, sizeof(dbt));
+		memset(&priv->tdc, 0, sizeof(priv->tdc));
+		if (data[IFLA_CAN_TDC]) {
+			/* TDC parameters are provided: use them */
+			err = can_tdc_changelink(priv, data[IFLA_CAN_TDC],
+						 extack);
+			if (err) {
+				priv->ctrlmode &= ~CAN_CTRLMODE_TDC_MASK;
+				return err;
+			}
+		} else if (!tdc_mask) {
+			/* Neither of TDC parameters nor TDC flags are
+			 * provided: do calculation
+			 */
+			can_calc_tdco(&priv->tdc, priv->tdc_const, &priv->data_bittiming,
+				      &priv->ctrlmode, priv->ctrlmode_supported);
+		} /* else: both CAN_CTRLMODE_TDC_{AUTO,MANUAL} are explicitly
+		   * turned off. TDC is disabled: do nothing
+		   */
 
-		can_calc_tdco(&priv->tdc, priv->tdc_const, &priv->data_bittiming,
-			      &priv->ctrlmode, priv->ctrlmode_supported);
+		memcpy(&priv->data_bittiming, &dbt, sizeof(dbt));
 
 		if (priv->do_set_data_bittiming) {
 			/* Finally, set the bit-timing registers */
@@ -227,6 +351,37 @@ static int can_changelink(struct net_device *dev, struct nlattr *tb[],
 	return 0;
 }
 
+static size_t can_tdc_get_size(const struct net_device *dev)
+{
+	struct can_priv *priv = netdev_priv(dev);
+	size_t size;
+
+	if (!priv->tdc_const)
+		return 0;
+
+	size = nla_total_size(0);			/* nest IFLA_CAN_TDC */
+	if (priv->ctrlmode_supported & CAN_CTRLMODE_TDC_MANUAL) {
+		size += nla_total_size(sizeof(u32));	/* IFLA_CAN_TDCV_MIN */
+		size += nla_total_size(sizeof(u32));	/* IFLA_CAN_TDCV_MAX */
+	}
+	size += nla_total_size(sizeof(u32));		/* IFLA_CAN_TDCO_MIN */
+	size += nla_total_size(sizeof(u32));		/* IFLA_CAN_TDCO_MAX */
+	if (priv->tdc_const->tdcf_max) {
+		size += nla_total_size(sizeof(u32));	/* IFLA_CAN_TDCF_MIN */
+		size += nla_total_size(sizeof(u32));	/* IFLA_CAN_TDCF_MAX */
+	}
+
+	if (can_tdc_is_enabled(priv)) {
+		if (priv->ctrlmode & CAN_CTRLMODE_TDC_MANUAL)
+			size += nla_total_size(sizeof(u32));	/* IFLA_CAN_TDCV */
+		size += nla_total_size(sizeof(u32));		/* IFLA_CAN_TDCO */
+		if (priv->tdc_const->tdcf_max)
+			size += nla_total_size(sizeof(u32));	/* IFLA_CAN_TDCF */
+	}
+
+	return size;
+}
+
 static size_t can_get_size(const struct net_device *dev)
 {
 	struct can_priv *priv = netdev_priv(dev);
@@ -258,10 +413,56 @@ static size_t can_get_size(const struct net_device *dev)
 		size += nla_total_size(sizeof(*priv->data_bitrate_const) *
 				       priv->data_bitrate_const_cnt);
 	size += sizeof(priv->bitrate_max);			/* IFLA_CAN_BITRATE_MAX */
+	size += can_tdc_get_size(dev);				/* IFLA_CAN_TDC */
 
 	return size;
 }
 
+static int can_tdc_fill_info(struct sk_buff *skb, const struct net_device *dev)
+{
+	struct nlattr *nest;
+	struct can_priv *priv = netdev_priv(dev);
+	struct can_tdc *tdc = &priv->tdc;
+	const struct can_tdc_const *tdc_const = priv->tdc_const;
+
+	if (!tdc_const)
+		return 0;
+
+	nest = nla_nest_start(skb, IFLA_CAN_TDC);
+	if (!nest)
+		return -EMSGSIZE;
+
+	if (priv->ctrlmode_supported & CAN_CTRLMODE_TDC_MANUAL &&
+	    (nla_put_u32(skb, IFLA_CAN_TDC_TDCV_MIN, tdc_const->tdcv_min) ||
+	     nla_put_u32(skb, IFLA_CAN_TDC_TDCV_MAX, tdc_const->tdcv_max)))
+		goto err_cancel;
+	if (nla_put_u32(skb, IFLA_CAN_TDC_TDCO_MIN, tdc_const->tdco_min) ||
+	    nla_put_u32(skb, IFLA_CAN_TDC_TDCO_MAX, tdc_const->tdco_max))
+		goto err_cancel;
+	if (tdc_const->tdcf_max &&
+	    (nla_put_u32(skb, IFLA_CAN_TDC_TDCF_MIN, tdc_const->tdcf_min) ||
+	     nla_put_u32(skb, IFLA_CAN_TDC_TDCF_MAX, tdc_const->tdcf_max)))
+		goto err_cancel;
+
+	if (can_tdc_is_enabled(priv)) {
+		if (priv->ctrlmode & CAN_CTRLMODE_TDC_MANUAL &&
+		    nla_put_u32(skb, IFLA_CAN_TDC_TDCV, tdc->tdcv))
+			goto err_cancel;
+		if (nla_put_u32(skb, IFLA_CAN_TDC_TDCO, tdc->tdco))
+			goto err_cancel;
+		if (tdc_const->tdcf_max &&
+		    nla_put_u32(skb, IFLA_CAN_TDC_TDCF, tdc->tdcf))
+			goto err_cancel;
+	}
+
+	nla_nest_end(skb, nest);
+	return 0;
+
+err_cancel:
+	nla_nest_cancel(skb, nest);
+	return -EMSGSIZE;
+}
+
 static int can_fill_info(struct sk_buff *skb, const struct net_device *dev)
 {
 	struct can_priv *priv = netdev_priv(dev);
@@ -319,7 +520,9 @@ static int can_fill_info(struct sk_buff *skb, const struct net_device *dev)
 
 	    (nla_put(skb, IFLA_CAN_BITRATE_MAX,
 		     sizeof(priv->bitrate_max),
-		     &priv->bitrate_max))
+		     &priv->bitrate_max)) ||
+
+	    (can_tdc_fill_info(skb, dev))
 	    )
 
 		return -EMSGSIZE;
include/uapi/linux/can/netlink.h +27 -2
index 004cd09a7d49..75b85c60efb2 100644
@@ -136,10 +136,35 @@ enum {
 	IFLA_CAN_BITRATE_CONST,
 	IFLA_CAN_DATA_BITRATE_CONST,
 	IFLA_CAN_BITRATE_MAX,
-	__IFLA_CAN_MAX
+	IFLA_CAN_TDC,
+
+	/* add new constants above here */
+	__IFLA_CAN_MAX,
+	IFLA_CAN_MAX = __IFLA_CAN_MAX - 1
 };
 
-#define IFLA_CAN_MAX	(__IFLA_CAN_MAX - 1)
+/*
+ * CAN FD Transmitter Delay Compensation (TDC)
+ *
+ * Please refer to struct can_tdc_const and can_tdc in
+ * include/linux/can/bittiming.h for further details.
+ */
+enum {
+	IFLA_CAN_TDC_UNSPEC,
+	IFLA_CAN_TDC_TDCV_MIN,	/* u32 */
+	IFLA_CAN_TDC_TDCV_MAX,	/* u32 */
+	IFLA_CAN_TDC_TDCO_MIN,	/* u32 */
+	IFLA_CAN_TDC_TDCO_MAX,	/* u32 */
+	IFLA_CAN_TDC_TDCF_MIN,	/* u32 */
+	IFLA_CAN_TDC_TDCF_MAX,	/* u32 */
+	IFLA_CAN_TDC_TDCV,	/* u32 */
+	IFLA_CAN_TDC_TDCO,	/* u32 */
+	IFLA_CAN_TDC_TDCF,	/* u32 */
+
+	/* add new constants above here */
+	__IFLA_CAN_TDC,
+	IFLA_CAN_TDC_MAX = __IFLA_CAN_TDC - 1
+};
 
 /* u16 termination range: 1..65535 Ohms */
 #define CAN_TERMINATION_DISABLED 0
Signature · 1 line
2.32.0
#5 lore raw

Re: [PATCH v6 4/6] can: netlink: add interface for CAN-FD Transmitter Delay Compensation (TDC)

Hi Vincent,

Thanks for your patch, which is now commit d99755f71a80df33 ("can: netlink: add interface for CAN-FD Transmitter Delay Compensation (TDC)") in v5.16.

Quoted 37 lines · Vincent Mailhol

Add the netlink interface for TDC parameters of struct can_tdc_const and can_tdc.

Contrary to the can_bittiming(_const) structures for which there is just a single IFLA_CAN(_DATA)_BITTMING(_CONST) entry per structure, here, we create a nested entry IFLA_CAN_TDC. Within this nested entry, additional IFLA_CAN_TDC_TDC* entries are added for each of the TDC parameters of the newly introduced struct can_tdc_const and struct can_tdc.

For struct can_tdc_const, these are:
IFLA_CAN_TDC_TDCV_MIN
IFLA_CAN_TDC_TDCV_MAX
IFLA_CAN_TDC_TDCO_MIN
IFLA_CAN_TDC_TDCO_MAX
IFLA_CAN_TDC_TDCF_MIN
IFLA_CAN_TDC_TDCF_MAX

For struct can_tdc, these are:
IFLA_CAN_TDC_TDCV
IFLA_CAN_TDC_TDCO
IFLA_CAN_TDC_TDCF

This is done so that changes can be applied in the future to the structures without breaking the netlink interface.

The TDC netlink logic works as follow:

* CAN_CTRLMODE_FD is not provided:
- if any TDC parameters are provided: error.

- TDC parameters not provided: TDC parameters unchanged.

* CAN_CTRLMODE_FD is provided and is false:
- TDC is deactivated: both the structure and the CAN_CTRLMODE_TDC_{AUTO,MANUAL} flags are flushed.

Quoted 5 lines

* CAN_CTRLMODE_FD provided and is true:
- CAN_CTRLMODE_TDC_{AUTO,MANUAL} and tdc{v,o,f} not provided: call can_calc_tdco() to automatically decide whether TDC should be activated and, if so, set CAN_CTRLMODE_TDC_AUTO and uses the calculated tdco value.

This is not reflected in the code (see below).

By default, a CAN-FD interface comes up in TDC-AUTO mode (if supported), using a calculated tdco value. However, enabling "tdc-mode auto" explicitly from userland requires also specifying an explicit tdco value. I.e.

ip link set can0 type can bitrate 500000 dbitrate 8000000 fd on

gives "can <FD,TDC-AUTO>" and "tdcv 0 tdco 3", while

ip link set can0 type can bitrate 500000 dbitrate 8000000 fd on
tdc-mode auto

gives:

tdc-mode auto: RTNETLINK answers: Operation not supported

unless I add an explicit "tdco 3".

According to your commit description, this is not the expected behavior? Thanks!

Quoted 44 lines

- CAN_CTRLMODE_TDC_AUTO and tdco provided: set CAN_CTRLMODE_TDC_AUTO and use the provided tdco value. Here, tdcv is illegal and tdcf is optional.

- CAN_CTRLMODE_TDC_MANUAL and both of tdcv and tdco provided: set CAN_CTRLMODE_TDC_MANUAL and use the provided tdcv and tdco value. Here, tdcf is optional.

- CAN_CTRLMODE_TDC_{AUTO,MANUAL} are mutually exclusive. Whenever one flag is turned on, the other will automatically be turned off. Providing both returns an error.

- Combination other than the one listed above are illegal and will return an error.

N.B. above rules mean that whenever CAN_CTRLMODE_FD is provided, the previous TDC values will be overwritten. The only option to reuse previous TDC value is to not provide CAN_CTRLMODE_FD.

All the new parameters are defined as u32. This arbitrary choice is done to mimic the other bittiming values with are also all of type u32. An u16 would have been sufficient to hold the TDC values.

This patch completes below series (c.f. [1]):
- commit 289ea9e4ae59 ("can: add new CAN FD bittiming parameters:
Transmitter Delay Compensation (TDC)")
- commit c25cc7993243 ("can: bittiming: add calculation for CAN FD Transmitter Delay Compensation (TDC)")

[1] https://lore.kernel.org/linux-can/[email protected]/T/#t

drivers/net/can/dev/netlink.c +1 -0
@@ -37,8 +52,43 @@ static int can_validate(struct nlattr *tb[], struct nlattr *data[],
 
        if (data[IFLA_CAN_CTRLMODE]) {
                struct can_ctrlmode *cm = nla_data(data[IFLA_CAN_CTRLMODE]);
+               u32 tdc_flags = cm->flags & CAN_CTRLMODE_TDC_MASK;
 

drivers/net/can/dev/netlink.c static int can_validate(struct nlattr *tb[], struct nlattr *data[], · line 66

Quoted 10 lines
drivers/net/can/dev/netlink.c +9 -0
                is_can_fd = cm->flags & cm->mask & CAN_CTRLMODE_FD;
+
+               /* CAN_CTRLMODE_TDC_{AUTO,MANUAL} are mutually exclusive */
+               if (tdc_flags == CAN_CTRLMODE_TDC_MASK)
+                       return -EOPNOTSUPP;
+               /* If one of the CAN_CTRLMODE_TDC_* flag is set then
+                * TDC must be set and vice-versa
+                */
+               if (!!tdc_flags != !!data[IFLA_CAN_TDC])
+                       return -EOPNOTSUPP;

CAN_CTRLMODE_TDC_{AUTO,MANUAL} and none of tdc{v,o,f} provided is rejected.

Quoted 14 lines
drivers/net/can/dev/netlink.c +14 -0
+               /* If providing TDC parameters, at least TDCO is
+                * needed. TDCV is needed if and only if
+                * CAN_CTRLMODE_TDC_MANUAL is set
+                */
+               if (data[IFLA_CAN_TDC]) {
+                       struct nlattr *tb_tdc[IFLA_CAN_TDC_MAX + 1];
+                       int err;
+
+                       err = nla_parse_nested(tb_tdc, IFLA_CAN_TDC_MAX,
+                                              data[IFLA_CAN_TDC],
+                                              can_tdc_policy, extack);
+                       if (err)
+                               return err;
+

drivers/net/can/dev/netlink.c static int can_validate(struct nlattr *tb[], struct nlattr *data[], · line 90

Quoted 10 lines
drivers/net/can/dev/netlink.c +10 -0
+                       if (tb_tdc[IFLA_CAN_TDC_TDCV]) {
+                               if (tdc_flags & CAN_CTRLMODE_TDC_AUTO)
+                                       return -EOPNOTSUPP;
+                       } else {
+                               if (tdc_flags & CAN_CTRLMODE_TDC_MANUAL)
+                                       return -EOPNOTSUPP;
+                       }
+
+                       if (!tb_tdc[IFLA_CAN_TDC_TDCO])
+                               return -EOPNOTSUPP;

CAN_CTRLMODE_TDC_AUTO and tdco not provided is rejected.

drivers/net/can/dev/netlink.c static int can_validate(struct nlattr *tb[], struct nlattr *data[], · line 94

Quoted 4 lines
drivers/net/can/dev/netlink.c +1 -0
+               }
        }
 
        if (is_can_fd) {

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
#6 lore raw

Re: [PATCH v6 4/6] can: netlink: add interface for CAN-FD Transmitter Delay Compensation (TDC)

Hi Geert,

Quoted 45 lines · Geert Uytterhoeven

Hi Vincent,

Thanks for your patch, which is now commit d99755f71a80df33 ("can: netlink: add interface for CAN-FD Transmitter Delay Compensation (TDC)") in v5.16.

Quoted 37 lines · Vincent Mailhol

Add the netlink interface for TDC parameters of struct can_tdc_const and can_tdc.

Contrary to the can_bittiming(_const) structures for which there is just a single IFLA_CAN(_DATA)_BITTMING(_CONST) entry per structure, here, we create a nested entry IFLA_CAN_TDC. Within this nested entry, additional IFLA_CAN_TDC_TDC* entries are added for each of the TDC parameters of the newly introduced struct can_tdc_const and struct can_tdc.

For struct can_tdc_const, these are:
IFLA_CAN_TDC_TDCV_MIN
IFLA_CAN_TDC_TDCV_MAX
IFLA_CAN_TDC_TDCO_MIN
IFLA_CAN_TDC_TDCO_MAX
IFLA_CAN_TDC_TDCF_MIN
IFLA_CAN_TDC_TDCF_MAX

For struct can_tdc, these are:
IFLA_CAN_TDC_TDCV
IFLA_CAN_TDC_TDCO
IFLA_CAN_TDC_TDCF

This is done so that changes can be applied in the future to the structures without breaking the netlink interface.

The TDC netlink logic works as follow:

* CAN_CTRLMODE_FD is not provided:
- if any TDC parameters are provided: error.

- TDC parameters not provided: TDC parameters unchanged.

* CAN_CTRLMODE_FD is provided and is false:
- TDC is deactivated: both the structure and the CAN_CTRLMODE_TDC_{AUTO,MANUAL} flags are flushed.

Quoted 7 lines
Quoted 5 lines

* CAN_CTRLMODE_FD provided and is true:
- CAN_CTRLMODE_TDC_{AUTO,MANUAL} and tdc{v,o,f} not provided: call can_calc_tdco() to automatically decide whether TDC should be activated and, if so, set CAN_CTRLMODE_TDC_AUTO and uses the calculated tdco value.

This is not reflected in the code (see below).

Let me first repost what I wrote but this time using numerals and letters instead of the bullet points:

The TDC netlink logic works as follow:

1. CAN_CTRLMODE_FD is not provided:
a) if any TDC parameters are provided: error.

b) TDC parameters not provided: TDC parameters unchanged.

2. CAN_CTRLMODE_FD is provided and is false:
a) TDC is deactivated: both the structure and the CAN_CTRLMODE_TDC_{AUTO,MANUAL} flags are flushed.

3. CAN_CTRLMODE_FD provided and is true:
a) CAN_CTRLMODE_TDC_{AUTO,MANUAL} and tdc{v,o,f} not provided: call can_calc_tdco() to automatically decide whether TDC should be activated and, if so, set CAN_CTRLMODE_TDC_AUTO and uses the calculated tdco value.

b) CAN_CTRLMODE_TDC_AUTO and tdco provided: set CAN_CTRLMODE_TDC_AUTO and use the provided tdco value. Here, tdcv is illegal and tdcf is optional.

c) CAN_CTRLMODE_TDC_MANUAL and both of tdcv and tdco provided: set CAN_CTRLMODE_TDC_MANUAL and use the provided tdcv and tdco value. Here, tdcf is optional.

d) CAN_CTRLMODE_TDC_{AUTO,MANUAL} are mutually exclusive. Whenever one flag is turned on, the other will automatically be turned off. Providing both returns an error.

e) Combination other than the one listed above are illegal and will return an error.

You can double check that it is the exact same as before.

Quoted 6 lines

By default, a CAN-FD interface comes up in TDC-AUTO mode (if supported), using a calculated tdco value. However, enabling "tdc-mode auto" explicitly from userland requires also specifying an explicit tdco value. I.e.

ip link set can0 type can bitrate 500000 dbitrate 8000000 fd on

                                                                ^^^^^

Here:

- CAN_CTRLMODE_FD provided and is true: so we are in close 3.

- CAN_CTRLMODE_TDC_{AUTO,MANUAL} and tdc{v,o,f} not provided: so we *are* in sub-clause a)

3.a) tells that the framework will decide whether or not TDC should be activated, and if activated, will set the TDCO.

Quoted 1 line

gives "can <FD,TDC-AUTO>" and "tdcv 0 tdco 3", while

Looks perfectly coherent with 3.a)

Note that with lower data bitrate, the framework might have decided to set TDC off.

Quoted 2 lines

ip link set can0 type can bitrate 500000 dbitrate 8000000 fd on
tdc-mode auto

This time:

- CAN_CTRLMODE_FD provided and is true: so we are in close 3.

- CAN_CTRLMODE_TDC_AUTO is provided, we are *not* in sub-clause a)

- tdco is not provided.

No explicit clauses matches this pattern so it defaults to the last
sub-clause: e), which means an error.

Quoted 3 lines

gives:

tdc-mode auto: RTNETLINK answers: Operation not supported

Looks perfectly coherent with 3.e)

Quoted 1 line

unless I add an explicit "tdco 3".

Yes, if you provide tcdo 3, then you are under 3.b).

Quoted 2 lines

According to your commit description, this is not the expected behavior? Thanks!

Looking back to my commit, I admit that the explanation is convoluted and could be hard to digest, but I do not see a mismatch between the description and the behaviour.

Yours sincerely,
Vincent Mailhol

#7 lore raw

Re: [PATCH v6 4/6] can: netlink: add interface for CAN-FD Transmitter Delay Compensation (TDC)

Hi Vincent,

Quoted 103 lines · Vincent Mailhol
Quoted 43 lines · Geert Uytterhoeven

Thanks for your patch, which is now commit d99755f71a80df33 ("can: netlink: add interface for CAN-FD Transmitter Delay Compensation (TDC)") in v5.16.

Quoted 37 lines · Vincent Mailhol

Add the netlink interface for TDC parameters of struct can_tdc_const and can_tdc.

Contrary to the can_bittiming(_const) structures for which there is just a single IFLA_CAN(_DATA)_BITTMING(_CONST) entry per structure, here, we create a nested entry IFLA_CAN_TDC. Within this nested entry, additional IFLA_CAN_TDC_TDC* entries are added for each of the TDC parameters of the newly introduced struct can_tdc_const and struct can_tdc.

For struct can_tdc_const, these are:
IFLA_CAN_TDC_TDCV_MIN
IFLA_CAN_TDC_TDCV_MAX
IFLA_CAN_TDC_TDCO_MIN
IFLA_CAN_TDC_TDCO_MAX
IFLA_CAN_TDC_TDCF_MIN
IFLA_CAN_TDC_TDCF_MAX

For struct can_tdc, these are:
IFLA_CAN_TDC_TDCV
IFLA_CAN_TDC_TDCO
IFLA_CAN_TDC_TDCF

This is done so that changes can be applied in the future to the structures without breaking the netlink interface.

The TDC netlink logic works as follow:

* CAN_CTRLMODE_FD is not provided:
- if any TDC parameters are provided: error.

- TDC parameters not provided: TDC parameters unchanged.

* CAN_CTRLMODE_FD is provided and is false:
- TDC is deactivated: both the structure and the CAN_CTRLMODE_TDC_{AUTO,MANUAL} flags are flushed.

Quoted 7 lines
Quoted 5 lines

* CAN_CTRLMODE_FD provided and is true:
- CAN_CTRLMODE_TDC_{AUTO,MANUAL} and tdc{v,o,f} not provided: call can_calc_tdco() to automatically decide whether TDC should be activated and, if so, set CAN_CTRLMODE_TDC_AUTO and uses the calculated tdco value.

This is not reflected in the code (see below).

Let me first repost what I wrote but this time using numerals and letters instead of the bullet points:

The TDC netlink logic works as follow:

1. CAN_CTRLMODE_FD is not provided:
a) if any TDC parameters are provided: error.

b) TDC parameters not provided: TDC parameters unchanged.

2. CAN_CTRLMODE_FD is provided and is false:
a) TDC is deactivated: both the structure and the CAN_CTRLMODE_TDC_{AUTO,MANUAL} flags are flushed.

3. CAN_CTRLMODE_FD provided and is true:
a) CAN_CTRLMODE_TDC_{AUTO,MANUAL} and tdc{v,o,f} not provided: call can_calc_tdco() to automatically decide whether TDC should be activated and, if so, set CAN_CTRLMODE_TDC_AUTO and uses the calculated tdco value.

b) CAN_CTRLMODE_TDC_AUTO and tdco provided: set CAN_CTRLMODE_TDC_AUTO and use the provided tdco value. Here, tdcv is illegal and tdcf is optional.

c) CAN_CTRLMODE_TDC_MANUAL and both of tdcv and tdco provided: set CAN_CTRLMODE_TDC_MANUAL and use the provided tdcv and tdco value. Here, tdcf is optional.

d) CAN_CTRLMODE_TDC_{AUTO,MANUAL} are mutually exclusive. Whenever one flag is turned on, the other will automatically be turned off. Providing both returns an error.

e) Combination other than the one listed above are illegal and will return an error.

You can double check that it is the exact same as before.

Quoted 6 lines

By default, a CAN-FD interface comes up in TDC-AUTO mode (if supported), using a calculated tdco value. However, enabling "tdc-mode auto" explicitly from userland requires also specifying an explicit tdco value. I.e.

ip link set can0 type can bitrate 500000 dbitrate 8000000 fd on

                                                                ^^^^^

Here:

- CAN_CTRLMODE_FD provided and is true: so we are in close 3.

- CAN_CTRLMODE_TDC_{AUTO,MANUAL} and tdc{v,o,f} not provided: so we *are* in sub-clause a)

Quoted 8 lines

3.a) tells that the framework will decide whether or not TDC should be activated, and if activated, will set the TDCO.

Quoted 1 line

gives "can <FD,TDC-AUTO>" and "tdcv 0 tdco 3", while

Looks perfectly coherent with 3.a)

Note that with lower data bitrate, the framework might have decided to set TDC off.

Yes, that case is fine for sure.

Quoted 11 lines
Quoted 2 lines

ip link set can0 type can bitrate 500000 dbitrate 8000000 fd on
tdc-mode auto

This time:

- CAN_CTRLMODE_FD provided and is true: so we are in close 3.

- CAN_CTRLMODE_TDC_AUTO is provided, we are *not* in sub-clause a)

- tdco is not provided.

Quoted 8 lines

No explicit clauses matches this pattern so it defaults to the last
sub-clause: e), which means an error.

Quoted 3 lines

gives:

tdc-mode auto: RTNETLINK answers: Operation not supported

Looks perfectly coherent with 3.e)

Thanks, I misread this as clause 3.a being applicable (hasn't NOT a higher precedence than AND? ;-)

Quoted 10 lines
Quoted 1 line

unless I add an explicit "tdco 3".

Yes, if you provide tcdo 3, then you are under 3.b).

Quoted 2 lines

According to your commit description, this is not the expected behavior? Thanks!

Looking back to my commit, I admit that the explanation is convoluted and could be hard to digest, but I do not see a mismatch between the description and the behaviour.

OK, so the description and the behaviour do match.

However, I still find it a bit counter-intuitive that CAN_CTRLMODE_TDC_AUTO is not fully automatic, but automatic-with-one- manual-knob.

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
#8 lore raw

Re: [PATCH v6 4/6] can: netlink: add interface for CAN-FD Transmitter Delay Compensation (TDC)

Quoted 5 lines · Geert Uytterhoeven

Hi Vincent,

Quoted 1 line · Vincent Mailhol

On 30/05/2025 at 20:44, Geert Uytterhoeven wrote:

(...)

Quoted 76 lines
Quoted 51 lines

Let me first repost what I wrote but this time using numerals and letters instead of the bullet points:

The TDC netlink logic works as follow:

1. CAN_CTRLMODE_FD is not provided:
a) if any TDC parameters are provided: error.

b) TDC parameters not provided: TDC parameters unchanged.

2. CAN_CTRLMODE_FD is provided and is false:
a) TDC is deactivated: both the structure and the CAN_CTRLMODE_TDC_{AUTO,MANUAL} flags are flushed.

3. CAN_CTRLMODE_FD provided and is true:
a) CAN_CTRLMODE_TDC_{AUTO,MANUAL} and tdc{v,o,f} not provided: call can_calc_tdco() to automatically decide whether TDC should be activated and, if so, set CAN_CTRLMODE_TDC_AUTO and uses the calculated tdco value.

b) CAN_CTRLMODE_TDC_AUTO and tdco provided: set CAN_CTRLMODE_TDC_AUTO and use the provided tdco value. Here, tdcv is illegal and tdcf is optional.

c) CAN_CTRLMODE_TDC_MANUAL and both of tdcv and tdco provided: set CAN_CTRLMODE_TDC_MANUAL and use the provided tdcv and tdco value. Here, tdcf is optional.

d) CAN_CTRLMODE_TDC_{AUTO,MANUAL} are mutually exclusive. Whenever one flag is turned on, the other will automatically be turned off. Providing both returns an error.

e) Combination other than the one listed above are illegal and will return an error.

You can double check that it is the exact same as before.

Quoted 6 lines

By default, a CAN-FD interface comes up in TDC-AUTO mode (if supported), using a calculated tdco value. However, enabling "tdc-mode auto" explicitly from userland requires also specifying an explicit tdco value. I.e.

ip link set can0 type can bitrate 500000 dbitrate 8000000 fd on

                                                                ^^^^^

Here:

- CAN_CTRLMODE_FD provided and is true: so we are in close 3.

- CAN_CTRLMODE_TDC_{AUTO,MANUAL} and tdc{v,o,f} not provided: so we *are* in sub-clause a)

Quoted 8 lines

3.a) tells that the framework will decide whether or not TDC should be activated, and if activated, will set the TDCO.

Quoted 1 line

gives "can <FD,TDC-AUTO>" and "tdcv 0 tdco 3", while

Looks perfectly coherent with 3.a)

Note that with lower data bitrate, the framework might have decided to set TDC off.

Yes, that case is fine for sure.

Quoted 14 lines
Quoted 2 lines

ip link set can0 type can bitrate 500000 dbitrate 8000000 fd on
tdc-mode auto

This time:

- CAN_CTRLMODE_FD provided and is true: so we are in close 3.

- CAN_CTRLMODE_TDC_AUTO is provided, we are *not* in sub-clause a)

- tdco is not provided.

No explicit clauses matches this pattern so it defaults to the last
sub-clause: e), which means an error.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

gives:

tdc-mode auto: RTNETLINK answers: Operation not supported

Looks perfectly coherent with 3.e)

Thanks, I misread this as clause 3.a being applicable (hasn't NOT a higher precedence than AND? ;-)

Now I see where your confusion comes from.

But if I read:

Paul and Mary not present

I understand that both are absent. I do not see an ambiguity that Paul may be present and only Mary absent.

Well, I guess this is what occurs when you write in English instead of C.

And I just realize that there was an actual mistake in my description: I forgot to state the obvious and I omitted to mention that TDC_OFF means that TDC is forcefully deactivated.

Quoted 11 lines
Quoted 10 lines
Quoted 1 line

unless I add an explicit "tdco 3".

Yes, if you provide tcdo 3, then you are under 3.b).

Quoted 2 lines

According to your commit description, this is not the expected behavior? Thanks!

Looking back to my commit, I admit that the explanation is convoluted and could be hard to digest, but I do not see a mismatch between the description and the behaviour.

Quoted 5 lines

OK, so the description and the behaviour do match.

However, I still find it a bit counter-intuitive that CAN_CTRLMODE_TDC_AUTO is not fully automatic, but automatic-with-one- manual-knob.

Fair enough. In truth, TDC_AUTO and TDC_MANUAL means respectively TDC_ON_WITH_TDCV_AUTO and TDC_ON_WITH_TDCV_MANUAL.

The problem is that it makes close to no sense to allow the user to ask to have TDC explicitly ON and TDCO automatically calculated. And if I recall, TDC_AUTO and TDC_MANUAL is also consistent with what I found in the datasheets.

On a side note, I still have to update Documentation/networking/can.rst to add some TDC explanations. I will of course not change the naming (this is part of the UAPI, so now it is set in stone), but I will try to clarify the concerns you raised here so that the documentation is more clear than my patch comments.

Also, I am not writing this documentation any soon. CAN-XL has a higher priority in my TODO list :)

Yours sincerely,
Vincent Mailhol

#9 lore raw

PATCH v6 5/6 can: netlink: add can_priv::do_get_auto_tdcv() to retrieve tdcv from device

In reply to Vincent Mailhol

Some CAN device can measure the TDCV (Transmission Delay Compensation Value) automatically for each transmitted CAN frames.

A callback function do_get_auto_tdcv() is added to retrieve that value. This function is used only if CAN_CTRLMODE_TDC_AUTO is enabled (if CAN_CTRLMODE_TDC_MANUAL is selected, the TDCV value is provided by the user).

If the device does not support reporting of TDCV, do_get_auto_tdcv() should be set to NULL and TDCV will not be reported by the netlink interface.

On success, do_get_auto_tdcv() shall return 0. If the value can not be measured by the device, for example because network is down or because no frames were transmitted yet, can_priv::do_get_auto_tdcv() shall return a negative error code (e.g. -EINVAL) to signify that the value is not yet available. In such cases, TDCV is not reported by the netlink interface.

---
 drivers/net/can/dev/netlink.c | 15 ++++++++++++---
 include/linux/can/dev.h       |  1 +
 2 files changed, 13 insertions(+), 3 deletions(-)
drivers/net/can/dev/netlink.c +12 -3
index c77cc6ae88b6..95cca4e5251f 100644
@@ -372,7 +372,8 @@ static size_t can_tdc_get_size(const struct net_device *dev)
 	}
 
 	if (can_tdc_is_enabled(priv)) {
-		if (priv->ctrlmode & CAN_CTRLMODE_TDC_MANUAL)
+		if (priv->ctrlmode & CAN_CTRLMODE_TDC_MANUAL ||
+		    priv->do_get_auto_tdcv)
 			size += nla_total_size(sizeof(u32));	/* IFLA_CAN_TDCV */
 		size += nla_total_size(sizeof(u32));		/* IFLA_CAN_TDCO */
 		if (priv->tdc_const->tdcf_max)
@@ -445,8 +446,16 @@ static int can_tdc_fill_info(struct sk_buff *skb, const struct net_device *dev)
 		goto err_cancel;
 
 	if (can_tdc_is_enabled(priv)) {
-		if (priv->ctrlmode & CAN_CTRLMODE_TDC_MANUAL &&
-		    nla_put_u32(skb, IFLA_CAN_TDC_TDCV, tdc->tdcv))
+		u32 tdcv;
+		int err = -EINVAL;
+
+		if (priv->ctrlmode & CAN_CTRLMODE_TDC_MANUAL) {
+			tdcv = tdc->tdcv;
+			err = 0;
+		} else if (priv->do_get_auto_tdcv) {
+			err = priv->do_get_auto_tdcv(dev, &tdcv);
+		}
+		if (!err && nla_put_u32(skb, IFLA_CAN_TDC_TDCV, tdcv))
 			goto err_cancel;
 		if (nla_put_u32(skb, IFLA_CAN_TDC_TDCO, tdc->tdco))
 			goto err_cancel;
include/linux/can/dev.h +1 -0
index 6dacbbb41e68..b4aa0f048cab 100644
@@ -82,6 +82,7 @@ struct can_priv {
 			    enum can_state *state);
 	int (*do_get_berr_counter)(const struct net_device *dev,
 				   struct can_berr_counter *bec);
+	int (*do_get_auto_tdcv)(const struct net_device *dev, u32 *tdcv);
 
 	unsigned int echo_skb_max;
 	struct sk_buff **echo_skb;
Signature · 1 line
2.32.0
#10 lore raw

PATCH v6 6/6 can: dev: add can_tdc_get_relative_tdco() helper function

In reply to Vincent Mailhol

struct can_tdc::tdco represents the absolute offset from TDCV. Some controllers use instead an offset relative to the Sample Point (SP) such that:
| SSP = TDCV + absolute TDCO
| = TDCV + SP + relative TDCO

Consequently:
| relative TDCO = absolute TDCO - SP

The function can_tdc_get_relative_tdco() allow to retrieve this relative TDCO value.

---
Hi Marc,

As Stefan pointed out in:
https://lore.kernel.org/linux-can/79691916e4280970f583a54cd5010ece025a1c53.c
[email protected]/
it seems that no so many CAN devices are using the relative TDCO (maybe the ESDACC is the only one?). Depending on the output of your discussion with Microchip and if the mcp25xxfd is indeed using an absolute tdco, it might make sense to drop this patch from the series.

Yours sincerely,
Vincent

---
 include/linux/can/dev.h | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
include/linux/can/dev.h +29 -0
index b4aa0f048cab..45f19d9db5ca 100644
@@ -102,6 +102,35 @@ static inline bool can_tdc_is_enabled(const struct can_priv *priv)
 	return !!(priv->ctrlmode & CAN_CTRLMODE_TDC_MASK);
 }
 
+/*
+ * can_get_relative_tdco() - TDCO relative to the sample point
+ *
+ * struct can_tdc::tdco represents the absolute offset from TDCV. Some
+ * controllers use instead an offset relative to the Sample Point (SP)
+ * such that:
+ *
+ * SSP = TDCV + absolute TDCO
+ *     = TDCV + SP + relative TDCO
+ *
+ * -+----------- one bit ----------+-- TX pin
+ *  |<--- Sample Point --->|
+ *
+ *                         --+----------- one bit ----------+-- RX pin
+ *  |<-------- TDCV -------->|
+ *                           |<------------------------>| absolute TDCO
+ *                           |<--- Sample Point --->|
+ *                           |                      |<->| relative TDCO
+ *  |<------------- Secondary Sample Point ------------>|
+ */
+static inline s32 can_get_relative_tdco(const struct can_priv *priv)
+{
+	const struct can_bittiming *dbt = &priv->data_bittiming;
+	s32 sample_point_in_tc = (CAN_SYNC_SEG + dbt->prop_seg +
+				  dbt->phase_seg1) * dbt->brp;
+
+	return (s32)priv->tdc.tdco - sample_point_in_tc;
+}
+
 /* helper to define static CAN controller features at device creation time */
 static inline void can_set_static_ctrlmode(struct net_device *dev,
 					   u32 static_mode)
Signature · 1 line
2.32.0