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