1 /* $NetBSD: miivar.h,v 1.8 1999/04/23 04:24:32 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 9 * NASA Ames Research Center. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 * 32 * $FreeBSD$ 33 */ 34 35 #ifndef _DEV_MII_MIIVAR_H_ 36 #define _DEV_MII_MIIVAR_H_ 37 38 #include <sys/queue.h> 39 #include <net/if_var.h> /* XXX driver API temporary */ 40 41 /* 42 * Media Independent Interface data structure defintions 43 */ 44 45 struct mii_softc; 46 47 /* 48 * Callbacks from MII layer into network interface device driver. 49 */ 50 typedef int (*mii_readreg_t)(struct device *, int, int); 51 typedef void (*mii_writereg_t)(struct device *, int, int, int); 52 typedef void (*mii_statchg_t)(struct device *); 53 54 /* 55 * A network interface driver has one of these structures in its softc. 56 * It is the interface from the network interface driver to the MII 57 * layer. 58 */ 59 struct mii_data { 60 struct ifmedia mii_media; /* media information */ 61 if_t mii_ifp; /* pointer back to network interface */ 62 63 /* 64 * For network interfaces with multiple PHYs, a list of all 65 * PHYs is required so they can all be notified when a media 66 * request is made. 67 */ 68 LIST_HEAD(mii_listhead, mii_softc) mii_phys; 69 u_int mii_instance; 70 71 /* 72 * PHY driver fills this in with active media status. 73 */ 74 u_int mii_media_status; 75 u_int mii_media_active; 76 77 /* 78 * Calls from MII layer into network interface driver. 79 */ 80 mii_readreg_t mii_readreg; 81 mii_writereg_t mii_writereg; 82 mii_statchg_t mii_statchg; 83 }; 84 typedef struct mii_data mii_data_t; 85 86 /* 87 * Functions provided by the PHY to perform various functions. 88 */ 89 struct mii_phy_funcs { 90 int (*pf_service)(struct mii_softc *, struct mii_data *, int); 91 void (*pf_status)(struct mii_softc *); 92 void (*pf_reset)(struct mii_softc *); 93 }; 94 95 /* 96 * Requests that can be made to the downcall. 97 */ 98 #define MII_TICK 1 /* once-per-second tick */ 99 #define MII_MEDIACHG 2 /* user changed media; perform the switch */ 100 #define MII_POLLSTAT 3 /* user requested media status; fill it in */ 101 102 /* 103 * Each PHY driver's softc has one of these as the first member. 104 * XXX This would be better named "phy_softc", but this is the name 105 * XXX BSDI used, and we would like to have the same interface. 106 */ 107 struct mii_softc { 108 device_t mii_dev; /* generic device glue */ 109 110 LIST_ENTRY(mii_softc) mii_list; /* entry on parent's PHY list */ 111 112 uint32_t mii_mpd_oui; /* the PHY's OUI (MII_OUI())*/ 113 uint32_t mii_mpd_model; /* the PHY's model (MII_MODEL())*/ 114 uint32_t mii_mpd_rev; /* the PHY's revision (MII_REV())*/ 115 u_int mii_capmask; /* capability mask for BMSR */ 116 u_int mii_phy; /* our MII address */ 117 u_int mii_offset; /* first PHY, second PHY, etc. */ 118 u_int mii_inst; /* instance for ifmedia */ 119 120 /* Our PHY functions. */ 121 const struct mii_phy_funcs *mii_funcs; 122 123 struct mii_data *mii_pdata; /* pointer to parent's mii_data */ 124 125 u_int mii_flags; /* misc. flags; see below */ 126 u_int mii_capabilities; /* capabilities from BMSR */ 127 u_int mii_extcapabilities; /* extended capabilities */ 128 u_int mii_ticks; /* MII_TICK counter */ 129 u_int mii_anegticks; /* ticks before retrying aneg */ 130 u_int mii_media_active; /* last active media */ 131 u_int mii_media_status; /* last active status */ 132 }; 133 typedef struct mii_softc mii_softc_t; 134 135 /* mii_flags */ 136 #define MIIF_INITDONE 0x00000001 /* has been initialized (mii_data) */ 137 #define MIIF_NOISOLATE 0x00000002 /* do not isolate the PHY */ 138 #if 0 139 #define MIIF_NOLOOP 0x00000004 /* no loopback capability */ 140 #endif 141 #define MIIF_DOINGAUTO 0x00000008 /* doing autonegotiation (mii_softc) */ 142 #define MIIF_AUTOTSLEEP 0x00000010 /* use tsleep(), not callout() */ 143 #define MIIF_HAVEFIBER 0x00000020 /* from parent: has fiber interface */ 144 #define MIIF_HAVE_GTCR 0x00000040 /* has 100base-T2/1000base-T CR */ 145 #define MIIF_IS_1000X 0x00000080 /* is a 1000BASE-X device */ 146 #define MIIF_DOPAUSE 0x00000100 /* advertise PAUSE capability */ 147 #define MIIF_IS_HPNA 0x00000200 /* is a HomePNA device */ 148 #define MIIF_FORCEANEG 0x00000400 /* force auto-negotiation */ 149 #define MIIF_NOMANPAUSE 0x00100000 /* no manual PAUSE selection */ 150 #define MIIF_FORCEPAUSE 0x00200000 /* force PAUSE advertisement */ 151 #define MIIF_MACPRIV0 0x01000000 /* private to the MAC driver */ 152 #define MIIF_MACPRIV1 0x02000000 /* private to the MAC driver */ 153 #define MIIF_MACPRIV2 0x04000000 /* private to the MAC driver */ 154 #define MIIF_PHYPRIV0 0x10000000 /* private to the PHY driver */ 155 #define MIIF_PHYPRIV1 0x20000000 /* private to the PHY driver */ 156 #define MIIF_PHYPRIV2 0x40000000 /* private to the PHY driver */ 157 158 /* Default mii_anegticks values */ 159 #define MII_ANEGTICKS 5 160 #define MII_ANEGTICKS_GIGE 17 161 162 #define MIIF_INHERIT_MASK (MIIF_NOISOLATE|MIIF_NOLOOP|MIIF_AUTOTSLEEP) 163 164 /* 165 * Special `locators' passed to mii_attach(). If one of these is not 166 * an `any' value, we look for *that* PHY and configure it. If both 167 * are not `any', that is an error, and mii_attach() will fail. 168 */ 169 #define MII_OFFSET_ANY -1 170 #define MII_PHY_ANY -1 171 172 /* 173 * Used to attach a PHY to a parent. 174 */ 175 struct mii_attach_args { 176 struct mii_data *mii_data; /* pointer to parent data */ 177 u_int mii_phyno; /* MII address */ 178 u_int mii_offset; /* first PHY, second PHY, etc. */ 179 uint32_t mii_id1; /* PHY ID register 1 */ 180 uint32_t mii_id2; /* PHY ID register 2 */ 181 u_int mii_capmask; /* capability mask for BMSR */ 182 }; 183 typedef struct mii_attach_args mii_attach_args_t; 184 185 /* 186 * Used to match a PHY. 187 */ 188 struct mii_phydesc { 189 uint32_t mpd_oui; /* the PHY's OUI */ 190 uint32_t mpd_model; /* the PHY's model */ 191 const char *mpd_name; /* the PHY's name */ 192 }; 193 #define MII_PHY_DESC(a, b) { MII_OUI_ ## a, MII_MODEL_ ## a ## _ ## b, \ 194 MII_STR_ ## a ## _ ## b } 195 #define MII_PHY_END { 0, 0, NULL } 196 197 /* 198 * An array of these structures map MII media types to BMCR/ANAR settings. 199 */ 200 struct mii_media { 201 u_int mm_bmcr; /* BMCR settings for this media */ 202 u_int mm_anar; /* ANAR settings for this media */ 203 u_int mm_gtcr; /* 100base-T2 or 1000base-T CR */ 204 }; 205 206 #define MII_MEDIA_NONE 0 207 #define MII_MEDIA_10_T 1 208 #define MII_MEDIA_10_T_FDX 2 209 #define MII_MEDIA_100_T4 3 210 #define MII_MEDIA_100_TX 4 211 #define MII_MEDIA_100_TX_FDX 5 212 #define MII_MEDIA_1000_X 6 213 #define MII_MEDIA_1000_X_FDX 7 214 #define MII_MEDIA_1000_T 8 215 #define MII_MEDIA_1000_T_FDX 9 216 #define MII_NMEDIA 10 217 218 #ifdef _KERNEL 219 220 #define PHY_READ(p, r) \ 221 MIIBUS_READREG((p)->mii_dev, (p)->mii_phy, (r)) 222 223 #define PHY_WRITE(p, r, v) \ 224 MIIBUS_WRITEREG((p)->mii_dev, (p)->mii_phy, (r), (v)) 225 226 #define PHY_SERVICE(p, d, o) \ 227 (*(p)->mii_funcs->pf_service)((p), (d), (o)) 228 229 #define PHY_STATUS(p) \ 230 (*(p)->mii_funcs->pf_status)(p) 231 232 #define PHY_RESET(p) \ 233 (*(p)->mii_funcs->pf_reset)(p) 234 235 enum miibus_device_ivars { 236 MIIBUS_IVAR_FLAGS 237 }; 238 239 /* 240 * Simplified accessors for miibus 241 */ 242 #define MIIBUS_ACCESSOR(var, ivar, type) \ 243 __BUS_ACCESSOR(miibus, var, MIIBUS, ivar, type) 244 245 MIIBUS_ACCESSOR(flags, FLAGS, u_int) 246 247 extern devclass_t miibus_devclass; 248 extern driver_t miibus_driver; 249 250 int mii_attach(device_t, device_t *, if_t, ifm_change_cb_t, 251 ifm_stat_cb_t, int, int, int, int); 252 void mii_down(struct mii_data *); 253 int mii_mediachg(struct mii_data *); 254 void mii_tick(struct mii_data *); 255 void mii_pollstat(struct mii_data *); 256 void mii_phy_add_media(struct mii_softc *); 257 258 int mii_phy_auto(struct mii_softc *); 259 int mii_phy_detach(device_t dev); 260 void mii_phy_down(struct mii_softc *); 261 u_int mii_phy_flowstatus(struct mii_softc *); 262 void mii_phy_reset(struct mii_softc *); 263 void mii_phy_setmedia(struct mii_softc *sc); 264 void mii_phy_update(struct mii_softc *, int); 265 int mii_phy_tick(struct mii_softc *); 266 267 const struct mii_phydesc * mii_phy_match(const struct mii_attach_args *ma, 268 const struct mii_phydesc *mpd); 269 const struct mii_phydesc * mii_phy_match_gen(const struct mii_attach_args *ma, 270 const struct mii_phydesc *mpd, size_t endlen); 271 int mii_phy_dev_probe(device_t dev, const struct mii_phydesc *mpd, int mrv); 272 void mii_phy_dev_attach(device_t dev, u_int flags, 273 const struct mii_phy_funcs *mpf, int add_media); 274 275 void ukphy_status(struct mii_softc *); 276 277 u_int mii_oui(u_int, u_int); 278 #define MII_OUI(id1, id2) mii_oui(id1, id2) 279 #define MII_MODEL(id2) (((id2) & IDR2_MODEL) >> 4) 280 #define MII_REV(id2) ((id2) & IDR2_REV) 281 282 #endif /* _KERNEL */ 283 284 #endif /* _DEV_MII_MIIVAR_H_ */ 285