Linux Audio

Check our new training course

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