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