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