blob: 27327c917a5907da6ab7be16d2bab7028dc01892 [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;
Russell King74ee0e82017-12-20 23:21:34 +0000570 pl->link_config.an_enabled = true;
Russell King9525ae82017-07-25 15:03:13 +0100571 pl->ops = ops;
572 __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
573
574 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
575 linkmode_copy(pl->link_config.advertising, pl->supported);
576 phylink_validate(pl, pl->supported, &pl->link_config);
577
Russell King8fa7b9b2017-12-01 10:25:09 +0000578 ret = phylink_parse_mode(pl, fwnode);
Russell King9525ae82017-07-25 15:03:13 +0100579 if (ret < 0) {
580 kfree(pl);
581 return ERR_PTR(ret);
582 }
583
584 if (pl->link_an_mode == MLO_AN_FIXED) {
Russell King8fa7b9b2017-12-01 10:25:09 +0000585 ret = phylink_parse_fixedlink(pl, fwnode);
Russell King9525ae82017-07-25 15:03:13 +0100586 if (ret < 0) {
587 kfree(pl);
588 return ERR_PTR(ret);
589 }
590 }
591
Russell King8fa7b9b2017-12-01 10:25:09 +0000592 ret = phylink_register_sfp(pl, fwnode);
Russell Kingce0aa272017-07-25 15:03:18 +0100593 if (ret < 0) {
594 kfree(pl);
595 return ERR_PTR(ret);
596 }
597
Russell King9525ae82017-07-25 15:03:13 +0100598 return pl;
599}
600EXPORT_SYMBOL_GPL(phylink_create);
601
Russell King8796c892017-12-01 10:24:47 +0000602/**
603 * phylink_destroy() - cleanup and destroy the phylink instance
604 * @pl: a pointer to a &struct phylink returned from phylink_create()
605 *
606 * Destroy a phylink instance. Any PHY that has been attached must have been
607 * cleaned up via phylink_disconnect_phy() prior to calling this function.
608 */
Russell King9525ae82017-07-25 15:03:13 +0100609void phylink_destroy(struct phylink *pl)
610{
Russell Kingce0aa272017-07-25 15:03:18 +0100611 if (pl->sfp_bus)
612 sfp_unregister_upstream(pl->sfp_bus);
613
Russell King9525ae82017-07-25 15:03:13 +0100614 cancel_work_sync(&pl->resolve);
615 kfree(pl);
616}
617EXPORT_SYMBOL_GPL(phylink_destroy);
618
Wei Yongjun1ec6e532017-11-02 11:14:48 +0000619static void phylink_phy_change(struct phy_device *phydev, bool up,
620 bool do_carrier)
Russell King9525ae82017-07-25 15:03:13 +0100621{
622 struct phylink *pl = phydev->phylink;
623
624 mutex_lock(&pl->state_mutex);
625 pl->phy_state.speed = phydev->speed;
626 pl->phy_state.duplex = phydev->duplex;
627 pl->phy_state.pause = MLO_PAUSE_NONE;
628 if (phydev->pause)
629 pl->phy_state.pause |= MLO_PAUSE_SYM;
630 if (phydev->asym_pause)
631 pl->phy_state.pause |= MLO_PAUSE_ASYM;
632 pl->phy_state.interface = phydev->interface;
633 pl->phy_state.link = up;
634 mutex_unlock(&pl->state_mutex);
635
636 phylink_run_resolve(pl);
637
638 netdev_dbg(pl->netdev, "phy link %s %s/%s/%s\n", up ? "up" : "down",
Florian Fainelli516b29e2017-10-30 21:42:57 -0700639 phy_modes(phydev->interface),
Russell King9525ae82017-07-25 15:03:13 +0100640 phy_speed_to_str(phydev->speed),
641 phy_duplex_to_str(phydev->duplex));
642}
643
644static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy)
645{
646 struct phylink_link_state config;
647 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
648 u32 advertising;
649 int ret;
650
651 memset(&config, 0, sizeof(config));
652 ethtool_convert_legacy_u32_to_link_mode(supported, phy->supported);
653 ethtool_convert_legacy_u32_to_link_mode(config.advertising,
654 phy->advertising);
655 config.interface = pl->link_config.interface;
656
657 /*
658 * This is the new way of dealing with flow control for PHYs,
659 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
660 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
661 * using our validate call to the MAC, we rely upon the MAC
662 * clearing the bits from both supported and advertising fields.
663 */
664 if (phylink_test(supported, Pause))
665 phylink_set(config.advertising, Pause);
666 if (phylink_test(supported, Asym_Pause))
667 phylink_set(config.advertising, Asym_Pause);
668
669 ret = phylink_validate(pl, supported, &config);
670 if (ret)
671 return ret;
672
673 phy->phylink = pl;
674 phy->phy_link_change = phylink_phy_change;
675
676 netdev_info(pl->netdev,
677 "PHY [%s] driver [%s]\n", dev_name(&phy->mdio.dev),
678 phy->drv->name);
679
680 mutex_lock(&phy->lock);
681 mutex_lock(&pl->state_mutex);
682 pl->netdev->phydev = phy;
683 pl->phydev = phy;
684 linkmode_copy(pl->supported, supported);
685 linkmode_copy(pl->link_config.advertising, config.advertising);
686
687 /* Restrict the phy advertisment according to the MAC support. */
688 ethtool_convert_link_mode_to_legacy_u32(&advertising, config.advertising);
689 phy->advertising = advertising;
690 mutex_unlock(&pl->state_mutex);
691 mutex_unlock(&phy->lock);
692
693 netdev_dbg(pl->netdev,
694 "phy: setting supported %*pb advertising 0x%08x\n",
695 __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
696 phy->advertising);
697
698 phy_start_machine(phy);
699 if (phy->irq > 0)
700 phy_start_interrupts(phy);
701
702 return 0;
703}
704
Russell King8796c892017-12-01 10:24:47 +0000705/**
706 * phylink_connect_phy() - connect a PHY to the phylink instance
707 * @pl: a pointer to a &struct phylink returned from phylink_create()
708 * @phy: a pointer to a &struct phy_device.
709 *
710 * Connect @phy to the phylink instance specified by @pl by calling
711 * phy_attach_direct(). Configure the @phy according to the MAC driver's
712 * capabilities, start the PHYLIB state machine and enable any interrupts
713 * that the PHY supports.
714 *
715 * This updates the phylink's ethtool supported and advertising link mode
716 * masks.
717 *
718 * Returns 0 on success or a negative errno.
719 */
Russell King9525ae82017-07-25 15:03:13 +0100720int phylink_connect_phy(struct phylink *pl, struct phy_device *phy)
721{
722 int ret;
723
Russell Kingcf4f2672017-12-01 10:24:21 +0000724 if (WARN_ON(pl->link_an_mode == MLO_AN_FIXED ||
Russell King86a362c2017-12-01 10:24:26 +0000725 (pl->link_an_mode == MLO_AN_INBAND &&
726 phy_interface_mode_is_8023z(pl->link_interface))))
Russell Kingcf4f2672017-12-01 10:24:21 +0000727 return -EINVAL;
728
Russell Kingfe1c3ef2017-12-20 23:23:33 +0000729 if (pl->phydev)
730 return -EBUSY;
731
Florian Fainelli4904b6e2017-12-12 16:00:26 -0800732 /* Use PHY device/driver interface */
733 if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
734 pl->link_interface = phy->interface;
735 pl->link_config.interface = pl->link_interface;
736 }
737
Russell King9525ae82017-07-25 15:03:13 +0100738 ret = phy_attach_direct(pl->netdev, phy, 0, pl->link_interface);
739 if (ret)
740 return ret;
741
742 ret = phylink_bringup_phy(pl, phy);
743 if (ret)
744 phy_detach(phy);
745
746 return ret;
747}
748EXPORT_SYMBOL_GPL(phylink_connect_phy);
749
Russell King8796c892017-12-01 10:24:47 +0000750/**
751 * phylink_of_phy_connect() - connect the PHY specified in the DT mode.
752 * @pl: a pointer to a &struct phylink returned from phylink_create()
753 * @dn: a pointer to a &struct device_node.
Florian Fainelli0a629642017-12-12 16:00:25 -0800754 * @flags: PHY-specific flags to communicate to the PHY device driver
Russell King8796c892017-12-01 10:24:47 +0000755 *
756 * Connect the phy specified in the device node @dn to the phylink instance
757 * specified by @pl. Actions specified in phylink_connect_phy() will be
758 * performed.
759 *
760 * Returns 0 on success or a negative errno.
761 */
Florian Fainelli0a629642017-12-12 16:00:25 -0800762int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn,
763 u32 flags)
Russell King9525ae82017-07-25 15:03:13 +0100764{
765 struct device_node *phy_node;
766 struct phy_device *phy_dev;
767 int ret;
768
Russell Kingcf4f2672017-12-01 10:24:21 +0000769 /* Fixed links and 802.3z are handled without needing a PHY */
770 if (pl->link_an_mode == MLO_AN_FIXED ||
Russell King86a362c2017-12-01 10:24:26 +0000771 (pl->link_an_mode == MLO_AN_INBAND &&
772 phy_interface_mode_is_8023z(pl->link_interface)))
Russell King9525ae82017-07-25 15:03:13 +0100773 return 0;
774
775 phy_node = of_parse_phandle(dn, "phy-handle", 0);
776 if (!phy_node)
777 phy_node = of_parse_phandle(dn, "phy", 0);
778 if (!phy_node)
779 phy_node = of_parse_phandle(dn, "phy-device", 0);
780
781 if (!phy_node) {
Florian Fainellid38b4afd2017-12-12 16:00:27 -0800782 if (pl->link_an_mode == MLO_AN_PHY)
Russell King9525ae82017-07-25 15:03:13 +0100783 return -ENODEV;
Russell King9525ae82017-07-25 15:03:13 +0100784 return 0;
785 }
786
Florian Fainelli0a629642017-12-12 16:00:25 -0800787 phy_dev = of_phy_attach(pl->netdev, phy_node, flags,
788 pl->link_interface);
Russell King9525ae82017-07-25 15:03:13 +0100789 /* We're done with the phy_node handle */
790 of_node_put(phy_node);
791
792 if (!phy_dev)
793 return -ENODEV;
794
795 ret = phylink_bringup_phy(pl, phy_dev);
796 if (ret)
797 phy_detach(phy_dev);
798
799 return ret;
800}
801EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
802
Russell King8796c892017-12-01 10:24:47 +0000803/**
804 * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
805 * instance.
806 * @pl: a pointer to a &struct phylink returned from phylink_create()
807 *
808 * Disconnect any current PHY from the phylink instance described by @pl.
809 */
Russell King9525ae82017-07-25 15:03:13 +0100810void phylink_disconnect_phy(struct phylink *pl)
811{
812 struct phy_device *phy;
813
Russell King8b874512017-12-15 16:09:47 +0000814 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +0100815
816 phy = pl->phydev;
817 if (phy) {
818 mutex_lock(&phy->lock);
819 mutex_lock(&pl->state_mutex);
820 pl->netdev->phydev = NULL;
821 pl->phydev = NULL;
822 mutex_unlock(&pl->state_mutex);
823 mutex_unlock(&phy->lock);
824 flush_work(&pl->resolve);
825
826 phy_disconnect(phy);
827 }
828}
829EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
830
Russell King8796c892017-12-01 10:24:47 +0000831/**
Florian Fainelli1ac63e32017-12-12 16:00:28 -0800832 * phylink_fixed_state_cb() - allow setting a fixed link callback
833 * @pl: a pointer to a &struct phylink returned from phylink_create()
834 * @cb: callback to execute to determine the fixed link state.
835 *
836 * The MAC driver should call this driver when the state of its link
837 * can be determined through e.g: an out of band MMIO register.
838 */
839int phylink_fixed_state_cb(struct phylink *pl,
840 void (*cb)(struct net_device *dev,
841 struct phylink_link_state *state))
842{
843 /* It does not make sense to let the link be overriden unless we use
844 * MLO_AN_FIXED
845 */
846 if (pl->link_an_mode != MLO_AN_FIXED)
847 return -EINVAL;
848
849 mutex_lock(&pl->state_mutex);
850 pl->get_fixed_state = cb;
851 mutex_unlock(&pl->state_mutex);
852
853 return 0;
854}
855EXPORT_SYMBOL_GPL(phylink_fixed_state_cb);
856
857/**
Russell King8796c892017-12-01 10:24:47 +0000858 * phylink_mac_change() - notify phylink of a change in MAC state
859 * @pl: a pointer to a &struct phylink returned from phylink_create()
860 * @up: indicates whether the link is currently up.
861 *
862 * The MAC driver should call this driver when the state of its link
863 * changes (eg, link failure, new negotiation results, etc.)
864 */
Russell King9525ae82017-07-25 15:03:13 +0100865void phylink_mac_change(struct phylink *pl, bool up)
866{
867 if (!up)
868 pl->mac_link_dropped = true;
869 phylink_run_resolve(pl);
870 netdev_dbg(pl->netdev, "mac link %s\n", up ? "up" : "down");
871}
872EXPORT_SYMBOL_GPL(phylink_mac_change);
873
Russell King8796c892017-12-01 10:24:47 +0000874/**
875 * phylink_start() - start a phylink instance
876 * @pl: a pointer to a &struct phylink returned from phylink_create()
877 *
878 * Start the phylink instance specified by @pl, configuring the MAC for the
879 * desired link mode(s) and negotiation style. This should be called from the
880 * network device driver's &struct net_device_ops ndo_open() method.
881 */
Russell King9525ae82017-07-25 15:03:13 +0100882void phylink_start(struct phylink *pl)
883{
Russell King8b874512017-12-15 16:09:47 +0000884 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +0100885
886 netdev_info(pl->netdev, "configuring for %s/%s link mode\n",
887 phylink_an_mode_str(pl->link_an_mode),
888 phy_modes(pl->link_config.interface));
889
890 /* Apply the link configuration to the MAC when starting. This allows
891 * a fixed-link to start with the correct parameters, and also
892 * ensures that we set the appropriate advertisment for Serdes links.
893 */
894 phylink_resolve_flow(pl, &pl->link_config);
895 phylink_mac_config(pl, &pl->link_config);
896
Russell King85b43942017-12-01 10:24:42 +0000897 /* Restart autonegotiation if using 802.3z to ensure that the link
898 * parameters are properly negotiated. This is necessary for DSA
899 * switches using 802.3z negotiation to ensure they see our modes.
900 */
901 phylink_mac_an_restart(pl);
902
Russell King9525ae82017-07-25 15:03:13 +0100903 clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
904 phylink_run_resolve(pl);
905
Russell Kingce0aa272017-07-25 15:03:18 +0100906 if (pl->sfp_bus)
907 sfp_upstream_start(pl->sfp_bus);
Russell King9525ae82017-07-25 15:03:13 +0100908 if (pl->phydev)
909 phy_start(pl->phydev);
910}
911EXPORT_SYMBOL_GPL(phylink_start);
912
Russell King8796c892017-12-01 10:24:47 +0000913/**
914 * phylink_stop() - stop a phylink instance
915 * @pl: a pointer to a &struct phylink returned from phylink_create()
916 *
917 * Stop the phylink instance specified by @pl. This should be called from the
918 * network device driver's &struct net_device_ops ndo_stop() method. The
919 * network device's carrier state should not be changed prior to calling this
920 * function.
921 */
Russell King9525ae82017-07-25 15:03:13 +0100922void phylink_stop(struct phylink *pl)
923{
Russell King8b874512017-12-15 16:09:47 +0000924 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +0100925
926 if (pl->phydev)
927 phy_stop(pl->phydev);
Russell Kingce0aa272017-07-25 15:03:18 +0100928 if (pl->sfp_bus)
929 sfp_upstream_stop(pl->sfp_bus);
Russell King9525ae82017-07-25 15:03:13 +0100930
931 set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
Russell King2012b7d2017-11-30 13:59:26 +0000932 queue_work(system_power_efficient_wq, &pl->resolve);
Russell King9525ae82017-07-25 15:03:13 +0100933 flush_work(&pl->resolve);
934}
935EXPORT_SYMBOL_GPL(phylink_stop);
936
Russell King8796c892017-12-01 10:24:47 +0000937/**
938 * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY
939 * @pl: a pointer to a &struct phylink returned from phylink_create()
940 * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters
941 *
942 * Read the wake on lan parameters from the PHY attached to the phylink
943 * instance specified by @pl. If no PHY is currently attached, report no
944 * support for wake on lan.
945 */
Russell King9525ae82017-07-25 15:03:13 +0100946void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
947{
Russell King8b874512017-12-15 16:09:47 +0000948 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +0100949
950 wol->supported = 0;
951 wol->wolopts = 0;
952
953 if (pl->phydev)
954 phy_ethtool_get_wol(pl->phydev, wol);
955}
956EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol);
957
Russell King8796c892017-12-01 10:24:47 +0000958/**
959 * phylink_ethtool_set_wol() - set wake on lan parameters
960 * @pl: a pointer to a &struct phylink returned from phylink_create()
961 * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters
962 *
963 * Set the wake on lan parameters for the PHY attached to the phylink
964 * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP
965 * error.
966 *
967 * Returns zero on success or negative errno code.
968 */
Russell King9525ae82017-07-25 15:03:13 +0100969int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
970{
971 int ret = -EOPNOTSUPP;
972
Russell King8b874512017-12-15 16:09:47 +0000973 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +0100974
975 if (pl->phydev)
976 ret = phy_ethtool_set_wol(pl->phydev, wol);
977
978 return ret;
979}
980EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol);
981
982static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b)
983{
984 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
985
986 linkmode_zero(mask);
987 phylink_set_port_modes(mask);
988
989 linkmode_and(dst, dst, mask);
990 linkmode_or(dst, dst, b);
991}
992
993static void phylink_get_ksettings(const struct phylink_link_state *state,
994 struct ethtool_link_ksettings *kset)
995{
996 phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
997 linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
998 kset->base.speed = state->speed;
999 kset->base.duplex = state->duplex;
1000 kset->base.autoneg = state->an_enabled ? AUTONEG_ENABLE :
1001 AUTONEG_DISABLE;
1002}
1003
Russell King8796c892017-12-01 10:24:47 +00001004/**
1005 * phylink_ethtool_ksettings_get() - get the current link settings
1006 * @pl: a pointer to a &struct phylink returned from phylink_create()
1007 * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings
1008 *
1009 * Read the current link settings for the phylink instance specified by @pl.
1010 * This will be the link settings read from the MAC, PHY or fixed link
1011 * settings depending on the current negotiation mode.
1012 */
Russell King9525ae82017-07-25 15:03:13 +01001013int phylink_ethtool_ksettings_get(struct phylink *pl,
Florian Fainelli516b29e2017-10-30 21:42:57 -07001014 struct ethtool_link_ksettings *kset)
Russell King9525ae82017-07-25 15:03:13 +01001015{
1016 struct phylink_link_state link_state;
1017
Russell King8b874512017-12-15 16:09:47 +00001018 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001019
1020 if (pl->phydev) {
1021 phy_ethtool_ksettings_get(pl->phydev, kset);
1022 } else {
1023 kset->base.port = pl->link_port;
1024 }
1025
1026 linkmode_copy(kset->link_modes.supported, pl->supported);
1027
1028 switch (pl->link_an_mode) {
1029 case MLO_AN_FIXED:
1030 /* We are using fixed settings. Report these as the
1031 * current link settings - and note that these also
1032 * represent the supported speeds/duplex/pause modes.
1033 */
1034 phylink_get_fixed_state(pl, &link_state);
1035 phylink_get_ksettings(&link_state, kset);
1036 break;
1037
Russell King86a362c2017-12-01 10:24:26 +00001038 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001039 /* If there is a phy attached, then use the reported
1040 * settings from the phy with no modification.
1041 */
1042 if (pl->phydev)
1043 break;
1044
Russell King9525ae82017-07-25 15:03:13 +01001045 phylink_get_mac_state(pl, &link_state);
1046
1047 /* The MAC is reporting the link results from its own PCS
1048 * layer via in-band status. Report these as the current
1049 * link settings.
1050 */
1051 phylink_get_ksettings(&link_state, kset);
1052 break;
1053 }
1054
1055 return 0;
1056}
1057EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get);
1058
Russell King8796c892017-12-01 10:24:47 +00001059/**
1060 * phylink_ethtool_ksettings_set() - set the link settings
1061 * @pl: a pointer to a &struct phylink returned from phylink_create()
1062 * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes
1063 */
Russell King9525ae82017-07-25 15:03:13 +01001064int phylink_ethtool_ksettings_set(struct phylink *pl,
Florian Fainelli516b29e2017-10-30 21:42:57 -07001065 const struct ethtool_link_ksettings *kset)
Russell King9525ae82017-07-25 15:03:13 +01001066{
1067 struct ethtool_link_ksettings our_kset;
1068 struct phylink_link_state config;
1069 int ret;
1070
Russell King8b874512017-12-15 16:09:47 +00001071 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001072
1073 if (kset->base.autoneg != AUTONEG_DISABLE &&
1074 kset->base.autoneg != AUTONEG_ENABLE)
1075 return -EINVAL;
1076
1077 config = pl->link_config;
1078
1079 /* Mask out unsupported advertisments */
1080 linkmode_and(config.advertising, kset->link_modes.advertising,
1081 pl->supported);
1082
1083 /* FIXME: should we reject autoneg if phy/mac does not support it? */
1084 if (kset->base.autoneg == AUTONEG_DISABLE) {
1085 const struct phy_setting *s;
1086
1087 /* Autonegotiation disabled, select a suitable speed and
1088 * duplex.
1089 */
1090 s = phy_lookup_setting(kset->base.speed, kset->base.duplex,
1091 pl->supported,
1092 __ETHTOOL_LINK_MODE_MASK_NBITS, false);
1093 if (!s)
1094 return -EINVAL;
1095
1096 /* If we have a fixed link (as specified by firmware), refuse
1097 * to change link parameters.
1098 */
1099 if (pl->link_an_mode == MLO_AN_FIXED &&
1100 (s->speed != pl->link_config.speed ||
1101 s->duplex != pl->link_config.duplex))
1102 return -EINVAL;
1103
1104 config.speed = s->speed;
1105 config.duplex = s->duplex;
1106 config.an_enabled = false;
1107
1108 __clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
1109 } else {
1110 /* If we have a fixed link, refuse to enable autonegotiation */
1111 if (pl->link_an_mode == MLO_AN_FIXED)
1112 return -EINVAL;
1113
1114 config.speed = SPEED_UNKNOWN;
1115 config.duplex = DUPLEX_UNKNOWN;
1116 config.an_enabled = true;
1117
1118 __set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
1119 }
1120
1121 if (phylink_validate(pl, pl->supported, &config))
1122 return -EINVAL;
1123
1124 /* If autonegotiation is enabled, we must have an advertisment */
1125 if (config.an_enabled && phylink_is_empty_linkmode(config.advertising))
1126 return -EINVAL;
1127
1128 our_kset = *kset;
1129 linkmode_copy(our_kset.link_modes.advertising, config.advertising);
1130 our_kset.base.speed = config.speed;
1131 our_kset.base.duplex = config.duplex;
1132
1133 /* If we have a PHY, configure the phy */
1134 if (pl->phydev) {
1135 ret = phy_ethtool_ksettings_set(pl->phydev, &our_kset);
1136 if (ret)
1137 return ret;
1138 }
1139
1140 mutex_lock(&pl->state_mutex);
1141 /* Configure the MAC to match the new settings */
1142 linkmode_copy(pl->link_config.advertising, our_kset.link_modes.advertising);
Russell King182088a2017-12-20 23:21:28 +00001143 pl->link_config.interface = config.interface;
Russell King9525ae82017-07-25 15:03:13 +01001144 pl->link_config.speed = our_kset.base.speed;
1145 pl->link_config.duplex = our_kset.base.duplex;
1146 pl->link_config.an_enabled = our_kset.base.autoneg != AUTONEG_DISABLE;
1147
1148 if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
1149 phylink_mac_config(pl, &pl->link_config);
1150 phylink_mac_an_restart(pl);
1151 }
1152 mutex_unlock(&pl->state_mutex);
1153
Dan Carpenterd18c2a12017-08-10 00:35:50 +03001154 return 0;
Russell King9525ae82017-07-25 15:03:13 +01001155}
1156EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
1157
Russell King8796c892017-12-01 10:24:47 +00001158/**
1159 * phylink_ethtool_nway_reset() - restart negotiation
1160 * @pl: a pointer to a &struct phylink returned from phylink_create()
1161 *
1162 * Restart negotiation for the phylink instance specified by @pl. This will
1163 * cause any attached phy to restart negotiation with the link partner, and
1164 * if the MAC is in a BaseX mode, the MAC will also be requested to restart
1165 * negotiation.
1166 *
1167 * Returns zero on success, or negative error code.
1168 */
Russell King9525ae82017-07-25 15:03:13 +01001169int phylink_ethtool_nway_reset(struct phylink *pl)
1170{
1171 int ret = 0;
1172
Russell King8b874512017-12-15 16:09:47 +00001173 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001174
1175 if (pl->phydev)
1176 ret = phy_restart_aneg(pl->phydev);
1177 phylink_mac_an_restart(pl);
1178
1179 return ret;
1180}
1181EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
1182
Russell King8796c892017-12-01 10:24:47 +00001183/**
1184 * phylink_ethtool_get_pauseparam() - get the current pause parameters
1185 * @pl: a pointer to a &struct phylink returned from phylink_create()
1186 * @pause: a pointer to a &struct ethtool_pauseparam
1187 */
Russell King9525ae82017-07-25 15:03:13 +01001188void phylink_ethtool_get_pauseparam(struct phylink *pl,
1189 struct ethtool_pauseparam *pause)
1190{
Russell King8b874512017-12-15 16:09:47 +00001191 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001192
1193 pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
1194 pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
1195 pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
1196}
1197EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
1198
Russell King8796c892017-12-01 10:24:47 +00001199/**
1200 * phylink_ethtool_set_pauseparam() - set the current pause parameters
1201 * @pl: a pointer to a &struct phylink returned from phylink_create()
1202 * @pause: a pointer to a &struct ethtool_pauseparam
1203 */
Russell King9525ae82017-07-25 15:03:13 +01001204int phylink_ethtool_set_pauseparam(struct phylink *pl,
1205 struct ethtool_pauseparam *pause)
1206{
1207 struct phylink_link_state *config = &pl->link_config;
1208
Russell King8b874512017-12-15 16:09:47 +00001209 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001210
1211 if (!phylink_test(pl->supported, Pause) &&
1212 !phylink_test(pl->supported, Asym_Pause))
1213 return -EOPNOTSUPP;
1214
1215 if (!phylink_test(pl->supported, Asym_Pause) &&
1216 !pause->autoneg && pause->rx_pause != pause->tx_pause)
1217 return -EINVAL;
1218
1219 config->pause &= ~(MLO_PAUSE_AN | MLO_PAUSE_TXRX_MASK);
1220
1221 if (pause->autoneg)
1222 config->pause |= MLO_PAUSE_AN;
1223 if (pause->rx_pause)
1224 config->pause |= MLO_PAUSE_RX;
1225 if (pause->tx_pause)
1226 config->pause |= MLO_PAUSE_TX;
1227
1228 if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
1229 switch (pl->link_an_mode) {
1230 case MLO_AN_PHY:
1231 /* Silently mark the carrier down, and then trigger a resolve */
1232 netif_carrier_off(pl->netdev);
1233 phylink_run_resolve(pl);
1234 break;
1235
1236 case MLO_AN_FIXED:
1237 /* Should we allow fixed links to change against the config? */
1238 phylink_resolve_flow(pl, config);
1239 phylink_mac_config(pl, config);
1240 break;
1241
Russell King86a362c2017-12-01 10:24:26 +00001242 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001243 phylink_mac_config(pl, config);
1244 phylink_mac_an_restart(pl);
1245 break;
1246 }
1247 }
1248
1249 return 0;
1250}
1251EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
1252
Russell King770a1ad2017-07-25 15:03:23 +01001253int phylink_ethtool_get_module_info(struct phylink *pl,
1254 struct ethtool_modinfo *modinfo)
1255{
1256 int ret = -EOPNOTSUPP;
1257
1258 WARN_ON(!lockdep_rtnl_is_held());
1259
1260 if (pl->sfp_bus)
1261 ret = sfp_get_module_info(pl->sfp_bus, modinfo);
1262
1263 return ret;
1264}
1265EXPORT_SYMBOL_GPL(phylink_ethtool_get_module_info);
1266
1267int phylink_ethtool_get_module_eeprom(struct phylink *pl,
1268 struct ethtool_eeprom *ee, u8 *buf)
1269{
1270 int ret = -EOPNOTSUPP;
1271
1272 WARN_ON(!lockdep_rtnl_is_held());
1273
1274 if (pl->sfp_bus)
1275 ret = sfp_get_module_eeprom(pl->sfp_bus, ee, buf);
1276
1277 return ret;
1278}
1279EXPORT_SYMBOL_GPL(phylink_ethtool_get_module_eeprom);
1280
Russell King8796c892017-12-01 10:24:47 +00001281/**
1282 * phylink_ethtool_get_eee_err() - read the energy efficient ethernet error
1283 * counter
1284 * @pl: a pointer to a &struct phylink returned from phylink_create().
1285 *
1286 * Read the Energy Efficient Ethernet error counter from the PHY associated
1287 * with the phylink instance specified by @pl.
1288 *
1289 * Returns positive error counter value, or negative error code.
1290 */
Russell King9525ae82017-07-25 15:03:13 +01001291int phylink_get_eee_err(struct phylink *pl)
1292{
1293 int ret = 0;
1294
Russell King8b874512017-12-15 16:09:47 +00001295 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001296
1297 if (pl->phydev)
1298 ret = phy_get_eee_err(pl->phydev);
1299
1300 return ret;
1301}
1302EXPORT_SYMBOL_GPL(phylink_get_eee_err);
1303
Russell King8796c892017-12-01 10:24:47 +00001304/**
1305 * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters
1306 * @pl: a pointer to a &struct phylink returned from phylink_create()
1307 * @eee: a pointer to a &struct ethtool_eee for the read parameters
1308 */
Russell King9525ae82017-07-25 15:03:13 +01001309int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee)
1310{
1311 int ret = -EOPNOTSUPP;
1312
Russell King8b874512017-12-15 16:09:47 +00001313 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001314
1315 if (pl->phydev)
1316 ret = phy_ethtool_get_eee(pl->phydev, eee);
1317
1318 return ret;
1319}
1320EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
1321
Russell King8796c892017-12-01 10:24:47 +00001322/**
1323 * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters
1324 * @pl: a pointer to a &struct phylink returned from phylink_create()
1325 * @eee: a pointer to a &struct ethtool_eee for the desired parameters
1326 */
Russell King9525ae82017-07-25 15:03:13 +01001327int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_eee *eee)
1328{
1329 int ret = -EOPNOTSUPP;
1330
Russell King8b874512017-12-15 16:09:47 +00001331 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001332
1333 if (pl->phydev)
1334 ret = phy_ethtool_set_eee(pl->phydev, eee);
1335
1336 return ret;
1337}
1338EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
1339
1340/* This emulates MII registers for a fixed-mode phy operating as per the
1341 * passed in state. "aneg" defines if we report negotiation is possible.
1342 *
1343 * FIXME: should deal with negotiation state too.
1344 */
1345static int phylink_mii_emul_read(struct net_device *ndev, unsigned int reg,
1346 struct phylink_link_state *state, bool aneg)
1347{
1348 struct fixed_phy_status fs;
1349 int val;
1350
1351 fs.link = state->link;
1352 fs.speed = state->speed;
1353 fs.duplex = state->duplex;
1354 fs.pause = state->pause & MLO_PAUSE_SYM;
1355 fs.asym_pause = state->pause & MLO_PAUSE_ASYM;
1356
1357 val = swphy_read_reg(reg, &fs);
1358 if (reg == MII_BMSR) {
1359 if (!state->an_complete)
1360 val &= ~BMSR_ANEGCOMPLETE;
1361 if (!aneg)
1362 val &= ~BMSR_ANEGCAPABLE;
1363 }
1364 return val;
1365}
1366
Russell Kingecbd87b2017-07-25 15:03:28 +01001367static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
1368 unsigned int reg)
1369{
1370 struct phy_device *phydev = pl->phydev;
1371 int prtad, devad;
1372
1373 if (mdio_phy_id_is_c45(phy_id)) {
1374 prtad = mdio_phy_id_prtad(phy_id);
1375 devad = mdio_phy_id_devad(phy_id);
1376 devad = MII_ADDR_C45 | devad << 16 | reg;
1377 } else if (phydev->is_c45) {
1378 switch (reg) {
1379 case MII_BMCR:
1380 case MII_BMSR:
1381 case MII_PHYSID1:
1382 case MII_PHYSID2:
1383 devad = __ffs(phydev->c45_ids.devices_in_package);
1384 break;
1385 case MII_ADVERTISE:
1386 case MII_LPA:
1387 if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1388 return -EINVAL;
1389 devad = MDIO_MMD_AN;
1390 if (reg == MII_ADVERTISE)
1391 reg = MDIO_AN_ADVERTISE;
1392 else
1393 reg = MDIO_AN_LPA;
1394 break;
1395 default:
1396 return -EINVAL;
1397 }
1398 prtad = phy_id;
1399 devad = MII_ADDR_C45 | devad << 16 | reg;
1400 } else {
1401 prtad = phy_id;
1402 devad = reg;
1403 }
1404 return mdiobus_read(pl->phydev->mdio.bus, prtad, devad);
1405}
1406
1407static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
1408 unsigned int reg, unsigned int val)
1409{
1410 struct phy_device *phydev = pl->phydev;
1411 int prtad, devad;
1412
1413 if (mdio_phy_id_is_c45(phy_id)) {
1414 prtad = mdio_phy_id_prtad(phy_id);
1415 devad = mdio_phy_id_devad(phy_id);
1416 devad = MII_ADDR_C45 | devad << 16 | reg;
1417 } else if (phydev->is_c45) {
1418 switch (reg) {
1419 case MII_BMCR:
1420 case MII_BMSR:
1421 case MII_PHYSID1:
1422 case MII_PHYSID2:
1423 devad = __ffs(phydev->c45_ids.devices_in_package);
1424 break;
1425 case MII_ADVERTISE:
1426 case MII_LPA:
1427 if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1428 return -EINVAL;
1429 devad = MDIO_MMD_AN;
1430 if (reg == MII_ADVERTISE)
1431 reg = MDIO_AN_ADVERTISE;
1432 else
1433 reg = MDIO_AN_LPA;
1434 break;
1435 default:
1436 return -EINVAL;
1437 }
1438 prtad = phy_id;
1439 devad = MII_ADDR_C45 | devad << 16 | reg;
1440 } else {
1441 prtad = phy_id;
1442 devad = reg;
1443 }
1444
1445 return mdiobus_write(phydev->mdio.bus, prtad, devad, val);
1446}
1447
Russell King9525ae82017-07-25 15:03:13 +01001448static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
1449 unsigned int reg)
1450{
1451 struct phylink_link_state state;
1452 int val = 0xffff;
1453
Russell King9525ae82017-07-25 15:03:13 +01001454 switch (pl->link_an_mode) {
1455 case MLO_AN_FIXED:
1456 if (phy_id == 0) {
1457 phylink_get_fixed_state(pl, &state);
1458 val = phylink_mii_emul_read(pl->netdev, reg, &state,
1459 true);
1460 }
1461 break;
1462
1463 case MLO_AN_PHY:
1464 return -EOPNOTSUPP;
1465
Russell King86a362c2017-12-01 10:24:26 +00001466 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001467 if (phy_id == 0) {
1468 val = phylink_get_mac_state(pl, &state);
1469 if (val < 0)
1470 return val;
1471
1472 val = phylink_mii_emul_read(pl->netdev, reg, &state,
1473 true);
1474 }
1475 break;
1476 }
1477
1478 return val & 0xffff;
1479}
1480
1481static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
1482 unsigned int reg, unsigned int val)
1483{
Russell King9525ae82017-07-25 15:03:13 +01001484 switch (pl->link_an_mode) {
1485 case MLO_AN_FIXED:
1486 break;
1487
1488 case MLO_AN_PHY:
1489 return -EOPNOTSUPP;
1490
Russell King86a362c2017-12-01 10:24:26 +00001491 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001492 break;
1493 }
1494
1495 return 0;
1496}
1497
Russell King8796c892017-12-01 10:24:47 +00001498/**
1499 * phylink_mii_ioctl() - generic mii ioctl interface
1500 * @pl: a pointer to a &struct phylink returned from phylink_create()
1501 * @ifr: a pointer to a &struct ifreq for socket ioctls
1502 * @cmd: ioctl cmd to execute
1503 *
1504 * Perform the specified MII ioctl on the PHY attached to the phylink instance
1505 * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
1506 *
1507 * Returns: zero on success or negative error code.
1508 *
1509 * %SIOCGMIIPHY:
1510 * read register from the current PHY.
1511 * %SIOCGMIIREG:
1512 * read register from the specified PHY.
1513 * %SIOCSMIIREG:
1514 * set a register on the specified PHY.
1515 */
Russell King9525ae82017-07-25 15:03:13 +01001516int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
1517{
Russell Kingecbd87b2017-07-25 15:03:28 +01001518 struct mii_ioctl_data *mii = if_mii(ifr);
1519 int ret;
Russell King9525ae82017-07-25 15:03:13 +01001520
Russell King8b874512017-12-15 16:09:47 +00001521 ASSERT_RTNL();
Russell King9525ae82017-07-25 15:03:13 +01001522
Russell Kingecbd87b2017-07-25 15:03:28 +01001523 if (pl->phydev) {
Russell King86a362c2017-12-01 10:24:26 +00001524 /* PHYs only exist for MLO_AN_PHY and SGMII */
Russell Kingecbd87b2017-07-25 15:03:28 +01001525 switch (cmd) {
1526 case SIOCGMIIPHY:
1527 mii->phy_id = pl->phydev->mdio.addr;
Gustavo A. R. Silva46cd7502018-01-05 11:23:45 -06001528 /* fall through */
Russell King9525ae82017-07-25 15:03:13 +01001529
Russell Kingecbd87b2017-07-25 15:03:28 +01001530 case SIOCGMIIREG:
1531 ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
1532 if (ret >= 0) {
1533 mii->val_out = ret;
1534 ret = 0;
1535 }
1536 break;
Russell King9525ae82017-07-25 15:03:13 +01001537
Russell Kingecbd87b2017-07-25 15:03:28 +01001538 case SIOCSMIIREG:
1539 ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
1540 mii->val_in);
1541 break;
Russell King9525ae82017-07-25 15:03:13 +01001542
Russell Kingecbd87b2017-07-25 15:03:28 +01001543 default:
Russell King9525ae82017-07-25 15:03:13 +01001544 ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
Russell Kingecbd87b2017-07-25 15:03:28 +01001545 break;
1546 }
1547 } else {
1548 switch (cmd) {
1549 case SIOCGMIIPHY:
1550 mii->phy_id = 0;
Gustavo A. R. Silva46cd7502018-01-05 11:23:45 -06001551 /* fall through */
Russell Kingecbd87b2017-07-25 15:03:28 +01001552
1553 case SIOCGMIIREG:
1554 ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
1555 if (ret >= 0) {
1556 mii->val_out = ret;
1557 ret = 0;
1558 }
1559 break;
1560
1561 case SIOCSMIIREG:
1562 ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
1563 mii->val_in);
1564 break;
1565
1566 default:
1567 ret = -EOPNOTSUPP;
1568 break;
1569 }
Russell King9525ae82017-07-25 15:03:13 +01001570 }
1571
1572 return ret;
1573}
1574EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
1575
Russell Kingce0aa272017-07-25 15:03:18 +01001576static int phylink_sfp_module_insert(void *upstream,
1577 const struct sfp_eeprom_id *id)
1578{
1579 struct phylink *pl = upstream;
1580 __ETHTOOL_DECLARE_LINK_MODE_MASK(support) = { 0, };
1581 struct phylink_link_state config;
1582 phy_interface_t iface;
Russell King444d3502017-12-29 12:15:33 +00001583 int ret = 0;
Russell Kingce0aa272017-07-25 15:03:18 +01001584 bool changed;
1585 u8 port;
1586
Russell King8b874512017-12-15 16:09:47 +00001587 ASSERT_RTNL();
Russell Kingce0aa272017-07-25 15:03:18 +01001588
Russell Kinga9c79362018-02-27 15:53:02 +00001589 sfp_parse_support(pl->sfp_bus, id, support);
1590 port = sfp_parse_port(pl->sfp_bus, id, support);
Russell Kingce0aa272017-07-25 15:03:18 +01001591
1592 memset(&config, 0, sizeof(config));
1593 linkmode_copy(config.advertising, support);
Russell Kinga9c79362018-02-27 15:53:02 +00001594 config.interface = PHY_INTERFACE_MODE_NA;
Russell Kingce0aa272017-07-25 15:03:18 +01001595 config.speed = SPEED_UNKNOWN;
1596 config.duplex = DUPLEX_UNKNOWN;
1597 config.pause = MLO_PAUSE_AN;
1598 config.an_enabled = pl->link_config.an_enabled;
1599
1600 /* Ignore errors if we're expecting a PHY to attach later */
1601 ret = phylink_validate(pl, support, &config);
1602 if (ret) {
Russell Kinga9c79362018-02-27 15:53:02 +00001603 netdev_err(pl->netdev, "validation with support %*pb failed: %d\n",
1604 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
1605 return ret;
1606 }
1607
1608 iface = sfp_select_interface(pl->sfp_bus, id, config.advertising);
1609 if (iface == PHY_INTERFACE_MODE_NA) {
1610 netdev_err(pl->netdev,
1611 "selection of interface failed, advertisment %*pb\n",
1612 __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising);
1613 return -EINVAL;
1614 }
1615
1616 config.interface = iface;
1617 ret = phylink_validate(pl, support, &config);
1618 if (ret) {
Russell Kingce0aa272017-07-25 15:03:18 +01001619 netdev_err(pl->netdev, "validation of %s/%s with support %*pb failed: %d\n",
Russell King444d3502017-12-29 12:15:33 +00001620 phylink_an_mode_str(MLO_AN_INBAND),
1621 phy_modes(config.interface),
Russell Kingce0aa272017-07-25 15:03:18 +01001622 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
1623 return ret;
1624 }
1625
1626 netdev_dbg(pl->netdev, "requesting link mode %s/%s with support %*pb\n",
Russell King444d3502017-12-29 12:15:33 +00001627 phylink_an_mode_str(MLO_AN_INBAND),
1628 phy_modes(config.interface),
Russell Kingce0aa272017-07-25 15:03:18 +01001629 __ETHTOOL_LINK_MODE_MASK_NBITS, support);
1630
Russell King86a362c2017-12-01 10:24:26 +00001631 if (phy_interface_mode_is_8023z(iface) && pl->phydev)
Russell Kingce0aa272017-07-25 15:03:18 +01001632 return -EINVAL;
1633
1634 changed = !bitmap_equal(pl->supported, support,
1635 __ETHTOOL_LINK_MODE_MASK_NBITS);
1636 if (changed) {
1637 linkmode_copy(pl->supported, support);
1638 linkmode_copy(pl->link_config.advertising, config.advertising);
1639 }
1640
Russell King444d3502017-12-29 12:15:33 +00001641 if (pl->link_an_mode != MLO_AN_INBAND ||
Russell Kingce0aa272017-07-25 15:03:18 +01001642 pl->link_config.interface != config.interface) {
1643 pl->link_config.interface = config.interface;
Russell King444d3502017-12-29 12:15:33 +00001644 pl->link_an_mode = MLO_AN_INBAND;
Russell Kingce0aa272017-07-25 15:03:18 +01001645
1646 changed = true;
1647
1648 netdev_info(pl->netdev, "switched to %s/%s link mode\n",
Russell King444d3502017-12-29 12:15:33 +00001649 phylink_an_mode_str(MLO_AN_INBAND),
Russell Kingce0aa272017-07-25 15:03:18 +01001650 phy_modes(config.interface));
1651 }
1652
1653 pl->link_port = port;
1654
1655 if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
1656 &pl->phylink_disable_state))
1657 phylink_mac_config(pl, &pl->link_config);
1658
1659 return ret;
1660}
1661
1662static void phylink_sfp_link_down(void *upstream)
1663{
1664 struct phylink *pl = upstream;
1665
Russell King8b874512017-12-15 16:09:47 +00001666 ASSERT_RTNL();
Russell Kingce0aa272017-07-25 15:03:18 +01001667
1668 set_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
Russell Kingac817f5a2017-12-26 23:15:12 +00001669 queue_work(system_power_efficient_wq, &pl->resolve);
Russell Kingce0aa272017-07-25 15:03:18 +01001670 flush_work(&pl->resolve);
Russell Kingce0aa272017-07-25 15:03:18 +01001671}
1672
1673static void phylink_sfp_link_up(void *upstream)
1674{
1675 struct phylink *pl = upstream;
1676
Russell King8b874512017-12-15 16:09:47 +00001677 ASSERT_RTNL();
Russell Kingce0aa272017-07-25 15:03:18 +01001678
1679 clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
1680 phylink_run_resolve(pl);
1681}
1682
1683static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
1684{
1685 return phylink_connect_phy(upstream, phy);
1686}
1687
1688static void phylink_sfp_disconnect_phy(void *upstream)
1689{
1690 phylink_disconnect_phy(upstream);
1691}
1692
1693static const struct sfp_upstream_ops sfp_phylink_ops = {
1694 .module_insert = phylink_sfp_module_insert,
1695 .link_up = phylink_sfp_link_up,
1696 .link_down = phylink_sfp_link_down,
1697 .connect_phy = phylink_sfp_connect_phy,
1698 .disconnect_phy = phylink_sfp_disconnect_phy,
1699};
1700
Russell King9525ae82017-07-25 15:03:13 +01001701MODULE_LICENSE("GPL");