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