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