1/*	$FreeBSD: releng/12.0/sys/dev/iwn/if_iwnvar.h 314923 2017-03-08 22:49:22Z avos $	*/
2/*	$OpenBSD: if_iwnvar.h,v 1.18 2010/04/30 16:06:46 damien Exp $	*/
3
4/*-
5 * Copyright (c) 2013 Cedric GROSS <cg@cgross.info>
6 * Copyright (c) 2011 Intel Corporation
7 * Copyright (c) 2007, 2008
8 *	Damien Bergamini <damien.bergamini@free.fr>
9 * Copyright (c) 2008 Sam Leffler, Errno Consulting
10 *
11 * Permission to use, copy, modify, and distribute this software for any
12 * purpose with or without fee is hereby granted, provided that the above
13 * copyright notice and this permission notice appear in all copies.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
16 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
18 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 */
23enum iwn_rxon_ctx_id {
24		IWN_RXON_BSS_CTX,
25		IWN_RXON_PAN_CTX,
26		IWN_NUM_RXON_CTX
27};
28
29struct iwn_pan_slot {
30	uint16_t	time;
31	uint8_t		type;
32	uint8_t		reserved;
33} __packed;
34
35struct iwn_pan_params_cmd {
36	uint16_t flags;
37#define	IWN_PAN_PARAMS_FLG_SLOTTED_MODE	(1 << 3)
38
39	uint8_t reserved;
40	uint8_t num_slots;
41	struct iwn_pan_slot slots[10];
42} __packed;
43
44struct iwn_led_mode
45{
46	uint8_t		led_cur_mode;
47	uint64_t	led_cur_bt;
48	uint64_t	led_last_bt;
49	uint64_t	led_cur_tpt;
50	uint64_t	led_last_tpt;
51	uint64_t	led_bt_diff;
52	int		led_cur_time;
53	int		led_last_time;
54};
55
56struct iwn_rx_radiotap_header {
57	struct ieee80211_radiotap_header wr_ihdr;
58	uint64_t	wr_tsft;
59	uint8_t		wr_flags;
60	uint8_t		wr_rate;
61	uint16_t	wr_chan_freq;
62	uint16_t	wr_chan_flags;
63	int8_t		wr_dbm_antsignal;
64	int8_t		wr_dbm_antnoise;
65} __packed __aligned(8);
66
67#define IWN_RX_RADIOTAP_PRESENT						\
68	((1 << IEEE80211_RADIOTAP_TSFT) |				\
69	 (1 << IEEE80211_RADIOTAP_FLAGS) |				\
70	 (1 << IEEE80211_RADIOTAP_RATE) |				\
71	 (1 << IEEE80211_RADIOTAP_CHANNEL) |				\
72	 (1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL) |			\
73	 (1 << IEEE80211_RADIOTAP_DBM_ANTNOISE))
74
75struct iwn_tx_radiotap_header {
76	struct ieee80211_radiotap_header wt_ihdr;
77	uint8_t		wt_flags;
78	uint8_t		wt_rate;
79	uint16_t	wt_chan_freq;
80	uint16_t	wt_chan_flags;
81} __packed;
82
83#define IWN_TX_RADIOTAP_PRESENT						\
84	((1 << IEEE80211_RADIOTAP_FLAGS) |				\
85	 (1 << IEEE80211_RADIOTAP_RATE) |				\
86	 (1 << IEEE80211_RADIOTAP_CHANNEL))
87
88struct iwn_dma_info {
89	bus_dma_tag_t		tag;
90	bus_dmamap_t		map;
91	bus_dma_segment_t	seg;
92	bus_addr_t		paddr;
93	caddr_t			vaddr;
94	bus_size_t		size;
95};
96
97struct iwn_tx_data {
98	bus_dmamap_t		map;
99	bus_addr_t		cmd_paddr;
100	bus_addr_t		scratch_paddr;
101	struct mbuf		*m;
102	struct ieee80211_node	*ni;
103	unsigned int		remapped:1;
104	unsigned int		long_retries:7;
105#define IWN_LONG_RETRY_FW_OVERFLOW	0x10
106#define IWN_LONG_RETRY_LIMIT_LOG	7
107#define IWN_LONG_RETRY_LIMIT		((1 << IWN_LONG_RETRY_LIMIT_LOG) - 3)
108};
109
110struct iwn_tx_ring {
111	struct iwn_dma_info	desc_dma;
112	struct iwn_dma_info	cmd_dma;
113	struct iwn_tx_desc	*desc;
114	struct iwn_tx_cmd	*cmd;
115	struct iwn_tx_data	data[IWN_TX_RING_COUNT];
116	bus_dma_tag_t		data_dmat;
117	int			qid;
118	int			queued;
119	int			cur;
120	int			read;
121};
122
123struct iwn_softc;
124
125struct iwn_rx_data {
126	struct mbuf	*m;
127	bus_dmamap_t	map;
128};
129
130struct iwn_rx_ring {
131	struct iwn_dma_info	desc_dma;
132	struct iwn_dma_info	stat_dma;
133	uint32_t		*desc;
134	struct iwn_rx_status	*stat;
135	struct iwn_rx_data	data[IWN_RX_RING_COUNT];
136	bus_dma_tag_t		data_dmat;
137	int			cur;
138};
139
140struct iwn_node {
141	struct	ieee80211_node		ni;	/* must be the first */
142	uint16_t			disable_tid;
143	uint8_t				id;
144	struct {
145		uint64_t		bitmap;
146		int			short_retries;
147		int			startidx;
148	} agg[IEEE80211_TID_SIZE];
149};
150
151struct iwn_calib_state {
152	uint8_t		state;
153#define IWN_CALIB_STATE_INIT	0
154#define IWN_CALIB_STATE_ASSOC	1
155#define IWN_CALIB_STATE_RUN	2
156
157	u_int		nbeacons;
158	uint32_t	noise[3];
159	uint32_t	rssi[3];
160	uint32_t	ofdm_x1;
161	uint32_t	ofdm_mrc_x1;
162	uint32_t	ofdm_x4;
163	uint32_t	ofdm_mrc_x4;
164	uint32_t	cck_x4;
165	uint32_t	cck_mrc_x4;
166	uint32_t	bad_plcp_ofdm;
167	uint32_t	fa_ofdm;
168	uint32_t	bad_plcp_cck;
169	uint32_t	fa_cck;
170	uint32_t	low_fa;
171	uint32_t	bad_plcp_ht;
172	uint8_t		cck_state;
173#define IWN_CCK_STATE_INIT	0
174#define IWN_CCK_STATE_LOFA	1
175#define IWN_CCK_STATE_HIFA	2
176
177	uint8_t		noise_samples[20];
178	u_int		cur_noise_sample;
179	uint8_t		noise_ref;
180	uint32_t	energy_samples[10];
181	u_int		cur_energy_sample;
182	uint32_t	energy_cck;
183};
184
185struct iwn_calib_info {
186	uint8_t		*buf;
187	u_int		len;
188};
189
190struct iwn_fw_part {
191	const uint8_t	*text;
192	uint32_t	textsz;
193	const uint8_t	*data;
194	uint32_t	datasz;
195};
196
197struct iwn_fw_info {
198	const uint8_t		*data;
199	size_t			size;
200	struct iwn_fw_part	init;
201	struct iwn_fw_part	main;
202	struct iwn_fw_part	boot;
203};
204
205struct iwn_ops {
206	int		(*load_firmware)(struct iwn_softc *);
207	void		(*read_eeprom)(struct iwn_softc *);
208	int		(*post_alive)(struct iwn_softc *);
209	int		(*nic_config)(struct iwn_softc *);
210	void		(*update_sched)(struct iwn_softc *, int, int, uint8_t,
211			    uint16_t);
212	int		(*get_temperature)(struct iwn_softc *);
213	int		(*get_rssi)(struct iwn_softc *, struct iwn_rx_stat *);
214	int		(*set_txpower)(struct iwn_softc *, int);
215	int		(*init_gains)(struct iwn_softc *);
216	int		(*set_gains)(struct iwn_softc *);
217	int		(*rxon_assoc)(struct iwn_softc *, int);
218	int		(*add_node)(struct iwn_softc *, struct iwn_node_info *,
219			    int);
220	void		(*tx_done)(struct iwn_softc *, struct iwn_rx_desc *,
221			    struct iwn_rx_data *);
222	void		(*ampdu_tx_start)(struct iwn_softc *,
223			    struct ieee80211_node *, int, uint8_t, uint16_t);
224	void		(*ampdu_tx_stop)(struct iwn_softc *, int, uint8_t,
225			    uint16_t);
226};
227
228struct iwn_vap {
229	struct ieee80211vap	iv_vap;
230	uint8_t			iv_ridx;
231
232	int			(*iv_newstate)(struct ieee80211vap *,
233				    enum ieee80211_state, int);
234	int			ctx;
235	int			beacon_int;
236
237};
238#define	IWN_VAP(_vap)	((struct iwn_vap *)(_vap))
239
240struct iwn_softc {
241	device_t		sc_dev;
242	int			sc_debug;
243	struct cdev		*sc_cdev;
244	struct mtx		sc_mtx;
245	struct ieee80211com	sc_ic;
246	struct ieee80211_ratectl_tx_status sc_txs;
247
248	u_int			sc_flags;
249#define IWN_FLAG_HAS_OTPROM	(1 << 1)
250#define IWN_FLAG_CALIB_DONE	(1 << 2)
251#define IWN_FLAG_USE_ICT	(1 << 3)
252#define IWN_FLAG_INTERNAL_PA	(1 << 4)
253#define IWN_FLAG_HAS_11N	(1 << 6)
254#define IWN_FLAG_ENH_SENS	(1 << 7)
255#define IWN_FLAG_ADV_BTCOEX	(1 << 8)
256#define IWN_FLAG_PAN_SUPPORT	(1 << 9)
257#define IWN_FLAG_BTCOEX		(1 << 10)
258#define	IWN_FLAG_RUNNING	(1 << 11)
259
260	uint8_t 		hw_type;
261	/* subdevice_id used to adjust configuration */
262	uint16_t		subdevice_id;
263
264	struct iwn_ops		ops;
265	const char		*fwname;
266	const struct iwn_sensitivity_limits
267				*limits;
268	int			ntxqs;
269	int			firstaggqueue;
270	int			ndmachnls;
271	uint8_t			broadcast_id;
272	int			rxonsz;
273	int			schedsz;
274	uint32_t		fw_text_maxsz;
275	uint32_t		fw_data_maxsz;
276	uint32_t		fwsz;
277	bus_size_t		sched_txfact_addr;
278	uint32_t		reset_noise_gain;
279	uint32_t		noise_gain;
280
281	/* TX scheduler rings. */
282	struct iwn_dma_info	sched_dma;
283	uint16_t		*sched;
284	uint32_t		sched_base;
285
286	/* "Keep Warm" page. */
287	struct iwn_dma_info	kw_dma;
288
289	/* Firmware image. */
290	const struct firmware	*fw_fp;
291
292	/* Firmware DMA transfer. */
293	struct iwn_dma_info	fw_dma;
294
295	/* ICT table. */
296	struct iwn_dma_info	ict_dma;
297	uint32_t		*ict;
298	int			ict_cur;
299
300	/* TX/RX rings. */
301	struct iwn_tx_ring	txq[IWN5000_NTXQUEUES];
302	struct iwn_rx_ring	rxq;
303
304	struct resource		*mem;
305	bus_space_tag_t		sc_st;
306	bus_space_handle_t	sc_sh;
307	struct resource		*irq;
308	void 			*sc_ih;
309	bus_size_t		sc_sz;
310	int			sc_cap_off;	/* PCIe Capabilities. */
311
312	/* Tasks used by the driver */
313	struct task		sc_rftoggle_task;
314	struct task		sc_panic_task;
315	struct task		sc_xmit_task;
316
317	/* Taskqueue */
318	struct taskqueue	*sc_tq;
319
320	/* Calibration information */
321	struct callout		calib_to;
322	int			calib_cnt;
323	struct iwn_calib_state	calib;
324	int			last_calib_ticks;
325	struct callout		scan_timeout;
326	struct callout		watchdog_to;
327	struct iwn_fw_info	fw;
328	struct iwn_calib_info	calibcmd[IWN5000_PHY_CALIB_MAX_RESULT];
329	uint32_t		errptr;
330
331	struct iwn_rx_stat	last_rx_stat;
332	int			last_rx_valid;
333	struct iwn_ucode_info	ucode_info;
334	struct iwn_rxon		rx_on[IWN_NUM_RXON_CTX];
335	struct iwn_rxon		*rxon;
336	int			ctx;
337	struct ieee80211vap	*ivap[IWN_NUM_RXON_CTX];
338
339	/* General statistics */
340	/*
341	 * The statistics are reset after each channel
342	 * change.  So it may be zeroed after things like
343	 * a background scan.
344	 *
345	 * So for now, this is just a cheap hack to
346	 * expose the last received statistics dump
347	 * via an ioctl().  Later versions of this
348	 * could expose the last 'n' messages, or just
349	 * provide a pipeline for the firmware responses
350	 * via something like BPF.
351	 */
352	struct iwn_stats	last_stat;
353	int			last_stat_valid;
354
355	uint8_t			uc_scan_progress;
356	uint32_t		rawtemp;
357	int			temp;
358	int			noise;
359	uint32_t		qfullmsk;
360
361	uint32_t		prom_base;
362	struct iwn4965_eeprom_band
363				bands[IWN_NBANDS];
364	struct iwn_eeprom_chan	eeprom_channels[IWN_NBANDS][IWN_MAX_CHAN_PER_BAND];
365	uint16_t		rfcfg;
366	uint8_t			calib_ver;
367	char			eeprom_domain[4];
368	uint32_t		eeprom_crystal;
369	int16_t			eeprom_temp;
370	int16_t			eeprom_temp_high;
371	int16_t			eeprom_voltage;
372	int8_t			maxpwr2GHz;
373	int8_t			maxpwr5GHz;
374	int8_t			maxpwr[IEEE80211_CHAN_MAX];
375
376	uint32_t		tlv_feature_flags;
377
378	int32_t			temp_off;
379	uint32_t		int_mask;
380	uint8_t			ntxchains;
381	uint8_t			nrxchains;
382	uint8_t			txchainmask;
383	uint8_t			rxchainmask;
384	uint8_t			chainmask;
385
386	int			sc_tx_timer;
387
388	/* Are we doing a scan? */
389	int			sc_is_scanning;
390
391	/* Are we waiting for a beacon before xmit? */
392	int			sc_beacon_wait;
393
394	struct ieee80211_tx_ampdu *qid2tap[IWN5000_NTXQUEUES];
395
396	int			(*sc_ampdu_rx_start)(struct ieee80211_node *,
397				    struct ieee80211_rx_ampdu *, int, int, int);
398	void			(*sc_ampdu_rx_stop)(struct ieee80211_node *,
399				    struct ieee80211_rx_ampdu *);
400	int			(*sc_addba_request)(struct ieee80211_node *,
401				    struct ieee80211_tx_ampdu *, int, int, int);
402	int			(*sc_addba_response)(struct ieee80211_node *,
403				    struct ieee80211_tx_ampdu *, int, int, int);
404	void			(*sc_addba_stop)(struct ieee80211_node *,
405				    struct ieee80211_tx_ampdu *);
406
407	struct	iwn_led_mode sc_led;
408
409	struct iwn_rx_radiotap_header sc_rxtap;
410	struct iwn_tx_radiotap_header sc_txtap;
411
412	/* The power save level originally configured by user */
413	int			desired_pwrsave_level;
414
415	/*
416	 * The current power save level, this may differ from the
417	 * configured value due to thermal throttling etc.
418	 */
419	int			current_pwrsave_level;
420
421	/* For specific params */
422	const struct iwn_base_params *base_params;
423
424#define	IWN_UCODE_API(ver)	(((ver) & 0x0000FF00) >> 8)
425	uint32_t		ucode_rev;
426
427	/*
428	 * Global queue for queuing xmit frames
429	 * when we can't yet transmit (eg raw
430	 * frames whilst waiting for beacons.)
431	 */
432	struct mbufq		sc_xmit_queue;
433
434#if defined(__HAIKU__)
435	uint32_t sc_intr_status_1;
436	uint32_t sc_intr_status_2;
437#endif
438};
439
440#define IWN_LOCK_INIT(_sc) \
441	mtx_init(&(_sc)->sc_mtx, device_get_nameunit((_sc)->sc_dev), \
442	    MTX_NETWORK_LOCK, MTX_DEF)
443#define IWN_LOCK(_sc)			mtx_lock(&(_sc)->sc_mtx)
444#define IWN_LOCK_ASSERT(_sc)		mtx_assert(&(_sc)->sc_mtx, MA_OWNED)
445#define IWN_UNLOCK(_sc)			mtx_unlock(&(_sc)->sc_mtx)
446#define IWN_LOCK_DESTROY(_sc)		mtx_destroy(&(_sc)->sc_mtx)
447