blob: c4cd363d81f2f028ec17cb81716b16698eb5bb46 [file] [log] [blame]
Emmanuel Grumbachc85eb612011-06-14 10:13:24 +03001/******************************************************************************
2 *
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
5 *
6 * GPL LICENSE SUMMARY
7 *
8 * Copyright(c) 2007 - 2011 Intel Corporation. All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
22 * USA
23 *
24 * The full GNU General Public License is included in this distribution
25 * in the file called LICENSE.GPL.
26 *
27 * Contact Information:
28 * Intel Linux Wireless <ilw@linux.intel.com>
29 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30 *
31 * BSD LICENSE
32 *
33 * Copyright(c) 2005 - 2011 Intel Corporation. All rights reserved.
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 *
40 * * Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * * Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in
44 * the documentation and/or other materials provided with the
45 * distribution.
46 * * Neither the name Intel Corporation nor the names of its
47 * contributors may be used to endorse or promote products derived
48 * from this software without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 *
62 *****************************************************************************/
Emmanuel Grumbacha0f6b0a2011-06-21 14:25:45 +030063#include "iwl-dev.h"
Emmanuel Grumbachc85eb612011-06-14 10:13:24 +030064#include "iwl-trans.h"
Emmanuel Grumbach02aca582011-06-28 08:58:41 -070065#include "iwl-core.h"
66#include "iwl-helpers.h"
67/*TODO remove uneeded includes when the transport layer tx_free will be here */
68#include "iwl-agn.h"
Emmanuel Grumbachc85eb612011-06-14 10:13:24 +030069
70static int iwl_trans_rx_alloc(struct iwl_priv *priv)
71{
72 struct iwl_rx_queue *rxq = &priv->rxq;
73 struct device *dev = priv->bus.dev;
74
75 memset(&priv->rxq, 0, sizeof(priv->rxq));
76
77 spin_lock_init(&rxq->lock);
78 INIT_LIST_HEAD(&rxq->rx_free);
79 INIT_LIST_HEAD(&rxq->rx_used);
80
81 if (WARN_ON(rxq->bd || rxq->rb_stts))
82 return -EINVAL;
83
84 /* Allocate the circular buffer of Read Buffer Descriptors (RBDs) */
Emmanuel Grumbacha0f6b0a2011-06-21 14:25:45 +030085 rxq->bd = dma_alloc_coherent(dev, sizeof(__le32) * RX_QUEUE_SIZE,
86 &rxq->bd_dma, GFP_KERNEL);
Emmanuel Grumbachc85eb612011-06-14 10:13:24 +030087 if (!rxq->bd)
88 goto err_bd;
Emmanuel Grumbacha0f6b0a2011-06-21 14:25:45 +030089 memset(rxq->bd, 0, sizeof(__le32) * RX_QUEUE_SIZE);
Emmanuel Grumbachc85eb612011-06-14 10:13:24 +030090
91 /*Allocate the driver's pointer to receive buffer status */
92 rxq->rb_stts = dma_alloc_coherent(dev, sizeof(*rxq->rb_stts),
93 &rxq->rb_stts_dma, GFP_KERNEL);
94 if (!rxq->rb_stts)
95 goto err_rb_stts;
96 memset(rxq->rb_stts, 0, sizeof(*rxq->rb_stts));
97
98 return 0;
99
100err_rb_stts:
Emmanuel Grumbacha0f6b0a2011-06-21 14:25:45 +0300101 dma_free_coherent(dev, sizeof(__le32) * RX_QUEUE_SIZE,
102 rxq->bd, rxq->bd_dma);
Emmanuel Grumbachc85eb612011-06-14 10:13:24 +0300103 memset(&rxq->bd_dma, 0, sizeof(rxq->bd_dma));
104 rxq->bd = NULL;
105err_bd:
106 return -ENOMEM;
107}
108
Emmanuel Grumbacha0f6b0a2011-06-21 14:25:45 +0300109static void iwl_trans_rxq_free_rx_bufs(struct iwl_priv *priv)
110{
111 struct iwl_rx_queue *rxq = &priv->rxq;
112 int i;
113
114 /* Fill the rx_used queue with _all_ of the Rx buffers */
115 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
116 /* In the reset function, these buffers may have been allocated
117 * to an SKB, so we need to unmap and free potential storage */
118 if (rxq->pool[i].page != NULL) {
119 dma_unmap_page(priv->bus.dev, rxq->pool[i].page_dma,
120 PAGE_SIZE << priv->hw_params.rx_page_order,
121 DMA_FROM_DEVICE);
122 __iwl_free_pages(priv, rxq->pool[i].page);
123 rxq->pool[i].page = NULL;
124 }
125 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
126 }
127}
128
Emmanuel Grumbachc85eb612011-06-14 10:13:24 +0300129static int iwl_trans_rx_init(struct iwl_priv *priv)
130{
131 struct iwl_rx_queue *rxq = &priv->rxq;
132 int i, err;
133 unsigned long flags;
134
135 if (!rxq->bd) {
136 err = iwl_trans_rx_alloc(priv);
137 if (err)
138 return err;
139 }
140
141 spin_lock_irqsave(&rxq->lock, flags);
142 INIT_LIST_HEAD(&rxq->rx_free);
143 INIT_LIST_HEAD(&rxq->rx_used);
144
Emmanuel Grumbacha0f6b0a2011-06-21 14:25:45 +0300145 iwl_trans_rxq_free_rx_bufs(priv);
Emmanuel Grumbachc85eb612011-06-14 10:13:24 +0300146
147 for (i = 0; i < RX_QUEUE_SIZE; i++)
148 rxq->queue[i] = NULL;
149
150 /* Set us so that we have processed and used all buffers, but have
151 * not restocked the Rx queue with fresh buffers */
152 rxq->read = rxq->write = 0;
153 rxq->write_actual = 0;
154 rxq->free_count = 0;
155 spin_unlock_irqrestore(&rxq->lock, flags);
156
157 return 0;
158}
159
Emmanuel Grumbacha0f6b0a2011-06-21 14:25:45 +0300160static void iwl_trans_rx_free(struct iwl_priv *priv)
161{
162 struct iwl_rx_queue *rxq = &priv->rxq;
163 unsigned long flags;
164
165 /*if rxq->bd is NULL, it means that nothing has been allocated,
166 * exit now */
167 if (!rxq->bd) {
168 IWL_DEBUG_INFO(priv, "Free NULL rx context\n");
169 return;
170 }
171
172 spin_lock_irqsave(&rxq->lock, flags);
173 iwl_trans_rxq_free_rx_bufs(priv);
174 spin_unlock_irqrestore(&rxq->lock, flags);
175
176 dma_free_coherent(priv->bus.dev, sizeof(__le32) * RX_QUEUE_SIZE,
177 rxq->bd, rxq->bd_dma);
178 memset(&rxq->bd_dma, 0, sizeof(rxq->bd_dma));
179 rxq->bd = NULL;
180
181 if (rxq->rb_stts)
182 dma_free_coherent(priv->bus.dev,
183 sizeof(struct iwl_rb_status),
184 rxq->rb_stts, rxq->rb_stts_dma);
185 else
186 IWL_DEBUG_INFO(priv, "Free rxq->rb_stts which is NULL\n");
187 memset(&rxq->rb_stts_dma, 0, sizeof(rxq->rb_stts_dma));
188 rxq->rb_stts = NULL;
189}
190
Emmanuel Grumbachc2c52e82011-07-08 08:46:11 -0700191static int iwl_trans_rx_stop(struct iwl_priv *priv)
192{
193
194 /* stop Rx DMA */
195 iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
196 return iwl_poll_direct_bit(priv, FH_MEM_RSSR_RX_STATUS_REG,
197 FH_RSSR_CHNL0_RX_STATUS_CHNL_IDLE, 1000);
198}
199
Emmanuel Grumbach02aca582011-06-28 08:58:41 -0700200/* TODO:remove this code duplication */
201static inline int iwlagn_alloc_dma_ptr(struct iwl_priv *priv,
202 struct iwl_dma_ptr *ptr, size_t size)
203{
204 if (WARN_ON(ptr->addr))
205 return -EINVAL;
206
207 ptr->addr = dma_alloc_coherent(priv->bus.dev, size,
208 &ptr->dma, GFP_KERNEL);
209 if (!ptr->addr)
210 return -ENOMEM;
211 ptr->size = size;
212 return 0;
213}
214
Emmanuel Grumbach1359ca42011-07-08 08:46:10 -0700215static inline void iwlagn_free_dma_ptr(struct iwl_priv *priv,
216 struct iwl_dma_ptr *ptr)
217{
218 if (unlikely(!ptr->addr))
219 return;
220
221 dma_free_coherent(priv->bus.dev, ptr->size, ptr->addr, ptr->dma);
222 memset(ptr, 0, sizeof(*ptr));
223}
224
Emmanuel Grumbach02aca582011-06-28 08:58:41 -0700225static int iwl_trans_txq_alloc(struct iwl_priv *priv, struct iwl_tx_queue *txq,
226 int slots_num, u32 txq_id)
227{
228 size_t tfd_sz = priv->hw_params.tfd_size * TFD_QUEUE_SIZE_MAX;
229 int i;
230
231 if (WARN_ON(txq->meta || txq->cmd || txq->txb || txq->tfds))
232 return -EINVAL;
233
Emmanuel Grumbach1359ca42011-07-08 08:46:10 -0700234 txq->q.n_window = slots_num;
235
Emmanuel Grumbach02aca582011-06-28 08:58:41 -0700236 txq->meta = kzalloc(sizeof(txq->meta[0]) * slots_num,
237 GFP_KERNEL);
238 txq->cmd = kzalloc(sizeof(txq->cmd[0]) * slots_num,
239 GFP_KERNEL);
240
241 if (!txq->meta || !txq->cmd)
242 goto error;
243
244 for (i = 0; i < slots_num; i++) {
245 txq->cmd[i] = kmalloc(sizeof(struct iwl_device_cmd),
246 GFP_KERNEL);
247 if (!txq->cmd[i])
248 goto error;
249 }
250
251 /* Alloc driver data array and TFD circular buffer */
252 /* Driver private data, only for Tx (not command) queues,
253 * not shared with device. */
254 if (txq_id != priv->cmd_queue) {
255 txq->txb = kzalloc(sizeof(txq->txb[0]) *
256 TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
257 if (!txq->txb) {
258 IWL_ERR(priv, "kmalloc for auxiliary BD "
259 "structures failed\n");
260 goto error;
261 }
262 } else {
263 txq->txb = NULL;
264 }
265
266 /* Circular buffer of transmit frame descriptors (TFDs),
267 * shared with device */
268 txq->tfds = dma_alloc_coherent(priv->bus.dev, tfd_sz, &txq->q.dma_addr,
269 GFP_KERNEL);
270 if (!txq->tfds) {
271 IWL_ERR(priv, "dma_alloc_coherent(%zd) failed\n", tfd_sz);
272 goto error;
273 }
274 txq->q.id = txq_id;
275
276 return 0;
277error:
278 kfree(txq->txb);
279 txq->txb = NULL;
280 /* since txq->cmd has been zeroed,
281 * all non allocated cmd[i] will be NULL */
282 if (txq->cmd)
283 for (i = 0; i < slots_num; i++)
284 kfree(txq->cmd[i]);
285 kfree(txq->meta);
286 kfree(txq->cmd);
287 txq->meta = NULL;
288 txq->cmd = NULL;
289
290 return -ENOMEM;
291
292}
293
294static int iwl_trans_txq_init(struct iwl_priv *priv, struct iwl_tx_queue *txq,
295 int slots_num, u32 txq_id)
296{
297 int ret;
298
299 txq->need_update = 0;
300 memset(txq->meta, 0, sizeof(txq->meta[0]) * slots_num);
301
302 /*
303 * For the default queues 0-3, set up the swq_id
304 * already -- all others need to get one later
305 * (if they need one at all).
306 */
307 if (txq_id < 4)
308 iwl_set_swq_id(txq, txq_id, txq_id);
309
310 /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
311 * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
312 BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
313
314 /* Initialize queue's high/low-water marks, and head/tail indexes */
315 ret = iwl_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num,
316 txq_id);
317 if (ret)
318 return ret;
319
320 /*
321 * Tell nic where to find circular buffer of Tx Frame Descriptors for
322 * given Tx queue, and enable the DMA channel used for that queue.
323 * Circular buffer (TFD queue in DRAM) physical base address */
324 iwl_write_direct32(priv, FH_MEM_CBBC_QUEUE(txq_id),
325 txq->q.dma_addr >> 8);
326
327 return 0;
328}
329
330/**
Emmanuel Grumbach1359ca42011-07-08 08:46:10 -0700331 * iwl_tx_queue_free - Deallocate DMA queue.
332 * @txq: Transmit queue to deallocate.
333 *
334 * Empty queue by removing and destroying all BD's.
335 * Free all buffers.
336 * 0-fill, but do not free "txq" descriptor structure.
337 */
338static void iwl_tx_queue_free(struct iwl_priv *priv, int txq_id)
339{
340 struct iwl_tx_queue *txq = &priv->txq[txq_id];
341 struct device *dev = priv->bus.dev;
342 int i;
343 if (WARN_ON(!txq))
344 return;
345
346 iwl_tx_queue_unmap(priv, txq_id);
347
348 /* De-alloc array of command/tx buffers */
349 for (i = 0; i < txq->q.n_window; i++)
350 kfree(txq->cmd[i]);
351
352 /* De-alloc circular buffer of TFDs */
353 if (txq->q.n_bd) {
354 dma_free_coherent(dev, priv->hw_params.tfd_size *
355 txq->q.n_bd, txq->tfds, txq->q.dma_addr);
356 memset(&txq->q.dma_addr, 0, sizeof(txq->q.dma_addr));
357 }
358
359 /* De-alloc array of per-TFD driver data */
360 kfree(txq->txb);
361 txq->txb = NULL;
362
363 /* deallocate arrays */
364 kfree(txq->cmd);
365 kfree(txq->meta);
366 txq->cmd = NULL;
367 txq->meta = NULL;
368
369 /* 0-fill queue descriptor structure */
370 memset(txq, 0, sizeof(*txq));
371}
372
373/**
374 * iwl_trans_tx_free - Free TXQ Context
375 *
376 * Destroy all TX DMA queues and structures
377 */
378static void iwl_trans_tx_free(struct iwl_priv *priv)
379{
380 int txq_id;
381
382 /* Tx queues */
383 if (priv->txq) {
384 for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++)
385 iwl_tx_queue_free(priv, txq_id);
386 }
387
388 kfree(priv->txq);
389 priv->txq = NULL;
390
391 iwlagn_free_dma_ptr(priv, &priv->kw);
392
393 iwlagn_free_dma_ptr(priv, &priv->scd_bc_tbls);
394}
395
396/**
Emmanuel Grumbach02aca582011-06-28 08:58:41 -0700397 * iwl_trans_tx_alloc - allocate TX context
398 * Allocate all Tx DMA structures and initialize them
399 *
400 * @param priv
401 * @return error code
402 */
403static int iwl_trans_tx_alloc(struct iwl_priv *priv)
404{
405 int ret;
406 int txq_id, slots_num;
407
408 /*It is not allowed to alloc twice, so warn when this happens.
409 * We cannot rely on the previous allocation, so free and fail */
410 if (WARN_ON(priv->txq)) {
411 ret = -EINVAL;
412 goto error;
413 }
414
415 ret = iwlagn_alloc_dma_ptr(priv, &priv->scd_bc_tbls,
416 priv->hw_params.scd_bc_tbls_size);
417 if (ret) {
418 IWL_ERR(priv, "Scheduler BC Table allocation failed\n");
419 goto error;
420 }
421
422 /* Alloc keep-warm buffer */
423 ret = iwlagn_alloc_dma_ptr(priv, &priv->kw, IWL_KW_SIZE);
424 if (ret) {
425 IWL_ERR(priv, "Keep Warm allocation failed\n");
426 goto error;
427 }
428
429 priv->txq = kzalloc(sizeof(struct iwl_tx_queue) *
430 priv->cfg->base_params->num_of_queues, GFP_KERNEL);
431 if (!priv->txq) {
432 IWL_ERR(priv, "Not enough memory for txq\n");
433 ret = ENOMEM;
434 goto error;
435 }
436
437 /* Alloc and init all Tx queues, including the command queue (#4/#9) */
438 for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++) {
439 slots_num = (txq_id == priv->cmd_queue) ?
440 TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
441 ret = iwl_trans_txq_alloc(priv, &priv->txq[txq_id], slots_num,
442 txq_id);
443 if (ret) {
444 IWL_ERR(priv, "Tx %d queue alloc failed\n", txq_id);
445 goto error;
446 }
447 }
448
449 return 0;
450
451error:
Emmanuel Grumbach1359ca42011-07-08 08:46:10 -0700452 priv->trans.ops->tx_free(priv);
Emmanuel Grumbach02aca582011-06-28 08:58:41 -0700453
454 return ret;
455}
456static int iwl_trans_tx_init(struct iwl_priv *priv)
457{
458 int ret;
459 int txq_id, slots_num;
460 unsigned long flags;
461 bool alloc = false;
462
463 if (!priv->txq) {
464 ret = iwl_trans_tx_alloc(priv);
465 if (ret)
466 goto error;
467 alloc = true;
468 }
469
470 spin_lock_irqsave(&priv->lock, flags);
471
472 /* Turn off all Tx DMA fifos */
473 iwl_write_prph(priv, IWLAGN_SCD_TXFACT, 0);
474
475 /* Tell NIC where to find the "keep warm" buffer */
476 iwl_write_direct32(priv, FH_KW_MEM_ADDR_REG, priv->kw.dma >> 4);
477
478 spin_unlock_irqrestore(&priv->lock, flags);
479
480 /* Alloc and init all Tx queues, including the command queue (#4/#9) */
481 for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++) {
482 slots_num = (txq_id == priv->cmd_queue) ?
483 TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
484 ret = iwl_trans_txq_init(priv, &priv->txq[txq_id], slots_num,
485 txq_id);
486 if (ret) {
487 IWL_ERR(priv, "Tx %d queue init failed\n", txq_id);
488 goto error;
489 }
490 }
491
492 return 0;
493error:
494 /*Upon error, free only if we allocated something */
495 if (alloc)
Emmanuel Grumbach1359ca42011-07-08 08:46:10 -0700496 priv->trans.ops->tx_free(priv);
Emmanuel Grumbach02aca582011-06-28 08:58:41 -0700497 return ret;
498}
499
Emmanuel Grumbachc85eb612011-06-14 10:13:24 +0300500static const struct iwl_trans_ops trans_ops = {
501 .rx_init = iwl_trans_rx_init,
Emmanuel Grumbachc2c52e82011-07-08 08:46:11 -0700502 .rx_stop = iwl_trans_rx_stop,
Emmanuel Grumbacha0f6b0a2011-06-21 14:25:45 +0300503 .rx_free = iwl_trans_rx_free,
Emmanuel Grumbach02aca582011-06-28 08:58:41 -0700504
505 .tx_init = iwl_trans_tx_init,
Emmanuel Grumbach1359ca42011-07-08 08:46:10 -0700506 .tx_free = iwl_trans_tx_free,
Emmanuel Grumbachc85eb612011-06-14 10:13:24 +0300507};
508
509void iwl_trans_register(struct iwl_trans *trans)
510{
511 trans->ops = &trans_ops;
512}