1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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_AR7240,
36 	AR8X16_SWITCH_AR8216,
37 	AR8X16_SWITCH_AR8226,
38 	AR8X16_SWITCH_AR8316,
39 	AR8X16_SWITCH_AR9340,
40 	AR8X16_SWITCH_AR8327,
41 	AR8X16_SWITCH_AR8337,
42 } ar8x16_switch_type;
43 
44 /*
45  * XXX TODO: start using this where required
46  */
47 #define	AR8X16_IS_SWITCH(_sc, _type) \
48 	    (!!((_sc)->sc_switchtype == AR8X16_SWITCH_ ## _type))
49 
50 #define ARSWITCH_NUM_PORTS	MAX(AR8327_NUM_PORTS, AR8X16_NUM_PORTS)
51 #define ARSWITCH_NUM_PHYS	MAX(AR8327_NUM_PHYS, AR8X16_NUM_PHYS)
52 
53 #define ARSWITCH_NUM_LEDS	3
54 
55 struct arswitch_dev_led {
56 	struct arswitch_softc	*sc;
57 	struct cdev	*led;
58 	int		phy;
59 	int		lednum;
60 };
61 
62 struct arswitch_softc {
63 	struct mtx	sc_mtx;		/* serialize access to softc */
64 	device_t	sc_dev;
65 	int		phy4cpu;	/* PHY4 is connected to the CPU */
66 	int		numphys;	/* PHYs we manage */
67 	int		is_rgmii;	/* PHY mode is RGMII (XXX which PHY?) */
68 	int		is_gmii;	/* PHY mode is GMII (XXX which PHY?) */
69 	int		is_mii;		/* PHY mode is MII (XXX which PHY?) */
70 	int		page;
71 	int		is_internal_switch;
72 	int		chip_ver;
73 	int		chip_rev;
74 	int		mii_lo_first;		/* Send low data DWORD before high */
75 	ar8x16_switch_type	sc_switchtype;
76 	/* should be the max of both pre-AR8327 and AR8327 ports */
77 	char		*ifname[ARSWITCH_NUM_PHYS];
78 	device_t	miibus[ARSWITCH_NUM_PHYS];
79 	struct ifnet	*ifp[ARSWITCH_NUM_PHYS];
80 	struct arswitch_dev_led	dev_led[ARSWITCH_NUM_PHYS][ARSWITCH_NUM_LEDS];
81 	struct callout	callout_tick;
82 	etherswitch_info_t info;
83 
84 	uint32_t	sc_debug;
85 
86 	/* VLANs support */
87 	int		vid[AR8X16_MAX_VLANS];
88 	uint32_t	vlan_mode;
89 
90 	/* ATU (address table unit) support */
91 	struct {
92 		int count;
93 		int size;
94 		etherswitch_atu_entry_t *entries;
95 	} atu;
96 
97 	struct {
98 		/* Global setup */
99 		int (* arswitch_hw_setup) (struct arswitch_softc *);
100 		int (* arswitch_hw_global_setup) (struct arswitch_softc *);
101 
102 		int (* arswitch_hw_get_switch_macaddr) (struct arswitch_softc *,
103 		    struct ether_addr *sa);
104 		int (* arswitch_hw_set_switch_macaddr) (struct arswitch_softc *,
105 		    const struct ether_addr *sa);
106 
107 		/* Port functions */
108 		void (* arswitch_port_init) (struct arswitch_softc *, int);
109 
110 		/* ATU functions */
111 		int (* arswitch_atu_flush) (struct arswitch_softc *);
112 		int (* arswitch_atu_flush_port) (struct arswitch_softc *, int);
113 		int (* arswitch_atu_learn_default) (struct arswitch_softc *);
114 		int (* arswitch_atu_fetch_table) (struct arswitch_softc *,
115 		    etherswitch_atu_entry_t *, int atu_fetch_op);
116 
117 		/* VLAN functions */
118 		int (* arswitch_port_vlan_setup) (struct arswitch_softc *,
119 		    etherswitch_port_t *);
120 		int (* arswitch_port_vlan_get) (struct arswitch_softc *,
121 		    etherswitch_port_t *);
122 		void (* arswitch_vlan_init_hw) (struct arswitch_softc *);
123 		int (* arswitch_vlan_getvgroup) (struct arswitch_softc *,
124 		    etherswitch_vlangroup_t *);
125 		int (* arswitch_vlan_setvgroup) (struct arswitch_softc *,
126 		    etherswitch_vlangroup_t *);
127 		int (* arswitch_vlan_get_pvid) (struct arswitch_softc *, int,
128 		    int *);
129 		int (* arswitch_vlan_set_pvid) (struct arswitch_softc *, int,
130 		    int);
131 
132 		int (* arswitch_flush_dot1q_vlan) (struct arswitch_softc *sc);
133 		int (* arswitch_purge_dot1q_vlan) (struct arswitch_softc *sc,
134 		    int vid);
135 		int (* arswitch_get_dot1q_vlan) (struct arswitch_softc *,
136 		    uint32_t *ports, uint32_t *untagged_ports, int vid);
137 		int (* arswitch_set_dot1q_vlan) (struct arswitch_softc *sc,
138 		    uint32_t ports, uint32_t untagged_ports, int vid);
139 		int (* arswitch_get_port_vlan) (struct arswitch_softc *sc,
140 		    uint32_t *ports, int vid);
141 		int (* arswitch_set_port_vlan) (struct arswitch_softc *sc,
142 		    uint32_t ports, int vid);
143 
144 		/* PHY functions */
145 		int (* arswitch_phy_read) (device_t, int, int);
146 		int (* arswitch_phy_write) (device_t, int, int, int);
147 	} hal;
148 
149 	struct {
150 		uint32_t port0_status;
151 		uint32_t port5_status;
152 		uint32_t port6_status;
153 	} ar8327;
154 };
155 
156 #define	ARSWITCH_LOCK(_sc)			\
157 	    mtx_lock(&(_sc)->sc_mtx)
158 #define	ARSWITCH_UNLOCK(_sc)			\
159 	    mtx_unlock(&(_sc)->sc_mtx)
160 #define	ARSWITCH_LOCK_ASSERT(_sc, _what)	\
161 	    mtx_assert(&(_sc)->sc_mtx, (_what))
162 #define	ARSWITCH_TRYLOCK(_sc)			\
163 	    mtx_trylock(&(_sc)->sc_mtx)
164 
165 #define	ARSWITCH_DBG_RESET		0x00000001
166 #define	ARSWITCH_DBG_REGIO		0x00000002
167 #define	ARSWITCH_DBG_PHYIO		0x00000004
168 #define	ARSWITCH_DBG_POLL		0x00000008
169 #define	ARSWITCH_DBG_VLAN		0x00000010
170 #define	ARSWITCH_DBG_ATU		0x00000020
171 #define	ARSWITCH_DBG_ANY		0xffffffff
172 
173 #if 1
174 #define DPRINTF(sc, dbg, args...) \
175 	do { \
176 		if (((sc)->sc_debug & (dbg)) || \
177 		    ((sc)->sc_debug == ARSWITCH_DBG_ANY)) { \
178 			device_printf((sc)->sc_dev, args); 	\
179 		} \
180 	} while (0)
181 #define DEVERR(dev, err, fmt, args...) do { \
182 		if (err != 0) device_printf(dev, fmt, err, args); \
183 	} while (0)
184 #else
185 #define DPRINTF(dev, dbg, args...)
186 #define DEVERR(dev, err, fmt, args...)
187 #endif
188 
189 #endif	/* __ARSWITCHVAR_H__ */
190 
191