smp, irq_work: Continue smp_call_function*() and irq_work*() integration
[sfrench/cifs-2.6.git] / include / linux / mdio.h
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * linux/mdio.h: definitions for MDIO (clause 45) transceivers
4  * Copyright 2006-2009 Solarflare Communications Inc.
5  */
6 #ifndef __LINUX_MDIO_H__
7 #define __LINUX_MDIO_H__
8
9 #include <uapi/linux/mdio.h>
10 #include <linux/mod_devicetable.h>
11
12 /* Or MII_ADDR_C45 into regnum for read/write on mii_bus to enable the 21 bit
13  * IEEE 802.3ae clause 45 addressing mode used by 10GIGE phy chips.
14  */
15 #define MII_ADDR_C45            (1<<30)
16 #define MII_DEVADDR_C45_SHIFT   16
17 #define MII_REGADDR_C45_MASK    GENMASK(15, 0)
18
19 struct gpio_desc;
20 struct mii_bus;
21
22 /* Multiple levels of nesting are possible. However typically this is
23  * limited to nested DSA like layer, a MUX layer, and the normal
24  * user. Instead of trying to handle the general case, just define
25  * these cases.
26  */
27 enum mdio_mutex_lock_class {
28         MDIO_MUTEX_NORMAL,
29         MDIO_MUTEX_MUX,
30         MDIO_MUTEX_NESTED,
31 };
32
33 struct mdio_device {
34         struct device dev;
35
36         struct mii_bus *bus;
37         char modalias[MDIO_NAME_SIZE];
38
39         int (*bus_match)(struct device *dev, struct device_driver *drv);
40         void (*device_free)(struct mdio_device *mdiodev);
41         void (*device_remove)(struct mdio_device *mdiodev);
42
43         /* Bus address of the MDIO device (0-31) */
44         int addr;
45         int flags;
46         struct gpio_desc *reset_gpio;
47         struct reset_control *reset_ctrl;
48         unsigned int reset_assert_delay;
49         unsigned int reset_deassert_delay;
50 };
51 #define to_mdio_device(d) container_of(d, struct mdio_device, dev)
52
53 /* struct mdio_driver_common: Common to all MDIO drivers */
54 struct mdio_driver_common {
55         struct device_driver driver;
56         int flags;
57 };
58 #define MDIO_DEVICE_FLAG_PHY            1
59 #define to_mdio_common_driver(d) \
60         container_of(d, struct mdio_driver_common, driver)
61
62 /* struct mdio_driver: Generic MDIO driver */
63 struct mdio_driver {
64         struct mdio_driver_common mdiodrv;
65
66         /*
67          * Called during discovery.  Used to set
68          * up device-specific structures, if any
69          */
70         int (*probe)(struct mdio_device *mdiodev);
71
72         /* Clears up any memory if needed */
73         void (*remove)(struct mdio_device *mdiodev);
74 };
75 #define to_mdio_driver(d)                                               \
76         container_of(to_mdio_common_driver(d), struct mdio_driver, mdiodrv)
77
78 /* device driver data */
79 static inline void mdiodev_set_drvdata(struct mdio_device *mdio, void *data)
80 {
81         dev_set_drvdata(&mdio->dev, data);
82 }
83
84 static inline void *mdiodev_get_drvdata(struct mdio_device *mdio)
85 {
86         return dev_get_drvdata(&mdio->dev);
87 }
88
89 void mdio_device_free(struct mdio_device *mdiodev);
90 struct mdio_device *mdio_device_create(struct mii_bus *bus, int addr);
91 int mdio_device_register(struct mdio_device *mdiodev);
92 void mdio_device_remove(struct mdio_device *mdiodev);
93 void mdio_device_reset(struct mdio_device *mdiodev, int value);
94 int mdio_driver_register(struct mdio_driver *drv);
95 void mdio_driver_unregister(struct mdio_driver *drv);
96 int mdio_device_bus_match(struct device *dev, struct device_driver *drv);
97
98 static inline bool mdio_phy_id_is_c45(int phy_id)
99 {
100         return (phy_id & MDIO_PHY_ID_C45) && !(phy_id & ~MDIO_PHY_ID_C45_MASK);
101 }
102
103 static inline __u16 mdio_phy_id_prtad(int phy_id)
104 {
105         return (phy_id & MDIO_PHY_ID_PRTAD) >> 5;
106 }
107
108 static inline __u16 mdio_phy_id_devad(int phy_id)
109 {
110         return phy_id & MDIO_PHY_ID_DEVAD;
111 }
112
113 /**
114  * struct mdio_if_info - Ethernet controller MDIO interface
115  * @prtad: PRTAD of the PHY (%MDIO_PRTAD_NONE if not present/unknown)
116  * @mmds: Mask of MMDs expected to be present in the PHY.  This must be
117  *      non-zero unless @prtad = %MDIO_PRTAD_NONE.
118  * @mode_support: MDIO modes supported.  If %MDIO_SUPPORTS_C22 is set then
119  *      MII register access will be passed through with @devad =
120  *      %MDIO_DEVAD_NONE.  If %MDIO_EMULATE_C22 is set then access to
121  *      commonly used clause 22 registers will be translated into
122  *      clause 45 registers.
123  * @dev: Net device structure
124  * @mdio_read: Register read function; returns value or negative error code
125  * @mdio_write: Register write function; returns 0 or negative error code
126  */
127 struct mdio_if_info {
128         int prtad;
129         u32 mmds;
130         unsigned mode_support;
131
132         struct net_device *dev;
133         int (*mdio_read)(struct net_device *dev, int prtad, int devad,
134                          u16 addr);
135         int (*mdio_write)(struct net_device *dev, int prtad, int devad,
136                           u16 addr, u16 val);
137 };
138
139 #define MDIO_PRTAD_NONE                 (-1)
140 #define MDIO_DEVAD_NONE                 (-1)
141 #define MDIO_SUPPORTS_C22               1
142 #define MDIO_SUPPORTS_C45               2
143 #define MDIO_EMULATE_C22                4
144
145 struct ethtool_cmd;
146 struct ethtool_pauseparam;
147 extern int mdio45_probe(struct mdio_if_info *mdio, int prtad);
148 extern int mdio_set_flag(const struct mdio_if_info *mdio,
149                          int prtad, int devad, u16 addr, int mask,
150                          bool sense);
151 extern int mdio45_links_ok(const struct mdio_if_info *mdio, u32 mmds);
152 extern int mdio45_nway_restart(const struct mdio_if_info *mdio);
153 extern void mdio45_ethtool_gset_npage(const struct mdio_if_info *mdio,
154                                       struct ethtool_cmd *ecmd,
155                                       u32 npage_adv, u32 npage_lpa);
156 extern void
157 mdio45_ethtool_ksettings_get_npage(const struct mdio_if_info *mdio,
158                                    struct ethtool_link_ksettings *cmd,
159                                    u32 npage_adv, u32 npage_lpa);
160
161 /**
162  * mdio45_ethtool_gset - get settings for ETHTOOL_GSET
163  * @mdio: MDIO interface
164  * @ecmd: Ethtool request structure
165  *
166  * Since the CSRs for auto-negotiation using next pages are not fully
167  * standardised, this function does not attempt to decode them.  Use
168  * mdio45_ethtool_gset_npage() to specify advertisement bits from next
169  * pages.
170  */
171 static inline void mdio45_ethtool_gset(const struct mdio_if_info *mdio,
172                                        struct ethtool_cmd *ecmd)
173 {
174         mdio45_ethtool_gset_npage(mdio, ecmd, 0, 0);
175 }
176
177 /**
178  * mdio45_ethtool_ksettings_get - get settings for ETHTOOL_GLINKSETTINGS
179  * @mdio: MDIO interface
180  * @cmd: Ethtool request structure
181  *
182  * Since the CSRs for auto-negotiation using next pages are not fully
183  * standardised, this function does not attempt to decode them.  Use
184  * mdio45_ethtool_ksettings_get_npage() to specify advertisement bits
185  * from next pages.
186  */
187 static inline void
188 mdio45_ethtool_ksettings_get(const struct mdio_if_info *mdio,
189                              struct ethtool_link_ksettings *cmd)
190 {
191         mdio45_ethtool_ksettings_get_npage(mdio, cmd, 0, 0);
192 }
193
194 extern int mdio_mii_ioctl(const struct mdio_if_info *mdio,
195                           struct mii_ioctl_data *mii_data, int cmd);
196
197 /**
198  * mmd_eee_cap_to_ethtool_sup_t
199  * @eee_cap: value of the MMD EEE Capability register
200  *
201  * A small helper function that translates MMD EEE Capability (3.20) bits
202  * to ethtool supported settings.
203  */
204 static inline u32 mmd_eee_cap_to_ethtool_sup_t(u16 eee_cap)
205 {
206         u32 supported = 0;
207
208         if (eee_cap & MDIO_EEE_100TX)
209                 supported |= SUPPORTED_100baseT_Full;
210         if (eee_cap & MDIO_EEE_1000T)
211                 supported |= SUPPORTED_1000baseT_Full;
212         if (eee_cap & MDIO_EEE_10GT)
213                 supported |= SUPPORTED_10000baseT_Full;
214         if (eee_cap & MDIO_EEE_1000KX)
215                 supported |= SUPPORTED_1000baseKX_Full;
216         if (eee_cap & MDIO_EEE_10GKX4)
217                 supported |= SUPPORTED_10000baseKX4_Full;
218         if (eee_cap & MDIO_EEE_10GKR)
219                 supported |= SUPPORTED_10000baseKR_Full;
220
221         return supported;
222 }
223
224 /**
225  * mmd_eee_adv_to_ethtool_adv_t
226  * @eee_adv: value of the MMD EEE Advertisement/Link Partner Ability registers
227  *
228  * A small helper function that translates the MMD EEE Advertisment (7.60)
229  * and MMD EEE Link Partner Ability (7.61) bits to ethtool advertisement
230  * settings.
231  */
232 static inline u32 mmd_eee_adv_to_ethtool_adv_t(u16 eee_adv)
233 {
234         u32 adv = 0;
235
236         if (eee_adv & MDIO_EEE_100TX)
237                 adv |= ADVERTISED_100baseT_Full;
238         if (eee_adv & MDIO_EEE_1000T)
239                 adv |= ADVERTISED_1000baseT_Full;
240         if (eee_adv & MDIO_EEE_10GT)
241                 adv |= ADVERTISED_10000baseT_Full;
242         if (eee_adv & MDIO_EEE_1000KX)
243                 adv |= ADVERTISED_1000baseKX_Full;
244         if (eee_adv & MDIO_EEE_10GKX4)
245                 adv |= ADVERTISED_10000baseKX4_Full;
246         if (eee_adv & MDIO_EEE_10GKR)
247                 adv |= ADVERTISED_10000baseKR_Full;
248
249         return adv;
250 }
251
252 /**
253  * ethtool_adv_to_mmd_eee_adv_t
254  * @adv: the ethtool advertisement settings
255  *
256  * A small helper function that translates ethtool advertisement settings
257  * to EEE advertisements for the MMD EEE Advertisement (7.60) and
258  * MMD EEE Link Partner Ability (7.61) registers.
259  */
260 static inline u16 ethtool_adv_to_mmd_eee_adv_t(u32 adv)
261 {
262         u16 reg = 0;
263
264         if (adv & ADVERTISED_100baseT_Full)
265                 reg |= MDIO_EEE_100TX;
266         if (adv & ADVERTISED_1000baseT_Full)
267                 reg |= MDIO_EEE_1000T;
268         if (adv & ADVERTISED_10000baseT_Full)
269                 reg |= MDIO_EEE_10GT;
270         if (adv & ADVERTISED_1000baseKX_Full)
271                 reg |= MDIO_EEE_1000KX;
272         if (adv & ADVERTISED_10000baseKX4_Full)
273                 reg |= MDIO_EEE_10GKX4;
274         if (adv & ADVERTISED_10000baseKR_Full)
275                 reg |= MDIO_EEE_10GKR;
276
277         return reg;
278 }
279
280 /**
281  * linkmode_adv_to_mii_10gbt_adv_t
282  * @advertising: the linkmode advertisement settings
283  *
284  * A small helper function that translates linkmode advertisement
285  * settings to phy autonegotiation advertisements for the C45
286  * 10GBASE-T AN CONTROL (7.32) register.
287  */
288 static inline u32 linkmode_adv_to_mii_10gbt_adv_t(unsigned long *advertising)
289 {
290         u32 result = 0;
291
292         if (linkmode_test_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
293                               advertising))
294                 result |= MDIO_AN_10GBT_CTRL_ADV2_5G;
295         if (linkmode_test_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT,
296                               advertising))
297                 result |= MDIO_AN_10GBT_CTRL_ADV5G;
298         if (linkmode_test_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
299                               advertising))
300                 result |= MDIO_AN_10GBT_CTRL_ADV10G;
301
302         return result;
303 }
304
305 /**
306  * mii_10gbt_stat_mod_linkmode_lpa_t
307  * @advertising: target the linkmode advertisement settings
308  * @adv: value of the C45 10GBASE-T AN STATUS register
309  *
310  * A small helper function that translates C45 10GBASE-T AN STATUS register bits
311  * to linkmode advertisement settings. Other bits in advertising aren't changed.
312  */
313 static inline void mii_10gbt_stat_mod_linkmode_lpa_t(unsigned long *advertising,
314                                                      u32 lpa)
315 {
316         linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
317                          advertising, lpa & MDIO_AN_10GBT_STAT_LP2_5G);
318         linkmode_mod_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT,
319                          advertising, lpa & MDIO_AN_10GBT_STAT_LP5G);
320         linkmode_mod_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
321                          advertising, lpa & MDIO_AN_10GBT_STAT_LP10G);
322 }
323
324 int __mdiobus_read(struct mii_bus *bus, int addr, u32 regnum);
325 int __mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val);
326 int __mdiobus_modify_changed(struct mii_bus *bus, int addr, u32 regnum,
327                              u16 mask, u16 set);
328
329 int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum);
330 int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum);
331 int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val);
332 int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val);
333 int mdiobus_modify(struct mii_bus *bus, int addr, u32 regnum, u16 mask,
334                    u16 set);
335
336 static inline u32 mdiobus_c45_addr(int devad, u16 regnum)
337 {
338         return MII_ADDR_C45 | devad << MII_DEVADDR_C45_SHIFT | regnum;
339 }
340
341 static inline int __mdiobus_c45_read(struct mii_bus *bus, int prtad, int devad,
342                                      u16 regnum)
343 {
344         return __mdiobus_read(bus, prtad, mdiobus_c45_addr(devad, regnum));
345 }
346
347 static inline int __mdiobus_c45_write(struct mii_bus *bus, int prtad, int devad,
348                                       u16 regnum, u16 val)
349 {
350         return __mdiobus_write(bus, prtad, mdiobus_c45_addr(devad, regnum),
351                                val);
352 }
353
354 static inline int mdiobus_c45_read(struct mii_bus *bus, int prtad, int devad,
355                                    u16 regnum)
356 {
357         return mdiobus_read(bus, prtad, mdiobus_c45_addr(devad, regnum));
358 }
359
360 int mdiobus_register_device(struct mdio_device *mdiodev);
361 int mdiobus_unregister_device(struct mdio_device *mdiodev);
362 bool mdiobus_is_registered_device(struct mii_bus *bus, int addr);
363 struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr);
364
365 /**
366  * mdio_module_driver() - Helper macro for registering mdio drivers
367  *
368  * Helper macro for MDIO drivers which do not do anything special in module
369  * init/exit. Each module may only use this macro once, and calling it
370  * replaces module_init() and module_exit().
371  */
372 #define mdio_module_driver(_mdio_driver)                                \
373 static int __init mdio_module_init(void)                                \
374 {                                                                       \
375         return mdio_driver_register(&_mdio_driver);                     \
376 }                                                                       \
377 module_init(mdio_module_init);                                          \
378 static void __exit mdio_module_exit(void)                               \
379 {                                                                       \
380         mdio_driver_unregister(&_mdio_driver);                          \
381 }                                                                       \
382 module_exit(mdio_module_exit)
383
384 #endif /* __LINUX_MDIO_H__ */