blob: 2bfb548d3dff46e3376f572713583c6e0945217e [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
Russell King9525ae82017-07-25 15:03:13 +0100718 ret = phy_attach_direct(pl->netdev, phy, 0, pl->link_interface);
719 if (ret)
720 return ret;
721
722 ret = phylink_bringup_phy(pl, phy);
723 if (ret)
724 phy_detach(phy);
725
726 return ret;
727}
728EXPORT_SYMBOL_GPL(phylink_connect_phy);
729
Russell King8796c892017-12-01 10:24:47 +0000730/**
731 * phylink_of_phy_connect() - connect the PHY specified in the DT mode.
732 * @pl: a pointer to a &struct phylink returned from phylink_create()
733 * @dn: a pointer to a &struct device_node.
734 *
735 * Connect the phy specified in the device node @dn to the phylink instance
736 * specified by @pl. Actions specified in phylink_connect_phy() will be
737 * performed.
738 *
739 * Returns 0 on success or a negative errno.
740 */
Russell King9525ae82017-07-25 15:03:13 +0100741int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn)
742{
743 struct device_node *phy_node;
744 struct phy_device *phy_dev;
745 int ret;
746
Russell Kingcf4f2672017-12-01 10:24:21 +0000747 /* Fixed links and 802.3z are handled without needing a PHY */
748 if (pl->link_an_mode == MLO_AN_FIXED ||
Russell King86a362c2017-12-01 10:24:26 +0000749 (pl->link_an_mode == MLO_AN_INBAND &&
750 phy_interface_mode_is_8023z(pl->link_interface)))
Russell King9525ae82017-07-25 15:03:13 +0100751 return 0;
752
753 phy_node = of_parse_phandle(dn, "phy-handle", 0);
754 if (!phy_node)
755 phy_node = of_parse_phandle(dn, "phy", 0);
756 if (!phy_node)
757 phy_node = of_parse_phandle(dn, "phy-device", 0);
758
759 if (!phy_node) {
760 if (pl->link_an_mode == MLO_AN_PHY) {
761 netdev_err(pl->netdev, "unable to find PHY node\n");
762 return -ENODEV;
763 }
764 return 0;
765 }
766
767 phy_dev = of_phy_attach(pl->netdev, phy_node, 0, pl->link_interface);
768 /* We're done with the phy_node handle */
769 of_node_put(phy_node);
770
771 if (!phy_dev)
772 return -ENODEV;
773
774 ret = phylink_bringup_phy(pl, phy_dev);
775 if (ret)
776 phy_detach(phy_dev);
777
778 return ret;
779}
780EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
781
Russell King8796c892017-12-01 10:24:47 +0000782/**
783 * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
784 * instance.
785 * @pl: a pointer to a &struct phylink returned from phylink_create()
786 *
787 * Disconnect any current PHY from the phylink instance described by @pl.
788 */
Russell King9525ae82017-07-25 15:03:13 +0100789void phylink_disconnect_phy(struct phylink *pl)
790{
791 struct phy_device *phy;
792
793 WARN_ON(!lockdep_rtnl_is_held());
794
795 phy = pl->phydev;
796 if (phy) {
797 mutex_lock(&phy->lock);
798 mutex_lock(&pl->state_mutex);
799 pl->netdev->phydev = NULL;
800 pl->phydev = NULL;
801 mutex_unlock(&pl->state_mutex);
802 mutex_unlock(&phy->lock);
803 flush_work(&pl->resolve);
804
805 phy_disconnect(phy);
806 }
807}
808EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
809
Russell King8796c892017-12-01 10:24:47 +0000810/**
811 * phylink_mac_change() - notify phylink of a change in MAC state
812 * @pl: a pointer to a &struct phylink returned from phylink_create()
813 * @up: indicates whether the link is currently up.
814 *
815 * The MAC driver should call this driver when the state of its link
816 * changes (eg, link failure, new negotiation results, etc.)
817 */
Russell King9525ae82017-07-25 15:03:13 +0100818void phylink_mac_change(struct phylink *pl, bool up)
819{
820 if (!up)
821 pl->mac_link_dropped = true;
822 phylink_run_resolve(pl);
823 netdev_dbg(pl->netdev, "mac link %s\n", up ? "up" : "down");
824}
825EXPORT_SYMBOL_GPL(phylink_mac_change);
826
Russell King8796c892017-12-01 10:24:47 +0000827/**
828 * phylink_start() - start a phylink instance
829 * @pl: a pointer to a &struct phylink returned from phylink_create()
830 *
831 * Start the phylink instance specified by @pl, configuring the MAC for the
832 * desired link mode(s) and negotiation style. This should be called from the
833 * network device driver's &struct net_device_ops ndo_open() method.
834 */
Russell King9525ae82017-07-25 15:03:13 +0100835void phylink_start(struct phylink *pl)
836{
837 WARN_ON(!lockdep_rtnl_is_held());
838
839 netdev_info(pl->netdev, "configuring for %s/%s link mode\n",
840 phylink_an_mode_str(pl->link_an_mode),
841 phy_modes(pl->link_config.interface));
842
843 /* Apply the link configuration to the MAC when starting. This allows
844 * a fixed-link to start with the correct parameters, and also
845 * ensures that we set the appropriate advertisment for Serdes links.
846 */
847 phylink_resolve_flow(pl, &pl->link_config);
848 phylink_mac_config(pl, &pl->link_config);
849
Russell King85b43942017-12-01 10:24:42 +0000850 /* Restart autonegotiation if using 802.3z to ensure that the link
851 * parameters are properly negotiated. This is necessary for DSA
852 * switches using 802.3z negotiation to ensure they see our modes.
853 */
854 phylink_mac_an_restart(pl);
855
Russell King9525ae82017-07-25 15:03:13 +0100856 clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
857 phylink_run_resolve(pl);
858
Russell Kingce0aa272017-07-25 15:03:18 +0100859 if (pl->sfp_bus)
860 sfp_upstream_start(pl->sfp_bus);
Russell King9525ae82017-07-25 15:03:13 +0100861 if (pl->phydev)
862 phy_start(pl->phydev);
863}
864EXPORT_SYMBOL_GPL(phylink_start);
865
Russell King8796c892017-12-01 10:24:47 +0000866/**
867 * phylink_stop() - stop a phylink instance
868 * @pl: a pointer to a &struct phylink returned from phylink_create()
869 *
870 * Stop the phylink instance specified by @pl. This should be called from the
871 * network device driver's &struct net_device_ops ndo_stop() method. The
872 * network device's carrier state should not be changed prior to calling this
873 * function.
874 */
Russell King9525ae82017-07-25 15:03:13 +0100875void phylink_stop(struct phylink *pl)
876{
877 WARN_ON(!lockdep_rtnl_is_held());
878
879 if (pl->phydev)
880 phy_stop(pl->phydev);
Russell Kingce0aa272017-07-25 15:03:18 +0100881 if (pl->sfp_bus)
882 sfp_upstream_stop(pl->sfp_bus);
Russell King9525ae82017-07-25 15:03:13 +0100883
884 set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
Russell King2012b7d2017-11-30 13:59:26 +0000885 queue_work(system_power_efficient_wq, &pl->resolve);
Russell King9525ae82017-07-25 15:03:13 +0100886 flush_work(&pl->resolve);
887}
888EXPORT_SYMBOL_GPL(phylink_stop);
889
Russell King8796c892017-12-01 10:24:47 +0000890/**
891 * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY
892 * @pl: a pointer to a &struct phylink returned from phylink_create()
893 * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters
894 *
895 * Read the wake on lan parameters from the PHY attached to the phylink
896 * instance specified by @pl. If no PHY is currently attached, report no
897 * support for wake on lan.
898 */
Russell King9525ae82017-07-25 15:03:13 +0100899void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
900{
901 WARN_ON(!lockdep_rtnl_is_held());
902
903 wol->supported = 0;
904 wol->wolopts = 0;
905
906 if (pl->phydev)
907 phy_ethtool_get_wol(pl->phydev, wol);
908}
909EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol);
910
Russell King8796c892017-12-01 10:24:47 +0000911/**
912 * phylink_ethtool_set_wol() - set wake on lan parameters
913 * @pl: a pointer to a &struct phylink returned from phylink_create()
914 * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters
915 *
916 * Set the wake on lan parameters for the PHY attached to the phylink
917 * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP
918 * error.
919 *
920 * Returns zero on success or negative errno code.
921 */
Russell King9525ae82017-07-25 15:03:13 +0100922int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
923{
924 int ret = -EOPNOTSUPP;
925
926 WARN_ON(!lockdep_rtnl_is_held());
927
928 if (pl->phydev)
929 ret = phy_ethtool_set_wol(pl->phydev, wol);
930
931 return ret;
932}
933EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol);
934
935static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b)
936{
937 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
938
939 linkmode_zero(mask);
940 phylink_set_port_modes(mask);
941
942 linkmode_and(dst, dst, mask);
943 linkmode_or(dst, dst, b);
944}
945
946static void phylink_get_ksettings(const struct phylink_link_state *state,
947 struct ethtool_link_ksettings *kset)
948{
949 phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
950 linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
951 kset->base.speed = state->speed;
952 kset->base.duplex = state->duplex;
953 kset->base.autoneg = state->an_enabled ? AUTONEG_ENABLE :
954 AUTONEG_DISABLE;
955}
956
Russell King8796c892017-12-01 10:24:47 +0000957/**
958 * phylink_ethtool_ksettings_get() - get the current link settings
959 * @pl: a pointer to a &struct phylink returned from phylink_create()
960 * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings
961 *
962 * Read the current link settings for the phylink instance specified by @pl.
963 * This will be the link settings read from the MAC, PHY or fixed link
964 * settings depending on the current negotiation mode.
965 */
Russell King9525ae82017-07-25 15:03:13 +0100966int phylink_ethtool_ksettings_get(struct phylink *pl,
Florian Fainelli516b29e2017-10-30 21:42:57 -0700967 struct ethtool_link_ksettings *kset)
Russell King9525ae82017-07-25 15:03:13 +0100968{
969 struct phylink_link_state link_state;
970
971 WARN_ON(!lockdep_rtnl_is_held());
972
973 if (pl->phydev) {
974 phy_ethtool_ksettings_get(pl->phydev, kset);
975 } else {
976 kset->base.port = pl->link_port;
977 }
978
979 linkmode_copy(kset->link_modes.supported, pl->supported);
980
981 switch (pl->link_an_mode) {
982 case MLO_AN_FIXED:
983 /* We are using fixed settings. Report these as the
984 * current link settings - and note that these also
985 * represent the supported speeds/duplex/pause modes.
986 */
987 phylink_get_fixed_state(pl, &link_state);
988 phylink_get_ksettings(&link_state, kset);
989 break;
990
Russell King86a362c2017-12-01 10:24:26 +0000991 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +0100992 /* If there is a phy attached, then use the reported
993 * settings from the phy with no modification.
994 */
995 if (pl->phydev)
996 break;
997
Russell King9525ae82017-07-25 15:03:13 +0100998 phylink_get_mac_state(pl, &link_state);
999
1000 /* The MAC is reporting the link results from its own PCS
1001 * layer via in-band status. Report these as the current
1002 * link settings.
1003 */
1004 phylink_get_ksettings(&link_state, kset);
1005 break;
1006 }
1007
1008 return 0;
1009}
1010EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get);
1011
Russell King8796c892017-12-01 10:24:47 +00001012/**
1013 * phylink_ethtool_ksettings_set() - set the link settings
1014 * @pl: a pointer to a &struct phylink returned from phylink_create()
1015 * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes
1016 */
Russell King9525ae82017-07-25 15:03:13 +01001017int phylink_ethtool_ksettings_set(struct phylink *pl,
Florian Fainelli516b29e2017-10-30 21:42:57 -07001018 const struct ethtool_link_ksettings *kset)
Russell King9525ae82017-07-25 15:03:13 +01001019{
1020 struct ethtool_link_ksettings our_kset;
1021 struct phylink_link_state config;
1022 int ret;
1023
1024 WARN_ON(!lockdep_rtnl_is_held());
1025
1026 if (kset->base.autoneg != AUTONEG_DISABLE &&
1027 kset->base.autoneg != AUTONEG_ENABLE)
1028 return -EINVAL;
1029
1030 config = pl->link_config;
1031
1032 /* Mask out unsupported advertisments */
1033 linkmode_and(config.advertising, kset->link_modes.advertising,
1034 pl->supported);
1035
1036 /* FIXME: should we reject autoneg if phy/mac does not support it? */
1037 if (kset->base.autoneg == AUTONEG_DISABLE) {
1038 const struct phy_setting *s;
1039
1040 /* Autonegotiation disabled, select a suitable speed and
1041 * duplex.
1042 */
1043 s = phy_lookup_setting(kset->base.speed, kset->base.duplex,
1044 pl->supported,
1045 __ETHTOOL_LINK_MODE_MASK_NBITS, false);
1046 if (!s)
1047 return -EINVAL;
1048
1049 /* If we have a fixed link (as specified by firmware), refuse
1050 * to change link parameters.
1051 */
1052 if (pl->link_an_mode == MLO_AN_FIXED &&
1053 (s->speed != pl->link_config.speed ||
1054 s->duplex != pl->link_config.duplex))
1055 return -EINVAL;
1056
1057 config.speed = s->speed;
1058 config.duplex = s->duplex;
1059 config.an_enabled = false;
1060
1061 __clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
1062 } else {
1063 /* If we have a fixed link, refuse to enable autonegotiation */
1064 if (pl->link_an_mode == MLO_AN_FIXED)
1065 return -EINVAL;
1066
1067 config.speed = SPEED_UNKNOWN;
1068 config.duplex = DUPLEX_UNKNOWN;
1069 config.an_enabled = true;
1070
1071 __set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
1072 }
1073
1074 if (phylink_validate(pl, pl->supported, &config))
1075 return -EINVAL;
1076
1077 /* If autonegotiation is enabled, we must have an advertisment */
1078 if (config.an_enabled && phylink_is_empty_linkmode(config.advertising))
1079 return -EINVAL;
1080
1081 our_kset = *kset;
1082 linkmode_copy(our_kset.link_modes.advertising, config.advertising);
1083 our_kset.base.speed = config.speed;
1084 our_kset.base.duplex = config.duplex;
1085
1086 /* If we have a PHY, configure the phy */
1087 if (pl->phydev) {
1088 ret = phy_ethtool_ksettings_set(pl->phydev, &our_kset);
1089 if (ret)
1090 return ret;
1091 }
1092
1093 mutex_lock(&pl->state_mutex);
1094 /* Configure the MAC to match the new settings */
1095 linkmode_copy(pl->link_config.advertising, our_kset.link_modes.advertising);
1096 pl->link_config.speed = our_kset.base.speed;
1097 pl->link_config.duplex = our_kset.base.duplex;
1098 pl->link_config.an_enabled = our_kset.base.autoneg != AUTONEG_DISABLE;
1099
1100 if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
1101 phylink_mac_config(pl, &pl->link_config);
1102 phylink_mac_an_restart(pl);
1103 }
1104 mutex_unlock(&pl->state_mutex);
1105
Dan Carpenterd18c2a12017-08-10 00:35:50 +03001106 return 0;
Russell King9525ae82017-07-25 15:03:13 +01001107}
1108EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
1109
Russell King8796c892017-12-01 10:24:47 +00001110/**
1111 * phylink_ethtool_nway_reset() - restart negotiation
1112 * @pl: a pointer to a &struct phylink returned from phylink_create()
1113 *
1114 * Restart negotiation for the phylink instance specified by @pl. This will
1115 * cause any attached phy to restart negotiation with the link partner, and
1116 * if the MAC is in a BaseX mode, the MAC will also be requested to restart
1117 * negotiation.
1118 *
1119 * Returns zero on success, or negative error code.
1120 */
Russell King9525ae82017-07-25 15:03:13 +01001121int phylink_ethtool_nway_reset(struct phylink *pl)
1122{
1123 int ret = 0;
1124
1125 WARN_ON(!lockdep_rtnl_is_held());
1126
1127 if (pl->phydev)
1128 ret = phy_restart_aneg(pl->phydev);
1129 phylink_mac_an_restart(pl);
1130
1131 return ret;
1132}
1133EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
1134
Russell King8796c892017-12-01 10:24:47 +00001135/**
1136 * phylink_ethtool_get_pauseparam() - get the current pause parameters
1137 * @pl: a pointer to a &struct phylink returned from phylink_create()
1138 * @pause: a pointer to a &struct ethtool_pauseparam
1139 */
Russell King9525ae82017-07-25 15:03:13 +01001140void phylink_ethtool_get_pauseparam(struct phylink *pl,
1141 struct ethtool_pauseparam *pause)
1142{
1143 WARN_ON(!lockdep_rtnl_is_held());
1144
1145 pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
1146 pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
1147 pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
1148}
1149EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
1150
Russell King8796c892017-12-01 10:24:47 +00001151/**
1152 * phylink_ethtool_set_pauseparam() - set the current pause parameters
1153 * @pl: a pointer to a &struct phylink returned from phylink_create()
1154 * @pause: a pointer to a &struct ethtool_pauseparam
1155 */
Russell King9525ae82017-07-25 15:03:13 +01001156int phylink_ethtool_set_pauseparam(struct phylink *pl,
1157 struct ethtool_pauseparam *pause)
1158{
1159 struct phylink_link_state *config = &pl->link_config;
1160
1161 WARN_ON(!lockdep_rtnl_is_held());
1162
1163 if (!phylink_test(pl->supported, Pause) &&
1164 !phylink_test(pl->supported, Asym_Pause))
1165 return -EOPNOTSUPP;
1166
1167 if (!phylink_test(pl->supported, Asym_Pause) &&
1168 !pause->autoneg && pause->rx_pause != pause->tx_pause)
1169 return -EINVAL;
1170
1171 config->pause &= ~(MLO_PAUSE_AN | MLO_PAUSE_TXRX_MASK);
1172
1173 if (pause->autoneg)
1174 config->pause |= MLO_PAUSE_AN;
1175 if (pause->rx_pause)
1176 config->pause |= MLO_PAUSE_RX;
1177 if (pause->tx_pause)
1178 config->pause |= MLO_PAUSE_TX;
1179
1180 if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
1181 switch (pl->link_an_mode) {
1182 case MLO_AN_PHY:
1183 /* Silently mark the carrier down, and then trigger a resolve */
1184 netif_carrier_off(pl->netdev);
1185 phylink_run_resolve(pl);
1186 break;
1187
1188 case MLO_AN_FIXED:
1189 /* Should we allow fixed links to change against the config? */
1190 phylink_resolve_flow(pl, config);
1191 phylink_mac_config(pl, config);
1192 break;
1193
Russell King86a362c2017-12-01 10:24:26 +00001194 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001195 phylink_mac_config(pl, config);
1196 phylink_mac_an_restart(pl);
1197 break;
1198 }
1199 }
1200
1201 return 0;
1202}
1203EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
1204
Russell King770a1ad2017-07-25 15:03:23 +01001205int phylink_ethtool_get_module_info(struct phylink *pl,
1206 struct ethtool_modinfo *modinfo)
1207{
1208 int ret = -EOPNOTSUPP;
1209
1210 WARN_ON(!lockdep_rtnl_is_held());
1211
1212 if (pl->sfp_bus)
1213 ret = sfp_get_module_info(pl->sfp_bus, modinfo);
1214
1215 return ret;
1216}
1217EXPORT_SYMBOL_GPL(phylink_ethtool_get_module_info);
1218
1219int phylink_ethtool_get_module_eeprom(struct phylink *pl,
1220 struct ethtool_eeprom *ee, u8 *buf)
1221{
1222 int ret = -EOPNOTSUPP;
1223
1224 WARN_ON(!lockdep_rtnl_is_held());
1225
1226 if (pl->sfp_bus)
1227 ret = sfp_get_module_eeprom(pl->sfp_bus, ee, buf);
1228
1229 return ret;
1230}
1231EXPORT_SYMBOL_GPL(phylink_ethtool_get_module_eeprom);
1232
Russell King8796c892017-12-01 10:24:47 +00001233/**
1234 * phylink_ethtool_get_eee_err() - read the energy efficient ethernet error
1235 * counter
1236 * @pl: a pointer to a &struct phylink returned from phylink_create().
1237 *
1238 * Read the Energy Efficient Ethernet error counter from the PHY associated
1239 * with the phylink instance specified by @pl.
1240 *
1241 * Returns positive error counter value, or negative error code.
1242 */
Russell King9525ae82017-07-25 15:03:13 +01001243int phylink_get_eee_err(struct phylink *pl)
1244{
1245 int ret = 0;
1246
1247 WARN_ON(!lockdep_rtnl_is_held());
1248
1249 if (pl->phydev)
1250 ret = phy_get_eee_err(pl->phydev);
1251
1252 return ret;
1253}
1254EXPORT_SYMBOL_GPL(phylink_get_eee_err);
1255
Russell King8796c892017-12-01 10:24:47 +00001256/**
1257 * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters
1258 * @pl: a pointer to a &struct phylink returned from phylink_create()
1259 * @eee: a pointer to a &struct ethtool_eee for the read parameters
1260 */
Russell King9525ae82017-07-25 15:03:13 +01001261int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee)
1262{
1263 int ret = -EOPNOTSUPP;
1264
1265 WARN_ON(!lockdep_rtnl_is_held());
1266
1267 if (pl->phydev)
1268 ret = phy_ethtool_get_eee(pl->phydev, eee);
1269
1270 return ret;
1271}
1272EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
1273
Russell King8796c892017-12-01 10:24:47 +00001274/**
1275 * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters
1276 * @pl: a pointer to a &struct phylink returned from phylink_create()
1277 * @eee: a pointer to a &struct ethtool_eee for the desired parameters
1278 */
Russell King9525ae82017-07-25 15:03:13 +01001279int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_eee *eee)
1280{
1281 int ret = -EOPNOTSUPP;
1282
1283 WARN_ON(!lockdep_rtnl_is_held());
1284
1285 if (pl->phydev)
1286 ret = phy_ethtool_set_eee(pl->phydev, eee);
1287
1288 return ret;
1289}
1290EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
1291
1292/* This emulates MII registers for a fixed-mode phy operating as per the
1293 * passed in state. "aneg" defines if we report negotiation is possible.
1294 *
1295 * FIXME: should deal with negotiation state too.
1296 */
1297static int phylink_mii_emul_read(struct net_device *ndev, unsigned int reg,
1298 struct phylink_link_state *state, bool aneg)
1299{
1300 struct fixed_phy_status fs;
1301 int val;
1302
1303 fs.link = state->link;
1304 fs.speed = state->speed;
1305 fs.duplex = state->duplex;
1306 fs.pause = state->pause & MLO_PAUSE_SYM;
1307 fs.asym_pause = state->pause & MLO_PAUSE_ASYM;
1308
1309 val = swphy_read_reg(reg, &fs);
1310 if (reg == MII_BMSR) {
1311 if (!state->an_complete)
1312 val &= ~BMSR_ANEGCOMPLETE;
1313 if (!aneg)
1314 val &= ~BMSR_ANEGCAPABLE;
1315 }
1316 return val;
1317}
1318
Russell Kingecbd87b2017-07-25 15:03:28 +01001319static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
1320 unsigned int reg)
1321{
1322 struct phy_device *phydev = pl->phydev;
1323 int prtad, devad;
1324
1325 if (mdio_phy_id_is_c45(phy_id)) {
1326 prtad = mdio_phy_id_prtad(phy_id);
1327 devad = mdio_phy_id_devad(phy_id);
1328 devad = MII_ADDR_C45 | devad << 16 | reg;
1329 } else if (phydev->is_c45) {
1330 switch (reg) {
1331 case MII_BMCR:
1332 case MII_BMSR:
1333 case MII_PHYSID1:
1334 case MII_PHYSID2:
1335 devad = __ffs(phydev->c45_ids.devices_in_package);
1336 break;
1337 case MII_ADVERTISE:
1338 case MII_LPA:
1339 if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1340 return -EINVAL;
1341 devad = MDIO_MMD_AN;
1342 if (reg == MII_ADVERTISE)
1343 reg = MDIO_AN_ADVERTISE;
1344 else
1345 reg = MDIO_AN_LPA;
1346 break;
1347 default:
1348 return -EINVAL;
1349 }
1350 prtad = phy_id;
1351 devad = MII_ADDR_C45 | devad << 16 | reg;
1352 } else {
1353 prtad = phy_id;
1354 devad = reg;
1355 }
1356 return mdiobus_read(pl->phydev->mdio.bus, prtad, devad);
1357}
1358
1359static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
1360 unsigned int reg, unsigned int val)
1361{
1362 struct phy_device *phydev = pl->phydev;
1363 int prtad, devad;
1364
1365 if (mdio_phy_id_is_c45(phy_id)) {
1366 prtad = mdio_phy_id_prtad(phy_id);
1367 devad = mdio_phy_id_devad(phy_id);
1368 devad = MII_ADDR_C45 | devad << 16 | reg;
1369 } else if (phydev->is_c45) {
1370 switch (reg) {
1371 case MII_BMCR:
1372 case MII_BMSR:
1373 case MII_PHYSID1:
1374 case MII_PHYSID2:
1375 devad = __ffs(phydev->c45_ids.devices_in_package);
1376 break;
1377 case MII_ADVERTISE:
1378 case MII_LPA:
1379 if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1380 return -EINVAL;
1381 devad = MDIO_MMD_AN;
1382 if (reg == MII_ADVERTISE)
1383 reg = MDIO_AN_ADVERTISE;
1384 else
1385 reg = MDIO_AN_LPA;
1386 break;
1387 default:
1388 return -EINVAL;
1389 }
1390 prtad = phy_id;
1391 devad = MII_ADDR_C45 | devad << 16 | reg;
1392 } else {
1393 prtad = phy_id;
1394 devad = reg;
1395 }
1396
1397 return mdiobus_write(phydev->mdio.bus, prtad, devad, val);
1398}
1399
Russell King9525ae82017-07-25 15:03:13 +01001400static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
1401 unsigned int reg)
1402{
1403 struct phylink_link_state state;
1404 int val = 0xffff;
1405
Russell King9525ae82017-07-25 15:03:13 +01001406 switch (pl->link_an_mode) {
1407 case MLO_AN_FIXED:
1408 if (phy_id == 0) {
1409 phylink_get_fixed_state(pl, &state);
1410 val = phylink_mii_emul_read(pl->netdev, reg, &state,
1411 true);
1412 }
1413 break;
1414
1415 case MLO_AN_PHY:
1416 return -EOPNOTSUPP;
1417
Russell King86a362c2017-12-01 10:24:26 +00001418 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001419 if (phy_id == 0) {
1420 val = phylink_get_mac_state(pl, &state);
1421 if (val < 0)
1422 return val;
1423
1424 val = phylink_mii_emul_read(pl->netdev, reg, &state,
1425 true);
1426 }
1427 break;
1428 }
1429
1430 return val & 0xffff;
1431}
1432
1433static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
1434 unsigned int reg, unsigned int val)
1435{
Russell King9525ae82017-07-25 15:03:13 +01001436 switch (pl->link_an_mode) {
1437 case MLO_AN_FIXED:
1438 break;
1439
1440 case MLO_AN_PHY:
1441 return -EOPNOTSUPP;
1442
Russell King86a362c2017-12-01 10:24:26 +00001443 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001444 break;
1445 }
1446
1447 return 0;
1448}
1449
Russell King8796c892017-12-01 10:24:47 +00001450/**
1451 * phylink_mii_ioctl() - generic mii ioctl interface
1452 * @pl: a pointer to a &struct phylink returned from phylink_create()
1453 * @ifr: a pointer to a &struct ifreq for socket ioctls
1454 * @cmd: ioctl cmd to execute
1455 *
1456 * Perform the specified MII ioctl on the PHY attached to the phylink instance
1457 * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
1458 *
1459 * Returns: zero on success or negative error code.
1460 *
1461 * %SIOCGMIIPHY:
1462 * read register from the current PHY.
1463 * %SIOCGMIIREG:
1464 * read register from the specified PHY.
1465 * %SIOCSMIIREG:
1466 * set a register on the specified PHY.
1467 */
Russell King9525ae82017-07-25 15:03:13 +01001468int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
1469{
Russell Kingecbd87b2017-07-25 15:03:28 +01001470 struct mii_ioctl_data *mii = if_mii(ifr);
1471 int ret;
Russell King9525ae82017-07-25 15:03:13 +01001472
1473 WARN_ON(!lockdep_rtnl_is_held());
1474
Russell Kingecbd87b2017-07-25 15:03:28 +01001475 if (pl->phydev) {
Russell King86a362c2017-12-01 10:24:26 +00001476 /* PHYs only exist for MLO_AN_PHY and SGMII */
Russell Kingecbd87b2017-07-25 15:03:28 +01001477 switch (cmd) {
1478 case SIOCGMIIPHY:
1479 mii->phy_id = pl->phydev->mdio.addr;
Russell King9525ae82017-07-25 15:03:13 +01001480
Russell Kingecbd87b2017-07-25 15:03:28 +01001481 case SIOCGMIIREG:
1482 ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
1483 if (ret >= 0) {
1484 mii->val_out = ret;
1485 ret = 0;
1486 }
1487 break;
Russell King9525ae82017-07-25 15:03:13 +01001488
Russell Kingecbd87b2017-07-25 15:03:28 +01001489 case SIOCSMIIREG:
1490 ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
1491 mii->val_in);
1492 break;
Russell King9525ae82017-07-25 15:03:13 +01001493
Russell Kingecbd87b2017-07-25 15:03:28 +01001494 default:
Russell King9525ae82017-07-25 15:03:13 +01001495 ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
Russell Kingecbd87b2017-07-25 15:03:28 +01001496 break;
1497 }
1498 } else {
1499 switch (cmd) {
1500 case SIOCGMIIPHY:
1501 mii->phy_id = 0;
1502
1503 case SIOCGMIIREG:
1504 ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
1505 if (ret >= 0) {
1506 mii->val_out = ret;
1507 ret = 0;
1508 }
1509 break;
1510
1511 case SIOCSMIIREG:
1512 ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
1513 mii->val_in);
1514 break;
1515
1516 default:
1517 ret = -EOPNOTSUPP;
1518 break;
1519 }
Russell King9525ae82017-07-25 15:03:13 +01001520 }
1521
1522 return ret;
1523}
1524EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
1525
Russell Kingce0aa272017-07-25 15:03:18 +01001526static int phylink_sfp_module_insert(void *upstream,
1527 const struct sfp_eeprom_id *id)
1528{
1529 struct phylink *pl = upstream;
1530 __ETHTOOL_DECLARE_LINK_MODE_MASK(support) = { 0, };
1531 struct phylink_link_state config;
1532 phy_interface_t iface;
1533 int mode, ret = 0;
1534 bool changed;
1535 u8 port;
1536
1537 sfp_parse_support(pl->sfp_bus, id, support);
1538 port = sfp_parse_port(pl->sfp_bus, id, support);
1539 iface = sfp_parse_interface(pl->sfp_bus, id);
1540
1541 WARN_ON(!lockdep_rtnl_is_held());
1542
1543 switch (iface) {
1544 case PHY_INTERFACE_MODE_SGMII:
Russell Kingce0aa272017-07-25 15:03:18 +01001545 case PHY_INTERFACE_MODE_1000BASEX:
Russell King4336c402017-12-01 10:24:32 +00001546 case PHY_INTERFACE_MODE_2500BASEX:
1547 case PHY_INTERFACE_MODE_10GKR:
Russell King86a362c2017-12-01 10:24:26 +00001548 mode = MLO_AN_INBAND;
Russell Kingce0aa272017-07-25 15:03:18 +01001549 break;
1550 default:
1551 return -EINVAL;
1552 }
1553
1554 memset(&config, 0, sizeof(config));
1555 linkmode_copy(config.advertising, support);
1556 config.interface = iface;
1557 config.speed = SPEED_UNKNOWN;
1558 config.duplex = DUPLEX_UNKNOWN;
1559 config.pause = MLO_PAUSE_AN;
1560 config.an_enabled = pl->link_config.an_enabled;
1561
1562 /* Ignore errors if we're expecting a PHY to attach later */
1563 ret = phylink_validate(pl, support, &config);
1564 if (ret) {
1565 netdev_err(pl->netdev, "validation of %s/%s with support %*pb failed: %d\n",
1566 phylink_an_mode_str(mode), phy_modes(config.interface),
1567 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
1568 return ret;
1569 }
1570
1571 netdev_dbg(pl->netdev, "requesting link mode %s/%s with support %*pb\n",
1572 phylink_an_mode_str(mode), phy_modes(config.interface),
1573 __ETHTOOL_LINK_MODE_MASK_NBITS, support);
1574
Russell King86a362c2017-12-01 10:24:26 +00001575 if (phy_interface_mode_is_8023z(iface) && pl->phydev)
Russell Kingce0aa272017-07-25 15:03:18 +01001576 return -EINVAL;
1577
1578 changed = !bitmap_equal(pl->supported, support,
1579 __ETHTOOL_LINK_MODE_MASK_NBITS);
1580 if (changed) {
1581 linkmode_copy(pl->supported, support);
1582 linkmode_copy(pl->link_config.advertising, config.advertising);
1583 }
1584
1585 if (pl->link_an_mode != mode ||
1586 pl->link_config.interface != config.interface) {
1587 pl->link_config.interface = config.interface;
1588 pl->link_an_mode = mode;
1589
1590 changed = true;
1591
1592 netdev_info(pl->netdev, "switched to %s/%s link mode\n",
1593 phylink_an_mode_str(mode),
1594 phy_modes(config.interface));
1595 }
1596
1597 pl->link_port = port;
1598
1599 if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
1600 &pl->phylink_disable_state))
1601 phylink_mac_config(pl, &pl->link_config);
1602
1603 return ret;
1604}
1605
1606static void phylink_sfp_link_down(void *upstream)
1607{
1608 struct phylink *pl = upstream;
1609
1610 WARN_ON(!lockdep_rtnl_is_held());
1611
1612 set_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
1613 flush_work(&pl->resolve);
1614
1615 netif_carrier_off(pl->netdev);
1616}
1617
1618static void phylink_sfp_link_up(void *upstream)
1619{
1620 struct phylink *pl = upstream;
1621
1622 WARN_ON(!lockdep_rtnl_is_held());
1623
1624 clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
1625 phylink_run_resolve(pl);
1626}
1627
1628static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
1629{
1630 return phylink_connect_phy(upstream, phy);
1631}
1632
1633static void phylink_sfp_disconnect_phy(void *upstream)
1634{
1635 phylink_disconnect_phy(upstream);
1636}
1637
1638static const struct sfp_upstream_ops sfp_phylink_ops = {
1639 .module_insert = phylink_sfp_module_insert,
1640 .link_up = phylink_sfp_link_up,
1641 .link_down = phylink_sfp_link_down,
1642 .connect_phy = phylink_sfp_connect_phy,
1643 .disconnect_phy = phylink_sfp_disconnect_phy,
1644};
1645
Russell King9525ae82017-07-25 15:03:13 +01001646MODULE_LICENSE("GPL");