blob: 5f07bc2e14e9df2e2cc0987f04735875a9d87be3 [file] [log] [blame]
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -07001/******************************************************************************
2 *
3 * GPL LICENSE SUMMARY
4 *
5 * Copyright(c) 2008 - 2010 Intel Corporation. All rights reserved.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
19 * USA
20 *
21 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.GPL.
23 *
24 * Contact Information:
25 * Intel Linux Wireless <ilw@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *
28 *****************************************************************************/
Wey-Yi Guy8d801082010-03-17 13:34:36 -070029#include <linux/etherdevice.h>
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -070030#include <linux/kernel.h>
31#include <linux/module.h>
32#include <linux/init.h>
33#include <linux/sched.h>
34
35#include "iwl-dev.h"
36#include "iwl-core.h"
37#include "iwl-io.h"
38#include "iwl-helpers.h"
39#include "iwl-agn-hw.h"
40#include "iwl-agn.h"
Johannes Berg1fa61b22010-04-28 08:44:52 -070041#include "iwl-sta.h"
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -070042
43static inline u32 iwlagn_get_scd_ssn(struct iwl5000_tx_resp *tx_resp)
44{
45 return le32_to_cpup((__le32 *)&tx_resp->status +
46 tx_resp->frame_count) & MAX_SN;
47}
48
49static int iwlagn_tx_status_reply_tx(struct iwl_priv *priv,
50 struct iwl_ht_agg *agg,
51 struct iwl5000_tx_resp *tx_resp,
52 int txq_id, u16 start_idx)
53{
54 u16 status;
55 struct agg_tx_status *frame_status = &tx_resp->status;
56 struct ieee80211_tx_info *info = NULL;
57 struct ieee80211_hdr *hdr = NULL;
58 u32 rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
59 int i, sh, idx;
60 u16 seq;
61
62 if (agg->wait_for_ba)
63 IWL_DEBUG_TX_REPLY(priv, "got tx response w/o block-ack\n");
64
65 agg->frame_count = tx_resp->frame_count;
66 agg->start_idx = start_idx;
67 agg->rate_n_flags = rate_n_flags;
68 agg->bitmap = 0;
69
70 /* # frames attempted by Tx command */
71 if (agg->frame_count == 1) {
72 /* Only one frame was attempted; no block-ack will arrive */
73 status = le16_to_cpu(frame_status[0].status);
74 idx = start_idx;
75
76 /* FIXME: code repetition */
77 IWL_DEBUG_TX_REPLY(priv, "FrameCnt = %d, StartIdx=%d idx=%d\n",
78 agg->frame_count, agg->start_idx, idx);
79
80 info = IEEE80211_SKB_CB(priv->txq[txq_id].txb[idx].skb[0]);
81 info->status.rates[0].count = tx_resp->failure_frame + 1;
82 info->flags &= ~IEEE80211_TX_CTL_AMPDU;
83 info->flags |= iwl_tx_status_to_mac80211(status);
Wey-Yi Guy8d801082010-03-17 13:34:36 -070084 iwlagn_hwrate_to_tx_control(priv, rate_n_flags, info);
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -070085
86 /* FIXME: code repetition end */
87
88 IWL_DEBUG_TX_REPLY(priv, "1 Frame 0x%x failure :%d\n",
89 status & 0xff, tx_resp->failure_frame);
90 IWL_DEBUG_TX_REPLY(priv, "Rate Info rate_n_flags=%x\n", rate_n_flags);
91
92 agg->wait_for_ba = 0;
93 } else {
94 /* Two or more frames were attempted; expect block-ack */
95 u64 bitmap = 0;
96 int start = agg->start_idx;
97
98 /* Construct bit-map of pending frames within Tx window */
99 for (i = 0; i < agg->frame_count; i++) {
100 u16 sc;
101 status = le16_to_cpu(frame_status[i].status);
102 seq = le16_to_cpu(frame_status[i].sequence);
103 idx = SEQ_TO_INDEX(seq);
104 txq_id = SEQ_TO_QUEUE(seq);
105
106 if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
107 AGG_TX_STATE_ABORT_MSK))
108 continue;
109
110 IWL_DEBUG_TX_REPLY(priv, "FrameCnt = %d, txq_id=%d idx=%d\n",
111 agg->frame_count, txq_id, idx);
112
113 hdr = iwl_tx_queue_get_hdr(priv, txq_id, idx);
114 if (!hdr) {
115 IWL_ERR(priv,
116 "BUG_ON idx doesn't point to valid skb"
117 " idx=%d, txq_id=%d\n", idx, txq_id);
118 return -1;
119 }
120
121 sc = le16_to_cpu(hdr->seq_ctrl);
122 if (idx != (SEQ_TO_SN(sc) & 0xff)) {
123 IWL_ERR(priv,
124 "BUG_ON idx doesn't match seq control"
125 " idx=%d, seq_idx=%d, seq=%d\n",
126 idx, SEQ_TO_SN(sc),
127 hdr->seq_ctrl);
128 return -1;
129 }
130
131 IWL_DEBUG_TX_REPLY(priv, "AGG Frame i=%d idx %d seq=%d\n",
132 i, idx, SEQ_TO_SN(sc));
133
134 sh = idx - start;
135 if (sh > 64) {
136 sh = (start - idx) + 0xff;
137 bitmap = bitmap << sh;
138 sh = 0;
139 start = idx;
140 } else if (sh < -64)
141 sh = 0xff - (start - idx);
142 else if (sh < 0) {
143 sh = start - idx;
144 start = idx;
145 bitmap = bitmap << sh;
146 sh = 0;
147 }
148 bitmap |= 1ULL << sh;
149 IWL_DEBUG_TX_REPLY(priv, "start=%d bitmap=0x%llx\n",
150 start, (unsigned long long)bitmap);
151 }
152
153 agg->bitmap = bitmap;
154 agg->start_idx = start;
155 IWL_DEBUG_TX_REPLY(priv, "Frames %d start_idx=%d bitmap=0x%llx\n",
156 agg->frame_count, agg->start_idx,
157 (unsigned long long)agg->bitmap);
158
159 if (bitmap)
160 agg->wait_for_ba = 1;
161 }
162 return 0;
163}
164
Wey-Yi Guy04569cb2010-03-31 17:57:28 -0700165void iwl_check_abort_status(struct iwl_priv *priv,
166 u8 frame_count, u32 status)
167{
168 if (frame_count == 1 && status == TX_STATUS_FAIL_RFKILL_FLUSH) {
169 IWL_ERR(priv, "TODO: Implement Tx flush command!!!\n");
170 }
171}
172
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700173static void iwlagn_rx_reply_tx(struct iwl_priv *priv,
174 struct iwl_rx_mem_buffer *rxb)
175{
176 struct iwl_rx_packet *pkt = rxb_addr(rxb);
177 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
178 int txq_id = SEQ_TO_QUEUE(sequence);
179 int index = SEQ_TO_INDEX(sequence);
180 struct iwl_tx_queue *txq = &priv->txq[txq_id];
181 struct ieee80211_tx_info *info;
182 struct iwl5000_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
183 u32 status = le16_to_cpu(tx_resp->status.status);
184 int tid;
185 int sta_id;
186 int freed;
187
188 if ((index >= txq->q.n_bd) || (iwl_queue_used(&txq->q, index) == 0)) {
189 IWL_ERR(priv, "Read index for DMA queue txq_id (%d) index %d "
190 "is out of range [0-%d] %d %d\n", txq_id,
191 index, txq->q.n_bd, txq->q.write_ptr,
192 txq->q.read_ptr);
193 return;
194 }
195
196 info = IEEE80211_SKB_CB(txq->txb[txq->q.read_ptr].skb[0]);
197 memset(&info->status, 0, sizeof(info->status));
198
199 tid = (tx_resp->ra_tid & IWL50_TX_RES_TID_MSK) >> IWL50_TX_RES_TID_POS;
200 sta_id = (tx_resp->ra_tid & IWL50_TX_RES_RA_MSK) >> IWL50_TX_RES_RA_POS;
201
202 if (txq->sched_retry) {
203 const u32 scd_ssn = iwlagn_get_scd_ssn(tx_resp);
204 struct iwl_ht_agg *agg = NULL;
205
206 agg = &priv->stations[sta_id].tid[tid].agg;
207
208 iwlagn_tx_status_reply_tx(priv, agg, tx_resp, txq_id, index);
209
210 /* check if BAR is needed */
211 if ((tx_resp->frame_count == 1) && !iwl_is_tx_success(status))
212 info->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
213
214 if (txq->q.read_ptr != (scd_ssn & 0xff)) {
215 index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
216 IWL_DEBUG_TX_REPLY(priv, "Retry scheduler reclaim "
217 "scd_ssn=%d idx=%d txq=%d swq=%d\n",
218 scd_ssn , index, txq_id, txq->swq_id);
219
Wey-Yi Guy74bcdb32010-03-17 13:34:34 -0700220 freed = iwlagn_tx_queue_reclaim(priv, txq_id, index);
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700221 iwl_free_tfds_in_queue(priv, sta_id, tid, freed);
222
223 if (priv->mac80211_registered &&
224 (iwl_queue_space(&txq->q) > txq->q.low_mark) &&
225 (agg->state != IWL_EMPTYING_HW_QUEUE_DELBA)) {
226 if (agg->state == IWL_AGG_OFF)
227 iwl_wake_queue(priv, txq_id);
228 else
229 iwl_wake_queue(priv, txq->swq_id);
230 }
231 }
232 } else {
233 BUG_ON(txq_id != txq->swq_id);
234
235 info->status.rates[0].count = tx_resp->failure_frame + 1;
236 info->flags |= iwl_tx_status_to_mac80211(status);
Wey-Yi Guy8d801082010-03-17 13:34:36 -0700237 iwlagn_hwrate_to_tx_control(priv,
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700238 le32_to_cpu(tx_resp->rate_n_flags),
239 info);
240
241 IWL_DEBUG_TX_REPLY(priv, "TXQ %d status %s (0x%08x) rate_n_flags "
242 "0x%x retries %d\n",
243 txq_id,
244 iwl_get_tx_fail_reason(status), status,
245 le32_to_cpu(tx_resp->rate_n_flags),
246 tx_resp->failure_frame);
247
Wey-Yi Guy74bcdb32010-03-17 13:34:34 -0700248 freed = iwlagn_tx_queue_reclaim(priv, txq_id, index);
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700249 iwl_free_tfds_in_queue(priv, sta_id, tid, freed);
250
251 if (priv->mac80211_registered &&
252 (iwl_queue_space(&txq->q) > txq->q.low_mark))
253 iwl_wake_queue(priv, txq_id);
254 }
255
Wey-Yi Guy74bcdb32010-03-17 13:34:34 -0700256 iwlagn_txq_check_empty(priv, sta_id, tid, txq_id);
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700257
Wey-Yi Guy04569cb2010-03-31 17:57:28 -0700258 iwl_check_abort_status(priv, tx_resp->frame_count, status);
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700259}
260
261void iwlagn_rx_handler_setup(struct iwl_priv *priv)
262{
263 /* init calibration handlers */
264 priv->rx_handlers[CALIBRATION_RES_NOTIFICATION] =
265 iwlagn_rx_calib_result;
266 priv->rx_handlers[CALIBRATION_COMPLETE_NOTIFICATION] =
267 iwlagn_rx_calib_complete;
268 priv->rx_handlers[REPLY_TX] = iwlagn_rx_reply_tx;
269}
270
271void iwlagn_setup_deferred_work(struct iwl_priv *priv)
272{
273 /* in agn, the tx power calibration is done in uCode */
274 priv->disable_tx_power_cal = 1;
275}
276
277int iwlagn_hw_valid_rtc_data_addr(u32 addr)
278{
279 return (addr >= IWLAGN_RTC_DATA_LOWER_BOUND) &&
280 (addr < IWLAGN_RTC_DATA_UPPER_BOUND);
281}
282
283int iwlagn_send_tx_power(struct iwl_priv *priv)
284{
285 struct iwl5000_tx_power_dbm_cmd tx_power_cmd;
286 u8 tx_ant_cfg_cmd;
287
288 /* half dBm need to multiply */
289 tx_power_cmd.global_lmt = (s8)(2 * priv->tx_power_user_lmt);
290
291 if (priv->tx_power_lmt_in_half_dbm &&
292 priv->tx_power_lmt_in_half_dbm < tx_power_cmd.global_lmt) {
293 /*
294 * For the newer devices which using enhanced/extend tx power
295 * table in EEPROM, the format is in half dBm. driver need to
296 * convert to dBm format before report to mac80211.
297 * By doing so, there is a possibility of 1/2 dBm resolution
298 * lost. driver will perform "round-up" operation before
299 * reporting, but it will cause 1/2 dBm tx power over the
300 * regulatory limit. Perform the checking here, if the
301 * "tx_power_user_lmt" is higher than EEPROM value (in
302 * half-dBm format), lower the tx power based on EEPROM
303 */
304 tx_power_cmd.global_lmt = priv->tx_power_lmt_in_half_dbm;
305 }
306 tx_power_cmd.flags = IWL50_TX_POWER_NO_CLOSED;
307 tx_power_cmd.srv_chan_lmt = IWL50_TX_POWER_AUTO;
308
309 if (IWL_UCODE_API(priv->ucode_ver) == 1)
310 tx_ant_cfg_cmd = REPLY_TX_POWER_DBM_CMD_V1;
311 else
312 tx_ant_cfg_cmd = REPLY_TX_POWER_DBM_CMD;
313
314 return iwl_send_cmd_pdu_async(priv, tx_ant_cfg_cmd,
315 sizeof(tx_power_cmd), &tx_power_cmd,
316 NULL);
317}
318
319void iwlagn_temperature(struct iwl_priv *priv)
320{
321 /* store temperature from statistics (in Celsius) */
Wey-Yi Guya2064b72010-04-30 14:21:48 -0700322 priv->temperature =
323 le32_to_cpu(priv->_agn.statistics.general.temperature);
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700324 iwl_tt_handler(priv);
325}
326
327u16 iwlagn_eeprom_calib_version(struct iwl_priv *priv)
328{
329 struct iwl_eeprom_calib_hdr {
330 u8 version;
331 u8 pa_type;
332 u16 voltage;
333 } *hdr;
334
335 hdr = (struct iwl_eeprom_calib_hdr *)iwl_eeprom_query_addr(priv,
Wey-Yi Guy7944f8e2010-04-06 21:10:33 -0700336 EEPROM_CALIB_ALL);
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700337 return hdr->version;
338
339}
340
341/*
342 * EEPROM
343 */
344static u32 eeprom_indirect_address(const struct iwl_priv *priv, u32 address)
345{
346 u16 offset = 0;
347
348 if ((address & INDIRECT_ADDRESS) == 0)
349 return address;
350
351 switch (address & INDIRECT_TYPE_MSK) {
352 case INDIRECT_HOST:
Wey-Yi Guy7944f8e2010-04-06 21:10:33 -0700353 offset = iwl_eeprom_query16(priv, EEPROM_LINK_HOST);
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700354 break;
355 case INDIRECT_GENERAL:
Wey-Yi Guy7944f8e2010-04-06 21:10:33 -0700356 offset = iwl_eeprom_query16(priv, EEPROM_LINK_GENERAL);
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700357 break;
358 case INDIRECT_REGULATORY:
Wey-Yi Guy7944f8e2010-04-06 21:10:33 -0700359 offset = iwl_eeprom_query16(priv, EEPROM_LINK_REGULATORY);
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700360 break;
361 case INDIRECT_CALIBRATION:
Wey-Yi Guy7944f8e2010-04-06 21:10:33 -0700362 offset = iwl_eeprom_query16(priv, EEPROM_LINK_CALIBRATION);
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700363 break;
364 case INDIRECT_PROCESS_ADJST:
Wey-Yi Guy7944f8e2010-04-06 21:10:33 -0700365 offset = iwl_eeprom_query16(priv, EEPROM_LINK_PROCESS_ADJST);
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700366 break;
367 case INDIRECT_OTHERS:
Wey-Yi Guy7944f8e2010-04-06 21:10:33 -0700368 offset = iwl_eeprom_query16(priv, EEPROM_LINK_OTHERS);
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700369 break;
370 default:
371 IWL_ERR(priv, "illegal indirect type: 0x%X\n",
372 address & INDIRECT_TYPE_MSK);
373 break;
374 }
375
376 /* translate the offset from words to byte */
377 return (address & ADDRESS_MSK) + (offset << 1);
378}
379
380const u8 *iwlagn_eeprom_query_addr(const struct iwl_priv *priv,
381 size_t offset)
382{
383 u32 address = eeprom_indirect_address(priv, offset);
384 BUG_ON(address >= priv->cfg->eeprom_size);
385 return &priv->eeprom[address];
386}
Wey-Yi Guy348ee7cd2010-03-16 12:37:27 -0700387
388struct iwl_mod_params iwlagn_mod_params = {
389 .amsdu_size_8K = 1,
390 .restart_fw = 1,
391 /* the rest are 0 by default */
392};
Wey-Yi Guy74bcdb32010-03-17 13:34:34 -0700393
394void iwlagn_rx_queue_reset(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
395{
396 unsigned long flags;
397 int i;
398 spin_lock_irqsave(&rxq->lock, flags);
399 INIT_LIST_HEAD(&rxq->rx_free);
400 INIT_LIST_HEAD(&rxq->rx_used);
401 /* Fill the rx_used queue with _all_ of the Rx buffers */
402 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
403 /* In the reset function, these buffers may have been allocated
404 * to an SKB, so we need to unmap and free potential storage */
405 if (rxq->pool[i].page != NULL) {
406 pci_unmap_page(priv->pci_dev, rxq->pool[i].page_dma,
407 PAGE_SIZE << priv->hw_params.rx_page_order,
408 PCI_DMA_FROMDEVICE);
409 __iwl_free_pages(priv, rxq->pool[i].page);
410 rxq->pool[i].page = NULL;
411 }
412 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
413 }
414
Zhu Yi6aac74b2010-03-22 19:33:41 -0700415 for (i = 0; i < RX_QUEUE_SIZE; i++)
416 rxq->queue[i] = NULL;
417
Wey-Yi Guy74bcdb32010-03-17 13:34:34 -0700418 /* Set us so that we have processed and used all buffers, but have
419 * not restocked the Rx queue with fresh buffers */
420 rxq->read = rxq->write = 0;
421 rxq->write_actual = 0;
422 rxq->free_count = 0;
423 spin_unlock_irqrestore(&rxq->lock, flags);
424}
425
426int iwlagn_rx_init(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
427{
428 u32 rb_size;
429 const u32 rfdnlog = RX_QUEUE_SIZE_LOG; /* 256 RBDs */
430 u32 rb_timeout = 0; /* FIXME: RX_RB_TIMEOUT for all devices? */
431
432 if (!priv->cfg->use_isr_legacy)
433 rb_timeout = RX_RB_TIMEOUT;
434
435 if (priv->cfg->mod_params->amsdu_size_8K)
436 rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_8K;
437 else
438 rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
439
440 /* Stop Rx DMA */
441 iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
442
443 /* Reset driver's Rx queue write index */
444 iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
445
446 /* Tell device where to find RBD circular buffer in DRAM */
447 iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_BASE_REG,
448 (u32)(rxq->dma_addr >> 8));
449
450 /* Tell device where in DRAM to update its Rx status */
451 iwl_write_direct32(priv, FH_RSCSR_CHNL0_STTS_WPTR_REG,
452 rxq->rb_stts_dma >> 4);
453
454 /* Enable Rx DMA
455 * FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY is set because of HW bug in
456 * the credit mechanism in 5000 HW RX FIFO
457 * Direct rx interrupts to hosts
458 * Rx buffer size 4 or 8k
459 * RB timeout 0x10
460 * 256 RBDs
461 */
462 iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG,
463 FH_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
464 FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY |
465 FH_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
466 FH_RCSR_CHNL0_RX_CONFIG_SINGLE_FRAME_MSK |
467 rb_size|
468 (rb_timeout << FH_RCSR_RX_CONFIG_REG_IRQ_RBTH_POS)|
469 (rfdnlog << FH_RCSR_RX_CONFIG_RBDCB_SIZE_POS));
470
471 /* Set interrupt coalescing timer to default (2048 usecs) */
472 iwl_write8(priv, CSR_INT_COALESCING, IWL_HOST_INT_TIMEOUT_DEF);
473
474 return 0;
475}
476
477int iwlagn_hw_nic_init(struct iwl_priv *priv)
478{
479 unsigned long flags;
480 struct iwl_rx_queue *rxq = &priv->rxq;
481 int ret;
482
483 /* nic_init */
484 spin_lock_irqsave(&priv->lock, flags);
485 priv->cfg->ops->lib->apm_ops.init(priv);
486
487 /* Set interrupt coalescing calibration timer to default (512 usecs) */
488 iwl_write8(priv, CSR_INT_COALESCING, IWL_HOST_INT_CALIB_TIMEOUT_DEF);
489
490 spin_unlock_irqrestore(&priv->lock, flags);
491
492 ret = priv->cfg->ops->lib->apm_ops.set_pwr_src(priv, IWL_PWR_SRC_VMAIN);
493
494 priv->cfg->ops->lib->apm_ops.config(priv);
495
496 /* Allocate the RX queue, or reset if it is already allocated */
497 if (!rxq->bd) {
498 ret = iwl_rx_queue_alloc(priv);
499 if (ret) {
500 IWL_ERR(priv, "Unable to initialize Rx queue\n");
501 return -ENOMEM;
502 }
503 } else
504 iwlagn_rx_queue_reset(priv, rxq);
505
Wey-Yi Guy54b81552010-03-17 13:34:35 -0700506 iwlagn_rx_replenish(priv);
Wey-Yi Guy74bcdb32010-03-17 13:34:34 -0700507
508 iwlagn_rx_init(priv, rxq);
509
510 spin_lock_irqsave(&priv->lock, flags);
511
512 rxq->need_update = 1;
513 iwl_rx_queue_update_write_ptr(priv, rxq);
514
515 spin_unlock_irqrestore(&priv->lock, flags);
516
Zhu Yi470058e2010-04-02 13:38:54 -0700517 /* Allocate or reset and init all Tx and Command queues */
518 if (!priv->txq) {
519 ret = iwlagn_txq_ctx_alloc(priv);
520 if (ret)
521 return ret;
522 } else
523 iwlagn_txq_ctx_reset(priv);
Wey-Yi Guy74bcdb32010-03-17 13:34:34 -0700524
525 set_bit(STATUS_INIT, &priv->status);
526
527 return 0;
528}
Wey-Yi Guy54b81552010-03-17 13:34:35 -0700529
530/**
531 * iwlagn_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
532 */
533static inline __le32 iwlagn_dma_addr2rbd_ptr(struct iwl_priv *priv,
534 dma_addr_t dma_addr)
535{
536 return cpu_to_le32((u32)(dma_addr >> 8));
537}
538
539/**
540 * iwlagn_rx_queue_restock - refill RX queue from pre-allocated pool
541 *
542 * If there are slots in the RX queue that need to be restocked,
543 * and we have free pre-allocated buffers, fill the ranks as much
544 * as we can, pulling from rx_free.
545 *
546 * This moves the 'write' index forward to catch up with 'processed', and
547 * also updates the memory address in the firmware to reference the new
548 * target buffer.
549 */
550void iwlagn_rx_queue_restock(struct iwl_priv *priv)
551{
552 struct iwl_rx_queue *rxq = &priv->rxq;
553 struct list_head *element;
554 struct iwl_rx_mem_buffer *rxb;
555 unsigned long flags;
Wey-Yi Guy54b81552010-03-17 13:34:35 -0700556
557 spin_lock_irqsave(&rxq->lock, flags);
Wey-Yi Guy54b81552010-03-17 13:34:35 -0700558 while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Zhu Yi6aac74b2010-03-22 19:33:41 -0700559 /* The overwritten rxb must be a used one */
560 rxb = rxq->queue[rxq->write];
561 BUG_ON(rxb && rxb->page);
562
Wey-Yi Guy54b81552010-03-17 13:34:35 -0700563 /* Get next free Rx buffer, remove from free list */
564 element = rxq->rx_free.next;
565 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
566 list_del(element);
567
568 /* Point to Rx buffer via next RBD in circular buffer */
569 rxq->bd[rxq->write] = iwlagn_dma_addr2rbd_ptr(priv,
570 rxb->page_dma);
571 rxq->queue[rxq->write] = rxb;
572 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
573 rxq->free_count--;
574 }
575 spin_unlock_irqrestore(&rxq->lock, flags);
576 /* If the pre-allocated buffer pool is dropping low, schedule to
577 * refill it */
578 if (rxq->free_count <= RX_LOW_WATERMARK)
579 queue_work(priv->workqueue, &priv->rx_replenish);
580
581
582 /* If we've added more space for the firmware to place data, tell it.
583 * Increment device's write pointer in multiples of 8. */
584 if (rxq->write_actual != (rxq->write & ~0x7)) {
585 spin_lock_irqsave(&rxq->lock, flags);
586 rxq->need_update = 1;
587 spin_unlock_irqrestore(&rxq->lock, flags);
588 iwl_rx_queue_update_write_ptr(priv, rxq);
589 }
590}
591
592/**
593 * iwlagn_rx_replenish - Move all used packet from rx_used to rx_free
594 *
595 * When moving to rx_free an SKB is allocated for the slot.
596 *
597 * Also restock the Rx queue via iwl_rx_queue_restock.
598 * This is called as a scheduled work item (except for during initialization)
599 */
600void iwlagn_rx_allocate(struct iwl_priv *priv, gfp_t priority)
601{
602 struct iwl_rx_queue *rxq = &priv->rxq;
603 struct list_head *element;
604 struct iwl_rx_mem_buffer *rxb;
605 struct page *page;
606 unsigned long flags;
607 gfp_t gfp_mask = priority;
608
609 while (1) {
610 spin_lock_irqsave(&rxq->lock, flags);
611 if (list_empty(&rxq->rx_used)) {
612 spin_unlock_irqrestore(&rxq->lock, flags);
613 return;
614 }
615 spin_unlock_irqrestore(&rxq->lock, flags);
616
617 if (rxq->free_count > RX_LOW_WATERMARK)
618 gfp_mask |= __GFP_NOWARN;
619
620 if (priv->hw_params.rx_page_order > 0)
621 gfp_mask |= __GFP_COMP;
622
623 /* Alloc a new receive buffer */
624 page = alloc_pages(gfp_mask, priv->hw_params.rx_page_order);
625 if (!page) {
626 if (net_ratelimit())
627 IWL_DEBUG_INFO(priv, "alloc_pages failed, "
628 "order: %d\n",
629 priv->hw_params.rx_page_order);
630
631 if ((rxq->free_count <= RX_LOW_WATERMARK) &&
632 net_ratelimit())
633 IWL_CRIT(priv, "Failed to alloc_pages with %s. Only %u free buffers remaining.\n",
634 priority == GFP_ATOMIC ? "GFP_ATOMIC" : "GFP_KERNEL",
635 rxq->free_count);
636 /* We don't reschedule replenish work here -- we will
637 * call the restock method and if it still needs
638 * more buffers it will schedule replenish */
639 return;
640 }
641
642 spin_lock_irqsave(&rxq->lock, flags);
643
644 if (list_empty(&rxq->rx_used)) {
645 spin_unlock_irqrestore(&rxq->lock, flags);
646 __free_pages(page, priv->hw_params.rx_page_order);
647 return;
648 }
649 element = rxq->rx_used.next;
650 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
651 list_del(element);
652
653 spin_unlock_irqrestore(&rxq->lock, flags);
654
Zhu Yi6aac74b2010-03-22 19:33:41 -0700655 BUG_ON(rxb->page);
Wey-Yi Guy54b81552010-03-17 13:34:35 -0700656 rxb->page = page;
657 /* Get physical address of the RB */
658 rxb->page_dma = pci_map_page(priv->pci_dev, page, 0,
659 PAGE_SIZE << priv->hw_params.rx_page_order,
660 PCI_DMA_FROMDEVICE);
661 /* dma address must be no more than 36 bits */
662 BUG_ON(rxb->page_dma & ~DMA_BIT_MASK(36));
663 /* and also 256 byte aligned! */
664 BUG_ON(rxb->page_dma & DMA_BIT_MASK(8));
665
666 spin_lock_irqsave(&rxq->lock, flags);
667
668 list_add_tail(&rxb->list, &rxq->rx_free);
669 rxq->free_count++;
670 priv->alloc_rxb_page++;
671
672 spin_unlock_irqrestore(&rxq->lock, flags);
673 }
674}
675
676void iwlagn_rx_replenish(struct iwl_priv *priv)
677{
678 unsigned long flags;
679
680 iwlagn_rx_allocate(priv, GFP_KERNEL);
681
682 spin_lock_irqsave(&priv->lock, flags);
683 iwlagn_rx_queue_restock(priv);
684 spin_unlock_irqrestore(&priv->lock, flags);
685}
686
687void iwlagn_rx_replenish_now(struct iwl_priv *priv)
688{
689 iwlagn_rx_allocate(priv, GFP_ATOMIC);
690
691 iwlagn_rx_queue_restock(priv);
692}
693
694/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
695 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
696 * This free routine walks the list of POOL entries and if SKB is set to
697 * non NULL it is unmapped and freed
698 */
699void iwlagn_rx_queue_free(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
700{
701 int i;
702 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
703 if (rxq->pool[i].page != NULL) {
704 pci_unmap_page(priv->pci_dev, rxq->pool[i].page_dma,
705 PAGE_SIZE << priv->hw_params.rx_page_order,
706 PCI_DMA_FROMDEVICE);
707 __iwl_free_pages(priv, rxq->pool[i].page);
708 rxq->pool[i].page = NULL;
709 }
710 }
711
712 dma_free_coherent(&priv->pci_dev->dev, 4 * RX_QUEUE_SIZE, rxq->bd,
713 rxq->dma_addr);
714 dma_free_coherent(&priv->pci_dev->dev, sizeof(struct iwl_rb_status),
715 rxq->rb_stts, rxq->rb_stts_dma);
716 rxq->bd = NULL;
717 rxq->rb_stts = NULL;
718}
719
720int iwlagn_rxq_stop(struct iwl_priv *priv)
721{
722
723 /* stop Rx DMA */
724 iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
725 iwl_poll_direct_bit(priv, FH_MEM_RSSR_RX_STATUS_REG,
726 FH_RSSR_CHNL0_RX_STATUS_CHNL_IDLE, 1000);
727
728 return 0;
729}
Wey-Yi Guy8d801082010-03-17 13:34:36 -0700730
731int iwlagn_hwrate_to_mac80211_idx(u32 rate_n_flags, enum ieee80211_band band)
732{
733 int idx = 0;
734 int band_offset = 0;
735
736 /* HT rate format: mac80211 wants an MCS number, which is just LSB */
737 if (rate_n_flags & RATE_MCS_HT_MSK) {
738 idx = (rate_n_flags & 0xff);
739 return idx;
740 /* Legacy rate format, search for match in table */
741 } else {
742 if (band == IEEE80211_BAND_5GHZ)
743 band_offset = IWL_FIRST_OFDM_RATE;
744 for (idx = band_offset; idx < IWL_RATE_COUNT_LEGACY; idx++)
745 if (iwl_rates[idx].plcp == (rate_n_flags & 0xFF))
746 return idx - band_offset;
747 }
748
749 return -1;
750}
751
752/* Calc max signal level (dBm) among 3 possible receivers */
753static inline int iwlagn_calc_rssi(struct iwl_priv *priv,
754 struct iwl_rx_phy_res *rx_resp)
755{
756 return priv->cfg->ops->utils->calc_rssi(priv, rx_resp);
757}
758
759#ifdef CONFIG_IWLWIFI_DEBUG
760/**
761 * iwlagn_dbg_report_frame - dump frame to syslog during debug sessions
762 *
763 * You may hack this function to show different aspects of received frames,
764 * including selective frame dumps.
765 * group100 parameter selects whether to show 1 out of 100 good data frames.
766 * All beacon and probe response frames are printed.
767 */
768static void iwlagn_dbg_report_frame(struct iwl_priv *priv,
769 struct iwl_rx_phy_res *phy_res, u16 length,
770 struct ieee80211_hdr *header, int group100)
771{
772 u32 to_us;
773 u32 print_summary = 0;
774 u32 print_dump = 0; /* set to 1 to dump all frames' contents */
775 u32 hundred = 0;
776 u32 dataframe = 0;
777 __le16 fc;
778 u16 seq_ctl;
779 u16 channel;
780 u16 phy_flags;
781 u32 rate_n_flags;
782 u32 tsf_low;
783 int rssi;
784
785 if (likely(!(iwl_get_debug_level(priv) & IWL_DL_RX)))
786 return;
787
788 /* MAC header */
789 fc = header->frame_control;
790 seq_ctl = le16_to_cpu(header->seq_ctrl);
791
792 /* metadata */
793 channel = le16_to_cpu(phy_res->channel);
794 phy_flags = le16_to_cpu(phy_res->phy_flags);
795 rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);
796
797 /* signal statistics */
798 rssi = iwlagn_calc_rssi(priv, phy_res);
799 tsf_low = le64_to_cpu(phy_res->timestamp) & 0x0ffffffff;
800
801 to_us = !compare_ether_addr(header->addr1, priv->mac_addr);
802
803 /* if data frame is to us and all is good,
804 * (optionally) print summary for only 1 out of every 100 */
805 if (to_us && (fc & ~cpu_to_le16(IEEE80211_FCTL_PROTECTED)) ==
806 cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FTYPE_DATA)) {
807 dataframe = 1;
808 if (!group100)
809 print_summary = 1; /* print each frame */
810 else if (priv->framecnt_to_us < 100) {
811 priv->framecnt_to_us++;
812 print_summary = 0;
813 } else {
814 priv->framecnt_to_us = 0;
815 print_summary = 1;
816 hundred = 1;
817 }
818 } else {
819 /* print summary for all other frames */
820 print_summary = 1;
821 }
822
823 if (print_summary) {
824 char *title;
825 int rate_idx;
826 u32 bitrate;
827
828 if (hundred)
829 title = "100Frames";
830 else if (ieee80211_has_retry(fc))
831 title = "Retry";
832 else if (ieee80211_is_assoc_resp(fc))
833 title = "AscRsp";
834 else if (ieee80211_is_reassoc_resp(fc))
835 title = "RasRsp";
836 else if (ieee80211_is_probe_resp(fc)) {
837 title = "PrbRsp";
838 print_dump = 1; /* dump frame contents */
839 } else if (ieee80211_is_beacon(fc)) {
840 title = "Beacon";
841 print_dump = 1; /* dump frame contents */
842 } else if (ieee80211_is_atim(fc))
843 title = "ATIM";
844 else if (ieee80211_is_auth(fc))
845 title = "Auth";
846 else if (ieee80211_is_deauth(fc))
847 title = "DeAuth";
848 else if (ieee80211_is_disassoc(fc))
849 title = "DisAssoc";
850 else
851 title = "Frame";
852
853 rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
854 if (unlikely((rate_idx < 0) || (rate_idx >= IWL_RATE_COUNT))) {
855 bitrate = 0;
856 WARN_ON_ONCE(1);
857 } else {
858 bitrate = iwl_rates[rate_idx].ieee / 2;
859 }
860
861 /* print frame summary.
862 * MAC addresses show just the last byte (for brevity),
863 * but you can hack it to show more, if you'd like to. */
864 if (dataframe)
865 IWL_DEBUG_RX(priv, "%s: mhd=0x%04x, dst=0x%02x, "
Frans Pop91dd6c22010-03-24 14:19:58 -0700866 "len=%u, rssi=%d, chnl=%d, rate=%u,\n",
Wey-Yi Guy8d801082010-03-17 13:34:36 -0700867 title, le16_to_cpu(fc), header->addr1[5],
868 length, rssi, channel, bitrate);
869 else {
870 /* src/dst addresses assume managed mode */
871 IWL_DEBUG_RX(priv, "%s: 0x%04x, dst=0x%02x, src=0x%02x, "
872 "len=%u, rssi=%d, tim=%lu usec, "
873 "phy=0x%02x, chnl=%d\n",
874 title, le16_to_cpu(fc), header->addr1[5],
875 header->addr3[5], length, rssi,
876 tsf_low - priv->scan_start_tsf,
877 phy_flags, channel);
878 }
879 }
880 if (print_dump)
881 iwl_print_hex_dump(priv, IWL_DL_RX, header, length);
882}
883#endif
884
885static u32 iwlagn_translate_rx_status(struct iwl_priv *priv, u32 decrypt_in)
886{
887 u32 decrypt_out = 0;
888
889 if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
890 RX_RES_STATUS_STATION_FOUND)
891 decrypt_out |= (RX_RES_STATUS_STATION_FOUND |
892 RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
893
894 decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
895
896 /* packet was not encrypted */
897 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
898 RX_RES_STATUS_SEC_TYPE_NONE)
899 return decrypt_out;
900
901 /* packet was encrypted with unknown alg */
902 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
903 RX_RES_STATUS_SEC_TYPE_ERR)
904 return decrypt_out;
905
906 /* decryption was not done in HW */
907 if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
908 RX_MPDU_RES_STATUS_DEC_DONE_MSK)
909 return decrypt_out;
910
911 switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
912
913 case RX_RES_STATUS_SEC_TYPE_CCMP:
914 /* alg is CCM: check MIC only */
915 if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
916 /* Bad MIC */
917 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
918 else
919 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
920
921 break;
922
923 case RX_RES_STATUS_SEC_TYPE_TKIP:
924 if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
925 /* Bad TTAK */
926 decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
927 break;
928 }
929 /* fall through if TTAK OK */
930 default:
931 if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
932 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
933 else
934 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
935 break;
936 };
937
938 IWL_DEBUG_RX(priv, "decrypt_in:0x%x decrypt_out = 0x%x\n",
939 decrypt_in, decrypt_out);
940
941 return decrypt_out;
942}
943
944static void iwlagn_pass_packet_to_mac80211(struct iwl_priv *priv,
945 struct ieee80211_hdr *hdr,
946 u16 len,
947 u32 ampdu_status,
948 struct iwl_rx_mem_buffer *rxb,
949 struct ieee80211_rx_status *stats)
950{
951 struct sk_buff *skb;
Wey-Yi Guy8d801082010-03-17 13:34:36 -0700952 __le16 fc = hdr->frame_control;
953
954 /* We only process data packets if the interface is open */
955 if (unlikely(!priv->is_open)) {
956 IWL_DEBUG_DROP_LIMIT(priv,
957 "Dropping packet while interface is not open.\n");
958 return;
959 }
960
961 /* In case of HW accelerated crypto and bad decryption, drop */
962 if (!priv->cfg->mod_params->sw_crypto &&
963 iwl_set_decrypted_flag(priv, hdr, ampdu_status, stats))
964 return;
965
Zhu Yiecdf94b2010-03-29 16:42:26 +0800966 skb = dev_alloc_skb(128);
Wey-Yi Guy8d801082010-03-17 13:34:36 -0700967 if (!skb) {
Zhu Yiecdf94b2010-03-29 16:42:26 +0800968 IWL_ERR(priv, "dev_alloc_skb failed\n");
Wey-Yi Guy8d801082010-03-17 13:34:36 -0700969 return;
970 }
971
Wey-Yi Guy8d801082010-03-17 13:34:36 -0700972 skb_add_rx_frag(skb, 0, rxb->page, (void *)hdr - rxb_addr(rxb), len);
973
Wey-Yi Guy8d801082010-03-17 13:34:36 -0700974 iwl_update_stats(priv, false, fc, len);
975 memcpy(IEEE80211_SKB_RXCB(skb), stats, sizeof(*stats));
976
977 ieee80211_rx(priv->hw, skb);
Wey-Yi Guy8d801082010-03-17 13:34:36 -0700978 priv->alloc_rxb_page--;
979 rxb->page = NULL;
980}
981
982/* Called for REPLY_RX (legacy ABG frames), or
983 * REPLY_RX_MPDU_CMD (HT high-throughput N frames). */
984void iwlagn_rx_reply_rx(struct iwl_priv *priv,
985 struct iwl_rx_mem_buffer *rxb)
986{
987 struct ieee80211_hdr *header;
988 struct ieee80211_rx_status rx_status;
989 struct iwl_rx_packet *pkt = rxb_addr(rxb);
990 struct iwl_rx_phy_res *phy_res;
991 __le32 rx_pkt_status;
992 struct iwl4965_rx_mpdu_res_start *amsdu;
993 u32 len;
994 u32 ampdu_status;
995 u32 rate_n_flags;
996
997 /**
998 * REPLY_RX and REPLY_RX_MPDU_CMD are handled differently.
999 * REPLY_RX: physical layer info is in this buffer
1000 * REPLY_RX_MPDU_CMD: physical layer info was sent in separate
1001 * command and cached in priv->last_phy_res
1002 *
1003 * Here we set up local variables depending on which command is
1004 * received.
1005 */
1006 if (pkt->hdr.cmd == REPLY_RX) {
1007 phy_res = (struct iwl_rx_phy_res *)pkt->u.raw;
1008 header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*phy_res)
1009 + phy_res->cfg_phy_cnt);
1010
1011 len = le16_to_cpu(phy_res->byte_count);
1012 rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*phy_res) +
1013 phy_res->cfg_phy_cnt + len);
1014 ampdu_status = le32_to_cpu(rx_pkt_status);
1015 } else {
Johannes Berg05d57522010-03-31 08:59:17 -07001016 if (!priv->_agn.last_phy_res_valid) {
Wey-Yi Guy8d801082010-03-17 13:34:36 -07001017 IWL_ERR(priv, "MPDU frame without cached PHY data\n");
1018 return;
1019 }
Johannes Berg05d57522010-03-31 08:59:17 -07001020 phy_res = &priv->_agn.last_phy_res;
Wey-Yi Guy8d801082010-03-17 13:34:36 -07001021 amsdu = (struct iwl4965_rx_mpdu_res_start *)pkt->u.raw;
1022 header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*amsdu));
1023 len = le16_to_cpu(amsdu->byte_count);
1024 rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*amsdu) + len);
1025 ampdu_status = iwlagn_translate_rx_status(priv,
1026 le32_to_cpu(rx_pkt_status));
1027 }
1028
1029 if ((unlikely(phy_res->cfg_phy_cnt > 20))) {
1030 IWL_DEBUG_DROP(priv, "dsp size out of range [0,20]: %d/n",
1031 phy_res->cfg_phy_cnt);
1032 return;
1033 }
1034
1035 if (!(rx_pkt_status & RX_RES_STATUS_NO_CRC32_ERROR) ||
1036 !(rx_pkt_status & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
1037 IWL_DEBUG_RX(priv, "Bad CRC or FIFO: 0x%08X.\n",
1038 le32_to_cpu(rx_pkt_status));
1039 return;
1040 }
1041
1042 /* This will be used in several places later */
1043 rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);
1044
1045 /* rx_status carries information about the packet to mac80211 */
1046 rx_status.mactime = le64_to_cpu(phy_res->timestamp);
1047 rx_status.freq =
1048 ieee80211_channel_to_frequency(le16_to_cpu(phy_res->channel));
1049 rx_status.band = (phy_res->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
1050 IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
1051 rx_status.rate_idx =
1052 iwlagn_hwrate_to_mac80211_idx(rate_n_flags, rx_status.band);
1053 rx_status.flag = 0;
1054
1055 /* TSF isn't reliable. In order to allow smooth user experience,
1056 * this W/A doesn't propagate it to the mac80211 */
1057 /*rx_status.flag |= RX_FLAG_TSFT;*/
1058
1059 priv->ucode_beacon_time = le32_to_cpu(phy_res->beacon_time_stamp);
1060
1061 /* Find max signal strength (dBm) among 3 antenna/receiver chains */
1062 rx_status.signal = iwlagn_calc_rssi(priv, phy_res);
1063
Wey-Yi Guy8d801082010-03-17 13:34:36 -07001064#ifdef CONFIG_IWLWIFI_DEBUG
1065 /* Set "1" to report good data frames in groups of 100 */
1066 if (unlikely(iwl_get_debug_level(priv) & IWL_DL_RX))
1067 iwlagn_dbg_report_frame(priv, phy_res, len, header, 1);
1068#endif
1069 iwl_dbg_log_rx_data_frame(priv, len, header);
Johannes Berged1b6e92010-03-18 09:58:27 -07001070 IWL_DEBUG_STATS_LIMIT(priv, "Rssi %d, TSF %llu\n",
1071 rx_status.signal, (unsigned long long)rx_status.mactime);
Wey-Yi Guy8d801082010-03-17 13:34:36 -07001072
1073 /*
1074 * "antenna number"
1075 *
1076 * It seems that the antenna field in the phy flags value
1077 * is actually a bit field. This is undefined by radiotap,
1078 * it wants an actual antenna number but I always get "7"
1079 * for most legacy frames I receive indicating that the
1080 * same frame was received on all three RX chains.
1081 *
1082 * I think this field should be removed in favor of a
1083 * new 802.11n radiotap field "RX chains" that is defined
1084 * as a bitmask.
1085 */
1086 rx_status.antenna =
1087 (le16_to_cpu(phy_res->phy_flags) & RX_RES_PHY_FLAGS_ANTENNA_MSK)
1088 >> RX_RES_PHY_FLAGS_ANTENNA_POS;
1089
1090 /* set the preamble flag if appropriate */
1091 if (phy_res->phy_flags & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
1092 rx_status.flag |= RX_FLAG_SHORTPRE;
1093
1094 /* Set up the HT phy flags */
1095 if (rate_n_flags & RATE_MCS_HT_MSK)
1096 rx_status.flag |= RX_FLAG_HT;
1097 if (rate_n_flags & RATE_MCS_HT40_MSK)
1098 rx_status.flag |= RX_FLAG_40MHZ;
1099 if (rate_n_flags & RATE_MCS_SGI_MSK)
1100 rx_status.flag |= RX_FLAG_SHORT_GI;
1101
1102 iwlagn_pass_packet_to_mac80211(priv, header, len, ampdu_status,
1103 rxb, &rx_status);
1104}
1105
1106/* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
1107 * This will be used later in iwl_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
1108void iwlagn_rx_reply_rx_phy(struct iwl_priv *priv,
Johannes Berg05d57522010-03-31 08:59:17 -07001109 struct iwl_rx_mem_buffer *rxb)
Wey-Yi Guy8d801082010-03-17 13:34:36 -07001110{
1111 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Johannes Berg05d57522010-03-31 08:59:17 -07001112 priv->_agn.last_phy_res_valid = true;
1113 memcpy(&priv->_agn.last_phy_res, pkt->u.raw,
Wey-Yi Guy8d801082010-03-17 13:34:36 -07001114 sizeof(struct iwl_rx_phy_res));
1115}
Johannes Bergb6e4c552010-04-06 04:12:42 -07001116
1117static int iwl_get_single_channel_for_scan(struct iwl_priv *priv,
Johannes Berg1dda6d22010-04-29 04:43:06 -07001118 struct ieee80211_vif *vif,
1119 enum ieee80211_band band,
1120 struct iwl_scan_channel *scan_ch)
Johannes Bergb6e4c552010-04-06 04:12:42 -07001121{
1122 const struct ieee80211_supported_band *sband;
1123 const struct iwl_channel_info *ch_info;
1124 u16 passive_dwell = 0;
1125 u16 active_dwell = 0;
1126 int i, added = 0;
1127 u16 channel = 0;
1128
1129 sband = iwl_get_hw_mode(priv, band);
1130 if (!sband) {
1131 IWL_ERR(priv, "invalid band\n");
1132 return added;
1133 }
1134
1135 active_dwell = iwl_get_active_dwell_time(priv, band, 0);
Johannes Berg1dda6d22010-04-29 04:43:06 -07001136 passive_dwell = iwl_get_passive_dwell_time(priv, band, vif);
Johannes Bergb6e4c552010-04-06 04:12:42 -07001137
1138 if (passive_dwell <= active_dwell)
1139 passive_dwell = active_dwell + 1;
1140
1141 /* only scan single channel, good enough to reset the RF */
1142 /* pick the first valid not in-use channel */
1143 if (band == IEEE80211_BAND_5GHZ) {
1144 for (i = 14; i < priv->channel_count; i++) {
1145 if (priv->channel_info[i].channel !=
1146 le16_to_cpu(priv->staging_rxon.channel)) {
1147 channel = priv->channel_info[i].channel;
1148 ch_info = iwl_get_channel_info(priv,
1149 band, channel);
1150 if (is_channel_valid(ch_info))
1151 break;
1152 }
1153 }
1154 } else {
1155 for (i = 0; i < 14; i++) {
1156 if (priv->channel_info[i].channel !=
1157 le16_to_cpu(priv->staging_rxon.channel)) {
1158 channel =
1159 priv->channel_info[i].channel;
1160 ch_info = iwl_get_channel_info(priv,
1161 band, channel);
1162 if (is_channel_valid(ch_info))
1163 break;
1164 }
1165 }
1166 }
1167 if (channel) {
1168 scan_ch->channel = cpu_to_le16(channel);
1169 scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
1170 scan_ch->active_dwell = cpu_to_le16(active_dwell);
1171 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
1172 /* Set txpower levels to defaults */
1173 scan_ch->dsp_atten = 110;
1174 if (band == IEEE80211_BAND_5GHZ)
1175 scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
1176 else
1177 scan_ch->tx_gain = ((1 << 5) | (5 << 3));
1178 added++;
1179 } else
1180 IWL_ERR(priv, "no valid channel found\n");
1181 return added;
1182}
1183
1184static int iwl_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg1dda6d22010-04-29 04:43:06 -07001185 struct ieee80211_vif *vif,
Johannes Bergb6e4c552010-04-06 04:12:42 -07001186 enum ieee80211_band band,
1187 u8 is_active, u8 n_probes,
1188 struct iwl_scan_channel *scan_ch)
1189{
1190 struct ieee80211_channel *chan;
1191 const struct ieee80211_supported_band *sband;
1192 const struct iwl_channel_info *ch_info;
1193 u16 passive_dwell = 0;
1194 u16 active_dwell = 0;
1195 int added, i;
1196 u16 channel;
1197
1198 sband = iwl_get_hw_mode(priv, band);
1199 if (!sband)
1200 return 0;
1201
1202 active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
Johannes Berg1dda6d22010-04-29 04:43:06 -07001203 passive_dwell = iwl_get_passive_dwell_time(priv, band, vif);
Johannes Bergb6e4c552010-04-06 04:12:42 -07001204
1205 if (passive_dwell <= active_dwell)
1206 passive_dwell = active_dwell + 1;
1207
1208 for (i = 0, added = 0; i < priv->scan_request->n_channels; i++) {
1209 chan = priv->scan_request->channels[i];
1210
1211 if (chan->band != band)
1212 continue;
1213
1214 channel = ieee80211_frequency_to_channel(chan->center_freq);
1215 scan_ch->channel = cpu_to_le16(channel);
1216
1217 ch_info = iwl_get_channel_info(priv, band, channel);
1218 if (!is_channel_valid(ch_info)) {
1219 IWL_DEBUG_SCAN(priv, "Channel %d is INVALID for this band.\n",
1220 channel);
1221 continue;
1222 }
1223
1224 if (!is_active || is_channel_passive(ch_info) ||
1225 (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN))
1226 scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
1227 else
1228 scan_ch->type = SCAN_CHANNEL_TYPE_ACTIVE;
1229
1230 if (n_probes)
1231 scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
1232
1233 scan_ch->active_dwell = cpu_to_le16(active_dwell);
1234 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
1235
1236 /* Set txpower levels to defaults */
1237 scan_ch->dsp_atten = 110;
1238
1239 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
1240 * power level:
1241 * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
1242 */
1243 if (band == IEEE80211_BAND_5GHZ)
1244 scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
1245 else
1246 scan_ch->tx_gain = ((1 << 5) | (5 << 3));
1247
1248 IWL_DEBUG_SCAN(priv, "Scanning ch=%d prob=0x%X [%s %d]\n",
1249 channel, le32_to_cpu(scan_ch->type),
1250 (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
1251 "ACTIVE" : "PASSIVE",
1252 (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
1253 active_dwell : passive_dwell);
1254
1255 scan_ch++;
1256 added++;
1257 }
1258
1259 IWL_DEBUG_SCAN(priv, "total channels to scan %d\n", added);
1260 return added;
1261}
1262
Johannes Berg1dda6d22010-04-29 04:43:06 -07001263void iwlagn_request_scan(struct iwl_priv *priv, struct ieee80211_vif *vif)
Johannes Bergb6e4c552010-04-06 04:12:42 -07001264{
1265 struct iwl_host_cmd cmd = {
1266 .id = REPLY_SCAN_CMD,
1267 .len = sizeof(struct iwl_scan_cmd),
1268 .flags = CMD_SIZE_HUGE,
1269 };
1270 struct iwl_scan_cmd *scan;
1271 struct ieee80211_conf *conf = NULL;
1272 u32 rate_flags = 0;
1273 u16 cmd_len;
1274 u16 rx_chain = 0;
1275 enum ieee80211_band band;
1276 u8 n_probes = 0;
1277 u8 rx_ant = priv->hw_params.valid_rx_ant;
1278 u8 rate;
1279 bool is_active = false;
1280 int chan_mod;
1281 u8 active_chains;
1282
1283 conf = ieee80211_get_hw_conf(priv->hw);
1284
1285 cancel_delayed_work(&priv->scan_check);
1286
1287 if (!iwl_is_ready(priv)) {
1288 IWL_WARN(priv, "request scan called when driver not ready.\n");
1289 goto done;
1290 }
1291
1292 /* Make sure the scan wasn't canceled before this queued work
1293 * was given the chance to run... */
1294 if (!test_bit(STATUS_SCANNING, &priv->status))
1295 goto done;
1296
1297 /* This should never be called or scheduled if there is currently
1298 * a scan active in the hardware. */
1299 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
1300 IWL_DEBUG_INFO(priv, "Multiple concurrent scan requests in parallel. "
1301 "Ignoring second request.\n");
1302 goto done;
1303 }
1304
1305 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
1306 IWL_DEBUG_SCAN(priv, "Aborting scan due to device shutdown\n");
1307 goto done;
1308 }
1309
1310 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1311 IWL_DEBUG_HC(priv, "Scan request while abort pending. Queuing.\n");
1312 goto done;
1313 }
1314
1315 if (iwl_is_rfkill(priv)) {
1316 IWL_DEBUG_HC(priv, "Aborting scan due to RF Kill activation\n");
1317 goto done;
1318 }
1319
1320 if (!test_bit(STATUS_READY, &priv->status)) {
1321 IWL_DEBUG_HC(priv, "Scan request while uninitialized. Queuing.\n");
1322 goto done;
1323 }
1324
1325 if (!priv->scan_cmd) {
1326 priv->scan_cmd = kmalloc(sizeof(struct iwl_scan_cmd) +
1327 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
1328 if (!priv->scan_cmd) {
1329 IWL_DEBUG_SCAN(priv,
1330 "fail to allocate memory for scan\n");
1331 goto done;
1332 }
1333 }
1334 scan = priv->scan_cmd;
1335 memset(scan, 0, sizeof(struct iwl_scan_cmd) + IWL_MAX_SCAN_SIZE);
1336
1337 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
1338 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
1339
1340 if (iwl_is_associated(priv)) {
1341 u16 interval = 0;
1342 u32 extra;
1343 u32 suspend_time = 100;
1344 u32 scan_suspend_time = 100;
1345 unsigned long flags;
1346
1347 IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
1348 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg1dda6d22010-04-29 04:43:06 -07001349 interval = vif ? vif->bss_conf.beacon_int : 0;
Johannes Bergb6e4c552010-04-06 04:12:42 -07001350 spin_unlock_irqrestore(&priv->lock, flags);
1351
1352 scan->suspend_time = 0;
1353 scan->max_out_time = cpu_to_le32(200 * 1024);
1354 if (!interval)
1355 interval = suspend_time;
1356
1357 extra = (suspend_time / interval) << 22;
1358 scan_suspend_time = (extra |
1359 ((suspend_time % interval) * 1024));
1360 scan->suspend_time = cpu_to_le32(scan_suspend_time);
1361 IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
1362 scan_suspend_time, interval);
1363 }
1364
1365 if (priv->is_internal_short_scan) {
1366 IWL_DEBUG_SCAN(priv, "Start internal passive scan.\n");
1367 } else if (priv->scan_request->n_ssids) {
1368 int i, p = 0;
1369 IWL_DEBUG_SCAN(priv, "Kicking off active scan\n");
1370 for (i = 0; i < priv->scan_request->n_ssids; i++) {
1371 /* always does wildcard anyway */
1372 if (!priv->scan_request->ssids[i].ssid_len)
1373 continue;
1374 scan->direct_scan[p].id = WLAN_EID_SSID;
1375 scan->direct_scan[p].len =
1376 priv->scan_request->ssids[i].ssid_len;
1377 memcpy(scan->direct_scan[p].ssid,
1378 priv->scan_request->ssids[i].ssid,
1379 priv->scan_request->ssids[i].ssid_len);
1380 n_probes++;
1381 p++;
1382 }
1383 is_active = true;
1384 } else
1385 IWL_DEBUG_SCAN(priv, "Start passive scan.\n");
1386
1387 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
1388 scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
1389 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
1390
1391 switch (priv->scan_band) {
1392 case IEEE80211_BAND_2GHZ:
1393 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
1394 chan_mod = le32_to_cpu(priv->active_rxon.flags & RXON_FLG_CHANNEL_MODE_MSK)
1395 >> RXON_FLG_CHANNEL_MODE_POS;
1396 if (chan_mod == CHANNEL_MODE_PURE_40) {
1397 rate = IWL_RATE_6M_PLCP;
1398 } else {
1399 rate = IWL_RATE_1M_PLCP;
1400 rate_flags = RATE_MCS_CCK_MSK;
1401 }
Reinette Chatread41ee32010-04-30 15:13:00 -07001402 scan->good_CRC_th = IWL_GOOD_CRC_TH_DISABLED;
Johannes Bergb6e4c552010-04-06 04:12:42 -07001403 break;
1404 case IEEE80211_BAND_5GHZ:
1405 rate = IWL_RATE_6M_PLCP;
1406 /*
Reinette Chatread41ee32010-04-30 15:13:00 -07001407 * If active scanning is requested but a certain channel is
1408 * marked passive, we can do active scanning if we detect
1409 * transmissions.
1410 *
1411 * There is an issue with some firmware versions that triggers
1412 * a sysassert on a "good CRC threshold" of zero (== disabled),
1413 * on a radar channel even though this means that we should NOT
1414 * send probes.
1415 *
1416 * The "good CRC threshold" is the number of frames that we
1417 * need to receive during our dwell time on a channel before
1418 * sending out probes -- setting this to a huge value will
1419 * mean we never reach it, but at the same time work around
1420 * the aforementioned issue. Thus use IWL_GOOD_CRC_TH_NEVER
1421 * here instead of IWL_GOOD_CRC_TH_DISABLED.
Johannes Bergb6e4c552010-04-06 04:12:42 -07001422 */
Reinette Chatread41ee32010-04-30 15:13:00 -07001423 scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
1424 IWL_GOOD_CRC_TH_NEVER;
Johannes Bergb6e4c552010-04-06 04:12:42 -07001425 break;
1426 default:
1427 IWL_WARN(priv, "Invalid scan band count\n");
1428 goto done;
1429 }
1430
1431 band = priv->scan_band;
1432
Johannes Berge7cb4952010-04-13 01:04:35 -07001433 if (priv->cfg->scan_antennas[band])
1434 rx_ant = priv->cfg->scan_antennas[band];
1435
Johannes Bergb6e4c552010-04-06 04:12:42 -07001436 priv->scan_tx_ant[band] =
1437 iwl_toggle_tx_ant(priv, priv->scan_tx_ant[band]);
1438 rate_flags |= iwl_ant_idx_to_flags(priv->scan_tx_ant[band]);
1439 scan->tx_cmd.rate_n_flags = iwl_hw_set_rate_n_flags(rate, rate_flags);
1440
1441 /* In power save mode use one chain, otherwise use all chains */
1442 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
1443 /* rx_ant has been set to all valid chains previously */
1444 active_chains = rx_ant &
1445 ((u8)(priv->chain_noise_data.active_chains));
1446 if (!active_chains)
1447 active_chains = rx_ant;
1448
1449 IWL_DEBUG_SCAN(priv, "chain_noise_data.active_chains: %u\n",
1450 priv->chain_noise_data.active_chains);
1451
1452 rx_ant = first_antenna(active_chains);
1453 }
1454 /* MIMO is not used here, but value is required */
1455 rx_chain |= priv->hw_params.valid_rx_ant << RXON_RX_CHAIN_VALID_POS;
1456 rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS;
1457 rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_SEL_POS;
1458 rx_chain |= 0x1 << RXON_RX_CHAIN_DRIVER_FORCE_POS;
1459 scan->rx_chain = cpu_to_le16(rx_chain);
1460 if (!priv->is_internal_short_scan) {
1461 cmd_len = iwl_fill_probe_req(priv,
1462 (struct ieee80211_mgmt *)scan->data,
1463 priv->scan_request->ie,
1464 priv->scan_request->ie_len,
1465 IWL_MAX_SCAN_SIZE - sizeof(*scan));
1466 } else {
1467 cmd_len = iwl_fill_probe_req(priv,
1468 (struct ieee80211_mgmt *)scan->data,
1469 NULL, 0,
1470 IWL_MAX_SCAN_SIZE - sizeof(*scan));
1471
1472 }
1473 scan->tx_cmd.len = cpu_to_le16(cmd_len);
Johannes Bergb6e4c552010-04-06 04:12:42 -07001474
1475 scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
1476 RXON_FILTER_BCON_AWARE_MSK);
1477
1478 if (priv->is_internal_short_scan) {
1479 scan->channel_count =
Johannes Berg1dda6d22010-04-29 04:43:06 -07001480 iwl_get_single_channel_for_scan(priv, vif, band,
Johannes Bergb6e4c552010-04-06 04:12:42 -07001481 (void *)&scan->data[le16_to_cpu(
1482 scan->tx_cmd.len)]);
1483 } else {
1484 scan->channel_count =
Johannes Berg1dda6d22010-04-29 04:43:06 -07001485 iwl_get_channels_for_scan(priv, vif, band,
Johannes Bergb6e4c552010-04-06 04:12:42 -07001486 is_active, n_probes,
1487 (void *)&scan->data[le16_to_cpu(
1488 scan->tx_cmd.len)]);
1489 }
1490 if (scan->channel_count == 0) {
1491 IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
1492 goto done;
1493 }
1494
1495 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
1496 scan->channel_count * sizeof(struct iwl_scan_channel);
1497 cmd.data = scan;
1498 scan->len = cpu_to_le16(cmd.len);
1499
1500 set_bit(STATUS_SCAN_HW, &priv->status);
1501 if (iwl_send_cmd_sync(priv, &cmd))
1502 goto done;
1503
1504 queue_delayed_work(priv->workqueue, &priv->scan_check,
1505 IWL_SCAN_CHECK_WATCHDOG);
1506
1507 return;
1508
1509 done:
1510 /* Cannot perform scan. Make sure we clear scanning
1511 * bits from status so next scan request can be performed.
1512 * If we don't clear scanning status bit here all next scan
1513 * will fail
1514 */
1515 clear_bit(STATUS_SCAN_HW, &priv->status);
1516 clear_bit(STATUS_SCANNING, &priv->status);
1517 /* inform mac80211 scan aborted */
1518 queue_work(priv->workqueue, &priv->scan_completed);
1519}
Johannes Berg1fa61b22010-04-28 08:44:52 -07001520
1521int iwlagn_manage_ibss_station(struct iwl_priv *priv,
1522 struct ieee80211_vif *vif, bool add)
1523{
Johannes Bergfd1af152010-04-30 11:30:43 -07001524 struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
1525
Johannes Berg1fa61b22010-04-28 08:44:52 -07001526 if (add)
Johannes Berg57f8db82010-04-30 11:30:49 -07001527 return iwl_add_bssid_station(priv, vif->bss_conf.bssid, true,
Johannes Bergfd1af152010-04-30 11:30:43 -07001528 &vif_priv->ibss_bssid_sta_id);
1529 return iwl_remove_station(priv, vif_priv->ibss_bssid_sta_id,
1530 vif->bss_conf.bssid);
Johannes Berg1fa61b22010-04-28 08:44:52 -07001531}
Johannes Berg1ff504e2010-05-03 01:22:42 -07001532
1533void iwl_free_tfds_in_queue(struct iwl_priv *priv,
1534 int sta_id, int tid, int freed)
1535{
1536 if (priv->stations[sta_id].tid[tid].tfds_in_queue >= freed)
1537 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
1538 else {
1539 IWL_DEBUG_TX(priv, "free more than tfds_in_queue (%u:%d)\n",
1540 priv->stations[sta_id].tid[tid].tfds_in_queue,
1541 freed);
1542 priv->stations[sta_id].tid[tid].tfds_in_queue = 0;
1543 }
1544}