blob: 70f3f90c2ed69277970e48a1dd88d88003f1c21b [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001/*
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/timer.h>
23#include <linux/workqueue.h>
24
25#include "sfp.h"
26#include "swphy.h"
27
28#define SUPPORTED_INTERFACES \
29 (SUPPORTED_TP | SUPPORTED_MII | SUPPORTED_FIBRE | \
30 SUPPORTED_BNC | SUPPORTED_AUI | SUPPORTED_Backplane)
31#define ADVERTISED_INTERFACES \
32 (ADVERTISED_TP | ADVERTISED_MII | ADVERTISED_FIBRE | \
33 ADVERTISED_BNC | ADVERTISED_AUI | ADVERTISED_Backplane)
34
35enum {
36 PHYLINK_DISABLE_STOPPED,
37 PHYLINK_DISABLE_LINK,
38};
39
40/**
41 * struct phylink - internal data type for phylink
42 */
43struct phylink {
44 /* private: */
45 struct net_device *netdev;
46 const struct phylink_mac_ops *ops;
47
48 unsigned long phylink_disable_state; /* bitmask of disables */
49 struct phy_device *phydev;
50 phy_interface_t link_interface; /* PHY_INTERFACE_xxx */
51 u8 link_an_mode; /* MLO_AN_xxx */
52 u8 link_port; /* The current non-phy ethtool port */
53 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
54
55 /* The link configuration settings */
56 struct phylink_link_state link_config;
57 struct gpio_desc *link_gpio;
58 struct timer_list link_poll;
59 void (*get_fixed_state)(struct net_device *dev,
60 struct phylink_link_state *s);
61
62 struct mutex state_mutex;
63 struct phylink_link_state phy_state;
64 struct work_struct resolve;
65
66 bool mac_link_dropped;
67
68 struct sfp_bus *sfp_bus;
69};
70
71static inline void linkmode_zero(unsigned long *dst)
72{
73 bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS);
74}
75
76static inline void linkmode_copy(unsigned long *dst, const unsigned long *src)
77{
78 bitmap_copy(dst, src, __ETHTOOL_LINK_MODE_MASK_NBITS);
79}
80
81static inline void linkmode_and(unsigned long *dst, const unsigned long *a,
82 const unsigned long *b)
83{
84 bitmap_and(dst, a, b, __ETHTOOL_LINK_MODE_MASK_NBITS);
85}
86
87static inline void linkmode_or(unsigned long *dst, const unsigned long *a,
88 const unsigned long *b)
89{
90 bitmap_or(dst, a, b, __ETHTOOL_LINK_MODE_MASK_NBITS);
91}
92
93static inline bool linkmode_empty(const unsigned long *src)
94{
95 return bitmap_empty(src, __ETHTOOL_LINK_MODE_MASK_NBITS);
96}
97
98/**
99 * phylink_set_port_modes() - set the port type modes in the ethtool mask
100 * @mask: ethtool link mode mask
101 *
102 * Sets all the port type modes in the ethtool mask. MAC drivers should
103 * use this in their 'validate' callback.
104 */
105void phylink_set_port_modes(unsigned long *mask)
106{
107 phylink_set(mask, TP);
108 phylink_set(mask, AUI);
109 phylink_set(mask, MII);
110 phylink_set(mask, FIBRE);
111 phylink_set(mask, BNC);
112 phylink_set(mask, Backplane);
113}
114EXPORT_SYMBOL_GPL(phylink_set_port_modes);
115
116static int phylink_is_empty_linkmode(const unsigned long *linkmode)
117{
118 __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp) = { 0, };
119
120 phylink_set_port_modes(tmp);
121 phylink_set(tmp, Autoneg);
122 phylink_set(tmp, Pause);
123 phylink_set(tmp, Asym_Pause);
124
125 bitmap_andnot(tmp, linkmode, tmp, __ETHTOOL_LINK_MODE_MASK_NBITS);
126
127 return linkmode_empty(tmp);
128}
129
130static const char *phylink_an_mode_str(unsigned int mode)
131{
132 static const char *modestr[] = {
133 [MLO_AN_PHY] = "phy",
134 [MLO_AN_FIXED] = "fixed",
135 [MLO_AN_INBAND] = "inband",
136 };
137
138 return mode < ARRAY_SIZE(modestr) ? modestr[mode] : "unknown";
139}
140
141static int phylink_validate(struct phylink *pl, unsigned long *supported,
142 struct phylink_link_state *state)
143{
144 pl->ops->validate(pl->netdev, supported, state);
145
146 return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
147}
148
149static int phylink_parse_fixedlink(struct phylink *pl,
150 struct fwnode_handle *fwnode)
151{
152 struct fwnode_handle *fixed_node;
153 const struct phy_setting *s;
154 struct gpio_desc *desc;
155 u32 speed;
156 int ret;
157
158 fixed_node = fwnode_get_named_child_node(fwnode, "fixed-link");
159 if (fixed_node) {
160 ret = fwnode_property_read_u32(fixed_node, "speed", &speed);
161
162 pl->link_config.speed = speed;
163 pl->link_config.duplex = DUPLEX_HALF;
164
165 if (fwnode_property_read_bool(fixed_node, "full-duplex"))
166 pl->link_config.duplex = DUPLEX_FULL;
167
168 /* We treat the "pause" and "asym-pause" terminology as
169 * defining the link partner's ability. */
170 if (fwnode_property_read_bool(fixed_node, "pause"))
171 pl->link_config.pause |= MLO_PAUSE_SYM;
172 if (fwnode_property_read_bool(fixed_node, "asym-pause"))
173 pl->link_config.pause |= MLO_PAUSE_ASYM;
174
175 if (ret == 0) {
176 desc = fwnode_get_named_gpiod(fixed_node, "link-gpios",
177 0, GPIOD_IN, "?");
178
179 if (!IS_ERR(desc))
180 pl->link_gpio = desc;
181 else if (desc == ERR_PTR(-EPROBE_DEFER))
182 ret = -EPROBE_DEFER;
183 }
184 fwnode_handle_put(fixed_node);
185
186 if (ret)
187 return ret;
188 } else {
189 u32 prop[5];
190
191 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
192 NULL, 0);
193 if (ret != ARRAY_SIZE(prop)) {
194 netdev_err(pl->netdev, "broken fixed-link?\n");
195 return -EINVAL;
196 }
197
198 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
199 prop, ARRAY_SIZE(prop));
200 if (!ret) {
201 pl->link_config.duplex = prop[1] ?
202 DUPLEX_FULL : DUPLEX_HALF;
203 pl->link_config.speed = prop[2];
204 if (prop[3])
205 pl->link_config.pause |= MLO_PAUSE_SYM;
206 if (prop[4])
207 pl->link_config.pause |= MLO_PAUSE_ASYM;
208 }
209 }
210
211 if (pl->link_config.speed > SPEED_1000 &&
212 pl->link_config.duplex != DUPLEX_FULL)
213 netdev_warn(pl->netdev, "fixed link specifies half duplex for %dMbps link?\n",
214 pl->link_config.speed);
215
216 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
217 linkmode_copy(pl->link_config.advertising, pl->supported);
218 phylink_validate(pl, pl->supported, &pl->link_config);
219
220 s = phy_lookup_setting(pl->link_config.speed, pl->link_config.duplex,
221 pl->supported,
222 __ETHTOOL_LINK_MODE_MASK_NBITS, true);
223 linkmode_zero(pl->supported);
224 phylink_set(pl->supported, MII);
225 if (s) {
226 __set_bit(s->bit, pl->supported);
227 } else {
228 netdev_warn(pl->netdev, "fixed link %s duplex %dMbps not recognised\n",
229 pl->link_config.duplex == DUPLEX_FULL ? "full" : "half",
230 pl->link_config.speed);
231 }
232
233 linkmode_and(pl->link_config.advertising, pl->link_config.advertising,
234 pl->supported);
235
236 pl->link_config.link = 1;
237 pl->link_config.an_complete = 1;
238
239 return 0;
240}
241
242static int phylink_parse_mode(struct phylink *pl, struct fwnode_handle *fwnode)
243{
244 struct fwnode_handle *dn;
245 const char *managed;
246
247 dn = fwnode_get_named_child_node(fwnode, "fixed-link");
248 if (dn || fwnode_property_present(fwnode, "fixed-link"))
249 pl->link_an_mode = MLO_AN_FIXED;
250 fwnode_handle_put(dn);
251
252 if (fwnode_property_read_string(fwnode, "managed", &managed) == 0 &&
253 strcmp(managed, "in-band-status") == 0) {
254 if (pl->link_an_mode == MLO_AN_FIXED) {
255 netdev_err(pl->netdev,
256 "can't use both fixed-link and in-band-status\n");
257 return -EINVAL;
258 }
259
260 linkmode_zero(pl->supported);
261 phylink_set(pl->supported, MII);
262 phylink_set(pl->supported, Autoneg);
263 phylink_set(pl->supported, Asym_Pause);
264 phylink_set(pl->supported, Pause);
265 pl->link_config.an_enabled = true;
266 pl->link_an_mode = MLO_AN_INBAND;
267
268 switch (pl->link_config.interface) {
269 case PHY_INTERFACE_MODE_SGMII:
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 break;
277
278 case PHY_INTERFACE_MODE_1000BASEX:
279 phylink_set(pl->supported, 1000baseX_Full);
280 break;
281
282 case PHY_INTERFACE_MODE_2500BASEX:
283 phylink_set(pl->supported, 2500baseX_Full);
284 break;
285
286 case PHY_INTERFACE_MODE_10GKR:
287 phylink_set(pl->supported, 10baseT_Half);
288 phylink_set(pl->supported, 10baseT_Full);
289 phylink_set(pl->supported, 100baseT_Half);
290 phylink_set(pl->supported, 100baseT_Full);
291 phylink_set(pl->supported, 1000baseT_Half);
292 phylink_set(pl->supported, 1000baseT_Full);
293 phylink_set(pl->supported, 1000baseX_Full);
294 phylink_set(pl->supported, 10000baseKR_Full);
295 phylink_set(pl->supported, 10000baseCR_Full);
296 phylink_set(pl->supported, 10000baseSR_Full);
297 phylink_set(pl->supported, 10000baseLR_Full);
298 phylink_set(pl->supported, 10000baseLRM_Full);
299 phylink_set(pl->supported, 10000baseER_Full);
300 break;
301
302 default:
303 netdev_err(pl->netdev,
304 "incorrect link mode %s for in-band status\n",
305 phy_modes(pl->link_config.interface));
306 return -EINVAL;
307 }
308
309 linkmode_copy(pl->link_config.advertising, pl->supported);
310
311 if (phylink_validate(pl, pl->supported, &pl->link_config)) {
312 netdev_err(pl->netdev,
313 "failed to validate link configuration for in-band status\n");
314 return -EINVAL;
315 }
316 }
317
318 return 0;
319}
320
321static void phylink_mac_config(struct phylink *pl,
322 const struct phylink_link_state *state)
323{
324 netdev_dbg(pl->netdev,
325 "%s: mode=%s/%s/%s/%s adv=%*pb pause=%02x link=%u an=%u\n",
326 __func__, phylink_an_mode_str(pl->link_an_mode),
327 phy_modes(state->interface),
328 phy_speed_to_str(state->speed),
329 phy_duplex_to_str(state->duplex),
330 __ETHTOOL_LINK_MODE_MASK_NBITS, state->advertising,
331 state->pause, state->link, state->an_enabled);
332
333 pl->ops->mac_config(pl->netdev, pl->link_an_mode, state);
334}
335
336static void phylink_mac_an_restart(struct phylink *pl)
337{
338 if (pl->link_config.an_enabled &&
339 phy_interface_mode_is_8023z(pl->link_config.interface))
340 pl->ops->mac_an_restart(pl->netdev);
341}
342
343static int phylink_get_mac_state(struct phylink *pl, struct phylink_link_state *state)
344{
345 struct net_device *ndev = pl->netdev;
346
347 linkmode_copy(state->advertising, pl->link_config.advertising);
348 linkmode_zero(state->lp_advertising);
349 state->interface = pl->link_config.interface;
350 state->an_enabled = pl->link_config.an_enabled;
351 state->link = 1;
352
353 return pl->ops->mac_link_state(ndev, state);
354}
355
356/* The fixed state is... fixed except for the link state,
357 * which may be determined by a GPIO or a callback.
358 */
359static void phylink_get_fixed_state(struct phylink *pl, struct phylink_link_state *state)
360{
361 *state = pl->link_config;
362 if (pl->get_fixed_state)
363 pl->get_fixed_state(pl->netdev, state);
364 else if (pl->link_gpio)
365 state->link = !!gpiod_get_value_cansleep(pl->link_gpio);
366}
367
368/* Flow control is resolved according to our and the link partners
369 * advertisements using the following drawn from the 802.3 specs:
370 * Local device Link partner
371 * Pause AsymDir Pause AsymDir Result
372 * 1 X 1 X TX+RX
373 * 0 1 1 1 RX
374 * 1 1 0 1 TX
375 */
376static void phylink_resolve_flow(struct phylink *pl,
377 struct phylink_link_state *state)
378{
379 int new_pause = 0;
380
381 if (pl->link_config.pause & MLO_PAUSE_AN) {
382 int pause = 0;
383
384 if (phylink_test(pl->link_config.advertising, Pause))
385 pause |= MLO_PAUSE_SYM;
386 if (phylink_test(pl->link_config.advertising, Asym_Pause))
387 pause |= MLO_PAUSE_ASYM;
388
389 pause &= state->pause;
390
391 if (pause & MLO_PAUSE_SYM)
392 new_pause = MLO_PAUSE_TX | MLO_PAUSE_RX;
393 else if (pause & MLO_PAUSE_ASYM)
394 new_pause = state->pause & MLO_PAUSE_SYM ?
395 MLO_PAUSE_RX : MLO_PAUSE_TX;
396 } else {
397 new_pause = pl->link_config.pause & MLO_PAUSE_TXRX_MASK;
398 }
399
400 state->pause &= ~MLO_PAUSE_TXRX_MASK;
401 state->pause |= new_pause;
402}
403
404static const char *phylink_pause_to_str(int pause)
405{
406 switch (pause & MLO_PAUSE_TXRX_MASK) {
407 case MLO_PAUSE_TX | MLO_PAUSE_RX:
408 return "rx/tx";
409 case MLO_PAUSE_TX:
410 return "tx";
411 case MLO_PAUSE_RX:
412 return "rx";
413 default:
414 return "off";
415 }
416}
417
418static void phylink_resolve(struct work_struct *w)
419{
420 struct phylink *pl = container_of(w, struct phylink, resolve);
421 struct phylink_link_state link_state;
422 struct net_device *ndev = pl->netdev;
423
424 mutex_lock(&pl->state_mutex);
425 if (pl->phylink_disable_state) {
426 pl->mac_link_dropped = false;
427 link_state.link = false;
428 } else if (pl->mac_link_dropped) {
429 link_state.link = false;
430 } else {
431 switch (pl->link_an_mode) {
432 case MLO_AN_PHY:
433 link_state = pl->phy_state;
434 phylink_resolve_flow(pl, &link_state);
435 phylink_mac_config(pl, &link_state);
436 break;
437
438 case MLO_AN_FIXED:
439 phylink_get_fixed_state(pl, &link_state);
440 phylink_mac_config(pl, &link_state);
441 break;
442
443 case MLO_AN_INBAND:
444 phylink_get_mac_state(pl, &link_state);
445 if (pl->phydev) {
446 bool changed = false;
447
448 link_state.link = link_state.link &&
449 pl->phy_state.link;
450
451 if (pl->phy_state.interface !=
452 link_state.interface) {
453 link_state.interface = pl->phy_state.interface;
454 changed = true;
455 }
456
457 /* Propagate the flow control from the PHY
458 * to the MAC. Also propagate the interface
459 * if changed.
460 */
461 if (pl->phy_state.link || changed) {
462 link_state.pause |= pl->phy_state.pause;
463 phylink_resolve_flow(pl, &link_state);
464
465 phylink_mac_config(pl, &link_state);
466 }
467 }
468 break;
469 }
470 }
471
472 if (link_state.link != netif_carrier_ok(ndev)) {
473 if (!link_state.link) {
474 netif_carrier_off(ndev);
475 pl->ops->mac_link_down(ndev, pl->link_an_mode,
476 pl->phy_state.interface);
477 netdev_info(ndev, "Link is Down\n");
478 } else {
479 pl->ops->mac_link_up(ndev, pl->link_an_mode,
480 pl->phy_state.interface,
481 pl->phydev);
482
483 netif_carrier_on(ndev);
484
485 netdev_info(ndev,
486 "Link is Up - %s/%s - flow control %s\n",
487 phy_speed_to_str(link_state.speed),
488 phy_duplex_to_str(link_state.duplex),
489 phylink_pause_to_str(link_state.pause));
490 }
491 }
492 if (!link_state.link && pl->mac_link_dropped) {
493 pl->mac_link_dropped = false;
494 queue_work(system_power_efficient_wq, &pl->resolve);
495 }
496 mutex_unlock(&pl->state_mutex);
497}
498
499static void phylink_run_resolve(struct phylink *pl)
500{
501 if (!pl->phylink_disable_state)
502 queue_work(system_power_efficient_wq, &pl->resolve);
503}
504
505static void phylink_fixed_poll(struct timer_list *t)
506{
507 struct phylink *pl = container_of(t, struct phylink, link_poll);
508
509 mod_timer(t, jiffies + HZ);
510
511 phylink_run_resolve(pl);
512}
513
514static const struct sfp_upstream_ops sfp_phylink_ops;
515
516static int phylink_register_sfp(struct phylink *pl,
517 struct fwnode_handle *fwnode)
518{
519 struct fwnode_reference_args ref;
520 int ret;
521
522 if (!fwnode)
523 return 0;
524
525 ret = fwnode_property_get_reference_args(fwnode, "sfp", NULL,
526 0, 0, &ref);
527 if (ret < 0) {
528 if (ret == -ENOENT)
529 return 0;
530
531 netdev_err(pl->netdev, "unable to parse \"sfp\" node: %d\n",
532 ret);
533 return ret;
534 }
535
536 pl->sfp_bus = sfp_register_upstream(ref.fwnode, pl->netdev, pl,
537 &sfp_phylink_ops);
538 if (!pl->sfp_bus)
539 return -ENOMEM;
540
541 return 0;
542}
543
544/**
545 * phylink_create() - create a phylink instance
546 * @ndev: a pointer to the &struct net_device
547 * @fwnode: a pointer to a &struct fwnode_handle describing the network
548 * interface
549 * @iface: the desired link mode defined by &typedef phy_interface_t
550 * @ops: a pointer to a &struct phylink_mac_ops for the MAC.
551 *
552 * Create a new phylink instance, and parse the link parameters found in @np.
553 * This will parse in-band modes, fixed-link or SFP configuration.
554 *
555 * Returns a pointer to a &struct phylink, or an error-pointer value. Users
556 * must use IS_ERR() to check for errors from this function.
557 */
558struct phylink *phylink_create(struct net_device *ndev,
559 struct fwnode_handle *fwnode,
560 phy_interface_t iface,
561 const struct phylink_mac_ops *ops)
562{
563 struct phylink *pl;
564 int ret;
565
566 pl = kzalloc(sizeof(*pl), GFP_KERNEL);
567 if (!pl)
568 return ERR_PTR(-ENOMEM);
569
570 mutex_init(&pl->state_mutex);
571 INIT_WORK(&pl->resolve, phylink_resolve);
572 pl->netdev = ndev;
573 pl->phy_state.interface = iface;
574 pl->link_interface = iface;
575 if (iface == PHY_INTERFACE_MODE_MOCA)
576 pl->link_port = PORT_BNC;
577 else
578 pl->link_port = PORT_MII;
579 pl->link_config.interface = iface;
580 pl->link_config.pause = MLO_PAUSE_AN;
581 pl->link_config.speed = SPEED_UNKNOWN;
582 pl->link_config.duplex = DUPLEX_UNKNOWN;
583 pl->link_config.an_enabled = true;
584 pl->ops = ops;
585 __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
586 timer_setup(&pl->link_poll, phylink_fixed_poll, 0);
587
588 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
589 linkmode_copy(pl->link_config.advertising, pl->supported);
590 phylink_validate(pl, pl->supported, &pl->link_config);
591
592 ret = phylink_parse_mode(pl, fwnode);
593 if (ret < 0) {
594 kfree(pl);
595 return ERR_PTR(ret);
596 }
597
598 if (pl->link_an_mode == MLO_AN_FIXED) {
599 ret = phylink_parse_fixedlink(pl, fwnode);
600 if (ret < 0) {
601 kfree(pl);
602 return ERR_PTR(ret);
603 }
604 }
605
606 ret = phylink_register_sfp(pl, fwnode);
607 if (ret < 0) {
608 kfree(pl);
609 return ERR_PTR(ret);
610 }
611
612 return pl;
613}
614EXPORT_SYMBOL_GPL(phylink_create);
615
616/**
617 * phylink_destroy() - cleanup and destroy the phylink instance
618 * @pl: a pointer to a &struct phylink returned from phylink_create()
619 *
620 * Destroy a phylink instance. Any PHY that has been attached must have been
621 * cleaned up via phylink_disconnect_phy() prior to calling this function.
622 */
623void phylink_destroy(struct phylink *pl)
624{
625 if (pl->sfp_bus)
626 sfp_unregister_upstream(pl->sfp_bus);
627 if (!IS_ERR_OR_NULL(pl->link_gpio))
628 gpiod_put(pl->link_gpio);
629
630 cancel_work_sync(&pl->resolve);
631 kfree(pl);
632}
633EXPORT_SYMBOL_GPL(phylink_destroy);
634
635static void phylink_phy_change(struct phy_device *phydev, bool up,
636 bool do_carrier)
637{
638 struct phylink *pl = phydev->phylink;
639
640 mutex_lock(&pl->state_mutex);
641 pl->phy_state.speed = phydev->speed;
642 pl->phy_state.duplex = phydev->duplex;
643 pl->phy_state.pause = MLO_PAUSE_NONE;
644 if (phydev->pause)
645 pl->phy_state.pause |= MLO_PAUSE_SYM;
646 if (phydev->asym_pause)
647 pl->phy_state.pause |= MLO_PAUSE_ASYM;
648 pl->phy_state.interface = phydev->interface;
649 pl->phy_state.link = up;
650 mutex_unlock(&pl->state_mutex);
651
652 phylink_run_resolve(pl);
653
654 netdev_dbg(pl->netdev, "phy link %s %s/%s/%s\n", up ? "up" : "down",
655 phy_modes(phydev->interface),
656 phy_speed_to_str(phydev->speed),
657 phy_duplex_to_str(phydev->duplex));
658}
659
660static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy)
661{
662 struct phylink_link_state config;
663 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
664 u32 advertising;
665 int ret;
666
667 memset(&config, 0, sizeof(config));
668 ethtool_convert_legacy_u32_to_link_mode(supported, phy->supported);
669 ethtool_convert_legacy_u32_to_link_mode(config.advertising,
670 phy->advertising);
671 config.interface = pl->link_config.interface;
672
673 /*
674 * This is the new way of dealing with flow control for PHYs,
675 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
676 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
677 * using our validate call to the MAC, we rely upon the MAC
678 * clearing the bits from both supported and advertising fields.
679 */
680 if (phylink_test(supported, Pause))
681 phylink_set(config.advertising, Pause);
682 if (phylink_test(supported, Asym_Pause))
683 phylink_set(config.advertising, Asym_Pause);
684
685 ret = phylink_validate(pl, supported, &config);
686 if (ret)
687 return ret;
688
689 phy->phylink = pl;
690 phy->phy_link_change = phylink_phy_change;
691
692 netdev_info(pl->netdev,
693 "PHY [%s] driver [%s]\n", dev_name(&phy->mdio.dev),
694 phy->drv->name);
695
696 mutex_lock(&phy->lock);
697 mutex_lock(&pl->state_mutex);
698 pl->phydev = phy;
699 linkmode_copy(pl->supported, supported);
700 linkmode_copy(pl->link_config.advertising, config.advertising);
701
702 /* Restrict the phy advertisement according to the MAC support. */
703 ethtool_convert_link_mode_to_legacy_u32(&advertising, config.advertising);
704 phy->advertising = advertising;
705 mutex_unlock(&pl->state_mutex);
706 mutex_unlock(&phy->lock);
707
708 netdev_dbg(pl->netdev,
709 "phy: setting supported %*pb advertising 0x%08x\n",
710 __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
711 phy->advertising);
712
713 phy_start_machine(phy);
714 if (phy->irq > 0)
715 phy_start_interrupts(phy);
716
717 return 0;
718}
719
720static int __phylink_connect_phy(struct phylink *pl, struct phy_device *phy,
721 phy_interface_t interface)
722{
723 int ret;
724
725 if (WARN_ON(pl->link_an_mode == MLO_AN_FIXED ||
726 (pl->link_an_mode == MLO_AN_INBAND &&
727 phy_interface_mode_is_8023z(interface))))
728 return -EINVAL;
729
730 if (pl->phydev)
731 return -EBUSY;
732
733 ret = phy_attach_direct(pl->netdev, phy, 0, interface);
734 if (ret)
735 return ret;
736
737 ret = phylink_bringup_phy(pl, phy);
738 if (ret)
739 phy_detach(phy);
740
741 return ret;
742}
743
744/**
745 * phylink_connect_phy() - connect a PHY to the phylink instance
746 * @pl: a pointer to a &struct phylink returned from phylink_create()
747 * @phy: a pointer to a &struct phy_device.
748 *
749 * Connect @phy to the phylink instance specified by @pl by calling
750 * phy_attach_direct(). Configure the @phy according to the MAC driver's
751 * capabilities, start the PHYLIB state machine and enable any interrupts
752 * that the PHY supports.
753 *
754 * This updates the phylink's ethtool supported and advertising link mode
755 * masks.
756 *
757 * Returns 0 on success or a negative errno.
758 */
759int phylink_connect_phy(struct phylink *pl, struct phy_device *phy)
760{
761 /* Use PHY device/driver interface */
762 if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
763 pl->link_interface = phy->interface;
764 pl->link_config.interface = pl->link_interface;
765 }
766
767 return __phylink_connect_phy(pl, phy, pl->link_interface);
768}
769EXPORT_SYMBOL_GPL(phylink_connect_phy);
770
771/**
772 * phylink_of_phy_connect() - connect the PHY specified in the DT mode.
773 * @pl: a pointer to a &struct phylink returned from phylink_create()
774 * @dn: a pointer to a &struct device_node.
775 * @flags: PHY-specific flags to communicate to the PHY device driver
776 *
777 * Connect the phy specified in the device node @dn to the phylink instance
778 * specified by @pl. Actions specified in phylink_connect_phy() will be
779 * performed.
780 *
781 * Returns 0 on success or a negative errno.
782 */
783int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn,
784 u32 flags)
785{
786 struct device_node *phy_node;
787 struct phy_device *phy_dev;
788 int ret;
789
790 /* Fixed links and 802.3z are handled without needing a PHY */
791 if (pl->link_an_mode == MLO_AN_FIXED ||
792 (pl->link_an_mode == MLO_AN_INBAND &&
793 phy_interface_mode_is_8023z(pl->link_interface)))
794 return 0;
795
796 phy_node = of_parse_phandle(dn, "phy-handle", 0);
797 if (!phy_node)
798 phy_node = of_parse_phandle(dn, "phy", 0);
799 if (!phy_node)
800 phy_node = of_parse_phandle(dn, "phy-device", 0);
801
802 if (!phy_node) {
803 if (pl->link_an_mode == MLO_AN_PHY)
804 return -ENODEV;
805 return 0;
806 }
807
808 phy_dev = of_phy_attach(pl->netdev, phy_node, flags,
809 pl->link_interface);
810 /* We're done with the phy_node handle */
811 of_node_put(phy_node);
812
813 if (!phy_dev)
814 return -ENODEV;
815
816 ret = phylink_bringup_phy(pl, phy_dev);
817 if (ret)
818 phy_detach(phy_dev);
819
820 return ret;
821}
822EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
823
824/**
825 * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
826 * instance.
827 * @pl: a pointer to a &struct phylink returned from phylink_create()
828 *
829 * Disconnect any current PHY from the phylink instance described by @pl.
830 */
831void phylink_disconnect_phy(struct phylink *pl)
832{
833 struct phy_device *phy;
834
835 ASSERT_RTNL();
836
837 phy = pl->phydev;
838 if (phy) {
839 mutex_lock(&phy->lock);
840 mutex_lock(&pl->state_mutex);
841 pl->phydev = NULL;
842 mutex_unlock(&pl->state_mutex);
843 mutex_unlock(&phy->lock);
844 flush_work(&pl->resolve);
845
846 phy_disconnect(phy);
847 }
848}
849EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
850
851/**
852 * phylink_fixed_state_cb() - allow setting a fixed link callback
853 * @pl: a pointer to a &struct phylink returned from phylink_create()
854 * @cb: callback to execute to determine the fixed link state.
855 *
856 * The MAC driver should call this driver when the state of its link
857 * can be determined through e.g: an out of band MMIO register.
858 */
859int phylink_fixed_state_cb(struct phylink *pl,
860 void (*cb)(struct net_device *dev,
861 struct phylink_link_state *state))
862{
863 /* It does not make sense to let the link be overriden unless we use
864 * MLO_AN_FIXED
865 */
866 if (pl->link_an_mode != MLO_AN_FIXED)
867 return -EINVAL;
868
869 mutex_lock(&pl->state_mutex);
870 pl->get_fixed_state = cb;
871 mutex_unlock(&pl->state_mutex);
872
873 return 0;
874}
875EXPORT_SYMBOL_GPL(phylink_fixed_state_cb);
876
877/**
878 * phylink_mac_change() - notify phylink of a change in MAC state
879 * @pl: a pointer to a &struct phylink returned from phylink_create()
880 * @up: indicates whether the link is currently up.
881 *
882 * The MAC driver should call this driver when the state of its link
883 * changes (eg, link failure, new negotiation results, etc.)
884 */
885void phylink_mac_change(struct phylink *pl, bool up)
886{
887 if (!up)
888 pl->mac_link_dropped = true;
889 phylink_run_resolve(pl);
890 netdev_dbg(pl->netdev, "mac link %s\n", up ? "up" : "down");
891}
892EXPORT_SYMBOL_GPL(phylink_mac_change);
893
894/**
895 * phylink_start() - start a phylink instance
896 * @pl: a pointer to a &struct phylink returned from phylink_create()
897 *
898 * Start the phylink instance specified by @pl, configuring the MAC for the
899 * desired link mode(s) and negotiation style. This should be called from the
900 * network device driver's &struct net_device_ops ndo_open() method.
901 */
902void phylink_start(struct phylink *pl)
903{
904 ASSERT_RTNL();
905
906 netdev_info(pl->netdev, "configuring for %s/%s link mode\n",
907 phylink_an_mode_str(pl->link_an_mode),
908 phy_modes(pl->link_config.interface));
909
910 /* Always set the carrier off */
911 netif_carrier_off(pl->netdev);
912
913 /* Apply the link configuration to the MAC when starting. This allows
914 * a fixed-link to start with the correct parameters, and also
915 * ensures that we set the appropriate advertisement for Serdes links.
916 */
917 phylink_resolve_flow(pl, &pl->link_config);
918 phylink_mac_config(pl, &pl->link_config);
919
920 /* Restart autonegotiation if using 802.3z to ensure that the link
921 * parameters are properly negotiated. This is necessary for DSA
922 * switches using 802.3z negotiation to ensure they see our modes.
923 */
924 phylink_mac_an_restart(pl);
925
926 clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
927 phylink_run_resolve(pl);
928
929 if (pl->link_an_mode == MLO_AN_FIXED && !IS_ERR(pl->link_gpio))
930 mod_timer(&pl->link_poll, jiffies + HZ);
931 if (pl->sfp_bus)
932 sfp_upstream_start(pl->sfp_bus);
933 if (pl->phydev)
934 phy_start(pl->phydev);
935}
936EXPORT_SYMBOL_GPL(phylink_start);
937
938/**
939 * phylink_stop() - stop a phylink instance
940 * @pl: a pointer to a &struct phylink returned from phylink_create()
941 *
942 * Stop the phylink instance specified by @pl. This should be called from the
943 * network device driver's &struct net_device_ops ndo_stop() method. The
944 * network device's carrier state should not be changed prior to calling this
945 * function.
946 */
947void phylink_stop(struct phylink *pl)
948{
949 ASSERT_RTNL();
950
951 if (pl->phydev)
952 phy_stop(pl->phydev);
953 if (pl->sfp_bus)
954 sfp_upstream_stop(pl->sfp_bus);
955 if (pl->link_an_mode == MLO_AN_FIXED && !IS_ERR(pl->link_gpio))
956 del_timer_sync(&pl->link_poll);
957
958 set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
959 queue_work(system_power_efficient_wq, &pl->resolve);
960 flush_work(&pl->resolve);
961}
962EXPORT_SYMBOL_GPL(phylink_stop);
963
964/**
965 * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY
966 * @pl: a pointer to a &struct phylink returned from phylink_create()
967 * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters
968 *
969 * Read the wake on lan parameters from the PHY attached to the phylink
970 * instance specified by @pl. If no PHY is currently attached, report no
971 * support for wake on lan.
972 */
973void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
974{
975 ASSERT_RTNL();
976
977 wol->supported = 0;
978 wol->wolopts = 0;
979
980 if (pl->phydev)
981 phy_ethtool_get_wol(pl->phydev, wol);
982}
983EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol);
984
985/**
986 * phylink_ethtool_set_wol() - set wake on lan parameters
987 * @pl: a pointer to a &struct phylink returned from phylink_create()
988 * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters
989 *
990 * Set the wake on lan parameters for the PHY attached to the phylink
991 * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP
992 * error.
993 *
994 * Returns zero on success or negative errno code.
995 */
996int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
997{
998 int ret = -EOPNOTSUPP;
999
1000 ASSERT_RTNL();
1001
1002 if (pl->phydev)
1003 ret = phy_ethtool_set_wol(pl->phydev, wol);
1004
1005 return ret;
1006}
1007EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol);
1008
1009static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b)
1010{
1011 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
1012
1013 linkmode_zero(mask);
1014 phylink_set_port_modes(mask);
1015
1016 linkmode_and(dst, dst, mask);
1017 linkmode_or(dst, dst, b);
1018}
1019
1020static void phylink_get_ksettings(const struct phylink_link_state *state,
1021 struct ethtool_link_ksettings *kset)
1022{
1023 phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
1024 linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
1025 kset->base.speed = state->speed;
1026 kset->base.duplex = state->duplex;
1027 kset->base.autoneg = state->an_enabled ? AUTONEG_ENABLE :
1028 AUTONEG_DISABLE;
1029}
1030
1031/**
1032 * phylink_ethtool_ksettings_get() - get the current link settings
1033 * @pl: a pointer to a &struct phylink returned from phylink_create()
1034 * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings
1035 *
1036 * Read the current link settings for the phylink instance specified by @pl.
1037 * This will be the link settings read from the MAC, PHY or fixed link
1038 * settings depending on the current negotiation mode.
1039 */
1040int phylink_ethtool_ksettings_get(struct phylink *pl,
1041 struct ethtool_link_ksettings *kset)
1042{
1043 struct phylink_link_state link_state;
1044
1045 ASSERT_RTNL();
1046
1047 if (pl->phydev) {
1048 phy_ethtool_ksettings_get(pl->phydev, kset);
1049 } else {
1050 kset->base.port = pl->link_port;
1051 }
1052
1053 linkmode_copy(kset->link_modes.supported, pl->supported);
1054
1055 switch (pl->link_an_mode) {
1056 case MLO_AN_FIXED:
1057 /* We are using fixed settings. Report these as the
1058 * current link settings - and note that these also
1059 * represent the supported speeds/duplex/pause modes.
1060 */
1061 phylink_get_fixed_state(pl, &link_state);
1062 phylink_get_ksettings(&link_state, kset);
1063 break;
1064
1065 case MLO_AN_INBAND:
1066 /* If there is a phy attached, then use the reported
1067 * settings from the phy with no modification.
1068 */
1069 if (pl->phydev)
1070 break;
1071
1072 phylink_get_mac_state(pl, &link_state);
1073
1074 /* The MAC is reporting the link results from its own PCS
1075 * layer via in-band status. Report these as the current
1076 * link settings.
1077 */
1078 phylink_get_ksettings(&link_state, kset);
1079 break;
1080 }
1081
1082 return 0;
1083}
1084EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get);
1085
1086/**
1087 * phylink_ethtool_ksettings_set() - set the link settings
1088 * @pl: a pointer to a &struct phylink returned from phylink_create()
1089 * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes
1090 */
1091int phylink_ethtool_ksettings_set(struct phylink *pl,
1092 const struct ethtool_link_ksettings *kset)
1093{
1094 struct ethtool_link_ksettings our_kset;
1095 struct phylink_link_state config;
1096 int ret;
1097
1098 ASSERT_RTNL();
1099
1100 if (kset->base.autoneg != AUTONEG_DISABLE &&
1101 kset->base.autoneg != AUTONEG_ENABLE)
1102 return -EINVAL;
1103
1104 config = pl->link_config;
1105
1106 /* Mask out unsupported advertisements */
1107 linkmode_and(config.advertising, kset->link_modes.advertising,
1108 pl->supported);
1109
1110 /* FIXME: should we reject autoneg if phy/mac does not support it? */
1111 if (kset->base.autoneg == AUTONEG_DISABLE) {
1112 const struct phy_setting *s;
1113
1114 /* Autonegotiation disabled, select a suitable speed and
1115 * duplex.
1116 */
1117 s = phy_lookup_setting(kset->base.speed, kset->base.duplex,
1118 pl->supported,
1119 __ETHTOOL_LINK_MODE_MASK_NBITS, false);
1120 if (!s)
1121 return -EINVAL;
1122
1123 /* If we have a fixed link (as specified by firmware), refuse
1124 * to change link parameters.
1125 */
1126 if (pl->link_an_mode == MLO_AN_FIXED &&
1127 (s->speed != pl->link_config.speed ||
1128 s->duplex != pl->link_config.duplex))
1129 return -EINVAL;
1130
1131 config.speed = s->speed;
1132 config.duplex = s->duplex;
1133 config.an_enabled = false;
1134
1135 __clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
1136 } else {
1137 /* If we have a fixed link, refuse to enable autonegotiation */
1138 if (pl->link_an_mode == MLO_AN_FIXED)
1139 return -EINVAL;
1140
1141 config.speed = SPEED_UNKNOWN;
1142 config.duplex = DUPLEX_UNKNOWN;
1143 config.an_enabled = true;
1144
1145 __set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
1146 }
1147
1148 if (phylink_validate(pl, pl->supported, &config))
1149 return -EINVAL;
1150
1151 /* If autonegotiation is enabled, we must have an advertisement */
1152 if (config.an_enabled && phylink_is_empty_linkmode(config.advertising))
1153 return -EINVAL;
1154
1155 our_kset = *kset;
1156 linkmode_copy(our_kset.link_modes.advertising, config.advertising);
1157 our_kset.base.speed = config.speed;
1158 our_kset.base.duplex = config.duplex;
1159
1160 /* If we have a PHY, configure the phy */
1161 if (pl->phydev) {
1162 ret = phy_ethtool_ksettings_set(pl->phydev, &our_kset);
1163 if (ret)
1164 return ret;
1165 }
1166
1167 mutex_lock(&pl->state_mutex);
1168 /* Configure the MAC to match the new settings */
1169 linkmode_copy(pl->link_config.advertising, our_kset.link_modes.advertising);
1170 pl->link_config.interface = config.interface;
1171 pl->link_config.speed = our_kset.base.speed;
1172 pl->link_config.duplex = our_kset.base.duplex;
1173 pl->link_config.an_enabled = our_kset.base.autoneg != AUTONEG_DISABLE;
1174
1175 if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
1176 phylink_mac_config(pl, &pl->link_config);
1177 phylink_mac_an_restart(pl);
1178 }
1179 mutex_unlock(&pl->state_mutex);
1180
1181 return 0;
1182}
1183EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
1184
1185/**
1186 * phylink_ethtool_nway_reset() - restart negotiation
1187 * @pl: a pointer to a &struct phylink returned from phylink_create()
1188 *
1189 * Restart negotiation for the phylink instance specified by @pl. This will
1190 * cause any attached phy to restart negotiation with the link partner, and
1191 * if the MAC is in a BaseX mode, the MAC will also be requested to restart
1192 * negotiation.
1193 *
1194 * Returns zero on success, or negative error code.
1195 */
1196int phylink_ethtool_nway_reset(struct phylink *pl)
1197{
1198 int ret = 0;
1199
1200 ASSERT_RTNL();
1201
1202 if (pl->phydev)
1203 ret = phy_restart_aneg(pl->phydev);
1204 phylink_mac_an_restart(pl);
1205
1206 return ret;
1207}
1208EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
1209
1210/**
1211 * phylink_ethtool_get_pauseparam() - get the current pause parameters
1212 * @pl: a pointer to a &struct phylink returned from phylink_create()
1213 * @pause: a pointer to a &struct ethtool_pauseparam
1214 */
1215void phylink_ethtool_get_pauseparam(struct phylink *pl,
1216 struct ethtool_pauseparam *pause)
1217{
1218 ASSERT_RTNL();
1219
1220 pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
1221 pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
1222 pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
1223}
1224EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
1225
1226/**
1227 * phylink_ethtool_set_pauseparam() - set the current pause parameters
1228 * @pl: a pointer to a &struct phylink returned from phylink_create()
1229 * @pause: a pointer to a &struct ethtool_pauseparam
1230 */
1231int phylink_ethtool_set_pauseparam(struct phylink *pl,
1232 struct ethtool_pauseparam *pause)
1233{
1234 struct phylink_link_state *config = &pl->link_config;
1235
1236 ASSERT_RTNL();
1237
1238 if (!phylink_test(pl->supported, Pause) &&
1239 !phylink_test(pl->supported, Asym_Pause))
1240 return -EOPNOTSUPP;
1241
1242 if (!phylink_test(pl->supported, Asym_Pause) &&
1243 !pause->autoneg && pause->rx_pause != pause->tx_pause)
1244 return -EINVAL;
1245
1246 config->pause &= ~(MLO_PAUSE_AN | MLO_PAUSE_TXRX_MASK);
1247
1248 if (pause->autoneg)
1249 config->pause |= MLO_PAUSE_AN;
1250 if (pause->rx_pause)
1251 config->pause |= MLO_PAUSE_RX;
1252 if (pause->tx_pause)
1253 config->pause |= MLO_PAUSE_TX;
1254
1255 if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
1256 switch (pl->link_an_mode) {
1257 case MLO_AN_PHY:
1258 /* Silently mark the carrier down, and then trigger a resolve */
1259 netif_carrier_off(pl->netdev);
1260 phylink_run_resolve(pl);
1261 break;
1262
1263 case MLO_AN_FIXED:
1264 /* Should we allow fixed links to change against the config? */
1265 phylink_resolve_flow(pl, config);
1266 phylink_mac_config(pl, config);
1267 break;
1268
1269 case MLO_AN_INBAND:
1270 phylink_mac_config(pl, config);
1271 phylink_mac_an_restart(pl);
1272 break;
1273 }
1274 }
1275
1276 return 0;
1277}
1278EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
1279
1280/**
1281 * phylink_ethtool_get_eee_err() - read the energy efficient ethernet error
1282 * counter
1283 * @pl: a pointer to a &struct phylink returned from phylink_create().
1284 *
1285 * Read the Energy Efficient Ethernet error counter from the PHY associated
1286 * with the phylink instance specified by @pl.
1287 *
1288 * Returns positive error counter value, or negative error code.
1289 */
1290int phylink_get_eee_err(struct phylink *pl)
1291{
1292 int ret = 0;
1293
1294 ASSERT_RTNL();
1295
1296 if (pl->phydev)
1297 ret = phy_get_eee_err(pl->phydev);
1298
1299 return ret;
1300}
1301EXPORT_SYMBOL_GPL(phylink_get_eee_err);
1302
1303/**
1304 * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters
1305 * @pl: a pointer to a &struct phylink returned from phylink_create()
1306 * @eee: a pointer to a &struct ethtool_eee for the read parameters
1307 */
1308int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee)
1309{
1310 int ret = -EOPNOTSUPP;
1311
1312 ASSERT_RTNL();
1313
1314 if (pl->phydev)
1315 ret = phy_ethtool_get_eee(pl->phydev, eee);
1316
1317 return ret;
1318}
1319EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
1320
1321/**
1322 * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters
1323 * @pl: a pointer to a &struct phylink returned from phylink_create()
1324 * @eee: a pointer to a &struct ethtool_eee for the desired parameters
1325 */
1326int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_eee *eee)
1327{
1328 int ret = -EOPNOTSUPP;
1329
1330 ASSERT_RTNL();
1331
1332 if (pl->phydev)
1333 ret = phy_ethtool_set_eee(pl->phydev, eee);
1334
1335 return ret;
1336}
1337EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
1338
1339/* This emulates MII registers for a fixed-mode phy operating as per the
1340 * passed in state. "aneg" defines if we report negotiation is possible.
1341 *
1342 * FIXME: should deal with negotiation state too.
1343 */
1344static int phylink_mii_emul_read(struct net_device *ndev, unsigned int reg,
1345 struct phylink_link_state *state, bool aneg)
1346{
1347 struct fixed_phy_status fs;
1348 int val;
1349
1350 fs.link = state->link;
1351 fs.speed = state->speed;
1352 fs.duplex = state->duplex;
1353 fs.pause = state->pause & MLO_PAUSE_SYM;
1354 fs.asym_pause = state->pause & MLO_PAUSE_ASYM;
1355
1356 val = swphy_read_reg(reg, &fs);
1357 if (reg == MII_BMSR) {
1358 if (!state->an_complete)
1359 val &= ~BMSR_ANEGCOMPLETE;
1360 if (!aneg)
1361 val &= ~BMSR_ANEGCAPABLE;
1362 }
1363 return val;
1364}
1365
1366static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
1367 unsigned int reg)
1368{
1369 struct phy_device *phydev = pl->phydev;
1370 int prtad, devad;
1371
1372 if (mdio_phy_id_is_c45(phy_id)) {
1373 prtad = mdio_phy_id_prtad(phy_id);
1374 devad = mdio_phy_id_devad(phy_id);
1375 devad = MII_ADDR_C45 | devad << 16 | reg;
1376 } else if (phydev->is_c45) {
1377 switch (reg) {
1378 case MII_BMCR:
1379 case MII_BMSR:
1380 case MII_PHYSID1:
1381 case MII_PHYSID2:
1382 devad = __ffs(phydev->c45_ids.devices_in_package);
1383 break;
1384 case MII_ADVERTISE:
1385 case MII_LPA:
1386 if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1387 return -EINVAL;
1388 devad = MDIO_MMD_AN;
1389 if (reg == MII_ADVERTISE)
1390 reg = MDIO_AN_ADVERTISE;
1391 else
1392 reg = MDIO_AN_LPA;
1393 break;
1394 default:
1395 return -EINVAL;
1396 }
1397 prtad = phy_id;
1398 devad = MII_ADDR_C45 | devad << 16 | reg;
1399 } else {
1400 prtad = phy_id;
1401 devad = reg;
1402 }
1403 return mdiobus_read(pl->phydev->mdio.bus, prtad, devad);
1404}
1405
1406static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
1407 unsigned int reg, unsigned int val)
1408{
1409 struct phy_device *phydev = pl->phydev;
1410 int prtad, devad;
1411
1412 if (mdio_phy_id_is_c45(phy_id)) {
1413 prtad = mdio_phy_id_prtad(phy_id);
1414 devad = mdio_phy_id_devad(phy_id);
1415 devad = MII_ADDR_C45 | devad << 16 | reg;
1416 } else if (phydev->is_c45) {
1417 switch (reg) {
1418 case MII_BMCR:
1419 case MII_BMSR:
1420 case MII_PHYSID1:
1421 case MII_PHYSID2:
1422 devad = __ffs(phydev->c45_ids.devices_in_package);
1423 break;
1424 case MII_ADVERTISE:
1425 case MII_LPA:
1426 if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1427 return -EINVAL;
1428 devad = MDIO_MMD_AN;
1429 if (reg == MII_ADVERTISE)
1430 reg = MDIO_AN_ADVERTISE;
1431 else
1432 reg = MDIO_AN_LPA;
1433 break;
1434 default:
1435 return -EINVAL;
1436 }
1437 prtad = phy_id;
1438 devad = MII_ADDR_C45 | devad << 16 | reg;
1439 } else {
1440 prtad = phy_id;
1441 devad = reg;
1442 }
1443
1444 return mdiobus_write(phydev->mdio.bus, prtad, devad, val);
1445}
1446
1447static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
1448 unsigned int reg)
1449{
1450 struct phylink_link_state state;
1451 int val = 0xffff;
1452
1453 switch (pl->link_an_mode) {
1454 case MLO_AN_FIXED:
1455 if (phy_id == 0) {
1456 phylink_get_fixed_state(pl, &state);
1457 val = phylink_mii_emul_read(pl->netdev, reg, &state,
1458 true);
1459 }
1460 break;
1461
1462 case MLO_AN_PHY:
1463 return -EOPNOTSUPP;
1464
1465 case MLO_AN_INBAND:
1466 if (phy_id == 0) {
1467 val = phylink_get_mac_state(pl, &state);
1468 if (val < 0)
1469 return val;
1470
1471 val = phylink_mii_emul_read(pl->netdev, reg, &state,
1472 true);
1473 }
1474 break;
1475 }
1476
1477 return val & 0xffff;
1478}
1479
1480static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
1481 unsigned int reg, unsigned int val)
1482{
1483 switch (pl->link_an_mode) {
1484 case MLO_AN_FIXED:
1485 break;
1486
1487 case MLO_AN_PHY:
1488 return -EOPNOTSUPP;
1489
1490 case MLO_AN_INBAND:
1491 break;
1492 }
1493
1494 return 0;
1495}
1496
1497/**
1498 * phylink_mii_ioctl() - generic mii ioctl interface
1499 * @pl: a pointer to a &struct phylink returned from phylink_create()
1500 * @ifr: a pointer to a &struct ifreq for socket ioctls
1501 * @cmd: ioctl cmd to execute
1502 *
1503 * Perform the specified MII ioctl on the PHY attached to the phylink instance
1504 * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
1505 *
1506 * Returns: zero on success or negative error code.
1507 *
1508 * %SIOCGMIIPHY:
1509 * read register from the current PHY.
1510 * %SIOCGMIIREG:
1511 * read register from the specified PHY.
1512 * %SIOCSMIIREG:
1513 * set a register on the specified PHY.
1514 */
1515int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
1516{
1517 struct mii_ioctl_data *mii = if_mii(ifr);
1518 int ret;
1519
1520 ASSERT_RTNL();
1521
1522 if (pl->phydev) {
1523 /* PHYs only exist for MLO_AN_PHY and SGMII */
1524 switch (cmd) {
1525 case SIOCGMIIPHY:
1526 mii->phy_id = pl->phydev->mdio.addr;
1527 /* fall through */
1528
1529 case SIOCGMIIREG:
1530 ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
1531 if (ret >= 0) {
1532 mii->val_out = ret;
1533 ret = 0;
1534 }
1535 break;
1536
1537 case SIOCSMIIREG:
1538 ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
1539 mii->val_in);
1540 break;
1541
1542 default:
1543 ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
1544 break;
1545 }
1546 } else {
1547 switch (cmd) {
1548 case SIOCGMIIPHY:
1549 mii->phy_id = 0;
1550 /* fall through */
1551
1552 case SIOCGMIIREG:
1553 ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
1554 if (ret >= 0) {
1555 mii->val_out = ret;
1556 ret = 0;
1557 }
1558 break;
1559
1560 case SIOCSMIIREG:
1561 ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
1562 mii->val_in);
1563 break;
1564
1565 default:
1566 ret = -EOPNOTSUPP;
1567 break;
1568 }
1569 }
1570
1571 return ret;
1572}
1573EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
1574
1575static int phylink_sfp_module_insert(void *upstream,
1576 const struct sfp_eeprom_id *id)
1577{
1578 struct phylink *pl = upstream;
1579 __ETHTOOL_DECLARE_LINK_MODE_MASK(support) = { 0, };
1580 struct phylink_link_state config;
1581 phy_interface_t iface;
1582 int ret = 0;
1583 bool changed;
1584 u8 port;
1585
1586 ASSERT_RTNL();
1587
1588 sfp_parse_support(pl->sfp_bus, id, support);
1589 port = sfp_parse_port(pl->sfp_bus, id, support);
1590
1591 memset(&config, 0, sizeof(config));
1592 linkmode_copy(config.advertising, support);
1593 config.interface = PHY_INTERFACE_MODE_NA;
1594 config.speed = SPEED_UNKNOWN;
1595 config.duplex = DUPLEX_UNKNOWN;
1596 config.pause = MLO_PAUSE_AN;
1597 config.an_enabled = pl->link_config.an_enabled;
1598
1599 /* Ignore errors if we're expecting a PHY to attach later */
1600 ret = phylink_validate(pl, support, &config);
1601 if (ret) {
1602 netdev_err(pl->netdev, "validation with support %*pb failed: %d\n",
1603 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
1604 return ret;
1605 }
1606
1607 iface = sfp_select_interface(pl->sfp_bus, id, config.advertising);
1608 if (iface == PHY_INTERFACE_MODE_NA) {
1609 netdev_err(pl->netdev,
1610 "selection of interface failed, advertisement %*pb\n",
1611 __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising);
1612 return -EINVAL;
1613 }
1614
1615 config.interface = iface;
1616 ret = phylink_validate(pl, support, &config);
1617 if (ret) {
1618 netdev_err(pl->netdev, "validation of %s/%s with support %*pb failed: %d\n",
1619 phylink_an_mode_str(MLO_AN_INBAND),
1620 phy_modes(config.interface),
1621 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
1622 return ret;
1623 }
1624
1625 netdev_dbg(pl->netdev, "requesting link mode %s/%s with support %*pb\n",
1626 phylink_an_mode_str(MLO_AN_INBAND),
1627 phy_modes(config.interface),
1628 __ETHTOOL_LINK_MODE_MASK_NBITS, support);
1629
1630 if (phy_interface_mode_is_8023z(iface) && pl->phydev)
1631 return -EINVAL;
1632
1633 changed = !bitmap_equal(pl->supported, support,
1634 __ETHTOOL_LINK_MODE_MASK_NBITS);
1635 if (changed) {
1636 linkmode_copy(pl->supported, support);
1637 linkmode_copy(pl->link_config.advertising, config.advertising);
1638 }
1639
1640 if (pl->link_an_mode != MLO_AN_INBAND ||
1641 pl->link_config.interface != config.interface) {
1642 pl->link_config.interface = config.interface;
1643 pl->link_an_mode = MLO_AN_INBAND;
1644
1645 changed = true;
1646
1647 netdev_info(pl->netdev, "switched to %s/%s link mode\n",
1648 phylink_an_mode_str(MLO_AN_INBAND),
1649 phy_modes(config.interface));
1650 }
1651
1652 pl->link_port = port;
1653
1654 if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
1655 &pl->phylink_disable_state))
1656 phylink_mac_config(pl, &pl->link_config);
1657
1658 return ret;
1659}
1660
1661static void phylink_sfp_link_down(void *upstream)
1662{
1663 struct phylink *pl = upstream;
1664
1665 ASSERT_RTNL();
1666
1667 set_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
1668 queue_work(system_power_efficient_wq, &pl->resolve);
1669 flush_work(&pl->resolve);
1670}
1671
1672static void phylink_sfp_link_up(void *upstream)
1673{
1674 struct phylink *pl = upstream;
1675
1676 ASSERT_RTNL();
1677
1678 clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
1679 phylink_run_resolve(pl);
1680}
1681
1682static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
1683{
1684 struct phylink *pl = upstream;
1685
1686 return __phylink_connect_phy(upstream, phy, pl->link_config.interface);
1687}
1688
1689static void phylink_sfp_disconnect_phy(void *upstream)
1690{
1691 phylink_disconnect_phy(upstream);
1692}
1693
1694static const struct sfp_upstream_ops sfp_phylink_ops = {
1695 .module_insert = phylink_sfp_module_insert,
1696 .link_up = phylink_sfp_link_up,
1697 .link_down = phylink_sfp_link_down,
1698 .connect_phy = phylink_sfp_connect_phy,
1699 .disconnect_phy = phylink_sfp_disconnect_phy,
1700};
1701
1702/* Helpers for MAC drivers */
1703
1704/**
1705 * phylink_helper_basex_speed() - 1000BaseX/2500BaseX helper
1706 * @state: a pointer to a &struct phylink_link_state
1707 *
1708 * Inspect the interface mode, advertising mask or forced speed and
1709 * decide whether to run at 2.5Gbit or 1Gbit appropriately, switching
1710 * the interface mode to suit. @state->interface is appropriately
1711 * updated, and the advertising mask has the "other" baseX_Full flag
1712 * cleared.
1713 */
1714void phylink_helper_basex_speed(struct phylink_link_state *state)
1715{
1716 if (phy_interface_mode_is_8023z(state->interface)) {
1717 bool want_2500 = state->an_enabled ?
1718 phylink_test(state->advertising, 2500baseX_Full) :
1719 state->speed == SPEED_2500;
1720
1721 if (want_2500) {
1722 phylink_clear(state->advertising, 1000baseX_Full);
1723 state->interface = PHY_INTERFACE_MODE_2500BASEX;
1724 } else {
1725 phylink_clear(state->advertising, 2500baseX_Full);
1726 state->interface = PHY_INTERFACE_MODE_1000BASEX;
1727 }
1728 }
1729}
1730EXPORT_SYMBOL_GPL(phylink_helper_basex_speed);
1731
1732MODULE_LICENSE("GPL");