Merge tag 'i3c/for-5.1' of git://git.kernel.org/pub/scm/linux/kernel/git/i3c/linux
[sfrench/cifs-2.6.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         struct gpio_desc *link_gpio;
58         struct timer_list link_poll;
59         void (*get_fixed_state)(struct net_device *dev,
60                                 struct phylink_link_state *s);
61
62         struct mutex state_mutex;
63         struct phylink_link_state phy_state;
64         struct work_struct resolve;
65
66         bool mac_link_dropped;
67
68         struct sfp_bus *sfp_bus;
69 };
70
71 /**
72  * phylink_set_port_modes() - set the port type modes in the ethtool mask
73  * @mask: ethtool link mode mask
74  *
75  * Sets all the port type modes in the ethtool mask.  MAC drivers should
76  * use this in their 'validate' callback.
77  */
78 void phylink_set_port_modes(unsigned long *mask)
79 {
80         phylink_set(mask, TP);
81         phylink_set(mask, AUI);
82         phylink_set(mask, MII);
83         phylink_set(mask, FIBRE);
84         phylink_set(mask, BNC);
85         phylink_set(mask, Backplane);
86 }
87 EXPORT_SYMBOL_GPL(phylink_set_port_modes);
88
89 static int phylink_is_empty_linkmode(const unsigned long *linkmode)
90 {
91         __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp) = { 0, };
92
93         phylink_set_port_modes(tmp);
94         phylink_set(tmp, Autoneg);
95         phylink_set(tmp, Pause);
96         phylink_set(tmp, Asym_Pause);
97
98         bitmap_andnot(tmp, linkmode, tmp, __ETHTOOL_LINK_MODE_MASK_NBITS);
99
100         return linkmode_empty(tmp);
101 }
102
103 static const char *phylink_an_mode_str(unsigned int mode)
104 {
105         static const char *modestr[] = {
106                 [MLO_AN_PHY] = "phy",
107                 [MLO_AN_FIXED] = "fixed",
108                 [MLO_AN_INBAND] = "inband",
109         };
110
111         return mode < ARRAY_SIZE(modestr) ? modestr[mode] : "unknown";
112 }
113
114 static int phylink_validate(struct phylink *pl, unsigned long *supported,
115                             struct phylink_link_state *state)
116 {
117         pl->ops->validate(pl->netdev, supported, state);
118
119         return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
120 }
121
122 static int phylink_parse_fixedlink(struct phylink *pl,
123                                    struct fwnode_handle *fwnode)
124 {
125         struct fwnode_handle *fixed_node;
126         const struct phy_setting *s;
127         struct gpio_desc *desc;
128         u32 speed;
129         int ret;
130
131         fixed_node = fwnode_get_named_child_node(fwnode, "fixed-link");
132         if (fixed_node) {
133                 ret = fwnode_property_read_u32(fixed_node, "speed", &speed);
134
135                 pl->link_config.speed = speed;
136                 pl->link_config.duplex = DUPLEX_HALF;
137
138                 if (fwnode_property_read_bool(fixed_node, "full-duplex"))
139                         pl->link_config.duplex = DUPLEX_FULL;
140
141                 /* We treat the "pause" and "asym-pause" terminology as
142                  * defining the link partner's ability. */
143                 if (fwnode_property_read_bool(fixed_node, "pause"))
144                         pl->link_config.pause |= MLO_PAUSE_SYM;
145                 if (fwnode_property_read_bool(fixed_node, "asym-pause"))
146                         pl->link_config.pause |= MLO_PAUSE_ASYM;
147
148                 if (ret == 0) {
149                         desc = fwnode_get_named_gpiod(fixed_node, "link-gpios",
150                                                       0, GPIOD_IN, "?");
151
152                         if (!IS_ERR(desc))
153                                 pl->link_gpio = desc;
154                         else if (desc == ERR_PTR(-EPROBE_DEFER))
155                                 ret = -EPROBE_DEFER;
156                 }
157                 fwnode_handle_put(fixed_node);
158
159                 if (ret)
160                         return ret;
161         } else {
162                 u32 prop[5];
163
164                 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
165                                                      NULL, 0);
166                 if (ret != ARRAY_SIZE(prop)) {
167                         netdev_err(pl->netdev, "broken fixed-link?\n");
168                         return -EINVAL;
169                 }
170
171                 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
172                                                      prop, ARRAY_SIZE(prop));
173                 if (!ret) {
174                         pl->link_config.duplex = prop[1] ?
175                                                 DUPLEX_FULL : DUPLEX_HALF;
176                         pl->link_config.speed = prop[2];
177                         if (prop[3])
178                                 pl->link_config.pause |= MLO_PAUSE_SYM;
179                         if (prop[4])
180                                 pl->link_config.pause |= MLO_PAUSE_ASYM;
181                 }
182         }
183
184         if (pl->link_config.speed > SPEED_1000 &&
185             pl->link_config.duplex != DUPLEX_FULL)
186                 netdev_warn(pl->netdev, "fixed link specifies half duplex for %dMbps link?\n",
187                             pl->link_config.speed);
188
189         bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
190         linkmode_copy(pl->link_config.advertising, pl->supported);
191         phylink_validate(pl, pl->supported, &pl->link_config);
192
193         s = phy_lookup_setting(pl->link_config.speed, pl->link_config.duplex,
194                                pl->supported, true);
195         linkmode_zero(pl->supported);
196         phylink_set(pl->supported, MII);
197         if (s) {
198                 __set_bit(s->bit, pl->supported);
199         } else {
200                 netdev_warn(pl->netdev, "fixed link %s duplex %dMbps not recognised\n",
201                             pl->link_config.duplex == DUPLEX_FULL ? "full" : "half",
202                             pl->link_config.speed);
203         }
204
205         linkmode_and(pl->link_config.advertising, pl->link_config.advertising,
206                      pl->supported);
207
208         pl->link_config.link = 1;
209         pl->link_config.an_complete = 1;
210
211         return 0;
212 }
213
214 static int phylink_parse_mode(struct phylink *pl, struct fwnode_handle *fwnode)
215 {
216         struct fwnode_handle *dn;
217         const char *managed;
218
219         dn = fwnode_get_named_child_node(fwnode, "fixed-link");
220         if (dn || fwnode_property_present(fwnode, "fixed-link"))
221                 pl->link_an_mode = MLO_AN_FIXED;
222         fwnode_handle_put(dn);
223
224         if (fwnode_property_read_string(fwnode, "managed", &managed) == 0 &&
225             strcmp(managed, "in-band-status") == 0) {
226                 if (pl->link_an_mode == MLO_AN_FIXED) {
227                         netdev_err(pl->netdev,
228                                    "can't use both fixed-link and in-band-status\n");
229                         return -EINVAL;
230                 }
231
232                 linkmode_zero(pl->supported);
233                 phylink_set(pl->supported, MII);
234                 phylink_set(pl->supported, Autoneg);
235                 phylink_set(pl->supported, Asym_Pause);
236                 phylink_set(pl->supported, Pause);
237                 pl->link_config.an_enabled = true;
238                 pl->link_an_mode = MLO_AN_INBAND;
239
240                 switch (pl->link_config.interface) {
241                 case PHY_INTERFACE_MODE_SGMII:
242                         phylink_set(pl->supported, 10baseT_Half);
243                         phylink_set(pl->supported, 10baseT_Full);
244                         phylink_set(pl->supported, 100baseT_Half);
245                         phylink_set(pl->supported, 100baseT_Full);
246                         phylink_set(pl->supported, 1000baseT_Half);
247                         phylink_set(pl->supported, 1000baseT_Full);
248                         break;
249
250                 case PHY_INTERFACE_MODE_1000BASEX:
251                         phylink_set(pl->supported, 1000baseX_Full);
252                         break;
253
254                 case PHY_INTERFACE_MODE_2500BASEX:
255                         phylink_set(pl->supported, 2500baseX_Full);
256                         break;
257
258                 case PHY_INTERFACE_MODE_10GKR:
259                         phylink_set(pl->supported, 10baseT_Half);
260                         phylink_set(pl->supported, 10baseT_Full);
261                         phylink_set(pl->supported, 100baseT_Half);
262                         phylink_set(pl->supported, 100baseT_Full);
263                         phylink_set(pl->supported, 1000baseT_Half);
264                         phylink_set(pl->supported, 1000baseT_Full);
265                         phylink_set(pl->supported, 1000baseX_Full);
266                         phylink_set(pl->supported, 10000baseKR_Full);
267                         phylink_set(pl->supported, 10000baseCR_Full);
268                         phylink_set(pl->supported, 10000baseSR_Full);
269                         phylink_set(pl->supported, 10000baseLR_Full);
270                         phylink_set(pl->supported, 10000baseLRM_Full);
271                         phylink_set(pl->supported, 10000baseER_Full);
272                         break;
273
274                 default:
275                         netdev_err(pl->netdev,
276                                    "incorrect link mode %s for in-band status\n",
277                                    phy_modes(pl->link_config.interface));
278                         return -EINVAL;
279                 }
280
281                 linkmode_copy(pl->link_config.advertising, pl->supported);
282
283                 if (phylink_validate(pl, pl->supported, &pl->link_config)) {
284                         netdev_err(pl->netdev,
285                                    "failed to validate link configuration for in-band status\n");
286                         return -EINVAL;
287                 }
288         }
289
290         return 0;
291 }
292
293 static void phylink_mac_config(struct phylink *pl,
294                                const struct phylink_link_state *state)
295 {
296         netdev_dbg(pl->netdev,
297                    "%s: mode=%s/%s/%s/%s adv=%*pb pause=%02x link=%u an=%u\n",
298                    __func__, phylink_an_mode_str(pl->link_an_mode),
299                    phy_modes(state->interface),
300                    phy_speed_to_str(state->speed),
301                    phy_duplex_to_str(state->duplex),
302                    __ETHTOOL_LINK_MODE_MASK_NBITS, state->advertising,
303                    state->pause, state->link, state->an_enabled);
304
305         pl->ops->mac_config(pl->netdev, pl->link_an_mode, state);
306 }
307
308 static void phylink_mac_an_restart(struct phylink *pl)
309 {
310         if (pl->link_config.an_enabled &&
311             phy_interface_mode_is_8023z(pl->link_config.interface))
312                 pl->ops->mac_an_restart(pl->netdev);
313 }
314
315 static int phylink_get_mac_state(struct phylink *pl, struct phylink_link_state *state)
316 {
317         struct net_device *ndev = pl->netdev;
318
319         linkmode_copy(state->advertising, pl->link_config.advertising);
320         linkmode_zero(state->lp_advertising);
321         state->interface = pl->link_config.interface;
322         state->an_enabled = pl->link_config.an_enabled;
323         state->speed = SPEED_UNKNOWN;
324         state->duplex = DUPLEX_UNKNOWN;
325         state->pause = MLO_PAUSE_NONE;
326         state->an_complete = 0;
327         state->link = 1;
328
329         return pl->ops->mac_link_state(ndev, state);
330 }
331
332 /* The fixed state is... fixed except for the link state,
333  * which may be determined by a GPIO or a callback.
334  */
335 static void phylink_get_fixed_state(struct phylink *pl, struct phylink_link_state *state)
336 {
337         *state = pl->link_config;
338         if (pl->get_fixed_state)
339                 pl->get_fixed_state(pl->netdev, state);
340         else if (pl->link_gpio)
341                 state->link = !!gpiod_get_value_cansleep(pl->link_gpio);
342 }
343
344 /* Flow control is resolved according to our and the link partners
345  * advertisements using the following drawn from the 802.3 specs:
346  *  Local device  Link partner
347  *  Pause AsymDir Pause AsymDir Result
348  *    1     X       1     X     TX+RX
349  *    0     1       1     1     RX
350  *    1     1       0     1     TX
351  */
352 static void phylink_resolve_flow(struct phylink *pl,
353                                  struct phylink_link_state *state)
354 {
355         int new_pause = 0;
356
357         if (pl->link_config.pause & MLO_PAUSE_AN) {
358                 int pause = 0;
359
360                 if (phylink_test(pl->link_config.advertising, Pause))
361                         pause |= MLO_PAUSE_SYM;
362                 if (phylink_test(pl->link_config.advertising, Asym_Pause))
363                         pause |= MLO_PAUSE_ASYM;
364
365                 pause &= state->pause;
366
367                 if (pause & MLO_PAUSE_SYM)
368                         new_pause = MLO_PAUSE_TX | MLO_PAUSE_RX;
369                 else if (pause & MLO_PAUSE_ASYM)
370                         new_pause = state->pause & MLO_PAUSE_SYM ?
371                                  MLO_PAUSE_RX : MLO_PAUSE_TX;
372         } else {
373                 new_pause = pl->link_config.pause & MLO_PAUSE_TXRX_MASK;
374         }
375
376         state->pause &= ~MLO_PAUSE_TXRX_MASK;
377         state->pause |= new_pause;
378 }
379
380 static const char *phylink_pause_to_str(int pause)
381 {
382         switch (pause & MLO_PAUSE_TXRX_MASK) {
383         case MLO_PAUSE_TX | MLO_PAUSE_RX:
384                 return "rx/tx";
385         case MLO_PAUSE_TX:
386                 return "tx";
387         case MLO_PAUSE_RX:
388                 return "rx";
389         default:
390                 return "off";
391         }
392 }
393
394 static void phylink_resolve(struct work_struct *w)
395 {
396         struct phylink *pl = container_of(w, struct phylink, resolve);
397         struct phylink_link_state link_state;
398         struct net_device *ndev = pl->netdev;
399
400         mutex_lock(&pl->state_mutex);
401         if (pl->phylink_disable_state) {
402                 pl->mac_link_dropped = false;
403                 link_state.link = false;
404         } else if (pl->mac_link_dropped) {
405                 link_state.link = false;
406         } else {
407                 switch (pl->link_an_mode) {
408                 case MLO_AN_PHY:
409                         link_state = pl->phy_state;
410                         phylink_resolve_flow(pl, &link_state);
411                         phylink_mac_config(pl, &link_state);
412                         break;
413
414                 case MLO_AN_FIXED:
415                         phylink_get_fixed_state(pl, &link_state);
416                         phylink_mac_config(pl, &link_state);
417                         break;
418
419                 case MLO_AN_INBAND:
420                         phylink_get_mac_state(pl, &link_state);
421                         if (pl->phydev) {
422                                 bool changed = false;
423
424                                 link_state.link = link_state.link &&
425                                                   pl->phy_state.link;
426
427                                 if (pl->phy_state.interface !=
428                                     link_state.interface) {
429                                         link_state.interface = pl->phy_state.interface;
430                                         changed = true;
431                                 }
432
433                                 /* Propagate the flow control from the PHY
434                                  * to the MAC. Also propagate the interface
435                                  * if changed.
436                                  */
437                                 if (pl->phy_state.link || changed) {
438                                         link_state.pause |= pl->phy_state.pause;
439                                         phylink_resolve_flow(pl, &link_state);
440
441                                         phylink_mac_config(pl, &link_state);
442                                 }
443                         }
444                         break;
445                 }
446         }
447
448         if (link_state.link != netif_carrier_ok(ndev)) {
449                 if (!link_state.link) {
450                         netif_carrier_off(ndev);
451                         pl->ops->mac_link_down(ndev, pl->link_an_mode,
452                                                pl->phy_state.interface);
453                         netdev_info(ndev, "Link is Down\n");
454                 } else {
455                         pl->ops->mac_link_up(ndev, pl->link_an_mode,
456                                              pl->phy_state.interface,
457                                              pl->phydev);
458
459                         netif_carrier_on(ndev);
460
461                         netdev_info(ndev,
462                                     "Link is Up - %s/%s - flow control %s\n",
463                                     phy_speed_to_str(link_state.speed),
464                                     phy_duplex_to_str(link_state.duplex),
465                                     phylink_pause_to_str(link_state.pause));
466                 }
467         }
468         if (!link_state.link && pl->mac_link_dropped) {
469                 pl->mac_link_dropped = false;
470                 queue_work(system_power_efficient_wq, &pl->resolve);
471         }
472         mutex_unlock(&pl->state_mutex);
473 }
474
475 static void phylink_run_resolve(struct phylink *pl)
476 {
477         if (!pl->phylink_disable_state)
478                 queue_work(system_power_efficient_wq, &pl->resolve);
479 }
480
481 static void phylink_run_resolve_and_disable(struct phylink *pl, int bit)
482 {
483         unsigned long state = pl->phylink_disable_state;
484
485         set_bit(bit, &pl->phylink_disable_state);
486         if (state == 0) {
487                 queue_work(system_power_efficient_wq, &pl->resolve);
488                 flush_work(&pl->resolve);
489         }
490 }
491
492 static void phylink_fixed_poll(struct timer_list *t)
493 {
494         struct phylink *pl = container_of(t, struct phylink, link_poll);
495
496         mod_timer(t, jiffies + HZ);
497
498         phylink_run_resolve(pl);
499 }
500
501 static const struct sfp_upstream_ops sfp_phylink_ops;
502
503 static int phylink_register_sfp(struct phylink *pl,
504                                 struct fwnode_handle *fwnode)
505 {
506         struct fwnode_reference_args ref;
507         int ret;
508
509         if (!fwnode)
510                 return 0;
511
512         ret = fwnode_property_get_reference_args(fwnode, "sfp", NULL,
513                                                  0, 0, &ref);
514         if (ret < 0) {
515                 if (ret == -ENOENT)
516                         return 0;
517
518                 netdev_err(pl->netdev, "unable to parse \"sfp\" node: %d\n",
519                            ret);
520                 return ret;
521         }
522
523         pl->sfp_bus = sfp_register_upstream(ref.fwnode, pl->netdev, pl,
524                                             &sfp_phylink_ops);
525         if (!pl->sfp_bus)
526                 return -ENOMEM;
527
528         return 0;
529 }
530
531 /**
532  * phylink_create() - create a phylink instance
533  * @ndev: a pointer to the &struct net_device
534  * @fwnode: a pointer to a &struct fwnode_handle describing the network
535  *      interface
536  * @iface: the desired link mode defined by &typedef phy_interface_t
537  * @ops: a pointer to a &struct phylink_mac_ops for the MAC.
538  *
539  * Create a new phylink instance, and parse the link parameters found in @np.
540  * This will parse in-band modes, fixed-link or SFP configuration.
541  *
542  * Returns a pointer to a &struct phylink, or an error-pointer value. Users
543  * must use IS_ERR() to check for errors from this function.
544  */
545 struct phylink *phylink_create(struct net_device *ndev,
546                                struct fwnode_handle *fwnode,
547                                phy_interface_t iface,
548                                const struct phylink_mac_ops *ops)
549 {
550         struct phylink *pl;
551         int ret;
552
553         pl = kzalloc(sizeof(*pl), GFP_KERNEL);
554         if (!pl)
555                 return ERR_PTR(-ENOMEM);
556
557         mutex_init(&pl->state_mutex);
558         INIT_WORK(&pl->resolve, phylink_resolve);
559         pl->netdev = ndev;
560         pl->phy_state.interface = iface;
561         pl->link_interface = iface;
562         if (iface == PHY_INTERFACE_MODE_MOCA)
563                 pl->link_port = PORT_BNC;
564         else
565                 pl->link_port = PORT_MII;
566         pl->link_config.interface = iface;
567         pl->link_config.pause = MLO_PAUSE_AN;
568         pl->link_config.speed = SPEED_UNKNOWN;
569         pl->link_config.duplex = DUPLEX_UNKNOWN;
570         pl->link_config.an_enabled = true;
571         pl->ops = ops;
572         __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
573         timer_setup(&pl->link_poll, phylink_fixed_poll, 0);
574
575         bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
576         linkmode_copy(pl->link_config.advertising, pl->supported);
577         phylink_validate(pl, pl->supported, &pl->link_config);
578
579         ret = phylink_parse_mode(pl, fwnode);
580         if (ret < 0) {
581                 kfree(pl);
582                 return ERR_PTR(ret);
583         }
584
585         if (pl->link_an_mode == MLO_AN_FIXED) {
586                 ret = phylink_parse_fixedlink(pl, fwnode);
587                 if (ret < 0) {
588                         kfree(pl);
589                         return ERR_PTR(ret);
590                 }
591         }
592
593         ret = phylink_register_sfp(pl, fwnode);
594         if (ret < 0) {
595                 kfree(pl);
596                 return ERR_PTR(ret);
597         }
598
599         return pl;
600 }
601 EXPORT_SYMBOL_GPL(phylink_create);
602
603 /**
604  * phylink_destroy() - cleanup and destroy the phylink instance
605  * @pl: a pointer to a &struct phylink returned from phylink_create()
606  *
607  * Destroy a phylink instance. Any PHY that has been attached must have been
608  * cleaned up via phylink_disconnect_phy() prior to calling this function.
609  */
610 void phylink_destroy(struct phylink *pl)
611 {
612         if (pl->sfp_bus)
613                 sfp_unregister_upstream(pl->sfp_bus);
614         if (!IS_ERR_OR_NULL(pl->link_gpio))
615                 gpiod_put(pl->link_gpio);
616
617         cancel_work_sync(&pl->resolve);
618         kfree(pl);
619 }
620 EXPORT_SYMBOL_GPL(phylink_destroy);
621
622 static void phylink_phy_change(struct phy_device *phydev, bool up,
623                                bool do_carrier)
624 {
625         struct phylink *pl = phydev->phylink;
626
627         mutex_lock(&pl->state_mutex);
628         pl->phy_state.speed = phydev->speed;
629         pl->phy_state.duplex = phydev->duplex;
630         pl->phy_state.pause = MLO_PAUSE_NONE;
631         if (phydev->pause)
632                 pl->phy_state.pause |= MLO_PAUSE_SYM;
633         if (phydev->asym_pause)
634                 pl->phy_state.pause |= MLO_PAUSE_ASYM;
635         pl->phy_state.interface = phydev->interface;
636         pl->phy_state.link = up;
637         mutex_unlock(&pl->state_mutex);
638
639         phylink_run_resolve(pl);
640
641         netdev_dbg(pl->netdev, "phy link %s %s/%s/%s\n", up ? "up" : "down",
642                    phy_modes(phydev->interface),
643                    phy_speed_to_str(phydev->speed),
644                    phy_duplex_to_str(phydev->duplex));
645 }
646
647 static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy)
648 {
649         struct phylink_link_state config;
650         __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
651         int ret;
652
653         memset(&config, 0, sizeof(config));
654         linkmode_copy(supported, phy->supported);
655         linkmode_copy(config.advertising, phy->advertising);
656         config.interface = pl->link_config.interface;
657
658         /*
659          * This is the new way of dealing with flow control for PHYs,
660          * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
661          * phy drivers should not set SUPPORTED_[Asym_]Pause") except
662          * using our validate call to the MAC, we rely upon the MAC
663          * clearing the bits from both supported and advertising fields.
664          */
665         if (phylink_test(supported, Pause))
666                 phylink_set(config.advertising, Pause);
667         if (phylink_test(supported, Asym_Pause))
668                 phylink_set(config.advertising, Asym_Pause);
669
670         ret = phylink_validate(pl, supported, &config);
671         if (ret)
672                 return ret;
673
674         phy->phylink = pl;
675         phy->phy_link_change = phylink_phy_change;
676
677         netdev_info(pl->netdev,
678                     "PHY [%s] driver [%s]\n", dev_name(&phy->mdio.dev),
679                     phy->drv->name);
680
681         mutex_lock(&phy->lock);
682         mutex_lock(&pl->state_mutex);
683         pl->phydev = phy;
684         linkmode_copy(pl->supported, supported);
685         linkmode_copy(pl->link_config.advertising, config.advertising);
686
687         /* Restrict the phy advertisement according to the MAC support. */
688         linkmode_copy(phy->advertising, config.advertising);
689         mutex_unlock(&pl->state_mutex);
690         mutex_unlock(&phy->lock);
691
692         netdev_dbg(pl->netdev,
693                    "phy: setting supported %*pb advertising %*pb\n",
694                    __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
695                    __ETHTOOL_LINK_MODE_MASK_NBITS, phy->advertising);
696
697         phy_start_machine(phy);
698         if (phy->irq > 0)
699                 phy_start_interrupts(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         struct ethtool_link_ksettings our_kset;
1077         struct phylink_link_state config;
1078         int ret;
1079
1080         ASSERT_RTNL();
1081
1082         if (kset->base.autoneg != AUTONEG_DISABLE &&
1083             kset->base.autoneg != AUTONEG_ENABLE)
1084                 return -EINVAL;
1085
1086         config = pl->link_config;
1087
1088         /* Mask out unsupported advertisements */
1089         linkmode_and(config.advertising, kset->link_modes.advertising,
1090                      pl->supported);
1091
1092         /* FIXME: should we reject autoneg if phy/mac does not support it? */
1093         if (kset->base.autoneg == AUTONEG_DISABLE) {
1094                 const struct phy_setting *s;
1095
1096                 /* Autonegotiation disabled, select a suitable speed and
1097                  * duplex.
1098                  */
1099                 s = phy_lookup_setting(kset->base.speed, kset->base.duplex,
1100                                        pl->supported, false);
1101                 if (!s)
1102                         return -EINVAL;
1103
1104                 /* If we have a fixed link (as specified by firmware), refuse
1105                  * to change link parameters.
1106                  */
1107                 if (pl->link_an_mode == MLO_AN_FIXED &&
1108                     (s->speed != pl->link_config.speed ||
1109                      s->duplex != pl->link_config.duplex))
1110                         return -EINVAL;
1111
1112                 config.speed = s->speed;
1113                 config.duplex = s->duplex;
1114                 config.an_enabled = false;
1115
1116                 __clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
1117         } else {
1118                 /* If we have a fixed link, refuse to enable autonegotiation */
1119                 if (pl->link_an_mode == MLO_AN_FIXED)
1120                         return -EINVAL;
1121
1122                 config.speed = SPEED_UNKNOWN;
1123                 config.duplex = DUPLEX_UNKNOWN;
1124                 config.an_enabled = true;
1125
1126                 __set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
1127         }
1128
1129         if (phylink_validate(pl, pl->supported, &config))
1130                 return -EINVAL;
1131
1132         /* If autonegotiation is enabled, we must have an advertisement */
1133         if (config.an_enabled && phylink_is_empty_linkmode(config.advertising))
1134                 return -EINVAL;
1135
1136         our_kset = *kset;
1137         linkmode_copy(our_kset.link_modes.advertising, config.advertising);
1138         our_kset.base.speed = config.speed;
1139         our_kset.base.duplex = config.duplex;
1140
1141         /* If we have a PHY, configure the phy */
1142         if (pl->phydev) {
1143                 ret = phy_ethtool_ksettings_set(pl->phydev, &our_kset);
1144                 if (ret)
1145                         return ret;
1146         }
1147
1148         mutex_lock(&pl->state_mutex);
1149         /* Configure the MAC to match the new settings */
1150         linkmode_copy(pl->link_config.advertising, our_kset.link_modes.advertising);
1151         pl->link_config.interface = config.interface;
1152         pl->link_config.speed = our_kset.base.speed;
1153         pl->link_config.duplex = our_kset.base.duplex;
1154         pl->link_config.an_enabled = our_kset.base.autoneg != AUTONEG_DISABLE;
1155
1156         if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
1157                 phylink_mac_config(pl, &pl->link_config);
1158                 phylink_mac_an_restart(pl);
1159         }
1160         mutex_unlock(&pl->state_mutex);
1161
1162         return 0;
1163 }
1164 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
1165
1166 /**
1167  * phylink_ethtool_nway_reset() - restart negotiation
1168  * @pl: a pointer to a &struct phylink returned from phylink_create()
1169  *
1170  * Restart negotiation for the phylink instance specified by @pl. This will
1171  * cause any attached phy to restart negotiation with the link partner, and
1172  * if the MAC is in a BaseX mode, the MAC will also be requested to restart
1173  * negotiation.
1174  *
1175  * Returns zero on success, or negative error code.
1176  */
1177 int phylink_ethtool_nway_reset(struct phylink *pl)
1178 {
1179         int ret = 0;
1180
1181         ASSERT_RTNL();
1182
1183         if (pl->phydev)
1184                 ret = phy_restart_aneg(pl->phydev);
1185         phylink_mac_an_restart(pl);
1186
1187         return ret;
1188 }
1189 EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
1190
1191 /**
1192  * phylink_ethtool_get_pauseparam() - get the current pause parameters
1193  * @pl: a pointer to a &struct phylink returned from phylink_create()
1194  * @pause: a pointer to a &struct ethtool_pauseparam
1195  */
1196 void phylink_ethtool_get_pauseparam(struct phylink *pl,
1197                                     struct ethtool_pauseparam *pause)
1198 {
1199         ASSERT_RTNL();
1200
1201         pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
1202         pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
1203         pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
1204 }
1205 EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
1206
1207 /**
1208  * phylink_ethtool_set_pauseparam() - set the current pause parameters
1209  * @pl: a pointer to a &struct phylink returned from phylink_create()
1210  * @pause: a pointer to a &struct ethtool_pauseparam
1211  */
1212 int phylink_ethtool_set_pauseparam(struct phylink *pl,
1213                                    struct ethtool_pauseparam *pause)
1214 {
1215         struct phylink_link_state *config = &pl->link_config;
1216
1217         ASSERT_RTNL();
1218
1219         if (!phylink_test(pl->supported, Pause) &&
1220             !phylink_test(pl->supported, Asym_Pause))
1221                 return -EOPNOTSUPP;
1222
1223         if (!phylink_test(pl->supported, Asym_Pause) &&
1224             !pause->autoneg && pause->rx_pause != pause->tx_pause)
1225                 return -EINVAL;
1226
1227         config->pause &= ~(MLO_PAUSE_AN | MLO_PAUSE_TXRX_MASK);
1228
1229         if (pause->autoneg)
1230                 config->pause |= MLO_PAUSE_AN;
1231         if (pause->rx_pause)
1232                 config->pause |= MLO_PAUSE_RX;
1233         if (pause->tx_pause)
1234                 config->pause |= MLO_PAUSE_TX;
1235
1236         if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
1237                 switch (pl->link_an_mode) {
1238                 case MLO_AN_PHY:
1239                         /* Silently mark the carrier down, and then trigger a resolve */
1240                         netif_carrier_off(pl->netdev);
1241                         phylink_run_resolve(pl);
1242                         break;
1243
1244                 case MLO_AN_FIXED:
1245                         /* Should we allow fixed links to change against the config? */
1246                         phylink_resolve_flow(pl, config);
1247                         phylink_mac_config(pl, config);
1248                         break;
1249
1250                 case MLO_AN_INBAND:
1251                         phylink_mac_config(pl, config);
1252                         phylink_mac_an_restart(pl);
1253                         break;
1254                 }
1255         }
1256
1257         return 0;
1258 }
1259 EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
1260
1261 /**
1262  * phylink_ethtool_get_eee_err() - read the energy efficient ethernet error
1263  *   counter
1264  * @pl: a pointer to a &struct phylink returned from phylink_create().
1265  *
1266  * Read the Energy Efficient Ethernet error counter from the PHY associated
1267  * with the phylink instance specified by @pl.
1268  *
1269  * Returns positive error counter value, or negative error code.
1270  */
1271 int phylink_get_eee_err(struct phylink *pl)
1272 {
1273         int ret = 0;
1274
1275         ASSERT_RTNL();
1276
1277         if (pl->phydev)
1278                 ret = phy_get_eee_err(pl->phydev);
1279
1280         return ret;
1281 }
1282 EXPORT_SYMBOL_GPL(phylink_get_eee_err);
1283
1284 /**
1285  * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters
1286  * @pl: a pointer to a &struct phylink returned from phylink_create()
1287  * @eee: a pointer to a &struct ethtool_eee for the read parameters
1288  */
1289 int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee)
1290 {
1291         int ret = -EOPNOTSUPP;
1292
1293         ASSERT_RTNL();
1294
1295         if (pl->phydev)
1296                 ret = phy_ethtool_get_eee(pl->phydev, eee);
1297
1298         return ret;
1299 }
1300 EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
1301
1302 /**
1303  * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters
1304  * @pl: a pointer to a &struct phylink returned from phylink_create()
1305  * @eee: a pointer to a &struct ethtool_eee for the desired parameters
1306  */
1307 int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_eee *eee)
1308 {
1309         int ret = -EOPNOTSUPP;
1310
1311         ASSERT_RTNL();
1312
1313         if (pl->phydev)
1314                 ret = phy_ethtool_set_eee(pl->phydev, eee);
1315
1316         return ret;
1317 }
1318 EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
1319
1320 /* This emulates MII registers for a fixed-mode phy operating as per the
1321  * passed in state. "aneg" defines if we report negotiation is possible.
1322  *
1323  * FIXME: should deal with negotiation state too.
1324  */
1325 static int phylink_mii_emul_read(struct net_device *ndev, unsigned int reg,
1326                                  struct phylink_link_state *state, bool aneg)
1327 {
1328         struct fixed_phy_status fs;
1329         int val;
1330
1331         fs.link = state->link;
1332         fs.speed = state->speed;
1333         fs.duplex = state->duplex;
1334         fs.pause = state->pause & MLO_PAUSE_SYM;
1335         fs.asym_pause = state->pause & MLO_PAUSE_ASYM;
1336
1337         val = swphy_read_reg(reg, &fs);
1338         if (reg == MII_BMSR) {
1339                 if (!state->an_complete)
1340                         val &= ~BMSR_ANEGCOMPLETE;
1341                 if (!aneg)
1342                         val &= ~BMSR_ANEGCAPABLE;
1343         }
1344         return val;
1345 }
1346
1347 static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
1348                             unsigned int reg)
1349 {
1350         struct phy_device *phydev = pl->phydev;
1351         int prtad, devad;
1352
1353         if (mdio_phy_id_is_c45(phy_id)) {
1354                 prtad = mdio_phy_id_prtad(phy_id);
1355                 devad = mdio_phy_id_devad(phy_id);
1356                 devad = MII_ADDR_C45 | devad << 16 | reg;
1357         } else if (phydev->is_c45) {
1358                 switch (reg) {
1359                 case MII_BMCR:
1360                 case MII_BMSR:
1361                 case MII_PHYSID1:
1362                 case MII_PHYSID2:
1363                         devad = __ffs(phydev->c45_ids.devices_in_package);
1364                         break;
1365                 case MII_ADVERTISE:
1366                 case MII_LPA:
1367                         if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1368                                 return -EINVAL;
1369                         devad = MDIO_MMD_AN;
1370                         if (reg == MII_ADVERTISE)
1371                                 reg = MDIO_AN_ADVERTISE;
1372                         else
1373                                 reg = MDIO_AN_LPA;
1374                         break;
1375                 default:
1376                         return -EINVAL;
1377                 }
1378                 prtad = phy_id;
1379                 devad = MII_ADDR_C45 | devad << 16 | reg;
1380         } else {
1381                 prtad = phy_id;
1382                 devad = reg;
1383         }
1384         return mdiobus_read(pl->phydev->mdio.bus, prtad, devad);
1385 }
1386
1387 static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
1388                              unsigned int reg, unsigned int val)
1389 {
1390         struct phy_device *phydev = pl->phydev;
1391         int prtad, devad;
1392
1393         if (mdio_phy_id_is_c45(phy_id)) {
1394                 prtad = mdio_phy_id_prtad(phy_id);
1395                 devad = mdio_phy_id_devad(phy_id);
1396                 devad = MII_ADDR_C45 | devad << 16 | reg;
1397         } else if (phydev->is_c45) {
1398                 switch (reg) {
1399                 case MII_BMCR:
1400                 case MII_BMSR:
1401                 case MII_PHYSID1:
1402                 case MII_PHYSID2:
1403                         devad = __ffs(phydev->c45_ids.devices_in_package);
1404                         break;
1405                 case MII_ADVERTISE:
1406                 case MII_LPA:
1407                         if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1408                                 return -EINVAL;
1409                         devad = MDIO_MMD_AN;
1410                         if (reg == MII_ADVERTISE)
1411                                 reg = MDIO_AN_ADVERTISE;
1412                         else
1413                                 reg = MDIO_AN_LPA;
1414                         break;
1415                 default:
1416                         return -EINVAL;
1417                 }
1418                 prtad = phy_id;
1419                 devad = MII_ADDR_C45 | devad << 16 | reg;
1420         } else {
1421                 prtad = phy_id;
1422                 devad = reg;
1423         }
1424
1425         return mdiobus_write(phydev->mdio.bus, prtad, devad, val);
1426 }
1427
1428 static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
1429                             unsigned int reg)
1430 {
1431         struct phylink_link_state state;
1432         int val = 0xffff;
1433
1434         switch (pl->link_an_mode) {
1435         case MLO_AN_FIXED:
1436                 if (phy_id == 0) {
1437                         phylink_get_fixed_state(pl, &state);
1438                         val = phylink_mii_emul_read(pl->netdev, reg, &state,
1439                                                     true);
1440                 }
1441                 break;
1442
1443         case MLO_AN_PHY:
1444                 return -EOPNOTSUPP;
1445
1446         case MLO_AN_INBAND:
1447                 if (phy_id == 0) {
1448                         val = phylink_get_mac_state(pl, &state);
1449                         if (val < 0)
1450                                 return val;
1451
1452                         val = phylink_mii_emul_read(pl->netdev, reg, &state,
1453                                                     true);
1454                 }
1455                 break;
1456         }
1457
1458         return val & 0xffff;
1459 }
1460
1461 static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
1462                              unsigned int reg, unsigned int val)
1463 {
1464         switch (pl->link_an_mode) {
1465         case MLO_AN_FIXED:
1466                 break;
1467
1468         case MLO_AN_PHY:
1469                 return -EOPNOTSUPP;
1470
1471         case MLO_AN_INBAND:
1472                 break;
1473         }
1474
1475         return 0;
1476 }
1477
1478 /**
1479  * phylink_mii_ioctl() - generic mii ioctl interface
1480  * @pl: a pointer to a &struct phylink returned from phylink_create()
1481  * @ifr: a pointer to a &struct ifreq for socket ioctls
1482  * @cmd: ioctl cmd to execute
1483  *
1484  * Perform the specified MII ioctl on the PHY attached to the phylink instance
1485  * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
1486  *
1487  * Returns: zero on success or negative error code.
1488  *
1489  * %SIOCGMIIPHY:
1490  *  read register from the current PHY.
1491  * %SIOCGMIIREG:
1492  *  read register from the specified PHY.
1493  * %SIOCSMIIREG:
1494  *  set a register on the specified PHY.
1495  */
1496 int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
1497 {
1498         struct mii_ioctl_data *mii = if_mii(ifr);
1499         int  ret;
1500
1501         ASSERT_RTNL();
1502
1503         if (pl->phydev) {
1504                 /* PHYs only exist for MLO_AN_PHY and SGMII */
1505                 switch (cmd) {
1506                 case SIOCGMIIPHY:
1507                         mii->phy_id = pl->phydev->mdio.addr;
1508                         /* fall through */
1509
1510                 case SIOCGMIIREG:
1511                         ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
1512                         if (ret >= 0) {
1513                                 mii->val_out = ret;
1514                                 ret = 0;
1515                         }
1516                         break;
1517
1518                 case SIOCSMIIREG:
1519                         ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
1520                                                 mii->val_in);
1521                         break;
1522
1523                 default:
1524                         ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
1525                         break;
1526                 }
1527         } else {
1528                 switch (cmd) {
1529                 case SIOCGMIIPHY:
1530                         mii->phy_id = 0;
1531                         /* fall through */
1532
1533                 case SIOCGMIIREG:
1534                         ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
1535                         if (ret >= 0) {
1536                                 mii->val_out = ret;
1537                                 ret = 0;
1538                         }
1539                         break;
1540
1541                 case SIOCSMIIREG:
1542                         ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
1543                                                 mii->val_in);
1544                         break;
1545
1546                 default:
1547                         ret = -EOPNOTSUPP;
1548                         break;
1549                 }
1550         }
1551
1552         return ret;
1553 }
1554 EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
1555
1556 static int phylink_sfp_module_insert(void *upstream,
1557                                      const struct sfp_eeprom_id *id)
1558 {
1559         struct phylink *pl = upstream;
1560         __ETHTOOL_DECLARE_LINK_MODE_MASK(support) = { 0, };
1561         struct phylink_link_state config;
1562         phy_interface_t iface;
1563         int ret = 0;
1564         bool changed;
1565         u8 port;
1566
1567         ASSERT_RTNL();
1568
1569         sfp_parse_support(pl->sfp_bus, id, support);
1570         port = sfp_parse_port(pl->sfp_bus, id, support);
1571
1572         memset(&config, 0, sizeof(config));
1573         linkmode_copy(config.advertising, support);
1574         config.interface = PHY_INTERFACE_MODE_NA;
1575         config.speed = SPEED_UNKNOWN;
1576         config.duplex = DUPLEX_UNKNOWN;
1577         config.pause = MLO_PAUSE_AN;
1578         config.an_enabled = pl->link_config.an_enabled;
1579
1580         /* Ignore errors if we're expecting a PHY to attach later */
1581         ret = phylink_validate(pl, support, &config);
1582         if (ret) {
1583                 netdev_err(pl->netdev, "validation with support %*pb failed: %d\n",
1584                            __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
1585                 return ret;
1586         }
1587
1588         iface = sfp_select_interface(pl->sfp_bus, id, config.advertising);
1589         if (iface == PHY_INTERFACE_MODE_NA) {
1590                 netdev_err(pl->netdev,
1591                            "selection of interface failed, advertisement %*pb\n",
1592                            __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising);
1593                 return -EINVAL;
1594         }
1595
1596         config.interface = iface;
1597         ret = phylink_validate(pl, support, &config);
1598         if (ret) {
1599                 netdev_err(pl->netdev, "validation of %s/%s with support %*pb failed: %d\n",
1600                            phylink_an_mode_str(MLO_AN_INBAND),
1601                            phy_modes(config.interface),
1602                            __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
1603                 return ret;
1604         }
1605
1606         netdev_dbg(pl->netdev, "requesting link mode %s/%s with support %*pb\n",
1607                    phylink_an_mode_str(MLO_AN_INBAND),
1608                    phy_modes(config.interface),
1609                    __ETHTOOL_LINK_MODE_MASK_NBITS, support);
1610
1611         if (phy_interface_mode_is_8023z(iface) && pl->phydev)
1612                 return -EINVAL;
1613
1614         changed = !bitmap_equal(pl->supported, support,
1615                                 __ETHTOOL_LINK_MODE_MASK_NBITS);
1616         if (changed) {
1617                 linkmode_copy(pl->supported, support);
1618                 linkmode_copy(pl->link_config.advertising, config.advertising);
1619         }
1620
1621         if (pl->link_an_mode != MLO_AN_INBAND ||
1622             pl->link_config.interface != config.interface) {
1623                 pl->link_config.interface = config.interface;
1624                 pl->link_an_mode = MLO_AN_INBAND;
1625
1626                 changed = true;
1627
1628                 netdev_info(pl->netdev, "switched to %s/%s link mode\n",
1629                             phylink_an_mode_str(MLO_AN_INBAND),
1630                             phy_modes(config.interface));
1631         }
1632
1633         pl->link_port = port;
1634
1635         if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
1636                                  &pl->phylink_disable_state))
1637                 phylink_mac_config(pl, &pl->link_config);
1638
1639         return ret;
1640 }
1641
1642 static void phylink_sfp_link_down(void *upstream)
1643 {
1644         struct phylink *pl = upstream;
1645
1646         ASSERT_RTNL();
1647
1648         phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK);
1649 }
1650
1651 static void phylink_sfp_link_up(void *upstream)
1652 {
1653         struct phylink *pl = upstream;
1654
1655         ASSERT_RTNL();
1656
1657         clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
1658         phylink_run_resolve(pl);
1659 }
1660
1661 static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
1662 {
1663         struct phylink *pl = upstream;
1664
1665         return __phylink_connect_phy(upstream, phy, pl->link_config.interface);
1666 }
1667
1668 static void phylink_sfp_disconnect_phy(void *upstream)
1669 {
1670         phylink_disconnect_phy(upstream);
1671 }
1672
1673 static const struct sfp_upstream_ops sfp_phylink_ops = {
1674         .module_insert = phylink_sfp_module_insert,
1675         .link_up = phylink_sfp_link_up,
1676         .link_down = phylink_sfp_link_down,
1677         .connect_phy = phylink_sfp_connect_phy,
1678         .disconnect_phy = phylink_sfp_disconnect_phy,
1679 };
1680
1681 /* Helpers for MAC drivers */
1682
1683 /**
1684  * phylink_helper_basex_speed() - 1000BaseX/2500BaseX helper
1685  * @state: a pointer to a &struct phylink_link_state
1686  *
1687  * Inspect the interface mode, advertising mask or forced speed and
1688  * decide whether to run at 2.5Gbit or 1Gbit appropriately, switching
1689  * the interface mode to suit.  @state->interface is appropriately
1690  * updated, and the advertising mask has the "other" baseX_Full flag
1691  * cleared.
1692  */
1693 void phylink_helper_basex_speed(struct phylink_link_state *state)
1694 {
1695         if (phy_interface_mode_is_8023z(state->interface)) {
1696                 bool want_2500 = state->an_enabled ?
1697                         phylink_test(state->advertising, 2500baseX_Full) :
1698                         state->speed == SPEED_2500;
1699
1700                 if (want_2500) {
1701                         phylink_clear(state->advertising, 1000baseX_Full);
1702                         state->interface = PHY_INTERFACE_MODE_2500BASEX;
1703                 } else {
1704                         phylink_clear(state->advertising, 2500baseX_Full);
1705                         state->interface = PHY_INTERFACE_MODE_1000BASEX;
1706                 }
1707         }
1708 }
1709 EXPORT_SYMBOL_GPL(phylink_helper_basex_speed);
1710
1711 MODULE_LICENSE("GPL");