Linux 3.8-rc1
[deliverable/linux.git] / drivers / net / wireless / iwlwifi / pcie / tx.c
CommitLineData
1053d35f
RR
1/******************************************************************************
2 *
fb4961db 3 * Copyright(c) 2003 - 2012 Intel Corporation. All rights reserved.
1053d35f
RR
4 *
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portions of the ieee80211 subsystem header files.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20 *
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
23 *
24 * Contact Information:
759ef89f 25 * Intel Linux Wireless <ilw@linux.intel.com>
1053d35f
RR
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *
28 *****************************************************************************/
fd4abac5 29#include <linux/etherdevice.h>
5a0e3ad6 30#include <linux/slab.h>
253a634c 31#include <linux/sched.h>
253a634c 32
522376d2
EG
33#include "iwl-debug.h"
34#include "iwl-csr.h"
35#include "iwl-prph.h"
1053d35f 36#include "iwl-io.h"
ed277c93 37#include "iwl-op-mode.h"
6468a01a 38#include "internal.h"
6238b008 39/* FIXME: need to abstract out TX command (once we know what it looks like) */
1023fdc4 40#include "dvm/commands.h"
1053d35f 41
522376d2
EG
42#define IWL_TX_CRC_SIZE 4
43#define IWL_TX_DELIMITER_SIZE 4
44
f02831be
EG
45/*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
46 * DMA services
47 *
48 * Theory of operation
49 *
50 * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
51 * of buffer descriptors, each of which points to one or more data buffers for
52 * the device to read from or fill. Driver and device exchange status of each
53 * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
54 * entries in each circular buffer, to protect against confusing empty and full
55 * queue states.
56 *
57 * The device reads or writes the data in the queues via the device's several
58 * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
59 *
60 * For Tx queue, there are low mark and high mark limits. If, after queuing
61 * the packet for Tx, free space become < low mark, Tx queue stopped. When
62 * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
63 * Tx queue resumed.
64 *
65 ***************************************************/
66static int iwl_queue_space(const struct iwl_queue *q)
67{
68 int s = q->read_ptr - q->write_ptr;
69
70 if (q->read_ptr > q->write_ptr)
71 s -= q->n_bd;
72
73 if (s <= 0)
74 s += q->n_window;
75 /* keep some reserve to not confuse empty and full situations */
76 s -= 2;
77 if (s < 0)
78 s = 0;
79 return s;
80}
81
82/*
83 * iwl_queue_init - Initialize queue's high/low-water and read/write indexes
84 */
85static int iwl_queue_init(struct iwl_queue *q, int count, int slots_num, u32 id)
86{
87 q->n_bd = count;
88 q->n_window = slots_num;
89 q->id = id;
90
91 /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
92 * and iwl_queue_dec_wrap are broken. */
93 if (WARN_ON(!is_power_of_2(count)))
94 return -EINVAL;
95
96 /* slots_num must be power-of-two size, otherwise
97 * get_cmd_index is broken. */
98 if (WARN_ON(!is_power_of_2(slots_num)))
99 return -EINVAL;
100
101 q->low_mark = q->n_window / 4;
102 if (q->low_mark < 4)
103 q->low_mark = 4;
104
105 q->high_mark = q->n_window / 8;
106 if (q->high_mark < 2)
107 q->high_mark = 2;
108
109 q->write_ptr = 0;
110 q->read_ptr = 0;
111
112 return 0;
113}
114
f02831be
EG
115static int iwl_pcie_alloc_dma_ptr(struct iwl_trans *trans,
116 struct iwl_dma_ptr *ptr, size_t size)
117{
118 if (WARN_ON(ptr->addr))
119 return -EINVAL;
120
121 ptr->addr = dma_alloc_coherent(trans->dev, size,
122 &ptr->dma, GFP_KERNEL);
123 if (!ptr->addr)
124 return -ENOMEM;
125 ptr->size = size;
126 return 0;
127}
128
129static void iwl_pcie_free_dma_ptr(struct iwl_trans *trans,
130 struct iwl_dma_ptr *ptr)
131{
132 if (unlikely(!ptr->addr))
133 return;
134
135 dma_free_coherent(trans->dev, ptr->size, ptr->addr, ptr->dma);
136 memset(ptr, 0, sizeof(*ptr));
137}
138
139static void iwl_pcie_txq_stuck_timer(unsigned long data)
140{
141 struct iwl_txq *txq = (void *)data;
142 struct iwl_queue *q = &txq->q;
143 struct iwl_trans_pcie *trans_pcie = txq->trans_pcie;
144 struct iwl_trans *trans = iwl_trans_pcie_get_trans(trans_pcie);
145 u32 scd_sram_addr = trans_pcie->scd_base_addr +
146 SCD_TX_STTS_QUEUE_OFFSET(txq->q.id);
147 u8 buf[16];
148 int i;
149
150 spin_lock(&txq->lock);
151 /* check if triggered erroneously */
152 if (txq->q.read_ptr == txq->q.write_ptr) {
153 spin_unlock(&txq->lock);
154 return;
155 }
156 spin_unlock(&txq->lock);
157
158 IWL_ERR(trans, "Queue %d stuck for %u ms.\n", txq->q.id,
159 jiffies_to_msecs(trans_pcie->wd_timeout));
160 IWL_ERR(trans, "Current SW read_ptr %d write_ptr %d\n",
161 txq->q.read_ptr, txq->q.write_ptr);
162
163 iwl_read_targ_mem_bytes(trans, scd_sram_addr, buf, sizeof(buf));
164
165 iwl_print_hex_error(trans, buf, sizeof(buf));
166
167 for (i = 0; i < FH_TCSR_CHNL_NUM; i++)
168 IWL_ERR(trans, "FH TRBs(%d) = 0x%08x\n", i,
169 iwl_read_direct32(trans, FH_TX_TRB_REG(i)));
170
171 for (i = 0; i < trans->cfg->base_params->num_of_queues; i++) {
172 u32 status = iwl_read_prph(trans, SCD_QUEUE_STATUS_BITS(i));
173 u8 fifo = (status >> SCD_QUEUE_STTS_REG_POS_TXF) & 0x7;
174 bool active = !!(status & BIT(SCD_QUEUE_STTS_REG_POS_ACTIVE));
175 u32 tbl_dw =
176 iwl_read_targ_mem(trans,
177 trans_pcie->scd_base_addr +
178 SCD_TRANS_TBL_OFFSET_QUEUE(i));
179
180 if (i & 0x1)
181 tbl_dw = (tbl_dw & 0xFFFF0000) >> 16;
182 else
183 tbl_dw = tbl_dw & 0x0000FFFF;
184
185 IWL_ERR(trans,
186 "Q %d is %sactive and mapped to fifo %d ra_tid 0x%04x [%d,%d]\n",
187 i, active ? "" : "in", fifo, tbl_dw,
188 iwl_read_prph(trans,
189 SCD_QUEUE_RDPTR(i)) & (txq->q.n_bd - 1),
190 iwl_read_prph(trans, SCD_QUEUE_WRPTR(i)));
191 }
192
193 for (i = q->read_ptr; i != q->write_ptr;
194 i = iwl_queue_inc_wrap(i, q->n_bd)) {
195 struct iwl_tx_cmd *tx_cmd =
196 (struct iwl_tx_cmd *)txq->entries[i].cmd->payload;
197 IWL_ERR(trans, "scratch %d = 0x%08x\n", i,
198 get_unaligned_le32(&tx_cmd->scratch));
199 }
200
201 iwl_op_mode_nic_error(trans->op_mode);
202}
203
990aa6d7
EG
204/*
205 * iwl_pcie_txq_update_byte_cnt_tbl - Set up entry in Tx byte-count array
48d42c42 206 */
f02831be
EG
207static void iwl_pcie_txq_update_byte_cnt_tbl(struct iwl_trans *trans,
208 struct iwl_txq *txq, u16 byte_cnt)
48d42c42 209{
105183b1 210 struct iwlagn_scd_bc_tbl *scd_bc_tbl;
20d3b647 211 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
48d42c42
EG
212 int write_ptr = txq->q.write_ptr;
213 int txq_id = txq->q.id;
214 u8 sec_ctl = 0;
215 u8 sta_id = 0;
216 u16 len = byte_cnt + IWL_TX_CRC_SIZE + IWL_TX_DELIMITER_SIZE;
217 __le16 bc_ent;
132f98c2 218 struct iwl_tx_cmd *tx_cmd =
bf8440e6 219 (void *) txq->entries[txq->q.write_ptr].cmd->payload;
48d42c42 220
105183b1
EG
221 scd_bc_tbl = trans_pcie->scd_bc_tbls.addr;
222
48d42c42
EG
223 WARN_ON(len > 0xFFF || write_ptr >= TFD_QUEUE_SIZE_MAX);
224
132f98c2
EG
225 sta_id = tx_cmd->sta_id;
226 sec_ctl = tx_cmd->sec_ctl;
48d42c42
EG
227
228 switch (sec_ctl & TX_CMD_SEC_MSK) {
229 case TX_CMD_SEC_CCM:
230 len += CCMP_MIC_LEN;
231 break;
232 case TX_CMD_SEC_TKIP:
233 len += TKIP_ICV_LEN;
234 break;
235 case TX_CMD_SEC_WEP:
236 len += WEP_IV_LEN + WEP_ICV_LEN;
237 break;
238 }
239
240 bc_ent = cpu_to_le16((len & 0xFFF) | (sta_id << 12));
241
242 scd_bc_tbl[txq_id].tfd_offset[write_ptr] = bc_ent;
243
244 if (write_ptr < TFD_QUEUE_SIZE_BC_DUP)
245 scd_bc_tbl[txq_id].
246 tfd_offset[TFD_QUEUE_SIZE_MAX + write_ptr] = bc_ent;
247}
248
f02831be
EG
249static void iwl_pcie_txq_inval_byte_cnt_tbl(struct iwl_trans *trans,
250 struct iwl_txq *txq)
251{
252 struct iwl_trans_pcie *trans_pcie =
253 IWL_TRANS_GET_PCIE_TRANS(trans);
254 struct iwlagn_scd_bc_tbl *scd_bc_tbl = trans_pcie->scd_bc_tbls.addr;
255 int txq_id = txq->q.id;
256 int read_ptr = txq->q.read_ptr;
257 u8 sta_id = 0;
258 __le16 bc_ent;
259 struct iwl_tx_cmd *tx_cmd =
260 (void *)txq->entries[txq->q.read_ptr].cmd->payload;
261
262 WARN_ON(read_ptr >= TFD_QUEUE_SIZE_MAX);
263
264 if (txq_id != trans_pcie->cmd_queue)
265 sta_id = tx_cmd->sta_id;
266
267 bc_ent = cpu_to_le16(1 | (sta_id << 12));
268 scd_bc_tbl[txq_id].tfd_offset[read_ptr] = bc_ent;
269
270 if (read_ptr < TFD_QUEUE_SIZE_BC_DUP)
271 scd_bc_tbl[txq_id].
272 tfd_offset[TFD_QUEUE_SIZE_MAX + read_ptr] = bc_ent;
273}
274
990aa6d7
EG
275/*
276 * iwl_pcie_txq_inc_wr_ptr - Send new write index to hardware
fd4abac5 277 */
990aa6d7 278void iwl_pcie_txq_inc_wr_ptr(struct iwl_trans *trans, struct iwl_txq *txq)
fd4abac5
TW
279{
280 u32 reg = 0;
fd4abac5
TW
281 int txq_id = txq->q.id;
282
283 if (txq->need_update == 0)
7bfedc59 284 return;
fd4abac5 285
035f7ff2 286 if (trans->cfg->base_params->shadow_reg_enable) {
f81c1f48 287 /* shadow register enabled */
1042db2a 288 iwl_write32(trans, HBUS_TARG_WRPTR,
f81c1f48
WYG
289 txq->q.write_ptr | (txq_id << 8));
290 } else {
47107e84
DF
291 struct iwl_trans_pcie *trans_pcie =
292 IWL_TRANS_GET_PCIE_TRANS(trans);
f81c1f48 293 /* if we're trying to save power */
01d651d4 294 if (test_bit(STATUS_TPOWER_PMI, &trans_pcie->status)) {
f81c1f48
WYG
295 /* wake up nic if it's powered down ...
296 * uCode will wake up, and interrupt us again, so next
297 * time we'll skip this part. */
1042db2a 298 reg = iwl_read32(trans, CSR_UCODE_DRV_GP1);
fd4abac5 299
f81c1f48 300 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
fd656935 301 IWL_DEBUG_INFO(trans,
f81c1f48
WYG
302 "Tx queue %d requesting wakeup,"
303 " GP1 = 0x%x\n", txq_id, reg);
1042db2a 304 iwl_set_bit(trans, CSR_GP_CNTRL,
f81c1f48
WYG
305 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
306 return;
307 }
fd4abac5 308
1042db2a 309 iwl_write_direct32(trans, HBUS_TARG_WRPTR,
fd4abac5 310 txq->q.write_ptr | (txq_id << 8));
fd4abac5 311
f81c1f48
WYG
312 /*
313 * else not in power-save mode,
314 * uCode will never sleep when we're
315 * trying to tx (during RFKILL, we're not trying to tx).
316 */
317 } else
1042db2a 318 iwl_write32(trans, HBUS_TARG_WRPTR,
f81c1f48
WYG
319 txq->q.write_ptr | (txq_id << 8));
320 }
fd4abac5 321 txq->need_update = 0;
fd4abac5 322}
fd4abac5 323
f02831be 324static inline dma_addr_t iwl_pcie_tfd_tb_get_addr(struct iwl_tfd *tfd, u8 idx)
214d14d4
JB
325{
326 struct iwl_tfd_tb *tb = &tfd->tbs[idx];
327
328 dma_addr_t addr = get_unaligned_le32(&tb->lo);
329 if (sizeof(dma_addr_t) > sizeof(u32))
330 addr |=
331 ((dma_addr_t)(le16_to_cpu(tb->hi_n_len) & 0xF) << 16) << 16;
332
333 return addr;
334}
335
f02831be 336static inline u16 iwl_pcie_tfd_tb_get_len(struct iwl_tfd *tfd, u8 idx)
214d14d4
JB
337{
338 struct iwl_tfd_tb *tb = &tfd->tbs[idx];
339
340 return le16_to_cpu(tb->hi_n_len) >> 4;
341}
342
f02831be
EG
343static inline void iwl_pcie_tfd_set_tb(struct iwl_tfd *tfd, u8 idx,
344 dma_addr_t addr, u16 len)
214d14d4
JB
345{
346 struct iwl_tfd_tb *tb = &tfd->tbs[idx];
347 u16 hi_n_len = len << 4;
348
349 put_unaligned_le32(addr, &tb->lo);
350 if (sizeof(dma_addr_t) > sizeof(u32))
351 hi_n_len |= ((addr >> 16) >> 16) & 0xF;
352
353 tb->hi_n_len = cpu_to_le16(hi_n_len);
354
355 tfd->num_tbs = idx + 1;
356}
357
f02831be 358static inline u8 iwl_pcie_tfd_get_num_tbs(struct iwl_tfd *tfd)
214d14d4
JB
359{
360 return tfd->num_tbs & 0x1f;
361}
362
f02831be
EG
363static void iwl_pcie_tfd_unmap(struct iwl_trans *trans,
364 struct iwl_cmd_meta *meta, struct iwl_tfd *tfd,
365 enum dma_data_direction dma_dir)
214d14d4 366{
214d14d4
JB
367 int i;
368 int num_tbs;
369
214d14d4 370 /* Sanity check on number of chunks */
f02831be 371 num_tbs = iwl_pcie_tfd_get_num_tbs(tfd);
214d14d4
JB
372
373 if (num_tbs >= IWL_NUM_OF_TBS) {
6d8f6eeb 374 IWL_ERR(trans, "Too many chunks: %i\n", num_tbs);
214d14d4
JB
375 /* @todo issue fatal error, it is quite serious situation */
376 return;
377 }
378
379 /* Unmap tx_cmd */
380 if (num_tbs)
1042db2a 381 dma_unmap_single(trans->dev,
4ce7cc2b
JB
382 dma_unmap_addr(meta, mapping),
383 dma_unmap_len(meta, len),
795414db 384 DMA_BIDIRECTIONAL);
214d14d4
JB
385
386 /* Unmap chunks, if any. */
387 for (i = 1; i < num_tbs; i++)
f02831be
EG
388 dma_unmap_single(trans->dev, iwl_pcie_tfd_tb_get_addr(tfd, i),
389 iwl_pcie_tfd_tb_get_len(tfd, i), dma_dir);
ebed633c
EG
390
391 tfd->num_tbs = 0;
4ce7cc2b
JB
392}
393
990aa6d7
EG
394/*
395 * iwl_pcie_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr]
6d8f6eeb 396 * @trans - transport private data
4ce7cc2b 397 * @txq - tx queue
ebed633c 398 * @dma_dir - the direction of the DMA mapping
4ce7cc2b
JB
399 *
400 * Does NOT advance any TFD circular buffer read/write indexes
401 * Does NOT free the TFD itself (which is within circular buffer)
402 */
f02831be
EG
403static void iwl_pcie_txq_free_tfd(struct iwl_trans *trans, struct iwl_txq *txq,
404 enum dma_data_direction dma_dir)
4ce7cc2b
JB
405{
406 struct iwl_tfd *tfd_tmp = txq->tfds;
4ce7cc2b 407
ebed633c
EG
408 /* rd_ptr is bounded by n_bd and idx is bounded by n_window */
409 int rd_ptr = txq->q.read_ptr;
410 int idx = get_cmd_index(&txq->q, rd_ptr);
411
015c15e1
JB
412 lockdep_assert_held(&txq->lock);
413
ebed633c 414 /* We have only q->n_window txq->entries, but we use q->n_bd tfds */
f02831be
EG
415 iwl_pcie_tfd_unmap(trans, &txq->entries[idx].meta, &tfd_tmp[rd_ptr],
416 dma_dir);
214d14d4
JB
417
418 /* free SKB */
bf8440e6 419 if (txq->entries) {
214d14d4
JB
420 struct sk_buff *skb;
421
ebed633c 422 skb = txq->entries[idx].skb;
214d14d4 423
909e9b23
EG
424 /* Can be called from irqs-disabled context
425 * If skb is not NULL, it means that the whole queue is being
426 * freed and that the queue is not empty - free the skb
427 */
214d14d4 428 if (skb) {
ed277c93 429 iwl_op_mode_free_skb(trans->op_mode, skb);
ebed633c 430 txq->entries[idx].skb = NULL;
214d14d4
JB
431 }
432 }
433}
434
f02831be
EG
435static int iwl_pcie_txq_build_tfd(struct iwl_trans *trans, struct iwl_txq *txq,
436 dma_addr_t addr, u16 len, u8 reset)
214d14d4
JB
437{
438 struct iwl_queue *q;
439 struct iwl_tfd *tfd, *tfd_tmp;
440 u32 num_tbs;
441
442 q = &txq->q;
4ce7cc2b 443 tfd_tmp = txq->tfds;
214d14d4
JB
444 tfd = &tfd_tmp[q->write_ptr];
445
f02831be
EG
446 if (reset)
447 memset(tfd, 0, sizeof(*tfd));
448
449 num_tbs = iwl_pcie_tfd_get_num_tbs(tfd);
450
451 /* Each TFD can point to a maximum 20 Tx buffers */
452 if (num_tbs >= IWL_NUM_OF_TBS) {
453 IWL_ERR(trans, "Error can not send more than %d chunks\n",
454 IWL_NUM_OF_TBS);
455 return -EINVAL;
456 }
457
458 if (WARN_ON(addr & ~DMA_BIT_MASK(36)))
459 return -EINVAL;
460
461 if (unlikely(addr & ~IWL_TX_DMA_MASK))
462 IWL_ERR(trans, "Unaligned address = %llx\n",
463 (unsigned long long)addr);
464
465 iwl_pcie_tfd_set_tb(tfd, num_tbs, addr, len);
466
467 return 0;
468}
469
470static int iwl_pcie_txq_alloc(struct iwl_trans *trans,
471 struct iwl_txq *txq, int slots_num,
472 u32 txq_id)
473{
474 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
475 size_t tfd_sz = sizeof(struct iwl_tfd) * TFD_QUEUE_SIZE_MAX;
476 int i;
477
478 if (WARN_ON(txq->entries || txq->tfds))
479 return -EINVAL;
480
481 setup_timer(&txq->stuck_timer, iwl_pcie_txq_stuck_timer,
482 (unsigned long)txq);
483 txq->trans_pcie = trans_pcie;
484
485 txq->q.n_window = slots_num;
486
487 txq->entries = kcalloc(slots_num,
488 sizeof(struct iwl_pcie_txq_entry),
489 GFP_KERNEL);
490
491 if (!txq->entries)
492 goto error;
493
494 if (txq_id == trans_pcie->cmd_queue)
495 for (i = 0; i < slots_num; i++) {
496 txq->entries[i].cmd =
497 kmalloc(sizeof(struct iwl_device_cmd),
498 GFP_KERNEL);
499 if (!txq->entries[i].cmd)
500 goto error;
501 }
502
503 /* Circular buffer of transmit frame descriptors (TFDs),
504 * shared with device */
505 txq->tfds = dma_alloc_coherent(trans->dev, tfd_sz,
506 &txq->q.dma_addr, GFP_KERNEL);
507 if (!txq->tfds) {
508 IWL_ERR(trans, "dma_alloc_coherent(%zd) failed\n", tfd_sz);
509 goto error;
510 }
511 txq->q.id = txq_id;
512
513 return 0;
514error:
515 if (txq->entries && txq_id == trans_pcie->cmd_queue)
516 for (i = 0; i < slots_num; i++)
517 kfree(txq->entries[i].cmd);
518 kfree(txq->entries);
519 txq->entries = NULL;
520
521 return -ENOMEM;
522
523}
524
525static int iwl_pcie_txq_init(struct iwl_trans *trans, struct iwl_txq *txq,
526 int slots_num, u32 txq_id)
527{
528 int ret;
529
530 txq->need_update = 0;
531
532 /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
533 * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
534 BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
535
536 /* Initialize queue's high/low-water marks, and head/tail indexes */
537 ret = iwl_queue_init(&txq->q, TFD_QUEUE_SIZE_MAX, slots_num,
538 txq_id);
539 if (ret)
540 return ret;
541
542 spin_lock_init(&txq->lock);
543
544 /*
545 * Tell nic where to find circular buffer of Tx Frame Descriptors for
546 * given Tx queue, and enable the DMA channel used for that queue.
547 * Circular buffer (TFD queue in DRAM) physical base address */
548 iwl_write_direct32(trans, FH_MEM_CBBC_QUEUE(txq_id),
549 txq->q.dma_addr >> 8);
550
551 return 0;
552}
553
554/*
555 * iwl_pcie_txq_unmap - Unmap any remaining DMA mappings and free skb's
556 */
557static void iwl_pcie_txq_unmap(struct iwl_trans *trans, int txq_id)
558{
559 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
560 struct iwl_txq *txq = &trans_pcie->txq[txq_id];
561 struct iwl_queue *q = &txq->q;
562 enum dma_data_direction dma_dir;
563
564 if (!q->n_bd)
565 return;
566
567 /* In the command queue, all the TBs are mapped as BIDI
568 * so unmap them as such.
569 */
570 if (txq_id == trans_pcie->cmd_queue)
571 dma_dir = DMA_BIDIRECTIONAL;
572 else
573 dma_dir = DMA_TO_DEVICE;
574
575 spin_lock_bh(&txq->lock);
576 while (q->write_ptr != q->read_ptr) {
577 iwl_pcie_txq_free_tfd(trans, txq, dma_dir);
578 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd);
579 }
580 spin_unlock_bh(&txq->lock);
581}
582
583/*
584 * iwl_pcie_txq_free - Deallocate DMA queue.
585 * @txq: Transmit queue to deallocate.
586 *
587 * Empty queue by removing and destroying all BD's.
588 * Free all buffers.
589 * 0-fill, but do not free "txq" descriptor structure.
590 */
591static void iwl_pcie_txq_free(struct iwl_trans *trans, int txq_id)
592{
593 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
594 struct iwl_txq *txq = &trans_pcie->txq[txq_id];
595 struct device *dev = trans->dev;
596 int i;
597
598 if (WARN_ON(!txq))
599 return;
600
601 iwl_pcie_txq_unmap(trans, txq_id);
602
603 /* De-alloc array of command/tx buffers */
604 if (txq_id == trans_pcie->cmd_queue)
605 for (i = 0; i < txq->q.n_window; i++) {
606 kfree(txq->entries[i].cmd);
607 kfree(txq->entries[i].copy_cmd);
608 kfree(txq->entries[i].free_buf);
609 }
610
611 /* De-alloc circular buffer of TFDs */
612 if (txq->q.n_bd) {
613 dma_free_coherent(dev, sizeof(struct iwl_tfd) *
614 txq->q.n_bd, txq->tfds, txq->q.dma_addr);
615 memset(&txq->q.dma_addr, 0, sizeof(txq->q.dma_addr));
616 }
617
618 kfree(txq->entries);
619 txq->entries = NULL;
620
621 del_timer_sync(&txq->stuck_timer);
622
623 /* 0-fill queue descriptor structure */
624 memset(txq, 0, sizeof(*txq));
625}
626
627/*
628 * Activate/Deactivate Tx DMA/FIFO channels according tx fifos mask
629 */
630static void iwl_pcie_txq_set_sched(struct iwl_trans *trans, u32 mask)
631{
632 struct iwl_trans_pcie __maybe_unused *trans_pcie =
633 IWL_TRANS_GET_PCIE_TRANS(trans);
634
635 iwl_write_prph(trans, SCD_TXFACT, mask);
636}
637
638void iwl_pcie_tx_start(struct iwl_trans *trans, u32 scd_base_addr)
639{
640 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
641 u32 a;
642 int chan;
643 u32 reg_val;
644
645 /* make sure all queue are not stopped/used */
646 memset(trans_pcie->queue_stopped, 0, sizeof(trans_pcie->queue_stopped));
647 memset(trans_pcie->queue_used, 0, sizeof(trans_pcie->queue_used));
648
649 trans_pcie->scd_base_addr =
650 iwl_read_prph(trans, SCD_SRAM_BASE_ADDR);
651
652 WARN_ON(scd_base_addr != 0 &&
653 scd_base_addr != trans_pcie->scd_base_addr);
654
655 a = trans_pcie->scd_base_addr + SCD_CONTEXT_MEM_LOWER_BOUND;
656 /* reset conext data memory */
657 for (; a < trans_pcie->scd_base_addr + SCD_CONTEXT_MEM_UPPER_BOUND;
658 a += 4)
659 iwl_write_targ_mem(trans, a, 0);
660 /* reset tx status memory */
661 for (; a < trans_pcie->scd_base_addr + SCD_TX_STTS_MEM_UPPER_BOUND;
662 a += 4)
663 iwl_write_targ_mem(trans, a, 0);
664 for (; a < trans_pcie->scd_base_addr +
665 SCD_TRANS_TBL_OFFSET_QUEUE(
666 trans->cfg->base_params->num_of_queues);
667 a += 4)
668 iwl_write_targ_mem(trans, a, 0);
669
670 iwl_write_prph(trans, SCD_DRAM_BASE_ADDR,
671 trans_pcie->scd_bc_tbls.dma >> 10);
672
673 /* The chain extension of the SCD doesn't work well. This feature is
674 * enabled by default by the HW, so we need to disable it manually.
675 */
676 iwl_write_prph(trans, SCD_CHAINEXT_EN, 0);
677
678 iwl_trans_ac_txq_enable(trans, trans_pcie->cmd_queue,
679 trans_pcie->cmd_fifo);
680
681 /* Activate all Tx DMA/FIFO channels */
682 iwl_pcie_txq_set_sched(trans, IWL_MASK(0, 7));
683
684 /* Enable DMA channel */
685 for (chan = 0; chan < FH_TCSR_CHNL_NUM; chan++)
686 iwl_write_direct32(trans, FH_TCSR_CHNL_TX_CONFIG_REG(chan),
687 FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
688 FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE);
689
690 /* Update FH chicken bits */
691 reg_val = iwl_read_direct32(trans, FH_TX_CHICKEN_BITS_REG);
692 iwl_write_direct32(trans, FH_TX_CHICKEN_BITS_REG,
693 reg_val | FH_TX_CHICKEN_BITS_SCD_AUTO_RETRY_EN);
694
695 /* Enable L1-Active */
696 iwl_clear_bits_prph(trans, APMG_PCIDEV_STT_REG,
697 APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
698}
699
700/*
701 * iwl_pcie_tx_stop - Stop all Tx DMA channels
702 */
703int iwl_pcie_tx_stop(struct iwl_trans *trans)
704{
705 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
706 int ch, txq_id, ret;
707 unsigned long flags;
708
709 /* Turn off all Tx DMA fifos */
710 spin_lock_irqsave(&trans_pcie->irq_lock, flags);
711
712 iwl_pcie_txq_set_sched(trans, 0);
713
714 /* Stop each Tx DMA channel, and wait for it to be idle */
715 for (ch = 0; ch < FH_TCSR_CHNL_NUM; ch++) {
716 iwl_write_direct32(trans,
717 FH_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0);
718 ret = iwl_poll_direct_bit(trans, FH_TSSR_TX_STATUS_REG,
719 FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch), 1000);
720 if (ret < 0)
721 IWL_ERR(trans,
722 "Failing on timeout while stopping DMA channel %d [0x%08x]\n",
723 ch,
724 iwl_read_direct32(trans,
725 FH_TSSR_TX_STATUS_REG));
726 }
727 spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
728
729 if (!trans_pcie->txq) {
730 IWL_WARN(trans,
731 "Stopping tx queues that aren't allocated...\n");
732 return 0;
733 }
734
735 /* Unmap DMA from host system and free skb's */
736 for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues;
737 txq_id++)
738 iwl_pcie_txq_unmap(trans, txq_id);
739
740 return 0;
741}
742
743/*
744 * iwl_trans_tx_free - Free TXQ Context
745 *
746 * Destroy all TX DMA queues and structures
747 */
748void iwl_pcie_tx_free(struct iwl_trans *trans)
749{
750 int txq_id;
751 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
752
753 /* Tx queues */
754 if (trans_pcie->txq) {
755 for (txq_id = 0;
756 txq_id < trans->cfg->base_params->num_of_queues; txq_id++)
757 iwl_pcie_txq_free(trans, txq_id);
758 }
759
760 kfree(trans_pcie->txq);
761 trans_pcie->txq = NULL;
762
763 iwl_pcie_free_dma_ptr(trans, &trans_pcie->kw);
764
765 iwl_pcie_free_dma_ptr(trans, &trans_pcie->scd_bc_tbls);
766}
767
768/*
769 * iwl_pcie_tx_alloc - allocate TX context
770 * Allocate all Tx DMA structures and initialize them
771 */
772static int iwl_pcie_tx_alloc(struct iwl_trans *trans)
773{
774 int ret;
775 int txq_id, slots_num;
776 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
777
778 u16 scd_bc_tbls_size = trans->cfg->base_params->num_of_queues *
779 sizeof(struct iwlagn_scd_bc_tbl);
780
781 /*It is not allowed to alloc twice, so warn when this happens.
782 * We cannot rely on the previous allocation, so free and fail */
783 if (WARN_ON(trans_pcie->txq)) {
784 ret = -EINVAL;
785 goto error;
786 }
787
788 ret = iwl_pcie_alloc_dma_ptr(trans, &trans_pcie->scd_bc_tbls,
789 scd_bc_tbls_size);
790 if (ret) {
791 IWL_ERR(trans, "Scheduler BC Table allocation failed\n");
792 goto error;
793 }
794
795 /* Alloc keep-warm buffer */
796 ret = iwl_pcie_alloc_dma_ptr(trans, &trans_pcie->kw, IWL_KW_SIZE);
797 if (ret) {
798 IWL_ERR(trans, "Keep Warm allocation failed\n");
799 goto error;
800 }
801
802 trans_pcie->txq = kcalloc(trans->cfg->base_params->num_of_queues,
803 sizeof(struct iwl_txq), GFP_KERNEL);
804 if (!trans_pcie->txq) {
805 IWL_ERR(trans, "Not enough memory for txq\n");
806 ret = ENOMEM;
807 goto error;
808 }
809
810 /* Alloc and init all Tx queues, including the command queue (#4/#9) */
811 for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues;
812 txq_id++) {
813 slots_num = (txq_id == trans_pcie->cmd_queue) ?
814 TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
815 ret = iwl_pcie_txq_alloc(trans, &trans_pcie->txq[txq_id],
816 slots_num, txq_id);
817 if (ret) {
818 IWL_ERR(trans, "Tx %d queue alloc failed\n", txq_id);
819 goto error;
820 }
821 }
822
823 return 0;
824
825error:
826 iwl_pcie_tx_free(trans);
827
828 return ret;
829}
830int iwl_pcie_tx_init(struct iwl_trans *trans)
831{
832 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
833 int ret;
834 int txq_id, slots_num;
835 unsigned long flags;
836 bool alloc = false;
837
838 if (!trans_pcie->txq) {
839 ret = iwl_pcie_tx_alloc(trans);
840 if (ret)
841 goto error;
842 alloc = true;
843 }
844
845 spin_lock_irqsave(&trans_pcie->irq_lock, flags);
846
847 /* Turn off all Tx DMA fifos */
848 iwl_write_prph(trans, SCD_TXFACT, 0);
849
850 /* Tell NIC where to find the "keep warm" buffer */
851 iwl_write_direct32(trans, FH_KW_MEM_ADDR_REG,
852 trans_pcie->kw.dma >> 4);
853
854 spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
855
856 /* Alloc and init all Tx queues, including the command queue (#4/#9) */
857 for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues;
858 txq_id++) {
859 slots_num = (txq_id == trans_pcie->cmd_queue) ?
860 TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
861 ret = iwl_pcie_txq_init(trans, &trans_pcie->txq[txq_id],
862 slots_num, txq_id);
863 if (ret) {
864 IWL_ERR(trans, "Tx %d queue init failed\n", txq_id);
865 goto error;
866 }
867 }
868
869 return 0;
870error:
871 /*Upon error, free only if we allocated something */
872 if (alloc)
873 iwl_pcie_tx_free(trans);
874 return ret;
875}
876
877static inline void iwl_pcie_txq_progress(struct iwl_trans_pcie *trans_pcie,
878 struct iwl_txq *txq)
879{
880 if (!trans_pcie->wd_timeout)
881 return;
882
883 /*
884 * if empty delete timer, otherwise move timer forward
885 * since we're making progress on this queue
886 */
887 if (txq->q.read_ptr == txq->q.write_ptr)
888 del_timer(&txq->stuck_timer);
889 else
890 mod_timer(&txq->stuck_timer, jiffies + trans_pcie->wd_timeout);
891}
892
893/* Frees buffers until index _not_ inclusive */
f6d497cd
EG
894void iwl_trans_pcie_reclaim(struct iwl_trans *trans, int txq_id, int ssn,
895 struct sk_buff_head *skbs)
f02831be
EG
896{
897 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
898 struct iwl_txq *txq = &trans_pcie->txq[txq_id];
f6d497cd
EG
899 /* n_bd is usually 256 => n_bd - 1 = 0xff */
900 int tfd_num = ssn & (txq->q.n_bd - 1);
f02831be
EG
901 struct iwl_queue *q = &txq->q;
902 int last_to_free;
f02831be
EG
903
904 /* This function is not meant to release cmd queue*/
905 if (WARN_ON(txq_id == trans_pcie->cmd_queue))
f6d497cd 906 return;
214d14d4 907
f6d497cd
EG
908 spin_lock(&txq->lock);
909
910 if (txq->q.read_ptr == tfd_num)
911 goto out;
912
913 IWL_DEBUG_TX_REPLY(trans, "[Q %d] %d -> %d (%d)\n",
914 txq_id, txq->q.read_ptr, tfd_num, ssn);
214d14d4 915
f02831be
EG
916 /*Since we free until index _not_ inclusive, the one before index is
917 * the last we will free. This one must be used */
f6d497cd 918 last_to_free = iwl_queue_dec_wrap(tfd_num, q->n_bd);
f02831be 919
6ca6ebc1 920 if (!iwl_queue_used(q, last_to_free)) {
f02831be
EG
921 IWL_ERR(trans,
922 "%s: Read index for DMA queue txq id (%d), last_to_free %d is out of range [0-%d] %d %d.\n",
923 __func__, txq_id, last_to_free, q->n_bd,
924 q->write_ptr, q->read_ptr);
f6d497cd 925 goto out;
214d14d4
JB
926 }
927
f02831be 928 if (WARN_ON(!skb_queue_empty(skbs)))
f6d497cd 929 goto out;
214d14d4 930
f02831be 931 for (;
f6d497cd 932 q->read_ptr != tfd_num;
f02831be 933 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
214d14d4 934
f02831be
EG
935 if (WARN_ON_ONCE(txq->entries[txq->q.read_ptr].skb == NULL))
936 continue;
214d14d4 937
f02831be 938 __skb_queue_tail(skbs, txq->entries[txq->q.read_ptr].skb);
214d14d4 939
f02831be 940 txq->entries[txq->q.read_ptr].skb = NULL;
fd4abac5 941
f02831be 942 iwl_pcie_txq_inval_byte_cnt_tbl(trans, txq);
fd4abac5 943
f02831be 944 iwl_pcie_txq_free_tfd(trans, txq, DMA_TO_DEVICE);
f02831be 945 }
fd4abac5 946
f02831be
EG
947 iwl_pcie_txq_progress(trans_pcie, txq);
948
f6d497cd
EG
949 if (iwl_queue_space(&txq->q) > txq->q.low_mark)
950 iwl_wake_queue(trans, txq);
951out:
f02831be 952 spin_unlock(&txq->lock);
1053d35f
RR
953}
954
f02831be
EG
955/*
956 * iwl_pcie_cmdq_reclaim - Reclaim TX command queue entries already Tx'd
957 *
958 * When FW advances 'R' index, all entries between old and new 'R' index
959 * need to be reclaimed. As result, some free space forms. If there is
960 * enough free space (> low mark), wake the stack that feeds us.
961 */
962static void iwl_pcie_cmdq_reclaim(struct iwl_trans *trans, int txq_id, int idx)
48d42c42 963{
f02831be
EG
964 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
965 struct iwl_txq *txq = &trans_pcie->txq[txq_id];
966 struct iwl_queue *q = &txq->q;
967 int nfreed = 0;
48d42c42 968
f02831be 969 lockdep_assert_held(&txq->lock);
48d42c42 970
6ca6ebc1 971 if ((idx >= q->n_bd) || (!iwl_queue_used(q, idx))) {
f02831be
EG
972 IWL_ERR(trans,
973 "%s: Read index for DMA queue txq id (%d), index %d is out of range [0-%d] %d %d.\n",
974 __func__, txq_id, idx, q->n_bd,
975 q->write_ptr, q->read_ptr);
976 return;
977 }
48d42c42 978
f02831be
EG
979 for (idx = iwl_queue_inc_wrap(idx, q->n_bd); q->read_ptr != idx;
980 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
48d42c42 981
f02831be
EG
982 if (nfreed++ > 0) {
983 IWL_ERR(trans, "HCMD skipped: index (%d) %d %d\n",
984 idx, q->write_ptr, q->read_ptr);
985 iwl_op_mode_nic_error(trans->op_mode);
986 }
987 }
988
989 iwl_pcie_txq_progress(trans_pcie, txq);
48d42c42
EG
990}
991
f02831be 992static int iwl_pcie_txq_set_ratid_map(struct iwl_trans *trans, u16 ra_tid,
1ce8658c 993 u16 txq_id)
48d42c42 994{
20d3b647 995 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
48d42c42
EG
996 u32 tbl_dw_addr;
997 u32 tbl_dw;
998 u16 scd_q2ratid;
999
1000 scd_q2ratid = ra_tid & SCD_QUEUE_RA_TID_MAP_RATID_MSK;
1001
105183b1 1002 tbl_dw_addr = trans_pcie->scd_base_addr +
48d42c42
EG
1003 SCD_TRANS_TBL_OFFSET_QUEUE(txq_id);
1004
1042db2a 1005 tbl_dw = iwl_read_targ_mem(trans, tbl_dw_addr);
48d42c42
EG
1006
1007 if (txq_id & 0x1)
1008 tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF);
1009 else
1010 tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000);
1011
1042db2a 1012 iwl_write_targ_mem(trans, tbl_dw_addr, tbl_dw);
48d42c42
EG
1013
1014 return 0;
1015}
1016
f02831be
EG
1017static inline void iwl_pcie_txq_set_inactive(struct iwl_trans *trans,
1018 u16 txq_id)
48d42c42
EG
1019{
1020 /* Simply stop the queue, but don't change any configuration;
1021 * the SCD_ACT_EN bit is the write-enable mask for the ACTIVE bit. */
1042db2a 1022 iwl_write_prph(trans,
48d42c42
EG
1023 SCD_QUEUE_STATUS_BITS(txq_id),
1024 (0 << SCD_QUEUE_STTS_REG_POS_ACTIVE)|
1025 (1 << SCD_QUEUE_STTS_REG_POS_SCD_ACT_EN));
1026}
1027
f02831be
EG
1028void iwl_trans_pcie_txq_enable(struct iwl_trans *trans, int txq_id, int fifo,
1029 int sta_id, int tid, int frame_limit, u16 ssn)
48d42c42 1030{
9eae88fa 1031 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
4beaf6c2 1032
9eae88fa
JB
1033 if (test_and_set_bit(txq_id, trans_pcie->queue_used))
1034 WARN_ONCE(1, "queue %d already used - expect issues", txq_id);
48d42c42 1035
48d42c42 1036 /* Stop this Tx queue before configuring it */
f02831be 1037 iwl_pcie_txq_set_inactive(trans, txq_id);
48d42c42 1038
4beaf6c2
EG
1039 /* Set this queue as a chain-building queue unless it is CMD queue */
1040 if (txq_id != trans_pcie->cmd_queue)
1041 iwl_set_bits_prph(trans, SCD_QUEUECHAIN_SEL, BIT(txq_id));
1042
1043 /* If this queue is mapped to a certain station: it is an AGG queue */
1044 if (sta_id != IWL_INVALID_STATION) {
1045 u16 ra_tid = BUILD_RAxTID(sta_id, tid);
48d42c42 1046
4beaf6c2 1047 /* Map receiver-address / traffic-ID to this queue */
f02831be 1048 iwl_pcie_txq_set_ratid_map(trans, ra_tid, txq_id);
48d42c42 1049
4beaf6c2
EG
1050 /* enable aggregations for the queue */
1051 iwl_set_bits_prph(trans, SCD_AGGR_SEL, BIT(txq_id));
1ce8658c
EG
1052 } else {
1053 /*
1054 * disable aggregations for the queue, this will also make the
1055 * ra_tid mapping configuration irrelevant since it is now a
1056 * non-AGG queue.
1057 */
1058 iwl_clear_bits_prph(trans, SCD_AGGR_SEL, BIT(txq_id));
4beaf6c2 1059 }
48d42c42
EG
1060
1061 /* Place first TFD at index corresponding to start sequence number.
1062 * Assumes that ssn_idx is valid (!= 0xFFF) */
822e8b2a
EG
1063 trans_pcie->txq[txq_id].q.read_ptr = (ssn & 0xff);
1064 trans_pcie->txq[txq_id].q.write_ptr = (ssn & 0xff);
1ce8658c
EG
1065
1066 iwl_write_direct32(trans, HBUS_TARG_WRPTR,
1067 (ssn & 0xff) | (txq_id << 8));
1068 iwl_write_prph(trans, SCD_QUEUE_RDPTR(txq_id), ssn);
48d42c42
EG
1069
1070 /* Set up Tx window size and frame limit for this queue */
4beaf6c2
EG
1071 iwl_write_targ_mem(trans, trans_pcie->scd_base_addr +
1072 SCD_CONTEXT_QUEUE_OFFSET(txq_id), 0);
1042db2a 1073 iwl_write_targ_mem(trans, trans_pcie->scd_base_addr +
9eae88fa
JB
1074 SCD_CONTEXT_QUEUE_OFFSET(txq_id) + sizeof(u32),
1075 ((frame_limit << SCD_QUEUE_CTX_REG2_WIN_SIZE_POS) &
1076 SCD_QUEUE_CTX_REG2_WIN_SIZE_MSK) |
1077 ((frame_limit << SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
1078 SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK));
48d42c42 1079
48d42c42 1080 /* Set up Status area in SRAM, map to Tx DMA/FIFO, activate the queue */
1ce8658c
EG
1081 iwl_write_prph(trans, SCD_QUEUE_STATUS_BITS(txq_id),
1082 (1 << SCD_QUEUE_STTS_REG_POS_ACTIVE) |
1083 (fifo << SCD_QUEUE_STTS_REG_POS_TXF) |
1084 (1 << SCD_QUEUE_STTS_REG_POS_WSL) |
1085 SCD_QUEUE_STTS_REG_MSK);
1086 IWL_DEBUG_TX_QUEUES(trans, "Activate queue %d on FIFO %d WrPtr: %d\n",
1087 txq_id, fifo, ssn & 0xff);
4beaf6c2
EG
1088}
1089
f02831be 1090void iwl_trans_pcie_txq_disable(struct iwl_trans *trans, int txq_id)
288712a6 1091{
8ad71bef 1092 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
986ea6c9
EG
1093 u32 stts_addr = trans_pcie->scd_base_addr +
1094 SCD_TX_STTS_QUEUE_OFFSET(txq_id);
1095 static const u32 zero_val[4] = {};
288712a6 1096
9eae88fa
JB
1097 if (!test_and_clear_bit(txq_id, trans_pcie->queue_used)) {
1098 WARN_ONCE(1, "queue %d not used", txq_id);
1099 return;
48d42c42
EG
1100 }
1101
f02831be 1102 iwl_pcie_txq_set_inactive(trans, txq_id);
ac928f8d 1103
986ea6c9
EG
1104 _iwl_write_targ_mem_dwords(trans, stts_addr,
1105 zero_val, ARRAY_SIZE(zero_val));
1106
990aa6d7 1107 iwl_pcie_txq_unmap(trans, txq_id);
6c3fd3f0 1108
1ce8658c 1109 IWL_DEBUG_TX_QUEUES(trans, "Deactivate queue %d\n", txq_id);
48d42c42
EG
1110}
1111
fd4abac5
TW
1112/*************** HOST COMMAND QUEUE FUNCTIONS *****/
1113
990aa6d7 1114/*
f02831be 1115 * iwl_pcie_enqueue_hcmd - enqueue a uCode command
fd4abac5
TW
1116 * @priv: device private data point
1117 * @cmd: a point to the ucode command structure
1118 *
1119 * The function returns < 0 values to indicate the operation is
1120 * failed. On success, it turns the index (> 0) of command in the
1121 * command queue.
1122 */
f02831be
EG
1123static int iwl_pcie_enqueue_hcmd(struct iwl_trans *trans,
1124 struct iwl_host_cmd *cmd)
fd4abac5 1125{
8ad71bef 1126 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
990aa6d7 1127 struct iwl_txq *txq = &trans_pcie->txq[trans_pcie->cmd_queue];
fd4abac5 1128 struct iwl_queue *q = &txq->q;
c2acea8e
JB
1129 struct iwl_device_cmd *out_cmd;
1130 struct iwl_cmd_meta *out_meta;
f4feb8ac 1131 void *dup_buf = NULL;
fd4abac5 1132 dma_addr_t phys_addr;
f4feb8ac 1133 int idx;
4ce7cc2b 1134 u16 copy_size, cmd_size;
4ce7cc2b
JB
1135 bool had_nocopy = false;
1136 int i;
96791422 1137 u32 cmd_pos;
fd4abac5 1138
4ce7cc2b
JB
1139 copy_size = sizeof(out_cmd->hdr);
1140 cmd_size = sizeof(out_cmd->hdr);
1141
1142 /* need one for the header if the first is NOCOPY */
1143 BUILD_BUG_ON(IWL_MAX_CMD_TFDS > IWL_NUM_OF_TBS - 1);
1144
1145 for (i = 0; i < IWL_MAX_CMD_TFDS; i++) {
1146 if (!cmd->len[i])
1147 continue;
1148 if (cmd->dataflags[i] & IWL_HCMD_DFL_NOCOPY) {
1149 had_nocopy = true;
f4feb8ac
JB
1150 if (WARN_ON(cmd->dataflags[i] & IWL_HCMD_DFL_DUP)) {
1151 idx = -EINVAL;
1152 goto free_dup_buf;
1153 }
1154 } else if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP) {
1155 /*
1156 * This is also a chunk that isn't copied
1157 * to the static buffer so set had_nocopy.
1158 */
1159 had_nocopy = true;
1160
1161 /* only allowed once */
1162 if (WARN_ON(dup_buf)) {
1163 idx = -EINVAL;
1164 goto free_dup_buf;
1165 }
1166
1167 dup_buf = kmemdup(cmd->data[i], cmd->len[i],
1168 GFP_ATOMIC);
1169 if (!dup_buf)
1170 return -ENOMEM;
4ce7cc2b
JB
1171 } else {
1172 /* NOCOPY must not be followed by normal! */
f4feb8ac
JB
1173 if (WARN_ON(had_nocopy)) {
1174 idx = -EINVAL;
1175 goto free_dup_buf;
1176 }
4ce7cc2b
JB
1177 copy_size += cmd->len[i];
1178 }
1179 cmd_size += cmd->len[i];
1180 }
fd4abac5 1181
3e41ace5
JB
1182 /*
1183 * If any of the command structures end up being larger than
4ce7cc2b
JB
1184 * the TFD_MAX_PAYLOAD_SIZE and they aren't dynamically
1185 * allocated into separate TFDs, then we will need to
1186 * increase the size of the buffers.
3e41ace5 1187 */
2a79e45e
JB
1188 if (WARN(copy_size > TFD_MAX_PAYLOAD_SIZE,
1189 "Command %s (%#x) is too large (%d bytes)\n",
990aa6d7 1190 get_cmd_string(trans_pcie, cmd->id), cmd->id, copy_size)) {
f4feb8ac
JB
1191 idx = -EINVAL;
1192 goto free_dup_buf;
1193 }
fd4abac5 1194
015c15e1 1195 spin_lock_bh(&txq->lock);
3598e177 1196
c2acea8e 1197 if (iwl_queue_space(q) < ((cmd->flags & CMD_ASYNC) ? 2 : 1)) {
015c15e1 1198 spin_unlock_bh(&txq->lock);
3598e177 1199
6d8f6eeb 1200 IWL_ERR(trans, "No space in command queue\n");
0e781842 1201 iwl_op_mode_cmd_queue_full(trans->op_mode);
f4feb8ac
JB
1202 idx = -ENOSPC;
1203 goto free_dup_buf;
fd4abac5
TW
1204 }
1205
4ce7cc2b 1206 idx = get_cmd_index(q, q->write_ptr);
bf8440e6
JB
1207 out_cmd = txq->entries[idx].cmd;
1208 out_meta = &txq->entries[idx].meta;
c2acea8e 1209
8ce73f3a 1210 memset(out_meta, 0, sizeof(*out_meta)); /* re-initialize to NULL */
c2acea8e
JB
1211 if (cmd->flags & CMD_WANT_SKB)
1212 out_meta->source = cmd;
fd4abac5 1213
4ce7cc2b 1214 /* set up the header */
fd4abac5 1215
4ce7cc2b 1216 out_cmd->hdr.cmd = cmd->id;
fd4abac5 1217 out_cmd->hdr.flags = 0;
cefeaa5f 1218 out_cmd->hdr.sequence =
c6f600fc 1219 cpu_to_le16(QUEUE_TO_SEQ(trans_pcie->cmd_queue) |
cefeaa5f 1220 INDEX_TO_SEQ(q->write_ptr));
4ce7cc2b
JB
1221
1222 /* and copy the data that needs to be copied */
96791422 1223 cmd_pos = offsetof(struct iwl_device_cmd, payload);
4ce7cc2b
JB
1224 for (i = 0; i < IWL_MAX_CMD_TFDS; i++) {
1225 if (!cmd->len[i])
1226 continue;
f4feb8ac
JB
1227 if (cmd->dataflags[i] & (IWL_HCMD_DFL_NOCOPY |
1228 IWL_HCMD_DFL_DUP))
4ce7cc2b 1229 break;
96791422
EG
1230 memcpy((u8 *)out_cmd + cmd_pos, cmd->data[i], cmd->len[i]);
1231 cmd_pos += cmd->len[i];
1232 }
1233
1234 WARN_ON_ONCE(txq->entries[idx].copy_cmd);
1235
1236 /*
1237 * since out_cmd will be the source address of the FH, it will write
1238 * the retry count there. So when the user needs to receivce the HCMD
1239 * that corresponds to the response in the response handler, it needs
1240 * to set CMD_WANT_HCMD.
1241 */
1242 if (cmd->flags & CMD_WANT_HCMD) {
1243 txq->entries[idx].copy_cmd =
1244 kmemdup(out_cmd, cmd_pos, GFP_ATOMIC);
1245 if (unlikely(!txq->entries[idx].copy_cmd)) {
1246 idx = -ENOMEM;
1247 goto out;
1248 }
ded2ae7c 1249 }
4ce7cc2b 1250
d9fb6465 1251 IWL_DEBUG_HC(trans,
20d3b647 1252 "Sending command %s (#%x), seq: 0x%04X, %d bytes at %d[%d]:%d\n",
990aa6d7 1253 get_cmd_string(trans_pcie, out_cmd->hdr.cmd),
20d3b647
JB
1254 out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
1255 cmd_size, q->write_ptr, idx, trans_pcie->cmd_queue);
4ce7cc2b 1256
1042db2a 1257 phys_addr = dma_map_single(trans->dev, &out_cmd->hdr, copy_size,
20d3b647 1258 DMA_BIDIRECTIONAL);
1042db2a 1259 if (unlikely(dma_mapping_error(trans->dev, phys_addr))) {
2c46f72e
JB
1260 idx = -ENOMEM;
1261 goto out;
1262 }
1263
2e724443 1264 dma_unmap_addr_set(out_meta, mapping, phys_addr);
4ce7cc2b
JB
1265 dma_unmap_len_set(out_meta, len, copy_size);
1266
f02831be 1267 iwl_pcie_txq_build_tfd(trans, txq, phys_addr, copy_size, 1);
4ce7cc2b
JB
1268
1269 for (i = 0; i < IWL_MAX_CMD_TFDS; i++) {
f4feb8ac
JB
1270 const void *data = cmd->data[i];
1271
4ce7cc2b
JB
1272 if (!cmd->len[i])
1273 continue;
f4feb8ac
JB
1274 if (!(cmd->dataflags[i] & (IWL_HCMD_DFL_NOCOPY |
1275 IWL_HCMD_DFL_DUP)))
4ce7cc2b 1276 continue;
f4feb8ac
JB
1277 if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP)
1278 data = dup_buf;
1279 phys_addr = dma_map_single(trans->dev, (void *)data,
3be3fdb5 1280 cmd->len[i], DMA_BIDIRECTIONAL);
1042db2a 1281 if (dma_mapping_error(trans->dev, phys_addr)) {
f02831be
EG
1282 iwl_pcie_tfd_unmap(trans, out_meta,
1283 &txq->tfds[q->write_ptr],
1284 DMA_BIDIRECTIONAL);
4ce7cc2b
JB
1285 idx = -ENOMEM;
1286 goto out;
1287 }
1288
f02831be 1289 iwl_pcie_txq_build_tfd(trans, txq, phys_addr, cmd->len[i], 0);
4ce7cc2b 1290 }
df833b1d 1291
afaf6b57 1292 out_meta->flags = cmd->flags;
f4feb8ac
JB
1293 if (WARN_ON_ONCE(txq->entries[idx].free_buf))
1294 kfree(txq->entries[idx].free_buf);
1295 txq->entries[idx].free_buf = dup_buf;
2c46f72e
JB
1296
1297 txq->need_update = 1;
1298
45eab7cc
JB
1299 trace_iwlwifi_dev_hcmd(trans->dev, cmd, cmd_size,
1300 &out_cmd->hdr, copy_size);
df833b1d 1301
7c5ba4a8
JB
1302 /* start timer if queue currently empty */
1303 if (q->read_ptr == q->write_ptr && trans_pcie->wd_timeout)
1304 mod_timer(&txq->stuck_timer, jiffies + trans_pcie->wd_timeout);
1305
fd4abac5
TW
1306 /* Increment and update queue's write index */
1307 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
990aa6d7 1308 iwl_pcie_txq_inc_wr_ptr(trans, txq);
fd4abac5 1309
2c46f72e 1310 out:
015c15e1 1311 spin_unlock_bh(&txq->lock);
f4feb8ac
JB
1312 free_dup_buf:
1313 if (idx < 0)
1314 kfree(dup_buf);
7bfedc59 1315 return idx;
fd4abac5
TW
1316}
1317
990aa6d7
EG
1318/*
1319 * iwl_pcie_hcmd_complete - Pull unused buffers off the queue and reclaim them
17b88929 1320 * @rxb: Rx buffer to reclaim
247c61d6
EG
1321 * @handler_status: return value of the handler of the command
1322 * (put in setup_rx_handlers)
17b88929
TW
1323 *
1324 * If an Rx buffer has an async callback associated with it the callback
1325 * will be executed. The attached skb (if present) will only be freed
1326 * if the callback returns 1
1327 */
990aa6d7
EG
1328void iwl_pcie_hcmd_complete(struct iwl_trans *trans,
1329 struct iwl_rx_cmd_buffer *rxb, int handler_status)
17b88929 1330{
2f301227 1331 struct iwl_rx_packet *pkt = rxb_addr(rxb);
17b88929
TW
1332 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
1333 int txq_id = SEQ_TO_QUEUE(sequence);
1334 int index = SEQ_TO_INDEX(sequence);
17b88929 1335 int cmd_index;
c2acea8e
JB
1336 struct iwl_device_cmd *cmd;
1337 struct iwl_cmd_meta *meta;
8ad71bef 1338 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
990aa6d7 1339 struct iwl_txq *txq = &trans_pcie->txq[trans_pcie->cmd_queue];
17b88929
TW
1340
1341 /* If a Tx command is being handled and it isn't in the actual
1342 * command queue then there a command routing bug has been introduced
1343 * in the queue management code. */
c6f600fc 1344 if (WARN(txq_id != trans_pcie->cmd_queue,
13bb9483 1345 "wrong command queue %d (should be %d), sequence 0x%X readp=%d writep=%d\n",
20d3b647
JB
1346 txq_id, trans_pcie->cmd_queue, sequence,
1347 trans_pcie->txq[trans_pcie->cmd_queue].q.read_ptr,
1348 trans_pcie->txq[trans_pcie->cmd_queue].q.write_ptr)) {
3e10caeb 1349 iwl_print_hex_error(trans, pkt, 32);
55d6a3cd 1350 return;
01ef9323 1351 }
17b88929 1352
015c15e1
JB
1353 spin_lock(&txq->lock);
1354
4ce7cc2b 1355 cmd_index = get_cmd_index(&txq->q, index);
bf8440e6
JB
1356 cmd = txq->entries[cmd_index].cmd;
1357 meta = &txq->entries[cmd_index].meta;
17b88929 1358
f02831be 1359 iwl_pcie_tfd_unmap(trans, meta, &txq->tfds[index], DMA_BIDIRECTIONAL);
c33de625 1360
17b88929 1361 /* Input error checking is done when commands are added to queue. */
c2acea8e 1362 if (meta->flags & CMD_WANT_SKB) {
48a2d66f 1363 struct page *p = rxb_steal_page(rxb);
65b94a4a 1364
65b94a4a
JB
1365 meta->source->resp_pkt = pkt;
1366 meta->source->_rx_page_addr = (unsigned long)page_address(p);
b2cf410c 1367 meta->source->_rx_page_order = trans_pcie->rx_page_order;
247c61d6 1368 meta->source->handler_status = handler_status;
247c61d6 1369 }
2624e96c 1370
f02831be 1371 iwl_pcie_cmdq_reclaim(trans, txq_id, index);
17b88929 1372
c2acea8e 1373 if (!(meta->flags & CMD_ASYNC)) {
74fda971 1374 if (!test_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status)) {
05c89b91
WYG
1375 IWL_WARN(trans,
1376 "HCMD_ACTIVE already clear for command %s\n",
990aa6d7 1377 get_cmd_string(trans_pcie, cmd->hdr.cmd));
05c89b91 1378 }
74fda971 1379 clear_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status);
6d8f6eeb 1380 IWL_DEBUG_INFO(trans, "Clearing HCMD_ACTIVE for command %s\n",
990aa6d7 1381 get_cmd_string(trans_pcie, cmd->hdr.cmd));
f946b529 1382 wake_up(&trans_pcie->wait_command_queue);
17b88929 1383 }
3598e177 1384
dd487449 1385 meta->flags = 0;
3598e177 1386
015c15e1 1387 spin_unlock(&txq->lock);
17b88929 1388}
253a634c 1389
253a634c
EG
1390#define HOST_COMPLETE_TIMEOUT (2 * HZ)
1391
f02831be
EG
1392static int iwl_pcie_send_hcmd_async(struct iwl_trans *trans,
1393 struct iwl_host_cmd *cmd)
253a634c 1394{
d9fb6465 1395 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
253a634c
EG
1396 int ret;
1397
1398 /* An asynchronous command can not expect an SKB to be set. */
1399 if (WARN_ON(cmd->flags & CMD_WANT_SKB))
1400 return -EINVAL;
1401
f02831be 1402 ret = iwl_pcie_enqueue_hcmd(trans, cmd);
253a634c 1403 if (ret < 0) {
721c32f7 1404 IWL_ERR(trans,
b36b110c 1405 "Error sending %s: enqueue_hcmd failed: %d\n",
990aa6d7 1406 get_cmd_string(trans_pcie, cmd->id), ret);
253a634c
EG
1407 return ret;
1408 }
1409 return 0;
1410}
1411
f02831be
EG
1412static int iwl_pcie_send_hcmd_sync(struct iwl_trans *trans,
1413 struct iwl_host_cmd *cmd)
253a634c 1414{
8ad71bef 1415 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
253a634c
EG
1416 int cmd_idx;
1417 int ret;
1418
6d8f6eeb 1419 IWL_DEBUG_INFO(trans, "Attempting to send sync command %s\n",
990aa6d7 1420 get_cmd_string(trans_pcie, cmd->id));
253a634c 1421
2cc39c94 1422 if (WARN_ON(test_and_set_bit(STATUS_HCMD_ACTIVE,
74fda971 1423 &trans_pcie->status))) {
2cc39c94 1424 IWL_ERR(trans, "Command %s: a command is already active!\n",
990aa6d7 1425 get_cmd_string(trans_pcie, cmd->id));
2cc39c94
JB
1426 return -EIO;
1427 }
1428
6d8f6eeb 1429 IWL_DEBUG_INFO(trans, "Setting HCMD_ACTIVE for command %s\n",
990aa6d7 1430 get_cmd_string(trans_pcie, cmd->id));
253a634c 1431
f02831be 1432 cmd_idx = iwl_pcie_enqueue_hcmd(trans, cmd);
253a634c
EG
1433 if (cmd_idx < 0) {
1434 ret = cmd_idx;
74fda971 1435 clear_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status);
721c32f7 1436 IWL_ERR(trans,
b36b110c 1437 "Error sending %s: enqueue_hcmd failed: %d\n",
990aa6d7 1438 get_cmd_string(trans_pcie, cmd->id), ret);
253a634c
EG
1439 return ret;
1440 }
1441
f946b529 1442 ret = wait_event_timeout(trans_pcie->wait_command_queue,
20d3b647
JB
1443 !test_bit(STATUS_HCMD_ACTIVE,
1444 &trans_pcie->status),
1445 HOST_COMPLETE_TIMEOUT);
253a634c 1446 if (!ret) {
74fda971 1447 if (test_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status)) {
990aa6d7 1448 struct iwl_txq *txq =
c6f600fc 1449 &trans_pcie->txq[trans_pcie->cmd_queue];
d10630af
WYG
1450 struct iwl_queue *q = &txq->q;
1451
721c32f7 1452 IWL_ERR(trans,
253a634c 1453 "Error sending %s: time out after %dms.\n",
990aa6d7 1454 get_cmd_string(trans_pcie, cmd->id),
253a634c
EG
1455 jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
1456
721c32f7 1457 IWL_ERR(trans,
d10630af
WYG
1458 "Current CMD queue read_ptr %d write_ptr %d\n",
1459 q->read_ptr, q->write_ptr);
1460
74fda971 1461 clear_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status);
d9fb6465
JB
1462 IWL_DEBUG_INFO(trans,
1463 "Clearing HCMD_ACTIVE for command %s\n",
990aa6d7 1464 get_cmd_string(trans_pcie, cmd->id));
253a634c
EG
1465 ret = -ETIMEDOUT;
1466 goto cancel;
1467 }
1468 }
1469
d18aa87f
JB
1470 if (test_bit(STATUS_FW_ERROR, &trans_pcie->status)) {
1471 IWL_ERR(trans, "FW error in SYNC CMD %s\n",
990aa6d7 1472 get_cmd_string(trans_pcie, cmd->id));
d18aa87f
JB
1473 ret = -EIO;
1474 goto cancel;
1475 }
1476
f946b529
EG
1477 if (test_bit(STATUS_RFKILL, &trans_pcie->status)) {
1478 IWL_DEBUG_RF_KILL(trans, "RFKILL in SYNC CMD... no rsp\n");
1479 ret = -ERFKILL;
1480 goto cancel;
1481 }
1482
65b94a4a 1483 if ((cmd->flags & CMD_WANT_SKB) && !cmd->resp_pkt) {
6d8f6eeb 1484 IWL_ERR(trans, "Error: Response NULL in '%s'\n",
990aa6d7 1485 get_cmd_string(trans_pcie, cmd->id));
253a634c
EG
1486 ret = -EIO;
1487 goto cancel;
1488 }
1489
1490 return 0;
1491
1492cancel:
1493 if (cmd->flags & CMD_WANT_SKB) {
1494 /*
1495 * Cancel the CMD_WANT_SKB flag for the cmd in the
1496 * TX cmd queue. Otherwise in case the cmd comes
1497 * in later, it will possibly set an invalid
1498 * address (cmd->meta.source).
1499 */
bf8440e6
JB
1500 trans_pcie->txq[trans_pcie->cmd_queue].
1501 entries[cmd_idx].meta.flags &= ~CMD_WANT_SKB;
253a634c 1502 }
9cac4943 1503
65b94a4a
JB
1504 if (cmd->resp_pkt) {
1505 iwl_free_resp(cmd);
1506 cmd->resp_pkt = NULL;
253a634c
EG
1507 }
1508
1509 return ret;
1510}
1511
f02831be 1512int iwl_trans_pcie_send_hcmd(struct iwl_trans *trans, struct iwl_host_cmd *cmd)
253a634c 1513{
f946b529
EG
1514 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1515
d18aa87f
JB
1516 if (test_bit(STATUS_FW_ERROR, &trans_pcie->status))
1517 return -EIO;
1518
f946b529
EG
1519 if (test_bit(STATUS_RFKILL, &trans_pcie->status))
1520 return -ERFKILL;
1521
253a634c 1522 if (cmd->flags & CMD_ASYNC)
f02831be 1523 return iwl_pcie_send_hcmd_async(trans, cmd);
253a634c 1524
f946b529 1525 /* We still can fail on RFKILL that can be asserted while we wait */
f02831be 1526 return iwl_pcie_send_hcmd_sync(trans, cmd);
253a634c
EG
1527}
1528
f02831be
EG
1529int iwl_trans_pcie_tx(struct iwl_trans *trans, struct sk_buff *skb,
1530 struct iwl_device_cmd *dev_cmd, int txq_id)
a0eaad71 1531{
8ad71bef 1532 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
f02831be
EG
1533 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1534 struct iwl_tx_cmd *tx_cmd = (struct iwl_tx_cmd *)dev_cmd->payload;
1535 struct iwl_cmd_meta *out_meta;
1536 struct iwl_txq *txq;
1537 struct iwl_queue *q;
1538 dma_addr_t phys_addr = 0;
1539 dma_addr_t txcmd_phys;
1540 dma_addr_t scratch_phys;
1541 u16 len, firstlen, secondlen;
1542 u8 wait_write_ptr = 0;
1543 __le16 fc = hdr->frame_control;
1544 u8 hdr_len = ieee80211_hdrlen(fc);
1545 u16 __maybe_unused wifi_seq;
1546
1547 txq = &trans_pcie->txq[txq_id];
1548 q = &txq->q;
a0eaad71 1549
f02831be
EG
1550 if (unlikely(!test_bit(txq_id, trans_pcie->queue_used))) {
1551 WARN_ON_ONCE(1);
1552 return -EINVAL;
1553 }
39644e9a 1554
f02831be 1555 spin_lock(&txq->lock);
015c15e1 1556
f02831be
EG
1557 /* In AGG mode, the index in the ring must correspond to the WiFi
1558 * sequence number. This is a HW requirements to help the SCD to parse
1559 * the BA.
1560 * Check here that the packets are in the right place on the ring.
1561 */
1562#ifdef CONFIG_IWLWIFI_DEBUG
1563 wifi_seq = SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
1564 WARN_ONCE((iwl_read_prph(trans, SCD_AGGR_SEL) & BIT(txq_id)) &&
1565 ((wifi_seq & 0xff) != q->write_ptr),
1566 "Q: %d WiFi Seq %d tfdNum %d",
1567 txq_id, wifi_seq, q->write_ptr);
1568#endif
1569
1570 /* Set up driver data for this TFD */
1571 txq->entries[q->write_ptr].skb = skb;
1572 txq->entries[q->write_ptr].cmd = dev_cmd;
1573
1574 dev_cmd->hdr.cmd = REPLY_TX;
1575 dev_cmd->hdr.sequence =
1576 cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
1577 INDEX_TO_SEQ(q->write_ptr)));
1578
1579 /* Set up first empty entry in queue's array of Tx/cmd buffers */
1580 out_meta = &txq->entries[q->write_ptr].meta;
a0eaad71 1581
f02831be
EG
1582 /*
1583 * Use the first empty entry in this queue's command buffer array
1584 * to contain the Tx command and MAC header concatenated together
1585 * (payload data will be in another buffer).
1586 * Size of this varies, due to varying MAC header length.
1587 * If end is not dword aligned, we'll have 2 extra bytes at the end
1588 * of the MAC header (device reads on dword boundaries).
1589 * We'll tell device about this padding later.
1590 */
1591 len = sizeof(struct iwl_tx_cmd) +
1592 sizeof(struct iwl_cmd_header) + hdr_len;
1593 firstlen = (len + 3) & ~3;
1594
1595 /* Tell NIC about any 2-byte padding after MAC header */
1596 if (firstlen != len)
1597 tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
1598
1599 /* Physical address of this Tx command's header (not MAC header!),
1600 * within command buffer array. */
1601 txcmd_phys = dma_map_single(trans->dev,
1602 &dev_cmd->hdr, firstlen,
1603 DMA_BIDIRECTIONAL);
1604 if (unlikely(dma_mapping_error(trans->dev, txcmd_phys)))
1605 goto out_err;
1606 dma_unmap_addr_set(out_meta, mapping, txcmd_phys);
1607 dma_unmap_len_set(out_meta, len, firstlen);
1608
1609 if (!ieee80211_has_morefrags(fc)) {
1610 txq->need_update = 1;
1611 } else {
1612 wait_write_ptr = 1;
1613 txq->need_update = 0;
a0eaad71
EG
1614 }
1615
f02831be
EG
1616 /* Set up TFD's 2nd entry to point directly to remainder of skb,
1617 * if any (802.11 null frames have no payload). */
1618 secondlen = skb->len - hdr_len;
1619 if (secondlen > 0) {
1620 phys_addr = dma_map_single(trans->dev, skb->data + hdr_len,
1621 secondlen, DMA_TO_DEVICE);
1622 if (unlikely(dma_mapping_error(trans->dev, phys_addr))) {
1623 dma_unmap_single(trans->dev,
1624 dma_unmap_addr(out_meta, mapping),
1625 dma_unmap_len(out_meta, len),
1626 DMA_BIDIRECTIONAL);
1627 goto out_err;
1628 }
1629 }
a0eaad71 1630
f02831be
EG
1631 /* Attach buffers to TFD */
1632 iwl_pcie_txq_build_tfd(trans, txq, txcmd_phys, firstlen, 1);
1633 if (secondlen > 0)
1634 iwl_pcie_txq_build_tfd(trans, txq, phys_addr, secondlen, 0);
a0eaad71 1635
f02831be
EG
1636 scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
1637 offsetof(struct iwl_tx_cmd, scratch);
a0eaad71 1638
f02831be
EG
1639 /* take back ownership of DMA buffer to enable update */
1640 dma_sync_single_for_cpu(trans->dev, txcmd_phys, firstlen,
1641 DMA_BIDIRECTIONAL);
1642 tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
1643 tx_cmd->dram_msb_ptr = iwl_get_dma_hi_addr(scratch_phys);
a0eaad71 1644
f02831be
EG
1645 IWL_DEBUG_TX(trans, "sequence nr = 0X%x\n",
1646 le16_to_cpu(dev_cmd->hdr.sequence));
1647 IWL_DEBUG_TX(trans, "tx_flags = 0X%x\n", le32_to_cpu(tx_cmd->tx_flags));
a0eaad71 1648
f02831be
EG
1649 /* Set up entry for this TFD in Tx byte-count array */
1650 iwl_pcie_txq_update_byte_cnt_tbl(trans, txq, le16_to_cpu(tx_cmd->len));
a0eaad71 1651
f02831be
EG
1652 dma_sync_single_for_device(trans->dev, txcmd_phys, firstlen,
1653 DMA_BIDIRECTIONAL);
7c5ba4a8 1654
f02831be
EG
1655 trace_iwlwifi_dev_tx(trans->dev, skb,
1656 &txq->tfds[txq->q.write_ptr],
1657 sizeof(struct iwl_tfd),
1658 &dev_cmd->hdr, firstlen,
1659 skb->data + hdr_len, secondlen);
1660 trace_iwlwifi_dev_tx_data(trans->dev, skb,
1661 skb->data + hdr_len, secondlen);
7c5ba4a8 1662
f02831be
EG
1663 /* start timer if queue currently empty */
1664 if (txq->need_update && q->read_ptr == q->write_ptr &&
1665 trans_pcie->wd_timeout)
1666 mod_timer(&txq->stuck_timer, jiffies + trans_pcie->wd_timeout);
1667
1668 /* Tell device the write index *just past* this latest filled TFD */
1669 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
1670 iwl_pcie_txq_inc_wr_ptr(trans, txq);
1671
1672 /*
1673 * At this point the frame is "transmitted" successfully
1674 * and we will get a TX status notification eventually,
1675 * regardless of the value of ret. "ret" only indicates
1676 * whether or not we should update the write pointer.
1677 */
1678 if (iwl_queue_space(q) < q->high_mark) {
1679 if (wait_write_ptr) {
1680 txq->need_update = 1;
1681 iwl_pcie_txq_inc_wr_ptr(trans, txq);
1682 } else {
1683 iwl_stop_queue(trans, txq);
1684 }
1685 }
1686 spin_unlock(&txq->lock);
1687 return 0;
1688out_err:
1689 spin_unlock(&txq->lock);
1690 return -1;
a0eaad71 1691}
This page took 0.715062 seconds and 5 git commands to generate.