Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp3149279ybl; Sun, 11 Aug 2019 16:44:49 -0700 (PDT) X-Google-Smtp-Source: APXvYqxIxS23a2Q5r+XHgL//aya6BrgVxqI4AMtxDBQ+DbZzFEgwqLa1+bC7tqw9+AHBzphaEd2J X-Received: by 2002:aa7:9a52:: with SMTP id x18mr4443435pfj.8.1565567089085; Sun, 11 Aug 2019 16:44:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565567089; cv=none; d=google.com; s=arc-20160816; b=CHbSuK2KEdpEiThQWjiCXQcAtwD8ojHeceYadFF57PnhFhdMNSPjjIw/mp9iiaGHe2 Ve0CZmpGVDQCEEhDmvzIJCsVSf/lTlB77J8QOUoEjDPsvzx3ynSx+8iESU4bw8emZLsU iPfXQtDEyjKqi+CSiy8HCr3sdPzb/Y8zqcBr+qHROq1kbkpM8+xubNh3nBfH/DzmxgHq g0UdZ8ZruXfAWnpsHuC6r4LMIzV2VznmMN7xCVdl0O4+GNaA++JM+ertp6b7gnatcabO DSq6kv/z2w332j7fosCEBB7gD4JoD3z5vpDqj69xSd6S9NuIv3Jb0xZVKOtF3wBZWcFg 670w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:subject :smtp-origin-cluster:cc:to:smtp-origin-hostname:from :smtp-origin-hostprefix:dkim-signature; bh=MrZKlAH9JnSrKEaObAOABlgYJDtjm+iuZCQm//AVx9g=; b=FLRaljySGn98zKGGD1e+fzum/JOsXxQuWCEpOc/4OrmTjDXOdR91zqW11O4N9fcNHK kgDnrlC6Z1v+dhUGr/LggMD8Ea0RnwIG3esAv5x0ha4+HLX++TjevpvoX7eL8YyQznIb pMOvg4s1AjcjQLJqFruNPHrYAvQIet7G5fOlql904lRrlBE2A+bAUWrD6XU7KcuDZ9KO j7uiKztzNP274gvVpd1i5V+fQxzR0pSrp0GXUzciyyp/ZFX3NyVOEhyRl3qSA/E54d7a bzNJTFte0Faw4ynQYnsrnWOjQKgWwJjJhfAi14TR/V2d5KAYgaZSFUMeOneUSWSS25Uo d5cA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=M5l2N9re; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=fb.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o18si27488912pgv.494.2019.08.11.16.44.33; Sun, 11 Aug 2019 16:44:49 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=M5l2N9re; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=fb.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726727AbfHKXm0 (ORCPT + 99 others); Sun, 11 Aug 2019 19:42:26 -0400 Received: from mx0a-00082601.pphosted.com ([67.231.145.42]:54130 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726055AbfHKXmZ (ORCPT ); Sun, 11 Aug 2019 19:42:25 -0400 Received: from pps.filterd (m0148461.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x7BNgJrN013309 for ; Sun, 11 Aug 2019 16:42:24 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : mime-version : content-type; s=facebook; bh=MrZKlAH9JnSrKEaObAOABlgYJDtjm+iuZCQm//AVx9g=; b=M5l2N9re4yvBPDqpHk89qmCwrCH5xfd/8fp83gtcbrtXK7eKfCvHDhF4p8BuqkRmvr9I D3tH5LoIrIustYq/F51/NF2o+AzcrkCSJz66ZG4wyY3mQoWBBnbIXoq+Qwjjc5dNzSjJ YcOYIzqty6LqfAF7084RlCX5p9hooBSM1mw= Received: from maileast.thefacebook.com ([163.114.130.16]) by mx0a-00082601.pphosted.com with ESMTP id 2uagfjhpnk-12 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Sun, 11 Aug 2019 16:42:24 -0700 Received: from mx-out.facebook.com (2620:10d:c0a8:1b::d) by mail.thefacebook.com (2620:10d:c0a8:83::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1713.5; Sun, 11 Aug 2019 16:42:18 -0700 Received: by devvm1794.vll1.facebook.com (Postfix, from userid 150176) id 807AF7BE8A9; Sun, 11 Aug 2019 16:40:11 -0700 (PDT) Smtp-Origin-Hostprefix: devvm From: Tao Ren Smtp-Origin-Hostname: devvm1794.vll1.facebook.com To: Andrew Lunn , Florian Fainelli , Heiner Kallweit , "David S . Miller" , Vladimir Oltean , Arun Parameswaran , Justin Chen , , , CC: Tao Ren Smtp-Origin-Cluster: vll1c12 Subject: [PATCH net-next v7 2/3] net: phy: add support for clause 37 auto-negotiation Date: Sun, 11 Aug 2019 16:40:10 -0700 Message-ID: <20190811234010.3673592-1-taoren@fb.com> X-Mailer: git-send-email 2.17.1 X-FB-Internal: Safe MIME-Version: 1.0 Content-Type: text/plain X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2019-08-11_12:,, signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 priorityscore=1501 malwarescore=0 suspectscore=0 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=913 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1906280000 definitions=main-1908110265 X-FB-Internal: deliver Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Heiner Kallweit This patch adds support for clause 37 1000Base-X auto-negotiation. Signed-off-by: Heiner Kallweit Signed-off-by: Tao Ren --- Changes in v7: - Update "if (AUTONEG_ENABLE != phydev->autoneg)" to "if (phydev->autoneg != AUTONEG_ENABLE)" so checkpatch.pl is happy. Changes in v6: - add "Signed-off-by: Tao Ren " Changes in v1-v5: - nothing changed. It's given v5 just to align with the version of patch series. drivers/net/phy/phy_device.c | 139 +++++++++++++++++++++++++++++++++++ include/linux/phy.h | 5 ++ 2 files changed, 144 insertions(+) diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c index 252a712d1b2b..301a794b2963 100644 --- a/drivers/net/phy/phy_device.c +++ b/drivers/net/phy/phy_device.c @@ -1617,6 +1617,40 @@ static int genphy_config_advert(struct phy_device *phydev) return changed; } +/** + * genphy_c37_config_advert - sanitize and advertise auto-negotiation parameters + * @phydev: target phy_device struct + * + * Description: Writes MII_ADVERTISE with the appropriate values, + * after sanitizing the values to make sure we only advertise + * what is supported. Returns < 0 on error, 0 if the PHY's advertisement + * hasn't changed, and > 0 if it has changed. This function is intended + * for Clause 37 1000Base-X mode. + */ +static int genphy_c37_config_advert(struct phy_device *phydev) +{ + u16 adv = 0; + + /* Only allow advertising what this PHY supports */ + linkmode_and(phydev->advertising, phydev->advertising, + phydev->supported); + + if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT, + phydev->advertising)) + adv |= ADVERTISE_1000XFULL; + if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, + phydev->advertising)) + adv |= ADVERTISE_1000XPAUSE; + if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, + phydev->advertising)) + adv |= ADVERTISE_1000XPSE_ASYM; + + return phy_modify_changed(phydev, MII_ADVERTISE, + ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE | + ADVERTISE_1000XHALF | ADVERTISE_1000XPSE_ASYM, + adv); +} + /** * genphy_config_eee_advert - disable unwanted eee mode advertisement * @phydev: target phy_device struct @@ -1726,6 +1760,54 @@ int genphy_config_aneg(struct phy_device *phydev) } EXPORT_SYMBOL(genphy_config_aneg); +/** + * genphy_c37_config_aneg - restart auto-negotiation or write BMCR + * @phydev: target phy_device struct + * + * Description: If auto-negotiation is enabled, we configure the + * advertising, and then restart auto-negotiation. If it is not + * enabled, then we write the BMCR. This function is intended + * for use with Clause 37 1000Base-X mode. + */ +int genphy_c37_config_aneg(struct phy_device *phydev) +{ + int err, changed; + + if (phydev->autoneg != AUTONEG_ENABLE) + return genphy_setup_forced(phydev); + + err = phy_modify(phydev, MII_BMCR, BMCR_SPEED1000 | BMCR_SPEED100, + BMCR_SPEED1000); + if (err) + return err; + + changed = genphy_c37_config_advert(phydev); + if (changed < 0) /* error */ + return changed; + + if (!changed) { + /* Advertisement hasn't changed, but maybe aneg was never on to + * begin with? Or maybe phy was isolated? + */ + int ctl = phy_read(phydev, MII_BMCR); + + if (ctl < 0) + return ctl; + + if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE)) + changed = 1; /* do restart aneg */ + } + + /* Only restart aneg if we are advertising something different + * than we were before. + */ + if (changed > 0) + return genphy_restart_aneg(phydev); + + return 0; +} +EXPORT_SYMBOL(genphy_c37_config_aneg); + /** * genphy_aneg_done - return auto-negotiation status * @phydev: target phy_device struct @@ -1864,6 +1946,63 @@ int genphy_read_status(struct phy_device *phydev) } EXPORT_SYMBOL(genphy_read_status); +/** + * genphy_c37_read_status - check the link status and update current link state + * @phydev: target phy_device struct + * + * Description: Check the link, then figure out the current state + * by comparing what we advertise with what the link partner + * advertises. This function is for Clause 37 1000Base-X mode. + */ +int genphy_c37_read_status(struct phy_device *phydev) +{ + int lpa, err, old_link = phydev->link; + + /* Update the link, but return if there was an error */ + err = genphy_update_link(phydev); + if (err) + return err; + + /* why bother the PHY if nothing can have changed */ + if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link) + return 0; + + phydev->duplex = DUPLEX_UNKNOWN; + phydev->pause = 0; + phydev->asym_pause = 0; + + if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) { + lpa = phy_read(phydev, MII_LPA); + if (lpa < 0) + return lpa; + + linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, + phydev->lp_advertising, lpa & LPA_LPACK); + linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT, + phydev->lp_advertising, lpa & LPA_1000XFULL); + linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT, + phydev->lp_advertising, lpa & LPA_1000XPAUSE); + linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, + phydev->lp_advertising, + lpa & LPA_1000XPAUSE_ASYM); + + phy_resolve_aneg_linkmode(phydev); + } else if (phydev->autoneg == AUTONEG_DISABLE) { + int bmcr = phy_read(phydev, MII_BMCR); + + if (bmcr < 0) + return bmcr; + + if (bmcr & BMCR_FULLDPLX) + phydev->duplex = DUPLEX_FULL; + else + phydev->duplex = DUPLEX_HALF; + } + + return 0; +} +EXPORT_SYMBOL(genphy_c37_read_status); + /** * genphy_soft_reset - software reset the PHY via BMCR_RESET bit * @phydev: target phy_device struct diff --git a/include/linux/phy.h b/include/linux/phy.h index 462b90b73f93..81a2921512ee 100644 --- a/include/linux/phy.h +++ b/include/linux/phy.h @@ -1077,6 +1077,11 @@ int genphy_suspend(struct phy_device *phydev); int genphy_resume(struct phy_device *phydev); int genphy_loopback(struct phy_device *phydev, bool enable); int genphy_soft_reset(struct phy_device *phydev); + +/* Clause 37 */ +int genphy_c37_config_aneg(struct phy_device *phydev); +int genphy_c37_read_status(struct phy_device *phydev); + static inline int genphy_no_soft_reset(struct phy_device *phydev) { return 0; -- 2.17.1