GNU Linux-libre 4.14.266-gnu1
[releases.git] / include / linux / mdio.h
1 /*
2  * linux/mdio.h: definitions for MDIO (clause 45) transceivers
3  * Copyright 2006-2009 Solarflare Communications Inc.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published
7  * by the Free Software Foundation, incorporated herein by reference.
8  */
9 #ifndef __LINUX_MDIO_H__
10 #define __LINUX_MDIO_H__
11
12 #include <uapi/linux/mdio.h>
13 #include <linux/mod_devicetable.h>
14
15 struct mii_bus;
16
17 /* Multiple levels of nesting are possible. However typically this is
18  * limited to nested DSA like layer, a MUX layer, and the normal
19  * user. Instead of trying to handle the general case, just define
20  * these cases.
21  */
22 enum mdio_mutex_lock_class {
23         MDIO_MUTEX_NORMAL,
24         MDIO_MUTEX_MUX,
25         MDIO_MUTEX_NESTED,
26 };
27
28 struct mdio_device {
29         struct device dev;
30
31         const struct dev_pm_ops *pm_ops;
32         struct mii_bus *bus;
33         char modalias[MDIO_NAME_SIZE];
34
35         int (*bus_match)(struct device *dev, struct device_driver *drv);
36         void (*device_free)(struct mdio_device *mdiodev);
37         void (*device_remove)(struct mdio_device *mdiodev);
38
39         /* Bus address of the MDIO device (0-31) */
40         int addr;
41         int flags;
42 };
43 #define to_mdio_device(d) container_of(d, struct mdio_device, dev)
44
45 /* struct mdio_driver_common: Common to all MDIO drivers */
46 struct mdio_driver_common {
47         struct device_driver driver;
48         int flags;
49 };
50 #define MDIO_DEVICE_FLAG_PHY            1
51 #define to_mdio_common_driver(d) \
52         container_of(d, struct mdio_driver_common, driver)
53
54 /* struct mdio_driver: Generic MDIO driver */
55 struct mdio_driver {
56         struct mdio_driver_common mdiodrv;
57
58         /*
59          * Called during discovery.  Used to set
60          * up device-specific structures, if any
61          */
62         int (*probe)(struct mdio_device *mdiodev);
63
64         /* Clears up any memory if needed */
65         void (*remove)(struct mdio_device *mdiodev);
66
67         /* Quiesces the device on system shutdown, turns off interrupts etc */
68         void (*shutdown)(struct mdio_device *mdiodev);
69 };
70 #define to_mdio_driver(d)                                               \
71         container_of(to_mdio_common_driver(d), struct mdio_driver, mdiodrv)
72
73 void mdio_device_free(struct mdio_device *mdiodev);
74 struct mdio_device *mdio_device_create(struct mii_bus *bus, int addr);
75 int mdio_device_register(struct mdio_device *mdiodev);
76 void mdio_device_remove(struct mdio_device *mdiodev);
77 int mdio_driver_register(struct mdio_driver *drv);
78 void mdio_driver_unregister(struct mdio_driver *drv);
79 int mdio_device_bus_match(struct device *dev, struct device_driver *drv);
80
81 static inline bool mdio_phy_id_is_c45(int phy_id)
82 {
83         return (phy_id & MDIO_PHY_ID_C45) && !(phy_id & ~MDIO_PHY_ID_C45_MASK);
84 }
85
86 static inline __u16 mdio_phy_id_prtad(int phy_id)
87 {
88         return (phy_id & MDIO_PHY_ID_PRTAD) >> 5;
89 }
90
91 static inline __u16 mdio_phy_id_devad(int phy_id)
92 {
93         return phy_id & MDIO_PHY_ID_DEVAD;
94 }
95
96 /**
97  * struct mdio_if_info - Ethernet controller MDIO interface
98  * @prtad: PRTAD of the PHY (%MDIO_PRTAD_NONE if not present/unknown)
99  * @mmds: Mask of MMDs expected to be present in the PHY.  This must be
100  *      non-zero unless @prtad = %MDIO_PRTAD_NONE.
101  * @mode_support: MDIO modes supported.  If %MDIO_SUPPORTS_C22 is set then
102  *      MII register access will be passed through with @devad =
103  *      %MDIO_DEVAD_NONE.  If %MDIO_EMULATE_C22 is set then access to
104  *      commonly used clause 22 registers will be translated into
105  *      clause 45 registers.
106  * @dev: Net device structure
107  * @mdio_read: Register read function; returns value or negative error code
108  * @mdio_write: Register write function; returns 0 or negative error code
109  */
110 struct mdio_if_info {
111         int prtad;
112         u32 mmds;
113         unsigned mode_support;
114
115         struct net_device *dev;
116         int (*mdio_read)(struct net_device *dev, int prtad, int devad,
117                          u16 addr);
118         int (*mdio_write)(struct net_device *dev, int prtad, int devad,
119                           u16 addr, u16 val);
120 };
121
122 #define MDIO_PRTAD_NONE                 (-1)
123 #define MDIO_DEVAD_NONE                 (-1)
124 #define MDIO_SUPPORTS_C22               1
125 #define MDIO_SUPPORTS_C45               2
126 #define MDIO_EMULATE_C22                4
127
128 struct ethtool_cmd;
129 struct ethtool_pauseparam;
130 extern int mdio45_probe(struct mdio_if_info *mdio, int prtad);
131 extern int mdio_set_flag(const struct mdio_if_info *mdio,
132                          int prtad, int devad, u16 addr, int mask,
133                          bool sense);
134 extern int mdio45_links_ok(const struct mdio_if_info *mdio, u32 mmds);
135 extern int mdio45_nway_restart(const struct mdio_if_info *mdio);
136 extern void mdio45_ethtool_gset_npage(const struct mdio_if_info *mdio,
137                                       struct ethtool_cmd *ecmd,
138                                       u32 npage_adv, u32 npage_lpa);
139 extern void
140 mdio45_ethtool_ksettings_get_npage(const struct mdio_if_info *mdio,
141                                    struct ethtool_link_ksettings *cmd,
142                                    u32 npage_adv, u32 npage_lpa);
143
144 /**
145  * mdio45_ethtool_gset - get settings for ETHTOOL_GSET
146  * @mdio: MDIO interface
147  * @ecmd: Ethtool request structure
148  *
149  * Since the CSRs for auto-negotiation using next pages are not fully
150  * standardised, this function does not attempt to decode them.  Use
151  * mdio45_ethtool_gset_npage() to specify advertisement bits from next
152  * pages.
153  */
154 static inline void mdio45_ethtool_gset(const struct mdio_if_info *mdio,
155                                        struct ethtool_cmd *ecmd)
156 {
157         mdio45_ethtool_gset_npage(mdio, ecmd, 0, 0);
158 }
159
160 /**
161  * mdio45_ethtool_ksettings_get - get settings for ETHTOOL_GLINKSETTINGS
162  * @mdio: MDIO interface
163  * @cmd: Ethtool request structure
164  *
165  * Since the CSRs for auto-negotiation using next pages are not fully
166  * standardised, this function does not attempt to decode them.  Use
167  * mdio45_ethtool_ksettings_get_npage() to specify advertisement bits
168  * from next pages.
169  */
170 static inline void
171 mdio45_ethtool_ksettings_get(const struct mdio_if_info *mdio,
172                              struct ethtool_link_ksettings *cmd)
173 {
174         mdio45_ethtool_ksettings_get_npage(mdio, cmd, 0, 0);
175 }
176
177 extern int mdio_mii_ioctl(const struct mdio_if_info *mdio,
178                           struct mii_ioctl_data *mii_data, int cmd);
179
180 /**
181  * mmd_eee_cap_to_ethtool_sup_t
182  * @eee_cap: value of the MMD EEE Capability register
183  *
184  * A small helper function that translates MMD EEE Capability (3.20) bits
185  * to ethtool supported settings.
186  */
187 static inline u32 mmd_eee_cap_to_ethtool_sup_t(u16 eee_cap)
188 {
189         u32 supported = 0;
190
191         if (eee_cap & MDIO_EEE_100TX)
192                 supported |= SUPPORTED_100baseT_Full;
193         if (eee_cap & MDIO_EEE_1000T)
194                 supported |= SUPPORTED_1000baseT_Full;
195         if (eee_cap & MDIO_EEE_10GT)
196                 supported |= SUPPORTED_10000baseT_Full;
197         if (eee_cap & MDIO_EEE_1000KX)
198                 supported |= SUPPORTED_1000baseKX_Full;
199         if (eee_cap & MDIO_EEE_10GKX4)
200                 supported |= SUPPORTED_10000baseKX4_Full;
201         if (eee_cap & MDIO_EEE_10GKR)
202                 supported |= SUPPORTED_10000baseKR_Full;
203
204         return supported;
205 }
206
207 /**
208  * mmd_eee_adv_to_ethtool_adv_t
209  * @eee_adv: value of the MMD EEE Advertisement/Link Partner Ability registers
210  *
211  * A small helper function that translates the MMD EEE Advertisment (7.60)
212  * and MMD EEE Link Partner Ability (7.61) bits to ethtool advertisement
213  * settings.
214  */
215 static inline u32 mmd_eee_adv_to_ethtool_adv_t(u16 eee_adv)
216 {
217         u32 adv = 0;
218
219         if (eee_adv & MDIO_EEE_100TX)
220                 adv |= ADVERTISED_100baseT_Full;
221         if (eee_adv & MDIO_EEE_1000T)
222                 adv |= ADVERTISED_1000baseT_Full;
223         if (eee_adv & MDIO_EEE_10GT)
224                 adv |= ADVERTISED_10000baseT_Full;
225         if (eee_adv & MDIO_EEE_1000KX)
226                 adv |= ADVERTISED_1000baseKX_Full;
227         if (eee_adv & MDIO_EEE_10GKX4)
228                 adv |= ADVERTISED_10000baseKX4_Full;
229         if (eee_adv & MDIO_EEE_10GKR)
230                 adv |= ADVERTISED_10000baseKR_Full;
231
232         return adv;
233 }
234
235 /**
236  * ethtool_adv_to_mmd_eee_adv_t
237  * @adv: the ethtool advertisement settings
238  *
239  * A small helper function that translates ethtool advertisement settings
240  * to EEE advertisements for the MMD EEE Advertisement (7.60) and
241  * MMD EEE Link Partner Ability (7.61) registers.
242  */
243 static inline u16 ethtool_adv_to_mmd_eee_adv_t(u32 adv)
244 {
245         u16 reg = 0;
246
247         if (adv & ADVERTISED_100baseT_Full)
248                 reg |= MDIO_EEE_100TX;
249         if (adv & ADVERTISED_1000baseT_Full)
250                 reg |= MDIO_EEE_1000T;
251         if (adv & ADVERTISED_10000baseT_Full)
252                 reg |= MDIO_EEE_10GT;
253         if (adv & ADVERTISED_1000baseKX_Full)
254                 reg |= MDIO_EEE_1000KX;
255         if (adv & ADVERTISED_10000baseKX4_Full)
256                 reg |= MDIO_EEE_10GKX4;
257         if (adv & ADVERTISED_10000baseKR_Full)
258                 reg |= MDIO_EEE_10GKR;
259
260         return reg;
261 }
262
263 int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum);
264 int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum);
265 int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val);
266 int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val);
267
268 int mdiobus_register_device(struct mdio_device *mdiodev);
269 int mdiobus_unregister_device(struct mdio_device *mdiodev);
270 bool mdiobus_is_registered_device(struct mii_bus *bus, int addr);
271 struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr);
272
273 /**
274  * mdio_module_driver() - Helper macro for registering mdio drivers
275  *
276  * Helper macro for MDIO drivers which do not do anything special in module
277  * init/exit. Each module may only use this macro once, and calling it
278  * replaces module_init() and module_exit().
279  */
280 #define mdio_module_driver(_mdio_driver)                                \
281 static int __init mdio_module_init(void)                                \
282 {                                                                       \
283         return mdio_driver_register(&_mdio_driver);                     \
284 }                                                                       \
285 module_init(mdio_module_init);                                          \
286 static void __exit mdio_module_exit(void)                               \
287 {                                                                       \
288         mdio_driver_unregister(&_mdio_driver);                          \
289 }                                                                       \
290 module_exit(mdio_module_exit)
291
292 #endif /* __LINUX_MDIO_H__ */