]>
Commit | Line | Data |
---|---|---|
cccf129f FF |
1 | /* |
2 | * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License version 2 as | |
6 | * published by the Free Software Foundation. | |
7 | * | |
8 | * Based on minstrel.c: | |
9 | * Copyright (C) 2005-2007 Derek Smithies <derek@indranet.co.nz> | |
10 | * Sponsored by Indranet Technologies Ltd | |
11 | * | |
12 | * Based on sample.c: | |
13 | * Copyright (c) 2005 John Bicket | |
14 | * All rights reserved. | |
15 | * | |
16 | * Redistribution and use in source and binary forms, with or without | |
17 | * modification, are permitted provided that the following conditions | |
18 | * are met: | |
19 | * 1. Redistributions of source code must retain the above copyright | |
20 | * notice, this list of conditions and the following disclaimer, | |
21 | * without modification. | |
22 | * 2. Redistributions in binary form must reproduce at minimum a disclaimer | |
23 | * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any | |
24 | * redistribution must be conditioned upon including a substantially | |
25 | * similar Disclaimer requirement for further binary redistribution. | |
26 | * 3. Neither the names of the above-listed copyright holders nor the names | |
27 | * of any contributors may be used to endorse or promote products derived | |
28 | * from this software without specific prior written permission. | |
29 | * | |
30 | * Alternatively, this software may be distributed under the terms of the | |
31 | * GNU General Public License ("GPL") version 2 as published by the Free | |
32 | * Software Foundation. | |
33 | * | |
34 | * NO WARRANTY | |
35 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
36 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
37 | * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY | |
38 | * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |
39 | * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, | |
40 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
41 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
42 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER | |
43 | * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
44 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | |
45 | * THE POSSIBILITY OF SUCH DAMAGES. | |
46 | */ | |
47 | #include <linux/netdevice.h> | |
48 | #include <linux/types.h> | |
49 | #include <linux/skbuff.h> | |
50 | #include <linux/debugfs.h> | |
51 | #include <linux/random.h> | |
52 | #include <linux/ieee80211.h> | |
5a0e3ad6 | 53 | #include <linux/slab.h> |
cccf129f FF |
54 | #include <net/mac80211.h> |
55 | #include "rate.h" | |
56 | #include "rc80211_minstrel.h" | |
57 | ||
cccf129f FF |
58 | #define SAMPLE_TBL(_mi, _idx, _col) \ |
59 | _mi->sample_table[(_idx * SAMPLE_COLUMNS) + _col] | |
60 | ||
61 | /* convert mac80211 rate index to local array index */ | |
62 | static inline int | |
63 | rix_to_ndx(struct minstrel_sta_info *mi, int rix) | |
64 | { | |
65 | int i = rix; | |
66 | for (i = rix; i >= 0; i--) | |
67 | if (mi->r[i].rix == rix) | |
68 | break; | |
cccf129f FF |
69 | return i; |
70 | } | |
71 | ||
2ff2b690 TH |
72 | /* find & sort topmost throughput rates */ |
73 | static inline void | |
74 | minstrel_sort_best_tp_rates(struct minstrel_sta_info *mi, int i, u8 *tp_list) | |
75 | { | |
76 | int j = MAX_THR_RATES; | |
77 | ||
78 | while (j > 0 && mi->r[i].cur_tp > mi->r[tp_list[j - 1]].cur_tp) | |
79 | j--; | |
80 | if (j < MAX_THR_RATES - 1) | |
81 | memmove(&tp_list[j + 1], &tp_list[j], MAX_THR_RATES - (j + 1)); | |
82 | if (j < MAX_THR_RATES) | |
83 | tp_list[j] = i; | |
84 | } | |
85 | ||
cccf129f FF |
86 | static void |
87 | minstrel_update_stats(struct minstrel_priv *mp, struct minstrel_sta_info *mi) | |
88 | { | |
2ff2b690 TH |
89 | u8 tmp_tp_rate[MAX_THR_RATES]; |
90 | u8 tmp_prob_rate = 0; | |
cccf129f | 91 | u32 usecs; |
cccf129f FF |
92 | int i; |
93 | ||
2ff2b690 TH |
94 | for (i=0; i < MAX_THR_RATES; i++) |
95 | tmp_tp_rate[i] = 0; | |
96 | ||
cccf129f FF |
97 | for (i = 0; i < mi->n_rates; i++) { |
98 | struct minstrel_rate *mr = &mi->r[i]; | |
99 | ||
100 | usecs = mr->perfect_tx_time; | |
101 | if (!usecs) | |
102 | usecs = 1000000; | |
103 | ||
1e9c27df TH |
104 | if (unlikely(mr->attempts > 0)) { |
105 | mr->sample_skipped = 0; | |
c8ca8c2f | 106 | mr->cur_prob = MINSTREL_FRAC(mr->success, mr->attempts); |
cccf129f FF |
107 | mr->succ_hist += mr->success; |
108 | mr->att_hist += mr->attempts; | |
a512d4b5 TH |
109 | mr->probability = minstrel_ewma(mr->probability, |
110 | mr->cur_prob, | |
111 | EWMA_LEVEL); | |
1e9c27df TH |
112 | } else |
113 | mr->sample_skipped++; | |
cccf129f FF |
114 | |
115 | mr->last_success = mr->success; | |
116 | mr->last_attempts = mr->attempts; | |
117 | mr->success = 0; | |
118 | mr->attempts = 0; | |
119 | ||
db8c5ee6 TH |
120 | /* Update throughput per rate, reset thr. below 10% success */ |
121 | if (mr->probability < MINSTREL_FRAC(10, 100)) | |
122 | mr->cur_tp = 0; | |
123 | else | |
124 | mr->cur_tp = mr->probability * (1000000 / usecs); | |
125 | ||
cccf129f FF |
126 | /* Sample less often below the 10% chance of success. |
127 | * Sample less often above the 95% chance of success. */ | |
c8ca8c2f TH |
128 | if (mr->probability > MINSTREL_FRAC(95, 100) || |
129 | mr->probability < MINSTREL_FRAC(10, 100)) { | |
cccf129f FF |
130 | mr->adjusted_retry_count = mr->retry_count >> 1; |
131 | if (mr->adjusted_retry_count > 2) | |
132 | mr->adjusted_retry_count = 2; | |
f4a8cd94 | 133 | mr->sample_limit = 4; |
cccf129f | 134 | } else { |
f4a8cd94 | 135 | mr->sample_limit = -1; |
cccf129f FF |
136 | mr->adjusted_retry_count = mr->retry_count; |
137 | } | |
138 | if (!mr->adjusted_retry_count) | |
139 | mr->adjusted_retry_count = 2; | |
cccf129f | 140 | |
2ff2b690 TH |
141 | minstrel_sort_best_tp_rates(mi, i, tmp_tp_rate); |
142 | ||
143 | /* To determine the most robust rate (max_prob_rate) used at | |
144 | * 3rd mmr stage we distinct between two cases: | |
145 | * (1) if any success probabilitiy >= 95%, out of those rates | |
146 | * choose the maximum throughput rate as max_prob_rate | |
147 | * (2) if all success probabilities < 95%, the rate with | |
148 | * highest success probability is choosen as max_prob_rate */ | |
149 | if (mr->probability >= MINSTREL_FRAC(95,100)) { | |
150 | if (mr->cur_tp >= mi->r[tmp_prob_rate].cur_tp) | |
151 | tmp_prob_rate = i; | |
152 | } else { | |
153 | if (mr->probability >= mi->r[tmp_prob_rate].probability) | |
154 | tmp_prob_rate = i; | |
cccf129f FF |
155 | } |
156 | } | |
157 | ||
2ff2b690 TH |
158 | /* Assign the new rate set */ |
159 | memcpy(mi->max_tp_rate, tmp_tp_rate, sizeof(mi->max_tp_rate)); | |
160 | mi->max_prob_rate = tmp_prob_rate; | |
8f157611 TH |
161 | |
162 | /* Reset update timer */ | |
163 | mi->stats_update = jiffies; | |
cccf129f FF |
164 | } |
165 | ||
166 | static void | |
167 | minstrel_tx_status(void *priv, struct ieee80211_supported_band *sband, | |
168 | struct ieee80211_sta *sta, void *priv_sta, | |
169 | struct sk_buff *skb) | |
170 | { | |
8acbcddb | 171 | struct minstrel_priv *mp = priv; |
cccf129f FF |
172 | struct minstrel_sta_info *mi = priv_sta; |
173 | struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); | |
e6a9854b JB |
174 | struct ieee80211_tx_rate *ar = info->status.rates; |
175 | int i, ndx; | |
176 | int success; | |
cccf129f | 177 | |
e6a9854b | 178 | success = !!(info->flags & IEEE80211_TX_STAT_ACK); |
cccf129f | 179 | |
e6a9854b JB |
180 | for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) { |
181 | if (ar[i].idx < 0) | |
cccf129f FF |
182 | break; |
183 | ||
e6a9854b | 184 | ndx = rix_to_ndx(mi, ar[i].idx); |
3938b45c LC |
185 | if (ndx < 0) |
186 | continue; | |
187 | ||
e6a9854b | 188 | mi->r[ndx].attempts += ar[i].count; |
cccf129f | 189 | |
bfc32e6a | 190 | if ((i != IEEE80211_TX_MAX_RATES - 1) && (ar[i + 1].idx < 0)) |
cccf129f FF |
191 | mi->r[ndx].success += success; |
192 | } | |
193 | ||
194 | if ((info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE) && (i >= 0)) | |
195 | mi->sample_count++; | |
196 | ||
197 | if (mi->sample_deferred > 0) | |
198 | mi->sample_deferred--; | |
8acbcddb JB |
199 | |
200 | if (time_after(jiffies, mi->stats_update + | |
201 | (mp->update_interval * HZ) / 1000)) | |
202 | minstrel_update_stats(mp, mi); | |
cccf129f FF |
203 | } |
204 | ||
205 | ||
206 | static inline unsigned int | |
207 | minstrel_get_retry_count(struct minstrel_rate *mr, | |
208 | struct ieee80211_tx_info *info) | |
209 | { | |
210 | unsigned int retry = mr->adjusted_retry_count; | |
211 | ||
991fec09 | 212 | if (info->control.use_rts) |
cccf129f | 213 | retry = max(2U, min(mr->retry_count_rtscts, retry)); |
991fec09 | 214 | else if (info->control.use_cts_prot) |
cccf129f FF |
215 | retry = max(2U, min(mr->retry_count_cts, retry)); |
216 | return retry; | |
217 | } | |
218 | ||
219 | ||
220 | static int | |
221 | minstrel_get_next_sample(struct minstrel_sta_info *mi) | |
222 | { | |
223 | unsigned int sample_ndx; | |
8f157611 TH |
224 | sample_ndx = SAMPLE_TBL(mi, mi->sample_row, mi->sample_column); |
225 | mi->sample_row++; | |
f744bf81 | 226 | if ((int) mi->sample_row >= mi->n_rates) { |
8f157611 | 227 | mi->sample_row = 0; |
cccf129f FF |
228 | mi->sample_column++; |
229 | if (mi->sample_column >= SAMPLE_COLUMNS) | |
230 | mi->sample_column = 0; | |
231 | } | |
232 | return sample_ndx; | |
233 | } | |
234 | ||
2df78167 | 235 | static void |
e6a9854b JB |
236 | minstrel_get_rate(void *priv, struct ieee80211_sta *sta, |
237 | void *priv_sta, struct ieee80211_tx_rate_control *txrc) | |
cccf129f | 238 | { |
e6a9854b | 239 | struct sk_buff *skb = txrc->skb; |
cccf129f FF |
240 | struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); |
241 | struct minstrel_sta_info *mi = priv_sta; | |
242 | struct minstrel_priv *mp = priv; | |
e6a9854b | 243 | struct ieee80211_tx_rate *ar = info->control.rates; |
cccf129f | 244 | unsigned int ndx, sample_ndx = 0; |
8f157611 TH |
245 | bool mrr_capable; |
246 | bool indirect_rate_sampling = false; | |
247 | bool rate_sampling = false; | |
cccf129f FF |
248 | int i, delta; |
249 | int mrr_ndx[3]; | |
8f157611 | 250 | int sampling_ratio; |
cccf129f | 251 | |
8f157611 | 252 | /* management/no-ack frames do not use rate control */ |
4c6d4f5c | 253 | if (rate_control_send_low(sta, priv_sta, txrc)) |
cccf129f | 254 | return; |
cccf129f | 255 | |
8f157611 TH |
256 | /* check multi-rate-retry capabilities & adjust lookaround_rate */ |
257 | mrr_capable = mp->has_mrr && | |
258 | !txrc->rts && | |
259 | !txrc->bss_conf->use_cts_prot; | |
260 | if (mrr_capable) | |
261 | sampling_ratio = mp->lookaround_rate_mrr; | |
262 | else | |
263 | sampling_ratio = mp->lookaround_rate; | |
cccf129f | 264 | |
8f157611 | 265 | /* init rateindex [ndx] with max throughput rate */ |
2ff2b690 | 266 | ndx = mi->max_tp_rate[0]; |
cccf129f | 267 | |
8f157611 | 268 | /* increase sum packet counter */ |
cccf129f | 269 | mi->packet_count++; |
8f157611 TH |
270 | |
271 | delta = (mi->packet_count * sampling_ratio / 100) - | |
cccf129f FF |
272 | (mi->sample_count + mi->sample_deferred / 2); |
273 | ||
274 | /* delta > 0: sampling required */ | |
8f157611 | 275 | if ((delta > 0) && (mrr_capable || !mi->prev_sample)) { |
f4a8cd94 | 276 | struct minstrel_rate *msr; |
cccf129f FF |
277 | if (mi->packet_count >= 10000) { |
278 | mi->sample_deferred = 0; | |
279 | mi->sample_count = 0; | |
280 | mi->packet_count = 0; | |
281 | } else if (delta > mi->n_rates * 2) { | |
282 | /* With multi-rate retry, not every planned sample | |
283 | * attempt actually gets used, due to the way the retry | |
284 | * chain is set up - [max_tp,sample,prob,lowest] for | |
285 | * sample_rate < max_tp. | |
286 | * | |
287 | * If there's too much sampling backlog and the link | |
288 | * starts getting worse, minstrel would start bursting | |
289 | * out lots of sampling frames, which would result | |
290 | * in a large throughput loss. */ | |
291 | mi->sample_count += (delta - mi->n_rates * 2); | |
292 | } | |
293 | ||
8f157611 | 294 | /* get next random rate sample */ |
cccf129f | 295 | sample_ndx = minstrel_get_next_sample(mi); |
f4a8cd94 | 296 | msr = &mi->r[sample_ndx]; |
8f157611 | 297 | rate_sampling = true; |
cccf129f | 298 | |
8f157611 | 299 | /* Decide if direct ( 1st mrr stage) or indirect (2nd mrr stage) |
1e9c27df TH |
300 | * rate sampling method should be used. |
301 | * Respect such rates that are not sampled for 20 interations. | |
302 | */ | |
8f157611 | 303 | if (mrr_capable && |
1e9c27df TH |
304 | msr->perfect_tx_time > mi->r[ndx].perfect_tx_time && |
305 | msr->sample_skipped < 20) | |
8f157611 TH |
306 | indirect_rate_sampling = true; |
307 | ||
308 | if (!indirect_rate_sampling) { | |
f4a8cd94 FF |
309 | if (msr->sample_limit != 0) { |
310 | ndx = sample_ndx; | |
311 | mi->sample_count++; | |
312 | if (msr->sample_limit > 0) | |
313 | msr->sample_limit--; | |
8f157611 TH |
314 | } else |
315 | rate_sampling = false; | |
cccf129f FF |
316 | } else { |
317 | /* Only use IEEE80211_TX_CTL_RATE_CTRL_PROBE to mark | |
318 | * packets that have the sampling rate deferred to the | |
319 | * second MRR stage. Increase the sample counter only | |
320 | * if the deferred sample rate was actually used. | |
321 | * Use the sample_deferred counter to make sure that | |
322 | * the sampling is not done in large bursts */ | |
323 | info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE; | |
324 | mi->sample_deferred++; | |
325 | } | |
326 | } | |
8f157611 | 327 | mi->prev_sample = rate_sampling; |
f4a8cd94 FF |
328 | |
329 | /* If we're not using MRR and the sampling rate already | |
330 | * has a probability of >95%, we shouldn't be attempting | |
331 | * to use it, as this only wastes precious airtime */ | |
8f157611 TH |
332 | if (!mrr_capable && rate_sampling && |
333 | (mi->r[ndx].probability > MINSTREL_FRAC(95, 100))) | |
2ff2b690 | 334 | ndx = mi->max_tp_rate[0]; |
f4a8cd94 | 335 | |
8f157611 | 336 | /* mrr setup for 1st stage */ |
e6a9854b JB |
337 | ar[0].idx = mi->r[ndx].rix; |
338 | ar[0].count = minstrel_get_retry_count(&mi->r[ndx], info); | |
cccf129f | 339 | |
8f157611 TH |
340 | /* non mrr setup for 2nd stage */ |
341 | if (!mrr_capable) { | |
342 | if (!rate_sampling) | |
f4a8cd94 | 343 | ar[0].count = mp->max_retry; |
e6a9854b JB |
344 | ar[1].idx = mi->lowest_rix; |
345 | ar[1].count = mp->max_retry; | |
cccf129f FF |
346 | return; |
347 | } | |
348 | ||
8f157611 TH |
349 | /* mrr setup for 2nd stage */ |
350 | if (rate_sampling) { | |
351 | if (indirect_rate_sampling) | |
cccf129f FF |
352 | mrr_ndx[0] = sample_ndx; |
353 | else | |
2ff2b690 | 354 | mrr_ndx[0] = mi->max_tp_rate[0]; |
cccf129f | 355 | } else { |
2ff2b690 | 356 | mrr_ndx[0] = mi->max_tp_rate[1]; |
cccf129f | 357 | } |
8f157611 TH |
358 | |
359 | /* mrr setup for 3rd & 4th stage */ | |
cccf129f FF |
360 | mrr_ndx[1] = mi->max_prob_rate; |
361 | mrr_ndx[2] = 0; | |
e6a9854b JB |
362 | for (i = 1; i < 4; i++) { |
363 | ar[i].idx = mi->r[mrr_ndx[i - 1]].rix; | |
364 | ar[i].count = mi->r[mrr_ndx[i - 1]].adjusted_retry_count; | |
cccf129f FF |
365 | } |
366 | } | |
367 | ||
368 | ||
369 | static void | |
4ee73f33 MK |
370 | calc_rate_durations(enum ieee80211_band band, |
371 | struct minstrel_rate *d, | |
868a5f71 | 372 | struct ieee80211_rate *rate) |
cccf129f FF |
373 | { |
374 | int erp = !!(rate->flags & IEEE80211_RATE_ERP_G); | |
375 | ||
4ee73f33 | 376 | d->perfect_tx_time = ieee80211_frame_duration(band, 1200, |
cccf129f | 377 | rate->bitrate, erp, 1); |
4ee73f33 | 378 | d->ack_time = ieee80211_frame_duration(band, 10, |
cccf129f FF |
379 | rate->bitrate, erp, 1); |
380 | } | |
381 | ||
382 | static void | |
383 | init_sample_table(struct minstrel_sta_info *mi) | |
384 | { | |
385 | unsigned int i, col, new_idx; | |
cccf129f FF |
386 | u8 rnd[8]; |
387 | ||
388 | mi->sample_column = 0; | |
8f157611 | 389 | mi->sample_row = 0; |
f744bf81 | 390 | memset(mi->sample_table, 0xff, SAMPLE_COLUMNS * mi->n_rates); |
cccf129f FF |
391 | |
392 | for (col = 0; col < SAMPLE_COLUMNS; col++) { | |
f744bf81 | 393 | for (i = 0; i < mi->n_rates; i++) { |
cccf129f | 394 | get_random_bytes(rnd, sizeof(rnd)); |
f744bf81 | 395 | new_idx = (i + rnd[i & 7]) % mi->n_rates; |
cccf129f | 396 | |
f744bf81 TH |
397 | while (SAMPLE_TBL(mi, new_idx, col) != 0xff) |
398 | new_idx = (new_idx + 1) % mi->n_rates; | |
cccf129f | 399 | |
f744bf81 | 400 | SAMPLE_TBL(mi, new_idx, col) = i; |
cccf129f FF |
401 | } |
402 | } | |
403 | } | |
404 | ||
405 | static void | |
406 | minstrel_rate_init(void *priv, struct ieee80211_supported_band *sband, | |
407 | struct ieee80211_sta *sta, void *priv_sta) | |
408 | { | |
409 | struct minstrel_sta_info *mi = priv_sta; | |
410 | struct minstrel_priv *mp = priv; | |
d57854bb | 411 | struct ieee80211_rate *ctl_rate; |
cccf129f FF |
412 | unsigned int i, n = 0; |
413 | unsigned int t_slot = 9; /* FIXME: get real slot time */ | |
414 | ||
415 | mi->lowest_rix = rate_lowest_index(sband, sta); | |
d57854bb | 416 | ctl_rate = &sband->bitrates[mi->lowest_rix]; |
4ee73f33 MK |
417 | mi->sp_ack_dur = ieee80211_frame_duration(sband->band, 10, |
418 | ctl_rate->bitrate, | |
d57854bb | 419 | !!(ctl_rate->flags & IEEE80211_RATE_ERP_G), 1); |
cccf129f FF |
420 | |
421 | for (i = 0; i < sband->n_bitrates; i++) { | |
422 | struct minstrel_rate *mr = &mi->r[n]; | |
423 | unsigned int tx_time = 0, tx_time_cts = 0, tx_time_rtscts = 0; | |
424 | unsigned int tx_time_single; | |
425 | unsigned int cw = mp->cw_min; | |
426 | ||
427 | if (!rate_supported(sta, sband->band, i)) | |
428 | continue; | |
429 | n++; | |
430 | memset(mr, 0, sizeof(*mr)); | |
431 | ||
432 | mr->rix = i; | |
433 | mr->bitrate = sband->bitrates[i].bitrate / 5; | |
4ee73f33 | 434 | calc_rate_durations(sband->band, mr, &sband->bitrates[i]); |
cccf129f FF |
435 | |
436 | /* calculate maximum number of retransmissions before | |
437 | * fallback (based on maximum segment size) */ | |
f4a8cd94 | 438 | mr->sample_limit = -1; |
cccf129f FF |
439 | mr->retry_count = 1; |
440 | mr->retry_count_cts = 1; | |
441 | mr->retry_count_rtscts = 1; | |
442 | tx_time = mr->perfect_tx_time + mi->sp_ack_dur; | |
443 | do { | |
444 | /* add one retransmission */ | |
445 | tx_time_single = mr->ack_time + mr->perfect_tx_time; | |
446 | ||
447 | /* contention window */ | |
8fddddff DH |
448 | tx_time_single += (t_slot * cw) >> 1; |
449 | cw = min((cw << 1) | 1, mp->cw_max); | |
cccf129f FF |
450 | |
451 | tx_time += tx_time_single; | |
452 | tx_time_cts += tx_time_single + mi->sp_ack_dur; | |
453 | tx_time_rtscts += tx_time_single + 2 * mi->sp_ack_dur; | |
454 | if ((tx_time_cts < mp->segment_size) && | |
455 | (mr->retry_count_cts < mp->max_retry)) | |
456 | mr->retry_count_cts++; | |
457 | if ((tx_time_rtscts < mp->segment_size) && | |
458 | (mr->retry_count_rtscts < mp->max_retry)) | |
459 | mr->retry_count_rtscts++; | |
460 | } while ((tx_time < mp->segment_size) && | |
461 | (++mr->retry_count < mp->max_retry)); | |
462 | mr->adjusted_retry_count = mr->retry_count; | |
991fec09 FF |
463 | if (!(sband->bitrates[i].flags & IEEE80211_RATE_ERP_G)) |
464 | mr->retry_count_cts = mr->retry_count; | |
cccf129f FF |
465 | } |
466 | ||
467 | for (i = n; i < sband->n_bitrates; i++) { | |
468 | struct minstrel_rate *mr = &mi->r[i]; | |
469 | mr->rix = -1; | |
470 | } | |
471 | ||
472 | mi->n_rates = n; | |
473 | mi->stats_update = jiffies; | |
474 | ||
475 | init_sample_table(mi); | |
476 | } | |
477 | ||
478 | static void * | |
479 | minstrel_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp) | |
480 | { | |
481 | struct ieee80211_supported_band *sband; | |
482 | struct minstrel_sta_info *mi; | |
483 | struct minstrel_priv *mp = priv; | |
484 | struct ieee80211_hw *hw = mp->hw; | |
485 | int max_rates = 0; | |
486 | int i; | |
487 | ||
488 | mi = kzalloc(sizeof(struct minstrel_sta_info), gfp); | |
489 | if (!mi) | |
490 | return NULL; | |
491 | ||
492 | for (i = 0; i < IEEE80211_NUM_BANDS; i++) { | |
8e532175 | 493 | sband = hw->wiphy->bands[i]; |
621ad7c9 | 494 | if (sband && sband->n_bitrates > max_rates) |
cccf129f FF |
495 | max_rates = sband->n_bitrates; |
496 | } | |
497 | ||
498 | mi->r = kzalloc(sizeof(struct minstrel_rate) * max_rates, gfp); | |
499 | if (!mi->r) | |
500 | goto error; | |
501 | ||
502 | mi->sample_table = kmalloc(SAMPLE_COLUMNS * max_rates, gfp); | |
503 | if (!mi->sample_table) | |
504 | goto error1; | |
505 | ||
506 | mi->stats_update = jiffies; | |
507 | return mi; | |
508 | ||
509 | error1: | |
510 | kfree(mi->r); | |
511 | error: | |
512 | kfree(mi); | |
513 | return NULL; | |
514 | } | |
515 | ||
516 | static void | |
517 | minstrel_free_sta(void *priv, struct ieee80211_sta *sta, void *priv_sta) | |
518 | { | |
519 | struct minstrel_sta_info *mi = priv_sta; | |
520 | ||
521 | kfree(mi->sample_table); | |
522 | kfree(mi->r); | |
523 | kfree(mi); | |
524 | } | |
525 | ||
a0497f9f FF |
526 | static void |
527 | minstrel_init_cck_rates(struct minstrel_priv *mp) | |
528 | { | |
529 | static const int bitrates[4] = { 10, 20, 55, 110 }; | |
530 | struct ieee80211_supported_band *sband; | |
531 | int i, j; | |
532 | ||
533 | sband = mp->hw->wiphy->bands[IEEE80211_BAND_2GHZ]; | |
534 | if (!sband) | |
535 | return; | |
536 | ||
537 | for (i = 0, j = 0; i < sband->n_bitrates; i++) { | |
538 | struct ieee80211_rate *rate = &sband->bitrates[i]; | |
539 | ||
540 | if (rate->flags & IEEE80211_RATE_ERP_G) | |
541 | continue; | |
542 | ||
543 | for (j = 0; j < ARRAY_SIZE(bitrates); j++) { | |
544 | if (rate->bitrate != bitrates[j]) | |
545 | continue; | |
546 | ||
547 | mp->cck_rates[j] = i; | |
548 | break; | |
549 | } | |
550 | } | |
551 | } | |
552 | ||
cccf129f FF |
553 | static void * |
554 | minstrel_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir) | |
555 | { | |
556 | struct minstrel_priv *mp; | |
557 | ||
558 | mp = kzalloc(sizeof(struct minstrel_priv), GFP_ATOMIC); | |
559 | if (!mp) | |
560 | return NULL; | |
561 | ||
562 | /* contention window settings | |
563 | * Just an approximation. Using the per-queue values would complicate | |
564 | * the calculations and is probably unnecessary */ | |
565 | mp->cw_min = 15; | |
566 | mp->cw_max = 1023; | |
567 | ||
568 | /* number of packets (in %) to use for sampling other rates | |
569 | * sample less often for non-mrr packets, because the overhead | |
570 | * is much higher than with mrr */ | |
571 | mp->lookaround_rate = 5; | |
572 | mp->lookaround_rate_mrr = 10; | |
573 | ||
cccf129f FF |
574 | /* maximum time that the hw is allowed to stay in one MRR segment */ |
575 | mp->segment_size = 6000; | |
576 | ||
e6a9854b JB |
577 | if (hw->max_rate_tries > 0) |
578 | mp->max_retry = hw->max_rate_tries; | |
cccf129f FF |
579 | else |
580 | /* safe default, does not necessarily have to match hw properties */ | |
581 | mp->max_retry = 7; | |
582 | ||
e6a9854b | 583 | if (hw->max_rates >= 4) |
cccf129f FF |
584 | mp->has_mrr = true; |
585 | ||
586 | mp->hw = hw; | |
587 | mp->update_interval = 100; | |
588 | ||
24f7580e ZK |
589 | #ifdef CONFIG_MAC80211_DEBUGFS |
590 | mp->fixed_rate_idx = (u32) -1; | |
591 | mp->dbg_fixed_rate = debugfs_create_u32("fixed_rate_idx", | |
592 | S_IRUGO | S_IWUGO, debugfsdir, &mp->fixed_rate_idx); | |
593 | #endif | |
594 | ||
a0497f9f FF |
595 | minstrel_init_cck_rates(mp); |
596 | ||
cccf129f FF |
597 | return mp; |
598 | } | |
599 | ||
600 | static void | |
601 | minstrel_free(void *priv) | |
602 | { | |
24f7580e ZK |
603 | #ifdef CONFIG_MAC80211_DEBUGFS |
604 | debugfs_remove(((struct minstrel_priv *)priv)->dbg_fixed_rate); | |
605 | #endif | |
cccf129f FF |
606 | kfree(priv); |
607 | } | |
608 | ||
eae44756 | 609 | struct rate_control_ops mac80211_minstrel = { |
cccf129f FF |
610 | .name = "minstrel", |
611 | .tx_status = minstrel_tx_status, | |
612 | .get_rate = minstrel_get_rate, | |
613 | .rate_init = minstrel_rate_init, | |
cccf129f FF |
614 | .alloc = minstrel_alloc, |
615 | .free = minstrel_free, | |
616 | .alloc_sta = minstrel_alloc_sta, | |
617 | .free_sta = minstrel_free_sta, | |
618 | #ifdef CONFIG_MAC80211_DEBUGFS | |
619 | .add_sta_debugfs = minstrel_add_sta_debugfs, | |
620 | .remove_sta_debugfs = minstrel_remove_sta_debugfs, | |
621 | #endif | |
622 | }; | |
623 | ||
624 | int __init | |
625 | rc80211_minstrel_init(void) | |
626 | { | |
627 | return ieee80211_rate_control_register(&mac80211_minstrel); | |
628 | } | |
629 | ||
630 | void | |
631 | rc80211_minstrel_exit(void) | |
632 | { | |
633 | ieee80211_rate_control_unregister(&mac80211_minstrel); | |
634 | } | |
635 |