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