2022-12-09 07:29:10

by Arun Ramadoss

[permalink] [raw]
Subject: [Patch net-next v3 00/13] net: dsa: microchip: add PTP support for KSZ9563/KSZ8563 and LAN937x

KSZ9563/KSZ8563 and LAN937x switch are capable for supporting IEEE 1588 PTP
protocol. LAN937x has the same PTP register set similar to KSZ9563, hence the
implementation has been made common for the KSZ switches. KSZ9563 does not
support two step timestamping but LAN937x supports both. Tested the 1step &
2step p2p timestamping in LAN937x and p2p1step timestamping in KSZ9563.

This patch series is based on the Christian Eggers PTP support for KSZ9563.
Applied the Christian patch and updated as per the latest refactoring of KSZ
series code. The features added on top are PTP packet Interrupt
implementation based on nested handler, LAN937x two step timestamping and
programmable per_out pins.

Link: https://www.spinics.net/lists/netdev/msg705531.html

Patch v2-> v3
- used port_rxtstamp for reconstructing the absolute timestamp instead of
tagger function pointer.
- Reverted to setting of 802.1As bit.

Patch v1 -> v2
- GPIO perout enable bit is different for LAN937x and KSZ9x. Added new patch
for configuring LAN937x programmable pins.
- PTP enabled in hardware based on both tx and rx timestamping of all the user
ports.
- Replaced setting of 802.1AS bit with P2P bit in PTP_MSG_CONF1 register.

RFC v2 -> Patch v1
- Changed the patch author based on past patch submission
- Changed the commit message prefix as net: dsa: microchip: ptp
Individual patch changes are listed in correspondig commits.

RFC v1 -> v2
- Added the p2p1step timestamping and conditional execution of 2 step for
LAN937x only.
- Added the periodic output support

Arun Ramadoss (5):
net: dsa: microchip: ptp: add 4 bytes in tail tag when ptp enabled
net: dsa: microchip: ptp: enable interrupt for timestamping
net: dsa: microchip: ptp: add support for perout programmable pins
net: dsa: microchip: ptp: lan937x: add 2 step timestamping
net: dsa: microchip: ptp: lan937x: Enable periodic output in LED pins

Christian Eggers (8):
net: dsa: microchip: ptp: add the posix clock support
net: dsa: microchip: ptp: Initial hardware time stamping support
net: dsa: microchip: ptp: manipulating absolute time using ptp hw
clock
net: ptp: add helper for one-step P2P clocks
net: dsa: microchip: ptp: add packet reception timestamping
net: dsa: microchip: ptp: add packet transmission timestamping
net: dsa: microchip: ptp: move pdelay_rsp correction field to tail tag
net: dsa: microchip: ptp: add periodic output signal

MAINTAINERS | 1 +
drivers/net/dsa/microchip/Kconfig | 11 +
drivers/net/dsa/microchip/Makefile | 5 +
drivers/net/dsa/microchip/ksz_common.c | 44 +-
drivers/net/dsa/microchip/ksz_common.h | 48 +
drivers/net/dsa/microchip/ksz_ptp.c | 1183 +++++++++++++++++++++++
drivers/net/dsa/microchip/ksz_ptp.h | 86 ++
drivers/net/dsa/microchip/ksz_ptp_reg.h | 142 +++
include/linux/dsa/ksz_common.h | 53 +
include/linux/ptp_classify.h | 71 ++
net/dsa/tag_ksz.c | 213 +++-
11 files changed, 1839 insertions(+), 18 deletions(-)
create mode 100644 drivers/net/dsa/microchip/ksz_ptp.c
create mode 100644 drivers/net/dsa/microchip/ksz_ptp.h
create mode 100644 drivers/net/dsa/microchip/ksz_ptp_reg.h
create mode 100644 include/linux/dsa/ksz_common.h


base-commit: 0bdff1152c2496acf29930ec9b3c3cd7790b3f68
--
2.36.1


2022-12-09 07:29:15

by Arun Ramadoss

[permalink] [raw]
Subject: [Patch net-next v3 02/13] net: dsa: microchip: ptp: Initial hardware time stamping support

From: Christian Eggers <[email protected]>

This patch adds the routine for get_ts_info, hwstamp_get, set. This enables
the PTP support towards userspace applications such as linuxptp.

Signed-off-by: Christian Eggers <[email protected]>
Co-developed-by: Arun Ramadoss <[email protected]>
Signed-off-by: Arun Ramadoss <[email protected]>

---
v1 -> v2
- Declared the ksz_hwtstamp_get/set to NULL as macro if ptp is not
enabled
- Removed mutex lock in hwtstamp_set()

RFC v2 -> Patch v1
- moved tagger set and get function to separate patch
- Removed unnecessary comments
---
drivers/net/dsa/microchip/ksz_common.c | 3 +
drivers/net/dsa/microchip/ksz_common.h | 3 +
drivers/net/dsa/microchip/ksz_ptp.c | 101 +++++++++++++++++++++++++
drivers/net/dsa/microchip/ksz_ptp.h | 11 +++
4 files changed, 118 insertions(+)

diff --git a/drivers/net/dsa/microchip/ksz_common.c b/drivers/net/dsa/microchip/ksz_common.c
index a1282347fdc6..81da650b70fb 100644
--- a/drivers/net/dsa/microchip/ksz_common.c
+++ b/drivers/net/dsa/microchip/ksz_common.c
@@ -2978,6 +2978,9 @@ static const struct dsa_switch_ops ksz_switch_ops = {
.get_pause_stats = ksz_get_pause_stats,
.port_change_mtu = ksz_change_mtu,
.port_max_mtu = ksz_max_mtu,
+ .get_ts_info = ksz_get_ts_info,
+ .port_hwtstamp_get = ksz_hwtstamp_get,
+ .port_hwtstamp_set = ksz_hwtstamp_set,
};

struct ksz_device *ksz_switch_alloc(struct device *base, void *priv)
diff --git a/drivers/net/dsa/microchip/ksz_common.h b/drivers/net/dsa/microchip/ksz_common.h
index 23ed7fa72a3c..a5ce7ec30ba2 100644
--- a/drivers/net/dsa/microchip/ksz_common.h
+++ b/drivers/net/dsa/microchip/ksz_common.h
@@ -102,6 +102,9 @@ struct ksz_port {
struct ksz_device *ksz_dev;
struct ksz_irq pirq;
u8 num;
+#if IS_ENABLED(CONFIG_NET_DSA_MICROCHIP_KSZ_PTP)
+ struct hwtstamp_config tstamp_config;
+#endif
};

struct ksz_device {
diff --git a/drivers/net/dsa/microchip/ksz_ptp.c b/drivers/net/dsa/microchip/ksz_ptp.c
index 2da9b2ec0c40..34cc6d92c4c8 100644
--- a/drivers/net/dsa/microchip/ksz_ptp.c
+++ b/drivers/net/dsa/microchip/ksz_ptp.c
@@ -24,6 +24,107 @@
#define KSZ_PTP_INC_NS 40 /* HW clock is incremented every 40 ns (by 40) */
#define KSZ_PTP_SUBNS_BITS 32

+/* The function is return back the capability of timestamping feature when
+ * requested through ethtool -T <interface> utility
+ */
+int ksz_get_ts_info(struct dsa_switch *ds, int port, struct ethtool_ts_info *ts)
+{
+ struct ksz_device *dev = ds->priv;
+ struct ksz_ptp_data *ptp_data;
+
+ ptp_data = &dev->ptp_data;
+
+ if (!ptp_data->clock)
+ return -ENODEV;
+
+ ts->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
+ SOF_TIMESTAMPING_RX_HARDWARE |
+ SOF_TIMESTAMPING_RAW_HARDWARE;
+
+ ts->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ONESTEP_P2P);
+
+ ts->rx_filters = BIT(HWTSTAMP_FILTER_NONE) |
+ BIT(HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
+ BIT(HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
+ BIT(HWTSTAMP_FILTER_PTP_V2_EVENT);
+
+ ts->phc_index = ptp_clock_index(ptp_data->clock);
+
+ return 0;
+}
+
+int ksz_hwtstamp_get(struct dsa_switch *ds, int port, struct ifreq *ifr)
+{
+ struct ksz_device *dev = ds->priv;
+ struct hwtstamp_config *config;
+ struct ksz_port *prt;
+
+ prt = &dev->ports[port];
+ config = &prt->tstamp_config;
+
+ return copy_to_user(ifr->ifr_data, config, sizeof(*config)) ?
+ -EFAULT : 0;
+}
+
+static int ksz_set_hwtstamp_config(struct ksz_device *dev,
+ struct hwtstamp_config *config)
+{
+ if (config->flags)
+ return -EINVAL;
+
+ switch (config->tx_type) {
+ case HWTSTAMP_TX_OFF:
+ case HWTSTAMP_TX_ONESTEP_P2P:
+ break;
+ default:
+ return -ERANGE;
+ }
+
+ switch (config->rx_filter) {
+ case HWTSTAMP_FILTER_NONE:
+ break;
+ case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
+ case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
+ config->rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
+ break;
+ case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
+ case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
+ config->rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
+ break;
+ case HWTSTAMP_FILTER_PTP_V2_EVENT:
+ case HWTSTAMP_FILTER_PTP_V2_SYNC:
+ config->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
+ break;
+ default:
+ config->rx_filter = HWTSTAMP_FILTER_NONE;
+ return -ERANGE;
+ }
+
+ return 0;
+}
+
+int ksz_hwtstamp_set(struct dsa_switch *ds, int port, struct ifreq *ifr)
+{
+ struct ksz_device *dev = ds->priv;
+ struct hwtstamp_config config;
+ struct ksz_port *prt;
+ int ret;
+
+ prt = &dev->ports[port];
+
+ ret = copy_from_user(&config, ifr->ifr_data, sizeof(config));
+ if (ret)
+ return ret;
+
+ ret = ksz_set_hwtstamp_config(dev, &config);
+ if (ret)
+ return ret;
+
+ memcpy(&prt->tstamp_config, &config, sizeof(config));
+
+ return copy_to_user(ifr->ifr_data, &config, sizeof(config));
+}
+
static int _ksz_ptp_gettime(struct ksz_device *dev, struct timespec64 *ts)
{
u32 nanoseconds;
diff --git a/drivers/net/dsa/microchip/ksz_ptp.h b/drivers/net/dsa/microchip/ksz_ptp.h
index 8930047da764..7bb3fde2dd14 100644
--- a/drivers/net/dsa/microchip/ksz_ptp.h
+++ b/drivers/net/dsa/microchip/ksz_ptp.h
@@ -23,6 +23,11 @@ int ksz_ptp_clock_register(struct dsa_switch *ds);

void ksz_ptp_clock_unregister(struct dsa_switch *ds);

+int ksz_get_ts_info(struct dsa_switch *ds, int port,
+ struct ethtool_ts_info *ts);
+int ksz_hwtstamp_get(struct dsa_switch *ds, int port, struct ifreq *ifr);
+int ksz_hwtstamp_set(struct dsa_switch *ds, int port, struct ifreq *ifr);
+
#else

struct ksz_ptp_data {
@@ -37,6 +42,12 @@ static inline int ksz_ptp_clock_register(struct dsa_switch *ds)

static inline void ksz_ptp_clock_unregister(struct dsa_switch *ds) { }

+#define ksz_get_ts_info NULL
+
+#define ksz_hwtstamp_get NULL
+
+#define ksz_hwtstamp_set NULL
+
#endif /* End of CONFIG_NET_DSA_MICROCHIP_KSZ_PTP */

#endif
--
2.36.1

2022-12-09 07:29:33

by Arun Ramadoss

[permalink] [raw]
Subject: [Patch net-next v3 04/13] net: dsa: microchip: ptp: manipulating absolute time using ptp hw clock

From: Christian Eggers <[email protected]>

This patch is used for reconstructing the absolute time from the 32bit
hardware time stamping value. The do_aux ioctl is used for reading the
ptp hardware clock and store it to global variable.
The timestamped value in tail tag during rx and register during tx are
32 bit value (2 bit seconds and 30 bit nanoseconds). The time taken to
read entire ptp clock will be time consuming. In order to speed up, the
software clock is maintained. This clock time will be added to 32 bit
timestamp to get the absolute time stamp.

Signed-off-by: Christian Eggers <[email protected]>
Co-developed-by: Arun Ramadoss <[email protected]>
Signed-off-by: Arun Ramadoss <[email protected]>
---
v1 -> v2
- Used ksz_ptp_gettime instead of _ksz_ptp_gettime in do_aux_work()
- Removed the spin_lock_bh in the ksz_ptp_start_clock()

RFC v1
- This patch is based on Christian Eggers Initial hardware timestamping
support
---
drivers/net/dsa/microchip/ksz_ptp.c | 52 ++++++++++++++++++++++++++++-
drivers/net/dsa/microchip/ksz_ptp.h | 3 ++
2 files changed, 54 insertions(+), 1 deletion(-)

diff --git a/drivers/net/dsa/microchip/ksz_ptp.c b/drivers/net/dsa/microchip/ksz_ptp.c
index c77a54c29920..b864b88dc6f9 100644
--- a/drivers/net/dsa/microchip/ksz_ptp.c
+++ b/drivers/net/dsa/microchip/ksz_ptp.c
@@ -28,9 +28,11 @@
static int ksz_ptp_enable_mode(struct ksz_device *dev)
{
struct ksz_tagger_data *tagger_data = ksz_tagger_data(dev->ds);
+ struct ksz_ptp_data *ptp_data = &dev->ptp_data;
struct ksz_port *prt;
struct dsa_port *dp;
bool tag_en = false;
+ int ret;

dsa_switch_for_each_user_port(dp, dev->ds) {
prt = &dev->ports[dp->index];
@@ -40,6 +42,14 @@ static int ksz_ptp_enable_mode(struct ksz_device *dev)
}
}

+ if (tag_en) {
+ ret = ptp_schedule_worker(ptp_data->clock, 0);
+ if (ret)
+ return ret;
+ } else {
+ ptp_cancel_worker_sync(ptp_data->clock);
+ }
+
tagger_data->hwtstamp_set_state(dev->ds, tag_en);

return ksz_rmw16(dev, REG_PTP_MSG_CONF1, PTP_ENABLE,
@@ -221,6 +231,12 @@ static int ksz_ptp_settime(struct ptp_clock_info *ptp,
goto unlock;

ret = ksz_rmw16(dev, REG_PTP_CLK_CTRL, PTP_LOAD_TIME, PTP_LOAD_TIME);
+ if (ret)
+ goto unlock;
+
+ spin_lock_bh(&ptp_data->clock_lock);
+ ptp_data->clock_time = *ts;
+ spin_unlock_bh(&ptp_data->clock_lock);

unlock:
mutex_unlock(&ptp_data->lock);
@@ -275,6 +291,7 @@ static int ksz_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
{
struct ksz_ptp_data *ptp_data = ptp_caps_to_data(ptp);
struct ksz_device *dev = ptp_data_to_ksz_dev(ptp_data);
+ struct timespec64 delta64 = ns_to_timespec64(delta);
s32 sec, nsec;
u16 data16;
int ret;
@@ -307,15 +324,46 @@ static int ksz_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
data16 |= PTP_STEP_DIR;

ret = ksz_write16(dev, REG_PTP_CLK_CTRL, data16);
+ if (ret)
+ goto unlock;
+
+ spin_lock_bh(&ptp_data->clock_lock);
+ ptp_data->clock_time = timespec64_add(ptp_data->clock_time, delta64);
+ spin_unlock_bh(&ptp_data->clock_lock);

unlock:
mutex_unlock(&ptp_data->lock);
return ret;
}

+/* Function is pointer to the do_aux_work in the ptp_clock capability */
+static long ksz_ptp_do_aux_work(struct ptp_clock_info *ptp)
+{
+ struct ksz_ptp_data *ptp_data = ptp_caps_to_data(ptp);
+ struct timespec64 ts;
+
+ ksz_ptp_gettime(ptp, &ts);
+
+ spin_lock_bh(&ptp_data->clock_lock);
+ ptp_data->clock_time = ts;
+ spin_unlock_bh(&ptp_data->clock_lock);
+
+ return HZ; /* reschedule in 1 second */
+}
+
static int ksz_ptp_start_clock(struct ksz_device *dev)
{
- return ksz_rmw16(dev, REG_PTP_CLK_CTRL, PTP_CLK_ENABLE, PTP_CLK_ENABLE);
+ struct ksz_ptp_data *ptp_data = &dev->ptp_data;
+ int ret;
+
+ ret = ksz_rmw16(dev, REG_PTP_CLK_CTRL, PTP_CLK_ENABLE, PTP_CLK_ENABLE);
+ if (ret)
+ return ret;
+
+ ptp_data->clock_time.tv_sec = 0;
+ ptp_data->clock_time.tv_nsec = 0;
+
+ return 0;
}

int ksz_ptp_clock_register(struct dsa_switch *ds)
@@ -326,6 +374,7 @@ int ksz_ptp_clock_register(struct dsa_switch *ds)

ptp_data = &dev->ptp_data;
mutex_init(&ptp_data->lock);
+ spin_lock_init(&ptp_data->clock_lock);

ptp_data->caps.owner = THIS_MODULE;
snprintf(ptp_data->caps.name, 16, "Microchip Clock");
@@ -334,6 +383,7 @@ int ksz_ptp_clock_register(struct dsa_switch *ds)
ptp_data->caps.settime64 = ksz_ptp_settime;
ptp_data->caps.adjfine = ksz_ptp_adjfine;
ptp_data->caps.adjtime = ksz_ptp_adjtime;
+ ptp_data->caps.do_aux_work = ksz_ptp_do_aux_work;

ret = ksz_ptp_start_clock(dev);
if (ret)
diff --git a/drivers/net/dsa/microchip/ksz_ptp.h b/drivers/net/dsa/microchip/ksz_ptp.h
index 7bb3fde2dd14..2c29a0b604bb 100644
--- a/drivers/net/dsa/microchip/ksz_ptp.h
+++ b/drivers/net/dsa/microchip/ksz_ptp.h
@@ -17,6 +17,9 @@ struct ksz_ptp_data {
struct ptp_clock *clock;
/* Serializes all operations on the PTP hardware clock */
struct mutex lock;
+ /* lock for accessing the clock_time */
+ spinlock_t clock_lock;
+ struct timespec64 clock_time;
};

int ksz_ptp_clock_register(struct dsa_switch *ds);
--
2.36.1

2022-12-09 07:31:05

by Arun Ramadoss

[permalink] [raw]
Subject: [Patch net-next v3 13/13] net: dsa: microchip: ptp: lan937x: Enable periodic output in LED pins

There is difference in implementation of per_out pins between KSZ9563
and LAN937x. In KSZ9563, Timestamping control register (0x052C) bit 6,
if 1 - timestamp input and 0 - trigger output. But it is opposite for
LAN937x 1 - trigger output and 0 - timestamp input.
As per per_out gpio pins, KSZ9563 has four Led pins and two dedicated
gpio pins. But in LAN937x dedicated gpio pins are removed instead there
are up to 10 LED pins out of which LED_0 and LED_1 can be mapped to PTP
tou 0, 1 or 2. This patch sets the bit 6 in 0x052C register and
configure the LED override and source register for LAN937x series of
switches alone.

Signed-off-by: Arun Ramadoss <[email protected]>
---
drivers/net/dsa/microchip/ksz_ptp.c | 26 +++++++++++++++++++++++++
drivers/net/dsa/microchip/ksz_ptp_reg.h | 8 ++++++++
2 files changed, 34 insertions(+)

diff --git a/drivers/net/dsa/microchip/ksz_ptp.c b/drivers/net/dsa/microchip/ksz_ptp.c
index ccb635585721..99aae649c4a7 100644
--- a/drivers/net/dsa/microchip/ksz_ptp.c
+++ b/drivers/net/dsa/microchip/ksz_ptp.c
@@ -33,6 +33,28 @@

static int _ksz_ptp_gettime(struct ksz_device *dev, struct timespec64 *ts);

+static int ksz_ptp_tou_gpio(struct ksz_device *dev)
+{
+ int ret;
+
+ if (!is_lan937x(dev))
+ return 0;
+
+ ret = ksz_rmw32(dev, REG_PTP_CTRL_STAT__4, GPIO_OUT,
+ GPIO_OUT);
+ if (ret)
+ return ret;
+
+ ret = ksz_rmw32(dev, REG_SW_GLOBAL_LED_OVR__4, LED_OVR_1 | LED_OVR_2,
+ LED_OVR_1 | LED_OVR_2);
+ if (ret)
+ return ret;
+
+ return ksz_rmw32(dev, REG_SW_GLOBAL_LED_SRC__4,
+ LED_SRC_PTP_GPIO_1 | LED_SRC_PTP_GPIO_2,
+ LED_SRC_PTP_GPIO_1 | LED_SRC_PTP_GPIO_2);
+}
+
static int ksz_ptp_tou_reset(struct ksz_device *dev, u8 unit)
{
u32 data;
@@ -229,6 +251,10 @@ static int ksz_ptp_enable_perout(struct ksz_device *dev,
if (ret)
return ret;

+ ret = ksz_ptp_tou_gpio(dev);
+ if (ret)
+ return ret;
+
ret = ksz_ptp_tou_start(dev, request->index);
if (ret)
return ret;
diff --git a/drivers/net/dsa/microchip/ksz_ptp_reg.h b/drivers/net/dsa/microchip/ksz_ptp_reg.h
index dbccfedf89e4..6fea39629460 100644
--- a/drivers/net/dsa/microchip/ksz_ptp_reg.h
+++ b/drivers/net/dsa/microchip/ksz_ptp_reg.h
@@ -6,6 +6,14 @@
#ifndef __KSZ_PTP_REGS_H
#define __KSZ_PTP_REGS_H

+#define REG_SW_GLOBAL_LED_OVR__4 0x0120
+#define LED_OVR_2 BIT(1)
+#define LED_OVR_1 BIT(0)
+
+#define REG_SW_GLOBAL_LED_SRC__4 0x0128
+#define LED_SRC_PTP_GPIO_1 BIT(3)
+#define LED_SRC_PTP_GPIO_2 BIT(2)
+
/* 5 - PTP Clock */
#define REG_PTP_CLK_CTRL 0x0500

--
2.36.1

2022-12-09 07:31:56

by Arun Ramadoss

[permalink] [raw]
Subject: [Patch net-next v3 01/13] net: dsa: microchip: ptp: add the posix clock support

From: Christian Eggers <[email protected]>

This patch implement routines (adjfine, adjtime, gettime and settime)
for manipulating the chip's PTP clock. It registers the ptp caps
to posix clock register.

Signed-off-by: Christian Eggers <[email protected]>
Co-developed-by: Arun Ramadoss <[email protected]>
Signed-off-by: Arun Ramadoss <[email protected]>

---
v2 -> v3
- Reverted back to setting 802_1as bit.
- Also set the p2p bit which is required for the updating correction
field.

v1 -> v2
- added arri in copyright message
- Deleted setting 8021_1as bit and added P2P bit in ksz_ptp_clock_register()
- Renamed error_return label to unlock
- Assigned the ptp_clock_info directly instead of const struct
- moved the semicolon in ksz_rmw16 after 0x%x:
- moved ts_info to next patch

RFC v2 -> Patch v1
- Repharsed the Kconfig help text
- Removed IS_ERR_OR_NULL check in ptp_clock_unregister
- Add the check for ptp_data->clock in ksz_ptp_ts_info
- Renamed MAX_DRIFT_CORR to KSZ_MAX_DRIFT_CORR
- Removed the comments
- Variables declaration in reverse christmas tree
- Added the ptp_clock_optional
---
drivers/net/dsa/microchip/Kconfig | 11 ++
drivers/net/dsa/microchip/Makefile | 5 +
drivers/net/dsa/microchip/ksz_common.c | 13 +-
drivers/net/dsa/microchip/ksz_common.h | 16 ++
drivers/net/dsa/microchip/ksz_ptp.c | 240 ++++++++++++++++++++++++
drivers/net/dsa/microchip/ksz_ptp.h | 42 +++++
drivers/net/dsa/microchip/ksz_ptp_reg.h | 52 +++++
7 files changed, 378 insertions(+), 1 deletion(-)
create mode 100644 drivers/net/dsa/microchip/ksz_ptp.c
create mode 100644 drivers/net/dsa/microchip/ksz_ptp.h
create mode 100644 drivers/net/dsa/microchip/ksz_ptp_reg.h

diff --git a/drivers/net/dsa/microchip/Kconfig b/drivers/net/dsa/microchip/Kconfig
index 913f83ef013c..0546c573668a 100644
--- a/drivers/net/dsa/microchip/Kconfig
+++ b/drivers/net/dsa/microchip/Kconfig
@@ -11,6 +11,7 @@ menuconfig NET_DSA_MICROCHIP_KSZ_COMMON
config NET_DSA_MICROCHIP_KSZ9477_I2C
tristate "KSZ series I2C connected switch driver"
depends on NET_DSA_MICROCHIP_KSZ_COMMON && I2C
+ depends on PTP_1588_CLOCK_OPTIONAL
select REGMAP_I2C
help
Select to enable support for registering switches configured through I2C.
@@ -18,10 +19,20 @@ config NET_DSA_MICROCHIP_KSZ9477_I2C
config NET_DSA_MICROCHIP_KSZ_SPI
tristate "KSZ series SPI connected switch driver"
depends on NET_DSA_MICROCHIP_KSZ_COMMON && SPI
+ depends on PTP_1588_CLOCK_OPTIONAL
select REGMAP_SPI
help
Select to enable support for registering switches configured through SPI.

+config NET_DSA_MICROCHIP_KSZ_PTP
+ bool "Support for the PTP clock on the KSZ9563/LAN937x Ethernet Switch"
+ depends on NET_DSA_MICROCHIP_KSZ_COMMON && PTP_1588_CLOCK
+ help
+ Select to enable support for timestamping & PTP clock manipulation in
+ KSZ8563/KSZ9563/LAN937x series of switches. KSZ9563/KSZ8563 supports
+ only one step timestamping. LAN937x switch supports both one step and
+ two step timestamping.
+
config NET_DSA_MICROCHIP_KSZ8863_SMI
tristate "KSZ series SMI connected switch driver"
depends on NET_DSA_MICROCHIP_KSZ_COMMON
diff --git a/drivers/net/dsa/microchip/Makefile b/drivers/net/dsa/microchip/Makefile
index 28873559efc2..48360cc9fc68 100644
--- a/drivers/net/dsa/microchip/Makefile
+++ b/drivers/net/dsa/microchip/Makefile
@@ -4,6 +4,11 @@ ksz_switch-objs := ksz_common.o
ksz_switch-objs += ksz9477.o
ksz_switch-objs += ksz8795.o
ksz_switch-objs += lan937x_main.o
+
+ifdef CONFIG_NET_DSA_MICROCHIP_KSZ_PTP
+ksz_switch-objs += ksz_ptp.o
+endif
+
obj-$(CONFIG_NET_DSA_MICROCHIP_KSZ9477_I2C) += ksz9477_i2c.o
obj-$(CONFIG_NET_DSA_MICROCHIP_KSZ_SPI) += ksz_spi.o
obj-$(CONFIG_NET_DSA_MICROCHIP_KSZ8863_SMI) += ksz8863_smi.o
diff --git a/drivers/net/dsa/microchip/ksz_common.c b/drivers/net/dsa/microchip/ksz_common.c
index 423f944cc34c..a1282347fdc6 100644
--- a/drivers/net/dsa/microchip/ksz_common.c
+++ b/drivers/net/dsa/microchip/ksz_common.c
@@ -25,6 +25,7 @@
#include <net/switchdev.h>

#include "ksz_common.h"
+#include "ksz_ptp.h"
#include "ksz8.h"
#include "ksz9477.h"
#include "lan937x.h"
@@ -2103,10 +2104,16 @@ static int ksz_setup(struct dsa_switch *ds)
}
}

+ ret = ksz_ptp_clock_register(ds);
+ if (ret) {
+ dev_err(dev->dev, "Failed to register PTP clock: %d\n", ret);
+ goto out_pirq;
+ }
+
ret = ksz_mdio_register(dev);
if (ret < 0) {
dev_err(dev->dev, "failed to register the mdio");
- goto out_pirq;
+ goto out_ptp_clock_unregister;
}

/* start switch */
@@ -2115,6 +2122,8 @@ static int ksz_setup(struct dsa_switch *ds)

return 0;

+out_ptp_clock_unregister:
+ ksz_ptp_clock_unregister(ds);
out_pirq:
if (dev->irq > 0)
dsa_switch_for_each_user_port(dp, dev->ds)
@@ -2131,6 +2140,8 @@ static void ksz_teardown(struct dsa_switch *ds)
struct ksz_device *dev = ds->priv;
struct dsa_port *dp;

+ ksz_ptp_clock_unregister(ds);
+
if (dev->irq > 0) {
dsa_switch_for_each_user_port(dp, dev->ds)
ksz_irq_free(&dev->ports[dp->index].pirq);
diff --git a/drivers/net/dsa/microchip/ksz_common.h b/drivers/net/dsa/microchip/ksz_common.h
index 055d61ff3fb8..23ed7fa72a3c 100644
--- a/drivers/net/dsa/microchip/ksz_common.h
+++ b/drivers/net/dsa/microchip/ksz_common.h
@@ -15,6 +15,8 @@
#include <net/dsa.h>
#include <linux/irq.h>

+#include "ksz_ptp.h"
+
#define KSZ_MAX_NUM_PORTS 8

struct ksz_device;
@@ -140,6 +142,7 @@ struct ksz_device {
u16 port_mask;
struct mutex lock_irq; /* IRQ Access */
struct ksz_irq girq;
+ struct ksz_ptp_data ptp_data;
};

/* List of supported models */
@@ -443,6 +446,19 @@ static inline int ksz_write32(struct ksz_device *dev, u32 reg, u32 value)
return ret;
}

+static inline int ksz_rmw16(struct ksz_device *dev, u32 reg, u16 mask,
+ u16 value)
+{
+ int ret;
+
+ ret = regmap_update_bits(dev->regmap[1], reg, mask, value);
+ if (ret)
+ dev_err(dev->dev, "can't rmw 16bit reg 0x%x: %pe\n", reg,
+ ERR_PTR(ret));
+
+ return ret;
+}
+
static inline int ksz_write64(struct ksz_device *dev, u32 reg, u64 value)
{
u32 val[2];
diff --git a/drivers/net/dsa/microchip/ksz_ptp.c b/drivers/net/dsa/microchip/ksz_ptp.c
new file mode 100644
index 000000000000..2da9b2ec0c40
--- /dev/null
+++ b/drivers/net/dsa/microchip/ksz_ptp.c
@@ -0,0 +1,240 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Microchip KSZ PTP Implementation
+ *
+ * Copyright (C) 2020 ARRI Lighting
+ * Copyright (C) 2022 Microchip Technology Inc.
+ */
+
+#include <linux/kernel.h>
+#include <linux/ptp_classify.h>
+#include <linux/ptp_clock_kernel.h>
+
+#include "ksz_common.h"
+#include "ksz_ptp.h"
+#include "ksz_ptp_reg.h"
+
+#define ptp_caps_to_data(d) container_of((d), struct ksz_ptp_data, caps)
+#define ptp_data_to_ksz_dev(d) container_of((d), struct ksz_device, ptp_data)
+
+/* Sub-nanoseconds-adj,max * sub-nanoseconds / 40ns * 1ns
+ * = (2^30-1) * (2 ^ 32) / 40 ns * 1 ns = 6249999
+ */
+#define KSZ_MAX_DRIFT_CORR 6249999
+
+#define KSZ_PTP_INC_NS 40 /* HW clock is incremented every 40 ns (by 40) */
+#define KSZ_PTP_SUBNS_BITS 32
+
+static int _ksz_ptp_gettime(struct ksz_device *dev, struct timespec64 *ts)
+{
+ u32 nanoseconds;
+ u32 seconds;
+ u8 phase;
+ int ret;
+
+ /* Copy current PTP clock into shadow registers and read */
+ ret = ksz_rmw16(dev, REG_PTP_CLK_CTRL, PTP_READ_TIME, PTP_READ_TIME);
+ if (ret)
+ return ret;
+
+ ret = ksz_read8(dev, REG_PTP_RTC_SUB_NANOSEC__2, &phase);
+ if (ret)
+ return ret;
+
+ ret = ksz_read32(dev, REG_PTP_RTC_NANOSEC, &nanoseconds);
+ if (ret)
+ return ret;
+
+ ret = ksz_read32(dev, REG_PTP_RTC_SEC, &seconds);
+ if (ret)
+ return ret;
+
+ ts->tv_sec = seconds;
+ ts->tv_nsec = nanoseconds + phase * 8;
+
+ return 0;
+}
+
+static int ksz_ptp_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
+{
+ struct ksz_ptp_data *ptp_data = ptp_caps_to_data(ptp);
+ struct ksz_device *dev = ptp_data_to_ksz_dev(ptp_data);
+ int ret;
+
+ mutex_lock(&ptp_data->lock);
+ ret = _ksz_ptp_gettime(dev, ts);
+ mutex_unlock(&ptp_data->lock);
+
+ return ret;
+}
+
+static int ksz_ptp_settime(struct ptp_clock_info *ptp,
+ const struct timespec64 *ts)
+{
+ struct ksz_ptp_data *ptp_data = ptp_caps_to_data(ptp);
+ struct ksz_device *dev = ptp_data_to_ksz_dev(ptp_data);
+ int ret;
+
+ mutex_lock(&ptp_data->lock);
+
+ /* Write to shadow registers and Load PTP clock */
+ ret = ksz_write16(dev, REG_PTP_RTC_SUB_NANOSEC__2, PTP_RTC_0NS);
+ if (ret)
+ goto unlock;
+
+ ret = ksz_write32(dev, REG_PTP_RTC_NANOSEC, ts->tv_nsec);
+ if (ret)
+ goto unlock;
+
+ ret = ksz_write32(dev, REG_PTP_RTC_SEC, ts->tv_sec);
+ if (ret)
+ goto unlock;
+
+ ret = ksz_rmw16(dev, REG_PTP_CLK_CTRL, PTP_LOAD_TIME, PTP_LOAD_TIME);
+
+unlock:
+ mutex_unlock(&ptp_data->lock);
+
+ return ret;
+}
+
+static int ksz_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
+{
+ struct ksz_ptp_data *ptp_data = ptp_caps_to_data(ptp);
+ struct ksz_device *dev = ptp_data_to_ksz_dev(ptp_data);
+ int ret;
+
+ mutex_lock(&ptp_data->lock);
+
+ if (scaled_ppm) {
+ s64 ppb, adj;
+ u32 data32;
+
+ /* see scaled_ppm_to_ppb() in ptp_clock.c for details */
+ ppb = 1 + scaled_ppm;
+ ppb *= 125;
+ ppb *= KSZ_PTP_INC_NS;
+ ppb <<= KSZ_PTP_SUBNS_BITS - 13;
+ adj = div_s64(ppb, NSEC_PER_SEC);
+
+ data32 = abs(adj);
+ data32 &= PTP_SUBNANOSEC_M;
+ if (adj >= 0)
+ data32 |= PTP_RATE_DIR;
+
+ ret = ksz_write32(dev, REG_PTP_SUBNANOSEC_RATE, data32);
+ if (ret)
+ goto unlock;
+
+ ret = ksz_rmw16(dev, REG_PTP_CLK_CTRL, PTP_CLK_ADJ_ENABLE,
+ PTP_CLK_ADJ_ENABLE);
+ if (ret)
+ goto unlock;
+ } else {
+ ret = ksz_rmw16(dev, REG_PTP_CLK_CTRL, PTP_CLK_ADJ_ENABLE, 0);
+ if (ret)
+ goto unlock;
+ }
+
+unlock:
+ mutex_unlock(&ptp_data->lock);
+ return ret;
+}
+
+static int ksz_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
+{
+ struct ksz_ptp_data *ptp_data = ptp_caps_to_data(ptp);
+ struct ksz_device *dev = ptp_data_to_ksz_dev(ptp_data);
+ s32 sec, nsec;
+ u16 data16;
+ int ret;
+
+ mutex_lock(&ptp_data->lock);
+
+ /* do not use ns_to_timespec64(),
+ * both sec and nsec are subtracted by hw
+ */
+ sec = div_s64_rem(delta, NSEC_PER_SEC, &nsec);
+
+ ret = ksz_write32(dev, REG_PTP_RTC_NANOSEC, abs(nsec));
+ if (ret)
+ goto unlock;
+
+ ret = ksz_write32(dev, REG_PTP_RTC_SEC, abs(sec));
+ if (ret)
+ goto unlock;
+
+ ret = ksz_read16(dev, REG_PTP_CLK_CTRL, &data16);
+ if (ret)
+ goto unlock;
+
+ data16 |= PTP_STEP_ADJ;
+
+ /* PTP_STEP_DIR -- 0: subtract, 1: add */
+ if (delta < 0)
+ data16 &= ~PTP_STEP_DIR;
+ else
+ data16 |= PTP_STEP_DIR;
+
+ ret = ksz_write16(dev, REG_PTP_CLK_CTRL, data16);
+
+unlock:
+ mutex_unlock(&ptp_data->lock);
+ return ret;
+}
+
+static int ksz_ptp_start_clock(struct ksz_device *dev)
+{
+ return ksz_rmw16(dev, REG_PTP_CLK_CTRL, PTP_CLK_ENABLE, PTP_CLK_ENABLE);
+}
+
+int ksz_ptp_clock_register(struct dsa_switch *ds)
+{
+ struct ksz_device *dev = ds->priv;
+ struct ksz_ptp_data *ptp_data;
+ int ret;
+
+ ptp_data = &dev->ptp_data;
+ mutex_init(&ptp_data->lock);
+
+ ptp_data->caps.owner = THIS_MODULE;
+ snprintf(ptp_data->caps.name, 16, "Microchip Clock");
+ ptp_data->caps.max_adj = KSZ_MAX_DRIFT_CORR;
+ ptp_data->caps.gettime64 = ksz_ptp_gettime;
+ ptp_data->caps.settime64 = ksz_ptp_settime;
+ ptp_data->caps.adjfine = ksz_ptp_adjfine;
+ ptp_data->caps.adjtime = ksz_ptp_adjtime;
+
+ ret = ksz_ptp_start_clock(dev);
+ if (ret)
+ return ret;
+
+ /* Currently only P2P mode is supported. When 802_1AS bit is set, it
+ * forwards all PTP packets to host port and none to other ports.
+ */
+ ret = ksz_rmw16(dev, REG_PTP_MSG_CONF1, PTP_TC_P2P | PTP_802_1AS,
+ PTP_TC_P2P | PTP_802_1AS);
+ if (ret)
+ return ret;
+
+ ptp_data->clock = ptp_clock_register(&ptp_data->caps, dev->dev);
+ if (IS_ERR_OR_NULL(ptp_data->clock))
+ return PTR_ERR(ptp_data->clock);
+
+ return 0;
+}
+
+void ksz_ptp_clock_unregister(struct dsa_switch *ds)
+{
+ struct ksz_device *dev = ds->priv;
+ struct ksz_ptp_data *ptp_data;
+
+ ptp_data = &dev->ptp_data;
+
+ if (ptp_data->clock)
+ ptp_clock_unregister(ptp_data->clock);
+}
+
+MODULE_AUTHOR("Christian Eggers <[email protected]>");
+MODULE_AUTHOR("Arun Ramadoss <[email protected]>");
+MODULE_DESCRIPTION("PTP support for KSZ switch");
+MODULE_LICENSE("GPL");
diff --git a/drivers/net/dsa/microchip/ksz_ptp.h b/drivers/net/dsa/microchip/ksz_ptp.h
new file mode 100644
index 000000000000..8930047da764
--- /dev/null
+++ b/drivers/net/dsa/microchip/ksz_ptp.h
@@ -0,0 +1,42 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Microchip KSZ PTP Implementation
+ *
+ * Copyright (C) 2020 ARRI Lighting
+ * Copyright (C) 2022 Microchip Technology Inc.
+ */
+
+#ifndef _NET_DSA_DRIVERS_KSZ_PTP_H
+#define _NET_DSA_DRIVERS_KSZ_PTP_H
+
+#if IS_ENABLED(CONFIG_NET_DSA_MICROCHIP_KSZ_PTP)
+
+#include <linux/ptp_clock_kernel.h>
+
+struct ksz_ptp_data {
+ struct ptp_clock_info caps;
+ struct ptp_clock *clock;
+ /* Serializes all operations on the PTP hardware clock */
+ struct mutex lock;
+};
+
+int ksz_ptp_clock_register(struct dsa_switch *ds);
+
+void ksz_ptp_clock_unregister(struct dsa_switch *ds);
+
+#else
+
+struct ksz_ptp_data {
+ /* Serializes all operations on the PTP hardware clock */
+ struct mutex lock;
+};
+
+static inline int ksz_ptp_clock_register(struct dsa_switch *ds)
+{
+ return 0;
+}
+
+static inline void ksz_ptp_clock_unregister(struct dsa_switch *ds) { }
+
+#endif /* End of CONFIG_NET_DSA_MICROCHIP_KSZ_PTP */
+
+#endif
diff --git a/drivers/net/dsa/microchip/ksz_ptp_reg.h b/drivers/net/dsa/microchip/ksz_ptp_reg.h
new file mode 100644
index 000000000000..4ca4ad4bba7e
--- /dev/null
+++ b/drivers/net/dsa/microchip/ksz_ptp_reg.h
@@ -0,0 +1,52 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Microchip KSZ PTP register definitions
+ * Copyright (C) 2022 Microchip Technology Inc.
+ */
+
+#ifndef __KSZ_PTP_REGS_H
+#define __KSZ_PTP_REGS_H
+
+/* 5 - PTP Clock */
+#define REG_PTP_CLK_CTRL 0x0500
+
+#define PTP_STEP_ADJ BIT(6)
+#define PTP_STEP_DIR BIT(5)
+#define PTP_READ_TIME BIT(4)
+#define PTP_LOAD_TIME BIT(3)
+#define PTP_CLK_ADJ_ENABLE BIT(2)
+#define PTP_CLK_ENABLE BIT(1)
+#define PTP_CLK_RESET BIT(0)
+
+#define REG_PTP_RTC_SUB_NANOSEC__2 0x0502
+
+#define PTP_RTC_SUB_NANOSEC_M 0x0007
+#define PTP_RTC_0NS 0x00
+
+#define REG_PTP_RTC_NANOSEC 0x0504
+
+#define REG_PTP_RTC_SEC 0x0508
+
+#define REG_PTP_SUBNANOSEC_RATE 0x050C
+
+#define PTP_SUBNANOSEC_M 0x3FFFFFFF
+#define PTP_RATE_DIR BIT(31)
+#define PTP_TMP_RATE_ENABLE BIT(30)
+
+#define REG_PTP_SUBNANOSEC_RATE_L 0x050E
+
+#define REG_PTP_RATE_DURATION 0x0510
+#define REG_PTP_RATE_DURATION_H 0x0510
+#define REG_PTP_RATE_DURATION_L 0x0512
+
+#define REG_PTP_MSG_CONF1 0x0514
+
+#define PTP_802_1AS BIT(7)
+#define PTP_ENABLE BIT(6)
+#define PTP_ETH_ENABLE BIT(5)
+#define PTP_IPV4_UDP_ENABLE BIT(4)
+#define PTP_IPV6_UDP_ENABLE BIT(3)
+#define PTP_TC_P2P BIT(2)
+#define PTP_MASTER BIT(1)
+#define PTP_1STEP BIT(0)
+
+#endif
--
2.36.1

2022-12-09 07:39:28

by Arun Ramadoss

[permalink] [raw]
Subject: [Patch net-next v3 11/13] net: dsa: microchip: ptp: add support for perout programmable pins

There are two programmable pins available for Trigger output unit to
generate periodic pulses. This patch add verify_pin for the available 2
pins and configure it with respect to GPIO index for the TOU unit.

Tested using testptp
./testptp -i 0 -L 0,2
./testptp -i 0 -d /dev/ptp0 -p 1000000000
./testptp -i 1 -L 1,2
./testptp -i 1 -d /dev/ptp0 -p 100000000

Signed-off-by: Arun Ramadoss <[email protected]>
---
v1 - v2
- checkpatch warning to limit 80 chars

Patch v1
- patch is new
---
drivers/net/dsa/microchip/ksz_ptp.c | 35 +++++++++++++++++++++++++++++
drivers/net/dsa/microchip/ksz_ptp.h | 3 +++
2 files changed, 38 insertions(+)

diff --git a/drivers/net/dsa/microchip/ksz_ptp.c b/drivers/net/dsa/microchip/ksz_ptp.c
index 6d7edc81909e..546211ae46a3 100644
--- a/drivers/net/dsa/microchip/ksz_ptp.c
+++ b/drivers/net/dsa/microchip/ksz_ptp.c
@@ -175,6 +175,10 @@ static int ksz_ptp_enable_perout(struct ksz_device *dev,
ptp_data->tou_mode != KSZ_PTP_TOU_IDLE)
return -EBUSY;

+ pin = ptp_find_pin(ptp_data->clock, PTP_PF_PEROUT, request->index);
+ if (pin < 0)
+ return -EINVAL;
+
data32 = FIELD_PREP(PTP_GPIO_INDEX, pin) |
FIELD_PREP(PTP_TOU_INDEX, request->index);
ret = ksz_rmw32(dev, REG_PTP_UNIT_INDEX__4,
@@ -792,6 +796,23 @@ static int ksz_ptp_enable(struct ptp_clock_info *ptp,
return ret;
}

+static int ksz_ptp_verify_pin(struct ptp_clock_info *ptp, unsigned int pin,
+ enum ptp_pin_function func, unsigned int chan)
+{
+ int ret = 0;
+
+ switch (func) {
+ case PTP_PF_NONE:
+ case PTP_PF_PEROUT:
+ break;
+ default:
+ ret = -1;
+ break;
+ }
+
+ return ret;
+}
+
/* Function is pointer to the do_aux_work in the ptp_clock capability */
static long ksz_ptp_do_aux_work(struct ptp_clock_info *ptp)
{
@@ -827,6 +848,7 @@ int ksz_ptp_clock_register(struct dsa_switch *ds)
struct ksz_device *dev = ds->priv;
struct ksz_ptp_data *ptp_data;
int ret;
+ u8 i;

ptp_data = &dev->ptp_data;
mutex_init(&ptp_data->lock);
@@ -841,12 +863,25 @@ int ksz_ptp_clock_register(struct dsa_switch *ds)
ptp_data->caps.adjtime = ksz_ptp_adjtime;
ptp_data->caps.do_aux_work = ksz_ptp_do_aux_work;
ptp_data->caps.enable = ksz_ptp_enable;
+ ptp_data->caps.verify = ksz_ptp_verify_pin;
+ ptp_data->caps.n_pins = KSZ_PTP_N_GPIO;
ptp_data->caps.n_per_out = 3;

ret = ksz_ptp_start_clock(dev);
if (ret)
return ret;

+ for (i = 0; i < KSZ_PTP_N_GPIO; i++) {
+ struct ptp_pin_desc *ptp_pin = &ptp_data->pin_config[i];
+
+ snprintf(ptp_pin->name,
+ sizeof(ptp_pin->name), "ksz_ptp_pin_%02d", i);
+ ptp_pin->index = i;
+ ptp_pin->func = PTP_PF_NONE;
+ }
+
+ ptp_data->caps.pin_config = ptp_data->pin_config;
+
/* Currently only P2P mode is supported. When 802_1AS bit is set, it
* forwards all PTP packets to host port and none to other ports.
*/
diff --git a/drivers/net/dsa/microchip/ksz_ptp.h b/drivers/net/dsa/microchip/ksz_ptp.h
index 9451e3a76375..0ca8ca4f804e 100644
--- a/drivers/net/dsa/microchip/ksz_ptp.h
+++ b/drivers/net/dsa/microchip/ksz_ptp.h
@@ -12,6 +12,8 @@

#include <linux/ptp_clock_kernel.h>

+#define KSZ_PTP_N_GPIO 2
+
enum ksz_ptp_tou_mode {
KSZ_PTP_TOU_IDLE,
KSZ_PTP_TOU_PEROUT,
@@ -20,6 +22,7 @@ enum ksz_ptp_tou_mode {
struct ksz_ptp_data {
struct ptp_clock_info caps;
struct ptp_clock *clock;
+ struct ptp_pin_desc pin_config[KSZ_PTP_N_GPIO];
/* Serializes all operations on the PTP hardware clock */
struct mutex lock;
/* lock for accessing the clock_time */
--
2.36.1

2022-12-09 07:46:15

by Arun Ramadoss

[permalink] [raw]
Subject: [Patch net-next v3 08/13] net: dsa: microchip: ptp: add packet transmission timestamping

From: Christian Eggers <[email protected]>

This patch adds the routines for transmission of ptp packets. When the
ptp pdelay_req packet to be transmitted, it uses the deferred xmit
worker to schedule the packets.
During irq_setup, interrupt for Sync, Pdelay_req and Pdelay_rsp are
enabled. So interrupt is triggered for all three packets. But for
p2p1step, we require only time stamp of Pdelay_req packet. Hence to
avoid posting of the completion from ISR routine for Sync and
Pdelay_resp packets, ts_en flag is introduced. This controls which
packets need to processed for timestamp.
After the packet is transmitted, ISR is triggered. The time at which
packet transmitted is recorded to separate register.
This value is reconstructed to absolute time and posted to the user
application through socket error queue.

Signed-off-by: Christian Eggers <[email protected]>
Co-developed-by: Arun Ramadoss <[email protected]>
Signed-off-by: Arun Ramadoss <[email protected]>
---
v1 -> v2
- Declared the deferred xmit and ksz_port_txtstamp function as null in
ptp disabled case

RFC v2 -> Patch v1
- separated the pdelay_rsp message correction update in different patch
---
drivers/net/dsa/microchip/ksz_common.c | 14 +++
drivers/net/dsa/microchip/ksz_common.h | 3 +
drivers/net/dsa/microchip/ksz_ptp.c | 116 ++++++++++++++++++++++++-
drivers/net/dsa/microchip/ksz_ptp.h | 6 ++
include/linux/dsa/ksz_common.h | 8 ++
net/dsa/tag_ksz.c | 54 +++++++++++-
6 files changed, 197 insertions(+), 4 deletions(-)

diff --git a/drivers/net/dsa/microchip/ksz_common.c b/drivers/net/dsa/microchip/ksz_common.c
index 127fdbc25141..c084c3b1bec0 100644
--- a/drivers/net/dsa/microchip/ksz_common.c
+++ b/drivers/net/dsa/microchip/ksz_common.c
@@ -6,6 +6,7 @@
*/

#include <linux/delay.h>
+#include <linux/dsa/ksz_common.h>
#include <linux/export.h>
#include <linux/gpio/consumer.h>
#include <linux/kernel.h>
@@ -2540,6 +2541,17 @@ static enum dsa_tag_protocol ksz_get_tag_protocol(struct dsa_switch *ds,
return proto;
}

+static int ksz_connect_tag_protocol(struct dsa_switch *ds,
+ enum dsa_tag_protocol proto)
+{
+ struct ksz_tagger_data *tagger_data;
+
+ tagger_data = ksz_tagger_data(ds);
+ tagger_data->xmit_work_fn = ksz_port_deferred_xmit;
+
+ return 0;
+}
+
static int ksz_port_vlan_filtering(struct dsa_switch *ds, int port,
bool flag, struct netlink_ext_ack *extack)
{
@@ -2955,6 +2967,7 @@ static int ksz_switch_detect(struct ksz_device *dev)

static const struct dsa_switch_ops ksz_switch_ops = {
.get_tag_protocol = ksz_get_tag_protocol,
+ .connect_tag_protocol = ksz_connect_tag_protocol,
.get_phy_flags = ksz_get_phy_flags,
.setup = ksz_setup,
.teardown = ksz_teardown,
@@ -2992,6 +3005,7 @@ static const struct dsa_switch_ops ksz_switch_ops = {
.get_ts_info = ksz_get_ts_info,
.port_hwtstamp_get = ksz_hwtstamp_get,
.port_hwtstamp_set = ksz_hwtstamp_set,
+ .port_txtstamp = ksz_port_txtstamp,
.port_rxtstamp = ksz_port_rxtstamp,
};

diff --git a/drivers/net/dsa/microchip/ksz_common.h b/drivers/net/dsa/microchip/ksz_common.h
index 918028db2e6f..6809e1a76037 100644
--- a/drivers/net/dsa/microchip/ksz_common.h
+++ b/drivers/net/dsa/microchip/ksz_common.h
@@ -87,6 +87,7 @@ struct ksz_irq {
struct ksz_ptp_irq {
struct ksz_port *port;
u16 ts_reg;
+ bool ts_en;
char name[16];
int irq_num;
};
@@ -116,6 +117,8 @@ struct ksz_port {
bool hwts_rx_en;
struct ksz_irq ptpirq;
struct ksz_ptp_irq ptpmsg_irq[3];
+ ktime_t tstamp_msg;
+ struct completion tstamp_msg_comp;
#endif
};

diff --git a/drivers/net/dsa/microchip/ksz_ptp.c b/drivers/net/dsa/microchip/ksz_ptp.c
index e9c2442eec71..96927a699665 100644
--- a/drivers/net/dsa/microchip/ksz_ptp.c
+++ b/drivers/net/dsa/microchip/ksz_ptp.c
@@ -18,6 +18,8 @@

#define ptp_caps_to_data(d) container_of((d), struct ksz_ptp_data, caps)
#define ptp_data_to_ksz_dev(d) container_of((d), struct ksz_device, ptp_data)
+#define work_to_xmit_work(w) \
+ container_of((w), struct ksz_deferred_xmit_work, work)

/* Sub-nanoseconds-adj,max * sub-nanoseconds / 40ns * 1ns
* = (2^30-1) * (2 ^ 32) / 40 ns * 1 ns = 6249999
@@ -111,9 +113,15 @@ static int ksz_set_hwtstamp_config(struct ksz_device *dev,

switch (config->tx_type) {
case HWTSTAMP_TX_OFF:
+ prt->ptpmsg_irq[KSZ_SYNC_MSG].ts_en = 0;
+ prt->ptpmsg_irq[KSZ_XDREQ_MSG].ts_en = 0;
+ prt->ptpmsg_irq[KSZ_PDRES_MSG].ts_en = 0;
prt->hwts_tx_en = false;
break;
case HWTSTAMP_TX_ONESTEP_P2P:
+ prt->ptpmsg_irq[KSZ_SYNC_MSG].ts_en = 0;
+ prt->ptpmsg_irq[KSZ_XDREQ_MSG].ts_en = 1;
+ prt->ptpmsg_irq[KSZ_PDRES_MSG].ts_en = 0;
prt->hwts_tx_en = true;
break;
default:
@@ -232,6 +240,88 @@ bool ksz_port_rxtstamp(struct dsa_switch *ds, int port, struct sk_buff *skb,
return 0;
}

+void ksz_port_txtstamp(struct dsa_switch *ds, int port,
+ struct sk_buff *skb)
+{
+ struct ksz_device *dev = ds->priv;
+ struct ptp_header *hdr;
+ struct sk_buff *clone;
+ struct ksz_port *prt;
+ unsigned int type;
+ u8 ptp_msg_type;
+
+ prt = &dev->ports[port];
+
+ if (!prt->hwts_tx_en)
+ return;
+
+ type = ptp_classify_raw(skb);
+ if (type == PTP_CLASS_NONE)
+ return;
+
+ hdr = ptp_parse_header(skb, type);
+ if (!hdr)
+ return;
+
+ ptp_msg_type = ptp_get_msgtype(hdr, type);
+
+ switch (ptp_msg_type) {
+ case PTP_MSGTYPE_PDELAY_REQ:
+ break;
+
+ default:
+ return;
+ }
+
+ clone = skb_clone_sk(skb);
+ if (!clone)
+ return;
+
+ /* caching the value to be used in tag_ksz.c */
+ KSZ_SKB_CB(skb)->clone = clone;
+}
+
+static void ksz_ptp_txtstamp_skb(struct ksz_device *dev,
+ struct ksz_port *prt, struct sk_buff *skb)
+{
+ struct skb_shared_hwtstamps hwtstamps = {};
+ int ret;
+
+ skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
+
+ /* timeout must include tstamp latency, IRQ latency and time for
+ * reading the time stamp.
+ */
+ ret = wait_for_completion_timeout(&prt->tstamp_msg_comp,
+ msecs_to_jiffies(100));
+ if (!ret)
+ return;
+
+ hwtstamps.hwtstamp = prt->tstamp_msg;
+ skb_complete_tx_timestamp(skb, &hwtstamps);
+}
+
+void ksz_port_deferred_xmit(struct kthread_work *work)
+{
+ struct ksz_deferred_xmit_work *xmit_work = work_to_xmit_work(work);
+ struct sk_buff *clone, *skb = xmit_work->skb;
+ struct dsa_switch *ds = xmit_work->dp->ds;
+ struct ksz_device *dev = ds->priv;
+ struct ksz_port *prt;
+
+ prt = &dev->ports[xmit_work->dp->index];
+
+ clone = KSZ_SKB_CB(skb)->clone;
+
+ reinit_completion(&prt->tstamp_msg_comp);
+
+ dsa_enqueue_skb(skb, skb->dev);
+
+ ksz_ptp_txtstamp_skb(dev, prt, clone);
+
+ kfree(xmit_work);
+}
+
static int _ksz_ptp_gettime(struct ksz_device *dev, struct timespec64 *ts)
{
u32 nanoseconds;
@@ -484,7 +574,29 @@ void ksz_ptp_clock_unregister(struct dsa_switch *ds)

static irqreturn_t ksz_ptp_msg_thread_fn(int irq, void *dev_id)
{
- return IRQ_NONE;
+ struct ksz_ptp_irq *ptpmsg_irq = dev_id;
+ struct ksz_device *dev;
+ struct ksz_port *port;
+ u32 tstamp_raw;
+ ktime_t tstamp;
+ int ret;
+
+ port = ptpmsg_irq->port;
+ dev = port->ksz_dev;
+
+ if (ptpmsg_irq->ts_en) {
+ ret = ksz_read32(dev, ptpmsg_irq->ts_reg, &tstamp_raw);
+ if (ret)
+ return IRQ_NONE;
+
+ tstamp = ksz_decode_tstamp(tstamp_raw);
+
+ port->tstamp_msg = ksz_tstamp_reconstruct(dev, tstamp);
+
+ complete(&port->tstamp_msg_comp);
+ }
+
+ return IRQ_HANDLED;
}

static irqreturn_t ksz_ptp_irq_thread_fn(int irq, void *dev_id)
@@ -635,6 +747,8 @@ int ksz_ptp_irq_setup(struct dsa_switch *ds, u8 p)
REG_PTP_PORT_TX_INT_STATUS__2);
snprintf(ptpirq->name, sizeof(ptpirq->name), "ptp_irq-%d", p);

+ init_completion(&port->tstamp_msg_comp);
+
ptpirq->irq_num = irq_find_mapping(port->pirq.domain, PORT_SRC_PTP_INT);
if (ptpirq->irq_num < 0)
return ptpirq->irq_num;
diff --git a/drivers/net/dsa/microchip/ksz_ptp.h b/drivers/net/dsa/microchip/ksz_ptp.h
index 9bb8fb059ac2..0b14aed71ec2 100644
--- a/drivers/net/dsa/microchip/ksz_ptp.h
+++ b/drivers/net/dsa/microchip/ksz_ptp.h
@@ -30,6 +30,8 @@ int ksz_get_ts_info(struct dsa_switch *ds, int port,
struct ethtool_ts_info *ts);
int ksz_hwtstamp_get(struct dsa_switch *ds, int port, struct ifreq *ifr);
int ksz_hwtstamp_set(struct dsa_switch *ds, int port, struct ifreq *ifr);
+void ksz_port_txtstamp(struct dsa_switch *ds, int port, struct sk_buff *skb);
+void ksz_port_deferred_xmit(struct kthread_work *work);
bool ksz_port_rxtstamp(struct dsa_switch *ds, int port, struct sk_buff *skb,
unsigned int type);
int ksz_ptp_irq_setup(struct dsa_switch *ds, u8 p);
@@ -64,6 +66,10 @@ static inline void ksz_ptp_irq_free(struct dsa_switch *ds, u8 p) {}

#define ksz_port_rxtstamp NULL

+#define ksz_port_txtstamp NULL
+
+#define ksz_port_deferred_xmit NULL
+
#endif /* End of CONFIG_NET_DSA_MICROCHIP_KSZ_PTP */

#endif
diff --git a/include/linux/dsa/ksz_common.h b/include/linux/dsa/ksz_common.h
index a256b08d837d..b91beab5e138 100644
--- a/include/linux/dsa/ksz_common.h
+++ b/include/linux/dsa/ksz_common.h
@@ -23,11 +23,19 @@ static inline ktime_t ksz_decode_tstamp(u32 tstamp)
return ns_to_ktime(ns);
}

+struct ksz_deferred_xmit_work {
+ struct dsa_port *dp;
+ struct sk_buff *skb;
+ struct kthread_work work;
+};
+
struct ksz_tagger_data {
+ void (*xmit_work_fn)(struct kthread_work *work);
void (*hwtstamp_set_state)(struct dsa_switch *ds, bool on);
};

struct ksz_skb_cb {
+ struct sk_buff *clone;
u32 tstamp;
};

diff --git a/net/dsa/tag_ksz.c b/net/dsa/tag_ksz.c
index 6603eaa234d2..e14ee26bf6a0 100644
--- a/net/dsa/tag_ksz.c
+++ b/net/dsa/tag_ksz.c
@@ -26,6 +26,7 @@
struct ksz_tagger_private {
struct ksz_tagger_data data; /* Must be first */
unsigned long state;
+ struct kthread_worker *xmit_worker;
};

static struct ksz_tagger_private *
@@ -48,6 +49,7 @@ static void ksz_disconnect(struct dsa_switch *ds)
{
struct ksz_tagger_private *priv = ds->tagger_data;

+ kthread_destroy_worker(priv->xmit_worker);
kfree(priv);
ds->tagger_data = NULL;
}
@@ -55,12 +57,23 @@ static void ksz_disconnect(struct dsa_switch *ds)
static int ksz_connect(struct dsa_switch *ds)
{
struct ksz_tagger_data *tagger_data;
+ struct kthread_worker *xmit_worker;
struct ksz_tagger_private *priv;
+ int ret;

priv = kzalloc(sizeof(*priv), GFP_KERNEL);
if (!priv)
return -ENOMEM;

+ xmit_worker = kthread_create_worker(0, "dsa%d:%d_xmit",
+ ds->dst->index, ds->index);
+ if (IS_ERR(xmit_worker)) {
+ ret = PTR_ERR(xmit_worker);
+ kfree(priv);
+ return ret;
+ }
+
+ priv->xmit_worker = xmit_worker;
/* Export functions for switch driver use */
tagger_data = &priv->data;
tagger_data->hwtstamp_set_state = ksz_hwtstamp_set_state;
@@ -191,6 +204,41 @@ static void ksz_xmit_timestamp(struct dsa_port *dp, struct sk_buff *skb)
put_unaligned_be32(0, skb_put(skb, KSZ_PTP_TAG_LEN));
}

+/* Defer transmit if waiting for egress time stamp is required. */
+static struct sk_buff *ksz_defer_xmit(struct dsa_port *dp, struct sk_buff *skb)
+{
+ struct ksz_tagger_data *tagger_data = ksz_tagger_data(dp->ds);
+ struct ksz_tagger_private *priv = ksz_tagger_private(dp->ds);
+ void (*xmit_work_fn)(struct kthread_work *work);
+ struct sk_buff *clone = KSZ_SKB_CB(skb)->clone;
+ struct ksz_deferred_xmit_work *xmit_work;
+ struct kthread_worker *xmit_worker;
+
+ if (!clone)
+ return skb; /* no deferred xmit for this packet */
+
+ xmit_work_fn = tagger_data->xmit_work_fn;
+ xmit_worker = priv->xmit_worker;
+
+ if (!xmit_work_fn || !xmit_worker)
+ return NULL;
+
+ xmit_work = kzalloc(sizeof(*xmit_work), GFP_ATOMIC);
+ if (!xmit_work)
+ return NULL;
+
+ kthread_init_work(&xmit_work->work, xmit_work_fn);
+ /* Increase refcount so the kfree_skb in dsa_slave_xmit
+ * won't really free the packet.
+ */
+ xmit_work->dp = dp;
+ xmit_work->skb = skb_get(skb);
+
+ kthread_queue_work(xmit_worker, &xmit_work->work);
+
+ return NULL;
+}
+
static struct sk_buff *ksz9477_xmit(struct sk_buff *skb,
struct net_device *dev)
{
@@ -215,7 +263,7 @@ static struct sk_buff *ksz9477_xmit(struct sk_buff *skb,

*tag = cpu_to_be16(val);

- return skb;
+ return ksz_defer_xmit(dp, skb);
}

static struct sk_buff *ksz9477_rcv(struct sk_buff *skb, struct net_device *dev)
@@ -271,7 +319,7 @@ static struct sk_buff *ksz9893_xmit(struct sk_buff *skb,
if (is_link_local_ether_addr(addr))
*tag |= KSZ9893_TAIL_TAG_OVERRIDE;

- return skb;
+ return ksz_defer_xmit(dp, skb);
}

static const struct dsa_device_ops ksz9893_netdev_ops = {
@@ -336,7 +384,7 @@ static struct sk_buff *lan937x_xmit(struct sk_buff *skb,

put_unaligned_be16(val, tag);

- return skb;
+ return ksz_defer_xmit(dp, skb);
}

static const struct dsa_device_ops lan937x_netdev_ops = {
--
2.36.1

2022-12-09 07:49:23

by Arun Ramadoss

[permalink] [raw]
Subject: [Patch net-next v3 06/13] net: ptp: add helper for one-step P2P clocks

From: Christian Eggers <[email protected]>

For P2P delay measurement, the ingress time stamp of the PDelay_Req is
required for the correction field of the PDelay_Resp. The application
echoes back the correction field of the PDelay_Req when sending the
PDelay_Resp.

Some hardware (like the ZHAW InES PTP time stamping IP core) subtracts
the ingress timestamp autonomously from the correction field, so that
the hardware only needs to add the egress timestamp on tx. Other
hardware (like the Microchip KSZ9563) reports the ingress time stamp via
an interrupt and requires that the software provides this time stamp via
tail-tag on tx.

In order to avoid introducing a further application interface for this,
the driver can simply emulate the behavior of the InES device and
subtract the ingress time stamp in software from the correction field.

On egress, the correction field can either be kept as it is (and the
time stamp field in the tail-tag is set to zero) or move the value from
the correction field back to the tail-tag.

Changing the correction field requires updating the UDP checksum (if UDP
is used as transport).

Signed-off-by: Christian Eggers <[email protected]>

---
v1 -> v2
- Fixed compilation issue when PTP_CLASSIFY not selected in menuconfig
as reported by kernel test robot <[email protected]>
---
include/linux/ptp_classify.h | 71 ++++++++++++++++++++++++++++++++++++
1 file changed, 71 insertions(+)

diff --git a/include/linux/ptp_classify.h b/include/linux/ptp_classify.h
index 2b6ea36ad162..6e5869c2504c 100644
--- a/include/linux/ptp_classify.h
+++ b/include/linux/ptp_classify.h
@@ -10,8 +10,12 @@
#ifndef _PTP_CLASSIFY_H_
#define _PTP_CLASSIFY_H_

+#include <asm/unaligned.h>
#include <linux/ip.h>
+#include <linux/ktime.h>
#include <linux/skbuff.h>
+#include <linux/udp.h>
+#include <net/checksum.h>

#define PTP_CLASS_NONE 0x00 /* not a PTP event message */
#define PTP_CLASS_V1 0x01 /* protocol version 1 */
@@ -129,6 +133,67 @@ static inline u8 ptp_get_msgtype(const struct ptp_header *hdr,
return msgtype;
}

+/**
+ * ptp_check_diff8 - Computes new checksum (when altering a 64-bit field)
+ * @old: old field value
+ * @new: new field value
+ * @oldsum: previous checksum
+ *
+ * This function can be used to calculate a new checksum when only a single
+ * field is changed. Similar as ip_vs_check_diff*() in ip_vs.h.
+ *
+ * Return: Updated checksum
+ */
+static inline __wsum ptp_check_diff8(__be64 old, __be64 new, __wsum oldsum)
+{
+ __be64 diff[2] = { ~old, new };
+
+ return csum_partial(diff, sizeof(diff), oldsum);
+}
+
+/**
+ * ptp_header_update_correction - Update PTP header's correction field
+ * @skb: packet buffer
+ * @type: type of the packet (see ptp_classify_raw())
+ * @hdr: ptp header
+ * @correction: new correction value
+ *
+ * This updates the correction field of a PTP header and updates the UDP
+ * checksum (if UDP is used as transport). It is needed for hardware capable of
+ * one-step P2P that does not already modify the correction field of Pdelay_Req
+ * event messages on ingress.
+ */
+static inline
+void ptp_header_update_correction(struct sk_buff *skb, unsigned int type,
+ struct ptp_header *hdr, s64 correction)
+{
+ __be64 correction_old;
+ struct udphdr *uhdr;
+
+ /* previous correction value is required for checksum update. */
+ memcpy(&correction_old, &hdr->correction, sizeof(correction_old));
+
+ /* write new correction value */
+ put_unaligned_be64((u64)correction, &hdr->correction);
+
+ switch (type & PTP_CLASS_PMASK) {
+ case PTP_CLASS_IPV4:
+ case PTP_CLASS_IPV6:
+ /* locate udp header */
+ uhdr = (struct udphdr *)((char *)hdr - sizeof(struct udphdr));
+ break;
+ default:
+ return;
+ }
+
+ /* update checksum */
+ uhdr->check = csum_fold(ptp_check_diff8(correction_old,
+ hdr->correction,
+ ~csum_unfold(uhdr->check)));
+ if (!uhdr->check)
+ uhdr->check = CSUM_MANGLED_0;
+}
+
/**
* ptp_msg_is_sync - Evaluates whether the given skb is a PTP Sync message
* @skb: packet buffer
@@ -166,5 +231,11 @@ static inline bool ptp_msg_is_sync(struct sk_buff *skb, unsigned int type)
{
return false;
}
+
+static inline
+void ptp_header_update_correction(struct sk_buff *skb, unsigned int type,
+ struct ptp_header *hdr, s64 correction)
+{
+}
#endif
#endif /* _PTP_CLASSIFY_H_ */
--
2.36.1

2022-12-09 07:51:03

by Arun Ramadoss

[permalink] [raw]
Subject: [Patch net-next v3 03/13] net: dsa: microchip: ptp: add 4 bytes in tail tag when ptp enabled

When the PTP is enabled in hardware bit 6 of PTP_MSG_CONF1 register, the
transmit frame needs additional 4 bytes before the tail tag. It is
needed for all the transmission packets irrespective of PTP packets or
not.
The 4-byte timestamp field is 0 for frames other than Pdelay_Resp. For
the one-step Pdelay_Resp, the switch needs the receive timestamp of the
Pdelay_Req message so that it can put the turnaround time in the
correction field.
Since PTP has to be enabled for both Transmission and reception
timestamping, driver needs to track of the tx and rx setting of the all
the user ports in the switch.
Two flags hw_tx_en and hw_rx_en are added in ksz_port to track the
timestampping setting of each port. When any one of ports has tx or rx
timestampping enabled, bit 6 of PTP_MSG_CONF1 is set and it is indicated
to tag_ksz.c through tagger bytes. This flag adds 4 additional bytes to
the tail tag. When tx and rx timestamping of all the ports are disabled,
then 4 bytes are not added.

Testing using hwstamp -i <interface>

Signed-off-by: Arun Ramadoss <[email protected]>
---
v1 - v2
- check patch warning for line exceeding 80
- Removed the tagger_get_state function
- Added the 4 additional bytes to tail tag based on the all the ports tx
and rx timestamping

Patch v1
- Patch is new
---
MAINTAINERS | 1 +
drivers/net/dsa/microchip/ksz_common.h | 2 +
drivers/net/dsa/microchip/ksz_ptp.c | 34 ++++++++-
include/linux/dsa/ksz_common.h | 22 ++++++
net/dsa/tag_ksz.c | 95 ++++++++++++++++++++++++--
5 files changed, 145 insertions(+), 9 deletions(-)
create mode 100644 include/linux/dsa/ksz_common.h

diff --git a/MAINTAINERS b/MAINTAINERS
index 955c1be1efb2..76296ceb57aa 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -13514,6 +13514,7 @@ S: Maintained
F: Documentation/devicetree/bindings/net/dsa/microchip,ksz.yaml
F: Documentation/devicetree/bindings/net/dsa/microchip,lan937x.yaml
F: drivers/net/dsa/microchip/*
+F: include/linux/dsa/ksz_common.h
F: include/linux/platform_data/microchip-ksz.h
F: net/dsa/tag_ksz.c

diff --git a/drivers/net/dsa/microchip/ksz_common.h b/drivers/net/dsa/microchip/ksz_common.h
index a5ce7ec30ba2..641aca78ef05 100644
--- a/drivers/net/dsa/microchip/ksz_common.h
+++ b/drivers/net/dsa/microchip/ksz_common.h
@@ -104,6 +104,8 @@ struct ksz_port {
u8 num;
#if IS_ENABLED(CONFIG_NET_DSA_MICROCHIP_KSZ_PTP)
struct hwtstamp_config tstamp_config;
+ bool hwts_tx_en;
+ bool hwts_rx_en;
#endif
};

diff --git a/drivers/net/dsa/microchip/ksz_ptp.c b/drivers/net/dsa/microchip/ksz_ptp.c
index 34cc6d92c4c8..c77a54c29920 100644
--- a/drivers/net/dsa/microchip/ksz_ptp.c
+++ b/drivers/net/dsa/microchip/ksz_ptp.c
@@ -5,6 +5,7 @@
* Copyright (C) 2022 Microchip Technology Inc.
*/

+#include <linux/dsa/ksz_common.h>
#include <linux/kernel.h>
#include <linux/ptp_classify.h>
#include <linux/ptp_clock_kernel.h>
@@ -24,6 +25,27 @@
#define KSZ_PTP_INC_NS 40 /* HW clock is incremented every 40 ns (by 40) */
#define KSZ_PTP_SUBNS_BITS 32

+static int ksz_ptp_enable_mode(struct ksz_device *dev)
+{
+ struct ksz_tagger_data *tagger_data = ksz_tagger_data(dev->ds);
+ struct ksz_port *prt;
+ struct dsa_port *dp;
+ bool tag_en = false;
+
+ dsa_switch_for_each_user_port(dp, dev->ds) {
+ prt = &dev->ports[dp->index];
+ if (prt->hwts_tx_en || prt->hwts_rx_en) {
+ tag_en = true;
+ break;
+ }
+ }
+
+ tagger_data->hwtstamp_set_state(dev->ds, tag_en);
+
+ return ksz_rmw16(dev, REG_PTP_MSG_CONF1, PTP_ENABLE,
+ tag_en ? PTP_ENABLE : 0);
+}
+
/* The function is return back the capability of timestamping feature when
* requested through ethtool -T <interface> utility
*/
@@ -67,6 +89,7 @@ int ksz_hwtstamp_get(struct dsa_switch *ds, int port, struct ifreq *ifr)
}

static int ksz_set_hwtstamp_config(struct ksz_device *dev,
+ struct ksz_port *prt,
struct hwtstamp_config *config)
{
if (config->flags)
@@ -74,7 +97,10 @@ static int ksz_set_hwtstamp_config(struct ksz_device *dev,

switch (config->tx_type) {
case HWTSTAMP_TX_OFF:
+ prt->hwts_tx_en = false;
+ break;
case HWTSTAMP_TX_ONESTEP_P2P:
+ prt->hwts_tx_en = true;
break;
default:
return -ERANGE;
@@ -82,25 +108,29 @@ static int ksz_set_hwtstamp_config(struct ksz_device *dev,

switch (config->rx_filter) {
case HWTSTAMP_FILTER_NONE:
+ prt->hwts_rx_en = false;
break;
case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
config->rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
+ prt->hwts_rx_en = true;
break;
case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
config->rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
+ prt->hwts_rx_en = true;
break;
case HWTSTAMP_FILTER_PTP_V2_EVENT:
case HWTSTAMP_FILTER_PTP_V2_SYNC:
config->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
+ prt->hwts_rx_en = true;
break;
default:
config->rx_filter = HWTSTAMP_FILTER_NONE;
return -ERANGE;
}

- return 0;
+ return ksz_ptp_enable_mode(dev);
}

int ksz_hwtstamp_set(struct dsa_switch *ds, int port, struct ifreq *ifr)
@@ -116,7 +146,7 @@ int ksz_hwtstamp_set(struct dsa_switch *ds, int port, struct ifreq *ifr)
if (ret)
return ret;

- ret = ksz_set_hwtstamp_config(dev, &config);
+ ret = ksz_set_hwtstamp_config(dev, prt, &config);
if (ret)
return ret;

diff --git a/include/linux/dsa/ksz_common.h b/include/linux/dsa/ksz_common.h
new file mode 100644
index 000000000000..d2a54161be97
--- /dev/null
+++ b/include/linux/dsa/ksz_common.h
@@ -0,0 +1,22 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Microchip switch tag common header
+ *
+ * Copyright (C) 2022 Microchip Technology Inc.
+ */
+
+#ifndef _NET_DSA_KSZ_COMMON_H_
+#define _NET_DSA_KSZ_COMMON_H_
+
+#include <net/dsa.h>
+
+struct ksz_tagger_data {
+ void (*hwtstamp_set_state)(struct dsa_switch *ds, bool on);
+};
+
+static inline struct ksz_tagger_data *
+ksz_tagger_data(struct dsa_switch *ds)
+{
+ return ds->tagger_data;
+}
+
+#endif /* _NET_DSA_KSZ_COMMON_H_ */
diff --git a/net/dsa/tag_ksz.c b/net/dsa/tag_ksz.c
index 080e5c369f5b..420a12853676 100644
--- a/net/dsa/tag_ksz.c
+++ b/net/dsa/tag_ksz.c
@@ -4,6 +4,7 @@
* Copyright (c) 2017 Microchip Technology
*/

+#include <linux/dsa/ksz_common.h>
#include <linux/etherdevice.h>
#include <linux/list.h>
#include <net/dsa.h>
@@ -16,9 +17,58 @@
#define LAN937X_NAME "lan937x"

/* Typically only one byte is used for tail tag. */
+#define KSZ_PTP_TAG_LEN 4
#define KSZ_EGRESS_TAG_LEN 1
#define KSZ_INGRESS_TAG_LEN 1

+#define KSZ_HWTS_EN 0
+
+struct ksz_tagger_private {
+ struct ksz_tagger_data data; /* Must be first */
+ unsigned long state;
+};
+
+static struct ksz_tagger_private *
+ksz_tagger_private(struct dsa_switch *ds)
+{
+ return ds->tagger_data;
+}
+
+static void ksz_hwtstamp_set_state(struct dsa_switch *ds, bool on)
+{
+ struct ksz_tagger_private *priv = ksz_tagger_private(ds);
+
+ if (on)
+ set_bit(KSZ_HWTS_EN, &priv->state);
+ else
+ clear_bit(KSZ_HWTS_EN, &priv->state);
+}
+
+static void ksz_disconnect(struct dsa_switch *ds)
+{
+ struct ksz_tagger_private *priv = ds->tagger_data;
+
+ kfree(priv);
+ ds->tagger_data = NULL;
+}
+
+static int ksz_connect(struct dsa_switch *ds)
+{
+ struct ksz_tagger_data *tagger_data;
+ struct ksz_tagger_private *priv;
+
+ priv = kzalloc(sizeof(*priv), GFP_KERNEL);
+ if (!priv)
+ return -ENOMEM;
+
+ /* Export functions for switch driver use */
+ tagger_data = &priv->data;
+ tagger_data->hwtstamp_set_state = ksz_hwtstamp_set_state;
+ ds->tagger_data = priv;
+
+ return 0;
+}
+
static struct sk_buff *ksz_common_rcv(struct sk_buff *skb,
struct net_device *dev,
unsigned int port, unsigned int len)
@@ -92,10 +142,12 @@ DSA_TAG_DRIVER(ksz8795_netdev_ops);
MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_KSZ8795, KSZ8795_NAME);

/*
- * For Ingress (Host -> KSZ9477), 2 bytes are added before FCS.
+ * For Ingress (Host -> KSZ9477), 2/6 bytes are added before FCS.
* ---------------------------------------------------------------------------
- * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|tag0(1byte)|tag1(1byte)|FCS(4bytes)
+ * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|ts(4bytes)|tag0(1byte)|tag1(1byte)|
+ * FCS(4bytes)
* ---------------------------------------------------------------------------
+ * ts : time stamp (Present only if PTP is enabled in the Hardware)
* tag0 : Prioritization (not used now)
* tag1 : each bit represents port (eg, 0x01=port1, 0x02=port2, 0x10=port5)
*
@@ -114,6 +166,21 @@ MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_KSZ8795, KSZ8795_NAME);
#define KSZ9477_TAIL_TAG_OVERRIDE BIT(9)
#define KSZ9477_TAIL_TAG_LOOKUP BIT(10)

+/* Time stamp tag *needs* to be inserted if PTP is enabled in hardware.
+ * Regardless of Whether it is a PTP frame or not.
+ */
+static void ksz_xmit_timestamp(struct dsa_port *dp, struct sk_buff *skb)
+{
+ struct ksz_tagger_private *priv;
+
+ priv = ksz_tagger_private(dp->ds);
+
+ if (!test_bit(KSZ_HWTS_EN, &priv->state))
+ return;
+
+ put_unaligned_be32(0, skb_put(skb, KSZ_PTP_TAG_LEN));
+}
+
static struct sk_buff *ksz9477_xmit(struct sk_buff *skb,
struct net_device *dev)
{
@@ -126,6 +193,8 @@ static struct sk_buff *ksz9477_xmit(struct sk_buff *skb,
return NULL;

/* Tag encoding */
+ ksz_xmit_timestamp(dp, skb);
+
tag = skb_put(skb, KSZ9477_INGRESS_TAG_LEN);
addr = skb_mac_header(skb);

@@ -158,7 +227,9 @@ static const struct dsa_device_ops ksz9477_netdev_ops = {
.proto = DSA_TAG_PROTO_KSZ9477,
.xmit = ksz9477_xmit,
.rcv = ksz9477_rcv,
- .needed_tailroom = KSZ9477_INGRESS_TAG_LEN,
+ .connect = ksz_connect,
+ .disconnect = ksz_disconnect,
+ .needed_tailroom = KSZ9477_INGRESS_TAG_LEN + KSZ_PTP_TAG_LEN,
};

DSA_TAG_DRIVER(ksz9477_netdev_ops);
@@ -178,6 +249,8 @@ static struct sk_buff *ksz9893_xmit(struct sk_buff *skb,
return NULL;

/* Tag encoding */
+ ksz_xmit_timestamp(dp, skb);
+
tag = skb_put(skb, KSZ_INGRESS_TAG_LEN);
addr = skb_mac_header(skb);

@@ -194,16 +267,20 @@ static const struct dsa_device_ops ksz9893_netdev_ops = {
.proto = DSA_TAG_PROTO_KSZ9893,
.xmit = ksz9893_xmit,
.rcv = ksz9477_rcv,
- .needed_tailroom = KSZ_INGRESS_TAG_LEN,
+ .connect = ksz_connect,
+ .disconnect = ksz_disconnect,
+ .needed_tailroom = KSZ_INGRESS_TAG_LEN + KSZ_PTP_TAG_LEN,
};

DSA_TAG_DRIVER(ksz9893_netdev_ops);
MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_KSZ9893, KSZ9893_NAME);

-/* For xmit, 2 bytes are added before FCS.
+/* For xmit, 2/6 bytes are added before FCS.
* ---------------------------------------------------------------------------
- * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|tag0(1byte)|tag1(1byte)|FCS(4bytes)
+ * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|ts(4bytes)|tag0(1byte)|tag1(1byte)|
+ * FCS(4bytes)
* ---------------------------------------------------------------------------
+ * ts : time stamp (Present only if PTP is enabled in the Hardware)
* tag0 : represents tag override, lookup and valid
* tag1 : each bit represents port (eg, 0x01=port1, 0x02=port2, 0x80=port8)
*
@@ -232,6 +309,8 @@ static struct sk_buff *lan937x_xmit(struct sk_buff *skb,
if (skb->ip_summed == CHECKSUM_PARTIAL && skb_checksum_help(skb))
return NULL;

+ ksz_xmit_timestamp(dp, skb);
+
tag = skb_put(skb, LAN937X_EGRESS_TAG_LEN);

val = BIT(dp->index);
@@ -252,7 +331,9 @@ static const struct dsa_device_ops lan937x_netdev_ops = {
.proto = DSA_TAG_PROTO_LAN937X,
.xmit = lan937x_xmit,
.rcv = ksz9477_rcv,
- .needed_tailroom = LAN937X_EGRESS_TAG_LEN,
+ .connect = ksz_connect,
+ .disconnect = ksz_disconnect,
+ .needed_tailroom = LAN937X_EGRESS_TAG_LEN + KSZ_PTP_TAG_LEN,
};

DSA_TAG_DRIVER(lan937x_netdev_ops);
--
2.36.1

2022-12-09 07:54:28

by Arun Ramadoss

[permalink] [raw]
Subject: [Patch net-next v3 10/13] net: dsa: microchip: ptp: add periodic output signal

From: Christian Eggers <[email protected]>

LAN937x and KSZ PTP supported switches has Three Trigger output unit.
This TOU can used to generate the periodic signal for PTP. TOU has the
cycle width register of 32 bit in size and period width register of 24
bit, each value is of 8ns so the pulse width can be maximum 125ms.

Tested using ./testptp -d /dev/ptp0 -p 1000000000 -w 100000000 for
generating the 10ms pulse width

Signed-off-by: Christian Eggers <[email protected]>
Co-developed-by: Arun Ramadoss <[email protected]>
Signed-off-by: Arun Ramadoss <[email protected]>
---
v1 -> v2
- In ksz_ptp_enable function, removed the check request->index since it
is handled in upper layer. For the default case use -EOPNOSUPP instead
of -EINVAL.
---
drivers/net/dsa/microchip/ksz_common.h | 13 ++
drivers/net/dsa/microchip/ksz_ptp.c | 296 ++++++++++++++++++++++++
drivers/net/dsa/microchip/ksz_ptp.h | 8 +
drivers/net/dsa/microchip/ksz_ptp_reg.h | 63 +++++
4 files changed, 380 insertions(+)

diff --git a/drivers/net/dsa/microchip/ksz_common.h b/drivers/net/dsa/microchip/ksz_common.h
index 6809e1a76037..ecc186c857aa 100644
--- a/drivers/net/dsa/microchip/ksz_common.h
+++ b/drivers/net/dsa/microchip/ksz_common.h
@@ -477,6 +477,19 @@ static inline int ksz_rmw16(struct ksz_device *dev, u32 reg, u16 mask,
return ret;
}

+static inline int ksz_rmw32(struct ksz_device *dev, u32 reg, u32 mask,
+ u32 value)
+{
+ int ret;
+
+ ret = regmap_update_bits(dev->regmap[2], reg, mask, value);
+ if (ret)
+ dev_err(dev->dev, "can't rmw 32bit reg 0x%x: %pe\n", reg,
+ ERR_PTR(ret));
+
+ return ret;
+}
+
static inline int ksz_write64(struct ksz_device *dev, u32 reg, u64 value)
{
u32 val[2];
diff --git a/drivers/net/dsa/microchip/ksz_ptp.c b/drivers/net/dsa/microchip/ksz_ptp.c
index c9da2a735165..6d7edc81909e 100644
--- a/drivers/net/dsa/microchip/ksz_ptp.c
+++ b/drivers/net/dsa/microchip/ksz_ptp.c
@@ -31,6 +31,252 @@

#define KSZ_PTP_INT_START 13

+static int _ksz_ptp_gettime(struct ksz_device *dev, struct timespec64 *ts);
+
+static int ksz_ptp_tou_reset(struct ksz_device *dev, u8 unit)
+{
+ u32 data;
+ int ret;
+
+ /* Reset trigger unit (clears TRIGGER_EN, but not GPIOSTATx) */
+ ret = ksz_rmw32(dev, REG_PTP_CTRL_STAT__4, TRIG_RESET, TRIG_RESET);
+
+ data = FIELD_PREP(TRIG_DONE_M, BIT(unit));
+ ret = ksz_write32(dev, REG_PTP_TRIG_STATUS__4, data);
+ if (ret)
+ return ret;
+
+ data = FIELD_PREP(TRIG_INT_M, BIT(unit));
+ ret = ksz_write32(dev, REG_PTP_INT_STATUS__4, data);
+ if (ret)
+ return ret;
+
+ /* Clear reset and set GPIO direction */
+ return ksz_rmw32(dev, REG_PTP_CTRL_STAT__4, (TRIG_RESET | TRIG_ENABLE),
+ 0);
+}
+
+static int ksz_ptp_tou_pulse_verify(u64 pulse_ns)
+{
+ u32 data;
+
+ if (pulse_ns & 0x3)
+ return -EINVAL;
+
+ data = (pulse_ns / 8);
+ if (!FIELD_FIT(TRIG_PULSE_WIDTH_M, data))
+ return -ERANGE;
+
+ return 0;
+}
+
+static int ksz_ptp_tou_target_time_set(struct ksz_device *dev,
+ struct timespec64 const *ts)
+{
+ int ret;
+
+ /* Hardware has only 32 bit */
+ if ((ts->tv_sec & 0xffffffff) != ts->tv_sec)
+ return -EINVAL;
+
+ ret = ksz_write32(dev, REG_TRIG_TARGET_NANOSEC, ts->tv_nsec);
+ if (ret)
+ return ret;
+
+ ret = ksz_write32(dev, REG_TRIG_TARGET_SEC, ts->tv_sec);
+ if (ret)
+ return ret;
+
+ return 0;
+}
+
+static int ksz_ptp_tou_start(struct ksz_device *dev, u8 unit)
+{
+ u32 data;
+ int ret;
+
+ ret = ksz_rmw32(dev, REG_PTP_CTRL_STAT__4, TRIG_ENABLE, TRIG_ENABLE);
+ if (ret)
+ return ret;
+
+ /* Check error flag:
+ * - the ACTIVE flag is NOT cleared an error!
+ */
+ ret = ksz_read32(dev, REG_PTP_TRIG_STATUS__4, &data);
+ if (ret)
+ return ret;
+
+ if (FIELD_GET(TRIG_ERROR_M, data) & (1 << unit)) {
+ dev_err(dev->dev, "%s: Trigger unit%d error!\n", __func__,
+ unit);
+ ret = -EIO;
+ /* Unit will be reset on next access */
+ return ret;
+ }
+
+ return 0;
+}
+
+static int ksz_ptp_configure_perout(struct ksz_device *dev,
+ u32 cycle_width_ns, u32 pulse_width_ns,
+ struct timespec64 const *target_time,
+ u8 index)
+{
+ u32 data;
+ int ret;
+
+ data = FIELD_PREP(TRIG_NOTIFY, 1) |
+ FIELD_PREP(TRIG_GPO_M, index) |
+ FIELD_PREP(TRIG_PATTERN_M, TRIG_POS_PERIOD);
+ ret = ksz_write32(dev, REG_TRIG_CTRL__4, data);
+ if (ret)
+ return ret;
+
+ ret = ksz_write32(dev, REG_TRIG_CYCLE_WIDTH, cycle_width_ns);
+ if (ret)
+ return ret;
+
+ /* Set cycle count 0 - Infinite */
+ ret = ksz_rmw32(dev, REG_TRIG_CYCLE_CNT, TRIG_CYCLE_CNT_M, 0);
+ if (ret)
+ return ret;
+
+ data = (pulse_width_ns / 8);
+ ret = ksz_write32(dev, REG_TRIG_PULSE_WIDTH__4, data);
+ if (ret)
+ return ret;
+
+ ret = ksz_ptp_tou_target_time_set(dev, target_time);
+ if (ret)
+ return ret;
+
+ return 0;
+}
+
+#define KSZ_PEROUT_VALID_FLAGS ( \
+ PTP_PEROUT_DUTY_CYCLE \
+ )
+
+static int ksz_ptp_enable_perout(struct ksz_device *dev,
+ struct ptp_perout_request const *request,
+ int on)
+{
+ struct ksz_ptp_data *ptp_data = &dev->ptp_data;
+ u64 cycle_width_ns;
+ u64 pulse_width_ns;
+ int pin = 0;
+ u32 data32;
+ int ret;
+
+ if (request->flags & ~KSZ_PEROUT_VALID_FLAGS)
+ return -EINVAL;
+
+ if (ptp_data->tou_mode != KSZ_PTP_TOU_PEROUT &&
+ ptp_data->tou_mode != KSZ_PTP_TOU_IDLE)
+ return -EBUSY;
+
+ data32 = FIELD_PREP(PTP_GPIO_INDEX, pin) |
+ FIELD_PREP(PTP_TOU_INDEX, request->index);
+ ret = ksz_rmw32(dev, REG_PTP_UNIT_INDEX__4,
+ PTP_GPIO_INDEX | PTP_TOU_INDEX, data32);
+ if (ret)
+ return ret;
+
+ ret = ksz_ptp_tou_reset(dev, request->index);
+ if (ret)
+ return ret;
+
+ if (!on) {
+ ptp_data->tou_mode = KSZ_PTP_TOU_IDLE;
+ return 0;
+ }
+
+ ptp_data->perout_target_time_first.tv_sec = request->start.sec;
+ ptp_data->perout_target_time_first.tv_nsec = request->start.nsec;
+
+ ptp_data->perout_period.tv_sec = request->period.sec;
+ ptp_data->perout_period.tv_nsec = request->period.nsec;
+
+ cycle_width_ns = timespec64_to_ns(&ptp_data->perout_period);
+ if ((cycle_width_ns & TRIG_CYCLE_WIDTH_M) != cycle_width_ns)
+ return -EINVAL;
+
+ if (request->flags & PTP_PEROUT_DUTY_CYCLE) {
+ pulse_width_ns = request->on.sec * NSEC_PER_SEC +
+ request->on.nsec;
+ } else {
+ /* Use a duty cycle of 50%. Maximum pulse width supported by the
+ * hardware is a little bit more than 125 ms.
+ */
+ pulse_width_ns = min_t(u64,
+ (request->period.sec * NSEC_PER_SEC
+ + request->period.nsec) / 2
+ / 8 * 8,
+ 125000000LL);
+ }
+
+ ret = ksz_ptp_tou_pulse_verify(pulse_width_ns);
+ if (ret)
+ return ret;
+
+ ret = ksz_ptp_configure_perout(dev, cycle_width_ns, pulse_width_ns,
+ &ptp_data->perout_target_time_first,
+ pin);
+ if (ret)
+ return ret;
+
+ ret = ksz_ptp_tou_start(dev, request->index);
+ if (ret)
+ return ret;
+
+ ptp_data->tou_mode = KSZ_PTP_TOU_PEROUT;
+
+ return 0;
+}
+
+static int ksz_ptp_restart_perout(struct ksz_device *dev)
+{
+ struct ksz_ptp_data *ptp_data = &dev->ptp_data;
+ s64 now_ns, first_ns, period_ns, next_ns;
+ struct ptp_perout_request request;
+ struct timespec64 next;
+ struct timespec64 now;
+ unsigned int count;
+ int ret;
+
+ ret = _ksz_ptp_gettime(dev, &now);
+ if (ret)
+ return ret;
+
+ now_ns = timespec64_to_ns(&now);
+ first_ns = timespec64_to_ns(&ptp_data->perout_target_time_first);
+
+ /* Calculate next perout event based on start time and period */
+ period_ns = timespec64_to_ns(&ptp_data->perout_period);
+
+ if (first_ns < now_ns) {
+ count = div_u64(now_ns - first_ns, period_ns);
+ next_ns = first_ns + count * period_ns;
+ } else {
+ next_ns = first_ns;
+ }
+
+ /* Ensure 100 ms guard time prior next event */
+ while (next_ns < now_ns + 100000000)
+ next_ns += period_ns;
+
+ /* Restart periodic output signal */
+ next = ns_to_timespec64(next_ns);
+ request.start.sec = next.tv_sec;
+ request.start.nsec = next.tv_nsec;
+ request.period.sec = ptp_data->perout_period.tv_sec;
+ request.period.nsec = ptp_data->perout_period.tv_nsec;
+ request.index = 0;
+ request.flags = 0;
+
+ return ksz_ptp_enable_perout(dev, &request, 1);
+}
+
static int ksz_ptp_enable_mode(struct ksz_device *dev)
{
struct ksz_tagger_data *tagger_data = ksz_tagger_data(dev->ds);
@@ -396,6 +642,20 @@ static int ksz_ptp_settime(struct ptp_clock_info *ptp,
if (ret)
goto unlock;

+ switch (ptp_data->tou_mode) {
+ case KSZ_PTP_TOU_IDLE:
+ break;
+
+ case KSZ_PTP_TOU_PEROUT:
+ dev_info(dev->dev, "Restarting periodic output signal\n");
+
+ ret = ksz_ptp_restart_perout(dev);
+ if (ret)
+ goto unlock;
+
+ break;
+ }
+
spin_lock_bh(&ptp_data->clock_lock);
ptp_data->clock_time = *ts;
spin_unlock_bh(&ptp_data->clock_lock);
@@ -489,6 +749,20 @@ static int ksz_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
if (ret)
goto unlock;

+ switch (ptp_data->tou_mode) {
+ case KSZ_PTP_TOU_IDLE:
+ break;
+
+ case KSZ_PTP_TOU_PEROUT:
+ dev_info(dev->dev, "Restarting periodic output signal\n");
+
+ ret = ksz_ptp_restart_perout(dev);
+ if (ret)
+ goto unlock;
+
+ break;
+ }
+
spin_lock_bh(&ptp_data->clock_lock);
ptp_data->clock_time = timespec64_add(ptp_data->clock_time, delta64);
spin_unlock_bh(&ptp_data->clock_lock);
@@ -498,6 +772,26 @@ static int ksz_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
return ret;
}

+static int ksz_ptp_enable(struct ptp_clock_info *ptp,
+ struct ptp_clock_request *req, int on)
+{
+ struct ksz_ptp_data *ptp_data = ptp_caps_to_data(ptp);
+ struct ksz_device *dev = ptp_data_to_ksz_dev(ptp_data);
+ int ret;
+
+ switch (req->type) {
+ case PTP_CLK_REQ_PEROUT:
+ mutex_lock(&ptp_data->lock);
+ ret = ksz_ptp_enable_perout(dev, &req->perout, on);
+ mutex_unlock(&ptp_data->lock);
+ break;
+ default:
+ return -EOPNOTSUPP;
+ }
+
+ return ret;
+}
+
/* Function is pointer to the do_aux_work in the ptp_clock capability */
static long ksz_ptp_do_aux_work(struct ptp_clock_info *ptp)
{
@@ -546,6 +840,8 @@ int ksz_ptp_clock_register(struct dsa_switch *ds)
ptp_data->caps.adjfine = ksz_ptp_adjfine;
ptp_data->caps.adjtime = ksz_ptp_adjtime;
ptp_data->caps.do_aux_work = ksz_ptp_do_aux_work;
+ ptp_data->caps.enable = ksz_ptp_enable;
+ ptp_data->caps.n_per_out = 3;

ret = ksz_ptp_start_clock(dev);
if (ret)
diff --git a/drivers/net/dsa/microchip/ksz_ptp.h b/drivers/net/dsa/microchip/ksz_ptp.h
index 0b14aed71ec2..9451e3a76375 100644
--- a/drivers/net/dsa/microchip/ksz_ptp.h
+++ b/drivers/net/dsa/microchip/ksz_ptp.h
@@ -12,6 +12,11 @@

#include <linux/ptp_clock_kernel.h>

+enum ksz_ptp_tou_mode {
+ KSZ_PTP_TOU_IDLE,
+ KSZ_PTP_TOU_PEROUT,
+};
+
struct ksz_ptp_data {
struct ptp_clock_info caps;
struct ptp_clock *clock;
@@ -20,6 +25,9 @@ struct ksz_ptp_data {
/* lock for accessing the clock_time */
spinlock_t clock_lock;
struct timespec64 clock_time;
+ enum ksz_ptp_tou_mode tou_mode;
+ struct timespec64 perout_target_time_first; /* start of first pulse */
+ struct timespec64 perout_period;
};

int ksz_ptp_clock_register(struct dsa_switch *ds);
diff --git a/drivers/net/dsa/microchip/ksz_ptp_reg.h b/drivers/net/dsa/microchip/ksz_ptp_reg.h
index abe95bbefc12..dbccfedf89e4 100644
--- a/drivers/net/dsa/microchip/ksz_ptp_reg.h
+++ b/drivers/net/dsa/microchip/ksz_ptp_reg.h
@@ -49,6 +49,69 @@
#define PTP_MASTER BIT(1)
#define PTP_1STEP BIT(0)

+#define REG_PTP_UNIT_INDEX__4 0x0520
+
+#define PTP_GPIO_INDEX GENMASK(19, 16)
+#define PTP_TSI_INDEX BIT(8)
+#define PTP_TOU_INDEX GENMASK(1, 0)
+
+#define REG_PTP_TRIG_STATUS__4 0x0524
+
+#define TRIG_ERROR_M GENMASK(18, 16)
+#define TRIG_DONE_M GENMASK(2, 0)
+
+#define REG_PTP_INT_STATUS__4 0x0528
+
+#define TRIG_INT_M GENMASK(18, 16)
+#define TS_INT_M GENMASK(1, 0)
+
+#define REG_PTP_CTRL_STAT__4 0x052C
+
+#define GPIO_IN BIT(7)
+#define GPIO_OUT BIT(6)
+#define TS_INT_ENABLE BIT(5)
+#define TRIG_ACTIVE BIT(4)
+#define TRIG_ENABLE BIT(3)
+#define TRIG_RESET BIT(2)
+#define TS_ENABLE BIT(1)
+#define TS_RESET BIT(0)
+
+#define REG_TRIG_TARGET_NANOSEC 0x0530
+#define REG_TRIG_TARGET_SEC 0x0534
+
+#define REG_TRIG_CTRL__4 0x0538
+
+#define TRIG_CASCADE_ENABLE BIT(31)
+#define TRIG_CASCADE_TAIL BIT(30)
+#define TRIG_CASCADE_UPS_M GENMASK(29, 26)
+#define TRIG_NOW BIT(25)
+#define TRIG_NOTIFY BIT(24)
+#define TRIG_EDGE BIT(23)
+#define TRIG_PATTERN_M GENMASK(22, 20)
+#define TRIG_NEG_EDGE 0
+#define TRIG_POS_EDGE 1
+#define TRIG_NEG_PULSE 2
+#define TRIG_POS_PULSE 3
+#define TRIG_NEG_PERIOD 4
+#define TRIG_POS_PERIOD 5
+#define TRIG_REG_OUTPUT 6
+#define TRIG_GPO_M GENMASK(19, 16)
+#define TRIG_CASCADE_ITERATE_CNT_M GENMASK(15, 0)
+
+#define REG_TRIG_CYCLE_WIDTH 0x053C
+#define TRIG_CYCLE_WIDTH_M GENMASK(31, 0)
+
+#define REG_TRIG_CYCLE_CNT 0x0540
+
+#define TRIG_CYCLE_CNT_M GENMASK(31, 16)
+#define TRIG_BIT_PATTERN_M GENMASK(15, 0)
+
+#define REG_TRIG_ITERATE_TIME 0x0544
+
+#define REG_TRIG_PULSE_WIDTH__4 0x0548
+
+#define TRIG_PULSE_WIDTH_M GENMASK(23, 0)
+
/* Port PTP Register */
#define REG_PTP_PORT_RX_DELAY__2 0x0C00
#define REG_PTP_PORT_TX_DELAY__2 0x0C02
--
2.36.1

2022-12-09 08:08:51

by Arun Ramadoss

[permalink] [raw]
Subject: [Patch net-next v3 07/13] net: dsa: microchip: ptp: add packet reception timestamping

From: Christian Eggers <[email protected]>

Rx Timestamping is done through 4 additional bytes in tail tag.
Whenever the ptp packet is received, the 4 byte hardware time stamped
value is added before 1 byte tail tag. Also, bit 7 in tail tag indicates
it as PTP frame. This 4 byte value is extracted from the tail tag and
reconstructed to absolute time and assigned to skb hwtstamp.
If the packet received in PDelay_Resp, then partial ingress timestamp
is subtracted from the correction field. Since user space tools expects
to be done in hardware.

Signed-off-by: Christian Eggers <[email protected]>
Co-developed-by: Arun Ramadoss <[email protected]>
Signed-off-by: Arun Ramadoss <[email protected]>

---
v2 - v3
- Replaced tagger_data->meta_timestamper handler with port_rxtstamp
routine

v1 - v2
- Checkpatch warning line limit to 80chars

RFC v2 -> Patch v1
- Fixed compilation issue
---
drivers/net/dsa/microchip/ksz_common.c | 1 +
drivers/net/dsa/microchip/ksz_ptp.c | 63 ++++++++++++++++++++++++++
drivers/net/dsa/microchip/ksz_ptp.h | 4 ++
include/linux/dsa/ksz_common.h | 21 +++++++++
net/dsa/tag_ksz.c | 25 +++++++---
5 files changed, 108 insertions(+), 6 deletions(-)

diff --git a/drivers/net/dsa/microchip/ksz_common.c b/drivers/net/dsa/microchip/ksz_common.c
index 1611f8f5cd6c..127fdbc25141 100644
--- a/drivers/net/dsa/microchip/ksz_common.c
+++ b/drivers/net/dsa/microchip/ksz_common.c
@@ -2992,6 +2992,7 @@ static const struct dsa_switch_ops ksz_switch_ops = {
.get_ts_info = ksz_get_ts_info,
.port_hwtstamp_get = ksz_hwtstamp_get,
.port_hwtstamp_set = ksz_hwtstamp_set,
+ .port_rxtstamp = ksz_port_rxtstamp,
};

struct ksz_device *ksz_switch_alloc(struct device *base, void *priv)
diff --git a/drivers/net/dsa/microchip/ksz_ptp.c b/drivers/net/dsa/microchip/ksz_ptp.c
index a7b015ac2734..e9c2442eec71 100644
--- a/drivers/net/dsa/microchip/ksz_ptp.c
+++ b/drivers/net/dsa/microchip/ksz_ptp.c
@@ -169,6 +169,69 @@ int ksz_hwtstamp_set(struct dsa_switch *ds, int port, struct ifreq *ifr)
return copy_to_user(ifr->ifr_data, &config, sizeof(config));
}

+static ktime_t ksz_tstamp_reconstruct(struct ksz_device *dev, ktime_t tstamp)
+{
+ struct timespec64 ptp_clock_time;
+ struct ksz_ptp_data *ptp_data;
+ struct timespec64 diff;
+ struct timespec64 ts;
+
+ ptp_data = &dev->ptp_data;
+ ts = ktime_to_timespec64(tstamp);
+
+ spin_lock_bh(&ptp_data->clock_lock);
+ ptp_clock_time = ptp_data->clock_time;
+ spin_unlock_bh(&ptp_data->clock_lock);
+
+ /* calculate full time from partial time stamp */
+ ts.tv_sec = (ptp_clock_time.tv_sec & ~3) | ts.tv_sec;
+
+ /* find nearest possible point in time */
+ diff = timespec64_sub(ts, ptp_clock_time);
+ if (diff.tv_sec > 2)
+ ts.tv_sec -= 4;
+ else if (diff.tv_sec < -2)
+ ts.tv_sec += 4;
+
+ return timespec64_to_ktime(ts);
+}
+
+bool ksz_port_rxtstamp(struct dsa_switch *ds, int port, struct sk_buff *skb,
+ unsigned int type)
+{
+ struct skb_shared_hwtstamps *hwtstamps = skb_hwtstamps(skb);
+ struct ksz_device *dev = ds->priv;
+ struct ptp_header *ptp_hdr;
+ u8 ptp_msg_type;
+ ktime_t tstamp;
+ s64 correction;
+
+ tstamp = KSZ_SKB_CB(skb)->tstamp;
+ memset(hwtstamps, 0, sizeof(*hwtstamps));
+ hwtstamps->hwtstamp = ksz_tstamp_reconstruct(dev, tstamp);
+
+ ptp_hdr = ptp_parse_header(skb, type);
+ if (!ptp_hdr)
+ goto out;
+
+ ptp_msg_type = ptp_get_msgtype(ptp_hdr, type);
+ if (ptp_msg_type != PTP_MSGTYPE_PDELAY_REQ)
+ goto out;
+
+ /* Only subtract the partial time stamp from the correction field. When
+ * the hardware adds the egress time stamp to the correction field of
+ * the PDelay_Resp message on tx, also only the partial time stamp will
+ * be added.
+ */
+ correction = (s64)get_unaligned_be64(&ptp_hdr->correction);
+ correction -= ktime_to_ns(tstamp) << 16;
+
+ ptp_header_update_correction(skb, type, ptp_hdr, correction);
+
+out:
+ return 0;
+}
+
static int _ksz_ptp_gettime(struct ksz_device *dev, struct timespec64 *ts)
{
u32 nanoseconds;
diff --git a/drivers/net/dsa/microchip/ksz_ptp.h b/drivers/net/dsa/microchip/ksz_ptp.h
index 7c5679372705..9bb8fb059ac2 100644
--- a/drivers/net/dsa/microchip/ksz_ptp.h
+++ b/drivers/net/dsa/microchip/ksz_ptp.h
@@ -30,6 +30,8 @@ int ksz_get_ts_info(struct dsa_switch *ds, int port,
struct ethtool_ts_info *ts);
int ksz_hwtstamp_get(struct dsa_switch *ds, int port, struct ifreq *ifr);
int ksz_hwtstamp_set(struct dsa_switch *ds, int port, struct ifreq *ifr);
+bool ksz_port_rxtstamp(struct dsa_switch *ds, int port, struct sk_buff *skb,
+ unsigned int type);
int ksz_ptp_irq_setup(struct dsa_switch *ds, u8 p);
void ksz_ptp_irq_free(struct dsa_switch *ds, u8 p);

@@ -60,6 +62,8 @@ static inline void ksz_ptp_irq_free(struct dsa_switch *ds, u8 p) {}

#define ksz_hwtstamp_set NULL

+#define ksz_port_rxtstamp NULL
+
#endif /* End of CONFIG_NET_DSA_MICROCHIP_KSZ_PTP */

#endif
diff --git a/include/linux/dsa/ksz_common.h b/include/linux/dsa/ksz_common.h
index d2a54161be97..a256b08d837d 100644
--- a/include/linux/dsa/ksz_common.h
+++ b/include/linux/dsa/ksz_common.h
@@ -9,10 +9,31 @@

#include <net/dsa.h>

+/* All time stamps from the KSZ consist of 2 bits for seconds and 30 bits for
+ * nanoseconds. This is NOT the same as 32 bits for nanoseconds.
+ */
+#define KSZ_TSTAMP_SEC_MASK GENMASK(31, 30)
+#define KSZ_TSTAMP_NSEC_MASK GENMASK(29, 0)
+
+static inline ktime_t ksz_decode_tstamp(u32 tstamp)
+{
+ u64 ns = FIELD_GET(KSZ_TSTAMP_SEC_MASK, tstamp) * NSEC_PER_SEC +
+ FIELD_GET(KSZ_TSTAMP_NSEC_MASK, tstamp);
+
+ return ns_to_ktime(ns);
+}
+
struct ksz_tagger_data {
void (*hwtstamp_set_state)(struct dsa_switch *ds, bool on);
};

+struct ksz_skb_cb {
+ u32 tstamp;
+};
+
+#define KSZ_SKB_CB(skb) \
+ ((struct ksz_skb_cb *)((skb)->cb))
+
static inline struct ksz_tagger_data *
ksz_tagger_data(struct dsa_switch *ds)
{
diff --git a/net/dsa/tag_ksz.c b/net/dsa/tag_ksz.c
index 420a12853676..6603eaa234d2 100644
--- a/net/dsa/tag_ksz.c
+++ b/net/dsa/tag_ksz.c
@@ -151,10 +151,11 @@ MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_KSZ8795, KSZ8795_NAME);
* tag0 : Prioritization (not used now)
* tag1 : each bit represents port (eg, 0x01=port1, 0x02=port2, 0x10=port5)
*
- * For Egress (KSZ9477 -> Host), 1 byte is added before FCS.
+ * For Egress (KSZ9477 -> Host), 1/5 bytes is added before FCS.
* ---------------------------------------------------------------------------
- * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|tag0(1byte)|FCS(4bytes)
+ * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|ts(4bytes)|tag0(1byte)|FCS(4bytes)
* ---------------------------------------------------------------------------
+ * ts : time stamp (Present only if bit 7 of tag0 is set)
* tag0 : zero-based value represents port
* (eg, 0x00=port1, 0x02=port3, 0x06=port7)
*/
@@ -166,6 +167,15 @@ MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_KSZ8795, KSZ8795_NAME);
#define KSZ9477_TAIL_TAG_OVERRIDE BIT(9)
#define KSZ9477_TAIL_TAG_LOOKUP BIT(10)

+static void ksz_rcv_timestamp(struct sk_buff *skb, u8 *tag)
+{
+ u8 *tstamp_raw = tag - KSZ_PTP_TAG_LEN;
+ ktime_t tstamp;
+
+ tstamp = ksz_decode_tstamp(get_unaligned_be32(tstamp_raw));
+ KSZ_SKB_CB(skb)->tstamp = tstamp;
+}
+
/* Time stamp tag *needs* to be inserted if PTP is enabled in hardware.
* Regardless of Whether it is a PTP frame or not.
*/
@@ -216,8 +226,10 @@ static struct sk_buff *ksz9477_rcv(struct sk_buff *skb, struct net_device *dev)
unsigned int len = KSZ_EGRESS_TAG_LEN;

/* Extra 4-bytes PTP timestamp */
- if (tag[0] & KSZ9477_PTP_TAG_INDICATION)
- len += KSZ9477_PTP_TAG_LEN;
+ if (tag[0] & KSZ9477_PTP_TAG_INDICATION) {
+ ksz_rcv_timestamp(skb, tag);
+ len += KSZ_PTP_TAG_LEN;
+ }

return ksz_common_rcv(skb, dev, port, len);
}
@@ -284,10 +296,11 @@ MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_KSZ9893, KSZ9893_NAME);
* tag0 : represents tag override, lookup and valid
* tag1 : each bit represents port (eg, 0x01=port1, 0x02=port2, 0x80=port8)
*
- * For rcv, 1 byte is added before FCS.
+ * For rcv, 1/5 bytes is added before FCS.
* ---------------------------------------------------------------------------
- * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|tag0(1byte)|FCS(4bytes)
+ * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|ts(4bytes)|tag0(1byte)|FCS(4bytes)
* ---------------------------------------------------------------------------
+ * ts : time stamp (Present only if bit 7 of tag0 is set)
* tag0 : zero-based value represents port
* (eg, 0x00=port1, 0x02=port3, 0x07=port8)
*/
--
2.36.1

2022-12-09 08:09:05

by Arun Ramadoss

[permalink] [raw]
Subject: [Patch net-next v3 05/13] net: dsa: microchip: ptp: enable interrupt for timestamping

PTP Interrupt mask and status register differ from the global and port
interrupt mechanism by two methods. One is that for global/port
interrupt enabling we have to clear the bit but for ptp interrupt we
have to set the bit. And other is bit12:0 is reserved in ptp interrupt
registers. This forced to not use the generic implementation of
global/port interrupt method routine. This patch implement the ptp
interrupt mechanism to read the timestamp register for sync, pdelay_req
and pdelay_resp.

Signed-off-by: Arun Ramadoss <[email protected]>

---
RFC v2 -> Patch v1
- Moved the acking of interrupts before calling the handle_nested_irq to
avoid race condition between deferred xmit and Irq threads
---
drivers/net/dsa/microchip/ksz_common.c | 15 +-
drivers/net/dsa/microchip/ksz_common.h | 11 ++
drivers/net/dsa/microchip/ksz_ptp.c | 200 ++++++++++++++++++++++++
drivers/net/dsa/microchip/ksz_ptp.h | 9 ++
drivers/net/dsa/microchip/ksz_ptp_reg.h | 19 +++
5 files changed, 252 insertions(+), 2 deletions(-)

diff --git a/drivers/net/dsa/microchip/ksz_common.c b/drivers/net/dsa/microchip/ksz_common.c
index 81da650b70fb..1611f8f5cd6c 100644
--- a/drivers/net/dsa/microchip/ksz_common.c
+++ b/drivers/net/dsa/microchip/ksz_common.c
@@ -2101,13 +2101,17 @@ static int ksz_setup(struct dsa_switch *ds)
ret = ksz_pirq_setup(dev, dp->index);
if (ret)
goto out_girq;
+
+ ret = ksz_ptp_irq_setup(ds, dp->index);
+ if (ret)
+ goto out_pirq;
}
}

ret = ksz_ptp_clock_register(ds);
if (ret) {
dev_err(dev->dev, "Failed to register PTP clock: %d\n", ret);
- goto out_pirq;
+ goto out_ptpirq;
}

ret = ksz_mdio_register(dev);
@@ -2124,6 +2128,10 @@ static int ksz_setup(struct dsa_switch *ds)

out_ptp_clock_unregister:
ksz_ptp_clock_unregister(ds);
+out_ptpirq:
+ if (dev->irq > 0)
+ dsa_switch_for_each_user_port(dp, dev->ds)
+ ksz_ptp_irq_free(ds, dp->index);
out_pirq:
if (dev->irq > 0)
dsa_switch_for_each_user_port(dp, dev->ds)
@@ -2143,8 +2151,11 @@ static void ksz_teardown(struct dsa_switch *ds)
ksz_ptp_clock_unregister(ds);

if (dev->irq > 0) {
- dsa_switch_for_each_user_port(dp, dev->ds)
+ dsa_switch_for_each_user_port(dp, dev->ds) {
+ ksz_ptp_irq_free(ds, dp->index);
+
ksz_irq_free(&dev->ports[dp->index].pirq);
+ }

ksz_irq_free(&dev->girq);
}
diff --git a/drivers/net/dsa/microchip/ksz_common.h b/drivers/net/dsa/microchip/ksz_common.h
index 641aca78ef05..918028db2e6f 100644
--- a/drivers/net/dsa/microchip/ksz_common.h
+++ b/drivers/net/dsa/microchip/ksz_common.h
@@ -20,6 +20,7 @@
#define KSZ_MAX_NUM_PORTS 8

struct ksz_device;
+struct ksz_port;

struct vlan_table {
u32 table[3];
@@ -83,6 +84,13 @@ struct ksz_irq {
struct ksz_device *dev;
};

+struct ksz_ptp_irq {
+ struct ksz_port *port;
+ u16 ts_reg;
+ char name[16];
+ int irq_num;
+};
+
struct ksz_port {
bool remove_tag; /* Remove Tag flag set, for ksz8795 only */
bool learning;
@@ -106,6 +114,8 @@ struct ksz_port {
struct hwtstamp_config tstamp_config;
bool hwts_tx_en;
bool hwts_rx_en;
+ struct ksz_irq ptpirq;
+ struct ksz_ptp_irq ptpmsg_irq[3];
#endif
};

@@ -612,6 +622,7 @@ static inline int is_lan937x(struct ksz_device *dev)
#define REG_PORT_INT_MASK 0x001F

#define PORT_SRC_PHY_INT 1
+#define PORT_SRC_PTP_INT 2

#define KSZ8795_HUGE_PACKET_SIZE 2000
#define KSZ8863_HUGE_PACKET_SIZE 1916
diff --git a/drivers/net/dsa/microchip/ksz_ptp.c b/drivers/net/dsa/microchip/ksz_ptp.c
index b864b88dc6f9..a7b015ac2734 100644
--- a/drivers/net/dsa/microchip/ksz_ptp.c
+++ b/drivers/net/dsa/microchip/ksz_ptp.c
@@ -6,6 +6,8 @@
*/

#include <linux/dsa/ksz_common.h>
+#include <linux/irq.h>
+#include <linux/irqdomain.h>
#include <linux/kernel.h>
#include <linux/ptp_classify.h>
#include <linux/ptp_clock_kernel.h>
@@ -25,6 +27,8 @@
#define KSZ_PTP_INC_NS 40 /* HW clock is incremented every 40 ns (by 40) */
#define KSZ_PTP_SUBNS_BITS 32

+#define KSZ_PTP_INT_START 13
+
static int ksz_ptp_enable_mode(struct ksz_device *dev)
{
struct ksz_tagger_data *tagger_data = ksz_tagger_data(dev->ds);
@@ -415,6 +419,202 @@ void ksz_ptp_clock_unregister(struct dsa_switch *ds)
ptp_clock_unregister(ptp_data->clock);
}

+static irqreturn_t ksz_ptp_msg_thread_fn(int irq, void *dev_id)
+{
+ return IRQ_NONE;
+}
+
+static irqreturn_t ksz_ptp_irq_thread_fn(int irq, void *dev_id)
+{
+ struct ksz_irq *ptpirq = dev_id;
+ unsigned int nhandled = 0;
+ struct ksz_device *dev;
+ unsigned int sub_irq;
+ u16 data;
+ int ret;
+ u8 n;
+
+ dev = ptpirq->dev;
+
+ ret = ksz_read16(dev, ptpirq->reg_status, &data);
+ if (ret)
+ goto out;
+
+ /* Clear the interrupts W1C */
+ ret = ksz_write16(dev, ptpirq->reg_status, data);
+ if (ret)
+ return IRQ_NONE;
+
+ for (n = 0; n < ptpirq->nirqs; ++n) {
+ if (data & BIT(n + KSZ_PTP_INT_START)) {
+ sub_irq = irq_find_mapping(ptpirq->domain, n);
+ handle_nested_irq(sub_irq);
+ ++nhandled;
+ }
+ }
+
+out:
+ return (nhandled > 0 ? IRQ_HANDLED : IRQ_NONE);
+}
+
+static void ksz_ptp_irq_mask(struct irq_data *d)
+{
+ struct ksz_irq *kirq = irq_data_get_irq_chip_data(d);
+
+ kirq->masked &= ~BIT(d->hwirq + KSZ_PTP_INT_START);
+}
+
+static void ksz_ptp_irq_unmask(struct irq_data *d)
+{
+ struct ksz_irq *kirq = irq_data_get_irq_chip_data(d);
+
+ kirq->masked |= BIT(d->hwirq + KSZ_PTP_INT_START);
+}
+
+static void ksz_ptp_irq_bus_lock(struct irq_data *d)
+{
+ struct ksz_irq *kirq = irq_data_get_irq_chip_data(d);
+
+ mutex_lock(&kirq->dev->lock_irq);
+}
+
+static void ksz_ptp_irq_bus_sync_unlock(struct irq_data *d)
+{
+ struct ksz_irq *kirq = irq_data_get_irq_chip_data(d);
+ struct ksz_device *dev = kirq->dev;
+ int ret;
+
+ ret = ksz_write16(dev, kirq->reg_mask, kirq->masked);
+ if (ret)
+ dev_err(dev->dev, "failed to change IRQ mask\n");
+
+ mutex_unlock(&dev->lock_irq);
+}
+
+static const struct irq_chip ksz_ptp_irq_chip = {
+ .name = "ksz-irq",
+ .irq_mask = ksz_ptp_irq_mask,
+ .irq_unmask = ksz_ptp_irq_unmask,
+ .irq_bus_lock = ksz_ptp_irq_bus_lock,
+ .irq_bus_sync_unlock = ksz_ptp_irq_bus_sync_unlock,
+};
+
+static int ksz_ptp_irq_domain_map(struct irq_domain *d,
+ unsigned int irq, irq_hw_number_t hwirq)
+{
+ irq_set_chip_data(irq, d->host_data);
+ irq_set_chip_and_handler(irq, &ksz_ptp_irq_chip, handle_level_irq);
+ irq_set_noprobe(irq);
+
+ return 0;
+}
+
+static const struct irq_domain_ops ksz_ptp_irq_domain_ops = {
+ .map = ksz_ptp_irq_domain_map,
+ .xlate = irq_domain_xlate_twocell,
+};
+
+static int ksz_ptp_msg_irq_setup(struct ksz_port *port)
+{
+ u16 ts_reg[] = {REG_PTP_PORT_PDRESP_TS, REG_PTP_PORT_XDELAY_TS,
+ REG_PTP_PORT_SYNC_TS};
+ struct ksz_device *dev = port->ksz_dev;
+ struct ksz_irq *ptpirq = &port->ptpirq;
+ struct ksz_ptp_irq *ptpmsg_irq;
+ int ret;
+ u8 n;
+
+ for (n = 0; n < ptpirq->nirqs; n++) {
+ ptpmsg_irq = &port->ptpmsg_irq[n];
+
+ ptpmsg_irq->port = port;
+ ptpmsg_irq->ts_reg = dev->dev_ops->get_port_addr(port->num,
+ ts_reg[n]);
+ ptpmsg_irq->irq_num = irq_create_mapping(ptpirq->domain, n);
+ if (ptpmsg_irq->irq_num < 0) {
+ ret = ptpmsg_irq->irq_num;
+ goto out;
+ }
+
+ snprintf(ptpmsg_irq->name, sizeof(ptpmsg_irq->name),
+ "PTP-MSG-%d", n);
+
+ ret = request_threaded_irq(ptpmsg_irq->irq_num, NULL,
+ ksz_ptp_msg_thread_fn,
+ IRQF_ONESHOT | IRQF_TRIGGER_FALLING,
+ ptpmsg_irq->name, ptpmsg_irq);
+ if (ret)
+ goto out;
+ }
+
+ return 0;
+
+out:
+ while (n--)
+ irq_dispose_mapping(port->ptpmsg_irq[n].irq_num);
+
+ return ret;
+}
+
+int ksz_ptp_irq_setup(struct dsa_switch *ds, u8 p)
+{
+ struct ksz_device *dev = ds->priv;
+ const struct ksz_dev_ops *ops = dev->dev_ops;
+ struct ksz_port *port = &dev->ports[p];
+ struct ksz_irq *ptpirq = &port->ptpirq;
+ int ret;
+
+ ptpirq->dev = dev;
+ ptpirq->masked = 0;
+ ptpirq->nirqs = 3;
+ ptpirq->reg_mask = ops->get_port_addr(p, REG_PTP_PORT_TX_INT_ENABLE__2);
+ ptpirq->reg_status = ops->get_port_addr(p,
+ REG_PTP_PORT_TX_INT_STATUS__2);
+ snprintf(ptpirq->name, sizeof(ptpirq->name), "ptp_irq-%d", p);
+
+ ptpirq->irq_num = irq_find_mapping(port->pirq.domain, PORT_SRC_PTP_INT);
+ if (ptpirq->irq_num < 0)
+ return ptpirq->irq_num;
+
+ ptpirq->domain = irq_domain_add_simple(dev->dev->of_node, ptpirq->nirqs,
+ 0, &ksz_ptp_irq_domain_ops,
+ ptpirq);
+ if (!ptpirq->domain)
+ return -ENOMEM;
+
+ ret = request_threaded_irq(ptpirq->irq_num, NULL, ksz_ptp_irq_thread_fn,
+ IRQF_ONESHOT | IRQF_TRIGGER_FALLING,
+ ptpirq->name, ptpirq);
+ if (ret)
+ goto out;
+
+ ret = ksz_ptp_msg_irq_setup(port);
+ if (ret)
+ goto out;
+
+ return 0;
+
+out:
+ irq_dispose_mapping(ptpirq->irq_num);
+
+ return ret;
+}
+
+void ksz_ptp_irq_free(struct dsa_switch *ds, u8 p)
+{
+ struct ksz_device *dev = ds->priv;
+ struct ksz_port *port = &dev->ports[p];
+ struct ksz_irq *ptpirq = &port->ptpirq;
+ u8 n;
+
+ free_irq(ptpirq->irq_num, ptpirq);
+
+ for (n = 0; n < ptpirq->nirqs; n++)
+ irq_dispose_mapping(port->ptpmsg_irq[n].irq_num);
+
+ irq_domain_remove(ptpirq->domain);
+}
+
MODULE_AUTHOR("Christian Eggers <[email protected]>");
MODULE_AUTHOR("Arun Ramadoss <[email protected]>");
MODULE_DESCRIPTION("PTP support for KSZ switch");
diff --git a/drivers/net/dsa/microchip/ksz_ptp.h b/drivers/net/dsa/microchip/ksz_ptp.h
index 2c29a0b604bb..7c5679372705 100644
--- a/drivers/net/dsa/microchip/ksz_ptp.h
+++ b/drivers/net/dsa/microchip/ksz_ptp.h
@@ -30,6 +30,8 @@ int ksz_get_ts_info(struct dsa_switch *ds, int port,
struct ethtool_ts_info *ts);
int ksz_hwtstamp_get(struct dsa_switch *ds, int port, struct ifreq *ifr);
int ksz_hwtstamp_set(struct dsa_switch *ds, int port, struct ifreq *ifr);
+int ksz_ptp_irq_setup(struct dsa_switch *ds, u8 p);
+void ksz_ptp_irq_free(struct dsa_switch *ds, u8 p);

#else

@@ -45,6 +47,13 @@ static inline int ksz_ptp_clock_register(struct dsa_switch *ds)

static inline void ksz_ptp_clock_unregister(struct dsa_switch *ds) { }

+static inline int ksz_ptp_irq_setup(struct dsa_switch *ds, u8 p)
+{
+ return 0;
+}
+
+static inline void ksz_ptp_irq_free(struct dsa_switch *ds, u8 p) {}
+
#define ksz_get_ts_info NULL

#define ksz_hwtstamp_get NULL
diff --git a/drivers/net/dsa/microchip/ksz_ptp_reg.h b/drivers/net/dsa/microchip/ksz_ptp_reg.h
index 4ca4ad4bba7e..abe95bbefc12 100644
--- a/drivers/net/dsa/microchip/ksz_ptp_reg.h
+++ b/drivers/net/dsa/microchip/ksz_ptp_reg.h
@@ -49,4 +49,23 @@
#define PTP_MASTER BIT(1)
#define PTP_1STEP BIT(0)

+/* Port PTP Register */
+#define REG_PTP_PORT_RX_DELAY__2 0x0C00
+#define REG_PTP_PORT_TX_DELAY__2 0x0C02
+#define REG_PTP_PORT_ASYM_DELAY__2 0x0C04
+
+#define REG_PTP_PORT_XDELAY_TS 0x0C08
+#define REG_PTP_PORT_SYNC_TS 0x0C0C
+#define REG_PTP_PORT_PDRESP_TS 0x0C10
+
+#define REG_PTP_PORT_TX_INT_STATUS__2 0x0C14
+#define REG_PTP_PORT_TX_INT_ENABLE__2 0x0C16
+
+#define PTP_PORT_SYNC_INT BIT(15)
+#define PTP_PORT_XDELAY_REQ_INT BIT(14)
+#define PTP_PORT_PDELAY_RESP_INT BIT(13)
+#define KSZ_SYNC_MSG 2
+#define KSZ_XDREQ_MSG 1
+#define KSZ_PDRES_MSG 0
+
#endif
--
2.36.1

2022-12-09 08:12:12

by Arun Ramadoss

[permalink] [raw]
Subject: [Patch net-next v3 12/13] net: dsa: microchip: ptp: lan937x: add 2 step timestamping

LAN937x series of switches support 2 step timestamping mechanism. There
are timestamp correction calculation performed in ksz_rcv_timestamp and
ksz_xmit_timestamp which are applicable only for p2p1step. To check
whether the 2 step is enabled or not in tag_ksz.c introduced the helper
function in taggger_data to query it from ksz_ptp.c. Based on whether 2
step is enabled or not, timestamp calculation are performed.

Signed-off-by: Arun Ramadoss <[email protected]>
---
v2 -> v3
- Reverted setting PTP_1Step bit as we are setting 802_1AS bit

v1 -> v2
- declard is_ptp_twostep as macro NULL for ptp disabled case
- Moved the patch in series to have continuity for lan937x updates 9/11
to 12/13
- enable PTP_1STEP bit based on tx timestamping

Patch v1
- Patch is new.
---
drivers/net/dsa/microchip/ksz_ptp.c | 25 ++++++++++++++++++++++++-
1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/drivers/net/dsa/microchip/ksz_ptp.c b/drivers/net/dsa/microchip/ksz_ptp.c
index 546211ae46a3..ccb635585721 100644
--- a/drivers/net/dsa/microchip/ksz_ptp.c
+++ b/drivers/net/dsa/microchip/ksz_ptp.c
@@ -331,6 +331,9 @@ int ksz_get_ts_info(struct dsa_switch *ds, int port, struct ethtool_ts_info *ts)

ts->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ONESTEP_P2P);

+ if (is_lan937x(dev))
+ ts->tx_types |= BIT(HWTSTAMP_TX_ON);
+
ts->rx_filters = BIT(HWTSTAMP_FILTER_NONE) |
BIT(HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
BIT(HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
@@ -374,6 +377,15 @@ static int ksz_set_hwtstamp_config(struct ksz_device *dev,
prt->ptpmsg_irq[KSZ_PDRES_MSG].ts_en = 0;
prt->hwts_tx_en = true;
break;
+ case HWTSTAMP_TX_ON:
+ if (!is_lan937x(dev))
+ return -ERANGE;
+
+ prt->ptpmsg_irq[KSZ_SYNC_MSG].ts_en = 1;
+ prt->ptpmsg_irq[KSZ_XDREQ_MSG].ts_en = 1;
+ prt->ptpmsg_irq[KSZ_PDRES_MSG].ts_en = 1;
+ prt->hwts_tx_en = true;
+ break;
default:
return -ERANGE;
}
@@ -460,14 +472,20 @@ bool ksz_port_rxtstamp(struct dsa_switch *ds, int port, struct sk_buff *skb,
struct skb_shared_hwtstamps *hwtstamps = skb_hwtstamps(skb);
struct ksz_device *dev = ds->priv;
struct ptp_header *ptp_hdr;
+ struct ksz_port *prt;
u8 ptp_msg_type;
ktime_t tstamp;
s64 correction;

+ prt = &dev->ports[port];
+
tstamp = KSZ_SKB_CB(skb)->tstamp;
memset(hwtstamps, 0, sizeof(*hwtstamps));
hwtstamps->hwtstamp = ksz_tstamp_reconstruct(dev, tstamp);

+ if (prt->tstamp_config.tx_type != HWTSTAMP_TX_ONESTEP_P2P)
+ goto out;
+
ptp_hdr = ptp_parse_header(skb, type);
if (!ptp_hdr)
goto out;
@@ -516,6 +534,10 @@ void ksz_port_txtstamp(struct dsa_switch *ds, int port,
ptp_msg_type = ptp_get_msgtype(hdr, type);

switch (ptp_msg_type) {
+ case PTP_MSGTYPE_SYNC:
+ if (prt->tstamp_config.tx_type == HWTSTAMP_TX_ONESTEP_P2P)
+ return;
+ fallthrough;
case PTP_MSGTYPE_PDELAY_REQ:
fallthrough;
case PTP_MSGTYPE_PDELAY_RESP:
@@ -532,7 +554,8 @@ void ksz_port_txtstamp(struct dsa_switch *ds, int port,
/* caching the value to be used in tag_ksz.c */
KSZ_SKB_CB(skb)->clone = clone;
KSZ_SKB_CB(clone)->ptp_type = type;
- if (ptp_msg_type == PTP_MSGTYPE_PDELAY_RESP)
+ if (ptp_msg_type == PTP_MSGTYPE_PDELAY_RESP &&
+ prt->tstamp_config.tx_type == HWTSTAMP_TX_ONESTEP_P2P)
KSZ_SKB_CB(clone)->update_correction = true;
}

--
2.36.1

2022-12-09 08:12:31

by Arun Ramadoss

[permalink] [raw]
Subject: [Patch net-next v3 09/13] net: dsa: microchip: ptp: move pdelay_rsp correction field to tail tag

From: Christian Eggers <[email protected]>

For PDelay_Resp messages we will likely have a negative value in the
correction field. The switch hardware cannot correctly update such
values (produces an off by one error in the UDP checksum), so it must be
moved to the time stamp field in the tail tag. Format of the correction
field is 48 bit ns + 16 bit fractional ns. After updating the
correction field, clone is no longer required hence it is freed.

Signed-off-by: Christian Eggers <[email protected]>
---
v2 -> v3
- used update_correction variable in skb->cb instead of ptp_msg_type

v1 -> v2
- added fallthrough keyword in switch case to suppress checkpatch
warning

RFC v3 -> Patch v1
- Patch is separated from transmission logic patch
---
drivers/net/dsa/microchip/ksz_ptp.c | 5 ++++
include/linux/dsa/ksz_common.h | 2 ++
net/dsa/tag_ksz.c | 41 ++++++++++++++++++++++++++++-
3 files changed, 47 insertions(+), 1 deletion(-)

diff --git a/drivers/net/dsa/microchip/ksz_ptp.c b/drivers/net/dsa/microchip/ksz_ptp.c
index 96927a699665..c9da2a735165 100644
--- a/drivers/net/dsa/microchip/ksz_ptp.c
+++ b/drivers/net/dsa/microchip/ksz_ptp.c
@@ -267,6 +267,8 @@ void ksz_port_txtstamp(struct dsa_switch *ds, int port,

switch (ptp_msg_type) {
case PTP_MSGTYPE_PDELAY_REQ:
+ fallthrough;
+ case PTP_MSGTYPE_PDELAY_RESP:
break;

default:
@@ -279,6 +281,9 @@ void ksz_port_txtstamp(struct dsa_switch *ds, int port,

/* caching the value to be used in tag_ksz.c */
KSZ_SKB_CB(skb)->clone = clone;
+ KSZ_SKB_CB(clone)->ptp_type = type;
+ if (ptp_msg_type == PTP_MSGTYPE_PDELAY_RESP)
+ KSZ_SKB_CB(clone)->update_correction = true;
}

static void ksz_ptp_txtstamp_skb(struct ksz_device *dev,
diff --git a/include/linux/dsa/ksz_common.h b/include/linux/dsa/ksz_common.h
index b91beab5e138..576a99ca698d 100644
--- a/include/linux/dsa/ksz_common.h
+++ b/include/linux/dsa/ksz_common.h
@@ -36,6 +36,8 @@ struct ksz_tagger_data {

struct ksz_skb_cb {
struct sk_buff *clone;
+ unsigned int ptp_type;
+ bool update_correction;
u32 tstamp;
};

diff --git a/net/dsa/tag_ksz.c b/net/dsa/tag_ksz.c
index e14ee26bf6a0..7dd2133b0820 100644
--- a/net/dsa/tag_ksz.c
+++ b/net/dsa/tag_ksz.c
@@ -7,6 +7,7 @@
#include <linux/dsa/ksz_common.h>
#include <linux/etherdevice.h>
#include <linux/list.h>
+#include <linux/ptp_classify.h>
#include <net/dsa.h>

#include "tag.h"
@@ -194,14 +195,52 @@ static void ksz_rcv_timestamp(struct sk_buff *skb, u8 *tag)
*/
static void ksz_xmit_timestamp(struct dsa_port *dp, struct sk_buff *skb)
{
+ struct sk_buff *clone = KSZ_SKB_CB(skb)->clone;
struct ksz_tagger_private *priv;
+ struct ptp_header *ptp_hdr;
+ bool update_correction;
+ unsigned int ptp_type;
+ u32 tstamp_raw = 0;
+ s64 correction;

priv = ksz_tagger_private(dp->ds);

if (!test_bit(KSZ_HWTS_EN, &priv->state))
return;

- put_unaligned_be32(0, skb_put(skb, KSZ_PTP_TAG_LEN));
+ if (!clone)
+ goto output_tag;
+
+ update_correction = KSZ_SKB_CB(clone)->update_correction;
+ if (!update_correction)
+ goto output_tag;
+
+ ptp_type = KSZ_SKB_CB(clone)->ptp_type;
+
+ ptp_hdr = ptp_parse_header(skb, ptp_type);
+ if (!ptp_hdr)
+ goto output_tag;
+
+ correction = (s64)get_unaligned_be64(&ptp_hdr->correction);
+
+ if (correction < 0) {
+ struct timespec64 ts;
+
+ ts = ns_to_timespec64(-correction >> 16);
+ tstamp_raw = ((ts.tv_sec & 3) << 30) | ts.tv_nsec;
+
+ /* Set correction field to 0 and update UDP checksum. */
+ ptp_header_update_correction(skb, ptp_type, ptp_hdr, 0);
+ }
+
+ /* For PDelay_Resp messages, the clone is not required in
+ * skb_complete_tx_timestamp() and should be freed here.
+ */
+ kfree_skb(clone);
+ KSZ_SKB_CB(skb)->clone = NULL;
+
+output_tag:
+ put_unaligned_be32(tstamp_raw, skb_put(skb, KSZ_PTP_TAG_LEN));
}

/* Defer transmit if waiting for egress time stamp is required. */
--
2.36.1

2022-12-09 11:45:18

by Vladimir Oltean

[permalink] [raw]
Subject: Re: [Patch net-next v3 01/13] net: dsa: microchip: ptp: add the posix clock support

On Fri, Dec 09, 2022 at 12:54:25PM +0530, Arun Ramadoss wrote:
> From: Christian Eggers <[email protected]>
>
> This patch implement routines (adjfine, adjtime, gettime and settime)
> for manipulating the chip's PTP clock. It registers the ptp caps
> to posix clock register.
>
> Signed-off-by: Christian Eggers <[email protected]>
> Co-developed-by: Arun Ramadoss <[email protected]>
> Signed-off-by: Arun Ramadoss <[email protected]>
>
> ---

Reviewed-by: Vladimir Oltean <[email protected]> # mostly api

2022-12-09 11:58:26

by Vladimir Oltean

[permalink] [raw]
Subject: Re: [Patch net-next v3 03/13] net: dsa: microchip: ptp: add 4 bytes in tail tag when ptp enabled

On Fri, Dec 09, 2022 at 12:54:27PM +0530, Arun Ramadoss wrote:
> When the PTP is enabled in hardware bit 6 of PTP_MSG_CONF1 register, the
> transmit frame needs additional 4 bytes before the tail tag. It is
> needed for all the transmission packets irrespective of PTP packets or
> not.
> The 4-byte timestamp field is 0 for frames other than Pdelay_Resp. For
> the one-step Pdelay_Resp, the switch needs the receive timestamp of the
> Pdelay_Req message so that it can put the turnaround time in the
> correction field.
> Since PTP has to be enabled for both Transmission and reception
> timestamping, driver needs to track of the tx and rx setting of the all
> the user ports in the switch.
> Two flags hw_tx_en and hw_rx_en are added in ksz_port to track the
> timestampping setting of each port. When any one of ports has tx or rx
> timestampping enabled, bit 6 of PTP_MSG_CONF1 is set and it is indicated
> to tag_ksz.c through tagger bytes. This flag adds 4 additional bytes to
> the tail tag. When tx and rx timestamping of all the ports are disabled,
> then 4 bytes are not added.
>
> Testing using hwstamp -i <interface>
>
> Signed-off-by: Arun Ramadoss <[email protected]>
> ---

Reviewed-by: Vladimir Oltean <[email protected]> # mostly api

2022-12-09 15:19:03

by Vladimir Oltean

[permalink] [raw]
Subject: Re: [Patch net-next v3 09/13] net: dsa: microchip: ptp: move pdelay_rsp correction field to tail tag

On Fri, Dec 09, 2022 at 12:54:33PM +0530, Arun Ramadoss wrote:
> From: Christian Eggers <[email protected]>
>
> For PDelay_Resp messages we will likely have a negative value in the
> correction field. The switch hardware cannot correctly update such
> values (produces an off by one error in the UDP checksum), so it must be
> moved to the time stamp field in the tail tag. Format of the correction
> field is 48 bit ns + 16 bit fractional ns. After updating the
> correction field, clone is no longer required hence it is freed.
>
> Signed-off-by: Christian Eggers <[email protected]>

Similar. This needs your sign off too.

2022-12-09 15:20:47

by Vladimir Oltean

[permalink] [raw]
Subject: Re: [Patch net-next v3 07/13] net: dsa: microchip: ptp: add packet reception timestamping

On Fri, Dec 09, 2022 at 12:54:31PM +0530, Arun Ramadoss wrote:
> From: Christian Eggers <[email protected]>
>
> Rx Timestamping is done through 4 additional bytes in tail tag.
> Whenever the ptp packet is received, the 4 byte hardware time stamped
> value is added before 1 byte tail tag. Also, bit 7 in tail tag indicates
> it as PTP frame. This 4 byte value is extracted from the tail tag and
> reconstructed to absolute time and assigned to skb hwtstamp.
> If the packet received in PDelay_Resp, then partial ingress timestamp
> is subtracted from the correction field. Since user space tools expects
> to be done in hardware.
>
> Signed-off-by: Christian Eggers <[email protected]>
> Co-developed-by: Arun Ramadoss <[email protected]>
> Signed-off-by: Arun Ramadoss <[email protected]>
>
> ---

Reviewed-by: Vladimir Oltean <[email protected]>

> +bool ksz_port_rxtstamp(struct dsa_switch *ds, int port, struct sk_buff *skb,
> + unsigned int type)
> +{
> + struct skb_shared_hwtstamps *hwtstamps = skb_hwtstamps(skb);
> + struct ksz_device *dev = ds->priv;
> + struct ptp_header *ptp_hdr;
> + u8 ptp_msg_type;
> + ktime_t tstamp;
> + s64 correction;
> +
> + tstamp = KSZ_SKB_CB(skb)->tstamp;
> + memset(hwtstamps, 0, sizeof(*hwtstamps));
> + hwtstamps->hwtstamp = ksz_tstamp_reconstruct(dev, tstamp);
> +
> + ptp_hdr = ptp_parse_header(skb, type);
> + if (!ptp_hdr)
> + goto out;
> +
> + ptp_msg_type = ptp_get_msgtype(ptp_hdr, type);
> + if (ptp_msg_type != PTP_MSGTYPE_PDELAY_REQ)
> + goto out;
> +
> + /* Only subtract the partial time stamp from the correction field. When
> + * the hardware adds the egress time stamp to the correction field of
> + * the PDelay_Resp message on tx, also only the partial time stamp will
> + * be added.
> + */
> + correction = (s64)get_unaligned_be64(&ptp_hdr->correction);
> + correction -= ktime_to_ns(tstamp) << 16;
> +
> + ptp_header_update_correction(skb, type, ptp_hdr, correction);
> +
> +out:
> + return 0;

Nitpick: port_rxtstamp() returns "bool" as in "should reception of this
packet be deferred until the full RX timestamp is available?". The
answer of 0 is coincidentally correct, but should have been "false".

> +}
> +
> static int _ksz_ptp_gettime(struct ksz_device *dev, struct timespec64 *ts)
> {
> u32 nanoseconds;

2022-12-09 15:23:14

by Vladimir Oltean

[permalink] [raw]
Subject: Re: [Patch net-next v3 10/13] net: dsa: microchip: ptp: add periodic output signal

On Fri, Dec 09, 2022 at 12:54:34PM +0530, Arun Ramadoss wrote:
> diff --git a/drivers/net/dsa/microchip/ksz_ptp.c b/drivers/net/dsa/microchip/ksz_ptp.c
> index c9da2a735165..6d7edc81909e 100644
> --- a/drivers/net/dsa/microchip/ksz_ptp.c
> +++ b/drivers/net/dsa/microchip/ksz_ptp.c
> @@ -31,6 +31,252 @@
>
> #define KSZ_PTP_INT_START 13
>
> +static int _ksz_ptp_gettime(struct ksz_device *dev, struct timespec64 *ts);

Can you enforce a natural function ordering from the beginning such that
forward declarations are not needed?

> +
> +static int ksz_ptp_tou_reset(struct ksz_device *dev, u8 unit)
> +{
> + u32 data;
> + int ret;
> +
> + /* Reset trigger unit (clears TRIGGER_EN, but not GPIOSTATx) */
> + ret = ksz_rmw32(dev, REG_PTP_CTRL_STAT__4, TRIG_RESET, TRIG_RESET);
> +
> + data = FIELD_PREP(TRIG_DONE_M, BIT(unit));
> + ret = ksz_write32(dev, REG_PTP_TRIG_STATUS__4, data);
> + if (ret)
> + return ret;
> +
> + data = FIELD_PREP(TRIG_INT_M, BIT(unit));
> + ret = ksz_write32(dev, REG_PTP_INT_STATUS__4, data);
> + if (ret)
> + return ret;
> +
> + /* Clear reset and set GPIO direction */
> + return ksz_rmw32(dev, REG_PTP_CTRL_STAT__4, (TRIG_RESET | TRIG_ENABLE),
> + 0);
> +}
> +
> +static int ksz_ptp_tou_pulse_verify(u64 pulse_ns)
> +{
> + u32 data;
> +
> + if (pulse_ns & 0x3)
> + return -EINVAL;
> +
> + data = (pulse_ns / 8);
> + if (!FIELD_FIT(TRIG_PULSE_WIDTH_M, data))
> + return -ERANGE;
> +
> + return 0;
> +}
> +
> +static int ksz_ptp_tou_target_time_set(struct ksz_device *dev,
> + struct timespec64 const *ts)
> +{
> + int ret;
> +
> + /* Hardware has only 32 bit */
> + if ((ts->tv_sec & 0xffffffff) != ts->tv_sec)
> + return -EINVAL;
> +
> + ret = ksz_write32(dev, REG_TRIG_TARGET_NANOSEC, ts->tv_nsec);
> + if (ret)
> + return ret;
> +
> + ret = ksz_write32(dev, REG_TRIG_TARGET_SEC, ts->tv_sec);
> + if (ret)
> + return ret;
> +
> + return 0;
> +}
> +
> +static int ksz_ptp_tou_start(struct ksz_device *dev, u8 unit)
> +{
> + u32 data;
> + int ret;
> +
> + ret = ksz_rmw32(dev, REG_PTP_CTRL_STAT__4, TRIG_ENABLE, TRIG_ENABLE);
> + if (ret)
> + return ret;
> +
> + /* Check error flag:
> + * - the ACTIVE flag is NOT cleared an error!
> + */
> + ret = ksz_read32(dev, REG_PTP_TRIG_STATUS__4, &data);
> + if (ret)
> + return ret;
> +
> + if (FIELD_GET(TRIG_ERROR_M, data) & (1 << unit)) {
> + dev_err(dev->dev, "%s: Trigger unit%d error!\n", __func__,
> + unit);
> + ret = -EIO;
> + /* Unit will be reset on next access */
> + return ret;
> + }
> +
> + return 0;
> +}
> +
> +static int ksz_ptp_configure_perout(struct ksz_device *dev,
> + u32 cycle_width_ns, u32 pulse_width_ns,
> + struct timespec64 const *target_time,
> + u8 index)
> +{
> + u32 data;
> + int ret;
> +
> + data = FIELD_PREP(TRIG_NOTIFY, 1) |
> + FIELD_PREP(TRIG_GPO_M, index) |
> + FIELD_PREP(TRIG_PATTERN_M, TRIG_POS_PERIOD);
> + ret = ksz_write32(dev, REG_TRIG_CTRL__4, data);
> + if (ret)
> + return ret;
> +
> + ret = ksz_write32(dev, REG_TRIG_CYCLE_WIDTH, cycle_width_ns);
> + if (ret)
> + return ret;
> +
> + /* Set cycle count 0 - Infinite */
> + ret = ksz_rmw32(dev, REG_TRIG_CYCLE_CNT, TRIG_CYCLE_CNT_M, 0);
> + if (ret)
> + return ret;
> +
> + data = (pulse_width_ns / 8);
> + ret = ksz_write32(dev, REG_TRIG_PULSE_WIDTH__4, data);
> + if (ret)
> + return ret;
> +
> + ret = ksz_ptp_tou_target_time_set(dev, target_time);
> + if (ret)
> + return ret;
> +
> + return 0;
> +}
> +
> +#define KSZ_PEROUT_VALID_FLAGS ( \
> + PTP_PEROUT_DUTY_CYCLE \
> + )

Do you have plans for more perout flags? If not, it looks odd for this
to unravel on 3 lines.

> +
> +static int ksz_ptp_enable_perout(struct ksz_device *dev,
> + struct ptp_perout_request const *request,
> + int on)
> +{
> + struct ksz_ptp_data *ptp_data = &dev->ptp_data;
> + u64 cycle_width_ns;
> + u64 pulse_width_ns;
> + int pin = 0;
> + u32 data32;
> + int ret;
> +
> + if (request->flags & ~KSZ_PEROUT_VALID_FLAGS)
> + return -EINVAL;
> +
> + if (ptp_data->tou_mode != KSZ_PTP_TOU_PEROUT &&
> + ptp_data->tou_mode != KSZ_PTP_TOU_IDLE)
> + return -EBUSY;
> +
> + data32 = FIELD_PREP(PTP_GPIO_INDEX, pin) |
> + FIELD_PREP(PTP_TOU_INDEX, request->index);
> + ret = ksz_rmw32(dev, REG_PTP_UNIT_INDEX__4,
> + PTP_GPIO_INDEX | PTP_TOU_INDEX, data32);
> + if (ret)
> + return ret;
> +
> + ret = ksz_ptp_tou_reset(dev, request->index);
> + if (ret)
> + return ret;
> +
> + if (!on) {
> + ptp_data->tou_mode = KSZ_PTP_TOU_IDLE;
> + return 0;
> + }
> +
> + ptp_data->perout_target_time_first.tv_sec = request->start.sec;
> + ptp_data->perout_target_time_first.tv_nsec = request->start.nsec;
> +
> + ptp_data->perout_period.tv_sec = request->period.sec;
> + ptp_data->perout_period.tv_nsec = request->period.nsec;
> +
> + cycle_width_ns = timespec64_to_ns(&ptp_data->perout_period);
> + if ((cycle_width_ns & TRIG_CYCLE_WIDTH_M) != cycle_width_ns)
> + return -EINVAL;
> +
> + if (request->flags & PTP_PEROUT_DUTY_CYCLE) {
> + pulse_width_ns = request->on.sec * NSEC_PER_SEC +
> + request->on.nsec;
> + } else {
> + /* Use a duty cycle of 50%. Maximum pulse width supported by the
> + * hardware is a little bit more than 125 ms.
> + */
> + pulse_width_ns = min_t(u64,
> + (request->period.sec * NSEC_PER_SEC
> + + request->period.nsec) / 2
> + / 8 * 8,

Coding style nitpick: operators aren't generally put at the beginning of
a new line, but at the end of the previous one.

> + 125000000LL);

You may want some helper macros for all of these magic constants, and
maybe a helper that transforms the perout period to a pulse width?

> + }
> +
> + ret = ksz_ptp_tou_pulse_verify(pulse_width_ns);
> + if (ret)
> + return ret;
> +
> + ret = ksz_ptp_configure_perout(dev, cycle_width_ns, pulse_width_ns,
> + &ptp_data->perout_target_time_first,
> + pin);
> + if (ret)
> + return ret;
> +
> + ret = ksz_ptp_tou_start(dev, request->index);
> + if (ret)
> + return ret;
> +
> + ptp_data->tou_mode = KSZ_PTP_TOU_PEROUT;
> +
> + return 0;
> +}
> +
> +static int ksz_ptp_restart_perout(struct ksz_device *dev)
> +{
> + struct ksz_ptp_data *ptp_data = &dev->ptp_data;
> + s64 now_ns, first_ns, period_ns, next_ns;
> + struct ptp_perout_request request;
> + struct timespec64 next;
> + struct timespec64 now;
> + unsigned int count;
> + int ret;
> +
> + ret = _ksz_ptp_gettime(dev, &now);
> + if (ret)
> + return ret;
> +
> + now_ns = timespec64_to_ns(&now);
> + first_ns = timespec64_to_ns(&ptp_data->perout_target_time_first);
> +
> + /* Calculate next perout event based on start time and period */
> + period_ns = timespec64_to_ns(&ptp_data->perout_period);
> +
> + if (first_ns < now_ns) {
> + count = div_u64(now_ns - first_ns, period_ns);
> + next_ns = first_ns + count * period_ns;
> + } else {
> + next_ns = first_ns;
> + }
> +
> + /* Ensure 100 ms guard time prior next event */
> + while (next_ns < now_ns + 100000000)
> + next_ns += period_ns;
> +
> + /* Restart periodic output signal */
> + next = ns_to_timespec64(next_ns);
> + request.start.sec = next.tv_sec;
> + request.start.nsec = next.tv_nsec;
> + request.period.sec = ptp_data->perout_period.tv_sec;
> + request.period.nsec = ptp_data->perout_period.tv_nsec;
> + request.index = 0;
> + request.flags = 0;
> +
> + return ksz_ptp_enable_perout(dev, &request, 1);
> +}
> +
> static int ksz_ptp_enable_mode(struct ksz_device *dev)
> {
> struct ksz_tagger_data *tagger_data = ksz_tagger_data(dev->ds);
> @@ -396,6 +642,20 @@ static int ksz_ptp_settime(struct ptp_clock_info *ptp,
> if (ret)
> goto unlock;
>
> + switch (ptp_data->tou_mode) {
> + case KSZ_PTP_TOU_IDLE:
> + break;
> +
> + case KSZ_PTP_TOU_PEROUT:
> + dev_info(dev->dev, "Restarting periodic output signal\n");
> +
> + ret = ksz_ptp_restart_perout(dev);
> + if (ret)
> + goto unlock;
> +
> + break;
> + }
> +
> spin_lock_bh(&ptp_data->clock_lock);
> ptp_data->clock_time = *ts;
> spin_unlock_bh(&ptp_data->clock_lock);
> @@ -489,6 +749,20 @@ static int ksz_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
> if (ret)
> goto unlock;
>
> + switch (ptp_data->tou_mode) {
> + case KSZ_PTP_TOU_IDLE:
> + break;
> +
> + case KSZ_PTP_TOU_PEROUT:
> + dev_info(dev->dev, "Restarting periodic output signal\n");

How about absorbing the dev_info() into the ksz_ptp_restart_perout()
call, so you don't have to duplicate it?

> +
> + ret = ksz_ptp_restart_perout(dev);
> + if (ret)
> + goto unlock;
> +
> + break;
> + }
> +
> spin_lock_bh(&ptp_data->clock_lock);
> ptp_data->clock_time = timespec64_add(ptp_data->clock_time, delta64);
> spin_unlock_bh(&ptp_data->clock_lock);
> @@ -498,6 +772,26 @@ static int ksz_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
> return ret;
> }
>
> +static int ksz_ptp_enable(struct ptp_clock_info *ptp,
> + struct ptp_clock_request *req, int on)
> +{
> + struct ksz_ptp_data *ptp_data = ptp_caps_to_data(ptp);
> + struct ksz_device *dev = ptp_data_to_ksz_dev(ptp_data);
> + int ret;
> +
> + switch (req->type) {
> + case PTP_CLK_REQ_PEROUT:
> + mutex_lock(&ptp_data->lock);
> + ret = ksz_ptp_enable_perout(dev, &req->perout, on);
> + mutex_unlock(&ptp_data->lock);
> + break;
> + default:
> + return -EOPNOTSUPP;
> + }
> +
> + return ret;
> +}
> +
> /* Function is pointer to the do_aux_work in the ptp_clock capability */
> static long ksz_ptp_do_aux_work(struct ptp_clock_info *ptp)
> {
> @@ -546,6 +840,8 @@ int ksz_ptp_clock_register(struct dsa_switch *ds)
> ptp_data->caps.adjfine = ksz_ptp_adjfine;
> ptp_data->caps.adjtime = ksz_ptp_adjtime;
> ptp_data->caps.do_aux_work = ksz_ptp_do_aux_work;
> + ptp_data->caps.enable = ksz_ptp_enable;
> + ptp_data->caps.n_per_out = 3;
>
> ret = ksz_ptp_start_clock(dev);
> if (ret)
> diff --git a/drivers/net/dsa/microchip/ksz_ptp.h b/drivers/net/dsa/microchip/ksz_ptp.h
> index 0b14aed71ec2..9451e3a76375 100644
> --- a/drivers/net/dsa/microchip/ksz_ptp.h
> +++ b/drivers/net/dsa/microchip/ksz_ptp.h
> @@ -12,6 +12,11 @@
>
> #include <linux/ptp_clock_kernel.h>
>
> +enum ksz_ptp_tou_mode {
> + KSZ_PTP_TOU_IDLE,
> + KSZ_PTP_TOU_PEROUT,
> +};
> +
> struct ksz_ptp_data {
> struct ptp_clock_info caps;
> struct ptp_clock *clock;
> @@ -20,6 +25,9 @@ struct ksz_ptp_data {
> /* lock for accessing the clock_time */
> spinlock_t clock_lock;
> struct timespec64 clock_time;
> + enum ksz_ptp_tou_mode tou_mode;
> + struct timespec64 perout_target_time_first; /* start of first pulse */
> + struct timespec64 perout_period;
> };
>
> int ksz_ptp_clock_register(struct dsa_switch *ds);
> diff --git a/drivers/net/dsa/microchip/ksz_ptp_reg.h b/drivers/net/dsa/microchip/ksz_ptp_reg.h
> index abe95bbefc12..dbccfedf89e4 100644
> --- a/drivers/net/dsa/microchip/ksz_ptp_reg.h
> +++ b/drivers/net/dsa/microchip/ksz_ptp_reg.h
> @@ -49,6 +49,69 @@
> #define PTP_MASTER BIT(1)
> #define PTP_1STEP BIT(0)
>
> +#define REG_PTP_UNIT_INDEX__4 0x0520
> +
> +#define PTP_GPIO_INDEX GENMASK(19, 16)
> +#define PTP_TSI_INDEX BIT(8)
> +#define PTP_TOU_INDEX GENMASK(1, 0)
> +
> +#define REG_PTP_TRIG_STATUS__4 0x0524
> +
> +#define TRIG_ERROR_M GENMASK(18, 16)
> +#define TRIG_DONE_M GENMASK(2, 0)
> +
> +#define REG_PTP_INT_STATUS__4 0x0528
> +
> +#define TRIG_INT_M GENMASK(18, 16)
> +#define TS_INT_M GENMASK(1, 0)
> +
> +#define REG_PTP_CTRL_STAT__4 0x052C
> +
> +#define GPIO_IN BIT(7)
> +#define GPIO_OUT BIT(6)
> +#define TS_INT_ENABLE BIT(5)
> +#define TRIG_ACTIVE BIT(4)
> +#define TRIG_ENABLE BIT(3)
> +#define TRIG_RESET BIT(2)
> +#define TS_ENABLE BIT(1)
> +#define TS_RESET BIT(0)
> +
> +#define REG_TRIG_TARGET_NANOSEC 0x0530
> +#define REG_TRIG_TARGET_SEC 0x0534
> +
> +#define REG_TRIG_CTRL__4 0x0538
> +
> +#define TRIG_CASCADE_ENABLE BIT(31)
> +#define TRIG_CASCADE_TAIL BIT(30)
> +#define TRIG_CASCADE_UPS_M GENMASK(29, 26)
> +#define TRIG_NOW BIT(25)
> +#define TRIG_NOTIFY BIT(24)
> +#define TRIG_EDGE BIT(23)
> +#define TRIG_PATTERN_M GENMASK(22, 20)

Nitpick: strange combination of tabs and spaces.

> +#define TRIG_NEG_EDGE 0
> +#define TRIG_POS_EDGE 1
> +#define TRIG_NEG_PULSE 2
> +#define TRIG_POS_PULSE 3
> +#define TRIG_NEG_PERIOD 4
> +#define TRIG_POS_PERIOD 5
> +#define TRIG_REG_OUTPUT 6
> +#define TRIG_GPO_M GENMASK(19, 16)
> +#define TRIG_CASCADE_ITERATE_CNT_M GENMASK(15, 0)
> +
> +#define REG_TRIG_CYCLE_WIDTH 0x053C
> +#define TRIG_CYCLE_WIDTH_M GENMASK(31, 0)
> +
> +#define REG_TRIG_CYCLE_CNT 0x0540
> +
> +#define TRIG_CYCLE_CNT_M GENMASK(31, 16)
> +#define TRIG_BIT_PATTERN_M GENMASK(15, 0)
> +
> +#define REG_TRIG_ITERATE_TIME 0x0544
> +
> +#define REG_TRIG_PULSE_WIDTH__4 0x0548
> +
> +#define TRIG_PULSE_WIDTH_M GENMASK(23, 0)
> +
> /* Port PTP Register */
> #define REG_PTP_PORT_RX_DELAY__2 0x0C00
> #define REG_PTP_PORT_TX_DELAY__2 0x0C02
> --
> 2.36.1
>

2022-12-09 15:41:23

by Vladimir Oltean

[permalink] [raw]
Subject: Re: [Patch net-next v3 02/13] net: dsa: microchip: ptp: Initial hardware time stamping support

On Fri, Dec 09, 2022 at 12:54:26PM +0530, Arun Ramadoss wrote:
> From: Christian Eggers <[email protected]>
>
> This patch adds the routine for get_ts_info, hwstamp_get, set. This enables
> the PTP support towards userspace applications such as linuxptp.
>
> Signed-off-by: Christian Eggers <[email protected]>
> Co-developed-by: Arun Ramadoss <[email protected]>
> Signed-off-by: Arun Ramadoss <[email protected]>
>

When you resend, could you also do something about these blank lines
after your sign off, here and in other patches? They shouldn't be there.
I think that a blank line will appear between this and other maintainer
sign offs.

> ---
> v1 -> v2
> - Declared the ksz_hwtstamp_get/set to NULL as macro if ptp is not
> enabled
> - Removed mutex lock in hwtstamp_set()

2022-12-09 15:42:29

by Vladimir Oltean

[permalink] [raw]
Subject: Re: [Patch net-next v3 06/13] net: ptp: add helper for one-step P2P clocks

On Fri, Dec 09, 2022 at 12:54:30PM +0530, Arun Ramadoss wrote:
> From: Christian Eggers <[email protected]>
>
> For P2P delay measurement, the ingress time stamp of the PDelay_Req is
> required for the correction field of the PDelay_Resp. The application
> echoes back the correction field of the PDelay_Req when sending the
> PDelay_Resp.
>
> Some hardware (like the ZHAW InES PTP time stamping IP core) subtracts
> the ingress timestamp autonomously from the correction field, so that
> the hardware only needs to add the egress timestamp on tx. Other
> hardware (like the Microchip KSZ9563) reports the ingress time stamp via
> an interrupt and requires that the software provides this time stamp via
> tail-tag on tx.
>
> In order to avoid introducing a further application interface for this,
> the driver can simply emulate the behavior of the InES device and
> subtract the ingress time stamp in software from the correction field.
>
> On egress, the correction field can either be kept as it is (and the
> time stamp field in the tail-tag is set to zero) or move the value from
> the correction field back to the tail-tag.
>
> Changing the correction field requires updating the UDP checksum (if UDP
> is used as transport).
>
> Signed-off-by: Christian Eggers <[email protected]>

Arun, this needs your Signed-off-by: tag too.
Sometimes patchwork catches tags like these and appends them to the
patch automatically when you just reply with them on a new line. Works
with Fixes:, Reviewed-by: and Acked-by: at least.

If there are no other review concerns with this series and it's
otherwise good to go, I suppose you could try that.

2022-12-09 15:43:00

by Vladimir Oltean

[permalink] [raw]
Subject: Re: [Patch net-next v3 00/13] net: dsa: microchip: add PTP support for KSZ9563/KSZ8563 and LAN937x

On Fri, Dec 09, 2022 at 12:54:24PM +0530, Arun Ramadoss wrote:
> KSZ9563/KSZ8563 and LAN937x switch are capable for supporting IEEE 1588 PTP
> protocol. LAN937x has the same PTP register set similar to KSZ9563, hence the
> implementation has been made common for the KSZ switches. KSZ9563 does not
> support two step timestamping but LAN937x supports both. Tested the 1step &
> 2step p2p timestamping in LAN937x and p2p1step timestamping in KSZ9563.
>
> This patch series is based on the Christian Eggers PTP support for KSZ9563.
> Applied the Christian patch and updated as per the latest refactoring of KSZ
> series code. The features added on top are PTP packet Interrupt
> implementation based on nested handler, LAN937x two step timestamping and
> programmable per_out pins.

From my perspective, the DSA API is used correctly and it should be good
to go in the next patchset iteration. API usage is about all I tried to
concentrate upon. I of course encourage Richard and Christian to point out
if there's something out of the ordinary on the protocol side of things.

2022-12-09 15:49:37

by Vladimir Oltean

[permalink] [raw]
Subject: Re: [Patch net-next v3 08/13] net: dsa: microchip: ptp: add packet transmission timestamping

On Fri, Dec 09, 2022 at 12:54:32PM +0530, Arun Ramadoss wrote:
> From: Christian Eggers <[email protected]>
>
> This patch adds the routines for transmission of ptp packets. When the
> ptp pdelay_req packet to be transmitted, it uses the deferred xmit
> worker to schedule the packets.
> During irq_setup, interrupt for Sync, Pdelay_req and Pdelay_rsp are
> enabled. So interrupt is triggered for all three packets. But for
> p2p1step, we require only time stamp of Pdelay_req packet. Hence to
> avoid posting of the completion from ISR routine for Sync and
> Pdelay_resp packets, ts_en flag is introduced. This controls which
> packets need to processed for timestamp.
> After the packet is transmitted, ISR is triggered. The time at which
> packet transmitted is recorded to separate register.
> This value is reconstructed to absolute time and posted to the user
> application through socket error queue.
>
> Signed-off-by: Christian Eggers <[email protected]>
> Co-developed-by: Arun Ramadoss <[email protected]>
> Signed-off-by: Arun Ramadoss <[email protected]>
> ---

Reviewed-by: Vladimir Oltean <[email protected]>

2022-12-09 20:23:02

by Christian Eggers

[permalink] [raw]
Subject: Re: [Patch net-next v3 05/13] net: dsa: microchip: ptp: enable interrupt for timestamping

Hi Arun,

On Friday, 9 December 2022, 08:24:29 CET, Arun Ramadoss wrote:
> PTP Interrupt mask and status register differ from the global and port
> interrupt mechanism by two methods. One is that for global/port
> interrupt enabling we have to clear the bit but for ptp interrupt we
> have to set the bit. And other is bit12:0 is reserved in ptp interrupt
> registers. This forced to not use the generic implementation of
> global/port interrupt method routine. This patch implement the ptp
> interrupt mechanism to read the timestamp register for sync, pdelay_req
> and pdelay_resp.
>
> Signed-off-by: Arun Ramadoss <[email protected]>
>
> ---
> RFC v2 -> Patch v1
> - Moved the acking of interrupts before calling the handle_nested_irq to
> avoid race condition between deferred xmit and Irq threads
> ---
> drivers/net/dsa/microchip/ksz_common.c | 15 +-
> drivers/net/dsa/microchip/ksz_common.h | 11 ++
> drivers/net/dsa/microchip/ksz_ptp.c | 200 ++++++++++++++++++++++++
> drivers/net/dsa/microchip/ksz_ptp.h | 9 ++
> drivers/net/dsa/microchip/ksz_ptp_reg.h | 19 +++
> 5 files changed, 252 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/net/dsa/microchip/ksz_common.c b/drivers/net/dsa/microchip/ksz_common.c
> index 81da650b70fb..1611f8f5cd6c 100644
> --- a/drivers/net/dsa/microchip/ksz_common.c
> +++ b/drivers/net/dsa/microchip/ksz_common.c
> @@ -2101,13 +2101,17 @@ static int ksz_setup(struct dsa_switch *ds)
> ret = ksz_pirq_setup(dev, dp->index);
> if (ret)
> goto out_girq;
> +
> + ret = ksz_ptp_irq_setup(ds, dp->index);
> + if (ret)
> + goto out_pirq;
> }
> }
>
> ret = ksz_ptp_clock_register(ds);
> if (ret) {
> dev_err(dev->dev, "Failed to register PTP clock: %d\n", ret);
> - goto out_pirq;
> + goto out_ptpirq;
> }
>
> ret = ksz_mdio_register(dev);
> @@ -2124,6 +2128,10 @@ static int ksz_setup(struct dsa_switch *ds)
>
> out_ptp_clock_unregister:
> ksz_ptp_clock_unregister(ds);
> +out_ptpirq:
> + if (dev->irq > 0)
> + dsa_switch_for_each_user_port(dp, dev->ds)
> + ksz_ptp_irq_free(ds, dp->index);
> out_pirq:
> if (dev->irq > 0)
> dsa_switch_for_each_user_port(dp, dev->ds)
> @@ -2143,8 +2151,11 @@ static void ksz_teardown(struct dsa_switch *ds)
> ksz_ptp_clock_unregister(ds);
>
> if (dev->irq > 0) {
> - dsa_switch_for_each_user_port(dp, dev->ds)
> + dsa_switch_for_each_user_port(dp, dev->ds) {
> + ksz_ptp_irq_free(ds, dp->index);
> +
> ksz_irq_free(&dev->ports[dp->index].pirq);
> + }
>
> ksz_irq_free(&dev->girq);
> }
> diff --git a/drivers/net/dsa/microchip/ksz_common.h b/drivers/net/dsa/microchip/ksz_common.h
> index 641aca78ef05..918028db2e6f 100644
> --- a/drivers/net/dsa/microchip/ksz_common.h
> +++ b/drivers/net/dsa/microchip/ksz_common.h
> @@ -20,6 +20,7 @@
> #define KSZ_MAX_NUM_PORTS 8
>
> struct ksz_device;
> +struct ksz_port;
>
> struct vlan_table {
> u32 table[3];
> @@ -83,6 +84,13 @@ struct ksz_irq {
> struct ksz_device *dev;
> };
>
> +struct ksz_ptp_irq {
> + struct ksz_port *port;
> + u16 ts_reg;
> + char name[16];
> + int irq_num;
> +};
> +
> struct ksz_port {
> bool remove_tag; /* Remove Tag flag set, for ksz8795 only */
> bool learning;
> @@ -106,6 +114,8 @@ struct ksz_port {
> struct hwtstamp_config tstamp_config;
> bool hwts_tx_en;
> bool hwts_rx_en;
> + struct ksz_irq ptpirq;
> + struct ksz_ptp_irq ptpmsg_irq[3];
> #endif
> };
>
> @@ -612,6 +622,7 @@ static inline int is_lan937x(struct ksz_device *dev)
> #define REG_PORT_INT_MASK 0x001F
>
> #define PORT_SRC_PHY_INT 1
> +#define PORT_SRC_PTP_INT 2
>
> #define KSZ8795_HUGE_PACKET_SIZE 2000
> #define KSZ8863_HUGE_PACKET_SIZE 1916
> diff --git a/drivers/net/dsa/microchip/ksz_ptp.c b/drivers/net/dsa/microchip/ksz_ptp.c
> index b864b88dc6f9..a7b015ac2734 100644
> --- a/drivers/net/dsa/microchip/ksz_ptp.c
> +++ b/drivers/net/dsa/microchip/ksz_ptp.c
> @@ -6,6 +6,8 @@
> */
>
> #include <linux/dsa/ksz_common.h>
> +#include <linux/irq.h>
> +#include <linux/irqdomain.h>
> #include <linux/kernel.h>
> #include <linux/ptp_classify.h>
> #include <linux/ptp_clock_kernel.h>
> @@ -25,6 +27,8 @@
> #define KSZ_PTP_INC_NS 40 /* HW clock is incremented every 40 ns (by 40) */
> #define KSZ_PTP_SUBNS_BITS 32
>
> +#define KSZ_PTP_INT_START 13
> +
> static int ksz_ptp_enable_mode(struct ksz_device *dev)
> {
> struct ksz_tagger_data *tagger_data = ksz_tagger_data(dev->ds);
> @@ -415,6 +419,202 @@ void ksz_ptp_clock_unregister(struct dsa_switch *ds)
> ptp_clock_unregister(ptp_data->clock);
> }
>
> +static irqreturn_t ksz_ptp_msg_thread_fn(int irq, void *dev_id)
> +{
> + return IRQ_NONE;
> +}
> +
> +static irqreturn_t ksz_ptp_irq_thread_fn(int irq, void *dev_id)
> +{
> + struct ksz_irq *ptpirq = dev_id;
> + unsigned int nhandled = 0;
> + struct ksz_device *dev;
> + unsigned int sub_irq;
> + u16 data;
> + int ret;
> + u8 n;
> +
> + dev = ptpirq->dev;
> +
> + ret = ksz_read16(dev, ptpirq->reg_status, &data);
> + if (ret)
> + goto out;
> +
> + /* Clear the interrupts W1C */
> + ret = ksz_write16(dev, ptpirq->reg_status, data);
> + if (ret)
> + return IRQ_NONE;
> +
> + for (n = 0; n < ptpirq->nirqs; ++n) {
> + if (data & BIT(n + KSZ_PTP_INT_START)) {
> + sub_irq = irq_find_mapping(ptpirq->domain, n);
> + handle_nested_irq(sub_irq);
> + ++nhandled;
> + }
> + }
> +
> +out:
> + return (nhandled > 0 ? IRQ_HANDLED : IRQ_NONE);
> +}
> +
> +static void ksz_ptp_irq_mask(struct irq_data *d)
> +{
> + struct ksz_irq *kirq = irq_data_get_irq_chip_data(d);
> +
> + kirq->masked &= ~BIT(d->hwirq + KSZ_PTP_INT_START);
> +}
> +
> +static void ksz_ptp_irq_unmask(struct irq_data *d)
> +{
> + struct ksz_irq *kirq = irq_data_get_irq_chip_data(d);
> +
> + kirq->masked |= BIT(d->hwirq + KSZ_PTP_INT_START);
> +}
> +
> +static void ksz_ptp_irq_bus_lock(struct irq_data *d)
> +{
> + struct ksz_irq *kirq = irq_data_get_irq_chip_data(d);
> +
> + mutex_lock(&kirq->dev->lock_irq);
> +}
> +
> +static void ksz_ptp_irq_bus_sync_unlock(struct irq_data *d)
> +{
> + struct ksz_irq *kirq = irq_data_get_irq_chip_data(d);
> + struct ksz_device *dev = kirq->dev;
> + int ret;
> +
> + ret = ksz_write16(dev, kirq->reg_mask, kirq->masked);
> + if (ret)
> + dev_err(dev->dev, "failed to change IRQ mask\n");
> +
> + mutex_unlock(&dev->lock_irq);
> +}
> +
> +static const struct irq_chip ksz_ptp_irq_chip = {
> + .name = "ksz-irq",
> + .irq_mask = ksz_ptp_irq_mask,
> + .irq_unmask = ksz_ptp_irq_unmask,
> + .irq_bus_lock = ksz_ptp_irq_bus_lock,
> + .irq_bus_sync_unlock = ksz_ptp_irq_bus_sync_unlock,
> +};
> +
> +static int ksz_ptp_irq_domain_map(struct irq_domain *d,
> + unsigned int irq, irq_hw_number_t hwirq)
> +{
> + irq_set_chip_data(irq, d->host_data);
> + irq_set_chip_and_handler(irq, &ksz_ptp_irq_chip, handle_level_irq);
> + irq_set_noprobe(irq);
> +
> + return 0;
> +}
> +
> +static const struct irq_domain_ops ksz_ptp_irq_domain_ops = {
> + .map = ksz_ptp_irq_domain_map,
> + .xlate = irq_domain_xlate_twocell,
> +};
> +
> +static int ksz_ptp_msg_irq_setup(struct ksz_port *port)
> +{
> + u16 ts_reg[] = {REG_PTP_PORT_PDRESP_TS, REG_PTP_PORT_XDELAY_TS,
> + REG_PTP_PORT_SYNC_TS};
> + struct ksz_device *dev = port->ksz_dev;
> + struct ksz_irq *ptpirq = &port->ptpirq;
> + struct ksz_ptp_irq *ptpmsg_irq;
> + int ret;
> + u8 n;
> +
> + for (n = 0; n < ptpirq->nirqs; n++) {
> + ptpmsg_irq = &port->ptpmsg_irq[n];
> +
> + ptpmsg_irq->port = port;
> + ptpmsg_irq->ts_reg = dev->dev_ops->get_port_addr(port->num,
> + ts_reg[n]);
> + ptpmsg_irq->irq_num = irq_create_mapping(ptpirq->domain, n);
> + if (ptpmsg_irq->irq_num < 0) {
> + ret = ptpmsg_irq->irq_num;
> + goto out;
> + }
> +
> + snprintf(ptpmsg_irq->name, sizeof(ptpmsg_irq->name),
> + "PTP-MSG-%d", n);
> +
> + ret = request_threaded_irq(ptpmsg_irq->irq_num, NULL,
> + ksz_ptp_msg_thread_fn,
> + IRQF_ONESHOT | IRQF_TRIGGER_FALLING,

I assume that IRQF_TRIGGER_FALLING is not required here as nested interrupts are fired by
software (withing having an edge / a level). Additionally I had to remove all existing
occurrences of this flag in ksz_common.c in order to rid of persistent "timed out while
polling for tx timestamp" messages which appeared randomly after some time of operation.
I think that on i.MX6 I need to use level triggered interrupts instead of edge triggered
ones. Additionally I think that such flags should be set in the device tree instead of
the driver:

https://stackoverflow.com/a/40051191


> + ptpmsg_irq->name, ptpmsg_irq);
> + if (ret)
> + goto out;
> + }
> +
> + return 0;
> +
> +out:
> + while (n--)
> + irq_dispose_mapping(port->ptpmsg_irq[n].irq_num);
> +
> + return ret;
> +}
> +
> +int ksz_ptp_irq_setup(struct dsa_switch *ds, u8 p)
> +{
> + struct ksz_device *dev = ds->priv;
> + const struct ksz_dev_ops *ops = dev->dev_ops;
> + struct ksz_port *port = &dev->ports[p];
> + struct ksz_irq *ptpirq = &port->ptpirq;
> + int ret;
> +
> + ptpirq->dev = dev;
> + ptpirq->masked = 0;
> + ptpirq->nirqs = 3;
> + ptpirq->reg_mask = ops->get_port_addr(p, REG_PTP_PORT_TX_INT_ENABLE__2);
> + ptpirq->reg_status = ops->get_port_addr(p,
> + REG_PTP_PORT_TX_INT_STATUS__2);
> + snprintf(ptpirq->name, sizeof(ptpirq->name), "ptp_irq-%d", p);
> +
> + ptpirq->irq_num = irq_find_mapping(port->pirq.domain, PORT_SRC_PTP_INT);
> + if (ptpirq->irq_num < 0)
> + return ptpirq->irq_num;
> +
> + ptpirq->domain = irq_domain_add_simple(dev->dev->of_node, ptpirq->nirqs,
> + 0, &ksz_ptp_irq_domain_ops,
> + ptpirq);
> + if (!ptpirq->domain)
> + return -ENOMEM;
> +
> + ret = request_threaded_irq(ptpirq->irq_num, NULL, ksz_ptp_irq_thread_fn,
> + IRQF_ONESHOT | IRQF_TRIGGER_FALLING,
> + ptpirq->name, ptpirq);
> + if (ret)
> + goto out;
> +
> + ret = ksz_ptp_msg_irq_setup(port);
> + if (ret)
> + goto out;
> +
> + return 0;
> +
> +out:
> + irq_dispose_mapping(ptpirq->irq_num);
> +
> + return ret;
> +}
> +
> +void ksz_ptp_irq_free(struct dsa_switch *ds, u8 p)
> +{
> + struct ksz_device *dev = ds->priv;
> + struct ksz_port *port = &dev->ports[p];
> + struct ksz_irq *ptpirq = &port->ptpirq;
> + u8 n;
> +
> + free_irq(ptpirq->irq_num, ptpirq);
> +
> + for (n = 0; n < ptpirq->nirqs; n++)
> + irq_dispose_mapping(port->ptpmsg_irq[n].irq_num);
> +
> + irq_domain_remove(ptpirq->domain);
> +}
> +
> MODULE_AUTHOR("Christian Eggers <[email protected]>");
> MODULE_AUTHOR("Arun Ramadoss <[email protected]>");
> MODULE_DESCRIPTION("PTP support for KSZ switch");
> diff --git a/drivers/net/dsa/microchip/ksz_ptp.h b/drivers/net/dsa/microchip/ksz_ptp.h
> index 2c29a0b604bb..7c5679372705 100644
> --- a/drivers/net/dsa/microchip/ksz_ptp.h
> +++ b/drivers/net/dsa/microchip/ksz_ptp.h
> @@ -30,6 +30,8 @@ int ksz_get_ts_info(struct dsa_switch *ds, int port,
> struct ethtool_ts_info *ts);
> int ksz_hwtstamp_get(struct dsa_switch *ds, int port, struct ifreq *ifr);
> int ksz_hwtstamp_set(struct dsa_switch *ds, int port, struct ifreq *ifr);
> +int ksz_ptp_irq_setup(struct dsa_switch *ds, u8 p);
> +void ksz_ptp_irq_free(struct dsa_switch *ds, u8 p);
>
> #else
>
> @@ -45,6 +47,13 @@ static inline int ksz_ptp_clock_register(struct dsa_switch *ds)
>
> static inline void ksz_ptp_clock_unregister(struct dsa_switch *ds) { }
>
> +static inline int ksz_ptp_irq_setup(struct dsa_switch *ds, u8 p)
> +{
> + return 0;
> +}
> +
> +static inline void ksz_ptp_irq_free(struct dsa_switch *ds, u8 p) {}
> +
> #define ksz_get_ts_info NULL
>
> #define ksz_hwtstamp_get NULL
> diff --git a/drivers/net/dsa/microchip/ksz_ptp_reg.h b/drivers/net/dsa/microchip/ksz_ptp_reg.h
> index 4ca4ad4bba7e..abe95bbefc12 100644
> --- a/drivers/net/dsa/microchip/ksz_ptp_reg.h
> +++ b/drivers/net/dsa/microchip/ksz_ptp_reg.h
> @@ -49,4 +49,23 @@
> #define PTP_MASTER BIT(1)
> #define PTP_1STEP BIT(0)
>
> +/* Port PTP Register */
> +#define REG_PTP_PORT_RX_DELAY__2 0x0C00
> +#define REG_PTP_PORT_TX_DELAY__2 0x0C02
> +#define REG_PTP_PORT_ASYM_DELAY__2 0x0C04
> +
> +#define REG_PTP_PORT_XDELAY_TS 0x0C08
> +#define REG_PTP_PORT_SYNC_TS 0x0C0C
> +#define REG_PTP_PORT_PDRESP_TS 0x0C10
> +
> +#define REG_PTP_PORT_TX_INT_STATUS__2 0x0C14
> +#define REG_PTP_PORT_TX_INT_ENABLE__2 0x0C16
> +
> +#define PTP_PORT_SYNC_INT BIT(15)
> +#define PTP_PORT_XDELAY_REQ_INT BIT(14)
> +#define PTP_PORT_PDELAY_RESP_INT BIT(13)
> +#define KSZ_SYNC_MSG 2
> +#define KSZ_XDREQ_MSG 1
> +#define KSZ_PDRES_MSG 0
> +
> #endif
>




2022-12-10 01:09:15

by Florian Fainelli

[permalink] [raw]
Subject: Re: [Patch net-next v3 01/13] net: dsa: microchip: ptp: add the posix clock support

On 12/8/22 23:24, Arun Ramadoss wrote:
> From: Christian Eggers <[email protected]>
>
> This patch implement routines (adjfine, adjtime, gettime and settime)
> for manipulating the chip's PTP clock. It registers the ptp caps
> to posix clock register.
>
> Signed-off-by: Christian Eggers <[email protected]>
> Co-developed-by: Arun Ramadoss <[email protected]>
> Signed-off-by: Arun Ramadoss <[email protected]>

Reviewed-by: Florian Fainelli <[email protected]>
--
Florian

2022-12-12 10:41:51

by Arun Ramadoss

[permalink] [raw]
Subject: Re: [Patch net-next v3 05/13] net: dsa: microchip: ptp: enable interrupt for timestamping

Hi Christian,
Thanks for the comment.

On Fri, 2022-12-09 at 20:40 +0100, Christian Eggers wrote:
> EXTERNAL EMAIL: Do not click links or open attachments unless you
> know the content is safe
>
> Hi Arun,
>
> On Friday, 9 December 2022, 08:24:29 CET, Arun Ramadoss wrote:
> > PTP Interrupt mask and status register differ from the global and
> > port
> > interrupt mechanism by two methods. One is that for global/port
> > interrupt enabling we have to clear the bit but for ptp interrupt
> > we
> > have to set the bit. And other is bit12:0 is reserved in ptp
> > interrupt
> > registers. This forced to not use the generic implementation of
> > global/port interrupt method routine. This patch implement the ptp
> > interrupt mechanism to read the timestamp register for sync,
> > pdelay_req
> > and pdelay_resp.
> >
> > Signed-off-by: Arun Ramadoss <[email protected]>
> >
> > +
> > + ret = request_threaded_irq(ptpmsg_irq->irq_num, NULL,
> > + ksz_ptp_msg_thread_fn,
> > + IRQF_ONESHOT |
> > IRQF_TRIGGER_FALLING,
>
> I assume that IRQF_TRIGGER_FALLING is not required here as nested
> interrupts are fired by
> software (withing having an edge / a level). Additionally I had to
> remove all existing
> occurrences of this flag in ksz_common.c in order to rid of
> persistent "timed out while
> polling for tx timestamp" messages which appeared randomly after some
> time of operation.
> I think that on i.MX6 I need to use level triggered interrupts
> instead of edge triggered
> ones. Additionally I think that such flags should be set in the
> device tree instead of
> the driver:
>
> https://stackoverflow.com/a/40051191

I will remove IRQF_TRIGGER_FALLING from the ksz_ptp.c in next patch
version. To remove the flag in ksz_common.c, I will send bug fix patch
separately.

>
>
> > + if (ret)
> > + goto out;
> > + }
> > +
> > + return 0;
> > +
> > +out:
> > + while (n--)
> > + irq_dispose_mapping(port->ptpmsg_irq[n].irq_num);
> > +
> > + return ret;
> > +}
> >
> >
>
>
>
>