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