Skip to contentSkip to conversation

can: kvaser_usb: Add hardware timestamp support to all devices

PATCH Superseded Jimmy Assarsson 3 participants 23 messages Jun 28, 2024 – Jul 1, 2024 CAN network drivers

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover can: kvaser_usb: Add hardware timestamp support to all devices

This patch series add hardware timestamp support to all devices supported by the kvaser_usb driver.

The first patches resolves a known issue; "Hardware timestamps are not set for CAN Tx frames". I can't remember why this wasn't implemented in the first version of the hydra driver.

Followed by, hardware timestamp support for leaf and usbcan based devices.

The final patches are removing code used for selecting the correct ethtool and netdev ops.

Note: This patch series depends on patch
"can: kvaser_usb: Explicitly initialize family in leafimx..." [1].

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

Jimmy Assarsson (15):
can: kvaser_usb: hydra: kvaser_usb_hydra_ktime_from_rx_cmd: Drop {rx_}
in function name
can: kvaser_usb: hydra: Add struct for Tx ACK commands
can: kvaser_usb: hydra: Set hardware timestamp on transmitted packets
can: kvaser_usb: Add function kvaser_usb_ticks_to_ktime()
can: kvaser_usb: leaf: Add struct for Tx ACK commands
can: kvaser_usb: leaf: Assign correct timestamp_freq for

    kvaser_usb_leaf_imx_dev_cfg_{16,24,32}mhz
  can: kvaser_usb: leaf: Replace kvaser_usb_leaf_m32c_dev_cfg with
    kvaser_usb_leaf_m32c_dev_cfg_{16,24,32}mhz

can: kvaser_usb: leaf: kvaser_usb_leaf_tx_acknowledge: Rename local
variable
can: kvaser_usb: leaf: Add hardware timestamp support to leaf based
devices
can: kvaser_usb: leaf: Add structs for Tx ACK and clock overflow
commands
can: kvaser_usb: leaf: Store MSB of timestamp
can: kvaser_usb: leaf: Add hardware timestamp support to usbcan
devices
can: kvaser_usb: Remove KVASER_USB_QUIRK_HAS_HARDWARE_TIMESTAMP
can: kvaser_usb: Remove struct variables
kvaser_usb_{ethtool,netdev}_ops
can: kvaser_usb: Rename kvaser_usb_{ethtool,netdev}_ops_hwts to
kvaser_usb_{ethtool,netdev}_ops

 drivers/net/can/usb/kvaser_usb/kvaser_usb.h   |  10 +-
 .../net/can/usb/kvaser_usb/kvaser_usb_core.c  |  21 +---
 .../net/can/usb/kvaser_usb/kvaser_usb_hydra.c |  39 ++++--
 .../net/can/usb/kvaser_usb/kvaser_usb_leaf.c  | 113 +++++++++++++++---
 4 files changed, 137 insertions(+), 46 deletions(-)
Signature · 1 line
2.45.2
#1 lore raw

PATCH 1/15 can: kvaser_usb: hydra: kvaser_usb_hydra_ktime_from_rx_cmd: Drop {rx_} in function name

Rename function, since this function will be used for more than just the rx commands.

---
 drivers/net/can/usb/kvaser_usb/kvaser_usb_hydra.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/net/can/usb/kvaser_usb/kvaser_usb_hydra.c +4 -5
index c7ba768dfe17..a971fcb6158a 100644
@@ -522,9 +522,8 @@ kvaser_usb_hydra_net_priv_from_cmd(const struct kvaser_usb *dev,
 	return priv;
 }
 
-static ktime_t
-kvaser_usb_hydra_ktime_from_rx_cmd(const struct kvaser_usb_dev_cfg *cfg,
-				   const struct kvaser_cmd *cmd)
+static ktime_t kvaser_usb_hydra_ktime_from_cmd(const struct kvaser_usb_dev_cfg *cfg,
+					       const struct kvaser_cmd *cmd)
 {
 	u64 ticks;
 
@@ -1234,7 +1233,7 @@ static void kvaser_usb_hydra_rx_msg_std(const struct kvaser_usb *dev,
 	stats = &priv->netdev->stats;
 
 	flags = cmd->rx_can.flags;
-	hwtstamp = kvaser_usb_hydra_ktime_from_rx_cmd(dev->cfg, cmd);
+	hwtstamp = kvaser_usb_hydra_ktime_from_cmd(dev->cfg, cmd);
 
 	if (flags & KVASER_USB_HYDRA_CF_FLAG_ERROR_FRAME) {
 		kvaser_usb_hydra_error_frame(priv, &cmd->rx_can.err_frame_data,
@@ -1302,7 +1301,7 @@ static void kvaser_usb_hydra_rx_msg_ext(const struct kvaser_usb *dev,
 		KVASER_USB_KCAN_DATA_DLC_SHIFT;
 
 	flags = le32_to_cpu(cmd->rx_can.flags);
-	hwtstamp = kvaser_usb_hydra_ktime_from_rx_cmd(dev->cfg, std_cmd);
+	hwtstamp = kvaser_usb_hydra_ktime_from_cmd(dev->cfg, std_cmd);
 
 	if (flags & KVASER_USB_HYDRA_CF_FLAG_ERROR_FRAME) {
 		kvaser_usb_hydra_error_frame(priv, &cmd->rx_can.err_frame_data,
Signature · 1 line
2.45.2
#2 lore raw

PATCH 2/15 can: kvaser_usb: hydra: Add struct for Tx ACK commands

In reply to Jimmy Assarsson

Add, struct kvaser_cmd_tx_ack, for standard Tx ACK commands.

Expand kvaser_usb_hydra_ktime_from_cmd() to extract timestamps from both standard and extended Tx ACK commands. Unsupported commands are silently ignored, and 0 is returned.

---
 .../net/can/usb/kvaser_usb/kvaser_usb_hydra.c | 23 ++++++++++++++++---
 1 file changed, 20 insertions(+), 3 deletions(-)
drivers/net/can/usb/kvaser_usb/kvaser_usb_hydra.c +20 -3
index a971fcb6158a..0be1cfe8d964 100644
@@ -261,6 +261,15 @@ struct kvaser_cmd_tx_can {
 	u8 reserved[11];
 } __packed;
 
+struct kvaser_cmd_tx_ack {
+	__le32 id;
+	u8 data[8];
+	u8 dlc;
+	u8 flags;
+	__le16 timestamp[3];
+	u8 reserved0[8];
+} __packed;
+
 struct kvaser_cmd_header {
 	u8 cmd_no;
 	/* The destination HE address is stored in 0..5 of he_addr.
@@ -297,6 +306,7 @@ struct kvaser_cmd {
 
 		struct kvaser_cmd_rx_can rx_can;
 		struct kvaser_cmd_tx_can tx_can;
+		struct kvaser_cmd_tx_ack tx_ack;
 	} __packed;
 } __packed;
 
@@ -525,16 +535,23 @@ kvaser_usb_hydra_net_priv_from_cmd(const struct kvaser_usb *dev,
 static ktime_t kvaser_usb_hydra_ktime_from_cmd(const struct kvaser_usb_dev_cfg *cfg,
 					       const struct kvaser_cmd *cmd)
 {
-	u64 ticks;
+	u64 ticks = 0;
 
 	if (cmd->header.cmd_no == CMD_EXTENDED) {
 		struct kvaser_cmd_ext *cmd_ext = (struct kvaser_cmd_ext *)cmd;
 
-		ticks = le64_to_cpu(cmd_ext->rx_can.timestamp);
-	} else {
+		if (cmd_ext->cmd_no_ext == CMD_RX_MESSAGE_FD)
+			ticks = le64_to_cpu(cmd_ext->rx_can.timestamp);
+		else if (cmd_ext->cmd_no_ext == CMD_TX_ACKNOWLEDGE_FD)
+			ticks = le64_to_cpu(cmd_ext->tx_ack.timestamp);
+	} else if (cmd->header.cmd_no == CMD_RX_MESSAGE) {
 		ticks = le16_to_cpu(cmd->rx_can.timestamp[0]);
 		ticks += (u64)(le16_to_cpu(cmd->rx_can.timestamp[1])) << 16;
 		ticks += (u64)(le16_to_cpu(cmd->rx_can.timestamp[2])) << 32;
+	} else if (cmd->header.cmd_no == CMD_TX_ACKNOWLEDGE) {
+		ticks = le16_to_cpu(cmd->tx_ack.timestamp[0]);
+		ticks += (u64)(le16_to_cpu(cmd->tx_ack.timestamp[1])) << 16;
+		ticks += (u64)(le16_to_cpu(cmd->tx_ack.timestamp[2])) << 32;
 	}
 
 	return ns_to_ktime(div_u64(ticks * 1000, cfg->timestamp_freq));
Signature · 1 line
2.45.2
#3 lore raw

Re: [PATCH can-next 02/15] can: kvaser_usb: hydra: Add struct for Tx ACK commands

Quoted 53 lines · Jimmy Assarsson

Add, struct kvaser_cmd_tx_ack, for standard Tx ACK commands.

Expand kvaser_usb_hydra_ktime_from_cmd() to extract timestamps from both standard and extended Tx ACK commands. Unsupported commands are silently ignored, and 0 is returned.

---
 .../net/can/usb/kvaser_usb/kvaser_usb_hydra.c | 23 ++++++++++++++++---
 1 file changed, 20 insertions(+), 3 deletions(-)
drivers/net/can/usb/kvaser_usb/kvaser_usb_hydra.c +13 -3
index a971fcb6158a..0be1cfe8d964 100644
@@ -261,6 +261,15 @@ struct kvaser_cmd_tx_can {
        u8 reserved[11];
 } __packed;
 
+struct kvaser_cmd_tx_ack {
+       __le32 id;
+       u8 data[8];
+       u8 dlc;
+       u8 flags;
+       __le16 timestamp[3];
+       u8 reserved0[8];
+} __packed;
+
 struct kvaser_cmd_header {
        u8 cmd_no;
        /* The destination HE address is stored in 0..5 of he_addr.
@@ -297,6 +306,7 @@ struct kvaser_cmd {
 
                struct kvaser_cmd_rx_can rx_can;
                struct kvaser_cmd_tx_can tx_can;
+               struct kvaser_cmd_tx_ack tx_ack;
        } __packed;
 } __packed;
 
@@ -525,16 +535,23 @@ kvaser_usb_hydra_net_priv_from_cmd(const struct kvaser_usb *dev,
 static ktime_t kvaser_usb_hydra_ktime_from_cmd(const struct kvaser_usb_dev_cfg *cfg,
                                               const struct kvaser_cmd *cmd)
 {
-       u64 ticks;
+       u64 ticks = 0;
 
        if (cmd->header.cmd_no == CMD_EXTENDED) {
                struct kvaser_cmd_ext *cmd_ext = (struct kvaser_cmd_ext *)cmd;
 
-               ticks = le64_to_cpu(cmd_ext->rx_can.timestamp);
-       } else {
+               if (cmd_ext->cmd_no_ext == CMD_RX_MESSAGE_FD)
+                       ticks = le64_to_cpu(cmd_ext->rx_can.timestamp);

drivers/net/can/usb/kvaser_usb/kvaser_usb_hydra.c kvaser_usb_hydra_net_priv_from_cmd(const struct kvaser_usb *dev, · line 554

Quoted 10 lines
drivers/net/can/usb/kvaser_usb/kvaser_usb_hydra.c +7 -0
+               else if (cmd_ext->cmd_no_ext == CMD_TX_ACKNOWLEDGE_FD)
+                       ticks = le64_to_cpu(cmd_ext->tx_ack.timestamp);
+       } else if (cmd->header.cmd_no == CMD_RX_MESSAGE) {
                ticks = le16_to_cpu(cmd->rx_can.timestamp[0]);
                ticks += (u64)(le16_to_cpu(cmd->rx_can.timestamp[1])) << 16;
                ticks += (u64)(le16_to_cpu(cmd->rx_can.timestamp[2])) << 32;
+       } else if (cmd->header.cmd_no == CMD_TX_ACKNOWLEDGE) {
+               ticks = le16_to_cpu(cmd->tx_ack.timestamp[0]);
+               ticks += (u64)(le16_to_cpu(cmd->tx_ack.timestamp[1])) << 16;
+               ticks += (u64)(le16_to_cpu(cmd->tx_ack.timestamp[2])) << 32;

Nitpick: the conversion of teh timestamp[3] array to the u64 tick is now duplicated. Maybe worth adding a

  kvaser_usb_hydra_convert_timestamp_to_ktime(__le16 *timestamp, u64 ticks);

helper function to factorize this and the ns_to_ktime() all together?

If you do so, it is better to add the new patch before this one.

Quoted 7 lines
drivers/net/can/usb/kvaser_usb/kvaser_usb_hydra.c +0 -1
        }
 
        return ns_to_ktime(div_u64(ticks * 1000, cfg->timestamp_freq));
--

2.45.2

#4 lore raw

Re: [PATCH can-next 02/15] can: kvaser_usb: hydra: Add struct for Tx ACK commands

Quoted 68 lines · Vincent MAILHOL
Quoted 53 lines · Jimmy Assarsson

Add, struct kvaser_cmd_tx_ack, for standard Tx ACK commands.

Expand kvaser_usb_hydra_ktime_from_cmd() to extract timestamps from both standard and extended Tx ACK commands. Unsupported commands are silently ignored, and 0 is returned.

---
  .../net/can/usb/kvaser_usb/kvaser_usb_hydra.c | 23 ++++++++++++++++---
  1 file changed, 20 insertions(+), 3 deletions(-)
drivers/net/can/usb/kvaser_usb/kvaser_usb_hydra.c +13 -3
index a971fcb6158a..0be1cfe8d964 100644
@@ -261,6 +261,15 @@ struct kvaser_cmd_tx_can {
         u8 reserved[11];
  } __packed;
 
+struct kvaser_cmd_tx_ack {
+       __le32 id;
+       u8 data[8];
+       u8 dlc;
+       u8 flags;
+       __le16 timestamp[3];
+       u8 reserved0[8];
+} __packed;
+
  struct kvaser_cmd_header {
         u8 cmd_no;
         /* The destination HE address is stored in 0..5 of he_addr.
@@ -297,6 +306,7 @@ struct kvaser_cmd {
 
                 struct kvaser_cmd_rx_can rx_can;
                 struct kvaser_cmd_tx_can tx_can;
+               struct kvaser_cmd_tx_ack tx_ack;
         } __packed;
  } __packed;
 
@@ -525,16 +535,23 @@ kvaser_usb_hydra_net_priv_from_cmd(const struct kvaser_usb *dev,
  static ktime_t kvaser_usb_hydra_ktime_from_cmd(const struct kvaser_usb_dev_cfg *cfg,
                                                const struct kvaser_cmd *cmd)
  {
-       u64 ticks;
+       u64 ticks = 0;
 
         if (cmd->header.cmd_no == CMD_EXTENDED) {
                 struct kvaser_cmd_ext *cmd_ext = (struct kvaser_cmd_ext *)cmd;
 
-               ticks = le64_to_cpu(cmd_ext->rx_can.timestamp);
-       } else {
+               if (cmd_ext->cmd_no_ext == CMD_RX_MESSAGE_FD)
+                       ticks = le64_to_cpu(cmd_ext->rx_can.timestamp);

drivers/net/can/usb/kvaser_usb/kvaser_usb_hydra.c kvaser_usb_hydra_net_priv_from_cmd(const struct kvaser_usb *dev, · line 554

Quoted 10 lines
drivers/net/can/usb/kvaser_usb/kvaser_usb_hydra.c +7 -0
+               else if (cmd_ext->cmd_no_ext == CMD_TX_ACKNOWLEDGE_FD)
+                       ticks = le64_to_cpu(cmd_ext->tx_ack.timestamp);
+       } else if (cmd->header.cmd_no == CMD_RX_MESSAGE) {
                 ticks = le16_to_cpu(cmd->rx_can.timestamp[0]);
                 ticks += (u64)(le16_to_cpu(cmd->rx_can.timestamp[1])) << 16;
                 ticks += (u64)(le16_to_cpu(cmd->rx_can.timestamp[2])) << 32;
+       } else if (cmd->header.cmd_no == CMD_TX_ACKNOWLEDGE) {
+               ticks = le16_to_cpu(cmd->tx_ack.timestamp[0]);
+               ticks += (u64)(le16_to_cpu(cmd->tx_ack.timestamp[1])) << 16;
+               ticks += (u64)(le16_to_cpu(cmd->tx_ack.timestamp[2])) << 32;

Nitpick: the conversion of teh timestamp[3] array to the u64 tick is now duplicated. Maybe worth adding a

Quoted 5 lines
   kvaser_usb_hydra_convert_timestamp_to_ktime(__le16 *timestamp, u64 ticks);

helper function to factorize this and the ns_to_ktime() all together?

If you do so, it is better to add the new patch before this one.

Hi Vincent,

I'll fix this in v2. I've added two new helper functions kvaser_usb_timestamp{48,64}_to_ktime(), that will convert from 48-bit and 64-bit device timestamps to to ktime [1]

Thanks for the comments!

Regards,
jimmy

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

Quoted 8 lines
Quoted 8 lines
drivers/net/can/usb/kvaser_usb/kvaser_usb_hydra.c +0 -1
         }
 
         return ns_to_ktime(div_u64(ticks * 1000, cfg->timestamp_freq));
--

2.45.2

#5 lore raw

PATCH 3/15 can: kvaser_usb: hydra: Set hardware timestamp on transmitted packets

In reply to Jimmy Assarsson

Set hardware timestamp on transmitted packets.

---
 drivers/net/can/usb/kvaser_usb/kvaser_usb_hydra.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/net/can/usb/kvaser_usb/kvaser_usb_hydra.c +4 -1
index 0be1cfe8d964..28a99d78ba8d 100644
@@ -10,7 +10,6 @@
  *  - Transition from CAN_STATE_ERROR_WARNING to CAN_STATE_ERROR_ACTIVE is only
  *    reported after a call to do_get_berr_counter(), since firmware does not
  *    distinguish between ERROR_WARNING and ERROR_ACTIVE.
- *  - Hardware timestamps are not set for CAN Tx frames.
  */
 
 #include <linux/completion.h>
@@ -1191,6 +1190,7 @@ static void kvaser_usb_hydra_tx_acknowledge(const struct kvaser_usb *dev,
 	bool one_shot_fail = false;
 	bool is_err_frame = false;
 	u16 transid = kvaser_usb_hydra_get_cmd_transid(cmd);
+	struct sk_buff *skb;
 
 	priv = kvaser_usb_hydra_net_priv_from_cmd(dev, cmd);
 	if (!priv)
@@ -1217,6 +1217,9 @@ static void kvaser_usb_hydra_tx_acknowledge(const struct kvaser_usb *dev,
 
 	spin_lock_irqsave(&priv->tx_contexts_lock, irq_flags);
 
+	skb = priv->can.echo_skb[context->echo_index];
+	if (skb)
+		skb_hwtstamps(skb)->hwtstamp = kvaser_usb_hydra_ktime_from_cmd(dev->cfg, cmd);
 	len = can_get_echo_skb(priv->netdev, context->echo_index, NULL);
 	context->echo_index = dev->max_tx_urbs;
 	--priv->active_tx_contexts;
Signature · 1 line
2.45.2
#6 lore raw

PATCH 4/15 can: kvaser_usb: Add function kvaser_usb_ticks_to_ktime()

In reply to Jimmy Assarsson

Add function, kvaser_usb_ticks_to_ktime(), that converts from device ticks to ktime.

---
 drivers/net/can/usb/kvaser_usb/kvaser_usb.h       | 8 ++++++++
 drivers/net/can/usb/kvaser_usb/kvaser_usb_hydra.c | 2 +-
 2 files changed, 9 insertions(+), 1 deletion(-)
drivers/net/can/usb/kvaser_usb/kvaser_usb.h +8 -0
index ff10b3790d84..f5868e0b3b23 100644
@@ -22,6 +22,8 @@
  */
 
 #include <linux/completion.h>
+#include <linux/ktime.h>
+#include <linux/math64.h>
 #include <linux/spinlock.h>
 #include <linux/types.h>
 #include <linux/usb.h>
@@ -216,4 +218,10 @@ int kvaser_usb_can_rx_over_error(struct net_device *netdev);
 
 extern const struct can_bittiming_const kvaser_usb_flexc_bittiming_const;
 
+static inline ktime_t kvaser_usb_ticks_to_ktime(const struct kvaser_usb_dev_cfg *cfg,
+						u64 ticks)
+{
+	return ns_to_ktime(div_u64(ticks * 1000, cfg->timestamp_freq));
+}
+
 #endif /* KVASER_USB_H */
drivers/net/can/usb/kvaser_usb/kvaser_usb_hydra.c +1 -1
index 28a99d78ba8d..08688514d142 100644
@@ -553,7 +553,7 @@ static ktime_t kvaser_usb_hydra_ktime_from_cmd(const struct kvaser_usb_dev_cfg *
 		ticks += (u64)(le16_to_cpu(cmd->tx_ack.timestamp[2])) << 32;
 	}
 
-	return ns_to_ktime(div_u64(ticks * 1000, cfg->timestamp_freq));
+	return kvaser_usb_ticks_to_ktime(cfg, ticks);
 }
 
 static int kvaser_usb_hydra_send_simple_cmd(struct kvaser_usb *dev,
Signature · 1 line
2.45.2
#7 lore raw

Re: [PATCH can-next 04/15] can: kvaser_usb: Add function kvaser_usb_ticks_to_ktime()

Quoted 23 lines · Jimmy Assarsson

Add function, kvaser_usb_ticks_to_ktime(), that converts from device ticks to ktime.

---
 drivers/net/can/usb/kvaser_usb/kvaser_usb.h       | 8 ++++++++
 drivers/net/can/usb/kvaser_usb/kvaser_usb_hydra.c | 2 +-
 2 files changed, 9 insertions(+), 1 deletion(-)
drivers/net/can/usb/kvaser_usb/kvaser_usb.h +2 -0
index ff10b3790d84..f5868e0b3b23 100644
@@ -22,6 +22,8 @@
  */
 
 #include <linux/completion.h>
+#include <linux/ktime.h>
+#include <linux/math64.h>
 #include <linux/spinlock.h>
 #include <linux/types.h>
 #include <linux/usb.h>

drivers/net/can/usb/kvaser_usb/kvaser_usb.h int kvaser_usb_can_rx_over_error(struct net_device *netdev); · line 225

Quoted 8 lines
drivers/net/can/usb/kvaser_usb/kvaser_usb.h +5 -0
@@ -216,4 +218,10 @@ int kvaser_usb_can_rx_over_error(struct net_device *netdev);
 
 extern const struct can_bittiming_const kvaser_usb_flexc_bittiming_const;
 
+static inline ktime_t kvaser_usb_ticks_to_ktime(const struct kvaser_usb_dev_cfg *cfg,
+                                               u64 ticks)
+{
+       return ns_to_ktime(div_u64(ticks * 1000, cfg->timestamp_freq));
+}

See my comment on patch 2: also add the timestamp to tick conversion here.

Quoted 18 lines
drivers/net/can/usb/kvaser_usb/kvaser_usb.h +0 -0
 #endif /* KVASER_USB_H */
drivers/net/can/usb/kvaser_usb/kvaser_usb_hydra.c +1 -2
index 28a99d78ba8d..08688514d142 100644
@@ -553,7 +553,7 @@ static ktime_t kvaser_usb_hydra_ktime_from_cmd(const struct kvaser_usb_dev_cfg *
                ticks += (u64)(le16_to_cpu(cmd->tx_ack.timestamp[2])) << 32;
        }
 
-       return ns_to_ktime(div_u64(ticks * 1000, cfg->timestamp_freq));
+       return kvaser_usb_ticks_to_ktime(cfg, ticks);
 }
 
 static int kvaser_usb_hydra_send_simple_cmd(struct kvaser_usb *dev,
--

2.45.2

#8 lore raw

PATCH 5/15 can: kvaser_usb: leaf: Add struct for Tx ACK commands

In reply to Jimmy Assarsson

Add, struct leaf_cmd_tx_acknowledge, for Tx ACK commands received from leaf devices (M32C and leafimx28).

---
 drivers/net/can/usb/kvaser_usb/kvaser_usb_leaf.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/net/can/usb/kvaser_usb/kvaser_usb_leaf.c +9 -1
index 23bd7574b1c7..70511e151a3b 100644
@@ -235,6 +235,13 @@ struct kvaser_cmd_tx_acknowledge_header {
 	u8 tid;
 } __packed;
 
+struct leaf_cmd_tx_acknowledge {
+	u8 channel;
+	u8 tid;
+	__le16 time[3];
+	u8 padding[2];
+} __packed;
+
 struct leaf_cmd_can_error_event {
 	u8 tid;
 	u8 flags;
@@ -347,6 +354,7 @@ struct kvaser_cmd {
 			struct leaf_cmd_error_event error_event;
 			struct kvaser_cmd_cap_req cap_req;
 			struct kvaser_cmd_cap_res cap_res;
+			struct leaf_cmd_tx_acknowledge tx_ack;
 		} __packed leaf;
 
 		union {
@@ -370,7 +378,7 @@ static const u8 kvaser_usb_leaf_cmd_sizes_leaf[] = {
 	[CMD_START_CHIP_REPLY]		= kvaser_fsize(u.simple),
 	[CMD_STOP_CHIP_REPLY]		= kvaser_fsize(u.simple),
 	[CMD_GET_CARD_INFO_REPLY]	= kvaser_fsize(u.cardinfo),
-	[CMD_TX_ACKNOWLEDGE]		= kvaser_fsize(u.tx_acknowledge_header),
+	[CMD_TX_ACKNOWLEDGE]		= kvaser_fsize(u.leaf.tx_ack),
 	[CMD_GET_SOFTWARE_INFO_REPLY]	= kvaser_fsize(u.leaf.softinfo),
 	[CMD_RX_STD_MESSAGE]		= kvaser_fsize(u.leaf.rx_can),
 	[CMD_RX_EXT_MESSAGE]		= kvaser_fsize(u.leaf.rx_can),
Signature · 1 line
2.45.2
#9 lore raw

PATCH 6/15 can: kvaser_usb: leaf: Assign correct timestamp_freq for kvaser_usb_leaf_imx_dev_cfg_{16,24,32}mhz

In reply to Jimmy Assarsson

Assign correct timestamp_freq to kvaser_usb_leaf_imx_dev_cfg_{16,24,32}mhz. Since the driver didn't utilize the value, this didn't cause any problems.

---
 drivers/net/can/usb/kvaser_usb/kvaser_usb_leaf.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/net/can/usb/kvaser_usb/kvaser_usb_leaf.c +3 -3
index 70511e151a3b..00fe7410e36d 100644
@@ -483,7 +483,7 @@ static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_imx_dev_cfg_16mhz = {
 	.clock = {
 		.freq = 16 * MEGA /* Hz */,
 	},
-	.timestamp_freq = 1,
+	.timestamp_freq = 16,
 	.bittiming_const = &kvaser_usb_flexc_bittiming_const,
 };
 
@@ -491,7 +491,7 @@ static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_imx_dev_cfg_24mhz = {
 	.clock = {
 		.freq = 24 * MEGA /* Hz */,
 	},
-	.timestamp_freq = 1,
+	.timestamp_freq = 24,
 	.bittiming_const = &kvaser_usb_flexc_bittiming_const,
 };
 
@@ -499,7 +499,7 @@ static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_imx_dev_cfg_32mhz = {
 	.clock = {
 		.freq = 32 * MEGA /* Hz */,
 	},
-	.timestamp_freq = 1,
+	.timestamp_freq = 32,
 	.bittiming_const = &kvaser_usb_flexc_bittiming_const,
 };
 
Signature · 1 line
2.45.2
#10 lore raw

PATCH 7/15 can: kvaser_usb: leaf: Replace kvaser_usb_leaf_m32c_dev_cfg with kvaser_usb_leaf_m32c_dev_cfg_{16,24,32}mhz

In reply to Jimmy Assarsson

Add new struct kvaser_usb_dev_cfg constants,
kvaser_usb_leaf_m32c_dev_cfg_{16,24,32}mhz,
for M32C based leaf devices.

Note that the bittiming parameters are always calculated for 16MHz clock, while the timestamps are in the actual clock frequency of the device.

---
 .../net/can/usb/kvaser_usb/kvaser_usb_leaf.c  | 33 +++++++++++++++++--
 1 file changed, 30 insertions(+), 3 deletions(-)
drivers/net/can/usb/kvaser_usb/kvaser_usb_leaf.c +30 -3
index 00fe7410e36d..3245471e906b 100644
@@ -471,11 +471,27 @@ static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_usbcan_dev_cfg = {
 	.bittiming_const = &kvaser_usb_leaf_m16c_bittiming_const,
 };
 
-static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_m32c_dev_cfg = {
+static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_m32c_dev_cfg_16mhz = {
 	.clock = {
 		.freq = 16 * MEGA /* Hz */,
 	},
-	.timestamp_freq = 1,
+	.timestamp_freq = 16,
+	.bittiming_const = &kvaser_usb_leaf_m32c_bittiming_const,
+};
+
+static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_m32c_dev_cfg_24mhz = {
+	.clock = {
+		.freq = 16 * MEGA /* Hz */,
+	},
+	.timestamp_freq = 24,
+	.bittiming_const = &kvaser_usb_leaf_m32c_bittiming_const,
+};
+
+static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_m32c_dev_cfg_32mhz = {
+	.clock = {
+		.freq = 16 * MEGA /* Hz */,
+	},
+	.timestamp_freq = 32,
 	.bittiming_const = &kvaser_usb_leaf_m32c_bittiming_const,
 };
 
@@ -686,8 +702,19 @@ static void kvaser_usb_leaf_get_software_info_leaf(struct kvaser_usb *dev,
 	if (dev->driver_info->quirks & KVASER_USB_QUIRK_IGNORE_CLK_FREQ) {
 		/* Firmware expects bittiming parameters calculated for 16MHz
 		 * clock, regardless of the actual clock
+		 * Though, the reported freq is used for timestamps
 		 */
-		dev->cfg = &kvaser_usb_leaf_m32c_dev_cfg;
+		switch (sw_options & KVASER_USB_LEAF_SWOPTION_FREQ_MASK) {
+		case KVASER_USB_LEAF_SWOPTION_FREQ_16_MHZ_CLK:
+			dev->cfg = &kvaser_usb_leaf_m32c_dev_cfg_16mhz;
+			break;
+		case KVASER_USB_LEAF_SWOPTION_FREQ_24_MHZ_CLK:
+			dev->cfg = &kvaser_usb_leaf_m32c_dev_cfg_24mhz;
+			break;
+		case KVASER_USB_LEAF_SWOPTION_FREQ_32_MHZ_CLK:
+			dev->cfg = &kvaser_usb_leaf_m32c_dev_cfg_32mhz;
+			break;
+		}
 	} else {
 		switch (sw_options & KVASER_USB_LEAF_SWOPTION_FREQ_MASK) {
 		case KVASER_USB_LEAF_SWOPTION_FREQ_16_MHZ_CLK:
Signature · 1 line
2.45.2
#11 lore raw

PATCH 8/15 can: kvaser_usb: leaf: kvaser_usb_leaf_tx_acknowledge: Rename local variable

In reply to Jimmy Assarsson

Rename local variable skb to err_skb.

---
 drivers/net/can/usb/kvaser_usb/kvaser_usb_leaf.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/net/can/usb/kvaser_usb/kvaser_usb_leaf.c +4 -4
index 3245471e906b..caef1f26a95c 100644
@@ -936,14 +936,14 @@ static void kvaser_usb_leaf_tx_acknowledge(const struct kvaser_usb *dev,
 
 	/* Sometimes the state change doesn't come after a bus-off event */
 	if (priv->can.restart_ms && priv->can.state == CAN_STATE_BUS_OFF) {
-		struct sk_buff *skb;
+		struct sk_buff *err_skb;
 		struct can_frame *cf;
 
-		skb = alloc_can_err_skb(priv->netdev, &cf);
-		if (skb) {
+		err_skb = alloc_can_err_skb(priv->netdev, &cf);
+		if (err_skb) {
 			cf->can_id |= CAN_ERR_RESTARTED;
 
-			netif_rx(skb);
+			netif_rx(err_skb);
 		} else {
 			netdev_err(priv->netdev,
 				   "No memory left for err_skb\n");
Signature · 1 line
2.45.2
#12 lore raw

PATCH 9/15 can: kvaser_usb: leaf: Add hardware timestamp support to leaf based devices

In reply to Jimmy Assarsson

Add hardware timestamp support to leaf based devices (M32C and leafimx).

---
 .../net/can/usb/kvaser_usb/kvaser_usb_core.c  | 11 +++++++----
 .../net/can/usb/kvaser_usb/kvaser_usb_leaf.c  | 19 +++++++++++++++++++
 2 files changed, 26 insertions(+), 4 deletions(-)
drivers/net/can/usb/kvaser_usb/kvaser_usb_core.c +7 -4
index daa34b532aa8..b5d762d38d5d 100644
@@ -106,14 +106,16 @@ static const struct kvaser_usb_driver_info kvaser_usb_driver_info_usbcan = {
 };
 
 static const struct kvaser_usb_driver_info kvaser_usb_driver_info_leaf = {
-	.quirks = KVASER_USB_QUIRK_IGNORE_CLK_FREQ,
+	.quirks = KVASER_USB_QUIRK_IGNORE_CLK_FREQ |
+		  KVASER_USB_QUIRK_HAS_HARDWARE_TIMESTAMP,
 	.family = KVASER_LEAF,
 	.ops = &kvaser_usb_leaf_dev_ops,
 };
 
 static const struct kvaser_usb_driver_info kvaser_usb_driver_info_leaf_err = {
 	.quirks = KVASER_USB_QUIRK_HAS_TXRX_ERRORS |
-		  KVASER_USB_QUIRK_IGNORE_CLK_FREQ,
+		  KVASER_USB_QUIRK_IGNORE_CLK_FREQ |
+		  KVASER_USB_QUIRK_HAS_HARDWARE_TIMESTAMP,
 	.family = KVASER_LEAF,
 	.ops = &kvaser_usb_leaf_dev_ops,
 };
@@ -121,13 +123,14 @@ static const struct kvaser_usb_driver_info kvaser_usb_driver_info_leaf_err = {
 static const struct kvaser_usb_driver_info kvaser_usb_driver_info_leaf_err_listen = {
 	.quirks = KVASER_USB_QUIRK_HAS_TXRX_ERRORS |
 		  KVASER_USB_QUIRK_HAS_SILENT_MODE |
-		  KVASER_USB_QUIRK_IGNORE_CLK_FREQ,
+		  KVASER_USB_QUIRK_IGNORE_CLK_FREQ |
+		  KVASER_USB_QUIRK_HAS_HARDWARE_TIMESTAMP,
 	.family = KVASER_LEAF,
 	.ops = &kvaser_usb_leaf_dev_ops,
 };
 
 static const struct kvaser_usb_driver_info kvaser_usb_driver_info_leafimx = {
-	.quirks = 0,
+	.quirks = KVASER_USB_QUIRK_HAS_HARDWARE_TIMESTAMP,
 	.family = KVASER_LEAF,
 	.ops = &kvaser_usb_leaf_dev_ops,
 };
drivers/net/can/usb/kvaser_usb/kvaser_usb_leaf.c +19 -0
index caef1f26a95c..c0a8713d8cf2 100644
@@ -915,6 +915,8 @@ static void kvaser_usb_leaf_tx_acknowledge(const struct kvaser_usb *dev,
 	struct kvaser_usb_net_priv *priv;
 	unsigned long flags;
 	u8 channel, tid;
+	struct sk_buff *skb;
+	u64 ticks = 0;
 
 	channel = cmd->u.tx_acknowledge_header.channel;
 	tid = cmd->u.tx_acknowledge_header.tid;
@@ -954,9 +956,21 @@ static void kvaser_usb_leaf_tx_acknowledge(const struct kvaser_usb *dev,
 
 		priv->can.state = CAN_STATE_ERROR_ACTIVE;
 	}
+	switch (dev->driver_info->family) {
+	case KVASER_LEAF:
+		ticks = le16_to_cpu(cmd->u.leaf.tx_ack.time[0]);
+		ticks += (u64)(le16_to_cpu(cmd->u.leaf.tx_ack.time[1])) << 16;
+		ticks += (u64)(le16_to_cpu(cmd->u.leaf.tx_ack.time[2])) << 32;
+		break;
+	case KVASER_USBCAN:
+		break;
+	}
 
 	spin_lock_irqsave(&priv->tx_contexts_lock, flags);
 
+	skb = priv->can.echo_skb[context->echo_index];
+	if (skb)
+		skb_hwtstamps(skb)->hwtstamp = kvaser_usb_ticks_to_ktime(dev->cfg, ticks);
 	stats->tx_packets++;
 	stats->tx_bytes += can_get_echo_skb(priv->netdev,
 					    context->echo_index, NULL);
@@ -1334,6 +1348,7 @@ static void kvaser_usb_leaf_rx_can_msg(const struct kvaser_usb *dev,
 	struct net_device_stats *stats;
 	u8 channel = cmd->u.rx_can_header.channel;
 	const u8 *rx_data = NULL;	/* GCC */
+	u64 ticks = 0;
 
 	if (channel >= dev->nchannels) {
 		dev_err(&dev->intf->dev,
@@ -1364,6 +1379,9 @@ static void kvaser_usb_leaf_rx_can_msg(const struct kvaser_usb *dev,
 	switch (dev->driver_info->family) {
 	case KVASER_LEAF:
 		rx_data = cmd->u.leaf.rx_can.data;
+		ticks = le16_to_cpu(cmd->u.leaf.rx_can.time[0]);
+		ticks += (u64)(le16_to_cpu(cmd->u.leaf.rx_can.time[1])) << 16;
+		ticks += (u64)(le16_to_cpu(cmd->u.leaf.rx_can.time[2])) << 32;
 		break;
 	case KVASER_USBCAN:
 		rx_data = cmd->u.usbcan.rx_can.data;
@@ -1410,6 +1428,7 @@ static void kvaser_usb_leaf_rx_can_msg(const struct kvaser_usb *dev,
 			memcpy(cf->data, &rx_data[6], cf->len);
 	}
 
+	skb_hwtstamps(skb)->hwtstamp = kvaser_usb_ticks_to_ktime(dev->cfg, ticks);
 	stats->rx_packets++;
 	if (!(cf->can_id & CAN_RTR_FLAG))
 		stats->rx_bytes += cf->len;
Signature · 1 line
2.45.2
#13 lore raw

PATCH 10/15 can: kvaser_usb: leaf: Add structs for Tx ACK and clock overflow commands

In reply to Jimmy Assarsson

For usbcan devices (M16C), add struct usbcan_cmd_tx_acknowledge for Tx ACK commands and struct usbcan_cmd_clk_overflow_event for clock overflow event commands.

---
 .../net/can/usb/kvaser_usb/kvaser_usb_leaf.c  | 22 +++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)
drivers/net/can/usb/kvaser_usb/kvaser_usb_leaf.c +18 -4
index c0a8713d8cf2..98bd6cfca2ca 100644
@@ -242,6 +242,13 @@ struct leaf_cmd_tx_acknowledge {
 	u8 padding[2];
 } __packed;
 
+struct usbcan_cmd_tx_acknowledge {
+	u8 channel;
+	u8 tid;
+	__le16 time;
+	u8 padding[2];
+} __packed;
+
 struct leaf_cmd_can_error_event {
 	u8 tid;
 	u8 flags;
@@ -288,6 +295,12 @@ struct usbcan_cmd_error_event {
 	__le16 padding;
 } __packed;
 
+struct usbcan_cmd_clk_overflow_event {
+	u8 tid;
+	u8 padding;
+	__le32 time;
+} __packed;
+
 struct kvaser_cmd_ctrl_mode {
 	u8 tid;
 	u8 channel;
@@ -363,6 +376,8 @@ struct kvaser_cmd {
 			struct usbcan_cmd_chip_state_event chip_state_event;
 			struct usbcan_cmd_can_error_event can_error_event;
 			struct usbcan_cmd_error_event error_event;
+			struct usbcan_cmd_tx_acknowledge tx_ack;
+			struct usbcan_cmd_clk_overflow_event clk_overflow_event;
 		} __packed usbcan;
 
 		struct kvaser_cmd_tx_can tx_can;
@@ -396,15 +411,14 @@ static const u8 kvaser_usb_leaf_cmd_sizes_usbcan[] = {
 	[CMD_START_CHIP_REPLY]		= kvaser_fsize(u.simple),
 	[CMD_STOP_CHIP_REPLY]		= kvaser_fsize(u.simple),
 	[CMD_GET_CARD_INFO_REPLY]	= kvaser_fsize(u.cardinfo),
-	[CMD_TX_ACKNOWLEDGE]		= kvaser_fsize(u.tx_acknowledge_header),
+	[CMD_TX_ACKNOWLEDGE]		= kvaser_fsize(u.usbcan.tx_ack),
 	[CMD_GET_SOFTWARE_INFO_REPLY]	= kvaser_fsize(u.usbcan.softinfo),
 	[CMD_RX_STD_MESSAGE]		= kvaser_fsize(u.usbcan.rx_can),
 	[CMD_RX_EXT_MESSAGE]		= kvaser_fsize(u.usbcan.rx_can),
 	[CMD_CHIP_STATE_EVENT]		= kvaser_fsize(u.usbcan.chip_state_event),
 	[CMD_CAN_ERROR_EVENT]		= kvaser_fsize(u.usbcan.can_error_event),
 	[CMD_ERROR_EVENT]		= kvaser_fsize(u.usbcan.error_event),
-	/* ignored events: */
-	[CMD_USBCAN_CLOCK_OVERFLOW_EVENT] = CMD_SIZE_ANY,
+	[CMD_USBCAN_CLOCK_OVERFLOW_EVENT] = kvaser_fsize(u.usbcan.clk_overflow_event),
 };
 
 /* Summary of a kvaser error event, for a unified Leaf/Usbcan error
@@ -1608,12 +1622,12 @@ static void kvaser_usb_leaf_handle_command(const struct kvaser_usb *dev,
 		kvaser_usb_leaf_get_busparams_reply(dev, cmd);
 		break;
 
-	/* Ignored commands */
 	case CMD_USBCAN_CLOCK_OVERFLOW_EVENT:
 		if (dev->driver_info->family != KVASER_USBCAN)
 			goto warn;
 		break;
 
+	/* Ignored commands */
 	case CMD_FLUSH_QUEUE_REPLY:
 		if (dev->driver_info->family != KVASER_LEAF)
 			goto warn;
Signature · 1 line
2.45.2
#14 lore raw

Re: [PATCH can-next 10/15] can: kvaser_usb: leaf: Add structs for Tx ACK and clock overflow commands

Quoted 68 lines · Jimmy Assarsson

For usbcan devices (M16C), add struct usbcan_cmd_tx_acknowledge for Tx ACK commands and struct usbcan_cmd_clk_overflow_event for clock overflow event commands.

---
 .../net/can/usb/kvaser_usb/kvaser_usb_leaf.c  | 22 +++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)
drivers/net/can/usb/kvaser_usb/kvaser_usb_leaf.c +17 -3
index c0a8713d8cf2..98bd6cfca2ca 100644
@@ -242,6 +242,13 @@ struct leaf_cmd_tx_acknowledge {
        u8 padding[2];
 } __packed;
 
+struct usbcan_cmd_tx_acknowledge {
+       u8 channel;
+       u8 tid;
+       __le16 time;
+       u8 padding[2];
+} __packed;
+
 struct leaf_cmd_can_error_event {
        u8 tid;
        u8 flags;
@@ -288,6 +295,12 @@ struct usbcan_cmd_error_event {
        __le16 padding;
 } __packed;
 
+struct usbcan_cmd_clk_overflow_event {
+       u8 tid;
+       u8 padding;
+       __le32 time;
+} __packed;
+
 struct kvaser_cmd_ctrl_mode {
        u8 tid;
        u8 channel;
@@ -363,6 +376,8 @@ struct kvaser_cmd {
                        struct usbcan_cmd_chip_state_event chip_state_event;
                        struct usbcan_cmd_can_error_event can_error_event;
                        struct usbcan_cmd_error_event error_event;
+                       struct usbcan_cmd_tx_acknowledge tx_ack;
+                       struct usbcan_cmd_clk_overflow_event clk_overflow_event;
                } __packed usbcan;
 
                struct kvaser_cmd_tx_can tx_can;
@@ -396,15 +411,14 @@ static const u8 kvaser_usb_leaf_cmd_sizes_usbcan[] = {
        [CMD_START_CHIP_REPLY]          = kvaser_fsize(u.simple),
        [CMD_STOP_CHIP_REPLY]           = kvaser_fsize(u.simple),
        [CMD_GET_CARD_INFO_REPLY]       = kvaser_fsize(u.cardinfo),
-       [CMD_TX_ACKNOWLEDGE]            = kvaser_fsize(u.tx_acknowledge_header),
+       [CMD_TX_ACKNOWLEDGE]            = kvaser_fsize(u.usbcan.tx_ack),
        [CMD_GET_SOFTWARE_INFO_REPLY]   = kvaser_fsize(u.usbcan.softinfo),
        [CMD_RX_STD_MESSAGE]            = kvaser_fsize(u.usbcan.rx_can),
        [CMD_RX_EXT_MESSAGE]            = kvaser_fsize(u.usbcan.rx_can),
        [CMD_CHIP_STATE_EVENT]          = kvaser_fsize(u.usbcan.chip_state_event),
        [CMD_CAN_ERROR_EVENT]           = kvaser_fsize(u.usbcan.can_error_event),
        [CMD_ERROR_EVENT]               = kvaser_fsize(u.usbcan.error_event),
-       /* ignored events: */
-       [CMD_USBCAN_CLOCK_OVERFLOW_EVENT] = CMD_SIZE_ANY,
+       [CMD_USBCAN_CLOCK_OVERFLOW_EVENT] = kvaser_fsize(u.usbcan.clk_overflow_event),
 };
 
 /* Summary of a kvaser error event, for a unified Leaf/Usbcan error

drivers/net/can/usb/kvaser_usb/kvaser_usb_leaf.c static void kvaser_usb_leaf_handle_command(const struct kvaser_usb *dev, · line 1628

Quoted 8 lines
drivers/net/can/usb/kvaser_usb/kvaser_usb_leaf.c +0 -1
@@ -1608,12 +1622,12 @@ static void kvaser_usb_leaf_handle_command(const struct kvaser_usb *dev,
                kvaser_usb_leaf_get_busparams_reply(dev, cmd);
                break;
 
-       /* Ignored commands */
        case CMD_USBCAN_CLOCK_OVERFLOW_EVENT:
                if (dev->driver_info->family != KVASER_USBCAN)
                        goto warn;
                break;

I did not understand this part. If the overflow event is now handled, why do you still have the

goto warn

?

Shouldn't you dispatch the command here?

Quoted 8 lines
drivers/net/can/usb/kvaser_usb/kvaser_usb_leaf.c +1 -1
+       /* Ignored commands */
        case CMD_FLUSH_QUEUE_REPLY:
                if (dev->driver_info->family != KVASER_LEAF)
                        goto warn;
--

2.45.2

#15 lore raw

Re: [PATCH can-next 10/15] can: kvaser_usb: leaf: Add structs for Tx ACK and clock overflow commands

Quoted 81 lines · Vincent MAILHOL
Quoted 68 lines · Jimmy Assarsson

For usbcan devices (M16C), add struct usbcan_cmd_tx_acknowledge for Tx ACK commands and struct usbcan_cmd_clk_overflow_event for clock overflow event commands.

---
  .../net/can/usb/kvaser_usb/kvaser_usb_leaf.c  | 22 +++++++++++++++----
  1 file changed, 18 insertions(+), 4 deletions(-)
drivers/net/can/usb/kvaser_usb/kvaser_usb_leaf.c +17 -3
index c0a8713d8cf2..98bd6cfca2ca 100644
@@ -242,6 +242,13 @@ struct leaf_cmd_tx_acknowledge {
         u8 padding[2];
  } __packed;
 
+struct usbcan_cmd_tx_acknowledge {
+       u8 channel;
+       u8 tid;
+       __le16 time;
+       u8 padding[2];
+} __packed;
+
  struct leaf_cmd_can_error_event {
         u8 tid;
         u8 flags;
@@ -288,6 +295,12 @@ struct usbcan_cmd_error_event {
         __le16 padding;
  } __packed;
 
+struct usbcan_cmd_clk_overflow_event {
+       u8 tid;
+       u8 padding;
+       __le32 time;
+} __packed;
+
  struct kvaser_cmd_ctrl_mode {
         u8 tid;
         u8 channel;
@@ -363,6 +376,8 @@ struct kvaser_cmd {
                         struct usbcan_cmd_chip_state_event chip_state_event;
                         struct usbcan_cmd_can_error_event can_error_event;
                         struct usbcan_cmd_error_event error_event;
+                       struct usbcan_cmd_tx_acknowledge tx_ack;
+                       struct usbcan_cmd_clk_overflow_event clk_overflow_event;
                 } __packed usbcan;
 
                 struct kvaser_cmd_tx_can tx_can;
@@ -396,15 +411,14 @@ static const u8 kvaser_usb_leaf_cmd_sizes_usbcan[] = {
         [CMD_START_CHIP_REPLY]          = kvaser_fsize(u.simple),
         [CMD_STOP_CHIP_REPLY]           = kvaser_fsize(u.simple),
         [CMD_GET_CARD_INFO_REPLY]       = kvaser_fsize(u.cardinfo),
-       [CMD_TX_ACKNOWLEDGE]            = kvaser_fsize(u.tx_acknowledge_header),
+       [CMD_TX_ACKNOWLEDGE]            = kvaser_fsize(u.usbcan.tx_ack),
         [CMD_GET_SOFTWARE_INFO_REPLY]   = kvaser_fsize(u.usbcan.softinfo),
         [CMD_RX_STD_MESSAGE]            = kvaser_fsize(u.usbcan.rx_can),
         [CMD_RX_EXT_MESSAGE]            = kvaser_fsize(u.usbcan.rx_can),
         [CMD_CHIP_STATE_EVENT]          = kvaser_fsize(u.usbcan.chip_state_event),
         [CMD_CAN_ERROR_EVENT]           = kvaser_fsize(u.usbcan.can_error_event),
         [CMD_ERROR_EVENT]               = kvaser_fsize(u.usbcan.error_event),
-       /* ignored events: */
-       [CMD_USBCAN_CLOCK_OVERFLOW_EVENT] = CMD_SIZE_ANY,
+       [CMD_USBCAN_CLOCK_OVERFLOW_EVENT] = kvaser_fsize(u.usbcan.clk_overflow_event),
  };
 
  /* Summary of a kvaser error event, for a unified Leaf/Usbcan error

drivers/net/can/usb/kvaser_usb/kvaser_usb_leaf.c static void kvaser_usb_leaf_handle_command(const struct kvaser_usb *dev, · line 1628

Quoted 8 lines
drivers/net/can/usb/kvaser_usb/kvaser_usb_leaf.c +0 -1
@@ -1608,12 +1622,12 @@ static void kvaser_usb_leaf_handle_command(const struct kvaser_usb *dev,
                 kvaser_usb_leaf_get_busparams_reply(dev, cmd);
                 break;
 
-       /* Ignored commands */
         case CMD_USBCAN_CLOCK_OVERFLOW_EVENT:
                 if (dev->driver_info->family != KVASER_USBCAN)
                         goto warn;
                 break;

I did not understand this part. If the overflow event is now handled, why do you still have the

Quoted 3 lines

goto warn

?

The warning is for non-usbcan device (leaf), where this command is unexpected.

Quoted 1 line

Shouldn't you dispatch the command here?

It's dispatched in the succeeding patch
can: kvaser_usb: leaf: Store MSB of timestamp [1]
I'll fix this in v2. I keep the "ignored events/commands" comments in this patch, and move them to the proper patch.

Regards,
/jimmy

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

Quoted 8 lines
Quoted 8 lines
drivers/net/can/usb/kvaser_usb/kvaser_usb_leaf.c +1 -1
+       /* Ignored commands */
         case CMD_FLUSH_QUEUE_REPLY:
                 if (dev->driver_info->family != KVASER_LEAF)
                         goto warn;
--

2.45.2

#16 lore raw

PATCH 11/15 can: kvaser_usb: leaf: Store MSB of timestamp

In reply to Jimmy Assarsson

Store MSB of timestamp, provided from the device via the clock overflow event, for usbcan devices (M16C).

---
 drivers/net/can/usb/kvaser_usb/kvaser_usb.h      | 1 +
 drivers/net/can/usb/kvaser_usb/kvaser_usb_leaf.c | 8 +++++++-
 2 files changed, 8 insertions(+), 1 deletion(-)
drivers/net/can/usb/kvaser_usb/kvaser_usb.h +1 -0
index f5868e0b3b23..5552140c0869 100644
@@ -70,6 +70,7 @@ struct kvaser_usb_dev_card_data {
 	u32 ctrlmode_supported;
 	u32 capabilities;
 	struct kvaser_usb_dev_card_data_hydra hydra;
+	u32 usbcan_timestamp_msb;
 };
 
 /* Context for an outstanding, not yet ACKed, transmission */
drivers/net/can/usb/kvaser_usb/kvaser_usb_leaf.c +7 -1
index 98bd6cfca2ca..9b75f240485d 100644
@@ -119,6 +119,9 @@
 /* Extended CAN identifier flag */
 #define KVASER_EXTENDED_FRAME		BIT(31)
 
+/* USBCanII timestamp */
+#define KVASER_USB_USBCAN_CLK_OVERFLOW_MASK GENMASK(31, 16)
+
 struct kvaser_cmd_simple {
 	u8 tid;
 	u8 channel;
@@ -1576,7 +1579,7 @@ static void kvaser_usb_leaf_get_busparams_reply(const struct kvaser_usb *dev,
 	complete(&priv->get_busparams_comp);
 }
 
-static void kvaser_usb_leaf_handle_command(const struct kvaser_usb *dev,
+static void kvaser_usb_leaf_handle_command(struct kvaser_usb *dev,
 					   const struct kvaser_cmd *cmd)
 {
 	if (kvaser_usb_leaf_verify_size(dev, cmd) < 0)
@@ -1625,6 +1628,9 @@ static void kvaser_usb_leaf_handle_command(const struct kvaser_usb *dev,
 	case CMD_USBCAN_CLOCK_OVERFLOW_EVENT:
 		if (dev->driver_info->family != KVASER_USBCAN)
 			goto warn;
+		dev->card_data.usbcan_timestamp_msb =
+					le32_to_cpu(cmd->u.usbcan.clk_overflow_event.time) &
+					KVASER_USB_USBCAN_CLK_OVERFLOW_MASK;
 		break;
 
 	/* Ignored commands */
Signature · 1 line
2.45.2
#17 lore raw

PATCH 12/15 can: kvaser_usb: leaf: Add hardware timestamp support to usbcan devices

In reply to Jimmy Assarsson

Add hardware timestamp support for all usbcan based devices (M16C). The usbcan firmware is slightly different compared to the other Kvaser USB interfaces:
- The timestamp is provided by a 32-bit counter, with 10us resolution. Hence, the hardware timestamp will wrap after less than 12 hours.
- Each Rx CAN or Tx ACK command only contains the 16-bits LSB of the timestamp counter.
- The 16-bits MSB are sent in an asynchronous event (command), if any change occurred in the MSB since the last event.

---
 drivers/net/can/usb/kvaser_usb/kvaser_usb_core.c | 3 ++-
 drivers/net/can/usb/kvaser_usb/kvaser_usb_leaf.c | 7 +++++++
 2 files changed, 9 insertions(+), 1 deletion(-)
drivers/net/can/usb/kvaser_usb/kvaser_usb_core.c +2 -1
index b5d762d38d5d..576ddf932f47 100644
@@ -100,7 +100,8 @@ static const struct kvaser_usb_driver_info kvaser_usb_driver_info_hydra = {
 
 static const struct kvaser_usb_driver_info kvaser_usb_driver_info_usbcan = {
 	.quirks = KVASER_USB_QUIRK_HAS_TXRX_ERRORS |
-		  KVASER_USB_QUIRK_HAS_SILENT_MODE,
+		  KVASER_USB_QUIRK_HAS_SILENT_MODE |
+		  KVASER_USB_QUIRK_HAS_HARDWARE_TIMESTAMP,
 	.family = KVASER_USBCAN,
 	.ops = &kvaser_usb_leaf_dev_ops,
 };
drivers/net/can/usb/kvaser_usb/kvaser_usb_leaf.c +7 -0
index 9b75f240485d..26227a4ed580 100644
@@ -121,6 +121,7 @@
 
 /* USBCanII timestamp */
 #define KVASER_USB_USBCAN_CLK_OVERFLOW_MASK GENMASK(31, 16)
+#define KVASER_USB_USBCAN_TIMESTAMP_FACTOR 10
 
 struct kvaser_cmd_simple {
 	u8 tid;
@@ -980,6 +981,9 @@ static void kvaser_usb_leaf_tx_acknowledge(const struct kvaser_usb *dev,
 		ticks += (u64)(le16_to_cpu(cmd->u.leaf.tx_ack.time[2])) << 32;
 		break;
 	case KVASER_USBCAN:
+		ticks = le16_to_cpu(cmd->u.usbcan.tx_ack.time);
+		ticks += dev->card_data.usbcan_timestamp_msb;
+		ticks = ticks * KVASER_USB_USBCAN_TIMESTAMP_FACTOR;
 		break;
 	}
 
@@ -1402,6 +1406,9 @@ static void kvaser_usb_leaf_rx_can_msg(const struct kvaser_usb *dev,
 		break;
 	case KVASER_USBCAN:
 		rx_data = cmd->u.usbcan.rx_can.data;
+		ticks = le16_to_cpu(cmd->u.usbcan.rx_can.time);
+		ticks += dev->card_data.usbcan_timestamp_msb;
+		ticks = ticks * KVASER_USB_USBCAN_TIMESTAMP_FACTOR;
 		break;
 	}
 
Signature · 1 line
2.45.2
#18 lore raw

PATCH 13/15 can: kvaser_usb: Remove KVASER_USB_QUIRK_HAS_HARDWARE_TIMESTAMP

In reply to Jimmy Assarsson

Remove KVASER_USB_QUIRK_HAS_HARDWARE_TIMESTAMP, since all devices got hardware timestamp support.

---
 drivers/net/can/usb/kvaser_usb/kvaser_usb.h   |  1 -
 .../net/can/usb/kvaser_usb/kvaser_usb_core.c  | 26 ++++++-------------
 2 files changed, 8 insertions(+), 19 deletions(-)
drivers/net/can/usb/kvaser_usb/kvaser_usb.h +0 -1
index 5552140c0869..9fa02d8f49f9 100644
@@ -41,7 +41,6 @@
 #define KVASER_USB_QUIRK_HAS_SILENT_MODE	BIT(0)
 #define KVASER_USB_QUIRK_HAS_TXRX_ERRORS	BIT(1)
 #define KVASER_USB_QUIRK_IGNORE_CLK_FREQ	BIT(2)
-#define KVASER_USB_QUIRK_HAS_HARDWARE_TIMESTAMP	BIT(3)
 
 /* Device capabilities */
 #define KVASER_USB_CAP_BERR_CAP			0x01
drivers/net/can/usb/kvaser_usb/kvaser_usb_core.c +8 -18
index 576ddf932f47..a4f32d57173a 100644
@@ -94,29 +94,26 @@
 #define USB_MINI_PCIE_1XCAN_PRODUCT_ID 0x011B
 
 static const struct kvaser_usb_driver_info kvaser_usb_driver_info_hydra = {
-	.quirks = KVASER_USB_QUIRK_HAS_HARDWARE_TIMESTAMP,
+	.quirks = 0,
 	.ops = &kvaser_usb_hydra_dev_ops,
 };
 
 static const struct kvaser_usb_driver_info kvaser_usb_driver_info_usbcan = {
 	.quirks = KVASER_USB_QUIRK_HAS_TXRX_ERRORS |
-		  KVASER_USB_QUIRK_HAS_SILENT_MODE |
-		  KVASER_USB_QUIRK_HAS_HARDWARE_TIMESTAMP,
+		  KVASER_USB_QUIRK_HAS_SILENT_MODE,
 	.family = KVASER_USBCAN,
 	.ops = &kvaser_usb_leaf_dev_ops,
 };
 
 static const struct kvaser_usb_driver_info kvaser_usb_driver_info_leaf = {
-	.quirks = KVASER_USB_QUIRK_IGNORE_CLK_FREQ |
-		  KVASER_USB_QUIRK_HAS_HARDWARE_TIMESTAMP,
+	.quirks = KVASER_USB_QUIRK_IGNORE_CLK_FREQ,
 	.family = KVASER_LEAF,
 	.ops = &kvaser_usb_leaf_dev_ops,
 };
 
 static const struct kvaser_usb_driver_info kvaser_usb_driver_info_leaf_err = {
 	.quirks = KVASER_USB_QUIRK_HAS_TXRX_ERRORS |
-		  KVASER_USB_QUIRK_IGNORE_CLK_FREQ |
-		  KVASER_USB_QUIRK_HAS_HARDWARE_TIMESTAMP,
+		  KVASER_USB_QUIRK_IGNORE_CLK_FREQ,
 	.family = KVASER_LEAF,
 	.ops = &kvaser_usb_leaf_dev_ops,
 };
@@ -124,14 +121,13 @@ static const struct kvaser_usb_driver_info kvaser_usb_driver_info_leaf_err = {
 static const struct kvaser_usb_driver_info kvaser_usb_driver_info_leaf_err_listen = {
 	.quirks = KVASER_USB_QUIRK_HAS_TXRX_ERRORS |
 		  KVASER_USB_QUIRK_HAS_SILENT_MODE |
-		  KVASER_USB_QUIRK_IGNORE_CLK_FREQ |
-		  KVASER_USB_QUIRK_HAS_HARDWARE_TIMESTAMP,
+		  KVASER_USB_QUIRK_IGNORE_CLK_FREQ,
 	.family = KVASER_LEAF,
 	.ops = &kvaser_usb_leaf_dev_ops,
 };
 
 static const struct kvaser_usb_driver_info kvaser_usb_driver_info_leafimx = {
-	.quirks = KVASER_USB_QUIRK_HAS_HARDWARE_TIMESTAMP,
+	.quirks = 0,
 	.family = KVASER_LEAF,
 	.ops = &kvaser_usb_leaf_dev_ops,
 };
@@ -862,14 +858,8 @@ static int kvaser_usb_init_one(struct kvaser_usb *dev, int channel)
 
 	netdev->flags |= IFF_ECHO;
 
-	netdev->netdev_ops = &kvaser_usb_netdev_ops;
-	if (driver_info->quirks & KVASER_USB_QUIRK_HAS_HARDWARE_TIMESTAMP) {
-		netdev->netdev_ops = &kvaser_usb_netdev_ops_hwts;
-		netdev->ethtool_ops = &kvaser_usb_ethtool_ops_hwts;
-	} else {
-		netdev->netdev_ops = &kvaser_usb_netdev_ops;
-		netdev->ethtool_ops = &kvaser_usb_ethtool_ops;
-	}
+	netdev->netdev_ops = &kvaser_usb_netdev_ops_hwts;
+	netdev->ethtool_ops = &kvaser_usb_ethtool_ops_hwts;
 	SET_NETDEV_DEV(netdev, &dev->intf->dev);
 	netdev->dev_id = channel;
 
Signature · 1 line
2.45.2
#19 lore raw

Re: [PATCH can-next 13/15] can: kvaser_usb: Remove KVASER_USB_QUIRK_HAS_HARDWARE_TIMESTAMP

Quoted 27 lines · Jimmy Assarsson

Remove KVASER_USB_QUIRK_HAS_HARDWARE_TIMESTAMP, since all devices got hardware timestamp support.

---
 drivers/net/can/usb/kvaser_usb/kvaser_usb.h   |  1 -
 .../net/can/usb/kvaser_usb/kvaser_usb_core.c  | 26 ++++++-------------
 2 files changed, 8 insertions(+), 19 deletions(-)
drivers/net/can/usb/kvaser_usb/kvaser_usb.h +0 -1
index 5552140c0869..9fa02d8f49f9 100644
@@ -41,7 +41,6 @@
 #define KVASER_USB_QUIRK_HAS_SILENT_MODE       BIT(0)
 #define KVASER_USB_QUIRK_HAS_TXRX_ERRORS       BIT(1)
 #define KVASER_USB_QUIRK_IGNORE_CLK_FREQ       BIT(2)
-#define KVASER_USB_QUIRK_HAS_HARDWARE_TIMESTAMP        BIT(3)
 
 /* Device capabilities */
 #define KVASER_USB_CAP_BERR_CAP                        0x01
drivers/net/can/usb/kvaser_usb/kvaser_usb_core.c +0 -0
index 576ddf932f47..a4f32d57173a 100644
@@ -94,29 +94,26 @@
 #define USB_MINI_PCIE_1XCAN_PRODUCT_ID 0x011B

(...)

drivers/net/can/usb/kvaser_usb/kvaser_usb_core.c static int kvaser_usb_init_one(struct kvaser_usb *dev, int channel) · line 865

Quoted 11 lines

static const struct kvaser_usb_driver_info kvaser_usb_driver_info_leafimx = {

drivers/net/can/usb/kvaser_usb/kvaser_usb_core.c +1 -2
-       .quirks = KVASER_USB_QUIRK_HAS_HARDWARE_TIMESTAMP,
+       .quirks = 0,
        .family = KVASER_LEAF,
        .ops = &kvaser_usb_leaf_dev_ops,
 };
@@ -862,14 +858,8 @@ static int kvaser_usb_init_one(struct kvaser_usb *dev, int channel)
 
        netdev->flags |= IFF_ECHO;
 
-       netdev->netdev_ops = &kvaser_usb_netdev_ops;

Ah, I missed this part. You can probably ignore this comment which I made on patch 14:

https://lore.kernel.org/linux-can/CAMZ6RqJ8dyc62Hj+ACV99BSuX3g3ARGCfY4HHUtiKmpmmotDJw@mail.gmail.com/

Quoted 16 lines
drivers/net/can/usb/kvaser_usb/kvaser_usb_core.c +2 -8
-       if (driver_info->quirks & KVASER_USB_QUIRK_HAS_HARDWARE_TIMESTAMP) {
-               netdev->netdev_ops = &kvaser_usb_netdev_ops_hwts;
-               netdev->ethtool_ops = &kvaser_usb_ethtool_ops_hwts;
-       } else {
-               netdev->netdev_ops = &kvaser_usb_netdev_ops;
-               netdev->ethtool_ops = &kvaser_usb_ethtool_ops;
-       }
+       netdev->netdev_ops = &kvaser_usb_netdev_ops_hwts;
+       netdev->ethtool_ops = &kvaser_usb_ethtool_ops_hwts;
        SET_NETDEV_DEV(netdev, &dev->intf->dev);
        netdev->dev_id = channel;
 
--

2.45.2

#20 lore raw

PATCH 14/15 can: kvaser_usb: Remove struct variables kvaser_usb_{ethtool,netdev}_ops

In reply to Jimmy Assarsson

Remove no longer used struct variables, kvaser_usb_ethtool_ops and kvaser_usb_netdev_ops.

---
 drivers/net/can/usb/kvaser_usb/kvaser_usb_core.c | 11 -----------
 1 file changed, 11 deletions(-)
drivers/net/can/usb/kvaser_usb/kvaser_usb_core.c +0 -11
index a4f32d57173a..4b6c23121b5d 100644
@@ -753,13 +753,6 @@ static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb,
 	return ret;
 }
 
-static const struct net_device_ops kvaser_usb_netdev_ops = {
-	.ndo_open = kvaser_usb_open,
-	.ndo_stop = kvaser_usb_close,
-	.ndo_start_xmit = kvaser_usb_start_xmit,
-	.ndo_change_mtu = can_change_mtu,
-};
-
 static const struct net_device_ops kvaser_usb_netdev_ops_hwts = {
 	.ndo_open = kvaser_usb_open,
 	.ndo_stop = kvaser_usb_close,
@@ -768,10 +761,6 @@ static const struct net_device_ops kvaser_usb_netdev_ops_hwts = {
 	.ndo_change_mtu = can_change_mtu,
 };
 
-static const struct ethtool_ops kvaser_usb_ethtool_ops = {
-	.get_ts_info = ethtool_op_get_ts_info,
-};
-
 static const struct ethtool_ops kvaser_usb_ethtool_ops_hwts = {
 	.get_ts_info = can_ethtool_op_get_ts_info_hwts,
 };
Signature · 1 line
2.45.2
#21 lore raw

Re: [PATCH can-next 14/15] can: kvaser_usb: Remove struct variables kvaser_usb_{ethtool,netdev}_ops

Quoted 2 lines · Jimmy Assarsson

Remove no longer used struct variables, kvaser_usb_ethtool_ops and kvaser_usb_netdev_ops.

Does the driver still compile after this patch? Wouldn't it be better to squash patch 14 and 15 in one?

Quoted 38 lines
---
 drivers/net/can/usb/kvaser_usb/kvaser_usb_core.c | 11 -----------
 1 file changed, 11 deletions(-)
drivers/net/can/usb/kvaser_usb/kvaser_usb_core.c +0 -12
index a4f32d57173a..4b6c23121b5d 100644
@@ -753,13 +753,6 @@ static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb,
        return ret;
 }
 
-static const struct net_device_ops kvaser_usb_netdev_ops = {
-       .ndo_open = kvaser_usb_open,
-       .ndo_stop = kvaser_usb_close,
-       .ndo_start_xmit = kvaser_usb_start_xmit,
-       .ndo_change_mtu = can_change_mtu,
-};
-
 static const struct net_device_ops kvaser_usb_netdev_ops_hwts = {
        .ndo_open = kvaser_usb_open,
        .ndo_stop = kvaser_usb_close,
@@ -768,10 +761,6 @@ static const struct net_device_ops kvaser_usb_netdev_ops_hwts = {
        .ndo_change_mtu = can_change_mtu,
 };
 
-static const struct ethtool_ops kvaser_usb_ethtool_ops = {
-       .get_ts_info = ethtool_op_get_ts_info,
-};
-
 static const struct ethtool_ops kvaser_usb_ethtool_ops_hwts = {
        .get_ts_info = can_ethtool_op_get_ts_info_hwts,
 };
--

2.45.2

#22 lore raw

PATCH 15/15 can: kvaser_usb: Rename kvaser_usb_{ethtool,netdev}_ops_hwts to kvaser_usb_{ethtool,netdev}_ops

In reply to Jimmy Assarsson

Now when we only got one set of ethtool_ops and netdev_ops, remove the "hwts" suffix from the struct variables kvaser_usb_{ethtool,netdev}_ops_hwts.

---
 drivers/net/can/usb/kvaser_usb/kvaser_usb_core.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/net/can/usb/kvaser_usb/kvaser_usb_core.c +4 -4
index 4b6c23121b5d..35b4132b0639 100644
@@ -753,7 +753,7 @@ static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb,
 	return ret;
 }
 
-static const struct net_device_ops kvaser_usb_netdev_ops_hwts = {
+static const struct net_device_ops kvaser_usb_netdev_ops = {
 	.ndo_open = kvaser_usb_open,
 	.ndo_stop = kvaser_usb_close,
 	.ndo_eth_ioctl = can_eth_ioctl_hwts,
@@ -761,7 +761,7 @@ static const struct net_device_ops kvaser_usb_netdev_ops_hwts = {
 	.ndo_change_mtu = can_change_mtu,
 };
 
-static const struct ethtool_ops kvaser_usb_ethtool_ops_hwts = {
+static const struct ethtool_ops kvaser_usb_ethtool_ops = {
 	.get_ts_info = can_ethtool_op_get_ts_info_hwts,
 };
 
@@ -847,8 +847,8 @@ static int kvaser_usb_init_one(struct kvaser_usb *dev, int channel)
 
 	netdev->flags |= IFF_ECHO;
 
-	netdev->netdev_ops = &kvaser_usb_netdev_ops_hwts;
-	netdev->ethtool_ops = &kvaser_usb_ethtool_ops_hwts;
+	netdev->netdev_ops = &kvaser_usb_netdev_ops;
+	netdev->ethtool_ops = &kvaser_usb_ethtool_ops;
 	SET_NETDEV_DEV(netdev, &dev->intf->dev);
 	netdev->dev_id = channel;
 
Signature · 1 line
2.45.2