cfg80211: remove enum ieee80211_band
[cascardo/linux.git] / drivers / net / wireless / marvell / mwifiex / cfg80211.c
1 /*
2  * Marvell Wireless LAN device driver: CFG80211
3  *
4  * Copyright (C) 2011-2014, Marvell International Ltd.
5  *
6  * This software file (the "File") is distributed by Marvell International
7  * Ltd. under the terms of the GNU General Public License Version 2, June 1991
8  * (the "License").  You may use, redistribute and/or modify this File in
9  * accordance with the terms and conditions of the License, a copy of which
10  * is available by writing to the Free Software Foundation, Inc.,
11  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
12  * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
13  *
14  * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
16  * ARE EXPRESSLY DISCLAIMED.  The License provides additional details about
17  * this warranty disclaimer.
18  */
19
20 #include "cfg80211.h"
21 #include "main.h"
22 #include "11n.h"
23 #include "wmm.h"
24
25 static char *reg_alpha2;
26 module_param(reg_alpha2, charp, 0);
27
28 static const struct ieee80211_iface_limit mwifiex_ap_sta_limits[] = {
29         {
30                 .max = 3, .types = BIT(NL80211_IFTYPE_STATION) |
31                                    BIT(NL80211_IFTYPE_P2P_GO) |
32                                    BIT(NL80211_IFTYPE_P2P_CLIENT) |
33                                    BIT(NL80211_IFTYPE_AP),
34         },
35 };
36
37 static const struct ieee80211_iface_combination
38 mwifiex_iface_comb_ap_sta = {
39         .limits = mwifiex_ap_sta_limits,
40         .num_different_channels = 1,
41         .n_limits = ARRAY_SIZE(mwifiex_ap_sta_limits),
42         .max_interfaces = MWIFIEX_MAX_BSS_NUM,
43         .beacon_int_infra_match = true,
44         .radar_detect_widths =  BIT(NL80211_CHAN_WIDTH_20_NOHT) |
45                                 BIT(NL80211_CHAN_WIDTH_20) |
46                                 BIT(NL80211_CHAN_WIDTH_40),
47 };
48
49 static const struct ieee80211_iface_combination
50 mwifiex_iface_comb_ap_sta_vht = {
51         .limits = mwifiex_ap_sta_limits,
52         .num_different_channels = 1,
53         .n_limits = ARRAY_SIZE(mwifiex_ap_sta_limits),
54         .max_interfaces = MWIFIEX_MAX_BSS_NUM,
55         .beacon_int_infra_match = true,
56         .radar_detect_widths =  BIT(NL80211_CHAN_WIDTH_20_NOHT) |
57                                 BIT(NL80211_CHAN_WIDTH_20) |
58                                 BIT(NL80211_CHAN_WIDTH_40) |
59                                 BIT(NL80211_CHAN_WIDTH_80),
60 };
61
62 static const struct
63 ieee80211_iface_combination mwifiex_iface_comb_ap_sta_drcs = {
64         .limits = mwifiex_ap_sta_limits,
65         .num_different_channels = 2,
66         .n_limits = ARRAY_SIZE(mwifiex_ap_sta_limits),
67         .max_interfaces = MWIFIEX_MAX_BSS_NUM,
68         .beacon_int_infra_match = true,
69 };
70
71 /*
72  * This function maps the nl802.11 channel type into driver channel type.
73  *
74  * The mapping is as follows -
75  *      NL80211_CHAN_NO_HT     -> IEEE80211_HT_PARAM_CHA_SEC_NONE
76  *      NL80211_CHAN_HT20      -> IEEE80211_HT_PARAM_CHA_SEC_NONE
77  *      NL80211_CHAN_HT40PLUS  -> IEEE80211_HT_PARAM_CHA_SEC_ABOVE
78  *      NL80211_CHAN_HT40MINUS -> IEEE80211_HT_PARAM_CHA_SEC_BELOW
79  *      Others                 -> IEEE80211_HT_PARAM_CHA_SEC_NONE
80  */
81 u8 mwifiex_chan_type_to_sec_chan_offset(enum nl80211_channel_type chan_type)
82 {
83         switch (chan_type) {
84         case NL80211_CHAN_NO_HT:
85         case NL80211_CHAN_HT20:
86                 return IEEE80211_HT_PARAM_CHA_SEC_NONE;
87         case NL80211_CHAN_HT40PLUS:
88                 return IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
89         case NL80211_CHAN_HT40MINUS:
90                 return IEEE80211_HT_PARAM_CHA_SEC_BELOW;
91         default:
92                 return IEEE80211_HT_PARAM_CHA_SEC_NONE;
93         }
94 }
95
96 /* This function maps IEEE HT secondary channel type to NL80211 channel type
97  */
98 u8 mwifiex_sec_chan_offset_to_chan_type(u8 second_chan_offset)
99 {
100         switch (second_chan_offset) {
101         case IEEE80211_HT_PARAM_CHA_SEC_NONE:
102                 return NL80211_CHAN_HT20;
103         case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
104                 return NL80211_CHAN_HT40PLUS;
105         case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
106                 return NL80211_CHAN_HT40MINUS;
107         default:
108                 return NL80211_CHAN_HT20;
109         }
110 }
111
112 /*
113  * This function checks whether WEP is set.
114  */
115 static int
116 mwifiex_is_alg_wep(u32 cipher)
117 {
118         switch (cipher) {
119         case WLAN_CIPHER_SUITE_WEP40:
120         case WLAN_CIPHER_SUITE_WEP104:
121                 return 1;
122         default:
123                 break;
124         }
125
126         return 0;
127 }
128
129 /*
130  * This function retrieves the private structure from kernel wiphy structure.
131  */
132 static void *mwifiex_cfg80211_get_adapter(struct wiphy *wiphy)
133 {
134         return (void *) (*(unsigned long *) wiphy_priv(wiphy));
135 }
136
137 /*
138  * CFG802.11 operation handler to delete a network key.
139  */
140 static int
141 mwifiex_cfg80211_del_key(struct wiphy *wiphy, struct net_device *netdev,
142                          u8 key_index, bool pairwise, const u8 *mac_addr)
143 {
144         struct mwifiex_private *priv = mwifiex_netdev_get_priv(netdev);
145         const u8 bc_mac[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
146         const u8 *peer_mac = pairwise ? mac_addr : bc_mac;
147
148         if (mwifiex_set_encode(priv, NULL, NULL, 0, key_index, peer_mac, 1)) {
149                 mwifiex_dbg(priv->adapter, ERROR, "deleting the crypto keys\n");
150                 return -EFAULT;
151         }
152
153         mwifiex_dbg(priv->adapter, INFO, "info: crypto keys deleted\n");
154         return 0;
155 }
156
157 /*
158  * This function forms an skb for management frame.
159  */
160 static int
161 mwifiex_form_mgmt_frame(struct sk_buff *skb, const u8 *buf, size_t len)
162 {
163         u8 addr[ETH_ALEN] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
164         u16 pkt_len;
165         u32 tx_control = 0, pkt_type = PKT_TYPE_MGMT;
166
167         pkt_len = len + ETH_ALEN;
168
169         skb_reserve(skb, MWIFIEX_MIN_DATA_HEADER_LEN +
170                     MWIFIEX_MGMT_FRAME_HEADER_SIZE + sizeof(pkt_len));
171         memcpy(skb_push(skb, sizeof(pkt_len)), &pkt_len, sizeof(pkt_len));
172
173         memcpy(skb_push(skb, sizeof(tx_control)),
174                &tx_control, sizeof(tx_control));
175
176         memcpy(skb_push(skb, sizeof(pkt_type)), &pkt_type, sizeof(pkt_type));
177
178         /* Add packet data and address4 */
179         memcpy(skb_put(skb, sizeof(struct ieee80211_hdr_3addr)), buf,
180                sizeof(struct ieee80211_hdr_3addr));
181         memcpy(skb_put(skb, ETH_ALEN), addr, ETH_ALEN);
182         memcpy(skb_put(skb, len - sizeof(struct ieee80211_hdr_3addr)),
183                buf + sizeof(struct ieee80211_hdr_3addr),
184                len - sizeof(struct ieee80211_hdr_3addr));
185
186         skb->priority = LOW_PRIO_TID;
187         __net_timestamp(skb);
188
189         return 0;
190 }
191
192 /*
193  * CFG802.11 operation handler to transmit a management frame.
194  */
195 static int
196 mwifiex_cfg80211_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev,
197                          struct cfg80211_mgmt_tx_params *params, u64 *cookie)
198 {
199         const u8 *buf = params->buf;
200         size_t len = params->len;
201         struct sk_buff *skb;
202         u16 pkt_len;
203         const struct ieee80211_mgmt *mgmt;
204         struct mwifiex_txinfo *tx_info;
205         struct mwifiex_private *priv = mwifiex_netdev_get_priv(wdev->netdev);
206
207         if (!buf || !len) {
208                 mwifiex_dbg(priv->adapter, ERROR, "invalid buffer and length\n");
209                 return -EFAULT;
210         }
211
212         mgmt = (const struct ieee80211_mgmt *)buf;
213         if (GET_BSS_ROLE(priv) != MWIFIEX_BSS_ROLE_STA &&
214             ieee80211_is_probe_resp(mgmt->frame_control)) {
215                 /* Since we support offload probe resp, we need to skip probe
216                  * resp in AP or GO mode */
217                 mwifiex_dbg(priv->adapter, INFO,
218                             "info: skip to send probe resp in AP or GO mode\n");
219                 return 0;
220         }
221
222         pkt_len = len + ETH_ALEN;
223         skb = dev_alloc_skb(MWIFIEX_MIN_DATA_HEADER_LEN +
224                             MWIFIEX_MGMT_FRAME_HEADER_SIZE +
225                             pkt_len + sizeof(pkt_len));
226
227         if (!skb) {
228                 mwifiex_dbg(priv->adapter, ERROR,
229                             "allocate skb failed for management frame\n");
230                 return -ENOMEM;
231         }
232
233         tx_info = MWIFIEX_SKB_TXCB(skb);
234         memset(tx_info, 0, sizeof(*tx_info));
235         tx_info->bss_num = priv->bss_num;
236         tx_info->bss_type = priv->bss_type;
237         tx_info->pkt_len = pkt_len;
238
239         mwifiex_form_mgmt_frame(skb, buf, len);
240         *cookie = prandom_u32() | 1;
241
242         if (ieee80211_is_action(mgmt->frame_control))
243                 skb = mwifiex_clone_skb_for_tx_status(priv,
244                                                       skb,
245                                 MWIFIEX_BUF_FLAG_ACTION_TX_STATUS, cookie);
246         else
247                 cfg80211_mgmt_tx_status(wdev, *cookie, buf, len, true,
248                                         GFP_ATOMIC);
249
250         mwifiex_queue_tx_pkt(priv, skb);
251
252         mwifiex_dbg(priv->adapter, INFO, "info: management frame transmitted\n");
253         return 0;
254 }
255
256 /*
257  * CFG802.11 operation handler to register a mgmt frame.
258  */
259 static void
260 mwifiex_cfg80211_mgmt_frame_register(struct wiphy *wiphy,
261                                      struct wireless_dev *wdev,
262                                      u16 frame_type, bool reg)
263 {
264         struct mwifiex_private *priv = mwifiex_netdev_get_priv(wdev->netdev);
265         u32 mask;
266
267         if (reg)
268                 mask = priv->mgmt_frame_mask | BIT(frame_type >> 4);
269         else
270                 mask = priv->mgmt_frame_mask & ~BIT(frame_type >> 4);
271
272         if (mask != priv->mgmt_frame_mask) {
273                 priv->mgmt_frame_mask = mask;
274                 mwifiex_send_cmd(priv, HostCmd_CMD_MGMT_FRAME_REG,
275                                  HostCmd_ACT_GEN_SET, 0,
276                                  &priv->mgmt_frame_mask, false);
277                 mwifiex_dbg(priv->adapter, INFO, "info: mgmt frame registered\n");
278         }
279 }
280
281 /*
282  * CFG802.11 operation handler to remain on channel.
283  */
284 static int
285 mwifiex_cfg80211_remain_on_channel(struct wiphy *wiphy,
286                                    struct wireless_dev *wdev,
287                                    struct ieee80211_channel *chan,
288                                    unsigned int duration, u64 *cookie)
289 {
290         struct mwifiex_private *priv = mwifiex_netdev_get_priv(wdev->netdev);
291         int ret;
292
293         if (!chan || !cookie) {
294                 mwifiex_dbg(priv->adapter, ERROR, "Invalid parameter for ROC\n");
295                 return -EINVAL;
296         }
297
298         if (priv->roc_cfg.cookie) {
299                 mwifiex_dbg(priv->adapter, INFO,
300                             "info: ongoing ROC, cookie = 0x%llx\n",
301                             priv->roc_cfg.cookie);
302                 return -EBUSY;
303         }
304
305         ret = mwifiex_remain_on_chan_cfg(priv, HostCmd_ACT_GEN_SET, chan,
306                                          duration);
307
308         if (!ret) {
309                 *cookie = prandom_u32() | 1;
310                 priv->roc_cfg.cookie = *cookie;
311                 priv->roc_cfg.chan = *chan;
312
313                 cfg80211_ready_on_channel(wdev, *cookie, chan,
314                                           duration, GFP_ATOMIC);
315
316                 mwifiex_dbg(priv->adapter, INFO,
317                             "info: ROC, cookie = 0x%llx\n", *cookie);
318         }
319
320         return ret;
321 }
322
323 /*
324  * CFG802.11 operation handler to cancel remain on channel.
325  */
326 static int
327 mwifiex_cfg80211_cancel_remain_on_channel(struct wiphy *wiphy,
328                                           struct wireless_dev *wdev, u64 cookie)
329 {
330         struct mwifiex_private *priv = mwifiex_netdev_get_priv(wdev->netdev);
331         int ret;
332
333         if (cookie != priv->roc_cfg.cookie)
334                 return -ENOENT;
335
336         ret = mwifiex_remain_on_chan_cfg(priv, HostCmd_ACT_GEN_REMOVE,
337                                          &priv->roc_cfg.chan, 0);
338
339         if (!ret) {
340                 cfg80211_remain_on_channel_expired(wdev, cookie,
341                                                    &priv->roc_cfg.chan,
342                                                    GFP_ATOMIC);
343
344                 memset(&priv->roc_cfg, 0, sizeof(struct mwifiex_roc_cfg));
345
346                 mwifiex_dbg(priv->adapter, INFO,
347                             "info: cancel ROC, cookie = 0x%llx\n", cookie);
348         }
349
350         return ret;
351 }
352
353 /*
354  * CFG802.11 operation handler to set Tx power.
355  */
356 static int
357 mwifiex_cfg80211_set_tx_power(struct wiphy *wiphy,
358                               struct wireless_dev *wdev,
359                               enum nl80211_tx_power_setting type,
360                               int mbm)
361 {
362         struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
363         struct mwifiex_private *priv;
364         struct mwifiex_power_cfg power_cfg;
365         int dbm = MBM_TO_DBM(mbm);
366
367         if (type == NL80211_TX_POWER_FIXED) {
368                 power_cfg.is_power_auto = 0;
369                 power_cfg.power_level = dbm;
370         } else {
371                 power_cfg.is_power_auto = 1;
372         }
373
374         priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
375
376         return mwifiex_set_tx_power(priv, &power_cfg);
377 }
378
379 /*
380  * CFG802.11 operation handler to set Power Save option.
381  *
382  * The timeout value, if provided, is currently ignored.
383  */
384 static int
385 mwifiex_cfg80211_set_power_mgmt(struct wiphy *wiphy,
386                                 struct net_device *dev,
387                                 bool enabled, int timeout)
388 {
389         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
390         u32 ps_mode;
391
392         if (timeout)
393                 mwifiex_dbg(priv->adapter, INFO,
394                             "info: ignore timeout value for IEEE Power Save\n");
395
396         ps_mode = enabled;
397
398         return mwifiex_drv_set_power(priv, &ps_mode);
399 }
400
401 /*
402  * CFG802.11 operation handler to set the default network key.
403  */
404 static int
405 mwifiex_cfg80211_set_default_key(struct wiphy *wiphy, struct net_device *netdev,
406                                  u8 key_index, bool unicast,
407                                  bool multicast)
408 {
409         struct mwifiex_private *priv = mwifiex_netdev_get_priv(netdev);
410
411         /* Return if WEP key not configured */
412         if (!priv->sec_info.wep_enabled)
413                 return 0;
414
415         if (priv->bss_type == MWIFIEX_BSS_TYPE_UAP) {
416                 priv->wep_key_curr_index = key_index;
417         } else if (mwifiex_set_encode(priv, NULL, NULL, 0, key_index,
418                                       NULL, 0)) {
419                 mwifiex_dbg(priv->adapter, ERROR, "set default Tx key index\n");
420                 return -EFAULT;
421         }
422
423         return 0;
424 }
425
426 /*
427  * CFG802.11 operation handler to add a network key.
428  */
429 static int
430 mwifiex_cfg80211_add_key(struct wiphy *wiphy, struct net_device *netdev,
431                          u8 key_index, bool pairwise, const u8 *mac_addr,
432                          struct key_params *params)
433 {
434         struct mwifiex_private *priv = mwifiex_netdev_get_priv(netdev);
435         struct mwifiex_wep_key *wep_key;
436         const u8 bc_mac[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
437         const u8 *peer_mac = pairwise ? mac_addr : bc_mac;
438
439         if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP &&
440             (params->cipher == WLAN_CIPHER_SUITE_WEP40 ||
441              params->cipher == WLAN_CIPHER_SUITE_WEP104)) {
442                 if (params->key && params->key_len) {
443                         wep_key = &priv->wep_key[key_index];
444                         memset(wep_key, 0, sizeof(struct mwifiex_wep_key));
445                         memcpy(wep_key->key_material, params->key,
446                                params->key_len);
447                         wep_key->key_index = key_index;
448                         wep_key->key_length = params->key_len;
449                         priv->sec_info.wep_enabled = 1;
450                 }
451                 return 0;
452         }
453
454         if (mwifiex_set_encode(priv, params, params->key, params->key_len,
455                                key_index, peer_mac, 0)) {
456                 mwifiex_dbg(priv->adapter, ERROR, "crypto keys added\n");
457                 return -EFAULT;
458         }
459
460         return 0;
461 }
462
463 /*
464  * This function sends domain information to the firmware.
465  *
466  * The following information are passed to the firmware -
467  *      - Country codes
468  *      - Sub bands (first channel, number of channels, maximum Tx power)
469  */
470 int mwifiex_send_domain_info_cmd_fw(struct wiphy *wiphy)
471 {
472         u8 no_of_triplet = 0;
473         struct ieee80211_country_ie_triplet *t;
474         u8 no_of_parsed_chan = 0;
475         u8 first_chan = 0, next_chan = 0, max_pwr = 0;
476         u8 i, flag = 0;
477         enum nl80211_band band;
478         struct ieee80211_supported_band *sband;
479         struct ieee80211_channel *ch;
480         struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
481         struct mwifiex_private *priv;
482         struct mwifiex_802_11d_domain_reg *domain_info = &adapter->domain_reg;
483
484         /* Set country code */
485         domain_info->country_code[0] = adapter->country_code[0];
486         domain_info->country_code[1] = adapter->country_code[1];
487         domain_info->country_code[2] = ' ';
488
489         band = mwifiex_band_to_radio_type(adapter->config_bands);
490         if (!wiphy->bands[band]) {
491                 mwifiex_dbg(adapter, ERROR,
492                             "11D: setting domain info in FW\n");
493                 return -1;
494         }
495
496         sband = wiphy->bands[band];
497
498         for (i = 0; i < sband->n_channels ; i++) {
499                 ch = &sband->channels[i];
500                 if (ch->flags & IEEE80211_CHAN_DISABLED)
501                         continue;
502
503                 if (!flag) {
504                         flag = 1;
505                         first_chan = (u32) ch->hw_value;
506                         next_chan = first_chan;
507                         max_pwr = ch->max_power;
508                         no_of_parsed_chan = 1;
509                         continue;
510                 }
511
512                 if (ch->hw_value == next_chan + 1 &&
513                     ch->max_power == max_pwr) {
514                         next_chan++;
515                         no_of_parsed_chan++;
516                 } else {
517                         t = &domain_info->triplet[no_of_triplet];
518                         t->chans.first_channel = first_chan;
519                         t->chans.num_channels = no_of_parsed_chan;
520                         t->chans.max_power = max_pwr;
521                         no_of_triplet++;
522                         first_chan = (u32) ch->hw_value;
523                         next_chan = first_chan;
524                         max_pwr = ch->max_power;
525                         no_of_parsed_chan = 1;
526                 }
527         }
528
529         if (flag) {
530                 t = &domain_info->triplet[no_of_triplet];
531                 t->chans.first_channel = first_chan;
532                 t->chans.num_channels = no_of_parsed_chan;
533                 t->chans.max_power = max_pwr;
534                 no_of_triplet++;
535         }
536
537         domain_info->no_of_triplet = no_of_triplet;
538
539         priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
540
541         if (mwifiex_send_cmd(priv, HostCmd_CMD_802_11D_DOMAIN_INFO,
542                              HostCmd_ACT_GEN_SET, 0, NULL, false)) {
543                 mwifiex_dbg(adapter, INFO,
544                             "11D: setting domain info in FW\n");
545                 return -1;
546         }
547
548         return 0;
549 }
550
551 /*
552  * CFG802.11 regulatory domain callback function.
553  *
554  * This function is called when the regulatory domain is changed due to the
555  * following reasons -
556  *      - Set by driver
557  *      - Set by system core
558  *      - Set by user
559  *      - Set bt Country IE
560  */
561 static void mwifiex_reg_notifier(struct wiphy *wiphy,
562                                  struct regulatory_request *request)
563 {
564         struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
565         struct mwifiex_private *priv = mwifiex_get_priv(adapter,
566                                                         MWIFIEX_BSS_ROLE_ANY);
567         mwifiex_dbg(adapter, INFO,
568                     "info: cfg80211 regulatory domain callback for %c%c\n",
569                     request->alpha2[0], request->alpha2[1]);
570
571         switch (request->initiator) {
572         case NL80211_REGDOM_SET_BY_DRIVER:
573         case NL80211_REGDOM_SET_BY_CORE:
574         case NL80211_REGDOM_SET_BY_USER:
575         case NL80211_REGDOM_SET_BY_COUNTRY_IE:
576                 break;
577         default:
578                 mwifiex_dbg(adapter, ERROR,
579                             "unknown regdom initiator: %d\n",
580                             request->initiator);
581                 return;
582         }
583
584         /* Don't send world or same regdom info to firmware */
585         if (strncmp(request->alpha2, "00", 2) &&
586             strncmp(request->alpha2, adapter->country_code,
587                     sizeof(request->alpha2))) {
588                 memcpy(adapter->country_code, request->alpha2,
589                        sizeof(request->alpha2));
590                 mwifiex_send_domain_info_cmd_fw(wiphy);
591                 mwifiex_dnld_txpwr_table(priv);
592         }
593 }
594
595 /*
596  * This function sets the fragmentation threshold.
597  *
598  * The fragmentation threshold value must lie between MWIFIEX_FRAG_MIN_VALUE
599  * and MWIFIEX_FRAG_MAX_VALUE.
600  */
601 static int
602 mwifiex_set_frag(struct mwifiex_private *priv, u32 frag_thr)
603 {
604         if (frag_thr < MWIFIEX_FRAG_MIN_VALUE ||
605             frag_thr > MWIFIEX_FRAG_MAX_VALUE)
606                 frag_thr = MWIFIEX_FRAG_MAX_VALUE;
607
608         return mwifiex_send_cmd(priv, HostCmd_CMD_802_11_SNMP_MIB,
609                                 HostCmd_ACT_GEN_SET, FRAG_THRESH_I,
610                                 &frag_thr, true);
611 }
612
613 /*
614  * This function sets the RTS threshold.
615
616  * The rts value must lie between MWIFIEX_RTS_MIN_VALUE
617  * and MWIFIEX_RTS_MAX_VALUE.
618  */
619 static int
620 mwifiex_set_rts(struct mwifiex_private *priv, u32 rts_thr)
621 {
622         if (rts_thr < MWIFIEX_RTS_MIN_VALUE || rts_thr > MWIFIEX_RTS_MAX_VALUE)
623                 rts_thr = MWIFIEX_RTS_MAX_VALUE;
624
625         return mwifiex_send_cmd(priv, HostCmd_CMD_802_11_SNMP_MIB,
626                                 HostCmd_ACT_GEN_SET, RTS_THRESH_I,
627                                 &rts_thr, true);
628 }
629
630 /*
631  * CFG802.11 operation handler to set wiphy parameters.
632  *
633  * This function can be used to set the RTS threshold and the
634  * Fragmentation threshold of the driver.
635  */
636 static int
637 mwifiex_cfg80211_set_wiphy_params(struct wiphy *wiphy, u32 changed)
638 {
639         struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
640         struct mwifiex_private *priv;
641         struct mwifiex_uap_bss_param *bss_cfg;
642         int ret;
643
644         priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
645
646         switch (priv->bss_role) {
647         case MWIFIEX_BSS_ROLE_UAP:
648                 if (priv->bss_started) {
649                         mwifiex_dbg(adapter, ERROR,
650                                     "cannot change wiphy params when bss started");
651                         return -EINVAL;
652                 }
653
654                 bss_cfg = kzalloc(sizeof(*bss_cfg), GFP_KERNEL);
655                 if (!bss_cfg)
656                         return -ENOMEM;
657
658                 mwifiex_set_sys_config_invalid_data(bss_cfg);
659
660                 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
661                         bss_cfg->rts_threshold = wiphy->rts_threshold;
662                 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
663                         bss_cfg->frag_threshold = wiphy->frag_threshold;
664                 if (changed & WIPHY_PARAM_RETRY_LONG)
665                         bss_cfg->retry_limit = wiphy->retry_long;
666
667                 ret = mwifiex_send_cmd(priv, HostCmd_CMD_UAP_SYS_CONFIG,
668                                        HostCmd_ACT_GEN_SET,
669                                        UAP_BSS_PARAMS_I, bss_cfg,
670                                        false);
671
672                 kfree(bss_cfg);
673                 if (ret) {
674                         mwifiex_dbg(adapter, ERROR,
675                                     "Failed to set wiphy phy params\n");
676                         return ret;
677                 }
678                 break;
679
680         case MWIFIEX_BSS_ROLE_STA:
681                 if (priv->media_connected) {
682                         mwifiex_dbg(adapter, ERROR,
683                                     "cannot change wiphy params when connected");
684                         return -EINVAL;
685                 }
686                 if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
687                         ret = mwifiex_set_rts(priv,
688                                               wiphy->rts_threshold);
689                         if (ret)
690                                 return ret;
691                 }
692                 if (changed & WIPHY_PARAM_FRAG_THRESHOLD) {
693                         ret = mwifiex_set_frag(priv,
694                                                wiphy->frag_threshold);
695                         if (ret)
696                                 return ret;
697                 }
698                 break;
699         }
700
701         return 0;
702 }
703
704 static int
705 mwifiex_cfg80211_deinit_p2p(struct mwifiex_private *priv)
706 {
707         u16 mode = P2P_MODE_DISABLE;
708
709         if (mwifiex_send_cmd(priv, HostCmd_CMD_P2P_MODE_CFG,
710                              HostCmd_ACT_GEN_SET, 0, &mode, true))
711                 return -1;
712
713         return 0;
714 }
715
716 /*
717  * This function initializes the functionalities for P2P client.
718  * The P2P client initialization sequence is:
719  * disable -> device -> client
720  */
721 static int
722 mwifiex_cfg80211_init_p2p_client(struct mwifiex_private *priv)
723 {
724         u16 mode;
725
726         if (mwifiex_cfg80211_deinit_p2p(priv))
727                 return -1;
728
729         mode = P2P_MODE_DEVICE;
730         if (mwifiex_send_cmd(priv, HostCmd_CMD_P2P_MODE_CFG,
731                              HostCmd_ACT_GEN_SET, 0, &mode, true))
732                 return -1;
733
734         mode = P2P_MODE_CLIENT;
735         if (mwifiex_send_cmd(priv, HostCmd_CMD_P2P_MODE_CFG,
736                              HostCmd_ACT_GEN_SET, 0, &mode, true))
737                 return -1;
738
739         return 0;
740 }
741
742 /*
743  * This function initializes the functionalities for P2P GO.
744  * The P2P GO initialization sequence is:
745  * disable -> device -> GO
746  */
747 static int
748 mwifiex_cfg80211_init_p2p_go(struct mwifiex_private *priv)
749 {
750         u16 mode;
751
752         if (mwifiex_cfg80211_deinit_p2p(priv))
753                 return -1;
754
755         mode = P2P_MODE_DEVICE;
756         if (mwifiex_send_cmd(priv, HostCmd_CMD_P2P_MODE_CFG,
757                              HostCmd_ACT_GEN_SET, 0, &mode, true))
758                 return -1;
759
760         mode = P2P_MODE_GO;
761         if (mwifiex_send_cmd(priv, HostCmd_CMD_P2P_MODE_CFG,
762                              HostCmd_ACT_GEN_SET, 0, &mode, true))
763                 return -1;
764
765         return 0;
766 }
767
768 static int mwifiex_deinit_priv_params(struct mwifiex_private *priv)
769 {
770         struct mwifiex_adapter *adapter = priv->adapter;
771         unsigned long flags;
772
773         priv->mgmt_frame_mask = 0;
774         if (mwifiex_send_cmd(priv, HostCmd_CMD_MGMT_FRAME_REG,
775                              HostCmd_ACT_GEN_SET, 0,
776                              &priv->mgmt_frame_mask, false)) {
777                 mwifiex_dbg(adapter, ERROR,
778                             "could not unregister mgmt frame rx\n");
779                 return -1;
780         }
781
782         mwifiex_deauthenticate(priv, NULL);
783
784         spin_lock_irqsave(&adapter->main_proc_lock, flags);
785         adapter->main_locked = true;
786         if (adapter->mwifiex_processing) {
787                 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
788                 flush_workqueue(adapter->workqueue);
789         } else {
790                 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
791         }
792
793         spin_lock_irqsave(&adapter->rx_proc_lock, flags);
794         adapter->rx_locked = true;
795         if (adapter->rx_processing) {
796                 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
797                 flush_workqueue(adapter->rx_workqueue);
798         } else {
799         spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
800         }
801
802         mwifiex_free_priv(priv);
803         priv->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
804         priv->bss_mode = NL80211_IFTYPE_UNSPECIFIED;
805         priv->sec_info.authentication_mode = NL80211_AUTHTYPE_OPEN_SYSTEM;
806
807         return 0;
808 }
809
810 static int
811 mwifiex_init_new_priv_params(struct mwifiex_private *priv,
812                              struct net_device *dev,
813                              enum nl80211_iftype type)
814 {
815         struct mwifiex_adapter *adapter = priv->adapter;
816         unsigned long flags;
817
818         mwifiex_init_priv(priv);
819
820         priv->bss_mode = type;
821         priv->wdev.iftype = type;
822
823         mwifiex_init_priv_params(priv, priv->netdev);
824         priv->bss_started = 0;
825
826         switch (type) {
827         case NL80211_IFTYPE_STATION:
828         case NL80211_IFTYPE_ADHOC:
829                 priv->bss_num = mwifiex_get_unused_bss_num(adapter,
830                          MWIFIEX_BSS_TYPE_STA);
831                 priv->bss_role =  MWIFIEX_BSS_ROLE_STA;
832                 priv->bss_type = MWIFIEX_BSS_TYPE_STA;
833                 break;
834         case NL80211_IFTYPE_P2P_CLIENT:
835                 priv->bss_num = mwifiex_get_unused_bss_num(adapter,
836                          MWIFIEX_BSS_TYPE_P2P);
837                 priv->bss_role =  MWIFIEX_BSS_ROLE_STA;
838                 priv->bss_type = MWIFIEX_BSS_TYPE_P2P;
839                 break;
840         case NL80211_IFTYPE_P2P_GO:
841                 priv->bss_num = mwifiex_get_unused_bss_num(adapter,
842                          MWIFIEX_BSS_TYPE_P2P);
843                 priv->bss_role =  MWIFIEX_BSS_ROLE_UAP;
844                 priv->bss_type = MWIFIEX_BSS_TYPE_P2P;
845                 break;
846         case NL80211_IFTYPE_AP:
847                 priv->bss_num = mwifiex_get_unused_bss_num(adapter,
848                          MWIFIEX_BSS_TYPE_UAP);
849                 priv->bss_type = MWIFIEX_BSS_TYPE_UAP;
850                 priv->bss_role = MWIFIEX_BSS_ROLE_UAP;
851                 break;
852         default:
853                 mwifiex_dbg(adapter, ERROR,
854                             "%s: changing to %d not supported\n",
855                             dev->name, type);
856                 return -EOPNOTSUPP;
857         }
858
859         spin_lock_irqsave(&adapter->main_proc_lock, flags);
860         adapter->main_locked = false;
861         spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
862
863         spin_lock_irqsave(&adapter->rx_proc_lock, flags);
864         adapter->rx_locked = false;
865         spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
866
867         return 0;
868 }
869
870 static int
871 mwifiex_change_vif_to_p2p(struct net_device *dev,
872                           enum nl80211_iftype curr_iftype,
873                           enum nl80211_iftype type, u32 *flags,
874                           struct vif_params *params)
875 {
876         struct mwifiex_private *priv;
877         struct mwifiex_adapter *adapter;
878
879         priv = mwifiex_netdev_get_priv(dev);
880
881         if (!priv)
882                 return -1;
883
884         adapter = priv->adapter;
885
886         if (adapter->curr_iface_comb.p2p_intf ==
887             adapter->iface_limit.p2p_intf) {
888                 mwifiex_dbg(adapter, ERROR,
889                             "cannot create multiple P2P ifaces\n");
890                 return -1;
891         }
892
893         mwifiex_dbg(adapter, INFO,
894                     "%s: changing role to p2p\n", dev->name);
895
896         if (mwifiex_deinit_priv_params(priv))
897                 return -1;
898         if (mwifiex_init_new_priv_params(priv, dev, type))
899                 return -1;
900
901         switch (type) {
902         case NL80211_IFTYPE_P2P_CLIENT:
903                 if (mwifiex_cfg80211_init_p2p_client(priv))
904                         return -EFAULT;
905                 break;
906         case NL80211_IFTYPE_P2P_GO:
907                 if (mwifiex_cfg80211_init_p2p_go(priv))
908                         return -EFAULT;
909                 break;
910         default:
911                 mwifiex_dbg(adapter, ERROR,
912                             "%s: changing to %d not supported\n",
913                             dev->name, type);
914                 return -EOPNOTSUPP;
915         }
916
917         if (mwifiex_send_cmd(priv, HostCmd_CMD_SET_BSS_MODE,
918                              HostCmd_ACT_GEN_SET, 0, NULL, true))
919                 return -1;
920
921         if (mwifiex_sta_init_cmd(priv, false, false))
922                 return -1;
923
924         switch (curr_iftype) {
925         case NL80211_IFTYPE_STATION:
926         case NL80211_IFTYPE_ADHOC:
927                 adapter->curr_iface_comb.sta_intf--;
928                 break;
929         case NL80211_IFTYPE_AP:
930                 adapter->curr_iface_comb.uap_intf--;
931                 break;
932         default:
933                 break;
934         }
935
936         adapter->curr_iface_comb.p2p_intf++;
937         dev->ieee80211_ptr->iftype = type;
938
939         return 0;
940 }
941
942 static int
943 mwifiex_change_vif_to_sta_adhoc(struct net_device *dev,
944                                 enum nl80211_iftype curr_iftype,
945                                 enum nl80211_iftype type, u32 *flags,
946                                 struct vif_params *params)
947 {
948         struct mwifiex_private *priv;
949         struct mwifiex_adapter *adapter;
950
951         priv = mwifiex_netdev_get_priv(dev);
952
953         if (!priv)
954                 return -1;
955
956         adapter = priv->adapter;
957
958         if ((curr_iftype != NL80211_IFTYPE_P2P_CLIENT &&
959              curr_iftype != NL80211_IFTYPE_P2P_GO) &&
960             (adapter->curr_iface_comb.sta_intf ==
961              adapter->iface_limit.sta_intf)) {
962                 mwifiex_dbg(adapter, ERROR,
963                             "cannot create multiple station/adhoc ifaces\n");
964                 return -1;
965         }
966
967         if (type == NL80211_IFTYPE_STATION)
968                 mwifiex_dbg(adapter, INFO,
969                             "%s: changing role to station\n", dev->name);
970         else
971                 mwifiex_dbg(adapter, INFO,
972                             "%s: changing role to adhoc\n", dev->name);
973
974         if (mwifiex_deinit_priv_params(priv))
975                 return -1;
976         if (mwifiex_init_new_priv_params(priv, dev, type))
977                 return -1;
978         if (mwifiex_send_cmd(priv, HostCmd_CMD_SET_BSS_MODE,
979                              HostCmd_ACT_GEN_SET, 0, NULL, true))
980                 return -1;
981         if (mwifiex_sta_init_cmd(priv, false, false))
982                 return -1;
983
984         switch (curr_iftype) {
985         case NL80211_IFTYPE_P2P_CLIENT:
986         case NL80211_IFTYPE_P2P_GO:
987                 adapter->curr_iface_comb.p2p_intf--;
988                 break;
989         case NL80211_IFTYPE_AP:
990                 adapter->curr_iface_comb.uap_intf--;
991                 break;
992         default:
993                 break;
994         }
995
996         adapter->curr_iface_comb.sta_intf++;
997         dev->ieee80211_ptr->iftype = type;
998         return 0;
999 }
1000
1001 static int
1002 mwifiex_change_vif_to_ap(struct net_device *dev,
1003                          enum nl80211_iftype curr_iftype,
1004                          enum nl80211_iftype type, u32 *flags,
1005                          struct vif_params *params)
1006 {
1007         struct mwifiex_private *priv;
1008         struct mwifiex_adapter *adapter;
1009
1010         priv = mwifiex_netdev_get_priv(dev);
1011
1012         if (!priv)
1013                 return -1;
1014
1015         adapter = priv->adapter;
1016
1017         if (adapter->curr_iface_comb.uap_intf ==
1018             adapter->iface_limit.uap_intf) {
1019                 mwifiex_dbg(adapter, ERROR,
1020                             "cannot create multiple AP ifaces\n");
1021                 return -1;
1022         }
1023
1024         mwifiex_dbg(adapter, INFO,
1025                     "%s: changing role to AP\n", dev->name);
1026
1027         if (mwifiex_deinit_priv_params(priv))
1028                 return -1;
1029         if (mwifiex_init_new_priv_params(priv, dev, type))
1030                 return -1;
1031         if (mwifiex_send_cmd(priv, HostCmd_CMD_SET_BSS_MODE,
1032                              HostCmd_ACT_GEN_SET, 0, NULL, true))
1033                 return -1;
1034         if (mwifiex_sta_init_cmd(priv, false, false))
1035                 return -1;
1036
1037         switch (curr_iftype) {
1038         case NL80211_IFTYPE_P2P_CLIENT:
1039         case NL80211_IFTYPE_P2P_GO:
1040                 adapter->curr_iface_comb.p2p_intf--;
1041                 break;
1042         case NL80211_IFTYPE_STATION:
1043         case NL80211_IFTYPE_ADHOC:
1044                 adapter->curr_iface_comb.sta_intf--;
1045                 break;
1046         default:
1047                 break;
1048         }
1049
1050         adapter->curr_iface_comb.uap_intf++;
1051         dev->ieee80211_ptr->iftype = type;
1052         return 0;
1053 }
1054 /*
1055  * CFG802.11 operation handler to change interface type.
1056  */
1057 static int
1058 mwifiex_cfg80211_change_virtual_intf(struct wiphy *wiphy,
1059                                      struct net_device *dev,
1060                                      enum nl80211_iftype type, u32 *flags,
1061                                      struct vif_params *params)
1062 {
1063         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1064         enum nl80211_iftype curr_iftype = dev->ieee80211_ptr->iftype;
1065
1066         switch (curr_iftype) {
1067         case NL80211_IFTYPE_ADHOC:
1068                 switch (type) {
1069                 case NL80211_IFTYPE_STATION:
1070                         priv->bss_mode = type;
1071                         priv->sec_info.authentication_mode =
1072                                                    NL80211_AUTHTYPE_OPEN_SYSTEM;
1073                         dev->ieee80211_ptr->iftype = type;
1074                         mwifiex_deauthenticate(priv, NULL);
1075                         return mwifiex_send_cmd(priv, HostCmd_CMD_SET_BSS_MODE,
1076                                                 HostCmd_ACT_GEN_SET, 0, NULL,
1077                                                 true);
1078                 case NL80211_IFTYPE_P2P_CLIENT:
1079                 case NL80211_IFTYPE_P2P_GO:
1080                         return mwifiex_change_vif_to_p2p(dev, curr_iftype,
1081                                                          type, flags, params);
1082                 case NL80211_IFTYPE_AP:
1083                         return mwifiex_change_vif_to_ap(dev, curr_iftype, type,
1084                                                         flags, params);
1085                 case NL80211_IFTYPE_UNSPECIFIED:
1086                         mwifiex_dbg(priv->adapter, INFO,
1087                                     "%s: kept type as IBSS\n", dev->name);
1088                 case NL80211_IFTYPE_ADHOC:      /* This shouldn't happen */
1089                         return 0;
1090                 default:
1091                         mwifiex_dbg(priv->adapter, ERROR,
1092                                     "%s: changing to %d not supported\n",
1093                                     dev->name, type);
1094                         return -EOPNOTSUPP;
1095                 }
1096                 break;
1097         case NL80211_IFTYPE_STATION:
1098                 switch (type) {
1099                 case NL80211_IFTYPE_ADHOC:
1100                         priv->bss_mode = type;
1101                         priv->sec_info.authentication_mode =
1102                                                    NL80211_AUTHTYPE_OPEN_SYSTEM;
1103                         dev->ieee80211_ptr->iftype = type;
1104                         mwifiex_deauthenticate(priv, NULL);
1105                         return mwifiex_send_cmd(priv, HostCmd_CMD_SET_BSS_MODE,
1106                                                 HostCmd_ACT_GEN_SET, 0, NULL,
1107                                                 true);
1108                 case NL80211_IFTYPE_P2P_CLIENT:
1109                 case NL80211_IFTYPE_P2P_GO:
1110                         return mwifiex_change_vif_to_p2p(dev, curr_iftype,
1111                                                          type, flags, params);
1112                 case NL80211_IFTYPE_AP:
1113                         return mwifiex_change_vif_to_ap(dev, curr_iftype, type,
1114                                                         flags, params);
1115                 case NL80211_IFTYPE_UNSPECIFIED:
1116                         mwifiex_dbg(priv->adapter, INFO,
1117                                     "%s: kept type as STA\n", dev->name);
1118                 case NL80211_IFTYPE_STATION:    /* This shouldn't happen */
1119                         return 0;
1120                 default:
1121                         mwifiex_dbg(priv->adapter, ERROR,
1122                                     "%s: changing to %d not supported\n",
1123                                     dev->name, type);
1124                         return -EOPNOTSUPP;
1125                 }
1126                 break;
1127         case NL80211_IFTYPE_AP:
1128                 switch (type) {
1129                 case NL80211_IFTYPE_ADHOC:
1130                 case NL80211_IFTYPE_STATION:
1131                         return mwifiex_change_vif_to_sta_adhoc(dev, curr_iftype,
1132                                                                type, flags,
1133                                                                params);
1134                         break;
1135                 case NL80211_IFTYPE_P2P_CLIENT:
1136                 case NL80211_IFTYPE_P2P_GO:
1137                         return mwifiex_change_vif_to_p2p(dev, curr_iftype,
1138                                                          type, flags, params);
1139                 case NL80211_IFTYPE_UNSPECIFIED:
1140                         mwifiex_dbg(priv->adapter, INFO,
1141                                     "%s: kept type as AP\n", dev->name);
1142                 case NL80211_IFTYPE_AP:         /* This shouldn't happen */
1143                         return 0;
1144                 default:
1145                         mwifiex_dbg(priv->adapter, ERROR,
1146                                     "%s: changing to %d not supported\n",
1147                                     dev->name, type);
1148                         return -EOPNOTSUPP;
1149                 }
1150                 break;
1151         case NL80211_IFTYPE_P2P_CLIENT:
1152         case NL80211_IFTYPE_P2P_GO:
1153                 switch (type) {
1154                 case NL80211_IFTYPE_STATION:
1155                         if (mwifiex_cfg80211_deinit_p2p(priv))
1156                                 return -EFAULT;
1157                         priv->adapter->curr_iface_comb.p2p_intf--;
1158                         priv->adapter->curr_iface_comb.sta_intf++;
1159                         dev->ieee80211_ptr->iftype = type;
1160                         break;
1161                 case NL80211_IFTYPE_ADHOC:
1162                         if (mwifiex_cfg80211_deinit_p2p(priv))
1163                                 return -EFAULT;
1164                         return mwifiex_change_vif_to_sta_adhoc(dev, curr_iftype,
1165                                                                type, flags,
1166                                                                params);
1167                         break;
1168                 case NL80211_IFTYPE_AP:
1169                         if (mwifiex_cfg80211_deinit_p2p(priv))
1170                                 return -EFAULT;
1171                         return mwifiex_change_vif_to_ap(dev, curr_iftype, type,
1172                                                         flags, params);
1173                 case NL80211_IFTYPE_UNSPECIFIED:
1174                         mwifiex_dbg(priv->adapter, INFO,
1175                                     "%s: kept type as P2P\n", dev->name);
1176                 case NL80211_IFTYPE_P2P_CLIENT:
1177                 case NL80211_IFTYPE_P2P_GO:
1178                         return 0;
1179                 default:
1180                         mwifiex_dbg(priv->adapter, ERROR,
1181                                     "%s: changing to %d not supported\n",
1182                                     dev->name, type);
1183                         return -EOPNOTSUPP;
1184                 }
1185                 break;
1186         default:
1187                 mwifiex_dbg(priv->adapter, ERROR,
1188                             "%s: unknown iftype: %d\n",
1189                             dev->name, dev->ieee80211_ptr->iftype);
1190                 return -EOPNOTSUPP;
1191         }
1192
1193
1194         return 0;
1195 }
1196
1197 static void
1198 mwifiex_parse_htinfo(struct mwifiex_private *priv, u8 tx_htinfo,
1199                      struct rate_info *rate)
1200 {
1201         struct mwifiex_adapter *adapter = priv->adapter;
1202
1203         if (adapter->is_hw_11ac_capable) {
1204                 /* bit[1-0]: 00=LG 01=HT 10=VHT */
1205                 if (tx_htinfo & BIT(0)) {
1206                         /* HT */
1207                         rate->mcs = priv->tx_rate;
1208                         rate->flags |= RATE_INFO_FLAGS_MCS;
1209                 }
1210                 if (tx_htinfo & BIT(1)) {
1211                         /* VHT */
1212                         rate->mcs = priv->tx_rate & 0x0F;
1213                         rate->flags |= RATE_INFO_FLAGS_VHT_MCS;
1214                 }
1215
1216                 if (tx_htinfo & (BIT(1) | BIT(0))) {
1217                         /* HT or VHT */
1218                         switch (tx_htinfo & (BIT(3) | BIT(2))) {
1219                         case 0:
1220                                 rate->bw = RATE_INFO_BW_20;
1221                                 break;
1222                         case (BIT(2)):
1223                                 rate->bw = RATE_INFO_BW_40;
1224                                 break;
1225                         case (BIT(3)):
1226                                 rate->bw = RATE_INFO_BW_80;
1227                                 break;
1228                         case (BIT(3) | BIT(2)):
1229                                 rate->bw = RATE_INFO_BW_160;
1230                                 break;
1231                         }
1232
1233                         if (tx_htinfo & BIT(4))
1234                                 rate->flags |= RATE_INFO_FLAGS_SHORT_GI;
1235
1236                         if ((priv->tx_rate >> 4) == 1)
1237                                 rate->nss = 2;
1238                         else
1239                                 rate->nss = 1;
1240                 }
1241         } else {
1242                 /*
1243                  * Bit 0 in tx_htinfo indicates that current Tx rate
1244                  * is 11n rate. Valid MCS index values for us are 0 to 15.
1245                  */
1246                 if ((tx_htinfo & BIT(0)) && (priv->tx_rate < 16)) {
1247                         rate->mcs = priv->tx_rate;
1248                         rate->flags |= RATE_INFO_FLAGS_MCS;
1249                         rate->bw = RATE_INFO_BW_20;
1250                         if (tx_htinfo & BIT(1))
1251                                 rate->bw = RATE_INFO_BW_40;
1252                         if (tx_htinfo & BIT(2))
1253                                 rate->flags |= RATE_INFO_FLAGS_SHORT_GI;
1254                 }
1255         }
1256 }
1257
1258 /*
1259  * This function dumps the station information on a buffer.
1260  *
1261  * The following information are shown -
1262  *      - Total bytes transmitted
1263  *      - Total bytes received
1264  *      - Total packets transmitted
1265  *      - Total packets received
1266  *      - Signal quality level
1267  *      - Transmission rate
1268  */
1269 static int
1270 mwifiex_dump_station_info(struct mwifiex_private *priv,
1271                           struct mwifiex_sta_node *node,
1272                           struct station_info *sinfo)
1273 {
1274         u32 rate;
1275
1276         sinfo->filled = BIT(NL80211_STA_INFO_RX_BYTES) | BIT(NL80211_STA_INFO_TX_BYTES) |
1277                         BIT(NL80211_STA_INFO_RX_PACKETS) | BIT(NL80211_STA_INFO_TX_PACKETS) |
1278                         BIT(NL80211_STA_INFO_TX_BITRATE) |
1279                         BIT(NL80211_STA_INFO_SIGNAL) | BIT(NL80211_STA_INFO_SIGNAL_AVG);
1280
1281         if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP) {
1282                 if (!node)
1283                         return -ENOENT;
1284
1285                 sinfo->filled |= BIT(NL80211_STA_INFO_INACTIVE_TIME) |
1286                                 BIT(NL80211_STA_INFO_TX_FAILED);
1287                 sinfo->inactive_time =
1288                         jiffies_to_msecs(jiffies - node->stats.last_rx);
1289
1290                 sinfo->signal = node->stats.rssi;
1291                 sinfo->signal_avg = node->stats.rssi;
1292                 sinfo->rx_bytes = node->stats.rx_bytes;
1293                 sinfo->tx_bytes = node->stats.tx_bytes;
1294                 sinfo->rx_packets = node->stats.rx_packets;
1295                 sinfo->tx_packets = node->stats.tx_packets;
1296                 sinfo->tx_failed = node->stats.tx_failed;
1297
1298                 mwifiex_parse_htinfo(priv, node->stats.last_tx_htinfo,
1299                                      &sinfo->txrate);
1300                 sinfo->txrate.legacy = node->stats.last_tx_rate * 5;
1301
1302                 return 0;
1303         }
1304
1305         /* Get signal information from the firmware */
1306         if (mwifiex_send_cmd(priv, HostCmd_CMD_RSSI_INFO,
1307                              HostCmd_ACT_GEN_GET, 0, NULL, true)) {
1308                 mwifiex_dbg(priv->adapter, ERROR,
1309                             "failed to get signal information\n");
1310                 return -EFAULT;
1311         }
1312
1313         if (mwifiex_drv_get_data_rate(priv, &rate)) {
1314                 mwifiex_dbg(priv->adapter, ERROR,
1315                             "getting data rate error\n");
1316                 return -EFAULT;
1317         }
1318
1319         /* Get DTIM period information from firmware */
1320         mwifiex_send_cmd(priv, HostCmd_CMD_802_11_SNMP_MIB,
1321                          HostCmd_ACT_GEN_GET, DTIM_PERIOD_I,
1322                          &priv->dtim_period, true);
1323
1324         mwifiex_parse_htinfo(priv, priv->tx_htinfo, &sinfo->txrate);
1325
1326         sinfo->signal_avg = priv->bcn_rssi_avg;
1327         sinfo->rx_bytes = priv->stats.rx_bytes;
1328         sinfo->tx_bytes = priv->stats.tx_bytes;
1329         sinfo->rx_packets = priv->stats.rx_packets;
1330         sinfo->tx_packets = priv->stats.tx_packets;
1331         sinfo->signal = priv->bcn_rssi_avg;
1332         /* bit rate is in 500 kb/s units. Convert it to 100kb/s units */
1333         sinfo->txrate.legacy = rate * 5;
1334
1335         if (priv->bss_mode == NL80211_IFTYPE_STATION) {
1336                 sinfo->filled |= BIT(NL80211_STA_INFO_BSS_PARAM);
1337                 sinfo->bss_param.flags = 0;
1338                 if (priv->curr_bss_params.bss_descriptor.cap_info_bitmap &
1339                                                 WLAN_CAPABILITY_SHORT_PREAMBLE)
1340                         sinfo->bss_param.flags |=
1341                                         BSS_PARAM_FLAGS_SHORT_PREAMBLE;
1342                 if (priv->curr_bss_params.bss_descriptor.cap_info_bitmap &
1343                                                 WLAN_CAPABILITY_SHORT_SLOT_TIME)
1344                         sinfo->bss_param.flags |=
1345                                         BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
1346                 sinfo->bss_param.dtim_period = priv->dtim_period;
1347                 sinfo->bss_param.beacon_interval =
1348                         priv->curr_bss_params.bss_descriptor.beacon_period;
1349         }
1350
1351         return 0;
1352 }
1353
1354 /*
1355  * CFG802.11 operation handler to get station information.
1356  *
1357  * This function only works in connected mode, and dumps the
1358  * requested station information, if available.
1359  */
1360 static int
1361 mwifiex_cfg80211_get_station(struct wiphy *wiphy, struct net_device *dev,
1362                              const u8 *mac, struct station_info *sinfo)
1363 {
1364         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1365
1366         if (!priv->media_connected)
1367                 return -ENOENT;
1368         if (memcmp(mac, priv->cfg_bssid, ETH_ALEN))
1369                 return -ENOENT;
1370
1371         return mwifiex_dump_station_info(priv, NULL, sinfo);
1372 }
1373
1374 /*
1375  * CFG802.11 operation handler to dump station information.
1376  */
1377 static int
1378 mwifiex_cfg80211_dump_station(struct wiphy *wiphy, struct net_device *dev,
1379                               int idx, u8 *mac, struct station_info *sinfo)
1380 {
1381         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1382         static struct mwifiex_sta_node *node;
1383
1384         if ((GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA) &&
1385             priv->media_connected && idx == 0) {
1386                 ether_addr_copy(mac, priv->cfg_bssid);
1387                 return mwifiex_dump_station_info(priv, NULL, sinfo);
1388         } else if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP) {
1389                 mwifiex_send_cmd(priv, HOST_CMD_APCMD_STA_LIST,
1390                                  HostCmd_ACT_GEN_GET, 0, NULL, true);
1391
1392                 if (node && (&node->list == &priv->sta_list)) {
1393                         node = NULL;
1394                         return -ENOENT;
1395                 }
1396
1397                 node = list_prepare_entry(node, &priv->sta_list, list);
1398                 list_for_each_entry_continue(node, &priv->sta_list, list) {
1399                         ether_addr_copy(mac, node->mac_addr);
1400                         return mwifiex_dump_station_info(priv, node, sinfo);
1401                 }
1402         }
1403
1404         return -ENOENT;
1405 }
1406
1407 static int
1408 mwifiex_cfg80211_dump_survey(struct wiphy *wiphy, struct net_device *dev,
1409                              int idx, struct survey_info *survey)
1410 {
1411         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1412         struct mwifiex_chan_stats *pchan_stats = priv->adapter->chan_stats;
1413         enum nl80211_band band;
1414
1415         mwifiex_dbg(priv->adapter, DUMP, "dump_survey idx=%d\n", idx);
1416
1417         memset(survey, 0, sizeof(struct survey_info));
1418
1419         if ((GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA) &&
1420             priv->media_connected && idx == 0) {
1421                         u8 curr_bss_band = priv->curr_bss_params.band;
1422                         u32 chan = priv->curr_bss_params.bss_descriptor.channel;
1423
1424                         band = mwifiex_band_to_radio_type(curr_bss_band);
1425                         survey->channel = ieee80211_get_channel(wiphy,
1426                                 ieee80211_channel_to_frequency(chan, band));
1427
1428                         if (priv->bcn_nf_last) {
1429                                 survey->filled = SURVEY_INFO_NOISE_DBM;
1430                                 survey->noise = priv->bcn_nf_last;
1431                         }
1432                         return 0;
1433         }
1434
1435         if (idx >= priv->adapter->num_in_chan_stats)
1436                 return -ENOENT;
1437
1438         if (!pchan_stats[idx].cca_scan_dur)
1439                 return 0;
1440
1441         band = pchan_stats[idx].bandcfg;
1442         survey->channel = ieee80211_get_channel(wiphy,
1443             ieee80211_channel_to_frequency(pchan_stats[idx].chan_num, band));
1444         survey->filled = SURVEY_INFO_NOISE_DBM |
1445                          SURVEY_INFO_TIME |
1446                          SURVEY_INFO_TIME_BUSY;
1447         survey->noise = pchan_stats[idx].noise;
1448         survey->time = pchan_stats[idx].cca_scan_dur;
1449         survey->time_busy = pchan_stats[idx].cca_busy_dur;
1450
1451         return 0;
1452 }
1453
1454 /* Supported rates to be advertised to the cfg80211 */
1455 static struct ieee80211_rate mwifiex_rates[] = {
1456         {.bitrate = 10, .hw_value = 2, },
1457         {.bitrate = 20, .hw_value = 4, },
1458         {.bitrate = 55, .hw_value = 11, },
1459         {.bitrate = 110, .hw_value = 22, },
1460         {.bitrate = 60, .hw_value = 12, },
1461         {.bitrate = 90, .hw_value = 18, },
1462         {.bitrate = 120, .hw_value = 24, },
1463         {.bitrate = 180, .hw_value = 36, },
1464         {.bitrate = 240, .hw_value = 48, },
1465         {.bitrate = 360, .hw_value = 72, },
1466         {.bitrate = 480, .hw_value = 96, },
1467         {.bitrate = 540, .hw_value = 108, },
1468 };
1469
1470 /* Channel definitions to be advertised to cfg80211 */
1471 static struct ieee80211_channel mwifiex_channels_2ghz[] = {
1472         {.center_freq = 2412, .hw_value = 1, },
1473         {.center_freq = 2417, .hw_value = 2, },
1474         {.center_freq = 2422, .hw_value = 3, },
1475         {.center_freq = 2427, .hw_value = 4, },
1476         {.center_freq = 2432, .hw_value = 5, },
1477         {.center_freq = 2437, .hw_value = 6, },
1478         {.center_freq = 2442, .hw_value = 7, },
1479         {.center_freq = 2447, .hw_value = 8, },
1480         {.center_freq = 2452, .hw_value = 9, },
1481         {.center_freq = 2457, .hw_value = 10, },
1482         {.center_freq = 2462, .hw_value = 11, },
1483         {.center_freq = 2467, .hw_value = 12, },
1484         {.center_freq = 2472, .hw_value = 13, },
1485         {.center_freq = 2484, .hw_value = 14, },
1486 };
1487
1488 static struct ieee80211_supported_band mwifiex_band_2ghz = {
1489         .channels = mwifiex_channels_2ghz,
1490         .n_channels = ARRAY_SIZE(mwifiex_channels_2ghz),
1491         .bitrates = mwifiex_rates,
1492         .n_bitrates = ARRAY_SIZE(mwifiex_rates),
1493 };
1494
1495 static struct ieee80211_channel mwifiex_channels_5ghz[] = {
1496         {.center_freq = 5040, .hw_value = 8, },
1497         {.center_freq = 5060, .hw_value = 12, },
1498         {.center_freq = 5080, .hw_value = 16, },
1499         {.center_freq = 5170, .hw_value = 34, },
1500         {.center_freq = 5190, .hw_value = 38, },
1501         {.center_freq = 5210, .hw_value = 42, },
1502         {.center_freq = 5230, .hw_value = 46, },
1503         {.center_freq = 5180, .hw_value = 36, },
1504         {.center_freq = 5200, .hw_value = 40, },
1505         {.center_freq = 5220, .hw_value = 44, },
1506         {.center_freq = 5240, .hw_value = 48, },
1507         {.center_freq = 5260, .hw_value = 52, },
1508         {.center_freq = 5280, .hw_value = 56, },
1509         {.center_freq = 5300, .hw_value = 60, },
1510         {.center_freq = 5320, .hw_value = 64, },
1511         {.center_freq = 5500, .hw_value = 100, },
1512         {.center_freq = 5520, .hw_value = 104, },
1513         {.center_freq = 5540, .hw_value = 108, },
1514         {.center_freq = 5560, .hw_value = 112, },
1515         {.center_freq = 5580, .hw_value = 116, },
1516         {.center_freq = 5600, .hw_value = 120, },
1517         {.center_freq = 5620, .hw_value = 124, },
1518         {.center_freq = 5640, .hw_value = 128, },
1519         {.center_freq = 5660, .hw_value = 132, },
1520         {.center_freq = 5680, .hw_value = 136, },
1521         {.center_freq = 5700, .hw_value = 140, },
1522         {.center_freq = 5745, .hw_value = 149, },
1523         {.center_freq = 5765, .hw_value = 153, },
1524         {.center_freq = 5785, .hw_value = 157, },
1525         {.center_freq = 5805, .hw_value = 161, },
1526         {.center_freq = 5825, .hw_value = 165, },
1527 };
1528
1529 static struct ieee80211_supported_band mwifiex_band_5ghz = {
1530         .channels = mwifiex_channels_5ghz,
1531         .n_channels = ARRAY_SIZE(mwifiex_channels_5ghz),
1532         .bitrates = mwifiex_rates + 4,
1533         .n_bitrates = ARRAY_SIZE(mwifiex_rates) - 4,
1534 };
1535
1536
1537 /* Supported crypto cipher suits to be advertised to cfg80211 */
1538 static const u32 mwifiex_cipher_suites[] = {
1539         WLAN_CIPHER_SUITE_WEP40,
1540         WLAN_CIPHER_SUITE_WEP104,
1541         WLAN_CIPHER_SUITE_TKIP,
1542         WLAN_CIPHER_SUITE_CCMP,
1543         WLAN_CIPHER_SUITE_SMS4,
1544         WLAN_CIPHER_SUITE_AES_CMAC,
1545 };
1546
1547 /* Supported mgmt frame types to be advertised to cfg80211 */
1548 static const struct ieee80211_txrx_stypes
1549 mwifiex_mgmt_stypes[NUM_NL80211_IFTYPES] = {
1550         [NL80211_IFTYPE_STATION] = {
1551                 .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1552                       BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
1553                 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1554                       BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
1555         },
1556         [NL80211_IFTYPE_AP] = {
1557                 .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1558                       BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
1559                 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1560                       BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
1561         },
1562         [NL80211_IFTYPE_P2P_CLIENT] = {
1563                 .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1564                       BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
1565                 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1566                       BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
1567         },
1568         [NL80211_IFTYPE_P2P_GO] = {
1569                 .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1570                       BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
1571                 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1572                       BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
1573         },
1574 };
1575
1576 /*
1577  * CFG802.11 operation handler for setting bit rates.
1578  *
1579  * Function configures data rates to firmware using bitrate mask
1580  * provided by cfg80211.
1581  */
1582 static int mwifiex_cfg80211_set_bitrate_mask(struct wiphy *wiphy,
1583                                 struct net_device *dev,
1584                                 const u8 *peer,
1585                                 const struct cfg80211_bitrate_mask *mask)
1586 {
1587         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1588         u16 bitmap_rates[MAX_BITMAP_RATES_SIZE];
1589         enum nl80211_band band;
1590         struct mwifiex_adapter *adapter = priv->adapter;
1591
1592         if (!priv->media_connected) {
1593                 mwifiex_dbg(adapter, ERROR,
1594                             "Can not set Tx data rate in disconnected state\n");
1595                 return -EINVAL;
1596         }
1597
1598         band = mwifiex_band_to_radio_type(priv->curr_bss_params.band);
1599
1600         memset(bitmap_rates, 0, sizeof(bitmap_rates));
1601
1602         /* Fill HR/DSSS rates. */
1603         if (band == NL80211_BAND_2GHZ)
1604                 bitmap_rates[0] = mask->control[band].legacy & 0x000f;
1605
1606         /* Fill OFDM rates */
1607         if (band == NL80211_BAND_2GHZ)
1608                 bitmap_rates[1] = (mask->control[band].legacy & 0x0ff0) >> 4;
1609         else
1610                 bitmap_rates[1] = mask->control[band].legacy;
1611
1612         /* Fill HT MCS rates */
1613         bitmap_rates[2] = mask->control[band].ht_mcs[0];
1614         if (adapter->hw_dev_mcs_support == HT_STREAM_2X2)
1615                 bitmap_rates[2] |= mask->control[band].ht_mcs[1] << 8;
1616
1617        /* Fill VHT MCS rates */
1618         if (adapter->fw_api_ver == MWIFIEX_FW_V15) {
1619                 bitmap_rates[10] = mask->control[band].vht_mcs[0];
1620                 if (adapter->hw_dev_mcs_support == HT_STREAM_2X2)
1621                         bitmap_rates[11] = mask->control[band].vht_mcs[1];
1622         }
1623
1624         return mwifiex_send_cmd(priv, HostCmd_CMD_TX_RATE_CFG,
1625                                 HostCmd_ACT_GEN_SET, 0, bitmap_rates, true);
1626 }
1627
1628 /*
1629  * CFG802.11 operation handler for connection quality monitoring.
1630  *
1631  * This function subscribes/unsubscribes HIGH_RSSI and LOW_RSSI
1632  * events to FW.
1633  */
1634 static int mwifiex_cfg80211_set_cqm_rssi_config(struct wiphy *wiphy,
1635                                                 struct net_device *dev,
1636                                                 s32 rssi_thold, u32 rssi_hyst)
1637 {
1638         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1639         struct mwifiex_ds_misc_subsc_evt subsc_evt;
1640
1641         priv->cqm_rssi_thold = rssi_thold;
1642         priv->cqm_rssi_hyst = rssi_hyst;
1643
1644         memset(&subsc_evt, 0x00, sizeof(struct mwifiex_ds_misc_subsc_evt));
1645         subsc_evt.events = BITMASK_BCN_RSSI_LOW | BITMASK_BCN_RSSI_HIGH;
1646
1647         /* Subscribe/unsubscribe low and high rssi events */
1648         if (rssi_thold && rssi_hyst) {
1649                 subsc_evt.action = HostCmd_ACT_BITWISE_SET;
1650                 subsc_evt.bcn_l_rssi_cfg.abs_value = abs(rssi_thold);
1651                 subsc_evt.bcn_h_rssi_cfg.abs_value = abs(rssi_thold);
1652                 subsc_evt.bcn_l_rssi_cfg.evt_freq = 1;
1653                 subsc_evt.bcn_h_rssi_cfg.evt_freq = 1;
1654                 return mwifiex_send_cmd(priv,
1655                                         HostCmd_CMD_802_11_SUBSCRIBE_EVENT,
1656                                         0, 0, &subsc_evt, true);
1657         } else {
1658                 subsc_evt.action = HostCmd_ACT_BITWISE_CLR;
1659                 return mwifiex_send_cmd(priv,
1660                                         HostCmd_CMD_802_11_SUBSCRIBE_EVENT,
1661                                         0, 0, &subsc_evt, true);
1662         }
1663
1664         return 0;
1665 }
1666
1667 /* cfg80211 operation handler for change_beacon.
1668  * Function retrieves and sets modified management IEs to FW.
1669  */
1670 static int mwifiex_cfg80211_change_beacon(struct wiphy *wiphy,
1671                                           struct net_device *dev,
1672                                           struct cfg80211_beacon_data *data)
1673 {
1674         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1675
1676         if (GET_BSS_ROLE(priv) != MWIFIEX_BSS_ROLE_UAP) {
1677                 mwifiex_dbg(priv->adapter, ERROR,
1678                             "%s: bss_type mismatched\n", __func__);
1679                 return -EINVAL;
1680         }
1681
1682         if (!priv->bss_started) {
1683                 mwifiex_dbg(priv->adapter, ERROR,
1684                             "%s: bss not started\n", __func__);
1685                 return -EINVAL;
1686         }
1687
1688         if (mwifiex_set_mgmt_ies(priv, data)) {
1689                 mwifiex_dbg(priv->adapter, ERROR,
1690                             "%s: setting mgmt ies failed\n", __func__);
1691                 return -EFAULT;
1692         }
1693
1694         return 0;
1695 }
1696
1697 /* cfg80211 operation handler for del_station.
1698  * Function deauthenticates station which value is provided in mac parameter.
1699  * If mac is NULL/broadcast, all stations in associated station list are
1700  * deauthenticated. If bss is not started or there are no stations in
1701  * associated stations list, no action is taken.
1702  */
1703 static int
1704 mwifiex_cfg80211_del_station(struct wiphy *wiphy, struct net_device *dev,
1705                              struct station_del_parameters *params)
1706 {
1707         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1708         struct mwifiex_sta_node *sta_node;
1709         u8 deauth_mac[ETH_ALEN];
1710         unsigned long flags;
1711
1712         if (!priv->bss_started && priv->wdev.cac_started) {
1713                 mwifiex_dbg(priv->adapter, INFO, "%s: abort CAC!\n", __func__);
1714                 mwifiex_abort_cac(priv);
1715         }
1716
1717         if (list_empty(&priv->sta_list) || !priv->bss_started)
1718                 return 0;
1719
1720         if (!params->mac || is_broadcast_ether_addr(params->mac))
1721                 return 0;
1722
1723         mwifiex_dbg(priv->adapter, INFO, "%s: mac address %pM\n",
1724                     __func__, params->mac);
1725
1726         eth_zero_addr(deauth_mac);
1727
1728         spin_lock_irqsave(&priv->sta_list_spinlock, flags);
1729         sta_node = mwifiex_get_sta_entry(priv, params->mac);
1730         if (sta_node)
1731                 ether_addr_copy(deauth_mac, params->mac);
1732         spin_unlock_irqrestore(&priv->sta_list_spinlock, flags);
1733
1734         if (is_valid_ether_addr(deauth_mac)) {
1735                 if (mwifiex_send_cmd(priv, HostCmd_CMD_UAP_STA_DEAUTH,
1736                                      HostCmd_ACT_GEN_SET, 0,
1737                                      deauth_mac, true))
1738                         return -1;
1739         }
1740
1741         return 0;
1742 }
1743
1744 static int
1745 mwifiex_cfg80211_set_antenna(struct wiphy *wiphy, u32 tx_ant, u32 rx_ant)
1746 {
1747         struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
1748         struct mwifiex_private *priv = mwifiex_get_priv(adapter,
1749                                                         MWIFIEX_BSS_ROLE_ANY);
1750         struct mwifiex_ds_ant_cfg ant_cfg;
1751
1752         if (!tx_ant || !rx_ant)
1753                 return -EOPNOTSUPP;
1754
1755         if (adapter->hw_dev_mcs_support != HT_STREAM_2X2) {
1756                 /* Not a MIMO chip. User should provide specific antenna number
1757                  * for Tx/Rx path or enable all antennas for diversity
1758                  */
1759                 if (tx_ant != rx_ant)
1760                         return -EOPNOTSUPP;
1761
1762                 if ((tx_ant & (tx_ant - 1)) &&
1763                     (tx_ant != BIT(adapter->number_of_antenna) - 1))
1764                         return -EOPNOTSUPP;
1765
1766                 if ((tx_ant == BIT(adapter->number_of_antenna) - 1) &&
1767                     (priv->adapter->number_of_antenna > 1)) {
1768                         tx_ant = RF_ANTENNA_AUTO;
1769                         rx_ant = RF_ANTENNA_AUTO;
1770                 }
1771         } else {
1772                 struct ieee80211_sta_ht_cap *ht_info;
1773                 int rx_mcs_supp;
1774                 enum nl80211_band band;
1775
1776                 if ((tx_ant == 0x1 && rx_ant == 0x1)) {
1777                         adapter->user_dev_mcs_support = HT_STREAM_1X1;
1778                         if (adapter->is_hw_11ac_capable)
1779                                 adapter->usr_dot_11ac_mcs_support =
1780                                                 MWIFIEX_11AC_MCS_MAP_1X1;
1781                 } else {
1782                         adapter->user_dev_mcs_support = HT_STREAM_2X2;
1783                         if (adapter->is_hw_11ac_capable)
1784                                 adapter->usr_dot_11ac_mcs_support =
1785                                                 MWIFIEX_11AC_MCS_MAP_2X2;
1786                 }
1787
1788                 for (band = 0; band < NUM_NL80211_BANDS; band++) {
1789                         if (!adapter->wiphy->bands[band])
1790                                 continue;
1791
1792                         ht_info = &adapter->wiphy->bands[band]->ht_cap;
1793                         rx_mcs_supp =
1794                                 GET_RXMCSSUPP(adapter->user_dev_mcs_support);
1795                         memset(&ht_info->mcs, 0, adapter->number_of_antenna);
1796                         memset(&ht_info->mcs, 0xff, rx_mcs_supp);
1797                 }
1798         }
1799
1800         ant_cfg.tx_ant = tx_ant;
1801         ant_cfg.rx_ant = rx_ant;
1802
1803         return mwifiex_send_cmd(priv, HostCmd_CMD_RF_ANTENNA,
1804                                 HostCmd_ACT_GEN_SET, 0, &ant_cfg, true);
1805 }
1806
1807 /* cfg80211 operation handler for stop ap.
1808  * Function stops BSS running at uAP interface.
1809  */
1810 static int mwifiex_cfg80211_stop_ap(struct wiphy *wiphy, struct net_device *dev)
1811 {
1812         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1813
1814         mwifiex_abort_cac(priv);
1815
1816         if (mwifiex_del_mgmt_ies(priv))
1817                 mwifiex_dbg(priv->adapter, ERROR,
1818                             "Failed to delete mgmt IEs!\n");
1819
1820         priv->ap_11n_enabled = 0;
1821         memset(&priv->bss_cfg, 0, sizeof(priv->bss_cfg));
1822
1823         if (mwifiex_send_cmd(priv, HostCmd_CMD_UAP_BSS_STOP,
1824                              HostCmd_ACT_GEN_SET, 0, NULL, true)) {
1825                 mwifiex_dbg(priv->adapter, ERROR,
1826                             "Failed to stop the BSS\n");
1827                 return -1;
1828         }
1829
1830         if (mwifiex_send_cmd(priv, HOST_CMD_APCMD_SYS_RESET,
1831                              HostCmd_ACT_GEN_SET, 0, NULL, true)) {
1832                 mwifiex_dbg(priv->adapter, ERROR,
1833                             "Failed to reset BSS\n");
1834                 return -1;
1835         }
1836
1837         if (netif_carrier_ok(priv->netdev))
1838                 netif_carrier_off(priv->netdev);
1839         mwifiex_stop_net_dev_queue(priv->netdev, priv->adapter);
1840
1841         return 0;
1842 }
1843
1844 /* cfg80211 operation handler for start_ap.
1845  * Function sets beacon period, DTIM period, SSID and security into
1846  * AP config structure.
1847  * AP is configured with these settings and BSS is started.
1848  */
1849 static int mwifiex_cfg80211_start_ap(struct wiphy *wiphy,
1850                                      struct net_device *dev,
1851                                      struct cfg80211_ap_settings *params)
1852 {
1853         struct mwifiex_uap_bss_param *bss_cfg;
1854         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1855
1856         if (GET_BSS_ROLE(priv) != MWIFIEX_BSS_ROLE_UAP)
1857                 return -1;
1858
1859         bss_cfg = kzalloc(sizeof(struct mwifiex_uap_bss_param), GFP_KERNEL);
1860         if (!bss_cfg)
1861                 return -ENOMEM;
1862
1863         mwifiex_set_sys_config_invalid_data(bss_cfg);
1864
1865         if (params->beacon_interval)
1866                 bss_cfg->beacon_period = params->beacon_interval;
1867         if (params->dtim_period)
1868                 bss_cfg->dtim_period = params->dtim_period;
1869
1870         if (params->ssid && params->ssid_len) {
1871                 memcpy(bss_cfg->ssid.ssid, params->ssid, params->ssid_len);
1872                 bss_cfg->ssid.ssid_len = params->ssid_len;
1873         }
1874         if (params->inactivity_timeout > 0) {
1875                 /* sta_ao_timer/ps_sta_ao_timer is in unit of 100ms */
1876                 bss_cfg->sta_ao_timer = 10 * params->inactivity_timeout;
1877                 bss_cfg->ps_sta_ao_timer = 10 * params->inactivity_timeout;
1878         }
1879
1880         switch (params->hidden_ssid) {
1881         case NL80211_HIDDEN_SSID_NOT_IN_USE:
1882                 bss_cfg->bcast_ssid_ctl = 1;
1883                 break;
1884         case NL80211_HIDDEN_SSID_ZERO_LEN:
1885                 bss_cfg->bcast_ssid_ctl = 0;
1886                 break;
1887         case NL80211_HIDDEN_SSID_ZERO_CONTENTS:
1888                 /* firmware doesn't support this type of hidden SSID */
1889         default:
1890                 kfree(bss_cfg);
1891                 return -EINVAL;
1892         }
1893
1894         mwifiex_uap_set_channel(priv, bss_cfg, params->chandef);
1895         mwifiex_set_uap_rates(bss_cfg, params);
1896
1897         if (mwifiex_set_secure_params(priv, bss_cfg, params)) {
1898                 kfree(bss_cfg);
1899                 mwifiex_dbg(priv->adapter, ERROR,
1900                             "Failed to parse secuirty parameters!\n");
1901                 return -1;
1902         }
1903
1904         mwifiex_set_ht_params(priv, bss_cfg, params);
1905
1906         if (priv->adapter->is_hw_11ac_capable) {
1907                 mwifiex_set_vht_params(priv, bss_cfg, params);
1908                 mwifiex_set_vht_width(priv, params->chandef.width,
1909                                       priv->ap_11ac_enabled);
1910         }
1911
1912         if (priv->ap_11ac_enabled)
1913                 mwifiex_set_11ac_ba_params(priv);
1914         else
1915                 mwifiex_set_ba_params(priv);
1916
1917         mwifiex_set_wmm_params(priv, bss_cfg, params);
1918
1919         if (mwifiex_is_11h_active(priv))
1920                 mwifiex_set_tpc_params(priv, bss_cfg, params);
1921
1922         if (mwifiex_is_11h_active(priv) &&
1923             !cfg80211_chandef_dfs_required(wiphy, &params->chandef,
1924                                            priv->bss_mode)) {
1925                 mwifiex_dbg(priv->adapter, INFO,
1926                             "Disable 11h extensions in FW\n");
1927                 if (mwifiex_11h_activate(priv, false)) {
1928                         mwifiex_dbg(priv->adapter, ERROR,
1929                                     "Failed to disable 11h extensions!!");
1930                         return -1;
1931                 }
1932                 priv->state_11h.is_11h_active = false;
1933         }
1934
1935         if (mwifiex_config_start_uap(priv, bss_cfg)) {
1936                 mwifiex_dbg(priv->adapter, ERROR,
1937                             "Failed to start AP\n");
1938                 kfree(bss_cfg);
1939                 return -1;
1940         }
1941
1942         if (mwifiex_set_mgmt_ies(priv, &params->beacon))
1943                 return -1;
1944
1945         if (!netif_carrier_ok(priv->netdev))
1946                 netif_carrier_on(priv->netdev);
1947         mwifiex_wake_up_net_dev_queue(priv->netdev, priv->adapter);
1948
1949         memcpy(&priv->bss_cfg, bss_cfg, sizeof(priv->bss_cfg));
1950         kfree(bss_cfg);
1951         return 0;
1952 }
1953
1954 /*
1955  * CFG802.11 operation handler for disconnection request.
1956  *
1957  * This function does not work when there is already a disconnection
1958  * procedure going on.
1959  */
1960 static int
1961 mwifiex_cfg80211_disconnect(struct wiphy *wiphy, struct net_device *dev,
1962                             u16 reason_code)
1963 {
1964         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1965
1966         if (!mwifiex_stop_bg_scan(priv))
1967                 cfg80211_sched_scan_stopped_rtnl(priv->wdev.wiphy);
1968
1969         if (mwifiex_deauthenticate(priv, NULL))
1970                 return -EFAULT;
1971
1972         mwifiex_dbg(priv->adapter, MSG,
1973                     "info: successfully disconnected from %pM:\t"
1974                     "reason code %d\n", priv->cfg_bssid, reason_code);
1975
1976         eth_zero_addr(priv->cfg_bssid);
1977         priv->hs2_enabled = false;
1978
1979         return 0;
1980 }
1981
1982 /*
1983  * This function informs the CFG802.11 subsystem of a new IBSS.
1984  *
1985  * The following information are sent to the CFG802.11 subsystem
1986  * to register the new IBSS. If we do not register the new IBSS,
1987  * a kernel panic will result.
1988  *      - SSID
1989  *      - SSID length
1990  *      - BSSID
1991  *      - Channel
1992  */
1993 static int mwifiex_cfg80211_inform_ibss_bss(struct mwifiex_private *priv)
1994 {
1995         struct ieee80211_channel *chan;
1996         struct mwifiex_bss_info bss_info;
1997         struct cfg80211_bss *bss;
1998         int ie_len;
1999         u8 ie_buf[IEEE80211_MAX_SSID_LEN + sizeof(struct ieee_types_header)];
2000         enum nl80211_band band;
2001
2002         if (mwifiex_get_bss_info(priv, &bss_info))
2003                 return -1;
2004
2005         ie_buf[0] = WLAN_EID_SSID;
2006         ie_buf[1] = bss_info.ssid.ssid_len;
2007
2008         memcpy(&ie_buf[sizeof(struct ieee_types_header)],
2009                &bss_info.ssid.ssid, bss_info.ssid.ssid_len);
2010         ie_len = ie_buf[1] + sizeof(struct ieee_types_header);
2011
2012         band = mwifiex_band_to_radio_type(priv->curr_bss_params.band);
2013         chan = __ieee80211_get_channel(priv->wdev.wiphy,
2014                         ieee80211_channel_to_frequency(bss_info.bss_chan,
2015                                                        band));
2016
2017         bss = cfg80211_inform_bss(priv->wdev.wiphy, chan,
2018                                   CFG80211_BSS_FTYPE_UNKNOWN,
2019                                   bss_info.bssid, 0, WLAN_CAPABILITY_IBSS,
2020                                   0, ie_buf, ie_len, 0, GFP_KERNEL);
2021         if (bss) {
2022                 cfg80211_put_bss(priv->wdev.wiphy, bss);
2023                 ether_addr_copy(priv->cfg_bssid, bss_info.bssid);
2024         }
2025
2026         return 0;
2027 }
2028
2029 /*
2030  * This function connects with a BSS.
2031  *
2032  * This function handles both Infra and Ad-Hoc modes. It also performs
2033  * validity checking on the provided parameters, disconnects from the
2034  * current BSS (if any), sets up the association/scan parameters,
2035  * including security settings, and performs specific SSID scan before
2036  * trying to connect.
2037  *
2038  * For Infra mode, the function returns failure if the specified SSID
2039  * is not found in scan table. However, for Ad-Hoc mode, it can create
2040  * the IBSS if it does not exist. On successful completion in either case,
2041  * the function notifies the CFG802.11 subsystem of the new BSS connection.
2042  */
2043 static int
2044 mwifiex_cfg80211_assoc(struct mwifiex_private *priv, size_t ssid_len,
2045                        const u8 *ssid, const u8 *bssid, int mode,
2046                        struct ieee80211_channel *channel,
2047                        struct cfg80211_connect_params *sme, bool privacy)
2048 {
2049         struct cfg80211_ssid req_ssid;
2050         int ret, auth_type = 0;
2051         struct cfg80211_bss *bss = NULL;
2052         u8 is_scanning_required = 0;
2053
2054         memset(&req_ssid, 0, sizeof(struct cfg80211_ssid));
2055
2056         req_ssid.ssid_len = ssid_len;
2057         if (ssid_len > IEEE80211_MAX_SSID_LEN) {
2058                 mwifiex_dbg(priv->adapter, ERROR, "invalid SSID - aborting\n");
2059                 return -EINVAL;
2060         }
2061
2062         memcpy(req_ssid.ssid, ssid, ssid_len);
2063         if (!req_ssid.ssid_len || req_ssid.ssid[0] < 0x20) {
2064                 mwifiex_dbg(priv->adapter, ERROR, "invalid SSID - aborting\n");
2065                 return -EINVAL;
2066         }
2067
2068         /* As this is new association, clear locally stored
2069          * keys and security related flags */
2070         priv->sec_info.wpa_enabled = false;
2071         priv->sec_info.wpa2_enabled = false;
2072         priv->wep_key_curr_index = 0;
2073         priv->sec_info.encryption_mode = 0;
2074         priv->sec_info.is_authtype_auto = 0;
2075         ret = mwifiex_set_encode(priv, NULL, NULL, 0, 0, NULL, 1);
2076
2077         if (mode == NL80211_IFTYPE_ADHOC) {
2078                 /* "privacy" is set only for ad-hoc mode */
2079                 if (privacy) {
2080                         /*
2081                          * Keep WLAN_CIPHER_SUITE_WEP104 for now so that
2082                          * the firmware can find a matching network from the
2083                          * scan. The cfg80211 does not give us the encryption
2084                          * mode at this stage so just setting it to WEP here.
2085                          */
2086                         priv->sec_info.encryption_mode =
2087                                         WLAN_CIPHER_SUITE_WEP104;
2088                         priv->sec_info.authentication_mode =
2089                                         NL80211_AUTHTYPE_OPEN_SYSTEM;
2090                 }
2091
2092                 goto done;
2093         }
2094
2095         /* Now handle infra mode. "sme" is valid for infra mode only */
2096         if (sme->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
2097                 auth_type = NL80211_AUTHTYPE_OPEN_SYSTEM;
2098                 priv->sec_info.is_authtype_auto = 1;
2099         } else {
2100                 auth_type = sme->auth_type;
2101         }
2102
2103         if (sme->crypto.n_ciphers_pairwise) {
2104                 priv->sec_info.encryption_mode =
2105                                                 sme->crypto.ciphers_pairwise[0];
2106                 priv->sec_info.authentication_mode = auth_type;
2107         }
2108
2109         if (sme->crypto.cipher_group) {
2110                 priv->sec_info.encryption_mode = sme->crypto.cipher_group;
2111                 priv->sec_info.authentication_mode = auth_type;
2112         }
2113         if (sme->ie)
2114                 ret = mwifiex_set_gen_ie(priv, sme->ie, sme->ie_len);
2115
2116         if (sme->key) {
2117                 if (mwifiex_is_alg_wep(priv->sec_info.encryption_mode)) {
2118                         mwifiex_dbg(priv->adapter, INFO,
2119                                     "info: setting wep encryption\t"
2120                                     "with key len %d\n", sme->key_len);
2121                         priv->wep_key_curr_index = sme->key_idx;
2122                         ret = mwifiex_set_encode(priv, NULL, sme->key,
2123                                                  sme->key_len, sme->key_idx,
2124                                                  NULL, 0);
2125                 }
2126         }
2127 done:
2128         /*
2129          * Scan entries are valid for some time (15 sec). So we can save one
2130          * active scan time if we just try cfg80211_get_bss first. If it fails
2131          * then request scan and cfg80211_get_bss() again for final output.
2132          */
2133         while (1) {
2134                 if (is_scanning_required) {
2135                         /* Do specific SSID scanning */
2136                         if (mwifiex_request_scan(priv, &req_ssid)) {
2137                                 mwifiex_dbg(priv->adapter, ERROR, "scan error\n");
2138                                 return -EFAULT;
2139                         }
2140                 }
2141
2142                 /* Find the BSS we want using available scan results */
2143                 if (mode == NL80211_IFTYPE_ADHOC)
2144                         bss = cfg80211_get_bss(priv->wdev.wiphy, channel,
2145                                                bssid, ssid, ssid_len,
2146                                                IEEE80211_BSS_TYPE_IBSS,
2147                                                IEEE80211_PRIVACY_ANY);
2148                 else
2149                         bss = cfg80211_get_bss(priv->wdev.wiphy, channel,
2150                                                bssid, ssid, ssid_len,
2151                                                IEEE80211_BSS_TYPE_ESS,
2152                                                IEEE80211_PRIVACY_ANY);
2153
2154                 if (!bss) {
2155                         if (is_scanning_required) {
2156                                 mwifiex_dbg(priv->adapter, WARN,
2157                                             "assoc: requested bss not found in scan results\n");
2158                                 break;
2159                         }
2160                         is_scanning_required = 1;
2161                 } else {
2162                         mwifiex_dbg(priv->adapter, MSG,
2163                                     "info: trying to associate to '%s' bssid %pM\n",
2164                                     (char *)req_ssid.ssid, bss->bssid);
2165                         memcpy(&priv->cfg_bssid, bss->bssid, ETH_ALEN);
2166                         break;
2167                 }
2168         }
2169
2170         ret = mwifiex_bss_start(priv, bss, &req_ssid);
2171         if (ret)
2172                 return ret;
2173
2174         if (mode == NL80211_IFTYPE_ADHOC) {
2175                 /* Inform the BSS information to kernel, otherwise
2176                  * kernel will give a panic after successful assoc */
2177                 if (mwifiex_cfg80211_inform_ibss_bss(priv))
2178                         return -EFAULT;
2179         }
2180
2181         return ret;
2182 }
2183
2184 /*
2185  * CFG802.11 operation handler for association request.
2186  *
2187  * This function does not work when the current mode is set to Ad-Hoc, or
2188  * when there is already an association procedure going on. The given BSS
2189  * information is used to associate.
2190  */
2191 static int
2192 mwifiex_cfg80211_connect(struct wiphy *wiphy, struct net_device *dev,
2193                          struct cfg80211_connect_params *sme)
2194 {
2195         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
2196         struct mwifiex_adapter *adapter = priv->adapter;
2197         int ret;
2198
2199         if (GET_BSS_ROLE(priv) != MWIFIEX_BSS_ROLE_STA) {
2200                 mwifiex_dbg(adapter, ERROR,
2201                             "%s: reject infra assoc request in non-STA role\n",
2202                             dev->name);
2203                 return -EINVAL;
2204         }
2205
2206         if (priv->wdev.current_bss) {
2207                 mwifiex_dbg(adapter, ERROR,
2208                             "%s: already connected\n", dev->name);
2209                 return -EALREADY;
2210         }
2211
2212         if (adapter->surprise_removed || adapter->is_cmd_timedout) {
2213                 mwifiex_dbg(adapter, ERROR,
2214                             "%s: Ignore connection.\t"
2215                             "Card removed or FW in bad state\n",
2216                             dev->name);
2217                 return -EFAULT;
2218         }
2219
2220         mwifiex_dbg(adapter, INFO,
2221                     "info: Trying to associate to %s and bssid %pM\n",
2222                     (char *)sme->ssid, sme->bssid);
2223
2224         if (!mwifiex_stop_bg_scan(priv))
2225                 cfg80211_sched_scan_stopped_rtnl(priv->wdev.wiphy);
2226
2227         ret = mwifiex_cfg80211_assoc(priv, sme->ssid_len, sme->ssid, sme->bssid,
2228                                      priv->bss_mode, sme->channel, sme, 0);
2229         if (!ret) {
2230                 cfg80211_connect_result(priv->netdev, priv->cfg_bssid, NULL, 0,
2231                                         NULL, 0, WLAN_STATUS_SUCCESS,
2232                                         GFP_KERNEL);
2233                 mwifiex_dbg(priv->adapter, MSG,
2234                             "info: associated to bssid %pM successfully\n",
2235                             priv->cfg_bssid);
2236                 if (ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
2237                     priv->adapter->auto_tdls &&
2238                     priv->bss_type == MWIFIEX_BSS_TYPE_STA)
2239                         mwifiex_setup_auto_tdls_timer(priv);
2240         } else {
2241                 mwifiex_dbg(priv->adapter, ERROR,
2242                             "info: association to bssid %pM failed\n",
2243                             priv->cfg_bssid);
2244                 eth_zero_addr(priv->cfg_bssid);
2245
2246                 if (ret > 0)
2247                         cfg80211_connect_result(priv->netdev, priv->cfg_bssid,
2248                                                 NULL, 0, NULL, 0, ret,
2249                                                 GFP_KERNEL);
2250                 else
2251                         cfg80211_connect_result(priv->netdev, priv->cfg_bssid,
2252                                                 NULL, 0, NULL, 0,
2253                                                 WLAN_STATUS_UNSPECIFIED_FAILURE,
2254                                                 GFP_KERNEL);
2255         }
2256
2257         return 0;
2258 }
2259
2260 /*
2261  * This function sets following parameters for ibss network.
2262  *  -  channel
2263  *  -  start band
2264  *  -  11n flag
2265  *  -  secondary channel offset
2266  */
2267 static int mwifiex_set_ibss_params(struct mwifiex_private *priv,
2268                                    struct cfg80211_ibss_params *params)
2269 {
2270         struct mwifiex_adapter *adapter = priv->adapter;
2271         int index = 0, i;
2272         u8 config_bands = 0;
2273
2274         if (params->chandef.chan->band == NL80211_BAND_2GHZ) {
2275                 if (!params->basic_rates) {
2276                         config_bands = BAND_B | BAND_G;
2277                 } else {
2278                         for (i = 0; i < mwifiex_band_2ghz.n_bitrates; i++) {
2279                                 /*
2280                                  * Rates below 6 Mbps in the table are CCK
2281                                  * rates; 802.11b and from 6 they are OFDM;
2282                                  * 802.11G
2283                                  */
2284                                 if (mwifiex_rates[i].bitrate == 60) {
2285                                         index = 1 << i;
2286                                         break;
2287                                 }
2288                         }
2289
2290                         if (params->basic_rates < index) {
2291                                 config_bands = BAND_B;
2292                         } else {
2293                                 config_bands = BAND_G;
2294                                 if (params->basic_rates % index)
2295                                         config_bands |= BAND_B;
2296                         }
2297                 }
2298
2299                 if (cfg80211_get_chandef_type(&params->chandef) !=
2300                                                 NL80211_CHAN_NO_HT)
2301                         config_bands |= BAND_G | BAND_GN;
2302         } else {
2303                 if (cfg80211_get_chandef_type(&params->chandef) ==
2304                                                 NL80211_CHAN_NO_HT)
2305                         config_bands = BAND_A;
2306                 else
2307                         config_bands = BAND_AN | BAND_A;
2308         }
2309
2310         if (!((config_bands | adapter->fw_bands) & ~adapter->fw_bands)) {
2311                 adapter->config_bands = config_bands;
2312                 adapter->adhoc_start_band = config_bands;
2313
2314                 if ((config_bands & BAND_GN) || (config_bands & BAND_AN))
2315                         adapter->adhoc_11n_enabled = true;
2316                 else
2317                         adapter->adhoc_11n_enabled = false;
2318         }
2319
2320         adapter->sec_chan_offset =
2321                 mwifiex_chan_type_to_sec_chan_offset(
2322                         cfg80211_get_chandef_type(&params->chandef));
2323         priv->adhoc_channel = ieee80211_frequency_to_channel(
2324                                 params->chandef.chan->center_freq);
2325
2326         mwifiex_dbg(adapter, INFO,
2327                     "info: set ibss band %d, chan %d, chan offset %d\n",
2328                     config_bands, priv->adhoc_channel,
2329                     adapter->sec_chan_offset);
2330
2331         return 0;
2332 }
2333
2334 /*
2335  * CFG802.11 operation handler to join an IBSS.
2336  *
2337  * This function does not work in any mode other than Ad-Hoc, or if
2338  * a join operation is already in progress.
2339  */
2340 static int
2341 mwifiex_cfg80211_join_ibss(struct wiphy *wiphy, struct net_device *dev,
2342                            struct cfg80211_ibss_params *params)
2343 {
2344         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
2345         int ret = 0;
2346
2347         if (priv->bss_mode != NL80211_IFTYPE_ADHOC) {
2348                 mwifiex_dbg(priv->adapter, ERROR,
2349                             "request to join ibss received\t"
2350                             "when station is not in ibss mode\n");
2351                 goto done;
2352         }
2353
2354         mwifiex_dbg(priv->adapter, MSG,
2355                     "info: trying to join to %s and bssid %pM\n",
2356                     (char *)params->ssid, params->bssid);
2357
2358         mwifiex_set_ibss_params(priv, params);
2359
2360         ret = mwifiex_cfg80211_assoc(priv, params->ssid_len, params->ssid,
2361                                      params->bssid, priv->bss_mode,
2362                                      params->chandef.chan, NULL,
2363                                      params->privacy);
2364 done:
2365         if (!ret) {
2366                 cfg80211_ibss_joined(priv->netdev, priv->cfg_bssid,
2367                                      params->chandef.chan, GFP_KERNEL);
2368                 mwifiex_dbg(priv->adapter, MSG,
2369                             "info: joined/created adhoc network with bssid\t"
2370                             "%pM successfully\n", priv->cfg_bssid);
2371         } else {
2372                 mwifiex_dbg(priv->adapter, ERROR,
2373                             "info: failed creating/joining adhoc network\n");
2374         }
2375
2376         return ret;
2377 }
2378
2379 /*
2380  * CFG802.11 operation handler to leave an IBSS.
2381  *
2382  * This function does not work if a leave operation is
2383  * already in progress.
2384  */
2385 static int
2386 mwifiex_cfg80211_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
2387 {
2388         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
2389
2390         mwifiex_dbg(priv->adapter, MSG, "info: disconnecting from essid %pM\n",
2391                     priv->cfg_bssid);
2392         if (mwifiex_deauthenticate(priv, NULL))
2393                 return -EFAULT;
2394
2395         eth_zero_addr(priv->cfg_bssid);
2396
2397         return 0;
2398 }
2399
2400 /*
2401  * CFG802.11 operation handler for scan request.
2402  *
2403  * This function issues a scan request to the firmware based upon
2404  * the user specified scan configuration. On successful completion,
2405  * it also informs the results.
2406  */
2407 static int
2408 mwifiex_cfg80211_scan(struct wiphy *wiphy,
2409                       struct cfg80211_scan_request *request)
2410 {
2411         struct net_device *dev = request->wdev->netdev;
2412         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
2413         int i, offset, ret;
2414         struct ieee80211_channel *chan;
2415         struct ieee_types_header *ie;
2416         struct mwifiex_user_scan_cfg *user_scan_cfg;
2417
2418         mwifiex_dbg(priv->adapter, CMD,
2419                     "info: received scan request on %s\n", dev->name);
2420
2421         /* Block scan request if scan operation or scan cleanup when interface
2422          * is disabled is in process
2423          */
2424         if (priv->scan_request || priv->scan_aborting) {
2425                 mwifiex_dbg(priv->adapter, WARN,
2426                             "cmd: Scan already in process..\n");
2427                 return -EBUSY;
2428         }
2429
2430         if (!mwifiex_stop_bg_scan(priv))
2431                 cfg80211_sched_scan_stopped_rtnl(priv->wdev.wiphy);
2432
2433         user_scan_cfg = kzalloc(sizeof(*user_scan_cfg), GFP_KERNEL);
2434         if (!user_scan_cfg)
2435                 return -ENOMEM;
2436
2437         priv->scan_request = request;
2438
2439         user_scan_cfg->num_ssids = request->n_ssids;
2440         user_scan_cfg->ssid_list = request->ssids;
2441
2442         if (request->ie && request->ie_len) {
2443                 offset = 0;
2444                 for (i = 0; i < MWIFIEX_MAX_VSIE_NUM; i++) {
2445                         if (priv->vs_ie[i].mask != MWIFIEX_VSIE_MASK_CLEAR)
2446                                 continue;
2447                         priv->vs_ie[i].mask = MWIFIEX_VSIE_MASK_SCAN;
2448                         ie = (struct ieee_types_header *)(request->ie + offset);
2449                         memcpy(&priv->vs_ie[i].ie, ie, sizeof(*ie) + ie->len);
2450                         offset += sizeof(*ie) + ie->len;
2451
2452                         if (offset >= request->ie_len)
2453                                 break;
2454                 }
2455         }
2456
2457         for (i = 0; i < min_t(u32, request->n_channels,
2458                               MWIFIEX_USER_SCAN_CHAN_MAX); i++) {
2459                 chan = request->channels[i];
2460                 user_scan_cfg->chan_list[i].chan_number = chan->hw_value;
2461                 user_scan_cfg->chan_list[i].radio_type = chan->band;
2462
2463                 if ((chan->flags & IEEE80211_CHAN_NO_IR) || !request->n_ssids)
2464                         user_scan_cfg->chan_list[i].scan_type =
2465                                                 MWIFIEX_SCAN_TYPE_PASSIVE;
2466                 else
2467                         user_scan_cfg->chan_list[i].scan_type =
2468                                                 MWIFIEX_SCAN_TYPE_ACTIVE;
2469
2470                 user_scan_cfg->chan_list[i].scan_time = 0;
2471         }
2472
2473         if (priv->adapter->scan_chan_gap_enabled &&
2474             mwifiex_is_any_intf_active(priv))
2475                 user_scan_cfg->scan_chan_gap =
2476                                               priv->adapter->scan_chan_gap_time;
2477
2478         ret = mwifiex_scan_networks(priv, user_scan_cfg);
2479         kfree(user_scan_cfg);
2480         if (ret) {
2481                 mwifiex_dbg(priv->adapter, ERROR,
2482                             "scan failed: %d\n", ret);
2483                 priv->scan_aborting = false;
2484                 priv->scan_request = NULL;
2485                 return ret;
2486         }
2487
2488         if (request->ie && request->ie_len) {
2489                 for (i = 0; i < MWIFIEX_MAX_VSIE_NUM; i++) {
2490                         if (priv->vs_ie[i].mask == MWIFIEX_VSIE_MASK_SCAN) {
2491                                 priv->vs_ie[i].mask = MWIFIEX_VSIE_MASK_CLEAR;
2492                                 memset(&priv->vs_ie[i].ie, 0,
2493                                        MWIFIEX_MAX_VSIE_LEN);
2494                         }
2495                 }
2496         }
2497         return 0;
2498 }
2499
2500 /* CFG802.11 operation handler for sched_scan_start.
2501  *
2502  * This function issues a bgscan config request to the firmware based upon
2503  * the user specified sched_scan configuration. On successful completion,
2504  * firmware will generate BGSCAN_REPORT event, driver should issue bgscan
2505  * query command to get sched_scan results from firmware.
2506  */
2507 static int
2508 mwifiex_cfg80211_sched_scan_start(struct wiphy *wiphy,
2509                                   struct net_device *dev,
2510                                   struct cfg80211_sched_scan_request *request)
2511 {
2512         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
2513         int i, offset;
2514         struct ieee80211_channel *chan;
2515         struct mwifiex_bg_scan_cfg *bgscan_cfg;
2516         struct ieee_types_header *ie;
2517
2518         if (!request || (!request->n_ssids && !request->n_match_sets)) {
2519                 wiphy_err(wiphy, "%s : Invalid Sched_scan parameters",
2520                           __func__);
2521                 return -EINVAL;
2522         }
2523
2524         wiphy_info(wiphy, "sched_scan start : n_ssids=%d n_match_sets=%d ",
2525                    request->n_ssids, request->n_match_sets);
2526         wiphy_info(wiphy, "n_channels=%d interval=%d ie_len=%d\n",
2527                    request->n_channels, request->scan_plans->interval,
2528                    (int)request->ie_len);
2529
2530         bgscan_cfg = kzalloc(sizeof(*bgscan_cfg), GFP_KERNEL);
2531         if (!bgscan_cfg)
2532                 return -ENOMEM;
2533
2534         if (priv->scan_request || priv->scan_aborting)
2535                 bgscan_cfg->start_later = true;
2536
2537         bgscan_cfg->num_ssids = request->n_match_sets;
2538         bgscan_cfg->ssid_list = request->match_sets;
2539
2540         if (request->ie && request->ie_len) {
2541                 offset = 0;
2542                 for (i = 0; i < MWIFIEX_MAX_VSIE_NUM; i++) {
2543                         if (priv->vs_ie[i].mask != MWIFIEX_VSIE_MASK_CLEAR)
2544                                 continue;
2545                         priv->vs_ie[i].mask = MWIFIEX_VSIE_MASK_BGSCAN;
2546                         ie = (struct ieee_types_header *)(request->ie + offset);
2547                         memcpy(&priv->vs_ie[i].ie, ie, sizeof(*ie) + ie->len);
2548                         offset += sizeof(*ie) + ie->len;
2549
2550                         if (offset >= request->ie_len)
2551                                 break;
2552                 }
2553         }
2554
2555         for (i = 0; i < min_t(u32, request->n_channels,
2556                               MWIFIEX_BG_SCAN_CHAN_MAX); i++) {
2557                 chan = request->channels[i];
2558                 bgscan_cfg->chan_list[i].chan_number = chan->hw_value;
2559                 bgscan_cfg->chan_list[i].radio_type = chan->band;
2560
2561                 if ((chan->flags & IEEE80211_CHAN_NO_IR) || !request->n_ssids)
2562                         bgscan_cfg->chan_list[i].scan_type =
2563                                                 MWIFIEX_SCAN_TYPE_PASSIVE;
2564                 else
2565                         bgscan_cfg->chan_list[i].scan_type =
2566                                                 MWIFIEX_SCAN_TYPE_ACTIVE;
2567
2568                 bgscan_cfg->chan_list[i].scan_time = 0;
2569         }
2570
2571         bgscan_cfg->chan_per_scan = min_t(u32, request->n_channels,
2572                                           MWIFIEX_BG_SCAN_CHAN_MAX);
2573
2574         /* Use at least 15 second for per scan cycle */
2575         bgscan_cfg->scan_interval = (request->scan_plans->interval >
2576                                      MWIFIEX_BGSCAN_INTERVAL) ?
2577                                 request->scan_plans->interval :
2578                                 MWIFIEX_BGSCAN_INTERVAL;
2579
2580         bgscan_cfg->repeat_count = MWIFIEX_BGSCAN_REPEAT_COUNT;
2581         bgscan_cfg->report_condition = MWIFIEX_BGSCAN_SSID_MATCH |
2582                                 MWIFIEX_BGSCAN_WAIT_ALL_CHAN_DONE;
2583         bgscan_cfg->bss_type = MWIFIEX_BSS_MODE_INFRA;
2584         bgscan_cfg->action = MWIFIEX_BGSCAN_ACT_SET;
2585         bgscan_cfg->enable = true;
2586         if (request->min_rssi_thold != NL80211_SCAN_RSSI_THOLD_OFF) {
2587                 bgscan_cfg->report_condition |= MWIFIEX_BGSCAN_SSID_RSSI_MATCH;
2588                 bgscan_cfg->rssi_threshold = request->min_rssi_thold;
2589         }
2590
2591         if (mwifiex_send_cmd(priv, HostCmd_CMD_802_11_BG_SCAN_CONFIG,
2592                              HostCmd_ACT_GEN_SET, 0, bgscan_cfg, true)) {
2593                 kfree(bgscan_cfg);
2594                 return -EFAULT;
2595         }
2596
2597         priv->sched_scanning = true;
2598
2599         kfree(bgscan_cfg);
2600         return 0;
2601 }
2602
2603 /* CFG802.11 operation handler for sched_scan_stop.
2604  *
2605  * This function issues a bgscan config command to disable
2606  * previous bgscan configuration in the firmware
2607  */
2608 static int mwifiex_cfg80211_sched_scan_stop(struct wiphy *wiphy,
2609                                             struct net_device *dev)
2610 {
2611         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
2612
2613         wiphy_info(wiphy, "sched scan stop!");
2614         mwifiex_stop_bg_scan(priv);
2615
2616         return 0;
2617 }
2618
2619 static void mwifiex_setup_vht_caps(struct ieee80211_sta_vht_cap *vht_info,
2620                                    struct mwifiex_private *priv)
2621 {
2622         struct mwifiex_adapter *adapter = priv->adapter;
2623
2624         vht_info->vht_supported = true;
2625
2626         vht_info->cap = adapter->hw_dot_11ac_dev_cap;
2627         /* Update MCS support for VHT */
2628         vht_info->vht_mcs.rx_mcs_map = cpu_to_le16(
2629                                 adapter->hw_dot_11ac_mcs_support & 0xFFFF);
2630         vht_info->vht_mcs.rx_highest = 0;
2631         vht_info->vht_mcs.tx_mcs_map = cpu_to_le16(
2632                                 adapter->hw_dot_11ac_mcs_support >> 16);
2633         vht_info->vht_mcs.tx_highest = 0;
2634 }
2635
2636 /*
2637  * This function sets up the CFG802.11 specific HT capability fields
2638  * with default values.
2639  *
2640  * The following default values are set -
2641  *      - HT Supported = True
2642  *      - Maximum AMPDU length factor = IEEE80211_HT_MAX_AMPDU_64K
2643  *      - Minimum AMPDU spacing = IEEE80211_HT_MPDU_DENSITY_NONE
2644  *      - HT Capabilities supported by firmware
2645  *      - MCS information, Rx mask = 0xff
2646  *      - MCD information, Tx parameters = IEEE80211_HT_MCS_TX_DEFINED (0x01)
2647  */
2648 static void
2649 mwifiex_setup_ht_caps(struct ieee80211_sta_ht_cap *ht_info,
2650                       struct mwifiex_private *priv)
2651 {
2652         int rx_mcs_supp;
2653         struct ieee80211_mcs_info mcs_set;
2654         u8 *mcs = (u8 *)&mcs_set;
2655         struct mwifiex_adapter *adapter = priv->adapter;
2656
2657         ht_info->ht_supported = true;
2658         ht_info->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
2659         ht_info->ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
2660
2661         memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
2662
2663         /* Fill HT capability information */
2664         if (ISSUPP_CHANWIDTH40(adapter->hw_dot_11n_dev_cap))
2665                 ht_info->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
2666         else
2667                 ht_info->cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
2668
2669         if (ISSUPP_SHORTGI20(adapter->hw_dot_11n_dev_cap))
2670                 ht_info->cap |= IEEE80211_HT_CAP_SGI_20;
2671         else
2672                 ht_info->cap &= ~IEEE80211_HT_CAP_SGI_20;
2673
2674         if (ISSUPP_SHORTGI40(adapter->hw_dot_11n_dev_cap))
2675                 ht_info->cap |= IEEE80211_HT_CAP_SGI_40;
2676         else
2677                 ht_info->cap &= ~IEEE80211_HT_CAP_SGI_40;
2678
2679         if (adapter->user_dev_mcs_support == HT_STREAM_2X2)
2680                 ht_info->cap |= 3 << IEEE80211_HT_CAP_RX_STBC_SHIFT;
2681         else
2682                 ht_info->cap |= 1 << IEEE80211_HT_CAP_RX_STBC_SHIFT;
2683
2684         if (ISSUPP_TXSTBC(adapter->hw_dot_11n_dev_cap))
2685                 ht_info->cap |= IEEE80211_HT_CAP_TX_STBC;
2686         else
2687                 ht_info->cap &= ~IEEE80211_HT_CAP_TX_STBC;
2688
2689         if (ISSUPP_GREENFIELD(adapter->hw_dot_11n_dev_cap))
2690                 ht_info->cap |= IEEE80211_HT_CAP_GRN_FLD;
2691         else
2692                 ht_info->cap &= ~IEEE80211_HT_CAP_GRN_FLD;
2693
2694         if (ISENABLED_40MHZ_INTOLERANT(adapter->hw_dot_11n_dev_cap))
2695                 ht_info->cap |= IEEE80211_HT_CAP_40MHZ_INTOLERANT;
2696         else
2697                 ht_info->cap &= ~IEEE80211_HT_CAP_40MHZ_INTOLERANT;
2698
2699         if (ISSUPP_RXLDPC(adapter->hw_dot_11n_dev_cap))
2700                 ht_info->cap |= IEEE80211_HT_CAP_LDPC_CODING;
2701         else
2702                 ht_info->cap &= ~IEEE80211_HT_CAP_LDPC_CODING;
2703
2704         ht_info->cap &= ~IEEE80211_HT_CAP_MAX_AMSDU;
2705         ht_info->cap |= IEEE80211_HT_CAP_SM_PS;
2706
2707         rx_mcs_supp = GET_RXMCSSUPP(adapter->user_dev_mcs_support);
2708         /* Set MCS for 1x1/2x2 */
2709         memset(mcs, 0xff, rx_mcs_supp);
2710         /* Clear all the other values */
2711         memset(&mcs[rx_mcs_supp], 0,
2712                sizeof(struct ieee80211_mcs_info) - rx_mcs_supp);
2713         if (priv->bss_mode == NL80211_IFTYPE_STATION ||
2714             ISSUPP_CHANWIDTH40(adapter->hw_dot_11n_dev_cap))
2715                 /* Set MCS32 for infra mode or ad-hoc mode with 40MHz support */
2716                 SETHT_MCS32(mcs_set.rx_mask);
2717
2718         memcpy((u8 *) &ht_info->mcs, mcs, sizeof(struct ieee80211_mcs_info));
2719
2720         ht_info->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
2721 }
2722
2723 /*
2724  *  create a new virtual interface with the given name and name assign type
2725  */
2726 struct wireless_dev *mwifiex_add_virtual_intf(struct wiphy *wiphy,
2727                                               const char *name,
2728                                               unsigned char name_assign_type,
2729                                               enum nl80211_iftype type,
2730                                               u32 *flags,
2731                                               struct vif_params *params)
2732 {
2733         struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
2734         struct mwifiex_private *priv;
2735         struct net_device *dev;
2736         void *mdev_priv;
2737
2738         if (!adapter)
2739                 return ERR_PTR(-EFAULT);
2740
2741         switch (type) {
2742         case NL80211_IFTYPE_UNSPECIFIED:
2743         case NL80211_IFTYPE_STATION:
2744         case NL80211_IFTYPE_ADHOC:
2745                 if (adapter->curr_iface_comb.sta_intf ==
2746                     adapter->iface_limit.sta_intf) {
2747                         mwifiex_dbg(adapter, ERROR,
2748                                     "cannot create multiple sta/adhoc ifaces\n");
2749                         return ERR_PTR(-EINVAL);
2750                 }
2751
2752                 priv = mwifiex_get_unused_priv_by_bss_type(
2753                                                 adapter, MWIFIEX_BSS_TYPE_STA);
2754                 if (!priv) {
2755                         mwifiex_dbg(adapter, ERROR,
2756                                     "could not get free private struct\n");
2757                         return ERR_PTR(-EFAULT);
2758                 }
2759
2760                 priv->wdev.wiphy = wiphy;
2761                 priv->wdev.iftype = NL80211_IFTYPE_STATION;
2762
2763                 if (type == NL80211_IFTYPE_UNSPECIFIED)
2764                         priv->bss_mode = NL80211_IFTYPE_STATION;
2765                 else
2766                         priv->bss_mode = type;
2767
2768                 priv->bss_type = MWIFIEX_BSS_TYPE_STA;
2769                 priv->frame_type = MWIFIEX_DATA_FRAME_TYPE_ETH_II;
2770                 priv->bss_priority = 0;
2771                 priv->bss_role = MWIFIEX_BSS_ROLE_STA;
2772
2773                 break;
2774         case NL80211_IFTYPE_AP:
2775                 if (adapter->curr_iface_comb.uap_intf ==
2776                     adapter->iface_limit.uap_intf) {
2777                         mwifiex_dbg(adapter, ERROR,
2778                                     "cannot create multiple AP ifaces\n");
2779                         return ERR_PTR(-EINVAL);
2780                 }
2781
2782                 priv = mwifiex_get_unused_priv_by_bss_type(
2783                                                 adapter, MWIFIEX_BSS_TYPE_UAP);
2784                 if (!priv) {
2785                         mwifiex_dbg(adapter, ERROR,
2786                                     "could not get free private struct\n");
2787                         return ERR_PTR(-EFAULT);
2788                 }
2789
2790                 priv->wdev.wiphy = wiphy;
2791                 priv->wdev.iftype = NL80211_IFTYPE_AP;
2792
2793                 priv->bss_type = MWIFIEX_BSS_TYPE_UAP;
2794                 priv->frame_type = MWIFIEX_DATA_FRAME_TYPE_ETH_II;
2795                 priv->bss_priority = 0;
2796                 priv->bss_role = MWIFIEX_BSS_ROLE_UAP;
2797                 priv->bss_started = 0;
2798                 priv->bss_mode = type;
2799
2800                 break;
2801         case NL80211_IFTYPE_P2P_CLIENT:
2802                 if (adapter->curr_iface_comb.p2p_intf ==
2803                     adapter->iface_limit.p2p_intf) {
2804                         mwifiex_dbg(adapter, ERROR,
2805                                     "cannot create multiple P2P ifaces\n");
2806                         return ERR_PTR(-EINVAL);
2807                 }
2808
2809                 priv = mwifiex_get_unused_priv_by_bss_type(
2810                                                 adapter, MWIFIEX_BSS_TYPE_P2P);
2811                 if (!priv) {
2812                         mwifiex_dbg(adapter, ERROR,
2813                                     "could not get free private struct\n");
2814                         return ERR_PTR(-EFAULT);
2815                 }
2816
2817                 priv->wdev.wiphy = wiphy;
2818                 /* At start-up, wpa_supplicant tries to change the interface
2819                  * to NL80211_IFTYPE_STATION if it is not managed mode.
2820                  */
2821                 priv->wdev.iftype = NL80211_IFTYPE_P2P_CLIENT;
2822                 priv->bss_mode = NL80211_IFTYPE_P2P_CLIENT;
2823
2824                 /* Setting bss_type to P2P tells firmware that this interface
2825                  * is receiving P2P peers found during find phase and doing
2826                  * action frame handshake.
2827                  */
2828                 priv->bss_type = MWIFIEX_BSS_TYPE_P2P;
2829
2830                 priv->frame_type = MWIFIEX_DATA_FRAME_TYPE_ETH_II;
2831                 priv->bss_priority = MWIFIEX_BSS_ROLE_STA;
2832                 priv->bss_role = MWIFIEX_BSS_ROLE_STA;
2833                 priv->bss_started = 0;
2834
2835                 if (mwifiex_cfg80211_init_p2p_client(priv)) {
2836                         memset(&priv->wdev, 0, sizeof(priv->wdev));
2837                         priv->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
2838                         return ERR_PTR(-EFAULT);
2839                 }
2840
2841                 break;
2842         default:
2843                 mwifiex_dbg(adapter, ERROR, "type not supported\n");
2844                 return ERR_PTR(-EINVAL);
2845         }
2846
2847         dev = alloc_netdev_mqs(sizeof(struct mwifiex_private *), name,
2848                                name_assign_type, ether_setup,
2849                                IEEE80211_NUM_ACS, 1);
2850         if (!dev) {
2851                 mwifiex_dbg(adapter, ERROR,
2852                             "no memory available for netdevice\n");
2853                 memset(&priv->wdev, 0, sizeof(priv->wdev));
2854                 priv->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
2855                 priv->bss_mode = NL80211_IFTYPE_UNSPECIFIED;
2856                 return ERR_PTR(-ENOMEM);
2857         }
2858
2859         mwifiex_init_priv_params(priv, dev);
2860         priv->netdev = dev;
2861
2862         mwifiex_setup_ht_caps(&wiphy->bands[NL80211_BAND_2GHZ]->ht_cap, priv);
2863         if (adapter->is_hw_11ac_capable)
2864                 mwifiex_setup_vht_caps(
2865                         &wiphy->bands[NL80211_BAND_2GHZ]->vht_cap, priv);
2866
2867         if (adapter->config_bands & BAND_A)
2868                 mwifiex_setup_ht_caps(
2869                         &wiphy->bands[NL80211_BAND_5GHZ]->ht_cap, priv);
2870
2871         if ((adapter->config_bands & BAND_A) && adapter->is_hw_11ac_capable)
2872                 mwifiex_setup_vht_caps(
2873                         &wiphy->bands[NL80211_BAND_5GHZ]->vht_cap, priv);
2874
2875         dev_net_set(dev, wiphy_net(wiphy));
2876         dev->ieee80211_ptr = &priv->wdev;
2877         dev->ieee80211_ptr->iftype = priv->bss_mode;
2878         memcpy(dev->dev_addr, wiphy->perm_addr, ETH_ALEN);
2879         SET_NETDEV_DEV(dev, wiphy_dev(wiphy));
2880
2881         dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
2882         dev->watchdog_timeo = MWIFIEX_DEFAULT_WATCHDOG_TIMEOUT;
2883         dev->hard_header_len += MWIFIEX_MIN_DATA_HEADER_LEN;
2884         dev->ethtool_ops = &mwifiex_ethtool_ops;
2885
2886         mdev_priv = netdev_priv(dev);
2887         *((unsigned long *) mdev_priv) = (unsigned long) priv;
2888
2889         SET_NETDEV_DEV(dev, adapter->dev);
2890
2891         /* Register network device */
2892         if (register_netdevice(dev)) {
2893                 mwifiex_dbg(adapter, ERROR,
2894                             "cannot register virtual network device\n");
2895                 free_netdev(dev);
2896                 priv->bss_mode = NL80211_IFTYPE_UNSPECIFIED;
2897                 priv->netdev = NULL;
2898                 memset(&priv->wdev, 0, sizeof(priv->wdev));
2899                 priv->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
2900                 return ERR_PTR(-EFAULT);
2901         }
2902
2903         priv->dfs_cac_workqueue = alloc_workqueue("MWIFIEX_DFS_CAC%s",
2904                                                   WQ_HIGHPRI |
2905                                                   WQ_MEM_RECLAIM |
2906                                                   WQ_UNBOUND, 1, name);
2907         if (!priv->dfs_cac_workqueue) {
2908                 mwifiex_dbg(adapter, ERROR,
2909                             "cannot register virtual network device\n");
2910                 free_netdev(dev);
2911                 priv->bss_mode = NL80211_IFTYPE_UNSPECIFIED;
2912                 priv->netdev = NULL;
2913                 memset(&priv->wdev, 0, sizeof(priv->wdev));
2914                 priv->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
2915                 return ERR_PTR(-ENOMEM);
2916         }
2917
2918         INIT_DELAYED_WORK(&priv->dfs_cac_work, mwifiex_dfs_cac_work_queue);
2919
2920         priv->dfs_chan_sw_workqueue = alloc_workqueue("MWIFIEX_DFS_CHSW%s",
2921                                                       WQ_HIGHPRI | WQ_UNBOUND |
2922                                                       WQ_MEM_RECLAIM, 1, name);
2923         if (!priv->dfs_chan_sw_workqueue) {
2924                 mwifiex_dbg(adapter, ERROR,
2925                             "cannot register virtual network device\n");
2926                 free_netdev(dev);
2927                 priv->bss_mode = NL80211_IFTYPE_UNSPECIFIED;
2928                 priv->netdev = NULL;
2929                 memset(&priv->wdev, 0, sizeof(priv->wdev));
2930                 priv->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
2931                 return ERR_PTR(-ENOMEM);
2932         }
2933
2934         INIT_DELAYED_WORK(&priv->dfs_chan_sw_work,
2935                           mwifiex_dfs_chan_sw_work_queue);
2936
2937         sema_init(&priv->async_sem, 1);
2938
2939         mwifiex_dbg(adapter, INFO,
2940                     "info: %s: Marvell 802.11 Adapter\n", dev->name);
2941
2942 #ifdef CONFIG_DEBUG_FS
2943         mwifiex_dev_debugfs_init(priv);
2944 #endif
2945
2946         switch (type) {
2947         case NL80211_IFTYPE_UNSPECIFIED:
2948         case NL80211_IFTYPE_STATION:
2949         case NL80211_IFTYPE_ADHOC:
2950                 adapter->curr_iface_comb.sta_intf++;
2951                 break;
2952         case NL80211_IFTYPE_AP:
2953                 adapter->curr_iface_comb.uap_intf++;
2954                 break;
2955         case NL80211_IFTYPE_P2P_CLIENT:
2956                 adapter->curr_iface_comb.p2p_intf++;
2957                 break;
2958         default:
2959                 mwifiex_dbg(adapter, ERROR, "type not supported\n");
2960                 return ERR_PTR(-EINVAL);
2961         }
2962
2963         return &priv->wdev;
2964 }
2965 EXPORT_SYMBOL_GPL(mwifiex_add_virtual_intf);
2966
2967 /*
2968  * del_virtual_intf: remove the virtual interface determined by dev
2969  */
2970 int mwifiex_del_virtual_intf(struct wiphy *wiphy, struct wireless_dev *wdev)
2971 {
2972         struct mwifiex_private *priv = mwifiex_netdev_get_priv(wdev->netdev);
2973         struct mwifiex_adapter *adapter = priv->adapter;
2974         struct sk_buff *skb, *tmp;
2975
2976 #ifdef CONFIG_DEBUG_FS
2977         mwifiex_dev_debugfs_remove(priv);
2978 #endif
2979
2980         if (priv->sched_scanning)
2981                 priv->sched_scanning = false;
2982
2983         mwifiex_stop_net_dev_queue(priv->netdev, adapter);
2984
2985         skb_queue_walk_safe(&priv->bypass_txq, skb, tmp)
2986                 mwifiex_write_data_complete(priv->adapter, skb, 0, -1);
2987
2988         if (netif_carrier_ok(priv->netdev))
2989                 netif_carrier_off(priv->netdev);
2990
2991         if (wdev->netdev->reg_state == NETREG_REGISTERED)
2992                 unregister_netdevice(wdev->netdev);
2993
2994         if (priv->dfs_cac_workqueue) {
2995                 flush_workqueue(priv->dfs_cac_workqueue);
2996                 destroy_workqueue(priv->dfs_cac_workqueue);
2997                 priv->dfs_cac_workqueue = NULL;
2998         }
2999
3000         if (priv->dfs_chan_sw_workqueue) {
3001                 flush_workqueue(priv->dfs_chan_sw_workqueue);
3002                 destroy_workqueue(priv->dfs_chan_sw_workqueue);
3003                 priv->dfs_chan_sw_workqueue = NULL;
3004         }
3005         /* Clear the priv in adapter */
3006         priv->netdev->ieee80211_ptr = NULL;
3007         priv->netdev = NULL;
3008         priv->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
3009
3010         priv->media_connected = false;
3011
3012         switch (priv->bss_mode) {
3013         case NL80211_IFTYPE_UNSPECIFIED:
3014         case NL80211_IFTYPE_STATION:
3015         case NL80211_IFTYPE_ADHOC:
3016                 adapter->curr_iface_comb.sta_intf--;
3017                 break;
3018         case NL80211_IFTYPE_AP:
3019                 adapter->curr_iface_comb.uap_intf--;
3020                 break;
3021         case NL80211_IFTYPE_P2P_CLIENT:
3022         case NL80211_IFTYPE_P2P_GO:
3023                 adapter->curr_iface_comb.p2p_intf--;
3024                 break;
3025         default:
3026                 mwifiex_dbg(adapter, ERROR,
3027                             "del_virtual_intf: type not supported\n");
3028                 break;
3029         }
3030
3031         priv->bss_mode = NL80211_IFTYPE_UNSPECIFIED;
3032
3033         if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA ||
3034             GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP)
3035                 kfree(priv->hist_data);
3036
3037         return 0;
3038 }
3039 EXPORT_SYMBOL_GPL(mwifiex_del_virtual_intf);
3040
3041 static bool
3042 mwifiex_is_pattern_supported(struct cfg80211_pkt_pattern *pat, s8 *byte_seq,
3043                              u8 max_byte_seq)
3044 {
3045         int j, k, valid_byte_cnt = 0;
3046         bool dont_care_byte = false;
3047
3048         for (j = 0; j < DIV_ROUND_UP(pat->pattern_len, 8); j++) {
3049                 for (k = 0; k < 8; k++) {
3050                         if (pat->mask[j] & 1 << k) {
3051                                 memcpy(byte_seq + valid_byte_cnt,
3052                                        &pat->pattern[j * 8 + k], 1);
3053                                 valid_byte_cnt++;
3054                                 if (dont_care_byte)
3055                                         return false;
3056                         } else {
3057                                 if (valid_byte_cnt)
3058                                         dont_care_byte = true;
3059                         }
3060
3061                         /* wildcard bytes record as the offset
3062                          * before the valid byte
3063                          */
3064                         if (!valid_byte_cnt && !dont_care_byte)
3065                                 pat->pkt_offset++;
3066
3067                         if (valid_byte_cnt > max_byte_seq)
3068                                 return false;
3069                 }
3070         }
3071
3072         byte_seq[max_byte_seq] = valid_byte_cnt;
3073
3074         return true;
3075 }
3076
3077 #ifdef CONFIG_PM
3078 static void mwifiex_set_auto_arp_mef_entry(struct mwifiex_private *priv,
3079                                            struct mwifiex_mef_entry *mef_entry)
3080 {
3081         int i, filt_num = 0, num_ipv4 = 0;
3082         struct in_device *in_dev;
3083         struct in_ifaddr *ifa;
3084         __be32 ips[MWIFIEX_MAX_SUPPORTED_IPADDR];
3085         struct mwifiex_adapter *adapter = priv->adapter;
3086
3087         mef_entry->mode = MEF_MODE_HOST_SLEEP;
3088         mef_entry->action = MEF_ACTION_AUTO_ARP;
3089
3090         /* Enable ARP offload feature */
3091         memset(ips, 0, sizeof(ips));
3092         for (i = 0; i < MWIFIEX_MAX_BSS_NUM; i++) {
3093                 if (adapter->priv[i]->netdev) {
3094                         in_dev = __in_dev_get_rtnl(adapter->priv[i]->netdev);
3095                         if (!in_dev)
3096                                 continue;
3097                         ifa = in_dev->ifa_list;
3098                         if (!ifa || !ifa->ifa_local)
3099                                 continue;
3100                         ips[i] = ifa->ifa_local;
3101                         num_ipv4++;
3102                 }
3103         }
3104
3105         for (i = 0; i < num_ipv4; i++) {
3106                 if (!ips[i])
3107                         continue;
3108                 mef_entry->filter[filt_num].repeat = 1;
3109                 memcpy(mef_entry->filter[filt_num].byte_seq,
3110                        (u8 *)&ips[i], sizeof(ips[i]));
3111                 mef_entry->filter[filt_num].
3112                         byte_seq[MWIFIEX_MEF_MAX_BYTESEQ] =
3113                         sizeof(ips[i]);
3114                 mef_entry->filter[filt_num].offset = 46;
3115                 mef_entry->filter[filt_num].filt_type = TYPE_EQ;
3116                 if (filt_num) {
3117                         mef_entry->filter[filt_num].filt_action =
3118                                 TYPE_OR;
3119                 }
3120                 filt_num++;
3121         }
3122
3123         mef_entry->filter[filt_num].repeat = 1;
3124         mef_entry->filter[filt_num].byte_seq[0] = 0x08;
3125         mef_entry->filter[filt_num].byte_seq[1] = 0x06;
3126         mef_entry->filter[filt_num].byte_seq[MWIFIEX_MEF_MAX_BYTESEQ] = 2;
3127         mef_entry->filter[filt_num].offset = 20;
3128         mef_entry->filter[filt_num].filt_type = TYPE_EQ;
3129         mef_entry->filter[filt_num].filt_action = TYPE_AND;
3130 }
3131
3132 static int mwifiex_set_wowlan_mef_entry(struct mwifiex_private *priv,
3133                                         struct mwifiex_ds_mef_cfg *mef_cfg,
3134                                         struct mwifiex_mef_entry *mef_entry,
3135                                         struct cfg80211_wowlan *wowlan)
3136 {
3137         int i, filt_num = 0, ret = 0;
3138         bool first_pat = true;
3139         u8 byte_seq[MWIFIEX_MEF_MAX_BYTESEQ + 1];
3140         const u8 ipv4_mc_mac[] = {0x33, 0x33};
3141         const u8 ipv6_mc_mac[] = {0x01, 0x00, 0x5e};
3142
3143         mef_entry->mode = MEF_MODE_HOST_SLEEP;
3144         mef_entry->action = MEF_ACTION_ALLOW_AND_WAKEUP_HOST;
3145
3146         for (i = 0; i < wowlan->n_patterns; i++) {
3147                 memset(byte_seq, 0, sizeof(byte_seq));
3148                 if (!mwifiex_is_pattern_supported(&wowlan->patterns[i],
3149                                         byte_seq,
3150                                         MWIFIEX_MEF_MAX_BYTESEQ)) {
3151                         mwifiex_dbg(priv->adapter, ERROR,
3152                                     "Pattern not supported\n");
3153                         return -EOPNOTSUPP;
3154                 }
3155
3156                 if (!wowlan->patterns[i].pkt_offset) {
3157                         if (!(byte_seq[0] & 0x01) &&
3158                             (byte_seq[MWIFIEX_MEF_MAX_BYTESEQ] == 1)) {
3159                                 mef_cfg->criteria |= MWIFIEX_CRITERIA_UNICAST;
3160                                 continue;
3161                         } else if (is_broadcast_ether_addr(byte_seq)) {
3162                                 mef_cfg->criteria |= MWIFIEX_CRITERIA_BROADCAST;
3163                                 continue;
3164                         } else if ((!memcmp(byte_seq, ipv4_mc_mac, 2) &&
3165                                     (byte_seq[MWIFIEX_MEF_MAX_BYTESEQ] == 2)) ||
3166                                    (!memcmp(byte_seq, ipv6_mc_mac, 3) &&
3167                                     (byte_seq[MWIFIEX_MEF_MAX_BYTESEQ] == 3))) {
3168                                 mef_cfg->criteria |= MWIFIEX_CRITERIA_MULTICAST;
3169                                 continue;
3170                         }
3171                 }
3172                 mef_entry->filter[filt_num].repeat = 1;
3173                 mef_entry->filter[filt_num].offset =
3174                         wowlan->patterns[i].pkt_offset;
3175                 memcpy(mef_entry->filter[filt_num].byte_seq, byte_seq,
3176                                 sizeof(byte_seq));
3177                 mef_entry->filter[filt_num].filt_type = TYPE_EQ;
3178
3179                 if (first_pat) {
3180                         first_pat = false;
3181                         mwifiex_dbg(priv->adapter, INFO, "Wake on patterns\n");
3182                 } else {
3183                         mef_entry->filter[filt_num].filt_action = TYPE_AND;
3184                 }
3185
3186                 filt_num++;
3187         }
3188
3189         if (wowlan->magic_pkt) {
3190                 mef_cfg->criteria |= MWIFIEX_CRITERIA_UNICAST;
3191                 mef_entry->filter[filt_num].repeat = 16;
3192                 memcpy(mef_entry->filter[filt_num].byte_seq, priv->curr_addr,
3193                                 ETH_ALEN);
3194                 mef_entry->filter[filt_num].byte_seq[MWIFIEX_MEF_MAX_BYTESEQ] =
3195                         ETH_ALEN;
3196                 mef_entry->filter[filt_num].offset = 28;
3197                 mef_entry->filter[filt_num].filt_type = TYPE_EQ;
3198                 if (filt_num)
3199                         mef_entry->filter[filt_num].filt_action = TYPE_OR;
3200
3201                 filt_num++;
3202                 mef_entry->filter[filt_num].repeat = 16;
3203                 memcpy(mef_entry->filter[filt_num].byte_seq, priv->curr_addr,
3204                                 ETH_ALEN);
3205                 mef_entry->filter[filt_num].byte_seq[MWIFIEX_MEF_MAX_BYTESEQ] =
3206                         ETH_ALEN;
3207                 mef_entry->filter[filt_num].offset = 56;
3208                 mef_entry->filter[filt_num].filt_type = TYPE_EQ;
3209                 mef_entry->filter[filt_num].filt_action = TYPE_OR;
3210                 mwifiex_dbg(priv->adapter, INFO, "Wake on magic packet\n");
3211         }
3212         return ret;
3213 }
3214
3215 static int mwifiex_set_mef_filter(struct mwifiex_private *priv,
3216                                   struct cfg80211_wowlan *wowlan)
3217 {
3218         int ret = 0, num_entries = 1;
3219         struct mwifiex_ds_mef_cfg mef_cfg;
3220         struct mwifiex_mef_entry *mef_entry;
3221
3222         if (wowlan->n_patterns || wowlan->magic_pkt)
3223                 num_entries++;
3224
3225         mef_entry = kcalloc(num_entries, sizeof(*mef_entry), GFP_KERNEL);
3226         if (!mef_entry)
3227                 return -ENOMEM;
3228
3229         memset(&mef_cfg, 0, sizeof(mef_cfg));
3230         mef_cfg.criteria |= MWIFIEX_CRITERIA_BROADCAST |
3231                 MWIFIEX_CRITERIA_UNICAST;
3232         mef_cfg.num_entries = num_entries;
3233         mef_cfg.mef_entry = mef_entry;
3234
3235         mwifiex_set_auto_arp_mef_entry(priv, &mef_entry[0]);
3236
3237         if (wowlan->n_patterns || wowlan->magic_pkt) {
3238                 ret = mwifiex_set_wowlan_mef_entry(priv, &mef_cfg,
3239                                                    &mef_entry[1], wowlan);
3240                 if (ret)
3241                         goto err;
3242         }
3243
3244         if (!mef_cfg.criteria)
3245                 mef_cfg.criteria = MWIFIEX_CRITERIA_BROADCAST |
3246                         MWIFIEX_CRITERIA_UNICAST |
3247                         MWIFIEX_CRITERIA_MULTICAST;
3248
3249         ret = mwifiex_send_cmd(priv, HostCmd_CMD_MEF_CFG,
3250                         HostCmd_ACT_GEN_SET, 0,
3251                         &mef_cfg, true);
3252
3253 err:
3254         kfree(mef_entry);
3255         return ret;
3256 }
3257
3258 static int mwifiex_cfg80211_suspend(struct wiphy *wiphy,
3259                                     struct cfg80211_wowlan *wowlan)
3260 {
3261         struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
3262         struct mwifiex_ds_hs_cfg hs_cfg;
3263         int i, ret = 0, retry_num = 10;
3264         struct mwifiex_private *priv;
3265
3266         for (i = 0; i < adapter->priv_num; i++) {
3267                 priv = adapter->priv[i];
3268                 mwifiex_abort_cac(priv);
3269         }
3270
3271         mwifiex_cancel_all_pending_cmd(adapter);
3272
3273         for (i = 0; i < adapter->priv_num; i++) {
3274                 priv = adapter->priv[i];
3275                 if (priv && priv->netdev) {
3276                         mwifiex_stop_net_dev_queue(priv->netdev, adapter);
3277                         if (netif_carrier_ok(priv->netdev))
3278                                 netif_carrier_off(priv->netdev);
3279                 }
3280         }
3281
3282         for (i = 0; i < retry_num; i++) {
3283                 if (!mwifiex_wmm_lists_empty(adapter) ||
3284                     !mwifiex_bypass_txlist_empty(adapter) ||
3285                     !skb_queue_empty(&adapter->tx_data_q))
3286                         usleep_range(10000, 15000);
3287                 else
3288                         break;
3289         }
3290
3291         if (!wowlan) {
3292                 mwifiex_dbg(adapter, ERROR,
3293                             "None of the WOWLAN triggers enabled\n");
3294                 return 0;
3295         }
3296
3297         priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA);
3298
3299         if (!priv->media_connected && !wowlan->nd_config) {
3300                 mwifiex_dbg(adapter, ERROR,
3301                             "Can not configure WOWLAN in disconnected state\n");
3302                 return 0;
3303         }
3304
3305         ret = mwifiex_set_mef_filter(priv, wowlan);
3306         if (ret) {
3307                 mwifiex_dbg(adapter, ERROR, "Failed to set MEF filter\n");
3308                 return ret;
3309         }
3310
3311         memset(&hs_cfg, 0, sizeof(hs_cfg));
3312         hs_cfg.conditions = le32_to_cpu(adapter->hs_cfg.conditions);
3313
3314         if (wowlan->nd_config) {
3315                 mwifiex_dbg(adapter, INFO, "Wake on net detect\n");
3316                 hs_cfg.conditions |= HS_CFG_COND_MAC_EVENT;
3317                 mwifiex_cfg80211_sched_scan_start(wiphy, priv->netdev,
3318                                                   wowlan->nd_config);
3319         }
3320
3321         if (wowlan->disconnect) {
3322                 hs_cfg.conditions |= HS_CFG_COND_MAC_EVENT;
3323                 mwifiex_dbg(priv->adapter, INFO, "Wake on device disconnect\n");
3324         }
3325
3326         hs_cfg.is_invoke_hostcmd = false;
3327         hs_cfg.gpio = adapter->hs_cfg.gpio;
3328         hs_cfg.gap = adapter->hs_cfg.gap;
3329         ret = mwifiex_set_hs_params(priv, HostCmd_ACT_GEN_SET,
3330                                     MWIFIEX_SYNC_CMD, &hs_cfg);
3331         if (ret) {
3332                 mwifiex_dbg(adapter, ERROR,
3333                             "Failed to set HS params\n");
3334                 return ret;
3335         }
3336
3337         return ret;
3338 }
3339
3340 static int mwifiex_cfg80211_resume(struct wiphy *wiphy)
3341 {
3342         struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
3343         struct mwifiex_private *priv;
3344         struct mwifiex_ds_wakeup_reason wakeup_reason;
3345         struct cfg80211_wowlan_wakeup wakeup_report;
3346         int i;
3347
3348         for (i = 0; i < adapter->priv_num; i++) {
3349                 priv = adapter->priv[i];
3350                 if (priv && priv->netdev) {
3351                         if (!netif_carrier_ok(priv->netdev))
3352                                 netif_carrier_on(priv->netdev);
3353                         mwifiex_wake_up_net_dev_queue(priv->netdev, adapter);
3354                 }
3355         }
3356
3357         priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA);
3358         mwifiex_get_wakeup_reason(priv, HostCmd_ACT_GEN_GET, MWIFIEX_SYNC_CMD,
3359                                   &wakeup_reason);
3360         memset(&wakeup_report, 0, sizeof(struct cfg80211_wowlan_wakeup));
3361
3362         wakeup_report.pattern_idx = -1;
3363
3364         switch (wakeup_reason.hs_wakeup_reason) {
3365         case NO_HSWAKEUP_REASON:
3366                 break;
3367         case BCAST_DATA_MATCHED:
3368                 break;
3369         case MCAST_DATA_MATCHED:
3370                 break;
3371         case UCAST_DATA_MATCHED:
3372                 break;
3373         case MASKTABLE_EVENT_MATCHED:
3374                 break;
3375         case NON_MASKABLE_EVENT_MATCHED:
3376                 if (wiphy->wowlan_config->disconnect)
3377                         wakeup_report.disconnect = true;
3378                 if (wiphy->wowlan_config->nd_config)
3379                         wakeup_report.net_detect = adapter->nd_info;
3380                 break;
3381         case NON_MASKABLE_CONDITION_MATCHED:
3382                 break;
3383         case MAGIC_PATTERN_MATCHED:
3384                 if (wiphy->wowlan_config->magic_pkt)
3385                         wakeup_report.magic_pkt = true;
3386                 if (wiphy->wowlan_config->n_patterns)
3387                         wakeup_report.pattern_idx = 1;
3388                 break;
3389         case CONTROL_FRAME_MATCHED:
3390                 break;
3391         case    MANAGEMENT_FRAME_MATCHED:
3392                 break;
3393         case GTK_REKEY_FAILURE:
3394                 if (wiphy->wowlan_config->gtk_rekey_failure)
3395                         wakeup_report.gtk_rekey_failure = true;
3396                 break;
3397         default:
3398                 break;
3399         }
3400
3401         if ((wakeup_reason.hs_wakeup_reason > 0) &&
3402             (wakeup_reason.hs_wakeup_reason <= 7))
3403                 cfg80211_report_wowlan_wakeup(&priv->wdev, &wakeup_report,
3404                                               GFP_KERNEL);
3405
3406         if (adapter->nd_info) {
3407                 for (i = 0 ; i < adapter->nd_info->n_matches ; i++)
3408                         kfree(adapter->nd_info->matches[i]);
3409                 kfree(adapter->nd_info);
3410                 adapter->nd_info = NULL;
3411         }
3412
3413         return 0;
3414 }
3415
3416 static void mwifiex_cfg80211_set_wakeup(struct wiphy *wiphy,
3417                                        bool enabled)
3418 {
3419         struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
3420
3421         device_set_wakeup_enable(adapter->dev, enabled);
3422 }
3423
3424 static int mwifiex_set_rekey_data(struct wiphy *wiphy, struct net_device *dev,
3425                                   struct cfg80211_gtk_rekey_data *data)
3426 {
3427         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
3428
3429         return mwifiex_send_cmd(priv, HostCmd_CMD_GTK_REKEY_OFFLOAD_CFG,
3430                                 HostCmd_ACT_GEN_SET, 0, data, true);
3431 }
3432
3433 #endif
3434
3435 static int mwifiex_get_coalesce_pkt_type(u8 *byte_seq)
3436 {
3437         const u8 ipv4_mc_mac[] = {0x33, 0x33};
3438         const u8 ipv6_mc_mac[] = {0x01, 0x00, 0x5e};
3439         const u8 bc_mac[] = {0xff, 0xff, 0xff, 0xff};
3440
3441         if ((byte_seq[0] & 0x01) &&
3442             (byte_seq[MWIFIEX_COALESCE_MAX_BYTESEQ] == 1))
3443                 return PACKET_TYPE_UNICAST;
3444         else if (!memcmp(byte_seq, bc_mac, 4))
3445                 return PACKET_TYPE_BROADCAST;
3446         else if ((!memcmp(byte_seq, ipv4_mc_mac, 2) &&
3447                   byte_seq[MWIFIEX_COALESCE_MAX_BYTESEQ] == 2) ||
3448                  (!memcmp(byte_seq, ipv6_mc_mac, 3) &&
3449                   byte_seq[MWIFIEX_COALESCE_MAX_BYTESEQ] == 3))
3450                 return PACKET_TYPE_MULTICAST;
3451
3452         return 0;
3453 }
3454
3455 static int
3456 mwifiex_fill_coalesce_rule_info(struct mwifiex_private *priv,
3457                                 struct cfg80211_coalesce_rules *crule,
3458                                 struct mwifiex_coalesce_rule *mrule)
3459 {
3460         u8 byte_seq[MWIFIEX_COALESCE_MAX_BYTESEQ + 1];
3461         struct filt_field_param *param;
3462         int i;
3463
3464         mrule->max_coalescing_delay = crule->delay;
3465
3466         param = mrule->params;
3467
3468         for (i = 0; i < crule->n_patterns; i++) {
3469                 memset(byte_seq, 0, sizeof(byte_seq));
3470                 if (!mwifiex_is_pattern_supported(&crule->patterns[i],
3471                                                   byte_seq,
3472                                                 MWIFIEX_COALESCE_MAX_BYTESEQ)) {
3473                         mwifiex_dbg(priv->adapter, ERROR,
3474                                     "Pattern not supported\n");
3475                         return -EOPNOTSUPP;
3476                 }
3477
3478                 if (!crule->patterns[i].pkt_offset) {
3479                         u8 pkt_type;
3480
3481                         pkt_type = mwifiex_get_coalesce_pkt_type(byte_seq);
3482                         if (pkt_type && mrule->pkt_type) {
3483                                 mwifiex_dbg(priv->adapter, ERROR,
3484                                             "Multiple packet types not allowed\n");
3485                                 return -EOPNOTSUPP;
3486                         } else if (pkt_type) {
3487                                 mrule->pkt_type = pkt_type;
3488                                 continue;
3489                         }
3490                 }
3491
3492                 if (crule->condition == NL80211_COALESCE_CONDITION_MATCH)
3493                         param->operation = RECV_FILTER_MATCH_TYPE_EQ;
3494                 else
3495                         param->operation = RECV_FILTER_MATCH_TYPE_NE;
3496
3497                 param->operand_len = byte_seq[MWIFIEX_COALESCE_MAX_BYTESEQ];
3498                 memcpy(param->operand_byte_stream, byte_seq,
3499                        param->operand_len);
3500                 param->offset = crule->patterns[i].pkt_offset;
3501                 param++;
3502
3503                 mrule->num_of_fields++;
3504         }
3505
3506         if (!mrule->pkt_type) {
3507                 mwifiex_dbg(priv->adapter, ERROR,
3508                             "Packet type can not be determined\n");
3509                 return -EOPNOTSUPP;
3510         }
3511
3512         return 0;
3513 }
3514
3515 static int mwifiex_cfg80211_set_coalesce(struct wiphy *wiphy,
3516                                          struct cfg80211_coalesce *coalesce)
3517 {
3518         struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
3519         int i, ret;
3520         struct mwifiex_ds_coalesce_cfg coalesce_cfg;
3521         struct mwifiex_private *priv =
3522                         mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA);
3523
3524         memset(&coalesce_cfg, 0, sizeof(coalesce_cfg));
3525         if (!coalesce) {
3526                 mwifiex_dbg(adapter, WARN,
3527                             "Disable coalesce and reset all previous rules\n");
3528                 return mwifiex_send_cmd(priv, HostCmd_CMD_COALESCE_CFG,
3529                                         HostCmd_ACT_GEN_SET, 0,
3530                                         &coalesce_cfg, true);
3531         }
3532
3533         coalesce_cfg.num_of_rules = coalesce->n_rules;
3534         for (i = 0; i < coalesce->n_rules; i++) {
3535                 ret = mwifiex_fill_coalesce_rule_info(priv, &coalesce->rules[i],
3536                                                       &coalesce_cfg.rule[i]);
3537                 if (ret) {
3538                         mwifiex_dbg(adapter, ERROR,
3539                                     "Recheck the patterns provided for rule %d\n",
3540                                 i + 1);
3541                         return ret;
3542                 }
3543         }
3544
3545         return mwifiex_send_cmd(priv, HostCmd_CMD_COALESCE_CFG,
3546                                 HostCmd_ACT_GEN_SET, 0, &coalesce_cfg, true);
3547 }
3548
3549 /* cfg80211 ops handler for tdls_mgmt.
3550  * Function prepares TDLS action frame packets and forwards them to FW
3551  */
3552 static int
3553 mwifiex_cfg80211_tdls_mgmt(struct wiphy *wiphy, struct net_device *dev,
3554                            const u8 *peer, u8 action_code, u8 dialog_token,
3555                            u16 status_code, u32 peer_capability,
3556                            bool initiator, const u8 *extra_ies,
3557                            size_t extra_ies_len)
3558 {
3559         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
3560         int ret;
3561
3562         if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
3563                 return -ENOTSUPP;
3564
3565         /* make sure we are in station mode and connected */
3566         if (!(priv->bss_type == MWIFIEX_BSS_TYPE_STA && priv->media_connected))
3567                 return -ENOTSUPP;
3568
3569         switch (action_code) {
3570         case WLAN_TDLS_SETUP_REQUEST:
3571                 mwifiex_dbg(priv->adapter, MSG,
3572                             "Send TDLS Setup Request to %pM status_code=%d\n",
3573                             peer, status_code);
3574                 mwifiex_add_auto_tdls_peer(priv, peer);
3575                 ret = mwifiex_send_tdls_data_frame(priv, peer, action_code,
3576                                                    dialog_token, status_code,
3577                                                    extra_ies, extra_ies_len);
3578                 break;
3579         case WLAN_TDLS_SETUP_RESPONSE:
3580                 mwifiex_add_auto_tdls_peer(priv, peer);
3581                 mwifiex_dbg(priv->adapter, MSG,
3582                             "Send TDLS Setup Response to %pM status_code=%d\n",
3583                             peer, status_code);
3584                 ret = mwifiex_send_tdls_data_frame(priv, peer, action_code,
3585                                                    dialog_token, status_code,
3586                                                    extra_ies, extra_ies_len);
3587                 break;
3588         case WLAN_TDLS_SETUP_CONFIRM:
3589                 mwifiex_dbg(priv->adapter, MSG,
3590                             "Send TDLS Confirm to %pM status_code=%d\n", peer,
3591                             status_code);
3592                 ret = mwifiex_send_tdls_data_frame(priv, peer, action_code,
3593                                                    dialog_token, status_code,
3594                                                    extra_ies, extra_ies_len);
3595                 break;
3596         case WLAN_TDLS_TEARDOWN:
3597                 mwifiex_dbg(priv->adapter, MSG,
3598                             "Send TDLS Tear down to %pM\n", peer);
3599                 ret = mwifiex_send_tdls_data_frame(priv, peer, action_code,
3600                                                    dialog_token, status_code,
3601                                                    extra_ies, extra_ies_len);
3602                 break;
3603         case WLAN_TDLS_DISCOVERY_REQUEST:
3604                 mwifiex_dbg(priv->adapter, MSG,
3605                             "Send TDLS Discovery Request to %pM\n", peer);
3606                 ret = mwifiex_send_tdls_data_frame(priv, peer, action_code,
3607                                                    dialog_token, status_code,
3608                                                    extra_ies, extra_ies_len);
3609                 break;
3610         case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
3611                 mwifiex_dbg(priv->adapter, MSG,
3612                             "Send TDLS Discovery Response to %pM\n", peer);
3613                 ret = mwifiex_send_tdls_action_frame(priv, peer, action_code,
3614                                                    dialog_token, status_code,
3615                                                    extra_ies, extra_ies_len);
3616                 break;
3617         default:
3618                 mwifiex_dbg(priv->adapter, ERROR,
3619                             "Unknown TDLS mgmt/action frame %pM\n", peer);
3620                 ret = -EINVAL;
3621                 break;
3622         }
3623
3624         return ret;
3625 }
3626
3627 static int
3628 mwifiex_cfg80211_tdls_oper(struct wiphy *wiphy, struct net_device *dev,
3629                            const u8 *peer, enum nl80211_tdls_operation action)
3630 {
3631         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
3632
3633         if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
3634             !(wiphy->flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
3635                 return -ENOTSUPP;
3636
3637         /* make sure we are in station mode and connected */
3638         if (!(priv->bss_type == MWIFIEX_BSS_TYPE_STA && priv->media_connected))
3639                 return -ENOTSUPP;
3640
3641         mwifiex_dbg(priv->adapter, MSG,
3642                     "TDLS peer=%pM, oper=%d\n", peer, action);
3643
3644         switch (action) {
3645         case NL80211_TDLS_ENABLE_LINK:
3646                 action = MWIFIEX_TDLS_ENABLE_LINK;
3647                 break;
3648         case NL80211_TDLS_DISABLE_LINK:
3649                 action = MWIFIEX_TDLS_DISABLE_LINK;
3650                 break;
3651         case NL80211_TDLS_TEARDOWN:
3652                 /* shouldn't happen!*/
3653                 mwifiex_dbg(priv->adapter, ERROR,
3654                             "tdls_oper: teardown from driver not supported\n");
3655                 return -EINVAL;
3656         case NL80211_TDLS_SETUP:
3657                 /* shouldn't happen!*/
3658                 mwifiex_dbg(priv->adapter, ERROR,
3659                             "tdls_oper: setup from driver not supported\n");
3660                 return -EINVAL;
3661         case NL80211_TDLS_DISCOVERY_REQ:
3662                 /* shouldn't happen!*/
3663                 mwifiex_dbg(priv->adapter, ERROR,
3664                             "tdls_oper: discovery from driver not supported\n");
3665                 return -EINVAL;
3666         default:
3667                 mwifiex_dbg(priv->adapter, ERROR,
3668                             "tdls_oper: operation not supported\n");
3669                 return -ENOTSUPP;
3670         }
3671
3672         return mwifiex_tdls_oper(priv, peer, action);
3673 }
3674
3675 static int
3676 mwifiex_cfg80211_tdls_chan_switch(struct wiphy *wiphy, struct net_device *dev,
3677                                   const u8 *addr, u8 oper_class,
3678                                   struct cfg80211_chan_def *chandef)
3679 {
3680         struct mwifiex_sta_node *sta_ptr;
3681         unsigned long flags;
3682         u16 chan;
3683         u8 second_chan_offset, band;
3684         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
3685
3686         spin_lock_irqsave(&priv->sta_list_spinlock, flags);
3687         sta_ptr = mwifiex_get_sta_entry(priv, addr);
3688         spin_unlock_irqrestore(&priv->sta_list_spinlock, flags);
3689
3690         if (!sta_ptr) {
3691                 wiphy_err(wiphy, "%s: Invalid TDLS peer %pM\n",
3692                           __func__, addr);
3693                 return -ENOENT;
3694         }
3695
3696         if (!(sta_ptr->tdls_cap.extcap.ext_capab[3] &
3697               WLAN_EXT_CAPA4_TDLS_CHAN_SWITCH)) {
3698                 wiphy_err(wiphy, "%pM do not support tdls cs\n", addr);
3699                 return -ENOENT;
3700         }
3701
3702         if (sta_ptr->tdls_status == TDLS_CHAN_SWITCHING ||
3703             sta_ptr->tdls_status == TDLS_IN_OFF_CHAN) {
3704                 wiphy_err(wiphy, "channel switch is running, abort request\n");
3705                 return -EALREADY;
3706         }
3707
3708         chan = chandef->chan->hw_value;
3709         second_chan_offset = mwifiex_get_sec_chan_offset(chan);
3710         band = chandef->chan->band;
3711         mwifiex_start_tdls_cs(priv, addr, chan, second_chan_offset, band);
3712
3713         return 0;
3714 }
3715
3716 static void
3717 mwifiex_cfg80211_tdls_cancel_chan_switch(struct wiphy *wiphy,
3718                                          struct net_device *dev,
3719                                          const u8 *addr)
3720 {
3721         struct mwifiex_sta_node *sta_ptr;
3722         unsigned long flags;
3723         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
3724
3725         spin_lock_irqsave(&priv->sta_list_spinlock, flags);
3726         sta_ptr = mwifiex_get_sta_entry(priv, addr);
3727         spin_unlock_irqrestore(&priv->sta_list_spinlock, flags);
3728
3729         if (!sta_ptr) {
3730                 wiphy_err(wiphy, "%s: Invalid TDLS peer %pM\n",
3731                           __func__, addr);
3732         } else if (!(sta_ptr->tdls_status == TDLS_CHAN_SWITCHING ||
3733                      sta_ptr->tdls_status == TDLS_IN_BASE_CHAN ||
3734                      sta_ptr->tdls_status == TDLS_IN_OFF_CHAN)) {
3735                 wiphy_err(wiphy, "tdls chan switch not initialize by %pM\n",
3736                           addr);
3737         } else
3738                 mwifiex_stop_tdls_cs(priv, addr);
3739 }
3740
3741 static int
3742 mwifiex_cfg80211_add_station(struct wiphy *wiphy, struct net_device *dev,
3743                              const u8 *mac, struct station_parameters *params)
3744 {
3745         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
3746
3747         if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3748                 return -ENOTSUPP;
3749
3750         /* make sure we are in station mode and connected */
3751         if ((priv->bss_type != MWIFIEX_BSS_TYPE_STA) || !priv->media_connected)
3752                 return -ENOTSUPP;
3753
3754         return mwifiex_tdls_oper(priv, mac, MWIFIEX_TDLS_CREATE_LINK);
3755 }
3756
3757 static int
3758 mwifiex_cfg80211_channel_switch(struct wiphy *wiphy, struct net_device *dev,
3759                                 struct cfg80211_csa_settings *params)
3760 {
3761         struct ieee_types_header *chsw_ie;
3762         struct ieee80211_channel_sw_ie *channel_sw;
3763         int chsw_msec;
3764         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
3765
3766         if (priv->adapter->scan_processing) {
3767                 mwifiex_dbg(priv->adapter, ERROR,
3768                             "radar detection: scan in process...\n");
3769                 return -EBUSY;
3770         }
3771
3772         if (priv->wdev.cac_started)
3773                 return -EBUSY;
3774
3775         if (cfg80211_chandef_identical(&params->chandef,
3776                                        &priv->dfs_chandef))
3777                 return -EINVAL;
3778
3779         chsw_ie = (void *)cfg80211_find_ie(WLAN_EID_CHANNEL_SWITCH,
3780                                            params->beacon_csa.tail,
3781                                            params->beacon_csa.tail_len);
3782         if (!chsw_ie) {
3783                 mwifiex_dbg(priv->adapter, ERROR,
3784                             "Could not parse channel switch announcement IE\n");
3785                 return -EINVAL;
3786         }
3787
3788         channel_sw = (void *)(chsw_ie + 1);
3789         if (channel_sw->mode) {
3790                 if (netif_carrier_ok(priv->netdev))
3791                         netif_carrier_off(priv->netdev);
3792                 mwifiex_stop_net_dev_queue(priv->netdev, priv->adapter);
3793         }
3794
3795         if (mwifiex_del_mgmt_ies(priv))
3796                 mwifiex_dbg(priv->adapter, ERROR,
3797                             "Failed to delete mgmt IEs!\n");
3798
3799         if (mwifiex_set_mgmt_ies(priv, &params->beacon_csa)) {
3800                 mwifiex_dbg(priv->adapter, ERROR,
3801                             "%s: setting mgmt ies failed\n", __func__);
3802                 return -EFAULT;
3803         }
3804
3805         memcpy(&priv->dfs_chandef, &params->chandef, sizeof(priv->dfs_chandef));
3806         memcpy(&priv->beacon_after, &params->beacon_after,
3807                sizeof(priv->beacon_after));
3808
3809         chsw_msec = max(channel_sw->count * priv->bss_cfg.beacon_period, 100);
3810         queue_delayed_work(priv->dfs_chan_sw_workqueue, &priv->dfs_chan_sw_work,
3811                            msecs_to_jiffies(chsw_msec));
3812         return 0;
3813 }
3814
3815 static int mwifiex_cfg80211_get_channel(struct wiphy *wiphy,
3816                                         struct wireless_dev *wdev,
3817                                         struct cfg80211_chan_def *chandef)
3818 {
3819         struct mwifiex_private *priv = mwifiex_netdev_get_priv(wdev->netdev);
3820         struct mwifiex_bssdescriptor *curr_bss;
3821         struct ieee80211_channel *chan;
3822         u8 second_chan_offset;
3823         enum nl80211_channel_type chan_type;
3824         enum nl80211_band band;
3825         int freq;
3826         int ret = -ENODATA;
3827
3828         if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP &&
3829             cfg80211_chandef_valid(&priv->bss_chandef)) {
3830                 *chandef = priv->bss_chandef;
3831                 ret = 0;
3832         } else if (priv->media_connected) {
3833                 curr_bss = &priv->curr_bss_params.bss_descriptor;
3834                 band = mwifiex_band_to_radio_type(priv->curr_bss_params.band);
3835                 freq = ieee80211_channel_to_frequency(curr_bss->channel, band);
3836                 chan = ieee80211_get_channel(wiphy, freq);
3837
3838                 if (priv->ht_param_present) {
3839                         second_chan_offset = priv->assoc_resp_ht_param &
3840                                         IEEE80211_HT_PARAM_CHA_SEC_OFFSET;
3841                         chan_type = mwifiex_sec_chan_offset_to_chan_type
3842                                                         (second_chan_offset);
3843                         cfg80211_chandef_create(chandef, chan, chan_type);
3844                 } else {
3845                         cfg80211_chandef_create(chandef, chan,
3846                                                 NL80211_CHAN_NO_HT);
3847                 }
3848                 ret = 0;
3849         }
3850
3851         return ret;
3852 }
3853
3854 static int
3855 mwifiex_cfg80211_start_radar_detection(struct wiphy *wiphy,
3856                                        struct net_device *dev,
3857                                        struct cfg80211_chan_def *chandef,
3858                                        u32 cac_time_ms)
3859 {
3860         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
3861         struct mwifiex_radar_params radar_params;
3862
3863         if (priv->adapter->scan_processing) {
3864                 mwifiex_dbg(priv->adapter, ERROR,
3865                             "radar detection: scan already in process...\n");
3866                 return -EBUSY;
3867         }
3868
3869         if (!mwifiex_is_11h_active(priv)) {
3870                 mwifiex_dbg(priv->adapter, INFO,
3871                             "Enable 11h extensions in FW\n");
3872                 if (mwifiex_11h_activate(priv, true)) {
3873                         mwifiex_dbg(priv->adapter, ERROR,
3874                                     "Failed to activate 11h extensions!!");
3875                         return -1;
3876                 }
3877                 priv->state_11h.is_11h_active = true;
3878         }
3879
3880         memset(&radar_params, 0, sizeof(struct mwifiex_radar_params));
3881         radar_params.chandef = chandef;
3882         radar_params.cac_time_ms = cac_time_ms;
3883
3884         memcpy(&priv->dfs_chandef, chandef, sizeof(priv->dfs_chandef));
3885
3886         if (mwifiex_send_cmd(priv, HostCmd_CMD_CHAN_REPORT_REQUEST,
3887                              HostCmd_ACT_GEN_SET, 0, &radar_params, true))
3888                 return -1;
3889
3890         queue_delayed_work(priv->dfs_cac_workqueue, &priv->dfs_cac_work,
3891                            msecs_to_jiffies(cac_time_ms));
3892         return 0;
3893 }
3894
3895 static int
3896 mwifiex_cfg80211_change_station(struct wiphy *wiphy, struct net_device *dev,
3897                                 const u8 *mac,
3898                                 struct station_parameters *params)
3899 {
3900         int ret;
3901         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
3902
3903         /* we support change_station handler only for TDLS peers*/
3904         if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3905                 return -ENOTSUPP;
3906
3907         /* make sure we are in station mode and connected */
3908         if ((priv->bss_type != MWIFIEX_BSS_TYPE_STA) || !priv->media_connected)
3909                 return -ENOTSUPP;
3910
3911         priv->sta_params = params;
3912
3913         ret = mwifiex_tdls_oper(priv, mac, MWIFIEX_TDLS_CONFIG_LINK);
3914         priv->sta_params = NULL;
3915
3916         return ret;
3917 }
3918
3919 /* station cfg80211 operations */
3920 static struct cfg80211_ops mwifiex_cfg80211_ops = {
3921         .add_virtual_intf = mwifiex_add_virtual_intf,
3922         .del_virtual_intf = mwifiex_del_virtual_intf,
3923         .change_virtual_intf = mwifiex_cfg80211_change_virtual_intf,
3924         .scan = mwifiex_cfg80211_scan,
3925         .connect = mwifiex_cfg80211_connect,
3926         .disconnect = mwifiex_cfg80211_disconnect,
3927         .get_station = mwifiex_cfg80211_get_station,
3928         .dump_station = mwifiex_cfg80211_dump_station,
3929         .dump_survey = mwifiex_cfg80211_dump_survey,
3930         .set_wiphy_params = mwifiex_cfg80211_set_wiphy_params,
3931         .join_ibss = mwifiex_cfg80211_join_ibss,
3932         .leave_ibss = mwifiex_cfg80211_leave_ibss,
3933         .add_key = mwifiex_cfg80211_add_key,
3934         .del_key = mwifiex_cfg80211_del_key,
3935         .mgmt_tx = mwifiex_cfg80211_mgmt_tx,
3936         .mgmt_frame_register = mwifiex_cfg80211_mgmt_frame_register,
3937         .remain_on_channel = mwifiex_cfg80211_remain_on_channel,
3938         .cancel_remain_on_channel = mwifiex_cfg80211_cancel_remain_on_channel,
3939         .set_default_key = mwifiex_cfg80211_set_default_key,
3940         .set_power_mgmt = mwifiex_cfg80211_set_power_mgmt,
3941         .set_tx_power = mwifiex_cfg80211_set_tx_power,
3942         .set_bitrate_mask = mwifiex_cfg80211_set_bitrate_mask,
3943         .start_ap = mwifiex_cfg80211_start_ap,
3944         .stop_ap = mwifiex_cfg80211_stop_ap,
3945         .change_beacon = mwifiex_cfg80211_change_beacon,
3946         .set_cqm_rssi_config = mwifiex_cfg80211_set_cqm_rssi_config,
3947         .set_antenna = mwifiex_cfg80211_set_antenna,
3948         .del_station = mwifiex_cfg80211_del_station,
3949         .sched_scan_start = mwifiex_cfg80211_sched_scan_start,
3950         .sched_scan_stop = mwifiex_cfg80211_sched_scan_stop,
3951 #ifdef CONFIG_PM
3952         .suspend = mwifiex_cfg80211_suspend,
3953         .resume = mwifiex_cfg80211_resume,
3954         .set_wakeup = mwifiex_cfg80211_set_wakeup,
3955         .set_rekey_data = mwifiex_set_rekey_data,
3956 #endif
3957         .set_coalesce = mwifiex_cfg80211_set_coalesce,
3958         .tdls_mgmt = mwifiex_cfg80211_tdls_mgmt,
3959         .tdls_oper = mwifiex_cfg80211_tdls_oper,
3960         .tdls_channel_switch = mwifiex_cfg80211_tdls_chan_switch,
3961         .tdls_cancel_channel_switch = mwifiex_cfg80211_tdls_cancel_chan_switch,
3962         .add_station = mwifiex_cfg80211_add_station,
3963         .change_station = mwifiex_cfg80211_change_station,
3964         .get_channel = mwifiex_cfg80211_get_channel,
3965         .start_radar_detection = mwifiex_cfg80211_start_radar_detection,
3966         .channel_switch = mwifiex_cfg80211_channel_switch,
3967 };
3968
3969 #ifdef CONFIG_PM
3970 static const struct wiphy_wowlan_support mwifiex_wowlan_support = {
3971         .flags = WIPHY_WOWLAN_MAGIC_PKT | WIPHY_WOWLAN_DISCONNECT |
3972                 WIPHY_WOWLAN_NET_DETECT | WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
3973                 WIPHY_WOWLAN_GTK_REKEY_FAILURE,
3974         .n_patterns = MWIFIEX_MEF_MAX_FILTERS,
3975         .pattern_min_len = 1,
3976         .pattern_max_len = MWIFIEX_MAX_PATTERN_LEN,
3977         .max_pkt_offset = MWIFIEX_MAX_OFFSET_LEN,
3978         .max_nd_match_sets = MWIFIEX_MAX_ND_MATCH_SETS,
3979 };
3980 #endif
3981
3982 static bool mwifiex_is_valid_alpha2(const char *alpha2)
3983 {
3984         if (!alpha2 || strlen(alpha2) != 2)
3985                 return false;
3986
3987         if (isalpha(alpha2[0]) && isalpha(alpha2[1]))
3988                 return true;
3989
3990         return false;
3991 }
3992
3993 static const struct wiphy_coalesce_support mwifiex_coalesce_support = {
3994         .n_rules = MWIFIEX_COALESCE_MAX_RULES,
3995         .max_delay = MWIFIEX_MAX_COALESCING_DELAY,
3996         .n_patterns = MWIFIEX_COALESCE_MAX_FILTERS,
3997         .pattern_min_len = 1,
3998         .pattern_max_len = MWIFIEX_MAX_PATTERN_LEN,
3999         .max_pkt_offset = MWIFIEX_MAX_OFFSET_LEN,
4000 };
4001
4002 int mwifiex_init_channel_scan_gap(struct mwifiex_adapter *adapter)
4003 {
4004         u32 n_channels_bg, n_channels_a = 0;
4005
4006         n_channels_bg = mwifiex_band_2ghz.n_channels;
4007
4008         if (adapter->config_bands & BAND_A)
4009                 n_channels_a = mwifiex_band_5ghz.n_channels;
4010
4011         adapter->num_in_chan_stats = max_t(u32, n_channels_bg, n_channels_a);
4012         adapter->chan_stats = vmalloc(sizeof(*adapter->chan_stats) *
4013                                       adapter->num_in_chan_stats);
4014
4015         if (!adapter->chan_stats)
4016                 return -ENOMEM;
4017
4018         return 0;
4019 }
4020
4021 /*
4022  * This function registers the device with CFG802.11 subsystem.
4023  *
4024  * The function creates the wireless device/wiphy, populates it with
4025  * default parameters and handler function pointers, and finally
4026  * registers the device.
4027  */
4028
4029 int mwifiex_register_cfg80211(struct mwifiex_adapter *adapter)
4030 {
4031         int ret;
4032         void *wdev_priv;
4033         struct wiphy *wiphy;
4034         struct mwifiex_private *priv = adapter->priv[MWIFIEX_BSS_TYPE_STA];
4035         u8 *country_code;
4036         u32 thr, retry;
4037
4038         /* create a new wiphy for use with cfg80211 */
4039         wiphy = wiphy_new(&mwifiex_cfg80211_ops,
4040                           sizeof(struct mwifiex_adapter *));
4041         if (!wiphy) {
4042                 mwifiex_dbg(adapter, ERROR,
4043                             "%s: creating new wiphy\n", __func__);
4044                 return -ENOMEM;
4045         }
4046         wiphy->max_scan_ssids = MWIFIEX_MAX_SSID_LIST_LENGTH;
4047         wiphy->max_scan_ie_len = MWIFIEX_MAX_VSIE_LEN;
4048         wiphy->mgmt_stypes = mwifiex_mgmt_stypes;
4049         wiphy->max_remain_on_channel_duration = 5000;
4050         wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
4051                                  BIT(NL80211_IFTYPE_ADHOC) |
4052                                  BIT(NL80211_IFTYPE_P2P_CLIENT) |
4053                                  BIT(NL80211_IFTYPE_P2P_GO) |
4054                                  BIT(NL80211_IFTYPE_AP);
4055
4056         wiphy->bands[NL80211_BAND_2GHZ] = &mwifiex_band_2ghz;
4057         if (adapter->config_bands & BAND_A)
4058                 wiphy->bands[NL80211_BAND_5GHZ] = &mwifiex_band_5ghz;
4059         else
4060                 wiphy->bands[NL80211_BAND_5GHZ] = NULL;
4061
4062         if (adapter->drcs_enabled && ISSUPP_DRCS_ENABLED(adapter->fw_cap_info))
4063                 wiphy->iface_combinations = &mwifiex_iface_comb_ap_sta_drcs;
4064         else if (adapter->is_hw_11ac_capable)
4065                 wiphy->iface_combinations = &mwifiex_iface_comb_ap_sta_vht;
4066         else
4067                 wiphy->iface_combinations = &mwifiex_iface_comb_ap_sta;
4068         wiphy->n_iface_combinations = 1;
4069
4070         /* Initialize cipher suits */
4071         wiphy->cipher_suites = mwifiex_cipher_suites;
4072         wiphy->n_cipher_suites = ARRAY_SIZE(mwifiex_cipher_suites);
4073
4074         if (adapter->region_code)
4075                 wiphy->regulatory_flags |= REGULATORY_DISABLE_BEACON_HINTS |
4076                                            REGULATORY_COUNTRY_IE_IGNORE;
4077
4078         ether_addr_copy(wiphy->perm_addr, adapter->perm_addr);
4079         wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
4080         wiphy->flags |= WIPHY_FLAG_HAVE_AP_SME |
4081                         WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD |
4082                         WIPHY_FLAG_AP_UAPSD |
4083                         WIPHY_FLAG_SUPPORTS_SCHED_SCAN |
4084                         WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
4085                         WIPHY_FLAG_HAS_CHANNEL_SWITCH |
4086                         WIPHY_FLAG_PS_ON_BY_DEFAULT;
4087
4088         if (ISSUPP_TDLS_ENABLED(adapter->fw_cap_info))
4089                 wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS |
4090                                 WIPHY_FLAG_TDLS_EXTERNAL_SETUP;
4091
4092 #ifdef CONFIG_PM
4093         wiphy->wowlan = &mwifiex_wowlan_support;
4094 #endif
4095
4096         wiphy->coalesce = &mwifiex_coalesce_support;
4097
4098         wiphy->probe_resp_offload = NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
4099                                     NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
4100                                     NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
4101
4102         wiphy->max_sched_scan_ssids = MWIFIEX_MAX_SSID_LIST_LENGTH;
4103         wiphy->max_sched_scan_ie_len = MWIFIEX_MAX_VSIE_LEN;
4104         wiphy->max_match_sets = MWIFIEX_MAX_SSID_LIST_LENGTH;
4105
4106         wiphy->available_antennas_tx = BIT(adapter->number_of_antenna) - 1;
4107         wiphy->available_antennas_rx = BIT(adapter->number_of_antenna) - 1;
4108
4109         wiphy->features |= NL80211_FEATURE_HT_IBSS |
4110                            NL80211_FEATURE_INACTIVITY_TIMER |
4111                            NL80211_FEATURE_LOW_PRIORITY_SCAN |
4112                            NL80211_FEATURE_NEED_OBSS_SCAN;
4113
4114         if (ISSUPP_TDLS_ENABLED(adapter->fw_cap_info))
4115                 wiphy->features |= NL80211_FEATURE_TDLS_CHANNEL_SWITCH;
4116
4117         if (adapter->fw_api_ver == MWIFIEX_FW_V15)
4118                 wiphy->features |= NL80211_FEATURE_SK_TX_STATUS;
4119
4120         /* Reserve space for mwifiex specific private data for BSS */
4121         wiphy->bss_priv_size = sizeof(struct mwifiex_bss_priv);
4122
4123         wiphy->reg_notifier = mwifiex_reg_notifier;
4124
4125         /* Set struct mwifiex_adapter pointer in wiphy_priv */
4126         wdev_priv = wiphy_priv(wiphy);
4127         *(unsigned long *)wdev_priv = (unsigned long)adapter;
4128
4129         set_wiphy_dev(wiphy, priv->adapter->dev);
4130
4131         ret = wiphy_register(wiphy);
4132         if (ret < 0) {
4133                 mwifiex_dbg(adapter, ERROR,
4134                             "%s: wiphy_register failed: %d\n", __func__, ret);
4135                 wiphy_free(wiphy);
4136                 return ret;
4137         }
4138
4139         if (reg_alpha2 && mwifiex_is_valid_alpha2(reg_alpha2)) {
4140                 mwifiex_dbg(adapter, INFO,
4141                             "driver hint alpha2: %2.2s\n", reg_alpha2);
4142                 regulatory_hint(wiphy, reg_alpha2);
4143         } else {
4144                 if (adapter->region_code == 0x00) {
4145                         mwifiex_dbg(adapter, WARN, "Ignore world regulatory domain\n");
4146                 } else {
4147                         country_code =
4148                                 mwifiex_11d_code_2_region(adapter->region_code);
4149                         if (country_code &&
4150                             regulatory_hint(wiphy, country_code))
4151                                 mwifiex_dbg(priv->adapter, ERROR, "regulatory_hint() failed\n");
4152                 }
4153         }
4154
4155         mwifiex_send_cmd(priv, HostCmd_CMD_802_11_SNMP_MIB,
4156                          HostCmd_ACT_GEN_GET, FRAG_THRESH_I, &thr, true);
4157         wiphy->frag_threshold = thr;
4158         mwifiex_send_cmd(priv, HostCmd_CMD_802_11_SNMP_MIB,
4159                          HostCmd_ACT_GEN_GET, RTS_THRESH_I, &thr, true);
4160         wiphy->rts_threshold = thr;
4161         mwifiex_send_cmd(priv, HostCmd_CMD_802_11_SNMP_MIB,
4162                          HostCmd_ACT_GEN_GET, SHORT_RETRY_LIM_I, &retry, true);
4163         wiphy->retry_short = (u8) retry;
4164         mwifiex_send_cmd(priv, HostCmd_CMD_802_11_SNMP_MIB,
4165                          HostCmd_ACT_GEN_GET, LONG_RETRY_LIM_I, &retry, true);
4166         wiphy->retry_long = (u8) retry;
4167
4168         adapter->wiphy = wiphy;
4169         return ret;
4170 }