Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0+
  2/* Microchip Sparx5 Switch driver
  3 *
  4 * Copyright (c) 2021 Microchip Technology Inc. and its subsidiaries.
  5 */
  6
  7#include <linux/module.h>
  8#include <linux/phylink.h>
  9#include <linux/device.h>
 10#include <linux/netdevice.h>
 11#include <linux/sfp.h>
 12
 13#include "sparx5_main_regs.h"
 14#include "sparx5_main.h"
 15#include "sparx5_port.h"
 16
 17static bool port_conf_has_changed(struct sparx5_port_config *a, struct sparx5_port_config *b)
 18{
 19	if (a->speed != b->speed ||
 20	    a->portmode != b->portmode ||
 21	    a->autoneg != b->autoneg ||
 22	    a->pause_adv != b->pause_adv ||
 23	    a->power_down != b->power_down ||
 24	    a->media != b->media)
 25		return true;
 26	return false;
 27}
 28
 29static struct phylink_pcs *
 30sparx5_phylink_mac_select_pcs(struct phylink_config *config,
 31			      phy_interface_t interface)
 32{
 33	struct sparx5_port *port = netdev_priv(to_net_dev(config->dev));
 
 34
 35	return &port->phylink_pcs;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 36}
 37
 38static void sparx5_phylink_mac_config(struct phylink_config *config,
 39				      unsigned int mode,
 40				      const struct phylink_link_state *state)
 41{
 42	/* Currently not used */
 43}
 44
 45static void sparx5_phylink_mac_link_up(struct phylink_config *config,
 46				       struct phy_device *phy,
 47				       unsigned int mode,
 48				       phy_interface_t interface,
 49				       int speed, int duplex,
 50				       bool tx_pause, bool rx_pause)
 51{
 52	struct sparx5_port *port = netdev_priv(to_net_dev(config->dev));
 53	struct sparx5_port_config conf;
 54	int err;
 55
 56	conf = port->conf;
 57	conf.duplex = duplex;
 58	conf.pause = 0;
 59	conf.pause |= tx_pause ? MLO_PAUSE_TX : 0;
 60	conf.pause |= rx_pause ? MLO_PAUSE_RX : 0;
 61	conf.speed = speed;
 62	/* Configure the port to speed/duplex/pause */
 63	err = sparx5_port_config(port->sparx5, port, &conf);
 64	if (err)
 65		netdev_err(port->ndev, "port config failed: %d\n", err);
 66}
 67
 68static void sparx5_phylink_mac_link_down(struct phylink_config *config,
 69					 unsigned int mode,
 70					 phy_interface_t interface)
 71{
 72	/* Currently not used */
 73}
 74
 75static struct sparx5_port *sparx5_pcs_to_port(struct phylink_pcs *pcs)
 76{
 77	return container_of(pcs, struct sparx5_port, phylink_pcs);
 78}
 79
 80static void sparx5_pcs_get_state(struct phylink_pcs *pcs,
 81				 struct phylink_link_state *state)
 82{
 83	struct sparx5_port *port = sparx5_pcs_to_port(pcs);
 84	struct sparx5_port_status status;
 85
 86	sparx5_get_port_status(port->sparx5, port, &status);
 87	state->link = status.link && !status.link_down;
 88	state->an_complete = status.an_complete;
 89	state->speed = status.speed;
 90	state->duplex = status.duplex;
 91	state->pause = status.pause;
 92}
 93
 94static int sparx5_pcs_config(struct phylink_pcs *pcs, unsigned int neg_mode,
 
 95			     phy_interface_t interface,
 96			     const unsigned long *advertising,
 97			     bool permit_pause_to_mac)
 98{
 99	struct sparx5_port *port = sparx5_pcs_to_port(pcs);
100	struct sparx5_port_config conf;
101	int ret = 0;
102
103	conf = port->conf;
104	conf.power_down = false;
105	conf.portmode = interface;
106	conf.inband = neg_mode == PHYLINK_PCS_NEG_INBAND_DISABLED ||
107		      neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED;
108	conf.autoneg = neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED;
109	conf.pause_adv = 0;
110	if (phylink_test(advertising, Pause))
111		conf.pause_adv |= ADVERTISE_1000XPAUSE;
112	if (phylink_test(advertising, Asym_Pause))
113		conf.pause_adv |= ADVERTISE_1000XPSE_ASYM;
114	if (sparx5_is_baser(interface)) {
115		if (phylink_test(advertising, FIBRE))
116			conf.media = PHY_MEDIA_SR;
117		else
118			conf.media = PHY_MEDIA_DAC;
119	}
120	if (!port_conf_has_changed(&port->conf, &conf))
121		return ret;
122	/* Enable the PCS matching this interface type */
123	ret = sparx5_port_pcs_set(port->sparx5, port, &conf);
124	if (ret)
125		netdev_err(port->ndev, "port PCS config failed: %d\n", ret);
126	return ret;
127}
128
129static void sparx5_pcs_aneg_restart(struct phylink_pcs *pcs)
130{
131	/* Currently not used */
132}
133
134const struct phylink_pcs_ops sparx5_phylink_pcs_ops = {
135	.pcs_get_state = sparx5_pcs_get_state,
136	.pcs_config = sparx5_pcs_config,
137	.pcs_an_restart = sparx5_pcs_aneg_restart,
138};
139
140const struct phylink_mac_ops sparx5_phylink_mac_ops = {
141	.mac_select_pcs = sparx5_phylink_mac_select_pcs,
142	.mac_config = sparx5_phylink_mac_config,
143	.mac_link_down = sparx5_phylink_mac_link_down,
144	.mac_link_up = sparx5_phylink_mac_link_up,
145};
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0+
  2/* Microchip Sparx5 Switch driver
  3 *
  4 * Copyright (c) 2021 Microchip Technology Inc. and its subsidiaries.
  5 */
  6
  7#include <linux/module.h>
  8#include <linux/phylink.h>
  9#include <linux/device.h>
 10#include <linux/netdevice.h>
 11#include <linux/sfp.h>
 12
 13#include "sparx5_main_regs.h"
 14#include "sparx5_main.h"
 15#include "sparx5_port.h"
 16
 17static bool port_conf_has_changed(struct sparx5_port_config *a, struct sparx5_port_config *b)
 18{
 19	if (a->speed != b->speed ||
 20	    a->portmode != b->portmode ||
 21	    a->autoneg != b->autoneg ||
 22	    a->pause_adv != b->pause_adv ||
 23	    a->power_down != b->power_down ||
 24	    a->media != b->media)
 25		return true;
 26	return false;
 27}
 28
 29static void sparx5_phylink_validate(struct phylink_config *config,
 30				    unsigned long *supported,
 31				    struct phylink_link_state *state)
 32{
 33	struct sparx5_port *port = netdev_priv(to_net_dev(config->dev));
 34	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
 35
 36	phylink_set(mask, Autoneg);
 37	phylink_set_port_modes(mask);
 38	phylink_set(mask, Pause);
 39	phylink_set(mask, Asym_Pause);
 40
 41	switch (state->interface) {
 42	case PHY_INTERFACE_MODE_5GBASER:
 43	case PHY_INTERFACE_MODE_10GBASER:
 44	case PHY_INTERFACE_MODE_25GBASER:
 45	case PHY_INTERFACE_MODE_NA:
 46		if (port->conf.bandwidth == SPEED_5000)
 47			phylink_set(mask, 5000baseT_Full);
 48		if (port->conf.bandwidth == SPEED_10000) {
 49			phylink_set(mask, 5000baseT_Full);
 50			phylink_set(mask, 10000baseT_Full);
 51			phylink_set(mask, 10000baseCR_Full);
 52			phylink_set(mask, 10000baseSR_Full);
 53			phylink_set(mask, 10000baseLR_Full);
 54			phylink_set(mask, 10000baseLRM_Full);
 55			phylink_set(mask, 10000baseER_Full);
 56		}
 57		if (port->conf.bandwidth == SPEED_25000) {
 58			phylink_set(mask, 5000baseT_Full);
 59			phylink_set(mask, 10000baseT_Full);
 60			phylink_set(mask, 10000baseCR_Full);
 61			phylink_set(mask, 10000baseSR_Full);
 62			phylink_set(mask, 10000baseLR_Full);
 63			phylink_set(mask, 10000baseLRM_Full);
 64			phylink_set(mask, 10000baseER_Full);
 65			phylink_set(mask, 25000baseCR_Full);
 66			phylink_set(mask, 25000baseSR_Full);
 67		}
 68		if (state->interface != PHY_INTERFACE_MODE_NA)
 69			break;
 70		fallthrough;
 71	case PHY_INTERFACE_MODE_SGMII:
 72	case PHY_INTERFACE_MODE_QSGMII:
 73		phylink_set(mask, 10baseT_Half);
 74		phylink_set(mask, 10baseT_Full);
 75		phylink_set(mask, 100baseT_Half);
 76		phylink_set(mask, 100baseT_Full);
 77		phylink_set(mask, 1000baseT_Full);
 78		phylink_set(mask, 1000baseX_Full);
 79		if (state->interface != PHY_INTERFACE_MODE_NA)
 80			break;
 81		fallthrough;
 82	case PHY_INTERFACE_MODE_1000BASEX:
 83	case PHY_INTERFACE_MODE_2500BASEX:
 84		if (state->interface != PHY_INTERFACE_MODE_2500BASEX) {
 85			phylink_set(mask, 1000baseT_Full);
 86			phylink_set(mask, 1000baseX_Full);
 87		}
 88		if (state->interface == PHY_INTERFACE_MODE_2500BASEX ||
 89		    state->interface == PHY_INTERFACE_MODE_NA) {
 90			phylink_set(mask, 2500baseT_Full);
 91			phylink_set(mask, 2500baseX_Full);
 92		}
 93		break;
 94	default:
 95		bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
 96		return;
 97	}
 98	bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS);
 99	bitmap_and(state->advertising, state->advertising, mask,
100		   __ETHTOOL_LINK_MODE_MASK_NBITS);
101}
102
103static void sparx5_phylink_mac_config(struct phylink_config *config,
104				      unsigned int mode,
105				      const struct phylink_link_state *state)
106{
107	/* Currently not used */
108}
109
110static void sparx5_phylink_mac_link_up(struct phylink_config *config,
111				       struct phy_device *phy,
112				       unsigned int mode,
113				       phy_interface_t interface,
114				       int speed, int duplex,
115				       bool tx_pause, bool rx_pause)
116{
117	struct sparx5_port *port = netdev_priv(to_net_dev(config->dev));
118	struct sparx5_port_config conf;
119	int err;
120
121	conf = port->conf;
122	conf.duplex = duplex;
123	conf.pause = 0;
124	conf.pause |= tx_pause ? MLO_PAUSE_TX : 0;
125	conf.pause |= rx_pause ? MLO_PAUSE_RX : 0;
126	conf.speed = speed;
127	/* Configure the port to speed/duplex/pause */
128	err = sparx5_port_config(port->sparx5, port, &conf);
129	if (err)
130		netdev_err(port->ndev, "port config failed: %d\n", err);
131}
132
133static void sparx5_phylink_mac_link_down(struct phylink_config *config,
134					 unsigned int mode,
135					 phy_interface_t interface)
136{
137	/* Currently not used */
138}
139
140static struct sparx5_port *sparx5_pcs_to_port(struct phylink_pcs *pcs)
141{
142	return container_of(pcs, struct sparx5_port, phylink_pcs);
143}
144
145static void sparx5_pcs_get_state(struct phylink_pcs *pcs,
146				 struct phylink_link_state *state)
147{
148	struct sparx5_port *port = sparx5_pcs_to_port(pcs);
149	struct sparx5_port_status status;
150
151	sparx5_get_port_status(port->sparx5, port, &status);
152	state->link = status.link && !status.link_down;
153	state->an_complete = status.an_complete;
154	state->speed = status.speed;
155	state->duplex = status.duplex;
156	state->pause = status.pause;
157}
158
159static int sparx5_pcs_config(struct phylink_pcs *pcs,
160			     unsigned int mode,
161			     phy_interface_t interface,
162			     const unsigned long *advertising,
163			     bool permit_pause_to_mac)
164{
165	struct sparx5_port *port = sparx5_pcs_to_port(pcs);
166	struct sparx5_port_config conf;
167	int ret = 0;
168
169	conf = port->conf;
170	conf.power_down = false;
171	conf.portmode = interface;
172	conf.inband = phylink_autoneg_inband(mode);
173	conf.autoneg = phylink_test(advertising, Autoneg);
 
174	conf.pause_adv = 0;
175	if (phylink_test(advertising, Pause))
176		conf.pause_adv |= ADVERTISE_1000XPAUSE;
177	if (phylink_test(advertising, Asym_Pause))
178		conf.pause_adv |= ADVERTISE_1000XPSE_ASYM;
179	if (sparx5_is_baser(interface)) {
180		if (phylink_test(advertising, FIBRE))
181			conf.media = PHY_MEDIA_SR;
182		else
183			conf.media = PHY_MEDIA_DAC;
184	}
185	if (!port_conf_has_changed(&port->conf, &conf))
186		return ret;
187	/* Enable the PCS matching this interface type */
188	ret = sparx5_port_pcs_set(port->sparx5, port, &conf);
189	if (ret)
190		netdev_err(port->ndev, "port PCS config failed: %d\n", ret);
191	return ret;
192}
193
194static void sparx5_pcs_aneg_restart(struct phylink_pcs *pcs)
195{
196	/* Currently not used */
197}
198
199const struct phylink_pcs_ops sparx5_phylink_pcs_ops = {
200	.pcs_get_state = sparx5_pcs_get_state,
201	.pcs_config = sparx5_pcs_config,
202	.pcs_an_restart = sparx5_pcs_aneg_restart,
203};
204
205const struct phylink_mac_ops sparx5_phylink_mac_ops = {
206	.validate = sparx5_phylink_validate,
207	.mac_config = sparx5_phylink_mac_config,
208	.mac_link_down = sparx5_phylink_mac_link_down,
209	.mac_link_up = sparx5_phylink_mac_link_up,
210};