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