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