1 /* $NetBSD: miivar.h,v 1.74 2021/06/29 21:03:36 pgoyette Exp $ */ 2 3 /*- 4 * Copyright (c) 1998, 1999, 2000, 2001, 2020 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 33 #ifndef _DEV_MII_MIIVAR_H_ 34 #define _DEV_MII_MIIVAR_H_ 35 36 #include <sys/queue.h> 37 #include <sys/callout.h> 38 #include <sys/condvar.h> 39 40 #include <dev/mii/mii.h> 41 #include <dev/mii/mii_verbose.h> 42 43 /* 44 * Media Independent Interface data structure definitions. 45 */ 46 47 struct ifnet; 48 struct mii_softc; 49 50 /* 51 * Callbacks from MII layer into network interface device driver. 52 */ 53 typedef int (*mii_readreg_t)(device_t, int, int, uint16_t *); 54 typedef int (*mii_writereg_t)(device_t, int, int, uint16_t); 55 typedef void (*mii_statchg_t)(struct ifnet *); 56 57 /* 58 * A network interface driver has one of these structures in its softc. 59 * It is the interface from the network interface driver to the MII 60 * layer. 61 * 62 * LOCKING 63 * ======= 64 * 65 * The MII shares the lock with its ifmedia (which in turn shares the 66 * lock with its driver). 67 * 68 * MII routines can be called from a driver's interrupt handler, as well 69 * as via administrative input (e.g. ifmedia_ioctl()). We therefore require 70 * that most MII APIs are called with the ifmedia lock HELD. (See below.) 71 * 72 * Field markings and the corresponding locks: 73 * 74 * m: ifmedia lock 75 * :: unlocked, stable 76 */ 77 struct mii_data { 78 struct ifmedia mii_media; /* m: media information */ 79 struct ifnet *mii_ifp; /* :: pointer back to interface */ 80 81 kcondvar_t mii_probe_cv; /* m: serialize probing the MII */ 82 u_int mii_probe_waiters; /* m: # threads waiting to probe */ 83 int mii_flags; /* m: misc. flags; see below */ 84 85 /* 86 * m: For network interfaces with multiple PHYs, a list of all 87 * PHYs is required so they can all be notified when a media 88 * request is made. 89 */ 90 LIST_HEAD(mii_listhead, mii_softc) mii_phys; 91 u_int mii_instance; 92 93 /* m: PHY driver fills this in with active media status. */ 94 int mii_media_status; 95 u_int mii_media_active; 96 97 /* :: Calls from MII layer into network interface driver. */ 98 mii_readreg_t mii_readreg; 99 mii_writereg_t mii_writereg; 100 mii_statchg_t mii_statchg; 101 }; 102 typedef struct mii_data mii_data_t; 103 104 #define mii_lock(mii) ifmedia_lock(&(mii)->mii_media) 105 #define mii_unlock(mii) ifmedia_unlock(&(mii)->mii_media) 106 #define mii_locked(mii) ifmedia_locked(&(mii)->mii_media) 107 108 /* 109 * Functions provided by the PHY to perform various functions. 110 */ 111 struct mii_phy_funcs { 112 int (*pf_service)(struct mii_softc *, struct mii_data *, int); 113 void (*pf_status)(struct mii_softc *); 114 void (*pf_reset)(struct mii_softc *); 115 }; 116 117 /* 118 * Requests that can be made to the downcall. 119 */ 120 #define MII_TICK 1 /* once-per-second tick */ 121 #define MII_MEDIACHG 2 /* user changed media; perform the switch */ 122 #define MII_POLLSTAT 3 /* user requested media status; fill it in */ 123 #define MII_DOWN 4 /* interface is down */ 124 125 /* 126 * Each PHY driver's softc has one of these as the first member. 127 * XXX This would be better named "phy_softc", but this is the name 128 * XXX BSDI used, and we would like to have the same interface. 129 */ 130 struct mii_softc { 131 device_t mii_dev; /* generic device glue */ 132 133 LIST_ENTRY(mii_softc) mii_list; /* m: entry on parent's PHY list */ 134 135 uint32_t mii_mpd_oui; /* :: the PHY's OUI (MII_OUI())*/ 136 uint32_t mii_mpd_model; /* :: the PHY's model (MII_MODEL())*/ 137 uint32_t mii_mpd_rev; /* :: the PHY's revision (MII_REV())*/ 138 int mii_phy; /* :: our MII address */ 139 int mii_offset; /* :: first PHY, second PHY, etc. */ 140 u_int mii_inst; /* :: instance for ifmedia */ 141 142 /* :: Our PHY functions. */ 143 const struct mii_phy_funcs *mii_funcs; 144 145 struct mii_data *mii_pdata; /* :: pointer to parent's mii_data */ 146 147 int mii_flags; /* m: misc. flags; see below */ 148 uint16_t mii_capabilities; /* :: capabilities from BMSR */ 149 uint16_t mii_extcapabilities; /* :: extended caps from EXTSR */ 150 int mii_ticks; /* m: MII_TICK counter */ 151 int mii_anegticks; /* m: ticks before retrying aneg */ 152 153 struct callout mii_nway_ch; /* m: NWAY callout */ 154 155 u_int mii_media_active; /* m: last active media */ 156 int mii_media_status; /* m: last active status */ 157 }; 158 typedef struct mii_softc mii_softc_t; 159 160 /* Default mii_anegticks values. */ 161 #define MII_ANEGTICKS 5 162 #define MII_ANEGTICKS_GIGE 10 163 164 /* mii_flags */ 165 #define MIIF_INITDONE 0x0001 /* has been initialized (mii_data) */ 166 #define MIIF_NOISOLATE 0x0002 /* do not isolate the PHY */ 167 #define MIIF_NOLOOP 0x0004 /* no loopback capability */ 168 #define MIIF_DOINGAUTO 0x0008 /* doing autonegotiation (mii_softc) */ 169 #define MIIF_AUTOTSLEEP 0x0010 /* use kpause(), not callout() */ 170 #define MIIF_HAVEFIBER 0x0020 /* from parent: has fiber interface */ 171 #define MIIF_HAVE_GTCR 0x0040 /* has 100base-T2/1000base-T CR */ 172 #define MIIF_IS_1000X 0x0080 /* is a 1000BASE-X device */ 173 #define MIIF_DOPAUSE 0x0100 /* advertise PAUSE capability */ 174 #define MIIF_IS_HPNA 0x0200 /* is a HomePNA device */ 175 #define MIIF_FORCEANEG 0x0400 /* force auto-negotiation */ 176 #define MIIF_PROBING 0x0800 /* PHY probe in-progress */ 177 #define MIIF_EXITING 0x1000 /* MII is exiting */ 178 #define MIIF_RXID 0x2000 /* add RX delay */ 179 #define MIIF_TXID 0x4000 /* add TX delay */ 180 181 #define MIIF_INHERIT_MASK (MIIF_NOISOLATE | MIIF_NOLOOP | MIIF_AUTOTSLEEP) 182 183 /* 184 * Special `locators' passed to mii_attach(). If one of these is not 185 * an `any' value, we look for *that* PHY and configure it. If both 186 * are not `any', that is an error, and mii_attach() will panic. 187 */ 188 #define MII_OFFSET_ANY -1 189 #define MII_PHY_ANY -1 190 191 /* 192 * Used to attach a PHY to a parent. 193 */ 194 struct mii_attach_args { 195 struct mii_data *mii_data; /* pointer to parent data */ 196 int mii_phyno; /* MII address */ 197 uint16_t mii_id1; /* PHY ID register 1 */ 198 uint16_t mii_id2; /* PHY ID register 2 */ 199 uint16_t mii_capmask; /* capability mask from BMSR */ 200 int mii_flags; /* flags from parent */ 201 }; 202 typedef struct mii_attach_args mii_attach_args_t; 203 204 /* 205 * Used to match a PHY. 206 */ 207 struct mii_phydesc { 208 uint32_t mpd_oui; /* the PHY's OUI */ 209 uint32_t mpd_model; /* the PHY's model */ 210 const char *mpd_name; /* the PHY's name */ 211 }; 212 213 #define MII_PHY_DESC(a, b) { MII_OUI_ ## a, MII_MODEL_ ## a ## _ ## b, \ 214 MII_STR_ ## a ## _ ## b } 215 #define MII_PHY_END { 0, 0, NULL } 216 217 /* 218 * An array of these structures map MII media types to BMCR/ANAR settings. 219 */ 220 struct mii_media { 221 uint16_t mm_bmcr; /* BMCR settings for this media */ 222 uint16_t mm_anar; /* ANAR settings for this media */ 223 uint16_t mm_gtcr; /* 100base-T2 or 1000base-T CR */ 224 }; 225 226 #define MII_MEDIA_NONE 0 227 #define MII_MEDIA_10_T 1 228 #define MII_MEDIA_10_T_FDX 2 229 #define MII_MEDIA_100_T4 3 230 #define MII_MEDIA_100_TX 4 231 #define MII_MEDIA_100_TX_FDX 5 232 #define MII_MEDIA_1000_X 6 233 #define MII_MEDIA_1000_X_FDX 7 234 #define MII_MEDIA_1000_T 8 235 #define MII_MEDIA_1000_T_FDX 9 236 #define MII_NMEDIA 10 237 238 #ifdef _KERNEL 239 240 #define PHY_READ(p, r, v) \ 241 (*(p)->mii_pdata->mii_readreg)(device_parent((p)->mii_dev), \ 242 (p)->mii_phy, (r), (v)) 243 244 #define PHY_WRITE(p, r, v) \ 245 (*(p)->mii_pdata->mii_writereg)(device_parent((p)->mii_dev), \ 246 (p)->mii_phy, (r), (v)) 247 248 /* 249 * Setup MDD indirect access. Set device address and register number. 250 * "addr" variable takes an address ORed with the function (MMDACR_FN_*). 251 * 252 */ 253 static inline int 254 MMD_INDIRECT(struct mii_softc *sc, uint16_t daddr, uint16_t regnum) 255 { 256 int rv; 257 258 /* 259 * Set the MMD device address and set the access mode (function) 260 * to address. 261 */ 262 if ((rv = PHY_WRITE(sc, MII_MMDACR, (daddr & ~MMDACR_FUNCMASK))) != 0) 263 return rv; 264 265 /* Set the register number */ 266 if ((rv = PHY_WRITE(sc, MII_MMDAADR, regnum)) != 0) 267 return rv; 268 269 /* Set the access mode (function) */ 270 rv = PHY_WRITE(sc, MII_MMDACR, daddr); 271 272 return rv; 273 } 274 275 static inline int 276 MMD_INDIRECT_READ(struct mii_softc *sc, uint16_t daddr, uint16_t regnum, 277 uint16_t *valp) 278 { 279 int rv; 280 281 if ((rv = MMD_INDIRECT(sc, daddr, regnum)) != 0) 282 return rv; 283 284 return PHY_READ(sc, MII_MMDAADR, valp); 285 } 286 287 static inline int 288 MMD_INDIRECT_WRITE(struct mii_softc *sc, uint16_t daddr, uint16_t regnum, 289 uint16_t val) 290 { 291 int rv; 292 293 if ((rv = MMD_INDIRECT(sc, daddr, regnum)) != 0) 294 return rv; 295 296 return PHY_WRITE(sc, MII_MMDAADR, val); 297 } 298 299 /* MII must be LOCKED. */ 300 #define PHY_SERVICE(p, d, o) \ 301 (*(p)->mii_funcs->pf_service)((p), (d), (o)) 302 #define PHY_STATUS(p) \ 303 (*(p)->mii_funcs->pf_status)((p)) 304 #define PHY_RESET(p) \ 305 (*(p)->mii_funcs->pf_reset)((p)) 306 307 /* MII must be UNLOCKED */ 308 void mii_attach(device_t, struct mii_data *, int, int, int, int); 309 void mii_detach(struct mii_data *, int, int); 310 311 /* MII must be LOCKED. */ 312 int mii_mediachg(struct mii_data *); 313 void mii_tick(struct mii_data *); 314 void mii_pollstat(struct mii_data *); 315 void mii_down(struct mii_data *); 316 int mii_ifmedia_change(struct mii_data *); 317 318 uint16_t mii_anar(struct ifmedia_entry *); 319 320 /* MII must be UNLOCKED */ 321 int mii_phy_activate(device_t, enum devact); 322 int mii_phy_detach(device_t, int); 323 bool mii_phy_resume(device_t, const pmf_qual_t *); 324 325 const struct mii_phydesc *mii_phy_match(const struct mii_attach_args *, 326 const struct mii_phydesc *); 327 328 /* MII must be UNLOCKED */ 329 void mii_phy_add_media(struct mii_softc *); 330 void mii_phy_delete_media(struct mii_softc *); 331 332 /* MII must be LOCKED */ 333 void mii_phy_setmedia(struct mii_softc *); 334 int mii_phy_auto(struct mii_softc *); 335 int mii_phy_auto_restart(struct mii_softc *); 336 void mii_phy_reset(struct mii_softc *); 337 void mii_phy_down(struct mii_softc *); 338 int mii_phy_tick(struct mii_softc *); 339 340 /* MII must be LOCKED */ 341 void mii_phy_status(struct mii_softc *); 342 void mii_phy_update(struct mii_softc *, int); 343 344 /* MII must be LOCKED */ 345 u_int mii_phy_flowstatus(struct mii_softc *); 346 347 /* MII must be LOCKED */ 348 void ukphy_status(struct mii_softc *); 349 350 u_int mii_oui(uint16_t, uint16_t); 351 #define MII_OUI(id1, id2) mii_oui(id1, id2) 352 #define MII_MODEL(id2) (((id2) & IDR2_MODEL) >> 4) 353 #define MII_REV(id2) ((id2) & IDR2_REV) 354 355 /* Max length for phy's verbose oui+model */ 356 #define MII_MAX_DESCR_LEN 68 357 358 #endif /* _KERNEL */ 359 360 #endif /* _DEV_MII_MIIVAR_H_ */ 361