GNU Linux-libre 4.14.332-gnu1
[releases.git] / drivers / net / wireless / rsi / rsi_91x_mac80211.c
1 /**
2  * Copyright (c) 2014 Redpine Signals Inc.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include <linux/etherdevice.h>
18 #include "rsi_debugfs.h"
19 #include "rsi_mgmt.h"
20 #include "rsi_common.h"
21 #include "rsi_ps.h"
22
23 static const struct ieee80211_channel rsi_2ghz_channels[] = {
24         { .band = NL80211_BAND_2GHZ, .center_freq = 2412,
25           .hw_value = 1 }, /* Channel 1 */
26         { .band = NL80211_BAND_2GHZ, .center_freq = 2417,
27           .hw_value = 2 }, /* Channel 2 */
28         { .band = NL80211_BAND_2GHZ, .center_freq = 2422,
29           .hw_value = 3 }, /* Channel 3 */
30         { .band = NL80211_BAND_2GHZ, .center_freq = 2427,
31           .hw_value = 4 }, /* Channel 4 */
32         { .band = NL80211_BAND_2GHZ, .center_freq = 2432,
33           .hw_value = 5 }, /* Channel 5 */
34         { .band = NL80211_BAND_2GHZ, .center_freq = 2437,
35           .hw_value = 6 }, /* Channel 6 */
36         { .band = NL80211_BAND_2GHZ, .center_freq = 2442,
37           .hw_value = 7 }, /* Channel 7 */
38         { .band = NL80211_BAND_2GHZ, .center_freq = 2447,
39           .hw_value = 8 }, /* Channel 8 */
40         { .band = NL80211_BAND_2GHZ, .center_freq = 2452,
41           .hw_value = 9 }, /* Channel 9 */
42         { .band = NL80211_BAND_2GHZ, .center_freq = 2457,
43           .hw_value = 10 }, /* Channel 10 */
44         { .band = NL80211_BAND_2GHZ, .center_freq = 2462,
45           .hw_value = 11 }, /* Channel 11 */
46         { .band = NL80211_BAND_2GHZ, .center_freq = 2467,
47           .hw_value = 12 }, /* Channel 12 */
48         { .band = NL80211_BAND_2GHZ, .center_freq = 2472,
49           .hw_value = 13 }, /* Channel 13 */
50         { .band = NL80211_BAND_2GHZ, .center_freq = 2484,
51           .hw_value = 14 }, /* Channel 14 */
52 };
53
54 static const struct ieee80211_channel rsi_5ghz_channels[] = {
55         { .band = NL80211_BAND_5GHZ, .center_freq = 5180,
56           .hw_value = 36,  }, /* Channel 36 */
57         { .band = NL80211_BAND_5GHZ, .center_freq = 5200,
58           .hw_value = 40, }, /* Channel 40 */
59         { .band = NL80211_BAND_5GHZ, .center_freq = 5220,
60           .hw_value = 44, }, /* Channel 44 */
61         { .band = NL80211_BAND_5GHZ, .center_freq = 5240,
62           .hw_value = 48, }, /* Channel 48 */
63         { .band = NL80211_BAND_5GHZ, .center_freq = 5260,
64           .hw_value = 52, }, /* Channel 52 */
65         { .band = NL80211_BAND_5GHZ, .center_freq = 5280,
66           .hw_value = 56, }, /* Channel 56 */
67         { .band = NL80211_BAND_5GHZ, .center_freq = 5300,
68           .hw_value = 60, }, /* Channel 60 */
69         { .band = NL80211_BAND_5GHZ, .center_freq = 5320,
70           .hw_value = 64, }, /* Channel 64 */
71         { .band = NL80211_BAND_5GHZ, .center_freq = 5500,
72           .hw_value = 100, }, /* Channel 100 */
73         { .band = NL80211_BAND_5GHZ, .center_freq = 5520,
74           .hw_value = 104, }, /* Channel 104 */
75         { .band = NL80211_BAND_5GHZ, .center_freq = 5540,
76           .hw_value = 108, }, /* Channel 108 */
77         { .band = NL80211_BAND_5GHZ, .center_freq = 5560,
78           .hw_value = 112, }, /* Channel 112 */
79         { .band = NL80211_BAND_5GHZ, .center_freq = 5580,
80           .hw_value = 116, }, /* Channel 116 */
81         { .band = NL80211_BAND_5GHZ, .center_freq = 5600,
82           .hw_value = 120, }, /* Channel 120 */
83         { .band = NL80211_BAND_5GHZ, .center_freq = 5620,
84           .hw_value = 124, }, /* Channel 124 */
85         { .band = NL80211_BAND_5GHZ, .center_freq = 5640,
86           .hw_value = 128, }, /* Channel 128 */
87         { .band = NL80211_BAND_5GHZ, .center_freq = 5660,
88           .hw_value = 132, }, /* Channel 132 */
89         { .band = NL80211_BAND_5GHZ, .center_freq = 5680,
90           .hw_value = 136, }, /* Channel 136 */
91         { .band = NL80211_BAND_5GHZ, .center_freq = 5700,
92           .hw_value = 140, }, /* Channel 140 */
93         { .band = NL80211_BAND_5GHZ, .center_freq = 5745,
94           .hw_value = 149, }, /* Channel 149 */
95         { .band = NL80211_BAND_5GHZ, .center_freq = 5765,
96           .hw_value = 153, }, /* Channel 153 */
97         { .band = NL80211_BAND_5GHZ, .center_freq = 5785,
98           .hw_value = 157, }, /* Channel 157 */
99         { .band = NL80211_BAND_5GHZ, .center_freq = 5805,
100           .hw_value = 161, }, /* Channel 161 */
101         { .band = NL80211_BAND_5GHZ, .center_freq = 5825,
102           .hw_value = 165, }, /* Channel 165 */
103 };
104
105 struct ieee80211_rate rsi_rates[12] = {
106         { .bitrate = STD_RATE_01  * 5, .hw_value = RSI_RATE_1 },
107         { .bitrate = STD_RATE_02  * 5, .hw_value = RSI_RATE_2 },
108         { .bitrate = STD_RATE_5_5 * 5, .hw_value = RSI_RATE_5_5 },
109         { .bitrate = STD_RATE_11  * 5, .hw_value = RSI_RATE_11 },
110         { .bitrate = STD_RATE_06  * 5, .hw_value = RSI_RATE_6 },
111         { .bitrate = STD_RATE_09  * 5, .hw_value = RSI_RATE_9 },
112         { .bitrate = STD_RATE_12  * 5, .hw_value = RSI_RATE_12 },
113         { .bitrate = STD_RATE_18  * 5, .hw_value = RSI_RATE_18 },
114         { .bitrate = STD_RATE_24  * 5, .hw_value = RSI_RATE_24 },
115         { .bitrate = STD_RATE_36  * 5, .hw_value = RSI_RATE_36 },
116         { .bitrate = STD_RATE_48  * 5, .hw_value = RSI_RATE_48 },
117         { .bitrate = STD_RATE_54  * 5, .hw_value = RSI_RATE_54 },
118 };
119
120 const u16 rsi_mcsrates[8] = {
121         RSI_RATE_MCS0, RSI_RATE_MCS1, RSI_RATE_MCS2, RSI_RATE_MCS3,
122         RSI_RATE_MCS4, RSI_RATE_MCS5, RSI_RATE_MCS6, RSI_RATE_MCS7
123 };
124
125 static const u32 rsi_max_ap_stas[16] = {
126         32,     /* 1 - Wi-Fi alone */
127         0,      /* 2 */
128         0,      /* 3 */
129         0,      /* 4 - BT EDR alone */
130         4,      /* 5 - STA + BT EDR */
131         32,     /* 6 - AP + BT EDR */
132         0,      /* 7 */
133         0,      /* 8 - BT LE alone */
134         4,      /* 9 - STA + BE LE */
135         0,      /* 10 */
136         0,      /* 11 */
137         0,      /* 12 */
138         1,      /* 13 - STA + BT Dual */
139         4,      /* 14 - AP + BT Dual */
140 };
141
142 /**
143  * rsi_is_cipher_wep() -  This function determines if the cipher is WEP or not.
144  * @common: Pointer to the driver private structure.
145  *
146  * Return: If cipher type is WEP, a value of 1 is returned, else 0.
147  */
148
149 bool rsi_is_cipher_wep(struct rsi_common *common)
150 {
151         if (((common->secinfo.gtk_cipher == WLAN_CIPHER_SUITE_WEP104) ||
152              (common->secinfo.gtk_cipher == WLAN_CIPHER_SUITE_WEP40)) &&
153             (!common->secinfo.ptk_cipher))
154                 return true;
155         else
156                 return false;
157 }
158
159 /**
160  * rsi_register_rates_channels() - This function registers channels and rates.
161  * @adapter: Pointer to the adapter structure.
162  * @band: Operating band to be set.
163  *
164  * Return: None.
165  */
166 static void rsi_register_rates_channels(struct rsi_hw *adapter, int band)
167 {
168         struct ieee80211_supported_band *sbands = &adapter->sbands[band];
169         void *channels = NULL;
170
171         if (band == NL80211_BAND_2GHZ) {
172                 channels = kmalloc(sizeof(rsi_2ghz_channels), GFP_KERNEL);
173                 memcpy(channels,
174                        rsi_2ghz_channels,
175                        sizeof(rsi_2ghz_channels));
176                 sbands->band = NL80211_BAND_2GHZ;
177                 sbands->n_channels = ARRAY_SIZE(rsi_2ghz_channels);
178                 sbands->bitrates = rsi_rates;
179                 sbands->n_bitrates = ARRAY_SIZE(rsi_rates);
180         } else {
181                 channels = kmalloc(sizeof(rsi_5ghz_channels), GFP_KERNEL);
182                 memcpy(channels,
183                        rsi_5ghz_channels,
184                        sizeof(rsi_5ghz_channels));
185                 sbands->band = NL80211_BAND_5GHZ;
186                 sbands->n_channels = ARRAY_SIZE(rsi_5ghz_channels);
187                 sbands->bitrates = &rsi_rates[4];
188                 sbands->n_bitrates = ARRAY_SIZE(rsi_rates) - 4;
189         }
190
191         sbands->channels = channels;
192
193         memset(&sbands->ht_cap, 0, sizeof(struct ieee80211_sta_ht_cap));
194         sbands->ht_cap.ht_supported = true;
195         sbands->ht_cap.cap = (IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
196                               IEEE80211_HT_CAP_SGI_20 |
197                               IEEE80211_HT_CAP_SGI_40);
198         sbands->ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_16K;
199         sbands->ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
200         sbands->ht_cap.mcs.rx_mask[0] = 0xff;
201         sbands->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
202         /* sbands->ht_cap.mcs.rx_highest = 0x82; */
203 }
204
205 /**
206  * rsi_mac80211_detach() - This function is used to de-initialize the
207  *                         Mac80211 stack.
208  * @adapter: Pointer to the adapter structure.
209  *
210  * Return: None.
211  */
212 void rsi_mac80211_detach(struct rsi_hw *adapter)
213 {
214         struct ieee80211_hw *hw = adapter->hw;
215         enum nl80211_band band;
216
217         if (hw) {
218                 ieee80211_stop_queues(hw);
219                 ieee80211_unregister_hw(hw);
220                 ieee80211_free_hw(hw);
221                 adapter->hw = NULL;
222         }
223
224         for (band = 0; band < NUM_NL80211_BANDS; band++) {
225                 struct ieee80211_supported_band *sband =
226                                         &adapter->sbands[band];
227
228                 kfree(sband->channels);
229         }
230
231 #ifdef CONFIG_RSI_DEBUGFS
232         rsi_remove_dbgfs(adapter);
233         kfree(adapter->dfsentry);
234 #endif
235 }
236 EXPORT_SYMBOL_GPL(rsi_mac80211_detach);
237
238 /**
239  * rsi_indicate_tx_status() - This function indicates the transmit status.
240  * @adapter: Pointer to the adapter structure.
241  * @skb: Pointer to the socket buffer structure.
242  * @status: Status
243  *
244  * Return: None.
245  */
246 void rsi_indicate_tx_status(struct rsi_hw *adapter,
247                             struct sk_buff *skb,
248                             int status)
249 {
250         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
251         struct skb_info *tx_params;
252
253         if (!adapter->hw) {
254                 rsi_dbg(ERR_ZONE, "##### No MAC #####\n");
255                 return;
256         }
257
258         if (!status)
259                 info->flags |= IEEE80211_TX_STAT_ACK;
260
261         tx_params = (struct skb_info *)info->driver_data;
262         skb_pull(skb, tx_params->internal_hdr_size);
263         memset(info->driver_data, 0, IEEE80211_TX_INFO_DRIVER_DATA_SIZE);
264
265         ieee80211_tx_status_irqsafe(adapter->hw, skb);
266 }
267
268 /**
269  * rsi_mac80211_tx() - This is the handler that 802.11 module calls for each
270  *                     transmitted frame.SKB contains the buffer starting
271  *                     from the IEEE 802.11 header.
272  * @hw: Pointer to the ieee80211_hw structure.
273  * @control: Pointer to the ieee80211_tx_control structure
274  * @skb: Pointer to the socket buffer structure.
275  *
276  * Return: None
277  */
278 static void rsi_mac80211_tx(struct ieee80211_hw *hw,
279                             struct ieee80211_tx_control *control,
280                             struct sk_buff *skb)
281 {
282         struct rsi_hw *adapter = hw->priv;
283         struct rsi_common *common = adapter->priv;
284
285         rsi_core_xmit(common, skb);
286 }
287
288 /**
289  * rsi_mac80211_start() - This is first handler that 802.11 module calls, since
290  *                        the driver init is complete by then, just
291  *                        returns success.
292  * @hw: Pointer to the ieee80211_hw structure.
293  *
294  * Return: 0 as success.
295  */
296 static int rsi_mac80211_start(struct ieee80211_hw *hw)
297 {
298         struct rsi_hw *adapter = hw->priv;
299         struct rsi_common *common = adapter->priv;
300
301         rsi_dbg(ERR_ZONE, "===> Interface UP <===\n");
302         mutex_lock(&common->mutex);
303         common->iface_down = false;
304         wiphy_rfkill_start_polling(hw->wiphy);
305         rsi_send_rx_filter_frame(common, 0);
306         mutex_unlock(&common->mutex);
307
308         return 0;
309 }
310
311 /**
312  * rsi_mac80211_stop() - This is the last handler that 802.11 module calls.
313  * @hw: Pointer to the ieee80211_hw structure.
314  *
315  * Return: None.
316  */
317 static void rsi_mac80211_stop(struct ieee80211_hw *hw)
318 {
319         struct rsi_hw *adapter = hw->priv;
320         struct rsi_common *common = adapter->priv;
321
322         rsi_dbg(ERR_ZONE, "===> Interface DOWN <===\n");
323         mutex_lock(&common->mutex);
324         common->iface_down = true;
325         wiphy_rfkill_stop_polling(hw->wiphy);
326
327         /* Block all rx frames */
328         rsi_send_rx_filter_frame(common, 0xffff);
329
330         mutex_unlock(&common->mutex);
331 }
332
333 /**
334  * rsi_mac80211_add_interface() - This function is called when a netdevice
335  *                                attached to the hardware is enabled.
336  * @hw: Pointer to the ieee80211_hw structure.
337  * @vif: Pointer to the ieee80211_vif structure.
338  *
339  * Return: ret: 0 on success, negative error code on failure.
340  */
341 static int rsi_mac80211_add_interface(struct ieee80211_hw *hw,
342                                       struct ieee80211_vif *vif)
343 {
344         struct rsi_hw *adapter = hw->priv;
345         struct rsi_common *common = adapter->priv;
346         enum opmode intf_mode;
347         int ret = -EOPNOTSUPP;
348
349         vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
350         mutex_lock(&common->mutex);
351
352         if (adapter->sc_nvifs > 1) {
353                 mutex_unlock(&common->mutex);
354                 return -EOPNOTSUPP;
355         }
356
357         switch (vif->type) {
358         case NL80211_IFTYPE_STATION:
359                 rsi_dbg(INFO_ZONE, "Station Mode");
360                 intf_mode = STA_OPMODE;
361                 break;
362         case NL80211_IFTYPE_AP:
363                 rsi_dbg(INFO_ZONE, "AP Mode");
364                 intf_mode = AP_OPMODE;
365                 break;
366         default:
367                 rsi_dbg(ERR_ZONE,
368                         "%s: Interface type %d not supported\n", __func__,
369                         vif->type);
370                 goto out;
371         }
372
373         adapter->vifs[adapter->sc_nvifs++] = vif;
374         ret = rsi_set_vap_capabilities(common, intf_mode, common->mac_addr,
375                                        0, VAP_ADD);
376         if (ret) {
377                 rsi_dbg(ERR_ZONE, "Failed to set VAP capabilities\n");
378                 goto out;
379         }
380
381         if (vif->type == NL80211_IFTYPE_AP) {
382                 int i;
383
384                 rsi_send_rx_filter_frame(common, DISALLOW_BEACONS);
385                 common->min_rate = RSI_RATE_AUTO;
386                 for (i = 0; i < common->max_stations; i++)
387                         common->stations[i].sta = NULL;
388         }
389
390 out:
391         mutex_unlock(&common->mutex);
392
393         return ret;
394 }
395
396 /**
397  * rsi_mac80211_remove_interface() - This function notifies driver that an
398  *                                   interface is going down.
399  * @hw: Pointer to the ieee80211_hw structure.
400  * @vif: Pointer to the ieee80211_vif structure.
401  *
402  * Return: None.
403  */
404 static void rsi_mac80211_remove_interface(struct ieee80211_hw *hw,
405                                           struct ieee80211_vif *vif)
406 {
407         struct rsi_hw *adapter = hw->priv;
408         struct rsi_common *common = adapter->priv;
409         enum opmode opmode;
410
411         rsi_dbg(INFO_ZONE, "Remove Interface Called\n");
412
413         mutex_lock(&common->mutex);
414
415         if (adapter->sc_nvifs <= 0) {
416                 mutex_unlock(&common->mutex);
417                 return;
418         }
419
420         switch (vif->type) {
421         case NL80211_IFTYPE_STATION:
422                 opmode = STA_OPMODE;
423                 break;
424         case NL80211_IFTYPE_AP:
425                 opmode = AP_OPMODE;
426                 break;
427         default:
428                 mutex_unlock(&common->mutex);
429                 return;
430         }
431         rsi_set_vap_capabilities(common, opmode, vif->addr,
432                                  0, VAP_DELETE);
433         adapter->sc_nvifs--;
434
435         if (!memcmp(adapter->vifs[0], vif, sizeof(struct ieee80211_vif)))
436                 adapter->vifs[0] = NULL;
437         mutex_unlock(&common->mutex);
438 }
439
440 /**
441  * rsi_channel_change() - This function is a performs the checks
442  *                        required for changing a channel and sets
443  *                        the channel accordingly.
444  * @hw: Pointer to the ieee80211_hw structure.
445  *
446  * Return: 0 on success, negative error code on failure.
447  */
448 static int rsi_channel_change(struct ieee80211_hw *hw)
449 {
450         struct rsi_hw *adapter = hw->priv;
451         struct rsi_common *common = adapter->priv;
452         int status = -EOPNOTSUPP;
453         struct ieee80211_channel *curchan = hw->conf.chandef.chan;
454         u16 channel = curchan->hw_value;
455         struct ieee80211_bss_conf *bss = &adapter->vifs[0]->bss_conf;
456
457         rsi_dbg(INFO_ZONE,
458                 "%s: Set channel: %d MHz type: %d channel_no %d\n",
459                 __func__, curchan->center_freq,
460                 curchan->flags, channel);
461
462         if (bss->assoc) {
463                 if (!common->hw_data_qs_blocked &&
464                     (rsi_get_connected_channel(adapter) != channel)) {
465                         rsi_dbg(INFO_ZONE, "blk data q %d\n", channel);
466                         if (!rsi_send_block_unblock_frame(common, true))
467                                 common->hw_data_qs_blocked = true;
468                 }
469         }
470
471         status = rsi_band_check(common);
472         if (!status)
473                 status = rsi_set_channel(adapter->priv, curchan);
474
475         if (bss->assoc) {
476                 if (common->hw_data_qs_blocked &&
477                     (rsi_get_connected_channel(adapter) == channel)) {
478                         rsi_dbg(INFO_ZONE, "unblk data q %d\n", channel);
479                         if (!rsi_send_block_unblock_frame(common, false))
480                                 common->hw_data_qs_blocked = false;
481                 }
482         } else {
483                 if (common->hw_data_qs_blocked) {
484                         rsi_dbg(INFO_ZONE, "unblk data q %d\n", channel);
485                         if (!rsi_send_block_unblock_frame(common, false))
486                                 common->hw_data_qs_blocked = false;
487                 }
488         }
489
490         return status;
491 }
492
493 /**
494  * rsi_config_power() - This function configures tx power to device
495  * @hw: Pointer to the ieee80211_hw structure.
496  *
497  * Return: 0 on success, negative error code on failure.
498  */
499 static int rsi_config_power(struct ieee80211_hw *hw)
500 {
501         struct rsi_hw *adapter = hw->priv;
502         struct rsi_common *common = adapter->priv;
503         struct ieee80211_conf *conf = &hw->conf;
504
505         if (adapter->sc_nvifs <= 0) {
506                 rsi_dbg(ERR_ZONE, "%s: No virtual interface found\n", __func__);
507                 return -EINVAL;
508         }
509
510         rsi_dbg(INFO_ZONE,
511                 "%s: Set tx power: %d dBM\n", __func__, conf->power_level);
512
513         if (conf->power_level == common->tx_power)
514                 return 0;
515
516         common->tx_power = conf->power_level;
517
518         return rsi_send_radio_params_update(common);
519 }
520
521 /**
522  * rsi_mac80211_config() - This function is a handler for configuration
523  *                         requests. The stack calls this function to
524  *                         change hardware configuration, e.g., channel.
525  * @hw: Pointer to the ieee80211_hw structure.
526  * @changed: Changed flags set.
527  *
528  * Return: 0 on success, negative error code on failure.
529  */
530 static int rsi_mac80211_config(struct ieee80211_hw *hw,
531                                u32 changed)
532 {
533         struct rsi_hw *adapter = hw->priv;
534         struct rsi_common *common = adapter->priv;
535         struct ieee80211_vif *vif = adapter->vifs[0];
536         struct ieee80211_conf *conf = &hw->conf;
537         int status = -EOPNOTSUPP;
538
539         mutex_lock(&common->mutex);
540
541         if (changed & IEEE80211_CONF_CHANGE_CHANNEL)
542                 status = rsi_channel_change(hw);
543
544         /* tx power */
545         if (changed & IEEE80211_CONF_CHANGE_POWER) {
546                 rsi_dbg(INFO_ZONE, "%s: Configuring Power\n", __func__);
547                 status = rsi_config_power(hw);
548         }
549
550         /* Power save parameters */
551         if ((changed & IEEE80211_CONF_CHANGE_PS) &&
552             (vif->type == NL80211_IFTYPE_STATION)) {
553                 unsigned long flags;
554
555                 spin_lock_irqsave(&adapter->ps_lock, flags);
556                 if (conf->flags & IEEE80211_CONF_PS)
557                         rsi_enable_ps(adapter);
558                 else
559                         rsi_disable_ps(adapter);
560                 spin_unlock_irqrestore(&adapter->ps_lock, flags);
561         }
562
563         /* RTS threshold */
564         if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
565                 rsi_dbg(INFO_ZONE, "RTS threshold\n");
566                 if ((common->rts_threshold) <= IEEE80211_MAX_RTS_THRESHOLD) {
567                         rsi_dbg(INFO_ZONE,
568                                 "%s: Sending vap updates....\n", __func__);
569                         status = rsi_send_vap_dynamic_update(common);
570                 }
571         }
572         mutex_unlock(&common->mutex);
573
574         return status;
575 }
576
577 /**
578  * rsi_get_connected_channel() - This function is used to get the current
579  *                               connected channel number.
580  * @adapter: Pointer to the adapter structure.
581  *
582  * Return: Current connected AP's channel number is returned.
583  */
584 u16 rsi_get_connected_channel(struct rsi_hw *adapter)
585 {
586         struct ieee80211_vif *vif = adapter->vifs[0];
587         if (vif) {
588                 struct ieee80211_bss_conf *bss = &vif->bss_conf;
589                 struct ieee80211_channel *channel = bss->chandef.chan;
590                 return channel->hw_value;
591         }
592
593         return 0;
594 }
595
596 /**
597  * rsi_mac80211_bss_info_changed() - This function is a handler for config
598  *                                   requests related to BSS parameters that
599  *                                   may vary during BSS's lifespan.
600  * @hw: Pointer to the ieee80211_hw structure.
601  * @vif: Pointer to the ieee80211_vif structure.
602  * @bss_conf: Pointer to the ieee80211_bss_conf structure.
603  * @changed: Changed flags set.
604  *
605  * Return: None.
606  */
607 static void rsi_mac80211_bss_info_changed(struct ieee80211_hw *hw,
608                                           struct ieee80211_vif *vif,
609                                           struct ieee80211_bss_conf *bss_conf,
610                                           u32 changed)
611 {
612         struct rsi_hw *adapter = hw->priv;
613         struct rsi_common *common = adapter->priv;
614         struct ieee80211_bss_conf *bss = &vif->bss_conf;
615         struct ieee80211_conf *conf = &hw->conf;
616         u16 rx_filter_word = 0;
617
618         mutex_lock(&common->mutex);
619         if (changed & BSS_CHANGED_ASSOC) {
620                 rsi_dbg(INFO_ZONE, "%s: Changed Association status: %d\n",
621                         __func__, bss_conf->assoc);
622                 if (bss_conf->assoc) {
623                         /* Send the RX filter frame */
624                         rx_filter_word = (ALLOW_DATA_ASSOC_PEER |
625                                           ALLOW_CTRL_ASSOC_PEER |
626                                           ALLOW_MGMT_ASSOC_PEER);
627                         rsi_send_rx_filter_frame(common, rx_filter_word);
628                 }
629                 rsi_inform_bss_status(common,
630                                       STA_OPMODE,
631                                       bss_conf->assoc,
632                                       bss_conf->bssid,
633                                       bss_conf->qos,
634                                       bss_conf->aid,
635                                       NULL, 0);
636                 adapter->ps_info.dtim_interval_duration = bss->dtim_period;
637                 adapter->ps_info.listen_interval = conf->listen_interval;
638
639         /* If U-APSD is updated, send ps parameters to firmware */
640         if (bss->assoc) {
641                 if (common->uapsd_bitmap) {
642                         rsi_dbg(INFO_ZONE, "Configuring UAPSD\n");
643                         rsi_conf_uapsd(adapter);
644                 }
645         } else {
646                 common->uapsd_bitmap = 0;
647         }
648         }
649
650         if (changed & BSS_CHANGED_CQM) {
651                 common->cqm_info.last_cqm_event_rssi = 0;
652                 common->cqm_info.rssi_thold = bss_conf->cqm_rssi_thold;
653                 common->cqm_info.rssi_hyst = bss_conf->cqm_rssi_hyst;
654                 rsi_dbg(INFO_ZONE, "RSSI throld & hysteresis are: %d %d\n",
655                         common->cqm_info.rssi_thold,
656                         common->cqm_info.rssi_hyst);
657         }
658
659         if ((changed & BSS_CHANGED_BEACON_ENABLED) &&
660             (vif->type == NL80211_IFTYPE_AP)) {
661                 if (bss->enable_beacon) {
662                         rsi_dbg(INFO_ZONE, "===> BEACON ENABLED <===\n");
663                         common->beacon_enabled = 1;
664                 } else {
665                         rsi_dbg(INFO_ZONE, "===> BEACON DISABLED <===\n");
666                         common->beacon_enabled = 0;
667                 }
668         }
669
670         mutex_unlock(&common->mutex);
671 }
672
673 /**
674  * rsi_mac80211_conf_filter() - This function configure the device's RX filter.
675  * @hw: Pointer to the ieee80211_hw structure.
676  * @changed: Changed flags set.
677  * @total_flags: Total initial flags set.
678  * @multicast: Multicast.
679  *
680  * Return: None.
681  */
682 static void rsi_mac80211_conf_filter(struct ieee80211_hw *hw,
683                                      u32 changed_flags,
684                                      u32 *total_flags,
685                                      u64 multicast)
686 {
687         /* Not doing much here as of now */
688         *total_flags &= RSI_SUPP_FILTERS;
689 }
690
691 /**
692  * rsi_mac80211_conf_tx() - This function configures TX queue parameters
693  *                          (EDCF (aifs, cw_min, cw_max), bursting)
694  *                          for a hardware TX queue.
695  * @hw: Pointer to the ieee80211_hw structure
696  * @vif: Pointer to the ieee80211_vif structure.
697  * @queue: Queue number.
698  * @params: Pointer to ieee80211_tx_queue_params structure.
699  *
700  * Return: 0 on success, negative error code on failure.
701  */
702 static int rsi_mac80211_conf_tx(struct ieee80211_hw *hw,
703                                 struct ieee80211_vif *vif, u16 queue,
704                                 const struct ieee80211_tx_queue_params *params)
705 {
706         struct rsi_hw *adapter = hw->priv;
707         struct rsi_common *common = adapter->priv;
708         u8 idx = 0;
709
710         if (queue >= IEEE80211_NUM_ACS)
711                 return 0;
712
713         rsi_dbg(INFO_ZONE,
714                 "%s: Conf queue %d, aifs: %d, cwmin: %d cwmax: %d, txop: %d\n",
715                 __func__, queue, params->aifs,
716                 params->cw_min, params->cw_max, params->txop);
717
718         mutex_lock(&common->mutex);
719         /* Map into the way the f/w expects */
720         switch (queue) {
721         case IEEE80211_AC_VO:
722                 idx = VO_Q;
723                 break;
724         case IEEE80211_AC_VI:
725                 idx = VI_Q;
726                 break;
727         case IEEE80211_AC_BE:
728                 idx = BE_Q;
729                 break;
730         case IEEE80211_AC_BK:
731                 idx = BK_Q;
732                 break;
733         default:
734                 idx = BE_Q;
735                 break;
736         }
737
738         memcpy(&common->edca_params[idx],
739                params,
740                sizeof(struct ieee80211_tx_queue_params));
741
742         if (params->uapsd)
743                 common->uapsd_bitmap |= idx;
744         else
745                 common->uapsd_bitmap &= (~idx);
746
747         mutex_unlock(&common->mutex);
748
749         return 0;
750 }
751
752 /**
753  * rsi_hal_key_config() - This function loads the keys into the firmware.
754  * @hw: Pointer to the ieee80211_hw structure.
755  * @vif: Pointer to the ieee80211_vif structure.
756  * @key: Pointer to the ieee80211_key_conf structure.
757  *
758  * Return: status: 0 on success, negative error codes on failure.
759  */
760 static int rsi_hal_key_config(struct ieee80211_hw *hw,
761                               struct ieee80211_vif *vif,
762                               struct ieee80211_key_conf *key,
763                               struct ieee80211_sta *sta)
764 {
765         struct rsi_hw *adapter = hw->priv;
766         struct rsi_sta *rsta = NULL;
767         int status;
768         u8 key_type;
769         s16 sta_id = 0;
770
771         if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
772                 key_type = RSI_PAIRWISE_KEY;
773         else
774                 key_type = RSI_GROUP_KEY;
775
776         rsi_dbg(ERR_ZONE, "%s: Cipher 0x%x key_type: %d key_len: %d\n",
777                 __func__, key->cipher, key_type, key->keylen);
778
779         if (vif->type == NL80211_IFTYPE_AP) {
780                 if (sta) {
781                         rsta = rsi_find_sta(adapter->priv, sta->addr);
782                         if (rsta)
783                                 sta_id = rsta->sta_id;
784                 }
785                 adapter->priv->key = key;
786         } else {
787                 if ((key->cipher == WLAN_CIPHER_SUITE_WEP104) ||
788                     (key->cipher == WLAN_CIPHER_SUITE_WEP40)) {
789                         status = rsi_hal_load_key(adapter->priv,
790                                                   key->key,
791                                                   key->keylen,
792                                                   RSI_PAIRWISE_KEY,
793                                                   key->keyidx,
794                                                   key->cipher,
795                                                   sta_id);
796                         if (status)
797                                 return status;
798                 }
799         }
800
801         return rsi_hal_load_key(adapter->priv,
802                                 key->key,
803                                 key->keylen,
804                                 key_type,
805                                 key->keyidx,
806                                 key->cipher,
807                                 sta_id);
808 }
809
810 /**
811  * rsi_mac80211_set_key() - This function sets type of key to be loaded.
812  * @hw: Pointer to the ieee80211_hw structure.
813  * @cmd: enum set_key_cmd.
814  * @vif: Pointer to the ieee80211_vif structure.
815  * @sta: Pointer to the ieee80211_sta structure.
816  * @key: Pointer to the ieee80211_key_conf structure.
817  *
818  * Return: status: 0 on success, negative error code on failure.
819  */
820 static int rsi_mac80211_set_key(struct ieee80211_hw *hw,
821                                 enum set_key_cmd cmd,
822                                 struct ieee80211_vif *vif,
823                                 struct ieee80211_sta *sta,
824                                 struct ieee80211_key_conf *key)
825 {
826         struct rsi_hw *adapter = hw->priv;
827         struct rsi_common *common = adapter->priv;
828         struct security_info *secinfo = &common->secinfo;
829         int status;
830
831         mutex_lock(&common->mutex);
832         switch (cmd) {
833         case SET_KEY:
834                 secinfo->security_enable = true;
835                 status = rsi_hal_key_config(hw, vif, key, sta);
836                 if (status) {
837                         mutex_unlock(&common->mutex);
838                         return status;
839                 }
840
841                 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
842                         secinfo->ptk_cipher = key->cipher;
843                 else
844                         secinfo->gtk_cipher = key->cipher;
845
846                 key->hw_key_idx = key->keyidx;
847                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
848
849                 rsi_dbg(ERR_ZONE, "%s: RSI set_key\n", __func__);
850                 break;
851
852         case DISABLE_KEY:
853                 if (vif->type == NL80211_IFTYPE_STATION)
854                         secinfo->security_enable = false;
855                 rsi_dbg(ERR_ZONE, "%s: RSI del key\n", __func__);
856                 memset(key, 0, sizeof(struct ieee80211_key_conf));
857                 status = rsi_hal_key_config(hw, vif, key, sta);
858                 break;
859
860         default:
861                 status = -EOPNOTSUPP;
862                 break;
863         }
864
865         mutex_unlock(&common->mutex);
866         return status;
867 }
868
869 /**
870  * rsi_mac80211_ampdu_action() - This function selects the AMPDU action for
871  *                               the corresponding mlme_action flag and
872  *                               informs the f/w regarding this.
873  * @hw: Pointer to the ieee80211_hw structure.
874  * @vif: Pointer to the ieee80211_vif structure.
875  * @params: Pointer to A-MPDU action parameters
876  *
877  * Return: status: 0 on success, negative error code on failure.
878  */
879 static int rsi_mac80211_ampdu_action(struct ieee80211_hw *hw,
880                                      struct ieee80211_vif *vif,
881                                      struct ieee80211_ampdu_params *params)
882 {
883         int status = -EOPNOTSUPP;
884         struct rsi_hw *adapter = hw->priv;
885         struct rsi_common *common = adapter->priv;
886         struct rsi_sta *rsta = NULL;
887         u16 seq_no = 0, seq_start = 0;
888         u8 ii = 0;
889         struct ieee80211_sta *sta = params->sta;
890         u8 sta_id = 0;
891         enum ieee80211_ampdu_mlme_action action = params->action;
892         u16 tid = params->tid;
893         u16 *ssn = &params->ssn;
894         u8 buf_size = params->buf_size;
895
896         for (ii = 0; ii < RSI_MAX_VIFS; ii++) {
897                 if (vif == adapter->vifs[ii])
898                         break;
899         }
900
901         mutex_lock(&common->mutex);
902
903         if (ssn != NULL)
904                 seq_no = *ssn;
905
906         if (vif->type == NL80211_IFTYPE_AP) {
907                 rsta = rsi_find_sta(common, sta->addr);
908                 if (!rsta) {
909                         rsi_dbg(ERR_ZONE, "No station mapped\n");
910                         status = 0;
911                         goto unlock;
912                 }
913                 sta_id = rsta->sta_id;
914         }
915
916         rsi_dbg(INFO_ZONE,
917                 "%s: AMPDU action tid=%d ssn=0x%x, buf_size=%d sta_id=%d\n",
918                 __func__, tid, seq_no, buf_size, sta_id);
919
920         switch (action) {
921         case IEEE80211_AMPDU_RX_START:
922                 status = rsi_send_aggregation_params_frame(common,
923                                                            tid,
924                                                            seq_no,
925                                                            buf_size,
926                                                            STA_RX_ADDBA_DONE,
927                                                            sta_id);
928                 break;
929
930         case IEEE80211_AMPDU_RX_STOP:
931                 status = rsi_send_aggregation_params_frame(common,
932                                                            tid,
933                                                            0,
934                                                            buf_size,
935                                                            STA_RX_DELBA,
936                                                            sta_id);
937                 break;
938
939         case IEEE80211_AMPDU_TX_START:
940                 if (vif->type == NL80211_IFTYPE_STATION)
941                         common->vif_info[ii].seq_start = seq_no;
942                 else if (vif->type == NL80211_IFTYPE_AP)
943                         rsta->seq_start[tid] = seq_no;
944                 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
945                 status = 0;
946                 break;
947
948         case IEEE80211_AMPDU_TX_STOP_CONT:
949         case IEEE80211_AMPDU_TX_STOP_FLUSH:
950         case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
951                 status = rsi_send_aggregation_params_frame(common,
952                                                            tid,
953                                                            seq_no,
954                                                            buf_size,
955                                                            STA_TX_DELBA,
956                                                            sta_id);
957                 if (!status)
958                         ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
959                 break;
960
961         case IEEE80211_AMPDU_TX_OPERATIONAL:
962                 if (vif->type == NL80211_IFTYPE_STATION)
963                         seq_start = common->vif_info[ii].seq_start;
964                 else if (vif->type == NL80211_IFTYPE_AP)
965                         seq_start = rsta->seq_start[tid];
966                 status = rsi_send_aggregation_params_frame(common,
967                                                            tid,
968                                                            seq_start,
969                                                            buf_size,
970                                                            STA_TX_ADDBA_DONE,
971                                                            sta_id);
972                 break;
973
974         default:
975                 rsi_dbg(ERR_ZONE, "%s: Uknown AMPDU action\n", __func__);
976                 break;
977         }
978
979 unlock:
980         mutex_unlock(&common->mutex);
981         return status;
982 }
983
984 /**
985  * rsi_mac80211_set_rts_threshold() - This function sets rts threshold value.
986  * @hw: Pointer to the ieee80211_hw structure.
987  * @value: Rts threshold value.
988  *
989  * Return: 0 on success.
990  */
991 static int rsi_mac80211_set_rts_threshold(struct ieee80211_hw *hw,
992                                           u32 value)
993 {
994         struct rsi_hw *adapter = hw->priv;
995         struct rsi_common *common = adapter->priv;
996
997         mutex_lock(&common->mutex);
998         common->rts_threshold = value;
999         mutex_unlock(&common->mutex);
1000
1001         return 0;
1002 }
1003
1004 /**
1005  * rsi_mac80211_set_rate_mask() - This function sets bitrate_mask to be used.
1006  * @hw: Pointer to the ieee80211_hw structure
1007  * @vif: Pointer to the ieee80211_vif structure.
1008  * @mask: Pointer to the cfg80211_bitrate_mask structure.
1009  *
1010  * Return: 0 on success.
1011  */
1012 static int rsi_mac80211_set_rate_mask(struct ieee80211_hw *hw,
1013                                       struct ieee80211_vif *vif,
1014                                       const struct cfg80211_bitrate_mask *mask)
1015 {
1016         struct rsi_hw *adapter = hw->priv;
1017         struct rsi_common *common = adapter->priv;
1018         enum nl80211_band band = hw->conf.chandef.chan->band;
1019
1020         mutex_lock(&common->mutex);
1021         common->fixedrate_mask[band] = 0;
1022
1023         if (mask->control[band].legacy == 0xfff) {
1024                 common->fixedrate_mask[band] =
1025                         (mask->control[band].ht_mcs[0] << 12);
1026         } else {
1027                 common->fixedrate_mask[band] =
1028                         mask->control[band].legacy;
1029         }
1030         mutex_unlock(&common->mutex);
1031
1032         return 0;
1033 }
1034
1035 /**
1036  * rsi_perform_cqm() - This function performs cqm.
1037  * @common: Pointer to the driver private structure.
1038  * @bssid: pointer to the bssid.
1039  * @rssi: RSSI value.
1040  */
1041 static void rsi_perform_cqm(struct rsi_common *common,
1042                             u8 *bssid,
1043                             s8 rssi)
1044 {
1045         struct rsi_hw *adapter = common->priv;
1046         s8 last_event = common->cqm_info.last_cqm_event_rssi;
1047         int thold = common->cqm_info.rssi_thold;
1048         u32 hyst = common->cqm_info.rssi_hyst;
1049         enum nl80211_cqm_rssi_threshold_event event;
1050
1051         if (rssi < thold && (last_event == 0 || rssi < (last_event - hyst)))
1052                 event = NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW;
1053         else if (rssi > thold &&
1054                  (last_event == 0 || rssi > (last_event + hyst)))
1055                 event = NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH;
1056         else
1057                 return;
1058
1059         common->cqm_info.last_cqm_event_rssi = rssi;
1060         rsi_dbg(INFO_ZONE, "CQM: Notifying event: %d\n", event);
1061         ieee80211_cqm_rssi_notify(adapter->vifs[0], event, rssi, GFP_KERNEL);
1062
1063         return;
1064 }
1065
1066 /**
1067  * rsi_fill_rx_status() - This function fills rx status in
1068  *                        ieee80211_rx_status structure.
1069  * @hw: Pointer to the ieee80211_hw structure.
1070  * @skb: Pointer to the socket buffer structure.
1071  * @common: Pointer to the driver private structure.
1072  * @rxs: Pointer to the ieee80211_rx_status structure.
1073  *
1074  * Return: None.
1075  */
1076 static void rsi_fill_rx_status(struct ieee80211_hw *hw,
1077                                struct sk_buff *skb,
1078                                struct rsi_common *common,
1079                                struct ieee80211_rx_status *rxs)
1080 {
1081         struct ieee80211_bss_conf *bss = &common->priv->vifs[0]->bss_conf;
1082         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1083         struct skb_info *rx_params = (struct skb_info *)info->driver_data;
1084         struct ieee80211_hdr *hdr;
1085         char rssi = rx_params->rssi;
1086         u8 hdrlen = 0;
1087         u8 channel = rx_params->channel;
1088         s32 freq;
1089
1090         hdr = ((struct ieee80211_hdr *)(skb->data));
1091         hdrlen = ieee80211_hdrlen(hdr->frame_control);
1092
1093         memset(info, 0, sizeof(struct ieee80211_tx_info));
1094
1095         rxs->signal = -(rssi);
1096
1097         rxs->band = common->band;
1098
1099         freq = ieee80211_channel_to_frequency(channel, rxs->band);
1100
1101         if (freq)
1102                 rxs->freq = freq;
1103
1104         if (ieee80211_has_protected(hdr->frame_control)) {
1105                 if (rsi_is_cipher_wep(common)) {
1106                         memmove(skb->data + 4, skb->data, hdrlen);
1107                         skb_pull(skb, 4);
1108                 } else {
1109                         memmove(skb->data + 8, skb->data, hdrlen);
1110                         skb_pull(skb, 8);
1111                         rxs->flag |= RX_FLAG_MMIC_STRIPPED;
1112                 }
1113                 rxs->flag |= RX_FLAG_DECRYPTED;
1114                 rxs->flag |= RX_FLAG_IV_STRIPPED;
1115         }
1116
1117         /* CQM only for connected AP beacons, the RSSI is a weighted avg */
1118         if (bss->assoc && !(memcmp(bss->bssid, hdr->addr2, ETH_ALEN))) {
1119                 if (ieee80211_is_beacon(hdr->frame_control))
1120                         rsi_perform_cqm(common, hdr->addr2, rxs->signal);
1121         }
1122
1123         return;
1124 }
1125
1126 /**
1127  * rsi_indicate_pkt_to_os() - This function sends recieved packet to mac80211.
1128  * @common: Pointer to the driver private structure.
1129  * @skb: Pointer to the socket buffer structure.
1130  *
1131  * Return: None.
1132  */
1133 void rsi_indicate_pkt_to_os(struct rsi_common *common,
1134                             struct sk_buff *skb)
1135 {
1136         struct rsi_hw *adapter = common->priv;
1137         struct ieee80211_hw *hw = adapter->hw;
1138         struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
1139
1140         if ((common->iface_down) || (!adapter->sc_nvifs)) {
1141                 dev_kfree_skb(skb);
1142                 return;
1143         }
1144
1145         /* filling in the ieee80211_rx_status flags */
1146         rsi_fill_rx_status(hw, skb, common, rx_status);
1147
1148         ieee80211_rx_irqsafe(hw, skb);
1149 }
1150
1151 static void rsi_set_min_rate(struct ieee80211_hw *hw,
1152                              struct ieee80211_sta *sta,
1153                              struct rsi_common *common)
1154 {
1155         u8 band = hw->conf.chandef.chan->band;
1156         u8 ii;
1157         u32 rate_bitmap;
1158         bool matched = false;
1159
1160         common->bitrate_mask[band] = sta->supp_rates[band];
1161
1162         rate_bitmap = (common->fixedrate_mask[band] & sta->supp_rates[band]);
1163
1164         if (rate_bitmap & 0xfff) {
1165                 /* Find out the min rate */
1166                 for (ii = 0; ii < ARRAY_SIZE(rsi_rates); ii++) {
1167                         if (rate_bitmap & BIT(ii)) {
1168                                 common->min_rate = rsi_rates[ii].hw_value;
1169                                 matched = true;
1170                                 break;
1171                         }
1172                 }
1173         }
1174
1175         common->vif_info[0].is_ht = sta->ht_cap.ht_supported;
1176
1177         if ((common->vif_info[0].is_ht) && (rate_bitmap >> 12)) {
1178                 for (ii = 0; ii < ARRAY_SIZE(rsi_mcsrates); ii++) {
1179                         if ((rate_bitmap >> 12) & BIT(ii)) {
1180                                 common->min_rate = rsi_mcsrates[ii];
1181                                 matched = true;
1182                                 break;
1183                         }
1184                 }
1185         }
1186
1187         if (!matched)
1188                 common->min_rate = 0xffff;
1189 }
1190
1191 /**
1192  * rsi_mac80211_sta_add() - This function notifies driver about a peer getting
1193  *                          connected.
1194  * @hw: pointer to the ieee80211_hw structure.
1195  * @vif: Pointer to the ieee80211_vif structure.
1196  * @sta: Pointer to the ieee80211_sta structure.
1197  *
1198  * Return: 0 on success, negative error codes on failure.
1199  */
1200 static int rsi_mac80211_sta_add(struct ieee80211_hw *hw,
1201                                 struct ieee80211_vif *vif,
1202                                 struct ieee80211_sta *sta)
1203 {
1204         struct rsi_hw *adapter = hw->priv;
1205         struct rsi_common *common = adapter->priv;
1206         bool sta_exist = false;
1207         struct rsi_sta *rsta;
1208         int status = 0;
1209
1210         rsi_dbg(INFO_ZONE, "Station Add: %pM\n", sta->addr);
1211
1212         mutex_lock(&common->mutex);
1213
1214         if (vif->type == NL80211_IFTYPE_AP) {
1215                 u8 cnt;
1216                 int sta_idx = -1;
1217                 int free_index = -1;
1218
1219                 /* Check if max stations reached */
1220                 if (common->num_stations >= common->max_stations) {
1221                         rsi_dbg(ERR_ZONE, "Reject: Max Stations exists\n");
1222                         status = -EOPNOTSUPP;
1223                         goto unlock;
1224                 }
1225                 for (cnt = 0; cnt < common->max_stations; cnt++) {
1226                         rsta = &common->stations[cnt];
1227
1228                         if (!rsta->sta) {
1229                                 if (free_index < 0)
1230                                         free_index = cnt;
1231                                 continue;
1232                         }
1233                         if (!memcmp(rsta->sta->addr, sta->addr, ETH_ALEN)) {
1234                                 rsi_dbg(INFO_ZONE, "Station exists\n");
1235                                 sta_idx = cnt;
1236                                 sta_exist = true;
1237                                 break;
1238                         }
1239                 }
1240                 if (!sta_exist) {
1241                         if (free_index >= 0)
1242                                 sta_idx = free_index;
1243                 }
1244                 if (sta_idx < 0) {
1245                         rsi_dbg(ERR_ZONE,
1246                                 "%s: Some problem reaching here...\n",
1247                                 __func__);
1248                         status = -EINVAL;
1249                         goto unlock;
1250                 }
1251                 rsta = &common->stations[sta_idx];
1252                 rsta->sta = sta;
1253                 rsta->sta_id = sta_idx;
1254                 for (cnt = 0; cnt < IEEE80211_NUM_TIDS; cnt++)
1255                         rsta->start_tx_aggr[cnt] = false;
1256                 for (cnt = 0; cnt < IEEE80211_NUM_TIDS; cnt++)
1257                         rsta->seq_start[cnt] = 0;
1258                 if (!sta_exist) {
1259                         rsi_dbg(INFO_ZONE, "New Station\n");
1260
1261                         /* Send peer notify to device */
1262                         rsi_dbg(INFO_ZONE, "Indicate bss status to device\n");
1263                         rsi_inform_bss_status(common, AP_OPMODE, 1, sta->addr,
1264                                               sta->wme, sta->aid, sta, sta_idx);
1265
1266                         if (common->key) {
1267                                 struct ieee80211_key_conf *key = common->key;
1268
1269                                 if ((key->cipher == WLAN_CIPHER_SUITE_WEP104) ||
1270                                     (key->cipher == WLAN_CIPHER_SUITE_WEP40))
1271                                         rsi_hal_load_key(adapter->priv,
1272                                                          key->key,
1273                                                          key->keylen,
1274                                                          RSI_PAIRWISE_KEY,
1275                                                          key->keyidx,
1276                                                          key->cipher,
1277                                                          sta_idx);
1278                         }
1279
1280                         common->num_stations++;
1281                 }
1282         }
1283
1284         if (vif->type == NL80211_IFTYPE_STATION) {
1285                 rsi_set_min_rate(hw, sta, common);
1286                 if (sta->ht_cap.ht_supported) {
1287                         common->vif_info[0].is_ht = true;
1288                         common->bitrate_mask[NL80211_BAND_2GHZ] =
1289                                         sta->supp_rates[NL80211_BAND_2GHZ];
1290                         if ((sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20) ||
1291                             (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40))
1292                                 common->vif_info[0].sgi = true;
1293                         ieee80211_start_tx_ba_session(sta, 0, 0);
1294                 }
1295         }
1296
1297 unlock:
1298         mutex_unlock(&common->mutex);
1299
1300         return status;
1301 }
1302
1303 /**
1304  * rsi_mac80211_sta_remove() - This function notifies driver about a peer
1305  *                             getting disconnected.
1306  * @hw: Pointer to the ieee80211_hw structure.
1307  * @vif: Pointer to the ieee80211_vif structure.
1308  * @sta: Pointer to the ieee80211_sta structure.
1309  *
1310  * Return: 0 on success, negative error codes on failure.
1311  */
1312 static int rsi_mac80211_sta_remove(struct ieee80211_hw *hw,
1313                                    struct ieee80211_vif *vif,
1314                                    struct ieee80211_sta *sta)
1315 {
1316         struct rsi_hw *adapter = hw->priv;
1317         struct rsi_common *common = adapter->priv;
1318         struct ieee80211_bss_conf *bss = &vif->bss_conf;
1319         struct rsi_sta *rsta;
1320
1321         rsi_dbg(INFO_ZONE, "Station Remove: %pM\n", sta->addr);
1322
1323         mutex_lock(&common->mutex);
1324
1325         if (vif->type == NL80211_IFTYPE_AP) {
1326                 u8 sta_idx, cnt;
1327
1328                 /* Send peer notify to device */
1329                 rsi_dbg(INFO_ZONE, "Indicate bss status to device\n");
1330                 for (sta_idx = 0; sta_idx < common->max_stations; sta_idx++) {
1331                         rsta = &common->stations[sta_idx];
1332
1333                         if (!rsta->sta)
1334                                 continue;
1335                         if (!memcmp(rsta->sta->addr, sta->addr, ETH_ALEN)) {
1336                                 rsi_inform_bss_status(common, AP_OPMODE, 0,
1337                                                       sta->addr, sta->wme,
1338                                                       sta->aid, sta, sta_idx);
1339                                 rsta->sta = NULL;
1340                                 rsta->sta_id = -1;
1341                                 for (cnt = 0; cnt < IEEE80211_NUM_TIDS; cnt++)
1342                                         rsta->start_tx_aggr[cnt] = false;
1343                                 if (common->num_stations > 0)
1344                                         common->num_stations--;
1345                                 break;
1346                         }
1347                 }
1348                 if (sta_idx >= common->max_stations)
1349                         rsi_dbg(ERR_ZONE, "%s: No station found\n", __func__);
1350         }
1351
1352         if (vif->type == NL80211_IFTYPE_STATION) {
1353                 /* Resetting all the fields to default values */
1354                 memcpy((u8 *)bss->bssid, (u8 *)sta->addr, ETH_ALEN);
1355                 bss->qos = sta->wme;
1356                 common->bitrate_mask[NL80211_BAND_2GHZ] = 0;
1357                 common->bitrate_mask[NL80211_BAND_5GHZ] = 0;
1358                 common->min_rate = 0xffff;
1359                 common->vif_info[0].is_ht = false;
1360                 common->vif_info[0].sgi = false;
1361                 common->vif_info[0].seq_start = 0;
1362                 common->secinfo.ptk_cipher = 0;
1363                 common->secinfo.gtk_cipher = 0;
1364                 if (!common->iface_down)
1365                         rsi_send_rx_filter_frame(common, 0);
1366         }
1367         mutex_unlock(&common->mutex);
1368         
1369         return 0;
1370 }
1371
1372 /**
1373  * rsi_mac80211_set_antenna() - This function is used to configure
1374  *                              tx and rx antennas.
1375  * @hw: Pointer to the ieee80211_hw structure.
1376  * @tx_ant: Bitmap for tx antenna
1377  * @rx_ant: Bitmap for rx antenna
1378  *
1379  * Return: 0 on success, Negative error code on failure.
1380  */
1381 static int rsi_mac80211_set_antenna(struct ieee80211_hw *hw,
1382                                     u32 tx_ant, u32 rx_ant)
1383 {
1384         struct rsi_hw *adapter = hw->priv;
1385         struct rsi_common *common = adapter->priv;
1386         u8 antenna = 0;
1387
1388         if (tx_ant > 1 || rx_ant > 1) {
1389                 rsi_dbg(ERR_ZONE,
1390                         "Invalid antenna selection (tx: %d, rx:%d)\n",
1391                         tx_ant, rx_ant);
1392                 rsi_dbg(ERR_ZONE,
1393                         "Use 0 for int_ant, 1 for ext_ant\n");
1394                 return -EINVAL; 
1395         }
1396
1397         rsi_dbg(INFO_ZONE, "%s: Antenna map Tx %x Rx %d\n",
1398                         __func__, tx_ant, rx_ant);
1399
1400         mutex_lock(&common->mutex);
1401
1402         antenna = tx_ant ? ANTENNA_SEL_UFL : ANTENNA_SEL_INT;
1403         if (common->ant_in_use != antenna)
1404                 if (rsi_set_antenna(common, antenna))
1405                         goto fail_set_antenna;
1406
1407         rsi_dbg(INFO_ZONE, "(%s) Antenna path configured successfully\n",
1408                 tx_ant ? "UFL" : "INT");
1409
1410         common->ant_in_use = antenna;
1411         
1412         mutex_unlock(&common->mutex);
1413         
1414         return 0;
1415
1416 fail_set_antenna:
1417         rsi_dbg(ERR_ZONE, "%s: Failed.\n", __func__);
1418         mutex_unlock(&common->mutex);
1419         return -EINVAL;
1420 }
1421
1422 /**
1423  * rsi_mac80211_get_antenna() - This function is used to configure 
1424  *                              tx and rx antennas.
1425  *
1426  * @hw: Pointer to the ieee80211_hw structure.
1427  * @tx_ant: Bitmap for tx antenna
1428  * @rx_ant: Bitmap for rx antenna
1429  * 
1430  * Return: 0 on success, negative error codes on failure.
1431  */
1432 static int rsi_mac80211_get_antenna(struct ieee80211_hw *hw,
1433                                     u32 *tx_ant, u32 *rx_ant)
1434 {
1435         struct rsi_hw *adapter = hw->priv;
1436         struct rsi_common *common = adapter->priv;
1437
1438         mutex_lock(&common->mutex);
1439
1440         *tx_ant = (common->ant_in_use == ANTENNA_SEL_UFL) ? 1 : 0;
1441         *rx_ant = 0;
1442
1443         mutex_unlock(&common->mutex);
1444         
1445         return 0;       
1446 }
1447
1448 static int rsi_map_region_code(enum nl80211_dfs_regions region_code)
1449 {
1450         switch (region_code) {
1451         case NL80211_DFS_FCC:
1452                 return RSI_REGION_FCC;
1453         case NL80211_DFS_ETSI:
1454                 return RSI_REGION_ETSI;
1455         case NL80211_DFS_JP:
1456                 return RSI_REGION_TELEC;
1457         case NL80211_DFS_UNSET:
1458                 return RSI_REGION_WORLD;
1459         }
1460         return RSI_REGION_WORLD;
1461 }
1462
1463 static void rsi_reg_notify(struct wiphy *wiphy,
1464                            struct regulatory_request *request)
1465 {
1466         struct ieee80211_supported_band *sband;
1467         struct ieee80211_channel *ch;
1468         struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
1469         struct rsi_hw * adapter = hw->priv; 
1470         struct rsi_common *common = adapter->priv;
1471         int i;
1472         
1473         mutex_lock(&common->mutex);
1474
1475         rsi_dbg(INFO_ZONE, "country = %s dfs_region = %d\n",
1476                 request->alpha2, request->dfs_region);
1477
1478         if (common->num_supp_bands > 1) {
1479                 sband = wiphy->bands[NL80211_BAND_5GHZ];
1480
1481                 for (i = 0; i < sband->n_channels; i++) {
1482                         ch = &sband->channels[i];
1483                         if (ch->flags & IEEE80211_CHAN_DISABLED)
1484                                 continue;
1485
1486                         if (ch->flags & IEEE80211_CHAN_RADAR)
1487                                 ch->flags |= IEEE80211_CHAN_NO_IR;
1488                 }
1489         }
1490         adapter->dfs_region = rsi_map_region_code(request->dfs_region);
1491         rsi_dbg(INFO_ZONE, "RSI region code = %d\n", adapter->dfs_region);
1492         
1493         adapter->country[0] = request->alpha2[0];
1494         adapter->country[1] = request->alpha2[1];
1495
1496         mutex_unlock(&common->mutex);
1497 }
1498
1499 static void rsi_mac80211_rfkill_poll(struct ieee80211_hw *hw)
1500 {
1501         struct rsi_hw *adapter = hw->priv;
1502         struct rsi_common *common = adapter->priv;
1503
1504         mutex_lock(&common->mutex);
1505         if (common->fsm_state != FSM_MAC_INIT_DONE)
1506                 wiphy_rfkill_set_hw_state(hw->wiphy, true);
1507         else
1508                 wiphy_rfkill_set_hw_state(hw->wiphy, false);
1509         mutex_unlock(&common->mutex);
1510 }
1511
1512 static const struct ieee80211_ops mac80211_ops = {
1513         .tx = rsi_mac80211_tx,
1514         .start = rsi_mac80211_start,
1515         .stop = rsi_mac80211_stop,
1516         .add_interface = rsi_mac80211_add_interface,
1517         .remove_interface = rsi_mac80211_remove_interface,
1518         .config = rsi_mac80211_config,
1519         .bss_info_changed = rsi_mac80211_bss_info_changed,
1520         .conf_tx = rsi_mac80211_conf_tx,
1521         .configure_filter = rsi_mac80211_conf_filter,
1522         .set_key = rsi_mac80211_set_key,
1523         .set_rts_threshold = rsi_mac80211_set_rts_threshold,
1524         .set_bitrate_mask = rsi_mac80211_set_rate_mask,
1525         .ampdu_action = rsi_mac80211_ampdu_action,
1526         .sta_add = rsi_mac80211_sta_add,
1527         .sta_remove = rsi_mac80211_sta_remove,
1528         .set_antenna = rsi_mac80211_set_antenna,
1529         .get_antenna = rsi_mac80211_get_antenna,
1530         .rfkill_poll = rsi_mac80211_rfkill_poll,
1531 };
1532
1533 /**
1534  * rsi_mac80211_attach() - This function is used to initialize Mac80211 stack.
1535  * @common: Pointer to the driver private structure.
1536  *
1537  * Return: 0 on success, negative error codes on failure.
1538  */
1539 int rsi_mac80211_attach(struct rsi_common *common)
1540 {
1541         int status = 0;
1542         struct ieee80211_hw *hw = NULL;
1543         struct wiphy *wiphy = NULL;
1544         struct rsi_hw *adapter = common->priv;
1545         u8 addr_mask[ETH_ALEN] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x3};
1546
1547         rsi_dbg(INIT_ZONE, "%s: Performing mac80211 attach\n", __func__);
1548
1549         hw = ieee80211_alloc_hw(sizeof(struct rsi_hw), &mac80211_ops);
1550         if (!hw) {
1551                 rsi_dbg(ERR_ZONE, "%s: ieee80211 hw alloc failed\n", __func__);
1552                 return -ENOMEM;
1553         }
1554
1555         wiphy = hw->wiphy;
1556
1557         SET_IEEE80211_DEV(hw, adapter->device);
1558
1559         hw->priv = adapter;
1560         adapter->hw = hw;
1561
1562         ieee80211_hw_set(hw, SIGNAL_DBM);
1563         ieee80211_hw_set(hw, HAS_RATE_CONTROL);
1564         ieee80211_hw_set(hw, AMPDU_AGGREGATION);
1565         ieee80211_hw_set(hw, SUPPORTS_PS);
1566         ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
1567
1568         hw->queues = MAX_HW_QUEUES;
1569         hw->extra_tx_headroom = RSI_NEEDED_HEADROOM;
1570
1571         hw->max_rates = 1;
1572         hw->max_rate_tries = MAX_RETRIES;
1573         hw->uapsd_queues = RSI_IEEE80211_UAPSD_QUEUES;
1574         hw->uapsd_max_sp_len = IEEE80211_WMM_IE_STA_QOSINFO_SP_ALL;
1575
1576         hw->max_tx_aggregation_subframes = 6;
1577         rsi_register_rates_channels(adapter, NL80211_BAND_2GHZ);
1578         rsi_register_rates_channels(adapter, NL80211_BAND_5GHZ);
1579         hw->rate_control_algorithm = "AARF";
1580
1581         SET_IEEE80211_PERM_ADDR(hw, common->mac_addr);
1582         ether_addr_copy(hw->wiphy->addr_mask, addr_mask);
1583
1584         wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
1585                                  BIT(NL80211_IFTYPE_AP);
1586         wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
1587         wiphy->retry_short = RETRY_SHORT;
1588         wiphy->retry_long  = RETRY_LONG;
1589         wiphy->frag_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
1590         wiphy->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
1591         wiphy->flags = 0;
1592
1593         wiphy->available_antennas_rx = 1;
1594         wiphy->available_antennas_tx = 1;
1595         wiphy->bands[NL80211_BAND_2GHZ] =
1596                 &adapter->sbands[NL80211_BAND_2GHZ];
1597         wiphy->bands[NL80211_BAND_5GHZ] =
1598                 &adapter->sbands[NL80211_BAND_5GHZ];
1599
1600         /* AP Parameters */
1601         wiphy->max_ap_assoc_sta = rsi_max_ap_stas[common->oper_mode - 1];
1602         common->max_stations = wiphy->max_ap_assoc_sta;
1603         rsi_dbg(ERR_ZONE, "Max Stations Allowed = %d\n", common->max_stations);
1604         hw->sta_data_size = sizeof(struct rsi_sta);
1605         wiphy->flags = WIPHY_FLAG_REPORTS_OBSS;
1606         wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
1607         wiphy->features |= NL80211_FEATURE_INACTIVITY_TIMER;
1608         wiphy->reg_notifier = rsi_reg_notify;
1609
1610         wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
1611
1612         status = ieee80211_register_hw(hw);
1613         if (status)
1614                 return status;
1615
1616         return rsi_init_dbgfs(adapter);
1617 }