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