1*4882a593Smuzhiyun.. SPDX-License-Identifier: GPL-2.0 2*4882a593Smuzhiyun 3*4882a593Smuzhiyun======= 4*4882a593Smuzhiyunphylink 5*4882a593Smuzhiyun======= 6*4882a593Smuzhiyun 7*4882a593SmuzhiyunOverview 8*4882a593Smuzhiyun======== 9*4882a593Smuzhiyun 10*4882a593Smuzhiyunphylink is a mechanism to support hot-pluggable networking modules 11*4882a593Smuzhiyundirectly connected to a MAC without needing to re-initialise the 12*4882a593Smuzhiyunadapter on hot-plug events. 13*4882a593Smuzhiyun 14*4882a593Smuzhiyunphylink supports conventional phylib-based setups, fixed link setups 15*4882a593Smuzhiyunand SFP (Small Formfactor Pluggable) modules at present. 16*4882a593Smuzhiyun 17*4882a593SmuzhiyunModes of operation 18*4882a593Smuzhiyun================== 19*4882a593Smuzhiyun 20*4882a593Smuzhiyunphylink has several modes of operation, which depend on the firmware 21*4882a593Smuzhiyunsettings. 22*4882a593Smuzhiyun 23*4882a593Smuzhiyun1. PHY mode 24*4882a593Smuzhiyun 25*4882a593Smuzhiyun In PHY mode, we use phylib to read the current link settings from 26*4882a593Smuzhiyun the PHY, and pass them to the MAC driver. We expect the MAC driver 27*4882a593Smuzhiyun to configure exactly the modes that are specified without any 28*4882a593Smuzhiyun negotiation being enabled on the link. 29*4882a593Smuzhiyun 30*4882a593Smuzhiyun2. Fixed mode 31*4882a593Smuzhiyun 32*4882a593Smuzhiyun Fixed mode is the same as PHY mode as far as the MAC driver is 33*4882a593Smuzhiyun concerned. 34*4882a593Smuzhiyun 35*4882a593Smuzhiyun3. In-band mode 36*4882a593Smuzhiyun 37*4882a593Smuzhiyun In-band mode is used with 802.3z, SGMII and similar interface modes, 38*4882a593Smuzhiyun and we are expecting to use and honor the in-band negotiation or 39*4882a593Smuzhiyun control word sent across the serdes channel. 40*4882a593Smuzhiyun 41*4882a593SmuzhiyunBy example, what this means is that: 42*4882a593Smuzhiyun 43*4882a593Smuzhiyun.. code-block:: none 44*4882a593Smuzhiyun 45*4882a593Smuzhiyun ð { 46*4882a593Smuzhiyun phy = <&phy>; 47*4882a593Smuzhiyun phy-mode = "sgmii"; 48*4882a593Smuzhiyun }; 49*4882a593Smuzhiyun 50*4882a593Smuzhiyundoes not use in-band SGMII signalling. The PHY is expected to follow 51*4882a593Smuzhiyunexactly the settings given to it in its :c:func:`mac_config` function. 52*4882a593SmuzhiyunThe link should be forced up or down appropriately in the 53*4882a593Smuzhiyun:c:func:`mac_link_up` and :c:func:`mac_link_down` functions. 54*4882a593Smuzhiyun 55*4882a593Smuzhiyun.. code-block:: none 56*4882a593Smuzhiyun 57*4882a593Smuzhiyun ð { 58*4882a593Smuzhiyun managed = "in-band-status"; 59*4882a593Smuzhiyun phy = <&phy>; 60*4882a593Smuzhiyun phy-mode = "sgmii"; 61*4882a593Smuzhiyun }; 62*4882a593Smuzhiyun 63*4882a593Smuzhiyunuses in-band mode, where results from the PHY's negotiation are passed 64*4882a593Smuzhiyunto the MAC through the SGMII control word, and the MAC is expected to 65*4882a593Smuzhiyunacknowledge the control word. The :c:func:`mac_link_up` and 66*4882a593Smuzhiyun:c:func:`mac_link_down` functions must not force the MAC side link 67*4882a593Smuzhiyunup and down. 68*4882a593Smuzhiyun 69*4882a593SmuzhiyunRough guide to converting a network driver to sfp/phylink 70*4882a593Smuzhiyun========================================================= 71*4882a593Smuzhiyun 72*4882a593SmuzhiyunThis guide briefly describes how to convert a network driver from 73*4882a593Smuzhiyunphylib to the sfp/phylink support. Please send patches to improve 74*4882a593Smuzhiyunthis documentation. 75*4882a593Smuzhiyun 76*4882a593Smuzhiyun1. Optionally split the network driver's phylib update function into 77*4882a593Smuzhiyun two parts dealing with link-down and link-up. This can be done as 78*4882a593Smuzhiyun a separate preparation commit. 79*4882a593Smuzhiyun 80*4882a593Smuzhiyun An older example of this preparation can be found in git commit 81*4882a593Smuzhiyun fc548b991fb0, although this was splitting into three parts; the 82*4882a593Smuzhiyun link-up part now includes configuring the MAC for the link settings. 83*4882a593Smuzhiyun Please see :c:func:`mac_link_up` for more information on this. 84*4882a593Smuzhiyun 85*4882a593Smuzhiyun2. Replace:: 86*4882a593Smuzhiyun 87*4882a593Smuzhiyun select FIXED_PHY 88*4882a593Smuzhiyun select PHYLIB 89*4882a593Smuzhiyun 90*4882a593Smuzhiyun with:: 91*4882a593Smuzhiyun 92*4882a593Smuzhiyun select PHYLINK 93*4882a593Smuzhiyun 94*4882a593Smuzhiyun in the driver's Kconfig stanza. 95*4882a593Smuzhiyun 96*4882a593Smuzhiyun3. Add:: 97*4882a593Smuzhiyun 98*4882a593Smuzhiyun #include <linux/phylink.h> 99*4882a593Smuzhiyun 100*4882a593Smuzhiyun to the driver's list of header files. 101*4882a593Smuzhiyun 102*4882a593Smuzhiyun4. Add:: 103*4882a593Smuzhiyun 104*4882a593Smuzhiyun struct phylink *phylink; 105*4882a593Smuzhiyun struct phylink_config phylink_config; 106*4882a593Smuzhiyun 107*4882a593Smuzhiyun to the driver's private data structure. We shall refer to the 108*4882a593Smuzhiyun driver's private data pointer as ``priv`` below, and the driver's 109*4882a593Smuzhiyun private data structure as ``struct foo_priv``. 110*4882a593Smuzhiyun 111*4882a593Smuzhiyun5. Replace the following functions: 112*4882a593Smuzhiyun 113*4882a593Smuzhiyun .. flat-table:: 114*4882a593Smuzhiyun :header-rows: 1 115*4882a593Smuzhiyun :widths: 1 1 116*4882a593Smuzhiyun :stub-columns: 0 117*4882a593Smuzhiyun 118*4882a593Smuzhiyun * - Original function 119*4882a593Smuzhiyun - Replacement function 120*4882a593Smuzhiyun * - phy_start(phydev) 121*4882a593Smuzhiyun - phylink_start(priv->phylink) 122*4882a593Smuzhiyun * - phy_stop(phydev) 123*4882a593Smuzhiyun - phylink_stop(priv->phylink) 124*4882a593Smuzhiyun * - phy_mii_ioctl(phydev, ifr, cmd) 125*4882a593Smuzhiyun - phylink_mii_ioctl(priv->phylink, ifr, cmd) 126*4882a593Smuzhiyun * - phy_ethtool_get_wol(phydev, wol) 127*4882a593Smuzhiyun - phylink_ethtool_get_wol(priv->phylink, wol) 128*4882a593Smuzhiyun * - phy_ethtool_set_wol(phydev, wol) 129*4882a593Smuzhiyun - phylink_ethtool_set_wol(priv->phylink, wol) 130*4882a593Smuzhiyun * - phy_disconnect(phydev) 131*4882a593Smuzhiyun - phylink_disconnect_phy(priv->phylink) 132*4882a593Smuzhiyun 133*4882a593Smuzhiyun Please note that some of these functions must be called under the 134*4882a593Smuzhiyun rtnl lock, and will warn if not. This will normally be the case, 135*4882a593Smuzhiyun except if these are called from the driver suspend/resume paths. 136*4882a593Smuzhiyun 137*4882a593Smuzhiyun6. Add/replace ksettings get/set methods with: 138*4882a593Smuzhiyun 139*4882a593Smuzhiyun .. code-block:: c 140*4882a593Smuzhiyun 141*4882a593Smuzhiyun static int foo_ethtool_set_link_ksettings(struct net_device *dev, 142*4882a593Smuzhiyun const struct ethtool_link_ksettings *cmd) 143*4882a593Smuzhiyun { 144*4882a593Smuzhiyun struct foo_priv *priv = netdev_priv(dev); 145*4882a593Smuzhiyun 146*4882a593Smuzhiyun return phylink_ethtool_ksettings_set(priv->phylink, cmd); 147*4882a593Smuzhiyun } 148*4882a593Smuzhiyun 149*4882a593Smuzhiyun static int foo_ethtool_get_link_ksettings(struct net_device *dev, 150*4882a593Smuzhiyun struct ethtool_link_ksettings *cmd) 151*4882a593Smuzhiyun { 152*4882a593Smuzhiyun struct foo_priv *priv = netdev_priv(dev); 153*4882a593Smuzhiyun 154*4882a593Smuzhiyun return phylink_ethtool_ksettings_get(priv->phylink, cmd); 155*4882a593Smuzhiyun } 156*4882a593Smuzhiyun 157*4882a593Smuzhiyun7. Replace the call to:: 158*4882a593Smuzhiyun 159*4882a593Smuzhiyun phy_dev = of_phy_connect(dev, node, link_func, flags, phy_interface); 160*4882a593Smuzhiyun 161*4882a593Smuzhiyun and associated code with a call to:: 162*4882a593Smuzhiyun 163*4882a593Smuzhiyun err = phylink_of_phy_connect(priv->phylink, node, flags); 164*4882a593Smuzhiyun 165*4882a593Smuzhiyun For the most part, ``flags`` can be zero; these flags are passed to 166*4882a593Smuzhiyun the of_phy_attach() inside this function call if a PHY is specified 167*4882a593Smuzhiyun in the DT node ``node``. 168*4882a593Smuzhiyun 169*4882a593Smuzhiyun ``node`` should be the DT node which contains the network phy property, 170*4882a593Smuzhiyun fixed link properties, and will also contain the sfp property. 171*4882a593Smuzhiyun 172*4882a593Smuzhiyun The setup of fixed links should also be removed; these are handled 173*4882a593Smuzhiyun internally by phylink. 174*4882a593Smuzhiyun 175*4882a593Smuzhiyun of_phy_connect() was also passed a function pointer for link updates. 176*4882a593Smuzhiyun This function is replaced by a different form of MAC updates 177*4882a593Smuzhiyun described below in (8). 178*4882a593Smuzhiyun 179*4882a593Smuzhiyun Manipulation of the PHY's supported/advertised happens within phylink 180*4882a593Smuzhiyun based on the validate callback, see below in (8). 181*4882a593Smuzhiyun 182*4882a593Smuzhiyun Note that the driver no longer needs to store the ``phy_interface``, 183*4882a593Smuzhiyun and also note that ``phy_interface`` becomes a dynamic property, 184*4882a593Smuzhiyun just like the speed, duplex etc. settings. 185*4882a593Smuzhiyun 186*4882a593Smuzhiyun Finally, note that the MAC driver has no direct access to the PHY 187*4882a593Smuzhiyun anymore; that is because in the phylink model, the PHY can be 188*4882a593Smuzhiyun dynamic. 189*4882a593Smuzhiyun 190*4882a593Smuzhiyun8. Add a :c:type:`struct phylink_mac_ops <phylink_mac_ops>` instance to 191*4882a593Smuzhiyun the driver, which is a table of function pointers, and implement 192*4882a593Smuzhiyun these functions. The old link update function for 193*4882a593Smuzhiyun :c:func:`of_phy_connect` becomes three methods: :c:func:`mac_link_up`, 194*4882a593Smuzhiyun :c:func:`mac_link_down`, and :c:func:`mac_config`. If step 1 was 195*4882a593Smuzhiyun performed, then the functionality will have been split there. 196*4882a593Smuzhiyun 197*4882a593Smuzhiyun It is important that if in-band negotiation is used, 198*4882a593Smuzhiyun :c:func:`mac_link_up` and :c:func:`mac_link_down` do not prevent the 199*4882a593Smuzhiyun in-band negotiation from completing, since these functions are called 200*4882a593Smuzhiyun when the in-band link state changes - otherwise the link will never 201*4882a593Smuzhiyun come up. 202*4882a593Smuzhiyun 203*4882a593Smuzhiyun The :c:func:`validate` method should mask the supplied supported mask, 204*4882a593Smuzhiyun and ``state->advertising`` with the supported ethtool link modes. 205*4882a593Smuzhiyun These are the new ethtool link modes, so bitmask operations must be 206*4882a593Smuzhiyun used. For an example, see drivers/net/ethernet/marvell/mvneta.c. 207*4882a593Smuzhiyun 208*4882a593Smuzhiyun The :c:func:`mac_link_state` method is used to read the link state 209*4882a593Smuzhiyun from the MAC, and report back the settings that the MAC is currently 210*4882a593Smuzhiyun using. This is particularly important for in-band negotiation 211*4882a593Smuzhiyun methods such as 1000base-X and SGMII. 212*4882a593Smuzhiyun 213*4882a593Smuzhiyun The :c:func:`mac_link_up` method is used to inform the MAC that the 214*4882a593Smuzhiyun link has come up. The call includes the negotiation mode and interface 215*4882a593Smuzhiyun for reference only. The finalised link parameters are also supplied 216*4882a593Smuzhiyun (speed, duplex and flow control/pause enablement settings) which 217*4882a593Smuzhiyun should be used to configure the MAC when the MAC and PCS are not 218*4882a593Smuzhiyun tightly integrated, or when the settings are not coming from in-band 219*4882a593Smuzhiyun negotiation. 220*4882a593Smuzhiyun 221*4882a593Smuzhiyun The :c:func:`mac_config` method is used to update the MAC with the 222*4882a593Smuzhiyun requested state, and must avoid unnecessarily taking the link down 223*4882a593Smuzhiyun when making changes to the MAC configuration. This means the 224*4882a593Smuzhiyun function should modify the state and only take the link down when 225*4882a593Smuzhiyun absolutely necessary to change the MAC configuration. An example 226*4882a593Smuzhiyun of how to do this can be found in :c:func:`mvneta_mac_config` in 227*4882a593Smuzhiyun drivers/net/ethernet/marvell/mvneta.c. 228*4882a593Smuzhiyun 229*4882a593Smuzhiyun For further information on these methods, please see the inline 230*4882a593Smuzhiyun documentation in :c:type:`struct phylink_mac_ops <phylink_mac_ops>`. 231*4882a593Smuzhiyun 232*4882a593Smuzhiyun9. Remove calls to of_parse_phandle() for the PHY, 233*4882a593Smuzhiyun of_phy_register_fixed_link() for fixed links etc. from the probe 234*4882a593Smuzhiyun function, and replace with: 235*4882a593Smuzhiyun 236*4882a593Smuzhiyun .. code-block:: c 237*4882a593Smuzhiyun 238*4882a593Smuzhiyun struct phylink *phylink; 239*4882a593Smuzhiyun priv->phylink_config.dev = &dev.dev; 240*4882a593Smuzhiyun priv->phylink_config.type = PHYLINK_NETDEV; 241*4882a593Smuzhiyun 242*4882a593Smuzhiyun phylink = phylink_create(&priv->phylink_config, node, phy_mode, &phylink_ops); 243*4882a593Smuzhiyun if (IS_ERR(phylink)) { 244*4882a593Smuzhiyun err = PTR_ERR(phylink); 245*4882a593Smuzhiyun fail probe; 246*4882a593Smuzhiyun } 247*4882a593Smuzhiyun 248*4882a593Smuzhiyun priv->phylink = phylink; 249*4882a593Smuzhiyun 250*4882a593Smuzhiyun and arrange to destroy the phylink in the probe failure path as 251*4882a593Smuzhiyun appropriate and the removal path too by calling: 252*4882a593Smuzhiyun 253*4882a593Smuzhiyun .. code-block:: c 254*4882a593Smuzhiyun 255*4882a593Smuzhiyun phylink_destroy(priv->phylink); 256*4882a593Smuzhiyun 257*4882a593Smuzhiyun10. Arrange for MAC link state interrupts to be forwarded into 258*4882a593Smuzhiyun phylink, via: 259*4882a593Smuzhiyun 260*4882a593Smuzhiyun .. code-block:: c 261*4882a593Smuzhiyun 262*4882a593Smuzhiyun phylink_mac_change(priv->phylink, link_is_up); 263*4882a593Smuzhiyun 264*4882a593Smuzhiyun where ``link_is_up`` is true if the link is currently up or false 265*4882a593Smuzhiyun otherwise. If a MAC is unable to provide these interrupts, then 266*4882a593Smuzhiyun it should set ``priv->phylink_config.pcs_poll = true;`` in step 9. 267*4882a593Smuzhiyun 268*4882a593Smuzhiyun11. Verify that the driver does not call:: 269*4882a593Smuzhiyun 270*4882a593Smuzhiyun netif_carrier_on() 271*4882a593Smuzhiyun netif_carrier_off() 272*4882a593Smuzhiyun 273*4882a593Smuzhiyun as these will interfere with phylink's tracking of the link state, 274*4882a593Smuzhiyun and cause phylink to omit calls via the :c:func:`mac_link_up` and 275*4882a593Smuzhiyun :c:func:`mac_link_down` methods. 276*4882a593Smuzhiyun 277*4882a593SmuzhiyunNetwork drivers should call phylink_stop() and phylink_start() via their 278*4882a593Smuzhiyunsuspend/resume paths, which ensures that the appropriate 279*4882a593Smuzhiyun:c:type:`struct phylink_mac_ops <phylink_mac_ops>` methods are called 280*4882a593Smuzhiyunas necessary. 281*4882a593Smuzhiyun 282*4882a593SmuzhiyunFor information describing the SFP cage in DT, please see the binding 283*4882a593Smuzhiyundocumentation in the kernel source tree 284*4882a593Smuzhiyun``Documentation/devicetree/bindings/net/sff,sfp.txt`` 285