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