blob: a8494d0e285a7207e333fdc69e5c33ee7dbda3bc [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
Wey-Yi Guy91835ba2010-09-05 10:49:41 -070049static void iwlagn_count_tx_err_status(struct iwl_priv *priv, u16 status)
50{
51 status &= TX_STATUS_MSK;
52
53 switch (status) {
54 case TX_STATUS_POSTPONE_DELAY:
55 priv->_agn.reply_tx_stats.pp_delay++;
56 break;
57 case TX_STATUS_POSTPONE_FEW_BYTES:
58 priv->_agn.reply_tx_stats.pp_few_bytes++;
59 break;
60 case TX_STATUS_POSTPONE_BT_PRIO:
61 priv->_agn.reply_tx_stats.pp_bt_prio++;
62 break;
63 case TX_STATUS_POSTPONE_QUIET_PERIOD:
64 priv->_agn.reply_tx_stats.pp_quiet_period++;
65 break;
66 case TX_STATUS_POSTPONE_CALC_TTAK:
67 priv->_agn.reply_tx_stats.pp_calc_ttak++;
68 break;
69 case TX_STATUS_FAIL_INTERNAL_CROSSED_RETRY:
70 priv->_agn.reply_tx_stats.int_crossed_retry++;
71 break;
72 case TX_STATUS_FAIL_SHORT_LIMIT:
73 priv->_agn.reply_tx_stats.short_limit++;
74 break;
75 case TX_STATUS_FAIL_LONG_LIMIT:
76 priv->_agn.reply_tx_stats.long_limit++;
77 break;
78 case TX_STATUS_FAIL_FIFO_UNDERRUN:
79 priv->_agn.reply_tx_stats.fifo_underrun++;
80 break;
81 case TX_STATUS_FAIL_DRAIN_FLOW:
82 priv->_agn.reply_tx_stats.drain_flow++;
83 break;
84 case TX_STATUS_FAIL_RFKILL_FLUSH:
85 priv->_agn.reply_tx_stats.rfkill_flush++;
86 break;
87 case TX_STATUS_FAIL_LIFE_EXPIRE:
88 priv->_agn.reply_tx_stats.life_expire++;
89 break;
90 case TX_STATUS_FAIL_DEST_PS:
91 priv->_agn.reply_tx_stats.dest_ps++;
92 break;
93 case TX_STATUS_FAIL_HOST_ABORTED:
94 priv->_agn.reply_tx_stats.host_abort++;
95 break;
96 case TX_STATUS_FAIL_BT_RETRY:
97 priv->_agn.reply_tx_stats.bt_retry++;
98 break;
99 case TX_STATUS_FAIL_STA_INVALID:
100 priv->_agn.reply_tx_stats.sta_invalid++;
101 break;
102 case TX_STATUS_FAIL_FRAG_DROPPED:
103 priv->_agn.reply_tx_stats.frag_drop++;
104 break;
105 case TX_STATUS_FAIL_TID_DISABLE:
106 priv->_agn.reply_tx_stats.tid_disable++;
107 break;
108 case TX_STATUS_FAIL_FIFO_FLUSHED:
109 priv->_agn.reply_tx_stats.fifo_flush++;
110 break;
111 case TX_STATUS_FAIL_INSUFFICIENT_CF_POLL:
112 priv->_agn.reply_tx_stats.insuff_cf_poll++;
113 break;
114 case TX_STATUS_FAIL_FW_DROP:
115 priv->_agn.reply_tx_stats.fail_hw_drop++;
116 break;
117 case TX_STATUS_FAIL_STA_COLOR_MISMATCH_DROP:
118 priv->_agn.reply_tx_stats.sta_color_mismatch++;
119 break;
120 default:
121 priv->_agn.reply_tx_stats.unknown++;
122 break;
123 }
124}
125
Wey-Yi Guy743e0152010-09-04 09:00:14 -0700126static void iwlagn_set_tx_status(struct iwl_priv *priv,
127 struct ieee80211_tx_info *info,
128 struct iwl5000_tx_resp *tx_resp,
129 int txq_id, bool is_agg)
130{
131 u16 status = le16_to_cpu(tx_resp->status.status);
132
133 info->status.rates[0].count = tx_resp->failure_frame + 1;
134 if (is_agg)
135 info->flags &= ~IEEE80211_TX_CTL_AMPDU;
136 info->flags |= iwl_tx_status_to_mac80211(status);
137 iwlagn_hwrate_to_tx_control(priv, le32_to_cpu(tx_resp->rate_n_flags),
138 info);
Wey-Yi Guy91835ba2010-09-05 10:49:41 -0700139 if (!iwl_is_tx_success(status))
140 iwlagn_count_tx_err_status(priv, status);
Wey-Yi Guy743e0152010-09-04 09:00:14 -0700141
142 IWL_DEBUG_TX_REPLY(priv, "TXQ %d status %s (0x%08x) rate_n_flags "
143 "0x%x retries %d\n",
144 txq_id,
145 iwl_get_tx_fail_reason(status), status,
146 le32_to_cpu(tx_resp->rate_n_flags),
147 tx_resp->failure_frame);
148}
149
Wey-Yi Guye1b3fa02010-09-05 10:49:43 -0700150#ifdef CONFIG_IWLWIFI_DEBUG
151#define AGG_TX_STATE_FAIL(x) case AGG_TX_STATE_ ## x: return #x
152
153const char *iwl_get_agg_tx_fail_reason(u16 status)
154{
155 status &= AGG_TX_STATUS_MSK;
156 switch (status) {
157 case AGG_TX_STATE_TRANSMITTED:
158 return "SUCCESS";
159 AGG_TX_STATE_FAIL(UNDERRUN_MSK);
160 AGG_TX_STATE_FAIL(BT_PRIO_MSK);
161 AGG_TX_STATE_FAIL(FEW_BYTES_MSK);
162 AGG_TX_STATE_FAIL(ABORT_MSK);
163 AGG_TX_STATE_FAIL(LAST_SENT_TTL_MSK);
164 AGG_TX_STATE_FAIL(LAST_SENT_TRY_CNT_MSK);
165 AGG_TX_STATE_FAIL(LAST_SENT_BT_KILL_MSK);
166 AGG_TX_STATE_FAIL(SCD_QUERY_MSK);
167 AGG_TX_STATE_FAIL(TEST_BAD_CRC32_MSK);
168 AGG_TX_STATE_FAIL(RESPONSE_MSK);
169 AGG_TX_STATE_FAIL(DUMP_TX_MSK);
170 AGG_TX_STATE_FAIL(DELAY_TX_MSK);
171 }
172
173 return "UNKNOWN";
174}
175#endif /* CONFIG_IWLWIFI_DEBUG */
176
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700177static int iwlagn_tx_status_reply_tx(struct iwl_priv *priv,
178 struct iwl_ht_agg *agg,
179 struct iwl5000_tx_resp *tx_resp,
180 int txq_id, u16 start_idx)
181{
182 u16 status;
183 struct agg_tx_status *frame_status = &tx_resp->status;
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700184 struct ieee80211_hdr *hdr = NULL;
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700185 int i, sh, idx;
186 u16 seq;
187
188 if (agg->wait_for_ba)
189 IWL_DEBUG_TX_REPLY(priv, "got tx response w/o block-ack\n");
190
191 agg->frame_count = tx_resp->frame_count;
192 agg->start_idx = start_idx;
Wey-Yi Guy743e0152010-09-04 09:00:14 -0700193 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700194 agg->bitmap = 0;
195
196 /* # frames attempted by Tx command */
197 if (agg->frame_count == 1) {
198 /* Only one frame was attempted; no block-ack will arrive */
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700199 idx = start_idx;
200
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700201 IWL_DEBUG_TX_REPLY(priv, "FrameCnt = %d, StartIdx=%d idx=%d\n",
202 agg->frame_count, agg->start_idx, idx);
Wey-Yi Guy743e0152010-09-04 09:00:14 -0700203 iwlagn_set_tx_status(priv,
204 IEEE80211_SKB_CB(
205 priv->txq[txq_id].txb[idx].skb),
206 tx_resp, txq_id, true);
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700207 agg->wait_for_ba = 0;
208 } else {
209 /* Two or more frames were attempted; expect block-ack */
210 u64 bitmap = 0;
Daniel Halperinf668da22010-05-25 10:22:49 -0700211
212 /*
213 * Start is the lowest frame sent. It may not be the first
214 * frame in the batch; we figure this out dynamically during
215 * the following loop.
216 */
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700217 int start = agg->start_idx;
218
219 /* Construct bit-map of pending frames within Tx window */
220 for (i = 0; i < agg->frame_count; i++) {
221 u16 sc;
222 status = le16_to_cpu(frame_status[i].status);
223 seq = le16_to_cpu(frame_status[i].sequence);
224 idx = SEQ_TO_INDEX(seq);
225 txq_id = SEQ_TO_QUEUE(seq);
226
227 if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
228 AGG_TX_STATE_ABORT_MSK))
229 continue;
230
231 IWL_DEBUG_TX_REPLY(priv, "FrameCnt = %d, txq_id=%d idx=%d\n",
232 agg->frame_count, txq_id, idx);
Wey-Yi Guye1b3fa02010-09-05 10:49:43 -0700233 IWL_DEBUG_TX_REPLY(priv, "status %s (0x%08x), "
234 "try-count (0x%08x)\n",
235 iwl_get_agg_tx_fail_reason(status),
236 status & AGG_TX_STATUS_MSK,
237 status & AGG_TX_TRY_MSK);
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700238
239 hdr = iwl_tx_queue_get_hdr(priv, txq_id, idx);
240 if (!hdr) {
241 IWL_ERR(priv,
242 "BUG_ON idx doesn't point to valid skb"
243 " idx=%d, txq_id=%d\n", idx, txq_id);
244 return -1;
245 }
246
247 sc = le16_to_cpu(hdr->seq_ctrl);
248 if (idx != (SEQ_TO_SN(sc) & 0xff)) {
249 IWL_ERR(priv,
250 "BUG_ON idx doesn't match seq control"
251 " idx=%d, seq_idx=%d, seq=%d\n",
252 idx, SEQ_TO_SN(sc),
253 hdr->seq_ctrl);
254 return -1;
255 }
256
257 IWL_DEBUG_TX_REPLY(priv, "AGG Frame i=%d idx %d seq=%d\n",
258 i, idx, SEQ_TO_SN(sc));
259
Daniel Halperinf668da22010-05-25 10:22:49 -0700260 /*
261 * sh -> how many frames ahead of the starting frame is
262 * the current one?
263 *
264 * Note that all frames sent in the batch must be in a
265 * 64-frame window, so this number should be in [0,63].
266 * If outside of this window, then we've found a new
267 * "first" frame in the batch and need to change start.
268 */
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700269 sh = idx - start;
Daniel Halperinf668da22010-05-25 10:22:49 -0700270
271 /*
272 * If >= 64, out of window. start must be at the front
273 * of the circular buffer, idx must be near the end of
274 * the buffer, and idx is the new "first" frame. Shift
275 * the indices around.
276 */
277 if (sh >= 64) {
278 /* Shift bitmap by start - idx, wrapped */
279 sh = 0x100 - idx + start;
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700280 bitmap = bitmap << sh;
Daniel Halperinf668da22010-05-25 10:22:49 -0700281 /* Now idx is the new start so sh = 0 */
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700282 sh = 0;
283 start = idx;
Daniel Halperinf668da22010-05-25 10:22:49 -0700284 /*
285 * If <= -64 then wraps the 256-pkt circular buffer
286 * (e.g., start = 255 and idx = 0, sh should be 1)
287 */
288 } else if (sh <= -64) {
289 sh = 0x100 - start + idx;
290 /*
291 * If < 0 but > -64, out of window. idx is before start
292 * but not wrapped. Shift the indices around.
293 */
294 } else if (sh < 0) {
295 /* Shift by how far start is ahead of idx */
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700296 sh = start - idx;
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700297 bitmap = bitmap << sh;
Daniel Halperinf668da22010-05-25 10:22:49 -0700298 /* Now idx is the new start so sh = 0 */
299 start = idx;
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700300 sh = 0;
301 }
Daniel Halperinf668da22010-05-25 10:22:49 -0700302 /* Sequence number start + sh was sent in this batch */
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700303 bitmap |= 1ULL << sh;
304 IWL_DEBUG_TX_REPLY(priv, "start=%d bitmap=0x%llx\n",
305 start, (unsigned long long)bitmap);
306 }
307
Daniel Halperinf668da22010-05-25 10:22:49 -0700308 /*
309 * Store the bitmap and possibly the new start, if we wrapped
310 * the buffer above
311 */
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700312 agg->bitmap = bitmap;
313 agg->start_idx = start;
314 IWL_DEBUG_TX_REPLY(priv, "Frames %d start_idx=%d bitmap=0x%llx\n",
315 agg->frame_count, agg->start_idx,
316 (unsigned long long)agg->bitmap);
317
318 if (bitmap)
319 agg->wait_for_ba = 1;
320 }
321 return 0;
322}
323
Wey-Yi Guy04569cb2010-03-31 17:57:28 -0700324void iwl_check_abort_status(struct iwl_priv *priv,
325 u8 frame_count, u32 status)
326{
327 if (frame_count == 1 && status == TX_STATUS_FAIL_RFKILL_FLUSH) {
Wey-Yi Guy65550632010-06-24 13:18:35 -0700328 IWL_ERR(priv, "Tx flush command to flush out all frames\n");
329 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
330 queue_work(priv->workqueue, &priv->tx_flush);
Wey-Yi Guy04569cb2010-03-31 17:57:28 -0700331 }
332}
333
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700334static void iwlagn_rx_reply_tx(struct iwl_priv *priv,
335 struct iwl_rx_mem_buffer *rxb)
336{
337 struct iwl_rx_packet *pkt = rxb_addr(rxb);
338 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
339 int txq_id = SEQ_TO_QUEUE(sequence);
340 int index = SEQ_TO_INDEX(sequence);
341 struct iwl_tx_queue *txq = &priv->txq[txq_id];
342 struct ieee80211_tx_info *info;
343 struct iwl5000_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
344 u32 status = le16_to_cpu(tx_resp->status.status);
345 int tid;
346 int sta_id;
347 int freed;
Reinette Chatre9c5ac092010-05-05 02:26:06 -0700348 unsigned long flags;
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700349
350 if ((index >= txq->q.n_bd) || (iwl_queue_used(&txq->q, index) == 0)) {
351 IWL_ERR(priv, "Read index for DMA queue txq_id (%d) index %d "
352 "is out of range [0-%d] %d %d\n", txq_id,
353 index, txq->q.n_bd, txq->q.write_ptr,
354 txq->q.read_ptr);
355 return;
356 }
357
Johannes Bergff0d91c2010-05-17 02:37:34 -0700358 info = IEEE80211_SKB_CB(txq->txb[txq->q.read_ptr].skb);
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700359 memset(&info->status, 0, sizeof(info->status));
360
361 tid = (tx_resp->ra_tid & IWL50_TX_RES_TID_MSK) >> IWL50_TX_RES_TID_POS;
362 sta_id = (tx_resp->ra_tid & IWL50_TX_RES_RA_MSK) >> IWL50_TX_RES_RA_POS;
363
Reinette Chatre9c5ac092010-05-05 02:26:06 -0700364 spin_lock_irqsave(&priv->sta_lock, flags);
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700365 if (txq->sched_retry) {
366 const u32 scd_ssn = iwlagn_get_scd_ssn(tx_resp);
Reinette Chatre9c5ac092010-05-05 02:26:06 -0700367 struct iwl_ht_agg *agg;
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700368
369 agg = &priv->stations[sta_id].tid[tid].agg;
Wey-Yi Guyc6c996b2010-08-23 07:57:06 -0700370 /*
371 * If the BT kill count is non-zero, we'll get this
372 * notification again.
373 */
374 if (tx_resp->bt_kill_count && tx_resp->frame_count == 1 &&
375 priv->cfg->advanced_bt_coexist) {
376 IWL_WARN(priv, "receive reply tx with bt_kill\n");
377 }
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700378 iwlagn_tx_status_reply_tx(priv, agg, tx_resp, txq_id, index);
379
380 /* check if BAR is needed */
381 if ((tx_resp->frame_count == 1) && !iwl_is_tx_success(status))
382 info->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
383
384 if (txq->q.read_ptr != (scd_ssn & 0xff)) {
385 index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
386 IWL_DEBUG_TX_REPLY(priv, "Retry scheduler reclaim "
387 "scd_ssn=%d idx=%d txq=%d swq=%d\n",
388 scd_ssn , index, txq_id, txq->swq_id);
389
Wey-Yi Guy74bcdb32010-03-17 13:34:34 -0700390 freed = iwlagn_tx_queue_reclaim(priv, txq_id, index);
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700391 iwl_free_tfds_in_queue(priv, sta_id, tid, freed);
392
393 if (priv->mac80211_registered &&
394 (iwl_queue_space(&txq->q) > txq->q.low_mark) &&
395 (agg->state != IWL_EMPTYING_HW_QUEUE_DELBA)) {
396 if (agg->state == IWL_AGG_OFF)
397 iwl_wake_queue(priv, txq_id);
398 else
399 iwl_wake_queue(priv, txq->swq_id);
400 }
401 }
402 } else {
403 BUG_ON(txq_id != txq->swq_id);
Wey-Yi Guy743e0152010-09-04 09:00:14 -0700404 iwlagn_set_tx_status(priv, info, tx_resp, txq_id, false);
Wey-Yi Guy74bcdb32010-03-17 13:34:34 -0700405 freed = iwlagn_tx_queue_reclaim(priv, txq_id, index);
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700406 iwl_free_tfds_in_queue(priv, sta_id, tid, freed);
407
408 if (priv->mac80211_registered &&
409 (iwl_queue_space(&txq->q) > txq->q.low_mark))
410 iwl_wake_queue(priv, txq_id);
411 }
412
Wey-Yi Guy74bcdb32010-03-17 13:34:34 -0700413 iwlagn_txq_check_empty(priv, sta_id, tid, txq_id);
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700414
Wey-Yi Guy04569cb2010-03-31 17:57:28 -0700415 iwl_check_abort_status(priv, tx_resp->frame_count, status);
Reinette Chatre9c5ac092010-05-05 02:26:06 -0700416 spin_unlock_irqrestore(&priv->sta_lock, flags);
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700417}
418
419void iwlagn_rx_handler_setup(struct iwl_priv *priv)
420{
421 /* init calibration handlers */
422 priv->rx_handlers[CALIBRATION_RES_NOTIFICATION] =
423 iwlagn_rx_calib_result;
424 priv->rx_handlers[CALIBRATION_COMPLETE_NOTIFICATION] =
425 iwlagn_rx_calib_complete;
426 priv->rx_handlers[REPLY_TX] = iwlagn_rx_reply_tx;
427}
428
429void iwlagn_setup_deferred_work(struct iwl_priv *priv)
430{
431 /* in agn, the tx power calibration is done in uCode */
432 priv->disable_tx_power_cal = 1;
433}
434
435int iwlagn_hw_valid_rtc_data_addr(u32 addr)
436{
437 return (addr >= IWLAGN_RTC_DATA_LOWER_BOUND) &&
438 (addr < IWLAGN_RTC_DATA_UPPER_BOUND);
439}
440
441int iwlagn_send_tx_power(struct iwl_priv *priv)
442{
443 struct iwl5000_tx_power_dbm_cmd tx_power_cmd;
444 u8 tx_ant_cfg_cmd;
445
446 /* half dBm need to multiply */
447 tx_power_cmd.global_lmt = (s8)(2 * priv->tx_power_user_lmt);
448
449 if (priv->tx_power_lmt_in_half_dbm &&
450 priv->tx_power_lmt_in_half_dbm < tx_power_cmd.global_lmt) {
451 /*
452 * For the newer devices which using enhanced/extend tx power
453 * table in EEPROM, the format is in half dBm. driver need to
454 * convert to dBm format before report to mac80211.
455 * By doing so, there is a possibility of 1/2 dBm resolution
456 * lost. driver will perform "round-up" operation before
457 * reporting, but it will cause 1/2 dBm tx power over the
458 * regulatory limit. Perform the checking here, if the
459 * "tx_power_user_lmt" is higher than EEPROM value (in
460 * half-dBm format), lower the tx power based on EEPROM
461 */
462 tx_power_cmd.global_lmt = priv->tx_power_lmt_in_half_dbm;
463 }
464 tx_power_cmd.flags = IWL50_TX_POWER_NO_CLOSED;
465 tx_power_cmd.srv_chan_lmt = IWL50_TX_POWER_AUTO;
466
467 if (IWL_UCODE_API(priv->ucode_ver) == 1)
468 tx_ant_cfg_cmd = REPLY_TX_POWER_DBM_CMD_V1;
469 else
470 tx_ant_cfg_cmd = REPLY_TX_POWER_DBM_CMD;
471
472 return iwl_send_cmd_pdu_async(priv, tx_ant_cfg_cmd,
473 sizeof(tx_power_cmd), &tx_power_cmd,
474 NULL);
475}
476
477void iwlagn_temperature(struct iwl_priv *priv)
478{
479 /* store temperature from statistics (in Celsius) */
Wey-Yi Guyf3aebee2010-06-14 17:09:54 -0700480 priv->temperature =
Wey-Yi Guy325322e2010-07-14 08:07:27 -0700481 le32_to_cpu(priv->_agn.statistics.general.common.temperature);
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700482 iwl_tt_handler(priv);
483}
484
485u16 iwlagn_eeprom_calib_version(struct iwl_priv *priv)
486{
487 struct iwl_eeprom_calib_hdr {
488 u8 version;
489 u8 pa_type;
490 u16 voltage;
491 } *hdr;
492
493 hdr = (struct iwl_eeprom_calib_hdr *)iwl_eeprom_query_addr(priv,
Wey-Yi Guy7944f8e2010-04-06 21:10:33 -0700494 EEPROM_CALIB_ALL);
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700495 return hdr->version;
496
497}
498
499/*
500 * EEPROM
501 */
502static u32 eeprom_indirect_address(const struct iwl_priv *priv, u32 address)
503{
504 u16 offset = 0;
505
506 if ((address & INDIRECT_ADDRESS) == 0)
507 return address;
508
509 switch (address & INDIRECT_TYPE_MSK) {
510 case INDIRECT_HOST:
Wey-Yi Guy7944f8e2010-04-06 21:10:33 -0700511 offset = iwl_eeprom_query16(priv, EEPROM_LINK_HOST);
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700512 break;
513 case INDIRECT_GENERAL:
Wey-Yi Guy7944f8e2010-04-06 21:10:33 -0700514 offset = iwl_eeprom_query16(priv, EEPROM_LINK_GENERAL);
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700515 break;
516 case INDIRECT_REGULATORY:
Wey-Yi Guy7944f8e2010-04-06 21:10:33 -0700517 offset = iwl_eeprom_query16(priv, EEPROM_LINK_REGULATORY);
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700518 break;
519 case INDIRECT_CALIBRATION:
Wey-Yi Guy7944f8e2010-04-06 21:10:33 -0700520 offset = iwl_eeprom_query16(priv, EEPROM_LINK_CALIBRATION);
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700521 break;
522 case INDIRECT_PROCESS_ADJST:
Wey-Yi Guy7944f8e2010-04-06 21:10:33 -0700523 offset = iwl_eeprom_query16(priv, EEPROM_LINK_PROCESS_ADJST);
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700524 break;
525 case INDIRECT_OTHERS:
Wey-Yi Guy7944f8e2010-04-06 21:10:33 -0700526 offset = iwl_eeprom_query16(priv, EEPROM_LINK_OTHERS);
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700527 break;
528 default:
529 IWL_ERR(priv, "illegal indirect type: 0x%X\n",
530 address & INDIRECT_TYPE_MSK);
531 break;
532 }
533
534 /* translate the offset from words to byte */
535 return (address & ADDRESS_MSK) + (offset << 1);
536}
537
538const u8 *iwlagn_eeprom_query_addr(const struct iwl_priv *priv,
539 size_t offset)
540{
541 u32 address = eeprom_indirect_address(priv, offset);
542 BUG_ON(address >= priv->cfg->eeprom_size);
543 return &priv->eeprom[address];
544}
Wey-Yi Guy348ee7c2010-03-16 12:37:27 -0700545
546struct iwl_mod_params iwlagn_mod_params = {
547 .amsdu_size_8K = 1,
548 .restart_fw = 1,
549 /* the rest are 0 by default */
550};
Wey-Yi Guy74bcdb32010-03-17 13:34:34 -0700551
552void iwlagn_rx_queue_reset(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
553{
554 unsigned long flags;
555 int i;
556 spin_lock_irqsave(&rxq->lock, flags);
557 INIT_LIST_HEAD(&rxq->rx_free);
558 INIT_LIST_HEAD(&rxq->rx_used);
559 /* Fill the rx_used queue with _all_ of the Rx buffers */
560 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
561 /* In the reset function, these buffers may have been allocated
562 * to an SKB, so we need to unmap and free potential storage */
563 if (rxq->pool[i].page != NULL) {
564 pci_unmap_page(priv->pci_dev, rxq->pool[i].page_dma,
565 PAGE_SIZE << priv->hw_params.rx_page_order,
566 PCI_DMA_FROMDEVICE);
567 __iwl_free_pages(priv, rxq->pool[i].page);
568 rxq->pool[i].page = NULL;
569 }
570 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
571 }
572
Zhu Yi6aac74b2010-03-22 19:33:41 -0700573 for (i = 0; i < RX_QUEUE_SIZE; i++)
574 rxq->queue[i] = NULL;
575
Wey-Yi Guy74bcdb32010-03-17 13:34:34 -0700576 /* Set us so that we have processed and used all buffers, but have
577 * not restocked the Rx queue with fresh buffers */
578 rxq->read = rxq->write = 0;
579 rxq->write_actual = 0;
580 rxq->free_count = 0;
581 spin_unlock_irqrestore(&rxq->lock, flags);
582}
583
584int iwlagn_rx_init(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
585{
586 u32 rb_size;
587 const u32 rfdnlog = RX_QUEUE_SIZE_LOG; /* 256 RBDs */
588 u32 rb_timeout = 0; /* FIXME: RX_RB_TIMEOUT for all devices? */
589
590 if (!priv->cfg->use_isr_legacy)
591 rb_timeout = RX_RB_TIMEOUT;
592
593 if (priv->cfg->mod_params->amsdu_size_8K)
594 rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_8K;
595 else
596 rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
597
598 /* Stop Rx DMA */
599 iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
600
601 /* Reset driver's Rx queue write index */
602 iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
603
604 /* Tell device where to find RBD circular buffer in DRAM */
605 iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_BASE_REG,
Emmanuel Grumbachd5b25c92010-06-07 13:21:46 -0700606 (u32)(rxq->bd_dma >> 8));
Wey-Yi Guy74bcdb32010-03-17 13:34:34 -0700607
608 /* Tell device where in DRAM to update its Rx status */
609 iwl_write_direct32(priv, FH_RSCSR_CHNL0_STTS_WPTR_REG,
610 rxq->rb_stts_dma >> 4);
611
612 /* Enable Rx DMA
613 * FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY is set because of HW bug in
614 * the credit mechanism in 5000 HW RX FIFO
615 * Direct rx interrupts to hosts
616 * Rx buffer size 4 or 8k
617 * RB timeout 0x10
618 * 256 RBDs
619 */
620 iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG,
621 FH_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
622 FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY |
623 FH_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
624 FH_RCSR_CHNL0_RX_CONFIG_SINGLE_FRAME_MSK |
625 rb_size|
626 (rb_timeout << FH_RCSR_RX_CONFIG_REG_IRQ_RBTH_POS)|
627 (rfdnlog << FH_RCSR_RX_CONFIG_RBDCB_SIZE_POS));
628
629 /* Set interrupt coalescing timer to default (2048 usecs) */
630 iwl_write8(priv, CSR_INT_COALESCING, IWL_HOST_INT_TIMEOUT_DEF);
631
632 return 0;
633}
634
635int iwlagn_hw_nic_init(struct iwl_priv *priv)
636{
637 unsigned long flags;
638 struct iwl_rx_queue *rxq = &priv->rxq;
639 int ret;
640
641 /* nic_init */
642 spin_lock_irqsave(&priv->lock, flags);
643 priv->cfg->ops->lib->apm_ops.init(priv);
644
645 /* Set interrupt coalescing calibration timer to default (512 usecs) */
646 iwl_write8(priv, CSR_INT_COALESCING, IWL_HOST_INT_CALIB_TIMEOUT_DEF);
647
648 spin_unlock_irqrestore(&priv->lock, flags);
649
650 ret = priv->cfg->ops->lib->apm_ops.set_pwr_src(priv, IWL_PWR_SRC_VMAIN);
651
652 priv->cfg->ops->lib->apm_ops.config(priv);
653
654 /* Allocate the RX queue, or reset if it is already allocated */
655 if (!rxq->bd) {
656 ret = iwl_rx_queue_alloc(priv);
657 if (ret) {
658 IWL_ERR(priv, "Unable to initialize Rx queue\n");
659 return -ENOMEM;
660 }
661 } else
662 iwlagn_rx_queue_reset(priv, rxq);
663
Wey-Yi Guy54b81552010-03-17 13:34:35 -0700664 iwlagn_rx_replenish(priv);
Wey-Yi Guy74bcdb32010-03-17 13:34:34 -0700665
666 iwlagn_rx_init(priv, rxq);
667
668 spin_lock_irqsave(&priv->lock, flags);
669
670 rxq->need_update = 1;
671 iwl_rx_queue_update_write_ptr(priv, rxq);
672
673 spin_unlock_irqrestore(&priv->lock, flags);
674
Zhu Yi470058e2010-04-02 13:38:54 -0700675 /* Allocate or reset and init all Tx and Command queues */
676 if (!priv->txq) {
677 ret = iwlagn_txq_ctx_alloc(priv);
678 if (ret)
679 return ret;
680 } else
681 iwlagn_txq_ctx_reset(priv);
Wey-Yi Guy74bcdb32010-03-17 13:34:34 -0700682
683 set_bit(STATUS_INIT, &priv->status);
684
685 return 0;
686}
Wey-Yi Guy54b81552010-03-17 13:34:35 -0700687
688/**
689 * iwlagn_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
690 */
691static inline __le32 iwlagn_dma_addr2rbd_ptr(struct iwl_priv *priv,
692 dma_addr_t dma_addr)
693{
694 return cpu_to_le32((u32)(dma_addr >> 8));
695}
696
697/**
698 * iwlagn_rx_queue_restock - refill RX queue from pre-allocated pool
699 *
700 * If there are slots in the RX queue that need to be restocked,
701 * and we have free pre-allocated buffers, fill the ranks as much
702 * as we can, pulling from rx_free.
703 *
704 * This moves the 'write' index forward to catch up with 'processed', and
705 * also updates the memory address in the firmware to reference the new
706 * target buffer.
707 */
708void iwlagn_rx_queue_restock(struct iwl_priv *priv)
709{
710 struct iwl_rx_queue *rxq = &priv->rxq;
711 struct list_head *element;
712 struct iwl_rx_mem_buffer *rxb;
713 unsigned long flags;
Wey-Yi Guy54b81552010-03-17 13:34:35 -0700714
715 spin_lock_irqsave(&rxq->lock, flags);
Wey-Yi Guy54b81552010-03-17 13:34:35 -0700716 while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Zhu Yi6aac74b2010-03-22 19:33:41 -0700717 /* The overwritten rxb must be a used one */
718 rxb = rxq->queue[rxq->write];
719 BUG_ON(rxb && rxb->page);
720
Wey-Yi Guy54b81552010-03-17 13:34:35 -0700721 /* Get next free Rx buffer, remove from free list */
722 element = rxq->rx_free.next;
723 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
724 list_del(element);
725
726 /* Point to Rx buffer via next RBD in circular buffer */
727 rxq->bd[rxq->write] = iwlagn_dma_addr2rbd_ptr(priv,
728 rxb->page_dma);
729 rxq->queue[rxq->write] = rxb;
730 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
731 rxq->free_count--;
732 }
733 spin_unlock_irqrestore(&rxq->lock, flags);
734 /* If the pre-allocated buffer pool is dropping low, schedule to
735 * refill it */
736 if (rxq->free_count <= RX_LOW_WATERMARK)
737 queue_work(priv->workqueue, &priv->rx_replenish);
738
739
740 /* If we've added more space for the firmware to place data, tell it.
741 * Increment device's write pointer in multiples of 8. */
742 if (rxq->write_actual != (rxq->write & ~0x7)) {
743 spin_lock_irqsave(&rxq->lock, flags);
744 rxq->need_update = 1;
745 spin_unlock_irqrestore(&rxq->lock, flags);
746 iwl_rx_queue_update_write_ptr(priv, rxq);
747 }
748}
749
750/**
751 * iwlagn_rx_replenish - Move all used packet from rx_used to rx_free
752 *
753 * When moving to rx_free an SKB is allocated for the slot.
754 *
755 * Also restock the Rx queue via iwl_rx_queue_restock.
756 * This is called as a scheduled work item (except for during initialization)
757 */
758void iwlagn_rx_allocate(struct iwl_priv *priv, gfp_t priority)
759{
760 struct iwl_rx_queue *rxq = &priv->rxq;
761 struct list_head *element;
762 struct iwl_rx_mem_buffer *rxb;
763 struct page *page;
764 unsigned long flags;
765 gfp_t gfp_mask = priority;
766
767 while (1) {
768 spin_lock_irqsave(&rxq->lock, flags);
769 if (list_empty(&rxq->rx_used)) {
770 spin_unlock_irqrestore(&rxq->lock, flags);
771 return;
772 }
773 spin_unlock_irqrestore(&rxq->lock, flags);
774
775 if (rxq->free_count > RX_LOW_WATERMARK)
776 gfp_mask |= __GFP_NOWARN;
777
778 if (priv->hw_params.rx_page_order > 0)
779 gfp_mask |= __GFP_COMP;
780
781 /* Alloc a new receive buffer */
782 page = alloc_pages(gfp_mask, priv->hw_params.rx_page_order);
783 if (!page) {
784 if (net_ratelimit())
785 IWL_DEBUG_INFO(priv, "alloc_pages failed, "
786 "order: %d\n",
787 priv->hw_params.rx_page_order);
788
789 if ((rxq->free_count <= RX_LOW_WATERMARK) &&
790 net_ratelimit())
791 IWL_CRIT(priv, "Failed to alloc_pages with %s. Only %u free buffers remaining.\n",
792 priority == GFP_ATOMIC ? "GFP_ATOMIC" : "GFP_KERNEL",
793 rxq->free_count);
794 /* We don't reschedule replenish work here -- we will
795 * call the restock method and if it still needs
796 * more buffers it will schedule replenish */
797 return;
798 }
799
800 spin_lock_irqsave(&rxq->lock, flags);
801
802 if (list_empty(&rxq->rx_used)) {
803 spin_unlock_irqrestore(&rxq->lock, flags);
804 __free_pages(page, priv->hw_params.rx_page_order);
805 return;
806 }
807 element = rxq->rx_used.next;
808 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
809 list_del(element);
810
811 spin_unlock_irqrestore(&rxq->lock, flags);
812
Zhu Yi6aac74b2010-03-22 19:33:41 -0700813 BUG_ON(rxb->page);
Wey-Yi Guy54b81552010-03-17 13:34:35 -0700814 rxb->page = page;
815 /* Get physical address of the RB */
816 rxb->page_dma = pci_map_page(priv->pci_dev, page, 0,
817 PAGE_SIZE << priv->hw_params.rx_page_order,
818 PCI_DMA_FROMDEVICE);
819 /* dma address must be no more than 36 bits */
820 BUG_ON(rxb->page_dma & ~DMA_BIT_MASK(36));
821 /* and also 256 byte aligned! */
822 BUG_ON(rxb->page_dma & DMA_BIT_MASK(8));
823
824 spin_lock_irqsave(&rxq->lock, flags);
825
826 list_add_tail(&rxb->list, &rxq->rx_free);
827 rxq->free_count++;
828 priv->alloc_rxb_page++;
829
830 spin_unlock_irqrestore(&rxq->lock, flags);
831 }
832}
833
834void iwlagn_rx_replenish(struct iwl_priv *priv)
835{
836 unsigned long flags;
837
838 iwlagn_rx_allocate(priv, GFP_KERNEL);
839
840 spin_lock_irqsave(&priv->lock, flags);
841 iwlagn_rx_queue_restock(priv);
842 spin_unlock_irqrestore(&priv->lock, flags);
843}
844
845void iwlagn_rx_replenish_now(struct iwl_priv *priv)
846{
847 iwlagn_rx_allocate(priv, GFP_ATOMIC);
848
849 iwlagn_rx_queue_restock(priv);
850}
851
852/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
853 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
854 * This free routine walks the list of POOL entries and if SKB is set to
855 * non NULL it is unmapped and freed
856 */
857void iwlagn_rx_queue_free(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
858{
859 int i;
860 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
861 if (rxq->pool[i].page != NULL) {
862 pci_unmap_page(priv->pci_dev, rxq->pool[i].page_dma,
863 PAGE_SIZE << priv->hw_params.rx_page_order,
864 PCI_DMA_FROMDEVICE);
865 __iwl_free_pages(priv, rxq->pool[i].page);
866 rxq->pool[i].page = NULL;
867 }
868 }
869
870 dma_free_coherent(&priv->pci_dev->dev, 4 * RX_QUEUE_SIZE, rxq->bd,
Emmanuel Grumbachd5b25c92010-06-07 13:21:46 -0700871 rxq->bd_dma);
Wey-Yi Guy54b81552010-03-17 13:34:35 -0700872 dma_free_coherent(&priv->pci_dev->dev, sizeof(struct iwl_rb_status),
873 rxq->rb_stts, rxq->rb_stts_dma);
874 rxq->bd = NULL;
875 rxq->rb_stts = NULL;
876}
877
878int iwlagn_rxq_stop(struct iwl_priv *priv)
879{
880
881 /* stop Rx DMA */
882 iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
883 iwl_poll_direct_bit(priv, FH_MEM_RSSR_RX_STATUS_REG,
884 FH_RSSR_CHNL0_RX_STATUS_CHNL_IDLE, 1000);
885
886 return 0;
887}
Wey-Yi Guy8d801082010-03-17 13:34:36 -0700888
889int iwlagn_hwrate_to_mac80211_idx(u32 rate_n_flags, enum ieee80211_band band)
890{
891 int idx = 0;
892 int band_offset = 0;
893
894 /* HT rate format: mac80211 wants an MCS number, which is just LSB */
895 if (rate_n_flags & RATE_MCS_HT_MSK) {
896 idx = (rate_n_flags & 0xff);
897 return idx;
898 /* Legacy rate format, search for match in table */
899 } else {
900 if (band == IEEE80211_BAND_5GHZ)
901 band_offset = IWL_FIRST_OFDM_RATE;
902 for (idx = band_offset; idx < IWL_RATE_COUNT_LEGACY; idx++)
903 if (iwl_rates[idx].plcp == (rate_n_flags & 0xFF))
904 return idx - band_offset;
905 }
906
907 return -1;
908}
909
910/* Calc max signal level (dBm) among 3 possible receivers */
911static inline int iwlagn_calc_rssi(struct iwl_priv *priv,
912 struct iwl_rx_phy_res *rx_resp)
913{
914 return priv->cfg->ops->utils->calc_rssi(priv, rx_resp);
915}
916
Wey-Yi Guy8d801082010-03-17 13:34:36 -0700917static u32 iwlagn_translate_rx_status(struct iwl_priv *priv, u32 decrypt_in)
918{
919 u32 decrypt_out = 0;
920
921 if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
922 RX_RES_STATUS_STATION_FOUND)
923 decrypt_out |= (RX_RES_STATUS_STATION_FOUND |
924 RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
925
926 decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
927
928 /* packet was not encrypted */
929 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
930 RX_RES_STATUS_SEC_TYPE_NONE)
931 return decrypt_out;
932
933 /* packet was encrypted with unknown alg */
934 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
935 RX_RES_STATUS_SEC_TYPE_ERR)
936 return decrypt_out;
937
938 /* decryption was not done in HW */
939 if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
940 RX_MPDU_RES_STATUS_DEC_DONE_MSK)
941 return decrypt_out;
942
943 switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
944
945 case RX_RES_STATUS_SEC_TYPE_CCMP:
946 /* alg is CCM: check MIC only */
947 if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
948 /* Bad MIC */
949 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
950 else
951 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
952
953 break;
954
955 case RX_RES_STATUS_SEC_TYPE_TKIP:
956 if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
957 /* Bad TTAK */
958 decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
959 break;
960 }
961 /* fall through if TTAK OK */
962 default:
963 if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
964 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
965 else
966 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
967 break;
Joe Perchesee289b62010-05-17 22:47:34 -0700968 }
Wey-Yi Guy8d801082010-03-17 13:34:36 -0700969
970 IWL_DEBUG_RX(priv, "decrypt_in:0x%x decrypt_out = 0x%x\n",
971 decrypt_in, decrypt_out);
972
973 return decrypt_out;
974}
975
976static void iwlagn_pass_packet_to_mac80211(struct iwl_priv *priv,
977 struct ieee80211_hdr *hdr,
978 u16 len,
979 u32 ampdu_status,
980 struct iwl_rx_mem_buffer *rxb,
981 struct ieee80211_rx_status *stats)
982{
983 struct sk_buff *skb;
Wey-Yi Guy8d801082010-03-17 13:34:36 -0700984 __le16 fc = hdr->frame_control;
985
986 /* We only process data packets if the interface is open */
987 if (unlikely(!priv->is_open)) {
988 IWL_DEBUG_DROP_LIMIT(priv,
989 "Dropping packet while interface is not open.\n");
990 return;
991 }
992
993 /* In case of HW accelerated crypto and bad decryption, drop */
994 if (!priv->cfg->mod_params->sw_crypto &&
995 iwl_set_decrypted_flag(priv, hdr, ampdu_status, stats))
996 return;
997
Zhu Yiecdf94b2010-03-29 16:42:26 +0800998 skb = dev_alloc_skb(128);
Wey-Yi Guy8d801082010-03-17 13:34:36 -0700999 if (!skb) {
Zhu Yiecdf94b2010-03-29 16:42:26 +08001000 IWL_ERR(priv, "dev_alloc_skb failed\n");
Wey-Yi Guy8d801082010-03-17 13:34:36 -07001001 return;
1002 }
1003
Wey-Yi Guy8d801082010-03-17 13:34:36 -07001004 skb_add_rx_frag(skb, 0, rxb->page, (void *)hdr - rxb_addr(rxb), len);
1005
Wey-Yi Guy8d801082010-03-17 13:34:36 -07001006 iwl_update_stats(priv, false, fc, len);
1007 memcpy(IEEE80211_SKB_RXCB(skb), stats, sizeof(*stats));
1008
1009 ieee80211_rx(priv->hw, skb);
Wey-Yi Guy8d801082010-03-17 13:34:36 -07001010 priv->alloc_rxb_page--;
1011 rxb->page = NULL;
1012}
1013
1014/* Called for REPLY_RX (legacy ABG frames), or
1015 * REPLY_RX_MPDU_CMD (HT high-throughput N frames). */
1016void iwlagn_rx_reply_rx(struct iwl_priv *priv,
1017 struct iwl_rx_mem_buffer *rxb)
1018{
1019 struct ieee80211_hdr *header;
1020 struct ieee80211_rx_status rx_status;
1021 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1022 struct iwl_rx_phy_res *phy_res;
1023 __le32 rx_pkt_status;
Emmanuel Grumbach2fb291e2010-06-07 13:21:47 -07001024 struct iwl_rx_mpdu_res_start *amsdu;
Wey-Yi Guy8d801082010-03-17 13:34:36 -07001025 u32 len;
1026 u32 ampdu_status;
1027 u32 rate_n_flags;
1028
1029 /**
1030 * REPLY_RX and REPLY_RX_MPDU_CMD are handled differently.
1031 * REPLY_RX: physical layer info is in this buffer
1032 * REPLY_RX_MPDU_CMD: physical layer info was sent in separate
1033 * command and cached in priv->last_phy_res
1034 *
1035 * Here we set up local variables depending on which command is
1036 * received.
1037 */
1038 if (pkt->hdr.cmd == REPLY_RX) {
1039 phy_res = (struct iwl_rx_phy_res *)pkt->u.raw;
1040 header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*phy_res)
1041 + phy_res->cfg_phy_cnt);
1042
1043 len = le16_to_cpu(phy_res->byte_count);
1044 rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*phy_res) +
1045 phy_res->cfg_phy_cnt + len);
1046 ampdu_status = le32_to_cpu(rx_pkt_status);
1047 } else {
Johannes Berg05d57522010-03-31 08:59:17 -07001048 if (!priv->_agn.last_phy_res_valid) {
Wey-Yi Guy8d801082010-03-17 13:34:36 -07001049 IWL_ERR(priv, "MPDU frame without cached PHY data\n");
1050 return;
1051 }
Johannes Berg05d57522010-03-31 08:59:17 -07001052 phy_res = &priv->_agn.last_phy_res;
Emmanuel Grumbach2fb291e2010-06-07 13:21:47 -07001053 amsdu = (struct iwl_rx_mpdu_res_start *)pkt->u.raw;
Wey-Yi Guy8d801082010-03-17 13:34:36 -07001054 header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*amsdu));
1055 len = le16_to_cpu(amsdu->byte_count);
1056 rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*amsdu) + len);
1057 ampdu_status = iwlagn_translate_rx_status(priv,
1058 le32_to_cpu(rx_pkt_status));
1059 }
1060
1061 if ((unlikely(phy_res->cfg_phy_cnt > 20))) {
1062 IWL_DEBUG_DROP(priv, "dsp size out of range [0,20]: %d/n",
1063 phy_res->cfg_phy_cnt);
1064 return;
1065 }
1066
1067 if (!(rx_pkt_status & RX_RES_STATUS_NO_CRC32_ERROR) ||
1068 !(rx_pkt_status & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
1069 IWL_DEBUG_RX(priv, "Bad CRC or FIFO: 0x%08X.\n",
1070 le32_to_cpu(rx_pkt_status));
1071 return;
1072 }
1073
1074 /* This will be used in several places later */
1075 rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);
1076
1077 /* rx_status carries information about the packet to mac80211 */
1078 rx_status.mactime = le64_to_cpu(phy_res->timestamp);
1079 rx_status.freq =
1080 ieee80211_channel_to_frequency(le16_to_cpu(phy_res->channel));
1081 rx_status.band = (phy_res->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
1082 IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
1083 rx_status.rate_idx =
1084 iwlagn_hwrate_to_mac80211_idx(rate_n_flags, rx_status.band);
1085 rx_status.flag = 0;
1086
1087 /* TSF isn't reliable. In order to allow smooth user experience,
1088 * this W/A doesn't propagate it to the mac80211 */
1089 /*rx_status.flag |= RX_FLAG_TSFT;*/
1090
1091 priv->ucode_beacon_time = le32_to_cpu(phy_res->beacon_time_stamp);
1092
1093 /* Find max signal strength (dBm) among 3 antenna/receiver chains */
1094 rx_status.signal = iwlagn_calc_rssi(priv, phy_res);
1095
Wey-Yi Guy8d801082010-03-17 13:34:36 -07001096 iwl_dbg_log_rx_data_frame(priv, len, header);
Johannes Berged1b6e92010-03-18 09:58:27 -07001097 IWL_DEBUG_STATS_LIMIT(priv, "Rssi %d, TSF %llu\n",
1098 rx_status.signal, (unsigned long long)rx_status.mactime);
Wey-Yi Guy8d801082010-03-17 13:34:36 -07001099
1100 /*
1101 * "antenna number"
1102 *
1103 * It seems that the antenna field in the phy flags value
1104 * is actually a bit field. This is undefined by radiotap,
1105 * it wants an actual antenna number but I always get "7"
1106 * for most legacy frames I receive indicating that the
1107 * same frame was received on all three RX chains.
1108 *
1109 * I think this field should be removed in favor of a
1110 * new 802.11n radiotap field "RX chains" that is defined
1111 * as a bitmask.
1112 */
1113 rx_status.antenna =
1114 (le16_to_cpu(phy_res->phy_flags) & RX_RES_PHY_FLAGS_ANTENNA_MSK)
1115 >> RX_RES_PHY_FLAGS_ANTENNA_POS;
1116
1117 /* set the preamble flag if appropriate */
1118 if (phy_res->phy_flags & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
1119 rx_status.flag |= RX_FLAG_SHORTPRE;
1120
1121 /* Set up the HT phy flags */
1122 if (rate_n_flags & RATE_MCS_HT_MSK)
1123 rx_status.flag |= RX_FLAG_HT;
1124 if (rate_n_flags & RATE_MCS_HT40_MSK)
1125 rx_status.flag |= RX_FLAG_40MHZ;
1126 if (rate_n_flags & RATE_MCS_SGI_MSK)
1127 rx_status.flag |= RX_FLAG_SHORT_GI;
1128
1129 iwlagn_pass_packet_to_mac80211(priv, header, len, ampdu_status,
1130 rxb, &rx_status);
1131}
1132
1133/* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
1134 * This will be used later in iwl_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
1135void iwlagn_rx_reply_rx_phy(struct iwl_priv *priv,
Johannes Berg05d57522010-03-31 08:59:17 -07001136 struct iwl_rx_mem_buffer *rxb)
Wey-Yi Guy8d801082010-03-17 13:34:36 -07001137{
1138 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Johannes Berg05d57522010-03-31 08:59:17 -07001139 priv->_agn.last_phy_res_valid = true;
1140 memcpy(&priv->_agn.last_phy_res, pkt->u.raw,
Wey-Yi Guy8d801082010-03-17 13:34:36 -07001141 sizeof(struct iwl_rx_phy_res));
1142}
Johannes Bergb6e4c552010-04-06 04:12:42 -07001143
1144static int iwl_get_single_channel_for_scan(struct iwl_priv *priv,
Johannes Berg1dda6d22010-04-29 04:43:06 -07001145 struct ieee80211_vif *vif,
1146 enum ieee80211_band band,
1147 struct iwl_scan_channel *scan_ch)
Johannes Bergb6e4c552010-04-06 04:12:42 -07001148{
1149 const struct ieee80211_supported_band *sband;
Johannes Bergb6e4c552010-04-06 04:12:42 -07001150 u16 passive_dwell = 0;
1151 u16 active_dwell = 0;
Abhijeet Kolekar14023642010-06-02 21:15:10 -07001152 int added = 0;
Johannes Bergb6e4c552010-04-06 04:12:42 -07001153 u16 channel = 0;
1154
1155 sband = iwl_get_hw_mode(priv, band);
1156 if (!sband) {
1157 IWL_ERR(priv, "invalid band\n");
1158 return added;
1159 }
1160
1161 active_dwell = iwl_get_active_dwell_time(priv, band, 0);
Johannes Berg1dda6d22010-04-29 04:43:06 -07001162 passive_dwell = iwl_get_passive_dwell_time(priv, band, vif);
Johannes Bergb6e4c552010-04-06 04:12:42 -07001163
1164 if (passive_dwell <= active_dwell)
1165 passive_dwell = active_dwell + 1;
1166
Abhijeet Kolekar14023642010-06-02 21:15:10 -07001167 channel = iwl_get_single_channel_number(priv, band);
Johannes Bergb6e4c552010-04-06 04:12:42 -07001168 if (channel) {
1169 scan_ch->channel = cpu_to_le16(channel);
1170 scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
1171 scan_ch->active_dwell = cpu_to_le16(active_dwell);
1172 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
1173 /* Set txpower levels to defaults */
1174 scan_ch->dsp_atten = 110;
1175 if (band == IEEE80211_BAND_5GHZ)
1176 scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
1177 else
1178 scan_ch->tx_gain = ((1 << 5) | (5 << 3));
1179 added++;
1180 } else
1181 IWL_ERR(priv, "no valid channel found\n");
1182 return added;
1183}
1184
1185static int iwl_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg1dda6d22010-04-29 04:43:06 -07001186 struct ieee80211_vif *vif,
Johannes Bergb6e4c552010-04-06 04:12:42 -07001187 enum ieee80211_band band,
1188 u8 is_active, u8 n_probes,
1189 struct iwl_scan_channel *scan_ch)
1190{
1191 struct ieee80211_channel *chan;
1192 const struct ieee80211_supported_band *sband;
1193 const struct iwl_channel_info *ch_info;
1194 u16 passive_dwell = 0;
1195 u16 active_dwell = 0;
1196 int added, i;
1197 u16 channel;
1198
1199 sband = iwl_get_hw_mode(priv, band);
1200 if (!sband)
1201 return 0;
1202
1203 active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
Johannes Berg1dda6d22010-04-29 04:43:06 -07001204 passive_dwell = iwl_get_passive_dwell_time(priv, band, vif);
Johannes Bergb6e4c552010-04-06 04:12:42 -07001205
1206 if (passive_dwell <= active_dwell)
1207 passive_dwell = active_dwell + 1;
1208
1209 for (i = 0, added = 0; i < priv->scan_request->n_channels; i++) {
1210 chan = priv->scan_request->channels[i];
1211
1212 if (chan->band != band)
1213 continue;
1214
Shanyu Zhao81e95432010-07-28 13:40:27 -07001215 channel = chan->hw_value;
Johannes Bergb6e4c552010-04-06 04:12:42 -07001216 scan_ch->channel = cpu_to_le16(channel);
1217
1218 ch_info = iwl_get_channel_info(priv, band, channel);
1219 if (!is_channel_valid(ch_info)) {
1220 IWL_DEBUG_SCAN(priv, "Channel %d is INVALID for this band.\n",
1221 channel);
1222 continue;
1223 }
1224
1225 if (!is_active || is_channel_passive(ch_info) ||
1226 (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN))
1227 scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
1228 else
1229 scan_ch->type = SCAN_CHANNEL_TYPE_ACTIVE;
1230
1231 if (n_probes)
1232 scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
1233
1234 scan_ch->active_dwell = cpu_to_le16(active_dwell);
1235 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
1236
1237 /* Set txpower levels to defaults */
1238 scan_ch->dsp_atten = 110;
1239
1240 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
1241 * power level:
1242 * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
1243 */
1244 if (band == IEEE80211_BAND_5GHZ)
1245 scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
1246 else
1247 scan_ch->tx_gain = ((1 << 5) | (5 << 3));
1248
1249 IWL_DEBUG_SCAN(priv, "Scanning ch=%d prob=0x%X [%s %d]\n",
1250 channel, le32_to_cpu(scan_ch->type),
1251 (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
1252 "ACTIVE" : "PASSIVE",
1253 (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
1254 active_dwell : passive_dwell);
1255
1256 scan_ch++;
1257 added++;
1258 }
1259
1260 IWL_DEBUG_SCAN(priv, "total channels to scan %d\n", added);
1261 return added;
1262}
1263
Johannes Berg3eecce52010-09-13 14:46:33 +02001264int iwlagn_request_scan(struct iwl_priv *priv, struct ieee80211_vif *vif)
Johannes Bergb6e4c552010-04-06 04:12:42 -07001265{
1266 struct iwl_host_cmd cmd = {
1267 .id = REPLY_SCAN_CMD,
1268 .len = sizeof(struct iwl_scan_cmd),
1269 .flags = CMD_SIZE_HUGE,
1270 };
1271 struct iwl_scan_cmd *scan;
Johannes Berga194e322010-08-27 08:53:46 -07001272 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Johannes Bergb6e4c552010-04-06 04:12:42 -07001273 u32 rate_flags = 0;
1274 u16 cmd_len;
1275 u16 rx_chain = 0;
1276 enum ieee80211_band band;
1277 u8 n_probes = 0;
1278 u8 rx_ant = priv->hw_params.valid_rx_ant;
1279 u8 rate;
1280 bool is_active = false;
1281 int chan_mod;
1282 u8 active_chains;
Johannes Berg0e1654f2010-05-18 02:48:36 -07001283 u8 scan_tx_antennas = priv->hw_params.valid_tx_ant;
Johannes Berg3eecce52010-09-13 14:46:33 +02001284 int ret;
1285
1286 lockdep_assert_held(&priv->mutex);
Johannes Bergb6e4c552010-04-06 04:12:42 -07001287
Johannes Berga194e322010-08-27 08:53:46 -07001288 if (vif)
1289 ctx = iwl_rxon_ctx_from_vif(vif);
1290
Johannes Bergb6e4c552010-04-06 04:12:42 -07001291 if (!priv->scan_cmd) {
1292 priv->scan_cmd = kmalloc(sizeof(struct iwl_scan_cmd) +
1293 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
1294 if (!priv->scan_cmd) {
1295 IWL_DEBUG_SCAN(priv,
1296 "fail to allocate memory for scan\n");
Johannes Berg3eecce52010-09-13 14:46:33 +02001297 return -ENOMEM;
Johannes Bergb6e4c552010-04-06 04:12:42 -07001298 }
1299 }
1300 scan = priv->scan_cmd;
1301 memset(scan, 0, sizeof(struct iwl_scan_cmd) + IWL_MAX_SCAN_SIZE);
1302
1303 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
1304 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
1305
Johannes Berg246ed352010-08-23 10:46:32 +02001306 if (iwl_is_any_associated(priv)) {
Johannes Bergb6e4c552010-04-06 04:12:42 -07001307 u16 interval = 0;
1308 u32 extra;
1309 u32 suspend_time = 100;
1310 u32 scan_suspend_time = 100;
1311 unsigned long flags;
1312
1313 IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
1314 spin_lock_irqsave(&priv->lock, flags);
John W. Linvillea6e492b2010-07-22 15:24:56 -04001315 if (priv->is_internal_short_scan)
1316 interval = 0;
1317 else
1318 interval = vif->bss_conf.beacon_int;
Johannes Bergb6e4c552010-04-06 04:12:42 -07001319 spin_unlock_irqrestore(&priv->lock, flags);
1320
1321 scan->suspend_time = 0;
1322 scan->max_out_time = cpu_to_le32(200 * 1024);
1323 if (!interval)
1324 interval = suspend_time;
1325
1326 extra = (suspend_time / interval) << 22;
1327 scan_suspend_time = (extra |
1328 ((suspend_time % interval) * 1024));
1329 scan->suspend_time = cpu_to_le32(scan_suspend_time);
1330 IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
1331 scan_suspend_time, interval);
1332 }
1333
1334 if (priv->is_internal_short_scan) {
1335 IWL_DEBUG_SCAN(priv, "Start internal passive scan.\n");
1336 } else if (priv->scan_request->n_ssids) {
1337 int i, p = 0;
1338 IWL_DEBUG_SCAN(priv, "Kicking off active scan\n");
1339 for (i = 0; i < priv->scan_request->n_ssids; i++) {
1340 /* always does wildcard anyway */
1341 if (!priv->scan_request->ssids[i].ssid_len)
1342 continue;
1343 scan->direct_scan[p].id = WLAN_EID_SSID;
1344 scan->direct_scan[p].len =
1345 priv->scan_request->ssids[i].ssid_len;
1346 memcpy(scan->direct_scan[p].ssid,
1347 priv->scan_request->ssids[i].ssid,
1348 priv->scan_request->ssids[i].ssid_len);
1349 n_probes++;
1350 p++;
1351 }
1352 is_active = true;
1353 } else
1354 IWL_DEBUG_SCAN(priv, "Start passive scan.\n");
1355
1356 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
Johannes Berga194e322010-08-27 08:53:46 -07001357 scan->tx_cmd.sta_id = ctx->bcast_sta_id;
Johannes Bergb6e4c552010-04-06 04:12:42 -07001358 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
1359
1360 switch (priv->scan_band) {
1361 case IEEE80211_BAND_2GHZ:
1362 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
Johannes Berg246ed352010-08-23 10:46:32 +02001363 chan_mod = le32_to_cpu(
1364 priv->contexts[IWL_RXON_CTX_BSS].active.flags &
1365 RXON_FLG_CHANNEL_MODE_MSK)
Johannes Bergb6e4c552010-04-06 04:12:42 -07001366 >> RXON_FLG_CHANNEL_MODE_POS;
1367 if (chan_mod == CHANNEL_MODE_PURE_40) {
1368 rate = IWL_RATE_6M_PLCP;
1369 } else {
1370 rate = IWL_RATE_1M_PLCP;
1371 rate_flags = RATE_MCS_CCK_MSK;
1372 }
Johannes Bergd44ae692010-08-23 07:56:56 -07001373 /*
1374 * Internal scans are passive, so we can indiscriminately set
1375 * the BT ignore flag on 2.4 GHz since it applies to TX only.
1376 */
1377 if (priv->cfg->advanced_bt_coexist)
1378 scan->tx_cmd.tx_flags |= TX_CMD_FLG_IGNORE_BT;
Reinette Chatread41ee32010-04-30 15:13:00 -07001379 scan->good_CRC_th = IWL_GOOD_CRC_TH_DISABLED;
Johannes Bergb6e4c552010-04-06 04:12:42 -07001380 break;
1381 case IEEE80211_BAND_5GHZ:
1382 rate = IWL_RATE_6M_PLCP;
1383 /*
Reinette Chatread41ee32010-04-30 15:13:00 -07001384 * If active scanning is requested but a certain channel is
1385 * marked passive, we can do active scanning if we detect
1386 * transmissions.
1387 *
1388 * There is an issue with some firmware versions that triggers
1389 * a sysassert on a "good CRC threshold" of zero (== disabled),
1390 * on a radar channel even though this means that we should NOT
1391 * send probes.
1392 *
1393 * The "good CRC threshold" is the number of frames that we
1394 * need to receive during our dwell time on a channel before
1395 * sending out probes -- setting this to a huge value will
1396 * mean we never reach it, but at the same time work around
1397 * the aforementioned issue. Thus use IWL_GOOD_CRC_TH_NEVER
1398 * here instead of IWL_GOOD_CRC_TH_DISABLED.
Johannes Bergb6e4c552010-04-06 04:12:42 -07001399 */
Reinette Chatread41ee32010-04-30 15:13:00 -07001400 scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
1401 IWL_GOOD_CRC_TH_NEVER;
Johannes Bergb6e4c552010-04-06 04:12:42 -07001402 break;
1403 default:
Johannes Berg3eecce52010-09-13 14:46:33 +02001404 IWL_WARN(priv, "Invalid scan band\n");
1405 return -EIO;
Johannes Bergb6e4c552010-04-06 04:12:42 -07001406 }
1407
1408 band = priv->scan_band;
1409
Johannes Berg0e1654f2010-05-18 02:48:36 -07001410 if (priv->cfg->scan_rx_antennas[band])
1411 rx_ant = priv->cfg->scan_rx_antennas[band];
Johannes Berge7cb4952010-04-13 01:04:35 -07001412
Johannes Berg0e1654f2010-05-18 02:48:36 -07001413 if (priv->cfg->scan_tx_antennas[band])
1414 scan_tx_antennas = priv->cfg->scan_tx_antennas[band];
1415
Wey-Yi Guybee008b2010-08-23 07:57:04 -07001416 if (priv->cfg->advanced_bt_coexist && priv->bt_full_concurrent) {
1417 /* operated as 1x1 in full concurrency mode */
1418 scan_tx_antennas =
1419 first_antenna(priv->cfg->scan_tx_antennas[band]);
1420 }
1421
Johannes Berg0e1654f2010-05-18 02:48:36 -07001422 priv->scan_tx_ant[band] = iwl_toggle_tx_ant(priv, priv->scan_tx_ant[band],
1423 scan_tx_antennas);
Johannes Bergb6e4c552010-04-06 04:12:42 -07001424 rate_flags |= iwl_ant_idx_to_flags(priv->scan_tx_ant[band]);
1425 scan->tx_cmd.rate_n_flags = iwl_hw_set_rate_n_flags(rate, rate_flags);
1426
1427 /* In power save mode use one chain, otherwise use all chains */
1428 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
1429 /* rx_ant has been set to all valid chains previously */
1430 active_chains = rx_ant &
1431 ((u8)(priv->chain_noise_data.active_chains));
1432 if (!active_chains)
1433 active_chains = rx_ant;
1434
1435 IWL_DEBUG_SCAN(priv, "chain_noise_data.active_chains: %u\n",
1436 priv->chain_noise_data.active_chains);
1437
1438 rx_ant = first_antenna(active_chains);
1439 }
Wey-Yi Guybee008b2010-08-23 07:57:04 -07001440 if (priv->cfg->advanced_bt_coexist && priv->bt_full_concurrent) {
1441 /* operated as 1x1 in full concurrency mode */
1442 rx_ant = first_antenna(rx_ant);
1443 }
1444
Johannes Bergb6e4c552010-04-06 04:12:42 -07001445 /* MIMO is not used here, but value is required */
1446 rx_chain |= priv->hw_params.valid_rx_ant << RXON_RX_CHAIN_VALID_POS;
1447 rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS;
1448 rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_SEL_POS;
1449 rx_chain |= 0x1 << RXON_RX_CHAIN_DRIVER_FORCE_POS;
1450 scan->rx_chain = cpu_to_le16(rx_chain);
1451 if (!priv->is_internal_short_scan) {
1452 cmd_len = iwl_fill_probe_req(priv,
1453 (struct ieee80211_mgmt *)scan->data,
Johannes Berg3a0b9aa2010-05-12 03:33:12 -07001454 vif->addr,
Johannes Bergb6e4c552010-04-06 04:12:42 -07001455 priv->scan_request->ie,
1456 priv->scan_request->ie_len,
1457 IWL_MAX_SCAN_SIZE - sizeof(*scan));
1458 } else {
Johannes Berg3a0b9aa2010-05-12 03:33:12 -07001459 /* use bcast addr, will not be transmitted but must be valid */
Johannes Bergb6e4c552010-04-06 04:12:42 -07001460 cmd_len = iwl_fill_probe_req(priv,
1461 (struct ieee80211_mgmt *)scan->data,
Johannes Berg3a0b9aa2010-05-12 03:33:12 -07001462 iwl_bcast_addr, NULL, 0,
Johannes Bergb6e4c552010-04-06 04:12:42 -07001463 IWL_MAX_SCAN_SIZE - sizeof(*scan));
1464
1465 }
1466 scan->tx_cmd.len = cpu_to_le16(cmd_len);
Johannes Bergb6e4c552010-04-06 04:12:42 -07001467
1468 scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
1469 RXON_FILTER_BCON_AWARE_MSK);
1470
1471 if (priv->is_internal_short_scan) {
1472 scan->channel_count =
Johannes Berg1dda6d22010-04-29 04:43:06 -07001473 iwl_get_single_channel_for_scan(priv, vif, band,
Johannes Bergb6e4c552010-04-06 04:12:42 -07001474 (void *)&scan->data[le16_to_cpu(
1475 scan->tx_cmd.len)]);
1476 } else {
1477 scan->channel_count =
Johannes Berg1dda6d22010-04-29 04:43:06 -07001478 iwl_get_channels_for_scan(priv, vif, band,
Johannes Bergb6e4c552010-04-06 04:12:42 -07001479 is_active, n_probes,
1480 (void *)&scan->data[le16_to_cpu(
1481 scan->tx_cmd.len)]);
1482 }
1483 if (scan->channel_count == 0) {
1484 IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
Johannes Berg3eecce52010-09-13 14:46:33 +02001485 return -EIO;
Johannes Bergb6e4c552010-04-06 04:12:42 -07001486 }
1487
1488 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
1489 scan->channel_count * sizeof(struct iwl_scan_channel);
1490 cmd.data = scan;
1491 scan->len = cpu_to_le16(cmd.len);
1492
Johannes Berg3eecce52010-09-13 14:46:33 +02001493 if (priv->cfg->ops->hcmd->set_pan_params) {
1494 ret = priv->cfg->ops->hcmd->set_pan_params(priv);
1495 if (ret)
1496 return ret;
1497 }
1498
Johannes Bergb6e4c552010-04-06 04:12:42 -07001499 set_bit(STATUS_SCAN_HW, &priv->status);
Johannes Berg3eecce52010-09-13 14:46:33 +02001500 ret = iwl_send_cmd_sync(priv, &cmd);
1501 if (ret) {
1502 clear_bit(STATUS_SCAN_HW, &priv->status);
1503 if (priv->cfg->ops->hcmd->set_pan_params)
1504 priv->cfg->ops->hcmd->set_pan_params(priv);
1505 }
Johannes Berg52a02d12010-08-27 09:44:50 -07001506
Johannes Berg3eecce52010-09-13 14:46:33 +02001507 return ret;
Johannes Bergb6e4c552010-04-06 04:12:42 -07001508}
Johannes Berg1fa61b22010-04-28 08:44:52 -07001509
1510int iwlagn_manage_ibss_station(struct iwl_priv *priv,
1511 struct ieee80211_vif *vif, bool add)
1512{
Johannes Bergfd1af152010-04-30 11:30:43 -07001513 struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
1514
Johannes Berg1fa61b22010-04-28 08:44:52 -07001515 if (add)
Johannes Berga194e322010-08-27 08:53:46 -07001516 return iwl_add_bssid_station(priv, vif_priv->ctx,
1517 vif->bss_conf.bssid, true,
Johannes Bergfd1af152010-04-30 11:30:43 -07001518 &vif_priv->ibss_bssid_sta_id);
1519 return iwl_remove_station(priv, vif_priv->ibss_bssid_sta_id,
1520 vif->bss_conf.bssid);
Johannes Berg1fa61b22010-04-28 08:44:52 -07001521}
Johannes Berg1ff504e2010-05-03 01:22:42 -07001522
1523void iwl_free_tfds_in_queue(struct iwl_priv *priv,
1524 int sta_id, int tid, int freed)
1525{
Johannes Berga24d52f2010-08-06 16:17:53 +02001526 lockdep_assert_held(&priv->sta_lock);
Reinette Chatre9c5ac092010-05-05 02:26:06 -07001527
Johannes Berg1ff504e2010-05-03 01:22:42 -07001528 if (priv->stations[sta_id].tid[tid].tfds_in_queue >= freed)
1529 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
1530 else {
1531 IWL_DEBUG_TX(priv, "free more than tfds_in_queue (%u:%d)\n",
1532 priv->stations[sta_id].tid[tid].tfds_in_queue,
1533 freed);
1534 priv->stations[sta_id].tid[tid].tfds_in_queue = 0;
1535 }
1536}
Wey-Yi Guy716c74b2010-06-24 13:22:36 -07001537
1538#define IWL_FLUSH_WAIT_MS 2000
1539
1540int iwlagn_wait_tx_queue_empty(struct iwl_priv *priv)
1541{
1542 struct iwl_tx_queue *txq;
1543 struct iwl_queue *q;
1544 int cnt;
1545 unsigned long now = jiffies;
1546 int ret = 0;
1547
1548 /* waiting for all the tx frames complete might take a while */
1549 for (cnt = 0; cnt < priv->hw_params.max_txq_num; cnt++) {
Johannes Berg13bb9482010-08-23 10:46:33 +02001550 if (cnt == priv->cmd_queue)
Wey-Yi Guy716c74b2010-06-24 13:22:36 -07001551 continue;
1552 txq = &priv->txq[cnt];
1553 q = &txq->q;
1554 while (q->read_ptr != q->write_ptr && !time_after(jiffies,
1555 now + msecs_to_jiffies(IWL_FLUSH_WAIT_MS)))
1556 msleep(1);
1557
1558 if (q->read_ptr != q->write_ptr) {
1559 IWL_ERR(priv, "fail to flush all tx fifo queues\n");
1560 ret = -ETIMEDOUT;
1561 break;
1562 }
1563 }
1564 return ret;
1565}
1566
1567#define IWL_TX_QUEUE_MSK 0xfffff
1568
1569/**
1570 * iwlagn_txfifo_flush: send REPLY_TXFIFO_FLUSH command to uCode
1571 *
1572 * pre-requirements:
1573 * 1. acquire mutex before calling
1574 * 2. make sure rf is on and not in exit state
1575 */
1576int iwlagn_txfifo_flush(struct iwl_priv *priv, u16 flush_control)
1577{
1578 struct iwl_txfifo_flush_cmd flush_cmd;
1579 struct iwl_host_cmd cmd = {
1580 .id = REPLY_TXFIFO_FLUSH,
1581 .len = sizeof(struct iwl_txfifo_flush_cmd),
1582 .flags = CMD_SYNC,
1583 .data = &flush_cmd,
1584 };
1585
1586 might_sleep();
1587
1588 memset(&flush_cmd, 0, sizeof(flush_cmd));
1589 flush_cmd.fifo_control = IWL_TX_FIFO_VO_MSK | IWL_TX_FIFO_VI_MSK |
1590 IWL_TX_FIFO_BE_MSK | IWL_TX_FIFO_BK_MSK;
1591 if (priv->cfg->sku & IWL_SKU_N)
1592 flush_cmd.fifo_control |= IWL_AGG_TX_QUEUE_MSK;
1593
1594 IWL_DEBUG_INFO(priv, "fifo queue control: 0X%x\n",
1595 flush_cmd.fifo_control);
1596 flush_cmd.flush_control = cpu_to_le16(flush_control);
1597
1598 return iwl_send_cmd(priv, &cmd);
1599}
Wey-Yi Guy65550632010-06-24 13:18:35 -07001600
1601void iwlagn_dev_txfifo_flush(struct iwl_priv *priv, u16 flush_control)
1602{
1603 mutex_lock(&priv->mutex);
1604 ieee80211_stop_queues(priv->hw);
1605 if (priv->cfg->ops->lib->txfifo_flush(priv, IWL_DROP_ALL)) {
1606 IWL_ERR(priv, "flush request fail\n");
1607 goto done;
1608 }
1609 IWL_DEBUG_INFO(priv, "wait transmit/flush all frames\n");
1610 iwlagn_wait_tx_queue_empty(priv);
1611done:
1612 ieee80211_wake_queues(priv->hw);
1613 mutex_unlock(&priv->mutex);
1614}
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -07001615
1616/*
1617 * BT coex
1618 */
1619/*
1620 * Macros to access the lookup table.
1621 *
1622 * The lookup table has 7 inputs: bt3_prio, bt3_txrx, bt_rf_act, wifi_req,
1623* wifi_prio, wifi_txrx and wifi_sh_ant_req.
1624 *
1625 * It has three outputs: WLAN_ACTIVE, WLAN_KILL and ANT_SWITCH
1626 *
1627 * The format is that "registers" 8 through 11 contain the WLAN_ACTIVE bits
1628 * one after another in 32-bit registers, and "registers" 0 through 7 contain
1629 * the WLAN_KILL and ANT_SWITCH bits interleaved (in that order).
1630 *
1631 * These macros encode that format.
1632 */
1633#define LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, wifi_req, wifi_prio, \
1634 wifi_txrx, wifi_sh_ant_req) \
1635 (bt3_prio | (bt3_txrx << 1) | (bt_rf_act << 2) | (wifi_req << 3) | \
1636 (wifi_prio << 4) | (wifi_txrx << 5) | (wifi_sh_ant_req << 6))
1637
1638#define LUT_PTA_WLAN_ACTIVE_OP(lut, op, val) \
1639 lut[8 + ((val) >> 5)] op (cpu_to_le32(BIT((val) & 0x1f)))
1640#define LUT_TEST_PTA_WLAN_ACTIVE(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
1641 wifi_prio, wifi_txrx, wifi_sh_ant_req) \
1642 (!!(LUT_PTA_WLAN_ACTIVE_OP(lut, &, LUT_VALUE(bt3_prio, bt3_txrx, \
1643 bt_rf_act, wifi_req, wifi_prio, wifi_txrx, \
1644 wifi_sh_ant_req))))
1645#define LUT_SET_PTA_WLAN_ACTIVE(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
1646 wifi_prio, wifi_txrx, wifi_sh_ant_req) \
1647 LUT_PTA_WLAN_ACTIVE_OP(lut, |=, LUT_VALUE(bt3_prio, bt3_txrx, \
1648 bt_rf_act, wifi_req, wifi_prio, wifi_txrx, \
1649 wifi_sh_ant_req))
1650#define LUT_CLEAR_PTA_WLAN_ACTIVE(lut, bt3_prio, bt3_txrx, bt_rf_act, \
1651 wifi_req, wifi_prio, wifi_txrx, \
1652 wifi_sh_ant_req) \
1653 LUT_PTA_WLAN_ACTIVE_OP(lut, &= ~, LUT_VALUE(bt3_prio, bt3_txrx, \
1654 bt_rf_act, wifi_req, wifi_prio, wifi_txrx, \
1655 wifi_sh_ant_req))
1656
1657#define LUT_WLAN_KILL_OP(lut, op, val) \
1658 lut[(val) >> 4] op (cpu_to_le32(BIT(((val) << 1) & 0x1e)))
1659#define LUT_TEST_WLAN_KILL(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
1660 wifi_prio, wifi_txrx, wifi_sh_ant_req) \
1661 (!!(LUT_WLAN_KILL_OP(lut, &, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
1662 wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))))
1663#define LUT_SET_WLAN_KILL(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
1664 wifi_prio, wifi_txrx, wifi_sh_ant_req) \
1665 LUT_WLAN_KILL_OP(lut, |=, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
1666 wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))
1667#define LUT_CLEAR_WLAN_KILL(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
1668 wifi_prio, wifi_txrx, wifi_sh_ant_req) \
1669 LUT_WLAN_KILL_OP(lut, &= ~, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
1670 wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))
1671
1672#define LUT_ANT_SWITCH_OP(lut, op, val) \
1673 lut[(val) >> 4] op (cpu_to_le32(BIT((((val) << 1) & 0x1e) + 1)))
1674#define LUT_TEST_ANT_SWITCH(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
1675 wifi_prio, wifi_txrx, wifi_sh_ant_req) \
1676 (!!(LUT_ANT_SWITCH_OP(lut, &, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
1677 wifi_req, wifi_prio, wifi_txrx, \
1678 wifi_sh_ant_req))))
1679#define LUT_SET_ANT_SWITCH(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
1680 wifi_prio, wifi_txrx, wifi_sh_ant_req) \
1681 LUT_ANT_SWITCH_OP(lut, |=, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
1682 wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))
1683#define LUT_CLEAR_ANT_SWITCH(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
1684 wifi_prio, wifi_txrx, wifi_sh_ant_req) \
1685 LUT_ANT_SWITCH_OP(lut, &= ~, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
1686 wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))
1687
1688static const __le32 iwlagn_def_3w_lookup[12] = {
1689 cpu_to_le32(0xaaaaaaaa),
1690 cpu_to_le32(0xaaaaaaaa),
1691 cpu_to_le32(0xaeaaaaaa),
1692 cpu_to_le32(0xaaaaaaaa),
1693 cpu_to_le32(0xcc00ff28),
1694 cpu_to_le32(0x0000aaaa),
1695 cpu_to_le32(0xcc00aaaa),
1696 cpu_to_le32(0x0000aaaa),
1697 cpu_to_le32(0xc0004000),
1698 cpu_to_le32(0x00004000),
1699 cpu_to_le32(0xf0005000),
1700 cpu_to_le32(0xf0004000),
1701};
1702
1703static const __le32 iwlagn_concurrent_lookup[12] = {
1704 cpu_to_le32(0xaaaaaaaa),
1705 cpu_to_le32(0xaaaaaaaa),
1706 cpu_to_le32(0xaaaaaaaa),
1707 cpu_to_le32(0xaaaaaaaa),
1708 cpu_to_le32(0xaaaaaaaa),
1709 cpu_to_le32(0xaaaaaaaa),
1710 cpu_to_le32(0xaaaaaaaa),
1711 cpu_to_le32(0xaaaaaaaa),
1712 cpu_to_le32(0x00000000),
1713 cpu_to_le32(0x00000000),
1714 cpu_to_le32(0x00000000),
1715 cpu_to_le32(0x00000000),
1716};
1717
1718void iwlagn_send_advance_bt_config(struct iwl_priv *priv)
1719{
1720 struct iwlagn_bt_cmd bt_cmd = {
1721 .max_kill = IWLAGN_BT_MAX_KILL_DEFAULT,
1722 .bt3_timer_t7_value = IWLAGN_BT3_T7_DEFAULT,
1723 .bt3_prio_sample_time = IWLAGN_BT3_PRIO_SAMPLE_DEFAULT,
1724 .bt3_timer_t2_value = IWLAGN_BT3_T2_DEFAULT,
1725 };
1726
1727 BUILD_BUG_ON(sizeof(iwlagn_def_3w_lookup) !=
1728 sizeof(bt_cmd.bt3_lookup_table));
1729
1730 bt_cmd.prio_boost = priv->cfg->bt_prio_boost;
1731 bt_cmd.kill_ack_mask = priv->kill_ack_mask;
1732 bt_cmd.kill_cts_mask = priv->kill_cts_mask;
1733 bt_cmd.valid = priv->bt_valid;
1734
1735 /*
1736 * Configure BT coex mode to "no coexistence" when the
1737 * user disabled BT coexistence, we have no interface
1738 * (might be in monitor mode), or the interface is in
1739 * IBSS mode (no proper uCode support for coex then).
1740 */
1741 if (!bt_coex_active || priv->iw_mode == NL80211_IFTYPE_ADHOC) {
1742 bt_cmd.flags = 0;
1743 } else {
1744 bt_cmd.flags = IWLAGN_BT_FLAG_COEX_MODE_3W <<
1745 IWLAGN_BT_FLAG_COEX_MODE_SHIFT;
1746 if (priv->bt_ch_announce)
1747 bt_cmd.flags |= IWLAGN_BT_FLAG_CHANNEL_INHIBITION;
1748 IWL_DEBUG_INFO(priv, "BT coex flag: 0X%x\n", bt_cmd.flags);
1749 }
1750 if (priv->bt_full_concurrent)
1751 memcpy(bt_cmd.bt3_lookup_table, iwlagn_concurrent_lookup,
1752 sizeof(iwlagn_concurrent_lookup));
1753 else
1754 memcpy(bt_cmd.bt3_lookup_table, iwlagn_def_3w_lookup,
1755 sizeof(iwlagn_def_3w_lookup));
1756
1757 IWL_DEBUG_INFO(priv, "BT coex %s in %s mode\n",
1758 bt_cmd.flags ? "active" : "disabled",
1759 priv->bt_full_concurrent ?
1760 "full concurrency" : "3-wire");
1761
1762 if (iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG, sizeof(bt_cmd), &bt_cmd))
1763 IWL_ERR(priv, "failed to send BT Coex Config\n");
1764
1765 /*
1766 * When we are doing a restart, need to also reconfigure BT
1767 * SCO to the device. If not doing a restart, bt_sco_active
1768 * will always be false, so there's no need to have an extra
1769 * variable to check for it.
1770 */
1771 if (priv->bt_sco_active) {
1772 struct iwlagn_bt_sco_cmd sco_cmd = { .flags = 0 };
1773
1774 if (priv->bt_sco_active)
1775 sco_cmd.flags |= IWLAGN_BT_SCO_ACTIVE;
1776 if (iwl_send_cmd_pdu(priv, REPLY_BT_COEX_SCO,
1777 sizeof(sco_cmd), &sco_cmd))
1778 IWL_ERR(priv, "failed to send BT SCO command\n");
1779 }
1780}
1781
1782static void iwlagn_bt_traffic_change_work(struct work_struct *work)
1783{
1784 struct iwl_priv *priv =
1785 container_of(work, struct iwl_priv, bt_traffic_change_work);
Johannes Berg8bd413e2010-08-23 10:46:40 +02001786 struct iwl_rxon_context *ctx;
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -07001787 int smps_request = -1;
1788
1789 IWL_DEBUG_INFO(priv, "BT traffic load changes: %d\n",
1790 priv->bt_traffic_load);
1791
1792 switch (priv->bt_traffic_load) {
1793 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1794 smps_request = IEEE80211_SMPS_AUTOMATIC;
1795 break;
1796 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1797 smps_request = IEEE80211_SMPS_DYNAMIC;
1798 break;
1799 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1800 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1801 smps_request = IEEE80211_SMPS_STATIC;
1802 break;
1803 default:
1804 IWL_ERR(priv, "Invalid BT traffic load: %d\n",
1805 priv->bt_traffic_load);
1806 break;
1807 }
1808
1809 mutex_lock(&priv->mutex);
1810
1811 if (priv->cfg->ops->lib->update_chain_flags)
1812 priv->cfg->ops->lib->update_chain_flags(priv);
1813
Johannes Berg8bd413e2010-08-23 10:46:40 +02001814 if (smps_request != -1) {
1815 for_each_context(priv, ctx) {
1816 if (ctx->vif && ctx->vif->type == NL80211_IFTYPE_STATION)
1817 ieee80211_request_smps(ctx->vif, smps_request);
1818 }
1819 }
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -07001820
1821 mutex_unlock(&priv->mutex);
1822}
1823
1824static void iwlagn_print_uartmsg(struct iwl_priv *priv,
1825 struct iwl_bt_uart_msg *uart_msg)
1826{
1827 IWL_DEBUG_NOTIF(priv, "Message Type = 0x%X, SSN = 0x%X, "
1828 "Update Req = 0x%X",
1829 (BT_UART_MSG_FRAME1MSGTYPE_MSK & uart_msg->frame1) >>
1830 BT_UART_MSG_FRAME1MSGTYPE_POS,
1831 (BT_UART_MSG_FRAME1SSN_MSK & uart_msg->frame1) >>
1832 BT_UART_MSG_FRAME1SSN_POS,
1833 (BT_UART_MSG_FRAME1UPDATEREQ_MSK & uart_msg->frame1) >>
1834 BT_UART_MSG_FRAME1UPDATEREQ_POS);
1835
1836 IWL_DEBUG_NOTIF(priv, "Open connections = 0x%X, Traffic load = 0x%X, "
1837 "Chl_SeqN = 0x%X, In band = 0x%X",
1838 (BT_UART_MSG_FRAME2OPENCONNECTIONS_MSK & uart_msg->frame2) >>
1839 BT_UART_MSG_FRAME2OPENCONNECTIONS_POS,
1840 (BT_UART_MSG_FRAME2TRAFFICLOAD_MSK & uart_msg->frame2) >>
1841 BT_UART_MSG_FRAME2TRAFFICLOAD_POS,
1842 (BT_UART_MSG_FRAME2CHLSEQN_MSK & uart_msg->frame2) >>
1843 BT_UART_MSG_FRAME2CHLSEQN_POS,
1844 (BT_UART_MSG_FRAME2INBAND_MSK & uart_msg->frame2) >>
1845 BT_UART_MSG_FRAME2INBAND_POS);
1846
1847 IWL_DEBUG_NOTIF(priv, "SCO/eSCO = 0x%X, Sniff = 0x%X, A2DP = 0x%X, "
1848 "ACL = 0x%X, Master = 0x%X, OBEX = 0x%X",
1849 (BT_UART_MSG_FRAME3SCOESCO_MSK & uart_msg->frame3) >>
1850 BT_UART_MSG_FRAME3SCOESCO_POS,
1851 (BT_UART_MSG_FRAME3SNIFF_MSK & uart_msg->frame3) >>
1852 BT_UART_MSG_FRAME3SNIFF_POS,
1853 (BT_UART_MSG_FRAME3A2DP_MSK & uart_msg->frame3) >>
1854 BT_UART_MSG_FRAME3A2DP_POS,
1855 (BT_UART_MSG_FRAME3ACL_MSK & uart_msg->frame3) >>
1856 BT_UART_MSG_FRAME3ACL_POS,
1857 (BT_UART_MSG_FRAME3MASTER_MSK & uart_msg->frame3) >>
1858 BT_UART_MSG_FRAME3MASTER_POS,
1859 (BT_UART_MSG_FRAME3OBEX_MSK & uart_msg->frame3) >>
1860 BT_UART_MSG_FRAME3OBEX_POS);
1861
1862 IWL_DEBUG_NOTIF(priv, "Idle duration = 0x%X",
1863 (BT_UART_MSG_FRAME4IDLEDURATION_MSK & uart_msg->frame4) >>
1864 BT_UART_MSG_FRAME4IDLEDURATION_POS);
1865
1866 IWL_DEBUG_NOTIF(priv, "Tx Activity = 0x%X, Rx Activity = 0x%X, "
1867 "eSCO Retransmissions = 0x%X",
1868 (BT_UART_MSG_FRAME5TXACTIVITY_MSK & uart_msg->frame5) >>
1869 BT_UART_MSG_FRAME5TXACTIVITY_POS,
1870 (BT_UART_MSG_FRAME5RXACTIVITY_MSK & uart_msg->frame5) >>
1871 BT_UART_MSG_FRAME5RXACTIVITY_POS,
1872 (BT_UART_MSG_FRAME5ESCORETRANSMIT_MSK & uart_msg->frame5) >>
1873 BT_UART_MSG_FRAME5ESCORETRANSMIT_POS);
1874
1875 IWL_DEBUG_NOTIF(priv, "Sniff Interval = 0x%X, Discoverable = 0x%X",
1876 (BT_UART_MSG_FRAME6SNIFFINTERVAL_MSK & uart_msg->frame6) >>
1877 BT_UART_MSG_FRAME6SNIFFINTERVAL_POS,
1878 (BT_UART_MSG_FRAME6DISCOVERABLE_MSK & uart_msg->frame6) >>
1879 BT_UART_MSG_FRAME6DISCOVERABLE_POS);
1880
1881 IWL_DEBUG_NOTIF(priv, "Sniff Activity = 0x%X, Inquiry/Page SR Mode = "
1882 "0x%X, Connectable = 0x%X",
1883 (BT_UART_MSG_FRAME7SNIFFACTIVITY_MSK & uart_msg->frame7) >>
1884 BT_UART_MSG_FRAME7SNIFFACTIVITY_POS,
1885 (BT_UART_MSG_FRAME7INQUIRYPAGESRMODE_MSK & uart_msg->frame7) >>
1886 BT_UART_MSG_FRAME7INQUIRYPAGESRMODE_POS,
1887 (BT_UART_MSG_FRAME7CONNECTABLE_MSK & uart_msg->frame7) >>
1888 BT_UART_MSG_FRAME7CONNECTABLE_POS);
1889}
1890
1891static void iwlagn_set_kill_ack_msk(struct iwl_priv *priv,
1892 struct iwl_bt_uart_msg *uart_msg)
1893{
1894 u8 kill_ack_msk;
1895 __le32 bt_kill_ack_msg[2] = {
1896 cpu_to_le32(0xFFFFFFF), cpu_to_le32(0xFFFFFC00) };
1897
1898 kill_ack_msk = (((BT_UART_MSG_FRAME3A2DP_MSK |
1899 BT_UART_MSG_FRAME3SNIFF_MSK |
1900 BT_UART_MSG_FRAME3SCOESCO_MSK) &
1901 uart_msg->frame3) == 0) ? 1 : 0;
1902 if (priv->kill_ack_mask != bt_kill_ack_msg[kill_ack_msk]) {
1903 priv->bt_valid |= IWLAGN_BT_VALID_KILL_ACK_MASK;
1904 priv->kill_ack_mask = bt_kill_ack_msg[kill_ack_msk];
1905 /* schedule to send runtime bt_config */
1906 queue_work(priv->workqueue, &priv->bt_runtime_config);
1907 }
1908
1909}
1910
1911void iwlagn_bt_coex_profile_notif(struct iwl_priv *priv,
1912 struct iwl_rx_mem_buffer *rxb)
1913{
1914 unsigned long flags;
1915 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1916 struct iwl_bt_coex_profile_notif *coex = &pkt->u.bt_coex_profile_notif;
1917 struct iwlagn_bt_sco_cmd sco_cmd = { .flags = 0 };
1918 struct iwl_bt_uart_msg *uart_msg = &coex->last_bt_uart_msg;
1919 u8 last_traffic_load;
1920
1921 IWL_DEBUG_NOTIF(priv, "BT Coex notification:\n");
1922 IWL_DEBUG_NOTIF(priv, " status: %d\n", coex->bt_status);
1923 IWL_DEBUG_NOTIF(priv, " traffic load: %d\n", coex->bt_traffic_load);
1924 IWL_DEBUG_NOTIF(priv, " CI compliance: %d\n",
1925 coex->bt_ci_compliance);
1926 iwlagn_print_uartmsg(priv, uart_msg);
1927
1928 last_traffic_load = priv->notif_bt_traffic_load;
1929 priv->notif_bt_traffic_load = coex->bt_traffic_load;
1930 if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
1931 if (priv->bt_status != coex->bt_status ||
1932 last_traffic_load != coex->bt_traffic_load) {
1933 if (coex->bt_status) {
1934 /* BT on */
1935 if (!priv->bt_ch_announce)
1936 priv->bt_traffic_load =
1937 IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
1938 else
1939 priv->bt_traffic_load =
1940 coex->bt_traffic_load;
1941 } else {
1942 /* BT off */
1943 priv->bt_traffic_load =
1944 IWL_BT_COEX_TRAFFIC_LOAD_NONE;
1945 }
1946 priv->bt_status = coex->bt_status;
1947 queue_work(priv->workqueue,
1948 &priv->bt_traffic_change_work);
1949 }
1950 if (priv->bt_sco_active !=
1951 (uart_msg->frame3 & BT_UART_MSG_FRAME3SCOESCO_MSK)) {
1952 priv->bt_sco_active = uart_msg->frame3 &
1953 BT_UART_MSG_FRAME3SCOESCO_MSK;
1954 if (priv->bt_sco_active)
1955 sco_cmd.flags |= IWLAGN_BT_SCO_ACTIVE;
1956 iwl_send_cmd_pdu_async(priv, REPLY_BT_COEX_SCO,
1957 sizeof(sco_cmd), &sco_cmd, NULL);
1958 }
1959 }
1960
1961 iwlagn_set_kill_ack_msk(priv, uart_msg);
1962
1963 /* FIXME: based on notification, adjust the prio_boost */
1964
1965 spin_lock_irqsave(&priv->lock, flags);
1966 priv->bt_ci_compliance = coex->bt_ci_compliance;
1967 spin_unlock_irqrestore(&priv->lock, flags);
1968}
1969
1970void iwlagn_bt_rx_handler_setup(struct iwl_priv *priv)
1971{
1972 iwlagn_rx_handler_setup(priv);
1973 priv->rx_handlers[REPLY_BT_COEX_PROFILE_NOTIF] =
1974 iwlagn_bt_coex_profile_notif;
1975}
1976
1977void iwlagn_bt_setup_deferred_work(struct iwl_priv *priv)
1978{
1979 iwlagn_setup_deferred_work(priv);
1980
1981 INIT_WORK(&priv->bt_traffic_change_work,
1982 iwlagn_bt_traffic_change_work);
1983}
1984
1985void iwlagn_bt_cancel_deferred_work(struct iwl_priv *priv)
1986{
1987 cancel_work_sync(&priv->bt_traffic_change_work);
1988}