5 #include <linux/spinlock.h>
6 #include <linux/workqueue.h>
15 MLO_PAUSE_RX = BIT(0),
16 MLO_PAUSE_TX = BIT(1),
17 MLO_PAUSE_TXRX_MASK = MLO_PAUSE_TX | MLO_PAUSE_RX,
18 MLO_PAUSE_AN = BIT(2),
20 MLO_AN_PHY = 0, /* Conventional PHY */
21 MLO_AN_FIXED, /* Fixed-link mode */
22 MLO_AN_INBAND, /* In-band protocol */
25 static inline bool phylink_autoneg_inband(unsigned int mode)
27 return mode == MLO_AN_INBAND;
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
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.
43 struct phylink_link_state {
44 __ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
45 __ETHTOOL_DECLARE_LINK_MODE_MASK(lp_advertising);
46 phy_interface_t interface;
51 unsigned int an_enabled:1;
52 unsigned int an_complete:1;
55 enum phylink_op_type {
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
64 * @pcs_poll: MAC PCS cannot provide link change interrupt
65 * @poll_fixed_state: if true, starts link_poll,
66 * if MAC link is at %MLO_AN_FIXED mode.
67 * @ovr_an_inband: if true, override PCS to MLO_AN_INBAND
68 * @get_fixed_state: callback to execute to determine the fixed link state,
69 * if MAC link is at %MLO_AN_FIXED mode.
71 struct phylink_config {
73 enum phylink_op_type type;
75 bool poll_fixed_state;
77 void (*get_fixed_state)(struct phylink_config *config,
78 struct phylink_link_state *state);
82 * struct phylink_mac_ops - MAC operations structure.
83 * @validate: Validate and update the link configuration.
84 * @mac_pcs_get_state: Read the current link state from the hardware.
85 * @mac_prepare: prepare for a major reconfiguration of the interface.
86 * @mac_config: configure the MAC for the selected mode and state.
87 * @mac_finish: finish a major reconfiguration of the interface.
88 * @mac_an_restart: restart 802.3z BaseX autonegotiation.
89 * @mac_link_down: take the link down.
90 * @mac_link_up: allow the link to come up.
92 * The individual methods are described more fully below.
94 struct phylink_mac_ops {
95 void (*validate)(struct phylink_config *config,
96 unsigned long *supported,
97 struct phylink_link_state *state);
98 void (*mac_pcs_get_state)(struct phylink_config *config,
99 struct phylink_link_state *state);
100 int (*mac_prepare)(struct phylink_config *config, unsigned int mode,
101 phy_interface_t iface);
102 void (*mac_config)(struct phylink_config *config, unsigned int mode,
103 const struct phylink_link_state *state);
104 int (*mac_finish)(struct phylink_config *config, unsigned int mode,
105 phy_interface_t iface);
106 void (*mac_an_restart)(struct phylink_config *config);
107 void (*mac_link_down)(struct phylink_config *config, unsigned int mode,
108 phy_interface_t interface);
109 void (*mac_link_up)(struct phylink_config *config,
110 struct phy_device *phy, unsigned int mode,
111 phy_interface_t interface, int speed, int duplex,
112 bool tx_pause, bool rx_pause);
115 #if 0 /* For kernel-doc purposes only. */
117 * validate - Validate and update the link configuration
118 * @config: a pointer to a &struct phylink_config.
119 * @supported: ethtool bitmask for supported link modes.
120 * @state: a pointer to a &struct phylink_link_state.
122 * Clear bits in the @supported and @state->advertising masks that
123 * are not supportable by the MAC.
125 * Note that the PHY may be able to transform from one connection
126 * technology to another, so, eg, don't clear 1000BaseX just
127 * because the MAC is unable to BaseX mode. This is more about
128 * clearing unsupported speeds and duplex settings. The port modes
129 * should not be cleared; phylink_set_port_modes() will help with this.
131 * If the @state->interface mode is %PHY_INTERFACE_MODE_1000BASEX
132 * or %PHY_INTERFACE_MODE_2500BASEX, select the appropriate mode
133 * based on @state->advertising and/or @state->speed and update
134 * @state->interface accordingly. See phylink_helper_basex_speed().
136 * When @state->interface is %PHY_INTERFACE_MODE_NA, phylink expects the
137 * MAC driver to return all supported link modes.
139 * If the @state->interface mode is not supported, then the @supported
140 * mask must be cleared.
142 void validate(struct phylink_config *config, unsigned long *supported,
143 struct phylink_link_state *state);
146 * mac_pcs_get_state() - Read the current inband link state from the hardware
147 * @config: a pointer to a &struct phylink_config.
148 * @state: a pointer to a &struct phylink_link_state.
150 * Read the current inband link state from the MAC PCS, reporting the
151 * current speed in @state->speed, duplex mode in @state->duplex, pause
152 * mode in @state->pause using the %MLO_PAUSE_RX and %MLO_PAUSE_TX bits,
153 * negotiation completion state in @state->an_complete, and link up state
154 * in @state->link. If possible, @state->lp_advertising should also be
157 void mac_pcs_get_state(struct phylink_config *config,
158 struct phylink_link_state *state);
161 * mac_prepare() - prepare to change the PHY interface mode
162 * @config: a pointer to a &struct phylink_config.
163 * @mode: one of %MLO_AN_FIXED, %MLO_AN_PHY, %MLO_AN_INBAND.
164 * @iface: interface mode to switch to
166 * phylink will call this method at the beginning of a full initialisation
167 * of the link, which includes changing the interface mode or at initial
168 * startup time. It may be called for the current mode. The MAC driver
169 * should perform whatever actions are required, e.g. disabling the
172 * This will be the first call in the sequence:
176 * - possible pcs_an_restart()
179 * Returns zero on success, or negative errno on failure which will be
180 * reported to the kernel log.
182 int mac_prepare(struct phylink_config *config, unsigned int mode,
183 phy_interface_t iface);
186 * mac_config() - configure the MAC for the selected mode and state
187 * @config: a pointer to a &struct phylink_config.
188 * @mode: one of %MLO_AN_FIXED, %MLO_AN_PHY, %MLO_AN_INBAND.
189 * @state: a pointer to a &struct phylink_link_state.
191 * Note - not all members of @state are valid. In particular,
192 * @state->lp_advertising, @state->link, @state->an_complete are never
193 * guaranteed to be correct, and so any mac_config() implementation must
194 * never reference these fields.
196 * (this requires a rewrite - please refer to mac_link_up() for situations
197 * where the PCS and MAC are not tightly integrated.)
199 * In all negotiation modes, as defined by @mode, @state->pause indicates the
200 * pause settings which should be applied as follows. If %MLO_PAUSE_AN is not
201 * set, %MLO_PAUSE_TX and %MLO_PAUSE_RX indicate whether the MAC should send
202 * pause frames and/or act on received pause frames respectively. Otherwise,
203 * the results of in-band negotiation/status from the MAC PCS should be used
204 * to control the MAC pause mode settings.
206 * The action performed depends on the currently selected mode:
208 * %MLO_AN_FIXED, %MLO_AN_PHY:
209 * Configure for non-inband negotiation mode, where the link settings
210 * are completely communicated via mac_link_up(). The physical link
211 * protocol from the MAC is specified by @state->interface.
213 * @state->advertising may be used, but is not required.
215 * Older drivers (prior to the mac_link_up() change) may use @state->speed,
216 * @state->duplex and @state->pause to configure the MAC, but this is
217 * deprecated; such drivers should be converted to use mac_link_up().
219 * Other members of @state must be ignored.
221 * Valid state members: interface, advertising.
222 * Deprecated state members: speed, duplex, pause.
225 * place the link in an inband negotiation mode (such as 802.3z
226 * 1000base-X or Cisco SGMII mode depending on the @state->interface
227 * mode). In both cases, link state management (whether the link
228 * is up or not) is performed by the MAC, and reported via the
229 * mac_pcs_get_state() callback. Changes in link state must be made
230 * by calling phylink_mac_change().
232 * Interface mode specific details are mentioned below.
234 * If in 802.3z mode, the link speed is fixed, dependent on the
235 * @state->interface. Duplex and pause modes are negotiated via
236 * the in-band configuration word. Advertised pause modes are set
237 * according to the @state->an_enabled and @state->advertising
238 * flags. Beware of MACs which only support full duplex at gigabit
241 * If in Cisco SGMII mode, the link speed and duplex mode are passed
242 * in the serial bitstream 16-bit configuration word, and the MAC
243 * should be configured to read these bits and acknowledge the
244 * configuration word. Nothing is advertised by the MAC. The MAC is
245 * responsible for reading the configuration word and configuring
246 * itself accordingly.
248 * Valid state members: interface, an_enabled, pause, advertising.
250 * Implementations are expected to update the MAC to reflect the
251 * requested settings - i.o.w., if nothing has changed between two
252 * calls, no action is expected. If only flow control settings have
253 * changed, flow control should be updated *without* taking the link
254 * down. This "update" behaviour is critical to avoid bouncing the
257 void mac_config(struct phylink_config *config, unsigned int mode,
258 const struct phylink_link_state *state);
261 * mac_finish() - finish a to change the PHY interface mode
262 * @config: a pointer to a &struct phylink_config.
263 * @mode: one of %MLO_AN_FIXED, %MLO_AN_PHY, %MLO_AN_INBAND.
264 * @iface: interface mode to switch to
266 * phylink will call this if it called mac_prepare() to allow the MAC to
267 * complete any necessary steps after the MAC and PCS have been configured
268 * for the @mode and @iface. E.g. a MAC driver may wish to re-enable the
269 * Serdes PHY here if it was previously disabled by mac_prepare().
271 * Returns zero on success, or negative errno on failure which will be
272 * reported to the kernel log.
274 int mac_finish(struct phylink_config *config, unsigned int mode,
275 phy_interface_t iface);
278 * mac_an_restart() - restart 802.3z BaseX autonegotiation
279 * @config: a pointer to a &struct phylink_config.
281 void mac_an_restart(struct phylink_config *config);
284 * mac_link_down() - take the link down
285 * @config: a pointer to a &struct phylink_config.
286 * @mode: link autonegotiation mode
287 * @interface: link &typedef phy_interface_t mode
289 * If @mode is not an in-band negotiation mode (as defined by
290 * phylink_autoneg_inband()), force the link down and disable any
291 * Energy Efficient Ethernet MAC configuration. Interface type
292 * selection must be done in mac_config().
294 void mac_link_down(struct phylink_config *config, unsigned int mode,
295 phy_interface_t interface);
298 * mac_link_up() - allow the link to come up
299 * @config: a pointer to a &struct phylink_config.
300 * @phy: any attached phy
301 * @mode: link autonegotiation mode
302 * @interface: link &typedef phy_interface_t mode
304 * @duplex: link duplex
305 * @tx_pause: link transmit pause enablement status
306 * @rx_pause: link receive pause enablement status
308 * Configure the MAC for an established link.
310 * @speed, @duplex, @tx_pause and @rx_pause indicate the finalised link
311 * settings, and should be used to configure the MAC block appropriately
312 * where these settings are not automatically conveyed from the PCS block,
313 * or if in-band negotiation (as defined by phylink_autoneg_inband(@mode))
316 * Note that when 802.3z in-band negotiation is in use, it is possible
317 * that the user wishes to override the pause settings, and this should
318 * be allowed when considering the implementation of this method.
320 * If in-band negotiation mode is disabled, allow the link to come up. If
321 * @phy is non-%NULL, configure Energy Efficient Ethernet by calling
322 * phy_init_eee() and perform appropriate MAC configuration for EEE.
323 * Interface type selection must be done in mac_config().
325 void mac_link_up(struct phylink_config *config, struct phy_device *phy,
326 unsigned int mode, phy_interface_t interface,
327 int speed, int duplex, bool tx_pause, bool rx_pause);
330 struct phylink_pcs_ops;
333 * struct phylink_pcs - PHYLINK PCS instance
334 * @ops: a pointer to the &struct phylink_pcs_ops structure
335 * @poll: poll the PCS for link changes
337 * This structure is designed to be embedded within the PCS private data,
338 * and will be passed between phylink and the PCS.
341 const struct phylink_pcs_ops *ops;
346 * struct phylink_pcs_ops - MAC PCS operations structure.
347 * @pcs_get_state: read the current MAC PCS link state from the hardware.
348 * @pcs_config: configure the MAC PCS for the selected mode and state.
349 * @pcs_an_restart: restart 802.3z BaseX autonegotiation.
350 * @pcs_link_up: program the PCS for the resolved link configuration
353 struct phylink_pcs_ops {
354 void (*pcs_get_state)(struct phylink_pcs *pcs,
355 struct phylink_link_state *state);
356 int (*pcs_config)(struct phylink_pcs *pcs, unsigned int mode,
357 phy_interface_t interface,
358 const unsigned long *advertising,
359 bool permit_pause_to_mac);
360 void (*pcs_an_restart)(struct phylink_pcs *pcs);
361 void (*pcs_link_up)(struct phylink_pcs *pcs, unsigned int mode,
362 phy_interface_t interface, int speed, int duplex);
365 #if 0 /* For kernel-doc purposes only. */
367 * pcs_get_state() - Read the current inband link state from the hardware
368 * @pcs: a pointer to a &struct phylink_pcs.
369 * @state: a pointer to a &struct phylink_link_state.
371 * Read the current inband link state from the MAC PCS, reporting the
372 * current speed in @state->speed, duplex mode in @state->duplex, pause
373 * mode in @state->pause using the %MLO_PAUSE_RX and %MLO_PAUSE_TX bits,
374 * negotiation completion state in @state->an_complete, and link up state
375 * in @state->link. If possible, @state->lp_advertising should also be
378 * When present, this overrides mac_pcs_get_state() in &struct
381 void pcs_get_state(struct phylink_pcs *pcs,
382 struct phylink_link_state *state);
385 * pcs_config() - Configure the PCS mode and advertisement
386 * @pcs: a pointer to a &struct phylink_pcs.
387 * @mode: one of %MLO_AN_FIXED, %MLO_AN_PHY, %MLO_AN_INBAND.
388 * @interface: interface mode to be used
389 * @advertising: adertisement ethtool link mode mask
390 * @permit_pause_to_mac: permit forwarding pause resolution to MAC
392 * Configure the PCS for the operating mode, the interface mode, and set
393 * the advertisement mask. @permit_pause_to_mac indicates whether the
394 * hardware may forward the pause mode resolution to the MAC.
396 * When operating in %MLO_AN_INBAND, inband should always be enabled,
397 * otherwise inband should be disabled.
399 * For SGMII, there is no advertisement from the MAC side, the PCS should
400 * be programmed to acknowledge the inband word from the PHY.
402 * For 1000BASE-X, the advertisement should be programmed into the PCS.
404 * For most 10GBASE-R, there is no advertisement.
406 int pcs_config(struct phylink_pcs *pcs, unsigned int mode,
407 phy_interface_t interface, const unsigned long *advertising,
408 bool permit_pause_to_mac);
411 * pcs_an_restart() - restart 802.3z BaseX autonegotiation
412 * @pcs: a pointer to a &struct phylink_pcs.
414 * When PCS ops are present, this overrides mac_an_restart() in &struct
417 void pcs_an_restart(struct phylink_pcs *pcs);
420 * pcs_link_up() - program the PCS for the resolved link configuration
421 * @pcs: a pointer to a &struct phylink_pcs.
422 * @mode: link autonegotiation mode
423 * @interface: link &typedef phy_interface_t mode
425 * @duplex: link duplex
427 * This call will be made just before mac_link_up() to inform the PCS of
428 * the resolved link parameters. For example, a PCS operating in SGMII
429 * mode without in-band AN needs to be manually configured for the link
430 * and duplex setting. Otherwise, this should be a no-op.
432 void pcs_link_up(struct phylink_pcs *pcs, unsigned int mode,
433 phy_interface_t interface, int speed, int duplex);
436 struct phylink *phylink_create(struct phylink_config *, struct fwnode_handle *,
437 phy_interface_t iface,
438 const struct phylink_mac_ops *mac_ops);
439 void phylink_set_pcs(struct phylink *, struct phylink_pcs *pcs);
440 void phylink_destroy(struct phylink *);
442 int phylink_connect_phy(struct phylink *, struct phy_device *);
443 int phylink_of_phy_connect(struct phylink *, struct device_node *, u32 flags);
444 int phylink_fwnode_phy_connect(struct phylink *pl,
445 struct fwnode_handle *fwnode,
447 void phylink_disconnect_phy(struct phylink *);
449 void phylink_mac_change(struct phylink *, bool up);
451 void phylink_start(struct phylink *);
452 void phylink_stop(struct phylink *);
454 void phylink_suspend(struct phylink *pl, bool mac_wol);
455 void phylink_resume(struct phylink *pl);
457 void phylink_ethtool_get_wol(struct phylink *, struct ethtool_wolinfo *);
458 int phylink_ethtool_set_wol(struct phylink *, struct ethtool_wolinfo *);
460 int phylink_ethtool_ksettings_get(struct phylink *,
461 struct ethtool_link_ksettings *);
462 int phylink_ethtool_ksettings_set(struct phylink *,
463 const struct ethtool_link_ksettings *);
464 int phylink_ethtool_nway_reset(struct phylink *);
465 void phylink_ethtool_get_pauseparam(struct phylink *,
466 struct ethtool_pauseparam *);
467 int phylink_ethtool_set_pauseparam(struct phylink *,
468 struct ethtool_pauseparam *);
469 int phylink_get_eee_err(struct phylink *);
470 int phylink_init_eee(struct phylink *, bool);
471 int phylink_ethtool_get_eee(struct phylink *, struct ethtool_eee *);
472 int phylink_ethtool_set_eee(struct phylink *, struct ethtool_eee *);
473 int phylink_mii_ioctl(struct phylink *, struct ifreq *, int);
474 int phylink_speed_down(struct phylink *pl, bool sync);
475 int phylink_speed_up(struct phylink *pl);
477 #define phylink_zero(bm) \
478 bitmap_zero(bm, __ETHTOOL_LINK_MODE_MASK_NBITS)
479 #define __phylink_do_bit(op, bm, mode) \
480 op(ETHTOOL_LINK_MODE_ ## mode ## _BIT, bm)
482 #define phylink_set(bm, mode) __phylink_do_bit(__set_bit, bm, mode)
483 #define phylink_clear(bm, mode) __phylink_do_bit(__clear_bit, bm, mode)
484 #define phylink_test(bm, mode) __phylink_do_bit(test_bit, bm, mode)
486 void phylink_set_port_modes(unsigned long *bits);
487 void phylink_helper_basex_speed(struct phylink_link_state *state);
489 void phylink_mii_c22_pcs_get_state(struct mdio_device *pcs,
490 struct phylink_link_state *state);
491 int phylink_mii_c22_pcs_set_advertisement(struct mdio_device *pcs,
492 phy_interface_t interface,
493 const unsigned long *advertising);
494 int phylink_mii_c22_pcs_config(struct mdio_device *pcs, unsigned int mode,
495 phy_interface_t interface,
496 const unsigned long *advertising);
497 void phylink_mii_c22_pcs_an_restart(struct mdio_device *pcs);
499 void phylink_mii_c45_pcs_get_state(struct mdio_device *pcs,
500 struct phylink_link_state *state);
502 void phylink_decode_usxgmii_word(struct phylink_link_state *state,