]> git.karo-electronics.de Git - karo-tx-linux.git/blob - include/linux/phy.h
net/mlx5e: Add header re-write offloading of IPv6 hop-limit
[karo-tx-linux.git] / include / linux / phy.h
1 /*
2  * Framework and drivers for configuring and reading different PHYs
3  * Based on code in sungem_phy.c and gianfar_phy.c
4  *
5  * Author: Andy Fleming
6  *
7  * Copyright (c) 2004 Freescale Semiconductor, Inc.
8  *
9  * This program is free software; you can redistribute  it and/or modify it
10  * under  the terms of  the GNU General  Public License as published by the
11  * Free Software Foundation;  either version 2 of the  License, or (at your
12  * option) any later version.
13  *
14  */
15
16 #ifndef __PHY_H
17 #define __PHY_H
18
19 #include <linux/compiler.h>
20 #include <linux/spinlock.h>
21 #include <linux/ethtool.h>
22 #include <linux/mdio.h>
23 #include <linux/mii.h>
24 #include <linux/module.h>
25 #include <linux/timer.h>
26 #include <linux/workqueue.h>
27 #include <linux/mod_devicetable.h>
28
29 #include <linux/atomic.h>
30
31 #define PHY_DEFAULT_FEATURES    (SUPPORTED_Autoneg | \
32                                  SUPPORTED_TP | \
33                                  SUPPORTED_MII)
34
35 #define PHY_10BT_FEATURES       (SUPPORTED_10baseT_Half | \
36                                  SUPPORTED_10baseT_Full)
37
38 #define PHY_100BT_FEATURES      (SUPPORTED_100baseT_Half | \
39                                  SUPPORTED_100baseT_Full)
40
41 #define PHY_1000BT_FEATURES     (SUPPORTED_1000baseT_Half | \
42                                  SUPPORTED_1000baseT_Full)
43
44 #define PHY_BASIC_FEATURES      (PHY_10BT_FEATURES | \
45                                  PHY_100BT_FEATURES | \
46                                  PHY_DEFAULT_FEATURES)
47
48 #define PHY_GBIT_FEATURES       (PHY_BASIC_FEATURES | \
49                                  PHY_1000BT_FEATURES)
50
51
52 /*
53  * Set phydev->irq to PHY_POLL if interrupts are not supported,
54  * or not desired for this PHY.  Set to PHY_IGNORE_INTERRUPT if
55  * the attached driver handles the interrupt
56  */
57 #define PHY_POLL                -1
58 #define PHY_IGNORE_INTERRUPT    -2
59
60 #define PHY_HAS_INTERRUPT       0x00000001
61 #define PHY_IS_INTERNAL         0x00000002
62 #define MDIO_DEVICE_IS_PHY      0x80000000
63
64 /* Interface Mode definitions */
65 typedef enum {
66         PHY_INTERFACE_MODE_NA,
67         PHY_INTERFACE_MODE_MII,
68         PHY_INTERFACE_MODE_GMII,
69         PHY_INTERFACE_MODE_SGMII,
70         PHY_INTERFACE_MODE_TBI,
71         PHY_INTERFACE_MODE_REVMII,
72         PHY_INTERFACE_MODE_RMII,
73         PHY_INTERFACE_MODE_RGMII,
74         PHY_INTERFACE_MODE_RGMII_ID,
75         PHY_INTERFACE_MODE_RGMII_RXID,
76         PHY_INTERFACE_MODE_RGMII_TXID,
77         PHY_INTERFACE_MODE_RTBI,
78         PHY_INTERFACE_MODE_SMII,
79         PHY_INTERFACE_MODE_XGMII,
80         PHY_INTERFACE_MODE_MOCA,
81         PHY_INTERFACE_MODE_QSGMII,
82         PHY_INTERFACE_MODE_TRGMII,
83         PHY_INTERFACE_MODE_1000BASEX,
84         PHY_INTERFACE_MODE_2500BASEX,
85         PHY_INTERFACE_MODE_RXAUI,
86         PHY_INTERFACE_MODE_XAUI,
87         /* 10GBASE-KR, XFI, SFI - single lane 10G Serdes */
88         PHY_INTERFACE_MODE_10GKR,
89         PHY_INTERFACE_MODE_MAX,
90 } phy_interface_t;
91
92 /**
93  * phy_supported_speeds - return all speeds currently supported by a phy device
94  * @phy: The phy device to return supported speeds of.
95  * @speeds: buffer to store supported speeds in.
96  * @size: size of speeds buffer.
97  *
98  * Description: Returns the number of supported speeds, and
99  * fills the speeds * buffer with the supported speeds. If speeds buffer is
100  * too small to contain * all currently supported speeds, will return as
101  * many speeds as can fit.
102  */
103 unsigned int phy_supported_speeds(struct phy_device *phy,
104                                       unsigned int *speeds,
105                                       unsigned int size);
106
107 /**
108  * It maps 'enum phy_interface_t' found in include/linux/phy.h
109  * into the device tree binding of 'phy-mode', so that Ethernet
110  * device driver can get phy interface from device tree.
111  */
112 static inline const char *phy_modes(phy_interface_t interface)
113 {
114         switch (interface) {
115         case PHY_INTERFACE_MODE_NA:
116                 return "";
117         case PHY_INTERFACE_MODE_MII:
118                 return "mii";
119         case PHY_INTERFACE_MODE_GMII:
120                 return "gmii";
121         case PHY_INTERFACE_MODE_SGMII:
122                 return "sgmii";
123         case PHY_INTERFACE_MODE_TBI:
124                 return "tbi";
125         case PHY_INTERFACE_MODE_REVMII:
126                 return "rev-mii";
127         case PHY_INTERFACE_MODE_RMII:
128                 return "rmii";
129         case PHY_INTERFACE_MODE_RGMII:
130                 return "rgmii";
131         case PHY_INTERFACE_MODE_RGMII_ID:
132                 return "rgmii-id";
133         case PHY_INTERFACE_MODE_RGMII_RXID:
134                 return "rgmii-rxid";
135         case PHY_INTERFACE_MODE_RGMII_TXID:
136                 return "rgmii-txid";
137         case PHY_INTERFACE_MODE_RTBI:
138                 return "rtbi";
139         case PHY_INTERFACE_MODE_SMII:
140                 return "smii";
141         case PHY_INTERFACE_MODE_XGMII:
142                 return "xgmii";
143         case PHY_INTERFACE_MODE_MOCA:
144                 return "moca";
145         case PHY_INTERFACE_MODE_QSGMII:
146                 return "qsgmii";
147         case PHY_INTERFACE_MODE_TRGMII:
148                 return "trgmii";
149         case PHY_INTERFACE_MODE_1000BASEX:
150                 return "1000base-x";
151         case PHY_INTERFACE_MODE_2500BASEX:
152                 return "2500base-x";
153         case PHY_INTERFACE_MODE_RXAUI:
154                 return "rxaui";
155         case PHY_INTERFACE_MODE_XAUI:
156                 return "xaui";
157         case PHY_INTERFACE_MODE_10GKR:
158                 return "10gbase-kr";
159         default:
160                 return "unknown";
161         }
162 }
163
164
165 #define PHY_INIT_TIMEOUT        100000
166 #define PHY_STATE_TIME          1
167 #define PHY_FORCE_TIMEOUT       10
168 #define PHY_AN_TIMEOUT          10
169
170 #define PHY_MAX_ADDR    32
171
172 /* Used when trying to connect to a specific phy (mii bus id:phy device id) */
173 #define PHY_ID_FMT "%s:%02x"
174
175 #define MII_BUS_ID_SIZE 61
176
177 /* Or MII_ADDR_C45 into regnum for read/write on mii_bus to enable the 21 bit
178    IEEE 802.3ae clause 45 addressing mode used by 10GIGE phy chips. */
179 #define MII_ADDR_C45 (1<<30)
180
181 struct device;
182 struct sk_buff;
183
184 /*
185  * The Bus class for PHYs.  Devices which provide access to
186  * PHYs should register using this structure
187  */
188 struct mii_bus {
189         struct module *owner;
190         const char *name;
191         char id[MII_BUS_ID_SIZE];
192         void *priv;
193         int (*read)(struct mii_bus *bus, int addr, int regnum);
194         int (*write)(struct mii_bus *bus, int addr, int regnum, u16 val);
195         int (*reset)(struct mii_bus *bus);
196
197         /*
198          * A lock to ensure that only one thing can read/write
199          * the MDIO bus at a time
200          */
201         struct mutex mdio_lock;
202
203         struct device *parent;
204         enum {
205                 MDIOBUS_ALLOCATED = 1,
206                 MDIOBUS_REGISTERED,
207                 MDIOBUS_UNREGISTERED,
208                 MDIOBUS_RELEASED,
209         } state;
210         struct device dev;
211
212         /* list of all PHYs on bus */
213         struct mdio_device *mdio_map[PHY_MAX_ADDR];
214
215         /* PHY addresses to be ignored when probing */
216         u32 phy_mask;
217
218         /* PHY addresses to ignore the TA/read failure */
219         u32 phy_ignore_ta_mask;
220
221         /*
222          * An array of interrupts, each PHY's interrupt at the index
223          * matching its address
224          */
225         int irq[PHY_MAX_ADDR];
226
227         /* GPIO reset pulse width in microseconds */
228         int reset_delay_us;
229         /* RESET GPIO descriptor pointer */
230         struct gpio_desc *reset_gpiod;
231 };
232 #define to_mii_bus(d) container_of(d, struct mii_bus, dev)
233
234 struct mii_bus *mdiobus_alloc_size(size_t);
235 static inline struct mii_bus *mdiobus_alloc(void)
236 {
237         return mdiobus_alloc_size(0);
238 }
239
240 int __mdiobus_register(struct mii_bus *bus, struct module *owner);
241 #define mdiobus_register(bus) __mdiobus_register(bus, THIS_MODULE)
242 void mdiobus_unregister(struct mii_bus *bus);
243 void mdiobus_free(struct mii_bus *bus);
244 struct mii_bus *devm_mdiobus_alloc_size(struct device *dev, int sizeof_priv);
245 static inline struct mii_bus *devm_mdiobus_alloc(struct device *dev)
246 {
247         return devm_mdiobus_alloc_size(dev, 0);
248 }
249
250 void devm_mdiobus_free(struct device *dev, struct mii_bus *bus);
251 struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr);
252
253 #define PHY_INTERRUPT_DISABLED  0x0
254 #define PHY_INTERRUPT_ENABLED   0x80000000
255
256 /* PHY state machine states:
257  *
258  * DOWN: PHY device and driver are not ready for anything.  probe
259  * should be called if and only if the PHY is in this state,
260  * given that the PHY device exists.
261  * - PHY driver probe function will, depending on the PHY, set
262  * the state to STARTING or READY
263  *
264  * STARTING:  PHY device is coming up, and the ethernet driver is
265  * not ready.  PHY drivers may set this in the probe function.
266  * If they do, they are responsible for making sure the state is
267  * eventually set to indicate whether the PHY is UP or READY,
268  * depending on the state when the PHY is done starting up.
269  * - PHY driver will set the state to READY
270  * - start will set the state to PENDING
271  *
272  * READY: PHY is ready to send and receive packets, but the
273  * controller is not.  By default, PHYs which do not implement
274  * probe will be set to this state by phy_probe().  If the PHY
275  * driver knows the PHY is ready, and the PHY state is STARTING,
276  * then it sets this STATE.
277  * - start will set the state to UP
278  *
279  * PENDING: PHY device is coming up, but the ethernet driver is
280  * ready.  phy_start will set this state if the PHY state is
281  * STARTING.
282  * - PHY driver will set the state to UP when the PHY is ready
283  *
284  * UP: The PHY and attached device are ready to do work.
285  * Interrupts should be started here.
286  * - timer moves to AN
287  *
288  * AN: The PHY is currently negotiating the link state.  Link is
289  * therefore down for now.  phy_timer will set this state when it
290  * detects the state is UP.  config_aneg will set this state
291  * whenever called with phydev->autoneg set to AUTONEG_ENABLE.
292  * - If autonegotiation finishes, but there's no link, it sets
293  *   the state to NOLINK.
294  * - If aneg finishes with link, it sets the state to RUNNING,
295  *   and calls adjust_link
296  * - If autonegotiation did not finish after an arbitrary amount
297  *   of time, autonegotiation should be tried again if the PHY
298  *   supports "magic" autonegotiation (back to AN)
299  * - If it didn't finish, and no magic_aneg, move to FORCING.
300  *
301  * NOLINK: PHY is up, but not currently plugged in.
302  * - If the timer notes that the link comes back, we move to RUNNING
303  * - config_aneg moves to AN
304  * - phy_stop moves to HALTED
305  *
306  * FORCING: PHY is being configured with forced settings
307  * - if link is up, move to RUNNING
308  * - If link is down, we drop to the next highest setting, and
309  *   retry (FORCING) after a timeout
310  * - phy_stop moves to HALTED
311  *
312  * RUNNING: PHY is currently up, running, and possibly sending
313  * and/or receiving packets
314  * - timer will set CHANGELINK if we're polling (this ensures the
315  *   link state is polled every other cycle of this state machine,
316  *   which makes it every other second)
317  * - irq will set CHANGELINK
318  * - config_aneg will set AN
319  * - phy_stop moves to HALTED
320  *
321  * CHANGELINK: PHY experienced a change in link state
322  * - timer moves to RUNNING if link
323  * - timer moves to NOLINK if the link is down
324  * - phy_stop moves to HALTED
325  *
326  * HALTED: PHY is up, but no polling or interrupts are done. Or
327  * PHY is in an error state.
328  *
329  * - phy_start moves to RESUMING
330  *
331  * RESUMING: PHY was halted, but now wants to run again.
332  * - If we are forcing, or aneg is done, timer moves to RUNNING
333  * - If aneg is not done, timer moves to AN
334  * - phy_stop moves to HALTED
335  */
336 enum phy_state {
337         PHY_DOWN = 0,
338         PHY_STARTING,
339         PHY_READY,
340         PHY_PENDING,
341         PHY_UP,
342         PHY_AN,
343         PHY_RUNNING,
344         PHY_NOLINK,
345         PHY_FORCING,
346         PHY_CHANGELINK,
347         PHY_HALTED,
348         PHY_RESUMING
349 };
350
351 /**
352  * struct phy_c45_device_ids - 802.3-c45 Device Identifiers
353  * @devices_in_package: Bit vector of devices present.
354  * @device_ids: The device identifer for each present device.
355  */
356 struct phy_c45_device_ids {
357         u32 devices_in_package;
358         u32 device_ids[8];
359 };
360
361 /* phy_device: An instance of a PHY
362  *
363  * drv: Pointer to the driver for this PHY instance
364  * phy_id: UID for this device found during discovery
365  * c45_ids: 802.3-c45 Device Identifers if is_c45.
366  * is_c45:  Set to true if this phy uses clause 45 addressing.
367  * is_internal: Set to true if this phy is internal to a MAC.
368  * is_pseudo_fixed_link: Set to true if this phy is an Ethernet switch, etc.
369  * has_fixups: Set to true if this phy has fixups/quirks.
370  * suspended: Set to true if this phy has been suspended successfully.
371  * sysfs_links: Internal boolean tracking sysfs symbolic links setup/removal.
372  * state: state of the PHY for management purposes
373  * dev_flags: Device-specific flags used by the PHY driver.
374  * link_timeout: The number of timer firings to wait before the
375  * giving up on the current attempt at acquiring a link
376  * irq: IRQ number of the PHY's interrupt (-1 if none)
377  * phy_timer: The timer for handling the state machine
378  * phy_queue: A work_queue for the phy_mac_interrupt
379  * attached_dev: The attached enet driver's device instance ptr
380  * adjust_link: Callback for the enet controller to respond to
381  * changes in the link state.
382  *
383  * speed, duplex, pause, supported, advertising, lp_advertising,
384  * and autoneg are used like in mii_if_info
385  *
386  * interrupts currently only supports enabled or disabled,
387  * but could be changed in the future to support enabling
388  * and disabling specific interrupts
389  *
390  * Contains some infrastructure for polling and interrupt
391  * handling, as well as handling shifts in PHY hardware state
392  */
393 struct phy_device {
394         struct mdio_device mdio;
395
396         /* Information about the PHY type */
397         /* And management functions */
398         struct phy_driver *drv;
399
400         u32 phy_id;
401
402         struct phy_c45_device_ids c45_ids;
403         bool is_c45;
404         bool is_internal;
405         bool is_pseudo_fixed_link;
406         bool has_fixups;
407         bool suspended;
408         bool sysfs_links;
409
410         enum phy_state state;
411
412         u32 dev_flags;
413
414         phy_interface_t interface;
415
416         /*
417          * forced speed & duplex (no autoneg)
418          * partner speed & duplex & pause (autoneg)
419          */
420         int speed;
421         int duplex;
422         int pause;
423         int asym_pause;
424
425         /* The most recently read link state */
426         int link;
427
428         /* Enabled Interrupts */
429         u32 interrupts;
430
431         /* Union of PHY and Attached devices' supported modes */
432         /* See mii.h for more info */
433         u32 supported;
434         u32 advertising;
435         u32 lp_advertising;
436
437         /* Energy efficient ethernet modes which should be prohibited */
438         u32 eee_broken_modes;
439
440         int autoneg;
441
442         int link_timeout;
443
444 #ifdef CONFIG_LED_TRIGGER_PHY
445         struct phy_led_trigger *phy_led_triggers;
446         unsigned int phy_num_led_triggers;
447         struct phy_led_trigger *last_triggered;
448 #endif
449
450         /*
451          * Interrupt number for this PHY
452          * -1 means no interrupt
453          */
454         int irq;
455
456         /* private data pointer */
457         /* For use by PHYs to maintain extra state */
458         void *priv;
459
460         /* Interrupt and Polling infrastructure */
461         struct work_struct phy_queue;
462         struct delayed_work state_queue;
463         atomic_t irq_disable;
464
465         struct mutex lock;
466
467         struct net_device *attached_dev;
468
469         u8 mdix;
470         u8 mdix_ctrl;
471
472         void (*adjust_link)(struct net_device *dev);
473 };
474 #define to_phy_device(d) container_of(to_mdio_device(d), \
475                                       struct phy_device, mdio)
476
477 /* struct phy_driver: Driver structure for a particular PHY type
478  *
479  * driver_data: static driver data
480  * phy_id: The result of reading the UID registers of this PHY
481  *   type, and ANDing them with the phy_id_mask.  This driver
482  *   only works for PHYs with IDs which match this field
483  * name: The friendly name of this PHY type
484  * phy_id_mask: Defines the important bits of the phy_id
485  * features: A list of features (speed, duplex, etc) supported
486  *   by this PHY
487  * flags: A bitfield defining certain other features this PHY
488  *   supports (like interrupts)
489  *
490  * The drivers must implement config_aneg and read_status.  All
491  * other functions are optional. Note that none of these
492  * functions should be called from interrupt time.  The goal is
493  * for the bus read/write functions to be able to block when the
494  * bus transaction is happening, and be freed up by an interrupt
495  * (The MPC85xx has this ability, though it is not currently
496  * supported in the driver).
497  */
498 struct phy_driver {
499         struct mdio_driver_common mdiodrv;
500         u32 phy_id;
501         char *name;
502         unsigned int phy_id_mask;
503         u32 features;
504         u32 flags;
505         const void *driver_data;
506
507         /*
508          * Called to issue a PHY software reset
509          */
510         int (*soft_reset)(struct phy_device *phydev);
511
512         /*
513          * Called to initialize the PHY,
514          * including after a reset
515          */
516         int (*config_init)(struct phy_device *phydev);
517
518         /*
519          * Called during discovery.  Used to set
520          * up device-specific structures, if any
521          */
522         int (*probe)(struct phy_device *phydev);
523
524         /* PHY Power Management */
525         int (*suspend)(struct phy_device *phydev);
526         int (*resume)(struct phy_device *phydev);
527
528         /*
529          * Configures the advertisement and resets
530          * autonegotiation if phydev->autoneg is on,
531          * forces the speed to the current settings in phydev
532          * if phydev->autoneg is off
533          */
534         int (*config_aneg)(struct phy_device *phydev);
535
536         /* Determines the auto negotiation result */
537         int (*aneg_done)(struct phy_device *phydev);
538
539         /* Determines the negotiated speed and duplex */
540         int (*read_status)(struct phy_device *phydev);
541
542         /* Clears any pending interrupts */
543         int (*ack_interrupt)(struct phy_device *phydev);
544
545         /* Enables or disables interrupts */
546         int (*config_intr)(struct phy_device *phydev);
547
548         /*
549          * Checks if the PHY generated an interrupt.
550          * For multi-PHY devices with shared PHY interrupt pin
551          */
552         int (*did_interrupt)(struct phy_device *phydev);
553
554         /* Clears up any memory if needed */
555         void (*remove)(struct phy_device *phydev);
556
557         /* Returns true if this is a suitable driver for the given
558          * phydev.  If NULL, matching is based on phy_id and
559          * phy_id_mask.
560          */
561         int (*match_phy_device)(struct phy_device *phydev);
562
563         /* Handles ethtool queries for hardware time stamping. */
564         int (*ts_info)(struct phy_device *phydev, struct ethtool_ts_info *ti);
565
566         /* Handles SIOCSHWTSTAMP ioctl for hardware time stamping. */
567         int  (*hwtstamp)(struct phy_device *phydev, struct ifreq *ifr);
568
569         /*
570          * Requests a Rx timestamp for 'skb'. If the skb is accepted,
571          * the phy driver promises to deliver it using netif_rx() as
572          * soon as a timestamp becomes available. One of the
573          * PTP_CLASS_ values is passed in 'type'. The function must
574          * return true if the skb is accepted for delivery.
575          */
576         bool (*rxtstamp)(struct phy_device *dev, struct sk_buff *skb, int type);
577
578         /*
579          * Requests a Tx timestamp for 'skb'. The phy driver promises
580          * to deliver it using skb_complete_tx_timestamp() as soon as a
581          * timestamp becomes available. One of the PTP_CLASS_ values
582          * is passed in 'type'.
583          */
584         void (*txtstamp)(struct phy_device *dev, struct sk_buff *skb, int type);
585
586         /* Some devices (e.g. qnap TS-119P II) require PHY register changes to
587          * enable Wake on LAN, so set_wol is provided to be called in the
588          * ethernet driver's set_wol function. */
589         int (*set_wol)(struct phy_device *dev, struct ethtool_wolinfo *wol);
590
591         /* See set_wol, but for checking whether Wake on LAN is enabled. */
592         void (*get_wol)(struct phy_device *dev, struct ethtool_wolinfo *wol);
593
594         /*
595          * Called to inform a PHY device driver when the core is about to
596          * change the link state. This callback is supposed to be used as
597          * fixup hook for drivers that need to take action when the link
598          * state changes. Drivers are by no means allowed to mess with the
599          * PHY device structure in their implementations.
600          */
601         void (*link_change_notify)(struct phy_device *dev);
602
603         /*
604          * Phy specific driver override for reading a MMD register.
605          * This function is optional for PHY specific drivers.  When
606          * not provided, the default MMD read function will be used
607          * by phy_read_mmd(), which will use either a direct read for
608          * Clause 45 PHYs or an indirect read for Clause 22 PHYs.
609          *  devnum is the MMD device number within the PHY device,
610          *  regnum is the register within the selected MMD device.
611          */
612         int (*read_mmd)(struct phy_device *dev, int devnum, u16 regnum);
613
614         /*
615          * Phy specific driver override for writing a MMD register.
616          * This function is optional for PHY specific drivers.  When
617          * not provided, the default MMD write function will be used
618          * by phy_write_mmd(), which will use either a direct write for
619          * Clause 45 PHYs, or an indirect write for Clause 22 PHYs.
620          *  devnum is the MMD device number within the PHY device,
621          *  regnum is the register within the selected MMD device.
622          *  val is the value to be written.
623          */
624         int (*write_mmd)(struct phy_device *dev, int devnum, u16 regnum,
625                          u16 val);
626
627         /* Get the size and type of the eeprom contained within a plug-in
628          * module */
629         int (*module_info)(struct phy_device *dev,
630                            struct ethtool_modinfo *modinfo);
631
632         /* Get the eeprom information from the plug-in module */
633         int (*module_eeprom)(struct phy_device *dev,
634                              struct ethtool_eeprom *ee, u8 *data);
635
636         /* Get statistics from the phy using ethtool */
637         int (*get_sset_count)(struct phy_device *dev);
638         void (*get_strings)(struct phy_device *dev, u8 *data);
639         void (*get_stats)(struct phy_device *dev,
640                           struct ethtool_stats *stats, u64 *data);
641
642         /* Get and Set PHY tunables */
643         int (*get_tunable)(struct phy_device *dev,
644                            struct ethtool_tunable *tuna, void *data);
645         int (*set_tunable)(struct phy_device *dev,
646                             struct ethtool_tunable *tuna,
647                             const void *data);
648 };
649 #define to_phy_driver(d) container_of(to_mdio_common_driver(d),         \
650                                       struct phy_driver, mdiodrv)
651
652 #define PHY_ANY_ID "MATCH ANY PHY"
653 #define PHY_ANY_UID 0xffffffff
654
655 /* A Structure for boards to register fixups with the PHY Lib */
656 struct phy_fixup {
657         struct list_head list;
658         char bus_id[MII_BUS_ID_SIZE + 3];
659         u32 phy_uid;
660         u32 phy_uid_mask;
661         int (*run)(struct phy_device *phydev);
662 };
663
664 /**
665  * phy_read_mmd - Convenience function for reading a register
666  * from an MMD on a given PHY.
667  * @phydev: The phy_device struct
668  * @devad: The MMD to read from
669  * @regnum: The register on the MMD to read
670  *
671  * Same rules as for phy_read();
672  */
673 int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum);
674
675 /**
676  * phy_read - Convenience function for reading a given PHY register
677  * @phydev: the phy_device struct
678  * @regnum: register number to read
679  *
680  * NOTE: MUST NOT be called from interrupt context,
681  * because the bus read/write functions may wait for an interrupt
682  * to conclude the operation.
683  */
684 static inline int phy_read(struct phy_device *phydev, u32 regnum)
685 {
686         return mdiobus_read(phydev->mdio.bus, phydev->mdio.addr, regnum);
687 }
688
689 /**
690  * phy_write - Convenience function for writing a given PHY register
691  * @phydev: the phy_device struct
692  * @regnum: register number to write
693  * @val: value to write to @regnum
694  *
695  * NOTE: MUST NOT be called from interrupt context,
696  * because the bus read/write functions may wait for an interrupt
697  * to conclude the operation.
698  */
699 static inline int phy_write(struct phy_device *phydev, u32 regnum, u16 val)
700 {
701         return mdiobus_write(phydev->mdio.bus, phydev->mdio.addr, regnum, val);
702 }
703
704 /**
705  * phy_interrupt_is_valid - Convenience function for testing a given PHY irq
706  * @phydev: the phy_device struct
707  *
708  * NOTE: must be kept in sync with addition/removal of PHY_POLL and
709  * PHY_IGNORE_INTERRUPT
710  */
711 static inline bool phy_interrupt_is_valid(struct phy_device *phydev)
712 {
713         return phydev->irq != PHY_POLL && phydev->irq != PHY_IGNORE_INTERRUPT;
714 }
715
716 /**
717  * phy_is_internal - Convenience function for testing if a PHY is internal
718  * @phydev: the phy_device struct
719  */
720 static inline bool phy_is_internal(struct phy_device *phydev)
721 {
722         return phydev->is_internal;
723 }
724
725 /**
726  * phy_interface_mode_is_rgmii - Convenience function for testing if a
727  * PHY interface mode is RGMII (all variants)
728  * @mode: the phy_interface_t enum
729  */
730 static inline bool phy_interface_mode_is_rgmii(phy_interface_t mode)
731 {
732         return mode >= PHY_INTERFACE_MODE_RGMII &&
733                 mode <= PHY_INTERFACE_MODE_RGMII_TXID;
734 };
735
736 /**
737  * phy_interface_is_rgmii - Convenience function for testing if a PHY interface
738  * is RGMII (all variants)
739  * @phydev: the phy_device struct
740  */
741 static inline bool phy_interface_is_rgmii(struct phy_device *phydev)
742 {
743         return phy_interface_mode_is_rgmii(phydev->interface);
744 };
745
746 /*
747  * phy_is_pseudo_fixed_link - Convenience function for testing if this
748  * PHY is the CPU port facing side of an Ethernet switch, or similar.
749  * @phydev: the phy_device struct
750  */
751 static inline bool phy_is_pseudo_fixed_link(struct phy_device *phydev)
752 {
753         return phydev->is_pseudo_fixed_link;
754 }
755
756 /**
757  * phy_write_mmd - Convenience function for writing a register
758  * on an MMD on a given PHY.
759  * @phydev: The phy_device struct
760  * @devad: The MMD to read from
761  * @regnum: The register on the MMD to read
762  * @val: value to write to @regnum
763  *
764  * Same rules as for phy_write();
765  */
766 int phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val);
767
768 struct phy_device *phy_device_create(struct mii_bus *bus, int addr, int phy_id,
769                                      bool is_c45,
770                                      struct phy_c45_device_ids *c45_ids);
771 #if IS_ENABLED(CONFIG_PHYLIB)
772 struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45);
773 int phy_device_register(struct phy_device *phy);
774 void phy_device_free(struct phy_device *phydev);
775 #else
776 static inline
777 struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45)
778 {
779         return NULL;
780 }
781
782 static inline int phy_device_register(struct phy_device *phy)
783 {
784         return 0;
785 }
786
787 static inline void phy_device_free(struct phy_device *phydev) { }
788 #endif /* CONFIG_PHYLIB */
789 void phy_device_remove(struct phy_device *phydev);
790 int phy_init_hw(struct phy_device *phydev);
791 int phy_suspend(struct phy_device *phydev);
792 int phy_resume(struct phy_device *phydev);
793 struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
794                               phy_interface_t interface);
795 struct phy_device *phy_find_first(struct mii_bus *bus);
796 int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
797                       u32 flags, phy_interface_t interface);
798 int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
799                        void (*handler)(struct net_device *),
800                        phy_interface_t interface);
801 struct phy_device *phy_connect(struct net_device *dev, const char *bus_id,
802                                void (*handler)(struct net_device *),
803                                phy_interface_t interface);
804 void phy_disconnect(struct phy_device *phydev);
805 void phy_detach(struct phy_device *phydev);
806 void phy_start(struct phy_device *phydev);
807 void phy_stop(struct phy_device *phydev);
808 int phy_start_aneg(struct phy_device *phydev);
809 int phy_aneg_done(struct phy_device *phydev);
810
811 int phy_stop_interrupts(struct phy_device *phydev);
812 int phy_restart_aneg(struct phy_device *phydev);
813
814 static inline int phy_read_status(struct phy_device *phydev)
815 {
816         if (!phydev->drv)
817                 return -EIO;
818
819         return phydev->drv->read_status(phydev);
820 }
821
822 #define phydev_err(_phydev, format, args...)    \
823         dev_err(&_phydev->mdio.dev, format, ##args)
824
825 #define phydev_dbg(_phydev, format, args...)    \
826         dev_dbg(&_phydev->mdio.dev, format, ##args);
827
828 static inline const char *phydev_name(const struct phy_device *phydev)
829 {
830         return dev_name(&phydev->mdio.dev);
831 }
832
833 void phy_attached_print(struct phy_device *phydev, const char *fmt, ...)
834         __printf(2, 3);
835 void phy_attached_info(struct phy_device *phydev);
836
837 /* Clause 22 PHY */
838 int genphy_config_init(struct phy_device *phydev);
839 int genphy_setup_forced(struct phy_device *phydev);
840 int genphy_restart_aneg(struct phy_device *phydev);
841 int genphy_config_aneg(struct phy_device *phydev);
842 int genphy_aneg_done(struct phy_device *phydev);
843 int genphy_update_link(struct phy_device *phydev);
844 int genphy_read_status(struct phy_device *phydev);
845 int genphy_suspend(struct phy_device *phydev);
846 int genphy_resume(struct phy_device *phydev);
847 int genphy_soft_reset(struct phy_device *phydev);
848 static inline int genphy_no_soft_reset(struct phy_device *phydev)
849 {
850         return 0;
851 }
852
853 /* Clause 45 PHY */
854 int genphy_c45_restart_aneg(struct phy_device *phydev);
855 int genphy_c45_aneg_done(struct phy_device *phydev);
856 int genphy_c45_read_link(struct phy_device *phydev, u32 mmd_mask);
857 int genphy_c45_read_lpa(struct phy_device *phydev);
858 int genphy_c45_read_pma(struct phy_device *phydev);
859 int genphy_c45_pma_setup_forced(struct phy_device *phydev);
860 int genphy_c45_an_disable_aneg(struct phy_device *phydev);
861
862 void phy_driver_unregister(struct phy_driver *drv);
863 void phy_drivers_unregister(struct phy_driver *drv, int n);
864 int phy_driver_register(struct phy_driver *new_driver, struct module *owner);
865 int phy_drivers_register(struct phy_driver *new_driver, int n,
866                          struct module *owner);
867 void phy_state_machine(struct work_struct *work);
868 void phy_change(struct phy_device *phydev);
869 void phy_change_work(struct work_struct *work);
870 void phy_mac_interrupt(struct phy_device *phydev, int new_link);
871 void phy_start_machine(struct phy_device *phydev);
872 void phy_stop_machine(struct phy_device *phydev);
873 void phy_trigger_machine(struct phy_device *phydev, bool sync);
874 int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd);
875 void phy_ethtool_ksettings_get(struct phy_device *phydev,
876                                struct ethtool_link_ksettings *cmd);
877 int phy_ethtool_ksettings_set(struct phy_device *phydev,
878                               const struct ethtool_link_ksettings *cmd);
879 int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd);
880 int phy_start_interrupts(struct phy_device *phydev);
881 void phy_print_status(struct phy_device *phydev);
882 int phy_set_max_speed(struct phy_device *phydev, u32 max_speed);
883
884 int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
885                        int (*run)(struct phy_device *));
886 int phy_register_fixup_for_id(const char *bus_id,
887                               int (*run)(struct phy_device *));
888 int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
889                                int (*run)(struct phy_device *));
890
891 int phy_unregister_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask);
892 int phy_unregister_fixup_for_id(const char *bus_id);
893 int phy_unregister_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask);
894
895 int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable);
896 int phy_get_eee_err(struct phy_device *phydev);
897 int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data);
898 int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data);
899 int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol);
900 void phy_ethtool_get_wol(struct phy_device *phydev,
901                          struct ethtool_wolinfo *wol);
902 int phy_ethtool_get_link_ksettings(struct net_device *ndev,
903                                    struct ethtool_link_ksettings *cmd);
904 int phy_ethtool_set_link_ksettings(struct net_device *ndev,
905                                    const struct ethtool_link_ksettings *cmd);
906 int phy_ethtool_nway_reset(struct net_device *ndev);
907
908 #if IS_ENABLED(CONFIG_PHYLIB)
909 int __init mdio_bus_init(void);
910 void mdio_bus_exit(void);
911 #endif
912
913 extern struct bus_type mdio_bus_type;
914
915 struct mdio_board_info {
916         const char      *bus_id;
917         char            modalias[MDIO_NAME_SIZE];
918         int             mdio_addr;
919         const void      *platform_data;
920 };
921
922 #if IS_ENABLED(CONFIG_MDIO_DEVICE)
923 int mdiobus_register_board_info(const struct mdio_board_info *info,
924                                 unsigned int n);
925 #else
926 static inline int mdiobus_register_board_info(const struct mdio_board_info *i,
927                                               unsigned int n)
928 {
929         return 0;
930 }
931 #endif
932
933
934 /**
935  * module_phy_driver() - Helper macro for registering PHY drivers
936  * @__phy_drivers: array of PHY drivers to register
937  *
938  * Helper macro for PHY drivers which do not do anything special in module
939  * init/exit. Each module may only use this macro once, and calling it
940  * replaces module_init() and module_exit().
941  */
942 #define phy_module_driver(__phy_drivers, __count)                       \
943 static int __init phy_module_init(void)                                 \
944 {                                                                       \
945         return phy_drivers_register(__phy_drivers, __count, THIS_MODULE); \
946 }                                                                       \
947 module_init(phy_module_init);                                           \
948 static void __exit phy_module_exit(void)                                \
949 {                                                                       \
950         phy_drivers_unregister(__phy_drivers, __count);                 \
951 }                                                                       \
952 module_exit(phy_module_exit)
953
954 #define module_phy_driver(__phy_drivers)                                \
955         phy_module_driver(__phy_drivers, ARRAY_SIZE(__phy_drivers))
956
957 #endif /* __PHY_H */