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