Commit | Line | Data |
---|---|---|
9525ae83 RK |
1 | #ifndef NETDEV_PCS_H |
2 | #define NETDEV_PCS_H | |
3 | ||
4 | #include <linux/phy.h> | |
5 | #include <linux/spinlock.h> | |
6 | #include <linux/workqueue.h> | |
7 | ||
8 | struct device_node; | |
9 | struct ethtool_cmd; | |
8fa7b9b6 | 10 | struct fwnode_handle; |
9525ae83 RK |
11 | struct net_device; |
12 | ||
13 | enum { | |
14 | MLO_PAUSE_NONE, | |
4e5aeb41 RK |
15 | MLO_PAUSE_RX = BIT(0), |
16 | MLO_PAUSE_TX = BIT(1), | |
9525ae83 | 17 | MLO_PAUSE_TXRX_MASK = MLO_PAUSE_TX | MLO_PAUSE_RX, |
4e5aeb41 | 18 | MLO_PAUSE_AN = BIT(2), |
9525ae83 RK |
19 | |
20 | MLO_AN_PHY = 0, /* Conventional PHY */ | |
21 | MLO_AN_FIXED, /* Fixed-link mode */ | |
86a362c4 | 22 | MLO_AN_INBAND, /* In-band protocol */ |
9525ae83 RK |
23 | }; |
24 | ||
25 | static inline bool phylink_autoneg_inband(unsigned int mode) | |
26 | { | |
86a362c4 | 27 | return mode == MLO_AN_INBAND; |
9525ae83 RK |
28 | } |
29 | ||
8796c892 RK |
30 | /** |
31 | * struct phylink_link_state - link state structure | |
32 | * @advertising: ethtool bitmask containing advertised link modes | |
33 | * @lp_advertising: ethtool bitmask containing link partner advertised link | |
34 | * modes | |
35 | * @interface: link &typedef phy_interface_t mode | |
36 | * @speed: link speed, one of the SPEED_* constants. | |
37 | * @duplex: link duplex mode, one of DUPLEX_* constants. | |
38 | * @pause: link pause state, described by MLO_PAUSE_* constants. | |
39 | * @link: true if the link is up. | |
40 | * @an_enabled: true if autonegotiation is enabled/desired. | |
41 | * @an_complete: true if autonegotiation has completed. | |
42 | */ | |
9525ae83 RK |
43 | struct phylink_link_state { |
44 | __ETHTOOL_DECLARE_LINK_MODE_MASK(advertising); | |
45 | __ETHTOOL_DECLARE_LINK_MODE_MASK(lp_advertising); | |
8796c892 | 46 | phy_interface_t interface; |
9525ae83 RK |
47 | int speed; |
48 | int duplex; | |
49 | int pause; | |
50 | unsigned int link:1; | |
51 | unsigned int an_enabled:1; | |
52 | unsigned int an_complete:1; | |
53 | }; | |
54 | ||
44cc27e4 IC |
55 | enum phylink_op_type { |
56 | PHYLINK_NETDEV = 0, | |
43de6195 | 57 | PHYLINK_DEV, |
44cc27e4 IC |
58 | }; |
59 | ||
60 | /** | |
61 | * struct phylink_config - PHYLINK configuration structure | |
62 | * @dev: a pointer to a struct device associated with the MAC | |
63 | * @type: operation type of PHYLINK instance | |
1511ed0a | 64 | * @pcs_poll: MAC PCS cannot provide link change interrupt |
44cc27e4 IC |
65 | */ |
66 | struct phylink_config { | |
67 | struct device *dev; | |
68 | enum phylink_op_type type; | |
1511ed0a | 69 | bool pcs_poll; |
5c05c1db RK |
70 | bool poll_fixed_state; |
71 | void (*get_fixed_state)(struct phylink_config *config, | |
72 | struct phylink_link_state *state); | |
44cc27e4 IC |
73 | }; |
74 | ||
8796c892 RK |
75 | /** |
76 | * struct phylink_mac_ops - MAC operations structure. | |
77 | * @validate: Validate and update the link configuration. | |
d46b7e4f | 78 | * @mac_pcs_get_state: Read the current link state from the hardware. |
8796c892 RK |
79 | * @mac_config: configure the MAC for the selected mode and state. |
80 | * @mac_an_restart: restart 802.3z BaseX autonegotiation. | |
81 | * @mac_link_down: take the link down. | |
82 | * @mac_link_up: allow the link to come up. | |
83 | * | |
84 | * The individual methods are described more fully below. | |
85 | */ | |
9525ae83 | 86 | struct phylink_mac_ops { |
44cc27e4 IC |
87 | void (*validate)(struct phylink_config *config, |
88 | unsigned long *supported, | |
9525ae83 | 89 | struct phylink_link_state *state); |
d46b7e4f RK |
90 | void (*mac_pcs_get_state)(struct phylink_config *config, |
91 | struct phylink_link_state *state); | |
44cc27e4 | 92 | void (*mac_config)(struct phylink_config *config, unsigned int mode, |
9525ae83 | 93 | const struct phylink_link_state *state); |
44cc27e4 IC |
94 | void (*mac_an_restart)(struct phylink_config *config); |
95 | void (*mac_link_down)(struct phylink_config *config, unsigned int mode, | |
c6ab3008 | 96 | phy_interface_t interface); |
91a208f2 RK |
97 | void (*mac_link_up)(struct phylink_config *config, |
98 | struct phy_device *phy, unsigned int mode, | |
99 | phy_interface_t interface, int speed, int duplex, | |
100 | bool tx_pause, bool rx_pause); | |
9525ae83 RK |
101 | }; |
102 | ||
8796c892 RK |
103 | #if 0 /* For kernel-doc purposes only. */ |
104 | /** | |
105 | * validate - Validate and update the link configuration | |
44cc27e4 | 106 | * @config: a pointer to a &struct phylink_config. |
8796c892 RK |
107 | * @supported: ethtool bitmask for supported link modes. |
108 | * @state: a pointer to a &struct phylink_link_state. | |
109 | * | |
110 | * Clear bits in the @supported and @state->advertising masks that | |
111 | * are not supportable by the MAC. | |
112 | * | |
113 | * Note that the PHY may be able to transform from one connection | |
114 | * technology to another, so, eg, don't clear 1000BaseX just | |
115 | * because the MAC is unable to BaseX mode. This is more about | |
32e454ef RK |
116 | * clearing unsupported speeds and duplex settings. The port modes |
117 | * should not be cleared; phylink_set_port_modes() will help with this. | |
8796c892 RK |
118 | * |
119 | * If the @state->interface mode is %PHY_INTERFACE_MODE_1000BASEX | |
120 | * or %PHY_INTERFACE_MODE_2500BASEX, select the appropriate mode | |
121 | * based on @state->advertising and/or @state->speed and update | |
32e454ef RK |
122 | * @state->interface accordingly. See phylink_helper_basex_speed(). |
123 | * | |
124 | * When @state->interface is %PHY_INTERFACE_MODE_NA, phylink expects the | |
125 | * MAC driver to return all supported link modes. | |
126 | * | |
127 | * If the @state->interface mode is not supported, then the @supported | |
128 | * mask must be cleared. | |
8796c892 | 129 | */ |
44cc27e4 | 130 | void validate(struct phylink_config *config, unsigned long *supported, |
8796c892 RK |
131 | struct phylink_link_state *state); |
132 | ||
133 | /** | |
d46b7e4f | 134 | * mac_pcs_get_state() - Read the current inband link state from the hardware |
44cc27e4 | 135 | * @config: a pointer to a &struct phylink_config. |
8796c892 RK |
136 | * @state: a pointer to a &struct phylink_link_state. |
137 | * | |
d46b7e4f RK |
138 | * Read the current inband link state from the MAC PCS, reporting the |
139 | * current speed in @state->speed, duplex mode in @state->duplex, pause | |
140 | * mode in @state->pause using the %MLO_PAUSE_RX and %MLO_PAUSE_TX bits, | |
141 | * negotiation completion state in @state->an_complete, and link up state | |
142 | * in @state->link. If possible, @state->lp_advertising should also be | |
143 | * populated. | |
8796c892 | 144 | */ |
d46b7e4f RK |
145 | void mac_pcs_get_state(struct phylink_config *config, |
146 | struct phylink_link_state *state); | |
8796c892 RK |
147 | |
148 | /** | |
149 | * mac_config() - configure the MAC for the selected mode and state | |
44cc27e4 | 150 | * @config: a pointer to a &struct phylink_config. |
8796c892 RK |
151 | * @mode: one of %MLO_AN_FIXED, %MLO_AN_PHY, %MLO_AN_INBAND. |
152 | * @state: a pointer to a &struct phylink_link_state. | |
153 | * | |
4add7009 RKAL |
154 | * Note - not all members of @state are valid. In particular, |
155 | * @state->lp_advertising, @state->link, @state->an_complete are never | |
156 | * guaranteed to be correct, and so any mac_config() implementation must | |
157 | * never reference these fields. | |
158 | * | |
91a208f2 RK |
159 | * (this requires a rewrite - please refer to mac_link_up() for situations |
160 | * where the PCS and MAC are not tightly integrated.) | |
161 | * | |
b70486f9 RK |
162 | * In all negotiation modes, as defined by @mode, @state->pause indicates the |
163 | * pause settings which should be applied as follows. If %MLO_PAUSE_AN is not | |
164 | * set, %MLO_PAUSE_TX and %MLO_PAUSE_RX indicate whether the MAC should send | |
165 | * pause frames and/or act on received pause frames respectively. Otherwise, | |
166 | * the results of in-band negotiation/status from the MAC PCS should be used | |
167 | * to control the MAC pause mode settings. | |
168 | * | |
8796c892 RK |
169 | * The action performed depends on the currently selected mode: |
170 | * | |
171 | * %MLO_AN_FIXED, %MLO_AN_PHY: | |
91a208f2 RK |
172 | * Configure for non-inband negotiation mode, where the link settings |
173 | * are completely communicated via mac_link_up(). The physical link | |
174 | * protocol from the MAC is specified by @state->interface. | |
175 | * | |
176 | * @state->advertising may be used, but is not required. | |
177 | * | |
178 | * Older drivers (prior to the mac_link_up() change) may use @state->speed, | |
179 | * @state->duplex and @state->pause to configure the MAC, but this is | |
180 | * deprecated; such drivers should be converted to use mac_link_up(). | |
4add7009 | 181 | * |
91a208f2 RK |
182 | * Other members of @state must be ignored. |
183 | * | |
184 | * Valid state members: interface, advertising. | |
185 | * Deprecated state members: speed, duplex, pause. | |
8796c892 RK |
186 | * |
187 | * %MLO_AN_INBAND: | |
188 | * place the link in an inband negotiation mode (such as 802.3z | |
189 | * 1000base-X or Cisco SGMII mode depending on the @state->interface | |
190 | * mode). In both cases, link state management (whether the link | |
191 | * is up or not) is performed by the MAC, and reported via the | |
d46b7e4f | 192 | * mac_pcs_get_state() callback. Changes in link state must be made |
8796c892 RK |
193 | * by calling phylink_mac_change(). |
194 | * | |
b70486f9 RK |
195 | * Interface mode specific details are mentioned below. |
196 | * | |
8796c892 | 197 | * If in 802.3z mode, the link speed is fixed, dependent on the |
b70486f9 RK |
198 | * @state->interface. Duplex and pause modes are negotiated via |
199 | * the in-band configuration word. Advertised pause modes are set | |
200 | * according to the @state->an_enabled and @state->advertising | |
201 | * flags. Beware of MACs which only support full duplex at gigabit | |
202 | * and higher speeds. | |
8796c892 RK |
203 | * |
204 | * If in Cisco SGMII mode, the link speed and duplex mode are passed | |
205 | * in the serial bitstream 16-bit configuration word, and the MAC | |
206 | * should be configured to read these bits and acknowledge the | |
207 | * configuration word. Nothing is advertised by the MAC. The MAC is | |
208 | * responsible for reading the configuration word and configuring | |
209 | * itself accordingly. | |
0e29ae03 | 210 | * |
4add7009 RKAL |
211 | * Valid state members: interface, an_enabled, pause, advertising. |
212 | * | |
0e29ae03 RK |
213 | * Implementations are expected to update the MAC to reflect the |
214 | * requested settings - i.o.w., if nothing has changed between two | |
215 | * calls, no action is expected. If only flow control settings have | |
216 | * changed, flow control should be updated *without* taking the link | |
217 | * down. This "update" behaviour is critical to avoid bouncing the | |
218 | * link up status. | |
8796c892 | 219 | */ |
44cc27e4 | 220 | void mac_config(struct phylink_config *config, unsigned int mode, |
8796c892 RK |
221 | const struct phylink_link_state *state); |
222 | ||
223 | /** | |
224 | * mac_an_restart() - restart 802.3z BaseX autonegotiation | |
44cc27e4 | 225 | * @config: a pointer to a &struct phylink_config. |
8796c892 | 226 | */ |
44cc27e4 | 227 | void mac_an_restart(struct phylink_config *config); |
8796c892 RK |
228 | |
229 | /** | |
230 | * mac_link_down() - take the link down | |
44cc27e4 | 231 | * @config: a pointer to a &struct phylink_config. |
8796c892 | 232 | * @mode: link autonegotiation mode |
c6ab3008 | 233 | * @interface: link &typedef phy_interface_t mode |
8796c892 RK |
234 | * |
235 | * If @mode is not an in-band negotiation mode (as defined by | |
236 | * phylink_autoneg_inband()), force the link down and disable any | |
c6ab3008 FF |
237 | * Energy Efficient Ethernet MAC configuration. Interface type |
238 | * selection must be done in mac_config(). | |
8796c892 | 239 | */ |
44cc27e4 | 240 | void mac_link_down(struct phylink_config *config, unsigned int mode, |
c6ab3008 | 241 | phy_interface_t interface); |
8796c892 RK |
242 | |
243 | /** | |
244 | * mac_link_up() - allow the link to come up | |
44cc27e4 | 245 | * @config: a pointer to a &struct phylink_config. |
91a208f2 | 246 | * @phy: any attached phy |
8796c892 | 247 | * @mode: link autonegotiation mode |
c6ab3008 | 248 | * @interface: link &typedef phy_interface_t mode |
91a208f2 RK |
249 | * @speed: link speed |
250 | * @duplex: link duplex | |
251 | * @tx_pause: link transmit pause enablement status | |
252 | * @rx_pause: link receive pause enablement status | |
8796c892 | 253 | * |
91a208f2 RK |
254 | * Configure the MAC for an established link. |
255 | * | |
256 | * @speed, @duplex, @tx_pause and @rx_pause indicate the finalised link | |
257 | * settings, and should be used to configure the MAC block appropriately | |
258 | * where these settings are not automatically conveyed from the PCS block, | |
259 | * or if in-band negotiation (as defined by phylink_autoneg_inband(@mode)) | |
260 | * is disabled. | |
261 | * | |
262 | * Note that when 802.3z in-band negotiation is in use, it is possible | |
263 | * that the user wishes to override the pause settings, and this should | |
264 | * be allowed when considering the implementation of this method. | |
265 | * | |
266 | * If in-band negotiation mode is disabled, allow the link to come up. If | |
267 | * @phy is non-%NULL, configure Energy Efficient Ethernet by calling | |
8796c892 | 268 | * phy_init_eee() and perform appropriate MAC configuration for EEE. |
c6ab3008 | 269 | * Interface type selection must be done in mac_config(). |
8796c892 | 270 | */ |
91a208f2 RK |
271 | void mac_link_up(struct phylink_config *config, struct phy_device *phy, |
272 | unsigned int mode, phy_interface_t interface, | |
273 | int speed, int duplex, bool tx_pause, bool rx_pause); | |
8796c892 RK |
274 | #endif |
275 | ||
4c0d6d3a RK |
276 | /** |
277 | * struct phylink_pcs_ops - MAC PCS operations structure. | |
278 | * @pcs_get_state: read the current MAC PCS link state from the hardware. | |
279 | * @pcs_config: configure the MAC PCS for the selected mode and state. | |
280 | * @pcs_an_restart: restart 802.3z BaseX autonegotiation. | |
281 | * @pcs_link_up: program the PCS for the resolved link configuration | |
282 | * (where necessary). | |
283 | */ | |
284 | struct phylink_pcs_ops { | |
285 | void (*pcs_get_state)(struct phylink_config *config, | |
286 | struct phylink_link_state *state); | |
287 | int (*pcs_config)(struct phylink_config *config, unsigned int mode, | |
288 | phy_interface_t interface, | |
289 | const unsigned long *advertising); | |
290 | void (*pcs_an_restart)(struct phylink_config *config); | |
291 | void (*pcs_link_up)(struct phylink_config *config, unsigned int mode, | |
292 | phy_interface_t interface, int speed, int duplex); | |
293 | }; | |
294 | ||
295 | #if 0 /* For kernel-doc purposes only. */ | |
296 | /** | |
297 | * pcs_get_state() - Read the current inband link state from the hardware | |
298 | * @config: a pointer to a &struct phylink_config. | |
299 | * @state: a pointer to a &struct phylink_link_state. | |
300 | * | |
301 | * Read the current inband link state from the MAC PCS, reporting the | |
302 | * current speed in @state->speed, duplex mode in @state->duplex, pause | |
303 | * mode in @state->pause using the %MLO_PAUSE_RX and %MLO_PAUSE_TX bits, | |
304 | * negotiation completion state in @state->an_complete, and link up state | |
305 | * in @state->link. If possible, @state->lp_advertising should also be | |
306 | * populated. | |
307 | * | |
308 | * When present, this overrides mac_pcs_get_state() in &struct | |
309 | * phylink_mac_ops. | |
310 | */ | |
311 | void pcs_get_state(struct phylink_config *config, | |
312 | struct phylink_link_state *state); | |
313 | ||
314 | /** | |
315 | * pcs_config() - Configure the PCS mode and advertisement | |
316 | * @config: a pointer to a &struct phylink_config. | |
317 | * @mode: one of %MLO_AN_FIXED, %MLO_AN_PHY, %MLO_AN_INBAND. | |
318 | * @interface: interface mode to be used | |
319 | * @advertising: adertisement ethtool link mode mask | |
320 | * | |
321 | * Configure the PCS for the operating mode, the interface mode, and set | |
322 | * the advertisement mask. | |
323 | * | |
324 | * When operating in %MLO_AN_INBAND, inband should always be enabled, | |
325 | * otherwise inband should be disabled. | |
326 | * | |
327 | * For SGMII, there is no advertisement from the MAC side, the PCS should | |
328 | * be programmed to acknowledge the inband word from the PHY. | |
329 | * | |
330 | * For 1000BASE-X, the advertisement should be programmed into the PCS. | |
331 | * | |
332 | * For most 10GBASE-R, there is no advertisement. | |
333 | */ | |
334 | int (*pcs_config)(struct phylink_config *config, unsigned int mode, | |
335 | phy_interface_t interface, const unsigned long *advertising); | |
336 | ||
337 | /** | |
338 | * pcs_an_restart() - restart 802.3z BaseX autonegotiation | |
339 | * @config: a pointer to a &struct phylink_config. | |
340 | * | |
341 | * When PCS ops are present, this overrides mac_an_restart() in &struct | |
342 | * phylink_mac_ops. | |
343 | */ | |
344 | void (*pcs_an_restart)(struct phylink_config *config); | |
345 | ||
346 | /** | |
347 | * pcs_link_up() - program the PCS for the resolved link configuration | |
348 | * @config: a pointer to a &struct phylink_config. | |
349 | * @mode: link autonegotiation mode | |
350 | * @interface: link &typedef phy_interface_t mode | |
351 | * @speed: link speed | |
352 | * @duplex: link duplex | |
353 | * | |
354 | * This call will be made just before mac_link_up() to inform the PCS of | |
355 | * the resolved link parameters. For example, a PCS operating in SGMII | |
356 | * mode without in-band AN needs to be manually configured for the link | |
357 | * and duplex setting. Otherwise, this should be a no-op. | |
358 | */ | |
359 | void (*pcs_link_up)(struct phylink_config *config, unsigned int mode, | |
360 | phy_interface_t interface, int speed, int duplex); | |
361 | #endif | |
362 | ||
44cc27e4 IC |
363 | struct phylink *phylink_create(struct phylink_config *, struct fwnode_handle *, |
364 | phy_interface_t iface, | |
e7765d63 | 365 | const struct phylink_mac_ops *mac_ops); |
4c0d6d3a | 366 | void phylink_add_pcs(struct phylink *, const struct phylink_pcs_ops *ops); |
9525ae83 RK |
367 | void phylink_destroy(struct phylink *); |
368 | ||
369 | int phylink_connect_phy(struct phylink *, struct phy_device *); | |
0a62964c | 370 | int phylink_of_phy_connect(struct phylink *, struct device_node *, u32 flags); |
9525ae83 RK |
371 | void phylink_disconnect_phy(struct phylink *); |
372 | ||
373 | void phylink_mac_change(struct phylink *, bool up); | |
374 | ||
375 | void phylink_start(struct phylink *); | |
376 | void phylink_stop(struct phylink *); | |
377 | ||
378 | void phylink_ethtool_get_wol(struct phylink *, struct ethtool_wolinfo *); | |
379 | int phylink_ethtool_set_wol(struct phylink *, struct ethtool_wolinfo *); | |
380 | ||
381 | int phylink_ethtool_ksettings_get(struct phylink *, | |
382 | struct ethtool_link_ksettings *); | |
383 | int phylink_ethtool_ksettings_set(struct phylink *, | |
384 | const struct ethtool_link_ksettings *); | |
385 | int phylink_ethtool_nway_reset(struct phylink *); | |
386 | void phylink_ethtool_get_pauseparam(struct phylink *, | |
387 | struct ethtool_pauseparam *); | |
388 | int phylink_ethtool_set_pauseparam(struct phylink *, | |
389 | struct ethtool_pauseparam *); | |
9525ae83 | 390 | int phylink_get_eee_err(struct phylink *); |
86e58135 | 391 | int phylink_init_eee(struct phylink *, bool); |
9525ae83 RK |
392 | int phylink_ethtool_get_eee(struct phylink *, struct ethtool_eee *); |
393 | int phylink_ethtool_set_eee(struct phylink *, struct ethtool_eee *); | |
394 | int phylink_mii_ioctl(struct phylink *, struct ifreq *, int); | |
c6d5d843 RK |
395 | int phylink_speed_down(struct phylink *pl, bool sync); |
396 | int phylink_speed_up(struct phylink *pl); | |
9525ae83 RK |
397 | |
398 | #define phylink_zero(bm) \ | |
399 | bitmap_zero(bm, __ETHTOOL_LINK_MODE_MASK_NBITS) | |
400 | #define __phylink_do_bit(op, bm, mode) \ | |
401 | op(ETHTOOL_LINK_MODE_ ## mode ## _BIT, bm) | |
402 | ||
403 | #define phylink_set(bm, mode) __phylink_do_bit(__set_bit, bm, mode) | |
404 | #define phylink_clear(bm, mode) __phylink_do_bit(__clear_bit, bm, mode) | |
405 | #define phylink_test(bm, mode) __phylink_do_bit(test_bit, bm, mode) | |
406 | ||
407 | void phylink_set_port_modes(unsigned long *bits); | |
624c0f02 | 408 | void phylink_helper_basex_speed(struct phylink_link_state *state); |
9525ae83 | 409 | |
74db1c18 RK |
410 | void phylink_mii_c22_pcs_get_state(struct mdio_device *pcs, |
411 | struct phylink_link_state *state); | |
412 | int phylink_mii_c22_pcs_set_advertisement(struct mdio_device *pcs, | |
0bd27406 RK |
413 | phy_interface_t interface, |
414 | const unsigned long *advertising); | |
74db1c18 RK |
415 | void phylink_mii_c22_pcs_an_restart(struct mdio_device *pcs); |
416 | ||
b8679ef8 RK |
417 | void phylink_mii_c45_pcs_get_state(struct mdio_device *pcs, |
418 | struct phylink_link_state *state); | |
9525ae83 | 419 | #endif |