blob: 5a283bf9d4026b5da2877b78c15591b7706d3c19 [file] [log] [blame]
Andrew Lunn5f857572019-01-21 19:10:19 +01001// SPDX-License-Identifier: GPL-2.0
Russell King9525ae82017-07-25 15:03:13 +01002/*
3 * phylink models the MAC to optional PHY connection, supporting
4 * technologies such as SFP cages where the PHY is hot-pluggable.
5 *
6 * Copyright (C) 2015 Russell King
Russell King9525ae82017-07-25 15:03:13 +01007 */
8#include <linux/ethtool.h>
9#include <linux/export.h>
10#include <linux/gpio/consumer.h>
11#include <linux/netdevice.h>
12#include <linux/of.h>
13#include <linux/of_mdio.h>
14#include <linux/phy.h>
15#include <linux/phy_fixed.h>
16#include <linux/phylink.h>
17#include <linux/rtnetlink.h>
18#include <linux/spinlock.h>
Russell King9cd00a82018-05-10 13:17:31 -070019#include <linux/timer.h>
Russell King9525ae82017-07-25 15:03:13 +010020#include <linux/workqueue.h>
21
Russell Kingce0aa272017-07-25 15:03:18 +010022#include "sfp.h"
Russell King9525ae82017-07-25 15:03:13 +010023#include "swphy.h"
24
25#define SUPPORTED_INTERFACES \
26 (SUPPORTED_TP | SUPPORTED_MII | SUPPORTED_FIBRE | \
27 SUPPORTED_BNC | SUPPORTED_AUI | SUPPORTED_Backplane)
28#define ADVERTISED_INTERFACES \
29 (ADVERTISED_TP | ADVERTISED_MII | ADVERTISED_FIBRE | \
30 ADVERTISED_BNC | ADVERTISED_AUI | ADVERTISED_Backplane)
31
32enum {
33 PHYLINK_DISABLE_STOPPED,
Russell Kingce0aa272017-07-25 15:03:18 +010034 PHYLINK_DISABLE_LINK,
Russell King9525ae82017-07-25 15:03:13 +010035};
36
Russell King8796c892017-12-01 10:24:47 +000037/**
38 * struct phylink - internal data type for phylink
39 */
Russell King9525ae82017-07-25 15:03:13 +010040struct phylink {
Russell King8796c892017-12-01 10:24:47 +000041 /* private: */
Russell King9525ae82017-07-25 15:03:13 +010042 struct net_device *netdev;
43 const struct phylink_mac_ops *ops;
Ioana Ciornei44cc27e2019-05-28 20:38:12 +030044 struct phylink_config *config;
Russell King9525ae82017-07-25 15:03:13 +010045
46 unsigned long phylink_disable_state; /* bitmask of disables */
47 struct phy_device *phydev;
48 phy_interface_t link_interface; /* PHY_INTERFACE_xxx */
49 u8 link_an_mode; /* MLO_AN_xxx */
50 u8 link_port; /* The current non-phy ethtool port */
51 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
52
53 /* The link configuration settings */
54 struct phylink_link_state link_config;
55 struct gpio_desc *link_gpio;
Russell King9cd00a82018-05-10 13:17:31 -070056 struct timer_list link_poll;
Florian Fainelli1ac63e32017-12-12 16:00:28 -080057 void (*get_fixed_state)(struct net_device *dev,
58 struct phylink_link_state *s);
Russell King9525ae82017-07-25 15:03:13 +010059
60 struct mutex state_mutex;
61 struct phylink_link_state phy_state;
62 struct work_struct resolve;
63
64 bool mac_link_dropped;
Russell Kingce0aa272017-07-25 15:03:18 +010065
66 struct sfp_bus *sfp_bus;
Russell King9525ae82017-07-25 15:03:13 +010067};
68
Russell King8796c892017-12-01 10:24:47 +000069/**
70 * phylink_set_port_modes() - set the port type modes in the ethtool mask
71 * @mask: ethtool link mode mask
72 *
73 * Sets all the port type modes in the ethtool mask. MAC drivers should
74 * use this in their 'validate' callback.
75 */
Russell King9525ae82017-07-25 15:03:13 +010076void phylink_set_port_modes(unsigned long *mask)
77{
78 phylink_set(mask, TP);
79 phylink_set(mask, AUI);
80 phylink_set(mask, MII);
81 phylink_set(mask, FIBRE);
82 phylink_set(mask, BNC);
83 phylink_set(mask, Backplane);
84}
85EXPORT_SYMBOL_GPL(phylink_set_port_modes);
86
87static int phylink_is_empty_linkmode(const unsigned long *linkmode)
88{
89 __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp) = { 0, };
90
91 phylink_set_port_modes(tmp);
92 phylink_set(tmp, Autoneg);
93 phylink_set(tmp, Pause);
94 phylink_set(tmp, Asym_Pause);
95
96 bitmap_andnot(tmp, linkmode, tmp, __ETHTOOL_LINK_MODE_MASK_NBITS);
97
98 return linkmode_empty(tmp);
99}
100
101static const char *phylink_an_mode_str(unsigned int mode)
102{
103 static const char *modestr[] = {
104 [MLO_AN_PHY] = "phy",
105 [MLO_AN_FIXED] = "fixed",
Russell King86a362c2017-12-01 10:24:26 +0000106 [MLO_AN_INBAND] = "inband",
Russell King9525ae82017-07-25 15:03:13 +0100107 };
108
109 return mode < ARRAY_SIZE(modestr) ? modestr[mode] : "unknown";
110}
111
112static int phylink_validate(struct phylink *pl, unsigned long *supported,
113 struct phylink_link_state *state)
114{
Ioana Ciornei44cc27e2019-05-28 20:38:12 +0300115 pl->ops->validate(pl->config, supported, state);
Russell King9525ae82017-07-25 15:03:13 +0100116
117 return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
118}
119
Russell King8fa7b9b2017-12-01 10:25:09 +0000120static int phylink_parse_fixedlink(struct phylink *pl,
121 struct fwnode_handle *fwnode)
Russell King9525ae82017-07-25 15:03:13 +0100122{
Russell King8fa7b9b2017-12-01 10:25:09 +0000123 struct fwnode_handle *fixed_node;
Russell King9525ae82017-07-25 15:03:13 +0100124 const struct phy_setting *s;
125 struct gpio_desc *desc;
Russell King9525ae82017-07-25 15:03:13 +0100126 u32 speed;
Russell King8fa7b9b2017-12-01 10:25:09 +0000127 int ret;
Russell King9525ae82017-07-25 15:03:13 +0100128
Russell King8fa7b9b2017-12-01 10:25:09 +0000129 fixed_node = fwnode_get_named_child_node(fwnode, "fixed-link");
Russell King9525ae82017-07-25 15:03:13 +0100130 if (fixed_node) {
Russell King8fa7b9b2017-12-01 10:25:09 +0000131 ret = fwnode_property_read_u32(fixed_node, "speed", &speed);
Russell King9525ae82017-07-25 15:03:13 +0100132
133 pl->link_config.speed = speed;
134 pl->link_config.duplex = DUPLEX_HALF;
135
Russell King8fa7b9b2017-12-01 10:25:09 +0000136 if (fwnode_property_read_bool(fixed_node, "full-duplex"))
Russell King9525ae82017-07-25 15:03:13 +0100137 pl->link_config.duplex = DUPLEX_FULL;
138
139 /* We treat the "pause" and "asym-pause" terminology as
140 * defining the link partner's ability. */
Russell King8fa7b9b2017-12-01 10:25:09 +0000141 if (fwnode_property_read_bool(fixed_node, "pause"))
Russell King9525ae82017-07-25 15:03:13 +0100142 pl->link_config.pause |= MLO_PAUSE_SYM;
Russell King8fa7b9b2017-12-01 10:25:09 +0000143 if (fwnode_property_read_bool(fixed_node, "asym-pause"))
Russell King9525ae82017-07-25 15:03:13 +0100144 pl->link_config.pause |= MLO_PAUSE_ASYM;
145
146 if (ret == 0) {
Russell King8fa7b9b2017-12-01 10:25:09 +0000147 desc = fwnode_get_named_gpiod(fixed_node, "link-gpios",
148 0, GPIOD_IN, "?");
Russell King9525ae82017-07-25 15:03:13 +0100149
150 if (!IS_ERR(desc))
151 pl->link_gpio = desc;
152 else if (desc == ERR_PTR(-EPROBE_DEFER))
153 ret = -EPROBE_DEFER;
154 }
Russell King8fa7b9b2017-12-01 10:25:09 +0000155 fwnode_handle_put(fixed_node);
Russell King9525ae82017-07-25 15:03:13 +0100156
157 if (ret)
158 return ret;
159 } else {
Russell King8fa7b9b2017-12-01 10:25:09 +0000160 u32 prop[5];
161
162 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
163 NULL, 0);
164 if (ret != ARRAY_SIZE(prop)) {
Russell King9525ae82017-07-25 15:03:13 +0100165 netdev_err(pl->netdev, "broken fixed-link?\n");
166 return -EINVAL;
167 }
Russell King8fa7b9b2017-12-01 10:25:09 +0000168
169 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
170 prop, ARRAY_SIZE(prop));
171 if (!ret) {
172 pl->link_config.duplex = prop[1] ?
Russell King9525ae82017-07-25 15:03:13 +0100173 DUPLEX_FULL : DUPLEX_HALF;
Russell King8fa7b9b2017-12-01 10:25:09 +0000174 pl->link_config.speed = prop[2];
175 if (prop[3])
Russell King9525ae82017-07-25 15:03:13 +0100176 pl->link_config.pause |= MLO_PAUSE_SYM;
Russell King8fa7b9b2017-12-01 10:25:09 +0000177 if (prop[4])
Russell King9525ae82017-07-25 15:03:13 +0100178 pl->link_config.pause |= MLO_PAUSE_ASYM;
179 }
180 }
181
182 if (pl->link_config.speed > SPEED_1000 &&
183 pl->link_config.duplex != DUPLEX_FULL)
184 netdev_warn(pl->netdev, "fixed link specifies half duplex for %dMbps link?\n",
185 pl->link_config.speed);
186
187 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
188 linkmode_copy(pl->link_config.advertising, pl->supported);
189 phylink_validate(pl, pl->supported, &pl->link_config);
190
191 s = phy_lookup_setting(pl->link_config.speed, pl->link_config.duplex,
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100192 pl->supported, true);
Russell King9525ae82017-07-25 15:03:13 +0100193 linkmode_zero(pl->supported);
194 phylink_set(pl->supported, MII);
195 if (s) {
196 __set_bit(s->bit, pl->supported);
197 } else {
198 netdev_warn(pl->netdev, "fixed link %s duplex %dMbps not recognised\n",
199 pl->link_config.duplex == DUPLEX_FULL ? "full" : "half",
200 pl->link_config.speed);
201 }
202
203 linkmode_and(pl->link_config.advertising, pl->link_config.advertising,
204 pl->supported);
205
206 pl->link_config.link = 1;
207 pl->link_config.an_complete = 1;
208
209 return 0;
210}
211
Russell King8fa7b9b2017-12-01 10:25:09 +0000212static int phylink_parse_mode(struct phylink *pl, struct fwnode_handle *fwnode)
Russell King9525ae82017-07-25 15:03:13 +0100213{
Russell King8fa7b9b2017-12-01 10:25:09 +0000214 struct fwnode_handle *dn;
Russell King9525ae82017-07-25 15:03:13 +0100215 const char *managed;
216
Russell King8fa7b9b2017-12-01 10:25:09 +0000217 dn = fwnode_get_named_child_node(fwnode, "fixed-link");
218 if (dn || fwnode_property_present(fwnode, "fixed-link"))
Russell King9525ae82017-07-25 15:03:13 +0100219 pl->link_an_mode = MLO_AN_FIXED;
Russell King8fa7b9b2017-12-01 10:25:09 +0000220 fwnode_handle_put(dn);
Russell King9525ae82017-07-25 15:03:13 +0100221
Russell King8fa7b9b2017-12-01 10:25:09 +0000222 if (fwnode_property_read_string(fwnode, "managed", &managed) == 0 &&
Russell King9525ae82017-07-25 15:03:13 +0100223 strcmp(managed, "in-band-status") == 0) {
224 if (pl->link_an_mode == MLO_AN_FIXED) {
225 netdev_err(pl->netdev,
226 "can't use both fixed-link and in-band-status\n");
227 return -EINVAL;
228 }
229
230 linkmode_zero(pl->supported);
231 phylink_set(pl->supported, MII);
232 phylink_set(pl->supported, Autoneg);
233 phylink_set(pl->supported, Asym_Pause);
234 phylink_set(pl->supported, Pause);
235 pl->link_config.an_enabled = true;
Russell King86a362c2017-12-01 10:24:26 +0000236 pl->link_an_mode = MLO_AN_INBAND;
Russell King9525ae82017-07-25 15:03:13 +0100237
238 switch (pl->link_config.interface) {
239 case PHY_INTERFACE_MODE_SGMII:
240 phylink_set(pl->supported, 10baseT_Half);
241 phylink_set(pl->supported, 10baseT_Full);
242 phylink_set(pl->supported, 100baseT_Half);
243 phylink_set(pl->supported, 100baseT_Full);
244 phylink_set(pl->supported, 1000baseT_Half);
245 phylink_set(pl->supported, 1000baseT_Full);
Russell King9525ae82017-07-25 15:03:13 +0100246 break;
247
248 case PHY_INTERFACE_MODE_1000BASEX:
249 phylink_set(pl->supported, 1000baseX_Full);
Russell King9525ae82017-07-25 15:03:13 +0100250 break;
251
252 case PHY_INTERFACE_MODE_2500BASEX:
253 phylink_set(pl->supported, 2500baseX_Full);
Russell King9525ae82017-07-25 15:03:13 +0100254 break;
255
Russell Kingda7c1862017-07-25 15:03:34 +0100256 case PHY_INTERFACE_MODE_10GKR:
257 phylink_set(pl->supported, 10baseT_Half);
258 phylink_set(pl->supported, 10baseT_Full);
259 phylink_set(pl->supported, 100baseT_Half);
260 phylink_set(pl->supported, 100baseT_Full);
261 phylink_set(pl->supported, 1000baseT_Half);
262 phylink_set(pl->supported, 1000baseT_Full);
263 phylink_set(pl->supported, 1000baseX_Full);
264 phylink_set(pl->supported, 10000baseKR_Full);
265 phylink_set(pl->supported, 10000baseCR_Full);
266 phylink_set(pl->supported, 10000baseSR_Full);
267 phylink_set(pl->supported, 10000baseLR_Full);
268 phylink_set(pl->supported, 10000baseLRM_Full);
269 phylink_set(pl->supported, 10000baseER_Full);
Russell Kingda7c1862017-07-25 15:03:34 +0100270 break;
271
Russell King9525ae82017-07-25 15:03:13 +0100272 default:
273 netdev_err(pl->netdev,
274 "incorrect link mode %s for in-band status\n",
275 phy_modes(pl->link_config.interface));
276 return -EINVAL;
277 }
278
279 linkmode_copy(pl->link_config.advertising, pl->supported);
280
281 if (phylink_validate(pl, pl->supported, &pl->link_config)) {
282 netdev_err(pl->netdev,
283 "failed to validate link configuration for in-band status\n");
284 return -EINVAL;
285 }
286 }
287
288 return 0;
289}
290
291static void phylink_mac_config(struct phylink *pl,
292 const struct phylink_link_state *state)
293{
294 netdev_dbg(pl->netdev,
295 "%s: mode=%s/%s/%s/%s adv=%*pb pause=%02x link=%u an=%u\n",
296 __func__, phylink_an_mode_str(pl->link_an_mode),
297 phy_modes(state->interface),
298 phy_speed_to_str(state->speed),
299 phy_duplex_to_str(state->duplex),
300 __ETHTOOL_LINK_MODE_MASK_NBITS, state->advertising,
301 state->pause, state->link, state->an_enabled);
302
Ioana Ciornei44cc27e2019-05-28 20:38:12 +0300303 pl->ops->mac_config(pl->config, pl->link_an_mode, state);
Russell King9525ae82017-07-25 15:03:13 +0100304}
305
Russell King0946cf12019-02-11 11:46:01 +0000306static void phylink_mac_config_up(struct phylink *pl,
307 const struct phylink_link_state *state)
308{
309 if (state->link)
310 phylink_mac_config(pl, state);
311}
312
Russell King9525ae82017-07-25 15:03:13 +0100313static void phylink_mac_an_restart(struct phylink *pl)
314{
315 if (pl->link_config.an_enabled &&
Russell King365c1e62017-12-01 10:24:16 +0000316 phy_interface_mode_is_8023z(pl->link_config.interface))
Ioana Ciornei44cc27e2019-05-28 20:38:12 +0300317 pl->ops->mac_an_restart(pl->config);
Russell King9525ae82017-07-25 15:03:13 +0100318}
319
320static int phylink_get_mac_state(struct phylink *pl, struct phylink_link_state *state)
321{
Russell King9525ae82017-07-25 15:03:13 +0100322
323 linkmode_copy(state->advertising, pl->link_config.advertising);
324 linkmode_zero(state->lp_advertising);
325 state->interface = pl->link_config.interface;
326 state->an_enabled = pl->link_config.an_enabled;
Heiner Kallweitd25ed412019-02-26 19:29:22 +0100327 state->speed = SPEED_UNKNOWN;
328 state->duplex = DUPLEX_UNKNOWN;
329 state->pause = MLO_PAUSE_NONE;
330 state->an_complete = 0;
Russell King9525ae82017-07-25 15:03:13 +0100331 state->link = 1;
332
Ioana Ciornei44cc27e2019-05-28 20:38:12 +0300333 return pl->ops->mac_link_state(pl->config, state);
Russell King9525ae82017-07-25 15:03:13 +0100334}
335
336/* The fixed state is... fixed except for the link state,
Florian Fainelli1ac63e32017-12-12 16:00:28 -0800337 * which may be determined by a GPIO or a callback.
Russell King9525ae82017-07-25 15:03:13 +0100338 */
339static void phylink_get_fixed_state(struct phylink *pl, struct phylink_link_state *state)
340{
341 *state = pl->link_config;
Florian Fainelli1ac63e32017-12-12 16:00:28 -0800342 if (pl->get_fixed_state)
343 pl->get_fixed_state(pl->netdev, state);
344 else if (pl->link_gpio)
Florian Fainellibb322a92018-05-10 13:17:29 -0700345 state->link = !!gpiod_get_value_cansleep(pl->link_gpio);
Russell King9525ae82017-07-25 15:03:13 +0100346}
347
348/* Flow control is resolved according to our and the link partners
Colin Ian Kingcc1122b2018-03-01 10:23:03 +0000349 * advertisements using the following drawn from the 802.3 specs:
Russell King9525ae82017-07-25 15:03:13 +0100350 * Local device Link partner
351 * Pause AsymDir Pause AsymDir Result
352 * 1 X 1 X TX+RX
353 * 0 1 1 1 RX
354 * 1 1 0 1 TX
355 */
356static void phylink_resolve_flow(struct phylink *pl,
Florian Fainelli516b29e2017-10-30 21:42:57 -0700357 struct phylink_link_state *state)
Russell King9525ae82017-07-25 15:03:13 +0100358{
359 int new_pause = 0;
360
361 if (pl->link_config.pause & MLO_PAUSE_AN) {
362 int pause = 0;
363
364 if (phylink_test(pl->link_config.advertising, Pause))
365 pause |= MLO_PAUSE_SYM;
366 if (phylink_test(pl->link_config.advertising, Asym_Pause))
367 pause |= MLO_PAUSE_ASYM;
368
369 pause &= state->pause;
370
371 if (pause & MLO_PAUSE_SYM)
372 new_pause = MLO_PAUSE_TX | MLO_PAUSE_RX;
373 else if (pause & MLO_PAUSE_ASYM)
374 new_pause = state->pause & MLO_PAUSE_SYM ?
375 MLO_PAUSE_RX : MLO_PAUSE_TX;
376 } else {
377 new_pause = pl->link_config.pause & MLO_PAUSE_TXRX_MASK;
378 }
379
380 state->pause &= ~MLO_PAUSE_TXRX_MASK;
381 state->pause |= new_pause;
382}
383
384static const char *phylink_pause_to_str(int pause)
385{
386 switch (pause & MLO_PAUSE_TXRX_MASK) {
387 case MLO_PAUSE_TX | MLO_PAUSE_RX:
388 return "rx/tx";
389 case MLO_PAUSE_TX:
390 return "tx";
391 case MLO_PAUSE_RX:
392 return "rx";
393 default:
394 return "off";
395 }
396}
397
Ioana Ciornei27755ff2019-05-28 20:38:11 +0300398static void phylink_mac_link_up(struct phylink *pl,
399 struct phylink_link_state link_state)
400{
401 struct net_device *ndev = pl->netdev;
402
Ioana Ciornei44cc27e2019-05-28 20:38:12 +0300403 pl->ops->mac_link_up(pl->config, pl->link_an_mode,
Ioana Ciornei27755ff2019-05-28 20:38:11 +0300404 pl->phy_state.interface,
405 pl->phydev);
406
407 netif_carrier_on(ndev);
408
409 netdev_info(ndev,
410 "Link is Up - %s/%s - flow control %s\n",
411 phy_speed_to_str(link_state.speed),
412 phy_duplex_to_str(link_state.duplex),
413 phylink_pause_to_str(link_state.pause));
414}
415
416static void phylink_mac_link_down(struct phylink *pl)
417{
418 struct net_device *ndev = pl->netdev;
419
420 netif_carrier_off(ndev);
Ioana Ciornei44cc27e2019-05-28 20:38:12 +0300421 pl->ops->mac_link_down(pl->config, pl->link_an_mode,
Ioana Ciornei27755ff2019-05-28 20:38:11 +0300422 pl->phy_state.interface);
423 netdev_info(ndev, "Link is Down\n");
424}
425
Russell King9525ae82017-07-25 15:03:13 +0100426static void phylink_resolve(struct work_struct *w)
427{
428 struct phylink *pl = container_of(w, struct phylink, resolve);
429 struct phylink_link_state link_state;
430 struct net_device *ndev = pl->netdev;
431
432 mutex_lock(&pl->state_mutex);
433 if (pl->phylink_disable_state) {
434 pl->mac_link_dropped = false;
435 link_state.link = false;
436 } else if (pl->mac_link_dropped) {
437 link_state.link = false;
438 } else {
439 switch (pl->link_an_mode) {
440 case MLO_AN_PHY:
441 link_state = pl->phy_state;
442 phylink_resolve_flow(pl, &link_state);
Russell King0946cf12019-02-11 11:46:01 +0000443 phylink_mac_config_up(pl, &link_state);
Russell King9525ae82017-07-25 15:03:13 +0100444 break;
445
446 case MLO_AN_FIXED:
447 phylink_get_fixed_state(pl, &link_state);
Russell King0946cf12019-02-11 11:46:01 +0000448 phylink_mac_config_up(pl, &link_state);
Russell King9525ae82017-07-25 15:03:13 +0100449 break;
450
Russell King86a362c2017-12-01 10:24:26 +0000451 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +0100452 phylink_get_mac_state(pl, &link_state);
Russell King9525ae82017-07-25 15:03:13 +0100453
Russell King406cb0c2019-05-20 16:07:20 +0100454 /* If we have a phy, the "up" state is the union of
455 * both the PHY and the MAC */
456 if (pl->phydev)
457 link_state.link &= pl->phy_state.link;
Russell King9525ae82017-07-25 15:03:13 +0100458
Russell King406cb0c2019-05-20 16:07:20 +0100459 /* Only update if the PHY link is up */
460 if (pl->phydev && pl->phy_state.link) {
461 link_state.interface = pl->phy_state.interface;
Russell King9525ae82017-07-25 15:03:13 +0100462
Russell King406cb0c2019-05-20 16:07:20 +0100463 /* If we have a PHY, we need to update with
464 * the pause mode bits. */
465 link_state.pause |= pl->phy_state.pause;
466 phylink_resolve_flow(pl, &link_state);
467 phylink_mac_config(pl, &link_state);
Russell King9525ae82017-07-25 15:03:13 +0100468 }
469 break;
Russell King9525ae82017-07-25 15:03:13 +0100470 }
471 }
472
473 if (link_state.link != netif_carrier_ok(ndev)) {
Ioana Ciornei27755ff2019-05-28 20:38:11 +0300474 if (!link_state.link)
475 phylink_mac_link_down(pl);
476 else
477 phylink_mac_link_up(pl, link_state);
Russell King9525ae82017-07-25 15:03:13 +0100478 }
479 if (!link_state.link && pl->mac_link_dropped) {
480 pl->mac_link_dropped = false;
481 queue_work(system_power_efficient_wq, &pl->resolve);
482 }
483 mutex_unlock(&pl->state_mutex);
484}
485
486static void phylink_run_resolve(struct phylink *pl)
487{
488 if (!pl->phylink_disable_state)
489 queue_work(system_power_efficient_wq, &pl->resolve);
490}
491
Russell King87454b62019-02-11 15:04:24 +0000492static void phylink_run_resolve_and_disable(struct phylink *pl, int bit)
493{
494 unsigned long state = pl->phylink_disable_state;
495
496 set_bit(bit, &pl->phylink_disable_state);
497 if (state == 0) {
498 queue_work(system_power_efficient_wq, &pl->resolve);
499 flush_work(&pl->resolve);
500 }
501}
502
Russell King9cd00a82018-05-10 13:17:31 -0700503static void phylink_fixed_poll(struct timer_list *t)
504{
505 struct phylink *pl = container_of(t, struct phylink, link_poll);
506
507 mod_timer(t, jiffies + HZ);
508
509 phylink_run_resolve(pl);
510}
511
Russell Kingce0aa272017-07-25 15:03:18 +0100512static const struct sfp_upstream_ops sfp_phylink_ops;
513
Russell King8fa7b9b2017-12-01 10:25:09 +0000514static int phylink_register_sfp(struct phylink *pl,
515 struct fwnode_handle *fwnode)
Russell Kingce0aa272017-07-25 15:03:18 +0100516{
Russell King8fa7b9b2017-12-01 10:25:09 +0000517 struct fwnode_reference_args ref;
518 int ret;
Russell Kingce0aa272017-07-25 15:03:18 +0100519
Florian Fainelli02477802017-12-14 15:57:58 -0800520 if (!fwnode)
521 return 0;
522
Russell King8fa7b9b2017-12-01 10:25:09 +0000523 ret = fwnode_property_get_reference_args(fwnode, "sfp", NULL,
524 0, 0, &ref);
525 if (ret < 0) {
526 if (ret == -ENOENT)
527 return 0;
Russell Kingce0aa272017-07-25 15:03:18 +0100528
Russell King8fa7b9b2017-12-01 10:25:09 +0000529 netdev_err(pl->netdev, "unable to parse \"sfp\" node: %d\n",
530 ret);
531 return ret;
532 }
533
534 pl->sfp_bus = sfp_register_upstream(ref.fwnode, pl->netdev, pl,
Russell Kingce0aa272017-07-25 15:03:18 +0100535 &sfp_phylink_ops);
536 if (!pl->sfp_bus)
537 return -ENOMEM;
538
539 return 0;
540}
541
Russell King8796c892017-12-01 10:24:47 +0000542/**
543 * phylink_create() - create a phylink instance
544 * @ndev: a pointer to the &struct net_device
Russell King8fa7b9b2017-12-01 10:25:09 +0000545 * @fwnode: a pointer to a &struct fwnode_handle describing the network
546 * interface
Russell King8796c892017-12-01 10:24:47 +0000547 * @iface: the desired link mode defined by &typedef phy_interface_t
548 * @ops: a pointer to a &struct phylink_mac_ops for the MAC.
549 *
550 * Create a new phylink instance, and parse the link parameters found in @np.
551 * This will parse in-band modes, fixed-link or SFP configuration.
552 *
553 * Returns a pointer to a &struct phylink, or an error-pointer value. Users
554 * must use IS_ERR() to check for errors from this function.
555 */
Ioana Ciornei44cc27e2019-05-28 20:38:12 +0300556struct phylink *phylink_create(struct phylink_config *config,
Russell King8fa7b9b2017-12-01 10:25:09 +0000557 struct fwnode_handle *fwnode,
Florian Fainelli516b29e2017-10-30 21:42:57 -0700558 phy_interface_t iface,
559 const struct phylink_mac_ops *ops)
Russell King9525ae82017-07-25 15:03:13 +0100560{
561 struct phylink *pl;
562 int ret;
563
564 pl = kzalloc(sizeof(*pl), GFP_KERNEL);
565 if (!pl)
566 return ERR_PTR(-ENOMEM);
567
568 mutex_init(&pl->state_mutex);
569 INIT_WORK(&pl->resolve, phylink_resolve);
Ioana Ciornei44cc27e2019-05-28 20:38:12 +0300570
571 pl->config = config;
572 if (config->type == PHYLINK_NETDEV) {
573 pl->netdev = to_net_dev(config->dev);
574 } else {
575 kfree(pl);
576 return ERR_PTR(-EINVAL);
577 }
578
Russell King9525ae82017-07-25 15:03:13 +0100579 pl->phy_state.interface = iface;
580 pl->link_interface = iface;
Florian Fainelli4be11ef2017-12-12 16:00:29 -0800581 if (iface == PHY_INTERFACE_MODE_MOCA)
582 pl->link_port = PORT_BNC;
583 else
584 pl->link_port = PORT_MII;
Russell King9525ae82017-07-25 15:03:13 +0100585 pl->link_config.interface = iface;
586 pl->link_config.pause = MLO_PAUSE_AN;
587 pl->link_config.speed = SPEED_UNKNOWN;
588 pl->link_config.duplex = DUPLEX_UNKNOWN;
Russell King74ee0e82017-12-20 23:21:34 +0000589 pl->link_config.an_enabled = true;
Russell King9525ae82017-07-25 15:03:13 +0100590 pl->ops = ops;
591 __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
Russell King9cd00a82018-05-10 13:17:31 -0700592 timer_setup(&pl->link_poll, phylink_fixed_poll, 0);
Russell King9525ae82017-07-25 15:03:13 +0100593
594 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
595 linkmode_copy(pl->link_config.advertising, pl->supported);
596 phylink_validate(pl, pl->supported, &pl->link_config);
597
Russell King8fa7b9b2017-12-01 10:25:09 +0000598 ret = phylink_parse_mode(pl, fwnode);
Russell King9525ae82017-07-25 15:03:13 +0100599 if (ret < 0) {
600 kfree(pl);
601 return ERR_PTR(ret);
602 }
603
604 if (pl->link_an_mode == MLO_AN_FIXED) {
Russell King8fa7b9b2017-12-01 10:25:09 +0000605 ret = phylink_parse_fixedlink(pl, fwnode);
Russell King9525ae82017-07-25 15:03:13 +0100606 if (ret < 0) {
607 kfree(pl);
608 return ERR_PTR(ret);
609 }
610 }
611
Russell King8fa7b9b2017-12-01 10:25:09 +0000612 ret = phylink_register_sfp(pl, fwnode);
Russell Kingce0aa272017-07-25 15:03:18 +0100613 if (ret < 0) {
614 kfree(pl);
615 return ERR_PTR(ret);
616 }
617
Russell King9525ae82017-07-25 15:03:13 +0100618 return pl;
619}
620EXPORT_SYMBOL_GPL(phylink_create);
621
Russell King8796c892017-12-01 10:24:47 +0000622/**
623 * phylink_destroy() - cleanup and destroy the phylink instance
624 * @pl: a pointer to a &struct phylink returned from phylink_create()
625 *
626 * Destroy a phylink instance. Any PHY that has been attached must have been
627 * cleaned up via phylink_disconnect_phy() prior to calling this function.
628 */
Russell King9525ae82017-07-25 15:03:13 +0100629void phylink_destroy(struct phylink *pl)
630{
Russell Kingce0aa272017-07-25 15:03:18 +0100631 if (pl->sfp_bus)
632 sfp_unregister_upstream(pl->sfp_bus);
Florian Fainelli3bcd4772018-05-20 20:49:47 -0700633 if (!IS_ERR_OR_NULL(pl->link_gpio))
Florian Fainellidaab3342018-05-10 13:17:30 -0700634 gpiod_put(pl->link_gpio);
Russell Kingce0aa272017-07-25 15:03:18 +0100635
Russell King9525ae82017-07-25 15:03:13 +0100636 cancel_work_sync(&pl->resolve);
637 kfree(pl);
638}
639EXPORT_SYMBOL_GPL(phylink_destroy);
640
Wei Yongjun1ec6e532017-11-02 11:14:48 +0000641static void phylink_phy_change(struct phy_device *phydev, bool up,
642 bool do_carrier)
Russell King9525ae82017-07-25 15:03:13 +0100643{
644 struct phylink *pl = phydev->phylink;
645
646 mutex_lock(&pl->state_mutex);
647 pl->phy_state.speed = phydev->speed;
648 pl->phy_state.duplex = phydev->duplex;
649 pl->phy_state.pause = MLO_PAUSE_NONE;
650 if (phydev->pause)
651 pl->phy_state.pause |= MLO_PAUSE_SYM;
652 if (phydev->asym_pause)
653 pl->phy_state.pause |= MLO_PAUSE_ASYM;
654 pl->phy_state.interface = phydev->interface;
655 pl->phy_state.link = up;
656 mutex_unlock(&pl->state_mutex);
657
658 phylink_run_resolve(pl);
659
660 netdev_dbg(pl->netdev, "phy link %s %s/%s/%s\n", up ? "up" : "down",
Florian Fainelli516b29e2017-10-30 21:42:57 -0700661 phy_modes(phydev->interface),
Russell King9525ae82017-07-25 15:03:13 +0100662 phy_speed_to_str(phydev->speed),
663 phy_duplex_to_str(phydev->duplex));
664}
665
666static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy)
667{
668 struct phylink_link_state config;
669 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
Russell King9525ae82017-07-25 15:03:13 +0100670 int ret;
671
672 memset(&config, 0, sizeof(config));
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100673 linkmode_copy(supported, phy->supported);
674 linkmode_copy(config.advertising, phy->advertising);
Russell King9525ae82017-07-25 15:03:13 +0100675 config.interface = pl->link_config.interface;
676
677 /*
678 * This is the new way of dealing with flow control for PHYs,
679 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
680 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
681 * using our validate call to the MAC, we rely upon the MAC
682 * clearing the bits from both supported and advertising fields.
683 */
684 if (phylink_test(supported, Pause))
685 phylink_set(config.advertising, Pause);
686 if (phylink_test(supported, Asym_Pause))
687 phylink_set(config.advertising, Asym_Pause);
688
689 ret = phylink_validate(pl, supported, &config);
690 if (ret)
691 return ret;
692
693 phy->phylink = pl;
694 phy->phy_link_change = phylink_phy_change;
695
696 netdev_info(pl->netdev,
697 "PHY [%s] driver [%s]\n", dev_name(&phy->mdio.dev),
698 phy->drv->name);
699
700 mutex_lock(&phy->lock);
701 mutex_lock(&pl->state_mutex);
Russell King9525ae82017-07-25 15:03:13 +0100702 pl->phydev = phy;
703 linkmode_copy(pl->supported, supported);
704 linkmode_copy(pl->link_config.advertising, config.advertising);
705
Colin Ian Kingcc1122b2018-03-01 10:23:03 +0000706 /* Restrict the phy advertisement according to the MAC support. */
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100707 linkmode_copy(phy->advertising, config.advertising);
Russell King9525ae82017-07-25 15:03:13 +0100708 mutex_unlock(&pl->state_mutex);
709 mutex_unlock(&phy->lock);
710
711 netdev_dbg(pl->netdev,
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100712 "phy: setting supported %*pb advertising %*pb\n",
Russell King9525ae82017-07-25 15:03:13 +0100713 __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100714 __ETHTOOL_LINK_MODE_MASK_NBITS, phy->advertising);
Russell King9525ae82017-07-25 15:03:13 +0100715
Heiner Kallweit434a4312019-01-23 07:31:58 +0100716 if (phy_interrupt_is_valid(phy))
717 phy_request_interrupt(phy);
Russell King9525ae82017-07-25 15:03:13 +0100718
719 return 0;
720}
721
Baruch Siach7e418372018-10-03 19:04:49 +0300722static int __phylink_connect_phy(struct phylink *pl, struct phy_device *phy,
723 phy_interface_t interface)
724{
725 int ret;
726
727 if (WARN_ON(pl->link_an_mode == MLO_AN_FIXED ||
728 (pl->link_an_mode == MLO_AN_INBAND &&
729 phy_interface_mode_is_8023z(interface))))
730 return -EINVAL;
731
732 if (pl->phydev)
733 return -EBUSY;
734
735 ret = phy_attach_direct(pl->netdev, phy, 0, interface);
736 if (ret)
737 return ret;
738
739 ret = phylink_bringup_phy(pl, phy);
740 if (ret)
741 phy_detach(phy);
742
743 return ret;
744}
745
Russell King8796c892017-12-01 10:24:47 +0000746/**
747 * phylink_connect_phy() - connect a PHY to the phylink instance
748 * @pl: a pointer to a &struct phylink returned from phylink_create()
749 * @phy: a pointer to a &struct phy_device.
750 *
751 * Connect @phy to the phylink instance specified by @pl by calling
752 * phy_attach_direct(). Configure the @phy according to the MAC driver's
753 * capabilities, start the PHYLIB state machine and enable any interrupts
754 * that the PHY supports.
755 *
756 * This updates the phylink's ethtool supported and advertising link mode
757 * masks.
758 *
759 * Returns 0 on success or a negative errno.
760 */
Russell King9525ae82017-07-25 15:03:13 +0100761int phylink_connect_phy(struct phylink *pl, struct phy_device *phy)
762{
Florian Fainelli4904b6e2017-12-12 16:00:26 -0800763 /* Use PHY device/driver interface */
764 if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
765 pl->link_interface = phy->interface;
766 pl->link_config.interface = pl->link_interface;
767 }
768
Baruch Siach7e418372018-10-03 19:04:49 +0300769 return __phylink_connect_phy(pl, phy, pl->link_interface);
Russell King9525ae82017-07-25 15:03:13 +0100770}
771EXPORT_SYMBOL_GPL(phylink_connect_phy);
772
Russell King8796c892017-12-01 10:24:47 +0000773/**
774 * phylink_of_phy_connect() - connect the PHY specified in the DT mode.
775 * @pl: a pointer to a &struct phylink returned from phylink_create()
776 * @dn: a pointer to a &struct device_node.
Florian Fainelli0a629642017-12-12 16:00:25 -0800777 * @flags: PHY-specific flags to communicate to the PHY device driver
Russell King8796c892017-12-01 10:24:47 +0000778 *
779 * Connect the phy specified in the device node @dn to the phylink instance
780 * specified by @pl. Actions specified in phylink_connect_phy() will be
781 * performed.
782 *
783 * Returns 0 on success or a negative errno.
784 */
Florian Fainelli0a629642017-12-12 16:00:25 -0800785int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn,
786 u32 flags)
Russell King9525ae82017-07-25 15:03:13 +0100787{
788 struct device_node *phy_node;
789 struct phy_device *phy_dev;
790 int ret;
791
Russell Kingcf4f2672017-12-01 10:24:21 +0000792 /* Fixed links and 802.3z are handled without needing a PHY */
793 if (pl->link_an_mode == MLO_AN_FIXED ||
Russell King86a362c2017-12-01 10:24:26 +0000794 (pl->link_an_mode == MLO_AN_INBAND &&
795 phy_interface_mode_is_8023z(pl->link_interface)))
Russell King9525ae82017-07-25 15:03:13 +0100796 return 0;
797
798 phy_node = of_parse_phandle(dn, "phy-handle", 0);
799 if (!phy_node)
800 phy_node = of_parse_phandle(dn, "phy", 0);
801 if (!phy_node)
802 phy_node = of_parse_phandle(dn, "phy-device", 0);
803
804 if (!phy_node) {
Florian Fainellid38b4afd2017-12-12 16:00:27 -0800805 if (pl->link_an_mode == MLO_AN_PHY)
Russell King9525ae82017-07-25 15:03:13 +0100806 return -ENODEV;
Russell King9525ae82017-07-25 15:03:13 +0100807 return 0;
808 }
809
Florian Fainelli0a629642017-12-12 16:00:25 -0800810 phy_dev = of_phy_attach(pl->netdev, phy_node, flags,
811 pl->link_interface);
Russell King9525ae82017-07-25 15:03:13 +0100812 /* We're done with the phy_node handle */
813 of_node_put(phy_node);
814
815 if (!phy_dev)
816 return -ENODEV;
817
818 ret = phylink_bringup_phy(pl, phy_dev);
819 if (ret)
820 phy_detach(phy_dev);
821
822 return ret;
823}
824EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
825
Russell King8796c892017-12-01 10:24:47 +0000826/**
827 * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
828 * instance.
829 * @pl: a pointer to a &struct phylink returned from phylink_create()
830 *
831 * Disconnect any current PHY from the phylink instance described by @pl.
832 */
Russell King9525ae82017-07-25 15:03:13 +0100833void phylink_disconnect_phy(struct phylink *pl)
834{
835 struct phy_device *phy;
836
Russell King8b874512017-12-15 16:09:47 +0000837 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +0100838
839 phy = pl->phydev;
840 if (phy) {
841 mutex_lock(&phy->lock);
842 mutex_lock(&pl->state_mutex);
Russell King9525ae82017-07-25 15:03:13 +0100843 pl->phydev = NULL;
844 mutex_unlock(&pl->state_mutex);
845 mutex_unlock(&phy->lock);
846 flush_work(&pl->resolve);
847
848 phy_disconnect(phy);
849 }
850}
851EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
852
Russell King8796c892017-12-01 10:24:47 +0000853/**
Florian Fainelli1ac63e32017-12-12 16:00:28 -0800854 * phylink_fixed_state_cb() - allow setting a fixed link callback
855 * @pl: a pointer to a &struct phylink returned from phylink_create()
856 * @cb: callback to execute to determine the fixed link state.
857 *
858 * The MAC driver should call this driver when the state of its link
859 * can be determined through e.g: an out of band MMIO register.
860 */
861int phylink_fixed_state_cb(struct phylink *pl,
862 void (*cb)(struct net_device *dev,
863 struct phylink_link_state *state))
864{
865 /* It does not make sense to let the link be overriden unless we use
866 * MLO_AN_FIXED
867 */
868 if (pl->link_an_mode != MLO_AN_FIXED)
869 return -EINVAL;
870
871 mutex_lock(&pl->state_mutex);
872 pl->get_fixed_state = cb;
873 mutex_unlock(&pl->state_mutex);
874
875 return 0;
876}
877EXPORT_SYMBOL_GPL(phylink_fixed_state_cb);
878
879/**
Russell King8796c892017-12-01 10:24:47 +0000880 * phylink_mac_change() - notify phylink of a change in MAC state
881 * @pl: a pointer to a &struct phylink returned from phylink_create()
882 * @up: indicates whether the link is currently up.
883 *
884 * The MAC driver should call this driver when the state of its link
885 * changes (eg, link failure, new negotiation results, etc.)
886 */
Russell King9525ae82017-07-25 15:03:13 +0100887void phylink_mac_change(struct phylink *pl, bool up)
888{
889 if (!up)
890 pl->mac_link_dropped = true;
891 phylink_run_resolve(pl);
892 netdev_dbg(pl->netdev, "mac link %s\n", up ? "up" : "down");
893}
894EXPORT_SYMBOL_GPL(phylink_mac_change);
895
Russell King8796c892017-12-01 10:24:47 +0000896/**
897 * phylink_start() - start a phylink instance
898 * @pl: a pointer to a &struct phylink returned from phylink_create()
899 *
900 * Start the phylink instance specified by @pl, configuring the MAC for the
901 * desired link mode(s) and negotiation style. This should be called from the
902 * network device driver's &struct net_device_ops ndo_open() method.
903 */
Russell King9525ae82017-07-25 15:03:13 +0100904void phylink_start(struct phylink *pl)
905{
Russell King8b874512017-12-15 16:09:47 +0000906 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +0100907
908 netdev_info(pl->netdev, "configuring for %s/%s link mode\n",
909 phylink_an_mode_str(pl->link_an_mode),
910 phy_modes(pl->link_config.interface));
911
Antoine Tenartaeeb2e82018-09-19 11:39:31 +0200912 /* Always set the carrier off */
913 netif_carrier_off(pl->netdev);
914
Russell King9525ae82017-07-25 15:03:13 +0100915 /* Apply the link configuration to the MAC when starting. This allows
916 * a fixed-link to start with the correct parameters, and also
Colin Ian Kingcc1122b2018-03-01 10:23:03 +0000917 * ensures that we set the appropriate advertisement for Serdes links.
Russell King9525ae82017-07-25 15:03:13 +0100918 */
919 phylink_resolve_flow(pl, &pl->link_config);
920 phylink_mac_config(pl, &pl->link_config);
921
Russell King85b43942017-12-01 10:24:42 +0000922 /* Restart autonegotiation if using 802.3z to ensure that the link
923 * parameters are properly negotiated. This is necessary for DSA
924 * switches using 802.3z negotiation to ensure they see our modes.
925 */
926 phylink_mac_an_restart(pl);
927
Russell King9525ae82017-07-25 15:03:13 +0100928 clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
929 phylink_run_resolve(pl);
930
Russell King9cd00a82018-05-10 13:17:31 -0700931 if (pl->link_an_mode == MLO_AN_FIXED && !IS_ERR(pl->link_gpio))
932 mod_timer(&pl->link_poll, jiffies + HZ);
Russell Kingce0aa272017-07-25 15:03:18 +0100933 if (pl->sfp_bus)
934 sfp_upstream_start(pl->sfp_bus);
Russell King9525ae82017-07-25 15:03:13 +0100935 if (pl->phydev)
936 phy_start(pl->phydev);
937}
938EXPORT_SYMBOL_GPL(phylink_start);
939
Russell King8796c892017-12-01 10:24:47 +0000940/**
941 * phylink_stop() - stop a phylink instance
942 * @pl: a pointer to a &struct phylink returned from phylink_create()
943 *
944 * Stop the phylink instance specified by @pl. This should be called from the
945 * network device driver's &struct net_device_ops ndo_stop() method. The
946 * network device's carrier state should not be changed prior to calling this
947 * function.
948 */
Russell King9525ae82017-07-25 15:03:13 +0100949void phylink_stop(struct phylink *pl)
950{
Russell King8b874512017-12-15 16:09:47 +0000951 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +0100952
953 if (pl->phydev)
954 phy_stop(pl->phydev);
Russell Kingce0aa272017-07-25 15:03:18 +0100955 if (pl->sfp_bus)
956 sfp_upstream_stop(pl->sfp_bus);
Russell King9cd00a82018-05-10 13:17:31 -0700957 if (pl->link_an_mode == MLO_AN_FIXED && !IS_ERR(pl->link_gpio))
958 del_timer_sync(&pl->link_poll);
Russell King9525ae82017-07-25 15:03:13 +0100959
Russell King87454b62019-02-11 15:04:24 +0000960 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_STOPPED);
Russell King9525ae82017-07-25 15:03:13 +0100961}
962EXPORT_SYMBOL_GPL(phylink_stop);
963
Russell King8796c892017-12-01 10:24:47 +0000964/**
965 * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY
966 * @pl: a pointer to a &struct phylink returned from phylink_create()
967 * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters
968 *
969 * Read the wake on lan parameters from the PHY attached to the phylink
970 * instance specified by @pl. If no PHY is currently attached, report no
971 * support for wake on lan.
972 */
Russell King9525ae82017-07-25 15:03:13 +0100973void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
974{
Russell King8b874512017-12-15 16:09:47 +0000975 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +0100976
977 wol->supported = 0;
978 wol->wolopts = 0;
979
980 if (pl->phydev)
981 phy_ethtool_get_wol(pl->phydev, wol);
982}
983EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol);
984
Russell King8796c892017-12-01 10:24:47 +0000985/**
986 * phylink_ethtool_set_wol() - set wake on lan parameters
987 * @pl: a pointer to a &struct phylink returned from phylink_create()
988 * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters
989 *
990 * Set the wake on lan parameters for the PHY attached to the phylink
991 * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP
992 * error.
993 *
994 * Returns zero on success or negative errno code.
995 */
Russell King9525ae82017-07-25 15:03:13 +0100996int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
997{
998 int ret = -EOPNOTSUPP;
999
Russell King8b874512017-12-15 16:09:47 +00001000 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001001
1002 if (pl->phydev)
1003 ret = phy_ethtool_set_wol(pl->phydev, wol);
1004
1005 return ret;
1006}
1007EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol);
1008
1009static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b)
1010{
1011 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
1012
1013 linkmode_zero(mask);
1014 phylink_set_port_modes(mask);
1015
1016 linkmode_and(dst, dst, mask);
1017 linkmode_or(dst, dst, b);
1018}
1019
1020static void phylink_get_ksettings(const struct phylink_link_state *state,
1021 struct ethtool_link_ksettings *kset)
1022{
1023 phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
1024 linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
1025 kset->base.speed = state->speed;
1026 kset->base.duplex = state->duplex;
1027 kset->base.autoneg = state->an_enabled ? AUTONEG_ENABLE :
1028 AUTONEG_DISABLE;
1029}
1030
Russell King8796c892017-12-01 10:24:47 +00001031/**
1032 * phylink_ethtool_ksettings_get() - get the current link settings
1033 * @pl: a pointer to a &struct phylink returned from phylink_create()
1034 * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings
1035 *
1036 * Read the current link settings for the phylink instance specified by @pl.
1037 * This will be the link settings read from the MAC, PHY or fixed link
1038 * settings depending on the current negotiation mode.
1039 */
Russell King9525ae82017-07-25 15:03:13 +01001040int phylink_ethtool_ksettings_get(struct phylink *pl,
Florian Fainelli516b29e2017-10-30 21:42:57 -07001041 struct ethtool_link_ksettings *kset)
Russell King9525ae82017-07-25 15:03:13 +01001042{
1043 struct phylink_link_state link_state;
1044
Russell King8b874512017-12-15 16:09:47 +00001045 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001046
1047 if (pl->phydev) {
1048 phy_ethtool_ksettings_get(pl->phydev, kset);
1049 } else {
1050 kset->base.port = pl->link_port;
1051 }
1052
1053 linkmode_copy(kset->link_modes.supported, pl->supported);
1054
1055 switch (pl->link_an_mode) {
1056 case MLO_AN_FIXED:
1057 /* We are using fixed settings. Report these as the
1058 * current link settings - and note that these also
1059 * represent the supported speeds/duplex/pause modes.
1060 */
1061 phylink_get_fixed_state(pl, &link_state);
1062 phylink_get_ksettings(&link_state, kset);
1063 break;
1064
Russell King86a362c2017-12-01 10:24:26 +00001065 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001066 /* If there is a phy attached, then use the reported
1067 * settings from the phy with no modification.
1068 */
1069 if (pl->phydev)
1070 break;
1071
Russell King9525ae82017-07-25 15:03:13 +01001072 phylink_get_mac_state(pl, &link_state);
1073
1074 /* The MAC is reporting the link results from its own PCS
1075 * layer via in-band status. Report these as the current
1076 * link settings.
1077 */
1078 phylink_get_ksettings(&link_state, kset);
1079 break;
1080 }
1081
1082 return 0;
1083}
1084EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get);
1085
Russell King8796c892017-12-01 10:24:47 +00001086/**
1087 * phylink_ethtool_ksettings_set() - set the link settings
1088 * @pl: a pointer to a &struct phylink returned from phylink_create()
1089 * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes
1090 */
Russell King9525ae82017-07-25 15:03:13 +01001091int phylink_ethtool_ksettings_set(struct phylink *pl,
Florian Fainelli516b29e2017-10-30 21:42:57 -07001092 const struct ethtool_link_ksettings *kset)
Russell King9525ae82017-07-25 15:03:13 +01001093{
1094 struct ethtool_link_ksettings our_kset;
1095 struct phylink_link_state config;
1096 int ret;
1097
Russell King8b874512017-12-15 16:09:47 +00001098 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001099
1100 if (kset->base.autoneg != AUTONEG_DISABLE &&
1101 kset->base.autoneg != AUTONEG_ENABLE)
1102 return -EINVAL;
1103
1104 config = pl->link_config;
1105
Colin Ian Kingcc1122b2018-03-01 10:23:03 +00001106 /* Mask out unsupported advertisements */
Russell King9525ae82017-07-25 15:03:13 +01001107 linkmode_and(config.advertising, kset->link_modes.advertising,
1108 pl->supported);
1109
1110 /* FIXME: should we reject autoneg if phy/mac does not support it? */
1111 if (kset->base.autoneg == AUTONEG_DISABLE) {
1112 const struct phy_setting *s;
1113
1114 /* Autonegotiation disabled, select a suitable speed and
1115 * duplex.
1116 */
1117 s = phy_lookup_setting(kset->base.speed, kset->base.duplex,
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01001118 pl->supported, false);
Russell King9525ae82017-07-25 15:03:13 +01001119 if (!s)
1120 return -EINVAL;
1121
1122 /* If we have a fixed link (as specified by firmware), refuse
1123 * to change link parameters.
1124 */
1125 if (pl->link_an_mode == MLO_AN_FIXED &&
1126 (s->speed != pl->link_config.speed ||
1127 s->duplex != pl->link_config.duplex))
1128 return -EINVAL;
1129
1130 config.speed = s->speed;
1131 config.duplex = s->duplex;
1132 config.an_enabled = false;
1133
1134 __clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
1135 } else {
1136 /* If we have a fixed link, refuse to enable autonegotiation */
1137 if (pl->link_an_mode == MLO_AN_FIXED)
1138 return -EINVAL;
1139
1140 config.speed = SPEED_UNKNOWN;
1141 config.duplex = DUPLEX_UNKNOWN;
1142 config.an_enabled = true;
1143
1144 __set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
1145 }
1146
1147 if (phylink_validate(pl, pl->supported, &config))
1148 return -EINVAL;
1149
Colin Ian Kingcc1122b2018-03-01 10:23:03 +00001150 /* If autonegotiation is enabled, we must have an advertisement */
Russell King9525ae82017-07-25 15:03:13 +01001151 if (config.an_enabled && phylink_is_empty_linkmode(config.advertising))
1152 return -EINVAL;
1153
1154 our_kset = *kset;
1155 linkmode_copy(our_kset.link_modes.advertising, config.advertising);
1156 our_kset.base.speed = config.speed;
1157 our_kset.base.duplex = config.duplex;
1158
1159 /* If we have a PHY, configure the phy */
1160 if (pl->phydev) {
1161 ret = phy_ethtool_ksettings_set(pl->phydev, &our_kset);
1162 if (ret)
1163 return ret;
1164 }
1165
1166 mutex_lock(&pl->state_mutex);
1167 /* Configure the MAC to match the new settings */
1168 linkmode_copy(pl->link_config.advertising, our_kset.link_modes.advertising);
Russell King182088a2017-12-20 23:21:28 +00001169 pl->link_config.interface = config.interface;
Russell King9525ae82017-07-25 15:03:13 +01001170 pl->link_config.speed = our_kset.base.speed;
1171 pl->link_config.duplex = our_kset.base.duplex;
1172 pl->link_config.an_enabled = our_kset.base.autoneg != AUTONEG_DISABLE;
1173
1174 if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
1175 phylink_mac_config(pl, &pl->link_config);
1176 phylink_mac_an_restart(pl);
1177 }
1178 mutex_unlock(&pl->state_mutex);
1179
Dan Carpenterd18c2a12017-08-10 00:35:50 +03001180 return 0;
Russell King9525ae82017-07-25 15:03:13 +01001181}
1182EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
1183
Russell King8796c892017-12-01 10:24:47 +00001184/**
1185 * phylink_ethtool_nway_reset() - restart negotiation
1186 * @pl: a pointer to a &struct phylink returned from phylink_create()
1187 *
1188 * Restart negotiation for the phylink instance specified by @pl. This will
1189 * cause any attached phy to restart negotiation with the link partner, and
1190 * if the MAC is in a BaseX mode, the MAC will also be requested to restart
1191 * negotiation.
1192 *
1193 * Returns zero on success, or negative error code.
1194 */
Russell King9525ae82017-07-25 15:03:13 +01001195int phylink_ethtool_nway_reset(struct phylink *pl)
1196{
1197 int ret = 0;
1198
Russell King8b874512017-12-15 16:09:47 +00001199 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001200
1201 if (pl->phydev)
1202 ret = phy_restart_aneg(pl->phydev);
1203 phylink_mac_an_restart(pl);
1204
1205 return ret;
1206}
1207EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
1208
Russell King8796c892017-12-01 10:24:47 +00001209/**
1210 * phylink_ethtool_get_pauseparam() - get the current pause parameters
1211 * @pl: a pointer to a &struct phylink returned from phylink_create()
1212 * @pause: a pointer to a &struct ethtool_pauseparam
1213 */
Russell King9525ae82017-07-25 15:03:13 +01001214void phylink_ethtool_get_pauseparam(struct phylink *pl,
1215 struct ethtool_pauseparam *pause)
1216{
Russell King8b874512017-12-15 16:09:47 +00001217 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001218
1219 pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
1220 pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
1221 pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
1222}
1223EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
1224
Russell King8796c892017-12-01 10:24:47 +00001225/**
1226 * phylink_ethtool_set_pauseparam() - set the current pause parameters
1227 * @pl: a pointer to a &struct phylink returned from phylink_create()
1228 * @pause: a pointer to a &struct ethtool_pauseparam
1229 */
Russell King9525ae82017-07-25 15:03:13 +01001230int phylink_ethtool_set_pauseparam(struct phylink *pl,
1231 struct ethtool_pauseparam *pause)
1232{
1233 struct phylink_link_state *config = &pl->link_config;
1234
Russell King8b874512017-12-15 16:09:47 +00001235 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001236
1237 if (!phylink_test(pl->supported, Pause) &&
1238 !phylink_test(pl->supported, Asym_Pause))
1239 return -EOPNOTSUPP;
1240
1241 if (!phylink_test(pl->supported, Asym_Pause) &&
1242 !pause->autoneg && pause->rx_pause != pause->tx_pause)
1243 return -EINVAL;
1244
1245 config->pause &= ~(MLO_PAUSE_AN | MLO_PAUSE_TXRX_MASK);
1246
1247 if (pause->autoneg)
1248 config->pause |= MLO_PAUSE_AN;
1249 if (pause->rx_pause)
1250 config->pause |= MLO_PAUSE_RX;
1251 if (pause->tx_pause)
1252 config->pause |= MLO_PAUSE_TX;
1253
1254 if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
1255 switch (pl->link_an_mode) {
1256 case MLO_AN_PHY:
1257 /* Silently mark the carrier down, and then trigger a resolve */
1258 netif_carrier_off(pl->netdev);
1259 phylink_run_resolve(pl);
1260 break;
1261
1262 case MLO_AN_FIXED:
1263 /* Should we allow fixed links to change against the config? */
1264 phylink_resolve_flow(pl, config);
1265 phylink_mac_config(pl, config);
1266 break;
1267
Russell King86a362c2017-12-01 10:24:26 +00001268 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001269 phylink_mac_config(pl, config);
1270 phylink_mac_an_restart(pl);
1271 break;
1272 }
1273 }
1274
1275 return 0;
1276}
1277EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
1278
Russell King8796c892017-12-01 10:24:47 +00001279/**
1280 * phylink_ethtool_get_eee_err() - read the energy efficient ethernet error
1281 * counter
1282 * @pl: a pointer to a &struct phylink returned from phylink_create().
1283 *
1284 * Read the Energy Efficient Ethernet error counter from the PHY associated
1285 * with the phylink instance specified by @pl.
1286 *
1287 * Returns positive error counter value, or negative error code.
1288 */
Russell King9525ae82017-07-25 15:03:13 +01001289int phylink_get_eee_err(struct phylink *pl)
1290{
1291 int ret = 0;
1292
Russell King8b874512017-12-15 16:09:47 +00001293 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001294
1295 if (pl->phydev)
1296 ret = phy_get_eee_err(pl->phydev);
1297
1298 return ret;
1299}
1300EXPORT_SYMBOL_GPL(phylink_get_eee_err);
1301
Russell King8796c892017-12-01 10:24:47 +00001302/**
Russell King86e58132019-02-11 11:46:06 +00001303 * phylink_init_eee() - init and check the EEE features
1304 * @pl: a pointer to a &struct phylink returned from phylink_create()
1305 * @clk_stop_enable: allow PHY to stop receive clock
1306 *
1307 * Must be called either with RTNL held or within mac_link_up()
1308 */
1309int phylink_init_eee(struct phylink *pl, bool clk_stop_enable)
1310{
1311 int ret = -EOPNOTSUPP;
1312
1313 if (pl->phydev)
1314 ret = phy_init_eee(pl->phydev, clk_stop_enable);
1315
1316 return ret;
1317}
1318EXPORT_SYMBOL_GPL(phylink_init_eee);
1319
1320/**
Russell King8796c892017-12-01 10:24:47 +00001321 * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters
1322 * @pl: a pointer to a &struct phylink returned from phylink_create()
1323 * @eee: a pointer to a &struct ethtool_eee for the read parameters
1324 */
Russell King9525ae82017-07-25 15:03:13 +01001325int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee)
1326{
1327 int ret = -EOPNOTSUPP;
1328
Russell King8b874512017-12-15 16:09:47 +00001329 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001330
1331 if (pl->phydev)
1332 ret = phy_ethtool_get_eee(pl->phydev, eee);
1333
1334 return ret;
1335}
1336EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
1337
Russell King8796c892017-12-01 10:24:47 +00001338/**
1339 * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters
1340 * @pl: a pointer to a &struct phylink returned from phylink_create()
1341 * @eee: a pointer to a &struct ethtool_eee for the desired parameters
1342 */
Russell King9525ae82017-07-25 15:03:13 +01001343int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_eee *eee)
1344{
1345 int ret = -EOPNOTSUPP;
1346
Russell King8b874512017-12-15 16:09:47 +00001347 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001348
1349 if (pl->phydev)
1350 ret = phy_ethtool_set_eee(pl->phydev, eee);
1351
1352 return ret;
1353}
1354EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
1355
1356/* This emulates MII registers for a fixed-mode phy operating as per the
1357 * passed in state. "aneg" defines if we report negotiation is possible.
1358 *
1359 * FIXME: should deal with negotiation state too.
1360 */
1361static int phylink_mii_emul_read(struct net_device *ndev, unsigned int reg,
1362 struct phylink_link_state *state, bool aneg)
1363{
1364 struct fixed_phy_status fs;
1365 int val;
1366
1367 fs.link = state->link;
1368 fs.speed = state->speed;
1369 fs.duplex = state->duplex;
1370 fs.pause = state->pause & MLO_PAUSE_SYM;
1371 fs.asym_pause = state->pause & MLO_PAUSE_ASYM;
1372
1373 val = swphy_read_reg(reg, &fs);
1374 if (reg == MII_BMSR) {
1375 if (!state->an_complete)
1376 val &= ~BMSR_ANEGCOMPLETE;
1377 if (!aneg)
1378 val &= ~BMSR_ANEGCAPABLE;
1379 }
1380 return val;
1381}
1382
Russell Kingecbd87b2017-07-25 15:03:28 +01001383static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
1384 unsigned int reg)
1385{
1386 struct phy_device *phydev = pl->phydev;
1387 int prtad, devad;
1388
1389 if (mdio_phy_id_is_c45(phy_id)) {
1390 prtad = mdio_phy_id_prtad(phy_id);
1391 devad = mdio_phy_id_devad(phy_id);
1392 devad = MII_ADDR_C45 | devad << 16 | reg;
1393 } else if (phydev->is_c45) {
1394 switch (reg) {
1395 case MII_BMCR:
1396 case MII_BMSR:
1397 case MII_PHYSID1:
1398 case MII_PHYSID2:
1399 devad = __ffs(phydev->c45_ids.devices_in_package);
1400 break;
1401 case MII_ADVERTISE:
1402 case MII_LPA:
1403 if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1404 return -EINVAL;
1405 devad = MDIO_MMD_AN;
1406 if (reg == MII_ADVERTISE)
1407 reg = MDIO_AN_ADVERTISE;
1408 else
1409 reg = MDIO_AN_LPA;
1410 break;
1411 default:
1412 return -EINVAL;
1413 }
1414 prtad = phy_id;
1415 devad = MII_ADDR_C45 | devad << 16 | reg;
1416 } else {
1417 prtad = phy_id;
1418 devad = reg;
1419 }
1420 return mdiobus_read(pl->phydev->mdio.bus, prtad, devad);
1421}
1422
1423static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
1424 unsigned int reg, unsigned int val)
1425{
1426 struct phy_device *phydev = pl->phydev;
1427 int prtad, devad;
1428
1429 if (mdio_phy_id_is_c45(phy_id)) {
1430 prtad = mdio_phy_id_prtad(phy_id);
1431 devad = mdio_phy_id_devad(phy_id);
1432 devad = MII_ADDR_C45 | devad << 16 | reg;
1433 } else if (phydev->is_c45) {
1434 switch (reg) {
1435 case MII_BMCR:
1436 case MII_BMSR:
1437 case MII_PHYSID1:
1438 case MII_PHYSID2:
1439 devad = __ffs(phydev->c45_ids.devices_in_package);
1440 break;
1441 case MII_ADVERTISE:
1442 case MII_LPA:
1443 if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1444 return -EINVAL;
1445 devad = MDIO_MMD_AN;
1446 if (reg == MII_ADVERTISE)
1447 reg = MDIO_AN_ADVERTISE;
1448 else
1449 reg = MDIO_AN_LPA;
1450 break;
1451 default:
1452 return -EINVAL;
1453 }
1454 prtad = phy_id;
1455 devad = MII_ADDR_C45 | devad << 16 | reg;
1456 } else {
1457 prtad = phy_id;
1458 devad = reg;
1459 }
1460
1461 return mdiobus_write(phydev->mdio.bus, prtad, devad, val);
1462}
1463
Russell King9525ae82017-07-25 15:03:13 +01001464static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
1465 unsigned int reg)
1466{
1467 struct phylink_link_state state;
1468 int val = 0xffff;
1469
Russell King9525ae82017-07-25 15:03:13 +01001470 switch (pl->link_an_mode) {
1471 case MLO_AN_FIXED:
1472 if (phy_id == 0) {
1473 phylink_get_fixed_state(pl, &state);
1474 val = phylink_mii_emul_read(pl->netdev, reg, &state,
1475 true);
1476 }
1477 break;
1478
1479 case MLO_AN_PHY:
1480 return -EOPNOTSUPP;
1481
Russell King86a362c2017-12-01 10:24:26 +00001482 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001483 if (phy_id == 0) {
1484 val = phylink_get_mac_state(pl, &state);
1485 if (val < 0)
1486 return val;
1487
1488 val = phylink_mii_emul_read(pl->netdev, reg, &state,
1489 true);
1490 }
1491 break;
1492 }
1493
1494 return val & 0xffff;
1495}
1496
1497static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
1498 unsigned int reg, unsigned int val)
1499{
Russell King9525ae82017-07-25 15:03:13 +01001500 switch (pl->link_an_mode) {
1501 case MLO_AN_FIXED:
1502 break;
1503
1504 case MLO_AN_PHY:
1505 return -EOPNOTSUPP;
1506
Russell King86a362c2017-12-01 10:24:26 +00001507 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001508 break;
1509 }
1510
1511 return 0;
1512}
1513
Russell King8796c892017-12-01 10:24:47 +00001514/**
1515 * phylink_mii_ioctl() - generic mii ioctl interface
1516 * @pl: a pointer to a &struct phylink returned from phylink_create()
1517 * @ifr: a pointer to a &struct ifreq for socket ioctls
1518 * @cmd: ioctl cmd to execute
1519 *
1520 * Perform the specified MII ioctl on the PHY attached to the phylink instance
1521 * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
1522 *
1523 * Returns: zero on success or negative error code.
1524 *
1525 * %SIOCGMIIPHY:
1526 * read register from the current PHY.
1527 * %SIOCGMIIREG:
1528 * read register from the specified PHY.
1529 * %SIOCSMIIREG:
1530 * set a register on the specified PHY.
1531 */
Russell King9525ae82017-07-25 15:03:13 +01001532int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
1533{
Russell Kingecbd87b2017-07-25 15:03:28 +01001534 struct mii_ioctl_data *mii = if_mii(ifr);
1535 int ret;
Russell King9525ae82017-07-25 15:03:13 +01001536
Russell King8b874512017-12-15 16:09:47 +00001537 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001538
Russell Kingecbd87b2017-07-25 15:03:28 +01001539 if (pl->phydev) {
Russell King86a362c2017-12-01 10:24:26 +00001540 /* PHYs only exist for MLO_AN_PHY and SGMII */
Russell Kingecbd87b2017-07-25 15:03:28 +01001541 switch (cmd) {
1542 case SIOCGMIIPHY:
1543 mii->phy_id = pl->phydev->mdio.addr;
Gustavo A. R. Silva46cd7502018-01-05 11:23:45 -06001544 /* fall through */
Russell King9525ae82017-07-25 15:03:13 +01001545
Russell Kingecbd87b2017-07-25 15:03:28 +01001546 case SIOCGMIIREG:
1547 ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
1548 if (ret >= 0) {
1549 mii->val_out = ret;
1550 ret = 0;
1551 }
1552 break;
Russell King9525ae82017-07-25 15:03:13 +01001553
Russell Kingecbd87b2017-07-25 15:03:28 +01001554 case SIOCSMIIREG:
1555 ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
1556 mii->val_in);
1557 break;
Russell King9525ae82017-07-25 15:03:13 +01001558
Russell Kingecbd87b2017-07-25 15:03:28 +01001559 default:
Russell King9525ae82017-07-25 15:03:13 +01001560 ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
Russell Kingecbd87b2017-07-25 15:03:28 +01001561 break;
1562 }
1563 } else {
1564 switch (cmd) {
1565 case SIOCGMIIPHY:
1566 mii->phy_id = 0;
Gustavo A. R. Silva46cd7502018-01-05 11:23:45 -06001567 /* fall through */
Russell Kingecbd87b2017-07-25 15:03:28 +01001568
1569 case SIOCGMIIREG:
1570 ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
1571 if (ret >= 0) {
1572 mii->val_out = ret;
1573 ret = 0;
1574 }
1575 break;
1576
1577 case SIOCSMIIREG:
1578 ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
1579 mii->val_in);
1580 break;
1581
1582 default:
1583 ret = -EOPNOTSUPP;
1584 break;
1585 }
Russell King9525ae82017-07-25 15:03:13 +01001586 }
1587
1588 return ret;
1589}
1590EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
1591
Russell Kingce0aa272017-07-25 15:03:18 +01001592static int phylink_sfp_module_insert(void *upstream,
1593 const struct sfp_eeprom_id *id)
1594{
1595 struct phylink *pl = upstream;
1596 __ETHTOOL_DECLARE_LINK_MODE_MASK(support) = { 0, };
1597 struct phylink_link_state config;
1598 phy_interface_t iface;
Russell King444d3502017-12-29 12:15:33 +00001599 int ret = 0;
Russell Kingce0aa272017-07-25 15:03:18 +01001600 bool changed;
1601 u8 port;
1602
Russell King8b874512017-12-15 16:09:47 +00001603 ASSERT_RTNL();
Russell Kingce0aa272017-07-25 15:03:18 +01001604
Russell Kinga9c79362018-02-27 15:53:02 +00001605 sfp_parse_support(pl->sfp_bus, id, support);
1606 port = sfp_parse_port(pl->sfp_bus, id, support);
Russell Kingce0aa272017-07-25 15:03:18 +01001607
1608 memset(&config, 0, sizeof(config));
1609 linkmode_copy(config.advertising, support);
Russell Kinga9c79362018-02-27 15:53:02 +00001610 config.interface = PHY_INTERFACE_MODE_NA;
Russell Kingce0aa272017-07-25 15:03:18 +01001611 config.speed = SPEED_UNKNOWN;
1612 config.duplex = DUPLEX_UNKNOWN;
1613 config.pause = MLO_PAUSE_AN;
1614 config.an_enabled = pl->link_config.an_enabled;
1615
1616 /* Ignore errors if we're expecting a PHY to attach later */
1617 ret = phylink_validate(pl, support, &config);
1618 if (ret) {
Russell Kinga9c79362018-02-27 15:53:02 +00001619 netdev_err(pl->netdev, "validation with support %*pb failed: %d\n",
1620 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
1621 return ret;
1622 }
1623
1624 iface = sfp_select_interface(pl->sfp_bus, id, config.advertising);
1625 if (iface == PHY_INTERFACE_MODE_NA) {
1626 netdev_err(pl->netdev,
Colin Ian Kingcc1122b2018-03-01 10:23:03 +00001627 "selection of interface failed, advertisement %*pb\n",
Russell Kinga9c79362018-02-27 15:53:02 +00001628 __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising);
1629 return -EINVAL;
1630 }
1631
1632 config.interface = iface;
1633 ret = phylink_validate(pl, support, &config);
1634 if (ret) {
Russell Kingce0aa272017-07-25 15:03:18 +01001635 netdev_err(pl->netdev, "validation of %s/%s with support %*pb failed: %d\n",
Russell King444d3502017-12-29 12:15:33 +00001636 phylink_an_mode_str(MLO_AN_INBAND),
1637 phy_modes(config.interface),
Russell Kingce0aa272017-07-25 15:03:18 +01001638 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
1639 return ret;
1640 }
1641
1642 netdev_dbg(pl->netdev, "requesting link mode %s/%s with support %*pb\n",
Russell King444d3502017-12-29 12:15:33 +00001643 phylink_an_mode_str(MLO_AN_INBAND),
1644 phy_modes(config.interface),
Russell Kingce0aa272017-07-25 15:03:18 +01001645 __ETHTOOL_LINK_MODE_MASK_NBITS, support);
1646
Russell King86a362c2017-12-01 10:24:26 +00001647 if (phy_interface_mode_is_8023z(iface) && pl->phydev)
Russell Kingce0aa272017-07-25 15:03:18 +01001648 return -EINVAL;
1649
1650 changed = !bitmap_equal(pl->supported, support,
1651 __ETHTOOL_LINK_MODE_MASK_NBITS);
1652 if (changed) {
1653 linkmode_copy(pl->supported, support);
1654 linkmode_copy(pl->link_config.advertising, config.advertising);
1655 }
1656
Russell King444d3502017-12-29 12:15:33 +00001657 if (pl->link_an_mode != MLO_AN_INBAND ||
Russell Kingce0aa272017-07-25 15:03:18 +01001658 pl->link_config.interface != config.interface) {
1659 pl->link_config.interface = config.interface;
Russell King444d3502017-12-29 12:15:33 +00001660 pl->link_an_mode = MLO_AN_INBAND;
Russell Kingce0aa272017-07-25 15:03:18 +01001661
1662 changed = true;
1663
1664 netdev_info(pl->netdev, "switched to %s/%s link mode\n",
Russell King444d3502017-12-29 12:15:33 +00001665 phylink_an_mode_str(MLO_AN_INBAND),
Russell Kingce0aa272017-07-25 15:03:18 +01001666 phy_modes(config.interface));
1667 }
1668
1669 pl->link_port = port;
1670
1671 if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
1672 &pl->phylink_disable_state))
1673 phylink_mac_config(pl, &pl->link_config);
1674
1675 return ret;
1676}
1677
1678static void phylink_sfp_link_down(void *upstream)
1679{
1680 struct phylink *pl = upstream;
1681
Russell King8b874512017-12-15 16:09:47 +00001682 ASSERT_RTNL();
Russell Kingce0aa272017-07-25 15:03:18 +01001683
Russell King87454b62019-02-11 15:04:24 +00001684 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK);
Russell Kingce0aa272017-07-25 15:03:18 +01001685}
1686
1687static void phylink_sfp_link_up(void *upstream)
1688{
1689 struct phylink *pl = upstream;
1690
Russell King8b874512017-12-15 16:09:47 +00001691 ASSERT_RTNL();
Russell Kingce0aa272017-07-25 15:03:18 +01001692
1693 clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
1694 phylink_run_resolve(pl);
1695}
1696
1697static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
1698{
Baruch Siach7e418372018-10-03 19:04:49 +03001699 struct phylink *pl = upstream;
1700
1701 return __phylink_connect_phy(upstream, phy, pl->link_config.interface);
Russell Kingce0aa272017-07-25 15:03:18 +01001702}
1703
1704static void phylink_sfp_disconnect_phy(void *upstream)
1705{
1706 phylink_disconnect_phy(upstream);
1707}
1708
1709static const struct sfp_upstream_ops sfp_phylink_ops = {
1710 .module_insert = phylink_sfp_module_insert,
1711 .link_up = phylink_sfp_link_up,
1712 .link_down = phylink_sfp_link_down,
1713 .connect_phy = phylink_sfp_connect_phy,
1714 .disconnect_phy = phylink_sfp_disconnect_phy,
1715};
1716
Russell King624c0f02018-08-09 15:38:38 +02001717/* Helpers for MAC drivers */
1718
1719/**
1720 * phylink_helper_basex_speed() - 1000BaseX/2500BaseX helper
1721 * @state: a pointer to a &struct phylink_link_state
1722 *
1723 * Inspect the interface mode, advertising mask or forced speed and
1724 * decide whether to run at 2.5Gbit or 1Gbit appropriately, switching
1725 * the interface mode to suit. @state->interface is appropriately
1726 * updated, and the advertising mask has the "other" baseX_Full flag
1727 * cleared.
1728 */
1729void phylink_helper_basex_speed(struct phylink_link_state *state)
1730{
1731 if (phy_interface_mode_is_8023z(state->interface)) {
1732 bool want_2500 = state->an_enabled ?
1733 phylink_test(state->advertising, 2500baseX_Full) :
1734 state->speed == SPEED_2500;
1735
1736 if (want_2500) {
1737 phylink_clear(state->advertising, 1000baseX_Full);
1738 state->interface = PHY_INTERFACE_MODE_2500BASEX;
1739 } else {
1740 phylink_clear(state->advertising, 2500baseX_Full);
1741 state->interface = PHY_INTERFACE_MODE_1000BASEX;
1742 }
1743 }
1744}
1745EXPORT_SYMBOL_GPL(phylink_helper_basex_speed);
1746
Andrew Lunn5f857572019-01-21 19:10:19 +01001747MODULE_LICENSE("GPL v2");