3127 lines · 81.6 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0+/* Framework for finding and configuring PHYs. * Also contains generic PHY driver * * Author: Andy Fleming * * Copyright (c) 2004 Freescale Semiconductor, Inc. */ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt #include <linux/bitmap.h>#include <linux/delay.h>#include <linux/errno.h>#include <linux/etherdevice.h>#include <linux/ethtool.h>#include <linux/init.h>#include <linux/interrupt.h>#include <linux/io.h>#include <linux/kernel.h>#include <linux/mdio.h>#include <linux/mii.h>#include <linux/mm.h>#include <linux/module.h>#include <linux/netdevice.h>#include <linux/phy.h>#include <linux/phy_led_triggers.h>#include <linux/property.h>#include <linux/sfp.h>#include <linux/skbuff.h>#include <linux/slab.h>#include <linux/string.h>#include <linux/uaccess.h>#include <linux/unistd.h> MODULE_DESCRIPTION("PHY library");MODULE_AUTHOR("Andy Fleming");MODULE_LICENSE("GPL"); __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_features) __ro_after_init;EXPORT_SYMBOL_GPL(phy_basic_features); __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_t1_features) __ro_after_init;EXPORT_SYMBOL_GPL(phy_basic_t1_features); __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_features) __ro_after_init;EXPORT_SYMBOL_GPL(phy_gbit_features); __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_fibre_features) __ro_after_init;EXPORT_SYMBOL_GPL(phy_gbit_fibre_features); __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_all_ports_features) __ro_after_init;EXPORT_SYMBOL_GPL(phy_gbit_all_ports_features); __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_features) __ro_after_init;EXPORT_SYMBOL_GPL(phy_10gbit_features); __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_fec_features) __ro_after_init;EXPORT_SYMBOL_GPL(phy_10gbit_fec_features); const int phy_basic_ports_array[3] = {	ETHTOOL_LINK_MODE_Autoneg_BIT,	ETHTOOL_LINK_MODE_TP_BIT,	ETHTOOL_LINK_MODE_MII_BIT,};EXPORT_SYMBOL_GPL(phy_basic_ports_array); const int phy_fibre_port_array[1] = {	ETHTOOL_LINK_MODE_FIBRE_BIT,};EXPORT_SYMBOL_GPL(phy_fibre_port_array); const int phy_all_ports_features_array[7] = {	ETHTOOL_LINK_MODE_Autoneg_BIT,	ETHTOOL_LINK_MODE_TP_BIT,	ETHTOOL_LINK_MODE_MII_BIT,	ETHTOOL_LINK_MODE_FIBRE_BIT,	ETHTOOL_LINK_MODE_AUI_BIT,	ETHTOOL_LINK_MODE_BNC_BIT,	ETHTOOL_LINK_MODE_Backplane_BIT,};EXPORT_SYMBOL_GPL(phy_all_ports_features_array); const int phy_10_100_features_array[4] = {	ETHTOOL_LINK_MODE_10baseT_Half_BIT,	ETHTOOL_LINK_MODE_10baseT_Full_BIT,	ETHTOOL_LINK_MODE_100baseT_Half_BIT,	ETHTOOL_LINK_MODE_100baseT_Full_BIT,};EXPORT_SYMBOL_GPL(phy_10_100_features_array); const int phy_basic_t1_features_array[2] = {	ETHTOOL_LINK_MODE_TP_BIT,	ETHTOOL_LINK_MODE_100baseT1_Full_BIT,};EXPORT_SYMBOL_GPL(phy_basic_t1_features_array); const int phy_gbit_features_array[2] = {	ETHTOOL_LINK_MODE_1000baseT_Half_BIT,	ETHTOOL_LINK_MODE_1000baseT_Full_BIT,};EXPORT_SYMBOL_GPL(phy_gbit_features_array); const int phy_10gbit_features_array[1] = {	ETHTOOL_LINK_MODE_10000baseT_Full_BIT,};EXPORT_SYMBOL_GPL(phy_10gbit_features_array); static const int phy_10gbit_fec_features_array[1] = {	ETHTOOL_LINK_MODE_10000baseR_FEC_BIT,}; __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_full_features) __ro_after_init;EXPORT_SYMBOL_GPL(phy_10gbit_full_features); static const int phy_10gbit_full_features_array[] = {	ETHTOOL_LINK_MODE_10baseT_Full_BIT,	ETHTOOL_LINK_MODE_100baseT_Full_BIT,	ETHTOOL_LINK_MODE_1000baseT_Full_BIT,	ETHTOOL_LINK_MODE_10000baseT_Full_BIT,}; static void features_init(void){	/* 10/100 half/full*/	linkmode_set_bit_array(phy_basic_ports_array,			       ARRAY_SIZE(phy_basic_ports_array),			       phy_basic_features);	linkmode_set_bit_array(phy_10_100_features_array,			       ARRAY_SIZE(phy_10_100_features_array),			       phy_basic_features); 	/* 100 full, TP */	linkmode_set_bit_array(phy_basic_t1_features_array,			       ARRAY_SIZE(phy_basic_t1_features_array),			       phy_basic_t1_features); 	/* 10/100 half/full + 1000 half/full */	linkmode_set_bit_array(phy_basic_ports_array,			       ARRAY_SIZE(phy_basic_ports_array),			       phy_gbit_features);	linkmode_set_bit_array(phy_10_100_features_array,			       ARRAY_SIZE(phy_10_100_features_array),			       phy_gbit_features);	linkmode_set_bit_array(phy_gbit_features_array,			       ARRAY_SIZE(phy_gbit_features_array),			       phy_gbit_features); 	/* 10/100 half/full + 1000 half/full + fibre*/	linkmode_set_bit_array(phy_basic_ports_array,			       ARRAY_SIZE(phy_basic_ports_array),			       phy_gbit_fibre_features);	linkmode_set_bit_array(phy_10_100_features_array,			       ARRAY_SIZE(phy_10_100_features_array),			       phy_gbit_fibre_features);	linkmode_set_bit_array(phy_gbit_features_array,			       ARRAY_SIZE(phy_gbit_features_array),			       phy_gbit_fibre_features);	linkmode_set_bit_array(phy_fibre_port_array,			       ARRAY_SIZE(phy_fibre_port_array),			       phy_gbit_fibre_features); 	/* 10/100 half/full + 1000 half/full + TP/MII/FIBRE/AUI/BNC/Backplane*/	linkmode_set_bit_array(phy_all_ports_features_array,			       ARRAY_SIZE(phy_all_ports_features_array),			       phy_gbit_all_ports_features);	linkmode_set_bit_array(phy_10_100_features_array,			       ARRAY_SIZE(phy_10_100_features_array),			       phy_gbit_all_ports_features);	linkmode_set_bit_array(phy_gbit_features_array,			       ARRAY_SIZE(phy_gbit_features_array),			       phy_gbit_all_ports_features); 	/* 10/100 half/full + 1000 half/full + 10G full*/	linkmode_set_bit_array(phy_all_ports_features_array,			       ARRAY_SIZE(phy_all_ports_features_array),			       phy_10gbit_features);	linkmode_set_bit_array(phy_10_100_features_array,			       ARRAY_SIZE(phy_10_100_features_array),			       phy_10gbit_features);	linkmode_set_bit_array(phy_gbit_features_array,			       ARRAY_SIZE(phy_gbit_features_array),			       phy_10gbit_features);	linkmode_set_bit_array(phy_10gbit_features_array,			       ARRAY_SIZE(phy_10gbit_features_array),			       phy_10gbit_features); 	/* 10/100/1000/10G full */	linkmode_set_bit_array(phy_all_ports_features_array,			       ARRAY_SIZE(phy_all_ports_features_array),			       phy_10gbit_full_features);	linkmode_set_bit_array(phy_10gbit_full_features_array,			       ARRAY_SIZE(phy_10gbit_full_features_array),			       phy_10gbit_full_features);	/* 10G FEC only */	linkmode_set_bit_array(phy_10gbit_fec_features_array,			       ARRAY_SIZE(phy_10gbit_fec_features_array),			       phy_10gbit_fec_features);} void phy_device_free(struct phy_device *phydev){	put_device(&phydev->mdio.dev);}EXPORT_SYMBOL(phy_device_free); static void phy_mdio_device_free(struct mdio_device *mdiodev){	struct phy_device *phydev; 	phydev = container_of(mdiodev, struct phy_device, mdio);	phy_device_free(phydev);} static void phy_device_release(struct device *dev){	kfree(to_phy_device(dev));} static void phy_mdio_device_remove(struct mdio_device *mdiodev){	struct phy_device *phydev; 	phydev = container_of(mdiodev, struct phy_device, mdio);	phy_device_remove(phydev);} static struct phy_driver genphy_driver; static LIST_HEAD(phy_fixup_list);static DEFINE_MUTEX(phy_fixup_lock); static bool mdio_bus_phy_may_suspend(struct phy_device *phydev){	struct device_driver *drv = phydev->mdio.dev.driver;	struct phy_driver *phydrv = to_phy_driver(drv);	struct net_device *netdev = phydev->attached_dev; 	if (!drv || !phydrv->suspend)		return false; 	/* PHY not attached? May suspend if the PHY has not already been	 * suspended as part of a prior call to phy_disconnect() ->	 * phy_detach() -> phy_suspend() because the parent netdev might be the	 * MDIO bus driver and clock gated at this point.	 */	if (!netdev)		goto out; 	if (netdev->wol_enabled)		return false; 	/* As long as not all affected network drivers support the	 * wol_enabled flag, let's check for hints that WoL is enabled.	 * Don't suspend PHY if the attached netdev parent may wake up.	 * The parent may point to a PCI device, as in tg3 driver.	 */	if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent))		return false; 	/* Also don't suspend PHY if the netdev itself may wakeup. This	 * is the case for devices w/o underlaying pwr. mgmt. aware bus,	 * e.g. SoC devices.	 */	if (device_may_wakeup(&netdev->dev))		return false; out:	return !phydev->suspended;} static __maybe_unused int mdio_bus_phy_suspend(struct device *dev){	struct phy_device *phydev = to_phy_device(dev); 	if (phydev->mac_managed_pm)		return 0; 	/* We must stop the state machine manually, otherwise it stops out of	 * control, possibly with the phydev->lock held. Upon resume, netdev	 * may call phy routines that try to grab the same lock, and that may	 * lead to a deadlock.	 */	if (phydev->attached_dev && phydev->adjust_link)		phy_stop_machine(phydev); 	if (!mdio_bus_phy_may_suspend(phydev))		return 0; 	phydev->suspended_by_mdio_bus = 1; 	return phy_suspend(phydev);} static __maybe_unused int mdio_bus_phy_resume(struct device *dev){	struct phy_device *phydev = to_phy_device(dev);	int ret; 	if (phydev->mac_managed_pm)		return 0; 	if (!phydev->suspended_by_mdio_bus)		goto no_resume; 	phydev->suspended_by_mdio_bus = 0; 	ret = phy_init_hw(phydev);	if (ret < 0)		return ret; 	ret = phy_resume(phydev);	if (ret < 0)		return ret;no_resume:	if (phydev->attached_dev && phydev->adjust_link)		phy_start_machine(phydev); 	return 0;} static SIMPLE_DEV_PM_OPS(mdio_bus_phy_pm_ops, mdio_bus_phy_suspend,			 mdio_bus_phy_resume); /** * phy_register_fixup - creates a new phy_fixup and adds it to the list * @bus_id: A string which matches phydev->mdio.dev.bus_id (or PHY_ANY_ID) * @phy_uid: Used to match against phydev->phy_id (the UID of the PHY) *	It can also be PHY_ANY_UID * @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before *	comparison * @run: The actual code to be run when a matching PHY is found */int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,		       int (*run)(struct phy_device *)){	struct phy_fixup *fixup = kzalloc(sizeof(*fixup), GFP_KERNEL); 	if (!fixup)		return -ENOMEM; 	strlcpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id));	fixup->phy_uid = phy_uid;	fixup->phy_uid_mask = phy_uid_mask;	fixup->run = run; 	mutex_lock(&phy_fixup_lock);	list_add_tail(&fixup->list, &phy_fixup_list);	mutex_unlock(&phy_fixup_lock); 	return 0;}EXPORT_SYMBOL(phy_register_fixup); /* Registers a fixup to be run on any PHY with the UID in phy_uid */int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,			       int (*run)(struct phy_device *)){	return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run);}EXPORT_SYMBOL(phy_register_fixup_for_uid); /* Registers a fixup to be run on the PHY with id string bus_id */int phy_register_fixup_for_id(const char *bus_id,			      int (*run)(struct phy_device *)){	return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run);}EXPORT_SYMBOL(phy_register_fixup_for_id); /** * phy_unregister_fixup - remove a phy_fixup from the list * @bus_id: A string matches fixup->bus_id (or PHY_ANY_ID) in phy_fixup_list * @phy_uid: A phy id matches fixup->phy_id (or PHY_ANY_UID) in phy_fixup_list * @phy_uid_mask: Applied to phy_uid and fixup->phy_uid before comparison */int phy_unregister_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask){	struct list_head *pos, *n;	struct phy_fixup *fixup;	int ret; 	ret = -ENODEV; 	mutex_lock(&phy_fixup_lock);	list_for_each_safe(pos, n, &phy_fixup_list) {		fixup = list_entry(pos, struct phy_fixup, list); 		if ((!strcmp(fixup->bus_id, bus_id)) &&		    ((fixup->phy_uid & phy_uid_mask) ==		     (phy_uid & phy_uid_mask))) {			list_del(&fixup->list);			kfree(fixup);			ret = 0;			break;		}	}	mutex_unlock(&phy_fixup_lock); 	return ret;}EXPORT_SYMBOL(phy_unregister_fixup); /* Unregisters a fixup of any PHY with the UID in phy_uid */int phy_unregister_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask){	return phy_unregister_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask);}EXPORT_SYMBOL(phy_unregister_fixup_for_uid); /* Unregisters a fixup of the PHY with id string bus_id */int phy_unregister_fixup_for_id(const char *bus_id){	return phy_unregister_fixup(bus_id, PHY_ANY_UID, 0xffffffff);}EXPORT_SYMBOL(phy_unregister_fixup_for_id); /* Returns 1 if fixup matches phydev in bus_id and phy_uid. * Fixups can be set to match any in one or more fields. */static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup){	if (strcmp(fixup->bus_id, phydev_name(phydev)) != 0)		if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0)			return 0; 	if ((fixup->phy_uid & fixup->phy_uid_mask) !=	    (phydev->phy_id & fixup->phy_uid_mask))		if (fixup->phy_uid != PHY_ANY_UID)			return 0; 	return 1;} /* Runs any matching fixups for this phydev */static int phy_scan_fixups(struct phy_device *phydev){	struct phy_fixup *fixup; 	mutex_lock(&phy_fixup_lock);	list_for_each_entry(fixup, &phy_fixup_list, list) {		if (phy_needs_fixup(phydev, fixup)) {			int err = fixup->run(phydev); 			if (err < 0) {				mutex_unlock(&phy_fixup_lock);				return err;			}			phydev->has_fixups = true;		}	}	mutex_unlock(&phy_fixup_lock); 	return 0;} static int phy_bus_match(struct device *dev, struct device_driver *drv){	struct phy_device *phydev = to_phy_device(dev);	struct phy_driver *phydrv = to_phy_driver(drv);	const int num_ids = ARRAY_SIZE(phydev->c45_ids.device_ids);	int i; 	if (!(phydrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY))		return 0; 	if (phydrv->match_phy_device)		return phydrv->match_phy_device(phydev); 	if (phydev->is_c45) {		for (i = 1; i < num_ids; i++) {			if (phydev->c45_ids.device_ids[i] == 0xffffffff)				continue; 			if ((phydrv->phy_id & phydrv->phy_id_mask) ==			    (phydev->c45_ids.device_ids[i] &			     phydrv->phy_id_mask))				return 1;		}		return 0;	} else {		return (phydrv->phy_id & phydrv->phy_id_mask) ==			(phydev->phy_id & phydrv->phy_id_mask);	}} static ssize_tphy_id_show(struct device *dev, struct device_attribute *attr, char *buf){	struct phy_device *phydev = to_phy_device(dev); 	return sprintf(buf, "0x%.8lx\n", (unsigned long)phydev->phy_id);}static DEVICE_ATTR_RO(phy_id); static ssize_tphy_interface_show(struct device *dev, struct device_attribute *attr, char *buf){	struct phy_device *phydev = to_phy_device(dev);	const char *mode = NULL;

Lines 1–500 of 3127.