]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - drivers/net/phy/phylink.c
phylink: merge SGMII and 802.3z handling
[mirror_ubuntu-eoan-kernel.git] / drivers / net / phy / phylink.c
CommitLineData
9525ae83
RK
1/*
2 * phylink models the MAC to optional PHY connection, supporting
3 * technologies such as SFP cages where the PHY is hot-pluggable.
4 *
5 * Copyright (C) 2015 Russell King
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#include <linux/ethtool.h>
12#include <linux/export.h>
13#include <linux/gpio/consumer.h>
14#include <linux/netdevice.h>
15#include <linux/of.h>
16#include <linux/of_mdio.h>
17#include <linux/phy.h>
18#include <linux/phy_fixed.h>
19#include <linux/phylink.h>
20#include <linux/rtnetlink.h>
21#include <linux/spinlock.h>
22#include <linux/workqueue.h>
23
ce0aa27f 24#include "sfp.h"
9525ae83
RK
25#include "swphy.h"
26
27#define SUPPORTED_INTERFACES \
28 (SUPPORTED_TP | SUPPORTED_MII | SUPPORTED_FIBRE | \
29 SUPPORTED_BNC | SUPPORTED_AUI | SUPPORTED_Backplane)
30#define ADVERTISED_INTERFACES \
31 (ADVERTISED_TP | ADVERTISED_MII | ADVERTISED_FIBRE | \
32 ADVERTISED_BNC | ADVERTISED_AUI | ADVERTISED_Backplane)
33
34enum {
35 PHYLINK_DISABLE_STOPPED,
ce0aa27f 36 PHYLINK_DISABLE_LINK,
9525ae83
RK
37};
38
39struct phylink {
40 struct net_device *netdev;
41 const struct phylink_mac_ops *ops;
42
43 unsigned long phylink_disable_state; /* bitmask of disables */
44 struct phy_device *phydev;
45 phy_interface_t link_interface; /* PHY_INTERFACE_xxx */
46 u8 link_an_mode; /* MLO_AN_xxx */
47 u8 link_port; /* The current non-phy ethtool port */
48 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
49
50 /* The link configuration settings */
51 struct phylink_link_state link_config;
52 struct gpio_desc *link_gpio;
53
54 struct mutex state_mutex;
55 struct phylink_link_state phy_state;
56 struct work_struct resolve;
57
58 bool mac_link_dropped;
ce0aa27f
RK
59
60 struct sfp_bus *sfp_bus;
9525ae83
RK
61};
62
63static inline void linkmode_zero(unsigned long *dst)
64{
65 bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS);
66}
67
68static inline void linkmode_copy(unsigned long *dst, const unsigned long *src)
69{
70 bitmap_copy(dst, src, __ETHTOOL_LINK_MODE_MASK_NBITS);
71}
72
73static inline void linkmode_and(unsigned long *dst, const unsigned long *a,
74 const unsigned long *b)
75{
76 bitmap_and(dst, a, b, __ETHTOOL_LINK_MODE_MASK_NBITS);
77}
78
79static inline void linkmode_or(unsigned long *dst, const unsigned long *a,
80 const unsigned long *b)
81{
82 bitmap_or(dst, a, b, __ETHTOOL_LINK_MODE_MASK_NBITS);
83}
84
85static inline bool linkmode_empty(const unsigned long *src)
86{
87 return bitmap_empty(src, __ETHTOOL_LINK_MODE_MASK_NBITS);
88}
89
90void phylink_set_port_modes(unsigned long *mask)
91{
92 phylink_set(mask, TP);
93 phylink_set(mask, AUI);
94 phylink_set(mask, MII);
95 phylink_set(mask, FIBRE);
96 phylink_set(mask, BNC);
97 phylink_set(mask, Backplane);
98}
99EXPORT_SYMBOL_GPL(phylink_set_port_modes);
100
101static int phylink_is_empty_linkmode(const unsigned long *linkmode)
102{
103 __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp) = { 0, };
104
105 phylink_set_port_modes(tmp);
106 phylink_set(tmp, Autoneg);
107 phylink_set(tmp, Pause);
108 phylink_set(tmp, Asym_Pause);
109
110 bitmap_andnot(tmp, linkmode, tmp, __ETHTOOL_LINK_MODE_MASK_NBITS);
111
112 return linkmode_empty(tmp);
113}
114
115static const char *phylink_an_mode_str(unsigned int mode)
116{
117 static const char *modestr[] = {
118 [MLO_AN_PHY] = "phy",
119 [MLO_AN_FIXED] = "fixed",
120 [MLO_AN_SGMII] = "SGMII",
121 [MLO_AN_8023Z] = "802.3z",
122 };
123
124 return mode < ARRAY_SIZE(modestr) ? modestr[mode] : "unknown";
125}
126
127static int phylink_validate(struct phylink *pl, unsigned long *supported,
128 struct phylink_link_state *state)
129{
130 pl->ops->validate(pl->netdev, supported, state);
131
132 return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
133}
134
135static int phylink_parse_fixedlink(struct phylink *pl, struct device_node *np)
136{
137 struct device_node *fixed_node;
138 const struct phy_setting *s;
139 struct gpio_desc *desc;
140 const __be32 *fixed_prop;
141 u32 speed;
142 int ret, len;
143
144 fixed_node = of_get_child_by_name(np, "fixed-link");
145 if (fixed_node) {
146 ret = of_property_read_u32(fixed_node, "speed", &speed);
147
148 pl->link_config.speed = speed;
149 pl->link_config.duplex = DUPLEX_HALF;
150
151 if (of_property_read_bool(fixed_node, "full-duplex"))
152 pl->link_config.duplex = DUPLEX_FULL;
153
154 /* We treat the "pause" and "asym-pause" terminology as
155 * defining the link partner's ability. */
156 if (of_property_read_bool(fixed_node, "pause"))
157 pl->link_config.pause |= MLO_PAUSE_SYM;
158 if (of_property_read_bool(fixed_node, "asym-pause"))
159 pl->link_config.pause |= MLO_PAUSE_ASYM;
160
161 if (ret == 0) {
162 desc = fwnode_get_named_gpiod(&fixed_node->fwnode,
163 "link-gpios", 0,
164 GPIOD_IN, "?");
165
166 if (!IS_ERR(desc))
167 pl->link_gpio = desc;
168 else if (desc == ERR_PTR(-EPROBE_DEFER))
169 ret = -EPROBE_DEFER;
170 }
171 of_node_put(fixed_node);
172
173 if (ret)
174 return ret;
175 } else {
176 fixed_prop = of_get_property(np, "fixed-link", &len);
177 if (!fixed_prop) {
178 netdev_err(pl->netdev, "broken fixed-link?\n");
179 return -EINVAL;
180 }
181 if (len == 5 * sizeof(*fixed_prop)) {
182 pl->link_config.duplex = be32_to_cpu(fixed_prop[1]) ?
183 DUPLEX_FULL : DUPLEX_HALF;
184 pl->link_config.speed = be32_to_cpu(fixed_prop[2]);
185 if (be32_to_cpu(fixed_prop[3]))
186 pl->link_config.pause |= MLO_PAUSE_SYM;
187 if (be32_to_cpu(fixed_prop[4]))
188 pl->link_config.pause |= MLO_PAUSE_ASYM;
189 }
190 }
191
192 if (pl->link_config.speed > SPEED_1000 &&
193 pl->link_config.duplex != DUPLEX_FULL)
194 netdev_warn(pl->netdev, "fixed link specifies half duplex for %dMbps link?\n",
195 pl->link_config.speed);
196
197 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
198 linkmode_copy(pl->link_config.advertising, pl->supported);
199 phylink_validate(pl, pl->supported, &pl->link_config);
200
201 s = phy_lookup_setting(pl->link_config.speed, pl->link_config.duplex,
202 pl->supported,
203 __ETHTOOL_LINK_MODE_MASK_NBITS, true);
204 linkmode_zero(pl->supported);
205 phylink_set(pl->supported, MII);
206 if (s) {
207 __set_bit(s->bit, pl->supported);
208 } else {
209 netdev_warn(pl->netdev, "fixed link %s duplex %dMbps not recognised\n",
210 pl->link_config.duplex == DUPLEX_FULL ? "full" : "half",
211 pl->link_config.speed);
212 }
213
214 linkmode_and(pl->link_config.advertising, pl->link_config.advertising,
215 pl->supported);
216
217 pl->link_config.link = 1;
218 pl->link_config.an_complete = 1;
219
220 return 0;
221}
222
223static int phylink_parse_mode(struct phylink *pl, struct device_node *np)
224{
225 struct device_node *dn;
226 const char *managed;
227
228 dn = of_get_child_by_name(np, "fixed-link");
229 if (dn || of_find_property(np, "fixed-link", NULL))
230 pl->link_an_mode = MLO_AN_FIXED;
231 of_node_put(dn);
232
233 if (of_property_read_string(np, "managed", &managed) == 0 &&
234 strcmp(managed, "in-band-status") == 0) {
235 if (pl->link_an_mode == MLO_AN_FIXED) {
236 netdev_err(pl->netdev,
237 "can't use both fixed-link and in-band-status\n");
238 return -EINVAL;
239 }
240
241 linkmode_zero(pl->supported);
242 phylink_set(pl->supported, MII);
243 phylink_set(pl->supported, Autoneg);
244 phylink_set(pl->supported, Asym_Pause);
245 phylink_set(pl->supported, Pause);
246 pl->link_config.an_enabled = true;
247
248 switch (pl->link_config.interface) {
249 case PHY_INTERFACE_MODE_SGMII:
250 phylink_set(pl->supported, 10baseT_Half);
251 phylink_set(pl->supported, 10baseT_Full);
252 phylink_set(pl->supported, 100baseT_Half);
253 phylink_set(pl->supported, 100baseT_Full);
254 phylink_set(pl->supported, 1000baseT_Half);
255 phylink_set(pl->supported, 1000baseT_Full);
256 pl->link_an_mode = MLO_AN_SGMII;
257 break;
258
259 case PHY_INTERFACE_MODE_1000BASEX:
260 phylink_set(pl->supported, 1000baseX_Full);
261 pl->link_an_mode = MLO_AN_8023Z;
262 break;
263
264 case PHY_INTERFACE_MODE_2500BASEX:
265 phylink_set(pl->supported, 2500baseX_Full);
266 pl->link_an_mode = MLO_AN_8023Z;
267 break;
268
da7c1862
RK
269 case PHY_INTERFACE_MODE_10GKR:
270 phylink_set(pl->supported, 10baseT_Half);
271 phylink_set(pl->supported, 10baseT_Full);
272 phylink_set(pl->supported, 100baseT_Half);
273 phylink_set(pl->supported, 100baseT_Full);
274 phylink_set(pl->supported, 1000baseT_Half);
275 phylink_set(pl->supported, 1000baseT_Full);
276 phylink_set(pl->supported, 1000baseX_Full);
277 phylink_set(pl->supported, 10000baseKR_Full);
278 phylink_set(pl->supported, 10000baseCR_Full);
279 phylink_set(pl->supported, 10000baseSR_Full);
280 phylink_set(pl->supported, 10000baseLR_Full);
281 phylink_set(pl->supported, 10000baseLRM_Full);
282 phylink_set(pl->supported, 10000baseER_Full);
283 pl->link_an_mode = MLO_AN_SGMII;
284 break;
285
9525ae83
RK
286 default:
287 netdev_err(pl->netdev,
288 "incorrect link mode %s for in-band status\n",
289 phy_modes(pl->link_config.interface));
290 return -EINVAL;
291 }
292
293 linkmode_copy(pl->link_config.advertising, pl->supported);
294
295 if (phylink_validate(pl, pl->supported, &pl->link_config)) {
296 netdev_err(pl->netdev,
297 "failed to validate link configuration for in-band status\n");
298 return -EINVAL;
299 }
300 }
301
302 return 0;
303}
304
305static void phylink_mac_config(struct phylink *pl,
306 const struct phylink_link_state *state)
307{
308 netdev_dbg(pl->netdev,
309 "%s: mode=%s/%s/%s/%s adv=%*pb pause=%02x link=%u an=%u\n",
310 __func__, phylink_an_mode_str(pl->link_an_mode),
311 phy_modes(state->interface),
312 phy_speed_to_str(state->speed),
313 phy_duplex_to_str(state->duplex),
314 __ETHTOOL_LINK_MODE_MASK_NBITS, state->advertising,
315 state->pause, state->link, state->an_enabled);
316
317 pl->ops->mac_config(pl->netdev, pl->link_an_mode, state);
318}
319
320static void phylink_mac_an_restart(struct phylink *pl)
321{
322 if (pl->link_config.an_enabled &&
365c1e64 323 phy_interface_mode_is_8023z(pl->link_config.interface))
9525ae83
RK
324 pl->ops->mac_an_restart(pl->netdev);
325}
326
327static int phylink_get_mac_state(struct phylink *pl, struct phylink_link_state *state)
328{
329 struct net_device *ndev = pl->netdev;
330
331 linkmode_copy(state->advertising, pl->link_config.advertising);
332 linkmode_zero(state->lp_advertising);
333 state->interface = pl->link_config.interface;
334 state->an_enabled = pl->link_config.an_enabled;
335 state->link = 1;
336
337 return pl->ops->mac_link_state(ndev, state);
338}
339
340/* The fixed state is... fixed except for the link state,
341 * which may be determined by a GPIO.
342 */
343static void phylink_get_fixed_state(struct phylink *pl, struct phylink_link_state *state)
344{
345 *state = pl->link_config;
346 if (pl->link_gpio)
347 state->link = !!gpiod_get_value(pl->link_gpio);
348}
349
350/* Flow control is resolved according to our and the link partners
351 * advertisments using the following drawn from the 802.3 specs:
352 * Local device Link partner
353 * Pause AsymDir Pause AsymDir Result
354 * 1 X 1 X TX+RX
355 * 0 1 1 1 RX
356 * 1 1 0 1 TX
357 */
358static void phylink_resolve_flow(struct phylink *pl,
516b29ed 359 struct phylink_link_state *state)
9525ae83
RK
360{
361 int new_pause = 0;
362
363 if (pl->link_config.pause & MLO_PAUSE_AN) {
364 int pause = 0;
365
366 if (phylink_test(pl->link_config.advertising, Pause))
367 pause |= MLO_PAUSE_SYM;
368 if (phylink_test(pl->link_config.advertising, Asym_Pause))
369 pause |= MLO_PAUSE_ASYM;
370
371 pause &= state->pause;
372
373 if (pause & MLO_PAUSE_SYM)
374 new_pause = MLO_PAUSE_TX | MLO_PAUSE_RX;
375 else if (pause & MLO_PAUSE_ASYM)
376 new_pause = state->pause & MLO_PAUSE_SYM ?
377 MLO_PAUSE_RX : MLO_PAUSE_TX;
378 } else {
379 new_pause = pl->link_config.pause & MLO_PAUSE_TXRX_MASK;
380 }
381
382 state->pause &= ~MLO_PAUSE_TXRX_MASK;
383 state->pause |= new_pause;
384}
385
386static const char *phylink_pause_to_str(int pause)
387{
388 switch (pause & MLO_PAUSE_TXRX_MASK) {
389 case MLO_PAUSE_TX | MLO_PAUSE_RX:
390 return "rx/tx";
391 case MLO_PAUSE_TX:
392 return "tx";
393 case MLO_PAUSE_RX:
394 return "rx";
395 default:
396 return "off";
397 }
398}
399
400static void phylink_resolve(struct work_struct *w)
401{
402 struct phylink *pl = container_of(w, struct phylink, resolve);
403 struct phylink_link_state link_state;
404 struct net_device *ndev = pl->netdev;
405
406 mutex_lock(&pl->state_mutex);
407 if (pl->phylink_disable_state) {
408 pl->mac_link_dropped = false;
409 link_state.link = false;
410 } else if (pl->mac_link_dropped) {
411 link_state.link = false;
412 } else {
413 switch (pl->link_an_mode) {
414 case MLO_AN_PHY:
415 link_state = pl->phy_state;
416 phylink_resolve_flow(pl, &link_state);
417 phylink_mac_config(pl, &link_state);
418 break;
419
420 case MLO_AN_FIXED:
421 phylink_get_fixed_state(pl, &link_state);
422 phylink_mac_config(pl, &link_state);
423 break;
424
425 case MLO_AN_SGMII:
cf4f2675 426 case MLO_AN_8023Z:
9525ae83
RK
427 phylink_get_mac_state(pl, &link_state);
428 if (pl->phydev) {
429 bool changed = false;
430
431 link_state.link = link_state.link &&
432 pl->phy_state.link;
433
434 if (pl->phy_state.interface !=
435 link_state.interface) {
436 link_state.interface = pl->phy_state.interface;
437 changed = true;
438 }
439
440 /* Propagate the flow control from the PHY
441 * to the MAC. Also propagate the interface
442 * if changed.
443 */
444 if (pl->phy_state.link || changed) {
445 link_state.pause |= pl->phy_state.pause;
446 phylink_resolve_flow(pl, &link_state);
447
448 phylink_mac_config(pl, &link_state);
449 }
450 }
451 break;
9525ae83
RK
452 }
453 }
454
455 if (link_state.link != netif_carrier_ok(ndev)) {
456 if (!link_state.link) {
457 netif_carrier_off(ndev);
458 pl->ops->mac_link_down(ndev, pl->link_an_mode);
459 netdev_info(ndev, "Link is Down\n");
460 } else {
461 pl->ops->mac_link_up(ndev, pl->link_an_mode,
462 pl->phydev);
463
464 netif_carrier_on(ndev);
465
466 netdev_info(ndev,
467 "Link is Up - %s/%s - flow control %s\n",
468 phy_speed_to_str(link_state.speed),
469 phy_duplex_to_str(link_state.duplex),
470 phylink_pause_to_str(link_state.pause));
471 }
472 }
473 if (!link_state.link && pl->mac_link_dropped) {
474 pl->mac_link_dropped = false;
475 queue_work(system_power_efficient_wq, &pl->resolve);
476 }
477 mutex_unlock(&pl->state_mutex);
478}
479
480static void phylink_run_resolve(struct phylink *pl)
481{
482 if (!pl->phylink_disable_state)
483 queue_work(system_power_efficient_wq, &pl->resolve);
484}
485
ce0aa27f
RK
486static const struct sfp_upstream_ops sfp_phylink_ops;
487
488static int phylink_register_sfp(struct phylink *pl, struct device_node *np)
489{
490 struct device_node *sfp_np;
491
492 sfp_np = of_parse_phandle(np, "sfp", 0);
493 if (!sfp_np)
494 return 0;
495
496 pl->sfp_bus = sfp_register_upstream(sfp_np, pl->netdev, pl,
497 &sfp_phylink_ops);
498 if (!pl->sfp_bus)
499 return -ENOMEM;
500
501 return 0;
502}
503
9525ae83 504struct phylink *phylink_create(struct net_device *ndev, struct device_node *np,
516b29ed
FF
505 phy_interface_t iface,
506 const struct phylink_mac_ops *ops)
9525ae83
RK
507{
508 struct phylink *pl;
509 int ret;
510
511 pl = kzalloc(sizeof(*pl), GFP_KERNEL);
512 if (!pl)
513 return ERR_PTR(-ENOMEM);
514
515 mutex_init(&pl->state_mutex);
516 INIT_WORK(&pl->resolve, phylink_resolve);
517 pl->netdev = ndev;
518 pl->phy_state.interface = iface;
519 pl->link_interface = iface;
520 pl->link_port = PORT_MII;
521 pl->link_config.interface = iface;
522 pl->link_config.pause = MLO_PAUSE_AN;
523 pl->link_config.speed = SPEED_UNKNOWN;
524 pl->link_config.duplex = DUPLEX_UNKNOWN;
525 pl->ops = ops;
526 __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
527
528 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
529 linkmode_copy(pl->link_config.advertising, pl->supported);
530 phylink_validate(pl, pl->supported, &pl->link_config);
531
532 ret = phylink_parse_mode(pl, np);
533 if (ret < 0) {
534 kfree(pl);
535 return ERR_PTR(ret);
536 }
537
538 if (pl->link_an_mode == MLO_AN_FIXED) {
539 ret = phylink_parse_fixedlink(pl, np);
540 if (ret < 0) {
541 kfree(pl);
542 return ERR_PTR(ret);
543 }
544 }
545
ce0aa27f
RK
546 ret = phylink_register_sfp(pl, np);
547 if (ret < 0) {
548 kfree(pl);
549 return ERR_PTR(ret);
550 }
551
9525ae83
RK
552 return pl;
553}
554EXPORT_SYMBOL_GPL(phylink_create);
555
556void phylink_destroy(struct phylink *pl)
557{
ce0aa27f
RK
558 if (pl->sfp_bus)
559 sfp_unregister_upstream(pl->sfp_bus);
560
9525ae83
RK
561 cancel_work_sync(&pl->resolve);
562 kfree(pl);
563}
564EXPORT_SYMBOL_GPL(phylink_destroy);
565
1ec6e530
WY
566static void phylink_phy_change(struct phy_device *phydev, bool up,
567 bool do_carrier)
9525ae83
RK
568{
569 struct phylink *pl = phydev->phylink;
570
571 mutex_lock(&pl->state_mutex);
572 pl->phy_state.speed = phydev->speed;
573 pl->phy_state.duplex = phydev->duplex;
574 pl->phy_state.pause = MLO_PAUSE_NONE;
575 if (phydev->pause)
576 pl->phy_state.pause |= MLO_PAUSE_SYM;
577 if (phydev->asym_pause)
578 pl->phy_state.pause |= MLO_PAUSE_ASYM;
579 pl->phy_state.interface = phydev->interface;
580 pl->phy_state.link = up;
581 mutex_unlock(&pl->state_mutex);
582
583 phylink_run_resolve(pl);
584
585 netdev_dbg(pl->netdev, "phy link %s %s/%s/%s\n", up ? "up" : "down",
516b29ed 586 phy_modes(phydev->interface),
9525ae83
RK
587 phy_speed_to_str(phydev->speed),
588 phy_duplex_to_str(phydev->duplex));
589}
590
591static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy)
592{
593 struct phylink_link_state config;
594 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
595 u32 advertising;
596 int ret;
597
598 memset(&config, 0, sizeof(config));
599 ethtool_convert_legacy_u32_to_link_mode(supported, phy->supported);
600 ethtool_convert_legacy_u32_to_link_mode(config.advertising,
601 phy->advertising);
602 config.interface = pl->link_config.interface;
603
604 /*
605 * This is the new way of dealing with flow control for PHYs,
606 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
607 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
608 * using our validate call to the MAC, we rely upon the MAC
609 * clearing the bits from both supported and advertising fields.
610 */
611 if (phylink_test(supported, Pause))
612 phylink_set(config.advertising, Pause);
613 if (phylink_test(supported, Asym_Pause))
614 phylink_set(config.advertising, Asym_Pause);
615
616 ret = phylink_validate(pl, supported, &config);
617 if (ret)
618 return ret;
619
620 phy->phylink = pl;
621 phy->phy_link_change = phylink_phy_change;
622
623 netdev_info(pl->netdev,
624 "PHY [%s] driver [%s]\n", dev_name(&phy->mdio.dev),
625 phy->drv->name);
626
627 mutex_lock(&phy->lock);
628 mutex_lock(&pl->state_mutex);
629 pl->netdev->phydev = phy;
630 pl->phydev = phy;
631 linkmode_copy(pl->supported, supported);
632 linkmode_copy(pl->link_config.advertising, config.advertising);
633
634 /* Restrict the phy advertisment according to the MAC support. */
635 ethtool_convert_link_mode_to_legacy_u32(&advertising, config.advertising);
636 phy->advertising = advertising;
637 mutex_unlock(&pl->state_mutex);
638 mutex_unlock(&phy->lock);
639
640 netdev_dbg(pl->netdev,
641 "phy: setting supported %*pb advertising 0x%08x\n",
642 __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
643 phy->advertising);
644
645 phy_start_machine(phy);
646 if (phy->irq > 0)
647 phy_start_interrupts(phy);
648
649 return 0;
650}
651
652int phylink_connect_phy(struct phylink *pl, struct phy_device *phy)
653{
654 int ret;
655
cf4f2675
RK
656 if (WARN_ON(pl->link_an_mode == MLO_AN_FIXED ||
657 pl->link_an_mode == MLO_AN_8023Z))
658 return -EINVAL;
659
9525ae83
RK
660 ret = phy_attach_direct(pl->netdev, phy, 0, pl->link_interface);
661 if (ret)
662 return ret;
663
664 ret = phylink_bringup_phy(pl, phy);
665 if (ret)
666 phy_detach(phy);
667
668 return ret;
669}
670EXPORT_SYMBOL_GPL(phylink_connect_phy);
671
672int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn)
673{
674 struct device_node *phy_node;
675 struct phy_device *phy_dev;
676 int ret;
677
cf4f2675
RK
678 /* Fixed links and 802.3z are handled without needing a PHY */
679 if (pl->link_an_mode == MLO_AN_FIXED ||
680 pl->link_an_mode == MLO_AN_8023Z)
9525ae83
RK
681 return 0;
682
683 phy_node = of_parse_phandle(dn, "phy-handle", 0);
684 if (!phy_node)
685 phy_node = of_parse_phandle(dn, "phy", 0);
686 if (!phy_node)
687 phy_node = of_parse_phandle(dn, "phy-device", 0);
688
689 if (!phy_node) {
690 if (pl->link_an_mode == MLO_AN_PHY) {
691 netdev_err(pl->netdev, "unable to find PHY node\n");
692 return -ENODEV;
693 }
694 return 0;
695 }
696
697 phy_dev = of_phy_attach(pl->netdev, phy_node, 0, pl->link_interface);
698 /* We're done with the phy_node handle */
699 of_node_put(phy_node);
700
701 if (!phy_dev)
702 return -ENODEV;
703
704 ret = phylink_bringup_phy(pl, phy_dev);
705 if (ret)
706 phy_detach(phy_dev);
707
708 return ret;
709}
710EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
711
712void phylink_disconnect_phy(struct phylink *pl)
713{
714 struct phy_device *phy;
715
716 WARN_ON(!lockdep_rtnl_is_held());
717
718 phy = pl->phydev;
719 if (phy) {
720 mutex_lock(&phy->lock);
721 mutex_lock(&pl->state_mutex);
722 pl->netdev->phydev = NULL;
723 pl->phydev = NULL;
724 mutex_unlock(&pl->state_mutex);
725 mutex_unlock(&phy->lock);
726 flush_work(&pl->resolve);
727
728 phy_disconnect(phy);
729 }
730}
731EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
732
733void phylink_mac_change(struct phylink *pl, bool up)
734{
735 if (!up)
736 pl->mac_link_dropped = true;
737 phylink_run_resolve(pl);
738 netdev_dbg(pl->netdev, "mac link %s\n", up ? "up" : "down");
739}
740EXPORT_SYMBOL_GPL(phylink_mac_change);
741
742void phylink_start(struct phylink *pl)
743{
744 WARN_ON(!lockdep_rtnl_is_held());
745
746 netdev_info(pl->netdev, "configuring for %s/%s link mode\n",
747 phylink_an_mode_str(pl->link_an_mode),
748 phy_modes(pl->link_config.interface));
749
750 /* Apply the link configuration to the MAC when starting. This allows
751 * a fixed-link to start with the correct parameters, and also
752 * ensures that we set the appropriate advertisment for Serdes links.
753 */
754 phylink_resolve_flow(pl, &pl->link_config);
755 phylink_mac_config(pl, &pl->link_config);
756
757 clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
758 phylink_run_resolve(pl);
759
ce0aa27f
RK
760 if (pl->sfp_bus)
761 sfp_upstream_start(pl->sfp_bus);
9525ae83
RK
762 if (pl->phydev)
763 phy_start(pl->phydev);
764}
765EXPORT_SYMBOL_GPL(phylink_start);
766
767void phylink_stop(struct phylink *pl)
768{
769 WARN_ON(!lockdep_rtnl_is_held());
770
771 if (pl->phydev)
772 phy_stop(pl->phydev);
ce0aa27f
RK
773 if (pl->sfp_bus)
774 sfp_upstream_stop(pl->sfp_bus);
9525ae83
RK
775
776 set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
2012b7d6 777 queue_work(system_power_efficient_wq, &pl->resolve);
9525ae83
RK
778 flush_work(&pl->resolve);
779}
780EXPORT_SYMBOL_GPL(phylink_stop);
781
782void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
783{
784 WARN_ON(!lockdep_rtnl_is_held());
785
786 wol->supported = 0;
787 wol->wolopts = 0;
788
789 if (pl->phydev)
790 phy_ethtool_get_wol(pl->phydev, wol);
791}
792EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol);
793
794int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
795{
796 int ret = -EOPNOTSUPP;
797
798 WARN_ON(!lockdep_rtnl_is_held());
799
800 if (pl->phydev)
801 ret = phy_ethtool_set_wol(pl->phydev, wol);
802
803 return ret;
804}
805EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol);
806
807static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b)
808{
809 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
810
811 linkmode_zero(mask);
812 phylink_set_port_modes(mask);
813
814 linkmode_and(dst, dst, mask);
815 linkmode_or(dst, dst, b);
816}
817
818static void phylink_get_ksettings(const struct phylink_link_state *state,
819 struct ethtool_link_ksettings *kset)
820{
821 phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
822 linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
823 kset->base.speed = state->speed;
824 kset->base.duplex = state->duplex;
825 kset->base.autoneg = state->an_enabled ? AUTONEG_ENABLE :
826 AUTONEG_DISABLE;
827}
828
829int phylink_ethtool_ksettings_get(struct phylink *pl,
516b29ed 830 struct ethtool_link_ksettings *kset)
9525ae83
RK
831{
832 struct phylink_link_state link_state;
833
834 WARN_ON(!lockdep_rtnl_is_held());
835
836 if (pl->phydev) {
837 phy_ethtool_ksettings_get(pl->phydev, kset);
838 } else {
839 kset->base.port = pl->link_port;
840 }
841
842 linkmode_copy(kset->link_modes.supported, pl->supported);
843
844 switch (pl->link_an_mode) {
845 case MLO_AN_FIXED:
846 /* We are using fixed settings. Report these as the
847 * current link settings - and note that these also
848 * represent the supported speeds/duplex/pause modes.
849 */
850 phylink_get_fixed_state(pl, &link_state);
851 phylink_get_ksettings(&link_state, kset);
852 break;
853
854 case MLO_AN_SGMII:
cf4f2675 855 case MLO_AN_8023Z:
9525ae83
RK
856 /* If there is a phy attached, then use the reported
857 * settings from the phy with no modification.
858 */
859 if (pl->phydev)
860 break;
861
9525ae83
RK
862 phylink_get_mac_state(pl, &link_state);
863
864 /* The MAC is reporting the link results from its own PCS
865 * layer via in-band status. Report these as the current
866 * link settings.
867 */
868 phylink_get_ksettings(&link_state, kset);
869 break;
870 }
871
872 return 0;
873}
874EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get);
875
876int phylink_ethtool_ksettings_set(struct phylink *pl,
516b29ed 877 const struct ethtool_link_ksettings *kset)
9525ae83
RK
878{
879 struct ethtool_link_ksettings our_kset;
880 struct phylink_link_state config;
881 int ret;
882
883 WARN_ON(!lockdep_rtnl_is_held());
884
885 if (kset->base.autoneg != AUTONEG_DISABLE &&
886 kset->base.autoneg != AUTONEG_ENABLE)
887 return -EINVAL;
888
889 config = pl->link_config;
890
891 /* Mask out unsupported advertisments */
892 linkmode_and(config.advertising, kset->link_modes.advertising,
893 pl->supported);
894
895 /* FIXME: should we reject autoneg if phy/mac does not support it? */
896 if (kset->base.autoneg == AUTONEG_DISABLE) {
897 const struct phy_setting *s;
898
899 /* Autonegotiation disabled, select a suitable speed and
900 * duplex.
901 */
902 s = phy_lookup_setting(kset->base.speed, kset->base.duplex,
903 pl->supported,
904 __ETHTOOL_LINK_MODE_MASK_NBITS, false);
905 if (!s)
906 return -EINVAL;
907
908 /* If we have a fixed link (as specified by firmware), refuse
909 * to change link parameters.
910 */
911 if (pl->link_an_mode == MLO_AN_FIXED &&
912 (s->speed != pl->link_config.speed ||
913 s->duplex != pl->link_config.duplex))
914 return -EINVAL;
915
916 config.speed = s->speed;
917 config.duplex = s->duplex;
918 config.an_enabled = false;
919
920 __clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
921 } else {
922 /* If we have a fixed link, refuse to enable autonegotiation */
923 if (pl->link_an_mode == MLO_AN_FIXED)
924 return -EINVAL;
925
926 config.speed = SPEED_UNKNOWN;
927 config.duplex = DUPLEX_UNKNOWN;
928 config.an_enabled = true;
929
930 __set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
931 }
932
933 if (phylink_validate(pl, pl->supported, &config))
934 return -EINVAL;
935
936 /* If autonegotiation is enabled, we must have an advertisment */
937 if (config.an_enabled && phylink_is_empty_linkmode(config.advertising))
938 return -EINVAL;
939
940 our_kset = *kset;
941 linkmode_copy(our_kset.link_modes.advertising, config.advertising);
942 our_kset.base.speed = config.speed;
943 our_kset.base.duplex = config.duplex;
944
945 /* If we have a PHY, configure the phy */
946 if (pl->phydev) {
947 ret = phy_ethtool_ksettings_set(pl->phydev, &our_kset);
948 if (ret)
949 return ret;
950 }
951
952 mutex_lock(&pl->state_mutex);
953 /* Configure the MAC to match the new settings */
954 linkmode_copy(pl->link_config.advertising, our_kset.link_modes.advertising);
955 pl->link_config.speed = our_kset.base.speed;
956 pl->link_config.duplex = our_kset.base.duplex;
957 pl->link_config.an_enabled = our_kset.base.autoneg != AUTONEG_DISABLE;
958
959 if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
960 phylink_mac_config(pl, &pl->link_config);
961 phylink_mac_an_restart(pl);
962 }
963 mutex_unlock(&pl->state_mutex);
964
d18c2a1b 965 return 0;
9525ae83
RK
966}
967EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
968
969int phylink_ethtool_nway_reset(struct phylink *pl)
970{
971 int ret = 0;
972
973 WARN_ON(!lockdep_rtnl_is_held());
974
975 if (pl->phydev)
976 ret = phy_restart_aneg(pl->phydev);
977 phylink_mac_an_restart(pl);
978
979 return ret;
980}
981EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
982
983void phylink_ethtool_get_pauseparam(struct phylink *pl,
984 struct ethtool_pauseparam *pause)
985{
986 WARN_ON(!lockdep_rtnl_is_held());
987
988 pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
989 pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
990 pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
991}
992EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
993
994int phylink_ethtool_set_pauseparam(struct phylink *pl,
995 struct ethtool_pauseparam *pause)
996{
997 struct phylink_link_state *config = &pl->link_config;
998
999 WARN_ON(!lockdep_rtnl_is_held());
1000
1001 if (!phylink_test(pl->supported, Pause) &&
1002 !phylink_test(pl->supported, Asym_Pause))
1003 return -EOPNOTSUPP;
1004
1005 if (!phylink_test(pl->supported, Asym_Pause) &&
1006 !pause->autoneg && pause->rx_pause != pause->tx_pause)
1007 return -EINVAL;
1008
1009 config->pause &= ~(MLO_PAUSE_AN | MLO_PAUSE_TXRX_MASK);
1010
1011 if (pause->autoneg)
1012 config->pause |= MLO_PAUSE_AN;
1013 if (pause->rx_pause)
1014 config->pause |= MLO_PAUSE_RX;
1015 if (pause->tx_pause)
1016 config->pause |= MLO_PAUSE_TX;
1017
1018 if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
1019 switch (pl->link_an_mode) {
1020 case MLO_AN_PHY:
1021 /* Silently mark the carrier down, and then trigger a resolve */
1022 netif_carrier_off(pl->netdev);
1023 phylink_run_resolve(pl);
1024 break;
1025
1026 case MLO_AN_FIXED:
1027 /* Should we allow fixed links to change against the config? */
1028 phylink_resolve_flow(pl, config);
1029 phylink_mac_config(pl, config);
1030 break;
1031
1032 case MLO_AN_SGMII:
1033 case MLO_AN_8023Z:
1034 phylink_mac_config(pl, config);
1035 phylink_mac_an_restart(pl);
1036 break;
1037 }
1038 }
1039
1040 return 0;
1041}
1042EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
1043
770a1ad5
RK
1044int phylink_ethtool_get_module_info(struct phylink *pl,
1045 struct ethtool_modinfo *modinfo)
1046{
1047 int ret = -EOPNOTSUPP;
1048
1049 WARN_ON(!lockdep_rtnl_is_held());
1050
1051 if (pl->sfp_bus)
1052 ret = sfp_get_module_info(pl->sfp_bus, modinfo);
1053
1054 return ret;
1055}
1056EXPORT_SYMBOL_GPL(phylink_ethtool_get_module_info);
1057
1058int phylink_ethtool_get_module_eeprom(struct phylink *pl,
1059 struct ethtool_eeprom *ee, u8 *buf)
1060{
1061 int ret = -EOPNOTSUPP;
1062
1063 WARN_ON(!lockdep_rtnl_is_held());
1064
1065 if (pl->sfp_bus)
1066 ret = sfp_get_module_eeprom(pl->sfp_bus, ee, buf);
1067
1068 return ret;
1069}
1070EXPORT_SYMBOL_GPL(phylink_ethtool_get_module_eeprom);
1071
9525ae83
RK
1072int phylink_init_eee(struct phylink *pl, bool clk_stop_enable)
1073{
1074 int ret = -EPROTONOSUPPORT;
1075
1076 WARN_ON(!lockdep_rtnl_is_held());
1077
1078 if (pl->phydev)
1079 ret = phy_init_eee(pl->phydev, clk_stop_enable);
1080
1081 return ret;
1082}
1083EXPORT_SYMBOL_GPL(phylink_init_eee);
1084
1085int phylink_get_eee_err(struct phylink *pl)
1086{
1087 int ret = 0;
1088
1089 WARN_ON(!lockdep_rtnl_is_held());
1090
1091 if (pl->phydev)
1092 ret = phy_get_eee_err(pl->phydev);
1093
1094 return ret;
1095}
1096EXPORT_SYMBOL_GPL(phylink_get_eee_err);
1097
1098int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee)
1099{
1100 int ret = -EOPNOTSUPP;
1101
1102 WARN_ON(!lockdep_rtnl_is_held());
1103
1104 if (pl->phydev)
1105 ret = phy_ethtool_get_eee(pl->phydev, eee);
1106
1107 return ret;
1108}
1109EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
1110
1111int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_eee *eee)
1112{
1113 int ret = -EOPNOTSUPP;
1114
1115 WARN_ON(!lockdep_rtnl_is_held());
1116
1117 if (pl->phydev)
1118 ret = phy_ethtool_set_eee(pl->phydev, eee);
1119
1120 return ret;
1121}
1122EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
1123
1124/* This emulates MII registers for a fixed-mode phy operating as per the
1125 * passed in state. "aneg" defines if we report negotiation is possible.
1126 *
1127 * FIXME: should deal with negotiation state too.
1128 */
1129static int phylink_mii_emul_read(struct net_device *ndev, unsigned int reg,
1130 struct phylink_link_state *state, bool aneg)
1131{
1132 struct fixed_phy_status fs;
1133 int val;
1134
1135 fs.link = state->link;
1136 fs.speed = state->speed;
1137 fs.duplex = state->duplex;
1138 fs.pause = state->pause & MLO_PAUSE_SYM;
1139 fs.asym_pause = state->pause & MLO_PAUSE_ASYM;
1140
1141 val = swphy_read_reg(reg, &fs);
1142 if (reg == MII_BMSR) {
1143 if (!state->an_complete)
1144 val &= ~BMSR_ANEGCOMPLETE;
1145 if (!aneg)
1146 val &= ~BMSR_ANEGCAPABLE;
1147 }
1148 return val;
1149}
1150
ecbd87b8
RK
1151static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
1152 unsigned int reg)
1153{
1154 struct phy_device *phydev = pl->phydev;
1155 int prtad, devad;
1156
1157 if (mdio_phy_id_is_c45(phy_id)) {
1158 prtad = mdio_phy_id_prtad(phy_id);
1159 devad = mdio_phy_id_devad(phy_id);
1160 devad = MII_ADDR_C45 | devad << 16 | reg;
1161 } else if (phydev->is_c45) {
1162 switch (reg) {
1163 case MII_BMCR:
1164 case MII_BMSR:
1165 case MII_PHYSID1:
1166 case MII_PHYSID2:
1167 devad = __ffs(phydev->c45_ids.devices_in_package);
1168 break;
1169 case MII_ADVERTISE:
1170 case MII_LPA:
1171 if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1172 return -EINVAL;
1173 devad = MDIO_MMD_AN;
1174 if (reg == MII_ADVERTISE)
1175 reg = MDIO_AN_ADVERTISE;
1176 else
1177 reg = MDIO_AN_LPA;
1178 break;
1179 default:
1180 return -EINVAL;
1181 }
1182 prtad = phy_id;
1183 devad = MII_ADDR_C45 | devad << 16 | reg;
1184 } else {
1185 prtad = phy_id;
1186 devad = reg;
1187 }
1188 return mdiobus_read(pl->phydev->mdio.bus, prtad, devad);
1189}
1190
1191static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
1192 unsigned int reg, unsigned int val)
1193{
1194 struct phy_device *phydev = pl->phydev;
1195 int prtad, devad;
1196
1197 if (mdio_phy_id_is_c45(phy_id)) {
1198 prtad = mdio_phy_id_prtad(phy_id);
1199 devad = mdio_phy_id_devad(phy_id);
1200 devad = MII_ADDR_C45 | devad << 16 | reg;
1201 } else if (phydev->is_c45) {
1202 switch (reg) {
1203 case MII_BMCR:
1204 case MII_BMSR:
1205 case MII_PHYSID1:
1206 case MII_PHYSID2:
1207 devad = __ffs(phydev->c45_ids.devices_in_package);
1208 break;
1209 case MII_ADVERTISE:
1210 case MII_LPA:
1211 if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1212 return -EINVAL;
1213 devad = MDIO_MMD_AN;
1214 if (reg == MII_ADVERTISE)
1215 reg = MDIO_AN_ADVERTISE;
1216 else
1217 reg = MDIO_AN_LPA;
1218 break;
1219 default:
1220 return -EINVAL;
1221 }
1222 prtad = phy_id;
1223 devad = MII_ADDR_C45 | devad << 16 | reg;
1224 } else {
1225 prtad = phy_id;
1226 devad = reg;
1227 }
1228
1229 return mdiobus_write(phydev->mdio.bus, prtad, devad, val);
1230}
1231
9525ae83
RK
1232static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
1233 unsigned int reg)
1234{
1235 struct phylink_link_state state;
1236 int val = 0xffff;
1237
9525ae83
RK
1238 switch (pl->link_an_mode) {
1239 case MLO_AN_FIXED:
1240 if (phy_id == 0) {
1241 phylink_get_fixed_state(pl, &state);
1242 val = phylink_mii_emul_read(pl->netdev, reg, &state,
1243 true);
1244 }
1245 break;
1246
1247 case MLO_AN_PHY:
1248 return -EOPNOTSUPP;
1249
1250 case MLO_AN_SGMII:
1251 /* No phy, fall through to 8023z method */
1252 case MLO_AN_8023Z:
1253 if (phy_id == 0) {
1254 val = phylink_get_mac_state(pl, &state);
1255 if (val < 0)
1256 return val;
1257
1258 val = phylink_mii_emul_read(pl->netdev, reg, &state,
1259 true);
1260 }
1261 break;
1262 }
1263
1264 return val & 0xffff;
1265}
1266
1267static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
1268 unsigned int reg, unsigned int val)
1269{
9525ae83
RK
1270 switch (pl->link_an_mode) {
1271 case MLO_AN_FIXED:
1272 break;
1273
1274 case MLO_AN_PHY:
1275 return -EOPNOTSUPP;
1276
1277 case MLO_AN_SGMII:
1278 /* No phy, fall through to 8023z method */
1279 case MLO_AN_8023Z:
1280 break;
1281 }
1282
1283 return 0;
1284}
1285
1286int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
1287{
ecbd87b8
RK
1288 struct mii_ioctl_data *mii = if_mii(ifr);
1289 int ret;
9525ae83
RK
1290
1291 WARN_ON(!lockdep_rtnl_is_held());
1292
ecbd87b8
RK
1293 if (pl->phydev) {
1294 /* PHYs only exist for MLO_AN_PHY and MLO_AN_SGMII */
1295 switch (cmd) {
1296 case SIOCGMIIPHY:
1297 mii->phy_id = pl->phydev->mdio.addr;
1298
1299 case SIOCGMIIREG:
1300 ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
1301 if (ret >= 0) {
1302 mii->val_out = ret;
1303 ret = 0;
1304 }
1305 break;
9525ae83 1306
ecbd87b8
RK
1307 case SIOCSMIIREG:
1308 ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
1309 mii->val_in);
1310 break;
1311
1312 default:
1313 ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
1314 break;
9525ae83 1315 }
ecbd87b8
RK
1316 } else {
1317 switch (cmd) {
1318 case SIOCGMIIPHY:
1319 mii->phy_id = 0;
1320
1321 case SIOCGMIIREG:
1322 ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
1323 if (ret >= 0) {
1324 mii->val_out = ret;
1325 ret = 0;
1326 }
1327 break;
9525ae83 1328
ecbd87b8
RK
1329 case SIOCSMIIREG:
1330 ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
1331 mii->val_in);
1332 break;
9525ae83 1333
ecbd87b8
RK
1334 default:
1335 ret = -EOPNOTSUPP;
1336 break;
1337 }
9525ae83
RK
1338 }
1339
1340 return ret;
1341}
1342EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
1343
ce0aa27f
RK
1344static int phylink_sfp_module_insert(void *upstream,
1345 const struct sfp_eeprom_id *id)
1346{
1347 struct phylink *pl = upstream;
1348 __ETHTOOL_DECLARE_LINK_MODE_MASK(support) = { 0, };
1349 struct phylink_link_state config;
1350 phy_interface_t iface;
1351 int mode, ret = 0;
1352 bool changed;
1353 u8 port;
1354
1355 sfp_parse_support(pl->sfp_bus, id, support);
1356 port = sfp_parse_port(pl->sfp_bus, id, support);
1357 iface = sfp_parse_interface(pl->sfp_bus, id);
1358
1359 WARN_ON(!lockdep_rtnl_is_held());
1360
1361 switch (iface) {
1362 case PHY_INTERFACE_MODE_SGMII:
1363 mode = MLO_AN_SGMII;
1364 break;
1365 case PHY_INTERFACE_MODE_1000BASEX:
1366 mode = MLO_AN_8023Z;
1367 break;
1368 default:
1369 return -EINVAL;
1370 }
1371
1372 memset(&config, 0, sizeof(config));
1373 linkmode_copy(config.advertising, support);
1374 config.interface = iface;
1375 config.speed = SPEED_UNKNOWN;
1376 config.duplex = DUPLEX_UNKNOWN;
1377 config.pause = MLO_PAUSE_AN;
1378 config.an_enabled = pl->link_config.an_enabled;
1379
1380 /* Ignore errors if we're expecting a PHY to attach later */
1381 ret = phylink_validate(pl, support, &config);
1382 if (ret) {
1383 netdev_err(pl->netdev, "validation of %s/%s with support %*pb failed: %d\n",
1384 phylink_an_mode_str(mode), phy_modes(config.interface),
1385 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
1386 return ret;
1387 }
1388
1389 netdev_dbg(pl->netdev, "requesting link mode %s/%s with support %*pb\n",
1390 phylink_an_mode_str(mode), phy_modes(config.interface),
1391 __ETHTOOL_LINK_MODE_MASK_NBITS, support);
1392
1393 if (mode == MLO_AN_8023Z && pl->phydev)
1394 return -EINVAL;
1395
1396 changed = !bitmap_equal(pl->supported, support,
1397 __ETHTOOL_LINK_MODE_MASK_NBITS);
1398 if (changed) {
1399 linkmode_copy(pl->supported, support);
1400 linkmode_copy(pl->link_config.advertising, config.advertising);
1401 }
1402
1403 if (pl->link_an_mode != mode ||
1404 pl->link_config.interface != config.interface) {
1405 pl->link_config.interface = config.interface;
1406 pl->link_an_mode = mode;
1407
1408 changed = true;
1409
1410 netdev_info(pl->netdev, "switched to %s/%s link mode\n",
1411 phylink_an_mode_str(mode),
1412 phy_modes(config.interface));
1413 }
1414
1415 pl->link_port = port;
1416
1417 if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
1418 &pl->phylink_disable_state))
1419 phylink_mac_config(pl, &pl->link_config);
1420
1421 return ret;
1422}
1423
1424static void phylink_sfp_link_down(void *upstream)
1425{
1426 struct phylink *pl = upstream;
1427
1428 WARN_ON(!lockdep_rtnl_is_held());
1429
1430 set_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
1431 flush_work(&pl->resolve);
1432
1433 netif_carrier_off(pl->netdev);
1434}
1435
1436static void phylink_sfp_link_up(void *upstream)
1437{
1438 struct phylink *pl = upstream;
1439
1440 WARN_ON(!lockdep_rtnl_is_held());
1441
1442 clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
1443 phylink_run_resolve(pl);
1444}
1445
1446static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
1447{
1448 return phylink_connect_phy(upstream, phy);
1449}
1450
1451static void phylink_sfp_disconnect_phy(void *upstream)
1452{
1453 phylink_disconnect_phy(upstream);
1454}
1455
1456static const struct sfp_upstream_ops sfp_phylink_ops = {
1457 .module_insert = phylink_sfp_module_insert,
1458 .link_up = phylink_sfp_link_up,
1459 .link_down = phylink_sfp_link_down,
1460 .connect_phy = phylink_sfp_connect_phy,
1461 .disconnect_phy = phylink_sfp_disconnect_phy,
1462};
1463
9525ae83 1464MODULE_LICENSE("GPL");