1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
3 * Copyright (C) 2003-2014, 2018-2021, 2023 Intel Corporation
4 * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
5 * Copyright (C) 2016-2017 Intel Deutschland GmbH
7 #include <linux/etherdevice.h>
8 #include <linux/ieee80211.h>
9 #include <linux/slab.h>
10 #include <linux/sched.h>
11 #include <net/ip6_checksum.h>
14 #include "iwl-debug.h"
19 #include "iwl-op-mode.h"
21 #include "fw/api/tx.h"
23 /*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
28 * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
29 * of buffer descriptors, each of which points to one or more data buffers for
30 * the device to read from or fill. Driver and device exchange status of each
31 * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
32 * entries in each circular buffer, to protect against confusing empty and full
35 * The device reads or writes the data in the queues via the device's several
36 * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
38 * For Tx queue, there are low mark and high mark limits. If, after queuing
39 * the packet for Tx, free space become < low mark, Tx queue stopped. When
40 * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
43 ***************************************************/
46 int iwl_pcie_alloc_dma_ptr(struct iwl_trans *trans,
47 struct iwl_dma_ptr *ptr, size_t size)
49 if (WARN_ON(ptr->addr))
52 ptr->addr = dma_alloc_coherent(trans->dev, size,
53 &ptr->dma, GFP_KERNEL);
60 void iwl_pcie_free_dma_ptr(struct iwl_trans *trans, struct iwl_dma_ptr *ptr)
62 if (unlikely(!ptr->addr))
65 dma_free_coherent(trans->dev, ptr->size, ptr->addr, ptr->dma);
66 memset(ptr, 0, sizeof(*ptr));
70 * iwl_pcie_txq_inc_wr_ptr - Send new write index to hardware
72 static void iwl_pcie_txq_inc_wr_ptr(struct iwl_trans *trans,
78 lockdep_assert_held(&txq->lock);
81 * explicitly wake up the NIC if:
82 * 1. shadow registers aren't enabled
83 * 2. NIC is woken up for CMD regardless of shadow outside this function
84 * 3. there is a chance that the NIC is asleep
86 if (!trans->trans_cfg->base_params->shadow_reg_enable &&
87 txq_id != trans->txqs.cmd.q_id &&
88 test_bit(STATUS_TPOWER_PMI, &trans->status)) {
90 * wake up nic if it's powered down ...
91 * uCode will wake up, and interrupt us again, so next
92 * time we'll skip this part.
94 reg = iwl_read32(trans, CSR_UCODE_DRV_GP1);
96 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
97 IWL_DEBUG_INFO(trans, "Tx queue %d requesting wakeup, GP1 = 0x%x\n",
99 iwl_set_bit(trans, CSR_GP_CNTRL,
100 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
101 txq->need_update = true;
107 * if not in power-save mode, uCode will never sleep when we're
108 * trying to tx (during RFKILL, we're not trying to tx).
110 IWL_DEBUG_TX(trans, "Q:%d WR: 0x%x\n", txq_id, txq->write_ptr);
112 iwl_write32(trans, HBUS_TARG_WRPTR,
113 txq->write_ptr | (txq_id << 8));
116 void iwl_pcie_txq_check_wrptrs(struct iwl_trans *trans)
120 for (i = 0; i < trans->trans_cfg->base_params->num_of_queues; i++) {
121 struct iwl_txq *txq = trans->txqs.txq[i];
123 if (!test_bit(i, trans->txqs.queue_used))
126 spin_lock_bh(&txq->lock);
127 if (txq->need_update) {
128 iwl_pcie_txq_inc_wr_ptr(trans, txq);
129 txq->need_update = false;
131 spin_unlock_bh(&txq->lock);
135 static inline void iwl_pcie_tfd_set_tb(struct iwl_trans *trans, void *tfd,
136 u8 idx, dma_addr_t addr, u16 len)
138 struct iwl_tfd *tfd_fh = (void *)tfd;
139 struct iwl_tfd_tb *tb = &tfd_fh->tbs[idx];
141 u16 hi_n_len = len << 4;
143 put_unaligned_le32(addr, &tb->lo);
144 hi_n_len |= iwl_get_dma_hi_addr(addr);
146 tb->hi_n_len = cpu_to_le16(hi_n_len);
148 tfd_fh->num_tbs = idx + 1;
151 static int iwl_pcie_txq_build_tfd(struct iwl_trans *trans, struct iwl_txq *txq,
152 dma_addr_t addr, u16 len, bool reset)
157 tfd = (u8 *)txq->tfds + trans->txqs.tfd.size * txq->write_ptr;
160 memset(tfd, 0, trans->txqs.tfd.size);
162 num_tbs = iwl_txq_gen1_tfd_get_num_tbs(trans, tfd);
164 /* Each TFD can point to a maximum max_tbs Tx buffers */
165 if (num_tbs >= trans->txqs.tfd.max_tbs) {
166 IWL_ERR(trans, "Error can not send more than %d chunks\n",
167 trans->txqs.tfd.max_tbs);
171 if (WARN(addr & ~IWL_TX_DMA_MASK,
172 "Unaligned address = %llx\n", (unsigned long long)addr))
175 iwl_pcie_tfd_set_tb(trans, tfd, num_tbs, addr, len);
180 static void iwl_pcie_clear_cmd_in_flight(struct iwl_trans *trans)
182 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
184 if (!trans->trans_cfg->base_params->apmg_wake_up_wa)
187 spin_lock(&trans_pcie->reg_lock);
189 if (WARN_ON(!trans_pcie->cmd_hold_nic_awake)) {
190 spin_unlock(&trans_pcie->reg_lock);
194 trans_pcie->cmd_hold_nic_awake = false;
195 __iwl_trans_pcie_clear_bit(trans, CSR_GP_CNTRL,
196 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
197 spin_unlock(&trans_pcie->reg_lock);
201 * iwl_pcie_txq_unmap - Unmap any remaining DMA mappings and free skb's
203 static void iwl_pcie_txq_unmap(struct iwl_trans *trans, int txq_id)
205 struct iwl_txq *txq = trans->txqs.txq[txq_id];
208 IWL_ERR(trans, "Trying to free a queue that wasn't allocated?\n");
212 spin_lock_bh(&txq->lock);
213 while (txq->write_ptr != txq->read_ptr) {
214 IWL_DEBUG_TX_REPLY(trans, "Q %d Free %d\n",
215 txq_id, txq->read_ptr);
217 if (txq_id != trans->txqs.cmd.q_id) {
218 struct sk_buff *skb = txq->entries[txq->read_ptr].skb;
220 if (WARN_ON_ONCE(!skb))
223 iwl_txq_free_tso_page(trans, skb);
225 iwl_txq_free_tfd(trans, txq);
226 txq->read_ptr = iwl_txq_inc_wrap(trans, txq->read_ptr);
228 if (txq->read_ptr == txq->write_ptr &&
229 txq_id == trans->txqs.cmd.q_id)
230 iwl_pcie_clear_cmd_in_flight(trans);
233 while (!skb_queue_empty(&txq->overflow_q)) {
234 struct sk_buff *skb = __skb_dequeue(&txq->overflow_q);
236 iwl_op_mode_free_skb(trans->op_mode, skb);
239 spin_unlock_bh(&txq->lock);
241 /* just in case - this queue may have been stopped */
242 iwl_wake_queue(trans, txq);
246 * iwl_pcie_txq_free - Deallocate DMA queue.
247 * @txq: Transmit queue to deallocate.
249 * Empty queue by removing and destroying all BD's.
251 * 0-fill, but do not free "txq" descriptor structure.
253 static void iwl_pcie_txq_free(struct iwl_trans *trans, int txq_id)
255 struct iwl_txq *txq = trans->txqs.txq[txq_id];
256 struct device *dev = trans->dev;
262 iwl_pcie_txq_unmap(trans, txq_id);
264 /* De-alloc array of command/tx buffers */
265 if (txq_id == trans->txqs.cmd.q_id)
266 for (i = 0; i < txq->n_window; i++) {
267 kfree_sensitive(txq->entries[i].cmd);
268 kfree_sensitive(txq->entries[i].free_buf);
271 /* De-alloc circular buffer of TFDs */
273 dma_free_coherent(dev,
274 trans->txqs.tfd.size *
275 trans->trans_cfg->base_params->max_tfd_queue_size,
276 txq->tfds, txq->dma_addr);
280 dma_free_coherent(dev,
281 sizeof(*txq->first_tb_bufs) * txq->n_window,
282 txq->first_tb_bufs, txq->first_tb_dma);
288 del_timer_sync(&txq->stuck_timer);
290 /* 0-fill queue descriptor structure */
291 memset(txq, 0, sizeof(*txq));
294 void iwl_pcie_tx_start(struct iwl_trans *trans, u32 scd_base_addr)
296 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
297 int nq = trans->trans_cfg->base_params->num_of_queues;
300 int clear_dwords = (SCD_TRANS_TBL_OFFSET_QUEUE(nq) -
301 SCD_CONTEXT_MEM_LOWER_BOUND) / sizeof(u32);
303 /* make sure all queue are not stopped/used */
304 memset(trans->txqs.queue_stopped, 0,
305 sizeof(trans->txqs.queue_stopped));
306 memset(trans->txqs.queue_used, 0, sizeof(trans->txqs.queue_used));
308 trans_pcie->scd_base_addr =
309 iwl_read_prph(trans, SCD_SRAM_BASE_ADDR);
311 WARN_ON(scd_base_addr != 0 &&
312 scd_base_addr != trans_pcie->scd_base_addr);
314 /* reset context data, TX status and translation data */
315 iwl_trans_write_mem(trans, trans_pcie->scd_base_addr +
316 SCD_CONTEXT_MEM_LOWER_BOUND,
319 iwl_write_prph(trans, SCD_DRAM_BASE_ADDR,
320 trans->txqs.scd_bc_tbls.dma >> 10);
322 /* The chain extension of the SCD doesn't work well. This feature is
323 * enabled by default by the HW, so we need to disable it manually.
325 if (trans->trans_cfg->base_params->scd_chain_ext_wa)
326 iwl_write_prph(trans, SCD_CHAINEXT_EN, 0);
328 iwl_trans_ac_txq_enable(trans, trans->txqs.cmd.q_id,
329 trans->txqs.cmd.fifo,
330 trans->txqs.cmd.wdg_timeout);
332 /* Activate all Tx DMA/FIFO channels */
333 iwl_scd_activate_fifos(trans);
335 /* Enable DMA channel */
336 for (chan = 0; chan < FH_TCSR_CHNL_NUM; chan++)
337 iwl_write_direct32(trans, FH_TCSR_CHNL_TX_CONFIG_REG(chan),
338 FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
339 FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE);
341 /* Update FH chicken bits */
342 reg_val = iwl_read_direct32(trans, FH_TX_CHICKEN_BITS_REG);
343 iwl_write_direct32(trans, FH_TX_CHICKEN_BITS_REG,
344 reg_val | FH_TX_CHICKEN_BITS_SCD_AUTO_RETRY_EN);
346 /* Enable L1-Active */
347 if (trans->trans_cfg->device_family < IWL_DEVICE_FAMILY_8000)
348 iwl_clear_bits_prph(trans, APMG_PCIDEV_STT_REG,
349 APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
352 void iwl_trans_pcie_tx_reset(struct iwl_trans *trans)
354 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
358 * we should never get here in gen2 trans mode return early to avoid
359 * having invalid accesses
361 if (WARN_ON_ONCE(trans->trans_cfg->gen2))
364 for (txq_id = 0; txq_id < trans->trans_cfg->base_params->num_of_queues;
366 struct iwl_txq *txq = trans->txqs.txq[txq_id];
367 if (trans->trans_cfg->gen2)
368 iwl_write_direct64(trans,
369 FH_MEM_CBBC_QUEUE(trans, txq_id),
372 iwl_write_direct32(trans,
373 FH_MEM_CBBC_QUEUE(trans, txq_id),
375 iwl_pcie_txq_unmap(trans, txq_id);
380 /* Tell NIC where to find the "keep warm" buffer */
381 iwl_write_direct32(trans, FH_KW_MEM_ADDR_REG,
382 trans_pcie->kw.dma >> 4);
385 * Send 0 as the scd_base_addr since the device may have be reset
386 * while we were in WoWLAN in which case SCD_SRAM_BASE_ADDR will
389 iwl_pcie_tx_start(trans, 0);
392 static void iwl_pcie_tx_stop_fh(struct iwl_trans *trans)
394 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
398 spin_lock_bh(&trans_pcie->irq_lock);
400 if (!iwl_trans_grab_nic_access(trans))
403 /* Stop each Tx DMA channel */
404 for (ch = 0; ch < FH_TCSR_CHNL_NUM; ch++) {
405 iwl_write32(trans, FH_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0);
406 mask |= FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch);
409 /* Wait for DMA channels to be idle */
410 ret = iwl_poll_bit(trans, FH_TSSR_TX_STATUS_REG, mask, mask, 5000);
413 "Failing on timeout while stopping DMA channel %d [0x%08x]\n",
414 ch, iwl_read32(trans, FH_TSSR_TX_STATUS_REG));
416 iwl_trans_release_nic_access(trans);
419 spin_unlock_bh(&trans_pcie->irq_lock);
423 * iwl_pcie_tx_stop - Stop all Tx DMA channels
425 int iwl_pcie_tx_stop(struct iwl_trans *trans)
427 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
430 /* Turn off all Tx DMA fifos */
431 iwl_scd_deactivate_fifos(trans);
433 /* Turn off all Tx DMA channels */
434 iwl_pcie_tx_stop_fh(trans);
437 * This function can be called before the op_mode disabled the
438 * queues. This happens when we have an rfkill interrupt.
439 * Since we stop Tx altogether - mark the queues as stopped.
441 memset(trans->txqs.queue_stopped, 0,
442 sizeof(trans->txqs.queue_stopped));
443 memset(trans->txqs.queue_used, 0, sizeof(trans->txqs.queue_used));
445 /* This can happen: start_hw, stop_device */
446 if (!trans_pcie->txq_memory)
449 /* Unmap DMA from host system and free skb's */
450 for (txq_id = 0; txq_id < trans->trans_cfg->base_params->num_of_queues;
452 iwl_pcie_txq_unmap(trans, txq_id);
458 * iwl_trans_tx_free - Free TXQ Context
460 * Destroy all TX DMA queues and structures
462 void iwl_pcie_tx_free(struct iwl_trans *trans)
465 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
467 memset(trans->txqs.queue_used, 0, sizeof(trans->txqs.queue_used));
470 if (trans_pcie->txq_memory) {
472 txq_id < trans->trans_cfg->base_params->num_of_queues;
474 iwl_pcie_txq_free(trans, txq_id);
475 trans->txqs.txq[txq_id] = NULL;
479 kfree(trans_pcie->txq_memory);
480 trans_pcie->txq_memory = NULL;
482 iwl_pcie_free_dma_ptr(trans, &trans_pcie->kw);
484 iwl_pcie_free_dma_ptr(trans, &trans->txqs.scd_bc_tbls);
488 * iwl_pcie_tx_alloc - allocate TX context
489 * Allocate all Tx DMA structures and initialize them
491 static int iwl_pcie_tx_alloc(struct iwl_trans *trans)
494 int txq_id, slots_num;
495 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
496 u16 bc_tbls_size = trans->trans_cfg->base_params->num_of_queues;
498 if (WARN_ON(trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210))
501 bc_tbls_size *= sizeof(struct iwlagn_scd_bc_tbl);
503 /*It is not allowed to alloc twice, so warn when this happens.
504 * We cannot rely on the previous allocation, so free and fail */
505 if (WARN_ON(trans_pcie->txq_memory)) {
510 ret = iwl_pcie_alloc_dma_ptr(trans, &trans->txqs.scd_bc_tbls,
513 IWL_ERR(trans, "Scheduler BC Table allocation failed\n");
517 /* Alloc keep-warm buffer */
518 ret = iwl_pcie_alloc_dma_ptr(trans, &trans_pcie->kw, IWL_KW_SIZE);
520 IWL_ERR(trans, "Keep Warm allocation failed\n");
524 trans_pcie->txq_memory =
525 kcalloc(trans->trans_cfg->base_params->num_of_queues,
526 sizeof(struct iwl_txq), GFP_KERNEL);
527 if (!trans_pcie->txq_memory) {
528 IWL_ERR(trans, "Not enough memory for txq\n");
533 /* Alloc and init all Tx queues, including the command queue (#4/#9) */
534 for (txq_id = 0; txq_id < trans->trans_cfg->base_params->num_of_queues;
536 bool cmd_queue = (txq_id == trans->txqs.cmd.q_id);
539 slots_num = max_t(u32, IWL_CMD_QUEUE_SIZE,
540 trans->cfg->min_txq_size);
542 slots_num = max_t(u32, IWL_DEFAULT_QUEUE_SIZE,
543 trans->cfg->min_ba_txq_size);
544 trans->txqs.txq[txq_id] = &trans_pcie->txq_memory[txq_id];
545 ret = iwl_txq_alloc(trans, trans->txqs.txq[txq_id], slots_num,
548 IWL_ERR(trans, "Tx %d queue alloc failed\n", txq_id);
551 trans->txqs.txq[txq_id]->id = txq_id;
557 iwl_pcie_tx_free(trans);
562 int iwl_pcie_tx_init(struct iwl_trans *trans)
564 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
566 int txq_id, slots_num;
569 if (!trans_pcie->txq_memory) {
570 ret = iwl_pcie_tx_alloc(trans);
576 spin_lock_bh(&trans_pcie->irq_lock);
578 /* Turn off all Tx DMA fifos */
579 iwl_scd_deactivate_fifos(trans);
581 /* Tell NIC where to find the "keep warm" buffer */
582 iwl_write_direct32(trans, FH_KW_MEM_ADDR_REG,
583 trans_pcie->kw.dma >> 4);
585 spin_unlock_bh(&trans_pcie->irq_lock);
587 /* Alloc and init all Tx queues, including the command queue (#4/#9) */
588 for (txq_id = 0; txq_id < trans->trans_cfg->base_params->num_of_queues;
590 bool cmd_queue = (txq_id == trans->txqs.cmd.q_id);
593 slots_num = max_t(u32, IWL_CMD_QUEUE_SIZE,
594 trans->cfg->min_txq_size);
596 slots_num = max_t(u32, IWL_DEFAULT_QUEUE_SIZE,
597 trans->cfg->min_ba_txq_size);
598 ret = iwl_txq_init(trans, trans->txqs.txq[txq_id], slots_num,
601 IWL_ERR(trans, "Tx %d queue init failed\n", txq_id);
606 * Tell nic where to find circular buffer of TFDs for a
607 * given Tx queue, and enable the DMA channel used for that
609 * Circular buffer (TFD queue in DRAM) physical base address
611 iwl_write_direct32(trans, FH_MEM_CBBC_QUEUE(trans, txq_id),
612 trans->txqs.txq[txq_id]->dma_addr >> 8);
615 iwl_set_bits_prph(trans, SCD_GP_CTRL, SCD_GP_CTRL_AUTO_ACTIVE_MODE);
616 if (trans->trans_cfg->base_params->num_of_queues > 20)
617 iwl_set_bits_prph(trans, SCD_GP_CTRL,
618 SCD_GP_CTRL_ENABLE_31_QUEUES);
622 /*Upon error, free only if we allocated something */
624 iwl_pcie_tx_free(trans);
628 static int iwl_pcie_set_cmd_in_flight(struct iwl_trans *trans,
629 const struct iwl_host_cmd *cmd)
631 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
633 /* Make sure the NIC is still alive in the bus */
634 if (test_bit(STATUS_TRANS_DEAD, &trans->status))
637 if (!trans->trans_cfg->base_params->apmg_wake_up_wa)
641 * wake up the NIC to make sure that the firmware will see the host
642 * command - we will let the NIC sleep once all the host commands
643 * returned. This needs to be done only on NICs that have
644 * apmg_wake_up_wa set (see above.)
646 if (!_iwl_trans_pcie_grab_nic_access(trans))
650 * In iwl_trans_grab_nic_access(), we've acquired the reg_lock.
651 * There, we also returned immediately if cmd_hold_nic_awake is
652 * already true, so it's OK to unconditionally set it to true.
654 trans_pcie->cmd_hold_nic_awake = true;
655 spin_unlock(&trans_pcie->reg_lock);
661 * iwl_pcie_cmdq_reclaim - Reclaim TX command queue entries already Tx'd
663 * When FW advances 'R' index, all entries between old and new 'R' index
664 * need to be reclaimed. As result, some free space forms. If there is
665 * enough free space (> low mark), wake the stack that feeds us.
667 static void iwl_pcie_cmdq_reclaim(struct iwl_trans *trans, int txq_id, int idx)
669 struct iwl_txq *txq = trans->txqs.txq[txq_id];
673 lockdep_assert_held(&txq->lock);
675 idx = iwl_txq_get_cmd_index(txq, idx);
676 r = iwl_txq_get_cmd_index(txq, txq->read_ptr);
678 if (idx >= trans->trans_cfg->base_params->max_tfd_queue_size ||
679 (!iwl_txq_used(txq, idx))) {
680 WARN_ONCE(test_bit(txq_id, trans->txqs.queue_used),
681 "%s: Read index for DMA queue txq id (%d), index %d is out of range [0-%d] %d %d.\n",
682 __func__, txq_id, idx,
683 trans->trans_cfg->base_params->max_tfd_queue_size,
684 txq->write_ptr, txq->read_ptr);
688 for (idx = iwl_txq_inc_wrap(trans, idx); r != idx;
689 r = iwl_txq_inc_wrap(trans, r)) {
690 txq->read_ptr = iwl_txq_inc_wrap(trans, txq->read_ptr);
693 IWL_ERR(trans, "HCMD skipped: index (%d) %d %d\n",
694 idx, txq->write_ptr, r);
695 iwl_force_nmi(trans);
699 if (txq->read_ptr == txq->write_ptr)
700 iwl_pcie_clear_cmd_in_flight(trans);
702 iwl_txq_progress(txq);
705 static int iwl_pcie_txq_set_ratid_map(struct iwl_trans *trans, u16 ra_tid,
708 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
713 scd_q2ratid = ra_tid & SCD_QUEUE_RA_TID_MAP_RATID_MSK;
715 tbl_dw_addr = trans_pcie->scd_base_addr +
716 SCD_TRANS_TBL_OFFSET_QUEUE(txq_id);
718 tbl_dw = iwl_trans_read_mem32(trans, tbl_dw_addr);
721 tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF);
723 tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000);
725 iwl_trans_write_mem32(trans, tbl_dw_addr, tbl_dw);
730 /* Receiver address (actually, Rx station's index into station table),
731 * combined with Traffic ID (QOS priority), in format used by Tx Scheduler */
732 #define BUILD_RAxTID(sta_id, tid) (((sta_id) << 4) + (tid))
734 bool iwl_trans_pcie_txq_enable(struct iwl_trans *trans, int txq_id, u16 ssn,
735 const struct iwl_trans_txq_scd_cfg *cfg,
736 unsigned int wdg_timeout)
738 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
739 struct iwl_txq *txq = trans->txqs.txq[txq_id];
741 bool scd_bug = false;
743 if (test_and_set_bit(txq_id, trans->txqs.queue_used))
744 WARN_ONCE(1, "queue %d already used - expect issues", txq_id);
746 txq->wd_timeout = msecs_to_jiffies(wdg_timeout);
751 /* Disable the scheduler prior configuring the cmd queue */
752 if (txq_id == trans->txqs.cmd.q_id &&
753 trans_pcie->scd_set_active)
754 iwl_scd_enable_set_active(trans, 0);
756 /* Stop this Tx queue before configuring it */
757 iwl_scd_txq_set_inactive(trans, txq_id);
759 /* Set this queue as a chain-building queue unless it is CMD */
760 if (txq_id != trans->txqs.cmd.q_id)
761 iwl_scd_txq_set_chain(trans, txq_id);
763 if (cfg->aggregate) {
764 u16 ra_tid = BUILD_RAxTID(cfg->sta_id, cfg->tid);
766 /* Map receiver-address / traffic-ID to this queue */
767 iwl_pcie_txq_set_ratid_map(trans, ra_tid, txq_id);
769 /* enable aggregations for the queue */
770 iwl_scd_txq_enable_agg(trans, txq_id);
774 * disable aggregations for the queue, this will also
775 * make the ra_tid mapping configuration irrelevant
776 * since it is now a non-AGG queue.
778 iwl_scd_txq_disable_agg(trans, txq_id);
784 * If we need to move the SCD write pointer by steps of
785 * 0x40, 0x80 or 0xc0, it gets stuck. Avoids this and let
786 * the op_mode know by returning true later.
787 * Do this only in case cfg is NULL since this trick can
788 * be done only if we have DQA enabled which is true for mvm
789 * only. And mvm never sets a cfg pointer.
790 * This is really ugly, but this is the easiest way out for
791 * this sad hardware issue.
792 * This bug has been fixed on devices 9000 and up.
794 scd_bug = !trans->trans_cfg->mq_rx_supported &&
795 !((ssn - txq->write_ptr) & 0x3f) &&
796 (ssn != txq->write_ptr);
801 /* Place first TFD at index corresponding to start sequence number.
802 * Assumes that ssn_idx is valid (!= 0xFFF) */
803 txq->read_ptr = (ssn & 0xff);
804 txq->write_ptr = (ssn & 0xff);
805 iwl_write_direct32(trans, HBUS_TARG_WRPTR,
806 (ssn & 0xff) | (txq_id << 8));
809 u8 frame_limit = cfg->frame_limit;
811 iwl_write_prph(trans, SCD_QUEUE_RDPTR(txq_id), ssn);
813 /* Set up Tx window size and frame limit for this queue */
814 iwl_trans_write_mem32(trans, trans_pcie->scd_base_addr +
815 SCD_CONTEXT_QUEUE_OFFSET(txq_id), 0);
816 iwl_trans_write_mem32(trans,
817 trans_pcie->scd_base_addr +
818 SCD_CONTEXT_QUEUE_OFFSET(txq_id) + sizeof(u32),
819 SCD_QUEUE_CTX_REG2_VAL(WIN_SIZE, frame_limit) |
820 SCD_QUEUE_CTX_REG2_VAL(FRAME_LIMIT, frame_limit));
822 /* Set up status area in SRAM, map to Tx DMA/FIFO, activate */
823 iwl_write_prph(trans, SCD_QUEUE_STATUS_BITS(txq_id),
824 (1 << SCD_QUEUE_STTS_REG_POS_ACTIVE) |
825 (cfg->fifo << SCD_QUEUE_STTS_REG_POS_TXF) |
826 (1 << SCD_QUEUE_STTS_REG_POS_WSL) |
827 SCD_QUEUE_STTS_REG_MSK);
829 /* enable the scheduler for this queue (only) */
830 if (txq_id == trans->txqs.cmd.q_id &&
831 trans_pcie->scd_set_active)
832 iwl_scd_enable_set_active(trans, BIT(txq_id));
834 IWL_DEBUG_TX_QUEUES(trans,
835 "Activate queue %d on FIFO %d WrPtr: %d\n",
836 txq_id, fifo, ssn & 0xff);
838 IWL_DEBUG_TX_QUEUES(trans,
839 "Activate queue %d WrPtr: %d\n",
846 void iwl_trans_pcie_txq_set_shared_mode(struct iwl_trans *trans, u32 txq_id,
849 struct iwl_txq *txq = trans->txqs.txq[txq_id];
851 txq->ampdu = !shared_mode;
854 void iwl_trans_pcie_txq_disable(struct iwl_trans *trans, int txq_id,
857 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
858 u32 stts_addr = trans_pcie->scd_base_addr +
859 SCD_TX_STTS_QUEUE_OFFSET(txq_id);
860 static const u32 zero_val[4] = {};
862 trans->txqs.txq[txq_id]->frozen_expiry_remainder = 0;
863 trans->txqs.txq[txq_id]->frozen = false;
866 * Upon HW Rfkill - we stop the device, and then stop the queues
867 * in the op_mode. Just for the sake of the simplicity of the op_mode,
868 * allow the op_mode to call txq_disable after it already called
871 if (!test_and_clear_bit(txq_id, trans->txqs.queue_used)) {
872 WARN_ONCE(test_bit(STATUS_DEVICE_ENABLED, &trans->status),
873 "queue %d not used", txq_id);
878 iwl_scd_txq_set_inactive(trans, txq_id);
880 iwl_trans_write_mem(trans, stts_addr, (const void *)zero_val,
881 ARRAY_SIZE(zero_val));
884 iwl_pcie_txq_unmap(trans, txq_id);
885 trans->txqs.txq[txq_id]->ampdu = false;
887 IWL_DEBUG_TX_QUEUES(trans, "Deactivate queue %d\n", txq_id);
890 /*************** HOST COMMAND QUEUE FUNCTIONS *****/
893 * iwl_pcie_enqueue_hcmd - enqueue a uCode command
894 * @priv: device private data point
895 * @cmd: a pointer to the ucode command structure
897 * The function returns < 0 values to indicate the operation
898 * failed. On success, it returns the index (>= 0) of command in the
901 int iwl_pcie_enqueue_hcmd(struct iwl_trans *trans,
902 struct iwl_host_cmd *cmd)
904 struct iwl_txq *txq = trans->txqs.txq[trans->txqs.cmd.q_id];
905 struct iwl_device_cmd *out_cmd;
906 struct iwl_cmd_meta *out_meta;
907 void *dup_buf = NULL;
908 dma_addr_t phys_addr;
910 u16 copy_size, cmd_size, tb0_size;
911 bool had_nocopy = false;
912 u8 group_id = iwl_cmd_groupid(cmd->id);
915 const u8 *cmddata[IWL_MAX_CMD_TBS_PER_TFD];
916 u16 cmdlen[IWL_MAX_CMD_TBS_PER_TFD];
919 if (WARN(!trans->wide_cmd_header &&
920 group_id > IWL_ALWAYS_LONG_GROUP,
921 "unsupported wide command %#x\n", cmd->id))
925 copy_size = sizeof(struct iwl_cmd_header_wide);
926 cmd_size = sizeof(struct iwl_cmd_header_wide);
928 copy_size = sizeof(struct iwl_cmd_header);
929 cmd_size = sizeof(struct iwl_cmd_header);
932 /* need one for the header if the first is NOCOPY */
933 BUILD_BUG_ON(IWL_MAX_CMD_TBS_PER_TFD > IWL_NUM_OF_TBS - 1);
935 for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
936 cmddata[i] = cmd->data[i];
937 cmdlen[i] = cmd->len[i];
942 /* need at least IWL_FIRST_TB_SIZE copied */
943 if (copy_size < IWL_FIRST_TB_SIZE) {
944 int copy = IWL_FIRST_TB_SIZE - copy_size;
946 if (copy > cmdlen[i])
953 if (cmd->dataflags[i] & IWL_HCMD_DFL_NOCOPY) {
955 if (WARN_ON(cmd->dataflags[i] & IWL_HCMD_DFL_DUP)) {
959 } else if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP) {
961 * This is also a chunk that isn't copied
962 * to the static buffer so set had_nocopy.
966 /* only allowed once */
967 if (WARN_ON(dup_buf)) {
972 dup_buf = kmemdup(cmddata[i], cmdlen[i],
977 /* NOCOPY must not be followed by normal! */
978 if (WARN_ON(had_nocopy)) {
982 copy_size += cmdlen[i];
984 cmd_size += cmd->len[i];
988 * If any of the command structures end up being larger than
989 * the TFD_MAX_PAYLOAD_SIZE and they aren't dynamically
990 * allocated into separate TFDs, then we will need to
991 * increase the size of the buffers.
993 if (WARN(copy_size > TFD_MAX_PAYLOAD_SIZE,
994 "Command %s (%#x) is too large (%d bytes)\n",
995 iwl_get_cmd_string(trans, cmd->id),
996 cmd->id, copy_size)) {
1001 spin_lock_irqsave(&txq->lock, flags);
1003 if (iwl_txq_space(trans, txq) < ((cmd->flags & CMD_ASYNC) ? 2 : 1)) {
1004 spin_unlock_irqrestore(&txq->lock, flags);
1006 IWL_ERR(trans, "No space in command queue\n");
1007 iwl_op_mode_cmd_queue_full(trans->op_mode);
1012 idx = iwl_txq_get_cmd_index(txq, txq->write_ptr);
1013 out_cmd = txq->entries[idx].cmd;
1014 out_meta = &txq->entries[idx].meta;
1016 memset(out_meta, 0, sizeof(*out_meta)); /* re-initialize to NULL */
1017 if (cmd->flags & CMD_WANT_SKB)
1018 out_meta->source = cmd;
1020 /* set up the header */
1021 if (group_id != 0) {
1022 out_cmd->hdr_wide.cmd = iwl_cmd_opcode(cmd->id);
1023 out_cmd->hdr_wide.group_id = group_id;
1024 out_cmd->hdr_wide.version = iwl_cmd_version(cmd->id);
1025 out_cmd->hdr_wide.length =
1026 cpu_to_le16(cmd_size -
1027 sizeof(struct iwl_cmd_header_wide));
1028 out_cmd->hdr_wide.reserved = 0;
1029 out_cmd->hdr_wide.sequence =
1030 cpu_to_le16(QUEUE_TO_SEQ(trans->txqs.cmd.q_id) |
1031 INDEX_TO_SEQ(txq->write_ptr));
1033 cmd_pos = sizeof(struct iwl_cmd_header_wide);
1034 copy_size = sizeof(struct iwl_cmd_header_wide);
1036 out_cmd->hdr.cmd = iwl_cmd_opcode(cmd->id);
1037 out_cmd->hdr.sequence =
1038 cpu_to_le16(QUEUE_TO_SEQ(trans->txqs.cmd.q_id) |
1039 INDEX_TO_SEQ(txq->write_ptr));
1040 out_cmd->hdr.group_id = 0;
1042 cmd_pos = sizeof(struct iwl_cmd_header);
1043 copy_size = sizeof(struct iwl_cmd_header);
1046 /* and copy the data that needs to be copied */
1047 for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
1053 /* copy everything if not nocopy/dup */
1054 if (!(cmd->dataflags[i] & (IWL_HCMD_DFL_NOCOPY |
1055 IWL_HCMD_DFL_DUP))) {
1058 memcpy((u8 *)out_cmd + cmd_pos, cmd->data[i], copy);
1065 * Otherwise we need at least IWL_FIRST_TB_SIZE copied
1066 * in total (for bi-directional DMA), but copy up to what
1067 * we can fit into the payload for debug dump purposes.
1069 copy = min_t(int, TFD_MAX_PAYLOAD_SIZE - cmd_pos, cmd->len[i]);
1071 memcpy((u8 *)out_cmd + cmd_pos, cmd->data[i], copy);
1074 /* However, treat copy_size the proper way, we need it below */
1075 if (copy_size < IWL_FIRST_TB_SIZE) {
1076 copy = IWL_FIRST_TB_SIZE - copy_size;
1078 if (copy > cmd->len[i])
1085 "Sending command %s (%.2x.%.2x), seq: 0x%04X, %d bytes at %d[%d]:%d\n",
1086 iwl_get_cmd_string(trans, cmd->id),
1087 group_id, out_cmd->hdr.cmd,
1088 le16_to_cpu(out_cmd->hdr.sequence),
1089 cmd_size, txq->write_ptr, idx, trans->txqs.cmd.q_id);
1091 /* start the TFD with the minimum copy bytes */
1092 tb0_size = min_t(int, copy_size, IWL_FIRST_TB_SIZE);
1093 memcpy(&txq->first_tb_bufs[idx], &out_cmd->hdr, tb0_size);
1094 iwl_pcie_txq_build_tfd(trans, txq,
1095 iwl_txq_get_first_tb_dma(txq, idx),
1098 /* map first command fragment, if any remains */
1099 if (copy_size > tb0_size) {
1100 phys_addr = dma_map_single(trans->dev,
1101 ((u8 *)&out_cmd->hdr) + tb0_size,
1102 copy_size - tb0_size,
1104 if (dma_mapping_error(trans->dev, phys_addr)) {
1105 iwl_txq_gen1_tfd_unmap(trans, out_meta, txq,
1111 iwl_pcie_txq_build_tfd(trans, txq, phys_addr,
1112 copy_size - tb0_size, false);
1115 /* map the remaining (adjusted) nocopy/dup fragments */
1116 for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
1117 void *data = (void *)(uintptr_t)cmddata[i];
1121 if (!(cmd->dataflags[i] & (IWL_HCMD_DFL_NOCOPY |
1124 if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP)
1126 phys_addr = dma_map_single(trans->dev, data,
1127 cmdlen[i], DMA_TO_DEVICE);
1128 if (dma_mapping_error(trans->dev, phys_addr)) {
1129 iwl_txq_gen1_tfd_unmap(trans, out_meta, txq,
1135 iwl_pcie_txq_build_tfd(trans, txq, phys_addr, cmdlen[i], false);
1138 BUILD_BUG_ON(IWL_TFH_NUM_TBS > sizeof(out_meta->tbs) * BITS_PER_BYTE);
1139 out_meta->flags = cmd->flags;
1140 if (WARN_ON_ONCE(txq->entries[idx].free_buf))
1141 kfree_sensitive(txq->entries[idx].free_buf);
1142 txq->entries[idx].free_buf = dup_buf;
1144 trace_iwlwifi_dev_hcmd(trans->dev, cmd, cmd_size, &out_cmd->hdr_wide);
1146 /* start timer if queue currently empty */
1147 if (txq->read_ptr == txq->write_ptr && txq->wd_timeout)
1148 mod_timer(&txq->stuck_timer, jiffies + txq->wd_timeout);
1150 ret = iwl_pcie_set_cmd_in_flight(trans, cmd);
1156 /* Increment and update queue's write index */
1157 txq->write_ptr = iwl_txq_inc_wrap(trans, txq->write_ptr);
1158 iwl_pcie_txq_inc_wr_ptr(trans, txq);
1161 spin_unlock_irqrestore(&txq->lock, flags);
1169 * iwl_pcie_hcmd_complete - Pull unused buffers off the queue and reclaim them
1170 * @rxb: Rx buffer to reclaim
1172 void iwl_pcie_hcmd_complete(struct iwl_trans *trans,
1173 struct iwl_rx_cmd_buffer *rxb)
1175 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1176 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
1179 int txq_id = SEQ_TO_QUEUE(sequence);
1180 int index = SEQ_TO_INDEX(sequence);
1182 struct iwl_device_cmd *cmd;
1183 struct iwl_cmd_meta *meta;
1184 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1185 struct iwl_txq *txq = trans->txqs.txq[trans->txqs.cmd.q_id];
1187 /* If a Tx command is being handled and it isn't in the actual
1188 * command queue then there a command routing bug has been introduced
1189 * in the queue management code. */
1190 if (WARN(txq_id != trans->txqs.cmd.q_id,
1191 "wrong command queue %d (should be %d), sequence 0x%X readp=%d writep=%d\n",
1192 txq_id, trans->txqs.cmd.q_id, sequence, txq->read_ptr,
1194 iwl_print_hex_error(trans, pkt, 32);
1198 spin_lock_bh(&txq->lock);
1200 cmd_index = iwl_txq_get_cmd_index(txq, index);
1201 cmd = txq->entries[cmd_index].cmd;
1202 meta = &txq->entries[cmd_index].meta;
1203 group_id = cmd->hdr.group_id;
1204 cmd_id = WIDE_ID(group_id, cmd->hdr.cmd);
1206 if (trans->trans_cfg->gen2)
1207 iwl_txq_gen2_tfd_unmap(trans, meta,
1208 iwl_txq_get_tfd(trans, txq, index));
1210 iwl_txq_gen1_tfd_unmap(trans, meta, txq, index);
1212 /* Input error checking is done when commands are added to queue. */
1213 if (meta->flags & CMD_WANT_SKB) {
1214 struct page *p = rxb_steal_page(rxb);
1216 meta->source->resp_pkt = pkt;
1217 meta->source->_rx_page_addr = (unsigned long)page_address(p);
1218 meta->source->_rx_page_order = trans_pcie->rx_page_order;
1221 if (meta->flags & CMD_WANT_ASYNC_CALLBACK)
1222 iwl_op_mode_async_cb(trans->op_mode, cmd);
1224 iwl_pcie_cmdq_reclaim(trans, txq_id, index);
1226 if (!(meta->flags & CMD_ASYNC)) {
1227 if (!test_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status)) {
1229 "HCMD_ACTIVE already clear for command %s\n",
1230 iwl_get_cmd_string(trans, cmd_id));
1232 clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
1233 IWL_DEBUG_INFO(trans, "Clearing HCMD_ACTIVE for command %s\n",
1234 iwl_get_cmd_string(trans, cmd_id));
1235 wake_up(&trans->wait_command_queue);
1240 spin_unlock_bh(&txq->lock);
1243 static int iwl_fill_data_tbs(struct iwl_trans *trans, struct sk_buff *skb,
1244 struct iwl_txq *txq, u8 hdr_len,
1245 struct iwl_cmd_meta *out_meta)
1251 * Set up TFD's third entry to point directly to remainder
1252 * of skb's head, if any
1254 head_tb_len = skb_headlen(skb) - hdr_len;
1256 if (head_tb_len > 0) {
1257 dma_addr_t tb_phys = dma_map_single(trans->dev,
1258 skb->data + hdr_len,
1259 head_tb_len, DMA_TO_DEVICE);
1260 if (unlikely(dma_mapping_error(trans->dev, tb_phys)))
1262 trace_iwlwifi_dev_tx_tb(trans->dev, skb, skb->data + hdr_len,
1263 tb_phys, head_tb_len);
1264 iwl_pcie_txq_build_tfd(trans, txq, tb_phys, head_tb_len, false);
1267 /* set up the remaining entries to point to the data */
1268 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1269 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1273 if (!skb_frag_size(frag))
1276 tb_phys = skb_frag_dma_map(trans->dev, frag, 0,
1277 skb_frag_size(frag), DMA_TO_DEVICE);
1279 if (unlikely(dma_mapping_error(trans->dev, tb_phys)))
1281 trace_iwlwifi_dev_tx_tb(trans->dev, skb, skb_frag_address(frag),
1282 tb_phys, skb_frag_size(frag));
1283 tb_idx = iwl_pcie_txq_build_tfd(trans, txq, tb_phys,
1284 skb_frag_size(frag), false);
1288 out_meta->tbs |= BIT(tb_idx);
1295 static int iwl_fill_data_tbs_amsdu(struct iwl_trans *trans, struct sk_buff *skb,
1296 struct iwl_txq *txq, u8 hdr_len,
1297 struct iwl_cmd_meta *out_meta,
1298 struct iwl_device_tx_cmd *dev_cmd,
1301 struct iwl_tx_cmd *tx_cmd = (void *)dev_cmd->payload;
1302 struct ieee80211_hdr *hdr = (void *)skb->data;
1303 unsigned int snap_ip_tcp_hdrlen, ip_hdrlen, total_len, hdr_room;
1304 unsigned int mss = skb_shinfo(skb)->gso_size;
1305 u16 length, iv_len, amsdu_pad;
1307 struct iwl_tso_hdr_page *hdr_page;
1310 /* if the packet is protected, then it must be CCMP or GCMP */
1311 BUILD_BUG_ON(IEEE80211_CCMP_HDR_LEN != IEEE80211_GCMP_HDR_LEN);
1312 iv_len = ieee80211_has_protected(hdr->frame_control) ?
1313 IEEE80211_CCMP_HDR_LEN : 0;
1315 trace_iwlwifi_dev_tx(trans->dev, skb,
1316 iwl_txq_get_tfd(trans, txq, txq->write_ptr),
1317 trans->txqs.tfd.size,
1318 &dev_cmd->hdr, IWL_FIRST_TB_SIZE + tb1_len, 0);
1320 ip_hdrlen = skb_transport_header(skb) - skb_network_header(skb);
1321 snap_ip_tcp_hdrlen = 8 + ip_hdrlen + tcp_hdrlen(skb);
1322 total_len = skb->len - snap_ip_tcp_hdrlen - hdr_len - iv_len;
1325 /* total amount of header we may need for this A-MSDU */
1326 hdr_room = DIV_ROUND_UP(total_len, mss) *
1327 (3 + snap_ip_tcp_hdrlen + sizeof(struct ethhdr)) + iv_len;
1329 /* Our device supports 9 segments at most, it will fit in 1 page */
1330 hdr_page = get_page_hdr(trans, hdr_room, skb);
1334 start_hdr = hdr_page->pos;
1335 memcpy(hdr_page->pos, skb->data + hdr_len, iv_len);
1336 hdr_page->pos += iv_len;
1339 * Pull the ieee80211 header + IV to be able to use TSO core,
1340 * we will restore it for the tx_status flow.
1342 skb_pull(skb, hdr_len + iv_len);
1345 * Remove the length of all the headers that we don't actually
1346 * have in the MPDU by themselves, but that we duplicate into
1347 * all the different MSDUs inside the A-MSDU.
1349 le16_add_cpu(&tx_cmd->len, -snap_ip_tcp_hdrlen);
1351 tso_start(skb, &tso);
1354 /* this is the data left for this subframe */
1355 unsigned int data_left =
1356 min_t(unsigned int, mss, total_len);
1357 unsigned int hdr_tb_len;
1358 dma_addr_t hdr_tb_phys;
1359 u8 *subf_hdrs_start = hdr_page->pos;
1361 total_len -= data_left;
1363 memset(hdr_page->pos, 0, amsdu_pad);
1364 hdr_page->pos += amsdu_pad;
1365 amsdu_pad = (4 - (sizeof(struct ethhdr) + snap_ip_tcp_hdrlen +
1367 ether_addr_copy(hdr_page->pos, ieee80211_get_DA(hdr));
1368 hdr_page->pos += ETH_ALEN;
1369 ether_addr_copy(hdr_page->pos, ieee80211_get_SA(hdr));
1370 hdr_page->pos += ETH_ALEN;
1372 length = snap_ip_tcp_hdrlen + data_left;
1373 *((__be16 *)hdr_page->pos) = cpu_to_be16(length);
1374 hdr_page->pos += sizeof(length);
1377 * This will copy the SNAP as well which will be considered
1380 tso_build_hdr(skb, hdr_page->pos, &tso, data_left, !total_len);
1382 hdr_page->pos += snap_ip_tcp_hdrlen;
1384 hdr_tb_len = hdr_page->pos - start_hdr;
1385 hdr_tb_phys = dma_map_single(trans->dev, start_hdr,
1386 hdr_tb_len, DMA_TO_DEVICE);
1387 if (unlikely(dma_mapping_error(trans->dev, hdr_tb_phys)))
1389 iwl_pcie_txq_build_tfd(trans, txq, hdr_tb_phys,
1391 trace_iwlwifi_dev_tx_tb(trans->dev, skb, start_hdr,
1392 hdr_tb_phys, hdr_tb_len);
1393 /* add this subframe's headers' length to the tx_cmd */
1394 le16_add_cpu(&tx_cmd->len, hdr_page->pos - subf_hdrs_start);
1396 /* prepare the start_hdr for the next subframe */
1397 start_hdr = hdr_page->pos;
1399 /* put the payload */
1401 unsigned int size = min_t(unsigned int, tso.size,
1405 tb_phys = dma_map_single(trans->dev, tso.data,
1406 size, DMA_TO_DEVICE);
1407 if (unlikely(dma_mapping_error(trans->dev, tb_phys)))
1410 iwl_pcie_txq_build_tfd(trans, txq, tb_phys,
1412 trace_iwlwifi_dev_tx_tb(trans->dev, skb, tso.data,
1416 tso_build_data(skb, &tso, size);
1420 /* re -add the WiFi header and IV */
1421 skb_push(skb, hdr_len + iv_len);
1425 #else /* CONFIG_INET */
1426 static int iwl_fill_data_tbs_amsdu(struct iwl_trans *trans, struct sk_buff *skb,
1427 struct iwl_txq *txq, u8 hdr_len,
1428 struct iwl_cmd_meta *out_meta,
1429 struct iwl_device_tx_cmd *dev_cmd,
1432 /* No A-MSDU without CONFIG_INET */
1437 #endif /* CONFIG_INET */
1439 int iwl_trans_pcie_tx(struct iwl_trans *trans, struct sk_buff *skb,
1440 struct iwl_device_tx_cmd *dev_cmd, int txq_id)
1442 struct ieee80211_hdr *hdr;
1443 struct iwl_tx_cmd *tx_cmd = (struct iwl_tx_cmd *)dev_cmd->payload;
1444 struct iwl_cmd_meta *out_meta;
1445 struct iwl_txq *txq;
1446 dma_addr_t tb0_phys, tb1_phys, scratch_phys;
1450 bool wait_write_ptr;
1456 txq = trans->txqs.txq[txq_id];
1458 if (WARN_ONCE(!test_bit(txq_id, trans->txqs.queue_used),
1459 "TX on unused queue %d\n", txq_id))
1462 if (skb_is_nonlinear(skb) &&
1463 skb_shinfo(skb)->nr_frags > IWL_TRANS_MAX_FRAGS(trans) &&
1464 __skb_linearize(skb))
1467 /* mac80211 always puts the full header into the SKB's head,
1468 * so there's no need to check if it's readable there
1470 hdr = (struct ieee80211_hdr *)skb->data;
1471 fc = hdr->frame_control;
1472 hdr_len = ieee80211_hdrlen(fc);
1474 spin_lock(&txq->lock);
1476 if (iwl_txq_space(trans, txq) < txq->high_mark) {
1477 iwl_txq_stop(trans, txq);
1479 /* don't put the packet on the ring, if there is no room */
1480 if (unlikely(iwl_txq_space(trans, txq) < 3)) {
1481 struct iwl_device_tx_cmd **dev_cmd_ptr;
1483 dev_cmd_ptr = (void *)((u8 *)skb->cb +
1484 trans->txqs.dev_cmd_offs);
1486 *dev_cmd_ptr = dev_cmd;
1487 __skb_queue_tail(&txq->overflow_q, skb);
1489 spin_unlock(&txq->lock);
1494 /* In AGG mode, the index in the ring must correspond to the WiFi
1495 * sequence number. This is a HW requirements to help the SCD to parse
1497 * Check here that the packets are in the right place on the ring.
1499 wifi_seq = IEEE80211_SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
1500 WARN_ONCE(txq->ampdu &&
1501 (wifi_seq & 0xff) != txq->write_ptr,
1502 "Q: %d WiFi Seq %d tfdNum %d",
1503 txq_id, wifi_seq, txq->write_ptr);
1505 /* Set up driver data for this TFD */
1506 txq->entries[txq->write_ptr].skb = skb;
1507 txq->entries[txq->write_ptr].cmd = dev_cmd;
1509 dev_cmd->hdr.sequence =
1510 cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
1511 INDEX_TO_SEQ(txq->write_ptr)));
1513 tb0_phys = iwl_txq_get_first_tb_dma(txq, txq->write_ptr);
1514 scratch_phys = tb0_phys + sizeof(struct iwl_cmd_header) +
1515 offsetof(struct iwl_tx_cmd, scratch);
1517 tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
1518 tx_cmd->dram_msb_ptr = iwl_get_dma_hi_addr(scratch_phys);
1520 /* Set up first empty entry in queue's array of Tx/cmd buffers */
1521 out_meta = &txq->entries[txq->write_ptr].meta;
1522 out_meta->flags = 0;
1525 * The second TB (tb1) points to the remainder of the TX command
1526 * and the 802.11 header - dword aligned size
1527 * (This calculation modifies the TX command, so do it before the
1528 * setup of the first TB)
1530 len = sizeof(struct iwl_tx_cmd) + sizeof(struct iwl_cmd_header) +
1531 hdr_len - IWL_FIRST_TB_SIZE;
1532 /* do not align A-MSDU to dword as the subframe header aligns it */
1533 amsdu = ieee80211_is_data_qos(fc) &&
1534 (*ieee80211_get_qos_ctl(hdr) &
1535 IEEE80211_QOS_CTL_A_MSDU_PRESENT);
1537 tb1_len = ALIGN(len, 4);
1538 /* Tell NIC about any 2-byte padding after MAC header */
1540 tx_cmd->tx_flags |= cpu_to_le32(TX_CMD_FLG_MH_PAD);
1546 * The first TB points to bi-directional DMA data, we'll
1547 * memcpy the data into it later.
1549 iwl_pcie_txq_build_tfd(trans, txq, tb0_phys,
1550 IWL_FIRST_TB_SIZE, true);
1552 /* there must be data left over for TB1 or this code must be changed */
1553 BUILD_BUG_ON(sizeof(struct iwl_tx_cmd) < IWL_FIRST_TB_SIZE);
1554 BUILD_BUG_ON(sizeof(struct iwl_cmd_header) +
1555 offsetofend(struct iwl_tx_cmd, scratch) >
1558 /* map the data for TB1 */
1559 tb1_addr = ((u8 *)&dev_cmd->hdr) + IWL_FIRST_TB_SIZE;
1560 tb1_phys = dma_map_single(trans->dev, tb1_addr, tb1_len, DMA_TO_DEVICE);
1561 if (unlikely(dma_mapping_error(trans->dev, tb1_phys)))
1563 iwl_pcie_txq_build_tfd(trans, txq, tb1_phys, tb1_len, false);
1565 trace_iwlwifi_dev_tx(trans->dev, skb,
1566 iwl_txq_get_tfd(trans, txq, txq->write_ptr),
1567 trans->txqs.tfd.size,
1568 &dev_cmd->hdr, IWL_FIRST_TB_SIZE + tb1_len,
1572 * If gso_size wasn't set, don't give the frame "amsdu treatment"
1573 * (adding subframes, etc.).
1574 * This can happen in some testing flows when the amsdu was already
1575 * pre-built, and we just need to send the resulting skb.
1577 if (amsdu && skb_shinfo(skb)->gso_size) {
1578 if (unlikely(iwl_fill_data_tbs_amsdu(trans, skb, txq, hdr_len,
1583 struct sk_buff *frag;
1585 if (unlikely(iwl_fill_data_tbs(trans, skb, txq, hdr_len,
1589 skb_walk_frags(skb, frag) {
1590 if (unlikely(iwl_fill_data_tbs(trans, frag, txq, 0,
1596 /* building the A-MSDU might have changed this data, so memcpy it now */
1597 memcpy(&txq->first_tb_bufs[txq->write_ptr], dev_cmd, IWL_FIRST_TB_SIZE);
1599 tfd = iwl_txq_get_tfd(trans, txq, txq->write_ptr);
1600 /* Set up entry for this TFD in Tx byte-count array */
1601 iwl_txq_gen1_update_byte_cnt_tbl(trans, txq, le16_to_cpu(tx_cmd->len),
1602 iwl_txq_gen1_tfd_get_num_tbs(trans,
1605 wait_write_ptr = ieee80211_has_morefrags(fc);
1607 /* start timer if queue currently empty */
1608 if (txq->read_ptr == txq->write_ptr && txq->wd_timeout) {
1610 * If the TXQ is active, then set the timer, if not,
1611 * set the timer in remainder so that the timer will
1612 * be armed with the right value when the station will
1616 mod_timer(&txq->stuck_timer,
1617 jiffies + txq->wd_timeout);
1619 txq->frozen_expiry_remainder = txq->wd_timeout;
1622 /* Tell device the write index *just past* this latest filled TFD */
1623 txq->write_ptr = iwl_txq_inc_wrap(trans, txq->write_ptr);
1624 if (!wait_write_ptr)
1625 iwl_pcie_txq_inc_wr_ptr(trans, txq);
1628 * At this point the frame is "transmitted" successfully
1629 * and we will get a TX status notification eventually.
1631 spin_unlock(&txq->lock);
1634 iwl_txq_gen1_tfd_unmap(trans, out_meta, txq, txq->write_ptr);
1635 spin_unlock(&txq->lock);