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