5 #include <linux/spinlock.h>
6 #include <linux/workqueue.h>
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),
20 MLO_AN_PHY = 0, /* Conventional PHY */
21 MLO_AN_FIXED, /* Fixed-link mode */
22 MLO_AN_INBAND, /* In-band protocol */
25 static inline bool phylink_autoneg_inband(unsigned int mode)
27 return mode == MLO_AN_INBAND;
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
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.
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;
51 unsigned int an_enabled:1;
52 unsigned int an_complete:1;
55 enum phylink_op_type {
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
66 struct phylink_config {
68 enum phylink_op_type type;
73 * struct phylink_mac_ops - MAC operations structure.
74 * @validate: Validate and update the link configuration.
75 * @mac_pcs_get_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.
81 * The individual methods are described more fully below.
83 struct phylink_mac_ops {
84 void (*validate)(struct phylink_config *config,
85 unsigned long *supported,
86 struct phylink_link_state *state);
87 void (*mac_pcs_get_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);
99 #if 0 /* For kernel-doc purposes only. */
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.
106 * Clear bits in the @supported and @state->advertising masks that
107 * are not supportable by the MAC.
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. The port modes
113 * should not be cleared; phylink_set_port_modes() will help with this.
115 * If the @state->interface mode is %PHY_INTERFACE_MODE_1000BASEX
116 * or %PHY_INTERFACE_MODE_2500BASEX, select the appropriate mode
117 * based on @state->advertising and/or @state->speed and update
118 * @state->interface accordingly. See phylink_helper_basex_speed().
120 * When @state->interface is %PHY_INTERFACE_MODE_NA, phylink expects the
121 * MAC driver to return all supported link modes.
123 * If the @state->interface mode is not supported, then the @supported
124 * mask must be cleared.
126 void validate(struct phylink_config *config, unsigned long *supported,
127 struct phylink_link_state *state);
130 * mac_pcs_get_state() - Read the current inband link state from the hardware
131 * @config: a pointer to a &struct phylink_config.
132 * @state: a pointer to a &struct phylink_link_state.
134 * Read the current inband link state from the MAC PCS, reporting the
135 * current speed in @state->speed, duplex mode in @state->duplex, pause
136 * mode in @state->pause using the %MLO_PAUSE_RX and %MLO_PAUSE_TX bits,
137 * negotiation completion state in @state->an_complete, and link up state
138 * in @state->link. If possible, @state->lp_advertising should also be
141 void mac_pcs_get_state(struct phylink_config *config,
142 struct phylink_link_state *state);
145 * mac_config() - configure the MAC for the selected mode and state
146 * @config: a pointer to a &struct phylink_config.
147 * @mode: one of %MLO_AN_FIXED, %MLO_AN_PHY, %MLO_AN_INBAND.
148 * @state: a pointer to a &struct phylink_link_state.
150 * Note - not all members of @state are valid. In particular,
151 * @state->lp_advertising, @state->link, @state->an_complete are never
152 * guaranteed to be correct, and so any mac_config() implementation must
153 * never reference these fields.
155 * In all negotiation modes, as defined by @mode, @state->pause indicates the
156 * pause settings which should be applied as follows. If %MLO_PAUSE_AN is not
157 * set, %MLO_PAUSE_TX and %MLO_PAUSE_RX indicate whether the MAC should send
158 * pause frames and/or act on received pause frames respectively. Otherwise,
159 * the results of in-band negotiation/status from the MAC PCS should be used
160 * to control the MAC pause mode settings.
162 * The action performed depends on the currently selected mode:
164 * %MLO_AN_FIXED, %MLO_AN_PHY:
165 * Configure the specified @state->speed and @state->duplex over a link
166 * specified by @state->interface. @state->advertising may be used, but
167 * is not required. Pause modes as above. Other members of @state must
170 * Valid state members: interface, speed, duplex, pause, advertising.
173 * place the link in an inband negotiation mode (such as 802.3z
174 * 1000base-X or Cisco SGMII mode depending on the @state->interface
175 * mode). In both cases, link state management (whether the link
176 * is up or not) is performed by the MAC, and reported via the
177 * mac_pcs_get_state() callback. Changes in link state must be made
178 * by calling phylink_mac_change().
180 * Interface mode specific details are mentioned below.
182 * If in 802.3z mode, the link speed is fixed, dependent on the
183 * @state->interface. Duplex and pause modes are negotiated via
184 * the in-band configuration word. Advertised pause modes are set
185 * according to the @state->an_enabled and @state->advertising
186 * flags. Beware of MACs which only support full duplex at gigabit
189 * If in Cisco SGMII mode, the link speed and duplex mode are passed
190 * in the serial bitstream 16-bit configuration word, and the MAC
191 * should be configured to read these bits and acknowledge the
192 * configuration word. Nothing is advertised by the MAC. The MAC is
193 * responsible for reading the configuration word and configuring
194 * itself accordingly.
196 * Valid state members: interface, an_enabled, pause, advertising.
198 * Implementations are expected to update the MAC to reflect the
199 * requested settings - i.o.w., if nothing has changed between two
200 * calls, no action is expected. If only flow control settings have
201 * changed, flow control should be updated *without* taking the link
202 * down. This "update" behaviour is critical to avoid bouncing the
205 void mac_config(struct phylink_config *config, unsigned int mode,
206 const struct phylink_link_state *state);
209 * mac_an_restart() - restart 802.3z BaseX autonegotiation
210 * @config: a pointer to a &struct phylink_config.
212 void mac_an_restart(struct phylink_config *config);
215 * mac_link_down() - take the link down
216 * @config: a pointer to a &struct phylink_config.
217 * @mode: link autonegotiation mode
218 * @interface: link &typedef phy_interface_t mode
220 * If @mode is not an in-band negotiation mode (as defined by
221 * phylink_autoneg_inband()), force the link down and disable any
222 * Energy Efficient Ethernet MAC configuration. Interface type
223 * selection must be done in mac_config().
225 void mac_link_down(struct phylink_config *config, unsigned int mode,
226 phy_interface_t interface);
229 * mac_link_up() - allow the link to come up
230 * @config: a pointer to a &struct phylink_config.
231 * @mode: link autonegotiation mode
232 * @interface: link &typedef phy_interface_t mode
233 * @phy: any attached phy
235 * If @mode is not an in-band negotiation mode (as defined by
236 * phylink_autoneg_inband()), allow the link to come up. If @phy
237 * is non-%NULL, configure Energy Efficient Ethernet by calling
238 * phy_init_eee() and perform appropriate MAC configuration for EEE.
239 * Interface type selection must be done in mac_config().
241 void mac_link_up(struct phylink_config *config, unsigned int mode,
242 phy_interface_t interface,
243 struct phy_device *phy);
246 struct phylink *phylink_create(struct phylink_config *, struct fwnode_handle *,
247 phy_interface_t iface,
248 const struct phylink_mac_ops *ops);
249 void phylink_destroy(struct phylink *);
251 int phylink_connect_phy(struct phylink *, struct phy_device *);
252 int phylink_of_phy_connect(struct phylink *, struct device_node *, u32 flags);
253 void phylink_disconnect_phy(struct phylink *);
254 int phylink_fixed_state_cb(struct phylink *,
255 void (*cb)(struct net_device *dev,
256 struct phylink_link_state *));
258 void phylink_mac_change(struct phylink *, bool up);
260 void phylink_start(struct phylink *);
261 void phylink_stop(struct phylink *);
263 void phylink_ethtool_get_wol(struct phylink *, struct ethtool_wolinfo *);
264 int phylink_ethtool_set_wol(struct phylink *, struct ethtool_wolinfo *);
266 int phylink_ethtool_ksettings_get(struct phylink *,
267 struct ethtool_link_ksettings *);
268 int phylink_ethtool_ksettings_set(struct phylink *,
269 const struct ethtool_link_ksettings *);
270 int phylink_ethtool_nway_reset(struct phylink *);
271 void phylink_ethtool_get_pauseparam(struct phylink *,
272 struct ethtool_pauseparam *);
273 int phylink_ethtool_set_pauseparam(struct phylink *,
274 struct ethtool_pauseparam *);
275 int phylink_get_eee_err(struct phylink *);
276 int phylink_init_eee(struct phylink *, bool);
277 int phylink_ethtool_get_eee(struct phylink *, struct ethtool_eee *);
278 int phylink_ethtool_set_eee(struct phylink *, struct ethtool_eee *);
279 int phylink_mii_ioctl(struct phylink *, struct ifreq *, int);
281 #define phylink_zero(bm) \
282 bitmap_zero(bm, __ETHTOOL_LINK_MODE_MASK_NBITS)
283 #define __phylink_do_bit(op, bm, mode) \
284 op(ETHTOOL_LINK_MODE_ ## mode ## _BIT, bm)
286 #define phylink_set(bm, mode) __phylink_do_bit(__set_bit, bm, mode)
287 #define phylink_clear(bm, mode) __phylink_do_bit(__clear_bit, bm, mode)
288 #define phylink_test(bm, mode) __phylink_do_bit(test_bit, bm, mode)
290 void phylink_set_port_modes(unsigned long *bits);
291 void phylink_helper_basex_speed(struct phylink_link_state *state);