Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

net: phy: provide phylib stubs for hardware timestamping operations

net/core/dev_ioctl.c (built-in code) will want to call phy_mii_ioctl()
for hardware timestamping purposes. This is not directly possible,
because phy_mii_ioctl() is a symbol provided under CONFIG_PHYLIB.

Do something similar to what was done in DSA in commit 5a17818682cf
("net: dsa: replace NETDEV_PRE_CHANGE_HWTSTAMP notifier with a stub"),
and arrange some indirect calls to phy_mii_ioctl() through a stub
structure containing function pointers, that's provided by phylib as
built-in even when CONFIG_PHYLIB=m, and which phy_init() populates at
runtime (module insertion).

Note: maybe the ownership of the ethtool_phy_ops singleton is backwards,
and the methods exposed by that should be later merged into phylib_stubs.

Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
Link: https://lore.kernel.org/r/20230801142824.1772134-12-vladimir.oltean@nxp.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

authored by

Vladimir Oltean and committed by
Jakub Kicinski
60495b66 70ef7d87

+141
+1
MAINTAINERS
··· 7752 7752 F: include/linux/of_net.h 7753 7753 F: include/linux/phy.h 7754 7754 F: include/linux/phy_fixed.h 7755 + F: include/linux/phylib_stubs.h 7755 7756 F: include/linux/platform_data/mdio-bcm-unimac.h 7756 7757 F: include/linux/platform_data/mdio-gpio.h 7757 7758 F: include/trace/events/mdio.h
+2
drivers/net/phy/Makefile
··· 14 14 # dedicated loadable module, so we bundle them all together into libphy.ko 15 15 ifdef CONFIG_PHYLIB 16 16 libphy-y += $(mdio-bus-y) 17 + # the stubs are built-in whenever PHYLIB is built-in or module 18 + obj-y += stubs.o 17 19 else 18 20 obj-$(CONFIG_MDIO_DEVICE) += mdio-bus.o 19 21 endif
+34
drivers/net/phy/phy.c
··· 456 456 EXPORT_SYMBOL(phy_do_ioctl_running); 457 457 458 458 /** 459 + * __phy_hwtstamp_get - Get hardware timestamping configuration from PHY 460 + * 461 + * @phydev: the PHY device structure 462 + * @config: structure holding the timestamping configuration 463 + * 464 + * Query the PHY device for its current hardware timestamping configuration. 465 + */ 466 + int __phy_hwtstamp_get(struct phy_device *phydev, 467 + struct kernel_hwtstamp_config *config) 468 + { 469 + if (!phydev) 470 + return -ENODEV; 471 + 472 + return phy_mii_ioctl(phydev, config->ifr, SIOCGHWTSTAMP); 473 + } 474 + 475 + /** 476 + * __phy_hwtstamp_set - Modify PHY hardware timestamping configuration 477 + * 478 + * @phydev: the PHY device structure 479 + * @config: structure holding the timestamping configuration 480 + * @extack: netlink extended ack structure, for error reporting 481 + */ 482 + int __phy_hwtstamp_set(struct phy_device *phydev, 483 + struct kernel_hwtstamp_config *config, 484 + struct netlink_ext_ack *extack) 485 + { 486 + if (!phydev) 487 + return -ENODEV; 488 + 489 + return phy_mii_ioctl(phydev, config->ifr, SIOCSHWTSTAMP); 490 + } 491 + 492 + /** 459 493 * phy_queue_state_machine - Trigger the state machine to run soon 460 494 * 461 495 * @phydev: the phy_device struct
+19
drivers/net/phy/phy_device.c
··· 27 27 #include <linux/of.h> 28 28 #include <linux/netdevice.h> 29 29 #include <linux/phy.h> 30 + #include <linux/phylib_stubs.h> 30 31 #include <linux/phy_led_triggers.h> 31 32 #include <linux/pse-pd/pse.h> 32 33 #include <linux/property.h> ··· 3449 3448 .start_cable_test_tdr = phy_start_cable_test_tdr, 3450 3449 }; 3451 3450 3451 + static const struct phylib_stubs __phylib_stubs = { 3452 + .hwtstamp_get = __phy_hwtstamp_get, 3453 + .hwtstamp_set = __phy_hwtstamp_set, 3454 + }; 3455 + 3456 + static void phylib_register_stubs(void) 3457 + { 3458 + phylib_stubs = &__phylib_stubs; 3459 + } 3460 + 3461 + static void phylib_unregister_stubs(void) 3462 + { 3463 + phylib_stubs = NULL; 3464 + } 3465 + 3452 3466 static int __init phy_init(void) 3453 3467 { 3454 3468 int rc; 3455 3469 3456 3470 rtnl_lock(); 3457 3471 ethtool_set_ethtool_phy_ops(&phy_ethtool_phy_ops); 3472 + phylib_register_stubs(); 3458 3473 rtnl_unlock(); 3459 3474 3460 3475 rc = mdio_bus_init(); ··· 3495 3478 mdio_bus_exit(); 3496 3479 err_ethtool_phy_ops: 3497 3480 rtnl_lock(); 3481 + phylib_unregister_stubs(); 3498 3482 ethtool_set_ethtool_phy_ops(NULL); 3499 3483 rtnl_unlock(); 3500 3484 ··· 3508 3490 phy_driver_unregister(&genphy_driver); 3509 3491 mdio_bus_exit(); 3510 3492 rtnl_lock(); 3493 + phylib_unregister_stubs(); 3511 3494 ethtool_set_ethtool_phy_ops(NULL); 3512 3495 rtnl_unlock(); 3513 3496 }
+10
drivers/net/phy/stubs.c
··· 1 + // SPDX-License-Identifier: GPL-2.0+ 2 + /* 3 + * Stubs for PHY library functionality called by the core network stack. 4 + * These are necessary because CONFIG_PHYLIB can be a module, and built-in 5 + * code cannot directly call symbols exported by modules. 6 + */ 7 + #include <linux/phylib_stubs.h> 8 + 9 + const struct phylib_stubs *phylib_stubs; 10 + EXPORT_SYMBOL_GPL(phylib_stubs);
+7
include/linux/phy.h
··· 298 298 #define MII_BUS_ID_SIZE 61 299 299 300 300 struct device; 301 + struct kernel_hwtstamp_config; 301 302 struct phylink; 302 303 struct sfp_bus; 303 304 struct sfp_upstream_ops; ··· 1955 1954 struct netlink_ext_ack *extack); 1956 1955 int phy_ethtool_get_plca_status(struct phy_device *phydev, 1957 1956 struct phy_plca_status *plca_st); 1957 + 1958 + int __phy_hwtstamp_get(struct phy_device *phydev, 1959 + struct kernel_hwtstamp_config *config); 1960 + int __phy_hwtstamp_set(struct phy_device *phydev, 1961 + struct kernel_hwtstamp_config *config, 1962 + struct netlink_ext_ack *extack); 1958 1963 1959 1964 static inline int phy_package_read(struct phy_device *phydev, u32 regnum) 1960 1965 {
+68
include/linux/phylib_stubs.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 + /* 3 + * Stubs for the Network PHY library 4 + */ 5 + 6 + #include <linux/rtnetlink.h> 7 + 8 + struct kernel_hwtstamp_config; 9 + struct netlink_ext_ack; 10 + struct phy_device; 11 + 12 + #if IS_ENABLED(CONFIG_PHYLIB) 13 + 14 + extern const struct phylib_stubs *phylib_stubs; 15 + 16 + struct phylib_stubs { 17 + int (*hwtstamp_get)(struct phy_device *phydev, 18 + struct kernel_hwtstamp_config *config); 19 + int (*hwtstamp_set)(struct phy_device *phydev, 20 + struct kernel_hwtstamp_config *config, 21 + struct netlink_ext_ack *extack); 22 + }; 23 + 24 + static inline int phy_hwtstamp_get(struct phy_device *phydev, 25 + struct kernel_hwtstamp_config *config) 26 + { 27 + /* phylib_register_stubs() and phylib_unregister_stubs() 28 + * also run under rtnl_lock(). 29 + */ 30 + ASSERT_RTNL(); 31 + 32 + if (!phylib_stubs) 33 + return -EOPNOTSUPP; 34 + 35 + return phylib_stubs->hwtstamp_get(phydev, config); 36 + } 37 + 38 + static inline int phy_hwtstamp_set(struct phy_device *phydev, 39 + struct kernel_hwtstamp_config *config, 40 + struct netlink_ext_ack *extack) 41 + { 42 + /* phylib_register_stubs() and phylib_unregister_stubs() 43 + * also run under rtnl_lock(). 44 + */ 45 + ASSERT_RTNL(); 46 + 47 + if (!phylib_stubs) 48 + return -EOPNOTSUPP; 49 + 50 + return phylib_stubs->hwtstamp_set(phydev, config, extack); 51 + } 52 + 53 + #else 54 + 55 + static inline int phy_hwtstamp_get(struct phy_device *phydev, 56 + struct kernel_hwtstamp_config *config) 57 + { 58 + return -EOPNOTSUPP; 59 + } 60 + 61 + static inline int phy_hwtstamp_set(struct phy_device *phydev, 62 + struct kernel_hwtstamp_config *config, 63 + struct netlink_ext_ack *extack) 64 + { 65 + return -EOPNOTSUPP; 66 + } 67 + 68 + #endif