util.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. /*
  27. * Firmware initialization complete callback handler.
  28. *
  29. * This function wakes up the function waiting on the init
  30. * wait queue for the firmware initialization to complete.
  31. */
  32. int mwifiex_init_fw_complete(struct mwifiex_adapter *adapter)
  33. {
  34. adapter->init_wait_q_woken = true;
  35. wake_up_interruptible(&adapter->init_wait_q);
  36. return 0;
  37. }
  38. /*
  39. * Firmware shutdown complete callback handler.
  40. *
  41. * This function sets the hardware status to not ready and wakes up
  42. * the function waiting on the init wait queue for the firmware
  43. * shutdown to complete.
  44. */
  45. int mwifiex_shutdown_fw_complete(struct mwifiex_adapter *adapter)
  46. {
  47. adapter->hw_status = MWIFIEX_HW_STATUS_NOT_READY;
  48. adapter->init_wait_q_woken = true;
  49. wake_up_interruptible(&adapter->init_wait_q);
  50. return 0;
  51. }
  52. /*
  53. * This function sends init/shutdown command
  54. * to firmware.
  55. */
  56. int mwifiex_init_shutdown_fw(struct mwifiex_private *priv,
  57. u32 func_init_shutdown)
  58. {
  59. u16 cmd;
  60. if (func_init_shutdown == MWIFIEX_FUNC_INIT) {
  61. cmd = HostCmd_CMD_FUNC_INIT;
  62. } else if (func_init_shutdown == MWIFIEX_FUNC_SHUTDOWN) {
  63. cmd = HostCmd_CMD_FUNC_SHUTDOWN;
  64. } else {
  65. dev_err(priv->adapter->dev, "unsupported parameter\n");
  66. return -1;
  67. }
  68. return mwifiex_send_cmd(priv, cmd, HostCmd_ACT_GEN_SET, 0, NULL, true);
  69. }
  70. EXPORT_SYMBOL_GPL(mwifiex_init_shutdown_fw);
  71. /*
  72. * IOCTL request handler to set/get debug information.
  73. *
  74. * This function collates/sets the information from/to different driver
  75. * structures.
  76. */
  77. int mwifiex_get_debug_info(struct mwifiex_private *priv,
  78. struct mwifiex_debug_info *info)
  79. {
  80. struct mwifiex_adapter *adapter = priv->adapter;
  81. if (info) {
  82. memcpy(info->packets_out,
  83. priv->wmm.packets_out,
  84. sizeof(priv->wmm.packets_out));
  85. info->curr_tx_buf_size = (u32) adapter->curr_tx_buf_size;
  86. info->tx_buf_size = (u32) adapter->tx_buf_size;
  87. info->rx_tbl_num = mwifiex_get_rx_reorder_tbl(priv,
  88. info->rx_tbl);
  89. info->tx_tbl_num = mwifiex_get_tx_ba_stream_tbl(priv,
  90. info->tx_tbl);
  91. info->ps_mode = adapter->ps_mode;
  92. info->ps_state = adapter->ps_state;
  93. info->is_deep_sleep = adapter->is_deep_sleep;
  94. info->pm_wakeup_card_req = adapter->pm_wakeup_card_req;
  95. info->pm_wakeup_fw_try = adapter->pm_wakeup_fw_try;
  96. info->is_hs_configured = adapter->is_hs_configured;
  97. info->hs_activated = adapter->hs_activated;
  98. info->is_cmd_timedout = adapter->is_cmd_timedout;
  99. info->num_cmd_host_to_card_failure
  100. = adapter->dbg.num_cmd_host_to_card_failure;
  101. info->num_cmd_sleep_cfm_host_to_card_failure
  102. = adapter->dbg.num_cmd_sleep_cfm_host_to_card_failure;
  103. info->num_tx_host_to_card_failure
  104. = adapter->dbg.num_tx_host_to_card_failure;
  105. info->num_event_deauth = adapter->dbg.num_event_deauth;
  106. info->num_event_disassoc = adapter->dbg.num_event_disassoc;
  107. info->num_event_link_lost = adapter->dbg.num_event_link_lost;
  108. info->num_cmd_deauth = adapter->dbg.num_cmd_deauth;
  109. info->num_cmd_assoc_success =
  110. adapter->dbg.num_cmd_assoc_success;
  111. info->num_cmd_assoc_failure =
  112. adapter->dbg.num_cmd_assoc_failure;
  113. info->num_tx_timeout = adapter->dbg.num_tx_timeout;
  114. info->timeout_cmd_id = adapter->dbg.timeout_cmd_id;
  115. info->timeout_cmd_act = adapter->dbg.timeout_cmd_act;
  116. memcpy(info->last_cmd_id, adapter->dbg.last_cmd_id,
  117. sizeof(adapter->dbg.last_cmd_id));
  118. memcpy(info->last_cmd_act, adapter->dbg.last_cmd_act,
  119. sizeof(adapter->dbg.last_cmd_act));
  120. info->last_cmd_index = adapter->dbg.last_cmd_index;
  121. memcpy(info->last_cmd_resp_id, adapter->dbg.last_cmd_resp_id,
  122. sizeof(adapter->dbg.last_cmd_resp_id));
  123. info->last_cmd_resp_index = adapter->dbg.last_cmd_resp_index;
  124. memcpy(info->last_event, adapter->dbg.last_event,
  125. sizeof(adapter->dbg.last_event));
  126. info->last_event_index = adapter->dbg.last_event_index;
  127. info->data_sent = adapter->data_sent;
  128. info->cmd_sent = adapter->cmd_sent;
  129. info->cmd_resp_received = adapter->cmd_resp_received;
  130. }
  131. return 0;
  132. }
  133. static int
  134. mwifiex_parse_mgmt_packet(struct mwifiex_private *priv, u8 *payload, u16 len,
  135. struct rxpd *rx_pd)
  136. {
  137. u16 stype;
  138. u8 category, action_code;
  139. struct ieee80211_hdr *ieee_hdr = (void *)payload;
  140. stype = (le16_to_cpu(ieee_hdr->frame_control) & IEEE80211_FCTL_STYPE);
  141. switch (stype) {
  142. case IEEE80211_STYPE_ACTION:
  143. category = *(payload + sizeof(struct ieee80211_hdr));
  144. action_code = *(payload + sizeof(struct ieee80211_hdr) + 1);
  145. if (category == WLAN_CATEGORY_PUBLIC &&
  146. action_code == WLAN_PUB_ACTION_TDLS_DISCOVER_RES) {
  147. dev_dbg(priv->adapter->dev,
  148. "TDLS discovery response %pM nf=%d, snr=%d\n",
  149. ieee_hdr->addr2, rx_pd->nf, rx_pd->snr);
  150. mwifiex_auto_tdls_update_peer_signal(priv,
  151. ieee_hdr->addr2,
  152. rx_pd->snr,
  153. rx_pd->nf);
  154. }
  155. break;
  156. default:
  157. dev_dbg(priv->adapter->dev,
  158. "unknown mgmt frame subytpe %#x\n", stype);
  159. }
  160. return 0;
  161. }
  162. /*
  163. * This function processes the received management packet and send it
  164. * to the kernel.
  165. */
  166. int
  167. mwifiex_process_mgmt_packet(struct mwifiex_private *priv,
  168. struct sk_buff *skb)
  169. {
  170. struct rxpd *rx_pd;
  171. u16 pkt_len;
  172. struct ieee80211_hdr *ieee_hdr;
  173. if (!skb)
  174. return -1;
  175. rx_pd = (struct rxpd *)skb->data;
  176. skb_pull(skb, le16_to_cpu(rx_pd->rx_pkt_offset));
  177. skb_pull(skb, sizeof(pkt_len));
  178. pkt_len = le16_to_cpu(rx_pd->rx_pkt_length);
  179. ieee_hdr = (void *)skb->data;
  180. if (ieee80211_is_mgmt(ieee_hdr->frame_control)) {
  181. mwifiex_parse_mgmt_packet(priv, (u8 *)ieee_hdr,
  182. pkt_len, rx_pd);
  183. }
  184. /* Remove address4 */
  185. memmove(skb->data + sizeof(struct ieee80211_hdr_3addr),
  186. skb->data + sizeof(struct ieee80211_hdr),
  187. pkt_len - sizeof(struct ieee80211_hdr));
  188. pkt_len -= ETH_ALEN + sizeof(pkt_len);
  189. rx_pd->rx_pkt_length = cpu_to_le16(pkt_len);
  190. cfg80211_rx_mgmt(priv->wdev, priv->roc_cfg.chan.center_freq,
  191. CAL_RSSI(rx_pd->snr, rx_pd->nf), skb->data, pkt_len,
  192. 0);
  193. return 0;
  194. }
  195. /*
  196. * This function processes the received packet before sending it to the
  197. * kernel.
  198. *
  199. * It extracts the SKB from the received buffer and sends it to kernel.
  200. * In case the received buffer does not contain the data in SKB format,
  201. * the function creates a blank SKB, fills it with the data from the
  202. * received buffer and then sends this new SKB to the kernel.
  203. */
  204. int mwifiex_recv_packet(struct mwifiex_private *priv, struct sk_buff *skb)
  205. {
  206. if (!skb)
  207. return -1;
  208. priv->stats.rx_bytes += skb->len;
  209. priv->stats.rx_packets++;
  210. skb->dev = priv->netdev;
  211. skb->protocol = eth_type_trans(skb, priv->netdev);
  212. skb->ip_summed = CHECKSUM_NONE;
  213. /* This is required only in case of 11n and USB/PCIE as we alloc
  214. * a buffer of 4K only if its 11N (to be able to receive 4K
  215. * AMSDU packets). In case of SD we allocate buffers based
  216. * on the size of packet and hence this is not needed.
  217. *
  218. * Modifying the truesize here as our allocation for each
  219. * skb is 4K but we only receive 2K packets and this cause
  220. * the kernel to start dropping packets in case where
  221. * application has allocated buffer based on 2K size i.e.
  222. * if there a 64K packet received (in IP fragments and
  223. * application allocates 64K to receive this packet but
  224. * this packet would almost double up because we allocate
  225. * each 1.5K fragment in 4K and pass it up. As soon as the
  226. * 64K limit hits kernel will start to drop rest of the
  227. * fragments. Currently we fail the Filesndl-ht.scr script
  228. * for UDP, hence this fix
  229. */
  230. if ((priv->adapter->iface_type == MWIFIEX_USB ||
  231. priv->adapter->iface_type == MWIFIEX_PCIE) &&
  232. (skb->truesize > MWIFIEX_RX_DATA_BUF_SIZE))
  233. skb->truesize += (skb->len - MWIFIEX_RX_DATA_BUF_SIZE);
  234. if (in_interrupt())
  235. netif_rx(skb);
  236. else
  237. netif_rx_ni(skb);
  238. return 0;
  239. }
  240. /*
  241. * IOCTL completion callback handler.
  242. *
  243. * This function is called when a pending IOCTL is completed.
  244. *
  245. * If work queue support is enabled, the function wakes up the
  246. * corresponding waiting function. Otherwise, it processes the
  247. * IOCTL response and frees the response buffer.
  248. */
  249. int mwifiex_complete_cmd(struct mwifiex_adapter *adapter,
  250. struct cmd_ctrl_node *cmd_node)
  251. {
  252. dev_dbg(adapter->dev, "cmd completed: status=%d\n",
  253. adapter->cmd_wait_q.status);
  254. *(cmd_node->condition) = true;
  255. if (adapter->cmd_wait_q.status == -ETIMEDOUT)
  256. dev_err(adapter->dev, "cmd timeout\n");
  257. else
  258. wake_up_interruptible(&adapter->cmd_wait_q.wait);
  259. return 0;
  260. }
  261. /* This function will return the pointer to station entry in station list
  262. * table which matches specified mac address.
  263. * This function should be called after acquiring RA list spinlock.
  264. * NULL is returned if station entry is not found in associated STA list.
  265. */
  266. struct mwifiex_sta_node *
  267. mwifiex_get_sta_entry(struct mwifiex_private *priv, const u8 *mac)
  268. {
  269. struct mwifiex_sta_node *node;
  270. if (!mac)
  271. return NULL;
  272. list_for_each_entry(node, &priv->sta_list, list) {
  273. if (!memcmp(node->mac_addr, mac, ETH_ALEN))
  274. return node;
  275. }
  276. return NULL;
  277. }
  278. /* This function will add a sta_node entry to associated station list
  279. * table with the given mac address.
  280. * If entry exist already, existing entry is returned.
  281. * If received mac address is NULL, NULL is returned.
  282. */
  283. struct mwifiex_sta_node *
  284. mwifiex_add_sta_entry(struct mwifiex_private *priv, const u8 *mac)
  285. {
  286. struct mwifiex_sta_node *node;
  287. unsigned long flags;
  288. if (!mac)
  289. return NULL;
  290. spin_lock_irqsave(&priv->sta_list_spinlock, flags);
  291. node = mwifiex_get_sta_entry(priv, mac);
  292. if (node)
  293. goto done;
  294. node = kzalloc(sizeof(*node), GFP_ATOMIC);
  295. if (!node)
  296. goto done;
  297. memcpy(node->mac_addr, mac, ETH_ALEN);
  298. list_add_tail(&node->list, &priv->sta_list);
  299. done:
  300. spin_unlock_irqrestore(&priv->sta_list_spinlock, flags);
  301. return node;
  302. }
  303. /* This function will search for HT IE in association request IEs
  304. * and set station HT parameters accordingly.
  305. */
  306. void
  307. mwifiex_set_sta_ht_cap(struct mwifiex_private *priv, const u8 *ies,
  308. int ies_len, struct mwifiex_sta_node *node)
  309. {
  310. const struct ieee80211_ht_cap *ht_cap;
  311. if (!ies)
  312. return;
  313. ht_cap = (void *)cfg80211_find_ie(WLAN_EID_HT_CAPABILITY, ies, ies_len);
  314. if (ht_cap) {
  315. node->is_11n_enabled = 1;
  316. node->max_amsdu = le16_to_cpu(ht_cap->cap_info) &
  317. IEEE80211_HT_CAP_MAX_AMSDU ?
  318. MWIFIEX_TX_DATA_BUF_SIZE_8K :
  319. MWIFIEX_TX_DATA_BUF_SIZE_4K;
  320. } else {
  321. node->is_11n_enabled = 0;
  322. }
  323. return;
  324. }
  325. /* This function will delete a station entry from station list */
  326. void mwifiex_del_sta_entry(struct mwifiex_private *priv, const u8 *mac)
  327. {
  328. struct mwifiex_sta_node *node;
  329. unsigned long flags;
  330. spin_lock_irqsave(&priv->sta_list_spinlock, flags);
  331. node = mwifiex_get_sta_entry(priv, mac);
  332. if (node) {
  333. list_del(&node->list);
  334. kfree(node);
  335. }
  336. spin_unlock_irqrestore(&priv->sta_list_spinlock, flags);
  337. return;
  338. }
  339. /* This function will delete all stations from associated station list. */
  340. void mwifiex_del_all_sta_list(struct mwifiex_private *priv)
  341. {
  342. struct mwifiex_sta_node *node, *tmp;
  343. unsigned long flags;
  344. spin_lock_irqsave(&priv->sta_list_spinlock, flags);
  345. list_for_each_entry_safe(node, tmp, &priv->sta_list, list) {
  346. list_del(&node->list);
  347. kfree(node);
  348. }
  349. INIT_LIST_HEAD(&priv->sta_list);
  350. spin_unlock_irqrestore(&priv->sta_list_spinlock, flags);
  351. return;
  352. }