xref: /linux/drivers/net/wireless/marvell/mwifiex/util.c (revision c7979c3917fa1326dae3607e1c6a04c12057b194)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * NXP Wireless LAN device driver: utility functions
4  *
5  * Copyright 2011-2020 NXP
6  */
7 
8 #include "decl.h"
9 #include "ioctl.h"
10 #include "util.h"
11 #include "fw.h"
12 #include "main.h"
13 #include "wmm.h"
14 #include "11n.h"
15 
16 static struct mwifiex_debug_data items[] = {
17 	{"debug_mask", item_size(debug_mask),
18 	 item_addr(debug_mask), 1},
19 	{"int_counter", item_size(int_counter),
20 	 item_addr(int_counter), 1},
21 	{"wmm_ac_vo", item_size(packets_out[WMM_AC_VO]),
22 	 item_addr(packets_out[WMM_AC_VO]), 1},
23 	{"wmm_ac_vi", item_size(packets_out[WMM_AC_VI]),
24 	 item_addr(packets_out[WMM_AC_VI]), 1},
25 	{"wmm_ac_be", item_size(packets_out[WMM_AC_BE]),
26 	 item_addr(packets_out[WMM_AC_BE]), 1},
27 	{"wmm_ac_bk", item_size(packets_out[WMM_AC_BK]),
28 	 item_addr(packets_out[WMM_AC_BK]), 1},
29 	{"tx_buf_size", item_size(tx_buf_size),
30 	 item_addr(tx_buf_size), 1},
31 	{"curr_tx_buf_size", item_size(curr_tx_buf_size),
32 	 item_addr(curr_tx_buf_size), 1},
33 	{"ps_mode", item_size(ps_mode),
34 	 item_addr(ps_mode), 1},
35 	{"ps_state", item_size(ps_state),
36 	 item_addr(ps_state), 1},
37 	{"is_deep_sleep", item_size(is_deep_sleep),
38 	 item_addr(is_deep_sleep), 1},
39 	{"wakeup_dev_req", item_size(pm_wakeup_card_req),
40 	 item_addr(pm_wakeup_card_req), 1},
41 	{"wakeup_tries", item_size(pm_wakeup_fw_try),
42 	 item_addr(pm_wakeup_fw_try), 1},
43 	{"hs_configured", item_size(is_hs_configured),
44 	 item_addr(is_hs_configured), 1},
45 	{"hs_activated", item_size(hs_activated),
46 	 item_addr(hs_activated), 1},
47 	{"num_tx_timeout", item_size(num_tx_timeout),
48 	 item_addr(num_tx_timeout), 1},
49 	{"is_cmd_timedout", item_size(is_cmd_timedout),
50 	 item_addr(is_cmd_timedout), 1},
51 	{"timeout_cmd_id", item_size(timeout_cmd_id),
52 	 item_addr(timeout_cmd_id), 1},
53 	{"timeout_cmd_act", item_size(timeout_cmd_act),
54 	 item_addr(timeout_cmd_act), 1},
55 	{"last_cmd_id", item_size(last_cmd_id),
56 	 item_addr(last_cmd_id), DBG_CMD_NUM},
57 	{"last_cmd_act", item_size(last_cmd_act),
58 	 item_addr(last_cmd_act), DBG_CMD_NUM},
59 	{"last_cmd_index", item_size(last_cmd_index),
60 	 item_addr(last_cmd_index), 1},
61 	{"last_cmd_resp_id", item_size(last_cmd_resp_id),
62 	 item_addr(last_cmd_resp_id), DBG_CMD_NUM},
63 	{"last_cmd_resp_index", item_size(last_cmd_resp_index),
64 	 item_addr(last_cmd_resp_index), 1},
65 	{"last_event", item_size(last_event),
66 	 item_addr(last_event), DBG_CMD_NUM},
67 	{"last_event_index", item_size(last_event_index),
68 	 item_addr(last_event_index), 1},
69 	{"last_mp_wr_bitmap", item_size(last_mp_wr_bitmap),
70 	 item_addr(last_mp_wr_bitmap), MWIFIEX_DBG_SDIO_MP_NUM},
71 	{"last_mp_wr_ports", item_size(last_mp_wr_ports),
72 	 item_addr(last_mp_wr_ports), MWIFIEX_DBG_SDIO_MP_NUM},
73 	{"last_mp_wr_len", item_size(last_mp_wr_len),
74 	 item_addr(last_mp_wr_len), MWIFIEX_DBG_SDIO_MP_NUM},
75 	{"last_mp_curr_wr_port", item_size(last_mp_curr_wr_port),
76 	 item_addr(last_mp_curr_wr_port), MWIFIEX_DBG_SDIO_MP_NUM},
77 	{"last_sdio_mp_index", item_size(last_sdio_mp_index),
78 	 item_addr(last_sdio_mp_index), 1},
79 	{"num_cmd_h2c_fail", item_size(num_cmd_host_to_card_failure),
80 	 item_addr(num_cmd_host_to_card_failure), 1},
81 	{"num_cmd_sleep_cfm_fail",
82 	 item_size(num_cmd_sleep_cfm_host_to_card_failure),
83 	 item_addr(num_cmd_sleep_cfm_host_to_card_failure), 1},
84 	{"num_tx_h2c_fail", item_size(num_tx_host_to_card_failure),
85 	 item_addr(num_tx_host_to_card_failure), 1},
86 	{"num_evt_deauth", item_size(num_event_deauth),
87 	 item_addr(num_event_deauth), 1},
88 	{"num_evt_disassoc", item_size(num_event_disassoc),
89 	 item_addr(num_event_disassoc), 1},
90 	{"num_evt_link_lost", item_size(num_event_link_lost),
91 	 item_addr(num_event_link_lost), 1},
92 	{"num_cmd_deauth", item_size(num_cmd_deauth),
93 	 item_addr(num_cmd_deauth), 1},
94 	{"num_cmd_assoc_ok", item_size(num_cmd_assoc_success),
95 	 item_addr(num_cmd_assoc_success), 1},
96 	{"num_cmd_assoc_fail", item_size(num_cmd_assoc_failure),
97 	 item_addr(num_cmd_assoc_failure), 1},
98 	{"cmd_sent", item_size(cmd_sent),
99 	 item_addr(cmd_sent), 1},
100 	{"data_sent", item_size(data_sent),
101 	 item_addr(data_sent), 1},
102 	{"cmd_resp_received", item_size(cmd_resp_received),
103 	 item_addr(cmd_resp_received), 1},
104 	{"event_received", item_size(event_received),
105 	 item_addr(event_received), 1},
106 
107 	/* variables defined in struct mwifiex_adapter */
108 	{"cmd_pending", adapter_item_size(cmd_pending),
109 	 adapter_item_addr(cmd_pending), 1},
110 	{"tx_pending", adapter_item_size(tx_pending),
111 	 adapter_item_addr(tx_pending), 1},
112 	{"rx_pending", adapter_item_size(rx_pending),
113 	 adapter_item_addr(rx_pending), 1},
114 };
115 
116 static int num_of_items = ARRAY_SIZE(items);
117 
118 /*
119  * This function sends init/shutdown command
120  * to firmware.
121  */
mwifiex_init_shutdown_fw(struct mwifiex_private * priv,u32 func_init_shutdown)122 int mwifiex_init_shutdown_fw(struct mwifiex_private *priv,
123 			     u32 func_init_shutdown)
124 {
125 	u16 cmd;
126 
127 	if (func_init_shutdown == MWIFIEX_FUNC_INIT) {
128 		cmd = HostCmd_CMD_FUNC_INIT;
129 	} else if (func_init_shutdown == MWIFIEX_FUNC_SHUTDOWN) {
130 		cmd = HostCmd_CMD_FUNC_SHUTDOWN;
131 	} else {
132 		mwifiex_dbg(priv->adapter, ERROR,
133 			    "unsupported parameter\n");
134 		return -1;
135 	}
136 
137 	return mwifiex_send_cmd(priv, cmd, HostCmd_ACT_GEN_SET, 0, NULL, true);
138 }
139 EXPORT_SYMBOL_GPL(mwifiex_init_shutdown_fw);
140 
141 /*
142  * IOCTL request handler to set/get debug information.
143  *
144  * This function collates/sets the information from/to different driver
145  * structures.
146  */
mwifiex_get_debug_info(struct mwifiex_private * priv,struct mwifiex_debug_info * info)147 int mwifiex_get_debug_info(struct mwifiex_private *priv,
148 			   struct mwifiex_debug_info *info)
149 {
150 	struct mwifiex_adapter *adapter = priv->adapter;
151 
152 	if (info) {
153 		info->debug_mask = adapter->debug_mask;
154 		memcpy(info->packets_out,
155 		       priv->wmm.packets_out,
156 		       sizeof(priv->wmm.packets_out));
157 		info->curr_tx_buf_size = (u32) adapter->curr_tx_buf_size;
158 		info->tx_buf_size = (u32) adapter->tx_buf_size;
159 		info->rx_tbl_num = mwifiex_get_rx_reorder_tbl(priv,
160 							      info->rx_tbl);
161 		info->tx_tbl_num = mwifiex_get_tx_ba_stream_tbl(priv,
162 								info->tx_tbl);
163 		info->tdls_peer_num = mwifiex_get_tdls_list(priv,
164 							    info->tdls_list);
165 		info->ps_mode = adapter->ps_mode;
166 		info->ps_state = adapter->ps_state;
167 		info->is_deep_sleep = adapter->is_deep_sleep;
168 		info->pm_wakeup_card_req = adapter->pm_wakeup_card_req;
169 		info->pm_wakeup_fw_try = adapter->pm_wakeup_fw_try;
170 		info->is_hs_configured = test_bit(MWIFIEX_IS_HS_CONFIGURED,
171 						  &adapter->work_flags);
172 		info->hs_activated = adapter->hs_activated;
173 		info->is_cmd_timedout = test_bit(MWIFIEX_IS_CMD_TIMEDOUT,
174 						 &adapter->work_flags);
175 		info->num_cmd_host_to_card_failure
176 				= adapter->dbg.num_cmd_host_to_card_failure;
177 		info->num_cmd_sleep_cfm_host_to_card_failure
178 			= adapter->dbg.num_cmd_sleep_cfm_host_to_card_failure;
179 		info->num_tx_host_to_card_failure
180 				= adapter->dbg.num_tx_host_to_card_failure;
181 		info->num_event_deauth = adapter->dbg.num_event_deauth;
182 		info->num_event_disassoc = adapter->dbg.num_event_disassoc;
183 		info->num_event_link_lost = adapter->dbg.num_event_link_lost;
184 		info->num_cmd_deauth = adapter->dbg.num_cmd_deauth;
185 		info->num_cmd_assoc_success =
186 					adapter->dbg.num_cmd_assoc_success;
187 		info->num_cmd_assoc_failure =
188 					adapter->dbg.num_cmd_assoc_failure;
189 		info->num_tx_timeout = adapter->dbg.num_tx_timeout;
190 		info->timeout_cmd_id = adapter->dbg.timeout_cmd_id;
191 		info->timeout_cmd_act = adapter->dbg.timeout_cmd_act;
192 		memcpy(info->last_cmd_id, adapter->dbg.last_cmd_id,
193 		       sizeof(adapter->dbg.last_cmd_id));
194 		memcpy(info->last_cmd_act, adapter->dbg.last_cmd_act,
195 		       sizeof(adapter->dbg.last_cmd_act));
196 		info->last_cmd_index = adapter->dbg.last_cmd_index;
197 		memcpy(info->last_cmd_resp_id, adapter->dbg.last_cmd_resp_id,
198 		       sizeof(adapter->dbg.last_cmd_resp_id));
199 		info->last_cmd_resp_index = adapter->dbg.last_cmd_resp_index;
200 		memcpy(info->last_event, adapter->dbg.last_event,
201 		       sizeof(adapter->dbg.last_event));
202 		info->last_event_index = adapter->dbg.last_event_index;
203 		memcpy(info->last_mp_wr_bitmap, adapter->dbg.last_mp_wr_bitmap,
204 		       sizeof(adapter->dbg.last_mp_wr_bitmap));
205 		memcpy(info->last_mp_wr_ports, adapter->dbg.last_mp_wr_ports,
206 		       sizeof(adapter->dbg.last_mp_wr_ports));
207 		memcpy(info->last_mp_curr_wr_port,
208 		       adapter->dbg.last_mp_curr_wr_port,
209 		       sizeof(adapter->dbg.last_mp_curr_wr_port));
210 		memcpy(info->last_mp_wr_len, adapter->dbg.last_mp_wr_len,
211 		       sizeof(adapter->dbg.last_mp_wr_len));
212 		info->last_sdio_mp_index = adapter->dbg.last_sdio_mp_index;
213 		info->data_sent = adapter->data_sent;
214 		info->cmd_sent = adapter->cmd_sent;
215 		info->cmd_resp_received = adapter->cmd_resp_received;
216 	}
217 
218 	return 0;
219 }
220 
mwifiex_debug_info_to_buffer(struct mwifiex_private * priv,char * buf,struct mwifiex_debug_info * info)221 int mwifiex_debug_info_to_buffer(struct mwifiex_private *priv, char *buf,
222 				 struct mwifiex_debug_info *info)
223 {
224 	char *p = buf;
225 	struct mwifiex_debug_data *d = &items[0];
226 	size_t size, addr;
227 	long val;
228 	int i, j;
229 
230 	if (!info)
231 		return 0;
232 
233 	for (i = 0; i < num_of_items; i++) {
234 		p += sprintf(p, "%s=", d[i].name);
235 
236 		size = d[i].size / d[i].num;
237 
238 		if (i < (num_of_items - 3))
239 			addr = d[i].addr + (size_t)info;
240 		else /* The last 3 items are struct mwifiex_adapter variables */
241 			addr = d[i].addr + (size_t)priv->adapter;
242 
243 		for (j = 0; j < d[i].num; j++) {
244 			switch (size) {
245 			case 1:
246 				val = *((u8 *)addr);
247 				break;
248 			case 2:
249 				val = get_unaligned((u16 *)addr);
250 				break;
251 			case 4:
252 				val = get_unaligned((u32 *)addr);
253 				break;
254 			case 8:
255 				val = get_unaligned((long long *)addr);
256 				break;
257 			default:
258 				val = -1;
259 				break;
260 			}
261 
262 			p += sprintf(p, "%#lx ", val);
263 			addr += size;
264 		}
265 
266 		p += sprintf(p, "\n");
267 	}
268 
269 	if (info->tx_tbl_num) {
270 		p += sprintf(p, "Tx BA stream table:\n");
271 		for (i = 0; i < info->tx_tbl_num; i++)
272 			p += sprintf(p, "tid = %d, ra = %pM\n",
273 				     info->tx_tbl[i].tid, info->tx_tbl[i].ra);
274 	}
275 
276 	if (info->rx_tbl_num) {
277 		p += sprintf(p, "Rx reorder table:\n");
278 		for (i = 0; i < info->rx_tbl_num; i++) {
279 			p += sprintf(p, "tid = %d, ta = %pM, ",
280 				     info->rx_tbl[i].tid,
281 				     info->rx_tbl[i].ta);
282 			p += sprintf(p, "start_win = %d, ",
283 				     info->rx_tbl[i].start_win);
284 			p += sprintf(p, "win_size = %d, buffer: ",
285 				     info->rx_tbl[i].win_size);
286 
287 			for (j = 0; j < info->rx_tbl[i].win_size; j++)
288 				p += sprintf(p, "%c ",
289 					     info->rx_tbl[i].buffer[j] ?
290 					     '1' : '0');
291 
292 			p += sprintf(p, "\n");
293 		}
294 	}
295 
296 	if (info->tdls_peer_num) {
297 		p += sprintf(p, "TDLS peer table:\n");
298 		for (i = 0; i < info->tdls_peer_num; i++) {
299 			p += sprintf(p, "peer = %pM",
300 				     info->tdls_list[i].peer_addr);
301 			p += sprintf(p, "\n");
302 		}
303 	}
304 
305 	return p - buf;
306 }
307 
308 static int
mwifiex_parse_mgmt_packet(struct mwifiex_private * priv,u8 * payload,u16 len,struct rxpd * rx_pd)309 mwifiex_parse_mgmt_packet(struct mwifiex_private *priv, u8 *payload, u16 len,
310 			  struct rxpd *rx_pd)
311 {
312 	u16 stype;
313 	u8 category, action_code, *addr2;
314 	struct ieee80211_hdr *ieee_hdr = (void *)payload;
315 
316 	stype = (le16_to_cpu(ieee_hdr->frame_control) & IEEE80211_FCTL_STYPE);
317 
318 	switch (stype) {
319 	case IEEE80211_STYPE_ACTION:
320 		category = *(payload + sizeof(struct ieee80211_hdr));
321 		switch (category) {
322 		case WLAN_CATEGORY_PUBLIC:
323 			action_code = *(payload + sizeof(struct ieee80211_hdr)
324 					+ 1);
325 			if (action_code == WLAN_PUB_ACTION_TDLS_DISCOVER_RES) {
326 				addr2 = ieee_hdr->addr2;
327 				mwifiex_dbg(priv->adapter, INFO,
328 					    "TDLS discovery response %pM nf=%d, snr=%d\n",
329 					    addr2, rx_pd->nf, rx_pd->snr);
330 				mwifiex_auto_tdls_update_peer_signal(priv,
331 								     addr2,
332 								     rx_pd->snr,
333 								     rx_pd->nf);
334 			}
335 			break;
336 		case WLAN_CATEGORY_BACK:
337 			/*we dont indicate BACK action frames to cfg80211*/
338 			mwifiex_dbg(priv->adapter, INFO,
339 				    "drop BACK action frames");
340 			return -1;
341 		default:
342 			mwifiex_dbg(priv->adapter, INFO,
343 				    "unknown public action frame category %d\n",
344 				    category);
345 		}
346 		break;
347 	default:
348 		mwifiex_dbg(priv->adapter, INFO,
349 		    "unknown mgmt frame subtype %#x\n", stype);
350 		return 0;
351 	}
352 
353 	return 0;
354 }
355 
356 /* This function sends deauth packet to the kernel. */
mwifiex_host_mlme_disconnect(struct mwifiex_private * priv,u16 reason_code,u8 * sa)357 void mwifiex_host_mlme_disconnect(struct mwifiex_private *priv,
358 				  u16 reason_code, u8 *sa)
359 {
360 	u8 frame_buf[100];
361 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)frame_buf;
362 
363 	memset(frame_buf, 0, sizeof(frame_buf));
364 	mgmt->frame_control = cpu_to_le16(IEEE80211_STYPE_DEAUTH);
365 	mgmt->duration = 0;
366 	mgmt->seq_ctrl = 0;
367 	mgmt->u.deauth.reason_code = cpu_to_le16(reason_code);
368 
369 	if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA) {
370 		eth_broadcast_addr(mgmt->da);
371 		memcpy(mgmt->sa,
372 		       priv->curr_bss_params.bss_descriptor.mac_address,
373 		       ETH_ALEN);
374 		memcpy(mgmt->bssid, priv->cfg_bssid, ETH_ALEN);
375 		priv->auth_flag = 0;
376 		priv->auth_alg = WLAN_AUTH_NONE;
377 	} else {
378 		memcpy(mgmt->da, priv->curr_addr, ETH_ALEN);
379 		memcpy(mgmt->sa, sa, ETH_ALEN);
380 		memcpy(mgmt->bssid, priv->curr_addr, ETH_ALEN);
381 	}
382 
383 	if (GET_BSS_ROLE(priv) != MWIFIEX_BSS_ROLE_UAP) {
384 		wiphy_lock(priv->wdev.wiphy);
385 		cfg80211_rx_mlme_mgmt(priv->netdev, frame_buf, 26);
386 		wiphy_unlock(priv->wdev.wiphy);
387 	} else {
388 		cfg80211_rx_mgmt(&priv->wdev,
389 				 priv->bss_chandef.chan->center_freq,
390 				 0, frame_buf, 26, 0);
391 	}
392 }
393 
394 /*
395  * This function processes the received management packet and send it
396  * to the kernel.
397  */
398 int
mwifiex_process_mgmt_packet(struct mwifiex_private * priv,struct sk_buff * skb)399 mwifiex_process_mgmt_packet(struct mwifiex_private *priv,
400 			    struct sk_buff *skb)
401 {
402 	struct rxpd *rx_pd;
403 	u16 pkt_len;
404 	struct ieee80211_hdr *ieee_hdr;
405 
406 	if (!skb)
407 		return -1;
408 
409 	if (!priv->mgmt_frame_mask ||
410 	    priv->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED) {
411 		mwifiex_dbg(priv->adapter, ERROR,
412 			    "do not receive mgmt frames on uninitialized intf");
413 		return -1;
414 	}
415 
416 	rx_pd = (struct rxpd *)skb->data;
417 	pkt_len = le16_to_cpu(rx_pd->rx_pkt_length);
418 	if (pkt_len < sizeof(struct ieee80211_hdr) + sizeof(pkt_len)) {
419 		mwifiex_dbg(priv->adapter, ERROR, "invalid rx_pkt_length");
420 		return -1;
421 	}
422 
423 	skb_pull(skb, le16_to_cpu(rx_pd->rx_pkt_offset));
424 	skb_pull(skb, sizeof(pkt_len));
425 	pkt_len -= sizeof(pkt_len);
426 
427 	ieee_hdr = (void *)skb->data;
428 	if (ieee80211_is_mgmt(ieee_hdr->frame_control)) {
429 		if (mwifiex_parse_mgmt_packet(priv, (u8 *)ieee_hdr,
430 					      pkt_len, rx_pd))
431 			return -1;
432 	}
433 	/* Remove address4 */
434 	memmove(skb->data + sizeof(struct ieee80211_hdr_3addr),
435 		skb->data + sizeof(struct ieee80211_hdr),
436 		pkt_len - sizeof(struct ieee80211_hdr));
437 
438 	pkt_len -= ETH_ALEN;
439 	rx_pd->rx_pkt_length = cpu_to_le16(pkt_len);
440 
441 	if (priv->host_mlme_reg &&
442 	    (GET_BSS_ROLE(priv) != MWIFIEX_BSS_ROLE_UAP) &&
443 	    (ieee80211_is_auth(ieee_hdr->frame_control) ||
444 	     ieee80211_is_deauth(ieee_hdr->frame_control) ||
445 	     ieee80211_is_disassoc(ieee_hdr->frame_control))) {
446 		if (ieee80211_is_auth(ieee_hdr->frame_control)) {
447 			if (priv->auth_flag & HOST_MLME_AUTH_PENDING) {
448 				if (priv->auth_alg != WLAN_AUTH_SAE) {
449 					priv->auth_flag &=
450 						~HOST_MLME_AUTH_PENDING;
451 					priv->auth_flag |=
452 						HOST_MLME_AUTH_DONE;
453 				}
454 			} else {
455 				return 0;
456 			}
457 
458 			mwifiex_dbg(priv->adapter, MSG,
459 				    "auth: receive authentication from %pM\n",
460 				    ieee_hdr->addr3);
461 		} else {
462 			if (!priv->wdev.connected ||
463 			    !ether_addr_equal(ieee_hdr->addr3,
464 					      priv->curr_bss_params.bss_descriptor.mac_address))
465 				return 0;
466 
467 			if (ieee80211_is_deauth(ieee_hdr->frame_control)) {
468 				mwifiex_dbg(priv->adapter, MSG,
469 					    "auth: receive deauth from %pM\n",
470 					    ieee_hdr->addr3);
471 				priv->auth_flag = 0;
472 				priv->auth_alg = WLAN_AUTH_NONE;
473 			} else {
474 				mwifiex_dbg
475 				(priv->adapter, MSG,
476 				 "assoc: receive disassoc from %pM\n",
477 				 ieee_hdr->addr3);
478 			}
479 		}
480 
481 		wiphy_lock(priv->wdev.wiphy);
482 		cfg80211_rx_mlme_mgmt(priv->netdev, skb->data, pkt_len);
483 		wiphy_unlock(priv->wdev.wiphy);
484 	}
485 
486 	if (priv->adapter->host_mlme_enabled &&
487 	    (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP)) {
488 		if (ieee80211_is_auth(ieee_hdr->frame_control))
489 			mwifiex_dbg(priv->adapter, MSG,
490 				    "auth: receive auth from %pM\n",
491 				    ieee_hdr->addr2);
492 		if (ieee80211_is_deauth(ieee_hdr->frame_control))
493 			mwifiex_dbg(priv->adapter, MSG,
494 				    "auth: receive deauth from %pM\n",
495 				    ieee_hdr->addr2);
496 		if (ieee80211_is_disassoc(ieee_hdr->frame_control))
497 			mwifiex_dbg(priv->adapter, MSG,
498 				    "assoc: receive disassoc from %pM\n",
499 				    ieee_hdr->addr2);
500 		if (ieee80211_is_assoc_req(ieee_hdr->frame_control))
501 			mwifiex_dbg(priv->adapter, MSG,
502 				    "assoc: receive assoc req from %pM\n",
503 				    ieee_hdr->addr2);
504 		if (ieee80211_is_reassoc_req(ieee_hdr->frame_control))
505 			mwifiex_dbg(priv->adapter, MSG,
506 				    "assoc: receive reassoc req from %pM\n",
507 				    ieee_hdr->addr2);
508 	}
509 
510 	cfg80211_rx_mgmt(&priv->wdev, priv->roc_cfg.chan.center_freq,
511 			 CAL_RSSI(rx_pd->snr, rx_pd->nf), skb->data, pkt_len,
512 			 0);
513 
514 	return 0;
515 }
516 
517 /*
518  * This function processes the received packet before sending it to the
519  * kernel.
520  *
521  * It extracts the SKB from the received buffer and sends it to kernel.
522  * In case the received buffer does not contain the data in SKB format,
523  * the function creates a blank SKB, fills it with the data from the
524  * received buffer and then sends this new SKB to the kernel.
525  */
mwifiex_recv_packet(struct mwifiex_private * priv,struct sk_buff * skb)526 int mwifiex_recv_packet(struct mwifiex_private *priv, struct sk_buff *skb)
527 {
528 	struct mwifiex_sta_node *src_node;
529 	struct ethhdr *p_ethhdr;
530 
531 	if (!skb)
532 		return -1;
533 
534 	priv->stats.rx_bytes += skb->len;
535 	priv->stats.rx_packets++;
536 
537 	if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP) {
538 		p_ethhdr = (void *)skb->data;
539 		src_node = mwifiex_get_sta_entry(priv, p_ethhdr->h_source);
540 		if (src_node) {
541 			src_node->stats.last_rx = jiffies;
542 			src_node->stats.rx_bytes += skb->len;
543 			src_node->stats.rx_packets++;
544 		}
545 	}
546 
547 	skb->dev = priv->netdev;
548 	skb->protocol = eth_type_trans(skb, priv->netdev);
549 	skb->ip_summed = CHECKSUM_NONE;
550 
551 	/* This is required only in case of 11n and USB/PCIE as we alloc
552 	 * a buffer of 4K only if its 11N (to be able to receive 4K
553 	 * AMSDU packets). In case of SD we allocate buffers based
554 	 * on the size of packet and hence this is not needed.
555 	 *
556 	 * Modifying the truesize here as our allocation for each
557 	 * skb is 4K but we only receive 2K packets and this cause
558 	 * the kernel to start dropping packets in case where
559 	 * application has allocated buffer based on 2K size i.e.
560 	 * if there a 64K packet received (in IP fragments and
561 	 * application allocates 64K to receive this packet but
562 	 * this packet would almost double up because we allocate
563 	 * each 1.5K fragment in 4K and pass it up. As soon as the
564 	 * 64K limit hits kernel will start to drop rest of the
565 	 * fragments. Currently we fail the Filesndl-ht.scr script
566 	 * for UDP, hence this fix
567 	 */
568 	if ((priv->adapter->iface_type == MWIFIEX_USB ||
569 	     priv->adapter->iface_type == MWIFIEX_PCIE) &&
570 	    (skb->truesize > MWIFIEX_RX_DATA_BUF_SIZE))
571 		skb->truesize += (skb->len - MWIFIEX_RX_DATA_BUF_SIZE);
572 
573 	netif_rx(skb);
574 	return 0;
575 }
576 
577 /*
578  * IOCTL completion callback handler.
579  *
580  * This function is called when a pending IOCTL is completed.
581  *
582  * If work queue support is enabled, the function wakes up the
583  * corresponding waiting function. Otherwise, it processes the
584  * IOCTL response and frees the response buffer.
585  */
mwifiex_complete_cmd(struct mwifiex_adapter * adapter,struct cmd_ctrl_node * cmd_node)586 int mwifiex_complete_cmd(struct mwifiex_adapter *adapter,
587 			 struct cmd_ctrl_node *cmd_node)
588 {
589 	WARN_ON(!cmd_node->wait_q_enabled);
590 	mwifiex_dbg(adapter, CMD, "cmd completed: status=%d\n",
591 		    adapter->cmd_wait_q.status);
592 
593 	*cmd_node->condition = true;
594 	wake_up_interruptible(&adapter->cmd_wait_q.wait);
595 
596 	return 0;
597 }
598 
599 /* This function will return the pointer to station entry in station list
600  * table which matches specified mac address.
601  * This function should be called after acquiring RA list spinlock.
602  * NULL is returned if station entry is not found in associated STA list.
603  */
604 struct mwifiex_sta_node *
mwifiex_get_sta_entry(struct mwifiex_private * priv,const u8 * mac)605 mwifiex_get_sta_entry(struct mwifiex_private *priv, const u8 *mac)
606 {
607 	struct mwifiex_sta_node *node;
608 
609 	if (!mac)
610 		return NULL;
611 
612 	list_for_each_entry(node, &priv->sta_list, list) {
613 		if (!memcmp(node->mac_addr, mac, ETH_ALEN))
614 			return node;
615 	}
616 
617 	return NULL;
618 }
619 
620 static struct mwifiex_sta_node *
mwifiex_get_tdls_sta_entry(struct mwifiex_private * priv,u8 status)621 mwifiex_get_tdls_sta_entry(struct mwifiex_private *priv, u8 status)
622 {
623 	struct mwifiex_sta_node *node;
624 
625 	list_for_each_entry(node, &priv->sta_list, list) {
626 		if (node->tdls_status == status)
627 			return node;
628 	}
629 
630 	return NULL;
631 }
632 
633 /* If tdls channel switching is on-going, tx data traffic should be
634  * blocked until the switching stage completed.
635  */
mwifiex_is_tdls_chan_switching(struct mwifiex_private * priv)636 u8 mwifiex_is_tdls_chan_switching(struct mwifiex_private *priv)
637 {
638 	struct mwifiex_sta_node *sta_ptr;
639 
640 	if (!priv || !ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info))
641 		return false;
642 
643 	sta_ptr = mwifiex_get_tdls_sta_entry(priv, TDLS_CHAN_SWITCHING);
644 	if (sta_ptr)
645 		return true;
646 
647 	return false;
648 }
649 
mwifiex_is_tdls_off_chan(struct mwifiex_private * priv)650 static u8 mwifiex_is_tdls_off_chan(struct mwifiex_private *priv)
651 {
652 	struct mwifiex_sta_node *sta_ptr;
653 
654 	if (!priv || !ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info))
655 		return false;
656 
657 	sta_ptr = mwifiex_get_tdls_sta_entry(priv, TDLS_IN_OFF_CHAN);
658 	if (sta_ptr)
659 		return true;
660 
661 	return false;
662 }
663 
664 /* If tdls channel switching is on-going or tdls operate on off-channel,
665  * cmd path should be blocked until tdls switched to base-channel.
666  */
mwifiex_is_send_cmd_allowed(struct mwifiex_private * priv)667 u8 mwifiex_is_send_cmd_allowed(struct mwifiex_private *priv)
668 {
669 	if (!priv || !ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info))
670 		return true;
671 
672 	if (mwifiex_is_tdls_chan_switching(priv) ||
673 	    mwifiex_is_tdls_off_chan(priv))
674 		return false;
675 
676 	return true;
677 }
678 
679 /* This function will add a sta_node entry to associated station list
680  * table with the given mac address.
681  * If entry exist already, existing entry is returned.
682  * If received mac address is NULL, NULL is returned.
683  */
684 struct mwifiex_sta_node *
mwifiex_add_sta_entry(struct mwifiex_private * priv,const u8 * mac)685 mwifiex_add_sta_entry(struct mwifiex_private *priv, const u8 *mac)
686 {
687 	struct mwifiex_sta_node *node;
688 
689 	if (!mac)
690 		return NULL;
691 
692 	spin_lock_bh(&priv->sta_list_spinlock);
693 	node = mwifiex_get_sta_entry(priv, mac);
694 	if (node)
695 		goto done;
696 
697 	node = kzalloc(sizeof(*node), GFP_ATOMIC);
698 	if (!node)
699 		goto done;
700 
701 	memcpy(node->mac_addr, mac, ETH_ALEN);
702 	list_add_tail(&node->list, &priv->sta_list);
703 
704 done:
705 	spin_unlock_bh(&priv->sta_list_spinlock);
706 	return node;
707 }
708 
709 /* This function will search for HT IE in association request IEs
710  * and set station HT parameters accordingly.
711  */
712 void
mwifiex_set_sta_ht_cap(struct mwifiex_private * priv,const u8 * ies,int ies_len,struct mwifiex_sta_node * node)713 mwifiex_set_sta_ht_cap(struct mwifiex_private *priv, const u8 *ies,
714 		       int ies_len, struct mwifiex_sta_node *node)
715 {
716 	struct ieee_types_header *ht_cap_ie;
717 	const struct ieee80211_ht_cap *ht_cap;
718 
719 	if (!ies)
720 		return;
721 
722 	ht_cap_ie = (void *)cfg80211_find_ie(WLAN_EID_HT_CAPABILITY, ies,
723 					     ies_len);
724 	if (ht_cap_ie) {
725 		ht_cap = (void *)(ht_cap_ie + 1);
726 		node->is_11n_enabled = 1;
727 		node->max_amsdu = le16_to_cpu(ht_cap->cap_info) &
728 				  IEEE80211_HT_CAP_MAX_AMSDU ?
729 				  MWIFIEX_TX_DATA_BUF_SIZE_8K :
730 				  MWIFIEX_TX_DATA_BUF_SIZE_4K;
731 	} else {
732 		node->is_11n_enabled = 0;
733 	}
734 
735 	return;
736 }
737 
738 /* This function will delete a station entry from station list */
mwifiex_del_sta_entry(struct mwifiex_private * priv,const u8 * mac)739 void mwifiex_del_sta_entry(struct mwifiex_private *priv, const u8 *mac)
740 {
741 	struct mwifiex_sta_node *node;
742 
743 	spin_lock_bh(&priv->sta_list_spinlock);
744 
745 	node = mwifiex_get_sta_entry(priv, mac);
746 	if (node) {
747 		list_del(&node->list);
748 		kfree(node);
749 	}
750 
751 	spin_unlock_bh(&priv->sta_list_spinlock);
752 	return;
753 }
754 
755 /* This function will delete all stations from associated station list. */
mwifiex_del_all_sta_list(struct mwifiex_private * priv)756 void mwifiex_del_all_sta_list(struct mwifiex_private *priv)
757 {
758 	struct mwifiex_sta_node *node, *tmp;
759 
760 	spin_lock_bh(&priv->sta_list_spinlock);
761 
762 	list_for_each_entry_safe(node, tmp, &priv->sta_list, list) {
763 		list_del(&node->list);
764 		kfree(node);
765 	}
766 
767 	INIT_LIST_HEAD(&priv->sta_list);
768 	spin_unlock_bh(&priv->sta_list_spinlock);
769 	return;
770 }
771 
772 /* This function adds histogram data to histogram array*/
mwifiex_hist_data_add(struct mwifiex_private * priv,u8 rx_rate,s8 snr,s8 nflr)773 void mwifiex_hist_data_add(struct mwifiex_private *priv,
774 			   u8 rx_rate, s8 snr, s8 nflr)
775 {
776 	struct mwifiex_histogram_data *phist_data = priv->hist_data;
777 
778 	if (atomic_read(&phist_data->num_samples) > MWIFIEX_HIST_MAX_SAMPLES)
779 		mwifiex_hist_data_reset(priv);
780 	mwifiex_hist_data_set(priv, rx_rate, snr, nflr);
781 }
782 
783 /* function to add histogram record */
mwifiex_hist_data_set(struct mwifiex_private * priv,u8 rx_rate,s8 snr,s8 nflr)784 void mwifiex_hist_data_set(struct mwifiex_private *priv, u8 rx_rate, s8 snr,
785 			   s8 nflr)
786 {
787 	struct mwifiex_histogram_data *phist_data = priv->hist_data;
788 	s8 nf   = -nflr;
789 	s8 rssi = snr - nflr;
790 
791 	atomic_inc(&phist_data->num_samples);
792 	atomic_inc(&phist_data->rx_rate[rx_rate]);
793 	atomic_inc(&phist_data->snr[snr + 128]);
794 	atomic_inc(&phist_data->noise_flr[nf + 128]);
795 	atomic_inc(&phist_data->sig_str[rssi + 128]);
796 }
797 
798 /* function to reset histogram data during init/reset */
mwifiex_hist_data_reset(struct mwifiex_private * priv)799 void mwifiex_hist_data_reset(struct mwifiex_private *priv)
800 {
801 	int ix;
802 	struct mwifiex_histogram_data *phist_data = priv->hist_data;
803 
804 	atomic_set(&phist_data->num_samples, 0);
805 	for (ix = 0; ix < MWIFIEX_MAX_AC_RX_RATES; ix++)
806 		atomic_set(&phist_data->rx_rate[ix], 0);
807 	for (ix = 0; ix < MWIFIEX_MAX_SNR; ix++)
808 		atomic_set(&phist_data->snr[ix], 0);
809 	for (ix = 0; ix < MWIFIEX_MAX_NOISE_FLR; ix++)
810 		atomic_set(&phist_data->noise_flr[ix], 0);
811 	for (ix = 0; ix < MWIFIEX_MAX_SIG_STRENGTH; ix++)
812 		atomic_set(&phist_data->sig_str[ix], 0);
813 }
814 
mwifiex_alloc_dma_align_buf(int rx_len,gfp_t flags)815 void *mwifiex_alloc_dma_align_buf(int rx_len, gfp_t flags)
816 {
817 	struct sk_buff *skb;
818 	int buf_len, pad;
819 
820 	buf_len = rx_len + MWIFIEX_RX_HEADROOM + MWIFIEX_DMA_ALIGN_SZ;
821 
822 	skb = __dev_alloc_skb(buf_len, flags);
823 
824 	if (!skb)
825 		return NULL;
826 
827 	skb_reserve(skb, MWIFIEX_RX_HEADROOM);
828 
829 	pad = MWIFIEX_ALIGN_ADDR(skb->data, MWIFIEX_DMA_ALIGN_SZ) -
830 	      (long)skb->data;
831 
832 	skb_reserve(skb, pad);
833 
834 	return skb;
835 }
836 EXPORT_SYMBOL_GPL(mwifiex_alloc_dma_align_buf);
837 
mwifiex_fw_dump_event(struct mwifiex_private * priv)838 void mwifiex_fw_dump_event(struct mwifiex_private *priv)
839 {
840 	mwifiex_send_cmd(priv, HostCmd_CMD_FW_DUMP_EVENT, HostCmd_ACT_GEN_SET,
841 			 0, NULL, true);
842 }
843 EXPORT_SYMBOL_GPL(mwifiex_fw_dump_event);
844