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