162306a36Sopenharmony_ci.. SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci
362306a36Sopenharmony_ci=======
462306a36Sopenharmony_ciphylink
562306a36Sopenharmony_ci=======
662306a36Sopenharmony_ci
762306a36Sopenharmony_ciOverview
862306a36Sopenharmony_ci========
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ciphylink is a mechanism to support hot-pluggable networking modules
1162306a36Sopenharmony_cidirectly connected to a MAC without needing to re-initialise the
1262306a36Sopenharmony_ciadapter on hot-plug events.
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ciphylink supports conventional phylib-based setups, fixed link setups
1562306a36Sopenharmony_ciand SFP (Small Formfactor Pluggable) modules at present.
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ciModes of operation
1862306a36Sopenharmony_ci==================
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ciphylink has several modes of operation, which depend on the firmware
2162306a36Sopenharmony_cisettings.
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci1. PHY mode
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci   In PHY mode, we use phylib to read the current link settings from
2662306a36Sopenharmony_ci   the PHY, and pass them to the MAC driver.  We expect the MAC driver
2762306a36Sopenharmony_ci   to configure exactly the modes that are specified without any
2862306a36Sopenharmony_ci   negotiation being enabled on the link.
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci2. Fixed mode
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci   Fixed mode is the same as PHY mode as far as the MAC driver is
3362306a36Sopenharmony_ci   concerned.
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci3. In-band mode
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci   In-band mode is used with 802.3z, SGMII and similar interface modes,
3862306a36Sopenharmony_ci   and we are expecting to use and honor the in-band negotiation or
3962306a36Sopenharmony_ci   control word sent across the serdes channel.
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ciBy example, what this means is that:
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci.. code-block:: none
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci  &eth {
4662306a36Sopenharmony_ci    phy = <&phy>;
4762306a36Sopenharmony_ci    phy-mode = "sgmii";
4862306a36Sopenharmony_ci  };
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_cidoes not use in-band SGMII signalling.  The PHY is expected to follow
5162306a36Sopenharmony_ciexactly the settings given to it in its :c:func:`mac_config` function.
5262306a36Sopenharmony_ciThe link should be forced up or down appropriately in the
5362306a36Sopenharmony_ci:c:func:`mac_link_up` and :c:func:`mac_link_down` functions.
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci.. code-block:: none
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci  &eth {
5862306a36Sopenharmony_ci    managed = "in-band-status";
5962306a36Sopenharmony_ci    phy = <&phy>;
6062306a36Sopenharmony_ci    phy-mode = "sgmii";
6162306a36Sopenharmony_ci  };
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ciuses in-band mode, where results from the PHY's negotiation are passed
6462306a36Sopenharmony_cito the MAC through the SGMII control word, and the MAC is expected to
6562306a36Sopenharmony_ciacknowledge the control word.  The :c:func:`mac_link_up` and
6662306a36Sopenharmony_ci:c:func:`mac_link_down` functions must not force the MAC side link
6762306a36Sopenharmony_ciup and down.
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ciRough guide to converting a network driver to sfp/phylink
7062306a36Sopenharmony_ci=========================================================
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ciThis guide briefly describes how to convert a network driver from
7362306a36Sopenharmony_ciphylib to the sfp/phylink support.  Please send patches to improve
7462306a36Sopenharmony_cithis documentation.
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci1. Optionally split the network driver's phylib update function into
7762306a36Sopenharmony_ci   two parts dealing with link-down and link-up. This can be done as
7862306a36Sopenharmony_ci   a separate preparation commit.
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci   An older example of this preparation can be found in git commit
8162306a36Sopenharmony_ci   fc548b991fb0, although this was splitting into three parts; the
8262306a36Sopenharmony_ci   link-up part now includes configuring the MAC for the link settings.
8362306a36Sopenharmony_ci   Please see :c:func:`mac_link_up` for more information on this.
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci2. Replace::
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci	select FIXED_PHY
8862306a36Sopenharmony_ci	select PHYLIB
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci   with::
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci	select PHYLINK
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci   in the driver's Kconfig stanza.
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci3. Add::
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci	#include <linux/phylink.h>
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci   to the driver's list of header files.
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci4. Add::
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci	struct phylink *phylink;
10562306a36Sopenharmony_ci	struct phylink_config phylink_config;
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci   to the driver's private data structure.  We shall refer to the
10862306a36Sopenharmony_ci   driver's private data pointer as ``priv`` below, and the driver's
10962306a36Sopenharmony_ci   private data structure as ``struct foo_priv``.
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci5. Replace the following functions:
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci   .. flat-table::
11462306a36Sopenharmony_ci    :header-rows: 1
11562306a36Sopenharmony_ci    :widths: 1 1
11662306a36Sopenharmony_ci    :stub-columns: 0
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ci    * - Original function
11962306a36Sopenharmony_ci      - Replacement function
12062306a36Sopenharmony_ci    * - phy_start(phydev)
12162306a36Sopenharmony_ci      - phylink_start(priv->phylink)
12262306a36Sopenharmony_ci    * - phy_stop(phydev)
12362306a36Sopenharmony_ci      - phylink_stop(priv->phylink)
12462306a36Sopenharmony_ci    * - phy_mii_ioctl(phydev, ifr, cmd)
12562306a36Sopenharmony_ci      - phylink_mii_ioctl(priv->phylink, ifr, cmd)
12662306a36Sopenharmony_ci    * - phy_ethtool_get_wol(phydev, wol)
12762306a36Sopenharmony_ci      - phylink_ethtool_get_wol(priv->phylink, wol)
12862306a36Sopenharmony_ci    * - phy_ethtool_set_wol(phydev, wol)
12962306a36Sopenharmony_ci      - phylink_ethtool_set_wol(priv->phylink, wol)
13062306a36Sopenharmony_ci    * - phy_disconnect(phydev)
13162306a36Sopenharmony_ci      - phylink_disconnect_phy(priv->phylink)
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ci   Please note that some of these functions must be called under the
13462306a36Sopenharmony_ci   rtnl lock, and will warn if not. This will normally be the case,
13562306a36Sopenharmony_ci   except if these are called from the driver suspend/resume paths.
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci6. Add/replace ksettings get/set methods with:
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci   .. code-block:: c
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci	static int foo_ethtool_set_link_ksettings(struct net_device *dev,
14262306a36Sopenharmony_ci						  const struct ethtool_link_ksettings *cmd)
14362306a36Sopenharmony_ci	{
14462306a36Sopenharmony_ci		struct foo_priv *priv = netdev_priv(dev);
14562306a36Sopenharmony_ci	
14662306a36Sopenharmony_ci		return phylink_ethtool_ksettings_set(priv->phylink, cmd);
14762306a36Sopenharmony_ci	}
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ci	static int foo_ethtool_get_link_ksettings(struct net_device *dev,
15062306a36Sopenharmony_ci						  struct ethtool_link_ksettings *cmd)
15162306a36Sopenharmony_ci	{
15262306a36Sopenharmony_ci		struct foo_priv *priv = netdev_priv(dev);
15362306a36Sopenharmony_ci	
15462306a36Sopenharmony_ci		return phylink_ethtool_ksettings_get(priv->phylink, cmd);
15562306a36Sopenharmony_ci	}
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ci7. Replace the call to::
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ci	phy_dev = of_phy_connect(dev, node, link_func, flags, phy_interface);
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ci   and associated code with a call to::
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_ci	err = phylink_of_phy_connect(priv->phylink, node, flags);
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ci   For the most part, ``flags`` can be zero; these flags are passed to
16662306a36Sopenharmony_ci   the phy_attach_direct() inside this function call if a PHY is specified
16762306a36Sopenharmony_ci   in the DT node ``node``.
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ci   ``node`` should be the DT node which contains the network phy property,
17062306a36Sopenharmony_ci   fixed link properties, and will also contain the sfp property.
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ci   The setup of fixed links should also be removed; these are handled
17362306a36Sopenharmony_ci   internally by phylink.
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ci   of_phy_connect() was also passed a function pointer for link updates.
17662306a36Sopenharmony_ci   This function is replaced by a different form of MAC updates
17762306a36Sopenharmony_ci   described below in (8).
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ci   Manipulation of the PHY's supported/advertised happens within phylink
18062306a36Sopenharmony_ci   based on the validate callback, see below in (8).
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci   Note that the driver no longer needs to store the ``phy_interface``,
18362306a36Sopenharmony_ci   and also note that ``phy_interface`` becomes a dynamic property,
18462306a36Sopenharmony_ci   just like the speed, duplex etc. settings.
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_ci   Finally, note that the MAC driver has no direct access to the PHY
18762306a36Sopenharmony_ci   anymore; that is because in the phylink model, the PHY can be
18862306a36Sopenharmony_ci   dynamic.
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_ci8. Add a :c:type:`struct phylink_mac_ops <phylink_mac_ops>` instance to
19162306a36Sopenharmony_ci   the driver, which is a table of function pointers, and implement
19262306a36Sopenharmony_ci   these functions. The old link update function for
19362306a36Sopenharmony_ci   :c:func:`of_phy_connect` becomes three methods: :c:func:`mac_link_up`,
19462306a36Sopenharmony_ci   :c:func:`mac_link_down`, and :c:func:`mac_config`. If step 1 was
19562306a36Sopenharmony_ci   performed, then the functionality will have been split there.
19662306a36Sopenharmony_ci
19762306a36Sopenharmony_ci   It is important that if in-band negotiation is used,
19862306a36Sopenharmony_ci   :c:func:`mac_link_up` and :c:func:`mac_link_down` do not prevent the
19962306a36Sopenharmony_ci   in-band negotiation from completing, since these functions are called
20062306a36Sopenharmony_ci   when the in-band link state changes - otherwise the link will never
20162306a36Sopenharmony_ci   come up.
20262306a36Sopenharmony_ci
20362306a36Sopenharmony_ci   The :c:func:`validate` method should mask the supplied supported mask,
20462306a36Sopenharmony_ci   and ``state->advertising`` with the supported ethtool link modes.
20562306a36Sopenharmony_ci   These are the new ethtool link modes, so bitmask operations must be
20662306a36Sopenharmony_ci   used. For an example, see ``drivers/net/ethernet/marvell/mvneta.c``.
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_ci   The :c:func:`mac_link_state` method is used to read the link state
20962306a36Sopenharmony_ci   from the MAC, and report back the settings that the MAC is currently
21062306a36Sopenharmony_ci   using. This is particularly important for in-band negotiation
21162306a36Sopenharmony_ci   methods such as 1000base-X and SGMII.
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ci   The :c:func:`mac_link_up` method is used to inform the MAC that the
21462306a36Sopenharmony_ci   link has come up. The call includes the negotiation mode and interface
21562306a36Sopenharmony_ci   for reference only. The finalised link parameters are also supplied
21662306a36Sopenharmony_ci   (speed, duplex and flow control/pause enablement settings) which
21762306a36Sopenharmony_ci   should be used to configure the MAC when the MAC and PCS are not
21862306a36Sopenharmony_ci   tightly integrated, or when the settings are not coming from in-band
21962306a36Sopenharmony_ci   negotiation.
22062306a36Sopenharmony_ci
22162306a36Sopenharmony_ci   The :c:func:`mac_config` method is used to update the MAC with the
22262306a36Sopenharmony_ci   requested state, and must avoid unnecessarily taking the link down
22362306a36Sopenharmony_ci   when making changes to the MAC configuration.  This means the
22462306a36Sopenharmony_ci   function should modify the state and only take the link down when
22562306a36Sopenharmony_ci   absolutely necessary to change the MAC configuration.  An example
22662306a36Sopenharmony_ci   of how to do this can be found in :c:func:`mvneta_mac_config` in
22762306a36Sopenharmony_ci   ``drivers/net/ethernet/marvell/mvneta.c``.
22862306a36Sopenharmony_ci
22962306a36Sopenharmony_ci   For further information on these methods, please see the inline
23062306a36Sopenharmony_ci   documentation in :c:type:`struct phylink_mac_ops <phylink_mac_ops>`.
23162306a36Sopenharmony_ci
23262306a36Sopenharmony_ci9. Remove calls to of_parse_phandle() for the PHY,
23362306a36Sopenharmony_ci   of_phy_register_fixed_link() for fixed links etc. from the probe
23462306a36Sopenharmony_ci   function, and replace with:
23562306a36Sopenharmony_ci
23662306a36Sopenharmony_ci   .. code-block:: c
23762306a36Sopenharmony_ci
23862306a36Sopenharmony_ci	struct phylink *phylink;
23962306a36Sopenharmony_ci	priv->phylink_config.dev = &dev.dev;
24062306a36Sopenharmony_ci	priv->phylink_config.type = PHYLINK_NETDEV;
24162306a36Sopenharmony_ci
24262306a36Sopenharmony_ci	phylink = phylink_create(&priv->phylink_config, node, phy_mode, &phylink_ops);
24362306a36Sopenharmony_ci	if (IS_ERR(phylink)) {
24462306a36Sopenharmony_ci		err = PTR_ERR(phylink);
24562306a36Sopenharmony_ci		fail probe;
24662306a36Sopenharmony_ci	}
24762306a36Sopenharmony_ci
24862306a36Sopenharmony_ci	priv->phylink = phylink;
24962306a36Sopenharmony_ci
25062306a36Sopenharmony_ci   and arrange to destroy the phylink in the probe failure path as
25162306a36Sopenharmony_ci   appropriate and the removal path too by calling:
25262306a36Sopenharmony_ci
25362306a36Sopenharmony_ci   .. code-block:: c
25462306a36Sopenharmony_ci
25562306a36Sopenharmony_ci	phylink_destroy(priv->phylink);
25662306a36Sopenharmony_ci
25762306a36Sopenharmony_ci10. Arrange for MAC link state interrupts to be forwarded into
25862306a36Sopenharmony_ci    phylink, via:
25962306a36Sopenharmony_ci
26062306a36Sopenharmony_ci    .. code-block:: c
26162306a36Sopenharmony_ci
26262306a36Sopenharmony_ci	phylink_mac_change(priv->phylink, link_is_up);
26362306a36Sopenharmony_ci
26462306a36Sopenharmony_ci    where ``link_is_up`` is true if the link is currently up or false
26562306a36Sopenharmony_ci    otherwise. If a MAC is unable to provide these interrupts, then
26662306a36Sopenharmony_ci    it should set ``priv->phylink_config.pcs_poll = true;`` in step 9.
26762306a36Sopenharmony_ci
26862306a36Sopenharmony_ci11. Verify that the driver does not call::
26962306a36Sopenharmony_ci
27062306a36Sopenharmony_ci	netif_carrier_on()
27162306a36Sopenharmony_ci	netif_carrier_off()
27262306a36Sopenharmony_ci
27362306a36Sopenharmony_ci   as these will interfere with phylink's tracking of the link state,
27462306a36Sopenharmony_ci   and cause phylink to omit calls via the :c:func:`mac_link_up` and
27562306a36Sopenharmony_ci   :c:func:`mac_link_down` methods.
27662306a36Sopenharmony_ci
27762306a36Sopenharmony_ciNetwork drivers should call phylink_stop() and phylink_start() via their
27862306a36Sopenharmony_cisuspend/resume paths, which ensures that the appropriate
27962306a36Sopenharmony_ci:c:type:`struct phylink_mac_ops <phylink_mac_ops>` methods are called
28062306a36Sopenharmony_cias necessary.
28162306a36Sopenharmony_ci
28262306a36Sopenharmony_ciFor information describing the SFP cage in DT, please see the binding
28362306a36Sopenharmony_cidocumentation in the kernel source tree
28462306a36Sopenharmony_ci``Documentation/devicetree/bindings/net/sff,sfp.yaml``.
285