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