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