From patchwork Thu Apr 6 17:33:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kory Maincent X-Patchwork-Id: 13203774 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C20A4C7618D for ; Thu, 6 Apr 2023 17:33:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230092AbjDFRda (ORCPT ); Thu, 6 Apr 2023 13:33:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51272 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239764AbjDFRd0 (ORCPT ); Thu, 6 Apr 2023 13:33:26 -0400 Received: from relay2-d.mail.gandi.net (relay2-d.mail.gandi.net [217.70.183.194]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0C3A08A51 for ; Thu, 6 Apr 2023 10:33:13 -0700 (PDT) Received: (Authenticated sender: kory.maincent@bootlin.com) by mail.gandi.net (Postfix) with ESMTPSA id 3E09E40007; Thu, 6 Apr 2023 17:33:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1680802392; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=2X9yclkWuBhz8HR1eliBTrTM71hNyePeZyIw+C8zcKU=; b=Wr6Tz/3oeaRJAqBDGaazDeGdO/P8Sqoq3OY6GjBuqnuBqSUUDLHlp7c8d+r8YpJmSf6oKv NbVi8gCPQloejMJGjPu1OXCANKaaY1XIC1B5f9MEaDRkxwg/WBi8KfaiTfH5GLBDtT7hur cWHPO31DRFEzI4x7uAC9mFXU6Wq1s40WNtqrs9ZXWSauPzoTks3lskFIypMwj3thgl33wB uK1Qd4pShFYTxjcgiI5Yfj7yK70hc9QUlZcnZL7woN+Wyh91pRYuaHC5J78j0WfGyiQrkA 2yxyLjyg0lsmjxlmexpRgZxg2mqGV1aAtHPvQnrfvZFRr5QkIvvE4m3c0cqO7A== From: =?utf-8?q?K=C3=B6ry_Maincent?= To: netdev@vger.kernel.org Cc: kuba@kernel.org, glipus@gmail.com, maxime.chevallier@bootlin.com, vladimir.oltean@nxp.com, vadim.fedorenko@linux.dev, richardcochran@gmail.com, gerhard@engleder-embedded.com, thomas.petazzoni@bootlin.com, krzysztof.kozlowski+dt@linaro.org, robh+dt@kernel.org, linux@armlinux.org.uk, Kory Maincent Subject: [PATCH net-next RFC v4 1/5] net: ethtool: Refactor identical get_ts_info implementations. Date: Thu, 6 Apr 2023 19:33:04 +0200 Message-Id: <20230406173308.401924-2-kory.maincent@bootlin.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230406173308.401924-1-kory.maincent@bootlin.com> References: <20230406173308.401924-1-kory.maincent@bootlin.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org X-Patchwork-State: RFC From: Richard Cochran The vlan, macvlan and the bonding drivers call their "real" device driver in order to report the time stamping capabilities. Provide a core ethtool helper function to avoid copy/paste in the stack. Signed-off-by: Richard Cochran Signed-off-by: Kory Maincent --- drivers/net/bonding/bond_main.c | 14 ++------------ drivers/net/macvlan.c | 14 +------------- include/linux/ethtool.h | 8 ++++++++ net/8021q/vlan_dev.c | 15 +-------------- net/ethtool/common.c | 6 ++++++ 5 files changed, 18 insertions(+), 39 deletions(-) diff --git a/drivers/net/bonding/bond_main.c b/drivers/net/bonding/bond_main.c index 236e5219c811..322fef637059 100644 --- a/drivers/net/bonding/bond_main.c +++ b/drivers/net/bonding/bond_main.c @@ -5695,9 +5695,7 @@ static int bond_ethtool_get_ts_info(struct net_device *bond_dev, struct ethtool_ts_info *info) { struct bonding *bond = netdev_priv(bond_dev); - const struct ethtool_ops *ops; struct net_device *real_dev; - struct phy_device *phydev; int ret = 0; rcu_read_lock(); @@ -5706,16 +5704,8 @@ static int bond_ethtool_get_ts_info(struct net_device *bond_dev, rcu_read_unlock(); if (real_dev) { - ops = real_dev->ethtool_ops; - phydev = real_dev->phydev; - - if (phy_has_tsinfo(phydev)) { - ret = phy_ts_info(phydev, info); - goto out; - } else if (ops->get_ts_info) { - ret = ops->get_ts_info(real_dev, info); - goto out; - } + ret = ethtool_get_ts_info_by_layer(real_dev, info); + goto out; } info->so_timestamping = SOF_TIMESTAMPING_RX_SOFTWARE | diff --git a/drivers/net/macvlan.c b/drivers/net/macvlan.c index 4a53debf9d7c..a396a35fec2e 100644 --- a/drivers/net/macvlan.c +++ b/drivers/net/macvlan.c @@ -1093,20 +1093,8 @@ static int macvlan_ethtool_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info) { struct net_device *real_dev = macvlan_dev_real_dev(dev); - const struct ethtool_ops *ops = real_dev->ethtool_ops; - struct phy_device *phydev = real_dev->phydev; - if (phy_has_tsinfo(phydev)) { - return phy_ts_info(phydev, info); - } else if (ops->get_ts_info) { - return ops->get_ts_info(real_dev, info); - } else { - info->so_timestamping = SOF_TIMESTAMPING_RX_SOFTWARE | - SOF_TIMESTAMPING_SOFTWARE; - info->phc_index = -1; - } - - return 0; + return ethtool_get_ts_info_by_layer(real_dev, info); } static netdev_features_t macvlan_fix_features(struct net_device *dev, diff --git a/include/linux/ethtool.h b/include/linux/ethtool.h index 798d35890118..a21302032dfa 100644 --- a/include/linux/ethtool.h +++ b/include/linux/ethtool.h @@ -1042,6 +1042,14 @@ static inline int ethtool_mm_frag_size_min_to_add(u32 val_min, u32 *val_add, return -EINVAL; } +/** + * ethtool_get_ts_info_by_layer - Obtains time stamping capabilities from the MAC or PHY layer. + * @dev: pointer to net_device structure + * @info: buffer to hold the result + * Returns zero on sauces, non-zero otherwise. + */ +int ethtool_get_ts_info_by_layer(struct net_device *dev, struct ethtool_ts_info *info); + /** * ethtool_sprintf - Write formatted string to ethtool string data * @data: Pointer to start of string to update diff --git a/net/8021q/vlan_dev.c b/net/8021q/vlan_dev.c index 5920544e93e8..7c33c7216f35 100644 --- a/net/8021q/vlan_dev.c +++ b/net/8021q/vlan_dev.c @@ -683,20 +683,7 @@ static int vlan_ethtool_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info) { const struct vlan_dev_priv *vlan = vlan_dev_priv(dev); - const struct ethtool_ops *ops = vlan->real_dev->ethtool_ops; - struct phy_device *phydev = vlan->real_dev->phydev; - - if (phy_has_tsinfo(phydev)) { - return phy_ts_info(phydev, info); - } else if (ops->get_ts_info) { - return ops->get_ts_info(vlan->real_dev, info); - } else { - info->so_timestamping = SOF_TIMESTAMPING_RX_SOFTWARE | - SOF_TIMESTAMPING_SOFTWARE; - info->phc_index = -1; - } - - return 0; + return ethtool_get_ts_info_by_layer(vlan->real_dev, info); } static void vlan_dev_get_stats64(struct net_device *dev, diff --git a/net/ethtool/common.c b/net/ethtool/common.c index 5fb19050991e..695c7c4a816b 100644 --- a/net/ethtool/common.c +++ b/net/ethtool/common.c @@ -661,6 +661,12 @@ int ethtool_get_phc_vclocks(struct net_device *dev, int **vclock_index) } EXPORT_SYMBOL(ethtool_get_phc_vclocks); +int ethtool_get_ts_info_by_layer(struct net_device *dev, struct ethtool_ts_info *info) +{ + return __ethtool_get_ts_info(dev, info); +} +EXPORT_SYMBOL(ethtool_get_ts_info_by_layer); + const struct ethtool_phy_ops *ethtool_phy_ops; void ethtool_set_ethtool_phy_ops(const struct ethtool_phy_ops *ops) From patchwork Thu Apr 6 17:33:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kory Maincent X-Patchwork-Id: 13203775 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0D796C76196 for ; Thu, 6 Apr 2023 17:33:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239784AbjDFRdb (ORCPT ); Thu, 6 Apr 2023 13:33:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51196 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239416AbjDFRd0 (ORCPT ); Thu, 6 Apr 2023 13:33:26 -0400 Received: from relay2-d.mail.gandi.net (relay2-d.mail.gandi.net [217.70.183.194]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 12FC09012 for ; Thu, 6 Apr 2023 10:33:15 -0700 (PDT) Received: (Authenticated sender: kory.maincent@bootlin.com) by mail.gandi.net (Postfix) with ESMTPSA id EAF4E40004; Thu, 6 Apr 2023 17:33:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1680802394; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3KPqk2KxJIKptib5iRH01V/CYIzs4u7kdne+zBfrxPM=; b=eQcDAo9cK4kjM9z+oTGdTwnxGvwTtcmxA4SmCrLjRgAk/Q6y7T/b/yTiYZaI9C5fqOmxk9 2F2f1Plc4fBW8hz/L9jbDI+7ffl6uK6Y/svVV/q5qxpr3jL19jPRW1/GT8zjN3c13+azbg enKMYOZQzCQsDMYfCHe5mWk4WAvudCyLjhAolvwMUKjON2doSfNy48PL6fzpDtG/mpneCK rJlezccFJJ8w6ZSNHiIGugySFLuplBwDN5m9zySKpBFppQot02W19Y1HFMvTh+IY8mufgA MfkYZxwmd5pP5lF2GHyeS+hdnuNNjwSyKincOzqVuteabBGu3UAQ0W3RZZBzEQ== From: =?utf-8?q?K=C3=B6ry_Maincent?= To: netdev@vger.kernel.org Cc: kuba@kernel.org, glipus@gmail.com, maxime.chevallier@bootlin.com, vladimir.oltean@nxp.com, vadim.fedorenko@linux.dev, richardcochran@gmail.com, gerhard@engleder-embedded.com, thomas.petazzoni@bootlin.com, krzysztof.kozlowski+dt@linaro.org, robh+dt@kernel.org, linux@armlinux.org.uk, Kory Maincent Subject: [PATCH net-next RFC v4 2/5] net: Expose available time stamping layers to user space. Date: Thu, 6 Apr 2023 19:33:05 +0200 Message-Id: <20230406173308.401924-3-kory.maincent@bootlin.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230406173308.401924-1-kory.maincent@bootlin.com> References: <20230406173308.401924-1-kory.maincent@bootlin.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org X-Patchwork-State: RFC From: Kory Maincent Time stamping on network packets may happen either in the MAC or in the PHY, but not both. In preparation for making the choice selectable, expose both the current and available layers via ethtool. In accordance with the kernel implementation as it stands, the current layer will always read as "phy" when a PHY time stamping device is present. Future patches will allow changing the current layer administratively. Signed-off-by: Richard Cochran Signed-off-by: Kory Maincent --- Notes: Changes in v2: - Move the introduction of selected_timestamping_layer variable in next patch. Changes in v3: - Move on to ethtool instead of syfs Changes in v4: - Move on to netlink ethtool instead of ioctl. I am not familiar with netlink so there might be some code that does not follow the good code practice. Documentation/networking/ethtool-netlink.rst | 40 +++++++ include/uapi/linux/ethtool_netlink.h | 15 +++ include/uapi/linux/net_tstamp.h | 8 ++ net/ethtool/Makefile | 2 +- net/ethtool/netlink.c | 20 ++++ net/ethtool/netlink.h | 3 + net/ethtool/ts.c | 114 +++++++++++++++++++ 7 files changed, 201 insertions(+), 1 deletion(-) create mode 100644 net/ethtool/ts.c diff --git a/Documentation/networking/ethtool-netlink.rst b/Documentation/networking/ethtool-netlink.rst index cd0973d4ba01..539425fdaf7c 100644 --- a/Documentation/networking/ethtool-netlink.rst +++ b/Documentation/networking/ethtool-netlink.rst @@ -210,6 +210,8 @@ Userspace to kernel: ``ETHTOOL_MSG_EEE_GET`` get EEE settings ``ETHTOOL_MSG_EEE_SET`` set EEE settings ``ETHTOOL_MSG_TSINFO_GET`` get timestamping info + ``ETHTOOL_MSG_TS_GET`` get current hardware timestamping + ``ETHTOOL_MSG_TSLIST_GET`` list available hardware timestamping ``ETHTOOL_MSG_CABLE_TEST_ACT`` action start cable test ``ETHTOOL_MSG_CABLE_TEST_TDR_ACT`` action start raw TDR cable test ``ETHTOOL_MSG_TUNNEL_INFO_GET`` get tunnel offload info @@ -1990,6 +1992,42 @@ The attributes are propagated to the driver through the following structure: .. kernel-doc:: include/linux/ethtool.h :identifiers: ethtool_mm_cfg +TS_GET +====== + +Gets transceiver module parameters. + +Request contents: + + ================================= ====== ========================== + ``ETHTOOL_A_TS_HEADER`` nested request header + ================================= ====== ========================== + +Kernel response contents: + + ======================= ====== ==================================== + ``ETHTOOL_A_TS_HEADER`` nested reply header + ``ETHTOOL_A_TS_LAYER`` u32 current hardware timestamping + ======================= ====== ==================================== + +TSLIST_GET +========== + +Gets transceiver module parameters. + +Request contents: + + ================================= ====== ========================== + ``ETHTOOL_A_TS_HEADER`` nested request header + ================================= ====== ========================== + +Kernel response contents: + + ======================= ====== =================================== + ``ETHTOOL_A_TS_HEADER`` nested reply header + ``ETHTOOL_A_TS_LAYER`` u32 available hardware timestamping + ======================= ====== =================================== + Request translation =================== @@ -2096,4 +2134,6 @@ are netlink only. n/a ``ETHTOOL_MSG_PLCA_GET_STATUS`` n/a ``ETHTOOL_MSG_MM_GET`` n/a ``ETHTOOL_MSG_MM_SET`` + n/a ``ETHTOOL_MSG_TS_GET`` + n/a ``ETHTOOL_MSG_TSLIST_GET`` =================================== ===================================== diff --git a/include/uapi/linux/ethtool_netlink.h b/include/uapi/linux/ethtool_netlink.h index 1ebf8d455f07..447908393b91 100644 --- a/include/uapi/linux/ethtool_netlink.h +++ b/include/uapi/linux/ethtool_netlink.h @@ -39,6 +39,8 @@ enum { ETHTOOL_MSG_EEE_GET, ETHTOOL_MSG_EEE_SET, ETHTOOL_MSG_TSINFO_GET, + ETHTOOL_MSG_TSLIST_GET, + ETHTOOL_MSG_TS_GET, ETHTOOL_MSG_CABLE_TEST_ACT, ETHTOOL_MSG_CABLE_TEST_TDR_ACT, ETHTOOL_MSG_TUNNEL_INFO_GET, @@ -92,6 +94,8 @@ enum { ETHTOOL_MSG_EEE_GET_REPLY, ETHTOOL_MSG_EEE_NTF, ETHTOOL_MSG_TSINFO_GET_REPLY, + ETHTOOL_MSG_TSLIST_GET_REPLY, + ETHTOOL_MSG_TS_GET_REPLY, ETHTOOL_MSG_CABLE_TEST_NTF, ETHTOOL_MSG_CABLE_TEST_TDR_NTF, ETHTOOL_MSG_TUNNEL_INFO_GET_REPLY, @@ -484,6 +488,17 @@ enum { ETHTOOL_A_TSINFO_MAX = (__ETHTOOL_A_TSINFO_CNT - 1) }; +/* TS LAYER */ + +enum { + ETHTOOL_A_TS_UNSPEC, + ETHTOOL_A_TS_HEADER, /* nest - _A_HEADER_* */ + ETHTOOL_A_TS_LAYER, /* u32 */ + + /* add new constants above here */ + __ETHTOOL_A_TS_CNT, + ETHTOOL_A_TS_MAX = (__ETHTOOL_A_TS_CNT - 1) +}; /* PHC VCLOCKS */ enum { diff --git a/include/uapi/linux/net_tstamp.h b/include/uapi/linux/net_tstamp.h index a2c66b3d7f0f..d7c1798d45fe 100644 --- a/include/uapi/linux/net_tstamp.h +++ b/include/uapi/linux/net_tstamp.h @@ -13,6 +13,14 @@ #include #include /* for SO_TIMESTAMPING */ +/* Hardware layer of the SO_TIMESTAMPING provider */ +enum timestamping_layer { + SOF_MAC_TIMESTAMPING = (1<<0), + SOF_PHY_TIMESTAMPING = (1<<1), + + SOF_LAYER_TIMESTAMPING_LAST = SOF_PHY_TIMESTAMPING, +}; + /* SO_TIMESTAMPING flags */ enum { SOF_TIMESTAMPING_TX_HARDWARE = (1<<0), diff --git a/net/ethtool/Makefile b/net/ethtool/Makefile index 504f954a1b28..4ea64c080639 100644 --- a/net/ethtool/Makefile +++ b/net/ethtool/Makefile @@ -8,4 +8,4 @@ ethtool_nl-y := netlink.o bitset.o strset.o linkinfo.o linkmodes.o rss.o \ linkstate.o debug.o wol.o features.o privflags.o rings.o \ channels.o coalesce.o pause.o eee.o tsinfo.o cabletest.o \ tunnels.o fec.o eeprom.o stats.o phc_vclocks.o mm.o \ - module.o pse-pd.o plca.o mm.o + module.o pse-pd.o plca.o mm.o ts.o diff --git a/net/ethtool/netlink.c b/net/ethtool/netlink.c index 08120095cc68..8d9e27b13e28 100644 --- a/net/ethtool/netlink.c +++ b/net/ethtool/netlink.c @@ -293,6 +293,8 @@ ethnl_default_requests[__ETHTOOL_MSG_USER_CNT] = { [ETHTOOL_MSG_FEC_GET] = ðnl_fec_request_ops, [ETHTOOL_MSG_FEC_SET] = ðnl_fec_request_ops, [ETHTOOL_MSG_TSINFO_GET] = ðnl_tsinfo_request_ops, + [ETHTOOL_MSG_TS_GET] = ðnl_ts_request_ops, + [ETHTOOL_MSG_TSLIST_GET] = ðnl_tslist_request_ops, [ETHTOOL_MSG_MODULE_EEPROM_GET] = ðnl_module_eeprom_request_ops, [ETHTOOL_MSG_STATS_GET] = ðnl_stats_request_ops, [ETHTOOL_MSG_PHC_VCLOCKS_GET] = ðnl_phc_vclocks_request_ops, @@ -1011,6 +1013,24 @@ static const struct genl_ops ethtool_genl_ops[] = { .policy = ethnl_tsinfo_get_policy, .maxattr = ARRAY_SIZE(ethnl_tsinfo_get_policy) - 1, }, + { + .cmd = ETHTOOL_MSG_TSLIST_GET, + .doit = ethnl_default_doit, + .start = ethnl_default_start, + .dumpit = ethnl_default_dumpit, + .done = ethnl_default_done, + .policy = ethnl_ts_get_policy, + .maxattr = ARRAY_SIZE(ethnl_ts_get_policy) - 1, + }, + { + .cmd = ETHTOOL_MSG_TS_GET, + .doit = ethnl_default_doit, + .start = ethnl_default_start, + .dumpit = ethnl_default_dumpit, + .done = ethnl_default_done, + .policy = ethnl_ts_get_policy, + .maxattr = ARRAY_SIZE(ethnl_ts_get_policy) - 1, + }, { .cmd = ETHTOOL_MSG_CABLE_TEST_ACT, .flags = GENL_UNS_ADMIN_PERM, diff --git a/net/ethtool/netlink.h b/net/ethtool/netlink.h index 79424b34b553..49c700777a32 100644 --- a/net/ethtool/netlink.h +++ b/net/ethtool/netlink.h @@ -385,6 +385,8 @@ extern const struct ethnl_request_ops ethnl_coalesce_request_ops; extern const struct ethnl_request_ops ethnl_pause_request_ops; extern const struct ethnl_request_ops ethnl_eee_request_ops; extern const struct ethnl_request_ops ethnl_tsinfo_request_ops; +extern const struct ethnl_request_ops ethnl_ts_request_ops; +extern const struct ethnl_request_ops ethnl_tslist_request_ops; extern const struct ethnl_request_ops ethnl_fec_request_ops; extern const struct ethnl_request_ops ethnl_module_eeprom_request_ops; extern const struct ethnl_request_ops ethnl_stats_request_ops; @@ -423,6 +425,7 @@ extern const struct nla_policy ethnl_pause_set_policy[ETHTOOL_A_PAUSE_TX + 1]; extern const struct nla_policy ethnl_eee_get_policy[ETHTOOL_A_EEE_HEADER + 1]; extern const struct nla_policy ethnl_eee_set_policy[ETHTOOL_A_EEE_TX_LPI_TIMER + 1]; extern const struct nla_policy ethnl_tsinfo_get_policy[ETHTOOL_A_TSINFO_HEADER + 1]; +extern const struct nla_policy ethnl_ts_get_policy[ETHTOOL_A_TS_HEADER + 1]; extern const struct nla_policy ethnl_cable_test_act_policy[ETHTOOL_A_CABLE_TEST_HEADER + 1]; extern const struct nla_policy ethnl_cable_test_tdr_act_policy[ETHTOOL_A_CABLE_TEST_TDR_CFG + 1]; extern const struct nla_policy ethnl_tunnel_info_get_policy[ETHTOOL_A_TUNNEL_INFO_HEADER + 1]; diff --git a/net/ethtool/ts.c b/net/ethtool/ts.c new file mode 100644 index 000000000000..a71c47ff0c6b --- /dev/null +++ b/net/ethtool/ts.c @@ -0,0 +1,114 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include +#include + +#include "netlink.h" +#include "common.h" +#include "bitset.h" + +struct ts_req_info { + struct ethnl_req_info base; +}; + +struct ts_reply_data { + struct ethnl_reply_data base; + u32 ts; +}; + +#define TS_REPDATA(__reply_base) \ + container_of(__reply_base, struct ts_reply_data, base) + +/* TS_GET */ +const struct nla_policy ethnl_ts_get_policy[] = { + [ETHTOOL_A_TS_HEADER] = + NLA_POLICY_NESTED(ethnl_header_policy), +}; + +static int ts_prepare_data(const struct ethnl_req_info *req_base, + struct ethnl_reply_data *reply_base, + struct genl_info *info) +{ + struct ts_reply_data *data = TS_REPDATA(reply_base); + struct net_device *dev = reply_base->dev; + const struct ethtool_ops *ops = dev->ethtool_ops; + int ret; + + ret = ethnl_ops_begin(dev); + if (ret < 0) + return ret; + + if (phy_has_tsinfo(dev->phydev)) + data->ts = SOF_PHY_TIMESTAMPING; + else if (ops->get_ts_info) + data->ts = SOF_MAC_TIMESTAMPING; + else + return -EOPNOTSUPP; + + ethnl_ops_complete(dev); + + return ret; +} + +static int ts_reply_size(const struct ethnl_req_info *req_base, + const struct ethnl_reply_data *reply_base) +{ + return nla_total_size(sizeof(u32)); +} + +static int ts_fill_reply(struct sk_buff *skb, + const struct ethnl_req_info *req_base, + const struct ethnl_reply_data *reply_base) +{ + struct ts_reply_data *data = TS_REPDATA(reply_base); + return nla_put_u32(skb, ETHTOOL_A_TS_LAYER, data->ts); +} + +const struct ethnl_request_ops ethnl_ts_request_ops = { + .request_cmd = ETHTOOL_MSG_TS_GET, + .reply_cmd = ETHTOOL_MSG_TS_GET_REPLY, + .hdr_attr = ETHTOOL_A_TS_HEADER, + .req_info_size = sizeof(struct ts_req_info), + .reply_data_size = sizeof(struct ts_reply_data), + + .prepare_data = ts_prepare_data, + .reply_size = ts_reply_size, + .fill_reply = ts_fill_reply, +}; + +/* TSLIST_GET */ +static int tslist_prepare_data(const struct ethnl_req_info *req_base, + struct ethnl_reply_data *reply_base, + struct genl_info *info) +{ + struct ts_reply_data *data = TS_REPDATA(reply_base); + struct net_device *dev = reply_base->dev; + const struct ethtool_ops *ops = dev->ethtool_ops; + int ret; + + ret = ethnl_ops_begin(dev); + if (ret < 0) + return ret; + + data->ts = 0; + if (phy_has_tsinfo(dev->phydev)) + data->ts = SOF_PHY_TIMESTAMPING; + if (ops->get_ts_info) + data->ts |= SOF_MAC_TIMESTAMPING; + + ethnl_ops_complete(dev); + + return ret; +} + +const struct ethnl_request_ops ethnl_tslist_request_ops = { + .request_cmd = ETHTOOL_MSG_TSLIST_GET, + .reply_cmd = ETHTOOL_MSG_TSLIST_GET_REPLY, + .hdr_attr = ETHTOOL_A_TS_HEADER, + .req_info_size = sizeof(struct ts_req_info), + .reply_data_size = sizeof(struct ts_reply_data), + + .prepare_data = tslist_prepare_data, + .reply_size = ts_reply_size, + .fill_reply = ts_fill_reply, +}; From patchwork Thu Apr 6 17:33:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kory Maincent X-Patchwork-Id: 13203776 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 345B3C7618D for ; Thu, 6 Apr 2023 17:33:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239451AbjDFRdd (ORCPT ); Thu, 6 Apr 2023 13:33:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51272 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229674AbjDFRd3 (ORCPT ); Thu, 6 Apr 2023 13:33:29 -0400 Received: from relay2-d.mail.gandi.net (relay2-d.mail.gandi.net [IPv6:2001:4b98:dc4:8::222]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 479CA93F4 for ; Thu, 6 Apr 2023 10:33:19 -0700 (PDT) Received: (Authenticated sender: kory.maincent@bootlin.com) by mail.gandi.net (Postfix) with ESMTPSA id 4087440009; Thu, 6 Apr 2023 17:33:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1680802397; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=lE+ScTHLoYOTTINdJCo2R2wh+bH4oHAkPrK/mHLAqkQ=; b=f0W1tbVTTYSTgyLGWEaUacd8Fq1iQGfxKgly4uXEAxMS04op2A9G4jDaF2JVDYHF/VLITN KdhA6VqnHkI1mHZ3PbT3i8BGTaqYYhhoGbjRKoiPCuJFglFM+lBoof8PZMH5JI+zJbZA// 9cFq6eVg9O+o28uyyeeRJiJPU8YSSoaItGJpaemamO2uuPhon9lXzHa0ADzHiGO8Z/BPM3 t8piJuGCaN9yXFytvZJjXza287N0sHhvXQNd22x+d3mKb6gDJ62sO7sS8LNHfFIVtbOPce PPLtLdQo1KWktO9jnjHrNW65wWBVe9ONN3jfXB3XDu4K/Rpv0oioxT0CBWHySw== From: =?utf-8?q?K=C3=B6ry_Maincent?= To: netdev@vger.kernel.org Cc: kuba@kernel.org, glipus@gmail.com, maxime.chevallier@bootlin.com, vladimir.oltean@nxp.com, vadim.fedorenko@linux.dev, richardcochran@gmail.com, gerhard@engleder-embedded.com, thomas.petazzoni@bootlin.com, krzysztof.kozlowski+dt@linaro.org, robh+dt@kernel.org, linux@armlinux.org.uk, Kory Maincent Subject: [PATCH net-next RFC v4 3/5] dt-bindings: net: phy: add timestamp preferred choice property Date: Thu, 6 Apr 2023 19:33:06 +0200 Message-Id: <20230406173308.401924-4-kory.maincent@bootlin.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230406173308.401924-1-kory.maincent@bootlin.com> References: <20230406173308.401924-1-kory.maincent@bootlin.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org X-Patchwork-State: RFC From: Kory Maincent Add property to select the preferred hardware timestamp layer. The choice of using devicetree binding has been made as the PTP precision and quality depends of external things, like adjustable clock, or the lack of a temperature compensated crystal or specific features. Even if the preferred timestamp is a configuration it is hardly related to the design of the board. Signed-off-by: Kory Maincent --- Documentation/devicetree/bindings/net/ethernet-phy.yaml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Documentation/devicetree/bindings/net/ethernet-phy.yaml b/Documentation/devicetree/bindings/net/ethernet-phy.yaml index ac04f8efa35c..32d7ef7520e6 100644 --- a/Documentation/devicetree/bindings/net/ethernet-phy.yaml +++ b/Documentation/devicetree/bindings/net/ethernet-phy.yaml @@ -144,6 +144,13 @@ properties: Mark the corresponding energy efficient ethernet mode as broken and request the ethernet to stop advertising it. + preferred-timestamp: + enum: + - phy + - mac + description: + Specifies the preferred hardware timestamp layer. + pses: $ref: /schemas/types.yaml#/definitions/phandle-array maxItems: 1 From patchwork Thu Apr 6 17:33:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kory Maincent X-Patchwork-Id: 13203777 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9A047C77B6F for ; Thu, 6 Apr 2023 17:33:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239764AbjDFRdo (ORCPT ); Thu, 6 Apr 2023 13:33:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51784 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240032AbjDFRdl (ORCPT ); Thu, 6 Apr 2023 13:33:41 -0400 Received: from relay2-d.mail.gandi.net (relay2-d.mail.gandi.net [IPv6:2001:4b98:dc4:8::222]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 87B1A93D8 for ; Thu, 6 Apr 2023 10:33:21 -0700 (PDT) Received: (Authenticated sender: kory.maincent@bootlin.com) by mail.gandi.net (Postfix) with ESMTPSA id 183F240005; Thu, 6 Apr 2023 17:33:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1680802400; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=AO+WFZSi5dejShmV6sNCLEI3ZlhMdfKqwn6QZy3oVuU=; b=GVutoYDUHz8i8nMVUkdXgANPPeKk98WnKoGCjIgb2H0PfkZ1wmuy9Ds501jCi3zHqCud/+ oLroba+m7mTrUdD6G+KpCvcDRLeTQ+AkrxNh9my1szVeHNxDaCIS6VZbhqg9/t/1oT/jOV f3nVR6NeibcQemVB8E2h6/igBvNn3IZBJQOLeQut+IYLUat3tUUXe5T7ZY71Hodhp8HBuj xyYCaYAmGJPduzLk8RpyodyyBqM4JOISz1pYA6echA+9fa0eKgnuuf/JCv6PbzZTh87h0k KBcvwPAAIctZ9ZTXgqKpYIgrucx73f+Bg6sle5f8SOQ+r2GfMP68gIeoWcatdw== From: =?utf-8?q?K=C3=B6ry_Maincent?= To: netdev@vger.kernel.org Cc: kuba@kernel.org, glipus@gmail.com, maxime.chevallier@bootlin.com, vladimir.oltean@nxp.com, vadim.fedorenko@linux.dev, richardcochran@gmail.com, gerhard@engleder-embedded.com, thomas.petazzoni@bootlin.com, krzysztof.kozlowski+dt@linaro.org, robh+dt@kernel.org, linux@armlinux.org.uk, Kory Maincent Subject: [PATCH net-next RFC v4 4/5] net: Let the active time stamping layer be selectable. Date: Thu, 6 Apr 2023 19:33:07 +0200 Message-Id: <20230406173308.401924-5-kory.maincent@bootlin.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230406173308.401924-1-kory.maincent@bootlin.com> References: <20230406173308.401924-1-kory.maincent@bootlin.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org X-Patchwork-State: RFC From: Kory Maincent Add the ETHTOOL_MSG_TS_SET ethtool netlink socket, and add checks in the ioctl and time stamping paths to respect the currently selected time stamping layer. Add a preferred-timestamp devicetree binding to select the preferred hardware timestamp layer between PHY and MAC. The choice of using devicetree binding has been made as the PTP precision and quality depends of external things, like adjustable clock, or the lack of a temperature compensated crystal or specific features. Even if the preferred timestamp is a configuration it is hardly related to the design oh the board. Introduce a NETDEV_CHANGE_HWTSTAMP netdev notifier to let MAC or DSA know when a hwtstamp change happen. This is useful to manage packet trap in that case like done by the lan966x driver. Change the API to select MAC default time stamping instead of the PHY. Indeed the PHY is closer to the wire therefore theoretically it have less delay than the MAC timestamping but the reality is different. Due to lower time stamping clock frequency, latency in the MDIO bus and no PHC hardware synchronization between different PHY, the PHY PTP is often less precise than the MAC. The exception is for PHY designed specially for PTP case but these board are not very widespread. For not breaking the compatibility I introduce a whitelist to reference all current PHY that support time stamping and let them keep the old API behavior. Signed-off-by: Richard Cochran Signed-off-by: Kory Maincent --- Notes: Changes in v2: - Move selected_timestamping_layer introduction in this patch. - Replace strmcmp by sysfs_streq. - Use the PHY timestamp only if available. Changes in v3: - Add a devicetree binding to select the preferred timestamp - Replace the way to select timestamp through ethtool instead of sysfs You can test it with the ethtool source on branch feature_ptp of: https://github.com/kmaincent/ethtool Changes in v4: - Change the API to select MAC default time stamping instead of the PHY. - Add a whitelist to no break the old timestamp PHY default preferences for current PHY. - Replace the ethtool ioctl by netlink. - Add a netdev notifier to allow network device to create trap on PTP packets. Not tested as it need to change the lan966x driver that implement packet traps. I will do after the hwtstamp management change to NDOs. Documentation/networking/ethtool-netlink.rst | 13 +++ drivers/net/phy/phy_device.c | 85 ++++++++++++++++++++ include/linux/netdevice.h | 12 +++ include/uapi/linux/ethtool_netlink.h | 2 + net/core/dev.c | 2 +- net/core/dev_ioctl.c | 56 ++++++++++++- net/core/timestamping.c | 6 ++ net/ethtool/common.c | 15 +++- net/ethtool/netlink.c | 10 +++ net/ethtool/netlink.h | 1 + net/ethtool/ts.c | 68 ++++++++++++++-- 11 files changed, 256 insertions(+), 14 deletions(-) diff --git a/Documentation/networking/ethtool-netlink.rst b/Documentation/networking/ethtool-netlink.rst index 539425fdaf7c..12996f38b956 100644 --- a/Documentation/networking/ethtool-netlink.rst +++ b/Documentation/networking/ethtool-netlink.rst @@ -2028,6 +2028,18 @@ Kernel response contents: ``ETHTOOL_A_TS_LAYER`` u32 available hardware timestamping ======================= ====== =================================== +TS_SET +====== + +Gets transceiver module parameters. + +Request contents: + + ============================= ====== ============================== + ``ETHTOOL_A_TS_HEADER`` nested request header + ``ETHTOOL_A_TS_LAYER`` u32 set hardware timestamping + ============================= ====== ============================== + Request translation =================== @@ -2136,4 +2148,5 @@ are netlink only. n/a ``ETHTOOL_MSG_MM_SET`` n/a ``ETHTOOL_MSG_TS_GET`` n/a ``ETHTOOL_MSG_TSLIST_GET`` + n/a ``ETHTOOL_MSG_TS_SET`` =================================== ===================================== diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c index 917ba84105fc..d415c62938cd 100644 --- a/drivers/net/phy/phy_device.c +++ b/drivers/net/phy/phy_device.c @@ -34,6 +34,8 @@ #include #include #include +#include +#include MODULE_DESCRIPTION("PHY library"); MODULE_AUTHOR("Andy Fleming"); @@ -1408,6 +1410,83 @@ int phy_sfp_probe(struct phy_device *phydev, } EXPORT_SYMBOL(phy_sfp_probe); +/** + * A whitelist for PHYs selected as default timesetamping. + * Its use is to keep compatibility with old PTP API which is selecting + * these PHYs as default timestamping. + * The new API is selecting the MAC as default timestamping. + */ +const char * const phy_timestamping_whitelist[] = { + "Broadcom BCM5411", + "Broadcom BCM5421", + "Broadcom BCM54210E", + "Broadcom BCM5461", + "Broadcom BCM54612E", + "Broadcom BCM5464", + "Broadcom BCM5481", + "Broadcom BCM54810", + "Broadcom BCM54811", + "Broadcom BCM5482", + "Broadcom BCM50610", + "Broadcom BCM50610M", + "Broadcom BCM57780", + "Broadcom BCM5395", + "Broadcom BCM53125", + "Broadcom BCM53128", + "Broadcom BCM89610", + "NatSemi DP83640", + "Microchip LAN8841 Gigabit PHY" + "Microchip INDY Gigabit Quad PHY", + "Microsemi GE VSC856X SyncE", + "Microsemi GE VSC8575 SyncE", + "Microsemi GE VSC8582 SyncE", + "Microsemi GE VSC8584 SyncE", + "NXP C45 TJA1103", + NULL, +}; + +static void of_set_timestamp(struct net_device *netdev, struct phy_device *phydev) +{ + struct device_node *node = phydev->mdio.dev.of_node; + const struct ethtool_ops *ops = netdev->ethtool_ops; + const char *s; + enum timestamping_layer ts_layer = 0; + int i; + + /* Backward compatibility to old API */ + for (i = 0; phy_timestamping_whitelist[i] != NULL; i++) + { + if (!strcmp(phy_timestamping_whitelist[i], + phydev->drv->name)) { + if (phy_has_hwtstamp(phydev)) + ts_layer = SOF_PHY_TIMESTAMPING; + else if (ops->get_ts_info) + ts_layer = SOF_MAC_TIMESTAMPING; + goto out; + } + } + + if (ops->get_ts_info) + ts_layer = SOF_MAC_TIMESTAMPING; + else if (phy_has_hwtstamp(phydev)) + ts_layer = SOF_PHY_TIMESTAMPING; + + if (of_property_read_string(node, "preferred-timestamp", &s)) + goto out; + + if (!s) + goto out; + + if (phy_has_hwtstamp(phydev) && !strcmp(s, "phy")) + ts_layer = SOF_PHY_TIMESTAMPING; + + if (ops->get_ts_info && !strcmp(s, "mac")) + ts_layer = SOF_MAC_TIMESTAMPING; + +out: + netdev->selected_timestamping_layer = ts_layer; +} + /** * phy_attach_direct - attach a network device to a given PHY device pointer * @dev: network device to attach @@ -1481,6 +1560,8 @@ int phy_attach_direct(struct net_device *dev, struct phy_device *phydev, phydev->phy_link_change = phy_link_change; if (dev) { + of_set_timestamp(dev, phydev); + phydev->attached_dev = dev; dev->phydev = phydev; @@ -1811,6 +1892,10 @@ void phy_detach(struct phy_device *phydev) phy_suspend(phydev); if (dev) { + if (dev->ethtool_ops->get_ts_info) + dev->selected_timestamping_layer = SOF_MAC_TIMESTAMPING; + else + dev->selected_timestamping_layer = 0; phydev->attached_dev->phydev = NULL; phydev->attached_dev = NULL; } diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h index a740be3bb911..3dd6be012daf 100644 --- a/include/linux/netdevice.h +++ b/include/linux/netdevice.h @@ -48,6 +48,7 @@ #include #include #include +#include #include #include #include @@ -2019,6 +2020,9 @@ enum netdev_ml_priv_type { * * @threaded: napi threaded mode is enabled * + * @selected_timestamping_layer: Tracks whether the MAC or the PHY + * performs packet time stamping. + * * @net_notifier_list: List of per-net netdev notifier block * that follow this device when it is moved * to another network namespace. @@ -2388,6 +2392,8 @@ struct net_device { unsigned wol_enabled:1; unsigned threaded:1; + enum timestamping_layer selected_timestamping_layer; + struct list_head net_notifier_list; #if IS_ENABLED(CONFIG_MACSEC) @@ -2879,6 +2885,7 @@ enum netdev_cmd { NETDEV_OFFLOAD_XSTATS_REPORT_DELTA, NETDEV_XDP_FEAT_CHANGE, NETDEV_PRE_CHANGE_HWTSTAMP, + NETDEV_CHANGE_HWTSTAMP, }; const char *netdev_cmd_to_name(enum netdev_cmd cmd); @@ -2934,6 +2941,11 @@ struct netdev_notifier_hwtstamp_info { struct kernel_hwtstamp_config *config; }; +struct netdev_notifier_hwtstamp_layer { + struct netdev_notifier_info info; /* must be first */ + enum timestamping_layer ts_layer; +}; + enum netdev_offload_xstats_type { NETDEV_OFFLOAD_XSTATS_TYPE_L3 = 1, }; diff --git a/include/uapi/linux/ethtool_netlink.h b/include/uapi/linux/ethtool_netlink.h index 447908393b91..4f03e7cde271 100644 --- a/include/uapi/linux/ethtool_netlink.h +++ b/include/uapi/linux/ethtool_netlink.h @@ -41,6 +41,7 @@ enum { ETHTOOL_MSG_TSINFO_GET, ETHTOOL_MSG_TSLIST_GET, ETHTOOL_MSG_TS_GET, + ETHTOOL_MSG_TS_SET, ETHTOOL_MSG_CABLE_TEST_ACT, ETHTOOL_MSG_CABLE_TEST_TDR_ACT, ETHTOOL_MSG_TUNNEL_INFO_GET, @@ -96,6 +97,7 @@ enum { ETHTOOL_MSG_TSINFO_GET_REPLY, ETHTOOL_MSG_TSLIST_GET_REPLY, ETHTOOL_MSG_TS_GET_REPLY, + ETHTOOL_MSG_TS_NTF, ETHTOOL_MSG_CABLE_TEST_NTF, ETHTOOL_MSG_CABLE_TEST_TDR_NTF, ETHTOOL_MSG_TUNNEL_INFO_GET_REPLY, diff --git a/net/core/dev.c b/net/core/dev.c index 7ce5985be84b..481f03ef2283 100644 --- a/net/core/dev.c +++ b/net/core/dev.c @@ -1612,7 +1612,7 @@ const char *netdev_cmd_to_name(enum netdev_cmd cmd) N(SVLAN_FILTER_PUSH_INFO) N(SVLAN_FILTER_DROP_INFO) N(PRE_CHANGEADDR) N(OFFLOAD_XSTATS_ENABLE) N(OFFLOAD_XSTATS_DISABLE) N(OFFLOAD_XSTATS_REPORT_USED) N(OFFLOAD_XSTATS_REPORT_DELTA) - N(XDP_FEAT_CHANGE) N(PRE_CHANGE_HWTSTAMP) + N(XDP_FEAT_CHANGE) N(PRE_CHANGE_HWTSTAMP) N(CHANGE_HWTSTAMP) } #undef N return "UNKNOWN_NETDEV_EVENT"; diff --git a/net/core/dev_ioctl.c b/net/core/dev_ioctl.c index 6d772837eb3f..210ff1fd0574 100644 --- a/net/core/dev_ioctl.c +++ b/net/core/dev_ioctl.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -252,9 +253,60 @@ static int dev_eth_ioctl(struct net_device *dev, return ops->ndo_eth_ioctl(dev, ifr, cmd); } +static int __dev_eth_ioctl(struct net_device *dev, + struct ifreq *ifr, unsigned int cmd) +{ + struct netdev_notifier_hwtstamp_layer hwtstamp_layer = { + .info.dev = dev, + .ts_layer = dev->selected_timestamping_layer, + }; + const struct net_device_ops *ops = dev->netdev_ops; + struct netlink_ext_ack extack = {}; + int err; + + if (!netif_device_present(dev)) + return -ENODEV; + + switch (dev->selected_timestamping_layer) { + case SOF_MAC_TIMESTAMPING: + if (ops->ndo_do_ioctl == phy_do_ioctl) { + /* Some drivers set .ndo_do_ioctl to phy_do_ioctl. */ + return -EOPNOTSUPP; + } else { + err = dev_eth_ioctl(dev, ifr, cmd); + if (err) + goto out; + } + break; + + case SOF_PHY_TIMESTAMPING: + if (phy_has_hwtstamp(dev->phydev)) { + err = phy_mii_ioctl(dev->phydev, ifr, cmd); + if (err) + goto out; + } else { + return -ENODEV; + } + break; + } + + hwtstamp_layer.info.extack = &extack; + + err = call_netdevice_notifiers_info(NETDEV_CHANGE_HWTSTAMP, + &hwtstamp_layer.info); + err = notifier_to_errno(err); + if (err) { + if (extack._msg) + netdev_err(dev, "%s\n", extack._msg); + } + +out: + return err; +} + static int dev_get_hwtstamp(struct net_device *dev, struct ifreq *ifr) { - return dev_eth_ioctl(dev, ifr, SIOCGHWTSTAMP); + return __dev_eth_ioctl(dev, ifr, SIOCGHWTSTAMP); } static int dev_set_hwtstamp(struct net_device *dev, struct ifreq *ifr) @@ -288,7 +340,7 @@ static int dev_set_hwtstamp(struct net_device *dev, struct ifreq *ifr) return err; } - return dev_eth_ioctl(dev, ifr, SIOCSHWTSTAMP); + return __dev_eth_ioctl(dev, ifr, SIOCSHWTSTAMP); } static int dev_siocbond(struct net_device *dev, diff --git a/net/core/timestamping.c b/net/core/timestamping.c index 04840697fe79..8bd7b2c21ab6 100644 --- a/net/core/timestamping.c +++ b/net/core/timestamping.c @@ -28,6 +28,9 @@ void skb_clone_tx_timestamp(struct sk_buff *skb) if (!skb->sk) return; + if (skb->dev->selected_timestamping_layer != SOF_PHY_TIMESTAMPING) + return; + type = classify(skb); if (type == PTP_CLASS_NONE) return; @@ -50,6 +53,9 @@ bool skb_defer_rx_timestamp(struct sk_buff *skb) if (!skb->dev || !skb->dev->phydev || !skb->dev->phydev->mii_ts) return false; + if (skb->dev->selected_timestamping_layer != SOF_PHY_TIMESTAMPING) + return false; + if (skb_headroom(skb) < ETH_HLEN) return false; diff --git a/net/ethtool/common.c b/net/ethtool/common.c index 695c7c4a816b..daea7221dd25 100644 --- a/net/ethtool/common.c +++ b/net/ethtool/common.c @@ -637,10 +637,17 @@ int __ethtool_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info) memset(info, 0, sizeof(*info)); info->cmd = ETHTOOL_GET_TS_INFO; - if (phy_has_tsinfo(phydev)) - return phy_ts_info(phydev, info); - if (ops->get_ts_info) - return ops->get_ts_info(dev, info); + switch (dev->selected_timestamping_layer) { + case SOF_MAC_TIMESTAMPING: + if (ops->get_ts_info) + return ops->get_ts_info(dev, info); + break; + + case SOF_PHY_TIMESTAMPING: + if (phy_has_tsinfo(phydev)) + return phy_ts_info(phydev, info); + return -ENODEV; + } info->so_timestamping = SOF_TIMESTAMPING_RX_SOFTWARE | SOF_TIMESTAMPING_SOFTWARE; diff --git a/net/ethtool/netlink.c b/net/ethtool/netlink.c index 8d9e27b13e28..b753b7da51f1 100644 --- a/net/ethtool/netlink.c +++ b/net/ethtool/netlink.c @@ -294,6 +294,7 @@ ethnl_default_requests[__ETHTOOL_MSG_USER_CNT] = { [ETHTOOL_MSG_FEC_SET] = ðnl_fec_request_ops, [ETHTOOL_MSG_TSINFO_GET] = ðnl_tsinfo_request_ops, [ETHTOOL_MSG_TS_GET] = ðnl_ts_request_ops, + [ETHTOOL_MSG_TS_SET] = ðnl_ts_request_ops, [ETHTOOL_MSG_TSLIST_GET] = ðnl_tslist_request_ops, [ETHTOOL_MSG_MODULE_EEPROM_GET] = ðnl_module_eeprom_request_ops, [ETHTOOL_MSG_STATS_GET] = ðnl_stats_request_ops, @@ -671,6 +672,7 @@ ethnl_default_notify_ops[ETHTOOL_MSG_KERNEL_MAX + 1] = { [ETHTOOL_MSG_MODULE_NTF] = ðnl_module_request_ops, [ETHTOOL_MSG_PLCA_NTF] = ðnl_plca_cfg_request_ops, [ETHTOOL_MSG_MM_NTF] = ðnl_mm_request_ops, + [ETHTOOL_MSG_TS_NTF] = ðnl_ts_request_ops, }; /* default notification handler */ @@ -766,6 +768,7 @@ static const ethnl_notify_handler_t ethnl_notify_handlers[] = { [ETHTOOL_MSG_MODULE_NTF] = ethnl_default_notify, [ETHTOOL_MSG_PLCA_NTF] = ethnl_default_notify, [ETHTOOL_MSG_MM_NTF] = ethnl_default_notify, + [ETHTOOL_MSG_TS_NTF] = ethnl_default_notify, }; void ethtool_notify(struct net_device *dev, unsigned int cmd, const void *data) @@ -1031,6 +1034,13 @@ static const struct genl_ops ethtool_genl_ops[] = { .policy = ethnl_ts_get_policy, .maxattr = ARRAY_SIZE(ethnl_ts_get_policy) - 1, }, + { + .cmd = ETHTOOL_MSG_TS_SET, + .flags = GENL_UNS_ADMIN_PERM, + .doit = ethnl_default_set_doit, + .policy = ethnl_ts_set_policy, + .maxattr = ARRAY_SIZE(ethnl_ts_set_policy) - 1, + }, { .cmd = ETHTOOL_MSG_CABLE_TEST_ACT, .flags = GENL_UNS_ADMIN_PERM, diff --git a/net/ethtool/netlink.h b/net/ethtool/netlink.h index 49c700777a32..ba38aa0b5506 100644 --- a/net/ethtool/netlink.h +++ b/net/ethtool/netlink.h @@ -426,6 +426,7 @@ extern const struct nla_policy ethnl_eee_get_policy[ETHTOOL_A_EEE_HEADER + 1]; extern const struct nla_policy ethnl_eee_set_policy[ETHTOOL_A_EEE_TX_LPI_TIMER + 1]; extern const struct nla_policy ethnl_tsinfo_get_policy[ETHTOOL_A_TSINFO_HEADER + 1]; extern const struct nla_policy ethnl_ts_get_policy[ETHTOOL_A_TS_HEADER + 1]; +extern const struct nla_policy ethnl_ts_set_policy[ETHTOOL_A_TS_LAYER + 1]; extern const struct nla_policy ethnl_cable_test_act_policy[ETHTOOL_A_CABLE_TEST_HEADER + 1]; extern const struct nla_policy ethnl_cable_test_tdr_act_policy[ETHTOOL_A_CABLE_TEST_TDR_CFG + 1]; extern const struct nla_policy ethnl_tunnel_info_get_policy[ETHTOOL_A_TUNNEL_INFO_HEADER + 1]; diff --git a/net/ethtool/ts.c b/net/ethtool/ts.c index a71c47ff0c6b..7a1e27add492 100644 --- a/net/ethtool/ts.c +++ b/net/ethtool/ts.c @@ -31,19 +31,13 @@ static int ts_prepare_data(const struct ethnl_req_info *req_base, { struct ts_reply_data *data = TS_REPDATA(reply_base); struct net_device *dev = reply_base->dev; - const struct ethtool_ops *ops = dev->ethtool_ops; int ret; ret = ethnl_ops_begin(dev); if (ret < 0) return ret; - if (phy_has_tsinfo(dev->phydev)) - data->ts = SOF_PHY_TIMESTAMPING; - else if (ops->get_ts_info) - data->ts = SOF_MAC_TIMESTAMPING; - else - return -EOPNOTSUPP; + data->ts = dev->selected_timestamping_layer; ethnl_ops_complete(dev); @@ -61,9 +55,65 @@ static int ts_fill_reply(struct sk_buff *skb, const struct ethnl_reply_data *reply_base) { struct ts_reply_data *data = TS_REPDATA(reply_base); + return nla_put_u32(skb, ETHTOOL_A_TS_LAYER, data->ts); } +/* TS_SET */ +const struct nla_policy ethnl_ts_set_policy[] = { + [ETHTOOL_A_TS_HEADER] = NLA_POLICY_NESTED(ethnl_header_policy), + [ETHTOOL_A_TS_LAYER] = { .type = NLA_U32 }, +}; + +static int ethnl_set_ts_validate(struct ethnl_req_info *req_info, + struct genl_info *info) +{ + const struct ethtool_ops *ops = req_info->dev->ethtool_ops; + struct net_device *dev = req_info->dev; + struct nlattr **tb = info->attrs; + + if (!tb[ETHTOOL_A_TS_LAYER]) + return 0; + + if (!phy_has_tsinfo(dev->phydev) && !ops->get_ts_info) { + NL_SET_ERR_MSG_ATTR(info->extack, + tb[ETHTOOL_A_TS_LAYER], + "Hardware time stamping is not supported by this device"); + return -EOPNOTSUPP; + } + + return 1; +} + +static int ethnl_set_ts(struct ethnl_req_info *req_info, struct genl_info *info) +{ + struct net_device *dev = req_info->dev; + struct nlattr **tb = info->attrs; + enum timestamping_layer flavor; + bool mod = false; + + if (!dev->phydev) { + return -EOPNOTSUPP; + } + + flavor = dev->selected_timestamping_layer; + ethnl_update_u32(&flavor, tb[ETHTOOL_A_TS_LAYER], &mod); + + if (mod) { + const struct net_device_ops *ops = dev->netdev_ops; + struct ifreq ifr = {0}; + + /* Disable time stamping in the current layer. */ + if (netif_device_present(dev) && ops->ndo_eth_ioctl) + ops->ndo_eth_ioctl(dev, &ifr, SIOCSHWTSTAMP); + + dev->selected_timestamping_layer = flavor; + return 1; + }; + + return 0; +} + const struct ethnl_request_ops ethnl_ts_request_ops = { .request_cmd = ETHTOOL_MSG_TS_GET, .reply_cmd = ETHTOOL_MSG_TS_GET_REPLY, @@ -74,6 +124,10 @@ const struct ethnl_request_ops ethnl_ts_request_ops = { .prepare_data = ts_prepare_data, .reply_size = ts_reply_size, .fill_reply = ts_fill_reply, + + .set_validate = ethnl_set_ts_validate, + .set = ethnl_set_ts, + .set_ntf_cmd = ETHTOOL_MSG_TS_NTF, }; /* TSLIST_GET */ From patchwork Thu Apr 6 17:33:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kory Maincent X-Patchwork-Id: 13203778 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 41312C77B75 for ; Thu, 6 Apr 2023 17:33:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238947AbjDFRdq (ORCPT ); Thu, 6 Apr 2023 13:33:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51744 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240049AbjDFRdl (ORCPT ); Thu, 6 Apr 2023 13:33:41 -0400 Received: from relay2-d.mail.gandi.net (relay2-d.mail.gandi.net [IPv6:2001:4b98:dc4:8::222]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B6A8D8A7D for ; Thu, 6 Apr 2023 10:33:23 -0700 (PDT) Received: (Authenticated sender: kory.maincent@bootlin.com) by mail.gandi.net (Postfix) with ESMTPSA id 633D640008; Thu, 6 Apr 2023 17:33:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1680802402; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Vhy97VSl2syCSI5pjgEciGFNAcpGRH/LFxJKqt4JIbw=; b=SjBJs0SkVlrJ5wVjrAqUebudjnCj5o4sFW0/lW+dC62qXo+ADj6rtGZoep+nS0i/4FHEOV qNSoNVaufo4YNRKjc7wF7xtdXykowUx3a+V0f/mjUGkmoO5nfkuDr1L+qK6ime/OdIWtYv hEh4xA94jvtug5kAlEdYGdrNYYEcw3deHzbvZZVubok1a3nwzN5ML11EMWGVwc7lUP+C/n 6req04AKR2JU2ZzwwnK3R8hlOLzHBKhXTFebkeiJh0e+C7JRUDSgHmM5ZUxNwaUzynz574 JCYLtAA4LPmL11eERZl8e7pxXb6UTN0UbIrCGeAUHeJbO9wTDluQJX4QPzsfgg== From: =?utf-8?q?K=C3=B6ry_Maincent?= To: netdev@vger.kernel.org Cc: kuba@kernel.org, glipus@gmail.com, maxime.chevallier@bootlin.com, vladimir.oltean@nxp.com, vadim.fedorenko@linux.dev, richardcochran@gmail.com, gerhard@engleder-embedded.com, thomas.petazzoni@bootlin.com, krzysztof.kozlowski+dt@linaro.org, robh+dt@kernel.org, linux@armlinux.org.uk, Kory Maincent Subject: [PATCH net-next RFC v4 5/5] net: fix up drivers WRT phy time stamping Date: Thu, 6 Apr 2023 19:33:08 +0200 Message-Id: <20230406173308.401924-6-kory.maincent@bootlin.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230406173308.401924-1-kory.maincent@bootlin.com> References: <20230406173308.401924-1-kory.maincent@bootlin.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org X-Patchwork-State: RFC From: Richard Cochran For "git bisect" correctness, this patch should be squashed in to the previous one, but it is broken out here for the purpose of review. I will also add the fix up of lan966x driver to validate the netdev notifier when the hwtstamp action will be converted to NDO. Signed-off-by: Richard Cochran Signed-off-by: Kory Maincent --- Notes: I will add the lan966x driver update to verify the netdev notifer once the replacement to NDO by Max will be done. Also I don't have this hardware to test it, it will be useful if someone will be willing to test for me. drivers/net/ethernet/freescale/fec_main.c | 23 +++++++++----------- drivers/net/ethernet/mscc/ocelot_net.c | 21 +++++++++--------- drivers/net/ethernet/ti/cpsw_priv.c | 12 +++++------ drivers/net/ethernet/ti/netcp_ethss.c | 26 +++++------------------ 4 files changed, 31 insertions(+), 51 deletions(-) diff --git a/drivers/net/ethernet/freescale/fec_main.c b/drivers/net/ethernet/freescale/fec_main.c index f3b16a6673e2..bc4bfdad83ca 100644 --- a/drivers/net/ethernet/freescale/fec_main.c +++ b/drivers/net/ethernet/freescale/fec_main.c @@ -3213,22 +3213,19 @@ static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd) if (!netif_running(ndev)) return -EINVAL; - if (!phydev) - return -ENODEV; + switch (cmd) { + case SIOCSHWTSTAMP: + return fep->bufdesc_ex ? fec_ptp_set(ndev, rq) : + -EOPNOTSUPP; - if (fep->bufdesc_ex) { - bool use_fec_hwts = !phy_has_hwtstamp(phydev); - - if (cmd == SIOCSHWTSTAMP) { - if (use_fec_hwts) - return fec_ptp_set(ndev, rq); - fec_ptp_disable_hwts(ndev); - } else if (cmd == SIOCGHWTSTAMP) { - if (use_fec_hwts) - return fec_ptp_get(ndev, rq); - } + case SIOCGHWTSTAMP: + return fep->bufdesc_ex ? fec_ptp_get(ndev, rq) : + -EOPNOTSUPP; } + if (!phydev) + return -ENODEV; + return phy_mii_ioctl(phydev, rq, cmd); } diff --git a/drivers/net/ethernet/mscc/ocelot_net.c b/drivers/net/ethernet/mscc/ocelot_net.c index 21a87a3fc556..29f7a8a3a4d9 100644 --- a/drivers/net/ethernet/mscc/ocelot_net.c +++ b/drivers/net/ethernet/mscc/ocelot_net.c @@ -873,18 +873,19 @@ static int ocelot_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) struct ocelot *ocelot = priv->port.ocelot; int port = priv->port.index; - /* If the attached PHY device isn't capable of timestamping operations, - * use our own (when possible). - */ - if (!phy_has_hwtstamp(dev->phydev) && ocelot->ptp) { - switch (cmd) { - case SIOCSHWTSTAMP: - return ocelot_hwstamp_set(ocelot, port, ifr); - case SIOCGHWTSTAMP: - return ocelot_hwstamp_get(ocelot, port, ifr); - } + switch (cmd) { + case SIOCSHWTSTAMP: + return ocelot->ptp ? ocelot_hwstamp_set(ocelot, port, ifr) : + -EOPNOTSUPP; + + case SIOCGHWTSTAMP: + return ocelot->ptp ? ocelot_hwstamp_get(ocelot, port, ifr) : + -EOPNOTSUPP; } + if (!dev->phydev) + return -ENODEV; + return phy_mii_ioctl(dev->phydev, ifr, cmd); } diff --git a/drivers/net/ethernet/ti/cpsw_priv.c b/drivers/net/ethernet/ti/cpsw_priv.c index e966dd47e2db..cec4c65532c4 100644 --- a/drivers/net/ethernet/ti/cpsw_priv.c +++ b/drivers/net/ethernet/ti/cpsw_priv.c @@ -715,13 +715,11 @@ int cpsw_ndo_ioctl(struct net_device *dev, struct ifreq *req, int cmd) phy = cpsw->slaves[slave_no].phy; - if (!phy_has_hwtstamp(phy)) { - switch (cmd) { - case SIOCSHWTSTAMP: - return cpsw_hwtstamp_set(dev, req); - case SIOCGHWTSTAMP: - return cpsw_hwtstamp_get(dev, req); - } + switch (cmd) { + case SIOCSHWTSTAMP: + return cpsw_hwtstamp_set(dev, req); + case SIOCGHWTSTAMP: + return cpsw_hwtstamp_get(dev, req); } if (phy) diff --git a/drivers/net/ethernet/ti/netcp_ethss.c b/drivers/net/ethernet/ti/netcp_ethss.c index 2adf82a32bf6..6074ce13e130 100644 --- a/drivers/net/ethernet/ti/netcp_ethss.c +++ b/drivers/net/ethernet/ti/netcp_ethss.c @@ -2557,15 +2557,6 @@ static int gbe_txtstamp_mark_pkt(struct gbe_intf *gbe_intf, !gbe_dev->tx_ts_enabled) return 0; - /* If phy has the txtstamp api, assume it will do it. - * We mark it here because skb_tx_timestamp() is called - * after all the txhooks are called. - */ - if (phy_has_txtstamp(phydev)) { - skb_shinfo(p_info->skb)->tx_flags |= SKBTX_IN_PROGRESS; - return 0; - } - if (gbe_need_txtstamp(gbe_intf, p_info)) { p_info->txtstamp = gbe_txtstamp; p_info->ts_context = (void *)gbe_intf; @@ -2583,11 +2574,6 @@ static int gbe_rxtstamp(struct gbe_intf *gbe_intf, struct netcp_packet *p_info) if (p_info->rxtstamp_complete) return 0; - if (phy_has_rxtstamp(phydev)) { - p_info->rxtstamp_complete = true; - return 0; - } - if (gbe_dev->rx_ts_enabled) cpts_rx_timestamp(gbe_dev->cpts, p_info->skb); @@ -2821,13 +2807,11 @@ static int gbe_ioctl(void *intf_priv, struct ifreq *req, int cmd) struct gbe_intf *gbe_intf = intf_priv; struct phy_device *phy = gbe_intf->slave->phy; - if (!phy_has_hwtstamp(phy)) { - switch (cmd) { - case SIOCGHWTSTAMP: - return gbe_hwtstamp_get(gbe_intf, req); - case SIOCSHWTSTAMP: - return gbe_hwtstamp_set(gbe_intf, req); - } + switch (cmd) { + case SIOCGHWTSTAMP: + return gbe_hwtstamp_get(gbe_intf, req); + case SIOCSHWTSTAMP: + return gbe_hwtstamp_set(gbe_intf, req); } if (phy)