]>
Commit | Line | Data |
---|---|---|
9525ae83 RK |
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/workqueue.h> | |
23 | ||
ce0aa27f | 24 | #include "sfp.h" |
9525ae83 RK |
25 | #include "swphy.h" |
26 | ||
27 | #define SUPPORTED_INTERFACES \ | |
28 | (SUPPORTED_TP | SUPPORTED_MII | SUPPORTED_FIBRE | \ | |
29 | SUPPORTED_BNC | SUPPORTED_AUI | SUPPORTED_Backplane) | |
30 | #define ADVERTISED_INTERFACES \ | |
31 | (ADVERTISED_TP | ADVERTISED_MII | ADVERTISED_FIBRE | \ | |
32 | ADVERTISED_BNC | ADVERTISED_AUI | ADVERTISED_Backplane) | |
33 | ||
34 | enum { | |
35 | PHYLINK_DISABLE_STOPPED, | |
ce0aa27f | 36 | PHYLINK_DISABLE_LINK, |
9525ae83 RK |
37 | }; |
38 | ||
39 | struct phylink { | |
40 | struct net_device *netdev; | |
41 | const struct phylink_mac_ops *ops; | |
42 | ||
43 | unsigned long phylink_disable_state; /* bitmask of disables */ | |
44 | struct phy_device *phydev; | |
45 | phy_interface_t link_interface; /* PHY_INTERFACE_xxx */ | |
46 | u8 link_an_mode; /* MLO_AN_xxx */ | |
47 | u8 link_port; /* The current non-phy ethtool port */ | |
48 | __ETHTOOL_DECLARE_LINK_MODE_MASK(supported); | |
49 | ||
50 | /* The link configuration settings */ | |
51 | struct phylink_link_state link_config; | |
52 | struct gpio_desc *link_gpio; | |
53 | ||
54 | struct mutex state_mutex; | |
55 | struct phylink_link_state phy_state; | |
56 | struct work_struct resolve; | |
57 | ||
58 | bool mac_link_dropped; | |
ce0aa27f RK |
59 | |
60 | struct sfp_bus *sfp_bus; | |
9525ae83 RK |
61 | }; |
62 | ||
63 | static inline void linkmode_zero(unsigned long *dst) | |
64 | { | |
65 | bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS); | |
66 | } | |
67 | ||
68 | static inline void linkmode_copy(unsigned long *dst, const unsigned long *src) | |
69 | { | |
70 | bitmap_copy(dst, src, __ETHTOOL_LINK_MODE_MASK_NBITS); | |
71 | } | |
72 | ||
73 | static inline void linkmode_and(unsigned long *dst, const unsigned long *a, | |
74 | const unsigned long *b) | |
75 | { | |
76 | bitmap_and(dst, a, b, __ETHTOOL_LINK_MODE_MASK_NBITS); | |
77 | } | |
78 | ||
79 | static inline void linkmode_or(unsigned long *dst, const unsigned long *a, | |
80 | const unsigned long *b) | |
81 | { | |
82 | bitmap_or(dst, a, b, __ETHTOOL_LINK_MODE_MASK_NBITS); | |
83 | } | |
84 | ||
85 | static inline bool linkmode_empty(const unsigned long *src) | |
86 | { | |
87 | return bitmap_empty(src, __ETHTOOL_LINK_MODE_MASK_NBITS); | |
88 | } | |
89 | ||
90 | void phylink_set_port_modes(unsigned long *mask) | |
91 | { | |
92 | phylink_set(mask, TP); | |
93 | phylink_set(mask, AUI); | |
94 | phylink_set(mask, MII); | |
95 | phylink_set(mask, FIBRE); | |
96 | phylink_set(mask, BNC); | |
97 | phylink_set(mask, Backplane); | |
98 | } | |
99 | EXPORT_SYMBOL_GPL(phylink_set_port_modes); | |
100 | ||
101 | static int phylink_is_empty_linkmode(const unsigned long *linkmode) | |
102 | { | |
103 | __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp) = { 0, }; | |
104 | ||
105 | phylink_set_port_modes(tmp); | |
106 | phylink_set(tmp, Autoneg); | |
107 | phylink_set(tmp, Pause); | |
108 | phylink_set(tmp, Asym_Pause); | |
109 | ||
110 | bitmap_andnot(tmp, linkmode, tmp, __ETHTOOL_LINK_MODE_MASK_NBITS); | |
111 | ||
112 | return linkmode_empty(tmp); | |
113 | } | |
114 | ||
115 | static const char *phylink_an_mode_str(unsigned int mode) | |
116 | { | |
117 | static const char *modestr[] = { | |
118 | [MLO_AN_PHY] = "phy", | |
119 | [MLO_AN_FIXED] = "fixed", | |
120 | [MLO_AN_SGMII] = "SGMII", | |
121 | [MLO_AN_8023Z] = "802.3z", | |
122 | }; | |
123 | ||
124 | return mode < ARRAY_SIZE(modestr) ? modestr[mode] : "unknown"; | |
125 | } | |
126 | ||
127 | static int phylink_validate(struct phylink *pl, unsigned long *supported, | |
128 | struct phylink_link_state *state) | |
129 | { | |
130 | pl->ops->validate(pl->netdev, supported, state); | |
131 | ||
132 | return phylink_is_empty_linkmode(supported) ? -EINVAL : 0; | |
133 | } | |
134 | ||
135 | static int phylink_parse_fixedlink(struct phylink *pl, struct device_node *np) | |
136 | { | |
137 | struct device_node *fixed_node; | |
138 | const struct phy_setting *s; | |
139 | struct gpio_desc *desc; | |
140 | const __be32 *fixed_prop; | |
141 | u32 speed; | |
142 | int ret, len; | |
143 | ||
144 | fixed_node = of_get_child_by_name(np, "fixed-link"); | |
145 | if (fixed_node) { | |
146 | ret = of_property_read_u32(fixed_node, "speed", &speed); | |
147 | ||
148 | pl->link_config.speed = speed; | |
149 | pl->link_config.duplex = DUPLEX_HALF; | |
150 | ||
151 | if (of_property_read_bool(fixed_node, "full-duplex")) | |
152 | pl->link_config.duplex = DUPLEX_FULL; | |
153 | ||
154 | /* We treat the "pause" and "asym-pause" terminology as | |
155 | * defining the link partner's ability. */ | |
156 | if (of_property_read_bool(fixed_node, "pause")) | |
157 | pl->link_config.pause |= MLO_PAUSE_SYM; | |
158 | if (of_property_read_bool(fixed_node, "asym-pause")) | |
159 | pl->link_config.pause |= MLO_PAUSE_ASYM; | |
160 | ||
161 | if (ret == 0) { | |
162 | desc = fwnode_get_named_gpiod(&fixed_node->fwnode, | |
163 | "link-gpios", 0, | |
164 | GPIOD_IN, "?"); | |
165 | ||
166 | if (!IS_ERR(desc)) | |
167 | pl->link_gpio = desc; | |
168 | else if (desc == ERR_PTR(-EPROBE_DEFER)) | |
169 | ret = -EPROBE_DEFER; | |
170 | } | |
171 | of_node_put(fixed_node); | |
172 | ||
173 | if (ret) | |
174 | return ret; | |
175 | } else { | |
176 | fixed_prop = of_get_property(np, "fixed-link", &len); | |
177 | if (!fixed_prop) { | |
178 | netdev_err(pl->netdev, "broken fixed-link?\n"); | |
179 | return -EINVAL; | |
180 | } | |
181 | if (len == 5 * sizeof(*fixed_prop)) { | |
182 | pl->link_config.duplex = be32_to_cpu(fixed_prop[1]) ? | |
183 | DUPLEX_FULL : DUPLEX_HALF; | |
184 | pl->link_config.speed = be32_to_cpu(fixed_prop[2]); | |
185 | if (be32_to_cpu(fixed_prop[3])) | |
186 | pl->link_config.pause |= MLO_PAUSE_SYM; | |
187 | if (be32_to_cpu(fixed_prop[4])) | |
188 | pl->link_config.pause |= MLO_PAUSE_ASYM; | |
189 | } | |
190 | } | |
191 | ||
192 | if (pl->link_config.speed > SPEED_1000 && | |
193 | pl->link_config.duplex != DUPLEX_FULL) | |
194 | netdev_warn(pl->netdev, "fixed link specifies half duplex for %dMbps link?\n", | |
195 | pl->link_config.speed); | |
196 | ||
197 | bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS); | |
198 | linkmode_copy(pl->link_config.advertising, pl->supported); | |
199 | phylink_validate(pl, pl->supported, &pl->link_config); | |
200 | ||
201 | s = phy_lookup_setting(pl->link_config.speed, pl->link_config.duplex, | |
202 | pl->supported, | |
203 | __ETHTOOL_LINK_MODE_MASK_NBITS, true); | |
204 | linkmode_zero(pl->supported); | |
205 | phylink_set(pl->supported, MII); | |
206 | if (s) { | |
207 | __set_bit(s->bit, pl->supported); | |
208 | } else { | |
209 | netdev_warn(pl->netdev, "fixed link %s duplex %dMbps not recognised\n", | |
210 | pl->link_config.duplex == DUPLEX_FULL ? "full" : "half", | |
211 | pl->link_config.speed); | |
212 | } | |
213 | ||
214 | linkmode_and(pl->link_config.advertising, pl->link_config.advertising, | |
215 | pl->supported); | |
216 | ||
217 | pl->link_config.link = 1; | |
218 | pl->link_config.an_complete = 1; | |
219 | ||
220 | return 0; | |
221 | } | |
222 | ||
223 | static int phylink_parse_mode(struct phylink *pl, struct device_node *np) | |
224 | { | |
225 | struct device_node *dn; | |
226 | const char *managed; | |
227 | ||
228 | dn = of_get_child_by_name(np, "fixed-link"); | |
229 | if (dn || of_find_property(np, "fixed-link", NULL)) | |
230 | pl->link_an_mode = MLO_AN_FIXED; | |
231 | of_node_put(dn); | |
232 | ||
233 | if (of_property_read_string(np, "managed", &managed) == 0 && | |
234 | strcmp(managed, "in-band-status") == 0) { | |
235 | if (pl->link_an_mode == MLO_AN_FIXED) { | |
236 | netdev_err(pl->netdev, | |
237 | "can't use both fixed-link and in-band-status\n"); | |
238 | return -EINVAL; | |
239 | } | |
240 | ||
241 | linkmode_zero(pl->supported); | |
242 | phylink_set(pl->supported, MII); | |
243 | phylink_set(pl->supported, Autoneg); | |
244 | phylink_set(pl->supported, Asym_Pause); | |
245 | phylink_set(pl->supported, Pause); | |
246 | pl->link_config.an_enabled = true; | |
247 | ||
248 | switch (pl->link_config.interface) { | |
249 | case PHY_INTERFACE_MODE_SGMII: | |
250 | phylink_set(pl->supported, 10baseT_Half); | |
251 | phylink_set(pl->supported, 10baseT_Full); | |
252 | phylink_set(pl->supported, 100baseT_Half); | |
253 | phylink_set(pl->supported, 100baseT_Full); | |
254 | phylink_set(pl->supported, 1000baseT_Half); | |
255 | phylink_set(pl->supported, 1000baseT_Full); | |
256 | pl->link_an_mode = MLO_AN_SGMII; | |
257 | break; | |
258 | ||
259 | case PHY_INTERFACE_MODE_1000BASEX: | |
260 | phylink_set(pl->supported, 1000baseX_Full); | |
261 | pl->link_an_mode = MLO_AN_8023Z; | |
262 | break; | |
263 | ||
264 | case PHY_INTERFACE_MODE_2500BASEX: | |
265 | phylink_set(pl->supported, 2500baseX_Full); | |
266 | pl->link_an_mode = MLO_AN_8023Z; | |
267 | break; | |
268 | ||
da7c1862 RK |
269 | case PHY_INTERFACE_MODE_10GKR: |
270 | phylink_set(pl->supported, 10baseT_Half); | |
271 | phylink_set(pl->supported, 10baseT_Full); | |
272 | phylink_set(pl->supported, 100baseT_Half); | |
273 | phylink_set(pl->supported, 100baseT_Full); | |
274 | phylink_set(pl->supported, 1000baseT_Half); | |
275 | phylink_set(pl->supported, 1000baseT_Full); | |
276 | phylink_set(pl->supported, 1000baseX_Full); | |
277 | phylink_set(pl->supported, 10000baseKR_Full); | |
278 | phylink_set(pl->supported, 10000baseCR_Full); | |
279 | phylink_set(pl->supported, 10000baseSR_Full); | |
280 | phylink_set(pl->supported, 10000baseLR_Full); | |
281 | phylink_set(pl->supported, 10000baseLRM_Full); | |
282 | phylink_set(pl->supported, 10000baseER_Full); | |
283 | pl->link_an_mode = MLO_AN_SGMII; | |
284 | break; | |
285 | ||
9525ae83 RK |
286 | default: |
287 | netdev_err(pl->netdev, | |
288 | "incorrect link mode %s for in-band status\n", | |
289 | phy_modes(pl->link_config.interface)); | |
290 | return -EINVAL; | |
291 | } | |
292 | ||
293 | linkmode_copy(pl->link_config.advertising, pl->supported); | |
294 | ||
295 | if (phylink_validate(pl, pl->supported, &pl->link_config)) { | |
296 | netdev_err(pl->netdev, | |
297 | "failed to validate link configuration for in-band status\n"); | |
298 | return -EINVAL; | |
299 | } | |
300 | } | |
301 | ||
302 | return 0; | |
303 | } | |
304 | ||
305 | static void phylink_mac_config(struct phylink *pl, | |
306 | const struct phylink_link_state *state) | |
307 | { | |
308 | netdev_dbg(pl->netdev, | |
309 | "%s: mode=%s/%s/%s/%s adv=%*pb pause=%02x link=%u an=%u\n", | |
310 | __func__, phylink_an_mode_str(pl->link_an_mode), | |
311 | phy_modes(state->interface), | |
312 | phy_speed_to_str(state->speed), | |
313 | phy_duplex_to_str(state->duplex), | |
314 | __ETHTOOL_LINK_MODE_MASK_NBITS, state->advertising, | |
315 | state->pause, state->link, state->an_enabled); | |
316 | ||
317 | pl->ops->mac_config(pl->netdev, pl->link_an_mode, state); | |
318 | } | |
319 | ||
320 | static void phylink_mac_an_restart(struct phylink *pl) | |
321 | { | |
322 | if (pl->link_config.an_enabled && | |
323 | (pl->link_config.interface == PHY_INTERFACE_MODE_1000BASEX || | |
324 | pl->link_config.interface == PHY_INTERFACE_MODE_2500BASEX)) | |
325 | pl->ops->mac_an_restart(pl->netdev); | |
326 | } | |
327 | ||
328 | static int phylink_get_mac_state(struct phylink *pl, struct phylink_link_state *state) | |
329 | { | |
330 | struct net_device *ndev = pl->netdev; | |
331 | ||
332 | linkmode_copy(state->advertising, pl->link_config.advertising); | |
333 | linkmode_zero(state->lp_advertising); | |
334 | state->interface = pl->link_config.interface; | |
335 | state->an_enabled = pl->link_config.an_enabled; | |
336 | state->link = 1; | |
337 | ||
338 | return pl->ops->mac_link_state(ndev, state); | |
339 | } | |
340 | ||
341 | /* The fixed state is... fixed except for the link state, | |
342 | * which may be determined by a GPIO. | |
343 | */ | |
344 | static void phylink_get_fixed_state(struct phylink *pl, struct phylink_link_state *state) | |
345 | { | |
346 | *state = pl->link_config; | |
347 | if (pl->link_gpio) | |
348 | state->link = !!gpiod_get_value(pl->link_gpio); | |
349 | } | |
350 | ||
351 | /* Flow control is resolved according to our and the link partners | |
352 | * advertisments using the following drawn from the 802.3 specs: | |
353 | * Local device Link partner | |
354 | * Pause AsymDir Pause AsymDir Result | |
355 | * 1 X 1 X TX+RX | |
356 | * 0 1 1 1 RX | |
357 | * 1 1 0 1 TX | |
358 | */ | |
359 | static void phylink_resolve_flow(struct phylink *pl, | |
516b29ed | 360 | struct phylink_link_state *state) |
9525ae83 RK |
361 | { |
362 | int new_pause = 0; | |
363 | ||
364 | if (pl->link_config.pause & MLO_PAUSE_AN) { | |
365 | int pause = 0; | |
366 | ||
367 | if (phylink_test(pl->link_config.advertising, Pause)) | |
368 | pause |= MLO_PAUSE_SYM; | |
369 | if (phylink_test(pl->link_config.advertising, Asym_Pause)) | |
370 | pause |= MLO_PAUSE_ASYM; | |
371 | ||
372 | pause &= state->pause; | |
373 | ||
374 | if (pause & MLO_PAUSE_SYM) | |
375 | new_pause = MLO_PAUSE_TX | MLO_PAUSE_RX; | |
376 | else if (pause & MLO_PAUSE_ASYM) | |
377 | new_pause = state->pause & MLO_PAUSE_SYM ? | |
378 | MLO_PAUSE_RX : MLO_PAUSE_TX; | |
379 | } else { | |
380 | new_pause = pl->link_config.pause & MLO_PAUSE_TXRX_MASK; | |
381 | } | |
382 | ||
383 | state->pause &= ~MLO_PAUSE_TXRX_MASK; | |
384 | state->pause |= new_pause; | |
385 | } | |
386 | ||
387 | static const char *phylink_pause_to_str(int pause) | |
388 | { | |
389 | switch (pause & MLO_PAUSE_TXRX_MASK) { | |
390 | case MLO_PAUSE_TX | MLO_PAUSE_RX: | |
391 | return "rx/tx"; | |
392 | case MLO_PAUSE_TX: | |
393 | return "tx"; | |
394 | case MLO_PAUSE_RX: | |
395 | return "rx"; | |
396 | default: | |
397 | return "off"; | |
398 | } | |
399 | } | |
400 | ||
401 | static void phylink_resolve(struct work_struct *w) | |
402 | { | |
403 | struct phylink *pl = container_of(w, struct phylink, resolve); | |
404 | struct phylink_link_state link_state; | |
405 | struct net_device *ndev = pl->netdev; | |
406 | ||
407 | mutex_lock(&pl->state_mutex); | |
408 | if (pl->phylink_disable_state) { | |
409 | pl->mac_link_dropped = false; | |
410 | link_state.link = false; | |
411 | } else if (pl->mac_link_dropped) { | |
412 | link_state.link = false; | |
413 | } else { | |
414 | switch (pl->link_an_mode) { | |
415 | case MLO_AN_PHY: | |
416 | link_state = pl->phy_state; | |
417 | phylink_resolve_flow(pl, &link_state); | |
418 | phylink_mac_config(pl, &link_state); | |
419 | break; | |
420 | ||
421 | case MLO_AN_FIXED: | |
422 | phylink_get_fixed_state(pl, &link_state); | |
423 | phylink_mac_config(pl, &link_state); | |
424 | break; | |
425 | ||
426 | case MLO_AN_SGMII: | |
427 | phylink_get_mac_state(pl, &link_state); | |
428 | if (pl->phydev) { | |
429 | bool changed = false; | |
430 | ||
431 | link_state.link = link_state.link && | |
432 | pl->phy_state.link; | |
433 | ||
434 | if (pl->phy_state.interface != | |
435 | link_state.interface) { | |
436 | link_state.interface = pl->phy_state.interface; | |
437 | changed = true; | |
438 | } | |
439 | ||
440 | /* Propagate the flow control from the PHY | |
441 | * to the MAC. Also propagate the interface | |
442 | * if changed. | |
443 | */ | |
444 | if (pl->phy_state.link || changed) { | |
445 | link_state.pause |= pl->phy_state.pause; | |
446 | phylink_resolve_flow(pl, &link_state); | |
447 | ||
448 | phylink_mac_config(pl, &link_state); | |
449 | } | |
450 | } | |
451 | break; | |
452 | ||
453 | case MLO_AN_8023Z: | |
454 | phylink_get_mac_state(pl, &link_state); | |
455 | break; | |
456 | } | |
457 | } | |
458 | ||
459 | if (link_state.link != netif_carrier_ok(ndev)) { | |
460 | if (!link_state.link) { | |
461 | netif_carrier_off(ndev); | |
462 | pl->ops->mac_link_down(ndev, pl->link_an_mode); | |
463 | netdev_info(ndev, "Link is Down\n"); | |
464 | } else { | |
465 | pl->ops->mac_link_up(ndev, pl->link_an_mode, | |
466 | pl->phydev); | |
467 | ||
468 | netif_carrier_on(ndev); | |
469 | ||
470 | netdev_info(ndev, | |
471 | "Link is Up - %s/%s - flow control %s\n", | |
472 | phy_speed_to_str(link_state.speed), | |
473 | phy_duplex_to_str(link_state.duplex), | |
474 | phylink_pause_to_str(link_state.pause)); | |
475 | } | |
476 | } | |
477 | if (!link_state.link && pl->mac_link_dropped) { | |
478 | pl->mac_link_dropped = false; | |
479 | queue_work(system_power_efficient_wq, &pl->resolve); | |
480 | } | |
481 | mutex_unlock(&pl->state_mutex); | |
482 | } | |
483 | ||
484 | static void phylink_run_resolve(struct phylink *pl) | |
485 | { | |
486 | if (!pl->phylink_disable_state) | |
487 | queue_work(system_power_efficient_wq, &pl->resolve); | |
488 | } | |
489 | ||
ce0aa27f RK |
490 | static const struct sfp_upstream_ops sfp_phylink_ops; |
491 | ||
492 | static int phylink_register_sfp(struct phylink *pl, struct device_node *np) | |
493 | { | |
494 | struct device_node *sfp_np; | |
495 | ||
496 | sfp_np = of_parse_phandle(np, "sfp", 0); | |
497 | if (!sfp_np) | |
498 | return 0; | |
499 | ||
500 | pl->sfp_bus = sfp_register_upstream(sfp_np, pl->netdev, pl, | |
501 | &sfp_phylink_ops); | |
502 | if (!pl->sfp_bus) | |
503 | return -ENOMEM; | |
504 | ||
505 | return 0; | |
506 | } | |
507 | ||
9525ae83 | 508 | struct phylink *phylink_create(struct net_device *ndev, struct device_node *np, |
516b29ed FF |
509 | phy_interface_t iface, |
510 | const struct phylink_mac_ops *ops) | |
9525ae83 RK |
511 | { |
512 | struct phylink *pl; | |
513 | int ret; | |
514 | ||
515 | pl = kzalloc(sizeof(*pl), GFP_KERNEL); | |
516 | if (!pl) | |
517 | return ERR_PTR(-ENOMEM); | |
518 | ||
519 | mutex_init(&pl->state_mutex); | |
520 | INIT_WORK(&pl->resolve, phylink_resolve); | |
521 | pl->netdev = ndev; | |
522 | pl->phy_state.interface = iface; | |
523 | pl->link_interface = iface; | |
524 | pl->link_port = PORT_MII; | |
525 | pl->link_config.interface = iface; | |
526 | pl->link_config.pause = MLO_PAUSE_AN; | |
527 | pl->link_config.speed = SPEED_UNKNOWN; | |
528 | pl->link_config.duplex = DUPLEX_UNKNOWN; | |
529 | pl->ops = ops; | |
530 | __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state); | |
531 | ||
532 | bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS); | |
533 | linkmode_copy(pl->link_config.advertising, pl->supported); | |
534 | phylink_validate(pl, pl->supported, &pl->link_config); | |
535 | ||
536 | ret = phylink_parse_mode(pl, np); | |
537 | if (ret < 0) { | |
538 | kfree(pl); | |
539 | return ERR_PTR(ret); | |
540 | } | |
541 | ||
542 | if (pl->link_an_mode == MLO_AN_FIXED) { | |
543 | ret = phylink_parse_fixedlink(pl, np); | |
544 | if (ret < 0) { | |
545 | kfree(pl); | |
546 | return ERR_PTR(ret); | |
547 | } | |
548 | } | |
549 | ||
ce0aa27f RK |
550 | ret = phylink_register_sfp(pl, np); |
551 | if (ret < 0) { | |
552 | kfree(pl); | |
553 | return ERR_PTR(ret); | |
554 | } | |
555 | ||
9525ae83 RK |
556 | return pl; |
557 | } | |
558 | EXPORT_SYMBOL_GPL(phylink_create); | |
559 | ||
560 | void phylink_destroy(struct phylink *pl) | |
561 | { | |
ce0aa27f RK |
562 | if (pl->sfp_bus) |
563 | sfp_unregister_upstream(pl->sfp_bus); | |
564 | ||
9525ae83 RK |
565 | cancel_work_sync(&pl->resolve); |
566 | kfree(pl); | |
567 | } | |
568 | EXPORT_SYMBOL_GPL(phylink_destroy); | |
569 | ||
570 | void phylink_phy_change(struct phy_device *phydev, bool up, bool do_carrier) | |
571 | { | |
572 | struct phylink *pl = phydev->phylink; | |
573 | ||
574 | mutex_lock(&pl->state_mutex); | |
575 | pl->phy_state.speed = phydev->speed; | |
576 | pl->phy_state.duplex = phydev->duplex; | |
577 | pl->phy_state.pause = MLO_PAUSE_NONE; | |
578 | if (phydev->pause) | |
579 | pl->phy_state.pause |= MLO_PAUSE_SYM; | |
580 | if (phydev->asym_pause) | |
581 | pl->phy_state.pause |= MLO_PAUSE_ASYM; | |
582 | pl->phy_state.interface = phydev->interface; | |
583 | pl->phy_state.link = up; | |
584 | mutex_unlock(&pl->state_mutex); | |
585 | ||
586 | phylink_run_resolve(pl); | |
587 | ||
588 | netdev_dbg(pl->netdev, "phy link %s %s/%s/%s\n", up ? "up" : "down", | |
516b29ed | 589 | phy_modes(phydev->interface), |
9525ae83 RK |
590 | phy_speed_to_str(phydev->speed), |
591 | phy_duplex_to_str(phydev->duplex)); | |
592 | } | |
593 | ||
594 | static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy) | |
595 | { | |
596 | struct phylink_link_state config; | |
597 | __ETHTOOL_DECLARE_LINK_MODE_MASK(supported); | |
598 | u32 advertising; | |
599 | int ret; | |
600 | ||
601 | memset(&config, 0, sizeof(config)); | |
602 | ethtool_convert_legacy_u32_to_link_mode(supported, phy->supported); | |
603 | ethtool_convert_legacy_u32_to_link_mode(config.advertising, | |
604 | phy->advertising); | |
605 | config.interface = pl->link_config.interface; | |
606 | ||
607 | /* | |
608 | * This is the new way of dealing with flow control for PHYs, | |
609 | * as described by Timur Tabi in commit 529ed1275263 ("net: phy: | |
610 | * phy drivers should not set SUPPORTED_[Asym_]Pause") except | |
611 | * using our validate call to the MAC, we rely upon the MAC | |
612 | * clearing the bits from both supported and advertising fields. | |
613 | */ | |
614 | if (phylink_test(supported, Pause)) | |
615 | phylink_set(config.advertising, Pause); | |
616 | if (phylink_test(supported, Asym_Pause)) | |
617 | phylink_set(config.advertising, Asym_Pause); | |
618 | ||
619 | ret = phylink_validate(pl, supported, &config); | |
620 | if (ret) | |
621 | return ret; | |
622 | ||
623 | phy->phylink = pl; | |
624 | phy->phy_link_change = phylink_phy_change; | |
625 | ||
626 | netdev_info(pl->netdev, | |
627 | "PHY [%s] driver [%s]\n", dev_name(&phy->mdio.dev), | |
628 | phy->drv->name); | |
629 | ||
630 | mutex_lock(&phy->lock); | |
631 | mutex_lock(&pl->state_mutex); | |
632 | pl->netdev->phydev = phy; | |
633 | pl->phydev = phy; | |
634 | linkmode_copy(pl->supported, supported); | |
635 | linkmode_copy(pl->link_config.advertising, config.advertising); | |
636 | ||
637 | /* Restrict the phy advertisment according to the MAC support. */ | |
638 | ethtool_convert_link_mode_to_legacy_u32(&advertising, config.advertising); | |
639 | phy->advertising = advertising; | |
640 | mutex_unlock(&pl->state_mutex); | |
641 | mutex_unlock(&phy->lock); | |
642 | ||
643 | netdev_dbg(pl->netdev, | |
644 | "phy: setting supported %*pb advertising 0x%08x\n", | |
645 | __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported, | |
646 | phy->advertising); | |
647 | ||
648 | phy_start_machine(phy); | |
649 | if (phy->irq > 0) | |
650 | phy_start_interrupts(phy); | |
651 | ||
652 | return 0; | |
653 | } | |
654 | ||
655 | int phylink_connect_phy(struct phylink *pl, struct phy_device *phy) | |
656 | { | |
657 | int ret; | |
658 | ||
659 | ret = phy_attach_direct(pl->netdev, phy, 0, pl->link_interface); | |
660 | if (ret) | |
661 | return ret; | |
662 | ||
663 | ret = phylink_bringup_phy(pl, phy); | |
664 | if (ret) | |
665 | phy_detach(phy); | |
666 | ||
667 | return ret; | |
668 | } | |
669 | EXPORT_SYMBOL_GPL(phylink_connect_phy); | |
670 | ||
671 | int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn) | |
672 | { | |
673 | struct device_node *phy_node; | |
674 | struct phy_device *phy_dev; | |
675 | int ret; | |
676 | ||
677 | /* Fixed links are handled without needing a PHY */ | |
678 | if (pl->link_an_mode == MLO_AN_FIXED) | |
679 | return 0; | |
680 | ||
681 | phy_node = of_parse_phandle(dn, "phy-handle", 0); | |
682 | if (!phy_node) | |
683 | phy_node = of_parse_phandle(dn, "phy", 0); | |
684 | if (!phy_node) | |
685 | phy_node = of_parse_phandle(dn, "phy-device", 0); | |
686 | ||
687 | if (!phy_node) { | |
688 | if (pl->link_an_mode == MLO_AN_PHY) { | |
689 | netdev_err(pl->netdev, "unable to find PHY node\n"); | |
690 | return -ENODEV; | |
691 | } | |
692 | return 0; | |
693 | } | |
694 | ||
695 | phy_dev = of_phy_attach(pl->netdev, phy_node, 0, pl->link_interface); | |
696 | /* We're done with the phy_node handle */ | |
697 | of_node_put(phy_node); | |
698 | ||
699 | if (!phy_dev) | |
700 | return -ENODEV; | |
701 | ||
702 | ret = phylink_bringup_phy(pl, phy_dev); | |
703 | if (ret) | |
704 | phy_detach(phy_dev); | |
705 | ||
706 | return ret; | |
707 | } | |
708 | EXPORT_SYMBOL_GPL(phylink_of_phy_connect); | |
709 | ||
710 | void phylink_disconnect_phy(struct phylink *pl) | |
711 | { | |
712 | struct phy_device *phy; | |
713 | ||
714 | WARN_ON(!lockdep_rtnl_is_held()); | |
715 | ||
716 | phy = pl->phydev; | |
717 | if (phy) { | |
718 | mutex_lock(&phy->lock); | |
719 | mutex_lock(&pl->state_mutex); | |
720 | pl->netdev->phydev = NULL; | |
721 | pl->phydev = NULL; | |
722 | mutex_unlock(&pl->state_mutex); | |
723 | mutex_unlock(&phy->lock); | |
724 | flush_work(&pl->resolve); | |
725 | ||
726 | phy_disconnect(phy); | |
727 | } | |
728 | } | |
729 | EXPORT_SYMBOL_GPL(phylink_disconnect_phy); | |
730 | ||
731 | void phylink_mac_change(struct phylink *pl, bool up) | |
732 | { | |
733 | if (!up) | |
734 | pl->mac_link_dropped = true; | |
735 | phylink_run_resolve(pl); | |
736 | netdev_dbg(pl->netdev, "mac link %s\n", up ? "up" : "down"); | |
737 | } | |
738 | EXPORT_SYMBOL_GPL(phylink_mac_change); | |
739 | ||
740 | void phylink_start(struct phylink *pl) | |
741 | { | |
742 | WARN_ON(!lockdep_rtnl_is_held()); | |
743 | ||
744 | netdev_info(pl->netdev, "configuring for %s/%s link mode\n", | |
745 | phylink_an_mode_str(pl->link_an_mode), | |
746 | phy_modes(pl->link_config.interface)); | |
747 | ||
748 | /* Apply the link configuration to the MAC when starting. This allows | |
749 | * a fixed-link to start with the correct parameters, and also | |
750 | * ensures that we set the appropriate advertisment for Serdes links. | |
751 | */ | |
752 | phylink_resolve_flow(pl, &pl->link_config); | |
753 | phylink_mac_config(pl, &pl->link_config); | |
754 | ||
755 | clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state); | |
756 | phylink_run_resolve(pl); | |
757 | ||
ce0aa27f RK |
758 | if (pl->sfp_bus) |
759 | sfp_upstream_start(pl->sfp_bus); | |
9525ae83 RK |
760 | if (pl->phydev) |
761 | phy_start(pl->phydev); | |
762 | } | |
763 | EXPORT_SYMBOL_GPL(phylink_start); | |
764 | ||
765 | void phylink_stop(struct phylink *pl) | |
766 | { | |
767 | WARN_ON(!lockdep_rtnl_is_held()); | |
768 | ||
769 | if (pl->phydev) | |
770 | phy_stop(pl->phydev); | |
ce0aa27f RK |
771 | if (pl->sfp_bus) |
772 | sfp_upstream_stop(pl->sfp_bus); | |
9525ae83 RK |
773 | |
774 | set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state); | |
775 | flush_work(&pl->resolve); | |
776 | } | |
777 | EXPORT_SYMBOL_GPL(phylink_stop); | |
778 | ||
779 | void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol) | |
780 | { | |
781 | WARN_ON(!lockdep_rtnl_is_held()); | |
782 | ||
783 | wol->supported = 0; | |
784 | wol->wolopts = 0; | |
785 | ||
786 | if (pl->phydev) | |
787 | phy_ethtool_get_wol(pl->phydev, wol); | |
788 | } | |
789 | EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol); | |
790 | ||
791 | int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol) | |
792 | { | |
793 | int ret = -EOPNOTSUPP; | |
794 | ||
795 | WARN_ON(!lockdep_rtnl_is_held()); | |
796 | ||
797 | if (pl->phydev) | |
798 | ret = phy_ethtool_set_wol(pl->phydev, wol); | |
799 | ||
800 | return ret; | |
801 | } | |
802 | EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol); | |
803 | ||
804 | static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b) | |
805 | { | |
806 | __ETHTOOL_DECLARE_LINK_MODE_MASK(mask); | |
807 | ||
808 | linkmode_zero(mask); | |
809 | phylink_set_port_modes(mask); | |
810 | ||
811 | linkmode_and(dst, dst, mask); | |
812 | linkmode_or(dst, dst, b); | |
813 | } | |
814 | ||
815 | static void phylink_get_ksettings(const struct phylink_link_state *state, | |
816 | struct ethtool_link_ksettings *kset) | |
817 | { | |
818 | phylink_merge_link_mode(kset->link_modes.advertising, state->advertising); | |
819 | linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising); | |
820 | kset->base.speed = state->speed; | |
821 | kset->base.duplex = state->duplex; | |
822 | kset->base.autoneg = state->an_enabled ? AUTONEG_ENABLE : | |
823 | AUTONEG_DISABLE; | |
824 | } | |
825 | ||
826 | int phylink_ethtool_ksettings_get(struct phylink *pl, | |
516b29ed | 827 | struct ethtool_link_ksettings *kset) |
9525ae83 RK |
828 | { |
829 | struct phylink_link_state link_state; | |
830 | ||
831 | WARN_ON(!lockdep_rtnl_is_held()); | |
832 | ||
833 | if (pl->phydev) { | |
834 | phy_ethtool_ksettings_get(pl->phydev, kset); | |
835 | } else { | |
836 | kset->base.port = pl->link_port; | |
837 | } | |
838 | ||
839 | linkmode_copy(kset->link_modes.supported, pl->supported); | |
840 | ||
841 | switch (pl->link_an_mode) { | |
842 | case MLO_AN_FIXED: | |
843 | /* We are using fixed settings. Report these as the | |
844 | * current link settings - and note that these also | |
845 | * represent the supported speeds/duplex/pause modes. | |
846 | */ | |
847 | phylink_get_fixed_state(pl, &link_state); | |
848 | phylink_get_ksettings(&link_state, kset); | |
849 | break; | |
850 | ||
851 | case MLO_AN_SGMII: | |
852 | /* If there is a phy attached, then use the reported | |
853 | * settings from the phy with no modification. | |
854 | */ | |
855 | if (pl->phydev) | |
856 | break; | |
857 | ||
858 | case MLO_AN_8023Z: | |
859 | phylink_get_mac_state(pl, &link_state); | |
860 | ||
861 | /* The MAC is reporting the link results from its own PCS | |
862 | * layer via in-band status. Report these as the current | |
863 | * link settings. | |
864 | */ | |
865 | phylink_get_ksettings(&link_state, kset); | |
866 | break; | |
867 | } | |
868 | ||
869 | return 0; | |
870 | } | |
871 | EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get); | |
872 | ||
873 | int phylink_ethtool_ksettings_set(struct phylink *pl, | |
516b29ed | 874 | const struct ethtool_link_ksettings *kset) |
9525ae83 RK |
875 | { |
876 | struct ethtool_link_ksettings our_kset; | |
877 | struct phylink_link_state config; | |
878 | int ret; | |
879 | ||
880 | WARN_ON(!lockdep_rtnl_is_held()); | |
881 | ||
882 | if (kset->base.autoneg != AUTONEG_DISABLE && | |
883 | kset->base.autoneg != AUTONEG_ENABLE) | |
884 | return -EINVAL; | |
885 | ||
886 | config = pl->link_config; | |
887 | ||
888 | /* Mask out unsupported advertisments */ | |
889 | linkmode_and(config.advertising, kset->link_modes.advertising, | |
890 | pl->supported); | |
891 | ||
892 | /* FIXME: should we reject autoneg if phy/mac does not support it? */ | |
893 | if (kset->base.autoneg == AUTONEG_DISABLE) { | |
894 | const struct phy_setting *s; | |
895 | ||
896 | /* Autonegotiation disabled, select a suitable speed and | |
897 | * duplex. | |
898 | */ | |
899 | s = phy_lookup_setting(kset->base.speed, kset->base.duplex, | |
900 | pl->supported, | |
901 | __ETHTOOL_LINK_MODE_MASK_NBITS, false); | |
902 | if (!s) | |
903 | return -EINVAL; | |
904 | ||
905 | /* If we have a fixed link (as specified by firmware), refuse | |
906 | * to change link parameters. | |
907 | */ | |
908 | if (pl->link_an_mode == MLO_AN_FIXED && | |
909 | (s->speed != pl->link_config.speed || | |
910 | s->duplex != pl->link_config.duplex)) | |
911 | return -EINVAL; | |
912 | ||
913 | config.speed = s->speed; | |
914 | config.duplex = s->duplex; | |
915 | config.an_enabled = false; | |
916 | ||
917 | __clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising); | |
918 | } else { | |
919 | /* If we have a fixed link, refuse to enable autonegotiation */ | |
920 | if (pl->link_an_mode == MLO_AN_FIXED) | |
921 | return -EINVAL; | |
922 | ||
923 | config.speed = SPEED_UNKNOWN; | |
924 | config.duplex = DUPLEX_UNKNOWN; | |
925 | config.an_enabled = true; | |
926 | ||
927 | __set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising); | |
928 | } | |
929 | ||
930 | if (phylink_validate(pl, pl->supported, &config)) | |
931 | return -EINVAL; | |
932 | ||
933 | /* If autonegotiation is enabled, we must have an advertisment */ | |
934 | if (config.an_enabled && phylink_is_empty_linkmode(config.advertising)) | |
935 | return -EINVAL; | |
936 | ||
937 | our_kset = *kset; | |
938 | linkmode_copy(our_kset.link_modes.advertising, config.advertising); | |
939 | our_kset.base.speed = config.speed; | |
940 | our_kset.base.duplex = config.duplex; | |
941 | ||
942 | /* If we have a PHY, configure the phy */ | |
943 | if (pl->phydev) { | |
944 | ret = phy_ethtool_ksettings_set(pl->phydev, &our_kset); | |
945 | if (ret) | |
946 | return ret; | |
947 | } | |
948 | ||
949 | mutex_lock(&pl->state_mutex); | |
950 | /* Configure the MAC to match the new settings */ | |
951 | linkmode_copy(pl->link_config.advertising, our_kset.link_modes.advertising); | |
952 | pl->link_config.speed = our_kset.base.speed; | |
953 | pl->link_config.duplex = our_kset.base.duplex; | |
954 | pl->link_config.an_enabled = our_kset.base.autoneg != AUTONEG_DISABLE; | |
955 | ||
956 | if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) { | |
957 | phylink_mac_config(pl, &pl->link_config); | |
958 | phylink_mac_an_restart(pl); | |
959 | } | |
960 | mutex_unlock(&pl->state_mutex); | |
961 | ||
d18c2a1b | 962 | return 0; |
9525ae83 RK |
963 | } |
964 | EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set); | |
965 | ||
966 | int phylink_ethtool_nway_reset(struct phylink *pl) | |
967 | { | |
968 | int ret = 0; | |
969 | ||
970 | WARN_ON(!lockdep_rtnl_is_held()); | |
971 | ||
972 | if (pl->phydev) | |
973 | ret = phy_restart_aneg(pl->phydev); | |
974 | phylink_mac_an_restart(pl); | |
975 | ||
976 | return ret; | |
977 | } | |
978 | EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset); | |
979 | ||
980 | void phylink_ethtool_get_pauseparam(struct phylink *pl, | |
981 | struct ethtool_pauseparam *pause) | |
982 | { | |
983 | WARN_ON(!lockdep_rtnl_is_held()); | |
984 | ||
985 | pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN); | |
986 | pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX); | |
987 | pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX); | |
988 | } | |
989 | EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam); | |
990 | ||
991 | int phylink_ethtool_set_pauseparam(struct phylink *pl, | |
992 | struct ethtool_pauseparam *pause) | |
993 | { | |
994 | struct phylink_link_state *config = &pl->link_config; | |
995 | ||
996 | WARN_ON(!lockdep_rtnl_is_held()); | |
997 | ||
998 | if (!phylink_test(pl->supported, Pause) && | |
999 | !phylink_test(pl->supported, Asym_Pause)) | |
1000 | return -EOPNOTSUPP; | |
1001 | ||
1002 | if (!phylink_test(pl->supported, Asym_Pause) && | |
1003 | !pause->autoneg && pause->rx_pause != pause->tx_pause) | |
1004 | return -EINVAL; | |
1005 | ||
1006 | config->pause &= ~(MLO_PAUSE_AN | MLO_PAUSE_TXRX_MASK); | |
1007 | ||
1008 | if (pause->autoneg) | |
1009 | config->pause |= MLO_PAUSE_AN; | |
1010 | if (pause->rx_pause) | |
1011 | config->pause |= MLO_PAUSE_RX; | |
1012 | if (pause->tx_pause) | |
1013 | config->pause |= MLO_PAUSE_TX; | |
1014 | ||
1015 | if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) { | |
1016 | switch (pl->link_an_mode) { | |
1017 | case MLO_AN_PHY: | |
1018 | /* Silently mark the carrier down, and then trigger a resolve */ | |
1019 | netif_carrier_off(pl->netdev); | |
1020 | phylink_run_resolve(pl); | |
1021 | break; | |
1022 | ||
1023 | case MLO_AN_FIXED: | |
1024 | /* Should we allow fixed links to change against the config? */ | |
1025 | phylink_resolve_flow(pl, config); | |
1026 | phylink_mac_config(pl, config); | |
1027 | break; | |
1028 | ||
1029 | case MLO_AN_SGMII: | |
1030 | case MLO_AN_8023Z: | |
1031 | phylink_mac_config(pl, config); | |
1032 | phylink_mac_an_restart(pl); | |
1033 | break; | |
1034 | } | |
1035 | } | |
1036 | ||
1037 | return 0; | |
1038 | } | |
1039 | EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam); | |
1040 | ||
770a1ad5 RK |
1041 | int phylink_ethtool_get_module_info(struct phylink *pl, |
1042 | struct ethtool_modinfo *modinfo) | |
1043 | { | |
1044 | int ret = -EOPNOTSUPP; | |
1045 | ||
1046 | WARN_ON(!lockdep_rtnl_is_held()); | |
1047 | ||
1048 | if (pl->sfp_bus) | |
1049 | ret = sfp_get_module_info(pl->sfp_bus, modinfo); | |
1050 | ||
1051 | return ret; | |
1052 | } | |
1053 | EXPORT_SYMBOL_GPL(phylink_ethtool_get_module_info); | |
1054 | ||
1055 | int phylink_ethtool_get_module_eeprom(struct phylink *pl, | |
1056 | struct ethtool_eeprom *ee, u8 *buf) | |
1057 | { | |
1058 | int ret = -EOPNOTSUPP; | |
1059 | ||
1060 | WARN_ON(!lockdep_rtnl_is_held()); | |
1061 | ||
1062 | if (pl->sfp_bus) | |
1063 | ret = sfp_get_module_eeprom(pl->sfp_bus, ee, buf); | |
1064 | ||
1065 | return ret; | |
1066 | } | |
1067 | EXPORT_SYMBOL_GPL(phylink_ethtool_get_module_eeprom); | |
1068 | ||
9525ae83 RK |
1069 | int phylink_init_eee(struct phylink *pl, bool clk_stop_enable) |
1070 | { | |
1071 | int ret = -EPROTONOSUPPORT; | |
1072 | ||
1073 | WARN_ON(!lockdep_rtnl_is_held()); | |
1074 | ||
1075 | if (pl->phydev) | |
1076 | ret = phy_init_eee(pl->phydev, clk_stop_enable); | |
1077 | ||
1078 | return ret; | |
1079 | } | |
1080 | EXPORT_SYMBOL_GPL(phylink_init_eee); | |
1081 | ||
1082 | int phylink_get_eee_err(struct phylink *pl) | |
1083 | { | |
1084 | int ret = 0; | |
1085 | ||
1086 | WARN_ON(!lockdep_rtnl_is_held()); | |
1087 | ||
1088 | if (pl->phydev) | |
1089 | ret = phy_get_eee_err(pl->phydev); | |
1090 | ||
1091 | return ret; | |
1092 | } | |
1093 | EXPORT_SYMBOL_GPL(phylink_get_eee_err); | |
1094 | ||
1095 | int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee) | |
1096 | { | |
1097 | int ret = -EOPNOTSUPP; | |
1098 | ||
1099 | WARN_ON(!lockdep_rtnl_is_held()); | |
1100 | ||
1101 | if (pl->phydev) | |
1102 | ret = phy_ethtool_get_eee(pl->phydev, eee); | |
1103 | ||
1104 | return ret; | |
1105 | } | |
1106 | EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee); | |
1107 | ||
1108 | int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_eee *eee) | |
1109 | { | |
1110 | int ret = -EOPNOTSUPP; | |
1111 | ||
1112 | WARN_ON(!lockdep_rtnl_is_held()); | |
1113 | ||
1114 | if (pl->phydev) | |
1115 | ret = phy_ethtool_set_eee(pl->phydev, eee); | |
1116 | ||
1117 | return ret; | |
1118 | } | |
1119 | EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee); | |
1120 | ||
1121 | /* This emulates MII registers for a fixed-mode phy operating as per the | |
1122 | * passed in state. "aneg" defines if we report negotiation is possible. | |
1123 | * | |
1124 | * FIXME: should deal with negotiation state too. | |
1125 | */ | |
1126 | static int phylink_mii_emul_read(struct net_device *ndev, unsigned int reg, | |
1127 | struct phylink_link_state *state, bool aneg) | |
1128 | { | |
1129 | struct fixed_phy_status fs; | |
1130 | int val; | |
1131 | ||
1132 | fs.link = state->link; | |
1133 | fs.speed = state->speed; | |
1134 | fs.duplex = state->duplex; | |
1135 | fs.pause = state->pause & MLO_PAUSE_SYM; | |
1136 | fs.asym_pause = state->pause & MLO_PAUSE_ASYM; | |
1137 | ||
1138 | val = swphy_read_reg(reg, &fs); | |
1139 | if (reg == MII_BMSR) { | |
1140 | if (!state->an_complete) | |
1141 | val &= ~BMSR_ANEGCOMPLETE; | |
1142 | if (!aneg) | |
1143 | val &= ~BMSR_ANEGCAPABLE; | |
1144 | } | |
1145 | return val; | |
1146 | } | |
1147 | ||
ecbd87b8 RK |
1148 | static int phylink_phy_read(struct phylink *pl, unsigned int phy_id, |
1149 | unsigned int reg) | |
1150 | { | |
1151 | struct phy_device *phydev = pl->phydev; | |
1152 | int prtad, devad; | |
1153 | ||
1154 | if (mdio_phy_id_is_c45(phy_id)) { | |
1155 | prtad = mdio_phy_id_prtad(phy_id); | |
1156 | devad = mdio_phy_id_devad(phy_id); | |
1157 | devad = MII_ADDR_C45 | devad << 16 | reg; | |
1158 | } else if (phydev->is_c45) { | |
1159 | switch (reg) { | |
1160 | case MII_BMCR: | |
1161 | case MII_BMSR: | |
1162 | case MII_PHYSID1: | |
1163 | case MII_PHYSID2: | |
1164 | devad = __ffs(phydev->c45_ids.devices_in_package); | |
1165 | break; | |
1166 | case MII_ADVERTISE: | |
1167 | case MII_LPA: | |
1168 | if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN)) | |
1169 | return -EINVAL; | |
1170 | devad = MDIO_MMD_AN; | |
1171 | if (reg == MII_ADVERTISE) | |
1172 | reg = MDIO_AN_ADVERTISE; | |
1173 | else | |
1174 | reg = MDIO_AN_LPA; | |
1175 | break; | |
1176 | default: | |
1177 | return -EINVAL; | |
1178 | } | |
1179 | prtad = phy_id; | |
1180 | devad = MII_ADDR_C45 | devad << 16 | reg; | |
1181 | } else { | |
1182 | prtad = phy_id; | |
1183 | devad = reg; | |
1184 | } | |
1185 | return mdiobus_read(pl->phydev->mdio.bus, prtad, devad); | |
1186 | } | |
1187 | ||
1188 | static int phylink_phy_write(struct phylink *pl, unsigned int phy_id, | |
1189 | unsigned int reg, unsigned int val) | |
1190 | { | |
1191 | struct phy_device *phydev = pl->phydev; | |
1192 | int prtad, devad; | |
1193 | ||
1194 | if (mdio_phy_id_is_c45(phy_id)) { | |
1195 | prtad = mdio_phy_id_prtad(phy_id); | |
1196 | devad = mdio_phy_id_devad(phy_id); | |
1197 | devad = MII_ADDR_C45 | devad << 16 | reg; | |
1198 | } else if (phydev->is_c45) { | |
1199 | switch (reg) { | |
1200 | case MII_BMCR: | |
1201 | case MII_BMSR: | |
1202 | case MII_PHYSID1: | |
1203 | case MII_PHYSID2: | |
1204 | devad = __ffs(phydev->c45_ids.devices_in_package); | |
1205 | break; | |
1206 | case MII_ADVERTISE: | |
1207 | case MII_LPA: | |
1208 | if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN)) | |
1209 | return -EINVAL; | |
1210 | devad = MDIO_MMD_AN; | |
1211 | if (reg == MII_ADVERTISE) | |
1212 | reg = MDIO_AN_ADVERTISE; | |
1213 | else | |
1214 | reg = MDIO_AN_LPA; | |
1215 | break; | |
1216 | default: | |
1217 | return -EINVAL; | |
1218 | } | |
1219 | prtad = phy_id; | |
1220 | devad = MII_ADDR_C45 | devad << 16 | reg; | |
1221 | } else { | |
1222 | prtad = phy_id; | |
1223 | devad = reg; | |
1224 | } | |
1225 | ||
1226 | return mdiobus_write(phydev->mdio.bus, prtad, devad, val); | |
1227 | } | |
1228 | ||
9525ae83 RK |
1229 | static int phylink_mii_read(struct phylink *pl, unsigned int phy_id, |
1230 | unsigned int reg) | |
1231 | { | |
1232 | struct phylink_link_state state; | |
1233 | int val = 0xffff; | |
1234 | ||
9525ae83 RK |
1235 | switch (pl->link_an_mode) { |
1236 | case MLO_AN_FIXED: | |
1237 | if (phy_id == 0) { | |
1238 | phylink_get_fixed_state(pl, &state); | |
1239 | val = phylink_mii_emul_read(pl->netdev, reg, &state, | |
1240 | true); | |
1241 | } | |
1242 | break; | |
1243 | ||
1244 | case MLO_AN_PHY: | |
1245 | return -EOPNOTSUPP; | |
1246 | ||
1247 | case MLO_AN_SGMII: | |
1248 | /* No phy, fall through to 8023z method */ | |
1249 | case MLO_AN_8023Z: | |
1250 | if (phy_id == 0) { | |
1251 | val = phylink_get_mac_state(pl, &state); | |
1252 | if (val < 0) | |
1253 | return val; | |
1254 | ||
1255 | val = phylink_mii_emul_read(pl->netdev, reg, &state, | |
1256 | true); | |
1257 | } | |
1258 | break; | |
1259 | } | |
1260 | ||
1261 | return val & 0xffff; | |
1262 | } | |
1263 | ||
1264 | static int phylink_mii_write(struct phylink *pl, unsigned int phy_id, | |
1265 | unsigned int reg, unsigned int val) | |
1266 | { | |
9525ae83 RK |
1267 | switch (pl->link_an_mode) { |
1268 | case MLO_AN_FIXED: | |
1269 | break; | |
1270 | ||
1271 | case MLO_AN_PHY: | |
1272 | return -EOPNOTSUPP; | |
1273 | ||
1274 | case MLO_AN_SGMII: | |
1275 | /* No phy, fall through to 8023z method */ | |
1276 | case MLO_AN_8023Z: | |
1277 | break; | |
1278 | } | |
1279 | ||
1280 | return 0; | |
1281 | } | |
1282 | ||
1283 | int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd) | |
1284 | { | |
ecbd87b8 RK |
1285 | struct mii_ioctl_data *mii = if_mii(ifr); |
1286 | int ret; | |
9525ae83 RK |
1287 | |
1288 | WARN_ON(!lockdep_rtnl_is_held()); | |
1289 | ||
ecbd87b8 RK |
1290 | if (pl->phydev) { |
1291 | /* PHYs only exist for MLO_AN_PHY and MLO_AN_SGMII */ | |
1292 | switch (cmd) { | |
1293 | case SIOCGMIIPHY: | |
1294 | mii->phy_id = pl->phydev->mdio.addr; | |
1295 | ||
1296 | case SIOCGMIIREG: | |
1297 | ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num); | |
1298 | if (ret >= 0) { | |
1299 | mii->val_out = ret; | |
1300 | ret = 0; | |
1301 | } | |
1302 | break; | |
9525ae83 | 1303 | |
ecbd87b8 RK |
1304 | case SIOCSMIIREG: |
1305 | ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num, | |
1306 | mii->val_in); | |
1307 | break; | |
1308 | ||
1309 | default: | |
1310 | ret = phy_mii_ioctl(pl->phydev, ifr, cmd); | |
1311 | break; | |
9525ae83 | 1312 | } |
ecbd87b8 RK |
1313 | } else { |
1314 | switch (cmd) { | |
1315 | case SIOCGMIIPHY: | |
1316 | mii->phy_id = 0; | |
1317 | ||
1318 | case SIOCGMIIREG: | |
1319 | ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num); | |
1320 | if (ret >= 0) { | |
1321 | mii->val_out = ret; | |
1322 | ret = 0; | |
1323 | } | |
1324 | break; | |
9525ae83 | 1325 | |
ecbd87b8 RK |
1326 | case SIOCSMIIREG: |
1327 | ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num, | |
1328 | mii->val_in); | |
1329 | break; | |
9525ae83 | 1330 | |
ecbd87b8 RK |
1331 | default: |
1332 | ret = -EOPNOTSUPP; | |
1333 | break; | |
1334 | } | |
9525ae83 RK |
1335 | } |
1336 | ||
1337 | return ret; | |
1338 | } | |
1339 | EXPORT_SYMBOL_GPL(phylink_mii_ioctl); | |
1340 | ||
ce0aa27f RK |
1341 | static int phylink_sfp_module_insert(void *upstream, |
1342 | const struct sfp_eeprom_id *id) | |
1343 | { | |
1344 | struct phylink *pl = upstream; | |
1345 | __ETHTOOL_DECLARE_LINK_MODE_MASK(support) = { 0, }; | |
1346 | struct phylink_link_state config; | |
1347 | phy_interface_t iface; | |
1348 | int mode, ret = 0; | |
1349 | bool changed; | |
1350 | u8 port; | |
1351 | ||
1352 | sfp_parse_support(pl->sfp_bus, id, support); | |
1353 | port = sfp_parse_port(pl->sfp_bus, id, support); | |
1354 | iface = sfp_parse_interface(pl->sfp_bus, id); | |
1355 | ||
1356 | WARN_ON(!lockdep_rtnl_is_held()); | |
1357 | ||
1358 | switch (iface) { | |
1359 | case PHY_INTERFACE_MODE_SGMII: | |
1360 | mode = MLO_AN_SGMII; | |
1361 | break; | |
1362 | case PHY_INTERFACE_MODE_1000BASEX: | |
1363 | mode = MLO_AN_8023Z; | |
1364 | break; | |
1365 | default: | |
1366 | return -EINVAL; | |
1367 | } | |
1368 | ||
1369 | memset(&config, 0, sizeof(config)); | |
1370 | linkmode_copy(config.advertising, support); | |
1371 | config.interface = iface; | |
1372 | config.speed = SPEED_UNKNOWN; | |
1373 | config.duplex = DUPLEX_UNKNOWN; | |
1374 | config.pause = MLO_PAUSE_AN; | |
1375 | config.an_enabled = pl->link_config.an_enabled; | |
1376 | ||
1377 | /* Ignore errors if we're expecting a PHY to attach later */ | |
1378 | ret = phylink_validate(pl, support, &config); | |
1379 | if (ret) { | |
1380 | netdev_err(pl->netdev, "validation of %s/%s with support %*pb failed: %d\n", | |
1381 | phylink_an_mode_str(mode), phy_modes(config.interface), | |
1382 | __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret); | |
1383 | return ret; | |
1384 | } | |
1385 | ||
1386 | netdev_dbg(pl->netdev, "requesting link mode %s/%s with support %*pb\n", | |
1387 | phylink_an_mode_str(mode), phy_modes(config.interface), | |
1388 | __ETHTOOL_LINK_MODE_MASK_NBITS, support); | |
1389 | ||
1390 | if (mode == MLO_AN_8023Z && pl->phydev) | |
1391 | return -EINVAL; | |
1392 | ||
1393 | changed = !bitmap_equal(pl->supported, support, | |
1394 | __ETHTOOL_LINK_MODE_MASK_NBITS); | |
1395 | if (changed) { | |
1396 | linkmode_copy(pl->supported, support); | |
1397 | linkmode_copy(pl->link_config.advertising, config.advertising); | |
1398 | } | |
1399 | ||
1400 | if (pl->link_an_mode != mode || | |
1401 | pl->link_config.interface != config.interface) { | |
1402 | pl->link_config.interface = config.interface; | |
1403 | pl->link_an_mode = mode; | |
1404 | ||
1405 | changed = true; | |
1406 | ||
1407 | netdev_info(pl->netdev, "switched to %s/%s link mode\n", | |
1408 | phylink_an_mode_str(mode), | |
1409 | phy_modes(config.interface)); | |
1410 | } | |
1411 | ||
1412 | pl->link_port = port; | |
1413 | ||
1414 | if (changed && !test_bit(PHYLINK_DISABLE_STOPPED, | |
1415 | &pl->phylink_disable_state)) | |
1416 | phylink_mac_config(pl, &pl->link_config); | |
1417 | ||
1418 | return ret; | |
1419 | } | |
1420 | ||
1421 | static void phylink_sfp_link_down(void *upstream) | |
1422 | { | |
1423 | struct phylink *pl = upstream; | |
1424 | ||
1425 | WARN_ON(!lockdep_rtnl_is_held()); | |
1426 | ||
1427 | set_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state); | |
1428 | flush_work(&pl->resolve); | |
1429 | ||
1430 | netif_carrier_off(pl->netdev); | |
1431 | } | |
1432 | ||
1433 | static void phylink_sfp_link_up(void *upstream) | |
1434 | { | |
1435 | struct phylink *pl = upstream; | |
1436 | ||
1437 | WARN_ON(!lockdep_rtnl_is_held()); | |
1438 | ||
1439 | clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state); | |
1440 | phylink_run_resolve(pl); | |
1441 | } | |
1442 | ||
1443 | static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy) | |
1444 | { | |
1445 | return phylink_connect_phy(upstream, phy); | |
1446 | } | |
1447 | ||
1448 | static void phylink_sfp_disconnect_phy(void *upstream) | |
1449 | { | |
1450 | phylink_disconnect_phy(upstream); | |
1451 | } | |
1452 | ||
1453 | static const struct sfp_upstream_ops sfp_phylink_ops = { | |
1454 | .module_insert = phylink_sfp_module_insert, | |
1455 | .link_up = phylink_sfp_link_up, | |
1456 | .link_down = phylink_sfp_link_down, | |
1457 | .connect_phy = phylink_sfp_connect_phy, | |
1458 | .disconnect_phy = phylink_sfp_disconnect_phy, | |
1459 | }; | |
1460 | ||
9525ae83 | 1461 | MODULE_LICENSE("GPL"); |