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