blob: 2ec140ec79237326a1aac44f7ae56fcc031f5fea [file] [log] [blame]
Russell King9525ae82017-07-25 15:03:13 +01001/*
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
Russell Kingce0aa272017-07-25 15:03:18 +010024#include "sfp.h"
Russell King9525ae82017-07-25 15:03:13 +010025#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,
Russell Kingce0aa272017-07-25 15:03:18 +010036 PHYLINK_DISABLE_LINK,
Russell King9525ae82017-07-25 15:03:13 +010037};
38
Russell King8796c892017-12-01 10:24:47 +000039/**
40 * struct phylink - internal data type for phylink
41 */
Russell King9525ae82017-07-25 15:03:13 +010042struct phylink {
Russell King8796c892017-12-01 10:24:47 +000043 /* private: */
Russell King9525ae82017-07-25 15:03:13 +010044 struct net_device *netdev;
45 const struct phylink_mac_ops *ops;
46
47 unsigned long phylink_disable_state; /* bitmask of disables */
48 struct phy_device *phydev;
49 phy_interface_t link_interface; /* PHY_INTERFACE_xxx */
50 u8 link_an_mode; /* MLO_AN_xxx */
51 u8 link_port; /* The current non-phy ethtool port */
52 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
53
54 /* The link configuration settings */
55 struct phylink_link_state link_config;
56 struct gpio_desc *link_gpio;
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
69static inline void linkmode_zero(unsigned long *dst)
70{
71 bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS);
72}
73
74static inline void linkmode_copy(unsigned long *dst, const unsigned long *src)
75{
76 bitmap_copy(dst, src, __ETHTOOL_LINK_MODE_MASK_NBITS);
77}
78
79static inline void linkmode_and(unsigned long *dst, const unsigned long *a,
80 const unsigned long *b)
81{
82 bitmap_and(dst, a, b, __ETHTOOL_LINK_MODE_MASK_NBITS);
83}
84
85static inline void linkmode_or(unsigned long *dst, const unsigned long *a,
86 const unsigned long *b)
87{
88 bitmap_or(dst, a, b, __ETHTOOL_LINK_MODE_MASK_NBITS);
89}
90
91static inline bool linkmode_empty(const unsigned long *src)
92{
93 return bitmap_empty(src, __ETHTOOL_LINK_MODE_MASK_NBITS);
94}
95
Russell King8796c892017-12-01 10:24:47 +000096/**
97 * phylink_set_port_modes() - set the port type modes in the ethtool mask
98 * @mask: ethtool link mode mask
99 *
100 * Sets all the port type modes in the ethtool mask. MAC drivers should
101 * use this in their 'validate' callback.
102 */
Russell King9525ae82017-07-25 15:03:13 +0100103void phylink_set_port_modes(unsigned long *mask)
104{
105 phylink_set(mask, TP);
106 phylink_set(mask, AUI);
107 phylink_set(mask, MII);
108 phylink_set(mask, FIBRE);
109 phylink_set(mask, BNC);
110 phylink_set(mask, Backplane);
111}
112EXPORT_SYMBOL_GPL(phylink_set_port_modes);
113
114static int phylink_is_empty_linkmode(const unsigned long *linkmode)
115{
116 __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp) = { 0, };
117
118 phylink_set_port_modes(tmp);
119 phylink_set(tmp, Autoneg);
120 phylink_set(tmp, Pause);
121 phylink_set(tmp, Asym_Pause);
122
123 bitmap_andnot(tmp, linkmode, tmp, __ETHTOOL_LINK_MODE_MASK_NBITS);
124
125 return linkmode_empty(tmp);
126}
127
128static const char *phylink_an_mode_str(unsigned int mode)
129{
130 static const char *modestr[] = {
131 [MLO_AN_PHY] = "phy",
132 [MLO_AN_FIXED] = "fixed",
Russell King86a362c2017-12-01 10:24:26 +0000133 [MLO_AN_INBAND] = "inband",
Russell King9525ae82017-07-25 15:03:13 +0100134 };
135
136 return mode < ARRAY_SIZE(modestr) ? modestr[mode] : "unknown";
137}
138
139static int phylink_validate(struct phylink *pl, unsigned long *supported,
140 struct phylink_link_state *state)
141{
142 pl->ops->validate(pl->netdev, supported, state);
143
144 return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
145}
146
Russell King8fa7b9b2017-12-01 10:25:09 +0000147static int phylink_parse_fixedlink(struct phylink *pl,
148 struct fwnode_handle *fwnode)
Russell King9525ae82017-07-25 15:03:13 +0100149{
Russell King8fa7b9b2017-12-01 10:25:09 +0000150 struct fwnode_handle *fixed_node;
Russell King9525ae82017-07-25 15:03:13 +0100151 const struct phy_setting *s;
152 struct gpio_desc *desc;
Russell King9525ae82017-07-25 15:03:13 +0100153 u32 speed;
Russell King8fa7b9b2017-12-01 10:25:09 +0000154 int ret;
Russell King9525ae82017-07-25 15:03:13 +0100155
Russell King8fa7b9b2017-12-01 10:25:09 +0000156 fixed_node = fwnode_get_named_child_node(fwnode, "fixed-link");
Russell King9525ae82017-07-25 15:03:13 +0100157 if (fixed_node) {
Russell King8fa7b9b2017-12-01 10:25:09 +0000158 ret = fwnode_property_read_u32(fixed_node, "speed", &speed);
Russell King9525ae82017-07-25 15:03:13 +0100159
160 pl->link_config.speed = speed;
161 pl->link_config.duplex = DUPLEX_HALF;
162
Russell King8fa7b9b2017-12-01 10:25:09 +0000163 if (fwnode_property_read_bool(fixed_node, "full-duplex"))
Russell King9525ae82017-07-25 15:03:13 +0100164 pl->link_config.duplex = DUPLEX_FULL;
165
166 /* We treat the "pause" and "asym-pause" terminology as
167 * defining the link partner's ability. */
Russell King8fa7b9b2017-12-01 10:25:09 +0000168 if (fwnode_property_read_bool(fixed_node, "pause"))
Russell King9525ae82017-07-25 15:03:13 +0100169 pl->link_config.pause |= MLO_PAUSE_SYM;
Russell King8fa7b9b2017-12-01 10:25:09 +0000170 if (fwnode_property_read_bool(fixed_node, "asym-pause"))
Russell King9525ae82017-07-25 15:03:13 +0100171 pl->link_config.pause |= MLO_PAUSE_ASYM;
172
173 if (ret == 0) {
Russell King8fa7b9b2017-12-01 10:25:09 +0000174 desc = fwnode_get_named_gpiod(fixed_node, "link-gpios",
175 0, GPIOD_IN, "?");
Russell King9525ae82017-07-25 15:03:13 +0100176
177 if (!IS_ERR(desc))
178 pl->link_gpio = desc;
179 else if (desc == ERR_PTR(-EPROBE_DEFER))
180 ret = -EPROBE_DEFER;
181 }
Russell King8fa7b9b2017-12-01 10:25:09 +0000182 fwnode_handle_put(fixed_node);
Russell King9525ae82017-07-25 15:03:13 +0100183
184 if (ret)
185 return ret;
186 } else {
Russell King8fa7b9b2017-12-01 10:25:09 +0000187 u32 prop[5];
188
189 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
190 NULL, 0);
191 if (ret != ARRAY_SIZE(prop)) {
Russell King9525ae82017-07-25 15:03:13 +0100192 netdev_err(pl->netdev, "broken fixed-link?\n");
193 return -EINVAL;
194 }
Russell King8fa7b9b2017-12-01 10:25:09 +0000195
196 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
197 prop, ARRAY_SIZE(prop));
198 if (!ret) {
199 pl->link_config.duplex = prop[1] ?
Russell King9525ae82017-07-25 15:03:13 +0100200 DUPLEX_FULL : DUPLEX_HALF;
Russell King8fa7b9b2017-12-01 10:25:09 +0000201 pl->link_config.speed = prop[2];
202 if (prop[3])
Russell King9525ae82017-07-25 15:03:13 +0100203 pl->link_config.pause |= MLO_PAUSE_SYM;
Russell King8fa7b9b2017-12-01 10:25:09 +0000204 if (prop[4])
Russell King9525ae82017-07-25 15:03:13 +0100205 pl->link_config.pause |= MLO_PAUSE_ASYM;
206 }
207 }
208
209 if (pl->link_config.speed > SPEED_1000 &&
210 pl->link_config.duplex != DUPLEX_FULL)
211 netdev_warn(pl->netdev, "fixed link specifies half duplex for %dMbps link?\n",
212 pl->link_config.speed);
213
214 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
215 linkmode_copy(pl->link_config.advertising, pl->supported);
216 phylink_validate(pl, pl->supported, &pl->link_config);
217
218 s = phy_lookup_setting(pl->link_config.speed, pl->link_config.duplex,
219 pl->supported,
220 __ETHTOOL_LINK_MODE_MASK_NBITS, true);
221 linkmode_zero(pl->supported);
222 phylink_set(pl->supported, MII);
223 if (s) {
224 __set_bit(s->bit, pl->supported);
225 } else {
226 netdev_warn(pl->netdev, "fixed link %s duplex %dMbps not recognised\n",
227 pl->link_config.duplex == DUPLEX_FULL ? "full" : "half",
228 pl->link_config.speed);
229 }
230
231 linkmode_and(pl->link_config.advertising, pl->link_config.advertising,
232 pl->supported);
233
234 pl->link_config.link = 1;
235 pl->link_config.an_complete = 1;
236
237 return 0;
238}
239
Russell King8fa7b9b2017-12-01 10:25:09 +0000240static int phylink_parse_mode(struct phylink *pl, struct fwnode_handle *fwnode)
Russell King9525ae82017-07-25 15:03:13 +0100241{
Russell King8fa7b9b2017-12-01 10:25:09 +0000242 struct fwnode_handle *dn;
Russell King9525ae82017-07-25 15:03:13 +0100243 const char *managed;
244
Russell King8fa7b9b2017-12-01 10:25:09 +0000245 dn = fwnode_get_named_child_node(fwnode, "fixed-link");
246 if (dn || fwnode_property_present(fwnode, "fixed-link"))
Russell King9525ae82017-07-25 15:03:13 +0100247 pl->link_an_mode = MLO_AN_FIXED;
Russell King8fa7b9b2017-12-01 10:25:09 +0000248 fwnode_handle_put(dn);
Russell King9525ae82017-07-25 15:03:13 +0100249
Russell King8fa7b9b2017-12-01 10:25:09 +0000250 if (fwnode_property_read_string(fwnode, "managed", &managed) == 0 &&
Russell King9525ae82017-07-25 15:03:13 +0100251 strcmp(managed, "in-band-status") == 0) {
252 if (pl->link_an_mode == MLO_AN_FIXED) {
253 netdev_err(pl->netdev,
254 "can't use both fixed-link and in-band-status\n");
255 return -EINVAL;
256 }
257
258 linkmode_zero(pl->supported);
259 phylink_set(pl->supported, MII);
260 phylink_set(pl->supported, Autoneg);
261 phylink_set(pl->supported, Asym_Pause);
262 phylink_set(pl->supported, Pause);
263 pl->link_config.an_enabled = true;
Russell King86a362c2017-12-01 10:24:26 +0000264 pl->link_an_mode = MLO_AN_INBAND;
Russell King9525ae82017-07-25 15:03:13 +0100265
266 switch (pl->link_config.interface) {
267 case PHY_INTERFACE_MODE_SGMII:
268 phylink_set(pl->supported, 10baseT_Half);
269 phylink_set(pl->supported, 10baseT_Full);
270 phylink_set(pl->supported, 100baseT_Half);
271 phylink_set(pl->supported, 100baseT_Full);
272 phylink_set(pl->supported, 1000baseT_Half);
273 phylink_set(pl->supported, 1000baseT_Full);
Russell King9525ae82017-07-25 15:03:13 +0100274 break;
275
276 case PHY_INTERFACE_MODE_1000BASEX:
277 phylink_set(pl->supported, 1000baseX_Full);
Russell King9525ae82017-07-25 15:03:13 +0100278 break;
279
280 case PHY_INTERFACE_MODE_2500BASEX:
281 phylink_set(pl->supported, 2500baseX_Full);
Russell King9525ae82017-07-25 15:03:13 +0100282 break;
283
Russell Kingda7c1862017-07-25 15:03:34 +0100284 case PHY_INTERFACE_MODE_10GKR:
285 phylink_set(pl->supported, 10baseT_Half);
286 phylink_set(pl->supported, 10baseT_Full);
287 phylink_set(pl->supported, 100baseT_Half);
288 phylink_set(pl->supported, 100baseT_Full);
289 phylink_set(pl->supported, 1000baseT_Half);
290 phylink_set(pl->supported, 1000baseT_Full);
291 phylink_set(pl->supported, 1000baseX_Full);
292 phylink_set(pl->supported, 10000baseKR_Full);
293 phylink_set(pl->supported, 10000baseCR_Full);
294 phylink_set(pl->supported, 10000baseSR_Full);
295 phylink_set(pl->supported, 10000baseLR_Full);
296 phylink_set(pl->supported, 10000baseLRM_Full);
297 phylink_set(pl->supported, 10000baseER_Full);
Russell Kingda7c1862017-07-25 15:03:34 +0100298 break;
299
Russell King9525ae82017-07-25 15:03:13 +0100300 default:
301 netdev_err(pl->netdev,
302 "incorrect link mode %s for in-band status\n",
303 phy_modes(pl->link_config.interface));
304 return -EINVAL;
305 }
306
307 linkmode_copy(pl->link_config.advertising, pl->supported);
308
309 if (phylink_validate(pl, pl->supported, &pl->link_config)) {
310 netdev_err(pl->netdev,
311 "failed to validate link configuration for in-band status\n");
312 return -EINVAL;
313 }
314 }
315
316 return 0;
317}
318
319static void phylink_mac_config(struct phylink *pl,
320 const struct phylink_link_state *state)
321{
322 netdev_dbg(pl->netdev,
323 "%s: mode=%s/%s/%s/%s adv=%*pb pause=%02x link=%u an=%u\n",
324 __func__, phylink_an_mode_str(pl->link_an_mode),
325 phy_modes(state->interface),
326 phy_speed_to_str(state->speed),
327 phy_duplex_to_str(state->duplex),
328 __ETHTOOL_LINK_MODE_MASK_NBITS, state->advertising,
329 state->pause, state->link, state->an_enabled);
330
331 pl->ops->mac_config(pl->netdev, pl->link_an_mode, state);
332}
333
334static void phylink_mac_an_restart(struct phylink *pl)
335{
336 if (pl->link_config.an_enabled &&
Russell King365c1e62017-12-01 10:24:16 +0000337 phy_interface_mode_is_8023z(pl->link_config.interface))
Russell King9525ae82017-07-25 15:03:13 +0100338 pl->ops->mac_an_restart(pl->netdev);
339}
340
341static int phylink_get_mac_state(struct phylink *pl, struct phylink_link_state *state)
342{
343 struct net_device *ndev = pl->netdev;
344
345 linkmode_copy(state->advertising, pl->link_config.advertising);
346 linkmode_zero(state->lp_advertising);
347 state->interface = pl->link_config.interface;
348 state->an_enabled = pl->link_config.an_enabled;
349 state->link = 1;
350
351 return pl->ops->mac_link_state(ndev, state);
352}
353
354/* The fixed state is... fixed except for the link state,
Florian Fainelli1ac63e32017-12-12 16:00:28 -0800355 * which may be determined by a GPIO or a callback.
Russell King9525ae82017-07-25 15:03:13 +0100356 */
357static void phylink_get_fixed_state(struct phylink *pl, struct phylink_link_state *state)
358{
359 *state = pl->link_config;
Florian Fainelli1ac63e32017-12-12 16:00:28 -0800360 if (pl->get_fixed_state)
361 pl->get_fixed_state(pl->netdev, state);
362 else if (pl->link_gpio)
Russell King9525ae82017-07-25 15:03:13 +0100363 state->link = !!gpiod_get_value(pl->link_gpio);
364}
365
366/* Flow control is resolved according to our and the link partners
367 * advertisments using the following drawn from the 802.3 specs:
368 * Local device Link partner
369 * Pause AsymDir Pause AsymDir Result
370 * 1 X 1 X TX+RX
371 * 0 1 1 1 RX
372 * 1 1 0 1 TX
373 */
374static void phylink_resolve_flow(struct phylink *pl,
Florian Fainelli516b29e2017-10-30 21:42:57 -0700375 struct phylink_link_state *state)
Russell King9525ae82017-07-25 15:03:13 +0100376{
377 int new_pause = 0;
378
379 if (pl->link_config.pause & MLO_PAUSE_AN) {
380 int pause = 0;
381
382 if (phylink_test(pl->link_config.advertising, Pause))
383 pause |= MLO_PAUSE_SYM;
384 if (phylink_test(pl->link_config.advertising, Asym_Pause))
385 pause |= MLO_PAUSE_ASYM;
386
387 pause &= state->pause;
388
389 if (pause & MLO_PAUSE_SYM)
390 new_pause = MLO_PAUSE_TX | MLO_PAUSE_RX;
391 else if (pause & MLO_PAUSE_ASYM)
392 new_pause = state->pause & MLO_PAUSE_SYM ?
393 MLO_PAUSE_RX : MLO_PAUSE_TX;
394 } else {
395 new_pause = pl->link_config.pause & MLO_PAUSE_TXRX_MASK;
396 }
397
398 state->pause &= ~MLO_PAUSE_TXRX_MASK;
399 state->pause |= new_pause;
400}
401
402static const char *phylink_pause_to_str(int pause)
403{
404 switch (pause & MLO_PAUSE_TXRX_MASK) {
405 case MLO_PAUSE_TX | MLO_PAUSE_RX:
406 return "rx/tx";
407 case MLO_PAUSE_TX:
408 return "tx";
409 case MLO_PAUSE_RX:
410 return "rx";
411 default:
412 return "off";
413 }
414}
415
416static void phylink_resolve(struct work_struct *w)
417{
418 struct phylink *pl = container_of(w, struct phylink, resolve);
419 struct phylink_link_state link_state;
420 struct net_device *ndev = pl->netdev;
421
422 mutex_lock(&pl->state_mutex);
423 if (pl->phylink_disable_state) {
424 pl->mac_link_dropped = false;
425 link_state.link = false;
426 } else if (pl->mac_link_dropped) {
427 link_state.link = false;
428 } else {
429 switch (pl->link_an_mode) {
430 case MLO_AN_PHY:
431 link_state = pl->phy_state;
432 phylink_resolve_flow(pl, &link_state);
433 phylink_mac_config(pl, &link_state);
434 break;
435
436 case MLO_AN_FIXED:
437 phylink_get_fixed_state(pl, &link_state);
438 phylink_mac_config(pl, &link_state);
439 break;
440
Russell King86a362c2017-12-01 10:24:26 +0000441 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +0100442 phylink_get_mac_state(pl, &link_state);
443 if (pl->phydev) {
444 bool changed = false;
445
446 link_state.link = link_state.link &&
447 pl->phy_state.link;
448
449 if (pl->phy_state.interface !=
450 link_state.interface) {
451 link_state.interface = pl->phy_state.interface;
452 changed = true;
453 }
454
455 /* Propagate the flow control from the PHY
456 * to the MAC. Also propagate the interface
457 * if changed.
458 */
459 if (pl->phy_state.link || changed) {
460 link_state.pause |= pl->phy_state.pause;
461 phylink_resolve_flow(pl, &link_state);
462
463 phylink_mac_config(pl, &link_state);
464 }
465 }
466 break;
Russell King9525ae82017-07-25 15:03:13 +0100467 }
468 }
469
470 if (link_state.link != netif_carrier_ok(ndev)) {
471 if (!link_state.link) {
472 netif_carrier_off(ndev);
473 pl->ops->mac_link_down(ndev, pl->link_an_mode);
474 netdev_info(ndev, "Link is Down\n");
475 } else {
476 pl->ops->mac_link_up(ndev, pl->link_an_mode,
477 pl->phydev);
478
479 netif_carrier_on(ndev);
480
481 netdev_info(ndev,
482 "Link is Up - %s/%s - flow control %s\n",
483 phy_speed_to_str(link_state.speed),
484 phy_duplex_to_str(link_state.duplex),
485 phylink_pause_to_str(link_state.pause));
486 }
487 }
488 if (!link_state.link && pl->mac_link_dropped) {
489 pl->mac_link_dropped = false;
490 queue_work(system_power_efficient_wq, &pl->resolve);
491 }
492 mutex_unlock(&pl->state_mutex);
493}
494
495static void phylink_run_resolve(struct phylink *pl)
496{
497 if (!pl->phylink_disable_state)
498 queue_work(system_power_efficient_wq, &pl->resolve);
499}
500
Russell Kingce0aa272017-07-25 15:03:18 +0100501static const struct sfp_upstream_ops sfp_phylink_ops;
502
Russell King8fa7b9b2017-12-01 10:25:09 +0000503static int phylink_register_sfp(struct phylink *pl,
504 struct fwnode_handle *fwnode)
Russell Kingce0aa272017-07-25 15:03:18 +0100505{
Russell King8fa7b9b2017-12-01 10:25:09 +0000506 struct fwnode_reference_args ref;
507 int ret;
Russell Kingce0aa272017-07-25 15:03:18 +0100508
Florian Fainelli02477802017-12-14 15:57:58 -0800509 if (!fwnode)
510 return 0;
511
Russell King8fa7b9b2017-12-01 10:25:09 +0000512 ret = fwnode_property_get_reference_args(fwnode, "sfp", NULL,
513 0, 0, &ref);
514 if (ret < 0) {
515 if (ret == -ENOENT)
516 return 0;
Russell Kingce0aa272017-07-25 15:03:18 +0100517
Russell King8fa7b9b2017-12-01 10:25:09 +0000518 netdev_err(pl->netdev, "unable to parse \"sfp\" node: %d\n",
519 ret);
520 return ret;
521 }
522
523 pl->sfp_bus = sfp_register_upstream(ref.fwnode, pl->netdev, pl,
Russell Kingce0aa272017-07-25 15:03:18 +0100524 &sfp_phylink_ops);
525 if (!pl->sfp_bus)
526 return -ENOMEM;
527
528 return 0;
529}
530
Russell King8796c892017-12-01 10:24:47 +0000531/**
532 * phylink_create() - create a phylink instance
533 * @ndev: a pointer to the &struct net_device
Russell King8fa7b9b2017-12-01 10:25:09 +0000534 * @fwnode: a pointer to a &struct fwnode_handle describing the network
535 * interface
Russell King8796c892017-12-01 10:24:47 +0000536 * @iface: the desired link mode defined by &typedef phy_interface_t
537 * @ops: a pointer to a &struct phylink_mac_ops for the MAC.
538 *
539 * Create a new phylink instance, and parse the link parameters found in @np.
540 * This will parse in-band modes, fixed-link or SFP configuration.
541 *
542 * Returns a pointer to a &struct phylink, or an error-pointer value. Users
543 * must use IS_ERR() to check for errors from this function.
544 */
Russell King8fa7b9b2017-12-01 10:25:09 +0000545struct phylink *phylink_create(struct net_device *ndev,
546 struct fwnode_handle *fwnode,
Florian Fainelli516b29e2017-10-30 21:42:57 -0700547 phy_interface_t iface,
548 const struct phylink_mac_ops *ops)
Russell King9525ae82017-07-25 15:03:13 +0100549{
550 struct phylink *pl;
551 int ret;
552
553 pl = kzalloc(sizeof(*pl), GFP_KERNEL);
554 if (!pl)
555 return ERR_PTR(-ENOMEM);
556
557 mutex_init(&pl->state_mutex);
558 INIT_WORK(&pl->resolve, phylink_resolve);
559 pl->netdev = ndev;
560 pl->phy_state.interface = iface;
561 pl->link_interface = iface;
Florian Fainelli4be11ef2017-12-12 16:00:29 -0800562 if (iface == PHY_INTERFACE_MODE_MOCA)
563 pl->link_port = PORT_BNC;
564 else
565 pl->link_port = PORT_MII;
Russell King9525ae82017-07-25 15:03:13 +0100566 pl->link_config.interface = iface;
567 pl->link_config.pause = MLO_PAUSE_AN;
568 pl->link_config.speed = SPEED_UNKNOWN;
569 pl->link_config.duplex = DUPLEX_UNKNOWN;
570 pl->ops = ops;
571 __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
572
573 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
574 linkmode_copy(pl->link_config.advertising, pl->supported);
575 phylink_validate(pl, pl->supported, &pl->link_config);
576
Russell King8fa7b9b2017-12-01 10:25:09 +0000577 ret = phylink_parse_mode(pl, fwnode);
Russell King9525ae82017-07-25 15:03:13 +0100578 if (ret < 0) {
579 kfree(pl);
580 return ERR_PTR(ret);
581 }
582
583 if (pl->link_an_mode == MLO_AN_FIXED) {
Russell King8fa7b9b2017-12-01 10:25:09 +0000584 ret = phylink_parse_fixedlink(pl, fwnode);
Russell King9525ae82017-07-25 15:03:13 +0100585 if (ret < 0) {
586 kfree(pl);
587 return ERR_PTR(ret);
588 }
589 }
590
Russell King8fa7b9b2017-12-01 10:25:09 +0000591 ret = phylink_register_sfp(pl, fwnode);
Russell Kingce0aa272017-07-25 15:03:18 +0100592 if (ret < 0) {
593 kfree(pl);
594 return ERR_PTR(ret);
595 }
596
Russell King9525ae82017-07-25 15:03:13 +0100597 return pl;
598}
599EXPORT_SYMBOL_GPL(phylink_create);
600
Russell King8796c892017-12-01 10:24:47 +0000601/**
602 * phylink_destroy() - cleanup and destroy the phylink instance
603 * @pl: a pointer to a &struct phylink returned from phylink_create()
604 *
605 * Destroy a phylink instance. Any PHY that has been attached must have been
606 * cleaned up via phylink_disconnect_phy() prior to calling this function.
607 */
Russell King9525ae82017-07-25 15:03:13 +0100608void phylink_destroy(struct phylink *pl)
609{
Russell Kingce0aa272017-07-25 15:03:18 +0100610 if (pl->sfp_bus)
611 sfp_unregister_upstream(pl->sfp_bus);
612
Russell King9525ae82017-07-25 15:03:13 +0100613 cancel_work_sync(&pl->resolve);
614 kfree(pl);
615}
616EXPORT_SYMBOL_GPL(phylink_destroy);
617
Wei Yongjun1ec6e532017-11-02 11:14:48 +0000618static void phylink_phy_change(struct phy_device *phydev, bool up,
619 bool do_carrier)
Russell King9525ae82017-07-25 15:03:13 +0100620{
621 struct phylink *pl = phydev->phylink;
622
623 mutex_lock(&pl->state_mutex);
624 pl->phy_state.speed = phydev->speed;
625 pl->phy_state.duplex = phydev->duplex;
626 pl->phy_state.pause = MLO_PAUSE_NONE;
627 if (phydev->pause)
628 pl->phy_state.pause |= MLO_PAUSE_SYM;
629 if (phydev->asym_pause)
630 pl->phy_state.pause |= MLO_PAUSE_ASYM;
631 pl->phy_state.interface = phydev->interface;
632 pl->phy_state.link = up;
633 mutex_unlock(&pl->state_mutex);
634
635 phylink_run_resolve(pl);
636
637 netdev_dbg(pl->netdev, "phy link %s %s/%s/%s\n", up ? "up" : "down",
Florian Fainelli516b29e2017-10-30 21:42:57 -0700638 phy_modes(phydev->interface),
Russell King9525ae82017-07-25 15:03:13 +0100639 phy_speed_to_str(phydev->speed),
640 phy_duplex_to_str(phydev->duplex));
641}
642
643static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy)
644{
645 struct phylink_link_state config;
646 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
647 u32 advertising;
648 int ret;
649
650 memset(&config, 0, sizeof(config));
651 ethtool_convert_legacy_u32_to_link_mode(supported, phy->supported);
652 ethtool_convert_legacy_u32_to_link_mode(config.advertising,
653 phy->advertising);
654 config.interface = pl->link_config.interface;
655
656 /*
657 * This is the new way of dealing with flow control for PHYs,
658 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
659 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
660 * using our validate call to the MAC, we rely upon the MAC
661 * clearing the bits from both supported and advertising fields.
662 */
663 if (phylink_test(supported, Pause))
664 phylink_set(config.advertising, Pause);
665 if (phylink_test(supported, Asym_Pause))
666 phylink_set(config.advertising, Asym_Pause);
667
668 ret = phylink_validate(pl, supported, &config);
669 if (ret)
670 return ret;
671
672 phy->phylink = pl;
673 phy->phy_link_change = phylink_phy_change;
674
675 netdev_info(pl->netdev,
676 "PHY [%s] driver [%s]\n", dev_name(&phy->mdio.dev),
677 phy->drv->name);
678
679 mutex_lock(&phy->lock);
680 mutex_lock(&pl->state_mutex);
681 pl->netdev->phydev = phy;
682 pl->phydev = phy;
683 linkmode_copy(pl->supported, supported);
684 linkmode_copy(pl->link_config.advertising, config.advertising);
685
686 /* Restrict the phy advertisment according to the MAC support. */
687 ethtool_convert_link_mode_to_legacy_u32(&advertising, config.advertising);
688 phy->advertising = advertising;
689 mutex_unlock(&pl->state_mutex);
690 mutex_unlock(&phy->lock);
691
692 netdev_dbg(pl->netdev,
693 "phy: setting supported %*pb advertising 0x%08x\n",
694 __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
695 phy->advertising);
696
697 phy_start_machine(phy);
698 if (phy->irq > 0)
699 phy_start_interrupts(phy);
700
701 return 0;
702}
703
Russell King8796c892017-12-01 10:24:47 +0000704/**
705 * phylink_connect_phy() - connect a PHY to the phylink instance
706 * @pl: a pointer to a &struct phylink returned from phylink_create()
707 * @phy: a pointer to a &struct phy_device.
708 *
709 * Connect @phy to the phylink instance specified by @pl by calling
710 * phy_attach_direct(). Configure the @phy according to the MAC driver's
711 * capabilities, start the PHYLIB state machine and enable any interrupts
712 * that the PHY supports.
713 *
714 * This updates the phylink's ethtool supported and advertising link mode
715 * masks.
716 *
717 * Returns 0 on success or a negative errno.
718 */
Russell King9525ae82017-07-25 15:03:13 +0100719int phylink_connect_phy(struct phylink *pl, struct phy_device *phy)
720{
721 int ret;
722
Russell Kingcf4f2672017-12-01 10:24:21 +0000723 if (WARN_ON(pl->link_an_mode == MLO_AN_FIXED ||
Russell King86a362c2017-12-01 10:24:26 +0000724 (pl->link_an_mode == MLO_AN_INBAND &&
725 phy_interface_mode_is_8023z(pl->link_interface))))
Russell Kingcf4f2672017-12-01 10:24:21 +0000726 return -EINVAL;
727
Russell Kingfe1c3ef2017-12-20 23:23:33 +0000728 if (pl->phydev)
729 return -EBUSY;
730
Florian Fainelli4904b6e2017-12-12 16:00:26 -0800731 /* Use PHY device/driver interface */
732 if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
733 pl->link_interface = phy->interface;
734 pl->link_config.interface = pl->link_interface;
735 }
736
Russell King9525ae82017-07-25 15:03:13 +0100737 ret = phy_attach_direct(pl->netdev, phy, 0, pl->link_interface);
738 if (ret)
739 return ret;
740
741 ret = phylink_bringup_phy(pl, phy);
742 if (ret)
743 phy_detach(phy);
744
745 return ret;
746}
747EXPORT_SYMBOL_GPL(phylink_connect_phy);
748
Russell King8796c892017-12-01 10:24:47 +0000749/**
750 * phylink_of_phy_connect() - connect the PHY specified in the DT mode.
751 * @pl: a pointer to a &struct phylink returned from phylink_create()
752 * @dn: a pointer to a &struct device_node.
Florian Fainelli0a629642017-12-12 16:00:25 -0800753 * @flags: PHY-specific flags to communicate to the PHY device driver
Russell King8796c892017-12-01 10:24:47 +0000754 *
755 * Connect the phy specified in the device node @dn to the phylink instance
756 * specified by @pl. Actions specified in phylink_connect_phy() will be
757 * performed.
758 *
759 * Returns 0 on success or a negative errno.
760 */
Florian Fainelli0a629642017-12-12 16:00:25 -0800761int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn,
762 u32 flags)
Russell King9525ae82017-07-25 15:03:13 +0100763{
764 struct device_node *phy_node;
765 struct phy_device *phy_dev;
766 int ret;
767
Russell Kingcf4f2672017-12-01 10:24:21 +0000768 /* Fixed links and 802.3z are handled without needing a PHY */
769 if (pl->link_an_mode == MLO_AN_FIXED ||
Russell King86a362c2017-12-01 10:24:26 +0000770 (pl->link_an_mode == MLO_AN_INBAND &&
771 phy_interface_mode_is_8023z(pl->link_interface)))
Russell King9525ae82017-07-25 15:03:13 +0100772 return 0;
773
774 phy_node = of_parse_phandle(dn, "phy-handle", 0);
775 if (!phy_node)
776 phy_node = of_parse_phandle(dn, "phy", 0);
777 if (!phy_node)
778 phy_node = of_parse_phandle(dn, "phy-device", 0);
779
780 if (!phy_node) {
Florian Fainellid38b4afd2017-12-12 16:00:27 -0800781 if (pl->link_an_mode == MLO_AN_PHY)
Russell King9525ae82017-07-25 15:03:13 +0100782 return -ENODEV;
Russell King9525ae82017-07-25 15:03:13 +0100783 return 0;
784 }
785
Florian Fainelli0a629642017-12-12 16:00:25 -0800786 phy_dev = of_phy_attach(pl->netdev, phy_node, flags,
787 pl->link_interface);
Russell King9525ae82017-07-25 15:03:13 +0100788 /* We're done with the phy_node handle */
789 of_node_put(phy_node);
790
791 if (!phy_dev)
792 return -ENODEV;
793
794 ret = phylink_bringup_phy(pl, phy_dev);
795 if (ret)
796 phy_detach(phy_dev);
797
798 return ret;
799}
800EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
801
Russell King8796c892017-12-01 10:24:47 +0000802/**
803 * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
804 * instance.
805 * @pl: a pointer to a &struct phylink returned from phylink_create()
806 *
807 * Disconnect any current PHY from the phylink instance described by @pl.
808 */
Russell King9525ae82017-07-25 15:03:13 +0100809void phylink_disconnect_phy(struct phylink *pl)
810{
811 struct phy_device *phy;
812
Russell King8b874512017-12-15 16:09:47 +0000813 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +0100814
815 phy = pl->phydev;
816 if (phy) {
817 mutex_lock(&phy->lock);
818 mutex_lock(&pl->state_mutex);
819 pl->netdev->phydev = NULL;
820 pl->phydev = NULL;
821 mutex_unlock(&pl->state_mutex);
822 mutex_unlock(&phy->lock);
823 flush_work(&pl->resolve);
824
825 phy_disconnect(phy);
826 }
827}
828EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
829
Russell King8796c892017-12-01 10:24:47 +0000830/**
Florian Fainelli1ac63e32017-12-12 16:00:28 -0800831 * phylink_fixed_state_cb() - allow setting a fixed link callback
832 * @pl: a pointer to a &struct phylink returned from phylink_create()
833 * @cb: callback to execute to determine the fixed link state.
834 *
835 * The MAC driver should call this driver when the state of its link
836 * can be determined through e.g: an out of band MMIO register.
837 */
838int phylink_fixed_state_cb(struct phylink *pl,
839 void (*cb)(struct net_device *dev,
840 struct phylink_link_state *state))
841{
842 /* It does not make sense to let the link be overriden unless we use
843 * MLO_AN_FIXED
844 */
845 if (pl->link_an_mode != MLO_AN_FIXED)
846 return -EINVAL;
847
848 mutex_lock(&pl->state_mutex);
849 pl->get_fixed_state = cb;
850 mutex_unlock(&pl->state_mutex);
851
852 return 0;
853}
854EXPORT_SYMBOL_GPL(phylink_fixed_state_cb);
855
856/**
Russell King8796c892017-12-01 10:24:47 +0000857 * phylink_mac_change() - notify phylink of a change in MAC state
858 * @pl: a pointer to a &struct phylink returned from phylink_create()
859 * @up: indicates whether the link is currently up.
860 *
861 * The MAC driver should call this driver when the state of its link
862 * changes (eg, link failure, new negotiation results, etc.)
863 */
Russell King9525ae82017-07-25 15:03:13 +0100864void phylink_mac_change(struct phylink *pl, bool up)
865{
866 if (!up)
867 pl->mac_link_dropped = true;
868 phylink_run_resolve(pl);
869 netdev_dbg(pl->netdev, "mac link %s\n", up ? "up" : "down");
870}
871EXPORT_SYMBOL_GPL(phylink_mac_change);
872
Russell King8796c892017-12-01 10:24:47 +0000873/**
874 * phylink_start() - start a phylink instance
875 * @pl: a pointer to a &struct phylink returned from phylink_create()
876 *
877 * Start the phylink instance specified by @pl, configuring the MAC for the
878 * desired link mode(s) and negotiation style. This should be called from the
879 * network device driver's &struct net_device_ops ndo_open() method.
880 */
Russell King9525ae82017-07-25 15:03:13 +0100881void phylink_start(struct phylink *pl)
882{
Russell King8b874512017-12-15 16:09:47 +0000883 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +0100884
885 netdev_info(pl->netdev, "configuring for %s/%s link mode\n",
886 phylink_an_mode_str(pl->link_an_mode),
887 phy_modes(pl->link_config.interface));
888
889 /* Apply the link configuration to the MAC when starting. This allows
890 * a fixed-link to start with the correct parameters, and also
891 * ensures that we set the appropriate advertisment for Serdes links.
892 */
893 phylink_resolve_flow(pl, &pl->link_config);
894 phylink_mac_config(pl, &pl->link_config);
895
Russell King85b43942017-12-01 10:24:42 +0000896 /* Restart autonegotiation if using 802.3z to ensure that the link
897 * parameters are properly negotiated. This is necessary for DSA
898 * switches using 802.3z negotiation to ensure they see our modes.
899 */
900 phylink_mac_an_restart(pl);
901
Russell King9525ae82017-07-25 15:03:13 +0100902 clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
903 phylink_run_resolve(pl);
904
Russell Kingce0aa272017-07-25 15:03:18 +0100905 if (pl->sfp_bus)
906 sfp_upstream_start(pl->sfp_bus);
Russell King9525ae82017-07-25 15:03:13 +0100907 if (pl->phydev)
908 phy_start(pl->phydev);
909}
910EXPORT_SYMBOL_GPL(phylink_start);
911
Russell King8796c892017-12-01 10:24:47 +0000912/**
913 * phylink_stop() - stop a phylink instance
914 * @pl: a pointer to a &struct phylink returned from phylink_create()
915 *
916 * Stop the phylink instance specified by @pl. This should be called from the
917 * network device driver's &struct net_device_ops ndo_stop() method. The
918 * network device's carrier state should not be changed prior to calling this
919 * function.
920 */
Russell King9525ae82017-07-25 15:03:13 +0100921void phylink_stop(struct phylink *pl)
922{
Russell King8b874512017-12-15 16:09:47 +0000923 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +0100924
925 if (pl->phydev)
926 phy_stop(pl->phydev);
Russell Kingce0aa272017-07-25 15:03:18 +0100927 if (pl->sfp_bus)
928 sfp_upstream_stop(pl->sfp_bus);
Russell King9525ae82017-07-25 15:03:13 +0100929
930 set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
Russell King2012b7d2017-11-30 13:59:26 +0000931 queue_work(system_power_efficient_wq, &pl->resolve);
Russell King9525ae82017-07-25 15:03:13 +0100932 flush_work(&pl->resolve);
933}
934EXPORT_SYMBOL_GPL(phylink_stop);
935
Russell King8796c892017-12-01 10:24:47 +0000936/**
937 * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY
938 * @pl: a pointer to a &struct phylink returned from phylink_create()
939 * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters
940 *
941 * Read the wake on lan parameters from the PHY attached to the phylink
942 * instance specified by @pl. If no PHY is currently attached, report no
943 * support for wake on lan.
944 */
Russell King9525ae82017-07-25 15:03:13 +0100945void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
946{
Russell King8b874512017-12-15 16:09:47 +0000947 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +0100948
949 wol->supported = 0;
950 wol->wolopts = 0;
951
952 if (pl->phydev)
953 phy_ethtool_get_wol(pl->phydev, wol);
954}
955EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol);
956
Russell King8796c892017-12-01 10:24:47 +0000957/**
958 * phylink_ethtool_set_wol() - set wake on lan parameters
959 * @pl: a pointer to a &struct phylink returned from phylink_create()
960 * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters
961 *
962 * Set the wake on lan parameters for the PHY attached to the phylink
963 * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP
964 * error.
965 *
966 * Returns zero on success or negative errno code.
967 */
Russell King9525ae82017-07-25 15:03:13 +0100968int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
969{
970 int ret = -EOPNOTSUPP;
971
Russell King8b874512017-12-15 16:09:47 +0000972 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +0100973
974 if (pl->phydev)
975 ret = phy_ethtool_set_wol(pl->phydev, wol);
976
977 return ret;
978}
979EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol);
980
981static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b)
982{
983 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
984
985 linkmode_zero(mask);
986 phylink_set_port_modes(mask);
987
988 linkmode_and(dst, dst, mask);
989 linkmode_or(dst, dst, b);
990}
991
992static void phylink_get_ksettings(const struct phylink_link_state *state,
993 struct ethtool_link_ksettings *kset)
994{
995 phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
996 linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
997 kset->base.speed = state->speed;
998 kset->base.duplex = state->duplex;
999 kset->base.autoneg = state->an_enabled ? AUTONEG_ENABLE :
1000 AUTONEG_DISABLE;
1001}
1002
Russell King8796c892017-12-01 10:24:47 +00001003/**
1004 * phylink_ethtool_ksettings_get() - get the current link settings
1005 * @pl: a pointer to a &struct phylink returned from phylink_create()
1006 * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings
1007 *
1008 * Read the current link settings for the phylink instance specified by @pl.
1009 * This will be the link settings read from the MAC, PHY or fixed link
1010 * settings depending on the current negotiation mode.
1011 */
Russell King9525ae82017-07-25 15:03:13 +01001012int phylink_ethtool_ksettings_get(struct phylink *pl,
Florian Fainelli516b29e2017-10-30 21:42:57 -07001013 struct ethtool_link_ksettings *kset)
Russell King9525ae82017-07-25 15:03:13 +01001014{
1015 struct phylink_link_state link_state;
1016
Russell King8b874512017-12-15 16:09:47 +00001017 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001018
1019 if (pl->phydev) {
1020 phy_ethtool_ksettings_get(pl->phydev, kset);
1021 } else {
1022 kset->base.port = pl->link_port;
1023 }
1024
1025 linkmode_copy(kset->link_modes.supported, pl->supported);
1026
1027 switch (pl->link_an_mode) {
1028 case MLO_AN_FIXED:
1029 /* We are using fixed settings. Report these as the
1030 * current link settings - and note that these also
1031 * represent the supported speeds/duplex/pause modes.
1032 */
1033 phylink_get_fixed_state(pl, &link_state);
1034 phylink_get_ksettings(&link_state, kset);
1035 break;
1036
Russell King86a362c2017-12-01 10:24:26 +00001037 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001038 /* If there is a phy attached, then use the reported
1039 * settings from the phy with no modification.
1040 */
1041 if (pl->phydev)
1042 break;
1043
Russell King9525ae82017-07-25 15:03:13 +01001044 phylink_get_mac_state(pl, &link_state);
1045
1046 /* The MAC is reporting the link results from its own PCS
1047 * layer via in-band status. Report these as the current
1048 * link settings.
1049 */
1050 phylink_get_ksettings(&link_state, kset);
1051 break;
1052 }
1053
1054 return 0;
1055}
1056EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get);
1057
Russell King8796c892017-12-01 10:24:47 +00001058/**
1059 * phylink_ethtool_ksettings_set() - set the link settings
1060 * @pl: a pointer to a &struct phylink returned from phylink_create()
1061 * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes
1062 */
Russell King9525ae82017-07-25 15:03:13 +01001063int phylink_ethtool_ksettings_set(struct phylink *pl,
Florian Fainelli516b29e2017-10-30 21:42:57 -07001064 const struct ethtool_link_ksettings *kset)
Russell King9525ae82017-07-25 15:03:13 +01001065{
1066 struct ethtool_link_ksettings our_kset;
1067 struct phylink_link_state config;
1068 int ret;
1069
Russell King8b874512017-12-15 16:09:47 +00001070 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001071
1072 if (kset->base.autoneg != AUTONEG_DISABLE &&
1073 kset->base.autoneg != AUTONEG_ENABLE)
1074 return -EINVAL;
1075
1076 config = pl->link_config;
1077
1078 /* Mask out unsupported advertisments */
1079 linkmode_and(config.advertising, kset->link_modes.advertising,
1080 pl->supported);
1081
1082 /* FIXME: should we reject autoneg if phy/mac does not support it? */
1083 if (kset->base.autoneg == AUTONEG_DISABLE) {
1084 const struct phy_setting *s;
1085
1086 /* Autonegotiation disabled, select a suitable speed and
1087 * duplex.
1088 */
1089 s = phy_lookup_setting(kset->base.speed, kset->base.duplex,
1090 pl->supported,
1091 __ETHTOOL_LINK_MODE_MASK_NBITS, false);
1092 if (!s)
1093 return -EINVAL;
1094
1095 /* If we have a fixed link (as specified by firmware), refuse
1096 * to change link parameters.
1097 */
1098 if (pl->link_an_mode == MLO_AN_FIXED &&
1099 (s->speed != pl->link_config.speed ||
1100 s->duplex != pl->link_config.duplex))
1101 return -EINVAL;
1102
1103 config.speed = s->speed;
1104 config.duplex = s->duplex;
1105 config.an_enabled = false;
1106
1107 __clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
1108 } else {
1109 /* If we have a fixed link, refuse to enable autonegotiation */
1110 if (pl->link_an_mode == MLO_AN_FIXED)
1111 return -EINVAL;
1112
1113 config.speed = SPEED_UNKNOWN;
1114 config.duplex = DUPLEX_UNKNOWN;
1115 config.an_enabled = true;
1116
1117 __set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
1118 }
1119
1120 if (phylink_validate(pl, pl->supported, &config))
1121 return -EINVAL;
1122
1123 /* If autonegotiation is enabled, we must have an advertisment */
1124 if (config.an_enabled && phylink_is_empty_linkmode(config.advertising))
1125 return -EINVAL;
1126
1127 our_kset = *kset;
1128 linkmode_copy(our_kset.link_modes.advertising, config.advertising);
1129 our_kset.base.speed = config.speed;
1130 our_kset.base.duplex = config.duplex;
1131
1132 /* If we have a PHY, configure the phy */
1133 if (pl->phydev) {
1134 ret = phy_ethtool_ksettings_set(pl->phydev, &our_kset);
1135 if (ret)
1136 return ret;
1137 }
1138
1139 mutex_lock(&pl->state_mutex);
1140 /* Configure the MAC to match the new settings */
1141 linkmode_copy(pl->link_config.advertising, our_kset.link_modes.advertising);
1142 pl->link_config.speed = our_kset.base.speed;
1143 pl->link_config.duplex = our_kset.base.duplex;
1144 pl->link_config.an_enabled = our_kset.base.autoneg != AUTONEG_DISABLE;
1145
1146 if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
1147 phylink_mac_config(pl, &pl->link_config);
1148 phylink_mac_an_restart(pl);
1149 }
1150 mutex_unlock(&pl->state_mutex);
1151
Dan Carpenterd18c2a12017-08-10 00:35:50 +03001152 return 0;
Russell King9525ae82017-07-25 15:03:13 +01001153}
1154EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
1155
Russell King8796c892017-12-01 10:24:47 +00001156/**
1157 * phylink_ethtool_nway_reset() - restart negotiation
1158 * @pl: a pointer to a &struct phylink returned from phylink_create()
1159 *
1160 * Restart negotiation for the phylink instance specified by @pl. This will
1161 * cause any attached phy to restart negotiation with the link partner, and
1162 * if the MAC is in a BaseX mode, the MAC will also be requested to restart
1163 * negotiation.
1164 *
1165 * Returns zero on success, or negative error code.
1166 */
Russell King9525ae82017-07-25 15:03:13 +01001167int phylink_ethtool_nway_reset(struct phylink *pl)
1168{
1169 int ret = 0;
1170
Russell King8b874512017-12-15 16:09:47 +00001171 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001172
1173 if (pl->phydev)
1174 ret = phy_restart_aneg(pl->phydev);
1175 phylink_mac_an_restart(pl);
1176
1177 return ret;
1178}
1179EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
1180
Russell King8796c892017-12-01 10:24:47 +00001181/**
1182 * phylink_ethtool_get_pauseparam() - get the current pause parameters
1183 * @pl: a pointer to a &struct phylink returned from phylink_create()
1184 * @pause: a pointer to a &struct ethtool_pauseparam
1185 */
Russell King9525ae82017-07-25 15:03:13 +01001186void phylink_ethtool_get_pauseparam(struct phylink *pl,
1187 struct ethtool_pauseparam *pause)
1188{
Russell King8b874512017-12-15 16:09:47 +00001189 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001190
1191 pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
1192 pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
1193 pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
1194}
1195EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
1196
Russell King8796c892017-12-01 10:24:47 +00001197/**
1198 * phylink_ethtool_set_pauseparam() - set the current pause parameters
1199 * @pl: a pointer to a &struct phylink returned from phylink_create()
1200 * @pause: a pointer to a &struct ethtool_pauseparam
1201 */
Russell King9525ae82017-07-25 15:03:13 +01001202int phylink_ethtool_set_pauseparam(struct phylink *pl,
1203 struct ethtool_pauseparam *pause)
1204{
1205 struct phylink_link_state *config = &pl->link_config;
1206
Russell King8b874512017-12-15 16:09:47 +00001207 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001208
1209 if (!phylink_test(pl->supported, Pause) &&
1210 !phylink_test(pl->supported, Asym_Pause))
1211 return -EOPNOTSUPP;
1212
1213 if (!phylink_test(pl->supported, Asym_Pause) &&
1214 !pause->autoneg && pause->rx_pause != pause->tx_pause)
1215 return -EINVAL;
1216
1217 config->pause &= ~(MLO_PAUSE_AN | MLO_PAUSE_TXRX_MASK);
1218
1219 if (pause->autoneg)
1220 config->pause |= MLO_PAUSE_AN;
1221 if (pause->rx_pause)
1222 config->pause |= MLO_PAUSE_RX;
1223 if (pause->tx_pause)
1224 config->pause |= MLO_PAUSE_TX;
1225
1226 if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
1227 switch (pl->link_an_mode) {
1228 case MLO_AN_PHY:
1229 /* Silently mark the carrier down, and then trigger a resolve */
1230 netif_carrier_off(pl->netdev);
1231 phylink_run_resolve(pl);
1232 break;
1233
1234 case MLO_AN_FIXED:
1235 /* Should we allow fixed links to change against the config? */
1236 phylink_resolve_flow(pl, config);
1237 phylink_mac_config(pl, config);
1238 break;
1239
Russell King86a362c2017-12-01 10:24:26 +00001240 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001241 phylink_mac_config(pl, config);
1242 phylink_mac_an_restart(pl);
1243 break;
1244 }
1245 }
1246
1247 return 0;
1248}
1249EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
1250
Russell King770a1ad2017-07-25 15:03:23 +01001251int phylink_ethtool_get_module_info(struct phylink *pl,
1252 struct ethtool_modinfo *modinfo)
1253{
1254 int ret = -EOPNOTSUPP;
1255
1256 WARN_ON(!lockdep_rtnl_is_held());
1257
1258 if (pl->sfp_bus)
1259 ret = sfp_get_module_info(pl->sfp_bus, modinfo);
1260
1261 return ret;
1262}
1263EXPORT_SYMBOL_GPL(phylink_ethtool_get_module_info);
1264
1265int phylink_ethtool_get_module_eeprom(struct phylink *pl,
1266 struct ethtool_eeprom *ee, u8 *buf)
1267{
1268 int ret = -EOPNOTSUPP;
1269
1270 WARN_ON(!lockdep_rtnl_is_held());
1271
1272 if (pl->sfp_bus)
1273 ret = sfp_get_module_eeprom(pl->sfp_bus, ee, buf);
1274
1275 return ret;
1276}
1277EXPORT_SYMBOL_GPL(phylink_ethtool_get_module_eeprom);
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/**
1303 * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters
1304 * @pl: a pointer to a &struct phylink returned from phylink_create()
1305 * @eee: a pointer to a &struct ethtool_eee for the read parameters
1306 */
Russell King9525ae82017-07-25 15:03:13 +01001307int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee)
1308{
1309 int ret = -EOPNOTSUPP;
1310
Russell King8b874512017-12-15 16:09:47 +00001311 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001312
1313 if (pl->phydev)
1314 ret = phy_ethtool_get_eee(pl->phydev, eee);
1315
1316 return ret;
1317}
1318EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
1319
Russell King8796c892017-12-01 10:24:47 +00001320/**
1321 * phylink_ethtool_set_eee() - set 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 desired parameters
1324 */
Russell King9525ae82017-07-25 15:03:13 +01001325int phylink_ethtool_set_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_set_eee(pl->phydev, eee);
1333
1334 return ret;
1335}
1336EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
1337
1338/* This emulates MII registers for a fixed-mode phy operating as per the
1339 * passed in state. "aneg" defines if we report negotiation is possible.
1340 *
1341 * FIXME: should deal with negotiation state too.
1342 */
1343static int phylink_mii_emul_read(struct net_device *ndev, unsigned int reg,
1344 struct phylink_link_state *state, bool aneg)
1345{
1346 struct fixed_phy_status fs;
1347 int val;
1348
1349 fs.link = state->link;
1350 fs.speed = state->speed;
1351 fs.duplex = state->duplex;
1352 fs.pause = state->pause & MLO_PAUSE_SYM;
1353 fs.asym_pause = state->pause & MLO_PAUSE_ASYM;
1354
1355 val = swphy_read_reg(reg, &fs);
1356 if (reg == MII_BMSR) {
1357 if (!state->an_complete)
1358 val &= ~BMSR_ANEGCOMPLETE;
1359 if (!aneg)
1360 val &= ~BMSR_ANEGCAPABLE;
1361 }
1362 return val;
1363}
1364
Russell Kingecbd87b2017-07-25 15:03:28 +01001365static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
1366 unsigned int reg)
1367{
1368 struct phy_device *phydev = pl->phydev;
1369 int prtad, devad;
1370
1371 if (mdio_phy_id_is_c45(phy_id)) {
1372 prtad = mdio_phy_id_prtad(phy_id);
1373 devad = mdio_phy_id_devad(phy_id);
1374 devad = MII_ADDR_C45 | devad << 16 | reg;
1375 } else if (phydev->is_c45) {
1376 switch (reg) {
1377 case MII_BMCR:
1378 case MII_BMSR:
1379 case MII_PHYSID1:
1380 case MII_PHYSID2:
1381 devad = __ffs(phydev->c45_ids.devices_in_package);
1382 break;
1383 case MII_ADVERTISE:
1384 case MII_LPA:
1385 if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1386 return -EINVAL;
1387 devad = MDIO_MMD_AN;
1388 if (reg == MII_ADVERTISE)
1389 reg = MDIO_AN_ADVERTISE;
1390 else
1391 reg = MDIO_AN_LPA;
1392 break;
1393 default:
1394 return -EINVAL;
1395 }
1396 prtad = phy_id;
1397 devad = MII_ADDR_C45 | devad << 16 | reg;
1398 } else {
1399 prtad = phy_id;
1400 devad = reg;
1401 }
1402 return mdiobus_read(pl->phydev->mdio.bus, prtad, devad);
1403}
1404
1405static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
1406 unsigned int reg, unsigned int val)
1407{
1408 struct phy_device *phydev = pl->phydev;
1409 int prtad, devad;
1410
1411 if (mdio_phy_id_is_c45(phy_id)) {
1412 prtad = mdio_phy_id_prtad(phy_id);
1413 devad = mdio_phy_id_devad(phy_id);
1414 devad = MII_ADDR_C45 | devad << 16 | reg;
1415 } else if (phydev->is_c45) {
1416 switch (reg) {
1417 case MII_BMCR:
1418 case MII_BMSR:
1419 case MII_PHYSID1:
1420 case MII_PHYSID2:
1421 devad = __ffs(phydev->c45_ids.devices_in_package);
1422 break;
1423 case MII_ADVERTISE:
1424 case MII_LPA:
1425 if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1426 return -EINVAL;
1427 devad = MDIO_MMD_AN;
1428 if (reg == MII_ADVERTISE)
1429 reg = MDIO_AN_ADVERTISE;
1430 else
1431 reg = MDIO_AN_LPA;
1432 break;
1433 default:
1434 return -EINVAL;
1435 }
1436 prtad = phy_id;
1437 devad = MII_ADDR_C45 | devad << 16 | reg;
1438 } else {
1439 prtad = phy_id;
1440 devad = reg;
1441 }
1442
1443 return mdiobus_write(phydev->mdio.bus, prtad, devad, val);
1444}
1445
Russell King9525ae82017-07-25 15:03:13 +01001446static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
1447 unsigned int reg)
1448{
1449 struct phylink_link_state state;
1450 int val = 0xffff;
1451
Russell King9525ae82017-07-25 15:03:13 +01001452 switch (pl->link_an_mode) {
1453 case MLO_AN_FIXED:
1454 if (phy_id == 0) {
1455 phylink_get_fixed_state(pl, &state);
1456 val = phylink_mii_emul_read(pl->netdev, reg, &state,
1457 true);
1458 }
1459 break;
1460
1461 case MLO_AN_PHY:
1462 return -EOPNOTSUPP;
1463
Russell King86a362c2017-12-01 10:24:26 +00001464 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001465 if (phy_id == 0) {
1466 val = phylink_get_mac_state(pl, &state);
1467 if (val < 0)
1468 return val;
1469
1470 val = phylink_mii_emul_read(pl->netdev, reg, &state,
1471 true);
1472 }
1473 break;
1474 }
1475
1476 return val & 0xffff;
1477}
1478
1479static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
1480 unsigned int reg, unsigned int val)
1481{
Russell King9525ae82017-07-25 15:03:13 +01001482 switch (pl->link_an_mode) {
1483 case MLO_AN_FIXED:
1484 break;
1485
1486 case MLO_AN_PHY:
1487 return -EOPNOTSUPP;
1488
Russell King86a362c2017-12-01 10:24:26 +00001489 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001490 break;
1491 }
1492
1493 return 0;
1494}
1495
Russell King8796c892017-12-01 10:24:47 +00001496/**
1497 * phylink_mii_ioctl() - generic mii ioctl interface
1498 * @pl: a pointer to a &struct phylink returned from phylink_create()
1499 * @ifr: a pointer to a &struct ifreq for socket ioctls
1500 * @cmd: ioctl cmd to execute
1501 *
1502 * Perform the specified MII ioctl on the PHY attached to the phylink instance
1503 * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
1504 *
1505 * Returns: zero on success or negative error code.
1506 *
1507 * %SIOCGMIIPHY:
1508 * read register from the current PHY.
1509 * %SIOCGMIIREG:
1510 * read register from the specified PHY.
1511 * %SIOCSMIIREG:
1512 * set a register on the specified PHY.
1513 */
Russell King9525ae82017-07-25 15:03:13 +01001514int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
1515{
Russell Kingecbd87b2017-07-25 15:03:28 +01001516 struct mii_ioctl_data *mii = if_mii(ifr);
1517 int ret;
Russell King9525ae82017-07-25 15:03:13 +01001518
Russell King8b874512017-12-15 16:09:47 +00001519 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001520
Russell Kingecbd87b2017-07-25 15:03:28 +01001521 if (pl->phydev) {
Russell King86a362c2017-12-01 10:24:26 +00001522 /* PHYs only exist for MLO_AN_PHY and SGMII */
Russell Kingecbd87b2017-07-25 15:03:28 +01001523 switch (cmd) {
1524 case SIOCGMIIPHY:
1525 mii->phy_id = pl->phydev->mdio.addr;
Russell King9525ae82017-07-25 15:03:13 +01001526
Russell Kingecbd87b2017-07-25 15:03:28 +01001527 case SIOCGMIIREG:
1528 ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
1529 if (ret >= 0) {
1530 mii->val_out = ret;
1531 ret = 0;
1532 }
1533 break;
Russell King9525ae82017-07-25 15:03:13 +01001534
Russell Kingecbd87b2017-07-25 15:03:28 +01001535 case SIOCSMIIREG:
1536 ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
1537 mii->val_in);
1538 break;
Russell King9525ae82017-07-25 15:03:13 +01001539
Russell Kingecbd87b2017-07-25 15:03:28 +01001540 default:
Russell King9525ae82017-07-25 15:03:13 +01001541 ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
Russell Kingecbd87b2017-07-25 15:03:28 +01001542 break;
1543 }
1544 } else {
1545 switch (cmd) {
1546 case SIOCGMIIPHY:
1547 mii->phy_id = 0;
1548
1549 case SIOCGMIIREG:
1550 ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
1551 if (ret >= 0) {
1552 mii->val_out = ret;
1553 ret = 0;
1554 }
1555 break;
1556
1557 case SIOCSMIIREG:
1558 ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
1559 mii->val_in);
1560 break;
1561
1562 default:
1563 ret = -EOPNOTSUPP;
1564 break;
1565 }
Russell King9525ae82017-07-25 15:03:13 +01001566 }
1567
1568 return ret;
1569}
1570EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
1571
Russell Kingce0aa272017-07-25 15:03:18 +01001572static int phylink_sfp_module_insert(void *upstream,
1573 const struct sfp_eeprom_id *id)
1574{
1575 struct phylink *pl = upstream;
1576 __ETHTOOL_DECLARE_LINK_MODE_MASK(support) = { 0, };
1577 struct phylink_link_state config;
1578 phy_interface_t iface;
1579 int mode, ret = 0;
1580 bool changed;
1581 u8 port;
1582
1583 sfp_parse_support(pl->sfp_bus, id, support);
1584 port = sfp_parse_port(pl->sfp_bus, id, support);
1585 iface = sfp_parse_interface(pl->sfp_bus, id);
1586
Russell King8b874512017-12-15 16:09:47 +00001587 ASSERT_RTNL();
Russell Kingce0aa272017-07-25 15:03:18 +01001588
1589 switch (iface) {
1590 case PHY_INTERFACE_MODE_SGMII:
Russell Kingce0aa272017-07-25 15:03:18 +01001591 case PHY_INTERFACE_MODE_1000BASEX:
Russell King4336c402017-12-01 10:24:32 +00001592 case PHY_INTERFACE_MODE_2500BASEX:
1593 case PHY_INTERFACE_MODE_10GKR:
Russell King86a362c2017-12-01 10:24:26 +00001594 mode = MLO_AN_INBAND;
Russell Kingce0aa272017-07-25 15:03:18 +01001595 break;
1596 default:
1597 return -EINVAL;
1598 }
1599
1600 memset(&config, 0, sizeof(config));
1601 linkmode_copy(config.advertising, support);
1602 config.interface = iface;
1603 config.speed = SPEED_UNKNOWN;
1604 config.duplex = DUPLEX_UNKNOWN;
1605 config.pause = MLO_PAUSE_AN;
1606 config.an_enabled = pl->link_config.an_enabled;
1607
1608 /* Ignore errors if we're expecting a PHY to attach later */
1609 ret = phylink_validate(pl, support, &config);
1610 if (ret) {
1611 netdev_err(pl->netdev, "validation of %s/%s with support %*pb failed: %d\n",
1612 phylink_an_mode_str(mode), phy_modes(config.interface),
1613 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
1614 return ret;
1615 }
1616
1617 netdev_dbg(pl->netdev, "requesting link mode %s/%s with support %*pb\n",
1618 phylink_an_mode_str(mode), phy_modes(config.interface),
1619 __ETHTOOL_LINK_MODE_MASK_NBITS, support);
1620
Russell King86a362c2017-12-01 10:24:26 +00001621 if (phy_interface_mode_is_8023z(iface) && pl->phydev)
Russell Kingce0aa272017-07-25 15:03:18 +01001622 return -EINVAL;
1623
1624 changed = !bitmap_equal(pl->supported, support,
1625 __ETHTOOL_LINK_MODE_MASK_NBITS);
1626 if (changed) {
1627 linkmode_copy(pl->supported, support);
1628 linkmode_copy(pl->link_config.advertising, config.advertising);
1629 }
1630
1631 if (pl->link_an_mode != mode ||
1632 pl->link_config.interface != config.interface) {
1633 pl->link_config.interface = config.interface;
1634 pl->link_an_mode = mode;
1635
1636 changed = true;
1637
1638 netdev_info(pl->netdev, "switched to %s/%s link mode\n",
1639 phylink_an_mode_str(mode),
1640 phy_modes(config.interface));
1641 }
1642
1643 pl->link_port = port;
1644
1645 if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
1646 &pl->phylink_disable_state))
1647 phylink_mac_config(pl, &pl->link_config);
1648
1649 return ret;
1650}
1651
1652static void phylink_sfp_link_down(void *upstream)
1653{
1654 struct phylink *pl = upstream;
1655
Russell King8b874512017-12-15 16:09:47 +00001656 ASSERT_RTNL();
Russell Kingce0aa272017-07-25 15:03:18 +01001657
1658 set_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
1659 flush_work(&pl->resolve);
1660
1661 netif_carrier_off(pl->netdev);
1662}
1663
1664static void phylink_sfp_link_up(void *upstream)
1665{
1666 struct phylink *pl = upstream;
1667
Russell King8b874512017-12-15 16:09:47 +00001668 ASSERT_RTNL();
Russell Kingce0aa272017-07-25 15:03:18 +01001669
1670 clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
1671 phylink_run_resolve(pl);
1672}
1673
1674static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
1675{
1676 return phylink_connect_phy(upstream, phy);
1677}
1678
1679static void phylink_sfp_disconnect_phy(void *upstream)
1680{
1681 phylink_disconnect_phy(upstream);
1682}
1683
1684static const struct sfp_upstream_ops sfp_phylink_ops = {
1685 .module_insert = phylink_sfp_module_insert,
1686 .link_up = phylink_sfp_link_up,
1687 .link_down = phylink_sfp_link_down,
1688 .connect_phy = phylink_sfp_connect_phy,
1689 .disconnect_phy = phylink_sfp_disconnect_phy,
1690};
1691
Russell King9525ae82017-07-25 15:03:13 +01001692MODULE_LICENSE("GPL");