]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/staging/wfx/sta.c
Merge tag 'armsoc-defconfig' of git://git.kernel.org/pub/scm/linux/kernel/git/soc/soc
[linux.git] / drivers / staging / wfx / sta.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Implementation of mac80211 API.
4  *
5  * Copyright (c) 2017-2019, Silicon Laboratories, Inc.
6  * Copyright (c) 2010, ST-Ericsson
7  */
8 #include <net/mac80211.h>
9
10 #include "sta.h"
11 #include "wfx.h"
12 #include "fwio.h"
13 #include "bh.h"
14 #include "key.h"
15 #include "scan.h"
16 #include "debug.h"
17 #include "hif_tx.h"
18 #include "hif_tx_mib.h"
19
20 #define TXOP_UNIT 32
21 #define HIF_MAX_ARP_IP_ADDRTABLE_ENTRIES 2
22
23 static u32 wfx_rate_mask_to_hw(struct wfx_dev *wdev, u32 rates)
24 {
25         int i;
26         u32 ret = 0;
27         // WFx only support 2GHz
28         struct ieee80211_supported_band *sband = wdev->hw->wiphy->bands[NL80211_BAND_2GHZ];
29
30         for (i = 0; i < sband->n_bitrates; i++) {
31                 if (rates & BIT(i)) {
32                         if (i >= sband->n_bitrates)
33                                 dev_warn(wdev->dev, "unsupported basic rate\n");
34                         else
35                                 ret |= BIT(sband->bitrates[i].hw_value);
36                 }
37         }
38         return ret;
39 }
40
41 static void __wfx_free_event_queue(struct list_head *list)
42 {
43         struct wfx_hif_event *event, *tmp;
44
45         list_for_each_entry_safe(event, tmp, list, link) {
46                 list_del(&event->link);
47                 kfree(event);
48         }
49 }
50
51 static void wfx_free_event_queue(struct wfx_vif *wvif)
52 {
53         LIST_HEAD(list);
54
55         spin_lock(&wvif->event_queue_lock);
56         list_splice_init(&wvif->event_queue, &list);
57         spin_unlock(&wvif->event_queue_lock);
58
59         __wfx_free_event_queue(&list);
60 }
61
62 void wfx_cqm_bssloss_sm(struct wfx_vif *wvif, int init, int good, int bad)
63 {
64         int tx = 0;
65
66         mutex_lock(&wvif->bss_loss_lock);
67         wvif->delayed_link_loss = 0;
68         cancel_work_sync(&wvif->bss_params_work);
69
70         /* If we have a pending unjoin */
71         if (wvif->delayed_unjoin)
72                 goto end;
73
74         if (init) {
75                 schedule_delayed_work(&wvif->bss_loss_work, HZ);
76                 wvif->bss_loss_state = 0;
77
78                 if (!atomic_read(&wvif->wdev->tx_lock))
79                         tx = 1;
80         } else if (good) {
81                 cancel_delayed_work_sync(&wvif->bss_loss_work);
82                 wvif->bss_loss_state = 0;
83                 schedule_work(&wvif->bss_params_work);
84         } else if (bad) {
85                 /* FIXME Should we just keep going until we time out? */
86                 if (wvif->bss_loss_state < 3)
87                         tx = 1;
88         } else {
89                 cancel_delayed_work_sync(&wvif->bss_loss_work);
90                 wvif->bss_loss_state = 0;
91         }
92
93         /* Spit out a NULL packet to our AP if necessary */
94         // FIXME: call ieee80211_beacon_loss/ieee80211_connection_loss instead
95         if (tx) {
96                 struct sk_buff *skb;
97
98                 wvif->bss_loss_state++;
99
100                 skb = ieee80211_nullfunc_get(wvif->wdev->hw, wvif->vif, false);
101                 if (!skb)
102                         goto end;
103                 memset(IEEE80211_SKB_CB(skb), 0,
104                        sizeof(*IEEE80211_SKB_CB(skb)));
105                 IEEE80211_SKB_CB(skb)->control.vif = wvif->vif;
106                 IEEE80211_SKB_CB(skb)->driver_rates[0].idx = 0;
107                 IEEE80211_SKB_CB(skb)->driver_rates[0].count = 1;
108                 IEEE80211_SKB_CB(skb)->driver_rates[1].idx = -1;
109                 wfx_tx(wvif->wdev->hw, NULL, skb);
110         }
111 end:
112         mutex_unlock(&wvif->bss_loss_lock);
113 }
114
115 static int wfx_set_uapsd_param(struct wfx_vif *wvif,
116                            const struct wfx_edca_params *arg)
117 {
118         /* Here's the mapping AC [queue, bit]
119          *  VO [0,3], VI [1, 2], BE [2, 1], BK [3, 0]
120          */
121
122         if (arg->uapsd_enable[IEEE80211_AC_VO])
123                 wvif->uapsd_info.trig_voice = 1;
124         else
125                 wvif->uapsd_info.trig_voice = 0;
126
127         if (arg->uapsd_enable[IEEE80211_AC_VI])
128                 wvif->uapsd_info.trig_video = 1;
129         else
130                 wvif->uapsd_info.trig_video = 0;
131
132         if (arg->uapsd_enable[IEEE80211_AC_BE])
133                 wvif->uapsd_info.trig_be = 1;
134         else
135                 wvif->uapsd_info.trig_be = 0;
136
137         if (arg->uapsd_enable[IEEE80211_AC_BK])
138                 wvif->uapsd_info.trig_bckgrnd = 1;
139         else
140                 wvif->uapsd_info.trig_bckgrnd = 0;
141
142         /* Currently pseudo U-APSD operation is not supported, so setting
143          * MinAutoTriggerInterval, MaxAutoTriggerInterval and
144          * AutoTriggerStep to 0
145          */
146         wvif->uapsd_info.min_auto_trigger_interval = 0;
147         wvif->uapsd_info.max_auto_trigger_interval = 0;
148         wvif->uapsd_info.auto_trigger_step = 0;
149
150         return hif_set_uapsd_info(wvif, &wvif->uapsd_info);
151 }
152
153 int wfx_fwd_probe_req(struct wfx_vif *wvif, bool enable)
154 {
155         wvif->fwd_probe_req = enable;
156         return hif_set_rx_filter(wvif, wvif->filter_bssid,
157                                  wvif->fwd_probe_req);
158 }
159
160 static int wfx_set_mcast_filter(struct wfx_vif *wvif,
161                                     struct wfx_grp_addr_table *fp)
162 {
163         int i, ret;
164         struct hif_mib_config_data_filter config = { };
165         struct hif_mib_set_data_filtering filter_data = { };
166         struct hif_mib_mac_addr_data_frame_condition filter_addr_val = { };
167         struct hif_mib_uc_mc_bc_data_frame_condition filter_addr_type = { };
168
169         // Temporary workaround for filters
170         return hif_set_data_filtering(wvif, &filter_data);
171
172         if (!fp->enable) {
173                 filter_data.enable = 0;
174                 return hif_set_data_filtering(wvif, &filter_data);
175         }
176
177         // A1 Address match on list
178         for (i = 0; i < fp->num_addresses; i++) {
179                 filter_addr_val.condition_idx = i;
180                 filter_addr_val.address_type = HIF_MAC_ADDR_A1;
181                 ether_addr_copy(filter_addr_val.mac_address,
182                                 fp->address_list[i]);
183                 ret = hif_set_mac_addr_condition(wvif,
184                                                  &filter_addr_val);
185                 if (ret)
186                         return ret;
187                 config.mac_cond |= 1 << i;
188         }
189
190         // Accept unicast and broadcast
191         filter_addr_type.condition_idx = 0;
192         filter_addr_type.param.bits.type_unicast = 1;
193         filter_addr_type.param.bits.type_broadcast = 1;
194         ret = hif_set_uc_mc_bc_condition(wvif, &filter_addr_type);
195         if (ret)
196                 return ret;
197
198         config.uc_mc_bc_cond = 1;
199         config.filter_idx = 0; // TODO #define MULTICAST_FILTERING 0
200         config.enable = 1;
201         ret = hif_set_config_data_filter(wvif, &config);
202         if (ret)
203                 return ret;
204
205         // discard all data frames except match filter
206         filter_data.enable = 1;
207         filter_data.default_filter = 1; // discard all
208         ret = hif_set_data_filtering(wvif, &filter_data);
209
210         return ret;
211 }
212
213 void wfx_update_filtering(struct wfx_vif *wvif)
214 {
215         int ret;
216         bool is_sta = wvif->vif && NL80211_IFTYPE_STATION == wvif->vif->type;
217         bool filter_bssid = wvif->filter_bssid;
218         bool fwd_probe_req = wvif->fwd_probe_req;
219         struct hif_mib_bcn_filter_enable bf_ctrl;
220         struct hif_ie_table_entry filter_ies[] = {
221                 {
222                         .ie_id        = WLAN_EID_VENDOR_SPECIFIC,
223                         .has_changed  = 1,
224                         .no_longer    = 1,
225                         .has_appeared = 1,
226                         .oui          = { 0x50, 0x6F, 0x9A },
227                 }, {
228                         .ie_id        = WLAN_EID_HT_OPERATION,
229                         .has_changed  = 1,
230                         .no_longer    = 1,
231                         .has_appeared = 1,
232                 }, {
233                         .ie_id        = WLAN_EID_ERP_INFO,
234                         .has_changed  = 1,
235                         .no_longer    = 1,
236                         .has_appeared = 1,
237                 }
238         };
239         int n_filter_ies;
240
241         if (wvif->state == WFX_STATE_PASSIVE)
242                 return;
243
244         if (wvif->disable_beacon_filter) {
245                 bf_ctrl.enable = 0;
246                 bf_ctrl.bcn_count = 1;
247                 n_filter_ies = 0;
248         } else if (!is_sta) {
249                 bf_ctrl.enable = HIF_BEACON_FILTER_ENABLE |
250                                  HIF_BEACON_FILTER_AUTO_ERP;
251                 bf_ctrl.bcn_count = 0;
252                 n_filter_ies = 2;
253         } else {
254                 bf_ctrl.enable = HIF_BEACON_FILTER_ENABLE;
255                 bf_ctrl.bcn_count = 0;
256                 n_filter_ies = 3;
257         }
258
259         ret = hif_set_rx_filter(wvif, filter_bssid, fwd_probe_req);
260         if (!ret)
261                 ret = hif_set_beacon_filter_table(wvif, n_filter_ies,
262                                                   filter_ies);
263         if (!ret)
264                 ret = hif_beacon_filter_control(wvif, bf_ctrl.enable,
265                                                 bf_ctrl.bcn_count);
266         if (!ret)
267                 ret = wfx_set_mcast_filter(wvif, &wvif->mcast_filter);
268         if (ret)
269                 dev_err(wvif->wdev->dev, "update filtering failed: %d\n", ret);
270 }
271
272 static void wfx_update_filtering_work(struct work_struct *work)
273 {
274         struct wfx_vif *wvif = container_of(work, struct wfx_vif,
275                                             update_filtering_work);
276
277         wfx_update_filtering(wvif);
278 }
279
280 u64 wfx_prepare_multicast(struct ieee80211_hw *hw,
281                           struct netdev_hw_addr_list *mc_list)
282 {
283         int i;
284         struct netdev_hw_addr *ha;
285         struct wfx_vif *wvif = NULL;
286         struct wfx_dev *wdev = hw->priv;
287         int count = netdev_hw_addr_list_count(mc_list);
288
289         while ((wvif = wvif_iterate(wdev, wvif)) != NULL) {
290                 memset(&wvif->mcast_filter, 0x00, sizeof(wvif->mcast_filter));
291                 if (!count ||
292                     count > ARRAY_SIZE(wvif->mcast_filter.address_list))
293                         continue;
294
295                 i = 0;
296                 netdev_hw_addr_list_for_each(ha, mc_list) {
297                         ether_addr_copy(wvif->mcast_filter.address_list[i],
298                                         ha->addr);
299                         i++;
300                 }
301                 wvif->mcast_filter.enable = true;
302                 wvif->mcast_filter.num_addresses = count;
303         }
304
305         return 0;
306 }
307
308 void wfx_configure_filter(struct ieee80211_hw *hw,
309                              unsigned int changed_flags,
310                              unsigned int *total_flags,
311                              u64 unused)
312 {
313         struct wfx_vif *wvif = NULL;
314         struct wfx_dev *wdev = hw->priv;
315
316         *total_flags &= FIF_OTHER_BSS | FIF_FCSFAIL | FIF_PROBE_REQ;
317
318         while ((wvif = wvif_iterate(wdev, wvif)) != NULL) {
319                 down(&wvif->scan.lock);
320                 wvif->filter_bssid = (*total_flags &
321                                       (FIF_OTHER_BSS | FIF_PROBE_REQ)) ? 0 : 1;
322                 wvif->disable_beacon_filter = !(*total_flags & FIF_PROBE_REQ);
323                 wfx_fwd_probe_req(wvif, true);
324                 wfx_update_filtering(wvif);
325                 up(&wvif->scan.lock);
326         }
327 }
328
329 int wfx_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
330                    u16 queue, const struct ieee80211_tx_queue_params *params)
331 {
332         struct wfx_dev *wdev = hw->priv;
333         struct wfx_vif *wvif = (struct wfx_vif *) vif->drv_priv;
334         int ret = 0;
335         /* To prevent re-applying PM request OID again and again*/
336         u16 old_uapsd_flags, new_uapsd_flags;
337         struct hif_req_edca_queue_params *edca;
338
339         mutex_lock(&wdev->conf_mutex);
340
341         if (queue < hw->queues) {
342                 old_uapsd_flags = *((u16 *) &wvif->uapsd_info);
343                 edca = &wvif->edca.params[queue];
344
345                 wvif->edca.uapsd_enable[queue] = params->uapsd;
346                 edca->aifsn = params->aifs;
347                 edca->cw_min = params->cw_min;
348                 edca->cw_max = params->cw_max;
349                 edca->tx_op_limit = params->txop * TXOP_UNIT;
350                 edca->allowed_medium_time = 0;
351                 ret = hif_set_edca_queue_params(wvif, edca);
352                 if (ret) {
353                         ret = -EINVAL;
354                         goto out;
355                 }
356
357                 if (wvif->vif->type == NL80211_IFTYPE_STATION) {
358                         ret = wfx_set_uapsd_param(wvif, &wvif->edca);
359                         new_uapsd_flags = *((u16 *) &wvif->uapsd_info);
360                         if (!ret && wvif->setbssparams_done &&
361                             wvif->state == WFX_STATE_STA &&
362                             old_uapsd_flags != new_uapsd_flags)
363                                 ret = wfx_set_pm(wvif, &wvif->powersave_mode);
364                 }
365         } else {
366                 ret = -EINVAL;
367         }
368
369 out:
370         mutex_unlock(&wdev->conf_mutex);
371         return ret;
372 }
373
374 int wfx_set_pm(struct wfx_vif *wvif, const struct hif_req_set_pm_mode *arg)
375 {
376         struct hif_req_set_pm_mode pm = *arg;
377         u16 uapsd_flags;
378         int ret;
379
380         if (wvif->state != WFX_STATE_STA || !wvif->bss_params.aid)
381                 return 0;
382
383         memcpy(&uapsd_flags, &wvif->uapsd_info, sizeof(uapsd_flags));
384
385         if (uapsd_flags != 0)
386                 pm.pm_mode.fast_psm = 0;
387
388         // Kernel disable PowerSave when multiple vifs are in use. In contrary,
389         // it is absolutly necessary to enable PowerSave for WF200
390         if (wvif_count(wvif->wdev) > 1) {
391                 pm.pm_mode.enter_psm = 1;
392                 pm.pm_mode.fast_psm = 0;
393         }
394
395         if (!wait_for_completion_timeout(&wvif->set_pm_mode_complete,
396                                          msecs_to_jiffies(300)))
397                 dev_warn(wvif->wdev->dev,
398                          "timeout while waiting of set_pm_mode_complete\n");
399         ret = hif_set_pm(wvif, &pm);
400         // FIXME: why ?
401         if (-ETIMEDOUT == wvif->scan.status)
402                 wvif->scan.status = 1;
403         return ret;
404 }
405
406 int wfx_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
407 {
408         struct wfx_dev *wdev = hw->priv;
409         struct wfx_vif *wvif = NULL;
410
411         while ((wvif = wvif_iterate(wdev, wvif)) != NULL)
412                 hif_rts_threshold(wvif, value);
413         return 0;
414 }
415
416 /* If successful, LOCKS the TX queue! */
417 static int __wfx_flush(struct wfx_dev *wdev, bool drop)
418 {
419         int ret;
420
421         for (;;) {
422                 if (drop) {
423                         wfx_tx_queues_clear(wdev);
424                 } else {
425                         ret = wait_event_timeout(
426                                 wdev->tx_queue_stats.wait_link_id_empty,
427                                 wfx_tx_queues_is_empty(wdev),
428                                 2 * HZ);
429                 }
430
431                 if (!drop && ret <= 0) {
432                         ret = -ETIMEDOUT;
433                         break;
434                 }
435                 ret = 0;
436
437                 wfx_tx_lock_flush(wdev);
438                 if (!wfx_tx_queues_is_empty(wdev)) {
439                         /* Highly unlikely: WSM requeued frames. */
440                         wfx_tx_unlock(wdev);
441                         continue;
442                 }
443                 break;
444         }
445         return ret;
446 }
447
448 void wfx_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
449                   u32 queues, bool drop)
450 {
451         struct wfx_dev *wdev = hw->priv;
452         struct wfx_vif *wvif;
453
454         if (vif) {
455                 wvif = (struct wfx_vif *) vif->drv_priv;
456                 if (wvif->vif->type == NL80211_IFTYPE_MONITOR)
457                         drop = true;
458                 if (wvif->vif->type == NL80211_IFTYPE_AP &&
459                     !wvif->enable_beacon)
460                         drop = true;
461         }
462
463         // FIXME: only flush requested vif
464         if (!__wfx_flush(wdev, drop))
465                 wfx_tx_unlock(wdev);
466 }
467
468 /* WSM callbacks */
469
470 static void wfx_event_report_rssi(struct wfx_vif *wvif, u8 raw_rcpi_rssi)
471 {
472         /* RSSI: signed Q8.0, RCPI: unsigned Q7.1
473          * RSSI = RCPI / 2 - 110
474          */
475         int rcpi_rssi;
476         int cqm_evt;
477
478         rcpi_rssi = raw_rcpi_rssi / 2 - 110;
479         if (rcpi_rssi <= wvif->cqm_rssi_thold)
480                 cqm_evt = NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW;
481         else
482                 cqm_evt = NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH;
483         ieee80211_cqm_rssi_notify(wvif->vif, cqm_evt, rcpi_rssi, GFP_KERNEL);
484 }
485
486 static void wfx_event_handler_work(struct work_struct *work)
487 {
488         struct wfx_vif *wvif =
489                 container_of(work, struct wfx_vif, event_handler_work);
490         struct wfx_hif_event *event;
491
492         LIST_HEAD(list);
493
494         spin_lock(&wvif->event_queue_lock);
495         list_splice_init(&wvif->event_queue, &list);
496         spin_unlock(&wvif->event_queue_lock);
497
498         list_for_each_entry(event, &list, link) {
499                 switch (event->evt.event_id) {
500                 case HIF_EVENT_IND_BSSLOST:
501                         cancel_work_sync(&wvif->unjoin_work);
502                         if (!down_trylock(&wvif->scan.lock)) {
503                                 wfx_cqm_bssloss_sm(wvif, 1, 0, 0);
504                                 up(&wvif->scan.lock);
505                         } else {
506                                 /* Scan is in progress. Delay reporting.
507                                  * Scan complete will trigger bss_loss_work
508                                  */
509                                 wvif->delayed_link_loss = 1;
510                                 /* Also start a watchdog. */
511                                 schedule_delayed_work(&wvif->bss_loss_work,
512                                                       5 * HZ);
513                         }
514                         break;
515                 case HIF_EVENT_IND_BSSREGAINED:
516                         wfx_cqm_bssloss_sm(wvif, 0, 0, 0);
517                         cancel_work_sync(&wvif->unjoin_work);
518                         break;
519                 case HIF_EVENT_IND_RCPI_RSSI:
520                         wfx_event_report_rssi(wvif,
521                                               event->evt.event_data.rcpi_rssi);
522                         break;
523                 case HIF_EVENT_IND_PS_MODE_ERROR:
524                         dev_warn(wvif->wdev->dev,
525                                  "error while processing power save request\n");
526                         break;
527                 default:
528                         dev_warn(wvif->wdev->dev,
529                                  "unhandled event indication: %.2x\n",
530                                  event->evt.event_id);
531                         break;
532                 }
533         }
534         __wfx_free_event_queue(&list);
535 }
536
537 static void wfx_bss_loss_work(struct work_struct *work)
538 {
539         struct wfx_vif *wvif = container_of(work, struct wfx_vif,
540                                             bss_loss_work.work);
541
542         ieee80211_connection_loss(wvif->vif);
543 }
544
545 static void wfx_bss_params_work(struct work_struct *work)
546 {
547         struct wfx_vif *wvif = container_of(work, struct wfx_vif,
548                                             bss_params_work);
549
550         mutex_lock(&wvif->wdev->conf_mutex);
551         wvif->bss_params.bss_flags.lost_count_only = 1;
552         hif_set_bss_params(wvif, &wvif->bss_params);
553         wvif->bss_params.bss_flags.lost_count_only = 0;
554         mutex_unlock(&wvif->wdev->conf_mutex);
555 }
556
557 static void wfx_set_beacon_wakeup_period_work(struct work_struct *work)
558 {
559         struct wfx_vif *wvif = container_of(work, struct wfx_vif,
560                                             set_beacon_wakeup_period_work);
561
562         hif_set_beacon_wakeup_period(wvif, wvif->dtim_period,
563                                      wvif->dtim_period);
564 }
565
566 static void wfx_do_unjoin(struct wfx_vif *wvif)
567 {
568         mutex_lock(&wvif->wdev->conf_mutex);
569
570         if (atomic_read(&wvif->scan.in_progress)) {
571                 if (wvif->delayed_unjoin)
572                         dev_dbg(wvif->wdev->dev,
573                                 "delayed unjoin is already scheduled\n");
574                 else
575                         wvif->delayed_unjoin = true;
576                 goto done;
577         }
578
579         wvif->delayed_link_loss = false;
580
581         if (!wvif->state)
582                 goto done;
583
584         if (wvif->state == WFX_STATE_AP)
585                 goto done;
586
587         cancel_work_sync(&wvif->update_filtering_work);
588         cancel_work_sync(&wvif->set_beacon_wakeup_period_work);
589         wvif->state = WFX_STATE_PASSIVE;
590
591         /* Unjoin is a reset. */
592         wfx_tx_flush(wvif->wdev);
593         hif_keep_alive_period(wvif, 0);
594         hif_reset(wvif, false);
595         hif_set_output_power(wvif, wvif->wdev->output_power * 10);
596         wvif->dtim_period = 0;
597         hif_set_macaddr(wvif, wvif->vif->addr);
598         wfx_free_event_queue(wvif);
599         cancel_work_sync(&wvif->event_handler_work);
600         wfx_cqm_bssloss_sm(wvif, 0, 0, 0);
601
602         /* Disable Block ACKs */
603         hif_set_block_ack_policy(wvif, 0, 0);
604
605         wvif->disable_beacon_filter = false;
606         wfx_update_filtering(wvif);
607         memset(&wvif->bss_params, 0, sizeof(wvif->bss_params));
608         wvif->setbssparams_done = false;
609         memset(&wvif->ht_info, 0, sizeof(wvif->ht_info));
610
611 done:
612         mutex_unlock(&wvif->wdev->conf_mutex);
613 }
614
615 static void wfx_set_mfp(struct wfx_vif *wvif,
616                         struct cfg80211_bss *bss)
617 {
618         const int pairwise_cipher_suite_count_offset = 8 / sizeof(u16);
619         const int pairwise_cipher_suite_size = 4 / sizeof(u16);
620         const int akm_suite_size = 4 / sizeof(u16);
621         const u16 *ptr = NULL;
622         bool mfpc = false;
623         bool mfpr = false;
624
625         /* 802.11w protected mgmt frames */
626
627         /* retrieve MFPC and MFPR flags from beacon or PBRSP */
628
629         rcu_read_lock();
630         if (bss)
631                 ptr = (const u16 *) ieee80211_bss_get_ie(bss,
632                                                               WLAN_EID_RSN);
633
634         if (ptr) {
635                 ptr += pairwise_cipher_suite_count_offset;
636                 ptr += 1 + pairwise_cipher_suite_size * *ptr;
637                 ptr += 1 + akm_suite_size * *ptr;
638                 mfpr = *ptr & BIT(6);
639                 mfpc = *ptr & BIT(7);
640         }
641         rcu_read_unlock();
642
643         hif_set_mfp(wvif, mfpc, mfpr);
644 }
645
646 /* MUST be called with tx_lock held!  It will be unlocked for us. */
647 static void wfx_do_join(struct wfx_vif *wvif)
648 {
649         const u8 *bssid;
650         struct ieee80211_bss_conf *conf = &wvif->vif->bss_conf;
651         struct cfg80211_bss *bss = NULL;
652         struct hif_req_join join = {
653                 .mode = conf->ibss_joined ? HIF_MODE_IBSS : HIF_MODE_BSS,
654                 .preamble_type = conf->use_short_preamble ? HIF_PREAMBLE_SHORT : HIF_PREAMBLE_LONG,
655                 .probe_for_join = 1,
656                 .atim_window = 0,
657                 .basic_rate_set = wfx_rate_mask_to_hw(wvif->wdev,
658                                                       conf->basic_rates),
659         };
660
661         if (wvif->channel->flags & IEEE80211_CHAN_NO_IR)
662                 join.probe_for_join = 0;
663
664         if (wvif->state)
665                 wfx_do_unjoin(wvif);
666
667         bssid = wvif->vif->bss_conf.bssid;
668
669         bss = cfg80211_get_bss(wvif->wdev->hw->wiphy, wvif->channel,
670                                bssid, NULL, 0,
671                                IEEE80211_BSS_TYPE_ANY, IEEE80211_PRIVACY_ANY);
672
673         if (!bss && !conf->ibss_joined) {
674                 wfx_tx_unlock(wvif->wdev);
675                 return;
676         }
677
678         mutex_lock(&wvif->wdev->conf_mutex);
679
680         /* Under the conf lock: check scan status and
681          * bail out if it is in progress.
682          */
683         if (atomic_read(&wvif->scan.in_progress)) {
684                 wfx_tx_unlock(wvif->wdev);
685                 goto done_put;
686         }
687
688         /* Sanity check basic rates */
689         if (!join.basic_rate_set)
690                 join.basic_rate_set = 7;
691
692         /* Sanity check beacon interval */
693         if (!wvif->beacon_int)
694                 wvif->beacon_int = 1;
695
696         join.beacon_interval = wvif->beacon_int;
697
698         // DTIM period will be set on first Beacon
699         wvif->dtim_period = 0;
700
701         join.channel_number = wvif->channel->hw_value;
702         memcpy(join.bssid, bssid, sizeof(join.bssid));
703
704         if (!conf->ibss_joined) {
705                 const u8 *ssidie;
706
707                 rcu_read_lock();
708                 ssidie = ieee80211_bss_get_ie(bss, WLAN_EID_SSID);
709                 if (ssidie) {
710                         join.ssid_length = ssidie[1];
711                         memcpy(join.ssid, &ssidie[2], join.ssid_length);
712                 }
713                 rcu_read_unlock();
714         }
715
716         wfx_tx_flush(wvif->wdev);
717
718         if (wvif_count(wvif->wdev) <= 1)
719                 hif_set_block_ack_policy(wvif, 0xFF, 0xFF);
720
721         wfx_set_mfp(wvif, bss);
722
723         /* Perform actual join */
724         wvif->wdev->tx_burst_idx = -1;
725         if (hif_join(wvif, &join)) {
726                 ieee80211_connection_loss(wvif->vif);
727                 wvif->join_complete_status = -1;
728                 /* Tx lock still held, unjoin will clear it. */
729                 if (!schedule_work(&wvif->unjoin_work))
730                         wfx_tx_unlock(wvif->wdev);
731         } else {
732                 wvif->join_complete_status = 0;
733                 if (wvif->vif->type == NL80211_IFTYPE_ADHOC)
734                         wvif->state = WFX_STATE_IBSS;
735                 else
736                         wvif->state = WFX_STATE_PRE_STA;
737                 wfx_tx_unlock(wvif->wdev);
738
739                 /* Upload keys */
740                 wfx_upload_keys(wvif);
741
742                 /* Due to beacon filtering it is possible that the
743                  * AP's beacon is not known for the mac80211 stack.
744                  * Disable filtering temporary to make sure the stack
745                  * receives at least one
746                  */
747                 wvif->disable_beacon_filter = true;
748         }
749         wfx_update_filtering(wvif);
750
751 done_put:
752         mutex_unlock(&wvif->wdev->conf_mutex);
753         if (bss)
754                 cfg80211_put_bss(wvif->wdev->hw->wiphy, bss);
755 }
756
757 static void wfx_unjoin_work(struct work_struct *work)
758 {
759         struct wfx_vif *wvif = container_of(work, struct wfx_vif, unjoin_work);
760
761         wfx_do_unjoin(wvif);
762         wfx_tx_unlock(wvif->wdev);
763 }
764
765 int wfx_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
766                 struct ieee80211_sta *sta)
767 {
768         struct wfx_dev *wdev = hw->priv;
769         struct wfx_vif *wvif = (struct wfx_vif *) vif->drv_priv;
770         struct wfx_sta_priv *sta_priv = (struct wfx_sta_priv *) &sta->drv_priv;
771         struct wfx_link_entry *entry;
772         struct sk_buff *skb;
773
774         if (wvif->vif->type != NL80211_IFTYPE_AP)
775                 return 0;
776
777         sta_priv->vif_id = wvif->id;
778         sta_priv->link_id = wfx_find_link_id(wvif, sta->addr);
779         if (!sta_priv->link_id) {
780                 dev_warn(wdev->dev, "mo more link-id available\n");
781                 return -ENOENT;
782         }
783
784         entry = &wvif->link_id_db[sta_priv->link_id - 1];
785         spin_lock_bh(&wvif->ps_state_lock);
786         if ((sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK) ==
787                                         IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
788                 wvif->sta_asleep_mask |= BIT(sta_priv->link_id);
789         entry->status = WFX_LINK_HARD;
790         while ((skb = skb_dequeue(&entry->rx_queue)))
791                 ieee80211_rx_irqsafe(wdev->hw, skb);
792         spin_unlock_bh(&wvif->ps_state_lock);
793         return 0;
794 }
795
796 int wfx_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
797                    struct ieee80211_sta *sta)
798 {
799         struct wfx_dev *wdev = hw->priv;
800         struct wfx_vif *wvif = (struct wfx_vif *) vif->drv_priv;
801         struct wfx_sta_priv *sta_priv = (struct wfx_sta_priv *) &sta->drv_priv;
802         struct wfx_link_entry *entry;
803
804         if (wvif->vif->type != NL80211_IFTYPE_AP || !sta_priv->link_id)
805                 return 0;
806
807         entry = &wvif->link_id_db[sta_priv->link_id - 1];
808         spin_lock_bh(&wvif->ps_state_lock);
809         entry->status = WFX_LINK_RESERVE;
810         entry->timestamp = jiffies;
811         wfx_tx_lock(wdev);
812         if (!schedule_work(&wvif->link_id_work))
813                 wfx_tx_unlock(wdev);
814         spin_unlock_bh(&wvif->ps_state_lock);
815         flush_work(&wvif->link_id_work);
816         return 0;
817 }
818
819 static void wfx_set_cts_work(struct work_struct *work)
820 {
821         struct wfx_vif *wvif = container_of(work, struct wfx_vif, set_cts_work);
822         u8 erp_ie[3] = { WLAN_EID_ERP_INFO, 1, 0 };
823         struct hif_ie_flags target_frame = {
824                 .beacon = 1,
825         };
826
827         mutex_lock(&wvif->wdev->conf_mutex);
828         erp_ie[2] = wvif->erp_info;
829         mutex_unlock(&wvif->wdev->conf_mutex);
830
831         hif_erp_use_protection(wvif, erp_ie[2] & WLAN_ERP_USE_PROTECTION);
832
833         if (wvif->vif->type != NL80211_IFTYPE_STATION)
834                 hif_update_ie(wvif, &target_frame, erp_ie, sizeof(erp_ie));
835 }
836
837 static int wfx_start_ap(struct wfx_vif *wvif)
838 {
839         int ret;
840         struct ieee80211_bss_conf *conf = &wvif->vif->bss_conf;
841         struct hif_req_start start = {
842                 .channel_number = wvif->channel->hw_value,
843                 .beacon_interval = conf->beacon_int,
844                 .dtim_period = conf->dtim_period,
845                 .preamble_type = conf->use_short_preamble ? HIF_PREAMBLE_SHORT : HIF_PREAMBLE_LONG,
846                 .basic_rate_set = wfx_rate_mask_to_hw(wvif->wdev,
847                                                       conf->basic_rates),
848         };
849
850         memset(start.ssid, 0, sizeof(start.ssid));
851         if (!conf->hidden_ssid) {
852                 start.ssid_length = conf->ssid_len;
853                 memcpy(start.ssid, conf->ssid, start.ssid_length);
854         }
855
856         wvif->beacon_int = conf->beacon_int;
857         wvif->dtim_period = conf->dtim_period;
858
859         memset(&wvif->link_id_db, 0, sizeof(wvif->link_id_db));
860
861         wvif->wdev->tx_burst_idx = -1;
862         ret = hif_start(wvif, &start);
863         if (!ret)
864                 ret = wfx_upload_keys(wvif);
865         if (!ret) {
866                 if (wvif_count(wvif->wdev) <= 1)
867                         hif_set_block_ack_policy(wvif, 0xFF, 0xFF);
868                 wvif->state = WFX_STATE_AP;
869                 wfx_update_filtering(wvif);
870         }
871         return ret;
872 }
873
874 static int wfx_update_beaconing(struct wfx_vif *wvif)
875 {
876         struct ieee80211_bss_conf *conf = &wvif->vif->bss_conf;
877
878         if (wvif->vif->type == NL80211_IFTYPE_AP) {
879                 /* TODO: check if changed channel, band */
880                 if (wvif->state != WFX_STATE_AP ||
881                     wvif->beacon_int != conf->beacon_int) {
882                         wfx_tx_lock_flush(wvif->wdev);
883                         if (wvif->state != WFX_STATE_PASSIVE)
884                                 hif_reset(wvif, false);
885                         wvif->state = WFX_STATE_PASSIVE;
886                         wfx_start_ap(wvif);
887                         wfx_tx_unlock(wvif->wdev);
888                 } else {
889                 }
890         }
891         return 0;
892 }
893
894 static int wfx_upload_beacon(struct wfx_vif *wvif)
895 {
896         int ret = 0;
897         struct sk_buff *skb = NULL;
898         struct ieee80211_mgmt *mgmt;
899         struct hif_mib_template_frame *p;
900
901         if (wvif->vif->type == NL80211_IFTYPE_STATION ||
902             wvif->vif->type == NL80211_IFTYPE_MONITOR ||
903             wvif->vif->type == NL80211_IFTYPE_UNSPECIFIED)
904                 goto done;
905
906         skb = ieee80211_beacon_get(wvif->wdev->hw, wvif->vif);
907
908         if (!skb)
909                 return -ENOMEM;
910
911         p = (struct hif_mib_template_frame *) skb_push(skb, 4);
912         p->frame_type = HIF_TMPLT_BCN;
913         p->init_rate = API_RATE_INDEX_B_1MBPS; /* 1Mbps DSSS */
914         p->frame_length = cpu_to_le16(skb->len - 4);
915
916         ret = hif_set_template_frame(wvif, p);
917
918         skb_pull(skb, 4);
919
920         if (ret)
921                 goto done;
922         /* TODO: Distill probe resp; remove TIM and any other beacon-specific
923          * IEs
924          */
925         mgmt = (void *)skb->data;
926         mgmt->frame_control =
927                 cpu_to_le16(IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_PROBE_RESP);
928
929         p->frame_type = HIF_TMPLT_PRBRES;
930
931         ret = hif_set_template_frame(wvif, p);
932         wfx_fwd_probe_req(wvif, false);
933
934 done:
935         dev_kfree_skb(skb);
936         return ret;
937 }
938
939 static int wfx_is_ht(const struct wfx_ht_info *ht_info)
940 {
941         return ht_info->channel_type != NL80211_CHAN_NO_HT;
942 }
943
944 static int wfx_ht_greenfield(const struct wfx_ht_info *ht_info)
945 {
946         return wfx_is_ht(ht_info) &&
947                 (ht_info->ht_cap.cap & IEEE80211_HT_CAP_GRN_FLD) &&
948                 !(ht_info->operation_mode &
949                   IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
950 }
951
952 static int wfx_ht_ampdu_density(const struct wfx_ht_info *ht_info)
953 {
954         if (!wfx_is_ht(ht_info))
955                 return 0;
956         return ht_info->ht_cap.ampdu_density;
957 }
958
959 static void wfx_join_finalize(struct wfx_vif *wvif,
960                               struct ieee80211_bss_conf *info)
961 {
962         struct ieee80211_sta *sta = NULL;
963         struct hif_mib_set_association_mode association_mode = { };
964
965         if (info->dtim_period)
966                 wvif->dtim_period = info->dtim_period;
967         wvif->beacon_int = info->beacon_int;
968
969         rcu_read_lock();
970         if (info->bssid && !info->ibss_joined)
971                 sta = ieee80211_find_sta(wvif->vif, info->bssid);
972         if (sta) {
973                 wvif->ht_info.ht_cap = sta->ht_cap;
974                 wvif->bss_params.operational_rate_set =
975                         wfx_rate_mask_to_hw(wvif->wdev, sta->supp_rates[wvif->channel->band]);
976                 wvif->ht_info.operation_mode = info->ht_operation_mode;
977         } else {
978                 memset(&wvif->ht_info, 0, sizeof(wvif->ht_info));
979                 wvif->bss_params.operational_rate_set = -1;
980         }
981         rcu_read_unlock();
982
983         /* Non Greenfield stations present */
984         if (wvif->ht_info.operation_mode &
985             IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT)
986                 hif_dual_cts_protection(wvif, true);
987         else
988                 hif_dual_cts_protection(wvif, false);
989
990         association_mode.preambtype_use = 1;
991         association_mode.mode = 1;
992         association_mode.rateset = 1;
993         association_mode.spacing = 1;
994         association_mode.preamble_type = info->use_short_preamble ? HIF_PREAMBLE_SHORT : HIF_PREAMBLE_LONG;
995         association_mode.basic_rate_set = cpu_to_le32(wfx_rate_mask_to_hw(wvif->wdev, info->basic_rates));
996         association_mode.mixed_or_greenfield_type = wfx_ht_greenfield(&wvif->ht_info);
997         association_mode.mpdu_start_spacing = wfx_ht_ampdu_density(&wvif->ht_info);
998
999         wfx_cqm_bssloss_sm(wvif, 0, 0, 0);
1000         cancel_work_sync(&wvif->unjoin_work);
1001
1002         wvif->bss_params.beacon_lost_count = 20;
1003         wvif->bss_params.aid = info->aid;
1004
1005         if (wvif->dtim_period < 1)
1006                 wvif->dtim_period = 1;
1007
1008         hif_set_association_mode(wvif, &association_mode);
1009
1010         if (!info->ibss_joined) {
1011                 hif_keep_alive_period(wvif, 30 /* sec */);
1012                 hif_set_bss_params(wvif, &wvif->bss_params);
1013                 wvif->setbssparams_done = true;
1014                 wfx_set_beacon_wakeup_period_work(&wvif->set_beacon_wakeup_period_work);
1015                 wfx_set_pm(wvif, &wvif->powersave_mode);
1016         }
1017 }
1018
1019 void wfx_bss_info_changed(struct ieee80211_hw *hw,
1020                              struct ieee80211_vif *vif,
1021                              struct ieee80211_bss_conf *info,
1022                              u32 changed)
1023 {
1024         struct wfx_dev *wdev = hw->priv;
1025         struct wfx_vif *wvif = (struct wfx_vif *) vif->drv_priv;
1026         bool do_join = false;
1027         int i;
1028         int nb_arp_addr;
1029
1030         mutex_lock(&wdev->conf_mutex);
1031
1032         /* TODO: BSS_CHANGED_QOS */
1033         if (changed & BSS_CHANGED_ARP_FILTER) {
1034                 struct hif_mib_arp_ip_addr_table filter = { };
1035
1036                 nb_arp_addr = info->arp_addr_cnt;
1037                 if (nb_arp_addr <= 0 || nb_arp_addr > HIF_MAX_ARP_IP_ADDRTABLE_ENTRIES)
1038                         nb_arp_addr = 0;
1039
1040                 for (i = 0; i < HIF_MAX_ARP_IP_ADDRTABLE_ENTRIES; i++) {
1041                         filter.condition_idx = i;
1042                         if (i < nb_arp_addr) {
1043                                 // Caution: type of arp_addr_list[i] is __be32
1044                                 memcpy(filter.ipv4_address,
1045                                        &info->arp_addr_list[i],
1046                                        sizeof(filter.ipv4_address));
1047                                 filter.arp_enable = HIF_ARP_NS_FILTERING_ENABLE;
1048                         } else {
1049                                 filter.arp_enable = HIF_ARP_NS_FILTERING_DISABLE;
1050                         }
1051                         hif_set_arp_ipv4_filter(wvif, &filter);
1052                 }
1053         }
1054
1055         if (changed &
1056             (BSS_CHANGED_BEACON | BSS_CHANGED_AP_PROBE_RESP |
1057              BSS_CHANGED_BSSID | BSS_CHANGED_SSID | BSS_CHANGED_IBSS)) {
1058                 wvif->beacon_int = info->beacon_int;
1059                 wfx_update_beaconing(wvif);
1060                 wfx_upload_beacon(wvif);
1061         }
1062
1063         if (changed & BSS_CHANGED_BEACON_ENABLED &&
1064             wvif->state != WFX_STATE_IBSS) {
1065                 if (wvif->enable_beacon != info->enable_beacon) {
1066                         hif_beacon_transmit(wvif, info->enable_beacon);
1067                         wvif->enable_beacon = info->enable_beacon;
1068                 }
1069         }
1070
1071         /* assoc/disassoc, or maybe AID changed */
1072         if (changed & BSS_CHANGED_ASSOC) {
1073                 wfx_tx_lock_flush(wdev);
1074                 wvif->wep_default_key_id = -1;
1075                 wfx_tx_unlock(wdev);
1076         }
1077
1078         if (changed & BSS_CHANGED_ASSOC && !info->assoc &&
1079             (wvif->state == WFX_STATE_STA || wvif->state == WFX_STATE_IBSS)) {
1080                 /* Shedule unjoin work */
1081                 wfx_tx_lock(wdev);
1082                 if (!schedule_work(&wvif->unjoin_work))
1083                         wfx_tx_unlock(wdev);
1084         } else {
1085                 if (changed & BSS_CHANGED_BEACON_INT) {
1086                         if (info->ibss_joined)
1087                                 do_join = true;
1088                         else if (wvif->state == WFX_STATE_AP)
1089                                 wfx_update_beaconing(wvif);
1090                 }
1091
1092                 if (changed & BSS_CHANGED_BSSID)
1093                         do_join = true;
1094
1095                 if (changed &
1096                     (BSS_CHANGED_ASSOC | BSS_CHANGED_BSSID |
1097                      BSS_CHANGED_IBSS | BSS_CHANGED_BASIC_RATES |
1098                      BSS_CHANGED_HT)) {
1099                         if (info->assoc) {
1100                                 if (wvif->state < WFX_STATE_PRE_STA) {
1101                                         ieee80211_connection_loss(vif);
1102                                         mutex_unlock(&wdev->conf_mutex);
1103                                         return;
1104                                 } else if (wvif->state == WFX_STATE_PRE_STA) {
1105                                         wvif->state = WFX_STATE_STA;
1106                                 }
1107                         } else {
1108                                 do_join = true;
1109                         }
1110
1111                         if (info->assoc || info->ibss_joined)
1112                                 wfx_join_finalize(wvif, info);
1113                         else
1114                                 memset(&wvif->bss_params, 0,
1115                                        sizeof(wvif->bss_params));
1116                 }
1117         }
1118
1119         /* ERP Protection */
1120         if (changed & (BSS_CHANGED_ASSOC |
1121                        BSS_CHANGED_ERP_CTS_PROT |
1122                        BSS_CHANGED_ERP_PREAMBLE)) {
1123                 u32 prev_erp_info = wvif->erp_info;
1124
1125                 if (info->use_cts_prot)
1126                         wvif->erp_info |= WLAN_ERP_USE_PROTECTION;
1127                 else if (!(prev_erp_info & WLAN_ERP_NON_ERP_PRESENT))
1128                         wvif->erp_info &= ~WLAN_ERP_USE_PROTECTION;
1129
1130                 if (info->use_short_preamble)
1131                         wvif->erp_info |= WLAN_ERP_BARKER_PREAMBLE;
1132                 else
1133                         wvif->erp_info &= ~WLAN_ERP_BARKER_PREAMBLE;
1134
1135                 if (prev_erp_info != wvif->erp_info)
1136                         schedule_work(&wvif->set_cts_work);
1137         }
1138
1139         if (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_ERP_SLOT))
1140                 hif_slot_time(wvif, info->use_short_slot ? 9 : 20);
1141
1142         if (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_CQM)) {
1143                 struct hif_mib_rcpi_rssi_threshold th = {
1144                         .rolling_average_count = 8,
1145                         .detection = 1,
1146                 };
1147
1148                 wvif->cqm_rssi_thold = info->cqm_rssi_thold;
1149
1150                 if (!info->cqm_rssi_thold && !info->cqm_rssi_hyst) {
1151                         th.upperthresh = 1;
1152                         th.lowerthresh = 1;
1153                 } else {
1154                         /* FIXME It's not a correct way of setting threshold.
1155                          * Upper and lower must be set equal here and adjusted
1156                          * in callback. However current implementation is much
1157                          * more reliable and stable.
1158                          */
1159                         /* RSSI: signed Q8.0, RCPI: unsigned Q7.1
1160                          * RSSI = RCPI / 2 - 110
1161                          */
1162                         th.upper_threshold = info->cqm_rssi_thold + info->cqm_rssi_hyst;
1163                         th.upper_threshold = (th.upper_threshold + 110) * 2;
1164                         th.lower_threshold = info->cqm_rssi_thold;
1165                         th.lower_threshold = (th.lower_threshold + 110) * 2;
1166                 }
1167                 hif_set_rcpi_rssi_threshold(wvif, &th);
1168         }
1169
1170         if (changed & BSS_CHANGED_TXPOWER &&
1171             info->txpower != wdev->output_power) {
1172                 wdev->output_power = info->txpower;
1173                 hif_set_output_power(wvif, wdev->output_power * 10);
1174         }
1175         mutex_unlock(&wdev->conf_mutex);
1176
1177         if (do_join) {
1178                 wfx_tx_lock_flush(wdev);
1179                 wfx_do_join(wvif); /* Will unlock it for us */
1180         }
1181 }
1182
1183 static void wfx_ps_notify(struct wfx_vif *wvif, enum sta_notify_cmd notify_cmd,
1184                           int link_id)
1185 {
1186         u32 bit, prev;
1187
1188         spin_lock_bh(&wvif->ps_state_lock);
1189         /* Zero link id means "for all link IDs" */
1190         if (link_id) {
1191                 bit = BIT(link_id);
1192         } else if (notify_cmd != STA_NOTIFY_AWAKE) {
1193                 dev_warn(wvif->wdev->dev, "unsupported notify command\n");
1194                 bit = 0;
1195         } else {
1196                 bit = wvif->link_id_map;
1197         }
1198         prev = wvif->sta_asleep_mask & bit;
1199
1200         switch (notify_cmd) {
1201         case STA_NOTIFY_SLEEP:
1202                 if (!prev) {
1203                         if (wvif->mcast_buffered && !wvif->sta_asleep_mask)
1204                                 schedule_work(&wvif->mcast_start_work);
1205                         wvif->sta_asleep_mask |= bit;
1206                 }
1207                 break;
1208         case STA_NOTIFY_AWAKE:
1209                 if (prev) {
1210                         wvif->sta_asleep_mask &= ~bit;
1211                         wvif->pspoll_mask &= ~bit;
1212                         if (link_id && !wvif->sta_asleep_mask)
1213                                 schedule_work(&wvif->mcast_stop_work);
1214                         wfx_bh_request_tx(wvif->wdev);
1215                 }
1216                 break;
1217         }
1218         spin_unlock_bh(&wvif->ps_state_lock);
1219 }
1220
1221 void wfx_sta_notify(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1222                     enum sta_notify_cmd notify_cmd, struct ieee80211_sta *sta)
1223 {
1224         struct wfx_vif *wvif = (struct wfx_vif *) vif->drv_priv;
1225         struct wfx_sta_priv *sta_priv = (struct wfx_sta_priv *) &sta->drv_priv;
1226
1227         wfx_ps_notify(wvif, notify_cmd, sta_priv->link_id);
1228 }
1229
1230 static int wfx_set_tim_impl(struct wfx_vif *wvif, bool aid0_bit_set)
1231 {
1232         struct sk_buff *skb;
1233         struct hif_ie_flags target_frame = {
1234                 .beacon = 1,
1235         };
1236         u16 tim_offset, tim_length;
1237         u8 *tim_ptr;
1238
1239         skb = ieee80211_beacon_get_tim(wvif->wdev->hw, wvif->vif,
1240                                        &tim_offset, &tim_length);
1241         if (!skb) {
1242                 if (!__wfx_flush(wvif->wdev, true))
1243                         wfx_tx_unlock(wvif->wdev);
1244                 return -ENOENT;
1245         }
1246         tim_ptr = skb->data + tim_offset;
1247
1248         if (tim_offset && tim_length >= 6) {
1249                 /* Ignore DTIM count from mac80211:
1250                  * firmware handles DTIM internally.
1251                  */
1252                 tim_ptr[2] = 0;
1253
1254                 /* Set/reset aid0 bit */
1255                 if (aid0_bit_set)
1256                         tim_ptr[4] |= 1;
1257                 else
1258                         tim_ptr[4] &= ~1;
1259         }
1260
1261         hif_update_ie(wvif, &target_frame, tim_ptr, tim_length);
1262         dev_kfree_skb(skb);
1263
1264         return 0;
1265 }
1266
1267 static void wfx_set_tim_work(struct work_struct *work)
1268 {
1269         struct wfx_vif *wvif = container_of(work, struct wfx_vif, set_tim_work);
1270
1271         wfx_set_tim_impl(wvif, wvif->aid0_bit_set);
1272 }
1273
1274 int wfx_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta, bool set)
1275 {
1276         struct wfx_dev *wdev = hw->priv;
1277         struct wfx_sta_priv *sta_dev = (struct wfx_sta_priv *) &sta->drv_priv;
1278         struct wfx_vif *wvif = wdev_to_wvif(wdev, sta_dev->vif_id);
1279
1280         schedule_work(&wvif->set_tim_work);
1281         return 0;
1282 }
1283
1284 static void wfx_mcast_start_work(struct work_struct *work)
1285 {
1286         struct wfx_vif *wvif = container_of(work, struct wfx_vif,
1287                                             mcast_start_work);
1288         long tmo = wvif->dtim_period * TU_TO_JIFFIES(wvif->beacon_int + 20);
1289
1290         cancel_work_sync(&wvif->mcast_stop_work);
1291         if (!wvif->aid0_bit_set) {
1292                 wfx_tx_lock_flush(wvif->wdev);
1293                 wfx_set_tim_impl(wvif, true);
1294                 wvif->aid0_bit_set = true;
1295                 mod_timer(&wvif->mcast_timeout, jiffies + tmo);
1296                 wfx_tx_unlock(wvif->wdev);
1297         }
1298 }
1299
1300 static void wfx_mcast_stop_work(struct work_struct *work)
1301 {
1302         struct wfx_vif *wvif = container_of(work, struct wfx_vif,
1303                                             mcast_stop_work);
1304
1305         if (wvif->aid0_bit_set) {
1306                 del_timer_sync(&wvif->mcast_timeout);
1307                 wfx_tx_lock_flush(wvif->wdev);
1308                 wvif->aid0_bit_set = false;
1309                 wfx_set_tim_impl(wvif, false);
1310                 wfx_tx_unlock(wvif->wdev);
1311         }
1312 }
1313
1314 static void wfx_mcast_timeout(struct timer_list *t)
1315 {
1316         struct wfx_vif *wvif = from_timer(wvif, t, mcast_timeout);
1317
1318         dev_warn(wvif->wdev->dev, "multicast delivery timeout\n");
1319         spin_lock_bh(&wvif->ps_state_lock);
1320         wvif->mcast_tx = wvif->aid0_bit_set && wvif->mcast_buffered;
1321         if (wvif->mcast_tx)
1322                 wfx_bh_request_tx(wvif->wdev);
1323         spin_unlock_bh(&wvif->ps_state_lock);
1324 }
1325
1326 int wfx_ampdu_action(struct ieee80211_hw *hw,
1327                      struct ieee80211_vif *vif,
1328                      struct ieee80211_ampdu_params *params)
1329 {
1330         /* Aggregation is implemented fully in firmware,
1331          * including block ack negotiation. Do not allow
1332          * mac80211 stack to do anything: it interferes with
1333          * the firmware.
1334          */
1335
1336         /* Note that we still need this function stubbed. */
1337
1338         return -ENOTSUPP;
1339 }
1340
1341 void wfx_suspend_resume(struct wfx_vif *wvif,
1342                         struct hif_ind_suspend_resume_tx *arg)
1343 {
1344         if (arg->suspend_resume_flags.bc_mc_only) {
1345                 bool cancel_tmo = false;
1346
1347                 spin_lock_bh(&wvif->ps_state_lock);
1348                 if (!arg->suspend_resume_flags.resume)
1349                         wvif->mcast_tx = false;
1350                 else
1351                         wvif->mcast_tx = wvif->aid0_bit_set &&
1352                                          wvif->mcast_buffered;
1353                 if (wvif->mcast_tx) {
1354                         cancel_tmo = true;
1355                         wfx_bh_request_tx(wvif->wdev);
1356                 }
1357                 spin_unlock_bh(&wvif->ps_state_lock);
1358                 if (cancel_tmo)
1359                         del_timer_sync(&wvif->mcast_timeout);
1360         } else if (arg->suspend_resume_flags.resume) {
1361                 // FIXME: should change each station status independently
1362                 wfx_ps_notify(wvif, STA_NOTIFY_AWAKE, 0);
1363                 wfx_bh_request_tx(wvif->wdev);
1364         } else {
1365                 // FIXME: should change each station status independently
1366                 wfx_ps_notify(wvif, STA_NOTIFY_SLEEP, 0);
1367         }
1368 }
1369
1370 int wfx_add_chanctx(struct ieee80211_hw *hw,
1371                     struct ieee80211_chanctx_conf *conf)
1372 {
1373         return 0;
1374 }
1375
1376 void wfx_remove_chanctx(struct ieee80211_hw *hw,
1377                         struct ieee80211_chanctx_conf *conf)
1378 {
1379 }
1380
1381 void wfx_change_chanctx(struct ieee80211_hw *hw,
1382                         struct ieee80211_chanctx_conf *conf,
1383                         u32 changed)
1384 {
1385 }
1386
1387 int wfx_assign_vif_chanctx(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1388                            struct ieee80211_chanctx_conf *conf)
1389 {
1390         struct wfx_vif *wvif = (struct wfx_vif *) vif->drv_priv;
1391         struct ieee80211_channel *ch = conf->def.chan;
1392
1393         WARN(wvif->channel, "channel overwrite");
1394         wvif->channel = ch;
1395         wvif->ht_info.channel_type = cfg80211_get_chandef_type(&conf->def);
1396
1397         return 0;
1398 }
1399
1400 void wfx_unassign_vif_chanctx(struct ieee80211_hw *hw,
1401                               struct ieee80211_vif *vif,
1402                               struct ieee80211_chanctx_conf *conf)
1403 {
1404         struct wfx_vif *wvif = (struct wfx_vif *) vif->drv_priv;
1405         struct ieee80211_channel *ch = conf->def.chan;
1406
1407         WARN(wvif->channel != ch, "channel mismatch");
1408         wvif->channel = NULL;
1409 }
1410
1411 int wfx_config(struct ieee80211_hw *hw, u32 changed)
1412 {
1413         int ret = 0;
1414         struct wfx_dev *wdev = hw->priv;
1415         struct ieee80211_conf *conf = &hw->conf;
1416         struct wfx_vif *wvif;
1417
1418         // FIXME: Interface id should not been hardcoded
1419         wvif = wdev_to_wvif(wdev, 0);
1420         if (!wvif) {
1421                 WARN(1, "interface 0 does not exist anymore");
1422                 return 0;
1423         }
1424
1425         down(&wvif->scan.lock);
1426         mutex_lock(&wdev->conf_mutex);
1427         if (changed & IEEE80211_CONF_CHANGE_POWER) {
1428                 wdev->output_power = conf->power_level;
1429                 hif_set_output_power(wvif, wdev->output_power * 10);
1430         }
1431
1432         if (changed & IEEE80211_CONF_CHANGE_PS) {
1433                 wvif = NULL;
1434                 while ((wvif = wvif_iterate(wdev, wvif)) != NULL) {
1435                         memset(&wvif->powersave_mode, 0,
1436                                sizeof(wvif->powersave_mode));
1437                         if (conf->flags & IEEE80211_CONF_PS) {
1438                                 wvif->powersave_mode.pm_mode.enter_psm = 1;
1439                                 if (conf->dynamic_ps_timeout > 0) {
1440                                         wvif->powersave_mode.pm_mode.fast_psm = 1;
1441                                         /*
1442                                          * Firmware does not support more than
1443                                          * 128ms
1444                                          */
1445                                         wvif->powersave_mode.fast_psm_idle_period =
1446                                                 min(conf->dynamic_ps_timeout *
1447                                                     2, 255);
1448                                 }
1449                         }
1450                         if (wvif->state == WFX_STATE_STA && wvif->bss_params.aid)
1451                                 wfx_set_pm(wvif, &wvif->powersave_mode);
1452                 }
1453                 wvif = wdev_to_wvif(wdev, 0);
1454         }
1455
1456         mutex_unlock(&wdev->conf_mutex);
1457         up(&wvif->scan.lock);
1458         return ret;
1459 }
1460
1461 int wfx_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1462 {
1463         int i;
1464         struct wfx_dev *wdev = hw->priv;
1465         struct wfx_vif *wvif = (struct wfx_vif *) vif->drv_priv;
1466         // FIXME: parameters are set by kernel juste after interface_add.
1467         // Keep struct hif_req_edca_queue_params blank?
1468         struct hif_req_edca_queue_params default_edca_params[] = {
1469                 [IEEE80211_AC_VO] = {
1470                         .queue_id = HIF_QUEUE_ID_VOICE,
1471                         .aifsn = 2,
1472                         .cw_min = 3,
1473                         .cw_max = 7,
1474                         .tx_op_limit = TXOP_UNIT * 47,
1475                 },
1476                 [IEEE80211_AC_VI] = {
1477                         .queue_id = HIF_QUEUE_ID_VIDEO,
1478                         .aifsn = 2,
1479                         .cw_min = 7,
1480                         .cw_max = 15,
1481                         .tx_op_limit = TXOP_UNIT * 94,
1482                 },
1483                 [IEEE80211_AC_BE] = {
1484                         .queue_id = HIF_QUEUE_ID_BESTEFFORT,
1485                         .aifsn = 3,
1486                         .cw_min = 15,
1487                         .cw_max = 1023,
1488                         .tx_op_limit = TXOP_UNIT * 0,
1489                 },
1490                 [IEEE80211_AC_BK] = {
1491                         .queue_id = HIF_QUEUE_ID_BACKGROUND,
1492                         .aifsn = 7,
1493                         .cw_min = 15,
1494                         .cw_max = 1023,
1495                         .tx_op_limit = TXOP_UNIT * 0,
1496                 },
1497         };
1498
1499         BUILD_BUG_ON(ARRAY_SIZE(default_edca_params) != ARRAY_SIZE(wvif->edca.params));
1500         if (wfx_api_older_than(wdev, 2, 0)) {
1501                 default_edca_params[IEEE80211_AC_BE].queue_id = HIF_QUEUE_ID_BACKGROUND;
1502                 default_edca_params[IEEE80211_AC_BK].queue_id = HIF_QUEUE_ID_BESTEFFORT;
1503         }
1504
1505         vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
1506                              IEEE80211_VIF_SUPPORTS_UAPSD |
1507                              IEEE80211_VIF_SUPPORTS_CQM_RSSI;
1508
1509         mutex_lock(&wdev->conf_mutex);
1510
1511         switch (vif->type) {
1512         case NL80211_IFTYPE_STATION:
1513         case NL80211_IFTYPE_ADHOC:
1514         case NL80211_IFTYPE_AP:
1515                 break;
1516         default:
1517                 mutex_unlock(&wdev->conf_mutex);
1518                 return -EOPNOTSUPP;
1519         }
1520
1521         for (i = 0; i < ARRAY_SIZE(wdev->vif); i++) {
1522                 if (!wdev->vif[i]) {
1523                         wdev->vif[i] = vif;
1524                         wvif->id = i;
1525                         break;
1526                 }
1527         }
1528         if (i == ARRAY_SIZE(wdev->vif)) {
1529                 mutex_unlock(&wdev->conf_mutex);
1530                 return -EOPNOTSUPP;
1531         }
1532         // FIXME: prefer use of container_of() to get vif
1533         wvif->vif = vif;
1534         wvif->wdev = wdev;
1535
1536         INIT_WORK(&wvif->link_id_work, wfx_link_id_work);
1537         INIT_DELAYED_WORK(&wvif->link_id_gc_work, wfx_link_id_gc_work);
1538
1539         spin_lock_init(&wvif->ps_state_lock);
1540         INIT_WORK(&wvif->set_tim_work, wfx_set_tim_work);
1541
1542         INIT_WORK(&wvif->mcast_start_work, wfx_mcast_start_work);
1543         INIT_WORK(&wvif->mcast_stop_work, wfx_mcast_stop_work);
1544         timer_setup(&wvif->mcast_timeout, wfx_mcast_timeout, 0);
1545
1546         wvif->setbssparams_done = false;
1547         mutex_init(&wvif->bss_loss_lock);
1548         INIT_DELAYED_WORK(&wvif->bss_loss_work, wfx_bss_loss_work);
1549
1550         wvif->wep_default_key_id = -1;
1551         INIT_WORK(&wvif->wep_key_work, wfx_wep_key_work);
1552
1553         sema_init(&wvif->scan.lock, 1);
1554         INIT_WORK(&wvif->scan.work, wfx_scan_work);
1555         INIT_DELAYED_WORK(&wvif->scan.timeout, wfx_scan_timeout);
1556
1557         spin_lock_init(&wvif->event_queue_lock);
1558         INIT_LIST_HEAD(&wvif->event_queue);
1559         INIT_WORK(&wvif->event_handler_work, wfx_event_handler_work);
1560
1561         init_completion(&wvif->set_pm_mode_complete);
1562         complete(&wvif->set_pm_mode_complete);
1563         INIT_WORK(&wvif->set_beacon_wakeup_period_work,
1564                   wfx_set_beacon_wakeup_period_work);
1565         INIT_WORK(&wvif->update_filtering_work, wfx_update_filtering_work);
1566         INIT_WORK(&wvif->bss_params_work, wfx_bss_params_work);
1567         INIT_WORK(&wvif->set_cts_work, wfx_set_cts_work);
1568         INIT_WORK(&wvif->unjoin_work, wfx_unjoin_work);
1569
1570         mutex_unlock(&wdev->conf_mutex);
1571
1572         hif_set_macaddr(wvif, vif->addr);
1573         for (i = 0; i < IEEE80211_NUM_ACS; i++) {
1574                 memcpy(&wvif->edca.params[i], &default_edca_params[i],
1575                        sizeof(default_edca_params[i]));
1576                 wvif->edca.uapsd_enable[i] = false;
1577                 hif_set_edca_queue_params(wvif, &wvif->edca.params[i]);
1578         }
1579         wfx_set_uapsd_param(wvif, &wvif->edca);
1580
1581         wfx_tx_policy_init(wvif);
1582         wvif = NULL;
1583         while ((wvif = wvif_iterate(wdev, wvif)) != NULL) {
1584                 // Combo mode does not support Block Acks. We can re-enable them
1585                 if (wvif_count(wdev) == 1)
1586                         hif_set_block_ack_policy(wvif, 0xFF, 0xFF);
1587                 else
1588                         hif_set_block_ack_policy(wvif, 0x00, 0x00);
1589                 // Combo force powersave mode. We can re-enable it now
1590                 wfx_set_pm(wvif, &wvif->powersave_mode);
1591         }
1592         return 0;
1593 }
1594
1595 void wfx_remove_interface(struct ieee80211_hw *hw,
1596                           struct ieee80211_vif *vif)
1597 {
1598         struct wfx_dev *wdev = hw->priv;
1599         struct wfx_vif *wvif = (struct wfx_vif *) vif->drv_priv;
1600         int i;
1601
1602         // If scan is in progress, stop it
1603         while (down_trylock(&wvif->scan.lock))
1604                 schedule();
1605         up(&wvif->scan.lock);
1606         wait_for_completion_timeout(&wvif->set_pm_mode_complete, msecs_to_jiffies(300));
1607
1608         mutex_lock(&wdev->conf_mutex);
1609         switch (wvif->state) {
1610         case WFX_STATE_PRE_STA:
1611         case WFX_STATE_STA:
1612         case WFX_STATE_IBSS:
1613                 wfx_tx_lock_flush(wdev);
1614                 if (!schedule_work(&wvif->unjoin_work))
1615                         wfx_tx_unlock(wdev);
1616                 break;
1617         case WFX_STATE_AP:
1618                 for (i = 0; wvif->link_id_map; ++i) {
1619                         if (wvif->link_id_map & BIT(i)) {
1620                                 wfx_unmap_link(wvif, i);
1621                                 wvif->link_id_map &= ~BIT(i);
1622                         }
1623                 }
1624                 memset(wvif->link_id_db, 0, sizeof(wvif->link_id_db));
1625                 wvif->sta_asleep_mask = 0;
1626                 wvif->enable_beacon = false;
1627                 wvif->mcast_tx = false;
1628                 wvif->aid0_bit_set = false;
1629                 wvif->mcast_buffered = false;
1630                 wvif->pspoll_mask = 0;
1631                 /* reset.link_id = 0; */
1632                 hif_reset(wvif, false);
1633                 break;
1634         default:
1635                 break;
1636         }
1637
1638         wvif->state = WFX_STATE_PASSIVE;
1639         wfx_tx_queues_wait_empty_vif(wvif);
1640         wfx_tx_unlock(wdev);
1641
1642         /* FIXME: In add to reset MAC address, try to reset interface */
1643         hif_set_macaddr(wvif, NULL);
1644
1645         cancel_delayed_work_sync(&wvif->scan.timeout);
1646
1647         wfx_cqm_bssloss_sm(wvif, 0, 0, 0);
1648         cancel_work_sync(&wvif->unjoin_work);
1649         cancel_delayed_work_sync(&wvif->link_id_gc_work);
1650         del_timer_sync(&wvif->mcast_timeout);
1651         wfx_free_event_queue(wvif);
1652
1653         wdev->vif[wvif->id] = NULL;
1654         wvif->vif = NULL;
1655
1656         mutex_unlock(&wdev->conf_mutex);
1657         wvif = NULL;
1658         while ((wvif = wvif_iterate(wdev, wvif)) != NULL) {
1659                 // Combo mode does not support Block Acks. We can re-enable them
1660                 if (wvif_count(wdev) == 1)
1661                         hif_set_block_ack_policy(wvif, 0xFF, 0xFF);
1662                 else
1663                         hif_set_block_ack_policy(wvif, 0x00, 0x00);
1664                 // Combo force powersave mode. We can re-enable it now
1665                 wfx_set_pm(wvif, &wvif->powersave_mode);
1666         }
1667 }
1668
1669 int wfx_start(struct ieee80211_hw *hw)
1670 {
1671         return 0;
1672 }
1673
1674 void wfx_stop(struct ieee80211_hw *hw)
1675 {
1676         struct wfx_dev *wdev = hw->priv;
1677
1678         wfx_tx_lock_flush(wdev);
1679         mutex_lock(&wdev->conf_mutex);
1680         wfx_tx_queues_clear(wdev);
1681         mutex_unlock(&wdev->conf_mutex);
1682         wfx_tx_unlock(wdev);
1683         WARN(atomic_read(&wdev->tx_lock), "tx_lock is locked");
1684 }