xref: /freebsd/sys/dev/iwm/if_iwmvar.h (revision 355c15130aef13484821051a655da9b9066e1015)
1 /*	$OpenBSD: if_iwmvar.h,v 1.7 2015/03/02 13:51:10 jsg Exp $	*/
2 /*	$FreeBSD$ */
3 
4 /*
5  * Copyright (c) 2014 genua mbh <info@genua.de>
6  * Copyright (c) 2014 Fixup Software Ltd.
7  *
8  * Permission to use, copy, modify, and distribute this software for any
9  * purpose with or without fee is hereby granted, provided that the above
10  * copyright notice and this permission notice appear in all copies.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19  */
20 
21 /*-
22  * Based on BSD-licensed source modules in the Linux iwlwifi driver,
23  * which were used as the reference documentation for this implementation.
24  *
25  * Driver version we are currently based off of is
26  * Linux 3.14.3 (tag id a2df521e42b1d9a23f620ac79dbfe8655a8391dd)
27  *
28  ***********************************************************************
29  *
30  * This file is provided under a dual BSD/GPLv2 license.  When using or
31  * redistributing this file, you may do so under either license.
32  *
33  * GPL LICENSE SUMMARY
34  *
35  * Copyright(c) 2007 - 2013 Intel Corporation. All rights reserved.
36  *
37  * This program is free software; you can redistribute it and/or modify
38  * it under the terms of version 2 of the GNU General Public License as
39  * published by the Free Software Foundation.
40  *
41  * This program is distributed in the hope that it will be useful, but
42  * WITHOUT ANY WARRANTY; without even the implied warranty of
43  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
44  * General Public License for more details.
45  *
46  * You should have received a copy of the GNU General Public License
47  * along with this program; if not, write to the Free Software
48  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
49  * USA
50  *
51  * The full GNU General Public License is included in this distribution
52  * in the file called COPYING.
53  *
54  * Contact Information:
55  *  Intel Linux Wireless <ilw@linux.intel.com>
56  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
57  *
58  *
59  * BSD LICENSE
60  *
61  * Copyright(c) 2005 - 2013 Intel Corporation. All rights reserved.
62  * All rights reserved.
63  *
64  * Redistribution and use in source and binary forms, with or without
65  * modification, are permitted provided that the following conditions
66  * are met:
67  *
68  *  * Redistributions of source code must retain the above copyright
69  *    notice, this list of conditions and the following disclaimer.
70  *  * Redistributions in binary form must reproduce the above copyright
71  *    notice, this list of conditions and the following disclaimer in
72  *    the documentation and/or other materials provided with the
73  *    distribution.
74  *  * Neither the name Intel Corporation nor the names of its
75  *    contributors may be used to endorse or promote products derived
76  *    from this software without specific prior written permission.
77  *
78  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
79  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
80  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
81  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
82  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
83  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
84  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
85  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
86  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
87  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
88  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
89  */
90 
91 /*-
92  * Copyright (c) 2007-2010 Damien Bergamini <damien.bergamini@free.fr>
93  *
94  * Permission to use, copy, modify, and distribute this software for any
95  * purpose with or without fee is hereby granted, provided that the above
96  * copyright notice and this permission notice appear in all copies.
97  *
98  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
99  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
100  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
101  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
102  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
103  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
104  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
105  */
106 
107 struct iwm_rx_radiotap_header {
108 	struct ieee80211_radiotap_header wr_ihdr;
109 	uint64_t	wr_tsft;
110 	uint8_t		wr_flags;
111 	uint8_t		wr_rate;
112 	uint16_t	wr_chan_freq;
113 	uint16_t	wr_chan_flags;
114 	int8_t		wr_dbm_antsignal;
115 	int8_t		wr_dbm_antnoise;
116 } __packed __aligned(8);
117 
118 #define IWM_RX_RADIOTAP_PRESENT						\
119 	((1 << IEEE80211_RADIOTAP_TSFT) |				\
120 	 (1 << IEEE80211_RADIOTAP_FLAGS) |				\
121 	 (1 << IEEE80211_RADIOTAP_RATE) |				\
122 	 (1 << IEEE80211_RADIOTAP_CHANNEL) |				\
123 	 (1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL) |			\
124 	 (1 << IEEE80211_RADIOTAP_DBM_ANTNOISE))
125 
126 struct iwm_tx_radiotap_header {
127 	struct ieee80211_radiotap_header wt_ihdr;
128 	uint8_t		wt_flags;
129 	uint8_t		wt_rate;
130 	uint16_t	wt_chan_freq;
131 	uint16_t	wt_chan_flags;
132 } __packed;
133 
134 #define IWM_TX_RADIOTAP_PRESENT						\
135 	((1 << IEEE80211_RADIOTAP_FLAGS) |				\
136 	 (1 << IEEE80211_RADIOTAP_RATE) |				\
137 	 (1 << IEEE80211_RADIOTAP_CHANNEL))
138 
139 
140 #define IWM_UCODE_SECTION_MAX 16
141 
142 /**
143  * enum iwm_ucode_type
144  *
145  * The type of ucode.
146  *
147  * @IWM_UCODE_REGULAR: Normal runtime ucode
148  * @IWM_UCODE_INIT: Initial ucode
149  * @IWM_UCODE_WOWLAN: Wake on Wireless enabled ucode
150  * @IWM_UCODE_REGULAR_USNIFFER: Normal runtime ucode when using usniffer image
151  */
152 enum iwm_ucode_type {
153 	IWM_UCODE_REGULAR,
154 	IWM_UCODE_INIT,
155 	IWM_UCODE_WOWLAN,
156 	IWM_UCODE_REGULAR_USNIFFER,
157 	IWM_UCODE_TYPE_MAX
158 };
159 
160 struct iwm_ucode_capabilities {
161 	uint32_t max_probe_length;
162 	uint32_t n_scan_channels;
163 	uint32_t flags;
164 	uint8_t enabled_api[howmany(IWM_NUM_UCODE_TLV_API, NBBY)];
165 	uint8_t enabled_capa[howmany(IWM_NUM_UCODE_TLV_CAPA, NBBY)];
166 };
167 
168 /* one for each uCode image (inst/data, init/runtime/wowlan) */
169 struct iwm_fw_desc {
170 	const void *data;	/* vmalloc'ed data */
171 	uint32_t len;		/* size in bytes */
172 	uint32_t offset;	/* offset in the device */
173 };
174 
175 struct iwm_fw_img {
176 	struct iwm_fw_desc sec[IWM_UCODE_SECTION_MAX];
177 	int fw_count;
178 	int is_dual_cpus;
179 	uint32_t paging_mem_size;
180 };
181 
182 struct iwm_fw_info {
183 	const struct firmware *fw_fp;
184 
185 	/* ucode images */
186 	struct iwm_fw_img img[IWM_UCODE_TYPE_MAX];
187 
188 	struct iwm_ucode_capabilities ucode_capa;
189 
190 	uint32_t phy_config;
191 	uint8_t valid_tx_ant;
192 	uint8_t valid_rx_ant;
193 };
194 
195 struct iwm_nvm_data {
196 	int n_hw_addrs;
197 	uint8_t hw_addr[IEEE80211_ADDR_LEN];
198 
199 	int sku_cap_band_24GHz_enable;
200 	int sku_cap_band_52GHz_enable;
201 	int sku_cap_11n_enable;
202 	int sku_cap_amt_enable;
203 	int sku_cap_ipan_enable;
204 
205 	uint8_t radio_cfg_type;
206 	uint8_t radio_cfg_step;
207 	uint8_t radio_cfg_dash;
208 	uint8_t radio_cfg_pnum;
209 	uint8_t valid_tx_ant, valid_rx_ant;
210 #define IWM_NUM_CHANNELS	39
211 #define IWM_NUM_CHANNELS_8000	51
212 
213 	uint16_t nvm_version;
214 	uint8_t max_tx_pwr_half_dbm;
215 
216 	boolean_t lar_enabled;
217 	uint16_t nvm_ch_flags[];
218 };
219 
220 /* max bufs per tfd the driver will use */
221 #define IWM_MAX_CMD_TBS_PER_TFD 2
222 
223 struct iwm_rx_packet;
224 struct iwm_host_cmd {
225 	const void *data[IWM_MAX_CMD_TBS_PER_TFD];
226 	struct iwm_rx_packet *resp_pkt;
227 	unsigned long _rx_page_addr;
228 	uint32_t _rx_page_order;
229 	int handler_status;
230 
231 	uint32_t flags;
232 	uint32_t id;
233 	uint16_t len[IWM_MAX_CMD_TBS_PER_TFD];
234 	uint8_t dataflags[IWM_MAX_CMD_TBS_PER_TFD];
235 };
236 
237 /*
238  * DMA glue is from iwn
239  */
240 
241 typedef caddr_t iwm_caddr_t;
242 typedef void *iwm_hookarg_t;
243 
244 struct iwm_dma_info {
245 	bus_dma_tag_t		tag;
246 	bus_dmamap_t		map;
247 	bus_dma_segment_t	seg;
248 	bus_addr_t		paddr;
249 	void 			*vaddr;
250 	bus_size_t		size;
251 };
252 
253 /**
254  * struct iwm_fw_paging
255  * @fw_paging_block: dma memory info
256  * @fw_paging_size: page size
257  */
258 struct iwm_fw_paging {
259 	struct iwm_dma_info fw_paging_block;
260 	uint32_t fw_paging_size;
261 };
262 
263 #define IWM_TX_RING_COUNT	256
264 #define IWM_TX_RING_LOMARK	192
265 #define IWM_TX_RING_HIMARK	224
266 
267 struct iwm_tx_data {
268 	bus_dmamap_t		map;
269 	bus_addr_t		cmd_paddr;
270 	bus_addr_t		scratch_paddr;
271 	struct mbuf		*m;
272 	struct iwm_node 	*in;
273 	int			done;
274 };
275 
276 struct iwm_tx_ring {
277 	struct iwm_dma_info	desc_dma;
278 	struct iwm_dma_info	cmd_dma;
279 	struct iwm_tfd		*desc;
280 	struct iwm_device_cmd	*cmd;
281 	bus_dma_tag_t		data_dmat;
282 	struct iwm_tx_data	data[IWM_TX_RING_COUNT];
283 	int			qid;
284 	int			queued;
285 	int			cur;
286 };
287 
288 #define IWM_RX_LEGACY_RING_COUNT	256
289 #define IWM_RX_MQ_RING_COUNT		512
290 
291 #define IWM_RBUF_SIZE		4096
292 
293 #define IWM_MAX_SCATTER		20
294 
295 struct iwm_rx_data {
296 	struct mbuf	*m;
297 	bus_dmamap_t	map;
298 };
299 
300 struct iwm_rx_ring {
301 	struct iwm_dma_info	free_desc_dma;
302 	struct iwm_dma_info	used_desc_dma;
303 	struct iwm_dma_info	stat_dma;
304 	struct iwm_dma_info	buf_dma;
305 	void			*desc;
306 	struct iwm_rb_status	*stat;
307 	struct iwm_rx_data	data[512];
308 	bus_dmamap_t		spare_map;	/* for iwm_rx_addbuf() */
309 	bus_dma_tag_t           data_dmat;
310 	int			cur;
311 };
312 
313 #define IWM_CMD_RESP_MAX PAGE_SIZE
314 
315 #define IWM_TE_SESSION_PROTECTION_MAX_TIME_MS 500
316 #define IWM_TE_SESSION_PROTECTION_MIN_TIME_MS 400
317 
318 /*
319  * Command headers are in iwl-trans.h, which is full of all
320  * kinds of other junk, so we just replicate the structures here.
321  * First the software bits:
322  */
323 enum IWM_CMD_MODE {
324 	IWM_CMD_SYNC		= 0,
325 	IWM_CMD_ASYNC		= (1 << 0),
326 	IWM_CMD_WANT_SKB	= (1 << 1),
327 	IWM_CMD_SEND_IN_RFKILL	= (1 << 2),
328 };
329 enum iwm_hcmd_dataflag {
330 	IWM_HCMD_DFL_NOCOPY     = (1 << 0),
331 	IWM_HCMD_DFL_DUP        = (1 << 1),
332 };
333 
334 struct iwm_int_sta {
335 	uint32_t sta_id;
336 	uint32_t tfd_queue_msk;
337 };
338 
339 struct iwm_phy_ctxt {
340 	uint16_t id;
341 	uint16_t color;
342 	uint32_t ref;
343 	struct ieee80211_channel *channel;
344 };
345 
346 struct iwm_bf_data {
347 	int bf_enabled;		/* filtering	*/
348 	int ba_enabled;		/* abort	*/
349 	int ave_beacon_signal;
350 	int last_cqm_event;
351 };
352 
353 struct iwm_vap {
354 	struct ieee80211vap	iv_vap;
355 	int			is_uploaded;
356 	int			iv_auth;
357 
358 	int			(*iv_newstate)(struct ieee80211vap *,
359 				    enum ieee80211_state, int);
360 
361 	struct iwm_phy_ctxt	*phy_ctxt;
362 
363 	uint16_t		id;
364 	uint16_t		color;
365 
366 	boolean_t		have_wme;
367 	/*
368 	 * QoS data from net80211, need to store this here
369 	 * as net80211 has a separate callback but we need
370 	 * to have the data for the MAC context
371 	 */
372         struct {
373 		uint16_t cw_min;
374 		uint16_t cw_max;
375 		uint16_t edca_txop;
376 		uint8_t aifsn;
377 	} queue_params[WME_NUM_AC];
378 
379 	/* indicates that this interface requires PS to be disabled */
380 	boolean_t		ps_disabled;
381 };
382 #define IWM_VAP(_vap)		((struct iwm_vap *)(_vap))
383 
384 struct iwm_node {
385 	struct ieee80211_node	in_ni;
386 
387 	/* status "bits" */
388 	int			in_assoc;
389 
390 	struct iwm_lq_cmd	in_lq;
391 };
392 #define IWM_NODE(_ni)		((struct iwm_node *)(_ni))
393 
394 #define IWM_STATION_ID 0
395 #define IWM_AUX_STA_ID 1
396 
397 #define	IWM_DEFAULT_MACID	0
398 #define	IWM_DEFAULT_COLOR	0
399 #define	IWM_DEFAULT_TSFID	0
400 
401 #define IWM_ICT_SIZE		4096
402 #define IWM_ICT_COUNT		(IWM_ICT_SIZE / sizeof (uint32_t))
403 #define IWM_ICT_PADDR_SHIFT	12
404 
405 struct iwm_cfg;
406 
407 struct iwm_softc {
408 	device_t		sc_dev;
409 	uint32_t		sc_debug;
410 	int			sc_attached;
411 
412 	struct mtx		sc_mtx;
413 	struct mbufq		sc_snd;
414 	struct ieee80211com	sc_ic;
415 	struct ieee80211_ratectl_tx_status sc_txs;
416 
417 	int			sc_flags;
418 #define IWM_FLAG_USE_ICT	(1 << 0)
419 #define IWM_FLAG_HW_INITED	(1 << 1)
420 #define IWM_FLAG_STOPPED	(1 << 2)
421 #define IWM_FLAG_RFKILL		(1 << 3)
422 #define IWM_FLAG_BUSY		(1 << 4)
423 #define IWM_FLAG_SCANNING	(1 << 5)
424 #define IWM_FLAG_SCAN_RUNNING	(1 << 6)
425 #define IWM_FLAG_TE_ACTIVE	(1 << 7)
426 
427 	struct intr_config_hook sc_preinit_hook;
428 	struct callout		sc_watchdog_to;
429 	struct callout		sc_led_blink_to;
430 
431 	struct task		init_task;
432 
433 	struct resource		*sc_irq;
434 	struct resource		*sc_mem;
435 	bus_space_tag_t		sc_st;
436 	bus_space_handle_t	sc_sh;
437 	bus_size_t		sc_sz;
438 	bus_dma_tag_t		sc_dmat;
439 	void			*sc_ih;
440 
441 	/* TX scheduler rings. */
442 	struct iwm_dma_info	sched_dma;
443 	uint32_t		scd_base_addr;
444 
445 	/* TX/RX rings. */
446 	struct iwm_tx_ring	txq[IWM_MAX_QUEUES];
447 	struct iwm_rx_ring	rxq;
448 	int			qfullmsk;
449 
450 	/* ICT table. */
451 	struct iwm_dma_info	ict_dma;
452 	int			ict_cur;
453 
454 	int			sc_hw_rev;
455 	int			sc_hw_id;
456 
457 	struct iwm_dma_info	kw_dma;
458 	struct iwm_dma_info	fw_dma;
459 
460 	int			sc_fw_chunk_done;
461 
462 	enum iwm_ucode_type	cur_ucode;
463 	int			ucode_loaded;
464 	char			sc_fwver[32];
465 
466 	char			sc_fw_mcc[3];
467 
468 	int			sc_intmask;
469 
470 	/*
471 	 * So why do we need a separate stopped flag and a generation?
472 	 * the former protects the device from issuing commands when it's
473 	 * stopped (duh).  The latter protects against race from a very
474 	 * fast stop/unstop cycle where threads waiting for responses do
475 	 * not have a chance to run in between.  Notably: we want to stop
476 	 * the device from interrupt context when it craps out, so we
477 	 * don't have the luxury of waiting for quiescense.
478 	 */
479 	int			sc_generation;
480 
481 	struct iwm_fw_info	sc_fw;
482 	struct iwm_tlv_calib_ctrl sc_default_calib[IWM_UCODE_TYPE_MAX];
483 
484 	const struct iwm_cfg	*cfg;
485 	struct iwm_nvm_data	*nvm_data;
486 	struct iwm_phy_db	*sc_phy_db;
487 
488 	struct iwm_bf_data	sc_bf;
489 
490 	int			sc_tx_timer;
491 
492 	int			sc_scan_last_antenna;
493 
494 	int			sc_fixed_ridx;
495 
496 	int			sc_staid;
497 	int			sc_nodecolor;
498 
499 	uint8_t			sc_cmd_resp[IWM_CMD_RESP_MAX];
500 	int			sc_wantresp;
501 
502 	struct taskqueue	*sc_tq;
503 	struct task		sc_es_task;
504 	struct task		sc_rftoggle_task;
505 
506 	struct iwm_rx_phy_info	sc_last_phy_info;
507 	int			sc_ampdu_ref;
508 
509 	struct iwm_int_sta	sc_aux_sta;
510 
511 	/* phy contexts.  we only use the first one */
512 	struct iwm_phy_ctxt	sc_phyctxt[IWM_NUM_PHY_CTX];
513 
514 	struct iwm_notif_statistics sc_stats;
515 	int			sc_noise;
516 
517 	struct iwm_rx_radiotap_header sc_rxtap;
518 	struct iwm_tx_radiotap_header sc_txtap;
519 
520 	int			sc_max_rssi;
521 
522 	struct iwm_notif_wait_data *sc_notif_wait;
523 
524 	int			cmd_hold_nic_awake;
525 
526 	/* Firmware status */
527 	uint32_t		error_event_table[2];
528 	uint32_t		log_event_table;
529 	uint32_t		umac_error_event_table;
530 	int			support_umac_log;
531 
532 	/*
533 	 * Paging parameters - All of the parameters should be set by the
534 	 * opmode when paging is enabled
535 	 */
536 	struct iwm_fw_paging	fw_paging_db[IWM_NUM_OF_FW_PAGING_BLOCKS];
537 	uint16_t		num_of_paging_blk;
538 	uint16_t		num_of_pages_in_last_blk;
539 
540 	boolean_t		last_ebs_successful;
541 
542 	/* last smart fifo state that was successfully sent to firmware */
543 	enum iwm_sf_state	sf_state;
544 
545 	/* Indicate if device power save is allowed */
546 	boolean_t		sc_ps_disabled;
547 
548 	int			sc_ltr_enabled;
549 
550 	/* Track firmware state for STA association. */
551 	int			sc_firmware_state;
552 
553 	/* Unique ID (assigned by the firmware) of the current Time Event. */
554 	uint32_t		sc_time_event_uid;
555 
556 	/* Duration of the Time Event in TU. */
557 	uint32_t		sc_time_event_duration;
558 
559 	/* Expected end of the Time Event in HZ ticks. */
560 	int			sc_time_event_end_ticks;
561 };
562 
563 #define IWM_LOCK_INIT(_sc) \
564 	mtx_init(&(_sc)->sc_mtx, device_get_nameunit((_sc)->sc_dev), \
565 	    MTX_NETWORK_LOCK, MTX_DEF);
566 #define	IWM_LOCK(_sc)		mtx_lock(&(_sc)->sc_mtx)
567 #define	IWM_UNLOCK(_sc)		mtx_unlock(&(_sc)->sc_mtx)
568 #define IWM_LOCK_DESTROY(_sc)	mtx_destroy(&(_sc)->sc_mtx)
569 
570 static inline bool
571 iwm_fw_has_api(struct iwm_softc *sc, unsigned int api)
572 {
573 	return isset(sc->sc_fw.ucode_capa.enabled_api, api);
574 }
575 
576 static inline bool
577 iwm_fw_has_capa(struct iwm_softc *sc, unsigned int capa)
578 {
579 	return isset(sc->sc_fw.ucode_capa.enabled_capa, capa);
580 }
581