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
66 struct phylink_config
{
68 enum phylink_op_type type
;
70 bool poll_fixed_state
;
71 void (*get_fixed_state
)(struct phylink_config
*config
,
72 struct phylink_link_state
*state
);
76 * struct phylink_mac_ops - MAC operations structure.
77 * @validate: Validate and update the link configuration.
78 * @mac_pcs_get_state: Read the current link state from the hardware.
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.
84 * The individual methods are described more fully below.
86 struct phylink_mac_ops
{
87 void (*validate
)(struct phylink_config
*config
,
88 unsigned long *supported
,
89 struct phylink_link_state
*state
);
90 void (*mac_pcs_get_state
)(struct phylink_config
*config
,
91 struct phylink_link_state
*state
);
92 void (*mac_config
)(struct phylink_config
*config
, unsigned int mode
,
93 const struct phylink_link_state
*state
);
94 void (*mac_an_restart
)(struct phylink_config
*config
);
95 void (*mac_link_down
)(struct phylink_config
*config
, unsigned int mode
,
96 phy_interface_t interface
);
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
);
103 #if 0 /* For kernel-doc purposes only. */
105 * validate - Validate and update the link configuration
106 * @config: a pointer to a &struct phylink_config.
107 * @supported: ethtool bitmask for supported link modes.
108 * @state: a pointer to a &struct phylink_link_state.
110 * Clear bits in the @supported and @state->advertising masks that
111 * are not supportable by the MAC.
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
116 * clearing unsupported speeds and duplex settings. The port modes
117 * should not be cleared; phylink_set_port_modes() will help with this.
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
122 * @state->interface accordingly. See phylink_helper_basex_speed().
124 * When @state->interface is %PHY_INTERFACE_MODE_NA, phylink expects the
125 * MAC driver to return all supported link modes.
127 * If the @state->interface mode is not supported, then the @supported
128 * mask must be cleared.
130 void validate(struct phylink_config
*config
, unsigned long *supported
,
131 struct phylink_link_state
*state
);
134 * mac_pcs_get_state() - Read the current inband link state from the hardware
135 * @config: a pointer to a &struct phylink_config.
136 * @state: a pointer to a &struct phylink_link_state.
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
145 void mac_pcs_get_state(struct phylink_config
*config
,
146 struct phylink_link_state
*state
);
149 * mac_config() - configure the MAC for the selected mode and state
150 * @config: a pointer to a &struct phylink_config.
151 * @mode: one of %MLO_AN_FIXED, %MLO_AN_PHY, %MLO_AN_INBAND.
152 * @state: a pointer to a &struct phylink_link_state.
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.
159 * (this requires a rewrite - please refer to mac_link_up() for situations
160 * where the PCS and MAC are not tightly integrated.)
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.
169 * The action performed depends on the currently selected mode:
171 * %MLO_AN_FIXED, %MLO_AN_PHY:
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.
176 * @state->advertising may be used, but is not required.
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().
182 * Other members of @state must be ignored.
184 * Valid state members: interface, advertising.
185 * Deprecated state members: speed, duplex, pause.
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
192 * mac_pcs_get_state() callback. Changes in link state must be made
193 * by calling phylink_mac_change().
195 * Interface mode specific details are mentioned below.
197 * If in 802.3z mode, the link speed is fixed, dependent on the
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
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.
211 * Valid state members: interface, an_enabled, pause, advertising.
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
220 void mac_config(struct phylink_config
*config
, unsigned int mode
,
221 const struct phylink_link_state
*state
);
224 * mac_an_restart() - restart 802.3z BaseX autonegotiation
225 * @config: a pointer to a &struct phylink_config.
227 void mac_an_restart(struct phylink_config
*config
);
230 * mac_link_down() - take the link down
231 * @config: a pointer to a &struct phylink_config.
232 * @mode: link autonegotiation mode
233 * @interface: link &typedef phy_interface_t mode
235 * If @mode is not an in-band negotiation mode (as defined by
236 * phylink_autoneg_inband()), force the link down and disable any
237 * Energy Efficient Ethernet MAC configuration. Interface type
238 * selection must be done in mac_config().
240 void mac_link_down(struct phylink_config
*config
, unsigned int mode
,
241 phy_interface_t interface
);
244 * mac_link_up() - allow the link to come up
245 * @config: a pointer to a &struct phylink_config.
246 * @phy: any attached phy
247 * @mode: link autonegotiation mode
248 * @interface: link &typedef phy_interface_t mode
250 * @duplex: link duplex
251 * @tx_pause: link transmit pause enablement status
252 * @rx_pause: link receive pause enablement status
254 * Configure the MAC for an established link.
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))
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.
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
268 * phy_init_eee() and perform appropriate MAC configuration for EEE.
269 * Interface type selection must be done in mac_config().
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
);
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
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
);
295 #if 0 /* For kernel-doc purposes only. */
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.
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
308 * When present, this overrides mac_pcs_get_state() in &struct
311 void pcs_get_state(struct phylink_config
*config
,
312 struct phylink_link_state
*state
);
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
321 * Configure the PCS for the operating mode, the interface mode, and set
322 * the advertisement mask.
324 * When operating in %MLO_AN_INBAND, inband should always be enabled,
325 * otherwise inband should be disabled.
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.
330 * For 1000BASE-X, the advertisement should be programmed into the PCS.
332 * For most 10GBASE-R, there is no advertisement.
334 int (*pcs_config
)(struct phylink_config
*config
, unsigned int mode
,
335 phy_interface_t interface
, const unsigned long *advertising
);
338 * pcs_an_restart() - restart 802.3z BaseX autonegotiation
339 * @config: a pointer to a &struct phylink_config.
341 * When PCS ops are present, this overrides mac_an_restart() in &struct
344 void (*pcs_an_restart
)(struct phylink_config
*config
);
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
352 * @duplex: link duplex
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.
359 void (*pcs_link_up
)(struct phylink_config
*config
, unsigned int mode
,
360 phy_interface_t interface
, int speed
, int duplex
);
363 struct phylink
*phylink_create(struct phylink_config
*, struct fwnode_handle
*,
364 phy_interface_t iface
,
365 const struct phylink_mac_ops
*mac_ops
);
366 void phylink_add_pcs(struct phylink
*, const struct phylink_pcs_ops
*ops
);
367 void phylink_destroy(struct phylink
*);
369 int phylink_connect_phy(struct phylink
*, struct phy_device
*);
370 int phylink_of_phy_connect(struct phylink
*, struct device_node
*, u32 flags
);
371 void phylink_disconnect_phy(struct phylink
*);
373 void phylink_mac_change(struct phylink
*, bool up
);
375 void phylink_start(struct phylink
*);
376 void phylink_stop(struct phylink
*);
378 void phylink_ethtool_get_wol(struct phylink
*, struct ethtool_wolinfo
*);
379 int phylink_ethtool_set_wol(struct phylink
*, struct ethtool_wolinfo
*);
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
*);
390 int phylink_get_eee_err(struct phylink
*);
391 int phylink_init_eee(struct phylink
*, bool);
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);
396 #define phylink_zero(bm) \
397 bitmap_zero(bm, __ETHTOOL_LINK_MODE_MASK_NBITS)
398 #define __phylink_do_bit(op, bm, mode) \
399 op(ETHTOOL_LINK_MODE_ ## mode ## _BIT, bm)
401 #define phylink_set(bm, mode) __phylink_do_bit(__set_bit, bm, mode)
402 #define phylink_clear(bm, mode) __phylink_do_bit(__clear_bit, bm, mode)
403 #define phylink_test(bm, mode) __phylink_do_bit(test_bit, bm, mode)
405 void phylink_set_port_modes(unsigned long *bits
);
406 void phylink_helper_basex_speed(struct phylink_link_state
*state
);
408 void phylink_mii_c22_pcs_get_state(struct mdio_device
*pcs
,
409 struct phylink_link_state
*state
);
410 int phylink_mii_c22_pcs_set_advertisement(struct mdio_device
*pcs
,
411 phy_interface_t interface
,
412 const unsigned long *advertising
);
413 void phylink_mii_c22_pcs_an_restart(struct mdio_device
*pcs
);
415 void phylink_mii_c45_pcs_get_state(struct mdio_device
*pcs
,
416 struct phylink_link_state
*state
);