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