GNU Linux-libre 4.19.286-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         if (!fwnode_device_is_available(ref.fwnode)) {
558                 fwnode_handle_put(ref.fwnode);
559                 return 0;
560         }
561
562         pl->sfp_bus = sfp_register_upstream(ref.fwnode, pl->netdev, pl,
563                                             &sfp_phylink_ops);
564         if (!pl->sfp_bus)
565                 return -ENOMEM;
566
567         return 0;
568 }
569
570 /**
571  * phylink_create() - create a phylink instance
572  * @ndev: a pointer to the &struct net_device
573  * @fwnode: a pointer to a &struct fwnode_handle describing the network
574  *      interface
575  * @iface: the desired link mode defined by &typedef phy_interface_t
576  * @ops: a pointer to a &struct phylink_mac_ops for the MAC.
577  *
578  * Create a new phylink instance, and parse the link parameters found in @np.
579  * This will parse in-band modes, fixed-link or SFP configuration.
580  *
581  * Returns a pointer to a &struct phylink, or an error-pointer value. Users
582  * must use IS_ERR() to check for errors from this function.
583  */
584 struct phylink *phylink_create(struct net_device *ndev,
585                                struct fwnode_handle *fwnode,
586                                phy_interface_t iface,
587                                const struct phylink_mac_ops *ops)
588 {
589         struct phylink *pl;
590         int ret;
591
592         pl = kzalloc(sizeof(*pl), GFP_KERNEL);
593         if (!pl)
594                 return ERR_PTR(-ENOMEM);
595
596         mutex_init(&pl->state_mutex);
597         INIT_WORK(&pl->resolve, phylink_resolve);
598         pl->netdev = ndev;
599         pl->phy_state.interface = iface;
600         pl->link_interface = iface;
601         if (iface == PHY_INTERFACE_MODE_MOCA)
602                 pl->link_port = PORT_BNC;
603         else
604                 pl->link_port = PORT_MII;
605         pl->link_config.interface = iface;
606         pl->link_config.pause = MLO_PAUSE_AN;
607         pl->link_config.speed = SPEED_UNKNOWN;
608         pl->link_config.duplex = DUPLEX_UNKNOWN;
609         pl->link_config.an_enabled = true;
610         pl->ops = ops;
611         __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
612         timer_setup(&pl->link_poll, phylink_fixed_poll, 0);
613
614         bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
615         linkmode_copy(pl->link_config.advertising, pl->supported);
616         phylink_validate(pl, pl->supported, &pl->link_config);
617
618         ret = phylink_parse_mode(pl, fwnode);
619         if (ret < 0) {
620                 kfree(pl);
621                 return ERR_PTR(ret);
622         }
623
624         if (pl->link_an_mode == MLO_AN_FIXED) {
625                 ret = phylink_parse_fixedlink(pl, fwnode);
626                 if (ret < 0) {
627                         kfree(pl);
628                         return ERR_PTR(ret);
629                 }
630         }
631
632         ret = phylink_register_sfp(pl, fwnode);
633         if (ret < 0) {
634                 kfree(pl);
635                 return ERR_PTR(ret);
636         }
637
638         return pl;
639 }
640 EXPORT_SYMBOL_GPL(phylink_create);
641
642 /**
643  * phylink_destroy() - cleanup and destroy the phylink instance
644  * @pl: a pointer to a &struct phylink returned from phylink_create()
645  *
646  * Destroy a phylink instance. Any PHY that has been attached must have been
647  * cleaned up via phylink_disconnect_phy() prior to calling this function.
648  */
649 void phylink_destroy(struct phylink *pl)
650 {
651         if (pl->sfp_bus)
652                 sfp_unregister_upstream(pl->sfp_bus);
653         if (!IS_ERR_OR_NULL(pl->link_gpio))
654                 gpiod_put(pl->link_gpio);
655
656         cancel_work_sync(&pl->resolve);
657         kfree(pl);
658 }
659 EXPORT_SYMBOL_GPL(phylink_destroy);
660
661 static void phylink_phy_change(struct phy_device *phydev, bool up,
662                                bool do_carrier)
663 {
664         struct phylink *pl = phydev->phylink;
665
666         mutex_lock(&pl->state_mutex);
667         pl->phy_state.speed = phydev->speed;
668         pl->phy_state.duplex = phydev->duplex;
669         pl->phy_state.pause = MLO_PAUSE_NONE;
670         if (phydev->pause)
671                 pl->phy_state.pause |= MLO_PAUSE_SYM;
672         if (phydev->asym_pause)
673                 pl->phy_state.pause |= MLO_PAUSE_ASYM;
674         pl->phy_state.interface = phydev->interface;
675         pl->phy_state.link = up;
676         mutex_unlock(&pl->state_mutex);
677
678         phylink_run_resolve(pl);
679
680         netdev_dbg(pl->netdev, "phy link %s %s/%s/%s\n", up ? "up" : "down",
681                    phy_modes(phydev->interface),
682                    phy_speed_to_str(phydev->speed),
683                    phy_duplex_to_str(phydev->duplex));
684 }
685
686 static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy)
687 {
688         struct phylink_link_state config;
689         __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
690         u32 advertising;
691         int ret;
692
693         memset(&config, 0, sizeof(config));
694         ethtool_convert_legacy_u32_to_link_mode(supported, phy->supported);
695         ethtool_convert_legacy_u32_to_link_mode(config.advertising,
696                                                 phy->advertising);
697         config.interface = pl->link_config.interface;
698
699         /*
700          * This is the new way of dealing with flow control for PHYs,
701          * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
702          * phy drivers should not set SUPPORTED_[Asym_]Pause") except
703          * using our validate call to the MAC, we rely upon the MAC
704          * clearing the bits from both supported and advertising fields.
705          */
706         if (phylink_test(supported, Pause))
707                 phylink_set(config.advertising, Pause);
708         if (phylink_test(supported, Asym_Pause))
709                 phylink_set(config.advertising, Asym_Pause);
710
711         ret = phylink_validate(pl, supported, &config);
712         if (ret)
713                 return ret;
714
715         phy->phylink = pl;
716         phy->phy_link_change = phylink_phy_change;
717
718         netdev_info(pl->netdev,
719                     "PHY [%s] driver [%s]\n", dev_name(&phy->mdio.dev),
720                     phy->drv->name);
721
722         mutex_lock(&phy->lock);
723         mutex_lock(&pl->state_mutex);
724         pl->phydev = phy;
725         linkmode_copy(pl->supported, supported);
726         linkmode_copy(pl->link_config.advertising, config.advertising);
727
728         /* Restrict the phy advertisement according to the MAC support. */
729         ethtool_convert_link_mode_to_legacy_u32(&advertising, config.advertising);
730         phy->advertising = advertising;
731         mutex_unlock(&pl->state_mutex);
732         mutex_unlock(&phy->lock);
733
734         netdev_dbg(pl->netdev,
735                    "phy: setting supported %*pb advertising 0x%08x\n",
736                    __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
737                    phy->advertising);
738
739         phy_start_machine(phy);
740         if (phy->irq > 0)
741                 phy_start_interrupts(phy);
742
743         return 0;
744 }
745
746 static int __phylink_connect_phy(struct phylink *pl, struct phy_device *phy,
747                 phy_interface_t interface)
748 {
749         int ret;
750
751         if (WARN_ON(pl->link_an_mode == MLO_AN_FIXED ||
752                     (pl->link_an_mode == MLO_AN_INBAND &&
753                      phy_interface_mode_is_8023z(interface))))
754                 return -EINVAL;
755
756         if (pl->phydev)
757                 return -EBUSY;
758
759         ret = phy_attach_direct(pl->netdev, phy, 0, interface);
760         if (ret)
761                 return ret;
762
763         ret = phylink_bringup_phy(pl, phy);
764         if (ret)
765                 phy_detach(phy);
766
767         return ret;
768 }
769
770 /**
771  * phylink_connect_phy() - connect a PHY to the phylink instance
772  * @pl: a pointer to a &struct phylink returned from phylink_create()
773  * @phy: a pointer to a &struct phy_device.
774  *
775  * Connect @phy to the phylink instance specified by @pl by calling
776  * phy_attach_direct(). Configure the @phy according to the MAC driver's
777  * capabilities, start the PHYLIB state machine and enable any interrupts
778  * that the PHY supports.
779  *
780  * This updates the phylink's ethtool supported and advertising link mode
781  * masks.
782  *
783  * Returns 0 on success or a negative errno.
784  */
785 int phylink_connect_phy(struct phylink *pl, struct phy_device *phy)
786 {
787         /* Use PHY device/driver interface */
788         if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
789                 pl->link_interface = phy->interface;
790                 pl->link_config.interface = pl->link_interface;
791         }
792
793         return __phylink_connect_phy(pl, phy, pl->link_interface);
794 }
795 EXPORT_SYMBOL_GPL(phylink_connect_phy);
796
797 /**
798  * phylink_of_phy_connect() - connect the PHY specified in the DT mode.
799  * @pl: a pointer to a &struct phylink returned from phylink_create()
800  * @dn: a pointer to a &struct device_node.
801  * @flags: PHY-specific flags to communicate to the PHY device driver
802  *
803  * Connect the phy specified in the device node @dn to the phylink instance
804  * specified by @pl. Actions specified in phylink_connect_phy() will be
805  * performed.
806  *
807  * Returns 0 on success or a negative errno.
808  */
809 int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn,
810                            u32 flags)
811 {
812         struct device_node *phy_node;
813         struct phy_device *phy_dev;
814         int ret;
815
816         /* Fixed links and 802.3z are handled without needing a PHY */
817         if (pl->link_an_mode == MLO_AN_FIXED ||
818             (pl->link_an_mode == MLO_AN_INBAND &&
819              phy_interface_mode_is_8023z(pl->link_interface)))
820                 return 0;
821
822         phy_node = of_parse_phandle(dn, "phy-handle", 0);
823         if (!phy_node)
824                 phy_node = of_parse_phandle(dn, "phy", 0);
825         if (!phy_node)
826                 phy_node = of_parse_phandle(dn, "phy-device", 0);
827
828         if (!phy_node) {
829                 if (pl->link_an_mode == MLO_AN_PHY)
830                         return -ENODEV;
831                 return 0;
832         }
833
834         phy_dev = of_phy_attach(pl->netdev, phy_node, flags,
835                                 pl->link_interface);
836         /* We're done with the phy_node handle */
837         of_node_put(phy_node);
838
839         if (!phy_dev)
840                 return -ENODEV;
841
842         ret = phylink_bringup_phy(pl, phy_dev);
843         if (ret)
844                 phy_detach(phy_dev);
845
846         return ret;
847 }
848 EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
849
850 /**
851  * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
852  *   instance.
853  * @pl: a pointer to a &struct phylink returned from phylink_create()
854  *
855  * Disconnect any current PHY from the phylink instance described by @pl.
856  */
857 void phylink_disconnect_phy(struct phylink *pl)
858 {
859         struct phy_device *phy;
860
861         ASSERT_RTNL();
862
863         phy = pl->phydev;
864         if (phy) {
865                 mutex_lock(&phy->lock);
866                 mutex_lock(&pl->state_mutex);
867                 pl->phydev = NULL;
868                 mutex_unlock(&pl->state_mutex);
869                 mutex_unlock(&phy->lock);
870                 flush_work(&pl->resolve);
871
872                 phy_disconnect(phy);
873         }
874 }
875 EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
876
877 /**
878  * phylink_fixed_state_cb() - allow setting a fixed link callback
879  * @pl: a pointer to a &struct phylink returned from phylink_create()
880  * @cb: callback to execute to determine the fixed link state.
881  *
882  * The MAC driver should call this driver when the state of its link
883  * can be determined through e.g: an out of band MMIO register.
884  */
885 int phylink_fixed_state_cb(struct phylink *pl,
886                            void (*cb)(struct net_device *dev,
887                                       struct phylink_link_state *state))
888 {
889         /* It does not make sense to let the link be overriden unless we use
890          * MLO_AN_FIXED
891          */
892         if (pl->link_an_mode != MLO_AN_FIXED)
893                 return -EINVAL;
894
895         mutex_lock(&pl->state_mutex);
896         pl->get_fixed_state = cb;
897         mutex_unlock(&pl->state_mutex);
898
899         return 0;
900 }
901 EXPORT_SYMBOL_GPL(phylink_fixed_state_cb);
902
903 /**
904  * phylink_mac_change() - notify phylink of a change in MAC state
905  * @pl: a pointer to a &struct phylink returned from phylink_create()
906  * @up: indicates whether the link is currently up.
907  *
908  * The MAC driver should call this driver when the state of its link
909  * changes (eg, link failure, new negotiation results, etc.)
910  */
911 void phylink_mac_change(struct phylink *pl, bool up)
912 {
913         if (!up)
914                 pl->mac_link_dropped = true;
915         phylink_run_resolve(pl);
916         netdev_dbg(pl->netdev, "mac link %s\n", up ? "up" : "down");
917 }
918 EXPORT_SYMBOL_GPL(phylink_mac_change);
919
920 /**
921  * phylink_start() - start a phylink instance
922  * @pl: a pointer to a &struct phylink returned from phylink_create()
923  *
924  * Start the phylink instance specified by @pl, configuring the MAC for the
925  * desired link mode(s) and negotiation style. This should be called from the
926  * network device driver's &struct net_device_ops ndo_open() method.
927  */
928 void phylink_start(struct phylink *pl)
929 {
930         ASSERT_RTNL();
931
932         netdev_info(pl->netdev, "configuring for %s/%s link mode\n",
933                     phylink_an_mode_str(pl->link_an_mode),
934                     phy_modes(pl->link_config.interface));
935
936         /* Always set the carrier off */
937         netif_carrier_off(pl->netdev);
938
939         /* Apply the link configuration to the MAC when starting. This allows
940          * a fixed-link to start with the correct parameters, and also
941          * ensures that we set the appropriate advertisement for Serdes links.
942          */
943         phylink_resolve_flow(pl, &pl->link_config);
944         phylink_mac_config(pl, &pl->link_config);
945
946         /* Restart autonegotiation if using 802.3z to ensure that the link
947          * parameters are properly negotiated.  This is necessary for DSA
948          * switches using 802.3z negotiation to ensure they see our modes.
949          */
950         phylink_mac_an_restart(pl);
951
952         clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
953         phylink_run_resolve(pl);
954
955         if (pl->link_an_mode == MLO_AN_FIXED && !IS_ERR(pl->link_gpio))
956                 mod_timer(&pl->link_poll, jiffies + HZ);
957         if (pl->sfp_bus)
958                 sfp_upstream_start(pl->sfp_bus);
959         if (pl->phydev)
960                 phy_start(pl->phydev);
961 }
962 EXPORT_SYMBOL_GPL(phylink_start);
963
964 /**
965  * phylink_stop() - stop a phylink instance
966  * @pl: a pointer to a &struct phylink returned from phylink_create()
967  *
968  * Stop the phylink instance specified by @pl. This should be called from the
969  * network device driver's &struct net_device_ops ndo_stop() method.  The
970  * network device's carrier state should not be changed prior to calling this
971  * function.
972  */
973 void phylink_stop(struct phylink *pl)
974 {
975         ASSERT_RTNL();
976
977         if (pl->phydev)
978                 phy_stop(pl->phydev);
979         if (pl->sfp_bus)
980                 sfp_upstream_stop(pl->sfp_bus);
981         if (pl->link_an_mode == MLO_AN_FIXED && !IS_ERR(pl->link_gpio))
982                 del_timer_sync(&pl->link_poll);
983
984         phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_STOPPED);
985 }
986 EXPORT_SYMBOL_GPL(phylink_stop);
987
988 /**
989  * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY
990  * @pl: a pointer to a &struct phylink returned from phylink_create()
991  * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters
992  *
993  * Read the wake on lan parameters from the PHY attached to the phylink
994  * instance specified by @pl. If no PHY is currently attached, report no
995  * support for wake on lan.
996  */
997 void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
998 {
999         ASSERT_RTNL();
1000
1001         wol->supported = 0;
1002         wol->wolopts = 0;
1003
1004         if (pl->phydev)
1005                 phy_ethtool_get_wol(pl->phydev, wol);
1006 }
1007 EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol);
1008
1009 /**
1010  * phylink_ethtool_set_wol() - set wake on lan parameters
1011  * @pl: a pointer to a &struct phylink returned from phylink_create()
1012  * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters
1013  *
1014  * Set the wake on lan parameters for the PHY attached to the phylink
1015  * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP
1016  * error.
1017  *
1018  * Returns zero on success or negative errno code.
1019  */
1020 int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
1021 {
1022         int ret = -EOPNOTSUPP;
1023
1024         ASSERT_RTNL();
1025
1026         if (pl->phydev)
1027                 ret = phy_ethtool_set_wol(pl->phydev, wol);
1028
1029         return ret;
1030 }
1031 EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol);
1032
1033 static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b)
1034 {
1035         __ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
1036
1037         linkmode_zero(mask);
1038         phylink_set_port_modes(mask);
1039
1040         linkmode_and(dst, dst, mask);
1041         linkmode_or(dst, dst, b);
1042 }
1043
1044 static void phylink_get_ksettings(const struct phylink_link_state *state,
1045                                   struct ethtool_link_ksettings *kset)
1046 {
1047         phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
1048         linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
1049         kset->base.speed = state->speed;
1050         kset->base.duplex = state->duplex;
1051         kset->base.autoneg = state->an_enabled ? AUTONEG_ENABLE :
1052                                 AUTONEG_DISABLE;
1053 }
1054
1055 /**
1056  * phylink_ethtool_ksettings_get() - get the current link settings
1057  * @pl: a pointer to a &struct phylink returned from phylink_create()
1058  * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings
1059  *
1060  * Read the current link settings for the phylink instance specified by @pl.
1061  * This will be the link settings read from the MAC, PHY or fixed link
1062  * settings depending on the current negotiation mode.
1063  */
1064 int phylink_ethtool_ksettings_get(struct phylink *pl,
1065                                   struct ethtool_link_ksettings *kset)
1066 {
1067         struct phylink_link_state link_state;
1068
1069         ASSERT_RTNL();
1070
1071         if (pl->phydev) {
1072                 phy_ethtool_ksettings_get(pl->phydev, kset);
1073         } else {
1074                 kset->base.port = pl->link_port;
1075         }
1076
1077         linkmode_copy(kset->link_modes.supported, pl->supported);
1078
1079         switch (pl->link_an_mode) {
1080         case MLO_AN_FIXED:
1081                 /* We are using fixed settings. Report these as the
1082                  * current link settings - and note that these also
1083                  * represent the supported speeds/duplex/pause modes.
1084                  */
1085                 phylink_get_fixed_state(pl, &link_state);
1086                 phylink_get_ksettings(&link_state, kset);
1087                 break;
1088
1089         case MLO_AN_INBAND:
1090                 /* If there is a phy attached, then use the reported
1091                  * settings from the phy with no modification.
1092                  */
1093                 if (pl->phydev)
1094                         break;
1095
1096                 phylink_get_mac_state(pl, &link_state);
1097
1098                 /* The MAC is reporting the link results from its own PCS
1099                  * layer via in-band status. Report these as the current
1100                  * link settings.
1101                  */
1102                 phylink_get_ksettings(&link_state, kset);
1103                 break;
1104         }
1105
1106         return 0;
1107 }
1108 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get);
1109
1110 /**
1111  * phylink_ethtool_ksettings_set() - set the link settings
1112  * @pl: a pointer to a &struct phylink returned from phylink_create()
1113  * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes
1114  */
1115 int phylink_ethtool_ksettings_set(struct phylink *pl,
1116                                   const struct ethtool_link_ksettings *kset)
1117 {
1118         struct ethtool_link_ksettings our_kset;
1119         struct phylink_link_state config;
1120         int ret;
1121
1122         ASSERT_RTNL();
1123
1124         if (kset->base.autoneg != AUTONEG_DISABLE &&
1125             kset->base.autoneg != AUTONEG_ENABLE)
1126                 return -EINVAL;
1127
1128         config = pl->link_config;
1129
1130         /* Mask out unsupported advertisements */
1131         linkmode_and(config.advertising, kset->link_modes.advertising,
1132                      pl->supported);
1133
1134         /* FIXME: should we reject autoneg if phy/mac does not support it? */
1135         if (kset->base.autoneg == AUTONEG_DISABLE) {
1136                 const struct phy_setting *s;
1137
1138                 /* Autonegotiation disabled, select a suitable speed and
1139                  * duplex.
1140                  */
1141                 s = phy_lookup_setting(kset->base.speed, kset->base.duplex,
1142                                        pl->supported,
1143                                        __ETHTOOL_LINK_MODE_MASK_NBITS, false);
1144                 if (!s)
1145                         return -EINVAL;
1146
1147                 /* If we have a fixed link (as specified by firmware), refuse
1148                  * to change link parameters.
1149                  */
1150                 if (pl->link_an_mode == MLO_AN_FIXED &&
1151                     (s->speed != pl->link_config.speed ||
1152                      s->duplex != pl->link_config.duplex))
1153                         return -EINVAL;
1154
1155                 config.speed = s->speed;
1156                 config.duplex = s->duplex;
1157                 config.an_enabled = false;
1158
1159                 __clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
1160         } else {
1161                 /* If we have a fixed link, refuse to enable autonegotiation */
1162                 if (pl->link_an_mode == MLO_AN_FIXED)
1163                         return -EINVAL;
1164
1165                 config.speed = SPEED_UNKNOWN;
1166                 config.duplex = DUPLEX_UNKNOWN;
1167                 config.an_enabled = true;
1168
1169                 __set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
1170         }
1171
1172         if (phylink_validate(pl, pl->supported, &config))
1173                 return -EINVAL;
1174
1175         /* If autonegotiation is enabled, we must have an advertisement */
1176         if (config.an_enabled && phylink_is_empty_linkmode(config.advertising))
1177                 return -EINVAL;
1178
1179         our_kset = *kset;
1180         linkmode_copy(our_kset.link_modes.advertising, config.advertising);
1181         our_kset.base.speed = config.speed;
1182         our_kset.base.duplex = config.duplex;
1183
1184         /* If we have a PHY, configure the phy */
1185         if (pl->phydev) {
1186                 ret = phy_ethtool_ksettings_set(pl->phydev, &our_kset);
1187                 if (ret)
1188                         return ret;
1189         }
1190
1191         mutex_lock(&pl->state_mutex);
1192         /* Configure the MAC to match the new settings */
1193         linkmode_copy(pl->link_config.advertising, our_kset.link_modes.advertising);
1194         pl->link_config.interface = config.interface;
1195         pl->link_config.speed = our_kset.base.speed;
1196         pl->link_config.duplex = our_kset.base.duplex;
1197         pl->link_config.an_enabled = our_kset.base.autoneg != AUTONEG_DISABLE;
1198
1199         if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
1200                 phylink_mac_config(pl, &pl->link_config);
1201                 phylink_mac_an_restart(pl);
1202         }
1203         mutex_unlock(&pl->state_mutex);
1204
1205         return 0;
1206 }
1207 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
1208
1209 /**
1210  * phylink_ethtool_nway_reset() - restart negotiation
1211  * @pl: a pointer to a &struct phylink returned from phylink_create()
1212  *
1213  * Restart negotiation for the phylink instance specified by @pl. This will
1214  * cause any attached phy to restart negotiation with the link partner, and
1215  * if the MAC is in a BaseX mode, the MAC will also be requested to restart
1216  * negotiation.
1217  *
1218  * Returns zero on success, or negative error code.
1219  */
1220 int phylink_ethtool_nway_reset(struct phylink *pl)
1221 {
1222         int ret = 0;
1223
1224         ASSERT_RTNL();
1225
1226         if (pl->phydev)
1227                 ret = phy_restart_aneg(pl->phydev);
1228         phylink_mac_an_restart(pl);
1229
1230         return ret;
1231 }
1232 EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
1233
1234 /**
1235  * phylink_ethtool_get_pauseparam() - get the current pause parameters
1236  * @pl: a pointer to a &struct phylink returned from phylink_create()
1237  * @pause: a pointer to a &struct ethtool_pauseparam
1238  */
1239 void phylink_ethtool_get_pauseparam(struct phylink *pl,
1240                                     struct ethtool_pauseparam *pause)
1241 {
1242         ASSERT_RTNL();
1243
1244         pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
1245         pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
1246         pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
1247 }
1248 EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
1249
1250 /**
1251  * phylink_ethtool_set_pauseparam() - set the current pause parameters
1252  * @pl: a pointer to a &struct phylink returned from phylink_create()
1253  * @pause: a pointer to a &struct ethtool_pauseparam
1254  */
1255 int phylink_ethtool_set_pauseparam(struct phylink *pl,
1256                                    struct ethtool_pauseparam *pause)
1257 {
1258         struct phylink_link_state *config = &pl->link_config;
1259
1260         ASSERT_RTNL();
1261
1262         if (!phylink_test(pl->supported, Pause) &&
1263             !phylink_test(pl->supported, Asym_Pause))
1264                 return -EOPNOTSUPP;
1265
1266         if (!phylink_test(pl->supported, Asym_Pause) &&
1267             pause->rx_pause != pause->tx_pause)
1268                 return -EINVAL;
1269
1270         config->pause &= ~(MLO_PAUSE_AN | MLO_PAUSE_TXRX_MASK);
1271
1272         if (pause->autoneg)
1273                 config->pause |= MLO_PAUSE_AN;
1274         if (pause->rx_pause)
1275                 config->pause |= MLO_PAUSE_RX;
1276         if (pause->tx_pause)
1277                 config->pause |= MLO_PAUSE_TX;
1278
1279         if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
1280                 switch (pl->link_an_mode) {
1281                 case MLO_AN_PHY:
1282                         /* Silently mark the carrier down, and then trigger a resolve */
1283                         netif_carrier_off(pl->netdev);
1284                         phylink_run_resolve(pl);
1285                         break;
1286
1287                 case MLO_AN_FIXED:
1288                         /* Should we allow fixed links to change against the config? */
1289                         phylink_resolve_flow(pl, config);
1290                         phylink_mac_config(pl, config);
1291                         break;
1292
1293                 case MLO_AN_INBAND:
1294                         phylink_mac_config(pl, config);
1295                         phylink_mac_an_restart(pl);
1296                         break;
1297                 }
1298         }
1299
1300         return 0;
1301 }
1302 EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
1303
1304 /**
1305  * phylink_ethtool_get_eee_err() - read the energy efficient ethernet error
1306  *   counter
1307  * @pl: a pointer to a &struct phylink returned from phylink_create().
1308  *
1309  * Read the Energy Efficient Ethernet error counter from the PHY associated
1310  * with the phylink instance specified by @pl.
1311  *
1312  * Returns positive error counter value, or negative error code.
1313  */
1314 int phylink_get_eee_err(struct phylink *pl)
1315 {
1316         int ret = 0;
1317
1318         ASSERT_RTNL();
1319
1320         if (pl->phydev)
1321                 ret = phy_get_eee_err(pl->phydev);
1322
1323         return ret;
1324 }
1325 EXPORT_SYMBOL_GPL(phylink_get_eee_err);
1326
1327 /**
1328  * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters
1329  * @pl: a pointer to a &struct phylink returned from phylink_create()
1330  * @eee: a pointer to a &struct ethtool_eee for the read parameters
1331  */
1332 int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee)
1333 {
1334         int ret = -EOPNOTSUPP;
1335
1336         ASSERT_RTNL();
1337
1338         if (pl->phydev)
1339                 ret = phy_ethtool_get_eee(pl->phydev, eee);
1340
1341         return ret;
1342 }
1343 EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
1344
1345 /**
1346  * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters
1347  * @pl: a pointer to a &struct phylink returned from phylink_create()
1348  * @eee: a pointer to a &struct ethtool_eee for the desired parameters
1349  */
1350 int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_eee *eee)
1351 {
1352         int ret = -EOPNOTSUPP;
1353
1354         ASSERT_RTNL();
1355
1356         if (pl->phydev)
1357                 ret = phy_ethtool_set_eee(pl->phydev, eee);
1358
1359         return ret;
1360 }
1361 EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
1362
1363 /* This emulates MII registers for a fixed-mode phy operating as per the
1364  * passed in state. "aneg" defines if we report negotiation is possible.
1365  *
1366  * FIXME: should deal with negotiation state too.
1367  */
1368 static int phylink_mii_emul_read(struct net_device *ndev, unsigned int reg,
1369                                  struct phylink_link_state *state, bool aneg)
1370 {
1371         struct fixed_phy_status fs;
1372         int val;
1373
1374         fs.link = state->link;
1375         fs.speed = state->speed;
1376         fs.duplex = state->duplex;
1377         fs.pause = state->pause & MLO_PAUSE_SYM;
1378         fs.asym_pause = state->pause & MLO_PAUSE_ASYM;
1379
1380         val = swphy_read_reg(reg, &fs);
1381         if (reg == MII_BMSR) {
1382                 if (!state->an_complete)
1383                         val &= ~BMSR_ANEGCOMPLETE;
1384                 if (!aneg)
1385                         val &= ~BMSR_ANEGCAPABLE;
1386         }
1387         return val;
1388 }
1389
1390 static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
1391                             unsigned int reg)
1392 {
1393         struct phy_device *phydev = pl->phydev;
1394         int prtad, devad;
1395
1396         if (mdio_phy_id_is_c45(phy_id)) {
1397                 prtad = mdio_phy_id_prtad(phy_id);
1398                 devad = mdio_phy_id_devad(phy_id);
1399                 devad = MII_ADDR_C45 | devad << 16 | reg;
1400         } else if (phydev->is_c45) {
1401                 switch (reg) {
1402                 case MII_BMCR:
1403                 case MII_BMSR:
1404                 case MII_PHYSID1:
1405                 case MII_PHYSID2:
1406                         devad = __ffs(phydev->c45_ids.devices_in_package);
1407                         break;
1408                 case MII_ADVERTISE:
1409                 case MII_LPA:
1410                         if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1411                                 return -EINVAL;
1412                         devad = MDIO_MMD_AN;
1413                         if (reg == MII_ADVERTISE)
1414                                 reg = MDIO_AN_ADVERTISE;
1415                         else
1416                                 reg = MDIO_AN_LPA;
1417                         break;
1418                 default:
1419                         return -EINVAL;
1420                 }
1421                 prtad = phy_id;
1422                 devad = MII_ADDR_C45 | devad << 16 | reg;
1423         } else {
1424                 prtad = phy_id;
1425                 devad = reg;
1426         }
1427         return mdiobus_read(pl->phydev->mdio.bus, prtad, devad);
1428 }
1429
1430 static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
1431                              unsigned int reg, unsigned int val)
1432 {
1433         struct phy_device *phydev = pl->phydev;
1434         int prtad, devad;
1435
1436         if (mdio_phy_id_is_c45(phy_id)) {
1437                 prtad = mdio_phy_id_prtad(phy_id);
1438                 devad = mdio_phy_id_devad(phy_id);
1439                 devad = MII_ADDR_C45 | devad << 16 | reg;
1440         } else if (phydev->is_c45) {
1441                 switch (reg) {
1442                 case MII_BMCR:
1443                 case MII_BMSR:
1444                 case MII_PHYSID1:
1445                 case MII_PHYSID2:
1446                         devad = __ffs(phydev->c45_ids.devices_in_package);
1447                         break;
1448                 case MII_ADVERTISE:
1449                 case MII_LPA:
1450                         if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1451                                 return -EINVAL;
1452                         devad = MDIO_MMD_AN;
1453                         if (reg == MII_ADVERTISE)
1454                                 reg = MDIO_AN_ADVERTISE;
1455                         else
1456                                 reg = MDIO_AN_LPA;
1457                         break;
1458                 default:
1459                         return -EINVAL;
1460                 }
1461                 prtad = phy_id;
1462                 devad = MII_ADDR_C45 | devad << 16 | reg;
1463         } else {
1464                 prtad = phy_id;
1465                 devad = reg;
1466         }
1467
1468         return mdiobus_write(phydev->mdio.bus, prtad, devad, val);
1469 }
1470
1471 static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
1472                             unsigned int reg)
1473 {
1474         struct phylink_link_state state;
1475         int val = 0xffff;
1476
1477         switch (pl->link_an_mode) {
1478         case MLO_AN_FIXED:
1479                 if (phy_id == 0) {
1480                         phylink_get_fixed_state(pl, &state);
1481                         val = phylink_mii_emul_read(pl->netdev, reg, &state,
1482                                                     true);
1483                 }
1484                 break;
1485
1486         case MLO_AN_PHY:
1487                 return -EOPNOTSUPP;
1488
1489         case MLO_AN_INBAND:
1490                 if (phy_id == 0) {
1491                         val = phylink_get_mac_state(pl, &state);
1492                         if (val < 0)
1493                                 return val;
1494
1495                         val = phylink_mii_emul_read(pl->netdev, reg, &state,
1496                                                     true);
1497                 }
1498                 break;
1499         }
1500
1501         return val & 0xffff;
1502 }
1503
1504 static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
1505                              unsigned int reg, unsigned int val)
1506 {
1507         switch (pl->link_an_mode) {
1508         case MLO_AN_FIXED:
1509                 break;
1510
1511         case MLO_AN_PHY:
1512                 return -EOPNOTSUPP;
1513
1514         case MLO_AN_INBAND:
1515                 break;
1516         }
1517
1518         return 0;
1519 }
1520
1521 /**
1522  * phylink_mii_ioctl() - generic mii ioctl interface
1523  * @pl: a pointer to a &struct phylink returned from phylink_create()
1524  * @ifr: a pointer to a &struct ifreq for socket ioctls
1525  * @cmd: ioctl cmd to execute
1526  *
1527  * Perform the specified MII ioctl on the PHY attached to the phylink instance
1528  * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
1529  *
1530  * Returns: zero on success or negative error code.
1531  *
1532  * %SIOCGMIIPHY:
1533  *  read register from the current PHY.
1534  * %SIOCGMIIREG:
1535  *  read register from the specified PHY.
1536  * %SIOCSMIIREG:
1537  *  set a register on the specified PHY.
1538  */
1539 int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
1540 {
1541         struct mii_ioctl_data *mii = if_mii(ifr);
1542         int  ret;
1543
1544         ASSERT_RTNL();
1545
1546         if (pl->phydev) {
1547                 /* PHYs only exist for MLO_AN_PHY and SGMII */
1548                 switch (cmd) {
1549                 case SIOCGMIIPHY:
1550                         mii->phy_id = pl->phydev->mdio.addr;
1551                         /* fall through */
1552
1553                 case SIOCGMIIREG:
1554                         ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
1555                         if (ret >= 0) {
1556                                 mii->val_out = ret;
1557                                 ret = 0;
1558                         }
1559                         break;
1560
1561                 case SIOCSMIIREG:
1562                         ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
1563                                                 mii->val_in);
1564                         break;
1565
1566                 default:
1567                         ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
1568                         break;
1569                 }
1570         } else {
1571                 switch (cmd) {
1572                 case SIOCGMIIPHY:
1573                         mii->phy_id = 0;
1574                         /* fall through */
1575
1576                 case SIOCGMIIREG:
1577                         ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
1578                         if (ret >= 0) {
1579                                 mii->val_out = ret;
1580                                 ret = 0;
1581                         }
1582                         break;
1583
1584                 case SIOCSMIIREG:
1585                         ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
1586                                                 mii->val_in);
1587                         break;
1588
1589                 default:
1590                         ret = -EOPNOTSUPP;
1591                         break;
1592                 }
1593         }
1594
1595         return ret;
1596 }
1597 EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
1598
1599 static int phylink_sfp_module_insert(void *upstream,
1600                                      const struct sfp_eeprom_id *id)
1601 {
1602         struct phylink *pl = upstream;
1603         __ETHTOOL_DECLARE_LINK_MODE_MASK(support) = { 0, };
1604         struct phylink_link_state config;
1605         phy_interface_t iface;
1606         int ret = 0;
1607         bool changed;
1608         u8 port;
1609
1610         ASSERT_RTNL();
1611
1612         sfp_parse_support(pl->sfp_bus, id, support);
1613         port = sfp_parse_port(pl->sfp_bus, id, support);
1614
1615         memset(&config, 0, sizeof(config));
1616         linkmode_copy(config.advertising, support);
1617         config.interface = PHY_INTERFACE_MODE_NA;
1618         config.speed = SPEED_UNKNOWN;
1619         config.duplex = DUPLEX_UNKNOWN;
1620         config.pause = MLO_PAUSE_AN;
1621         config.an_enabled = pl->link_config.an_enabled;
1622
1623         /* Ignore errors if we're expecting a PHY to attach later */
1624         ret = phylink_validate(pl, support, &config);
1625         if (ret) {
1626                 netdev_err(pl->netdev, "validation with support %*pb failed: %d\n",
1627                            __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
1628                 return ret;
1629         }
1630
1631         iface = sfp_select_interface(pl->sfp_bus, id, config.advertising);
1632         if (iface == PHY_INTERFACE_MODE_NA) {
1633                 netdev_err(pl->netdev,
1634                            "selection of interface failed, advertisement %*pb\n",
1635                            __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising);
1636                 return -EINVAL;
1637         }
1638
1639         config.interface = iface;
1640         ret = phylink_validate(pl, support, &config);
1641         if (ret) {
1642                 netdev_err(pl->netdev, "validation of %s/%s with support %*pb failed: %d\n",
1643                            phylink_an_mode_str(MLO_AN_INBAND),
1644                            phy_modes(config.interface),
1645                            __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
1646                 return ret;
1647         }
1648
1649         netdev_dbg(pl->netdev, "requesting link mode %s/%s with support %*pb\n",
1650                    phylink_an_mode_str(MLO_AN_INBAND),
1651                    phy_modes(config.interface),
1652                    __ETHTOOL_LINK_MODE_MASK_NBITS, support);
1653
1654         if (phy_interface_mode_is_8023z(iface) && pl->phydev)
1655                 return -EINVAL;
1656
1657         changed = !bitmap_equal(pl->supported, support,
1658                                 __ETHTOOL_LINK_MODE_MASK_NBITS);
1659         if (changed) {
1660                 linkmode_copy(pl->supported, support);
1661                 linkmode_copy(pl->link_config.advertising, config.advertising);
1662         }
1663
1664         if (pl->link_an_mode != MLO_AN_INBAND ||
1665             pl->link_config.interface != config.interface) {
1666                 pl->link_config.interface = config.interface;
1667                 pl->link_an_mode = MLO_AN_INBAND;
1668
1669                 changed = true;
1670
1671                 netdev_info(pl->netdev, "switched to %s/%s link mode\n",
1672                             phylink_an_mode_str(MLO_AN_INBAND),
1673                             phy_modes(config.interface));
1674         }
1675
1676         pl->link_port = port;
1677
1678         if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
1679                                  &pl->phylink_disable_state))
1680                 phylink_mac_config(pl, &pl->link_config);
1681
1682         return ret;
1683 }
1684
1685 static void phylink_sfp_link_down(void *upstream)
1686 {
1687         struct phylink *pl = upstream;
1688
1689         ASSERT_RTNL();
1690
1691         phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK);
1692 }
1693
1694 static void phylink_sfp_link_up(void *upstream)
1695 {
1696         struct phylink *pl = upstream;
1697
1698         ASSERT_RTNL();
1699
1700         clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
1701         phylink_run_resolve(pl);
1702 }
1703
1704 static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
1705 {
1706         struct phylink *pl = upstream;
1707
1708         return __phylink_connect_phy(upstream, phy, pl->link_config.interface);
1709 }
1710
1711 static void phylink_sfp_disconnect_phy(void *upstream)
1712 {
1713         phylink_disconnect_phy(upstream);
1714 }
1715
1716 static const struct sfp_upstream_ops sfp_phylink_ops = {
1717         .module_insert = phylink_sfp_module_insert,
1718         .link_up = phylink_sfp_link_up,
1719         .link_down = phylink_sfp_link_down,
1720         .connect_phy = phylink_sfp_connect_phy,
1721         .disconnect_phy = phylink_sfp_disconnect_phy,
1722 };
1723
1724 /* Helpers for MAC drivers */
1725
1726 /**
1727  * phylink_helper_basex_speed() - 1000BaseX/2500BaseX helper
1728  * @state: a pointer to a &struct phylink_link_state
1729  *
1730  * Inspect the interface mode, advertising mask or forced speed and
1731  * decide whether to run at 2.5Gbit or 1Gbit appropriately, switching
1732  * the interface mode to suit.  @state->interface is appropriately
1733  * updated, and the advertising mask has the "other" baseX_Full flag
1734  * cleared.
1735  */
1736 void phylink_helper_basex_speed(struct phylink_link_state *state)
1737 {
1738         if (phy_interface_mode_is_8023z(state->interface)) {
1739                 bool want_2500 = state->an_enabled ?
1740                         phylink_test(state->advertising, 2500baseX_Full) :
1741                         state->speed == SPEED_2500;
1742
1743                 if (want_2500) {
1744                         phylink_clear(state->advertising, 1000baseX_Full);
1745                         state->interface = PHY_INTERFACE_MODE_2500BASEX;
1746                 } else {
1747                         phylink_clear(state->advertising, 2500baseX_Full);
1748                         state->interface = PHY_INTERFACE_MODE_1000BASEX;
1749                 }
1750         }
1751 }
1752 EXPORT_SYMBOL_GPL(phylink_helper_basex_speed);
1753
1754 MODULE_LICENSE("GPL");