Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next
[linux-2.6-microblaze.git] / drivers / net / wireless / mwifiex / tdls.c
1 /* Marvell Wireless LAN device driver: TDLS handling
2  *
3  * Copyright (C) 2014, Marvell International Ltd.
4  *
5  * This software file (the "File") is distributed by Marvell International
6  * Ltd. under the terms of the GNU General Public License Version 2, June 1991
7  * (the "License").  You may use, redistribute and/or modify this File in
8  * accordance with the terms and conditions of the License, a copy of which
9  * is available on the worldwide web at
10  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
11  *
12  * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
13  * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
14  * ARE EXPRESSLY DISCLAIMED.  The License provides additional details about
15  * this warranty disclaimer.
16  */
17
18 #include "main.h"
19 #include "wmm.h"
20 #include "11n.h"
21 #include "11n_rxreorder.h"
22 #include "11ac.h"
23
24 #define TDLS_REQ_FIX_LEN      6
25 #define TDLS_RESP_FIX_LEN     8
26 #define TDLS_CONFIRM_FIX_LEN  6
27 #define MWIFIEX_TDLS_WMM_INFO_SIZE 7
28
29 static void mwifiex_restore_tdls_packets(struct mwifiex_private *priv,
30                                          const u8 *mac, u8 status)
31 {
32         struct mwifiex_ra_list_tbl *ra_list;
33         struct list_head *tid_list;
34         struct sk_buff *skb, *tmp;
35         struct mwifiex_txinfo *tx_info;
36         unsigned long flags;
37         u32 tid;
38         u8 tid_down;
39
40         mwifiex_dbg(priv->adapter, DATA, "%s: %pM\n", __func__, mac);
41         spin_lock_irqsave(&priv->wmm.ra_list_spinlock, flags);
42
43         skb_queue_walk_safe(&priv->tdls_txq, skb, tmp) {
44                 if (!ether_addr_equal(mac, skb->data))
45                         continue;
46
47                 __skb_unlink(skb, &priv->tdls_txq);
48                 tx_info = MWIFIEX_SKB_TXCB(skb);
49                 tid = skb->priority;
50                 tid_down = mwifiex_wmm_downgrade_tid(priv, tid);
51
52                 if (mwifiex_is_tdls_link_setup(status)) {
53                         ra_list = mwifiex_wmm_get_queue_raptr(priv, tid, mac);
54                         ra_list->tdls_link = true;
55                         tx_info->flags |= MWIFIEX_BUF_FLAG_TDLS_PKT;
56                 } else {
57                         tid_list = &priv->wmm.tid_tbl_ptr[tid_down].ra_list;
58                         if (!list_empty(tid_list))
59                                 ra_list = list_first_entry(tid_list,
60                                               struct mwifiex_ra_list_tbl, list);
61                         else
62                                 ra_list = NULL;
63                         tx_info->flags &= ~MWIFIEX_BUF_FLAG_TDLS_PKT;
64                 }
65
66                 if (!ra_list) {
67                         mwifiex_write_data_complete(priv->adapter, skb, 0, -1);
68                         continue;
69                 }
70
71                 skb_queue_tail(&ra_list->skb_head, skb);
72
73                 ra_list->ba_pkt_count++;
74                 ra_list->total_pkt_count++;
75
76                 if (atomic_read(&priv->wmm.highest_queued_prio) <
77                                                        tos_to_tid_inv[tid_down])
78                         atomic_set(&priv->wmm.highest_queued_prio,
79                                    tos_to_tid_inv[tid_down]);
80
81                 atomic_inc(&priv->wmm.tx_pkts_queued);
82         }
83
84         spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock, flags);
85         return;
86 }
87
88 static void mwifiex_hold_tdls_packets(struct mwifiex_private *priv,
89                                       const u8 *mac)
90 {
91         struct mwifiex_ra_list_tbl *ra_list;
92         struct list_head *ra_list_head;
93         struct sk_buff *skb, *tmp;
94         unsigned long flags;
95         int i;
96
97         mwifiex_dbg(priv->adapter, DATA, "%s: %pM\n", __func__, mac);
98         spin_lock_irqsave(&priv->wmm.ra_list_spinlock, flags);
99
100         for (i = 0; i < MAX_NUM_TID; i++) {
101                 if (!list_empty(&priv->wmm.tid_tbl_ptr[i].ra_list)) {
102                         ra_list_head = &priv->wmm.tid_tbl_ptr[i].ra_list;
103                         list_for_each_entry(ra_list, ra_list_head, list) {
104                                 skb_queue_walk_safe(&ra_list->skb_head, skb,
105                                                     tmp) {
106                                         if (!ether_addr_equal(mac, skb->data))
107                                                 continue;
108                                         __skb_unlink(skb, &ra_list->skb_head);
109                                         atomic_dec(&priv->wmm.tx_pkts_queued);
110                                         ra_list->total_pkt_count--;
111                                         skb_queue_tail(&priv->tdls_txq, skb);
112                                 }
113                         }
114                 }
115         }
116
117         spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock, flags);
118         return;
119 }
120
121 /* This function appends rate TLV to scan config command. */
122 static int
123 mwifiex_tdls_append_rates_ie(struct mwifiex_private *priv,
124                              struct sk_buff *skb)
125 {
126         u8 rates[MWIFIEX_SUPPORTED_RATES], *pos;
127         u16 rates_size, supp_rates_size, ext_rates_size;
128
129         memset(rates, 0, sizeof(rates));
130         rates_size = mwifiex_get_supported_rates(priv, rates);
131
132         supp_rates_size = min_t(u16, rates_size, MWIFIEX_TDLS_SUPPORTED_RATES);
133
134         if (skb_tailroom(skb) < rates_size + 4) {
135                 mwifiex_dbg(priv->adapter, ERROR,
136                             "Insuffient space while adding rates\n");
137                 return -ENOMEM;
138         }
139
140         pos = skb_put(skb, supp_rates_size + 2);
141         *pos++ = WLAN_EID_SUPP_RATES;
142         *pos++ = supp_rates_size;
143         memcpy(pos, rates, supp_rates_size);
144
145         if (rates_size > MWIFIEX_TDLS_SUPPORTED_RATES) {
146                 ext_rates_size = rates_size - MWIFIEX_TDLS_SUPPORTED_RATES;
147                 pos = skb_put(skb, ext_rates_size + 2);
148                 *pos++ = WLAN_EID_EXT_SUPP_RATES;
149                 *pos++ = ext_rates_size;
150                 memcpy(pos, rates + MWIFIEX_TDLS_SUPPORTED_RATES,
151                        ext_rates_size);
152         }
153
154         return 0;
155 }
156
157 static void mwifiex_tdls_add_aid(struct mwifiex_private *priv,
158                                 struct sk_buff *skb)
159 {
160         struct ieee_types_assoc_rsp *assoc_rsp;
161         u8 *pos;
162
163         assoc_rsp = (struct ieee_types_assoc_rsp *)&priv->assoc_rsp_buf;
164         pos = (void *)skb_put(skb, 4);
165         *pos++ = WLAN_EID_AID;
166         *pos++ = 2;
167         memcpy(pos, &assoc_rsp->a_id, sizeof(assoc_rsp->a_id));
168
169         return;
170 }
171
172 static int mwifiex_tdls_add_vht_capab(struct mwifiex_private *priv,
173                                       struct sk_buff *skb)
174 {
175         struct ieee80211_vht_cap vht_cap;
176         u8 *pos;
177
178         pos = (void *)skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
179         *pos++ = WLAN_EID_VHT_CAPABILITY;
180         *pos++ = sizeof(struct ieee80211_vht_cap);
181
182         memset(&vht_cap, 0, sizeof(struct ieee80211_vht_cap));
183
184         mwifiex_fill_vht_cap_tlv(priv, &vht_cap, priv->curr_bss_params.band);
185         memcpy(pos, &vht_cap, sizeof(vht_cap));
186
187         return 0;
188 }
189
190 static int
191 mwifiex_tdls_add_ht_oper(struct mwifiex_private *priv, const u8 *mac,
192                          u8 vht_enabled, struct sk_buff *skb)
193 {
194         struct ieee80211_ht_operation *ht_oper;
195         struct mwifiex_sta_node *sta_ptr;
196         struct mwifiex_bssdescriptor *bss_desc =
197                                         &priv->curr_bss_params.bss_descriptor;
198         u8 *pos;
199
200         sta_ptr = mwifiex_get_sta_entry(priv, mac);
201         if (unlikely(!sta_ptr)) {
202                 mwifiex_dbg(priv->adapter, ERROR,
203                             "TDLS peer station not found in list\n");
204                 return -1;
205         }
206
207         pos = (void *)skb_put(skb, sizeof(struct ieee80211_ht_operation) + 2);
208         *pos++ = WLAN_EID_HT_OPERATION;
209         *pos++ = sizeof(struct ieee80211_ht_operation);
210         ht_oper = (void *)pos;
211
212         ht_oper->primary_chan = bss_desc->channel;
213
214         /* follow AP's channel bandwidth */
215         if (ISSUPP_CHANWIDTH40(priv->adapter->hw_dot_11n_dev_cap) &&
216             bss_desc->bcn_ht_cap &&
217             ISALLOWED_CHANWIDTH40(bss_desc->bcn_ht_oper->ht_param))
218                 ht_oper->ht_param = bss_desc->bcn_ht_oper->ht_param;
219
220         if (vht_enabled) {
221                 ht_oper->ht_param =
222                           mwifiex_get_sec_chan_offset(bss_desc->channel);
223                 ht_oper->ht_param |= BIT(2);
224         }
225
226         memcpy(&sta_ptr->tdls_cap.ht_oper, ht_oper,
227                sizeof(struct ieee80211_ht_operation));
228
229         return 0;
230 }
231
232 static int mwifiex_tdls_add_vht_oper(struct mwifiex_private *priv,
233                                      const u8 *mac, struct sk_buff *skb)
234 {
235         struct mwifiex_bssdescriptor *bss_desc;
236         struct ieee80211_vht_operation *vht_oper;
237         struct ieee80211_vht_cap *vht_cap, *ap_vht_cap = NULL;
238         struct mwifiex_sta_node *sta_ptr;
239         struct mwifiex_adapter *adapter = priv->adapter;
240         u8 supp_chwd_set, peer_supp_chwd_set;
241         u8 *pos, ap_supp_chwd_set, chan_bw;
242         u16 mcs_map_user, mcs_map_resp, mcs_map_result;
243         u16 mcs_user, mcs_resp, nss;
244         u32 usr_vht_cap_info;
245
246         bss_desc = &priv->curr_bss_params.bss_descriptor;
247
248         sta_ptr = mwifiex_get_sta_entry(priv, mac);
249         if (unlikely(!sta_ptr)) {
250                 mwifiex_dbg(adapter, ERROR,
251                             "TDLS peer station not found in list\n");
252                 return -1;
253         }
254
255         if (!mwifiex_is_bss_in_11ac_mode(priv)) {
256                 if (sta_ptr->tdls_cap.extcap.ext_capab[7] &
257                    WLAN_EXT_CAPA8_TDLS_WIDE_BW_ENABLED) {
258                         mwifiex_dbg(adapter, WARN,
259                                     "TDLS peer doesn't support wider bandwidth\n");
260                         return 0;
261                 }
262         } else {
263                 ap_vht_cap = bss_desc->bcn_vht_cap;
264         }
265
266         pos = (void *)skb_put(skb, sizeof(struct ieee80211_vht_operation) + 2);
267         *pos++ = WLAN_EID_VHT_OPERATION;
268         *pos++ = sizeof(struct ieee80211_vht_operation);
269         vht_oper = (struct ieee80211_vht_operation *)pos;
270
271         if (bss_desc->bss_band & BAND_A)
272                 usr_vht_cap_info = adapter->usr_dot_11ac_dev_cap_a;
273         else
274                 usr_vht_cap_info = adapter->usr_dot_11ac_dev_cap_bg;
275
276         /* find the minmum bandwith between AP/TDLS peers */
277         vht_cap = &sta_ptr->tdls_cap.vhtcap;
278         supp_chwd_set = GET_VHTCAP_CHWDSET(usr_vht_cap_info);
279         peer_supp_chwd_set =
280                          GET_VHTCAP_CHWDSET(le32_to_cpu(vht_cap->vht_cap_info));
281         supp_chwd_set = min_t(u8, supp_chwd_set, peer_supp_chwd_set);
282
283         /* We need check AP's bandwidth when TDLS_WIDER_BANDWIDTH is off */
284
285         if (ap_vht_cap && sta_ptr->tdls_cap.extcap.ext_capab[7] &
286             WLAN_EXT_CAPA8_TDLS_WIDE_BW_ENABLED) {
287                 ap_supp_chwd_set =
288                       GET_VHTCAP_CHWDSET(le32_to_cpu(ap_vht_cap->vht_cap_info));
289                 supp_chwd_set = min_t(u8, supp_chwd_set, ap_supp_chwd_set);
290         }
291
292         switch (supp_chwd_set) {
293         case IEEE80211_VHT_CHANWIDTH_80MHZ:
294                 vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
295                 break;
296         case IEEE80211_VHT_CHANWIDTH_160MHZ:
297                 vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_160MHZ;
298                 break;
299         case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
300                 vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80P80MHZ;
301                 break;
302         default:
303                 vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_USE_HT;
304                 break;
305         }
306
307         mcs_map_user = GET_DEVRXMCSMAP(adapter->usr_dot_11ac_mcs_support);
308         mcs_map_resp = le16_to_cpu(vht_cap->supp_mcs.rx_mcs_map);
309         mcs_map_result = 0;
310
311         for (nss = 1; nss <= 8; nss++) {
312                 mcs_user = GET_VHTNSSMCS(mcs_map_user, nss);
313                 mcs_resp = GET_VHTNSSMCS(mcs_map_resp, nss);
314
315                 if ((mcs_user == IEEE80211_VHT_MCS_NOT_SUPPORTED) ||
316                     (mcs_resp == IEEE80211_VHT_MCS_NOT_SUPPORTED))
317                         SET_VHTNSSMCS(mcs_map_result, nss,
318                                       IEEE80211_VHT_MCS_NOT_SUPPORTED);
319                 else
320                         SET_VHTNSSMCS(mcs_map_result, nss,
321                                       min_t(u16, mcs_user, mcs_resp));
322         }
323
324         vht_oper->basic_mcs_set = cpu_to_le16(mcs_map_result);
325
326         switch (vht_oper->chan_width) {
327         case IEEE80211_VHT_CHANWIDTH_80MHZ:
328                 chan_bw = IEEE80211_VHT_CHANWIDTH_80MHZ;
329                 break;
330         case IEEE80211_VHT_CHANWIDTH_160MHZ:
331                 chan_bw = IEEE80211_VHT_CHANWIDTH_160MHZ;
332                 break;
333         case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
334                 chan_bw = IEEE80211_VHT_CHANWIDTH_80MHZ;
335                 break;
336         default:
337                 chan_bw = IEEE80211_VHT_CHANWIDTH_USE_HT;
338                 break;
339         }
340         vht_oper->center_freq_seg1_idx =
341                         mwifiex_get_center_freq_index(priv, BAND_AAC,
342                                                       bss_desc->channel,
343                                                       chan_bw);
344
345         return 0;
346 }
347
348 static void mwifiex_tdls_add_ext_capab(struct mwifiex_private *priv,
349                                        struct sk_buff *skb)
350 {
351         struct ieee_types_extcap *extcap;
352
353         extcap = (void *)skb_put(skb, sizeof(struct ieee_types_extcap));
354         extcap->ieee_hdr.element_id = WLAN_EID_EXT_CAPABILITY;
355         extcap->ieee_hdr.len = 8;
356         memset(extcap->ext_capab, 0, 8);
357         extcap->ext_capab[4] |= WLAN_EXT_CAPA5_TDLS_ENABLED;
358         extcap->ext_capab[3] |= WLAN_EXT_CAPA4_TDLS_CHAN_SWITCH;
359
360         if (priv->adapter->is_hw_11ac_capable)
361                 extcap->ext_capab[7] |= WLAN_EXT_CAPA8_TDLS_WIDE_BW_ENABLED;
362 }
363
364 static void mwifiex_tdls_add_qos_capab(struct sk_buff *skb)
365 {
366         u8 *pos = (void *)skb_put(skb, 3);
367
368         *pos++ = WLAN_EID_QOS_CAPA;
369         *pos++ = 1;
370         *pos++ = MWIFIEX_TDLS_DEF_QOS_CAPAB;
371 }
372
373 static void
374 mwifiex_tdls_add_wmm_param_ie(struct mwifiex_private *priv, struct sk_buff *skb)
375 {
376         struct ieee80211_wmm_param_ie *wmm;
377         u8 ac_vi[] = {0x42, 0x43, 0x5e, 0x00};
378         u8 ac_vo[] = {0x62, 0x32, 0x2f, 0x00};
379         u8 ac_be[] = {0x03, 0xa4, 0x00, 0x00};
380         u8 ac_bk[] = {0x27, 0xa4, 0x00, 0x00};
381
382         wmm = (void *)skb_put(skb, sizeof(*wmm));
383         memset(wmm, 0, sizeof(*wmm));
384
385         wmm->element_id = WLAN_EID_VENDOR_SPECIFIC;
386         wmm->len = sizeof(*wmm) - 2;
387         wmm->oui[0] = 0x00; /* Microsoft OUI 00:50:F2 */
388         wmm->oui[1] = 0x50;
389         wmm->oui[2] = 0xf2;
390         wmm->oui_type = 2; /* WME */
391         wmm->oui_subtype = 1; /* WME param */
392         wmm->version = 1; /* WME ver */
393         wmm->qos_info = 0; /* U-APSD not in use */
394
395         /* use default WMM AC parameters for TDLS link*/
396         memcpy(&wmm->ac[0], ac_be, sizeof(ac_be));
397         memcpy(&wmm->ac[1], ac_bk, sizeof(ac_bk));
398         memcpy(&wmm->ac[2], ac_vi, sizeof(ac_vi));
399         memcpy(&wmm->ac[3], ac_vo, sizeof(ac_vo));
400 }
401
402 static void
403 mwifiex_add_wmm_info_ie(struct mwifiex_private *priv, struct sk_buff *skb,
404                         u8 qosinfo)
405 {
406         u8 *buf;
407
408         buf = (void *)skb_put(skb, MWIFIEX_TDLS_WMM_INFO_SIZE +
409                               sizeof(struct ieee_types_header));
410
411         *buf++ = WLAN_EID_VENDOR_SPECIFIC;
412         *buf++ = 7; /* len */
413         *buf++ = 0x00; /* Microsoft OUI 00:50:F2 */
414         *buf++ = 0x50;
415         *buf++ = 0xf2;
416         *buf++ = 2; /* WME */
417         *buf++ = 0; /* WME info */
418         *buf++ = 1; /* WME ver */
419         *buf++ = qosinfo; /* U-APSD no in use */
420 }
421
422 static int mwifiex_prep_tdls_encap_data(struct mwifiex_private *priv,
423                                         const u8 *peer, u8 action_code,
424                                         u8 dialog_token,
425                                         u16 status_code, struct sk_buff *skb)
426 {
427         struct ieee80211_tdls_data *tf;
428         int ret;
429         u16 capab;
430         struct ieee80211_ht_cap *ht_cap;
431         u8 radio, *pos;
432
433         capab = priv->curr_bss_params.bss_descriptor.cap_info_bitmap;
434
435         tf = (void *)skb_put(skb, offsetof(struct ieee80211_tdls_data, u));
436         memcpy(tf->da, peer, ETH_ALEN);
437         memcpy(tf->sa, priv->curr_addr, ETH_ALEN);
438         tf->ether_type = cpu_to_be16(ETH_P_TDLS);
439         tf->payload_type = WLAN_TDLS_SNAP_RFTYPE;
440
441         switch (action_code) {
442         case WLAN_TDLS_SETUP_REQUEST:
443                 tf->category = WLAN_CATEGORY_TDLS;
444                 tf->action_code = WLAN_TDLS_SETUP_REQUEST;
445                 skb_put(skb, sizeof(tf->u.setup_req));
446                 tf->u.setup_req.dialog_token = dialog_token;
447                 tf->u.setup_req.capability = cpu_to_le16(capab);
448                 ret = mwifiex_tdls_append_rates_ie(priv, skb);
449                 if (ret) {
450                         dev_kfree_skb_any(skb);
451                         return ret;
452                 }
453
454                 pos = (void *)skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
455                 *pos++ = WLAN_EID_HT_CAPABILITY;
456                 *pos++ = sizeof(struct ieee80211_ht_cap);
457                 ht_cap = (void *)pos;
458                 radio = mwifiex_band_to_radio_type(priv->curr_bss_params.band);
459                 ret = mwifiex_fill_cap_info(priv, radio, ht_cap);
460                 if (ret) {
461                         dev_kfree_skb_any(skb);
462                         return ret;
463                 }
464
465                 if (priv->adapter->is_hw_11ac_capable) {
466                         ret = mwifiex_tdls_add_vht_capab(priv, skb);
467                         if (ret) {
468                                 dev_kfree_skb_any(skb);
469                                 return ret;
470                         }
471                         mwifiex_tdls_add_aid(priv, skb);
472                 }
473
474                 mwifiex_tdls_add_ext_capab(priv, skb);
475                 mwifiex_tdls_add_qos_capab(skb);
476                 mwifiex_add_wmm_info_ie(priv, skb, 0);
477                 break;
478
479         case WLAN_TDLS_SETUP_RESPONSE:
480                 tf->category = WLAN_CATEGORY_TDLS;
481                 tf->action_code = WLAN_TDLS_SETUP_RESPONSE;
482                 skb_put(skb, sizeof(tf->u.setup_resp));
483                 tf->u.setup_resp.status_code = cpu_to_le16(status_code);
484                 tf->u.setup_resp.dialog_token = dialog_token;
485                 tf->u.setup_resp.capability = cpu_to_le16(capab);
486                 ret = mwifiex_tdls_append_rates_ie(priv, skb);
487                 if (ret) {
488                         dev_kfree_skb_any(skb);
489                         return ret;
490                 }
491
492                 pos = (void *)skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
493                 *pos++ = WLAN_EID_HT_CAPABILITY;
494                 *pos++ = sizeof(struct ieee80211_ht_cap);
495                 ht_cap = (void *)pos;
496                 radio = mwifiex_band_to_radio_type(priv->curr_bss_params.band);
497                 ret = mwifiex_fill_cap_info(priv, radio, ht_cap);
498                 if (ret) {
499                         dev_kfree_skb_any(skb);
500                         return ret;
501                 }
502
503                 if (priv->adapter->is_hw_11ac_capable) {
504                         ret = mwifiex_tdls_add_vht_capab(priv, skb);
505                         if (ret) {
506                                 dev_kfree_skb_any(skb);
507                                 return ret;
508                         }
509                         mwifiex_tdls_add_aid(priv, skb);
510                 }
511
512                 mwifiex_tdls_add_ext_capab(priv, skb);
513                 mwifiex_tdls_add_qos_capab(skb);
514                 mwifiex_add_wmm_info_ie(priv, skb, 0);
515                 break;
516
517         case WLAN_TDLS_SETUP_CONFIRM:
518                 tf->category = WLAN_CATEGORY_TDLS;
519                 tf->action_code = WLAN_TDLS_SETUP_CONFIRM;
520                 skb_put(skb, sizeof(tf->u.setup_cfm));
521                 tf->u.setup_cfm.status_code = cpu_to_le16(status_code);
522                 tf->u.setup_cfm.dialog_token = dialog_token;
523
524                 mwifiex_tdls_add_wmm_param_ie(priv, skb);
525                 if (priv->adapter->is_hw_11ac_capable) {
526                         ret = mwifiex_tdls_add_vht_oper(priv, peer, skb);
527                         if (ret) {
528                                 dev_kfree_skb_any(skb);
529                                 return ret;
530                         }
531                         ret = mwifiex_tdls_add_ht_oper(priv, peer, 1, skb);
532                         if (ret) {
533                                 dev_kfree_skb_any(skb);
534                                 return ret;
535                         }
536                 } else {
537                         ret = mwifiex_tdls_add_ht_oper(priv, peer, 0, skb);
538                         if (ret) {
539                                 dev_kfree_skb_any(skb);
540                                 return ret;
541                         }
542                 }
543                 break;
544
545         case WLAN_TDLS_TEARDOWN:
546                 tf->category = WLAN_CATEGORY_TDLS;
547                 tf->action_code = WLAN_TDLS_TEARDOWN;
548                 skb_put(skb, sizeof(tf->u.teardown));
549                 tf->u.teardown.reason_code = cpu_to_le16(status_code);
550                 break;
551
552         case WLAN_TDLS_DISCOVERY_REQUEST:
553                 tf->category = WLAN_CATEGORY_TDLS;
554                 tf->action_code = WLAN_TDLS_DISCOVERY_REQUEST;
555                 skb_put(skb, sizeof(tf->u.discover_req));
556                 tf->u.discover_req.dialog_token = dialog_token;
557                 break;
558         default:
559                 mwifiex_dbg(priv->adapter, ERROR, "Unknown TDLS frame type.\n");
560                 return -EINVAL;
561         }
562
563         return 0;
564 }
565
566 static void
567 mwifiex_tdls_add_link_ie(struct sk_buff *skb, const u8 *src_addr,
568                          const u8 *peer, const u8 *bssid)
569 {
570         struct ieee80211_tdls_lnkie *lnkid;
571
572         lnkid = (void *)skb_put(skb, sizeof(struct ieee80211_tdls_lnkie));
573         lnkid->ie_type = WLAN_EID_LINK_ID;
574         lnkid->ie_len = sizeof(struct ieee80211_tdls_lnkie) -
575                         sizeof(struct ieee_types_header);
576
577         memcpy(lnkid->bssid, bssid, ETH_ALEN);
578         memcpy(lnkid->init_sta, src_addr, ETH_ALEN);
579         memcpy(lnkid->resp_sta, peer, ETH_ALEN);
580 }
581
582 int mwifiex_send_tdls_data_frame(struct mwifiex_private *priv, const u8 *peer,
583                                  u8 action_code, u8 dialog_token,
584                                  u16 status_code, const u8 *extra_ies,
585                                  size_t extra_ies_len)
586 {
587         struct sk_buff *skb;
588         struct mwifiex_txinfo *tx_info;
589         int ret;
590         u16 skb_len;
591
592         skb_len = MWIFIEX_MIN_DATA_HEADER_LEN +
593                   max(sizeof(struct ieee80211_mgmt),
594                       sizeof(struct ieee80211_tdls_data)) +
595                   MWIFIEX_MGMT_FRAME_HEADER_SIZE +
596                   MWIFIEX_SUPPORTED_RATES +
597                   3 + /* Qos Info */
598                   sizeof(struct ieee_types_extcap) +
599                   sizeof(struct ieee80211_ht_cap) +
600                   sizeof(struct ieee_types_bss_co_2040) +
601                   sizeof(struct ieee80211_ht_operation) +
602                   sizeof(struct ieee80211_tdls_lnkie) +
603                   sizeof(struct ieee80211_wmm_param_ie) +
604                   extra_ies_len;
605
606         if (priv->adapter->is_hw_11ac_capable)
607                 skb_len += sizeof(struct ieee_types_vht_cap) +
608                            sizeof(struct ieee_types_vht_oper) +
609                            sizeof(struct ieee_types_aid);
610
611         skb = dev_alloc_skb(skb_len);
612         if (!skb) {
613                 mwifiex_dbg(priv->adapter, ERROR,
614                             "allocate skb failed for management frame\n");
615                 return -ENOMEM;
616         }
617         skb_reserve(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
618
619         switch (action_code) {
620         case WLAN_TDLS_SETUP_REQUEST:
621         case WLAN_TDLS_SETUP_CONFIRM:
622         case WLAN_TDLS_TEARDOWN:
623         case WLAN_TDLS_DISCOVERY_REQUEST:
624                 ret = mwifiex_prep_tdls_encap_data(priv, peer, action_code,
625                                                    dialog_token, status_code,
626                                                    skb);
627                 if (ret) {
628                         dev_kfree_skb_any(skb);
629                         return ret;
630                 }
631                 if (extra_ies_len)
632                         memcpy(skb_put(skb, extra_ies_len), extra_ies,
633                                extra_ies_len);
634                 mwifiex_tdls_add_link_ie(skb, priv->curr_addr, peer,
635                                          priv->cfg_bssid);
636                 break;
637         case WLAN_TDLS_SETUP_RESPONSE:
638                 ret = mwifiex_prep_tdls_encap_data(priv, peer, action_code,
639                                                    dialog_token, status_code,
640                                                    skb);
641                 if (ret) {
642                         dev_kfree_skb_any(skb);
643                         return ret;
644                 }
645                 if (extra_ies_len)
646                         memcpy(skb_put(skb, extra_ies_len), extra_ies,
647                                extra_ies_len);
648                 mwifiex_tdls_add_link_ie(skb, peer, priv->curr_addr,
649                                          priv->cfg_bssid);
650                 break;
651         }
652
653         switch (action_code) {
654         case WLAN_TDLS_SETUP_REQUEST:
655         case WLAN_TDLS_SETUP_RESPONSE:
656                 skb->priority = MWIFIEX_PRIO_BK;
657                 break;
658         default:
659                 skb->priority = MWIFIEX_PRIO_VI;
660                 break;
661         }
662
663         tx_info = MWIFIEX_SKB_TXCB(skb);
664         memset(tx_info, 0, sizeof(*tx_info));
665         tx_info->bss_num = priv->bss_num;
666         tx_info->bss_type = priv->bss_type;
667
668         __net_timestamp(skb);
669         mwifiex_queue_tx_pkt(priv, skb);
670
671         return 0;
672 }
673
674 static int
675 mwifiex_construct_tdls_action_frame(struct mwifiex_private *priv,
676                                     const u8 *peer,
677                                     u8 action_code, u8 dialog_token,
678                                     u16 status_code, struct sk_buff *skb)
679 {
680         struct ieee80211_mgmt *mgmt;
681         u8 bc_addr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
682         int ret;
683         u16 capab;
684         struct ieee80211_ht_cap *ht_cap;
685         u8 radio, *pos;
686
687         capab = priv->curr_bss_params.bss_descriptor.cap_info_bitmap;
688
689         mgmt = (void *)skb_put(skb, offsetof(struct ieee80211_mgmt, u));
690
691         memset(mgmt, 0, 24);
692         memcpy(mgmt->da, peer, ETH_ALEN);
693         memcpy(mgmt->sa, priv->curr_addr, ETH_ALEN);
694         memcpy(mgmt->bssid, priv->cfg_bssid, ETH_ALEN);
695         mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
696                                           IEEE80211_STYPE_ACTION);
697
698         /* add address 4 */
699         pos = skb_put(skb, ETH_ALEN);
700
701         switch (action_code) {
702         case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
703                 skb_put(skb, sizeof(mgmt->u.action.u.tdls_discover_resp) + 1);
704                 mgmt->u.action.category = WLAN_CATEGORY_PUBLIC;
705                 mgmt->u.action.u.tdls_discover_resp.action_code =
706                                               WLAN_PUB_ACTION_TDLS_DISCOVER_RES;
707                 mgmt->u.action.u.tdls_discover_resp.dialog_token =
708                                                                    dialog_token;
709                 mgmt->u.action.u.tdls_discover_resp.capability =
710                                                              cpu_to_le16(capab);
711                 /* move back for addr4 */
712                 memmove(pos + ETH_ALEN, &mgmt->u.action.category,
713                         sizeof(mgmt->u.action.u.tdls_discover_resp));
714                 /* init address 4 */
715                 memcpy(pos, bc_addr, ETH_ALEN);
716
717                 ret = mwifiex_tdls_append_rates_ie(priv, skb);
718                 if (ret) {
719                         dev_kfree_skb_any(skb);
720                         return ret;
721                 }
722
723                 pos = (void *)skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
724                 *pos++ = WLAN_EID_HT_CAPABILITY;
725                 *pos++ = sizeof(struct ieee80211_ht_cap);
726                 ht_cap = (void *)pos;
727                 radio = mwifiex_band_to_radio_type(priv->curr_bss_params.band);
728                 ret = mwifiex_fill_cap_info(priv, radio, ht_cap);
729                 if (ret) {
730                         dev_kfree_skb_any(skb);
731                         return ret;
732                 }
733
734                 if (priv->adapter->is_hw_11ac_capable) {
735                         ret = mwifiex_tdls_add_vht_capab(priv, skb);
736                         if (ret) {
737                                 dev_kfree_skb_any(skb);
738                                 return ret;
739                         }
740                         mwifiex_tdls_add_aid(priv, skb);
741                 }
742
743                 mwifiex_tdls_add_ext_capab(priv, skb);
744                 mwifiex_tdls_add_qos_capab(skb);
745                 break;
746         default:
747                 mwifiex_dbg(priv->adapter, ERROR, "Unknown TDLS action frame type\n");
748                 return -EINVAL;
749         }
750
751         return 0;
752 }
753
754 int mwifiex_send_tdls_action_frame(struct mwifiex_private *priv, const u8 *peer,
755                                    u8 action_code, u8 dialog_token,
756                                    u16 status_code, const u8 *extra_ies,
757                                    size_t extra_ies_len)
758 {
759         struct sk_buff *skb;
760         struct mwifiex_txinfo *tx_info;
761         u8 *pos;
762         u32 pkt_type, tx_control;
763         u16 pkt_len, skb_len;
764
765         skb_len = MWIFIEX_MIN_DATA_HEADER_LEN +
766                   max(sizeof(struct ieee80211_mgmt),
767                       sizeof(struct ieee80211_tdls_data)) +
768                   MWIFIEX_MGMT_FRAME_HEADER_SIZE +
769                   MWIFIEX_SUPPORTED_RATES +
770                   sizeof(struct ieee_types_extcap) +
771                   sizeof(struct ieee80211_ht_cap) +
772                   sizeof(struct ieee_types_bss_co_2040) +
773                   sizeof(struct ieee80211_ht_operation) +
774                   sizeof(struct ieee80211_tdls_lnkie) +
775                   extra_ies_len +
776                   3 + /* Qos Info */
777                   ETH_ALEN; /* Address4 */
778
779         if (priv->adapter->is_hw_11ac_capable)
780                 skb_len += sizeof(struct ieee_types_vht_cap) +
781                            sizeof(struct ieee_types_vht_oper) +
782                            sizeof(struct ieee_types_aid);
783
784         skb = dev_alloc_skb(skb_len);
785         if (!skb) {
786                 mwifiex_dbg(priv->adapter, ERROR,
787                             "allocate skb failed for management frame\n");
788                 return -ENOMEM;
789         }
790
791         skb_reserve(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
792
793         pkt_type = PKT_TYPE_MGMT;
794         tx_control = 0;
795         pos = skb_put(skb, MWIFIEX_MGMT_FRAME_HEADER_SIZE + sizeof(pkt_len));
796         memset(pos, 0, MWIFIEX_MGMT_FRAME_HEADER_SIZE + sizeof(pkt_len));
797         memcpy(pos, &pkt_type, sizeof(pkt_type));
798         memcpy(pos + sizeof(pkt_type), &tx_control, sizeof(tx_control));
799
800         if (mwifiex_construct_tdls_action_frame(priv, peer, action_code,
801                                                 dialog_token, status_code,
802                                                 skb)) {
803                 dev_kfree_skb_any(skb);
804                 return -EINVAL;
805         }
806
807         if (extra_ies_len)
808                 memcpy(skb_put(skb, extra_ies_len), extra_ies, extra_ies_len);
809
810         /* the TDLS link IE is always added last we are the responder */
811
812         mwifiex_tdls_add_link_ie(skb, peer, priv->curr_addr,
813                                  priv->cfg_bssid);
814
815         skb->priority = MWIFIEX_PRIO_VI;
816
817         tx_info = MWIFIEX_SKB_TXCB(skb);
818         memset(tx_info, 0, sizeof(*tx_info));
819         tx_info->bss_num = priv->bss_num;
820         tx_info->bss_type = priv->bss_type;
821         tx_info->flags |= MWIFIEX_BUF_FLAG_TDLS_PKT;
822
823         pkt_len = skb->len - MWIFIEX_MGMT_FRAME_HEADER_SIZE - sizeof(pkt_len);
824         memcpy(skb->data + MWIFIEX_MGMT_FRAME_HEADER_SIZE, &pkt_len,
825                sizeof(pkt_len));
826         __net_timestamp(skb);
827         mwifiex_queue_tx_pkt(priv, skb);
828
829         return 0;
830 }
831
832 /* This function process tdls action frame from peer.
833  * Peer capabilities are stored into station node structure.
834  */
835 void mwifiex_process_tdls_action_frame(struct mwifiex_private *priv,
836                                        u8 *buf, int len)
837 {
838         struct mwifiex_sta_node *sta_ptr;
839         u8 *peer, *pos, *end;
840         u8 i, action, basic;
841         __le16 cap = 0;
842         int ie_len = 0;
843
844         if (len < (sizeof(struct ethhdr) + 3))
845                 return;
846         if (*(buf + sizeof(struct ethhdr)) != WLAN_TDLS_SNAP_RFTYPE)
847                 return;
848         if (*(buf + sizeof(struct ethhdr) + 1) != WLAN_CATEGORY_TDLS)
849                 return;
850
851         peer = buf + ETH_ALEN;
852         action = *(buf + sizeof(struct ethhdr) + 2);
853         mwifiex_dbg(priv->adapter, DATA,
854                     "rx:tdls action: peer=%pM, action=%d\n", peer, action);
855
856         switch (action) {
857         case WLAN_TDLS_SETUP_REQUEST:
858                 if (len < (sizeof(struct ethhdr) + TDLS_REQ_FIX_LEN))
859                         return;
860
861                 pos = buf + sizeof(struct ethhdr) + 4;
862                 /* payload 1+ category 1 + action 1 + dialog 1 */
863                 cap = cpu_to_le16(*(u16 *)pos);
864                 ie_len = len - sizeof(struct ethhdr) - TDLS_REQ_FIX_LEN;
865                 pos += 2;
866                 break;
867
868         case WLAN_TDLS_SETUP_RESPONSE:
869                 if (len < (sizeof(struct ethhdr) + TDLS_RESP_FIX_LEN))
870                         return;
871                 /* payload 1+ category 1 + action 1 + dialog 1 + status code 2*/
872                 pos = buf + sizeof(struct ethhdr) + 6;
873                 cap = cpu_to_le16(*(u16 *)pos);
874                 ie_len = len - sizeof(struct ethhdr) - TDLS_RESP_FIX_LEN;
875                 pos += 2;
876                 break;
877
878         case WLAN_TDLS_SETUP_CONFIRM:
879                 if (len < (sizeof(struct ethhdr) + TDLS_CONFIRM_FIX_LEN))
880                         return;
881                 pos = buf + sizeof(struct ethhdr) + TDLS_CONFIRM_FIX_LEN;
882                 ie_len = len - sizeof(struct ethhdr) - TDLS_CONFIRM_FIX_LEN;
883                 break;
884         default:
885                 mwifiex_dbg(priv->adapter, ERROR, "Unknown TDLS frame type.\n");
886                 return;
887         }
888
889         sta_ptr = mwifiex_add_sta_entry(priv, peer);
890         if (!sta_ptr)
891                 return;
892
893         sta_ptr->tdls_cap.capab = cap;
894
895         for (end = pos + ie_len; pos + 1 < end; pos += 2 + pos[1]) {
896                 if (pos + 2 + pos[1] > end)
897                         break;
898
899                 switch (*pos) {
900                 case WLAN_EID_SUPP_RATES:
901                         sta_ptr->tdls_cap.rates_len = pos[1];
902                         for (i = 0; i < pos[1]; i++)
903                                 sta_ptr->tdls_cap.rates[i] = pos[i + 2];
904                         break;
905
906                 case WLAN_EID_EXT_SUPP_RATES:
907                         basic = sta_ptr->tdls_cap.rates_len;
908                         for (i = 0; i < pos[1]; i++)
909                                 sta_ptr->tdls_cap.rates[basic + i] = pos[i + 2];
910                         sta_ptr->tdls_cap.rates_len += pos[1];
911                         break;
912                 case WLAN_EID_HT_CAPABILITY:
913                         memcpy((u8 *)&sta_ptr->tdls_cap.ht_capb, pos,
914                                sizeof(struct ieee80211_ht_cap));
915                         sta_ptr->is_11n_enabled = 1;
916                         break;
917                 case WLAN_EID_HT_OPERATION:
918                         memcpy(&sta_ptr->tdls_cap.ht_oper, pos,
919                                sizeof(struct ieee80211_ht_operation));
920                         break;
921                 case WLAN_EID_BSS_COEX_2040:
922                         sta_ptr->tdls_cap.coex_2040 = pos[2];
923                         break;
924                 case WLAN_EID_EXT_CAPABILITY:
925                         memcpy((u8 *)&sta_ptr->tdls_cap.extcap, pos,
926                                sizeof(struct ieee_types_header) +
927                                min_t(u8, pos[1], 8));
928                         break;
929                 case WLAN_EID_RSN:
930                         memcpy((u8 *)&sta_ptr->tdls_cap.rsn_ie, pos,
931                                sizeof(struct ieee_types_header) +
932                                min_t(u8, pos[1], IEEE_MAX_IE_SIZE -
933                                      sizeof(struct ieee_types_header)));
934                         break;
935                 case WLAN_EID_QOS_CAPA:
936                         sta_ptr->tdls_cap.qos_info = pos[2];
937                         break;
938                 case WLAN_EID_VHT_OPERATION:
939                         if (priv->adapter->is_hw_11ac_capable)
940                                 memcpy(&sta_ptr->tdls_cap.vhtoper, pos,
941                                        sizeof(struct ieee80211_vht_operation));
942                         break;
943                 case WLAN_EID_VHT_CAPABILITY:
944                         if (priv->adapter->is_hw_11ac_capable) {
945                                 memcpy((u8 *)&sta_ptr->tdls_cap.vhtcap, pos,
946                                        sizeof(struct ieee80211_vht_cap));
947                                 sta_ptr->is_11ac_enabled = 1;
948                         }
949                         break;
950                 case WLAN_EID_AID:
951                         if (priv->adapter->is_hw_11ac_capable)
952                                 sta_ptr->tdls_cap.aid =
953                                               le16_to_cpu(*(__le16 *)(pos + 2));
954                 default:
955                         break;
956                 }
957         }
958
959         return;
960 }
961
962 static int
963 mwifiex_tdls_process_config_link(struct mwifiex_private *priv, const u8 *peer)
964 {
965         struct mwifiex_sta_node *sta_ptr;
966         struct mwifiex_ds_tdls_oper tdls_oper;
967
968         memset(&tdls_oper, 0, sizeof(struct mwifiex_ds_tdls_oper));
969         sta_ptr = mwifiex_get_sta_entry(priv, peer);
970
971         if (!sta_ptr || sta_ptr->tdls_status == TDLS_SETUP_FAILURE) {
972                 mwifiex_dbg(priv->adapter, ERROR,
973                             "link absent for peer %pM; cannot config\n", peer);
974                 return -EINVAL;
975         }
976
977         memcpy(&tdls_oper.peer_mac, peer, ETH_ALEN);
978         tdls_oper.tdls_action = MWIFIEX_TDLS_CONFIG_LINK;
979         return mwifiex_send_cmd(priv, HostCmd_CMD_TDLS_OPER,
980                                 HostCmd_ACT_GEN_SET, 0, &tdls_oper, true);
981 }
982
983 static int
984 mwifiex_tdls_process_create_link(struct mwifiex_private *priv, const u8 *peer)
985 {
986         struct mwifiex_sta_node *sta_ptr;
987         struct mwifiex_ds_tdls_oper tdls_oper;
988
989         memset(&tdls_oper, 0, sizeof(struct mwifiex_ds_tdls_oper));
990         sta_ptr = mwifiex_get_sta_entry(priv, peer);
991
992         if (sta_ptr && sta_ptr->tdls_status == TDLS_SETUP_INPROGRESS) {
993                 mwifiex_dbg(priv->adapter, WARN,
994                             "Setup already in progress for peer %pM\n", peer);
995                 return 0;
996         }
997
998         sta_ptr = mwifiex_add_sta_entry(priv, peer);
999         if (!sta_ptr)
1000                 return -ENOMEM;
1001
1002         sta_ptr->tdls_status = TDLS_SETUP_INPROGRESS;
1003         mwifiex_hold_tdls_packets(priv, peer);
1004         memcpy(&tdls_oper.peer_mac, peer, ETH_ALEN);
1005         tdls_oper.tdls_action = MWIFIEX_TDLS_CREATE_LINK;
1006         return mwifiex_send_cmd(priv, HostCmd_CMD_TDLS_OPER,
1007                                 HostCmd_ACT_GEN_SET, 0, &tdls_oper, true);
1008 }
1009
1010 static int
1011 mwifiex_tdls_process_disable_link(struct mwifiex_private *priv, const u8 *peer)
1012 {
1013         struct mwifiex_sta_node *sta_ptr;
1014         struct mwifiex_ds_tdls_oper tdls_oper;
1015         unsigned long flags;
1016
1017         memset(&tdls_oper, 0, sizeof(struct mwifiex_ds_tdls_oper));
1018         sta_ptr = mwifiex_get_sta_entry(priv, peer);
1019
1020         if (sta_ptr) {
1021                 if (sta_ptr->is_11n_enabled) {
1022                         mwifiex_11n_cleanup_reorder_tbl(priv);
1023                         spin_lock_irqsave(&priv->wmm.ra_list_spinlock,
1024                                           flags);
1025                         mwifiex_11n_delete_all_tx_ba_stream_tbl(priv);
1026                         spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock,
1027                                                flags);
1028                 }
1029                 mwifiex_del_sta_entry(priv, peer);
1030         }
1031
1032         mwifiex_restore_tdls_packets(priv, peer, TDLS_LINK_TEARDOWN);
1033         mwifiex_auto_tdls_update_peer_status(priv, peer, TDLS_NOT_SETUP);
1034         memcpy(&tdls_oper.peer_mac, peer, ETH_ALEN);
1035         tdls_oper.tdls_action = MWIFIEX_TDLS_DISABLE_LINK;
1036         return mwifiex_send_cmd(priv, HostCmd_CMD_TDLS_OPER,
1037                                 HostCmd_ACT_GEN_SET, 0, &tdls_oper, true);
1038 }
1039
1040 static int
1041 mwifiex_tdls_process_enable_link(struct mwifiex_private *priv, const u8 *peer)
1042 {
1043         struct mwifiex_sta_node *sta_ptr;
1044         struct ieee80211_mcs_info mcs;
1045         unsigned long flags;
1046         int i;
1047
1048         sta_ptr = mwifiex_get_sta_entry(priv, peer);
1049
1050         if (sta_ptr && (sta_ptr->tdls_status != TDLS_SETUP_FAILURE)) {
1051                 mwifiex_dbg(priv->adapter, MSG,
1052                             "tdls: enable link %pM success\n", peer);
1053
1054                 sta_ptr->tdls_status = TDLS_SETUP_COMPLETE;
1055
1056                 mcs = sta_ptr->tdls_cap.ht_capb.mcs;
1057                 if (mcs.rx_mask[0] != 0xff)
1058                         sta_ptr->is_11n_enabled = true;
1059                 if (sta_ptr->is_11n_enabled) {
1060                         if (le16_to_cpu(sta_ptr->tdls_cap.ht_capb.cap_info) &
1061                             IEEE80211_HT_CAP_MAX_AMSDU)
1062                                 sta_ptr->max_amsdu =
1063                                         MWIFIEX_TX_DATA_BUF_SIZE_8K;
1064                         else
1065                                 sta_ptr->max_amsdu =
1066                                         MWIFIEX_TX_DATA_BUF_SIZE_4K;
1067
1068                         for (i = 0; i < MAX_NUM_TID; i++)
1069                                 sta_ptr->ampdu_sta[i] =
1070                                               priv->aggr_prio_tbl[i].ampdu_user;
1071                 } else {
1072                         for (i = 0; i < MAX_NUM_TID; i++)
1073                                 sta_ptr->ampdu_sta[i] = BA_STREAM_NOT_ALLOWED;
1074                 }
1075                 if (sta_ptr->tdls_cap.extcap.ext_capab[3] &
1076                     WLAN_EXT_CAPA4_TDLS_CHAN_SWITCH) {
1077                         mwifiex_config_tdls_enable(priv);
1078                         mwifiex_config_tdls_cs_params(priv);
1079                 }
1080
1081                 memset(sta_ptr->rx_seq, 0xff, sizeof(sta_ptr->rx_seq));
1082                 mwifiex_restore_tdls_packets(priv, peer, TDLS_SETUP_COMPLETE);
1083                 mwifiex_auto_tdls_update_peer_status(priv, peer,
1084                                                      TDLS_SETUP_COMPLETE);
1085         } else {
1086                 mwifiex_dbg(priv->adapter, ERROR,
1087                             "tdls: enable link %pM failed\n", peer);
1088                 if (sta_ptr) {
1089                         mwifiex_11n_cleanup_reorder_tbl(priv);
1090                         spin_lock_irqsave(&priv->wmm.ra_list_spinlock,
1091                                           flags);
1092                         mwifiex_11n_delete_all_tx_ba_stream_tbl(priv);
1093                         spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock,
1094                                                flags);
1095                         mwifiex_del_sta_entry(priv, peer);
1096                 }
1097                 mwifiex_restore_tdls_packets(priv, peer, TDLS_LINK_TEARDOWN);
1098                 mwifiex_auto_tdls_update_peer_status(priv, peer,
1099                                                      TDLS_NOT_SETUP);
1100
1101                 return -1;
1102         }
1103
1104         return 0;
1105 }
1106
1107 int mwifiex_tdls_oper(struct mwifiex_private *priv, const u8 *peer, u8 action)
1108 {
1109         switch (action) {
1110         case MWIFIEX_TDLS_ENABLE_LINK:
1111                 return mwifiex_tdls_process_enable_link(priv, peer);
1112         case MWIFIEX_TDLS_DISABLE_LINK:
1113                 return mwifiex_tdls_process_disable_link(priv, peer);
1114         case MWIFIEX_TDLS_CREATE_LINK:
1115                 return mwifiex_tdls_process_create_link(priv, peer);
1116         case MWIFIEX_TDLS_CONFIG_LINK:
1117                 return mwifiex_tdls_process_config_link(priv, peer);
1118         }
1119         return 0;
1120 }
1121
1122 int mwifiex_get_tdls_link_status(struct mwifiex_private *priv, const u8 *mac)
1123 {
1124         struct mwifiex_sta_node *sta_ptr;
1125
1126         sta_ptr = mwifiex_get_sta_entry(priv, mac);
1127         if (sta_ptr)
1128                 return sta_ptr->tdls_status;
1129
1130         return TDLS_NOT_SETUP;
1131 }
1132
1133 int mwifiex_get_tdls_list(struct mwifiex_private *priv,
1134                           struct tdls_peer_info *buf)
1135 {
1136         struct mwifiex_sta_node *sta_ptr;
1137         struct tdls_peer_info *peer = buf;
1138         int count = 0;
1139         unsigned long flags;
1140
1141         if (!ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info))
1142                 return 0;
1143
1144         /* make sure we are in station mode and connected */
1145         if (!(priv->bss_type == MWIFIEX_BSS_TYPE_STA && priv->media_connected))
1146                 return 0;
1147
1148         spin_lock_irqsave(&priv->sta_list_spinlock, flags);
1149         list_for_each_entry(sta_ptr, &priv->sta_list, list) {
1150                 if (mwifiex_is_tdls_link_setup(sta_ptr->tdls_status)) {
1151                         ether_addr_copy(peer->peer_addr, sta_ptr->mac_addr);
1152                         peer++;
1153                         count++;
1154                         if (count >= MWIFIEX_MAX_TDLS_PEER_SUPPORTED)
1155                                 break;
1156                 }
1157         }
1158         spin_unlock_irqrestore(&priv->sta_list_spinlock, flags);
1159
1160         return count;
1161 }
1162
1163 void mwifiex_disable_all_tdls_links(struct mwifiex_private *priv)
1164 {
1165         struct mwifiex_sta_node *sta_ptr;
1166         struct mwifiex_ds_tdls_oper tdls_oper;
1167         unsigned long flags;
1168
1169         if (list_empty(&priv->sta_list))
1170                 return;
1171
1172         list_for_each_entry(sta_ptr, &priv->sta_list, list) {
1173                 memset(&tdls_oper, 0, sizeof(struct mwifiex_ds_tdls_oper));
1174
1175                 if (sta_ptr->is_11n_enabled) {
1176                         mwifiex_11n_cleanup_reorder_tbl(priv);
1177                         spin_lock_irqsave(&priv->wmm.ra_list_spinlock,
1178                                           flags);
1179                         mwifiex_11n_delete_all_tx_ba_stream_tbl(priv);
1180                         spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock,
1181                                                flags);
1182                 }
1183
1184                 mwifiex_restore_tdls_packets(priv, sta_ptr->mac_addr,
1185                                              TDLS_LINK_TEARDOWN);
1186                 memcpy(&tdls_oper.peer_mac, sta_ptr->mac_addr, ETH_ALEN);
1187                 tdls_oper.tdls_action = MWIFIEX_TDLS_DISABLE_LINK;
1188                 if (mwifiex_send_cmd(priv, HostCmd_CMD_TDLS_OPER,
1189                                      HostCmd_ACT_GEN_SET, 0, &tdls_oper, false))
1190                         mwifiex_dbg(priv->adapter, ERROR,
1191                                     "Disable link failed for TDLS peer %pM",
1192                                     sta_ptr->mac_addr);
1193         }
1194
1195         mwifiex_del_all_sta_list(priv);
1196 }
1197
1198 int mwifiex_tdls_check_tx(struct mwifiex_private *priv, struct sk_buff *skb)
1199 {
1200         struct mwifiex_auto_tdls_peer *peer;
1201         unsigned long flags;
1202         u8 mac[ETH_ALEN];
1203
1204         ether_addr_copy(mac, skb->data);
1205
1206         spin_lock_irqsave(&priv->auto_tdls_lock, flags);
1207         list_for_each_entry(peer, &priv->auto_tdls_list, list) {
1208                 if (!memcmp(mac, peer->mac_addr, ETH_ALEN)) {
1209                         if (peer->rssi <= MWIFIEX_TDLS_RSSI_HIGH &&
1210                             peer->tdls_status == TDLS_NOT_SETUP &&
1211                             (peer->failure_count <
1212                              MWIFIEX_TDLS_MAX_FAIL_COUNT)) {
1213                                 peer->tdls_status = TDLS_SETUP_INPROGRESS;
1214                                 mwifiex_dbg(priv->adapter, INFO,
1215                                             "setup TDLS link, peer=%pM rssi=%d\n",
1216                                             peer->mac_addr, peer->rssi);
1217
1218                                 cfg80211_tdls_oper_request(priv->netdev,
1219                                                            peer->mac_addr,
1220                                                            NL80211_TDLS_SETUP,
1221                                                            0, GFP_ATOMIC);
1222                                 peer->do_setup = false;
1223                                 priv->check_tdls_tx = false;
1224                         } else if (peer->failure_count <
1225                                    MWIFIEX_TDLS_MAX_FAIL_COUNT &&
1226                                    peer->do_discover) {
1227                                 mwifiex_send_tdls_data_frame(priv,
1228                                                              peer->mac_addr,
1229                                                     WLAN_TDLS_DISCOVERY_REQUEST,
1230                                                              1, 0, NULL, 0);
1231                                 peer->do_discover = false;
1232                         }
1233                 }
1234         }
1235         spin_unlock_irqrestore(&priv->auto_tdls_lock, flags);
1236
1237         return 0;
1238 }
1239
1240 void mwifiex_flush_auto_tdls_list(struct mwifiex_private *priv)
1241 {
1242         struct mwifiex_auto_tdls_peer *peer, *tmp_node;
1243         unsigned long flags;
1244
1245         spin_lock_irqsave(&priv->auto_tdls_lock, flags);
1246         list_for_each_entry_safe(peer, tmp_node, &priv->auto_tdls_list, list) {
1247                 list_del(&peer->list);
1248                 kfree(peer);
1249         }
1250
1251         INIT_LIST_HEAD(&priv->auto_tdls_list);
1252         spin_unlock_irqrestore(&priv->auto_tdls_lock, flags);
1253         priv->check_tdls_tx = false;
1254 }
1255
1256 void mwifiex_add_auto_tdls_peer(struct mwifiex_private *priv, const u8 *mac)
1257 {
1258         struct mwifiex_auto_tdls_peer *tdls_peer;
1259         unsigned long flags;
1260
1261         if (!priv->adapter->auto_tdls)
1262                 return;
1263
1264         spin_lock_irqsave(&priv->auto_tdls_lock, flags);
1265         list_for_each_entry(tdls_peer, &priv->auto_tdls_list, list) {
1266                 if (!memcmp(tdls_peer->mac_addr, mac, ETH_ALEN)) {
1267                         tdls_peer->tdls_status = TDLS_SETUP_INPROGRESS;
1268                         tdls_peer->rssi_jiffies = jiffies;
1269                         spin_unlock_irqrestore(&priv->auto_tdls_lock, flags);
1270                         return;
1271                 }
1272         }
1273
1274         /* create new TDLS peer */
1275         tdls_peer = kzalloc(sizeof(*tdls_peer), GFP_ATOMIC);
1276         if (tdls_peer) {
1277                 ether_addr_copy(tdls_peer->mac_addr, mac);
1278                 tdls_peer->tdls_status = TDLS_SETUP_INPROGRESS;
1279                 tdls_peer->rssi_jiffies = jiffies;
1280                 INIT_LIST_HEAD(&tdls_peer->list);
1281                 list_add_tail(&tdls_peer->list, &priv->auto_tdls_list);
1282                 mwifiex_dbg(priv->adapter, INFO,
1283                             "Add auto TDLS peer= %pM to list\n", mac);
1284         }
1285
1286         spin_unlock_irqrestore(&priv->auto_tdls_lock, flags);
1287 }
1288
1289 void mwifiex_auto_tdls_update_peer_status(struct mwifiex_private *priv,
1290                                           const u8 *mac, u8 link_status)
1291 {
1292         struct mwifiex_auto_tdls_peer *peer;
1293         unsigned long flags;
1294
1295         if (!priv->adapter->auto_tdls)
1296                 return;
1297
1298         spin_lock_irqsave(&priv->auto_tdls_lock, flags);
1299         list_for_each_entry(peer, &priv->auto_tdls_list, list) {
1300                 if (!memcmp(peer->mac_addr, mac, ETH_ALEN)) {
1301                         if ((link_status == TDLS_NOT_SETUP) &&
1302                             (peer->tdls_status == TDLS_SETUP_INPROGRESS))
1303                                 peer->failure_count++;
1304                         else if (mwifiex_is_tdls_link_setup(link_status))
1305                                 peer->failure_count = 0;
1306
1307                         peer->tdls_status = link_status;
1308                         break;
1309                 }
1310         }
1311         spin_unlock_irqrestore(&priv->auto_tdls_lock, flags);
1312 }
1313
1314 void mwifiex_auto_tdls_update_peer_signal(struct mwifiex_private *priv,
1315                                           u8 *mac, s8 snr, s8 nflr)
1316 {
1317         struct mwifiex_auto_tdls_peer *peer;
1318         unsigned long flags;
1319
1320         if (!priv->adapter->auto_tdls)
1321                 return;
1322
1323         spin_lock_irqsave(&priv->auto_tdls_lock, flags);
1324         list_for_each_entry(peer, &priv->auto_tdls_list, list) {
1325                 if (!memcmp(peer->mac_addr, mac, ETH_ALEN)) {
1326                         peer->rssi = nflr - snr;
1327                         peer->rssi_jiffies = jiffies;
1328                         break;
1329                 }
1330         }
1331         spin_unlock_irqrestore(&priv->auto_tdls_lock, flags);
1332 }
1333
1334 void mwifiex_check_auto_tdls(unsigned long context)
1335 {
1336         struct mwifiex_private *priv = (struct mwifiex_private *)context;
1337         struct mwifiex_auto_tdls_peer *tdls_peer;
1338         unsigned long flags;
1339         u16 reason = WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED;
1340
1341         if (WARN_ON_ONCE(!priv || !priv->adapter)) {
1342                 pr_err("mwifiex: %s: adapter or private structure is NULL\n",
1343                        __func__);
1344                 return;
1345         }
1346
1347         if (unlikely(!priv->adapter->auto_tdls))
1348                 return;
1349
1350         if (!priv->auto_tdls_timer_active) {
1351                 mwifiex_dbg(priv->adapter, INFO,
1352                             "auto TDLS timer inactive; return");
1353                 return;
1354         }
1355
1356         priv->check_tdls_tx = false;
1357
1358         if (list_empty(&priv->auto_tdls_list)) {
1359                 mod_timer(&priv->auto_tdls_timer,
1360                           jiffies +
1361                           msecs_to_jiffies(MWIFIEX_TIMER_10S));
1362                 return;
1363         }
1364
1365         spin_lock_irqsave(&priv->auto_tdls_lock, flags);
1366         list_for_each_entry(tdls_peer, &priv->auto_tdls_list, list) {
1367                 if ((jiffies - tdls_peer->rssi_jiffies) >
1368                     (MWIFIEX_AUTO_TDLS_IDLE_TIME * HZ)) {
1369                         tdls_peer->rssi = 0;
1370                         tdls_peer->do_discover = true;
1371                         priv->check_tdls_tx = true;
1372                 }
1373
1374                 if (((tdls_peer->rssi >= MWIFIEX_TDLS_RSSI_LOW) ||
1375                      !tdls_peer->rssi) &&
1376                     mwifiex_is_tdls_link_setup(tdls_peer->tdls_status)) {
1377                         tdls_peer->tdls_status = TDLS_LINK_TEARDOWN;
1378                         mwifiex_dbg(priv->adapter, MSG,
1379                                     "teardown TDLS link,peer=%pM rssi=%d\n",
1380                                     tdls_peer->mac_addr, -tdls_peer->rssi);
1381                         tdls_peer->do_discover = true;
1382                         priv->check_tdls_tx = true;
1383                         cfg80211_tdls_oper_request(priv->netdev,
1384                                                    tdls_peer->mac_addr,
1385                                                    NL80211_TDLS_TEARDOWN,
1386                                                    reason, GFP_ATOMIC);
1387                 } else if (tdls_peer->rssi &&
1388                            tdls_peer->rssi <= MWIFIEX_TDLS_RSSI_HIGH &&
1389                            tdls_peer->tdls_status == TDLS_NOT_SETUP &&
1390                            tdls_peer->failure_count <
1391                            MWIFIEX_TDLS_MAX_FAIL_COUNT) {
1392                                 priv->check_tdls_tx = true;
1393                                 tdls_peer->do_setup = true;
1394                                 mwifiex_dbg(priv->adapter, INFO,
1395                                             "check TDLS with peer=%pM\t"
1396                                             "rssi=%d\n", tdls_peer->mac_addr,
1397                                             tdls_peer->rssi);
1398                 }
1399         }
1400         spin_unlock_irqrestore(&priv->auto_tdls_lock, flags);
1401
1402         mod_timer(&priv->auto_tdls_timer,
1403                   jiffies + msecs_to_jiffies(MWIFIEX_TIMER_10S));
1404 }
1405
1406 void mwifiex_setup_auto_tdls_timer(struct mwifiex_private *priv)
1407 {
1408         setup_timer(&priv->auto_tdls_timer, mwifiex_check_auto_tdls,
1409                     (unsigned long)priv);
1410         priv->auto_tdls_timer_active = true;
1411         mod_timer(&priv->auto_tdls_timer,
1412                   jiffies + msecs_to_jiffies(MWIFIEX_TIMER_10S));
1413 }
1414
1415 void mwifiex_clean_auto_tdls(struct mwifiex_private *priv)
1416 {
1417         if (ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
1418             priv->adapter->auto_tdls &&
1419             priv->bss_type == MWIFIEX_BSS_TYPE_STA) {
1420                 priv->auto_tdls_timer_active = false;
1421                 del_timer(&priv->auto_tdls_timer);
1422                 mwifiex_flush_auto_tdls_list(priv);
1423         }
1424 }
1425
1426 static int mwifiex_config_tdls(struct mwifiex_private *priv, u8 enable)
1427 {
1428         struct mwifiex_tdls_config config;
1429
1430         config.enable = cpu_to_le16(enable);
1431         return mwifiex_send_cmd(priv, HostCmd_CMD_TDLS_CONFIG,
1432                                 ACT_TDLS_CS_ENABLE_CONFIG, 0, &config, true);
1433 }
1434
1435 int mwifiex_config_tdls_enable(struct mwifiex_private *priv)
1436 {
1437         return mwifiex_config_tdls(priv, true);
1438 }
1439
1440 int mwifiex_config_tdls_disable(struct mwifiex_private *priv)
1441 {
1442         return mwifiex_config_tdls(priv, false);
1443 }
1444
1445 int mwifiex_config_tdls_cs_params(struct mwifiex_private *priv)
1446 {
1447         struct mwifiex_tdls_config_cs_params config_tdls_cs_params;
1448
1449         config_tdls_cs_params.unit_time = MWIFIEX_DEF_CS_UNIT_TIME;
1450         config_tdls_cs_params.thr_otherlink = MWIFIEX_DEF_CS_THR_OTHERLINK;
1451         config_tdls_cs_params.thr_directlink = MWIFIEX_DEF_THR_DIRECTLINK;
1452
1453         return mwifiex_send_cmd(priv, HostCmd_CMD_TDLS_CONFIG,
1454                                 ACT_TDLS_CS_PARAMS, 0,
1455                                 &config_tdls_cs_params, true);
1456 }
1457
1458 int mwifiex_stop_tdls_cs(struct mwifiex_private *priv, const u8 *peer_mac)
1459 {
1460         struct mwifiex_tdls_stop_cs_params stop_tdls_cs_params;
1461
1462         ether_addr_copy(stop_tdls_cs_params.peer_mac, peer_mac);
1463
1464         return mwifiex_send_cmd(priv, HostCmd_CMD_TDLS_CONFIG,
1465                                 ACT_TDLS_CS_STOP, 0,
1466                                 &stop_tdls_cs_params, true);
1467 }
1468
1469 int mwifiex_start_tdls_cs(struct mwifiex_private *priv, const u8 *peer_mac,
1470                           u8 primary_chan, u8 second_chan_offset, u8 band)
1471 {
1472         struct mwifiex_tdls_init_cs_params start_tdls_cs_params;
1473
1474         ether_addr_copy(start_tdls_cs_params.peer_mac, peer_mac);
1475         start_tdls_cs_params.primary_chan = primary_chan;
1476         start_tdls_cs_params.second_chan_offset = second_chan_offset;
1477         start_tdls_cs_params.band = band;
1478
1479         start_tdls_cs_params.switch_time = cpu_to_le16(MWIFIEX_DEF_CS_TIME);
1480         start_tdls_cs_params.switch_timeout =
1481                                         cpu_to_le16(MWIFIEX_DEF_CS_TIMEOUT);
1482         start_tdls_cs_params.reg_class = MWIFIEX_DEF_CS_REG_CLASS;
1483         start_tdls_cs_params.periodicity = MWIFIEX_DEF_CS_PERIODICITY;
1484
1485         return mwifiex_send_cmd(priv, HostCmd_CMD_TDLS_CONFIG,
1486                                 ACT_TDLS_CS_INIT, 0,
1487                                 &start_tdls_cs_params, true);
1488 }