1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (c) 2012 - 2018 Microchip Technology Inc., and its subsidiaries.
4  * All rights reserved.
5  */
6 
7 #ifndef WILC_NETDEV_H
8 #define WILC_NETDEV_H
9 
10 #include <linux/tcp.h>
11 #include <linux/ieee80211.h>
12 #include <net/cfg80211.h>
13 #include <net/ieee80211_radiotap.h>
14 #include <linux/if_arp.h>
15 #include <linux/gpio/consumer.h>
16 
17 #include "hif.h"
18 #include "wlan.h"
19 #include "wlan_cfg.h"
20 
21 #define FLOW_CONTROL_LOWER_THRESHOLD		128
22 #define FLOW_CONTROL_UPPER_THRESHOLD		256
23 
24 #define PMKID_FOUND				1
25 #define NUM_STA_ASSOCIATED			8
26 
27 #define TCP_ACK_FILTER_LINK_SPEED_THRESH	54
28 #define DEFAULT_LINK_SPEED			72
29 
30 struct wilc_wfi_stats {
31 	unsigned long rx_packets;
32 	unsigned long tx_packets;
33 	unsigned long rx_bytes;
34 	unsigned long tx_bytes;
35 	u64 rx_time;
36 	u64 tx_time;
37 
38 };
39 
40 struct wilc_wfi_key {
41 	u8 *key;
42 	u8 *seq;
43 	int key_len;
44 	int seq_len;
45 	u32 cipher;
46 };
47 
48 struct wilc_wfi_wep_key {
49 	u8 *key;
50 	u8 key_len;
51 	u8 key_idx;
52 };
53 
54 struct sta_info {
55 	u8 sta_associated_bss[WILC_MAX_NUM_STA][ETH_ALEN];
56 };
57 
58 /* Parameters needed for host interface for remaining on channel */
59 struct wilc_wfi_p2p_listen_params {
60 	struct ieee80211_channel *listen_ch;
61 	u32 listen_duration;
62 	u64 listen_cookie;
63 };
64 
65 static const u32 wilc_cipher_suites[] = {
66 	WLAN_CIPHER_SUITE_WEP40,
67 	WLAN_CIPHER_SUITE_WEP104,
68 	WLAN_CIPHER_SUITE_TKIP,
69 	WLAN_CIPHER_SUITE_CCMP,
70 	WLAN_CIPHER_SUITE_AES_CMAC
71 };
72 
73 #define CHAN2G(_channel, _freq, _flags) {	 \
74 	.band             = NL80211_BAND_2GHZ, \
75 	.center_freq      = (_freq),		 \
76 	.hw_value         = (_channel),		 \
77 	.flags            = (_flags),		 \
78 	.max_antenna_gain = 0,			 \
79 	.max_power        = 30,			 \
80 }
81 
82 static const struct ieee80211_channel wilc_2ghz_channels[] = {
83 	CHAN2G(1,  2412, 0),
84 	CHAN2G(2,  2417, 0),
85 	CHAN2G(3,  2422, 0),
86 	CHAN2G(4,  2427, 0),
87 	CHAN2G(5,  2432, 0),
88 	CHAN2G(6,  2437, 0),
89 	CHAN2G(7,  2442, 0),
90 	CHAN2G(8,  2447, 0),
91 	CHAN2G(9,  2452, 0),
92 	CHAN2G(10, 2457, 0),
93 	CHAN2G(11, 2462, 0),
94 	CHAN2G(12, 2467, 0),
95 	CHAN2G(13, 2472, 0),
96 	CHAN2G(14, 2484, 0)
97 };
98 
99 #define RATETAB_ENT(_rate, _hw_value, _flags) {	\
100 	.bitrate  = (_rate),			\
101 	.hw_value = (_hw_value),		\
102 	.flags    = (_flags),			\
103 }
104 
105 static struct ieee80211_rate wilc_bitrates[] = {
106 	RATETAB_ENT(10,  0,  0),
107 	RATETAB_ENT(20,  1,  0),
108 	RATETAB_ENT(55,  2,  0),
109 	RATETAB_ENT(110, 3,  0),
110 	RATETAB_ENT(60,  9,  0),
111 	RATETAB_ENT(90,  6,  0),
112 	RATETAB_ENT(120, 7,  0),
113 	RATETAB_ENT(180, 8,  0),
114 	RATETAB_ENT(240, 9,  0),
115 	RATETAB_ENT(360, 10, 0),
116 	RATETAB_ENT(480, 11, 0),
117 	RATETAB_ENT(540, 12, 0)
118 };
119 
120 struct wilc_priv {
121 	struct wireless_dev wdev;
122 	struct cfg80211_scan_request *scan_req;
123 
124 	struct wilc_wfi_p2p_listen_params remain_on_ch_params;
125 	u64 tx_cookie;
126 
127 	bool cfg_scanning;
128 
129 	u8 associated_bss[ETH_ALEN];
130 	struct sta_info assoc_stainfo;
131 	struct sk_buff *skb;
132 	struct net_device *dev;
133 	struct host_if_drv *hif_drv;
134 	struct wilc_pmkid_attr pmkid_list;
135 	u8 wep_key[4][WLAN_KEY_LEN_WEP104];
136 	u8 wep_key_len[4];
137 
138 	/* The real interface that the monitor is on */
139 	struct net_device *real_ndev;
140 	struct wilc_wfi_key *wilc_gtk[WILC_MAX_NUM_STA];
141 	struct wilc_wfi_key *wilc_ptk[WILC_MAX_NUM_STA];
142 	u8 wilc_groupkey;
143 
144 	/* mutexes */
145 	struct mutex scan_req_lock;
146 	bool p2p_listen_state;
147 	int scanned_cnt;
148 
149 	u64 inc_roc_cookie;
150 };
151 
152 #define MAX_TCP_SESSION                25
153 #define MAX_PENDING_ACKS               256
154 
155 struct ack_session_info {
156 	u32 seq_num;
157 	u32 bigger_ack_num;
158 	u16 src_port;
159 	u16 dst_port;
160 	u16 status;
161 };
162 
163 struct pending_acks {
164 	u32 ack_num;
165 	u32 session_index;
166 	struct txq_entry_t  *txqe;
167 };
168 
169 struct tcp_ack_filter {
170 	struct ack_session_info ack_session_info[2 * MAX_TCP_SESSION];
171 	struct pending_acks pending_acks[MAX_PENDING_ACKS];
172 	u32 pending_base;
173 	u32 tcp_session;
174 	u32 pending_acks_idx;
175 	bool enabled;
176 };
177 
178 struct wilc_vif {
179 	u8 idx;
180 	u8 iftype;
181 	int monitor_flag;
182 	int mac_opened;
183 	u32 mgmt_reg_stypes;
184 	struct net_device_stats netstats;
185 	struct wilc *wilc;
186 	u8 bssid[ETH_ALEN];
187 	struct host_if_drv *hif_drv;
188 	struct net_device *ndev;
189 	u8 mode;
190 	struct timer_list during_ip_timer;
191 	struct timer_list periodic_rssi;
192 	struct rf_info periodic_stat;
193 	struct tcp_ack_filter ack_filter;
194 	bool connecting;
195 	struct wilc_priv priv;
196 	struct list_head list;
197 	struct cfg80211_bss *bss;
198 };
199 
200 struct wilc {
201 	struct wiphy *wiphy;
202 	const struct wilc_hif_func *hif_func;
203 	int io_type;
204 	s8 mac_status;
205 	struct clk *rtc_clk;
206 	bool initialized;
207 	int dev_irq_num;
208 	int close;
209 	u8 vif_num;
210 	struct list_head vif_list;
211 
212 	/* protect vif list */
213 	struct mutex vif_mutex;
214 	struct srcu_struct srcu;
215 	u8 open_ifcs;
216 
217 	/* protect head of transmit queue */
218 	struct mutex txq_add_to_head_cs;
219 
220 	/* protect txq_entry_t transmit queue */
221 	spinlock_t txq_spinlock;
222 
223 	/* protect rxq_entry_t receiver queue */
224 	struct mutex rxq_cs;
225 
226 	/* lock to protect hif access */
227 	struct mutex hif_cs;
228 
229 	struct completion cfg_event;
230 	struct completion sync_event;
231 	struct completion txq_event;
232 	struct completion txq_thread_started;
233 
234 	struct task_struct *txq_thread;
235 
236 	int quit;
237 
238 	/* lock to protect issue of wid command to firmware */
239 	struct mutex cfg_cmd_lock;
240 	struct wilc_cfg_frame cfg_frame;
241 	u32 cfg_frame_offset;
242 	u8 cfg_seq_no;
243 
244 	u8 *rx_buffer;
245 	u32 rx_buffer_offset;
246 	u8 *tx_buffer;
247 
248 	struct txq_entry_t txq_head;
249 	int txq_entries;
250 
251 	struct rxq_entry_t rxq_head;
252 
253 	const struct firmware *firmware;
254 
255 	struct device *dev;
256 	bool suspend_event;
257 
258 	int clients_count;
259 	struct workqueue_struct *hif_workqueue;
260 	enum chip_ps_states chip_ps_state;
261 	struct wilc_cfg cfg;
262 	void *bus_data;
263 	struct net_device *monitor_dev;
264 
265 	/* deinit lock */
266 	struct mutex deinit_lock;
267 	u8 sta_ch;
268 	u8 op_ch;
269 	struct ieee80211_channel channels[ARRAY_SIZE(wilc_2ghz_channels)];
270 	struct ieee80211_rate bitrates[ARRAY_SIZE(wilc_bitrates)];
271 	struct ieee80211_supported_band band;
272 	u32 cipher_suites[ARRAY_SIZE(wilc_cipher_suites)];
273 };
274 
275 struct wilc_wfi_mon_priv {
276 	struct net_device *real_ndev;
277 };
278 
279 void wilc_frmw_to_host(struct wilc *wilc, u8 *buff, u32 size, u32 pkt_offset);
280 void wilc_mac_indicate(struct wilc *wilc);
281 void wilc_netdev_cleanup(struct wilc *wilc);
282 void wilc_wfi_mgmt_rx(struct wilc *wilc, u8 *buff, u32 size);
283 void wilc_wlan_set_bssid(struct net_device *wilc_netdev, u8 *bssid, u8 mode);
284 struct wilc_vif *wilc_netdev_ifc_init(struct wilc *wl, const char *name,
285 				      int vif_type, enum nl80211_iftype type,
286 				      bool rtnl_locked);
287 #endif
288