1 /*
2  * This file is part of wl1271
3  *
4  * Copyright (C) 1998-2009 Texas Instruments. All rights reserved.
5  * Copyright (C) 2008-2009 Nokia Corporation
6  *
7  * Contact: Luciano Coelho <luciano.coelho@nokia.com>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * version 2 as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  */
24 
25 #ifndef __WL12XX_H__
26 #define __WL12XX_H__
27 
28 #include <linux/mutex.h>
29 #include <linux/completion.h>
30 #include <linux/spinlock.h>
31 #include <linux/list.h>
32 #include <linux/bitops.h>
33 #include <net/mac80211.h>
34 
35 #include "conf.h"
36 #include "ini.h"
37 
38 #define WL127X_FW_NAME "ti-connectivity/wl127x-fw-3.bin"
39 #define WL128X_FW_NAME "ti-connectivity/wl128x-fw-3.bin"
40 
41 /*
42  * wl127x and wl128x are using the same NVS file name. However, the
43  * ini parameters between them are different.  The driver validates
44  * the correct NVS size in wl1271_boot_upload_nvs().
45  */
46 #define WL12XX_NVS_NAME "ti-connectivity/wl1271-nvs.bin"
47 
48 #define WL1271_TX_SECURITY_LO16(s) ((u16)((s) & 0xffff))
49 #define WL1271_TX_SECURITY_HI32(s) ((u32)(((s) >> 16) & 0xffffffff))
50 #define WL1271_TX_SQN_POST_RECOVERY_PADDING 0xff
51 
52 #define WL1271_CIPHER_SUITE_GEM 0x00147201
53 
54 #define WL1271_BUSY_WORD_CNT 1
55 #define WL1271_BUSY_WORD_LEN (WL1271_BUSY_WORD_CNT * sizeof(u32))
56 
57 #define WL1271_ELP_HW_STATE_ASLEEP 0
58 #define WL1271_ELP_HW_STATE_IRQ    1
59 
60 #define WL1271_DEFAULT_BEACON_INT  100
61 #define WL1271_DEFAULT_DTIM_PERIOD 1
62 
63 #define WL12XX_MAX_ROLES           4
64 #define WL12XX_MAX_LINKS           12
65 #define WL12XX_INVALID_ROLE_ID     0xff
66 #define WL12XX_INVALID_LINK_ID     0xff
67 
68 #define WL12XX_MAX_RATE_POLICIES 16
69 
70 /* Defined by FW as 0. Will not be freed or allocated. */
71 #define WL12XX_SYSTEM_HLID         0
72 
73 /*
74  * When in AP-mode, we allow (at least) this number of packets
75  * to be transmitted to FW for a STA in PS-mode. Only when packets are
76  * present in the FW buffers it will wake the sleeping STA. We want to put
77  * enough packets for the driver to transmit all of its buffered data before
78  * the STA goes to sleep again. But we don't want to take too much memory
79  * as it might hurt the throughput of active STAs.
80  */
81 #define WL1271_PS_STA_MAX_PACKETS  2
82 
83 #define WL1271_AP_BSS_INDEX        0
84 #define WL1271_AP_DEF_BEACON_EXP   20
85 
86 #define ACX_TX_DESCRIPTORS         16
87 
88 #define WL1271_AGGR_BUFFER_SIZE (4 * PAGE_SIZE)
89 
90 enum wl1271_state {
91 	WL1271_STATE_OFF,
92 	WL1271_STATE_ON,
93 	WL1271_STATE_PLT,
94 };
95 
96 enum wl1271_partition_type {
97 	PART_DOWN,
98 	PART_WORK,
99 	PART_DRPW,
100 
101 	PART_TABLE_LEN
102 };
103 
104 struct wl1271_partition {
105 	u32 size;
106 	u32 start;
107 };
108 
109 struct wl1271_partition_set {
110 	struct wl1271_partition mem;
111 	struct wl1271_partition reg;
112 	struct wl1271_partition mem2;
113 	struct wl1271_partition mem3;
114 };
115 
116 struct wl1271;
117 
118 enum {
119 	FW_VER_CHIP,
120 	FW_VER_IF_TYPE,
121 	FW_VER_MAJOR,
122 	FW_VER_SUBTYPE,
123 	FW_VER_MINOR,
124 
125 	NUM_FW_VER
126 };
127 
128 #define FW_VER_CHIP_WL127X 6
129 #define FW_VER_CHIP_WL128X 7
130 
131 #define FW_VER_IF_TYPE_STA 1
132 #define FW_VER_IF_TYPE_AP  2
133 
134 #define FW_VER_MINOR_1_SPARE_STA_MIN 58
135 #define FW_VER_MINOR_1_SPARE_AP_MIN  47
136 
137 #define FW_VER_MINOR_FWLOG_STA_MIN 70
138 
139 struct wl1271_chip {
140 	u32 id;
141 	char fw_ver_str[ETHTOOL_BUSINFO_LEN];
142 	unsigned int fw_ver[NUM_FW_VER];
143 };
144 
145 struct wl1271_stats {
146 	struct acx_statistics *fw_stats;
147 	unsigned long fw_stats_update;
148 
149 	unsigned int retry_count;
150 	unsigned int excessive_retries;
151 };
152 
153 #define NUM_TX_QUEUES              4
154 #define NUM_RX_PKT_DESC            8
155 
156 #define AP_MAX_STATIONS            8
157 
158 /* FW status registers */
159 struct wl12xx_fw_status {
160 	__le32 intr;
161 	u8  fw_rx_counter;
162 	u8  drv_rx_counter;
163 	u8  reserved;
164 	u8  tx_results_counter;
165 	__le32 rx_pkt_descs[NUM_RX_PKT_DESC];
166 	__le32 fw_localtime;
167 
168 	/*
169 	 * A bitmap (where each bit represents a single HLID)
170 	 * to indicate if the station is in PS mode.
171 	 */
172 	__le32 link_ps_bitmap;
173 
174 	/*
175 	 * A bitmap (where each bit represents a single HLID) to indicate
176 	 * if the station is in Fast mode
177 	 */
178 	__le32 link_fast_bitmap;
179 
180 	/* Cumulative counter of total released mem blocks since FW-reset */
181 	__le32 total_released_blks;
182 
183 	/* Size (in Memory Blocks) of TX pool */
184 	__le32 tx_total;
185 
186 	/* Cumulative counter of released packets per AC */
187 	u8 tx_released_pkts[NUM_TX_QUEUES];
188 
189 	/* Cumulative counter of freed packets per HLID */
190 	u8 tx_lnk_free_pkts[WL12XX_MAX_LINKS];
191 
192 	/* Cumulative counter of released Voice memory blocks */
193 	u8 tx_voice_released_blks;
194 	u8 padding_1[3];
195 	__le32 log_start_addr;
196 } __packed;
197 
198 struct wl1271_rx_mem_pool_addr {
199 	u32 addr;
200 	u32 addr_extra;
201 };
202 
203 #define WL1271_MAX_CHANNELS 64
204 struct wl1271_scan {
205 	struct cfg80211_scan_request *req;
206 	unsigned long scanned_ch[BITS_TO_LONGS(WL1271_MAX_CHANNELS)];
207 	bool failed;
208 	u8 state;
209 	u8 ssid[IEEE80211_MAX_SSID_LEN+1];
210 	size_t ssid_len;
211 };
212 
213 struct wl1271_if_operations {
214 	void (*read)(struct device *child, int addr, void *buf, size_t len,
215 		     bool fixed);
216 	void (*write)(struct device *child, int addr, void *buf, size_t len,
217 		     bool fixed);
218 	void (*reset)(struct device *child);
219 	void (*init)(struct device *child);
220 	int (*power)(struct device *child, bool enable);
221 	void (*set_block_size) (struct device *child, unsigned int blksz);
222 };
223 
224 #define MAX_NUM_KEYS 14
225 #define MAX_KEY_SIZE 32
226 
227 struct wl1271_ap_key {
228 	u8 id;
229 	u8 key_type;
230 	u8 key_size;
231 	u8 key[MAX_KEY_SIZE];
232 	u8 hlid;
233 	u32 tx_seq_32;
234 	u16 tx_seq_16;
235 };
236 
237 enum wl12xx_flags {
238 	WL1271_FLAG_GPIO_POWER,
239 	WL1271_FLAG_TX_QUEUE_STOPPED,
240 	WL1271_FLAG_TX_PENDING,
241 	WL1271_FLAG_IN_ELP,
242 	WL1271_FLAG_ELP_REQUESTED,
243 	WL1271_FLAG_IRQ_RUNNING,
244 	WL1271_FLAG_FW_TX_BUSY,
245 	WL1271_FLAG_DUMMY_PACKET_PENDING,
246 	WL1271_FLAG_SUSPENDED,
247 	WL1271_FLAG_PENDING_WORK,
248 	WL1271_FLAG_SOFT_GEMINI,
249 	WL1271_FLAG_RECOVERY_IN_PROGRESS,
250 };
251 
252 enum wl12xx_vif_flags {
253 	WLVIF_FLAG_INITIALIZED,
254 	WLVIF_FLAG_STA_ASSOCIATED,
255 	WLVIF_FLAG_IBSS_JOINED,
256 	WLVIF_FLAG_AP_STARTED,
257 	WLVIF_FLAG_PSM,
258 	WLVIF_FLAG_PSM_REQUESTED,
259 	WLVIF_FLAG_STA_STATE_SENT,
260 	WLVIF_FLAG_RX_STREAMING_STARTED,
261 	WLVIF_FLAG_PSPOLL_FAILURE,
262 	WLVIF_FLAG_CS_PROGRESS,
263 	WLVIF_FLAG_AP_PROBE_RESP_SET,
264 	WLVIF_FLAG_IN_USE,
265 };
266 
267 struct wl1271_link {
268 	/* AP-mode - TX queue per AC in link */
269 	struct sk_buff_head tx_queue[NUM_TX_QUEUES];
270 
271 	/* accounting for allocated / freed packets in FW */
272 	u8 allocated_pkts;
273 	u8 prev_freed_pkts;
274 
275 	u8 addr[ETH_ALEN];
276 
277 	/* bitmap of TIDs where RX BA sessions are active for this link */
278 	u8 ba_bitmap;
279 };
280 
281 struct wl1271 {
282 	struct ieee80211_hw *hw;
283 	bool mac80211_registered;
284 
285 	struct device *dev;
286 
287 	void *if_priv;
288 
289 	struct wl1271_if_operations *if_ops;
290 
291 	void (*set_power)(bool enable);
292 	int irq;
293 	int ref_clock;
294 
295 	spinlock_t wl_lock;
296 
297 	enum wl1271_state state;
298 	struct mutex mutex;
299 
300 	unsigned long flags;
301 
302 	struct wl1271_partition_set part;
303 
304 	struct wl1271_chip chip;
305 
306 	int cmd_box_addr;
307 	int event_box_addr;
308 
309 	u8 *fw;
310 	size_t fw_len;
311 	void *nvs;
312 	size_t nvs_len;
313 
314 	s8 hw_pg_ver;
315 
316 	u8 mac_addr[ETH_ALEN];
317 	int channel;
318 	u8 system_hlid;
319 
320 	unsigned long links_map[BITS_TO_LONGS(WL12XX_MAX_LINKS)];
321 	unsigned long roles_map[BITS_TO_LONGS(WL12XX_MAX_ROLES)];
322 	unsigned long roc_map[BITS_TO_LONGS(WL12XX_MAX_ROLES)];
323 	unsigned long rate_policies_map[
324 			BITS_TO_LONGS(WL12XX_MAX_RATE_POLICIES)];
325 
326 	struct list_head wlvif_list;
327 
328 	u8 sta_count;
329 	u8 ap_count;
330 
331 	struct wl1271_acx_mem_map *target_mem_map;
332 
333 	/* Accounting for allocated / available TX blocks on HW */
334 	u32 tx_blocks_freed;
335 	u32 tx_blocks_available;
336 	u32 tx_allocated_blocks;
337 	u32 tx_results_count;
338 
339 	/* amount of spare TX blocks to use */
340 	u32 tx_spare_blocks;
341 
342 	/* Accounting for allocated / available Tx packets in HW */
343 	u32 tx_pkts_freed[NUM_TX_QUEUES];
344 	u32 tx_allocated_pkts[NUM_TX_QUEUES];
345 
346 	/* Transmitted TX packets counter for chipset interface */
347 	u32 tx_packets_count;
348 
349 	/* Time-offset between host and chipset clocks */
350 	s64 time_offset;
351 
352 	/* Frames scheduled for transmission, not handled yet */
353 	int tx_queue_count[NUM_TX_QUEUES];
354 	long stopped_queues_map;
355 
356 	/* Frames received, not handled yet by mac80211 */
357 	struct sk_buff_head deferred_rx_queue;
358 
359 	/* Frames sent, not returned yet to mac80211 */
360 	struct sk_buff_head deferred_tx_queue;
361 
362 	struct work_struct tx_work;
363 	struct workqueue_struct *freezable_wq;
364 
365 	/* Pending TX frames */
366 	unsigned long tx_frames_map[BITS_TO_LONGS(ACX_TX_DESCRIPTORS)];
367 	struct sk_buff *tx_frames[ACX_TX_DESCRIPTORS];
368 	int tx_frames_cnt;
369 
370 	/* FW Rx counter */
371 	u32 rx_counter;
372 
373 	/* Rx memory pool address */
374 	struct wl1271_rx_mem_pool_addr rx_mem_pool_addr;
375 
376 	/* Intermediate buffer, used for packet aggregation */
377 	u8 *aggr_buf;
378 
379 	/* Reusable dummy packet template */
380 	struct sk_buff *dummy_packet;
381 
382 	/* Network stack work  */
383 	struct work_struct netstack_work;
384 
385 	/* FW log buffer */
386 	u8 *fwlog;
387 
388 	/* Number of valid bytes in the FW log buffer */
389 	ssize_t fwlog_size;
390 
391 	/* Sysfs FW log entry readers wait queue */
392 	wait_queue_head_t fwlog_waitq;
393 
394 	/* Hardware recovery work */
395 	struct work_struct recovery_work;
396 
397 	/* The mbox event mask */
398 	u32 event_mask;
399 
400 	/* Mailbox pointers */
401 	u32 mbox_ptr[2];
402 
403 	/* Are we currently scanning */
404 	struct ieee80211_vif *scan_vif;
405 	struct wl1271_scan scan;
406 	struct delayed_work scan_complete_work;
407 
408 	bool sched_scanning;
409 
410 	/* The current band */
411 	enum ieee80211_band band;
412 
413 	struct completion *elp_compl;
414 	struct delayed_work elp_work;
415 
416 	/* in dBm */
417 	int power_level;
418 
419 	struct wl1271_stats stats;
420 
421 	__le32 buffer_32;
422 	u32 buffer_cmd;
423 	u32 buffer_busyword[WL1271_BUSY_WORD_CNT];
424 
425 	struct wl12xx_fw_status *fw_status;
426 	struct wl1271_tx_hw_res_if *tx_res_if;
427 
428 	struct ieee80211_vif *vif;
429 
430 	/* Current chipset configuration */
431 	struct conf_drv_settings conf;
432 
433 	bool sg_enabled;
434 
435 	bool enable_11a;
436 
437 	struct list_head list;
438 
439 	/* Most recently reported noise in dBm */
440 	s8 noise;
441 
442 	/* bands supported by this instance of wl12xx */
443 	struct ieee80211_supported_band bands[IEEE80211_NUM_BANDS];
444 
445 	int tcxo_clock;
446 
447 	/*
448 	 * wowlan trigger was configured during suspend.
449 	 * (currently, only "ANY" trigger is supported)
450 	 */
451 	bool wow_enabled;
452 	bool irq_wake_enabled;
453 
454 	/*
455 	 * AP-mode - links indexed by HLID. The global and broadcast links
456 	 * are always active.
457 	 */
458 	struct wl1271_link links[WL12XX_MAX_LINKS];
459 
460 	/* AP-mode - a bitmap of links currently in PS mode according to FW */
461 	u32 ap_fw_ps_map;
462 
463 	/* AP-mode - a bitmap of links currently in PS mode in mac80211 */
464 	unsigned long ap_ps_map;
465 
466 	/* Quirks of specific hardware revisions */
467 	unsigned int quirks;
468 
469 	/* Platform limitations */
470 	unsigned int platform_quirks;
471 
472 	/* number of currently active RX BA sessions */
473 	int ba_rx_session_count;
474 
475 	/* AP-mode - number of currently connected stations */
476 	int active_sta_count;
477 
478 	/* last wlvif we transmitted from */
479 	struct wl12xx_vif *last_wlvif;
480 };
481 
482 struct wl1271_station {
483 	u8 hlid;
484 };
485 
486 struct wl12xx_vif {
487 	struct wl1271 *wl;
488 	struct list_head list;
489 	unsigned long flags;
490 	u8 bss_type;
491 	u8 p2p; /* we are using p2p role */
492 	u8 role_id;
493 
494 	/* sta/ibss specific */
495 	u8 dev_role_id;
496 	u8 dev_hlid;
497 
498 	union {
499 		struct {
500 			u8 hlid;
501 			u8 ba_rx_bitmap;
502 
503 			u8 basic_rate_idx;
504 			u8 ap_rate_idx;
505 			u8 p2p_rate_idx;
506 		} sta;
507 		struct {
508 			u8 global_hlid;
509 			u8 bcast_hlid;
510 
511 			/* HLIDs bitmap of associated stations */
512 			unsigned long sta_hlid_map[BITS_TO_LONGS(
513 							WL12XX_MAX_LINKS)];
514 
515 			/* recoreded keys - set here before AP startup */
516 			struct wl1271_ap_key *recorded_keys[MAX_NUM_KEYS];
517 
518 			u8 mgmt_rate_idx;
519 			u8 bcast_rate_idx;
520 			u8 ucast_rate_idx[CONF_TX_MAX_AC_COUNT];
521 		} ap;
522 	};
523 
524 	/* the hlid of the last transmitted skb */
525 	int last_tx_hlid;
526 
527 	unsigned long links_map[BITS_TO_LONGS(WL12XX_MAX_LINKS)];
528 
529 	u8 ssid[IEEE80211_MAX_SSID_LEN + 1];
530 	u8 ssid_len;
531 
532 	/* The current band */
533 	enum ieee80211_band band;
534 	int channel;
535 
536 	u32 bitrate_masks[IEEE80211_NUM_BANDS];
537 	u32 basic_rate_set;
538 
539 	/*
540 	 * currently configured rate set:
541 	 *	bits  0-15 - 802.11abg rates
542 	 *	bits 16-23 - 802.11n   MCS index mask
543 	 * support only 1 stream, thus only 8 bits for the MCS rates (0-7).
544 	 */
545 	u32 basic_rate;
546 	u32 rate_set;
547 
548 	/* probe-req template for the current AP */
549 	struct sk_buff *probereq;
550 
551 	/* Beaconing interval (needed for ad-hoc) */
552 	u32 beacon_int;
553 
554 	/* Default key (for WEP) */
555 	u32 default_key;
556 
557 	/* Our association ID */
558 	u16 aid;
559 
560 	/* Session counter for the chipset */
561 	int session_counter;
562 
563 	struct completion *ps_compl;
564 	struct delayed_work pspoll_work;
565 
566 	/* counter for ps-poll delivery failures */
567 	int ps_poll_failures;
568 
569 	/* retry counter for PSM entries */
570 	u8 psm_entry_retry;
571 
572 	/* in dBm */
573 	int power_level;
574 
575 	int rssi_thold;
576 	int last_rssi_event;
577 
578 	/* RX BA constraint value */
579 	bool ba_support;
580 	bool ba_allowed;
581 
582 	/* Rx Streaming */
583 	struct work_struct rx_streaming_enable_work;
584 	struct work_struct rx_streaming_disable_work;
585 	struct timer_list rx_streaming_timer;
586 
587 	/*
588 	 * This struct must be last!
589 	 * data that has to be saved acrossed reconfigs (e.g. recovery)
590 	 * should be declared in this struct.
591 	 */
592 	struct {
593 		u8 persistent[0];
594 		/*
595 		 * Security sequence number
596 		 *     bits 0-15: lower 16 bits part of sequence number
597 		 *     bits 16-47: higher 32 bits part of sequence number
598 		 *     bits 48-63: not in use
599 		 */
600 		u64 tx_security_seq;
601 
602 		/* 8 bits of the last sequence number in use */
603 		u8 tx_security_last_seq_lsb;
604 	};
605 };
606 
wl12xx_vif_to_data(struct ieee80211_vif * vif)607 static inline struct wl12xx_vif *wl12xx_vif_to_data(struct ieee80211_vif *vif)
608 {
609 	return (struct wl12xx_vif *)vif->drv_priv;
610 }
611 
612 static inline
wl12xx_wlvif_to_vif(struct wl12xx_vif * wlvif)613 struct ieee80211_vif *wl12xx_wlvif_to_vif(struct wl12xx_vif *wlvif)
614 {
615 	return container_of((void *)wlvif, struct ieee80211_vif, drv_priv);
616 }
617 
618 #define wl12xx_for_each_wlvif(wl, wlvif) \
619 		list_for_each_entry(wlvif, &wl->wlvif_list, list)
620 
621 #define wl12xx_for_each_wlvif_continue(wl, wlvif) \
622 		list_for_each_entry_continue(wlvif, &wl->wlvif_list, list)
623 
624 #define wl12xx_for_each_wlvif_bss_type(wl, wlvif, _bss_type)	\
625 		wl12xx_for_each_wlvif(wl, wlvif)		\
626 			if (wlvif->bss_type == _bss_type)
627 
628 #define wl12xx_for_each_wlvif_sta(wl, wlvif)	\
629 		wl12xx_for_each_wlvif_bss_type(wl, wlvif, BSS_TYPE_STA_BSS)
630 
631 #define wl12xx_for_each_wlvif_ap(wl, wlvif)	\
632 		wl12xx_for_each_wlvif_bss_type(wl, wlvif, BSS_TYPE_AP_BSS)
633 
634 int wl1271_plt_start(struct wl1271 *wl);
635 int wl1271_plt_stop(struct wl1271 *wl);
636 int wl1271_recalc_rx_streaming(struct wl1271 *wl, struct wl12xx_vif *wlvif);
637 void wl12xx_queue_recovery_work(struct wl1271 *wl);
638 size_t wl12xx_copy_fwlog(struct wl1271 *wl, u8 *memblock, size_t maxlen);
639 
640 #define JOIN_TIMEOUT 5000 /* 5000 milliseconds to join */
641 
642 #define SESSION_COUNTER_MAX 6 /* maximum value for the session counter */
643 #define SESSION_COUNTER_INVALID 7 /* used with dummy_packet */
644 
645 #define WL1271_DEFAULT_POWER_LEVEL 0
646 
647 #define WL1271_TX_QUEUE_LOW_WATERMARK  32
648 #define WL1271_TX_QUEUE_HIGH_WATERMARK 256
649 
650 #define WL1271_DEFERRED_QUEUE_LIMIT    64
651 
652 /* WL1271 needs a 200ms sleep after power on, and a 20ms sleep before power
653    on in case is has been shut down shortly before */
654 #define WL1271_PRE_POWER_ON_SLEEP 20 /* in milliseconds */
655 #define WL1271_POWER_ON_SLEEP 200 /* in milliseconds */
656 
657 /* Macros to handle wl1271.sta_rate_set */
658 #define HW_BG_RATES_MASK	0xffff
659 #define HW_HT_RATES_OFFSET	16
660 
661 /* Quirks */
662 
663 /* Each RX/TX transaction requires an end-of-transaction transfer */
664 #define WL12XX_QUIRK_END_OF_TRANSACTION		BIT(0)
665 
666 /* wl127x and SPI don't support SDIO block size alignment */
667 #define WL12XX_QUIRK_NO_BLOCKSIZE_ALIGNMENT	BIT(2)
668 
669 /* Older firmwares did not implement the FW logger over bus feature */
670 #define WL12XX_QUIRK_FWLOG_NOT_IMPLEMENTED	BIT(4)
671 
672 #define WL12XX_HW_BLOCK_SIZE	256
673 
674 #endif
675