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