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 processes the received management packet and send it
375  * to the kernel.
376  */
377 int
378 mwifiex_process_mgmt_packet(struct mwifiex_private *priv,
379 			    struct sk_buff *skb)
380 {
381 	struct rxpd *rx_pd;
382 	u16 pkt_len;
383 	struct ieee80211_hdr *ieee_hdr;
384 
385 	if (!skb)
386 		return -1;
387 
388 	if (!priv->mgmt_frame_mask ||
389 	    priv->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED) {
390 		mwifiex_dbg(priv->adapter, ERROR,
391 			    "do not receive mgmt frames on uninitialized intf");
392 		return -1;
393 	}
394 
395 	rx_pd = (struct rxpd *)skb->data;
396 	pkt_len = le16_to_cpu(rx_pd->rx_pkt_length);
397 	if (pkt_len < sizeof(struct ieee80211_hdr) + sizeof(pkt_len)) {
398 		mwifiex_dbg(priv->adapter, ERROR, "invalid rx_pkt_length");
399 		return -1;
400 	}
401 
402 	skb_pull(skb, le16_to_cpu(rx_pd->rx_pkt_offset));
403 	skb_pull(skb, sizeof(pkt_len));
404 	pkt_len -= sizeof(pkt_len);
405 
406 	ieee_hdr = (void *)skb->data;
407 	if (ieee80211_is_mgmt(ieee_hdr->frame_control)) {
408 		if (mwifiex_parse_mgmt_packet(priv, (u8 *)ieee_hdr,
409 					      pkt_len, rx_pd))
410 			return -1;
411 	}
412 	/* Remove address4 */
413 	memmove(skb->data + sizeof(struct ieee80211_hdr_3addr),
414 		skb->data + sizeof(struct ieee80211_hdr),
415 		pkt_len - sizeof(struct ieee80211_hdr));
416 
417 	pkt_len -= ETH_ALEN;
418 	rx_pd->rx_pkt_length = cpu_to_le16(pkt_len);
419 
420 	cfg80211_rx_mgmt(&priv->wdev, priv->roc_cfg.chan.center_freq,
421 			 CAL_RSSI(rx_pd->snr, rx_pd->nf), skb->data, pkt_len,
422 			 0);
423 
424 	return 0;
425 }
426 
427 /*
428  * This function processes the received packet before sending it to the
429  * kernel.
430  *
431  * It extracts the SKB from the received buffer and sends it to kernel.
432  * In case the received buffer does not contain the data in SKB format,
433  * the function creates a blank SKB, fills it with the data from the
434  * received buffer and then sends this new SKB to the kernel.
435  */
436 int mwifiex_recv_packet(struct mwifiex_private *priv, struct sk_buff *skb)
437 {
438 	struct mwifiex_sta_node *src_node;
439 	struct ethhdr *p_ethhdr;
440 
441 	if (!skb)
442 		return -1;
443 
444 	priv->stats.rx_bytes += skb->len;
445 	priv->stats.rx_packets++;
446 
447 	if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP) {
448 		p_ethhdr = (void *)skb->data;
449 		src_node = mwifiex_get_sta_entry(priv, p_ethhdr->h_source);
450 		if (src_node) {
451 			src_node->stats.last_rx = jiffies;
452 			src_node->stats.rx_bytes += skb->len;
453 			src_node->stats.rx_packets++;
454 		}
455 	}
456 
457 	skb->dev = priv->netdev;
458 	skb->protocol = eth_type_trans(skb, priv->netdev);
459 	skb->ip_summed = CHECKSUM_NONE;
460 
461 	/* This is required only in case of 11n and USB/PCIE as we alloc
462 	 * a buffer of 4K only if its 11N (to be able to receive 4K
463 	 * AMSDU packets). In case of SD we allocate buffers based
464 	 * on the size of packet and hence this is not needed.
465 	 *
466 	 * Modifying the truesize here as our allocation for each
467 	 * skb is 4K but we only receive 2K packets and this cause
468 	 * the kernel to start dropping packets in case where
469 	 * application has allocated buffer based on 2K size i.e.
470 	 * if there a 64K packet received (in IP fragments and
471 	 * application allocates 64K to receive this packet but
472 	 * this packet would almost double up because we allocate
473 	 * each 1.5K fragment in 4K and pass it up. As soon as the
474 	 * 64K limit hits kernel will start to drop rest of the
475 	 * fragments. Currently we fail the Filesndl-ht.scr script
476 	 * for UDP, hence this fix
477 	 */
478 	if ((priv->adapter->iface_type == MWIFIEX_USB ||
479 	     priv->adapter->iface_type == MWIFIEX_PCIE) &&
480 	    (skb->truesize > MWIFIEX_RX_DATA_BUF_SIZE))
481 		skb->truesize += (skb->len - MWIFIEX_RX_DATA_BUF_SIZE);
482 
483 	netif_rx(skb);
484 	return 0;
485 }
486 
487 /*
488  * IOCTL completion callback handler.
489  *
490  * This function is called when a pending IOCTL is completed.
491  *
492  * If work queue support is enabled, the function wakes up the
493  * corresponding waiting function. Otherwise, it processes the
494  * IOCTL response and frees the response buffer.
495  */
496 int mwifiex_complete_cmd(struct mwifiex_adapter *adapter,
497 			 struct cmd_ctrl_node *cmd_node)
498 {
499 	WARN_ON(!cmd_node->wait_q_enabled);
500 	mwifiex_dbg(adapter, CMD, "cmd completed: status=%d\n",
501 		    adapter->cmd_wait_q.status);
502 
503 	*cmd_node->condition = true;
504 	wake_up_interruptible(&adapter->cmd_wait_q.wait);
505 
506 	return 0;
507 }
508 
509 /* This function will return the pointer to station entry in station list
510  * table which matches specified mac address.
511  * This function should be called after acquiring RA list spinlock.
512  * NULL is returned if station entry is not found in associated STA list.
513  */
514 struct mwifiex_sta_node *
515 mwifiex_get_sta_entry(struct mwifiex_private *priv, const u8 *mac)
516 {
517 	struct mwifiex_sta_node *node;
518 
519 	if (!mac)
520 		return NULL;
521 
522 	list_for_each_entry(node, &priv->sta_list, list) {
523 		if (!memcmp(node->mac_addr, mac, ETH_ALEN))
524 			return node;
525 	}
526 
527 	return NULL;
528 }
529 
530 static struct mwifiex_sta_node *
531 mwifiex_get_tdls_sta_entry(struct mwifiex_private *priv, u8 status)
532 {
533 	struct mwifiex_sta_node *node;
534 
535 	list_for_each_entry(node, &priv->sta_list, list) {
536 		if (node->tdls_status == status)
537 			return node;
538 	}
539 
540 	return NULL;
541 }
542 
543 /* If tdls channel switching is on-going, tx data traffic should be
544  * blocked until the switching stage completed.
545  */
546 u8 mwifiex_is_tdls_chan_switching(struct mwifiex_private *priv)
547 {
548 	struct mwifiex_sta_node *sta_ptr;
549 
550 	if (!priv || !ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info))
551 		return false;
552 
553 	sta_ptr = mwifiex_get_tdls_sta_entry(priv, TDLS_CHAN_SWITCHING);
554 	if (sta_ptr)
555 		return true;
556 
557 	return false;
558 }
559 
560 u8 mwifiex_is_tdls_off_chan(struct mwifiex_private *priv)
561 {
562 	struct mwifiex_sta_node *sta_ptr;
563 
564 	if (!priv || !ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info))
565 		return false;
566 
567 	sta_ptr = mwifiex_get_tdls_sta_entry(priv, TDLS_IN_OFF_CHAN);
568 	if (sta_ptr)
569 		return true;
570 
571 	return false;
572 }
573 
574 /* If tdls channel switching is on-going or tdls operate on off-channel,
575  * cmd path should be blocked until tdls switched to base-channel.
576  */
577 u8 mwifiex_is_send_cmd_allowed(struct mwifiex_private *priv)
578 {
579 	if (!priv || !ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info))
580 		return true;
581 
582 	if (mwifiex_is_tdls_chan_switching(priv) ||
583 	    mwifiex_is_tdls_off_chan(priv))
584 		return false;
585 
586 	return true;
587 }
588 
589 /* This function will add a sta_node entry to associated station list
590  * table with the given mac address.
591  * If entry exist already, existing entry is returned.
592  * If received mac address is NULL, NULL is returned.
593  */
594 struct mwifiex_sta_node *
595 mwifiex_add_sta_entry(struct mwifiex_private *priv, const u8 *mac)
596 {
597 	struct mwifiex_sta_node *node;
598 
599 	if (!mac)
600 		return NULL;
601 
602 	spin_lock_bh(&priv->sta_list_spinlock);
603 	node = mwifiex_get_sta_entry(priv, mac);
604 	if (node)
605 		goto done;
606 
607 	node = kzalloc(sizeof(*node), GFP_ATOMIC);
608 	if (!node)
609 		goto done;
610 
611 	memcpy(node->mac_addr, mac, ETH_ALEN);
612 	list_add_tail(&node->list, &priv->sta_list);
613 
614 done:
615 	spin_unlock_bh(&priv->sta_list_spinlock);
616 	return node;
617 }
618 
619 /* This function will search for HT IE in association request IEs
620  * and set station HT parameters accordingly.
621  */
622 void
623 mwifiex_set_sta_ht_cap(struct mwifiex_private *priv, const u8 *ies,
624 		       int ies_len, struct mwifiex_sta_node *node)
625 {
626 	struct ieee_types_header *ht_cap_ie;
627 	const struct ieee80211_ht_cap *ht_cap;
628 
629 	if (!ies)
630 		return;
631 
632 	ht_cap_ie = (void *)cfg80211_find_ie(WLAN_EID_HT_CAPABILITY, ies,
633 					     ies_len);
634 	if (ht_cap_ie) {
635 		ht_cap = (void *)(ht_cap_ie + 1);
636 		node->is_11n_enabled = 1;
637 		node->max_amsdu = le16_to_cpu(ht_cap->cap_info) &
638 				  IEEE80211_HT_CAP_MAX_AMSDU ?
639 				  MWIFIEX_TX_DATA_BUF_SIZE_8K :
640 				  MWIFIEX_TX_DATA_BUF_SIZE_4K;
641 	} else {
642 		node->is_11n_enabled = 0;
643 	}
644 
645 	return;
646 }
647 
648 /* This function will delete a station entry from station list */
649 void mwifiex_del_sta_entry(struct mwifiex_private *priv, const u8 *mac)
650 {
651 	struct mwifiex_sta_node *node;
652 
653 	spin_lock_bh(&priv->sta_list_spinlock);
654 
655 	node = mwifiex_get_sta_entry(priv, mac);
656 	if (node) {
657 		list_del(&node->list);
658 		kfree(node);
659 	}
660 
661 	spin_unlock_bh(&priv->sta_list_spinlock);
662 	return;
663 }
664 
665 /* This function will delete all stations from associated station list. */
666 void mwifiex_del_all_sta_list(struct mwifiex_private *priv)
667 {
668 	struct mwifiex_sta_node *node, *tmp;
669 
670 	spin_lock_bh(&priv->sta_list_spinlock);
671 
672 	list_for_each_entry_safe(node, tmp, &priv->sta_list, list) {
673 		list_del(&node->list);
674 		kfree(node);
675 	}
676 
677 	INIT_LIST_HEAD(&priv->sta_list);
678 	spin_unlock_bh(&priv->sta_list_spinlock);
679 	return;
680 }
681 
682 /* This function adds histogram data to histogram array*/
683 void mwifiex_hist_data_add(struct mwifiex_private *priv,
684 			   u8 rx_rate, s8 snr, s8 nflr)
685 {
686 	struct mwifiex_histogram_data *phist_data = priv->hist_data;
687 
688 	if (atomic_read(&phist_data->num_samples) > MWIFIEX_HIST_MAX_SAMPLES)
689 		mwifiex_hist_data_reset(priv);
690 	mwifiex_hist_data_set(priv, rx_rate, snr, nflr);
691 }
692 
693 /* function to add histogram record */
694 void mwifiex_hist_data_set(struct mwifiex_private *priv, u8 rx_rate, s8 snr,
695 			   s8 nflr)
696 {
697 	struct mwifiex_histogram_data *phist_data = priv->hist_data;
698 	s8 nf   = -nflr;
699 	s8 rssi = snr - nflr;
700 
701 	atomic_inc(&phist_data->num_samples);
702 	atomic_inc(&phist_data->rx_rate[rx_rate]);
703 	atomic_inc(&phist_data->snr[snr + 128]);
704 	atomic_inc(&phist_data->noise_flr[nf + 128]);
705 	atomic_inc(&phist_data->sig_str[rssi + 128]);
706 }
707 
708 /* function to reset histogram data during init/reset */
709 void mwifiex_hist_data_reset(struct mwifiex_private *priv)
710 {
711 	int ix;
712 	struct mwifiex_histogram_data *phist_data = priv->hist_data;
713 
714 	atomic_set(&phist_data->num_samples, 0);
715 	for (ix = 0; ix < MWIFIEX_MAX_AC_RX_RATES; ix++)
716 		atomic_set(&phist_data->rx_rate[ix], 0);
717 	for (ix = 0; ix < MWIFIEX_MAX_SNR; ix++)
718 		atomic_set(&phist_data->snr[ix], 0);
719 	for (ix = 0; ix < MWIFIEX_MAX_NOISE_FLR; ix++)
720 		atomic_set(&phist_data->noise_flr[ix], 0);
721 	for (ix = 0; ix < MWIFIEX_MAX_SIG_STRENGTH; ix++)
722 		atomic_set(&phist_data->sig_str[ix], 0);
723 }
724 
725 void *mwifiex_alloc_dma_align_buf(int rx_len, gfp_t flags)
726 {
727 	struct sk_buff *skb;
728 	int buf_len, pad;
729 
730 	buf_len = rx_len + MWIFIEX_RX_HEADROOM + MWIFIEX_DMA_ALIGN_SZ;
731 
732 	skb = __dev_alloc_skb(buf_len, flags);
733 
734 	if (!skb)
735 		return NULL;
736 
737 	skb_reserve(skb, MWIFIEX_RX_HEADROOM);
738 
739 	pad = MWIFIEX_ALIGN_ADDR(skb->data, MWIFIEX_DMA_ALIGN_SZ) -
740 	      (long)skb->data;
741 
742 	skb_reserve(skb, pad);
743 
744 	return skb;
745 }
746 EXPORT_SYMBOL_GPL(mwifiex_alloc_dma_align_buf);
747 
748 void mwifiex_fw_dump_event(struct mwifiex_private *priv)
749 {
750 	mwifiex_send_cmd(priv, HostCmd_CMD_FW_DUMP_EVENT, HostCmd_ACT_GEN_SET,
751 			 0, NULL, true);
752 }
753 EXPORT_SYMBOL_GPL(mwifiex_fw_dump_event);
754