GNU Linux-libre 4.9.284-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
22 static const struct ieee80211_channel rsi_2ghz_channels[] = {
23         { .band = NL80211_BAND_2GHZ, .center_freq = 2412,
24           .hw_value = 1 }, /* Channel 1 */
25         { .band = NL80211_BAND_2GHZ, .center_freq = 2417,
26           .hw_value = 2 }, /* Channel 2 */
27         { .band = NL80211_BAND_2GHZ, .center_freq = 2422,
28           .hw_value = 3 }, /* Channel 3 */
29         { .band = NL80211_BAND_2GHZ, .center_freq = 2427,
30           .hw_value = 4 }, /* Channel 4 */
31         { .band = NL80211_BAND_2GHZ, .center_freq = 2432,
32           .hw_value = 5 }, /* Channel 5 */
33         { .band = NL80211_BAND_2GHZ, .center_freq = 2437,
34           .hw_value = 6 }, /* Channel 6 */
35         { .band = NL80211_BAND_2GHZ, .center_freq = 2442,
36           .hw_value = 7 }, /* Channel 7 */
37         { .band = NL80211_BAND_2GHZ, .center_freq = 2447,
38           .hw_value = 8 }, /* Channel 8 */
39         { .band = NL80211_BAND_2GHZ, .center_freq = 2452,
40           .hw_value = 9 }, /* Channel 9 */
41         { .band = NL80211_BAND_2GHZ, .center_freq = 2457,
42           .hw_value = 10 }, /* Channel 10 */
43         { .band = NL80211_BAND_2GHZ, .center_freq = 2462,
44           .hw_value = 11 }, /* Channel 11 */
45         { .band = NL80211_BAND_2GHZ, .center_freq = 2467,
46           .hw_value = 12 }, /* Channel 12 */
47         { .band = NL80211_BAND_2GHZ, .center_freq = 2472,
48           .hw_value = 13 }, /* Channel 13 */
49         { .band = NL80211_BAND_2GHZ, .center_freq = 2484,
50           .hw_value = 14 }, /* Channel 14 */
51 };
52
53 static const struct ieee80211_channel rsi_5ghz_channels[] = {
54         { .band = NL80211_BAND_5GHZ, .center_freq = 5180,
55           .hw_value = 36,  }, /* Channel 36 */
56         { .band = NL80211_BAND_5GHZ, .center_freq = 5200,
57           .hw_value = 40, }, /* Channel 40 */
58         { .band = NL80211_BAND_5GHZ, .center_freq = 5220,
59           .hw_value = 44, }, /* Channel 44 */
60         { .band = NL80211_BAND_5GHZ, .center_freq = 5240,
61           .hw_value = 48, }, /* Channel 48 */
62         { .band = NL80211_BAND_5GHZ, .center_freq = 5260,
63           .hw_value = 52, }, /* Channel 52 */
64         { .band = NL80211_BAND_5GHZ, .center_freq = 5280,
65           .hw_value = 56, }, /* Channel 56 */
66         { .band = NL80211_BAND_5GHZ, .center_freq = 5300,
67           .hw_value = 60, }, /* Channel 60 */
68         { .band = NL80211_BAND_5GHZ, .center_freq = 5320,
69           .hw_value = 64, }, /* Channel 64 */
70         { .band = NL80211_BAND_5GHZ, .center_freq = 5500,
71           .hw_value = 100, }, /* Channel 100 */
72         { .band = NL80211_BAND_5GHZ, .center_freq = 5520,
73           .hw_value = 104, }, /* Channel 104 */
74         { .band = NL80211_BAND_5GHZ, .center_freq = 5540,
75           .hw_value = 108, }, /* Channel 108 */
76         { .band = NL80211_BAND_5GHZ, .center_freq = 5560,
77           .hw_value = 112, }, /* Channel 112 */
78         { .band = NL80211_BAND_5GHZ, .center_freq = 5580,
79           .hw_value = 116, }, /* Channel 116 */
80         { .band = NL80211_BAND_5GHZ, .center_freq = 5600,
81           .hw_value = 120, }, /* Channel 120 */
82         { .band = NL80211_BAND_5GHZ, .center_freq = 5620,
83           .hw_value = 124, }, /* Channel 124 */
84         { .band = NL80211_BAND_5GHZ, .center_freq = 5640,
85           .hw_value = 128, }, /* Channel 128 */
86         { .band = NL80211_BAND_5GHZ, .center_freq = 5660,
87           .hw_value = 132, }, /* Channel 132 */
88         { .band = NL80211_BAND_5GHZ, .center_freq = 5680,
89           .hw_value = 136, }, /* Channel 136 */
90         { .band = NL80211_BAND_5GHZ, .center_freq = 5700,
91           .hw_value = 140, }, /* Channel 140 */
92         { .band = NL80211_BAND_5GHZ, .center_freq = 5745,
93           .hw_value = 149, }, /* Channel 149 */
94         { .band = NL80211_BAND_5GHZ, .center_freq = 5765,
95           .hw_value = 153, }, /* Channel 153 */
96         { .band = NL80211_BAND_5GHZ, .center_freq = 5785,
97           .hw_value = 157, }, /* Channel 157 */
98         { .band = NL80211_BAND_5GHZ, .center_freq = 5805,
99           .hw_value = 161, }, /* Channel 161 */
100         { .band = NL80211_BAND_5GHZ, .center_freq = 5825,
101           .hw_value = 165, }, /* Channel 165 */
102 };
103
104 struct ieee80211_rate rsi_rates[12] = {
105         { .bitrate = STD_RATE_01  * 5, .hw_value = RSI_RATE_1 },
106         { .bitrate = STD_RATE_02  * 5, .hw_value = RSI_RATE_2 },
107         { .bitrate = STD_RATE_5_5 * 5, .hw_value = RSI_RATE_5_5 },
108         { .bitrate = STD_RATE_11  * 5, .hw_value = RSI_RATE_11 },
109         { .bitrate = STD_RATE_06  * 5, .hw_value = RSI_RATE_6 },
110         { .bitrate = STD_RATE_09  * 5, .hw_value = RSI_RATE_9 },
111         { .bitrate = STD_RATE_12  * 5, .hw_value = RSI_RATE_12 },
112         { .bitrate = STD_RATE_18  * 5, .hw_value = RSI_RATE_18 },
113         { .bitrate = STD_RATE_24  * 5, .hw_value = RSI_RATE_24 },
114         { .bitrate = STD_RATE_36  * 5, .hw_value = RSI_RATE_36 },
115         { .bitrate = STD_RATE_48  * 5, .hw_value = RSI_RATE_48 },
116         { .bitrate = STD_RATE_54  * 5, .hw_value = RSI_RATE_54 },
117 };
118
119 const u16 rsi_mcsrates[8] = {
120         RSI_RATE_MCS0, RSI_RATE_MCS1, RSI_RATE_MCS2, RSI_RATE_MCS3,
121         RSI_RATE_MCS4, RSI_RATE_MCS5, RSI_RATE_MCS6, RSI_RATE_MCS7
122 };
123
124 /**
125  * rsi_is_cipher_wep() -  This function determines if the cipher is WEP or not.
126  * @common: Pointer to the driver private structure.
127  *
128  * Return: If cipher type is WEP, a value of 1 is returned, else 0.
129  */
130
131 bool rsi_is_cipher_wep(struct rsi_common *common)
132 {
133         if (((common->secinfo.gtk_cipher == WLAN_CIPHER_SUITE_WEP104) ||
134              (common->secinfo.gtk_cipher == WLAN_CIPHER_SUITE_WEP40)) &&
135             (!common->secinfo.ptk_cipher))
136                 return true;
137         else
138                 return false;
139 }
140
141 /**
142  * rsi_register_rates_channels() - This function registers channels and rates.
143  * @adapter: Pointer to the adapter structure.
144  * @band: Operating band to be set.
145  *
146  * Return: None.
147  */
148 static void rsi_register_rates_channels(struct rsi_hw *adapter, int band)
149 {
150         struct ieee80211_supported_band *sbands = &adapter->sbands[band];
151         void *channels = NULL;
152
153         if (band == NL80211_BAND_2GHZ) {
154                 channels = kmalloc(sizeof(rsi_2ghz_channels), GFP_KERNEL);
155                 memcpy(channels,
156                        rsi_2ghz_channels,
157                        sizeof(rsi_2ghz_channels));
158                 sbands->band = NL80211_BAND_2GHZ;
159                 sbands->n_channels = ARRAY_SIZE(rsi_2ghz_channels);
160                 sbands->bitrates = rsi_rates;
161                 sbands->n_bitrates = ARRAY_SIZE(rsi_rates);
162         } else {
163                 channels = kmalloc(sizeof(rsi_5ghz_channels), GFP_KERNEL);
164                 memcpy(channels,
165                        rsi_5ghz_channels,
166                        sizeof(rsi_5ghz_channels));
167                 sbands->band = NL80211_BAND_5GHZ;
168                 sbands->n_channels = ARRAY_SIZE(rsi_5ghz_channels);
169                 sbands->bitrates = &rsi_rates[4];
170                 sbands->n_bitrates = ARRAY_SIZE(rsi_rates) - 4;
171         }
172
173         sbands->channels = channels;
174
175         memset(&sbands->ht_cap, 0, sizeof(struct ieee80211_sta_ht_cap));
176         sbands->ht_cap.ht_supported = true;
177         sbands->ht_cap.cap = (IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
178                               IEEE80211_HT_CAP_SGI_20 |
179                               IEEE80211_HT_CAP_SGI_40);
180         sbands->ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_16K;
181         sbands->ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
182         sbands->ht_cap.mcs.rx_mask[0] = 0xff;
183         sbands->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
184         /* sbands->ht_cap.mcs.rx_highest = 0x82; */
185 }
186
187 /**
188  * rsi_mac80211_detach() - This function is used to de-initialize the
189  *                         Mac80211 stack.
190  * @adapter: Pointer to the adapter structure.
191  *
192  * Return: None.
193  */
194 void rsi_mac80211_detach(struct rsi_hw *adapter)
195 {
196         struct ieee80211_hw *hw = adapter->hw;
197
198         if (hw) {
199                 ieee80211_stop_queues(hw);
200                 ieee80211_unregister_hw(hw);
201                 ieee80211_free_hw(hw);
202                 adapter->hw = NULL;
203         }
204
205         rsi_remove_dbgfs(adapter);
206 }
207 EXPORT_SYMBOL_GPL(rsi_mac80211_detach);
208
209 /**
210  * rsi_indicate_tx_status() - This function indicates the transmit status.
211  * @adapter: Pointer to the adapter structure.
212  * @skb: Pointer to the socket buffer structure.
213  * @status: Status
214  *
215  * Return: None.
216  */
217 void rsi_indicate_tx_status(struct rsi_hw *adapter,
218                             struct sk_buff *skb,
219                             int status)
220 {
221         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
222
223         memset(info->driver_data, 0, IEEE80211_TX_INFO_DRIVER_DATA_SIZE);
224
225         if (!status)
226                 info->flags |= IEEE80211_TX_STAT_ACK;
227
228         ieee80211_tx_status_irqsafe(adapter->hw, skb);
229 }
230
231 /**
232  * rsi_mac80211_tx() - This is the handler that 802.11 module calls for each
233  *                     transmitted frame.SKB contains the buffer starting
234  *                     from the IEEE 802.11 header.
235  * @hw: Pointer to the ieee80211_hw structure.
236  * @control: Pointer to the ieee80211_tx_control structure
237  * @skb: Pointer to the socket buffer structure.
238  *
239  * Return: None
240  */
241 static void rsi_mac80211_tx(struct ieee80211_hw *hw,
242                             struct ieee80211_tx_control *control,
243                             struct sk_buff *skb)
244 {
245         struct rsi_hw *adapter = hw->priv;
246         struct rsi_common *common = adapter->priv;
247
248         rsi_core_xmit(common, skb);
249 }
250
251 /**
252  * rsi_mac80211_start() - This is first handler that 802.11 module calls, since
253  *                        the driver init is complete by then, just
254  *                        returns success.
255  * @hw: Pointer to the ieee80211_hw structure.
256  *
257  * Return: 0 as success.
258  */
259 static int rsi_mac80211_start(struct ieee80211_hw *hw)
260 {
261         struct rsi_hw *adapter = hw->priv;
262         struct rsi_common *common = adapter->priv;
263
264         mutex_lock(&common->mutex);
265         common->iface_down = false;
266         mutex_unlock(&common->mutex);
267
268         return 0;
269 }
270
271 /**
272  * rsi_mac80211_stop() - This is the last handler that 802.11 module calls.
273  * @hw: Pointer to the ieee80211_hw structure.
274  *
275  * Return: None.
276  */
277 static void rsi_mac80211_stop(struct ieee80211_hw *hw)
278 {
279         struct rsi_hw *adapter = hw->priv;
280         struct rsi_common *common = adapter->priv;
281
282         mutex_lock(&common->mutex);
283         common->iface_down = true;
284         mutex_unlock(&common->mutex);
285 }
286
287 /**
288  * rsi_mac80211_add_interface() - This function is called when a netdevice
289  *                                attached to the hardware is enabled.
290  * @hw: Pointer to the ieee80211_hw structure.
291  * @vif: Pointer to the ieee80211_vif structure.
292  *
293  * Return: ret: 0 on success, negative error code on failure.
294  */
295 static int rsi_mac80211_add_interface(struct ieee80211_hw *hw,
296                                       struct ieee80211_vif *vif)
297 {
298         struct rsi_hw *adapter = hw->priv;
299         struct rsi_common *common = adapter->priv;
300         int ret = -EOPNOTSUPP;
301
302         mutex_lock(&common->mutex);
303         switch (vif->type) {
304         case NL80211_IFTYPE_STATION:
305                 if (!adapter->sc_nvifs) {
306                         ++adapter->sc_nvifs;
307                         adapter->vifs[0] = vif;
308                         ret = rsi_set_vap_capabilities(common, STA_OPMODE);
309                 }
310                 break;
311         default:
312                 rsi_dbg(ERR_ZONE,
313                         "%s: Interface type %d not supported\n", __func__,
314                         vif->type);
315         }
316         mutex_unlock(&common->mutex);
317
318         return ret;
319 }
320
321 /**
322  * rsi_mac80211_remove_interface() - This function notifies driver that an
323  *                                   interface is going down.
324  * @hw: Pointer to the ieee80211_hw structure.
325  * @vif: Pointer to the ieee80211_vif structure.
326  *
327  * Return: None.
328  */
329 static void rsi_mac80211_remove_interface(struct ieee80211_hw *hw,
330                                           struct ieee80211_vif *vif)
331 {
332         struct rsi_hw *adapter = hw->priv;
333         struct rsi_common *common = adapter->priv;
334
335         mutex_lock(&common->mutex);
336         if (vif->type == NL80211_IFTYPE_STATION)
337                 adapter->sc_nvifs--;
338
339         if (!memcmp(adapter->vifs[0], vif, sizeof(struct ieee80211_vif)))
340                 adapter->vifs[0] = NULL;
341         mutex_unlock(&common->mutex);
342 }
343
344 /**
345  * rsi_channel_change() - This function is a performs the checks
346  *                        required for changing a channel and sets
347  *                        the channel accordingly.
348  * @hw: Pointer to the ieee80211_hw structure.
349  *
350  * Return: 0 on success, negative error code on failure.
351  */
352 static int rsi_channel_change(struct ieee80211_hw *hw)
353 {
354         struct rsi_hw *adapter = hw->priv;
355         struct rsi_common *common = adapter->priv;
356         int status = -EOPNOTSUPP;
357         struct ieee80211_channel *curchan = hw->conf.chandef.chan;
358         u16 channel = curchan->hw_value;
359         struct ieee80211_bss_conf *bss = &adapter->vifs[0]->bss_conf;
360
361         rsi_dbg(INFO_ZONE,
362                 "%s: Set channel: %d MHz type: %d channel_no %d\n",
363                 __func__, curchan->center_freq,
364                 curchan->flags, channel);
365
366         if (bss->assoc) {
367                 if (!common->hw_data_qs_blocked &&
368                     (rsi_get_connected_channel(adapter) != channel)) {
369                         rsi_dbg(INFO_ZONE, "blk data q %d\n", channel);
370                         if (!rsi_send_block_unblock_frame(common, true))
371                                 common->hw_data_qs_blocked = true;
372                 }
373         }
374
375         status = rsi_band_check(common);
376         if (!status)
377                 status = rsi_set_channel(adapter->priv, channel);
378
379         if (bss->assoc) {
380                 if (common->hw_data_qs_blocked &&
381                     (rsi_get_connected_channel(adapter) == channel)) {
382                         rsi_dbg(INFO_ZONE, "unblk data q %d\n", channel);
383                         if (!rsi_send_block_unblock_frame(common, false))
384                                 common->hw_data_qs_blocked = false;
385                 }
386         } else {
387                 if (common->hw_data_qs_blocked) {
388                         rsi_dbg(INFO_ZONE, "unblk data q %d\n", channel);
389                         if (!rsi_send_block_unblock_frame(common, false))
390                                 common->hw_data_qs_blocked = false;
391                 }
392         }
393
394         return status;
395 }
396
397 /**
398  * rsi_mac80211_config() - This function is a handler for configuration
399  *                         requests. The stack calls this function to
400  *                         change hardware configuration, e.g., channel.
401  * @hw: Pointer to the ieee80211_hw structure.
402  * @changed: Changed flags set.
403  *
404  * Return: 0 on success, negative error code on failure.
405  */
406 static int rsi_mac80211_config(struct ieee80211_hw *hw,
407                                u32 changed)
408 {
409         struct rsi_hw *adapter = hw->priv;
410         struct rsi_common *common = adapter->priv;
411         int status = -EOPNOTSUPP;
412
413         mutex_lock(&common->mutex);
414
415         if (changed & IEEE80211_CONF_CHANGE_CHANNEL)
416                 status = rsi_channel_change(hw);
417
418         mutex_unlock(&common->mutex);
419
420         return status;
421 }
422
423 /**
424  * rsi_get_connected_channel() - This function is used to get the current
425  *                               connected channel number.
426  * @adapter: Pointer to the adapter structure.
427  *
428  * Return: Current connected AP's channel number is returned.
429  */
430 u16 rsi_get_connected_channel(struct rsi_hw *adapter)
431 {
432         struct ieee80211_vif *vif = adapter->vifs[0];
433         if (vif) {
434                 struct ieee80211_bss_conf *bss = &vif->bss_conf;
435                 struct ieee80211_channel *channel = bss->chandef.chan;
436                 return channel->hw_value;
437         }
438
439         return 0;
440 }
441
442 /**
443  * rsi_mac80211_bss_info_changed() - This function is a handler for config
444  *                                   requests related to BSS parameters that
445  *                                   may vary during BSS's lifespan.
446  * @hw: Pointer to the ieee80211_hw structure.
447  * @vif: Pointer to the ieee80211_vif structure.
448  * @bss_conf: Pointer to the ieee80211_bss_conf structure.
449  * @changed: Changed flags set.
450  *
451  * Return: None.
452  */
453 static void rsi_mac80211_bss_info_changed(struct ieee80211_hw *hw,
454                                           struct ieee80211_vif *vif,
455                                           struct ieee80211_bss_conf *bss_conf,
456                                           u32 changed)
457 {
458         struct rsi_hw *adapter = hw->priv;
459         struct rsi_common *common = adapter->priv;
460
461         mutex_lock(&common->mutex);
462         if (changed & BSS_CHANGED_ASSOC) {
463                 rsi_dbg(INFO_ZONE, "%s: Changed Association status: %d\n",
464                         __func__, bss_conf->assoc);
465                 rsi_inform_bss_status(common,
466                                       bss_conf->assoc,
467                                       bss_conf->bssid,
468                                       bss_conf->qos,
469                                       bss_conf->aid);
470         }
471
472         if (changed & BSS_CHANGED_CQM) {
473                 common->cqm_info.last_cqm_event_rssi = 0;
474                 common->cqm_info.rssi_thold = bss_conf->cqm_rssi_thold;
475                 common->cqm_info.rssi_hyst = bss_conf->cqm_rssi_hyst;
476                 rsi_dbg(INFO_ZONE, "RSSI throld & hysteresis are: %d %d\n",
477                         common->cqm_info.rssi_thold,
478                         common->cqm_info.rssi_hyst);
479         }
480         mutex_unlock(&common->mutex);
481 }
482
483 /**
484  * rsi_mac80211_conf_filter() - This function configure the device's RX filter.
485  * @hw: Pointer to the ieee80211_hw structure.
486  * @changed: Changed flags set.
487  * @total_flags: Total initial flags set.
488  * @multicast: Multicast.
489  *
490  * Return: None.
491  */
492 static void rsi_mac80211_conf_filter(struct ieee80211_hw *hw,
493                                      u32 changed_flags,
494                                      u32 *total_flags,
495                                      u64 multicast)
496 {
497         /* Not doing much here as of now */
498         *total_flags &= RSI_SUPP_FILTERS;
499 }
500
501 /**
502  * rsi_mac80211_conf_tx() - This function configures TX queue parameters
503  *                          (EDCF (aifs, cw_min, cw_max), bursting)
504  *                          for a hardware TX queue.
505  * @hw: Pointer to the ieee80211_hw structure
506  * @vif: Pointer to the ieee80211_vif structure.
507  * @queue: Queue number.
508  * @params: Pointer to ieee80211_tx_queue_params structure.
509  *
510  * Return: 0 on success, negative error code on failure.
511  */
512 static int rsi_mac80211_conf_tx(struct ieee80211_hw *hw,
513                                 struct ieee80211_vif *vif, u16 queue,
514                                 const struct ieee80211_tx_queue_params *params)
515 {
516         struct rsi_hw *adapter = hw->priv;
517         struct rsi_common *common = adapter->priv;
518         u8 idx = 0;
519
520         if (queue >= IEEE80211_NUM_ACS)
521                 return 0;
522
523         rsi_dbg(INFO_ZONE,
524                 "%s: Conf queue %d, aifs: %d, cwmin: %d cwmax: %d, txop: %d\n",
525                 __func__, queue, params->aifs,
526                 params->cw_min, params->cw_max, params->txop);
527
528         mutex_lock(&common->mutex);
529         /* Map into the way the f/w expects */
530         switch (queue) {
531         case IEEE80211_AC_VO:
532                 idx = VO_Q;
533                 break;
534         case IEEE80211_AC_VI:
535                 idx = VI_Q;
536                 break;
537         case IEEE80211_AC_BE:
538                 idx = BE_Q;
539                 break;
540         case IEEE80211_AC_BK:
541                 idx = BK_Q;
542                 break;
543         default:
544                 idx = BE_Q;
545                 break;
546         }
547
548         memcpy(&common->edca_params[idx],
549                params,
550                sizeof(struct ieee80211_tx_queue_params));
551         mutex_unlock(&common->mutex);
552
553         return 0;
554 }
555
556 /**
557  * rsi_hal_key_config() - This function loads the keys into the firmware.
558  * @hw: Pointer to the ieee80211_hw structure.
559  * @vif: Pointer to the ieee80211_vif structure.
560  * @key: Pointer to the ieee80211_key_conf structure.
561  *
562  * Return: status: 0 on success, -1 on failure.
563  */
564 static int rsi_hal_key_config(struct ieee80211_hw *hw,
565                               struct ieee80211_vif *vif,
566                               struct ieee80211_key_conf *key)
567 {
568         struct rsi_hw *adapter = hw->priv;
569         int status;
570         u8 key_type;
571
572         if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
573                 key_type = RSI_PAIRWISE_KEY;
574         else
575                 key_type = RSI_GROUP_KEY;
576
577         rsi_dbg(ERR_ZONE, "%s: Cipher 0x%x key_type: %d key_len: %d\n",
578                 __func__, key->cipher, key_type, key->keylen);
579
580         if ((key->cipher == WLAN_CIPHER_SUITE_WEP104) ||
581             (key->cipher == WLAN_CIPHER_SUITE_WEP40)) {
582                 status = rsi_hal_load_key(adapter->priv,
583                                           key->key,
584                                           key->keylen,
585                                           RSI_PAIRWISE_KEY,
586                                           key->keyidx,
587                                           key->cipher);
588                 if (status)
589                         return status;
590         }
591         return rsi_hal_load_key(adapter->priv,
592                                 key->key,
593                                 key->keylen,
594                                 key_type,
595                                 key->keyidx,
596                                 key->cipher);
597 }
598
599 /**
600  * rsi_mac80211_set_key() - This function sets type of key to be loaded.
601  * @hw: Pointer to the ieee80211_hw structure.
602  * @cmd: enum set_key_cmd.
603  * @vif: Pointer to the ieee80211_vif structure.
604  * @sta: Pointer to the ieee80211_sta structure.
605  * @key: Pointer to the ieee80211_key_conf structure.
606  *
607  * Return: status: 0 on success, negative error code on failure.
608  */
609 static int rsi_mac80211_set_key(struct ieee80211_hw *hw,
610                                 enum set_key_cmd cmd,
611                                 struct ieee80211_vif *vif,
612                                 struct ieee80211_sta *sta,
613                                 struct ieee80211_key_conf *key)
614 {
615         struct rsi_hw *adapter = hw->priv;
616         struct rsi_common *common = adapter->priv;
617         struct security_info *secinfo = &common->secinfo;
618         int status;
619
620         mutex_lock(&common->mutex);
621         switch (cmd) {
622         case SET_KEY:
623                 secinfo->security_enable = true;
624                 status = rsi_hal_key_config(hw, vif, key);
625                 if (status) {
626                         mutex_unlock(&common->mutex);
627                         return status;
628                 }
629
630                 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
631                         secinfo->ptk_cipher = key->cipher;
632                 else
633                         secinfo->gtk_cipher = key->cipher;
634
635                 key->hw_key_idx = key->keyidx;
636                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
637
638                 rsi_dbg(ERR_ZONE, "%s: RSI set_key\n", __func__);
639                 break;
640
641         case DISABLE_KEY:
642                 secinfo->security_enable = false;
643                 rsi_dbg(ERR_ZONE, "%s: RSI del key\n", __func__);
644                 memset(key, 0, sizeof(struct ieee80211_key_conf));
645                 status = rsi_hal_key_config(hw, vif, key);
646                 break;
647
648         default:
649                 status = -EOPNOTSUPP;
650                 break;
651         }
652
653         mutex_unlock(&common->mutex);
654         return status;
655 }
656
657 /**
658  * rsi_mac80211_ampdu_action() - This function selects the AMPDU action for
659  *                               the corresponding mlme_action flag and
660  *                               informs the f/w regarding this.
661  * @hw: Pointer to the ieee80211_hw structure.
662  * @vif: Pointer to the ieee80211_vif structure.
663  * @params: Pointer to A-MPDU action parameters
664  *
665  * Return: status: 0 on success, negative error code on failure.
666  */
667 static int rsi_mac80211_ampdu_action(struct ieee80211_hw *hw,
668                                      struct ieee80211_vif *vif,
669                                      struct ieee80211_ampdu_params *params)
670 {
671         int status = -EOPNOTSUPP;
672         struct rsi_hw *adapter = hw->priv;
673         struct rsi_common *common = adapter->priv;
674         u16 seq_no = 0;
675         u8 ii = 0;
676         struct ieee80211_sta *sta = params->sta;
677         enum ieee80211_ampdu_mlme_action action = params->action;
678         u16 tid = params->tid;
679         u16 *ssn = &params->ssn;
680         u8 buf_size = params->buf_size;
681
682         for (ii = 0; ii < RSI_MAX_VIFS; ii++) {
683                 if (vif == adapter->vifs[ii])
684                         break;
685         }
686
687         mutex_lock(&common->mutex);
688         rsi_dbg(INFO_ZONE, "%s: AMPDU action %d called\n", __func__, action);
689         if (ssn != NULL)
690                 seq_no = *ssn;
691
692         switch (action) {
693         case IEEE80211_AMPDU_RX_START:
694                 status = rsi_send_aggregation_params_frame(common,
695                                                            tid,
696                                                            seq_no,
697                                                            buf_size,
698                                                            STA_RX_ADDBA_DONE);
699                 break;
700
701         case IEEE80211_AMPDU_RX_STOP:
702                 status = rsi_send_aggregation_params_frame(common,
703                                                            tid,
704                                                            0,
705                                                            buf_size,
706                                                            STA_RX_DELBA);
707                 break;
708
709         case IEEE80211_AMPDU_TX_START:
710                 common->vif_info[ii].seq_start = seq_no;
711                 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
712                 status = 0;
713                 break;
714
715         case IEEE80211_AMPDU_TX_STOP_CONT:
716         case IEEE80211_AMPDU_TX_STOP_FLUSH:
717         case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
718                 status = rsi_send_aggregation_params_frame(common,
719                                                            tid,
720                                                            seq_no,
721                                                            buf_size,
722                                                            STA_TX_DELBA);
723                 if (!status)
724                         ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
725                 break;
726
727         case IEEE80211_AMPDU_TX_OPERATIONAL:
728                 status = rsi_send_aggregation_params_frame(common,
729                                                            tid,
730                                                            common->vif_info[ii]
731                                                                 .seq_start,
732                                                            buf_size,
733                                                            STA_TX_ADDBA_DONE);
734                 break;
735
736         default:
737                 rsi_dbg(ERR_ZONE, "%s: Uknown AMPDU action\n", __func__);
738                 break;
739         }
740
741         mutex_unlock(&common->mutex);
742         return status;
743 }
744
745 /**
746  * rsi_mac80211_set_rts_threshold() - This function sets rts threshold value.
747  * @hw: Pointer to the ieee80211_hw structure.
748  * @value: Rts threshold value.
749  *
750  * Return: 0 on success.
751  */
752 static int rsi_mac80211_set_rts_threshold(struct ieee80211_hw *hw,
753                                           u32 value)
754 {
755         struct rsi_hw *adapter = hw->priv;
756         struct rsi_common *common = adapter->priv;
757
758         mutex_lock(&common->mutex);
759         common->rts_threshold = value;
760         mutex_unlock(&common->mutex);
761
762         return 0;
763 }
764
765 /**
766  * rsi_mac80211_set_rate_mask() - This function sets bitrate_mask to be used.
767  * @hw: Pointer to the ieee80211_hw structure
768  * @vif: Pointer to the ieee80211_vif structure.
769  * @mask: Pointer to the cfg80211_bitrate_mask structure.
770  *
771  * Return: 0 on success.
772  */
773 static int rsi_mac80211_set_rate_mask(struct ieee80211_hw *hw,
774                                       struct ieee80211_vif *vif,
775                                       const struct cfg80211_bitrate_mask *mask)
776 {
777         struct rsi_hw *adapter = hw->priv;
778         struct rsi_common *common = adapter->priv;
779         enum nl80211_band band = hw->conf.chandef.chan->band;
780
781         mutex_lock(&common->mutex);
782         common->fixedrate_mask[band] = 0;
783
784         if (mask->control[band].legacy == 0xfff) {
785                 common->fixedrate_mask[band] =
786                         (mask->control[band].ht_mcs[0] << 12);
787         } else {
788                 common->fixedrate_mask[band] =
789                         mask->control[band].legacy;
790         }
791         mutex_unlock(&common->mutex);
792
793         return 0;
794 }
795
796 /**
797  * rsi_perform_cqm() - This function performs cqm.
798  * @common: Pointer to the driver private structure.
799  * @bssid: pointer to the bssid.
800  * @rssi: RSSI value.
801  */
802 static void rsi_perform_cqm(struct rsi_common *common,
803                             u8 *bssid,
804                             s8 rssi)
805 {
806         struct rsi_hw *adapter = common->priv;
807         s8 last_event = common->cqm_info.last_cqm_event_rssi;
808         int thold = common->cqm_info.rssi_thold;
809         u32 hyst = common->cqm_info.rssi_hyst;
810         enum nl80211_cqm_rssi_threshold_event event;
811
812         if (rssi < thold && (last_event == 0 || rssi < (last_event - hyst)))
813                 event = NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW;
814         else if (rssi > thold &&
815                  (last_event == 0 || rssi > (last_event + hyst)))
816                 event = NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH;
817         else
818                 return;
819
820         common->cqm_info.last_cqm_event_rssi = rssi;
821         rsi_dbg(INFO_ZONE, "CQM: Notifying event: %d\n", event);
822         ieee80211_cqm_rssi_notify(adapter->vifs[0], event, GFP_KERNEL);
823
824         return;
825 }
826
827 /**
828  * rsi_fill_rx_status() - This function fills rx status in
829  *                        ieee80211_rx_status structure.
830  * @hw: Pointer to the ieee80211_hw structure.
831  * @skb: Pointer to the socket buffer structure.
832  * @common: Pointer to the driver private structure.
833  * @rxs: Pointer to the ieee80211_rx_status structure.
834  *
835  * Return: None.
836  */
837 static void rsi_fill_rx_status(struct ieee80211_hw *hw,
838                                struct sk_buff *skb,
839                                struct rsi_common *common,
840                                struct ieee80211_rx_status *rxs)
841 {
842         struct ieee80211_bss_conf *bss = &common->priv->vifs[0]->bss_conf;
843         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
844         struct skb_info *rx_params = (struct skb_info *)info->driver_data;
845         struct ieee80211_hdr *hdr;
846         char rssi = rx_params->rssi;
847         u8 hdrlen = 0;
848         u8 channel = rx_params->channel;
849         s32 freq;
850
851         hdr = ((struct ieee80211_hdr *)(skb->data));
852         hdrlen = ieee80211_hdrlen(hdr->frame_control);
853
854         memset(info, 0, sizeof(struct ieee80211_tx_info));
855
856         rxs->signal = -(rssi);
857
858         rxs->band = common->band;
859
860         freq = ieee80211_channel_to_frequency(channel, rxs->band);
861
862         if (freq)
863                 rxs->freq = freq;
864
865         if (ieee80211_has_protected(hdr->frame_control)) {
866                 if (rsi_is_cipher_wep(common)) {
867                         memmove(skb->data + 4, skb->data, hdrlen);
868                         skb_pull(skb, 4);
869                 } else {
870                         memmove(skb->data + 8, skb->data, hdrlen);
871                         skb_pull(skb, 8);
872                         rxs->flag |= RX_FLAG_MMIC_STRIPPED;
873                 }
874                 rxs->flag |= RX_FLAG_DECRYPTED;
875                 rxs->flag |= RX_FLAG_IV_STRIPPED;
876         }
877
878         /* CQM only for connected AP beacons, the RSSI is a weighted avg */
879         if (bss->assoc && !(memcmp(bss->bssid, hdr->addr2, ETH_ALEN))) {
880                 if (ieee80211_is_beacon(hdr->frame_control))
881                         rsi_perform_cqm(common, hdr->addr2, rxs->signal);
882         }
883
884         return;
885 }
886
887 /**
888  * rsi_indicate_pkt_to_os() - This function sends recieved packet to mac80211.
889  * @common: Pointer to the driver private structure.
890  * @skb: Pointer to the socket buffer structure.
891  *
892  * Return: None.
893  */
894 void rsi_indicate_pkt_to_os(struct rsi_common *common,
895                             struct sk_buff *skb)
896 {
897         struct rsi_hw *adapter = common->priv;
898         struct ieee80211_hw *hw = adapter->hw;
899         struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
900
901         if ((common->iface_down) || (!adapter->sc_nvifs)) {
902                 dev_kfree_skb(skb);
903                 return;
904         }
905
906         /* filling in the ieee80211_rx_status flags */
907         rsi_fill_rx_status(hw, skb, common, rx_status);
908
909         ieee80211_rx_irqsafe(hw, skb);
910 }
911
912 static void rsi_set_min_rate(struct ieee80211_hw *hw,
913                              struct ieee80211_sta *sta,
914                              struct rsi_common *common)
915 {
916         u8 band = hw->conf.chandef.chan->band;
917         u8 ii;
918         u32 rate_bitmap;
919         bool matched = false;
920
921         common->bitrate_mask[band] = sta->supp_rates[band];
922
923         rate_bitmap = (common->fixedrate_mask[band] & sta->supp_rates[band]);
924
925         if (rate_bitmap & 0xfff) {
926                 /* Find out the min rate */
927                 for (ii = 0; ii < ARRAY_SIZE(rsi_rates); ii++) {
928                         if (rate_bitmap & BIT(ii)) {
929                                 common->min_rate = rsi_rates[ii].hw_value;
930                                 matched = true;
931                                 break;
932                         }
933                 }
934         }
935
936         common->vif_info[0].is_ht = sta->ht_cap.ht_supported;
937
938         if ((common->vif_info[0].is_ht) && (rate_bitmap >> 12)) {
939                 for (ii = 0; ii < ARRAY_SIZE(rsi_mcsrates); ii++) {
940                         if ((rate_bitmap >> 12) & BIT(ii)) {
941                                 common->min_rate = rsi_mcsrates[ii];
942                                 matched = true;
943                                 break;
944                         }
945                 }
946         }
947
948         if (!matched)
949                 common->min_rate = 0xffff;
950 }
951
952 /**
953  * rsi_mac80211_sta_add() - This function notifies driver about a peer getting
954  *                          connected.
955  * @hw: pointer to the ieee80211_hw structure.
956  * @vif: Pointer to the ieee80211_vif structure.
957  * @sta: Pointer to the ieee80211_sta structure.
958  *
959  * Return: 0 on success, -1 on failure.
960  */
961 static int rsi_mac80211_sta_add(struct ieee80211_hw *hw,
962                                 struct ieee80211_vif *vif,
963                                 struct ieee80211_sta *sta)
964 {
965         struct rsi_hw *adapter = hw->priv;
966         struct rsi_common *common = adapter->priv;
967
968         mutex_lock(&common->mutex);
969
970         rsi_set_min_rate(hw, sta, common);
971
972         if ((sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20) ||
973             (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40)) {
974                 common->vif_info[0].sgi = true;
975         }
976
977         if (sta->ht_cap.ht_supported)
978                 ieee80211_start_tx_ba_session(sta, 0, 0);
979
980         mutex_unlock(&common->mutex);
981
982         return 0;
983 }
984
985 /**
986  * rsi_mac80211_sta_remove() - This function notifies driver about a peer
987  *                             getting disconnected.
988  * @hw: Pointer to the ieee80211_hw structure.
989  * @vif: Pointer to the ieee80211_vif structure.
990  * @sta: Pointer to the ieee80211_sta structure.
991  *
992  * Return: 0 on success, -1 on failure.
993  */
994 static int rsi_mac80211_sta_remove(struct ieee80211_hw *hw,
995                                    struct ieee80211_vif *vif,
996                                    struct ieee80211_sta *sta)
997 {
998         struct rsi_hw *adapter = hw->priv;
999         struct rsi_common *common = adapter->priv;
1000
1001         mutex_lock(&common->mutex);
1002         /* Resetting all the fields to default values */
1003         common->bitrate_mask[NL80211_BAND_2GHZ] = 0;
1004         common->bitrate_mask[NL80211_BAND_5GHZ] = 0;
1005         common->min_rate = 0xffff;
1006         common->vif_info[0].is_ht = false;
1007         common->vif_info[0].sgi = false;
1008         common->vif_info[0].seq_start = 0;
1009         common->secinfo.ptk_cipher = 0;
1010         common->secinfo.gtk_cipher = 0;
1011         mutex_unlock(&common->mutex);
1012
1013         return 0;
1014 }
1015
1016 static struct ieee80211_ops mac80211_ops = {
1017         .tx = rsi_mac80211_tx,
1018         .start = rsi_mac80211_start,
1019         .stop = rsi_mac80211_stop,
1020         .add_interface = rsi_mac80211_add_interface,
1021         .remove_interface = rsi_mac80211_remove_interface,
1022         .config = rsi_mac80211_config,
1023         .bss_info_changed = rsi_mac80211_bss_info_changed,
1024         .conf_tx = rsi_mac80211_conf_tx,
1025         .configure_filter = rsi_mac80211_conf_filter,
1026         .set_key = rsi_mac80211_set_key,
1027         .set_rts_threshold = rsi_mac80211_set_rts_threshold,
1028         .set_bitrate_mask = rsi_mac80211_set_rate_mask,
1029         .ampdu_action = rsi_mac80211_ampdu_action,
1030         .sta_add = rsi_mac80211_sta_add,
1031         .sta_remove = rsi_mac80211_sta_remove,
1032 };
1033
1034 /**
1035  * rsi_mac80211_attach() - This function is used to initialize Mac80211 stack.
1036  * @common: Pointer to the driver private structure.
1037  *
1038  * Return: 0 on success, -1 on failure.
1039  */
1040 int rsi_mac80211_attach(struct rsi_common *common)
1041 {
1042         int status = 0;
1043         struct ieee80211_hw *hw = NULL;
1044         struct wiphy *wiphy = NULL;
1045         struct rsi_hw *adapter = common->priv;
1046         u8 addr_mask[ETH_ALEN] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x3};
1047
1048         rsi_dbg(INIT_ZONE, "%s: Performing mac80211 attach\n", __func__);
1049
1050         hw = ieee80211_alloc_hw(sizeof(struct rsi_hw), &mac80211_ops);
1051         if (!hw) {
1052                 rsi_dbg(ERR_ZONE, "%s: ieee80211 hw alloc failed\n", __func__);
1053                 return -ENOMEM;
1054         }
1055
1056         wiphy = hw->wiphy;
1057
1058         SET_IEEE80211_DEV(hw, adapter->device);
1059
1060         hw->priv = adapter;
1061         adapter->hw = hw;
1062
1063         ieee80211_hw_set(hw, SIGNAL_DBM);
1064         ieee80211_hw_set(hw, HAS_RATE_CONTROL);
1065         ieee80211_hw_set(hw, AMPDU_AGGREGATION);
1066
1067         hw->queues = MAX_HW_QUEUES;
1068         hw->extra_tx_headroom = RSI_NEEDED_HEADROOM;
1069
1070         hw->max_rates = 1;
1071         hw->max_rate_tries = MAX_RETRIES;
1072
1073         hw->max_tx_aggregation_subframes = 6;
1074         rsi_register_rates_channels(adapter, NL80211_BAND_2GHZ);
1075         rsi_register_rates_channels(adapter, NL80211_BAND_5GHZ);
1076         hw->rate_control_algorithm = "AARF";
1077
1078         SET_IEEE80211_PERM_ADDR(hw, common->mac_addr);
1079         ether_addr_copy(hw->wiphy->addr_mask, addr_mask);
1080
1081         wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
1082         wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
1083         wiphy->retry_short = RETRY_SHORT;
1084         wiphy->retry_long  = RETRY_LONG;
1085         wiphy->frag_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
1086         wiphy->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
1087         wiphy->flags = 0;
1088
1089         wiphy->available_antennas_rx = 1;
1090         wiphy->available_antennas_tx = 1;
1091         wiphy->bands[NL80211_BAND_2GHZ] =
1092                 &adapter->sbands[NL80211_BAND_2GHZ];
1093         wiphy->bands[NL80211_BAND_5GHZ] =
1094                 &adapter->sbands[NL80211_BAND_5GHZ];
1095
1096         status = ieee80211_register_hw(hw);
1097         if (status)
1098                 return status;
1099
1100         return rsi_init_dbgfs(adapter);
1101 }