xref: /freebsd/sys/dev/etherswitch/arswitch/arswitchvar.h (revision f3e11927dc287ff686b5cc3618aba4e1e58a5dca)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2011-2012 Stefan Bethke.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 #ifndef	__ARSWITCHVAR_H__
31 #define	__ARSWITCHVAR_H__
32 
33 typedef enum {
34 	AR8X16_SWITCH_NONE,
35 	AR8X16_SWITCH_AR8216,
36 	AR8X16_SWITCH_AR8226,
37 	AR8X16_SWITCH_AR8316,
38 	AR8X16_SWITCH_AR8327,
39 	AR8X16_SWITCH_AR8337,
40 } ar8x16_switch_type;
41 
42 /*
43  * XXX TODO: start using this where required
44  */
45 #define	AR8X16_IS_SWITCH(_sc, _type) \
46 	    (!!((_sc)->sc_switchtype == AR8X16_SWITCH_ ## _type))
47 
48 #define ARSWITCH_NUM_PORTS	MAX(AR8327_NUM_PORTS, AR8X16_NUM_PORTS)
49 #define ARSWITCH_NUM_PHYS	MAX(AR8327_NUM_PHYS, AR8X16_NUM_PHYS)
50 
51 #define ARSWITCH_NUM_LEDS	3
52 
53 struct arswitch_dev_led {
54 	struct arswitch_softc	*sc;
55 	struct cdev	*led;
56 	int		phy;
57 	int		lednum;
58 };
59 
60 struct arswitch_softc {
61 	struct mtx	sc_mtx;		/* serialize access to softc */
62 	device_t	sc_dev;
63 	int		phy4cpu;	/* PHY4 is connected to the CPU */
64 	int		numphys;	/* PHYs we manage */
65 	int		is_rgmii;	/* PHY mode is RGMII (XXX which PHY?) */
66 	int		is_gmii;	/* PHY mode is GMII (XXX which PHY?) */
67 	int		is_mii;		/* PHY mode is MII (XXX which PHY?) */
68 	int		page;
69 	int		chip_ver;
70 	int		chip_rev;
71 	int		mii_lo_first;		/* Send low data DWORD before high */
72 	ar8x16_switch_type	sc_switchtype;
73 	/* should be the max of both pre-AR8327 and AR8327 ports */
74 	char		*ifname[ARSWITCH_NUM_PHYS];
75 	device_t	miibus[ARSWITCH_NUM_PHYS];
76 	if_t ifp[ARSWITCH_NUM_PHYS];
77 	struct arswitch_dev_led	dev_led[ARSWITCH_NUM_PHYS][ARSWITCH_NUM_LEDS];
78 	struct callout	callout_tick;
79 	etherswitch_info_t info;
80 
81 	uint32_t	sc_debug;
82 
83 	/* VLANs support */
84 	int		vid[AR8X16_MAX_VLANS];
85 	uint32_t	vlan_mode;
86 
87 	/* ATU (address table unit) support */
88 	struct {
89 		int count;
90 		int size;
91 		etherswitch_atu_entry_t *entries;
92 	} atu;
93 
94 	struct {
95 		/* Global setup */
96 		int (* arswitch_hw_setup) (struct arswitch_softc *);
97 		int (* arswitch_hw_global_setup) (struct arswitch_softc *);
98 
99 		int (* arswitch_hw_get_switch_macaddr) (struct arswitch_softc *,
100 		    struct ether_addr *sa);
101 		int (* arswitch_hw_set_switch_macaddr) (struct arswitch_softc *,
102 		    const struct ether_addr *sa);
103 
104 		/* Port functions */
105 		void (* arswitch_port_init) (struct arswitch_softc *, int);
106 
107 		/* ATU functions */
108 		int (* arswitch_atu_flush) (struct arswitch_softc *);
109 		int (* arswitch_atu_flush_port) (struct arswitch_softc *, int);
110 		int (* arswitch_atu_learn_default) (struct arswitch_softc *);
111 		int (* arswitch_atu_fetch_table) (struct arswitch_softc *,
112 		    etherswitch_atu_entry_t *, int atu_fetch_op);
113 
114 		/* VLAN functions */
115 		int (* arswitch_port_vlan_setup) (struct arswitch_softc *,
116 		    etherswitch_port_t *);
117 		int (* arswitch_port_vlan_get) (struct arswitch_softc *,
118 		    etherswitch_port_t *);
119 		void (* arswitch_vlan_init_hw) (struct arswitch_softc *);
120 		int (* arswitch_vlan_getvgroup) (struct arswitch_softc *,
121 		    etherswitch_vlangroup_t *);
122 		int (* arswitch_vlan_setvgroup) (struct arswitch_softc *,
123 		    etherswitch_vlangroup_t *);
124 		int (* arswitch_vlan_get_pvid) (struct arswitch_softc *, int,
125 		    int *);
126 		int (* arswitch_vlan_set_pvid) (struct arswitch_softc *, int,
127 		    int);
128 
129 		int (* arswitch_flush_dot1q_vlan) (struct arswitch_softc *sc);
130 		int (* arswitch_purge_dot1q_vlan) (struct arswitch_softc *sc,
131 		    int vid);
132 		int (* arswitch_get_dot1q_vlan) (struct arswitch_softc *,
133 		    uint32_t *ports, uint32_t *untagged_ports, int vid);
134 		int (* arswitch_set_dot1q_vlan) (struct arswitch_softc *sc,
135 		    uint32_t ports, uint32_t untagged_ports, int vid);
136 		int (* arswitch_get_port_vlan) (struct arswitch_softc *sc,
137 		    uint32_t *ports, int vid);
138 		int (* arswitch_set_port_vlan) (struct arswitch_softc *sc,
139 		    uint32_t ports, int vid);
140 
141 		/* PHY functions */
142 		int (* arswitch_phy_read) (device_t, int, int);
143 		int (* arswitch_phy_write) (device_t, int, int, int);
144 	} hal;
145 
146 	struct {
147 		uint32_t port0_status;
148 		uint32_t port5_status;
149 		uint32_t port6_status;
150 	} ar8327;
151 };
152 
153 #define	ARSWITCH_LOCK(_sc)			\
154 	    mtx_lock(&(_sc)->sc_mtx)
155 #define	ARSWITCH_UNLOCK(_sc)			\
156 	    mtx_unlock(&(_sc)->sc_mtx)
157 #define	ARSWITCH_LOCK_ASSERT(_sc, _what)	\
158 	    mtx_assert(&(_sc)->sc_mtx, (_what))
159 #define	ARSWITCH_TRYLOCK(_sc)			\
160 	    mtx_trylock(&(_sc)->sc_mtx)
161 
162 #define	ARSWITCH_DBG_RESET		0x00000001
163 #define	ARSWITCH_DBG_REGIO		0x00000002
164 #define	ARSWITCH_DBG_PHYIO		0x00000004
165 #define	ARSWITCH_DBG_POLL		0x00000008
166 #define	ARSWITCH_DBG_VLAN		0x00000010
167 #define	ARSWITCH_DBG_ATU		0x00000020
168 #define	ARSWITCH_DBG_ANY		0xffffffff
169 
170 #if 1
171 #define DPRINTF(sc, dbg, args...) \
172 	do { \
173 		if (((sc)->sc_debug & (dbg)) || \
174 		    ((sc)->sc_debug == ARSWITCH_DBG_ANY)) { \
175 			device_printf((sc)->sc_dev, args); 	\
176 		} \
177 	} while (0)
178 #define DEVERR(dev, err, fmt, args...) do { \
179 		if (err != 0) device_printf(dev, fmt, err, args); \
180 	} while (0)
181 #else
182 #define DPRINTF(dev, dbg, args...)
183 #define DEVERR(dev, err, fmt, args...)
184 #endif
185 
186 #endif	/* __ARSWITCHVAR_H__ */
187 
188