blob: 60c1b7e5490e88a4633fd117edd9e285bfb3d44c [file] [log] [blame]
Russell King9525ae82017-07-25 15:03:13 +01001/*
2 * phylink models the MAC to optional PHY connection, supporting
3 * technologies such as SFP cages where the PHY is hot-pluggable.
4 *
5 * Copyright (C) 2015 Russell King
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#include <linux/ethtool.h>
12#include <linux/export.h>
13#include <linux/gpio/consumer.h>
14#include <linux/netdevice.h>
15#include <linux/of.h>
16#include <linux/of_mdio.h>
17#include <linux/phy.h>
18#include <linux/phy_fixed.h>
19#include <linux/phylink.h>
20#include <linux/rtnetlink.h>
21#include <linux/spinlock.h>
22#include <linux/workqueue.h>
23
Russell Kingce0aa272017-07-25 15:03:18 +010024#include "sfp.h"
Russell King9525ae82017-07-25 15:03:13 +010025#include "swphy.h"
26
27#define SUPPORTED_INTERFACES \
28 (SUPPORTED_TP | SUPPORTED_MII | SUPPORTED_FIBRE | \
29 SUPPORTED_BNC | SUPPORTED_AUI | SUPPORTED_Backplane)
30#define ADVERTISED_INTERFACES \
31 (ADVERTISED_TP | ADVERTISED_MII | ADVERTISED_FIBRE | \
32 ADVERTISED_BNC | ADVERTISED_AUI | ADVERTISED_Backplane)
33
34enum {
35 PHYLINK_DISABLE_STOPPED,
Russell Kingce0aa272017-07-25 15:03:18 +010036 PHYLINK_DISABLE_LINK,
Russell King9525ae82017-07-25 15:03:13 +010037};
38
Russell King8796c892017-12-01 10:24:47 +000039/**
40 * struct phylink - internal data type for phylink
41 */
Russell King9525ae82017-07-25 15:03:13 +010042struct phylink {
Russell King8796c892017-12-01 10:24:47 +000043 /* private: */
Russell King9525ae82017-07-25 15:03:13 +010044 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
58 struct mutex state_mutex;
59 struct phylink_link_state phy_state;
60 struct work_struct resolve;
61
62 bool mac_link_dropped;
Russell Kingce0aa272017-07-25 15:03:18 +010063
64 struct sfp_bus *sfp_bus;
Russell King9525ae82017-07-25 15:03:13 +010065};
66
67static inline void linkmode_zero(unsigned long *dst)
68{
69 bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS);
70}
71
72static inline void linkmode_copy(unsigned long *dst, const unsigned long *src)
73{
74 bitmap_copy(dst, src, __ETHTOOL_LINK_MODE_MASK_NBITS);
75}
76
77static inline void linkmode_and(unsigned long *dst, const unsigned long *a,
78 const unsigned long *b)
79{
80 bitmap_and(dst, a, b, __ETHTOOL_LINK_MODE_MASK_NBITS);
81}
82
83static inline void linkmode_or(unsigned long *dst, const unsigned long *a,
84 const unsigned long *b)
85{
86 bitmap_or(dst, a, b, __ETHTOOL_LINK_MODE_MASK_NBITS);
87}
88
89static inline bool linkmode_empty(const unsigned long *src)
90{
91 return bitmap_empty(src, __ETHTOOL_LINK_MODE_MASK_NBITS);
92}
93
Russell King8796c892017-12-01 10:24:47 +000094/**
95 * phylink_set_port_modes() - set the port type modes in the ethtool mask
96 * @mask: ethtool link mode mask
97 *
98 * Sets all the port type modes in the ethtool mask. MAC drivers should
99 * use this in their 'validate' callback.
100 */
Russell King9525ae82017-07-25 15:03:13 +0100101void phylink_set_port_modes(unsigned long *mask)
102{
103 phylink_set(mask, TP);
104 phylink_set(mask, AUI);
105 phylink_set(mask, MII);
106 phylink_set(mask, FIBRE);
107 phylink_set(mask, BNC);
108 phylink_set(mask, Backplane);
109}
110EXPORT_SYMBOL_GPL(phylink_set_port_modes);
111
112static int phylink_is_empty_linkmode(const unsigned long *linkmode)
113{
114 __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp) = { 0, };
115
116 phylink_set_port_modes(tmp);
117 phylink_set(tmp, Autoneg);
118 phylink_set(tmp, Pause);
119 phylink_set(tmp, Asym_Pause);
120
121 bitmap_andnot(tmp, linkmode, tmp, __ETHTOOL_LINK_MODE_MASK_NBITS);
122
123 return linkmode_empty(tmp);
124}
125
126static const char *phylink_an_mode_str(unsigned int mode)
127{
128 static const char *modestr[] = {
129 [MLO_AN_PHY] = "phy",
130 [MLO_AN_FIXED] = "fixed",
Russell King86a362c2017-12-01 10:24:26 +0000131 [MLO_AN_INBAND] = "inband",
Russell King9525ae82017-07-25 15:03:13 +0100132 };
133
134 return mode < ARRAY_SIZE(modestr) ? modestr[mode] : "unknown";
135}
136
137static int phylink_validate(struct phylink *pl, unsigned long *supported,
138 struct phylink_link_state *state)
139{
140 pl->ops->validate(pl->netdev, supported, state);
141
142 return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
143}
144
Russell King8fa7b9b2017-12-01 10:25:09 +0000145static int phylink_parse_fixedlink(struct phylink *pl,
146 struct fwnode_handle *fwnode)
Russell King9525ae82017-07-25 15:03:13 +0100147{
Russell King8fa7b9b2017-12-01 10:25:09 +0000148 struct fwnode_handle *fixed_node;
Russell King9525ae82017-07-25 15:03:13 +0100149 const struct phy_setting *s;
150 struct gpio_desc *desc;
Russell King9525ae82017-07-25 15:03:13 +0100151 u32 speed;
Russell King8fa7b9b2017-12-01 10:25:09 +0000152 int ret;
Russell King9525ae82017-07-25 15:03:13 +0100153
Russell King8fa7b9b2017-12-01 10:25:09 +0000154 fixed_node = fwnode_get_named_child_node(fwnode, "fixed-link");
Russell King9525ae82017-07-25 15:03:13 +0100155 if (fixed_node) {
Russell King8fa7b9b2017-12-01 10:25:09 +0000156 ret = fwnode_property_read_u32(fixed_node, "speed", &speed);
Russell King9525ae82017-07-25 15:03:13 +0100157
158 pl->link_config.speed = speed;
159 pl->link_config.duplex = DUPLEX_HALF;
160
Russell King8fa7b9b2017-12-01 10:25:09 +0000161 if (fwnode_property_read_bool(fixed_node, "full-duplex"))
Russell King9525ae82017-07-25 15:03:13 +0100162 pl->link_config.duplex = DUPLEX_FULL;
163
164 /* We treat the "pause" and "asym-pause" terminology as
165 * defining the link partner's ability. */
Russell King8fa7b9b2017-12-01 10:25:09 +0000166 if (fwnode_property_read_bool(fixed_node, "pause"))
Russell King9525ae82017-07-25 15:03:13 +0100167 pl->link_config.pause |= MLO_PAUSE_SYM;
Russell King8fa7b9b2017-12-01 10:25:09 +0000168 if (fwnode_property_read_bool(fixed_node, "asym-pause"))
Russell King9525ae82017-07-25 15:03:13 +0100169 pl->link_config.pause |= MLO_PAUSE_ASYM;
170
171 if (ret == 0) {
Russell King8fa7b9b2017-12-01 10:25:09 +0000172 desc = fwnode_get_named_gpiod(fixed_node, "link-gpios",
173 0, GPIOD_IN, "?");
Russell King9525ae82017-07-25 15:03:13 +0100174
175 if (!IS_ERR(desc))
176 pl->link_gpio = desc;
177 else if (desc == ERR_PTR(-EPROBE_DEFER))
178 ret = -EPROBE_DEFER;
179 }
Russell King8fa7b9b2017-12-01 10:25:09 +0000180 fwnode_handle_put(fixed_node);
Russell King9525ae82017-07-25 15:03:13 +0100181
182 if (ret)
183 return ret;
184 } else {
Russell King8fa7b9b2017-12-01 10:25:09 +0000185 u32 prop[5];
186
187 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
188 NULL, 0);
189 if (ret != ARRAY_SIZE(prop)) {
Russell King9525ae82017-07-25 15:03:13 +0100190 netdev_err(pl->netdev, "broken fixed-link?\n");
191 return -EINVAL;
192 }
Russell King8fa7b9b2017-12-01 10:25:09 +0000193
194 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
195 prop, ARRAY_SIZE(prop));
196 if (!ret) {
197 pl->link_config.duplex = prop[1] ?
Russell King9525ae82017-07-25 15:03:13 +0100198 DUPLEX_FULL : DUPLEX_HALF;
Russell King8fa7b9b2017-12-01 10:25:09 +0000199 pl->link_config.speed = prop[2];
200 if (prop[3])
Russell King9525ae82017-07-25 15:03:13 +0100201 pl->link_config.pause |= MLO_PAUSE_SYM;
Russell King8fa7b9b2017-12-01 10:25:09 +0000202 if (prop[4])
Russell King9525ae82017-07-25 15:03:13 +0100203 pl->link_config.pause |= MLO_PAUSE_ASYM;
204 }
205 }
206
207 if (pl->link_config.speed > SPEED_1000 &&
208 pl->link_config.duplex != DUPLEX_FULL)
209 netdev_warn(pl->netdev, "fixed link specifies half duplex for %dMbps link?\n",
210 pl->link_config.speed);
211
212 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
213 linkmode_copy(pl->link_config.advertising, pl->supported);
214 phylink_validate(pl, pl->supported, &pl->link_config);
215
216 s = phy_lookup_setting(pl->link_config.speed, pl->link_config.duplex,
217 pl->supported,
218 __ETHTOOL_LINK_MODE_MASK_NBITS, true);
219 linkmode_zero(pl->supported);
220 phylink_set(pl->supported, MII);
221 if (s) {
222 __set_bit(s->bit, pl->supported);
223 } else {
224 netdev_warn(pl->netdev, "fixed link %s duplex %dMbps not recognised\n",
225 pl->link_config.duplex == DUPLEX_FULL ? "full" : "half",
226 pl->link_config.speed);
227 }
228
229 linkmode_and(pl->link_config.advertising, pl->link_config.advertising,
230 pl->supported);
231
232 pl->link_config.link = 1;
233 pl->link_config.an_complete = 1;
234
235 return 0;
236}
237
Russell King8fa7b9b2017-12-01 10:25:09 +0000238static int phylink_parse_mode(struct phylink *pl, struct fwnode_handle *fwnode)
Russell King9525ae82017-07-25 15:03:13 +0100239{
Russell King8fa7b9b2017-12-01 10:25:09 +0000240 struct fwnode_handle *dn;
Russell King9525ae82017-07-25 15:03:13 +0100241 const char *managed;
242
Russell King8fa7b9b2017-12-01 10:25:09 +0000243 dn = fwnode_get_named_child_node(fwnode, "fixed-link");
244 if (dn || fwnode_property_present(fwnode, "fixed-link"))
Russell King9525ae82017-07-25 15:03:13 +0100245 pl->link_an_mode = MLO_AN_FIXED;
Russell King8fa7b9b2017-12-01 10:25:09 +0000246 fwnode_handle_put(dn);
Russell King9525ae82017-07-25 15:03:13 +0100247
Russell King8fa7b9b2017-12-01 10:25:09 +0000248 if (fwnode_property_read_string(fwnode, "managed", &managed) == 0 &&
Russell King9525ae82017-07-25 15:03:13 +0100249 strcmp(managed, "in-band-status") == 0) {
250 if (pl->link_an_mode == MLO_AN_FIXED) {
251 netdev_err(pl->netdev,
252 "can't use both fixed-link and in-band-status\n");
253 return -EINVAL;
254 }
255
256 linkmode_zero(pl->supported);
257 phylink_set(pl->supported, MII);
258 phylink_set(pl->supported, Autoneg);
259 phylink_set(pl->supported, Asym_Pause);
260 phylink_set(pl->supported, Pause);
261 pl->link_config.an_enabled = true;
Russell King86a362c2017-12-01 10:24:26 +0000262 pl->link_an_mode = MLO_AN_INBAND;
Russell King9525ae82017-07-25 15:03:13 +0100263
264 switch (pl->link_config.interface) {
265 case PHY_INTERFACE_MODE_SGMII:
266 phylink_set(pl->supported, 10baseT_Half);
267 phylink_set(pl->supported, 10baseT_Full);
268 phylink_set(pl->supported, 100baseT_Half);
269 phylink_set(pl->supported, 100baseT_Full);
270 phylink_set(pl->supported, 1000baseT_Half);
271 phylink_set(pl->supported, 1000baseT_Full);
Russell King9525ae82017-07-25 15:03:13 +0100272 break;
273
274 case PHY_INTERFACE_MODE_1000BASEX:
275 phylink_set(pl->supported, 1000baseX_Full);
Russell King9525ae82017-07-25 15:03:13 +0100276 break;
277
278 case PHY_INTERFACE_MODE_2500BASEX:
279 phylink_set(pl->supported, 2500baseX_Full);
Russell King9525ae82017-07-25 15:03:13 +0100280 break;
281
Russell Kingda7c1862017-07-25 15:03:34 +0100282 case PHY_INTERFACE_MODE_10GKR:
283 phylink_set(pl->supported, 10baseT_Half);
284 phylink_set(pl->supported, 10baseT_Full);
285 phylink_set(pl->supported, 100baseT_Half);
286 phylink_set(pl->supported, 100baseT_Full);
287 phylink_set(pl->supported, 1000baseT_Half);
288 phylink_set(pl->supported, 1000baseT_Full);
289 phylink_set(pl->supported, 1000baseX_Full);
290 phylink_set(pl->supported, 10000baseKR_Full);
291 phylink_set(pl->supported, 10000baseCR_Full);
292 phylink_set(pl->supported, 10000baseSR_Full);
293 phylink_set(pl->supported, 10000baseLR_Full);
294 phylink_set(pl->supported, 10000baseLRM_Full);
295 phylink_set(pl->supported, 10000baseER_Full);
Russell Kingda7c1862017-07-25 15:03:34 +0100296 break;
297
Russell King9525ae82017-07-25 15:03:13 +0100298 default:
299 netdev_err(pl->netdev,
300 "incorrect link mode %s for in-band status\n",
301 phy_modes(pl->link_config.interface));
302 return -EINVAL;
303 }
304
305 linkmode_copy(pl->link_config.advertising, pl->supported);
306
307 if (phylink_validate(pl, pl->supported, &pl->link_config)) {
308 netdev_err(pl->netdev,
309 "failed to validate link configuration for in-band status\n");
310 return -EINVAL;
311 }
312 }
313
314 return 0;
315}
316
317static void phylink_mac_config(struct phylink *pl,
318 const struct phylink_link_state *state)
319{
320 netdev_dbg(pl->netdev,
321 "%s: mode=%s/%s/%s/%s adv=%*pb pause=%02x link=%u an=%u\n",
322 __func__, phylink_an_mode_str(pl->link_an_mode),
323 phy_modes(state->interface),
324 phy_speed_to_str(state->speed),
325 phy_duplex_to_str(state->duplex),
326 __ETHTOOL_LINK_MODE_MASK_NBITS, state->advertising,
327 state->pause, state->link, state->an_enabled);
328
329 pl->ops->mac_config(pl->netdev, pl->link_an_mode, state);
330}
331
332static void phylink_mac_an_restart(struct phylink *pl)
333{
334 if (pl->link_config.an_enabled &&
Russell King365c1e62017-12-01 10:24:16 +0000335 phy_interface_mode_is_8023z(pl->link_config.interface))
Russell King9525ae82017-07-25 15:03:13 +0100336 pl->ops->mac_an_restart(pl->netdev);
337}
338
339static int phylink_get_mac_state(struct phylink *pl, struct phylink_link_state *state)
340{
341 struct net_device *ndev = pl->netdev;
342
343 linkmode_copy(state->advertising, pl->link_config.advertising);
344 linkmode_zero(state->lp_advertising);
345 state->interface = pl->link_config.interface;
346 state->an_enabled = pl->link_config.an_enabled;
347 state->link = 1;
348
349 return pl->ops->mac_link_state(ndev, state);
350}
351
352/* The fixed state is... fixed except for the link state,
353 * which may be determined by a GPIO.
354 */
355static void phylink_get_fixed_state(struct phylink *pl, struct phylink_link_state *state)
356{
357 *state = pl->link_config;
358 if (pl->link_gpio)
359 state->link = !!gpiod_get_value(pl->link_gpio);
360}
361
362/* Flow control is resolved according to our and the link partners
363 * advertisments using the following drawn from the 802.3 specs:
364 * Local device Link partner
365 * Pause AsymDir Pause AsymDir Result
366 * 1 X 1 X TX+RX
367 * 0 1 1 1 RX
368 * 1 1 0 1 TX
369 */
370static void phylink_resolve_flow(struct phylink *pl,
Florian Fainelli516b29e2017-10-30 21:42:57 -0700371 struct phylink_link_state *state)
Russell King9525ae82017-07-25 15:03:13 +0100372{
373 int new_pause = 0;
374
375 if (pl->link_config.pause & MLO_PAUSE_AN) {
376 int pause = 0;
377
378 if (phylink_test(pl->link_config.advertising, Pause))
379 pause |= MLO_PAUSE_SYM;
380 if (phylink_test(pl->link_config.advertising, Asym_Pause))
381 pause |= MLO_PAUSE_ASYM;
382
383 pause &= state->pause;
384
385 if (pause & MLO_PAUSE_SYM)
386 new_pause = MLO_PAUSE_TX | MLO_PAUSE_RX;
387 else if (pause & MLO_PAUSE_ASYM)
388 new_pause = state->pause & MLO_PAUSE_SYM ?
389 MLO_PAUSE_RX : MLO_PAUSE_TX;
390 } else {
391 new_pause = pl->link_config.pause & MLO_PAUSE_TXRX_MASK;
392 }
393
394 state->pause &= ~MLO_PAUSE_TXRX_MASK;
395 state->pause |= new_pause;
396}
397
398static const char *phylink_pause_to_str(int pause)
399{
400 switch (pause & MLO_PAUSE_TXRX_MASK) {
401 case MLO_PAUSE_TX | MLO_PAUSE_RX:
402 return "rx/tx";
403 case MLO_PAUSE_TX:
404 return "tx";
405 case MLO_PAUSE_RX:
406 return "rx";
407 default:
408 return "off";
409 }
410}
411
412static void phylink_resolve(struct work_struct *w)
413{
414 struct phylink *pl = container_of(w, struct phylink, resolve);
415 struct phylink_link_state link_state;
416 struct net_device *ndev = pl->netdev;
417
418 mutex_lock(&pl->state_mutex);
419 if (pl->phylink_disable_state) {
420 pl->mac_link_dropped = false;
421 link_state.link = false;
422 } else if (pl->mac_link_dropped) {
423 link_state.link = false;
424 } else {
425 switch (pl->link_an_mode) {
426 case MLO_AN_PHY:
427 link_state = pl->phy_state;
428 phylink_resolve_flow(pl, &link_state);
429 phylink_mac_config(pl, &link_state);
430 break;
431
432 case MLO_AN_FIXED:
433 phylink_get_fixed_state(pl, &link_state);
434 phylink_mac_config(pl, &link_state);
435 break;
436
Russell King86a362c2017-12-01 10:24:26 +0000437 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +0100438 phylink_get_mac_state(pl, &link_state);
439 if (pl->phydev) {
440 bool changed = false;
441
442 link_state.link = link_state.link &&
443 pl->phy_state.link;
444
445 if (pl->phy_state.interface !=
446 link_state.interface) {
447 link_state.interface = pl->phy_state.interface;
448 changed = true;
449 }
450
451 /* Propagate the flow control from the PHY
452 * to the MAC. Also propagate the interface
453 * if changed.
454 */
455 if (pl->phy_state.link || changed) {
456 link_state.pause |= pl->phy_state.pause;
457 phylink_resolve_flow(pl, &link_state);
458
459 phylink_mac_config(pl, &link_state);
460 }
461 }
462 break;
Russell King9525ae82017-07-25 15:03:13 +0100463 }
464 }
465
466 if (link_state.link != netif_carrier_ok(ndev)) {
467 if (!link_state.link) {
468 netif_carrier_off(ndev);
469 pl->ops->mac_link_down(ndev, pl->link_an_mode);
470 netdev_info(ndev, "Link is Down\n");
471 } else {
472 pl->ops->mac_link_up(ndev, pl->link_an_mode,
473 pl->phydev);
474
475 netif_carrier_on(ndev);
476
477 netdev_info(ndev,
478 "Link is Up - %s/%s - flow control %s\n",
479 phy_speed_to_str(link_state.speed),
480 phy_duplex_to_str(link_state.duplex),
481 phylink_pause_to_str(link_state.pause));
482 }
483 }
484 if (!link_state.link && pl->mac_link_dropped) {
485 pl->mac_link_dropped = false;
486 queue_work(system_power_efficient_wq, &pl->resolve);
487 }
488 mutex_unlock(&pl->state_mutex);
489}
490
491static void phylink_run_resolve(struct phylink *pl)
492{
493 if (!pl->phylink_disable_state)
494 queue_work(system_power_efficient_wq, &pl->resolve);
495}
496
Russell Kingce0aa272017-07-25 15:03:18 +0100497static const struct sfp_upstream_ops sfp_phylink_ops;
498
Russell King8fa7b9b2017-12-01 10:25:09 +0000499static int phylink_register_sfp(struct phylink *pl,
500 struct fwnode_handle *fwnode)
Russell Kingce0aa272017-07-25 15:03:18 +0100501{
Russell King8fa7b9b2017-12-01 10:25:09 +0000502 struct fwnode_reference_args ref;
503 int ret;
Russell Kingce0aa272017-07-25 15:03:18 +0100504
Russell King8fa7b9b2017-12-01 10:25:09 +0000505 ret = fwnode_property_get_reference_args(fwnode, "sfp", NULL,
506 0, 0, &ref);
507 if (ret < 0) {
508 if (ret == -ENOENT)
509 return 0;
Russell Kingce0aa272017-07-25 15:03:18 +0100510
Russell King8fa7b9b2017-12-01 10:25:09 +0000511 netdev_err(pl->netdev, "unable to parse \"sfp\" node: %d\n",
512 ret);
513 return ret;
514 }
515
516 pl->sfp_bus = sfp_register_upstream(ref.fwnode, pl->netdev, pl,
Russell Kingce0aa272017-07-25 15:03:18 +0100517 &sfp_phylink_ops);
518 if (!pl->sfp_bus)
519 return -ENOMEM;
520
521 return 0;
522}
523
Russell King8796c892017-12-01 10:24:47 +0000524/**
525 * phylink_create() - create a phylink instance
526 * @ndev: a pointer to the &struct net_device
Russell King8fa7b9b2017-12-01 10:25:09 +0000527 * @fwnode: a pointer to a &struct fwnode_handle describing the network
528 * interface
Russell King8796c892017-12-01 10:24:47 +0000529 * @iface: the desired link mode defined by &typedef phy_interface_t
530 * @ops: a pointer to a &struct phylink_mac_ops for the MAC.
531 *
532 * Create a new phylink instance, and parse the link parameters found in @np.
533 * This will parse in-band modes, fixed-link or SFP configuration.
534 *
535 * Returns a pointer to a &struct phylink, or an error-pointer value. Users
536 * must use IS_ERR() to check for errors from this function.
537 */
Russell King8fa7b9b2017-12-01 10:25:09 +0000538struct phylink *phylink_create(struct net_device *ndev,
539 struct fwnode_handle *fwnode,
Florian Fainelli516b29e2017-10-30 21:42:57 -0700540 phy_interface_t iface,
541 const struct phylink_mac_ops *ops)
Russell King9525ae82017-07-25 15:03:13 +0100542{
543 struct phylink *pl;
544 int ret;
545
546 pl = kzalloc(sizeof(*pl), GFP_KERNEL);
547 if (!pl)
548 return ERR_PTR(-ENOMEM);
549
550 mutex_init(&pl->state_mutex);
551 INIT_WORK(&pl->resolve, phylink_resolve);
552 pl->netdev = ndev;
553 pl->phy_state.interface = iface;
554 pl->link_interface = iface;
555 pl->link_port = PORT_MII;
556 pl->link_config.interface = iface;
557 pl->link_config.pause = MLO_PAUSE_AN;
558 pl->link_config.speed = SPEED_UNKNOWN;
559 pl->link_config.duplex = DUPLEX_UNKNOWN;
560 pl->ops = ops;
561 __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
562
563 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
564 linkmode_copy(pl->link_config.advertising, pl->supported);
565 phylink_validate(pl, pl->supported, &pl->link_config);
566
Russell King8fa7b9b2017-12-01 10:25:09 +0000567 ret = phylink_parse_mode(pl, fwnode);
Russell King9525ae82017-07-25 15:03:13 +0100568 if (ret < 0) {
569 kfree(pl);
570 return ERR_PTR(ret);
571 }
572
573 if (pl->link_an_mode == MLO_AN_FIXED) {
Russell King8fa7b9b2017-12-01 10:25:09 +0000574 ret = phylink_parse_fixedlink(pl, fwnode);
Russell King9525ae82017-07-25 15:03:13 +0100575 if (ret < 0) {
576 kfree(pl);
577 return ERR_PTR(ret);
578 }
579 }
580
Russell King8fa7b9b2017-12-01 10:25:09 +0000581 ret = phylink_register_sfp(pl, fwnode);
Russell Kingce0aa272017-07-25 15:03:18 +0100582 if (ret < 0) {
583 kfree(pl);
584 return ERR_PTR(ret);
585 }
586
Russell King9525ae82017-07-25 15:03:13 +0100587 return pl;
588}
589EXPORT_SYMBOL_GPL(phylink_create);
590
Russell King8796c892017-12-01 10:24:47 +0000591/**
592 * phylink_destroy() - cleanup and destroy the phylink instance
593 * @pl: a pointer to a &struct phylink returned from phylink_create()
594 *
595 * Destroy a phylink instance. Any PHY that has been attached must have been
596 * cleaned up via phylink_disconnect_phy() prior to calling this function.
597 */
Russell King9525ae82017-07-25 15:03:13 +0100598void phylink_destroy(struct phylink *pl)
599{
Russell Kingce0aa272017-07-25 15:03:18 +0100600 if (pl->sfp_bus)
601 sfp_unregister_upstream(pl->sfp_bus);
602
Russell King9525ae82017-07-25 15:03:13 +0100603 cancel_work_sync(&pl->resolve);
604 kfree(pl);
605}
606EXPORT_SYMBOL_GPL(phylink_destroy);
607
Wei Yongjun1ec6e532017-11-02 11:14:48 +0000608static void phylink_phy_change(struct phy_device *phydev, bool up,
609 bool do_carrier)
Russell King9525ae82017-07-25 15:03:13 +0100610{
611 struct phylink *pl = phydev->phylink;
612
613 mutex_lock(&pl->state_mutex);
614 pl->phy_state.speed = phydev->speed;
615 pl->phy_state.duplex = phydev->duplex;
616 pl->phy_state.pause = MLO_PAUSE_NONE;
617 if (phydev->pause)
618 pl->phy_state.pause |= MLO_PAUSE_SYM;
619 if (phydev->asym_pause)
620 pl->phy_state.pause |= MLO_PAUSE_ASYM;
621 pl->phy_state.interface = phydev->interface;
622 pl->phy_state.link = up;
623 mutex_unlock(&pl->state_mutex);
624
625 phylink_run_resolve(pl);
626
627 netdev_dbg(pl->netdev, "phy link %s %s/%s/%s\n", up ? "up" : "down",
Florian Fainelli516b29e2017-10-30 21:42:57 -0700628 phy_modes(phydev->interface),
Russell King9525ae82017-07-25 15:03:13 +0100629 phy_speed_to_str(phydev->speed),
630 phy_duplex_to_str(phydev->duplex));
631}
632
633static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy)
634{
635 struct phylink_link_state config;
636 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
637 u32 advertising;
638 int ret;
639
640 memset(&config, 0, sizeof(config));
641 ethtool_convert_legacy_u32_to_link_mode(supported, phy->supported);
642 ethtool_convert_legacy_u32_to_link_mode(config.advertising,
643 phy->advertising);
644 config.interface = pl->link_config.interface;
645
646 /*
647 * This is the new way of dealing with flow control for PHYs,
648 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
649 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
650 * using our validate call to the MAC, we rely upon the MAC
651 * clearing the bits from both supported and advertising fields.
652 */
653 if (phylink_test(supported, Pause))
654 phylink_set(config.advertising, Pause);
655 if (phylink_test(supported, Asym_Pause))
656 phylink_set(config.advertising, Asym_Pause);
657
658 ret = phylink_validate(pl, supported, &config);
659 if (ret)
660 return ret;
661
662 phy->phylink = pl;
663 phy->phy_link_change = phylink_phy_change;
664
665 netdev_info(pl->netdev,
666 "PHY [%s] driver [%s]\n", dev_name(&phy->mdio.dev),
667 phy->drv->name);
668
669 mutex_lock(&phy->lock);
670 mutex_lock(&pl->state_mutex);
671 pl->netdev->phydev = phy;
672 pl->phydev = phy;
673 linkmode_copy(pl->supported, supported);
674 linkmode_copy(pl->link_config.advertising, config.advertising);
675
676 /* Restrict the phy advertisment according to the MAC support. */
677 ethtool_convert_link_mode_to_legacy_u32(&advertising, config.advertising);
678 phy->advertising = advertising;
679 mutex_unlock(&pl->state_mutex);
680 mutex_unlock(&phy->lock);
681
682 netdev_dbg(pl->netdev,
683 "phy: setting supported %*pb advertising 0x%08x\n",
684 __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
685 phy->advertising);
686
687 phy_start_machine(phy);
688 if (phy->irq > 0)
689 phy_start_interrupts(phy);
690
691 return 0;
692}
693
Russell King8796c892017-12-01 10:24:47 +0000694/**
695 * phylink_connect_phy() - connect a PHY to the phylink instance
696 * @pl: a pointer to a &struct phylink returned from phylink_create()
697 * @phy: a pointer to a &struct phy_device.
698 *
699 * Connect @phy to the phylink instance specified by @pl by calling
700 * phy_attach_direct(). Configure the @phy according to the MAC driver's
701 * capabilities, start the PHYLIB state machine and enable any interrupts
702 * that the PHY supports.
703 *
704 * This updates the phylink's ethtool supported and advertising link mode
705 * masks.
706 *
707 * Returns 0 on success or a negative errno.
708 */
Russell King9525ae82017-07-25 15:03:13 +0100709int phylink_connect_phy(struct phylink *pl, struct phy_device *phy)
710{
711 int ret;
712
Russell Kingcf4f2672017-12-01 10:24:21 +0000713 if (WARN_ON(pl->link_an_mode == MLO_AN_FIXED ||
Russell King86a362c2017-12-01 10:24:26 +0000714 (pl->link_an_mode == MLO_AN_INBAND &&
715 phy_interface_mode_is_8023z(pl->link_interface))))
Russell Kingcf4f2672017-12-01 10:24:21 +0000716 return -EINVAL;
717
Florian Fainelli4904b6e2017-12-12 16:00:26 -0800718 /* Use PHY device/driver interface */
719 if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
720 pl->link_interface = phy->interface;
721 pl->link_config.interface = pl->link_interface;
722 }
723
Russell King9525ae82017-07-25 15:03:13 +0100724 ret = phy_attach_direct(pl->netdev, phy, 0, pl->link_interface);
725 if (ret)
726 return ret;
727
728 ret = phylink_bringup_phy(pl, phy);
729 if (ret)
730 phy_detach(phy);
731
732 return ret;
733}
734EXPORT_SYMBOL_GPL(phylink_connect_phy);
735
Russell King8796c892017-12-01 10:24:47 +0000736/**
737 * phylink_of_phy_connect() - connect the PHY specified in the DT mode.
738 * @pl: a pointer to a &struct phylink returned from phylink_create()
739 * @dn: a pointer to a &struct device_node.
Florian Fainelli0a629642017-12-12 16:00:25 -0800740 * @flags: PHY-specific flags to communicate to the PHY device driver
Russell King8796c892017-12-01 10:24:47 +0000741 *
742 * Connect the phy specified in the device node @dn to the phylink instance
743 * specified by @pl. Actions specified in phylink_connect_phy() will be
744 * performed.
745 *
746 * Returns 0 on success or a negative errno.
747 */
Florian Fainelli0a629642017-12-12 16:00:25 -0800748int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn,
749 u32 flags)
Russell King9525ae82017-07-25 15:03:13 +0100750{
751 struct device_node *phy_node;
752 struct phy_device *phy_dev;
753 int ret;
754
Russell Kingcf4f2672017-12-01 10:24:21 +0000755 /* Fixed links and 802.3z are handled without needing a PHY */
756 if (pl->link_an_mode == MLO_AN_FIXED ||
Russell King86a362c2017-12-01 10:24:26 +0000757 (pl->link_an_mode == MLO_AN_INBAND &&
758 phy_interface_mode_is_8023z(pl->link_interface)))
Russell King9525ae82017-07-25 15:03:13 +0100759 return 0;
760
761 phy_node = of_parse_phandle(dn, "phy-handle", 0);
762 if (!phy_node)
763 phy_node = of_parse_phandle(dn, "phy", 0);
764 if (!phy_node)
765 phy_node = of_parse_phandle(dn, "phy-device", 0);
766
767 if (!phy_node) {
768 if (pl->link_an_mode == MLO_AN_PHY) {
769 netdev_err(pl->netdev, "unable to find PHY node\n");
770 return -ENODEV;
771 }
772 return 0;
773 }
774
Florian Fainelli0a629642017-12-12 16:00:25 -0800775 phy_dev = of_phy_attach(pl->netdev, phy_node, flags,
776 pl->link_interface);
Russell King9525ae82017-07-25 15:03:13 +0100777 /* We're done with the phy_node handle */
778 of_node_put(phy_node);
779
780 if (!phy_dev)
781 return -ENODEV;
782
783 ret = phylink_bringup_phy(pl, phy_dev);
784 if (ret)
785 phy_detach(phy_dev);
786
787 return ret;
788}
789EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
790
Russell King8796c892017-12-01 10:24:47 +0000791/**
792 * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
793 * instance.
794 * @pl: a pointer to a &struct phylink returned from phylink_create()
795 *
796 * Disconnect any current PHY from the phylink instance described by @pl.
797 */
Russell King9525ae82017-07-25 15:03:13 +0100798void phylink_disconnect_phy(struct phylink *pl)
799{
800 struct phy_device *phy;
801
802 WARN_ON(!lockdep_rtnl_is_held());
803
804 phy = pl->phydev;
805 if (phy) {
806 mutex_lock(&phy->lock);
807 mutex_lock(&pl->state_mutex);
808 pl->netdev->phydev = NULL;
809 pl->phydev = NULL;
810 mutex_unlock(&pl->state_mutex);
811 mutex_unlock(&phy->lock);
812 flush_work(&pl->resolve);
813
814 phy_disconnect(phy);
815 }
816}
817EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
818
Russell King8796c892017-12-01 10:24:47 +0000819/**
820 * phylink_mac_change() - notify phylink of a change in MAC state
821 * @pl: a pointer to a &struct phylink returned from phylink_create()
822 * @up: indicates whether the link is currently up.
823 *
824 * The MAC driver should call this driver when the state of its link
825 * changes (eg, link failure, new negotiation results, etc.)
826 */
Russell King9525ae82017-07-25 15:03:13 +0100827void phylink_mac_change(struct phylink *pl, bool up)
828{
829 if (!up)
830 pl->mac_link_dropped = true;
831 phylink_run_resolve(pl);
832 netdev_dbg(pl->netdev, "mac link %s\n", up ? "up" : "down");
833}
834EXPORT_SYMBOL_GPL(phylink_mac_change);
835
Russell King8796c892017-12-01 10:24:47 +0000836/**
837 * phylink_start() - start a phylink instance
838 * @pl: a pointer to a &struct phylink returned from phylink_create()
839 *
840 * Start the phylink instance specified by @pl, configuring the MAC for the
841 * desired link mode(s) and negotiation style. This should be called from the
842 * network device driver's &struct net_device_ops ndo_open() method.
843 */
Russell King9525ae82017-07-25 15:03:13 +0100844void phylink_start(struct phylink *pl)
845{
846 WARN_ON(!lockdep_rtnl_is_held());
847
848 netdev_info(pl->netdev, "configuring for %s/%s link mode\n",
849 phylink_an_mode_str(pl->link_an_mode),
850 phy_modes(pl->link_config.interface));
851
852 /* Apply the link configuration to the MAC when starting. This allows
853 * a fixed-link to start with the correct parameters, and also
854 * ensures that we set the appropriate advertisment for Serdes links.
855 */
856 phylink_resolve_flow(pl, &pl->link_config);
857 phylink_mac_config(pl, &pl->link_config);
858
Russell King85b43942017-12-01 10:24:42 +0000859 /* Restart autonegotiation if using 802.3z to ensure that the link
860 * parameters are properly negotiated. This is necessary for DSA
861 * switches using 802.3z negotiation to ensure they see our modes.
862 */
863 phylink_mac_an_restart(pl);
864
Russell King9525ae82017-07-25 15:03:13 +0100865 clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
866 phylink_run_resolve(pl);
867
Russell Kingce0aa272017-07-25 15:03:18 +0100868 if (pl->sfp_bus)
869 sfp_upstream_start(pl->sfp_bus);
Russell King9525ae82017-07-25 15:03:13 +0100870 if (pl->phydev)
871 phy_start(pl->phydev);
872}
873EXPORT_SYMBOL_GPL(phylink_start);
874
Russell King8796c892017-12-01 10:24:47 +0000875/**
876 * phylink_stop() - stop a phylink instance
877 * @pl: a pointer to a &struct phylink returned from phylink_create()
878 *
879 * Stop the phylink instance specified by @pl. This should be called from the
880 * network device driver's &struct net_device_ops ndo_stop() method. The
881 * network device's carrier state should not be changed prior to calling this
882 * function.
883 */
Russell King9525ae82017-07-25 15:03:13 +0100884void phylink_stop(struct phylink *pl)
885{
886 WARN_ON(!lockdep_rtnl_is_held());
887
888 if (pl->phydev)
889 phy_stop(pl->phydev);
Russell Kingce0aa272017-07-25 15:03:18 +0100890 if (pl->sfp_bus)
891 sfp_upstream_stop(pl->sfp_bus);
Russell King9525ae82017-07-25 15:03:13 +0100892
893 set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
Russell King2012b7d2017-11-30 13:59:26 +0000894 queue_work(system_power_efficient_wq, &pl->resolve);
Russell King9525ae82017-07-25 15:03:13 +0100895 flush_work(&pl->resolve);
896}
897EXPORT_SYMBOL_GPL(phylink_stop);
898
Russell King8796c892017-12-01 10:24:47 +0000899/**
900 * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY
901 * @pl: a pointer to a &struct phylink returned from phylink_create()
902 * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters
903 *
904 * Read the wake on lan parameters from the PHY attached to the phylink
905 * instance specified by @pl. If no PHY is currently attached, report no
906 * support for wake on lan.
907 */
Russell King9525ae82017-07-25 15:03:13 +0100908void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
909{
910 WARN_ON(!lockdep_rtnl_is_held());
911
912 wol->supported = 0;
913 wol->wolopts = 0;
914
915 if (pl->phydev)
916 phy_ethtool_get_wol(pl->phydev, wol);
917}
918EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol);
919
Russell King8796c892017-12-01 10:24:47 +0000920/**
921 * phylink_ethtool_set_wol() - set wake on lan parameters
922 * @pl: a pointer to a &struct phylink returned from phylink_create()
923 * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters
924 *
925 * Set the wake on lan parameters for the PHY attached to the phylink
926 * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP
927 * error.
928 *
929 * Returns zero on success or negative errno code.
930 */
Russell King9525ae82017-07-25 15:03:13 +0100931int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
932{
933 int ret = -EOPNOTSUPP;
934
935 WARN_ON(!lockdep_rtnl_is_held());
936
937 if (pl->phydev)
938 ret = phy_ethtool_set_wol(pl->phydev, wol);
939
940 return ret;
941}
942EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol);
943
944static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b)
945{
946 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
947
948 linkmode_zero(mask);
949 phylink_set_port_modes(mask);
950
951 linkmode_and(dst, dst, mask);
952 linkmode_or(dst, dst, b);
953}
954
955static void phylink_get_ksettings(const struct phylink_link_state *state,
956 struct ethtool_link_ksettings *kset)
957{
958 phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
959 linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
960 kset->base.speed = state->speed;
961 kset->base.duplex = state->duplex;
962 kset->base.autoneg = state->an_enabled ? AUTONEG_ENABLE :
963 AUTONEG_DISABLE;
964}
965
Russell King8796c892017-12-01 10:24:47 +0000966/**
967 * phylink_ethtool_ksettings_get() - get the current link settings
968 * @pl: a pointer to a &struct phylink returned from phylink_create()
969 * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings
970 *
971 * Read the current link settings for the phylink instance specified by @pl.
972 * This will be the link settings read from the MAC, PHY or fixed link
973 * settings depending on the current negotiation mode.
974 */
Russell King9525ae82017-07-25 15:03:13 +0100975int phylink_ethtool_ksettings_get(struct phylink *pl,
Florian Fainelli516b29e2017-10-30 21:42:57 -0700976 struct ethtool_link_ksettings *kset)
Russell King9525ae82017-07-25 15:03:13 +0100977{
978 struct phylink_link_state link_state;
979
980 WARN_ON(!lockdep_rtnl_is_held());
981
982 if (pl->phydev) {
983 phy_ethtool_ksettings_get(pl->phydev, kset);
984 } else {
985 kset->base.port = pl->link_port;
986 }
987
988 linkmode_copy(kset->link_modes.supported, pl->supported);
989
990 switch (pl->link_an_mode) {
991 case MLO_AN_FIXED:
992 /* We are using fixed settings. Report these as the
993 * current link settings - and note that these also
994 * represent the supported speeds/duplex/pause modes.
995 */
996 phylink_get_fixed_state(pl, &link_state);
997 phylink_get_ksettings(&link_state, kset);
998 break;
999
Russell King86a362c2017-12-01 10:24:26 +00001000 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001001 /* If there is a phy attached, then use the reported
1002 * settings from the phy with no modification.
1003 */
1004 if (pl->phydev)
1005 break;
1006
Russell King9525ae82017-07-25 15:03:13 +01001007 phylink_get_mac_state(pl, &link_state);
1008
1009 /* The MAC is reporting the link results from its own PCS
1010 * layer via in-band status. Report these as the current
1011 * link settings.
1012 */
1013 phylink_get_ksettings(&link_state, kset);
1014 break;
1015 }
1016
1017 return 0;
1018}
1019EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get);
1020
Russell King8796c892017-12-01 10:24:47 +00001021/**
1022 * phylink_ethtool_ksettings_set() - set the link settings
1023 * @pl: a pointer to a &struct phylink returned from phylink_create()
1024 * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes
1025 */
Russell King9525ae82017-07-25 15:03:13 +01001026int phylink_ethtool_ksettings_set(struct phylink *pl,
Florian Fainelli516b29e2017-10-30 21:42:57 -07001027 const struct ethtool_link_ksettings *kset)
Russell King9525ae82017-07-25 15:03:13 +01001028{
1029 struct ethtool_link_ksettings our_kset;
1030 struct phylink_link_state config;
1031 int ret;
1032
1033 WARN_ON(!lockdep_rtnl_is_held());
1034
1035 if (kset->base.autoneg != AUTONEG_DISABLE &&
1036 kset->base.autoneg != AUTONEG_ENABLE)
1037 return -EINVAL;
1038
1039 config = pl->link_config;
1040
1041 /* Mask out unsupported advertisments */
1042 linkmode_and(config.advertising, kset->link_modes.advertising,
1043 pl->supported);
1044
1045 /* FIXME: should we reject autoneg if phy/mac does not support it? */
1046 if (kset->base.autoneg == AUTONEG_DISABLE) {
1047 const struct phy_setting *s;
1048
1049 /* Autonegotiation disabled, select a suitable speed and
1050 * duplex.
1051 */
1052 s = phy_lookup_setting(kset->base.speed, kset->base.duplex,
1053 pl->supported,
1054 __ETHTOOL_LINK_MODE_MASK_NBITS, false);
1055 if (!s)
1056 return -EINVAL;
1057
1058 /* If we have a fixed link (as specified by firmware), refuse
1059 * to change link parameters.
1060 */
1061 if (pl->link_an_mode == MLO_AN_FIXED &&
1062 (s->speed != pl->link_config.speed ||
1063 s->duplex != pl->link_config.duplex))
1064 return -EINVAL;
1065
1066 config.speed = s->speed;
1067 config.duplex = s->duplex;
1068 config.an_enabled = false;
1069
1070 __clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
1071 } else {
1072 /* If we have a fixed link, refuse to enable autonegotiation */
1073 if (pl->link_an_mode == MLO_AN_FIXED)
1074 return -EINVAL;
1075
1076 config.speed = SPEED_UNKNOWN;
1077 config.duplex = DUPLEX_UNKNOWN;
1078 config.an_enabled = true;
1079
1080 __set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
1081 }
1082
1083 if (phylink_validate(pl, pl->supported, &config))
1084 return -EINVAL;
1085
1086 /* If autonegotiation is enabled, we must have an advertisment */
1087 if (config.an_enabled && phylink_is_empty_linkmode(config.advertising))
1088 return -EINVAL;
1089
1090 our_kset = *kset;
1091 linkmode_copy(our_kset.link_modes.advertising, config.advertising);
1092 our_kset.base.speed = config.speed;
1093 our_kset.base.duplex = config.duplex;
1094
1095 /* If we have a PHY, configure the phy */
1096 if (pl->phydev) {
1097 ret = phy_ethtool_ksettings_set(pl->phydev, &our_kset);
1098 if (ret)
1099 return ret;
1100 }
1101
1102 mutex_lock(&pl->state_mutex);
1103 /* Configure the MAC to match the new settings */
1104 linkmode_copy(pl->link_config.advertising, our_kset.link_modes.advertising);
1105 pl->link_config.speed = our_kset.base.speed;
1106 pl->link_config.duplex = our_kset.base.duplex;
1107 pl->link_config.an_enabled = our_kset.base.autoneg != AUTONEG_DISABLE;
1108
1109 if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
1110 phylink_mac_config(pl, &pl->link_config);
1111 phylink_mac_an_restart(pl);
1112 }
1113 mutex_unlock(&pl->state_mutex);
1114
Dan Carpenterd18c2a12017-08-10 00:35:50 +03001115 return 0;
Russell King9525ae82017-07-25 15:03:13 +01001116}
1117EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
1118
Russell King8796c892017-12-01 10:24:47 +00001119/**
1120 * phylink_ethtool_nway_reset() - restart negotiation
1121 * @pl: a pointer to a &struct phylink returned from phylink_create()
1122 *
1123 * Restart negotiation for the phylink instance specified by @pl. This will
1124 * cause any attached phy to restart negotiation with the link partner, and
1125 * if the MAC is in a BaseX mode, the MAC will also be requested to restart
1126 * negotiation.
1127 *
1128 * Returns zero on success, or negative error code.
1129 */
Russell King9525ae82017-07-25 15:03:13 +01001130int phylink_ethtool_nway_reset(struct phylink *pl)
1131{
1132 int ret = 0;
1133
1134 WARN_ON(!lockdep_rtnl_is_held());
1135
1136 if (pl->phydev)
1137 ret = phy_restart_aneg(pl->phydev);
1138 phylink_mac_an_restart(pl);
1139
1140 return ret;
1141}
1142EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
1143
Russell King8796c892017-12-01 10:24:47 +00001144/**
1145 * phylink_ethtool_get_pauseparam() - get the current pause parameters
1146 * @pl: a pointer to a &struct phylink returned from phylink_create()
1147 * @pause: a pointer to a &struct ethtool_pauseparam
1148 */
Russell King9525ae82017-07-25 15:03:13 +01001149void phylink_ethtool_get_pauseparam(struct phylink *pl,
1150 struct ethtool_pauseparam *pause)
1151{
1152 WARN_ON(!lockdep_rtnl_is_held());
1153
1154 pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
1155 pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
1156 pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
1157}
1158EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
1159
Russell King8796c892017-12-01 10:24:47 +00001160/**
1161 * phylink_ethtool_set_pauseparam() - set the current pause parameters
1162 * @pl: a pointer to a &struct phylink returned from phylink_create()
1163 * @pause: a pointer to a &struct ethtool_pauseparam
1164 */
Russell King9525ae82017-07-25 15:03:13 +01001165int phylink_ethtool_set_pauseparam(struct phylink *pl,
1166 struct ethtool_pauseparam *pause)
1167{
1168 struct phylink_link_state *config = &pl->link_config;
1169
1170 WARN_ON(!lockdep_rtnl_is_held());
1171
1172 if (!phylink_test(pl->supported, Pause) &&
1173 !phylink_test(pl->supported, Asym_Pause))
1174 return -EOPNOTSUPP;
1175
1176 if (!phylink_test(pl->supported, Asym_Pause) &&
1177 !pause->autoneg && pause->rx_pause != pause->tx_pause)
1178 return -EINVAL;
1179
1180 config->pause &= ~(MLO_PAUSE_AN | MLO_PAUSE_TXRX_MASK);
1181
1182 if (pause->autoneg)
1183 config->pause |= MLO_PAUSE_AN;
1184 if (pause->rx_pause)
1185 config->pause |= MLO_PAUSE_RX;
1186 if (pause->tx_pause)
1187 config->pause |= MLO_PAUSE_TX;
1188
1189 if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
1190 switch (pl->link_an_mode) {
1191 case MLO_AN_PHY:
1192 /* Silently mark the carrier down, and then trigger a resolve */
1193 netif_carrier_off(pl->netdev);
1194 phylink_run_resolve(pl);
1195 break;
1196
1197 case MLO_AN_FIXED:
1198 /* Should we allow fixed links to change against the config? */
1199 phylink_resolve_flow(pl, config);
1200 phylink_mac_config(pl, config);
1201 break;
1202
Russell King86a362c2017-12-01 10:24:26 +00001203 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001204 phylink_mac_config(pl, config);
1205 phylink_mac_an_restart(pl);
1206 break;
1207 }
1208 }
1209
1210 return 0;
1211}
1212EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
1213
Russell King770a1ad2017-07-25 15:03:23 +01001214int phylink_ethtool_get_module_info(struct phylink *pl,
1215 struct ethtool_modinfo *modinfo)
1216{
1217 int ret = -EOPNOTSUPP;
1218
1219 WARN_ON(!lockdep_rtnl_is_held());
1220
1221 if (pl->sfp_bus)
1222 ret = sfp_get_module_info(pl->sfp_bus, modinfo);
1223
1224 return ret;
1225}
1226EXPORT_SYMBOL_GPL(phylink_ethtool_get_module_info);
1227
1228int phylink_ethtool_get_module_eeprom(struct phylink *pl,
1229 struct ethtool_eeprom *ee, u8 *buf)
1230{
1231 int ret = -EOPNOTSUPP;
1232
1233 WARN_ON(!lockdep_rtnl_is_held());
1234
1235 if (pl->sfp_bus)
1236 ret = sfp_get_module_eeprom(pl->sfp_bus, ee, buf);
1237
1238 return ret;
1239}
1240EXPORT_SYMBOL_GPL(phylink_ethtool_get_module_eeprom);
1241
Russell King8796c892017-12-01 10:24:47 +00001242/**
1243 * phylink_ethtool_get_eee_err() - read the energy efficient ethernet error
1244 * counter
1245 * @pl: a pointer to a &struct phylink returned from phylink_create().
1246 *
1247 * Read the Energy Efficient Ethernet error counter from the PHY associated
1248 * with the phylink instance specified by @pl.
1249 *
1250 * Returns positive error counter value, or negative error code.
1251 */
Russell King9525ae82017-07-25 15:03:13 +01001252int phylink_get_eee_err(struct phylink *pl)
1253{
1254 int ret = 0;
1255
1256 WARN_ON(!lockdep_rtnl_is_held());
1257
1258 if (pl->phydev)
1259 ret = phy_get_eee_err(pl->phydev);
1260
1261 return ret;
1262}
1263EXPORT_SYMBOL_GPL(phylink_get_eee_err);
1264
Russell King8796c892017-12-01 10:24:47 +00001265/**
1266 * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters
1267 * @pl: a pointer to a &struct phylink returned from phylink_create()
1268 * @eee: a pointer to a &struct ethtool_eee for the read parameters
1269 */
Russell King9525ae82017-07-25 15:03:13 +01001270int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee)
1271{
1272 int ret = -EOPNOTSUPP;
1273
1274 WARN_ON(!lockdep_rtnl_is_held());
1275
1276 if (pl->phydev)
1277 ret = phy_ethtool_get_eee(pl->phydev, eee);
1278
1279 return ret;
1280}
1281EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
1282
Russell King8796c892017-12-01 10:24:47 +00001283/**
1284 * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters
1285 * @pl: a pointer to a &struct phylink returned from phylink_create()
1286 * @eee: a pointer to a &struct ethtool_eee for the desired parameters
1287 */
Russell King9525ae82017-07-25 15:03:13 +01001288int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_eee *eee)
1289{
1290 int ret = -EOPNOTSUPP;
1291
1292 WARN_ON(!lockdep_rtnl_is_held());
1293
1294 if (pl->phydev)
1295 ret = phy_ethtool_set_eee(pl->phydev, eee);
1296
1297 return ret;
1298}
1299EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
1300
1301/* This emulates MII registers for a fixed-mode phy operating as per the
1302 * passed in state. "aneg" defines if we report negotiation is possible.
1303 *
1304 * FIXME: should deal with negotiation state too.
1305 */
1306static int phylink_mii_emul_read(struct net_device *ndev, unsigned int reg,
1307 struct phylink_link_state *state, bool aneg)
1308{
1309 struct fixed_phy_status fs;
1310 int val;
1311
1312 fs.link = state->link;
1313 fs.speed = state->speed;
1314 fs.duplex = state->duplex;
1315 fs.pause = state->pause & MLO_PAUSE_SYM;
1316 fs.asym_pause = state->pause & MLO_PAUSE_ASYM;
1317
1318 val = swphy_read_reg(reg, &fs);
1319 if (reg == MII_BMSR) {
1320 if (!state->an_complete)
1321 val &= ~BMSR_ANEGCOMPLETE;
1322 if (!aneg)
1323 val &= ~BMSR_ANEGCAPABLE;
1324 }
1325 return val;
1326}
1327
Russell Kingecbd87b2017-07-25 15:03:28 +01001328static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
1329 unsigned int reg)
1330{
1331 struct phy_device *phydev = pl->phydev;
1332 int prtad, devad;
1333
1334 if (mdio_phy_id_is_c45(phy_id)) {
1335 prtad = mdio_phy_id_prtad(phy_id);
1336 devad = mdio_phy_id_devad(phy_id);
1337 devad = MII_ADDR_C45 | devad << 16 | reg;
1338 } else if (phydev->is_c45) {
1339 switch (reg) {
1340 case MII_BMCR:
1341 case MII_BMSR:
1342 case MII_PHYSID1:
1343 case MII_PHYSID2:
1344 devad = __ffs(phydev->c45_ids.devices_in_package);
1345 break;
1346 case MII_ADVERTISE:
1347 case MII_LPA:
1348 if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1349 return -EINVAL;
1350 devad = MDIO_MMD_AN;
1351 if (reg == MII_ADVERTISE)
1352 reg = MDIO_AN_ADVERTISE;
1353 else
1354 reg = MDIO_AN_LPA;
1355 break;
1356 default:
1357 return -EINVAL;
1358 }
1359 prtad = phy_id;
1360 devad = MII_ADDR_C45 | devad << 16 | reg;
1361 } else {
1362 prtad = phy_id;
1363 devad = reg;
1364 }
1365 return mdiobus_read(pl->phydev->mdio.bus, prtad, devad);
1366}
1367
1368static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
1369 unsigned int reg, unsigned int val)
1370{
1371 struct phy_device *phydev = pl->phydev;
1372 int prtad, devad;
1373
1374 if (mdio_phy_id_is_c45(phy_id)) {
1375 prtad = mdio_phy_id_prtad(phy_id);
1376 devad = mdio_phy_id_devad(phy_id);
1377 devad = MII_ADDR_C45 | devad << 16 | reg;
1378 } else if (phydev->is_c45) {
1379 switch (reg) {
1380 case MII_BMCR:
1381 case MII_BMSR:
1382 case MII_PHYSID1:
1383 case MII_PHYSID2:
1384 devad = __ffs(phydev->c45_ids.devices_in_package);
1385 break;
1386 case MII_ADVERTISE:
1387 case MII_LPA:
1388 if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1389 return -EINVAL;
1390 devad = MDIO_MMD_AN;
1391 if (reg == MII_ADVERTISE)
1392 reg = MDIO_AN_ADVERTISE;
1393 else
1394 reg = MDIO_AN_LPA;
1395 break;
1396 default:
1397 return -EINVAL;
1398 }
1399 prtad = phy_id;
1400 devad = MII_ADDR_C45 | devad << 16 | reg;
1401 } else {
1402 prtad = phy_id;
1403 devad = reg;
1404 }
1405
1406 return mdiobus_write(phydev->mdio.bus, prtad, devad, val);
1407}
1408
Russell King9525ae82017-07-25 15:03:13 +01001409static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
1410 unsigned int reg)
1411{
1412 struct phylink_link_state state;
1413 int val = 0xffff;
1414
Russell King9525ae82017-07-25 15:03:13 +01001415 switch (pl->link_an_mode) {
1416 case MLO_AN_FIXED:
1417 if (phy_id == 0) {
1418 phylink_get_fixed_state(pl, &state);
1419 val = phylink_mii_emul_read(pl->netdev, reg, &state,
1420 true);
1421 }
1422 break;
1423
1424 case MLO_AN_PHY:
1425 return -EOPNOTSUPP;
1426
Russell King86a362c2017-12-01 10:24:26 +00001427 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001428 if (phy_id == 0) {
1429 val = phylink_get_mac_state(pl, &state);
1430 if (val < 0)
1431 return val;
1432
1433 val = phylink_mii_emul_read(pl->netdev, reg, &state,
1434 true);
1435 }
1436 break;
1437 }
1438
1439 return val & 0xffff;
1440}
1441
1442static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
1443 unsigned int reg, unsigned int val)
1444{
Russell King9525ae82017-07-25 15:03:13 +01001445 switch (pl->link_an_mode) {
1446 case MLO_AN_FIXED:
1447 break;
1448
1449 case MLO_AN_PHY:
1450 return -EOPNOTSUPP;
1451
Russell King86a362c2017-12-01 10:24:26 +00001452 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001453 break;
1454 }
1455
1456 return 0;
1457}
1458
Russell King8796c892017-12-01 10:24:47 +00001459/**
1460 * phylink_mii_ioctl() - generic mii ioctl interface
1461 * @pl: a pointer to a &struct phylink returned from phylink_create()
1462 * @ifr: a pointer to a &struct ifreq for socket ioctls
1463 * @cmd: ioctl cmd to execute
1464 *
1465 * Perform the specified MII ioctl on the PHY attached to the phylink instance
1466 * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
1467 *
1468 * Returns: zero on success or negative error code.
1469 *
1470 * %SIOCGMIIPHY:
1471 * read register from the current PHY.
1472 * %SIOCGMIIREG:
1473 * read register from the specified PHY.
1474 * %SIOCSMIIREG:
1475 * set a register on the specified PHY.
1476 */
Russell King9525ae82017-07-25 15:03:13 +01001477int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
1478{
Russell Kingecbd87b2017-07-25 15:03:28 +01001479 struct mii_ioctl_data *mii = if_mii(ifr);
1480 int ret;
Russell King9525ae82017-07-25 15:03:13 +01001481
1482 WARN_ON(!lockdep_rtnl_is_held());
1483
Russell Kingecbd87b2017-07-25 15:03:28 +01001484 if (pl->phydev) {
Russell King86a362c2017-12-01 10:24:26 +00001485 /* PHYs only exist for MLO_AN_PHY and SGMII */
Russell Kingecbd87b2017-07-25 15:03:28 +01001486 switch (cmd) {
1487 case SIOCGMIIPHY:
1488 mii->phy_id = pl->phydev->mdio.addr;
Russell King9525ae82017-07-25 15:03:13 +01001489
Russell Kingecbd87b2017-07-25 15:03:28 +01001490 case SIOCGMIIREG:
1491 ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
1492 if (ret >= 0) {
1493 mii->val_out = ret;
1494 ret = 0;
1495 }
1496 break;
Russell King9525ae82017-07-25 15:03:13 +01001497
Russell Kingecbd87b2017-07-25 15:03:28 +01001498 case SIOCSMIIREG:
1499 ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
1500 mii->val_in);
1501 break;
Russell King9525ae82017-07-25 15:03:13 +01001502
Russell Kingecbd87b2017-07-25 15:03:28 +01001503 default:
Russell King9525ae82017-07-25 15:03:13 +01001504 ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
Russell Kingecbd87b2017-07-25 15:03:28 +01001505 break;
1506 }
1507 } else {
1508 switch (cmd) {
1509 case SIOCGMIIPHY:
1510 mii->phy_id = 0;
1511
1512 case SIOCGMIIREG:
1513 ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
1514 if (ret >= 0) {
1515 mii->val_out = ret;
1516 ret = 0;
1517 }
1518 break;
1519
1520 case SIOCSMIIREG:
1521 ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
1522 mii->val_in);
1523 break;
1524
1525 default:
1526 ret = -EOPNOTSUPP;
1527 break;
1528 }
Russell King9525ae82017-07-25 15:03:13 +01001529 }
1530
1531 return ret;
1532}
1533EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
1534
Russell Kingce0aa272017-07-25 15:03:18 +01001535static int phylink_sfp_module_insert(void *upstream,
1536 const struct sfp_eeprom_id *id)
1537{
1538 struct phylink *pl = upstream;
1539 __ETHTOOL_DECLARE_LINK_MODE_MASK(support) = { 0, };
1540 struct phylink_link_state config;
1541 phy_interface_t iface;
1542 int mode, ret = 0;
1543 bool changed;
1544 u8 port;
1545
1546 sfp_parse_support(pl->sfp_bus, id, support);
1547 port = sfp_parse_port(pl->sfp_bus, id, support);
1548 iface = sfp_parse_interface(pl->sfp_bus, id);
1549
1550 WARN_ON(!lockdep_rtnl_is_held());
1551
1552 switch (iface) {
1553 case PHY_INTERFACE_MODE_SGMII:
Russell Kingce0aa272017-07-25 15:03:18 +01001554 case PHY_INTERFACE_MODE_1000BASEX:
Russell King4336c402017-12-01 10:24:32 +00001555 case PHY_INTERFACE_MODE_2500BASEX:
1556 case PHY_INTERFACE_MODE_10GKR:
Russell King86a362c2017-12-01 10:24:26 +00001557 mode = MLO_AN_INBAND;
Russell Kingce0aa272017-07-25 15:03:18 +01001558 break;
1559 default:
1560 return -EINVAL;
1561 }
1562
1563 memset(&config, 0, sizeof(config));
1564 linkmode_copy(config.advertising, support);
1565 config.interface = iface;
1566 config.speed = SPEED_UNKNOWN;
1567 config.duplex = DUPLEX_UNKNOWN;
1568 config.pause = MLO_PAUSE_AN;
1569 config.an_enabled = pl->link_config.an_enabled;
1570
1571 /* Ignore errors if we're expecting a PHY to attach later */
1572 ret = phylink_validate(pl, support, &config);
1573 if (ret) {
1574 netdev_err(pl->netdev, "validation of %s/%s with support %*pb failed: %d\n",
1575 phylink_an_mode_str(mode), phy_modes(config.interface),
1576 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
1577 return ret;
1578 }
1579
1580 netdev_dbg(pl->netdev, "requesting link mode %s/%s with support %*pb\n",
1581 phylink_an_mode_str(mode), phy_modes(config.interface),
1582 __ETHTOOL_LINK_MODE_MASK_NBITS, support);
1583
Russell King86a362c2017-12-01 10:24:26 +00001584 if (phy_interface_mode_is_8023z(iface) && pl->phydev)
Russell Kingce0aa272017-07-25 15:03:18 +01001585 return -EINVAL;
1586
1587 changed = !bitmap_equal(pl->supported, support,
1588 __ETHTOOL_LINK_MODE_MASK_NBITS);
1589 if (changed) {
1590 linkmode_copy(pl->supported, support);
1591 linkmode_copy(pl->link_config.advertising, config.advertising);
1592 }
1593
1594 if (pl->link_an_mode != mode ||
1595 pl->link_config.interface != config.interface) {
1596 pl->link_config.interface = config.interface;
1597 pl->link_an_mode = mode;
1598
1599 changed = true;
1600
1601 netdev_info(pl->netdev, "switched to %s/%s link mode\n",
1602 phylink_an_mode_str(mode),
1603 phy_modes(config.interface));
1604 }
1605
1606 pl->link_port = port;
1607
1608 if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
1609 &pl->phylink_disable_state))
1610 phylink_mac_config(pl, &pl->link_config);
1611
1612 return ret;
1613}
1614
1615static void phylink_sfp_link_down(void *upstream)
1616{
1617 struct phylink *pl = upstream;
1618
1619 WARN_ON(!lockdep_rtnl_is_held());
1620
1621 set_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
1622 flush_work(&pl->resolve);
1623
1624 netif_carrier_off(pl->netdev);
1625}
1626
1627static void phylink_sfp_link_up(void *upstream)
1628{
1629 struct phylink *pl = upstream;
1630
1631 WARN_ON(!lockdep_rtnl_is_held());
1632
1633 clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
1634 phylink_run_resolve(pl);
1635}
1636
1637static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
1638{
1639 return phylink_connect_phy(upstream, phy);
1640}
1641
1642static void phylink_sfp_disconnect_phy(void *upstream)
1643{
1644 phylink_disconnect_phy(upstream);
1645}
1646
1647static const struct sfp_upstream_ops sfp_phylink_ops = {
1648 .module_insert = phylink_sfp_module_insert,
1649 .link_up = phylink_sfp_link_up,
1650 .link_down = phylink_sfp_link_down,
1651 .connect_phy = phylink_sfp_connect_phy,
1652 .disconnect_phy = phylink_sfp_disconnect_phy,
1653};
1654
Russell King9525ae82017-07-25 15:03:13 +01001655MODULE_LICENSE("GPL");