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