xref: /dragonfly/sys/dev/netif/iwi/if_iwivar.h (revision ef2b2b9d)
1 /*	$FreeBSD: head/sys/dev/iwi/if_iwivar.h 290407 2015-11-05 17:58:18Z avos $	*/
2 
3 /*-
4  * Copyright (c) 2004, 2005
5  *      Damien Bergamini <damien.bergamini@free.fr>. 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 unmodified, this list of conditions, and the following
12  *    disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 struct iwi_rx_radiotap_header {
31 	struct ieee80211_radiotap_header wr_ihdr;
32 	uint8_t		wr_flags;
33 	uint8_t		wr_rate;
34 	uint16_t	wr_chan_freq;
35 	uint16_t	wr_chan_flags;
36 	int8_t		wr_antsignal;
37 	int8_t		wr_antnoise;
38 	uint8_t		wr_antenna;
39 };
40 
41 #define IWI_RX_RADIOTAP_PRESENT						\
42 	((1 << IEEE80211_RADIOTAP_FLAGS) |				\
43 	 (1 << IEEE80211_RADIOTAP_RATE) |				\
44 	 (1 << IEEE80211_RADIOTAP_CHANNEL) |				\
45 	 (1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL) |			\
46 	 (1 << IEEE80211_RADIOTAP_DBM_ANTNOISE) |			\
47 	 (1 << IEEE80211_RADIOTAP_ANTENNA))
48 
49 struct iwi_tx_radiotap_header {
50 	struct ieee80211_radiotap_header wt_ihdr;
51 	uint8_t		wt_flags;
52 	uint16_t	wt_chan_freq;
53 	uint16_t	wt_chan_flags;
54 };
55 
56 #define IWI_TX_RADIOTAP_PRESENT						\
57 	((1 << IEEE80211_RADIOTAP_FLAGS) |				\
58 	 (1 << IEEE80211_RADIOTAP_CHANNEL))
59 
60 struct iwi_cmd_ring {
61 	bus_dma_tag_t		desc_dmat;
62 	bus_dmamap_t		desc_map;
63 	bus_addr_t		physaddr;
64 	struct iwi_cmd_desc	*desc;
65 	int			count;
66 	int			queued;
67 	int			cur;
68 	int			next;
69 };
70 
71 struct iwi_tx_data {
72 	bus_dmamap_t		map;
73 	struct mbuf		*m;
74 	struct ieee80211_node	*ni;
75 };
76 
77 struct iwi_tx_ring {
78 	bus_dma_tag_t		desc_dmat;
79 	bus_dma_tag_t		data_dmat;
80 	bus_dmamap_t		desc_map;
81 	bus_addr_t		physaddr;
82 	bus_addr_t		csr_ridx;
83 	bus_addr_t		csr_widx;
84 	struct iwi_tx_desc	*desc;
85 	struct iwi_tx_data	*data;
86 	int			count;
87 	int			queued;
88 	int			cur;
89 	int			next;
90 };
91 
92 struct iwi_rx_data {
93 	bus_dmamap_t	map;
94 	bus_addr_t	physaddr;
95 	uint32_t	reg;
96 	struct mbuf	*m;
97 };
98 
99 struct iwi_rx_ring {
100 	bus_dma_tag_t		data_dmat;
101 	struct iwi_rx_data	*data;
102 	int			count;
103 	int			cur;
104 };
105 
106 struct iwi_node {
107 	struct ieee80211_node	in_node;
108 	int			in_station;
109 #define IWI_MAX_IBSSNODE	32
110 };
111 
112 struct iwi_fw {
113 	const struct firmware	*fp;		/* image handle */
114 	const char		*data;		/* firmware image data */
115 	size_t			size;		/* firmware image size */
116 	const char		*name;		/* associated image name */
117 };
118 
119 struct iwi_vap {
120 	struct ieee80211vap	iwi_vap;
121 
122 	int			(*iwi_newstate)(struct ieee80211vap *,
123 				    enum ieee80211_state, int);
124 };
125 #define	IWI_VAP(vap)	((struct iwi_vap *)(vap))
126 
127 struct iwi_softc {
128 #if defined(__DragonFly__)
129 	struct lock		sc_lock;
130 #else
131 	struct mtx		sc_mtx;
132 #endif
133 	struct ieee80211com	sc_ic;
134 	struct mbufq		sc_snd;
135 	device_t		sc_dev;
136 
137 	void			(*sc_node_free)(struct ieee80211_node *);
138 
139 	uint8_t			sc_mcast[IEEE80211_ADDR_LEN];
140 #if defined(__DragonFly__)
141 	struct devfs_bitmap	sc_unr;
142 #else
143 	struct unrhdr		*sc_unr;
144 #endif
145 
146 	uint32_t		flags;
147 #define IWI_FLAG_FW_INITED	(1 << 0)
148 #define	IWI_FLAG_BUSY		(1 << 3)	/* busy sending a command */
149 #define	IWI_FLAG_ASSOCIATED	(1 << 4)	/* currently associated  */
150 #define IWI_FLAG_CHANNEL_SCAN	(1 << 5)
151 	uint32_t		fw_state;
152 #define IWI_FW_IDLE		0
153 #define IWI_FW_LOADING		1
154 #define IWI_FW_ASSOCIATING	2
155 #define IWI_FW_DISASSOCIATING	3
156 #define IWI_FW_SCANNING		4
157 	struct iwi_cmd_ring	cmdq;
158 	struct iwi_tx_ring	txq[WME_NUM_AC];
159 	struct iwi_rx_ring	rxq;
160 
161 	struct resource		*irq;
162 	struct resource		*mem;
163 	bus_space_tag_t		sc_st;
164 	bus_space_handle_t	sc_sh;
165 	void 			*sc_ih;
166 
167 	/*
168 	 * The card needs external firmware images to work, which is made of a
169 	 * bootloader, microcode and firmware proper. In version 3.00 and
170 	 * above, all pieces are contained in a single image, preceded by a
171 	 * struct iwi_firmware_hdr indicating the size of the 3 pieces.
172 	 * Old firmware < 3.0 has separate boot and ucode, so we need to
173 	 * load all of them explicitly.
174 	 * To avoid issues related to fragmentation, we keep the block of
175 	 * dma-ble memory around until detach time, and reallocate it when
176 	 * it becomes too small. fw_dma_size is the size currently allocated.
177 	 */
178 	int			fw_dma_size;
179 	uint32_t		fw_flags;	/* allocation status */
180 #define	IWI_FW_HAVE_DMAT	0x01
181 #define	IWI_FW_HAVE_MAP		0x02
182 #define	IWI_FW_HAVE_PHY		0x04
183 	bus_dma_tag_t		fw_dmat;
184 	bus_dmamap_t		fw_map;
185 	bus_addr_t		fw_physaddr;
186 	void			*fw_virtaddr;
187 	enum ieee80211_opmode	fw_mode;	/* mode of current firmware */
188 	struct iwi_fw		fw_boot;	/* boot firmware */
189 	struct iwi_fw		fw_uc;		/* microcode */
190 	struct iwi_fw		fw_fw;		/* operating mode support */
191 
192 	int			curchan;	/* current h/w channel # */
193 	int			antenna;
194 	int			bluetooth;
195 	struct iwi_associate	assoc;
196 	struct iwi_wme_params	wme[3];
197 	u_int			sc_scangen;
198 
199 	struct task		sc_radiontask;	/* radio on processing */
200 	struct task		sc_radiofftask;	/* radio off processing */
201 	struct task		sc_restarttask;	/* restart adapter processing */
202 	struct task		sc_disassoctask;
203 	struct task		sc_monitortask;
204 
205 	unsigned int		sc_running : 1,	/* initialized */
206 				sc_softled : 1,	/* enable LED gpio status */
207 				sc_ledstate: 1,	/* LED on/off state */
208 				sc_blinking: 1;	/* LED blink operation active */
209 	u_int			sc_nictype;	/* NIC type from EEPROM */
210 	u_int			sc_ledpin;	/* mask for activity LED */
211 	u_int			sc_ledidle;	/* idle polling interval */
212 	int			sc_ledevent;	/* time of last LED event */
213 	u_int8_t		sc_rxrate;	/* current rx rate for LED */
214 	u_int8_t		sc_rxrix;
215 	u_int8_t		sc_txrate;	/* current tx rate for LED */
216 	u_int8_t		sc_txrix;
217 	u_int16_t		sc_ledoff;	/* off time for current blink */
218 	struct callout		sc_ledtimer;	/* led off timer */
219 	struct callout		sc_wdtimer;	/* watchdog timer */
220 	struct callout		sc_rftimer;	/* rfkill timer */
221 
222 	int			sc_tx_timer;
223 	int			sc_state_timer;	/* firmware state timer */
224 	int			sc_busy_timer;	/* firmware cmd timer */
225 
226 	struct iwi_rx_radiotap_header sc_rxtap;
227 	struct iwi_tx_radiotap_header sc_txtap;
228 
229 	struct iwi_notif_link_quality sc_linkqual;
230 	int			sc_linkqual_valid;
231 };
232 
233 #define	IWI_STATE_BEGIN(_sc, _state)	do {			\
234 	KASSERT(_sc->fw_state == IWI_FW_IDLE,			\
235 	    ("iwi firmware not idle, state %s", iwi_fw_states[_sc->fw_state]));\
236 	_sc->fw_state = _state;					\
237 	_sc->sc_state_timer = 5;				\
238 	DPRINTF(("enter %s state\n", iwi_fw_states[_state]));	\
239 } while (0)
240 
241 #define	IWI_STATE_END(_sc, _state)	do {			\
242 	if (_sc->fw_state == _state)				\
243 		DPRINTF(("exit %s state\n", iwi_fw_states[_state])); \
244 	 else							\
245 		DPRINTF(("expected %s state, got %s\n",	\
246 		    iwi_fw_states[_state], iwi_fw_states[_sc->fw_state])); \
247 	_sc->fw_state = IWI_FW_IDLE;				\
248 	wakeup(_sc);						\
249 	_sc->sc_state_timer = 0;				\
250 } while (0)
251 
252 /*
253  * NB.: This models the only instance of async locking in iwi_init_locked
254  *	and must be kept in sync.
255  */
256 #if defined(__DragonFly__)
257 
258 #define	IWI_LOCK_INIT(sc) \
259 	lockinit(&(sc)->sc_lock, device_get_nameunit((sc)->sc_dev), 0, 0)
260 #define	IWI_LOCK_DESTROY(sc)	lockuninit(&(sc)->sc_lock)
261 #define	IWI_LOCK_DECL	int	__waslocked = 0
262 #define IWI_LOCK_ASSERT(sc)	KKASSERT(lockstatus(&(sc)->sc_lock, curthread) == LK_EXCLUSIVE)
263 #define IWI_LOCK(sc)	do {				\
264 	if (!(__waslocked = (lockstatus(&(sc)->sc_lock, curthread) == LK_EXCLUSIVE)))	\
265 		lockmgr(&(sc)->sc_lock, LK_EXCLUSIVE);	\
266 } while (0)
267 #define IWI_UNLOCK(sc)	do {			\
268 	if (!__waslocked)			\
269 		lockmgr(&(sc)->sc_lock, LK_RELEASE);	\
270 } while (0)
271 
272 #else
273 
274 #define	IWI_LOCK_INIT(sc) \
275 	mtx_init(&(sc)->sc_mtx, device_get_nameunit((sc)->sc_dev), \
276 	    MTX_NETWORK_LOCK, MTX_DEF)
277 #define	IWI_LOCK_DESTROY(sc)	mtx_destroy(&(sc)->sc_mtx)
278 #define	IWI_LOCK_DECL	int	__waslocked = 0
279 #define IWI_LOCK_ASSERT(sc)	mtx_assert(&(sc)->sc_mtx, MA_OWNED)
280 #define IWI_LOCK(sc)	do {				\
281 	if (!(__waslocked = mtx_owned(&(sc)->sc_mtx)))	\
282 		mtx_lock(&(sc)->sc_mtx);		\
283 } while (0)
284 #define IWI_UNLOCK(sc)	do {			\
285 	if (!__waslocked)			\
286 		mtx_unlock(&(sc)->sc_mtx);	\
287 } while (0)
288 
289 #endif
290