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