]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/net/wireless/iwlwifi/iwl-trans.c
iwlagn: move tx transport functions to iwl-trans-tx-pcie.c
[mirror_ubuntu-bionic-kernel.git] / drivers / net / wireless / iwlwifi / iwl-trans.c
CommitLineData
c85eb619
EG
1/******************************************************************************
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 *****************************************************************************/
a0f6b0a2 63#include "iwl-dev.h"
c85eb619 64#include "iwl-trans.h"
02aca585
EG
65#include "iwl-core.h"
66#include "iwl-helpers.h"
ab697a9f 67#include "iwl-trans-int-pcie.h"
02aca585
EG
68/*TODO remove uneeded includes when the transport layer tx_free will be here */
69#include "iwl-agn.h"
e419d62d 70#include "iwl-core.h"
c85eb619
EG
71
72static int iwl_trans_rx_alloc(struct iwl_priv *priv)
73{
74 struct iwl_rx_queue *rxq = &priv->rxq;
75 struct device *dev = priv->bus.dev;
76
77 memset(&priv->rxq, 0, sizeof(priv->rxq));
78
79 spin_lock_init(&rxq->lock);
80 INIT_LIST_HEAD(&rxq->rx_free);
81 INIT_LIST_HEAD(&rxq->rx_used);
82
83 if (WARN_ON(rxq->bd || rxq->rb_stts))
84 return -EINVAL;
85
86 /* Allocate the circular buffer of Read Buffer Descriptors (RBDs) */
a0f6b0a2
EG
87 rxq->bd = dma_alloc_coherent(dev, sizeof(__le32) * RX_QUEUE_SIZE,
88 &rxq->bd_dma, GFP_KERNEL);
c85eb619
EG
89 if (!rxq->bd)
90 goto err_bd;
a0f6b0a2 91 memset(rxq->bd, 0, sizeof(__le32) * RX_QUEUE_SIZE);
c85eb619
EG
92
93 /*Allocate the driver's pointer to receive buffer status */
94 rxq->rb_stts = dma_alloc_coherent(dev, sizeof(*rxq->rb_stts),
95 &rxq->rb_stts_dma, GFP_KERNEL);
96 if (!rxq->rb_stts)
97 goto err_rb_stts;
98 memset(rxq->rb_stts, 0, sizeof(*rxq->rb_stts));
99
100 return 0;
101
102err_rb_stts:
a0f6b0a2
EG
103 dma_free_coherent(dev, sizeof(__le32) * RX_QUEUE_SIZE,
104 rxq->bd, rxq->bd_dma);
c85eb619
EG
105 memset(&rxq->bd_dma, 0, sizeof(rxq->bd_dma));
106 rxq->bd = NULL;
107err_bd:
108 return -ENOMEM;
109}
110
a0f6b0a2 111static void iwl_trans_rxq_free_rx_bufs(struct iwl_priv *priv)
c85eb619
EG
112{
113 struct iwl_rx_queue *rxq = &priv->rxq;
a0f6b0a2 114 int i;
c85eb619
EG
115
116 /* Fill the rx_used queue with _all_ of the Rx buffers */
117 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
118 /* In the reset function, these buffers may have been allocated
119 * to an SKB, so we need to unmap and free potential storage */
120 if (rxq->pool[i].page != NULL) {
121 dma_unmap_page(priv->bus.dev, rxq->pool[i].page_dma,
122 PAGE_SIZE << priv->hw_params.rx_page_order,
123 DMA_FROM_DEVICE);
124 __iwl_free_pages(priv, rxq->pool[i].page);
125 rxq->pool[i].page = NULL;
126 }
127 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
128 }
a0f6b0a2
EG
129}
130
ab697a9f
EG
131static void iwl_trans_rx_hw_init(struct iwl_priv *priv,
132 struct iwl_rx_queue *rxq)
133{
134 u32 rb_size;
135 const u32 rfdnlog = RX_QUEUE_SIZE_LOG; /* 256 RBDs */
136 u32 rb_timeout = 0; /* FIXME: RX_RB_TIMEOUT for all devices? */
137
138 rb_timeout = RX_RB_TIMEOUT;
139
140 if (iwlagn_mod_params.amsdu_size_8K)
141 rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_8K;
142 else
143 rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
144
145 /* Stop Rx DMA */
146 iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
147
148 /* Reset driver's Rx queue write index */
149 iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
150
151 /* Tell device where to find RBD circular buffer in DRAM */
152 iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_BASE_REG,
153 (u32)(rxq->bd_dma >> 8));
154
155 /* Tell device where in DRAM to update its Rx status */
156 iwl_write_direct32(priv, FH_RSCSR_CHNL0_STTS_WPTR_REG,
157 rxq->rb_stts_dma >> 4);
158
159 /* Enable Rx DMA
160 * FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY is set because of HW bug in
161 * the credit mechanism in 5000 HW RX FIFO
162 * Direct rx interrupts to hosts
163 * Rx buffer size 4 or 8k
164 * RB timeout 0x10
165 * 256 RBDs
166 */
167 iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG,
168 FH_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
169 FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY |
170 FH_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
171 FH_RCSR_CHNL0_RX_CONFIG_SINGLE_FRAME_MSK |
172 rb_size|
173 (rb_timeout << FH_RCSR_RX_CONFIG_REG_IRQ_RBTH_POS)|
174 (rfdnlog << FH_RCSR_RX_CONFIG_RBDCB_SIZE_POS));
175
176 /* Set interrupt coalescing timer to default (2048 usecs) */
177 iwl_write8(priv, CSR_INT_COALESCING, IWL_HOST_INT_TIMEOUT_DEF);
178}
179
a0f6b0a2
EG
180static int iwl_trans_rx_init(struct iwl_priv *priv)
181{
182 struct iwl_rx_queue *rxq = &priv->rxq;
183 int i, err;
184 unsigned long flags;
185
186 if (!rxq->bd) {
187 err = iwl_trans_rx_alloc(priv);
188 if (err)
189 return err;
190 }
191
192 spin_lock_irqsave(&rxq->lock, flags);
193 INIT_LIST_HEAD(&rxq->rx_free);
194 INIT_LIST_HEAD(&rxq->rx_used);
195
196 iwl_trans_rxq_free_rx_bufs(priv);
c85eb619
EG
197
198 for (i = 0; i < RX_QUEUE_SIZE; i++)
199 rxq->queue[i] = NULL;
200
201 /* Set us so that we have processed and used all buffers, but have
202 * not restocked the Rx queue with fresh buffers */
203 rxq->read = rxq->write = 0;
204 rxq->write_actual = 0;
205 rxq->free_count = 0;
206 spin_unlock_irqrestore(&rxq->lock, flags);
207
ab697a9f
EG
208 iwlagn_rx_replenish(priv);
209
210 iwl_trans_rx_hw_init(priv, rxq);
211
212 spin_lock_irqsave(&priv->lock, flags);
213 rxq->need_update = 1;
214 iwl_rx_queue_update_write_ptr(priv, rxq);
215 spin_unlock_irqrestore(&priv->lock, flags);
216
c85eb619
EG
217 return 0;
218}
219
a0f6b0a2
EG
220static void iwl_trans_rx_free(struct iwl_priv *priv)
221{
222 struct iwl_rx_queue *rxq = &priv->rxq;
223 unsigned long flags;
224
225 /*if rxq->bd is NULL, it means that nothing has been allocated,
226 * exit now */
227 if (!rxq->bd) {
228 IWL_DEBUG_INFO(priv, "Free NULL rx context\n");
229 return;
230 }
231
232 spin_lock_irqsave(&rxq->lock, flags);
233 iwl_trans_rxq_free_rx_bufs(priv);
234 spin_unlock_irqrestore(&rxq->lock, flags);
235
236 dma_free_coherent(priv->bus.dev, sizeof(__le32) * RX_QUEUE_SIZE,
237 rxq->bd, rxq->bd_dma);
238 memset(&rxq->bd_dma, 0, sizeof(rxq->bd_dma));
239 rxq->bd = NULL;
240
241 if (rxq->rb_stts)
242 dma_free_coherent(priv->bus.dev,
243 sizeof(struct iwl_rb_status),
244 rxq->rb_stts, rxq->rb_stts_dma);
245 else
246 IWL_DEBUG_INFO(priv, "Free rxq->rb_stts which is NULL\n");
247 memset(&rxq->rb_stts_dma, 0, sizeof(rxq->rb_stts_dma));
248 rxq->rb_stts = NULL;
249}
250
c2c52e8b
EG
251static int iwl_trans_rx_stop(struct iwl_priv *priv)
252{
253
254 /* stop Rx DMA */
255 iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
256 return iwl_poll_direct_bit(priv, FH_MEM_RSSR_RX_STATUS_REG,
257 FH_RSSR_CHNL0_RX_STATUS_CHNL_IDLE, 1000);
258}
259
02aca585
EG
260static inline int iwlagn_alloc_dma_ptr(struct iwl_priv *priv,
261 struct iwl_dma_ptr *ptr, size_t size)
262{
263 if (WARN_ON(ptr->addr))
264 return -EINVAL;
265
266 ptr->addr = dma_alloc_coherent(priv->bus.dev, size,
267 &ptr->dma, GFP_KERNEL);
268 if (!ptr->addr)
269 return -ENOMEM;
270 ptr->size = size;
271 return 0;
272}
273
1359ca4f
EG
274static inline void iwlagn_free_dma_ptr(struct iwl_priv *priv,
275 struct iwl_dma_ptr *ptr)
276{
277 if (unlikely(!ptr->addr))
278 return;
279
280 dma_free_coherent(priv->bus.dev, ptr->size, ptr->addr, ptr->dma);
281 memset(ptr, 0, sizeof(*ptr));
282}
283
02aca585
EG
284static int iwl_trans_txq_alloc(struct iwl_priv *priv, struct iwl_tx_queue *txq,
285 int slots_num, u32 txq_id)
286{
287 size_t tfd_sz = priv->hw_params.tfd_size * TFD_QUEUE_SIZE_MAX;
288 int i;
289
290 if (WARN_ON(txq->meta || txq->cmd || txq->txb || txq->tfds))
291 return -EINVAL;
292
1359ca4f
EG
293 txq->q.n_window = slots_num;
294
02aca585
EG
295 txq->meta = kzalloc(sizeof(txq->meta[0]) * slots_num,
296 GFP_KERNEL);
297 txq->cmd = kzalloc(sizeof(txq->cmd[0]) * slots_num,
298 GFP_KERNEL);
299
300 if (!txq->meta || !txq->cmd)
301 goto error;
302
303 for (i = 0; i < slots_num; i++) {
304 txq->cmd[i] = kmalloc(sizeof(struct iwl_device_cmd),
305 GFP_KERNEL);
306 if (!txq->cmd[i])
307 goto error;
308 }
309
310 /* Alloc driver data array and TFD circular buffer */
311 /* Driver private data, only for Tx (not command) queues,
312 * not shared with device. */
313 if (txq_id != priv->cmd_queue) {
314 txq->txb = kzalloc(sizeof(txq->txb[0]) *
315 TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
316 if (!txq->txb) {
317 IWL_ERR(priv, "kmalloc for auxiliary BD "
318 "structures failed\n");
319 goto error;
320 }
321 } else {
322 txq->txb = NULL;
323 }
324
325 /* Circular buffer of transmit frame descriptors (TFDs),
326 * shared with device */
327 txq->tfds = dma_alloc_coherent(priv->bus.dev, tfd_sz, &txq->q.dma_addr,
328 GFP_KERNEL);
329 if (!txq->tfds) {
330 IWL_ERR(priv, "dma_alloc_coherent(%zd) failed\n", tfd_sz);
331 goto error;
332 }
333 txq->q.id = txq_id;
334
335 return 0;
336error:
337 kfree(txq->txb);
338 txq->txb = NULL;
339 /* since txq->cmd has been zeroed,
340 * all non allocated cmd[i] will be NULL */
341 if (txq->cmd)
342 for (i = 0; i < slots_num; i++)
343 kfree(txq->cmd[i]);
344 kfree(txq->meta);
345 kfree(txq->cmd);
346 txq->meta = NULL;
347 txq->cmd = NULL;
348
349 return -ENOMEM;
350
351}
352
353static int iwl_trans_txq_init(struct iwl_priv *priv, struct iwl_tx_queue *txq,
354 int slots_num, u32 txq_id)
355{
356 int ret;
357
358 txq->need_update = 0;
359 memset(txq->meta, 0, sizeof(txq->meta[0]) * slots_num);
360
361 /*
362 * For the default queues 0-3, set up the swq_id
363 * already -- all others need to get one later
364 * (if they need one at all).
365 */
366 if (txq_id < 4)
367 iwl_set_swq_id(txq, txq_id, txq_id);
368
369 /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
370 * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
371 BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
372
373 /* Initialize queue's high/low-water marks, and head/tail indexes */
374 ret = iwl_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num,
375 txq_id);
376 if (ret)
377 return ret;
378
379 /*
380 * Tell nic where to find circular buffer of Tx Frame Descriptors for
381 * given Tx queue, and enable the DMA channel used for that queue.
382 * Circular buffer (TFD queue in DRAM) physical base address */
383 iwl_write_direct32(priv, FH_MEM_CBBC_QUEUE(txq_id),
384 txq->q.dma_addr >> 8);
385
386 return 0;
387}
388
c170b867
EG
389/**
390 * iwl_tx_queue_unmap - Unmap any remaining DMA mappings and free skb's
391 */
392static void iwl_tx_queue_unmap(struct iwl_priv *priv, int txq_id)
393{
394 struct iwl_tx_queue *txq = &priv->txq[txq_id];
395 struct iwl_queue *q = &txq->q;
396
397 if (!q->n_bd)
398 return;
399
400 while (q->write_ptr != q->read_ptr) {
401 /* The read_ptr needs to bound by q->n_window */
402 iwlagn_txq_free_tfd(priv, txq, get_cmd_index(q, q->read_ptr));
403 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd);
404 }
405}
406
1359ca4f
EG
407/**
408 * iwl_tx_queue_free - Deallocate DMA queue.
409 * @txq: Transmit queue to deallocate.
410 *
411 * Empty queue by removing and destroying all BD's.
412 * Free all buffers.
413 * 0-fill, but do not free "txq" descriptor structure.
414 */
415static void iwl_tx_queue_free(struct iwl_priv *priv, int txq_id)
416{
417 struct iwl_tx_queue *txq = &priv->txq[txq_id];
418 struct device *dev = priv->bus.dev;
419 int i;
420 if (WARN_ON(!txq))
421 return;
422
423 iwl_tx_queue_unmap(priv, txq_id);
424
425 /* De-alloc array of command/tx buffers */
426 for (i = 0; i < txq->q.n_window; i++)
427 kfree(txq->cmd[i]);
428
429 /* De-alloc circular buffer of TFDs */
430 if (txq->q.n_bd) {
431 dma_free_coherent(dev, priv->hw_params.tfd_size *
432 txq->q.n_bd, txq->tfds, txq->q.dma_addr);
433 memset(&txq->q.dma_addr, 0, sizeof(txq->q.dma_addr));
434 }
435
436 /* De-alloc array of per-TFD driver data */
437 kfree(txq->txb);
438 txq->txb = NULL;
439
440 /* deallocate arrays */
441 kfree(txq->cmd);
442 kfree(txq->meta);
443 txq->cmd = NULL;
444 txq->meta = NULL;
445
446 /* 0-fill queue descriptor structure */
447 memset(txq, 0, sizeof(*txq));
448}
449
450/**
451 * iwl_trans_tx_free - Free TXQ Context
452 *
453 * Destroy all TX DMA queues and structures
454 */
455static void iwl_trans_tx_free(struct iwl_priv *priv)
456{
457 int txq_id;
458
459 /* Tx queues */
460 if (priv->txq) {
461 for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++)
462 iwl_tx_queue_free(priv, txq_id);
463 }
464
465 kfree(priv->txq);
466 priv->txq = NULL;
467
468 iwlagn_free_dma_ptr(priv, &priv->kw);
469
470 iwlagn_free_dma_ptr(priv, &priv->scd_bc_tbls);
471}
472
02aca585
EG
473/**
474 * iwl_trans_tx_alloc - allocate TX context
475 * Allocate all Tx DMA structures and initialize them
476 *
477 * @param priv
478 * @return error code
479 */
480static int iwl_trans_tx_alloc(struct iwl_priv *priv)
481{
482 int ret;
483 int txq_id, slots_num;
484
485 /*It is not allowed to alloc twice, so warn when this happens.
486 * We cannot rely on the previous allocation, so free and fail */
487 if (WARN_ON(priv->txq)) {
488 ret = -EINVAL;
489 goto error;
490 }
491
492 ret = iwlagn_alloc_dma_ptr(priv, &priv->scd_bc_tbls,
493 priv->hw_params.scd_bc_tbls_size);
494 if (ret) {
495 IWL_ERR(priv, "Scheduler BC Table allocation failed\n");
496 goto error;
497 }
498
499 /* Alloc keep-warm buffer */
500 ret = iwlagn_alloc_dma_ptr(priv, &priv->kw, IWL_KW_SIZE);
501 if (ret) {
502 IWL_ERR(priv, "Keep Warm allocation failed\n");
503 goto error;
504 }
505
506 priv->txq = kzalloc(sizeof(struct iwl_tx_queue) *
507 priv->cfg->base_params->num_of_queues, GFP_KERNEL);
508 if (!priv->txq) {
509 IWL_ERR(priv, "Not enough memory for txq\n");
510 ret = ENOMEM;
511 goto error;
512 }
513
514 /* Alloc and init all Tx queues, including the command queue (#4/#9) */
515 for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++) {
516 slots_num = (txq_id == priv->cmd_queue) ?
517 TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
518 ret = iwl_trans_txq_alloc(priv, &priv->txq[txq_id], slots_num,
519 txq_id);
520 if (ret) {
521 IWL_ERR(priv, "Tx %d queue alloc failed\n", txq_id);
522 goto error;
523 }
524 }
525
526 return 0;
527
528error:
bdfbf092 529 trans_tx_free(priv);
02aca585
EG
530
531 return ret;
532}
533static int iwl_trans_tx_init(struct iwl_priv *priv)
534{
535 int ret;
536 int txq_id, slots_num;
537 unsigned long flags;
538 bool alloc = false;
539
540 if (!priv->txq) {
541 ret = iwl_trans_tx_alloc(priv);
542 if (ret)
543 goto error;
544 alloc = true;
545 }
546
547 spin_lock_irqsave(&priv->lock, flags);
548
549 /* Turn off all Tx DMA fifos */
550 iwl_write_prph(priv, IWLAGN_SCD_TXFACT, 0);
551
552 /* Tell NIC where to find the "keep warm" buffer */
553 iwl_write_direct32(priv, FH_KW_MEM_ADDR_REG, priv->kw.dma >> 4);
554
555 spin_unlock_irqrestore(&priv->lock, flags);
556
557 /* Alloc and init all Tx queues, including the command queue (#4/#9) */
558 for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++) {
559 slots_num = (txq_id == priv->cmd_queue) ?
560 TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
561 ret = iwl_trans_txq_init(priv, &priv->txq[txq_id], slots_num,
562 txq_id);
563 if (ret) {
564 IWL_ERR(priv, "Tx %d queue init failed\n", txq_id);
565 goto error;
566 }
567 }
568
569 return 0;
570error:
571 /*Upon error, free only if we allocated something */
572 if (alloc)
bdfbf092 573 trans_tx_free(priv);
02aca585
EG
574 return ret;
575}
576
c170b867
EG
577/**
578 * iwlagn_txq_ctx_stop - Stop all Tx DMA channels
579 */
580static int iwl_trans_tx_stop(struct iwl_priv *priv)
581{
582 int ch, txq_id;
583 unsigned long flags;
584
585 /* Turn off all Tx DMA fifos */
586 spin_lock_irqsave(&priv->lock, flags);
587
588 iwlagn_txq_set_sched(priv, 0);
589
590 /* Stop each Tx DMA channel, and wait for it to be idle */
02f6f659 591 for (ch = 0; ch < FH_TCSR_CHNL_NUM; ch++) {
c170b867
EG
592 iwl_write_direct32(priv, FH_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0);
593 if (iwl_poll_direct_bit(priv, FH_TSSR_TX_STATUS_REG,
594 FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch),
595 1000))
596 IWL_ERR(priv, "Failing on timeout while stopping"
597 " DMA channel %d [0x%08x]", ch,
598 iwl_read_direct32(priv, FH_TSSR_TX_STATUS_REG));
599 }
600 spin_unlock_irqrestore(&priv->lock, flags);
601
602 if (!priv->txq) {
603 IWL_WARN(priv, "Stopping tx queues that aren't allocated...");
604 return 0;
605 }
606
607 /* Unmap DMA from host system and free skb's */
608 for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++)
609 iwl_tx_queue_unmap(priv, txq_id);
610
611 return 0;
612}
613
47c1b496
EG
614static struct iwl_tx_cmd *iwl_trans_get_tx_cmd(struct iwl_priv *priv,
615 int txq_id)
616{
617 struct iwl_tx_queue *txq = &priv->txq[txq_id];
618 struct iwl_queue *q = &txq->q;
619 struct iwl_device_cmd *dev_cmd;
620
621 if (unlikely(iwl_queue_space(q) < q->high_mark))
622 return NULL;
623
624 /*
625 * Set up the Tx-command (not MAC!) header.
626 * Store the chosen Tx queue and TFD index within the sequence field;
627 * after Tx, uCode's Tx response will return this value so driver can
628 * locate the frame within the tx queue and do post-tx processing.
629 */
630 dev_cmd = txq->cmd[q->write_ptr];
631 memset(dev_cmd, 0, sizeof(*dev_cmd));
632 dev_cmd->hdr.cmd = REPLY_TX;
633 dev_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
634 INDEX_TO_SEQ(q->write_ptr)));
635 return &dev_cmd->cmd.tx;
636}
637
638static int iwl_trans_tx(struct iwl_priv *priv, struct sk_buff *skb,
639 struct iwl_tx_cmd *tx_cmd, int txq_id, __le16 fc, bool ampdu,
640 struct iwl_rxon_context *ctx)
641{
642 struct iwl_tx_queue *txq = &priv->txq[txq_id];
643 struct iwl_queue *q = &txq->q;
644 struct iwl_device_cmd *dev_cmd = txq->cmd[q->write_ptr];
645 struct iwl_cmd_meta *out_meta;
646
647 dma_addr_t phys_addr = 0;
648 dma_addr_t txcmd_phys;
649 dma_addr_t scratch_phys;
650 u16 len, firstlen, secondlen;
651 u8 wait_write_ptr = 0;
652 u8 hdr_len = ieee80211_hdrlen(fc);
653
654 /* Set up driver data for this TFD */
655 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
656 txq->txb[q->write_ptr].skb = skb;
657 txq->txb[q->write_ptr].ctx = ctx;
658
659 /* Set up first empty entry in queue's array of Tx/cmd buffers */
660 out_meta = &txq->meta[q->write_ptr];
661
662 /*
663 * Use the first empty entry in this queue's command buffer array
664 * to contain the Tx command and MAC header concatenated together
665 * (payload data will be in another buffer).
666 * Size of this varies, due to varying MAC header length.
667 * If end is not dword aligned, we'll have 2 extra bytes at the end
668 * of the MAC header (device reads on dword boundaries).
669 * We'll tell device about this padding later.
670 */
671 len = sizeof(struct iwl_tx_cmd) +
672 sizeof(struct iwl_cmd_header) + hdr_len;
673 firstlen = (len + 3) & ~3;
674
675 /* Tell NIC about any 2-byte padding after MAC header */
676 if (firstlen != len)
677 tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
678
679 /* Physical address of this Tx command's header (not MAC header!),
680 * within command buffer array. */
681 txcmd_phys = dma_map_single(priv->bus.dev,
682 &dev_cmd->hdr, firstlen,
683 DMA_BIDIRECTIONAL);
684 if (unlikely(dma_mapping_error(priv->bus.dev, txcmd_phys)))
685 return -1;
686 dma_unmap_addr_set(out_meta, mapping, txcmd_phys);
687 dma_unmap_len_set(out_meta, len, firstlen);
688
689 if (!ieee80211_has_morefrags(fc)) {
690 txq->need_update = 1;
691 } else {
692 wait_write_ptr = 1;
693 txq->need_update = 0;
694 }
695
696 /* Set up TFD's 2nd entry to point directly to remainder of skb,
697 * if any (802.11 null frames have no payload). */
698 secondlen = skb->len - hdr_len;
699 if (secondlen > 0) {
700 phys_addr = dma_map_single(priv->bus.dev, skb->data + hdr_len,
701 secondlen, DMA_TO_DEVICE);
702 if (unlikely(dma_mapping_error(priv->bus.dev, phys_addr))) {
703 dma_unmap_single(priv->bus.dev,
704 dma_unmap_addr(out_meta, mapping),
705 dma_unmap_len(out_meta, len),
706 DMA_BIDIRECTIONAL);
707 return -1;
708 }
709 }
710
711 /* Attach buffers to TFD */
712 iwlagn_txq_attach_buf_to_tfd(priv, txq, txcmd_phys, firstlen, 1);
713 if (secondlen > 0)
714 iwlagn_txq_attach_buf_to_tfd(priv, txq, phys_addr,
715 secondlen, 0);
716
717 scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
718 offsetof(struct iwl_tx_cmd, scratch);
719
720 /* take back ownership of DMA buffer to enable update */
721 dma_sync_single_for_cpu(priv->bus.dev, txcmd_phys, firstlen,
722 DMA_BIDIRECTIONAL);
723 tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
724 tx_cmd->dram_msb_ptr = iwl_get_dma_hi_addr(scratch_phys);
725
726 IWL_DEBUG_TX(priv, "sequence nr = 0X%x\n",
727 le16_to_cpu(dev_cmd->hdr.sequence));
728 IWL_DEBUG_TX(priv, "tx_flags = 0X%x\n", le32_to_cpu(tx_cmd->tx_flags));
729 iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx_cmd, sizeof(*tx_cmd));
730 iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx_cmd->hdr, hdr_len);
731
732 /* Set up entry for this TFD in Tx byte-count array */
733 if (ampdu)
734 iwlagn_txq_update_byte_cnt_tbl(priv, txq,
735 le16_to_cpu(tx_cmd->len));
736
737 dma_sync_single_for_device(priv->bus.dev, txcmd_phys, firstlen,
738 DMA_BIDIRECTIONAL);
739
740 trace_iwlwifi_dev_tx(priv,
741 &((struct iwl_tfd *)txq->tfds)[txq->q.write_ptr],
742 sizeof(struct iwl_tfd),
743 &dev_cmd->hdr, firstlen,
744 skb->data + hdr_len, secondlen);
745
746 /* Tell device the write index *just past* this latest filled TFD */
747 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
748 iwl_txq_update_write_ptr(priv, txq);
749
750 /*
751 * At this point the frame is "transmitted" successfully
752 * and we will get a TX status notification eventually,
753 * regardless of the value of ret. "ret" only indicates
754 * whether or not we should update the write pointer.
755 */
756 if ((iwl_queue_space(q) < q->high_mark) && priv->mac80211_registered) {
757 if (wait_write_ptr) {
758 txq->need_update = 1;
759 iwl_txq_update_write_ptr(priv, txq);
760 } else {
761 iwl_stop_queue(priv, txq);
762 }
763 }
764 return 0;
765}
766
a27367d2
EG
767static void iwl_trans_sync_irq(struct iwl_priv *priv)
768{
769 /* wait to make sure we flush pending tasklet*/
770 synchronize_irq(priv->bus.irq);
771 tasklet_kill(&priv->irq_tasklet);
772}
773
34c1b7ba
EG
774static void iwl_trans_free(struct iwl_priv *priv)
775{
776 free_irq(priv->bus.irq, priv);
777 iwl_free_isr_ict(priv);
778}
779
c85eb619
EG
780static const struct iwl_trans_ops trans_ops = {
781 .rx_init = iwl_trans_rx_init,
c2c52e8b 782 .rx_stop = iwl_trans_rx_stop,
a0f6b0a2 783 .rx_free = iwl_trans_rx_free,
02aca585
EG
784
785 .tx_init = iwl_trans_tx_init,
c170b867 786 .tx_stop = iwl_trans_tx_stop,
1359ca4f 787 .tx_free = iwl_trans_tx_free,
e419d62d
EG
788
789 .send_cmd = iwl_send_cmd,
790 .send_cmd_pdu = iwl_send_cmd_pdu,
47c1b496
EG
791
792 .get_tx_cmd = iwl_trans_get_tx_cmd,
793 .tx = iwl_trans_tx,
34c1b7ba 794
a27367d2 795 .sync_irq = iwl_trans_sync_irq,
34c1b7ba 796 .free = iwl_trans_free,
c85eb619
EG
797};
798
34c1b7ba 799int iwl_trans_register(struct iwl_priv *priv)
c85eb619 800{
34c1b7ba
EG
801 int err;
802
803 priv->trans.ops = &trans_ops;
804
805 iwl_alloc_isr_ict(priv);
806
807 err = request_irq(priv->bus.irq, iwl_isr_ict, IRQF_SHARED,
808 DRV_NAME, priv);
809 if (err) {
810 IWL_ERR(priv, "Error allocating IRQ %d\n", priv->bus.irq);
811 iwl_free_isr_ict(priv);
812 return err;
813 }
814
815 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
816 iwl_irq_tasklet, (unsigned long)priv);
817
ab697a9f
EG
818 INIT_WORK(&priv->rx_replenish, iwl_bg_rx_replenish);
819
34c1b7ba 820 return 0;
c85eb619 821}