blob: 91f9c89b1b6dbd85ef527e4e689af517917d2d85 [file] [log] [blame]
Ron Rindjunsky1053d352008-05-05 10:22:43 +08001/******************************************************************************
2 *
Reinette Chatre1f447802010-01-15 13:43:41 -08003 * Copyright(c) 2003 - 2010 Intel Corporation. All rights reserved.
Ron Rindjunsky1053d352008-05-05 10:22:43 +08004 *
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:
Winkler, Tomas759ef892008-12-09 11:28:58 -080025 * Intel Linux Wireless <ilw@linux.intel.com>
Ron Rindjunsky1053d352008-05-05 10:22:43 +080026 * 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>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040031#include <linux/sched.h>
Ron Rindjunsky1053d352008-05-05 10:22:43 +080032#include <net/mac80211.h>
33#include "iwl-eeprom.h"
34#include "iwl-dev.h"
35#include "iwl-core.h"
36#include "iwl-sta.h"
37#include "iwl-io.h"
38#include "iwl-helpers.h"
39
Tomas Winklerfd4abac2008-05-15 13:54:07 +080040/**
41 * iwl_txq_update_write_ptr - Send new write index to hardware
42 */
Abhijeet Kolekar7bfedc52010-02-03 13:47:56 -080043void iwl_txq_update_write_ptr(struct iwl_priv *priv, struct iwl_tx_queue *txq)
Tomas Winklerfd4abac2008-05-15 13:54:07 +080044{
45 u32 reg = 0;
Tomas Winklerfd4abac2008-05-15 13:54:07 +080046 int txq_id = txq->q.id;
47
48 if (txq->need_update == 0)
Abhijeet Kolekar7bfedc52010-02-03 13:47:56 -080049 return;
Tomas Winklerfd4abac2008-05-15 13:54:07 +080050
51 /* if we're trying to save power */
52 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
53 /* wake up nic if it's powered down ...
54 * uCode will wake up, and interrupt us again, so next
55 * time we'll skip this part. */
56 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
57
58 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
Ben Cahill309e7312009-11-06 14:53:03 -080059 IWL_DEBUG_INFO(priv, "Tx queue %d requesting wakeup, GP1 = 0x%x\n",
60 txq_id, reg);
Tomas Winklerfd4abac2008-05-15 13:54:07 +080061 iwl_set_bit(priv, CSR_GP_CNTRL,
62 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Abhijeet Kolekar7bfedc52010-02-03 13:47:56 -080063 return;
Tomas Winklerfd4abac2008-05-15 13:54:07 +080064 }
65
Tomas Winklerfd4abac2008-05-15 13:54:07 +080066 iwl_write_direct32(priv, HBUS_TARG_WRPTR,
67 txq->q.write_ptr | (txq_id << 8));
Tomas Winklerfd4abac2008-05-15 13:54:07 +080068
69 /* else not in power-save mode, uCode will never sleep when we're
70 * trying to tx (during RFKILL, we're not trying to tx). */
71 } else
72 iwl_write32(priv, HBUS_TARG_WRPTR,
73 txq->q.write_ptr | (txq_id << 8));
74
75 txq->need_update = 0;
Tomas Winklerfd4abac2008-05-15 13:54:07 +080076}
77EXPORT_SYMBOL(iwl_txq_update_write_ptr);
78
79
Wey-Yi Guya239a8b2010-02-19 15:47:32 -080080void iwl_free_tfds_in_queue(struct iwl_priv *priv,
81 int sta_id, int tid, int freed)
82{
83 if (priv->stations[sta_id].tid[tid].tfds_in_queue >= freed)
84 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
85 else {
86 IWL_ERR(priv, "free more than tfds_in_queue (%u:%d)\n",
87 priv->stations[sta_id].tid[tid].tfds_in_queue,
88 freed);
89 priv->stations[sta_id].tid[tid].tfds_in_queue = 0;
90 }
91}
92EXPORT_SYMBOL(iwl_free_tfds_in_queue);
93
Ron Rindjunsky1053d352008-05-05 10:22:43 +080094/**
95 * iwl_tx_queue_free - Deallocate DMA queue.
96 * @txq: Transmit queue to deallocate.
97 *
98 * Empty queue by removing and destroying all BD's.
99 * Free all buffers.
100 * 0-fill, but do not free "txq" descriptor structure.
101 */
Samuel Ortiza8e74e272009-01-23 13:45:14 -0800102void iwl_tx_queue_free(struct iwl_priv *priv, int txq_id)
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800103{
Gregory Greenmanda99c4b2008-08-04 16:00:40 +0800104 struct iwl_tx_queue *txq = &priv->txq[txq_id];
Tomas Winkler443cfd42008-05-15 13:53:57 +0800105 struct iwl_queue *q = &txq->q;
Stanislaw Gruszkaf36d04a2010-02-10 05:07:45 -0800106 struct device *dev = &priv->pci_dev->dev;
Wey-Yi Guy71c55d92009-10-23 13:42:31 -0700107 int i;
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800108
109 if (q->n_bd == 0)
110 return;
111
112 /* first, empty all BD's */
113 for (; q->write_ptr != q->read_ptr;
114 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd))
Samuel Ortiz7aaa1d72009-01-19 15:30:26 -0800115 priv->cfg->ops->lib->txq_free_tfd(priv, txq);
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800116
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800117 /* De-alloc array of command/tx buffers */
Tomas Winkler961ba602008-10-14 12:32:44 -0700118 for (i = 0; i < TFD_TX_CMD_SLOTS; i++)
Gregory Greenmanda99c4b2008-08-04 16:00:40 +0800119 kfree(txq->cmd[i]);
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800120
121 /* De-alloc circular buffer of TFDs */
122 if (txq->q.n_bd)
Stanislaw Gruszkaf36d04a2010-02-10 05:07:45 -0800123 dma_free_coherent(dev, priv->hw_params.tfd_size *
124 txq->q.n_bd, txq->tfds, txq->q.dma_addr);
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800125
126 /* De-alloc array of per-TFD driver data */
127 kfree(txq->txb);
128 txq->txb = NULL;
129
Johannes Bergc2acea82009-07-24 11:13:05 -0700130 /* deallocate arrays */
131 kfree(txq->cmd);
132 kfree(txq->meta);
133 txq->cmd = NULL;
134 txq->meta = NULL;
135
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800136 /* 0-fill queue descriptor structure */
137 memset(txq, 0, sizeof(*txq));
138}
Samuel Ortiza8e74e272009-01-23 13:45:14 -0800139EXPORT_SYMBOL(iwl_tx_queue_free);
Tomas Winkler961ba602008-10-14 12:32:44 -0700140
141/**
142 * iwl_cmd_queue_free - Deallocate DMA queue.
143 * @txq: Transmit queue to deallocate.
144 *
145 * Empty queue by removing and destroying all BD's.
146 * Free all buffers.
147 * 0-fill, but do not free "txq" descriptor structure.
148 */
Abhijeet Kolekar3e5d2382009-03-17 21:51:49 -0700149void iwl_cmd_queue_free(struct iwl_priv *priv)
Tomas Winkler961ba602008-10-14 12:32:44 -0700150{
151 struct iwl_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
152 struct iwl_queue *q = &txq->q;
Stanislaw Gruszkaf36d04a2010-02-10 05:07:45 -0800153 struct device *dev = &priv->pci_dev->dev;
Wey-Yi Guy71c55d92009-10-23 13:42:31 -0700154 int i;
Tomas Winkler961ba602008-10-14 12:32:44 -0700155
156 if (q->n_bd == 0)
157 return;
158
Tomas Winkler961ba602008-10-14 12:32:44 -0700159 /* De-alloc array of command/tx buffers */
160 for (i = 0; i <= TFD_CMD_SLOTS; i++)
161 kfree(txq->cmd[i]);
162
163 /* De-alloc circular buffer of TFDs */
164 if (txq->q.n_bd)
Stanislaw Gruszkaf36d04a2010-02-10 05:07:45 -0800165 dma_free_coherent(dev, priv->hw_params.tfd_size * txq->q.n_bd,
166 txq->tfds, txq->q.dma_addr);
Tomas Winkler961ba602008-10-14 12:32:44 -0700167
Reinette Chatre28142982009-09-25 14:24:22 -0700168 /* deallocate arrays */
169 kfree(txq->cmd);
170 kfree(txq->meta);
171 txq->cmd = NULL;
172 txq->meta = NULL;
173
Tomas Winkler961ba602008-10-14 12:32:44 -0700174 /* 0-fill queue descriptor structure */
175 memset(txq, 0, sizeof(*txq));
176}
Abhijeet Kolekar3e5d2382009-03-17 21:51:49 -0700177EXPORT_SYMBOL(iwl_cmd_queue_free);
178
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800179/*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
180 * DMA services
181 *
182 * Theory of operation
183 *
184 * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
185 * of buffer descriptors, each of which points to one or more data buffers for
186 * the device to read from or fill. Driver and device exchange status of each
187 * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
188 * entries in each circular buffer, to protect against confusing empty and full
189 * queue states.
190 *
191 * The device reads or writes the data in the queues via the device's several
192 * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
193 *
194 * For Tx queue, there are low mark and high mark limits. If, after queuing
195 * the packet for Tx, free space become < low mark, Tx queue stopped. When
196 * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
197 * Tx queue resumed.
198 *
199 * See more detailed info in iwl-4965-hw.h.
200 ***************************************************/
201
202int iwl_queue_space(const struct iwl_queue *q)
203{
204 int s = q->read_ptr - q->write_ptr;
205
206 if (q->read_ptr > q->write_ptr)
207 s -= q->n_bd;
208
209 if (s <= 0)
210 s += q->n_window;
211 /* keep some reserve to not confuse empty and full situations */
212 s -= 2;
213 if (s < 0)
214 s = 0;
215 return s;
216}
217EXPORT_SYMBOL(iwl_queue_space);
218
219
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800220/**
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800221 * iwl_queue_init - Initialize queue's high/low-water and read/write indexes
222 */
Tomas Winkler443cfd42008-05-15 13:53:57 +0800223static int iwl_queue_init(struct iwl_priv *priv, struct iwl_queue *q,
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800224 int count, int slots_num, u32 id)
225{
226 q->n_bd = count;
227 q->n_window = slots_num;
228 q->id = id;
229
230 /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
231 * and iwl_queue_dec_wrap are broken. */
232 BUG_ON(!is_power_of_2(count));
233
234 /* slots_num must be power-of-two size, otherwise
235 * get_cmd_index is broken. */
236 BUG_ON(!is_power_of_2(slots_num));
237
238 q->low_mark = q->n_window / 4;
239 if (q->low_mark < 4)
240 q->low_mark = 4;
241
242 q->high_mark = q->n_window / 8;
243 if (q->high_mark < 2)
244 q->high_mark = 2;
245
246 q->write_ptr = q->read_ptr = 0;
Wey-Yi Guyb74e31a2010-03-01 17:23:50 -0800247 q->last_read_ptr = 0;
248 q->repeat_same_read_ptr = 0;
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800249
250 return 0;
251}
252
253/**
254 * iwl_tx_queue_alloc - Alloc driver data and TFD CB for one Tx/cmd queue
255 */
256static int iwl_tx_queue_alloc(struct iwl_priv *priv,
Ron Rindjunsky16466902008-05-05 10:22:50 +0800257 struct iwl_tx_queue *txq, u32 id)
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800258{
Stanislaw Gruszkaf36d04a2010-02-10 05:07:45 -0800259 struct device *dev = &priv->pci_dev->dev;
Winkler, Tomas3978e5b2009-01-23 13:45:23 -0800260 size_t tfd_sz = priv->hw_params.tfd_size * TFD_QUEUE_SIZE_MAX;
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800261
262 /* Driver private data, only for Tx (not command) queues,
263 * not shared with device. */
264 if (id != IWL_CMD_QUEUE_NUM) {
265 txq->txb = kmalloc(sizeof(txq->txb[0]) *
266 TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
267 if (!txq->txb) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800268 IWL_ERR(priv, "kmalloc for auxiliary BD "
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800269 "structures failed\n");
270 goto error;
271 }
Winkler, Tomas3978e5b2009-01-23 13:45:23 -0800272 } else {
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800273 txq->txb = NULL;
Winkler, Tomas3978e5b2009-01-23 13:45:23 -0800274 }
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800275
276 /* Circular buffer of transmit frame descriptors (TFDs),
277 * shared with device */
Stanislaw Gruszkaf36d04a2010-02-10 05:07:45 -0800278 txq->tfds = dma_alloc_coherent(dev, tfd_sz, &txq->q.dma_addr,
279 GFP_KERNEL);
Tomas Winkler499b1882008-10-14 12:32:48 -0700280 if (!txq->tfds) {
Winkler, Tomas3978e5b2009-01-23 13:45:23 -0800281 IWL_ERR(priv, "pci_alloc_consistent(%zd) failed\n", tfd_sz);
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800282 goto error;
283 }
284 txq->q.id = id;
285
286 return 0;
287
288 error:
289 kfree(txq->txb);
290 txq->txb = NULL;
291
292 return -ENOMEM;
293}
294
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800295/**
296 * iwl_tx_queue_init - Allocate and initialize one tx/cmd queue
297 */
Samuel Ortiza8e74e272009-01-23 13:45:14 -0800298int iwl_tx_queue_init(struct iwl_priv *priv, struct iwl_tx_queue *txq,
299 int slots_num, u32 txq_id)
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800300{
Gregory Greenmanda99c4b2008-08-04 16:00:40 +0800301 int i, len;
Tomas Winkler73b7d742008-09-03 11:18:48 +0800302 int ret;
Johannes Bergc2acea82009-07-24 11:13:05 -0700303 int actual_slots = slots_num;
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800304
305 /*
306 * Alloc buffer array for commands (Tx or other types of commands).
307 * For the command queue (#4), allocate command space + one big
308 * command for scan, since scan command is very huge; the system will
309 * not have two scans at the same time, so only one is needed.
310 * For normal Tx queues (all other queues), no super-size command
311 * space is needed.
312 */
Johannes Bergc2acea82009-07-24 11:13:05 -0700313 if (txq_id == IWL_CMD_QUEUE_NUM)
314 actual_slots++;
315
316 txq->meta = kzalloc(sizeof(struct iwl_cmd_meta) * actual_slots,
317 GFP_KERNEL);
318 txq->cmd = kzalloc(sizeof(struct iwl_device_cmd *) * actual_slots,
319 GFP_KERNEL);
320
321 if (!txq->meta || !txq->cmd)
322 goto out_free_arrays;
323
324 len = sizeof(struct iwl_device_cmd);
325 for (i = 0; i < actual_slots; i++) {
326 /* only happens for cmd queue */
327 if (i == slots_num)
Abhijeet Kolekar89612122010-02-19 11:49:49 -0800328 len = IWL_MAX_CMD_SIZE;
Gregory Greenmanda99c4b2008-08-04 16:00:40 +0800329
John W. Linville49898852008-09-02 15:07:18 -0400330 txq->cmd[i] = kmalloc(len, GFP_KERNEL);
Gregory Greenmanda99c4b2008-08-04 16:00:40 +0800331 if (!txq->cmd[i])
Tomas Winkler73b7d742008-09-03 11:18:48 +0800332 goto err;
Gregory Greenmanda99c4b2008-08-04 16:00:40 +0800333 }
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800334
335 /* Alloc driver data array and TFD circular buffer */
Tomas Winkler73b7d742008-09-03 11:18:48 +0800336 ret = iwl_tx_queue_alloc(priv, txq, txq_id);
337 if (ret)
338 goto err;
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800339
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800340 txq->need_update = 0;
341
Johannes Berg1a716552009-11-06 14:52:51 -0800342 /*
343 * Aggregation TX queues will get their ID when aggregation begins;
344 * they overwrite the setting done here. The command FIFO doesn't
345 * need an swq_id so don't set one to catch errors, all others can
346 * be set up to the identity mapping.
347 */
348 if (txq_id != IWL_CMD_QUEUE_NUM)
Johannes Berg45af8192009-06-19 13:52:43 -0700349 txq->swq_id = txq_id;
350
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800351 /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
352 * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
353 BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
354
355 /* Initialize queue's high/low-water marks, and head/tail indexes */
356 iwl_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
357
358 /* Tell device where to find queue */
Samuel Ortiza8e74e272009-01-23 13:45:14 -0800359 priv->cfg->ops->lib->txq_init(priv, txq);
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800360
361 return 0;
Tomas Winkler73b7d742008-09-03 11:18:48 +0800362err:
Johannes Bergc2acea82009-07-24 11:13:05 -0700363 for (i = 0; i < actual_slots; i++)
Tomas Winkler73b7d742008-09-03 11:18:48 +0800364 kfree(txq->cmd[i]);
Johannes Bergc2acea82009-07-24 11:13:05 -0700365out_free_arrays:
366 kfree(txq->meta);
367 kfree(txq->cmd);
Tomas Winkler73b7d742008-09-03 11:18:48 +0800368
Tomas Winkler73b7d742008-09-03 11:18:48 +0800369 return -ENOMEM;
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800370}
Samuel Ortiza8e74e272009-01-23 13:45:14 -0800371EXPORT_SYMBOL(iwl_tx_queue_init);
372
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800373/*************** HOST COMMAND QUEUE FUNCTIONS *****/
374
375/**
376 * iwl_enqueue_hcmd - enqueue a uCode command
377 * @priv: device private data point
378 * @cmd: a point to the ucode command structure
379 *
380 * The function returns < 0 values to indicate the operation is
381 * failed. On success, it turns the index (> 0) of command in the
382 * command queue.
383 */
384int iwl_enqueue_hcmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
385{
386 struct iwl_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
387 struct iwl_queue *q = &txq->q;
Johannes Bergc2acea82009-07-24 11:13:05 -0700388 struct iwl_device_cmd *out_cmd;
389 struct iwl_cmd_meta *out_meta;
Tomas Winklerf3674222008-08-04 16:00:44 +0800390 dma_addr_t phys_addr;
391 unsigned long flags;
Abhijeet Kolekar7bfedc52010-02-03 13:47:56 -0800392 int len;
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800393 u32 idx;
394 u16 fix_size;
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800395
396 cmd->len = priv->cfg->ops->utils->get_hcmd_size(cmd->id, cmd->len);
397 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
398
399 /* If any of the command structures end up being larger than
400 * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
Abhijeet Kolekar89612122010-02-19 11:49:49 -0800401 * we will need to increase the size of the TFD entries
402 * Also, check to see if command buffer should not exceed the size
403 * of device_cmd and max_cmd_size. */
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800404 BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
Johannes Bergc2acea82009-07-24 11:13:05 -0700405 !(cmd->flags & CMD_SIZE_HUGE));
Abhijeet Kolekar89612122010-02-19 11:49:49 -0800406 BUG_ON(fix_size > IWL_MAX_CMD_SIZE);
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800407
Wey-Yi Guy7812b162009-10-02 13:43:58 -0700408 if (iwl_is_rfkill(priv) || iwl_is_ctkill(priv)) {
Reinette Chatref2f21b42009-10-30 14:36:15 -0700409 IWL_WARN(priv, "Not sending command - %s KILL\n",
410 iwl_is_rfkill(priv) ? "RF" : "CT");
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800411 return -EIO;
412 }
413
Johannes Bergc2acea82009-07-24 11:13:05 -0700414 if (iwl_queue_space(q) < ((cmd->flags & CMD_ASYNC) ? 2 : 1)) {
Wey-Yi Guy2d237f72009-11-20 12:05:08 -0800415 IWL_ERR(priv, "No space in command queue\n");
Wey-Yi Guy7812b162009-10-02 13:43:58 -0700416 if (iwl_within_ct_kill_margin(priv))
417 iwl_tt_enter_ct_kill(priv);
418 else {
419 IWL_ERR(priv, "Restarting adapter due to queue full\n");
420 queue_work(priv->workqueue, &priv->restart);
421 }
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800422 return -ENOSPC;
423 }
424
425 spin_lock_irqsave(&priv->hcmd_lock, flags);
426
Johannes Bergc2acea82009-07-24 11:13:05 -0700427 idx = get_cmd_index(q, q->write_ptr, cmd->flags & CMD_SIZE_HUGE);
Gregory Greenmanda99c4b2008-08-04 16:00:40 +0800428 out_cmd = txq->cmd[idx];
Johannes Bergc2acea82009-07-24 11:13:05 -0700429 out_meta = &txq->meta[idx];
430
Daniel C Halperin8ce73f32009-07-31 14:28:06 -0700431 memset(out_meta, 0, sizeof(*out_meta)); /* re-initialize to NULL */
Johannes Bergc2acea82009-07-24 11:13:05 -0700432 out_meta->flags = cmd->flags;
433 if (cmd->flags & CMD_WANT_SKB)
434 out_meta->source = cmd;
435 if (cmd->flags & CMD_ASYNC)
436 out_meta->callback = cmd->callback;
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800437
438 out_cmd->hdr.cmd = cmd->id;
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800439 memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
440
441 /* At this point, the out_cmd now has all of the incoming cmd
442 * information */
443
444 out_cmd->hdr.flags = 0;
445 out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
446 INDEX_TO_SEQ(q->write_ptr));
Johannes Bergc2acea82009-07-24 11:13:05 -0700447 if (cmd->flags & CMD_SIZE_HUGE)
Tomas Winkler9734cb22008-09-03 11:26:52 +0800448 out_cmd->hdr.sequence |= SEQ_HUGE_FRAME;
Johannes Bergc2acea82009-07-24 11:13:05 -0700449 len = sizeof(struct iwl_device_cmd);
Abhijeet Kolekar89612122010-02-19 11:49:49 -0800450 if (idx == TFD_CMD_SLOTS)
451 len = IWL_MAX_CMD_SIZE;
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800452
Esti Kummerded2ae72008-08-04 16:00:45 +0800453#ifdef CONFIG_IWLWIFI_DEBUG
454 switch (out_cmd->hdr.cmd) {
455 case REPLY_TX_LINK_QUALITY_CMD:
456 case SENSITIVITY_CMD:
Tomas Winklere1623442009-01-27 14:27:56 -0800457 IWL_DEBUG_HC_DUMP(priv, "Sending command %s (#%x), seq: 0x%04X, "
Esti Kummerded2ae72008-08-04 16:00:45 +0800458 "%d bytes at %d[%d]:%d\n",
459 get_cmd_string(out_cmd->hdr.cmd),
460 out_cmd->hdr.cmd,
461 le16_to_cpu(out_cmd->hdr.sequence), fix_size,
462 q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
463 break;
464 default:
Tomas Winklere1623442009-01-27 14:27:56 -0800465 IWL_DEBUG_HC(priv, "Sending command %s (#%x), seq: 0x%04X, "
Esti Kummerded2ae72008-08-04 16:00:45 +0800466 "%d bytes at %d[%d]:%d\n",
467 get_cmd_string(out_cmd->hdr.cmd),
468 out_cmd->hdr.cmd,
469 le16_to_cpu(out_cmd->hdr.sequence), fix_size,
470 q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
471 }
472#endif
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800473 txq->need_update = 1;
474
Samuel Ortiz518099a2009-01-19 15:30:27 -0800475 if (priv->cfg->ops->lib->txq_update_byte_cnt_tbl)
476 /* Set up entry in queue's byte count circular buffer */
477 priv->cfg->ops->lib->txq_update_byte_cnt_tbl(priv, txq, 0);
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800478
Reinette Chatredf833b12009-04-21 10:55:48 -0700479 phys_addr = pci_map_single(priv->pci_dev, &out_cmd->hdr,
480 fix_size, PCI_DMA_BIDIRECTIONAL);
Johannes Bergc2acea82009-07-24 11:13:05 -0700481 pci_unmap_addr_set(out_meta, mapping, phys_addr);
482 pci_unmap_len_set(out_meta, len, fix_size);
Reinette Chatredf833b12009-04-21 10:55:48 -0700483
Johannes Bergbe1a71a2009-10-02 13:44:02 -0700484 trace_iwlwifi_dev_hcmd(priv, &out_cmd->hdr, fix_size, cmd->flags);
485
Reinette Chatredf833b12009-04-21 10:55:48 -0700486 priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
487 phys_addr, fix_size, 1,
488 U32_PAD(cmd->len));
489
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800490 /* Increment and update queue's write index */
491 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Abhijeet Kolekar7bfedc52010-02-03 13:47:56 -0800492 iwl_txq_update_write_ptr(priv, txq);
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800493
494 spin_unlock_irqrestore(&priv->hcmd_lock, flags);
Abhijeet Kolekar7bfedc52010-02-03 13:47:56 -0800495 return idx;
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800496}
497
Tomas Winkler17b88922008-05-29 16:35:12 +0800498/**
499 * iwl_hcmd_queue_reclaim - Reclaim TX command queue entries already Tx'd
500 *
501 * When FW advances 'R' index, all entries between old and new 'R' index
502 * need to be reclaimed. As result, some free space forms. If there is
503 * enough free space (> low mark), wake the stack that feeds us.
504 */
Tomas Winkler499b1882008-10-14 12:32:48 -0700505static void iwl_hcmd_queue_reclaim(struct iwl_priv *priv, int txq_id,
506 int idx, int cmd_idx)
Tomas Winkler17b88922008-05-29 16:35:12 +0800507{
508 struct iwl_tx_queue *txq = &priv->txq[txq_id];
509 struct iwl_queue *q = &txq->q;
510 int nfreed = 0;
511
Tomas Winkler499b1882008-10-14 12:32:48 -0700512 if ((idx >= q->n_bd) || (iwl_queue_used(q, idx) == 0)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800513 IWL_ERR(priv, "Read index for DMA queue txq id (%d), index %d, "
Tomas Winkler17b88922008-05-29 16:35:12 +0800514 "is out of range [0-%d] %d %d.\n", txq_id,
Tomas Winkler499b1882008-10-14 12:32:48 -0700515 idx, q->n_bd, q->write_ptr, q->read_ptr);
Tomas Winkler17b88922008-05-29 16:35:12 +0800516 return;
517 }
518
Tomas Winkler499b1882008-10-14 12:32:48 -0700519 for (idx = iwl_queue_inc_wrap(idx, q->n_bd); q->read_ptr != idx;
520 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
521
522 if (nfreed++ > 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800523 IWL_ERR(priv, "HCMD skipped: index (%d) %d %d\n", idx,
Tomas Winkler17b88922008-05-29 16:35:12 +0800524 q->write_ptr, q->read_ptr);
525 queue_work(priv->workqueue, &priv->restart);
526 }
Gregory Greenmanda99c4b2008-08-04 16:00:40 +0800527
Tomas Winkler17b88922008-05-29 16:35:12 +0800528 }
529}
530
531/**
532 * iwl_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
533 * @rxb: Rx buffer to reclaim
534 *
535 * If an Rx buffer has an async callback associated with it the callback
536 * will be executed. The attached skb (if present) will only be freed
537 * if the callback returns 1
538 */
539void iwl_tx_cmd_complete(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
540{
Zhu Yi2f301222009-10-09 17:19:45 +0800541 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Tomas Winkler17b88922008-05-29 16:35:12 +0800542 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
543 int txq_id = SEQ_TO_QUEUE(sequence);
544 int index = SEQ_TO_INDEX(sequence);
Tomas Winkler17b88922008-05-29 16:35:12 +0800545 int cmd_index;
Tomas Winkler9734cb22008-09-03 11:26:52 +0800546 bool huge = !!(pkt->hdr.sequence & SEQ_HUGE_FRAME);
Johannes Bergc2acea82009-07-24 11:13:05 -0700547 struct iwl_device_cmd *cmd;
548 struct iwl_cmd_meta *meta;
Tomas Winkler17b88922008-05-29 16:35:12 +0800549
550 /* If a Tx command is being handled and it isn't in the actual
551 * command queue then there a command routing bug has been introduced
552 * in the queue management code. */
Johannes Berg55d6a3c2008-09-23 19:18:43 +0200553 if (WARN(txq_id != IWL_CMD_QUEUE_NUM,
Winkler, Tomas01ef93232008-11-07 09:58:45 -0800554 "wrong command queue %d, sequence 0x%X readp=%d writep=%d\n",
555 txq_id, sequence,
556 priv->txq[IWL_CMD_QUEUE_NUM].q.read_ptr,
557 priv->txq[IWL_CMD_QUEUE_NUM].q.write_ptr)) {
Reinette Chatreec741162009-07-24 11:13:08 -0700558 iwl_print_hex_error(priv, pkt, 32);
Johannes Berg55d6a3c2008-09-23 19:18:43 +0200559 return;
Winkler, Tomas01ef93232008-11-07 09:58:45 -0800560 }
Tomas Winkler17b88922008-05-29 16:35:12 +0800561
562 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
Gregory Greenmanda99c4b2008-08-04 16:00:40 +0800563 cmd = priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
Johannes Bergc2acea82009-07-24 11:13:05 -0700564 meta = &priv->txq[IWL_CMD_QUEUE_NUM].meta[cmd_index];
Tomas Winkler17b88922008-05-29 16:35:12 +0800565
Reinette Chatrec33de622009-10-30 14:36:10 -0700566 pci_unmap_single(priv->pci_dev,
567 pci_unmap_addr(meta, mapping),
568 pci_unmap_len(meta, len),
569 PCI_DMA_BIDIRECTIONAL);
570
Tomas Winkler17b88922008-05-29 16:35:12 +0800571 /* Input error checking is done when commands are added to queue. */
Johannes Bergc2acea82009-07-24 11:13:05 -0700572 if (meta->flags & CMD_WANT_SKB) {
Zhu Yi2f301222009-10-09 17:19:45 +0800573 meta->source->reply_page = (unsigned long)rxb_addr(rxb);
574 rxb->page = NULL;
Johannes Berg5696aea2009-07-24 11:13:06 -0700575 } else if (meta->callback)
Zhu Yi2f301222009-10-09 17:19:45 +0800576 meta->callback(priv, cmd, pkt);
Tomas Winkler17b88922008-05-29 16:35:12 +0800577
Tomas Winkler499b1882008-10-14 12:32:48 -0700578 iwl_hcmd_queue_reclaim(priv, txq_id, index, cmd_index);
Tomas Winkler17b88922008-05-29 16:35:12 +0800579
Johannes Bergc2acea82009-07-24 11:13:05 -0700580 if (!(meta->flags & CMD_ASYNC)) {
Tomas Winkler17b88922008-05-29 16:35:12 +0800581 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
Reinette Chatred2dfe6d2010-02-18 22:03:04 -0800582 IWL_DEBUG_INFO(priv, "Clearing HCMD_ACTIVE for command %s \n",
583 get_cmd_string(cmd->hdr.cmd));
Tomas Winkler17b88922008-05-29 16:35:12 +0800584 wake_up_interruptible(&priv->wait_command_queue);
585 }
586}
587EXPORT_SYMBOL(iwl_tx_cmd_complete);
588
Helmut Schaa994d31f2008-07-02 12:17:06 +0200589#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winklera332f8d62008-05-29 16:35:08 +0800590#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
591
592const char *iwl_get_tx_fail_reason(u32 status)
593{
594 switch (status & TX_STATUS_MSK) {
595 case TX_STATUS_SUCCESS:
596 return "SUCCESS";
597 TX_STATUS_ENTRY(SHORT_LIMIT);
598 TX_STATUS_ENTRY(LONG_LIMIT);
599 TX_STATUS_ENTRY(FIFO_UNDERRUN);
600 TX_STATUS_ENTRY(MGMNT_ABORT);
601 TX_STATUS_ENTRY(NEXT_FRAG);
602 TX_STATUS_ENTRY(LIFE_EXPIRE);
603 TX_STATUS_ENTRY(DEST_PS);
604 TX_STATUS_ENTRY(ABORTED);
605 TX_STATUS_ENTRY(BT_RETRY);
606 TX_STATUS_ENTRY(STA_INVALID);
607 TX_STATUS_ENTRY(FRAG_DROPPED);
608 TX_STATUS_ENTRY(TID_DISABLE);
609 TX_STATUS_ENTRY(FRAME_FLUSHED);
610 TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
611 TX_STATUS_ENTRY(TX_LOCKED);
612 TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
613 }
614
615 return "UNKNOWN";
616}
617EXPORT_SYMBOL(iwl_get_tx_fail_reason);
618#endif /* CONFIG_IWLWIFI_DEBUG */