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
45struct mii_softc;
46
47/*
48 * A network interface driver has one of these structures in its softc.
49 * It is the interface from the network interface driver to the MII
50 * layer.
51 */
52struct mii_data {
53	struct ifmedia mii_media;	/* media information */
54	if_t mii_ifp;		/* pointer back to network interface */
55
56	/*
57	 * For network interfaces with multiple PHYs, a list of all
58	 * PHYs is required so they can all be notified when a media
59	 * request is made.
60	 */
61	LIST_HEAD(mii_listhead, mii_softc) mii_phys;
62	u_int mii_instance;
63
64	/*
65	 * PHY driver fills this in with active media status.
66	 */
67	u_int mii_media_status;
68	u_int mii_media_active;
69};
70typedef struct mii_data mii_data_t;
71
72/*
73 * Functions provided by the PHY to perform various functions.
74 */
75struct mii_phy_funcs {
76	int (*pf_service)(struct mii_softc *, struct mii_data *, int);
77	void (*pf_status)(struct mii_softc *);
78	void (*pf_reset)(struct mii_softc *);
79};
80
81/*
82 * Requests that can be made to the downcall.
83 */
84#define	MII_TICK	1	/* once-per-second tick */
85#define	MII_MEDIACHG	2	/* user changed media; perform the switch */
86#define	MII_POLLSTAT	3	/* user requested media status; fill it in */
87
88/*
89 * Each PHY driver's softc has one of these as the first member.
90 * XXX This would be better named "phy_softc", but this is the name
91 * XXX BSDI used, and we would like to have the same interface.
92 */
93struct mii_softc {
94	device_t mii_dev;		/* generic device glue */
95
96	LIST_ENTRY(mii_softc) mii_list;	/* entry on parent's PHY list */
97
98	uint32_t mii_mpd_oui;		/* the PHY's OUI (MII_OUI())*/
99	uint32_t mii_mpd_model;		/* the PHY's model (MII_MODEL())*/
100	uint32_t mii_mpd_rev;		/* the PHY's revision (MII_REV())*/
101	u_int mii_capmask;		/* capability mask for BMSR */
102	u_int mii_phy;			/* our MII address */
103	u_int mii_offset;		/* first PHY, second PHY, etc. */
104	u_int mii_inst;			/* instance for ifmedia */
105
106	/* Our PHY functions. */
107	const struct mii_phy_funcs *mii_funcs;
108
109	struct mii_data *mii_pdata;	/* pointer to parent's mii_data */
110
111	u_int mii_flags;		/* misc. flags; see below */
112	u_int mii_capabilities;		/* capabilities from BMSR */
113	u_int mii_extcapabilities;	/* extended capabilities */
114	u_int mii_ticks;		/* MII_TICK counter */
115	u_int mii_anegticks;		/* ticks before retrying aneg */
116	u_int mii_media_active;		/* last active media */
117	u_int mii_media_status;		/* last active status */
118};
119typedef struct mii_softc mii_softc_t;
120
121/* mii_flags */
122#define	MIIF_INITDONE	0x00000001	/* has been initialized (mii_data) */
123#define	MIIF_NOISOLATE	0x00000002	/* do not isolate the PHY */
124#if 0
125#define	MIIF_NOLOOP	0x00000004	/* no loopback capability */
126#endif
127#define	MIIF_DOINGAUTO	0x00000008	/* doing autonegotiation (mii_softc) */
128#define	MIIF_AUTOTSLEEP	0x00000010	/* use tsleep(), not callout() */
129#define	MIIF_HAVEFIBER	0x00000020	/* from parent: has fiber interface */
130#define	MIIF_HAVE_GTCR	0x00000040	/* has 100base-T2/1000base-T CR */
131#define	MIIF_IS_1000X	0x00000080	/* is a 1000BASE-X device */
132#define	MIIF_DOPAUSE	0x00000100	/* advertise PAUSE capability */
133#define	MIIF_IS_HPNA	0x00000200	/* is a HomePNA device */
134#define	MIIF_FORCEANEG	0x00000400	/* force auto-negotiation */
135#define	MIIF_NOMANPAUSE	0x00100000	/* no manual PAUSE selection */
136#define	MIIF_FORCEPAUSE	0x00200000	/* force PAUSE advertisement */
137#define	MIIF_MACPRIV0	0x01000000	/* private to the MAC driver */
138#define	MIIF_MACPRIV1	0x02000000	/* private to the MAC driver */
139#define	MIIF_MACPRIV2	0x04000000	/* private to the MAC driver */
140#define	MIIF_PHYPRIV0	0x10000000	/* private to the PHY driver */
141#define	MIIF_PHYPRIV1	0x20000000	/* private to the PHY driver */
142#define	MIIF_PHYPRIV2	0x40000000	/* private to the PHY driver */
143
144/* Default mii_anegticks values */
145#define	MII_ANEGTICKS		5
146#define	MII_ANEGTICKS_GIGE	17
147
148#define	MIIF_INHERIT_MASK	(MIIF_NOISOLATE|MIIF_NOLOOP|MIIF_AUTOTSLEEP)
149
150/*
151 * Special `locators' passed to mii_attach().  If one of these is not
152 * an `any' value, we look for *that* PHY and configure it.  If both
153 * are not `any', that is an error, and mii_attach() will fail.
154 */
155#define	MII_OFFSET_ANY		-1
156#define	MII_PHY_ANY		-1
157
158/*
159 * Used to attach a PHY to a parent.
160 */
161struct mii_attach_args {
162	struct mii_data *mii_data;	/* pointer to parent data */
163	u_int mii_phyno;		/* MII address */
164	u_int mii_offset;		/* first PHY, second PHY, etc. */
165	uint32_t mii_id1;		/* PHY ID register 1 */
166	uint32_t mii_id2;		/* PHY ID register 2 */
167	u_int mii_capmask;		/* capability mask for BMSR */
168};
169typedef struct mii_attach_args mii_attach_args_t;
170
171/*
172 * Used to match a PHY.
173 */
174struct mii_phydesc {
175	uint32_t mpd_oui;		/* the PHY's OUI */
176	uint32_t mpd_model;		/* the PHY's model */
177	const char *mpd_name;		/* the PHY's name */
178};
179#define MII_PHY_DESC(a, b) { MII_OUI_ ## a, MII_MODEL_ ## a ## _ ## b, \
180	MII_STR_ ## a ## _ ## b }
181#define MII_PHY_END	{ 0, 0, NULL }
182
183#ifdef _KERNEL
184
185#ifdef __HAIKU__
186int __haiku_miibus_readreg(device_t dev, int phy, int reg);
187int __haiku_miibus_writereg(device_t dev, int phy, int reg, int data);
188void __haiku_miibus_statchg(device_t dev);
189void __haiku_miibus_linkchg(device_t dev);
190void __haiku_miibus_mediainit(device_t dev);
191
192#define MIIBUS_READREG(dev, phy, reg) \
193	__haiku_miibus_readreg((dev), (phy), (reg))
194
195#define MIIBUS_WRITEREG(dev, phy, reg, value) \
196	__haiku_miibus_writereg((dev), (phy), (reg), (value))
197
198#define MIIBUS_STATCHG(dev) \
199	__haiku_miibus_statchg(dev)
200
201#define MIIBUS_LINKCHG(dev) \
202	__haiku_miibus_linkchg(dev)
203
204#define MIIBUS_MEDIAINIT(dev) \
205	__haiku_miibus_mediainit(dev)
206
207#endif
208
209#define PHY_READ(p, r) \
210	MIIBUS_READREG((p)->mii_dev, (p)->mii_phy, (r))
211
212#define PHY_WRITE(p, r, v) \
213	MIIBUS_WRITEREG((p)->mii_dev, (p)->mii_phy, (r), (v))
214
215#define	PHY_SERVICE(p, d, o) \
216	(*(p)->mii_funcs->pf_service)((p), (d), (o))
217
218#define	PHY_STATUS(p) \
219	(*(p)->mii_funcs->pf_status)(p)
220
221#define	PHY_RESET(p) \
222	(*(p)->mii_funcs->pf_reset)(p)
223
224enum miibus_device_ivars {
225	MIIBUS_IVAR_FLAGS
226};
227
228/*
229 * Simplified accessors for miibus
230 */
231#define	MIIBUS_ACCESSOR(var, ivar, type)				\
232	__BUS_ACCESSOR(miibus, var, MIIBUS, ivar, type)
233
234MIIBUS_ACCESSOR(flags,		FLAGS,		u_int)
235
236extern devclass_t	miibus_devclass;
237extern driver_t		miibus_driver;
238
239int	mii_attach(device_t, device_t *, if_t, ifm_change_cb_t,
240	    ifm_stat_cb_t, int, int, int, int);
241int	mii_mediachg(struct mii_data *);
242void	mii_tick(struct mii_data *);
243void	mii_pollstat(struct mii_data *);
244void	mii_phy_add_media(struct mii_softc *);
245
246int	mii_phy_auto(struct mii_softc *);
247int	mii_phy_detach(device_t dev);
248u_int	mii_phy_flowstatus(struct mii_softc *);
249void	mii_phy_reset(struct mii_softc *);
250void	mii_phy_setmedia(struct mii_softc *sc);
251void	mii_phy_update(struct mii_softc *, int);
252int	mii_phy_tick(struct mii_softc *);
253int	mii_phy_mac_match(struct mii_softc *, const char *);
254int	mii_dev_mac_match(device_t, const char *);
255void	*mii_phy_mac_softc(struct mii_softc *);
256void	*mii_dev_mac_softc(device_t);
257
258const struct mii_phydesc * mii_phy_match(const struct mii_attach_args *ma,
259    const struct mii_phydesc *mpd);
260const struct mii_phydesc * mii_phy_match_gen(const struct mii_attach_args *ma,
261    const struct mii_phydesc *mpd, size_t endlen);
262int mii_phy_dev_probe(device_t dev, const struct mii_phydesc *mpd, int mrv);
263void mii_phy_dev_attach(device_t dev, u_int flags,
264    const struct mii_phy_funcs *mpf, int add_media);
265
266void	ukphy_status(struct mii_softc *);
267
268u_int	mii_oui(u_int, u_int);
269#define	MII_OUI(id1, id2)	mii_oui(id1, id2)
270#define	MII_MODEL(id2)		(((id2) & IDR2_MODEL) >> 4)
271#define	MII_REV(id2)		((id2) & IDR2_REV)
272
273#endif /* _KERNEL */
274
275#endif /* _DEV_MII_MIIVAR_H_ */
276