]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/net/wireless/ath9k/rc.c
ath9k: General code scrub
[linux-2.6-omap-h63xx.git] / drivers / net / wireless / ath9k / rc.c
1 /*
2  * Copyright (c) 2004 Video54 Technologies, Inc.
3  * Copyright (c) 2004-2008 Atheros Communications, Inc.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 #include "core.h"
19
20 static struct ath_rate_table ar5416_11na_ratetable = {
21         42,
22         {0},
23         {
24                 { VALID, VALID, WLAN_RC_PHY_OFDM, 6000, /* 6 Mb */
25                         5400, 0x0b, 0x00, 12,
26                         0, 2, 1, 0, 0, 0, 0, 0 },
27                 { VALID,        VALID, WLAN_RC_PHY_OFDM, 9000, /* 9 Mb */
28                         7800,  0x0f, 0x00, 18,
29                         0, 3, 1, 1, 1, 1, 1, 0 },
30                 { VALID, VALID, WLAN_RC_PHY_OFDM, 12000, /* 12 Mb */
31                         10000, 0x0a, 0x00, 24,
32                         2, 4, 2, 2, 2, 2, 2, 0 },
33                 { VALID, VALID, WLAN_RC_PHY_OFDM, 18000, /* 18 Mb */
34                         13900, 0x0e, 0x00, 36,
35                         2, 6,  2, 3, 3, 3, 3, 0 },
36                 { VALID, VALID, WLAN_RC_PHY_OFDM, 24000, /* 24 Mb */
37                         17300, 0x09, 0x00, 48,
38                         4, 10, 3, 4, 4, 4, 4, 0 },
39                 { VALID, VALID, WLAN_RC_PHY_OFDM, 36000, /* 36 Mb */
40                         23000, 0x0d, 0x00, 72,
41                         4, 14, 3, 5, 5, 5, 5, 0 },
42                 { VALID, VALID, WLAN_RC_PHY_OFDM, 48000, /* 48 Mb */
43                         27400, 0x08, 0x00, 96,
44                         4, 20, 3, 6, 6, 6, 6, 0 },
45                 { VALID, VALID, WLAN_RC_PHY_OFDM, 54000, /* 54 Mb */
46                         29300, 0x0c, 0x00, 108,
47                         4, 23, 3, 7, 7, 7, 7, 0 },
48                 { VALID_20, VALID_20, WLAN_RC_PHY_HT_20_SS, 6500, /* 6.5 Mb */
49                         6400, 0x80, 0x00, 0,
50                         0, 2, 3, 8, 24, 8, 24, 3216 },
51                 { VALID_20, VALID_20, WLAN_RC_PHY_HT_20_SS, 13000, /* 13 Mb */
52                         12700, 0x81, 0x00, 1,
53                         2, 4, 3, 9, 25, 9, 25, 6434 },
54                 { VALID_20, VALID_20, WLAN_RC_PHY_HT_20_SS, 19500, /* 19.5 Mb */
55                         18800, 0x82, 0x00, 2,
56                         2, 6, 3, 10, 26, 10, 26, 9650 },
57                 { VALID_20, VALID_20, WLAN_RC_PHY_HT_20_SS, 26000, /* 26 Mb */
58                         25000, 0x83, 0x00, 3,
59                         4, 10, 3, 11, 27, 11, 27, 12868 },
60                 { VALID_20, VALID_20, WLAN_RC_PHY_HT_20_SS, 39000, /* 39 Mb */
61                         36700, 0x84, 0x00, 4,
62                         4, 14, 3, 12, 28, 12, 28, 19304 },
63                 { INVALID, VALID_20, WLAN_RC_PHY_HT_20_SS, 52000, /* 52 Mb */
64                         48100, 0x85, 0x00, 5,
65                         4, 20, 3, 13, 29, 13, 29, 25740 },
66                 { INVALID, VALID_20, WLAN_RC_PHY_HT_20_SS, 58500, /* 58.5 Mb */
67                         53500, 0x86, 0x00, 6,
68                         4, 23, 3, 14, 30, 14, 30,  28956 },
69                 { INVALID, VALID_20, WLAN_RC_PHY_HT_20_SS, 65000, /* 65 Mb */
70                         59000, 0x87, 0x00, 7,
71                         4, 25, 3, 15, 31, 15, 32, 32180 },
72                 { INVALID, INVALID, WLAN_RC_PHY_HT_20_DS, 13000, /* 13 Mb */
73                         12700, 0x88, 0x00,
74                         8, 0, 2, 3, 16, 33, 16, 33, 6430 },
75                 { INVALID, INVALID, WLAN_RC_PHY_HT_20_DS, 26000, /* 26 Mb */
76                         24800, 0x89, 0x00, 9,
77                         2, 4, 3, 17, 34, 17, 34, 12860 },
78                 { INVALID, INVALID, WLAN_RC_PHY_HT_20_DS, 39000, /* 39 Mb */
79                         36600, 0x8a, 0x00, 10,
80                         2, 6, 3, 18, 35, 18, 35, 19300 },
81                 { VALID_20, INVALID, WLAN_RC_PHY_HT_20_DS, 52000, /* 52 Mb */
82                         48100, 0x8b, 0x00, 11,
83                         4, 10, 3, 19, 36, 19, 36, 25736 },
84                 { VALID_20, INVALID, WLAN_RC_PHY_HT_20_DS, 78000, /* 78 Mb */
85                         69500, 0x8c, 0x00, 12,
86                         4, 14, 3, 20, 37, 20, 37, 38600 },
87                 { VALID_20, INVALID, WLAN_RC_PHY_HT_20_DS, 104000, /* 104 Mb */
88                         89500, 0x8d, 0x00, 13,
89                         4, 20, 3, 21, 38, 21, 38, 51472 },
90                 { VALID_20, INVALID, WLAN_RC_PHY_HT_20_DS, 117000, /* 117 Mb */
91                         98900, 0x8e, 0x00, 14,
92                         4, 23, 3, 22, 39, 22, 39, 57890 },
93                 { VALID_20, INVALID, WLAN_RC_PHY_HT_20_DS, 130000, /* 130 Mb */
94                         108300, 0x8f, 0x00, 15,
95                         4, 25, 3, 23, 40, 23, 41, 64320 },
96                 { VALID_40, VALID_40, WLAN_RC_PHY_HT_40_SS, 13500, /* 13.5 Mb */
97                         13200, 0x80, 0x00, 0,
98                         0, 2, 3, 8, 24, 24, 24, 6684 },
99                 { VALID_40, VALID_40, WLAN_RC_PHY_HT_40_SS, 27500, /* 27.0 Mb */
100                         25900, 0x81, 0x00, 1,
101                         2, 4, 3, 9, 25, 25, 25, 13368 },
102                 { VALID_40, VALID_40, WLAN_RC_PHY_HT_40_SS, 40500, /* 40.5 Mb */
103                         38600, 0x82, 0x00, 2,
104                         2, 6, 3, 10, 26, 26, 26, 20052 },
105                 { VALID_40, VALID_40, WLAN_RC_PHY_HT_40_SS, 54000, /* 54 Mb */
106                         49800, 0x83, 0x00, 3,
107                         4, 10, 3, 11, 27, 27, 27, 26738 },
108                 { VALID_40, VALID_40, WLAN_RC_PHY_HT_40_SS, 81500, /* 81 Mb */
109                         72200, 0x84, 0x00, 4,
110                         4, 14, 3, 12, 28, 28, 28, 40104 },
111                 { INVALID, VALID_40, WLAN_RC_PHY_HT_40_SS, 108000, /* 108 Mb */
112                         92900, 0x85, 0x00, 5,
113                         4, 20, 3, 13, 29, 29, 29, 53476 },
114                 { INVALID, VALID_40, WLAN_RC_PHY_HT_40_SS, 121500, /* 121.5 Mb */
115                         102700, 0x86, 0x00, 6,
116                         4, 23, 3, 14, 30, 30, 30, 60156 },
117                 { INVALID, VALID_40, WLAN_RC_PHY_HT_40_SS, 135000, /* 135 Mb */
118                         112000, 0x87, 0x00, 7,
119                         4, 25, 3, 15, 31, 32, 32, 66840 },
120                 { INVALID, VALID_40, WLAN_RC_PHY_HT_40_SS_HGI, 150000, /* 150 Mb */
121                         122000, 0x87, 0x00, 7,
122                         4, 25, 3, 15, 31, 32, 32, 74200 },
123                 { INVALID, INVALID, WLAN_RC_PHY_HT_40_DS, 27000, /* 27 Mb */
124                         25800, 0x88, 0x00, 8,
125                         0, 2, 3, 16, 33, 33, 33, 13360 },
126                 { INVALID, INVALID, WLAN_RC_PHY_HT_40_DS, 54000, /* 54 Mb */
127                         49800, 0x89, 0x00, 9,
128                         2, 4, 3, 17, 34, 34, 34, 26720 },
129                 { INVALID, INVALID, WLAN_RC_PHY_HT_40_DS, 81000, /* 81 Mb */
130                         71900, 0x8a, 0x00, 10,
131                         2, 6, 3, 18, 35, 35, 35, 40080 },
132                 { VALID_40, INVALID, WLAN_RC_PHY_HT_40_DS, 108000, /* 108 Mb */
133                         92500, 0x8b, 0x00, 11,
134                         4, 10, 3, 19, 36, 36, 36, 53440 },
135                 { VALID_40, INVALID, WLAN_RC_PHY_HT_40_DS, 162000, /* 162 Mb */
136                         130300, 0x8c, 0x00, 12,
137                         4, 14, 3, 20, 37, 37, 37, 80160 },
138                 { VALID_40, INVALID, WLAN_RC_PHY_HT_40_DS, 216000, /* 216 Mb */
139                         162800, 0x8d, 0x00, 13,
140                         4, 20, 3, 21, 38, 38, 38, 106880 },
141                 { VALID_40, INVALID, WLAN_RC_PHY_HT_40_DS, 243000, /* 243 Mb */
142                         178200, 0x8e, 0x00, 14,
143                         4, 23, 3, 22, 39, 39, 39, 120240 },
144                 { VALID_40, INVALID, WLAN_RC_PHY_HT_40_DS, 270000, /* 270 Mb */
145                         192100, 0x8f, 0x00, 15,
146                         4, 25, 3, 23, 40, 41, 41, 133600 },
147                 { VALID_40, INVALID, WLAN_RC_PHY_HT_40_DS_HGI, 300000, /* 300 Mb */
148                         207000, 0x8f, 0x00, 15,
149                         4, 25, 3, 23, 40, 41, 41, 148400 },
150         },
151         50,  /* probe interval */
152         50,  /* rssi reduce interval */
153         WLAN_RC_HT_FLAG,  /* Phy rates allowed initially */
154 };
155
156 /* VALID_ALL - valid for 20/40/Legacy,
157  * VALID - Legacy only,
158  * VALID_20 - HT 20 only,
159  * VALID_40 - HT 40 only */
160
161 /* 4ms frame limit not used for NG mode.  The values filled
162  * for HT are the 64K max aggregate limit */
163
164 static struct ath_rate_table ar5416_11ng_ratetable = {
165         46,
166         {0},
167         {
168                 { VALID_ALL, VALID_ALL, WLAN_RC_PHY_CCK, 1000, /* 1 Mb */
169                         900, 0x1b, 0x00, 2,
170                         0, 0, 1, 0, 0, 0, 0, 0 },
171                 { VALID_ALL, VALID_ALL, WLAN_RC_PHY_CCK, 2000, /* 2 Mb */
172                         1900, 0x1a, 0x04, 4,
173                         1, 1, 1, 1, 1, 1, 1, 0 },
174                 { VALID_ALL, VALID_ALL, WLAN_RC_PHY_CCK, 5500, /* 5.5 Mb */
175                         4900, 0x19, 0x04, 11,
176                         2, 2, 2, 2, 2, 2, 2, 0 },
177                 { VALID_ALL, VALID_ALL, WLAN_RC_PHY_CCK, 11000, /* 11 Mb */
178                         8100, 0x18, 0x04, 22,
179                         3, 3, 2, 3, 3, 3, 3, 0 },
180                 { INVALID, INVALID, WLAN_RC_PHY_OFDM, 6000, /* 6 Mb */
181                         5400, 0x0b, 0x00, 12,
182                         4, 2, 1, 4, 4, 4, 4, 0 },
183                 { INVALID, INVALID, WLAN_RC_PHY_OFDM, 9000, /* 9 Mb */
184                         7800, 0x0f, 0x00, 18,
185                         4, 3, 1, 5, 5, 5, 5, 0 },
186                 { VALID, VALID, WLAN_RC_PHY_OFDM, 12000, /* 12 Mb */
187                         10100, 0x0a, 0x00, 24,
188                         6, 4, 1, 6, 6, 6, 6, 0 },
189                 { VALID, VALID, WLAN_RC_PHY_OFDM, 18000, /* 18 Mb */
190                         14100,  0x0e, 0x00, 36,
191                         6, 6, 2, 7, 7, 7, 7, 0 },
192                 { VALID, VALID, WLAN_RC_PHY_OFDM, 24000, /* 24 Mb */
193                         17700, 0x09, 0x00, 48,
194                         8, 10, 3, 8, 8, 8, 8, 0 },
195                 { VALID, VALID, WLAN_RC_PHY_OFDM, 36000, /* 36 Mb */
196                         23700, 0x0d, 0x00, 72,
197                         8, 14, 3, 9, 9, 9, 9, 0 },
198                 { VALID, VALID, WLAN_RC_PHY_OFDM, 48000, /* 48 Mb */
199                         27400, 0x08, 0x00, 96,
200                         8, 20, 3, 10, 10, 10, 10, 0 },
201                 { VALID, VALID, WLAN_RC_PHY_OFDM, 54000, /* 54 Mb */
202                         30900, 0x0c, 0x00, 108,
203                         8, 23, 3, 11, 11, 11, 11, 0 },
204                 { INVALID, INVALID, WLAN_RC_PHY_HT_20_SS, 6500, /* 6.5 Mb */
205                         6400, 0x80, 0x00, 0,
206                         4, 2, 3, 12, 28, 12, 28, 3216 },
207                 { VALID_20, VALID_20, WLAN_RC_PHY_HT_20_SS, 13000, /* 13 Mb */
208                         12700, 0x81, 0x00, 1,
209                         6, 4, 3, 13, 29, 13, 29, 6434 },
210                 { VALID_20, VALID_20, WLAN_RC_PHY_HT_20_SS, 19500, /* 19.5 Mb */
211                         18800, 0x82, 0x00, 2,
212                         6, 6, 3, 14, 30, 14, 30, 9650 },
213                 { VALID_20, VALID_20, WLAN_RC_PHY_HT_20_SS, 26000, /* 26 Mb */
214                         25000, 0x83, 0x00, 3,
215                         8, 10, 3, 15, 31, 15, 31, 12868 },
216                 { VALID_20, VALID_20, WLAN_RC_PHY_HT_20_SS, 39000, /* 39 Mb */
217                         36700, 0x84, 0x00, 4,
218                         8, 14, 3, 16, 32, 16, 32, 19304 },
219                 { INVALID, VALID_20, WLAN_RC_PHY_HT_20_SS, 52000, /* 52 Mb */
220                         48100, 0x85, 0x00, 5,
221                         8, 20, 3, 17, 33, 17, 33, 25740 },
222                 { INVALID,  VALID_20, WLAN_RC_PHY_HT_20_SS, 58500, /* 58.5 Mb */
223                         53500, 0x86, 0x00, 6,
224                         8, 23, 3, 18, 34, 18, 34, 28956 },
225                 { INVALID, VALID_20, WLAN_RC_PHY_HT_20_SS, 65000, /* 65 Mb */
226                         59000, 0x87, 0x00, 7,
227                         8, 25, 3, 19, 35, 19, 36, 32180 },
228                 { INVALID, INVALID, WLAN_RC_PHY_HT_20_DS, 13000, /* 13 Mb */
229                         12700, 0x88, 0x00, 8,
230                         4, 2, 3, 20, 37, 20, 37, 6430 },
231                 { INVALID, INVALID, WLAN_RC_PHY_HT_20_DS, 26000, /* 26 Mb */
232                         24800, 0x89, 0x00, 9,
233                         6, 4, 3, 21, 38, 21, 38, 12860 },
234                 { INVALID, INVALID, WLAN_RC_PHY_HT_20_DS, 39000, /* 39 Mb */
235                         36600, 0x8a, 0x00, 10,
236                         6, 6, 3, 22, 39, 22, 39, 19300 },
237                 { VALID_20, INVALID, WLAN_RC_PHY_HT_20_DS, 52000, /* 52 Mb */
238                         48100, 0x8b, 0x00, 11,
239                         8, 10, 3, 23, 40, 23, 40, 25736 },
240                 { VALID_20, INVALID, WLAN_RC_PHY_HT_20_DS, 78000, /* 78 Mb */
241                         69500, 0x8c, 0x00, 12,
242                         8, 14, 3, 24, 41, 24, 41, 38600 },
243                 { VALID_20, INVALID, WLAN_RC_PHY_HT_20_DS, 104000, /* 104 Mb */
244                         89500, 0x8d, 0x00, 13,
245                         8, 20, 3, 25, 42, 25, 42, 51472 },
246                 { VALID_20, INVALID, WLAN_RC_PHY_HT_20_DS, 117000, /* 117 Mb */
247                         98900, 0x8e, 0x00, 14,
248                         8, 23, 3, 26, 43, 26, 44, 57890 },
249                 { VALID_20, INVALID, WLAN_RC_PHY_HT_20_DS, 130000, /* 130 Mb */
250                         108300, 0x8f, 0x00, 15,
251                         8, 25, 3, 27, 44, 27, 45, 64320 },
252                 { VALID_40, VALID_40, WLAN_RC_PHY_HT_40_SS, 13500, /* 13.5 Mb */
253                         13200, 0x80, 0x00, 0,
254                         8, 2, 3, 12, 28, 28, 28, 6684 },
255                 { VALID_40, VALID_40, WLAN_RC_PHY_HT_40_SS, 27500, /* 27.0 Mb */
256                         25900, 0x81, 0x00, 1,
257                         8, 4, 3, 13, 29, 29, 29, 13368 },
258                 { VALID_40, VALID_40, WLAN_RC_PHY_HT_40_SS, 40500, /* 40.5 Mb */
259                         38600, 0x82, 0x00, 2,
260                         8, 6, 3, 14, 30, 30, 30, 20052 },
261                 { VALID_40, VALID_40, WLAN_RC_PHY_HT_40_SS, 54000, /* 54 Mb */
262                         49800, 0x83, 0x00, 3,
263                         8, 10, 3, 15, 31, 31, 31, 26738 },
264                 { VALID_40, VALID_40, WLAN_RC_PHY_HT_40_SS, 81500, /* 81 Mb */
265                         72200, 0x84, 0x00, 4,
266                         8, 14, 3, 16, 32, 32, 32, 40104 },
267                 { INVALID, VALID_40, WLAN_RC_PHY_HT_40_SS, 108000, /* 108 Mb */
268                         92900, 0x85, 0x00, 5,
269                         8, 20, 3, 17, 33, 33, 33, 53476 },
270                 { INVALID,  VALID_40, WLAN_RC_PHY_HT_40_SS, 121500, /* 121.5 Mb */
271                         102700, 0x86, 0x00, 6,
272                         8, 23, 3, 18, 34, 34, 34, 60156 },
273                 { INVALID, VALID_40, WLAN_RC_PHY_HT_40_SS, 135000, /* 135 Mb */
274                         112000, 0x87, 0x00, 7,
275                         8, 23, 3, 19, 35, 36, 36, 66840 },
276                 { INVALID, VALID_40, WLAN_RC_PHY_HT_40_SS_HGI, 150000, /* 150 Mb */
277                         122000, 0x87, 0x00, 7,
278                         8, 25, 3, 19, 35, 36, 36, 74200 },
279                 { INVALID, INVALID, WLAN_RC_PHY_HT_40_DS, 27000, /* 27 Mb */
280                         25800, 0x88, 0x00, 8,
281                         8, 2, 3, 20, 37, 37, 37, 13360 },
282                 { INVALID, INVALID, WLAN_RC_PHY_HT_40_DS, 54000, /* 54 Mb */
283                         49800, 0x89, 0x00, 9,
284                         8, 4, 3, 21, 38, 38, 38, 26720 },
285                 { INVALID, INVALID, WLAN_RC_PHY_HT_40_DS, 81000, /* 81 Mb */
286                         71900, 0x8a, 0x00, 10,
287                         8, 6, 3, 22, 39, 39, 39, 40080 },
288                 { VALID_40, INVALID, WLAN_RC_PHY_HT_40_DS, 108000, /* 108 Mb */
289                         92500, 0x8b, 0x00, 11,
290                         8, 10, 3, 23, 40, 40, 40, 53440 },
291                 { VALID_40, INVALID, WLAN_RC_PHY_HT_40_DS, 162000, /* 162 Mb */
292                         130300, 0x8c, 0x00, 12,
293                         8, 14, 3, 24, 41, 41, 41, 80160 },
294                 { VALID_40, INVALID, WLAN_RC_PHY_HT_40_DS, 216000, /* 216 Mb */
295                         162800, 0x8d, 0x00, 13,
296                         8, 20, 3, 25, 42, 42, 42, 106880 },
297                 { VALID_40, INVALID, WLAN_RC_PHY_HT_40_DS, 243000, /* 243 Mb */
298                         178200, 0x8e, 0x00, 14,
299                         8, 23, 3, 26, 43, 43, 43, 120240 },
300                 { VALID_40, INVALID, WLAN_RC_PHY_HT_40_DS, 270000, /* 270 Mb */
301                         192100, 0x8f, 0x00, 15,
302                         8, 23, 3, 27, 44, 45, 45, 133600 },
303                 { VALID_40, INVALID, WLAN_RC_PHY_HT_40_DS_HGI, 300000, /* 300 Mb */
304                         207000, 0x8f, 0x00, 15,
305                         8, 25, 3, 27, 44, 45, 45, 148400 },
306                 },
307         50,  /* probe interval */
308         50,  /* rssi reduce interval */
309         WLAN_RC_HT_FLAG,  /* Phy rates allowed initially */
310 };
311
312 static struct ath_rate_table ar5416_11a_ratetable = {
313         8,
314         {0},
315         {
316                 { VALID, VALID, WLAN_RC_PHY_OFDM, 6000, /* 6 Mb */
317                         5400, 0x0b, 0x00, (0x80|12),
318                         0, 2, 1, 0, 0 },
319                 { VALID, VALID, WLAN_RC_PHY_OFDM, 9000, /* 9 Mb */
320                         7800, 0x0f, 0x00, 18,
321                         0, 3, 1, 1, 0 },
322                 { VALID, VALID, WLAN_RC_PHY_OFDM, 12000, /* 12 Mb */
323                         10000, 0x0a, 0x00, (0x80|24),
324                         2, 4, 2, 2, 0 },
325                 { VALID, VALID, WLAN_RC_PHY_OFDM, 18000, /* 18 Mb */
326                         13900, 0x0e, 0x00, 36,
327                         2, 6, 2, 3, 0 },
328                 { VALID, VALID, WLAN_RC_PHY_OFDM, 24000, /* 24 Mb */
329                         17300, 0x09, 0x00, (0x80|48),
330                         4, 10, 3, 4, 0 },
331                 { VALID, VALID, WLAN_RC_PHY_OFDM, 36000, /* 36 Mb */
332                         23000, 0x0d, 0x00, 72,
333                         4, 14, 3, 5, 0 },
334                 { VALID, VALID, WLAN_RC_PHY_OFDM, 48000, /* 48 Mb */
335                         27400, 0x08, 0x00, 96,
336                         4, 19, 3, 6, 0 },
337                 { VALID, VALID, WLAN_RC_PHY_OFDM, 54000, /* 54 Mb */
338                         29300, 0x0c, 0x00, 108,
339                         4, 23, 3, 7, 0 },
340         },
341         50,  /* probe interval */
342         50,  /* rssi reduce interval */
343         0,   /* Phy rates allowed initially */
344 };
345
346 static struct ath_rate_table ar5416_11g_ratetable = {
347         12,
348         {0},
349         {
350                 { VALID, VALID, WLAN_RC_PHY_CCK, 1000, /* 1 Mb */
351                         900, 0x1b, 0x00, 2,
352                         0, 0, 1, 0, 0 },
353                 { VALID, VALID, WLAN_RC_PHY_CCK, 2000, /* 2 Mb */
354                         1900, 0x1a, 0x04, 4,
355                         1, 1, 1, 1, 0 },
356                 { VALID, VALID, WLAN_RC_PHY_CCK, 5500, /* 5.5 Mb */
357                         4900, 0x19, 0x04, 11,
358                         2, 2, 2, 2, 0 },
359                 { VALID, VALID, WLAN_RC_PHY_CCK, 11000, /* 11 Mb */
360                         8100, 0x18, 0x04, 22,
361                         3, 3, 2, 3, 0 },
362                 { INVALID, INVALID, WLAN_RC_PHY_OFDM, 6000, /* 6 Mb */
363                         5400, 0x0b, 0x00, 12,
364                         4, 2, 1, 4, 0 },
365                 { INVALID, INVALID, WLAN_RC_PHY_OFDM, 9000, /* 9 Mb */
366                         7800, 0x0f, 0x00, 18,
367                         4, 3, 1, 5, 0 },
368                 { VALID, VALID, WLAN_RC_PHY_OFDM, 12000, /* 12 Mb */
369                         10000, 0x0a, 0x00, 24,
370                         6, 4, 1, 6, 0 },
371                 { VALID, VALID, WLAN_RC_PHY_OFDM, 18000, /* 18 Mb */
372                         13900, 0x0e, 0x00, 36,
373                         6, 6, 2, 7, 0 },
374                 { VALID, VALID, WLAN_RC_PHY_OFDM, 24000, /* 24 Mb */
375                         17300, 0x09, 0x00, 48,
376                         8, 10, 3, 8, 0 },
377                 { VALID, VALID, WLAN_RC_PHY_OFDM, 36000, /* 36 Mb */
378                         23000, 0x0d, 0x00, 72,
379                         8, 14, 3, 9, 0 },
380                 { VALID, VALID, WLAN_RC_PHY_OFDM, 48000, /* 48 Mb */
381                         27400, 0x08, 0x00, 96,
382                         8, 19, 3, 10, 0 },
383                 { VALID, VALID, WLAN_RC_PHY_OFDM, 54000, /* 54 Mb */
384                         29300, 0x0c, 0x00, 108,
385                         8, 23, 3, 11, 0 },
386         },
387         50,  /* probe interval */
388         50,  /* rssi reduce interval */
389         0,   /* Phy rates allowed initially */
390 };
391
392 static struct ath_rate_table ar5416_11b_ratetable = {
393         4,
394         {0},
395         {
396                 { VALID, VALID, WLAN_RC_PHY_CCK, 1000, /* 1 Mb */
397                         900, 0x1b,  0x00, (0x80|2),
398                         0, 0, 1, 0, 0 },
399                 { VALID, VALID, WLAN_RC_PHY_CCK, 2000, /* 2 Mb */
400                         1800, 0x1a, 0x04, (0x80|4),
401                         1, 1, 1, 1, 0 },
402                 { VALID, VALID, WLAN_RC_PHY_CCK, 5500, /* 5.5 Mb */
403                         4300, 0x19, 0x04, (0x80|11),
404                         1, 2, 2, 2, 0 },
405                 { VALID, VALID, WLAN_RC_PHY_CCK, 11000, /* 11 Mb */
406                         7100, 0x18, 0x04, (0x80|22),
407                         1, 4, 100, 3, 0 },
408         },
409         100, /* probe interval */
410         100, /* rssi reduce interval */
411         0,   /* Phy rates allowed initially */
412 };
413
414 static inline int8_t median(int8_t a, int8_t b, int8_t c)
415 {
416         if (a >= b) {
417                 if (b >= c)
418                         return b;
419                 else if (a > c)
420                         return c;
421                 else
422                         return a;
423         } else {
424                 if (a >= c)
425                         return a;
426                 else if (b >= c)
427                         return c;
428                 else
429                         return b;
430         }
431 }
432
433 static void ath_rc_sort_validrates(struct ath_rate_table *rate_table,
434                                    struct ath_rate_priv *ath_rc_priv)
435 {
436         u8 i, j, idx, idx_next;
437
438         for (i = ath_rc_priv->max_valid_rate - 1; i > 0; i--) {
439                 for (j = 0; j <= i-1; j++) {
440                         idx = ath_rc_priv->valid_rate_index[j];
441                         idx_next = ath_rc_priv->valid_rate_index[j+1];
442
443                         if (rate_table->info[idx].ratekbps >
444                                 rate_table->info[idx_next].ratekbps) {
445                                 ath_rc_priv->valid_rate_index[j] = idx_next;
446                                 ath_rc_priv->valid_rate_index[j+1] = idx;
447                         }
448                 }
449         }
450 }
451
452 static void ath_rc_init_valid_txmask(struct ath_rate_priv *ath_rc_priv)
453 {
454         u8 i;
455
456         for (i = 0; i < ath_rc_priv->rate_table_size; i++)
457                 ath_rc_priv->valid_rate_index[i] = 0;
458 }
459
460 static inline void ath_rc_set_valid_txmask(struct ath_rate_priv *ath_rc_priv,
461                                            u8 index, int valid_tx_rate)
462 {
463         ASSERT(index <= ath_rc_priv->rate_table_size);
464         ath_rc_priv->valid_rate_index[index] = valid_tx_rate ? 1 : 0;
465 }
466
467 static inline int ath_rc_isvalid_txmask(struct ath_rate_priv *ath_rc_priv,
468                                         u8 index)
469 {
470         ASSERT(index <= ath_rc_priv->rate_table_size);
471         return ath_rc_priv->valid_rate_index[index];
472 }
473
474 static inline int
475 ath_rc_get_nextvalid_txrate(struct ath_rate_table *rate_table,
476                             struct ath_rate_priv *ath_rc_priv,
477                             u8 cur_valid_txrate,
478                             u8 *next_idx)
479 {
480         u8 i;
481
482         for (i = 0; i < ath_rc_priv->max_valid_rate - 1; i++) {
483                 if (ath_rc_priv->valid_rate_index[i] == cur_valid_txrate) {
484                         *next_idx = ath_rc_priv->valid_rate_index[i+1];
485                         return 1;
486                 }
487         }
488
489         /* No more valid rates */
490         *next_idx = 0;
491         return 0;
492 }
493
494 /* Return true only for single stream */
495
496 static int ath_rc_valid_phyrate(u32 phy, u32 capflag, int ignore_cw)
497 {
498         if (WLAN_RC_PHY_HT(phy) & !(capflag & WLAN_RC_HT_FLAG))
499                 return 0;
500         if (WLAN_RC_PHY_DS(phy) && !(capflag & WLAN_RC_DS_FLAG))
501                 return 0;
502         if (WLAN_RC_PHY_SGI(phy) && !(capflag & WLAN_RC_SGI_FLAG))
503                 return 0;
504         if (!ignore_cw && WLAN_RC_PHY_HT(phy))
505                 if (WLAN_RC_PHY_40(phy) && !(capflag & WLAN_RC_40_FLAG))
506                         return 0;
507                 if (!WLAN_RC_PHY_40(phy) && (capflag & WLAN_RC_40_FLAG))
508                         return 0;
509         return 1;
510 }
511
512 static inline int
513 ath_rc_get_nextlowervalid_txrate(struct ath_rate_table *rate_table,
514                                  struct ath_rate_priv *ath_rc_priv,
515                                  u8 cur_valid_txrate, u8 *next_idx)
516 {
517         int8_t i;
518
519         for (i = 1; i < ath_rc_priv->max_valid_rate ; i++) {
520                 if (ath_rc_priv->valid_rate_index[i] == cur_valid_txrate) {
521                         *next_idx = ath_rc_priv->valid_rate_index[i-1];
522                         return 1;
523                 }
524         }
525         return 0;
526 }
527
528 static u8
529 ath_rc_sib_init_validrates(struct ath_rate_priv *ath_rc_priv,
530                            struct ath_rate_table *rate_table,
531                            u32 capflag)
532 {
533         u8 i, hi = 0;
534         u32 valid;
535
536         for (i = 0; i < rate_table->rate_cnt; i++) {
537                 valid = (ath_rc_priv->single_stream ?
538                          rate_table->info[i].valid_single_stream :
539                          rate_table->info[i].valid);
540                 if (valid == 1) {
541                         u32 phy = rate_table->info[i].phy;
542                         u8 valid_rate_count = 0;
543
544                         if (!ath_rc_valid_phyrate(phy, capflag, 0))
545                                 continue;
546
547                         valid_rate_count = ath_rc_priv->valid_phy_ratecnt[phy];
548
549                         ath_rc_priv->valid_phy_rateidx[phy][valid_rate_count] = i;
550                         ath_rc_priv->valid_phy_ratecnt[phy] += 1;
551                         ath_rc_set_valid_txmask(ath_rc_priv, i, 1);
552                         hi = A_MAX(hi, i);
553                 }
554         }
555         return hi;
556 }
557
558 static u8
559 ath_rc_sib_setvalid_rates(struct ath_rate_priv *ath_rc_priv,
560                           struct ath_rate_table *rate_table,
561                           struct ath_rateset *rateset,
562                           u32 capflag)
563 {
564         u8 i, j, hi = 0;
565
566         /* Use intersection of working rates and valid rates */
567         for (i = 0; i < rateset->rs_nrates; i++) {
568                 for (j = 0; j < rate_table->rate_cnt; j++) {
569                         u32 phy = rate_table->info[j].phy;
570                         u32 valid = (ath_rc_priv->single_stream ?
571                                 rate_table->info[j].valid_single_stream :
572                                 rate_table->info[j].valid);
573
574                         /* We allow a rate only if its valid and the
575                          * capflag matches one of the validity
576                          * (VALID/VALID_20/VALID_40) flags */
577
578                         if (((rateset->rs_rates[i] & 0x7F) ==
579                                 (rate_table->info[j].dot11rate & 0x7F)) &&
580                                 ((valid & WLAN_RC_CAP_MODE(capflag)) ==
581                                 WLAN_RC_CAP_MODE(capflag)) &&
582                                 !WLAN_RC_PHY_HT(phy)) {
583
584                                 u8 valid_rate_count = 0;
585
586                                 if (!ath_rc_valid_phyrate(phy, capflag, 0))
587                                         continue;
588
589                                 valid_rate_count =
590                                         ath_rc_priv->valid_phy_ratecnt[phy];
591
592                                 ath_rc_priv->valid_phy_rateidx[phy]
593                                         [valid_rate_count] = j;
594                                 ath_rc_priv->valid_phy_ratecnt[phy] += 1;
595                                 ath_rc_set_valid_txmask(ath_rc_priv, j, 1);
596                                 hi = A_MAX(hi, j);
597                         }
598                 }
599         }
600         return hi;
601 }
602
603 static u8
604 ath_rc_sib_setvalid_htrates(struct ath_rate_priv *ath_rc_priv,
605                             struct ath_rate_table *rate_table,
606                             u8 *mcs_set, u32 capflag)
607 {
608         u8 i, j, hi = 0;
609
610         /* Use intersection of working rates and valid rates */
611         for (i = 0; i <  ((struct ath_rateset *)mcs_set)->rs_nrates; i++) {
612                 for (j = 0; j < rate_table->rate_cnt; j++) {
613                         u32 phy = rate_table->info[j].phy;
614                         u32 valid = (ath_rc_priv->single_stream ?
615                                      rate_table->info[j].valid_single_stream :
616                                      rate_table->info[j].valid);
617
618                         if (((((struct ath_rateset *)
619                                mcs_set)->rs_rates[i] & 0x7F) !=
620                              (rate_table->info[j].dot11rate & 0x7F)) ||
621                             !WLAN_RC_PHY_HT(phy) ||
622                             !WLAN_RC_PHY_HT_VALID(valid, capflag))
623                                 continue;
624
625                         if (!ath_rc_valid_phyrate(phy, capflag, 0))
626                                 continue;
627
628                         ath_rc_priv->valid_phy_rateidx[phy]
629                                 [ath_rc_priv->valid_phy_ratecnt[phy]] = j;
630                         ath_rc_priv->valid_phy_ratecnt[phy] += 1;
631                         ath_rc_set_valid_txmask(ath_rc_priv, j, 1);
632                         hi = A_MAX(hi, j);
633                 }
634         }
635         return hi;
636 }
637
638 u8 ath_rate_findrateix(struct ath_softc *sc,
639                        u8 dot11rate)
640 {
641         struct ath_rate_table *ratetable;
642         int i;
643
644         ratetable = sc->hw_rate_table[sc->sc_curmode];
645
646         if (WARN_ON(!ratetable))
647                 return 0;
648
649         for (i = 0; i < ratetable->rate_cnt; i++) {
650                 if ((ratetable->info[i].dot11rate & 0x7f) == (dot11rate & 0x7f))
651                         return i;
652         }
653
654         return 0;
655 }
656
657 static u8 ath_rc_ratefind_ht(struct ath_softc *sc,
658                              struct ath_rate_priv *ath_rc_priv,
659                              struct ath_rate_table *rate_table,
660                              int probe_allowed, int *is_probing,
661                              int is_retry)
662 {
663         u32 dt, best_thruput, this_thruput, now_msec;
664         u8 rate, next_rate, best_rate, maxindex, minindex;
665         int8_t  rssi_last, rssi_reduce = 0, index = 0;
666
667         *is_probing = 0;
668
669         rssi_last = median(ath_rc_priv->rssi_last,
670                            ath_rc_priv->rssi_last_prev,
671                            ath_rc_priv->rssi_last_prev2);
672
673         /*
674          * Age (reduce) last ack rssi based on how old it is.
675          * The bizarre numbers are so the delta is 160msec,
676          * meaning we divide by 16.
677          *   0msec   <= dt <= 25msec:   don't derate
678          *   25msec  <= dt <= 185msec:  derate linearly from 0 to 10dB
679          *   185msec <= dt:             derate by 10dB
680          */
681
682         now_msec = jiffies_to_msecs(jiffies);
683         dt = now_msec - ath_rc_priv->rssi_time;
684
685         if (dt >= 185)
686                 rssi_reduce = 10;
687         else if (dt >= 25)
688                 rssi_reduce = (u8)((dt - 25) >> 4);
689
690         /* Now reduce rssi_last by rssi_reduce */
691         if (rssi_last < rssi_reduce)
692                 rssi_last = 0;
693         else
694                 rssi_last -= rssi_reduce;
695
696         /*
697          * Now look up the rate in the rssi table and return it.
698          * If no rates match then we return 0 (lowest rate)
699          */
700
701         best_thruput = 0;
702         maxindex = ath_rc_priv->max_valid_rate-1;
703
704         minindex = 0;
705         best_rate = minindex;
706
707         /*
708          * Try the higher rate first. It will reduce memory moving time
709          * if we have very good channel characteristics.
710          */
711         for (index = maxindex; index >= minindex ; index--) {
712                 u8 per_thres;
713
714                 rate = ath_rc_priv->valid_rate_index[index];
715                 if (rate > ath_rc_priv->rate_max_phy)
716                         continue;
717
718                 /*
719                  * For TCP the average collision rate is around 11%,
720                  * so we ignore PERs less than this.  This is to
721                  * prevent the rate we are currently using (whose
722                  * PER might be in the 10-15 range because of TCP
723                  * collisions) looking worse than the next lower
724                  * rate whose PER has decayed close to 0.  If we
725                  * used to next lower rate, its PER would grow to
726                  * 10-15 and we would be worse off then staying
727                  * at the current rate.
728                  */
729                 per_thres = ath_rc_priv->state[rate].per;
730                 if (per_thres < 12)
731                         per_thres = 12;
732
733                 this_thruput = rate_table->info[rate].user_ratekbps *
734                         (100 - per_thres);
735
736                 if (best_thruput <= this_thruput) {
737                         best_thruput = this_thruput;
738                         best_rate    = rate;
739                 }
740         }
741
742         rate = best_rate;
743
744         /* if we are retrying for more than half the number
745          * of max retries, use the min rate for the next retry
746          */
747         if (is_retry)
748                 rate = ath_rc_priv->valid_rate_index[minindex];
749
750         ath_rc_priv->rssi_last_lookup = rssi_last;
751
752         /*
753          * Must check the actual rate (ratekbps) to account for
754          * non-monoticity of 11g's rate table
755          */
756
757         if (rate >= ath_rc_priv->rate_max_phy && probe_allowed) {
758                 rate = ath_rc_priv->rate_max_phy;
759
760                 /* Probe the next allowed phy state */
761                 /* FIXME:XXXX Check to make sure ratMax is checked properly */
762                 if (ath_rc_get_nextvalid_txrate(rate_table,
763                                                 ath_rc_priv, rate, &next_rate) &&
764                     (now_msec - ath_rc_priv->probe_time >
765                      rate_table->probe_interval) &&
766                     (ath_rc_priv->hw_maxretry_pktcnt >= 1)) {
767                         rate = next_rate;
768                         ath_rc_priv->probe_rate = rate;
769                         ath_rc_priv->probe_time = now_msec;
770                         ath_rc_priv->hw_maxretry_pktcnt = 0;
771                         *is_probing = 1;
772                 }
773         }
774
775         if (rate > (ath_rc_priv->rate_table_size - 1))
776                 rate = ath_rc_priv->rate_table_size - 1;
777
778         ASSERT((rate_table->info[rate].valid && !ath_rc_priv->single_stream) ||
779                (rate_table->info[rate].valid_single_stream &&
780                 ath_rc_priv->single_stream));
781
782         return rate;
783 }
784
785 static void ath_rc_rate_set_series(struct ath_rate_table *rate_table ,
786                                    struct ieee80211_tx_rate *rate,
787                                    u8 tries,
788                                    u8 rix,
789                                    int rtsctsenable)
790 {
791         rate->count = tries;
792         rate->idx = rix;
793
794         if (rtsctsenable)
795                 rate->flags |= IEEE80211_TX_RC_USE_RTS_CTS;
796         if (WLAN_RC_PHY_40(rate_table->info[rix].phy))
797                 rate->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
798         if (WLAN_RC_PHY_SGI(rate_table->info[rix].phy))
799                 rate->flags |= IEEE80211_TX_RC_SHORT_GI;
800         if (WLAN_RC_PHY_HT(rate_table->info[rix].phy))
801                 rate->flags |= IEEE80211_TX_RC_MCS;
802 }
803
804 static u8 ath_rc_rate_getidx(struct ath_softc *sc,
805                              struct ath_rate_priv *ath_rc_priv,
806                              struct ath_rate_table *rate_table,
807                              u8 rix, u16 stepdown,
808                              u16 min_rate)
809 {
810         u32 j;
811         u8 nextindex;
812
813         if (min_rate) {
814                 for (j = RATE_TABLE_SIZE; j > 0; j--) {
815                         if (ath_rc_get_nextlowervalid_txrate(rate_table,
816                                                 ath_rc_priv, rix, &nextindex))
817                                 rix = nextindex;
818                         else
819                                 break;
820                 }
821         } else {
822                 for (j = stepdown; j > 0; j--) {
823                         if (ath_rc_get_nextlowervalid_txrate(rate_table,
824                                                 ath_rc_priv, rix, &nextindex))
825                                 rix = nextindex;
826                         else
827                                 break;
828                 }
829         }
830         return rix;
831 }
832
833 static void ath_rc_ratefind(struct ath_softc *sc,
834                             struct ath_rate_priv *ath_rc_priv,
835                             int num_tries, int num_rates,
836                             struct ieee80211_tx_info *tx_info, int *is_probe,
837                             int is_retry)
838 {
839         u8 try_per_rate = 0, i = 0, rix, nrix;
840         struct ath_rate_table *rate_table;
841         struct ieee80211_tx_rate *rates = tx_info->control.rates;
842
843         rate_table = sc->hw_rate_table[sc->sc_curmode];
844         rix = ath_rc_ratefind_ht(sc, ath_rc_priv, rate_table, 1,
845                                  is_probe, is_retry);
846         nrix = rix;
847
848         if (*is_probe) {
849                 /* set one try for probe rates. For the
850                  * probes don't enable rts */
851                 ath_rc_rate_set_series(rate_table,
852                         &rates[i++], 1, nrix, 0);
853
854                 try_per_rate = (num_tries/num_rates);
855                 /* Get the next tried/allowed rate. No RTS for the next series
856                  * after the probe rate
857                  */
858                 nrix = ath_rc_rate_getidx(sc,
859                         ath_rc_priv, rate_table, nrix, 1, 0);
860                 ath_rc_rate_set_series(rate_table,
861                         &rates[i++], try_per_rate, nrix, 0);
862         } else {
863                 try_per_rate = (num_tries/num_rates);
864                 /* Set the choosen rate. No RTS for first series entry. */
865                 ath_rc_rate_set_series(rate_table,
866                         &rates[i++], try_per_rate, nrix, 0);
867         }
868
869         /* Fill in the other rates for multirate retry */
870         for ( ; i < num_rates; i++) {
871                 u8 try_num;
872                 u8 min_rate;
873
874                 try_num = ((i + 1) == num_rates) ?
875                         num_tries - (try_per_rate * i) : try_per_rate ;
876                 min_rate = (((i + 1) == num_rates) && 0);
877
878                 nrix = ath_rc_rate_getidx(sc, ath_rc_priv,
879                                           rate_table, nrix, 1, min_rate);
880                 /* All other rates in the series have RTS enabled */
881                 ath_rc_rate_set_series(rate_table,
882                                        &rates[i], try_num, nrix, 1);
883         }
884
885         /*
886          * NB:Change rate series to enable aggregation when operating
887          * at lower MCS rates. When first rate in series is MCS2
888          * in HT40 @ 2.4GHz, series should look like:
889          *
890          * {MCS2, MCS1, MCS0, MCS0}.
891          *
892          * When first rate in series is MCS3 in HT20 @ 2.4GHz, series should
893          * look like:
894          *
895          * {MCS3, MCS2, MCS1, MCS1}
896          *
897          * So, set fourth rate in series to be same as third one for
898          * above conditions.
899          */
900         if ((sc->sc_curmode == ATH9K_MODE_11NG_HT20) ||
901             (sc->sc_curmode == ATH9K_MODE_11NG_HT40PLUS) ||
902             (sc->sc_curmode == ATH9K_MODE_11NG_HT40MINUS)) {
903                 u8  dot11rate = rate_table->info[rix].dot11rate;
904                 u8 phy = rate_table->info[rix].phy;
905                 if (i == 4 &&
906                     ((dot11rate == 2 && phy == WLAN_RC_PHY_HT_40_SS) ||
907                      (dot11rate == 3 && phy == WLAN_RC_PHY_HT_20_SS))) {
908                         rates[3].idx = rates[2].idx;
909                         rates[3].flags = rates[2].flags;
910                 }
911         }
912 }
913
914 static void ath_rc_update_ht(struct ath_softc *sc,
915                              struct ath_rate_priv *ath_rc_priv,
916                              struct ath_tx_info_priv *tx_info_priv,
917                              int tx_rate, int xretries, int retries)
918 {
919         u32 now_msec = jiffies_to_msecs(jiffies);
920         int state_change = 0, rate, count;
921         u8 last_per;
922         struct ath_rate_table *rate_table = sc->hw_rate_table[sc->sc_curmode];
923         static u32 nretry_to_per_lookup[10] = {
924                 100 * 0 / 1,
925                 100 * 1 / 4,
926                 100 * 1 / 2,
927                 100 * 3 / 4,
928                 100 * 4 / 5,
929                 100 * 5 / 6,
930                 100 * 6 / 7,
931                 100 * 7 / 8,
932                 100 * 8 / 9,
933                 100 * 9 / 10
934         };
935
936         if (!ath_rc_priv)
937                 return;
938
939         ASSERT(tx_rate >= 0);
940         if (tx_rate < 0)
941                 return;
942
943         /* To compensate for some imbalance between ctrl and ext. channel */
944
945         if (WLAN_RC_PHY_40(rate_table->info[tx_rate].phy))
946                 tx_info_priv->tx.ts_rssi =
947                         tx_info_priv->tx.ts_rssi < 3 ? 0 :
948                         tx_info_priv->tx.ts_rssi - 3;
949
950         last_per = ath_rc_priv->state[tx_rate].per;
951
952         if (xretries) {
953                 /* Update the PER. */
954                 if (xretries == 1) {
955                         ath_rc_priv->state[tx_rate].per += 30;
956                         if (ath_rc_priv->state[tx_rate].per > 100)
957                                 ath_rc_priv->state[tx_rate].per = 100;
958                 } else {
959                         /* xretries == 2 */
960                         count = ARRAY_SIZE(nretry_to_per_lookup);
961                         if (retries >= count)
962                                 retries = count - 1;
963                         /* new_PER = 7/8*old_PER + 1/8*(currentPER) */
964                         ath_rc_priv->state[tx_rate].per =
965                                 (u8)(ath_rc_priv->state[tx_rate].per -
966                                      (ath_rc_priv->state[tx_rate].per >> 3) +
967                                      ((100) >> 3));
968                 }
969
970                 /* xretries == 1 or 2 */
971
972                 if (ath_rc_priv->probe_rate == tx_rate)
973                         ath_rc_priv->probe_rate = 0;
974
975         } else {        /* xretries == 0 */
976                 /* Update the PER. */
977                 /* Make sure it doesn't index out of array's bounds. */
978                 count = ARRAY_SIZE(nretry_to_per_lookup);
979                 if (retries >= count)
980                         retries = count - 1;
981                 if (tx_info_priv->n_bad_frames) {
982                         /* new_PER = 7/8*old_PER + 1/8*(currentPER)
983                          * Assuming that n_frames is not 0.  The current PER
984                          * from the retries is 100 * retries / (retries+1),
985                          * since the first retries attempts failed, and the
986                          * next one worked.  For the one that worked,
987                          * n_bad_frames subframes out of n_frames wored,
988                          * so the PER for that part is
989                          * 100 * n_bad_frames / n_frames, and it contributes
990                          * 100 * n_bad_frames / (n_frames * (retries+1)) to
991                          * the above PER.  The expression below is a
992                          * simplified version of the sum of these two terms.
993                          */
994                         if (tx_info_priv->n_frames > 0)
995                                 ath_rc_priv->state[tx_rate].per
996                                       = (u8)
997                                         (ath_rc_priv->state[tx_rate].per -
998                                         (ath_rc_priv->state[tx_rate].per >> 3) +
999                                         ((100*(retries*tx_info_priv->n_frames +
1000                                         tx_info_priv->n_bad_frames) /
1001                                         (tx_info_priv->n_frames *
1002                                                 (retries+1))) >> 3));
1003                 } else {
1004                         /* new_PER = 7/8*old_PER + 1/8*(currentPER) */
1005
1006                         ath_rc_priv->state[tx_rate].per = (u8)
1007                                 (ath_rc_priv->state[tx_rate].per -
1008                                 (ath_rc_priv->state[tx_rate].per >> 3) +
1009                                 (nretry_to_per_lookup[retries] >> 3));
1010                 }
1011
1012                 ath_rc_priv->rssi_last_prev2 = ath_rc_priv->rssi_last_prev;
1013                 ath_rc_priv->rssi_last_prev  = ath_rc_priv->rssi_last;
1014                 ath_rc_priv->rssi_last = tx_info_priv->tx.ts_rssi;
1015                 ath_rc_priv->rssi_time = now_msec;
1016
1017                 /*
1018                  * If we got at most one retry then increase the max rate if
1019                  * this was a probe.  Otherwise, ignore the probe.
1020                  */
1021
1022                 if (ath_rc_priv->probe_rate && ath_rc_priv->probe_rate == tx_rate) {
1023                         if (retries > 0 || 2 * tx_info_priv->n_bad_frames >
1024                                 tx_info_priv->n_frames) {
1025                                 /*
1026                                  * Since we probed with just a single attempt,
1027                                  * any retries means the probe failed.  Also,
1028                                  * if the attempt worked, but more than half
1029                                  * the subframes were bad then also consider
1030                                  * the probe a failure.
1031                                  */
1032                                 ath_rc_priv->probe_rate = 0;
1033                         } else {
1034                                 u8 probe_rate = 0;
1035
1036                                 ath_rc_priv->rate_max_phy = ath_rc_priv->probe_rate;
1037                                 probe_rate = ath_rc_priv->probe_rate;
1038
1039                                 if (ath_rc_priv->state[probe_rate].per > 30)
1040                                         ath_rc_priv->state[probe_rate].per = 20;
1041
1042                                 ath_rc_priv->probe_rate = 0;
1043
1044                                 /*
1045                                  * Since this probe succeeded, we allow the next
1046                                  * probe twice as soon.  This allows the maxRate
1047                                  * to move up faster if the probes are
1048                                  * succesful.
1049                                  */
1050                                 ath_rc_priv->probe_time = now_msec -
1051                                         rate_table->probe_interval / 2;
1052                         }
1053                 }
1054
1055                 if (retries > 0) {
1056                         /*
1057                          * Don't update anything.  We don't know if
1058                          * this was because of collisions or poor signal.
1059                          *
1060                          * Later: if rssi_ack is close to
1061                          * ath_rc_priv->state[txRate].rssi_thres and we see lots
1062                          * of retries, then we could increase
1063                          * ath_rc_priv->state[txRate].rssi_thres.
1064                          */
1065                         ath_rc_priv->hw_maxretry_pktcnt = 0;
1066                 } else {
1067                         /*
1068                          * It worked with no retries. First ignore bogus (small)
1069                          * rssi_ack values.
1070                          */
1071                         if (tx_rate == ath_rc_priv->rate_max_phy &&
1072                             ath_rc_priv->hw_maxretry_pktcnt < 255) {
1073                                 ath_rc_priv->hw_maxretry_pktcnt++;
1074                         }
1075
1076                         if (tx_info_priv->tx.ts_rssi >=
1077                                 rate_table->info[tx_rate].rssi_ack_validmin) {
1078                                 /* Average the rssi */
1079                                 if (tx_rate != ath_rc_priv->rssi_sum_rate) {
1080                                         ath_rc_priv->rssi_sum_rate = tx_rate;
1081                                         ath_rc_priv->rssi_sum =
1082                                                 ath_rc_priv->rssi_sum_cnt = 0;
1083                                 }
1084
1085                                 ath_rc_priv->rssi_sum += tx_info_priv->tx.ts_rssi;
1086                                 ath_rc_priv->rssi_sum_cnt++;
1087
1088                                 if (ath_rc_priv->rssi_sum_cnt > 4) {
1089                                         int32_t rssi_ackAvg =
1090                                                 (ath_rc_priv->rssi_sum + 2) / 4;
1091                                         int8_t rssi_thres =
1092                                                 ath_rc_priv->state[tx_rate].
1093                                                 rssi_thres;
1094                                         int8_t rssi_ack_vmin =
1095                                                 rate_table->info[tx_rate].
1096                                                 rssi_ack_validmin;
1097
1098                                         ath_rc_priv->rssi_sum =
1099                                                 ath_rc_priv->rssi_sum_cnt = 0;
1100
1101                                         /* Now reduce the current
1102                                          * rssi threshold. */
1103                                         if ((rssi_ackAvg < rssi_thres + 2) &&
1104                                             (rssi_thres > rssi_ack_vmin)) {
1105                                                 ath_rc_priv->state[tx_rate].
1106                                                         rssi_thres--;
1107                                         }
1108
1109                                         state_change = 1;
1110                                 }
1111                         }
1112                 }
1113         }
1114
1115         /* For all cases */
1116
1117         /*
1118          * If this rate looks bad (high PER) then stop using it for
1119          * a while (except if we are probing).
1120          */
1121         if (ath_rc_priv->state[tx_rate].per >= 55 && tx_rate > 0 &&
1122             rate_table->info[tx_rate].ratekbps <=
1123             rate_table->info[ath_rc_priv->rate_max_phy].ratekbps) {
1124                 ath_rc_get_nextlowervalid_txrate(rate_table, ath_rc_priv,
1125                                  (u8) tx_rate, &ath_rc_priv->rate_max_phy);
1126
1127                 /* Don't probe for a little while. */
1128                 ath_rc_priv->probe_time = now_msec;
1129         }
1130
1131         if (state_change) {
1132                 /*
1133                  * Make sure the rates above this have higher rssi thresholds.
1134                  * (Note:  Monotonicity is kept within the OFDM rates and
1135                  *         within the CCK rates. However, no adjustment is
1136                  *         made to keep the rssi thresholds monotonically
1137                  *         increasing between the CCK and OFDM rates.)
1138                  */
1139                 for (rate = tx_rate; rate <
1140                                 ath_rc_priv->rate_table_size - 1; rate++) {
1141                         if (rate_table->info[rate+1].phy !=
1142                                 rate_table->info[tx_rate].phy)
1143                                 break;
1144
1145                         if (ath_rc_priv->state[rate].rssi_thres +
1146                             rate_table->info[rate].rssi_ack_deltamin >
1147                             ath_rc_priv->state[rate+1].rssi_thres) {
1148                                 ath_rc_priv->state[rate+1].rssi_thres =
1149                                         ath_rc_priv->state[rate].
1150                                         rssi_thres +
1151                                         rate_table->info[rate].
1152                                         rssi_ack_deltamin;
1153                         }
1154                 }
1155
1156                 /* Make sure the rates below this have lower rssi thresholds. */
1157                 for (rate = tx_rate - 1; rate >= 0; rate--) {
1158                         if (rate_table->info[rate].phy !=
1159                             rate_table->info[tx_rate].phy)
1160                                 break;
1161
1162                         if (ath_rc_priv->state[rate].rssi_thres +
1163                             rate_table->info[rate].rssi_ack_deltamin >
1164                             ath_rc_priv->state[rate+1].rssi_thres) {
1165                                 if (ath_rc_priv->state[rate+1].rssi_thres <
1166                                     rate_table->info[rate].
1167                                     rssi_ack_deltamin)
1168                                         ath_rc_priv->state[rate].rssi_thres = 0;
1169                                 else {
1170                                         ath_rc_priv->state[rate].rssi_thres =
1171                                                 ath_rc_priv->state[rate+1].
1172                                                 rssi_thres -
1173                                                 rate_table->info[rate].
1174                                                 rssi_ack_deltamin;
1175                                 }
1176
1177                                 if (ath_rc_priv->state[rate].rssi_thres <
1178                                     rate_table->info[rate].
1179                                     rssi_ack_validmin) {
1180                                         ath_rc_priv->state[rate].rssi_thres =
1181                                                 rate_table->info[rate].
1182                                                 rssi_ack_validmin;
1183                                 }
1184                         }
1185                 }
1186         }
1187
1188         /* Make sure the rates below this have lower PER */
1189         /* Monotonicity is kept only for rates below the current rate. */
1190         if (ath_rc_priv->state[tx_rate].per < last_per) {
1191                 for (rate = tx_rate - 1; rate >= 0; rate--) {
1192                         if (rate_table->info[rate].phy !=
1193                             rate_table->info[tx_rate].phy)
1194                                 break;
1195
1196                         if (ath_rc_priv->state[rate].per >
1197                             ath_rc_priv->state[rate+1].per) {
1198                                 ath_rc_priv->state[rate].per =
1199                                         ath_rc_priv->state[rate+1].per;
1200                         }
1201                 }
1202         }
1203
1204         /* Maintain monotonicity for rates above the current rate */
1205         for (rate = tx_rate; rate < ath_rc_priv->rate_table_size - 1; rate++) {
1206                 if (ath_rc_priv->state[rate+1].per < ath_rc_priv->state[rate].per)
1207                         ath_rc_priv->state[rate+1].per =
1208                                 ath_rc_priv->state[rate].per;
1209         }
1210
1211         /* Every so often, we reduce the thresholds and
1212          * PER (different for CCK and OFDM). */
1213         if (now_msec - ath_rc_priv->rssi_down_time >=
1214             rate_table->rssi_reduce_interval) {
1215
1216                 for (rate = 0; rate < ath_rc_priv->rate_table_size; rate++) {
1217                         if (ath_rc_priv->state[rate].rssi_thres >
1218                             rate_table->info[rate].rssi_ack_validmin)
1219                                 ath_rc_priv->state[rate].rssi_thres -= 1;
1220                 }
1221                 ath_rc_priv->rssi_down_time = now_msec;
1222         }
1223
1224         /* Every so often, we reduce the thresholds
1225          * and PER (different for CCK and OFDM). */
1226         if (now_msec - ath_rc_priv->per_down_time >=
1227             rate_table->rssi_reduce_interval) {
1228                 for (rate = 0; rate < ath_rc_priv->rate_table_size; rate++) {
1229                         ath_rc_priv->state[rate].per =
1230                                 7 * ath_rc_priv->state[rate].per / 8;
1231                 }
1232
1233                 ath_rc_priv->per_down_time = now_msec;
1234         }
1235 }
1236
1237 static void ath_rc_tx_status(struct ath_softc *sc,
1238                              struct ath_rate_priv *ath_rc_priv,
1239                              struct ieee80211_tx_info *tx_info,
1240                              int final_ts_idx, int xretries, int long_retry)
1241 {
1242         struct ath_tx_info_priv *tx_info_priv = ATH_TX_INFO_PRIV(tx_info);
1243         struct ath_rate_table *rate_table;
1244         struct ieee80211_tx_rate *rates = tx_info->status.rates;
1245         u8 flags;
1246         u32 series = 0, rix;
1247
1248         rate_table = sc->hw_rate_table[sc->sc_curmode];
1249
1250         /*
1251          * If the first rate is not the final index, there
1252          * are intermediate rate failures to be processed.
1253          */
1254         if (final_ts_idx != 0) {
1255                 /* Process intermediate rates that failed.*/
1256                 for (series = 0; series < final_ts_idx ; series++) {
1257                         if (rates[series].count != 0 && (rates[series].idx >= 0)) {
1258                                 flags = rates[series].flags;
1259                                 /* If HT40 and we have switched mode from
1260                                  * 40 to 20 => don't update */
1261                                 if ((flags & IEEE80211_TX_RC_40_MHZ_WIDTH) &&
1262                                     (ath_rc_priv->rc_phy_mode != WLAN_RC_40_FLAG))
1263                                         return;
1264
1265                                 if ((flags & IEEE80211_TX_RC_40_MHZ_WIDTH) &&
1266                                         (flags & IEEE80211_TX_RC_SHORT_GI))
1267                                         rix = rate_table->info[
1268                                                 rates[series].idx].ht_index;
1269                                 else if (flags & IEEE80211_TX_RC_SHORT_GI)
1270                                         rix = rate_table->info[
1271                                                 rates[series].idx].sgi_index;
1272                                 else if (flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1273                                         rix = rate_table->info[
1274                                                 rates[series].idx].cw40index;
1275                                 else
1276                                         rix = rate_table->info[
1277                                                 rates[series].idx].base_index;
1278                                 ath_rc_update_ht(sc, ath_rc_priv,
1279                                                 tx_info_priv, rix,
1280                                                 xretries ? 1 : 2,
1281                                                 rates[series].count);
1282                         }
1283                 }
1284         } else {
1285                 /*
1286                  * Handle the special case of MIMO PS burst, where the second
1287                  * aggregate is sent out with only one rate and one try.
1288                  * Treating it as an excessive retry penalizes the rate
1289                  * inordinately.
1290                  */
1291                 if (rates[0].count == 1 && xretries == 1)
1292                         xretries = 2;
1293         }
1294
1295         flags = rates[series].flags;
1296         /* If HT40 and we have switched mode from 40 to 20 => don't update */
1297         if ((flags & IEEE80211_TX_RC_40_MHZ_WIDTH) &&
1298             (ath_rc_priv->rc_phy_mode != WLAN_RC_40_FLAG)) {
1299                 return;
1300         }
1301
1302         if ((flags & IEEE80211_TX_RC_40_MHZ_WIDTH) && (flags & IEEE80211_TX_RC_SHORT_GI))
1303                 rix = rate_table->info[rates[series].idx].ht_index;
1304         else if (flags & IEEE80211_TX_RC_SHORT_GI)
1305                 rix = rate_table->info[rates[series].idx].sgi_index;
1306         else if (flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1307                 rix = rate_table->info[rates[series].idx].cw40index;
1308         else
1309                 rix = rate_table->info[rates[series].idx].base_index;
1310
1311         ath_rc_update_ht(sc, ath_rc_priv, tx_info_priv, rix,
1312                 xretries, long_retry);
1313 }
1314
1315 static void ath_rc_init(struct ath_softc *sc,
1316                         struct ath_rate_priv *ath_rc_priv,
1317                         struct ieee80211_supported_band *sband,
1318                         struct ieee80211_sta *sta)
1319 {
1320         struct ath_rate_table *rate_table = NULL;
1321         struct ath_rateset *rateset = &ath_rc_priv->neg_rates;
1322         u8 *ht_mcs = (u8 *)&ath_rc_priv->neg_ht_rates;
1323         u8 i, j, k, hi = 0, hthi = 0;
1324
1325         rate_table = sc->hw_rate_table[sc->sc_curmode];
1326
1327         if (sta->ht_cap.ht_supported) {
1328                 if (sband->band == IEEE80211_BAND_2GHZ)
1329                         rate_table = sc->hw_rate_table[ATH9K_MODE_11NG_HT20];
1330                 else
1331                         rate_table = sc->hw_rate_table[ATH9K_MODE_11NA_HT20];
1332
1333                 ath_rc_priv->ht_cap = (WLAN_RC_HT_FLAG | WLAN_RC_DS_FLAG);
1334
1335                 if (sta->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40)
1336                         ath_rc_priv->ht_cap |= WLAN_RC_40_FLAG;
1337         }
1338
1339         /* Initial rate table size. Will change depending
1340          * on the working rate set */
1341         ath_rc_priv->rate_table_size = RATE_TABLE_SIZE;
1342
1343         /* Initialize thresholds according to the global rate table */
1344         for (i = 0 ; i < ath_rc_priv->rate_table_size; i++) {
1345                 ath_rc_priv->state[i].rssi_thres =
1346                         rate_table->info[i].rssi_ack_validmin;
1347                 ath_rc_priv->state[i].per = 0;
1348         }
1349
1350         /* Determine the valid rates */
1351         ath_rc_init_valid_txmask(ath_rc_priv);
1352
1353         for (i = 0; i < WLAN_RC_PHY_MAX; i++) {
1354                 for (j = 0; j < MAX_TX_RATE_PHY; j++)
1355                         ath_rc_priv->valid_phy_rateidx[i][j] = 0;
1356                 ath_rc_priv->valid_phy_ratecnt[i] = 0;
1357         }
1358         ath_rc_priv->rc_phy_mode = (ath_rc_priv->ht_cap & WLAN_RC_40_FLAG);
1359
1360         /* Set stream capability */
1361         ath_rc_priv->single_stream = (ath_rc_priv->ht_cap & WLAN_RC_DS_FLAG) ? 0 : 1;
1362
1363         if (!rateset->rs_nrates) {
1364                 /* No working rate, just initialize valid rates */
1365                 hi = ath_rc_sib_init_validrates(ath_rc_priv, rate_table,
1366                                                 ath_rc_priv->ht_cap);
1367         } else {
1368                 /* Use intersection of working rates and valid rates */
1369                 hi = ath_rc_sib_setvalid_rates(ath_rc_priv, rate_table,
1370                                                rateset, ath_rc_priv->ht_cap);
1371                 if (ath_rc_priv->ht_cap & WLAN_RC_HT_FLAG) {
1372                         hthi = ath_rc_sib_setvalid_htrates(ath_rc_priv,
1373                                                            rate_table,
1374                                                            ht_mcs,
1375                                                            ath_rc_priv->ht_cap);
1376                 }
1377                 hi = A_MAX(hi, hthi);
1378         }
1379
1380         ath_rc_priv->rate_table_size = hi + 1;
1381         ath_rc_priv->rate_max_phy = 0;
1382         ASSERT(ath_rc_priv->rate_table_size <= RATE_TABLE_SIZE);
1383
1384         for (i = 0, k = 0; i < WLAN_RC_PHY_MAX; i++) {
1385                 for (j = 0; j < ath_rc_priv->valid_phy_ratecnt[i]; j++) {
1386                         ath_rc_priv->valid_rate_index[k++] =
1387                                 ath_rc_priv->valid_phy_rateidx[i][j];
1388                 }
1389
1390                 if (!ath_rc_valid_phyrate(i, rate_table->initial_ratemax, 1)
1391                     || !ath_rc_priv->valid_phy_ratecnt[i])
1392                         continue;
1393
1394                 ath_rc_priv->rate_max_phy = ath_rc_priv->valid_phy_rateidx[i][j-1];
1395         }
1396         ASSERT(ath_rc_priv->rate_table_size <= RATE_TABLE_SIZE);
1397         ASSERT(k <= RATE_TABLE_SIZE);
1398
1399         ath_rc_priv->max_valid_rate = k;
1400         ath_rc_sort_validrates(rate_table, ath_rc_priv);
1401         ath_rc_priv->rate_max_phy = ath_rc_priv->valid_rate_index[k-4];
1402 }
1403
1404 /* Rate Control callbacks */
1405 static void ath_tx_status(void *priv, struct ieee80211_supported_band *sband,
1406                           struct ieee80211_sta *sta, void *priv_sta,
1407                           struct sk_buff *skb)
1408 {
1409         struct ath_softc *sc = priv;
1410         struct ath_rate_priv *ath_rc_priv = priv_sta;
1411         struct ath_tx_info_priv *tx_info_priv = NULL;
1412         struct ath_node *an;
1413         struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
1414         struct ieee80211_hdr *hdr;
1415         int final_ts_idx, tx_status = 0, is_underrun = 0;
1416         __le16 fc;
1417
1418         hdr = (struct ieee80211_hdr *)skb->data;
1419         fc = hdr->frame_control;
1420         tx_info_priv = ATH_TX_INFO_PRIV(tx_info);
1421         an = (struct ath_node *)sta->drv_priv;
1422         final_ts_idx = tx_info_priv->tx.ts_rateindex;
1423
1424         if (!an || !priv_sta || !ieee80211_is_data(fc))
1425                 goto exit;
1426
1427         if (tx_info_priv->tx.ts_status & ATH9K_TXERR_FILT)
1428                 goto exit;
1429
1430         if (tx_info_priv->tx.ts_rssi > 0) {
1431                 ATH_RSSI_LPF(an->an_chainmask_sel.tx_avgrssi,
1432                              tx_info_priv->tx.ts_rssi);
1433         }
1434
1435         /*
1436          * If underrun error is seen assume it as an excessive retry only
1437          * if prefetch trigger level have reached the max (0x3f for 5416)
1438          * Adjust the long retry as if the frame was tried ATH_11N_TXMAXTRY
1439          * times. This affects how ratectrl updates PER for the failed rate.
1440          */
1441         if (tx_info_priv->tx.ts_flags &
1442             (ATH9K_TX_DATA_UNDERRUN | ATH9K_TX_DELIM_UNDERRUN) &&
1443             ((sc->sc_ah->ah_txTrigLevel) >= ath_rc_priv->tx_triglevel_max)) {
1444                 tx_status = 1;
1445                 is_underrun = 1;
1446         }
1447
1448         if ((tx_info_priv->tx.ts_status & ATH9K_TXERR_XRETRY) ||
1449             (tx_info_priv->tx.ts_status & ATH9K_TXERR_FIFO))
1450                 tx_status = 1;
1451
1452         ath_rc_tx_status(sc, ath_rc_priv, tx_info, final_ts_idx, tx_status,
1453                          (is_underrun) ? ATH_11N_TXMAXTRY :
1454                          tx_info_priv->tx.ts_longretry);
1455
1456 exit:
1457         kfree(tx_info_priv);
1458 }
1459
1460 static void ath_get_rate(void *priv, struct ieee80211_sta *sta, void *priv_sta,
1461                          struct ieee80211_tx_rate_control *txrc)
1462 {
1463         struct ieee80211_supported_band *sband = txrc->sband;
1464         struct sk_buff *skb = txrc->skb;
1465         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1466         struct ath_softc *sc = priv;
1467         struct ieee80211_hw *hw = sc->hw;
1468         struct ath_rate_priv *ath_rc_priv = priv_sta;
1469         struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
1470         int is_probe = 0;
1471         __le16 fc = hdr->frame_control;
1472
1473         /* lowest rate for management and multicast/broadcast frames */
1474         if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1)) {
1475                 tx_info->control.rates[0].idx = rate_lowest_index(sband, sta);
1476                 tx_info->control.rates[0].count =
1477                         is_multicast_ether_addr(hdr->addr1) ? 1 : ATH_MGT_TXMAXTRY;
1478                 return;
1479         }
1480
1481         /* Find tx rate for unicast frames */
1482         ath_rc_ratefind(sc, ath_rc_priv, ATH_11N_TXMAXTRY, 4,
1483                         tx_info, &is_probe, false);
1484
1485         /* Check if aggregation has to be enabled for this tid */
1486         if (hw->conf.ht.enabled) {
1487                 if (ieee80211_is_data_qos(fc)) {
1488                         u8 *qc, tid;
1489                         struct ath_node *an;
1490
1491                         qc = ieee80211_get_qos_ctl(hdr);
1492                         tid = qc[0] & 0xf;
1493                         an = (struct ath_node *)sta->drv_priv;
1494
1495                         if(ath_tx_aggr_check(sc, an, tid))
1496                                 ieee80211_start_tx_ba_session(hw, hdr->addr1, tid);
1497                 }
1498         }
1499 }
1500
1501 static void ath_rate_init(void *priv, struct ieee80211_supported_band *sband,
1502                           struct ieee80211_sta *sta, void *priv_sta)
1503 {
1504         struct ath_softc *sc = priv;
1505         struct ath_rate_priv *ath_rc_priv = priv_sta;
1506         int i, j = 0;
1507
1508         for (i = 0; i < sband->n_bitrates; i++) {
1509                 if (sta->supp_rates[sband->band] & BIT(i)) {
1510                         ath_rc_priv->neg_rates.rs_rates[j]
1511                                 = (sband->bitrates[i].bitrate * 2) / 10;
1512                         j++;
1513                 }
1514         }
1515         ath_rc_priv->neg_rates.rs_nrates = j;
1516
1517         if (sta->ht_cap.ht_supported) {
1518                 for (i = 0, j = 0; i < 77; i++) {
1519                         if (sta->ht_cap.mcs.rx_mask[i/8] & (1<<(i%8)))
1520                                 ath_rc_priv->neg_ht_rates.rs_rates[j++] = i;
1521                         if (j == ATH_RATE_MAX)
1522                                 break;
1523                 }
1524                 ath_rc_priv->neg_ht_rates.rs_nrates = j;
1525         }
1526
1527         ath_rc_init(sc, priv_sta, sband, sta);
1528 }
1529
1530 static void *ath_rate_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
1531 {
1532         return hw->priv;
1533 }
1534
1535 static void ath_rate_free(void *priv)
1536 {
1537         return;
1538 }
1539
1540 static void *ath_rate_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp)
1541 {
1542         struct ath_softc *sc = priv;
1543         struct ath_rate_priv *rate_priv;
1544
1545         rate_priv = kzalloc(sizeof(struct ath_rate_priv), gfp);
1546         if (!rate_priv) {
1547                 DPRINTF(sc, ATH_DBG_FATAL,
1548                         "%s: Unable to allocate private rc structure\n",
1549                         __func__);
1550                 return NULL;
1551         }
1552
1553         rate_priv->rssi_down_time = jiffies_to_msecs(jiffies);
1554         rate_priv->tx_triglevel_max = sc->sc_ah->ah_caps.tx_triglevel_max;
1555
1556         return rate_priv;
1557 }
1558
1559 static void ath_rate_free_sta(void *priv, struct ieee80211_sta *sta,
1560                               void *priv_sta)
1561 {
1562         struct ath_rate_priv *rate_priv = priv_sta;
1563         kfree(rate_priv);
1564 }
1565
1566 static struct rate_control_ops ath_rate_ops = {
1567         .module = NULL,
1568         .name = "ath9k_rate_control",
1569         .tx_status = ath_tx_status,
1570         .get_rate = ath_get_rate,
1571         .rate_init = ath_rate_init,
1572         .alloc = ath_rate_alloc,
1573         .free = ath_rate_free,
1574         .alloc_sta = ath_rate_alloc_sta,
1575         .free_sta = ath_rate_free_sta,
1576 };
1577
1578 static void ath_setup_rate_table(struct ath_softc *sc,
1579                                  struct ath_rate_table *rate_table)
1580 {
1581         int i;
1582
1583         for (i = 0; i < 256; i++)
1584                 rate_table->rateCodeToIndex[i] = (u8)-1;
1585
1586         for (i = 0; i < rate_table->rate_cnt; i++) {
1587                 u8 code = rate_table->info[i].ratecode;
1588                 u8 cix = rate_table->info[i].ctrl_rate;
1589                 u8 sh = rate_table->info[i].short_preamble;
1590
1591                 rate_table->rateCodeToIndex[code] = i;
1592                 rate_table->rateCodeToIndex[code | sh] = i;
1593
1594                 rate_table->info[i].lpAckDuration =
1595                         ath9k_hw_computetxtime(sc->sc_ah, rate_table,
1596                                                WLAN_CTRL_FRAME_SIZE,
1597                                                cix,
1598                                                false);
1599                 rate_table->info[i].spAckDuration =
1600                         ath9k_hw_computetxtime(sc->sc_ah, rate_table,
1601                                                WLAN_CTRL_FRAME_SIZE,
1602                                                cix,
1603                                                true);
1604         }
1605 }
1606
1607 void ath_rate_attach(struct ath_softc *sc)
1608 {
1609         sc->hw_rate_table[ATH9K_MODE_11B] =
1610                 &ar5416_11b_ratetable;
1611         sc->hw_rate_table[ATH9K_MODE_11A] =
1612                 &ar5416_11a_ratetable;
1613         sc->hw_rate_table[ATH9K_MODE_11G] =
1614                 &ar5416_11g_ratetable;
1615         sc->hw_rate_table[ATH9K_MODE_11NA_HT20] =
1616                 &ar5416_11na_ratetable;
1617         sc->hw_rate_table[ATH9K_MODE_11NG_HT20] =
1618                 &ar5416_11ng_ratetable;
1619         sc->hw_rate_table[ATH9K_MODE_11NA_HT40PLUS] =
1620                 &ar5416_11na_ratetable;
1621         sc->hw_rate_table[ATH9K_MODE_11NA_HT40MINUS] =
1622                 &ar5416_11na_ratetable;
1623         sc->hw_rate_table[ATH9K_MODE_11NG_HT40PLUS] =
1624                 &ar5416_11ng_ratetable;
1625         sc->hw_rate_table[ATH9K_MODE_11NG_HT40MINUS] =
1626                 &ar5416_11ng_ratetable;
1627
1628         ath_setup_rate_table(sc, &ar5416_11b_ratetable);
1629         ath_setup_rate_table(sc, &ar5416_11a_ratetable);
1630         ath_setup_rate_table(sc, &ar5416_11g_ratetable);
1631         ath_setup_rate_table(sc, &ar5416_11na_ratetable);
1632         ath_setup_rate_table(sc, &ar5416_11ng_ratetable);
1633 }
1634
1635 int ath_rate_control_register(void)
1636 {
1637         return ieee80211_rate_control_register(&ath_rate_ops);
1638 }
1639
1640 void ath_rate_control_unregister(void)
1641 {
1642         ieee80211_rate_control_unregister(&ath_rate_ops);
1643 }