blob: 64e3eb73a2378d86ac532796a3426a8b7bf7b3b6 [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) {
Florian Fainellid38b4afd2017-12-12 16:00:27 -0800768 if (pl->link_an_mode == MLO_AN_PHY)
Russell King9525ae82017-07-25 15:03:13 +0100769 return -ENODEV;
Russell King9525ae82017-07-25 15:03:13 +0100770 return 0;
771 }
772
Florian Fainelli0a629642017-12-12 16:00:25 -0800773 phy_dev = of_phy_attach(pl->netdev, phy_node, flags,
774 pl->link_interface);
Russell King9525ae82017-07-25 15:03:13 +0100775 /* We're done with the phy_node handle */
776 of_node_put(phy_node);
777
778 if (!phy_dev)
779 return -ENODEV;
780
781 ret = phylink_bringup_phy(pl, phy_dev);
782 if (ret)
783 phy_detach(phy_dev);
784
785 return ret;
786}
787EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
788
Russell King8796c892017-12-01 10:24:47 +0000789/**
790 * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
791 * instance.
792 * @pl: a pointer to a &struct phylink returned from phylink_create()
793 *
794 * Disconnect any current PHY from the phylink instance described by @pl.
795 */
Russell King9525ae82017-07-25 15:03:13 +0100796void phylink_disconnect_phy(struct phylink *pl)
797{
798 struct phy_device *phy;
799
800 WARN_ON(!lockdep_rtnl_is_held());
801
802 phy = pl->phydev;
803 if (phy) {
804 mutex_lock(&phy->lock);
805 mutex_lock(&pl->state_mutex);
806 pl->netdev->phydev = NULL;
807 pl->phydev = NULL;
808 mutex_unlock(&pl->state_mutex);
809 mutex_unlock(&phy->lock);
810 flush_work(&pl->resolve);
811
812 phy_disconnect(phy);
813 }
814}
815EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
816
Russell King8796c892017-12-01 10:24:47 +0000817/**
818 * phylink_mac_change() - notify phylink of a change in MAC state
819 * @pl: a pointer to a &struct phylink returned from phylink_create()
820 * @up: indicates whether the link is currently up.
821 *
822 * The MAC driver should call this driver when the state of its link
823 * changes (eg, link failure, new negotiation results, etc.)
824 */
Russell King9525ae82017-07-25 15:03:13 +0100825void phylink_mac_change(struct phylink *pl, bool up)
826{
827 if (!up)
828 pl->mac_link_dropped = true;
829 phylink_run_resolve(pl);
830 netdev_dbg(pl->netdev, "mac link %s\n", up ? "up" : "down");
831}
832EXPORT_SYMBOL_GPL(phylink_mac_change);
833
Russell King8796c892017-12-01 10:24:47 +0000834/**
835 * phylink_start() - start a phylink instance
836 * @pl: a pointer to a &struct phylink returned from phylink_create()
837 *
838 * Start the phylink instance specified by @pl, configuring the MAC for the
839 * desired link mode(s) and negotiation style. This should be called from the
840 * network device driver's &struct net_device_ops ndo_open() method.
841 */
Russell King9525ae82017-07-25 15:03:13 +0100842void phylink_start(struct phylink *pl)
843{
844 WARN_ON(!lockdep_rtnl_is_held());
845
846 netdev_info(pl->netdev, "configuring for %s/%s link mode\n",
847 phylink_an_mode_str(pl->link_an_mode),
848 phy_modes(pl->link_config.interface));
849
850 /* Apply the link configuration to the MAC when starting. This allows
851 * a fixed-link to start with the correct parameters, and also
852 * ensures that we set the appropriate advertisment for Serdes links.
853 */
854 phylink_resolve_flow(pl, &pl->link_config);
855 phylink_mac_config(pl, &pl->link_config);
856
Russell King85b43942017-12-01 10:24:42 +0000857 /* Restart autonegotiation if using 802.3z to ensure that the link
858 * parameters are properly negotiated. This is necessary for DSA
859 * switches using 802.3z negotiation to ensure they see our modes.
860 */
861 phylink_mac_an_restart(pl);
862
Russell King9525ae82017-07-25 15:03:13 +0100863 clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
864 phylink_run_resolve(pl);
865
Russell Kingce0aa272017-07-25 15:03:18 +0100866 if (pl->sfp_bus)
867 sfp_upstream_start(pl->sfp_bus);
Russell King9525ae82017-07-25 15:03:13 +0100868 if (pl->phydev)
869 phy_start(pl->phydev);
870}
871EXPORT_SYMBOL_GPL(phylink_start);
872
Russell King8796c892017-12-01 10:24:47 +0000873/**
874 * phylink_stop() - stop a phylink instance
875 * @pl: a pointer to a &struct phylink returned from phylink_create()
876 *
877 * Stop the phylink instance specified by @pl. This should be called from the
878 * network device driver's &struct net_device_ops ndo_stop() method. The
879 * network device's carrier state should not be changed prior to calling this
880 * function.
881 */
Russell King9525ae82017-07-25 15:03:13 +0100882void phylink_stop(struct phylink *pl)
883{
884 WARN_ON(!lockdep_rtnl_is_held());
885
886 if (pl->phydev)
887 phy_stop(pl->phydev);
Russell Kingce0aa272017-07-25 15:03:18 +0100888 if (pl->sfp_bus)
889 sfp_upstream_stop(pl->sfp_bus);
Russell King9525ae82017-07-25 15:03:13 +0100890
891 set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
Russell King2012b7d2017-11-30 13:59:26 +0000892 queue_work(system_power_efficient_wq, &pl->resolve);
Russell King9525ae82017-07-25 15:03:13 +0100893 flush_work(&pl->resolve);
894}
895EXPORT_SYMBOL_GPL(phylink_stop);
896
Russell King8796c892017-12-01 10:24:47 +0000897/**
898 * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY
899 * @pl: a pointer to a &struct phylink returned from phylink_create()
900 * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters
901 *
902 * Read the wake on lan parameters from the PHY attached to the phylink
903 * instance specified by @pl. If no PHY is currently attached, report no
904 * support for wake on lan.
905 */
Russell King9525ae82017-07-25 15:03:13 +0100906void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
907{
908 WARN_ON(!lockdep_rtnl_is_held());
909
910 wol->supported = 0;
911 wol->wolopts = 0;
912
913 if (pl->phydev)
914 phy_ethtool_get_wol(pl->phydev, wol);
915}
916EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol);
917
Russell King8796c892017-12-01 10:24:47 +0000918/**
919 * phylink_ethtool_set_wol() - set wake on lan parameters
920 * @pl: a pointer to a &struct phylink returned from phylink_create()
921 * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters
922 *
923 * Set the wake on lan parameters for the PHY attached to the phylink
924 * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP
925 * error.
926 *
927 * Returns zero on success or negative errno code.
928 */
Russell King9525ae82017-07-25 15:03:13 +0100929int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
930{
931 int ret = -EOPNOTSUPP;
932
933 WARN_ON(!lockdep_rtnl_is_held());
934
935 if (pl->phydev)
936 ret = phy_ethtool_set_wol(pl->phydev, wol);
937
938 return ret;
939}
940EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol);
941
942static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b)
943{
944 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
945
946 linkmode_zero(mask);
947 phylink_set_port_modes(mask);
948
949 linkmode_and(dst, dst, mask);
950 linkmode_or(dst, dst, b);
951}
952
953static void phylink_get_ksettings(const struct phylink_link_state *state,
954 struct ethtool_link_ksettings *kset)
955{
956 phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
957 linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
958 kset->base.speed = state->speed;
959 kset->base.duplex = state->duplex;
960 kset->base.autoneg = state->an_enabled ? AUTONEG_ENABLE :
961 AUTONEG_DISABLE;
962}
963
Russell King8796c892017-12-01 10:24:47 +0000964/**
965 * phylink_ethtool_ksettings_get() - get the current link settings
966 * @pl: a pointer to a &struct phylink returned from phylink_create()
967 * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings
968 *
969 * Read the current link settings for the phylink instance specified by @pl.
970 * This will be the link settings read from the MAC, PHY or fixed link
971 * settings depending on the current negotiation mode.
972 */
Russell King9525ae82017-07-25 15:03:13 +0100973int phylink_ethtool_ksettings_get(struct phylink *pl,
Florian Fainelli516b29e2017-10-30 21:42:57 -0700974 struct ethtool_link_ksettings *kset)
Russell King9525ae82017-07-25 15:03:13 +0100975{
976 struct phylink_link_state link_state;
977
978 WARN_ON(!lockdep_rtnl_is_held());
979
980 if (pl->phydev) {
981 phy_ethtool_ksettings_get(pl->phydev, kset);
982 } else {
983 kset->base.port = pl->link_port;
984 }
985
986 linkmode_copy(kset->link_modes.supported, pl->supported);
987
988 switch (pl->link_an_mode) {
989 case MLO_AN_FIXED:
990 /* We are using fixed settings. Report these as the
991 * current link settings - and note that these also
992 * represent the supported speeds/duplex/pause modes.
993 */
994 phylink_get_fixed_state(pl, &link_state);
995 phylink_get_ksettings(&link_state, kset);
996 break;
997
Russell King86a362c2017-12-01 10:24:26 +0000998 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +0100999 /* If there is a phy attached, then use the reported
1000 * settings from the phy with no modification.
1001 */
1002 if (pl->phydev)
1003 break;
1004
Russell King9525ae82017-07-25 15:03:13 +01001005 phylink_get_mac_state(pl, &link_state);
1006
1007 /* The MAC is reporting the link results from its own PCS
1008 * layer via in-band status. Report these as the current
1009 * link settings.
1010 */
1011 phylink_get_ksettings(&link_state, kset);
1012 break;
1013 }
1014
1015 return 0;
1016}
1017EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get);
1018
Russell King8796c892017-12-01 10:24:47 +00001019/**
1020 * phylink_ethtool_ksettings_set() - set the link settings
1021 * @pl: a pointer to a &struct phylink returned from phylink_create()
1022 * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes
1023 */
Russell King9525ae82017-07-25 15:03:13 +01001024int phylink_ethtool_ksettings_set(struct phylink *pl,
Florian Fainelli516b29e2017-10-30 21:42:57 -07001025 const struct ethtool_link_ksettings *kset)
Russell King9525ae82017-07-25 15:03:13 +01001026{
1027 struct ethtool_link_ksettings our_kset;
1028 struct phylink_link_state config;
1029 int ret;
1030
1031 WARN_ON(!lockdep_rtnl_is_held());
1032
1033 if (kset->base.autoneg != AUTONEG_DISABLE &&
1034 kset->base.autoneg != AUTONEG_ENABLE)
1035 return -EINVAL;
1036
1037 config = pl->link_config;
1038
1039 /* Mask out unsupported advertisments */
1040 linkmode_and(config.advertising, kset->link_modes.advertising,
1041 pl->supported);
1042
1043 /* FIXME: should we reject autoneg if phy/mac does not support it? */
1044 if (kset->base.autoneg == AUTONEG_DISABLE) {
1045 const struct phy_setting *s;
1046
1047 /* Autonegotiation disabled, select a suitable speed and
1048 * duplex.
1049 */
1050 s = phy_lookup_setting(kset->base.speed, kset->base.duplex,
1051 pl->supported,
1052 __ETHTOOL_LINK_MODE_MASK_NBITS, false);
1053 if (!s)
1054 return -EINVAL;
1055
1056 /* If we have a fixed link (as specified by firmware), refuse
1057 * to change link parameters.
1058 */
1059 if (pl->link_an_mode == MLO_AN_FIXED &&
1060 (s->speed != pl->link_config.speed ||
1061 s->duplex != pl->link_config.duplex))
1062 return -EINVAL;
1063
1064 config.speed = s->speed;
1065 config.duplex = s->duplex;
1066 config.an_enabled = false;
1067
1068 __clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
1069 } else {
1070 /* If we have a fixed link, refuse to enable autonegotiation */
1071 if (pl->link_an_mode == MLO_AN_FIXED)
1072 return -EINVAL;
1073
1074 config.speed = SPEED_UNKNOWN;
1075 config.duplex = DUPLEX_UNKNOWN;
1076 config.an_enabled = true;
1077
1078 __set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
1079 }
1080
1081 if (phylink_validate(pl, pl->supported, &config))
1082 return -EINVAL;
1083
1084 /* If autonegotiation is enabled, we must have an advertisment */
1085 if (config.an_enabled && phylink_is_empty_linkmode(config.advertising))
1086 return -EINVAL;
1087
1088 our_kset = *kset;
1089 linkmode_copy(our_kset.link_modes.advertising, config.advertising);
1090 our_kset.base.speed = config.speed;
1091 our_kset.base.duplex = config.duplex;
1092
1093 /* If we have a PHY, configure the phy */
1094 if (pl->phydev) {
1095 ret = phy_ethtool_ksettings_set(pl->phydev, &our_kset);
1096 if (ret)
1097 return ret;
1098 }
1099
1100 mutex_lock(&pl->state_mutex);
1101 /* Configure the MAC to match the new settings */
1102 linkmode_copy(pl->link_config.advertising, our_kset.link_modes.advertising);
1103 pl->link_config.speed = our_kset.base.speed;
1104 pl->link_config.duplex = our_kset.base.duplex;
1105 pl->link_config.an_enabled = our_kset.base.autoneg != AUTONEG_DISABLE;
1106
1107 if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
1108 phylink_mac_config(pl, &pl->link_config);
1109 phylink_mac_an_restart(pl);
1110 }
1111 mutex_unlock(&pl->state_mutex);
1112
Dan Carpenterd18c2a12017-08-10 00:35:50 +03001113 return 0;
Russell King9525ae82017-07-25 15:03:13 +01001114}
1115EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
1116
Russell King8796c892017-12-01 10:24:47 +00001117/**
1118 * phylink_ethtool_nway_reset() - restart negotiation
1119 * @pl: a pointer to a &struct phylink returned from phylink_create()
1120 *
1121 * Restart negotiation for the phylink instance specified by @pl. This will
1122 * cause any attached phy to restart negotiation with the link partner, and
1123 * if the MAC is in a BaseX mode, the MAC will also be requested to restart
1124 * negotiation.
1125 *
1126 * Returns zero on success, or negative error code.
1127 */
Russell King9525ae82017-07-25 15:03:13 +01001128int phylink_ethtool_nway_reset(struct phylink *pl)
1129{
1130 int ret = 0;
1131
1132 WARN_ON(!lockdep_rtnl_is_held());
1133
1134 if (pl->phydev)
1135 ret = phy_restart_aneg(pl->phydev);
1136 phylink_mac_an_restart(pl);
1137
1138 return ret;
1139}
1140EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
1141
Russell King8796c892017-12-01 10:24:47 +00001142/**
1143 * phylink_ethtool_get_pauseparam() - get the current pause parameters
1144 * @pl: a pointer to a &struct phylink returned from phylink_create()
1145 * @pause: a pointer to a &struct ethtool_pauseparam
1146 */
Russell King9525ae82017-07-25 15:03:13 +01001147void phylink_ethtool_get_pauseparam(struct phylink *pl,
1148 struct ethtool_pauseparam *pause)
1149{
1150 WARN_ON(!lockdep_rtnl_is_held());
1151
1152 pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
1153 pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
1154 pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
1155}
1156EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
1157
Russell King8796c892017-12-01 10:24:47 +00001158/**
1159 * phylink_ethtool_set_pauseparam() - set the current pause parameters
1160 * @pl: a pointer to a &struct phylink returned from phylink_create()
1161 * @pause: a pointer to a &struct ethtool_pauseparam
1162 */
Russell King9525ae82017-07-25 15:03:13 +01001163int phylink_ethtool_set_pauseparam(struct phylink *pl,
1164 struct ethtool_pauseparam *pause)
1165{
1166 struct phylink_link_state *config = &pl->link_config;
1167
1168 WARN_ON(!lockdep_rtnl_is_held());
1169
1170 if (!phylink_test(pl->supported, Pause) &&
1171 !phylink_test(pl->supported, Asym_Pause))
1172 return -EOPNOTSUPP;
1173
1174 if (!phylink_test(pl->supported, Asym_Pause) &&
1175 !pause->autoneg && pause->rx_pause != pause->tx_pause)
1176 return -EINVAL;
1177
1178 config->pause &= ~(MLO_PAUSE_AN | MLO_PAUSE_TXRX_MASK);
1179
1180 if (pause->autoneg)
1181 config->pause |= MLO_PAUSE_AN;
1182 if (pause->rx_pause)
1183 config->pause |= MLO_PAUSE_RX;
1184 if (pause->tx_pause)
1185 config->pause |= MLO_PAUSE_TX;
1186
1187 if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
1188 switch (pl->link_an_mode) {
1189 case MLO_AN_PHY:
1190 /* Silently mark the carrier down, and then trigger a resolve */
1191 netif_carrier_off(pl->netdev);
1192 phylink_run_resolve(pl);
1193 break;
1194
1195 case MLO_AN_FIXED:
1196 /* Should we allow fixed links to change against the config? */
1197 phylink_resolve_flow(pl, config);
1198 phylink_mac_config(pl, config);
1199 break;
1200
Russell King86a362c2017-12-01 10:24:26 +00001201 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001202 phylink_mac_config(pl, config);
1203 phylink_mac_an_restart(pl);
1204 break;
1205 }
1206 }
1207
1208 return 0;
1209}
1210EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
1211
Russell King770a1ad2017-07-25 15:03:23 +01001212int phylink_ethtool_get_module_info(struct phylink *pl,
1213 struct ethtool_modinfo *modinfo)
1214{
1215 int ret = -EOPNOTSUPP;
1216
1217 WARN_ON(!lockdep_rtnl_is_held());
1218
1219 if (pl->sfp_bus)
1220 ret = sfp_get_module_info(pl->sfp_bus, modinfo);
1221
1222 return ret;
1223}
1224EXPORT_SYMBOL_GPL(phylink_ethtool_get_module_info);
1225
1226int phylink_ethtool_get_module_eeprom(struct phylink *pl,
1227 struct ethtool_eeprom *ee, u8 *buf)
1228{
1229 int ret = -EOPNOTSUPP;
1230
1231 WARN_ON(!lockdep_rtnl_is_held());
1232
1233 if (pl->sfp_bus)
1234 ret = sfp_get_module_eeprom(pl->sfp_bus, ee, buf);
1235
1236 return ret;
1237}
1238EXPORT_SYMBOL_GPL(phylink_ethtool_get_module_eeprom);
1239
Russell King8796c892017-12-01 10:24:47 +00001240/**
1241 * phylink_ethtool_get_eee_err() - read the energy efficient ethernet error
1242 * counter
1243 * @pl: a pointer to a &struct phylink returned from phylink_create().
1244 *
1245 * Read the Energy Efficient Ethernet error counter from the PHY associated
1246 * with the phylink instance specified by @pl.
1247 *
1248 * Returns positive error counter value, or negative error code.
1249 */
Russell King9525ae82017-07-25 15:03:13 +01001250int phylink_get_eee_err(struct phylink *pl)
1251{
1252 int ret = 0;
1253
1254 WARN_ON(!lockdep_rtnl_is_held());
1255
1256 if (pl->phydev)
1257 ret = phy_get_eee_err(pl->phydev);
1258
1259 return ret;
1260}
1261EXPORT_SYMBOL_GPL(phylink_get_eee_err);
1262
Russell King8796c892017-12-01 10:24:47 +00001263/**
1264 * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters
1265 * @pl: a pointer to a &struct phylink returned from phylink_create()
1266 * @eee: a pointer to a &struct ethtool_eee for the read parameters
1267 */
Russell King9525ae82017-07-25 15:03:13 +01001268int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee)
1269{
1270 int ret = -EOPNOTSUPP;
1271
1272 WARN_ON(!lockdep_rtnl_is_held());
1273
1274 if (pl->phydev)
1275 ret = phy_ethtool_get_eee(pl->phydev, eee);
1276
1277 return ret;
1278}
1279EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
1280
Russell King8796c892017-12-01 10:24:47 +00001281/**
1282 * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters
1283 * @pl: a pointer to a &struct phylink returned from phylink_create()
1284 * @eee: a pointer to a &struct ethtool_eee for the desired parameters
1285 */
Russell King9525ae82017-07-25 15:03:13 +01001286int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_eee *eee)
1287{
1288 int ret = -EOPNOTSUPP;
1289
1290 WARN_ON(!lockdep_rtnl_is_held());
1291
1292 if (pl->phydev)
1293 ret = phy_ethtool_set_eee(pl->phydev, eee);
1294
1295 return ret;
1296}
1297EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
1298
1299/* This emulates MII registers for a fixed-mode phy operating as per the
1300 * passed in state. "aneg" defines if we report negotiation is possible.
1301 *
1302 * FIXME: should deal with negotiation state too.
1303 */
1304static int phylink_mii_emul_read(struct net_device *ndev, unsigned int reg,
1305 struct phylink_link_state *state, bool aneg)
1306{
1307 struct fixed_phy_status fs;
1308 int val;
1309
1310 fs.link = state->link;
1311 fs.speed = state->speed;
1312 fs.duplex = state->duplex;
1313 fs.pause = state->pause & MLO_PAUSE_SYM;
1314 fs.asym_pause = state->pause & MLO_PAUSE_ASYM;
1315
1316 val = swphy_read_reg(reg, &fs);
1317 if (reg == MII_BMSR) {
1318 if (!state->an_complete)
1319 val &= ~BMSR_ANEGCOMPLETE;
1320 if (!aneg)
1321 val &= ~BMSR_ANEGCAPABLE;
1322 }
1323 return val;
1324}
1325
Russell Kingecbd87b2017-07-25 15:03:28 +01001326static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
1327 unsigned int reg)
1328{
1329 struct phy_device *phydev = pl->phydev;
1330 int prtad, devad;
1331
1332 if (mdio_phy_id_is_c45(phy_id)) {
1333 prtad = mdio_phy_id_prtad(phy_id);
1334 devad = mdio_phy_id_devad(phy_id);
1335 devad = MII_ADDR_C45 | devad << 16 | reg;
1336 } else if (phydev->is_c45) {
1337 switch (reg) {
1338 case MII_BMCR:
1339 case MII_BMSR:
1340 case MII_PHYSID1:
1341 case MII_PHYSID2:
1342 devad = __ffs(phydev->c45_ids.devices_in_package);
1343 break;
1344 case MII_ADVERTISE:
1345 case MII_LPA:
1346 if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1347 return -EINVAL;
1348 devad = MDIO_MMD_AN;
1349 if (reg == MII_ADVERTISE)
1350 reg = MDIO_AN_ADVERTISE;
1351 else
1352 reg = MDIO_AN_LPA;
1353 break;
1354 default:
1355 return -EINVAL;
1356 }
1357 prtad = phy_id;
1358 devad = MII_ADDR_C45 | devad << 16 | reg;
1359 } else {
1360 prtad = phy_id;
1361 devad = reg;
1362 }
1363 return mdiobus_read(pl->phydev->mdio.bus, prtad, devad);
1364}
1365
1366static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
1367 unsigned int reg, unsigned int val)
1368{
1369 struct phy_device *phydev = pl->phydev;
1370 int prtad, devad;
1371
1372 if (mdio_phy_id_is_c45(phy_id)) {
1373 prtad = mdio_phy_id_prtad(phy_id);
1374 devad = mdio_phy_id_devad(phy_id);
1375 devad = MII_ADDR_C45 | devad << 16 | reg;
1376 } else if (phydev->is_c45) {
1377 switch (reg) {
1378 case MII_BMCR:
1379 case MII_BMSR:
1380 case MII_PHYSID1:
1381 case MII_PHYSID2:
1382 devad = __ffs(phydev->c45_ids.devices_in_package);
1383 break;
1384 case MII_ADVERTISE:
1385 case MII_LPA:
1386 if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1387 return -EINVAL;
1388 devad = MDIO_MMD_AN;
1389 if (reg == MII_ADVERTISE)
1390 reg = MDIO_AN_ADVERTISE;
1391 else
1392 reg = MDIO_AN_LPA;
1393 break;
1394 default:
1395 return -EINVAL;
1396 }
1397 prtad = phy_id;
1398 devad = MII_ADDR_C45 | devad << 16 | reg;
1399 } else {
1400 prtad = phy_id;
1401 devad = reg;
1402 }
1403
1404 return mdiobus_write(phydev->mdio.bus, prtad, devad, val);
1405}
1406
Russell King9525ae82017-07-25 15:03:13 +01001407static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
1408 unsigned int reg)
1409{
1410 struct phylink_link_state state;
1411 int val = 0xffff;
1412
Russell King9525ae82017-07-25 15:03:13 +01001413 switch (pl->link_an_mode) {
1414 case MLO_AN_FIXED:
1415 if (phy_id == 0) {
1416 phylink_get_fixed_state(pl, &state);
1417 val = phylink_mii_emul_read(pl->netdev, reg, &state,
1418 true);
1419 }
1420 break;
1421
1422 case MLO_AN_PHY:
1423 return -EOPNOTSUPP;
1424
Russell King86a362c2017-12-01 10:24:26 +00001425 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001426 if (phy_id == 0) {
1427 val = phylink_get_mac_state(pl, &state);
1428 if (val < 0)
1429 return val;
1430
1431 val = phylink_mii_emul_read(pl->netdev, reg, &state,
1432 true);
1433 }
1434 break;
1435 }
1436
1437 return val & 0xffff;
1438}
1439
1440static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
1441 unsigned int reg, unsigned int val)
1442{
Russell King9525ae82017-07-25 15:03:13 +01001443 switch (pl->link_an_mode) {
1444 case MLO_AN_FIXED:
1445 break;
1446
1447 case MLO_AN_PHY:
1448 return -EOPNOTSUPP;
1449
Russell King86a362c2017-12-01 10:24:26 +00001450 case MLO_AN_INBAND:
Russell King9525ae82017-07-25 15:03:13 +01001451 break;
1452 }
1453
1454 return 0;
1455}
1456
Russell King8796c892017-12-01 10:24:47 +00001457/**
1458 * phylink_mii_ioctl() - generic mii ioctl interface
1459 * @pl: a pointer to a &struct phylink returned from phylink_create()
1460 * @ifr: a pointer to a &struct ifreq for socket ioctls
1461 * @cmd: ioctl cmd to execute
1462 *
1463 * Perform the specified MII ioctl on the PHY attached to the phylink instance
1464 * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
1465 *
1466 * Returns: zero on success or negative error code.
1467 *
1468 * %SIOCGMIIPHY:
1469 * read register from the current PHY.
1470 * %SIOCGMIIREG:
1471 * read register from the specified PHY.
1472 * %SIOCSMIIREG:
1473 * set a register on the specified PHY.
1474 */
Russell King9525ae82017-07-25 15:03:13 +01001475int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
1476{
Russell Kingecbd87b2017-07-25 15:03:28 +01001477 struct mii_ioctl_data *mii = if_mii(ifr);
1478 int ret;
Russell King9525ae82017-07-25 15:03:13 +01001479
1480 WARN_ON(!lockdep_rtnl_is_held());
1481
Russell Kingecbd87b2017-07-25 15:03:28 +01001482 if (pl->phydev) {
Russell King86a362c2017-12-01 10:24:26 +00001483 /* PHYs only exist for MLO_AN_PHY and SGMII */
Russell Kingecbd87b2017-07-25 15:03:28 +01001484 switch (cmd) {
1485 case SIOCGMIIPHY:
1486 mii->phy_id = pl->phydev->mdio.addr;
Russell King9525ae82017-07-25 15:03:13 +01001487
Russell Kingecbd87b2017-07-25 15:03:28 +01001488 case SIOCGMIIREG:
1489 ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
1490 if (ret >= 0) {
1491 mii->val_out = ret;
1492 ret = 0;
1493 }
1494 break;
Russell King9525ae82017-07-25 15:03:13 +01001495
Russell Kingecbd87b2017-07-25 15:03:28 +01001496 case SIOCSMIIREG:
1497 ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
1498 mii->val_in);
1499 break;
Russell King9525ae82017-07-25 15:03:13 +01001500
Russell Kingecbd87b2017-07-25 15:03:28 +01001501 default:
Russell King9525ae82017-07-25 15:03:13 +01001502 ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
Russell Kingecbd87b2017-07-25 15:03:28 +01001503 break;
1504 }
1505 } else {
1506 switch (cmd) {
1507 case SIOCGMIIPHY:
1508 mii->phy_id = 0;
1509
1510 case SIOCGMIIREG:
1511 ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
1512 if (ret >= 0) {
1513 mii->val_out = ret;
1514 ret = 0;
1515 }
1516 break;
1517
1518 case SIOCSMIIREG:
1519 ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
1520 mii->val_in);
1521 break;
1522
1523 default:
1524 ret = -EOPNOTSUPP;
1525 break;
1526 }
Russell King9525ae82017-07-25 15:03:13 +01001527 }
1528
1529 return ret;
1530}
1531EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
1532
Russell Kingce0aa272017-07-25 15:03:18 +01001533static int phylink_sfp_module_insert(void *upstream,
1534 const struct sfp_eeprom_id *id)
1535{
1536 struct phylink *pl = upstream;
1537 __ETHTOOL_DECLARE_LINK_MODE_MASK(support) = { 0, };
1538 struct phylink_link_state config;
1539 phy_interface_t iface;
1540 int mode, ret = 0;
1541 bool changed;
1542 u8 port;
1543
1544 sfp_parse_support(pl->sfp_bus, id, support);
1545 port = sfp_parse_port(pl->sfp_bus, id, support);
1546 iface = sfp_parse_interface(pl->sfp_bus, id);
1547
1548 WARN_ON(!lockdep_rtnl_is_held());
1549
1550 switch (iface) {
1551 case PHY_INTERFACE_MODE_SGMII:
Russell Kingce0aa272017-07-25 15:03:18 +01001552 case PHY_INTERFACE_MODE_1000BASEX:
Russell King4336c402017-12-01 10:24:32 +00001553 case PHY_INTERFACE_MODE_2500BASEX:
1554 case PHY_INTERFACE_MODE_10GKR:
Russell King86a362c2017-12-01 10:24:26 +00001555 mode = MLO_AN_INBAND;
Russell Kingce0aa272017-07-25 15:03:18 +01001556 break;
1557 default:
1558 return -EINVAL;
1559 }
1560
1561 memset(&config, 0, sizeof(config));
1562 linkmode_copy(config.advertising, support);
1563 config.interface = iface;
1564 config.speed = SPEED_UNKNOWN;
1565 config.duplex = DUPLEX_UNKNOWN;
1566 config.pause = MLO_PAUSE_AN;
1567 config.an_enabled = pl->link_config.an_enabled;
1568
1569 /* Ignore errors if we're expecting a PHY to attach later */
1570 ret = phylink_validate(pl, support, &config);
1571 if (ret) {
1572 netdev_err(pl->netdev, "validation of %s/%s with support %*pb failed: %d\n",
1573 phylink_an_mode_str(mode), phy_modes(config.interface),
1574 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
1575 return ret;
1576 }
1577
1578 netdev_dbg(pl->netdev, "requesting link mode %s/%s with support %*pb\n",
1579 phylink_an_mode_str(mode), phy_modes(config.interface),
1580 __ETHTOOL_LINK_MODE_MASK_NBITS, support);
1581
Russell King86a362c2017-12-01 10:24:26 +00001582 if (phy_interface_mode_is_8023z(iface) && pl->phydev)
Russell Kingce0aa272017-07-25 15:03:18 +01001583 return -EINVAL;
1584
1585 changed = !bitmap_equal(pl->supported, support,
1586 __ETHTOOL_LINK_MODE_MASK_NBITS);
1587 if (changed) {
1588 linkmode_copy(pl->supported, support);
1589 linkmode_copy(pl->link_config.advertising, config.advertising);
1590 }
1591
1592 if (pl->link_an_mode != mode ||
1593 pl->link_config.interface != config.interface) {
1594 pl->link_config.interface = config.interface;
1595 pl->link_an_mode = mode;
1596
1597 changed = true;
1598
1599 netdev_info(pl->netdev, "switched to %s/%s link mode\n",
1600 phylink_an_mode_str(mode),
1601 phy_modes(config.interface));
1602 }
1603
1604 pl->link_port = port;
1605
1606 if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
1607 &pl->phylink_disable_state))
1608 phylink_mac_config(pl, &pl->link_config);
1609
1610 return ret;
1611}
1612
1613static void phylink_sfp_link_down(void *upstream)
1614{
1615 struct phylink *pl = upstream;
1616
1617 WARN_ON(!lockdep_rtnl_is_held());
1618
1619 set_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
1620 flush_work(&pl->resolve);
1621
1622 netif_carrier_off(pl->netdev);
1623}
1624
1625static void phylink_sfp_link_up(void *upstream)
1626{
1627 struct phylink *pl = upstream;
1628
1629 WARN_ON(!lockdep_rtnl_is_held());
1630
1631 clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
1632 phylink_run_resolve(pl);
1633}
1634
1635static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
1636{
1637 return phylink_connect_phy(upstream, phy);
1638}
1639
1640static void phylink_sfp_disconnect_phy(void *upstream)
1641{
1642 phylink_disconnect_phy(upstream);
1643}
1644
1645static const struct sfp_upstream_ops sfp_phylink_ops = {
1646 .module_insert = phylink_sfp_module_insert,
1647 .link_up = phylink_sfp_link_up,
1648 .link_down = phylink_sfp_link_down,
1649 .connect_phy = phylink_sfp_connect_phy,
1650 .disconnect_phy = phylink_sfp_disconnect_phy,
1651};
1652
Russell King9525ae82017-07-25 15:03:13 +01001653MODULE_LICENSE("GPL");