xref: /linux/include/linux/phylink.h (revision 1a9239bb4253f9076b5b4b2a1a4e8d7defd77a95)
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 #include <net/eee.h>
9 
10 struct device_node;
11 struct ethtool_cmd;
12 struct fwnode_handle;
13 struct net_device;
14 struct phylink;
15 
16 enum {
17 	MLO_PAUSE_NONE,
18 	MLO_PAUSE_RX = BIT(0),
19 	MLO_PAUSE_TX = BIT(1),
20 	MLO_PAUSE_TXRX_MASK = MLO_PAUSE_TX | MLO_PAUSE_RX,
21 	MLO_PAUSE_AN = BIT(2),
22 
23 	MLO_AN_PHY = 0,	/* Conventional PHY */
24 	MLO_AN_FIXED,	/* Fixed-link mode */
25 	MLO_AN_INBAND,	/* In-band protocol */
26 
27 	/* PCS "negotiation" mode.
28 	 *  PHYLINK_PCS_NEG_NONE - protocol has no inband capability
29 	 *  PHYLINK_PCS_NEG_OUTBAND - some out of band or fixed link setting
30 	 *  PHYLINK_PCS_NEG_INBAND_DISABLED - inband mode disabled, e.g.
31 	 *				      1000base-X with autoneg off
32 	 *  PHYLINK_PCS_NEG_INBAND_ENABLED - inband mode enabled
33 	 * Additionally, this can be tested using bitmasks:
34 	 *  PHYLINK_PCS_NEG_INBAND - inband mode selected
35 	 *  PHYLINK_PCS_NEG_ENABLED - negotiation mode enabled
36 	 */
37 	PHYLINK_PCS_NEG_NONE = 0,
38 	PHYLINK_PCS_NEG_ENABLED = BIT(4),
39 	PHYLINK_PCS_NEG_OUTBAND = BIT(5),
40 	PHYLINK_PCS_NEG_INBAND = BIT(6),
41 	PHYLINK_PCS_NEG_INBAND_DISABLED = PHYLINK_PCS_NEG_INBAND,
42 	PHYLINK_PCS_NEG_INBAND_ENABLED = PHYLINK_PCS_NEG_INBAND |
43 					 PHYLINK_PCS_NEG_ENABLED,
44 
45 	/* MAC_SYM_PAUSE and MAC_ASYM_PAUSE are used when configuring our
46 	 * autonegotiation advertisement. They correspond to the PAUSE and
47 	 * ASM_DIR bits defined by 802.3, respectively.
48 	 *
49 	 * The following table lists the values of tx_pause and rx_pause which
50 	 * might be requested in mac_link_up. The exact values depend on either
51 	 * the results of autonegotation (if MLO_PAUSE_AN is set) or user
52 	 * configuration (if MLO_PAUSE_AN is not set).
53 	 *
54 	 * MAC_SYM_PAUSE MAC_ASYM_PAUSE MLO_PAUSE_AN tx_pause/rx_pause
55 	 * ============= ============== ============ ==================
56 	 *             0              0            0 0/0
57 	 *             0              0            1 0/0
58 	 *             0              1            0 0/0, 0/1, 1/0, 1/1
59 	 *             0              1            1 0/0,      1/0
60 	 *             1              0            0 0/0,           1/1
61 	 *             1              0            1 0/0,           1/1
62 	 *             1              1            0 0/0, 0/1, 1/0, 1/1
63 	 *             1              1            1 0/0, 0/1,      1/1
64 	 *
65 	 * If you set MAC_ASYM_PAUSE, the user may request any combination of
66 	 * tx_pause and rx_pause. You do not have to support these
67 	 * combinations.
68 	 *
69 	 * However, you should support combinations of tx_pause and rx_pause
70 	 * which might be the result of autonegotation. For example, don't set
71 	 * MAC_SYM_PAUSE unless your device can support tx_pause and rx_pause
72 	 * at the same time.
73 	 */
74 	MAC_SYM_PAUSE	= BIT(0),
75 	MAC_ASYM_PAUSE	= BIT(1),
76 	MAC_10HD	= BIT(2),
77 	MAC_10FD	= BIT(3),
78 	MAC_10		= MAC_10HD | MAC_10FD,
79 	MAC_100HD	= BIT(4),
80 	MAC_100FD	= BIT(5),
81 	MAC_100		= MAC_100HD | MAC_100FD,
82 	MAC_1000HD	= BIT(6),
83 	MAC_1000FD	= BIT(7),
84 	MAC_1000	= MAC_1000HD | MAC_1000FD,
85 	MAC_2500FD	= BIT(8),
86 	MAC_5000FD	= BIT(9),
87 	MAC_10000FD	= BIT(10),
88 	MAC_20000FD	= BIT(11),
89 	MAC_25000FD	= BIT(12),
90 	MAC_40000FD	= BIT(13),
91 	MAC_50000FD	= BIT(14),
92 	MAC_56000FD	= BIT(15),
93 	MAC_100000FD	= BIT(16),
94 	MAC_200000FD	= BIT(17),
95 	MAC_400000FD	= BIT(18),
96 };
97 
phylink_autoneg_inband(unsigned int mode)98 static inline bool phylink_autoneg_inband(unsigned int mode)
99 {
100 	return mode == MLO_AN_INBAND;
101 }
102 
103 /**
104  * struct phylink_link_state - link state structure
105  * @advertising: ethtool bitmask containing advertised link modes
106  * @lp_advertising: ethtool bitmask containing link partner advertised link
107  *   modes
108  * @interface: link &typedef phy_interface_t mode
109  * @speed: link speed, one of the SPEED_* constants.
110  * @duplex: link duplex mode, one of DUPLEX_* constants.
111  * @pause: link pause state, described by MLO_PAUSE_* constants.
112  * @rate_matching: rate matching being performed, one of the RATE_MATCH_*
113  *   constants. If rate matching is taking place, then the speed/duplex of
114  *   the medium link mode (@speed and @duplex) and the speed/duplex of the phy
115  *   interface mode (@interface) are different.
116  * @link: true if the link is up.
117  * @an_complete: true if autonegotiation has completed.
118  */
119 struct phylink_link_state {
120 	__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
121 	__ETHTOOL_DECLARE_LINK_MODE_MASK(lp_advertising);
122 	phy_interface_t interface;
123 	int speed;
124 	int duplex;
125 	int pause;
126 	int rate_matching;
127 	unsigned int link:1;
128 	unsigned int an_complete:1;
129 };
130 
131 enum phylink_op_type {
132 	PHYLINK_NETDEV = 0,
133 	PHYLINK_DEV,
134 };
135 
136 /**
137  * struct phylink_config - PHYLINK configuration structure
138  * @dev: a pointer to a struct device associated with the MAC
139  * @type: operation type of PHYLINK instance
140  * @poll_fixed_state: if true, starts link_poll,
141  *		      if MAC link is at %MLO_AN_FIXED mode.
142  * @mac_managed_pm: if true, indicate the MAC driver is responsible for PHY PM.
143  * @mac_requires_rxc: if true, the MAC always requires a receive clock from PHY.
144  *                    The PHY driver should start the clock signal as soon as
145  *                    possible and avoid stopping it during suspend events.
146  * @default_an_inband: if true, defaults to MLO_AN_INBAND rather than
147  *		       MLO_AN_PHY. A fixed-link specification will override.
148  * @eee_rx_clk_stop_enable: if true, PHY can stop the receive clock during LPI
149  * @get_fixed_state: callback to execute to determine the fixed link state,
150  *		     if MAC link is at %MLO_AN_FIXED mode.
151  * @supported_interfaces: bitmap describing which PHY_INTERFACE_MODE_xxx
152  *                        are supported by the MAC/PCS.
153  * @lpi_interfaces: bitmap describing which PHY interface modes can support
154  *		    LPI signalling.
155  * @mac_capabilities: MAC pause/speed/duplex capabilities.
156  * @lpi_capabilities: MAC speeds which can support LPI signalling
157  * @lpi_timer_default: Default EEE LPI timer setting.
158  * @eee_enabled_default: If set, EEE will be enabled by phylink at creation time
159  */
160 struct phylink_config {
161 	struct device *dev;
162 	enum phylink_op_type type;
163 	bool poll_fixed_state;
164 	bool mac_managed_pm;
165 	bool mac_requires_rxc;
166 	bool default_an_inband;
167 	bool eee_rx_clk_stop_enable;
168 	void (*get_fixed_state)(struct phylink_config *config,
169 				struct phylink_link_state *state);
170 	DECLARE_PHY_INTERFACE_MASK(supported_interfaces);
171 	DECLARE_PHY_INTERFACE_MASK(lpi_interfaces);
172 	unsigned long mac_capabilities;
173 	unsigned long lpi_capabilities;
174 	u32 lpi_timer_default;
175 	bool eee_enabled_default;
176 };
177 
178 void phylink_limit_mac_speed(struct phylink_config *config, u32 max_speed);
179 
180 /**
181  * struct phylink_mac_ops - MAC operations structure.
182  * @mac_get_caps: Get MAC capabilities for interface mode.
183  * @mac_select_pcs: Select a PCS for the interface mode.
184  * @mac_prepare: prepare for a major reconfiguration of the interface.
185  * @mac_config: configure the MAC for the selected mode and state.
186  * @mac_finish: finish a major reconfiguration of the interface.
187  * @mac_link_down: take the link down.
188  * @mac_link_up: allow the link to come up.
189  * @mac_disable_tx_lpi: disable LPI.
190  * @mac_enable_tx_lpi: enable and configure LPI.
191  *
192  * The individual methods are described more fully below.
193  */
194 struct phylink_mac_ops {
195 	unsigned long (*mac_get_caps)(struct phylink_config *config,
196 				      phy_interface_t interface);
197 	struct phylink_pcs *(*mac_select_pcs)(struct phylink_config *config,
198 					      phy_interface_t interface);
199 	int (*mac_prepare)(struct phylink_config *config, unsigned int mode,
200 			   phy_interface_t iface);
201 	void (*mac_config)(struct phylink_config *config, unsigned int mode,
202 			   const struct phylink_link_state *state);
203 	int (*mac_finish)(struct phylink_config *config, unsigned int mode,
204 			  phy_interface_t iface);
205 	void (*mac_link_down)(struct phylink_config *config, unsigned int mode,
206 			      phy_interface_t interface);
207 	void (*mac_link_up)(struct phylink_config *config,
208 			    struct phy_device *phy, unsigned int mode,
209 			    phy_interface_t interface, int speed, int duplex,
210 			    bool tx_pause, bool rx_pause);
211 	void (*mac_disable_tx_lpi)(struct phylink_config *config);
212 	int (*mac_enable_tx_lpi)(struct phylink_config *config, u32 timer,
213 				 bool tx_clk_stop);
214 };
215 
216 #if 0 /* For kernel-doc purposes only. */
217 /**
218  * mac_get_caps: Get MAC capabilities for interface mode.
219  * @config: a pointer to a &struct phylink_config.
220  * @interface: PHY interface mode.
221  *
222  * Optional method. When not provided, config->mac_capabilities will be used.
223  * When implemented, this returns the MAC capabilities for the specified
224  * interface mode where there is some special handling required by the MAC
225  * driver (e.g. not supporting half-duplex in certain interface modes.)
226  */
227 unsigned long mac_get_caps(struct phylink_config *config,
228 			   phy_interface_t interface);
229 /**
230  * mac_select_pcs: Select a PCS for the interface mode.
231  * @config: a pointer to a &struct phylink_config.
232  * @interface: PHY interface mode for PCS
233  *
234  * Return the &struct phylink_pcs for the specified interface mode, or
235  * NULL if none is required, or an error pointer on error.
236  *
237  * This must not modify any state. It is used to query which PCS should
238  * be used. Phylink will use this during validation to ensure that the
239  * configuration is valid, and when setting a configuration to internally
240  * set the PCS that will be used.
241  */
242 struct phylink_pcs *mac_select_pcs(struct phylink_config *config,
243 				   phy_interface_t interface);
244 
245 /**
246  * mac_prepare() - prepare to change the PHY interface mode
247  * @config: a pointer to a &struct phylink_config.
248  * @mode: one of %MLO_AN_FIXED, %MLO_AN_PHY, %MLO_AN_INBAND.
249  * @iface: interface mode to switch to
250  *
251  * phylink will call this method at the beginning of a full initialisation
252  * of the link, which includes changing the interface mode or at initial
253  * startup time. It may be called for the current mode. The MAC driver
254  * should perform whatever actions are required, e.g. disabling the
255  * Serdes PHY.
256  *
257  * This will be the first call in the sequence:
258  * - mac_prepare()
259  * - mac_config()
260  * - pcs_config()
261  * - possible pcs_an_restart()
262  * - mac_finish()
263  *
264  * Returns zero on success, or negative errno on failure which will be
265  * reported to the kernel log.
266  */
267 int mac_prepare(struct phylink_config *config, unsigned int mode,
268 		phy_interface_t iface);
269 
270 /**
271  * mac_config() - configure the MAC for the selected mode and state
272  * @config: a pointer to a &struct phylink_config.
273  * @mode: one of %MLO_AN_FIXED, %MLO_AN_PHY, %MLO_AN_INBAND.
274  * @state: a pointer to a &struct phylink_link_state.
275  *
276  * Note - not all members of @state are valid.  In particular,
277  * @state->lp_advertising, @state->link, @state->an_complete are never
278  * guaranteed to be correct, and so any mac_config() implementation must
279  * never reference these fields.
280  *
281  * This will only be called to reconfigure the MAC for a "major" change in
282  * e.g. interface mode. It will not be called for changes in speed, duplex
283  * or pause modes or to change the in-band advertisement.
284  *
285  * In all negotiation modes, as defined by @mode, @state->pause indicates the
286  * pause settings which should be applied as follows. If %MLO_PAUSE_AN is not
287  * set, %MLO_PAUSE_TX and %MLO_PAUSE_RX indicate whether the MAC should send
288  * pause frames and/or act on received pause frames respectively. Otherwise,
289  * the results of in-band negotiation/status from the MAC PCS should be used
290  * to control the MAC pause mode settings.
291  *
292  * The action performed depends on the currently selected mode:
293  *
294  * %MLO_AN_FIXED, %MLO_AN_PHY:
295  *   Configure for non-inband negotiation mode, where the link settings
296  *   are completely communicated via mac_link_up().  The physical link
297  *   protocol from the MAC is specified by @state->interface.
298  *
299  *   @state->advertising may be used, but is not required.
300  *
301  *   Older drivers (prior to the mac_link_up() change) may use @state->speed,
302  *   @state->duplex and @state->pause to configure the MAC, but this is
303  *   deprecated; such drivers should be converted to use mac_link_up().
304  *
305  *   Other members of @state must be ignored.
306  *
307  *   Valid state members: interface, advertising.
308  *   Deprecated state members: speed, duplex, pause.
309  *
310  * %MLO_AN_INBAND:
311  *   place the link in an inband negotiation mode (such as 802.3z
312  *   1000base-X or Cisco SGMII mode depending on the @state->interface
313  *   mode). In both cases, link state management (whether the link
314  *   is up or not) is performed by the MAC, and reported via the
315  *   pcs_get_state() callback. Changes in link state must be made
316  *   by calling phylink_mac_change().
317  *
318  *   Interface mode specific details are mentioned below.
319  *
320  *   If in 802.3z mode, the link speed is fixed, dependent on the
321  *   @state->interface. Duplex and pause modes are negotiated via
322  *   the in-band configuration word. Advertised pause modes are set
323  *   according to the @state->an_enabled and @state->advertising
324  *   flags. Beware of MACs which only support full duplex at gigabit
325  *   and higher speeds.
326  *
327  *   If in Cisco SGMII mode, the link speed and duplex mode are passed
328  *   in the serial bitstream 16-bit configuration word, and the MAC
329  *   should be configured to read these bits and acknowledge the
330  *   configuration word. Nothing is advertised by the MAC. The MAC is
331  *   responsible for reading the configuration word and configuring
332  *   itself accordingly.
333  *
334  *   Valid state members: interface, an_enabled, pause, advertising.
335  *
336  * Implementations are expected to update the MAC to reflect the
337  * requested settings - i.o.w., if nothing has changed between two
338  * calls, no action is expected.  If only flow control settings have
339  * changed, flow control should be updated *without* taking the link
340  * down.  This "update" behaviour is critical to avoid bouncing the
341  * link up status.
342  */
343 void mac_config(struct phylink_config *config, unsigned int mode,
344 		const struct phylink_link_state *state);
345 
346 /**
347  * mac_finish() - finish a to change the PHY interface mode
348  * @config: a pointer to a &struct phylink_config.
349  * @mode: one of %MLO_AN_FIXED, %MLO_AN_PHY, %MLO_AN_INBAND.
350  * @iface: interface mode to switch to
351  *
352  * phylink will call this if it called mac_prepare() to allow the MAC to
353  * complete any necessary steps after the MAC and PCS have been configured
354  * for the @mode and @iface. E.g. a MAC driver may wish to re-enable the
355  * Serdes PHY here if it was previously disabled by mac_prepare().
356  *
357  * Returns zero on success, or negative errno on failure which will be
358  * reported to the kernel log.
359  */
360 int mac_finish(struct phylink_config *config, unsigned int mode,
361 		phy_interface_t iface);
362 
363 /**
364  * mac_link_down() - take the link down
365  * @config: a pointer to a &struct phylink_config.
366  * @mode: link autonegotiation mode
367  * @interface: link &typedef phy_interface_t mode
368  *
369  * If @mode is not an in-band negotiation mode (as defined by
370  * phylink_autoneg_inband()), force the link down and disable any
371  * Energy Efficient Ethernet MAC configuration. Interface type
372  * selection must be done in mac_config().
373  */
374 void mac_link_down(struct phylink_config *config, unsigned int mode,
375 		   phy_interface_t interface);
376 
377 /**
378  * mac_link_up() - allow the link to come up
379  * @config: a pointer to a &struct phylink_config.
380  * @phy: any attached phy
381  * @mode: link autonegotiation mode
382  * @interface: link &typedef phy_interface_t mode
383  * @speed: link speed
384  * @duplex: link duplex
385  * @tx_pause: link transmit pause enablement status
386  * @rx_pause: link receive pause enablement status
387  *
388  * Configure the MAC for an established link.
389  *
390  * @speed, @duplex, @tx_pause and @rx_pause indicate the finalised link
391  * settings, and should be used to configure the MAC block appropriately
392  * where these settings are not automatically conveyed from the PCS block,
393  * or if in-band negotiation (as defined by phylink_autoneg_inband(@mode))
394  * is disabled.
395  *
396  * Note that when 802.3z in-band negotiation is in use, it is possible
397  * that the user wishes to override the pause settings, and this should
398  * be allowed when considering the implementation of this method.
399  *
400  * If in-band negotiation mode is disabled, allow the link to come up. If
401  * @phy is non-%NULL, configure Energy Efficient Ethernet by calling
402  * phy_init_eee() and perform appropriate MAC configuration for EEE.
403  * Interface type selection must be done in mac_config().
404  */
405 void mac_link_up(struct phylink_config *config, struct phy_device *phy,
406 		 unsigned int mode, phy_interface_t interface,
407 		 int speed, int duplex, bool tx_pause, bool rx_pause);
408 
409 /**
410  * mac_disable_tx_lpi() - disable LPI generation at the MAC
411  * @config: a pointer to a &struct phylink_config.
412  *
413  * Disable generation of LPI at the MAC, effectively preventing the MAC
414  * from indicating that it is idle.
415  */
416 void mac_disable_tx_lpi(struct phylink_config *config);
417 
418 /**
419  * mac_enable_tx_lpi() - configure and enable LPI generation at the MAC
420  * @config: a pointer to a &struct phylink_config.
421  * @timer: LPI timeout in microseconds.
422  * @tx_clk_stop: allow xMII transmit clock to be stopped during LPI
423  *
424  * Configure the LPI timeout accordingly. This will only be called when
425  * the link is already up, to cater for situations where the hardware
426  * needs to be programmed according to the link speed.
427  *
428  * Enable LPI generation at the MAC, and configure whether the xMII transmit
429  * clock may be stopped.
430  *
431  * Returns: 0 on success. Please consult with rmk before returning an error.
432  */
433 int mac_enable_tx_lpi(struct phylink_config *config, u32 timer,
434 		      bool tx_clk_stop);
435 #endif
436 
437 struct phylink_pcs_ops;
438 
439 /**
440  * struct phylink_pcs - PHYLINK PCS instance
441  * @supported_interfaces: describing which PHY_INTERFACE_MODE_xxx
442  *                        are supported by this PCS.
443  * @ops: a pointer to the &struct phylink_pcs_ops structure
444  * @phylink: pointer to &struct phylink_config
445  * @poll: poll the PCS for link changes
446  * @rxc_always_on: The MAC driver requires the reference clock
447  *                 to always be on. Standalone PCS drivers which
448  *                 do not have access to a PHY device can check
449  *                 this instead of PHY_F_RXC_ALWAYS_ON.
450  *
451  * This structure is designed to be embedded within the PCS private data,
452  * and will be passed between phylink and the PCS.
453  *
454  * The @phylink member is private to phylink and must not be touched by
455  * the PCS driver.
456  */
457 struct phylink_pcs {
458 	DECLARE_PHY_INTERFACE_MASK(supported_interfaces);
459 	const struct phylink_pcs_ops *ops;
460 	struct phylink *phylink;
461 	bool poll;
462 	bool rxc_always_on;
463 };
464 
465 /**
466  * struct phylink_pcs_ops - MAC PCS operations structure.
467  * @pcs_validate: validate the link configuration.
468  * @pcs_inband_caps: query inband support for interface mode.
469  * @pcs_enable: enable the PCS.
470  * @pcs_disable: disable the PCS.
471  * @pcs_pre_config: pre-mac_config method (for errata)
472  * @pcs_post_config: post-mac_config method (for arrata)
473  * @pcs_get_state: read the current MAC PCS link state from the hardware.
474  * @pcs_config: configure the MAC PCS for the selected mode and state.
475  * @pcs_an_restart: restart 802.3z BaseX autonegotiation.
476  * @pcs_link_up: program the PCS for the resolved link configuration
477  *               (where necessary).
478  * @pcs_disable_eee: optional notification to PCS that EEE has been disabled
479  *		     at the MAC.
480  * @pcs_enable_eee: optional notification to PCS that EEE will be enabled at
481  *		    the MAC.
482  * @pcs_pre_init: configure PCS components necessary for MAC hardware
483  *                initialization e.g. RX clock for stmmac.
484  */
485 struct phylink_pcs_ops {
486 	int (*pcs_validate)(struct phylink_pcs *pcs, unsigned long *supported,
487 			    const struct phylink_link_state *state);
488 	unsigned int (*pcs_inband_caps)(struct phylink_pcs *pcs,
489 					phy_interface_t interface);
490 	int (*pcs_enable)(struct phylink_pcs *pcs);
491 	void (*pcs_disable)(struct phylink_pcs *pcs);
492 	void (*pcs_pre_config)(struct phylink_pcs *pcs,
493 			       phy_interface_t interface);
494 	int (*pcs_post_config)(struct phylink_pcs *pcs,
495 			       phy_interface_t interface);
496 	void (*pcs_get_state)(struct phylink_pcs *pcs, unsigned int neg_mode,
497 			      struct phylink_link_state *state);
498 	int (*pcs_config)(struct phylink_pcs *pcs, unsigned int neg_mode,
499 			  phy_interface_t interface,
500 			  const unsigned long *advertising,
501 			  bool permit_pause_to_mac);
502 	void (*pcs_an_restart)(struct phylink_pcs *pcs);
503 	void (*pcs_link_up)(struct phylink_pcs *pcs, unsigned int neg_mode,
504 			    phy_interface_t interface, int speed, int duplex);
505 	void (*pcs_disable_eee)(struct phylink_pcs *pcs);
506 	void (*pcs_enable_eee)(struct phylink_pcs *pcs);
507 	int (*pcs_pre_init)(struct phylink_pcs *pcs);
508 };
509 
510 #if 0 /* For kernel-doc purposes only. */
511 /**
512  * pcs_validate() - validate the link configuration.
513  * @pcs: a pointer to a &struct phylink_pcs.
514  * @supported: ethtool bitmask for supported link modes.
515  * @state: a const pointer to a &struct phylink_link_state.
516  *
517  * Validate the interface mode, and advertising's autoneg bit, removing any
518  * media ethtool link modes that would not be supportable from the supported
519  * mask. Phylink will propagate the changes to the advertising mask. See the
520  * &struct phylink_mac_ops validate() method.
521  *
522  * Returns -EINVAL if the interface mode/autoneg mode is not supported.
523  * Returns non-zero positive if the link state can be supported.
524  */
525 int pcs_validate(struct phylink_pcs *pcs, unsigned long *supported,
526 		 const struct phylink_link_state *state);
527 
528 /**
529  * pcs_inband_caps - query PCS in-band capabilities for interface mode.
530  * @pcs: a pointer to a &struct phylink_pcs.
531  * @interface: interface mode to be queried
532  *
533  * Returns zero if it is unknown what in-band signalling is supported by the
534  * PHY (e.g. because the PHY driver doesn't implement the method.) Otherwise,
535  * returns a bit mask of the LINK_INBAND_* values from
536  * &enum link_inband_signalling to describe which inband modes are supported
537  * for this interface mode.
538  */
539 unsigned int pcs_inband_caps(struct phylink_pcs *pcs,
540 			     phy_interface_t interface);
541 
542 /**
543  * pcs_enable() - enable the PCS.
544  * @pcs: a pointer to a &struct phylink_pcs.
545  */
546 int pcs_enable(struct phylink_pcs *pcs);
547 
548 /**
549  * pcs_disable() - disable the PCS.
550  * @pcs: a pointer to a &struct phylink_pcs.
551  */
552 void pcs_disable(struct phylink_pcs *pcs);
553 
554 /**
555  * pcs_get_state() - Read the current inband link state from the hardware
556  * @pcs: a pointer to a &struct phylink_pcs.
557  * @neg_mode: link negotiation mode (PHYLINK_PCS_NEG_xxx)
558  * @state: a pointer to a &struct phylink_link_state.
559  *
560  * Read the current inband link state from the MAC PCS, reporting the
561  * current speed in @state->speed, duplex mode in @state->duplex, pause
562  * mode in @state->pause using the %MLO_PAUSE_RX and %MLO_PAUSE_TX bits,
563  * negotiation completion state in @state->an_complete, and link up state
564  * in @state->link. If possible, @state->lp_advertising should also be
565  * populated.
566  *
567  * Note that the @neg_mode parameter is always the PHYLINK_PCS_NEG_xxx
568  * state, not MLO_AN_xxx.
569  */
570 void pcs_get_state(struct phylink_pcs *pcs, unsigned int neg_mode,
571 		   struct phylink_link_state *state);
572 
573 /**
574  * pcs_config() - Configure the PCS mode and advertisement
575  * @pcs: a pointer to a &struct phylink_pcs.
576  * @neg_mode: link negotiation mode (see below)
577  * @interface: interface mode to be used
578  * @advertising: adertisement ethtool link mode mask
579  * @permit_pause_to_mac: permit forwarding pause resolution to MAC
580  *
581  * Configure the PCS for the operating mode, the interface mode, and set
582  * the advertisement mask. @permit_pause_to_mac indicates whether the
583  * hardware may forward the pause mode resolution to the MAC.
584  *
585  * When operating in %MLO_AN_INBAND, inband should always be enabled,
586  * otherwise inband should be disabled.
587  *
588  * For SGMII, there is no advertisement from the MAC side, the PCS should
589  * be programmed to acknowledge the inband word from the PHY.
590  *
591  * For 1000BASE-X, the advertisement should be programmed into the PCS.
592  *
593  * For most 10GBASE-R, there is no advertisement.
594  *
595  * The %neg_mode argument should be tested via the phylink_mode_*() family of
596  * functions, or for PCS that set pcs->neg_mode true, should be tested
597  * against the PHYLINK_PCS_NEG_* definitions.
598  *
599  * pcs_config() will be called when configuration of the PCS is required
600  * or when the advertisement is possibly updated. It must not unnecessarily
601  * disrupt an established link.
602  *
603  * When an autonegotiation restart is required for 802.3z modes, .pcs_config()
604  * should return a positive non-zero integer (e.g. 1) to indicate to phylink
605  * to call the pcs_an_restart() method.
606  */
607 int pcs_config(struct phylink_pcs *pcs, unsigned int neg_mode,
608 	       phy_interface_t interface, const unsigned long *advertising,
609 	       bool permit_pause_to_mac);
610 
611 /**
612  * pcs_an_restart() - restart 802.3z BaseX autonegotiation
613  * @pcs: a pointer to a &struct phylink_pcs.
614  *
615  * When PCS ops are present, this overrides mac_an_restart() in &struct
616  * phylink_mac_ops.
617  */
618 void pcs_an_restart(struct phylink_pcs *pcs);
619 
620 /**
621  * pcs_link_up() - program the PCS for the resolved link configuration
622  * @pcs: a pointer to a &struct phylink_pcs.
623  * @neg_mode: link negotiation mode (see below)
624  * @interface: link &typedef phy_interface_t mode
625  * @speed: link speed
626  * @duplex: link duplex
627  *
628  * This call will be made just before mac_link_up() to inform the PCS of
629  * the resolved link parameters. For example, a PCS operating in SGMII
630  * mode without in-band AN needs to be manually configured for the link
631  * and duplex setting. Otherwise, this should be a no-op.
632  *
633  * The %mode argument should be tested via the phylink_mode_*() family of
634  * functions, or for PCS that set pcs->neg_mode true, should be tested
635  * against the PHYLINK_PCS_NEG_* definitions.
636  */
637 void pcs_link_up(struct phylink_pcs *pcs, unsigned int neg_mode,
638 		 phy_interface_t interface, int speed, int duplex);
639 
640 /**
641  * pcs_disable_eee() - Disable EEE at the PCS
642  * @pcs: a pointer to a &struct phylink_pcs
643  *
644  * Optional method informing the PCS that EEE has been disabled at the MAC.
645  */
646 void pcs_disable_eee(struct phylink_pcs *pcs);
647 
648 /**
649  * pcs_enable_eee() - Enable EEE at the PCS
650  * @pcs: a pointer to a &struct phylink_pcs
651  *
652  * Optional method informing the PCS that EEE is about to be enabled at the MAC.
653  */
654 void pcs_enable_eee(struct phylink_pcs *pcs);
655 
656 /**
657  * pcs_pre_init() - Configure PCS components necessary for MAC initialization
658  * @pcs: a pointer to a &struct phylink_pcs.
659  *
660  * This function can be called by MAC drivers through the
661  * phylink_pcs_pre_init() wrapper, before their hardware is initialized. It
662  * should not be called after the link is brought up, as reconfiguring the PCS
663  * at this point could break the link.
664  *
665  * Some MAC devices require specific hardware initialization to be performed by
666  * their associated PCS device before they can properly initialize their own
667  * hardware. An example of this is the initialization of stmmac controllers,
668  * which requires an active REF_CLK signal to be provided by the PHY/PCS.
669  *
670  * By calling phylink_pcs_pre_init(), MAC drivers can ensure that the PCS is
671  * setup in a way that allows for successful hardware initialization.
672  *
673  * The specific configuration performed by pcs_pre_init() is dependent on the
674  * model of PCS and the requirements of the MAC device attached to it. PCS
675  * driver authors should consider whether their target device is to be used in
676  * conjunction with a MAC device whose driver calls phylink_pcs_pre_init(). MAC
677  * driver authors should document their requirements for the PCS
678  * pre-initialization.
679  *
680  */
681 int pcs_pre_init(struct phylink_pcs *pcs);
682 
683 #endif
684 
685 struct phylink *phylink_create(struct phylink_config *,
686 			       const struct fwnode_handle *,
687 			       phy_interface_t,
688 			       const struct phylink_mac_ops *);
689 void phylink_destroy(struct phylink *);
690 bool phylink_expects_phy(struct phylink *pl);
691 
692 int phylink_connect_phy(struct phylink *, struct phy_device *);
693 int phylink_of_phy_connect(struct phylink *, struct device_node *, u32 flags);
694 int phylink_fwnode_phy_connect(struct phylink *pl,
695 			       const struct fwnode_handle *fwnode,
696 			       u32 flags);
697 void phylink_disconnect_phy(struct phylink *);
698 int phylink_set_fixed_link(struct phylink *,
699 			   const struct phylink_link_state *);
700 
701 void phylink_mac_change(struct phylink *, bool up);
702 void phylink_pcs_change(struct phylink_pcs *, bool up);
703 
704 int phylink_pcs_pre_init(struct phylink *pl, struct phylink_pcs *pcs);
705 
706 void phylink_start(struct phylink *);
707 void phylink_stop(struct phylink *);
708 
709 void phylink_rx_clk_stop_block(struct phylink *);
710 void phylink_rx_clk_stop_unblock(struct phylink *);
711 
712 void phylink_suspend(struct phylink *pl, bool mac_wol);
713 void phylink_prepare_resume(struct phylink *pl);
714 void phylink_resume(struct phylink *pl);
715 
716 void phylink_ethtool_get_wol(struct phylink *, struct ethtool_wolinfo *);
717 int phylink_ethtool_set_wol(struct phylink *, struct ethtool_wolinfo *);
718 
719 int phylink_ethtool_ksettings_get(struct phylink *,
720 				  struct ethtool_link_ksettings *);
721 int phylink_ethtool_ksettings_set(struct phylink *,
722 				  const struct ethtool_link_ksettings *);
723 int phylink_ethtool_nway_reset(struct phylink *);
724 void phylink_ethtool_get_pauseparam(struct phylink *,
725 				    struct ethtool_pauseparam *);
726 int phylink_ethtool_set_pauseparam(struct phylink *,
727 				   struct ethtool_pauseparam *);
728 int phylink_get_eee_err(struct phylink *);
729 int phylink_ethtool_get_eee(struct phylink *link, struct ethtool_keee *eee);
730 int phylink_ethtool_set_eee(struct phylink *link, struct ethtool_keee *eee);
731 int phylink_mii_ioctl(struct phylink *, struct ifreq *, int);
732 int phylink_speed_down(struct phylink *pl, bool sync);
733 int phylink_speed_up(struct phylink *pl);
734 
735 #define phylink_zero(bm) \
736 	bitmap_zero(bm, __ETHTOOL_LINK_MODE_MASK_NBITS)
737 #define __phylink_do_bit(op, bm, mode) \
738 	op(ETHTOOL_LINK_MODE_ ## mode ## _BIT, bm)
739 
740 #define phylink_set(bm, mode)	__phylink_do_bit(__set_bit, bm, mode)
741 #define phylink_clear(bm, mode)	__phylink_do_bit(__clear_bit, bm, mode)
742 #define phylink_test(bm, mode)	__phylink_do_bit(test_bit, bm, mode)
743 
744 void phylink_set_port_modes(unsigned long *bits);
745 
746 /**
747  * phylink_get_link_timer_ns - return the PCS link timer value
748  * @interface: link &typedef phy_interface_t mode
749  *
750  * Return the PCS link timer setting in nanoseconds for the PHY @interface
751  * mode, or -EINVAL if not appropriate.
752  */
phylink_get_link_timer_ns(phy_interface_t interface)753 static inline int phylink_get_link_timer_ns(phy_interface_t interface)
754 {
755 	switch (interface) {
756 	case PHY_INTERFACE_MODE_SGMII:
757 	case PHY_INTERFACE_MODE_QSGMII:
758 	case PHY_INTERFACE_MODE_USXGMII:
759 	case PHY_INTERFACE_MODE_10G_QXGMII:
760 		return 1600000;
761 
762 	case PHY_INTERFACE_MODE_1000BASEX:
763 	case PHY_INTERFACE_MODE_2500BASEX:
764 		return 10000000;
765 
766 	default:
767 		return -EINVAL;
768 	}
769 }
770 
771 /**
772  * phylink_mac_implements_lpi() - determine if MAC implements LPI ops
773  * @ops: phylink_mac_ops structure
774  *
775  * Returns true if the phylink MAC operations structure indicates that the
776  * LPI operations have been implemented, false otherwise.
777  */
phylink_mac_implements_lpi(const struct phylink_mac_ops * ops)778 static inline bool phylink_mac_implements_lpi(const struct phylink_mac_ops *ops)
779 {
780 	return ops && ops->mac_disable_tx_lpi && ops->mac_enable_tx_lpi;
781 }
782 
783 void phylink_mii_c22_pcs_decode_state(struct phylink_link_state *state,
784 				      unsigned int neg_mode, u16 bmsr, u16 lpa);
785 void phylink_mii_c22_pcs_get_state(struct mdio_device *pcs,
786 				   unsigned int neg_mode,
787 				   struct phylink_link_state *state);
788 int phylink_mii_c22_pcs_encode_advertisement(phy_interface_t interface,
789 					     const unsigned long *advertising);
790 int phylink_mii_c22_pcs_config(struct mdio_device *pcs,
791 			       phy_interface_t interface,
792 			       const unsigned long *advertising,
793 			       unsigned int neg_mode);
794 void phylink_mii_c22_pcs_an_restart(struct mdio_device *pcs);
795 
796 void phylink_resolve_c73(struct phylink_link_state *state);
797 
798 void phylink_mii_c45_pcs_get_state(struct mdio_device *pcs,
799 				   struct phylink_link_state *state);
800 
801 void phylink_decode_usxgmii_word(struct phylink_link_state *state,
802 				 uint16_t lpa);
803 #endif
804