1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * phylink models the MAC to optional PHY connection, supporting
4 * technologies such as SFP cages where the PHY is hot-pluggable.
5 *
6 * Copyright (C) 2015 Russell King
7 */
8 #include <linux/ethtool.h>
9 #include <linux/export.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/netdevice.h>
12 #include <linux/of.h>
13 #include <linux/of_mdio.h>
14 #include <linux/phy.h>
15 #include <linux/phy_fixed.h>
16 #include <linux/phylink.h>
17 #include <linux/rtnetlink.h>
18 #include <linux/spinlock.h>
19 #include <linux/timer.h>
20 #include <linux/workqueue.h>
21
22 #include "sfp.h"
23 #include "swphy.h"
24
25 #define SUPPORTED_INTERFACES \
26 (SUPPORTED_TP | SUPPORTED_MII | SUPPORTED_FIBRE | \
27 SUPPORTED_BNC | SUPPORTED_AUI | SUPPORTED_Backplane)
28 #define ADVERTISED_INTERFACES \
29 (ADVERTISED_TP | ADVERTISED_MII | ADVERTISED_FIBRE | \
30 ADVERTISED_BNC | ADVERTISED_AUI | ADVERTISED_Backplane)
31
32 enum {
33 PHYLINK_DISABLE_STOPPED,
34 PHYLINK_DISABLE_LINK,
35 };
36
37 /**
38 * struct phylink - internal data type for phylink
39 */
40 struct phylink {
41 /* private: */
42 struct net_device *netdev;
43 const struct phylink_mac_ops *mac_ops;
44 const struct phylink_pcs_ops *pcs_ops;
45 struct phylink_config *config;
46 struct phylink_pcs *pcs;
47 struct device *dev;
48 unsigned int old_link_state:1;
49
50 unsigned long phylink_disable_state; /* bitmask of disables */
51 struct phy_device *phydev;
52 phy_interface_t link_interface; /* PHY_INTERFACE_xxx */
53 u8 cfg_link_an_mode; /* MLO_AN_xxx */
54 u8 cur_link_an_mode;
55 u8 link_port; /* The current non-phy ethtool port */
56 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
57
58 /* The link configuration settings */
59 struct phylink_link_state link_config;
60
61 /* The current settings */
62 phy_interface_t cur_interface;
63
64 struct gpio_desc *link_gpio;
65 unsigned int link_irq;
66 struct timer_list link_poll;
67 void (*get_fixed_state)(struct net_device *dev,
68 struct phylink_link_state *s);
69
70 struct mutex state_mutex;
71 struct phylink_link_state phy_state;
72 struct work_struct resolve;
73
74 bool mac_link_dropped;
75
76 struct sfp_bus *sfp_bus;
77 bool sfp_may_have_phy;
78 __ETHTOOL_DECLARE_LINK_MODE_MASK(sfp_support);
79 u8 sfp_port;
80 };
81
82 #define phylink_printk(level, pl, fmt, ...) \
83 do { \
84 if ((pl)->config->type == PHYLINK_NETDEV) \
85 netdev_printk(level, (pl)->netdev, fmt, ##__VA_ARGS__); \
86 else if ((pl)->config->type == PHYLINK_DEV) \
87 dev_printk(level, (pl)->dev, fmt, ##__VA_ARGS__); \
88 } while (0)
89
90 #define phylink_err(pl, fmt, ...) \
91 phylink_printk(KERN_ERR, pl, fmt, ##__VA_ARGS__)
92 #define phylink_warn(pl, fmt, ...) \
93 phylink_printk(KERN_WARNING, pl, fmt, ##__VA_ARGS__)
94 #define phylink_info(pl, fmt, ...) \
95 phylink_printk(KERN_INFO, pl, fmt, ##__VA_ARGS__)
96 #if defined(CONFIG_DYNAMIC_DEBUG)
97 #define phylink_dbg(pl, fmt, ...) \
98 do { \
99 if ((pl)->config->type == PHYLINK_NETDEV) \
100 netdev_dbg((pl)->netdev, fmt, ##__VA_ARGS__); \
101 else if ((pl)->config->type == PHYLINK_DEV) \
102 dev_dbg((pl)->dev, fmt, ##__VA_ARGS__); \
103 } while (0)
104 #elif defined(DEBUG)
105 #define phylink_dbg(pl, fmt, ...) \
106 phylink_printk(KERN_DEBUG, pl, fmt, ##__VA_ARGS__)
107 #else
108 #define phylink_dbg(pl, fmt, ...) \
109 ({ \
110 if (0) \
111 phylink_printk(KERN_DEBUG, pl, fmt, ##__VA_ARGS__); \
112 })
113 #endif
114
115 /**
116 * phylink_set_port_modes() - set the port type modes in the ethtool mask
117 * @mask: ethtool link mode mask
118 *
119 * Sets all the port type modes in the ethtool mask. MAC drivers should
120 * use this in their 'validate' callback.
121 */
phylink_set_port_modes(unsigned long * mask)122 void phylink_set_port_modes(unsigned long *mask)
123 {
124 phylink_set(mask, TP);
125 phylink_set(mask, AUI);
126 phylink_set(mask, MII);
127 phylink_set(mask, FIBRE);
128 phylink_set(mask, BNC);
129 phylink_set(mask, Backplane);
130 }
131 EXPORT_SYMBOL_GPL(phylink_set_port_modes);
132
phylink_is_empty_linkmode(const unsigned long * linkmode)133 static int phylink_is_empty_linkmode(const unsigned long *linkmode)
134 {
135 __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp) = { 0, };
136
137 phylink_set_port_modes(tmp);
138 phylink_set(tmp, Autoneg);
139 phylink_set(tmp, Pause);
140 phylink_set(tmp, Asym_Pause);
141
142 return linkmode_subset(linkmode, tmp);
143 }
144
phylink_an_mode_str(unsigned int mode)145 static const char *phylink_an_mode_str(unsigned int mode)
146 {
147 static const char *modestr[] = {
148 [MLO_AN_PHY] = "phy",
149 [MLO_AN_FIXED] = "fixed",
150 [MLO_AN_INBAND] = "inband",
151 };
152
153 return mode < ARRAY_SIZE(modestr) ? modestr[mode] : "unknown";
154 }
155
phylink_validate(struct phylink * pl,unsigned long * supported,struct phylink_link_state * state)156 static int phylink_validate(struct phylink *pl, unsigned long *supported,
157 struct phylink_link_state *state)
158 {
159 pl->mac_ops->validate(pl->config, supported, state);
160
161 return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
162 }
163
phylink_parse_fixedlink(struct phylink * pl,struct fwnode_handle * fwnode)164 static int phylink_parse_fixedlink(struct phylink *pl,
165 struct fwnode_handle *fwnode)
166 {
167 struct fwnode_handle *fixed_node;
168 const struct phy_setting *s;
169 struct gpio_desc *desc;
170 u32 speed;
171 int ret;
172
173 fixed_node = fwnode_get_named_child_node(fwnode, "fixed-link");
174 if (fixed_node) {
175 ret = fwnode_property_read_u32(fixed_node, "speed", &speed);
176
177 pl->link_config.speed = speed;
178 pl->link_config.duplex = DUPLEX_HALF;
179
180 if (fwnode_property_read_bool(fixed_node, "full-duplex"))
181 pl->link_config.duplex = DUPLEX_FULL;
182
183 /* We treat the "pause" and "asym-pause" terminology as
184 * defining the link partner's ability. */
185 if (fwnode_property_read_bool(fixed_node, "pause"))
186 __set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
187 pl->link_config.lp_advertising);
188 if (fwnode_property_read_bool(fixed_node, "asym-pause"))
189 __set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
190 pl->link_config.lp_advertising);
191
192 if (ret == 0) {
193 desc = fwnode_gpiod_get_index(fixed_node, "link", 0,
194 GPIOD_IN, "?");
195
196 if (!IS_ERR(desc))
197 pl->link_gpio = desc;
198 else if (desc == ERR_PTR(-EPROBE_DEFER))
199 ret = -EPROBE_DEFER;
200 }
201 fwnode_handle_put(fixed_node);
202
203 if (ret)
204 return ret;
205 } else {
206 u32 prop[5];
207
208 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
209 NULL, 0);
210 if (ret != ARRAY_SIZE(prop)) {
211 phylink_err(pl, "broken fixed-link?\n");
212 return -EINVAL;
213 }
214
215 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
216 prop, ARRAY_SIZE(prop));
217 if (!ret) {
218 pl->link_config.duplex = prop[1] ?
219 DUPLEX_FULL : DUPLEX_HALF;
220 pl->link_config.speed = prop[2];
221 if (prop[3])
222 __set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
223 pl->link_config.lp_advertising);
224 if (prop[4])
225 __set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
226 pl->link_config.lp_advertising);
227 }
228 }
229
230 if (pl->link_config.speed > SPEED_1000 &&
231 pl->link_config.duplex != DUPLEX_FULL)
232 phylink_warn(pl, "fixed link specifies half duplex for %dMbps link?\n",
233 pl->link_config.speed);
234
235 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
236 linkmode_copy(pl->link_config.advertising, pl->supported);
237 phylink_validate(pl, pl->supported, &pl->link_config);
238
239 s = phy_lookup_setting(pl->link_config.speed, pl->link_config.duplex,
240 pl->supported, true);
241 linkmode_zero(pl->supported);
242 phylink_set(pl->supported, MII);
243 phylink_set(pl->supported, Pause);
244 phylink_set(pl->supported, Asym_Pause);
245 phylink_set(pl->supported, Autoneg);
246 if (s) {
247 __set_bit(s->bit, pl->supported);
248 __set_bit(s->bit, pl->link_config.lp_advertising);
249 } else {
250 phylink_warn(pl, "fixed link %s duplex %dMbps not recognised\n",
251 pl->link_config.duplex == DUPLEX_FULL ? "full" : "half",
252 pl->link_config.speed);
253 }
254
255 linkmode_and(pl->link_config.advertising, pl->link_config.advertising,
256 pl->supported);
257
258 pl->link_config.link = 1;
259 pl->link_config.an_complete = 1;
260
261 return 0;
262 }
263
phylink_parse_mode(struct phylink * pl,struct fwnode_handle * fwnode)264 static int phylink_parse_mode(struct phylink *pl, struct fwnode_handle *fwnode)
265 {
266 struct fwnode_handle *dn;
267 const char *managed;
268
269 dn = fwnode_get_named_child_node(fwnode, "fixed-link");
270 if (dn || fwnode_property_present(fwnode, "fixed-link"))
271 pl->cfg_link_an_mode = MLO_AN_FIXED;
272 fwnode_handle_put(dn);
273
274 if (fwnode_property_read_string(fwnode, "managed", &managed) == 0 &&
275 strcmp(managed, "in-band-status") == 0) {
276 if (pl->cfg_link_an_mode == MLO_AN_FIXED) {
277 phylink_err(pl,
278 "can't use both fixed-link and in-band-status\n");
279 return -EINVAL;
280 }
281
282 linkmode_zero(pl->supported);
283 phylink_set(pl->supported, MII);
284 phylink_set(pl->supported, Autoneg);
285 phylink_set(pl->supported, Asym_Pause);
286 phylink_set(pl->supported, Pause);
287 pl->link_config.an_enabled = true;
288 pl->cfg_link_an_mode = MLO_AN_INBAND;
289
290 switch (pl->link_config.interface) {
291 case PHY_INTERFACE_MODE_SGMII:
292 case PHY_INTERFACE_MODE_QSGMII:
293 phylink_set(pl->supported, 10baseT_Half);
294 phylink_set(pl->supported, 10baseT_Full);
295 phylink_set(pl->supported, 100baseT_Half);
296 phylink_set(pl->supported, 100baseT_Full);
297 phylink_set(pl->supported, 1000baseT_Half);
298 phylink_set(pl->supported, 1000baseT_Full);
299 break;
300
301 case PHY_INTERFACE_MODE_1000BASEX:
302 phylink_set(pl->supported, 1000baseX_Full);
303 break;
304
305 case PHY_INTERFACE_MODE_2500BASEX:
306 phylink_set(pl->supported, 2500baseX_Full);
307 break;
308
309 case PHY_INTERFACE_MODE_USXGMII:
310 case PHY_INTERFACE_MODE_10GKR:
311 case PHY_INTERFACE_MODE_10GBASER:
312 phylink_set(pl->supported, 10baseT_Half);
313 phylink_set(pl->supported, 10baseT_Full);
314 phylink_set(pl->supported, 100baseT_Half);
315 phylink_set(pl->supported, 100baseT_Full);
316 phylink_set(pl->supported, 1000baseT_Half);
317 phylink_set(pl->supported, 1000baseT_Full);
318 phylink_set(pl->supported, 1000baseX_Full);
319 phylink_set(pl->supported, 1000baseKX_Full);
320 phylink_set(pl->supported, 2500baseT_Full);
321 phylink_set(pl->supported, 2500baseX_Full);
322 phylink_set(pl->supported, 5000baseT_Full);
323 phylink_set(pl->supported, 10000baseT_Full);
324 phylink_set(pl->supported, 10000baseKR_Full);
325 phylink_set(pl->supported, 10000baseKX4_Full);
326 phylink_set(pl->supported, 10000baseCR_Full);
327 phylink_set(pl->supported, 10000baseSR_Full);
328 phylink_set(pl->supported, 10000baseLR_Full);
329 phylink_set(pl->supported, 10000baseLRM_Full);
330 phylink_set(pl->supported, 10000baseER_Full);
331 break;
332
333 case PHY_INTERFACE_MODE_XLGMII:
334 phylink_set(pl->supported, 25000baseCR_Full);
335 phylink_set(pl->supported, 25000baseKR_Full);
336 phylink_set(pl->supported, 25000baseSR_Full);
337 phylink_set(pl->supported, 40000baseKR4_Full);
338 phylink_set(pl->supported, 40000baseCR4_Full);
339 phylink_set(pl->supported, 40000baseSR4_Full);
340 phylink_set(pl->supported, 40000baseLR4_Full);
341 phylink_set(pl->supported, 50000baseCR2_Full);
342 phylink_set(pl->supported, 50000baseKR2_Full);
343 phylink_set(pl->supported, 50000baseSR2_Full);
344 phylink_set(pl->supported, 50000baseKR_Full);
345 phylink_set(pl->supported, 50000baseSR_Full);
346 phylink_set(pl->supported, 50000baseCR_Full);
347 phylink_set(pl->supported, 50000baseLR_ER_FR_Full);
348 phylink_set(pl->supported, 50000baseDR_Full);
349 phylink_set(pl->supported, 100000baseKR4_Full);
350 phylink_set(pl->supported, 100000baseSR4_Full);
351 phylink_set(pl->supported, 100000baseCR4_Full);
352 phylink_set(pl->supported, 100000baseLR4_ER4_Full);
353 phylink_set(pl->supported, 100000baseKR2_Full);
354 phylink_set(pl->supported, 100000baseSR2_Full);
355 phylink_set(pl->supported, 100000baseCR2_Full);
356 phylink_set(pl->supported, 100000baseLR2_ER2_FR2_Full);
357 phylink_set(pl->supported, 100000baseDR2_Full);
358 break;
359
360 default:
361 phylink_err(pl,
362 "incorrect link mode %s for in-band status\n",
363 phy_modes(pl->link_config.interface));
364 return -EINVAL;
365 }
366
367 linkmode_copy(pl->link_config.advertising, pl->supported);
368
369 if (phylink_validate(pl, pl->supported, &pl->link_config)) {
370 phylink_err(pl,
371 "failed to validate link configuration for in-band status\n");
372 return -EINVAL;
373 }
374
375 /* Check if MAC/PCS also supports Autoneg. */
376 pl->link_config.an_enabled = phylink_test(pl->supported, Autoneg);
377 }
378
379 return 0;
380 }
381
phylink_apply_manual_flow(struct phylink * pl,struct phylink_link_state * state)382 static void phylink_apply_manual_flow(struct phylink *pl,
383 struct phylink_link_state *state)
384 {
385 /* If autoneg is disabled, pause AN is also disabled */
386 if (!state->an_enabled)
387 state->pause &= ~MLO_PAUSE_AN;
388
389 /* Manual configuration of pause modes */
390 if (!(pl->link_config.pause & MLO_PAUSE_AN))
391 state->pause = pl->link_config.pause;
392 }
393
phylink_resolve_flow(struct phylink_link_state * state)394 static void phylink_resolve_flow(struct phylink_link_state *state)
395 {
396 bool tx_pause, rx_pause;
397
398 state->pause = MLO_PAUSE_NONE;
399 if (state->duplex == DUPLEX_FULL) {
400 linkmode_resolve_pause(state->advertising,
401 state->lp_advertising,
402 &tx_pause, &rx_pause);
403 if (tx_pause)
404 state->pause |= MLO_PAUSE_TX;
405 if (rx_pause)
406 state->pause |= MLO_PAUSE_RX;
407 }
408 }
409
phylink_mac_config(struct phylink * pl,const struct phylink_link_state * state)410 static void phylink_mac_config(struct phylink *pl,
411 const struct phylink_link_state *state)
412 {
413 phylink_dbg(pl,
414 "%s: mode=%s/%s/%s/%s adv=%*pb pause=%02x link=%u an=%u\n",
415 __func__, phylink_an_mode_str(pl->cur_link_an_mode),
416 phy_modes(state->interface),
417 phy_speed_to_str(state->speed),
418 phy_duplex_to_str(state->duplex),
419 __ETHTOOL_LINK_MODE_MASK_NBITS, state->advertising,
420 state->pause, state->link, state->an_enabled);
421
422 pl->mac_ops->mac_config(pl->config, pl->cur_link_an_mode, state);
423 }
424
phylink_mac_pcs_an_restart(struct phylink * pl)425 static void phylink_mac_pcs_an_restart(struct phylink *pl)
426 {
427 if (pl->link_config.an_enabled &&
428 phy_interface_mode_is_8023z(pl->link_config.interface) &&
429 phylink_autoneg_inband(pl->cur_link_an_mode)) {
430 if (pl->pcs_ops)
431 pl->pcs_ops->pcs_an_restart(pl->pcs);
432 else
433 pl->mac_ops->mac_an_restart(pl->config);
434 }
435 }
436
phylink_major_config(struct phylink * pl,bool restart,const struct phylink_link_state * state)437 static void phylink_major_config(struct phylink *pl, bool restart,
438 const struct phylink_link_state *state)
439 {
440 int err;
441
442 phylink_dbg(pl, "major config %s\n", phy_modes(state->interface));
443
444 if (pl->mac_ops->mac_prepare) {
445 err = pl->mac_ops->mac_prepare(pl->config, pl->cur_link_an_mode,
446 state->interface);
447 if (err < 0) {
448 phylink_err(pl, "mac_prepare failed: %pe\n",
449 ERR_PTR(err));
450 return;
451 }
452 }
453
454 phylink_mac_config(pl, state);
455
456 if (pl->pcs_ops) {
457 err = pl->pcs_ops->pcs_config(pl->pcs, pl->cur_link_an_mode,
458 state->interface,
459 state->advertising,
460 !!(pl->link_config.pause &
461 MLO_PAUSE_AN));
462 if (err < 0)
463 phylink_err(pl, "pcs_config failed: %pe\n",
464 ERR_PTR(err));
465 if (err > 0)
466 restart = true;
467 }
468 if (restart)
469 phylink_mac_pcs_an_restart(pl);
470
471 if (pl->mac_ops->mac_finish) {
472 err = pl->mac_ops->mac_finish(pl->config, pl->cur_link_an_mode,
473 state->interface);
474 if (err < 0)
475 phylink_err(pl, "mac_finish failed: %pe\n",
476 ERR_PTR(err));
477 }
478 }
479
480 /*
481 * Reconfigure for a change of inband advertisement.
482 * If we have a separate PCS, we only need to call its pcs_config() method,
483 * and then restart AN if it indicates something changed. Otherwise, we do
484 * the full MAC reconfiguration.
485 */
phylink_change_inband_advert(struct phylink * pl)486 static int phylink_change_inband_advert(struct phylink *pl)
487 {
488 int ret;
489
490 if (test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state))
491 return 0;
492
493 if (!pl->pcs_ops) {
494 /* Legacy method */
495 phylink_mac_config(pl, &pl->link_config);
496 phylink_mac_pcs_an_restart(pl);
497 return 0;
498 }
499
500 phylink_dbg(pl, "%s: mode=%s/%s adv=%*pb pause=%02x\n", __func__,
501 phylink_an_mode_str(pl->cur_link_an_mode),
502 phy_modes(pl->link_config.interface),
503 __ETHTOOL_LINK_MODE_MASK_NBITS, pl->link_config.advertising,
504 pl->link_config.pause);
505
506 /* Modern PCS-based method; update the advert at the PCS, and
507 * restart negotiation if the pcs_config() helper indicates that
508 * the programmed advertisement has changed.
509 */
510 ret = pl->pcs_ops->pcs_config(pl->pcs, pl->cur_link_an_mode,
511 pl->link_config.interface,
512 pl->link_config.advertising,
513 !!(pl->link_config.pause & MLO_PAUSE_AN));
514 if (ret < 0)
515 return ret;
516
517 if (ret > 0)
518 phylink_mac_pcs_an_restart(pl);
519
520 return 0;
521 }
522
phylink_mac_pcs_get_state(struct phylink * pl,struct phylink_link_state * state)523 static void phylink_mac_pcs_get_state(struct phylink *pl,
524 struct phylink_link_state *state)
525 {
526 linkmode_copy(state->advertising, pl->link_config.advertising);
527 linkmode_zero(state->lp_advertising);
528 state->interface = pl->link_config.interface;
529 state->an_enabled = pl->link_config.an_enabled;
530 state->speed = SPEED_UNKNOWN;
531 state->duplex = DUPLEX_UNKNOWN;
532 state->pause = MLO_PAUSE_NONE;
533 state->an_complete = 0;
534 state->link = 1;
535
536 if (pl->pcs_ops)
537 pl->pcs_ops->pcs_get_state(pl->pcs, state);
538 else if (pl->mac_ops->mac_pcs_get_state)
539 pl->mac_ops->mac_pcs_get_state(pl->config, state);
540 else
541 state->link = 0;
542 }
543
544 /* The fixed state is... fixed except for the link state,
545 * which may be determined by a GPIO or a callback.
546 */
phylink_get_fixed_state(struct phylink * pl,struct phylink_link_state * state)547 static void phylink_get_fixed_state(struct phylink *pl,
548 struct phylink_link_state *state)
549 {
550 *state = pl->link_config;
551 if (pl->config->get_fixed_state)
552 pl->config->get_fixed_state(pl->config, state);
553 else if (pl->link_gpio)
554 state->link = !!gpiod_get_value_cansleep(pl->link_gpio);
555
556 phylink_resolve_flow(state);
557 }
558
phylink_mac_initial_config(struct phylink * pl,bool force_restart)559 static void phylink_mac_initial_config(struct phylink *pl, bool force_restart)
560 {
561 struct phylink_link_state link_state;
562
563 switch (pl->cur_link_an_mode) {
564 case MLO_AN_PHY:
565 link_state = pl->phy_state;
566 break;
567
568 case MLO_AN_FIXED:
569 phylink_get_fixed_state(pl, &link_state);
570 break;
571
572 case MLO_AN_INBAND:
573 link_state = pl->link_config;
574 if (link_state.interface == PHY_INTERFACE_MODE_SGMII)
575 link_state.pause = MLO_PAUSE_NONE;
576 break;
577
578 default: /* can't happen */
579 return;
580 }
581
582 link_state.link = false;
583
584 phylink_apply_manual_flow(pl, &link_state);
585 phylink_major_config(pl, force_restart, &link_state);
586 }
587
phylink_pause_to_str(int pause)588 static const char *phylink_pause_to_str(int pause)
589 {
590 switch (pause & MLO_PAUSE_TXRX_MASK) {
591 case MLO_PAUSE_TX | MLO_PAUSE_RX:
592 return "rx/tx";
593 case MLO_PAUSE_TX:
594 return "tx";
595 case MLO_PAUSE_RX:
596 return "rx";
597 default:
598 return "off";
599 }
600 }
601
phylink_link_up(struct phylink * pl,struct phylink_link_state link_state)602 static void phylink_link_up(struct phylink *pl,
603 struct phylink_link_state link_state)
604 {
605 struct net_device *ndev = pl->netdev;
606
607 pl->cur_interface = link_state.interface;
608
609 if (pl->pcs_ops && pl->pcs_ops->pcs_link_up)
610 pl->pcs_ops->pcs_link_up(pl->pcs, pl->cur_link_an_mode,
611 pl->cur_interface,
612 link_state.speed, link_state.duplex);
613
614 pl->mac_ops->mac_link_up(pl->config, pl->phydev,
615 pl->cur_link_an_mode, pl->cur_interface,
616 link_state.speed, link_state.duplex,
617 !!(link_state.pause & MLO_PAUSE_TX),
618 !!(link_state.pause & MLO_PAUSE_RX));
619
620 if (ndev)
621 netif_carrier_on(ndev);
622
623 phylink_info(pl,
624 "Link is Up - %s/%s - flow control %s\n",
625 phy_speed_to_str(link_state.speed),
626 phy_duplex_to_str(link_state.duplex),
627 phylink_pause_to_str(link_state.pause));
628 }
629
phylink_link_down(struct phylink * pl)630 static void phylink_link_down(struct phylink *pl)
631 {
632 struct net_device *ndev = pl->netdev;
633
634 if (ndev)
635 netif_carrier_off(ndev);
636 pl->mac_ops->mac_link_down(pl->config, pl->cur_link_an_mode,
637 pl->cur_interface);
638 phylink_info(pl, "Link is Down\n");
639 }
640
phylink_resolve(struct work_struct * w)641 static void phylink_resolve(struct work_struct *w)
642 {
643 struct phylink *pl = container_of(w, struct phylink, resolve);
644 struct phylink_link_state link_state;
645 struct net_device *ndev = pl->netdev;
646 bool mac_config = false;
647 bool retrigger = false;
648 bool cur_link_state;
649
650 mutex_lock(&pl->state_mutex);
651 if (pl->netdev)
652 cur_link_state = netif_carrier_ok(ndev);
653 else
654 cur_link_state = pl->old_link_state;
655
656 if (pl->phylink_disable_state) {
657 pl->mac_link_dropped = false;
658 link_state.link = false;
659 } else if (pl->mac_link_dropped) {
660 link_state.link = false;
661 retrigger = true;
662 } else {
663 switch (pl->cur_link_an_mode) {
664 case MLO_AN_PHY:
665 link_state = pl->phy_state;
666 phylink_apply_manual_flow(pl, &link_state);
667 mac_config = link_state.link;
668 break;
669
670 case MLO_AN_FIXED:
671 phylink_get_fixed_state(pl, &link_state);
672 mac_config = link_state.link;
673 break;
674
675 case MLO_AN_INBAND:
676 phylink_mac_pcs_get_state(pl, &link_state);
677
678 /* The PCS may have a latching link-fail indicator.
679 * If the link was up, bring the link down and
680 * re-trigger the resolve. Otherwise, re-read the
681 * PCS state to get the current status of the link.
682 */
683 if (!link_state.link) {
684 if (cur_link_state)
685 retrigger = true;
686 else
687 phylink_mac_pcs_get_state(pl,
688 &link_state);
689 }
690
691 /* If we have a phy, the "up" state is the union of
692 * both the PHY and the MAC */
693 if (pl->phydev)
694 link_state.link &= pl->phy_state.link;
695
696 /* Only update if the PHY link is up */
697 if (pl->phydev && pl->phy_state.link) {
698 /* If the interface has changed, force a
699 * link down event if the link isn't already
700 * down, and re-resolve.
701 */
702 if (link_state.interface !=
703 pl->phy_state.interface) {
704 retrigger = true;
705 link_state.link = false;
706 }
707 link_state.interface = pl->phy_state.interface;
708
709 /* If we have a PHY, we need to update with
710 * the PHY flow control bits. */
711 link_state.pause = pl->phy_state.pause;
712 mac_config = true;
713 }
714 phylink_apply_manual_flow(pl, &link_state);
715 break;
716 }
717 }
718
719 if (mac_config) {
720 if (link_state.interface != pl->link_config.interface) {
721 /* The interface has changed, force the link down and
722 * then reconfigure.
723 */
724 if (cur_link_state) {
725 phylink_link_down(pl);
726 cur_link_state = false;
727 }
728 phylink_major_config(pl, false, &link_state);
729 pl->link_config.interface = link_state.interface;
730 } else if (!pl->pcs_ops) {
731 /* The interface remains unchanged, only the speed,
732 * duplex or pause settings have changed. Call the
733 * old mac_config() method to configure the MAC/PCS
734 * only if we do not have a PCS installed (an
735 * unconverted user.)
736 */
737 phylink_mac_config(pl, &link_state);
738 }
739 }
740
741 if (link_state.link != cur_link_state) {
742 pl->old_link_state = link_state.link;
743 if (!link_state.link)
744 phylink_link_down(pl);
745 else
746 phylink_link_up(pl, link_state);
747 }
748 if (!link_state.link && retrigger) {
749 pl->mac_link_dropped = false;
750 queue_work(system_power_efficient_wq, &pl->resolve);
751 }
752 mutex_unlock(&pl->state_mutex);
753 }
754
phylink_run_resolve(struct phylink * pl)755 static void phylink_run_resolve(struct phylink *pl)
756 {
757 if (!pl->phylink_disable_state)
758 queue_work(system_power_efficient_wq, &pl->resolve);
759 }
760
phylink_run_resolve_and_disable(struct phylink * pl,int bit)761 static void phylink_run_resolve_and_disable(struct phylink *pl, int bit)
762 {
763 unsigned long state = pl->phylink_disable_state;
764
765 set_bit(bit, &pl->phylink_disable_state);
766 if (state == 0) {
767 queue_work(system_power_efficient_wq, &pl->resolve);
768 flush_work(&pl->resolve);
769 }
770 }
771
phylink_fixed_poll(struct timer_list * t)772 static void phylink_fixed_poll(struct timer_list *t)
773 {
774 struct phylink *pl = container_of(t, struct phylink, link_poll);
775
776 mod_timer(t, jiffies + HZ);
777
778 phylink_run_resolve(pl);
779 }
780
781 static const struct sfp_upstream_ops sfp_phylink_ops;
782
phylink_register_sfp(struct phylink * pl,struct fwnode_handle * fwnode)783 static int phylink_register_sfp(struct phylink *pl,
784 struct fwnode_handle *fwnode)
785 {
786 struct sfp_bus *bus;
787 int ret;
788
789 if (!fwnode)
790 return 0;
791
792 bus = sfp_bus_find_fwnode(fwnode);
793 if (IS_ERR(bus)) {
794 ret = PTR_ERR(bus);
795 phylink_err(pl, "unable to attach SFP bus: %d\n", ret);
796 return ret;
797 }
798
799 pl->sfp_bus = bus;
800
801 ret = sfp_bus_add_upstream(bus, pl, &sfp_phylink_ops);
802 sfp_bus_put(bus);
803
804 return ret;
805 }
806
807 /**
808 * phylink_create() - create a phylink instance
809 * @config: a pointer to the target &struct phylink_config
810 * @fwnode: a pointer to a &struct fwnode_handle describing the network
811 * interface
812 * @iface: the desired link mode defined by &typedef phy_interface_t
813 * @mac_ops: a pointer to a &struct phylink_mac_ops for the MAC.
814 *
815 * Create a new phylink instance, and parse the link parameters found in @np.
816 * This will parse in-band modes, fixed-link or SFP configuration.
817 *
818 * Note: the rtnl lock must not be held when calling this function.
819 *
820 * Returns a pointer to a &struct phylink, or an error-pointer value. Users
821 * must use IS_ERR() to check for errors from this function.
822 */
phylink_create(struct phylink_config * config,struct fwnode_handle * fwnode,phy_interface_t iface,const struct phylink_mac_ops * mac_ops)823 struct phylink *phylink_create(struct phylink_config *config,
824 struct fwnode_handle *fwnode,
825 phy_interface_t iface,
826 const struct phylink_mac_ops *mac_ops)
827 {
828 struct phylink *pl;
829 int ret;
830
831 pl = kzalloc(sizeof(*pl), GFP_KERNEL);
832 if (!pl)
833 return ERR_PTR(-ENOMEM);
834
835 mutex_init(&pl->state_mutex);
836 INIT_WORK(&pl->resolve, phylink_resolve);
837
838 pl->config = config;
839 if (config->type == PHYLINK_NETDEV) {
840 pl->netdev = to_net_dev(config->dev);
841 netif_carrier_off(pl->netdev);
842 } else if (config->type == PHYLINK_DEV) {
843 pl->dev = config->dev;
844 } else {
845 kfree(pl);
846 return ERR_PTR(-EINVAL);
847 }
848
849 pl->phy_state.interface = iface;
850 pl->link_interface = iface;
851 if (iface == PHY_INTERFACE_MODE_MOCA)
852 pl->link_port = PORT_BNC;
853 else
854 pl->link_port = PORT_MII;
855 pl->link_config.interface = iface;
856 pl->link_config.pause = MLO_PAUSE_AN;
857 pl->link_config.speed = SPEED_UNKNOWN;
858 pl->link_config.duplex = DUPLEX_UNKNOWN;
859 pl->link_config.an_enabled = true;
860 pl->mac_ops = mac_ops;
861 __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
862 timer_setup(&pl->link_poll, phylink_fixed_poll, 0);
863
864 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
865 linkmode_copy(pl->link_config.advertising, pl->supported);
866 phylink_validate(pl, pl->supported, &pl->link_config);
867
868 ret = phylink_parse_mode(pl, fwnode);
869 if (ret < 0) {
870 kfree(pl);
871 return ERR_PTR(ret);
872 }
873
874 if (pl->cfg_link_an_mode == MLO_AN_FIXED) {
875 ret = phylink_parse_fixedlink(pl, fwnode);
876 if (ret < 0) {
877 kfree(pl);
878 return ERR_PTR(ret);
879 }
880 }
881
882 pl->cur_link_an_mode = pl->cfg_link_an_mode;
883
884 ret = phylink_register_sfp(pl, fwnode);
885 if (ret < 0) {
886 kfree(pl);
887 return ERR_PTR(ret);
888 }
889
890 return pl;
891 }
892 EXPORT_SYMBOL_GPL(phylink_create);
893
894 /**
895 * phylink_set_pcs() - set the current PCS for phylink to use
896 * @pl: a pointer to a &struct phylink returned from phylink_create()
897 * @pcs: a pointer to the &struct phylink_pcs
898 *
899 * Bind the MAC PCS to phylink. This may be called after phylink_create(),
900 * in mac_prepare() or mac_config() methods if it is desired to dynamically
901 * change the PCS.
902 *
903 * Please note that there are behavioural changes with the mac_config()
904 * callback if a PCS is present (denoting a newer setup) so removing a PCS
905 * is not supported, and if a PCS is going to be used, it must be registered
906 * by calling phylink_set_pcs() at the latest in the first mac_config() call.
907 */
phylink_set_pcs(struct phylink * pl,struct phylink_pcs * pcs)908 void phylink_set_pcs(struct phylink *pl, struct phylink_pcs *pcs)
909 {
910 pl->pcs = pcs;
911 pl->pcs_ops = pcs->ops;
912 }
913 EXPORT_SYMBOL_GPL(phylink_set_pcs);
914
915 /**
916 * phylink_destroy() - cleanup and destroy the phylink instance
917 * @pl: a pointer to a &struct phylink returned from phylink_create()
918 *
919 * Destroy a phylink instance. Any PHY that has been attached must have been
920 * cleaned up via phylink_disconnect_phy() prior to calling this function.
921 *
922 * Note: the rtnl lock must not be held when calling this function.
923 */
phylink_destroy(struct phylink * pl)924 void phylink_destroy(struct phylink *pl)
925 {
926 sfp_bus_del_upstream(pl->sfp_bus);
927 if (pl->link_gpio)
928 gpiod_put(pl->link_gpio);
929
930 cancel_work_sync(&pl->resolve);
931 kfree(pl);
932 }
933 EXPORT_SYMBOL_GPL(phylink_destroy);
934
phylink_phy_change(struct phy_device * phydev,bool up)935 static void phylink_phy_change(struct phy_device *phydev, bool up)
936 {
937 struct phylink *pl = phydev->phylink;
938 bool tx_pause, rx_pause;
939
940 phy_get_pause(phydev, &tx_pause, &rx_pause);
941
942 mutex_lock(&pl->state_mutex);
943 pl->phy_state.speed = phydev->speed;
944 pl->phy_state.duplex = phydev->duplex;
945 pl->phy_state.pause = MLO_PAUSE_NONE;
946 if (tx_pause)
947 pl->phy_state.pause |= MLO_PAUSE_TX;
948 if (rx_pause)
949 pl->phy_state.pause |= MLO_PAUSE_RX;
950 pl->phy_state.interface = phydev->interface;
951 pl->phy_state.link = up;
952 mutex_unlock(&pl->state_mutex);
953
954 phylink_run_resolve(pl);
955
956 phylink_dbg(pl, "phy link %s %s/%s/%s\n", up ? "up" : "down",
957 phy_modes(phydev->interface),
958 phy_speed_to_str(phydev->speed),
959 phy_duplex_to_str(phydev->duplex));
960 }
961
phylink_bringup_phy(struct phylink * pl,struct phy_device * phy,phy_interface_t interface)962 static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy,
963 phy_interface_t interface)
964 {
965 struct phylink_link_state config;
966 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
967 char *irq_str;
968 int ret;
969
970 /*
971 * This is the new way of dealing with flow control for PHYs,
972 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
973 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
974 * using our validate call to the MAC, we rely upon the MAC
975 * clearing the bits from both supported and advertising fields.
976 */
977 phy_support_asym_pause(phy);
978
979 memset(&config, 0, sizeof(config));
980 linkmode_copy(supported, phy->supported);
981 linkmode_copy(config.advertising, phy->advertising);
982
983 /* Clause 45 PHYs switch their Serdes lane between several different
984 * modes, normally 10GBASE-R, SGMII. Some use 2500BASE-X for 2.5G
985 * speeds. We really need to know which interface modes the PHY and
986 * MAC supports to properly work out which linkmodes can be supported.
987 */
988 if (phy->is_c45 &&
989 interface != PHY_INTERFACE_MODE_RXAUI &&
990 interface != PHY_INTERFACE_MODE_XAUI &&
991 interface != PHY_INTERFACE_MODE_USXGMII)
992 config.interface = PHY_INTERFACE_MODE_NA;
993 else
994 config.interface = interface;
995
996 ret = phylink_validate(pl, supported, &config);
997 if (ret) {
998 phylink_warn(pl, "validation of %s with support %*pb and advertisement %*pb failed: %d\n",
999 phy_modes(config.interface),
1000 __ETHTOOL_LINK_MODE_MASK_NBITS, phy->supported,
1001 __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising,
1002 ret);
1003 return ret;
1004 }
1005
1006 phy->phylink = pl;
1007 phy->phy_link_change = phylink_phy_change;
1008
1009 irq_str = phy_attached_info_irq(phy);
1010 phylink_info(pl,
1011 "PHY [%s] driver [%s] (irq=%s)\n",
1012 dev_name(&phy->mdio.dev), phy->drv->name, irq_str);
1013 kfree(irq_str);
1014
1015 mutex_lock(&phy->lock);
1016 mutex_lock(&pl->state_mutex);
1017 pl->phydev = phy;
1018 pl->phy_state.interface = interface;
1019 pl->phy_state.pause = MLO_PAUSE_NONE;
1020 pl->phy_state.speed = SPEED_UNKNOWN;
1021 pl->phy_state.duplex = DUPLEX_UNKNOWN;
1022 linkmode_copy(pl->supported, supported);
1023 linkmode_copy(pl->link_config.advertising, config.advertising);
1024
1025 /* Restrict the phy advertisement according to the MAC support. */
1026 linkmode_copy(phy->advertising, config.advertising);
1027 mutex_unlock(&pl->state_mutex);
1028 mutex_unlock(&phy->lock);
1029
1030 phylink_dbg(pl,
1031 "phy: setting supported %*pb advertising %*pb\n",
1032 __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
1033 __ETHTOOL_LINK_MODE_MASK_NBITS, phy->advertising);
1034
1035 if (phy_interrupt_is_valid(phy))
1036 phy_request_interrupt(phy);
1037
1038 return 0;
1039 }
1040
phylink_attach_phy(struct phylink * pl,struct phy_device * phy,phy_interface_t interface)1041 static int phylink_attach_phy(struct phylink *pl, struct phy_device *phy,
1042 phy_interface_t interface)
1043 {
1044 if (WARN_ON(pl->cfg_link_an_mode == MLO_AN_FIXED ||
1045 (pl->cfg_link_an_mode == MLO_AN_INBAND &&
1046 phy_interface_mode_is_8023z(interface))))
1047 return -EINVAL;
1048
1049 if (pl->phydev)
1050 return -EBUSY;
1051
1052 return phy_attach_direct(pl->netdev, phy, 0, interface);
1053 }
1054
1055 /**
1056 * phylink_connect_phy() - connect a PHY to the phylink instance
1057 * @pl: a pointer to a &struct phylink returned from phylink_create()
1058 * @phy: a pointer to a &struct phy_device.
1059 *
1060 * Connect @phy to the phylink instance specified by @pl by calling
1061 * phy_attach_direct(). Configure the @phy according to the MAC driver's
1062 * capabilities, start the PHYLIB state machine and enable any interrupts
1063 * that the PHY supports.
1064 *
1065 * This updates the phylink's ethtool supported and advertising link mode
1066 * masks.
1067 *
1068 * Returns 0 on success or a negative errno.
1069 */
phylink_connect_phy(struct phylink * pl,struct phy_device * phy)1070 int phylink_connect_phy(struct phylink *pl, struct phy_device *phy)
1071 {
1072 int ret;
1073
1074 /* Use PHY device/driver interface */
1075 if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
1076 pl->link_interface = phy->interface;
1077 pl->link_config.interface = pl->link_interface;
1078 }
1079
1080 ret = phylink_attach_phy(pl, phy, pl->link_interface);
1081 if (ret < 0)
1082 return ret;
1083
1084 ret = phylink_bringup_phy(pl, phy, pl->link_config.interface);
1085 if (ret)
1086 phy_detach(phy);
1087
1088 return ret;
1089 }
1090 EXPORT_SYMBOL_GPL(phylink_connect_phy);
1091
1092 /**
1093 * phylink_of_phy_connect() - connect the PHY specified in the DT mode.
1094 * @pl: a pointer to a &struct phylink returned from phylink_create()
1095 * @dn: a pointer to a &struct device_node.
1096 * @flags: PHY-specific flags to communicate to the PHY device driver
1097 *
1098 * Connect the phy specified in the device node @dn to the phylink instance
1099 * specified by @pl. Actions specified in phylink_connect_phy() will be
1100 * performed.
1101 *
1102 * Returns 0 on success or a negative errno.
1103 */
phylink_of_phy_connect(struct phylink * pl,struct device_node * dn,u32 flags)1104 int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn,
1105 u32 flags)
1106 {
1107 struct device_node *phy_node;
1108 struct phy_device *phy_dev;
1109 int ret;
1110
1111 /* Fixed links and 802.3z are handled without needing a PHY */
1112 if (pl->cfg_link_an_mode == MLO_AN_FIXED ||
1113 (pl->cfg_link_an_mode == MLO_AN_INBAND &&
1114 phy_interface_mode_is_8023z(pl->link_interface)))
1115 return 0;
1116
1117 phy_node = of_parse_phandle(dn, "phy-handle", 0);
1118 if (!phy_node)
1119 phy_node = of_parse_phandle(dn, "phy", 0);
1120 if (!phy_node)
1121 phy_node = of_parse_phandle(dn, "phy-device", 0);
1122
1123 if (!phy_node) {
1124 if (pl->cfg_link_an_mode == MLO_AN_PHY)
1125 return -ENODEV;
1126 return 0;
1127 }
1128
1129 phy_dev = of_phy_find_device(phy_node);
1130 /* We're done with the phy_node handle */
1131 of_node_put(phy_node);
1132 if (!phy_dev)
1133 return -ENODEV;
1134
1135 ret = phy_attach_direct(pl->netdev, phy_dev, flags,
1136 pl->link_interface);
1137 if (ret)
1138 return ret;
1139
1140 ret = phylink_bringup_phy(pl, phy_dev, pl->link_config.interface);
1141 if (ret)
1142 phy_detach(phy_dev);
1143
1144 return ret;
1145 }
1146 EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
1147
1148 /**
1149 * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
1150 * instance.
1151 * @pl: a pointer to a &struct phylink returned from phylink_create()
1152 *
1153 * Disconnect any current PHY from the phylink instance described by @pl.
1154 */
phylink_disconnect_phy(struct phylink * pl)1155 void phylink_disconnect_phy(struct phylink *pl)
1156 {
1157 struct phy_device *phy;
1158
1159 ASSERT_RTNL();
1160
1161 phy = pl->phydev;
1162 if (phy) {
1163 mutex_lock(&phy->lock);
1164 mutex_lock(&pl->state_mutex);
1165 pl->phydev = NULL;
1166 mutex_unlock(&pl->state_mutex);
1167 mutex_unlock(&phy->lock);
1168 flush_work(&pl->resolve);
1169
1170 phy_disconnect(phy);
1171 }
1172 }
1173 EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
1174
1175 /**
1176 * phylink_mac_change() - notify phylink of a change in MAC state
1177 * @pl: a pointer to a &struct phylink returned from phylink_create()
1178 * @up: indicates whether the link is currently up.
1179 *
1180 * The MAC driver should call this driver when the state of its link
1181 * changes (eg, link failure, new negotiation results, etc.)
1182 */
phylink_mac_change(struct phylink * pl,bool up)1183 void phylink_mac_change(struct phylink *pl, bool up)
1184 {
1185 if (!up)
1186 pl->mac_link_dropped = true;
1187 phylink_run_resolve(pl);
1188 phylink_dbg(pl, "mac link %s\n", up ? "up" : "down");
1189 }
1190 EXPORT_SYMBOL_GPL(phylink_mac_change);
1191
phylink_link_handler(int irq,void * data)1192 static irqreturn_t phylink_link_handler(int irq, void *data)
1193 {
1194 struct phylink *pl = data;
1195
1196 phylink_run_resolve(pl);
1197
1198 return IRQ_HANDLED;
1199 }
1200
1201 /**
1202 * phylink_start() - start a phylink instance
1203 * @pl: a pointer to a &struct phylink returned from phylink_create()
1204 *
1205 * Start the phylink instance specified by @pl, configuring the MAC for the
1206 * desired link mode(s) and negotiation style. This should be called from the
1207 * network device driver's &struct net_device_ops ndo_open() method.
1208 */
phylink_start(struct phylink * pl)1209 void phylink_start(struct phylink *pl)
1210 {
1211 bool poll = false;
1212
1213 ASSERT_RTNL();
1214
1215 phylink_info(pl, "configuring for %s/%s link mode\n",
1216 phylink_an_mode_str(pl->cur_link_an_mode),
1217 phy_modes(pl->link_config.interface));
1218
1219 /* Always set the carrier off */
1220 if (pl->netdev)
1221 netif_carrier_off(pl->netdev);
1222
1223 /* Apply the link configuration to the MAC when starting. This allows
1224 * a fixed-link to start with the correct parameters, and also
1225 * ensures that we set the appropriate advertisement for Serdes links.
1226 *
1227 * Restart autonegotiation if using 802.3z to ensure that the link
1228 * parameters are properly negotiated. This is necessary for DSA
1229 * switches using 802.3z negotiation to ensure they see our modes.
1230 */
1231 phylink_mac_initial_config(pl, true);
1232
1233 clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
1234 phylink_run_resolve(pl);
1235
1236 if (pl->cfg_link_an_mode == MLO_AN_FIXED && pl->link_gpio) {
1237 int irq = gpiod_to_irq(pl->link_gpio);
1238
1239 if (irq > 0) {
1240 if (!request_irq(irq, phylink_link_handler,
1241 IRQF_TRIGGER_RISING |
1242 IRQF_TRIGGER_FALLING,
1243 "netdev link", pl))
1244 pl->link_irq = irq;
1245 else
1246 irq = 0;
1247 }
1248 if (irq <= 0)
1249 poll = true;
1250 }
1251
1252 switch (pl->cfg_link_an_mode) {
1253 case MLO_AN_FIXED:
1254 poll |= pl->config->poll_fixed_state;
1255 break;
1256 case MLO_AN_INBAND:
1257 poll |= pl->config->pcs_poll;
1258 if (pl->pcs)
1259 poll |= pl->pcs->poll;
1260 break;
1261 }
1262 if (poll)
1263 mod_timer(&pl->link_poll, jiffies + HZ);
1264 if (pl->phydev)
1265 phy_start(pl->phydev);
1266 if (pl->sfp_bus)
1267 sfp_upstream_start(pl->sfp_bus);
1268 }
1269 EXPORT_SYMBOL_GPL(phylink_start);
1270
1271 /**
1272 * phylink_stop() - stop a phylink instance
1273 * @pl: a pointer to a &struct phylink returned from phylink_create()
1274 *
1275 * Stop the phylink instance specified by @pl. This should be called from the
1276 * network device driver's &struct net_device_ops ndo_stop() method. The
1277 * network device's carrier state should not be changed prior to calling this
1278 * function.
1279 */
phylink_stop(struct phylink * pl)1280 void phylink_stop(struct phylink *pl)
1281 {
1282 ASSERT_RTNL();
1283
1284 if (pl->sfp_bus)
1285 sfp_upstream_stop(pl->sfp_bus);
1286 if (pl->phydev)
1287 phy_stop(pl->phydev);
1288 del_timer_sync(&pl->link_poll);
1289 if (pl->link_irq) {
1290 free_irq(pl->link_irq, pl);
1291 pl->link_irq = 0;
1292 }
1293
1294 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_STOPPED);
1295 }
1296 EXPORT_SYMBOL_GPL(phylink_stop);
1297
1298 /**
1299 * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY
1300 * @pl: a pointer to a &struct phylink returned from phylink_create()
1301 * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters
1302 *
1303 * Read the wake on lan parameters from the PHY attached to the phylink
1304 * instance specified by @pl. If no PHY is currently attached, report no
1305 * support for wake on lan.
1306 */
phylink_ethtool_get_wol(struct phylink * pl,struct ethtool_wolinfo * wol)1307 void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
1308 {
1309 ASSERT_RTNL();
1310
1311 wol->supported = 0;
1312 wol->wolopts = 0;
1313
1314 if (pl->phydev)
1315 phy_ethtool_get_wol(pl->phydev, wol);
1316 }
1317 EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol);
1318
1319 /**
1320 * phylink_ethtool_set_wol() - set wake on lan parameters
1321 * @pl: a pointer to a &struct phylink returned from phylink_create()
1322 * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters
1323 *
1324 * Set the wake on lan parameters for the PHY attached to the phylink
1325 * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP
1326 * error.
1327 *
1328 * Returns zero on success or negative errno code.
1329 */
phylink_ethtool_set_wol(struct phylink * pl,struct ethtool_wolinfo * wol)1330 int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
1331 {
1332 int ret = -EOPNOTSUPP;
1333
1334 ASSERT_RTNL();
1335
1336 if (pl->phydev)
1337 ret = phy_ethtool_set_wol(pl->phydev, wol);
1338
1339 return ret;
1340 }
1341 EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol);
1342
phylink_merge_link_mode(unsigned long * dst,const unsigned long * b)1343 static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b)
1344 {
1345 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
1346
1347 linkmode_zero(mask);
1348 phylink_set_port_modes(mask);
1349
1350 linkmode_and(dst, dst, mask);
1351 linkmode_or(dst, dst, b);
1352 }
1353
phylink_get_ksettings(const struct phylink_link_state * state,struct ethtool_link_ksettings * kset)1354 static void phylink_get_ksettings(const struct phylink_link_state *state,
1355 struct ethtool_link_ksettings *kset)
1356 {
1357 phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
1358 linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
1359 kset->base.speed = state->speed;
1360 kset->base.duplex = state->duplex;
1361 kset->base.autoneg = state->an_enabled ? AUTONEG_ENABLE :
1362 AUTONEG_DISABLE;
1363 }
1364
1365 /**
1366 * phylink_ethtool_ksettings_get() - get the current link settings
1367 * @pl: a pointer to a &struct phylink returned from phylink_create()
1368 * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings
1369 *
1370 * Read the current link settings for the phylink instance specified by @pl.
1371 * This will be the link settings read from the MAC, PHY or fixed link
1372 * settings depending on the current negotiation mode.
1373 */
phylink_ethtool_ksettings_get(struct phylink * pl,struct ethtool_link_ksettings * kset)1374 int phylink_ethtool_ksettings_get(struct phylink *pl,
1375 struct ethtool_link_ksettings *kset)
1376 {
1377 struct phylink_link_state link_state;
1378
1379 ASSERT_RTNL();
1380
1381 if (pl->phydev) {
1382 phy_ethtool_ksettings_get(pl->phydev, kset);
1383 } else {
1384 kset->base.port = pl->link_port;
1385 }
1386
1387 linkmode_copy(kset->link_modes.supported, pl->supported);
1388
1389 switch (pl->cur_link_an_mode) {
1390 case MLO_AN_FIXED:
1391 /* We are using fixed settings. Report these as the
1392 * current link settings - and note that these also
1393 * represent the supported speeds/duplex/pause modes.
1394 */
1395 phylink_get_fixed_state(pl, &link_state);
1396 phylink_get_ksettings(&link_state, kset);
1397 break;
1398
1399 case MLO_AN_INBAND:
1400 /* If there is a phy attached, then use the reported
1401 * settings from the phy with no modification.
1402 */
1403 if (pl->phydev)
1404 break;
1405
1406 phylink_mac_pcs_get_state(pl, &link_state);
1407
1408 /* The MAC is reporting the link results from its own PCS
1409 * layer via in-band status. Report these as the current
1410 * link settings.
1411 */
1412 phylink_get_ksettings(&link_state, kset);
1413 break;
1414 }
1415
1416 return 0;
1417 }
1418 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get);
1419
1420 /**
1421 * phylink_ethtool_ksettings_set() - set the link settings
1422 * @pl: a pointer to a &struct phylink returned from phylink_create()
1423 * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes
1424 */
phylink_ethtool_ksettings_set(struct phylink * pl,const struct ethtool_link_ksettings * kset)1425 int phylink_ethtool_ksettings_set(struct phylink *pl,
1426 const struct ethtool_link_ksettings *kset)
1427 {
1428 __ETHTOOL_DECLARE_LINK_MODE_MASK(support);
1429 struct phylink_link_state config;
1430 const struct phy_setting *s;
1431
1432 ASSERT_RTNL();
1433
1434 if (pl->phydev) {
1435 /* We can rely on phylib for this update; we also do not need
1436 * to update the pl->link_config settings:
1437 * - the configuration returned via ksettings_get() will come
1438 * from phylib whenever a PHY is present.
1439 * - link_config.interface will be updated by the PHY calling
1440 * back via phylink_phy_change() and a subsequent resolve.
1441 * - initial link configuration for PHY mode comes from the
1442 * last phy state updated via phylink_phy_change().
1443 * - other configuration changes (e.g. pause modes) are
1444 * performed directly via phylib.
1445 * - if in in-band mode with a PHY, the link configuration
1446 * is passed on the link from the PHY, and all of
1447 * link_config.{speed,duplex,an_enabled,pause} are not used.
1448 * - the only possible use would be link_config.advertising
1449 * pause modes when in 1000base-X mode with a PHY, but in
1450 * the presence of a PHY, this should not be changed as that
1451 * should be determined from the media side advertisement.
1452 */
1453 return phy_ethtool_ksettings_set(pl->phydev, kset);
1454 }
1455
1456 linkmode_copy(support, pl->supported);
1457 config = pl->link_config;
1458 config.an_enabled = kset->base.autoneg == AUTONEG_ENABLE;
1459
1460 /* Mask out unsupported advertisements, and force the autoneg bit */
1461 linkmode_and(config.advertising, kset->link_modes.advertising,
1462 support);
1463 linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising,
1464 config.an_enabled);
1465
1466 /* FIXME: should we reject autoneg if phy/mac does not support it? */
1467 switch (kset->base.autoneg) {
1468 case AUTONEG_DISABLE:
1469 /* Autonegotiation disabled, select a suitable speed and
1470 * duplex.
1471 */
1472 s = phy_lookup_setting(kset->base.speed, kset->base.duplex,
1473 support, false);
1474 if (!s)
1475 return -EINVAL;
1476
1477 /* If we have a fixed link, refuse to change link parameters.
1478 * If the link parameters match, accept them but do nothing.
1479 */
1480 if (pl->cur_link_an_mode == MLO_AN_FIXED) {
1481 if (s->speed != pl->link_config.speed ||
1482 s->duplex != pl->link_config.duplex)
1483 return -EINVAL;
1484 return 0;
1485 }
1486
1487 config.speed = s->speed;
1488 config.duplex = s->duplex;
1489 break;
1490
1491 case AUTONEG_ENABLE:
1492 /* If we have a fixed link, allow autonegotiation (since that
1493 * is our default case) but do not allow the advertisement to
1494 * be changed. If the advertisement matches, simply return.
1495 */
1496 if (pl->cur_link_an_mode == MLO_AN_FIXED) {
1497 if (!linkmode_equal(config.advertising,
1498 pl->link_config.advertising))
1499 return -EINVAL;
1500 return 0;
1501 }
1502
1503 config.speed = SPEED_UNKNOWN;
1504 config.duplex = DUPLEX_UNKNOWN;
1505 break;
1506
1507 default:
1508 return -EINVAL;
1509 }
1510
1511 /* We have ruled out the case with a PHY attached, and the
1512 * fixed-link cases. All that is left are in-band links.
1513 */
1514 if (phylink_validate(pl, support, &config))
1515 return -EINVAL;
1516
1517 /* If autonegotiation is enabled, we must have an advertisement */
1518 if (config.an_enabled && phylink_is_empty_linkmode(config.advertising))
1519 return -EINVAL;
1520
1521 /* If this link is with an SFP, ensure that changes to advertised modes
1522 * also cause the associated interface to be selected such that the
1523 * link can be configured correctly.
1524 */
1525 if (pl->sfp_port && pl->sfp_bus) {
1526 config.interface = sfp_select_interface(pl->sfp_bus,
1527 config.advertising);
1528 if (config.interface == PHY_INTERFACE_MODE_NA) {
1529 phylink_err(pl,
1530 "selection of interface failed, advertisement %*pb\n",
1531 __ETHTOOL_LINK_MODE_MASK_NBITS,
1532 config.advertising);
1533 return -EINVAL;
1534 }
1535
1536 /* Revalidate with the selected interface */
1537 linkmode_copy(support, pl->supported);
1538 if (phylink_validate(pl, support, &config)) {
1539 phylink_err(pl, "validation of %s/%s with support %*pb failed\n",
1540 phylink_an_mode_str(pl->cur_link_an_mode),
1541 phy_modes(config.interface),
1542 __ETHTOOL_LINK_MODE_MASK_NBITS, support);
1543 return -EINVAL;
1544 }
1545 }
1546
1547 mutex_lock(&pl->state_mutex);
1548 pl->link_config.speed = config.speed;
1549 pl->link_config.duplex = config.duplex;
1550 pl->link_config.an_enabled = config.an_enabled;
1551
1552 if (pl->link_config.interface != config.interface) {
1553 /* The interface changed, e.g. 1000base-X <-> 2500base-X */
1554 /* We need to force the link down, then change the interface */
1555 if (pl->old_link_state) {
1556 phylink_link_down(pl);
1557 pl->old_link_state = false;
1558 }
1559 if (!test_bit(PHYLINK_DISABLE_STOPPED,
1560 &pl->phylink_disable_state))
1561 phylink_major_config(pl, false, &config);
1562 pl->link_config.interface = config.interface;
1563 linkmode_copy(pl->link_config.advertising, config.advertising);
1564 } else if (!linkmode_equal(pl->link_config.advertising,
1565 config.advertising)) {
1566 linkmode_copy(pl->link_config.advertising, config.advertising);
1567 phylink_change_inband_advert(pl);
1568 }
1569 mutex_unlock(&pl->state_mutex);
1570
1571 return 0;
1572 }
1573 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
1574
1575 /**
1576 * phylink_ethtool_nway_reset() - restart negotiation
1577 * @pl: a pointer to a &struct phylink returned from phylink_create()
1578 *
1579 * Restart negotiation for the phylink instance specified by @pl. This will
1580 * cause any attached phy to restart negotiation with the link partner, and
1581 * if the MAC is in a BaseX mode, the MAC will also be requested to restart
1582 * negotiation.
1583 *
1584 * Returns zero on success, or negative error code.
1585 */
phylink_ethtool_nway_reset(struct phylink * pl)1586 int phylink_ethtool_nway_reset(struct phylink *pl)
1587 {
1588 int ret = 0;
1589
1590 ASSERT_RTNL();
1591
1592 if (pl->phydev)
1593 ret = phy_restart_aneg(pl->phydev);
1594 phylink_mac_pcs_an_restart(pl);
1595
1596 return ret;
1597 }
1598 EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
1599
1600 /**
1601 * phylink_ethtool_get_pauseparam() - get the current pause parameters
1602 * @pl: a pointer to a &struct phylink returned from phylink_create()
1603 * @pause: a pointer to a &struct ethtool_pauseparam
1604 */
phylink_ethtool_get_pauseparam(struct phylink * pl,struct ethtool_pauseparam * pause)1605 void phylink_ethtool_get_pauseparam(struct phylink *pl,
1606 struct ethtool_pauseparam *pause)
1607 {
1608 ASSERT_RTNL();
1609
1610 pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
1611 pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
1612 pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
1613 }
1614 EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
1615
1616 /**
1617 * phylink_ethtool_set_pauseparam() - set the current pause parameters
1618 * @pl: a pointer to a &struct phylink returned from phylink_create()
1619 * @pause: a pointer to a &struct ethtool_pauseparam
1620 */
phylink_ethtool_set_pauseparam(struct phylink * pl,struct ethtool_pauseparam * pause)1621 int phylink_ethtool_set_pauseparam(struct phylink *pl,
1622 struct ethtool_pauseparam *pause)
1623 {
1624 struct phylink_link_state *config = &pl->link_config;
1625 bool manual_changed;
1626 int pause_state;
1627
1628 ASSERT_RTNL();
1629
1630 if (pl->cur_link_an_mode == MLO_AN_FIXED)
1631 return -EOPNOTSUPP;
1632
1633 if (!phylink_test(pl->supported, Pause) &&
1634 !phylink_test(pl->supported, Asym_Pause))
1635 return -EOPNOTSUPP;
1636
1637 if (!phylink_test(pl->supported, Asym_Pause) &&
1638 pause->rx_pause != pause->tx_pause)
1639 return -EINVAL;
1640
1641 pause_state = 0;
1642 if (pause->autoneg)
1643 pause_state |= MLO_PAUSE_AN;
1644 if (pause->rx_pause)
1645 pause_state |= MLO_PAUSE_RX;
1646 if (pause->tx_pause)
1647 pause_state |= MLO_PAUSE_TX;
1648
1649 mutex_lock(&pl->state_mutex);
1650 /*
1651 * See the comments for linkmode_set_pause(), wrt the deficiencies
1652 * with the current implementation. A solution to this issue would
1653 * be:
1654 * ethtool Local device
1655 * rx tx Pause AsymDir
1656 * 0 0 0 0
1657 * 1 0 1 1
1658 * 0 1 0 1
1659 * 1 1 1 1
1660 * and then use the ethtool rx/tx enablement status to mask the
1661 * rx/tx pause resolution.
1662 */
1663 linkmode_set_pause(config->advertising, pause->tx_pause,
1664 pause->rx_pause);
1665
1666 manual_changed = (config->pause ^ pause_state) & MLO_PAUSE_AN ||
1667 (!(pause_state & MLO_PAUSE_AN) &&
1668 (config->pause ^ pause_state) & MLO_PAUSE_TXRX_MASK);
1669
1670 config->pause = pause_state;
1671
1672 /* Update our in-band advertisement, triggering a renegotiation if
1673 * the advertisement changed.
1674 */
1675 if (!pl->phydev)
1676 phylink_change_inband_advert(pl);
1677
1678 mutex_unlock(&pl->state_mutex);
1679
1680 /* If we have a PHY, a change of the pause frame advertisement will
1681 * cause phylib to renegotiate (if AN is enabled) which will in turn
1682 * call our phylink_phy_change() and trigger a resolve. Note that
1683 * we can't hold our state mutex while calling phy_set_asym_pause().
1684 */
1685 if (pl->phydev)
1686 phy_set_asym_pause(pl->phydev, pause->rx_pause,
1687 pause->tx_pause);
1688
1689 /* If the manual pause settings changed, make sure we trigger a
1690 * resolve to update their state; we can not guarantee that the
1691 * link will cycle.
1692 */
1693 if (manual_changed) {
1694 pl->mac_link_dropped = true;
1695 phylink_run_resolve(pl);
1696 }
1697
1698 return 0;
1699 }
1700 EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
1701
1702 /**
1703 * phylink_ethtool_get_eee_err() - read the energy efficient ethernet error
1704 * counter
1705 * @pl: a pointer to a &struct phylink returned from phylink_create().
1706 *
1707 * Read the Energy Efficient Ethernet error counter from the PHY associated
1708 * with the phylink instance specified by @pl.
1709 *
1710 * Returns positive error counter value, or negative error code.
1711 */
phylink_get_eee_err(struct phylink * pl)1712 int phylink_get_eee_err(struct phylink *pl)
1713 {
1714 int ret = 0;
1715
1716 ASSERT_RTNL();
1717
1718 if (pl->phydev)
1719 ret = phy_get_eee_err(pl->phydev);
1720
1721 return ret;
1722 }
1723 EXPORT_SYMBOL_GPL(phylink_get_eee_err);
1724
1725 /**
1726 * phylink_init_eee() - init and check the EEE features
1727 * @pl: a pointer to a &struct phylink returned from phylink_create()
1728 * @clk_stop_enable: allow PHY to stop receive clock
1729 *
1730 * Must be called either with RTNL held or within mac_link_up()
1731 */
phylink_init_eee(struct phylink * pl,bool clk_stop_enable)1732 int phylink_init_eee(struct phylink *pl, bool clk_stop_enable)
1733 {
1734 int ret = -EOPNOTSUPP;
1735
1736 if (pl->phydev)
1737 ret = phy_init_eee(pl->phydev, clk_stop_enable);
1738
1739 return ret;
1740 }
1741 EXPORT_SYMBOL_GPL(phylink_init_eee);
1742
1743 /**
1744 * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters
1745 * @pl: a pointer to a &struct phylink returned from phylink_create()
1746 * @eee: a pointer to a &struct ethtool_eee for the read parameters
1747 */
phylink_ethtool_get_eee(struct phylink * pl,struct ethtool_eee * eee)1748 int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee)
1749 {
1750 int ret = -EOPNOTSUPP;
1751
1752 ASSERT_RTNL();
1753
1754 if (pl->phydev)
1755 ret = phy_ethtool_get_eee(pl->phydev, eee);
1756
1757 return ret;
1758 }
1759 EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
1760
1761 /**
1762 * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters
1763 * @pl: a pointer to a &struct phylink returned from phylink_create()
1764 * @eee: a pointer to a &struct ethtool_eee for the desired parameters
1765 */
phylink_ethtool_set_eee(struct phylink * pl,struct ethtool_eee * eee)1766 int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_eee *eee)
1767 {
1768 int ret = -EOPNOTSUPP;
1769
1770 ASSERT_RTNL();
1771
1772 if (pl->phydev)
1773 ret = phy_ethtool_set_eee(pl->phydev, eee);
1774
1775 return ret;
1776 }
1777 EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
1778
1779 /* This emulates MII registers for a fixed-mode phy operating as per the
1780 * passed in state. "aneg" defines if we report negotiation is possible.
1781 *
1782 * FIXME: should deal with negotiation state too.
1783 */
phylink_mii_emul_read(unsigned int reg,struct phylink_link_state * state)1784 static int phylink_mii_emul_read(unsigned int reg,
1785 struct phylink_link_state *state)
1786 {
1787 struct fixed_phy_status fs;
1788 unsigned long *lpa = state->lp_advertising;
1789 int val;
1790
1791 fs.link = state->link;
1792 fs.speed = state->speed;
1793 fs.duplex = state->duplex;
1794 fs.pause = test_bit(ETHTOOL_LINK_MODE_Pause_BIT, lpa);
1795 fs.asym_pause = test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, lpa);
1796
1797 val = swphy_read_reg(reg, &fs);
1798 if (reg == MII_BMSR) {
1799 if (!state->an_complete)
1800 val &= ~BMSR_ANEGCOMPLETE;
1801 }
1802 return val;
1803 }
1804
phylink_phy_read(struct phylink * pl,unsigned int phy_id,unsigned int reg)1805 static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
1806 unsigned int reg)
1807 {
1808 struct phy_device *phydev = pl->phydev;
1809 int prtad, devad;
1810
1811 if (mdio_phy_id_is_c45(phy_id)) {
1812 prtad = mdio_phy_id_prtad(phy_id);
1813 devad = mdio_phy_id_devad(phy_id);
1814 devad = mdiobus_c45_addr(devad, reg);
1815 } else if (phydev->is_c45) {
1816 switch (reg) {
1817 case MII_BMCR:
1818 case MII_BMSR:
1819 case MII_PHYSID1:
1820 case MII_PHYSID2:
1821 devad = __ffs(phydev->c45_ids.mmds_present);
1822 break;
1823 case MII_ADVERTISE:
1824 case MII_LPA:
1825 if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN))
1826 return -EINVAL;
1827 devad = MDIO_MMD_AN;
1828 if (reg == MII_ADVERTISE)
1829 reg = MDIO_AN_ADVERTISE;
1830 else
1831 reg = MDIO_AN_LPA;
1832 break;
1833 default:
1834 return -EINVAL;
1835 }
1836 prtad = phy_id;
1837 devad = mdiobus_c45_addr(devad, reg);
1838 } else {
1839 prtad = phy_id;
1840 devad = reg;
1841 }
1842 return mdiobus_read(pl->phydev->mdio.bus, prtad, devad);
1843 }
1844
phylink_phy_write(struct phylink * pl,unsigned int phy_id,unsigned int reg,unsigned int val)1845 static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
1846 unsigned int reg, unsigned int val)
1847 {
1848 struct phy_device *phydev = pl->phydev;
1849 int prtad, devad;
1850
1851 if (mdio_phy_id_is_c45(phy_id)) {
1852 prtad = mdio_phy_id_prtad(phy_id);
1853 devad = mdio_phy_id_devad(phy_id);
1854 devad = mdiobus_c45_addr(devad, reg);
1855 } else if (phydev->is_c45) {
1856 switch (reg) {
1857 case MII_BMCR:
1858 case MII_BMSR:
1859 case MII_PHYSID1:
1860 case MII_PHYSID2:
1861 devad = __ffs(phydev->c45_ids.mmds_present);
1862 break;
1863 case MII_ADVERTISE:
1864 case MII_LPA:
1865 if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN))
1866 return -EINVAL;
1867 devad = MDIO_MMD_AN;
1868 if (reg == MII_ADVERTISE)
1869 reg = MDIO_AN_ADVERTISE;
1870 else
1871 reg = MDIO_AN_LPA;
1872 break;
1873 default:
1874 return -EINVAL;
1875 }
1876 prtad = phy_id;
1877 devad = mdiobus_c45_addr(devad, reg);
1878 } else {
1879 prtad = phy_id;
1880 devad = reg;
1881 }
1882
1883 return mdiobus_write(phydev->mdio.bus, prtad, devad, val);
1884 }
1885
phylink_mii_read(struct phylink * pl,unsigned int phy_id,unsigned int reg)1886 static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
1887 unsigned int reg)
1888 {
1889 struct phylink_link_state state;
1890 int val = 0xffff;
1891
1892 switch (pl->cur_link_an_mode) {
1893 case MLO_AN_FIXED:
1894 if (phy_id == 0) {
1895 phylink_get_fixed_state(pl, &state);
1896 val = phylink_mii_emul_read(reg, &state);
1897 }
1898 break;
1899
1900 case MLO_AN_PHY:
1901 return -EOPNOTSUPP;
1902
1903 case MLO_AN_INBAND:
1904 if (phy_id == 0) {
1905 phylink_mac_pcs_get_state(pl, &state);
1906 val = phylink_mii_emul_read(reg, &state);
1907 }
1908 break;
1909 }
1910
1911 return val & 0xffff;
1912 }
1913
phylink_mii_write(struct phylink * pl,unsigned int phy_id,unsigned int reg,unsigned int val)1914 static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
1915 unsigned int reg, unsigned int val)
1916 {
1917 switch (pl->cur_link_an_mode) {
1918 case MLO_AN_FIXED:
1919 break;
1920
1921 case MLO_AN_PHY:
1922 return -EOPNOTSUPP;
1923
1924 case MLO_AN_INBAND:
1925 break;
1926 }
1927
1928 return 0;
1929 }
1930
1931 /**
1932 * phylink_mii_ioctl() - generic mii ioctl interface
1933 * @pl: a pointer to a &struct phylink returned from phylink_create()
1934 * @ifr: a pointer to a &struct ifreq for socket ioctls
1935 * @cmd: ioctl cmd to execute
1936 *
1937 * Perform the specified MII ioctl on the PHY attached to the phylink instance
1938 * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
1939 *
1940 * Returns: zero on success or negative error code.
1941 *
1942 * %SIOCGMIIPHY:
1943 * read register from the current PHY.
1944 * %SIOCGMIIREG:
1945 * read register from the specified PHY.
1946 * %SIOCSMIIREG:
1947 * set a register on the specified PHY.
1948 */
phylink_mii_ioctl(struct phylink * pl,struct ifreq * ifr,int cmd)1949 int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
1950 {
1951 struct mii_ioctl_data *mii = if_mii(ifr);
1952 int ret;
1953
1954 ASSERT_RTNL();
1955
1956 if (pl->phydev) {
1957 /* PHYs only exist for MLO_AN_PHY and SGMII */
1958 switch (cmd) {
1959 case SIOCGMIIPHY:
1960 mii->phy_id = pl->phydev->mdio.addr;
1961 fallthrough;
1962
1963 case SIOCGMIIREG:
1964 ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
1965 if (ret >= 0) {
1966 mii->val_out = ret;
1967 ret = 0;
1968 }
1969 break;
1970
1971 case SIOCSMIIREG:
1972 ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
1973 mii->val_in);
1974 break;
1975
1976 default:
1977 ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
1978 break;
1979 }
1980 } else {
1981 switch (cmd) {
1982 case SIOCGMIIPHY:
1983 mii->phy_id = 0;
1984 fallthrough;
1985
1986 case SIOCGMIIREG:
1987 ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
1988 if (ret >= 0) {
1989 mii->val_out = ret;
1990 ret = 0;
1991 }
1992 break;
1993
1994 case SIOCSMIIREG:
1995 ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
1996 mii->val_in);
1997 break;
1998
1999 default:
2000 ret = -EOPNOTSUPP;
2001 break;
2002 }
2003 }
2004
2005 return ret;
2006 }
2007 EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
2008
2009 /**
2010 * phylink_speed_down() - set the non-SFP PHY to lowest speed supported by both
2011 * link partners
2012 * @pl: a pointer to a &struct phylink returned from phylink_create()
2013 * @sync: perform action synchronously
2014 *
2015 * If we have a PHY that is not part of a SFP module, then set the speed
2016 * as described in the phy_speed_down() function. Please see this function
2017 * for a description of the @sync parameter.
2018 *
2019 * Returns zero if there is no PHY, otherwise as per phy_speed_down().
2020 */
phylink_speed_down(struct phylink * pl,bool sync)2021 int phylink_speed_down(struct phylink *pl, bool sync)
2022 {
2023 int ret = 0;
2024
2025 ASSERT_RTNL();
2026
2027 if (!pl->sfp_bus && pl->phydev)
2028 ret = phy_speed_down(pl->phydev, sync);
2029
2030 return ret;
2031 }
2032 EXPORT_SYMBOL_GPL(phylink_speed_down);
2033
2034 /**
2035 * phylink_speed_up() - restore the advertised speeds prior to the call to
2036 * phylink_speed_down()
2037 * @pl: a pointer to a &struct phylink returned from phylink_create()
2038 *
2039 * If we have a PHY that is not part of a SFP module, then restore the
2040 * PHY speeds as per phy_speed_up().
2041 *
2042 * Returns zero if there is no PHY, otherwise as per phy_speed_up().
2043 */
phylink_speed_up(struct phylink * pl)2044 int phylink_speed_up(struct phylink *pl)
2045 {
2046 int ret = 0;
2047
2048 ASSERT_RTNL();
2049
2050 if (!pl->sfp_bus && pl->phydev)
2051 ret = phy_speed_up(pl->phydev);
2052
2053 return ret;
2054 }
2055 EXPORT_SYMBOL_GPL(phylink_speed_up);
2056
phylink_sfp_attach(void * upstream,struct sfp_bus * bus)2057 static void phylink_sfp_attach(void *upstream, struct sfp_bus *bus)
2058 {
2059 struct phylink *pl = upstream;
2060
2061 pl->netdev->sfp_bus = bus;
2062 }
2063
phylink_sfp_detach(void * upstream,struct sfp_bus * bus)2064 static void phylink_sfp_detach(void *upstream, struct sfp_bus *bus)
2065 {
2066 struct phylink *pl = upstream;
2067
2068 pl->netdev->sfp_bus = NULL;
2069 }
2070
phylink_sfp_config(struct phylink * pl,u8 mode,const unsigned long * supported,const unsigned long * advertising)2071 static int phylink_sfp_config(struct phylink *pl, u8 mode,
2072 const unsigned long *supported,
2073 const unsigned long *advertising)
2074 {
2075 __ETHTOOL_DECLARE_LINK_MODE_MASK(support1);
2076 __ETHTOOL_DECLARE_LINK_MODE_MASK(support);
2077 struct phylink_link_state config;
2078 phy_interface_t iface;
2079 bool changed;
2080 int ret;
2081
2082 linkmode_copy(support, supported);
2083
2084 memset(&config, 0, sizeof(config));
2085 linkmode_copy(config.advertising, advertising);
2086 config.interface = PHY_INTERFACE_MODE_NA;
2087 config.speed = SPEED_UNKNOWN;
2088 config.duplex = DUPLEX_UNKNOWN;
2089 config.pause = MLO_PAUSE_AN;
2090 config.an_enabled = pl->link_config.an_enabled;
2091
2092 /* Ignore errors if we're expecting a PHY to attach later */
2093 ret = phylink_validate(pl, support, &config);
2094 if (ret) {
2095 phylink_err(pl, "validation with support %*pb failed: %d\n",
2096 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
2097 return ret;
2098 }
2099
2100 iface = sfp_select_interface(pl->sfp_bus, config.advertising);
2101 if (iface == PHY_INTERFACE_MODE_NA) {
2102 phylink_err(pl,
2103 "selection of interface failed, advertisement %*pb\n",
2104 __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising);
2105 return -EINVAL;
2106 }
2107
2108 config.interface = iface;
2109 linkmode_copy(support1, support);
2110 ret = phylink_validate(pl, support1, &config);
2111 if (ret) {
2112 phylink_err(pl, "validation of %s/%s with support %*pb failed: %d\n",
2113 phylink_an_mode_str(mode),
2114 phy_modes(config.interface),
2115 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
2116 return ret;
2117 }
2118
2119 phylink_dbg(pl, "requesting link mode %s/%s with support %*pb\n",
2120 phylink_an_mode_str(mode), phy_modes(config.interface),
2121 __ETHTOOL_LINK_MODE_MASK_NBITS, support);
2122
2123 if (phy_interface_mode_is_8023z(iface) && pl->phydev)
2124 return -EINVAL;
2125
2126 changed = !linkmode_equal(pl->supported, support) ||
2127 !linkmode_equal(pl->link_config.advertising,
2128 config.advertising);
2129 if (changed) {
2130 linkmode_copy(pl->supported, support);
2131 linkmode_copy(pl->link_config.advertising, config.advertising);
2132 }
2133
2134 if (pl->cur_link_an_mode != mode ||
2135 pl->link_config.interface != config.interface) {
2136 pl->link_config.interface = config.interface;
2137 pl->cur_link_an_mode = mode;
2138
2139 changed = true;
2140
2141 phylink_info(pl, "switched to %s/%s link mode\n",
2142 phylink_an_mode_str(mode),
2143 phy_modes(config.interface));
2144 }
2145
2146 pl->link_port = pl->sfp_port;
2147
2148 if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
2149 &pl->phylink_disable_state))
2150 phylink_mac_initial_config(pl, false);
2151
2152 return ret;
2153 }
2154
phylink_sfp_module_insert(void * upstream,const struct sfp_eeprom_id * id)2155 static int phylink_sfp_module_insert(void *upstream,
2156 const struct sfp_eeprom_id *id)
2157 {
2158 struct phylink *pl = upstream;
2159 unsigned long *support = pl->sfp_support;
2160
2161 ASSERT_RTNL();
2162
2163 linkmode_zero(support);
2164 sfp_parse_support(pl->sfp_bus, id, support);
2165 pl->sfp_port = sfp_parse_port(pl->sfp_bus, id, support);
2166
2167 /* If this module may have a PHY connecting later, defer until later */
2168 pl->sfp_may_have_phy = sfp_may_have_phy(pl->sfp_bus, id);
2169 if (pl->sfp_may_have_phy)
2170 return 0;
2171
2172 return phylink_sfp_config(pl, MLO_AN_INBAND, support, support);
2173 }
2174
phylink_sfp_module_start(void * upstream)2175 static int phylink_sfp_module_start(void *upstream)
2176 {
2177 struct phylink *pl = upstream;
2178
2179 /* If this SFP module has a PHY, start the PHY now. */
2180 if (pl->phydev) {
2181 phy_start(pl->phydev);
2182 return 0;
2183 }
2184
2185 /* If the module may have a PHY but we didn't detect one we
2186 * need to configure the MAC here.
2187 */
2188 if (!pl->sfp_may_have_phy)
2189 return 0;
2190
2191 return phylink_sfp_config(pl, MLO_AN_INBAND,
2192 pl->sfp_support, pl->sfp_support);
2193 }
2194
phylink_sfp_module_stop(void * upstream)2195 static void phylink_sfp_module_stop(void *upstream)
2196 {
2197 struct phylink *pl = upstream;
2198
2199 /* If this SFP module has a PHY, stop it. */
2200 if (pl->phydev)
2201 phy_stop(pl->phydev);
2202 }
2203
phylink_sfp_link_down(void * upstream)2204 static void phylink_sfp_link_down(void *upstream)
2205 {
2206 struct phylink *pl = upstream;
2207
2208 ASSERT_RTNL();
2209
2210 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK);
2211 }
2212
phylink_sfp_link_up(void * upstream)2213 static void phylink_sfp_link_up(void *upstream)
2214 {
2215 struct phylink *pl = upstream;
2216
2217 ASSERT_RTNL();
2218
2219 clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
2220 phylink_run_resolve(pl);
2221 }
2222
2223 /* The Broadcom BCM84881 in the Methode DM7052 is unable to provide a SGMII
2224 * or 802.3z control word, so inband will not work.
2225 */
phylink_phy_no_inband(struct phy_device * phy)2226 static bool phylink_phy_no_inband(struct phy_device *phy)
2227 {
2228 return phy->is_c45 &&
2229 (phy->c45_ids.device_ids[1] & 0xfffffff0) == 0xae025150;
2230 }
2231
phylink_sfp_connect_phy(void * upstream,struct phy_device * phy)2232 static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
2233 {
2234 struct phylink *pl = upstream;
2235 phy_interface_t interface;
2236 u8 mode;
2237 int ret;
2238
2239 /*
2240 * This is the new way of dealing with flow control for PHYs,
2241 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
2242 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
2243 * using our validate call to the MAC, we rely upon the MAC
2244 * clearing the bits from both supported and advertising fields.
2245 */
2246 phy_support_asym_pause(phy);
2247
2248 if (phylink_phy_no_inband(phy))
2249 mode = MLO_AN_PHY;
2250 else
2251 mode = MLO_AN_INBAND;
2252
2253 /* Do the initial configuration */
2254 ret = phylink_sfp_config(pl, mode, phy->supported, phy->advertising);
2255 if (ret < 0)
2256 return ret;
2257
2258 interface = pl->link_config.interface;
2259 ret = phylink_attach_phy(pl, phy, interface);
2260 if (ret < 0)
2261 return ret;
2262
2263 ret = phylink_bringup_phy(pl, phy, interface);
2264 if (ret)
2265 phy_detach(phy);
2266
2267 return ret;
2268 }
2269
phylink_sfp_disconnect_phy(void * upstream)2270 static void phylink_sfp_disconnect_phy(void *upstream)
2271 {
2272 phylink_disconnect_phy(upstream);
2273 }
2274
2275 static const struct sfp_upstream_ops sfp_phylink_ops = {
2276 .attach = phylink_sfp_attach,
2277 .detach = phylink_sfp_detach,
2278 .module_insert = phylink_sfp_module_insert,
2279 .module_start = phylink_sfp_module_start,
2280 .module_stop = phylink_sfp_module_stop,
2281 .link_up = phylink_sfp_link_up,
2282 .link_down = phylink_sfp_link_down,
2283 .connect_phy = phylink_sfp_connect_phy,
2284 .disconnect_phy = phylink_sfp_disconnect_phy,
2285 };
2286
2287 /* Helpers for MAC drivers */
2288
2289 /**
2290 * phylink_helper_basex_speed() - 1000BaseX/2500BaseX helper
2291 * @state: a pointer to a &struct phylink_link_state
2292 *
2293 * Inspect the interface mode, advertising mask or forced speed and
2294 * decide whether to run at 2.5Gbit or 1Gbit appropriately, switching
2295 * the interface mode to suit. @state->interface is appropriately
2296 * updated, and the advertising mask has the "other" baseX_Full flag
2297 * cleared.
2298 */
phylink_helper_basex_speed(struct phylink_link_state * state)2299 void phylink_helper_basex_speed(struct phylink_link_state *state)
2300 {
2301 if (phy_interface_mode_is_8023z(state->interface)) {
2302 bool want_2500 = state->an_enabled ?
2303 phylink_test(state->advertising, 2500baseX_Full) :
2304 state->speed == SPEED_2500;
2305
2306 if (want_2500) {
2307 phylink_clear(state->advertising, 1000baseX_Full);
2308 state->interface = PHY_INTERFACE_MODE_2500BASEX;
2309 } else {
2310 phylink_clear(state->advertising, 2500baseX_Full);
2311 state->interface = PHY_INTERFACE_MODE_1000BASEX;
2312 }
2313 }
2314 }
2315 EXPORT_SYMBOL_GPL(phylink_helper_basex_speed);
2316
phylink_decode_c37_word(struct phylink_link_state * state,uint16_t config_reg,int speed)2317 static void phylink_decode_c37_word(struct phylink_link_state *state,
2318 uint16_t config_reg, int speed)
2319 {
2320 bool tx_pause, rx_pause;
2321 int fd_bit;
2322
2323 if (speed == SPEED_2500)
2324 fd_bit = ETHTOOL_LINK_MODE_2500baseX_Full_BIT;
2325 else
2326 fd_bit = ETHTOOL_LINK_MODE_1000baseX_Full_BIT;
2327
2328 mii_lpa_mod_linkmode_x(state->lp_advertising, config_reg, fd_bit);
2329
2330 if (linkmode_test_bit(fd_bit, state->advertising) &&
2331 linkmode_test_bit(fd_bit, state->lp_advertising)) {
2332 state->speed = speed;
2333 state->duplex = DUPLEX_FULL;
2334 } else {
2335 /* negotiation failure */
2336 state->link = false;
2337 }
2338
2339 linkmode_resolve_pause(state->advertising, state->lp_advertising,
2340 &tx_pause, &rx_pause);
2341
2342 if (tx_pause)
2343 state->pause |= MLO_PAUSE_TX;
2344 if (rx_pause)
2345 state->pause |= MLO_PAUSE_RX;
2346 }
2347
phylink_decode_sgmii_word(struct phylink_link_state * state,uint16_t config_reg)2348 static void phylink_decode_sgmii_word(struct phylink_link_state *state,
2349 uint16_t config_reg)
2350 {
2351 if (!(config_reg & LPA_SGMII_LINK)) {
2352 state->link = false;
2353 return;
2354 }
2355
2356 switch (config_reg & LPA_SGMII_SPD_MASK) {
2357 case LPA_SGMII_10:
2358 state->speed = SPEED_10;
2359 break;
2360 case LPA_SGMII_100:
2361 state->speed = SPEED_100;
2362 break;
2363 case LPA_SGMII_1000:
2364 state->speed = SPEED_1000;
2365 break;
2366 default:
2367 state->link = false;
2368 return;
2369 }
2370 if (config_reg & LPA_SGMII_FULL_DUPLEX)
2371 state->duplex = DUPLEX_FULL;
2372 else
2373 state->duplex = DUPLEX_HALF;
2374 }
2375
2376 /**
2377 * phylink_decode_usxgmii_word() - decode the USXGMII word from a MAC PCS
2378 * @state: a pointer to a struct phylink_link_state.
2379 * @lpa: a 16 bit value which stores the USXGMII auto-negotiation word
2380 *
2381 * Helper for MAC PCS supporting the USXGMII protocol and the auto-negotiation
2382 * code word. Decode the USXGMII code word and populate the corresponding fields
2383 * (speed, duplex) into the phylink_link_state structure.
2384 */
phylink_decode_usxgmii_word(struct phylink_link_state * state,uint16_t lpa)2385 void phylink_decode_usxgmii_word(struct phylink_link_state *state,
2386 uint16_t lpa)
2387 {
2388 switch (lpa & MDIO_USXGMII_SPD_MASK) {
2389 case MDIO_USXGMII_10:
2390 state->speed = SPEED_10;
2391 break;
2392 case MDIO_USXGMII_100:
2393 state->speed = SPEED_100;
2394 break;
2395 case MDIO_USXGMII_1000:
2396 state->speed = SPEED_1000;
2397 break;
2398 case MDIO_USXGMII_2500:
2399 state->speed = SPEED_2500;
2400 break;
2401 case MDIO_USXGMII_5000:
2402 state->speed = SPEED_5000;
2403 break;
2404 case MDIO_USXGMII_10G:
2405 state->speed = SPEED_10000;
2406 break;
2407 default:
2408 state->link = false;
2409 return;
2410 }
2411
2412 if (lpa & MDIO_USXGMII_FULL_DUPLEX)
2413 state->duplex = DUPLEX_FULL;
2414 else
2415 state->duplex = DUPLEX_HALF;
2416 }
2417 EXPORT_SYMBOL_GPL(phylink_decode_usxgmii_word);
2418
2419 /**
2420 * phylink_mii_c22_pcs_get_state() - read the MAC PCS state
2421 * @pcs: a pointer to a &struct mdio_device.
2422 * @state: a pointer to a &struct phylink_link_state.
2423 *
2424 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
2425 * clause 37 negotiation and/or SGMII control.
2426 *
2427 * Read the MAC PCS state from the MII device configured in @config and
2428 * parse the Clause 37 or Cisco SGMII link partner negotiation word into
2429 * the phylink @state structure. This is suitable to be directly plugged
2430 * into the mac_pcs_get_state() member of the struct phylink_mac_ops
2431 * structure.
2432 */
phylink_mii_c22_pcs_get_state(struct mdio_device * pcs,struct phylink_link_state * state)2433 void phylink_mii_c22_pcs_get_state(struct mdio_device *pcs,
2434 struct phylink_link_state *state)
2435 {
2436 struct mii_bus *bus = pcs->bus;
2437 int addr = pcs->addr;
2438 int bmsr, lpa;
2439
2440 bmsr = mdiobus_read(bus, addr, MII_BMSR);
2441 lpa = mdiobus_read(bus, addr, MII_LPA);
2442 if (bmsr < 0 || lpa < 0) {
2443 state->link = false;
2444 return;
2445 }
2446
2447 state->link = !!(bmsr & BMSR_LSTATUS);
2448 state->an_complete = !!(bmsr & BMSR_ANEGCOMPLETE);
2449 if (!state->link)
2450 return;
2451
2452 switch (state->interface) {
2453 case PHY_INTERFACE_MODE_1000BASEX:
2454 phylink_decode_c37_word(state, lpa, SPEED_1000);
2455 break;
2456
2457 case PHY_INTERFACE_MODE_2500BASEX:
2458 phylink_decode_c37_word(state, lpa, SPEED_2500);
2459 break;
2460
2461 case PHY_INTERFACE_MODE_SGMII:
2462 case PHY_INTERFACE_MODE_QSGMII:
2463 phylink_decode_sgmii_word(state, lpa);
2464 break;
2465
2466 default:
2467 state->link = false;
2468 break;
2469 }
2470 }
2471 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_get_state);
2472
2473 /**
2474 * phylink_mii_c22_pcs_set_advertisement() - configure the clause 37 PCS
2475 * advertisement
2476 * @pcs: a pointer to a &struct mdio_device.
2477 * @interface: the PHY interface mode being configured
2478 * @advertising: the ethtool advertisement mask
2479 *
2480 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
2481 * clause 37 negotiation and/or SGMII control.
2482 *
2483 * Configure the clause 37 PCS advertisement as specified by @state. This
2484 * does not trigger a renegotiation; phylink will do that via the
2485 * mac_an_restart() method of the struct phylink_mac_ops structure.
2486 *
2487 * Returns negative error code on failure to configure the advertisement,
2488 * zero if no change has been made, or one if the advertisement has changed.
2489 */
phylink_mii_c22_pcs_set_advertisement(struct mdio_device * pcs,phy_interface_t interface,const unsigned long * advertising)2490 int phylink_mii_c22_pcs_set_advertisement(struct mdio_device *pcs,
2491 phy_interface_t interface,
2492 const unsigned long *advertising)
2493 {
2494 struct mii_bus *bus = pcs->bus;
2495 int addr = pcs->addr;
2496 int val, ret;
2497 u16 adv;
2498
2499 switch (interface) {
2500 case PHY_INTERFACE_MODE_1000BASEX:
2501 case PHY_INTERFACE_MODE_2500BASEX:
2502 adv = ADVERTISE_1000XFULL;
2503 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2504 advertising))
2505 adv |= ADVERTISE_1000XPAUSE;
2506 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2507 advertising))
2508 adv |= ADVERTISE_1000XPSE_ASYM;
2509
2510 val = mdiobus_read(bus, addr, MII_ADVERTISE);
2511 if (val < 0)
2512 return val;
2513
2514 if (val == adv)
2515 return 0;
2516
2517 ret = mdiobus_write(bus, addr, MII_ADVERTISE, adv);
2518 if (ret < 0)
2519 return ret;
2520
2521 return 1;
2522
2523 case PHY_INTERFACE_MODE_SGMII:
2524 val = mdiobus_read(bus, addr, MII_ADVERTISE);
2525 if (val < 0)
2526 return val;
2527
2528 if (val == 0x0001)
2529 return 0;
2530
2531 ret = mdiobus_write(bus, addr, MII_ADVERTISE, 0x0001);
2532 if (ret < 0)
2533 return ret;
2534
2535 return 1;
2536
2537 default:
2538 /* Nothing to do for other modes */
2539 return 0;
2540 }
2541 }
2542 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_set_advertisement);
2543
2544 /**
2545 * phylink_mii_c22_pcs_config() - configure clause 22 PCS
2546 * @pcs: a pointer to a &struct mdio_device.
2547 * @mode: link autonegotiation mode
2548 * @interface: the PHY interface mode being configured
2549 * @advertising: the ethtool advertisement mask
2550 *
2551 * Configure a Clause 22 PCS PHY with the appropriate negotiation
2552 * parameters for the @mode, @interface and @advertising parameters.
2553 * Returns negative error number on failure, zero if the advertisement
2554 * has not changed, or positive if there is a change.
2555 */
phylink_mii_c22_pcs_config(struct mdio_device * pcs,unsigned int mode,phy_interface_t interface,const unsigned long * advertising)2556 int phylink_mii_c22_pcs_config(struct mdio_device *pcs, unsigned int mode,
2557 phy_interface_t interface,
2558 const unsigned long *advertising)
2559 {
2560 bool changed;
2561 u16 bmcr;
2562 int ret;
2563
2564 ret = phylink_mii_c22_pcs_set_advertisement(pcs, interface,
2565 advertising);
2566 if (ret < 0)
2567 return ret;
2568
2569 changed = ret > 0;
2570
2571 bmcr = mode == MLO_AN_INBAND ? BMCR_ANENABLE : 0;
2572 ret = mdiobus_modify(pcs->bus, pcs->addr, MII_BMCR,
2573 BMCR_ANENABLE, bmcr);
2574 if (ret < 0)
2575 return ret;
2576
2577 return changed ? 1 : 0;
2578 }
2579 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_config);
2580
2581 /**
2582 * phylink_mii_c22_pcs_an_restart() - restart 802.3z autonegotiation
2583 * @pcs: a pointer to a &struct mdio_device.
2584 *
2585 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
2586 * clause 37 negotiation.
2587 *
2588 * Restart the clause 37 negotiation with the link partner. This is
2589 * suitable to be directly plugged into the mac_pcs_get_state() member
2590 * of the struct phylink_mac_ops structure.
2591 */
phylink_mii_c22_pcs_an_restart(struct mdio_device * pcs)2592 void phylink_mii_c22_pcs_an_restart(struct mdio_device *pcs)
2593 {
2594 struct mii_bus *bus = pcs->bus;
2595 int val, addr = pcs->addr;
2596
2597 val = mdiobus_read(bus, addr, MII_BMCR);
2598 if (val >= 0) {
2599 val |= BMCR_ANRESTART;
2600
2601 mdiobus_write(bus, addr, MII_BMCR, val);
2602 }
2603 }
2604 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_an_restart);
2605
phylink_mii_c45_pcs_get_state(struct mdio_device * pcs,struct phylink_link_state * state)2606 void phylink_mii_c45_pcs_get_state(struct mdio_device *pcs,
2607 struct phylink_link_state *state)
2608 {
2609 struct mii_bus *bus = pcs->bus;
2610 int addr = pcs->addr;
2611 int stat;
2612
2613 stat = mdiobus_c45_read(bus, addr, MDIO_MMD_PCS, MDIO_STAT1);
2614 if (stat < 0) {
2615 state->link = false;
2616 return;
2617 }
2618
2619 state->link = !!(stat & MDIO_STAT1_LSTATUS);
2620 if (!state->link)
2621 return;
2622
2623 switch (state->interface) {
2624 case PHY_INTERFACE_MODE_10GBASER:
2625 state->speed = SPEED_10000;
2626 state->duplex = DUPLEX_FULL;
2627 break;
2628
2629 default:
2630 break;
2631 }
2632 }
2633 EXPORT_SYMBOL_GPL(phylink_mii_c45_pcs_get_state);
2634
2635 MODULE_LICENSE("GPL v2");
2636