iwl-4965-rs.c 79.9 KB
Newer Older
Z
Zhu Yi 已提交
1 2
/******************************************************************************
 *
R
Reinette Chatre 已提交
3
 * Copyright(c) 2005 - 2008 Intel Corporation. All rights reserved.
Z
Zhu Yi 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
 *
 * The full GNU General Public License is included in this distribution in the
 * file called LICENSE.
 *
 * Contact Information:
 * James P. Ketrenos <ipw2100-admin@linux.intel.com>
 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
 *
 *****************************************************************************/
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/skbuff.h>
#include <linux/wireless.h>
#include <net/mac80211.h>
#include <net/ieee80211.h>

#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/delay.h>

#include <linux/workqueue.h>

#include "../net/mac80211/ieee80211_rate.h"

41
#include "iwl-4965.h"
Z
Zhu Yi 已提交
42 43 44 45 46 47 48 49
#include "iwl-helpers.h"

#define RS_NAME "iwl-4965-rs"

#define NUM_TRY_BEFORE_ANTENNA_TOGGLE 1
#define IWL_NUMBER_TRY      1
#define IWL_HT_NUMBER_TRY   3

50 51 52 53 54 55
#define IWL_RATE_MAX_WINDOW		62	/* # tx in history window */
#define IWL_RATE_MIN_FAILURE_TH		6	/* min failures to calc tpt */
#define IWL_RATE_MIN_SUCCESS_TH		8	/* min successes to calc tpt */

/* max time to accum history 2 seconds */
#define IWL_RATE_SCALE_FLUSH_INTVL   (2*HZ)
Z
Zhu Yi 已提交
56 57 58 59 60 61 62 63 64 65 66

static u8 rs_ht_to_legacy[] = {
	IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
	IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
	IWL_RATE_6M_INDEX,
	IWL_RATE_6M_INDEX, IWL_RATE_9M_INDEX,
	IWL_RATE_12M_INDEX, IWL_RATE_18M_INDEX,
	IWL_RATE_24M_INDEX, IWL_RATE_36M_INDEX,
	IWL_RATE_48M_INDEX, IWL_RATE_54M_INDEX
};

C
Christoph Hellwig 已提交
67
struct iwl4965_rate {
Z
Zhu Yi 已提交
68 69 70
	u32 rate_n_flags;
} __attribute__ ((packed));

71 72 73
/**
 * struct iwl4965_rate_scale_data -- tx success history for one rate
 */
C
Christoph Hellwig 已提交
74
struct iwl4965_rate_scale_data {
75 76 77 78 79
	u64 data;		/* bitmap of successful frames */
	s32 success_counter;	/* number of frames successful */
	s32 success_ratio;	/* per-cent * 128  */
	s32 counter;		/* number of frames attempted */
	s32 average_tpt;	/* success ratio * expected throughput */
Z
Zhu Yi 已提交
80 81 82
	unsigned long stamp;
};

83 84 85
/**
 * struct iwl4965_scale_tbl_info -- tx params and success history for all rates
 *
86
 * There are two of these in struct iwl4965_lq_sta,
87 88
 * one for "active", and one for "search".
 */
C
Christoph Hellwig 已提交
89 90 91
struct iwl4965_scale_tbl_info {
	enum iwl4965_table_type lq_type;
	enum iwl4965_antenna_type antenna_type;
92 93 94 95 96 97 98
	u8 is_SGI;	/* 1 = short guard interval */
	u8 is_fat;	/* 1 = 40 MHz channel width */
	u8 is_dup;	/* 1 = duplicated data streams */
	u8 action;	/* change modulation; IWL_[LEGACY/SISO/MIMO]_SWITCH_* */
	s32 *expected_tpt;	/* throughput metrics; expected_tpt_G, etc. */
	struct iwl4965_rate current_rate;  /* rate_n_flags, uCode API format */
	struct iwl4965_rate_scale_data win[IWL_RATE_COUNT]; /* rate histories */
Z
Zhu Yi 已提交
99 100
};

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
#ifdef CONFIG_IWL4965_HT

struct iwl4965_traffic_load {
	unsigned long time_stamp;	/* age of the oldest statistics */
	u32 packet_count[TID_QUEUE_MAX_SIZE];   /* packet count in this time
						 * slice */
	u32 total;			/* total num of packets during the
					 * last TID_MAX_TIME_DIFF */
	u8 queue_count;			/* number of queues that has
					 * been used since the last cleanup */
	u8 head;			/* start of the circular buffer */
};

#endif /* CONFIG_IWL4965_HT */

116
/**
117
 * struct iwl4965_lq_sta -- driver's rate scaling private structure
118 119 120
 *
 * Pointer to this gets passed back and forth between driver and mac80211.
 */
121
struct iwl4965_lq_sta {
122 123 124 125
	u8 active_tbl;		/* index of active table, range 0-1 */
	u8 enable_counter;	/* indicates HT mode */
	u8 stay_in_tbl;		/* 1: disallow, 0: allow search for new mode */
	u8 search_better_tbl;	/* 1: currently trying alternate mode */
Z
Zhu Yi 已提交
126
	s32 last_tpt;
127 128

	/* The following determine when to search for a new mode */
Z
Zhu Yi 已提交
129
	u32 table_count_limit;
130 131
	u32 max_failure_limit;	/* # failed frames before new search */
	u32 max_success_limit;	/* # successful frames before new search */
Z
Zhu Yi 已提交
132
	u32 table_count;
133 134 135 136 137
	u32 total_failed;	/* total failed frames, any/all rates */
	u32 total_success;	/* total successful frames, any/all rates */
	u32 flush_timer;	/* time staying in mode before new search */

	u8 action_counter;	/* # mode-switch actions tried */
Z
Zhu Yi 已提交
138 139 140 141
	u8 antenna;
	u8 valid_antenna;
	u8 is_green;
	u8 is_dup;
142
	enum ieee80211_band band;
Z
Zhu Yi 已提交
143
	u8 ibss_sta_added;
144 145

	/* The following are bitmaps of rates; IWL_RATE_6M_MASK, etc. */
146
	u32 supp_rates;
Z
Zhu Yi 已提交
147 148 149 150
	u16 active_rate;
	u16 active_siso_rate;
	u16 active_mimo_rate;
	u16 active_rate_basic;
151

C
Christoph Hellwig 已提交
152
	struct iwl4965_link_quality_cmd lq;
153
	struct iwl4965_scale_tbl_info lq_info[LQ_SIZE]; /* "active", "search" */
154 155 156 157
#ifdef CONFIG_IWL4965_HT
	struct iwl4965_traffic_load load[TID_MAX_LOAD_COUNT];
	u8 tx_agg_tid_en;
#endif
158
#ifdef CONFIG_MAC80211_DEBUGFS
159
	struct dentry *rs_sta_dbgfs_scale_table_file;
160
	struct dentry *rs_sta_dbgfs_stats_table_file;
161 162 163
#ifdef CONFIG_IWL4965_HT
	struct dentry *rs_sta_dbgfs_tx_agg_tid_en_file;
#endif
C
Christoph Hellwig 已提交
164 165
	struct iwl4965_rate dbg_fixed;
	struct iwl4965_priv *drv;
166
#endif
Z
Zhu Yi 已提交
167 168
};

C
Christoph Hellwig 已提交
169
static void rs_rate_scale_perform(struct iwl4965_priv *priv,
Z
Zhu Yi 已提交
170 171 172
				   struct net_device *dev,
				   struct ieee80211_hdr *hdr,
				   struct sta_info *sta);
173
static void rs_fill_link_cmd(struct iwl4965_lq_sta *lq_sta,
C
Christoph Hellwig 已提交
174 175
			     struct iwl4965_rate *tx_mcs,
			     struct iwl4965_link_quality_cmd *tbl);
Z
Zhu Yi 已提交
176 177


178
#ifdef CONFIG_MAC80211_DEBUGFS
179
static void rs_dbgfs_set_mcs(struct iwl4965_lq_sta *lq_sta,
C
Christoph Hellwig 已提交
180
				struct iwl4965_rate *mcs, int index);
181
#else
182
static void rs_dbgfs_set_mcs(struct iwl4965_lq_sta *lq_sta,
C
Christoph Hellwig 已提交
183
				struct iwl4965_rate *mcs, int index)
184 185
{}
#endif
186 187 188 189 190 191

/*
 * Expected throughput metrics for following rates:
 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
 * "G" is the only table that supports CCK (the first 4 rates).
 */
Z
Zhu Yi 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
static s32 expected_tpt_A[IWL_RATE_COUNT] = {
	0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186, 186
};

static s32 expected_tpt_G[IWL_RATE_COUNT] = {
	7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 186
};

static s32 expected_tpt_siso20MHz[IWL_RATE_COUNT] = {
	0, 0, 0, 0, 42, 42, 76, 102, 124, 159, 183, 193, 202
};

static s32 expected_tpt_siso20MHzSGI[IWL_RATE_COUNT] = {
	0, 0, 0, 0, 46, 46, 82, 110, 132, 168, 192, 202, 211
};

static s32 expected_tpt_mimo20MHz[IWL_RATE_COUNT] = {
	0, 0, 0, 0, 74, 74, 123, 155, 179, 214, 236, 244, 251
};

static s32 expected_tpt_mimo20MHzSGI[IWL_RATE_COUNT] = {
	0, 0, 0, 0, 81, 81, 131, 164, 188, 222, 243, 251, 257
};

static s32 expected_tpt_siso40MHz[IWL_RATE_COUNT] = {
	0, 0, 0, 0, 77, 77, 127, 160, 184, 220, 242, 250, 257
};

static s32 expected_tpt_siso40MHzSGI[IWL_RATE_COUNT] = {
	0, 0, 0, 0, 83, 83, 135, 169, 193, 229, 250, 257, 264
};

static s32 expected_tpt_mimo40MHz[IWL_RATE_COUNT] = {
	0, 0, 0, 0, 123, 123, 182, 214, 235, 264, 279, 285, 289
};

static s32 expected_tpt_mimo40MHzSGI[IWL_RATE_COUNT] = {
	0, 0, 0, 0, 131, 131, 191, 222, 242, 270, 284, 289, 293
};

C
Christoph Hellwig 已提交
232 233
static int iwl4965_lq_sync_callback(struct iwl4965_priv *priv,
				struct iwl4965_cmd *cmd, struct sk_buff *skb)
Z
Zhu Yi 已提交
234 235 236 237 238
{
	/*We didn't cache the SKB; let the caller free it */
	return 1;
}

C
Christoph Hellwig 已提交
239
static inline u8 iwl4965_rate_get_rate(u32 rate_n_flags)
Z
Zhu Yi 已提交
240 241 242 243
{
	return (u8)(rate_n_flags & 0xFF);
}

C
Christoph Hellwig 已提交
244 245
static int rs_send_lq_cmd(struct iwl4965_priv *priv,
			  struct iwl4965_link_quality_cmd *lq, u8 flags)
Z
Zhu Yi 已提交
246
{
247
#ifdef CONFIG_IWL4965_DEBUG
Z
Zhu Yi 已提交
248 249
	int i;
#endif
C
Christoph Hellwig 已提交
250
	struct iwl4965_host_cmd cmd = {
Z
Zhu Yi 已提交
251
		.id = REPLY_TX_LINK_QUALITY_CMD,
C
Christoph Hellwig 已提交
252
		.len = sizeof(struct iwl4965_link_quality_cmd),
Z
Zhu Yi 已提交
253 254 255 256 257 258
		.meta.flags = flags,
		.data = lq,
	};

	if ((lq->sta_id == 0xFF) &&
	    (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
259
		return -EINVAL;
Z
Zhu Yi 已提交
260 261 262 263 264 265 266 267

	if (lq->sta_id == 0xFF)
		lq->sta_id = IWL_AP_ID;

	IWL_DEBUG_RATE("lq station id 0x%x\n", lq->sta_id);
	IWL_DEBUG_RATE("lq dta 0x%X 0x%X\n",
		       lq->general_params.single_stream_ant_msk,
		       lq->general_params.dual_stream_ant_msk);
268
#ifdef CONFIG_IWL4965_DEBUG
Z
Zhu Yi 已提交
269 270 271 272 273 274
	for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
		IWL_DEBUG_RATE("lq index %d 0x%X\n",
				i, lq->rs_table[i].rate_n_flags);
#endif

	if (flags & CMD_ASYNC)
C
Christoph Hellwig 已提交
275
		cmd.meta.u.callback = iwl4965_lq_sync_callback;
Z
Zhu Yi 已提交
276

C
Christoph Hellwig 已提交
277
	if (iwl4965_is_associated(priv) && priv->assoc_station_added &&
Z
Zhu Yi 已提交
278
	    priv->lq_mngr.lq_ready)
C
Christoph Hellwig 已提交
279
		return  iwl4965_send_cmd(priv, &cmd);
Z
Zhu Yi 已提交
280

281
	return 0;
Z
Zhu Yi 已提交
282 283
}

C
Christoph Hellwig 已提交
284
static void rs_rate_scale_clear_window(struct iwl4965_rate_scale_data *window)
Z
Zhu Yi 已提交
285 286 287 288 289 290 291 292 293
{
	window->data = 0;
	window->success_counter = 0;
	window->success_ratio = IWL_INVALID_VALUE;
	window->counter = 0;
	window->average_tpt = IWL_INVALID_VALUE;
	window->stamp = 0;
}

294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
#ifdef CONFIG_IWL4965_HT
/*
 *	removes the old data from the statistics. All data that is older than
 *	TID_MAX_TIME_DIFF, will be deleted.
 */
static void rs_tl_rm_old_stats(struct iwl4965_traffic_load *tl, u32 curr_time)
{
	/* The oldest age we want to keep */
	u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;

	while (tl->queue_count &&
	       (tl->time_stamp < oldest_time)) {
		tl->total -= tl->packet_count[tl->head];
		tl->packet_count[tl->head] = 0;
		tl->time_stamp += TID_QUEUE_CELL_SPACING;
		tl->queue_count--;
		tl->head++;
		if (tl->head >= TID_QUEUE_MAX_SIZE)
			tl->head = 0;
	}
}

/*
 *	increment traffic load value for tid and also remove
 *	any old values if passed the certain time period
 */
static void rs_tl_add_packet(struct iwl4965_lq_sta *lq_data, u8 tid)
{
	u32 curr_time = jiffies_to_msecs(jiffies);
	u32 time_diff;
	s32 index;
	struct iwl4965_traffic_load *tl = NULL;

	if (tid >= TID_MAX_LOAD_COUNT)
		return;

	tl = &lq_data->load[tid];

	curr_time -= curr_time % TID_ROUND_VALUE;

	/* Happens only for the first packet. Initialize the data */
	if (!(tl->queue_count)) {
		tl->total = 1;
		tl->time_stamp = curr_time;
		tl->queue_count = 1;
		tl->head = 0;
		tl->packet_count[0] = 1;
		return;
	}

	time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
	index = time_diff / TID_QUEUE_CELL_SPACING;

	/* The history is too long: remove data that is older than */
	/* TID_MAX_TIME_DIFF */
	if (index >= TID_QUEUE_MAX_SIZE)
		rs_tl_rm_old_stats(tl, curr_time);

	index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
	tl->packet_count[index] = tl->packet_count[index] + 1;
	tl->total = tl->total + 1;

	if ((index + 1) > tl->queue_count)
		tl->queue_count = index + 1;
}

/*
	get the traffic load value for tid
*/
static u32 rs_tl_get_load(struct iwl4965_lq_sta *lq_data, u8 tid)
{
	u32 curr_time = jiffies_to_msecs(jiffies);
	u32 time_diff;
	s32 index;
	struct iwl4965_traffic_load *tl = NULL;

	if (tid >= TID_MAX_LOAD_COUNT)
		return 0;

	tl = &(lq_data->load[tid]);

	curr_time -= curr_time % TID_ROUND_VALUE;

	if (!(tl->queue_count))
		return 0;

	time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
	index = time_diff / TID_QUEUE_CELL_SPACING;

	/* The history is too long: remove data that is older than */
	/* TID_MAX_TIME_DIFF */
	if (index >= TID_QUEUE_MAX_SIZE)
		rs_tl_rm_old_stats(tl, curr_time);

	return tl->total;
}

static void rs_tl_turn_on_agg_for_tid(struct iwl4965_priv *priv,
				struct iwl4965_lq_sta *lq_data, u8 tid,
				struct sta_info *sta)
{
	unsigned long state;
	DECLARE_MAC_BUF(mac);

	spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
	state = sta->ampdu_mlme.tid_tx[tid].state;
	spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);

	if (state == HT_AGG_STATE_IDLE &&
	    rs_tl_get_load(lq_data, tid) > IWL_AGG_LOAD_THRESHOLD) {
		IWL_DEBUG_HT("Starting Tx agg: STA: %s tid: %d\n",
				print_mac(mac, sta->addr), tid);
		ieee80211_start_tx_ba_session(priv->hw, sta->addr, tid);
	}
}

static void rs_tl_turn_on_agg(struct iwl4965_priv *priv, u8 tid,
				struct iwl4965_lq_sta *lq_data,
				struct sta_info *sta)
{
	if ((tid < TID_MAX_LOAD_COUNT))
		rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
	else if (tid == IWL_AGG_ALL_TID)
		for (tid = 0; tid < TID_MAX_LOAD_COUNT; tid++)
			rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
}

#endif /* CONFIG_IWLWIFI_HT */

423 424 425 426 427 428 429
/**
 * rs_collect_tx_data - Update the success/failure sliding window
 *
 * We keep a sliding window of the last 62 packets transmitted
 * at this rate.  window->data contains the bitmask of successful
 * packets.
 */
C
Christoph Hellwig 已提交
430
static int rs_collect_tx_data(struct iwl4965_rate_scale_data *windows,
431 432
			      int scale_index, s32 tpt, int retries,
			      int successes)
Z
Zhu Yi 已提交
433
{
C
Christoph Hellwig 已提交
434
	struct iwl4965_rate_scale_data *window = NULL;
Z
Zhu Yi 已提交
435 436 437 438
	u64 mask;
	u8 win_size = IWL_RATE_MAX_WINDOW;
	s32 fail_count;

439 440
	if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
		return -EINVAL;
Z
Zhu Yi 已提交
441

442
	/* Select data for current tx bit rate */
Z
Zhu Yi 已提交
443 444
	window = &(windows[scale_index]);

445 446 447 448 449 450 451 452
	/*
	 * Keep track of only the latest 62 tx frame attempts in this rate's
	 * history window; anything older isn't really relevant any more.
	 * If we have filled up the sliding window, drop the oldest attempt;
	 * if the oldest attempt (highest bit in bitmap) shows "success",
	 * subtract "1" from the success counter (this is the main reason
	 * we keep these bitmaps!).
	 */
453 454 455 456 457 458 459 460 461 462
	while (retries > 0) {
		if (window->counter >= win_size) {
			window->counter = win_size - 1;
			mask = 1;
			mask = (mask << (win_size - 1));
			if (window->data & mask) {
				window->data &= ~mask;
				window->success_counter =
					window->success_counter - 1;
			}
Z
Zhu Yi 已提交
463 464
		}

465 466 467 468 469 470 471 472 473 474 475 476 477
		/* Increment frames-attempted counter */
		window->counter++;

		/* Shift bitmap by one frame (throw away oldest history),
		 * OR in "1", and increment "success" if this
		 * frame was successful. */
		mask = window->data;
		window->data = (mask << 1);
		if (successes > 0) {
			window->success_counter = window->success_counter + 1;
			window->data |= 0x1;
			successes--;
		}
478

479
		retries--;
Z
Zhu Yi 已提交
480 481
	}

482
	/* Calculate current success ratio, avoid divide-by-0! */
Z
Zhu Yi 已提交
483 484 485 486 487 488 489 490
	if (window->counter > 0)
		window->success_ratio = 128 * (100 * window->success_counter)
					/ window->counter;
	else
		window->success_ratio = IWL_INVALID_VALUE;

	fail_count = window->counter - window->success_counter;

491
	/* Calculate average throughput, if we have enough history. */
Z
Zhu Yi 已提交
492 493 494 495 496 497
	if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
	    (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
		window->average_tpt = (window->success_ratio * tpt + 64) / 128;
	else
		window->average_tpt = IWL_INVALID_VALUE;

498
	/* Tag this window as having been updated */
Z
Zhu Yi 已提交
499 500
	window->stamp = jiffies;

501
	return 0;
Z
Zhu Yi 已提交
502 503
}

504 505 506
/*
 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
 */
C
Christoph Hellwig 已提交
507 508
static void rs_mcs_from_tbl(struct iwl4965_rate *mcs_rate,
			   struct iwl4965_scale_tbl_info *tbl,
Z
Zhu Yi 已提交
509 510 511
			   int index, u8 use_green)
{
	if (is_legacy(tbl->lq_type)) {
C
Christoph Hellwig 已提交
512
		mcs_rate->rate_n_flags = iwl4965_rates[index].plcp;
Z
Zhu Yi 已提交
513 514 515 516 517 518
		if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
			mcs_rate->rate_n_flags |= RATE_MCS_CCK_MSK;

	} else if (is_siso(tbl->lq_type)) {
		if (index > IWL_LAST_OFDM_RATE)
			index = IWL_LAST_OFDM_RATE;
C
Christoph Hellwig 已提交
519
		 mcs_rate->rate_n_flags = iwl4965_rates[index].plcp_siso |
Z
Zhu Yi 已提交
520 521 522 523
					  RATE_MCS_HT_MSK;
	} else {
		if (index > IWL_LAST_OFDM_RATE)
			index = IWL_LAST_OFDM_RATE;
C
Christoph Hellwig 已提交
524
		mcs_rate->rate_n_flags = iwl4965_rates[index].plcp_mimo |
Z
Zhu Yi 已提交
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
					 RATE_MCS_HT_MSK;
	}

	switch (tbl->antenna_type) {
	case ANT_BOTH:
		mcs_rate->rate_n_flags |= RATE_MCS_ANT_AB_MSK;
		break;
	case ANT_MAIN:
		mcs_rate->rate_n_flags |= RATE_MCS_ANT_A_MSK;
		break;
	case ANT_AUX:
		mcs_rate->rate_n_flags |= RATE_MCS_ANT_B_MSK;
		break;
	case ANT_NONE:
		break;
	}

	if (is_legacy(tbl->lq_type))
543
		return;
Z
Zhu Yi 已提交
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560

	if (tbl->is_fat) {
		if (tbl->is_dup)
			mcs_rate->rate_n_flags |= RATE_MCS_DUP_MSK;
		else
			mcs_rate->rate_n_flags |= RATE_MCS_FAT_MSK;
	}
	if (tbl->is_SGI)
		mcs_rate->rate_n_flags |= RATE_MCS_SGI_MSK;

	if (use_green) {
		mcs_rate->rate_n_flags |= RATE_MCS_GF_MSK;
		if (is_siso(tbl->lq_type))
			mcs_rate->rate_n_flags &= ~RATE_MCS_SGI_MSK;
	}
}

561 562 563 564
/*
 * Interpret uCode API's rate_n_flags format,
 * fill "search" or "active" tx mode table.
 */
C
Christoph Hellwig 已提交
565
static int rs_get_tbl_info_from_mcs(const struct iwl4965_rate *mcs_rate,
566 567
				    enum ieee80211_band band,
				    struct iwl4965_scale_tbl_info *tbl,
Z
Zhu Yi 已提交
568 569 570 571 572
				    int *rate_idx)
{
	int index;
	u32 ant_msk;

573
	index = iwl4965_hwrate_to_plcp_idx(mcs_rate->rate_n_flags);
Z
Zhu Yi 已提交
574 575 576

	if (index  == IWL_RATE_INVALID) {
		*rate_idx = -1;
577
		return -EINVAL;
Z
Zhu Yi 已提交
578
	}
579
	tbl->is_SGI = 0;	/* default legacy setup */
Z
Zhu Yi 已提交
580 581
	tbl->is_fat = 0;
	tbl->is_dup = 0;
582
	tbl->antenna_type = ANT_BOTH;	/* default MIMO setup */
Z
Zhu Yi 已提交
583

584
	/* legacy rate format */
Z
Zhu Yi 已提交
585 586 587 588 589 590 591
	if (!(mcs_rate->rate_n_flags & RATE_MCS_HT_MSK)) {
		ant_msk = (mcs_rate->rate_n_flags & RATE_MCS_ANT_AB_MSK);

		if (ant_msk == RATE_MCS_ANT_AB_MSK)
			tbl->lq_type = LQ_NONE;
		else {

592
			if (band == IEEE80211_BAND_5GHZ)
Z
Zhu Yi 已提交
593 594 595 596 597 598 599 600 601 602 603
				tbl->lq_type = LQ_A;
			else
				tbl->lq_type = LQ_G;

			if (mcs_rate->rate_n_flags & RATE_MCS_ANT_A_MSK)
				tbl->antenna_type = ANT_MAIN;
			else
				tbl->antenna_type = ANT_AUX;
		}
		*rate_idx = index;

604
	/* HT rate format, SISO (might be 20 MHz legacy or 40 MHz fat width) */
C
Christoph Hellwig 已提交
605
	} else if (iwl4965_rate_get_rate(mcs_rate->rate_n_flags)
Z
Zhu Yi 已提交
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
					<= IWL_RATE_SISO_60M_PLCP) {
		tbl->lq_type = LQ_SISO;

		ant_msk = (mcs_rate->rate_n_flags & RATE_MCS_ANT_AB_MSK);
		if (ant_msk == RATE_MCS_ANT_AB_MSK)
			tbl->lq_type = LQ_NONE;
		else {
			if (mcs_rate->rate_n_flags & RATE_MCS_ANT_A_MSK)
				tbl->antenna_type = ANT_MAIN;
			else
				tbl->antenna_type = ANT_AUX;
		}
		if (mcs_rate->rate_n_flags & RATE_MCS_SGI_MSK)
			tbl->is_SGI = 1;

		if ((mcs_rate->rate_n_flags & RATE_MCS_FAT_MSK) ||
		    (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK))
			tbl->is_fat = 1;

		if (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK)
			tbl->is_dup = 1;

		*rate_idx = index;
629 630

	/* HT rate format, MIMO (might be 20 MHz legacy or 40 MHz fat width) */
Z
Zhu Yi 已提交
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
	} else {
		tbl->lq_type = LQ_MIMO;
		if (mcs_rate->rate_n_flags & RATE_MCS_SGI_MSK)
			tbl->is_SGI = 1;

		if ((mcs_rate->rate_n_flags & RATE_MCS_FAT_MSK) ||
		    (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK))
			tbl->is_fat = 1;

		if (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK)
			tbl->is_dup = 1;
		*rate_idx = index;
	}
	return 0;
}

C
Christoph Hellwig 已提交
647 648
static inline void rs_toggle_antenna(struct iwl4965_rate *new_rate,
				     struct iwl4965_scale_tbl_info *tbl)
Z
Zhu Yi 已提交
649 650 651 652 653 654 655 656 657 658 659 660
{
	if (tbl->antenna_type == ANT_AUX) {
		tbl->antenna_type = ANT_MAIN;
		new_rate->rate_n_flags &= ~RATE_MCS_ANT_B_MSK;
		new_rate->rate_n_flags |= RATE_MCS_ANT_A_MSK;
	} else {
		tbl->antenna_type = ANT_AUX;
		new_rate->rate_n_flags &= ~RATE_MCS_ANT_A_MSK;
		new_rate->rate_n_flags |= RATE_MCS_ANT_B_MSK;
	}
}

661 662
static inline u8 rs_use_green(struct iwl4965_priv *priv,
			      struct ieee80211_conf *conf)
Z
Zhu Yi 已提交
663
{
664
#ifdef CONFIG_IWL4965_HT
665 666 667 668
	return ((conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
		priv->current_ht_config.is_green_field &&
		!priv->current_ht_config.non_GF_STA_present);
#endif	/* CONFIG_IWL4965_HT */
669
	return 0;
Z
Zhu Yi 已提交
670 671 672 673 674 675 676 677 678
}

/**
 * rs_get_supported_rates - get the available rates
 *
 * if management frame or broadcast frame only return
 * basic available rates.
 *
 */
679
static void rs_get_supported_rates(struct iwl4965_lq_sta *lq_sta,
Z
Zhu Yi 已提交
680
				   struct ieee80211_hdr *hdr,
C
Christoph Hellwig 已提交
681
				   enum iwl4965_table_type rate_type,
Z
Zhu Yi 已提交
682 683 684
				   u16 *data_rate)
{
	if (is_legacy(rate_type))
685
		*data_rate = lq_sta->active_rate;
Z
Zhu Yi 已提交
686 687
	else {
		if (is_siso(rate_type))
688
			*data_rate = lq_sta->active_siso_rate;
Z
Zhu Yi 已提交
689
		else
690
			*data_rate = lq_sta->active_mimo_rate;
Z
Zhu Yi 已提交
691 692 693
	}

	if (hdr && is_multicast_ether_addr(hdr->addr1) &&
694 695 696
	    lq_sta->active_rate_basic) {
		*data_rate = lq_sta->active_rate_basic;
	}
Z
Zhu Yi 已提交
697 698 699 700 701 702 703
}

static u16 rs_get_adjacent_rate(u8 index, u16 rate_mask, int rate_type)
{
	u8 high = IWL_RATE_INVALID;
	u8 low = IWL_RATE_INVALID;

704
	/* 802.11A or ht walks to the next literal adjacent rate in
Z
Zhu Yi 已提交
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
	 * the rate table */
	if (is_a_band(rate_type) || !is_legacy(rate_type)) {
		int i;
		u32 mask;

		/* Find the previous rate that is in the rate mask */
		i = index - 1;
		for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
			if (rate_mask & mask) {
				low = i;
				break;
			}
		}

		/* Find the next rate that is in the rate mask */
		i = index + 1;
		for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
			if (rate_mask & mask) {
				high = i;
				break;
			}
		}

		return (high << 8) | low;
	}

	low = index;
	while (low != IWL_RATE_INVALID) {
C
Christoph Hellwig 已提交
733
		low = iwl4965_rates[low].prev_rs;
Z
Zhu Yi 已提交
734 735 736 737 738 739 740 741 742
		if (low == IWL_RATE_INVALID)
			break;
		if (rate_mask & (1 << low))
			break;
		IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low);
	}

	high = index;
	while (high != IWL_RATE_INVALID) {
C
Christoph Hellwig 已提交
743
		high = iwl4965_rates[high].next_rs;
Z
Zhu Yi 已提交
744 745 746 747 748 749 750 751 752 753
		if (high == IWL_RATE_INVALID)
			break;
		if (rate_mask & (1 << high))
			break;
		IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high);
	}

	return (high << 8) | low;
}

754
static void rs_get_lower_rate(struct iwl4965_lq_sta *lq_sta,
C
Christoph Hellwig 已提交
755 756
			     struct iwl4965_scale_tbl_info *tbl, u8 scale_index,
			     u8 ht_possible, struct iwl4965_rate *mcs_rate)
Z
Zhu Yi 已提交
757 758 759 760 761
{
	s32 low;
	u16 rate_mask;
	u16 high_low;
	u8 switch_to_legacy = 0;
762
	u8 is_green = lq_sta->is_green;
Z
Zhu Yi 已提交
763 764 765 766 767 768 769

	/* check if we need to switch from HT to legacy rates.
	 * assumption is that mandatory rates (1Mbps or 6Mbps)
	 * are always supported (spec demand) */
	if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
		switch_to_legacy = 1;
		scale_index = rs_ht_to_legacy[scale_index];
770
		if (lq_sta->band == IEEE80211_BAND_5GHZ)
Z
Zhu Yi 已提交
771 772 773 774 775 776 777 778 779 780 781 782
			tbl->lq_type = LQ_A;
		else
			tbl->lq_type = LQ_G;

		if ((tbl->antenna_type == ANT_BOTH) ||
		    (tbl->antenna_type == ANT_NONE))
			tbl->antenna_type = ANT_MAIN;

		tbl->is_fat = 0;
		tbl->is_SGI = 0;
	}

783
	rs_get_supported_rates(lq_sta, NULL, tbl->lq_type, &rate_mask);
Z
Zhu Yi 已提交
784

785
	/* Mask with station rate restriction */
Z
Zhu Yi 已提交
786
	if (is_legacy(tbl->lq_type)) {
787
		/* supp_rates has no CCK bits in A mode */
788
		if (lq_sta->band == IEEE80211_BAND_5GHZ)
Z
Zhu Yi 已提交
789
			rate_mask  = (u16)(rate_mask &
790
			   (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Z
Zhu Yi 已提交
791
		else
792
			rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
Z
Zhu Yi 已提交
793 794
	}

795
	/* If we switched from HT to legacy, check current rate */
796
	if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
Z
Zhu Yi 已提交
797
		rs_mcs_from_tbl(mcs_rate, tbl, scale_index, is_green);
798
		return;
Z
Zhu Yi 已提交
799 800 801 802 803 804 805 806 807 808 809
	}

	high_low = rs_get_adjacent_rate(scale_index, rate_mask, tbl->lq_type);
	low = high_low & 0xff;

	if (low != IWL_RATE_INVALID)
		rs_mcs_from_tbl(mcs_rate, tbl, low, is_green);
	else
		rs_mcs_from_tbl(mcs_rate, tbl, scale_index, is_green);
}

810 811 812
/*
 * mac80211 sends us Tx status
 */
813
static void rs_tx_status(void *priv_rate, struct net_device *dev,
Z
Zhu Yi 已提交
814 815 816 817 818 819
			 struct sk_buff *skb,
			 struct ieee80211_tx_status *tx_resp)
{
	int status;
	u8 retries;
	int rs_index, index = 0;
820
	struct iwl4965_lq_sta *lq_sta;
C
Christoph Hellwig 已提交
821
	struct iwl4965_link_quality_cmd *table;
Z
Zhu Yi 已提交
822 823
	struct sta_info *sta;
	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
C
Christoph Hellwig 已提交
824
	struct iwl4965_priv *priv = (struct iwl4965_priv *)priv_rate;
Z
Zhu Yi 已提交
825
	struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
826
	struct ieee80211_hw *hw = local_to_hw(local);
C
Christoph Hellwig 已提交
827 828 829 830 831
	struct iwl4965_rate_scale_data *window = NULL;
	struct iwl4965_rate_scale_data *search_win = NULL;
	struct iwl4965_rate tx_mcs;
	struct iwl4965_scale_tbl_info tbl_type;
	struct iwl4965_scale_tbl_info *curr_tbl, *search_tbl;
Z
Zhu Yi 已提交
832 833 834 835
	u8 active_index = 0;
	u16 fc = le16_to_cpu(hdr->frame_control);
	s32 tpt = 0;

836
	IWL_DEBUG_RATE_LIMIT("get frame ack response, update rate scale window\n");
Z
Zhu Yi 已提交
837 838 839 840

	if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1))
		return;

841 842 843 844 845
	/* This packet was aggregated but doesn't carry rate scale info */
	if ((tx_resp->control.flags & IEEE80211_TXCTL_AMPDU) &&
	    !(tx_resp->flags & IEEE80211_TX_STATUS_AMPDU))
		return;

Z
Zhu Yi 已提交
846 847 848 849 850
	retries = tx_resp->retry_count;

	if (retries > 15)
		retries = 15;

851
	rcu_read_lock();
Z
Zhu Yi 已提交
852 853 854

	sta = sta_info_get(local, hdr->addr1);

855 856 857
	if (!sta || !sta->rate_ctrl_priv)
		goto out;

Z
Zhu Yi 已提交
858

859
	lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
Z
Zhu Yi 已提交
860 861

	if (!priv->lq_mngr.lq_ready)
862
		goto out;
Z
Zhu Yi 已提交
863

864 865
	if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
	    !lq_sta->ibss_sta_added)
866
		goto out;
Z
Zhu Yi 已提交
867

868 869
	table = &lq_sta->lq;
	active_index = lq_sta->active_tbl;
Z
Zhu Yi 已提交
870

871
	/* Get mac80211 antenna info */
872 873 874 875
	lq_sta->antenna =
		(lq_sta->valid_antenna & local->hw.conf.antenna_sel_tx);
	if (!lq_sta->antenna)
		lq_sta->antenna = lq_sta->valid_antenna;
Z
Zhu Yi 已提交
876

877
	/* Ignore mac80211 antenna info for now */
878
	lq_sta->antenna = lq_sta->valid_antenna;
879

880 881
	curr_tbl = &(lq_sta->lq_info[active_index]);
	search_tbl = &(lq_sta->lq_info[(1 - active_index)]);
C
Christoph Hellwig 已提交
882
	window = (struct iwl4965_rate_scale_data *)
Z
Zhu Yi 已提交
883
	    &(curr_tbl->win[0]);
C
Christoph Hellwig 已提交
884
	search_win = (struct iwl4965_rate_scale_data *)
Z
Zhu Yi 已提交
885 886
	    &(search_tbl->win[0]);

887 888 889 890 891 892 893 894
	/*
	 * Ignore this Tx frame response if its initial rate doesn't match
	 * that of latest Link Quality command.  There may be stragglers
	 * from a previous Link Quality command, but we're no longer interested
	 * in those; they're either from the "active" mode while we're trying
	 * to check "search" mode, or a prior "search" mode after we've moved
	 * to a new "search" mode (which might become the new "active" mode).
	 */
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
	tx_mcs.rate_n_flags = le32_to_cpu(table->rs_table[0].rate_n_flags);
	rs_get_tbl_info_from_mcs(&tx_mcs, priv->band, &tbl_type, &rs_index);
	if (priv->band == IEEE80211_BAND_5GHZ)
		rs_index -= IWL_FIRST_OFDM_RATE;

	if ((tx_resp->control.tx_rate == NULL) ||
	    (tbl_type.is_SGI ^
		!!(tx_resp->control.flags & IEEE80211_TXCTL_SHORT_GI)) ||
	    (tbl_type.is_fat ^
		!!(tx_resp->control.flags & IEEE80211_TXCTL_40_MHZ_WIDTH)) ||
	    (tbl_type.is_dup ^
		!!(tx_resp->control.flags & IEEE80211_TXCTL_DUP_DATA)) ||
	    (tbl_type.antenna_type ^
		tx_resp->control.antenna_sel_tx) ||
	    (!!(tx_mcs.rate_n_flags & RATE_MCS_HT_MSK) ^
		!!(tx_resp->control.flags & IEEE80211_TXCTL_OFDM_HT)) ||
	    (!!(tx_mcs.rate_n_flags & RATE_MCS_GF_MSK) ^
		!!(tx_resp->control.flags & IEEE80211_TXCTL_GREEN_FIELD)) ||
	    (hw->wiphy->bands[priv->band]->bitrates[rs_index].bitrate !=
		tx_resp->control.tx_rate->bitrate)) {
		IWL_DEBUG_RATE("initial rate does not match 0x%x\n",
				tx_mcs.rate_n_flags);
917
		goto out;
Z
Zhu Yi 已提交
918 919
	}

920
	/* Update frame history window with "failure" for each Tx retry. */
Z
Zhu Yi 已提交
921
	while (retries) {
922 923
		/* Look up the rate and other info used for each tx attempt.
		 * Each tx attempt steps one entry deeper in the rate table. */
Z
Zhu Yi 已提交
924 925
		tx_mcs.rate_n_flags =
		    le32_to_cpu(table->rs_table[index].rate_n_flags);
926
		rs_get_tbl_info_from_mcs(&tx_mcs, priv->band,
Z
Zhu Yi 已提交
927 928
					  &tbl_type, &rs_index);

929 930
		/* If type matches "search" table,
		 * add failure to "search" history */
Z
Zhu Yi 已提交
931 932 933 934 935 936 937
		if ((tbl_type.lq_type == search_tbl->lq_type) &&
		    (tbl_type.antenna_type == search_tbl->antenna_type) &&
		    (tbl_type.is_SGI == search_tbl->is_SGI)) {
			if (search_tbl->expected_tpt)
				tpt = search_tbl->expected_tpt[rs_index];
			else
				tpt = 0;
938
			rs_collect_tx_data(search_win, rs_index, tpt, 1, 0);
939 940 941

		/* Else if type matches "current/active" table,
		 * add failure to "current/active" history */
Z
Zhu Yi 已提交
942 943 944 945 946 947 948
		} else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
			   (tbl_type.antenna_type == curr_tbl->antenna_type) &&
			   (tbl_type.is_SGI == curr_tbl->is_SGI)) {
			if (curr_tbl->expected_tpt)
				tpt = curr_tbl->expected_tpt[rs_index];
			else
				tpt = 0;
949
			rs_collect_tx_data(window, rs_index, tpt, 1, 0);
Z
Zhu Yi 已提交
950
		}
951 952 953

		/* If not searching for a new mode, increment failed counter
		 * ... this helps determine when to start searching again */
954 955
		if (lq_sta->stay_in_tbl)
			lq_sta->total_failed++;
Z
Zhu Yi 已提交
956 957 958 959 960
		--retries;
		index++;

	}

961 962 963 964 965
	/*
	 * Find (by rate) the history window to update with final Tx attempt;
	 * if Tx was successful first try, use original rate,
	 * else look up the rate that was, finally, successful.
	 */
966 967
	tx_mcs.rate_n_flags = le32_to_cpu(table->rs_table[index].rate_n_flags);
	rs_get_tbl_info_from_mcs(&tx_mcs, priv->band, &tbl_type, &rs_index);
Z
Zhu Yi 已提交
968

969
	/* Update frame history window with "success" if Tx got ACKed ... */
Z
Zhu Yi 已提交
970 971 972 973 974
	if (tx_resp->flags & IEEE80211_TX_STATUS_ACK)
		status = 1;
	else
		status = 0;

975 976
	/* If type matches "search" table,
	 * add final tx status to "search" history */
Z
Zhu Yi 已提交
977 978 979 980 981 982 983
	if ((tbl_type.lq_type == search_tbl->lq_type) &&
	    (tbl_type.antenna_type == search_tbl->antenna_type) &&
	    (tbl_type.is_SGI == search_tbl->is_SGI)) {
		if (search_tbl->expected_tpt)
			tpt = search_tbl->expected_tpt[rs_index];
		else
			tpt = 0;
984 985 986 987 988 989 990
		if (tx_resp->control.flags & IEEE80211_TXCTL_AMPDU)
			rs_collect_tx_data(search_win, rs_index, tpt,
					   tx_resp->ampdu_ack_len,
					   tx_resp->ampdu_ack_map);
		else
			rs_collect_tx_data(search_win, rs_index, tpt,
					   1, status);
991 992
	/* Else if type matches "current/active" table,
	 * add final tx status to "current/active" history */
Z
Zhu Yi 已提交
993 994 995 996 997 998 999
	} else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
		   (tbl_type.antenna_type == curr_tbl->antenna_type) &&
		   (tbl_type.is_SGI == curr_tbl->is_SGI)) {
		if (curr_tbl->expected_tpt)
			tpt = curr_tbl->expected_tpt[rs_index];
		else
			tpt = 0;
1000 1001 1002 1003 1004 1005 1006
		if (tx_resp->control.flags & IEEE80211_TXCTL_AMPDU)
			rs_collect_tx_data(window, rs_index, tpt,
					   tx_resp->ampdu_ack_len,
					   tx_resp->ampdu_ack_map);
		else
			rs_collect_tx_data(window, rs_index, tpt,
					   1, status);
Z
Zhu Yi 已提交
1007 1008
	}

1009 1010
	/* If not searching for new mode, increment success/failed counter
	 * ... these help determine when to start searching again */
1011
	if (lq_sta->stay_in_tbl) {
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
		if (tx_resp->control.flags & IEEE80211_TXCTL_AMPDU) {
			lq_sta->total_success += tx_resp->ampdu_ack_map;
			lq_sta->total_failed +=
			     (tx_resp->ampdu_ack_len - tx_resp->ampdu_ack_map);
		} else {
			if (status)
				lq_sta->total_success++;
			else
				lq_sta->total_failed++;
		}
Z
Zhu Yi 已提交
1022 1023
	}

1024
	/* See if there's a better rate or modulation mode to try. */
Z
Zhu Yi 已提交
1025
	rs_rate_scale_perform(priv, dev, hdr, sta);
1026
out:
1027
	rcu_read_unlock();
Z
Zhu Yi 已提交
1028 1029 1030 1031
	return;
}

static u8 rs_is_ant_connected(u8 valid_antenna,
C
Christoph Hellwig 已提交
1032
			      enum iwl4965_antenna_type antenna_type)
Z
Zhu Yi 已提交
1033 1034 1035 1036 1037
{
	if (antenna_type == ANT_AUX)
		return ((valid_antenna & 0x2) ? 1:0);
	else if (antenna_type == ANT_MAIN)
		return ((valid_antenna & 0x1) ? 1:0);
1038 1039
	else if (antenna_type == ANT_BOTH)
		return ((valid_antenna & 0x3) == 0x3);
Z
Zhu Yi 已提交
1040 1041 1042 1043 1044

	return 1;
}

static u8 rs_is_other_ant_connected(u8 valid_antenna,
C
Christoph Hellwig 已提交
1045
				    enum iwl4965_antenna_type antenna_type)
Z
Zhu Yi 已提交
1046 1047
{
	if (antenna_type == ANT_AUX)
1048
		return rs_is_ant_connected(valid_antenna, ANT_MAIN);
Z
Zhu Yi 已提交
1049
	else
1050
		return rs_is_ant_connected(valid_antenna, ANT_AUX);
Z
Zhu Yi 已提交
1051 1052 1053 1054

	return 0;
}

1055 1056 1057 1058 1059 1060 1061 1062
/*
 * Begin a period of staying with a selected modulation mode.
 * Set "stay_in_tbl" flag to prevent any mode switches.
 * Set frame tx success limits according to legacy vs. high-throughput,
 * and reset overall (spanning all rates) tx success history statistics.
 * These control how long we stay using same modulation mode before
 * searching for a new mode.
 */
Z
Zhu Yi 已提交
1063
static void rs_set_stay_in_table(u8 is_legacy,
1064
				 struct iwl4965_lq_sta *lq_sta)
Z
Zhu Yi 已提交
1065 1066
{
	IWL_DEBUG_HT("we are staying in the same table\n");
1067
	lq_sta->stay_in_tbl = 1;	/* only place this gets set */
Z
Zhu Yi 已提交
1068
	if (is_legacy) {
1069 1070 1071
		lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
		lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
		lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
Z
Zhu Yi 已提交
1072
	} else {
1073 1074 1075
		lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
		lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
		lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
Z
Zhu Yi 已提交
1076
	}
1077 1078 1079
	lq_sta->table_count = 0;
	lq_sta->total_failed = 0;
	lq_sta->total_success = 0;
Z
Zhu Yi 已提交
1080 1081
}

1082 1083 1084
/*
 * Find correct throughput table for given mode of modulation
 */
1085
static void rs_get_expected_tpt_table(struct iwl4965_lq_sta *lq_sta,
C
Christoph Hellwig 已提交
1086
				      struct iwl4965_scale_tbl_info *tbl)
Z
Zhu Yi 已提交
1087 1088 1089 1090 1091 1092 1093
{
	if (is_legacy(tbl->lq_type)) {
		if (!is_a_band(tbl->lq_type))
			tbl->expected_tpt = expected_tpt_G;
		else
			tbl->expected_tpt = expected_tpt_A;
	} else if (is_siso(tbl->lq_type)) {
1094
		if (tbl->is_fat && !lq_sta->is_dup)
Z
Zhu Yi 已提交
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
			if (tbl->is_SGI)
				tbl->expected_tpt = expected_tpt_siso40MHzSGI;
			else
				tbl->expected_tpt = expected_tpt_siso40MHz;
		else if (tbl->is_SGI)
			tbl->expected_tpt = expected_tpt_siso20MHzSGI;
		else
			tbl->expected_tpt = expected_tpt_siso20MHz;

	} else if (is_mimo(tbl->lq_type)) {
1105
		if (tbl->is_fat && !lq_sta->is_dup)
Z
Zhu Yi 已提交
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
			if (tbl->is_SGI)
				tbl->expected_tpt = expected_tpt_mimo40MHzSGI;
			else
				tbl->expected_tpt = expected_tpt_mimo40MHz;
		else if (tbl->is_SGI)
			tbl->expected_tpt = expected_tpt_mimo20MHzSGI;
		else
			tbl->expected_tpt = expected_tpt_mimo20MHz;
	} else
		tbl->expected_tpt = expected_tpt_G;
}

1118
#ifdef CONFIG_IWL4965_HT
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
/*
 * Find starting rate for new "search" high-throughput mode of modulation.
 * Goal is to find lowest expected rate (under perfect conditions) that is
 * above the current measured throughput of "active" mode, to give new mode
 * a fair chance to prove itself without too many challenges.
 *
 * This gets called when transitioning to more aggressive modulation
 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
 * (i.e. MIMO to SISO).  When moving to MIMO, bit rate will typically need
 * to decrease to match "active" throughput.  When moving from MIMO to SISO,
 * bit rate will typically need to increase, but not if performance was bad.
 */
C
Christoph Hellwig 已提交
1131
static s32 rs_get_best_rate(struct iwl4965_priv *priv,
1132
			    struct iwl4965_lq_sta *lq_sta,
1133
			    struct iwl4965_scale_tbl_info *tbl,	/* "search" */
Z
Zhu Yi 已提交
1134 1135
			    u16 rate_mask, s8 index, s8 rate)
{
1136
	/* "active" values */
C
Christoph Hellwig 已提交
1137
	struct iwl4965_scale_tbl_info *active_tbl =
1138
	    &(lq_sta->lq_info[lq_sta->active_tbl]);
Z
Zhu Yi 已提交
1139 1140
	s32 active_sr = active_tbl->win[index].success_ratio;
	s32 active_tpt = active_tbl->expected_tpt[index];
1141 1142 1143 1144 1145

	/* expected "search" throughput */
	s32 *tpt_tbl = tbl->expected_tpt;

	s32 new_rate, high, low, start_hi;
Z
Zhu Yi 已提交
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
	u16 high_low;

	new_rate = high = low = start_hi = IWL_RATE_INVALID;

	for (; ;) {
		high_low = rs_get_adjacent_rate(rate, rate_mask, tbl->lq_type);

		low = high_low & 0xff;
		high = (high_low >> 8) & 0xff;

1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
		/*
		 * Lower the "search" bit rate, to give new "search" mode
		 * approximately the same throughput as "active" if:
		 *
		 * 1) "Active" mode has been working modestly well (but not
		 *    great), and expected "search" throughput (under perfect
		 *    conditions) at candidate rate is above the actual
		 *    measured "active" throughput (but less than expected
		 *    "active" throughput under perfect conditions).
		 * OR
		 * 2) "Active" mode has been working perfectly or very well
		 *    and expected "search" throughput (under perfect
		 *    conditions) at candidate rate is above expected
		 *    "active" throughput (under perfect conditions).
		 */
1171
		if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
Z
Zhu Yi 已提交
1172 1173 1174 1175 1176 1177
		     ((active_sr > IWL_RATE_DECREASE_TH) &&
		      (active_sr <= IWL_RATE_HIGH_TH) &&
		      (tpt_tbl[rate] <= active_tpt))) ||
		    ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
		     (tpt_tbl[rate] > active_tpt))) {

1178 1179 1180
			/* (2nd or later pass)
			 * If we've already tried to raise the rate, and are
			 * now trying to lower it, use the higher rate. */
Z
Zhu Yi 已提交
1181 1182 1183 1184
			if (start_hi != IWL_RATE_INVALID) {
				new_rate = start_hi;
				break;
			}
1185

Z
Zhu Yi 已提交
1186
			new_rate = rate;
1187 1188

			/* Loop again with lower rate */
Z
Zhu Yi 已提交
1189 1190
			if (low != IWL_RATE_INVALID)
				rate = low;
1191 1192

			/* Lower rate not available, use the original */
Z
Zhu Yi 已提交
1193 1194
			else
				break;
1195 1196

		/* Else try to raise the "search" rate to match "active" */
Z
Zhu Yi 已提交
1197
		} else {
1198 1199 1200
			/* (2nd or later pass)
			 * If we've already tried to lower the rate, and are
			 * now trying to raise it, use the lower rate. */
Z
Zhu Yi 已提交
1201 1202
			if (new_rate != IWL_RATE_INVALID)
				break;
1203 1204

			/* Loop again with higher rate */
Z
Zhu Yi 已提交
1205 1206 1207
			else if (high != IWL_RATE_INVALID) {
				start_hi = high;
				rate = high;
1208 1209

			/* Higher rate not available, use the original */
Z
Zhu Yi 已提交
1210 1211 1212 1213 1214 1215 1216 1217 1218
			} else {
				new_rate = rate;
				break;
			}
		}
	}

	return new_rate;
}
1219
#endif				/* CONFIG_IWL4965_HT */
Z
Zhu Yi 已提交
1220 1221 1222 1223 1224 1225

static inline u8 rs_is_both_ant_supp(u8 valid_antenna)
{
	return (rs_is_ant_connected(valid_antenna, ANT_BOTH));
}

1226 1227 1228
/*
 * Set up search table for MIMO
 */
C
Christoph Hellwig 已提交
1229
static int rs_switch_to_mimo(struct iwl4965_priv *priv,
1230
			     struct iwl4965_lq_sta *lq_sta,
1231 1232
			     struct ieee80211_conf *conf,
			     struct sta_info *sta,
C
Christoph Hellwig 已提交
1233
			     struct iwl4965_scale_tbl_info *tbl, int index)
Z
Zhu Yi 已提交
1234
{
1235
#ifdef CONFIG_IWL4965_HT
Z
Zhu Yi 已提交
1236 1237
	u16 rate_mask;
	s32 rate;
1238
	s8 is_green = lq_sta->is_green;
Z
Zhu Yi 已提交
1239

1240 1241
	if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) ||
	    !sta->ht_info.ht_supported)
Z
Zhu Yi 已提交
1242 1243 1244 1245
		return -1;

	IWL_DEBUG_HT("LQ: try to switch to MIMO\n");
	tbl->lq_type = LQ_MIMO;
1246
	rs_get_supported_rates(lq_sta, NULL, tbl->lq_type,
Z
Zhu Yi 已提交
1247 1248
				&rate_mask);

1249
	if (priv->current_ht_config.tx_mimo_ps_mode == IWL_MIMO_PS_STATIC)
Z
Zhu Yi 已提交
1250 1251
		return -1;

1252
	/* Need both Tx chains/antennas to support MIMO */
1253
	if (!rs_is_both_ant_supp(lq_sta->antenna))
Z
Zhu Yi 已提交
1254 1255
		return -1;

1256
	tbl->is_dup = lq_sta->is_dup;
Z
Zhu Yi 已提交
1257
	tbl->action = 0;
1258 1259
	if (priv->current_ht_config.supported_chan_width
	    == IWL_CHANNEL_WIDTH_40MHZ)
Z
Zhu Yi 已提交
1260 1261 1262 1263 1264
		tbl->is_fat = 1;
	else
		tbl->is_fat = 0;

	if (tbl->is_fat) {
1265
		if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
Z
Zhu Yi 已提交
1266 1267 1268
			tbl->is_SGI = 1;
		else
			tbl->is_SGI = 0;
1269
	} else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
Z
Zhu Yi 已提交
1270 1271 1272 1273
		tbl->is_SGI = 1;
	else
		tbl->is_SGI = 0;

1274
	rs_get_expected_tpt_table(lq_sta, tbl);
Z
Zhu Yi 已提交
1275

1276
	rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index, index);
Z
Zhu Yi 已提交
1277 1278 1279 1280 1281 1282 1283 1284

	IWL_DEBUG_HT("LQ: MIMO best rate %d mask %X\n", rate, rate_mask);
	if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask))
		return -1;
	rs_mcs_from_tbl(&tbl->current_rate, tbl, rate, is_green);

	IWL_DEBUG_HT("LQ: Switch to new mcs %X index is green %X\n",
		     tbl->current_rate.rate_n_flags, is_green);
1285
	return 0;
1286 1287
#else
	return -1;
1288
#endif	/*CONFIG_IWL4965_HT */
Z
Zhu Yi 已提交
1289 1290
}

1291 1292 1293
/*
 * Set up search table for SISO
 */
C
Christoph Hellwig 已提交
1294
static int rs_switch_to_siso(struct iwl4965_priv *priv,
1295
			     struct iwl4965_lq_sta *lq_sta,
1296 1297
			     struct ieee80211_conf *conf,
			     struct sta_info *sta,
C
Christoph Hellwig 已提交
1298
			     struct iwl4965_scale_tbl_info *tbl, int index)
Z
Zhu Yi 已提交
1299
{
1300
#ifdef CONFIG_IWL4965_HT
Z
Zhu Yi 已提交
1301
	u16 rate_mask;
1302
	u8 is_green = lq_sta->is_green;
Z
Zhu Yi 已提交
1303 1304 1305
	s32 rate;

	IWL_DEBUG_HT("LQ: try to switch to SISO\n");
1306 1307
	if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) ||
	    !sta->ht_info.ht_supported)
Z
Zhu Yi 已提交
1308 1309
		return -1;

1310
	tbl->is_dup = lq_sta->is_dup;
Z
Zhu Yi 已提交
1311 1312
	tbl->lq_type = LQ_SISO;
	tbl->action = 0;
1313
	rs_get_supported_rates(lq_sta, NULL, tbl->lq_type,
Z
Zhu Yi 已提交
1314 1315
				&rate_mask);

1316 1317
	if (priv->current_ht_config.supported_chan_width
	    == IWL_CHANNEL_WIDTH_40MHZ)
Z
Zhu Yi 已提交
1318 1319 1320 1321 1322
		tbl->is_fat = 1;
	else
		tbl->is_fat = 0;

	if (tbl->is_fat) {
1323
		if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
Z
Zhu Yi 已提交
1324 1325 1326
			tbl->is_SGI = 1;
		else
			tbl->is_SGI = 0;
1327
	} else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
Z
Zhu Yi 已提交
1328 1329 1330 1331 1332 1333 1334
		tbl->is_SGI = 1;
	else
		tbl->is_SGI = 0;

	if (is_green)
		tbl->is_SGI = 0;

1335 1336
	rs_get_expected_tpt_table(lq_sta, tbl);
	rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index, index);
Z
Zhu Yi 已提交
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346

	IWL_DEBUG_HT("LQ: get best rate %d mask %X\n", rate, rate_mask);
	if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
		IWL_DEBUG_HT("can not switch with index %d rate mask %x\n",
			     rate, rate_mask);
		return -1;
	}
	rs_mcs_from_tbl(&tbl->current_rate, tbl, rate, is_green);
	IWL_DEBUG_HT("LQ: Switch to new mcs %X index is green %X\n",
		     tbl->current_rate.rate_n_flags, is_green);
1347 1348 1349
	return 0;
#else
	return -1;
Z
Zhu Yi 已提交
1350

1351
#endif	/*CONFIG_IWL4965_HT */
Z
Zhu Yi 已提交
1352 1353
}

1354 1355 1356
/*
 * Try to switch to new modulation mode from legacy
 */
C
Christoph Hellwig 已提交
1357
static int rs_move_legacy_other(struct iwl4965_priv *priv,
1358
				struct iwl4965_lq_sta *lq_sta,
1359 1360
				struct ieee80211_conf *conf,
				struct sta_info *sta,
Z
Zhu Yi 已提交
1361 1362
				int index)
{
1363
	int ret = 0;
C
Christoph Hellwig 已提交
1364
	struct iwl4965_scale_tbl_info *tbl =
1365
	    &(lq_sta->lq_info[lq_sta->active_tbl]);
C
Christoph Hellwig 已提交
1366
	struct iwl4965_scale_tbl_info *search_tbl =
1367
	    &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
C
Christoph Hellwig 已提交
1368 1369 1370
	struct iwl4965_rate_scale_data *window = &(tbl->win[index]);
	u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
		  (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
Z
Zhu Yi 已提交
1371 1372 1373 1374 1375 1376 1377 1378
	u8 start_action = tbl->action;

	for (; ;) {
		switch (tbl->action) {
		case IWL_LEGACY_SWITCH_ANTENNA:
			IWL_DEBUG_HT("LQ Legacy switch Antenna\n");

			search_tbl->lq_type = LQ_NONE;
1379
			lq_sta->action_counter++;
1380 1381

			/* Don't change antenna if success has been great */
Z
Zhu Yi 已提交
1382 1383
			if (window->success_ratio >= IWL_RS_GOOD_RATIO)
				break;
1384 1385

			/* Don't change antenna if other one is not connected */
1386
			if (!rs_is_other_ant_connected(lq_sta->antenna,
Z
Zhu Yi 已提交
1387 1388 1389
							tbl->antenna_type))
				break;

1390
			/* Set up search table to try other antenna */
Z
Zhu Yi 已提交
1391 1392 1393 1394
			memcpy(search_tbl, tbl, sz);

			rs_toggle_antenna(&(search_tbl->current_rate),
					   search_tbl);
1395 1396
			rs_get_expected_tpt_table(lq_sta, search_tbl);
			lq_sta->search_better_tbl = 1;
Z
Zhu Yi 已提交
1397 1398 1399 1400
			goto out;

		case IWL_LEGACY_SWITCH_SISO:
			IWL_DEBUG_HT("LQ: Legacy switch to SISO\n");
1401 1402

			/* Set up search table to try SISO */
Z
Zhu Yi 已提交
1403 1404 1405 1406
			memcpy(search_tbl, tbl, sz);
			search_tbl->lq_type = LQ_SISO;
			search_tbl->is_SGI = 0;
			search_tbl->is_fat = 0;
1407
			ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1408
						 search_tbl, index);
1409
			if (!ret) {
1410 1411
				lq_sta->search_better_tbl = 1;
				lq_sta->action_counter = 0;
Z
Zhu Yi 已提交
1412
				goto out;
1413
			}
Z
Zhu Yi 已提交
1414 1415 1416 1417

			break;
		case IWL_LEGACY_SWITCH_MIMO:
			IWL_DEBUG_HT("LQ: Legacy switch MIMO\n");
1418 1419

			/* Set up search table to try MIMO */
Z
Zhu Yi 已提交
1420 1421 1422 1423 1424
			memcpy(search_tbl, tbl, sz);
			search_tbl->lq_type = LQ_MIMO;
			search_tbl->is_SGI = 0;
			search_tbl->is_fat = 0;
			search_tbl->antenna_type = ANT_BOTH;
1425
			ret = rs_switch_to_mimo(priv, lq_sta, conf, sta,
1426
						 search_tbl, index);
1427
			if (!ret) {
1428 1429
				lq_sta->search_better_tbl = 1;
				lq_sta->action_counter = 0;
Z
Zhu Yi 已提交
1430
				goto out;
1431
			}
Z
Zhu Yi 已提交
1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
			break;
		}
		tbl->action++;
		if (tbl->action > IWL_LEGACY_SWITCH_MIMO)
			tbl->action = IWL_LEGACY_SWITCH_ANTENNA;

		if (tbl->action == start_action)
			break;

	}
	return 0;

 out:
	tbl->action++;
	if (tbl->action > IWL_LEGACY_SWITCH_MIMO)
		tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
	return 0;

}

1452 1453 1454
/*
 * Try to switch to new modulation mode from SISO
 */
C
Christoph Hellwig 已提交
1455
static int rs_move_siso_to_other(struct iwl4965_priv *priv,
1456
				 struct iwl4965_lq_sta *lq_sta,
1457 1458
				 struct ieee80211_conf *conf,
				 struct sta_info *sta,
Z
Zhu Yi 已提交
1459 1460
				 int index)
{
1461
	int ret;
1462
	u8 is_green = lq_sta->is_green;
C
Christoph Hellwig 已提交
1463
	struct iwl4965_scale_tbl_info *tbl =
1464
	    &(lq_sta->lq_info[lq_sta->active_tbl]);
C
Christoph Hellwig 已提交
1465
	struct iwl4965_scale_tbl_info *search_tbl =
1466
	    &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
C
Christoph Hellwig 已提交
1467 1468 1469
	struct iwl4965_rate_scale_data *window = &(tbl->win[index]);
	u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
		  (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
Z
Zhu Yi 已提交
1470 1471 1472
	u8 start_action = tbl->action;

	for (;;) {
1473
		lq_sta->action_counter++;
Z
Zhu Yi 已提交
1474 1475 1476 1477 1478 1479
		switch (tbl->action) {
		case IWL_SISO_SWITCH_ANTENNA:
			IWL_DEBUG_HT("LQ: SISO SWITCH ANTENNA SISO\n");
			search_tbl->lq_type = LQ_NONE;
			if (window->success_ratio >= IWL_RS_GOOD_RATIO)
				break;
1480
			if (!rs_is_other_ant_connected(lq_sta->antenna,
Z
Zhu Yi 已提交
1481 1482 1483 1484 1485 1486 1487
						       tbl->antenna_type))
				break;

			memcpy(search_tbl, tbl, sz);
			search_tbl->action = IWL_SISO_SWITCH_MIMO;
			rs_toggle_antenna(&(search_tbl->current_rate),
					   search_tbl);
1488
			lq_sta->search_better_tbl = 1;
Z
Zhu Yi 已提交
1489 1490 1491 1492 1493 1494 1495 1496 1497 1498

			goto out;

		case IWL_SISO_SWITCH_MIMO:
			IWL_DEBUG_HT("LQ: SISO SWITCH TO MIMO FROM SISO\n");
			memcpy(search_tbl, tbl, sz);
			search_tbl->lq_type = LQ_MIMO;
			search_tbl->is_SGI = 0;
			search_tbl->is_fat = 0;
			search_tbl->antenna_type = ANT_BOTH;
1499
			ret = rs_switch_to_mimo(priv, lq_sta, conf, sta,
1500
						 search_tbl, index);
1501
			if (!ret) {
1502
				lq_sta->search_better_tbl = 1;
Z
Zhu Yi 已提交
1503
				goto out;
1504
			}
Z
Zhu Yi 已提交
1505 1506 1507
			break;
		case IWL_SISO_SWITCH_GI:
			IWL_DEBUG_HT("LQ: SISO SWITCH TO GI\n");
1508

Z
Zhu Yi 已提交
1509 1510 1511 1512 1513 1514 1515 1516
			memcpy(search_tbl, tbl, sz);
			search_tbl->action = 0;
			if (search_tbl->is_SGI)
				search_tbl->is_SGI = 0;
			else if (!is_green)
				search_tbl->is_SGI = 1;
			else
				break;
1517
			lq_sta->search_better_tbl = 1;
Z
Zhu Yi 已提交
1518 1519
			if ((tbl->lq_type == LQ_SISO) &&
			    (tbl->is_SGI)) {
1520
				s32 tpt = lq_sta->last_tpt / 100;
Z
Zhu Yi 已提交
1521 1522 1523 1524
				if (((!tbl->is_fat) &&
				     (tpt >= expected_tpt_siso20MHz[index])) ||
				    ((tbl->is_fat) &&
				     (tpt >= expected_tpt_siso40MHz[index])))
1525
					lq_sta->search_better_tbl = 0;
Z
Zhu Yi 已提交
1526
			}
1527
			rs_get_expected_tpt_table(lq_sta, search_tbl);
Z
Zhu Yi 已提交
1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547
			rs_mcs_from_tbl(&search_tbl->current_rate,
					     search_tbl, index, is_green);
			goto out;
		}
		tbl->action++;
		if (tbl->action > IWL_SISO_SWITCH_GI)
			tbl->action = IWL_SISO_SWITCH_ANTENNA;

		if (tbl->action == start_action)
			break;
	}
	return 0;

 out:
	tbl->action++;
	if (tbl->action > IWL_SISO_SWITCH_GI)
		tbl->action = IWL_SISO_SWITCH_ANTENNA;
	return 0;
}

1548 1549 1550
/*
 * Try to switch to new modulation mode from MIMO
 */
C
Christoph Hellwig 已提交
1551
static int rs_move_mimo_to_other(struct iwl4965_priv *priv,
1552
				 struct iwl4965_lq_sta *lq_sta,
1553 1554
				 struct ieee80211_conf *conf,
				 struct sta_info *sta,
Z
Zhu Yi 已提交
1555 1556
				 int index)
{
1557
	int ret;
1558
	s8 is_green = lq_sta->is_green;
C
Christoph Hellwig 已提交
1559
	struct iwl4965_scale_tbl_info *tbl =
1560
	    &(lq_sta->lq_info[lq_sta->active_tbl]);
C
Christoph Hellwig 已提交
1561
	struct iwl4965_scale_tbl_info *search_tbl =
1562
	    &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
C
Christoph Hellwig 已提交
1563 1564
	u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
		  (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
Z
Zhu Yi 已提交
1565 1566 1567
	u8 start_action = tbl->action;

	for (;;) {
1568
		lq_sta->action_counter++;
Z
Zhu Yi 已提交
1569 1570 1571 1572
		switch (tbl->action) {
		case IWL_MIMO_SWITCH_ANTENNA_A:
		case IWL_MIMO_SWITCH_ANTENNA_B:
			IWL_DEBUG_HT("LQ: MIMO SWITCH TO SISO\n");
1573

1574

1575
			/* Set up new search table for SISO */
Z
Zhu Yi 已提交
1576 1577 1578 1579 1580 1581 1582 1583 1584
			memcpy(search_tbl, tbl, sz);
			search_tbl->lq_type = LQ_SISO;
			search_tbl->is_SGI = 0;
			search_tbl->is_fat = 0;
			if (tbl->action == IWL_MIMO_SWITCH_ANTENNA_A)
				search_tbl->antenna_type = ANT_MAIN;
			else
				search_tbl->antenna_type = ANT_AUX;

1585
			ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1586
						 search_tbl, index);
1587
			if (!ret) {
1588
				lq_sta->search_better_tbl = 1;
Z
Zhu Yi 已提交
1589 1590 1591 1592 1593 1594
				goto out;
			}
			break;

		case IWL_MIMO_SWITCH_GI:
			IWL_DEBUG_HT("LQ: MIMO SWITCH TO GI\n");
1595 1596

			/* Set up new search table for MIMO */
Z
Zhu Yi 已提交
1597 1598 1599 1600 1601 1602 1603 1604
			memcpy(search_tbl, tbl, sz);
			search_tbl->lq_type = LQ_MIMO;
			search_tbl->antenna_type = ANT_BOTH;
			search_tbl->action = 0;
			if (search_tbl->is_SGI)
				search_tbl->is_SGI = 0;
			else
				search_tbl->is_SGI = 1;
1605
			lq_sta->search_better_tbl = 1;
1606 1607 1608 1609 1610 1611 1612

			/*
			 * If active table already uses the fastest possible
			 * modulation (dual stream with short guard interval),
			 * and it's working well, there's no need to look
			 * for a better type of modulation!
			 */
Z
Zhu Yi 已提交
1613 1614
			if ((tbl->lq_type == LQ_MIMO) &&
			    (tbl->is_SGI)) {
1615
				s32 tpt = lq_sta->last_tpt / 100;
Z
Zhu Yi 已提交
1616 1617 1618 1619
				if (((!tbl->is_fat) &&
				     (tpt >= expected_tpt_mimo20MHz[index])) ||
				    ((tbl->is_fat) &&
				     (tpt >= expected_tpt_mimo40MHz[index])))
1620
					lq_sta->search_better_tbl = 0;
Z
Zhu Yi 已提交
1621
			}
1622
			rs_get_expected_tpt_table(lq_sta, search_tbl);
Z
Zhu Yi 已提交
1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644
			rs_mcs_from_tbl(&search_tbl->current_rate,
					     search_tbl, index, is_green);
			goto out;

		}
		tbl->action++;
		if (tbl->action > IWL_MIMO_SWITCH_GI)
			tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;

		if (tbl->action == start_action)
			break;
	}

	return 0;
 out:
	tbl->action++;
	if (tbl->action > IWL_MIMO_SWITCH_GI)
		tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
	return 0;

}

1645 1646 1647 1648 1649 1650 1651
/*
 * Check whether we should continue using same modulation mode, or
 * begin search for a new mode, based on:
 * 1) # tx successes or failures while using this mode
 * 2) # times calling this function
 * 3) elapsed time in this mode (not used, for now)
 */
1652
static void rs_stay_in_table(struct iwl4965_lq_sta *lq_sta)
Z
Zhu Yi 已提交
1653
{
C
Christoph Hellwig 已提交
1654
	struct iwl4965_scale_tbl_info *tbl;
Z
Zhu Yi 已提交
1655 1656 1657 1658
	int i;
	int active_tbl;
	int flush_interval_passed = 0;

1659
	active_tbl = lq_sta->active_tbl;
Z
Zhu Yi 已提交
1660

1661
	tbl = &(lq_sta->lq_info[active_tbl]);
Z
Zhu Yi 已提交
1662

1663
	/* If we've been disallowing search, see if we should now allow it */
1664
	if (lq_sta->stay_in_tbl) {
Z
Zhu Yi 已提交
1665

1666
		/* Elapsed time using current modulation mode */
1667
		if (lq_sta->flush_timer)
Z
Zhu Yi 已提交
1668 1669
			flush_interval_passed =
			    time_after(jiffies,
1670
				       (unsigned long)(lq_sta->flush_timer +
Z
Zhu Yi 已提交
1671 1672
					IWL_RATE_SCALE_FLUSH_INTVL));

1673
		/* For now, disable the elapsed time criterion */
Z
Zhu Yi 已提交
1674
		flush_interval_passed = 0;
1675 1676 1677 1678 1679 1680 1681 1682 1683

		/*
		 * Check if we should allow search for new modulation mode.
		 * If many frames have failed or succeeded, or we've used
		 * this same modulation for a long time, allow search, and
		 * reset history stats that keep track of whether we should
		 * allow a new search.  Also (below) reset all bitmaps and
		 * stats in active history.
		 */
1684 1685 1686
		if ((lq_sta->total_failed > lq_sta->max_failure_limit) ||
		    (lq_sta->total_success > lq_sta->max_success_limit) ||
		    ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
Z
Zhu Yi 已提交
1687 1688
		     && (flush_interval_passed))) {
			IWL_DEBUG_HT("LQ: stay is expired %d %d %d\n:",
1689 1690
				     lq_sta->total_failed,
				     lq_sta->total_success,
Z
Zhu Yi 已提交
1691
				     flush_interval_passed);
1692 1693

			/* Allow search for new mode */
1694 1695 1696 1697
			lq_sta->stay_in_tbl = 0;	/* only place reset */
			lq_sta->total_failed = 0;
			lq_sta->total_success = 0;
			lq_sta->flush_timer = 0;
1698 1699 1700 1701 1702 1703 1704

		/*
		 * Else if we've used this modulation mode enough repetitions
		 * (regardless of elapsed time or success/failure), reset
		 * history bitmaps and rate-specific stats for all rates in
		 * active table.
		 */
1705
		} else {
1706 1707 1708 1709
			lq_sta->table_count++;
			if (lq_sta->table_count >=
			    lq_sta->table_count_limit) {
				lq_sta->table_count = 0;
Z
Zhu Yi 已提交
1710 1711 1712 1713 1714 1715 1716 1717

				IWL_DEBUG_HT("LQ: stay in table clear win\n");
				for (i = 0; i < IWL_RATE_COUNT; i++)
					rs_rate_scale_clear_window(
						&(tbl->win[i]));
			}
		}

1718 1719 1720
		/* If transitioning to allow "search", reset all history
		 * bitmaps and stats in active table (this will become the new
		 * "search" table). */
1721
		if (!lq_sta->stay_in_tbl) {
Z
Zhu Yi 已提交
1722 1723 1724 1725 1726 1727
			for (i = 0; i < IWL_RATE_COUNT; i++)
				rs_rate_scale_clear_window(&(tbl->win[i]));
		}
	}
}

1728 1729 1730
/*
 * Do rate scaling and search for new modulation mode.
 */
C
Christoph Hellwig 已提交
1731
static void rs_rate_scale_perform(struct iwl4965_priv *priv,
Z
Zhu Yi 已提交
1732 1733 1734 1735
				  struct net_device *dev,
				  struct ieee80211_hdr *hdr,
				  struct sta_info *sta)
{
1736 1737 1738
	struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
	struct ieee80211_hw *hw = local_to_hw(local);
	struct ieee80211_conf *conf = &hw->conf;
Z
Zhu Yi 已提交
1739 1740 1741 1742
	int low = IWL_RATE_INVALID;
	int high = IWL_RATE_INVALID;
	int index;
	int i;
C
Christoph Hellwig 已提交
1743
	struct iwl4965_rate_scale_data *window = NULL;
Z
Zhu Yi 已提交
1744 1745 1746 1747 1748 1749 1750
	int current_tpt = IWL_INVALID_VALUE;
	int low_tpt = IWL_INVALID_VALUE;
	int high_tpt = IWL_INVALID_VALUE;
	u32 fail_count;
	s8 scale_action = 0;
	u16 fc, rate_mask;
	u8 update_lq = 0;
1751
	struct iwl4965_lq_sta *lq_sta;
C
Christoph Hellwig 已提交
1752
	struct iwl4965_scale_tbl_info *tbl, *tbl1;
Z
Zhu Yi 已提交
1753
	u16 rate_scale_index_msk = 0;
C
Christoph Hellwig 已提交
1754
	struct iwl4965_rate mcs_rate;
Z
Zhu Yi 已提交
1755 1756 1757 1758
	u8 is_green = 0;
	u8 active_tbl = 0;
	u8 done_search = 0;
	u16 high_low;
1759 1760 1761 1762
#ifdef CONFIG_IWL4965_HT
	u8 tid = MAX_TID_COUNT;
	__le16 *qc;
#endif
Z
Zhu Yi 已提交
1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780

	IWL_DEBUG_RATE("rate scale calculate new rate for skb\n");

	fc = le16_to_cpu(hdr->frame_control);
	if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1)) {
		/* Send management frames and broadcast/multicast data using
		 * lowest rate. */
		/* TODO: this could probably be improved.. */
		return;
	}

	if (!sta || !sta->rate_ctrl_priv)
		return;

	if (!priv->lq_mngr.lq_ready) {
		IWL_DEBUG_RATE("still rate scaling not ready\n");
		return;
	}
1781
	lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
Z
Zhu Yi 已提交
1782

1783 1784 1785 1786 1787 1788 1789
#ifdef CONFIG_IWL4965_HT
	qc = ieee80211_get_qos_ctrl(hdr);
	if (qc) {
		tid = (u8)(le16_to_cpu(*qc) & 0xf);
		rs_tl_add_packet(lq_sta, tid);
	}
#endif
1790 1791 1792 1793 1794
	/*
	 * Select rate-scale / modulation-mode table to work with in
	 * the rest of this function:  "search" if searching for better
	 * modulation mode, or "active" if doing rate scaling within a mode.
	 */
1795 1796
	if (!lq_sta->search_better_tbl)
		active_tbl = lq_sta->active_tbl;
Z
Zhu Yi 已提交
1797
	else
1798
		active_tbl = 1 - lq_sta->active_tbl;
Z
Zhu Yi 已提交
1799

1800 1801
	tbl = &(lq_sta->lq_info[active_tbl]);
	is_green = lq_sta->is_green;
Z
Zhu Yi 已提交
1802

1803
	/* current tx rate */
1804
	index = sta->last_txrate_idx;
Z
Zhu Yi 已提交
1805 1806 1807 1808

	IWL_DEBUG_RATE("Rate scale index %d for type %d\n", index,
		       tbl->lq_type);

1809
	/* rates available for this association, and for modulation mode */
1810
	rs_get_supported_rates(lq_sta, hdr, tbl->lq_type,
Z
Zhu Yi 已提交
1811 1812 1813 1814 1815 1816
				&rate_mask);

	IWL_DEBUG_RATE("mask 0x%04X \n", rate_mask);

	/* mask with station rate restriction */
	if (is_legacy(tbl->lq_type)) {
1817
		if (lq_sta->band == IEEE80211_BAND_5GHZ)
1818
			/* supp_rates has no CCK bits in A mode */
Z
Zhu Yi 已提交
1819
			rate_scale_index_msk = (u16) (rate_mask &
1820
				(lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Z
Zhu Yi 已提交
1821 1822
		else
			rate_scale_index_msk = (u16) (rate_mask &
1823
						      lq_sta->supp_rates);
Z
Zhu Yi 已提交
1824 1825 1826 1827 1828 1829 1830

	} else
		rate_scale_index_msk = rate_mask;

	if (!rate_scale_index_msk)
		rate_scale_index_msk = rate_mask;

1831 1832
	/* If current rate is no longer supported on current association,
	 * or user changed preferences for rates, find a new supported rate. */
Z
Zhu Yi 已提交
1833 1834 1835 1836
	if (index < 0 || !((1 << index) & rate_scale_index_msk)) {
		index = IWL_INVALID_VALUE;
		update_lq = 1;

1837
		/* get the highest available rate */
Z
Zhu Yi 已提交
1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848
		for (i = 0; i <= IWL_RATE_COUNT; i++) {
			if ((1 << i) & rate_scale_index_msk)
				index = i;
		}

		if (index == IWL_INVALID_VALUE) {
			IWL_WARNING("Can not find a suitable rate\n");
			return;
		}
	}

1849
	/* Get expected throughput table and history window for current rate */
Z
Zhu Yi 已提交
1850
	if (!tbl->expected_tpt)
1851
		rs_get_expected_tpt_table(lq_sta, tbl);
Z
Zhu Yi 已提交
1852 1853 1854

	window = &(tbl->win[index]);

1855 1856 1857 1858 1859 1860 1861
	/*
	 * If there is not enough history to calculate actual average
	 * throughput, keep analyzing results of more tx frames, without
	 * changing rate or mode (bypass most of the rest of this function).
	 * Set up new rate table in uCode only if old rate is not supported
	 * in current association (use new rate found above).
	 */
Z
Zhu Yi 已提交
1862 1863 1864 1865 1866 1867 1868
	fail_count = window->counter - window->success_counter;
	if (((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
	     (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))
	    || (tbl->expected_tpt == NULL)) {
		IWL_DEBUG_RATE("LQ: still below TH succ %d total %d "
			       "for index %d\n",
			       window->success_counter, window->counter, index);
1869 1870

		/* Can't calculate this yet; not enough history */
Z
Zhu Yi 已提交
1871
		window->average_tpt = IWL_INVALID_VALUE;
1872 1873 1874

		/* Should we stay with this modulation mode,
		 * or search for a new one? */
1875
		rs_stay_in_table(lq_sta);
1876 1877

		/* Set up new rate table in uCode, if needed */
Z
Zhu Yi 已提交
1878 1879
		if (update_lq) {
			rs_mcs_from_tbl(&mcs_rate, tbl, index, is_green);
1880 1881
			rs_fill_link_cmd(lq_sta, &mcs_rate, &lq_sta->lq);
			rs_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Z
Zhu Yi 已提交
1882 1883 1884
		}
		goto out;

1885 1886
	/* Else we have enough samples; calculate estimate of
	 * actual average throughput */
Z
Zhu Yi 已提交
1887 1888 1889 1890
	} else
		window->average_tpt = ((window->success_ratio *
					tbl->expected_tpt[index] + 64) / 128);

1891
	/* If we are searching for better modulation mode, check success. */
1892
	if (lq_sta->search_better_tbl) {
Z
Zhu Yi 已提交
1893 1894
		int success_limit = IWL_RATE_SCALE_SWITCH;

1895 1896 1897
		/* If good success, continue using the "search" mode;
		 * no need to send new link quality command, since we're
		 * continuing to use the setup that we've been trying. */
Z
Zhu Yi 已提交
1898
		if ((window->success_ratio > success_limit) ||
1899
		    (window->average_tpt > lq_sta->last_tpt)) {
Z
Zhu Yi 已提交
1900 1901 1902 1903 1904 1905
			if (!is_legacy(tbl->lq_type)) {
				IWL_DEBUG_HT("LQ: we are switching to HT"
					     " rate suc %d current tpt %d"
					     " old tpt %d\n",
					     window->success_ratio,
					     window->average_tpt,
1906 1907
					     lq_sta->last_tpt);
				lq_sta->enable_counter = 1;
Z
Zhu Yi 已提交
1908
			}
1909
			/* Swap tables; "search" becomes "active" */
1910
			lq_sta->active_tbl = active_tbl;
Z
Zhu Yi 已提交
1911
			current_tpt = window->average_tpt;
1912 1913

		/* Else poor success; go back to mode in "active" table */
Z
Zhu Yi 已提交
1914
		} else {
1915
			/* Nullify "search" table */
Z
Zhu Yi 已提交
1916
			tbl->lq_type = LQ_NONE;
1917 1918

			/* Revert to "active" table */
1919 1920
			active_tbl = lq_sta->active_tbl;
			tbl = &(lq_sta->lq_info[active_tbl]);
Z
Zhu Yi 已提交
1921

1922
			/* Revert to "active" rate and throughput info */
1923
			index = iwl4965_hwrate_to_plcp_idx(
Z
Zhu Yi 已提交
1924
				tbl->current_rate.rate_n_flags);
1925
			current_tpt = lq_sta->last_tpt;
Z
Zhu Yi 已提交
1926

1927
			/* Need to set up a new rate table in uCode */
Z
Zhu Yi 已提交
1928 1929 1930
			update_lq = 1;
			IWL_DEBUG_HT("XXY GO BACK TO OLD TABLE\n");
		}
1931 1932 1933

		/* Either way, we've made a decision; modulation mode
		 * search is done, allow rate adjustment next time. */
1934
		lq_sta->search_better_tbl = 0;
1935
		done_search = 1;	/* Don't switch modes below! */
Z
Zhu Yi 已提交
1936 1937 1938
		goto lq_update;
	}

1939 1940
	/* (Else) not in search of better modulation mode, try for better
	 * starting rate, while staying in this mode. */
Z
Zhu Yi 已提交
1941 1942 1943 1944 1945
	high_low = rs_get_adjacent_rate(index, rate_scale_index_msk,
					tbl->lq_type);
	low = high_low & 0xff;
	high = (high_low >> 8) & 0xff;

1946
	/* Collect measured throughputs for current and adjacent rates */
Z
Zhu Yi 已提交
1947 1948 1949 1950 1951 1952
	current_tpt = window->average_tpt;
	if (low != IWL_RATE_INVALID)
		low_tpt = tbl->win[low].average_tpt;
	if (high != IWL_RATE_INVALID)
		high_tpt = tbl->win[high].average_tpt;

1953
	/* Assume rate increase */
Z
Zhu Yi 已提交
1954 1955
	scale_action = 1;

1956
	/* Too many failures, decrease rate */
Z
Zhu Yi 已提交
1957 1958 1959 1960
	if ((window->success_ratio <= IWL_RATE_DECREASE_TH) ||
	    (current_tpt == 0)) {
		IWL_DEBUG_RATE("decrease rate because of low success_ratio\n");
		scale_action = -1;
1961 1962

	/* No throughput measured yet for adjacent rates; try increase. */
Z
Zhu Yi 已提交
1963 1964 1965
	} else if ((low_tpt == IWL_INVALID_VALUE) &&
		   (high_tpt == IWL_INVALID_VALUE))
		scale_action = 1;
1966 1967 1968

	/* Both adjacent throughputs are measured, but neither one has better
	 * throughput; we're using the best rate, don't change it! */
Z
Zhu Yi 已提交
1969 1970 1971 1972 1973
	else if ((low_tpt != IWL_INVALID_VALUE) &&
		 (high_tpt != IWL_INVALID_VALUE) &&
		 (low_tpt < current_tpt) &&
		 (high_tpt < current_tpt))
		scale_action = 0;
1974 1975 1976

	/* At least one adjacent rate's throughput is measured,
	 * and may have better performance. */
Z
Zhu Yi 已提交
1977
	else {
1978
		/* Higher adjacent rate's throughput is measured */
Z
Zhu Yi 已提交
1979
		if (high_tpt != IWL_INVALID_VALUE) {
1980
			/* Higher rate has better throughput */
Z
Zhu Yi 已提交
1981 1982 1983 1984 1985 1986 1987
			if (high_tpt > current_tpt)
				scale_action = 1;
			else {
				IWL_DEBUG_RATE
				    ("decrease rate because of high tpt\n");
				scale_action = -1;
			}
1988 1989

		/* Lower adjacent rate's throughput is measured */
Z
Zhu Yi 已提交
1990
		} else if (low_tpt != IWL_INVALID_VALUE) {
1991
			/* Lower rate has better throughput */
Z
Zhu Yi 已提交
1992 1993 1994 1995 1996 1997 1998 1999 2000
			if (low_tpt > current_tpt) {
				IWL_DEBUG_RATE
				    ("decrease rate because of low tpt\n");
				scale_action = -1;
			} else
				scale_action = 1;
		}
	}

2001 2002
	/* Sanity check; asked for decrease, but success rate or throughput
	 * has been good at old rate.  Don't change it. */
Z
Zhu Yi 已提交
2003 2004 2005 2006 2007
	if (scale_action == -1) {
		if ((low != IWL_RATE_INVALID) &&
		    ((window->success_ratio > IWL_RATE_HIGH_TH) ||
		     (current_tpt > (100 * tbl->expected_tpt[low]))))
			scale_action = 0;
2008 2009 2010

	/* Sanity check; asked for increase, but success rate has not been great
	 * even at old rate, higher rate will be worse.  Don't change it. */
Z
Zhu Yi 已提交
2011 2012 2013 2014 2015 2016
	} else if ((scale_action == 1) &&
		   (window->success_ratio < IWL_RATE_INCREASE_TH))
		scale_action = 0;

	switch (scale_action) {
	case -1:
2017
		/* Decrease starting rate, update uCode's rate table */
Z
Zhu Yi 已提交
2018 2019 2020 2021 2022 2023
		if (low != IWL_RATE_INVALID) {
			update_lq = 1;
			index = low;
		}
		break;
	case 1:
2024
		/* Increase starting rate, update uCode's rate table */
Z
Zhu Yi 已提交
2025 2026 2027 2028 2029 2030 2031
		if (high != IWL_RATE_INVALID) {
			update_lq = 1;
			index = high;
		}

		break;
	case 0:
2032
		/* No change */
Z
Zhu Yi 已提交
2033 2034 2035 2036 2037 2038 2039 2040 2041
	default:
		break;
	}

	IWL_DEBUG_HT("choose rate scale index %d action %d low %d "
		    "high %d type %d\n",
		     index, scale_action, low, high, tbl->lq_type);

 lq_update:
2042
	/* Replace uCode's rate table for the destination station. */
Z
Zhu Yi 已提交
2043 2044
	if (update_lq) {
		rs_mcs_from_tbl(&mcs_rate, tbl, index, is_green);
2045 2046
		rs_fill_link_cmd(lq_sta, &mcs_rate, &lq_sta->lq);
		rs_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Z
Zhu Yi 已提交
2047
	}
2048 2049

	/* Should we stay with this modulation mode, or search for a new one? */
2050
	rs_stay_in_table(lq_sta);
Z
Zhu Yi 已提交
2051

2052 2053 2054 2055 2056 2057
	/*
	 * Search for new modulation mode if we're:
	 * 1)  Not changing rates right now
	 * 2)  Not just finishing up a search
	 * 3)  Allowing a new search
	 */
2058
	if (!update_lq && !done_search && !lq_sta->stay_in_tbl) {
2059
		/* Save current throughput to compare with "search" throughput*/
2060
		lq_sta->last_tpt = current_tpt;
Z
Zhu Yi 已提交
2061

2062 2063
		/* Select a new "search" modulation mode to try.
		 * If one is found, set up the new "search" table. */
Z
Zhu Yi 已提交
2064
		if (is_legacy(tbl->lq_type))
2065
			rs_move_legacy_other(priv, lq_sta, conf, sta, index);
Z
Zhu Yi 已提交
2066
		else if (is_siso(tbl->lq_type))
2067
			rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
Z
Zhu Yi 已提交
2068
		else
2069
			rs_move_mimo_to_other(priv, lq_sta, conf, sta, index);
Z
Zhu Yi 已提交
2070

2071
		/* If new "search" mode was selected, set up in uCode table */
2072
		if (lq_sta->search_better_tbl) {
2073
			/* Access the "search" table, clear its history. */
2074
			tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Z
Zhu Yi 已提交
2075 2076 2077
			for (i = 0; i < IWL_RATE_COUNT; i++)
				rs_rate_scale_clear_window(&(tbl->win[i]));

2078
			/* Use new "search" start rate */
2079
			index = iwl4965_hwrate_to_plcp_idx(
Z
Zhu Yi 已提交
2080 2081 2082 2083
					tbl->current_rate.rate_n_flags);

			IWL_DEBUG_HT("Switch current  mcs: %X index: %d\n",
				     tbl->current_rate.rate_n_flags, index);
2084 2085 2086
			rs_fill_link_cmd(lq_sta, &tbl->current_rate,
					 &lq_sta->lq);
			rs_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Z
Zhu Yi 已提交
2087 2088
		}

2089 2090 2091 2092 2093
		/* If the "active" (non-search) mode was legacy,
		 * and we've tried switching antennas,
		 * but we haven't been able to try HT modes (not available),
		 * stay with best antenna legacy modulation for a while
		 * before next round of mode comparisons. */
2094
		tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
Z
Zhu Yi 已提交
2095
		if (is_legacy(tbl1->lq_type) &&
2096
#ifdef CONFIG_IWL4965_HT
2097
		   (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE)) &&
Z
Zhu Yi 已提交
2098
#endif
2099 2100
		    (lq_sta->action_counter >= 1)) {
			lq_sta->action_counter = 0;
Z
Zhu Yi 已提交
2101
			IWL_DEBUG_HT("LQ: STAY in legacy table\n");
2102
			rs_set_stay_in_table(1, lq_sta);
Z
Zhu Yi 已提交
2103 2104
		}

2105 2106 2107
		/* If we're in an HT mode, and all 3 mode switch actions
		 * have been tried and compared, stay in this best modulation
		 * mode for a while before next round of mode comparisons. */
2108 2109
		if (lq_sta->enable_counter &&
		    (lq_sta->action_counter >= IWL_ACTION_LIMIT)) {
2110 2111 2112 2113 2114 2115
#ifdef CONFIG_IWL4965_HT
			if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
			    (lq_sta->tx_agg_tid_en & (1 << tid)) &&
			    (tid != MAX_TID_COUNT)) {
				IWL_DEBUG_HT("try to aggregate tid %d\n", tid);
				rs_tl_turn_on_agg(priv, tid, lq_sta, sta);
Z
Zhu Yi 已提交
2116
			}
2117
#endif /*CONFIG_IWL4965_HT */
2118 2119
			lq_sta->action_counter = 0;
			rs_set_stay_in_table(0, lq_sta);
Z
Zhu Yi 已提交
2120
		}
2121 2122 2123 2124 2125 2126 2127 2128

	/*
	 * Else, don't search for a new modulation mode.
	 * Put new timestamp in stay-in-modulation-mode flush timer if:
	 * 1)  Not changing rates right now
	 * 2)  Not just finishing up a search
	 * 3)  flush timer is empty
	 */
Z
Zhu Yi 已提交
2129
	} else {
2130 2131
		if ((!update_lq) && (!done_search) && (!lq_sta->flush_timer))
			lq_sta->flush_timer = jiffies;
Z
Zhu Yi 已提交
2132 2133 2134 2135 2136
	}

out:
	rs_mcs_from_tbl(&tbl->current_rate, tbl, index, is_green);
	i = index;
2137
	sta->last_txrate_idx = i;
Z
Zhu Yi 已提交
2138

2139
	/* sta->txrate_idx is an index to A mode rates which start
Z
Zhu Yi 已提交
2140 2141
	 * at IWL_FIRST_OFDM_RATE
	 */
2142 2143
	if (lq_sta->band == IEEE80211_BAND_5GHZ)
		sta->txrate_idx = i - IWL_FIRST_OFDM_RATE;
Z
Zhu Yi 已提交
2144
	else
2145
		sta->txrate_idx = i;
Z
Zhu Yi 已提交
2146 2147 2148 2149 2150

	return;
}


C
Christoph Hellwig 已提交
2151
static void rs_initialize_lq(struct iwl4965_priv *priv,
2152
			     struct ieee80211_conf *conf,
Z
Zhu Yi 已提交
2153 2154 2155
			     struct sta_info *sta)
{
	int i;
2156
	struct iwl4965_lq_sta *lq_sta;
C
Christoph Hellwig 已提交
2157
	struct iwl4965_scale_tbl_info *tbl;
Z
Zhu Yi 已提交
2158 2159
	u8 active_tbl = 0;
	int rate_idx;
2160
	u8 use_green = rs_use_green(priv, conf);
C
Christoph Hellwig 已提交
2161
	struct iwl4965_rate mcs_rate;
Z
Zhu Yi 已提交
2162 2163 2164 2165

	if (!sta || !sta->rate_ctrl_priv)
		goto out;

2166
	lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
2167
	i = sta->last_txrate_idx;
Z
Zhu Yi 已提交
2168

2169
	if ((lq_sta->lq.sta_id == 0xff) &&
Z
Zhu Yi 已提交
2170 2171 2172
	    (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
		goto out;

2173 2174
	if (!lq_sta->search_better_tbl)
		active_tbl = lq_sta->active_tbl;
Z
Zhu Yi 已提交
2175
	else
2176
		active_tbl = 1 - lq_sta->active_tbl;
Z
Zhu Yi 已提交
2177

2178
	tbl = &(lq_sta->lq_info[active_tbl]);
Z
Zhu Yi 已提交
2179 2180 2181 2182

	if ((i < 0) || (i >= IWL_RATE_COUNT))
		i = 0;

C
Christoph Hellwig 已提交
2183
	mcs_rate.rate_n_flags = iwl4965_rates[i].plcp ;
Z
Zhu Yi 已提交
2184 2185 2186 2187 2188 2189 2190
	mcs_rate.rate_n_flags |= RATE_MCS_ANT_B_MSK;
	mcs_rate.rate_n_flags &= ~RATE_MCS_ANT_A_MSK;

	if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
		mcs_rate.rate_n_flags |= RATE_MCS_CCK_MSK;

	tbl->antenna_type = ANT_AUX;
2191
	rs_get_tbl_info_from_mcs(&mcs_rate, priv->band, tbl, &rate_idx);
Z
Zhu Yi 已提交
2192
	if (!rs_is_ant_connected(priv->valid_antenna, tbl->antenna_type))
2193
	    rs_toggle_antenna(&mcs_rate, tbl);
Z
Zhu Yi 已提交
2194 2195 2196

	rs_mcs_from_tbl(&mcs_rate, tbl, rate_idx, use_green);
	tbl->current_rate.rate_n_flags = mcs_rate.rate_n_flags;
2197 2198 2199
	rs_get_expected_tpt_table(lq_sta, tbl);
	rs_fill_link_cmd(lq_sta, &mcs_rate, &lq_sta->lq);
	rs_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Z
Zhu Yi 已提交
2200 2201 2202 2203
 out:
	return;
}

2204
static void rs_get_rate(void *priv_rate, struct net_device *dev,
2205 2206
			struct ieee80211_supported_band *sband,
			struct sk_buff *skb,
2207
			struct rate_selection *sel)
Z
Zhu Yi 已提交
2208 2209 2210 2211
{

	int i;
	struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2212
	struct ieee80211_conf *conf = &local->hw.conf;
Z
Zhu Yi 已提交
2213 2214
	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
	struct sta_info *sta;
2215
	u16 fc;
C
Christoph Hellwig 已提交
2216
	struct iwl4965_priv *priv = (struct iwl4965_priv *)priv_rate;
2217
	struct iwl4965_lq_sta *lq_sta;
Z
Zhu Yi 已提交
2218

2219
	IWL_DEBUG_RATE_LIMIT("rate scale calculate new rate for skb\n");
Z
Zhu Yi 已提交
2220

2221 2222
	rcu_read_lock();

Z
Zhu Yi 已提交
2223 2224
	sta = sta_info_get(local, hdr->addr1);

2225 2226 2227 2228 2229
	/* Send management frames and broadcast/multicast data using lowest
	 * rate. */
	fc = le16_to_cpu(hdr->frame_control);
	if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1) ||
	    !sta || !sta->rate_ctrl_priv) {
2230
		sel->rate = rate_lowest(local, sband, sta);
2231
		goto out;
Z
Zhu Yi 已提交
2232 2233
	}

2234
	lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
2235
	i = sta->last_txrate_idx;
Z
Zhu Yi 已提交
2236

2237 2238
	if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
	    !lq_sta->ibss_sta_added) {
C
Christoph Hellwig 已提交
2239
		u8 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
2240
		DECLARE_MAC_BUF(mac);
Z
Zhu Yi 已提交
2241 2242

		if (sta_id == IWL_INVALID_STATION) {
2243 2244
			IWL_DEBUG_RATE("LQ: ADD station %s\n",
				       print_mac(mac, hdr->addr1));
2245 2246
			sta_id = iwl4965_add_station_flags(priv, hdr->addr1,
							0, CMD_ASYNC, NULL);
Z
Zhu Yi 已提交
2247 2248
		}
		if ((sta_id != IWL_INVALID_STATION)) {
2249 2250 2251
			lq_sta->lq.sta_id = sta_id;
			lq_sta->lq.rs_table[0].rate_n_flags = 0;
			lq_sta->ibss_sta_added = 1;
2252
			rs_initialize_lq(priv, conf, sta);
Z
Zhu Yi 已提交
2253
		}
2254
		if (!lq_sta->ibss_sta_added)
Z
Zhu Yi 已提交
2255 2256 2257
			goto done;
	}

2258
done:
2259
	if ((i < 0) || (i > IWL_RATE_COUNT)) {
2260
		sel->rate = rate_lowest(local, sband, sta);
2261
		goto out;
2262
	}
Z
Zhu Yi 已提交
2263

2264
	sel->rate = &priv->ieee_rates[i];
2265 2266
out:
	rcu_read_unlock();
Z
Zhu Yi 已提交
2267 2268 2269 2270
}

static void *rs_alloc_sta(void *priv, gfp_t gfp)
{
2271
	struct iwl4965_lq_sta *lq_sta;
Z
Zhu Yi 已提交
2272 2273 2274 2275
	int i, j;

	IWL_DEBUG_RATE("create station rate scale window\n");

2276
	lq_sta = kzalloc(sizeof(struct iwl4965_lq_sta), gfp);
Z
Zhu Yi 已提交
2277

2278
	if (lq_sta == NULL)
Z
Zhu Yi 已提交
2279
		return NULL;
2280
	lq_sta->lq.sta_id = 0xff;
Z
Zhu Yi 已提交
2281

2282

Z
Zhu Yi 已提交
2283 2284
	for (j = 0; j < LQ_SIZE; j++)
		for (i = 0; i < IWL_RATE_COUNT; i++)
2285
			rs_rate_scale_clear_window(&(lq_sta->lq_info[j].win[i]));
Z
Zhu Yi 已提交
2286

2287
	return lq_sta;
Z
Zhu Yi 已提交
2288 2289 2290 2291 2292 2293 2294
}

static void rs_rate_init(void *priv_rate, void *priv_sta,
			 struct ieee80211_local *local,
			 struct sta_info *sta)
{
	int i, j;
2295
	struct ieee80211_conf *conf = &local->hw.conf;
2296
	struct ieee80211_supported_band *sband;
C
Christoph Hellwig 已提交
2297
	struct iwl4965_priv *priv = (struct iwl4965_priv *)priv_rate;
2298
	struct iwl4965_lq_sta *lq_sta = priv_sta;
Z
Zhu Yi 已提交
2299

2300 2301
	sband = local->hw.wiphy->bands[local->hw.conf.channel->band];

2302
	lq_sta->flush_timer = 0;
2303 2304
	lq_sta->supp_rates = sta->supp_rates[sband->band];
	sta->txrate_idx = 3;
Z
Zhu Yi 已提交
2305 2306
	for (j = 0; j < LQ_SIZE; j++)
		for (i = 0; i < IWL_RATE_COUNT; i++)
2307
			rs_rate_scale_clear_window(&(lq_sta->lq_info[j].win[i]));
Z
Zhu Yi 已提交
2308 2309 2310 2311 2312 2313 2314

	IWL_DEBUG_RATE("rate scale global init\n");
	/* TODO: what is a good starting rate for STA? About middle? Maybe not
	 * the lowest or the highest rate.. Could consider using RSSI from
	 * previous packets? Need to have IEEE 802.1X auth succeed immediately
	 * after assoc.. */

2315
	lq_sta->ibss_sta_added = 0;
Z
Zhu Yi 已提交
2316
	if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
C
Christoph Hellwig 已提交
2317
		u8 sta_id = iwl4965_hw_find_station(priv, sta->addr);
2318 2319
		DECLARE_MAC_BUF(mac);

Z
Zhu Yi 已提交
2320
		/* for IBSS the call are from tasklet */
2321 2322
		IWL_DEBUG_HT("LQ: ADD station %s\n",
			     print_mac(mac, sta->addr));
Z
Zhu Yi 已提交
2323 2324

		if (sta_id == IWL_INVALID_STATION) {
2325 2326
			IWL_DEBUG_RATE("LQ: ADD station %s\n",
				       print_mac(mac, sta->addr));
2327 2328
			sta_id = iwl4965_add_station_flags(priv, sta->addr,
							0, CMD_ASYNC, NULL);
Z
Zhu Yi 已提交
2329 2330
		}
		if ((sta_id != IWL_INVALID_STATION)) {
2331 2332
			lq_sta->lq.sta_id = sta_id;
			lq_sta->lq.rs_table[0].rate_n_flags = 0;
Z
Zhu Yi 已提交
2333 2334 2335 2336 2337
		}
		/* FIXME: this is w/a remove it later */
		priv->assoc_station_added = 1;
	}

2338
	/* Find highest tx rate supported by hardware and destination station */
2339 2340 2341 2342 2343 2344
	for (i = 0; i < sband->n_bitrates; i++)
		if (sta->supp_rates[sband->band] & BIT(i))
			sta->txrate_idx = i;

	sta->last_txrate_idx = sta->txrate_idx;
	/* WTF is with this bogus comment? A doesn't have cck rates */
2345
	/* For MODE_IEEE80211A, cck rates are at end of rate table */
2346 2347
	if (local->hw.conf.channel->band == IEEE80211_BAND_5GHZ)
		sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
Z
Zhu Yi 已提交
2348

2349 2350 2351 2352 2353 2354 2355
	lq_sta->is_dup = 0;
	lq_sta->valid_antenna = priv->valid_antenna;
	lq_sta->antenna = priv->antenna;
	lq_sta->is_green = rs_use_green(priv, conf);
	lq_sta->active_rate = priv->active_rate;
	lq_sta->active_rate &= ~(0x1000);
	lq_sta->active_rate_basic = priv->active_rate_basic;
2356
	lq_sta->band = priv->band;
2357
#ifdef CONFIG_IWL4965_HT
2358 2359 2360 2361
	/*
	 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
	 * supp_rates[] does not; shift to convert format, force 9 MBits off.
	 */
2362 2363
	lq_sta->active_siso_rate = (priv->current_ht_config.supp_mcs_set[0] << 1);
	lq_sta->active_siso_rate |=
2364
			(priv->current_ht_config.supp_mcs_set[0] & 0x1);
2365 2366 2367
	lq_sta->active_siso_rate &= ~((u16)0x2);
	lq_sta->active_siso_rate =
			lq_sta->active_siso_rate << IWL_FIRST_OFDM_RATE;
Z
Zhu Yi 已提交
2368

2369
	/* Same here */
2370 2371
	lq_sta->active_mimo_rate = (priv->current_ht_config.supp_mcs_set[1] << 1);
	lq_sta->active_mimo_rate |=
2372
			(priv->current_ht_config.supp_mcs_set[1] & 0x1);
2373 2374 2375 2376 2377 2378
	lq_sta->active_mimo_rate &= ~((u16)0x2);
	lq_sta->active_mimo_rate =
			lq_sta->active_mimo_rate << IWL_FIRST_OFDM_RATE;
	IWL_DEBUG_HT("SISO RATE 0x%X MIMO RATE 0x%X\n",
		     lq_sta->active_siso_rate,
		     lq_sta->active_mimo_rate);
2379 2380
	/* as default allow aggregation for all tids */
	lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
2381
#endif /*CONFIG_IWL4965_HT*/
2382
#ifdef CONFIG_MAC80211_DEBUGFS
2383
	lq_sta->drv = priv;
2384
#endif
Z
Zhu Yi 已提交
2385 2386 2387 2388

	if (priv->assoc_station_added)
		priv->lq_mngr.lq_ready = 1;

2389
	rs_initialize_lq(priv, conf, sta);
Z
Zhu Yi 已提交
2390 2391
}

2392
static void rs_fill_link_cmd(struct iwl4965_lq_sta *lq_sta,
C
Christoph Hellwig 已提交
2393 2394
			    struct iwl4965_rate *tx_mcs,
			    struct iwl4965_link_quality_cmd *lq_cmd)
Z
Zhu Yi 已提交
2395 2396 2397
{
	int index = 0;
	int rate_idx;
2398
	int repeat_rate = 0;
Z
Zhu Yi 已提交
2399 2400
	u8 ant_toggle_count = 0;
	u8 use_ht_possible = 1;
C
Christoph Hellwig 已提交
2401 2402
	struct iwl4965_rate new_rate;
	struct iwl4965_scale_tbl_info tbl_type = { 0 };
Z
Zhu Yi 已提交
2403

2404
	/* Override starting rate (index 0) if needed for debug purposes */
2405
	rs_dbgfs_set_mcs(lq_sta, tx_mcs, index);
2406

2407
	/* Interpret rate_n_flags */
2408
	rs_get_tbl_info_from_mcs(tx_mcs, lq_sta->band,
Z
Zhu Yi 已提交
2409 2410
				  &tbl_type, &rate_idx);

2411
	/* How many times should we repeat the initial rate? */
Z
Zhu Yi 已提交
2412 2413
	if (is_legacy(tbl_type.lq_type)) {
		ant_toggle_count = 1;
2414
		repeat_rate = IWL_NUMBER_TRY;
Z
Zhu Yi 已提交
2415
	} else
2416
		repeat_rate = IWL_HT_NUMBER_TRY;
Z
Zhu Yi 已提交
2417 2418 2419

	lq_cmd->general_params.mimo_delimiter =
			is_mimo(tbl_type.lq_type) ? 1 : 0;
2420 2421

	/* Fill 1st table entry (index 0) */
Z
Zhu Yi 已提交
2422 2423 2424 2425 2426
	lq_cmd->rs_table[index].rate_n_flags =
			cpu_to_le32(tx_mcs->rate_n_flags);
	new_rate.rate_n_flags = tx_mcs->rate_n_flags;

	if (is_mimo(tbl_type.lq_type) || (tbl_type.antenna_type == ANT_MAIN))
2427 2428
		lq_cmd->general_params.single_stream_ant_msk
			= LINK_QUAL_ANT_A_MSK;
Z
Zhu Yi 已提交
2429
	else
2430 2431
		lq_cmd->general_params.single_stream_ant_msk
			= LINK_QUAL_ANT_B_MSK;
Z
Zhu Yi 已提交
2432 2433

	index++;
2434
	repeat_rate--;
Z
Zhu Yi 已提交
2435

2436
	/* Fill rest of rate table */
Z
Zhu Yi 已提交
2437
	while (index < LINK_QUAL_MAX_RETRY_NUM) {
2438 2439 2440
		/* Repeat initial/next rate.
		 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
		 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
2441
		while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
Z
Zhu Yi 已提交
2442 2443 2444 2445 2446 2447 2448 2449 2450
			if (is_legacy(tbl_type.lq_type)) {
				if (ant_toggle_count <
				    NUM_TRY_BEFORE_ANTENNA_TOGGLE)
					ant_toggle_count++;
				else {
					rs_toggle_antenna(&new_rate, &tbl_type);
					ant_toggle_count = 1;
				}
			}
2451

2452
			/* Override next rate if needed for debug purposes */
2453
			rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2454 2455

			/* Fill next table entry */
Z
Zhu Yi 已提交
2456 2457
			lq_cmd->rs_table[index].rate_n_flags =
					cpu_to_le32(new_rate.rate_n_flags);
2458
			repeat_rate--;
Z
Zhu Yi 已提交
2459 2460 2461
			index++;
		}

2462
		rs_get_tbl_info_from_mcs(&new_rate, lq_sta->band, &tbl_type,
Z
Zhu Yi 已提交
2463 2464
						&rate_idx);

2465 2466 2467
		/* Indicate to uCode which entries might be MIMO.
		 * If initial rate was MIMO, this will finally end up
		 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
Z
Zhu Yi 已提交
2468 2469 2470
		if (is_mimo(tbl_type.lq_type))
			lq_cmd->general_params.mimo_delimiter = index;

2471
		/* Get next rate */
2472
		rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
2473
				  use_ht_possible, &new_rate);
Z
Zhu Yi 已提交
2474

2475
		/* How many times should we repeat the next rate? */
Z
Zhu Yi 已提交
2476 2477 2478 2479 2480 2481 2482
		if (is_legacy(tbl_type.lq_type)) {
			if (ant_toggle_count < NUM_TRY_BEFORE_ANTENNA_TOGGLE)
				ant_toggle_count++;
			else {
				rs_toggle_antenna(&new_rate, &tbl_type);
				ant_toggle_count = 1;
			}
2483
			repeat_rate = IWL_NUMBER_TRY;
Z
Zhu Yi 已提交
2484
		} else
2485
			repeat_rate = IWL_HT_NUMBER_TRY;
Z
Zhu Yi 已提交
2486

2487 2488
		/* Don't allow HT rates after next pass.
		 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
Z
Zhu Yi 已提交
2489 2490
		use_ht_possible = 0;

2491
		/* Override next rate if needed for debug purposes */
2492
		rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2493 2494

		/* Fill next table entry */
Z
Zhu Yi 已提交
2495 2496 2497 2498
		lq_cmd->rs_table[index].rate_n_flags =
				cpu_to_le32(new_rate.rate_n_flags);

		index++;
2499
		repeat_rate--;
Z
Zhu Yi 已提交
2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518
	}

	lq_cmd->general_params.dual_stream_ant_msk = 3;
	lq_cmd->agg_params.agg_dis_start_th = 3;
	lq_cmd->agg_params.agg_time_limit = cpu_to_le16(4000);
}

static void *rs_alloc(struct ieee80211_local *local)
{
	return local->hw.priv;
}
/* rate scale requires free function to be implemented */
static void rs_free(void *priv_rate)
{
	return;
}

static void rs_clear(void *priv_rate)
{
C
Christoph Hellwig 已提交
2519
	struct iwl4965_priv *priv = (struct iwl4965_priv *) priv_rate;
Z
Zhu Yi 已提交
2520 2521 2522 2523 2524 2525 2526 2527 2528 2529

	IWL_DEBUG_RATE("enter\n");

	priv->lq_mngr.lq_ready = 0;

	IWL_DEBUG_RATE("leave\n");
}

static void rs_free_sta(void *priv, void *priv_sta)
{
2530
	struct iwl4965_lq_sta *lq_sta = priv_sta;
Z
Zhu Yi 已提交
2531 2532

	IWL_DEBUG_RATE("enter\n");
2533
	kfree(lq_sta);
Z
Zhu Yi 已提交
2534 2535 2536 2537
	IWL_DEBUG_RATE("leave\n");
}


2538
#ifdef CONFIG_MAC80211_DEBUGFS
2539 2540 2541 2542 2543
static int open_file_generic(struct inode *inode, struct file *file)
{
	file->private_data = inode->i_private;
	return 0;
}
2544
static void rs_dbgfs_set_mcs(struct iwl4965_lq_sta *lq_sta,
C
Christoph Hellwig 已提交
2545
				struct iwl4965_rate *mcs, int index)
2546
{
2547 2548
	u32 base_rate;

2549
	if (lq_sta->band == IEEE80211_BAND_5GHZ)
2550 2551 2552 2553
		base_rate = 0x800D;
	else
		base_rate = 0x820A;

2554
	if (lq_sta->dbg_fixed.rate_n_flags) {
2555
		if (index < 12)
2556
			mcs->rate_n_flags = lq_sta->dbg_fixed.rate_n_flags;
2557
		else
2558
			mcs->rate_n_flags = base_rate;
2559 2560 2561 2562 2563 2564
		IWL_DEBUG_RATE("Fixed rate ON\n");
		return;
	}

	IWL_DEBUG_RATE("Fixed rate OFF\n");
}
2565

2566 2567 2568
static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
			const char __user *user_buf, size_t count, loff_t *ppos)
{
2569
	struct iwl4965_lq_sta *lq_sta = file->private_data;
2570 2571 2572 2573 2574 2575 2576 2577 2578 2579
	char buf[64];
	int buf_size;
	u32 parsed_rate;

	memset(buf, 0, sizeof(buf));
	buf_size = min(count, sizeof(buf) -  1);
	if (copy_from_user(buf, user_buf, buf_size))
		return -EFAULT;

	if (sscanf(buf, "%x", &parsed_rate) == 1)
2580
		lq_sta->dbg_fixed.rate_n_flags = parsed_rate;
2581
	else
2582
		lq_sta->dbg_fixed.rate_n_flags = 0;
2583

2584 2585 2586
	lq_sta->active_rate = 0x0FFF;	/* 1 - 54 MBits, includes CCK */
	lq_sta->active_siso_rate = 0x1FD0;	/* 6 - 60 MBits, no 9, no CCK */
	lq_sta->active_mimo_rate = 0x1FD0;	/* 6 - 60 MBits, no 9, no CCK */
2587 2588

	IWL_DEBUG_RATE("sta_id %d rate 0x%X\n",
2589
		lq_sta->lq.sta_id, lq_sta->dbg_fixed.rate_n_flags);
2590

2591 2592 2593
	if (lq_sta->dbg_fixed.rate_n_flags) {
		rs_fill_link_cmd(lq_sta, &lq_sta->dbg_fixed, &lq_sta->lq);
		rs_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC);
2594 2595 2596 2597
	}

	return count;
}
2598

2599 2600 2601 2602 2603 2604 2605
static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
			char __user *user_buf, size_t count, loff_t *ppos)
{
	char buff[1024];
	int desc = 0;
	int i = 0;

2606
	struct iwl4965_lq_sta *lq_sta = file->private_data;
2607

2608
	desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
2609
	desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
2610 2611
			lq_sta->total_failed, lq_sta->total_success,
			lq_sta->active_rate);
2612
	desc += sprintf(buff+desc, "fixed rate 0x%X\n",
2613
			lq_sta->dbg_fixed.rate_n_flags);
2614 2615
	desc += sprintf(buff+desc, "general:"
		"flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
2616 2617 2618 2619
		lq_sta->lq.general_params.flags,
		lq_sta->lq.general_params.mimo_delimiter,
		lq_sta->lq.general_params.single_stream_ant_msk,
		lq_sta->lq.general_params.dual_stream_ant_msk);
2620 2621 2622

	desc += sprintf(buff+desc, "agg:"
			"time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
2623 2624 2625
			le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
			lq_sta->lq.agg_params.agg_dis_start_th,
			lq_sta->lq.agg_params.agg_frame_cnt_limit);
2626 2627 2628

	desc += sprintf(buff+desc,
			"Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
2629 2630 2631 2632
			lq_sta->lq.general_params.start_rate_index[0],
			lq_sta->lq.general_params.start_rate_index[1],
			lq_sta->lq.general_params.start_rate_index[2],
			lq_sta->lq.general_params.start_rate_index[3]);
2633 2634 2635 2636


	for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
		desc += sprintf(buff+desc, " rate[%d] 0x%X\n",
2637
			i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
2638 2639 2640 2641 2642

	return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
}

static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
2643
	.write = rs_sta_dbgfs_scale_table_write,
2644 2645 2646
	.read = rs_sta_dbgfs_scale_table_read,
	.open = open_file_generic,
};
2647 2648 2649 2650 2651 2652 2653
static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
			char __user *user_buf, size_t count, loff_t *ppos)
{
	char buff[1024];
	int desc = 0;
	int i, j;

2654
	struct iwl4965_lq_sta *lq_sta = file->private_data;
2655 2656 2657
	for (i = 0; i < LQ_SIZE; i++) {
		desc += sprintf(buff+desc, "%s type=%d SGI=%d FAT=%d DUP=%d\n"
				"rate=0x%X\n",
2658 2659 2660 2661 2662 2663
				lq_sta->active_tbl == i?"*":"x",
				lq_sta->lq_info[i].lq_type,
				lq_sta->lq_info[i].is_SGI,
				lq_sta->lq_info[i].is_fat,
				lq_sta->lq_info[i].is_dup,
				lq_sta->lq_info[i].current_rate.rate_n_flags);
2664 2665
		for (j = 0; j < IWL_RATE_COUNT; j++) {
			desc += sprintf(buff+desc,
2666 2667 2668 2669
				"counter=%d success=%d %%=%d\n",
				lq_sta->lq_info[i].win[j].counter,
				lq_sta->lq_info[i].win[j].success_counter,
				lq_sta->lq_info[i].win[j].success_ratio);
2670 2671 2672 2673 2674 2675 2676 2677 2678
		}
	}
	return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
}

static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
	.read = rs_sta_dbgfs_stats_table_read,
	.open = open_file_generic,
};
2679

2680 2681 2682
static void rs_add_debugfs(void *priv, void *priv_sta,
					struct dentry *dir)
{
2683 2684
	struct iwl4965_lq_sta *lq_sta = priv_sta;
	lq_sta->rs_sta_dbgfs_scale_table_file =
2685
		debugfs_create_file("rate_scale_table", 0600, dir,
2686 2687
				lq_sta, &rs_sta_dbgfs_scale_table_ops);
	lq_sta->rs_sta_dbgfs_stats_table_file =
2688
		debugfs_create_file("rate_stats_table", 0600, dir,
2689
			lq_sta, &rs_sta_dbgfs_stats_table_ops);
2690 2691 2692 2693 2694 2695
#ifdef CONFIG_IWL4965_HT
	lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
		debugfs_create_u8("tx_agg_tid_enable", 0600, dir,
		&lq_sta->tx_agg_tid_en);
#endif

2696 2697 2698 2699
}

static void rs_remove_debugfs(void *priv, void *priv_sta)
{
2700 2701 2702
	struct iwl4965_lq_sta *lq_sta = priv_sta;
	debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
	debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
2703 2704 2705
#ifdef CONFIG_IWL4965_HT
	debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
#endif
2706 2707 2708
}
#endif

Z
Zhu Yi 已提交
2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719
static struct rate_control_ops rs_ops = {
	.module = NULL,
	.name = RS_NAME,
	.tx_status = rs_tx_status,
	.get_rate = rs_get_rate,
	.rate_init = rs_rate_init,
	.clear = rs_clear,
	.alloc = rs_alloc,
	.free = rs_free,
	.alloc_sta = rs_alloc_sta,
	.free_sta = rs_free_sta,
2720 2721 2722 2723
#ifdef CONFIG_MAC80211_DEBUGFS
	.add_sta_debugfs = rs_add_debugfs,
	.remove_sta_debugfs = rs_remove_debugfs,
#endif
Z
Zhu Yi 已提交
2724 2725
};

C
Christoph Hellwig 已提交
2726
int iwl4965_fill_rs_info(struct ieee80211_hw *hw, char *buf, u8 sta_id)
Z
Zhu Yi 已提交
2727 2728
{
	struct ieee80211_local *local = hw_to_local(hw);
C
Christoph Hellwig 已提交
2729
	struct iwl4965_priv *priv = hw->priv;
2730
	struct iwl4965_lq_sta *lq_sta;
Z
Zhu Yi 已提交
2731
	struct sta_info *sta;
2732
	int cnt = 0, i;
Z
Zhu Yi 已提交
2733 2734 2735 2736 2737
	u32 samples = 0, success = 0, good = 0;
	unsigned long now = jiffies;
	u32 max_time = 0;
	u8 lq_type, antenna;

2738 2739
	rcu_read_lock();

Z
Zhu Yi 已提交
2740 2741
	sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr);
	if (!sta || !sta->rate_ctrl_priv) {
2742
		if (sta)
Z
Zhu Yi 已提交
2743
			IWL_DEBUG_RATE("leave - no private rate data!\n");
2744
		else
Z
Zhu Yi 已提交
2745
			IWL_DEBUG_RATE("leave - no station!\n");
2746
		rcu_read_unlock();
Z
Zhu Yi 已提交
2747 2748 2749
		return sprintf(buf, "station %d not found\n", sta_id);
	}

2750
	lq_sta = (void *)sta->rate_ctrl_priv;
Z
Zhu Yi 已提交
2751

2752 2753
	lq_type = lq_sta->lq_info[lq_sta->active_tbl].lq_type;
	antenna = lq_sta->lq_info[lq_sta->active_tbl].antenna_type;
Z
Zhu Yi 已提交
2754 2755 2756 2757 2758 2759 2760 2761

	if (is_legacy(lq_type))
		i = IWL_RATE_54M_INDEX;
	else
		i = IWL_RATE_60M_INDEX;
	while (1) {
		u64 mask;
		int j;
2762
		int active = lq_sta->active_tbl;
Z
Zhu Yi 已提交
2763

2764 2765
		cnt +=
		    sprintf(&buf[cnt], " %2dMbs: ", iwl4965_rates[i].ieee / 2);
Z
Zhu Yi 已提交
2766 2767 2768

		mask = (1ULL << (IWL_RATE_MAX_WINDOW - 1));
		for (j = 0; j < IWL_RATE_MAX_WINDOW; j++, mask >>= 1)
2769 2770
			buf[cnt++] =
				(lq_sta->lq_info[active].win[i].data & mask)
Z
Zhu Yi 已提交
2771 2772
				? '1' : '0';

2773 2774 2775
		samples += lq_sta->lq_info[active].win[i].counter;
		good += lq_sta->lq_info[active].win[i].success_counter;
		success += lq_sta->lq_info[active].win[i].success_counter *
C
Christoph Hellwig 已提交
2776
			   iwl4965_rates[i].ieee;
Z
Zhu Yi 已提交
2777

2778
		if (lq_sta->lq_info[active].win[i].stamp) {
Z
Zhu Yi 已提交
2779 2780
			int delta =
				   jiffies_to_msecs(now -
2781
				   lq_sta->lq_info[active].win[i].stamp);
Z
Zhu Yi 已提交
2782 2783 2784 2785

			if (delta > max_time)
				max_time = delta;

2786
			cnt += sprintf(&buf[cnt], "%5dms\n", delta);
Z
Zhu Yi 已提交
2787
		} else
2788
			buf[cnt++] = '\n';
Z
Zhu Yi 已提交
2789

C
Christoph Hellwig 已提交
2790
		j = iwl4965_get_prev_ieee_rate(i);
Z
Zhu Yi 已提交
2791 2792 2793 2794 2795 2796 2797
		if (j == i)
			break;
		i = j;
	}

	/* Display the average rate of all samples taken.
	 *
2798
	 * NOTE:  We multiply # of samples by 2 since the IEEE measurement
C
Christoph Hellwig 已提交
2799
	 * added from iwl4965_rates is actually 2X the rate */
Z
Zhu Yi 已提交
2800
	if (samples)
2801
		cnt += sprintf(&buf[cnt],
Z
Zhu Yi 已提交
2802 2803 2804 2805 2806
			 "\nAverage rate is %3d.%02dMbs over last %4dms\n"
			 "%3d%% success (%d good packets over %d tries)\n",
			 success / (2 * samples), (success * 5 / samples) % 10,
			 max_time, good * 100 / samples, good, samples);
	else
2807 2808 2809
		cnt += sprintf(&buf[cnt], "\nAverage rate: 0Mbs\n");

	cnt += sprintf(&buf[cnt], "\nrate scale type %d antenna %d "
Z
Zhu Yi 已提交
2810
			 "active_search %d rate index %d\n", lq_type, antenna,
2811
			 lq_sta->search_better_tbl, sta->last_txrate_idx);
Z
Zhu Yi 已提交
2812

2813
	rcu_read_unlock();
2814
	return cnt;
Z
Zhu Yi 已提交
2815 2816
}

C
Christoph Hellwig 已提交
2817
void iwl4965_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
Z
Zhu Yi 已提交
2818
{
C
Christoph Hellwig 已提交
2819
	struct iwl4965_priv *priv = hw->priv;
Z
Zhu Yi 已提交
2820 2821 2822 2823

	priv->lq_mngr.lq_ready = 1;
}

C
Christoph Hellwig 已提交
2824
void iwl4965_rate_control_register(struct ieee80211_hw *hw)
Z
Zhu Yi 已提交
2825 2826 2827 2828
{
	ieee80211_rate_control_register(&rs_ops);
}

C
Christoph Hellwig 已提交
2829
void iwl4965_rate_control_unregister(struct ieee80211_hw *hw)
Z
Zhu Yi 已提交
2830 2831 2832 2833
{
	ieee80211_rate_control_unregister(&rs_ops);
}