Linux 4.16.11
[linux/fpc-iii.git] / drivers / net / phy / phylink.c
blob6ac8b29b2dc3c5fcf630a8e54fc4897ae330d24b
1 /*
2 * phylink models the MAC to optional PHY connection, supporting
3 * technologies such as SFP cages where the PHY is hot-pluggable.
5 * Copyright (C) 2015 Russell King
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.
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>
24 #include "sfp.h"
25 #include "swphy.h"
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)
34 enum {
35 PHYLINK_DISABLE_STOPPED,
36 PHYLINK_DISABLE_LINK,
39 /**
40 * struct phylink - internal data type for phylink
42 struct phylink {
43 /* private: */
44 struct net_device *netdev;
45 const struct phylink_mac_ops *ops;
47 unsigned long phylink_disable_state; /* bitmask of disables */
48 struct phy_device *phydev;
49 phy_interface_t link_interface; /* PHY_INTERFACE_xxx */
50 u8 link_an_mode; /* MLO_AN_xxx */
51 u8 link_port; /* The current non-phy ethtool port */
52 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
54 /* The link configuration settings */
55 struct phylink_link_state link_config;
56 struct gpio_desc *link_gpio;
57 void (*get_fixed_state)(struct net_device *dev,
58 struct phylink_link_state *s);
60 struct mutex state_mutex;
61 struct phylink_link_state phy_state;
62 struct work_struct resolve;
64 bool mac_link_dropped;
66 struct sfp_bus *sfp_bus;
69 static inline void linkmode_zero(unsigned long *dst)
71 bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS);
74 static inline void linkmode_copy(unsigned long *dst, const unsigned long *src)
76 bitmap_copy(dst, src, __ETHTOOL_LINK_MODE_MASK_NBITS);
79 static inline void linkmode_and(unsigned long *dst, const unsigned long *a,
80 const unsigned long *b)
82 bitmap_and(dst, a, b, __ETHTOOL_LINK_MODE_MASK_NBITS);
85 static inline void linkmode_or(unsigned long *dst, const unsigned long *a,
86 const unsigned long *b)
88 bitmap_or(dst, a, b, __ETHTOOL_LINK_MODE_MASK_NBITS);
91 static inline bool linkmode_empty(const unsigned long *src)
93 return bitmap_empty(src, __ETHTOOL_LINK_MODE_MASK_NBITS);
96 /**
97 * phylink_set_port_modes() - set the port type modes in the ethtool mask
98 * @mask: ethtool link mode mask
100 * Sets all the port type modes in the ethtool mask. MAC drivers should
101 * use this in their 'validate' callback.
103 void phylink_set_port_modes(unsigned long *mask)
105 phylink_set(mask, TP);
106 phylink_set(mask, AUI);
107 phylink_set(mask, MII);
108 phylink_set(mask, FIBRE);
109 phylink_set(mask, BNC);
110 phylink_set(mask, Backplane);
112 EXPORT_SYMBOL_GPL(phylink_set_port_modes);
114 static int phylink_is_empty_linkmode(const unsigned long *linkmode)
116 __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp) = { 0, };
118 phylink_set_port_modes(tmp);
119 phylink_set(tmp, Autoneg);
120 phylink_set(tmp, Pause);
121 phylink_set(tmp, Asym_Pause);
123 bitmap_andnot(tmp, linkmode, tmp, __ETHTOOL_LINK_MODE_MASK_NBITS);
125 return linkmode_empty(tmp);
128 static const char *phylink_an_mode_str(unsigned int mode)
130 static const char *modestr[] = {
131 [MLO_AN_PHY] = "phy",
132 [MLO_AN_FIXED] = "fixed",
133 [MLO_AN_INBAND] = "inband",
136 return mode < ARRAY_SIZE(modestr) ? modestr[mode] : "unknown";
139 static int phylink_validate(struct phylink *pl, unsigned long *supported,
140 struct phylink_link_state *state)
142 pl->ops->validate(pl->netdev, supported, state);
144 return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
147 static int phylink_parse_fixedlink(struct phylink *pl,
148 struct fwnode_handle *fwnode)
150 struct fwnode_handle *fixed_node;
151 const struct phy_setting *s;
152 struct gpio_desc *desc;
153 u32 speed;
154 int ret;
156 fixed_node = fwnode_get_named_child_node(fwnode, "fixed-link");
157 if (fixed_node) {
158 ret = fwnode_property_read_u32(fixed_node, "speed", &speed);
160 pl->link_config.speed = speed;
161 pl->link_config.duplex = DUPLEX_HALF;
163 if (fwnode_property_read_bool(fixed_node, "full-duplex"))
164 pl->link_config.duplex = DUPLEX_FULL;
166 /* We treat the "pause" and "asym-pause" terminology as
167 * defining the link partner's ability. */
168 if (fwnode_property_read_bool(fixed_node, "pause"))
169 pl->link_config.pause |= MLO_PAUSE_SYM;
170 if (fwnode_property_read_bool(fixed_node, "asym-pause"))
171 pl->link_config.pause |= MLO_PAUSE_ASYM;
173 if (ret == 0) {
174 desc = fwnode_get_named_gpiod(fixed_node, "link-gpios",
175 0, GPIOD_IN, "?");
177 if (!IS_ERR(desc))
178 pl->link_gpio = desc;
179 else if (desc == ERR_PTR(-EPROBE_DEFER))
180 ret = -EPROBE_DEFER;
182 fwnode_handle_put(fixed_node);
184 if (ret)
185 return ret;
186 } else {
187 u32 prop[5];
189 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
190 NULL, 0);
191 if (ret != ARRAY_SIZE(prop)) {
192 netdev_err(pl->netdev, "broken fixed-link?\n");
193 return -EINVAL;
196 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
197 prop, ARRAY_SIZE(prop));
198 if (!ret) {
199 pl->link_config.duplex = prop[1] ?
200 DUPLEX_FULL : DUPLEX_HALF;
201 pl->link_config.speed = prop[2];
202 if (prop[3])
203 pl->link_config.pause |= MLO_PAUSE_SYM;
204 if (prop[4])
205 pl->link_config.pause |= MLO_PAUSE_ASYM;
209 if (pl->link_config.speed > SPEED_1000 &&
210 pl->link_config.duplex != DUPLEX_FULL)
211 netdev_warn(pl->netdev, "fixed link specifies half duplex for %dMbps link?\n",
212 pl->link_config.speed);
214 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
215 linkmode_copy(pl->link_config.advertising, pl->supported);
216 phylink_validate(pl, pl->supported, &pl->link_config);
218 s = phy_lookup_setting(pl->link_config.speed, pl->link_config.duplex,
219 pl->supported,
220 __ETHTOOL_LINK_MODE_MASK_NBITS, true);
221 linkmode_zero(pl->supported);
222 phylink_set(pl->supported, MII);
223 if (s) {
224 __set_bit(s->bit, pl->supported);
225 } else {
226 netdev_warn(pl->netdev, "fixed link %s duplex %dMbps not recognised\n",
227 pl->link_config.duplex == DUPLEX_FULL ? "full" : "half",
228 pl->link_config.speed);
231 linkmode_and(pl->link_config.advertising, pl->link_config.advertising,
232 pl->supported);
234 pl->link_config.link = 1;
235 pl->link_config.an_complete = 1;
237 return 0;
240 static int phylink_parse_mode(struct phylink *pl, struct fwnode_handle *fwnode)
242 struct fwnode_handle *dn;
243 const char *managed;
245 dn = fwnode_get_named_child_node(fwnode, "fixed-link");
246 if (dn || fwnode_property_present(fwnode, "fixed-link"))
247 pl->link_an_mode = MLO_AN_FIXED;
248 fwnode_handle_put(dn);
250 if (fwnode_property_read_string(fwnode, "managed", &managed) == 0 &&
251 strcmp(managed, "in-band-status") == 0) {
252 if (pl->link_an_mode == MLO_AN_FIXED) {
253 netdev_err(pl->netdev,
254 "can't use both fixed-link and in-band-status\n");
255 return -EINVAL;
258 linkmode_zero(pl->supported);
259 phylink_set(pl->supported, MII);
260 phylink_set(pl->supported, Autoneg);
261 phylink_set(pl->supported, Asym_Pause);
262 phylink_set(pl->supported, Pause);
263 pl->link_config.an_enabled = true;
264 pl->link_an_mode = MLO_AN_INBAND;
266 switch (pl->link_config.interface) {
267 case PHY_INTERFACE_MODE_SGMII:
268 phylink_set(pl->supported, 10baseT_Half);
269 phylink_set(pl->supported, 10baseT_Full);
270 phylink_set(pl->supported, 100baseT_Half);
271 phylink_set(pl->supported, 100baseT_Full);
272 phylink_set(pl->supported, 1000baseT_Half);
273 phylink_set(pl->supported, 1000baseT_Full);
274 break;
276 case PHY_INTERFACE_MODE_1000BASEX:
277 phylink_set(pl->supported, 1000baseX_Full);
278 break;
280 case PHY_INTERFACE_MODE_2500BASEX:
281 phylink_set(pl->supported, 2500baseX_Full);
282 break;
284 case PHY_INTERFACE_MODE_10GKR:
285 phylink_set(pl->supported, 10baseT_Half);
286 phylink_set(pl->supported, 10baseT_Full);
287 phylink_set(pl->supported, 100baseT_Half);
288 phylink_set(pl->supported, 100baseT_Full);
289 phylink_set(pl->supported, 1000baseT_Half);
290 phylink_set(pl->supported, 1000baseT_Full);
291 phylink_set(pl->supported, 1000baseX_Full);
292 phylink_set(pl->supported, 10000baseKR_Full);
293 phylink_set(pl->supported, 10000baseCR_Full);
294 phylink_set(pl->supported, 10000baseSR_Full);
295 phylink_set(pl->supported, 10000baseLR_Full);
296 phylink_set(pl->supported, 10000baseLRM_Full);
297 phylink_set(pl->supported, 10000baseER_Full);
298 break;
300 default:
301 netdev_err(pl->netdev,
302 "incorrect link mode %s for in-band status\n",
303 phy_modes(pl->link_config.interface));
304 return -EINVAL;
307 linkmode_copy(pl->link_config.advertising, pl->supported);
309 if (phylink_validate(pl, pl->supported, &pl->link_config)) {
310 netdev_err(pl->netdev,
311 "failed to validate link configuration for in-band status\n");
312 return -EINVAL;
316 return 0;
319 static void phylink_mac_config(struct phylink *pl,
320 const struct phylink_link_state *state)
322 netdev_dbg(pl->netdev,
323 "%s: mode=%s/%s/%s/%s adv=%*pb pause=%02x link=%u an=%u\n",
324 __func__, phylink_an_mode_str(pl->link_an_mode),
325 phy_modes(state->interface),
326 phy_speed_to_str(state->speed),
327 phy_duplex_to_str(state->duplex),
328 __ETHTOOL_LINK_MODE_MASK_NBITS, state->advertising,
329 state->pause, state->link, state->an_enabled);
331 pl->ops->mac_config(pl->netdev, pl->link_an_mode, state);
334 static void phylink_mac_an_restart(struct phylink *pl)
336 if (pl->link_config.an_enabled &&
337 phy_interface_mode_is_8023z(pl->link_config.interface))
338 pl->ops->mac_an_restart(pl->netdev);
341 static int phylink_get_mac_state(struct phylink *pl, struct phylink_link_state *state)
343 struct net_device *ndev = pl->netdev;
345 linkmode_copy(state->advertising, pl->link_config.advertising);
346 linkmode_zero(state->lp_advertising);
347 state->interface = pl->link_config.interface;
348 state->an_enabled = pl->link_config.an_enabled;
349 state->link = 1;
351 return pl->ops->mac_link_state(ndev, state);
354 /* The fixed state is... fixed except for the link state,
355 * which may be determined by a GPIO or a callback.
357 static void phylink_get_fixed_state(struct phylink *pl, struct phylink_link_state *state)
359 *state = pl->link_config;
360 if (pl->get_fixed_state)
361 pl->get_fixed_state(pl->netdev, state);
362 else if (pl->link_gpio)
363 state->link = !!gpiod_get_value(pl->link_gpio);
366 /* Flow control is resolved according to our and the link partners
367 * advertisments using the following drawn from the 802.3 specs:
368 * Local device Link partner
369 * Pause AsymDir Pause AsymDir Result
370 * 1 X 1 X TX+RX
371 * 0 1 1 1 RX
372 * 1 1 0 1 TX
374 static void phylink_resolve_flow(struct phylink *pl,
375 struct phylink_link_state *state)
377 int new_pause = 0;
379 if (pl->link_config.pause & MLO_PAUSE_AN) {
380 int pause = 0;
382 if (phylink_test(pl->link_config.advertising, Pause))
383 pause |= MLO_PAUSE_SYM;
384 if (phylink_test(pl->link_config.advertising, Asym_Pause))
385 pause |= MLO_PAUSE_ASYM;
387 pause &= state->pause;
389 if (pause & MLO_PAUSE_SYM)
390 new_pause = MLO_PAUSE_TX | MLO_PAUSE_RX;
391 else if (pause & MLO_PAUSE_ASYM)
392 new_pause = state->pause & MLO_PAUSE_SYM ?
393 MLO_PAUSE_RX : MLO_PAUSE_TX;
394 } else {
395 new_pause = pl->link_config.pause & MLO_PAUSE_TXRX_MASK;
398 state->pause &= ~MLO_PAUSE_TXRX_MASK;
399 state->pause |= new_pause;
402 static const char *phylink_pause_to_str(int pause)
404 switch (pause & MLO_PAUSE_TXRX_MASK) {
405 case MLO_PAUSE_TX | MLO_PAUSE_RX:
406 return "rx/tx";
407 case MLO_PAUSE_TX:
408 return "tx";
409 case MLO_PAUSE_RX:
410 return "rx";
411 default:
412 return "off";
416 static void phylink_resolve(struct work_struct *w)
418 struct phylink *pl = container_of(w, struct phylink, resolve);
419 struct phylink_link_state link_state;
420 struct net_device *ndev = pl->netdev;
422 mutex_lock(&pl->state_mutex);
423 if (pl->phylink_disable_state) {
424 pl->mac_link_dropped = false;
425 link_state.link = false;
426 } else if (pl->mac_link_dropped) {
427 link_state.link = false;
428 } else {
429 switch (pl->link_an_mode) {
430 case MLO_AN_PHY:
431 link_state = pl->phy_state;
432 phylink_resolve_flow(pl, &link_state);
433 phylink_mac_config(pl, &link_state);
434 break;
436 case MLO_AN_FIXED:
437 phylink_get_fixed_state(pl, &link_state);
438 phylink_mac_config(pl, &link_state);
439 break;
441 case MLO_AN_INBAND:
442 phylink_get_mac_state(pl, &link_state);
443 if (pl->phydev) {
444 bool changed = false;
446 link_state.link = link_state.link &&
447 pl->phy_state.link;
449 if (pl->phy_state.interface !=
450 link_state.interface) {
451 link_state.interface = pl->phy_state.interface;
452 changed = true;
455 /* Propagate the flow control from the PHY
456 * to the MAC. Also propagate the interface
457 * if changed.
459 if (pl->phy_state.link || changed) {
460 link_state.pause |= pl->phy_state.pause;
461 phylink_resolve_flow(pl, &link_state);
463 phylink_mac_config(pl, &link_state);
466 break;
470 if (link_state.link != netif_carrier_ok(ndev)) {
471 if (!link_state.link) {
472 netif_carrier_off(ndev);
473 pl->ops->mac_link_down(ndev, pl->link_an_mode);
474 netdev_info(ndev, "Link is Down\n");
475 } else {
476 pl->ops->mac_link_up(ndev, pl->link_an_mode,
477 pl->phydev);
479 netif_carrier_on(ndev);
481 netdev_info(ndev,
482 "Link is Up - %s/%s - flow control %s\n",
483 phy_speed_to_str(link_state.speed),
484 phy_duplex_to_str(link_state.duplex),
485 phylink_pause_to_str(link_state.pause));
488 if (!link_state.link && pl->mac_link_dropped) {
489 pl->mac_link_dropped = false;
490 queue_work(system_power_efficient_wq, &pl->resolve);
492 mutex_unlock(&pl->state_mutex);
495 static void phylink_run_resolve(struct phylink *pl)
497 if (!pl->phylink_disable_state)
498 queue_work(system_power_efficient_wq, &pl->resolve);
501 static const struct sfp_upstream_ops sfp_phylink_ops;
503 static int phylink_register_sfp(struct phylink *pl,
504 struct fwnode_handle *fwnode)
506 struct fwnode_reference_args ref;
507 int ret;
509 if (!fwnode)
510 return 0;
512 ret = fwnode_property_get_reference_args(fwnode, "sfp", NULL,
513 0, 0, &ref);
514 if (ret < 0) {
515 if (ret == -ENOENT)
516 return 0;
518 netdev_err(pl->netdev, "unable to parse \"sfp\" node: %d\n",
519 ret);
520 return ret;
523 pl->sfp_bus = sfp_register_upstream(ref.fwnode, pl->netdev, pl,
524 &sfp_phylink_ops);
525 if (!pl->sfp_bus)
526 return -ENOMEM;
528 return 0;
532 * phylink_create() - create a phylink instance
533 * @ndev: a pointer to the &struct net_device
534 * @fwnode: a pointer to a &struct fwnode_handle describing the network
535 * interface
536 * @iface: the desired link mode defined by &typedef phy_interface_t
537 * @ops: a pointer to a &struct phylink_mac_ops for the MAC.
539 * Create a new phylink instance, and parse the link parameters found in @np.
540 * This will parse in-band modes, fixed-link or SFP configuration.
542 * Returns a pointer to a &struct phylink, or an error-pointer value. Users
543 * must use IS_ERR() to check for errors from this function.
545 struct phylink *phylink_create(struct net_device *ndev,
546 struct fwnode_handle *fwnode,
547 phy_interface_t iface,
548 const struct phylink_mac_ops *ops)
550 struct phylink *pl;
551 int ret;
553 pl = kzalloc(sizeof(*pl), GFP_KERNEL);
554 if (!pl)
555 return ERR_PTR(-ENOMEM);
557 mutex_init(&pl->state_mutex);
558 INIT_WORK(&pl->resolve, phylink_resolve);
559 pl->netdev = ndev;
560 pl->phy_state.interface = iface;
561 pl->link_interface = iface;
562 if (iface == PHY_INTERFACE_MODE_MOCA)
563 pl->link_port = PORT_BNC;
564 else
565 pl->link_port = PORT_MII;
566 pl->link_config.interface = iface;
567 pl->link_config.pause = MLO_PAUSE_AN;
568 pl->link_config.speed = SPEED_UNKNOWN;
569 pl->link_config.duplex = DUPLEX_UNKNOWN;
570 pl->link_config.an_enabled = true;
571 pl->ops = ops;
572 __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
574 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
575 linkmode_copy(pl->link_config.advertising, pl->supported);
576 phylink_validate(pl, pl->supported, &pl->link_config);
578 ret = phylink_parse_mode(pl, fwnode);
579 if (ret < 0) {
580 kfree(pl);
581 return ERR_PTR(ret);
584 if (pl->link_an_mode == MLO_AN_FIXED) {
585 ret = phylink_parse_fixedlink(pl, fwnode);
586 if (ret < 0) {
587 kfree(pl);
588 return ERR_PTR(ret);
592 ret = phylink_register_sfp(pl, fwnode);
593 if (ret < 0) {
594 kfree(pl);
595 return ERR_PTR(ret);
598 return pl;
600 EXPORT_SYMBOL_GPL(phylink_create);
603 * phylink_destroy() - cleanup and destroy the phylink instance
604 * @pl: a pointer to a &struct phylink returned from phylink_create()
606 * Destroy a phylink instance. Any PHY that has been attached must have been
607 * cleaned up via phylink_disconnect_phy() prior to calling this function.
609 void phylink_destroy(struct phylink *pl)
611 if (pl->sfp_bus)
612 sfp_unregister_upstream(pl->sfp_bus);
614 cancel_work_sync(&pl->resolve);
615 kfree(pl);
617 EXPORT_SYMBOL_GPL(phylink_destroy);
619 static void phylink_phy_change(struct phy_device *phydev, bool up,
620 bool do_carrier)
622 struct phylink *pl = phydev->phylink;
624 mutex_lock(&pl->state_mutex);
625 pl->phy_state.speed = phydev->speed;
626 pl->phy_state.duplex = phydev->duplex;
627 pl->phy_state.pause = MLO_PAUSE_NONE;
628 if (phydev->pause)
629 pl->phy_state.pause |= MLO_PAUSE_SYM;
630 if (phydev->asym_pause)
631 pl->phy_state.pause |= MLO_PAUSE_ASYM;
632 pl->phy_state.interface = phydev->interface;
633 pl->phy_state.link = up;
634 mutex_unlock(&pl->state_mutex);
636 phylink_run_resolve(pl);
638 netdev_dbg(pl->netdev, "phy link %s %s/%s/%s\n", up ? "up" : "down",
639 phy_modes(phydev->interface),
640 phy_speed_to_str(phydev->speed),
641 phy_duplex_to_str(phydev->duplex));
644 static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy)
646 struct phylink_link_state config;
647 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
648 u32 advertising;
649 int ret;
651 memset(&config, 0, sizeof(config));
652 ethtool_convert_legacy_u32_to_link_mode(supported, phy->supported);
653 ethtool_convert_legacy_u32_to_link_mode(config.advertising,
654 phy->advertising);
655 config.interface = pl->link_config.interface;
658 * This is the new way of dealing with flow control for PHYs,
659 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
660 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
661 * using our validate call to the MAC, we rely upon the MAC
662 * clearing the bits from both supported and advertising fields.
664 if (phylink_test(supported, Pause))
665 phylink_set(config.advertising, Pause);
666 if (phylink_test(supported, Asym_Pause))
667 phylink_set(config.advertising, Asym_Pause);
669 ret = phylink_validate(pl, supported, &config);
670 if (ret)
671 return ret;
673 phy->phylink = pl;
674 phy->phy_link_change = phylink_phy_change;
676 netdev_info(pl->netdev,
677 "PHY [%s] driver [%s]\n", dev_name(&phy->mdio.dev),
678 phy->drv->name);
680 mutex_lock(&phy->lock);
681 mutex_lock(&pl->state_mutex);
682 pl->netdev->phydev = phy;
683 pl->phydev = phy;
684 linkmode_copy(pl->supported, supported);
685 linkmode_copy(pl->link_config.advertising, config.advertising);
687 /* Restrict the phy advertisment according to the MAC support. */
688 ethtool_convert_link_mode_to_legacy_u32(&advertising, config.advertising);
689 phy->advertising = advertising;
690 mutex_unlock(&pl->state_mutex);
691 mutex_unlock(&phy->lock);
693 netdev_dbg(pl->netdev,
694 "phy: setting supported %*pb advertising 0x%08x\n",
695 __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
696 phy->advertising);
698 phy_start_machine(phy);
699 if (phy->irq > 0)
700 phy_start_interrupts(phy);
702 return 0;
706 * phylink_connect_phy() - connect a PHY to the phylink instance
707 * @pl: a pointer to a &struct phylink returned from phylink_create()
708 * @phy: a pointer to a &struct phy_device.
710 * Connect @phy to the phylink instance specified by @pl by calling
711 * phy_attach_direct(). Configure the @phy according to the MAC driver's
712 * capabilities, start the PHYLIB state machine and enable any interrupts
713 * that the PHY supports.
715 * This updates the phylink's ethtool supported and advertising link mode
716 * masks.
718 * Returns 0 on success or a negative errno.
720 int phylink_connect_phy(struct phylink *pl, struct phy_device *phy)
722 int ret;
724 if (WARN_ON(pl->link_an_mode == MLO_AN_FIXED ||
725 (pl->link_an_mode == MLO_AN_INBAND &&
726 phy_interface_mode_is_8023z(pl->link_interface))))
727 return -EINVAL;
729 if (pl->phydev)
730 return -EBUSY;
732 /* Use PHY device/driver interface */
733 if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
734 pl->link_interface = phy->interface;
735 pl->link_config.interface = pl->link_interface;
738 ret = phy_attach_direct(pl->netdev, phy, 0, pl->link_interface);
739 if (ret)
740 return ret;
742 ret = phylink_bringup_phy(pl, phy);
743 if (ret)
744 phy_detach(phy);
746 return ret;
748 EXPORT_SYMBOL_GPL(phylink_connect_phy);
751 * phylink_of_phy_connect() - connect the PHY specified in the DT mode.
752 * @pl: a pointer to a &struct phylink returned from phylink_create()
753 * @dn: a pointer to a &struct device_node.
754 * @flags: PHY-specific flags to communicate to the PHY device driver
756 * Connect the phy specified in the device node @dn to the phylink instance
757 * specified by @pl. Actions specified in phylink_connect_phy() will be
758 * performed.
760 * Returns 0 on success or a negative errno.
762 int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn,
763 u32 flags)
765 struct device_node *phy_node;
766 struct phy_device *phy_dev;
767 int ret;
769 /* Fixed links and 802.3z are handled without needing a PHY */
770 if (pl->link_an_mode == MLO_AN_FIXED ||
771 (pl->link_an_mode == MLO_AN_INBAND &&
772 phy_interface_mode_is_8023z(pl->link_interface)))
773 return 0;
775 phy_node = of_parse_phandle(dn, "phy-handle", 0);
776 if (!phy_node)
777 phy_node = of_parse_phandle(dn, "phy", 0);
778 if (!phy_node)
779 phy_node = of_parse_phandle(dn, "phy-device", 0);
781 if (!phy_node) {
782 if (pl->link_an_mode == MLO_AN_PHY)
783 return -ENODEV;
784 return 0;
787 phy_dev = of_phy_attach(pl->netdev, phy_node, flags,
788 pl->link_interface);
789 /* We're done with the phy_node handle */
790 of_node_put(phy_node);
792 if (!phy_dev)
793 return -ENODEV;
795 ret = phylink_bringup_phy(pl, phy_dev);
796 if (ret)
797 phy_detach(phy_dev);
799 return ret;
801 EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
804 * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
805 * instance.
806 * @pl: a pointer to a &struct phylink returned from phylink_create()
808 * Disconnect any current PHY from the phylink instance described by @pl.
810 void phylink_disconnect_phy(struct phylink *pl)
812 struct phy_device *phy;
814 ASSERT_RTNL();
816 phy = pl->phydev;
817 if (phy) {
818 mutex_lock(&phy->lock);
819 mutex_lock(&pl->state_mutex);
820 pl->netdev->phydev = NULL;
821 pl->phydev = NULL;
822 mutex_unlock(&pl->state_mutex);
823 mutex_unlock(&phy->lock);
824 flush_work(&pl->resolve);
826 phy_disconnect(phy);
829 EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
832 * phylink_fixed_state_cb() - allow setting a fixed link callback
833 * @pl: a pointer to a &struct phylink returned from phylink_create()
834 * @cb: callback to execute to determine the fixed link state.
836 * The MAC driver should call this driver when the state of its link
837 * can be determined through e.g: an out of band MMIO register.
839 int phylink_fixed_state_cb(struct phylink *pl,
840 void (*cb)(struct net_device *dev,
841 struct phylink_link_state *state))
843 /* It does not make sense to let the link be overriden unless we use
844 * MLO_AN_FIXED
846 if (pl->link_an_mode != MLO_AN_FIXED)
847 return -EINVAL;
849 mutex_lock(&pl->state_mutex);
850 pl->get_fixed_state = cb;
851 mutex_unlock(&pl->state_mutex);
853 return 0;
855 EXPORT_SYMBOL_GPL(phylink_fixed_state_cb);
858 * phylink_mac_change() - notify phylink of a change in MAC state
859 * @pl: a pointer to a &struct phylink returned from phylink_create()
860 * @up: indicates whether the link is currently up.
862 * The MAC driver should call this driver when the state of its link
863 * changes (eg, link failure, new negotiation results, etc.)
865 void phylink_mac_change(struct phylink *pl, bool up)
867 if (!up)
868 pl->mac_link_dropped = true;
869 phylink_run_resolve(pl);
870 netdev_dbg(pl->netdev, "mac link %s\n", up ? "up" : "down");
872 EXPORT_SYMBOL_GPL(phylink_mac_change);
875 * phylink_start() - start a phylink instance
876 * @pl: a pointer to a &struct phylink returned from phylink_create()
878 * Start the phylink instance specified by @pl, configuring the MAC for the
879 * desired link mode(s) and negotiation style. This should be called from the
880 * network device driver's &struct net_device_ops ndo_open() method.
882 void phylink_start(struct phylink *pl)
884 ASSERT_RTNL();
886 netdev_info(pl->netdev, "configuring for %s/%s link mode\n",
887 phylink_an_mode_str(pl->link_an_mode),
888 phy_modes(pl->link_config.interface));
890 /* Apply the link configuration to the MAC when starting. This allows
891 * a fixed-link to start with the correct parameters, and also
892 * ensures that we set the appropriate advertisment for Serdes links.
894 phylink_resolve_flow(pl, &pl->link_config);
895 phylink_mac_config(pl, &pl->link_config);
897 /* Restart autonegotiation if using 802.3z to ensure that the link
898 * parameters are properly negotiated. This is necessary for DSA
899 * switches using 802.3z negotiation to ensure they see our modes.
901 phylink_mac_an_restart(pl);
903 clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
904 phylink_run_resolve(pl);
906 if (pl->sfp_bus)
907 sfp_upstream_start(pl->sfp_bus);
908 if (pl->phydev)
909 phy_start(pl->phydev);
911 EXPORT_SYMBOL_GPL(phylink_start);
914 * phylink_stop() - stop a phylink instance
915 * @pl: a pointer to a &struct phylink returned from phylink_create()
917 * Stop the phylink instance specified by @pl. This should be called from the
918 * network device driver's &struct net_device_ops ndo_stop() method. The
919 * network device's carrier state should not be changed prior to calling this
920 * function.
922 void phylink_stop(struct phylink *pl)
924 ASSERT_RTNL();
926 if (pl->phydev)
927 phy_stop(pl->phydev);
928 if (pl->sfp_bus)
929 sfp_upstream_stop(pl->sfp_bus);
931 set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
932 queue_work(system_power_efficient_wq, &pl->resolve);
933 flush_work(&pl->resolve);
935 EXPORT_SYMBOL_GPL(phylink_stop);
938 * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY
939 * @pl: a pointer to a &struct phylink returned from phylink_create()
940 * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters
942 * Read the wake on lan parameters from the PHY attached to the phylink
943 * instance specified by @pl. If no PHY is currently attached, report no
944 * support for wake on lan.
946 void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
948 ASSERT_RTNL();
950 wol->supported = 0;
951 wol->wolopts = 0;
953 if (pl->phydev)
954 phy_ethtool_get_wol(pl->phydev, wol);
956 EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol);
959 * phylink_ethtool_set_wol() - set wake on lan parameters
960 * @pl: a pointer to a &struct phylink returned from phylink_create()
961 * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters
963 * Set the wake on lan parameters for the PHY attached to the phylink
964 * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP
965 * error.
967 * Returns zero on success or negative errno code.
969 int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
971 int ret = -EOPNOTSUPP;
973 ASSERT_RTNL();
975 if (pl->phydev)
976 ret = phy_ethtool_set_wol(pl->phydev, wol);
978 return ret;
980 EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol);
982 static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b)
984 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
986 linkmode_zero(mask);
987 phylink_set_port_modes(mask);
989 linkmode_and(dst, dst, mask);
990 linkmode_or(dst, dst, b);
993 static void phylink_get_ksettings(const struct phylink_link_state *state,
994 struct ethtool_link_ksettings *kset)
996 phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
997 linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
998 kset->base.speed = state->speed;
999 kset->base.duplex = state->duplex;
1000 kset->base.autoneg = state->an_enabled ? AUTONEG_ENABLE :
1001 AUTONEG_DISABLE;
1005 * phylink_ethtool_ksettings_get() - get the current link settings
1006 * @pl: a pointer to a &struct phylink returned from phylink_create()
1007 * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings
1009 * Read the current link settings for the phylink instance specified by @pl.
1010 * This will be the link settings read from the MAC, PHY or fixed link
1011 * settings depending on the current negotiation mode.
1013 int phylink_ethtool_ksettings_get(struct phylink *pl,
1014 struct ethtool_link_ksettings *kset)
1016 struct phylink_link_state link_state;
1018 ASSERT_RTNL();
1020 if (pl->phydev) {
1021 phy_ethtool_ksettings_get(pl->phydev, kset);
1022 } else {
1023 kset->base.port = pl->link_port;
1026 linkmode_copy(kset->link_modes.supported, pl->supported);
1028 switch (pl->link_an_mode) {
1029 case MLO_AN_FIXED:
1030 /* We are using fixed settings. Report these as the
1031 * current link settings - and note that these also
1032 * represent the supported speeds/duplex/pause modes.
1034 phylink_get_fixed_state(pl, &link_state);
1035 phylink_get_ksettings(&link_state, kset);
1036 break;
1038 case MLO_AN_INBAND:
1039 /* If there is a phy attached, then use the reported
1040 * settings from the phy with no modification.
1042 if (pl->phydev)
1043 break;
1045 phylink_get_mac_state(pl, &link_state);
1047 /* The MAC is reporting the link results from its own PCS
1048 * layer via in-band status. Report these as the current
1049 * link settings.
1051 phylink_get_ksettings(&link_state, kset);
1052 break;
1055 return 0;
1057 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get);
1060 * phylink_ethtool_ksettings_set() - set the link settings
1061 * @pl: a pointer to a &struct phylink returned from phylink_create()
1062 * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes
1064 int phylink_ethtool_ksettings_set(struct phylink *pl,
1065 const struct ethtool_link_ksettings *kset)
1067 struct ethtool_link_ksettings our_kset;
1068 struct phylink_link_state config;
1069 int ret;
1071 ASSERT_RTNL();
1073 if (kset->base.autoneg != AUTONEG_DISABLE &&
1074 kset->base.autoneg != AUTONEG_ENABLE)
1075 return -EINVAL;
1077 config = pl->link_config;
1079 /* Mask out unsupported advertisments */
1080 linkmode_and(config.advertising, kset->link_modes.advertising,
1081 pl->supported);
1083 /* FIXME: should we reject autoneg if phy/mac does not support it? */
1084 if (kset->base.autoneg == AUTONEG_DISABLE) {
1085 const struct phy_setting *s;
1087 /* Autonegotiation disabled, select a suitable speed and
1088 * duplex.
1090 s = phy_lookup_setting(kset->base.speed, kset->base.duplex,
1091 pl->supported,
1092 __ETHTOOL_LINK_MODE_MASK_NBITS, false);
1093 if (!s)
1094 return -EINVAL;
1096 /* If we have a fixed link (as specified by firmware), refuse
1097 * to change link parameters.
1099 if (pl->link_an_mode == MLO_AN_FIXED &&
1100 (s->speed != pl->link_config.speed ||
1101 s->duplex != pl->link_config.duplex))
1102 return -EINVAL;
1104 config.speed = s->speed;
1105 config.duplex = s->duplex;
1106 config.an_enabled = false;
1108 __clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
1109 } else {
1110 /* If we have a fixed link, refuse to enable autonegotiation */
1111 if (pl->link_an_mode == MLO_AN_FIXED)
1112 return -EINVAL;
1114 config.speed = SPEED_UNKNOWN;
1115 config.duplex = DUPLEX_UNKNOWN;
1116 config.an_enabled = true;
1118 __set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
1121 if (phylink_validate(pl, pl->supported, &config))
1122 return -EINVAL;
1124 /* If autonegotiation is enabled, we must have an advertisment */
1125 if (config.an_enabled && phylink_is_empty_linkmode(config.advertising))
1126 return -EINVAL;
1128 our_kset = *kset;
1129 linkmode_copy(our_kset.link_modes.advertising, config.advertising);
1130 our_kset.base.speed = config.speed;
1131 our_kset.base.duplex = config.duplex;
1133 /* If we have a PHY, configure the phy */
1134 if (pl->phydev) {
1135 ret = phy_ethtool_ksettings_set(pl->phydev, &our_kset);
1136 if (ret)
1137 return ret;
1140 mutex_lock(&pl->state_mutex);
1141 /* Configure the MAC to match the new settings */
1142 linkmode_copy(pl->link_config.advertising, our_kset.link_modes.advertising);
1143 pl->link_config.interface = config.interface;
1144 pl->link_config.speed = our_kset.base.speed;
1145 pl->link_config.duplex = our_kset.base.duplex;
1146 pl->link_config.an_enabled = our_kset.base.autoneg != AUTONEG_DISABLE;
1148 if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
1149 phylink_mac_config(pl, &pl->link_config);
1150 phylink_mac_an_restart(pl);
1152 mutex_unlock(&pl->state_mutex);
1154 return 0;
1156 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
1159 * phylink_ethtool_nway_reset() - restart negotiation
1160 * @pl: a pointer to a &struct phylink returned from phylink_create()
1162 * Restart negotiation for the phylink instance specified by @pl. This will
1163 * cause any attached phy to restart negotiation with the link partner, and
1164 * if the MAC is in a BaseX mode, the MAC will also be requested to restart
1165 * negotiation.
1167 * Returns zero on success, or negative error code.
1169 int phylink_ethtool_nway_reset(struct phylink *pl)
1171 int ret = 0;
1173 ASSERT_RTNL();
1175 if (pl->phydev)
1176 ret = phy_restart_aneg(pl->phydev);
1177 phylink_mac_an_restart(pl);
1179 return ret;
1181 EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
1184 * phylink_ethtool_get_pauseparam() - get the current pause parameters
1185 * @pl: a pointer to a &struct phylink returned from phylink_create()
1186 * @pause: a pointer to a &struct ethtool_pauseparam
1188 void phylink_ethtool_get_pauseparam(struct phylink *pl,
1189 struct ethtool_pauseparam *pause)
1191 ASSERT_RTNL();
1193 pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
1194 pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
1195 pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
1197 EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
1200 * phylink_ethtool_set_pauseparam() - set the current pause parameters
1201 * @pl: a pointer to a &struct phylink returned from phylink_create()
1202 * @pause: a pointer to a &struct ethtool_pauseparam
1204 int phylink_ethtool_set_pauseparam(struct phylink *pl,
1205 struct ethtool_pauseparam *pause)
1207 struct phylink_link_state *config = &pl->link_config;
1209 ASSERT_RTNL();
1211 if (!phylink_test(pl->supported, Pause) &&
1212 !phylink_test(pl->supported, Asym_Pause))
1213 return -EOPNOTSUPP;
1215 if (!phylink_test(pl->supported, Asym_Pause) &&
1216 !pause->autoneg && pause->rx_pause != pause->tx_pause)
1217 return -EINVAL;
1219 config->pause &= ~(MLO_PAUSE_AN | MLO_PAUSE_TXRX_MASK);
1221 if (pause->autoneg)
1222 config->pause |= MLO_PAUSE_AN;
1223 if (pause->rx_pause)
1224 config->pause |= MLO_PAUSE_RX;
1225 if (pause->tx_pause)
1226 config->pause |= MLO_PAUSE_TX;
1228 if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
1229 switch (pl->link_an_mode) {
1230 case MLO_AN_PHY:
1231 /* Silently mark the carrier down, and then trigger a resolve */
1232 netif_carrier_off(pl->netdev);
1233 phylink_run_resolve(pl);
1234 break;
1236 case MLO_AN_FIXED:
1237 /* Should we allow fixed links to change against the config? */
1238 phylink_resolve_flow(pl, config);
1239 phylink_mac_config(pl, config);
1240 break;
1242 case MLO_AN_INBAND:
1243 phylink_mac_config(pl, config);
1244 phylink_mac_an_restart(pl);
1245 break;
1249 return 0;
1251 EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
1253 int phylink_ethtool_get_module_info(struct phylink *pl,
1254 struct ethtool_modinfo *modinfo)
1256 int ret = -EOPNOTSUPP;
1258 WARN_ON(!lockdep_rtnl_is_held());
1260 if (pl->sfp_bus)
1261 ret = sfp_get_module_info(pl->sfp_bus, modinfo);
1263 return ret;
1265 EXPORT_SYMBOL_GPL(phylink_ethtool_get_module_info);
1267 int phylink_ethtool_get_module_eeprom(struct phylink *pl,
1268 struct ethtool_eeprom *ee, u8 *buf)
1270 int ret = -EOPNOTSUPP;
1272 WARN_ON(!lockdep_rtnl_is_held());
1274 if (pl->sfp_bus)
1275 ret = sfp_get_module_eeprom(pl->sfp_bus, ee, buf);
1277 return ret;
1279 EXPORT_SYMBOL_GPL(phylink_ethtool_get_module_eeprom);
1282 * phylink_ethtool_get_eee_err() - read the energy efficient ethernet error
1283 * counter
1284 * @pl: a pointer to a &struct phylink returned from phylink_create().
1286 * Read the Energy Efficient Ethernet error counter from the PHY associated
1287 * with the phylink instance specified by @pl.
1289 * Returns positive error counter value, or negative error code.
1291 int phylink_get_eee_err(struct phylink *pl)
1293 int ret = 0;
1295 ASSERT_RTNL();
1297 if (pl->phydev)
1298 ret = phy_get_eee_err(pl->phydev);
1300 return ret;
1302 EXPORT_SYMBOL_GPL(phylink_get_eee_err);
1305 * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters
1306 * @pl: a pointer to a &struct phylink returned from phylink_create()
1307 * @eee: a pointer to a &struct ethtool_eee for the read parameters
1309 int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee)
1311 int ret = -EOPNOTSUPP;
1313 ASSERT_RTNL();
1315 if (pl->phydev)
1316 ret = phy_ethtool_get_eee(pl->phydev, eee);
1318 return ret;
1320 EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
1323 * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters
1324 * @pl: a pointer to a &struct phylink returned from phylink_create()
1325 * @eee: a pointer to a &struct ethtool_eee for the desired parameters
1327 int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_eee *eee)
1329 int ret = -EOPNOTSUPP;
1331 ASSERT_RTNL();
1333 if (pl->phydev)
1334 ret = phy_ethtool_set_eee(pl->phydev, eee);
1336 return ret;
1338 EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
1340 /* This emulates MII registers for a fixed-mode phy operating as per the
1341 * passed in state. "aneg" defines if we report negotiation is possible.
1343 * FIXME: should deal with negotiation state too.
1345 static int phylink_mii_emul_read(struct net_device *ndev, unsigned int reg,
1346 struct phylink_link_state *state, bool aneg)
1348 struct fixed_phy_status fs;
1349 int val;
1351 fs.link = state->link;
1352 fs.speed = state->speed;
1353 fs.duplex = state->duplex;
1354 fs.pause = state->pause & MLO_PAUSE_SYM;
1355 fs.asym_pause = state->pause & MLO_PAUSE_ASYM;
1357 val = swphy_read_reg(reg, &fs);
1358 if (reg == MII_BMSR) {
1359 if (!state->an_complete)
1360 val &= ~BMSR_ANEGCOMPLETE;
1361 if (!aneg)
1362 val &= ~BMSR_ANEGCAPABLE;
1364 return val;
1367 static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
1368 unsigned int reg)
1370 struct phy_device *phydev = pl->phydev;
1371 int prtad, devad;
1373 if (mdio_phy_id_is_c45(phy_id)) {
1374 prtad = mdio_phy_id_prtad(phy_id);
1375 devad = mdio_phy_id_devad(phy_id);
1376 devad = MII_ADDR_C45 | devad << 16 | reg;
1377 } else if (phydev->is_c45) {
1378 switch (reg) {
1379 case MII_BMCR:
1380 case MII_BMSR:
1381 case MII_PHYSID1:
1382 case MII_PHYSID2:
1383 devad = __ffs(phydev->c45_ids.devices_in_package);
1384 break;
1385 case MII_ADVERTISE:
1386 case MII_LPA:
1387 if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1388 return -EINVAL;
1389 devad = MDIO_MMD_AN;
1390 if (reg == MII_ADVERTISE)
1391 reg = MDIO_AN_ADVERTISE;
1392 else
1393 reg = MDIO_AN_LPA;
1394 break;
1395 default:
1396 return -EINVAL;
1398 prtad = phy_id;
1399 devad = MII_ADDR_C45 | devad << 16 | reg;
1400 } else {
1401 prtad = phy_id;
1402 devad = reg;
1404 return mdiobus_read(pl->phydev->mdio.bus, prtad, devad);
1407 static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
1408 unsigned int reg, unsigned int val)
1410 struct phy_device *phydev = pl->phydev;
1411 int prtad, devad;
1413 if (mdio_phy_id_is_c45(phy_id)) {
1414 prtad = mdio_phy_id_prtad(phy_id);
1415 devad = mdio_phy_id_devad(phy_id);
1416 devad = MII_ADDR_C45 | devad << 16 | reg;
1417 } else if (phydev->is_c45) {
1418 switch (reg) {
1419 case MII_BMCR:
1420 case MII_BMSR:
1421 case MII_PHYSID1:
1422 case MII_PHYSID2:
1423 devad = __ffs(phydev->c45_ids.devices_in_package);
1424 break;
1425 case MII_ADVERTISE:
1426 case MII_LPA:
1427 if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1428 return -EINVAL;
1429 devad = MDIO_MMD_AN;
1430 if (reg == MII_ADVERTISE)
1431 reg = MDIO_AN_ADVERTISE;
1432 else
1433 reg = MDIO_AN_LPA;
1434 break;
1435 default:
1436 return -EINVAL;
1438 prtad = phy_id;
1439 devad = MII_ADDR_C45 | devad << 16 | reg;
1440 } else {
1441 prtad = phy_id;
1442 devad = reg;
1445 return mdiobus_write(phydev->mdio.bus, prtad, devad, val);
1448 static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
1449 unsigned int reg)
1451 struct phylink_link_state state;
1452 int val = 0xffff;
1454 switch (pl->link_an_mode) {
1455 case MLO_AN_FIXED:
1456 if (phy_id == 0) {
1457 phylink_get_fixed_state(pl, &state);
1458 val = phylink_mii_emul_read(pl->netdev, reg, &state,
1459 true);
1461 break;
1463 case MLO_AN_PHY:
1464 return -EOPNOTSUPP;
1466 case MLO_AN_INBAND:
1467 if (phy_id == 0) {
1468 val = phylink_get_mac_state(pl, &state);
1469 if (val < 0)
1470 return val;
1472 val = phylink_mii_emul_read(pl->netdev, reg, &state,
1473 true);
1475 break;
1478 return val & 0xffff;
1481 static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
1482 unsigned int reg, unsigned int val)
1484 switch (pl->link_an_mode) {
1485 case MLO_AN_FIXED:
1486 break;
1488 case MLO_AN_PHY:
1489 return -EOPNOTSUPP;
1491 case MLO_AN_INBAND:
1492 break;
1495 return 0;
1499 * phylink_mii_ioctl() - generic mii ioctl interface
1500 * @pl: a pointer to a &struct phylink returned from phylink_create()
1501 * @ifr: a pointer to a &struct ifreq for socket ioctls
1502 * @cmd: ioctl cmd to execute
1504 * Perform the specified MII ioctl on the PHY attached to the phylink instance
1505 * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
1507 * Returns: zero on success or negative error code.
1509 * %SIOCGMIIPHY:
1510 * read register from the current PHY.
1511 * %SIOCGMIIREG:
1512 * read register from the specified PHY.
1513 * %SIOCSMIIREG:
1514 * set a register on the specified PHY.
1516 int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
1518 struct mii_ioctl_data *mii = if_mii(ifr);
1519 int ret;
1521 ASSERT_RTNL();
1523 if (pl->phydev) {
1524 /* PHYs only exist for MLO_AN_PHY and SGMII */
1525 switch (cmd) {
1526 case SIOCGMIIPHY:
1527 mii->phy_id = pl->phydev->mdio.addr;
1528 /* fall through */
1530 case SIOCGMIIREG:
1531 ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
1532 if (ret >= 0) {
1533 mii->val_out = ret;
1534 ret = 0;
1536 break;
1538 case SIOCSMIIREG:
1539 ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
1540 mii->val_in);
1541 break;
1543 default:
1544 ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
1545 break;
1547 } else {
1548 switch (cmd) {
1549 case SIOCGMIIPHY:
1550 mii->phy_id = 0;
1551 /* fall through */
1553 case SIOCGMIIREG:
1554 ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
1555 if (ret >= 0) {
1556 mii->val_out = ret;
1557 ret = 0;
1559 break;
1561 case SIOCSMIIREG:
1562 ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
1563 mii->val_in);
1564 break;
1566 default:
1567 ret = -EOPNOTSUPP;
1568 break;
1572 return ret;
1574 EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
1576 static int phylink_sfp_module_insert(void *upstream,
1577 const struct sfp_eeprom_id *id)
1579 struct phylink *pl = upstream;
1580 __ETHTOOL_DECLARE_LINK_MODE_MASK(support) = { 0, };
1581 struct phylink_link_state config;
1582 phy_interface_t iface;
1583 int ret = 0;
1584 bool changed;
1585 u8 port;
1587 sfp_parse_support(pl->sfp_bus, id, support);
1588 port = sfp_parse_port(pl->sfp_bus, id, support);
1589 iface = sfp_parse_interface(pl->sfp_bus, id);
1591 ASSERT_RTNL();
1593 switch (iface) {
1594 case PHY_INTERFACE_MODE_SGMII:
1595 case PHY_INTERFACE_MODE_1000BASEX:
1596 case PHY_INTERFACE_MODE_2500BASEX:
1597 case PHY_INTERFACE_MODE_10GKR:
1598 break;
1599 default:
1600 return -EINVAL;
1603 memset(&config, 0, sizeof(config));
1604 linkmode_copy(config.advertising, support);
1605 config.interface = iface;
1606 config.speed = SPEED_UNKNOWN;
1607 config.duplex = DUPLEX_UNKNOWN;
1608 config.pause = MLO_PAUSE_AN;
1609 config.an_enabled = pl->link_config.an_enabled;
1611 /* Ignore errors if we're expecting a PHY to attach later */
1612 ret = phylink_validate(pl, support, &config);
1613 if (ret) {
1614 netdev_err(pl->netdev, "validation of %s/%s with support %*pb failed: %d\n",
1615 phylink_an_mode_str(MLO_AN_INBAND),
1616 phy_modes(config.interface),
1617 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
1618 return ret;
1621 netdev_dbg(pl->netdev, "requesting link mode %s/%s with support %*pb\n",
1622 phylink_an_mode_str(MLO_AN_INBAND),
1623 phy_modes(config.interface),
1624 __ETHTOOL_LINK_MODE_MASK_NBITS, support);
1626 if (phy_interface_mode_is_8023z(iface) && pl->phydev)
1627 return -EINVAL;
1629 changed = !bitmap_equal(pl->supported, support,
1630 __ETHTOOL_LINK_MODE_MASK_NBITS);
1631 if (changed) {
1632 linkmode_copy(pl->supported, support);
1633 linkmode_copy(pl->link_config.advertising, config.advertising);
1636 if (pl->link_an_mode != MLO_AN_INBAND ||
1637 pl->link_config.interface != config.interface) {
1638 pl->link_config.interface = config.interface;
1639 pl->link_an_mode = MLO_AN_INBAND;
1641 changed = true;
1643 netdev_info(pl->netdev, "switched to %s/%s link mode\n",
1644 phylink_an_mode_str(MLO_AN_INBAND),
1645 phy_modes(config.interface));
1648 pl->link_port = port;
1650 if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
1651 &pl->phylink_disable_state))
1652 phylink_mac_config(pl, &pl->link_config);
1654 return ret;
1657 static void phylink_sfp_link_down(void *upstream)
1659 struct phylink *pl = upstream;
1661 ASSERT_RTNL();
1663 set_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
1664 queue_work(system_power_efficient_wq, &pl->resolve);
1665 flush_work(&pl->resolve);
1668 static void phylink_sfp_link_up(void *upstream)
1670 struct phylink *pl = upstream;
1672 ASSERT_RTNL();
1674 clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
1675 phylink_run_resolve(pl);
1678 static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
1680 return phylink_connect_phy(upstream, phy);
1683 static void phylink_sfp_disconnect_phy(void *upstream)
1685 phylink_disconnect_phy(upstream);
1688 static const struct sfp_upstream_ops sfp_phylink_ops = {
1689 .module_insert = phylink_sfp_module_insert,
1690 .link_up = phylink_sfp_link_up,
1691 .link_down = phylink_sfp_link_down,
1692 .connect_phy = phylink_sfp_connect_phy,
1693 .disconnect_phy = phylink_sfp_disconnect_phy,
1696 MODULE_LICENSE("GPL");