xref: /linux-6.15/include/linux/phylink.h (revision b5962294)
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_ASYM = BIT(0),
16 	MLO_PAUSE_SYM = BIT(1),
17 	MLO_PAUSE_RX = BIT(2),
18 	MLO_PAUSE_TX = BIT(3),
19 	MLO_PAUSE_TXRX_MASK = MLO_PAUSE_TX | MLO_PAUSE_RX,
20 	MLO_PAUSE_AN = BIT(4),
21 
22 	MLO_AN_PHY = 0,	/* Conventional PHY */
23 	MLO_AN_FIXED,	/* Fixed-link mode */
24 	MLO_AN_INBAND,	/* In-band protocol */
25 };
26 
27 static inline bool phylink_autoneg_inband(unsigned int mode)
28 {
29 	return mode == MLO_AN_INBAND;
30 }
31 
32 /**
33  * struct phylink_link_state - link state structure
34  * @advertising: ethtool bitmask containing advertised link modes
35  * @lp_advertising: ethtool bitmask containing link partner advertised link
36  *   modes
37  * @interface: link &typedef phy_interface_t mode
38  * @speed: link speed, one of the SPEED_* constants.
39  * @duplex: link duplex mode, one of DUPLEX_* constants.
40  * @pause: link pause state, described by MLO_PAUSE_* constants.
41  * @link: true if the link is up.
42  * @an_enabled: true if autonegotiation is enabled/desired.
43  * @an_complete: true if autonegotiation has completed.
44  */
45 struct phylink_link_state {
46 	__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
47 	__ETHTOOL_DECLARE_LINK_MODE_MASK(lp_advertising);
48 	phy_interface_t interface;
49 	int speed;
50 	int duplex;
51 	int pause;
52 	unsigned int link:1;
53 	unsigned int an_enabled:1;
54 	unsigned int an_complete:1;
55 };
56 
57 enum phylink_op_type {
58 	PHYLINK_NETDEV = 0,
59 	PHYLINK_DEV,
60 };
61 
62 /**
63  * struct phylink_config - PHYLINK configuration structure
64  * @dev: a pointer to a struct device associated with the MAC
65  * @type: operation type of PHYLINK instance
66  */
67 struct phylink_config {
68 	struct device *dev;
69 	enum phylink_op_type type;
70 };
71 
72 /**
73  * struct phylink_mac_ops - MAC operations structure.
74  * @validate: Validate and update the link configuration.
75  * @mac_link_state: Read the current link state from the hardware.
76  * @mac_config: configure the MAC for the selected mode and state.
77  * @mac_an_restart: restart 802.3z BaseX autonegotiation.
78  * @mac_link_down: take the link down.
79  * @mac_link_up: allow the link to come up.
80  *
81  * The individual methods are described more fully below.
82  */
83 struct phylink_mac_ops {
84 	void (*validate)(struct phylink_config *config,
85 			 unsigned long *supported,
86 			 struct phylink_link_state *state);
87 	int (*mac_link_state)(struct phylink_config *config,
88 			      struct phylink_link_state *state);
89 	void (*mac_config)(struct phylink_config *config, unsigned int mode,
90 			   const struct phylink_link_state *state);
91 	void (*mac_an_restart)(struct phylink_config *config);
92 	void (*mac_link_down)(struct phylink_config *config, unsigned int mode,
93 			      phy_interface_t interface);
94 	void (*mac_link_up)(struct phylink_config *config, unsigned int mode,
95 			    phy_interface_t interface,
96 			    struct phy_device *phy);
97 };
98 
99 #if 0 /* For kernel-doc purposes only. */
100 /**
101  * validate - Validate and update the link configuration
102  * @config: a pointer to a &struct phylink_config.
103  * @supported: ethtool bitmask for supported link modes.
104  * @state: a pointer to a &struct phylink_link_state.
105  *
106  * Clear bits in the @supported and @state->advertising masks that
107  * are not supportable by the MAC.
108  *
109  * Note that the PHY may be able to transform from one connection
110  * technology to another, so, eg, don't clear 1000BaseX just
111  * because the MAC is unable to BaseX mode. This is more about
112  * clearing unsupported speeds and duplex settings.
113  *
114  * If the @state->interface mode is %PHY_INTERFACE_MODE_1000BASEX
115  * or %PHY_INTERFACE_MODE_2500BASEX, select the appropriate mode
116  * based on @state->advertising and/or @state->speed and update
117  * @state->interface accordingly.
118  */
119 void validate(struct phylink_config *config, unsigned long *supported,
120 	      struct phylink_link_state *state);
121 
122 /**
123  * mac_link_state() - Read the current link state from the hardware
124  * @config: a pointer to a &struct phylink_config.
125  * @state: a pointer to a &struct phylink_link_state.
126  *
127  * Read the current link state from the MAC, reporting the current
128  * speed in @state->speed, duplex mode in @state->duplex, pause mode
129  * in @state->pause using the %MLO_PAUSE_RX and %MLO_PAUSE_TX bits,
130  * negotiation completion state in @state->an_complete, and link
131  * up state in @state->link.
132  */
133 int mac_link_state(struct phylink_config *config,
134 		   struct phylink_link_state *state);
135 
136 /**
137  * mac_config() - configure the MAC for the selected mode and state
138  * @config: a pointer to a &struct phylink_config.
139  * @mode: one of %MLO_AN_FIXED, %MLO_AN_PHY, %MLO_AN_INBAND.
140  * @state: a pointer to a &struct phylink_link_state.
141  *
142  * The action performed depends on the currently selected mode:
143  *
144  * %MLO_AN_FIXED, %MLO_AN_PHY:
145  *   Configure the specified @state->speed, @state->duplex and
146  *   @state->pause (%MLO_PAUSE_TX / %MLO_PAUSE_RX) mode.
147  *
148  * %MLO_AN_INBAND:
149  *   place the link in an inband negotiation mode (such as 802.3z
150  *   1000base-X or Cisco SGMII mode depending on the @state->interface
151  *   mode). In both cases, link state management (whether the link
152  *   is up or not) is performed by the MAC, and reported via the
153  *   mac_link_state() callback. Changes in link state must be made
154  *   by calling phylink_mac_change().
155  *
156  *   If in 802.3z mode, the link speed is fixed, dependent on the
157  *   @state->interface. Duplex is negotiated, and pause is advertised
158  *   according to @state->an_enabled, @state->pause and
159  *   @state->advertising flags. Beware of MACs which only support full
160  *   duplex at gigabit and higher speeds.
161  *
162  *   If in Cisco SGMII mode, the link speed and duplex mode are passed
163  *   in the serial bitstream 16-bit configuration word, and the MAC
164  *   should be configured to read these bits and acknowledge the
165  *   configuration word. Nothing is advertised by the MAC. The MAC is
166  *   responsible for reading the configuration word and configuring
167  *   itself accordingly.
168  *
169  * Implementations are expected to update the MAC to reflect the
170  * requested settings - i.o.w., if nothing has changed between two
171  * calls, no action is expected.  If only flow control settings have
172  * changed, flow control should be updated *without* taking the link
173  * down.  This "update" behaviour is critical to avoid bouncing the
174  * link up status.
175  */
176 void mac_config(struct phylink_config *config, unsigned int mode,
177 		const struct phylink_link_state *state);
178 
179 /**
180  * mac_an_restart() - restart 802.3z BaseX autonegotiation
181  * @config: a pointer to a &struct phylink_config.
182  */
183 void mac_an_restart(struct phylink_config *config);
184 
185 /**
186  * mac_link_down() - take the link down
187  * @config: a pointer to a &struct phylink_config.
188  * @mode: link autonegotiation mode
189  * @interface: link &typedef phy_interface_t mode
190  *
191  * If @mode is not an in-band negotiation mode (as defined by
192  * phylink_autoneg_inband()), force the link down and disable any
193  * Energy Efficient Ethernet MAC configuration. Interface type
194  * selection must be done in mac_config().
195  */
196 void mac_link_down(struct phylink_config *config, unsigned int mode,
197 		   phy_interface_t interface);
198 
199 /**
200  * mac_link_up() - allow the link to come up
201  * @config: a pointer to a &struct phylink_config.
202  * @mode: link autonegotiation mode
203  * @interface: link &typedef phy_interface_t mode
204  * @phy: any attached phy
205  *
206  * If @mode is not an in-band negotiation mode (as defined by
207  * phylink_autoneg_inband()), allow the link to come up. If @phy
208  * is non-%NULL, configure Energy Efficient Ethernet by calling
209  * phy_init_eee() and perform appropriate MAC configuration for EEE.
210  * Interface type selection must be done in mac_config().
211  */
212 void mac_link_up(struct phylink_config *config, unsigned int mode,
213 		 phy_interface_t interface,
214 		 struct phy_device *phy);
215 #endif
216 
217 struct phylink *phylink_create(struct phylink_config *, struct fwnode_handle *,
218 			       phy_interface_t iface,
219 			       const struct phylink_mac_ops *ops);
220 void phylink_destroy(struct phylink *);
221 
222 int phylink_connect_phy(struct phylink *, struct phy_device *);
223 int phylink_of_phy_connect(struct phylink *, struct device_node *, u32 flags);
224 void phylink_disconnect_phy(struct phylink *);
225 int phylink_fixed_state_cb(struct phylink *,
226 			   void (*cb)(struct net_device *dev,
227 				      struct phylink_link_state *));
228 
229 void phylink_mac_change(struct phylink *, bool up);
230 
231 void phylink_start(struct phylink *);
232 void phylink_stop(struct phylink *);
233 
234 void phylink_ethtool_get_wol(struct phylink *, struct ethtool_wolinfo *);
235 int phylink_ethtool_set_wol(struct phylink *, struct ethtool_wolinfo *);
236 
237 int phylink_ethtool_ksettings_get(struct phylink *,
238 				  struct ethtool_link_ksettings *);
239 int phylink_ethtool_ksettings_set(struct phylink *,
240 				  const struct ethtool_link_ksettings *);
241 int phylink_ethtool_nway_reset(struct phylink *);
242 void phylink_ethtool_get_pauseparam(struct phylink *,
243 				    struct ethtool_pauseparam *);
244 int phylink_ethtool_set_pauseparam(struct phylink *,
245 				   struct ethtool_pauseparam *);
246 int phylink_get_eee_err(struct phylink *);
247 int phylink_init_eee(struct phylink *, bool);
248 int phylink_ethtool_get_eee(struct phylink *, struct ethtool_eee *);
249 int phylink_ethtool_set_eee(struct phylink *, struct ethtool_eee *);
250 int phylink_mii_ioctl(struct phylink *, struct ifreq *, int);
251 
252 #define phylink_zero(bm) \
253 	bitmap_zero(bm, __ETHTOOL_LINK_MODE_MASK_NBITS)
254 #define __phylink_do_bit(op, bm, mode) \
255 	op(ETHTOOL_LINK_MODE_ ## mode ## _BIT, bm)
256 
257 #define phylink_set(bm, mode)	__phylink_do_bit(__set_bit, bm, mode)
258 #define phylink_clear(bm, mode)	__phylink_do_bit(__clear_bit, bm, mode)
259 #define phylink_test(bm, mode)	__phylink_do_bit(test_bit, bm, mode)
260 
261 void phylink_set_port_modes(unsigned long *bits);
262 void phylink_helper_basex_speed(struct phylink_link_state *state);
263 
264 #endif
265