util.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. /*
  2. * Marvell Wireless LAN device driver: utility functions
  3. *
  4. * Copyright (C) 2011-2014, Marvell International Ltd.
  5. *
  6. * This software file (the "File") is distributed by Marvell International
  7. * Ltd. under the terms of the GNU General Public License Version 2, June 1991
  8. * (the "License"). You may use, redistribute and/or modify this File in
  9. * accordance with the terms and conditions of the License, a copy of which
  10. * is available by writing to the Free Software Foundation, Inc.,
  11. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
  12. * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
  13. *
  14. * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
  16. * ARE EXPRESSLY DISCLAIMED. The License provides additional details about
  17. * this warranty disclaimer.
  18. */
  19. #include "decl.h"
  20. #include "ioctl.h"
  21. #include "util.h"
  22. #include "fw.h"
  23. #include "main.h"
  24. #include "wmm.h"
  25. #include "11n.h"
  26. static struct mwifiex_debug_data items[] = {
  27. {"debug_mask", item_size(debug_mask),
  28. item_addr(debug_mask), 1},
  29. {"int_counter", item_size(int_counter),
  30. item_addr(int_counter), 1},
  31. {"wmm_ac_vo", item_size(packets_out[WMM_AC_VO]),
  32. item_addr(packets_out[WMM_AC_VO]), 1},
  33. {"wmm_ac_vi", item_size(packets_out[WMM_AC_VI]),
  34. item_addr(packets_out[WMM_AC_VI]), 1},
  35. {"wmm_ac_be", item_size(packets_out[WMM_AC_BE]),
  36. item_addr(packets_out[WMM_AC_BE]), 1},
  37. {"wmm_ac_bk", item_size(packets_out[WMM_AC_BK]),
  38. item_addr(packets_out[WMM_AC_BK]), 1},
  39. {"tx_buf_size", item_size(tx_buf_size),
  40. item_addr(tx_buf_size), 1},
  41. {"curr_tx_buf_size", item_size(curr_tx_buf_size),
  42. item_addr(curr_tx_buf_size), 1},
  43. {"ps_mode", item_size(ps_mode),
  44. item_addr(ps_mode), 1},
  45. {"ps_state", item_size(ps_state),
  46. item_addr(ps_state), 1},
  47. {"is_deep_sleep", item_size(is_deep_sleep),
  48. item_addr(is_deep_sleep), 1},
  49. {"wakeup_dev_req", item_size(pm_wakeup_card_req),
  50. item_addr(pm_wakeup_card_req), 1},
  51. {"wakeup_tries", item_size(pm_wakeup_fw_try),
  52. item_addr(pm_wakeup_fw_try), 1},
  53. {"hs_configured", item_size(is_hs_configured),
  54. item_addr(is_hs_configured), 1},
  55. {"hs_activated", item_size(hs_activated),
  56. item_addr(hs_activated), 1},
  57. {"num_tx_timeout", item_size(num_tx_timeout),
  58. item_addr(num_tx_timeout), 1},
  59. {"is_cmd_timedout", item_size(is_cmd_timedout),
  60. item_addr(is_cmd_timedout), 1},
  61. {"timeout_cmd_id", item_size(timeout_cmd_id),
  62. item_addr(timeout_cmd_id), 1},
  63. {"timeout_cmd_act", item_size(timeout_cmd_act),
  64. item_addr(timeout_cmd_act), 1},
  65. {"last_cmd_id", item_size(last_cmd_id),
  66. item_addr(last_cmd_id), DBG_CMD_NUM},
  67. {"last_cmd_act", item_size(last_cmd_act),
  68. item_addr(last_cmd_act), DBG_CMD_NUM},
  69. {"last_cmd_index", item_size(last_cmd_index),
  70. item_addr(last_cmd_index), 1},
  71. {"last_cmd_resp_id", item_size(last_cmd_resp_id),
  72. item_addr(last_cmd_resp_id), DBG_CMD_NUM},
  73. {"last_cmd_resp_index", item_size(last_cmd_resp_index),
  74. item_addr(last_cmd_resp_index), 1},
  75. {"last_event", item_size(last_event),
  76. item_addr(last_event), DBG_CMD_NUM},
  77. {"last_event_index", item_size(last_event_index),
  78. item_addr(last_event_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. /* variables defined in struct mwifiex_adapter */
  107. {"cmd_pending", adapter_item_size(cmd_pending),
  108. adapter_item_addr(cmd_pending), 1},
  109. {"tx_pending", adapter_item_size(tx_pending),
  110. adapter_item_addr(tx_pending), 1},
  111. {"rx_pending", adapter_item_size(rx_pending),
  112. adapter_item_addr(rx_pending), 1},
  113. };
  114. static int num_of_items = ARRAY_SIZE(items);
  115. /*
  116. * Firmware initialization complete callback handler.
  117. *
  118. * This function wakes up the function waiting on the init
  119. * wait queue for the firmware initialization to complete.
  120. */
  121. int mwifiex_init_fw_complete(struct mwifiex_adapter *adapter)
  122. {
  123. adapter->init_wait_q_woken = true;
  124. wake_up_interruptible(&adapter->init_wait_q);
  125. return 0;
  126. }
  127. /*
  128. * Firmware shutdown complete callback handler.
  129. *
  130. * This function sets the hardware status to not ready and wakes up
  131. * the function waiting on the init wait queue for the firmware
  132. * shutdown to complete.
  133. */
  134. int mwifiex_shutdown_fw_complete(struct mwifiex_adapter *adapter)
  135. {
  136. adapter->hw_status = MWIFIEX_HW_STATUS_NOT_READY;
  137. adapter->init_wait_q_woken = true;
  138. wake_up_interruptible(&adapter->init_wait_q);
  139. return 0;
  140. }
  141. /*
  142. * This function sends init/shutdown command
  143. * to firmware.
  144. */
  145. int mwifiex_init_shutdown_fw(struct mwifiex_private *priv,
  146. u32 func_init_shutdown)
  147. {
  148. u16 cmd;
  149. if (func_init_shutdown == MWIFIEX_FUNC_INIT) {
  150. cmd = HostCmd_CMD_FUNC_INIT;
  151. } else if (func_init_shutdown == MWIFIEX_FUNC_SHUTDOWN) {
  152. cmd = HostCmd_CMD_FUNC_SHUTDOWN;
  153. } else {
  154. mwifiex_dbg(priv->adapter, ERROR,
  155. "unsupported parameter\n");
  156. return -1;
  157. }
  158. return mwifiex_send_cmd(priv, cmd, HostCmd_ACT_GEN_SET, 0, NULL, true);
  159. }
  160. EXPORT_SYMBOL_GPL(mwifiex_init_shutdown_fw);
  161. /*
  162. * IOCTL request handler to set/get debug information.
  163. *
  164. * This function collates/sets the information from/to different driver
  165. * structures.
  166. */
  167. int mwifiex_get_debug_info(struct mwifiex_private *priv,
  168. struct mwifiex_debug_info *info)
  169. {
  170. struct mwifiex_adapter *adapter = priv->adapter;
  171. if (info) {
  172. info->debug_mask = adapter->debug_mask;
  173. memcpy(info->packets_out,
  174. priv->wmm.packets_out,
  175. sizeof(priv->wmm.packets_out));
  176. info->curr_tx_buf_size = (u32) adapter->curr_tx_buf_size;
  177. info->tx_buf_size = (u32) adapter->tx_buf_size;
  178. info->rx_tbl_num = mwifiex_get_rx_reorder_tbl(priv,
  179. info->rx_tbl);
  180. info->tx_tbl_num = mwifiex_get_tx_ba_stream_tbl(priv,
  181. info->tx_tbl);
  182. info->tdls_peer_num = mwifiex_get_tdls_list(priv,
  183. info->tdls_list);
  184. info->ps_mode = adapter->ps_mode;
  185. info->ps_state = adapter->ps_state;
  186. info->is_deep_sleep = adapter->is_deep_sleep;
  187. info->pm_wakeup_card_req = adapter->pm_wakeup_card_req;
  188. info->pm_wakeup_fw_try = adapter->pm_wakeup_fw_try;
  189. info->is_hs_configured = adapter->is_hs_configured;
  190. info->hs_activated = adapter->hs_activated;
  191. info->is_cmd_timedout = adapter->is_cmd_timedout;
  192. info->num_cmd_host_to_card_failure
  193. = adapter->dbg.num_cmd_host_to_card_failure;
  194. info->num_cmd_sleep_cfm_host_to_card_failure
  195. = adapter->dbg.num_cmd_sleep_cfm_host_to_card_failure;
  196. info->num_tx_host_to_card_failure
  197. = adapter->dbg.num_tx_host_to_card_failure;
  198. info->num_event_deauth = adapter->dbg.num_event_deauth;
  199. info->num_event_disassoc = adapter->dbg.num_event_disassoc;
  200. info->num_event_link_lost = adapter->dbg.num_event_link_lost;
  201. info->num_cmd_deauth = adapter->dbg.num_cmd_deauth;
  202. info->num_cmd_assoc_success =
  203. adapter->dbg.num_cmd_assoc_success;
  204. info->num_cmd_assoc_failure =
  205. adapter->dbg.num_cmd_assoc_failure;
  206. info->num_tx_timeout = adapter->dbg.num_tx_timeout;
  207. info->timeout_cmd_id = adapter->dbg.timeout_cmd_id;
  208. info->timeout_cmd_act = adapter->dbg.timeout_cmd_act;
  209. memcpy(info->last_cmd_id, adapter->dbg.last_cmd_id,
  210. sizeof(adapter->dbg.last_cmd_id));
  211. memcpy(info->last_cmd_act, adapter->dbg.last_cmd_act,
  212. sizeof(adapter->dbg.last_cmd_act));
  213. info->last_cmd_index = adapter->dbg.last_cmd_index;
  214. memcpy(info->last_cmd_resp_id, adapter->dbg.last_cmd_resp_id,
  215. sizeof(adapter->dbg.last_cmd_resp_id));
  216. info->last_cmd_resp_index = adapter->dbg.last_cmd_resp_index;
  217. memcpy(info->last_event, adapter->dbg.last_event,
  218. sizeof(adapter->dbg.last_event));
  219. info->last_event_index = adapter->dbg.last_event_index;
  220. info->data_sent = adapter->data_sent;
  221. info->cmd_sent = adapter->cmd_sent;
  222. info->cmd_resp_received = adapter->cmd_resp_received;
  223. }
  224. return 0;
  225. }
  226. int mwifiex_debug_info_to_buffer(struct mwifiex_private *priv, char *buf,
  227. struct mwifiex_debug_info *info)
  228. {
  229. char *p = buf;
  230. struct mwifiex_debug_data *d = &items[0];
  231. size_t size, addr;
  232. long val;
  233. int i, j;
  234. if (!info)
  235. return 0;
  236. for (i = 0; i < num_of_items; i++) {
  237. p += sprintf(p, "%s=", d[i].name);
  238. size = d[i].size / d[i].num;
  239. if (i < (num_of_items - 3))
  240. addr = d[i].addr + (size_t)info;
  241. else /* The last 3 items are struct mwifiex_adapter variables */
  242. addr = d[i].addr + (size_t)priv->adapter;
  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 = *((u16 *)addr);
  250. break;
  251. case 4:
  252. val = *((u32 *)addr);
  253. break;
  254. case 8:
  255. val = *((long long *)addr);
  256. break;
  257. default:
  258. val = -1;
  259. break;
  260. }
  261. p += sprintf(p, "%#lx ", val);
  262. addr += size;
  263. }
  264. p += sprintf(p, "\n");
  265. }
  266. if (info->tx_tbl_num) {
  267. p += sprintf(p, "Tx BA stream table:\n");
  268. for (i = 0; i < info->tx_tbl_num; i++)
  269. p += sprintf(p, "tid = %d, ra = %pM\n",
  270. info->tx_tbl[i].tid, info->tx_tbl[i].ra);
  271. }
  272. if (info->rx_tbl_num) {
  273. p += sprintf(p, "Rx reorder table:\n");
  274. for (i = 0; i < info->rx_tbl_num; i++) {
  275. p += sprintf(p, "tid = %d, ta = %pM, ",
  276. info->rx_tbl[i].tid,
  277. info->rx_tbl[i].ta);
  278. p += sprintf(p, "start_win = %d, ",
  279. info->rx_tbl[i].start_win);
  280. p += sprintf(p, "win_size = %d, buffer: ",
  281. info->rx_tbl[i].win_size);
  282. for (j = 0; j < info->rx_tbl[i].win_size; j++)
  283. p += sprintf(p, "%c ",
  284. info->rx_tbl[i].buffer[j] ?
  285. '1' : '0');
  286. p += sprintf(p, "\n");
  287. }
  288. }
  289. if (info->tdls_peer_num) {
  290. p += sprintf(p, "TDLS peer table:\n");
  291. for (i = 0; i < info->tdls_peer_num; i++) {
  292. p += sprintf(p, "peer = %pM",
  293. info->tdls_list[i].peer_addr);
  294. p += sprintf(p, "\n");
  295. }
  296. }
  297. return p - buf;
  298. }
  299. static int
  300. mwifiex_parse_mgmt_packet(struct mwifiex_private *priv, u8 *payload, u16 len,
  301. struct rxpd *rx_pd)
  302. {
  303. u16 stype;
  304. u8 category, action_code, *addr2;
  305. struct ieee80211_hdr *ieee_hdr = (void *)payload;
  306. stype = (le16_to_cpu(ieee_hdr->frame_control) & IEEE80211_FCTL_STYPE);
  307. switch (stype) {
  308. case IEEE80211_STYPE_ACTION:
  309. category = *(payload + sizeof(struct ieee80211_hdr));
  310. switch (category) {
  311. case WLAN_CATEGORY_PUBLIC:
  312. action_code = *(payload + sizeof(struct ieee80211_hdr)
  313. + 1);
  314. if (action_code == WLAN_PUB_ACTION_TDLS_DISCOVER_RES) {
  315. addr2 = ieee_hdr->addr2;
  316. mwifiex_dbg(priv->adapter, INFO,
  317. "TDLS discovery response %pM nf=%d, snr=%d\n",
  318. addr2, rx_pd->nf, rx_pd->snr);
  319. mwifiex_auto_tdls_update_peer_signal(priv,
  320. addr2,
  321. rx_pd->snr,
  322. rx_pd->nf);
  323. }
  324. break;
  325. case WLAN_CATEGORY_BACK:
  326. /*we dont indicate BACK action frames to cfg80211*/
  327. mwifiex_dbg(priv->adapter, INFO,
  328. "drop BACK action frames");
  329. return -1;
  330. default:
  331. mwifiex_dbg(priv->adapter, INFO,
  332. "unknown public action frame category %d\n",
  333. category);
  334. }
  335. default:
  336. mwifiex_dbg(priv->adapter, INFO,
  337. "unknown mgmt frame subtype %#x\n", stype);
  338. return 0;
  339. }
  340. return 0;
  341. }
  342. /*
  343. * This function processes the received management packet and send it
  344. * to the kernel.
  345. */
  346. int
  347. mwifiex_process_mgmt_packet(struct mwifiex_private *priv,
  348. struct sk_buff *skb)
  349. {
  350. struct rxpd *rx_pd;
  351. u16 pkt_len;
  352. struct ieee80211_hdr *ieee_hdr;
  353. if (!skb)
  354. return -1;
  355. if (!priv->mgmt_frame_mask ||
  356. priv->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED) {
  357. mwifiex_dbg(priv->adapter, ERROR,
  358. "do not receive mgmt frames on uninitialized intf");
  359. return -1;
  360. }
  361. rx_pd = (struct rxpd *)skb->data;
  362. skb_pull(skb, le16_to_cpu(rx_pd->rx_pkt_offset));
  363. skb_pull(skb, sizeof(pkt_len));
  364. pkt_len = le16_to_cpu(rx_pd->rx_pkt_length);
  365. ieee_hdr = (void *)skb->data;
  366. if (ieee80211_is_mgmt(ieee_hdr->frame_control)) {
  367. if (mwifiex_parse_mgmt_packet(priv, (u8 *)ieee_hdr,
  368. pkt_len, rx_pd))
  369. return -1;
  370. }
  371. /* Remove address4 */
  372. memmove(skb->data + sizeof(struct ieee80211_hdr_3addr),
  373. skb->data + sizeof(struct ieee80211_hdr),
  374. pkt_len - sizeof(struct ieee80211_hdr));
  375. pkt_len -= ETH_ALEN + sizeof(pkt_len);
  376. rx_pd->rx_pkt_length = cpu_to_le16(pkt_len);
  377. cfg80211_rx_mgmt(&priv->wdev, priv->roc_cfg.chan.center_freq,
  378. CAL_RSSI(rx_pd->snr, rx_pd->nf), skb->data, pkt_len,
  379. 0);
  380. return 0;
  381. }
  382. /*
  383. * This function processes the received packet before sending it to the
  384. * kernel.
  385. *
  386. * It extracts the SKB from the received buffer and sends it to kernel.
  387. * In case the received buffer does not contain the data in SKB format,
  388. * the function creates a blank SKB, fills it with the data from the
  389. * received buffer and then sends this new SKB to the kernel.
  390. */
  391. int mwifiex_recv_packet(struct mwifiex_private *priv, struct sk_buff *skb)
  392. {
  393. struct mwifiex_sta_node *src_node;
  394. struct ethhdr *p_ethhdr;
  395. if (!skb)
  396. return -1;
  397. priv->stats.rx_bytes += skb->len;
  398. priv->stats.rx_packets++;
  399. if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP) {
  400. p_ethhdr = (void *)skb->data;
  401. src_node = mwifiex_get_sta_entry(priv, p_ethhdr->h_source);
  402. if (src_node) {
  403. src_node->stats.last_rx = jiffies;
  404. src_node->stats.rx_bytes += skb->len;
  405. src_node->stats.rx_packets++;
  406. }
  407. }
  408. skb->dev = priv->netdev;
  409. skb->protocol = eth_type_trans(skb, priv->netdev);
  410. skb->ip_summed = CHECKSUM_NONE;
  411. /* This is required only in case of 11n and USB/PCIE as we alloc
  412. * a buffer of 4K only if its 11N (to be able to receive 4K
  413. * AMSDU packets). In case of SD we allocate buffers based
  414. * on the size of packet and hence this is not needed.
  415. *
  416. * Modifying the truesize here as our allocation for each
  417. * skb is 4K but we only receive 2K packets and this cause
  418. * the kernel to start dropping packets in case where
  419. * application has allocated buffer based on 2K size i.e.
  420. * if there a 64K packet received (in IP fragments and
  421. * application allocates 64K to receive this packet but
  422. * this packet would almost double up because we allocate
  423. * each 1.5K fragment in 4K and pass it up. As soon as the
  424. * 64K limit hits kernel will start to drop rest of the
  425. * fragments. Currently we fail the Filesndl-ht.scr script
  426. * for UDP, hence this fix
  427. */
  428. if ((priv->adapter->iface_type == MWIFIEX_USB ||
  429. priv->adapter->iface_type == MWIFIEX_PCIE) &&
  430. (skb->truesize > MWIFIEX_RX_DATA_BUF_SIZE))
  431. skb->truesize += (skb->len - MWIFIEX_RX_DATA_BUF_SIZE);
  432. if (in_interrupt())
  433. netif_rx(skb);
  434. else
  435. netif_rx_ni(skb);
  436. return 0;
  437. }
  438. /*
  439. * IOCTL completion callback handler.
  440. *
  441. * This function is called when a pending IOCTL is completed.
  442. *
  443. * If work queue support is enabled, the function wakes up the
  444. * corresponding waiting function. Otherwise, it processes the
  445. * IOCTL response and frees the response buffer.
  446. */
  447. int mwifiex_complete_cmd(struct mwifiex_adapter *adapter,
  448. struct cmd_ctrl_node *cmd_node)
  449. {
  450. mwifiex_dbg(adapter, CMD,
  451. "cmd completed: status=%d\n",
  452. adapter->cmd_wait_q.status);
  453. *(cmd_node->condition) = true;
  454. if (adapter->cmd_wait_q.status == -ETIMEDOUT)
  455. mwifiex_dbg(adapter, ERROR, "cmd timeout\n");
  456. else
  457. wake_up_interruptible(&adapter->cmd_wait_q.wait);
  458. return 0;
  459. }
  460. /* This function will return the pointer to station entry in station list
  461. * table which matches specified mac address.
  462. * This function should be called after acquiring RA list spinlock.
  463. * NULL is returned if station entry is not found in associated STA list.
  464. */
  465. struct mwifiex_sta_node *
  466. mwifiex_get_sta_entry(struct mwifiex_private *priv, const u8 *mac)
  467. {
  468. struct mwifiex_sta_node *node;
  469. if (!mac)
  470. return NULL;
  471. list_for_each_entry(node, &priv->sta_list, list) {
  472. if (!memcmp(node->mac_addr, mac, ETH_ALEN))
  473. return node;
  474. }
  475. return NULL;
  476. }
  477. /* This function will add a sta_node entry to associated station list
  478. * table with the given mac address.
  479. * If entry exist already, existing entry is returned.
  480. * If received mac address is NULL, NULL is returned.
  481. */
  482. struct mwifiex_sta_node *
  483. mwifiex_add_sta_entry(struct mwifiex_private *priv, const u8 *mac)
  484. {
  485. struct mwifiex_sta_node *node;
  486. unsigned long flags;
  487. if (!mac)
  488. return NULL;
  489. spin_lock_irqsave(&priv->sta_list_spinlock, flags);
  490. node = mwifiex_get_sta_entry(priv, mac);
  491. if (node)
  492. goto done;
  493. node = kzalloc(sizeof(*node), GFP_ATOMIC);
  494. if (!node)
  495. goto done;
  496. memcpy(node->mac_addr, mac, ETH_ALEN);
  497. list_add_tail(&node->list, &priv->sta_list);
  498. done:
  499. spin_unlock_irqrestore(&priv->sta_list_spinlock, flags);
  500. return node;
  501. }
  502. /* This function will search for HT IE in association request IEs
  503. * and set station HT parameters accordingly.
  504. */
  505. void
  506. mwifiex_set_sta_ht_cap(struct mwifiex_private *priv, const u8 *ies,
  507. int ies_len, struct mwifiex_sta_node *node)
  508. {
  509. struct ieee_types_header *ht_cap_ie;
  510. const struct ieee80211_ht_cap *ht_cap;
  511. if (!ies)
  512. return;
  513. ht_cap_ie = (void *)cfg80211_find_ie(WLAN_EID_HT_CAPABILITY, ies,
  514. ies_len);
  515. if (ht_cap_ie) {
  516. ht_cap = (void *)(ht_cap_ie + 1);
  517. node->is_11n_enabled = 1;
  518. node->max_amsdu = le16_to_cpu(ht_cap->cap_info) &
  519. IEEE80211_HT_CAP_MAX_AMSDU ?
  520. MWIFIEX_TX_DATA_BUF_SIZE_8K :
  521. MWIFIEX_TX_DATA_BUF_SIZE_4K;
  522. } else {
  523. node->is_11n_enabled = 0;
  524. }
  525. return;
  526. }
  527. /* This function will delete a station entry from station list */
  528. void mwifiex_del_sta_entry(struct mwifiex_private *priv, const u8 *mac)
  529. {
  530. struct mwifiex_sta_node *node;
  531. unsigned long flags;
  532. spin_lock_irqsave(&priv->sta_list_spinlock, flags);
  533. node = mwifiex_get_sta_entry(priv, mac);
  534. if (node) {
  535. list_del(&node->list);
  536. kfree(node);
  537. }
  538. spin_unlock_irqrestore(&priv->sta_list_spinlock, flags);
  539. return;
  540. }
  541. /* This function will delete all stations from associated station list. */
  542. void mwifiex_del_all_sta_list(struct mwifiex_private *priv)
  543. {
  544. struct mwifiex_sta_node *node, *tmp;
  545. unsigned long flags;
  546. spin_lock_irqsave(&priv->sta_list_spinlock, flags);
  547. list_for_each_entry_safe(node, tmp, &priv->sta_list, list) {
  548. list_del(&node->list);
  549. kfree(node);
  550. }
  551. INIT_LIST_HEAD(&priv->sta_list);
  552. spin_unlock_irqrestore(&priv->sta_list_spinlock, flags);
  553. return;
  554. }
  555. /* This function adds histogram data to histogram array*/
  556. void mwifiex_hist_data_add(struct mwifiex_private *priv,
  557. u8 rx_rate, s8 snr, s8 nflr)
  558. {
  559. struct mwifiex_histogram_data *phist_data = priv->hist_data;
  560. if (atomic_read(&phist_data->num_samples) > MWIFIEX_HIST_MAX_SAMPLES)
  561. mwifiex_hist_data_reset(priv);
  562. mwifiex_hist_data_set(priv, rx_rate, snr, nflr);
  563. }
  564. /* function to add histogram record */
  565. void mwifiex_hist_data_set(struct mwifiex_private *priv, u8 rx_rate, s8 snr,
  566. s8 nflr)
  567. {
  568. struct mwifiex_histogram_data *phist_data = priv->hist_data;
  569. atomic_inc(&phist_data->num_samples);
  570. atomic_inc(&phist_data->rx_rate[rx_rate]);
  571. atomic_inc(&phist_data->snr[snr]);
  572. atomic_inc(&phist_data->noise_flr[128 + nflr]);
  573. atomic_inc(&phist_data->sig_str[nflr - snr]);
  574. }
  575. /* function to reset histogram data during init/reset */
  576. void mwifiex_hist_data_reset(struct mwifiex_private *priv)
  577. {
  578. int ix;
  579. struct mwifiex_histogram_data *phist_data = priv->hist_data;
  580. atomic_set(&phist_data->num_samples, 0);
  581. for (ix = 0; ix < MWIFIEX_MAX_AC_RX_RATES; ix++)
  582. atomic_set(&phist_data->rx_rate[ix], 0);
  583. for (ix = 0; ix < MWIFIEX_MAX_SNR; ix++)
  584. atomic_set(&phist_data->snr[ix], 0);
  585. for (ix = 0; ix < MWIFIEX_MAX_NOISE_FLR; ix++)
  586. atomic_set(&phist_data->noise_flr[ix], 0);
  587. for (ix = 0; ix < MWIFIEX_MAX_SIG_STRENGTH; ix++)
  588. atomic_set(&phist_data->sig_str[ix], 0);
  589. }
  590. void *mwifiex_alloc_dma_align_buf(int rx_len, gfp_t flags)
  591. {
  592. struct sk_buff *skb;
  593. int buf_len, pad;
  594. buf_len = rx_len + MWIFIEX_RX_HEADROOM + MWIFIEX_DMA_ALIGN_SZ;
  595. skb = __dev_alloc_skb(buf_len, flags);
  596. if (!skb)
  597. return NULL;
  598. skb_reserve(skb, MWIFIEX_RX_HEADROOM);
  599. pad = MWIFIEX_ALIGN_ADDR(skb->data, MWIFIEX_DMA_ALIGN_SZ) -
  600. (long)skb->data;
  601. skb_reserve(skb, pad);
  602. return skb;
  603. }
  604. EXPORT_SYMBOL_GPL(mwifiex_alloc_dma_align_buf);