Merge branch 'rc-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/mmarek/kbuild
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / net / wireless / ath / ath9k / rc.c
CommitLineData
f078f209
LR
1/*
2 * Copyright (c) 2004 Video54 Technologies, Inc.
5b68138e 3 * Copyright (c) 2004-2011 Atheros Communications, Inc.
f078f209
LR
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
5a0e3ad6 18#include <linux/slab.h>
ee40fa06 19#include <linux/export.h>
5a0e3ad6 20
394cf0a1 21#include "ath9k.h"
f078f209 22
4f0fc7c3 23static const struct ath_rate_table ar5416_11na_ratetable = {
1d9d06a2 24 68,
545750d3 25 8, /* MCS start */
f078f209 26 {
1d9d06a2 27 [0] = { RC_L_SDT, WLAN_RC_PHY_OFDM, 6000,
23d99394 28 5400, 0, 12 }, /* 6 Mb */
1d9d06a2 29 [1] = { RC_L_SDT, WLAN_RC_PHY_OFDM, 9000,
23d99394 30 7800, 1, 18 }, /* 9 Mb */
1d9d06a2 31 [2] = { RC_L_SDT, WLAN_RC_PHY_OFDM, 12000,
23d99394 32 10000, 2, 24 }, /* 12 Mb */
1d9d06a2 33 [3] = { RC_L_SDT, WLAN_RC_PHY_OFDM, 18000,
23d99394 34 13900, 3, 36 }, /* 18 Mb */
1d9d06a2 35 [4] = { RC_L_SDT, WLAN_RC_PHY_OFDM, 24000,
23d99394 36 17300, 4, 48 }, /* 24 Mb */
1d9d06a2 37 [5] = { RC_L_SDT, WLAN_RC_PHY_OFDM, 36000,
23d99394 38 23000, 5, 72 }, /* 36 Mb */
1d9d06a2 39 [6] = { RC_L_SDT, WLAN_RC_PHY_OFDM, 48000,
23d99394 40 27400, 6, 96 }, /* 48 Mb */
1d9d06a2 41 [7] = { RC_L_SDT, WLAN_RC_PHY_OFDM, 54000,
23d99394 42 29300, 7, 108 }, /* 54 Mb */
1d9d06a2 43 [8] = { RC_HT_SDT_2040, WLAN_RC_PHY_HT_20_SS, 6500,
23d99394 44 6400, 0, 0 }, /* 6.5 Mb */
1d9d06a2 45 [9] = { RC_HT_SDT_20, WLAN_RC_PHY_HT_20_SS, 13000,
23d99394 46 12700, 1, 1 }, /* 13 Mb */
1d9d06a2 47 [10] = { RC_HT_SDT_20, WLAN_RC_PHY_HT_20_SS, 19500,
23d99394 48 18800, 2, 2 }, /* 19.5 Mb */
1d9d06a2 49 [11] = { RC_HT_SD_20, WLAN_RC_PHY_HT_20_SS, 26000,
23d99394 50 25000, 3, 3 }, /* 26 Mb */
1d9d06a2 51 [12] = { RC_HT_SD_20, WLAN_RC_PHY_HT_20_SS, 39000,
23d99394 52 36700, 4, 4 }, /* 39 Mb */
1d9d06a2 53 [13] = { RC_HT_S_20, WLAN_RC_PHY_HT_20_SS, 52000,
23d99394 54 48100, 5, 5 }, /* 52 Mb */
1d9d06a2 55 [14] = { RC_HT_S_20, WLAN_RC_PHY_HT_20_SS, 58500,
23d99394 56 53500, 6, 6 }, /* 58.5 Mb */
1d9d06a2 57 [15] = { RC_HT_S_20, WLAN_RC_PHY_HT_20_SS, 65000,
23d99394 58 59000, 7, 7 }, /* 65 Mb */
1d9d06a2 59 [16] = { RC_HT_S_20, WLAN_RC_PHY_HT_20_SS_HGI, 72200,
23d99394 60 65400, 7, 7 }, /* 75 Mb */
1d9d06a2 61 [17] = { RC_INVALID, WLAN_RC_PHY_HT_20_DS, 13000,
23d99394 62 12700, 8, 8 }, /* 13 Mb */
1d9d06a2 63 [18] = { RC_HT_T_20, WLAN_RC_PHY_HT_20_DS, 26000,
23d99394 64 24800, 9, 9 }, /* 26 Mb */
1d9d06a2 65 [19] = { RC_HT_T_20, WLAN_RC_PHY_HT_20_DS, 39000,
23d99394 66 36600, 10, 10 }, /* 39 Mb */
1d9d06a2 67 [20] = { RC_HT_DT_20, WLAN_RC_PHY_HT_20_DS, 52000,
23d99394 68 48100, 11, 11 }, /* 52 Mb */
1d9d06a2 69 [21] = { RC_HT_DT_20, WLAN_RC_PHY_HT_20_DS, 78000,
23d99394 70 69500, 12, 12 }, /* 78 Mb */
1d9d06a2 71 [22] = { RC_HT_DT_20, WLAN_RC_PHY_HT_20_DS, 104000,
23d99394 72 89500, 13, 13 }, /* 104 Mb */
1d9d06a2 73 [23] = { RC_HT_DT_20, WLAN_RC_PHY_HT_20_DS, 117000,
23d99394 74 98900, 14, 14 }, /* 117 Mb */
1d9d06a2 75 [24] = { RC_HT_DT_20, WLAN_RC_PHY_HT_20_DS, 130000,
23d99394 76 108300, 15, 15 }, /* 130 Mb */
1d9d06a2 77 [25] = { RC_HT_DT_20, WLAN_RC_PHY_HT_20_DS_HGI, 144400,
23d99394 78 120000, 15, 15 }, /* 144.4 Mb */
1d9d06a2 79 [26] = { RC_INVALID, WLAN_RC_PHY_HT_20_TS, 19500,
23d99394 80 17400, 16, 16 }, /* 19.5 Mb */
1d9d06a2 81 [27] = { RC_INVALID, WLAN_RC_PHY_HT_20_TS, 39000,
23d99394 82 35100, 17, 17 }, /* 39 Mb */
1d9d06a2 83 [28] = { RC_INVALID, WLAN_RC_PHY_HT_20_TS, 58500,
23d99394 84 52600, 18, 18 }, /* 58.5 Mb */
1d9d06a2 85 [29] = { RC_INVALID, WLAN_RC_PHY_HT_20_TS, 78000,
23d99394 86 70400, 19, 19 }, /* 78 Mb */
1d9d06a2 87 [30] = { RC_INVALID, WLAN_RC_PHY_HT_20_TS, 117000,
23d99394 88 104900, 20, 20 }, /* 117 Mb */
1d9d06a2 89 [31] = { RC_INVALID, WLAN_RC_PHY_HT_20_TS_HGI, 130000,
23d99394 90 115800, 20, 20 }, /* 130 Mb*/
1d9d06a2 91 [32] = { RC_HT_T_20, WLAN_RC_PHY_HT_20_TS, 156000,
23d99394 92 137200, 21, 21 }, /* 156 Mb */
1d9d06a2 93 [33] = { RC_HT_T_20, WLAN_RC_PHY_HT_20_TS_HGI, 173300,
23d99394 94 151100, 21, 21 }, /* 173.3 Mb */
1d9d06a2 95 [34] = { RC_HT_T_20, WLAN_RC_PHY_HT_20_TS, 175500,
23d99394 96 152800, 22, 22 }, /* 175.5 Mb */
1d9d06a2 97 [35] = { RC_HT_T_20, WLAN_RC_PHY_HT_20_TS_HGI, 195000,
23d99394 98 168400, 22, 22 }, /* 195 Mb*/
1d9d06a2 99 [36] = { RC_HT_T_20, WLAN_RC_PHY_HT_20_TS, 195000,
23d99394 100 168400, 23, 23 }, /* 195 Mb */
1d9d06a2 101 [37] = { RC_HT_T_20, WLAN_RC_PHY_HT_20_TS_HGI, 216700,
23d99394 102 185000, 23, 23 }, /* 216.7 Mb */
1d9d06a2 103 [38] = { RC_HT_SDT_40, WLAN_RC_PHY_HT_40_SS, 13500,
23d99394 104 13200, 0, 0 }, /* 13.5 Mb*/
1d9d06a2 105 [39] = { RC_HT_SDT_40, WLAN_RC_PHY_HT_40_SS, 27500,
23d99394 106 25900, 1, 1 }, /* 27.0 Mb*/
1d9d06a2 107 [40] = { RC_HT_SDT_40, WLAN_RC_PHY_HT_40_SS, 40500,
23d99394 108 38600, 2, 2 }, /* 40.5 Mb*/
1d9d06a2 109 [41] = { RC_HT_SD_40, WLAN_RC_PHY_HT_40_SS, 54000,
23d99394 110 49800, 3, 3 }, /* 54 Mb */
1d9d06a2 111 [42] = { RC_HT_SD_40, WLAN_RC_PHY_HT_40_SS, 81500,
23d99394 112 72200, 4, 4 }, /* 81 Mb */
1d9d06a2 113 [43] = { RC_HT_S_40, WLAN_RC_PHY_HT_40_SS, 108000,
23d99394 114 92900, 5, 5 }, /* 108 Mb */
1d9d06a2 115 [44] = { RC_HT_S_40, WLAN_RC_PHY_HT_40_SS, 121500,
23d99394 116 102700, 6, 6 }, /* 121.5 Mb*/
1d9d06a2 117 [45] = { RC_HT_S_40, WLAN_RC_PHY_HT_40_SS, 135000,
23d99394 118 112000, 7, 7 }, /* 135 Mb */
1d9d06a2 119 [46] = { RC_HT_S_40, WLAN_RC_PHY_HT_40_SS_HGI, 150000,
23d99394 120 122000, 7, 7 }, /* 150 Mb */
1d9d06a2 121 [47] = { RC_INVALID, WLAN_RC_PHY_HT_40_DS, 27000,
23d99394 122 25800, 8, 8 }, /* 27 Mb */
1d9d06a2 123 [48] = { RC_HT_T_40, WLAN_RC_PHY_HT_40_DS, 54000,
23d99394 124 49800, 9, 9 }, /* 54 Mb */
1d9d06a2 125 [49] = { RC_HT_T_40, WLAN_RC_PHY_HT_40_DS, 81000,
23d99394 126 71900, 10, 10 }, /* 81 Mb */
1d9d06a2 127 [50] = { RC_HT_DT_40, WLAN_RC_PHY_HT_40_DS, 108000,
23d99394 128 92500, 11, 11 }, /* 108 Mb */
1d9d06a2 129 [51] = { RC_HT_DT_40, WLAN_RC_PHY_HT_40_DS, 162000,
23d99394 130 130300, 12, 12 }, /* 162 Mb */
1d9d06a2 131 [52] = { RC_HT_DT_40, WLAN_RC_PHY_HT_40_DS, 216000,
23d99394 132 162800, 13, 13 }, /* 216 Mb */
1d9d06a2 133 [53] = { RC_HT_DT_40, WLAN_RC_PHY_HT_40_DS, 243000,
23d99394 134 178200, 14, 14 }, /* 243 Mb */
1d9d06a2 135 [54] = { RC_HT_DT_40, WLAN_RC_PHY_HT_40_DS, 270000,
23d99394 136 192100, 15, 15 }, /* 270 Mb */
1d9d06a2 137 [55] = { RC_HT_DT_40, WLAN_RC_PHY_HT_40_DS_HGI, 300000,
23d99394 138 207000, 15, 15 }, /* 300 Mb */
1d9d06a2 139 [56] = { RC_INVALID, WLAN_RC_PHY_HT_40_TS, 40500,
23d99394 140 36100, 16, 16 }, /* 40.5 Mb */
1d9d06a2 141 [57] = { RC_INVALID, WLAN_RC_PHY_HT_40_TS, 81000,
23d99394 142 72900, 17, 17 }, /* 81 Mb */
1d9d06a2 143 [58] = { RC_INVALID, WLAN_RC_PHY_HT_40_TS, 121500,
23d99394 144 108300, 18, 18 }, /* 121.5 Mb */
1d9d06a2 145 [59] = { RC_INVALID, WLAN_RC_PHY_HT_40_TS, 162000,
23d99394 146 142000, 19, 19 }, /* 162 Mb */
1d9d06a2 147 [60] = { RC_INVALID, WLAN_RC_PHY_HT_40_TS, 243000,
23d99394 148 205100, 20, 20 }, /* 243 Mb */
1d9d06a2 149 [61] = { RC_INVALID, WLAN_RC_PHY_HT_40_TS_HGI, 270000,
23d99394 150 224700, 20, 20 }, /* 270 Mb */
1d9d06a2 151 [62] = { RC_HT_T_40, WLAN_RC_PHY_HT_40_TS, 324000,
23d99394 152 263100, 21, 21 }, /* 324 Mb */
1d9d06a2 153 [63] = { RC_HT_T_40, WLAN_RC_PHY_HT_40_TS_HGI, 360000,
23d99394 154 288000, 21, 21 }, /* 360 Mb */
1d9d06a2 155 [64] = { RC_HT_T_40, WLAN_RC_PHY_HT_40_TS, 364500,
23d99394 156 290700, 22, 22 }, /* 364.5 Mb */
1d9d06a2 157 [65] = { RC_HT_T_40, WLAN_RC_PHY_HT_40_TS_HGI, 405000,
23d99394 158 317200, 22, 22 }, /* 405 Mb */
1d9d06a2 159 [66] = { RC_HT_T_40, WLAN_RC_PHY_HT_40_TS, 405000,
23d99394 160 317200, 23, 23 }, /* 405 Mb */
1d9d06a2 161 [67] = { RC_HT_T_40, WLAN_RC_PHY_HT_40_TS_HGI, 450000,
23d99394 162 346400, 23, 23 }, /* 450 Mb */
f078f209
LR
163 },
164 50, /* probe interval */
f078f209
LR
165 WLAN_RC_HT_FLAG, /* Phy rates allowed initially */
166};
167
f078f209
LR
168/* 4ms frame limit not used for NG mode. The values filled
169 * for HT are the 64K max aggregate limit */
170
4f0fc7c3 171static const struct ath_rate_table ar5416_11ng_ratetable = {
1d9d06a2 172 72,
545750d3 173 12, /* MCS start */
f078f209 174 {
1d9d06a2 175 [0] = { RC_ALL, WLAN_RC_PHY_CCK, 1000,
23d99394 176 900, 0, 2 }, /* 1 Mb */
1d9d06a2 177 [1] = { RC_ALL, WLAN_RC_PHY_CCK, 2000,
23d99394 178 1900, 1, 4 }, /* 2 Mb */
1d9d06a2 179 [2] = { RC_ALL, WLAN_RC_PHY_CCK, 5500,
23d99394 180 4900, 2, 11 }, /* 5.5 Mb */
1d9d06a2 181 [3] = { RC_ALL, WLAN_RC_PHY_CCK, 11000,
23d99394 182 8100, 3, 22 }, /* 11 Mb */
1d9d06a2 183 [4] = { RC_INVALID, WLAN_RC_PHY_OFDM, 6000,
23d99394 184 5400, 4, 12 }, /* 6 Mb */
1d9d06a2 185 [5] = { RC_INVALID, WLAN_RC_PHY_OFDM, 9000,
23d99394 186 7800, 5, 18 }, /* 9 Mb */
1d9d06a2 187 [6] = { RC_L_SDT, WLAN_RC_PHY_OFDM, 12000,
23d99394 188 10100, 6, 24 }, /* 12 Mb */
1d9d06a2 189 [7] = { RC_L_SDT, WLAN_RC_PHY_OFDM, 18000,
23d99394 190 14100, 7, 36 }, /* 18 Mb */
1d9d06a2 191 [8] = { RC_L_SDT, WLAN_RC_PHY_OFDM, 24000,
23d99394 192 17700, 8, 48 }, /* 24 Mb */
1d9d06a2 193 [9] = { RC_L_SDT, WLAN_RC_PHY_OFDM, 36000,
23d99394 194 23700, 9, 72 }, /* 36 Mb */
1d9d06a2 195 [10] = { RC_L_SDT, WLAN_RC_PHY_OFDM, 48000,
23d99394 196 27400, 10, 96 }, /* 48 Mb */
1d9d06a2 197 [11] = { RC_L_SDT, WLAN_RC_PHY_OFDM, 54000,
23d99394 198 30900, 11, 108 }, /* 54 Mb */
1d9d06a2 199 [12] = { RC_INVALID, WLAN_RC_PHY_HT_20_SS, 6500,
23d99394 200 6400, 0, 0 }, /* 6.5 Mb */
1d9d06a2 201 [13] = { RC_HT_SDT_20, WLAN_RC_PHY_HT_20_SS, 13000,
23d99394 202 12700, 1, 1 }, /* 13 Mb */
1d9d06a2 203 [14] = { RC_HT_SDT_20, WLAN_RC_PHY_HT_20_SS, 19500,
23d99394 204 18800, 2, 2 }, /* 19.5 Mb*/
1d9d06a2 205 [15] = { RC_HT_SD_20, WLAN_RC_PHY_HT_20_SS, 26000,
23d99394 206 25000, 3, 3 }, /* 26 Mb */
1d9d06a2 207 [16] = { RC_HT_SD_20, WLAN_RC_PHY_HT_20_SS, 39000,
23d99394 208 36700, 4, 4 }, /* 39 Mb */
1d9d06a2 209 [17] = { RC_HT_S_20, WLAN_RC_PHY_HT_20_SS, 52000,
23d99394 210 48100, 5, 5 }, /* 52 Mb */
1d9d06a2 211 [18] = { RC_HT_S_20, WLAN_RC_PHY_HT_20_SS, 58500,
23d99394 212 53500, 6, 6 }, /* 58.5 Mb */
1d9d06a2 213 [19] = { RC_HT_S_20, WLAN_RC_PHY_HT_20_SS, 65000,
23d99394 214 59000, 7, 7 }, /* 65 Mb */
1d9d06a2 215 [20] = { RC_HT_S_20, WLAN_RC_PHY_HT_20_SS_HGI, 72200,
23d99394 216 65400, 7, 7 }, /* 65 Mb*/
1d9d06a2 217 [21] = { RC_INVALID, WLAN_RC_PHY_HT_20_DS, 13000,
23d99394 218 12700, 8, 8 }, /* 13 Mb */
1d9d06a2 219 [22] = { RC_HT_T_20, WLAN_RC_PHY_HT_20_DS, 26000,
23d99394 220 24800, 9, 9 }, /* 26 Mb */
1d9d06a2 221 [23] = { RC_HT_T_20, WLAN_RC_PHY_HT_20_DS, 39000,
23d99394 222 36600, 10, 10 }, /* 39 Mb */
1d9d06a2 223 [24] = { RC_HT_DT_20, WLAN_RC_PHY_HT_20_DS, 52000,
23d99394 224 48100, 11, 11 }, /* 52 Mb */
1d9d06a2 225 [25] = { RC_HT_DT_20, WLAN_RC_PHY_HT_20_DS, 78000,
23d99394 226 69500, 12, 12 }, /* 78 Mb */
1d9d06a2 227 [26] = { RC_HT_DT_20, WLAN_RC_PHY_HT_20_DS, 104000,
23d99394 228 89500, 13, 13 }, /* 104 Mb */
1d9d06a2 229 [27] = { RC_HT_DT_20, WLAN_RC_PHY_HT_20_DS, 117000,
23d99394 230 98900, 14, 14 }, /* 117 Mb */
1d9d06a2 231 [28] = { RC_HT_DT_20, WLAN_RC_PHY_HT_20_DS, 130000,
23d99394 232 108300, 15, 15 }, /* 130 Mb */
1d9d06a2 233 [29] = { RC_HT_DT_20, WLAN_RC_PHY_HT_20_DS_HGI, 144400,
23d99394 234 120000, 15, 15 }, /* 144.4 Mb */
1d9d06a2 235 [30] = { RC_INVALID, WLAN_RC_PHY_HT_20_TS, 19500,
23d99394 236 17400, 16, 16 }, /* 19.5 Mb */
1d9d06a2 237 [31] = { RC_INVALID, WLAN_RC_PHY_HT_20_TS, 39000,
23d99394 238 35100, 17, 17 }, /* 39 Mb */
1d9d06a2 239 [32] = { RC_INVALID, WLAN_RC_PHY_HT_20_TS, 58500,
23d99394 240 52600, 18, 18 }, /* 58.5 Mb */
1d9d06a2 241 [33] = { RC_INVALID, WLAN_RC_PHY_HT_20_TS, 78000,
23d99394 242 70400, 19, 19 }, /* 78 Mb */
1d9d06a2 243 [34] = { RC_INVALID, WLAN_RC_PHY_HT_20_TS, 117000,
23d99394 244 104900, 20, 20 }, /* 117 Mb */
1d9d06a2 245 [35] = { RC_INVALID, WLAN_RC_PHY_HT_20_TS_HGI, 130000,
23d99394 246 115800, 20, 20 }, /* 130 Mb */
1d9d06a2 247 [36] = { RC_HT_T_20, WLAN_RC_PHY_HT_20_TS, 156000,
23d99394 248 137200, 21, 21 }, /* 156 Mb */
1d9d06a2 249 [37] = { RC_HT_T_20, WLAN_RC_PHY_HT_20_TS_HGI, 173300,
23d99394 250 151100, 21, 21 }, /* 173.3 Mb */
1d9d06a2 251 [38] = { RC_HT_T_20, WLAN_RC_PHY_HT_20_TS, 175500,
23d99394 252 152800, 22, 22 }, /* 175.5 Mb */
1d9d06a2 253 [39] = { RC_HT_T_20, WLAN_RC_PHY_HT_20_TS_HGI, 195000,
23d99394 254 168400, 22, 22 }, /* 195 Mb */
1d9d06a2 255 [40] = { RC_HT_T_20, WLAN_RC_PHY_HT_20_TS, 195000,
23d99394 256 168400, 23, 23 }, /* 195 Mb */
1d9d06a2 257 [41] = { RC_HT_T_20, WLAN_RC_PHY_HT_20_TS_HGI, 216700,
23d99394 258 185000, 23, 23 }, /* 216.7 Mb */
1d9d06a2 259 [42] = { RC_HT_SDT_40, WLAN_RC_PHY_HT_40_SS, 13500,
23d99394 260 13200, 0, 0 }, /* 13.5 Mb */
1d9d06a2 261 [43] = { RC_HT_SDT_40, WLAN_RC_PHY_HT_40_SS, 27500,
23d99394 262 25900, 1, 1 }, /* 27.0 Mb */
1d9d06a2 263 [44] = { RC_HT_SDT_40, WLAN_RC_PHY_HT_40_SS, 40500,
23d99394 264 38600, 2, 2 }, /* 40.5 Mb */
1d9d06a2 265 [45] = { RC_HT_SD_40, WLAN_RC_PHY_HT_40_SS, 54000,
23d99394 266 49800, 3, 3 }, /* 54 Mb */
1d9d06a2 267 [46] = { RC_HT_SD_40, WLAN_RC_PHY_HT_40_SS, 81500,
23d99394 268 72200, 4, 4 }, /* 81 Mb */
1d9d06a2 269 [47] = { RC_HT_S_40 , WLAN_RC_PHY_HT_40_SS, 108000,
23d99394 270 92900, 5, 5 }, /* 108 Mb */
1d9d06a2 271 [48] = { RC_HT_S_40, WLAN_RC_PHY_HT_40_SS, 121500,
23d99394 272 102700, 6, 6 }, /* 121.5 Mb */
1d9d06a2 273 [49] = { RC_HT_S_40, WLAN_RC_PHY_HT_40_SS, 135000,
23d99394 274 112000, 7, 7 }, /* 135 Mb */
1d9d06a2 275 [50] = { RC_HT_S_40, WLAN_RC_PHY_HT_40_SS_HGI, 150000,
23d99394 276 122000, 7, 7 }, /* 150 Mb */
1d9d06a2 277 [51] = { RC_INVALID, WLAN_RC_PHY_HT_40_DS, 27000,
23d99394 278 25800, 8, 8 }, /* 27 Mb */
1d9d06a2 279 [52] = { RC_HT_T_40, WLAN_RC_PHY_HT_40_DS, 54000,
23d99394 280 49800, 9, 9 }, /* 54 Mb */
1d9d06a2 281 [53] = { RC_HT_T_40, WLAN_RC_PHY_HT_40_DS, 81000,
23d99394 282 71900, 10, 10 }, /* 81 Mb */
1d9d06a2 283 [54] = { RC_HT_DT_40, WLAN_RC_PHY_HT_40_DS, 108000,
23d99394 284 92500, 11, 11 }, /* 108 Mb */
1d9d06a2 285 [55] = { RC_HT_DT_40, WLAN_RC_PHY_HT_40_DS, 162000,
23d99394 286 130300, 12, 12 }, /* 162 Mb */
1d9d06a2 287 [56] = { RC_HT_DT_40, WLAN_RC_PHY_HT_40_DS, 216000,
23d99394 288 162800, 13, 13 }, /* 216 Mb */
1d9d06a2 289 [57] = { RC_HT_DT_40, WLAN_RC_PHY_HT_40_DS, 243000,
23d99394 290 178200, 14, 14 }, /* 243 Mb */
1d9d06a2 291 [58] = { RC_HT_DT_40, WLAN_RC_PHY_HT_40_DS, 270000,
23d99394 292 192100, 15, 15 }, /* 270 Mb */
1d9d06a2 293 [59] = { RC_HT_DT_40, WLAN_RC_PHY_HT_40_DS_HGI, 300000,
23d99394 294 207000, 15, 15 }, /* 300 Mb */
1d9d06a2 295 [60] = { RC_INVALID, WLAN_RC_PHY_HT_40_TS, 40500,
23d99394 296 36100, 16, 16 }, /* 40.5 Mb */
1d9d06a2 297 [61] = { RC_INVALID, WLAN_RC_PHY_HT_40_TS, 81000,
23d99394 298 72900, 17, 17 }, /* 81 Mb */
1d9d06a2 299 [62] = { RC_INVALID, WLAN_RC_PHY_HT_40_TS, 121500,
23d99394 300 108300, 18, 18 }, /* 121.5 Mb */
1d9d06a2 301 [63] = { RC_INVALID, WLAN_RC_PHY_HT_40_TS, 162000,
23d99394 302 142000, 19, 19 }, /* 162 Mb */
1d9d06a2 303 [64] = { RC_INVALID, WLAN_RC_PHY_HT_40_TS, 243000,
23d99394 304 205100, 20, 20 }, /* 243 Mb */
1d9d06a2 305 [65] = { RC_INVALID, WLAN_RC_PHY_HT_40_TS_HGI, 270000,
23d99394 306 224700, 20, 20 }, /* 270 Mb */
1d9d06a2 307 [66] = { RC_HT_T_40, WLAN_RC_PHY_HT_40_TS, 324000,
23d99394 308 263100, 21, 21 }, /* 324 Mb */
1d9d06a2 309 [67] = { RC_HT_T_40, WLAN_RC_PHY_HT_40_TS_HGI, 360000,
23d99394 310 288000, 21, 21 }, /* 360 Mb */
1d9d06a2 311 [68] = { RC_HT_T_40, WLAN_RC_PHY_HT_40_TS, 364500,
23d99394 312 290700, 22, 22 }, /* 364.5 Mb */
1d9d06a2 313 [69] = { RC_HT_T_40, WLAN_RC_PHY_HT_40_TS_HGI, 405000,
23d99394 314 317200, 22, 22 }, /* 405 Mb */
1d9d06a2 315 [70] = { RC_HT_T_40, WLAN_RC_PHY_HT_40_TS, 405000,
23d99394 316 317200, 23, 23 }, /* 405 Mb */
1d9d06a2 317 [71] = { RC_HT_T_40, WLAN_RC_PHY_HT_40_TS_HGI, 450000,
23d99394 318 346400, 23, 23 }, /* 450 Mb */
1fe8234a 319 },
f078f209 320 50, /* probe interval */
f078f209
LR
321 WLAN_RC_HT_FLAG, /* Phy rates allowed initially */
322};
323
4f0fc7c3 324static const struct ath_rate_table ar5416_11a_ratetable = {
f078f209 325 8,
545750d3 326 0,
f078f209 327 {
1d9d06a2 328 { RC_L_SDT, WLAN_RC_PHY_OFDM, 6000, /* 6 Mb */
23d99394 329 5400, 0, 12},
1d9d06a2 330 { RC_L_SDT, WLAN_RC_PHY_OFDM, 9000, /* 9 Mb */
23d99394 331 7800, 1, 18},
1d9d06a2 332 { RC_L_SDT, WLAN_RC_PHY_OFDM, 12000, /* 12 Mb */
23d99394 333 10000, 2, 24},
1d9d06a2 334 { RC_L_SDT, WLAN_RC_PHY_OFDM, 18000, /* 18 Mb */
23d99394 335 13900, 3, 36},
1d9d06a2 336 { RC_L_SDT, WLAN_RC_PHY_OFDM, 24000, /* 24 Mb */
23d99394 337 17300, 4, 48},
1d9d06a2 338 { RC_L_SDT, WLAN_RC_PHY_OFDM, 36000, /* 36 Mb */
23d99394 339 23000, 5, 72},
1d9d06a2 340 { RC_L_SDT, WLAN_RC_PHY_OFDM, 48000, /* 48 Mb */
23d99394 341 27400, 6, 96},
1d9d06a2 342 { RC_L_SDT, WLAN_RC_PHY_OFDM, 54000, /* 54 Mb */
23d99394 343 29300, 7, 108},
f078f209
LR
344 },
345 50, /* probe interval */
f078f209
LR
346 0, /* Phy rates allowed initially */
347};
348
4f0fc7c3 349static const struct ath_rate_table ar5416_11g_ratetable = {
f078f209 350 12,
545750d3 351 0,
f078f209 352 {
1d9d06a2 353 { RC_L_SDT, WLAN_RC_PHY_CCK, 1000, /* 1 Mb */
23d99394 354 900, 0, 2},
1d9d06a2 355 { RC_L_SDT, WLAN_RC_PHY_CCK, 2000, /* 2 Mb */
23d99394 356 1900, 1, 4},
1d9d06a2 357 { RC_L_SDT, WLAN_RC_PHY_CCK, 5500, /* 5.5 Mb */
23d99394 358 4900, 2, 11},
1d9d06a2 359 { RC_L_SDT, WLAN_RC_PHY_CCK, 11000, /* 11 Mb */
23d99394 360 8100, 3, 22},
f63b340d 361 { RC_INVALID, WLAN_RC_PHY_OFDM, 6000, /* 6 Mb */
23d99394 362 5400, 4, 12},
f63b340d 363 { RC_INVALID, WLAN_RC_PHY_OFDM, 9000, /* 9 Mb */
23d99394 364 7800, 5, 18},
1d9d06a2 365 { RC_L_SDT, WLAN_RC_PHY_OFDM, 12000, /* 12 Mb */
23d99394 366 10000, 6, 24},
1d9d06a2 367 { RC_L_SDT, WLAN_RC_PHY_OFDM, 18000, /* 18 Mb */
23d99394 368 13900, 7, 36},
1d9d06a2 369 { RC_L_SDT, WLAN_RC_PHY_OFDM, 24000, /* 24 Mb */
23d99394 370 17300, 8, 48},
1d9d06a2 371 { RC_L_SDT, WLAN_RC_PHY_OFDM, 36000, /* 36 Mb */
23d99394 372 23000, 9, 72},
1d9d06a2 373 { RC_L_SDT, WLAN_RC_PHY_OFDM, 48000, /* 48 Mb */
23d99394 374 27400, 10, 96},
1d9d06a2 375 { RC_L_SDT, WLAN_RC_PHY_OFDM, 54000, /* 54 Mb */
23d99394 376 29300, 11, 108},
f078f209
LR
377 },
378 50, /* probe interval */
f078f209
LR
379 0, /* Phy rates allowed initially */
380};
381
23d99394 382static int ath_rc_get_rateindex(struct ath_rate_priv *ath_rc_priv,
9dff3870
MSS
383 struct ieee80211_tx_rate *rate)
384{
23d99394
SM
385 const struct ath_rate_table *rate_table = ath_rc_priv->rate_table;
386 int rix, i, idx = 0;
9dff3870
MSS
387
388 if (!(rate->flags & IEEE80211_TX_RC_MCS))
389 return rate->idx;
390
23d99394
SM
391 for (i = 0; i < ath_rc_priv->max_valid_rate; i++) {
392 idx = ath_rc_priv->valid_rate_index[i];
393
394 if (WLAN_RC_PHY_HT(rate_table->info[idx].phy) &&
395 rate_table->info[idx].ratecode == rate->idx)
396 break;
9dff3870
MSS
397 }
398
23d99394 399 rix = idx;
9dff3870 400
23d99394
SM
401 if (rate->flags & IEEE80211_TX_RC_SHORT_GI)
402 rix++;
9dff3870
MSS
403
404 return rix;
405}
545750d3 406
ea2771f6 407static void ath_rc_sort_validrates(struct ath_rate_priv *ath_rc_priv)
f078f209 408{
ea2771f6 409 const struct ath_rate_table *rate_table = ath_rc_priv->rate_table;
f078f209
LR
410 u8 i, j, idx, idx_next;
411
256b7759 412 for (i = ath_rc_priv->max_valid_rate - 1; i > 0; i--) {
f078f209 413 for (j = 0; j <= i-1; j++) {
256b7759
S
414 idx = ath_rc_priv->valid_rate_index[j];
415 idx_next = ath_rc_priv->valid_rate_index[j+1];
f078f209
LR
416
417 if (rate_table->info[idx].ratekbps >
418 rate_table->info[idx_next].ratekbps) {
256b7759
S
419 ath_rc_priv->valid_rate_index[j] = idx_next;
420 ath_rc_priv->valid_rate_index[j+1] = idx;
f078f209
LR
421 }
422 }
423 }
424}
425
4f0fc7c3
LR
426static inline
427int ath_rc_get_nextvalid_txrate(const struct ath_rate_table *rate_table,
428 struct ath_rate_priv *ath_rc_priv,
429 u8 cur_valid_txrate,
430 u8 *next_idx)
f078f209
LR
431{
432 u8 i;
433
256b7759
S
434 for (i = 0; i < ath_rc_priv->max_valid_rate - 1; i++) {
435 if (ath_rc_priv->valid_rate_index[i] == cur_valid_txrate) {
436 *next_idx = ath_rc_priv->valid_rate_index[i+1];
46d14a58 437 return 1;
f078f209
LR
438 }
439 }
440
441 /* No more valid rates */
442 *next_idx = 0;
2c5a744d 443
46d14a58 444 return 0;
f078f209
LR
445}
446
447/* Return true only for single stream */
448
449static int ath_rc_valid_phyrate(u32 phy, u32 capflag, int ignore_cw)
450{
b657eade 451 if (WLAN_RC_PHY_HT(phy) && !(capflag & WLAN_RC_HT_FLAG))
46d14a58 452 return 0;
f078f209 453 if (WLAN_RC_PHY_DS(phy) && !(capflag & WLAN_RC_DS_FLAG))
46d14a58 454 return 0;
1d9d06a2
SB
455 if (WLAN_RC_PHY_TS(phy) && !(capflag & WLAN_RC_TS_FLAG))
456 return 0;
f078f209 457 if (WLAN_RC_PHY_SGI(phy) && !(capflag & WLAN_RC_SGI_FLAG))
46d14a58 458 return 0;
f078f209
LR
459 if (!ignore_cw && WLAN_RC_PHY_HT(phy))
460 if (WLAN_RC_PHY_40(phy) && !(capflag & WLAN_RC_40_FLAG))
46d14a58 461 return 0;
46d14a58 462 return 1;
f078f209
LR
463}
464
465static inline int
fc8d0238 466ath_rc_get_lower_rix(struct ath_rate_priv *ath_rc_priv,
39448b0a 467 u8 cur_valid_txrate, u8 *next_idx)
f078f209
LR
468{
469 int8_t i;
470
256b7759
S
471 for (i = 1; i < ath_rc_priv->max_valid_rate ; i++) {
472 if (ath_rc_priv->valid_rate_index[i] == cur_valid_txrate) {
473 *next_idx = ath_rc_priv->valid_rate_index[i-1];
46d14a58 474 return 1;
f078f209
LR
475 }
476 }
2c5a744d 477
46d14a58 478 return 0;
f078f209
LR
479}
480
ea2771f6 481static u8 ath_rc_init_validrates(struct ath_rate_priv *ath_rc_priv)
f078f209 482{
ea2771f6 483 const struct ath_rate_table *rate_table = ath_rc_priv->rate_table;
f078f209 484 u8 i, hi = 0;
f078f209 485
f078f209 486 for (i = 0; i < rate_table->rate_cnt; i++) {
f63b340d 487 if (rate_table->info[i].rate_flags & RC_LEGACY) {
f078f209
LR
488 u32 phy = rate_table->info[i].phy;
489 u8 valid_rate_count = 0;
490
ea2771f6 491 if (!ath_rc_valid_phyrate(phy, ath_rc_priv->ht_cap, 0))
f078f209
LR
492 continue;
493
256b7759 494 valid_rate_count = ath_rc_priv->valid_phy_ratecnt[phy];
f078f209 495
256b7759
S
496 ath_rc_priv->valid_phy_rateidx[phy][valid_rate_count] = i;
497 ath_rc_priv->valid_phy_ratecnt[phy] += 1;
f5c9a804 498 ath_rc_priv->valid_rate_index[i] = true;
f63b340d 499 hi = i;
f078f209
LR
500 }
501 }
2c5a744d 502
f078f209
LR
503 return hi;
504}
505
3d2776f6
SM
506static inline bool ath_rc_check_legacy(u8 rate, u8 dot11rate, u16 rate_flags,
507 u32 phy, u32 capflag)
f078f209 508{
3d2776f6
SM
509 if (rate != dot11rate || WLAN_RC_PHY_HT(phy))
510 return false;
f078f209 511
3d2776f6
SM
512 if ((rate_flags & WLAN_RC_CAP_MODE(capflag)) != WLAN_RC_CAP_MODE(capflag))
513 return false;
c05ea151 514
3d2776f6
SM
515 if (!(rate_flags & WLAN_RC_CAP_STREAM(capflag)))
516 return false;
c05ea151 517
3d2776f6
SM
518 return true;
519}
c05ea151 520
3d2776f6
SM
521static inline bool ath_rc_check_ht(u8 rate, u8 dot11rate, u16 rate_flags,
522 u32 phy, u32 capflag)
523{
524 if (rate != dot11rate || !WLAN_RC_PHY_HT(phy))
525 return false;
2c5a744d 526
3d2776f6
SM
527 if (!WLAN_RC_PHY_HT_VALID(rate_flags, capflag))
528 return false;
529
530 if (!(rate_flags & WLAN_RC_CAP_STREAM(capflag)))
531 return false;
532
533 return true;
f078f209
LR
534}
535
3d2776f6 536static u8 ath_rc_setvalid_rates(struct ath_rate_priv *ath_rc_priv, bool legacy)
f078f209 537{
ea2771f6 538 const struct ath_rate_table *rate_table = ath_rc_priv->rate_table;
3d2776f6 539 struct ath_rateset *rateset;
dacde357
SM
540 u32 phy, capflag = ath_rc_priv->ht_cap;
541 u16 rate_flags;
542 u8 i, j, hi = 0, rate, dot11rate, valid_rate_count;
f078f209 543
3d2776f6
SM
544 if (legacy)
545 rateset = &ath_rc_priv->neg_rates;
546 else
547 rateset = &ath_rc_priv->neg_ht_rates;
548
2c5a744d 549 for (i = 0; i < rateset->rs_nrates; i++) {
f078f209 550 for (j = 0; j < rate_table->rate_cnt; j++) {
dacde357
SM
551 phy = rate_table->info[j].phy;
552 rate_flags = rate_table->info[j].rate_flags;
553 rate = rateset->rs_rates[i];
554 dot11rate = rate_table->info[j].dot11rate;
f078f209 555
3d2776f6
SM
556 if (legacy &&
557 !ath_rc_check_legacy(rate, dot11rate,
558 rate_flags, phy, capflag))
559 continue;
560
561 if (!legacy &&
562 !ath_rc_check_ht(rate, dot11rate,
563 rate_flags, phy, capflag))
f078f209
LR
564 continue;
565
46d14a58 566 if (!ath_rc_valid_phyrate(phy, capflag, 0))
f078f209
LR
567 continue;
568
dacde357
SM
569 valid_rate_count = ath_rc_priv->valid_phy_ratecnt[phy];
570 ath_rc_priv->valid_phy_rateidx[phy][valid_rate_count] = j;
256b7759 571 ath_rc_priv->valid_phy_ratecnt[phy] += 1;
f5c9a804 572 ath_rc_priv->valid_rate_index[j] = true;
f6351d48 573 hi = max(hi, j);
f078f209
LR
574 }
575 }
f078f209 576
2c5a744d 577 return hi;
f078f209
LR
578}
579
fc8d0238 580static u8 ath_rc_get_highest_rix(struct ath_rate_priv *ath_rc_priv,
6e1e3743 581 int *is_probing)
f078f209 582{
fc8d0238 583 const struct ath_rate_table *rate_table = ath_rc_priv->rate_table;
39a4cafe 584 u32 best_thruput, this_thruput, now_msec;
f078f209 585 u8 rate, next_rate, best_rate, maxindex, minindex;
39a4cafe 586 int8_t index = 0;
f078f209
LR
587
588 now_msec = jiffies_to_msecs(jiffies);
39a4cafe 589 *is_probing = 0;
f078f209 590 best_thruput = 0;
256b7759 591 maxindex = ath_rc_priv->max_valid_rate-1;
f078f209
LR
592 minindex = 0;
593 best_rate = minindex;
594
595 /*
596 * Try the higher rate first. It will reduce memory moving time
597 * if we have very good channel characteristics.
598 */
599 for (index = maxindex; index >= minindex ; index--) {
600 u8 per_thres;
601
256b7759
S
602 rate = ath_rc_priv->valid_rate_index[index];
603 if (rate > ath_rc_priv->rate_max_phy)
f078f209
LR
604 continue;
605
606 /*
607 * For TCP the average collision rate is around 11%,
608 * so we ignore PERs less than this. This is to
609 * prevent the rate we are currently using (whose
610 * PER might be in the 10-15 range because of TCP
611 * collisions) looking worse than the next lower
612 * rate whose PER has decayed close to 0. If we
613 * used to next lower rate, its PER would grow to
614 * 10-15 and we would be worse off then staying
615 * at the current rate.
616 */
922bac60 617 per_thres = ath_rc_priv->per[rate];
f078f209
LR
618 if (per_thres < 12)
619 per_thres = 12;
620
621 this_thruput = rate_table->info[rate].user_ratekbps *
622 (100 - per_thres);
623
624 if (best_thruput <= this_thruput) {
625 best_thruput = this_thruput;
626 best_rate = rate;
627 }
628 }
629
630 rate = best_rate;
f078f209
LR
631
632 /*
633 * Must check the actual rate (ratekbps) to account for
634 * non-monoticity of 11g's rate table
635 */
636
3900898c 637 if (rate >= ath_rc_priv->rate_max_phy) {
256b7759 638 rate = ath_rc_priv->rate_max_phy;
f078f209
LR
639
640 /* Probe the next allowed phy state */
f078f209 641 if (ath_rc_get_nextvalid_txrate(rate_table,
3900898c 642 ath_rc_priv, rate, &next_rate) &&
256b7759 643 (now_msec - ath_rc_priv->probe_time >
f078f209 644 rate_table->probe_interval) &&
256b7759 645 (ath_rc_priv->hw_maxretry_pktcnt >= 1)) {
f078f209 646 rate = next_rate;
256b7759
S
647 ath_rc_priv->probe_rate = rate;
648 ath_rc_priv->probe_time = now_msec;
649 ath_rc_priv->hw_maxretry_pktcnt = 0;
46d14a58 650 *is_probing = 1;
f078f209
LR
651 }
652 }
653
256b7759
S
654 if (rate > (ath_rc_priv->rate_table_size - 1))
655 rate = ath_rc_priv->rate_table_size - 1;
f078f209 656
1d9d06a2
SB
657 if (RC_TS_ONLY(rate_table->info[rate].rate_flags) &&
658 (ath_rc_priv->ht_cap & WLAN_RC_TS_FLAG))
659 return rate;
660
661 if (RC_DS_OR_LATER(rate_table->info[rate].rate_flags) &&
662 (ath_rc_priv->ht_cap & (WLAN_RC_DS_FLAG | WLAN_RC_TS_FLAG)))
e43419f9
LR
663 return rate;
664
f63b340d 665 if (RC_SS_OR_LEGACY(rate_table->info[rate].rate_flags))
e43419f9
LR
666 return rate;
667
668 /* This should not happen */
8149415e 669 WARN_ON_ONCE(1);
e43419f9
LR
670
671 rate = ath_rc_priv->valid_rate_index[0];
f078f209
LR
672
673 return rate;
674}
675
4f0fc7c3 676static void ath_rc_rate_set_series(const struct ath_rate_table *rate_table,
a8efee4f 677 struct ieee80211_tx_rate *rate,
c89424df 678 struct ieee80211_tx_rate_control *txrc,
2c5a744d 679 u8 tries, u8 rix, int rtsctsenable)
f078f209 680{
a8efee4f 681 rate->count = tries;
545750d3 682 rate->idx = rate_table->info[rix].ratecode;
a8efee4f 683
c89424df 684 if (txrc->rts || rtsctsenable)
a8efee4f 685 rate->flags |= IEEE80211_TX_RC_USE_RTS_CTS;
545750d3
FF
686
687 if (WLAN_RC_PHY_HT(rate_table->info[rix].phy)) {
a8efee4f 688 rate->flags |= IEEE80211_TX_RC_MCS;
41e2b05b
RM
689 if (WLAN_RC_PHY_40(rate_table->info[rix].phy) &&
690 conf_is_ht40(&txrc->hw->conf))
545750d3
FF
691 rate->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
692 if (WLAN_RC_PHY_SGI(rate_table->info[rix].phy))
693 rate->flags |= IEEE80211_TX_RC_SHORT_GI;
694 }
f078f209
LR
695}
696
c89424df 697static void ath_rc_rate_set_rtscts(struct ath_softc *sc,
4f0fc7c3 698 const struct ath_rate_table *rate_table,
c89424df
S
699 struct ieee80211_tx_info *tx_info)
700{
2e546755 701 struct ieee80211_bss_conf *bss_conf;
c89424df 702
2e546755
SM
703 if (!tx_info->control.vif)
704 return;
705 /*
706 * For legacy frames, mac80211 takes care of CTS protection.
707 */
708 if (!(tx_info->control.rates[0].flags & IEEE80211_TX_RC_MCS))
709 return;
710
711 bss_conf = &tx_info->control.vif->bss_conf;
c89424df 712
2e546755
SM
713 if (!bss_conf->basic_rates)
714 return;
c89424df
S
715
716 /*
2e546755 717 * For now, use the lowest allowed basic rate for HT frames.
c89424df 718 */
2e546755 719 tx_info->control.rts_cts_rate_idx = __ffs(bss_conf->basic_rates);
c89424df
S
720}
721
e25739a1
LR
722static void ath_get_rate(void *priv, struct ieee80211_sta *sta, void *priv_sta,
723 struct ieee80211_tx_rate_control *txrc)
f078f209 724{
e25739a1
LR
725 struct ath_softc *sc = priv;
726 struct ath_rate_priv *ath_rc_priv = priv_sta;
4f0fc7c3 727 const struct ath_rate_table *rate_table;
3900898c
S
728 struct sk_buff *skb = txrc->skb;
729 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
a8efee4f 730 struct ieee80211_tx_rate *rates = tx_info->control.rates;
c89424df
S
731 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
732 __le16 fc = hdr->frame_control;
cec429dd 733 u8 try_per_rate, i = 0, rix;
3900898c 734 int is_probe = 0;
f078f209 735
e25739a1
LR
736 if (rate_control_send_low(sta, priv_sta, txrc))
737 return;
738
dd190183
LR
739 /*
740 * For Multi Rate Retry we use a different number of
741 * retry attempt counts. This ends up looking like this:
742 *
65896510
FF
743 * MRR[0] = 4
744 * MRR[1] = 4
745 * MRR[2] = 4
746 * MRR[3] = 8
dd190183
LR
747 *
748 */
65896510 749 try_per_rate = 4;
dd190183 750
4e990018 751 rate_table = ath_rc_priv->rate_table;
6e1e3743 752 rix = ath_rc_get_highest_rix(ath_rc_priv, &is_probe);
f078f209 753
b0a33448
LR
754 if (conf_is_ht(&sc->hw->conf) &&
755 (sta->ht_cap.cap & IEEE80211_HT_CAP_LDPC_CODING))
756 tx_info->flags |= IEEE80211_TX_CTL_LDPC;
757
58b5190e
FF
758 if (conf_is_ht(&sc->hw->conf) &&
759 (sta->ht_cap.cap & IEEE80211_HT_CAP_TX_STBC))
760 tx_info->flags |= (1 << IEEE80211_TX_CTL_STBC_SHIFT);
761
3900898c 762 if (is_probe) {
fc8d0238
SM
763 /*
764 * Set one try for probe rates. For the
765 * probes don't enable RTS.
766 */
c89424df 767 ath_rc_rate_set_series(rate_table, &rates[i++], txrc,
5c0ba62f 768 1, rix, 0);
fc8d0238
SM
769 /*
770 * Get the next tried/allowed rate.
771 * No RTS for the next series after the probe rate.
f078f209 772 */
fc8d0238 773 ath_rc_get_lower_rix(ath_rc_priv, rix, &rix);
c89424df 774 ath_rc_rate_set_series(rate_table, &rates[i++], txrc,
5c0ba62f 775 try_per_rate, rix, 0);
14587ce2
VT
776
777 tx_info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE;
f078f209 778 } else {
fc8d0238
SM
779 /*
780 * Set the chosen rate. No RTS for first series entry.
781 */
c89424df 782 ath_rc_rate_set_series(rate_table, &rates[i++], txrc,
5c0ba62f 783 try_per_rate, rix, 0);
f078f209
LR
784 }
785
6e1e3743
SM
786 for ( ; i < 4; i++) {
787 /*
788 * Use twice the number of tries for the last MRR segment.
789 */
790 if (i + 1 == 4)
791 try_per_rate = 8;
792
fc8d0238 793 ath_rc_get_lower_rix(ath_rc_priv, rix, &rix);
dd190183 794
fc8d0238
SM
795 /*
796 * All other rates in the series have RTS enabled.
797 */
c89424df 798 ath_rc_rate_set_series(rate_table, &rates[i], txrc,
5c0ba62f 799 try_per_rate, rix, 1);
f078f209
LR
800 }
801
802 /*
803 * NB:Change rate series to enable aggregation when operating
804 * at lower MCS rates. When first rate in series is MCS2
805 * in HT40 @ 2.4GHz, series should look like:
806 *
807 * {MCS2, MCS1, MCS0, MCS0}.
808 *
809 * When first rate in series is MCS3 in HT20 @ 2.4GHz, series should
810 * look like:
811 *
812 * {MCS3, MCS2, MCS1, MCS1}
813 *
814 * So, set fourth rate in series to be same as third one for
815 * above conditions.
816 */
675a0b04 817 if ((sc->hw->conf.chandef.chan->band == IEEE80211_BAND_2GHZ) &&
ecf70441 818 (conf_is_ht(&sc->hw->conf))) {
40990ec0 819 u8 dot11rate = rate_table->info[rix].dot11rate;
f078f209
LR
820 u8 phy = rate_table->info[rix].phy;
821 if (i == 4 &&
822 ((dot11rate == 2 && phy == WLAN_RC_PHY_HT_40_SS) ||
dc2222a8 823 (dot11rate == 3 && phy == WLAN_RC_PHY_HT_20_SS))) {
a8efee4f
S
824 rates[3].idx = rates[2].idx;
825 rates[3].flags = rates[2].flags;
f078f209
LR
826 }
827 }
c89424df
S
828
829 /*
830 * Force hardware to use computed duration for next
831 * fragment by disabling multi-rate retry, which
832 * updates duration based on the multi-rate duration table.
833 *
834 * FIXME: Fix duration
835 */
7682a76d
LR
836 if (ieee80211_has_morefrags(fc) ||
837 (le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG)) {
c89424df
S
838 rates[1].count = rates[2].count = rates[3].count = 0;
839 rates[1].idx = rates[2].idx = rates[3].idx = 0;
840 rates[0].count = ATH_TXMAXTRY;
841 }
842
c89424df 843 ath_rc_rate_set_rtscts(sc, rate_table, tx_info);
f078f209
LR
844}
845
0ff2b5c0 846static void ath_rc_update_per(struct ath_softc *sc,
4f0fc7c3 847 const struct ath_rate_table *rate_table,
2c5a744d 848 struct ath_rate_priv *ath_rc_priv,
827e69bf 849 struct ieee80211_tx_info *tx_info,
2c5a744d
S
850 int tx_rate, int xretries, int retries,
851 u32 now_msec)
f078f209 852{
827e69bf 853 int count, n_bad_frames;
f078f209 854 u8 last_per;
07b2fa5a 855 static const u32 nretry_to_per_lookup[10] = {
f078f209
LR
856 100 * 0 / 1,
857 100 * 1 / 4,
858 100 * 1 / 2,
859 100 * 3 / 4,
860 100 * 4 / 5,
861 100 * 5 / 6,
862 100 * 6 / 7,
863 100 * 7 / 8,
864 100 * 8 / 9,
865 100 * 9 / 10
866 };
867
922bac60 868 last_per = ath_rc_priv->per[tx_rate];
827e69bf 869 n_bad_frames = tx_info->status.ampdu_len - tx_info->status.ampdu_ack_len;
f078f209
LR
870
871 if (xretries) {
f078f209 872 if (xretries == 1) {
922bac60
VT
873 ath_rc_priv->per[tx_rate] += 30;
874 if (ath_rc_priv->per[tx_rate] > 100)
875 ath_rc_priv->per[tx_rate] = 100;
f078f209
LR
876 } else {
877 /* xretries == 2 */
87c1687d 878 count = ARRAY_SIZE(nretry_to_per_lookup);
f078f209
LR
879 if (retries >= count)
880 retries = count - 1;
2c5a744d 881
f078f209 882 /* new_PER = 7/8*old_PER + 1/8*(currentPER) */
922bac60 883 ath_rc_priv->per[tx_rate] =
2c5a744d 884 (u8)(last_per - (last_per >> 3) + (100 >> 3));
f078f209
LR
885 }
886
887 /* xretries == 1 or 2 */
888
256b7759
S
889 if (ath_rc_priv->probe_rate == tx_rate)
890 ath_rc_priv->probe_rate = 0;
f078f209 891
2c5a744d 892 } else { /* xretries == 0 */
87c1687d 893 count = ARRAY_SIZE(nretry_to_per_lookup);
f078f209
LR
894 if (retries >= count)
895 retries = count - 1;
2c5a744d 896
827e69bf 897 if (n_bad_frames) {
dc2222a8 898 /* new_PER = 7/8*old_PER + 1/8*(currentPER)
f078f209
LR
899 * Assuming that n_frames is not 0. The current PER
900 * from the retries is 100 * retries / (retries+1),
901 * since the first retries attempts failed, and the
902 * next one worked. For the one that worked,
903 * n_bad_frames subframes out of n_frames wored,
904 * so the PER for that part is
905 * 100 * n_bad_frames / n_frames, and it contributes
906 * 100 * n_bad_frames / (n_frames * (retries+1)) to
907 * the above PER. The expression below is a
908 * simplified version of the sum of these two terms.
909 */
827e69bf
FF
910 if (tx_info->status.ampdu_len > 0) {
911 int n_frames, n_bad_tries;
2c5a744d
S
912 u8 cur_per, new_per;
913
827e69bf
FF
914 n_bad_tries = retries * tx_info->status.ampdu_len +
915 n_bad_frames;
916 n_frames = tx_info->status.ampdu_len * (retries + 1);
917 cur_per = (100 * n_bad_tries / n_frames) >> 3;
2c5a744d 918 new_per = (u8)(last_per - (last_per >> 3) + cur_per);
922bac60 919 ath_rc_priv->per[tx_rate] = new_per;
2c5a744d 920 }
f078f209 921 } else {
922bac60 922 ath_rc_priv->per[tx_rate] =
2c5a744d
S
923 (u8)(last_per - (last_per >> 3) +
924 (nretry_to_per_lookup[retries] >> 3));
f078f209
LR
925 }
926
f078f209
LR
927
928 /*
929 * If we got at most one retry then increase the max rate if
930 * this was a probe. Otherwise, ignore the probe.
931 */
256b7759 932 if (ath_rc_priv->probe_rate && ath_rc_priv->probe_rate == tx_rate) {
827e69bf 933 if (retries > 0 || 2 * n_bad_frames > tx_info->status.ampdu_len) {
f078f209
LR
934 /*
935 * Since we probed with just a single attempt,
936 * any retries means the probe failed. Also,
937 * if the attempt worked, but more than half
938 * the subframes were bad then also consider
939 * the probe a failure.
940 */
256b7759 941 ath_rc_priv->probe_rate = 0;
f078f209
LR
942 } else {
943 u8 probe_rate = 0;
944
2c5a744d
S
945 ath_rc_priv->rate_max_phy =
946 ath_rc_priv->probe_rate;
256b7759 947 probe_rate = ath_rc_priv->probe_rate;
f078f209 948
922bac60
VT
949 if (ath_rc_priv->per[probe_rate] > 30)
950 ath_rc_priv->per[probe_rate] = 20;
f078f209 951
256b7759 952 ath_rc_priv->probe_rate = 0;
f078f209
LR
953
954 /*
955 * Since this probe succeeded, we allow the next
956 * probe twice as soon. This allows the maxRate
957 * to move up faster if the probes are
af901ca1 958 * successful.
f078f209 959 */
2c5a744d
S
960 ath_rc_priv->probe_time =
961 now_msec - rate_table->probe_interval / 2;
f078f209
LR
962 }
963 }
964
965 if (retries > 0) {
966 /*
967 * Don't update anything. We don't know if
968 * this was because of collisions or poor signal.
f078f209 969 */
256b7759 970 ath_rc_priv->hw_maxretry_pktcnt = 0;
f078f209
LR
971 } else {
972 /*
973 * It worked with no retries. First ignore bogus (small)
974 * rssi_ack values.
975 */
256b7759
S
976 if (tx_rate == ath_rc_priv->rate_max_phy &&
977 ath_rc_priv->hw_maxretry_pktcnt < 255) {
978 ath_rc_priv->hw_maxretry_pktcnt++;
f078f209
LR
979 }
980
f078f209
LR
981 }
982 }
2c5a744d
S
983}
984
2c5a744d
S
985static void ath_rc_update_ht(struct ath_softc *sc,
986 struct ath_rate_priv *ath_rc_priv,
827e69bf 987 struct ieee80211_tx_info *tx_info,
2c5a744d
S
988 int tx_rate, int xretries, int retries)
989{
2c5a744d
S
990 u32 now_msec = jiffies_to_msecs(jiffies);
991 int rate;
992 u8 last_per;
4e990018 993 const struct ath_rate_table *rate_table = ath_rc_priv->rate_table;
2c5a744d
S
994 int size = ath_rc_priv->rate_table_size;
995
996 if ((tx_rate < 0) || (tx_rate > rate_table->rate_cnt))
997 return;
f078f209 998
922bac60 999 last_per = ath_rc_priv->per[tx_rate];
2c5a744d
S
1000
1001 /* Update PER first */
0ff2b5c0
SM
1002 ath_rc_update_per(sc, rate_table, ath_rc_priv,
1003 tx_info, tx_rate, xretries,
1004 retries, now_msec);
f078f209
LR
1005
1006 /*
1007 * If this rate looks bad (high PER) then stop using it for
1008 * a while (except if we are probing).
1009 */
922bac60 1010 if (ath_rc_priv->per[tx_rate] >= 55 && tx_rate > 0 &&
dc2222a8 1011 rate_table->info[tx_rate].ratekbps <=
256b7759 1012 rate_table->info[ath_rc_priv->rate_max_phy].ratekbps) {
fc8d0238
SM
1013 ath_rc_get_lower_rix(ath_rc_priv, (u8)tx_rate,
1014 &ath_rc_priv->rate_max_phy);
f078f209
LR
1015
1016 /* Don't probe for a little while. */
256b7759 1017 ath_rc_priv->probe_time = now_msec;
f078f209
LR
1018 }
1019
f078f209
LR
1020 /* Make sure the rates below this have lower PER */
1021 /* Monotonicity is kept only for rates below the current rate. */
922bac60 1022 if (ath_rc_priv->per[tx_rate] < last_per) {
f078f209 1023 for (rate = tx_rate - 1; rate >= 0; rate--) {
f078f209 1024
922bac60
VT
1025 if (ath_rc_priv->per[rate] >
1026 ath_rc_priv->per[rate+1]) {
1027 ath_rc_priv->per[rate] =
1028 ath_rc_priv->per[rate+1];
f078f209
LR
1029 }
1030 }
1031 }
1032
1033 /* Maintain monotonicity for rates above the current rate */
2c5a744d 1034 for (rate = tx_rate; rate < size - 1; rate++) {
922bac60
VT
1035 if (ath_rc_priv->per[rate+1] <
1036 ath_rc_priv->per[rate])
1037 ath_rc_priv->per[rate+1] =
1038 ath_rc_priv->per[rate];
f078f209
LR
1039 }
1040
f078f209
LR
1041 /* Every so often, we reduce the thresholds
1042 * and PER (different for CCK and OFDM). */
256b7759 1043 if (now_msec - ath_rc_priv->per_down_time >=
c4130465 1044 rate_table->probe_interval) {
2c5a744d 1045 for (rate = 0; rate < size; rate++) {
922bac60
VT
1046 ath_rc_priv->per[rate] =
1047 7 * ath_rc_priv->per[rate] / 8;
f078f209
LR
1048 }
1049
256b7759 1050 ath_rc_priv->per_down_time = now_msec;
f078f209 1051 }
2c5a744d 1052
772d5515 1053 ath_debug_stat_retries(ath_rc_priv, tx_rate, xretries, retries,
922bac60 1054 ath_rc_priv->per[tx_rate]);
029bc432 1055
2c5a744d
S
1056}
1057
46d14a58
S
1058static void ath_rc_tx_status(struct ath_softc *sc,
1059 struct ath_rate_priv *ath_rc_priv,
88dcc2dd 1060 struct sk_buff *skb)
f078f209 1061{
88dcc2dd 1062 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
a8efee4f 1063 struct ieee80211_tx_rate *rates = tx_info->status.rates;
88dcc2dd
SM
1064 struct ieee80211_tx_rate *rate;
1065 int final_ts_idx = 0, xretries = 0, long_retry = 0;
f078f209 1066 u8 flags;
2c5a744d 1067 u32 i = 0, rix;
f078f209 1068
88dcc2dd
SM
1069 for (i = 0; i < sc->hw->max_rates; i++) {
1070 rate = &tx_info->status.rates[i];
1071 if (rate->idx < 0 || !rate->count)
1072 break;
1073
1074 final_ts_idx = i;
1075 long_retry = rate->count - 1;
1076 }
1077
1078 if (!(tx_info->flags & IEEE80211_TX_STAT_ACK))
1079 xretries = 1;
f078f209
LR
1080
1081 /*
1082 * If the first rate is not the final index, there
1083 * are intermediate rate failures to be processed.
1084 */
1085 if (final_ts_idx != 0) {
2c5a744d
S
1086 for (i = 0; i < final_ts_idx ; i++) {
1087 if (rates[i].count != 0 && (rates[i].idx >= 0)) {
1088 flags = rates[i].flags;
1089
f078f209
LR
1090 /* If HT40 and we have switched mode from
1091 * 40 to 20 => don't update */
2c5a744d 1092
a8efee4f 1093 if ((flags & IEEE80211_TX_RC_40_MHZ_WIDTH) &&
f5c38ef0 1094 !(ath_rc_priv->ht_cap & WLAN_RC_40_FLAG))
f078f209 1095 return;
e63835b0 1096
23d99394 1097 rix = ath_rc_get_rateindex(ath_rc_priv, &rates[i]);
827e69bf 1098 ath_rc_update_ht(sc, ath_rc_priv, tx_info,
88dcc2dd
SM
1099 rix, xretries ? 1 : 2,
1100 rates[i].count);
f078f209
LR
1101 }
1102 }
f078f209
LR
1103 }
1104
88dcc2dd 1105 flags = rates[final_ts_idx].flags;
2c5a744d 1106
f078f209 1107 /* If HT40 and we have switched mode from 40 to 20 => don't update */
a8efee4f 1108 if ((flags & IEEE80211_TX_RC_40_MHZ_WIDTH) &&
f5c38ef0 1109 !(ath_rc_priv->ht_cap & WLAN_RC_40_FLAG))
f078f209
LR
1110 return;
1111
23d99394 1112 rix = ath_rc_get_rateindex(ath_rc_priv, &rates[final_ts_idx]);
827e69bf 1113 ath_rc_update_ht(sc, ath_rc_priv, tx_info, rix, xretries, long_retry);
88dcc2dd 1114 ath_debug_stat_rc(ath_rc_priv, rix);
f078f209
LR
1115}
1116
4f0fc7c3
LR
1117static const
1118struct ath_rate_table *ath_choose_rate_table(struct ath_softc *sc,
1119 enum ieee80211_band band,
d4659912 1120 bool is_ht)
e11602b7 1121{
e11602b7
S
1122 switch(band) {
1123 case IEEE80211_BAND_2GHZ:
e11602b7 1124 if (is_ht)
d4659912
FF
1125 return &ar5416_11ng_ratetable;
1126 return &ar5416_11g_ratetable;
e11602b7 1127 case IEEE80211_BAND_5GHZ:
e11602b7 1128 if (is_ht)
d4659912
FF
1129 return &ar5416_11na_ratetable;
1130 return &ar5416_11a_ratetable;
e11602b7 1131 default:
e11602b7
S
1132 return NULL;
1133 }
e11602b7
S
1134}
1135
5ddfac3b 1136static void ath_rc_init(struct ath_softc *sc,
ea2771f6 1137 struct ath_rate_priv *ath_rc_priv)
f078f209 1138{
ea2771f6 1139 const struct ath_rate_table *rate_table = ath_rc_priv->rate_table;
5ddfac3b 1140 struct ath_rateset *rateset = &ath_rc_priv->neg_rates;
c46917bb 1141 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
f078f209 1142 u8 i, j, k, hi = 0, hthi = 0;
f078f209 1143
46d14a58 1144 ath_rc_priv->rate_table_size = RATE_TABLE_SIZE;
f078f209 1145
5ddfac3b 1146 for (i = 0 ; i < ath_rc_priv->rate_table_size; i++) {
922bac60 1147 ath_rc_priv->per[i] = 0;
ea2771f6 1148 ath_rc_priv->valid_rate_index[i] = 0;
f078f209
LR
1149 }
1150
f078f209 1151 for (i = 0; i < WLAN_RC_PHY_MAX; i++) {
d53c74e5 1152 for (j = 0; j < RATE_TABLE_SIZE; j++)
256b7759
S
1153 ath_rc_priv->valid_phy_rateidx[i][j] = 0;
1154 ath_rc_priv->valid_phy_ratecnt[i] = 0;
f078f209 1155 }
f078f209 1156
f078f209 1157 if (!rateset->rs_nrates) {
ea2771f6 1158 hi = ath_rc_init_validrates(ath_rc_priv);
f078f209 1159 } else {
3d2776f6 1160 hi = ath_rc_setvalid_rates(ath_rc_priv, true);
ea2771f6
SM
1161
1162 if (ath_rc_priv->ht_cap & WLAN_RC_HT_FLAG)
3d2776f6 1163 hthi = ath_rc_setvalid_rates(ath_rc_priv, false);
ea2771f6 1164
f6351d48 1165 hi = max(hi, hthi);
f078f209
LR
1166 }
1167
256b7759
S
1168 ath_rc_priv->rate_table_size = hi + 1;
1169 ath_rc_priv->rate_max_phy = 0;
ea2771f6 1170 WARN_ON(ath_rc_priv->rate_table_size > RATE_TABLE_SIZE);
f078f209
LR
1171
1172 for (i = 0, k = 0; i < WLAN_RC_PHY_MAX; i++) {
256b7759
S
1173 for (j = 0; j < ath_rc_priv->valid_phy_ratecnt[i]; j++) {
1174 ath_rc_priv->valid_rate_index[k++] =
1175 ath_rc_priv->valid_phy_rateidx[i][j];
f078f209
LR
1176 }
1177
ea2771f6
SM
1178 if (!ath_rc_valid_phyrate(i, rate_table->initial_ratemax, 1) ||
1179 !ath_rc_priv->valid_phy_ratecnt[i])
f078f209
LR
1180 continue;
1181
256b7759 1182 ath_rc_priv->rate_max_phy = ath_rc_priv->valid_phy_rateidx[i][j-1];
f078f209 1183 }
ea2771f6
SM
1184 WARN_ON(ath_rc_priv->rate_table_size > RATE_TABLE_SIZE);
1185 WARN_ON(k > RATE_TABLE_SIZE);
f078f209 1186
256b7759 1187 ath_rc_priv->max_valid_rate = k;
ea2771f6 1188 ath_rc_sort_validrates(ath_rc_priv);
10636bc2 1189 ath_rc_priv->rate_max_phy = (k > 4) ?
ea2771f6
SM
1190 ath_rc_priv->valid_rate_index[k-4] :
1191 ath_rc_priv->valid_rate_index[k-1];
9331ec80 1192
d2182b69 1193 ath_dbg(common, CONFIG, "RC Initialized with capabilities: 0x%x\n",
226afe68 1194 ath_rc_priv->ht_cap);
f078f209
LR
1195}
1196
62a29186 1197static u8 ath_rc_build_ht_caps(struct ath_softc *sc, struct ieee80211_sta *sta)
9331ec80
S
1198{
1199 u8 caps = 0;
1200
c2da50e5 1201 if (sta->ht_cap.ht_supported) {
9331ec80 1202 caps = WLAN_RC_HT_FLAG;
1d9d06a2
SB
1203 if (sta->ht_cap.mcs.rx_mask[1] && sta->ht_cap.mcs.rx_mask[2])
1204 caps |= WLAN_RC_TS_FLAG | WLAN_RC_DS_FLAG;
1205 else if (sta->ht_cap.mcs.rx_mask[1])
3553727c 1206 caps |= WLAN_RC_DS_FLAG;
e1a0c6b3 1207 if (sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
9331ec80 1208 caps |= WLAN_RC_40_FLAG;
215999f3
SM
1209 if (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40)
1210 caps |= WLAN_RC_SGI_FLAG;
1211 } else {
1212 if (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20)
1213 caps |= WLAN_RC_SGI_FLAG;
1214 }
9331ec80
S
1215 }
1216
1217 return caps;
1218}
1219
3d4e20f2 1220static bool ath_tx_aggr_check(struct ath_softc *sc, struct ieee80211_sta *sta,
334b0602
FF
1221 u8 tidno)
1222{
3d4e20f2 1223 struct ath_node *an = (struct ath_node *)sta->drv_priv;
334b0602
FF
1224 struct ath_atx_tid *txtid;
1225
3d4e20f2 1226 if (!sta->ht_cap.ht_supported)
334b0602
FF
1227 return false;
1228
1229 txtid = ATH_AN_2_TID(an, tidno);
08c96abd 1230 return !txtid->active;
334b0602
FF
1231}
1232
1233
9331ec80
S
1234/***********************************/
1235/* mac80211 Rate Control callbacks */
1236/***********************************/
1237
4b7679a5
JB
1238static void ath_tx_status(void *priv, struct ieee80211_supported_band *sband,
1239 struct ieee80211_sta *sta, void *priv_sta,
f078f209
LR
1240 struct sk_buff *skb)
1241{
1242 struct ath_softc *sc = priv;
46d14a58 1243 struct ath_rate_priv *ath_rc_priv = priv_sta;
f078f209 1244 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
c1610117 1245 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
c1610117 1246 __le16 fc = hdr->frame_control;
f078f209 1247
d969847c
FF
1248 if (!priv_sta || !ieee80211_is_data(fc))
1249 return;
1250
1251 /* This packet was aggregated but doesn't carry status info */
1252 if ((tx_info->flags & IEEE80211_TX_CTL_AMPDU) &&
1253 !(tx_info->flags & IEEE80211_TX_STAT_AMPDU))
827e69bf 1254 return;
e6a9854b 1255
827e69bf
FF
1256 if (tx_info->flags & IEEE80211_TX_STAT_TX_FILTERED)
1257 return;
e6a9854b 1258
88dcc2dd 1259 ath_rc_tx_status(sc, ath_rc_priv, skb);
46d14a58 1260
d22b0022 1261 /* Check if aggregation has to be enabled for this tid */
d5c232ff
VT
1262 if (conf_is_ht(&sc->hw->conf) &&
1263 !(skb->protocol == cpu_to_be16(ETH_P_PAE))) {
c8b57606
LR
1264 if (ieee80211_is_data_qos(fc) &&
1265 skb_get_queue_mapping(skb) != IEEE80211_AC_VO) {
d22b0022 1266 u8 *qc, tid;
d22b0022
S
1267
1268 qc = ieee80211_get_qos_ctl(hdr);
1269 tid = qc[0] & 0xf;
d22b0022 1270
3d4e20f2 1271 if(ath_tx_aggr_check(sc, sta, tid))
bd2ce6e4 1272 ieee80211_start_tx_ba_session(sta, tid, 0);
d22b0022
S
1273 }
1274 }
f078f209
LR
1275}
1276
4b7679a5
JB
1277static void ath_rate_init(void *priv, struct ieee80211_supported_band *sband,
1278 struct ieee80211_sta *sta, void *priv_sta)
f078f209 1279{
4b7679a5 1280 struct ath_softc *sc = priv;
62a29186 1281 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
46d14a58 1282 struct ath_rate_priv *ath_rc_priv = priv_sta;
f078f209
LR
1283 int i, j = 0;
1284
7b4d2735
S
1285 for (i = 0; i < sband->n_bitrates; i++) {
1286 if (sta->supp_rates[sband->band] & BIT(i)) {
1287 ath_rc_priv->neg_rates.rs_rates[j]
1288 = (sband->bitrates[i].bitrate * 2) / 10;
1289 j++;
1290 }
1291 }
1292 ath_rc_priv->neg_rates.rs_nrates = j;
f078f209 1293
a4510bf8 1294 if (sta->ht_cap.ht_supported) {
7b4d2735 1295 for (i = 0, j = 0; i < 77; i++) {
ae5eb026 1296 if (sta->ht_cap.mcs.rx_mask[i/8] & (1<<(i%8)))
dc2222a8 1297 ath_rc_priv->neg_ht_rates.rs_rates[j++] = i;
f078f209
LR
1298 if (j == ATH_RATE_MAX)
1299 break;
1300 }
dc2222a8 1301 ath_rc_priv->neg_ht_rates.rs_nrates = j;
f078f209 1302 }
7b4d2735 1303
ea2771f6
SM
1304 ath_rc_priv->rate_table = ath_choose_rate_table(sc, sband->band,
1305 sta->ht_cap.ht_supported);
1306 if (!ath_rc_priv->rate_table) {
62a29186
SM
1307 ath_err(common, "No rate table chosen\n");
1308 return;
1309 }
9331ec80 1310
62a29186 1311 ath_rc_priv->ht_cap = ath_rc_build_ht_caps(sc, sta);
ea2771f6 1312 ath_rc_init(sc, priv_sta);
9331ec80
S
1313}
1314
1315static void ath_rate_update(void *priv, struct ieee80211_supported_band *sband,
1316 struct ieee80211_sta *sta, void *priv_sta,
64f68e5d 1317 u32 changed)
9331ec80
S
1318{
1319 struct ath_softc *sc = priv;
1320 struct ath_rate_priv *ath_rc_priv = priv_sta;
9331ec80 1321
8f727ef3 1322 if (changed & IEEE80211_RC_BW_CHANGED) {
62a29186 1323 ath_rc_priv->ht_cap = ath_rc_build_ht_caps(sc, sta);
ea2771f6 1324 ath_rc_init(sc, priv_sta);
9331ec80 1325
62a29186
SM
1326 ath_dbg(ath9k_hw_common(sc->sc_ah), CONFIG,
1327 "Operating HT Bandwidth changed to: %d\n",
675a0b04 1328 cfg80211_get_chandef_type(&sc->hw->conf.chandef));
9331ec80 1329 }
f078f209
LR
1330}
1331
1c11e10b 1332#if defined(CONFIG_MAC80211_DEBUGFS) && defined(CONFIG_ATH9K_DEBUGFS)
772d5515 1333
4d28f771
SM
1334void ath_debug_stat_rc(struct ath_rate_priv *rc, int final_rate)
1335{
1336 struct ath_rc_stats *stats;
1337
1338 stats = &rc->rcstats[final_rate];
1339 stats->success++;
1340}
1341
1342void ath_debug_stat_retries(struct ath_rate_priv *rc, int rix,
1343 int xretries, int retries, u8 per)
1344{
1345 struct ath_rc_stats *stats = &rc->rcstats[rix];
1346
1347 stats->xretries += xretries;
1348 stats->retries += retries;
1349 stats->per = per;
1350}
1351
772d5515
FF
1352static ssize_t read_file_rcstat(struct file *file, char __user *user_buf,
1353 size_t count, loff_t *ppos)
1354{
1355 struct ath_rate_priv *rc = file->private_data;
1356 char *buf;
1357 unsigned int len = 0, max;
c771b518 1358 int rix;
772d5515
FF
1359 ssize_t retval;
1360
1361 if (rc->rate_table == NULL)
1362 return 0;
1363
32359e30 1364 max = 80 + rc->rate_table_size * 1024 + 1;
772d5515
FF
1365 buf = kmalloc(max, GFP_KERNEL);
1366 if (buf == NULL)
1367 return -ENOMEM;
1368
1369 len += sprintf(buf, "%6s %6s %6s "
1370 "%10s %10s %10s %10s\n",
1371 "HT", "MCS", "Rate",
1372 "Success", "Retries", "XRetries", "PER");
1373
c771b518
RM
1374 for (rix = 0; rix < rc->max_valid_rate; rix++) {
1375 u8 i = rc->valid_rate_index[rix];
772d5515
FF
1376 u32 ratekbps = rc->rate_table->info[i].ratekbps;
1377 struct ath_rc_stats *stats = &rc->rcstats[i];
1378 char mcs[5];
1379 char htmode[5];
1380 int used_mcs = 0, used_htmode = 0;
1381
1382 if (WLAN_RC_PHY_HT(rc->rate_table->info[i].phy)) {
1383 used_mcs = snprintf(mcs, 5, "%d",
1384 rc->rate_table->info[i].ratecode);
1385
1386 if (WLAN_RC_PHY_40(rc->rate_table->info[i].phy))
1387 used_htmode = snprintf(htmode, 5, "HT40");
1388 else if (WLAN_RC_PHY_20(rc->rate_table->info[i].phy))
1389 used_htmode = snprintf(htmode, 5, "HT20");
1390 else
1391 used_htmode = snprintf(htmode, 5, "????");
1392 }
1393
1394 mcs[used_mcs] = '\0';
1395 htmode[used_htmode] = '\0';
1396
1397 len += snprintf(buf + len, max - len,
1398 "%6s %6s %3u.%d: "
1399 "%10u %10u %10u %10u\n",
1400 htmode,
1401 mcs,
1402 ratekbps / 1000,
1403 (ratekbps % 1000) / 100,
1404 stats->success,
1405 stats->retries,
1406 stats->xretries,
1407 stats->per);
1408 }
1409
1410 if (len > max)
1411 len = max;
1412
1413 retval = simple_read_from_buffer(user_buf, count, ppos, buf, len);
1414 kfree(buf);
1415 return retval;
1416}
1417
1418static const struct file_operations fops_rcstat = {
1419 .read = read_file_rcstat,
234e3405 1420 .open = simple_open,
772d5515
FF
1421 .owner = THIS_MODULE
1422};
1423
1424static void ath_rate_add_sta_debugfs(void *priv, void *priv_sta,
1425 struct dentry *dir)
1426{
1427 struct ath_rate_priv *rc = priv_sta;
1c11e10b
SM
1428 rc->debugfs_rcstats = debugfs_create_file("rc_stats", S_IRUGO,
1429 dir, rc, &fops_rcstat);
772d5515
FF
1430}
1431
1c11e10b
SM
1432static void ath_rate_remove_sta_debugfs(void *priv, void *priv_sta)
1433{
1434 struct ath_rate_priv *rc = priv_sta;
1435 debugfs_remove(rc->debugfs_rcstats);
1436}
1437
1438#endif /* CONFIG_MAC80211_DEBUGFS && CONFIG_ATH9K_DEBUGFS */
772d5515 1439
4b7679a5 1440static void *ath_rate_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
f078f209 1441{
9ac58615 1442 return hw->priv;
f078f209
LR
1443}
1444
1445static void ath_rate_free(void *priv)
1446{
1447 return;
1448}
1449
4b7679a5 1450static void *ath_rate_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp)
f078f209 1451{
14f8dc49 1452 return kzalloc(sizeof(struct ath_rate_priv), gfp);
f078f209
LR
1453}
1454
4b7679a5
JB
1455static void ath_rate_free_sta(void *priv, struct ieee80211_sta *sta,
1456 void *priv_sta)
f078f209 1457{
46d14a58 1458 struct ath_rate_priv *rate_priv = priv_sta;
fe60594a 1459 kfree(rate_priv);
f078f209
LR
1460}
1461
1462static struct rate_control_ops ath_rate_ops = {
1463 .module = NULL,
1464 .name = "ath9k_rate_control",
1465 .tx_status = ath_tx_status,
1466 .get_rate = ath_get_rate,
1467 .rate_init = ath_rate_init,
9331ec80 1468 .rate_update = ath_rate_update,
f078f209
LR
1469 .alloc = ath_rate_alloc,
1470 .free = ath_rate_free,
1471 .alloc_sta = ath_rate_alloc_sta,
4b7679a5 1472 .free_sta = ath_rate_free_sta,
1c11e10b
SM
1473
1474#if defined(CONFIG_MAC80211_DEBUGFS) && defined(CONFIG_ATH9K_DEBUGFS)
772d5515 1475 .add_sta_debugfs = ath_rate_add_sta_debugfs,
1c11e10b 1476 .remove_sta_debugfs = ath_rate_remove_sta_debugfs,
772d5515 1477#endif
f078f209
LR
1478};
1479
1480int ath_rate_control_register(void)
1481{
1482 return ieee80211_rate_control_register(&ath_rate_ops);
1483}
1484
1485void ath_rate_control_unregister(void)
1486{
1487 ieee80211_rate_control_unregister(&ath_rate_ops);
1488}