Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0-only
  2/******************************************************************************
  3 *
  4 * Copyright(c) 2005 - 2011 Intel Corporation. All rights reserved.
  5 *
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  6 * Contact Information:
  7 *  Intel Linux Wireless <ilw@linux.intel.com>
  8 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  9 *
 10 *****************************************************************************/
 11
 12#include <linux/kernel.h>
 13#include <linux/skbuff.h>
 14#include <linux/slab.h>
 15#include <net/mac80211.h>
 16
 17#include <linux/netdevice.h>
 18#include <linux/etherdevice.h>
 19#include <linux/delay.h>
 20
 21#include <linux/workqueue.h>
 22
 23#include "commands.h"
 24#include "3945.h"
 25
 26#define RS_NAME "iwl-3945-rs"
 27
 28static s32 il3945_expected_tpt_g[RATE_COUNT_3945] = {
 29	7, 13, 35, 58, 0, 0, 76, 104, 130, 168, 191, 202
 30};
 31
 32static s32 il3945_expected_tpt_g_prot[RATE_COUNT_3945] = {
 33	7, 13, 35, 58, 0, 0, 0, 80, 93, 113, 123, 125
 34};
 35
 36static s32 il3945_expected_tpt_a[RATE_COUNT_3945] = {
 37	0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186
 38};
 39
 40static s32 il3945_expected_tpt_b[RATE_COUNT_3945] = {
 41	7, 13, 35, 58, 0, 0, 0, 0, 0, 0, 0, 0
 42};
 43
 44struct il3945_tpt_entry {
 45	s8 min_rssi;
 46	u8 idx;
 47};
 48
 49static struct il3945_tpt_entry il3945_tpt_table_a[] = {
 50	{-60, RATE_54M_IDX},
 51	{-64, RATE_48M_IDX},
 52	{-72, RATE_36M_IDX},
 53	{-80, RATE_24M_IDX},
 54	{-84, RATE_18M_IDX},
 55	{-85, RATE_12M_IDX},
 56	{-87, RATE_9M_IDX},
 57	{-89, RATE_6M_IDX}
 58};
 59
 60static struct il3945_tpt_entry il3945_tpt_table_g[] = {
 61	{-60, RATE_54M_IDX},
 62	{-64, RATE_48M_IDX},
 63	{-68, RATE_36M_IDX},
 64	{-80, RATE_24M_IDX},
 65	{-84, RATE_18M_IDX},
 66	{-85, RATE_12M_IDX},
 67	{-86, RATE_11M_IDX},
 68	{-88, RATE_5M_IDX},
 69	{-90, RATE_2M_IDX},
 70	{-92, RATE_1M_IDX}
 71};
 72
 73#define RATE_MAX_WINDOW		62
 74#define RATE_FLUSH		(3*HZ)
 75#define RATE_WIN_FLUSH		(HZ/2)
 76#define IL39_RATE_HIGH_TH	11520
 77#define IL_SUCCESS_UP_TH	8960
 78#define IL_SUCCESS_DOWN_TH	10880
 79#define RATE_MIN_FAILURE_TH	6
 80#define RATE_MIN_SUCCESS_TH	8
 81#define RATE_DECREASE_TH	1920
 82#define RATE_RETRY_TH		15
 83
 84static u8
 85il3945_get_rate_idx_by_rssi(s32 rssi, enum nl80211_band band)
 86{
 87	u32 idx = 0;
 88	u32 table_size = 0;
 89	struct il3945_tpt_entry *tpt_table = NULL;
 90
 91	if (rssi < IL_MIN_RSSI_VAL || rssi > IL_MAX_RSSI_VAL)
 92		rssi = IL_MIN_RSSI_VAL;
 93
 94	switch (band) {
 95	case NL80211_BAND_2GHZ:
 96		tpt_table = il3945_tpt_table_g;
 97		table_size = ARRAY_SIZE(il3945_tpt_table_g);
 98		break;
 99	case NL80211_BAND_5GHZ:
100		tpt_table = il3945_tpt_table_a;
101		table_size = ARRAY_SIZE(il3945_tpt_table_a);
102		break;
103	default:
104		BUG();
105		break;
106	}
107
108	while (idx < table_size && rssi < tpt_table[idx].min_rssi)
109		idx++;
110
111	idx = min(idx, table_size - 1);
112
113	return tpt_table[idx].idx;
114}
115
116static void
117il3945_clear_win(struct il3945_rate_scale_data *win)
118{
119	win->data = 0;
120	win->success_counter = 0;
121	win->success_ratio = -1;
122	win->counter = 0;
123	win->average_tpt = IL_INVALID_VALUE;
124	win->stamp = 0;
125}
126
127/*
128 * il3945_rate_scale_flush_wins - flush out the rate scale wins
129 *
130 * Returns the number of wins that have gathered data but were
131 * not flushed.  If there were any that were not flushed, then
132 * reschedule the rate flushing routine.
133 */
134static int
135il3945_rate_scale_flush_wins(struct il3945_rs_sta *rs_sta)
136{
137	int unflushed = 0;
138	int i;
139	unsigned long flags;
140	struct il_priv *il __maybe_unused = rs_sta->il;
141
142	/*
143	 * For each rate, if we have collected data on that rate
144	 * and it has been more than RATE_WIN_FLUSH
145	 * since we flushed, clear out the gathered stats
146	 */
147	for (i = 0; i < RATE_COUNT_3945; i++) {
148		if (!rs_sta->win[i].counter)
149			continue;
150
151		spin_lock_irqsave(&rs_sta->lock, flags);
152		if (time_after(jiffies, rs_sta->win[i].stamp + RATE_WIN_FLUSH)) {
153			D_RATE("flushing %d samples of rate " "idx %d\n",
154			       rs_sta->win[i].counter, i);
155			il3945_clear_win(&rs_sta->win[i]);
156		} else
157			unflushed++;
158		spin_unlock_irqrestore(&rs_sta->lock, flags);
159	}
160
161	return unflushed;
162}
163
164#define RATE_FLUSH_MAX              5000	/* msec */
165#define RATE_FLUSH_MIN              50	/* msec */
166#define IL_AVERAGE_PACKETS             1500
167
168static void
169il3945_bg_rate_scale_flush(struct timer_list *t)
170{
171	struct il3945_rs_sta *rs_sta = from_timer(rs_sta, t, rate_scale_flush);
172	struct il_priv *il __maybe_unused = rs_sta->il;
173	int unflushed = 0;
174	unsigned long flags;
175	u32 packet_count, duration, pps;
176
177	D_RATE("enter\n");
178
179	unflushed = il3945_rate_scale_flush_wins(rs_sta);
180
181	spin_lock_irqsave(&rs_sta->lock, flags);
182
183	/* Number of packets Rx'd since last time this timer ran */
184	packet_count = (rs_sta->tx_packets - rs_sta->last_tx_packets) + 1;
185
186	rs_sta->last_tx_packets = rs_sta->tx_packets + 1;
187
188	if (unflushed) {
189		duration =
190		    jiffies_to_msecs(jiffies - rs_sta->last_partial_flush);
191
192		D_RATE("Tx'd %d packets in %dms\n", packet_count, duration);
193
194		/* Determine packets per second */
195		if (duration)
196			pps = (packet_count * 1000) / duration;
197		else
198			pps = 0;
199
200		if (pps) {
201			duration = (IL_AVERAGE_PACKETS * 1000) / pps;
202			if (duration < RATE_FLUSH_MIN)
203				duration = RATE_FLUSH_MIN;
204			else if (duration > RATE_FLUSH_MAX)
205				duration = RATE_FLUSH_MAX;
206		} else
207			duration = RATE_FLUSH_MAX;
208
209		rs_sta->flush_time = msecs_to_jiffies(duration);
210
211		D_RATE("new flush period: %d msec ave %d\n", duration,
212		       packet_count);
213
214		mod_timer(&rs_sta->rate_scale_flush,
215			  jiffies + rs_sta->flush_time);
216
217		rs_sta->last_partial_flush = jiffies;
218	} else {
219		rs_sta->flush_time = RATE_FLUSH;
220		rs_sta->flush_pending = 0;
221	}
222	/* If there weren't any unflushed entries, we don't schedule the timer
223	 * to run again */
224
225	rs_sta->last_flush = jiffies;
226
227	spin_unlock_irqrestore(&rs_sta->lock, flags);
228
229	D_RATE("leave\n");
230}
231
232/*
233 * il3945_collect_tx_data - Update the success/failure sliding win
234 *
235 * We keep a sliding win of the last 64 packets transmitted
236 * at this rate.  win->data contains the bitmask of successful
237 * packets.
238 */
239static void
240il3945_collect_tx_data(struct il3945_rs_sta *rs_sta,
241		       struct il3945_rate_scale_data *win, int success,
242		       int retries, int idx)
243{
244	unsigned long flags;
245	s32 fail_count;
246	struct il_priv *il __maybe_unused = rs_sta->il;
247
248	if (!retries) {
249		D_RATE("leave: retries == 0 -- should be at least 1\n");
250		return;
251	}
252
253	spin_lock_irqsave(&rs_sta->lock, flags);
254
255	/*
256	 * Keep track of only the latest 62 tx frame attempts in this rate's
257	 * history win; anything older isn't really relevant any more.
258	 * If we have filled up the sliding win, drop the oldest attempt;
259	 * if the oldest attempt (highest bit in bitmap) shows "success",
260	 * subtract "1" from the success counter (this is the main reason
261	 * we keep these bitmaps!).
262	 * */
263	while (retries > 0) {
264		if (win->counter >= RATE_MAX_WINDOW) {
265
266			/* remove earliest */
267			win->counter = RATE_MAX_WINDOW - 1;
268
269			if (win->data & (1ULL << (RATE_MAX_WINDOW - 1))) {
270				win->data &= ~(1ULL << (RATE_MAX_WINDOW - 1));
271				win->success_counter--;
272			}
273		}
274
275		/* Increment frames-attempted counter */
276		win->counter++;
277
278		/* Shift bitmap by one frame (throw away oldest history),
279		 * OR in "1", and increment "success" if this
280		 * frame was successful. */
281		win->data <<= 1;
282		if (success > 0) {
283			win->success_counter++;
284			win->data |= 0x1;
285			success--;
286		}
287
288		retries--;
289	}
290
291	/* Calculate current success ratio, avoid divide-by-0! */
292	if (win->counter > 0)
293		win->success_ratio =
294		    128 * (100 * win->success_counter) / win->counter;
295	else
296		win->success_ratio = IL_INVALID_VALUE;
297
298	fail_count = win->counter - win->success_counter;
299
300	/* Calculate average throughput, if we have enough history. */
301	if (fail_count >= RATE_MIN_FAILURE_TH ||
302	    win->success_counter >= RATE_MIN_SUCCESS_TH)
303		win->average_tpt =
304		    ((win->success_ratio * rs_sta->expected_tpt[idx] +
305		      64) / 128);
306	else
307		win->average_tpt = IL_INVALID_VALUE;
308
309	/* Tag this win as having been updated */
310	win->stamp = jiffies;
311
312	spin_unlock_irqrestore(&rs_sta->lock, flags);
313}
314
315/*
316 * Called after adding a new station to initialize rate scaling
317 */
318void
319il3945_rs_rate_init(struct il_priv *il, struct ieee80211_sta *sta, u8 sta_id)
320{
321	struct ieee80211_hw *hw = il->hw;
322	struct ieee80211_conf *conf = &il->hw->conf;
323	struct il3945_sta_priv *psta;
324	struct il3945_rs_sta *rs_sta;
325	struct ieee80211_supported_band *sband;
326	int i;
327
328	D_INFO("enter\n");
329	if (sta_id == il->hw_params.bcast_id)
330		goto out;
331
332	psta = (struct il3945_sta_priv *)sta->drv_priv;
333	rs_sta = &psta->rs_sta;
334	sband = hw->wiphy->bands[conf->chandef.chan->band];
335
336	rs_sta->il = il;
337
338	rs_sta->start_rate = RATE_INVALID;
339
340	/* default to just 802.11b */
341	rs_sta->expected_tpt = il3945_expected_tpt_b;
342
343	rs_sta->last_partial_flush = jiffies;
344	rs_sta->last_flush = jiffies;
345	rs_sta->flush_time = RATE_FLUSH;
346	rs_sta->last_tx_packets = 0;
347
 
 
 
348	for (i = 0; i < RATE_COUNT_3945; i++)
349		il3945_clear_win(&rs_sta->win[i]);
350
351	/* TODO: what is a good starting rate for STA? About middle? Maybe not
352	 * the lowest or the highest rate.. Could consider using RSSI from
353	 * previous packets? Need to have IEEE 802.1X auth succeed immediately
354	 * after assoc.. */
355
356	for (i = sband->n_bitrates - 1; i >= 0; i--) {
357		if (sta->deflink.supp_rates[sband->band] & (1 << i)) {
358			rs_sta->last_txrate_idx = i;
359			break;
360		}
361	}
362
363	il->_3945.sta_supp_rates = sta->deflink.supp_rates[sband->band];
364	/* For 5 GHz band it start at IL_FIRST_OFDM_RATE */
365	if (sband->band == NL80211_BAND_5GHZ) {
366		rs_sta->last_txrate_idx += IL_FIRST_OFDM_RATE;
367		il->_3945.sta_supp_rates <<= IL_FIRST_OFDM_RATE;
368	}
369
370out:
371	il->stations[sta_id].used &= ~IL_STA_UCODE_INPROGRESS;
372
373	D_INFO("leave\n");
374}
375
376static void *
377il3945_rs_alloc(struct ieee80211_hw *hw)
378{
379	return hw->priv;
380}
381
382/* rate scale requires free function to be implemented */
383static void
384il3945_rs_free(void *il)
385{
386}
387
388static void *
389il3945_rs_alloc_sta(void *il_priv, struct ieee80211_sta *sta, gfp_t gfp)
390{
391	struct il3945_rs_sta *rs_sta;
392	struct il3945_sta_priv *psta = (void *)sta->drv_priv;
393	struct il_priv *il __maybe_unused = il_priv;
394
395	D_RATE("enter\n");
396
397	rs_sta = &psta->rs_sta;
398
399	spin_lock_init(&rs_sta->lock);
400	timer_setup(&rs_sta->rate_scale_flush, il3945_bg_rate_scale_flush, 0);
 
401	D_RATE("leave\n");
402
403	return rs_sta;
404}
405
406static void
407il3945_rs_free_sta(void *il_priv, struct ieee80211_sta *sta, void *il_sta)
408{
409	struct il3945_rs_sta *rs_sta = il_sta;
410
411	/*
412	 * Be careful not to use any members of il3945_rs_sta (like trying
413	 * to use il_priv to print out debugging) since it may not be fully
414	 * initialized at this point.
415	 */
416	del_timer_sync(&rs_sta->rate_scale_flush);
417}
418
419/*
420 * il3945_rs_tx_status - Update rate control values based on Tx results
421 *
422 * NOTE: Uses il_priv->retry_rate for the # of retries attempted by
423 * the hardware for each rate.
424 */
425static void
426il3945_rs_tx_status(void *il_rate, struct ieee80211_supported_band *sband,
427		    struct ieee80211_sta *sta, void *il_sta,
428		    struct sk_buff *skb)
429{
430	s8 retries = 0, current_count;
431	int scale_rate_idx, first_idx, last_idx;
432	unsigned long flags;
433	struct il_priv *il = (struct il_priv *)il_rate;
434	struct il3945_rs_sta *rs_sta = il_sta;
435	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
436
437	D_RATE("enter\n");
438
439	retries = info->status.rates[0].count;
440	/* Sanity Check for retries */
441	if (retries > RATE_RETRY_TH)
442		retries = RATE_RETRY_TH;
443
444	first_idx = sband->bitrates[info->status.rates[0].idx].hw_value;
445	if (first_idx < 0 || first_idx >= RATE_COUNT_3945) {
446		D_RATE("leave: Rate out of bounds: %d\n", first_idx);
447		return;
448	}
449
450	if (!il_sta) {
451		D_RATE("leave: No STA il data to update!\n");
452		return;
453	}
454
455	/* Treat uninitialized rate scaling data same as non-existing. */
456	if (!rs_sta->il) {
457		D_RATE("leave: STA il data uninitialized!\n");
458		return;
459	}
460
461	rs_sta->tx_packets++;
462
463	scale_rate_idx = first_idx;
464	last_idx = first_idx;
465
466	/*
467	 * Update the win for each rate.  We determine which rates
468	 * were Tx'd based on the total number of retries vs. the number
469	 * of retries configured for each rate -- currently set to the
470	 * il value 'retry_rate' vs. rate specific
471	 *
472	 * On exit from this while loop last_idx indicates the rate
473	 * at which the frame was finally transmitted (or failed if no
474	 * ACK)
475	 */
476	while (retries > 1) {
477		if ((retries - 1) < il->retry_rate) {
478			current_count = (retries - 1);
479			last_idx = scale_rate_idx;
480		} else {
481			current_count = il->retry_rate;
482			last_idx = il3945_rs_next_rate(il, scale_rate_idx);
483		}
484
485		/* Update this rate accounting for as many retries
486		 * as was used for it (per current_count) */
487		il3945_collect_tx_data(rs_sta, &rs_sta->win[scale_rate_idx], 0,
488				       current_count, scale_rate_idx);
489		D_RATE("Update rate %d for %d retries.\n", scale_rate_idx,
490		       current_count);
491
492		retries -= current_count;
493
494		scale_rate_idx = last_idx;
495	}
496
497	/* Update the last idx win with success/failure based on ACK */
498	D_RATE("Update rate %d with %s.\n", last_idx,
499	       (info->flags & IEEE80211_TX_STAT_ACK) ? "success" : "failure");
500	il3945_collect_tx_data(rs_sta, &rs_sta->win[last_idx],
501			       info->flags & IEEE80211_TX_STAT_ACK, 1,
502			       last_idx);
503
504	/* We updated the rate scale win -- if its been more than
505	 * flush_time since the last run, schedule the flush
506	 * again */
507	spin_lock_irqsave(&rs_sta->lock, flags);
508
509	if (!rs_sta->flush_pending &&
510	    time_after(jiffies, rs_sta->last_flush + rs_sta->flush_time)) {
511
512		rs_sta->last_partial_flush = jiffies;
513		rs_sta->flush_pending = 1;
514		mod_timer(&rs_sta->rate_scale_flush,
515			  jiffies + rs_sta->flush_time);
516	}
517
518	spin_unlock_irqrestore(&rs_sta->lock, flags);
519
520	D_RATE("leave\n");
521}
522
523static u16
524il3945_get_adjacent_rate(struct il3945_rs_sta *rs_sta, u8 idx, u16 rate_mask,
525			 enum nl80211_band band)
526{
527	u8 high = RATE_INVALID;
528	u8 low = RATE_INVALID;
529	struct il_priv *il __maybe_unused = rs_sta->il;
530
531	/* 802.11A walks to the next literal adjacent rate in
532	 * the rate table */
533	if (unlikely(band == NL80211_BAND_5GHZ)) {
534		int i;
535		u32 mask;
536
537		/* Find the previous rate that is in the rate mask */
538		i = idx - 1;
539		for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
540			if (rate_mask & mask) {
541				low = i;
542				break;
543			}
544		}
545
546		/* Find the next rate that is in the rate mask */
547		i = idx + 1;
548		for (mask = (1 << i); i < RATE_COUNT_3945; i++, mask <<= 1) {
549			if (rate_mask & mask) {
550				high = i;
551				break;
552			}
553		}
554
555		return (high << 8) | low;
556	}
557
558	low = idx;
559	while (low != RATE_INVALID) {
560		if (rs_sta->tgg)
561			low = il3945_rates[low].prev_rs_tgg;
562		else
563			low = il3945_rates[low].prev_rs;
564		if (low == RATE_INVALID)
565			break;
566		if (rate_mask & (1 << low))
567			break;
568		D_RATE("Skipping masked lower rate: %d\n", low);
569	}
570
571	high = idx;
572	while (high != RATE_INVALID) {
573		if (rs_sta->tgg)
574			high = il3945_rates[high].next_rs_tgg;
575		else
576			high = il3945_rates[high].next_rs;
577		if (high == RATE_INVALID)
578			break;
579		if (rate_mask & (1 << high))
580			break;
581		D_RATE("Skipping masked higher rate: %d\n", high);
582	}
583
584	return (high << 8) | low;
585}
586
587/*
588 * il3945_rs_get_rate - find the rate for the requested packet
589 *
590 * Returns the ieee80211_rate structure allocated by the driver.
591 *
592 * The rate control algorithm has no internal mapping between hw_mode's
593 * rate ordering and the rate ordering used by the rate control algorithm.
594 *
595 * The rate control algorithm uses a single table of rates that goes across
596 * the entire A/B/G spectrum vs. being limited to just one particular
597 * hw_mode.
598 *
599 * As such, we can't convert the idx obtained below into the hw_mode's
600 * rate table and must reference the driver allocated rate table
601 *
602 */
603static void
604il3945_rs_get_rate(void *il_r, struct ieee80211_sta *sta, void *il_sta,
605		   struct ieee80211_tx_rate_control *txrc)
606{
607	struct ieee80211_supported_band *sband = txrc->sband;
608	struct sk_buff *skb = txrc->skb;
609	u8 low = RATE_INVALID;
610	u8 high = RATE_INVALID;
611	u16 high_low;
612	int idx;
613	struct il3945_rs_sta *rs_sta = il_sta;
614	struct il3945_rate_scale_data *win = NULL;
615	int current_tpt = IL_INVALID_VALUE;
616	int low_tpt = IL_INVALID_VALUE;
617	int high_tpt = IL_INVALID_VALUE;
618	u32 fail_count;
619	s8 scale_action = 0;
620	unsigned long flags;
621	u16 rate_mask;
622	s8 max_rate_idx = -1;
623	struct il_priv *il __maybe_unused = (struct il_priv *)il_r;
624	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
625
626	D_RATE("enter\n");
627
628	/* Treat uninitialized rate scaling data same as non-existing. */
629	if (rs_sta && !rs_sta->il) {
630		D_RATE("Rate scaling information not initialized yet.\n");
631		il_sta = NULL;
632	}
633
634	rate_mask = sta->deflink.supp_rates[sband->band];
 
 
 
635
636	/* get user max rate if set */
637	max_rate_idx = fls(txrc->rate_idx_mask) - 1;
638	if (sband->band == NL80211_BAND_5GHZ && max_rate_idx != -1)
639		max_rate_idx += IL_FIRST_OFDM_RATE;
640	if (max_rate_idx < 0 || max_rate_idx >= RATE_COUNT)
641		max_rate_idx = -1;
642
643	idx = min(rs_sta->last_txrate_idx & 0xffff, RATE_COUNT_3945 - 1);
644
645	if (sband->band == NL80211_BAND_5GHZ)
646		rate_mask = rate_mask << IL_FIRST_OFDM_RATE;
647
648	spin_lock_irqsave(&rs_sta->lock, flags);
649
650	/* for recent assoc, choose best rate regarding
651	 * to rssi value
652	 */
653	if (rs_sta->start_rate != RATE_INVALID) {
654		if (rs_sta->start_rate < idx &&
655		    (rate_mask & (1 << rs_sta->start_rate)))
656			idx = rs_sta->start_rate;
657		rs_sta->start_rate = RATE_INVALID;
658	}
659
660	/* force user max rate if set by user */
661	if (max_rate_idx != -1 && max_rate_idx < idx) {
662		if (rate_mask & (1 << max_rate_idx))
663			idx = max_rate_idx;
664	}
665
666	win = &(rs_sta->win[idx]);
667
668	fail_count = win->counter - win->success_counter;
669
670	if (fail_count < RATE_MIN_FAILURE_TH &&
671	    win->success_counter < RATE_MIN_SUCCESS_TH) {
672		spin_unlock_irqrestore(&rs_sta->lock, flags);
673
674		D_RATE("Invalid average_tpt on rate %d: "
675		       "counter: %d, success_counter: %d, "
676		       "expected_tpt is %sNULL\n", idx, win->counter,
677		       win->success_counter,
678		       rs_sta->expected_tpt ? "not " : "");
679
680		/* Can't calculate this yet; not enough history */
681		win->average_tpt = IL_INVALID_VALUE;
682		goto out;
683
684	}
685
686	current_tpt = win->average_tpt;
687
688	high_low =
689	    il3945_get_adjacent_rate(rs_sta, idx, rate_mask, sband->band);
690	low = high_low & 0xff;
691	high = (high_low >> 8) & 0xff;
692
693	/* If user set max rate, dont allow higher than user constrain */
694	if (max_rate_idx != -1 && max_rate_idx < high)
695		high = RATE_INVALID;
696
697	/* Collect Measured throughputs of adjacent rates */
698	if (low != RATE_INVALID)
699		low_tpt = rs_sta->win[low].average_tpt;
700
701	if (high != RATE_INVALID)
702		high_tpt = rs_sta->win[high].average_tpt;
703
704	spin_unlock_irqrestore(&rs_sta->lock, flags);
705
706	scale_action = 0;
707
708	/* Low success ratio , need to drop the rate */
709	if (win->success_ratio < RATE_DECREASE_TH || !current_tpt) {
710		D_RATE("decrease rate because of low success_ratio\n");
711		scale_action = -1;
712		/* No throughput measured yet for adjacent rates,
713		 * try increase */
714	} else if (low_tpt == IL_INVALID_VALUE && high_tpt == IL_INVALID_VALUE) {
715
716		if (high != RATE_INVALID &&
717		    win->success_ratio >= RATE_INCREASE_TH)
718			scale_action = 1;
719		else if (low != RATE_INVALID)
720			scale_action = 0;
721
722		/* Both adjacent throughputs are measured, but neither one has
723		 * better throughput; we're using the best rate, don't change
724		 * it! */
725	} else if (low_tpt != IL_INVALID_VALUE && high_tpt != IL_INVALID_VALUE
726		   && low_tpt < current_tpt && high_tpt < current_tpt) {
727
728		D_RATE("No action -- low [%d] & high [%d] < "
729		       "current_tpt [%d]\n", low_tpt, high_tpt, current_tpt);
730		scale_action = 0;
731
732		/* At least one of the rates has better throughput */
733	} else {
734		if (high_tpt != IL_INVALID_VALUE) {
735
736			/* High rate has better throughput, Increase
737			 * rate */
738			if (high_tpt > current_tpt &&
739			    win->success_ratio >= RATE_INCREASE_TH)
740				scale_action = 1;
741			else {
742				D_RATE("decrease rate because of high tpt\n");
743				scale_action = 0;
744			}
745		} else if (low_tpt != IL_INVALID_VALUE) {
746			if (low_tpt > current_tpt) {
747				D_RATE("decrease rate because of low tpt\n");
748				scale_action = -1;
749			} else if (win->success_ratio >= RATE_INCREASE_TH) {
750				/* Lower rate has better
751				 * throughput,decrease rate */
752				scale_action = 1;
753			}
754		}
755	}
756
757	/* Sanity check; asked for decrease, but success rate or throughput
758	 * has been good at old rate.  Don't change it. */
759	if (scale_action == -1 && low != RATE_INVALID &&
760	    (win->success_ratio > RATE_HIGH_TH ||
761	     current_tpt > 100 * rs_sta->expected_tpt[low]))
762		scale_action = 0;
763
764	switch (scale_action) {
765	case -1:
766		/* Decrease rate */
767		if (low != RATE_INVALID)
768			idx = low;
769		break;
770	case 1:
771		/* Increase rate */
772		if (high != RATE_INVALID)
773			idx = high;
774
775		break;
776	case 0:
777	default:
778		/* No change */
779		break;
780	}
781
782	D_RATE("Selected %d (action %d) - low %d high %d\n", idx, scale_action,
783	       low, high);
784
785out:
786
787	if (sband->band == NL80211_BAND_5GHZ) {
788		if (WARN_ON_ONCE(idx < IL_FIRST_OFDM_RATE))
789			idx = IL_FIRST_OFDM_RATE;
790		rs_sta->last_txrate_idx = idx;
791		info->control.rates[0].idx = idx - IL_FIRST_OFDM_RATE;
792	} else {
793		rs_sta->last_txrate_idx = idx;
794		info->control.rates[0].idx = rs_sta->last_txrate_idx;
795	}
796	info->control.rates[0].count = 1;
797
798	D_RATE("leave: %d\n", idx);
799}
800
801#ifdef CONFIG_MAC80211_DEBUGFS
802
803static ssize_t
804il3945_sta_dbgfs_stats_table_read(struct file *file, char __user *user_buf,
805				  size_t count, loff_t *ppos)
806{
807	char *buff;
808	int desc = 0;
809	int j;
810	ssize_t ret;
811	struct il3945_rs_sta *lq_sta = file->private_data;
812
813	buff = kmalloc(1024, GFP_KERNEL);
814	if (!buff)
815		return -ENOMEM;
816
817	desc +=
818	    sprintf(buff + desc,
819		    "tx packets=%d last rate idx=%d\n"
820		    "rate=0x%X flush time %d\n", lq_sta->tx_packets,
821		    lq_sta->last_txrate_idx, lq_sta->start_rate,
822		    jiffies_to_msecs(lq_sta->flush_time));
823	for (j = 0; j < RATE_COUNT_3945; j++) {
824		desc +=
825		    sprintf(buff + desc, "counter=%d success=%d %%=%d\n",
826			    lq_sta->win[j].counter,
827			    lq_sta->win[j].success_counter,
828			    lq_sta->win[j].success_ratio);
829	}
830	ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
831	kfree(buff);
832	return ret;
833}
834
835static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
836	.read = il3945_sta_dbgfs_stats_table_read,
837	.open = simple_open,
838	.llseek = default_llseek,
839};
840
841static void
842il3945_add_debugfs(void *il, void *il_sta, struct dentry *dir)
843{
844	struct il3945_rs_sta *lq_sta = il_sta;
845
846	debugfs_create_file("rate_stats_table", 0600, dir, lq_sta,
847			    &rs_sta_dbgfs_stats_table_ops);
 
 
 
 
 
 
 
 
 
848}
849#endif
850
851/*
852 * Initialization of rate scaling information is done by driver after
853 * the station is added. Since mac80211 calls this function before a
854 * station is added we ignore it.
855 */
856static void
857il3945_rs_rate_init_stub(void *il_r, struct ieee80211_supported_band *sband,
858			 struct cfg80211_chan_def *chandef,
859			 struct ieee80211_sta *sta, void *il_sta)
860{
861}
862
863static const struct rate_control_ops rs_ops = {
864	.name = RS_NAME,
865	.tx_status = il3945_rs_tx_status,
866	.get_rate = il3945_rs_get_rate,
867	.rate_init = il3945_rs_rate_init_stub,
868	.alloc = il3945_rs_alloc,
869	.free = il3945_rs_free,
870	.alloc_sta = il3945_rs_alloc_sta,
871	.free_sta = il3945_rs_free_sta,
872#ifdef CONFIG_MAC80211_DEBUGFS
873	.add_sta_debugfs = il3945_add_debugfs,
 
874#endif
875
876};
877
878void
879il3945_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
880{
881	struct il_priv *il = hw->priv;
882	s32 rssi = 0;
883	unsigned long flags;
884	struct il3945_rs_sta *rs_sta;
885	struct ieee80211_sta *sta;
886	struct il3945_sta_priv *psta;
887
888	D_RATE("enter\n");
889
890	rcu_read_lock();
891
892	sta = ieee80211_find_sta(il->vif, il->stations[sta_id].sta.sta.addr);
893	if (!sta) {
894		D_RATE("Unable to find station to initialize rate scaling.\n");
895		rcu_read_unlock();
896		return;
897	}
898
899	psta = (void *)sta->drv_priv;
900	rs_sta = &psta->rs_sta;
901
902	spin_lock_irqsave(&rs_sta->lock, flags);
903
904	rs_sta->tgg = 0;
905	switch (il->band) {
906	case NL80211_BAND_2GHZ:
907		/* TODO: this always does G, not a regression */
908		if (il->active.flags & RXON_FLG_TGG_PROTECT_MSK) {
909			rs_sta->tgg = 1;
910			rs_sta->expected_tpt = il3945_expected_tpt_g_prot;
911		} else
912			rs_sta->expected_tpt = il3945_expected_tpt_g;
913		break;
914	case NL80211_BAND_5GHZ:
915		rs_sta->expected_tpt = il3945_expected_tpt_a;
916		break;
917	default:
918		BUG();
919		break;
920	}
921
922	spin_unlock_irqrestore(&rs_sta->lock, flags);
923
924	rssi = il->_3945.last_rx_rssi;
925	if (rssi == 0)
926		rssi = IL_MIN_RSSI_VAL;
927
928	D_RATE("Network RSSI: %d\n", rssi);
929
930	rs_sta->start_rate = il3945_get_rate_idx_by_rssi(rssi, il->band);
931
932	D_RATE("leave: rssi %d assign rate idx: " "%d (plcp 0x%x)\n", rssi,
933	       rs_sta->start_rate, il3945_rates[rs_sta->start_rate].plcp);
934	rcu_read_unlock();
935}
936
937int
938il3945_rate_control_register(void)
939{
940	return ieee80211_rate_control_register(&rs_ops);
941}
942
943void
944il3945_rate_control_unregister(void)
945{
946	ieee80211_rate_control_unregister(&rs_ops);
947}
v4.6
 
  1/******************************************************************************
  2 *
  3 * Copyright(c) 2005 - 2011 Intel Corporation. All rights reserved.
  4 *
  5 * This program is free software; you can redistribute it and/or modify it
  6 * under the terms of version 2 of the GNU General Public License as
  7 * published by the Free Software Foundation.
  8 *
  9 * This program is distributed in the hope that it will be useful, but WITHOUT
 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 12 * more details.
 13 *
 14 * You should have received a copy of the GNU General Public License along with
 15 * this program; if not, write to the Free Software Foundation, Inc.,
 16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
 17 *
 18 * The full GNU General Public License is included in this distribution in the
 19 * file called LICENSE.
 20 *
 21 * Contact Information:
 22 *  Intel Linux Wireless <ilw@linux.intel.com>
 23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
 24 *
 25 *****************************************************************************/
 26
 27#include <linux/kernel.h>
 28#include <linux/skbuff.h>
 29#include <linux/slab.h>
 30#include <net/mac80211.h>
 31
 32#include <linux/netdevice.h>
 33#include <linux/etherdevice.h>
 34#include <linux/delay.h>
 35
 36#include <linux/workqueue.h>
 37
 38#include "commands.h"
 39#include "3945.h"
 40
 41#define RS_NAME "iwl-3945-rs"
 42
 43static s32 il3945_expected_tpt_g[RATE_COUNT_3945] = {
 44	7, 13, 35, 58, 0, 0, 76, 104, 130, 168, 191, 202
 45};
 46
 47static s32 il3945_expected_tpt_g_prot[RATE_COUNT_3945] = {
 48	7, 13, 35, 58, 0, 0, 0, 80, 93, 113, 123, 125
 49};
 50
 51static s32 il3945_expected_tpt_a[RATE_COUNT_3945] = {
 52	0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186
 53};
 54
 55static s32 il3945_expected_tpt_b[RATE_COUNT_3945] = {
 56	7, 13, 35, 58, 0, 0, 0, 0, 0, 0, 0, 0
 57};
 58
 59struct il3945_tpt_entry {
 60	s8 min_rssi;
 61	u8 idx;
 62};
 63
 64static struct il3945_tpt_entry il3945_tpt_table_a[] = {
 65	{-60, RATE_54M_IDX},
 66	{-64, RATE_48M_IDX},
 67	{-72, RATE_36M_IDX},
 68	{-80, RATE_24M_IDX},
 69	{-84, RATE_18M_IDX},
 70	{-85, RATE_12M_IDX},
 71	{-87, RATE_9M_IDX},
 72	{-89, RATE_6M_IDX}
 73};
 74
 75static struct il3945_tpt_entry il3945_tpt_table_g[] = {
 76	{-60, RATE_54M_IDX},
 77	{-64, RATE_48M_IDX},
 78	{-68, RATE_36M_IDX},
 79	{-80, RATE_24M_IDX},
 80	{-84, RATE_18M_IDX},
 81	{-85, RATE_12M_IDX},
 82	{-86, RATE_11M_IDX},
 83	{-88, RATE_5M_IDX},
 84	{-90, RATE_2M_IDX},
 85	{-92, RATE_1M_IDX}
 86};
 87
 88#define RATE_MAX_WINDOW		62
 89#define RATE_FLUSH		(3*HZ)
 90#define RATE_WIN_FLUSH		(HZ/2)
 91#define IL39_RATE_HIGH_TH	11520
 92#define IL_SUCCESS_UP_TH	8960
 93#define IL_SUCCESS_DOWN_TH	10880
 94#define RATE_MIN_FAILURE_TH	6
 95#define RATE_MIN_SUCCESS_TH	8
 96#define RATE_DECREASE_TH	1920
 97#define RATE_RETRY_TH		15
 98
 99static u8
100il3945_get_rate_idx_by_rssi(s32 rssi, enum ieee80211_band band)
101{
102	u32 idx = 0;
103	u32 table_size = 0;
104	struct il3945_tpt_entry *tpt_table = NULL;
105
106	if (rssi < IL_MIN_RSSI_VAL || rssi > IL_MAX_RSSI_VAL)
107		rssi = IL_MIN_RSSI_VAL;
108
109	switch (band) {
110	case IEEE80211_BAND_2GHZ:
111		tpt_table = il3945_tpt_table_g;
112		table_size = ARRAY_SIZE(il3945_tpt_table_g);
113		break;
114	case IEEE80211_BAND_5GHZ:
115		tpt_table = il3945_tpt_table_a;
116		table_size = ARRAY_SIZE(il3945_tpt_table_a);
117		break;
118	default:
119		BUG();
120		break;
121	}
122
123	while (idx < table_size && rssi < tpt_table[idx].min_rssi)
124		idx++;
125
126	idx = min(idx, table_size - 1);
127
128	return tpt_table[idx].idx;
129}
130
131static void
132il3945_clear_win(struct il3945_rate_scale_data *win)
133{
134	win->data = 0;
135	win->success_counter = 0;
136	win->success_ratio = -1;
137	win->counter = 0;
138	win->average_tpt = IL_INVALID_VALUE;
139	win->stamp = 0;
140}
141
142/**
143 * il3945_rate_scale_flush_wins - flush out the rate scale wins
144 *
145 * Returns the number of wins that have gathered data but were
146 * not flushed.  If there were any that were not flushed, then
147 * reschedule the rate flushing routine.
148 */
149static int
150il3945_rate_scale_flush_wins(struct il3945_rs_sta *rs_sta)
151{
152	int unflushed = 0;
153	int i;
154	unsigned long flags;
155	struct il_priv *il __maybe_unused = rs_sta->il;
156
157	/*
158	 * For each rate, if we have collected data on that rate
159	 * and it has been more than RATE_WIN_FLUSH
160	 * since we flushed, clear out the gathered stats
161	 */
162	for (i = 0; i < RATE_COUNT_3945; i++) {
163		if (!rs_sta->win[i].counter)
164			continue;
165
166		spin_lock_irqsave(&rs_sta->lock, flags);
167		if (time_after(jiffies, rs_sta->win[i].stamp + RATE_WIN_FLUSH)) {
168			D_RATE("flushing %d samples of rate " "idx %d\n",
169			       rs_sta->win[i].counter, i);
170			il3945_clear_win(&rs_sta->win[i]);
171		} else
172			unflushed++;
173		spin_unlock_irqrestore(&rs_sta->lock, flags);
174	}
175
176	return unflushed;
177}
178
179#define RATE_FLUSH_MAX              5000	/* msec */
180#define RATE_FLUSH_MIN              50	/* msec */
181#define IL_AVERAGE_PACKETS             1500
182
183static void
184il3945_bg_rate_scale_flush(unsigned long data)
185{
186	struct il3945_rs_sta *rs_sta = (void *)data;
187	struct il_priv *il __maybe_unused = rs_sta->il;
188	int unflushed = 0;
189	unsigned long flags;
190	u32 packet_count, duration, pps;
191
192	D_RATE("enter\n");
193
194	unflushed = il3945_rate_scale_flush_wins(rs_sta);
195
196	spin_lock_irqsave(&rs_sta->lock, flags);
197
198	/* Number of packets Rx'd since last time this timer ran */
199	packet_count = (rs_sta->tx_packets - rs_sta->last_tx_packets) + 1;
200
201	rs_sta->last_tx_packets = rs_sta->tx_packets + 1;
202
203	if (unflushed) {
204		duration =
205		    jiffies_to_msecs(jiffies - rs_sta->last_partial_flush);
206
207		D_RATE("Tx'd %d packets in %dms\n", packet_count, duration);
208
209		/* Determine packets per second */
210		if (duration)
211			pps = (packet_count * 1000) / duration;
212		else
213			pps = 0;
214
215		if (pps) {
216			duration = (IL_AVERAGE_PACKETS * 1000) / pps;
217			if (duration < RATE_FLUSH_MIN)
218				duration = RATE_FLUSH_MIN;
219			else if (duration > RATE_FLUSH_MAX)
220				duration = RATE_FLUSH_MAX;
221		} else
222			duration = RATE_FLUSH_MAX;
223
224		rs_sta->flush_time = msecs_to_jiffies(duration);
225
226		D_RATE("new flush period: %d msec ave %d\n", duration,
227		       packet_count);
228
229		mod_timer(&rs_sta->rate_scale_flush,
230			  jiffies + rs_sta->flush_time);
231
232		rs_sta->last_partial_flush = jiffies;
233	} else {
234		rs_sta->flush_time = RATE_FLUSH;
235		rs_sta->flush_pending = 0;
236	}
237	/* If there weren't any unflushed entries, we don't schedule the timer
238	 * to run again */
239
240	rs_sta->last_flush = jiffies;
241
242	spin_unlock_irqrestore(&rs_sta->lock, flags);
243
244	D_RATE("leave\n");
245}
246
247/**
248 * il3945_collect_tx_data - Update the success/failure sliding win
249 *
250 * We keep a sliding win of the last 64 packets transmitted
251 * at this rate.  win->data contains the bitmask of successful
252 * packets.
253 */
254static void
255il3945_collect_tx_data(struct il3945_rs_sta *rs_sta,
256		       struct il3945_rate_scale_data *win, int success,
257		       int retries, int idx)
258{
259	unsigned long flags;
260	s32 fail_count;
261	struct il_priv *il __maybe_unused = rs_sta->il;
262
263	if (!retries) {
264		D_RATE("leave: retries == 0 -- should be at least 1\n");
265		return;
266	}
267
268	spin_lock_irqsave(&rs_sta->lock, flags);
269
270	/*
271	 * Keep track of only the latest 62 tx frame attempts in this rate's
272	 * history win; anything older isn't really relevant any more.
273	 * If we have filled up the sliding win, drop the oldest attempt;
274	 * if the oldest attempt (highest bit in bitmap) shows "success",
275	 * subtract "1" from the success counter (this is the main reason
276	 * we keep these bitmaps!).
277	 * */
278	while (retries > 0) {
279		if (win->counter >= RATE_MAX_WINDOW) {
280
281			/* remove earliest */
282			win->counter = RATE_MAX_WINDOW - 1;
283
284			if (win->data & (1ULL << (RATE_MAX_WINDOW - 1))) {
285				win->data &= ~(1ULL << (RATE_MAX_WINDOW - 1));
286				win->success_counter--;
287			}
288		}
289
290		/* Increment frames-attempted counter */
291		win->counter++;
292
293		/* Shift bitmap by one frame (throw away oldest history),
294		 * OR in "1", and increment "success" if this
295		 * frame was successful. */
296		win->data <<= 1;
297		if (success > 0) {
298			win->success_counter++;
299			win->data |= 0x1;
300			success--;
301		}
302
303		retries--;
304	}
305
306	/* Calculate current success ratio, avoid divide-by-0! */
307	if (win->counter > 0)
308		win->success_ratio =
309		    128 * (100 * win->success_counter) / win->counter;
310	else
311		win->success_ratio = IL_INVALID_VALUE;
312
313	fail_count = win->counter - win->success_counter;
314
315	/* Calculate average throughput, if we have enough history. */
316	if (fail_count >= RATE_MIN_FAILURE_TH ||
317	    win->success_counter >= RATE_MIN_SUCCESS_TH)
318		win->average_tpt =
319		    ((win->success_ratio * rs_sta->expected_tpt[idx] +
320		      64) / 128);
321	else
322		win->average_tpt = IL_INVALID_VALUE;
323
324	/* Tag this win as having been updated */
325	win->stamp = jiffies;
326
327	spin_unlock_irqrestore(&rs_sta->lock, flags);
328}
329
330/*
331 * Called after adding a new station to initialize rate scaling
332 */
333void
334il3945_rs_rate_init(struct il_priv *il, struct ieee80211_sta *sta, u8 sta_id)
335{
336	struct ieee80211_hw *hw = il->hw;
337	struct ieee80211_conf *conf = &il->hw->conf;
338	struct il3945_sta_priv *psta;
339	struct il3945_rs_sta *rs_sta;
340	struct ieee80211_supported_band *sband;
341	int i;
342
343	D_INFO("enter\n");
344	if (sta_id == il->hw_params.bcast_id)
345		goto out;
346
347	psta = (struct il3945_sta_priv *)sta->drv_priv;
348	rs_sta = &psta->rs_sta;
349	sband = hw->wiphy->bands[conf->chandef.chan->band];
350
351	rs_sta->il = il;
352
353	rs_sta->start_rate = RATE_INVALID;
354
355	/* default to just 802.11b */
356	rs_sta->expected_tpt = il3945_expected_tpt_b;
357
358	rs_sta->last_partial_flush = jiffies;
359	rs_sta->last_flush = jiffies;
360	rs_sta->flush_time = RATE_FLUSH;
361	rs_sta->last_tx_packets = 0;
362
363	rs_sta->rate_scale_flush.data = (unsigned long)rs_sta;
364	rs_sta->rate_scale_flush.function = il3945_bg_rate_scale_flush;
365
366	for (i = 0; i < RATE_COUNT_3945; i++)
367		il3945_clear_win(&rs_sta->win[i]);
368
369	/* TODO: what is a good starting rate for STA? About middle? Maybe not
370	 * the lowest or the highest rate.. Could consider using RSSI from
371	 * previous packets? Need to have IEEE 802.1X auth succeed immediately
372	 * after assoc.. */
373
374	for (i = sband->n_bitrates - 1; i >= 0; i--) {
375		if (sta->supp_rates[sband->band] & (1 << i)) {
376			rs_sta->last_txrate_idx = i;
377			break;
378		}
379	}
380
381	il->_3945.sta_supp_rates = sta->supp_rates[sband->band];
382	/* For 5 GHz band it start at IL_FIRST_OFDM_RATE */
383	if (sband->band == IEEE80211_BAND_5GHZ) {
384		rs_sta->last_txrate_idx += IL_FIRST_OFDM_RATE;
385		il->_3945.sta_supp_rates <<= IL_FIRST_OFDM_RATE;
386	}
387
388out:
389	il->stations[sta_id].used &= ~IL_STA_UCODE_INPROGRESS;
390
391	D_INFO("leave\n");
392}
393
394static void *
395il3945_rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
396{
397	return hw->priv;
398}
399
400/* rate scale requires free function to be implemented */
401static void
402il3945_rs_free(void *il)
403{
404}
405
406static void *
407il3945_rs_alloc_sta(void *il_priv, struct ieee80211_sta *sta, gfp_t gfp)
408{
409	struct il3945_rs_sta *rs_sta;
410	struct il3945_sta_priv *psta = (void *)sta->drv_priv;
411	struct il_priv *il __maybe_unused = il_priv;
412
413	D_RATE("enter\n");
414
415	rs_sta = &psta->rs_sta;
416
417	spin_lock_init(&rs_sta->lock);
418	init_timer(&rs_sta->rate_scale_flush);
419
420	D_RATE("leave\n");
421
422	return rs_sta;
423}
424
425static void
426il3945_rs_free_sta(void *il_priv, struct ieee80211_sta *sta, void *il_sta)
427{
428	struct il3945_rs_sta *rs_sta = il_sta;
429
430	/*
431	 * Be careful not to use any members of il3945_rs_sta (like trying
432	 * to use il_priv to print out debugging) since it may not be fully
433	 * initialized at this point.
434	 */
435	del_timer_sync(&rs_sta->rate_scale_flush);
436}
437
438/**
439 * il3945_rs_tx_status - Update rate control values based on Tx results
440 *
441 * NOTE: Uses il_priv->retry_rate for the # of retries attempted by
442 * the hardware for each rate.
443 */
444static void
445il3945_rs_tx_status(void *il_rate, struct ieee80211_supported_band *sband,
446		    struct ieee80211_sta *sta, void *il_sta,
447		    struct sk_buff *skb)
448{
449	s8 retries = 0, current_count;
450	int scale_rate_idx, first_idx, last_idx;
451	unsigned long flags;
452	struct il_priv *il = (struct il_priv *)il_rate;
453	struct il3945_rs_sta *rs_sta = il_sta;
454	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
455
456	D_RATE("enter\n");
457
458	retries = info->status.rates[0].count;
459	/* Sanity Check for retries */
460	if (retries > RATE_RETRY_TH)
461		retries = RATE_RETRY_TH;
462
463	first_idx = sband->bitrates[info->status.rates[0].idx].hw_value;
464	if (first_idx < 0 || first_idx >= RATE_COUNT_3945) {
465		D_RATE("leave: Rate out of bounds: %d\n", first_idx);
466		return;
467	}
468
469	if (!il_sta) {
470		D_RATE("leave: No STA il data to update!\n");
471		return;
472	}
473
474	/* Treat uninitialized rate scaling data same as non-existing. */
475	if (!rs_sta->il) {
476		D_RATE("leave: STA il data uninitialized!\n");
477		return;
478	}
479
480	rs_sta->tx_packets++;
481
482	scale_rate_idx = first_idx;
483	last_idx = first_idx;
484
485	/*
486	 * Update the win for each rate.  We determine which rates
487	 * were Tx'd based on the total number of retries vs. the number
488	 * of retries configured for each rate -- currently set to the
489	 * il value 'retry_rate' vs. rate specific
490	 *
491	 * On exit from this while loop last_idx indicates the rate
492	 * at which the frame was finally transmitted (or failed if no
493	 * ACK)
494	 */
495	while (retries > 1) {
496		if ((retries - 1) < il->retry_rate) {
497			current_count = (retries - 1);
498			last_idx = scale_rate_idx;
499		} else {
500			current_count = il->retry_rate;
501			last_idx = il3945_rs_next_rate(il, scale_rate_idx);
502		}
503
504		/* Update this rate accounting for as many retries
505		 * as was used for it (per current_count) */
506		il3945_collect_tx_data(rs_sta, &rs_sta->win[scale_rate_idx], 0,
507				       current_count, scale_rate_idx);
508		D_RATE("Update rate %d for %d retries.\n", scale_rate_idx,
509		       current_count);
510
511		retries -= current_count;
512
513		scale_rate_idx = last_idx;
514	}
515
516	/* Update the last idx win with success/failure based on ACK */
517	D_RATE("Update rate %d with %s.\n", last_idx,
518	       (info->flags & IEEE80211_TX_STAT_ACK) ? "success" : "failure");
519	il3945_collect_tx_data(rs_sta, &rs_sta->win[last_idx],
520			       info->flags & IEEE80211_TX_STAT_ACK, 1,
521			       last_idx);
522
523	/* We updated the rate scale win -- if its been more than
524	 * flush_time since the last run, schedule the flush
525	 * again */
526	spin_lock_irqsave(&rs_sta->lock, flags);
527
528	if (!rs_sta->flush_pending &&
529	    time_after(jiffies, rs_sta->last_flush + rs_sta->flush_time)) {
530
531		rs_sta->last_partial_flush = jiffies;
532		rs_sta->flush_pending = 1;
533		mod_timer(&rs_sta->rate_scale_flush,
534			  jiffies + rs_sta->flush_time);
535	}
536
537	spin_unlock_irqrestore(&rs_sta->lock, flags);
538
539	D_RATE("leave\n");
540}
541
542static u16
543il3945_get_adjacent_rate(struct il3945_rs_sta *rs_sta, u8 idx, u16 rate_mask,
544			 enum ieee80211_band band)
545{
546	u8 high = RATE_INVALID;
547	u8 low = RATE_INVALID;
548	struct il_priv *il __maybe_unused = rs_sta->il;
549
550	/* 802.11A walks to the next literal adjacent rate in
551	 * the rate table */
552	if (unlikely(band == IEEE80211_BAND_5GHZ)) {
553		int i;
554		u32 mask;
555
556		/* Find the previous rate that is in the rate mask */
557		i = idx - 1;
558		for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
559			if (rate_mask & mask) {
560				low = i;
561				break;
562			}
563		}
564
565		/* Find the next rate that is in the rate mask */
566		i = idx + 1;
567		for (mask = (1 << i); i < RATE_COUNT_3945; i++, mask <<= 1) {
568			if (rate_mask & mask) {
569				high = i;
570				break;
571			}
572		}
573
574		return (high << 8) | low;
575	}
576
577	low = idx;
578	while (low != RATE_INVALID) {
579		if (rs_sta->tgg)
580			low = il3945_rates[low].prev_rs_tgg;
581		else
582			low = il3945_rates[low].prev_rs;
583		if (low == RATE_INVALID)
584			break;
585		if (rate_mask & (1 << low))
586			break;
587		D_RATE("Skipping masked lower rate: %d\n", low);
588	}
589
590	high = idx;
591	while (high != RATE_INVALID) {
592		if (rs_sta->tgg)
593			high = il3945_rates[high].next_rs_tgg;
594		else
595			high = il3945_rates[high].next_rs;
596		if (high == RATE_INVALID)
597			break;
598		if (rate_mask & (1 << high))
599			break;
600		D_RATE("Skipping masked higher rate: %d\n", high);
601	}
602
603	return (high << 8) | low;
604}
605
606/**
607 * il3945_rs_get_rate - find the rate for the requested packet
608 *
609 * Returns the ieee80211_rate structure allocated by the driver.
610 *
611 * The rate control algorithm has no internal mapping between hw_mode's
612 * rate ordering and the rate ordering used by the rate control algorithm.
613 *
614 * The rate control algorithm uses a single table of rates that goes across
615 * the entire A/B/G spectrum vs. being limited to just one particular
616 * hw_mode.
617 *
618 * As such, we can't convert the idx obtained below into the hw_mode's
619 * rate table and must reference the driver allocated rate table
620 *
621 */
622static void
623il3945_rs_get_rate(void *il_r, struct ieee80211_sta *sta, void *il_sta,
624		   struct ieee80211_tx_rate_control *txrc)
625{
626	struct ieee80211_supported_band *sband = txrc->sband;
627	struct sk_buff *skb = txrc->skb;
628	u8 low = RATE_INVALID;
629	u8 high = RATE_INVALID;
630	u16 high_low;
631	int idx;
632	struct il3945_rs_sta *rs_sta = il_sta;
633	struct il3945_rate_scale_data *win = NULL;
634	int current_tpt = IL_INVALID_VALUE;
635	int low_tpt = IL_INVALID_VALUE;
636	int high_tpt = IL_INVALID_VALUE;
637	u32 fail_count;
638	s8 scale_action = 0;
639	unsigned long flags;
640	u16 rate_mask;
641	s8 max_rate_idx = -1;
642	struct il_priv *il __maybe_unused = (struct il_priv *)il_r;
643	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
644
645	D_RATE("enter\n");
646
647	/* Treat uninitialized rate scaling data same as non-existing. */
648	if (rs_sta && !rs_sta->il) {
649		D_RATE("Rate scaling information not initialized yet.\n");
650		il_sta = NULL;
651	}
652
653	if (rate_control_send_low(sta, il_sta, txrc))
654		return;
655
656	rate_mask = sta->supp_rates[sband->band];
657
658	/* get user max rate if set */
659	max_rate_idx = txrc->max_rate_idx;
660	if (sband->band == IEEE80211_BAND_5GHZ && max_rate_idx != -1)
661		max_rate_idx += IL_FIRST_OFDM_RATE;
662	if (max_rate_idx < 0 || max_rate_idx >= RATE_COUNT)
663		max_rate_idx = -1;
664
665	idx = min(rs_sta->last_txrate_idx & 0xffff, RATE_COUNT_3945 - 1);
666
667	if (sband->band == IEEE80211_BAND_5GHZ)
668		rate_mask = rate_mask << IL_FIRST_OFDM_RATE;
669
670	spin_lock_irqsave(&rs_sta->lock, flags);
671
672	/* for recent assoc, choose best rate regarding
673	 * to rssi value
674	 */
675	if (rs_sta->start_rate != RATE_INVALID) {
676		if (rs_sta->start_rate < idx &&
677		    (rate_mask & (1 << rs_sta->start_rate)))
678			idx = rs_sta->start_rate;
679		rs_sta->start_rate = RATE_INVALID;
680	}
681
682	/* force user max rate if set by user */
683	if (max_rate_idx != -1 && max_rate_idx < idx) {
684		if (rate_mask & (1 << max_rate_idx))
685			idx = max_rate_idx;
686	}
687
688	win = &(rs_sta->win[idx]);
689
690	fail_count = win->counter - win->success_counter;
691
692	if (fail_count < RATE_MIN_FAILURE_TH &&
693	    win->success_counter < RATE_MIN_SUCCESS_TH) {
694		spin_unlock_irqrestore(&rs_sta->lock, flags);
695
696		D_RATE("Invalid average_tpt on rate %d: "
697		       "counter: %d, success_counter: %d, "
698		       "expected_tpt is %sNULL\n", idx, win->counter,
699		       win->success_counter,
700		       rs_sta->expected_tpt ? "not " : "");
701
702		/* Can't calculate this yet; not enough history */
703		win->average_tpt = IL_INVALID_VALUE;
704		goto out;
705
706	}
707
708	current_tpt = win->average_tpt;
709
710	high_low =
711	    il3945_get_adjacent_rate(rs_sta, idx, rate_mask, sband->band);
712	low = high_low & 0xff;
713	high = (high_low >> 8) & 0xff;
714
715	/* If user set max rate, dont allow higher than user constrain */
716	if (max_rate_idx != -1 && max_rate_idx < high)
717		high = RATE_INVALID;
718
719	/* Collect Measured throughputs of adjacent rates */
720	if (low != RATE_INVALID)
721		low_tpt = rs_sta->win[low].average_tpt;
722
723	if (high != RATE_INVALID)
724		high_tpt = rs_sta->win[high].average_tpt;
725
726	spin_unlock_irqrestore(&rs_sta->lock, flags);
727
728	scale_action = 0;
729
730	/* Low success ratio , need to drop the rate */
731	if (win->success_ratio < RATE_DECREASE_TH || !current_tpt) {
732		D_RATE("decrease rate because of low success_ratio\n");
733		scale_action = -1;
734		/* No throughput measured yet for adjacent rates,
735		 * try increase */
736	} else if (low_tpt == IL_INVALID_VALUE && high_tpt == IL_INVALID_VALUE) {
737
738		if (high != RATE_INVALID &&
739		    win->success_ratio >= RATE_INCREASE_TH)
740			scale_action = 1;
741		else if (low != RATE_INVALID)
742			scale_action = 0;
743
744		/* Both adjacent throughputs are measured, but neither one has
745		 * better throughput; we're using the best rate, don't change
746		 * it! */
747	} else if (low_tpt != IL_INVALID_VALUE && high_tpt != IL_INVALID_VALUE
748		   && low_tpt < current_tpt && high_tpt < current_tpt) {
749
750		D_RATE("No action -- low [%d] & high [%d] < "
751		       "current_tpt [%d]\n", low_tpt, high_tpt, current_tpt);
752		scale_action = 0;
753
754		/* At least one of the rates has better throughput */
755	} else {
756		if (high_tpt != IL_INVALID_VALUE) {
757
758			/* High rate has better throughput, Increase
759			 * rate */
760			if (high_tpt > current_tpt &&
761			    win->success_ratio >= RATE_INCREASE_TH)
762				scale_action = 1;
763			else {
764				D_RATE("decrease rate because of high tpt\n");
765				scale_action = 0;
766			}
767		} else if (low_tpt != IL_INVALID_VALUE) {
768			if (low_tpt > current_tpt) {
769				D_RATE("decrease rate because of low tpt\n");
770				scale_action = -1;
771			} else if (win->success_ratio >= RATE_INCREASE_TH) {
772				/* Lower rate has better
773				 * throughput,decrease rate */
774				scale_action = 1;
775			}
776		}
777	}
778
779	/* Sanity check; asked for decrease, but success rate or throughput
780	 * has been good at old rate.  Don't change it. */
781	if (scale_action == -1 && low != RATE_INVALID &&
782	    (win->success_ratio > RATE_HIGH_TH ||
783	     current_tpt > 100 * rs_sta->expected_tpt[low]))
784		scale_action = 0;
785
786	switch (scale_action) {
787	case -1:
788		/* Decrese rate */
789		if (low != RATE_INVALID)
790			idx = low;
791		break;
792	case 1:
793		/* Increase rate */
794		if (high != RATE_INVALID)
795			idx = high;
796
797		break;
798	case 0:
799	default:
800		/* No change */
801		break;
802	}
803
804	D_RATE("Selected %d (action %d) - low %d high %d\n", idx, scale_action,
805	       low, high);
806
807out:
808
809	if (sband->band == IEEE80211_BAND_5GHZ) {
810		if (WARN_ON_ONCE(idx < IL_FIRST_OFDM_RATE))
811			idx = IL_FIRST_OFDM_RATE;
812		rs_sta->last_txrate_idx = idx;
813		info->control.rates[0].idx = idx - IL_FIRST_OFDM_RATE;
814	} else {
815		rs_sta->last_txrate_idx = idx;
816		info->control.rates[0].idx = rs_sta->last_txrate_idx;
817	}
818	info->control.rates[0].count = 1;
819
820	D_RATE("leave: %d\n", idx);
821}
822
823#ifdef CONFIG_MAC80211_DEBUGFS
824
825static ssize_t
826il3945_sta_dbgfs_stats_table_read(struct file *file, char __user *user_buf,
827				  size_t count, loff_t *ppos)
828{
829	char *buff;
830	int desc = 0;
831	int j;
832	ssize_t ret;
833	struct il3945_rs_sta *lq_sta = file->private_data;
834
835	buff = kmalloc(1024, GFP_KERNEL);
836	if (!buff)
837		return -ENOMEM;
838
839	desc +=
840	    sprintf(buff + desc,
841		    "tx packets=%d last rate idx=%d\n"
842		    "rate=0x%X flush time %d\n", lq_sta->tx_packets,
843		    lq_sta->last_txrate_idx, lq_sta->start_rate,
844		    jiffies_to_msecs(lq_sta->flush_time));
845	for (j = 0; j < RATE_COUNT_3945; j++) {
846		desc +=
847		    sprintf(buff + desc, "counter=%d success=%d %%=%d\n",
848			    lq_sta->win[j].counter,
849			    lq_sta->win[j].success_counter,
850			    lq_sta->win[j].success_ratio);
851	}
852	ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
853	kfree(buff);
854	return ret;
855}
856
857static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
858	.read = il3945_sta_dbgfs_stats_table_read,
859	.open = simple_open,
860	.llseek = default_llseek,
861};
862
863static void
864il3945_add_debugfs(void *il, void *il_sta, struct dentry *dir)
865{
866	struct il3945_rs_sta *lq_sta = il_sta;
867
868	lq_sta->rs_sta_dbgfs_stats_table_file =
869	    debugfs_create_file("rate_stats_table", 0600, dir, lq_sta,
870				&rs_sta_dbgfs_stats_table_ops);
871
872}
873
874static void
875il3945_remove_debugfs(void *il, void *il_sta)
876{
877	struct il3945_rs_sta *lq_sta = il_sta;
878	debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
879}
880#endif
881
882/*
883 * Initialization of rate scaling information is done by driver after
884 * the station is added. Since mac80211 calls this function before a
885 * station is added we ignore it.
886 */
887static void
888il3945_rs_rate_init_stub(void *il_r, struct ieee80211_supported_band *sband,
889			 struct cfg80211_chan_def *chandef,
890			 struct ieee80211_sta *sta, void *il_sta)
891{
892}
893
894static const struct rate_control_ops rs_ops = {
895	.name = RS_NAME,
896	.tx_status = il3945_rs_tx_status,
897	.get_rate = il3945_rs_get_rate,
898	.rate_init = il3945_rs_rate_init_stub,
899	.alloc = il3945_rs_alloc,
900	.free = il3945_rs_free,
901	.alloc_sta = il3945_rs_alloc_sta,
902	.free_sta = il3945_rs_free_sta,
903#ifdef CONFIG_MAC80211_DEBUGFS
904	.add_sta_debugfs = il3945_add_debugfs,
905	.remove_sta_debugfs = il3945_remove_debugfs,
906#endif
907
908};
909
910void
911il3945_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
912{
913	struct il_priv *il = hw->priv;
914	s32 rssi = 0;
915	unsigned long flags;
916	struct il3945_rs_sta *rs_sta;
917	struct ieee80211_sta *sta;
918	struct il3945_sta_priv *psta;
919
920	D_RATE("enter\n");
921
922	rcu_read_lock();
923
924	sta = ieee80211_find_sta(il->vif, il->stations[sta_id].sta.sta.addr);
925	if (!sta) {
926		D_RATE("Unable to find station to initialize rate scaling.\n");
927		rcu_read_unlock();
928		return;
929	}
930
931	psta = (void *)sta->drv_priv;
932	rs_sta = &psta->rs_sta;
933
934	spin_lock_irqsave(&rs_sta->lock, flags);
935
936	rs_sta->tgg = 0;
937	switch (il->band) {
938	case IEEE80211_BAND_2GHZ:
939		/* TODO: this always does G, not a regression */
940		if (il->active.flags & RXON_FLG_TGG_PROTECT_MSK) {
941			rs_sta->tgg = 1;
942			rs_sta->expected_tpt = il3945_expected_tpt_g_prot;
943		} else
944			rs_sta->expected_tpt = il3945_expected_tpt_g;
945		break;
946	case IEEE80211_BAND_5GHZ:
947		rs_sta->expected_tpt = il3945_expected_tpt_a;
948		break;
949	default:
950		BUG();
951		break;
952	}
953
954	spin_unlock_irqrestore(&rs_sta->lock, flags);
955
956	rssi = il->_3945.last_rx_rssi;
957	if (rssi == 0)
958		rssi = IL_MIN_RSSI_VAL;
959
960	D_RATE("Network RSSI: %d\n", rssi);
961
962	rs_sta->start_rate = il3945_get_rate_idx_by_rssi(rssi, il->band);
963
964	D_RATE("leave: rssi %d assign rate idx: " "%d (plcp 0x%x)\n", rssi,
965	       rs_sta->start_rate, il3945_rates[rs_sta->start_rate].plcp);
966	rcu_read_unlock();
967}
968
969int
970il3945_rate_control_register(void)
971{
972	return ieee80211_rate_control_register(&rs_ops);
973}
974
975void
976il3945_rate_control_unregister(void)
977{
978	ieee80211_rate_control_unregister(&rs_ops);
979}