1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * This file is part of wl1271
4  *
5  * Copyright (C) 1998-2009 Texas Instruments. All rights reserved.
6  * Copyright (C) 2008-2009 Nokia Corporation
7  *
8  * Contact: Luciano Coelho <luciano.coelho@nokia.com>
9  */
10 
11 #ifndef __WLCORE_I_H__
12 #define __WLCORE_I_H__
13 
14 #include <linux/mutex.h>
15 #include <linux/completion.h>
16 #include <linux/spinlock.h>
17 #include <linux/list.h>
18 #include <linux/bitops.h>
19 #include <net/mac80211.h>
20 
21 #include "conf.h"
22 #include "ini.h"
23 
24 struct wilink_family_data {
25 	const char *name;
26 	const char *nvs_name;	/* wl12xx nvs file */
27 	const char *cfg_name;	/* wl18xx cfg file */
28 };
29 
30 #define WL1271_TX_SECURITY_LO16(s) ((u16)((s) & 0xffff))
31 #define WL1271_TX_SECURITY_HI32(s) ((u32)(((s) >> 16) & 0xffffffff))
32 #define WL1271_TX_SQN_POST_RECOVERY_PADDING 0xff
33 /* Use smaller padding for GEM, as some  APs have issues when it's too big */
34 #define WL1271_TX_SQN_POST_RECOVERY_PADDING_GEM 0x20
35 
36 
37 #define WL1271_CIPHER_SUITE_GEM 0x00147201
38 
39 #define WL1271_BUSY_WORD_CNT 1
40 #define WL1271_BUSY_WORD_LEN (WL1271_BUSY_WORD_CNT * sizeof(u32))
41 
42 #define WL1271_ELP_HW_STATE_ASLEEP 0
43 #define WL1271_ELP_HW_STATE_IRQ    1
44 
45 #define WL1271_DEFAULT_BEACON_INT  100
46 #define WL1271_DEFAULT_DTIM_PERIOD 1
47 
48 #define WL12XX_MAX_ROLES           4
49 #define WL12XX_INVALID_ROLE_ID     0xff
50 #define WL12XX_INVALID_LINK_ID     0xff
51 
52 /*
53  * max number of links allowed by all HWs.
54  * this is NOT the actual max links supported by the current hw.
55  */
56 #define WLCORE_MAX_LINKS 16
57 
58 /* the driver supports the 2.4Ghz and 5Ghz bands */
59 #define WLCORE_NUM_BANDS           2
60 
61 #define WL12XX_MAX_RATE_POLICIES 16
62 #define WLCORE_MAX_KLV_TEMPLATES 4
63 
64 /* Defined by FW as 0. Will not be freed or allocated. */
65 #define WL12XX_SYSTEM_HLID         0
66 
67 /*
68  * When in AP-mode, we allow (at least) this number of packets
69  * to be transmitted to FW for a STA in PS-mode. Only when packets are
70  * present in the FW buffers it will wake the sleeping STA. We want to put
71  * enough packets for the driver to transmit all of its buffered data before
72  * the STA goes to sleep again. But we don't want to take too much memory
73  * as it might hurt the throughput of active STAs.
74  */
75 #define WL1271_PS_STA_MAX_PACKETS  2
76 
77 #define WL1271_AP_BSS_INDEX        0
78 #define WL1271_AP_DEF_BEACON_EXP   20
79 
80 enum wlcore_state {
81 	WLCORE_STATE_OFF,
82 	WLCORE_STATE_RESTARTING,
83 	WLCORE_STATE_ON,
84 };
85 
86 enum wl12xx_fw_type {
87 	WL12XX_FW_TYPE_NONE,
88 	WL12XX_FW_TYPE_NORMAL,
89 	WL12XX_FW_TYPE_MULTI,
90 	WL12XX_FW_TYPE_PLT,
91 };
92 
93 struct wl1271;
94 
95 enum {
96 	FW_VER_CHIP,
97 	FW_VER_IF_TYPE,
98 	FW_VER_MAJOR,
99 	FW_VER_SUBTYPE,
100 	FW_VER_MINOR,
101 
102 	NUM_FW_VER
103 };
104 
105 struct wl1271_chip {
106 	u32 id;
107 	char fw_ver_str[ETHTOOL_FWVERS_LEN];
108 	unsigned int fw_ver[NUM_FW_VER];
109 	char phy_fw_ver_str[ETHTOOL_FWVERS_LEN];
110 };
111 
112 #define NUM_TX_QUEUES              4
113 
114 struct wl_fw_status {
115 	u32 intr;
116 	u8  fw_rx_counter;
117 	u8  drv_rx_counter;
118 	u8  tx_results_counter;
119 	__le32 *rx_pkt_descs;
120 
121 	u32 fw_localtime;
122 
123 	/*
124 	 * A bitmap (where each bit represents a single HLID)
125 	 * to indicate if the station is in PS mode.
126 	 */
127 	u32 link_ps_bitmap;
128 
129 	/*
130 	 * A bitmap (where each bit represents a single HLID) to indicate
131 	 * if the station is in Fast mode
132 	 */
133 	u32 link_fast_bitmap;
134 
135 	/* Cumulative counter of total released mem blocks since FW-reset */
136 	u32 total_released_blks;
137 
138 	/* Size (in Memory Blocks) of TX pool */
139 	u32 tx_total;
140 
141 	struct {
142 		/*
143 		 * Cumulative counter of released packets per AC
144 		 * (length of the array is NUM_TX_QUEUES)
145 		 */
146 		u8 *tx_released_pkts;
147 
148 		/*
149 		 * Cumulative counter of freed packets per HLID
150 		 * (length of the array is wl->num_links)
151 		 */
152 		u8 *tx_lnk_free_pkts;
153 
154 		/* PN16 of last TKIP/AES seq-num per HLID */
155 		__le16 *tx_lnk_sec_pn16;
156 
157 		/* Cumulative counter of released Voice memory blocks */
158 		u8 tx_voice_released_blks;
159 
160 		/* Tx rate of the last transmitted packet */
161 		u8 tx_last_rate;
162 
163 		/* Tx rate or Tx rate estimate pre calculated by fw in mbps */
164 		u8 tx_last_rate_mbps;
165 
166 		/* hlid for which the rates were reported */
167 		u8 hlid;
168 	} counters;
169 
170 	u32 log_start_addr;
171 
172 	/* Private status to be used by the lower drivers */
173 	void *priv;
174 };
175 
176 #define WL1271_MAX_CHANNELS 64
177 struct wl1271_scan {
178 	struct cfg80211_scan_request *req;
179 	unsigned long scanned_ch[BITS_TO_LONGS(WL1271_MAX_CHANNELS)];
180 	bool failed;
181 	u8 state;
182 	u8 ssid[IEEE80211_MAX_SSID_LEN+1];
183 	size_t ssid_len;
184 };
185 
186 struct wl1271_if_operations {
187 	int __must_check (*read)(struct device *child, int addr, void *buf,
188 				 size_t len, bool fixed);
189 	int __must_check (*write)(struct device *child, int addr, void *buf,
190 				  size_t len, bool fixed);
191 	void (*reset)(struct device *child);
192 	void (*init)(struct device *child);
193 	int (*power)(struct device *child, bool enable);
194 	void (*set_block_size) (struct device *child, unsigned int blksz);
195 };
196 
197 struct wlcore_platdev_data {
198 	struct wl1271_if_operations *if_ops;
199 	const struct wilink_family_data *family;
200 
201 	bool ref_clock_xtal;	/* specify whether the clock is XTAL or not */
202 	u32 ref_clock_freq;	/* in Hertz */
203 	u32 tcxo_clock_freq;	/* in Hertz, tcxo is always XTAL */
204 	bool pwr_in_suspend;
205 };
206 
207 #define MAX_NUM_KEYS 14
208 #define MAX_KEY_SIZE 32
209 
210 struct wl1271_ap_key {
211 	u8 id;
212 	u8 key_type;
213 	u8 key_size;
214 	u8 key[MAX_KEY_SIZE];
215 	u8 hlid;
216 	u32 tx_seq_32;
217 	u16 tx_seq_16;
218 	bool is_pairwise;
219 };
220 
221 enum wl12xx_flags {
222 	WL1271_FLAG_GPIO_POWER,
223 	WL1271_FLAG_TX_QUEUE_STOPPED,
224 	WL1271_FLAG_TX_PENDING,
225 	WL1271_FLAG_IN_ELP,
226 	WL1271_FLAG_IRQ_RUNNING,
227 	WL1271_FLAG_FW_TX_BUSY,
228 	WL1271_FLAG_DUMMY_PACKET_PENDING,
229 	WL1271_FLAG_SUSPENDED,
230 	WL1271_FLAG_PENDING_WORK,
231 	WL1271_FLAG_SOFT_GEMINI,
232 	WL1271_FLAG_RECOVERY_IN_PROGRESS,
233 	WL1271_FLAG_VIF_CHANGE_IN_PROGRESS,
234 	WL1271_FLAG_INTENDED_FW_RECOVERY,
235 	WL1271_FLAG_IO_FAILED,
236 	WL1271_FLAG_REINIT_TX_WDOG,
237 };
238 
239 enum wl12xx_vif_flags {
240 	WLVIF_FLAG_INITIALIZED,
241 	WLVIF_FLAG_STA_ASSOCIATED,
242 	WLVIF_FLAG_STA_AUTHORIZED,
243 	WLVIF_FLAG_IBSS_JOINED,
244 	WLVIF_FLAG_AP_STARTED,
245 	WLVIF_FLAG_IN_PS,
246 	WLVIF_FLAG_STA_STATE_SENT,
247 	WLVIF_FLAG_RX_STREAMING_STARTED,
248 	WLVIF_FLAG_PSPOLL_FAILURE,
249 	WLVIF_FLAG_CS_PROGRESS,
250 	WLVIF_FLAG_AP_PROBE_RESP_SET,
251 	WLVIF_FLAG_IN_USE,
252 	WLVIF_FLAG_ACTIVE,
253 	WLVIF_FLAG_BEACON_DISABLED,
254 };
255 
256 struct wl12xx_vif;
257 
258 struct wl1271_link {
259 	/* AP-mode - TX queue per AC in link */
260 	struct sk_buff_head tx_queue[NUM_TX_QUEUES];
261 
262 	/* accounting for allocated / freed packets in FW */
263 	u8 allocated_pkts;
264 	u8 prev_freed_pkts;
265 	u16 prev_sec_pn16;
266 
267 	u8 addr[ETH_ALEN];
268 
269 	/* bitmap of TIDs where RX BA sessions are active for this link */
270 	u8 ba_bitmap;
271 
272 	/* the last fw rate index we used for this link */
273 	u8 fw_rate_idx;
274 
275 	/* the last fw rate [Mbps] we used for this link */
276 	u8 fw_rate_mbps;
277 
278 	/* The wlvif this link belongs to. Might be null for global links */
279 	struct wl12xx_vif *wlvif;
280 
281 	/*
282 	 * total freed FW packets on the link - used for tracking the
283 	 * AES/TKIP PN across recoveries. Re-initialized each time
284 	 * from the wl1271_station structure.
285 	 */
286 	u64 total_freed_pkts;
287 };
288 
289 #define WL1271_MAX_RX_FILTERS 5
290 #define WL1271_RX_FILTER_MAX_FIELDS 8
291 
292 #define WL1271_RX_FILTER_ETH_HEADER_SIZE 14
293 #define WL1271_RX_FILTER_MAX_FIELDS_SIZE 95
294 #define RX_FILTER_FIELD_OVERHEAD				\
295 	(sizeof(struct wl12xx_rx_filter_field) - sizeof(u8 *))
296 #define WL1271_RX_FILTER_MAX_PATTERN_SIZE			\
297 	(WL1271_RX_FILTER_MAX_FIELDS_SIZE - RX_FILTER_FIELD_OVERHEAD)
298 
299 #define WL1271_RX_FILTER_FLAG_MASK                BIT(0)
300 #define WL1271_RX_FILTER_FLAG_IP_HEADER           0
301 #define WL1271_RX_FILTER_FLAG_ETHERNET_HEADER     BIT(1)
302 
303 enum rx_filter_action {
304 	FILTER_DROP = 0,
305 	FILTER_SIGNAL = 1,
306 	FILTER_FW_HANDLE = 2
307 };
308 
309 enum plt_mode {
310 	PLT_OFF = 0,
311 	PLT_ON = 1,
312 	PLT_FEM_DETECT = 2,
313 	PLT_CHIP_AWAKE = 3
314 };
315 
316 struct wl12xx_rx_filter_field {
317 	__le16 offset;
318 	u8 len;
319 	u8 flags;
320 	u8 *pattern;
321 } __packed;
322 
323 struct wl12xx_rx_filter {
324 	u8 action;
325 	int num_fields;
326 	struct wl12xx_rx_filter_field fields[WL1271_RX_FILTER_MAX_FIELDS];
327 };
328 
329 struct wl1271_station {
330 	u8 hlid;
331 	bool fw_added;
332 	bool in_connection;
333 
334 	/*
335 	 * total freed FW packets on the link to the STA - used for tracking the
336 	 * AES/TKIP PN across recoveries. Re-initialized each time from the
337 	 * wl1271_station structure.
338 	 * Used in both AP and STA mode.
339 	 */
340 	u64 total_freed_pkts;
341 };
342 
wl1271_station(struct ieee80211_sta * sta)343 static inline struct wl1271_station *wl1271_station(struct ieee80211_sta *sta)
344 {
345 	return (struct wl1271_station *)sta->drv_priv;
346 }
347 
348 struct wl12xx_vif {
349 	struct wl1271 *wl;
350 	struct list_head list;
351 	unsigned long flags;
352 	u8 bss_type;
353 	u8 p2p; /* we are using p2p role */
354 	u8 role_id;
355 
356 	/* sta/ibss specific */
357 	u8 dev_role_id;
358 	u8 dev_hlid;
359 
360 	union {
361 		struct {
362 			u8 hlid;
363 
364 			u8 basic_rate_idx;
365 			u8 ap_rate_idx;
366 			u8 p2p_rate_idx;
367 
368 			u8 klv_template_id;
369 
370 			bool qos;
371 			/* channel type we started the STA role with */
372 			enum nl80211_channel_type role_chan_type;
373 		} sta;
374 		struct {
375 			u8 global_hlid;
376 			u8 bcast_hlid;
377 
378 			/* HLIDs bitmap of associated stations */
379 			unsigned long sta_hlid_map[BITS_TO_LONGS(
380 							WLCORE_MAX_LINKS)];
381 
382 			/* recoreded keys - set here before AP startup */
383 			struct wl1271_ap_key *recorded_keys[MAX_NUM_KEYS];
384 
385 			u8 mgmt_rate_idx;
386 			u8 bcast_rate_idx;
387 			u8 ucast_rate_idx[CONF_TX_MAX_AC_COUNT];
388 		} ap;
389 	};
390 
391 	/* the hlid of the last transmitted skb */
392 	int last_tx_hlid;
393 
394 	/* counters of packets per AC, across all links in the vif */
395 	int tx_queue_count[NUM_TX_QUEUES];
396 
397 	unsigned long links_map[BITS_TO_LONGS(WLCORE_MAX_LINKS)];
398 
399 	u8 ssid[IEEE80211_MAX_SSID_LEN + 1];
400 	u8 ssid_len;
401 
402 	/* The current band */
403 	enum nl80211_band band;
404 	int channel;
405 	enum nl80211_channel_type channel_type;
406 
407 	u32 bitrate_masks[WLCORE_NUM_BANDS];
408 	u32 basic_rate_set;
409 
410 	/*
411 	 * currently configured rate set:
412 	 *	bits  0-15 - 802.11abg rates
413 	 *	bits 16-23 - 802.11n   MCS index mask
414 	 * support only 1 stream, thus only 8 bits for the MCS rates (0-7).
415 	 */
416 	u32 basic_rate;
417 	u32 rate_set;
418 
419 	/* probe-req template for the current AP */
420 	struct sk_buff *probereq;
421 
422 	/* Beaconing interval (needed for ad-hoc) */
423 	u32 beacon_int;
424 
425 	/* Default key (for WEP) */
426 	u32 default_key;
427 
428 	/* Our association ID */
429 	u16 aid;
430 
431 	/* retry counter for PSM entries */
432 	u8 psm_entry_retry;
433 
434 	/* in dBm */
435 	int power_level;
436 
437 	int rssi_thold;
438 	int last_rssi_event;
439 
440 	/* save the current encryption type for auto-arp config */
441 	u8 encryption_type;
442 	__be32 ip_addr;
443 
444 	/* RX BA constraint value */
445 	bool ba_support;
446 	bool ba_allowed;
447 
448 	bool wmm_enabled;
449 
450 	bool radar_enabled;
451 
452 	/* Rx Streaming */
453 	struct work_struct rx_streaming_enable_work;
454 	struct work_struct rx_streaming_disable_work;
455 	struct timer_list rx_streaming_timer;
456 
457 	struct delayed_work channel_switch_work;
458 	struct delayed_work connection_loss_work;
459 
460 	/* number of in connection stations */
461 	int inconn_count;
462 
463 	/*
464 	 * This vif's queues are mapped to mac80211 HW queues as:
465 	 * VO - hw_queue_base
466 	 * VI - hw_queue_base + 1
467 	 * BE - hw_queue_base + 2
468 	 * BK - hw_queue_base + 3
469 	 */
470 	int hw_queue_base;
471 
472 	/* do we have a pending auth reply? (and ROC) */
473 	bool ap_pending_auth_reply;
474 
475 	/* time when we sent the pending auth reply */
476 	unsigned long pending_auth_reply_time;
477 
478 	/* work for canceling ROC after pending auth reply */
479 	struct delayed_work pending_auth_complete_work;
480 
481 	/* update rate conrol */
482 	enum ieee80211_sta_rx_bandwidth rc_update_bw;
483 	struct ieee80211_sta_ht_cap rc_ht_cap;
484 	struct work_struct rc_update_work;
485 
486 	/*
487 	 * total freed FW packets on the link.
488 	 * For STA this holds the PN of the link to the AP.
489 	 * For AP this holds the PN of the broadcast link.
490 	 */
491 	u64 total_freed_pkts;
492 
493 	/*
494 	 * This struct must be last!
495 	 * data that has to be saved acrossed reconfigs (e.g. recovery)
496 	 * should be declared in this struct.
497 	 */
498 	struct {
499 		u8 persistent[0];
500 	};
501 };
502 
wl12xx_vif_to_data(struct ieee80211_vif * vif)503 static inline struct wl12xx_vif *wl12xx_vif_to_data(struct ieee80211_vif *vif)
504 {
505 	WARN_ON(!vif);
506 	return (struct wl12xx_vif *)vif->drv_priv;
507 }
508 
509 static inline
wl12xx_wlvif_to_vif(struct wl12xx_vif * wlvif)510 struct ieee80211_vif *wl12xx_wlvif_to_vif(struct wl12xx_vif *wlvif)
511 {
512 	return container_of((void *)wlvif, struct ieee80211_vif, drv_priv);
513 }
514 
wlcore_is_p2p_mgmt(struct wl12xx_vif * wlvif)515 static inline bool wlcore_is_p2p_mgmt(struct wl12xx_vif *wlvif)
516 {
517 	return wl12xx_wlvif_to_vif(wlvif)->type == NL80211_IFTYPE_P2P_DEVICE;
518 }
519 
520 #define wl12xx_for_each_wlvif(wl, wlvif) \
521 		list_for_each_entry(wlvif, &wl->wlvif_list, list)
522 
523 #define wl12xx_for_each_wlvif_continue(wl, wlvif) \
524 		list_for_each_entry_continue(wlvif, &wl->wlvif_list, list)
525 
526 #define wl12xx_for_each_wlvif_bss_type(wl, wlvif, _bss_type)	\
527 		wl12xx_for_each_wlvif(wl, wlvif)		\
528 			if (wlvif->bss_type == _bss_type)
529 
530 #define wl12xx_for_each_wlvif_sta(wl, wlvif)	\
531 		wl12xx_for_each_wlvif_bss_type(wl, wlvif, BSS_TYPE_STA_BSS)
532 
533 #define wl12xx_for_each_wlvif_ap(wl, wlvif)	\
534 		wl12xx_for_each_wlvif_bss_type(wl, wlvif, BSS_TYPE_AP_BSS)
535 
536 int wl1271_plt_start(struct wl1271 *wl, const enum plt_mode plt_mode);
537 int wl1271_plt_stop(struct wl1271 *wl);
538 int wl1271_recalc_rx_streaming(struct wl1271 *wl, struct wl12xx_vif *wlvif);
539 void wl12xx_queue_recovery_work(struct wl1271 *wl);
540 size_t wl12xx_copy_fwlog(struct wl1271 *wl, u8 *memblock, size_t maxlen);
541 int wl1271_rx_filter_alloc_field(struct wl12xx_rx_filter *filter,
542 				 u16 offset, u8 flags,
543 				 const u8 *pattern, u8 len);
544 void wl1271_rx_filter_free(struct wl12xx_rx_filter *filter);
545 struct wl12xx_rx_filter *wl1271_rx_filter_alloc(void);
546 int wl1271_rx_filter_get_fields_size(struct wl12xx_rx_filter *filter);
547 void wl1271_rx_filter_flatten_fields(struct wl12xx_rx_filter *filter,
548 				     u8 *buf);
549 
550 #define JOIN_TIMEOUT 5000 /* 5000 milliseconds to join */
551 
552 #define SESSION_COUNTER_MAX 6 /* maximum value for the session counter */
553 #define SESSION_COUNTER_INVALID 7 /* used with dummy_packet */
554 
555 #define WL1271_DEFAULT_POWER_LEVEL 0
556 
557 #define WL1271_TX_QUEUE_LOW_WATERMARK  32
558 #define WL1271_TX_QUEUE_HIGH_WATERMARK 256
559 
560 #define WL1271_DEFERRED_QUEUE_LIMIT    64
561 
562 /* WL1271 needs a 200ms sleep after power on, and a 20ms sleep before power
563    on in case is has been shut down shortly before */
564 #define WL1271_PRE_POWER_ON_SLEEP 20 /* in milliseconds */
565 #define WL1271_POWER_ON_SLEEP 200 /* in milliseconds */
566 
567 /* Macros to handle wl1271.sta_rate_set */
568 #define HW_BG_RATES_MASK	0xffff
569 #define HW_HT_RATES_OFFSET	16
570 #define HW_MIMO_RATES_OFFSET	24
571 
572 #endif /* __WLCORE_I_H__ */
573