aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid S. Miller <davem@davemloft.net>2017-08-06 23:55:29 -0400
committerDavid S. Miller <davem@davemloft.net>2017-08-06 23:55:29 -0400
commit234709336b8484a1e7f32710a3f06bc3f54dd9c4 (patch)
tree9f11642c4ca76d2ad62e1ec290492da6be0a13c5
parent21e27f2d8652522ca0ef35918e8d24b329149a73 (diff)
parent73970055450eebc6fc36fd170e56cc45889d0093 (diff)
Merge branch 'phylink-and-sfp-support'
Russell King says: ==================== phylink and sfp support This patch series introduces generic support for SFP sockets found on various Marvell based platforms. The idea here is to provide common SFP socket support which can be re-used by network drivers as appropriate, rather than each network driver having to re-implement SFP socket support. SFP sockets typically use other system resources, eg, I2C buses to read identifying information, and GPIOs to monitor socket state and control the socket. Meanwhile, some network drivers drive multiple ethernet ports from one instantiation of the driver. It is not desirable to block the initialisation of a network driver (thus denying other ports from being operational) if the resources for the SFP socket are not yet available. This means that an element of independence between the SFP support code and the driver is required. More than that, SFP modules effectively bring hotplug PHYs to networking - SFP copper modules normally contain a standard PHY accessed over the I2C bus, and it is desirable to read their state so network drivers can be appropriately configured. To add to the complexity, SFP modules can be connected in at least two places: 1. Directly to the serdes output of a MAC with no intervening PHY. For example: mvneta ----> SFP socket 2. To a PHY, for example: mvpp2 ---> PHY ---> copper | `-----> SFP socket This code supports both setups, although it's not fully implemented with scenario (2). Moreover, the link presented by the SFP module can be one of the 10Gbase-R family (for SFP+ sockets), SGMII or 1000base-X (for SFP sockets) depending on the module, and network drivers need to reconfigure themselves accordingly for the link to come up. For example, if the MAC is configured for SGMII and a fibre module is plugged in, the link won't come up until the MAC is reconfigured for 1000base-X mode. The SFP code manages the SFP socket - detecting the module, reading the identifying information, and managing the control and status signals. Importantly, it disables the SFP module transmitter when the MAC is down, so that the laser is turned off (but that is not a guarantee.) phylink provides the mechanisms necessary to manage the link modes, based on the SFP module type, and supports hot-plugging of the PHY without needing the MAC driver to be brought up and down on transitions. phylink also supports the classical static PHY and fixed-link modes. I currently (but not included in this series) have code to convert mvneta to use phylink, and the out of tree mvpp2x driver. I have nothing for the mvpp2 driver at present as that driver is only recently becoming functional on 10G hardware, and is missing a lot of features that are necessary to make things work correctly. ==================== Signed-off-by: David S. Miller <davem@davemloft.net>
-rw-r--r--drivers/net/phy/Kconfig25
-rw-r--r--drivers/net/phy/Makefile6
-rw-r--r--drivers/net/phy/mdio-i2c.c109
-rw-r--r--drivers/net/phy/mdio-i2c.h19
-rw-r--r--drivers/net/phy/phy-core.c180
-rw-r--r--drivers/net/phy/phy.c217
-rw-r--r--drivers/net/phy/phy_device.c15
-rw-r--r--drivers/net/phy/phylink.c1462
-rw-r--r--drivers/net/phy/sfp-bus.c475
-rw-r--r--drivers/net/phy/sfp.c915
-rw-r--r--drivers/net/phy/sfp.h28
-rw-r--r--include/linux/phy.h21
-rw-r--r--include/linux/phylink.h148
-rw-r--r--include/linux/sfp.h434
14 files changed, 3867 insertions, 187 deletions
diff --git a/drivers/net/phy/Kconfig b/drivers/net/phy/Kconfig
index 928fd892f167..bf73969a9d2b 100644
--- a/drivers/net/phy/Kconfig
+++ b/drivers/net/phy/Kconfig
@@ -106,6 +106,16 @@ config MDIO_HISI_FEMAC
106 This module provides a driver for the MDIO busses found in the 106 This module provides a driver for the MDIO busses found in the
107 Hisilicon SoC that have an Fast Ethernet MAC. 107 Hisilicon SoC that have an Fast Ethernet MAC.
108 108
109config MDIO_I2C
110 tristate
111 depends on I2C
112 help
113 Support I2C based PHYs. This provides a MDIO bus bridged
114 to I2C to allow PHYs connected in I2C mode to be accessed
115 using the existing infrastructure.
116
117 This is library mode.
118
109config MDIO_MOXART 119config MDIO_MOXART
110 tristate "MOXA ART MDIO interface support" 120 tristate "MOXA ART MDIO interface support"
111 depends on ARCH_MOXART 121 depends on ARCH_MOXART
@@ -159,6 +169,16 @@ menuconfig PHYLIB
159 devices. This option provides infrastructure for 169 devices. This option provides infrastructure for
160 managing PHY devices. 170 managing PHY devices.
161 171
172config PHYLINK
173 tristate
174 depends on NETDEVICES
175 select PHYLIB
176 select SWPHY
177 help
178 PHYlink models the link between the PHY and MAC, allowing fixed
179 configuration links, PHYs, and Serdes links with MAC level
180 autonegotiation modes.
181
162if PHYLIB 182if PHYLIB
163 183
164config SWPHY 184config SWPHY
@@ -180,6 +200,11 @@ config LED_TRIGGER_PHY
180 200
181comment "MII PHY device drivers" 201comment "MII PHY device drivers"
182 202
203config SFP
204 tristate "SFP cage support"
205 depends on I2C && PHYLINK
206 select MDIO_I2C
207
183config AMD_PHY 208config AMD_PHY
184 tristate "AMD PHYs" 209 tristate "AMD PHYs"
185 ---help--- 210 ---help---
diff --git a/drivers/net/phy/Makefile b/drivers/net/phy/Makefile
index 8e9b9f349384..7237255bad68 100644
--- a/drivers/net/phy/Makefile
+++ b/drivers/net/phy/Makefile
@@ -18,6 +18,7 @@ endif
18libphy-$(CONFIG_SWPHY) += swphy.o 18libphy-$(CONFIG_SWPHY) += swphy.o
19libphy-$(CONFIG_LED_TRIGGER_PHY) += phy_led_triggers.o 19libphy-$(CONFIG_LED_TRIGGER_PHY) += phy_led_triggers.o
20 20
21obj-$(CONFIG_PHYLINK) += phylink.o
21obj-$(CONFIG_PHYLIB) += libphy.o 22obj-$(CONFIG_PHYLIB) += libphy.o
22 23
23obj-$(CONFIG_MDIO_BCM_IPROC) += mdio-bcm-iproc.o 24obj-$(CONFIG_MDIO_BCM_IPROC) += mdio-bcm-iproc.o
@@ -30,12 +31,17 @@ obj-$(CONFIG_MDIO_BUS_MUX_MMIOREG) += mdio-mux-mmioreg.o
30obj-$(CONFIG_MDIO_CAVIUM) += mdio-cavium.o 31obj-$(CONFIG_MDIO_CAVIUM) += mdio-cavium.o
31obj-$(CONFIG_MDIO_GPIO) += mdio-gpio.o 32obj-$(CONFIG_MDIO_GPIO) += mdio-gpio.o
32obj-$(CONFIG_MDIO_HISI_FEMAC) += mdio-hisi-femac.o 33obj-$(CONFIG_MDIO_HISI_FEMAC) += mdio-hisi-femac.o
34obj-$(CONFIG_MDIO_I2C) += mdio-i2c.o
33obj-$(CONFIG_MDIO_MOXART) += mdio-moxart.o 35obj-$(CONFIG_MDIO_MOXART) += mdio-moxart.o
34obj-$(CONFIG_MDIO_OCTEON) += mdio-octeon.o 36obj-$(CONFIG_MDIO_OCTEON) += mdio-octeon.o
35obj-$(CONFIG_MDIO_SUN4I) += mdio-sun4i.o 37obj-$(CONFIG_MDIO_SUN4I) += mdio-sun4i.o
36obj-$(CONFIG_MDIO_THUNDER) += mdio-thunder.o 38obj-$(CONFIG_MDIO_THUNDER) += mdio-thunder.o
37obj-$(CONFIG_MDIO_XGENE) += mdio-xgene.o 39obj-$(CONFIG_MDIO_XGENE) += mdio-xgene.o
38 40
41obj-$(CONFIG_SFP) += sfp.o
42sfp-obj-$(CONFIG_SFP) += sfp-bus.o
43obj-y += $(sfp-obj-y) $(sfp-obj-m)
44
39obj-$(CONFIG_AMD_PHY) += amd.o 45obj-$(CONFIG_AMD_PHY) += amd.o
40obj-$(CONFIG_AQUANTIA_PHY) += aquantia.o 46obj-$(CONFIG_AQUANTIA_PHY) += aquantia.o
41obj-$(CONFIG_AT803X_PHY) += at803x.o 47obj-$(CONFIG_AT803X_PHY) += at803x.o
diff --git a/drivers/net/phy/mdio-i2c.c b/drivers/net/phy/mdio-i2c.c
new file mode 100644
index 000000000000..6d24fd13ca86
--- /dev/null
+++ b/drivers/net/phy/mdio-i2c.c
@@ -0,0 +1,109 @@
1/*
2 * MDIO I2C bridge
3 *
4 * Copyright (C) 2015-2016 Russell King
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * Network PHYs can appear on I2C buses when they are part of SFP module.
11 * This driver exposes these PHYs to the networking PHY code, allowing
12 * our PHY drivers access to these PHYs, and so allowing configuration
13 * of their settings.
14 */
15#include <linux/i2c.h>
16#include <linux/phy.h>
17
18#include "mdio-i2c.h"
19
20/*
21 * I2C bus addresses 0x50 and 0x51 are normally an EEPROM, which is
22 * specified to be present in SFP modules. These correspond with PHY
23 * addresses 16 and 17. Disallow access to these "phy" addresses.
24 */
25static bool i2c_mii_valid_phy_id(int phy_id)
26{
27 return phy_id != 0x10 && phy_id != 0x11;
28}
29
30static unsigned int i2c_mii_phy_addr(int phy_id)
31{
32 return phy_id + 0x40;
33}
34
35static int i2c_mii_read(struct mii_bus *bus, int phy_id, int reg)
36{
37 struct i2c_adapter *i2c = bus->priv;
38 struct i2c_msg msgs[2];
39 u8 data[2], dev_addr = reg;
40 int bus_addr, ret;
41
42 if (!i2c_mii_valid_phy_id(phy_id))
43 return 0xffff;
44
45 bus_addr = i2c_mii_phy_addr(phy_id);
46 msgs[0].addr = bus_addr;
47 msgs[0].flags = 0;
48 msgs[0].len = 1;
49 msgs[0].buf = &dev_addr;
50 msgs[1].addr = bus_addr;
51 msgs[1].flags = I2C_M_RD;
52 msgs[1].len = sizeof(data);
53 msgs[1].buf = data;
54
55 ret = i2c_transfer(i2c, msgs, ARRAY_SIZE(msgs));
56 if (ret != ARRAY_SIZE(msgs))
57 return 0xffff;
58
59 return data[0] << 8 | data[1];
60}
61
62static int i2c_mii_write(struct mii_bus *bus, int phy_id, int reg, u16 val)
63{
64 struct i2c_adapter *i2c = bus->priv;
65 struct i2c_msg msg;
66 int ret;
67 u8 data[3];
68
69 if (!i2c_mii_valid_phy_id(phy_id))
70 return 0;
71
72 data[0] = reg;
73 data[1] = val >> 8;
74 data[2] = val;
75
76 msg.addr = i2c_mii_phy_addr(phy_id);
77 msg.flags = 0;
78 msg.len = 3;
79 msg.buf = data;
80
81 ret = i2c_transfer(i2c, &msg, 1);
82
83 return ret < 0 ? ret : 0;
84}
85
86struct mii_bus *mdio_i2c_alloc(struct device *parent, struct i2c_adapter *i2c)
87{
88 struct mii_bus *mii;
89
90 if (!i2c_check_functionality(i2c, I2C_FUNC_I2C))
91 return ERR_PTR(-EINVAL);
92
93 mii = mdiobus_alloc();
94 if (!mii)
95 return ERR_PTR(-ENOMEM);
96
97 snprintf(mii->id, MII_BUS_ID_SIZE, "i2c:%s", dev_name(parent));
98 mii->parent = parent;
99 mii->read = i2c_mii_read;
100 mii->write = i2c_mii_write;
101 mii->priv = i2c;
102
103 return mii;
104}
105EXPORT_SYMBOL_GPL(mdio_i2c_alloc);
106
107MODULE_AUTHOR("Russell King");
108MODULE_DESCRIPTION("MDIO I2C bridge library");
109MODULE_LICENSE("GPL v2");
diff --git a/drivers/net/phy/mdio-i2c.h b/drivers/net/phy/mdio-i2c.h
new file mode 100644
index 000000000000..889ab57d7f3e
--- /dev/null
+++ b/drivers/net/phy/mdio-i2c.h
@@ -0,0 +1,19 @@
1/*
2 * MDIO I2C bridge
3 *
4 * Copyright (C) 2015 Russell King
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10#ifndef MDIO_I2C_H
11#define MDIO_I2C_H
12
13struct device;
14struct i2c_adapter;
15struct mii_bus;
16
17struct mii_bus *mdio_i2c_alloc(struct device *parent, struct i2c_adapter *i2c);
18
19#endif
diff --git a/drivers/net/phy/phy-core.c b/drivers/net/phy/phy-core.c
index 6739b738bbaf..21f75ae244b3 100644
--- a/drivers/net/phy/phy-core.c
+++ b/drivers/net/phy/phy-core.c
@@ -9,6 +9,186 @@
9#include <linux/export.h> 9#include <linux/export.h>
10#include <linux/phy.h> 10#include <linux/phy.h>
11 11
12const char *phy_speed_to_str(int speed)
13{
14 switch (speed) {
15 case SPEED_10:
16 return "10Mbps";
17 case SPEED_100:
18 return "100Mbps";
19 case SPEED_1000:
20 return "1Gbps";
21 case SPEED_2500:
22 return "2.5Gbps";
23 case SPEED_5000:
24 return "5Gbps";
25 case SPEED_10000:
26 return "10Gbps";
27 case SPEED_14000:
28 return "14Gbps";
29 case SPEED_20000:
30 return "20Gbps";
31 case SPEED_25000:
32 return "25Gbps";
33 case SPEED_40000:
34 return "40Gbps";
35 case SPEED_50000:
36 return "50Gbps";
37 case SPEED_56000:
38 return "56Gbps";
39 case SPEED_100000:
40 return "100Gbps";
41 case SPEED_UNKNOWN:
42 return "Unknown";
43 default:
44 return "Unsupported (update phy-core.c)";
45 }
46}
47EXPORT_SYMBOL_GPL(phy_speed_to_str);
48
49const char *phy_duplex_to_str(unsigned int duplex)
50{
51 if (duplex == DUPLEX_HALF)
52 return "Half";
53 if (duplex == DUPLEX_FULL)
54 return "Full";
55 if (duplex == DUPLEX_UNKNOWN)
56 return "Unknown";
57 return "Unsupported (update phy-core.c)";
58}
59EXPORT_SYMBOL_GPL(phy_duplex_to_str);
60
61/* A mapping of all SUPPORTED settings to speed/duplex. This table
62 * must be grouped by speed and sorted in descending match priority
63 * - iow, descending speed. */
64static const struct phy_setting settings[] = {
65 {
66 .speed = SPEED_10000,
67 .duplex = DUPLEX_FULL,
68 .bit = ETHTOOL_LINK_MODE_10000baseKR_Full_BIT,
69 },
70 {
71 .speed = SPEED_10000,
72 .duplex = DUPLEX_FULL,
73 .bit = ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT,
74 },
75 {
76 .speed = SPEED_10000,
77 .duplex = DUPLEX_FULL,
78 .bit = ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
79 },
80 {
81 .speed = SPEED_2500,
82 .duplex = DUPLEX_FULL,
83 .bit = ETHTOOL_LINK_MODE_2500baseX_Full_BIT,
84 },
85 {
86 .speed = SPEED_1000,
87 .duplex = DUPLEX_FULL,
88 .bit = ETHTOOL_LINK_MODE_1000baseKX_Full_BIT,
89 },
90 {
91 .speed = SPEED_1000,
92 .duplex = DUPLEX_FULL,
93 .bit = ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
94 },
95 {
96 .speed = SPEED_1000,
97 .duplex = DUPLEX_FULL,
98 .bit = ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
99 },
100 {
101 .speed = SPEED_1000,
102 .duplex = DUPLEX_HALF,
103 .bit = ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
104 },
105 {
106 .speed = SPEED_100,
107 .duplex = DUPLEX_FULL,
108 .bit = ETHTOOL_LINK_MODE_100baseT_Full_BIT,
109 },
110 {
111 .speed = SPEED_100,
112 .duplex = DUPLEX_HALF,
113 .bit = ETHTOOL_LINK_MODE_100baseT_Half_BIT,
114 },
115 {
116 .speed = SPEED_10,
117 .duplex = DUPLEX_FULL,
118 .bit = ETHTOOL_LINK_MODE_10baseT_Full_BIT,
119 },
120 {
121 .speed = SPEED_10,
122 .duplex = DUPLEX_HALF,
123 .bit = ETHTOOL_LINK_MODE_10baseT_Half_BIT,
124 },
125};
126
127/**
128 * phy_lookup_setting - lookup a PHY setting
129 * @speed: speed to match
130 * @duplex: duplex to match
131 * @mask: allowed link modes
132 * @maxbit: bit size of link modes
133 * @exact: an exact match is required
134 *
135 * Search the settings array for a setting that matches the speed and
136 * duplex, and which is supported.
137 *
138 * If @exact is unset, either an exact match or %NULL for no match will
139 * be returned.
140 *
141 * If @exact is set, an exact match, the fastest supported setting at
142 * or below the specified speed, the slowest supported setting, or if
143 * they all fail, %NULL will be returned.
144 */
145const struct phy_setting *
146phy_lookup_setting(int speed, int duplex, const unsigned long *mask,
147 size_t maxbit, bool exact)
148{
149 const struct phy_setting *p, *match = NULL, *last = NULL;
150 int i;
151
152 for (i = 0, p = settings; i < ARRAY_SIZE(settings); i++, p++) {
153 if (p->bit < maxbit && test_bit(p->bit, mask)) {
154 last = p;
155 if (p->speed == speed && p->duplex == duplex) {
156 /* Exact match for speed and duplex */
157 match = p;
158 break;
159 } else if (!exact) {
160 if (!match && p->speed <= speed)
161 /* Candidate */
162 match = p;
163
164 if (p->speed < speed)
165 break;
166 }
167 }
168 }
169
170 if (!match && !exact)
171 match = last;
172
173 return match;
174}
175EXPORT_SYMBOL_GPL(phy_lookup_setting);
176
177size_t phy_speeds(unsigned int *speeds, size_t size,
178 unsigned long *mask, size_t maxbit)
179{
180 size_t count;
181 int i;
182
183 for (i = 0, count = 0; i < ARRAY_SIZE(settings) && count < size; i++)
184 if (settings[i].bit < maxbit &&
185 test_bit(settings[i].bit, mask) &&
186 (count == 0 || speeds[count - 1] != settings[i].speed))
187 speeds[count++] = settings[i].speed;
188
189 return count;
190}
191
12static void mmd_phy_indirect(struct mii_bus *bus, int phy_addr, int devad, 192static void mmd_phy_indirect(struct mii_bus *bus, int phy_addr, int devad,
13 u16 regnum) 193 u16 regnum)
14{ 194{
diff --git a/drivers/net/phy/phy.c b/drivers/net/phy/phy.c
index b9d4922581de..dae13f028c84 100644
--- a/drivers/net/phy/phy.c
+++ b/drivers/net/phy/phy.c
@@ -38,42 +38,6 @@
38 38
39#include <asm/irq.h> 39#include <asm/irq.h>
40 40
41static const char *phy_speed_to_str(int speed)
42{
43 switch (speed) {
44 case SPEED_10:
45 return "10Mbps";
46 case SPEED_100:
47 return "100Mbps";
48 case SPEED_1000:
49 return "1Gbps";
50 case SPEED_2500:
51 return "2.5Gbps";
52 case SPEED_5000:
53 return "5Gbps";
54 case SPEED_10000:
55 return "10Gbps";
56 case SPEED_14000:
57 return "14Gbps";
58 case SPEED_20000:
59 return "20Gbps";
60 case SPEED_25000:
61 return "25Gbps";
62 case SPEED_40000:
63 return "40Gbps";
64 case SPEED_50000:
65 return "50Gbps";
66 case SPEED_56000:
67 return "56Gbps";
68 case SPEED_100000:
69 return "100Gbps";
70 case SPEED_UNKNOWN:
71 return "Unknown";
72 default:
73 return "Unsupported (update phy.c)";
74 }
75}
76
77#define PHY_STATE_STR(_state) \ 41#define PHY_STATE_STR(_state) \
78 case PHY_##_state: \ 42 case PHY_##_state: \
79 return __stringify(_state); \ 43 return __stringify(_state); \
@@ -109,7 +73,7 @@ void phy_print_status(struct phy_device *phydev)
109 netdev_info(phydev->attached_dev, 73 netdev_info(phydev->attached_dev,
110 "Link is Up - %s/%s - flow control %s\n", 74 "Link is Up - %s/%s - flow control %s\n",
111 phy_speed_to_str(phydev->speed), 75 phy_speed_to_str(phydev->speed),
112 DUPLEX_FULL == phydev->duplex ? "Full" : "Half", 76 phy_duplex_to_str(phydev->duplex),
113 phydev->pause ? "rx/tx" : "off"); 77 phydev->pause ? "rx/tx" : "off");
114 } else { 78 } else {
115 netdev_info(phydev->attached_dev, "Link is Down\n"); 79 netdev_info(phydev->attached_dev, "Link is Down\n");
@@ -193,123 +157,6 @@ int phy_aneg_done(struct phy_device *phydev)
193} 157}
194EXPORT_SYMBOL(phy_aneg_done); 158EXPORT_SYMBOL(phy_aneg_done);
195 159
196/* A structure for mapping a particular speed and duplex
197 * combination to a particular SUPPORTED and ADVERTISED value
198 */
199struct phy_setting {
200 int speed;
201 int duplex;
202 u32 setting;
203};
204
205/* A mapping of all SUPPORTED settings to speed/duplex. This table
206 * must be grouped by speed and sorted in descending match priority
207 * - iow, descending speed. */
208static const struct phy_setting settings[] = {
209 {
210 .speed = SPEED_10000,
211 .duplex = DUPLEX_FULL,
212 .setting = SUPPORTED_10000baseKR_Full,
213 },
214 {
215 .speed = SPEED_10000,
216 .duplex = DUPLEX_FULL,
217 .setting = SUPPORTED_10000baseKX4_Full,
218 },
219 {
220 .speed = SPEED_10000,
221 .duplex = DUPLEX_FULL,
222 .setting = SUPPORTED_10000baseT_Full,
223 },
224 {
225 .speed = SPEED_2500,
226 .duplex = DUPLEX_FULL,
227 .setting = SUPPORTED_2500baseX_Full,
228 },
229 {
230 .speed = SPEED_1000,
231 .duplex = DUPLEX_FULL,
232 .setting = SUPPORTED_1000baseKX_Full,
233 },
234 {
235 .speed = SPEED_1000,
236 .duplex = DUPLEX_FULL,
237 .setting = SUPPORTED_1000baseT_Full,
238 },
239 {
240 .speed = SPEED_1000,
241 .duplex = DUPLEX_HALF,
242 .setting = SUPPORTED_1000baseT_Half,
243 },
244 {
245 .speed = SPEED_100,
246 .duplex = DUPLEX_FULL,
247 .setting = SUPPORTED_100baseT_Full,
248 },
249 {
250 .speed = SPEED_100,
251 .duplex = DUPLEX_HALF,
252 .setting = SUPPORTED_100baseT_Half,
253 },
254 {
255 .speed = SPEED_10,
256 .duplex = DUPLEX_FULL,
257 .setting = SUPPORTED_10baseT_Full,
258 },
259 {
260 .speed = SPEED_10,
261 .duplex = DUPLEX_HALF,
262 .setting = SUPPORTED_10baseT_Half,
263 },
264};
265
266/**
267 * phy_lookup_setting - lookup a PHY setting
268 * @speed: speed to match
269 * @duplex: duplex to match
270 * @features: allowed link modes
271 * @exact: an exact match is required
272 *
273 * Search the settings array for a setting that matches the speed and
274 * duplex, and which is supported.
275 *
276 * If @exact is unset, either an exact match or %NULL for no match will
277 * be returned.
278 *
279 * If @exact is set, an exact match, the fastest supported setting at
280 * or below the specified speed, the slowest supported setting, or if
281 * they all fail, %NULL will be returned.
282 */
283static const struct phy_setting *
284phy_lookup_setting(int speed, int duplex, u32 features, bool exact)
285{
286 const struct phy_setting *p, *match = NULL, *last = NULL;
287 int i;
288
289 for (i = 0, p = settings; i < ARRAY_SIZE(settings); i++, p++) {
290 if (p->setting & features) {
291 last = p;
292 if (p->speed == speed && p->duplex == duplex) {
293 /* Exact match for speed and duplex */
294 match = p;
295 break;
296 } else if (!exact) {
297 if (!match && p->speed <= speed)
298 /* Candidate */
299 match = p;
300
301 if (p->speed < speed)
302 break;
303 }
304 }
305 }
306
307 if (!match && !exact)
308 match = last;
309
310 return match;
311}
312
313/** 160/**
314 * phy_find_valid - find a PHY setting that matches the requested parameters 161 * phy_find_valid - find a PHY setting that matches the requested parameters
315 * @speed: desired speed 162 * @speed: desired speed
@@ -326,7 +173,9 @@ phy_lookup_setting(int speed, int duplex, u32 features, bool exact)
326static const struct phy_setting * 173static const struct phy_setting *
327phy_find_valid(int speed, int duplex, u32 supported) 174phy_find_valid(int speed, int duplex, u32 supported)
328{ 175{
329 return phy_lookup_setting(speed, duplex, supported, false); 176 unsigned long mask = supported;
177
178 return phy_lookup_setting(speed, duplex, &mask, BITS_PER_LONG, false);
330} 179}
331 180
332/** 181/**
@@ -343,16 +192,9 @@ unsigned int phy_supported_speeds(struct phy_device *phy,
343 unsigned int *speeds, 192 unsigned int *speeds,
344 unsigned int size) 193 unsigned int size)
345{ 194{
346 unsigned int count = 0; 195 unsigned long supported = phy->supported;
347 unsigned int idx = 0;
348 196
349 for (idx = 0; idx < ARRAY_SIZE(settings) && count < size; idx++) 197 return phy_speeds(speeds, size, &supported, BITS_PER_LONG);
350 /* Assumes settings are grouped by speed */
351 if ((settings[idx].setting & phy->supported) &&
352 (count == 0 || speeds[count - 1] != settings[idx].speed))
353 speeds[count++] = settings[idx].speed;
354
355 return count;
356} 198}
357 199
358/** 200/**
@@ -366,7 +208,9 @@ unsigned int phy_supported_speeds(struct phy_device *phy,
366 */ 208 */
367static inline bool phy_check_valid(int speed, int duplex, u32 features) 209static inline bool phy_check_valid(int speed, int duplex, u32 features)
368{ 210{
369 return !!phy_lookup_setting(speed, duplex, features, true); 211 unsigned long mask = features;
212
213 return !!phy_lookup_setting(speed, duplex, &mask, BITS_PER_LONG, true);
370} 214}
371 215
372/** 216/**
@@ -712,6 +556,7 @@ void phy_start_machine(struct phy_device *phydev)
712{ 556{
713 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, HZ); 557 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, HZ);
714} 558}
559EXPORT_SYMBOL_GPL(phy_start_machine);
715 560
716/** 561/**
717 * phy_trigger_machine - trigger the state machine to run 562 * phy_trigger_machine - trigger the state machine to run
@@ -1021,9 +866,15 @@ void phy_start(struct phy_device *phydev)
1021} 866}
1022EXPORT_SYMBOL(phy_start); 867EXPORT_SYMBOL(phy_start);
1023 868
1024static void phy_adjust_link(struct phy_device *phydev) 869static void phy_link_up(struct phy_device *phydev)
1025{ 870{
1026 phydev->adjust_link(phydev->attached_dev); 871 phydev->phy_link_change(phydev, true, true);
872 phy_led_trigger_change_speed(phydev);
873}
874
875static void phy_link_down(struct phy_device *phydev, bool do_carrier)
876{
877 phydev->phy_link_change(phydev, false, do_carrier);
1027 phy_led_trigger_change_speed(phydev); 878 phy_led_trigger_change_speed(phydev);
1028} 879}
1029 880
@@ -1068,8 +919,7 @@ void phy_state_machine(struct work_struct *work)
1068 /* If the link is down, give up on negotiation for now */ 919 /* If the link is down, give up on negotiation for now */
1069 if (!phydev->link) { 920 if (!phydev->link) {
1070 phydev->state = PHY_NOLINK; 921 phydev->state = PHY_NOLINK;
1071 netif_carrier_off(phydev->attached_dev); 922 phy_link_down(phydev, true);
1072 phy_adjust_link(phydev);
1073 break; 923 break;
1074 } 924 }
1075 925
@@ -1081,9 +931,7 @@ void phy_state_machine(struct work_struct *work)
1081 /* If AN is done, we're running */ 931 /* If AN is done, we're running */
1082 if (err > 0) { 932 if (err > 0) {
1083 phydev->state = PHY_RUNNING; 933 phydev->state = PHY_RUNNING;
1084 netif_carrier_on(phydev->attached_dev); 934 phy_link_up(phydev);
1085 phy_adjust_link(phydev);
1086
1087 } else if (0 == phydev->link_timeout--) 935 } else if (0 == phydev->link_timeout--)
1088 needs_aneg = true; 936 needs_aneg = true;
1089 break; 937 break;
@@ -1108,8 +956,7 @@ void phy_state_machine(struct work_struct *work)
1108 } 956 }
1109 } 957 }
1110 phydev->state = PHY_RUNNING; 958 phydev->state = PHY_RUNNING;
1111 netif_carrier_on(phydev->attached_dev); 959 phy_link_up(phydev);
1112 phy_adjust_link(phydev);
1113 } 960 }
1114 break; 961 break;
1115 case PHY_FORCING: 962 case PHY_FORCING:
@@ -1119,13 +966,12 @@ void phy_state_machine(struct work_struct *work)
1119 966
1120 if (phydev->link) { 967 if (phydev->link) {
1121 phydev->state = PHY_RUNNING; 968 phydev->state = PHY_RUNNING;
1122 netif_carrier_on(phydev->attached_dev); 969 phy_link_up(phydev);
1123 } else { 970 } else {
1124 if (0 == phydev->link_timeout--) 971 if (0 == phydev->link_timeout--)
1125 needs_aneg = true; 972 needs_aneg = true;
973 phy_link_down(phydev, false);
1126 } 974 }
1127
1128 phy_adjust_link(phydev);
1129 break; 975 break;
1130 case PHY_RUNNING: 976 case PHY_RUNNING:
1131 /* Only register a CHANGE if we are polling and link changed 977 /* Only register a CHANGE if we are polling and link changed
@@ -1157,14 +1003,12 @@ void phy_state_machine(struct work_struct *work)
1157 1003
1158 if (phydev->link) { 1004 if (phydev->link) {
1159 phydev->state = PHY_RUNNING; 1005 phydev->state = PHY_RUNNING;
1160 netif_carrier_on(phydev->attached_dev); 1006 phy_link_up(phydev);
1161 } else { 1007 } else {
1162 phydev->state = PHY_NOLINK; 1008 phydev->state = PHY_NOLINK;
1163 netif_carrier_off(phydev->attached_dev); 1009 phy_link_down(phydev, true);
1164 } 1010 }
1165 1011
1166 phy_adjust_link(phydev);
1167
1168 if (phy_interrupt_is_valid(phydev)) 1012 if (phy_interrupt_is_valid(phydev))
1169 err = phy_config_interrupt(phydev, 1013 err = phy_config_interrupt(phydev,
1170 PHY_INTERRUPT_ENABLED); 1014 PHY_INTERRUPT_ENABLED);
@@ -1172,8 +1016,7 @@ void phy_state_machine(struct work_struct *work)
1172 case PHY_HALTED: 1016 case PHY_HALTED:
1173 if (phydev->link) { 1017 if (phydev->link) {
1174 phydev->link = 0; 1018 phydev->link = 0;
1175 netif_carrier_off(phydev->attached_dev); 1019 phy_link_down(phydev, true);
1176 phy_adjust_link(phydev);
1177 do_suspend = true; 1020 do_suspend = true;
1178 } 1021 }
1179 break; 1022 break;
@@ -1193,11 +1036,11 @@ void phy_state_machine(struct work_struct *work)
1193 1036
1194 if (phydev->link) { 1037 if (phydev->link) {
1195 phydev->state = PHY_RUNNING; 1038 phydev->state = PHY_RUNNING;
1196 netif_carrier_on(phydev->attached_dev); 1039 phy_link_up(phydev);
1197 } else { 1040 } else {
1198 phydev->state = PHY_NOLINK; 1041 phydev->state = PHY_NOLINK;
1042 phy_link_down(phydev, false);
1199 } 1043 }
1200 phy_adjust_link(phydev);
1201 } else { 1044 } else {
1202 phydev->state = PHY_AN; 1045 phydev->state = PHY_AN;
1203 phydev->link_timeout = PHY_AN_TIMEOUT; 1046 phydev->link_timeout = PHY_AN_TIMEOUT;
@@ -1209,11 +1052,11 @@ void phy_state_machine(struct work_struct *work)
1209 1052
1210 if (phydev->link) { 1053 if (phydev->link) {
1211 phydev->state = PHY_RUNNING; 1054 phydev->state = PHY_RUNNING;
1212 netif_carrier_on(phydev->attached_dev); 1055 phy_link_up(phydev);
1213 } else { 1056 } else {
1214 phydev->state = PHY_NOLINK; 1057 phydev->state = PHY_NOLINK;
1058 phy_link_down(phydev, false);
1215 } 1059 }
1216 phy_adjust_link(phydev);
1217 } 1060 }
1218 break; 1061 break;
1219 } 1062 }
diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
index 1790f7fec125..9493fb369682 100644
--- a/drivers/net/phy/phy_device.c
+++ b/drivers/net/phy/phy_device.c
@@ -688,6 +688,19 @@ struct phy_device *phy_find_first(struct mii_bus *bus)
688} 688}
689EXPORT_SYMBOL(phy_find_first); 689EXPORT_SYMBOL(phy_find_first);
690 690
691static void phy_link_change(struct phy_device *phydev, bool up, bool do_carrier)
692{
693 struct net_device *netdev = phydev->attached_dev;
694
695 if (do_carrier) {
696 if (up)
697 netif_carrier_on(netdev);
698 else
699 netif_carrier_off(netdev);
700 }
701 phydev->adjust_link(netdev);
702}
703
691/** 704/**
692 * phy_prepare_link - prepares the PHY layer to monitor link status 705 * phy_prepare_link - prepares the PHY layer to monitor link status
693 * @phydev: target phy_device struct 706 * @phydev: target phy_device struct
@@ -951,6 +964,7 @@ int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
951 goto error; 964 goto error;
952 } 965 }
953 966
967 phydev->phy_link_change = phy_link_change;
954 phydev->attached_dev = dev; 968 phydev->attached_dev = dev;
955 dev->phydev = phydev; 969 dev->phydev = phydev;
956 970
@@ -1070,6 +1084,7 @@ void phy_detach(struct phy_device *phydev)
1070 phydev->attached_dev->phydev = NULL; 1084 phydev->attached_dev->phydev = NULL;
1071 phydev->attached_dev = NULL; 1085 phydev->attached_dev = NULL;
1072 phy_suspend(phydev); 1086 phy_suspend(phydev);
1087 phydev->phylink = NULL;
1073 1088
1074 phy_led_triggers_unregister(phydev); 1089 phy_led_triggers_unregister(phydev);
1075 1090
diff --git a/drivers/net/phy/phylink.c b/drivers/net/phy/phylink.c
new file mode 100644
index 000000000000..32917bdd1432
--- /dev/null
+++ b/drivers/net/phy/phylink.c
@@ -0,0 +1,1462 @@
1/*
2 * phylink models the MAC to optional PHY connection, supporting
3 * technologies such as SFP cages where the PHY is hot-pluggable.
4 *
5 * Copyright (C) 2015 Russell King
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#include <linux/ethtool.h>
12#include <linux/export.h>
13#include <linux/gpio/consumer.h>
14#include <linux/netdevice.h>
15#include <linux/of.h>
16#include <linux/of_mdio.h>
17#include <linux/phy.h>
18#include <linux/phy_fixed.h>
19#include <linux/phylink.h>
20#include <linux/rtnetlink.h>
21#include <linux/spinlock.h>
22#include <linux/workqueue.h>
23
24#include "sfp.h"
25#include "swphy.h"
26
27#define SUPPORTED_INTERFACES \
28 (SUPPORTED_TP | SUPPORTED_MII | SUPPORTED_FIBRE | \
29 SUPPORTED_BNC | SUPPORTED_AUI | SUPPORTED_Backplane)
30#define ADVERTISED_INTERFACES \
31 (ADVERTISED_TP | ADVERTISED_MII | ADVERTISED_FIBRE | \
32 ADVERTISED_BNC | ADVERTISED_AUI | ADVERTISED_Backplane)
33
34enum {
35 PHYLINK_DISABLE_STOPPED,
36 PHYLINK_DISABLE_LINK,
37};
38
39struct phylink {
40 struct net_device *netdev;
41 const struct phylink_mac_ops *ops;
42
43 unsigned long phylink_disable_state; /* bitmask of disables */
44 struct phy_device *phydev;
45 phy_interface_t link_interface; /* PHY_INTERFACE_xxx */
46 u8 link_an_mode; /* MLO_AN_xxx */
47 u8 link_port; /* The current non-phy ethtool port */
48 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
49
50 /* The link configuration settings */
51 struct phylink_link_state link_config;
52 struct gpio_desc *link_gpio;
53
54 struct mutex state_mutex;
55 struct phylink_link_state phy_state;
56 struct work_struct resolve;
57
58 bool mac_link_dropped;
59
60 struct sfp_bus *sfp_bus;
61};
62
63static inline void linkmode_zero(unsigned long *dst)
64{
65 bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS);
66}
67
68static inline void linkmode_copy(unsigned long *dst, const unsigned long *src)
69{
70 bitmap_copy(dst, src, __ETHTOOL_LINK_MODE_MASK_NBITS);
71}
72
73static inline void linkmode_and(unsigned long *dst, const unsigned long *a,
74 const unsigned long *b)
75{
76 bitmap_and(dst, a, b, __ETHTOOL_LINK_MODE_MASK_NBITS);
77}
78
79static inline void linkmode_or(unsigned long *dst, const unsigned long *a,
80 const unsigned long *b)
81{
82 bitmap_or(dst, a, b, __ETHTOOL_LINK_MODE_MASK_NBITS);
83}
84
85static inline bool linkmode_empty(const unsigned long *src)
86{
87 return bitmap_empty(src, __ETHTOOL_LINK_MODE_MASK_NBITS);
88}
89
90void phylink_set_port_modes(unsigned long *mask)
91{
92 phylink_set(mask, TP);
93 phylink_set(mask, AUI);
94 phylink_set(mask, MII);
95 phylink_set(mask, FIBRE);
96 phylink_set(mask, BNC);
97 phylink_set(mask, Backplane);
98}
99EXPORT_SYMBOL_GPL(phylink_set_port_modes);
100
101static int phylink_is_empty_linkmode(const unsigned long *linkmode)
102{
103 __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp) = { 0, };
104
105 phylink_set_port_modes(tmp);
106 phylink_set(tmp, Autoneg);
107 phylink_set(tmp, Pause);
108 phylink_set(tmp, Asym_Pause);
109
110 bitmap_andnot(tmp, linkmode, tmp, __ETHTOOL_LINK_MODE_MASK_NBITS);
111
112 return linkmode_empty(tmp);
113}
114
115static const char *phylink_an_mode_str(unsigned int mode)
116{
117 static const char *modestr[] = {
118 [MLO_AN_PHY] = "phy",
119 [MLO_AN_FIXED] = "fixed",
120 [MLO_AN_SGMII] = "SGMII",
121 [MLO_AN_8023Z] = "802.3z",
122 };
123
124 return mode < ARRAY_SIZE(modestr) ? modestr[mode] : "unknown";
125}
126
127static int phylink_validate(struct phylink *pl, unsigned long *supported,
128 struct phylink_link_state *state)
129{
130 pl->ops->validate(pl->netdev, supported, state);
131
132 return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
133}
134
135static int phylink_parse_fixedlink(struct phylink *pl, struct device_node *np)
136{
137 struct device_node *fixed_node;
138 const struct phy_setting *s;
139 struct gpio_desc *desc;
140 const __be32 *fixed_prop;
141 u32 speed;
142 int ret, len;
143
144 fixed_node = of_get_child_by_name(np, "fixed-link");
145 if (fixed_node) {
146 ret = of_property_read_u32(fixed_node, "speed", &speed);
147
148 pl->link_config.speed = speed;
149 pl->link_config.duplex = DUPLEX_HALF;
150
151 if (of_property_read_bool(fixed_node, "full-duplex"))
152 pl->link_config.duplex = DUPLEX_FULL;
153
154 /* We treat the "pause" and "asym-pause" terminology as
155 * defining the link partner's ability. */
156 if (of_property_read_bool(fixed_node, "pause"))
157 pl->link_config.pause |= MLO_PAUSE_SYM;
158 if (of_property_read_bool(fixed_node, "asym-pause"))
159 pl->link_config.pause |= MLO_PAUSE_ASYM;
160
161 if (ret == 0) {
162 desc = fwnode_get_named_gpiod(&fixed_node->fwnode,
163 "link-gpios", 0,
164 GPIOD_IN, "?");
165
166 if (!IS_ERR(desc))
167 pl->link_gpio = desc;
168 else if (desc == ERR_PTR(-EPROBE_DEFER))
169 ret = -EPROBE_DEFER;
170 }
171 of_node_put(fixed_node);
172
173 if (ret)
174 return ret;
175 } else {
176 fixed_prop = of_get_property(np, "fixed-link", &len);
177 if (!fixed_prop) {
178 netdev_err(pl->netdev, "broken fixed-link?\n");
179 return -EINVAL;
180 }
181 if (len == 5 * sizeof(*fixed_prop)) {
182 pl->link_config.duplex = be32_to_cpu(fixed_prop[1]) ?
183 DUPLEX_FULL : DUPLEX_HALF;
184 pl->link_config.speed = be32_to_cpu(fixed_prop[2]);
185 if (be32_to_cpu(fixed_prop[3]))
186 pl->link_config.pause |= MLO_PAUSE_SYM;
187 if (be32_to_cpu(fixed_prop[4]))
188 pl->link_config.pause |= MLO_PAUSE_ASYM;
189 }
190 }
191
192 if (pl->link_config.speed > SPEED_1000 &&
193 pl->link_config.duplex != DUPLEX_FULL)
194 netdev_warn(pl->netdev, "fixed link specifies half duplex for %dMbps link?\n",
195 pl->link_config.speed);
196
197 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
198 linkmode_copy(pl->link_config.advertising, pl->supported);
199 phylink_validate(pl, pl->supported, &pl->link_config);
200
201 s = phy_lookup_setting(pl->link_config.speed, pl->link_config.duplex,
202 pl->supported,
203 __ETHTOOL_LINK_MODE_MASK_NBITS, true);
204 linkmode_zero(pl->supported);
205 phylink_set(pl->supported, MII);
206 if (s) {
207 __set_bit(s->bit, pl->supported);
208 } else {
209 netdev_warn(pl->netdev, "fixed link %s duplex %dMbps not recognised\n",
210 pl->link_config.duplex == DUPLEX_FULL ? "full" : "half",
211 pl->link_config.speed);
212 }
213
214 linkmode_and(pl->link_config.advertising, pl->link_config.advertising,
215 pl->supported);
216
217 pl->link_config.link = 1;
218 pl->link_config.an_complete = 1;
219
220 return 0;
221}
222
223static int phylink_parse_mode(struct phylink *pl, struct device_node *np)
224{
225 struct device_node *dn;
226 const char *managed;
227
228 dn = of_get_child_by_name(np, "fixed-link");
229 if (dn || of_find_property(np, "fixed-link", NULL))
230 pl->link_an_mode = MLO_AN_FIXED;
231 of_node_put(dn);
232
233 if (of_property_read_string(np, "managed", &managed) == 0 &&
234 strcmp(managed, "in-band-status") == 0) {
235 if (pl->link_an_mode == MLO_AN_FIXED) {
236 netdev_err(pl->netdev,
237 "can't use both fixed-link and in-band-status\n");
238 return -EINVAL;
239 }
240
241 linkmode_zero(pl->supported);
242 phylink_set(pl->supported, MII);
243 phylink_set(pl->supported, Autoneg);
244 phylink_set(pl->supported, Asym_Pause);
245 phylink_set(pl->supported, Pause);
246 pl->link_config.an_enabled = true;
247
248 switch (pl->link_config.interface) {
249 case PHY_INTERFACE_MODE_SGMII:
250 phylink_set(pl->supported, 10baseT_Half);
251 phylink_set(pl->supported, 10baseT_Full);
252 phylink_set(pl->supported, 100baseT_Half);
253 phylink_set(pl->supported, 100baseT_Full);
254 phylink_set(pl->supported, 1000baseT_Half);
255 phylink_set(pl->supported, 1000baseT_Full);
256 pl->link_an_mode = MLO_AN_SGMII;
257 break;
258
259 case PHY_INTERFACE_MODE_1000BASEX:
260 phylink_set(pl->supported, 1000baseX_Full);
261 pl->link_an_mode = MLO_AN_8023Z;
262 break;
263
264 case PHY_INTERFACE_MODE_2500BASEX:
265 phylink_set(pl->supported, 2500baseX_Full);
266 pl->link_an_mode = MLO_AN_8023Z;
267 break;
268
269 case PHY_INTERFACE_MODE_10GKR:
270 phylink_set(pl->supported, 10baseT_Half);
271 phylink_set(pl->supported, 10baseT_Full);
272 phylink_set(pl->supported, 100baseT_Half);
273 phylink_set(pl->supported, 100baseT_Full);
274 phylink_set(pl->supported, 1000baseT_Half);
275 phylink_set(pl->supported, 1000baseT_Full);
276 phylink_set(pl->supported, 1000baseX_Full);
277 phylink_set(pl->supported, 10000baseKR_Full);
278 phylink_set(pl->supported, 10000baseCR_Full);
279 phylink_set(pl->supported, 10000baseSR_Full);
280 phylink_set(pl->supported, 10000baseLR_Full);
281 phylink_set(pl->supported, 10000baseLRM_Full);
282 phylink_set(pl->supported, 10000baseER_Full);
283 pl->link_an_mode = MLO_AN_SGMII;
284 break;
285
286 default:
287 netdev_err(pl->netdev,
288 "incorrect link mode %s for in-band status\n",
289 phy_modes(pl->link_config.interface));
290 return -EINVAL;
291 }
292
293 linkmode_copy(pl->link_config.advertising, pl->supported);
294
295 if (phylink_validate(pl, pl->supported, &pl->link_config)) {
296 netdev_err(pl->netdev,
297 "failed to validate link configuration for in-band status\n");
298 return -EINVAL;
299 }
300 }
301
302 return 0;
303}
304
305static void phylink_mac_config(struct phylink *pl,
306 const struct phylink_link_state *state)
307{
308 netdev_dbg(pl->netdev,
309 "%s: mode=%s/%s/%s/%s adv=%*pb pause=%02x link=%u an=%u\n",
310 __func__, phylink_an_mode_str(pl->link_an_mode),
311 phy_modes(state->interface),
312 phy_speed_to_str(state->speed),
313 phy_duplex_to_str(state->duplex),
314 __ETHTOOL_LINK_MODE_MASK_NBITS, state->advertising,
315 state->pause, state->link, state->an_enabled);
316
317 pl->ops->mac_config(pl->netdev, pl->link_an_mode, state);
318}
319
320static void phylink_mac_an_restart(struct phylink *pl)
321{
322 if (pl->link_config.an_enabled &&
323 (pl->link_config.interface == PHY_INTERFACE_MODE_1000BASEX ||
324 pl->link_config.interface == PHY_INTERFACE_MODE_2500BASEX))
325 pl->ops->mac_an_restart(pl->netdev);
326}
327
328static int phylink_get_mac_state(struct phylink *pl, struct phylink_link_state *state)
329{
330 struct net_device *ndev = pl->netdev;
331
332 linkmode_copy(state->advertising, pl->link_config.advertising);
333 linkmode_zero(state->lp_advertising);
334 state->interface = pl->link_config.interface;
335 state->an_enabled = pl->link_config.an_enabled;
336 state->link = 1;
337
338 return pl->ops->mac_link_state(ndev, state);
339}
340
341/* The fixed state is... fixed except for the link state,
342 * which may be determined by a GPIO.
343 */
344static void phylink_get_fixed_state(struct phylink *pl, struct phylink_link_state *state)
345{
346 *state = pl->link_config;
347 if (pl->link_gpio)
348 state->link = !!gpiod_get_value(pl->link_gpio);
349}
350
351/* Flow control is resolved according to our and the link partners
352 * advertisments using the following drawn from the 802.3 specs:
353 * Local device Link partner
354 * Pause AsymDir Pause AsymDir Result
355 * 1 X 1 X TX+RX
356 * 0 1 1 1 RX
357 * 1 1 0 1 TX
358 */
359static void phylink_resolve_flow(struct phylink *pl,
360 struct phylink_link_state *state)
361{
362 int new_pause = 0;
363
364 if (pl->link_config.pause & MLO_PAUSE_AN) {
365 int pause = 0;
366
367 if (phylink_test(pl->link_config.advertising, Pause))
368 pause |= MLO_PAUSE_SYM;
369 if (phylink_test(pl->link_config.advertising, Asym_Pause))
370 pause |= MLO_PAUSE_ASYM;
371
372 pause &= state->pause;
373
374 if (pause & MLO_PAUSE_SYM)
375 new_pause = MLO_PAUSE_TX | MLO_PAUSE_RX;
376 else if (pause & MLO_PAUSE_ASYM)
377 new_pause = state->pause & MLO_PAUSE_SYM ?
378 MLO_PAUSE_RX : MLO_PAUSE_TX;
379 } else {
380 new_pause = pl->link_config.pause & MLO_PAUSE_TXRX_MASK;
381 }
382
383 state->pause &= ~MLO_PAUSE_TXRX_MASK;
384 state->pause |= new_pause;
385}
386
387static const char *phylink_pause_to_str(int pause)
388{
389 switch (pause & MLO_PAUSE_TXRX_MASK) {
390 case MLO_PAUSE_TX | MLO_PAUSE_RX:
391 return "rx/tx";
392 case MLO_PAUSE_TX:
393 return "tx";
394 case MLO_PAUSE_RX:
395 return "rx";
396 default:
397 return "off";
398 }
399}
400
401static void phylink_resolve(struct work_struct *w)
402{
403 struct phylink *pl = container_of(w, struct phylink, resolve);
404 struct phylink_link_state link_state;
405 struct net_device *ndev = pl->netdev;
406
407 mutex_lock(&pl->state_mutex);
408 if (pl->phylink_disable_state) {
409 pl->mac_link_dropped = false;
410 link_state.link = false;
411 } else if (pl->mac_link_dropped) {
412 link_state.link = false;
413 } else {
414 switch (pl->link_an_mode) {
415 case MLO_AN_PHY:
416 link_state = pl->phy_state;
417 phylink_resolve_flow(pl, &link_state);
418 phylink_mac_config(pl, &link_state);
419 break;
420
421 case MLO_AN_FIXED:
422 phylink_get_fixed_state(pl, &link_state);
423 phylink_mac_config(pl, &link_state);
424 break;
425
426 case MLO_AN_SGMII:
427 phylink_get_mac_state(pl, &link_state);
428 if (pl->phydev) {
429 bool changed = false;
430
431 link_state.link = link_state.link &&
432 pl->phy_state.link;
433
434 if (pl->phy_state.interface !=
435 link_state.interface) {
436 link_state.interface = pl->phy_state.interface;
437 changed = true;
438 }
439
440 /* Propagate the flow control from the PHY
441 * to the MAC. Also propagate the interface
442 * if changed.
443 */
444 if (pl->phy_state.link || changed) {
445 link_state.pause |= pl->phy_state.pause;
446 phylink_resolve_flow(pl, &link_state);
447
448 phylink_mac_config(pl, &link_state);
449 }
450 }
451 break;
452
453 case MLO_AN_8023Z:
454 phylink_get_mac_state(pl, &link_state);
455 break;
456 }
457 }
458
459 if (link_state.link != netif_carrier_ok(ndev)) {
460 if (!link_state.link) {
461 netif_carrier_off(ndev);
462 pl->ops->mac_link_down(ndev, pl->link_an_mode);
463 netdev_info(ndev, "Link is Down\n");
464 } else {
465 pl->ops->mac_link_up(ndev, pl->link_an_mode,
466 pl->phydev);
467
468 netif_carrier_on(ndev);
469
470 netdev_info(ndev,
471 "Link is Up - %s/%s - flow control %s\n",
472 phy_speed_to_str(link_state.speed),
473 phy_duplex_to_str(link_state.duplex),
474 phylink_pause_to_str(link_state.pause));
475 }
476 }
477 if (!link_state.link && pl->mac_link_dropped) {
478 pl->mac_link_dropped = false;
479 queue_work(system_power_efficient_wq, &pl->resolve);
480 }
481 mutex_unlock(&pl->state_mutex);
482}
483
484static void phylink_run_resolve(struct phylink *pl)
485{
486 if (!pl->phylink_disable_state)
487 queue_work(system_power_efficient_wq, &pl->resolve);
488}
489
490static const struct sfp_upstream_ops sfp_phylink_ops;
491
492static int phylink_register_sfp(struct phylink *pl, struct device_node *np)
493{
494 struct device_node *sfp_np;
495
496 sfp_np = of_parse_phandle(np, "sfp", 0);
497 if (!sfp_np)
498 return 0;
499
500 pl->sfp_bus = sfp_register_upstream(sfp_np, pl->netdev, pl,
501 &sfp_phylink_ops);
502 if (!pl->sfp_bus)
503 return -ENOMEM;
504
505 return 0;
506}
507
508struct phylink *phylink_create(struct net_device *ndev, struct device_node *np,
509 phy_interface_t iface, const struct phylink_mac_ops *ops)
510{
511 struct phylink *pl;
512 int ret;
513
514 pl = kzalloc(sizeof(*pl), GFP_KERNEL);
515 if (!pl)
516 return ERR_PTR(-ENOMEM);
517
518 mutex_init(&pl->state_mutex);
519 INIT_WORK(&pl->resolve, phylink_resolve);
520 pl->netdev = ndev;
521 pl->phy_state.interface = iface;
522 pl->link_interface = iface;
523 pl->link_port = PORT_MII;
524 pl->link_config.interface = iface;
525 pl->link_config.pause = MLO_PAUSE_AN;
526 pl->link_config.speed = SPEED_UNKNOWN;
527 pl->link_config.duplex = DUPLEX_UNKNOWN;
528 pl->ops = ops;
529 __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
530
531 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
532 linkmode_copy(pl->link_config.advertising, pl->supported);
533 phylink_validate(pl, pl->supported, &pl->link_config);
534
535 ret = phylink_parse_mode(pl, np);
536 if (ret < 0) {
537 kfree(pl);
538 return ERR_PTR(ret);
539 }
540
541 if (pl->link_an_mode == MLO_AN_FIXED) {
542 ret = phylink_parse_fixedlink(pl, np);
543 if (ret < 0) {
544 kfree(pl);
545 return ERR_PTR(ret);
546 }
547 }
548
549 ret = phylink_register_sfp(pl, np);
550 if (ret < 0) {
551 kfree(pl);
552 return ERR_PTR(ret);
553 }
554
555 return pl;
556}
557EXPORT_SYMBOL_GPL(phylink_create);
558
559void phylink_destroy(struct phylink *pl)
560{
561 if (pl->sfp_bus)
562 sfp_unregister_upstream(pl->sfp_bus);
563
564 cancel_work_sync(&pl->resolve);
565 kfree(pl);
566}
567EXPORT_SYMBOL_GPL(phylink_destroy);
568
569void phylink_phy_change(struct phy_device *phydev, bool up, bool do_carrier)
570{
571 struct phylink *pl = phydev->phylink;
572
573 mutex_lock(&pl->state_mutex);
574 pl->phy_state.speed = phydev->speed;
575 pl->phy_state.duplex = phydev->duplex;
576 pl->phy_state.pause = MLO_PAUSE_NONE;
577 if (phydev->pause)
578 pl->phy_state.pause |= MLO_PAUSE_SYM;
579 if (phydev->asym_pause)
580 pl->phy_state.pause |= MLO_PAUSE_ASYM;
581 pl->phy_state.interface = phydev->interface;
582 pl->phy_state.link = up;
583 mutex_unlock(&pl->state_mutex);
584
585 phylink_run_resolve(pl);
586
587 netdev_dbg(pl->netdev, "phy link %s %s/%s/%s\n", up ? "up" : "down",
588 phy_modes(phydev->interface),
589 phy_speed_to_str(phydev->speed),
590 phy_duplex_to_str(phydev->duplex));
591}
592
593static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy)
594{
595 struct phylink_link_state config;
596 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
597 u32 advertising;
598 int ret;
599
600 memset(&config, 0, sizeof(config));
601 ethtool_convert_legacy_u32_to_link_mode(supported, phy->supported);
602 ethtool_convert_legacy_u32_to_link_mode(config.advertising,
603 phy->advertising);
604 config.interface = pl->link_config.interface;
605
606 /*
607 * This is the new way of dealing with flow control for PHYs,
608 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
609 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
610 * using our validate call to the MAC, we rely upon the MAC
611 * clearing the bits from both supported and advertising fields.
612 */
613 if (phylink_test(supported, Pause))
614 phylink_set(config.advertising, Pause);
615 if (phylink_test(supported, Asym_Pause))
616 phylink_set(config.advertising, Asym_Pause);
617
618 ret = phylink_validate(pl, supported, &config);
619 if (ret)
620 return ret;
621
622 phy->phylink = pl;
623 phy->phy_link_change = phylink_phy_change;
624
625 netdev_info(pl->netdev,
626 "PHY [%s] driver [%s]\n", dev_name(&phy->mdio.dev),
627 phy->drv->name);
628
629 mutex_lock(&phy->lock);
630 mutex_lock(&pl->state_mutex);
631 pl->netdev->phydev = phy;
632 pl->phydev = phy;
633 linkmode_copy(pl->supported, supported);
634 linkmode_copy(pl->link_config.advertising, config.advertising);
635
636 /* Restrict the phy advertisment according to the MAC support. */
637 ethtool_convert_link_mode_to_legacy_u32(&advertising, config.advertising);
638 phy->advertising = advertising;
639 mutex_unlock(&pl->state_mutex);
640 mutex_unlock(&phy->lock);
641
642 netdev_dbg(pl->netdev,
643 "phy: setting supported %*pb advertising 0x%08x\n",
644 __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
645 phy->advertising);
646
647 phy_start_machine(phy);
648 if (phy->irq > 0)
649 phy_start_interrupts(phy);
650
651 return 0;
652}
653
654int phylink_connect_phy(struct phylink *pl, struct phy_device *phy)
655{
656 int ret;
657
658 ret = phy_attach_direct(pl->netdev, phy, 0, pl->link_interface);
659 if (ret)
660 return ret;
661
662 ret = phylink_bringup_phy(pl, phy);
663 if (ret)
664 phy_detach(phy);
665
666 return ret;
667}
668EXPORT_SYMBOL_GPL(phylink_connect_phy);
669
670int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn)
671{
672 struct device_node *phy_node;
673 struct phy_device *phy_dev;
674 int ret;
675
676 /* Fixed links are handled without needing a PHY */
677 if (pl->link_an_mode == MLO_AN_FIXED)
678 return 0;
679
680 phy_node = of_parse_phandle(dn, "phy-handle", 0);
681 if (!phy_node)
682 phy_node = of_parse_phandle(dn, "phy", 0);
683 if (!phy_node)
684 phy_node = of_parse_phandle(dn, "phy-device", 0);
685
686 if (!phy_node) {
687 if (pl->link_an_mode == MLO_AN_PHY) {
688 netdev_err(pl->netdev, "unable to find PHY node\n");
689 return -ENODEV;
690 }
691 return 0;
692 }
693
694 phy_dev = of_phy_attach(pl->netdev, phy_node, 0, pl->link_interface);
695 /* We're done with the phy_node handle */
696 of_node_put(phy_node);
697
698 if (!phy_dev)
699 return -ENODEV;
700
701 ret = phylink_bringup_phy(pl, phy_dev);
702 if (ret)
703 phy_detach(phy_dev);
704
705 return ret;
706}
707EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
708
709void phylink_disconnect_phy(struct phylink *pl)
710{
711 struct phy_device *phy;
712
713 WARN_ON(!lockdep_rtnl_is_held());
714
715 phy = pl->phydev;
716 if (phy) {
717 mutex_lock(&phy->lock);
718 mutex_lock(&pl->state_mutex);
719 pl->netdev->phydev = NULL;
720 pl->phydev = NULL;
721 mutex_unlock(&pl->state_mutex);
722 mutex_unlock(&phy->lock);
723 flush_work(&pl->resolve);
724
725 phy_disconnect(phy);
726 }
727}
728EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
729
730void phylink_mac_change(struct phylink *pl, bool up)
731{
732 if (!up)
733 pl->mac_link_dropped = true;
734 phylink_run_resolve(pl);
735 netdev_dbg(pl->netdev, "mac link %s\n", up ? "up" : "down");
736}
737EXPORT_SYMBOL_GPL(phylink_mac_change);
738
739void phylink_start(struct phylink *pl)
740{
741 WARN_ON(!lockdep_rtnl_is_held());
742
743 netdev_info(pl->netdev, "configuring for %s/%s link mode\n",
744 phylink_an_mode_str(pl->link_an_mode),
745 phy_modes(pl->link_config.interface));
746
747 /* Apply the link configuration to the MAC when starting. This allows
748 * a fixed-link to start with the correct parameters, and also
749 * ensures that we set the appropriate advertisment for Serdes links.
750 */
751 phylink_resolve_flow(pl, &pl->link_config);
752 phylink_mac_config(pl, &pl->link_config);
753
754 clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
755 phylink_run_resolve(pl);
756
757 if (pl->sfp_bus)
758 sfp_upstream_start(pl->sfp_bus);
759 if (pl->phydev)
760 phy_start(pl->phydev);
761}
762EXPORT_SYMBOL_GPL(phylink_start);
763
764void phylink_stop(struct phylink *pl)
765{
766 WARN_ON(!lockdep_rtnl_is_held());
767
768 if (pl->phydev)
769 phy_stop(pl->phydev);
770 if (pl->sfp_bus)
771 sfp_upstream_stop(pl->sfp_bus);
772
773 set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
774 flush_work(&pl->resolve);
775}
776EXPORT_SYMBOL_GPL(phylink_stop);
777
778void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
779{
780 WARN_ON(!lockdep_rtnl_is_held());
781
782 wol->supported = 0;
783 wol->wolopts = 0;
784
785 if (pl->phydev)
786 phy_ethtool_get_wol(pl->phydev, wol);
787}
788EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol);
789
790int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
791{
792 int ret = -EOPNOTSUPP;
793
794 WARN_ON(!lockdep_rtnl_is_held());
795
796 if (pl->phydev)
797 ret = phy_ethtool_set_wol(pl->phydev, wol);
798
799 return ret;
800}
801EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol);
802
803static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b)
804{
805 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
806
807 linkmode_zero(mask);
808 phylink_set_port_modes(mask);
809
810 linkmode_and(dst, dst, mask);
811 linkmode_or(dst, dst, b);
812}
813
814static void phylink_get_ksettings(const struct phylink_link_state *state,
815 struct ethtool_link_ksettings *kset)
816{
817 phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
818 linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
819 kset->base.speed = state->speed;
820 kset->base.duplex = state->duplex;
821 kset->base.autoneg = state->an_enabled ? AUTONEG_ENABLE :
822 AUTONEG_DISABLE;
823}
824
825int phylink_ethtool_ksettings_get(struct phylink *pl,
826 struct ethtool_link_ksettings *kset)
827{
828 struct phylink_link_state link_state;
829
830 WARN_ON(!lockdep_rtnl_is_held());
831
832 if (pl->phydev) {
833 phy_ethtool_ksettings_get(pl->phydev, kset);
834 } else {
835 kset->base.port = pl->link_port;
836 }
837
838 linkmode_copy(kset->link_modes.supported, pl->supported);
839
840 switch (pl->link_an_mode) {
841 case MLO_AN_FIXED:
842 /* We are using fixed settings. Report these as the
843 * current link settings - and note that these also
844 * represent the supported speeds/duplex/pause modes.
845 */
846 phylink_get_fixed_state(pl, &link_state);
847 phylink_get_ksettings(&link_state, kset);
848 break;
849
850 case MLO_AN_SGMII:
851 /* If there is a phy attached, then use the reported
852 * settings from the phy with no modification.
853 */
854 if (pl->phydev)
855 break;
856
857 case MLO_AN_8023Z:
858 phylink_get_mac_state(pl, &link_state);
859
860 /* The MAC is reporting the link results from its own PCS
861 * layer via in-band status. Report these as the current
862 * link settings.
863 */
864 phylink_get_ksettings(&link_state, kset);
865 break;
866 }
867
868 return 0;
869}
870EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get);
871
872int phylink_ethtool_ksettings_set(struct phylink *pl,
873 const struct ethtool_link_ksettings *kset)
874{
875 struct ethtool_link_ksettings our_kset;
876 struct phylink_link_state config;
877 int ret;
878
879 WARN_ON(!lockdep_rtnl_is_held());
880
881 if (kset->base.autoneg != AUTONEG_DISABLE &&
882 kset->base.autoneg != AUTONEG_ENABLE)
883 return -EINVAL;
884
885 config = pl->link_config;
886
887 /* Mask out unsupported advertisments */
888 linkmode_and(config.advertising, kset->link_modes.advertising,
889 pl->supported);
890
891 /* FIXME: should we reject autoneg if phy/mac does not support it? */
892 if (kset->base.autoneg == AUTONEG_DISABLE) {
893 const struct phy_setting *s;
894
895 /* Autonegotiation disabled, select a suitable speed and
896 * duplex.
897 */
898 s = phy_lookup_setting(kset->base.speed, kset->base.duplex,
899 pl->supported,
900 __ETHTOOL_LINK_MODE_MASK_NBITS, false);
901 if (!s)
902 return -EINVAL;
903
904 /* If we have a fixed link (as specified by firmware), refuse
905 * to change link parameters.
906 */
907 if (pl->link_an_mode == MLO_AN_FIXED &&
908 (s->speed != pl->link_config.speed ||
909 s->duplex != pl->link_config.duplex))
910 return -EINVAL;
911
912 config.speed = s->speed;
913 config.duplex = s->duplex;
914 config.an_enabled = false;
915
916 __clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
917 } else {
918 /* If we have a fixed link, refuse to enable autonegotiation */
919 if (pl->link_an_mode == MLO_AN_FIXED)
920 return -EINVAL;
921
922 config.speed = SPEED_UNKNOWN;
923 config.duplex = DUPLEX_UNKNOWN;
924 config.an_enabled = true;
925
926 __set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
927 }
928
929 if (phylink_validate(pl, pl->supported, &config))
930 return -EINVAL;
931
932 /* If autonegotiation is enabled, we must have an advertisment */
933 if (config.an_enabled && phylink_is_empty_linkmode(config.advertising))
934 return -EINVAL;
935
936 our_kset = *kset;
937 linkmode_copy(our_kset.link_modes.advertising, config.advertising);
938 our_kset.base.speed = config.speed;
939 our_kset.base.duplex = config.duplex;
940
941 /* If we have a PHY, configure the phy */
942 if (pl->phydev) {
943 ret = phy_ethtool_ksettings_set(pl->phydev, &our_kset);
944 if (ret)
945 return ret;
946 }
947
948 mutex_lock(&pl->state_mutex);
949 /* Configure the MAC to match the new settings */
950 linkmode_copy(pl->link_config.advertising, our_kset.link_modes.advertising);
951 pl->link_config.speed = our_kset.base.speed;
952 pl->link_config.duplex = our_kset.base.duplex;
953 pl->link_config.an_enabled = our_kset.base.autoneg != AUTONEG_DISABLE;
954
955 if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
956 phylink_mac_config(pl, &pl->link_config);
957 phylink_mac_an_restart(pl);
958 }
959 mutex_unlock(&pl->state_mutex);
960
961 return ret;
962}
963EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
964
965int phylink_ethtool_nway_reset(struct phylink *pl)
966{
967 int ret = 0;
968
969 WARN_ON(!lockdep_rtnl_is_held());
970
971 if (pl->phydev)
972 ret = phy_restart_aneg(pl->phydev);
973 phylink_mac_an_restart(pl);
974
975 return ret;
976}
977EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
978
979void phylink_ethtool_get_pauseparam(struct phylink *pl,
980 struct ethtool_pauseparam *pause)
981{
982 WARN_ON(!lockdep_rtnl_is_held());
983
984 pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
985 pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
986 pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
987}
988EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
989
990int phylink_ethtool_set_pauseparam(struct phylink *pl,
991 struct ethtool_pauseparam *pause)
992{
993 struct phylink_link_state *config = &pl->link_config;
994
995 WARN_ON(!lockdep_rtnl_is_held());
996
997 if (!phylink_test(pl->supported, Pause) &&
998 !phylink_test(pl->supported, Asym_Pause))
999 return -EOPNOTSUPP;
1000
1001 if (!phylink_test(pl->supported, Asym_Pause) &&
1002 !pause->autoneg && pause->rx_pause != pause->tx_pause)
1003 return -EINVAL;
1004
1005 config->pause &= ~(MLO_PAUSE_AN | MLO_PAUSE_TXRX_MASK);
1006
1007 if (pause->autoneg)
1008 config->pause |= MLO_PAUSE_AN;
1009 if (pause->rx_pause)
1010 config->pause |= MLO_PAUSE_RX;
1011 if (pause->tx_pause)
1012 config->pause |= MLO_PAUSE_TX;
1013
1014 if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
1015 switch (pl->link_an_mode) {
1016 case MLO_AN_PHY:
1017 /* Silently mark the carrier down, and then trigger a resolve */
1018 netif_carrier_off(pl->netdev);
1019 phylink_run_resolve(pl);
1020 break;
1021
1022 case MLO_AN_FIXED:
1023 /* Should we allow fixed links to change against the config? */
1024 phylink_resolve_flow(pl, config);
1025 phylink_mac_config(pl, config);
1026 break;
1027
1028 case MLO_AN_SGMII:
1029 case MLO_AN_8023Z:
1030 phylink_mac_config(pl, config);
1031 phylink_mac_an_restart(pl);
1032 break;
1033 }
1034 }
1035
1036 return 0;
1037}
1038EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
1039
1040int phylink_ethtool_get_module_info(struct phylink *pl,
1041 struct ethtool_modinfo *modinfo)
1042{
1043 int ret = -EOPNOTSUPP;
1044
1045 WARN_ON(!lockdep_rtnl_is_held());
1046
1047 if (pl->sfp_bus)
1048 ret = sfp_get_module_info(pl->sfp_bus, modinfo);
1049
1050 return ret;
1051}
1052EXPORT_SYMBOL_GPL(phylink_ethtool_get_module_info);
1053
1054int phylink_ethtool_get_module_eeprom(struct phylink *pl,
1055 struct ethtool_eeprom *ee, u8 *buf)
1056{
1057 int ret = -EOPNOTSUPP;
1058
1059 WARN_ON(!lockdep_rtnl_is_held());
1060
1061 if (pl->sfp_bus)
1062 ret = sfp_get_module_eeprom(pl->sfp_bus, ee, buf);
1063
1064 return ret;
1065}
1066EXPORT_SYMBOL_GPL(phylink_ethtool_get_module_eeprom);
1067
1068int phylink_init_eee(struct phylink *pl, bool clk_stop_enable)
1069{
1070 int ret = -EPROTONOSUPPORT;
1071
1072 WARN_ON(!lockdep_rtnl_is_held());
1073
1074 if (pl->phydev)
1075 ret = phy_init_eee(pl->phydev, clk_stop_enable);
1076
1077 return ret;
1078}
1079EXPORT_SYMBOL_GPL(phylink_init_eee);
1080
1081int phylink_get_eee_err(struct phylink *pl)
1082{
1083 int ret = 0;
1084
1085 WARN_ON(!lockdep_rtnl_is_held());
1086
1087 if (pl->phydev)
1088 ret = phy_get_eee_err(pl->phydev);
1089
1090 return ret;
1091}
1092EXPORT_SYMBOL_GPL(phylink_get_eee_err);
1093
1094int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee)
1095{
1096 int ret = -EOPNOTSUPP;
1097
1098 WARN_ON(!lockdep_rtnl_is_held());
1099
1100 if (pl->phydev)
1101 ret = phy_ethtool_get_eee(pl->phydev, eee);
1102
1103 return ret;
1104}
1105EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
1106
1107int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_eee *eee)
1108{
1109 int ret = -EOPNOTSUPP;
1110
1111 WARN_ON(!lockdep_rtnl_is_held());
1112
1113 if (pl->phydev)
1114 ret = phy_ethtool_set_eee(pl->phydev, eee);
1115
1116 return ret;
1117}
1118EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
1119
1120/* This emulates MII registers for a fixed-mode phy operating as per the
1121 * passed in state. "aneg" defines if we report negotiation is possible.
1122 *
1123 * FIXME: should deal with negotiation state too.
1124 */
1125static int phylink_mii_emul_read(struct net_device *ndev, unsigned int reg,
1126 struct phylink_link_state *state, bool aneg)
1127{
1128 struct fixed_phy_status fs;
1129 int val;
1130
1131 fs.link = state->link;
1132 fs.speed = state->speed;
1133 fs.duplex = state->duplex;
1134 fs.pause = state->pause & MLO_PAUSE_SYM;
1135 fs.asym_pause = state->pause & MLO_PAUSE_ASYM;
1136
1137 val = swphy_read_reg(reg, &fs);
1138 if (reg == MII_BMSR) {
1139 if (!state->an_complete)
1140 val &= ~BMSR_ANEGCOMPLETE;
1141 if (!aneg)
1142 val &= ~BMSR_ANEGCAPABLE;
1143 }
1144 return val;
1145}
1146
1147static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
1148 unsigned int reg)
1149{
1150 struct phy_device *phydev = pl->phydev;
1151 int prtad, devad;
1152
1153 if (mdio_phy_id_is_c45(phy_id)) {
1154 prtad = mdio_phy_id_prtad(phy_id);
1155 devad = mdio_phy_id_devad(phy_id);
1156 devad = MII_ADDR_C45 | devad << 16 | reg;
1157 } else if (phydev->is_c45) {
1158 switch (reg) {
1159 case MII_BMCR:
1160 case MII_BMSR:
1161 case MII_PHYSID1:
1162 case MII_PHYSID2:
1163 devad = __ffs(phydev->c45_ids.devices_in_package);
1164 break;
1165 case MII_ADVERTISE:
1166 case MII_LPA:
1167 if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1168 return -EINVAL;
1169 devad = MDIO_MMD_AN;
1170 if (reg == MII_ADVERTISE)
1171 reg = MDIO_AN_ADVERTISE;
1172 else
1173 reg = MDIO_AN_LPA;
1174 break;
1175 default:
1176 return -EINVAL;
1177 }
1178 prtad = phy_id;
1179 devad = MII_ADDR_C45 | devad << 16 | reg;
1180 } else {
1181 prtad = phy_id;
1182 devad = reg;
1183 }
1184 return mdiobus_read(pl->phydev->mdio.bus, prtad, devad);
1185}
1186
1187static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
1188 unsigned int reg, unsigned int val)
1189{
1190 struct phy_device *phydev = pl->phydev;
1191 int prtad, devad;
1192
1193 if (mdio_phy_id_is_c45(phy_id)) {
1194 prtad = mdio_phy_id_prtad(phy_id);
1195 devad = mdio_phy_id_devad(phy_id);
1196 devad = MII_ADDR_C45 | devad << 16 | reg;
1197 } else if (phydev->is_c45) {
1198 switch (reg) {
1199 case MII_BMCR:
1200 case MII_BMSR:
1201 case MII_PHYSID1:
1202 case MII_PHYSID2:
1203 devad = __ffs(phydev->c45_ids.devices_in_package);
1204 break;
1205 case MII_ADVERTISE:
1206 case MII_LPA:
1207 if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1208 return -EINVAL;
1209 devad = MDIO_MMD_AN;
1210 if (reg == MII_ADVERTISE)
1211 reg = MDIO_AN_ADVERTISE;
1212 else
1213 reg = MDIO_AN_LPA;
1214 break;
1215 default:
1216 return -EINVAL;
1217 }
1218 prtad = phy_id;
1219 devad = MII_ADDR_C45 | devad << 16 | reg;
1220 } else {
1221 prtad = phy_id;
1222 devad = reg;
1223 }
1224
1225 return mdiobus_write(phydev->mdio.bus, prtad, devad, val);
1226}
1227
1228static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
1229 unsigned int reg)
1230{
1231 struct phylink_link_state state;
1232 int val = 0xffff;
1233
1234 switch (pl->link_an_mode) {
1235 case MLO_AN_FIXED:
1236 if (phy_id == 0) {
1237 phylink_get_fixed_state(pl, &state);
1238 val = phylink_mii_emul_read(pl->netdev, reg, &state,
1239 true);
1240 }
1241 break;
1242
1243 case MLO_AN_PHY:
1244 return -EOPNOTSUPP;
1245
1246 case MLO_AN_SGMII:
1247 /* No phy, fall through to 8023z method */
1248 case MLO_AN_8023Z:
1249 if (phy_id == 0) {
1250 val = phylink_get_mac_state(pl, &state);
1251 if (val < 0)
1252 return val;
1253
1254 val = phylink_mii_emul_read(pl->netdev, reg, &state,
1255 true);
1256 }
1257 break;
1258 }
1259
1260 return val & 0xffff;
1261}
1262
1263static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
1264 unsigned int reg, unsigned int val)
1265{
1266 switch (pl->link_an_mode) {
1267 case MLO_AN_FIXED:
1268 break;
1269
1270 case MLO_AN_PHY:
1271 return -EOPNOTSUPP;
1272
1273 case MLO_AN_SGMII:
1274 /* No phy, fall through to 8023z method */
1275 case MLO_AN_8023Z:
1276 break;
1277 }
1278
1279 return 0;
1280}
1281
1282int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
1283{
1284 struct mii_ioctl_data *mii = if_mii(ifr);
1285 int ret;
1286
1287 WARN_ON(!lockdep_rtnl_is_held());
1288
1289 if (pl->phydev) {
1290 /* PHYs only exist for MLO_AN_PHY and MLO_AN_SGMII */
1291 switch (cmd) {
1292 case SIOCGMIIPHY:
1293 mii->phy_id = pl->phydev->mdio.addr;
1294
1295 case SIOCGMIIREG:
1296 ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
1297 if (ret >= 0) {
1298 mii->val_out = ret;
1299 ret = 0;
1300 }
1301 break;
1302
1303 case SIOCSMIIREG:
1304 ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
1305 mii->val_in);
1306 break;
1307
1308 default:
1309 ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
1310 break;
1311 }
1312 } else {
1313 switch (cmd) {
1314 case SIOCGMIIPHY:
1315 mii->phy_id = 0;
1316
1317 case SIOCGMIIREG:
1318 ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
1319 if (ret >= 0) {
1320 mii->val_out = ret;
1321 ret = 0;
1322 }
1323 break;
1324
1325 case SIOCSMIIREG:
1326 ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
1327 mii->val_in);
1328 break;
1329
1330 default:
1331 ret = -EOPNOTSUPP;
1332 break;
1333 }
1334 }
1335
1336 return ret;
1337}
1338EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
1339
1340
1341
1342static int phylink_sfp_module_insert(void *upstream,
1343 const struct sfp_eeprom_id *id)
1344{
1345 struct phylink *pl = upstream;
1346 __ETHTOOL_DECLARE_LINK_MODE_MASK(support) = { 0, };
1347 struct phylink_link_state config;
1348 phy_interface_t iface;
1349 int mode, ret = 0;
1350 bool changed;
1351 u8 port;
1352
1353 sfp_parse_support(pl->sfp_bus, id, support);
1354 port = sfp_parse_port(pl->sfp_bus, id, support);
1355 iface = sfp_parse_interface(pl->sfp_bus, id);
1356
1357 WARN_ON(!lockdep_rtnl_is_held());
1358
1359 switch (iface) {
1360 case PHY_INTERFACE_MODE_SGMII:
1361 mode = MLO_AN_SGMII;
1362 break;
1363 case PHY_INTERFACE_MODE_1000BASEX:
1364 mode = MLO_AN_8023Z;
1365 break;
1366 default:
1367 return -EINVAL;
1368 }
1369
1370 memset(&config, 0, sizeof(config));
1371 linkmode_copy(config.advertising, support);
1372 config.interface = iface;
1373 config.speed = SPEED_UNKNOWN;
1374 config.duplex = DUPLEX_UNKNOWN;
1375 config.pause = MLO_PAUSE_AN;
1376 config.an_enabled = pl->link_config.an_enabled;
1377
1378 /* Ignore errors if we're expecting a PHY to attach later */
1379 ret = phylink_validate(pl, support, &config);
1380 if (ret) {
1381 netdev_err(pl->netdev, "validation of %s/%s with support %*pb failed: %d\n",
1382 phylink_an_mode_str(mode), phy_modes(config.interface),
1383 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
1384 return ret;
1385 }
1386
1387 netdev_dbg(pl->netdev, "requesting link mode %s/%s with support %*pb\n",
1388 phylink_an_mode_str(mode), phy_modes(config.interface),
1389 __ETHTOOL_LINK_MODE_MASK_NBITS, support);
1390
1391 if (mode == MLO_AN_8023Z && pl->phydev)
1392 return -EINVAL;
1393
1394 changed = !bitmap_equal(pl->supported, support,
1395 __ETHTOOL_LINK_MODE_MASK_NBITS);
1396 if (changed) {
1397 linkmode_copy(pl->supported, support);
1398 linkmode_copy(pl->link_config.advertising, config.advertising);
1399 }
1400
1401 if (pl->link_an_mode != mode ||
1402 pl->link_config.interface != config.interface) {
1403 pl->link_config.interface = config.interface;
1404 pl->link_an_mode = mode;
1405
1406 changed = true;
1407
1408 netdev_info(pl->netdev, "switched to %s/%s link mode\n",
1409 phylink_an_mode_str(mode),
1410 phy_modes(config.interface));
1411 }
1412
1413 pl->link_port = port;
1414
1415 if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
1416 &pl->phylink_disable_state))
1417 phylink_mac_config(pl, &pl->link_config);
1418
1419 return ret;
1420}
1421
1422static void phylink_sfp_link_down(void *upstream)
1423{
1424 struct phylink *pl = upstream;
1425
1426 WARN_ON(!lockdep_rtnl_is_held());
1427
1428 set_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
1429 flush_work(&pl->resolve);
1430
1431 netif_carrier_off(pl->netdev);
1432}
1433
1434static void phylink_sfp_link_up(void *upstream)
1435{
1436 struct phylink *pl = upstream;
1437
1438 WARN_ON(!lockdep_rtnl_is_held());
1439
1440 clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
1441 phylink_run_resolve(pl);
1442}
1443
1444static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
1445{
1446 return phylink_connect_phy(upstream, phy);
1447}
1448
1449static void phylink_sfp_disconnect_phy(void *upstream)
1450{
1451 phylink_disconnect_phy(upstream);
1452}
1453
1454static const struct sfp_upstream_ops sfp_phylink_ops = {
1455 .module_insert = phylink_sfp_module_insert,
1456 .link_up = phylink_sfp_link_up,
1457 .link_down = phylink_sfp_link_down,
1458 .connect_phy = phylink_sfp_connect_phy,
1459 .disconnect_phy = phylink_sfp_disconnect_phy,
1460};
1461
1462MODULE_LICENSE("GPL");
diff --git a/drivers/net/phy/sfp-bus.c b/drivers/net/phy/sfp-bus.c
new file mode 100644
index 000000000000..5cb5384697ea
--- /dev/null
+++ b/drivers/net/phy/sfp-bus.c
@@ -0,0 +1,475 @@
1#include <linux/export.h>
2#include <linux/kref.h>
3#include <linux/list.h>
4#include <linux/mutex.h>
5#include <linux/phylink.h>
6#include <linux/rtnetlink.h>
7#include <linux/slab.h>
8
9#include "sfp.h"
10
11struct sfp_bus {
12 struct kref kref;
13 struct list_head node;
14 struct device_node *device_node;
15
16 const struct sfp_socket_ops *socket_ops;
17 struct device *sfp_dev;
18 struct sfp *sfp;
19
20 const struct sfp_upstream_ops *upstream_ops;
21 void *upstream;
22 struct net_device *netdev;
23 struct phy_device *phydev;
24
25 bool registered;
26 bool started;
27};
28
29
30int sfp_parse_port(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
31 unsigned long *support)
32{
33 int port;
34
35 /* port is the physical connector, set this from the connector field. */
36 switch (id->base.connector) {
37 case SFP_CONNECTOR_SC:
38 case SFP_CONNECTOR_FIBERJACK:
39 case SFP_CONNECTOR_LC:
40 case SFP_CONNECTOR_MT_RJ:
41 case SFP_CONNECTOR_MU:
42 case SFP_CONNECTOR_OPTICAL_PIGTAIL:
43 if (support)
44 phylink_set(support, FIBRE);
45 port = PORT_FIBRE;
46 break;
47
48 case SFP_CONNECTOR_RJ45:
49 if (support)
50 phylink_set(support, TP);
51 port = PORT_TP;
52 break;
53
54 case SFP_CONNECTOR_UNSPEC:
55 if (id->base.e1000_base_t) {
56 if (support)
57 phylink_set(support, TP);
58 port = PORT_TP;
59 break;
60 }
61 /* fallthrough */
62 case SFP_CONNECTOR_SG: /* guess */
63 case SFP_CONNECTOR_MPO_1X12:
64 case SFP_CONNECTOR_MPO_2X16:
65 case SFP_CONNECTOR_HSSDC_II:
66 case SFP_CONNECTOR_COPPER_PIGTAIL:
67 case SFP_CONNECTOR_NOSEPARATE:
68 case SFP_CONNECTOR_MXC_2X16:
69 port = PORT_OTHER;
70 break;
71 default:
72 dev_warn(bus->sfp_dev, "SFP: unknown connector id 0x%02x\n",
73 id->base.connector);
74 port = PORT_OTHER;
75 break;
76 }
77
78 return port;
79}
80EXPORT_SYMBOL_GPL(sfp_parse_port);
81
82phy_interface_t sfp_parse_interface(struct sfp_bus *bus,
83 const struct sfp_eeprom_id *id)
84{
85 phy_interface_t iface;
86
87 /* Setting the serdes link mode is guesswork: there's no field in
88 * the EEPROM which indicates what mode should be used.
89 *
90 * If the module wants 64b66b, then it must be >= 10G.
91 *
92 * If it's a gigabit-only fiber module, it probably does not have
93 * a PHY, so switch to 802.3z negotiation mode. Otherwise, switch
94 * to SGMII mode (which is required to support non-gigabit speeds).
95 */
96 switch (id->base.encoding) {
97 case SFP_ENCODING_8472_64B66B:
98 iface = PHY_INTERFACE_MODE_10GKR;
99 break;
100
101 case SFP_ENCODING_8B10B:
102 if (!id->base.e1000_base_t &&
103 !id->base.e100_base_lx &&
104 !id->base.e100_base_fx)
105 iface = PHY_INTERFACE_MODE_1000BASEX;
106 else
107 iface = PHY_INTERFACE_MODE_SGMII;
108 break;
109
110 default:
111 iface = PHY_INTERFACE_MODE_NA;
112 dev_err(bus->sfp_dev,
113 "SFP module encoding does not support 8b10b nor 64b66b\n");
114 break;
115 }
116
117 return iface;
118}
119EXPORT_SYMBOL_GPL(sfp_parse_interface);
120
121void sfp_parse_support(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
122 unsigned long *support)
123{
124 phylink_set(support, Autoneg);
125 phylink_set(support, Pause);
126 phylink_set(support, Asym_Pause);
127
128 /* Set ethtool support from the compliance fields. */
129 if (id->base.e10g_base_sr)
130 phylink_set(support, 10000baseSR_Full);
131 if (id->base.e10g_base_lr)
132 phylink_set(support, 10000baseLR_Full);
133 if (id->base.e10g_base_lrm)
134 phylink_set(support, 10000baseLRM_Full);
135 if (id->base.e10g_base_er)
136 phylink_set(support, 10000baseER_Full);
137 if (id->base.e1000_base_sx ||
138 id->base.e1000_base_lx ||
139 id->base.e1000_base_cx)
140 phylink_set(support, 1000baseX_Full);
141 if (id->base.e1000_base_t) {
142 phylink_set(support, 1000baseT_Half);
143 phylink_set(support, 1000baseT_Full);
144 }
145
146 switch (id->base.extended_cc) {
147 case 0x00: /* Unspecified */
148 break;
149 case 0x02: /* 100Gbase-SR4 or 25Gbase-SR */
150 phylink_set(support, 100000baseSR4_Full);
151 phylink_set(support, 25000baseSR_Full);
152 break;
153 case 0x03: /* 100Gbase-LR4 or 25Gbase-LR */
154 case 0x04: /* 100Gbase-ER4 or 25Gbase-ER */
155 phylink_set(support, 100000baseLR4_ER4_Full);
156 break;
157 case 0x0b: /* 100Gbase-CR4 or 25Gbase-CR CA-L */
158 case 0x0c: /* 25Gbase-CR CA-S */
159 case 0x0d: /* 25Gbase-CR CA-N */
160 phylink_set(support, 100000baseCR4_Full);
161 phylink_set(support, 25000baseCR_Full);
162 break;
163 default:
164 dev_warn(bus->sfp_dev,
165 "Unknown/unsupported extended compliance code: 0x%02x\n",
166 id->base.extended_cc);
167 break;
168 }
169
170 /* For fibre channel SFP, derive possible BaseX modes */
171 if (id->base.fc_speed_100 ||
172 id->base.fc_speed_200 ||
173 id->base.fc_speed_400) {
174 if (id->base.br_nominal >= 31)
175 phylink_set(support, 2500baseX_Full);
176 if (id->base.br_nominal >= 12)
177 phylink_set(support, 1000baseX_Full);
178 }
179
180 switch (id->base.connector) {
181 case SFP_CONNECTOR_SC:
182 case SFP_CONNECTOR_FIBERJACK:
183 case SFP_CONNECTOR_LC:
184 case SFP_CONNECTOR_MT_RJ:
185 case SFP_CONNECTOR_MU:
186 case SFP_CONNECTOR_OPTICAL_PIGTAIL:
187 break;
188
189 case SFP_CONNECTOR_UNSPEC:
190 if (id->base.e1000_base_t)
191 break;
192
193 case SFP_CONNECTOR_SG: /* guess */
194 case SFP_CONNECTOR_MPO_1X12:
195 case SFP_CONNECTOR_MPO_2X16:
196 case SFP_CONNECTOR_HSSDC_II:
197 case SFP_CONNECTOR_COPPER_PIGTAIL:
198 case SFP_CONNECTOR_NOSEPARATE:
199 case SFP_CONNECTOR_MXC_2X16:
200 default:
201 /* a guess at the supported link modes */
202 dev_warn(bus->sfp_dev,
203 "Guessing link modes, please report...\n");
204 phylink_set(support, 1000baseT_Half);
205 phylink_set(support, 1000baseT_Full);
206 break;
207 }
208}
209EXPORT_SYMBOL_GPL(sfp_parse_support);
210
211
212static LIST_HEAD(sfp_buses);
213static DEFINE_MUTEX(sfp_mutex);
214
215static const struct sfp_upstream_ops *sfp_get_upstream_ops(struct sfp_bus *bus)
216{
217 return bus->registered ? bus->upstream_ops : NULL;
218}
219
220static struct sfp_bus *sfp_bus_get(struct device_node *np)
221{
222 struct sfp_bus *sfp, *new, *found = NULL;
223
224 new = kzalloc(sizeof(*new), GFP_KERNEL);
225
226 mutex_lock(&sfp_mutex);
227
228 list_for_each_entry(sfp, &sfp_buses, node) {
229 if (sfp->device_node == np) {
230 kref_get(&sfp->kref);
231 found = sfp;
232 break;
233 }
234 }
235
236 if (!found && new) {
237 kref_init(&new->kref);
238 new->device_node = np;
239 list_add(&new->node, &sfp_buses);
240 found = new;
241 new = NULL;
242 }
243
244 mutex_unlock(&sfp_mutex);
245
246 kfree(new);
247
248 return found;
249}
250
251static void sfp_bus_release(struct kref *kref) __releases(sfp_mutex)
252{
253 struct sfp_bus *bus = container_of(kref, struct sfp_bus, kref);
254
255 list_del(&bus->node);
256 mutex_unlock(&sfp_mutex);
257 kfree(bus);
258}
259
260static void sfp_bus_put(struct sfp_bus *bus)
261{
262 kref_put_mutex(&bus->kref, sfp_bus_release, &sfp_mutex);
263}
264
265static int sfp_register_bus(struct sfp_bus *bus)
266{
267 const struct sfp_upstream_ops *ops = bus->upstream_ops;
268 int ret;
269
270 if (ops) {
271 if (ops->link_down)
272 ops->link_down(bus->upstream);
273 if (ops->connect_phy && bus->phydev) {
274 ret = ops->connect_phy(bus->upstream, bus->phydev);
275 if (ret)
276 return ret;
277 }
278 }
279 if (bus->started)
280 bus->socket_ops->start(bus->sfp);
281 bus->registered = true;
282 return 0;
283}
284
285static void sfp_unregister_bus(struct sfp_bus *bus)
286{
287 const struct sfp_upstream_ops *ops = bus->upstream_ops;
288
289 if (bus->registered) {
290 if (bus->started)
291 bus->socket_ops->stop(bus->sfp);
292 if (bus->phydev && ops && ops->disconnect_phy)
293 ops->disconnect_phy(bus->upstream);
294 }
295 bus->registered = false;
296}
297
298
299int sfp_get_module_info(struct sfp_bus *bus, struct ethtool_modinfo *modinfo)
300{
301 if (!bus->registered)
302 return -ENOIOCTLCMD;
303 return bus->socket_ops->module_info(bus->sfp, modinfo);
304}
305EXPORT_SYMBOL_GPL(sfp_get_module_info);
306
307int sfp_get_module_eeprom(struct sfp_bus *bus, struct ethtool_eeprom *ee,
308 u8 *data)
309{
310 if (!bus->registered)
311 return -ENOIOCTLCMD;
312 return bus->socket_ops->module_eeprom(bus->sfp, ee, data);
313}
314EXPORT_SYMBOL_GPL(sfp_get_module_eeprom);
315
316void sfp_upstream_start(struct sfp_bus *bus)
317{
318 if (bus->registered)
319 bus->socket_ops->start(bus->sfp);
320 bus->started = true;
321}
322EXPORT_SYMBOL_GPL(sfp_upstream_start);
323
324void sfp_upstream_stop(struct sfp_bus *bus)
325{
326 if (bus->registered)
327 bus->socket_ops->stop(bus->sfp);
328 bus->started = false;
329}
330EXPORT_SYMBOL_GPL(sfp_upstream_stop);
331
332struct sfp_bus *sfp_register_upstream(struct device_node *np,
333 struct net_device *ndev, void *upstream,
334 const struct sfp_upstream_ops *ops)
335{
336 struct sfp_bus *bus = sfp_bus_get(np);
337 int ret = 0;
338
339 if (bus) {
340 rtnl_lock();
341 bus->upstream_ops = ops;
342 bus->upstream = upstream;
343 bus->netdev = ndev;
344
345 if (bus->sfp)
346 ret = sfp_register_bus(bus);
347 rtnl_unlock();
348 }
349
350 if (ret) {
351 sfp_bus_put(bus);
352 bus = NULL;
353 }
354
355 return bus;
356}
357EXPORT_SYMBOL_GPL(sfp_register_upstream);
358
359void sfp_unregister_upstream(struct sfp_bus *bus)
360{
361 rtnl_lock();
362 sfp_unregister_bus(bus);
363 bus->upstream = NULL;
364 bus->netdev = NULL;
365 rtnl_unlock();
366
367 sfp_bus_put(bus);
368}
369EXPORT_SYMBOL_GPL(sfp_unregister_upstream);
370
371
372/* Socket driver entry points */
373int sfp_add_phy(struct sfp_bus *bus, struct phy_device *phydev)
374{
375 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
376 int ret = 0;
377
378 if (ops && ops->connect_phy)
379 ret = ops->connect_phy(bus->upstream, phydev);
380
381 if (ret == 0)
382 bus->phydev = phydev;
383
384 return ret;
385}
386EXPORT_SYMBOL_GPL(sfp_add_phy);
387
388void sfp_remove_phy(struct sfp_bus *bus)
389{
390 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
391
392 if (ops && ops->disconnect_phy)
393 ops->disconnect_phy(bus->upstream);
394 bus->phydev = NULL;
395}
396EXPORT_SYMBOL_GPL(sfp_remove_phy);
397
398
399void sfp_link_up(struct sfp_bus *bus)
400{
401 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
402
403 if (ops && ops->link_up)
404 ops->link_up(bus->upstream);
405}
406EXPORT_SYMBOL_GPL(sfp_link_up);
407
408void sfp_link_down(struct sfp_bus *bus)
409{
410 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
411
412 if (ops && ops->link_down)
413 ops->link_down(bus->upstream);
414}
415EXPORT_SYMBOL_GPL(sfp_link_down);
416
417int sfp_module_insert(struct sfp_bus *bus, const struct sfp_eeprom_id *id)
418{
419 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
420 int ret = 0;
421
422 if (ops && ops->module_insert)
423 ret = ops->module_insert(bus->upstream, id);
424
425 return ret;
426}
427EXPORT_SYMBOL_GPL(sfp_module_insert);
428
429void sfp_module_remove(struct sfp_bus *bus)
430{
431 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
432
433 if (ops && ops->module_remove)
434 ops->module_remove(bus->upstream);
435}
436EXPORT_SYMBOL_GPL(sfp_module_remove);
437
438struct sfp_bus *sfp_register_socket(struct device *dev, struct sfp *sfp,
439 const struct sfp_socket_ops *ops)
440{
441 struct sfp_bus *bus = sfp_bus_get(dev->of_node);
442 int ret = 0;
443
444 if (bus) {
445 rtnl_lock();
446 bus->sfp_dev = dev;
447 bus->sfp = sfp;
448 bus->socket_ops = ops;
449
450 if (bus->netdev)
451 ret = sfp_register_bus(bus);
452 rtnl_unlock();
453 }
454
455 if (ret) {
456 sfp_bus_put(bus);
457 bus = NULL;
458 }
459
460 return bus;
461}
462EXPORT_SYMBOL_GPL(sfp_register_socket);
463
464void sfp_unregister_socket(struct sfp_bus *bus)
465{
466 rtnl_lock();
467 sfp_unregister_bus(bus);
468 bus->sfp_dev = NULL;
469 bus->sfp = NULL;
470 bus->socket_ops = NULL;
471 rtnl_unlock();
472
473 sfp_bus_put(bus);
474}
475EXPORT_SYMBOL_GPL(sfp_unregister_socket);
diff --git a/drivers/net/phy/sfp.c b/drivers/net/phy/sfp.c
new file mode 100644
index 000000000000..fb2cf4342f48
--- /dev/null
+++ b/drivers/net/phy/sfp.c
@@ -0,0 +1,915 @@
1#include <linux/delay.h>
2#include <linux/gpio.h>
3#include <linux/i2c.h>
4#include <linux/interrupt.h>
5#include <linux/jiffies.h>
6#include <linux/module.h>
7#include <linux/mutex.h>
8#include <linux/of.h>
9#include <linux/phy.h>
10#include <linux/platform_device.h>
11#include <linux/rtnetlink.h>
12#include <linux/slab.h>
13#include <linux/workqueue.h>
14
15#include "mdio-i2c.h"
16#include "sfp.h"
17#include "swphy.h"
18
19enum {
20 GPIO_MODDEF0,
21 GPIO_LOS,
22 GPIO_TX_FAULT,
23 GPIO_TX_DISABLE,
24 GPIO_RATE_SELECT,
25 GPIO_MAX,
26
27 SFP_F_PRESENT = BIT(GPIO_MODDEF0),
28 SFP_F_LOS = BIT(GPIO_LOS),
29 SFP_F_TX_FAULT = BIT(GPIO_TX_FAULT),
30 SFP_F_TX_DISABLE = BIT(GPIO_TX_DISABLE),
31 SFP_F_RATE_SELECT = BIT(GPIO_RATE_SELECT),
32
33 SFP_E_INSERT = 0,
34 SFP_E_REMOVE,
35 SFP_E_DEV_DOWN,
36 SFP_E_DEV_UP,
37 SFP_E_TX_FAULT,
38 SFP_E_TX_CLEAR,
39 SFP_E_LOS_HIGH,
40 SFP_E_LOS_LOW,
41 SFP_E_TIMEOUT,
42
43 SFP_MOD_EMPTY = 0,
44 SFP_MOD_PROBE,
45 SFP_MOD_PRESENT,
46 SFP_MOD_ERROR,
47
48 SFP_DEV_DOWN = 0,
49 SFP_DEV_UP,
50
51 SFP_S_DOWN = 0,
52 SFP_S_INIT,
53 SFP_S_WAIT_LOS,
54 SFP_S_LINK_UP,
55 SFP_S_TX_FAULT,
56 SFP_S_REINIT,
57 SFP_S_TX_DISABLE,
58};
59
60static const char *gpio_of_names[] = {
61 "moddef0",
62 "los",
63 "tx-fault",
64 "tx-disable",
65 "rate-select",
66};
67
68static const enum gpiod_flags gpio_flags[] = {
69 GPIOD_IN,
70 GPIOD_IN,
71 GPIOD_IN,
72 GPIOD_ASIS,
73 GPIOD_ASIS,
74};
75
76#define T_INIT_JIFFIES msecs_to_jiffies(300)
77#define T_RESET_US 10
78#define T_FAULT_RECOVER msecs_to_jiffies(1000)
79
80/* SFP module presence detection is poor: the three MOD DEF signals are
81 * the same length on the PCB, which means it's possible for MOD DEF 0 to
82 * connect before the I2C bus on MOD DEF 1/2.
83 *
84 * The SFP MSA specifies 300ms as t_init (the time taken for TX_FAULT to
85 * be deasserted) but makes no mention of the earliest time before we can
86 * access the I2C EEPROM. However, Avago modules require 300ms.
87 */
88#define T_PROBE_INIT msecs_to_jiffies(300)
89#define T_PROBE_RETRY msecs_to_jiffies(100)
90
91/*
92 * SFP modules appear to always have their PHY configured for bus address
93 * 0x56 (which with mdio-i2c, translates to a PHY address of 22).
94 */
95#define SFP_PHY_ADDR 22
96
97/*
98 * Give this long for the PHY to reset.
99 */
100#define T_PHY_RESET_MS 50
101
102static DEFINE_MUTEX(sfp_mutex);
103
104struct sfp {
105 struct device *dev;
106 struct i2c_adapter *i2c;
107 struct mii_bus *i2c_mii;
108 struct sfp_bus *sfp_bus;
109 struct phy_device *mod_phy;
110
111 unsigned int (*get_state)(struct sfp *);
112 void (*set_state)(struct sfp *, unsigned int);
113 int (*read)(struct sfp *, bool, u8, void *, size_t);
114
115 struct gpio_desc *gpio[GPIO_MAX];
116
117 unsigned int state;
118 struct delayed_work poll;
119 struct delayed_work timeout;
120 struct mutex sm_mutex;
121 unsigned char sm_mod_state;
122 unsigned char sm_dev_state;
123 unsigned short sm_state;
124 unsigned int sm_retries;
125
126 struct sfp_eeprom_id id;
127};
128
129static unsigned long poll_jiffies;
130
131static unsigned int sfp_gpio_get_state(struct sfp *sfp)
132{
133 unsigned int i, state, v;
134
135 for (i = state = 0; i < GPIO_MAX; i++) {
136 if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
137 continue;
138
139 v = gpiod_get_value_cansleep(sfp->gpio[i]);
140 if (v)
141 state |= BIT(i);
142 }
143
144 return state;
145}
146
147static void sfp_gpio_set_state(struct sfp *sfp, unsigned int state)
148{
149 if (state & SFP_F_PRESENT) {
150 /* If the module is present, drive the signals */
151 if (sfp->gpio[GPIO_TX_DISABLE])
152 gpiod_direction_output(sfp->gpio[GPIO_TX_DISABLE],
153 state & SFP_F_TX_DISABLE);
154 if (state & SFP_F_RATE_SELECT)
155 gpiod_direction_output(sfp->gpio[GPIO_RATE_SELECT],
156 state & SFP_F_RATE_SELECT);
157 } else {
158 /* Otherwise, let them float to the pull-ups */
159 if (sfp->gpio[GPIO_TX_DISABLE])
160 gpiod_direction_input(sfp->gpio[GPIO_TX_DISABLE]);
161 if (state & SFP_F_RATE_SELECT)
162 gpiod_direction_input(sfp->gpio[GPIO_RATE_SELECT]);
163 }
164}
165
166static int sfp__i2c_read(struct i2c_adapter *i2c, u8 bus_addr, u8 dev_addr,
167 void *buf, size_t len)
168{
169 struct i2c_msg msgs[2];
170 int ret;
171
172 msgs[0].addr = bus_addr;
173 msgs[0].flags = 0;
174 msgs[0].len = 1;
175 msgs[0].buf = &dev_addr;
176 msgs[1].addr = bus_addr;
177 msgs[1].flags = I2C_M_RD;
178 msgs[1].len = len;
179 msgs[1].buf = buf;
180
181 ret = i2c_transfer(i2c, msgs, ARRAY_SIZE(msgs));
182 if (ret < 0)
183 return ret;
184
185 return ret == ARRAY_SIZE(msgs) ? len : 0;
186}
187
188static int sfp_i2c_read(struct sfp *sfp, bool a2, u8 addr, void *buf,
189 size_t len)
190{
191 return sfp__i2c_read(sfp->i2c, a2 ? 0x51 : 0x50, addr, buf, len);
192}
193
194static int sfp_i2c_configure(struct sfp *sfp, struct i2c_adapter *i2c)
195{
196 struct mii_bus *i2c_mii;
197 int ret;
198
199 if (!i2c_check_functionality(i2c, I2C_FUNC_I2C))
200 return -EINVAL;
201
202 sfp->i2c = i2c;
203 sfp->read = sfp_i2c_read;
204
205 i2c_mii = mdio_i2c_alloc(sfp->dev, i2c);
206 if (IS_ERR(i2c_mii))
207 return PTR_ERR(i2c_mii);
208
209 i2c_mii->name = "SFP I2C Bus";
210 i2c_mii->phy_mask = ~0;
211
212 ret = mdiobus_register(i2c_mii);
213 if (ret < 0) {
214 mdiobus_free(i2c_mii);
215 return ret;
216 }
217
218 sfp->i2c_mii = i2c_mii;
219
220 return 0;
221}
222
223
224/* Interface */
225static unsigned int sfp_get_state(struct sfp *sfp)
226{
227 return sfp->get_state(sfp);
228}
229
230static void sfp_set_state(struct sfp *sfp, unsigned int state)
231{
232 sfp->set_state(sfp, state);
233}
234
235static int sfp_read(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
236{
237 return sfp->read(sfp, a2, addr, buf, len);
238}
239
240static unsigned int sfp_check(void *buf, size_t len)
241{
242 u8 *p, check;
243
244 for (p = buf, check = 0; len; p++, len--)
245 check += *p;
246
247 return check;
248}
249
250/* Helpers */
251static void sfp_module_tx_disable(struct sfp *sfp)
252{
253 dev_dbg(sfp->dev, "tx disable %u -> %u\n",
254 sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 1);
255 sfp->state |= SFP_F_TX_DISABLE;
256 sfp_set_state(sfp, sfp->state);
257}
258
259static void sfp_module_tx_enable(struct sfp *sfp)
260{
261 dev_dbg(sfp->dev, "tx disable %u -> %u\n",
262 sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 0);
263 sfp->state &= ~SFP_F_TX_DISABLE;
264 sfp_set_state(sfp, sfp->state);
265}
266
267static void sfp_module_tx_fault_reset(struct sfp *sfp)
268{
269 unsigned int state = sfp->state;
270
271 if (state & SFP_F_TX_DISABLE)
272 return;
273
274 sfp_set_state(sfp, state | SFP_F_TX_DISABLE);
275
276 udelay(T_RESET_US);
277
278 sfp_set_state(sfp, state);
279}
280
281/* SFP state machine */
282static void sfp_sm_set_timer(struct sfp *sfp, unsigned int timeout)
283{
284 if (timeout)
285 mod_delayed_work(system_power_efficient_wq, &sfp->timeout,
286 timeout);
287 else
288 cancel_delayed_work(&sfp->timeout);
289}
290
291static void sfp_sm_next(struct sfp *sfp, unsigned int state,
292 unsigned int timeout)
293{
294 sfp->sm_state = state;
295 sfp_sm_set_timer(sfp, timeout);
296}
297
298static void sfp_sm_ins_next(struct sfp *sfp, unsigned int state, unsigned int timeout)
299{
300 sfp->sm_mod_state = state;
301 sfp_sm_set_timer(sfp, timeout);
302}
303
304static void sfp_sm_phy_detach(struct sfp *sfp)
305{
306 phy_stop(sfp->mod_phy);
307 sfp_remove_phy(sfp->sfp_bus);
308 phy_device_remove(sfp->mod_phy);
309 phy_device_free(sfp->mod_phy);
310 sfp->mod_phy = NULL;
311}
312
313static void sfp_sm_probe_phy(struct sfp *sfp)
314{
315 struct phy_device *phy;
316 int err;
317
318 msleep(T_PHY_RESET_MS);
319
320 phy = mdiobus_scan(sfp->i2c_mii, SFP_PHY_ADDR);
321 if (IS_ERR(phy)) {
322 dev_err(sfp->dev, "mdiobus scan returned %ld\n", PTR_ERR(phy));
323 return;
324 }
325 if (!phy) {
326 dev_info(sfp->dev, "no PHY detected\n");
327 return;
328 }
329
330 err = sfp_add_phy(sfp->sfp_bus, phy);
331 if (err) {
332 phy_device_remove(phy);
333 phy_device_free(phy);
334 dev_err(sfp->dev, "sfp_add_phy failed: %d\n", err);
335 return;
336 }
337
338 sfp->mod_phy = phy;
339 phy_start(phy);
340}
341
342static void sfp_sm_link_up(struct sfp *sfp)
343{
344 sfp_link_up(sfp->sfp_bus);
345 sfp_sm_next(sfp, SFP_S_LINK_UP, 0);
346}
347
348static void sfp_sm_link_down(struct sfp *sfp)
349{
350 sfp_link_down(sfp->sfp_bus);
351}
352
353static void sfp_sm_link_check_los(struct sfp *sfp)
354{
355 unsigned int los = sfp->state & SFP_F_LOS;
356
357 /* FIXME: what if neither SFP_OPTIONS_LOS_INVERTED nor
358 * SFP_OPTIONS_LOS_NORMAL are set? For now, we assume
359 * the same as SFP_OPTIONS_LOS_NORMAL set.
360 */
361 if (sfp->id.ext.options & SFP_OPTIONS_LOS_INVERTED)
362 los ^= SFP_F_LOS;
363
364 if (los)
365 sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
366 else
367 sfp_sm_link_up(sfp);
368}
369
370static void sfp_sm_fault(struct sfp *sfp, bool warn)
371{
372 if (sfp->sm_retries && !--sfp->sm_retries) {
373 dev_err(sfp->dev, "module persistently indicates fault, disabling\n");
374 sfp_sm_next(sfp, SFP_S_TX_DISABLE, 0);
375 } else {
376 if (warn)
377 dev_err(sfp->dev, "module transmit fault indicated\n");
378
379 sfp_sm_next(sfp, SFP_S_TX_FAULT, T_FAULT_RECOVER);
380 }
381}
382
383static void sfp_sm_mod_init(struct sfp *sfp)
384{
385 sfp_module_tx_enable(sfp);
386
387 /* Wait t_init before indicating that the link is up, provided the
388 * current state indicates no TX_FAULT. If TX_FAULT clears before
389 * this time, that's fine too.
390 */
391 sfp_sm_next(sfp, SFP_S_INIT, T_INIT_JIFFIES);
392 sfp->sm_retries = 5;
393
394 /* Setting the serdes link mode is guesswork: there's no
395 * field in the EEPROM which indicates what mode should
396 * be used.
397 *
398 * If it's a gigabit-only fiber module, it probably does
399 * not have a PHY, so switch to 802.3z negotiation mode.
400 * Otherwise, switch to SGMII mode (which is required to
401 * support non-gigabit speeds) and probe for a PHY.
402 */
403 if (sfp->id.base.e1000_base_t ||
404 sfp->id.base.e100_base_lx ||
405 sfp->id.base.e100_base_fx)
406 sfp_sm_probe_phy(sfp);
407}
408
409static int sfp_sm_mod_probe(struct sfp *sfp)
410{
411 /* SFP module inserted - read I2C data */
412 struct sfp_eeprom_id id;
413 char vendor[17];
414 char part[17];
415 char sn[17];
416 char date[9];
417 char rev[5];
418 u8 check;
419 int err;
420
421 err = sfp_read(sfp, false, 0, &id, sizeof(id));
422 if (err < 0) {
423 dev_err(sfp->dev, "failed to read EEPROM: %d\n", err);
424 return -EAGAIN;
425 }
426
427 if (err != sizeof(id)) {
428 dev_err(sfp->dev, "EEPROM short read: %d\n", err);
429 return -EAGAIN;
430 }
431
432 /* Validate the checksum over the base structure */
433 check = sfp_check(&id.base, sizeof(id.base) - 1);
434 if (check != id.base.cc_base) {
435 dev_err(sfp->dev,
436 "EEPROM base structure checksum failure: 0x%02x\n",
437 check);
438 print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET,
439 16, 1, &id, sizeof(id.base) - 1, true);
440 return -EINVAL;
441 }
442
443 check = sfp_check(&id.ext, sizeof(id.ext) - 1);
444 if (check != id.ext.cc_ext) {
445 dev_err(sfp->dev,
446 "EEPROM extended structure checksum failure: 0x%02x\n",
447 check);
448 memset(&id.ext, 0, sizeof(id.ext));
449 }
450
451 sfp->id = id;
452
453 memcpy(vendor, sfp->id.base.vendor_name, 16);
454 vendor[16] = '\0';
455 memcpy(part, sfp->id.base.vendor_pn, 16);
456 part[16] = '\0';
457 memcpy(rev, sfp->id.base.vendor_rev, 4);
458 rev[4] = '\0';
459 memcpy(sn, sfp->id.ext.vendor_sn, 16);
460 sn[16] = '\0';
461 memcpy(date, sfp->id.ext.datecode, 8);
462 date[8] = '\0';
463
464 dev_info(sfp->dev, "module %s %s rev %s sn %s dc %s\n", vendor, part, rev, sn, date);
465
466 /* We only support SFP modules, not the legacy GBIC modules. */
467 if (sfp->id.base.phys_id != SFP_PHYS_ID_SFP ||
468 sfp->id.base.phys_ext_id != SFP_PHYS_EXT_ID_SFP) {
469 dev_err(sfp->dev, "module is not SFP - phys id 0x%02x 0x%02x\n",
470 sfp->id.base.phys_id, sfp->id.base.phys_ext_id);
471 return -EINVAL;
472 }
473
474 return sfp_module_insert(sfp->sfp_bus, &sfp->id);
475}
476
477static void sfp_sm_mod_remove(struct sfp *sfp)
478{
479 sfp_module_remove(sfp->sfp_bus);
480
481 if (sfp->mod_phy)
482 sfp_sm_phy_detach(sfp);
483
484 sfp_module_tx_disable(sfp);
485
486 memset(&sfp->id, 0, sizeof(sfp->id));
487
488 dev_info(sfp->dev, "module removed\n");
489}
490
491static void sfp_sm_event(struct sfp *sfp, unsigned int event)
492{
493 mutex_lock(&sfp->sm_mutex);
494
495 dev_dbg(sfp->dev, "SM: enter %u:%u:%u event %u\n",
496 sfp->sm_mod_state, sfp->sm_dev_state, sfp->sm_state, event);
497
498 /* This state machine tracks the insert/remove state of
499 * the module, and handles probing the on-board EEPROM.
500 */
501 switch (sfp->sm_mod_state) {
502 default:
503 if (event == SFP_E_INSERT) {
504 sfp_module_tx_disable(sfp);
505 sfp_sm_ins_next(sfp, SFP_MOD_PROBE, T_PROBE_INIT);
506 }
507 break;
508
509 case SFP_MOD_PROBE:
510 if (event == SFP_E_REMOVE) {
511 sfp_sm_ins_next(sfp, SFP_MOD_EMPTY, 0);
512 } else if (event == SFP_E_TIMEOUT) {
513 int err = sfp_sm_mod_probe(sfp);
514
515 if (err == 0)
516 sfp_sm_ins_next(sfp, SFP_MOD_PRESENT, 0);
517 else if (err == -EAGAIN)
518 sfp_sm_set_timer(sfp, T_PROBE_RETRY);
519 else
520 sfp_sm_ins_next(sfp, SFP_MOD_ERROR, 0);
521 }
522 break;
523
524 case SFP_MOD_PRESENT:
525 case SFP_MOD_ERROR:
526 if (event == SFP_E_REMOVE) {
527 sfp_sm_mod_remove(sfp);
528 sfp_sm_ins_next(sfp, SFP_MOD_EMPTY, 0);
529 }
530 break;
531 }
532
533 /* This state machine tracks the netdev up/down state */
534 switch (sfp->sm_dev_state) {
535 default:
536 if (event == SFP_E_DEV_UP)
537 sfp->sm_dev_state = SFP_DEV_UP;
538 break;
539
540 case SFP_DEV_UP:
541 if (event == SFP_E_DEV_DOWN) {
542 /* If the module has a PHY, avoid raising TX disable
543 * as this resets the PHY. Otherwise, raise it to
544 * turn the laser off.
545 */
546 if (!sfp->mod_phy)
547 sfp_module_tx_disable(sfp);
548 sfp->sm_dev_state = SFP_DEV_DOWN;
549 }
550 break;
551 }
552
553 /* Some events are global */
554 if (sfp->sm_state != SFP_S_DOWN &&
555 (sfp->sm_mod_state != SFP_MOD_PRESENT ||
556 sfp->sm_dev_state != SFP_DEV_UP)) {
557 if (sfp->sm_state == SFP_S_LINK_UP &&
558 sfp->sm_dev_state == SFP_DEV_UP)
559 sfp_sm_link_down(sfp);
560 if (sfp->mod_phy)
561 sfp_sm_phy_detach(sfp);
562 sfp_sm_next(sfp, SFP_S_DOWN, 0);
563 mutex_unlock(&sfp->sm_mutex);
564 return;
565 }
566
567 /* The main state machine */
568 switch (sfp->sm_state) {
569 case SFP_S_DOWN:
570 if (sfp->sm_mod_state == SFP_MOD_PRESENT &&
571 sfp->sm_dev_state == SFP_DEV_UP)
572 sfp_sm_mod_init(sfp);
573 break;
574
575 case SFP_S_INIT:
576 if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT)
577 sfp_sm_fault(sfp, true);
578 else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR)
579 sfp_sm_link_check_los(sfp);
580 break;
581
582 case SFP_S_WAIT_LOS:
583 if (event == SFP_E_TX_FAULT)
584 sfp_sm_fault(sfp, true);
585 else if (event ==
586 (sfp->id.ext.options & SFP_OPTIONS_LOS_INVERTED ?
587 SFP_E_LOS_HIGH : SFP_E_LOS_LOW))
588 sfp_sm_link_up(sfp);
589 break;
590
591 case SFP_S_LINK_UP:
592 if (event == SFP_E_TX_FAULT) {
593 sfp_sm_link_down(sfp);
594 sfp_sm_fault(sfp, true);
595 } else if (event ==
596 (sfp->id.ext.options & SFP_OPTIONS_LOS_INVERTED ?
597 SFP_E_LOS_LOW : SFP_E_LOS_HIGH)) {
598 sfp_sm_link_down(sfp);
599 sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
600 }
601 break;
602
603 case SFP_S_TX_FAULT:
604 if (event == SFP_E_TIMEOUT) {
605 sfp_module_tx_fault_reset(sfp);
606 sfp_sm_next(sfp, SFP_S_REINIT, T_INIT_JIFFIES);
607 }
608 break;
609
610 case SFP_S_REINIT:
611 if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT) {
612 sfp_sm_fault(sfp, false);
613 } else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) {
614 dev_info(sfp->dev, "module transmit fault recovered\n");
615 sfp_sm_link_check_los(sfp);
616 }
617 break;
618
619 case SFP_S_TX_DISABLE:
620 break;
621 }
622
623 dev_dbg(sfp->dev, "SM: exit %u:%u:%u\n",
624 sfp->sm_mod_state, sfp->sm_dev_state, sfp->sm_state);
625
626 mutex_unlock(&sfp->sm_mutex);
627}
628
629static void sfp_start(struct sfp *sfp)
630{
631 sfp_sm_event(sfp, SFP_E_DEV_UP);
632}
633
634static void sfp_stop(struct sfp *sfp)
635{
636 sfp_sm_event(sfp, SFP_E_DEV_DOWN);
637}
638
639static int sfp_module_info(struct sfp *sfp, struct ethtool_modinfo *modinfo)
640{
641 /* locking... and check module is present */
642
643 if (sfp->id.ext.sff8472_compliance) {
644 modinfo->type = ETH_MODULE_SFF_8472;
645 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
646 } else {
647 modinfo->type = ETH_MODULE_SFF_8079;
648 modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
649 }
650 return 0;
651}
652
653static int sfp_module_eeprom(struct sfp *sfp, struct ethtool_eeprom *ee,
654 u8 *data)
655{
656 unsigned int first, last, len;
657 int ret;
658
659 if (ee->len == 0)
660 return -EINVAL;
661
662 first = ee->offset;
663 last = ee->offset + ee->len;
664 if (first < ETH_MODULE_SFF_8079_LEN) {
665 len = min_t(unsigned int, last, ETH_MODULE_SFF_8079_LEN);
666 len -= first;
667
668 ret = sfp->read(sfp, false, first, data, len);
669 if (ret < 0)
670 return ret;
671
672 first += len;
673 data += len;
674 }
675 if (first >= ETH_MODULE_SFF_8079_LEN &&
676 first < ETH_MODULE_SFF_8472_LEN) {
677 len = min_t(unsigned int, last, ETH_MODULE_SFF_8472_LEN);
678 len -= first;
679 first -= ETH_MODULE_SFF_8079_LEN;
680
681 ret = sfp->read(sfp, true, first, data, len);
682 if (ret < 0)
683 return ret;
684 }
685 return 0;
686}
687
688static const struct sfp_socket_ops sfp_module_ops = {
689 .start = sfp_start,
690 .stop = sfp_stop,
691 .module_info = sfp_module_info,
692 .module_eeprom = sfp_module_eeprom,
693};
694
695static void sfp_timeout(struct work_struct *work)
696{
697 struct sfp *sfp = container_of(work, struct sfp, timeout.work);
698
699 rtnl_lock();
700 sfp_sm_event(sfp, SFP_E_TIMEOUT);
701 rtnl_unlock();
702}
703
704static void sfp_check_state(struct sfp *sfp)
705{
706 unsigned int state, i, changed;
707
708 state = sfp_get_state(sfp);
709 changed = state ^ sfp->state;
710 changed &= SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT;
711
712 for (i = 0; i < GPIO_MAX; i++)
713 if (changed & BIT(i))
714 dev_dbg(sfp->dev, "%s %u -> %u\n", gpio_of_names[i],
715 !!(sfp->state & BIT(i)), !!(state & BIT(i)));
716
717 state |= sfp->state & (SFP_F_TX_DISABLE | SFP_F_RATE_SELECT);
718 sfp->state = state;
719
720 rtnl_lock();
721 if (changed & SFP_F_PRESENT)
722 sfp_sm_event(sfp, state & SFP_F_PRESENT ?
723 SFP_E_INSERT : SFP_E_REMOVE);
724
725 if (changed & SFP_F_TX_FAULT)
726 sfp_sm_event(sfp, state & SFP_F_TX_FAULT ?
727 SFP_E_TX_FAULT : SFP_E_TX_CLEAR);
728
729 if (changed & SFP_F_LOS)
730 sfp_sm_event(sfp, state & SFP_F_LOS ?
731 SFP_E_LOS_HIGH : SFP_E_LOS_LOW);
732 rtnl_unlock();
733}
734
735static irqreturn_t sfp_irq(int irq, void *data)
736{
737 struct sfp *sfp = data;
738
739 sfp_check_state(sfp);
740
741 return IRQ_HANDLED;
742}
743
744static void sfp_poll(struct work_struct *work)
745{
746 struct sfp *sfp = container_of(work, struct sfp, poll.work);
747
748 sfp_check_state(sfp);
749 mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
750}
751
752static struct sfp *sfp_alloc(struct device *dev)
753{
754 struct sfp *sfp;
755
756 sfp = kzalloc(sizeof(*sfp), GFP_KERNEL);
757 if (!sfp)
758 return ERR_PTR(-ENOMEM);
759
760 sfp->dev = dev;
761
762 mutex_init(&sfp->sm_mutex);
763 INIT_DELAYED_WORK(&sfp->poll, sfp_poll);
764 INIT_DELAYED_WORK(&sfp->timeout, sfp_timeout);
765
766 return sfp;
767}
768
769static void sfp_cleanup(void *data)
770{
771 struct sfp *sfp = data;
772
773 cancel_delayed_work_sync(&sfp->poll);
774 cancel_delayed_work_sync(&sfp->timeout);
775 if (sfp->i2c_mii) {
776 mdiobus_unregister(sfp->i2c_mii);
777 mdiobus_free(sfp->i2c_mii);
778 }
779 if (sfp->i2c)
780 i2c_put_adapter(sfp->i2c);
781 kfree(sfp);
782}
783
784static int sfp_probe(struct platform_device *pdev)
785{
786 struct sfp *sfp;
787 bool poll = false;
788 int irq, err, i;
789
790 sfp = sfp_alloc(&pdev->dev);
791 if (IS_ERR(sfp))
792 return PTR_ERR(sfp);
793
794 platform_set_drvdata(pdev, sfp);
795
796 err = devm_add_action(sfp->dev, sfp_cleanup, sfp);
797 if (err < 0)
798 return err;
799
800 if (pdev->dev.of_node) {
801 struct device_node *node = pdev->dev.of_node;
802 struct device_node *np;
803
804 np = of_parse_phandle(node, "i2c-bus", 0);
805 if (np) {
806 struct i2c_adapter *i2c;
807
808 i2c = of_find_i2c_adapter_by_node(np);
809 of_node_put(np);
810 if (!i2c)
811 return -EPROBE_DEFER;
812
813 err = sfp_i2c_configure(sfp, i2c);
814 if (err < 0) {
815 i2c_put_adapter(i2c);
816 return err;
817 }
818 }
819
820 for (i = 0; i < GPIO_MAX; i++) {
821 sfp->gpio[i] = devm_gpiod_get_optional(sfp->dev,
822 gpio_of_names[i], gpio_flags[i]);
823 if (IS_ERR(sfp->gpio[i]))
824 return PTR_ERR(sfp->gpio[i]);
825 }
826
827 sfp->get_state = sfp_gpio_get_state;
828 sfp->set_state = sfp_gpio_set_state;
829 }
830
831 sfp->sfp_bus = sfp_register_socket(sfp->dev, sfp, &sfp_module_ops);
832 if (!sfp->sfp_bus)
833 return -ENOMEM;
834
835 /* Get the initial state, and always signal TX disable,
836 * since the network interface will not be up.
837 */
838 sfp->state = sfp_get_state(sfp) | SFP_F_TX_DISABLE;
839
840 if (sfp->gpio[GPIO_RATE_SELECT] &&
841 gpiod_get_value_cansleep(sfp->gpio[GPIO_RATE_SELECT]))
842 sfp->state |= SFP_F_RATE_SELECT;
843 sfp_set_state(sfp, sfp->state);
844 sfp_module_tx_disable(sfp);
845 rtnl_lock();
846 if (sfp->state & SFP_F_PRESENT)
847 sfp_sm_event(sfp, SFP_E_INSERT);
848 rtnl_unlock();
849
850 for (i = 0; i < GPIO_MAX; i++) {
851 if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
852 continue;
853
854 irq = gpiod_to_irq(sfp->gpio[i]);
855 if (!irq) {
856 poll = true;
857 continue;
858 }
859
860 err = devm_request_threaded_irq(sfp->dev, irq, NULL, sfp_irq,
861 IRQF_ONESHOT |
862 IRQF_TRIGGER_RISING |
863 IRQF_TRIGGER_FALLING,
864 dev_name(sfp->dev), sfp);
865 if (err)
866 poll = true;
867 }
868
869 if (poll)
870 mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
871
872 return 0;
873}
874
875static int sfp_remove(struct platform_device *pdev)
876{
877 struct sfp *sfp = platform_get_drvdata(pdev);
878
879 sfp_unregister_socket(sfp->sfp_bus);
880
881 return 0;
882}
883
884static const struct of_device_id sfp_of_match[] = {
885 { .compatible = "sff,sfp", },
886 { },
887};
888MODULE_DEVICE_TABLE(of, sfp_of_match);
889
890static struct platform_driver sfp_driver = {
891 .probe = sfp_probe,
892 .remove = sfp_remove,
893 .driver = {
894 .name = "sfp",
895 .of_match_table = sfp_of_match,
896 },
897};
898
899static int sfp_init(void)
900{
901 poll_jiffies = msecs_to_jiffies(100);
902
903 return platform_driver_register(&sfp_driver);
904}
905module_init(sfp_init);
906
907static void sfp_exit(void)
908{
909 platform_driver_unregister(&sfp_driver);
910}
911module_exit(sfp_exit);
912
913MODULE_ALIAS("platform:sfp");
914MODULE_AUTHOR("Russell King");
915MODULE_LICENSE("GPL v2");
diff --git a/drivers/net/phy/sfp.h b/drivers/net/phy/sfp.h
new file mode 100644
index 000000000000..31b0acf337e2
--- /dev/null
+++ b/drivers/net/phy/sfp.h
@@ -0,0 +1,28 @@
1#ifndef SFP_H
2#define SFP_H
3
4#include <linux/ethtool.h>
5#include <linux/sfp.h>
6
7struct sfp;
8
9struct sfp_socket_ops {
10 void (*start)(struct sfp *sfp);
11 void (*stop)(struct sfp *sfp);
12 int (*module_info)(struct sfp *sfp, struct ethtool_modinfo *modinfo);
13 int (*module_eeprom)(struct sfp *sfp, struct ethtool_eeprom *ee,
14 u8 *data);
15};
16
17int sfp_add_phy(struct sfp_bus *bus, struct phy_device *phydev);
18void sfp_remove_phy(struct sfp_bus *bus);
19void sfp_link_up(struct sfp_bus *bus);
20void sfp_link_down(struct sfp_bus *bus);
21int sfp_module_insert(struct sfp_bus *bus, const struct sfp_eeprom_id *id);
22void sfp_module_remove(struct sfp_bus *bus);
23int sfp_link_configure(struct sfp_bus *bus, const struct sfp_eeprom_id *id);
24struct sfp_bus *sfp_register_socket(struct device *dev, struct sfp *sfp,
25 const struct sfp_socket_ops *ops);
26void sfp_unregister_socket(struct sfp_bus *bus);
27
28#endif
diff --git a/include/linux/phy.h b/include/linux/phy.h
index 0bb5b212ab42..d78cd01ea513 100644
--- a/include/linux/phy.h
+++ b/include/linux/phy.h
@@ -182,6 +182,7 @@ static inline const char *phy_modes(phy_interface_t interface)
182#define MII_ADDR_C45 (1<<30) 182#define MII_ADDR_C45 (1<<30)
183 183
184struct device; 184struct device;
185struct phylink;
185struct sk_buff; 186struct sk_buff;
186 187
187/* 188/*
@@ -469,11 +470,13 @@ struct phy_device {
469 470
470 struct mutex lock; 471 struct mutex lock;
471 472
473 struct phylink *phylink;
472 struct net_device *attached_dev; 474 struct net_device *attached_dev;
473 475
474 u8 mdix; 476 u8 mdix;
475 u8 mdix_ctrl; 477 u8 mdix_ctrl;
476 478
479 void (*phy_link_change)(struct phy_device *, bool up, bool do_carrier);
477 void (*adjust_link)(struct net_device *dev); 480 void (*adjust_link)(struct net_device *dev);
478}; 481};
479#define to_phy_device(d) container_of(to_mdio_device(d), \ 482#define to_phy_device(d) container_of(to_mdio_device(d), \
@@ -667,6 +670,24 @@ struct phy_fixup {
667 int (*run)(struct phy_device *phydev); 670 int (*run)(struct phy_device *phydev);
668}; 671};
669 672
673const char *phy_speed_to_str(int speed);
674const char *phy_duplex_to_str(unsigned int duplex);
675
676/* A structure for mapping a particular speed and duplex
677 * combination to a particular SUPPORTED and ADVERTISED value
678 */
679struct phy_setting {
680 u32 speed;
681 u8 duplex;
682 u8 bit;
683};
684
685const struct phy_setting *
686phy_lookup_setting(int speed, int duplex, const unsigned long *mask,
687 size_t maxbit, bool exact);
688size_t phy_speeds(unsigned int *speeds, size_t size,
689 unsigned long *mask, size_t maxbit);
690
670/** 691/**
671 * phy_read_mmd - Convenience function for reading a register 692 * phy_read_mmd - Convenience function for reading a register
672 * from an MMD on a given PHY. 693 * from an MMD on a given PHY.
diff --git a/include/linux/phylink.h b/include/linux/phylink.h
new file mode 100644
index 000000000000..af67edd4ae38
--- /dev/null
+++ b/include/linux/phylink.h
@@ -0,0 +1,148 @@
1#ifndef NETDEV_PCS_H
2#define NETDEV_PCS_H
3
4#include <linux/phy.h>
5#include <linux/spinlock.h>
6#include <linux/workqueue.h>
7
8struct device_node;
9struct ethtool_cmd;
10struct net_device;
11
12enum {
13 MLO_PAUSE_NONE,
14 MLO_PAUSE_ASYM = BIT(0),
15 MLO_PAUSE_SYM = BIT(1),
16 MLO_PAUSE_RX = BIT(2),
17 MLO_PAUSE_TX = BIT(3),
18 MLO_PAUSE_TXRX_MASK = MLO_PAUSE_TX | MLO_PAUSE_RX,
19 MLO_PAUSE_AN = BIT(4),
20
21 MLO_AN_PHY = 0, /* Conventional PHY */
22 MLO_AN_FIXED, /* Fixed-link mode */
23 MLO_AN_SGMII, /* Cisco SGMII protocol */
24 MLO_AN_8023Z, /* 1000base-X protocol */
25};
26
27static inline bool phylink_autoneg_inband(unsigned int mode)
28{
29 return mode == MLO_AN_SGMII || mode == MLO_AN_8023Z;
30}
31
32struct phylink_link_state {
33 __ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
34 __ETHTOOL_DECLARE_LINK_MODE_MASK(lp_advertising);
35 phy_interface_t interface; /* PHY_INTERFACE_xxx */
36 int speed;
37 int duplex;
38 int pause;
39 unsigned int link:1;
40 unsigned int an_enabled:1;
41 unsigned int an_complete:1;
42};
43
44struct phylink_mac_ops {
45 /**
46 * validate: validate and update the link configuration
47 * @ndev: net_device structure associated with MAC
48 * @config: configuration to validate
49 *
50 * Update the %config->supported and %config->advertised masks
51 * clearing bits that can not be supported.
52 *
53 * Note: the PHY may be able to transform from one connection
54 * technology to another, so, eg, don't clear 1000BaseX just
55 * because the MAC is unable to support it. This is more about
56 * clearing unsupported speeds and duplex settings.
57 *
58 * If the %config->interface mode is %PHY_INTERFACE_MODE_1000BASEX
59 * or %PHY_INTERFACE_MODE_2500BASEX, select the appropriate mode
60 * based on %config->advertised and/or %config->speed.
61 */
62 void (*validate)(struct net_device *ndev, unsigned long *supported,
63 struct phylink_link_state *state);
64
65 /* Read the current link state from the hardware */
66 int (*mac_link_state)(struct net_device *, struct phylink_link_state *);
67
68 /* Configure the MAC */
69 /**
70 * mac_config: configure the MAC for the selected mode and state
71 * @ndev: net_device structure for the MAC
72 * @mode: one of MLO_AN_FIXED, MLO_AN_PHY, MLO_AN_8023Z, MLO_AN_SGMII
73 * @state: state structure
74 *
75 * The action performed depends on the currently selected mode:
76 *
77 * %MLO_AN_FIXED, %MLO_AN_PHY:
78 * set the specified speed, duplex, pause mode, and phy interface
79 * mode in the provided @state.
80 * %MLO_AN_8023Z:
81 * place the link in 1000base-X mode, advertising the parameters
82 * given in advertising in @state.
83 * %MLO_AN_SGMII:
84 * place the link in Cisco SGMII mode - there is no advertisment
85 * to make as the PHY communicates the speed and duplex to the
86 * MAC over the in-band control word. Configuration of the pause
87 * mode is as per MLO_AN_PHY since this is not included.
88 */
89 void (*mac_config)(struct net_device *ndev, unsigned int mode,
90 const struct phylink_link_state *state);
91
92 /**
93 * mac_an_restart: restart 802.3z BaseX autonegotiation
94 * @ndev: net_device structure for the MAC
95 */
96 void (*mac_an_restart)(struct net_device *ndev);
97
98 void (*mac_link_down)(struct net_device *, unsigned int mode);
99 void (*mac_link_up)(struct net_device *, unsigned int mode,
100 struct phy_device *);
101};
102
103struct phylink *phylink_create(struct net_device *, struct device_node *,
104 phy_interface_t iface, const struct phylink_mac_ops *ops);
105void phylink_destroy(struct phylink *);
106
107int phylink_connect_phy(struct phylink *, struct phy_device *);
108int phylink_of_phy_connect(struct phylink *, struct device_node *);
109void phylink_disconnect_phy(struct phylink *);
110
111void phylink_mac_change(struct phylink *, bool up);
112
113void phylink_start(struct phylink *);
114void phylink_stop(struct phylink *);
115
116void phylink_ethtool_get_wol(struct phylink *, struct ethtool_wolinfo *);
117int phylink_ethtool_set_wol(struct phylink *, struct ethtool_wolinfo *);
118
119int phylink_ethtool_ksettings_get(struct phylink *,
120 struct ethtool_link_ksettings *);
121int phylink_ethtool_ksettings_set(struct phylink *,
122 const struct ethtool_link_ksettings *);
123int phylink_ethtool_nway_reset(struct phylink *);
124void phylink_ethtool_get_pauseparam(struct phylink *,
125 struct ethtool_pauseparam *);
126int phylink_ethtool_set_pauseparam(struct phylink *,
127 struct ethtool_pauseparam *);
128int phylink_ethtool_get_module_info(struct phylink *, struct ethtool_modinfo *);
129int phylink_ethtool_get_module_eeprom(struct phylink *,
130 struct ethtool_eeprom *, u8 *);
131int phylink_init_eee(struct phylink *, bool);
132int phylink_get_eee_err(struct phylink *);
133int phylink_ethtool_get_eee(struct phylink *, struct ethtool_eee *);
134int phylink_ethtool_set_eee(struct phylink *, struct ethtool_eee *);
135int phylink_mii_ioctl(struct phylink *, struct ifreq *, int);
136
137#define phylink_zero(bm) \
138 bitmap_zero(bm, __ETHTOOL_LINK_MODE_MASK_NBITS)
139#define __phylink_do_bit(op, bm, mode) \
140 op(ETHTOOL_LINK_MODE_ ## mode ## _BIT, bm)
141
142#define phylink_set(bm, mode) __phylink_do_bit(__set_bit, bm, mode)
143#define phylink_clear(bm, mode) __phylink_do_bit(__clear_bit, bm, mode)
144#define phylink_test(bm, mode) __phylink_do_bit(test_bit, bm, mode)
145
146void phylink_set_port_modes(unsigned long *bits);
147
148#endif
diff --git a/include/linux/sfp.h b/include/linux/sfp.h
new file mode 100644
index 000000000000..4a906f560817
--- /dev/null
+++ b/include/linux/sfp.h
@@ -0,0 +1,434 @@
1#ifndef LINUX_SFP_H
2#define LINUX_SFP_H
3
4#include <linux/phy.h>
5
6struct __packed sfp_eeprom_base {
7 u8 phys_id;
8 u8 phys_ext_id;
9 u8 connector;
10#if defined __BIG_ENDIAN_BITFIELD
11 u8 e10g_base_er:1;
12 u8 e10g_base_lrm:1;
13 u8 e10g_base_lr:1;
14 u8 e10g_base_sr:1;
15 u8 if_1x_sx:1;
16 u8 if_1x_lx:1;
17 u8 if_1x_copper_active:1;
18 u8 if_1x_copper_passive:1;
19
20 u8 escon_mmf_1310_led:1;
21 u8 escon_smf_1310_laser:1;
22 u8 sonet_oc192_short_reach:1;
23 u8 sonet_reach_bit1:1;
24 u8 sonet_reach_bit2:1;
25 u8 sonet_oc48_long_reach:1;
26 u8 sonet_oc48_intermediate_reach:1;
27 u8 sonet_oc48_short_reach:1;
28
29 u8 unallocated_5_7:1;
30 u8 sonet_oc12_smf_long_reach:1;
31 u8 sonet_oc12_smf_intermediate_reach:1;
32 u8 sonet_oc12_short_reach:1;
33 u8 unallocated_5_3:1;
34 u8 sonet_oc3_smf_long_reach:1;
35 u8 sonet_oc3_smf_intermediate_reach:1;
36 u8 sonet_oc3_short_reach:1;
37
38 u8 e_base_px:1;
39 u8 e_base_bx10:1;
40 u8 e100_base_fx:1;
41 u8 e100_base_lx:1;
42 u8 e1000_base_t:1;
43 u8 e1000_base_cx:1;
44 u8 e1000_base_lx:1;
45 u8 e1000_base_sx:1;
46
47 u8 fc_ll_v:1;
48 u8 fc_ll_s:1;
49 u8 fc_ll_i:1;
50 u8 fc_ll_l:1;
51 u8 fc_ll_m:1;
52 u8 fc_tech_sa:1;
53 u8 fc_tech_lc:1;
54 u8 fc_tech_electrical_inter_enclosure:1;
55
56 u8 fc_tech_electrical_intra_enclosure:1;
57 u8 fc_tech_sn:1;
58 u8 fc_tech_sl:1;
59 u8 fc_tech_ll:1;
60 u8 sfp_ct_active:1;
61 u8 sfp_ct_passive:1;
62 u8 unallocated_8_1:1;
63 u8 unallocated_8_0:1;
64
65 u8 fc_media_tw:1;
66 u8 fc_media_tp:1;
67 u8 fc_media_mi:1;
68 u8 fc_media_tv:1;
69 u8 fc_media_m6:1;
70 u8 fc_media_m5:1;
71 u8 unallocated_9_1:1;
72 u8 fc_media_sm:1;
73
74 u8 fc_speed_1200:1;
75 u8 fc_speed_800:1;
76 u8 fc_speed_1600:1;
77 u8 fc_speed_400:1;
78 u8 fc_speed_3200:1;
79 u8 fc_speed_200:1;
80 u8 unallocated_10_1:1;
81 u8 fc_speed_100:1;
82#elif defined __LITTLE_ENDIAN_BITFIELD
83 u8 if_1x_copper_passive:1;
84 u8 if_1x_copper_active:1;
85 u8 if_1x_lx:1;
86 u8 if_1x_sx:1;
87 u8 e10g_base_sr:1;
88 u8 e10g_base_lr:1;
89 u8 e10g_base_lrm:1;
90 u8 e10g_base_er:1;
91
92 u8 sonet_oc3_short_reach:1;
93 u8 sonet_oc3_smf_intermediate_reach:1;
94 u8 sonet_oc3_smf_long_reach:1;
95 u8 unallocated_5_3:1;
96 u8 sonet_oc12_short_reach:1;
97 u8 sonet_oc12_smf_intermediate_reach:1;
98 u8 sonet_oc12_smf_long_reach:1;
99 u8 unallocated_5_7:1;
100
101 u8 sonet_oc48_short_reach:1;
102 u8 sonet_oc48_intermediate_reach:1;
103 u8 sonet_oc48_long_reach:1;
104 u8 sonet_reach_bit2:1;
105 u8 sonet_reach_bit1:1;
106 u8 sonet_oc192_short_reach:1;
107 u8 escon_smf_1310_laser:1;
108 u8 escon_mmf_1310_led:1;
109
110 u8 e1000_base_sx:1;
111 u8 e1000_base_lx:1;
112 u8 e1000_base_cx:1;
113 u8 e1000_base_t:1;
114 u8 e100_base_lx:1;
115 u8 e100_base_fx:1;
116 u8 e_base_bx10:1;
117 u8 e_base_px:1;
118
119 u8 fc_tech_electrical_inter_enclosure:1;
120 u8 fc_tech_lc:1;
121 u8 fc_tech_sa:1;
122 u8 fc_ll_m:1;
123 u8 fc_ll_l:1;
124 u8 fc_ll_i:1;
125 u8 fc_ll_s:1;
126 u8 fc_ll_v:1;
127
128 u8 unallocated_8_0:1;
129 u8 unallocated_8_1:1;
130 u8 sfp_ct_passive:1;
131 u8 sfp_ct_active:1;
132 u8 fc_tech_ll:1;
133 u8 fc_tech_sl:1;
134 u8 fc_tech_sn:1;
135 u8 fc_tech_electrical_intra_enclosure:1;
136
137 u8 fc_media_sm:1;
138 u8 unallocated_9_1:1;
139 u8 fc_media_m5:1;
140 u8 fc_media_m6:1;
141 u8 fc_media_tv:1;
142 u8 fc_media_mi:1;
143 u8 fc_media_tp:1;
144 u8 fc_media_tw:1;
145
146 u8 fc_speed_100:1;
147 u8 unallocated_10_1:1;
148 u8 fc_speed_200:1;
149 u8 fc_speed_3200:1;
150 u8 fc_speed_400:1;
151 u8 fc_speed_1600:1;
152 u8 fc_speed_800:1;
153 u8 fc_speed_1200:1;
154#else
155#error Unknown Endian
156#endif
157 u8 encoding;
158 u8 br_nominal;
159 u8 rate_id;
160 u8 link_len[6];
161 char vendor_name[16];
162 u8 extended_cc;
163 char vendor_oui[3];
164 char vendor_pn[16];
165 char vendor_rev[4];
166 union {
167 __be16 optical_wavelength;
168 u8 cable_spec;
169 };
170 u8 reserved62;
171 u8 cc_base;
172};
173
174struct __packed sfp_eeprom_ext {
175 __be16 options;
176 u8 br_max;
177 u8 br_min;
178 char vendor_sn[16];
179 char datecode[8];
180 u8 diagmon;
181 u8 enhopts;
182 u8 sff8472_compliance;
183 u8 cc_ext;
184};
185
186struct __packed sfp_eeprom_id {
187 struct sfp_eeprom_base base;
188 struct sfp_eeprom_ext ext;
189};
190
191/* SFP EEPROM registers */
192enum {
193 SFP_PHYS_ID = 0x00,
194 SFP_PHYS_EXT_ID = 0x01,
195 SFP_CONNECTOR = 0x02,
196 SFP_COMPLIANCE = 0x03,
197 SFP_ENCODING = 0x0b,
198 SFP_BR_NOMINAL = 0x0c,
199 SFP_RATE_ID = 0x0d,
200 SFP_LINK_LEN_SM_KM = 0x0e,
201 SFP_LINK_LEN_SM_100M = 0x0f,
202 SFP_LINK_LEN_50UM_OM2_10M = 0x10,
203 SFP_LINK_LEN_62_5UM_OM1_10M = 0x11,
204 SFP_LINK_LEN_COPPER_1M = 0x12,
205 SFP_LINK_LEN_50UM_OM4_10M = 0x12,
206 SFP_LINK_LEN_50UM_OM3_10M = 0x13,
207 SFP_VENDOR_NAME = 0x14,
208 SFP_VENDOR_OUI = 0x25,
209 SFP_VENDOR_PN = 0x28,
210 SFP_VENDOR_REV = 0x38,
211 SFP_OPTICAL_WAVELENGTH_MSB = 0x3c,
212 SFP_OPTICAL_WAVELENGTH_LSB = 0x3d,
213 SFP_CABLE_SPEC = 0x3c,
214 SFP_CC_BASE = 0x3f,
215 SFP_OPTIONS = 0x40, /* 2 bytes, MSB, LSB */
216 SFP_BR_MAX = 0x42,
217 SFP_BR_MIN = 0x43,
218 SFP_VENDOR_SN = 0x44,
219 SFP_DATECODE = 0x54,
220 SFP_DIAGMON = 0x5c,
221 SFP_ENHOPTS = 0x5d,
222 SFP_SFF8472_COMPLIANCE = 0x5e,
223 SFP_CC_EXT = 0x5f,
224
225 SFP_PHYS_ID_SFP = 0x03,
226 SFP_PHYS_EXT_ID_SFP = 0x04,
227 SFP_CONNECTOR_UNSPEC = 0x00,
228 /* codes 01-05 not supportable on SFP, but some modules have single SC */
229 SFP_CONNECTOR_SC = 0x01,
230 SFP_CONNECTOR_FIBERJACK = 0x06,
231 SFP_CONNECTOR_LC = 0x07,
232 SFP_CONNECTOR_MT_RJ = 0x08,
233 SFP_CONNECTOR_MU = 0x09,
234 SFP_CONNECTOR_SG = 0x0a,
235 SFP_CONNECTOR_OPTICAL_PIGTAIL = 0x0b,
236 SFP_CONNECTOR_MPO_1X12 = 0x0c,
237 SFP_CONNECTOR_MPO_2X16 = 0x0d,
238 SFP_CONNECTOR_HSSDC_II = 0x20,
239 SFP_CONNECTOR_COPPER_PIGTAIL = 0x21,
240 SFP_CONNECTOR_RJ45 = 0x22,
241 SFP_CONNECTOR_NOSEPARATE = 0x23,
242 SFP_CONNECTOR_MXC_2X16 = 0x24,
243 SFP_ENCODING_UNSPEC = 0x00,
244 SFP_ENCODING_8B10B = 0x01,
245 SFP_ENCODING_4B5B = 0x02,
246 SFP_ENCODING_NRZ = 0x03,
247 SFP_ENCODING_8472_MANCHESTER = 0x04,
248 SFP_ENCODING_8472_SONET = 0x05,
249 SFP_ENCODING_8472_64B66B = 0x06,
250 SFP_ENCODING_256B257B = 0x07,
251 SFP_ENCODING_PAM4 = 0x08,
252 SFP_OPTIONS_HIGH_POWER_LEVEL = BIT(13),
253 SFP_OPTIONS_PAGING_A2 = BIT(12),
254 SFP_OPTIONS_RETIMER = BIT(11),
255 SFP_OPTIONS_COOLED_XCVR = BIT(10),
256 SFP_OPTIONS_POWER_DECL = BIT(9),
257 SFP_OPTIONS_RX_LINEAR_OUT = BIT(8),
258 SFP_OPTIONS_RX_DECISION_THRESH = BIT(7),
259 SFP_OPTIONS_TUNABLE_TX = BIT(6),
260 SFP_OPTIONS_RATE_SELECT = BIT(5),
261 SFP_OPTIONS_TX_DISABLE = BIT(4),
262 SFP_OPTIONS_TX_FAULT = BIT(3),
263 SFP_OPTIONS_LOS_INVERTED = BIT(2),
264 SFP_OPTIONS_LOS_NORMAL = BIT(1),
265 SFP_DIAGMON_DDM = BIT(6),
266 SFP_DIAGMON_INT_CAL = BIT(5),
267 SFP_DIAGMON_EXT_CAL = BIT(4),
268 SFP_DIAGMON_RXPWR_AVG = BIT(3),
269 SFP_DIAGMON_ADDRMODE = BIT(2),
270 SFP_ENHOPTS_ALARMWARN = BIT(7),
271 SFP_ENHOPTS_SOFT_TX_DISABLE = BIT(6),
272 SFP_ENHOPTS_SOFT_TX_FAULT = BIT(5),
273 SFP_ENHOPTS_SOFT_RX_LOS = BIT(4),
274 SFP_ENHOPTS_SOFT_RATE_SELECT = BIT(3),
275 SFP_ENHOPTS_APP_SELECT_SFF8079 = BIT(2),
276 SFP_ENHOPTS_SOFT_RATE_SFF8431 = BIT(1),
277 SFP_SFF8472_COMPLIANCE_NONE = 0x00,
278 SFP_SFF8472_COMPLIANCE_REV9_3 = 0x01,
279 SFP_SFF8472_COMPLIANCE_REV9_5 = 0x02,
280 SFP_SFF8472_COMPLIANCE_REV10_2 = 0x03,
281 SFP_SFF8472_COMPLIANCE_REV10_4 = 0x04,
282 SFP_SFF8472_COMPLIANCE_REV11_0 = 0x05,
283 SFP_SFF8472_COMPLIANCE_REV11_3 = 0x06,
284 SFP_SFF8472_COMPLIANCE_REV11_4 = 0x07,
285 SFP_SFF8472_COMPLIANCE_REV12_0 = 0x08,
286};
287
288/* SFP Diagnostics */
289enum {
290 /* Alarm and warnings stored MSB at lower address then LSB */
291 SFP_TEMP_HIGH_ALARM = 0x00,
292 SFP_TEMP_LOW_ALARM = 0x02,
293 SFP_TEMP_HIGH_WARN = 0x04,
294 SFP_TEMP_LOW_WARN = 0x06,
295 SFP_VOLT_HIGH_ALARM = 0x08,
296 SFP_VOLT_LOW_ALARM = 0x0a,
297 SFP_VOLT_HIGH_WARN = 0x0c,
298 SFP_VOLT_LOW_WARN = 0x0e,
299 SFP_BIAS_HIGH_ALARM = 0x10,
300 SFP_BIAS_LOW_ALARM = 0x12,
301 SFP_BIAS_HIGH_WARN = 0x14,
302 SFP_BIAS_LOW_WARN = 0x16,
303 SFP_TXPWR_HIGH_ALARM = 0x18,
304 SFP_TXPWR_LOW_ALARM = 0x1a,
305 SFP_TXPWR_HIGH_WARN = 0x1c,
306 SFP_TXPWR_LOW_WARN = 0x1e,
307 SFP_RXPWR_HIGH_ALARM = 0x20,
308 SFP_RXPWR_LOW_ALARM = 0x22,
309 SFP_RXPWR_HIGH_WARN = 0x24,
310 SFP_RXPWR_LOW_WARN = 0x26,
311 SFP_LASER_TEMP_HIGH_ALARM = 0x28,
312 SFP_LASER_TEMP_LOW_ALARM = 0x2a,
313 SFP_LASER_TEMP_HIGH_WARN = 0x2c,
314 SFP_LASER_TEMP_LOW_WARN = 0x2e,
315 SFP_TEC_CUR_HIGH_ALARM = 0x30,
316 SFP_TEC_CUR_LOW_ALARM = 0x32,
317 SFP_TEC_CUR_HIGH_WARN = 0x34,
318 SFP_TEC_CUR_LOW_WARN = 0x36,
319 SFP_CAL_RXPWR4 = 0x38,
320 SFP_CAL_RXPWR3 = 0x3c,
321 SFP_CAL_RXPWR2 = 0x40,
322 SFP_CAL_RXPWR1 = 0x44,
323 SFP_CAL_RXPWR0 = 0x48,
324 SFP_CAL_TXI_SLOPE = 0x4c,
325 SFP_CAL_TXI_OFFSET = 0x4e,
326 SFP_CAL_TXPWR_SLOPE = 0x50,
327 SFP_CAL_TXPWR_OFFSET = 0x52,
328 SFP_CAL_T_SLOPE = 0x54,
329 SFP_CAL_T_OFFSET = 0x56,
330 SFP_CAL_V_SLOPE = 0x58,
331 SFP_CAL_V_OFFSET = 0x5a,
332 SFP_CHKSUM = 0x5f,
333
334 SFP_TEMP = 0x60,
335 SFP_VCC = 0x62,
336 SFP_TX_BIAS = 0x64,
337 SFP_TX_POWER = 0x66,
338 SFP_RX_POWER = 0x68,
339 SFP_LASER_TEMP = 0x6a,
340 SFP_TEC_CUR = 0x6c,
341
342 SFP_STATUS = 0x6e,
343 SFP_ALARM = 0x70,
344
345 SFP_EXT_STATUS = 0x76,
346 SFP_VSL = 0x78,
347 SFP_PAGE = 0x7f,
348};
349
350struct device_node;
351struct ethtool_eeprom;
352struct ethtool_modinfo;
353struct net_device;
354struct sfp_bus;
355
356struct sfp_upstream_ops {
357 int (*module_insert)(void *, const struct sfp_eeprom_id *id);
358 void (*module_remove)(void *);
359 void (*link_down)(void *);
360 void (*link_up)(void *);
361 int (*connect_phy)(void *, struct phy_device *);
362 void (*disconnect_phy)(void *);
363};
364
365#if IS_ENABLED(CONFIG_SFP)
366int sfp_parse_port(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
367 unsigned long *support);
368phy_interface_t sfp_parse_interface(struct sfp_bus *bus,
369 const struct sfp_eeprom_id *id);
370void sfp_parse_support(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
371 unsigned long *support);
372
373int sfp_get_module_info(struct sfp_bus *bus, struct ethtool_modinfo *modinfo);
374int sfp_get_module_eeprom(struct sfp_bus *bus, struct ethtool_eeprom *ee,
375 u8 *data);
376void sfp_upstream_start(struct sfp_bus *bus);
377void sfp_upstream_stop(struct sfp_bus *bus);
378struct sfp_bus *sfp_register_upstream(struct device_node *np,
379 struct net_device *ndev, void *upstream,
380 const struct sfp_upstream_ops *ops);
381void sfp_unregister_upstream(struct sfp_bus *bus);
382#else
383static inline int sfp_parse_port(struct sfp_bus *bus,
384 const struct sfp_eeprom_id *id,
385 unsigned long *support)
386{
387 return PORT_OTHER;
388}
389
390static inline phy_interface_t sfp_parse_interface(struct sfp_bus *bus,
391 const struct sfp_eeprom_id *id)
392{
393 return PHY_INTERFACE_MODE_NA;
394}
395
396static inline void sfp_parse_support(struct sfp_bus *bus,
397 const struct sfp_eeprom_id *id,
398 unsigned long *support)
399{
400}
401
402static inline int sfp_get_module_info(struct sfp_bus *bus,
403 struct ethtool_modinfo *modinfo)
404{
405 return -EOPNOTSUPP;
406}
407
408static inline int sfp_get_module_eeprom(struct sfp_bus *bus,
409 struct ethtool_eeprom *ee, u8 *data)
410{
411 return -EOPNOTSUPP;
412}
413
414static inline void sfp_upstream_start(struct sfp_bus *bus)
415{
416}
417
418static inline void sfp_upstream_stop(struct sfp_bus *bus)
419{
420}
421
422static inline struct sfp_bus *sfp_register_upstream(struct device_node *np,
423 struct net_device *ndev, void *upstream,
424 const struct sfp_upstream_ops *ops)
425{
426 return (struct sfp_bus *)-1;
427}
428
429static inline void sfp_unregister_upstream(struct sfp_bus *bus)
430{
431}
432#endif
433
434#endif