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