xref: /freebsd/sys/dev/iwi/if_iwivar.h (revision 0c927cdd8e6e05387fc5a9ffcb5dbe128d4ad749)
1 /*	$FreeBSD$	*/
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 	uint8_t		wr_antsignal;
37 	uint8_t		wr_antenna;
38 };
39 
40 #define IWI_RX_RADIOTAP_PRESENT						\
41 	((1 << IEEE80211_RADIOTAP_FLAGS) |				\
42 	 (1 << IEEE80211_RADIOTAP_RATE) |				\
43 	 (1 << IEEE80211_RADIOTAP_CHANNEL) |				\
44 	 (1 << IEEE80211_RADIOTAP_DB_ANTSIGNAL) |			\
45 	 (1 << IEEE80211_RADIOTAP_ANTENNA))
46 
47 struct iwi_tx_radiotap_header {
48 	struct ieee80211_radiotap_header wt_ihdr;
49 	uint8_t		wt_flags;
50 	uint16_t	wt_chan_freq;
51 	uint16_t	wt_chan_flags;
52 };
53 
54 #define IWI_TX_RADIOTAP_PRESENT						\
55 	((1 << IEEE80211_RADIOTAP_FLAGS) |				\
56 	 (1 << IEEE80211_RADIOTAP_CHANNEL))
57 
58 struct iwi_cmd_ring {
59 	bus_dma_tag_t		desc_dmat;
60 	bus_dmamap_t		desc_map;
61 	bus_addr_t		physaddr;
62 	struct iwi_cmd_desc	*desc;
63 	int			count;
64 	int			queued;
65 	int			cur;
66 	int			next;
67 };
68 
69 struct iwi_tx_data {
70 	bus_dmamap_t		map;
71 	struct mbuf		*m;
72 	struct ieee80211_node	*ni;
73 };
74 
75 struct iwi_tx_ring {
76 	bus_dma_tag_t		desc_dmat;
77 	bus_dma_tag_t		data_dmat;
78 	bus_dmamap_t		desc_map;
79 	bus_addr_t		physaddr;
80 	bus_addr_t		csr_ridx;
81 	bus_addr_t		csr_widx;
82 	struct iwi_tx_desc	*desc;
83 	struct iwi_tx_data	*data;
84 	int			count;
85 	int			queued;
86 	int			cur;
87 	int			next;
88 };
89 
90 struct iwi_rx_data {
91 	bus_dmamap_t	map;
92 	bus_addr_t	physaddr;
93 	uint32_t	reg;
94 	struct mbuf	*m;
95 };
96 
97 struct iwi_rx_ring {
98 	bus_dma_tag_t		data_dmat;
99 	struct iwi_rx_data	*data;
100 	int			count;
101 	int			cur;
102 };
103 
104 struct iwi_node {
105 	struct ieee80211_node	in_node;
106 	int			in_station;
107 #define IWI_MAX_IBSSNODE	32
108 };
109 
110 struct iwi_fw {
111 	const struct firmware	*fp;		/* image handle */
112 	const char		*data;		/* firmware image data */
113 	size_t			size;		/* firmware image size */
114 	const char		*name;		/* associated image name */
115 };
116 
117 struct iwi_softc {
118 	struct ifnet		*sc_ifp;
119 	struct ieee80211com	sc_ic;
120 	int			(*sc_newstate)(struct ieee80211com *,
121 				    enum ieee80211_state, int);
122 	void			(*sc_node_free)(struct ieee80211_node *);
123 	device_t		sc_dev;
124 
125 	struct mtx		sc_mtx;
126 	struct mtx		sc_cmdlock;
127 	char			sc_cmdname[12];	/* e.g. "iwi0_cmd" */
128 	uint8_t			sc_mcast[IEEE80211_ADDR_LEN];
129 	struct unrhdr		*sc_unr;
130 	struct taskqueue	*sc_tq;		/* private task queue */
131 #if __FreeBSD_version < 700000
132 	struct proc		*sc_tqproc;
133 #endif
134 
135 	uint32_t		flags;
136 #define IWI_FLAG_FW_INITED	(1 << 0)
137 #define	IWI_FLAG_BUSY		(1 << 3)	/* busy sending a command */
138 #define	IWI_FLAG_ASSOCIATED	(1 << 4)	/* currently associated  */
139 #define IWI_FLAG_CHANNEL_SCAN	(1 << 5)
140 	uint32_t		fw_state;
141 #define IWI_FW_IDLE		0
142 #define IWI_FW_LOADING		1
143 #define IWI_FW_ASSOCIATING	2
144 #define IWI_FW_DISASSOCIATING	3
145 #define IWI_FW_SCANNING		4
146 	struct iwi_cmd_ring	cmdq;
147 	struct iwi_tx_ring	txq[WME_NUM_AC];
148 	struct iwi_rx_ring	rxq;
149 
150 	struct resource		*irq;
151 	struct resource		*mem;
152 	bus_space_tag_t		sc_st;
153 	bus_space_handle_t	sc_sh;
154 	void 			*sc_ih;
155 	int			mem_rid;
156 	int			irq_rid;
157 
158 	/*
159 	 * The card needs external firmware images to work, which is made of a
160 	 * bootloader, microcode and firmware proper. In version 3.00 and
161 	 * above, all pieces are contained in a single image, preceded by a
162 	 * struct iwi_firmware_hdr indicating the size of the 3 pieces.
163 	 * Old firmware < 3.0 has separate boot and ucode, so we need to
164 	 * load all of them explicitly.
165 	 * To avoid issues related to fragmentation, we keep the block of
166 	 * dma-ble memory around until detach time, and reallocate it when
167 	 * it becomes too small. fw_dma_size is the size currently allocated.
168 	 */
169 	int			fw_dma_size;
170 	uint32_t		fw_flags;	/* allocation status */
171 #define	IWI_FW_HAVE_DMAT	0x01
172 #define	IWI_FW_HAVE_MAP		0x02
173 #define	IWI_FW_HAVE_PHY		0x04
174 	bus_dma_tag_t		fw_dmat;
175 	bus_dmamap_t		fw_map;
176 	bus_addr_t		fw_physaddr;
177 	void			*fw_virtaddr;
178 	enum ieee80211_opmode	fw_mode;	/* mode of current firmware */
179 	struct iwi_fw		fw_boot;	/* boot firmware */
180 	struct iwi_fw		fw_uc;		/* microcode */
181 	struct iwi_fw		fw_fw;		/* operating mode support */
182 
183 	int			curchan;	/* current h/w channel # */
184 	int			antenna;
185 	int			bluetooth;
186 	struct iwi_associate	assoc;
187 	struct iwi_wme_params	wme[3];
188 	u_int			sc_scangen;
189 
190 	struct task		sc_radiontask;	/* radio on processing */
191 	struct task		sc_radiofftask;	/* radio off processing */
192 	struct task		sc_scanaborttask;	/* cancel active scan */
193 	struct task		sc_restarttask;	/* restart adapter processing */
194 	struct task		sc_opstask;	/* scan / auth processing */
195 
196 	unsigned int		sc_softled : 1,	/* enable LED gpio status */
197 				sc_ledstate: 1,	/* LED on/off state */
198 				sc_blinking: 1;	/* LED blink operation active */
199 	u_int			sc_nictype;	/* NIC type from EEPROM */
200 	u_int			sc_ledpin;	/* mask for activity LED */
201 	u_int			sc_ledidle;	/* idle polling interval */
202 	int			sc_ledevent;	/* time of last LED event */
203 	u_int8_t		sc_rxrate;	/* current rx rate for LED */
204 	u_int8_t		sc_rxrix;
205 	u_int8_t		sc_txrate;	/* current tx rate for LED */
206 	u_int8_t		sc_txrix;
207 	u_int16_t		sc_ledoff;	/* off time for current blink */
208 	struct callout		sc_ledtimer;	/* led off timer */
209 	struct callout		sc_wdtimer;	/* watchdog timer */
210 
211 	int			sc_tx_timer;
212 	int			sc_rfkill_timer;/* poll for rfkill change */
213 	int			sc_state_timer;	/* firmware state timer */
214 	int			sc_busy_timer;	/* firmware cmd timer */
215 
216 #define IWI_SCAN_START		(1 << 0)
217 #define IWI_SET_CHANNEL	        (1 << 1)
218 #define	IWI_SCAN_END		(1 << 2)
219 #define	IWI_ASSOC		(1 << 3)
220 #define	IWI_DISASSOC		(1 << 4)
221 #define	IWI_SCAN_CURCHAN	(1 << 5)
222 #define	IWI_SCAN_ALLCHAN	(1 << 6)
223 #define	IWI_SET_WME		(1 << 7)
224 #define	IWI_CMD_MAXOPS		10
225 	int                     sc_cmd[IWI_CMD_MAXOPS];
226 	int                     sc_cmd_cur;    /* current queued scan task */
227 	int                     sc_cmd_next;   /* last queued scan task */
228 	unsigned long		sc_maxdwell;	/* max dwell time for curchan */
229 	struct bpf_if		*sc_drvbpf;
230 
231 	union {
232 		struct iwi_rx_radiotap_header th;
233 		uint8_t	pad[64];
234 	}			sc_rxtapu;
235 #define sc_rxtap	sc_rxtapu.th
236 	int			sc_rxtap_len;
237 
238 	union {
239 		struct iwi_tx_radiotap_header th;
240 		uint8_t	pad[64];
241 	}			sc_txtapu;
242 #define sc_txtap	sc_txtapu.th
243 	int			sc_txtap_len;
244 };
245 
246 #define	IWI_STATE_BEGIN(_sc, _state)	do {			\
247 	KASSERT(_sc->fw_state == IWI_FW_IDLE,			\
248 	    ("iwi firmware not idle"));				\
249 	_sc->fw_state = _state;					\
250 	_sc->sc_state_timer = 5;				\
251 	DPRINTF(("enter FW state %d\n",	_state));		\
252 } while (0)
253 
254 #define	IWI_STATE_END(_sc, _state)	do {			\
255 	if (_sc->fw_state == _state)				\
256 		DPRINTF(("exit FW state %d\n", _state));	\
257 	 else							\
258 		DPRINTF(("expected FW state %d, got %d\n",	\
259 			    _state, _sc->fw_state));		\
260 	_sc->fw_state = IWI_FW_IDLE;				\
261 	wakeup(_sc);						\
262 	_sc->sc_state_timer = 0;				\
263 } while (0)
264 /*
265  * NB.: This models the only instance of async locking in iwi_init_locked
266  *	and must be kept in sync.
267  */
268 #define	IWI_LOCK_INIT(sc) \
269 	mtx_init(&(sc)->sc_mtx, device_get_nameunit((sc)->sc_dev), \
270 	    MTX_NETWORK_LOCK, MTX_DEF)
271 #define	IWI_LOCK_DESTROY(sc)	mtx_destroy(&(sc)->sc_mtx)
272 #define	IWI_LOCK_DECL	int	__waslocked = 0
273 #define IWI_LOCK_ASSERT(sc)	mtx_assert(&(sc)->sc_mtx, MA_OWNED)
274 #define IWI_LOCK(sc)	do {				\
275 	if (!(__waslocked = mtx_owned(&(sc)->sc_mtx)))	\
276 		mtx_lock(&(sc)->sc_mtx);		\
277 } while (0)
278 #define IWI_UNLOCK(sc)	do {			\
279 	if (!__waslocked)			\
280 		mtx_unlock(&(sc)->sc_mtx);	\
281 } while (0)
282 #define	IWI_CMD_LOCK_INIT(sc) do { \
283 	snprintf((sc)->sc_cmdname, sizeof((sc)->sc_cmdname), "%s_cmd", \
284 		device_get_nameunit((sc)->sc_dev)); \
285 	mtx_init(&(sc)->sc_cmdlock, (sc)->sc_cmdname, NULL, MTX_DEF); \
286 } while (0)
287 #define	IWI_CMD_LOCK_DESTROY(sc)	mtx_destroy(&(sc)->sc_cmdlock)
288 #define	IWI_CMD_LOCK(sc)		mtx_lock(&(sc)->sc_cmdlock)
289 #define	IWI_CMD_UNLOCK(sc)		mtx_unlock(&(sc)->sc_cmdlock)
290