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