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