Merge branch 'topic/tasklet-convert' into for-linus
[linux-block.git] / include / linux / phylink.h
CommitLineData
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
8struct device_node;
9struct ethtool_cmd;
8fa7b9b6 10struct fwnode_handle;
9525ae83
RK
11struct net_device;
12
13enum {
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
25static 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
43struct 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
55enum 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
5d682f5e
MCC
65 * @poll_fixed_state: if true, starts link_poll,
66 * if MAC link is at %MLO_AN_FIXED mode.
67 * @get_fixed_state: callback to execute to determine the fixed link state,
68 * if MAC link is at %MLO_AN_FIXED mode.
44cc27e4
IC
69 */
70struct phylink_config {
71 struct device *dev;
72 enum phylink_op_type type;
1511ed0a 73 bool pcs_poll;
5c05c1db
RK
74 bool poll_fixed_state;
75 void (*get_fixed_state)(struct phylink_config *config,
76 struct phylink_link_state *state);
44cc27e4
IC
77};
78
8796c892
RK
79/**
80 * struct phylink_mac_ops - MAC operations structure.
81 * @validate: Validate and update the link configuration.
d46b7e4f 82 * @mac_pcs_get_state: Read the current link state from the hardware.
b7ad14c2 83 * @mac_prepare: prepare for a major reconfiguration of the interface.
8796c892 84 * @mac_config: configure the MAC for the selected mode and state.
b7ad14c2 85 * @mac_finish: finish a major reconfiguration of the interface.
8796c892
RK
86 * @mac_an_restart: restart 802.3z BaseX autonegotiation.
87 * @mac_link_down: take the link down.
88 * @mac_link_up: allow the link to come up.
89 *
90 * The individual methods are described more fully below.
91 */
9525ae83 92struct phylink_mac_ops {
44cc27e4
IC
93 void (*validate)(struct phylink_config *config,
94 unsigned long *supported,
9525ae83 95 struct phylink_link_state *state);
d46b7e4f
RK
96 void (*mac_pcs_get_state)(struct phylink_config *config,
97 struct phylink_link_state *state);
b7ad14c2
RK
98 int (*mac_prepare)(struct phylink_config *config, unsigned int mode,
99 phy_interface_t iface);
44cc27e4 100 void (*mac_config)(struct phylink_config *config, unsigned int mode,
9525ae83 101 const struct phylink_link_state *state);
b7ad14c2
RK
102 int (*mac_finish)(struct phylink_config *config, unsigned int mode,
103 phy_interface_t iface);
44cc27e4
IC
104 void (*mac_an_restart)(struct phylink_config *config);
105 void (*mac_link_down)(struct phylink_config *config, unsigned int mode,
c6ab3008 106 phy_interface_t interface);
91a208f2
RK
107 void (*mac_link_up)(struct phylink_config *config,
108 struct phy_device *phy, unsigned int mode,
109 phy_interface_t interface, int speed, int duplex,
110 bool tx_pause, bool rx_pause);
9525ae83
RK
111};
112
8796c892
RK
113#if 0 /* For kernel-doc purposes only. */
114/**
115 * validate - Validate and update the link configuration
44cc27e4 116 * @config: a pointer to a &struct phylink_config.
8796c892
RK
117 * @supported: ethtool bitmask for supported link modes.
118 * @state: a pointer to a &struct phylink_link_state.
119 *
120 * Clear bits in the @supported and @state->advertising masks that
121 * are not supportable by the MAC.
122 *
123 * Note that the PHY may be able to transform from one connection
124 * technology to another, so, eg, don't clear 1000BaseX just
125 * because the MAC is unable to BaseX mode. This is more about
32e454ef
RK
126 * clearing unsupported speeds and duplex settings. The port modes
127 * should not be cleared; phylink_set_port_modes() will help with this.
8796c892
RK
128 *
129 * If the @state->interface mode is %PHY_INTERFACE_MODE_1000BASEX
130 * or %PHY_INTERFACE_MODE_2500BASEX, select the appropriate mode
131 * based on @state->advertising and/or @state->speed and update
32e454ef
RK
132 * @state->interface accordingly. See phylink_helper_basex_speed().
133 *
134 * When @state->interface is %PHY_INTERFACE_MODE_NA, phylink expects the
135 * MAC driver to return all supported link modes.
136 *
137 * If the @state->interface mode is not supported, then the @supported
138 * mask must be cleared.
8796c892 139 */
44cc27e4 140void validate(struct phylink_config *config, unsigned long *supported,
8796c892
RK
141 struct phylink_link_state *state);
142
143/**
d46b7e4f 144 * mac_pcs_get_state() - Read the current inband link state from the hardware
44cc27e4 145 * @config: a pointer to a &struct phylink_config.
8796c892
RK
146 * @state: a pointer to a &struct phylink_link_state.
147 *
d46b7e4f
RK
148 * Read the current inband link state from the MAC PCS, reporting the
149 * current speed in @state->speed, duplex mode in @state->duplex, pause
150 * mode in @state->pause using the %MLO_PAUSE_RX and %MLO_PAUSE_TX bits,
151 * negotiation completion state in @state->an_complete, and link up state
152 * in @state->link. If possible, @state->lp_advertising should also be
153 * populated.
8796c892 154 */
d46b7e4f
RK
155void mac_pcs_get_state(struct phylink_config *config,
156 struct phylink_link_state *state);
8796c892 157
b7ad14c2
RK
158/**
159 * mac_prepare() - prepare to change the PHY interface mode
160 * @config: a pointer to a &struct phylink_config.
161 * @mode: one of %MLO_AN_FIXED, %MLO_AN_PHY, %MLO_AN_INBAND.
162 * @iface: interface mode to switch to
163 *
164 * phylink will call this method at the beginning of a full initialisation
165 * of the link, which includes changing the interface mode or at initial
166 * startup time. It may be called for the current mode. The MAC driver
167 * should perform whatever actions are required, e.g. disabling the
168 * Serdes PHY.
169 *
170 * This will be the first call in the sequence:
171 * - mac_prepare()
172 * - mac_config()
173 * - pcs_config()
174 * - possible pcs_an_restart()
175 * - mac_finish()
176 *
177 * Returns zero on success, or negative errno on failure which will be
178 * reported to the kernel log.
179 */
180int mac_prepare(struct phylink_config *config, unsigned int mode,
181 phy_interface_t iface);
182
8796c892
RK
183/**
184 * mac_config() - configure the MAC for the selected mode and state
44cc27e4 185 * @config: a pointer to a &struct phylink_config.
8796c892
RK
186 * @mode: one of %MLO_AN_FIXED, %MLO_AN_PHY, %MLO_AN_INBAND.
187 * @state: a pointer to a &struct phylink_link_state.
188 *
4add7009
RKAL
189 * Note - not all members of @state are valid. In particular,
190 * @state->lp_advertising, @state->link, @state->an_complete are never
191 * guaranteed to be correct, and so any mac_config() implementation must
192 * never reference these fields.
193 *
91a208f2
RK
194 * (this requires a rewrite - please refer to mac_link_up() for situations
195 * where the PCS and MAC are not tightly integrated.)
196 *
b70486f9
RK
197 * In all negotiation modes, as defined by @mode, @state->pause indicates the
198 * pause settings which should be applied as follows. If %MLO_PAUSE_AN is not
199 * set, %MLO_PAUSE_TX and %MLO_PAUSE_RX indicate whether the MAC should send
200 * pause frames and/or act on received pause frames respectively. Otherwise,
201 * the results of in-band negotiation/status from the MAC PCS should be used
202 * to control the MAC pause mode settings.
203 *
8796c892
RK
204 * The action performed depends on the currently selected mode:
205 *
206 * %MLO_AN_FIXED, %MLO_AN_PHY:
91a208f2
RK
207 * Configure for non-inband negotiation mode, where the link settings
208 * are completely communicated via mac_link_up(). The physical link
209 * protocol from the MAC is specified by @state->interface.
210 *
211 * @state->advertising may be used, but is not required.
212 *
213 * Older drivers (prior to the mac_link_up() change) may use @state->speed,
214 * @state->duplex and @state->pause to configure the MAC, but this is
215 * deprecated; such drivers should be converted to use mac_link_up().
4add7009 216 *
91a208f2
RK
217 * Other members of @state must be ignored.
218 *
219 * Valid state members: interface, advertising.
220 * Deprecated state members: speed, duplex, pause.
8796c892
RK
221 *
222 * %MLO_AN_INBAND:
223 * place the link in an inband negotiation mode (such as 802.3z
224 * 1000base-X or Cisco SGMII mode depending on the @state->interface
225 * mode). In both cases, link state management (whether the link
226 * is up or not) is performed by the MAC, and reported via the
d46b7e4f 227 * mac_pcs_get_state() callback. Changes in link state must be made
8796c892
RK
228 * by calling phylink_mac_change().
229 *
b70486f9
RK
230 * Interface mode specific details are mentioned below.
231 *
8796c892 232 * If in 802.3z mode, the link speed is fixed, dependent on the
b70486f9
RK
233 * @state->interface. Duplex and pause modes are negotiated via
234 * the in-band configuration word. Advertised pause modes are set
235 * according to the @state->an_enabled and @state->advertising
236 * flags. Beware of MACs which only support full duplex at gigabit
237 * and higher speeds.
8796c892
RK
238 *
239 * If in Cisco SGMII mode, the link speed and duplex mode are passed
240 * in the serial bitstream 16-bit configuration word, and the MAC
241 * should be configured to read these bits and acknowledge the
242 * configuration word. Nothing is advertised by the MAC. The MAC is
243 * responsible for reading the configuration word and configuring
244 * itself accordingly.
0e29ae03 245 *
4add7009
RKAL
246 * Valid state members: interface, an_enabled, pause, advertising.
247 *
0e29ae03
RK
248 * Implementations are expected to update the MAC to reflect the
249 * requested settings - i.o.w., if nothing has changed between two
250 * calls, no action is expected. If only flow control settings have
251 * changed, flow control should be updated *without* taking the link
252 * down. This "update" behaviour is critical to avoid bouncing the
253 * link up status.
8796c892 254 */
44cc27e4 255void mac_config(struct phylink_config *config, unsigned int mode,
8796c892
RK
256 const struct phylink_link_state *state);
257
b7ad14c2
RK
258/**
259 * mac_finish() - finish a to change the PHY interface mode
260 * @config: a pointer to a &struct phylink_config.
261 * @mode: one of %MLO_AN_FIXED, %MLO_AN_PHY, %MLO_AN_INBAND.
262 * @iface: interface mode to switch to
263 *
264 * phylink will call this if it called mac_prepare() to allow the MAC to
265 * complete any necessary steps after the MAC and PCS have been configured
266 * for the @mode and @iface. E.g. a MAC driver may wish to re-enable the
267 * Serdes PHY here if it was previously disabled by mac_prepare().
268 *
269 * Returns zero on success, or negative errno on failure which will be
270 * reported to the kernel log.
271 */
272int mac_finish(struct phylink_config *config, unsigned int mode,
273 phy_interface_t iface);
274
8796c892
RK
275/**
276 * mac_an_restart() - restart 802.3z BaseX autonegotiation
44cc27e4 277 * @config: a pointer to a &struct phylink_config.
8796c892 278 */
44cc27e4 279void mac_an_restart(struct phylink_config *config);
8796c892
RK
280
281/**
282 * mac_link_down() - take the link down
44cc27e4 283 * @config: a pointer to a &struct phylink_config.
8796c892 284 * @mode: link autonegotiation mode
c6ab3008 285 * @interface: link &typedef phy_interface_t mode
8796c892
RK
286 *
287 * If @mode is not an in-band negotiation mode (as defined by
288 * phylink_autoneg_inband()), force the link down and disable any
c6ab3008
FF
289 * Energy Efficient Ethernet MAC configuration. Interface type
290 * selection must be done in mac_config().
8796c892 291 */
44cc27e4 292void mac_link_down(struct phylink_config *config, unsigned int mode,
c6ab3008 293 phy_interface_t interface);
8796c892
RK
294
295/**
296 * mac_link_up() - allow the link to come up
44cc27e4 297 * @config: a pointer to a &struct phylink_config.
91a208f2 298 * @phy: any attached phy
8796c892 299 * @mode: link autonegotiation mode
c6ab3008 300 * @interface: link &typedef phy_interface_t mode
91a208f2
RK
301 * @speed: link speed
302 * @duplex: link duplex
303 * @tx_pause: link transmit pause enablement status
304 * @rx_pause: link receive pause enablement status
8796c892 305 *
91a208f2
RK
306 * Configure the MAC for an established link.
307 *
308 * @speed, @duplex, @tx_pause and @rx_pause indicate the finalised link
309 * settings, and should be used to configure the MAC block appropriately
310 * where these settings are not automatically conveyed from the PCS block,
311 * or if in-band negotiation (as defined by phylink_autoneg_inband(@mode))
312 * is disabled.
313 *
314 * Note that when 802.3z in-band negotiation is in use, it is possible
315 * that the user wishes to override the pause settings, and this should
316 * be allowed when considering the implementation of this method.
317 *
318 * If in-band negotiation mode is disabled, allow the link to come up. If
319 * @phy is non-%NULL, configure Energy Efficient Ethernet by calling
8796c892 320 * phy_init_eee() and perform appropriate MAC configuration for EEE.
c6ab3008 321 * Interface type selection must be done in mac_config().
8796c892 322 */
91a208f2
RK
323void mac_link_up(struct phylink_config *config, struct phy_device *phy,
324 unsigned int mode, phy_interface_t interface,
325 int speed, int duplex, bool tx_pause, bool rx_pause);
8796c892
RK
326#endif
327
7137e18f
RK
328struct phylink_pcs_ops;
329
330/**
331 * struct phylink_pcs - PHYLINK PCS instance
332 * @ops: a pointer to the &struct phylink_pcs_ops structure
333 * @poll: poll the PCS for link changes
334 *
335 * This structure is designed to be embedded within the PCS private data,
336 * and will be passed between phylink and the PCS.
337 */
338struct phylink_pcs {
339 const struct phylink_pcs_ops *ops;
340 bool poll;
341};
342
4c0d6d3a
RK
343/**
344 * struct phylink_pcs_ops - MAC PCS operations structure.
345 * @pcs_get_state: read the current MAC PCS link state from the hardware.
346 * @pcs_config: configure the MAC PCS for the selected mode and state.
347 * @pcs_an_restart: restart 802.3z BaseX autonegotiation.
348 * @pcs_link_up: program the PCS for the resolved link configuration
349 * (where necessary).
350 */
351struct phylink_pcs_ops {
7137e18f 352 void (*pcs_get_state)(struct phylink_pcs *pcs,
4c0d6d3a 353 struct phylink_link_state *state);
7137e18f 354 int (*pcs_config)(struct phylink_pcs *pcs, unsigned int mode,
4c0d6d3a 355 phy_interface_t interface,
1571e700
RK
356 const unsigned long *advertising,
357 bool permit_pause_to_mac);
7137e18f
RK
358 void (*pcs_an_restart)(struct phylink_pcs *pcs);
359 void (*pcs_link_up)(struct phylink_pcs *pcs, unsigned int mode,
4c0d6d3a
RK
360 phy_interface_t interface, int speed, int duplex);
361};
362
363#if 0 /* For kernel-doc purposes only. */
364/**
365 * pcs_get_state() - Read the current inband link state from the hardware
7137e18f 366 * @pcs: a pointer to a &struct phylink_pcs.
4c0d6d3a
RK
367 * @state: a pointer to a &struct phylink_link_state.
368 *
369 * Read the current inband link state from the MAC PCS, reporting the
370 * current speed in @state->speed, duplex mode in @state->duplex, pause
371 * mode in @state->pause using the %MLO_PAUSE_RX and %MLO_PAUSE_TX bits,
372 * negotiation completion state in @state->an_complete, and link up state
373 * in @state->link. If possible, @state->lp_advertising should also be
374 * populated.
375 *
376 * When present, this overrides mac_pcs_get_state() in &struct
377 * phylink_mac_ops.
378 */
7137e18f 379void pcs_get_state(struct phylink_pcs *pcs,
4c0d6d3a
RK
380 struct phylink_link_state *state);
381
382/**
383 * pcs_config() - Configure the PCS mode and advertisement
7137e18f 384 * @pcs: a pointer to a &struct phylink_pcs.
4c0d6d3a
RK
385 * @mode: one of %MLO_AN_FIXED, %MLO_AN_PHY, %MLO_AN_INBAND.
386 * @interface: interface mode to be used
387 * @advertising: adertisement ethtool link mode mask
1571e700 388 * @permit_pause_to_mac: permit forwarding pause resolution to MAC
4c0d6d3a
RK
389 *
390 * Configure the PCS for the operating mode, the interface mode, and set
1571e700
RK
391 * the advertisement mask. @permit_pause_to_mac indicates whether the
392 * hardware may forward the pause mode resolution to the MAC.
4c0d6d3a
RK
393 *
394 * When operating in %MLO_AN_INBAND, inband should always be enabled,
395 * otherwise inband should be disabled.
396 *
397 * For SGMII, there is no advertisement from the MAC side, the PCS should
398 * be programmed to acknowledge the inband word from the PHY.
399 *
400 * For 1000BASE-X, the advertisement should be programmed into the PCS.
401 *
402 * For most 10GBASE-R, there is no advertisement.
403 */
7137e18f 404int pcs_config(struct phylink_pcs *pcs, unsigned int mode,
0b76e642
RD
405 phy_interface_t interface, const unsigned long *advertising,
406 bool permit_pause_to_mac);
4c0d6d3a
RK
407
408/**
409 * pcs_an_restart() - restart 802.3z BaseX autonegotiation
7137e18f 410 * @pcs: a pointer to a &struct phylink_pcs.
4c0d6d3a
RK
411 *
412 * When PCS ops are present, this overrides mac_an_restart() in &struct
413 * phylink_mac_ops.
414 */
7137e18f 415void pcs_an_restart(struct phylink_pcs *pcs);
4c0d6d3a
RK
416
417/**
418 * pcs_link_up() - program the PCS for the resolved link configuration
7137e18f 419 * @pcs: a pointer to a &struct phylink_pcs.
4c0d6d3a
RK
420 * @mode: link autonegotiation mode
421 * @interface: link &typedef phy_interface_t mode
422 * @speed: link speed
423 * @duplex: link duplex
424 *
425 * This call will be made just before mac_link_up() to inform the PCS of
426 * the resolved link parameters. For example, a PCS operating in SGMII
427 * mode without in-band AN needs to be manually configured for the link
428 * and duplex setting. Otherwise, this should be a no-op.
429 */
7137e18f
RK
430void pcs_link_up(struct phylink_pcs *pcs, unsigned int mode,
431 phy_interface_t interface, int speed, int duplex);
4c0d6d3a
RK
432#endif
433
44cc27e4
IC
434struct phylink *phylink_create(struct phylink_config *, struct fwnode_handle *,
435 phy_interface_t iface,
e7765d63 436 const struct phylink_mac_ops *mac_ops);
7137e18f 437void phylink_set_pcs(struct phylink *, struct phylink_pcs *pcs);
9525ae83
RK
438void phylink_destroy(struct phylink *);
439
440int phylink_connect_phy(struct phylink *, struct phy_device *);
0a62964c 441int phylink_of_phy_connect(struct phylink *, struct device_node *, u32 flags);
9525ae83
RK
442void phylink_disconnect_phy(struct phylink *);
443
444void phylink_mac_change(struct phylink *, bool up);
445
446void phylink_start(struct phylink *);
447void phylink_stop(struct phylink *);
448
449void phylink_ethtool_get_wol(struct phylink *, struct ethtool_wolinfo *);
450int phylink_ethtool_set_wol(struct phylink *, struct ethtool_wolinfo *);
451
452int phylink_ethtool_ksettings_get(struct phylink *,
453 struct ethtool_link_ksettings *);
454int phylink_ethtool_ksettings_set(struct phylink *,
455 const struct ethtool_link_ksettings *);
456int phylink_ethtool_nway_reset(struct phylink *);
457void phylink_ethtool_get_pauseparam(struct phylink *,
458 struct ethtool_pauseparam *);
459int phylink_ethtool_set_pauseparam(struct phylink *,
460 struct ethtool_pauseparam *);
9525ae83 461int phylink_get_eee_err(struct phylink *);
86e58135 462int phylink_init_eee(struct phylink *, bool);
9525ae83
RK
463int phylink_ethtool_get_eee(struct phylink *, struct ethtool_eee *);
464int phylink_ethtool_set_eee(struct phylink *, struct ethtool_eee *);
465int phylink_mii_ioctl(struct phylink *, struct ifreq *, int);
c6d5d843
RK
466int phylink_speed_down(struct phylink *pl, bool sync);
467int phylink_speed_up(struct phylink *pl);
9525ae83
RK
468
469#define phylink_zero(bm) \
470 bitmap_zero(bm, __ETHTOOL_LINK_MODE_MASK_NBITS)
471#define __phylink_do_bit(op, bm, mode) \
472 op(ETHTOOL_LINK_MODE_ ## mode ## _BIT, bm)
473
474#define phylink_set(bm, mode) __phylink_do_bit(__set_bit, bm, mode)
475#define phylink_clear(bm, mode) __phylink_do_bit(__clear_bit, bm, mode)
476#define phylink_test(bm, mode) __phylink_do_bit(test_bit, bm, mode)
477
478void phylink_set_port_modes(unsigned long *bits);
624c0f02 479void phylink_helper_basex_speed(struct phylink_link_state *state);
9525ae83 480
74db1c18
RK
481void phylink_mii_c22_pcs_get_state(struct mdio_device *pcs,
482 struct phylink_link_state *state);
483int phylink_mii_c22_pcs_set_advertisement(struct mdio_device *pcs,
0bd27406
RK
484 phy_interface_t interface,
485 const unsigned long *advertising);
93eaceb0
RK
486int phylink_mii_c22_pcs_config(struct mdio_device *pcs, unsigned int mode,
487 phy_interface_t interface,
488 const unsigned long *advertising);
74db1c18
RK
489void phylink_mii_c22_pcs_an_restart(struct mdio_device *pcs);
490
b8679ef8
RK
491void phylink_mii_c45_pcs_get_state(struct mdio_device *pcs,
492 struct phylink_link_state *state);
9525ae83 493#endif