xref: /OK3568_Linux_fs/kernel/drivers/net/wireless/intel/iwlwifi/pcie/tx.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /******************************************************************************
2*4882a593Smuzhiyun  *
3*4882a593Smuzhiyun  * This file is provided under a dual BSD/GPLv2 license.  When using or
4*4882a593Smuzhiyun  * redistributing this file, you may do so under either license.
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * GPL LICENSE SUMMARY
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Copyright(c) 2003 - 2014 Intel Corporation. All rights reserved.
9*4882a593Smuzhiyun  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
10*4882a593Smuzhiyun  * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
11*4882a593Smuzhiyun  * Copyright(c) 2018 - 2020 Intel Corporation
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  * This program is free software; you can redistribute it and/or modify it
14*4882a593Smuzhiyun  * under the terms of version 2 of the GNU General Public License as
15*4882a593Smuzhiyun  * published by the Free Software Foundation.
16*4882a593Smuzhiyun  *
17*4882a593Smuzhiyun  * This program is distributed in the hope that it will be useful, but WITHOUT
18*4882a593Smuzhiyun  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19*4882a593Smuzhiyun  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
20*4882a593Smuzhiyun  * more details.
21*4882a593Smuzhiyun  *
22*4882a593Smuzhiyun  * The full GNU General Public License is included in this distribution in the
23*4882a593Smuzhiyun  * file called COPYING.
24*4882a593Smuzhiyun  *
25*4882a593Smuzhiyun  * Contact Information:
26*4882a593Smuzhiyun  *  Intel Linux Wireless <linuxwifi@intel.com>
27*4882a593Smuzhiyun  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
28*4882a593Smuzhiyun  *
29*4882a593Smuzhiyun  * BSD LICENSE
30*4882a593Smuzhiyun  *
31*4882a593Smuzhiyun  * Copyright(c) 2003 - 2014 Intel Corporation. All rights reserved.
32*4882a593Smuzhiyun  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
33*4882a593Smuzhiyun  * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
34*4882a593Smuzhiyun  * Copyright(c) 2018 - 2020 Intel Corporation
35*4882a593Smuzhiyun  * All rights reserved.
36*4882a593Smuzhiyun  *
37*4882a593Smuzhiyun  * Redistribution and use in source and binary forms, with or without
38*4882a593Smuzhiyun  * modification, are permitted provided that the following conditions
39*4882a593Smuzhiyun  * are met:
40*4882a593Smuzhiyun  *
41*4882a593Smuzhiyun  *  * Redistributions of source code must retain the above copyright
42*4882a593Smuzhiyun  *    notice, this list of conditions and the following disclaimer.
43*4882a593Smuzhiyun  *  * Redistributions in binary form must reproduce the above copyright
44*4882a593Smuzhiyun  *    notice, this list of conditions and the following disclaimer in
45*4882a593Smuzhiyun  *    the documentation and/or other materials provided with the
46*4882a593Smuzhiyun  *    distribution.
47*4882a593Smuzhiyun  *  * Neither the name Intel Corporation nor the names of its
48*4882a593Smuzhiyun  *    contributors may be used to endorse or promote products derived
49*4882a593Smuzhiyun  *    from this software without specific prior written permission.
50*4882a593Smuzhiyun  *
51*4882a593Smuzhiyun  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
52*4882a593Smuzhiyun  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
53*4882a593Smuzhiyun  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
54*4882a593Smuzhiyun  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
55*4882a593Smuzhiyun  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
56*4882a593Smuzhiyun  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
57*4882a593Smuzhiyun  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58*4882a593Smuzhiyun  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59*4882a593Smuzhiyun  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60*4882a593Smuzhiyun  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
61*4882a593Smuzhiyun  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62*4882a593Smuzhiyun  *
63*4882a593Smuzhiyun  *****************************************************************************/
64*4882a593Smuzhiyun #include <linux/etherdevice.h>
65*4882a593Smuzhiyun #include <linux/ieee80211.h>
66*4882a593Smuzhiyun #include <linux/slab.h>
67*4882a593Smuzhiyun #include <linux/sched.h>
68*4882a593Smuzhiyun #include <net/ip6_checksum.h>
69*4882a593Smuzhiyun #include <net/tso.h>
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun #include "iwl-debug.h"
72*4882a593Smuzhiyun #include "iwl-csr.h"
73*4882a593Smuzhiyun #include "iwl-prph.h"
74*4882a593Smuzhiyun #include "iwl-io.h"
75*4882a593Smuzhiyun #include "iwl-scd.h"
76*4882a593Smuzhiyun #include "iwl-op-mode.h"
77*4882a593Smuzhiyun #include "internal.h"
78*4882a593Smuzhiyun #include "fw/api/tx.h"
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun /*************** DMA-QUEUE-GENERAL-FUNCTIONS  *****
81*4882a593Smuzhiyun  * DMA services
82*4882a593Smuzhiyun  *
83*4882a593Smuzhiyun  * Theory of operation
84*4882a593Smuzhiyun  *
85*4882a593Smuzhiyun  * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
86*4882a593Smuzhiyun  * of buffer descriptors, each of which points to one or more data buffers for
87*4882a593Smuzhiyun  * the device to read from or fill.  Driver and device exchange status of each
88*4882a593Smuzhiyun  * queue via "read" and "write" pointers.  Driver keeps minimum of 2 empty
89*4882a593Smuzhiyun  * entries in each circular buffer, to protect against confusing empty and full
90*4882a593Smuzhiyun  * queue states.
91*4882a593Smuzhiyun  *
92*4882a593Smuzhiyun  * The device reads or writes the data in the queues via the device's several
93*4882a593Smuzhiyun  * DMA/FIFO channels.  Each queue is mapped to a single DMA channel.
94*4882a593Smuzhiyun  *
95*4882a593Smuzhiyun  * For Tx queue, there are low mark and high mark limits. If, after queuing
96*4882a593Smuzhiyun  * the packet for Tx, free space become < low mark, Tx queue stopped. When
97*4882a593Smuzhiyun  * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
98*4882a593Smuzhiyun  * Tx queue resumed.
99*4882a593Smuzhiyun  *
100*4882a593Smuzhiyun  ***************************************************/
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 
iwl_pcie_alloc_dma_ptr(struct iwl_trans * trans,struct iwl_dma_ptr * ptr,size_t size)103*4882a593Smuzhiyun int iwl_pcie_alloc_dma_ptr(struct iwl_trans *trans,
104*4882a593Smuzhiyun 			   struct iwl_dma_ptr *ptr, size_t size)
105*4882a593Smuzhiyun {
106*4882a593Smuzhiyun 	if (WARN_ON(ptr->addr))
107*4882a593Smuzhiyun 		return -EINVAL;
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 	ptr->addr = dma_alloc_coherent(trans->dev, size,
110*4882a593Smuzhiyun 				       &ptr->dma, GFP_KERNEL);
111*4882a593Smuzhiyun 	if (!ptr->addr)
112*4882a593Smuzhiyun 		return -ENOMEM;
113*4882a593Smuzhiyun 	ptr->size = size;
114*4882a593Smuzhiyun 	return 0;
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun 
iwl_pcie_free_dma_ptr(struct iwl_trans * trans,struct iwl_dma_ptr * ptr)117*4882a593Smuzhiyun void iwl_pcie_free_dma_ptr(struct iwl_trans *trans, struct iwl_dma_ptr *ptr)
118*4882a593Smuzhiyun {
119*4882a593Smuzhiyun 	if (unlikely(!ptr->addr))
120*4882a593Smuzhiyun 		return;
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun 	dma_free_coherent(trans->dev, ptr->size, ptr->addr, ptr->dma);
123*4882a593Smuzhiyun 	memset(ptr, 0, sizeof(*ptr));
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun /*
127*4882a593Smuzhiyun  * iwl_pcie_txq_inc_wr_ptr - Send new write index to hardware
128*4882a593Smuzhiyun  */
iwl_pcie_txq_inc_wr_ptr(struct iwl_trans * trans,struct iwl_txq * txq)129*4882a593Smuzhiyun static void iwl_pcie_txq_inc_wr_ptr(struct iwl_trans *trans,
130*4882a593Smuzhiyun 				    struct iwl_txq *txq)
131*4882a593Smuzhiyun {
132*4882a593Smuzhiyun 	u32 reg = 0;
133*4882a593Smuzhiyun 	int txq_id = txq->id;
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	lockdep_assert_held(&txq->lock);
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 	/*
138*4882a593Smuzhiyun 	 * explicitly wake up the NIC if:
139*4882a593Smuzhiyun 	 * 1. shadow registers aren't enabled
140*4882a593Smuzhiyun 	 * 2. NIC is woken up for CMD regardless of shadow outside this function
141*4882a593Smuzhiyun 	 * 3. there is a chance that the NIC is asleep
142*4882a593Smuzhiyun 	 */
143*4882a593Smuzhiyun 	if (!trans->trans_cfg->base_params->shadow_reg_enable &&
144*4882a593Smuzhiyun 	    txq_id != trans->txqs.cmd.q_id &&
145*4882a593Smuzhiyun 	    test_bit(STATUS_TPOWER_PMI, &trans->status)) {
146*4882a593Smuzhiyun 		/*
147*4882a593Smuzhiyun 		 * wake up nic if it's powered down ...
148*4882a593Smuzhiyun 		 * uCode will wake up, and interrupt us again, so next
149*4882a593Smuzhiyun 		 * time we'll skip this part.
150*4882a593Smuzhiyun 		 */
151*4882a593Smuzhiyun 		reg = iwl_read32(trans, CSR_UCODE_DRV_GP1);
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 		if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
154*4882a593Smuzhiyun 			IWL_DEBUG_INFO(trans, "Tx queue %d requesting wakeup, GP1 = 0x%x\n",
155*4882a593Smuzhiyun 				       txq_id, reg);
156*4882a593Smuzhiyun 			iwl_set_bit(trans, CSR_GP_CNTRL,
157*4882a593Smuzhiyun 				    CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
158*4882a593Smuzhiyun 			txq->need_update = true;
159*4882a593Smuzhiyun 			return;
160*4882a593Smuzhiyun 		}
161*4882a593Smuzhiyun 	}
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	/*
164*4882a593Smuzhiyun 	 * if not in power-save mode, uCode will never sleep when we're
165*4882a593Smuzhiyun 	 * trying to tx (during RFKILL, we're not trying to tx).
166*4882a593Smuzhiyun 	 */
167*4882a593Smuzhiyun 	IWL_DEBUG_TX(trans, "Q:%d WR: 0x%x\n", txq_id, txq->write_ptr);
168*4882a593Smuzhiyun 	if (!txq->block)
169*4882a593Smuzhiyun 		iwl_write32(trans, HBUS_TARG_WRPTR,
170*4882a593Smuzhiyun 			    txq->write_ptr | (txq_id << 8));
171*4882a593Smuzhiyun }
172*4882a593Smuzhiyun 
iwl_pcie_txq_check_wrptrs(struct iwl_trans * trans)173*4882a593Smuzhiyun void iwl_pcie_txq_check_wrptrs(struct iwl_trans *trans)
174*4882a593Smuzhiyun {
175*4882a593Smuzhiyun 	int i;
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 	for (i = 0; i < trans->trans_cfg->base_params->num_of_queues; i++) {
178*4882a593Smuzhiyun 		struct iwl_txq *txq = trans->txqs.txq[i];
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun 		if (!test_bit(i, trans->txqs.queue_used))
181*4882a593Smuzhiyun 			continue;
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun 		spin_lock_bh(&txq->lock);
184*4882a593Smuzhiyun 		if (txq->need_update) {
185*4882a593Smuzhiyun 			iwl_pcie_txq_inc_wr_ptr(trans, txq);
186*4882a593Smuzhiyun 			txq->need_update = false;
187*4882a593Smuzhiyun 		}
188*4882a593Smuzhiyun 		spin_unlock_bh(&txq->lock);
189*4882a593Smuzhiyun 	}
190*4882a593Smuzhiyun }
191*4882a593Smuzhiyun 
iwl_pcie_tfd_set_tb(struct iwl_trans * trans,void * tfd,u8 idx,dma_addr_t addr,u16 len)192*4882a593Smuzhiyun static inline void iwl_pcie_tfd_set_tb(struct iwl_trans *trans, void *tfd,
193*4882a593Smuzhiyun 				       u8 idx, dma_addr_t addr, u16 len)
194*4882a593Smuzhiyun {
195*4882a593Smuzhiyun 	struct iwl_tfd *tfd_fh = (void *)tfd;
196*4882a593Smuzhiyun 	struct iwl_tfd_tb *tb = &tfd_fh->tbs[idx];
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	u16 hi_n_len = len << 4;
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun 	put_unaligned_le32(addr, &tb->lo);
201*4882a593Smuzhiyun 	hi_n_len |= iwl_get_dma_hi_addr(addr);
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun 	tb->hi_n_len = cpu_to_le16(hi_n_len);
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 	tfd_fh->num_tbs = idx + 1;
206*4882a593Smuzhiyun }
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun /*
209*4882a593Smuzhiyun  * iwl_pcie_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr]
210*4882a593Smuzhiyun  * @trans - transport private data
211*4882a593Smuzhiyun  * @txq - tx queue
212*4882a593Smuzhiyun  * @dma_dir - the direction of the DMA mapping
213*4882a593Smuzhiyun  *
214*4882a593Smuzhiyun  * Does NOT advance any TFD circular buffer read/write indexes
215*4882a593Smuzhiyun  * Does NOT free the TFD itself (which is within circular buffer)
216*4882a593Smuzhiyun  */
iwl_pcie_txq_free_tfd(struct iwl_trans * trans,struct iwl_txq * txq)217*4882a593Smuzhiyun void iwl_pcie_txq_free_tfd(struct iwl_trans *trans, struct iwl_txq *txq)
218*4882a593Smuzhiyun {
219*4882a593Smuzhiyun 	/* rd_ptr is bounded by TFD_QUEUE_SIZE_MAX and
220*4882a593Smuzhiyun 	 * idx is bounded by n_window
221*4882a593Smuzhiyun 	 */
222*4882a593Smuzhiyun 	int rd_ptr = txq->read_ptr;
223*4882a593Smuzhiyun 	int idx = iwl_txq_get_cmd_index(txq, rd_ptr);
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 	lockdep_assert_held(&txq->lock);
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun 	/* We have only q->n_window txq->entries, but we use
228*4882a593Smuzhiyun 	 * TFD_QUEUE_SIZE_MAX tfds
229*4882a593Smuzhiyun 	 */
230*4882a593Smuzhiyun 	iwl_txq_gen1_tfd_unmap(trans, &txq->entries[idx].meta, txq, rd_ptr);
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun 	/* free SKB */
233*4882a593Smuzhiyun 	if (txq->entries) {
234*4882a593Smuzhiyun 		struct sk_buff *skb;
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun 		skb = txq->entries[idx].skb;
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun 		/* Can be called from irqs-disabled context
239*4882a593Smuzhiyun 		 * If skb is not NULL, it means that the whole queue is being
240*4882a593Smuzhiyun 		 * freed and that the queue is not empty - free the skb
241*4882a593Smuzhiyun 		 */
242*4882a593Smuzhiyun 		if (skb) {
243*4882a593Smuzhiyun 			iwl_op_mode_free_skb(trans->op_mode, skb);
244*4882a593Smuzhiyun 			txq->entries[idx].skb = NULL;
245*4882a593Smuzhiyun 		}
246*4882a593Smuzhiyun 	}
247*4882a593Smuzhiyun }
248*4882a593Smuzhiyun 
iwl_pcie_txq_build_tfd(struct iwl_trans * trans,struct iwl_txq * txq,dma_addr_t addr,u16 len,bool reset)249*4882a593Smuzhiyun static int iwl_pcie_txq_build_tfd(struct iwl_trans *trans, struct iwl_txq *txq,
250*4882a593Smuzhiyun 				  dma_addr_t addr, u16 len, bool reset)
251*4882a593Smuzhiyun {
252*4882a593Smuzhiyun 	void *tfd;
253*4882a593Smuzhiyun 	u32 num_tbs;
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 	tfd = txq->tfds + trans->txqs.tfd.size * txq->write_ptr;
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 	if (reset)
258*4882a593Smuzhiyun 		memset(tfd, 0, trans->txqs.tfd.size);
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 	num_tbs = iwl_txq_gen1_tfd_get_num_tbs(trans, tfd);
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 	/* Each TFD can point to a maximum max_tbs Tx buffers */
263*4882a593Smuzhiyun 	if (num_tbs >= trans->txqs.tfd.max_tbs) {
264*4882a593Smuzhiyun 		IWL_ERR(trans, "Error can not send more than %d chunks\n",
265*4882a593Smuzhiyun 			trans->txqs.tfd.max_tbs);
266*4882a593Smuzhiyun 		return -EINVAL;
267*4882a593Smuzhiyun 	}
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 	if (WARN(addr & ~IWL_TX_DMA_MASK,
270*4882a593Smuzhiyun 		 "Unaligned address = %llx\n", (unsigned long long)addr))
271*4882a593Smuzhiyun 		return -EINVAL;
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 	iwl_pcie_tfd_set_tb(trans, tfd, num_tbs, addr, len);
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun 	return num_tbs;
276*4882a593Smuzhiyun }
277*4882a593Smuzhiyun 
iwl_pcie_clear_cmd_in_flight(struct iwl_trans * trans)278*4882a593Smuzhiyun static void iwl_pcie_clear_cmd_in_flight(struct iwl_trans *trans)
279*4882a593Smuzhiyun {
280*4882a593Smuzhiyun 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun 	lockdep_assert_held(&trans_pcie->reg_lock);
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun 	if (!trans->trans_cfg->base_params->apmg_wake_up_wa)
285*4882a593Smuzhiyun 		return;
286*4882a593Smuzhiyun 	if (WARN_ON(!trans_pcie->cmd_hold_nic_awake))
287*4882a593Smuzhiyun 		return;
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun 	trans_pcie->cmd_hold_nic_awake = false;
290*4882a593Smuzhiyun 	__iwl_trans_pcie_clear_bit(trans, CSR_GP_CNTRL,
291*4882a593Smuzhiyun 				   CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
292*4882a593Smuzhiyun }
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun /*
295*4882a593Smuzhiyun  * iwl_pcie_txq_unmap -  Unmap any remaining DMA mappings and free skb's
296*4882a593Smuzhiyun  */
iwl_pcie_txq_unmap(struct iwl_trans * trans,int txq_id)297*4882a593Smuzhiyun static void iwl_pcie_txq_unmap(struct iwl_trans *trans, int txq_id)
298*4882a593Smuzhiyun {
299*4882a593Smuzhiyun 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
300*4882a593Smuzhiyun 	struct iwl_txq *txq = trans->txqs.txq[txq_id];
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun 	if (!txq) {
303*4882a593Smuzhiyun 		IWL_ERR(trans, "Trying to free a queue that wasn't allocated?\n");
304*4882a593Smuzhiyun 		return;
305*4882a593Smuzhiyun 	}
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun 	spin_lock_bh(&txq->lock);
308*4882a593Smuzhiyun 	while (txq->write_ptr != txq->read_ptr) {
309*4882a593Smuzhiyun 		IWL_DEBUG_TX_REPLY(trans, "Q %d Free %d\n",
310*4882a593Smuzhiyun 				   txq_id, txq->read_ptr);
311*4882a593Smuzhiyun 
312*4882a593Smuzhiyun 		if (txq_id != trans->txqs.cmd.q_id) {
313*4882a593Smuzhiyun 			struct sk_buff *skb = txq->entries[txq->read_ptr].skb;
314*4882a593Smuzhiyun 
315*4882a593Smuzhiyun 			if (WARN_ON_ONCE(!skb))
316*4882a593Smuzhiyun 				continue;
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 			iwl_txq_free_tso_page(trans, skb);
319*4882a593Smuzhiyun 		}
320*4882a593Smuzhiyun 		iwl_pcie_txq_free_tfd(trans, txq);
321*4882a593Smuzhiyun 		txq->read_ptr = iwl_txq_inc_wrap(trans, txq->read_ptr);
322*4882a593Smuzhiyun 
323*4882a593Smuzhiyun 		if (txq->read_ptr == txq->write_ptr) {
324*4882a593Smuzhiyun 			spin_lock(&trans_pcie->reg_lock);
325*4882a593Smuzhiyun 			if (txq_id == trans->txqs.cmd.q_id)
326*4882a593Smuzhiyun 				iwl_pcie_clear_cmd_in_flight(trans);
327*4882a593Smuzhiyun 			spin_unlock(&trans_pcie->reg_lock);
328*4882a593Smuzhiyun 		}
329*4882a593Smuzhiyun 	}
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun 	while (!skb_queue_empty(&txq->overflow_q)) {
332*4882a593Smuzhiyun 		struct sk_buff *skb = __skb_dequeue(&txq->overflow_q);
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun 		iwl_op_mode_free_skb(trans->op_mode, skb);
335*4882a593Smuzhiyun 	}
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun 	spin_unlock_bh(&txq->lock);
338*4882a593Smuzhiyun 
339*4882a593Smuzhiyun 	/* just in case - this queue may have been stopped */
340*4882a593Smuzhiyun 	iwl_wake_queue(trans, txq);
341*4882a593Smuzhiyun }
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun /*
344*4882a593Smuzhiyun  * iwl_pcie_txq_free - Deallocate DMA queue.
345*4882a593Smuzhiyun  * @txq: Transmit queue to deallocate.
346*4882a593Smuzhiyun  *
347*4882a593Smuzhiyun  * Empty queue by removing and destroying all BD's.
348*4882a593Smuzhiyun  * Free all buffers.
349*4882a593Smuzhiyun  * 0-fill, but do not free "txq" descriptor structure.
350*4882a593Smuzhiyun  */
iwl_pcie_txq_free(struct iwl_trans * trans,int txq_id)351*4882a593Smuzhiyun static void iwl_pcie_txq_free(struct iwl_trans *trans, int txq_id)
352*4882a593Smuzhiyun {
353*4882a593Smuzhiyun 	struct iwl_txq *txq = trans->txqs.txq[txq_id];
354*4882a593Smuzhiyun 	struct device *dev = trans->dev;
355*4882a593Smuzhiyun 	int i;
356*4882a593Smuzhiyun 
357*4882a593Smuzhiyun 	if (WARN_ON(!txq))
358*4882a593Smuzhiyun 		return;
359*4882a593Smuzhiyun 
360*4882a593Smuzhiyun 	iwl_pcie_txq_unmap(trans, txq_id);
361*4882a593Smuzhiyun 
362*4882a593Smuzhiyun 	/* De-alloc array of command/tx buffers */
363*4882a593Smuzhiyun 	if (txq_id == trans->txqs.cmd.q_id)
364*4882a593Smuzhiyun 		for (i = 0; i < txq->n_window; i++) {
365*4882a593Smuzhiyun 			kfree_sensitive(txq->entries[i].cmd);
366*4882a593Smuzhiyun 			kfree_sensitive(txq->entries[i].free_buf);
367*4882a593Smuzhiyun 		}
368*4882a593Smuzhiyun 
369*4882a593Smuzhiyun 	/* De-alloc circular buffer of TFDs */
370*4882a593Smuzhiyun 	if (txq->tfds) {
371*4882a593Smuzhiyun 		dma_free_coherent(dev,
372*4882a593Smuzhiyun 				  trans->txqs.tfd.size *
373*4882a593Smuzhiyun 				  trans->trans_cfg->base_params->max_tfd_queue_size,
374*4882a593Smuzhiyun 				  txq->tfds, txq->dma_addr);
375*4882a593Smuzhiyun 		txq->dma_addr = 0;
376*4882a593Smuzhiyun 		txq->tfds = NULL;
377*4882a593Smuzhiyun 
378*4882a593Smuzhiyun 		dma_free_coherent(dev,
379*4882a593Smuzhiyun 				  sizeof(*txq->first_tb_bufs) * txq->n_window,
380*4882a593Smuzhiyun 				  txq->first_tb_bufs, txq->first_tb_dma);
381*4882a593Smuzhiyun 	}
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun 	kfree(txq->entries);
384*4882a593Smuzhiyun 	txq->entries = NULL;
385*4882a593Smuzhiyun 
386*4882a593Smuzhiyun 	del_timer_sync(&txq->stuck_timer);
387*4882a593Smuzhiyun 
388*4882a593Smuzhiyun 	/* 0-fill queue descriptor structure */
389*4882a593Smuzhiyun 	memset(txq, 0, sizeof(*txq));
390*4882a593Smuzhiyun }
391*4882a593Smuzhiyun 
iwl_pcie_tx_start(struct iwl_trans * trans,u32 scd_base_addr)392*4882a593Smuzhiyun void iwl_pcie_tx_start(struct iwl_trans *trans, u32 scd_base_addr)
393*4882a593Smuzhiyun {
394*4882a593Smuzhiyun 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
395*4882a593Smuzhiyun 	int nq = trans->trans_cfg->base_params->num_of_queues;
396*4882a593Smuzhiyun 	int chan;
397*4882a593Smuzhiyun 	u32 reg_val;
398*4882a593Smuzhiyun 	int clear_dwords = (SCD_TRANS_TBL_OFFSET_QUEUE(nq) -
399*4882a593Smuzhiyun 				SCD_CONTEXT_MEM_LOWER_BOUND) / sizeof(u32);
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun 	/* make sure all queue are not stopped/used */
402*4882a593Smuzhiyun 	memset(trans->txqs.queue_stopped, 0,
403*4882a593Smuzhiyun 	       sizeof(trans->txqs.queue_stopped));
404*4882a593Smuzhiyun 	memset(trans->txqs.queue_used, 0, sizeof(trans->txqs.queue_used));
405*4882a593Smuzhiyun 
406*4882a593Smuzhiyun 	trans_pcie->scd_base_addr =
407*4882a593Smuzhiyun 		iwl_read_prph(trans, SCD_SRAM_BASE_ADDR);
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun 	WARN_ON(scd_base_addr != 0 &&
410*4882a593Smuzhiyun 		scd_base_addr != trans_pcie->scd_base_addr);
411*4882a593Smuzhiyun 
412*4882a593Smuzhiyun 	/* reset context data, TX status and translation data */
413*4882a593Smuzhiyun 	iwl_trans_write_mem(trans, trans_pcie->scd_base_addr +
414*4882a593Smuzhiyun 				   SCD_CONTEXT_MEM_LOWER_BOUND,
415*4882a593Smuzhiyun 			    NULL, clear_dwords);
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun 	iwl_write_prph(trans, SCD_DRAM_BASE_ADDR,
418*4882a593Smuzhiyun 		       trans->txqs.scd_bc_tbls.dma >> 10);
419*4882a593Smuzhiyun 
420*4882a593Smuzhiyun 	/* The chain extension of the SCD doesn't work well. This feature is
421*4882a593Smuzhiyun 	 * enabled by default by the HW, so we need to disable it manually.
422*4882a593Smuzhiyun 	 */
423*4882a593Smuzhiyun 	if (trans->trans_cfg->base_params->scd_chain_ext_wa)
424*4882a593Smuzhiyun 		iwl_write_prph(trans, SCD_CHAINEXT_EN, 0);
425*4882a593Smuzhiyun 
426*4882a593Smuzhiyun 	iwl_trans_ac_txq_enable(trans, trans->txqs.cmd.q_id,
427*4882a593Smuzhiyun 				trans->txqs.cmd.fifo,
428*4882a593Smuzhiyun 				trans->txqs.cmd.wdg_timeout);
429*4882a593Smuzhiyun 
430*4882a593Smuzhiyun 	/* Activate all Tx DMA/FIFO channels */
431*4882a593Smuzhiyun 	iwl_scd_activate_fifos(trans);
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun 	/* Enable DMA channel */
434*4882a593Smuzhiyun 	for (chan = 0; chan < FH_TCSR_CHNL_NUM; chan++)
435*4882a593Smuzhiyun 		iwl_write_direct32(trans, FH_TCSR_CHNL_TX_CONFIG_REG(chan),
436*4882a593Smuzhiyun 				   FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
437*4882a593Smuzhiyun 				   FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE);
438*4882a593Smuzhiyun 
439*4882a593Smuzhiyun 	/* Update FH chicken bits */
440*4882a593Smuzhiyun 	reg_val = iwl_read_direct32(trans, FH_TX_CHICKEN_BITS_REG);
441*4882a593Smuzhiyun 	iwl_write_direct32(trans, FH_TX_CHICKEN_BITS_REG,
442*4882a593Smuzhiyun 			   reg_val | FH_TX_CHICKEN_BITS_SCD_AUTO_RETRY_EN);
443*4882a593Smuzhiyun 
444*4882a593Smuzhiyun 	/* Enable L1-Active */
445*4882a593Smuzhiyun 	if (trans->trans_cfg->device_family < IWL_DEVICE_FAMILY_8000)
446*4882a593Smuzhiyun 		iwl_clear_bits_prph(trans, APMG_PCIDEV_STT_REG,
447*4882a593Smuzhiyun 				    APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
448*4882a593Smuzhiyun }
449*4882a593Smuzhiyun 
iwl_trans_pcie_tx_reset(struct iwl_trans * trans)450*4882a593Smuzhiyun void iwl_trans_pcie_tx_reset(struct iwl_trans *trans)
451*4882a593Smuzhiyun {
452*4882a593Smuzhiyun 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
453*4882a593Smuzhiyun 	int txq_id;
454*4882a593Smuzhiyun 
455*4882a593Smuzhiyun 	/*
456*4882a593Smuzhiyun 	 * we should never get here in gen2 trans mode return early to avoid
457*4882a593Smuzhiyun 	 * having invalid accesses
458*4882a593Smuzhiyun 	 */
459*4882a593Smuzhiyun 	if (WARN_ON_ONCE(trans->trans_cfg->gen2))
460*4882a593Smuzhiyun 		return;
461*4882a593Smuzhiyun 
462*4882a593Smuzhiyun 	for (txq_id = 0; txq_id < trans->trans_cfg->base_params->num_of_queues;
463*4882a593Smuzhiyun 	     txq_id++) {
464*4882a593Smuzhiyun 		struct iwl_txq *txq = trans->txqs.txq[txq_id];
465*4882a593Smuzhiyun 		if (trans->trans_cfg->use_tfh)
466*4882a593Smuzhiyun 			iwl_write_direct64(trans,
467*4882a593Smuzhiyun 					   FH_MEM_CBBC_QUEUE(trans, txq_id),
468*4882a593Smuzhiyun 					   txq->dma_addr);
469*4882a593Smuzhiyun 		else
470*4882a593Smuzhiyun 			iwl_write_direct32(trans,
471*4882a593Smuzhiyun 					   FH_MEM_CBBC_QUEUE(trans, txq_id),
472*4882a593Smuzhiyun 					   txq->dma_addr >> 8);
473*4882a593Smuzhiyun 		iwl_pcie_txq_unmap(trans, txq_id);
474*4882a593Smuzhiyun 		txq->read_ptr = 0;
475*4882a593Smuzhiyun 		txq->write_ptr = 0;
476*4882a593Smuzhiyun 	}
477*4882a593Smuzhiyun 
478*4882a593Smuzhiyun 	/* Tell NIC where to find the "keep warm" buffer */
479*4882a593Smuzhiyun 	iwl_write_direct32(trans, FH_KW_MEM_ADDR_REG,
480*4882a593Smuzhiyun 			   trans_pcie->kw.dma >> 4);
481*4882a593Smuzhiyun 
482*4882a593Smuzhiyun 	/*
483*4882a593Smuzhiyun 	 * Send 0 as the scd_base_addr since the device may have be reset
484*4882a593Smuzhiyun 	 * while we were in WoWLAN in which case SCD_SRAM_BASE_ADDR will
485*4882a593Smuzhiyun 	 * contain garbage.
486*4882a593Smuzhiyun 	 */
487*4882a593Smuzhiyun 	iwl_pcie_tx_start(trans, 0);
488*4882a593Smuzhiyun }
489*4882a593Smuzhiyun 
iwl_pcie_tx_stop_fh(struct iwl_trans * trans)490*4882a593Smuzhiyun static void iwl_pcie_tx_stop_fh(struct iwl_trans *trans)
491*4882a593Smuzhiyun {
492*4882a593Smuzhiyun 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
493*4882a593Smuzhiyun 	unsigned long flags;
494*4882a593Smuzhiyun 	int ch, ret;
495*4882a593Smuzhiyun 	u32 mask = 0;
496*4882a593Smuzhiyun 
497*4882a593Smuzhiyun 	spin_lock(&trans_pcie->irq_lock);
498*4882a593Smuzhiyun 
499*4882a593Smuzhiyun 	if (!iwl_trans_grab_nic_access(trans, &flags))
500*4882a593Smuzhiyun 		goto out;
501*4882a593Smuzhiyun 
502*4882a593Smuzhiyun 	/* Stop each Tx DMA channel */
503*4882a593Smuzhiyun 	for (ch = 0; ch < FH_TCSR_CHNL_NUM; ch++) {
504*4882a593Smuzhiyun 		iwl_write32(trans, FH_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0);
505*4882a593Smuzhiyun 		mask |= FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch);
506*4882a593Smuzhiyun 	}
507*4882a593Smuzhiyun 
508*4882a593Smuzhiyun 	/* Wait for DMA channels to be idle */
509*4882a593Smuzhiyun 	ret = iwl_poll_bit(trans, FH_TSSR_TX_STATUS_REG, mask, mask, 5000);
510*4882a593Smuzhiyun 	if (ret < 0)
511*4882a593Smuzhiyun 		IWL_ERR(trans,
512*4882a593Smuzhiyun 			"Failing on timeout while stopping DMA channel %d [0x%08x]\n",
513*4882a593Smuzhiyun 			ch, iwl_read32(trans, FH_TSSR_TX_STATUS_REG));
514*4882a593Smuzhiyun 
515*4882a593Smuzhiyun 	iwl_trans_release_nic_access(trans, &flags);
516*4882a593Smuzhiyun 
517*4882a593Smuzhiyun out:
518*4882a593Smuzhiyun 	spin_unlock(&trans_pcie->irq_lock);
519*4882a593Smuzhiyun }
520*4882a593Smuzhiyun 
521*4882a593Smuzhiyun /*
522*4882a593Smuzhiyun  * iwl_pcie_tx_stop - Stop all Tx DMA channels
523*4882a593Smuzhiyun  */
iwl_pcie_tx_stop(struct iwl_trans * trans)524*4882a593Smuzhiyun int iwl_pcie_tx_stop(struct iwl_trans *trans)
525*4882a593Smuzhiyun {
526*4882a593Smuzhiyun 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
527*4882a593Smuzhiyun 	int txq_id;
528*4882a593Smuzhiyun 
529*4882a593Smuzhiyun 	/* Turn off all Tx DMA fifos */
530*4882a593Smuzhiyun 	iwl_scd_deactivate_fifos(trans);
531*4882a593Smuzhiyun 
532*4882a593Smuzhiyun 	/* Turn off all Tx DMA channels */
533*4882a593Smuzhiyun 	iwl_pcie_tx_stop_fh(trans);
534*4882a593Smuzhiyun 
535*4882a593Smuzhiyun 	/*
536*4882a593Smuzhiyun 	 * This function can be called before the op_mode disabled the
537*4882a593Smuzhiyun 	 * queues. This happens when we have an rfkill interrupt.
538*4882a593Smuzhiyun 	 * Since we stop Tx altogether - mark the queues as stopped.
539*4882a593Smuzhiyun 	 */
540*4882a593Smuzhiyun 	memset(trans->txqs.queue_stopped, 0,
541*4882a593Smuzhiyun 	       sizeof(trans->txqs.queue_stopped));
542*4882a593Smuzhiyun 	memset(trans->txqs.queue_used, 0, sizeof(trans->txqs.queue_used));
543*4882a593Smuzhiyun 
544*4882a593Smuzhiyun 	/* This can happen: start_hw, stop_device */
545*4882a593Smuzhiyun 	if (!trans_pcie->txq_memory)
546*4882a593Smuzhiyun 		return 0;
547*4882a593Smuzhiyun 
548*4882a593Smuzhiyun 	/* Unmap DMA from host system and free skb's */
549*4882a593Smuzhiyun 	for (txq_id = 0; txq_id < trans->trans_cfg->base_params->num_of_queues;
550*4882a593Smuzhiyun 	     txq_id++)
551*4882a593Smuzhiyun 		iwl_pcie_txq_unmap(trans, txq_id);
552*4882a593Smuzhiyun 
553*4882a593Smuzhiyun 	return 0;
554*4882a593Smuzhiyun }
555*4882a593Smuzhiyun 
556*4882a593Smuzhiyun /*
557*4882a593Smuzhiyun  * iwl_trans_tx_free - Free TXQ Context
558*4882a593Smuzhiyun  *
559*4882a593Smuzhiyun  * Destroy all TX DMA queues and structures
560*4882a593Smuzhiyun  */
iwl_pcie_tx_free(struct iwl_trans * trans)561*4882a593Smuzhiyun void iwl_pcie_tx_free(struct iwl_trans *trans)
562*4882a593Smuzhiyun {
563*4882a593Smuzhiyun 	int txq_id;
564*4882a593Smuzhiyun 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
565*4882a593Smuzhiyun 
566*4882a593Smuzhiyun 	memset(trans->txqs.queue_used, 0, sizeof(trans->txqs.queue_used));
567*4882a593Smuzhiyun 
568*4882a593Smuzhiyun 	/* Tx queues */
569*4882a593Smuzhiyun 	if (trans_pcie->txq_memory) {
570*4882a593Smuzhiyun 		for (txq_id = 0;
571*4882a593Smuzhiyun 		     txq_id < trans->trans_cfg->base_params->num_of_queues;
572*4882a593Smuzhiyun 		     txq_id++) {
573*4882a593Smuzhiyun 			iwl_pcie_txq_free(trans, txq_id);
574*4882a593Smuzhiyun 			trans->txqs.txq[txq_id] = NULL;
575*4882a593Smuzhiyun 		}
576*4882a593Smuzhiyun 	}
577*4882a593Smuzhiyun 
578*4882a593Smuzhiyun 	kfree(trans_pcie->txq_memory);
579*4882a593Smuzhiyun 	trans_pcie->txq_memory = NULL;
580*4882a593Smuzhiyun 
581*4882a593Smuzhiyun 	iwl_pcie_free_dma_ptr(trans, &trans_pcie->kw);
582*4882a593Smuzhiyun 
583*4882a593Smuzhiyun 	iwl_pcie_free_dma_ptr(trans, &trans->txqs.scd_bc_tbls);
584*4882a593Smuzhiyun }
585*4882a593Smuzhiyun 
586*4882a593Smuzhiyun /*
587*4882a593Smuzhiyun  * iwl_pcie_tx_alloc - allocate TX context
588*4882a593Smuzhiyun  * Allocate all Tx DMA structures and initialize them
589*4882a593Smuzhiyun  */
iwl_pcie_tx_alloc(struct iwl_trans * trans)590*4882a593Smuzhiyun static int iwl_pcie_tx_alloc(struct iwl_trans *trans)
591*4882a593Smuzhiyun {
592*4882a593Smuzhiyun 	int ret;
593*4882a593Smuzhiyun 	int txq_id, slots_num;
594*4882a593Smuzhiyun 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
595*4882a593Smuzhiyun 	u16 bc_tbls_size = trans->trans_cfg->base_params->num_of_queues;
596*4882a593Smuzhiyun 
597*4882a593Smuzhiyun 	if (WARN_ON(trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210))
598*4882a593Smuzhiyun 		return -EINVAL;
599*4882a593Smuzhiyun 
600*4882a593Smuzhiyun 	bc_tbls_size *= sizeof(struct iwlagn_scd_bc_tbl);
601*4882a593Smuzhiyun 
602*4882a593Smuzhiyun 	/*It is not allowed to alloc twice, so warn when this happens.
603*4882a593Smuzhiyun 	 * We cannot rely on the previous allocation, so free and fail */
604*4882a593Smuzhiyun 	if (WARN_ON(trans_pcie->txq_memory)) {
605*4882a593Smuzhiyun 		ret = -EINVAL;
606*4882a593Smuzhiyun 		goto error;
607*4882a593Smuzhiyun 	}
608*4882a593Smuzhiyun 
609*4882a593Smuzhiyun 	ret = iwl_pcie_alloc_dma_ptr(trans, &trans->txqs.scd_bc_tbls,
610*4882a593Smuzhiyun 				     bc_tbls_size);
611*4882a593Smuzhiyun 	if (ret) {
612*4882a593Smuzhiyun 		IWL_ERR(trans, "Scheduler BC Table allocation failed\n");
613*4882a593Smuzhiyun 		goto error;
614*4882a593Smuzhiyun 	}
615*4882a593Smuzhiyun 
616*4882a593Smuzhiyun 	/* Alloc keep-warm buffer */
617*4882a593Smuzhiyun 	ret = iwl_pcie_alloc_dma_ptr(trans, &trans_pcie->kw, IWL_KW_SIZE);
618*4882a593Smuzhiyun 	if (ret) {
619*4882a593Smuzhiyun 		IWL_ERR(trans, "Keep Warm allocation failed\n");
620*4882a593Smuzhiyun 		goto error;
621*4882a593Smuzhiyun 	}
622*4882a593Smuzhiyun 
623*4882a593Smuzhiyun 	trans_pcie->txq_memory =
624*4882a593Smuzhiyun 		kcalloc(trans->trans_cfg->base_params->num_of_queues,
625*4882a593Smuzhiyun 			sizeof(struct iwl_txq), GFP_KERNEL);
626*4882a593Smuzhiyun 	if (!trans_pcie->txq_memory) {
627*4882a593Smuzhiyun 		IWL_ERR(trans, "Not enough memory for txq\n");
628*4882a593Smuzhiyun 		ret = -ENOMEM;
629*4882a593Smuzhiyun 		goto error;
630*4882a593Smuzhiyun 	}
631*4882a593Smuzhiyun 
632*4882a593Smuzhiyun 	/* Alloc and init all Tx queues, including the command queue (#4/#9) */
633*4882a593Smuzhiyun 	for (txq_id = 0; txq_id < trans->trans_cfg->base_params->num_of_queues;
634*4882a593Smuzhiyun 	     txq_id++) {
635*4882a593Smuzhiyun 		bool cmd_queue = (txq_id == trans->txqs.cmd.q_id);
636*4882a593Smuzhiyun 
637*4882a593Smuzhiyun 		if (cmd_queue)
638*4882a593Smuzhiyun 			slots_num = max_t(u32, IWL_CMD_QUEUE_SIZE,
639*4882a593Smuzhiyun 					  trans->cfg->min_txq_size);
640*4882a593Smuzhiyun 		else
641*4882a593Smuzhiyun 			slots_num = max_t(u32, IWL_DEFAULT_QUEUE_SIZE,
642*4882a593Smuzhiyun 					  trans->cfg->min_256_ba_txq_size);
643*4882a593Smuzhiyun 		trans->txqs.txq[txq_id] = &trans_pcie->txq_memory[txq_id];
644*4882a593Smuzhiyun 		ret = iwl_txq_alloc(trans, trans->txqs.txq[txq_id], slots_num,
645*4882a593Smuzhiyun 				    cmd_queue);
646*4882a593Smuzhiyun 		if (ret) {
647*4882a593Smuzhiyun 			IWL_ERR(trans, "Tx %d queue alloc failed\n", txq_id);
648*4882a593Smuzhiyun 			goto error;
649*4882a593Smuzhiyun 		}
650*4882a593Smuzhiyun 		trans->txqs.txq[txq_id]->id = txq_id;
651*4882a593Smuzhiyun 	}
652*4882a593Smuzhiyun 
653*4882a593Smuzhiyun 	return 0;
654*4882a593Smuzhiyun 
655*4882a593Smuzhiyun error:
656*4882a593Smuzhiyun 	iwl_pcie_tx_free(trans);
657*4882a593Smuzhiyun 
658*4882a593Smuzhiyun 	return ret;
659*4882a593Smuzhiyun }
660*4882a593Smuzhiyun 
iwl_pcie_tx_init(struct iwl_trans * trans)661*4882a593Smuzhiyun int iwl_pcie_tx_init(struct iwl_trans *trans)
662*4882a593Smuzhiyun {
663*4882a593Smuzhiyun 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
664*4882a593Smuzhiyun 	int ret;
665*4882a593Smuzhiyun 	int txq_id, slots_num;
666*4882a593Smuzhiyun 	bool alloc = false;
667*4882a593Smuzhiyun 
668*4882a593Smuzhiyun 	if (!trans_pcie->txq_memory) {
669*4882a593Smuzhiyun 		ret = iwl_pcie_tx_alloc(trans);
670*4882a593Smuzhiyun 		if (ret)
671*4882a593Smuzhiyun 			goto error;
672*4882a593Smuzhiyun 		alloc = true;
673*4882a593Smuzhiyun 	}
674*4882a593Smuzhiyun 
675*4882a593Smuzhiyun 	spin_lock(&trans_pcie->irq_lock);
676*4882a593Smuzhiyun 
677*4882a593Smuzhiyun 	/* Turn off all Tx DMA fifos */
678*4882a593Smuzhiyun 	iwl_scd_deactivate_fifos(trans);
679*4882a593Smuzhiyun 
680*4882a593Smuzhiyun 	/* Tell NIC where to find the "keep warm" buffer */
681*4882a593Smuzhiyun 	iwl_write_direct32(trans, FH_KW_MEM_ADDR_REG,
682*4882a593Smuzhiyun 			   trans_pcie->kw.dma >> 4);
683*4882a593Smuzhiyun 
684*4882a593Smuzhiyun 	spin_unlock(&trans_pcie->irq_lock);
685*4882a593Smuzhiyun 
686*4882a593Smuzhiyun 	/* Alloc and init all Tx queues, including the command queue (#4/#9) */
687*4882a593Smuzhiyun 	for (txq_id = 0; txq_id < trans->trans_cfg->base_params->num_of_queues;
688*4882a593Smuzhiyun 	     txq_id++) {
689*4882a593Smuzhiyun 		bool cmd_queue = (txq_id == trans->txqs.cmd.q_id);
690*4882a593Smuzhiyun 
691*4882a593Smuzhiyun 		if (cmd_queue)
692*4882a593Smuzhiyun 			slots_num = max_t(u32, IWL_CMD_QUEUE_SIZE,
693*4882a593Smuzhiyun 					  trans->cfg->min_txq_size);
694*4882a593Smuzhiyun 		else
695*4882a593Smuzhiyun 			slots_num = max_t(u32, IWL_DEFAULT_QUEUE_SIZE,
696*4882a593Smuzhiyun 					  trans->cfg->min_256_ba_txq_size);
697*4882a593Smuzhiyun 		ret = iwl_txq_init(trans, trans->txqs.txq[txq_id], slots_num,
698*4882a593Smuzhiyun 				   cmd_queue);
699*4882a593Smuzhiyun 		if (ret) {
700*4882a593Smuzhiyun 			IWL_ERR(trans, "Tx %d queue init failed\n", txq_id);
701*4882a593Smuzhiyun 			goto error;
702*4882a593Smuzhiyun 		}
703*4882a593Smuzhiyun 
704*4882a593Smuzhiyun 		/*
705*4882a593Smuzhiyun 		 * Tell nic where to find circular buffer of TFDs for a
706*4882a593Smuzhiyun 		 * given Tx queue, and enable the DMA channel used for that
707*4882a593Smuzhiyun 		 * queue.
708*4882a593Smuzhiyun 		 * Circular buffer (TFD queue in DRAM) physical base address
709*4882a593Smuzhiyun 		 */
710*4882a593Smuzhiyun 		iwl_write_direct32(trans, FH_MEM_CBBC_QUEUE(trans, txq_id),
711*4882a593Smuzhiyun 				   trans->txqs.txq[txq_id]->dma_addr >> 8);
712*4882a593Smuzhiyun 	}
713*4882a593Smuzhiyun 
714*4882a593Smuzhiyun 	iwl_set_bits_prph(trans, SCD_GP_CTRL, SCD_GP_CTRL_AUTO_ACTIVE_MODE);
715*4882a593Smuzhiyun 	if (trans->trans_cfg->base_params->num_of_queues > 20)
716*4882a593Smuzhiyun 		iwl_set_bits_prph(trans, SCD_GP_CTRL,
717*4882a593Smuzhiyun 				  SCD_GP_CTRL_ENABLE_31_QUEUES);
718*4882a593Smuzhiyun 
719*4882a593Smuzhiyun 	return 0;
720*4882a593Smuzhiyun error:
721*4882a593Smuzhiyun 	/*Upon error, free only if we allocated something */
722*4882a593Smuzhiyun 	if (alloc)
723*4882a593Smuzhiyun 		iwl_pcie_tx_free(trans);
724*4882a593Smuzhiyun 	return ret;
725*4882a593Smuzhiyun }
726*4882a593Smuzhiyun 
iwl_pcie_txq_progress(struct iwl_txq * txq)727*4882a593Smuzhiyun static inline void iwl_pcie_txq_progress(struct iwl_txq *txq)
728*4882a593Smuzhiyun {
729*4882a593Smuzhiyun 	lockdep_assert_held(&txq->lock);
730*4882a593Smuzhiyun 
731*4882a593Smuzhiyun 	if (!txq->wd_timeout)
732*4882a593Smuzhiyun 		return;
733*4882a593Smuzhiyun 
734*4882a593Smuzhiyun 	/*
735*4882a593Smuzhiyun 	 * station is asleep and we send data - that must
736*4882a593Smuzhiyun 	 * be uAPSD or PS-Poll. Don't rearm the timer.
737*4882a593Smuzhiyun 	 */
738*4882a593Smuzhiyun 	if (txq->frozen)
739*4882a593Smuzhiyun 		return;
740*4882a593Smuzhiyun 
741*4882a593Smuzhiyun 	/*
742*4882a593Smuzhiyun 	 * if empty delete timer, otherwise move timer forward
743*4882a593Smuzhiyun 	 * since we're making progress on this queue
744*4882a593Smuzhiyun 	 */
745*4882a593Smuzhiyun 	if (txq->read_ptr == txq->write_ptr)
746*4882a593Smuzhiyun 		del_timer(&txq->stuck_timer);
747*4882a593Smuzhiyun 	else
748*4882a593Smuzhiyun 		mod_timer(&txq->stuck_timer, jiffies + txq->wd_timeout);
749*4882a593Smuzhiyun }
750*4882a593Smuzhiyun 
751*4882a593Smuzhiyun /* Frees buffers until index _not_ inclusive */
iwl_trans_pcie_reclaim(struct iwl_trans * trans,int txq_id,int ssn,struct sk_buff_head * skbs)752*4882a593Smuzhiyun void iwl_trans_pcie_reclaim(struct iwl_trans *trans, int txq_id, int ssn,
753*4882a593Smuzhiyun 			    struct sk_buff_head *skbs)
754*4882a593Smuzhiyun {
755*4882a593Smuzhiyun 	struct iwl_txq *txq = trans->txqs.txq[txq_id];
756*4882a593Smuzhiyun 	int tfd_num = iwl_txq_get_cmd_index(txq, ssn);
757*4882a593Smuzhiyun 	int read_ptr = iwl_txq_get_cmd_index(txq, txq->read_ptr);
758*4882a593Smuzhiyun 	int last_to_free;
759*4882a593Smuzhiyun 
760*4882a593Smuzhiyun 	/* This function is not meant to release cmd queue*/
761*4882a593Smuzhiyun 	if (WARN_ON(txq_id == trans->txqs.cmd.q_id))
762*4882a593Smuzhiyun 		return;
763*4882a593Smuzhiyun 
764*4882a593Smuzhiyun 	spin_lock_bh(&txq->lock);
765*4882a593Smuzhiyun 
766*4882a593Smuzhiyun 	if (!test_bit(txq_id, trans->txqs.queue_used)) {
767*4882a593Smuzhiyun 		IWL_DEBUG_TX_QUEUES(trans, "Q %d inactive - ignoring idx %d\n",
768*4882a593Smuzhiyun 				    txq_id, ssn);
769*4882a593Smuzhiyun 		goto out;
770*4882a593Smuzhiyun 	}
771*4882a593Smuzhiyun 
772*4882a593Smuzhiyun 	if (read_ptr == tfd_num)
773*4882a593Smuzhiyun 		goto out;
774*4882a593Smuzhiyun 
775*4882a593Smuzhiyun 	IWL_DEBUG_TX_REPLY(trans, "[Q %d] %d -> %d (%d)\n",
776*4882a593Smuzhiyun 			   txq_id, txq->read_ptr, tfd_num, ssn);
777*4882a593Smuzhiyun 
778*4882a593Smuzhiyun 	/*Since we free until index _not_ inclusive, the one before index is
779*4882a593Smuzhiyun 	 * the last we will free. This one must be used */
780*4882a593Smuzhiyun 	last_to_free = iwl_txq_dec_wrap(trans, tfd_num);
781*4882a593Smuzhiyun 
782*4882a593Smuzhiyun 	if (!iwl_txq_used(txq, last_to_free)) {
783*4882a593Smuzhiyun 		IWL_ERR(trans,
784*4882a593Smuzhiyun 			"%s: Read index for txq id (%d), last_to_free %d is out of range [0-%d] %d %d.\n",
785*4882a593Smuzhiyun 			__func__, txq_id, last_to_free,
786*4882a593Smuzhiyun 			trans->trans_cfg->base_params->max_tfd_queue_size,
787*4882a593Smuzhiyun 			txq->write_ptr, txq->read_ptr);
788*4882a593Smuzhiyun 		goto out;
789*4882a593Smuzhiyun 	}
790*4882a593Smuzhiyun 
791*4882a593Smuzhiyun 	if (WARN_ON(!skb_queue_empty(skbs)))
792*4882a593Smuzhiyun 		goto out;
793*4882a593Smuzhiyun 
794*4882a593Smuzhiyun 	for (;
795*4882a593Smuzhiyun 	     read_ptr != tfd_num;
796*4882a593Smuzhiyun 	     txq->read_ptr = iwl_txq_inc_wrap(trans, txq->read_ptr),
797*4882a593Smuzhiyun 	     read_ptr = iwl_txq_get_cmd_index(txq, txq->read_ptr)) {
798*4882a593Smuzhiyun 		struct sk_buff *skb = txq->entries[read_ptr].skb;
799*4882a593Smuzhiyun 
800*4882a593Smuzhiyun 		if (WARN_ON_ONCE(!skb))
801*4882a593Smuzhiyun 			continue;
802*4882a593Smuzhiyun 
803*4882a593Smuzhiyun 		iwl_txq_free_tso_page(trans, skb);
804*4882a593Smuzhiyun 
805*4882a593Smuzhiyun 		__skb_queue_tail(skbs, skb);
806*4882a593Smuzhiyun 
807*4882a593Smuzhiyun 		txq->entries[read_ptr].skb = NULL;
808*4882a593Smuzhiyun 
809*4882a593Smuzhiyun 		if (!trans->trans_cfg->use_tfh)
810*4882a593Smuzhiyun 			iwl_txq_gen1_inval_byte_cnt_tbl(trans, txq);
811*4882a593Smuzhiyun 
812*4882a593Smuzhiyun 		iwl_pcie_txq_free_tfd(trans, txq);
813*4882a593Smuzhiyun 	}
814*4882a593Smuzhiyun 
815*4882a593Smuzhiyun 	iwl_pcie_txq_progress(txq);
816*4882a593Smuzhiyun 
817*4882a593Smuzhiyun 	if (iwl_txq_space(trans, txq) > txq->low_mark &&
818*4882a593Smuzhiyun 	    test_bit(txq_id, trans->txqs.queue_stopped)) {
819*4882a593Smuzhiyun 		struct sk_buff_head overflow_skbs;
820*4882a593Smuzhiyun 
821*4882a593Smuzhiyun 		__skb_queue_head_init(&overflow_skbs);
822*4882a593Smuzhiyun 		skb_queue_splice_init(&txq->overflow_q, &overflow_skbs);
823*4882a593Smuzhiyun 
824*4882a593Smuzhiyun 		/*
825*4882a593Smuzhiyun 		 * We are going to transmit from the overflow queue.
826*4882a593Smuzhiyun 		 * Remember this state so that wait_for_txq_empty will know we
827*4882a593Smuzhiyun 		 * are adding more packets to the TFD queue. It cannot rely on
828*4882a593Smuzhiyun 		 * the state of &txq->overflow_q, as we just emptied it, but
829*4882a593Smuzhiyun 		 * haven't TXed the content yet.
830*4882a593Smuzhiyun 		 */
831*4882a593Smuzhiyun 		txq->overflow_tx = true;
832*4882a593Smuzhiyun 
833*4882a593Smuzhiyun 		/*
834*4882a593Smuzhiyun 		 * This is tricky: we are in reclaim path which is non
835*4882a593Smuzhiyun 		 * re-entrant, so noone will try to take the access the
836*4882a593Smuzhiyun 		 * txq data from that path. We stopped tx, so we can't
837*4882a593Smuzhiyun 		 * have tx as well. Bottom line, we can unlock and re-lock
838*4882a593Smuzhiyun 		 * later.
839*4882a593Smuzhiyun 		 */
840*4882a593Smuzhiyun 		spin_unlock_bh(&txq->lock);
841*4882a593Smuzhiyun 
842*4882a593Smuzhiyun 		while (!skb_queue_empty(&overflow_skbs)) {
843*4882a593Smuzhiyun 			struct sk_buff *skb = __skb_dequeue(&overflow_skbs);
844*4882a593Smuzhiyun 			struct iwl_device_tx_cmd *dev_cmd_ptr;
845*4882a593Smuzhiyun 
846*4882a593Smuzhiyun 			dev_cmd_ptr = *(void **)((u8 *)skb->cb +
847*4882a593Smuzhiyun 						 trans->txqs.dev_cmd_offs);
848*4882a593Smuzhiyun 
849*4882a593Smuzhiyun 			/*
850*4882a593Smuzhiyun 			 * Note that we can very well be overflowing again.
851*4882a593Smuzhiyun 			 * In that case, iwl_txq_space will be small again
852*4882a593Smuzhiyun 			 * and we won't wake mac80211's queue.
853*4882a593Smuzhiyun 			 */
854*4882a593Smuzhiyun 			iwl_trans_tx(trans, skb, dev_cmd_ptr, txq_id);
855*4882a593Smuzhiyun 		}
856*4882a593Smuzhiyun 
857*4882a593Smuzhiyun 		if (iwl_txq_space(trans, txq) > txq->low_mark)
858*4882a593Smuzhiyun 			iwl_wake_queue(trans, txq);
859*4882a593Smuzhiyun 
860*4882a593Smuzhiyun 		spin_lock_bh(&txq->lock);
861*4882a593Smuzhiyun 		txq->overflow_tx = false;
862*4882a593Smuzhiyun 	}
863*4882a593Smuzhiyun 
864*4882a593Smuzhiyun out:
865*4882a593Smuzhiyun 	spin_unlock_bh(&txq->lock);
866*4882a593Smuzhiyun }
867*4882a593Smuzhiyun 
868*4882a593Smuzhiyun /* Set wr_ptr of specific device and txq  */
iwl_trans_pcie_set_q_ptrs(struct iwl_trans * trans,int txq_id,int ptr)869*4882a593Smuzhiyun void iwl_trans_pcie_set_q_ptrs(struct iwl_trans *trans, int txq_id, int ptr)
870*4882a593Smuzhiyun {
871*4882a593Smuzhiyun 	struct iwl_txq *txq = trans->txqs.txq[txq_id];
872*4882a593Smuzhiyun 
873*4882a593Smuzhiyun 	spin_lock_bh(&txq->lock);
874*4882a593Smuzhiyun 
875*4882a593Smuzhiyun 	txq->write_ptr = ptr;
876*4882a593Smuzhiyun 	txq->read_ptr = txq->write_ptr;
877*4882a593Smuzhiyun 
878*4882a593Smuzhiyun 	spin_unlock_bh(&txq->lock);
879*4882a593Smuzhiyun }
880*4882a593Smuzhiyun 
iwl_pcie_set_cmd_in_flight(struct iwl_trans * trans,const struct iwl_host_cmd * cmd)881*4882a593Smuzhiyun static int iwl_pcie_set_cmd_in_flight(struct iwl_trans *trans,
882*4882a593Smuzhiyun 				      const struct iwl_host_cmd *cmd)
883*4882a593Smuzhiyun {
884*4882a593Smuzhiyun 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
885*4882a593Smuzhiyun 	int ret;
886*4882a593Smuzhiyun 
887*4882a593Smuzhiyun 	lockdep_assert_held(&trans_pcie->reg_lock);
888*4882a593Smuzhiyun 
889*4882a593Smuzhiyun 	/* Make sure the NIC is still alive in the bus */
890*4882a593Smuzhiyun 	if (test_bit(STATUS_TRANS_DEAD, &trans->status))
891*4882a593Smuzhiyun 		return -ENODEV;
892*4882a593Smuzhiyun 
893*4882a593Smuzhiyun 	/*
894*4882a593Smuzhiyun 	 * wake up the NIC to make sure that the firmware will see the host
895*4882a593Smuzhiyun 	 * command - we will let the NIC sleep once all the host commands
896*4882a593Smuzhiyun 	 * returned. This needs to be done only on NICs that have
897*4882a593Smuzhiyun 	 * apmg_wake_up_wa set.
898*4882a593Smuzhiyun 	 */
899*4882a593Smuzhiyun 	if (trans->trans_cfg->base_params->apmg_wake_up_wa &&
900*4882a593Smuzhiyun 	    !trans_pcie->cmd_hold_nic_awake) {
901*4882a593Smuzhiyun 		__iwl_trans_pcie_set_bit(trans, CSR_GP_CNTRL,
902*4882a593Smuzhiyun 					 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
903*4882a593Smuzhiyun 
904*4882a593Smuzhiyun 		ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
905*4882a593Smuzhiyun 				   CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN,
906*4882a593Smuzhiyun 				   (CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
907*4882a593Smuzhiyun 				    CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP),
908*4882a593Smuzhiyun 				   15000);
909*4882a593Smuzhiyun 		if (ret < 0) {
910*4882a593Smuzhiyun 			__iwl_trans_pcie_clear_bit(trans, CSR_GP_CNTRL,
911*4882a593Smuzhiyun 					CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
912*4882a593Smuzhiyun 			IWL_ERR(trans, "Failed to wake NIC for hcmd\n");
913*4882a593Smuzhiyun 			return -EIO;
914*4882a593Smuzhiyun 		}
915*4882a593Smuzhiyun 		trans_pcie->cmd_hold_nic_awake = true;
916*4882a593Smuzhiyun 	}
917*4882a593Smuzhiyun 
918*4882a593Smuzhiyun 	return 0;
919*4882a593Smuzhiyun }
920*4882a593Smuzhiyun 
921*4882a593Smuzhiyun /*
922*4882a593Smuzhiyun  * iwl_pcie_cmdq_reclaim - Reclaim TX command queue entries already Tx'd
923*4882a593Smuzhiyun  *
924*4882a593Smuzhiyun  * When FW advances 'R' index, all entries between old and new 'R' index
925*4882a593Smuzhiyun  * need to be reclaimed. As result, some free space forms.  If there is
926*4882a593Smuzhiyun  * enough free space (> low mark), wake the stack that feeds us.
927*4882a593Smuzhiyun  */
iwl_pcie_cmdq_reclaim(struct iwl_trans * trans,int txq_id,int idx)928*4882a593Smuzhiyun static void iwl_pcie_cmdq_reclaim(struct iwl_trans *trans, int txq_id, int idx)
929*4882a593Smuzhiyun {
930*4882a593Smuzhiyun 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
931*4882a593Smuzhiyun 	struct iwl_txq *txq = trans->txqs.txq[txq_id];
932*4882a593Smuzhiyun 	int nfreed = 0;
933*4882a593Smuzhiyun 	u16 r;
934*4882a593Smuzhiyun 
935*4882a593Smuzhiyun 	lockdep_assert_held(&txq->lock);
936*4882a593Smuzhiyun 
937*4882a593Smuzhiyun 	idx = iwl_txq_get_cmd_index(txq, idx);
938*4882a593Smuzhiyun 	r = iwl_txq_get_cmd_index(txq, txq->read_ptr);
939*4882a593Smuzhiyun 
940*4882a593Smuzhiyun 	if (idx >= trans->trans_cfg->base_params->max_tfd_queue_size ||
941*4882a593Smuzhiyun 	    (!iwl_txq_used(txq, idx))) {
942*4882a593Smuzhiyun 		WARN_ONCE(test_bit(txq_id, trans->txqs.queue_used),
943*4882a593Smuzhiyun 			  "%s: Read index for DMA queue txq id (%d), index %d is out of range [0-%d] %d %d.\n",
944*4882a593Smuzhiyun 			  __func__, txq_id, idx,
945*4882a593Smuzhiyun 			  trans->trans_cfg->base_params->max_tfd_queue_size,
946*4882a593Smuzhiyun 			  txq->write_ptr, txq->read_ptr);
947*4882a593Smuzhiyun 		return;
948*4882a593Smuzhiyun 	}
949*4882a593Smuzhiyun 
950*4882a593Smuzhiyun 	for (idx = iwl_txq_inc_wrap(trans, idx); r != idx;
951*4882a593Smuzhiyun 	     r = iwl_txq_inc_wrap(trans, r)) {
952*4882a593Smuzhiyun 		txq->read_ptr = iwl_txq_inc_wrap(trans, txq->read_ptr);
953*4882a593Smuzhiyun 
954*4882a593Smuzhiyun 		if (nfreed++ > 0) {
955*4882a593Smuzhiyun 			IWL_ERR(trans, "HCMD skipped: index (%d) %d %d\n",
956*4882a593Smuzhiyun 				idx, txq->write_ptr, r);
957*4882a593Smuzhiyun 			iwl_force_nmi(trans);
958*4882a593Smuzhiyun 		}
959*4882a593Smuzhiyun 	}
960*4882a593Smuzhiyun 
961*4882a593Smuzhiyun 	if (txq->read_ptr == txq->write_ptr) {
962*4882a593Smuzhiyun 		/* BHs are also disabled due to txq->lock */
963*4882a593Smuzhiyun 		spin_lock(&trans_pcie->reg_lock);
964*4882a593Smuzhiyun 		iwl_pcie_clear_cmd_in_flight(trans);
965*4882a593Smuzhiyun 		spin_unlock(&trans_pcie->reg_lock);
966*4882a593Smuzhiyun 	}
967*4882a593Smuzhiyun 
968*4882a593Smuzhiyun 	iwl_pcie_txq_progress(txq);
969*4882a593Smuzhiyun }
970*4882a593Smuzhiyun 
iwl_pcie_txq_set_ratid_map(struct iwl_trans * trans,u16 ra_tid,u16 txq_id)971*4882a593Smuzhiyun static int iwl_pcie_txq_set_ratid_map(struct iwl_trans *trans, u16 ra_tid,
972*4882a593Smuzhiyun 				 u16 txq_id)
973*4882a593Smuzhiyun {
974*4882a593Smuzhiyun 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
975*4882a593Smuzhiyun 	u32 tbl_dw_addr;
976*4882a593Smuzhiyun 	u32 tbl_dw;
977*4882a593Smuzhiyun 	u16 scd_q2ratid;
978*4882a593Smuzhiyun 
979*4882a593Smuzhiyun 	scd_q2ratid = ra_tid & SCD_QUEUE_RA_TID_MAP_RATID_MSK;
980*4882a593Smuzhiyun 
981*4882a593Smuzhiyun 	tbl_dw_addr = trans_pcie->scd_base_addr +
982*4882a593Smuzhiyun 			SCD_TRANS_TBL_OFFSET_QUEUE(txq_id);
983*4882a593Smuzhiyun 
984*4882a593Smuzhiyun 	tbl_dw = iwl_trans_read_mem32(trans, tbl_dw_addr);
985*4882a593Smuzhiyun 
986*4882a593Smuzhiyun 	if (txq_id & 0x1)
987*4882a593Smuzhiyun 		tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF);
988*4882a593Smuzhiyun 	else
989*4882a593Smuzhiyun 		tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000);
990*4882a593Smuzhiyun 
991*4882a593Smuzhiyun 	iwl_trans_write_mem32(trans, tbl_dw_addr, tbl_dw);
992*4882a593Smuzhiyun 
993*4882a593Smuzhiyun 	return 0;
994*4882a593Smuzhiyun }
995*4882a593Smuzhiyun 
996*4882a593Smuzhiyun /* Receiver address (actually, Rx station's index into station table),
997*4882a593Smuzhiyun  * combined with Traffic ID (QOS priority), in format used by Tx Scheduler */
998*4882a593Smuzhiyun #define BUILD_RAxTID(sta_id, tid)	(((sta_id) << 4) + (tid))
999*4882a593Smuzhiyun 
iwl_trans_pcie_txq_enable(struct iwl_trans * trans,int txq_id,u16 ssn,const struct iwl_trans_txq_scd_cfg * cfg,unsigned int wdg_timeout)1000*4882a593Smuzhiyun bool iwl_trans_pcie_txq_enable(struct iwl_trans *trans, int txq_id, u16 ssn,
1001*4882a593Smuzhiyun 			       const struct iwl_trans_txq_scd_cfg *cfg,
1002*4882a593Smuzhiyun 			       unsigned int wdg_timeout)
1003*4882a593Smuzhiyun {
1004*4882a593Smuzhiyun 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1005*4882a593Smuzhiyun 	struct iwl_txq *txq = trans->txqs.txq[txq_id];
1006*4882a593Smuzhiyun 	int fifo = -1;
1007*4882a593Smuzhiyun 	bool scd_bug = false;
1008*4882a593Smuzhiyun 
1009*4882a593Smuzhiyun 	if (test_and_set_bit(txq_id, trans->txqs.queue_used))
1010*4882a593Smuzhiyun 		WARN_ONCE(1, "queue %d already used - expect issues", txq_id);
1011*4882a593Smuzhiyun 
1012*4882a593Smuzhiyun 	txq->wd_timeout = msecs_to_jiffies(wdg_timeout);
1013*4882a593Smuzhiyun 
1014*4882a593Smuzhiyun 	if (cfg) {
1015*4882a593Smuzhiyun 		fifo = cfg->fifo;
1016*4882a593Smuzhiyun 
1017*4882a593Smuzhiyun 		/* Disable the scheduler prior configuring the cmd queue */
1018*4882a593Smuzhiyun 		if (txq_id == trans->txqs.cmd.q_id &&
1019*4882a593Smuzhiyun 		    trans_pcie->scd_set_active)
1020*4882a593Smuzhiyun 			iwl_scd_enable_set_active(trans, 0);
1021*4882a593Smuzhiyun 
1022*4882a593Smuzhiyun 		/* Stop this Tx queue before configuring it */
1023*4882a593Smuzhiyun 		iwl_scd_txq_set_inactive(trans, txq_id);
1024*4882a593Smuzhiyun 
1025*4882a593Smuzhiyun 		/* Set this queue as a chain-building queue unless it is CMD */
1026*4882a593Smuzhiyun 		if (txq_id != trans->txqs.cmd.q_id)
1027*4882a593Smuzhiyun 			iwl_scd_txq_set_chain(trans, txq_id);
1028*4882a593Smuzhiyun 
1029*4882a593Smuzhiyun 		if (cfg->aggregate) {
1030*4882a593Smuzhiyun 			u16 ra_tid = BUILD_RAxTID(cfg->sta_id, cfg->tid);
1031*4882a593Smuzhiyun 
1032*4882a593Smuzhiyun 			/* Map receiver-address / traffic-ID to this queue */
1033*4882a593Smuzhiyun 			iwl_pcie_txq_set_ratid_map(trans, ra_tid, txq_id);
1034*4882a593Smuzhiyun 
1035*4882a593Smuzhiyun 			/* enable aggregations for the queue */
1036*4882a593Smuzhiyun 			iwl_scd_txq_enable_agg(trans, txq_id);
1037*4882a593Smuzhiyun 			txq->ampdu = true;
1038*4882a593Smuzhiyun 		} else {
1039*4882a593Smuzhiyun 			/*
1040*4882a593Smuzhiyun 			 * disable aggregations for the queue, this will also
1041*4882a593Smuzhiyun 			 * make the ra_tid mapping configuration irrelevant
1042*4882a593Smuzhiyun 			 * since it is now a non-AGG queue.
1043*4882a593Smuzhiyun 			 */
1044*4882a593Smuzhiyun 			iwl_scd_txq_disable_agg(trans, txq_id);
1045*4882a593Smuzhiyun 
1046*4882a593Smuzhiyun 			ssn = txq->read_ptr;
1047*4882a593Smuzhiyun 		}
1048*4882a593Smuzhiyun 	} else {
1049*4882a593Smuzhiyun 		/*
1050*4882a593Smuzhiyun 		 * If we need to move the SCD write pointer by steps of
1051*4882a593Smuzhiyun 		 * 0x40, 0x80 or 0xc0, it gets stuck. Avoids this and let
1052*4882a593Smuzhiyun 		 * the op_mode know by returning true later.
1053*4882a593Smuzhiyun 		 * Do this only in case cfg is NULL since this trick can
1054*4882a593Smuzhiyun 		 * be done only if we have DQA enabled which is true for mvm
1055*4882a593Smuzhiyun 		 * only. And mvm never sets a cfg pointer.
1056*4882a593Smuzhiyun 		 * This is really ugly, but this is the easiest way out for
1057*4882a593Smuzhiyun 		 * this sad hardware issue.
1058*4882a593Smuzhiyun 		 * This bug has been fixed on devices 9000 and up.
1059*4882a593Smuzhiyun 		 */
1060*4882a593Smuzhiyun 		scd_bug = !trans->trans_cfg->mq_rx_supported &&
1061*4882a593Smuzhiyun 			!((ssn - txq->write_ptr) & 0x3f) &&
1062*4882a593Smuzhiyun 			(ssn != txq->write_ptr);
1063*4882a593Smuzhiyun 		if (scd_bug)
1064*4882a593Smuzhiyun 			ssn++;
1065*4882a593Smuzhiyun 	}
1066*4882a593Smuzhiyun 
1067*4882a593Smuzhiyun 	/* Place first TFD at index corresponding to start sequence number.
1068*4882a593Smuzhiyun 	 * Assumes that ssn_idx is valid (!= 0xFFF) */
1069*4882a593Smuzhiyun 	txq->read_ptr = (ssn & 0xff);
1070*4882a593Smuzhiyun 	txq->write_ptr = (ssn & 0xff);
1071*4882a593Smuzhiyun 	iwl_write_direct32(trans, HBUS_TARG_WRPTR,
1072*4882a593Smuzhiyun 			   (ssn & 0xff) | (txq_id << 8));
1073*4882a593Smuzhiyun 
1074*4882a593Smuzhiyun 	if (cfg) {
1075*4882a593Smuzhiyun 		u8 frame_limit = cfg->frame_limit;
1076*4882a593Smuzhiyun 
1077*4882a593Smuzhiyun 		iwl_write_prph(trans, SCD_QUEUE_RDPTR(txq_id), ssn);
1078*4882a593Smuzhiyun 
1079*4882a593Smuzhiyun 		/* Set up Tx window size and frame limit for this queue */
1080*4882a593Smuzhiyun 		iwl_trans_write_mem32(trans, trans_pcie->scd_base_addr +
1081*4882a593Smuzhiyun 				SCD_CONTEXT_QUEUE_OFFSET(txq_id), 0);
1082*4882a593Smuzhiyun 		iwl_trans_write_mem32(trans,
1083*4882a593Smuzhiyun 			trans_pcie->scd_base_addr +
1084*4882a593Smuzhiyun 			SCD_CONTEXT_QUEUE_OFFSET(txq_id) + sizeof(u32),
1085*4882a593Smuzhiyun 			SCD_QUEUE_CTX_REG2_VAL(WIN_SIZE, frame_limit) |
1086*4882a593Smuzhiyun 			SCD_QUEUE_CTX_REG2_VAL(FRAME_LIMIT, frame_limit));
1087*4882a593Smuzhiyun 
1088*4882a593Smuzhiyun 		/* Set up status area in SRAM, map to Tx DMA/FIFO, activate */
1089*4882a593Smuzhiyun 		iwl_write_prph(trans, SCD_QUEUE_STATUS_BITS(txq_id),
1090*4882a593Smuzhiyun 			       (1 << SCD_QUEUE_STTS_REG_POS_ACTIVE) |
1091*4882a593Smuzhiyun 			       (cfg->fifo << SCD_QUEUE_STTS_REG_POS_TXF) |
1092*4882a593Smuzhiyun 			       (1 << SCD_QUEUE_STTS_REG_POS_WSL) |
1093*4882a593Smuzhiyun 			       SCD_QUEUE_STTS_REG_MSK);
1094*4882a593Smuzhiyun 
1095*4882a593Smuzhiyun 		/* enable the scheduler for this queue (only) */
1096*4882a593Smuzhiyun 		if (txq_id == trans->txqs.cmd.q_id &&
1097*4882a593Smuzhiyun 		    trans_pcie->scd_set_active)
1098*4882a593Smuzhiyun 			iwl_scd_enable_set_active(trans, BIT(txq_id));
1099*4882a593Smuzhiyun 
1100*4882a593Smuzhiyun 		IWL_DEBUG_TX_QUEUES(trans,
1101*4882a593Smuzhiyun 				    "Activate queue %d on FIFO %d WrPtr: %d\n",
1102*4882a593Smuzhiyun 				    txq_id, fifo, ssn & 0xff);
1103*4882a593Smuzhiyun 	} else {
1104*4882a593Smuzhiyun 		IWL_DEBUG_TX_QUEUES(trans,
1105*4882a593Smuzhiyun 				    "Activate queue %d WrPtr: %d\n",
1106*4882a593Smuzhiyun 				    txq_id, ssn & 0xff);
1107*4882a593Smuzhiyun 	}
1108*4882a593Smuzhiyun 
1109*4882a593Smuzhiyun 	return scd_bug;
1110*4882a593Smuzhiyun }
1111*4882a593Smuzhiyun 
iwl_trans_pcie_txq_set_shared_mode(struct iwl_trans * trans,u32 txq_id,bool shared_mode)1112*4882a593Smuzhiyun void iwl_trans_pcie_txq_set_shared_mode(struct iwl_trans *trans, u32 txq_id,
1113*4882a593Smuzhiyun 					bool shared_mode)
1114*4882a593Smuzhiyun {
1115*4882a593Smuzhiyun 	struct iwl_txq *txq = trans->txqs.txq[txq_id];
1116*4882a593Smuzhiyun 
1117*4882a593Smuzhiyun 	txq->ampdu = !shared_mode;
1118*4882a593Smuzhiyun }
1119*4882a593Smuzhiyun 
iwl_trans_pcie_txq_disable(struct iwl_trans * trans,int txq_id,bool configure_scd)1120*4882a593Smuzhiyun void iwl_trans_pcie_txq_disable(struct iwl_trans *trans, int txq_id,
1121*4882a593Smuzhiyun 				bool configure_scd)
1122*4882a593Smuzhiyun {
1123*4882a593Smuzhiyun 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1124*4882a593Smuzhiyun 	u32 stts_addr = trans_pcie->scd_base_addr +
1125*4882a593Smuzhiyun 			SCD_TX_STTS_QUEUE_OFFSET(txq_id);
1126*4882a593Smuzhiyun 	static const u32 zero_val[4] = {};
1127*4882a593Smuzhiyun 
1128*4882a593Smuzhiyun 	trans->txqs.txq[txq_id]->frozen_expiry_remainder = 0;
1129*4882a593Smuzhiyun 	trans->txqs.txq[txq_id]->frozen = false;
1130*4882a593Smuzhiyun 
1131*4882a593Smuzhiyun 	/*
1132*4882a593Smuzhiyun 	 * Upon HW Rfkill - we stop the device, and then stop the queues
1133*4882a593Smuzhiyun 	 * in the op_mode. Just for the sake of the simplicity of the op_mode,
1134*4882a593Smuzhiyun 	 * allow the op_mode to call txq_disable after it already called
1135*4882a593Smuzhiyun 	 * stop_device.
1136*4882a593Smuzhiyun 	 */
1137*4882a593Smuzhiyun 	if (!test_and_clear_bit(txq_id, trans->txqs.queue_used)) {
1138*4882a593Smuzhiyun 		WARN_ONCE(test_bit(STATUS_DEVICE_ENABLED, &trans->status),
1139*4882a593Smuzhiyun 			  "queue %d not used", txq_id);
1140*4882a593Smuzhiyun 		return;
1141*4882a593Smuzhiyun 	}
1142*4882a593Smuzhiyun 
1143*4882a593Smuzhiyun 	if (configure_scd) {
1144*4882a593Smuzhiyun 		iwl_scd_txq_set_inactive(trans, txq_id);
1145*4882a593Smuzhiyun 
1146*4882a593Smuzhiyun 		iwl_trans_write_mem(trans, stts_addr, (void *)zero_val,
1147*4882a593Smuzhiyun 				    ARRAY_SIZE(zero_val));
1148*4882a593Smuzhiyun 	}
1149*4882a593Smuzhiyun 
1150*4882a593Smuzhiyun 	iwl_pcie_txq_unmap(trans, txq_id);
1151*4882a593Smuzhiyun 	trans->txqs.txq[txq_id]->ampdu = false;
1152*4882a593Smuzhiyun 
1153*4882a593Smuzhiyun 	IWL_DEBUG_TX_QUEUES(trans, "Deactivate queue %d\n", txq_id);
1154*4882a593Smuzhiyun }
1155*4882a593Smuzhiyun 
1156*4882a593Smuzhiyun /*************** HOST COMMAND QUEUE FUNCTIONS   *****/
1157*4882a593Smuzhiyun 
1158*4882a593Smuzhiyun /*
1159*4882a593Smuzhiyun  * iwl_pcie_enqueue_hcmd - enqueue a uCode command
1160*4882a593Smuzhiyun  * @priv: device private data point
1161*4882a593Smuzhiyun  * @cmd: a pointer to the ucode command structure
1162*4882a593Smuzhiyun  *
1163*4882a593Smuzhiyun  * The function returns < 0 values to indicate the operation
1164*4882a593Smuzhiyun  * failed. On success, it returns the index (>= 0) of command in the
1165*4882a593Smuzhiyun  * command queue.
1166*4882a593Smuzhiyun  */
iwl_pcie_enqueue_hcmd(struct iwl_trans * trans,struct iwl_host_cmd * cmd)1167*4882a593Smuzhiyun static int iwl_pcie_enqueue_hcmd(struct iwl_trans *trans,
1168*4882a593Smuzhiyun 				 struct iwl_host_cmd *cmd)
1169*4882a593Smuzhiyun {
1170*4882a593Smuzhiyun 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1171*4882a593Smuzhiyun 	struct iwl_txq *txq = trans->txqs.txq[trans->txqs.cmd.q_id];
1172*4882a593Smuzhiyun 	struct iwl_device_cmd *out_cmd;
1173*4882a593Smuzhiyun 	struct iwl_cmd_meta *out_meta;
1174*4882a593Smuzhiyun 	void *dup_buf = NULL;
1175*4882a593Smuzhiyun 	dma_addr_t phys_addr;
1176*4882a593Smuzhiyun 	int idx;
1177*4882a593Smuzhiyun 	u16 copy_size, cmd_size, tb0_size;
1178*4882a593Smuzhiyun 	bool had_nocopy = false;
1179*4882a593Smuzhiyun 	u8 group_id = iwl_cmd_groupid(cmd->id);
1180*4882a593Smuzhiyun 	int i, ret;
1181*4882a593Smuzhiyun 	u32 cmd_pos;
1182*4882a593Smuzhiyun 	const u8 *cmddata[IWL_MAX_CMD_TBS_PER_TFD];
1183*4882a593Smuzhiyun 	u16 cmdlen[IWL_MAX_CMD_TBS_PER_TFD];
1184*4882a593Smuzhiyun 	unsigned long flags;
1185*4882a593Smuzhiyun 
1186*4882a593Smuzhiyun 	if (WARN(!trans->wide_cmd_header &&
1187*4882a593Smuzhiyun 		 group_id > IWL_ALWAYS_LONG_GROUP,
1188*4882a593Smuzhiyun 		 "unsupported wide command %#x\n", cmd->id))
1189*4882a593Smuzhiyun 		return -EINVAL;
1190*4882a593Smuzhiyun 
1191*4882a593Smuzhiyun 	if (group_id != 0) {
1192*4882a593Smuzhiyun 		copy_size = sizeof(struct iwl_cmd_header_wide);
1193*4882a593Smuzhiyun 		cmd_size = sizeof(struct iwl_cmd_header_wide);
1194*4882a593Smuzhiyun 	} else {
1195*4882a593Smuzhiyun 		copy_size = sizeof(struct iwl_cmd_header);
1196*4882a593Smuzhiyun 		cmd_size = sizeof(struct iwl_cmd_header);
1197*4882a593Smuzhiyun 	}
1198*4882a593Smuzhiyun 
1199*4882a593Smuzhiyun 	/* need one for the header if the first is NOCOPY */
1200*4882a593Smuzhiyun 	BUILD_BUG_ON(IWL_MAX_CMD_TBS_PER_TFD > IWL_NUM_OF_TBS - 1);
1201*4882a593Smuzhiyun 
1202*4882a593Smuzhiyun 	for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
1203*4882a593Smuzhiyun 		cmddata[i] = cmd->data[i];
1204*4882a593Smuzhiyun 		cmdlen[i] = cmd->len[i];
1205*4882a593Smuzhiyun 
1206*4882a593Smuzhiyun 		if (!cmd->len[i])
1207*4882a593Smuzhiyun 			continue;
1208*4882a593Smuzhiyun 
1209*4882a593Smuzhiyun 		/* need at least IWL_FIRST_TB_SIZE copied */
1210*4882a593Smuzhiyun 		if (copy_size < IWL_FIRST_TB_SIZE) {
1211*4882a593Smuzhiyun 			int copy = IWL_FIRST_TB_SIZE - copy_size;
1212*4882a593Smuzhiyun 
1213*4882a593Smuzhiyun 			if (copy > cmdlen[i])
1214*4882a593Smuzhiyun 				copy = cmdlen[i];
1215*4882a593Smuzhiyun 			cmdlen[i] -= copy;
1216*4882a593Smuzhiyun 			cmddata[i] += copy;
1217*4882a593Smuzhiyun 			copy_size += copy;
1218*4882a593Smuzhiyun 		}
1219*4882a593Smuzhiyun 
1220*4882a593Smuzhiyun 		if (cmd->dataflags[i] & IWL_HCMD_DFL_NOCOPY) {
1221*4882a593Smuzhiyun 			had_nocopy = true;
1222*4882a593Smuzhiyun 			if (WARN_ON(cmd->dataflags[i] & IWL_HCMD_DFL_DUP)) {
1223*4882a593Smuzhiyun 				idx = -EINVAL;
1224*4882a593Smuzhiyun 				goto free_dup_buf;
1225*4882a593Smuzhiyun 			}
1226*4882a593Smuzhiyun 		} else if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP) {
1227*4882a593Smuzhiyun 			/*
1228*4882a593Smuzhiyun 			 * This is also a chunk that isn't copied
1229*4882a593Smuzhiyun 			 * to the static buffer so set had_nocopy.
1230*4882a593Smuzhiyun 			 */
1231*4882a593Smuzhiyun 			had_nocopy = true;
1232*4882a593Smuzhiyun 
1233*4882a593Smuzhiyun 			/* only allowed once */
1234*4882a593Smuzhiyun 			if (WARN_ON(dup_buf)) {
1235*4882a593Smuzhiyun 				idx = -EINVAL;
1236*4882a593Smuzhiyun 				goto free_dup_buf;
1237*4882a593Smuzhiyun 			}
1238*4882a593Smuzhiyun 
1239*4882a593Smuzhiyun 			dup_buf = kmemdup(cmddata[i], cmdlen[i],
1240*4882a593Smuzhiyun 					  GFP_ATOMIC);
1241*4882a593Smuzhiyun 			if (!dup_buf)
1242*4882a593Smuzhiyun 				return -ENOMEM;
1243*4882a593Smuzhiyun 		} else {
1244*4882a593Smuzhiyun 			/* NOCOPY must not be followed by normal! */
1245*4882a593Smuzhiyun 			if (WARN_ON(had_nocopy)) {
1246*4882a593Smuzhiyun 				idx = -EINVAL;
1247*4882a593Smuzhiyun 				goto free_dup_buf;
1248*4882a593Smuzhiyun 			}
1249*4882a593Smuzhiyun 			copy_size += cmdlen[i];
1250*4882a593Smuzhiyun 		}
1251*4882a593Smuzhiyun 		cmd_size += cmd->len[i];
1252*4882a593Smuzhiyun 	}
1253*4882a593Smuzhiyun 
1254*4882a593Smuzhiyun 	/*
1255*4882a593Smuzhiyun 	 * If any of the command structures end up being larger than
1256*4882a593Smuzhiyun 	 * the TFD_MAX_PAYLOAD_SIZE and they aren't dynamically
1257*4882a593Smuzhiyun 	 * allocated into separate TFDs, then we will need to
1258*4882a593Smuzhiyun 	 * increase the size of the buffers.
1259*4882a593Smuzhiyun 	 */
1260*4882a593Smuzhiyun 	if (WARN(copy_size > TFD_MAX_PAYLOAD_SIZE,
1261*4882a593Smuzhiyun 		 "Command %s (%#x) is too large (%d bytes)\n",
1262*4882a593Smuzhiyun 		 iwl_get_cmd_string(trans, cmd->id),
1263*4882a593Smuzhiyun 		 cmd->id, copy_size)) {
1264*4882a593Smuzhiyun 		idx = -EINVAL;
1265*4882a593Smuzhiyun 		goto free_dup_buf;
1266*4882a593Smuzhiyun 	}
1267*4882a593Smuzhiyun 
1268*4882a593Smuzhiyun 	spin_lock_irqsave(&txq->lock, flags);
1269*4882a593Smuzhiyun 
1270*4882a593Smuzhiyun 	if (iwl_txq_space(trans, txq) < ((cmd->flags & CMD_ASYNC) ? 2 : 1)) {
1271*4882a593Smuzhiyun 		spin_unlock_irqrestore(&txq->lock, flags);
1272*4882a593Smuzhiyun 
1273*4882a593Smuzhiyun 		IWL_ERR(trans, "No space in command queue\n");
1274*4882a593Smuzhiyun 		iwl_op_mode_cmd_queue_full(trans->op_mode);
1275*4882a593Smuzhiyun 		idx = -ENOSPC;
1276*4882a593Smuzhiyun 		goto free_dup_buf;
1277*4882a593Smuzhiyun 	}
1278*4882a593Smuzhiyun 
1279*4882a593Smuzhiyun 	idx = iwl_txq_get_cmd_index(txq, txq->write_ptr);
1280*4882a593Smuzhiyun 	out_cmd = txq->entries[idx].cmd;
1281*4882a593Smuzhiyun 	out_meta = &txq->entries[idx].meta;
1282*4882a593Smuzhiyun 
1283*4882a593Smuzhiyun 	memset(out_meta, 0, sizeof(*out_meta));	/* re-initialize to NULL */
1284*4882a593Smuzhiyun 	if (cmd->flags & CMD_WANT_SKB)
1285*4882a593Smuzhiyun 		out_meta->source = cmd;
1286*4882a593Smuzhiyun 
1287*4882a593Smuzhiyun 	/* set up the header */
1288*4882a593Smuzhiyun 	if (group_id != 0) {
1289*4882a593Smuzhiyun 		out_cmd->hdr_wide.cmd = iwl_cmd_opcode(cmd->id);
1290*4882a593Smuzhiyun 		out_cmd->hdr_wide.group_id = group_id;
1291*4882a593Smuzhiyun 		out_cmd->hdr_wide.version = iwl_cmd_version(cmd->id);
1292*4882a593Smuzhiyun 		out_cmd->hdr_wide.length =
1293*4882a593Smuzhiyun 			cpu_to_le16(cmd_size -
1294*4882a593Smuzhiyun 				    sizeof(struct iwl_cmd_header_wide));
1295*4882a593Smuzhiyun 		out_cmd->hdr_wide.reserved = 0;
1296*4882a593Smuzhiyun 		out_cmd->hdr_wide.sequence =
1297*4882a593Smuzhiyun 			cpu_to_le16(QUEUE_TO_SEQ(trans->txqs.cmd.q_id) |
1298*4882a593Smuzhiyun 						 INDEX_TO_SEQ(txq->write_ptr));
1299*4882a593Smuzhiyun 
1300*4882a593Smuzhiyun 		cmd_pos = sizeof(struct iwl_cmd_header_wide);
1301*4882a593Smuzhiyun 		copy_size = sizeof(struct iwl_cmd_header_wide);
1302*4882a593Smuzhiyun 	} else {
1303*4882a593Smuzhiyun 		out_cmd->hdr.cmd = iwl_cmd_opcode(cmd->id);
1304*4882a593Smuzhiyun 		out_cmd->hdr.sequence =
1305*4882a593Smuzhiyun 			cpu_to_le16(QUEUE_TO_SEQ(trans->txqs.cmd.q_id) |
1306*4882a593Smuzhiyun 						 INDEX_TO_SEQ(txq->write_ptr));
1307*4882a593Smuzhiyun 		out_cmd->hdr.group_id = 0;
1308*4882a593Smuzhiyun 
1309*4882a593Smuzhiyun 		cmd_pos = sizeof(struct iwl_cmd_header);
1310*4882a593Smuzhiyun 		copy_size = sizeof(struct iwl_cmd_header);
1311*4882a593Smuzhiyun 	}
1312*4882a593Smuzhiyun 
1313*4882a593Smuzhiyun 	/* and copy the data that needs to be copied */
1314*4882a593Smuzhiyun 	for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
1315*4882a593Smuzhiyun 		int copy;
1316*4882a593Smuzhiyun 
1317*4882a593Smuzhiyun 		if (!cmd->len[i])
1318*4882a593Smuzhiyun 			continue;
1319*4882a593Smuzhiyun 
1320*4882a593Smuzhiyun 		/* copy everything if not nocopy/dup */
1321*4882a593Smuzhiyun 		if (!(cmd->dataflags[i] & (IWL_HCMD_DFL_NOCOPY |
1322*4882a593Smuzhiyun 					   IWL_HCMD_DFL_DUP))) {
1323*4882a593Smuzhiyun 			copy = cmd->len[i];
1324*4882a593Smuzhiyun 
1325*4882a593Smuzhiyun 			memcpy((u8 *)out_cmd + cmd_pos, cmd->data[i], copy);
1326*4882a593Smuzhiyun 			cmd_pos += copy;
1327*4882a593Smuzhiyun 			copy_size += copy;
1328*4882a593Smuzhiyun 			continue;
1329*4882a593Smuzhiyun 		}
1330*4882a593Smuzhiyun 
1331*4882a593Smuzhiyun 		/*
1332*4882a593Smuzhiyun 		 * Otherwise we need at least IWL_FIRST_TB_SIZE copied
1333*4882a593Smuzhiyun 		 * in total (for bi-directional DMA), but copy up to what
1334*4882a593Smuzhiyun 		 * we can fit into the payload for debug dump purposes.
1335*4882a593Smuzhiyun 		 */
1336*4882a593Smuzhiyun 		copy = min_t(int, TFD_MAX_PAYLOAD_SIZE - cmd_pos, cmd->len[i]);
1337*4882a593Smuzhiyun 
1338*4882a593Smuzhiyun 		memcpy((u8 *)out_cmd + cmd_pos, cmd->data[i], copy);
1339*4882a593Smuzhiyun 		cmd_pos += copy;
1340*4882a593Smuzhiyun 
1341*4882a593Smuzhiyun 		/* However, treat copy_size the proper way, we need it below */
1342*4882a593Smuzhiyun 		if (copy_size < IWL_FIRST_TB_SIZE) {
1343*4882a593Smuzhiyun 			copy = IWL_FIRST_TB_SIZE - copy_size;
1344*4882a593Smuzhiyun 
1345*4882a593Smuzhiyun 			if (copy > cmd->len[i])
1346*4882a593Smuzhiyun 				copy = cmd->len[i];
1347*4882a593Smuzhiyun 			copy_size += copy;
1348*4882a593Smuzhiyun 		}
1349*4882a593Smuzhiyun 	}
1350*4882a593Smuzhiyun 
1351*4882a593Smuzhiyun 	IWL_DEBUG_HC(trans,
1352*4882a593Smuzhiyun 		     "Sending command %s (%.2x.%.2x), seq: 0x%04X, %d bytes at %d[%d]:%d\n",
1353*4882a593Smuzhiyun 		     iwl_get_cmd_string(trans, cmd->id),
1354*4882a593Smuzhiyun 		     group_id, out_cmd->hdr.cmd,
1355*4882a593Smuzhiyun 		     le16_to_cpu(out_cmd->hdr.sequence),
1356*4882a593Smuzhiyun 		     cmd_size, txq->write_ptr, idx, trans->txqs.cmd.q_id);
1357*4882a593Smuzhiyun 
1358*4882a593Smuzhiyun 	/* start the TFD with the minimum copy bytes */
1359*4882a593Smuzhiyun 	tb0_size = min_t(int, copy_size, IWL_FIRST_TB_SIZE);
1360*4882a593Smuzhiyun 	memcpy(&txq->first_tb_bufs[idx], &out_cmd->hdr, tb0_size);
1361*4882a593Smuzhiyun 	iwl_pcie_txq_build_tfd(trans, txq,
1362*4882a593Smuzhiyun 			       iwl_txq_get_first_tb_dma(txq, idx),
1363*4882a593Smuzhiyun 			       tb0_size, true);
1364*4882a593Smuzhiyun 
1365*4882a593Smuzhiyun 	/* map first command fragment, if any remains */
1366*4882a593Smuzhiyun 	if (copy_size > tb0_size) {
1367*4882a593Smuzhiyun 		phys_addr = dma_map_single(trans->dev,
1368*4882a593Smuzhiyun 					   ((u8 *)&out_cmd->hdr) + tb0_size,
1369*4882a593Smuzhiyun 					   copy_size - tb0_size,
1370*4882a593Smuzhiyun 					   DMA_TO_DEVICE);
1371*4882a593Smuzhiyun 		if (dma_mapping_error(trans->dev, phys_addr)) {
1372*4882a593Smuzhiyun 			iwl_txq_gen1_tfd_unmap(trans, out_meta, txq,
1373*4882a593Smuzhiyun 					       txq->write_ptr);
1374*4882a593Smuzhiyun 			idx = -ENOMEM;
1375*4882a593Smuzhiyun 			goto out;
1376*4882a593Smuzhiyun 		}
1377*4882a593Smuzhiyun 
1378*4882a593Smuzhiyun 		iwl_pcie_txq_build_tfd(trans, txq, phys_addr,
1379*4882a593Smuzhiyun 				       copy_size - tb0_size, false);
1380*4882a593Smuzhiyun 	}
1381*4882a593Smuzhiyun 
1382*4882a593Smuzhiyun 	/* map the remaining (adjusted) nocopy/dup fragments */
1383*4882a593Smuzhiyun 	for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
1384*4882a593Smuzhiyun 		const void *data = cmddata[i];
1385*4882a593Smuzhiyun 
1386*4882a593Smuzhiyun 		if (!cmdlen[i])
1387*4882a593Smuzhiyun 			continue;
1388*4882a593Smuzhiyun 		if (!(cmd->dataflags[i] & (IWL_HCMD_DFL_NOCOPY |
1389*4882a593Smuzhiyun 					   IWL_HCMD_DFL_DUP)))
1390*4882a593Smuzhiyun 			continue;
1391*4882a593Smuzhiyun 		if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP)
1392*4882a593Smuzhiyun 			data = dup_buf;
1393*4882a593Smuzhiyun 		phys_addr = dma_map_single(trans->dev, (void *)data,
1394*4882a593Smuzhiyun 					   cmdlen[i], DMA_TO_DEVICE);
1395*4882a593Smuzhiyun 		if (dma_mapping_error(trans->dev, phys_addr)) {
1396*4882a593Smuzhiyun 			iwl_txq_gen1_tfd_unmap(trans, out_meta, txq,
1397*4882a593Smuzhiyun 					       txq->write_ptr);
1398*4882a593Smuzhiyun 			idx = -ENOMEM;
1399*4882a593Smuzhiyun 			goto out;
1400*4882a593Smuzhiyun 		}
1401*4882a593Smuzhiyun 
1402*4882a593Smuzhiyun 		iwl_pcie_txq_build_tfd(trans, txq, phys_addr, cmdlen[i], false);
1403*4882a593Smuzhiyun 	}
1404*4882a593Smuzhiyun 
1405*4882a593Smuzhiyun 	BUILD_BUG_ON(IWL_TFH_NUM_TBS > sizeof(out_meta->tbs) * BITS_PER_BYTE);
1406*4882a593Smuzhiyun 	out_meta->flags = cmd->flags;
1407*4882a593Smuzhiyun 	if (WARN_ON_ONCE(txq->entries[idx].free_buf))
1408*4882a593Smuzhiyun 		kfree_sensitive(txq->entries[idx].free_buf);
1409*4882a593Smuzhiyun 	txq->entries[idx].free_buf = dup_buf;
1410*4882a593Smuzhiyun 
1411*4882a593Smuzhiyun 	trace_iwlwifi_dev_hcmd(trans->dev, cmd, cmd_size, &out_cmd->hdr_wide);
1412*4882a593Smuzhiyun 
1413*4882a593Smuzhiyun 	/* start timer if queue currently empty */
1414*4882a593Smuzhiyun 	if (txq->read_ptr == txq->write_ptr && txq->wd_timeout)
1415*4882a593Smuzhiyun 		mod_timer(&txq->stuck_timer, jiffies + txq->wd_timeout);
1416*4882a593Smuzhiyun 
1417*4882a593Smuzhiyun 	spin_lock(&trans_pcie->reg_lock);
1418*4882a593Smuzhiyun 	ret = iwl_pcie_set_cmd_in_flight(trans, cmd);
1419*4882a593Smuzhiyun 	if (ret < 0) {
1420*4882a593Smuzhiyun 		idx = ret;
1421*4882a593Smuzhiyun 		goto unlock_reg;
1422*4882a593Smuzhiyun 	}
1423*4882a593Smuzhiyun 
1424*4882a593Smuzhiyun 	/* Increment and update queue's write index */
1425*4882a593Smuzhiyun 	txq->write_ptr = iwl_txq_inc_wrap(trans, txq->write_ptr);
1426*4882a593Smuzhiyun 	iwl_pcie_txq_inc_wr_ptr(trans, txq);
1427*4882a593Smuzhiyun 
1428*4882a593Smuzhiyun  unlock_reg:
1429*4882a593Smuzhiyun 	spin_unlock(&trans_pcie->reg_lock);
1430*4882a593Smuzhiyun  out:
1431*4882a593Smuzhiyun 	spin_unlock_irqrestore(&txq->lock, flags);
1432*4882a593Smuzhiyun  free_dup_buf:
1433*4882a593Smuzhiyun 	if (idx < 0)
1434*4882a593Smuzhiyun 		kfree(dup_buf);
1435*4882a593Smuzhiyun 	return idx;
1436*4882a593Smuzhiyun }
1437*4882a593Smuzhiyun 
1438*4882a593Smuzhiyun /*
1439*4882a593Smuzhiyun  * iwl_pcie_hcmd_complete - Pull unused buffers off the queue and reclaim them
1440*4882a593Smuzhiyun  * @rxb: Rx buffer to reclaim
1441*4882a593Smuzhiyun  */
iwl_pcie_hcmd_complete(struct iwl_trans * trans,struct iwl_rx_cmd_buffer * rxb)1442*4882a593Smuzhiyun void iwl_pcie_hcmd_complete(struct iwl_trans *trans,
1443*4882a593Smuzhiyun 			    struct iwl_rx_cmd_buffer *rxb)
1444*4882a593Smuzhiyun {
1445*4882a593Smuzhiyun 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1446*4882a593Smuzhiyun 	u16 sequence = le16_to_cpu(pkt->hdr.sequence);
1447*4882a593Smuzhiyun 	u8 group_id;
1448*4882a593Smuzhiyun 	u32 cmd_id;
1449*4882a593Smuzhiyun 	int txq_id = SEQ_TO_QUEUE(sequence);
1450*4882a593Smuzhiyun 	int index = SEQ_TO_INDEX(sequence);
1451*4882a593Smuzhiyun 	int cmd_index;
1452*4882a593Smuzhiyun 	struct iwl_device_cmd *cmd;
1453*4882a593Smuzhiyun 	struct iwl_cmd_meta *meta;
1454*4882a593Smuzhiyun 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1455*4882a593Smuzhiyun 	struct iwl_txq *txq = trans->txqs.txq[trans->txqs.cmd.q_id];
1456*4882a593Smuzhiyun 
1457*4882a593Smuzhiyun 	/* If a Tx command is being handled and it isn't in the actual
1458*4882a593Smuzhiyun 	 * command queue then there a command routing bug has been introduced
1459*4882a593Smuzhiyun 	 * in the queue management code. */
1460*4882a593Smuzhiyun 	if (WARN(txq_id != trans->txqs.cmd.q_id,
1461*4882a593Smuzhiyun 		 "wrong command queue %d (should be %d), sequence 0x%X readp=%d writep=%d\n",
1462*4882a593Smuzhiyun 		 txq_id, trans->txqs.cmd.q_id, sequence, txq->read_ptr,
1463*4882a593Smuzhiyun 		 txq->write_ptr)) {
1464*4882a593Smuzhiyun 		iwl_print_hex_error(trans, pkt, 32);
1465*4882a593Smuzhiyun 		return;
1466*4882a593Smuzhiyun 	}
1467*4882a593Smuzhiyun 
1468*4882a593Smuzhiyun 	spin_lock_bh(&txq->lock);
1469*4882a593Smuzhiyun 
1470*4882a593Smuzhiyun 	cmd_index = iwl_txq_get_cmd_index(txq, index);
1471*4882a593Smuzhiyun 	cmd = txq->entries[cmd_index].cmd;
1472*4882a593Smuzhiyun 	meta = &txq->entries[cmd_index].meta;
1473*4882a593Smuzhiyun 	group_id = cmd->hdr.group_id;
1474*4882a593Smuzhiyun 	cmd_id = iwl_cmd_id(cmd->hdr.cmd, group_id, 0);
1475*4882a593Smuzhiyun 
1476*4882a593Smuzhiyun 	iwl_txq_gen1_tfd_unmap(trans, meta, txq, index);
1477*4882a593Smuzhiyun 
1478*4882a593Smuzhiyun 	/* Input error checking is done when commands are added to queue. */
1479*4882a593Smuzhiyun 	if (meta->flags & CMD_WANT_SKB) {
1480*4882a593Smuzhiyun 		struct page *p = rxb_steal_page(rxb);
1481*4882a593Smuzhiyun 
1482*4882a593Smuzhiyun 		meta->source->resp_pkt = pkt;
1483*4882a593Smuzhiyun 		meta->source->_rx_page_addr = (unsigned long)page_address(p);
1484*4882a593Smuzhiyun 		meta->source->_rx_page_order = trans_pcie->rx_page_order;
1485*4882a593Smuzhiyun 	}
1486*4882a593Smuzhiyun 
1487*4882a593Smuzhiyun 	if (meta->flags & CMD_WANT_ASYNC_CALLBACK)
1488*4882a593Smuzhiyun 		iwl_op_mode_async_cb(trans->op_mode, cmd);
1489*4882a593Smuzhiyun 
1490*4882a593Smuzhiyun 	iwl_pcie_cmdq_reclaim(trans, txq_id, index);
1491*4882a593Smuzhiyun 
1492*4882a593Smuzhiyun 	if (!(meta->flags & CMD_ASYNC)) {
1493*4882a593Smuzhiyun 		if (!test_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status)) {
1494*4882a593Smuzhiyun 			IWL_WARN(trans,
1495*4882a593Smuzhiyun 				 "HCMD_ACTIVE already clear for command %s\n",
1496*4882a593Smuzhiyun 				 iwl_get_cmd_string(trans, cmd_id));
1497*4882a593Smuzhiyun 		}
1498*4882a593Smuzhiyun 		clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
1499*4882a593Smuzhiyun 		IWL_DEBUG_INFO(trans, "Clearing HCMD_ACTIVE for command %s\n",
1500*4882a593Smuzhiyun 			       iwl_get_cmd_string(trans, cmd_id));
1501*4882a593Smuzhiyun 		wake_up(&trans_pcie->wait_command_queue);
1502*4882a593Smuzhiyun 	}
1503*4882a593Smuzhiyun 
1504*4882a593Smuzhiyun 	meta->flags = 0;
1505*4882a593Smuzhiyun 
1506*4882a593Smuzhiyun 	spin_unlock_bh(&txq->lock);
1507*4882a593Smuzhiyun }
1508*4882a593Smuzhiyun 
1509*4882a593Smuzhiyun #define HOST_COMPLETE_TIMEOUT	(2 * HZ)
1510*4882a593Smuzhiyun 
iwl_pcie_send_hcmd_async(struct iwl_trans * trans,struct iwl_host_cmd * cmd)1511*4882a593Smuzhiyun static int iwl_pcie_send_hcmd_async(struct iwl_trans *trans,
1512*4882a593Smuzhiyun 				    struct iwl_host_cmd *cmd)
1513*4882a593Smuzhiyun {
1514*4882a593Smuzhiyun 	int ret;
1515*4882a593Smuzhiyun 
1516*4882a593Smuzhiyun 	/* An asynchronous command can not expect an SKB to be set. */
1517*4882a593Smuzhiyun 	if (WARN_ON(cmd->flags & CMD_WANT_SKB))
1518*4882a593Smuzhiyun 		return -EINVAL;
1519*4882a593Smuzhiyun 
1520*4882a593Smuzhiyun 	ret = iwl_pcie_enqueue_hcmd(trans, cmd);
1521*4882a593Smuzhiyun 	if (ret < 0) {
1522*4882a593Smuzhiyun 		IWL_ERR(trans,
1523*4882a593Smuzhiyun 			"Error sending %s: enqueue_hcmd failed: %d\n",
1524*4882a593Smuzhiyun 			iwl_get_cmd_string(trans, cmd->id), ret);
1525*4882a593Smuzhiyun 		return ret;
1526*4882a593Smuzhiyun 	}
1527*4882a593Smuzhiyun 	return 0;
1528*4882a593Smuzhiyun }
1529*4882a593Smuzhiyun 
iwl_pcie_send_hcmd_sync(struct iwl_trans * trans,struct iwl_host_cmd * cmd)1530*4882a593Smuzhiyun static int iwl_pcie_send_hcmd_sync(struct iwl_trans *trans,
1531*4882a593Smuzhiyun 				   struct iwl_host_cmd *cmd)
1532*4882a593Smuzhiyun {
1533*4882a593Smuzhiyun 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1534*4882a593Smuzhiyun 	struct iwl_txq *txq = trans->txqs.txq[trans->txqs.cmd.q_id];
1535*4882a593Smuzhiyun 	int cmd_idx;
1536*4882a593Smuzhiyun 	int ret;
1537*4882a593Smuzhiyun 
1538*4882a593Smuzhiyun 	IWL_DEBUG_INFO(trans, "Attempting to send sync command %s\n",
1539*4882a593Smuzhiyun 		       iwl_get_cmd_string(trans, cmd->id));
1540*4882a593Smuzhiyun 
1541*4882a593Smuzhiyun 	if (WARN(test_and_set_bit(STATUS_SYNC_HCMD_ACTIVE,
1542*4882a593Smuzhiyun 				  &trans->status),
1543*4882a593Smuzhiyun 		 "Command %s: a command is already active!\n",
1544*4882a593Smuzhiyun 		 iwl_get_cmd_string(trans, cmd->id)))
1545*4882a593Smuzhiyun 		return -EIO;
1546*4882a593Smuzhiyun 
1547*4882a593Smuzhiyun 	IWL_DEBUG_INFO(trans, "Setting HCMD_ACTIVE for command %s\n",
1548*4882a593Smuzhiyun 		       iwl_get_cmd_string(trans, cmd->id));
1549*4882a593Smuzhiyun 
1550*4882a593Smuzhiyun 	cmd_idx = iwl_pcie_enqueue_hcmd(trans, cmd);
1551*4882a593Smuzhiyun 	if (cmd_idx < 0) {
1552*4882a593Smuzhiyun 		ret = cmd_idx;
1553*4882a593Smuzhiyun 		clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
1554*4882a593Smuzhiyun 		IWL_ERR(trans,
1555*4882a593Smuzhiyun 			"Error sending %s: enqueue_hcmd failed: %d\n",
1556*4882a593Smuzhiyun 			iwl_get_cmd_string(trans, cmd->id), ret);
1557*4882a593Smuzhiyun 		return ret;
1558*4882a593Smuzhiyun 	}
1559*4882a593Smuzhiyun 
1560*4882a593Smuzhiyun 	ret = wait_event_timeout(trans_pcie->wait_command_queue,
1561*4882a593Smuzhiyun 				 !test_bit(STATUS_SYNC_HCMD_ACTIVE,
1562*4882a593Smuzhiyun 					   &trans->status),
1563*4882a593Smuzhiyun 				 HOST_COMPLETE_TIMEOUT);
1564*4882a593Smuzhiyun 	if (!ret) {
1565*4882a593Smuzhiyun 		IWL_ERR(trans, "Error sending %s: time out after %dms.\n",
1566*4882a593Smuzhiyun 			iwl_get_cmd_string(trans, cmd->id),
1567*4882a593Smuzhiyun 			jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
1568*4882a593Smuzhiyun 
1569*4882a593Smuzhiyun 		IWL_ERR(trans, "Current CMD queue read_ptr %d write_ptr %d\n",
1570*4882a593Smuzhiyun 			txq->read_ptr, txq->write_ptr);
1571*4882a593Smuzhiyun 
1572*4882a593Smuzhiyun 		clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
1573*4882a593Smuzhiyun 		IWL_DEBUG_INFO(trans, "Clearing HCMD_ACTIVE for command %s\n",
1574*4882a593Smuzhiyun 			       iwl_get_cmd_string(trans, cmd->id));
1575*4882a593Smuzhiyun 		ret = -ETIMEDOUT;
1576*4882a593Smuzhiyun 
1577*4882a593Smuzhiyun 		iwl_trans_pcie_sync_nmi(trans);
1578*4882a593Smuzhiyun 		goto cancel;
1579*4882a593Smuzhiyun 	}
1580*4882a593Smuzhiyun 
1581*4882a593Smuzhiyun 	if (test_bit(STATUS_FW_ERROR, &trans->status)) {
1582*4882a593Smuzhiyun 		iwl_trans_pcie_dump_regs(trans);
1583*4882a593Smuzhiyun 		IWL_ERR(trans, "FW error in SYNC CMD %s\n",
1584*4882a593Smuzhiyun 			iwl_get_cmd_string(trans, cmd->id));
1585*4882a593Smuzhiyun 		dump_stack();
1586*4882a593Smuzhiyun 		ret = -EIO;
1587*4882a593Smuzhiyun 		goto cancel;
1588*4882a593Smuzhiyun 	}
1589*4882a593Smuzhiyun 
1590*4882a593Smuzhiyun 	if (!(cmd->flags & CMD_SEND_IN_RFKILL) &&
1591*4882a593Smuzhiyun 	    test_bit(STATUS_RFKILL_OPMODE, &trans->status)) {
1592*4882a593Smuzhiyun 		IWL_DEBUG_RF_KILL(trans, "RFKILL in SYNC CMD... no rsp\n");
1593*4882a593Smuzhiyun 		ret = -ERFKILL;
1594*4882a593Smuzhiyun 		goto cancel;
1595*4882a593Smuzhiyun 	}
1596*4882a593Smuzhiyun 
1597*4882a593Smuzhiyun 	if ((cmd->flags & CMD_WANT_SKB) && !cmd->resp_pkt) {
1598*4882a593Smuzhiyun 		IWL_ERR(trans, "Error: Response NULL in '%s'\n",
1599*4882a593Smuzhiyun 			iwl_get_cmd_string(trans, cmd->id));
1600*4882a593Smuzhiyun 		ret = -EIO;
1601*4882a593Smuzhiyun 		goto cancel;
1602*4882a593Smuzhiyun 	}
1603*4882a593Smuzhiyun 
1604*4882a593Smuzhiyun 	return 0;
1605*4882a593Smuzhiyun 
1606*4882a593Smuzhiyun cancel:
1607*4882a593Smuzhiyun 	if (cmd->flags & CMD_WANT_SKB) {
1608*4882a593Smuzhiyun 		/*
1609*4882a593Smuzhiyun 		 * Cancel the CMD_WANT_SKB flag for the cmd in the
1610*4882a593Smuzhiyun 		 * TX cmd queue. Otherwise in case the cmd comes
1611*4882a593Smuzhiyun 		 * in later, it will possibly set an invalid
1612*4882a593Smuzhiyun 		 * address (cmd->meta.source).
1613*4882a593Smuzhiyun 		 */
1614*4882a593Smuzhiyun 		txq->entries[cmd_idx].meta.flags &= ~CMD_WANT_SKB;
1615*4882a593Smuzhiyun 	}
1616*4882a593Smuzhiyun 
1617*4882a593Smuzhiyun 	if (cmd->resp_pkt) {
1618*4882a593Smuzhiyun 		iwl_free_resp(cmd);
1619*4882a593Smuzhiyun 		cmd->resp_pkt = NULL;
1620*4882a593Smuzhiyun 	}
1621*4882a593Smuzhiyun 
1622*4882a593Smuzhiyun 	return ret;
1623*4882a593Smuzhiyun }
1624*4882a593Smuzhiyun 
iwl_trans_pcie_send_hcmd(struct iwl_trans * trans,struct iwl_host_cmd * cmd)1625*4882a593Smuzhiyun int iwl_trans_pcie_send_hcmd(struct iwl_trans *trans, struct iwl_host_cmd *cmd)
1626*4882a593Smuzhiyun {
1627*4882a593Smuzhiyun 	/* Make sure the NIC is still alive in the bus */
1628*4882a593Smuzhiyun 	if (test_bit(STATUS_TRANS_DEAD, &trans->status))
1629*4882a593Smuzhiyun 		return -ENODEV;
1630*4882a593Smuzhiyun 
1631*4882a593Smuzhiyun 	if (!(cmd->flags & CMD_SEND_IN_RFKILL) &&
1632*4882a593Smuzhiyun 	    test_bit(STATUS_RFKILL_OPMODE, &trans->status)) {
1633*4882a593Smuzhiyun 		IWL_DEBUG_RF_KILL(trans, "Dropping CMD 0x%x: RF KILL\n",
1634*4882a593Smuzhiyun 				  cmd->id);
1635*4882a593Smuzhiyun 		return -ERFKILL;
1636*4882a593Smuzhiyun 	}
1637*4882a593Smuzhiyun 
1638*4882a593Smuzhiyun 	if (cmd->flags & CMD_ASYNC)
1639*4882a593Smuzhiyun 		return iwl_pcie_send_hcmd_async(trans, cmd);
1640*4882a593Smuzhiyun 
1641*4882a593Smuzhiyun 	/* We still can fail on RFKILL that can be asserted while we wait */
1642*4882a593Smuzhiyun 	return iwl_pcie_send_hcmd_sync(trans, cmd);
1643*4882a593Smuzhiyun }
1644*4882a593Smuzhiyun 
iwl_fill_data_tbs(struct iwl_trans * trans,struct sk_buff * skb,struct iwl_txq * txq,u8 hdr_len,struct iwl_cmd_meta * out_meta)1645*4882a593Smuzhiyun static int iwl_fill_data_tbs(struct iwl_trans *trans, struct sk_buff *skb,
1646*4882a593Smuzhiyun 			     struct iwl_txq *txq, u8 hdr_len,
1647*4882a593Smuzhiyun 			     struct iwl_cmd_meta *out_meta)
1648*4882a593Smuzhiyun {
1649*4882a593Smuzhiyun 	u16 head_tb_len;
1650*4882a593Smuzhiyun 	int i;
1651*4882a593Smuzhiyun 
1652*4882a593Smuzhiyun 	/*
1653*4882a593Smuzhiyun 	 * Set up TFD's third entry to point directly to remainder
1654*4882a593Smuzhiyun 	 * of skb's head, if any
1655*4882a593Smuzhiyun 	 */
1656*4882a593Smuzhiyun 	head_tb_len = skb_headlen(skb) - hdr_len;
1657*4882a593Smuzhiyun 
1658*4882a593Smuzhiyun 	if (head_tb_len > 0) {
1659*4882a593Smuzhiyun 		dma_addr_t tb_phys = dma_map_single(trans->dev,
1660*4882a593Smuzhiyun 						    skb->data + hdr_len,
1661*4882a593Smuzhiyun 						    head_tb_len, DMA_TO_DEVICE);
1662*4882a593Smuzhiyun 		if (unlikely(dma_mapping_error(trans->dev, tb_phys)))
1663*4882a593Smuzhiyun 			return -EINVAL;
1664*4882a593Smuzhiyun 		trace_iwlwifi_dev_tx_tb(trans->dev, skb, skb->data + hdr_len,
1665*4882a593Smuzhiyun 					tb_phys, head_tb_len);
1666*4882a593Smuzhiyun 		iwl_pcie_txq_build_tfd(trans, txq, tb_phys, head_tb_len, false);
1667*4882a593Smuzhiyun 	}
1668*4882a593Smuzhiyun 
1669*4882a593Smuzhiyun 	/* set up the remaining entries to point to the data */
1670*4882a593Smuzhiyun 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1671*4882a593Smuzhiyun 		const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1672*4882a593Smuzhiyun 		dma_addr_t tb_phys;
1673*4882a593Smuzhiyun 		int tb_idx;
1674*4882a593Smuzhiyun 
1675*4882a593Smuzhiyun 		if (!skb_frag_size(frag))
1676*4882a593Smuzhiyun 			continue;
1677*4882a593Smuzhiyun 
1678*4882a593Smuzhiyun 		tb_phys = skb_frag_dma_map(trans->dev, frag, 0,
1679*4882a593Smuzhiyun 					   skb_frag_size(frag), DMA_TO_DEVICE);
1680*4882a593Smuzhiyun 
1681*4882a593Smuzhiyun 		if (unlikely(dma_mapping_error(trans->dev, tb_phys)))
1682*4882a593Smuzhiyun 			return -EINVAL;
1683*4882a593Smuzhiyun 		trace_iwlwifi_dev_tx_tb(trans->dev, skb, skb_frag_address(frag),
1684*4882a593Smuzhiyun 					tb_phys, skb_frag_size(frag));
1685*4882a593Smuzhiyun 		tb_idx = iwl_pcie_txq_build_tfd(trans, txq, tb_phys,
1686*4882a593Smuzhiyun 						skb_frag_size(frag), false);
1687*4882a593Smuzhiyun 		if (tb_idx < 0)
1688*4882a593Smuzhiyun 			return tb_idx;
1689*4882a593Smuzhiyun 
1690*4882a593Smuzhiyun 		out_meta->tbs |= BIT(tb_idx);
1691*4882a593Smuzhiyun 	}
1692*4882a593Smuzhiyun 
1693*4882a593Smuzhiyun 	return 0;
1694*4882a593Smuzhiyun }
1695*4882a593Smuzhiyun 
1696*4882a593Smuzhiyun #ifdef CONFIG_INET
iwl_compute_pseudo_hdr_csum(void * iph,struct tcphdr * tcph,bool ipv6,unsigned int len)1697*4882a593Smuzhiyun static void iwl_compute_pseudo_hdr_csum(void *iph, struct tcphdr *tcph,
1698*4882a593Smuzhiyun 					bool ipv6, unsigned int len)
1699*4882a593Smuzhiyun {
1700*4882a593Smuzhiyun 	if (ipv6) {
1701*4882a593Smuzhiyun 		struct ipv6hdr *iphv6 = iph;
1702*4882a593Smuzhiyun 
1703*4882a593Smuzhiyun 		tcph->check = ~csum_ipv6_magic(&iphv6->saddr, &iphv6->daddr,
1704*4882a593Smuzhiyun 					       len + tcph->doff * 4,
1705*4882a593Smuzhiyun 					       IPPROTO_TCP, 0);
1706*4882a593Smuzhiyun 	} else {
1707*4882a593Smuzhiyun 		struct iphdr *iphv4 = iph;
1708*4882a593Smuzhiyun 
1709*4882a593Smuzhiyun 		ip_send_check(iphv4);
1710*4882a593Smuzhiyun 		tcph->check = ~csum_tcpudp_magic(iphv4->saddr, iphv4->daddr,
1711*4882a593Smuzhiyun 						 len + tcph->doff * 4,
1712*4882a593Smuzhiyun 						 IPPROTO_TCP, 0);
1713*4882a593Smuzhiyun 	}
1714*4882a593Smuzhiyun }
1715*4882a593Smuzhiyun 
iwl_fill_data_tbs_amsdu(struct iwl_trans * trans,struct sk_buff * skb,struct iwl_txq * txq,u8 hdr_len,struct iwl_cmd_meta * out_meta,struct iwl_device_tx_cmd * dev_cmd,u16 tb1_len)1716*4882a593Smuzhiyun static int iwl_fill_data_tbs_amsdu(struct iwl_trans *trans, struct sk_buff *skb,
1717*4882a593Smuzhiyun 				   struct iwl_txq *txq, u8 hdr_len,
1718*4882a593Smuzhiyun 				   struct iwl_cmd_meta *out_meta,
1719*4882a593Smuzhiyun 				   struct iwl_device_tx_cmd *dev_cmd,
1720*4882a593Smuzhiyun 				   u16 tb1_len)
1721*4882a593Smuzhiyun {
1722*4882a593Smuzhiyun 	struct iwl_tx_cmd *tx_cmd = (void *)dev_cmd->payload;
1723*4882a593Smuzhiyun 	struct iwl_trans_pcie *trans_pcie =
1724*4882a593Smuzhiyun 		IWL_TRANS_GET_PCIE_TRANS(txq->trans);
1725*4882a593Smuzhiyun 	struct ieee80211_hdr *hdr = (void *)skb->data;
1726*4882a593Smuzhiyun 	unsigned int snap_ip_tcp_hdrlen, ip_hdrlen, total_len, hdr_room;
1727*4882a593Smuzhiyun 	unsigned int mss = skb_shinfo(skb)->gso_size;
1728*4882a593Smuzhiyun 	u16 length, iv_len, amsdu_pad;
1729*4882a593Smuzhiyun 	u8 *start_hdr;
1730*4882a593Smuzhiyun 	struct iwl_tso_hdr_page *hdr_page;
1731*4882a593Smuzhiyun 	struct tso_t tso;
1732*4882a593Smuzhiyun 
1733*4882a593Smuzhiyun 	/* if the packet is protected, then it must be CCMP or GCMP */
1734*4882a593Smuzhiyun 	BUILD_BUG_ON(IEEE80211_CCMP_HDR_LEN != IEEE80211_GCMP_HDR_LEN);
1735*4882a593Smuzhiyun 	iv_len = ieee80211_has_protected(hdr->frame_control) ?
1736*4882a593Smuzhiyun 		IEEE80211_CCMP_HDR_LEN : 0;
1737*4882a593Smuzhiyun 
1738*4882a593Smuzhiyun 	trace_iwlwifi_dev_tx(trans->dev, skb,
1739*4882a593Smuzhiyun 			     iwl_txq_get_tfd(trans, txq, txq->write_ptr),
1740*4882a593Smuzhiyun 			     trans->txqs.tfd.size,
1741*4882a593Smuzhiyun 			     &dev_cmd->hdr, IWL_FIRST_TB_SIZE + tb1_len, 0);
1742*4882a593Smuzhiyun 
1743*4882a593Smuzhiyun 	ip_hdrlen = skb_transport_header(skb) - skb_network_header(skb);
1744*4882a593Smuzhiyun 	snap_ip_tcp_hdrlen = 8 + ip_hdrlen + tcp_hdrlen(skb);
1745*4882a593Smuzhiyun 	total_len = skb->len - snap_ip_tcp_hdrlen - hdr_len - iv_len;
1746*4882a593Smuzhiyun 	amsdu_pad = 0;
1747*4882a593Smuzhiyun 
1748*4882a593Smuzhiyun 	/* total amount of header we may need for this A-MSDU */
1749*4882a593Smuzhiyun 	hdr_room = DIV_ROUND_UP(total_len, mss) *
1750*4882a593Smuzhiyun 		(3 + snap_ip_tcp_hdrlen + sizeof(struct ethhdr)) + iv_len;
1751*4882a593Smuzhiyun 
1752*4882a593Smuzhiyun 	/* Our device supports 9 segments at most, it will fit in 1 page */
1753*4882a593Smuzhiyun 	hdr_page = get_page_hdr(trans, hdr_room, skb);
1754*4882a593Smuzhiyun 	if (!hdr_page)
1755*4882a593Smuzhiyun 		return -ENOMEM;
1756*4882a593Smuzhiyun 
1757*4882a593Smuzhiyun 	start_hdr = hdr_page->pos;
1758*4882a593Smuzhiyun 	memcpy(hdr_page->pos, skb->data + hdr_len, iv_len);
1759*4882a593Smuzhiyun 	hdr_page->pos += iv_len;
1760*4882a593Smuzhiyun 
1761*4882a593Smuzhiyun 	/*
1762*4882a593Smuzhiyun 	 * Pull the ieee80211 header + IV to be able to use TSO core,
1763*4882a593Smuzhiyun 	 * we will restore it for the tx_status flow.
1764*4882a593Smuzhiyun 	 */
1765*4882a593Smuzhiyun 	skb_pull(skb, hdr_len + iv_len);
1766*4882a593Smuzhiyun 
1767*4882a593Smuzhiyun 	/*
1768*4882a593Smuzhiyun 	 * Remove the length of all the headers that we don't actually
1769*4882a593Smuzhiyun 	 * have in the MPDU by themselves, but that we duplicate into
1770*4882a593Smuzhiyun 	 * all the different MSDUs inside the A-MSDU.
1771*4882a593Smuzhiyun 	 */
1772*4882a593Smuzhiyun 	le16_add_cpu(&tx_cmd->len, -snap_ip_tcp_hdrlen);
1773*4882a593Smuzhiyun 
1774*4882a593Smuzhiyun 	tso_start(skb, &tso);
1775*4882a593Smuzhiyun 
1776*4882a593Smuzhiyun 	while (total_len) {
1777*4882a593Smuzhiyun 		/* this is the data left for this subframe */
1778*4882a593Smuzhiyun 		unsigned int data_left =
1779*4882a593Smuzhiyun 			min_t(unsigned int, mss, total_len);
1780*4882a593Smuzhiyun 		struct sk_buff *csum_skb = NULL;
1781*4882a593Smuzhiyun 		unsigned int hdr_tb_len;
1782*4882a593Smuzhiyun 		dma_addr_t hdr_tb_phys;
1783*4882a593Smuzhiyun 		struct tcphdr *tcph;
1784*4882a593Smuzhiyun 		u8 *iph, *subf_hdrs_start = hdr_page->pos;
1785*4882a593Smuzhiyun 
1786*4882a593Smuzhiyun 		total_len -= data_left;
1787*4882a593Smuzhiyun 
1788*4882a593Smuzhiyun 		memset(hdr_page->pos, 0, amsdu_pad);
1789*4882a593Smuzhiyun 		hdr_page->pos += amsdu_pad;
1790*4882a593Smuzhiyun 		amsdu_pad = (4 - (sizeof(struct ethhdr) + snap_ip_tcp_hdrlen +
1791*4882a593Smuzhiyun 				  data_left)) & 0x3;
1792*4882a593Smuzhiyun 		ether_addr_copy(hdr_page->pos, ieee80211_get_DA(hdr));
1793*4882a593Smuzhiyun 		hdr_page->pos += ETH_ALEN;
1794*4882a593Smuzhiyun 		ether_addr_copy(hdr_page->pos, ieee80211_get_SA(hdr));
1795*4882a593Smuzhiyun 		hdr_page->pos += ETH_ALEN;
1796*4882a593Smuzhiyun 
1797*4882a593Smuzhiyun 		length = snap_ip_tcp_hdrlen + data_left;
1798*4882a593Smuzhiyun 		*((__be16 *)hdr_page->pos) = cpu_to_be16(length);
1799*4882a593Smuzhiyun 		hdr_page->pos += sizeof(length);
1800*4882a593Smuzhiyun 
1801*4882a593Smuzhiyun 		/*
1802*4882a593Smuzhiyun 		 * This will copy the SNAP as well which will be considered
1803*4882a593Smuzhiyun 		 * as MAC header.
1804*4882a593Smuzhiyun 		 */
1805*4882a593Smuzhiyun 		tso_build_hdr(skb, hdr_page->pos, &tso, data_left, !total_len);
1806*4882a593Smuzhiyun 		iph = hdr_page->pos + 8;
1807*4882a593Smuzhiyun 		tcph = (void *)(iph + ip_hdrlen);
1808*4882a593Smuzhiyun 
1809*4882a593Smuzhiyun 		/* For testing on current hardware only */
1810*4882a593Smuzhiyun 		if (trans_pcie->sw_csum_tx) {
1811*4882a593Smuzhiyun 			csum_skb = alloc_skb(data_left + tcp_hdrlen(skb),
1812*4882a593Smuzhiyun 					     GFP_ATOMIC);
1813*4882a593Smuzhiyun 			if (!csum_skb)
1814*4882a593Smuzhiyun 				return -ENOMEM;
1815*4882a593Smuzhiyun 
1816*4882a593Smuzhiyun 			iwl_compute_pseudo_hdr_csum(iph, tcph,
1817*4882a593Smuzhiyun 						    skb->protocol ==
1818*4882a593Smuzhiyun 							htons(ETH_P_IPV6),
1819*4882a593Smuzhiyun 						    data_left);
1820*4882a593Smuzhiyun 
1821*4882a593Smuzhiyun 			skb_put_data(csum_skb, tcph, tcp_hdrlen(skb));
1822*4882a593Smuzhiyun 			skb_reset_transport_header(csum_skb);
1823*4882a593Smuzhiyun 			csum_skb->csum_start =
1824*4882a593Smuzhiyun 				(unsigned char *)tcp_hdr(csum_skb) -
1825*4882a593Smuzhiyun 						 csum_skb->head;
1826*4882a593Smuzhiyun 		}
1827*4882a593Smuzhiyun 
1828*4882a593Smuzhiyun 		hdr_page->pos += snap_ip_tcp_hdrlen;
1829*4882a593Smuzhiyun 
1830*4882a593Smuzhiyun 		hdr_tb_len = hdr_page->pos - start_hdr;
1831*4882a593Smuzhiyun 		hdr_tb_phys = dma_map_single(trans->dev, start_hdr,
1832*4882a593Smuzhiyun 					     hdr_tb_len, DMA_TO_DEVICE);
1833*4882a593Smuzhiyun 		if (unlikely(dma_mapping_error(trans->dev, hdr_tb_phys))) {
1834*4882a593Smuzhiyun 			dev_kfree_skb(csum_skb);
1835*4882a593Smuzhiyun 			return -EINVAL;
1836*4882a593Smuzhiyun 		}
1837*4882a593Smuzhiyun 		iwl_pcie_txq_build_tfd(trans, txq, hdr_tb_phys,
1838*4882a593Smuzhiyun 				       hdr_tb_len, false);
1839*4882a593Smuzhiyun 		trace_iwlwifi_dev_tx_tb(trans->dev, skb, start_hdr,
1840*4882a593Smuzhiyun 					hdr_tb_phys, hdr_tb_len);
1841*4882a593Smuzhiyun 		/* add this subframe's headers' length to the tx_cmd */
1842*4882a593Smuzhiyun 		le16_add_cpu(&tx_cmd->len, hdr_page->pos - subf_hdrs_start);
1843*4882a593Smuzhiyun 
1844*4882a593Smuzhiyun 		/* prepare the start_hdr for the next subframe */
1845*4882a593Smuzhiyun 		start_hdr = hdr_page->pos;
1846*4882a593Smuzhiyun 
1847*4882a593Smuzhiyun 		/* put the payload */
1848*4882a593Smuzhiyun 		while (data_left) {
1849*4882a593Smuzhiyun 			unsigned int size = min_t(unsigned int, tso.size,
1850*4882a593Smuzhiyun 						  data_left);
1851*4882a593Smuzhiyun 			dma_addr_t tb_phys;
1852*4882a593Smuzhiyun 
1853*4882a593Smuzhiyun 			if (trans_pcie->sw_csum_tx)
1854*4882a593Smuzhiyun 				skb_put_data(csum_skb, tso.data, size);
1855*4882a593Smuzhiyun 
1856*4882a593Smuzhiyun 			tb_phys = dma_map_single(trans->dev, tso.data,
1857*4882a593Smuzhiyun 						 size, DMA_TO_DEVICE);
1858*4882a593Smuzhiyun 			if (unlikely(dma_mapping_error(trans->dev, tb_phys))) {
1859*4882a593Smuzhiyun 				dev_kfree_skb(csum_skb);
1860*4882a593Smuzhiyun 				return -EINVAL;
1861*4882a593Smuzhiyun 			}
1862*4882a593Smuzhiyun 
1863*4882a593Smuzhiyun 			iwl_pcie_txq_build_tfd(trans, txq, tb_phys,
1864*4882a593Smuzhiyun 					       size, false);
1865*4882a593Smuzhiyun 			trace_iwlwifi_dev_tx_tb(trans->dev, skb, tso.data,
1866*4882a593Smuzhiyun 						tb_phys, size);
1867*4882a593Smuzhiyun 
1868*4882a593Smuzhiyun 			data_left -= size;
1869*4882a593Smuzhiyun 			tso_build_data(skb, &tso, size);
1870*4882a593Smuzhiyun 		}
1871*4882a593Smuzhiyun 
1872*4882a593Smuzhiyun 		/* For testing on early hardware only */
1873*4882a593Smuzhiyun 		if (trans_pcie->sw_csum_tx) {
1874*4882a593Smuzhiyun 			__wsum csum;
1875*4882a593Smuzhiyun 
1876*4882a593Smuzhiyun 			csum = skb_checksum(csum_skb,
1877*4882a593Smuzhiyun 					    skb_checksum_start_offset(csum_skb),
1878*4882a593Smuzhiyun 					    csum_skb->len -
1879*4882a593Smuzhiyun 					    skb_checksum_start_offset(csum_skb),
1880*4882a593Smuzhiyun 					    0);
1881*4882a593Smuzhiyun 			dev_kfree_skb(csum_skb);
1882*4882a593Smuzhiyun 			dma_sync_single_for_cpu(trans->dev, hdr_tb_phys,
1883*4882a593Smuzhiyun 						hdr_tb_len, DMA_TO_DEVICE);
1884*4882a593Smuzhiyun 			tcph->check = csum_fold(csum);
1885*4882a593Smuzhiyun 			dma_sync_single_for_device(trans->dev, hdr_tb_phys,
1886*4882a593Smuzhiyun 						   hdr_tb_len, DMA_TO_DEVICE);
1887*4882a593Smuzhiyun 		}
1888*4882a593Smuzhiyun 	}
1889*4882a593Smuzhiyun 
1890*4882a593Smuzhiyun 	/* re -add the WiFi header and IV */
1891*4882a593Smuzhiyun 	skb_push(skb, hdr_len + iv_len);
1892*4882a593Smuzhiyun 
1893*4882a593Smuzhiyun 	return 0;
1894*4882a593Smuzhiyun }
1895*4882a593Smuzhiyun #else /* CONFIG_INET */
iwl_fill_data_tbs_amsdu(struct iwl_trans * trans,struct sk_buff * skb,struct iwl_txq * txq,u8 hdr_len,struct iwl_cmd_meta * out_meta,struct iwl_device_tx_cmd * dev_cmd,u16 tb1_len)1896*4882a593Smuzhiyun static int iwl_fill_data_tbs_amsdu(struct iwl_trans *trans, struct sk_buff *skb,
1897*4882a593Smuzhiyun 				   struct iwl_txq *txq, u8 hdr_len,
1898*4882a593Smuzhiyun 				   struct iwl_cmd_meta *out_meta,
1899*4882a593Smuzhiyun 				   struct iwl_device_tx_cmd *dev_cmd,
1900*4882a593Smuzhiyun 				   u16 tb1_len)
1901*4882a593Smuzhiyun {
1902*4882a593Smuzhiyun 	/* No A-MSDU without CONFIG_INET */
1903*4882a593Smuzhiyun 	WARN_ON(1);
1904*4882a593Smuzhiyun 
1905*4882a593Smuzhiyun 	return -1;
1906*4882a593Smuzhiyun }
1907*4882a593Smuzhiyun #endif /* CONFIG_INET */
1908*4882a593Smuzhiyun 
iwl_trans_pcie_tx(struct iwl_trans * trans,struct sk_buff * skb,struct iwl_device_tx_cmd * dev_cmd,int txq_id)1909*4882a593Smuzhiyun int iwl_trans_pcie_tx(struct iwl_trans *trans, struct sk_buff *skb,
1910*4882a593Smuzhiyun 		      struct iwl_device_tx_cmd *dev_cmd, int txq_id)
1911*4882a593Smuzhiyun {
1912*4882a593Smuzhiyun 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1913*4882a593Smuzhiyun 	struct ieee80211_hdr *hdr;
1914*4882a593Smuzhiyun 	struct iwl_tx_cmd *tx_cmd = (struct iwl_tx_cmd *)dev_cmd->payload;
1915*4882a593Smuzhiyun 	struct iwl_cmd_meta *out_meta;
1916*4882a593Smuzhiyun 	struct iwl_txq *txq;
1917*4882a593Smuzhiyun 	dma_addr_t tb0_phys, tb1_phys, scratch_phys;
1918*4882a593Smuzhiyun 	void *tb1_addr;
1919*4882a593Smuzhiyun 	void *tfd;
1920*4882a593Smuzhiyun 	u16 len, tb1_len;
1921*4882a593Smuzhiyun 	bool wait_write_ptr;
1922*4882a593Smuzhiyun 	__le16 fc;
1923*4882a593Smuzhiyun 	u8 hdr_len;
1924*4882a593Smuzhiyun 	u16 wifi_seq;
1925*4882a593Smuzhiyun 	bool amsdu;
1926*4882a593Smuzhiyun 
1927*4882a593Smuzhiyun 	txq = trans->txqs.txq[txq_id];
1928*4882a593Smuzhiyun 
1929*4882a593Smuzhiyun 	if (WARN_ONCE(!test_bit(txq_id, trans->txqs.queue_used),
1930*4882a593Smuzhiyun 		      "TX on unused queue %d\n", txq_id))
1931*4882a593Smuzhiyun 		return -EINVAL;
1932*4882a593Smuzhiyun 
1933*4882a593Smuzhiyun 	if (unlikely(trans_pcie->sw_csum_tx &&
1934*4882a593Smuzhiyun 		     skb->ip_summed == CHECKSUM_PARTIAL)) {
1935*4882a593Smuzhiyun 		int offs = skb_checksum_start_offset(skb);
1936*4882a593Smuzhiyun 		int csum_offs = offs + skb->csum_offset;
1937*4882a593Smuzhiyun 		__wsum csum;
1938*4882a593Smuzhiyun 
1939*4882a593Smuzhiyun 		if (skb_ensure_writable(skb, csum_offs + sizeof(__sum16)))
1940*4882a593Smuzhiyun 			return -1;
1941*4882a593Smuzhiyun 
1942*4882a593Smuzhiyun 		csum = skb_checksum(skb, offs, skb->len - offs, 0);
1943*4882a593Smuzhiyun 		*(__sum16 *)(skb->data + csum_offs) = csum_fold(csum);
1944*4882a593Smuzhiyun 
1945*4882a593Smuzhiyun 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1946*4882a593Smuzhiyun 	}
1947*4882a593Smuzhiyun 
1948*4882a593Smuzhiyun 	if (skb_is_nonlinear(skb) &&
1949*4882a593Smuzhiyun 	    skb_shinfo(skb)->nr_frags > IWL_TRANS_MAX_FRAGS(trans) &&
1950*4882a593Smuzhiyun 	    __skb_linearize(skb))
1951*4882a593Smuzhiyun 		return -ENOMEM;
1952*4882a593Smuzhiyun 
1953*4882a593Smuzhiyun 	/* mac80211 always puts the full header into the SKB's head,
1954*4882a593Smuzhiyun 	 * so there's no need to check if it's readable there
1955*4882a593Smuzhiyun 	 */
1956*4882a593Smuzhiyun 	hdr = (struct ieee80211_hdr *)skb->data;
1957*4882a593Smuzhiyun 	fc = hdr->frame_control;
1958*4882a593Smuzhiyun 	hdr_len = ieee80211_hdrlen(fc);
1959*4882a593Smuzhiyun 
1960*4882a593Smuzhiyun 	spin_lock(&txq->lock);
1961*4882a593Smuzhiyun 
1962*4882a593Smuzhiyun 	if (iwl_txq_space(trans, txq) < txq->high_mark) {
1963*4882a593Smuzhiyun 		iwl_txq_stop(trans, txq);
1964*4882a593Smuzhiyun 
1965*4882a593Smuzhiyun 		/* don't put the packet on the ring, if there is no room */
1966*4882a593Smuzhiyun 		if (unlikely(iwl_txq_space(trans, txq) < 3)) {
1967*4882a593Smuzhiyun 			struct iwl_device_tx_cmd **dev_cmd_ptr;
1968*4882a593Smuzhiyun 
1969*4882a593Smuzhiyun 			dev_cmd_ptr = (void *)((u8 *)skb->cb +
1970*4882a593Smuzhiyun 					       trans->txqs.dev_cmd_offs);
1971*4882a593Smuzhiyun 
1972*4882a593Smuzhiyun 			*dev_cmd_ptr = dev_cmd;
1973*4882a593Smuzhiyun 			__skb_queue_tail(&txq->overflow_q, skb);
1974*4882a593Smuzhiyun 
1975*4882a593Smuzhiyun 			spin_unlock(&txq->lock);
1976*4882a593Smuzhiyun 			return 0;
1977*4882a593Smuzhiyun 		}
1978*4882a593Smuzhiyun 	}
1979*4882a593Smuzhiyun 
1980*4882a593Smuzhiyun 	/* In AGG mode, the index in the ring must correspond to the WiFi
1981*4882a593Smuzhiyun 	 * sequence number. This is a HW requirements to help the SCD to parse
1982*4882a593Smuzhiyun 	 * the BA.
1983*4882a593Smuzhiyun 	 * Check here that the packets are in the right place on the ring.
1984*4882a593Smuzhiyun 	 */
1985*4882a593Smuzhiyun 	wifi_seq = IEEE80211_SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
1986*4882a593Smuzhiyun 	WARN_ONCE(txq->ampdu &&
1987*4882a593Smuzhiyun 		  (wifi_seq & 0xff) != txq->write_ptr,
1988*4882a593Smuzhiyun 		  "Q: %d WiFi Seq %d tfdNum %d",
1989*4882a593Smuzhiyun 		  txq_id, wifi_seq, txq->write_ptr);
1990*4882a593Smuzhiyun 
1991*4882a593Smuzhiyun 	/* Set up driver data for this TFD */
1992*4882a593Smuzhiyun 	txq->entries[txq->write_ptr].skb = skb;
1993*4882a593Smuzhiyun 	txq->entries[txq->write_ptr].cmd = dev_cmd;
1994*4882a593Smuzhiyun 
1995*4882a593Smuzhiyun 	dev_cmd->hdr.sequence =
1996*4882a593Smuzhiyun 		cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
1997*4882a593Smuzhiyun 			    INDEX_TO_SEQ(txq->write_ptr)));
1998*4882a593Smuzhiyun 
1999*4882a593Smuzhiyun 	tb0_phys = iwl_txq_get_first_tb_dma(txq, txq->write_ptr);
2000*4882a593Smuzhiyun 	scratch_phys = tb0_phys + sizeof(struct iwl_cmd_header) +
2001*4882a593Smuzhiyun 		       offsetof(struct iwl_tx_cmd, scratch);
2002*4882a593Smuzhiyun 
2003*4882a593Smuzhiyun 	tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
2004*4882a593Smuzhiyun 	tx_cmd->dram_msb_ptr = iwl_get_dma_hi_addr(scratch_phys);
2005*4882a593Smuzhiyun 
2006*4882a593Smuzhiyun 	/* Set up first empty entry in queue's array of Tx/cmd buffers */
2007*4882a593Smuzhiyun 	out_meta = &txq->entries[txq->write_ptr].meta;
2008*4882a593Smuzhiyun 	out_meta->flags = 0;
2009*4882a593Smuzhiyun 
2010*4882a593Smuzhiyun 	/*
2011*4882a593Smuzhiyun 	 * The second TB (tb1) points to the remainder of the TX command
2012*4882a593Smuzhiyun 	 * and the 802.11 header - dword aligned size
2013*4882a593Smuzhiyun 	 * (This calculation modifies the TX command, so do it before the
2014*4882a593Smuzhiyun 	 * setup of the first TB)
2015*4882a593Smuzhiyun 	 */
2016*4882a593Smuzhiyun 	len = sizeof(struct iwl_tx_cmd) + sizeof(struct iwl_cmd_header) +
2017*4882a593Smuzhiyun 	      hdr_len - IWL_FIRST_TB_SIZE;
2018*4882a593Smuzhiyun 	/* do not align A-MSDU to dword as the subframe header aligns it */
2019*4882a593Smuzhiyun 	amsdu = ieee80211_is_data_qos(fc) &&
2020*4882a593Smuzhiyun 		(*ieee80211_get_qos_ctl(hdr) &
2021*4882a593Smuzhiyun 		 IEEE80211_QOS_CTL_A_MSDU_PRESENT);
2022*4882a593Smuzhiyun 	if (trans_pcie->sw_csum_tx || !amsdu) {
2023*4882a593Smuzhiyun 		tb1_len = ALIGN(len, 4);
2024*4882a593Smuzhiyun 		/* Tell NIC about any 2-byte padding after MAC header */
2025*4882a593Smuzhiyun 		if (tb1_len != len)
2026*4882a593Smuzhiyun 			tx_cmd->tx_flags |= cpu_to_le32(TX_CMD_FLG_MH_PAD);
2027*4882a593Smuzhiyun 	} else {
2028*4882a593Smuzhiyun 		tb1_len = len;
2029*4882a593Smuzhiyun 	}
2030*4882a593Smuzhiyun 
2031*4882a593Smuzhiyun 	/*
2032*4882a593Smuzhiyun 	 * The first TB points to bi-directional DMA data, we'll
2033*4882a593Smuzhiyun 	 * memcpy the data into it later.
2034*4882a593Smuzhiyun 	 */
2035*4882a593Smuzhiyun 	iwl_pcie_txq_build_tfd(trans, txq, tb0_phys,
2036*4882a593Smuzhiyun 			       IWL_FIRST_TB_SIZE, true);
2037*4882a593Smuzhiyun 
2038*4882a593Smuzhiyun 	/* there must be data left over for TB1 or this code must be changed */
2039*4882a593Smuzhiyun 	BUILD_BUG_ON(sizeof(struct iwl_tx_cmd) < IWL_FIRST_TB_SIZE);
2040*4882a593Smuzhiyun 
2041*4882a593Smuzhiyun 	/* map the data for TB1 */
2042*4882a593Smuzhiyun 	tb1_addr = ((u8 *)&dev_cmd->hdr) + IWL_FIRST_TB_SIZE;
2043*4882a593Smuzhiyun 	tb1_phys = dma_map_single(trans->dev, tb1_addr, tb1_len, DMA_TO_DEVICE);
2044*4882a593Smuzhiyun 	if (unlikely(dma_mapping_error(trans->dev, tb1_phys)))
2045*4882a593Smuzhiyun 		goto out_err;
2046*4882a593Smuzhiyun 	iwl_pcie_txq_build_tfd(trans, txq, tb1_phys, tb1_len, false);
2047*4882a593Smuzhiyun 
2048*4882a593Smuzhiyun 	trace_iwlwifi_dev_tx(trans->dev, skb,
2049*4882a593Smuzhiyun 			     iwl_txq_get_tfd(trans, txq, txq->write_ptr),
2050*4882a593Smuzhiyun 			     trans->txqs.tfd.size,
2051*4882a593Smuzhiyun 			     &dev_cmd->hdr, IWL_FIRST_TB_SIZE + tb1_len,
2052*4882a593Smuzhiyun 			     hdr_len);
2053*4882a593Smuzhiyun 
2054*4882a593Smuzhiyun 	/*
2055*4882a593Smuzhiyun 	 * If gso_size wasn't set, don't give the frame "amsdu treatment"
2056*4882a593Smuzhiyun 	 * (adding subframes, etc.).
2057*4882a593Smuzhiyun 	 * This can happen in some testing flows when the amsdu was already
2058*4882a593Smuzhiyun 	 * pre-built, and we just need to send the resulting skb.
2059*4882a593Smuzhiyun 	 */
2060*4882a593Smuzhiyun 	if (amsdu && skb_shinfo(skb)->gso_size) {
2061*4882a593Smuzhiyun 		if (unlikely(iwl_fill_data_tbs_amsdu(trans, skb, txq, hdr_len,
2062*4882a593Smuzhiyun 						     out_meta, dev_cmd,
2063*4882a593Smuzhiyun 						     tb1_len)))
2064*4882a593Smuzhiyun 			goto out_err;
2065*4882a593Smuzhiyun 	} else {
2066*4882a593Smuzhiyun 		struct sk_buff *frag;
2067*4882a593Smuzhiyun 
2068*4882a593Smuzhiyun 		if (unlikely(iwl_fill_data_tbs(trans, skb, txq, hdr_len,
2069*4882a593Smuzhiyun 					       out_meta)))
2070*4882a593Smuzhiyun 			goto out_err;
2071*4882a593Smuzhiyun 
2072*4882a593Smuzhiyun 		skb_walk_frags(skb, frag) {
2073*4882a593Smuzhiyun 			if (unlikely(iwl_fill_data_tbs(trans, frag, txq, 0,
2074*4882a593Smuzhiyun 						       out_meta)))
2075*4882a593Smuzhiyun 				goto out_err;
2076*4882a593Smuzhiyun 		}
2077*4882a593Smuzhiyun 	}
2078*4882a593Smuzhiyun 
2079*4882a593Smuzhiyun 	/* building the A-MSDU might have changed this data, so memcpy it now */
2080*4882a593Smuzhiyun 	memcpy(&txq->first_tb_bufs[txq->write_ptr], dev_cmd, IWL_FIRST_TB_SIZE);
2081*4882a593Smuzhiyun 
2082*4882a593Smuzhiyun 	tfd = iwl_txq_get_tfd(trans, txq, txq->write_ptr);
2083*4882a593Smuzhiyun 	/* Set up entry for this TFD in Tx byte-count array */
2084*4882a593Smuzhiyun 	iwl_txq_gen1_update_byte_cnt_tbl(trans, txq, le16_to_cpu(tx_cmd->len),
2085*4882a593Smuzhiyun 					 iwl_txq_gen1_tfd_get_num_tbs(trans,
2086*4882a593Smuzhiyun 								      tfd));
2087*4882a593Smuzhiyun 
2088*4882a593Smuzhiyun 	wait_write_ptr = ieee80211_has_morefrags(fc);
2089*4882a593Smuzhiyun 
2090*4882a593Smuzhiyun 	/* start timer if queue currently empty */
2091*4882a593Smuzhiyun 	if (txq->read_ptr == txq->write_ptr && txq->wd_timeout) {
2092*4882a593Smuzhiyun 		/*
2093*4882a593Smuzhiyun 		 * If the TXQ is active, then set the timer, if not,
2094*4882a593Smuzhiyun 		 * set the timer in remainder so that the timer will
2095*4882a593Smuzhiyun 		 * be armed with the right value when the station will
2096*4882a593Smuzhiyun 		 * wake up.
2097*4882a593Smuzhiyun 		 */
2098*4882a593Smuzhiyun 		if (!txq->frozen)
2099*4882a593Smuzhiyun 			mod_timer(&txq->stuck_timer,
2100*4882a593Smuzhiyun 				  jiffies + txq->wd_timeout);
2101*4882a593Smuzhiyun 		else
2102*4882a593Smuzhiyun 			txq->frozen_expiry_remainder = txq->wd_timeout;
2103*4882a593Smuzhiyun 	}
2104*4882a593Smuzhiyun 
2105*4882a593Smuzhiyun 	/* Tell device the write index *just past* this latest filled TFD */
2106*4882a593Smuzhiyun 	txq->write_ptr = iwl_txq_inc_wrap(trans, txq->write_ptr);
2107*4882a593Smuzhiyun 	if (!wait_write_ptr)
2108*4882a593Smuzhiyun 		iwl_pcie_txq_inc_wr_ptr(trans, txq);
2109*4882a593Smuzhiyun 
2110*4882a593Smuzhiyun 	/*
2111*4882a593Smuzhiyun 	 * At this point the frame is "transmitted" successfully
2112*4882a593Smuzhiyun 	 * and we will get a TX status notification eventually.
2113*4882a593Smuzhiyun 	 */
2114*4882a593Smuzhiyun 	spin_unlock(&txq->lock);
2115*4882a593Smuzhiyun 	return 0;
2116*4882a593Smuzhiyun out_err:
2117*4882a593Smuzhiyun 	iwl_txq_gen1_tfd_unmap(trans, out_meta, txq, txq->write_ptr);
2118*4882a593Smuzhiyun 	spin_unlock(&txq->lock);
2119*4882a593Smuzhiyun 	return -1;
2120*4882a593Smuzhiyun }
2121