iwl-agn-lib.c 58.7 KB
Newer Older
1
2
3
4
/******************************************************************************
 *
 * GPL LICENSE SUMMARY
 *
Wey-Yi Guy's avatar
Wey-Yi Guy committed
5
 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 *
 * 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.GPL.
 *
 * Contact Information:
 *  Intel Linux Wireless <ilw@linux.intel.com>
 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
 *
 *****************************************************************************/
29
#include <linux/etherdevice.h>
30
31
32
33
34
35
36
37
38
39
40
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/sched.h>

#include "iwl-dev.h"
#include "iwl-core.h"
#include "iwl-io.h"
#include "iwl-helpers.h"
#include "iwl-agn-hw.h"
#include "iwl-agn.h"
41
#include "iwl-sta.h"
42
#include "iwl-trans.h"
43

Wey-Yi Guy's avatar
Wey-Yi Guy committed
44
static inline u32 iwlagn_get_scd_ssn(struct iwlagn_tx_resp *tx_resp)
45
46
47
48
49
{
	return le32_to_cpup((__le32 *)&tx_resp->status +
			    tx_resp->frame_count) & MAX_SN;
}

50
51
52
53
54
55
static void iwlagn_count_tx_err_status(struct iwl_priv *priv, u16 status)
{
	status &= TX_STATUS_MSK;

	switch (status) {
	case TX_STATUS_POSTPONE_DELAY:
56
		priv->reply_tx_stats.pp_delay++;
57
58
		break;
	case TX_STATUS_POSTPONE_FEW_BYTES:
59
		priv->reply_tx_stats.pp_few_bytes++;
60
61
		break;
	case TX_STATUS_POSTPONE_BT_PRIO:
62
		priv->reply_tx_stats.pp_bt_prio++;
63
64
		break;
	case TX_STATUS_POSTPONE_QUIET_PERIOD:
65
		priv->reply_tx_stats.pp_quiet_period++;
66
67
		break;
	case TX_STATUS_POSTPONE_CALC_TTAK:
68
		priv->reply_tx_stats.pp_calc_ttak++;
69
70
		break;
	case TX_STATUS_FAIL_INTERNAL_CROSSED_RETRY:
71
		priv->reply_tx_stats.int_crossed_retry++;
72
73
		break;
	case TX_STATUS_FAIL_SHORT_LIMIT:
74
		priv->reply_tx_stats.short_limit++;
75
76
		break;
	case TX_STATUS_FAIL_LONG_LIMIT:
77
		priv->reply_tx_stats.long_limit++;
78
79
		break;
	case TX_STATUS_FAIL_FIFO_UNDERRUN:
80
		priv->reply_tx_stats.fifo_underrun++;
81
82
		break;
	case TX_STATUS_FAIL_DRAIN_FLOW:
83
		priv->reply_tx_stats.drain_flow++;
84
85
		break;
	case TX_STATUS_FAIL_RFKILL_FLUSH:
86
		priv->reply_tx_stats.rfkill_flush++;
87
88
		break;
	case TX_STATUS_FAIL_LIFE_EXPIRE:
89
		priv->reply_tx_stats.life_expire++;
90
91
		break;
	case TX_STATUS_FAIL_DEST_PS:
92
		priv->reply_tx_stats.dest_ps++;
93
94
		break;
	case TX_STATUS_FAIL_HOST_ABORTED:
95
		priv->reply_tx_stats.host_abort++;
96
97
		break;
	case TX_STATUS_FAIL_BT_RETRY:
98
		priv->reply_tx_stats.bt_retry++;
99
100
		break;
	case TX_STATUS_FAIL_STA_INVALID:
101
		priv->reply_tx_stats.sta_invalid++;
102
103
		break;
	case TX_STATUS_FAIL_FRAG_DROPPED:
104
		priv->reply_tx_stats.frag_drop++;
105
106
		break;
	case TX_STATUS_FAIL_TID_DISABLE:
107
		priv->reply_tx_stats.tid_disable++;
108
109
		break;
	case TX_STATUS_FAIL_FIFO_FLUSHED:
110
		priv->reply_tx_stats.fifo_flush++;
111
112
		break;
	case TX_STATUS_FAIL_INSUFFICIENT_CF_POLL:
113
		priv->reply_tx_stats.insuff_cf_poll++;
114
		break;
115
	case TX_STATUS_FAIL_PASSIVE_NO_RX:
116
		priv->reply_tx_stats.fail_hw_drop++;
117
		break;
118
	case TX_STATUS_FAIL_NO_BEACON_ON_RADAR:
119
		priv->reply_tx_stats.sta_color_mismatch++;
120
121
		break;
	default:
122
		priv->reply_tx_stats.unknown++;
123
124
125
126
		break;
	}
}

127
128
129
130
131
132
static void iwlagn_count_agg_tx_err_status(struct iwl_priv *priv, u16 status)
{
	status &= AGG_TX_STATUS_MSK;

	switch (status) {
	case AGG_TX_STATE_UNDERRUN_MSK:
133
		priv->reply_agg_tx_stats.underrun++;
134
135
		break;
	case AGG_TX_STATE_BT_PRIO_MSK:
136
		priv->reply_agg_tx_stats.bt_prio++;
137
138
		break;
	case AGG_TX_STATE_FEW_BYTES_MSK:
139
		priv->reply_agg_tx_stats.few_bytes++;
140
141
		break;
	case AGG_TX_STATE_ABORT_MSK:
142
		priv->reply_agg_tx_stats.abort++;
143
144
		break;
	case AGG_TX_STATE_LAST_SENT_TTL_MSK:
145
		priv->reply_agg_tx_stats.last_sent_ttl++;
146
147
		break;
	case AGG_TX_STATE_LAST_SENT_TRY_CNT_MSK:
148
		priv->reply_agg_tx_stats.last_sent_try++;
149
150
		break;
	case AGG_TX_STATE_LAST_SENT_BT_KILL_MSK:
151
		priv->reply_agg_tx_stats.last_sent_bt_kill++;
152
153
		break;
	case AGG_TX_STATE_SCD_QUERY_MSK:
154
		priv->reply_agg_tx_stats.scd_query++;
155
156
		break;
	case AGG_TX_STATE_TEST_BAD_CRC32_MSK:
157
		priv->reply_agg_tx_stats.bad_crc32++;
158
159
		break;
	case AGG_TX_STATE_RESPONSE_MSK:
160
		priv->reply_agg_tx_stats.response++;
161
162
		break;
	case AGG_TX_STATE_DUMP_TX_MSK:
163
		priv->reply_agg_tx_stats.dump_tx++;
164
165
		break;
	case AGG_TX_STATE_DELAY_TX_MSK:
166
		priv->reply_agg_tx_stats.delay_tx++;
167
168
		break;
	default:
169
		priv->reply_agg_tx_stats.unknown++;
170
171
172
173
		break;
	}
}

Wey-Yi Guy's avatar
Wey-Yi Guy committed
174
175
static void iwlagn_set_tx_status(struct iwl_priv *priv,
				 struct ieee80211_tx_info *info,
176
				 struct iwl_rxon_context *ctx,
Wey-Yi Guy's avatar
Wey-Yi Guy committed
177
				 struct iwlagn_tx_resp *tx_resp,
Wey-Yi Guy's avatar
Wey-Yi Guy committed
178
179
180
181
182
183
184
185
186
187
				 int txq_id, bool is_agg)
{
	u16  status = le16_to_cpu(tx_resp->status.status);

	info->status.rates[0].count = tx_resp->failure_frame + 1;
	if (is_agg)
		info->flags &= ~IEEE80211_TX_CTL_AMPDU;
	info->flags |= iwl_tx_status_to_mac80211(status);
	iwlagn_hwrate_to_tx_control(priv, le32_to_cpu(tx_resp->rate_n_flags),
				    info);
188
189
	if (!iwl_is_tx_success(status))
		iwlagn_count_tx_err_status(priv, status);
Wey-Yi Guy's avatar
Wey-Yi Guy committed
190

191
192
193
194
195
196
197
	if (status == TX_STATUS_FAIL_PASSIVE_NO_RX &&
	    iwl_is_associated_ctx(ctx) && ctx->vif &&
	    ctx->vif->type == NL80211_IFTYPE_STATION) {
		ctx->last_tx_rejected = true;
		iwl_stop_queue(priv, &priv->txq[txq_id]);
	}

Wey-Yi Guy's avatar
Wey-Yi Guy committed
198
199
200
201
202
203
204
205
	IWL_DEBUG_TX_REPLY(priv, "TXQ %d status %s (0x%08x) rate_n_flags "
			   "0x%x retries %d\n",
			   txq_id,
			   iwl_get_tx_fail_reason(status), status,
			   le32_to_cpu(tx_resp->rate_n_flags),
			   tx_resp->failure_frame);
}

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
232
#ifdef CONFIG_IWLWIFI_DEBUG
#define AGG_TX_STATE_FAIL(x) case AGG_TX_STATE_ ## x: return #x

const char *iwl_get_agg_tx_fail_reason(u16 status)
{
	status &= AGG_TX_STATUS_MSK;
	switch (status) {
	case AGG_TX_STATE_TRANSMITTED:
		return "SUCCESS";
		AGG_TX_STATE_FAIL(UNDERRUN_MSK);
		AGG_TX_STATE_FAIL(BT_PRIO_MSK);
		AGG_TX_STATE_FAIL(FEW_BYTES_MSK);
		AGG_TX_STATE_FAIL(ABORT_MSK);
		AGG_TX_STATE_FAIL(LAST_SENT_TTL_MSK);
		AGG_TX_STATE_FAIL(LAST_SENT_TRY_CNT_MSK);
		AGG_TX_STATE_FAIL(LAST_SENT_BT_KILL_MSK);
		AGG_TX_STATE_FAIL(SCD_QUERY_MSK);
		AGG_TX_STATE_FAIL(TEST_BAD_CRC32_MSK);
		AGG_TX_STATE_FAIL(RESPONSE_MSK);
		AGG_TX_STATE_FAIL(DUMP_TX_MSK);
		AGG_TX_STATE_FAIL(DELAY_TX_MSK);
	}

	return "UNKNOWN";
}
#endif /* CONFIG_IWLWIFI_DEBUG */

233
234
static int iwlagn_tx_status_reply_tx(struct iwl_priv *priv,
				      struct iwl_ht_agg *agg,
Wey-Yi Guy's avatar
Wey-Yi Guy committed
235
				      struct iwlagn_tx_resp *tx_resp,
236
237
238
239
240
241
242
243
244
245
246
247
248
				      int txq_id, u16 start_idx)
{
	u16 status;
	struct agg_tx_status *frame_status = &tx_resp->status;
	struct ieee80211_hdr *hdr = NULL;
	int i, sh, idx;
	u16 seq;

	if (agg->wait_for_ba)
		IWL_DEBUG_TX_REPLY(priv, "got tx response w/o block-ack\n");

	agg->frame_count = tx_resp->frame_count;
	agg->start_idx = start_idx;
Wey-Yi Guy's avatar
Wey-Yi Guy committed
249
	agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
250
251
252
253
	agg->bitmap = 0;

	/* # frames attempted by Tx command */
	if (agg->frame_count == 1) {
254
255
		struct iwl_tx_info *txb;

256
257
258
259
260
		/* Only one frame was attempted; no block-ack will arrive */
		idx = start_idx;

		IWL_DEBUG_TX_REPLY(priv, "FrameCnt = %d, StartIdx=%d idx=%d\n",
				   agg->frame_count, agg->start_idx, idx);
261
262
263
		txb = &priv->txq[txq_id].txb[idx];
		iwlagn_set_tx_status(priv, IEEE80211_SKB_CB(txb->skb),
				     txb->ctx, tx_resp, txq_id, true);
264
265
266
267
		agg->wait_for_ba = 0;
	} else {
		/* Two or more frames were attempted; expect block-ack */
		u64 bitmap = 0;
268
269
270
271
272
273

		/*
		 * Start is the lowest frame sent. It may not be the first
		 * frame in the batch; we figure this out dynamically during
		 * the following loop.
		 */
274
275
276
277
278
279
280
281
282
283
		int start = agg->start_idx;

		/* Construct bit-map of pending frames within Tx window */
		for (i = 0; i < agg->frame_count; i++) {
			u16 sc;
			status = le16_to_cpu(frame_status[i].status);
			seq  = le16_to_cpu(frame_status[i].sequence);
			idx = SEQ_TO_INDEX(seq);
			txq_id = SEQ_TO_QUEUE(seq);

284
285
286
			if (status & AGG_TX_STATUS_MSK)
				iwlagn_count_agg_tx_err_status(priv, status);

287
288
289
290
291
292
			if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
				      AGG_TX_STATE_ABORT_MSK))
				continue;

			IWL_DEBUG_TX_REPLY(priv, "FrameCnt = %d, txq_id=%d idx=%d\n",
					   agg->frame_count, txq_id, idx);
293
294
295
296
297
			IWL_DEBUG_TX_REPLY(priv, "status %s (0x%08x), "
					   "try-count (0x%08x)\n",
					   iwl_get_agg_tx_fail_reason(status),
					   status & AGG_TX_STATUS_MSK,
					   status & AGG_TX_TRY_MSK);
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319

			hdr = iwl_tx_queue_get_hdr(priv, txq_id, idx);
			if (!hdr) {
				IWL_ERR(priv,
					"BUG_ON idx doesn't point to valid skb"
					" idx=%d, txq_id=%d\n", idx, txq_id);
				return -1;
			}

			sc = le16_to_cpu(hdr->seq_ctrl);
			if (idx != (SEQ_TO_SN(sc) & 0xff)) {
				IWL_ERR(priv,
					"BUG_ON idx doesn't match seq control"
					" idx=%d, seq_idx=%d, seq=%d\n",
					  idx, SEQ_TO_SN(sc),
					  hdr->seq_ctrl);
				return -1;
			}

			IWL_DEBUG_TX_REPLY(priv, "AGG Frame i=%d idx %d seq=%d\n",
					   i, idx, SEQ_TO_SN(sc));

320
321
322
323
324
325
326
327
328
			/*
			 * sh -> how many frames ahead of the starting frame is
			 * the current one?
			 *
			 * Note that all frames sent in the batch must be in a
			 * 64-frame window, so this number should be in [0,63].
			 * If outside of this window, then we've found a new
			 * "first" frame in the batch and need to change start.
			 */
329
			sh = idx - start;
330
331
332
333
334
335
336
337
338
339

			/*
			 * If >= 64, out of window. start must be at the front
			 * of the circular buffer, idx must be near the end of
			 * the buffer, and idx is the new "first" frame. Shift
			 * the indices around.
			 */
			if (sh >= 64) {
				/* Shift bitmap by start - idx, wrapped */
				sh = 0x100 - idx + start;
340
				bitmap = bitmap << sh;
341
				/* Now idx is the new start so sh = 0 */
342
343
				sh = 0;
				start = idx;
344
345
346
347
348
349
350
351
352
353
354
355
			/*
			 * If <= -64 then wraps the 256-pkt circular buffer
			 * (e.g., start = 255 and idx = 0, sh should be 1)
			 */
			} else if (sh <= -64) {
				sh  = 0x100 - start + idx;
			/*
			 * If < 0 but > -64, out of window. idx is before start
			 * but not wrapped. Shift the indices around.
			 */
			} else if (sh < 0) {
				/* Shift by how far start is ahead of idx */
356
357
				sh = start - idx;
				bitmap = bitmap << sh;
358
359
				/* Now idx is the new start so sh = 0 */
				start = idx;
360
361
				sh = 0;
			}
362
			/* Sequence number start + sh was sent in this batch */
363
364
365
366
367
			bitmap |= 1ULL << sh;
			IWL_DEBUG_TX_REPLY(priv, "start=%d bitmap=0x%llx\n",
					   start, (unsigned long long)bitmap);
		}

368
369
370
371
		/*
		 * Store the bitmap and possibly the new start, if we wrapped
		 * the buffer above
		 */
372
373
374
375
376
377
378
379
380
381
382
383
		agg->bitmap = bitmap;
		agg->start_idx = start;
		IWL_DEBUG_TX_REPLY(priv, "Frames %d start_idx=%d bitmap=0x%llx\n",
				   agg->frame_count, agg->start_idx,
				   (unsigned long long)agg->bitmap);

		if (bitmap)
			agg->wait_for_ba = 1;
	}
	return 0;
}

384
385
386
387
void iwl_check_abort_status(struct iwl_priv *priv,
			    u8 frame_count, u32 status)
{
	if (frame_count == 1 && status == TX_STATUS_FAIL_RFKILL_FLUSH) {
388
389
390
		IWL_ERR(priv, "Tx flush command to flush out all frames\n");
		if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
			queue_work(priv->workqueue, &priv->tx_flush);
391
392
393
	}
}

394
void iwlagn_rx_reply_tx(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
395
396
397
398
399
400
401
{
	struct iwl_rx_packet *pkt = rxb_addr(rxb);
	u16 sequence = le16_to_cpu(pkt->hdr.sequence);
	int txq_id = SEQ_TO_QUEUE(sequence);
	int index = SEQ_TO_INDEX(sequence);
	struct iwl_tx_queue *txq = &priv->txq[txq_id];
	struct ieee80211_tx_info *info;
Wey-Yi Guy's avatar
Wey-Yi Guy committed
402
	struct iwlagn_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
403
404
	struct iwl_tx_info *txb;
	u32 status = le16_to_cpu(tx_resp->status.status);
405
406
407
	int tid;
	int sta_id;
	int freed;
408
	unsigned long flags;
409
410

	if ((index >= txq->q.n_bd) || (iwl_queue_used(&txq->q, index) == 0)) {
411
412
413
		IWL_ERR(priv, "%s: Read index for DMA queue txq_id (%d) "
			  "index %d is out of range [0-%d] %d %d\n", __func__,
			  txq_id, index, txq->q.n_bd, txq->q.write_ptr,
414
415
416
417
			  txq->q.read_ptr);
		return;
	}

418
	txq->time_stamp = jiffies;
419
420
	txb = &txq->txb[txq->q.read_ptr];
	info = IEEE80211_SKB_CB(txb->skb);
421
422
	memset(&info->status, 0, sizeof(info->status));

Wey-Yi Guy's avatar
Wey-Yi Guy committed
423
424
425
426
	tid = (tx_resp->ra_tid & IWLAGN_TX_RES_TID_MSK) >>
		IWLAGN_TX_RES_TID_POS;
	sta_id = (tx_resp->ra_tid & IWLAGN_TX_RES_RA_MSK) >>
		IWLAGN_TX_RES_RA_POS;
427

428
	spin_lock_irqsave(&priv->sta_lock, flags);
429
430
	if (txq->sched_retry) {
		const u32 scd_ssn = iwlagn_get_scd_ssn(tx_resp);
431
		struct iwl_ht_agg *agg;
432
433

		agg = &priv->stations[sta_id].tid[tid].agg;
434
435
436
437
438
		/*
		 * If the BT kill count is non-zero, we'll get this
		 * notification again.
		 */
		if (tx_resp->bt_kill_count && tx_resp->frame_count == 1 &&
439
440
		    priv->cfg->bt_params &&
		    priv->cfg->bt_params->advanced_bt_coexist) {
441
			IWL_DEBUG_COEX(priv, "receive reply tx with bt_kill\n");
442
		}
443
444
445
446
447
448
449
450
451
452
453
454
		iwlagn_tx_status_reply_tx(priv, agg, tx_resp, txq_id, index);

		/* check if BAR is needed */
		if ((tx_resp->frame_count == 1) && !iwl_is_tx_success(status))
			info->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;

		if (txq->q.read_ptr != (scd_ssn & 0xff)) {
			index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
			IWL_DEBUG_TX_REPLY(priv, "Retry scheduler reclaim "
					"scd_ssn=%d idx=%d txq=%d swq=%d\n",
					scd_ssn , index, txq_id, txq->swq_id);

455
			freed = iwlagn_tx_queue_reclaim(priv, txq_id, index);
456
457
458
459
			iwl_free_tfds_in_queue(priv, sta_id, tid, freed);

			if (priv->mac80211_registered &&
			    (iwl_queue_space(&txq->q) > txq->q.low_mark) &&
460
			    (agg->state != IWL_EMPTYING_HW_QUEUE_DELBA))
461
				iwl_wake_queue(priv, txq);
462
463
		}
	} else {
464
465
		iwlagn_set_tx_status(priv, info, txb->ctx, tx_resp,
				     txq_id, false);
466
		freed = iwlagn_tx_queue_reclaim(priv, txq_id, index);
467
468
469
		iwl_free_tfds_in_queue(priv, sta_id, tid, freed);

		if (priv->mac80211_registered &&
470
471
		    iwl_queue_space(&txq->q) > txq->q.low_mark &&
		    status != TX_STATUS_FAIL_PASSIVE_NO_RX)
472
			iwl_wake_queue(priv, txq);
473
474
	}

475
	iwlagn_txq_check_empty(priv, sta_id, tid, txq_id);
476

477
	iwl_check_abort_status(priv, tx_resp->frame_count, status);
478
	spin_unlock_irqrestore(&priv->sta_lock, flags);
479
480
481
482
483
484
485
486
487
488
}

int iwlagn_hw_valid_rtc_data_addr(u32 addr)
{
	return (addr >= IWLAGN_RTC_DATA_LOWER_BOUND) &&
		(addr < IWLAGN_RTC_DATA_UPPER_BOUND);
}

int iwlagn_send_tx_power(struct iwl_priv *priv)
{
489
	struct iwlagn_tx_power_dbm_cmd tx_power_cmd;
490
491
	u8 tx_ant_cfg_cmd;

492
493
494
495
	if (WARN_ONCE(test_bit(STATUS_SCAN_HW, &priv->status),
		      "TX Power requested while scanning!\n"))
		return -EAGAIN;

496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
	/* half dBm need to multiply */
	tx_power_cmd.global_lmt = (s8)(2 * priv->tx_power_user_lmt);

	if (priv->tx_power_lmt_in_half_dbm &&
	    priv->tx_power_lmt_in_half_dbm < tx_power_cmd.global_lmt) {
		/*
		 * For the newer devices which using enhanced/extend tx power
		 * table in EEPROM, the format is in half dBm. driver need to
		 * convert to dBm format before report to mac80211.
		 * By doing so, there is a possibility of 1/2 dBm resolution
		 * lost. driver will perform "round-up" operation before
		 * reporting, but it will cause 1/2 dBm tx power over the
		 * regulatory limit. Perform the checking here, if the
		 * "tx_power_user_lmt" is higher than EEPROM value (in
		 * half-dBm format), lower the tx power based on EEPROM
		 */
		tx_power_cmd.global_lmt = priv->tx_power_lmt_in_half_dbm;
	}
514
515
	tx_power_cmd.flags = IWLAGN_TX_POWER_NO_CLOSED;
	tx_power_cmd.srv_chan_lmt = IWLAGN_TX_POWER_AUTO;
516
517
518
519
520
521

	if (IWL_UCODE_API(priv->ucode_ver) == 1)
		tx_ant_cfg_cmd = REPLY_TX_POWER_DBM_CMD_V1;
	else
		tx_ant_cfg_cmd = REPLY_TX_POWER_DBM_CMD;

522
	return trans_send_cmd_pdu(&priv->trans, tx_ant_cfg_cmd, CMD_SYNC,
523
			sizeof(tx_power_cmd), &tx_power_cmd);
524
525
526
527
}

void iwlagn_temperature(struct iwl_priv *priv)
{
528
	/* store temperature from correct statistics (in Celsius) */
529
	priv->temperature = le32_to_cpu(priv->statistics.common.temperature);
530
531
532
533
534
535
536
537
538
539
540
541
	iwl_tt_handler(priv);
}

u16 iwlagn_eeprom_calib_version(struct iwl_priv *priv)
{
	struct iwl_eeprom_calib_hdr {
		u8 version;
		u8 pa_type;
		u16 voltage;
	} *hdr;

	hdr = (struct iwl_eeprom_calib_hdr *)iwl_eeprom_query_addr(priv,
542
							EEPROM_CALIB_ALL);
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
	return hdr->version;

}

/*
 * EEPROM
 */
static u32 eeprom_indirect_address(const struct iwl_priv *priv, u32 address)
{
	u16 offset = 0;

	if ((address & INDIRECT_ADDRESS) == 0)
		return address;

	switch (address & INDIRECT_TYPE_MSK) {
	case INDIRECT_HOST:
559
		offset = iwl_eeprom_query16(priv, EEPROM_LINK_HOST);
560
561
		break;
	case INDIRECT_GENERAL:
562
		offset = iwl_eeprom_query16(priv, EEPROM_LINK_GENERAL);
563
564
		break;
	case INDIRECT_REGULATORY:
565
		offset = iwl_eeprom_query16(priv, EEPROM_LINK_REGULATORY);
566
		break;
567
568
569
570
571
572
	case INDIRECT_TXP_LIMIT:
		offset = iwl_eeprom_query16(priv, EEPROM_LINK_TXP_LIMIT);
		break;
	case INDIRECT_TXP_LIMIT_SIZE:
		offset = iwl_eeprom_query16(priv, EEPROM_LINK_TXP_LIMIT_SIZE);
		break;
573
	case INDIRECT_CALIBRATION:
574
		offset = iwl_eeprom_query16(priv, EEPROM_LINK_CALIBRATION);
575
576
		break;
	case INDIRECT_PROCESS_ADJST:
577
		offset = iwl_eeprom_query16(priv, EEPROM_LINK_PROCESS_ADJST);
578
579
		break;
	case INDIRECT_OTHERS:
580
		offset = iwl_eeprom_query16(priv, EEPROM_LINK_OTHERS);
581
582
583
584
585
586
587
588
589
590
591
		break;
	default:
		IWL_ERR(priv, "illegal indirect type: 0x%X\n",
		address & INDIRECT_TYPE_MSK);
		break;
	}

	/* translate the offset from words to byte */
	return (address & ADDRESS_MSK) + (offset << 1);
}

592
const u8 *iwl_eeprom_query_addr(const struct iwl_priv *priv, size_t offset)
593
594
{
	u32 address = eeprom_indirect_address(priv, offset);
595
	BUG_ON(address >= priv->cfg->base_params->eeprom_size);
596
597
	return &priv->eeprom[address];
}
598
599
600
601

struct iwl_mod_params iwlagn_mod_params = {
	.amsdu_size_8K = 1,
	.restart_fw = 1,
602
	.plcp_check = true,
603
	.bt_coex_active = true,
604
	.no_sleep_autoadjust = true,
605
	.power_level = IWL_POWER_INDEX_1,
606
607
	/* the rest are 0 by default */
};
608

609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
int iwlagn_hwrate_to_mac80211_idx(u32 rate_n_flags, enum ieee80211_band band)
{
	int idx = 0;
	int band_offset = 0;

	/* HT rate format: mac80211 wants an MCS number, which is just LSB */
	if (rate_n_flags & RATE_MCS_HT_MSK) {
		idx = (rate_n_flags & 0xff);
		return idx;
	/* Legacy rate format, search for match in table */
	} else {
		if (band == IEEE80211_BAND_5GHZ)
			band_offset = IWL_FIRST_OFDM_RATE;
		for (idx = band_offset; idx < IWL_RATE_COUNT_LEGACY; idx++)
			if (iwl_rates[idx].plcp == (rate_n_flags & 0xFF))
				return idx - band_offset;
	}

	return -1;
}

630
static int iwl_get_single_channel_for_scan(struct iwl_priv *priv,
631
632
633
					   struct ieee80211_vif *vif,
					   enum ieee80211_band band,
					   struct iwl_scan_channel *scan_ch)
634
635
636
637
{
	const struct ieee80211_supported_band *sband;
	u16 passive_dwell = 0;
	u16 active_dwell = 0;
Abhijeet Kolekar's avatar
Abhijeet Kolekar committed
638
	int added = 0;
639
640
641
642
643
644
645
646
647
	u16 channel = 0;

	sband = iwl_get_hw_mode(priv, band);
	if (!sband) {
		IWL_ERR(priv, "invalid band\n");
		return added;
	}

	active_dwell = iwl_get_active_dwell_time(priv, band, 0);
648
	passive_dwell = iwl_get_passive_dwell_time(priv, band, vif);
649
650
651
652

	if (passive_dwell <= active_dwell)
		passive_dwell = active_dwell + 1;

Abhijeet Kolekar's avatar
Abhijeet Kolekar committed
653
	channel = iwl_get_single_channel_number(priv, band);
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
	if (channel) {
		scan_ch->channel = cpu_to_le16(channel);
		scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
		scan_ch->active_dwell = cpu_to_le16(active_dwell);
		scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
		/* Set txpower levels to defaults */
		scan_ch->dsp_atten = 110;
		if (band == IEEE80211_BAND_5GHZ)
			scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
		else
			scan_ch->tx_gain = ((1 << 5) | (5 << 3));
		added++;
	} else
		IWL_ERR(priv, "no valid channel found\n");
	return added;
}

static int iwl_get_channels_for_scan(struct iwl_priv *priv,
672
				     struct ieee80211_vif *vif,
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
				     enum ieee80211_band band,
				     u8 is_active, u8 n_probes,
				     struct iwl_scan_channel *scan_ch)
{
	struct ieee80211_channel *chan;
	const struct ieee80211_supported_band *sband;
	const struct iwl_channel_info *ch_info;
	u16 passive_dwell = 0;
	u16 active_dwell = 0;
	int added, i;
	u16 channel;

	sband = iwl_get_hw_mode(priv, band);
	if (!sband)
		return 0;

	active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
690
	passive_dwell = iwl_get_passive_dwell_time(priv, band, vif);
691
692
693
694
695
696
697
698
699
700

	if (passive_dwell <= active_dwell)
		passive_dwell = active_dwell + 1;

	for (i = 0, added = 0; i < priv->scan_request->n_channels; i++) {
		chan = priv->scan_request->channels[i];

		if (chan->band != band)
			continue;

701
		channel = chan->hw_value;
702
703
704
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
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
		scan_ch->channel = cpu_to_le16(channel);

		ch_info = iwl_get_channel_info(priv, band, channel);
		if (!is_channel_valid(ch_info)) {
			IWL_DEBUG_SCAN(priv, "Channel %d is INVALID for this band.\n",
					channel);
			continue;
		}

		if (!is_active || is_channel_passive(ch_info) ||
		    (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN))
			scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
		else
			scan_ch->type = SCAN_CHANNEL_TYPE_ACTIVE;

		if (n_probes)
			scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);

		scan_ch->active_dwell = cpu_to_le16(active_dwell);
		scan_ch->passive_dwell = cpu_to_le16(passive_dwell);

		/* Set txpower levels to defaults */
		scan_ch->dsp_atten = 110;

		/* NOTE: if we were doing 6Mb OFDM for scans we'd use
		 * power level:
		 * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
		 */
		if (band == IEEE80211_BAND_5GHZ)
			scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
		else
			scan_ch->tx_gain = ((1 << 5) | (5 << 3));

		IWL_DEBUG_SCAN(priv, "Scanning ch=%d prob=0x%X [%s %d]\n",
			       channel, le32_to_cpu(scan_ch->type),
			       (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
				"ACTIVE" : "PASSIVE",
			       (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
			       active_dwell : passive_dwell);

		scan_ch++;
		added++;
	}

	IWL_DEBUG_SCAN(priv, "total channels to scan %d\n", added);
	return added;
}

Johannes Berg's avatar
Johannes Berg committed
750
751
static int iwl_fill_offch_tx(struct iwl_priv *priv, void *data, size_t maxlen)
{
752
	struct sk_buff *skb = priv->offchan_tx_skb;
Johannes Berg's avatar
Johannes Berg committed
753
754
755
756
757
758
759
760
761

	if (skb->len < maxlen)
		maxlen = skb->len;

	memcpy(data, skb->data, maxlen);

	return maxlen;
}

762
int iwlagn_request_scan(struct iwl_priv *priv, struct ieee80211_vif *vif)
763
764
765
{
	struct iwl_host_cmd cmd = {
		.id = REPLY_SCAN_CMD,
766
		.len = { sizeof(struct iwl_scan_cmd), },
767
		.flags = CMD_SYNC,
768
769
	};
	struct iwl_scan_cmd *scan;
770
	struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
771
772
773
774
775
776
777
778
779
780
	u32 rate_flags = 0;
	u16 cmd_len;
	u16 rx_chain = 0;
	enum ieee80211_band band;
	u8 n_probes = 0;
	u8 rx_ant = priv->hw_params.valid_rx_ant;
	u8 rate;
	bool is_active = false;
	int  chan_mod;
	u8 active_chains;
781
	u8 scan_tx_antennas = priv->hw_params.valid_tx_ant;
782
783
784
	int ret;

	lockdep_assert_held(&priv->mutex);
785

786
787
788
	if (vif)
		ctx = iwl_rxon_ctx_from_vif(vif);

789
790
791
792
793
794
	if (!priv->scan_cmd) {
		priv->scan_cmd = kmalloc(sizeof(struct iwl_scan_cmd) +
					 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
		if (!priv->scan_cmd) {
			IWL_DEBUG_SCAN(priv,
				       "fail to allocate memory for scan\n");
795
			return -ENOMEM;
796
797
798
799
800
801
802
803
		}
	}
	scan = priv->scan_cmd;
	memset(scan, 0, sizeof(struct iwl_scan_cmd) + IWL_MAX_SCAN_SIZE);

	scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
	scan->quiet_time = IWL_ACTIVE_QUIET_TIME;

Johannes Berg's avatar
Johannes Berg committed
804
805
	if (priv->scan_type != IWL_SCAN_OFFCH_TX &&
	    iwl_is_any_associated(priv)) {
806
807
808
809
810
811
		u16 interval = 0;
		u32 extra;
		u32 suspend_time = 100;
		u32 scan_suspend_time = 100;

		IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
Johannes Berg's avatar
Johannes Berg committed
812
813
814
815
816
		switch (priv->scan_type) {
		case IWL_SCAN_OFFCH_TX:
			WARN_ON(1);
			break;
		case IWL_SCAN_RADIO_RESET:
817
			interval = 0;
Johannes Berg's avatar
Johannes Berg committed
818
819
			break;
		case IWL_SCAN_NORMAL:
820
			interval = vif->bss_conf.beacon_int;
Johannes Berg's avatar
Johannes Berg committed
821
822
			break;
		}
823
824
825
826
827
828
829
830
831
832
833
834

		scan->suspend_time = 0;
		scan->max_out_time = cpu_to_le32(200 * 1024);
		if (!interval)
			interval = suspend_time;

		extra = (suspend_time / interval) << 22;
		scan_suspend_time = (extra |
		    ((suspend_time % interval) * 1024));
		scan->suspend_time = cpu_to_le32(scan_suspend_time);
		IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
			       scan_suspend_time, interval);
Johannes Berg's avatar
Johannes Berg committed
835
836
837
	} else if (priv->scan_type == IWL_SCAN_OFFCH_TX) {
		scan->suspend_time = 0;
		scan->max_out_time =
838
			cpu_to_le32(1024 * priv->offchan_tx_timeout);
839
840
	}

Johannes Berg's avatar
Johannes Berg committed
841
842
	switch (priv->scan_type) {
	case IWL_SCAN_RADIO_RESET:
843
		IWL_DEBUG_SCAN(priv, "Start internal passive scan.\n");
Johannes Berg's avatar
Johannes Berg committed
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
		break;
	case IWL_SCAN_NORMAL:
		if (priv->scan_request->n_ssids) {
			int i, p = 0;
			IWL_DEBUG_SCAN(priv, "Kicking off active scan\n");
			for (i = 0; i < priv->scan_request->n_ssids; i++) {
				/* always does wildcard anyway */
				if (!priv->scan_request->ssids[i].ssid_len)
					continue;
				scan->direct_scan[p].id = WLAN_EID_SSID;
				scan->direct_scan[p].len =
					priv->scan_request->ssids[i].ssid_len;
				memcpy(scan->direct_scan[p].ssid,
				       priv->scan_request->ssids[i].ssid,
				       priv->scan_request->ssids[i].ssid_len);
				n_probes++;
				p++;
			}
			is_active = true;
		} else
			IWL_DEBUG_SCAN(priv, "Start passive scan.\n");
		break;
	case IWL_SCAN_OFFCH_TX:
		IWL_DEBUG_SCAN(priv, "Start offchannel TX scan.\n");
		break;
	}
870
871

	scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
872
	scan->tx_cmd.sta_id = ctx->bcast_sta_id;
873
874
875
876
877
	scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;

	switch (priv->scan_band) {
	case IEEE80211_BAND_2GHZ:
		scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
878
879
880
		chan_mod = le32_to_cpu(
			priv->contexts[IWL_RXON_CTX_BSS].active.flags &
						RXON_FLG_CHANNEL_MODE_MSK)
881
882
883
884
885
886
887
				       >> RXON_FLG_CHANNEL_MODE_POS;
		if (chan_mod == CHANNEL_MODE_PURE_40) {
			rate = IWL_RATE_6M_PLCP;
		} else {
			rate = IWL_RATE_1M_PLCP;
			rate_flags = RATE_MCS_CCK_MSK;
		}
888
889
890
891
		/*
		 * Internal scans are passive, so we can indiscriminately set
		 * the BT ignore flag on 2.4 GHz since it applies to TX only.
		 */
892
893
		if (priv->cfg->bt_params &&
		    priv->cfg->bt_params->advanced_bt_coexist)
894
			scan->tx_cmd.tx_flags |= TX_CMD_FLG_IGNORE_BT;
895
896
897
898
899
		break;
	case IEEE80211_BAND_5GHZ:
		rate = IWL_RATE_6M_PLCP;
		break;
	default:
900
901
		IWL_WARN(priv, "Invalid scan band\n");
		return -EIO;
902
903
	}

904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
	/*
	 * If active scanning is requested but a certain channel is
	 * marked passive, we can do active scanning if we detect
	 * transmissions.
	 *
	 * There is an issue with some firmware versions that triggers
	 * a sysassert on a "good CRC threshold" of zero (== disabled),
	 * on a radar channel even though this means that we should NOT
	 * send probes.
	 *
	 * The "good CRC threshold" is the number of frames that we
	 * need to receive during our dwell time on a channel before
	 * sending out probes -- setting this to a huge value will
	 * mean we never reach it, but at the same time work around
	 * the aforementioned issue. Thus use IWL_GOOD_CRC_TH_NEVER
	 * here instead of IWL_GOOD_CRC_TH_DISABLED.
920
921
922
923
	 *
	 * This was fixed in later versions along with some other
	 * scan changes, and the threshold behaves as a flag in those
	 * versions.
924
	 */
925
926
927
928
929
930
	if (priv->new_scan_threshold_behaviour)
		scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
						IWL_GOOD_CRC_TH_DISABLED;
	else
		scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
						IWL_GOOD_CRC_TH_NEVER;
931

932
933
	band = priv->scan_band;

934
935
	if (priv->cfg->scan_rx_antennas[band])
		rx_ant = priv->cfg->scan_rx_antennas[band];
936

937
938
939
940
941
	if (band == IEEE80211_BAND_2GHZ &&
	    priv->cfg->bt_params &&
	    priv->cfg->bt_params->advanced_bt_coexist) {
		/* transmit 2.4 GHz probes only on first antenna */
		scan_tx_antennas = first_antenna(scan_tx_antennas);
942
943
	}

944
945
	priv->scan_tx_ant[band] = iwl_toggle_tx_ant(priv, priv->scan_tx_ant[band],
						    scan_tx_antennas);
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
	rate_flags |= iwl_ant_idx_to_flags(priv->scan_tx_ant[band]);
	scan->tx_cmd.rate_n_flags = iwl_hw_set_rate_n_flags(rate, rate_flags);

	/* In power save mode use one chain, otherwise use all chains */
	if (test_bit(STATUS_POWER_PMI, &priv->status)) {
		/* rx_ant has been set to all valid chains previously */
		active_chains = rx_ant &
				((u8)(priv->chain_noise_data.active_chains));
		if (!active_chains)
			active_chains = rx_ant;

		IWL_DEBUG_SCAN(priv, "chain_noise_data.active_chains: %u\n",
				priv->chain_noise_data.active_chains);

		rx_ant = first_antenna(active_chains);
	}
962
963
964
	if (priv->cfg->bt_params &&
	    priv->cfg->bt_params->advanced_bt_coexist &&
	    priv->bt_full_concurrent) {
965
966
967
968
		/* operated as 1x1 in full concurrency mode */
		rx_ant = first_antenna(rx_ant);
	}

969
970
971
972
973
974
	/* MIMO is not used here, but value is required */
	rx_chain |= priv->hw_params.valid_rx_ant << RXON_RX_CHAIN_VALID_POS;
	rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS;
	rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_SEL_POS;
	rx_chain |= 0x1 << RXON_RX_CHAIN_DRIVER_FORCE_POS;
	scan->rx_chain = cpu_to_le16(rx_chain);
Johannes Berg's avatar
Johannes Berg committed
975
976
	switch (priv->scan_type) {
	case IWL_SCAN_NORMAL:
977
978
		cmd_len = iwl_fill_probe_req(priv,
					(struct ieee80211_mgmt *)scan->data,
979
					vif->addr,
980
981
982
					priv->scan_request->ie,
					priv->scan_request->ie_len,
					IWL_MAX_SCAN_SIZE - sizeof(*scan));
Johannes Berg's avatar
Johannes Berg committed
983
984
		break;
	case IWL_SCAN_RADIO_RESET:
985
		/* use bcast addr, will not be transmitted but must be valid */
986
987
		cmd_len = iwl_fill_probe_req(priv,
					(struct ieee80211_mgmt *)scan->data,
988
					iwl_bcast_addr, NULL, 0,
989
					IWL_MAX_SCAN_SIZE - sizeof(*scan));
Johannes Berg's avatar
Johannes Berg committed
990
991
992
993
994
995
996
997
998
999
		break;
	case IWL_SCAN_OFFCH_TX:
		cmd_len = iwl_fill_offch_tx(priv, scan->data,
					    IWL_MAX_SCAN_SIZE
					     - sizeof(*scan)
					     - sizeof(struct iwl_scan_channel));
		scan->scan_flags |= IWL_SCAN_FLAGS_ACTION_FRAME_TX;
		break;
	default:
		BUG();
1000
	}