net/ipv6: Move && and || to end of previous line
[platform/kernel/linux-arm64.git] / drivers / net / wireless / ath / ath9k / common.c
1 /*
2  * Copyright (c) 2009 Atheros Communications 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 /*
18  * Module for common driver code between ath9k and ath9k_htc
19  */
20
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23
24 #include "common.h"
25
26 MODULE_AUTHOR("Atheros Communications");
27 MODULE_DESCRIPTION("Shared library for Atheros wireless 802.11n LAN cards.");
28 MODULE_LICENSE("Dual BSD/GPL");
29
30 /* Common RX processing */
31
32 /* Assumes you've already done the endian to CPU conversion */
33 static bool ath9k_rx_accept(struct ath_common *common,
34                             struct sk_buff *skb,
35                             struct ieee80211_rx_status *rxs,
36                             struct ath_rx_status *rx_stats,
37                             bool *decrypt_error)
38 {
39         struct ath_hw *ah = common->ah;
40         struct ieee80211_hdr *hdr;
41         __le16 fc;
42
43         hdr = (struct ieee80211_hdr *) skb->data;
44         fc = hdr->frame_control;
45
46         if (!rx_stats->rs_datalen)
47                 return false;
48         /*
49          * rs_status follows rs_datalen so if rs_datalen is too large
50          * we can take a hint that hardware corrupted it, so ignore
51          * those frames.
52          */
53         if (rx_stats->rs_datalen > common->rx_bufsize)
54                 return false;
55
56         /*
57          * rs_more indicates chained descriptors which can be used
58          * to link buffers together for a sort of scatter-gather
59          * operation.
60          *
61          * The rx_stats->rs_status will not be set until the end of the
62          * chained descriptors so it can be ignored if rs_more is set. The
63          * rs_more will be false at the last element of the chained
64          * descriptors.
65          */
66         if (!rx_stats->rs_more && rx_stats->rs_status != 0) {
67                 if (rx_stats->rs_status & ATH9K_RXERR_CRC)
68                         rxs->flag |= RX_FLAG_FAILED_FCS_CRC;
69                 if (rx_stats->rs_status & ATH9K_RXERR_PHY)
70                         return false;
71
72                 if (rx_stats->rs_status & ATH9K_RXERR_DECRYPT) {
73                         *decrypt_error = true;
74                 } else if (rx_stats->rs_status & ATH9K_RXERR_MIC) {
75                         if (ieee80211_is_ctl(fc))
76                                 /*
77                                  * Sometimes, we get invalid
78                                  * MIC failures on valid control frames.
79                                  * Remove these mic errors.
80                                  */
81                                 rx_stats->rs_status &= ~ATH9K_RXERR_MIC;
82                         else
83                                 rxs->flag |= RX_FLAG_MMIC_ERROR;
84                 }
85                 /*
86                  * Reject error frames with the exception of
87                  * decryption and MIC failures. For monitor mode,
88                  * we also ignore the CRC error.
89                  */
90                 if (ah->opmode == NL80211_IFTYPE_MONITOR) {
91                         if (rx_stats->rs_status &
92                             ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
93                               ATH9K_RXERR_CRC))
94                                 return false;
95                 } else {
96                         if (rx_stats->rs_status &
97                             ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC)) {
98                                 return false;
99                         }
100                 }
101         }
102         return true;
103 }
104
105 static u8 ath9k_process_rate(struct ath_common *common,
106                              struct ieee80211_hw *hw,
107                              struct ath_rx_status *rx_stats,
108                              struct ieee80211_rx_status *rxs,
109                              struct sk_buff *skb)
110 {
111         struct ieee80211_supported_band *sband;
112         enum ieee80211_band band;
113         unsigned int i = 0;
114
115         band = hw->conf.channel->band;
116         sband = hw->wiphy->bands[band];
117
118         if (rx_stats->rs_rate & 0x80) {
119                 /* HT rate */
120                 rxs->flag |= RX_FLAG_HT;
121                 if (rx_stats->rs_flags & ATH9K_RX_2040)
122                         rxs->flag |= RX_FLAG_40MHZ;
123                 if (rx_stats->rs_flags & ATH9K_RX_GI)
124                         rxs->flag |= RX_FLAG_SHORT_GI;
125                 return rx_stats->rs_rate & 0x7f;
126         }
127
128         for (i = 0; i < sband->n_bitrates; i++) {
129                 if (sband->bitrates[i].hw_value == rx_stats->rs_rate)
130                         return i;
131                 if (sband->bitrates[i].hw_value_short == rx_stats->rs_rate) {
132                         rxs->flag |= RX_FLAG_SHORTPRE;
133                         return i;
134                 }
135         }
136
137         /* No valid hardware bitrate found -- we should not get here */
138         ath_print(common, ATH_DBG_XMIT, "unsupported hw bitrate detected "
139                   "0x%02x using 1 Mbit\n", rx_stats->rs_rate);
140         if ((common->debug_mask & ATH_DBG_XMIT))
141                 print_hex_dump_bytes("", DUMP_PREFIX_NONE, skb->data, skb->len);
142
143         return 0;
144 }
145
146 static void ath9k_process_rssi(struct ath_common *common,
147                                struct ieee80211_hw *hw,
148                                struct sk_buff *skb,
149                                struct ath_rx_status *rx_stats)
150 {
151         struct ath_hw *ah = common->ah;
152         struct ieee80211_sta *sta;
153         struct ieee80211_hdr *hdr;
154         struct ath_node *an;
155         int last_rssi = ATH_RSSI_DUMMY_MARKER;
156         __le16 fc;
157
158         hdr = (struct ieee80211_hdr *)skb->data;
159         fc = hdr->frame_control;
160
161         rcu_read_lock();
162         /*
163          * XXX: use ieee80211_find_sta! This requires quite a bit of work
164          * under the current ath9k virtual wiphy implementation as we have
165          * no way of tying a vif to wiphy. Typically vifs are attached to
166          * at least one sdata of a wiphy on mac80211 but with ath9k virtual
167          * wiphy you'd have to iterate over every wiphy and each sdata.
168          */
169         sta = ieee80211_find_sta_by_hw(hw, hdr->addr2);
170         if (sta) {
171                 an = (struct ath_node *) sta->drv_priv;
172                 if (rx_stats->rs_rssi != ATH9K_RSSI_BAD &&
173                    !rx_stats->rs_moreaggr)
174                         ATH_RSSI_LPF(an->last_rssi, rx_stats->rs_rssi);
175                 last_rssi = an->last_rssi;
176         }
177         rcu_read_unlock();
178
179         if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
180                 rx_stats->rs_rssi = ATH_EP_RND(last_rssi,
181                                               ATH_RSSI_EP_MULTIPLIER);
182         if (rx_stats->rs_rssi < 0)
183                 rx_stats->rs_rssi = 0;
184         else if (rx_stats->rs_rssi > 127)
185                 rx_stats->rs_rssi = 127;
186
187         /* Update Beacon RSSI, this is used by ANI. */
188         if (ieee80211_is_beacon(fc))
189                 ah->stats.avgbrssi = rx_stats->rs_rssi;
190 }
191
192 /*
193  * For Decrypt or Demic errors, we only mark packet status here and always push
194  * up the frame up to let mac80211 handle the actual error case, be it no
195  * decryption key or real decryption error. This let us keep statistics there.
196  */
197 int ath9k_cmn_rx_skb_preprocess(struct ath_common *common,
198                                 struct ieee80211_hw *hw,
199                                 struct sk_buff *skb,
200                                 struct ath_rx_status *rx_stats,
201                                 struct ieee80211_rx_status *rx_status,
202                                 bool *decrypt_error)
203 {
204         struct ath_hw *ah = common->ah;
205
206         memset(rx_status, 0, sizeof(struct ieee80211_rx_status));
207         if (!ath9k_rx_accept(common, skb, rx_status, rx_stats, decrypt_error))
208                 return -EINVAL;
209
210         ath9k_process_rssi(common, hw, skb, rx_stats);
211
212         rx_status->rate_idx = ath9k_process_rate(common, hw,
213                                                  rx_stats, rx_status, skb);
214         rx_status->mactime = ath9k_hw_extend_tsf(ah, rx_stats->rs_tstamp);
215         rx_status->band = hw->conf.channel->band;
216         rx_status->freq = hw->conf.channel->center_freq;
217         rx_status->noise = common->ani.noise_floor;
218         rx_status->signal = ATH_DEFAULT_NOISE_FLOOR + rx_stats->rs_rssi;
219         rx_status->antenna = rx_stats->rs_antenna;
220         rx_status->flag |= RX_FLAG_TSFT;
221
222         return 0;
223 }
224 EXPORT_SYMBOL(ath9k_cmn_rx_skb_preprocess);
225
226 void ath9k_cmn_rx_skb_postprocess(struct ath_common *common,
227                                   struct sk_buff *skb,
228                                   struct ath_rx_status *rx_stats,
229                                   struct ieee80211_rx_status *rxs,
230                                   bool decrypt_error)
231 {
232         struct ath_hw *ah = common->ah;
233         struct ieee80211_hdr *hdr;
234         int hdrlen, padsize;
235         u8 keyix;
236         __le16 fc;
237
238         /* see if any padding is done by the hw and remove it */
239         hdr = (struct ieee80211_hdr *) skb->data;
240         hdrlen = ieee80211_get_hdrlen_from_skb(skb);
241         fc = hdr->frame_control;
242
243         /* The MAC header is padded to have 32-bit boundary if the
244          * packet payload is non-zero. The general calculation for
245          * padsize would take into account odd header lengths:
246          * padsize = (4 - hdrlen % 4) % 4; However, since only
247          * even-length headers are used, padding can only be 0 or 2
248          * bytes and we can optimize this a bit. In addition, we must
249          * not try to remove padding from short control frames that do
250          * not have payload. */
251         padsize = hdrlen & 3;
252         if (padsize && hdrlen >= 24) {
253                 memmove(skb->data + padsize, skb->data, hdrlen);
254                 skb_pull(skb, padsize);
255         }
256
257         keyix = rx_stats->rs_keyix;
258
259         if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error) {
260                 rxs->flag |= RX_FLAG_DECRYPTED;
261         } else if (ieee80211_has_protected(fc)
262                    && !decrypt_error && skb->len >= hdrlen + 4) {
263                 keyix = skb->data[hdrlen + 3] >> 6;
264
265                 if (test_bit(keyix, common->keymap))
266                         rxs->flag |= RX_FLAG_DECRYPTED;
267         }
268         if (ah->sw_mgmt_crypto &&
269             (rxs->flag & RX_FLAG_DECRYPTED) &&
270             ieee80211_is_mgmt(fc))
271                 /* Use software decrypt for management frames. */
272                 rxs->flag &= ~RX_FLAG_DECRYPTED;
273 }
274 EXPORT_SYMBOL(ath9k_cmn_rx_skb_postprocess);
275
276 static int __init ath9k_cmn_init(void)
277 {
278         return 0;
279 }
280 module_init(ath9k_cmn_init);
281
282 static void __exit ath9k_cmn_exit(void)
283 {
284         return;
285 }
286 module_exit(ath9k_cmn_exit);