Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * This file is based on code from OCTEON SDK by Cavium Networks.
0004  *
0005  * Copyright (c) 2003-2007 Cavium Networks
0006  */
0007 
0008 #include <linux/kernel.h>
0009 #include <linux/ethtool.h>
0010 #include <linux/phy.h>
0011 #include <linux/ratelimit.h>
0012 #include <linux/of_mdio.h>
0013 #include <generated/utsrelease.h>
0014 #include <net/dst.h>
0015 
0016 #include "octeon-ethernet.h"
0017 #include "ethernet-defines.h"
0018 #include "ethernet-mdio.h"
0019 #include "ethernet-util.h"
0020 
0021 static void cvm_oct_get_drvinfo(struct net_device *dev,
0022                 struct ethtool_drvinfo *info)
0023 {
0024     strscpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
0025     strscpy(info->version, UTS_RELEASE, sizeof(info->version));
0026     strscpy(info->bus_info, "Builtin", sizeof(info->bus_info));
0027 }
0028 
0029 static int cvm_oct_nway_reset(struct net_device *dev)
0030 {
0031     if (!capable(CAP_NET_ADMIN))
0032         return -EPERM;
0033 
0034     if (dev->phydev)
0035         return phy_start_aneg(dev->phydev);
0036 
0037     return -EINVAL;
0038 }
0039 
0040 const struct ethtool_ops cvm_oct_ethtool_ops = {
0041     .get_drvinfo = cvm_oct_get_drvinfo,
0042     .nway_reset = cvm_oct_nway_reset,
0043     .get_link = ethtool_op_get_link,
0044     .get_link_ksettings = phy_ethtool_get_link_ksettings,
0045     .set_link_ksettings = phy_ethtool_set_link_ksettings,
0046 };
0047 
0048 /**
0049  * cvm_oct_ioctl - IOCTL support for PHY control
0050  * @dev:    Device to change
0051  * @rq:     the request
0052  * @cmd:    the command
0053  *
0054  * Returns Zero on success
0055  */
0056 int cvm_oct_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
0057 {
0058     if (!netif_running(dev))
0059         return -EINVAL;
0060 
0061     if (!dev->phydev)
0062         return -EINVAL;
0063 
0064     return phy_mii_ioctl(dev->phydev, rq, cmd);
0065 }
0066 
0067 void cvm_oct_note_carrier(struct octeon_ethernet *priv,
0068               union cvmx_helper_link_info li)
0069 {
0070     if (li.s.link_up) {
0071         pr_notice_ratelimited("%s: %u Mbps %s duplex, port %d, queue %d\n",
0072                       netdev_name(priv->netdev), li.s.speed,
0073                       (li.s.full_duplex) ? "Full" : "Half",
0074                       priv->port, priv->queue);
0075     } else {
0076         pr_notice_ratelimited("%s: Link down\n",
0077                       netdev_name(priv->netdev));
0078     }
0079 }
0080 
0081 void cvm_oct_adjust_link(struct net_device *dev)
0082 {
0083     struct octeon_ethernet *priv = netdev_priv(dev);
0084     union cvmx_helper_link_info link_info;
0085 
0086     link_info.u64       = 0;
0087     link_info.s.link_up = dev->phydev->link ? 1 : 0;
0088     link_info.s.full_duplex = dev->phydev->duplex ? 1 : 0;
0089     link_info.s.speed   = dev->phydev->speed;
0090     priv->link_info     = link_info.u64;
0091 
0092     /*
0093      * The polling task need to know about link status changes.
0094      */
0095     if (priv->poll)
0096         priv->poll(dev);
0097 
0098     if (priv->last_link != dev->phydev->link) {
0099         priv->last_link = dev->phydev->link;
0100         cvmx_helper_link_set(priv->port, link_info);
0101         cvm_oct_note_carrier(priv, link_info);
0102     }
0103 }
0104 
0105 int cvm_oct_common_stop(struct net_device *dev)
0106 {
0107     struct octeon_ethernet *priv = netdev_priv(dev);
0108     int interface = INTERFACE(priv->port);
0109     union cvmx_helper_link_info link_info;
0110     union cvmx_gmxx_prtx_cfg gmx_cfg;
0111     int index = INDEX(priv->port);
0112 
0113     gmx_cfg.u64 = cvmx_read_csr(CVMX_GMXX_PRTX_CFG(index, interface));
0114     gmx_cfg.s.en = 0;
0115     cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface), gmx_cfg.u64);
0116 
0117     priv->poll = NULL;
0118 
0119     if (dev->phydev)
0120         phy_disconnect(dev->phydev);
0121 
0122     if (priv->last_link) {
0123         link_info.u64 = 0;
0124         priv->last_link = 0;
0125 
0126         cvmx_helper_link_set(priv->port, link_info);
0127         cvm_oct_note_carrier(priv, link_info);
0128     }
0129     return 0;
0130 }
0131 
0132 /**
0133  * cvm_oct_phy_setup_device - setup the PHY
0134  *
0135  * @dev:    Device to setup
0136  *
0137  * Returns Zero on success, negative on failure
0138  */
0139 int cvm_oct_phy_setup_device(struct net_device *dev)
0140 {
0141     struct octeon_ethernet *priv = netdev_priv(dev);
0142     struct device_node *phy_node;
0143     struct phy_device *phydev = NULL;
0144 
0145     if (!priv->of_node)
0146         goto no_phy;
0147 
0148     phy_node = of_parse_phandle(priv->of_node, "phy-handle", 0);
0149     if (!phy_node && of_phy_is_fixed_link(priv->of_node))
0150         phy_node = of_node_get(priv->of_node);
0151     if (!phy_node)
0152         goto no_phy;
0153 
0154     phydev = of_phy_connect(dev, phy_node, cvm_oct_adjust_link, 0,
0155                 priv->phy_mode);
0156     of_node_put(phy_node);
0157 
0158     if (!phydev)
0159         return -EPROBE_DEFER;
0160 
0161     priv->last_link = 0;
0162     phy_start(phydev);
0163 
0164     return 0;
0165 no_phy:
0166     /* If there is no phy, assume a direct MAC connection and that
0167      * the link is up.
0168      */
0169     netif_carrier_on(dev);
0170     return 0;
0171 }