From patchwork Thu Jul 13 12:18:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vladimir Oltean X-Patchwork-Id: 13311953 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id BA54CEB64DD for ; Thu, 13 Jul 2023 12:20:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=mgNEgmeiMac9VjrURBIbdWeuX9Rnm090bWRSPtPqKjU=; b=ojgjos+KkDWb9w n3pnxJfEmRC8H3YIrZamunO6foDc/mVKSLw7xTfALVN0BoNME6yGizgAE9Q4sdX0bLtSqnCdh+Mi4 otfjeG0chDMgTsOYcDYfe57lZL1n7AB6Lv7V2uf6A7VXuabQoXJZPpbqPhPdytuwRypUTTJrUuUzr RJevZ51DFyzAPFXVpeRMlxhWDZTzzy/5Uq6jHJnc/FDJeiuvbXlJeNiBCvPBKRrjvVOLYHZRAmxaO Vzh9Gf87JUgbjpkhINsgXuqp7nFZbeExSj6urHFSe9dQkV8hUKH1Npx7cV5lMPqhcoTvkwl1nCW0W ehcyhhML22ZUaWd+OkxA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qJvIO-003DNa-2N; Thu, 13 Jul 2023 12:19:44 +0000 Received: from mail-dbaeur03on20600.outbound.protection.outlook.com ([2a01:111:f400:fe1a::600] helo=EUR03-DBA-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qJvII-003DL0-2T for linux-arm-kernel@lists.infradead.org; Thu, 13 Jul 2023 12:19:40 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=n9Lj1FSmn46iQca72EXRctKcbd80onpCMKFbTCuESXLeeDqTN1oT1I4RRoQKmc/gECuPkadHRYBzjcDcj5gGHo5g4XGZMh9JWJJq/qXBjMfPY9gvAPyY5pGUHgEGNP8v8hLKttbmXtzp4QquftueUJZ3fDx7cdEd5eaofzF+/YBxbSbOR/ICfWySDlXotf4BwZuKCU8WYpZAuzzDCW5NqIeeQLP2aAufucWlG/YDKPd0oAtmi5gZXgQjTB+5xDnL8oGgVwPxoYbvqbainFu1zVnzUzrcPoh5MwbmjrGLZmrQg81mng1hsc2zDf5e8+vH/pLzPiTVNCs3hlvWObmlVA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=5CiXt8/qYpORRjk3gY2/uz8cHJvXxKiQHD9i9t0WgKM=; b=OdYWpyBdr/10gihxfZe3r24+pXy5qygWeTcDv6r8EhfEpMFsaM0cHnzkE6cZFeg67hQZXN1t21Y4KewweqCMufMqX0nCSBEhTZTLbDfl1HMzGULwX0fuH/9K4K1yebUaG17VAbS5IQFb6FFqlTmFdXsOOAZG+8VMFRFoZM4sGjS39lq+g1R3upRh3r3yBDCNEqldh/Vkc0rQiwNJOnbg8A7VLILehVsv6z0wTmyTGU4SPq0RZsfPuh3ByvnT6GWkMsd8nJuOjqNpX5NFGSFMkIGPPXE8YwDrpv0or/8bSOZBKdxxRKZngA4xbS8sY0ZnJG5Tt+O595vszMaov6hlwg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nxp.com; dmarc=pass action=none header.from=nxp.com; dkim=pass header.d=nxp.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=5CiXt8/qYpORRjk3gY2/uz8cHJvXxKiQHD9i9t0WgKM=; b=GAlGJ9eHGiFhn90f38OmZBQ8WJewLlia8RP/wXYGm9cs1QaFTcV4o/eCgHEnCKRXBcPizf7bwcGpSvxvcIk4P8LWeta6ctTYTCAlMYajqtn5ZCCAD/ifrMf3Pdt2CaHypSdL+NdhimVCnh8tVLXqfamY6E4H1VaAr6BRUGX0vjQ= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nxp.com; Received: from AM0PR04MB6452.eurprd04.prod.outlook.com (2603:10a6:208:16d::21) by DBBPR04MB8010.eurprd04.prod.outlook.com (2603:10a6:10:1f1::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6588.20; Thu, 13 Jul 2023 12:19:32 +0000 Received: from AM0PR04MB6452.eurprd04.prod.outlook.com ([fe80::cc2a:5d80:9dbd:d850]) by AM0PR04MB6452.eurprd04.prod.outlook.com ([fe80::cc2a:5d80:9dbd:d850%7]) with mapi id 15.20.6588.017; Thu, 13 Jul 2023 12:19:31 +0000 From: Vladimir Oltean To: netdev@vger.kernel.org Cc: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Andrew Lunn , Florian Fainelli , Maxim Georgiev , Horatiu Vultur , =?utf-8?q?K=C3=B6ry_Maincent?= , Maxime Chevallier , Richard Cochran , Vadim Fedorenko , Gerhard Engleder , Hangbin Liu , Russell King , Heiner Kallweit , Jacob Keller , Jay Vosburgh , Andy Gospodarek , Wei Fang , Shenwei Wang , Clark Wang , NXP Linux Team , UNGLinuxDriver@microchip.com, Lars Povlsen , Steen Hegelund , Daniel Machon , Simon Horman , Casper Andersson , Sergey Organov , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH v7 net-next 01/10] net: add NDOs for configuring hardware timestamping Date: Thu, 13 Jul 2023 15:18:58 +0300 Message-Id: <20230713121907.3249291-2-vladimir.oltean@nxp.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230713121907.3249291-1-vladimir.oltean@nxp.com> References: <20230713121907.3249291-1-vladimir.oltean@nxp.com> X-ClientProxiedBy: FR2P281CA0157.DEUP281.PROD.OUTLOOK.COM (2603:10a6:d10:99::18) To AM0PR04MB6452.eurprd04.prod.outlook.com (2603:10a6:208:16d::21) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AM0PR04MB6452:EE_|DBBPR04MB8010:EE_ X-MS-Office365-Filtering-Correlation-Id: 978580c5-e89c-4b50-70fe-08db839b696f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: kjje17W1JDfTZIDZ2rLue7Ng+qL7jg/vrRhc+aIoq+FjDtkEgrWDhsTOrdMm9ORX79FJtMEeI+PZ5IImLmUdcyFRE43a2AEgC+woRS3i2SsRr310LxOQQraqF7IwVf8QYBEXRb++1DwUJImpFh80zolDt147R1smh6wPZxzvxMtCajMMb451Wy4p+F2xCP/YfHCqW1ih+wEgymrUREQpToaqdpdaJL4V8e2oyzGqPCdqYYFPeCOGZE8ip54XRky1G+lyJGgXTOqBx7DN+OADednQD/3pyBLkhP+8uKBBUDl7D6sDUIxRc/B9wPoCsTOkkndeTLzGb+cJsFWk0Ue0aiephogboJlI8eMDcb9tFBNF83Z/EamoUHTavaGmXX9W2tC1CVyrYq2DezCZ3EmslECJQpZFheAz2Vu31mF+e6FR6GYQCyb/DyusDfNJ9kcWG/ES/Y2NHCHyxAT0pzqT8NhjzBL8Nd09AFmv3UBfIhm87g4HdR4uk5i8vvi7HBT0cviPW1rlU50FOYhV6prdwmu3KECzvblQVzzM9G1FcFV9h6OtHALztJ47JzgKvu/nwNZVH/KFiywZXj4LGSpEUUN3HPjWlvYXLH1fdNxuj09XFHuBoOboLhbW/POfdzp1 X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:AM0PR04MB6452.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230028)(4636009)(136003)(366004)(346002)(39860400002)(376002)(396003)(451199021)(6512007)(6916009)(4326008)(66556008)(66946007)(66476007)(38350700002)(38100700002)(86362001)(186003)(1076003)(26005)(6506007)(2616005)(83380400001)(52116002)(36756003)(478600001)(54906003)(41300700001)(5660300002)(8676002)(8936002)(44832011)(6486002)(6666004)(316002)(2906002)(7416002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: skrtnNHxhkaBpuOB0T5QT0nweBw+uqAXvMdUCfUmszOsABSTZ2KXzr/2t8S5I9UGB2KJbxKW2tKOQm+BVkNy3gTcsOKWp3D+q5BqSU3I3bw7QfkK7b4Iifi7VtliW4OYZHeQRzulu6FthmG/Jkgn+4ix5zq+wYm0vXY/6iMijl9+OnxoBf23o8n1bM7OGqwq9Ri3P9YMBBqS8sCuNuxUdqrzh4vBlZBThkbAWzVjFiPVWY8tv7jvxba6vRZGqdMurs23OSl1FCPduUqrODxhrVOqZSMtM536JjbCZ0B0s6+srRU3Su9ydsQfJMGDPuPJTB/AzioyJSKhePXb8MUsPXTkeKlky5yO7hEARZvpMF5GRVsZ8IN9OBmtJNFgUjoi2JoXG4UWvVv+kovCyDa10rq36R4EhUKEK4zH8bC328ly58/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8TJGgc0vcJzdDa4UpFscV2etvLwtRJCj4PNUI2TOcTh+iKtLv/j3tsaJ8HhF4fVmulXIoXSJilHu3ID0ur2rYc9KC6poThwqicT31jserdKy3Vtw== X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 978580c5-e89c-4b50-70fe-08db839b696f X-MS-Exchange-CrossTenant-AuthSource: AM0PR04MB6452.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Jul 2023 12:19:31.6794 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 7jg0thKYXNmrrjOvPCPlCo5Gn0lRCQ0wUuPWkSyOy8C1j1fFCvg+yMGHmOm7yiozSlODSr9NKPOuGMJjj0jrBw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DBBPR04MB8010 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230713_051938_827932_9E4B77DE X-CRM114-Status: GOOD ( 21.80 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Maxim Georgiev Current hardware timestamping API for NICs requires implementing .ndo_eth_ioctl() for SIOCGHWTSTAMP and SIOCSHWTSTAMP. That API has some boilerplate such as request parameter translation between user and kernel address spaces, handling possible translation failures correctly, etc. Since it is the same all across the board, it would be desirable to handle it through generic code. Here we introduce .ndo_hwtstamp_get() and .ndo_hwtstamp_set(), which implement that boilerplate and allow drivers to just act upon requests. Suggested-by: Jakub Kicinski Signed-off-by: Maxim Georgiev Signed-off-by: Vladimir Oltean --- Changes in v7: - Remove extack from ndo_hwtstamp_get() - Reword commit message Changes in v6: - The patch title was updated. No code changes. Changes in v4: - Renamed hwtstamp_kernel_to_config() function to hwtstamp_config_from_kernel(). - Added struct kernel_hwtstamp_config zero initialization in dev_get_hwtstamp() and in dev_get_hwtstamp(). Changes in v3: - Moved individual driver conversions to separate patches Changes in v2: - Introduced kernel_hwtstamp_config structure - Added netlink_ext_ack* and kernel_hwtstamp_config* as NDO hw timestamp function parameters - Reodered function variable declarations in dev_hwtstamp() - Refactored error handling logic in dev_hwtstamp() - Split dev_hwtstamp() into GET and SET versions - Changed net_hwtstamp_validate() to accept struct hwtstamp_config * as a parameter include/linux/net_tstamp.h | 8 +++++++ include/linux/netdevice.h | 16 +++++++++++++ net/core/dev_ioctl.c | 46 ++++++++++++++++++++++++++++++++++++-- 3 files changed, 68 insertions(+), 2 deletions(-) diff --git a/include/linux/net_tstamp.h b/include/linux/net_tstamp.h index fd67f3cc0c4b..7c59824f43f5 100644 --- a/include/linux/net_tstamp.h +++ b/include/linux/net_tstamp.h @@ -30,4 +30,12 @@ static inline void hwtstamp_config_to_kernel(struct kernel_hwtstamp_config *kern kernel_cfg->rx_filter = cfg->rx_filter; } +static inline void hwtstamp_config_from_kernel(struct hwtstamp_config *cfg, + const struct kernel_hwtstamp_config *kernel_cfg) +{ + cfg->flags = kernel_cfg->flags; + cfg->tx_type = kernel_cfg->tx_type; + cfg->rx_filter = kernel_cfg->rx_filter; +} + #endif /* _LINUX_NET_TIMESTAMPING_H_ */ diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h index b828c7a75be2..17a442ed683b 100644 --- a/include/linux/netdevice.h +++ b/include/linux/netdevice.h @@ -57,6 +57,7 @@ struct netpoll_info; struct device; struct ethtool_ops; +struct kernel_hwtstamp_config; struct phy_device; struct dsa_port; struct ip_tunnel_parm; @@ -1418,6 +1419,16 @@ struct netdev_net_notifier { * Get hardware timestamp based on normal/adjustable time or free running * cycle counter. This function is required if physical clock supports a * free running cycle counter. + * + * int (*ndo_hwtstamp_get)(struct net_device *dev, + * struct kernel_hwtstamp_config *kernel_config); + * Get the currently configured hardware timestamping parameters for the + * NIC device. + * + * int (*ndo_hwtstamp_set)(struct net_device *dev, + * struct kernel_hwtstamp_config *kernel_config, + * struct netlink_ext_ack *extack); + * Change the hardware timestamping parameters for NIC device. */ struct net_device_ops { int (*ndo_init)(struct net_device *dev); @@ -1652,6 +1663,11 @@ struct net_device_ops { ktime_t (*ndo_get_tstamp)(struct net_device *dev, const struct skb_shared_hwtstamps *hwtstamps, bool cycles); + int (*ndo_hwtstamp_get)(struct net_device *dev, + struct kernel_hwtstamp_config *kernel_config); + int (*ndo_hwtstamp_set)(struct net_device *dev, + struct kernel_hwtstamp_config *kernel_config, + struct netlink_ext_ack *extack); }; struct xdp_metadata_ops { diff --git a/net/core/dev_ioctl.c b/net/core/dev_ioctl.c index 3730945ee294..10c0e173b38b 100644 --- a/net/core/dev_ioctl.c +++ b/net/core/dev_ioctl.c @@ -254,11 +254,32 @@ static int dev_eth_ioctl(struct net_device *dev, static int dev_get_hwtstamp(struct net_device *dev, struct ifreq *ifr) { - return dev_eth_ioctl(dev, ifr, SIOCGHWTSTAMP); + const struct net_device_ops *ops = dev->netdev_ops; + struct kernel_hwtstamp_config kernel_cfg = {}; + struct hwtstamp_config cfg; + int err; + + if (!ops->ndo_hwtstamp_get) + return dev_eth_ioctl(dev, ifr, SIOCGHWTSTAMP); /* legacy */ + + if (!netif_device_present(dev)) + return -ENODEV; + + err = ops->ndo_hwtstamp_get(dev, &kernel_cfg); + if (err) + return err; + + hwtstamp_config_from_kernel(&cfg, &kernel_cfg); + + if (copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg))) + return -EFAULT; + + return 0; } static int dev_set_hwtstamp(struct net_device *dev, struct ifreq *ifr) { + const struct net_device_ops *ops = dev->netdev_ops; struct kernel_hwtstamp_config kernel_cfg; struct netlink_ext_ack extack = {}; struct hwtstamp_config cfg; @@ -280,7 +301,28 @@ static int dev_set_hwtstamp(struct net_device *dev, struct ifreq *ifr) return err; } - return dev_eth_ioctl(dev, ifr, SIOCSHWTSTAMP); + if (!ops->ndo_hwtstamp_set) + return dev_eth_ioctl(dev, ifr, SIOCSHWTSTAMP); /* legacy */ + + if (!netif_device_present(dev)) + return -ENODEV; + + err = ops->ndo_hwtstamp_set(dev, &kernel_cfg, &extack); + if (err) { + if (extack._msg) + netdev_err(dev, "%s\n", extack._msg); + return err; + } + + /* The driver may have modified the configuration, so copy the + * updated version of it back to user space + */ + hwtstamp_config_from_kernel(&cfg, &kernel_cfg); + + if (copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg))) + return -EFAULT; + + return 0; } static int dev_siocbond(struct net_device *dev,