blob: c3656c46f55f91c44bd44fd9943e8d92f320a388 [file] [log] [blame]
Ron Rindjunsky1053d352008-05-05 10:22:43 +08001/******************************************************************************
2 *
3 * Copyright(c) 2003 - 2008 Intel Corporation. All rights reserved.
4 *
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portions of the ieee80211 subsystem header files.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20 *
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
23 *
24 * Contact Information:
25 * James P. Ketrenos <ipw2100-admin@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *
28 *****************************************************************************/
29
Tomas Winklerfd4abac2008-05-15 13:54:07 +080030#include <linux/etherdevice.h>
Ron Rindjunsky1053d352008-05-05 10:22:43 +080031#include <net/mac80211.h>
32#include "iwl-eeprom.h"
33#include "iwl-dev.h"
34#include "iwl-core.h"
35#include "iwl-sta.h"
36#include "iwl-io.h"
37#include "iwl-helpers.h"
38
Tomas Winkler30e553e2008-05-29 16:35:16 +080039static const u16 default_tid_to_tx_fifo[] = {
40 IWL_TX_FIFO_AC1,
41 IWL_TX_FIFO_AC0,
42 IWL_TX_FIFO_AC0,
43 IWL_TX_FIFO_AC1,
44 IWL_TX_FIFO_AC2,
45 IWL_TX_FIFO_AC2,
46 IWL_TX_FIFO_AC3,
47 IWL_TX_FIFO_AC3,
48 IWL_TX_FIFO_NONE,
49 IWL_TX_FIFO_NONE,
50 IWL_TX_FIFO_NONE,
51 IWL_TX_FIFO_NONE,
52 IWL_TX_FIFO_NONE,
53 IWL_TX_FIFO_NONE,
54 IWL_TX_FIFO_NONE,
55 IWL_TX_FIFO_NONE,
56 IWL_TX_FIFO_AC3
57};
58
Tomas Winkler499b1882008-10-14 12:32:48 -070059static inline dma_addr_t iwl_tfd_tb_get_addr(struct iwl_tfd *tfd, u8 idx)
60{
61 struct iwl_tfd_tb *tb = &tfd->tbs[idx];
62
63 dma_addr_t addr = get_unaligned_le32(&tb->lo);
64 if (sizeof(dma_addr_t) > sizeof(u32))
65 addr |=
66 ((dma_addr_t)(le16_to_cpu(tb->hi_n_len) & 0xF) << 16) << 16;
67
68 return addr;
69}
70
71static inline u16 iwl_tfd_tb_get_len(struct iwl_tfd *tfd, u8 idx)
72{
73 struct iwl_tfd_tb *tb = &tfd->tbs[idx];
74
75 return le16_to_cpu(tb->hi_n_len) >> 4;
76}
77
78static inline void iwl_tfd_set_tb(struct iwl_tfd *tfd, u8 idx,
79 dma_addr_t addr, u16 len)
80{
81 struct iwl_tfd_tb *tb = &tfd->tbs[idx];
82 u16 hi_n_len = len << 4;
83
84 put_unaligned_le32(addr, &tb->lo);
85 if (sizeof(dma_addr_t) > sizeof(u32))
86 hi_n_len |= ((addr >> 16) >> 16) & 0xF;
87
88 tb->hi_n_len = cpu_to_le16(hi_n_len);
89
90 tfd->num_tbs = idx + 1;
91}
92
93static inline u8 iwl_tfd_get_num_tbs(struct iwl_tfd *tfd)
94{
95 return tfd->num_tbs & 0x1f;
96}
Tomas Winkler30e553e2008-05-29 16:35:16 +080097
Ron Rindjunsky1053d352008-05-05 10:22:43 +080098/**
99 * iwl_hw_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr]
Tomas Winkler499b1882008-10-14 12:32:48 -0700100 * @priv - driver private data
101 * @txq - tx queue
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800102 *
103 * Does NOT advance any TFD circular buffer read/write indexes
104 * Does NOT free the TFD itself (which is within circular buffer)
105 */
Tomas Winkler499b1882008-10-14 12:32:48 -0700106static void iwl_hw_txq_free_tfd(struct iwl_priv *priv, struct iwl_tx_queue *txq)
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800107{
Tomas Winkler499b1882008-10-14 12:32:48 -0700108 struct iwl_tfd *tfd_tmp = (struct iwl_tfd *)&txq->tfds[0];
109 struct iwl_tfd *tfd;
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800110 struct pci_dev *dev = priv->pci_dev;
Tomas Winkler499b1882008-10-14 12:32:48 -0700111 int index = txq->q.read_ptr;
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800112 int i;
Tomas Winkler499b1882008-10-14 12:32:48 -0700113 int num_tbs;
114
115 tfd = &tfd_tmp[index];
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800116
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800117 /* Sanity check on number of chunks */
Tomas Winkler499b1882008-10-14 12:32:48 -0700118 num_tbs = iwl_tfd_get_num_tbs(tfd);
119
120 if (num_tbs >= IWL_NUM_OF_TBS) {
121 IWL_ERROR("Too many chunks: %i\n", num_tbs);
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800122 /* @todo issue fatal error, it is quite serious situation */
Tomas Winkler499b1882008-10-14 12:32:48 -0700123 return;
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800124 }
125
Tomas Winkler499b1882008-10-14 12:32:48 -0700126 /* Unmap tx_cmd */
127 if (num_tbs)
128 pci_unmap_single(dev,
129 pci_unmap_addr(&txq->cmd[index]->meta, mapping),
130 pci_unmap_len(&txq->cmd[index]->meta, len),
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800131 PCI_DMA_TODEVICE);
132
Tomas Winkler499b1882008-10-14 12:32:48 -0700133 /* Unmap chunks, if any. */
134 for (i = 1; i < num_tbs; i++) {
135 pci_unmap_single(dev, iwl_tfd_tb_get_addr(tfd, i),
136 iwl_tfd_tb_get_len(tfd, i), PCI_DMA_TODEVICE);
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800137
Tomas Winkler499b1882008-10-14 12:32:48 -0700138 if (txq->txb) {
139 dev_kfree_skb(txq->txb[txq->q.read_ptr].skb[i - 1]);
140 txq->txb[txq->q.read_ptr].skb[i - 1] = NULL;
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800141 }
142 }
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800143}
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800144
Tomas Winkler499b1882008-10-14 12:32:48 -0700145static int iwl_hw_txq_attach_buf_to_tfd(struct iwl_priv *priv,
146 struct iwl_tfd *tfd,
147 dma_addr_t addr, u16 len)
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800148{
Tomas Winkler499b1882008-10-14 12:32:48 -0700149
150 u32 num_tbs = iwl_tfd_get_num_tbs(tfd);
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800151
152 /* Each TFD can point to a maximum 20 Tx buffers */
Tomas Winkler499b1882008-10-14 12:32:48 -0700153 if (num_tbs >= IWL_NUM_OF_TBS) {
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800154 IWL_ERROR("Error can not send more than %d chunks\n",
Tomas Winkler499b1882008-10-14 12:32:48 -0700155 IWL_NUM_OF_TBS);
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800156 return -EINVAL;
157 }
158
Tomas Winkler499b1882008-10-14 12:32:48 -0700159 BUG_ON(addr & ~DMA_BIT_MASK(36));
160 if (unlikely(addr & ~IWL_TX_DMA_MASK))
161 IWL_ERROR("Unaligned address = %llx\n",
162 (unsigned long long)addr);
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800163
Tomas Winkler499b1882008-10-14 12:32:48 -0700164 iwl_tfd_set_tb(tfd, num_tbs, addr, len);
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800165
166 return 0;
167}
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800168
169/**
170 * iwl_txq_update_write_ptr - Send new write index to hardware
171 */
172int iwl_txq_update_write_ptr(struct iwl_priv *priv, struct iwl_tx_queue *txq)
173{
174 u32 reg = 0;
175 int ret = 0;
176 int txq_id = txq->q.id;
177
178 if (txq->need_update == 0)
179 return ret;
180
181 /* if we're trying to save power */
182 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
183 /* wake up nic if it's powered down ...
184 * uCode will wake up, and interrupt us again, so next
185 * time we'll skip this part. */
186 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
187
188 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
189 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
190 iwl_set_bit(priv, CSR_GP_CNTRL,
191 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
192 return ret;
193 }
194
195 /* restore this queue's parameters in nic hardware. */
196 ret = iwl_grab_nic_access(priv);
197 if (ret)
198 return ret;
199 iwl_write_direct32(priv, HBUS_TARG_WRPTR,
200 txq->q.write_ptr | (txq_id << 8));
201 iwl_release_nic_access(priv);
202
203 /* else not in power-save mode, uCode will never sleep when we're
204 * trying to tx (during RFKILL, we're not trying to tx). */
205 } else
206 iwl_write32(priv, HBUS_TARG_WRPTR,
207 txq->q.write_ptr | (txq_id << 8));
208
209 txq->need_update = 0;
210
211 return ret;
212}
213EXPORT_SYMBOL(iwl_txq_update_write_ptr);
214
215
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800216/**
217 * iwl_tx_queue_free - Deallocate DMA queue.
218 * @txq: Transmit queue to deallocate.
219 *
220 * Empty queue by removing and destroying all BD's.
221 * Free all buffers.
222 * 0-fill, but do not free "txq" descriptor structure.
223 */
Gregory Greenmanda99c4b2008-08-04 16:00:40 +0800224static void iwl_tx_queue_free(struct iwl_priv *priv, int txq_id)
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800225{
Gregory Greenmanda99c4b2008-08-04 16:00:40 +0800226 struct iwl_tx_queue *txq = &priv->txq[txq_id];
Tomas Winkler443cfd42008-05-15 13:53:57 +0800227 struct iwl_queue *q = &txq->q;
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800228 struct pci_dev *dev = priv->pci_dev;
Tomas Winkler961ba602008-10-14 12:32:44 -0700229 int i, len;
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800230
231 if (q->n_bd == 0)
232 return;
233
234 /* first, empty all BD's */
235 for (; q->write_ptr != q->read_ptr;
236 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd))
237 iwl_hw_txq_free_tfd(priv, txq);
238
239 len = sizeof(struct iwl_cmd) * q->n_window;
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800240
241 /* De-alloc array of command/tx buffers */
Tomas Winkler961ba602008-10-14 12:32:44 -0700242 for (i = 0; i < TFD_TX_CMD_SLOTS; i++)
Gregory Greenmanda99c4b2008-08-04 16:00:40 +0800243 kfree(txq->cmd[i]);
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800244
245 /* De-alloc circular buffer of TFDs */
246 if (txq->q.n_bd)
Tomas Winkler499b1882008-10-14 12:32:48 -0700247 pci_free_consistent(dev, sizeof(struct iwl_tfd) *
248 txq->q.n_bd, txq->tfds, txq->q.dma_addr);
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800249
250 /* De-alloc array of per-TFD driver data */
251 kfree(txq->txb);
252 txq->txb = NULL;
253
254 /* 0-fill queue descriptor structure */
255 memset(txq, 0, sizeof(*txq));
256}
257
Tomas Winkler961ba602008-10-14 12:32:44 -0700258
259/**
260 * iwl_cmd_queue_free - Deallocate DMA queue.
261 * @txq: Transmit queue to deallocate.
262 *
263 * Empty queue by removing and destroying all BD's.
264 * Free all buffers.
265 * 0-fill, but do not free "txq" descriptor structure.
266 */
267static void iwl_cmd_queue_free(struct iwl_priv *priv)
268{
269 struct iwl_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
270 struct iwl_queue *q = &txq->q;
271 struct pci_dev *dev = priv->pci_dev;
272 int i, len;
273
274 if (q->n_bd == 0)
275 return;
276
277 len = sizeof(struct iwl_cmd) * q->n_window;
278 len += IWL_MAX_SCAN_SIZE;
279
280 /* De-alloc array of command/tx buffers */
281 for (i = 0; i <= TFD_CMD_SLOTS; i++)
282 kfree(txq->cmd[i]);
283
284 /* De-alloc circular buffer of TFDs */
285 if (txq->q.n_bd)
Tomas Winkler499b1882008-10-14 12:32:48 -0700286 pci_free_consistent(dev, sizeof(struct iwl_tfd) *
287 txq->q.n_bd, txq->tfds, txq->q.dma_addr);
Tomas Winkler961ba602008-10-14 12:32:44 -0700288
289 /* 0-fill queue descriptor structure */
290 memset(txq, 0, sizeof(*txq));
291}
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800292/*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
293 * DMA services
294 *
295 * Theory of operation
296 *
297 * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
298 * of buffer descriptors, each of which points to one or more data buffers for
299 * the device to read from or fill. Driver and device exchange status of each
300 * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
301 * entries in each circular buffer, to protect against confusing empty and full
302 * queue states.
303 *
304 * The device reads or writes the data in the queues via the device's several
305 * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
306 *
307 * For Tx queue, there are low mark and high mark limits. If, after queuing
308 * the packet for Tx, free space become < low mark, Tx queue stopped. When
309 * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
310 * Tx queue resumed.
311 *
312 * See more detailed info in iwl-4965-hw.h.
313 ***************************************************/
314
315int iwl_queue_space(const struct iwl_queue *q)
316{
317 int s = q->read_ptr - q->write_ptr;
318
319 if (q->read_ptr > q->write_ptr)
320 s -= q->n_bd;
321
322 if (s <= 0)
323 s += q->n_window;
324 /* keep some reserve to not confuse empty and full situations */
325 s -= 2;
326 if (s < 0)
327 s = 0;
328 return s;
329}
330EXPORT_SYMBOL(iwl_queue_space);
331
332
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800333/**
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800334 * iwl_queue_init - Initialize queue's high/low-water and read/write indexes
335 */
Tomas Winkler443cfd42008-05-15 13:53:57 +0800336static int iwl_queue_init(struct iwl_priv *priv, struct iwl_queue *q,
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800337 int count, int slots_num, u32 id)
338{
339 q->n_bd = count;
340 q->n_window = slots_num;
341 q->id = id;
342
343 /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
344 * and iwl_queue_dec_wrap are broken. */
345 BUG_ON(!is_power_of_2(count));
346
347 /* slots_num must be power-of-two size, otherwise
348 * get_cmd_index is broken. */
349 BUG_ON(!is_power_of_2(slots_num));
350
351 q->low_mark = q->n_window / 4;
352 if (q->low_mark < 4)
353 q->low_mark = 4;
354
355 q->high_mark = q->n_window / 8;
356 if (q->high_mark < 2)
357 q->high_mark = 2;
358
359 q->write_ptr = q->read_ptr = 0;
360
361 return 0;
362}
363
364/**
365 * iwl_tx_queue_alloc - Alloc driver data and TFD CB for one Tx/cmd queue
366 */
367static int iwl_tx_queue_alloc(struct iwl_priv *priv,
Ron Rindjunsky16466902008-05-05 10:22:50 +0800368 struct iwl_tx_queue *txq, u32 id)
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800369{
370 struct pci_dev *dev = priv->pci_dev;
371
372 /* Driver private data, only for Tx (not command) queues,
373 * not shared with device. */
374 if (id != IWL_CMD_QUEUE_NUM) {
375 txq->txb = kmalloc(sizeof(txq->txb[0]) *
376 TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
377 if (!txq->txb) {
378 IWL_ERROR("kmalloc for auxiliary BD "
379 "structures failed\n");
380 goto error;
381 }
382 } else
383 txq->txb = NULL;
384
385 /* Circular buffer of transmit frame descriptors (TFDs),
386 * shared with device */
Tomas Winkler499b1882008-10-14 12:32:48 -0700387 txq->tfds = pci_alloc_consistent(dev,
388 sizeof(txq->tfds[0]) * TFD_QUEUE_SIZE_MAX,
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800389 &txq->q.dma_addr);
390
Tomas Winkler499b1882008-10-14 12:32:48 -0700391 if (!txq->tfds) {
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800392 IWL_ERROR("pci_alloc_consistent(%zd) failed\n",
Tomas Winkler499b1882008-10-14 12:32:48 -0700393 sizeof(txq->tfds[0]) * TFD_QUEUE_SIZE_MAX);
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800394 goto error;
395 }
396 txq->q.id = id;
397
398 return 0;
399
400 error:
401 kfree(txq->txb);
402 txq->txb = NULL;
403
404 return -ENOMEM;
405}
406
407/*
408 * Tell nic where to find circular buffer of Tx Frame Descriptors for
409 * given Tx queue, and enable the DMA channel used for that queue.
410 *
411 * 4965 supports up to 16 Tx queues in DRAM, mapped to up to 8 Tx DMA
412 * channels supported in hardware.
413 */
414static int iwl_hw_tx_queue_init(struct iwl_priv *priv,
Ron Rindjunsky16466902008-05-05 10:22:50 +0800415 struct iwl_tx_queue *txq)
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800416{
Tomas Winkler499b1882008-10-14 12:32:48 -0700417 int ret;
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800418 unsigned long flags;
419 int txq_id = txq->q.id;
420
421 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler499b1882008-10-14 12:32:48 -0700422 ret = iwl_grab_nic_access(priv);
423 if (ret) {
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800424 spin_unlock_irqrestore(&priv->lock, flags);
Tomas Winkler499b1882008-10-14 12:32:48 -0700425 return ret;
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800426 }
427
428 /* Circular buffer (TFD queue in DRAM) physical base address */
429 iwl_write_direct32(priv, FH_MEM_CBBC_QUEUE(txq_id),
430 txq->q.dma_addr >> 8);
431
432 /* Enable DMA channel, using same id as for TFD queue */
Tomas Winkler499b1882008-10-14 12:32:48 -0700433 iwl_write_direct32(priv, FH_TCSR_CHNL_TX_CONFIG_REG(txq_id),
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800434 FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
435 FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE_VAL);
Tomas Winkler499b1882008-10-14 12:32:48 -0700436
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800437 iwl_release_nic_access(priv);
438 spin_unlock_irqrestore(&priv->lock, flags);
439
440 return 0;
441}
442
443/**
444 * iwl_tx_queue_init - Allocate and initialize one tx/cmd queue
445 */
Tomas Winkler73b7d742008-09-03 11:18:48 +0800446static int iwl_tx_queue_init(struct iwl_priv *priv, struct iwl_tx_queue *txq,
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800447 int slots_num, u32 txq_id)
448{
Gregory Greenmanda99c4b2008-08-04 16:00:40 +0800449 int i, len;
Tomas Winkler73b7d742008-09-03 11:18:48 +0800450 int ret;
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800451
452 /*
453 * Alloc buffer array for commands (Tx or other types of commands).
454 * For the command queue (#4), allocate command space + one big
455 * command for scan, since scan command is very huge; the system will
456 * not have two scans at the same time, so only one is needed.
457 * For normal Tx queues (all other queues), no super-size command
458 * space is needed.
459 */
Gregory Greenmanda99c4b2008-08-04 16:00:40 +0800460 len = sizeof(struct iwl_cmd);
461 for (i = 0; i <= slots_num; i++) {
462 if (i == slots_num) {
463 if (txq_id == IWL_CMD_QUEUE_NUM)
464 len += IWL_MAX_SCAN_SIZE;
465 else
466 continue;
467 }
468
John W. Linville49898852008-09-02 15:07:18 -0400469 txq->cmd[i] = kmalloc(len, GFP_KERNEL);
Gregory Greenmanda99c4b2008-08-04 16:00:40 +0800470 if (!txq->cmd[i])
Tomas Winkler73b7d742008-09-03 11:18:48 +0800471 goto err;
Gregory Greenmanda99c4b2008-08-04 16:00:40 +0800472 }
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800473
474 /* Alloc driver data array and TFD circular buffer */
Tomas Winkler73b7d742008-09-03 11:18:48 +0800475 ret = iwl_tx_queue_alloc(priv, txq, txq_id);
476 if (ret)
477 goto err;
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800478
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800479 txq->need_update = 0;
480
481 /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
482 * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
483 BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
484
485 /* Initialize queue's high/low-water marks, and head/tail indexes */
486 iwl_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
487
488 /* Tell device where to find queue */
489 iwl_hw_tx_queue_init(priv, txq);
490
491 return 0;
Tomas Winkler73b7d742008-09-03 11:18:48 +0800492err:
493 for (i = 0; i < slots_num; i++) {
494 kfree(txq->cmd[i]);
495 txq->cmd[i] = NULL;
496 }
497
498 if (txq_id == IWL_CMD_QUEUE_NUM) {
499 kfree(txq->cmd[slots_num]);
500 txq->cmd[slots_num] = NULL;
501 }
502 return -ENOMEM;
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800503}
Tomas Winklerda1bc452008-05-29 16:35:00 +0800504/**
505 * iwl_hw_txq_ctx_free - Free TXQ Context
506 *
507 * Destroy all TX DMA queues and structures
508 */
509void iwl_hw_txq_ctx_free(struct iwl_priv *priv)
510{
511 int txq_id;
512
513 /* Tx queues */
514 for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++)
Tomas Winkler961ba602008-10-14 12:32:44 -0700515 if (txq_id == IWL_CMD_QUEUE_NUM)
516 iwl_cmd_queue_free(priv);
517 else
518 iwl_tx_queue_free(priv, txq_id);
Tomas Winklerda1bc452008-05-29 16:35:00 +0800519
520 /* Keep-warm buffer */
521 iwl_kw_free(priv);
522}
523EXPORT_SYMBOL(iwl_hw_txq_ctx_free);
524
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800525/**
526 * iwl_txq_ctx_reset - Reset TX queue context
527 * Destroys all DMA structures and initialise them again
528 *
529 * @param priv
530 * @return error code
531 */
532int iwl_txq_ctx_reset(struct iwl_priv *priv)
533{
534 int ret = 0;
535 int txq_id, slots_num;
Tomas Winklerda1bc452008-05-29 16:35:00 +0800536 unsigned long flags;
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800537
538 iwl_kw_free(priv);
539
540 /* Free all tx/cmd queues and keep-warm buffer */
541 iwl_hw_txq_ctx_free(priv);
542
543 /* Alloc keep-warm buffer */
544 ret = iwl_kw_alloc(priv);
545 if (ret) {
Jiri Slaby6f147922008-08-11 23:49:41 +0200546 IWL_ERROR("Keep Warm allocation failed\n");
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800547 goto error_kw;
548 }
Tomas Winklerda1bc452008-05-29 16:35:00 +0800549 spin_lock_irqsave(&priv->lock, flags);
550 ret = iwl_grab_nic_access(priv);
551 if (unlikely(ret)) {
552 spin_unlock_irqrestore(&priv->lock, flags);
553 goto error_reset;
554 }
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800555
556 /* Turn off all Tx DMA fifos */
Tomas Winklerda1bc452008-05-29 16:35:00 +0800557 priv->cfg->ops->lib->txq_set_sched(priv, 0);
558
559 iwl_release_nic_access(priv);
560 spin_unlock_irqrestore(&priv->lock, flags);
561
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800562
563 /* Tell nic where to find the keep-warm buffer */
564 ret = iwl_kw_init(priv);
565 if (ret) {
566 IWL_ERROR("kw_init failed\n");
567 goto error_reset;
568 }
569
Tomas Winklerda1bc452008-05-29 16:35:00 +0800570 /* Alloc and init all Tx queues, including the command queue (#4) */
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800571 for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++) {
572 slots_num = (txq_id == IWL_CMD_QUEUE_NUM) ?
573 TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
574 ret = iwl_tx_queue_init(priv, &priv->txq[txq_id], slots_num,
575 txq_id);
576 if (ret) {
577 IWL_ERROR("Tx %d queue init failed\n", txq_id);
578 goto error;
579 }
580 }
581
582 return ret;
583
584 error:
585 iwl_hw_txq_ctx_free(priv);
586 error_reset:
587 iwl_kw_free(priv);
588 error_kw:
589 return ret;
590}
Emmanuel Grumbacha33c2f42008-09-03 11:26:56 +0800591
Tomas Winklerda1bc452008-05-29 16:35:00 +0800592/**
593 * iwl_txq_ctx_stop - Stop all Tx DMA channels, free Tx queue memory
594 */
595void iwl_txq_ctx_stop(struct iwl_priv *priv)
596{
597
598 int txq_id;
599 unsigned long flags;
600
601
602 /* Turn off all Tx DMA fifos */
603 spin_lock_irqsave(&priv->lock, flags);
604 if (iwl_grab_nic_access(priv)) {
605 spin_unlock_irqrestore(&priv->lock, flags);
606 return;
607 }
608
609 priv->cfg->ops->lib->txq_set_sched(priv, 0);
610
611 /* Stop each Tx DMA channel, and wait for it to be idle */
612 for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++) {
613 iwl_write_direct32(priv,
614 FH_TCSR_CHNL_TX_CONFIG_REG(txq_id), 0x0);
615 iwl_poll_direct_bit(priv, FH_TSSR_TX_STATUS_REG,
616 FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE
617 (txq_id), 200);
618 }
619 iwl_release_nic_access(priv);
620 spin_unlock_irqrestore(&priv->lock, flags);
621
622 /* Deallocate memory for all Tx queues */
623 iwl_hw_txq_ctx_free(priv);
624}
625EXPORT_SYMBOL(iwl_txq_ctx_stop);
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800626
627/*
628 * handle build REPLY_TX command notification.
629 */
630static void iwl_tx_cmd_build_basic(struct iwl_priv *priv,
631 struct iwl_tx_cmd *tx_cmd,
Johannes Berge039fa42008-05-15 12:55:29 +0200632 struct ieee80211_tx_info *info,
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800633 struct ieee80211_hdr *hdr,
634 int is_unicast, u8 std_id)
635{
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700636 __le16 fc = hdr->frame_control;
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800637 __le32 tx_flags = tx_cmd->tx_flags;
638
639 tx_cmd->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
Johannes Berge039fa42008-05-15 12:55:29 +0200640 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800641 tx_flags |= TX_CMD_FLG_ACK_MSK;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700642 if (ieee80211_is_mgmt(fc))
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800643 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700644 if (ieee80211_is_probe_resp(fc) &&
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800645 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
646 tx_flags |= TX_CMD_FLG_TSF_MSK;
647 } else {
648 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
649 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
650 }
651
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700652 if (ieee80211_is_back_req(fc))
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800653 tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
654
655
656 tx_cmd->sta_id = std_id;
Harvey Harrison8b7b1e02008-06-11 14:21:56 -0700657 if (ieee80211_has_morefrags(fc))
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800658 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
659
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700660 if (ieee80211_is_data_qos(fc)) {
661 u8 *qc = ieee80211_get_qos_ctl(hdr);
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800662 tx_cmd->tid_tspec = qc[0] & 0xf;
663 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
664 } else {
665 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
666 }
667
Emmanuel Grumbacha326a5d2008-07-11 11:53:31 +0800668 priv->cfg->ops->utils->rts_tx_cmd_flag(info, &tx_flags);
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800669
670 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
671 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
672
673 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700674 if (ieee80211_is_mgmt(fc)) {
675 if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800676 tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(3);
677 else
678 tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(2);
679 } else {
680 tx_cmd->timeout.pm_frame_timeout = 0;
681 }
682
683 tx_cmd->driver_txop = 0;
684 tx_cmd->tx_flags = tx_flags;
685 tx_cmd->next_frame_len = 0;
686}
687
688#define RTS_HCCA_RETRY_LIMIT 3
689#define RTS_DFAULT_RETRY_LIMIT 60
690
691static void iwl_tx_cmd_build_rate(struct iwl_priv *priv,
692 struct iwl_tx_cmd *tx_cmd,
Johannes Berge039fa42008-05-15 12:55:29 +0200693 struct ieee80211_tx_info *info,
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700694 __le16 fc, int sta_id,
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800695 int is_hcca)
696{
Tomas Winkler76eff182008-10-14 12:32:45 -0700697 u32 rate_flags = 0;
698 int rate_idx;
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800699 u8 rts_retry_limit = 0;
700 u8 data_retry_limit = 0;
701 u8 rate_plcp;
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200702
Johannes Berge039fa42008-05-15 12:55:29 +0200703 rate_idx = min(ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xffff,
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200704 IWL_RATE_COUNT - 1);
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800705
706 rate_plcp = iwl_rates[rate_idx].plcp;
707
708 rts_retry_limit = (is_hcca) ?
709 RTS_HCCA_RETRY_LIMIT : RTS_DFAULT_RETRY_LIMIT;
710
711 if ((rate_idx >= IWL_FIRST_CCK_RATE) && (rate_idx <= IWL_LAST_CCK_RATE))
712 rate_flags |= RATE_MCS_CCK_MSK;
713
714
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700715 if (ieee80211_is_probe_resp(fc)) {
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800716 data_retry_limit = 3;
717 if (data_retry_limit < rts_retry_limit)
718 rts_retry_limit = data_retry_limit;
719 } else
720 data_retry_limit = IWL_DEFAULT_TX_RETRY;
721
722 if (priv->data_retry_limit != -1)
723 data_retry_limit = priv->data_retry_limit;
724
725
726 if (ieee80211_is_data(fc)) {
727 tx_cmd->initial_rate_index = 0;
728 tx_cmd->tx_flags |= TX_CMD_FLG_STA_RATE_MSK;
729 } else {
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700730 switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
731 case cpu_to_le16(IEEE80211_STYPE_AUTH):
732 case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
733 case cpu_to_le16(IEEE80211_STYPE_ASSOC_REQ):
734 case cpu_to_le16(IEEE80211_STYPE_REASSOC_REQ):
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800735 if (tx_cmd->tx_flags & TX_CMD_FLG_RTS_MSK) {
736 tx_cmd->tx_flags &= ~TX_CMD_FLG_RTS_MSK;
737 tx_cmd->tx_flags |= TX_CMD_FLG_CTS_MSK;
738 }
739 break;
740 default:
741 break;
742 }
743
Tomas Winkler76eff182008-10-14 12:32:45 -0700744 priv->mgmt_tx_ant = iwl_toggle_tx_ant(priv, priv->mgmt_tx_ant);
745 rate_flags |= iwl_ant_idx_to_flags(priv->mgmt_tx_ant);
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800746 }
747
748 tx_cmd->rts_retry_limit = rts_retry_limit;
749 tx_cmd->data_retry_limit = data_retry_limit;
Tomas Winklere7d326ac2008-06-12 09:47:11 +0800750 tx_cmd->rate_n_flags = iwl_hw_set_rate_n_flags(rate_plcp, rate_flags);
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800751}
752
753static void iwl_tx_cmd_build_hwcrypto(struct iwl_priv *priv,
Johannes Berge039fa42008-05-15 12:55:29 +0200754 struct ieee80211_tx_info *info,
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800755 struct iwl_tx_cmd *tx_cmd,
756 struct sk_buff *skb_frag,
757 int sta_id)
758{
Johannes Berge039fa42008-05-15 12:55:29 +0200759 struct ieee80211_key_conf *keyconf = info->control.hw_key;
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800760
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800761 switch (keyconf->alg) {
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800762 case ALG_CCMP:
763 tx_cmd->sec_ctl = TX_CMD_SEC_CCM;
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800764 memcpy(tx_cmd->key, keyconf->key, keyconf->keylen);
Johannes Berge039fa42008-05-15 12:55:29 +0200765 if (info->flags & IEEE80211_TX_CTL_AMPDU)
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800766 tx_cmd->tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
767 IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
768 break;
769
770 case ALG_TKIP:
771 tx_cmd->sec_ctl = TX_CMD_SEC_TKIP;
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800772 ieee80211_get_tkip_key(keyconf, skb_frag,
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800773 IEEE80211_TKIP_P2_KEY, tx_cmd->key);
774 IWL_DEBUG_TX("tx_cmd with tkip hwcrypto\n");
775 break;
776
777 case ALG_WEP:
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800778 tx_cmd->sec_ctl |= (TX_CMD_SEC_WEP |
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800779 (keyconf->keyidx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT);
780
781 if (keyconf->keylen == WEP_KEY_LEN_128)
782 tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
783
784 memcpy(&tx_cmd->key[3], keyconf->key, keyconf->keylen);
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800785
786 IWL_DEBUG_TX("Configuring packet for WEP encryption "
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800787 "with key %d\n", keyconf->keyidx);
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800788 break;
789
790 default:
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800791 printk(KERN_ERR "Unknown encode alg %d\n", keyconf->alg);
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800792 break;
793 }
794}
795
796static void iwl_update_tx_stats(struct iwl_priv *priv, u16 fc, u16 len)
797{
798 /* 0 - mgmt, 1 - cnt, 2 - data */
799 int idx = (fc & IEEE80211_FCTL_FTYPE) >> 2;
800 priv->tx_stats[idx].cnt++;
801 priv->tx_stats[idx].bytes += len;
802}
803
804/*
805 * start REPLY_TX command process
806 */
Johannes Berge039fa42008-05-15 12:55:29 +0200807int iwl_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800808{
809 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berge039fa42008-05-15 12:55:29 +0200810 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Tomas Winkler499b1882008-10-14 12:32:48 -0700811 struct iwl_tfd *tfd;
Tomas Winklerf3674222008-08-04 16:00:44 +0800812 struct iwl_tx_queue *txq;
813 struct iwl_queue *q;
814 struct iwl_cmd *out_cmd;
815 struct iwl_tx_cmd *tx_cmd;
816 int swq_id, txq_id;
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800817 dma_addr_t phys_addr;
818 dma_addr_t txcmd_phys;
819 dma_addr_t scratch_phys;
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800820 u16 len, idx, len_org;
821 u16 seq_number = 0;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700822 __le16 fc;
Tomas Winklerf3674222008-08-04 16:00:44 +0800823 u8 hdr_len, unicast;
824 u8 sta_id;
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800825 u8 wait_write_ptr = 0;
826 u8 tid = 0;
827 u8 *qc = NULL;
828 unsigned long flags;
829 int ret;
830
831 spin_lock_irqsave(&priv->lock, flags);
832 if (iwl_is_rfkill(priv)) {
833 IWL_DEBUG_DROP("Dropping - RF KILL\n");
834 goto drop_unlock;
835 }
836
Johannes Berge039fa42008-05-15 12:55:29 +0200837 if ((ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xFF) ==
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200838 IWL_INVALID_RATE) {
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800839 IWL_ERROR("ERROR: No TX rate available.\n");
840 goto drop_unlock;
841 }
842
843 unicast = !is_multicast_ether_addr(hdr->addr1);
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800844
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700845 fc = hdr->frame_control;
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800846
847#ifdef CONFIG_IWLWIFI_DEBUG
848 if (ieee80211_is_auth(fc))
849 IWL_DEBUG_TX("Sending AUTH frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700850 else if (ieee80211_is_assoc_req(fc))
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800851 IWL_DEBUG_TX("Sending ASSOC frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700852 else if (ieee80211_is_reassoc_req(fc))
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800853 IWL_DEBUG_TX("Sending REASSOC frame\n");
854#endif
855
856 /* drop all data frame if we are not associated */
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700857 if (ieee80211_is_data(fc) &&
Johannes Berg05c914f2008-09-11 00:01:58 +0200858 (priv->iw_mode != NL80211_IFTYPE_MONITOR ||
Stefanik Gábord10c4ec2008-09-03 11:26:59 +0800859 !(info->flags & IEEE80211_TX_CTL_INJECTED)) && /* packet injection */
860 (!iwl_is_associated(priv) ||
Johannes Berg05c914f2008-09-11 00:01:58 +0200861 ((priv->iw_mode == NL80211_IFTYPE_STATION) && !priv->assoc_id) ||
Stefanik Gábord10c4ec2008-09-03 11:26:59 +0800862 !priv->assoc_station_added)) {
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800863 IWL_DEBUG_DROP("Dropping - !iwl_is_associated\n");
864 goto drop_unlock;
865 }
866
867 spin_unlock_irqrestore(&priv->lock, flags);
868
Harvey Harrison7294ec92008-07-15 18:43:59 -0700869 hdr_len = ieee80211_hdrlen(fc);
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800870
871 /* Find (or create) index into station table for destination station */
872 sta_id = iwl_get_sta_id(priv, hdr);
873 if (sta_id == IWL_INVALID_STATION) {
Johannes Berge1749612008-10-27 15:59:26 -0700874 IWL_DEBUG_DROP("Dropping - INVALID STATION: %pM\n",
875 hdr->addr1);
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800876 goto drop;
877 }
878
879 IWL_DEBUG_TX("station Id %d\n", sta_id);
880
Tomas Winklerf3674222008-08-04 16:00:44 +0800881 swq_id = skb_get_queue_mapping(skb);
882 txq_id = swq_id;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700883 if (ieee80211_is_data_qos(fc)) {
884 qc = ieee80211_get_qos_ctl(hdr);
Harvey Harrison7294ec92008-07-15 18:43:59 -0700885 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
Tomas Winklerf3674222008-08-04 16:00:44 +0800886 seq_number = priv->stations[sta_id].tid[tid].seq_number;
887 seq_number &= IEEE80211_SCTL_SEQ;
888 hdr->seq_ctrl = hdr->seq_ctrl &
889 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG);
890 hdr->seq_ctrl |= cpu_to_le16(seq_number);
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800891 seq_number += 0x10;
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800892 /* aggregation is on for this <sta,tid> */
Johannes Berge039fa42008-05-15 12:55:29 +0200893 if (info->flags & IEEE80211_TX_CTL_AMPDU)
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800894 txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
895 priv->stations[sta_id].tid[tid].tfds_in_queue++;
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800896 }
897
898 /* Descriptor for chosen Tx queue */
899 txq = &priv->txq[txq_id];
900 q = &txq->q;
901
902 spin_lock_irqsave(&priv->lock, flags);
903
904 /* Set up first empty TFD within this queue's circular TFD buffer */
Tomas Winkler499b1882008-10-14 12:32:48 -0700905 tfd = &txq->tfds[q->write_ptr];
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800906 memset(tfd, 0, sizeof(*tfd));
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800907 idx = get_cmd_index(q, q->write_ptr, 0);
908
909 /* Set up driver data for this TFD */
910 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
911 txq->txb[q->write_ptr].skb[0] = skb;
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800912
913 /* Set up first empty entry in queue's array of Tx/cmd buffers */
Gregory Greenmanda99c4b2008-08-04 16:00:40 +0800914 out_cmd = txq->cmd[idx];
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800915 tx_cmd = &out_cmd->cmd.tx;
916 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
917 memset(tx_cmd, 0, sizeof(struct iwl_tx_cmd));
918
919 /*
920 * Set up the Tx-command (not MAC!) header.
921 * Store the chosen Tx queue and TFD index within the sequence field;
922 * after Tx, uCode's Tx response will return this value so driver can
923 * locate the frame within the tx queue and do post-tx processing.
924 */
925 out_cmd->hdr.cmd = REPLY_TX;
926 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
927 INDEX_TO_SEQ(q->write_ptr)));
928
929 /* Copy MAC header from skb into command buffer */
930 memcpy(tx_cmd->hdr, hdr, hdr_len);
931
932 /*
933 * Use the first empty entry in this queue's command buffer array
934 * to contain the Tx command and MAC header concatenated together
935 * (payload data will be in another buffer).
936 * Size of this varies, due to varying MAC header length.
937 * If end is not dword aligned, we'll have 2 extra bytes at the end
938 * of the MAC header (device reads on dword boundaries).
939 * We'll tell device about this padding later.
940 */
941 len = sizeof(struct iwl_tx_cmd) +
942 sizeof(struct iwl_cmd_header) + hdr_len;
943
944 len_org = len;
945 len = (len + 3) & ~3;
946
947 if (len_org != len)
948 len_org = 1;
949 else
950 len_org = 0;
951
952 /* Physical address of this Tx command's header (not MAC header!),
953 * within command buffer array. */
Tomas Winkler499b1882008-10-14 12:32:48 -0700954 txcmd_phys = pci_map_single(priv->pci_dev,
955 out_cmd, sizeof(struct iwl_cmd),
956 PCI_DMA_TODEVICE);
957 pci_unmap_addr_set(&out_cmd->meta, mapping, txcmd_phys);
958 pci_unmap_len_set(&out_cmd->meta, len, sizeof(struct iwl_cmd));
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800959 /* Add buffer containing Tx command and MAC(!) header to TFD's
960 * first entry */
Tomas Winkler499b1882008-10-14 12:32:48 -0700961 txcmd_phys += offsetof(struct iwl_cmd, hdr);
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800962 iwl_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
963
Johannes Bergd0f09802008-07-29 11:32:07 +0200964 if (info->control.hw_key)
Johannes Berge039fa42008-05-15 12:55:29 +0200965 iwl_tx_cmd_build_hwcrypto(priv, info, tx_cmd, skb, sta_id);
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800966
967 /* Set up TFD's 2nd entry to point directly to remainder of skb,
968 * if any (802.11 null frames have no payload). */
969 len = skb->len - hdr_len;
970 if (len) {
971 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
972 len, PCI_DMA_TODEVICE);
973 iwl_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
974 }
975
976 /* Tell NIC about any 2-byte padding after MAC header */
977 if (len_org)
978 tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
979
980 /* Total # bytes to be transmitted */
981 len = (u16)skb->len;
982 tx_cmd->len = cpu_to_le16(len);
983 /* TODO need this for burst mode later on */
Johannes Berge039fa42008-05-15 12:55:29 +0200984 iwl_tx_cmd_build_basic(priv, tx_cmd, info, hdr, unicast, sta_id);
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800985
986 /* set is_hcca to 0; it probably will never be implemented */
Johannes Berge039fa42008-05-15 12:55:29 +0200987 iwl_tx_cmd_build_rate(priv, tx_cmd, info, fc, sta_id, 0);
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800988
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700989 iwl_update_tx_stats(priv, le16_to_cpu(fc), len);
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800990
991 scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
992 offsetof(struct iwl_tx_cmd, scratch);
993 tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
Tomas Winkler499b1882008-10-14 12:32:48 -0700994 tx_cmd->dram_msb_ptr = iwl_get_dma_hi_addr(scratch_phys);
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800995
Harvey Harrison8b7b1e02008-06-11 14:21:56 -0700996 if (!ieee80211_has_morefrags(hdr->frame_control)) {
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800997 txq->need_update = 1;
998 if (qc)
999 priv->stations[sta_id].tid[tid].seq_number = seq_number;
1000 } else {
1001 wait_write_ptr = 1;
1002 txq->need_update = 0;
1003 }
1004
1005 iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx_cmd, sizeof(*tx_cmd));
1006
1007 iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx_cmd->hdr, hdr_len);
1008
1009 /* Set up entry for this TFD in Tx byte-count array */
1010 priv->cfg->ops->lib->txq_update_byte_cnt_tbl(priv, txq, len);
1011
1012 /* Tell device the write index *just past* this latest filled TFD */
1013 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
1014 ret = iwl_txq_update_write_ptr(priv, txq);
1015 spin_unlock_irqrestore(&priv->lock, flags);
1016
1017 if (ret)
1018 return ret;
1019
Tomas Winkler143b09e2008-07-24 21:33:42 +03001020 if ((iwl_queue_space(q) < q->high_mark) && priv->mac80211_registered) {
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001021 if (wait_write_ptr) {
1022 spin_lock_irqsave(&priv->lock, flags);
1023 txq->need_update = 1;
1024 iwl_txq_update_write_ptr(priv, txq);
1025 spin_unlock_irqrestore(&priv->lock, flags);
Tomas Winkler143b09e2008-07-24 21:33:42 +03001026 } else {
Tomas Winklerf3674222008-08-04 16:00:44 +08001027 ieee80211_stop_queue(priv->hw, swq_id);
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001028 }
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001029 }
1030
1031 return 0;
1032
1033drop_unlock:
1034 spin_unlock_irqrestore(&priv->lock, flags);
1035drop:
1036 return -1;
1037}
1038EXPORT_SYMBOL(iwl_tx_skb);
1039
1040/*************** HOST COMMAND QUEUE FUNCTIONS *****/
1041
1042/**
1043 * iwl_enqueue_hcmd - enqueue a uCode command
1044 * @priv: device private data point
1045 * @cmd: a point to the ucode command structure
1046 *
1047 * The function returns < 0 values to indicate the operation is
1048 * failed. On success, it turns the index (> 0) of command in the
1049 * command queue.
1050 */
1051int iwl_enqueue_hcmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
1052{
1053 struct iwl_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
1054 struct iwl_queue *q = &txq->q;
Tomas Winkler499b1882008-10-14 12:32:48 -07001055 struct iwl_tfd *tfd;
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001056 struct iwl_cmd *out_cmd;
Tomas Winklerf3674222008-08-04 16:00:44 +08001057 dma_addr_t phys_addr;
1058 unsigned long flags;
1059 int len, ret;
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001060 u32 idx;
1061 u16 fix_size;
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001062
1063 cmd->len = priv->cfg->ops->utils->get_hcmd_size(cmd->id, cmd->len);
1064 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
1065
1066 /* If any of the command structures end up being larger than
1067 * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
1068 * we will need to increase the size of the TFD entries */
1069 BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
1070 !(cmd->meta.flags & CMD_SIZE_HUGE));
1071
1072 if (iwl_is_rfkill(priv)) {
1073 IWL_DEBUG_INFO("Not sending command - RF KILL");
1074 return -EIO;
1075 }
1076
1077 if (iwl_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
1078 IWL_ERROR("No space for Tx\n");
1079 return -ENOSPC;
1080 }
1081
1082 spin_lock_irqsave(&priv->hcmd_lock, flags);
1083
Tomas Winkler499b1882008-10-14 12:32:48 -07001084 tfd = &txq->tfds[q->write_ptr];
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001085 memset(tfd, 0, sizeof(*tfd));
1086
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001087
1088 idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
Gregory Greenmanda99c4b2008-08-04 16:00:40 +08001089 out_cmd = txq->cmd[idx];
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001090
1091 out_cmd->hdr.cmd = cmd->id;
1092 memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
1093 memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
1094
1095 /* At this point, the out_cmd now has all of the incoming cmd
1096 * information */
1097
1098 out_cmd->hdr.flags = 0;
1099 out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
1100 INDEX_TO_SEQ(q->write_ptr));
1101 if (out_cmd->meta.flags & CMD_SIZE_HUGE)
Tomas Winkler9734cb22008-09-03 11:26:52 +08001102 out_cmd->hdr.sequence |= SEQ_HUGE_FRAME;
Gregory Greenmanda99c4b2008-08-04 16:00:40 +08001103 len = (idx == TFD_CMD_SLOTS) ?
1104 IWL_MAX_SCAN_SIZE : sizeof(struct iwl_cmd);
Tomas Winkler499b1882008-10-14 12:32:48 -07001105
1106 phys_addr = pci_map_single(priv->pci_dev, out_cmd,
1107 len, PCI_DMA_TODEVICE);
1108 pci_unmap_addr_set(&out_cmd->meta, mapping, phys_addr);
1109 pci_unmap_len_set(&out_cmd->meta, len, len);
Gregory Greenmanda99c4b2008-08-04 16:00:40 +08001110 phys_addr += offsetof(struct iwl_cmd, hdr);
Tomas Winkler499b1882008-10-14 12:32:48 -07001111
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001112 iwl_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
1113
Esti Kummerded2ae72008-08-04 16:00:45 +08001114#ifdef CONFIG_IWLWIFI_DEBUG
1115 switch (out_cmd->hdr.cmd) {
1116 case REPLY_TX_LINK_QUALITY_CMD:
1117 case SENSITIVITY_CMD:
1118 IWL_DEBUG_HC_DUMP("Sending command %s (#%x), seq: 0x%04X, "
1119 "%d bytes at %d[%d]:%d\n",
1120 get_cmd_string(out_cmd->hdr.cmd),
1121 out_cmd->hdr.cmd,
1122 le16_to_cpu(out_cmd->hdr.sequence), fix_size,
1123 q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
1124 break;
1125 default:
1126 IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
1127 "%d bytes at %d[%d]:%d\n",
1128 get_cmd_string(out_cmd->hdr.cmd),
1129 out_cmd->hdr.cmd,
1130 le16_to_cpu(out_cmd->hdr.sequence), fix_size,
1131 q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
1132 }
1133#endif
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001134 txq->need_update = 1;
1135
1136 /* Set up entry in queue's byte count circular buffer */
1137 priv->cfg->ops->lib->txq_update_byte_cnt_tbl(priv, txq, 0);
1138
1139 /* Increment and update queue's write index */
1140 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
1141 ret = iwl_txq_update_write_ptr(priv, txq);
1142
1143 spin_unlock_irqrestore(&priv->hcmd_lock, flags);
1144 return ret ? ret : idx;
1145}
1146
Tomas Winkler17b88922008-05-29 16:35:12 +08001147int iwl_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
1148{
1149 struct iwl_tx_queue *txq = &priv->txq[txq_id];
1150 struct iwl_queue *q = &txq->q;
1151 struct iwl_tx_info *tx_info;
1152 int nfreed = 0;
1153
1154 if ((index >= q->n_bd) || (iwl_queue_used(q, index) == 0)) {
1155 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
1156 "is out of range [0-%d] %d %d.\n", txq_id,
1157 index, q->n_bd, q->write_ptr, q->read_ptr);
1158 return 0;
1159 }
1160
Tomas Winkler499b1882008-10-14 12:32:48 -07001161 for (index = iwl_queue_inc_wrap(index, q->n_bd);
1162 q->read_ptr != index;
1163 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
Tomas Winkler17b88922008-05-29 16:35:12 +08001164
1165 tx_info = &txq->txb[txq->q.read_ptr];
1166 ieee80211_tx_status_irqsafe(priv->hw, tx_info->skb[0]);
1167 tx_info->skb[0] = NULL;
Tomas Winkler17b88922008-05-29 16:35:12 +08001168
Tomas Winkler972cf442008-05-29 16:35:13 +08001169 if (priv->cfg->ops->lib->txq_inval_byte_cnt_tbl)
1170 priv->cfg->ops->lib->txq_inval_byte_cnt_tbl(priv, txq);
1171
1172 iwl_hw_txq_free_tfd(priv, txq);
Tomas Winkler17b88922008-05-29 16:35:12 +08001173 nfreed++;
1174 }
1175 return nfreed;
1176}
1177EXPORT_SYMBOL(iwl_tx_queue_reclaim);
1178
1179
1180/**
1181 * iwl_hcmd_queue_reclaim - Reclaim TX command queue entries already Tx'd
1182 *
1183 * When FW advances 'R' index, all entries between old and new 'R' index
1184 * need to be reclaimed. As result, some free space forms. If there is
1185 * enough free space (> low mark), wake the stack that feeds us.
1186 */
Tomas Winkler499b1882008-10-14 12:32:48 -07001187static void iwl_hcmd_queue_reclaim(struct iwl_priv *priv, int txq_id,
1188 int idx, int cmd_idx)
Tomas Winkler17b88922008-05-29 16:35:12 +08001189{
1190 struct iwl_tx_queue *txq = &priv->txq[txq_id];
1191 struct iwl_queue *q = &txq->q;
1192 int nfreed = 0;
1193
Tomas Winkler499b1882008-10-14 12:32:48 -07001194 if ((idx >= q->n_bd) || (iwl_queue_used(q, idx) == 0)) {
Tomas Winkler17b88922008-05-29 16:35:12 +08001195 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
1196 "is out of range [0-%d] %d %d.\n", txq_id,
Tomas Winkler499b1882008-10-14 12:32:48 -07001197 idx, q->n_bd, q->write_ptr, q->read_ptr);
Tomas Winkler17b88922008-05-29 16:35:12 +08001198 return;
1199 }
1200
Tomas Winkler499b1882008-10-14 12:32:48 -07001201 pci_unmap_single(priv->pci_dev,
1202 pci_unmap_addr(&txq->cmd[cmd_idx]->meta, mapping),
1203 pci_unmap_len(&txq->cmd[cmd_idx]->meta, len),
1204 PCI_DMA_TODEVICE);
Tomas Winkler17b88922008-05-29 16:35:12 +08001205
Tomas Winkler499b1882008-10-14 12:32:48 -07001206 for (idx = iwl_queue_inc_wrap(idx, q->n_bd); q->read_ptr != idx;
1207 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
1208
1209 if (nfreed++ > 0) {
1210 IWL_ERROR("HCMD skipped: index (%d) %d %d\n", idx,
Tomas Winkler17b88922008-05-29 16:35:12 +08001211 q->write_ptr, q->read_ptr);
1212 queue_work(priv->workqueue, &priv->restart);
1213 }
Gregory Greenmanda99c4b2008-08-04 16:00:40 +08001214
Tomas Winkler17b88922008-05-29 16:35:12 +08001215 }
1216}
1217
1218/**
1219 * iwl_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
1220 * @rxb: Rx buffer to reclaim
1221 *
1222 * If an Rx buffer has an async callback associated with it the callback
1223 * will be executed. The attached skb (if present) will only be freed
1224 * if the callback returns 1
1225 */
1226void iwl_tx_cmd_complete(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
1227{
1228 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
1229 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
1230 int txq_id = SEQ_TO_QUEUE(sequence);
1231 int index = SEQ_TO_INDEX(sequence);
Tomas Winkler17b88922008-05-29 16:35:12 +08001232 int cmd_index;
Tomas Winkler9734cb22008-09-03 11:26:52 +08001233 bool huge = !!(pkt->hdr.sequence & SEQ_HUGE_FRAME);
Tomas Winkler17b88922008-05-29 16:35:12 +08001234 struct iwl_cmd *cmd;
1235
1236 /* If a Tx command is being handled and it isn't in the actual
1237 * command queue then there a command routing bug has been introduced
1238 * in the queue management code. */
Johannes Berg55d6a3c2008-09-23 19:18:43 +02001239 if (WARN(txq_id != IWL_CMD_QUEUE_NUM,
1240 "wrong command queue %d, command id 0x%X\n", txq_id, pkt->hdr.cmd))
1241 return;
Tomas Winkler17b88922008-05-29 16:35:12 +08001242
1243 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
Gregory Greenmanda99c4b2008-08-04 16:00:40 +08001244 cmd = priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
Tomas Winkler17b88922008-05-29 16:35:12 +08001245
1246 /* Input error checking is done when commands are added to queue. */
1247 if (cmd->meta.flags & CMD_WANT_SKB) {
1248 cmd->meta.source->u.skb = rxb->skb;
1249 rxb->skb = NULL;
1250 } else if (cmd->meta.u.callback &&
1251 !cmd->meta.u.callback(priv, cmd, rxb->skb))
1252 rxb->skb = NULL;
1253
Tomas Winkler499b1882008-10-14 12:32:48 -07001254 iwl_hcmd_queue_reclaim(priv, txq_id, index, cmd_index);
Tomas Winkler17b88922008-05-29 16:35:12 +08001255
1256 if (!(cmd->meta.flags & CMD_ASYNC)) {
1257 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
1258 wake_up_interruptible(&priv->wait_command_queue);
1259 }
1260}
1261EXPORT_SYMBOL(iwl_tx_cmd_complete);
1262
Tomas Winkler30e553e2008-05-29 16:35:16 +08001263/*
1264 * Find first available (lowest unused) Tx Queue, mark it "active".
1265 * Called only when finding queue for aggregation.
1266 * Should never return anything < 7, because they should already
1267 * be in use as EDCA AC (0-3), Command (4), HCCA (5, 6).
1268 */
1269static int iwl_txq_ctx_activate_free(struct iwl_priv *priv)
1270{
1271 int txq_id;
1272
1273 for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++)
1274 if (!test_and_set_bit(txq_id, &priv->txq_ctx_active_msk))
1275 return txq_id;
1276 return -1;
1277}
1278
1279int iwl_tx_agg_start(struct iwl_priv *priv, const u8 *ra, u16 tid, u16 *ssn)
1280{
1281 int sta_id;
1282 int tx_fifo;
1283 int txq_id;
1284 int ret;
1285 unsigned long flags;
1286 struct iwl_tid_data *tid_data;
Tomas Winkler30e553e2008-05-29 16:35:16 +08001287
1288 if (likely(tid < ARRAY_SIZE(default_tid_to_tx_fifo)))
1289 tx_fifo = default_tid_to_tx_fifo[tid];
1290 else
1291 return -EINVAL;
1292
Johannes Berge1749612008-10-27 15:59:26 -07001293 IWL_WARNING("%s on ra = %pM tid = %d\n",
1294 __func__, ra, tid);
Tomas Winkler30e553e2008-05-29 16:35:16 +08001295
1296 sta_id = iwl_find_station(priv, ra);
1297 if (sta_id == IWL_INVALID_STATION)
1298 return -ENXIO;
1299
1300 if (priv->stations[sta_id].tid[tid].agg.state != IWL_AGG_OFF) {
1301 IWL_ERROR("Start AGG when state is not IWL_AGG_OFF !\n");
1302 return -ENXIO;
1303 }
1304
1305 txq_id = iwl_txq_ctx_activate_free(priv);
1306 if (txq_id == -1)
1307 return -ENXIO;
1308
1309 spin_lock_irqsave(&priv->sta_lock, flags);
1310 tid_data = &priv->stations[sta_id].tid[tid];
1311 *ssn = SEQ_TO_SN(tid_data->seq_number);
1312 tid_data->agg.txq_id = txq_id;
1313 spin_unlock_irqrestore(&priv->sta_lock, flags);
1314
1315 ret = priv->cfg->ops->lib->txq_agg_enable(priv, txq_id, tx_fifo,
1316 sta_id, tid, *ssn);
1317 if (ret)
1318 return ret;
1319
1320 if (tid_data->tfds_in_queue == 0) {
1321 printk(KERN_ERR "HW queue is empty\n");
1322 tid_data->agg.state = IWL_AGG_ON;
1323 ieee80211_start_tx_ba_cb_irqsafe(priv->hw, ra, tid);
1324 } else {
1325 IWL_DEBUG_HT("HW queue is NOT empty: %d packets in HW queue\n",
1326 tid_data->tfds_in_queue);
1327 tid_data->agg.state = IWL_EMPTYING_HW_QUEUE_ADDBA;
1328 }
1329 return ret;
1330}
1331EXPORT_SYMBOL(iwl_tx_agg_start);
1332
1333int iwl_tx_agg_stop(struct iwl_priv *priv , const u8 *ra, u16 tid)
1334{
1335 int tx_fifo_id, txq_id, sta_id, ssn = -1;
1336 struct iwl_tid_data *tid_data;
1337 int ret, write_ptr, read_ptr;
1338 unsigned long flags;
Tomas Winkler30e553e2008-05-29 16:35:16 +08001339
1340 if (!ra) {
1341 IWL_ERROR("ra = NULL\n");
1342 return -EINVAL;
1343 }
1344
1345 if (likely(tid < ARRAY_SIZE(default_tid_to_tx_fifo)))
1346 tx_fifo_id = default_tid_to_tx_fifo[tid];
1347 else
1348 return -EINVAL;
1349
1350 sta_id = iwl_find_station(priv, ra);
1351
1352 if (sta_id == IWL_INVALID_STATION)
1353 return -ENXIO;
1354
1355 if (priv->stations[sta_id].tid[tid].agg.state != IWL_AGG_ON)
1356 IWL_WARNING("Stopping AGG while state not IWL_AGG_ON\n");
1357
1358 tid_data = &priv->stations[sta_id].tid[tid];
1359 ssn = (tid_data->seq_number & IEEE80211_SCTL_SEQ) >> 4;
1360 txq_id = tid_data->agg.txq_id;
1361 write_ptr = priv->txq[txq_id].q.write_ptr;
1362 read_ptr = priv->txq[txq_id].q.read_ptr;
1363
1364 /* The queue is not empty */
1365 if (write_ptr != read_ptr) {
1366 IWL_DEBUG_HT("Stopping a non empty AGG HW QUEUE\n");
1367 priv->stations[sta_id].tid[tid].agg.state =
1368 IWL_EMPTYING_HW_QUEUE_DELBA;
1369 return 0;
1370 }
1371
1372 IWL_DEBUG_HT("HW queue is empty\n");
1373 priv->stations[sta_id].tid[tid].agg.state = IWL_AGG_OFF;
1374
1375 spin_lock_irqsave(&priv->lock, flags);
1376 ret = priv->cfg->ops->lib->txq_agg_disable(priv, txq_id, ssn,
1377 tx_fifo_id);
1378 spin_unlock_irqrestore(&priv->lock, flags);
1379
1380 if (ret)
1381 return ret;
1382
1383 ieee80211_stop_tx_ba_cb_irqsafe(priv->hw, ra, tid);
1384
1385 return 0;
1386}
1387EXPORT_SYMBOL(iwl_tx_agg_stop);
1388
1389int iwl_txq_check_empty(struct iwl_priv *priv, int sta_id, u8 tid, int txq_id)
1390{
1391 struct iwl_queue *q = &priv->txq[txq_id].q;
1392 u8 *addr = priv->stations[sta_id].sta.sta.addr;
1393 struct iwl_tid_data *tid_data = &priv->stations[sta_id].tid[tid];
1394
1395 switch (priv->stations[sta_id].tid[tid].agg.state) {
1396 case IWL_EMPTYING_HW_QUEUE_DELBA:
1397 /* We are reclaiming the last packet of the */
1398 /* aggregated HW queue */
1399 if (txq_id == tid_data->agg.txq_id &&
1400 q->read_ptr == q->write_ptr) {
1401 u16 ssn = SEQ_TO_SN(tid_data->seq_number);
1402 int tx_fifo = default_tid_to_tx_fifo[tid];
1403 IWL_DEBUG_HT("HW queue empty: continue DELBA flow\n");
1404 priv->cfg->ops->lib->txq_agg_disable(priv, txq_id,
1405 ssn, tx_fifo);
1406 tid_data->agg.state = IWL_AGG_OFF;
1407 ieee80211_stop_tx_ba_cb_irqsafe(priv->hw, addr, tid);
1408 }
1409 break;
1410 case IWL_EMPTYING_HW_QUEUE_ADDBA:
1411 /* We are reclaiming the last packet of the queue */
1412 if (tid_data->tfds_in_queue == 0) {
1413 IWL_DEBUG_HT("HW queue empty: continue ADDBA flow\n");
1414 tid_data->agg.state = IWL_AGG_ON;
1415 ieee80211_start_tx_ba_cb_irqsafe(priv->hw, addr, tid);
1416 }
1417 break;
1418 }
1419 return 0;
1420}
1421EXPORT_SYMBOL(iwl_txq_check_empty);
Tomas Winkler30e553e2008-05-29 16:35:16 +08001422
Emmanuel Grumbach653fa4a2008-06-30 17:23:11 +08001423/**
1424 * iwl_tx_status_reply_compressed_ba - Update tx status from block-ack
1425 *
1426 * Go through block-ack's bitmap of ACK'd frames, update driver's record of
1427 * ACK vs. not. This gets sent to mac80211, then to rate scaling algo.
1428 */
1429static int iwl_tx_status_reply_compressed_ba(struct iwl_priv *priv,
1430 struct iwl_ht_agg *agg,
1431 struct iwl_compressed_ba_resp *ba_resp)
1432
1433{
1434 int i, sh, ack;
1435 u16 seq_ctl = le16_to_cpu(ba_resp->seq_ctl);
1436 u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
1437 u64 bitmap;
1438 int successes = 0;
1439 struct ieee80211_tx_info *info;
1440
1441 if (unlikely(!agg->wait_for_ba)) {
1442 IWL_ERROR("Received BA when not expected\n");
1443 return -EINVAL;
1444 }
1445
1446 /* Mark that the expected block-ack response arrived */
1447 agg->wait_for_ba = 0;
1448 IWL_DEBUG_TX_REPLY("BA %d %d\n", agg->start_idx, ba_resp->seq_ctl);
1449
1450 /* Calculate shift to align block-ack bits with our Tx window bits */
1451 sh = agg->start_idx - SEQ_TO_INDEX(seq_ctl>>4);
1452 if (sh < 0) /* tbw something is wrong with indices */
1453 sh += 0x100;
1454
1455 /* don't use 64-bit values for now */
1456 bitmap = le64_to_cpu(ba_resp->bitmap) >> sh;
1457
1458 if (agg->frame_count > (64 - sh)) {
1459 IWL_DEBUG_TX_REPLY("more frames than bitmap size");
1460 return -1;
1461 }
1462
1463 /* check for success or failure according to the
1464 * transmitted bitmap and block-ack bitmap */
1465 bitmap &= agg->bitmap;
1466
1467 /* For each frame attempted in aggregation,
1468 * update driver's record of tx frame's status. */
1469 for (i = 0; i < agg->frame_count ; i++) {
Emmanuel Grumbach4aa41f12008-07-18 13:53:09 +08001470 ack = bitmap & (1ULL << i);
Emmanuel Grumbach653fa4a2008-06-30 17:23:11 +08001471 successes += !!ack;
1472 IWL_DEBUG_TX_REPLY("%s ON i=%d idx=%d raw=%d\n",
1473 ack? "ACK":"NACK", i, (agg->start_idx + i) & 0xff,
1474 agg->start_idx + i);
1475 }
1476
1477 info = IEEE80211_SKB_CB(priv->txq[scd_flow].txb[agg->start_idx].skb[0]);
1478 memset(&info->status, 0, sizeof(info->status));
1479 info->flags = IEEE80211_TX_STAT_ACK;
1480 info->flags |= IEEE80211_TX_STAT_AMPDU;
1481 info->status.ampdu_ack_map = successes;
1482 info->status.ampdu_ack_len = agg->frame_count;
1483 iwl_hwrate_to_tx_control(priv, agg->rate_n_flags, info);
1484
1485 IWL_DEBUG_TX_REPLY("Bitmap %llx\n", (unsigned long long)bitmap);
1486
1487 return 0;
1488}
1489
1490/**
1491 * iwl_rx_reply_compressed_ba - Handler for REPLY_COMPRESSED_BA
1492 *
1493 * Handles block-acknowledge notification from device, which reports success
1494 * of frames sent via aggregation.
1495 */
1496void iwl_rx_reply_compressed_ba(struct iwl_priv *priv,
1497 struct iwl_rx_mem_buffer *rxb)
1498{
1499 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
1500 struct iwl_compressed_ba_resp *ba_resp = &pkt->u.compressed_ba;
1501 int index;
1502 struct iwl_tx_queue *txq = NULL;
1503 struct iwl_ht_agg *agg;
Emmanuel Grumbach653fa4a2008-06-30 17:23:11 +08001504
1505 /* "flow" corresponds to Tx queue */
1506 u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
1507
1508 /* "ssn" is start of block-ack Tx window, corresponds to index
1509 * (in Tx queue's circular buffer) of first TFD/frame in window */
1510 u16 ba_resp_scd_ssn = le16_to_cpu(ba_resp->scd_ssn);
1511
1512 if (scd_flow >= priv->hw_params.max_txq_num) {
Jiri Slaby6f147922008-08-11 23:49:41 +02001513 IWL_ERROR("BUG_ON scd_flow is bigger than number of queues\n");
Emmanuel Grumbach653fa4a2008-06-30 17:23:11 +08001514 return;
1515 }
1516
1517 txq = &priv->txq[scd_flow];
1518 agg = &priv->stations[ba_resp->sta_id].tid[ba_resp->tid].agg;
1519
1520 /* Find index just before block-ack window */
1521 index = iwl_queue_dec_wrap(ba_resp_scd_ssn & 0xff, txq->q.n_bd);
1522
1523 /* TODO: Need to get this copy more safely - now good for debug */
1524
Johannes Berge1749612008-10-27 15:59:26 -07001525 IWL_DEBUG_TX_REPLY("REPLY_COMPRESSED_BA [%d]Received from %pM, "
Emmanuel Grumbach653fa4a2008-06-30 17:23:11 +08001526 "sta_id = %d\n",
1527 agg->wait_for_ba,
Johannes Berge1749612008-10-27 15:59:26 -07001528 (u8 *) &ba_resp->sta_addr_lo32,
Emmanuel Grumbach653fa4a2008-06-30 17:23:11 +08001529 ba_resp->sta_id);
1530 IWL_DEBUG_TX_REPLY("TID = %d, SeqCtl = %d, bitmap = 0x%llx, scd_flow = "
1531 "%d, scd_ssn = %d\n",
1532 ba_resp->tid,
1533 ba_resp->seq_ctl,
1534 (unsigned long long)le64_to_cpu(ba_resp->bitmap),
1535 ba_resp->scd_flow,
1536 ba_resp->scd_ssn);
1537 IWL_DEBUG_TX_REPLY("DAT start_idx = %d, bitmap = 0x%llx \n",
1538 agg->start_idx,
1539 (unsigned long long)agg->bitmap);
1540
1541 /* Update driver's record of ACK vs. not for each frame in window */
1542 iwl_tx_status_reply_compressed_ba(priv, agg, ba_resp);
1543
1544 /* Release all TFDs before the SSN, i.e. all TFDs in front of
1545 * block-ack window (we assume that they've been successfully
1546 * transmitted ... if not, it's too late anyway). */
1547 if (txq->q.read_ptr != (ba_resp_scd_ssn & 0xff)) {
1548 /* calculate mac80211 ampdu sw queue to wake */
1549 int ampdu_q =
1550 scd_flow - priv->hw_params.first_ampdu_q + priv->hw->queues;
1551 int freed = iwl_tx_queue_reclaim(priv, scd_flow, index);
1552 priv->stations[ba_resp->sta_id].
1553 tid[ba_resp->tid].tfds_in_queue -= freed;
1554 if (iwl_queue_space(&txq->q) > txq->q.low_mark &&
1555 priv->mac80211_registered &&
1556 agg->state != IWL_EMPTYING_HW_QUEUE_DELBA)
1557 ieee80211_wake_queue(priv->hw, ampdu_q);
1558
1559 iwl_txq_check_empty(priv, ba_resp->sta_id,
1560 ba_resp->tid, scd_flow);
1561 }
1562}
1563EXPORT_SYMBOL(iwl_rx_reply_compressed_ba);
1564
Helmut Schaa994d31f2008-07-02 12:17:06 +02001565#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winklera332f8d62008-05-29 16:35:08 +08001566#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
1567
1568const char *iwl_get_tx_fail_reason(u32 status)
1569{
1570 switch (status & TX_STATUS_MSK) {
1571 case TX_STATUS_SUCCESS:
1572 return "SUCCESS";
1573 TX_STATUS_ENTRY(SHORT_LIMIT);
1574 TX_STATUS_ENTRY(LONG_LIMIT);
1575 TX_STATUS_ENTRY(FIFO_UNDERRUN);
1576 TX_STATUS_ENTRY(MGMNT_ABORT);
1577 TX_STATUS_ENTRY(NEXT_FRAG);
1578 TX_STATUS_ENTRY(LIFE_EXPIRE);
1579 TX_STATUS_ENTRY(DEST_PS);
1580 TX_STATUS_ENTRY(ABORTED);
1581 TX_STATUS_ENTRY(BT_RETRY);
1582 TX_STATUS_ENTRY(STA_INVALID);
1583 TX_STATUS_ENTRY(FRAG_DROPPED);
1584 TX_STATUS_ENTRY(TID_DISABLE);
1585 TX_STATUS_ENTRY(FRAME_FLUSHED);
1586 TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
1587 TX_STATUS_ENTRY(TX_LOCKED);
1588 TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
1589 }
1590
1591 return "UNKNOWN";
1592}
1593EXPORT_SYMBOL(iwl_get_tx_fail_reason);
1594#endif /* CONFIG_IWLWIFI_DEBUG */