xref: /linux/drivers/net/wireless/marvell/mwifiex/util.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
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 		wiphy_lock(priv->wdev.wiphy);
498 		cfg80211_rx_mlme_mgmt(priv->netdev, skb->data, pkt_len);
499 		wiphy_unlock(priv->wdev.wiphy);
500 	}
501 
502 	if (priv->adapter->host_mlme_enabled &&
503 	    (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP)) {
504 		if (ieee80211_is_auth(ieee_hdr->frame_control))
505 			mwifiex_dbg(priv->adapter, MSG,
506 				    "auth: receive auth from %pM\n",
507 				    ieee_hdr->addr2);
508 		if (ieee80211_is_deauth(ieee_hdr->frame_control))
509 			mwifiex_dbg(priv->adapter, MSG,
510 				    "auth: receive deauth from %pM\n",
511 				    ieee_hdr->addr2);
512 		if (ieee80211_is_disassoc(ieee_hdr->frame_control))
513 			mwifiex_dbg(priv->adapter, MSG,
514 				    "assoc: receive disassoc from %pM\n",
515 				    ieee_hdr->addr2);
516 		if (ieee80211_is_assoc_req(ieee_hdr->frame_control))
517 			mwifiex_dbg(priv->adapter, MSG,
518 				    "assoc: receive assoc req from %pM\n",
519 				    ieee_hdr->addr2);
520 		if (ieee80211_is_reassoc_req(ieee_hdr->frame_control))
521 			mwifiex_dbg(priv->adapter, MSG,
522 				    "assoc: receive reassoc req from %pM\n",
523 				    ieee_hdr->addr2);
524 	}
525 
526 	cfg80211_rx_mgmt(&priv->wdev, priv->roc_cfg.chan.center_freq,
527 			 CAL_RSSI(rx_pd->snr, rx_pd->nf), skb->data, pkt_len,
528 			 0);
529 
530 	return 0;
531 }
532 
533 /*
534  * This function processes the received packet before sending it to the
535  * kernel.
536  *
537  * It extracts the SKB from the received buffer and sends it to kernel.
538  * In case the received buffer does not contain the data in SKB format,
539  * the function creates a blank SKB, fills it with the data from the
540  * received buffer and then sends this new SKB to the kernel.
541  */
542 int mwifiex_recv_packet(struct mwifiex_private *priv, struct sk_buff *skb)
543 {
544 	struct mwifiex_sta_node *src_node;
545 	struct ethhdr *p_ethhdr;
546 
547 	if (!skb)
548 		return -1;
549 
550 	priv->stats.rx_bytes += skb->len;
551 	priv->stats.rx_packets++;
552 
553 	if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP) {
554 		p_ethhdr = (void *)skb->data;
555 		src_node = mwifiex_get_sta_entry(priv, p_ethhdr->h_source);
556 		if (src_node) {
557 			src_node->stats.last_rx = jiffies;
558 			src_node->stats.rx_bytes += skb->len;
559 			src_node->stats.rx_packets++;
560 		}
561 	}
562 
563 	skb->dev = priv->netdev;
564 	skb->protocol = eth_type_trans(skb, priv->netdev);
565 	skb->ip_summed = CHECKSUM_NONE;
566 
567 	/* This is required only in case of 11n and USB/PCIE as we alloc
568 	 * a buffer of 4K only if its 11N (to be able to receive 4K
569 	 * AMSDU packets). In case of SD we allocate buffers based
570 	 * on the size of packet and hence this is not needed.
571 	 *
572 	 * Modifying the truesize here as our allocation for each
573 	 * skb is 4K but we only receive 2K packets and this cause
574 	 * the kernel to start dropping packets in case where
575 	 * application has allocated buffer based on 2K size i.e.
576 	 * if there a 64K packet received (in IP fragments and
577 	 * application allocates 64K to receive this packet but
578 	 * this packet would almost double up because we allocate
579 	 * each 1.5K fragment in 4K and pass it up. As soon as the
580 	 * 64K limit hits kernel will start to drop rest of the
581 	 * fragments. Currently we fail the Filesndl-ht.scr script
582 	 * for UDP, hence this fix
583 	 */
584 	if ((priv->adapter->iface_type == MWIFIEX_USB ||
585 	     priv->adapter->iface_type == MWIFIEX_PCIE) &&
586 	    (skb->truesize > MWIFIEX_RX_DATA_BUF_SIZE))
587 		skb->truesize += (skb->len - MWIFIEX_RX_DATA_BUF_SIZE);
588 
589 	netif_rx(skb);
590 	return 0;
591 }
592 
593 /*
594  * IOCTL completion callback handler.
595  *
596  * This function is called when a pending IOCTL is completed.
597  *
598  * If work queue support is enabled, the function wakes up the
599  * corresponding waiting function. Otherwise, it processes the
600  * IOCTL response and frees the response buffer.
601  */
602 int mwifiex_complete_cmd(struct mwifiex_adapter *adapter,
603 			 struct cmd_ctrl_node *cmd_node)
604 {
605 	WARN_ON(!cmd_node->wait_q_enabled);
606 	mwifiex_dbg(adapter, CMD, "cmd completed: status=%d\n",
607 		    adapter->cmd_wait_q.status);
608 
609 	*cmd_node->condition = true;
610 	wake_up_interruptible(&adapter->cmd_wait_q.wait);
611 
612 	return 0;
613 }
614 
615 /* This function will return the pointer to station entry in station list
616  * table which matches specified mac address.
617  * This function should be called after acquiring RA list spinlock.
618  * NULL is returned if station entry is not found in associated STA list.
619  */
620 struct mwifiex_sta_node *
621 mwifiex_get_sta_entry(struct mwifiex_private *priv, const u8 *mac)
622 {
623 	struct mwifiex_sta_node *node;
624 
625 	if (!mac)
626 		return NULL;
627 
628 	list_for_each_entry(node, &priv->sta_list, list) {
629 		if (!memcmp(node->mac_addr, mac, ETH_ALEN))
630 			return node;
631 	}
632 
633 	return NULL;
634 }
635 
636 static struct mwifiex_sta_node *
637 mwifiex_get_tdls_sta_entry(struct mwifiex_private *priv, u8 status)
638 {
639 	struct mwifiex_sta_node *node;
640 
641 	list_for_each_entry(node, &priv->sta_list, list) {
642 		if (node->tdls_status == status)
643 			return node;
644 	}
645 
646 	return NULL;
647 }
648 
649 /* If tdls channel switching is on-going, tx data traffic should be
650  * blocked until the switching stage completed.
651  */
652 u8 mwifiex_is_tdls_chan_switching(struct mwifiex_private *priv)
653 {
654 	struct mwifiex_sta_node *sta_ptr;
655 
656 	if (!priv || !ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info))
657 		return false;
658 
659 	sta_ptr = mwifiex_get_tdls_sta_entry(priv, TDLS_CHAN_SWITCHING);
660 	if (sta_ptr)
661 		return true;
662 
663 	return false;
664 }
665 
666 u8 mwifiex_is_tdls_off_chan(struct mwifiex_private *priv)
667 {
668 	struct mwifiex_sta_node *sta_ptr;
669 
670 	if (!priv || !ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info))
671 		return false;
672 
673 	sta_ptr = mwifiex_get_tdls_sta_entry(priv, TDLS_IN_OFF_CHAN);
674 	if (sta_ptr)
675 		return true;
676 
677 	return false;
678 }
679 
680 /* If tdls channel switching is on-going or tdls operate on off-channel,
681  * cmd path should be blocked until tdls switched to base-channel.
682  */
683 u8 mwifiex_is_send_cmd_allowed(struct mwifiex_private *priv)
684 {
685 	if (!priv || !ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info))
686 		return true;
687 
688 	if (mwifiex_is_tdls_chan_switching(priv) ||
689 	    mwifiex_is_tdls_off_chan(priv))
690 		return false;
691 
692 	return true;
693 }
694 
695 /* This function will add a sta_node entry to associated station list
696  * table with the given mac address.
697  * If entry exist already, existing entry is returned.
698  * If received mac address is NULL, NULL is returned.
699  */
700 struct mwifiex_sta_node *
701 mwifiex_add_sta_entry(struct mwifiex_private *priv, const u8 *mac)
702 {
703 	struct mwifiex_sta_node *node;
704 
705 	if (!mac)
706 		return NULL;
707 
708 	spin_lock_bh(&priv->sta_list_spinlock);
709 	node = mwifiex_get_sta_entry(priv, mac);
710 	if (node)
711 		goto done;
712 
713 	node = kzalloc(sizeof(*node), GFP_ATOMIC);
714 	if (!node)
715 		goto done;
716 
717 	memcpy(node->mac_addr, mac, ETH_ALEN);
718 	list_add_tail(&node->list, &priv->sta_list);
719 
720 done:
721 	spin_unlock_bh(&priv->sta_list_spinlock);
722 	return node;
723 }
724 
725 /* This function will search for HT IE in association request IEs
726  * and set station HT parameters accordingly.
727  */
728 void
729 mwifiex_set_sta_ht_cap(struct mwifiex_private *priv, const u8 *ies,
730 		       int ies_len, struct mwifiex_sta_node *node)
731 {
732 	struct ieee_types_header *ht_cap_ie;
733 	const struct ieee80211_ht_cap *ht_cap;
734 
735 	if (!ies)
736 		return;
737 
738 	ht_cap_ie = (void *)cfg80211_find_ie(WLAN_EID_HT_CAPABILITY, ies,
739 					     ies_len);
740 	if (ht_cap_ie) {
741 		ht_cap = (void *)(ht_cap_ie + 1);
742 		node->is_11n_enabled = 1;
743 		node->max_amsdu = le16_to_cpu(ht_cap->cap_info) &
744 				  IEEE80211_HT_CAP_MAX_AMSDU ?
745 				  MWIFIEX_TX_DATA_BUF_SIZE_8K :
746 				  MWIFIEX_TX_DATA_BUF_SIZE_4K;
747 	} else {
748 		node->is_11n_enabled = 0;
749 	}
750 
751 	return;
752 }
753 
754 /* This function will delete a station entry from station list */
755 void mwifiex_del_sta_entry(struct mwifiex_private *priv, const u8 *mac)
756 {
757 	struct mwifiex_sta_node *node;
758 
759 	spin_lock_bh(&priv->sta_list_spinlock);
760 
761 	node = mwifiex_get_sta_entry(priv, mac);
762 	if (node) {
763 		list_del(&node->list);
764 		kfree(node);
765 	}
766 
767 	spin_unlock_bh(&priv->sta_list_spinlock);
768 	return;
769 }
770 
771 /* This function will delete all stations from associated station list. */
772 void mwifiex_del_all_sta_list(struct mwifiex_private *priv)
773 {
774 	struct mwifiex_sta_node *node, *tmp;
775 
776 	spin_lock_bh(&priv->sta_list_spinlock);
777 
778 	list_for_each_entry_safe(node, tmp, &priv->sta_list, list) {
779 		list_del(&node->list);
780 		kfree(node);
781 	}
782 
783 	INIT_LIST_HEAD(&priv->sta_list);
784 	spin_unlock_bh(&priv->sta_list_spinlock);
785 	return;
786 }
787 
788 /* This function adds histogram data to histogram array*/
789 void mwifiex_hist_data_add(struct mwifiex_private *priv,
790 			   u8 rx_rate, s8 snr, s8 nflr)
791 {
792 	struct mwifiex_histogram_data *phist_data = priv->hist_data;
793 
794 	if (atomic_read(&phist_data->num_samples) > MWIFIEX_HIST_MAX_SAMPLES)
795 		mwifiex_hist_data_reset(priv);
796 	mwifiex_hist_data_set(priv, rx_rate, snr, nflr);
797 }
798 
799 /* function to add histogram record */
800 void mwifiex_hist_data_set(struct mwifiex_private *priv, u8 rx_rate, s8 snr,
801 			   s8 nflr)
802 {
803 	struct mwifiex_histogram_data *phist_data = priv->hist_data;
804 	s8 nf   = -nflr;
805 	s8 rssi = snr - nflr;
806 
807 	atomic_inc(&phist_data->num_samples);
808 	atomic_inc(&phist_data->rx_rate[rx_rate]);
809 	atomic_inc(&phist_data->snr[snr + 128]);
810 	atomic_inc(&phist_data->noise_flr[nf + 128]);
811 	atomic_inc(&phist_data->sig_str[rssi + 128]);
812 }
813 
814 /* function to reset histogram data during init/reset */
815 void mwifiex_hist_data_reset(struct mwifiex_private *priv)
816 {
817 	int ix;
818 	struct mwifiex_histogram_data *phist_data = priv->hist_data;
819 
820 	atomic_set(&phist_data->num_samples, 0);
821 	for (ix = 0; ix < MWIFIEX_MAX_AC_RX_RATES; ix++)
822 		atomic_set(&phist_data->rx_rate[ix], 0);
823 	for (ix = 0; ix < MWIFIEX_MAX_SNR; ix++)
824 		atomic_set(&phist_data->snr[ix], 0);
825 	for (ix = 0; ix < MWIFIEX_MAX_NOISE_FLR; ix++)
826 		atomic_set(&phist_data->noise_flr[ix], 0);
827 	for (ix = 0; ix < MWIFIEX_MAX_SIG_STRENGTH; ix++)
828 		atomic_set(&phist_data->sig_str[ix], 0);
829 }
830 
831 void *mwifiex_alloc_dma_align_buf(int rx_len, gfp_t flags)
832 {
833 	struct sk_buff *skb;
834 	int buf_len, pad;
835 
836 	buf_len = rx_len + MWIFIEX_RX_HEADROOM + MWIFIEX_DMA_ALIGN_SZ;
837 
838 	skb = __dev_alloc_skb(buf_len, flags);
839 
840 	if (!skb)
841 		return NULL;
842 
843 	skb_reserve(skb, MWIFIEX_RX_HEADROOM);
844 
845 	pad = MWIFIEX_ALIGN_ADDR(skb->data, MWIFIEX_DMA_ALIGN_SZ) -
846 	      (long)skb->data;
847 
848 	skb_reserve(skb, pad);
849 
850 	return skb;
851 }
852 EXPORT_SYMBOL_GPL(mwifiex_alloc_dma_align_buf);
853 
854 void mwifiex_fw_dump_event(struct mwifiex_private *priv)
855 {
856 	mwifiex_send_cmd(priv, HostCmd_CMD_FW_DUMP_EVENT, HostCmd_ACT_GEN_SET,
857 			 0, NULL, true);
858 }
859 EXPORT_SYMBOL_GPL(mwifiex_fw_dump_event);
860