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