blob: aea6a2eee9c34ee38433fa4bb3acebfaa743c9b6 [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 {
Adel Gadllahc8406ea2010-03-14 19:16:25 +010086 IWL_DEBUG_TX(priv, "free more than tfds_in_queue (%u:%d)\n",
Wey-Yi Guya239a8b2010-02-19 15:47:32 -080087 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 Ortiza8e74e22009-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 Ortiza8e74e22009-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;
Zhu Yidd487442010-03-22 02:28:41 -0700155 bool huge = false;
Tomas Winkler961ba602008-10-14 12:32:44 -0700156
157 if (q->n_bd == 0)
158 return;
159
Zhu Yidd487442010-03-22 02:28:41 -0700160 for (; q->read_ptr != q->write_ptr;
161 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
162 /* we have no way to tell if it is a huge cmd ATM */
163 i = get_cmd_index(q, q->read_ptr, 0);
164
165 if (txq->meta[i].flags & CMD_SIZE_HUGE) {
166 huge = true;
167 continue;
168 }
169
170 pci_unmap_single(priv->pci_dev,
171 pci_unmap_addr(&txq->meta[i], mapping),
172 pci_unmap_len(&txq->meta[i], len),
173 PCI_DMA_BIDIRECTIONAL);
174 }
175 if (huge) {
176 i = q->n_window;
177 pci_unmap_single(priv->pci_dev,
178 pci_unmap_addr(&txq->meta[i], mapping),
179 pci_unmap_len(&txq->meta[i], len),
180 PCI_DMA_BIDIRECTIONAL);
181 }
182
Tomas Winkler961ba602008-10-14 12:32:44 -0700183 /* De-alloc array of command/tx buffers */
184 for (i = 0; i <= TFD_CMD_SLOTS; i++)
185 kfree(txq->cmd[i]);
186
187 /* De-alloc circular buffer of TFDs */
188 if (txq->q.n_bd)
Stanislaw Gruszkaf36d04a2010-02-10 05:07:45 -0800189 dma_free_coherent(dev, priv->hw_params.tfd_size * txq->q.n_bd,
190 txq->tfds, txq->q.dma_addr);
Tomas Winkler961ba602008-10-14 12:32:44 -0700191
Reinette Chatre28142982009-09-25 14:24:22 -0700192 /* deallocate arrays */
193 kfree(txq->cmd);
194 kfree(txq->meta);
195 txq->cmd = NULL;
196 txq->meta = NULL;
197
Tomas Winkler961ba602008-10-14 12:32:44 -0700198 /* 0-fill queue descriptor structure */
199 memset(txq, 0, sizeof(*txq));
200}
Abhijeet Kolekar3e5d2382009-03-17 21:51:49 -0700201EXPORT_SYMBOL(iwl_cmd_queue_free);
202
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800203/*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
204 * DMA services
205 *
206 * Theory of operation
207 *
208 * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
209 * of buffer descriptors, each of which points to one or more data buffers for
210 * the device to read from or fill. Driver and device exchange status of each
211 * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
212 * entries in each circular buffer, to protect against confusing empty and full
213 * queue states.
214 *
215 * The device reads or writes the data in the queues via the device's several
216 * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
217 *
218 * For Tx queue, there are low mark and high mark limits. If, after queuing
219 * the packet for Tx, free space become < low mark, Tx queue stopped. When
220 * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
221 * Tx queue resumed.
222 *
223 * See more detailed info in iwl-4965-hw.h.
224 ***************************************************/
225
226int iwl_queue_space(const struct iwl_queue *q)
227{
228 int s = q->read_ptr - q->write_ptr;
229
230 if (q->read_ptr > q->write_ptr)
231 s -= q->n_bd;
232
233 if (s <= 0)
234 s += q->n_window;
235 /* keep some reserve to not confuse empty and full situations */
236 s -= 2;
237 if (s < 0)
238 s = 0;
239 return s;
240}
241EXPORT_SYMBOL(iwl_queue_space);
242
243
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800244/**
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800245 * iwl_queue_init - Initialize queue's high/low-water and read/write indexes
246 */
Tomas Winkler443cfd42008-05-15 13:53:57 +0800247static int iwl_queue_init(struct iwl_priv *priv, struct iwl_queue *q,
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800248 int count, int slots_num, u32 id)
249{
250 q->n_bd = count;
251 q->n_window = slots_num;
252 q->id = id;
253
254 /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
255 * and iwl_queue_dec_wrap are broken. */
256 BUG_ON(!is_power_of_2(count));
257
258 /* slots_num must be power-of-two size, otherwise
259 * get_cmd_index is broken. */
260 BUG_ON(!is_power_of_2(slots_num));
261
262 q->low_mark = q->n_window / 4;
263 if (q->low_mark < 4)
264 q->low_mark = 4;
265
266 q->high_mark = q->n_window / 8;
267 if (q->high_mark < 2)
268 q->high_mark = 2;
269
270 q->write_ptr = q->read_ptr = 0;
Wey-Yi Guyb74e31a2010-03-01 17:23:50 -0800271 q->last_read_ptr = 0;
272 q->repeat_same_read_ptr = 0;
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800273
274 return 0;
275}
276
277/**
278 * iwl_tx_queue_alloc - Alloc driver data and TFD CB for one Tx/cmd queue
279 */
280static int iwl_tx_queue_alloc(struct iwl_priv *priv,
Ron Rindjunsky16466902008-05-05 10:22:50 +0800281 struct iwl_tx_queue *txq, u32 id)
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800282{
Stanislaw Gruszkaf36d04a2010-02-10 05:07:45 -0800283 struct device *dev = &priv->pci_dev->dev;
Winkler, Tomas3978e5b2009-01-23 13:45:23 -0800284 size_t tfd_sz = priv->hw_params.tfd_size * TFD_QUEUE_SIZE_MAX;
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800285
286 /* Driver private data, only for Tx (not command) queues,
287 * not shared with device. */
288 if (id != IWL_CMD_QUEUE_NUM) {
289 txq->txb = kmalloc(sizeof(txq->txb[0]) *
290 TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
291 if (!txq->txb) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800292 IWL_ERR(priv, "kmalloc for auxiliary BD "
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800293 "structures failed\n");
294 goto error;
295 }
Winkler, Tomas3978e5b2009-01-23 13:45:23 -0800296 } else {
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800297 txq->txb = NULL;
Winkler, Tomas3978e5b2009-01-23 13:45:23 -0800298 }
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800299
300 /* Circular buffer of transmit frame descriptors (TFDs),
301 * shared with device */
Stanislaw Gruszkaf36d04a2010-02-10 05:07:45 -0800302 txq->tfds = dma_alloc_coherent(dev, tfd_sz, &txq->q.dma_addr,
303 GFP_KERNEL);
Tomas Winkler499b1882008-10-14 12:32:48 -0700304 if (!txq->tfds) {
Winkler, Tomas3978e5b2009-01-23 13:45:23 -0800305 IWL_ERR(priv, "pci_alloc_consistent(%zd) failed\n", tfd_sz);
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800306 goto error;
307 }
308 txq->q.id = id;
309
310 return 0;
311
312 error:
313 kfree(txq->txb);
314 txq->txb = NULL;
315
316 return -ENOMEM;
317}
318
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800319/**
320 * iwl_tx_queue_init - Allocate and initialize one tx/cmd queue
321 */
Samuel Ortiza8e74e22009-01-23 13:45:14 -0800322int iwl_tx_queue_init(struct iwl_priv *priv, struct iwl_tx_queue *txq,
323 int slots_num, u32 txq_id)
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800324{
Gregory Greenmanda99c4b2008-08-04 16:00:40 +0800325 int i, len;
Tomas Winkler73b7d742008-09-03 11:18:48 +0800326 int ret;
Johannes Bergc2acea82009-07-24 11:13:05 -0700327 int actual_slots = slots_num;
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800328
329 /*
330 * Alloc buffer array for commands (Tx or other types of commands).
331 * For the command queue (#4), allocate command space + one big
332 * command for scan, since scan command is very huge; the system will
333 * not have two scans at the same time, so only one is needed.
334 * For normal Tx queues (all other queues), no super-size command
335 * space is needed.
336 */
Johannes Bergc2acea82009-07-24 11:13:05 -0700337 if (txq_id == IWL_CMD_QUEUE_NUM)
338 actual_slots++;
339
340 txq->meta = kzalloc(sizeof(struct iwl_cmd_meta) * actual_slots,
341 GFP_KERNEL);
342 txq->cmd = kzalloc(sizeof(struct iwl_device_cmd *) * actual_slots,
343 GFP_KERNEL);
344
345 if (!txq->meta || !txq->cmd)
346 goto out_free_arrays;
347
348 len = sizeof(struct iwl_device_cmd);
349 for (i = 0; i < actual_slots; i++) {
350 /* only happens for cmd queue */
351 if (i == slots_num)
Abhijeet Kolekar89612122010-02-19 11:49:49 -0800352 len = IWL_MAX_CMD_SIZE;
Gregory Greenmanda99c4b2008-08-04 16:00:40 +0800353
John W. Linville49898852008-09-02 15:07:18 -0400354 txq->cmd[i] = kmalloc(len, GFP_KERNEL);
Gregory Greenmanda99c4b2008-08-04 16:00:40 +0800355 if (!txq->cmd[i])
Tomas Winkler73b7d742008-09-03 11:18:48 +0800356 goto err;
Gregory Greenmanda99c4b2008-08-04 16:00:40 +0800357 }
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800358
359 /* Alloc driver data array and TFD circular buffer */
Tomas Winkler73b7d742008-09-03 11:18:48 +0800360 ret = iwl_tx_queue_alloc(priv, txq, txq_id);
361 if (ret)
362 goto err;
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800363
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800364 txq->need_update = 0;
365
Johannes Berg1a716552009-11-06 14:52:51 -0800366 /*
367 * Aggregation TX queues will get their ID when aggregation begins;
368 * they overwrite the setting done here. The command FIFO doesn't
369 * need an swq_id so don't set one to catch errors, all others can
370 * be set up to the identity mapping.
371 */
372 if (txq_id != IWL_CMD_QUEUE_NUM)
Johannes Berg45af8192009-06-19 13:52:43 -0700373 txq->swq_id = txq_id;
374
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800375 /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
376 * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
377 BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
378
379 /* Initialize queue's high/low-water marks, and head/tail indexes */
380 iwl_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
381
382 /* Tell device where to find queue */
Samuel Ortiza8e74e22009-01-23 13:45:14 -0800383 priv->cfg->ops->lib->txq_init(priv, txq);
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800384
385 return 0;
Tomas Winkler73b7d742008-09-03 11:18:48 +0800386err:
Johannes Bergc2acea82009-07-24 11:13:05 -0700387 for (i = 0; i < actual_slots; i++)
Tomas Winkler73b7d742008-09-03 11:18:48 +0800388 kfree(txq->cmd[i]);
Johannes Bergc2acea82009-07-24 11:13:05 -0700389out_free_arrays:
390 kfree(txq->meta);
391 kfree(txq->cmd);
Tomas Winkler73b7d742008-09-03 11:18:48 +0800392
Tomas Winkler73b7d742008-09-03 11:18:48 +0800393 return -ENOMEM;
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800394}
Samuel Ortiza8e74e22009-01-23 13:45:14 -0800395EXPORT_SYMBOL(iwl_tx_queue_init);
396
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800397/*************** HOST COMMAND QUEUE FUNCTIONS *****/
398
399/**
400 * iwl_enqueue_hcmd - enqueue a uCode command
401 * @priv: device private data point
402 * @cmd: a point to the ucode command structure
403 *
404 * The function returns < 0 values to indicate the operation is
405 * failed. On success, it turns the index (> 0) of command in the
406 * command queue.
407 */
408int iwl_enqueue_hcmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
409{
410 struct iwl_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
411 struct iwl_queue *q = &txq->q;
Johannes Bergc2acea82009-07-24 11:13:05 -0700412 struct iwl_device_cmd *out_cmd;
413 struct iwl_cmd_meta *out_meta;
Tomas Winklerf3674222008-08-04 16:00:44 +0800414 dma_addr_t phys_addr;
415 unsigned long flags;
Abhijeet Kolekar7bfedc52010-02-03 13:47:56 -0800416 int len;
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800417 u32 idx;
418 u16 fix_size;
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800419
420 cmd->len = priv->cfg->ops->utils->get_hcmd_size(cmd->id, cmd->len);
421 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
422
423 /* If any of the command structures end up being larger than
424 * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
Abhijeet Kolekar89612122010-02-19 11:49:49 -0800425 * we will need to increase the size of the TFD entries
426 * Also, check to see if command buffer should not exceed the size
427 * of device_cmd and max_cmd_size. */
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800428 BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
Johannes Bergc2acea82009-07-24 11:13:05 -0700429 !(cmd->flags & CMD_SIZE_HUGE));
Abhijeet Kolekar89612122010-02-19 11:49:49 -0800430 BUG_ON(fix_size > IWL_MAX_CMD_SIZE);
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800431
Wey-Yi Guy7812b162009-10-02 13:43:58 -0700432 if (iwl_is_rfkill(priv) || iwl_is_ctkill(priv)) {
Reinette Chatref2f21b42009-10-30 14:36:15 -0700433 IWL_WARN(priv, "Not sending command - %s KILL\n",
434 iwl_is_rfkill(priv) ? "RF" : "CT");
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800435 return -EIO;
436 }
437
Johannes Bergc2acea82009-07-24 11:13:05 -0700438 if (iwl_queue_space(q) < ((cmd->flags & CMD_ASYNC) ? 2 : 1)) {
Wey-Yi Guy2d237f72009-11-20 12:05:08 -0800439 IWL_ERR(priv, "No space in command queue\n");
Wey-Yi Guy7812b162009-10-02 13:43:58 -0700440 if (iwl_within_ct_kill_margin(priv))
441 iwl_tt_enter_ct_kill(priv);
442 else {
443 IWL_ERR(priv, "Restarting adapter due to queue full\n");
444 queue_work(priv->workqueue, &priv->restart);
445 }
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800446 return -ENOSPC;
447 }
448
449 spin_lock_irqsave(&priv->hcmd_lock, flags);
450
Zhu Yidd487442010-03-22 02:28:41 -0700451 /* If this is a huge cmd, mark the huge flag also on the meta.flags
452 * of the _original_ cmd. This is used for DMA mapping clean up.
453 */
454 if (cmd->flags & CMD_SIZE_HUGE) {
455 idx = get_cmd_index(q, q->write_ptr, 0);
456 txq->meta[idx].flags = CMD_SIZE_HUGE;
457 }
458
Johannes Bergc2acea82009-07-24 11:13:05 -0700459 idx = get_cmd_index(q, q->write_ptr, cmd->flags & CMD_SIZE_HUGE);
Gregory Greenmanda99c4b2008-08-04 16:00:40 +0800460 out_cmd = txq->cmd[idx];
Johannes Bergc2acea82009-07-24 11:13:05 -0700461 out_meta = &txq->meta[idx];
462
Daniel C Halperin8ce73f32009-07-31 14:28:06 -0700463 memset(out_meta, 0, sizeof(*out_meta)); /* re-initialize to NULL */
Johannes Bergc2acea82009-07-24 11:13:05 -0700464 out_meta->flags = cmd->flags;
465 if (cmd->flags & CMD_WANT_SKB)
466 out_meta->source = cmd;
467 if (cmd->flags & CMD_ASYNC)
468 out_meta->callback = cmd->callback;
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800469
470 out_cmd->hdr.cmd = cmd->id;
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800471 memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
472
473 /* At this point, the out_cmd now has all of the incoming cmd
474 * information */
475
476 out_cmd->hdr.flags = 0;
477 out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
478 INDEX_TO_SEQ(q->write_ptr));
Johannes Bergc2acea82009-07-24 11:13:05 -0700479 if (cmd->flags & CMD_SIZE_HUGE)
Tomas Winkler9734cb22008-09-03 11:26:52 +0800480 out_cmd->hdr.sequence |= SEQ_HUGE_FRAME;
Johannes Bergc2acea82009-07-24 11:13:05 -0700481 len = sizeof(struct iwl_device_cmd);
Abhijeet Kolekar89612122010-02-19 11:49:49 -0800482 if (idx == TFD_CMD_SLOTS)
483 len = IWL_MAX_CMD_SIZE;
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800484
Esti Kummerded2ae72008-08-04 16:00:45 +0800485#ifdef CONFIG_IWLWIFI_DEBUG
486 switch (out_cmd->hdr.cmd) {
487 case REPLY_TX_LINK_QUALITY_CMD:
488 case SENSITIVITY_CMD:
Tomas Winklere1623442009-01-27 14:27:56 -0800489 IWL_DEBUG_HC_DUMP(priv, "Sending command %s (#%x), seq: 0x%04X, "
Esti Kummerded2ae72008-08-04 16:00:45 +0800490 "%d bytes at %d[%d]:%d\n",
491 get_cmd_string(out_cmd->hdr.cmd),
492 out_cmd->hdr.cmd,
493 le16_to_cpu(out_cmd->hdr.sequence), fix_size,
494 q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
495 break;
496 default:
Tomas Winklere1623442009-01-27 14:27:56 -0800497 IWL_DEBUG_HC(priv, "Sending command %s (#%x), seq: 0x%04X, "
Esti Kummerded2ae72008-08-04 16:00:45 +0800498 "%d bytes at %d[%d]:%d\n",
499 get_cmd_string(out_cmd->hdr.cmd),
500 out_cmd->hdr.cmd,
501 le16_to_cpu(out_cmd->hdr.sequence), fix_size,
502 q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
503 }
504#endif
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800505 txq->need_update = 1;
506
Samuel Ortiz518099a2009-01-19 15:30:27 -0800507 if (priv->cfg->ops->lib->txq_update_byte_cnt_tbl)
508 /* Set up entry in queue's byte count circular buffer */
509 priv->cfg->ops->lib->txq_update_byte_cnt_tbl(priv, txq, 0);
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800510
Reinette Chatredf833b12009-04-21 10:55:48 -0700511 phys_addr = pci_map_single(priv->pci_dev, &out_cmd->hdr,
512 fix_size, PCI_DMA_BIDIRECTIONAL);
Johannes Bergc2acea82009-07-24 11:13:05 -0700513 pci_unmap_addr_set(out_meta, mapping, phys_addr);
514 pci_unmap_len_set(out_meta, len, fix_size);
Reinette Chatredf833b12009-04-21 10:55:48 -0700515
Johannes Bergbe1a71a2009-10-02 13:44:02 -0700516 trace_iwlwifi_dev_hcmd(priv, &out_cmd->hdr, fix_size, cmd->flags);
517
Reinette Chatredf833b12009-04-21 10:55:48 -0700518 priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
519 phys_addr, fix_size, 1,
520 U32_PAD(cmd->len));
521
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800522 /* Increment and update queue's write index */
523 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Abhijeet Kolekar7bfedc52010-02-03 13:47:56 -0800524 iwl_txq_update_write_ptr(priv, txq);
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800525
526 spin_unlock_irqrestore(&priv->hcmd_lock, flags);
Abhijeet Kolekar7bfedc52010-02-03 13:47:56 -0800527 return idx;
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800528}
529
Tomas Winkler17b88922008-05-29 16:35:12 +0800530/**
531 * iwl_hcmd_queue_reclaim - Reclaim TX command queue entries already Tx'd
532 *
533 * When FW advances 'R' index, all entries between old and new 'R' index
534 * need to be reclaimed. As result, some free space forms. If there is
535 * enough free space (> low mark), wake the stack that feeds us.
536 */
Tomas Winkler499b1882008-10-14 12:32:48 -0700537static void iwl_hcmd_queue_reclaim(struct iwl_priv *priv, int txq_id,
538 int idx, int cmd_idx)
Tomas Winkler17b88922008-05-29 16:35:12 +0800539{
540 struct iwl_tx_queue *txq = &priv->txq[txq_id];
541 struct iwl_queue *q = &txq->q;
542 int nfreed = 0;
543
Tomas Winkler499b1882008-10-14 12:32:48 -0700544 if ((idx >= q->n_bd) || (iwl_queue_used(q, idx) == 0)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800545 IWL_ERR(priv, "Read index for DMA queue txq id (%d), index %d, "
Tomas Winkler17b88922008-05-29 16:35:12 +0800546 "is out of range [0-%d] %d %d.\n", txq_id,
Tomas Winkler499b1882008-10-14 12:32:48 -0700547 idx, q->n_bd, q->write_ptr, q->read_ptr);
Tomas Winkler17b88922008-05-29 16:35:12 +0800548 return;
549 }
550
Tomas Winkler499b1882008-10-14 12:32:48 -0700551 for (idx = iwl_queue_inc_wrap(idx, q->n_bd); q->read_ptr != idx;
552 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
553
554 if (nfreed++ > 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800555 IWL_ERR(priv, "HCMD skipped: index (%d) %d %d\n", idx,
Tomas Winkler17b88922008-05-29 16:35:12 +0800556 q->write_ptr, q->read_ptr);
557 queue_work(priv->workqueue, &priv->restart);
558 }
Gregory Greenmanda99c4b2008-08-04 16:00:40 +0800559
Tomas Winkler17b88922008-05-29 16:35:12 +0800560 }
561}
562
563/**
564 * iwl_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
565 * @rxb: Rx buffer to reclaim
566 *
567 * If an Rx buffer has an async callback associated with it the callback
568 * will be executed. The attached skb (if present) will only be freed
569 * if the callback returns 1
570 */
571void iwl_tx_cmd_complete(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
572{
Zhu Yi2f301222009-10-09 17:19:45 +0800573 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Tomas Winkler17b88922008-05-29 16:35:12 +0800574 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
575 int txq_id = SEQ_TO_QUEUE(sequence);
576 int index = SEQ_TO_INDEX(sequence);
Tomas Winkler17b88922008-05-29 16:35:12 +0800577 int cmd_index;
Tomas Winkler9734cb22008-09-03 11:26:52 +0800578 bool huge = !!(pkt->hdr.sequence & SEQ_HUGE_FRAME);
Johannes Bergc2acea82009-07-24 11:13:05 -0700579 struct iwl_device_cmd *cmd;
580 struct iwl_cmd_meta *meta;
Zhu Yidd487442010-03-22 02:28:41 -0700581 struct iwl_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
Tomas Winkler17b88922008-05-29 16:35:12 +0800582
583 /* If a Tx command is being handled and it isn't in the actual
584 * command queue then there a command routing bug has been introduced
585 * in the queue management code. */
Johannes Berg55d6a3c2008-09-23 19:18:43 +0200586 if (WARN(txq_id != IWL_CMD_QUEUE_NUM,
Winkler, Tomas01ef9322008-11-07 09:58:45 -0800587 "wrong command queue %d, sequence 0x%X readp=%d writep=%d\n",
588 txq_id, sequence,
589 priv->txq[IWL_CMD_QUEUE_NUM].q.read_ptr,
590 priv->txq[IWL_CMD_QUEUE_NUM].q.write_ptr)) {
Reinette Chatreec741162009-07-24 11:13:08 -0700591 iwl_print_hex_error(priv, pkt, 32);
Johannes Berg55d6a3c2008-09-23 19:18:43 +0200592 return;
Winkler, Tomas01ef9322008-11-07 09:58:45 -0800593 }
Tomas Winkler17b88922008-05-29 16:35:12 +0800594
Zhu Yidd487442010-03-22 02:28:41 -0700595 /* If this is a huge cmd, clear the huge flag on the meta.flags
596 * of the _original_ cmd. So that iwl_cmd_queue_free won't unmap
597 * the DMA buffer for the scan (huge) command.
598 */
599 if (huge) {
600 cmd_index = get_cmd_index(&txq->q, index, 0);
601 txq->meta[cmd_index].flags = 0;
602 }
603 cmd_index = get_cmd_index(&txq->q, index, huge);
604 cmd = txq->cmd[cmd_index];
605 meta = &txq->meta[cmd_index];
Tomas Winkler17b88922008-05-29 16:35:12 +0800606
Reinette Chatrec33de622009-10-30 14:36:10 -0700607 pci_unmap_single(priv->pci_dev,
608 pci_unmap_addr(meta, mapping),
609 pci_unmap_len(meta, len),
610 PCI_DMA_BIDIRECTIONAL);
611
Tomas Winkler17b88922008-05-29 16:35:12 +0800612 /* Input error checking is done when commands are added to queue. */
Johannes Bergc2acea82009-07-24 11:13:05 -0700613 if (meta->flags & CMD_WANT_SKB) {
Zhu Yi2f301222009-10-09 17:19:45 +0800614 meta->source->reply_page = (unsigned long)rxb_addr(rxb);
615 rxb->page = NULL;
Johannes Berg5696aea2009-07-24 11:13:06 -0700616 } else if (meta->callback)
Zhu Yi2f301222009-10-09 17:19:45 +0800617 meta->callback(priv, cmd, pkt);
Tomas Winkler17b88922008-05-29 16:35:12 +0800618
Tomas Winkler499b1882008-10-14 12:32:48 -0700619 iwl_hcmd_queue_reclaim(priv, txq_id, index, cmd_index);
Tomas Winkler17b88922008-05-29 16:35:12 +0800620
Johannes Bergc2acea82009-07-24 11:13:05 -0700621 if (!(meta->flags & CMD_ASYNC)) {
Tomas Winkler17b88922008-05-29 16:35:12 +0800622 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
Frans Pop91dd6c22010-03-24 14:19:58 -0700623 IWL_DEBUG_INFO(priv, "Clearing HCMD_ACTIVE for command %s\n",
Reinette Chatred2dfe6d2010-02-18 22:03:04 -0800624 get_cmd_string(cmd->hdr.cmd));
Tomas Winkler17b88922008-05-29 16:35:12 +0800625 wake_up_interruptible(&priv->wait_command_queue);
626 }
Zhu Yidd487442010-03-22 02:28:41 -0700627 meta->flags = 0;
Tomas Winkler17b88922008-05-29 16:35:12 +0800628}
629EXPORT_SYMBOL(iwl_tx_cmd_complete);
630
Helmut Schaa994d31f2008-07-02 12:17:06 +0200631#ifdef CONFIG_IWLWIFI_DEBUG
Wey-Yi Guy04569cb2010-03-31 17:57:28 -0700632#define TX_STATUS_FAIL(x) case TX_STATUS_FAIL_ ## x: return #x
633#define TX_STATUS_POSTPONE(x) case TX_STATUS_POSTPONE_ ## x: return #x
Tomas Winklera332f8d2008-05-29 16:35:08 +0800634
635const char *iwl_get_tx_fail_reason(u32 status)
636{
637 switch (status & TX_STATUS_MSK) {
638 case TX_STATUS_SUCCESS:
639 return "SUCCESS";
Wey-Yi Guy04569cb2010-03-31 17:57:28 -0700640 TX_STATUS_POSTPONE(DELAY);
641 TX_STATUS_POSTPONE(FEW_BYTES);
642 TX_STATUS_POSTPONE(BT_PRIO);
643 TX_STATUS_POSTPONE(QUIET_PERIOD);
644 TX_STATUS_POSTPONE(CALC_TTAK);
645 TX_STATUS_FAIL(INTERNAL_CROSSED_RETRY);
646 TX_STATUS_FAIL(SHORT_LIMIT);
647 TX_STATUS_FAIL(LONG_LIMIT);
648 TX_STATUS_FAIL(FIFO_UNDERRUN);
649 TX_STATUS_FAIL(DRAIN_FLOW);
650 TX_STATUS_FAIL(RFKILL_FLUSH);
651 TX_STATUS_FAIL(LIFE_EXPIRE);
652 TX_STATUS_FAIL(DEST_PS);
653 TX_STATUS_FAIL(HOST_ABORTED);
654 TX_STATUS_FAIL(BT_RETRY);
655 TX_STATUS_FAIL(STA_INVALID);
656 TX_STATUS_FAIL(FRAG_DROPPED);
657 TX_STATUS_FAIL(TID_DISABLE);
658 TX_STATUS_FAIL(FIFO_FLUSHED);
659 TX_STATUS_FAIL(INSUFFICIENT_CF_POLL);
660 TX_STATUS_FAIL(FW_DROP);
661 TX_STATUS_FAIL(STA_COLOR_MISMATCH_DROP);
Tomas Winklera332f8d2008-05-29 16:35:08 +0800662 }
663
664 return "UNKNOWN";
665}
666EXPORT_SYMBOL(iwl_get_tx_fail_reason);
667#endif /* CONFIG_IWLWIFI_DEBUG */