1e705c121SKalle Valo /******************************************************************************
2e705c121SKalle Valo  *
3e705c121SKalle Valo  * Copyright(c) 2003 - 2014 Intel Corporation. All rights reserved.
44cbb8e50SLuciano Coelho  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
5eda50cdeSSara Sharon  * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
6e705c121SKalle Valo  *
7e705c121SKalle Valo  * Portions of this file are derived from the ipw3945 project, as well
8e705c121SKalle Valo  * as portions of the ieee80211 subsystem header files.
9e705c121SKalle Valo  *
10e705c121SKalle Valo  * This program is free software; you can redistribute it and/or modify it
11e705c121SKalle Valo  * under the terms of version 2 of the GNU General Public License as
12e705c121SKalle Valo  * published by the Free Software Foundation.
13e705c121SKalle Valo  *
14e705c121SKalle Valo  * This program is distributed in the hope that it will be useful, but WITHOUT
15e705c121SKalle Valo  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16e705c121SKalle Valo  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
17e705c121SKalle Valo  * more details.
18e705c121SKalle Valo  *
19e705c121SKalle Valo  * You should have received a copy of the GNU General Public License along with
20e705c121SKalle Valo  * this program; if not, write to the Free Software Foundation, Inc.,
21e705c121SKalle Valo  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
22e705c121SKalle Valo  *
23e705c121SKalle Valo  * The full GNU General Public License is included in this distribution in the
24e705c121SKalle Valo  * file called LICENSE.
25e705c121SKalle Valo  *
26e705c121SKalle Valo  * Contact Information:
27cb2f8277SEmmanuel Grumbach  *  Intel Linux Wireless <linuxwifi@intel.com>
28e705c121SKalle Valo  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
29e705c121SKalle Valo  *
30e705c121SKalle Valo  *****************************************************************************/
31e705c121SKalle Valo #include <linux/etherdevice.h>
326eb5e529SEmmanuel Grumbach #include <linux/ieee80211.h>
33e705c121SKalle Valo #include <linux/slab.h>
34e705c121SKalle Valo #include <linux/sched.h>
3571b1230cSLuca Coelho #include <linux/pm_runtime.h>
366eb5e529SEmmanuel Grumbach #include <net/ip6_checksum.h>
376eb5e529SEmmanuel Grumbach #include <net/tso.h>
38e705c121SKalle Valo 
39e705c121SKalle Valo #include "iwl-debug.h"
40e705c121SKalle Valo #include "iwl-csr.h"
41e705c121SKalle Valo #include "iwl-prph.h"
42e705c121SKalle Valo #include "iwl-io.h"
43e705c121SKalle Valo #include "iwl-scd.h"
44e705c121SKalle Valo #include "iwl-op-mode.h"
45e705c121SKalle Valo #include "internal.h"
46e705c121SKalle Valo /* FIXME: need to abstract out TX command (once we know what it looks like) */
47e705c121SKalle Valo #include "dvm/commands.h"
48e705c121SKalle Valo 
49e705c121SKalle Valo #define IWL_TX_CRC_SIZE 4
50e705c121SKalle Valo #define IWL_TX_DELIMITER_SIZE 4
51e705c121SKalle Valo 
52e705c121SKalle Valo /*************** DMA-QUEUE-GENERAL-FUNCTIONS  *****
53e705c121SKalle Valo  * DMA services
54e705c121SKalle Valo  *
55e705c121SKalle Valo  * Theory of operation
56e705c121SKalle Valo  *
57e705c121SKalle Valo  * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
58e705c121SKalle Valo  * of buffer descriptors, each of which points to one or more data buffers for
59e705c121SKalle Valo  * the device to read from or fill.  Driver and device exchange status of each
60e705c121SKalle Valo  * queue via "read" and "write" pointers.  Driver keeps minimum of 2 empty
61e705c121SKalle Valo  * entries in each circular buffer, to protect against confusing empty and full
62e705c121SKalle Valo  * queue states.
63e705c121SKalle Valo  *
64e705c121SKalle Valo  * The device reads or writes the data in the queues via the device's several
65e705c121SKalle Valo  * DMA/FIFO channels.  Each queue is mapped to a single DMA channel.
66e705c121SKalle Valo  *
67e705c121SKalle Valo  * For Tx queue, there are low mark and high mark limits. If, after queuing
68e705c121SKalle Valo  * the packet for Tx, free space become < low mark, Tx queue stopped. When
69e705c121SKalle Valo  * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
70e705c121SKalle Valo  * Tx queue resumed.
71e705c121SKalle Valo  *
72e705c121SKalle Valo  ***************************************************/
73e22744afSSara Sharon 
74ab6c6445SSara Sharon int iwl_queue_space(const struct iwl_txq *q)
75e705c121SKalle Valo {
76e705c121SKalle Valo 	unsigned int max;
77e705c121SKalle Valo 	unsigned int used;
78e705c121SKalle Valo 
79e705c121SKalle Valo 	/*
80e705c121SKalle Valo 	 * To avoid ambiguity between empty and completely full queues, there
81e705c121SKalle Valo 	 * should always be less than TFD_QUEUE_SIZE_MAX elements in the queue.
82e705c121SKalle Valo 	 * If q->n_window is smaller than TFD_QUEUE_SIZE_MAX, there is no need
83e705c121SKalle Valo 	 * to reserve any queue entries for this purpose.
84e705c121SKalle Valo 	 */
85e705c121SKalle Valo 	if (q->n_window < TFD_QUEUE_SIZE_MAX)
86e705c121SKalle Valo 		max = q->n_window;
87e705c121SKalle Valo 	else
88e705c121SKalle Valo 		max = TFD_QUEUE_SIZE_MAX - 1;
89e705c121SKalle Valo 
90e705c121SKalle Valo 	/*
91e705c121SKalle Valo 	 * TFD_QUEUE_SIZE_MAX is a power of 2, so the following is equivalent to
92e705c121SKalle Valo 	 * modulo by TFD_QUEUE_SIZE_MAX and is well defined.
93e705c121SKalle Valo 	 */
94e705c121SKalle Valo 	used = (q->write_ptr - q->read_ptr) & (TFD_QUEUE_SIZE_MAX - 1);
95e705c121SKalle Valo 
96e705c121SKalle Valo 	if (WARN_ON(used > max))
97e705c121SKalle Valo 		return 0;
98e705c121SKalle Valo 
99e705c121SKalle Valo 	return max - used;
100e705c121SKalle Valo }
101e705c121SKalle Valo 
102e705c121SKalle Valo /*
103e705c121SKalle Valo  * iwl_queue_init - Initialize queue's high/low-water and read/write indexes
104e705c121SKalle Valo  */
105b8e8d7ceSSara Sharon static int iwl_queue_init(struct iwl_txq *q, int slots_num)
106e705c121SKalle Valo {
107e705c121SKalle Valo 	q->n_window = slots_num;
108e705c121SKalle Valo 
109e705c121SKalle Valo 	/* slots_num must be power-of-two size, otherwise
110e705c121SKalle Valo 	 * get_cmd_index is broken. */
111e705c121SKalle Valo 	if (WARN_ON(!is_power_of_2(slots_num)))
112e705c121SKalle Valo 		return -EINVAL;
113e705c121SKalle Valo 
114e705c121SKalle Valo 	q->low_mark = q->n_window / 4;
115e705c121SKalle Valo 	if (q->low_mark < 4)
116e705c121SKalle Valo 		q->low_mark = 4;
117e705c121SKalle Valo 
118e705c121SKalle Valo 	q->high_mark = q->n_window / 8;
119e705c121SKalle Valo 	if (q->high_mark < 2)
120e705c121SKalle Valo 		q->high_mark = 2;
121e705c121SKalle Valo 
122e705c121SKalle Valo 	q->write_ptr = 0;
123e705c121SKalle Valo 	q->read_ptr = 0;
124e705c121SKalle Valo 
125e705c121SKalle Valo 	return 0;
126e705c121SKalle Valo }
127e705c121SKalle Valo 
12813a3a390SSara Sharon int iwl_pcie_alloc_dma_ptr(struct iwl_trans *trans,
129e705c121SKalle Valo 			   struct iwl_dma_ptr *ptr, size_t size)
130e705c121SKalle Valo {
131e705c121SKalle Valo 	if (WARN_ON(ptr->addr))
132e705c121SKalle Valo 		return -EINVAL;
133e705c121SKalle Valo 
134e705c121SKalle Valo 	ptr->addr = dma_alloc_coherent(trans->dev, size,
135e705c121SKalle Valo 				       &ptr->dma, GFP_KERNEL);
136e705c121SKalle Valo 	if (!ptr->addr)
137e705c121SKalle Valo 		return -ENOMEM;
138e705c121SKalle Valo 	ptr->size = size;
139e705c121SKalle Valo 	return 0;
140e705c121SKalle Valo }
141e705c121SKalle Valo 
14213a3a390SSara Sharon void iwl_pcie_free_dma_ptr(struct iwl_trans *trans, struct iwl_dma_ptr *ptr)
143e705c121SKalle Valo {
144e705c121SKalle Valo 	if (unlikely(!ptr->addr))
145e705c121SKalle Valo 		return;
146e705c121SKalle Valo 
147e705c121SKalle Valo 	dma_free_coherent(trans->dev, ptr->size, ptr->addr, ptr->dma);
148e705c121SKalle Valo 	memset(ptr, 0, sizeof(*ptr));
149e705c121SKalle Valo }
150e705c121SKalle Valo 
151e705c121SKalle Valo static void iwl_pcie_txq_stuck_timer(unsigned long data)
152e705c121SKalle Valo {
153e705c121SKalle Valo 	struct iwl_txq *txq = (void *)data;
154e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = txq->trans_pcie;
155e705c121SKalle Valo 	struct iwl_trans *trans = iwl_trans_pcie_get_trans(trans_pcie);
156e705c121SKalle Valo 
157e705c121SKalle Valo 	spin_lock(&txq->lock);
158e705c121SKalle Valo 	/* check if triggered erroneously */
159bb98ecd4SSara Sharon 	if (txq->read_ptr == txq->write_ptr) {
160e705c121SKalle Valo 		spin_unlock(&txq->lock);
161e705c121SKalle Valo 		return;
162e705c121SKalle Valo 	}
163e705c121SKalle Valo 	spin_unlock(&txq->lock);
164e705c121SKalle Valo 
16538398efbSSara Sharon 	iwl_trans_pcie_log_scd_error(trans, txq);
166e705c121SKalle Valo 
167e705c121SKalle Valo 	iwl_force_nmi(trans);
168e705c121SKalle Valo }
169e705c121SKalle Valo 
170e705c121SKalle Valo /*
171e705c121SKalle Valo  * iwl_pcie_txq_update_byte_cnt_tbl - Set up entry in Tx byte-count array
172e705c121SKalle Valo  */
173e705c121SKalle Valo static void iwl_pcie_txq_update_byte_cnt_tbl(struct iwl_trans *trans,
1744fe10bc6SSara Sharon 					     struct iwl_txq *txq, u16 byte_cnt,
1754fe10bc6SSara Sharon 					     int num_tbs)
176e705c121SKalle Valo {
177e705c121SKalle Valo 	struct iwlagn_scd_bc_tbl *scd_bc_tbl;
178e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
179bb98ecd4SSara Sharon 	int write_ptr = txq->write_ptr;
180bb98ecd4SSara Sharon 	int txq_id = txq->id;
181e705c121SKalle Valo 	u8 sec_ctl = 0;
182e705c121SKalle Valo 	u16 len = byte_cnt + IWL_TX_CRC_SIZE + IWL_TX_DELIMITER_SIZE;
183e705c121SKalle Valo 	__le16 bc_ent;
184e705c121SKalle Valo 	struct iwl_tx_cmd *tx_cmd =
185bb98ecd4SSara Sharon 		(void *)txq->entries[txq->write_ptr].cmd->payload;
186ab6c6445SSara Sharon 	u8 sta_id = tx_cmd->sta_id;
187e705c121SKalle Valo 
188e705c121SKalle Valo 	scd_bc_tbl = trans_pcie->scd_bc_tbls.addr;
189e705c121SKalle Valo 
190e705c121SKalle Valo 	sec_ctl = tx_cmd->sec_ctl;
191e705c121SKalle Valo 
192e705c121SKalle Valo 	switch (sec_ctl & TX_CMD_SEC_MSK) {
193e705c121SKalle Valo 	case TX_CMD_SEC_CCM:
194e705c121SKalle Valo 		len += IEEE80211_CCMP_MIC_LEN;
195e705c121SKalle Valo 		break;
196e705c121SKalle Valo 	case TX_CMD_SEC_TKIP:
197e705c121SKalle Valo 		len += IEEE80211_TKIP_ICV_LEN;
198e705c121SKalle Valo 		break;
199e705c121SKalle Valo 	case TX_CMD_SEC_WEP:
200e705c121SKalle Valo 		len += IEEE80211_WEP_IV_LEN + IEEE80211_WEP_ICV_LEN;
201e705c121SKalle Valo 		break;
202e705c121SKalle Valo 	}
203e705c121SKalle Valo 	if (trans_pcie->bc_table_dword)
204e705c121SKalle Valo 		len = DIV_ROUND_UP(len, 4);
205e705c121SKalle Valo 
206e705c121SKalle Valo 	if (WARN_ON(len > 0xFFF || write_ptr >= TFD_QUEUE_SIZE_MAX))
207e705c121SKalle Valo 		return;
208e705c121SKalle Valo 
209e705c121SKalle Valo 	bc_ent = cpu_to_le16(len | (sta_id << 12));
210e705c121SKalle Valo 
211e705c121SKalle Valo 	scd_bc_tbl[txq_id].tfd_offset[write_ptr] = bc_ent;
212e705c121SKalle Valo 
213e705c121SKalle Valo 	if (write_ptr < TFD_QUEUE_SIZE_BC_DUP)
214e705c121SKalle Valo 		scd_bc_tbl[txq_id].
215e705c121SKalle Valo 			tfd_offset[TFD_QUEUE_SIZE_MAX + write_ptr] = bc_ent;
216e705c121SKalle Valo }
217e705c121SKalle Valo 
218e705c121SKalle Valo static void iwl_pcie_txq_inval_byte_cnt_tbl(struct iwl_trans *trans,
219e705c121SKalle Valo 					    struct iwl_txq *txq)
220e705c121SKalle Valo {
221e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie =
222e705c121SKalle Valo 		IWL_TRANS_GET_PCIE_TRANS(trans);
223e705c121SKalle Valo 	struct iwlagn_scd_bc_tbl *scd_bc_tbl = trans_pcie->scd_bc_tbls.addr;
224bb98ecd4SSara Sharon 	int txq_id = txq->id;
225bb98ecd4SSara Sharon 	int read_ptr = txq->read_ptr;
226e705c121SKalle Valo 	u8 sta_id = 0;
227e705c121SKalle Valo 	__le16 bc_ent;
228e705c121SKalle Valo 	struct iwl_tx_cmd *tx_cmd =
229bb98ecd4SSara Sharon 		(void *)txq->entries[read_ptr].cmd->payload;
230e705c121SKalle Valo 
231e705c121SKalle Valo 	WARN_ON(read_ptr >= TFD_QUEUE_SIZE_MAX);
232e705c121SKalle Valo 
233e705c121SKalle Valo 	if (txq_id != trans_pcie->cmd_queue)
234e705c121SKalle Valo 		sta_id = tx_cmd->sta_id;
235e705c121SKalle Valo 
236e705c121SKalle Valo 	bc_ent = cpu_to_le16(1 | (sta_id << 12));
2374fe10bc6SSara Sharon 
238e705c121SKalle Valo 	scd_bc_tbl[txq_id].tfd_offset[read_ptr] = bc_ent;
239e705c121SKalle Valo 
240e705c121SKalle Valo 	if (read_ptr < TFD_QUEUE_SIZE_BC_DUP)
241e705c121SKalle Valo 		scd_bc_tbl[txq_id].
242e705c121SKalle Valo 			tfd_offset[TFD_QUEUE_SIZE_MAX + read_ptr] = bc_ent;
243e705c121SKalle Valo }
244e705c121SKalle Valo 
245e705c121SKalle Valo /*
246e705c121SKalle Valo  * iwl_pcie_txq_inc_wr_ptr - Send new write index to hardware
247e705c121SKalle Valo  */
248e705c121SKalle Valo static void iwl_pcie_txq_inc_wr_ptr(struct iwl_trans *trans,
249e705c121SKalle Valo 				    struct iwl_txq *txq)
250e705c121SKalle Valo {
251e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
252e705c121SKalle Valo 	u32 reg = 0;
253bb98ecd4SSara Sharon 	int txq_id = txq->id;
254e705c121SKalle Valo 
255e705c121SKalle Valo 	lockdep_assert_held(&txq->lock);
256e705c121SKalle Valo 
257e705c121SKalle Valo 	/*
258e705c121SKalle Valo 	 * explicitly wake up the NIC if:
259e705c121SKalle Valo 	 * 1. shadow registers aren't enabled
260e705c121SKalle Valo 	 * 2. NIC is woken up for CMD regardless of shadow outside this function
261e705c121SKalle Valo 	 * 3. there is a chance that the NIC is asleep
262e705c121SKalle Valo 	 */
263e705c121SKalle Valo 	if (!trans->cfg->base_params->shadow_reg_enable &&
264e705c121SKalle Valo 	    txq_id != trans_pcie->cmd_queue &&
265e705c121SKalle Valo 	    test_bit(STATUS_TPOWER_PMI, &trans->status)) {
266e705c121SKalle Valo 		/*
267e705c121SKalle Valo 		 * wake up nic if it's powered down ...
268e705c121SKalle Valo 		 * uCode will wake up, and interrupt us again, so next
269e705c121SKalle Valo 		 * time we'll skip this part.
270e705c121SKalle Valo 		 */
271e705c121SKalle Valo 		reg = iwl_read32(trans, CSR_UCODE_DRV_GP1);
272e705c121SKalle Valo 
273e705c121SKalle Valo 		if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
274e705c121SKalle Valo 			IWL_DEBUG_INFO(trans, "Tx queue %d requesting wakeup, GP1 = 0x%x\n",
275e705c121SKalle Valo 				       txq_id, reg);
276e705c121SKalle Valo 			iwl_set_bit(trans, CSR_GP_CNTRL,
277e705c121SKalle Valo 				    CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
278e705c121SKalle Valo 			txq->need_update = true;
279e705c121SKalle Valo 			return;
280e705c121SKalle Valo 		}
281e705c121SKalle Valo 	}
282e705c121SKalle Valo 
283e705c121SKalle Valo 	/*
284e705c121SKalle Valo 	 * if not in power-save mode, uCode will never sleep when we're
285e705c121SKalle Valo 	 * trying to tx (during RFKILL, we're not trying to tx).
286e705c121SKalle Valo 	 */
287bb98ecd4SSara Sharon 	IWL_DEBUG_TX(trans, "Q:%d WR: 0x%x\n", txq_id, txq->write_ptr);
2880cd58eaaSEmmanuel Grumbach 	if (!txq->block)
2890cd58eaaSEmmanuel Grumbach 		iwl_write32(trans, HBUS_TARG_WRPTR,
290bb98ecd4SSara Sharon 			    txq->write_ptr | (txq_id << 8));
291e705c121SKalle Valo }
292e705c121SKalle Valo 
293e705c121SKalle Valo void iwl_pcie_txq_check_wrptrs(struct iwl_trans *trans)
294e705c121SKalle Valo {
295e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
296e705c121SKalle Valo 	int i;
297e705c121SKalle Valo 
298e705c121SKalle Valo 	for (i = 0; i < trans->cfg->base_params->num_of_queues; i++) {
299b2a3b1c1SSara Sharon 		struct iwl_txq *txq = trans_pcie->txq[i];
300e705c121SKalle Valo 
301f6eac740SMordechai Goodstein 		if (!test_bit(i, trans_pcie->queue_used))
302f6eac740SMordechai Goodstein 			continue;
303f6eac740SMordechai Goodstein 
304e705c121SKalle Valo 		spin_lock_bh(&txq->lock);
305b2a3b1c1SSara Sharon 		if (txq->need_update) {
306e705c121SKalle Valo 			iwl_pcie_txq_inc_wr_ptr(trans, txq);
307b2a3b1c1SSara Sharon 			txq->need_update = false;
308e705c121SKalle Valo 		}
309e705c121SKalle Valo 		spin_unlock_bh(&txq->lock);
310e705c121SKalle Valo 	}
311e705c121SKalle Valo }
312e705c121SKalle Valo 
3136983ba69SSara Sharon static inline dma_addr_t iwl_pcie_tfd_tb_get_addr(struct iwl_trans *trans,
314cc2f41f8SJohannes Berg 						  void *_tfd, u8 idx)
3156983ba69SSara Sharon {
3166983ba69SSara Sharon 
3176983ba69SSara Sharon 	if (trans->cfg->use_tfh) {
318cc2f41f8SJohannes Berg 		struct iwl_tfh_tfd *tfd = _tfd;
319cc2f41f8SJohannes Berg 		struct iwl_tfh_tb *tb = &tfd->tbs[idx];
3206983ba69SSara Sharon 
3216983ba69SSara Sharon 		return (dma_addr_t)(le64_to_cpu(tb->addr));
322cc2f41f8SJohannes Berg 	} else {
323cc2f41f8SJohannes Berg 		struct iwl_tfd *tfd = _tfd;
324cc2f41f8SJohannes Berg 		struct iwl_tfd_tb *tb = &tfd->tbs[idx];
325cc2f41f8SJohannes Berg 		dma_addr_t addr = get_unaligned_le32(&tb->lo);
326cc2f41f8SJohannes Berg 		dma_addr_t hi_len;
3276983ba69SSara Sharon 
328cc2f41f8SJohannes Berg 		if (sizeof(dma_addr_t) <= sizeof(u32))
329e705c121SKalle Valo 			return addr;
330cc2f41f8SJohannes Berg 
331cc2f41f8SJohannes Berg 		hi_len = le16_to_cpu(tb->hi_n_len) & 0xF;
332cc2f41f8SJohannes Berg 
333cc2f41f8SJohannes Berg 		/*
334cc2f41f8SJohannes Berg 		 * shift by 16 twice to avoid warnings on 32-bit
335cc2f41f8SJohannes Berg 		 * (where this code never runs anyway due to the
336cc2f41f8SJohannes Berg 		 * if statement above)
337cc2f41f8SJohannes Berg 		 */
338cc2f41f8SJohannes Berg 		return addr | ((hi_len << 16) << 16);
339cc2f41f8SJohannes Berg 	}
340e705c121SKalle Valo }
341e705c121SKalle Valo 
3426983ba69SSara Sharon static inline void iwl_pcie_tfd_set_tb(struct iwl_trans *trans, void *tfd,
3436983ba69SSara Sharon 				       u8 idx, dma_addr_t addr, u16 len)
344e705c121SKalle Valo {
3456983ba69SSara Sharon 	struct iwl_tfd *tfd_fh = (void *)tfd;
3466983ba69SSara Sharon 	struct iwl_tfd_tb *tb = &tfd_fh->tbs[idx];
3476983ba69SSara Sharon 
348e705c121SKalle Valo 	u16 hi_n_len = len << 4;
349e705c121SKalle Valo 
350e705c121SKalle Valo 	put_unaligned_le32(addr, &tb->lo);
3517abf6fdeSJohannes Berg 	hi_n_len |= iwl_get_dma_hi_addr(addr);
352e705c121SKalle Valo 
353e705c121SKalle Valo 	tb->hi_n_len = cpu_to_le16(hi_n_len);
354e705c121SKalle Valo 
3556983ba69SSara Sharon 	tfd_fh->num_tbs = idx + 1;
3566983ba69SSara Sharon }
357e705c121SKalle Valo 
358cc2f41f8SJohannes Berg static inline u8 iwl_pcie_tfd_get_num_tbs(struct iwl_trans *trans, void *_tfd)
359e705c121SKalle Valo {
3606983ba69SSara Sharon 	if (trans->cfg->use_tfh) {
361cc2f41f8SJohannes Berg 		struct iwl_tfh_tfd *tfd = _tfd;
3626983ba69SSara Sharon 
363cc2f41f8SJohannes Berg 		return le16_to_cpu(tfd->num_tbs) & 0x1f;
364cc2f41f8SJohannes Berg 	} else {
365cc2f41f8SJohannes Berg 		struct iwl_tfd *tfd = _tfd;
366cc2f41f8SJohannes Berg 
367cc2f41f8SJohannes Berg 		return tfd->num_tbs & 0x1f;
3686983ba69SSara Sharon 	}
369e705c121SKalle Valo }
370e705c121SKalle Valo 
371e705c121SKalle Valo static void iwl_pcie_tfd_unmap(struct iwl_trans *trans,
372e705c121SKalle Valo 			       struct iwl_cmd_meta *meta,
3736983ba69SSara Sharon 			       struct iwl_txq *txq, int index)
374e705c121SKalle Valo {
3753cd1980bSSara Sharon 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
3763cd1980bSSara Sharon 	int i, num_tbs;
3776983ba69SSara Sharon 	void *tfd = iwl_pcie_get_tfd(trans_pcie, txq, index);
378e705c121SKalle Valo 
379e705c121SKalle Valo 	/* Sanity check on number of chunks */
3806983ba69SSara Sharon 	num_tbs = iwl_pcie_tfd_get_num_tbs(trans, tfd);
381e705c121SKalle Valo 
3823cd1980bSSara Sharon 	if (num_tbs >= trans_pcie->max_tbs) {
383e705c121SKalle Valo 		IWL_ERR(trans, "Too many chunks: %i\n", num_tbs);
384e705c121SKalle Valo 		/* @todo issue fatal error, it is quite serious situation */
385e705c121SKalle Valo 		return;
386e705c121SKalle Valo 	}
387e705c121SKalle Valo 
3888de437c7SSara Sharon 	/* first TB is never freed - it's the bidirectional DMA data */
389e705c121SKalle Valo 
390e705c121SKalle Valo 	for (i = 1; i < num_tbs; i++) {
3913cd1980bSSara Sharon 		if (meta->tbs & BIT(i))
392e705c121SKalle Valo 			dma_unmap_page(trans->dev,
3936983ba69SSara Sharon 				       iwl_pcie_tfd_tb_get_addr(trans, tfd, i),
3946983ba69SSara Sharon 				       iwl_pcie_tfd_tb_get_len(trans, tfd, i),
395e705c121SKalle Valo 				       DMA_TO_DEVICE);
396e705c121SKalle Valo 		else
397e705c121SKalle Valo 			dma_unmap_single(trans->dev,
3986983ba69SSara Sharon 					 iwl_pcie_tfd_tb_get_addr(trans, tfd,
3996983ba69SSara Sharon 								  i),
4006983ba69SSara Sharon 					 iwl_pcie_tfd_tb_get_len(trans, tfd,
4016983ba69SSara Sharon 								 i),
402e705c121SKalle Valo 					 DMA_TO_DEVICE);
403e705c121SKalle Valo 	}
4046983ba69SSara Sharon 
4056983ba69SSara Sharon 	if (trans->cfg->use_tfh) {
4066983ba69SSara Sharon 		struct iwl_tfh_tfd *tfd_fh = (void *)tfd;
4076983ba69SSara Sharon 
4086983ba69SSara Sharon 		tfd_fh->num_tbs = 0;
4096983ba69SSara Sharon 	} else {
4106983ba69SSara Sharon 		struct iwl_tfd *tfd_fh = (void *)tfd;
4116983ba69SSara Sharon 
4126983ba69SSara Sharon 		tfd_fh->num_tbs = 0;
4136983ba69SSara Sharon 	}
4146983ba69SSara Sharon 
415e705c121SKalle Valo }
416e705c121SKalle Valo 
417e705c121SKalle Valo /*
418e705c121SKalle Valo  * iwl_pcie_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr]
419e705c121SKalle Valo  * @trans - transport private data
420e705c121SKalle Valo  * @txq - tx queue
421e705c121SKalle Valo  * @dma_dir - the direction of the DMA mapping
422e705c121SKalle Valo  *
423e705c121SKalle Valo  * Does NOT advance any TFD circular buffer read/write indexes
424e705c121SKalle Valo  * Does NOT free the TFD itself (which is within circular buffer)
425e705c121SKalle Valo  */
4266b35ff91SSara Sharon void iwl_pcie_txq_free_tfd(struct iwl_trans *trans, struct iwl_txq *txq)
427e705c121SKalle Valo {
428e705c121SKalle Valo 	/* rd_ptr is bounded by TFD_QUEUE_SIZE_MAX and
429e705c121SKalle Valo 	 * idx is bounded by n_window
430e705c121SKalle Valo 	 */
431bb98ecd4SSara Sharon 	int rd_ptr = txq->read_ptr;
432bb98ecd4SSara Sharon 	int idx = get_cmd_index(txq, rd_ptr);
433e705c121SKalle Valo 
434e705c121SKalle Valo 	lockdep_assert_held(&txq->lock);
435e705c121SKalle Valo 
436e705c121SKalle Valo 	/* We have only q->n_window txq->entries, but we use
437e705c121SKalle Valo 	 * TFD_QUEUE_SIZE_MAX tfds
438e705c121SKalle Valo 	 */
4396983ba69SSara Sharon 	iwl_pcie_tfd_unmap(trans, &txq->entries[idx].meta, txq, rd_ptr);
440e705c121SKalle Valo 
441e705c121SKalle Valo 	/* free SKB */
442e705c121SKalle Valo 	if (txq->entries) {
443e705c121SKalle Valo 		struct sk_buff *skb;
444e705c121SKalle Valo 
445e705c121SKalle Valo 		skb = txq->entries[idx].skb;
446e705c121SKalle Valo 
447e705c121SKalle Valo 		/* Can be called from irqs-disabled context
448e705c121SKalle Valo 		 * If skb is not NULL, it means that the whole queue is being
449e705c121SKalle Valo 		 * freed and that the queue is not empty - free the skb
450e705c121SKalle Valo 		 */
451e705c121SKalle Valo 		if (skb) {
452e705c121SKalle Valo 			iwl_op_mode_free_skb(trans->op_mode, skb);
453e705c121SKalle Valo 			txq->entries[idx].skb = NULL;
454e705c121SKalle Valo 		}
455e705c121SKalle Valo 	}
456e705c121SKalle Valo }
457e705c121SKalle Valo 
458e705c121SKalle Valo static int iwl_pcie_txq_build_tfd(struct iwl_trans *trans, struct iwl_txq *txq,
459e705c121SKalle Valo 				  dma_addr_t addr, u16 len, bool reset)
460e705c121SKalle Valo {
4613cd1980bSSara Sharon 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
4626983ba69SSara Sharon 	void *tfd;
463e705c121SKalle Valo 	u32 num_tbs;
464e705c121SKalle Valo 
465bb98ecd4SSara Sharon 	tfd = txq->tfds + trans_pcie->tfd_size * txq->write_ptr;
466e705c121SKalle Valo 
467e705c121SKalle Valo 	if (reset)
4686983ba69SSara Sharon 		memset(tfd, 0, trans_pcie->tfd_size);
469e705c121SKalle Valo 
4706983ba69SSara Sharon 	num_tbs = iwl_pcie_tfd_get_num_tbs(trans, tfd);
471e705c121SKalle Valo 
4726983ba69SSara Sharon 	/* Each TFD can point to a maximum max_tbs Tx buffers */
4733cd1980bSSara Sharon 	if (num_tbs >= trans_pcie->max_tbs) {
474e705c121SKalle Valo 		IWL_ERR(trans, "Error can not send more than %d chunks\n",
4753cd1980bSSara Sharon 			trans_pcie->max_tbs);
476e705c121SKalle Valo 		return -EINVAL;
477e705c121SKalle Valo 	}
478e705c121SKalle Valo 
479e705c121SKalle Valo 	if (WARN(addr & ~IWL_TX_DMA_MASK,
480e705c121SKalle Valo 		 "Unaligned address = %llx\n", (unsigned long long)addr))
481e705c121SKalle Valo 		return -EINVAL;
482e705c121SKalle Valo 
4836983ba69SSara Sharon 	iwl_pcie_tfd_set_tb(trans, tfd, num_tbs, addr, len);
484e705c121SKalle Valo 
485e705c121SKalle Valo 	return num_tbs;
486e705c121SKalle Valo }
487e705c121SKalle Valo 
48813a3a390SSara Sharon int iwl_pcie_txq_alloc(struct iwl_trans *trans, struct iwl_txq *txq,
489b8e8d7ceSSara Sharon 		       int slots_num, bool cmd_queue)
490e705c121SKalle Valo {
491e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
4926983ba69SSara Sharon 	size_t tfd_sz = trans_pcie->tfd_size * TFD_QUEUE_SIZE_MAX;
4938de437c7SSara Sharon 	size_t tb0_buf_sz;
494e705c121SKalle Valo 	int i;
495e705c121SKalle Valo 
496e705c121SKalle Valo 	if (WARN_ON(txq->entries || txq->tfds))
497e705c121SKalle Valo 		return -EINVAL;
498e705c121SKalle Valo 
499e705c121SKalle Valo 	setup_timer(&txq->stuck_timer, iwl_pcie_txq_stuck_timer,
500e705c121SKalle Valo 		    (unsigned long)txq);
501e705c121SKalle Valo 	txq->trans_pcie = trans_pcie;
502e705c121SKalle Valo 
503bb98ecd4SSara Sharon 	txq->n_window = slots_num;
504e705c121SKalle Valo 
505e705c121SKalle Valo 	txq->entries = kcalloc(slots_num,
506e705c121SKalle Valo 			       sizeof(struct iwl_pcie_txq_entry),
507e705c121SKalle Valo 			       GFP_KERNEL);
508e705c121SKalle Valo 
509e705c121SKalle Valo 	if (!txq->entries)
510e705c121SKalle Valo 		goto error;
511e705c121SKalle Valo 
512b8e8d7ceSSara Sharon 	if (cmd_queue)
513e705c121SKalle Valo 		for (i = 0; i < slots_num; i++) {
514e705c121SKalle Valo 			txq->entries[i].cmd =
515e705c121SKalle Valo 				kmalloc(sizeof(struct iwl_device_cmd),
516e705c121SKalle Valo 					GFP_KERNEL);
517e705c121SKalle Valo 			if (!txq->entries[i].cmd)
518e705c121SKalle Valo 				goto error;
519e705c121SKalle Valo 		}
520e705c121SKalle Valo 
521e705c121SKalle Valo 	/* Circular buffer of transmit frame descriptors (TFDs),
522e705c121SKalle Valo 	 * shared with device */
523e705c121SKalle Valo 	txq->tfds = dma_alloc_coherent(trans->dev, tfd_sz,
524bb98ecd4SSara Sharon 				       &txq->dma_addr, GFP_KERNEL);
525e705c121SKalle Valo 	if (!txq->tfds)
526e705c121SKalle Valo 		goto error;
527e705c121SKalle Valo 
5288de437c7SSara Sharon 	BUILD_BUG_ON(IWL_FIRST_TB_SIZE_ALIGN != sizeof(*txq->first_tb_bufs));
529e705c121SKalle Valo 
5308de437c7SSara Sharon 	tb0_buf_sz = sizeof(*txq->first_tb_bufs) * slots_num;
531e705c121SKalle Valo 
5328de437c7SSara Sharon 	txq->first_tb_bufs = dma_alloc_coherent(trans->dev, tb0_buf_sz,
5338de437c7SSara Sharon 					      &txq->first_tb_dma,
534e705c121SKalle Valo 					      GFP_KERNEL);
5358de437c7SSara Sharon 	if (!txq->first_tb_bufs)
536e705c121SKalle Valo 		goto err_free_tfds;
537e705c121SKalle Valo 
538e705c121SKalle Valo 	return 0;
539e705c121SKalle Valo err_free_tfds:
540bb98ecd4SSara Sharon 	dma_free_coherent(trans->dev, tfd_sz, txq->tfds, txq->dma_addr);
541e705c121SKalle Valo error:
542b8e8d7ceSSara Sharon 	if (txq->entries && cmd_queue)
543e705c121SKalle Valo 		for (i = 0; i < slots_num; i++)
544e705c121SKalle Valo 			kfree(txq->entries[i].cmd);
545e705c121SKalle Valo 	kfree(txq->entries);
546e705c121SKalle Valo 	txq->entries = NULL;
547e705c121SKalle Valo 
548e705c121SKalle Valo 	return -ENOMEM;
549e705c121SKalle Valo 
550e705c121SKalle Valo }
551e705c121SKalle Valo 
55213a3a390SSara Sharon int iwl_pcie_txq_init(struct iwl_trans *trans, struct iwl_txq *txq,
553b8e8d7ceSSara Sharon 		      int slots_num, bool cmd_queue)
554e705c121SKalle Valo {
555e705c121SKalle Valo 	int ret;
556e705c121SKalle Valo 
557e705c121SKalle Valo 	txq->need_update = false;
558e705c121SKalle Valo 
559e705c121SKalle Valo 	/* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
560e705c121SKalle Valo 	 * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
561e705c121SKalle Valo 	BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
562e705c121SKalle Valo 
563e705c121SKalle Valo 	/* Initialize queue's high/low-water marks, and head/tail indexes */
564b8e8d7ceSSara Sharon 	ret = iwl_queue_init(txq, slots_num);
565e705c121SKalle Valo 	if (ret)
566e705c121SKalle Valo 		return ret;
567e705c121SKalle Valo 
568e705c121SKalle Valo 	spin_lock_init(&txq->lock);
569faead41cSJohannes Berg 
570b8e8d7ceSSara Sharon 	if (cmd_queue) {
571faead41cSJohannes Berg 		static struct lock_class_key iwl_pcie_cmd_queue_lock_class;
572faead41cSJohannes Berg 
573faead41cSJohannes Berg 		lockdep_set_class(&txq->lock, &iwl_pcie_cmd_queue_lock_class);
574faead41cSJohannes Berg 	}
575faead41cSJohannes Berg 
5763955525dSEmmanuel Grumbach 	__skb_queue_head_init(&txq->overflow_q);
577e705c121SKalle Valo 
578e705c121SKalle Valo 	return 0;
579e705c121SKalle Valo }
580e705c121SKalle Valo 
58121cb3222SJohannes Berg static void iwl_pcie_free_tso_page(struct iwl_trans_pcie *trans_pcie,
58221cb3222SJohannes Berg 				   struct sk_buff *skb)
5836eb5e529SEmmanuel Grumbach {
58421cb3222SJohannes Berg 	struct page **page_ptr;
5856eb5e529SEmmanuel Grumbach 
58621cb3222SJohannes Berg 	page_ptr = (void *)((u8 *)skb->cb + trans_pcie->page_offs);
5876eb5e529SEmmanuel Grumbach 
58821cb3222SJohannes Berg 	if (*page_ptr) {
58921cb3222SJohannes Berg 		__free_page(*page_ptr);
59021cb3222SJohannes Berg 		*page_ptr = NULL;
5916eb5e529SEmmanuel Grumbach 	}
5926eb5e529SEmmanuel Grumbach }
5936eb5e529SEmmanuel Grumbach 
59401d11cd1SSara Sharon static void iwl_pcie_clear_cmd_in_flight(struct iwl_trans *trans)
59501d11cd1SSara Sharon {
59601d11cd1SSara Sharon 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
59701d11cd1SSara Sharon 
59801d11cd1SSara Sharon 	lockdep_assert_held(&trans_pcie->reg_lock);
59901d11cd1SSara Sharon 
60001d11cd1SSara Sharon 	if (trans_pcie->ref_cmd_in_flight) {
60101d11cd1SSara Sharon 		trans_pcie->ref_cmd_in_flight = false;
60201d11cd1SSara Sharon 		IWL_DEBUG_RPM(trans, "clear ref_cmd_in_flight - unref\n");
603c24c7f58SLuca Coelho 		iwl_trans_unref(trans);
60401d11cd1SSara Sharon 	}
60501d11cd1SSara Sharon 
60601d11cd1SSara Sharon 	if (!trans->cfg->base_params->apmg_wake_up_wa)
60701d11cd1SSara Sharon 		return;
60801d11cd1SSara Sharon 	if (WARN_ON(!trans_pcie->cmd_hold_nic_awake))
60901d11cd1SSara Sharon 		return;
61001d11cd1SSara Sharon 
61101d11cd1SSara Sharon 	trans_pcie->cmd_hold_nic_awake = false;
61201d11cd1SSara Sharon 	__iwl_trans_pcie_clear_bit(trans, CSR_GP_CNTRL,
61301d11cd1SSara Sharon 				   CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
61401d11cd1SSara Sharon }
61501d11cd1SSara Sharon 
616e705c121SKalle Valo /*
617e705c121SKalle Valo  * iwl_pcie_txq_unmap -  Unmap any remaining DMA mappings and free skb's
618e705c121SKalle Valo  */
619e705c121SKalle Valo static void iwl_pcie_txq_unmap(struct iwl_trans *trans, int txq_id)
620e705c121SKalle Valo {
621e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
622b2a3b1c1SSara Sharon 	struct iwl_txq *txq = trans_pcie->txq[txq_id];
623e705c121SKalle Valo 
624e705c121SKalle Valo 	spin_lock_bh(&txq->lock);
625bb98ecd4SSara Sharon 	while (txq->write_ptr != txq->read_ptr) {
626e705c121SKalle Valo 		IWL_DEBUG_TX_REPLY(trans, "Q %d Free %d\n",
627bb98ecd4SSara Sharon 				   txq_id, txq->read_ptr);
6286eb5e529SEmmanuel Grumbach 
6296eb5e529SEmmanuel Grumbach 		if (txq_id != trans_pcie->cmd_queue) {
630bb98ecd4SSara Sharon 			struct sk_buff *skb = txq->entries[txq->read_ptr].skb;
6316eb5e529SEmmanuel Grumbach 
6326eb5e529SEmmanuel Grumbach 			if (WARN_ON_ONCE(!skb))
6336eb5e529SEmmanuel Grumbach 				continue;
6346eb5e529SEmmanuel Grumbach 
63521cb3222SJohannes Berg 			iwl_pcie_free_tso_page(trans_pcie, skb);
6366eb5e529SEmmanuel Grumbach 		}
637e705c121SKalle Valo 		iwl_pcie_txq_free_tfd(trans, txq);
638bb98ecd4SSara Sharon 		txq->read_ptr = iwl_queue_inc_wrap(txq->read_ptr);
63901d11cd1SSara Sharon 
640bb98ecd4SSara Sharon 		if (txq->read_ptr == txq->write_ptr) {
64101d11cd1SSara Sharon 			unsigned long flags;
64201d11cd1SSara Sharon 
64301d11cd1SSara Sharon 			spin_lock_irqsave(&trans_pcie->reg_lock, flags);
64401d11cd1SSara Sharon 			if (txq_id != trans_pcie->cmd_queue) {
64501d11cd1SSara Sharon 				IWL_DEBUG_RPM(trans, "Q %d - last tx freed\n",
646bb98ecd4SSara Sharon 					      txq->id);
647c24c7f58SLuca Coelho 				iwl_trans_unref(trans);
64801d11cd1SSara Sharon 			} else {
64901d11cd1SSara Sharon 				iwl_pcie_clear_cmd_in_flight(trans);
65001d11cd1SSara Sharon 			}
65101d11cd1SSara Sharon 			spin_unlock_irqrestore(&trans_pcie->reg_lock, flags);
65201d11cd1SSara Sharon 		}
653e705c121SKalle Valo 	}
6543955525dSEmmanuel Grumbach 
6553955525dSEmmanuel Grumbach 	while (!skb_queue_empty(&txq->overflow_q)) {
6563955525dSEmmanuel Grumbach 		struct sk_buff *skb = __skb_dequeue(&txq->overflow_q);
6573955525dSEmmanuel Grumbach 
6583955525dSEmmanuel Grumbach 		iwl_op_mode_free_skb(trans->op_mode, skb);
6593955525dSEmmanuel Grumbach 	}
6603955525dSEmmanuel Grumbach 
661e705c121SKalle Valo 	spin_unlock_bh(&txq->lock);
662e705c121SKalle Valo 
663e705c121SKalle Valo 	/* just in case - this queue may have been stopped */
664e705c121SKalle Valo 	iwl_wake_queue(trans, txq);
665e705c121SKalle Valo }
666e705c121SKalle Valo 
667e705c121SKalle Valo /*
668e705c121SKalle Valo  * iwl_pcie_txq_free - Deallocate DMA queue.
669e705c121SKalle Valo  * @txq: Transmit queue to deallocate.
670e705c121SKalle Valo  *
671e705c121SKalle Valo  * Empty queue by removing and destroying all BD's.
672e705c121SKalle Valo  * Free all buffers.
673e705c121SKalle Valo  * 0-fill, but do not free "txq" descriptor structure.
674e705c121SKalle Valo  */
675e705c121SKalle Valo static void iwl_pcie_txq_free(struct iwl_trans *trans, int txq_id)
676e705c121SKalle Valo {
677e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
678b2a3b1c1SSara Sharon 	struct iwl_txq *txq = trans_pcie->txq[txq_id];
679e705c121SKalle Valo 	struct device *dev = trans->dev;
680e705c121SKalle Valo 	int i;
681e705c121SKalle Valo 
682e705c121SKalle Valo 	if (WARN_ON(!txq))
683e705c121SKalle Valo 		return;
684e705c121SKalle Valo 
685e705c121SKalle Valo 	iwl_pcie_txq_unmap(trans, txq_id);
686e705c121SKalle Valo 
687e705c121SKalle Valo 	/* De-alloc array of command/tx buffers */
688e705c121SKalle Valo 	if (txq_id == trans_pcie->cmd_queue)
689bb98ecd4SSara Sharon 		for (i = 0; i < txq->n_window; i++) {
690e705c121SKalle Valo 			kzfree(txq->entries[i].cmd);
691e705c121SKalle Valo 			kzfree(txq->entries[i].free_buf);
692e705c121SKalle Valo 		}
693e705c121SKalle Valo 
694e705c121SKalle Valo 	/* De-alloc circular buffer of TFDs */
695e705c121SKalle Valo 	if (txq->tfds) {
696e705c121SKalle Valo 		dma_free_coherent(dev,
6976983ba69SSara Sharon 				  trans_pcie->tfd_size * TFD_QUEUE_SIZE_MAX,
698bb98ecd4SSara Sharon 				  txq->tfds, txq->dma_addr);
699bb98ecd4SSara Sharon 		txq->dma_addr = 0;
700e705c121SKalle Valo 		txq->tfds = NULL;
701e705c121SKalle Valo 
702e705c121SKalle Valo 		dma_free_coherent(dev,
703bb98ecd4SSara Sharon 				  sizeof(*txq->first_tb_bufs) * txq->n_window,
7048de437c7SSara Sharon 				  txq->first_tb_bufs, txq->first_tb_dma);
705e705c121SKalle Valo 	}
706e705c121SKalle Valo 
707e705c121SKalle Valo 	kfree(txq->entries);
708e705c121SKalle Valo 	txq->entries = NULL;
709e705c121SKalle Valo 
710e705c121SKalle Valo 	del_timer_sync(&txq->stuck_timer);
711e705c121SKalle Valo 
712e705c121SKalle Valo 	/* 0-fill queue descriptor structure */
713e705c121SKalle Valo 	memset(txq, 0, sizeof(*txq));
714e705c121SKalle Valo }
715e705c121SKalle Valo 
716e705c121SKalle Valo void iwl_pcie_tx_start(struct iwl_trans *trans, u32 scd_base_addr)
717e705c121SKalle Valo {
718e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
719e705c121SKalle Valo 	int nq = trans->cfg->base_params->num_of_queues;
720e705c121SKalle Valo 	int chan;
721e705c121SKalle Valo 	u32 reg_val;
722e705c121SKalle Valo 	int clear_dwords = (SCD_TRANS_TBL_OFFSET_QUEUE(nq) -
723e705c121SKalle Valo 				SCD_CONTEXT_MEM_LOWER_BOUND) / sizeof(u32);
724e705c121SKalle Valo 
725e705c121SKalle Valo 	/* make sure all queue are not stopped/used */
726e705c121SKalle Valo 	memset(trans_pcie->queue_stopped, 0, sizeof(trans_pcie->queue_stopped));
727e705c121SKalle Valo 	memset(trans_pcie->queue_used, 0, sizeof(trans_pcie->queue_used));
728e705c121SKalle Valo 
729e705c121SKalle Valo 	trans_pcie->scd_base_addr =
730e705c121SKalle Valo 		iwl_read_prph(trans, SCD_SRAM_BASE_ADDR);
731e705c121SKalle Valo 
732e705c121SKalle Valo 	WARN_ON(scd_base_addr != 0 &&
733e705c121SKalle Valo 		scd_base_addr != trans_pcie->scd_base_addr);
734e705c121SKalle Valo 
735e705c121SKalle Valo 	/* reset context data, TX status and translation data */
736e705c121SKalle Valo 	iwl_trans_write_mem(trans, trans_pcie->scd_base_addr +
737e705c121SKalle Valo 				   SCD_CONTEXT_MEM_LOWER_BOUND,
738e705c121SKalle Valo 			    NULL, clear_dwords);
739e705c121SKalle Valo 
740e705c121SKalle Valo 	iwl_write_prph(trans, SCD_DRAM_BASE_ADDR,
741e705c121SKalle Valo 		       trans_pcie->scd_bc_tbls.dma >> 10);
742e705c121SKalle Valo 
743e705c121SKalle Valo 	/* The chain extension of the SCD doesn't work well. This feature is
744e705c121SKalle Valo 	 * enabled by default by the HW, so we need to disable it manually.
745e705c121SKalle Valo 	 */
746e705c121SKalle Valo 	if (trans->cfg->base_params->scd_chain_ext_wa)
747e705c121SKalle Valo 		iwl_write_prph(trans, SCD_CHAINEXT_EN, 0);
748e705c121SKalle Valo 
749e705c121SKalle Valo 	iwl_trans_ac_txq_enable(trans, trans_pcie->cmd_queue,
750e705c121SKalle Valo 				trans_pcie->cmd_fifo,
751e705c121SKalle Valo 				trans_pcie->cmd_q_wdg_timeout);
752e705c121SKalle Valo 
753e705c121SKalle Valo 	/* Activate all Tx DMA/FIFO channels */
754e705c121SKalle Valo 	iwl_scd_activate_fifos(trans);
755e705c121SKalle Valo 
756e705c121SKalle Valo 	/* Enable DMA channel */
757e705c121SKalle Valo 	for (chan = 0; chan < FH_TCSR_CHNL_NUM; chan++)
758e705c121SKalle Valo 		iwl_write_direct32(trans, FH_TCSR_CHNL_TX_CONFIG_REG(chan),
759e705c121SKalle Valo 				   FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
760e705c121SKalle Valo 				   FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE);
761e705c121SKalle Valo 
762e705c121SKalle Valo 	/* Update FH chicken bits */
763e705c121SKalle Valo 	reg_val = iwl_read_direct32(trans, FH_TX_CHICKEN_BITS_REG);
764e705c121SKalle Valo 	iwl_write_direct32(trans, FH_TX_CHICKEN_BITS_REG,
765e705c121SKalle Valo 			   reg_val | FH_TX_CHICKEN_BITS_SCD_AUTO_RETRY_EN);
766e705c121SKalle Valo 
767e705c121SKalle Valo 	/* Enable L1-Active */
7686e584873SSara Sharon 	if (trans->cfg->device_family < IWL_DEVICE_FAMILY_8000)
769e705c121SKalle Valo 		iwl_clear_bits_prph(trans, APMG_PCIDEV_STT_REG,
770e705c121SKalle Valo 				    APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
771e705c121SKalle Valo }
772e705c121SKalle Valo 
773e705c121SKalle Valo void iwl_trans_pcie_tx_reset(struct iwl_trans *trans)
774e705c121SKalle Valo {
775e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
776e705c121SKalle Valo 	int txq_id;
777e705c121SKalle Valo 
77813a3a390SSara Sharon 	/*
77913a3a390SSara Sharon 	 * we should never get here in gen2 trans mode return early to avoid
78013a3a390SSara Sharon 	 * having invalid accesses
78113a3a390SSara Sharon 	 */
78213a3a390SSara Sharon 	if (WARN_ON_ONCE(trans->cfg->gen2))
78313a3a390SSara Sharon 		return;
78413a3a390SSara Sharon 
785e705c121SKalle Valo 	for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues;
786e705c121SKalle Valo 	     txq_id++) {
787b2a3b1c1SSara Sharon 		struct iwl_txq *txq = trans_pcie->txq[txq_id];
788e22744afSSara Sharon 		if (trans->cfg->use_tfh)
789e22744afSSara Sharon 			iwl_write_direct64(trans,
790e22744afSSara Sharon 					   FH_MEM_CBBC_QUEUE(trans, txq_id),
791bb98ecd4SSara Sharon 					   txq->dma_addr);
792e22744afSSara Sharon 		else
793e22744afSSara Sharon 			iwl_write_direct32(trans,
794e22744afSSara Sharon 					   FH_MEM_CBBC_QUEUE(trans, txq_id),
795bb98ecd4SSara Sharon 					   txq->dma_addr >> 8);
796e705c121SKalle Valo 		iwl_pcie_txq_unmap(trans, txq_id);
797bb98ecd4SSara Sharon 		txq->read_ptr = 0;
798bb98ecd4SSara Sharon 		txq->write_ptr = 0;
799e705c121SKalle Valo 	}
800e705c121SKalle Valo 
801e705c121SKalle Valo 	/* Tell NIC where to find the "keep warm" buffer */
802e705c121SKalle Valo 	iwl_write_direct32(trans, FH_KW_MEM_ADDR_REG,
803e705c121SKalle Valo 			   trans_pcie->kw.dma >> 4);
804e705c121SKalle Valo 
805e705c121SKalle Valo 	/*
806e705c121SKalle Valo 	 * Send 0 as the scd_base_addr since the device may have be reset
807e705c121SKalle Valo 	 * while we were in WoWLAN in which case SCD_SRAM_BASE_ADDR will
808e705c121SKalle Valo 	 * contain garbage.
809e705c121SKalle Valo 	 */
810e705c121SKalle Valo 	iwl_pcie_tx_start(trans, 0);
811e705c121SKalle Valo }
812e705c121SKalle Valo 
813e705c121SKalle Valo static void iwl_pcie_tx_stop_fh(struct iwl_trans *trans)
814e705c121SKalle Valo {
815e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
816e705c121SKalle Valo 	unsigned long flags;
817e705c121SKalle Valo 	int ch, ret;
818e705c121SKalle Valo 	u32 mask = 0;
819e705c121SKalle Valo 
820e705c121SKalle Valo 	spin_lock(&trans_pcie->irq_lock);
821e705c121SKalle Valo 
82223ba9340SEmmanuel Grumbach 	if (!iwl_trans_grab_nic_access(trans, &flags))
823e705c121SKalle Valo 		goto out;
824e705c121SKalle Valo 
825e705c121SKalle Valo 	/* Stop each Tx DMA channel */
826e705c121SKalle Valo 	for (ch = 0; ch < FH_TCSR_CHNL_NUM; ch++) {
827e705c121SKalle Valo 		iwl_write32(trans, FH_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0);
828e705c121SKalle Valo 		mask |= FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch);
829e705c121SKalle Valo 	}
830e705c121SKalle Valo 
831e705c121SKalle Valo 	/* Wait for DMA channels to be idle */
832e705c121SKalle Valo 	ret = iwl_poll_bit(trans, FH_TSSR_TX_STATUS_REG, mask, mask, 5000);
833e705c121SKalle Valo 	if (ret < 0)
834e705c121SKalle Valo 		IWL_ERR(trans,
835e705c121SKalle Valo 			"Failing on timeout while stopping DMA channel %d [0x%08x]\n",
836e705c121SKalle Valo 			ch, iwl_read32(trans, FH_TSSR_TX_STATUS_REG));
837e705c121SKalle Valo 
838e705c121SKalle Valo 	iwl_trans_release_nic_access(trans, &flags);
839e705c121SKalle Valo 
840e705c121SKalle Valo out:
841e705c121SKalle Valo 	spin_unlock(&trans_pcie->irq_lock);
842e705c121SKalle Valo }
843e705c121SKalle Valo 
844e705c121SKalle Valo /*
845e705c121SKalle Valo  * iwl_pcie_tx_stop - Stop all Tx DMA channels
846e705c121SKalle Valo  */
847e705c121SKalle Valo int iwl_pcie_tx_stop(struct iwl_trans *trans)
848e705c121SKalle Valo {
849e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
850e705c121SKalle Valo 	int txq_id;
851e705c121SKalle Valo 
852e705c121SKalle Valo 	/* Turn off all Tx DMA fifos */
853e705c121SKalle Valo 	iwl_scd_deactivate_fifos(trans);
854e705c121SKalle Valo 
855e705c121SKalle Valo 	/* Turn off all Tx DMA channels */
856e705c121SKalle Valo 	iwl_pcie_tx_stop_fh(trans);
857e705c121SKalle Valo 
858e705c121SKalle Valo 	/*
859e705c121SKalle Valo 	 * This function can be called before the op_mode disabled the
860e705c121SKalle Valo 	 * queues. This happens when we have an rfkill interrupt.
861e705c121SKalle Valo 	 * Since we stop Tx altogether - mark the queues as stopped.
862e705c121SKalle Valo 	 */
863e705c121SKalle Valo 	memset(trans_pcie->queue_stopped, 0, sizeof(trans_pcie->queue_stopped));
864e705c121SKalle Valo 	memset(trans_pcie->queue_used, 0, sizeof(trans_pcie->queue_used));
865e705c121SKalle Valo 
866e705c121SKalle Valo 	/* This can happen: start_hw, stop_device */
867b2a3b1c1SSara Sharon 	if (!trans_pcie->txq_memory)
868e705c121SKalle Valo 		return 0;
869e705c121SKalle Valo 
870e705c121SKalle Valo 	/* Unmap DMA from host system and free skb's */
871e705c121SKalle Valo 	for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues;
872e705c121SKalle Valo 	     txq_id++)
873e705c121SKalle Valo 		iwl_pcie_txq_unmap(trans, txq_id);
874e705c121SKalle Valo 
875e705c121SKalle Valo 	return 0;
876e705c121SKalle Valo }
877e705c121SKalle Valo 
878e705c121SKalle Valo /*
879e705c121SKalle Valo  * iwl_trans_tx_free - Free TXQ Context
880e705c121SKalle Valo  *
881e705c121SKalle Valo  * Destroy all TX DMA queues and structures
882e705c121SKalle Valo  */
883e705c121SKalle Valo void iwl_pcie_tx_free(struct iwl_trans *trans)
884e705c121SKalle Valo {
885e705c121SKalle Valo 	int txq_id;
886e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
887e705c121SKalle Valo 
888de74c455SSara Sharon 	memset(trans_pcie->queue_used, 0, sizeof(trans_pcie->queue_used));
889de74c455SSara Sharon 
890e705c121SKalle Valo 	/* Tx queues */
891b2a3b1c1SSara Sharon 	if (trans_pcie->txq_memory) {
892e705c121SKalle Valo 		for (txq_id = 0;
893b2a3b1c1SSara Sharon 		     txq_id < trans->cfg->base_params->num_of_queues;
894b2a3b1c1SSara Sharon 		     txq_id++) {
895e705c121SKalle Valo 			iwl_pcie_txq_free(trans, txq_id);
896b2a3b1c1SSara Sharon 			trans_pcie->txq[txq_id] = NULL;
897b2a3b1c1SSara Sharon 		}
898e705c121SKalle Valo 	}
899e705c121SKalle Valo 
900b2a3b1c1SSara Sharon 	kfree(trans_pcie->txq_memory);
901b2a3b1c1SSara Sharon 	trans_pcie->txq_memory = NULL;
902e705c121SKalle Valo 
903e705c121SKalle Valo 	iwl_pcie_free_dma_ptr(trans, &trans_pcie->kw);
904e705c121SKalle Valo 
905e705c121SKalle Valo 	iwl_pcie_free_dma_ptr(trans, &trans_pcie->scd_bc_tbls);
906e705c121SKalle Valo }
907e705c121SKalle Valo 
908e705c121SKalle Valo /*
909e705c121SKalle Valo  * iwl_pcie_tx_alloc - allocate TX context
910e705c121SKalle Valo  * Allocate all Tx DMA structures and initialize them
911e705c121SKalle Valo  */
912e705c121SKalle Valo static int iwl_pcie_tx_alloc(struct iwl_trans *trans)
913e705c121SKalle Valo {
914e705c121SKalle Valo 	int ret;
915e705c121SKalle Valo 	int txq_id, slots_num;
916e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
917e705c121SKalle Valo 
918e705c121SKalle Valo 	u16 scd_bc_tbls_size = trans->cfg->base_params->num_of_queues *
919e705c121SKalle Valo 			sizeof(struct iwlagn_scd_bc_tbl);
920e705c121SKalle Valo 
921e705c121SKalle Valo 	/*It is not allowed to alloc twice, so warn when this happens.
922e705c121SKalle Valo 	 * We cannot rely on the previous allocation, so free and fail */
923b2a3b1c1SSara Sharon 	if (WARN_ON(trans_pcie->txq_memory)) {
924e705c121SKalle Valo 		ret = -EINVAL;
925e705c121SKalle Valo 		goto error;
926e705c121SKalle Valo 	}
927e705c121SKalle Valo 
928e705c121SKalle Valo 	ret = iwl_pcie_alloc_dma_ptr(trans, &trans_pcie->scd_bc_tbls,
929e705c121SKalle Valo 				   scd_bc_tbls_size);
930e705c121SKalle Valo 	if (ret) {
931e705c121SKalle Valo 		IWL_ERR(trans, "Scheduler BC Table allocation failed\n");
932e705c121SKalle Valo 		goto error;
933e705c121SKalle Valo 	}
934e705c121SKalle Valo 
935e705c121SKalle Valo 	/* Alloc keep-warm buffer */
936e705c121SKalle Valo 	ret = iwl_pcie_alloc_dma_ptr(trans, &trans_pcie->kw, IWL_KW_SIZE);
937e705c121SKalle Valo 	if (ret) {
938e705c121SKalle Valo 		IWL_ERR(trans, "Keep Warm allocation failed\n");
939e705c121SKalle Valo 		goto error;
940e705c121SKalle Valo 	}
941e705c121SKalle Valo 
942b2a3b1c1SSara Sharon 	trans_pcie->txq_memory = kcalloc(trans->cfg->base_params->num_of_queues,
943e705c121SKalle Valo 					 sizeof(struct iwl_txq), GFP_KERNEL);
944b2a3b1c1SSara Sharon 	if (!trans_pcie->txq_memory) {
945e705c121SKalle Valo 		IWL_ERR(trans, "Not enough memory for txq\n");
946e705c121SKalle Valo 		ret = -ENOMEM;
947e705c121SKalle Valo 		goto error;
948e705c121SKalle Valo 	}
949e705c121SKalle Valo 
950e705c121SKalle Valo 	/* Alloc and init all Tx queues, including the command queue (#4/#9) */
951e705c121SKalle Valo 	for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues;
952e705c121SKalle Valo 	     txq_id++) {
953b8e8d7ceSSara Sharon 		bool cmd_queue = (txq_id == trans_pcie->cmd_queue);
954b8e8d7ceSSara Sharon 
955b8e8d7ceSSara Sharon 		slots_num = cmd_queue ? TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
956b2a3b1c1SSara Sharon 		trans_pcie->txq[txq_id] = &trans_pcie->txq_memory[txq_id];
957b2a3b1c1SSara Sharon 		ret = iwl_pcie_txq_alloc(trans, trans_pcie->txq[txq_id],
958b8e8d7ceSSara Sharon 					 slots_num, cmd_queue);
959e705c121SKalle Valo 		if (ret) {
960e705c121SKalle Valo 			IWL_ERR(trans, "Tx %d queue alloc failed\n", txq_id);
961e705c121SKalle Valo 			goto error;
962e705c121SKalle Valo 		}
963b8e8d7ceSSara Sharon 		trans_pcie->txq[txq_id]->id = txq_id;
964e705c121SKalle Valo 	}
965e705c121SKalle Valo 
966e705c121SKalle Valo 	return 0;
967e705c121SKalle Valo 
968e705c121SKalle Valo error:
969e705c121SKalle Valo 	iwl_pcie_tx_free(trans);
970e705c121SKalle Valo 
971e705c121SKalle Valo 	return ret;
972e705c121SKalle Valo }
973eda50cdeSSara Sharon 
974e705c121SKalle Valo int iwl_pcie_tx_init(struct iwl_trans *trans)
975e705c121SKalle Valo {
976e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
977e705c121SKalle Valo 	int ret;
978e705c121SKalle Valo 	int txq_id, slots_num;
979e705c121SKalle Valo 	bool alloc = false;
980e705c121SKalle Valo 
981b2a3b1c1SSara Sharon 	if (!trans_pcie->txq_memory) {
982e705c121SKalle Valo 		ret = iwl_pcie_tx_alloc(trans);
983e705c121SKalle Valo 		if (ret)
984e705c121SKalle Valo 			goto error;
985e705c121SKalle Valo 		alloc = true;
986e705c121SKalle Valo 	}
987e705c121SKalle Valo 
988e705c121SKalle Valo 	spin_lock(&trans_pcie->irq_lock);
989e705c121SKalle Valo 
990e705c121SKalle Valo 	/* Turn off all Tx DMA fifos */
991e705c121SKalle Valo 	iwl_scd_deactivate_fifos(trans);
992e705c121SKalle Valo 
993e705c121SKalle Valo 	/* Tell NIC where to find the "keep warm" buffer */
994e705c121SKalle Valo 	iwl_write_direct32(trans, FH_KW_MEM_ADDR_REG,
995e705c121SKalle Valo 			   trans_pcie->kw.dma >> 4);
996e705c121SKalle Valo 
997e705c121SKalle Valo 	spin_unlock(&trans_pcie->irq_lock);
998e705c121SKalle Valo 
999e705c121SKalle Valo 	/* Alloc and init all Tx queues, including the command queue (#4/#9) */
1000e705c121SKalle Valo 	for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues;
1001e705c121SKalle Valo 	     txq_id++) {
1002b8e8d7ceSSara Sharon 		bool cmd_queue = (txq_id == trans_pcie->cmd_queue);
1003b8e8d7ceSSara Sharon 
1004b8e8d7ceSSara Sharon 		slots_num = cmd_queue ? TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
1005b2a3b1c1SSara Sharon 		ret = iwl_pcie_txq_init(trans, trans_pcie->txq[txq_id],
1006b8e8d7ceSSara Sharon 					slots_num, cmd_queue);
1007e705c121SKalle Valo 		if (ret) {
1008e705c121SKalle Valo 			IWL_ERR(trans, "Tx %d queue init failed\n", txq_id);
1009e705c121SKalle Valo 			goto error;
1010e705c121SKalle Valo 		}
1011e705c121SKalle Valo 
1012eda50cdeSSara Sharon 		/*
1013eda50cdeSSara Sharon 		 * Tell nic where to find circular buffer of TFDs for a
1014eda50cdeSSara Sharon 		 * given Tx queue, and enable the DMA channel used for that
1015eda50cdeSSara Sharon 		 * queue.
1016eda50cdeSSara Sharon 		 * Circular buffer (TFD queue in DRAM) physical base address
1017eda50cdeSSara Sharon 		 */
1018eda50cdeSSara Sharon 		iwl_write_direct32(trans, FH_MEM_CBBC_QUEUE(trans, txq_id),
1019b2a3b1c1SSara Sharon 				   trans_pcie->txq[txq_id]->dma_addr >> 8);
1020ae79785fSSara Sharon 	}
1021e22744afSSara Sharon 
1022e705c121SKalle Valo 	iwl_set_bits_prph(trans, SCD_GP_CTRL, SCD_GP_CTRL_AUTO_ACTIVE_MODE);
1023e705c121SKalle Valo 	if (trans->cfg->base_params->num_of_queues > 20)
1024e705c121SKalle Valo 		iwl_set_bits_prph(trans, SCD_GP_CTRL,
1025e705c121SKalle Valo 				  SCD_GP_CTRL_ENABLE_31_QUEUES);
1026e705c121SKalle Valo 
1027e705c121SKalle Valo 	return 0;
1028e705c121SKalle Valo error:
1029e705c121SKalle Valo 	/*Upon error, free only if we allocated something */
1030e705c121SKalle Valo 	if (alloc)
1031e705c121SKalle Valo 		iwl_pcie_tx_free(trans);
1032e705c121SKalle Valo 	return ret;
1033e705c121SKalle Valo }
1034e705c121SKalle Valo 
1035e705c121SKalle Valo static inline void iwl_pcie_txq_progress(struct iwl_txq *txq)
1036e705c121SKalle Valo {
1037e705c121SKalle Valo 	lockdep_assert_held(&txq->lock);
1038e705c121SKalle Valo 
1039e705c121SKalle Valo 	if (!txq->wd_timeout)
1040e705c121SKalle Valo 		return;
1041e705c121SKalle Valo 
1042e705c121SKalle Valo 	/*
1043e705c121SKalle Valo 	 * station is asleep and we send data - that must
1044e705c121SKalle Valo 	 * be uAPSD or PS-Poll. Don't rearm the timer.
1045e705c121SKalle Valo 	 */
1046e705c121SKalle Valo 	if (txq->frozen)
1047e705c121SKalle Valo 		return;
1048e705c121SKalle Valo 
1049e705c121SKalle Valo 	/*
1050e705c121SKalle Valo 	 * if empty delete timer, otherwise move timer forward
1051e705c121SKalle Valo 	 * since we're making progress on this queue
1052e705c121SKalle Valo 	 */
1053bb98ecd4SSara Sharon 	if (txq->read_ptr == txq->write_ptr)
1054e705c121SKalle Valo 		del_timer(&txq->stuck_timer);
1055e705c121SKalle Valo 	else
1056e705c121SKalle Valo 		mod_timer(&txq->stuck_timer, jiffies + txq->wd_timeout);
1057e705c121SKalle Valo }
1058e705c121SKalle Valo 
1059e705c121SKalle Valo /* Frees buffers until index _not_ inclusive */
1060e705c121SKalle Valo void iwl_trans_pcie_reclaim(struct iwl_trans *trans, int txq_id, int ssn,
1061e705c121SKalle Valo 			    struct sk_buff_head *skbs)
1062e705c121SKalle Valo {
1063e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1064b2a3b1c1SSara Sharon 	struct iwl_txq *txq = trans_pcie->txq[txq_id];
1065e705c121SKalle Valo 	int tfd_num = ssn & (TFD_QUEUE_SIZE_MAX - 1);
1066e705c121SKalle Valo 	int last_to_free;
1067e705c121SKalle Valo 
1068e705c121SKalle Valo 	/* This function is not meant to release cmd queue*/
1069e705c121SKalle Valo 	if (WARN_ON(txq_id == trans_pcie->cmd_queue))
1070e705c121SKalle Valo 		return;
1071e705c121SKalle Valo 
1072e705c121SKalle Valo 	spin_lock_bh(&txq->lock);
1073e705c121SKalle Valo 
1074de74c455SSara Sharon 	if (!test_bit(txq_id, trans_pcie->queue_used)) {
1075e705c121SKalle Valo 		IWL_DEBUG_TX_QUEUES(trans, "Q %d inactive - ignoring idx %d\n",
1076e705c121SKalle Valo 				    txq_id, ssn);
1077e705c121SKalle Valo 		goto out;
1078e705c121SKalle Valo 	}
1079e705c121SKalle Valo 
1080bb98ecd4SSara Sharon 	if (txq->read_ptr == tfd_num)
1081e705c121SKalle Valo 		goto out;
1082e705c121SKalle Valo 
1083e705c121SKalle Valo 	IWL_DEBUG_TX_REPLY(trans, "[Q %d] %d -> %d (%d)\n",
1084bb98ecd4SSara Sharon 			   txq_id, txq->read_ptr, tfd_num, ssn);
1085e705c121SKalle Valo 
1086e705c121SKalle Valo 	/*Since we free until index _not_ inclusive, the one before index is
1087e705c121SKalle Valo 	 * the last we will free. This one must be used */
1088e705c121SKalle Valo 	last_to_free = iwl_queue_dec_wrap(tfd_num);
1089e705c121SKalle Valo 
1090bb98ecd4SSara Sharon 	if (!iwl_queue_used(txq, last_to_free)) {
1091e705c121SKalle Valo 		IWL_ERR(trans,
1092e705c121SKalle Valo 			"%s: Read index for DMA queue txq id (%d), last_to_free %d is out of range [0-%d] %d %d.\n",
1093e705c121SKalle Valo 			__func__, txq_id, last_to_free, TFD_QUEUE_SIZE_MAX,
1094bb98ecd4SSara Sharon 			txq->write_ptr, txq->read_ptr);
1095e705c121SKalle Valo 		goto out;
1096e705c121SKalle Valo 	}
1097e705c121SKalle Valo 
1098e705c121SKalle Valo 	if (WARN_ON(!skb_queue_empty(skbs)))
1099e705c121SKalle Valo 		goto out;
1100e705c121SKalle Valo 
1101e705c121SKalle Valo 	for (;
1102bb98ecd4SSara Sharon 	     txq->read_ptr != tfd_num;
1103bb98ecd4SSara Sharon 	     txq->read_ptr = iwl_queue_inc_wrap(txq->read_ptr)) {
1104bb98ecd4SSara Sharon 		struct sk_buff *skb = txq->entries[txq->read_ptr].skb;
1105e705c121SKalle Valo 
11066eb5e529SEmmanuel Grumbach 		if (WARN_ON_ONCE(!skb))
1107e705c121SKalle Valo 			continue;
1108e705c121SKalle Valo 
110921cb3222SJohannes Berg 		iwl_pcie_free_tso_page(trans_pcie, skb);
11106eb5e529SEmmanuel Grumbach 
11116eb5e529SEmmanuel Grumbach 		__skb_queue_tail(skbs, skb);
1112e705c121SKalle Valo 
1113bb98ecd4SSara Sharon 		txq->entries[txq->read_ptr].skb = NULL;
1114e705c121SKalle Valo 
11154fe10bc6SSara Sharon 		if (!trans->cfg->use_tfh)
1116e705c121SKalle Valo 			iwl_pcie_txq_inval_byte_cnt_tbl(trans, txq);
1117e705c121SKalle Valo 
1118e705c121SKalle Valo 		iwl_pcie_txq_free_tfd(trans, txq);
1119e705c121SKalle Valo 	}
1120e705c121SKalle Valo 
1121e705c121SKalle Valo 	iwl_pcie_txq_progress(txq);
1122e705c121SKalle Valo 
1123bb98ecd4SSara Sharon 	if (iwl_queue_space(txq) > txq->low_mark &&
11243955525dSEmmanuel Grumbach 	    test_bit(txq_id, trans_pcie->queue_stopped)) {
1125685b346cSEmmanuel Grumbach 		struct sk_buff_head overflow_skbs;
11263955525dSEmmanuel Grumbach 
1127685b346cSEmmanuel Grumbach 		__skb_queue_head_init(&overflow_skbs);
1128685b346cSEmmanuel Grumbach 		skb_queue_splice_init(&txq->overflow_q, &overflow_skbs);
11293955525dSEmmanuel Grumbach 
11303955525dSEmmanuel Grumbach 		/*
11313955525dSEmmanuel Grumbach 		 * This is tricky: we are in reclaim path which is non
11323955525dSEmmanuel Grumbach 		 * re-entrant, so noone will try to take the access the
11333955525dSEmmanuel Grumbach 		 * txq data from that path. We stopped tx, so we can't
11343955525dSEmmanuel Grumbach 		 * have tx as well. Bottom line, we can unlock and re-lock
11353955525dSEmmanuel Grumbach 		 * later.
11363955525dSEmmanuel Grumbach 		 */
11373955525dSEmmanuel Grumbach 		spin_unlock_bh(&txq->lock);
11383955525dSEmmanuel Grumbach 
1139685b346cSEmmanuel Grumbach 		while (!skb_queue_empty(&overflow_skbs)) {
1140685b346cSEmmanuel Grumbach 			struct sk_buff *skb = __skb_dequeue(&overflow_skbs);
114121cb3222SJohannes Berg 			struct iwl_device_cmd *dev_cmd_ptr;
114221cb3222SJohannes Berg 
114321cb3222SJohannes Berg 			dev_cmd_ptr = *(void **)((u8 *)skb->cb +
114421cb3222SJohannes Berg 						 trans_pcie->dev_cmd_offs);
11453955525dSEmmanuel Grumbach 
11463955525dSEmmanuel Grumbach 			/*
11473955525dSEmmanuel Grumbach 			 * Note that we can very well be overflowing again.
11483955525dSEmmanuel Grumbach 			 * In that case, iwl_queue_space will be small again
11493955525dSEmmanuel Grumbach 			 * and we won't wake mac80211's queue.
11503955525dSEmmanuel Grumbach 			 */
115121cb3222SJohannes Berg 			iwl_trans_pcie_tx(trans, skb, dev_cmd_ptr, txq_id);
11523955525dSEmmanuel Grumbach 		}
11533955525dSEmmanuel Grumbach 		spin_lock_bh(&txq->lock);
11543955525dSEmmanuel Grumbach 
1155bb98ecd4SSara Sharon 		if (iwl_queue_space(txq) > txq->low_mark)
1156e705c121SKalle Valo 			iwl_wake_queue(trans, txq);
11573955525dSEmmanuel Grumbach 	}
1158e705c121SKalle Valo 
1159bb98ecd4SSara Sharon 	if (txq->read_ptr == txq->write_ptr) {
1160bb98ecd4SSara Sharon 		IWL_DEBUG_RPM(trans, "Q %d - last tx reclaimed\n", txq->id);
1161c24c7f58SLuca Coelho 		iwl_trans_unref(trans);
1162e705c121SKalle Valo 	}
1163e705c121SKalle Valo 
1164e705c121SKalle Valo out:
1165e705c121SKalle Valo 	spin_unlock_bh(&txq->lock);
1166e705c121SKalle Valo }
1167e705c121SKalle Valo 
1168e705c121SKalle Valo static int iwl_pcie_set_cmd_in_flight(struct iwl_trans *trans,
1169e705c121SKalle Valo 				      const struct iwl_host_cmd *cmd)
1170e705c121SKalle Valo {
1171e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1172e705c121SKalle Valo 	int ret;
1173e705c121SKalle Valo 
1174e705c121SKalle Valo 	lockdep_assert_held(&trans_pcie->reg_lock);
1175e705c121SKalle Valo 
1176e705c121SKalle Valo 	if (!(cmd->flags & CMD_SEND_IN_IDLE) &&
1177e705c121SKalle Valo 	    !trans_pcie->ref_cmd_in_flight) {
1178e705c121SKalle Valo 		trans_pcie->ref_cmd_in_flight = true;
1179e705c121SKalle Valo 		IWL_DEBUG_RPM(trans, "set ref_cmd_in_flight - ref\n");
1180c24c7f58SLuca Coelho 		iwl_trans_ref(trans);
1181e705c121SKalle Valo 	}
1182e705c121SKalle Valo 
1183e705c121SKalle Valo 	/*
1184e705c121SKalle Valo 	 * wake up the NIC to make sure that the firmware will see the host
1185e705c121SKalle Valo 	 * command - we will let the NIC sleep once all the host commands
1186e705c121SKalle Valo 	 * returned. This needs to be done only on NICs that have
1187e705c121SKalle Valo 	 * apmg_wake_up_wa set.
1188e705c121SKalle Valo 	 */
1189e705c121SKalle Valo 	if (trans->cfg->base_params->apmg_wake_up_wa &&
1190e705c121SKalle Valo 	    !trans_pcie->cmd_hold_nic_awake) {
1191e705c121SKalle Valo 		__iwl_trans_pcie_set_bit(trans, CSR_GP_CNTRL,
1192e705c121SKalle Valo 					 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
1193e705c121SKalle Valo 
1194e705c121SKalle Valo 		ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
1195e705c121SKalle Valo 				   CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN,
1196e705c121SKalle Valo 				   (CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
1197e705c121SKalle Valo 				    CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP),
1198e705c121SKalle Valo 				   15000);
1199e705c121SKalle Valo 		if (ret < 0) {
1200e705c121SKalle Valo 			__iwl_trans_pcie_clear_bit(trans, CSR_GP_CNTRL,
1201e705c121SKalle Valo 					CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
1202e705c121SKalle Valo 			IWL_ERR(trans, "Failed to wake NIC for hcmd\n");
1203e705c121SKalle Valo 			return -EIO;
1204e705c121SKalle Valo 		}
1205e705c121SKalle Valo 		trans_pcie->cmd_hold_nic_awake = true;
1206e705c121SKalle Valo 	}
1207e705c121SKalle Valo 
1208e705c121SKalle Valo 	return 0;
1209e705c121SKalle Valo }
1210e705c121SKalle Valo 
1211e705c121SKalle Valo /*
1212e705c121SKalle Valo  * iwl_pcie_cmdq_reclaim - Reclaim TX command queue entries already Tx'd
1213e705c121SKalle Valo  *
1214e705c121SKalle Valo  * When FW advances 'R' index, all entries between old and new 'R' index
1215e705c121SKalle Valo  * need to be reclaimed. As result, some free space forms.  If there is
1216e705c121SKalle Valo  * enough free space (> low mark), wake the stack that feeds us.
1217e705c121SKalle Valo  */
1218e705c121SKalle Valo static void iwl_pcie_cmdq_reclaim(struct iwl_trans *trans, int txq_id, int idx)
1219e705c121SKalle Valo {
1220e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1221b2a3b1c1SSara Sharon 	struct iwl_txq *txq = trans_pcie->txq[txq_id];
1222e705c121SKalle Valo 	unsigned long flags;
1223e705c121SKalle Valo 	int nfreed = 0;
1224e705c121SKalle Valo 
1225e705c121SKalle Valo 	lockdep_assert_held(&txq->lock);
1226e705c121SKalle Valo 
1227bb98ecd4SSara Sharon 	if ((idx >= TFD_QUEUE_SIZE_MAX) || (!iwl_queue_used(txq, idx))) {
1228e705c121SKalle Valo 		IWL_ERR(trans,
1229e705c121SKalle Valo 			"%s: Read index for DMA queue txq id (%d), index %d is out of range [0-%d] %d %d.\n",
1230e705c121SKalle Valo 			__func__, txq_id, idx, TFD_QUEUE_SIZE_MAX,
1231bb98ecd4SSara Sharon 			txq->write_ptr, txq->read_ptr);
1232e705c121SKalle Valo 		return;
1233e705c121SKalle Valo 	}
1234e705c121SKalle Valo 
1235bb98ecd4SSara Sharon 	for (idx = iwl_queue_inc_wrap(idx); txq->read_ptr != idx;
1236bb98ecd4SSara Sharon 	     txq->read_ptr = iwl_queue_inc_wrap(txq->read_ptr)) {
1237e705c121SKalle Valo 
1238e705c121SKalle Valo 		if (nfreed++ > 0) {
1239e705c121SKalle Valo 			IWL_ERR(trans, "HCMD skipped: index (%d) %d %d\n",
1240bb98ecd4SSara Sharon 				idx, txq->write_ptr, txq->read_ptr);
1241e705c121SKalle Valo 			iwl_force_nmi(trans);
1242e705c121SKalle Valo 		}
1243e705c121SKalle Valo 	}
1244e705c121SKalle Valo 
1245bb98ecd4SSara Sharon 	if (txq->read_ptr == txq->write_ptr) {
1246e705c121SKalle Valo 		spin_lock_irqsave(&trans_pcie->reg_lock, flags);
1247e705c121SKalle Valo 		iwl_pcie_clear_cmd_in_flight(trans);
1248e705c121SKalle Valo 		spin_unlock_irqrestore(&trans_pcie->reg_lock, flags);
1249e705c121SKalle Valo 	}
1250e705c121SKalle Valo 
1251e705c121SKalle Valo 	iwl_pcie_txq_progress(txq);
1252e705c121SKalle Valo }
1253e705c121SKalle Valo 
1254e705c121SKalle Valo static int iwl_pcie_txq_set_ratid_map(struct iwl_trans *trans, u16 ra_tid,
1255e705c121SKalle Valo 				 u16 txq_id)
1256e705c121SKalle Valo {
1257e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1258e705c121SKalle Valo 	u32 tbl_dw_addr;
1259e705c121SKalle Valo 	u32 tbl_dw;
1260e705c121SKalle Valo 	u16 scd_q2ratid;
1261e705c121SKalle Valo 
1262e705c121SKalle Valo 	scd_q2ratid = ra_tid & SCD_QUEUE_RA_TID_MAP_RATID_MSK;
1263e705c121SKalle Valo 
1264e705c121SKalle Valo 	tbl_dw_addr = trans_pcie->scd_base_addr +
1265e705c121SKalle Valo 			SCD_TRANS_TBL_OFFSET_QUEUE(txq_id);
1266e705c121SKalle Valo 
1267e705c121SKalle Valo 	tbl_dw = iwl_trans_read_mem32(trans, tbl_dw_addr);
1268e705c121SKalle Valo 
1269e705c121SKalle Valo 	if (txq_id & 0x1)
1270e705c121SKalle Valo 		tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF);
1271e705c121SKalle Valo 	else
1272e705c121SKalle Valo 		tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000);
1273e705c121SKalle Valo 
1274e705c121SKalle Valo 	iwl_trans_write_mem32(trans, tbl_dw_addr, tbl_dw);
1275e705c121SKalle Valo 
1276e705c121SKalle Valo 	return 0;
1277e705c121SKalle Valo }
1278e705c121SKalle Valo 
1279e705c121SKalle Valo /* Receiver address (actually, Rx station's index into station table),
1280e705c121SKalle Valo  * combined with Traffic ID (QOS priority), in format used by Tx Scheduler */
1281e705c121SKalle Valo #define BUILD_RAxTID(sta_id, tid)	(((sta_id) << 4) + (tid))
1282e705c121SKalle Valo 
1283dcfbd67bSEmmanuel Grumbach bool iwl_trans_pcie_txq_enable(struct iwl_trans *trans, int txq_id, u16 ssn,
1284e705c121SKalle Valo 			       const struct iwl_trans_txq_scd_cfg *cfg,
1285e705c121SKalle Valo 			       unsigned int wdg_timeout)
1286e705c121SKalle Valo {
1287e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1288b2a3b1c1SSara Sharon 	struct iwl_txq *txq = trans_pcie->txq[txq_id];
1289e705c121SKalle Valo 	int fifo = -1;
1290dcfbd67bSEmmanuel Grumbach 	bool scd_bug = false;
1291e705c121SKalle Valo 
1292e705c121SKalle Valo 	if (test_and_set_bit(txq_id, trans_pcie->queue_used))
1293e705c121SKalle Valo 		WARN_ONCE(1, "queue %d already used - expect issues", txq_id);
1294e705c121SKalle Valo 
1295e705c121SKalle Valo 	txq->wd_timeout = msecs_to_jiffies(wdg_timeout);
1296e705c121SKalle Valo 
1297e705c121SKalle Valo 	if (cfg) {
1298e705c121SKalle Valo 		fifo = cfg->fifo;
1299e705c121SKalle Valo 
1300e705c121SKalle Valo 		/* Disable the scheduler prior configuring the cmd queue */
1301e705c121SKalle Valo 		if (txq_id == trans_pcie->cmd_queue &&
1302e705c121SKalle Valo 		    trans_pcie->scd_set_active)
1303e705c121SKalle Valo 			iwl_scd_enable_set_active(trans, 0);
1304e705c121SKalle Valo 
1305e705c121SKalle Valo 		/* Stop this Tx queue before configuring it */
1306e705c121SKalle Valo 		iwl_scd_txq_set_inactive(trans, txq_id);
1307e705c121SKalle Valo 
1308e705c121SKalle Valo 		/* Set this queue as a chain-building queue unless it is CMD */
1309e705c121SKalle Valo 		if (txq_id != trans_pcie->cmd_queue)
1310e705c121SKalle Valo 			iwl_scd_txq_set_chain(trans, txq_id);
1311e705c121SKalle Valo 
1312e705c121SKalle Valo 		if (cfg->aggregate) {
1313e705c121SKalle Valo 			u16 ra_tid = BUILD_RAxTID(cfg->sta_id, cfg->tid);
1314e705c121SKalle Valo 
1315e705c121SKalle Valo 			/* Map receiver-address / traffic-ID to this queue */
1316e705c121SKalle Valo 			iwl_pcie_txq_set_ratid_map(trans, ra_tid, txq_id);
1317e705c121SKalle Valo 
1318e705c121SKalle Valo 			/* enable aggregations for the queue */
1319e705c121SKalle Valo 			iwl_scd_txq_enable_agg(trans, txq_id);
1320e705c121SKalle Valo 			txq->ampdu = true;
1321e705c121SKalle Valo 		} else {
1322e705c121SKalle Valo 			/*
1323e705c121SKalle Valo 			 * disable aggregations for the queue, this will also
1324e705c121SKalle Valo 			 * make the ra_tid mapping configuration irrelevant
1325e705c121SKalle Valo 			 * since it is now a non-AGG queue.
1326e705c121SKalle Valo 			 */
1327e705c121SKalle Valo 			iwl_scd_txq_disable_agg(trans, txq_id);
1328e705c121SKalle Valo 
1329bb98ecd4SSara Sharon 			ssn = txq->read_ptr;
1330e705c121SKalle Valo 		}
1331dcfbd67bSEmmanuel Grumbach 	} else {
1332dcfbd67bSEmmanuel Grumbach 		/*
1333dcfbd67bSEmmanuel Grumbach 		 * If we need to move the SCD write pointer by steps of
1334dcfbd67bSEmmanuel Grumbach 		 * 0x40, 0x80 or 0xc0, it gets stuck. Avoids this and let
1335dcfbd67bSEmmanuel Grumbach 		 * the op_mode know by returning true later.
1336dcfbd67bSEmmanuel Grumbach 		 * Do this only in case cfg is NULL since this trick can
1337dcfbd67bSEmmanuel Grumbach 		 * be done only if we have DQA enabled which is true for mvm
1338dcfbd67bSEmmanuel Grumbach 		 * only. And mvm never sets a cfg pointer.
1339dcfbd67bSEmmanuel Grumbach 		 * This is really ugly, but this is the easiest way out for
1340dcfbd67bSEmmanuel Grumbach 		 * this sad hardware issue.
1341dcfbd67bSEmmanuel Grumbach 		 * This bug has been fixed on devices 9000 and up.
1342dcfbd67bSEmmanuel Grumbach 		 */
1343dcfbd67bSEmmanuel Grumbach 		scd_bug = !trans->cfg->mq_rx_supported &&
1344dcfbd67bSEmmanuel Grumbach 			!((ssn - txq->write_ptr) & 0x3f) &&
1345dcfbd67bSEmmanuel Grumbach 			(ssn != txq->write_ptr);
1346dcfbd67bSEmmanuel Grumbach 		if (scd_bug)
1347dcfbd67bSEmmanuel Grumbach 			ssn++;
1348e705c121SKalle Valo 	}
1349e705c121SKalle Valo 
1350e705c121SKalle Valo 	/* Place first TFD at index corresponding to start sequence number.
1351e705c121SKalle Valo 	 * Assumes that ssn_idx is valid (!= 0xFFF) */
1352bb98ecd4SSara Sharon 	txq->read_ptr = (ssn & 0xff);
1353bb98ecd4SSara Sharon 	txq->write_ptr = (ssn & 0xff);
1354e705c121SKalle Valo 	iwl_write_direct32(trans, HBUS_TARG_WRPTR,
1355e705c121SKalle Valo 			   (ssn & 0xff) | (txq_id << 8));
1356e705c121SKalle Valo 
1357e705c121SKalle Valo 	if (cfg) {
1358e705c121SKalle Valo 		u8 frame_limit = cfg->frame_limit;
1359e705c121SKalle Valo 
1360e705c121SKalle Valo 		iwl_write_prph(trans, SCD_QUEUE_RDPTR(txq_id), ssn);
1361e705c121SKalle Valo 
1362e705c121SKalle Valo 		/* Set up Tx window size and frame limit for this queue */
1363e705c121SKalle Valo 		iwl_trans_write_mem32(trans, trans_pcie->scd_base_addr +
1364e705c121SKalle Valo 				SCD_CONTEXT_QUEUE_OFFSET(txq_id), 0);
1365e705c121SKalle Valo 		iwl_trans_write_mem32(trans,
1366e705c121SKalle Valo 			trans_pcie->scd_base_addr +
1367e705c121SKalle Valo 			SCD_CONTEXT_QUEUE_OFFSET(txq_id) + sizeof(u32),
1368f3779f47SJohannes Berg 			SCD_QUEUE_CTX_REG2_VAL(WIN_SIZE, frame_limit) |
1369f3779f47SJohannes Berg 			SCD_QUEUE_CTX_REG2_VAL(FRAME_LIMIT, frame_limit));
1370e705c121SKalle Valo 
1371e705c121SKalle Valo 		/* Set up status area in SRAM, map to Tx DMA/FIFO, activate */
1372e705c121SKalle Valo 		iwl_write_prph(trans, SCD_QUEUE_STATUS_BITS(txq_id),
1373e705c121SKalle Valo 			       (1 << SCD_QUEUE_STTS_REG_POS_ACTIVE) |
1374e705c121SKalle Valo 			       (cfg->fifo << SCD_QUEUE_STTS_REG_POS_TXF) |
1375e705c121SKalle Valo 			       (1 << SCD_QUEUE_STTS_REG_POS_WSL) |
1376e705c121SKalle Valo 			       SCD_QUEUE_STTS_REG_MSK);
1377e705c121SKalle Valo 
1378e705c121SKalle Valo 		/* enable the scheduler for this queue (only) */
1379e705c121SKalle Valo 		if (txq_id == trans_pcie->cmd_queue &&
1380e705c121SKalle Valo 		    trans_pcie->scd_set_active)
1381e705c121SKalle Valo 			iwl_scd_enable_set_active(trans, BIT(txq_id));
1382e705c121SKalle Valo 
1383e705c121SKalle Valo 		IWL_DEBUG_TX_QUEUES(trans,
1384e705c121SKalle Valo 				    "Activate queue %d on FIFO %d WrPtr: %d\n",
1385e705c121SKalle Valo 				    txq_id, fifo, ssn & 0xff);
1386e705c121SKalle Valo 	} else {
1387e705c121SKalle Valo 		IWL_DEBUG_TX_QUEUES(trans,
1388e705c121SKalle Valo 				    "Activate queue %d WrPtr: %d\n",
1389e705c121SKalle Valo 				    txq_id, ssn & 0xff);
1390e705c121SKalle Valo 	}
1391dcfbd67bSEmmanuel Grumbach 
1392dcfbd67bSEmmanuel Grumbach 	return scd_bug;
1393e705c121SKalle Valo }
1394e705c121SKalle Valo 
139542db09c1SLiad Kaufman void iwl_trans_pcie_txq_set_shared_mode(struct iwl_trans *trans, u32 txq_id,
139642db09c1SLiad Kaufman 					bool shared_mode)
139742db09c1SLiad Kaufman {
139842db09c1SLiad Kaufman 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1399b2a3b1c1SSara Sharon 	struct iwl_txq *txq = trans_pcie->txq[txq_id];
140042db09c1SLiad Kaufman 
140142db09c1SLiad Kaufman 	txq->ampdu = !shared_mode;
140242db09c1SLiad Kaufman }
140342db09c1SLiad Kaufman 
1404e705c121SKalle Valo void iwl_trans_pcie_txq_disable(struct iwl_trans *trans, int txq_id,
1405e705c121SKalle Valo 				bool configure_scd)
1406e705c121SKalle Valo {
1407e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1408e705c121SKalle Valo 	u32 stts_addr = trans_pcie->scd_base_addr +
1409e705c121SKalle Valo 			SCD_TX_STTS_QUEUE_OFFSET(txq_id);
1410e705c121SKalle Valo 	static const u32 zero_val[4] = {};
1411e705c121SKalle Valo 
1412b2a3b1c1SSara Sharon 	trans_pcie->txq[txq_id]->frozen_expiry_remainder = 0;
1413b2a3b1c1SSara Sharon 	trans_pcie->txq[txq_id]->frozen = false;
1414e705c121SKalle Valo 
1415e705c121SKalle Valo 	/*
1416e705c121SKalle Valo 	 * Upon HW Rfkill - we stop the device, and then stop the queues
1417e705c121SKalle Valo 	 * in the op_mode. Just for the sake of the simplicity of the op_mode,
1418e705c121SKalle Valo 	 * allow the op_mode to call txq_disable after it already called
1419e705c121SKalle Valo 	 * stop_device.
1420e705c121SKalle Valo 	 */
1421e705c121SKalle Valo 	if (!test_and_clear_bit(txq_id, trans_pcie->queue_used)) {
1422e705c121SKalle Valo 		WARN_ONCE(test_bit(STATUS_DEVICE_ENABLED, &trans->status),
1423e705c121SKalle Valo 			  "queue %d not used", txq_id);
1424e705c121SKalle Valo 		return;
1425e705c121SKalle Valo 	}
1426e705c121SKalle Valo 
1427e705c121SKalle Valo 	if (configure_scd) {
1428e705c121SKalle Valo 		iwl_scd_txq_set_inactive(trans, txq_id);
1429e705c121SKalle Valo 
1430e705c121SKalle Valo 		iwl_trans_write_mem(trans, stts_addr, (void *)zero_val,
1431e705c121SKalle Valo 				    ARRAY_SIZE(zero_val));
1432e705c121SKalle Valo 	}
1433e705c121SKalle Valo 
1434e705c121SKalle Valo 	iwl_pcie_txq_unmap(trans, txq_id);
1435b2a3b1c1SSara Sharon 	trans_pcie->txq[txq_id]->ampdu = false;
1436e705c121SKalle Valo 
1437e705c121SKalle Valo 	IWL_DEBUG_TX_QUEUES(trans, "Deactivate queue %d\n", txq_id);
1438e705c121SKalle Valo }
1439e705c121SKalle Valo 
1440e705c121SKalle Valo /*************** HOST COMMAND QUEUE FUNCTIONS   *****/
1441e705c121SKalle Valo 
1442e705c121SKalle Valo /*
1443e705c121SKalle Valo  * iwl_pcie_enqueue_hcmd - enqueue a uCode command
1444e705c121SKalle Valo  * @priv: device private data point
1445e705c121SKalle Valo  * @cmd: a pointer to the ucode command structure
1446e705c121SKalle Valo  *
1447e705c121SKalle Valo  * The function returns < 0 values to indicate the operation
1448e705c121SKalle Valo  * failed. On success, it returns the index (>= 0) of command in the
1449e705c121SKalle Valo  * command queue.
1450e705c121SKalle Valo  */
1451e705c121SKalle Valo static int iwl_pcie_enqueue_hcmd(struct iwl_trans *trans,
1452e705c121SKalle Valo 				 struct iwl_host_cmd *cmd)
1453e705c121SKalle Valo {
1454e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1455b2a3b1c1SSara Sharon 	struct iwl_txq *txq = trans_pcie->txq[trans_pcie->cmd_queue];
1456e705c121SKalle Valo 	struct iwl_device_cmd *out_cmd;
1457e705c121SKalle Valo 	struct iwl_cmd_meta *out_meta;
1458e705c121SKalle Valo 	unsigned long flags;
1459e705c121SKalle Valo 	void *dup_buf = NULL;
1460e705c121SKalle Valo 	dma_addr_t phys_addr;
1461e705c121SKalle Valo 	int idx;
14628de437c7SSara Sharon 	u16 copy_size, cmd_size, tb0_size;
1463e705c121SKalle Valo 	bool had_nocopy = false;
1464e705c121SKalle Valo 	u8 group_id = iwl_cmd_groupid(cmd->id);
1465e705c121SKalle Valo 	int i, ret;
1466e705c121SKalle Valo 	u32 cmd_pos;
1467e705c121SKalle Valo 	const u8 *cmddata[IWL_MAX_CMD_TBS_PER_TFD];
1468e705c121SKalle Valo 	u16 cmdlen[IWL_MAX_CMD_TBS_PER_TFD];
1469e705c121SKalle Valo 
14705b88792cSSara Sharon 	if (WARN(!trans->wide_cmd_header &&
1471e705c121SKalle Valo 		 group_id > IWL_ALWAYS_LONG_GROUP,
1472e705c121SKalle Valo 		 "unsupported wide command %#x\n", cmd->id))
1473e705c121SKalle Valo 		return -EINVAL;
1474e705c121SKalle Valo 
1475e705c121SKalle Valo 	if (group_id != 0) {
1476e705c121SKalle Valo 		copy_size = sizeof(struct iwl_cmd_header_wide);
1477e705c121SKalle Valo 		cmd_size = sizeof(struct iwl_cmd_header_wide);
1478e705c121SKalle Valo 	} else {
1479e705c121SKalle Valo 		copy_size = sizeof(struct iwl_cmd_header);
1480e705c121SKalle Valo 		cmd_size = sizeof(struct iwl_cmd_header);
1481e705c121SKalle Valo 	}
1482e705c121SKalle Valo 
1483e705c121SKalle Valo 	/* need one for the header if the first is NOCOPY */
1484e705c121SKalle Valo 	BUILD_BUG_ON(IWL_MAX_CMD_TBS_PER_TFD > IWL_NUM_OF_TBS - 1);
1485e705c121SKalle Valo 
1486e705c121SKalle Valo 	for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
1487e705c121SKalle Valo 		cmddata[i] = cmd->data[i];
1488e705c121SKalle Valo 		cmdlen[i] = cmd->len[i];
1489e705c121SKalle Valo 
1490e705c121SKalle Valo 		if (!cmd->len[i])
1491e705c121SKalle Valo 			continue;
1492e705c121SKalle Valo 
14938de437c7SSara Sharon 		/* need at least IWL_FIRST_TB_SIZE copied */
14948de437c7SSara Sharon 		if (copy_size < IWL_FIRST_TB_SIZE) {
14958de437c7SSara Sharon 			int copy = IWL_FIRST_TB_SIZE - copy_size;
1496e705c121SKalle Valo 
1497e705c121SKalle Valo 			if (copy > cmdlen[i])
1498e705c121SKalle Valo 				copy = cmdlen[i];
1499e705c121SKalle Valo 			cmdlen[i] -= copy;
1500e705c121SKalle Valo 			cmddata[i] += copy;
1501e705c121SKalle Valo 			copy_size += copy;
1502e705c121SKalle Valo 		}
1503e705c121SKalle Valo 
1504e705c121SKalle Valo 		if (cmd->dataflags[i] & IWL_HCMD_DFL_NOCOPY) {
1505e705c121SKalle Valo 			had_nocopy = true;
1506e705c121SKalle Valo 			if (WARN_ON(cmd->dataflags[i] & IWL_HCMD_DFL_DUP)) {
1507e705c121SKalle Valo 				idx = -EINVAL;
1508e705c121SKalle Valo 				goto free_dup_buf;
1509e705c121SKalle Valo 			}
1510e705c121SKalle Valo 		} else if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP) {
1511e705c121SKalle Valo 			/*
1512e705c121SKalle Valo 			 * This is also a chunk that isn't copied
1513e705c121SKalle Valo 			 * to the static buffer so set had_nocopy.
1514e705c121SKalle Valo 			 */
1515e705c121SKalle Valo 			had_nocopy = true;
1516e705c121SKalle Valo 
1517e705c121SKalle Valo 			/* only allowed once */
1518e705c121SKalle Valo 			if (WARN_ON(dup_buf)) {
1519e705c121SKalle Valo 				idx = -EINVAL;
1520e705c121SKalle Valo 				goto free_dup_buf;
1521e705c121SKalle Valo 			}
1522e705c121SKalle Valo 
1523e705c121SKalle Valo 			dup_buf = kmemdup(cmddata[i], cmdlen[i],
1524e705c121SKalle Valo 					  GFP_ATOMIC);
1525e705c121SKalle Valo 			if (!dup_buf)
1526e705c121SKalle Valo 				return -ENOMEM;
1527e705c121SKalle Valo 		} else {
1528e705c121SKalle Valo 			/* NOCOPY must not be followed by normal! */
1529e705c121SKalle Valo 			if (WARN_ON(had_nocopy)) {
1530e705c121SKalle Valo 				idx = -EINVAL;
1531e705c121SKalle Valo 				goto free_dup_buf;
1532e705c121SKalle Valo 			}
1533e705c121SKalle Valo 			copy_size += cmdlen[i];
1534e705c121SKalle Valo 		}
1535e705c121SKalle Valo 		cmd_size += cmd->len[i];
1536e705c121SKalle Valo 	}
1537e705c121SKalle Valo 
1538e705c121SKalle Valo 	/*
1539e705c121SKalle Valo 	 * If any of the command structures end up being larger than
1540e705c121SKalle Valo 	 * the TFD_MAX_PAYLOAD_SIZE and they aren't dynamically
1541e705c121SKalle Valo 	 * allocated into separate TFDs, then we will need to
1542e705c121SKalle Valo 	 * increase the size of the buffers.
1543e705c121SKalle Valo 	 */
1544e705c121SKalle Valo 	if (WARN(copy_size > TFD_MAX_PAYLOAD_SIZE,
1545e705c121SKalle Valo 		 "Command %s (%#x) is too large (%d bytes)\n",
154639bdb17eSSharon Dvir 		 iwl_get_cmd_string(trans, cmd->id),
154739bdb17eSSharon Dvir 		 cmd->id, copy_size)) {
1548e705c121SKalle Valo 		idx = -EINVAL;
1549e705c121SKalle Valo 		goto free_dup_buf;
1550e705c121SKalle Valo 	}
1551e705c121SKalle Valo 
1552e705c121SKalle Valo 	spin_lock_bh(&txq->lock);
1553e705c121SKalle Valo 
1554bb98ecd4SSara Sharon 	if (iwl_queue_space(txq) < ((cmd->flags & CMD_ASYNC) ? 2 : 1)) {
1555e705c121SKalle Valo 		spin_unlock_bh(&txq->lock);
1556e705c121SKalle Valo 
1557e705c121SKalle Valo 		IWL_ERR(trans, "No space in command queue\n");
1558e705c121SKalle Valo 		iwl_op_mode_cmd_queue_full(trans->op_mode);
1559e705c121SKalle Valo 		idx = -ENOSPC;
1560e705c121SKalle Valo 		goto free_dup_buf;
1561e705c121SKalle Valo 	}
1562e705c121SKalle Valo 
1563bb98ecd4SSara Sharon 	idx = get_cmd_index(txq, txq->write_ptr);
1564e705c121SKalle Valo 	out_cmd = txq->entries[idx].cmd;
1565e705c121SKalle Valo 	out_meta = &txq->entries[idx].meta;
1566e705c121SKalle Valo 
1567e705c121SKalle Valo 	memset(out_meta, 0, sizeof(*out_meta));	/* re-initialize to NULL */
1568e705c121SKalle Valo 	if (cmd->flags & CMD_WANT_SKB)
1569e705c121SKalle Valo 		out_meta->source = cmd;
1570e705c121SKalle Valo 
1571e705c121SKalle Valo 	/* set up the header */
1572e705c121SKalle Valo 	if (group_id != 0) {
1573e705c121SKalle Valo 		out_cmd->hdr_wide.cmd = iwl_cmd_opcode(cmd->id);
1574e705c121SKalle Valo 		out_cmd->hdr_wide.group_id = group_id;
1575e705c121SKalle Valo 		out_cmd->hdr_wide.version = iwl_cmd_version(cmd->id);
1576e705c121SKalle Valo 		out_cmd->hdr_wide.length =
1577e705c121SKalle Valo 			cpu_to_le16(cmd_size -
1578e705c121SKalle Valo 				    sizeof(struct iwl_cmd_header_wide));
1579e705c121SKalle Valo 		out_cmd->hdr_wide.reserved = 0;
1580e705c121SKalle Valo 		out_cmd->hdr_wide.sequence =
1581e705c121SKalle Valo 			cpu_to_le16(QUEUE_TO_SEQ(trans_pcie->cmd_queue) |
1582bb98ecd4SSara Sharon 						 INDEX_TO_SEQ(txq->write_ptr));
1583e705c121SKalle Valo 
1584e705c121SKalle Valo 		cmd_pos = sizeof(struct iwl_cmd_header_wide);
1585e705c121SKalle Valo 		copy_size = sizeof(struct iwl_cmd_header_wide);
1586e705c121SKalle Valo 	} else {
1587e705c121SKalle Valo 		out_cmd->hdr.cmd = iwl_cmd_opcode(cmd->id);
1588e705c121SKalle Valo 		out_cmd->hdr.sequence =
1589e705c121SKalle Valo 			cpu_to_le16(QUEUE_TO_SEQ(trans_pcie->cmd_queue) |
1590bb98ecd4SSara Sharon 						 INDEX_TO_SEQ(txq->write_ptr));
1591e705c121SKalle Valo 		out_cmd->hdr.group_id = 0;
1592e705c121SKalle Valo 
1593e705c121SKalle Valo 		cmd_pos = sizeof(struct iwl_cmd_header);
1594e705c121SKalle Valo 		copy_size = sizeof(struct iwl_cmd_header);
1595e705c121SKalle Valo 	}
1596e705c121SKalle Valo 
1597e705c121SKalle Valo 	/* and copy the data that needs to be copied */
1598e705c121SKalle Valo 	for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
1599e705c121SKalle Valo 		int copy;
1600e705c121SKalle Valo 
1601e705c121SKalle Valo 		if (!cmd->len[i])
1602e705c121SKalle Valo 			continue;
1603e705c121SKalle Valo 
1604e705c121SKalle Valo 		/* copy everything if not nocopy/dup */
1605e705c121SKalle Valo 		if (!(cmd->dataflags[i] & (IWL_HCMD_DFL_NOCOPY |
1606e705c121SKalle Valo 					   IWL_HCMD_DFL_DUP))) {
1607e705c121SKalle Valo 			copy = cmd->len[i];
1608e705c121SKalle Valo 
1609e705c121SKalle Valo 			memcpy((u8 *)out_cmd + cmd_pos, cmd->data[i], copy);
1610e705c121SKalle Valo 			cmd_pos += copy;
1611e705c121SKalle Valo 			copy_size += copy;
1612e705c121SKalle Valo 			continue;
1613e705c121SKalle Valo 		}
1614e705c121SKalle Valo 
1615e705c121SKalle Valo 		/*
16168de437c7SSara Sharon 		 * Otherwise we need at least IWL_FIRST_TB_SIZE copied
16178de437c7SSara Sharon 		 * in total (for bi-directional DMA), but copy up to what
1618e705c121SKalle Valo 		 * we can fit into the payload for debug dump purposes.
1619e705c121SKalle Valo 		 */
1620e705c121SKalle Valo 		copy = min_t(int, TFD_MAX_PAYLOAD_SIZE - cmd_pos, cmd->len[i]);
1621e705c121SKalle Valo 
1622e705c121SKalle Valo 		memcpy((u8 *)out_cmd + cmd_pos, cmd->data[i], copy);
1623e705c121SKalle Valo 		cmd_pos += copy;
1624e705c121SKalle Valo 
1625e705c121SKalle Valo 		/* However, treat copy_size the proper way, we need it below */
16268de437c7SSara Sharon 		if (copy_size < IWL_FIRST_TB_SIZE) {
16278de437c7SSara Sharon 			copy = IWL_FIRST_TB_SIZE - copy_size;
1628e705c121SKalle Valo 
1629e705c121SKalle Valo 			if (copy > cmd->len[i])
1630e705c121SKalle Valo 				copy = cmd->len[i];
1631e705c121SKalle Valo 			copy_size += copy;
1632e705c121SKalle Valo 		}
1633e705c121SKalle Valo 	}
1634e705c121SKalle Valo 
1635e705c121SKalle Valo 	IWL_DEBUG_HC(trans,
1636e705c121SKalle Valo 		     "Sending command %s (%.2x.%.2x), seq: 0x%04X, %d bytes at %d[%d]:%d\n",
163739bdb17eSSharon Dvir 		     iwl_get_cmd_string(trans, cmd->id),
1638e705c121SKalle Valo 		     group_id, out_cmd->hdr.cmd,
1639e705c121SKalle Valo 		     le16_to_cpu(out_cmd->hdr.sequence),
1640bb98ecd4SSara Sharon 		     cmd_size, txq->write_ptr, idx, trans_pcie->cmd_queue);
1641e705c121SKalle Valo 
16428de437c7SSara Sharon 	/* start the TFD with the minimum copy bytes */
16438de437c7SSara Sharon 	tb0_size = min_t(int, copy_size, IWL_FIRST_TB_SIZE);
16448de437c7SSara Sharon 	memcpy(&txq->first_tb_bufs[idx], &out_cmd->hdr, tb0_size);
1645e705c121SKalle Valo 	iwl_pcie_txq_build_tfd(trans, txq,
16468de437c7SSara Sharon 			       iwl_pcie_get_first_tb_dma(txq, idx),
16478de437c7SSara Sharon 			       tb0_size, true);
1648e705c121SKalle Valo 
1649e705c121SKalle Valo 	/* map first command fragment, if any remains */
16508de437c7SSara Sharon 	if (copy_size > tb0_size) {
1651e705c121SKalle Valo 		phys_addr = dma_map_single(trans->dev,
16528de437c7SSara Sharon 					   ((u8 *)&out_cmd->hdr) + tb0_size,
16538de437c7SSara Sharon 					   copy_size - tb0_size,
1654e705c121SKalle Valo 					   DMA_TO_DEVICE);
1655e705c121SKalle Valo 		if (dma_mapping_error(trans->dev, phys_addr)) {
1656bb98ecd4SSara Sharon 			iwl_pcie_tfd_unmap(trans, out_meta, txq,
1657bb98ecd4SSara Sharon 					   txq->write_ptr);
1658e705c121SKalle Valo 			idx = -ENOMEM;
1659e705c121SKalle Valo 			goto out;
1660e705c121SKalle Valo 		}
1661e705c121SKalle Valo 
1662e705c121SKalle Valo 		iwl_pcie_txq_build_tfd(trans, txq, phys_addr,
16638de437c7SSara Sharon 				       copy_size - tb0_size, false);
1664e705c121SKalle Valo 	}
1665e705c121SKalle Valo 
1666e705c121SKalle Valo 	/* map the remaining (adjusted) nocopy/dup fragments */
1667e705c121SKalle Valo 	for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
1668e705c121SKalle Valo 		const void *data = cmddata[i];
1669e705c121SKalle Valo 
1670e705c121SKalle Valo 		if (!cmdlen[i])
1671e705c121SKalle Valo 			continue;
1672e705c121SKalle Valo 		if (!(cmd->dataflags[i] & (IWL_HCMD_DFL_NOCOPY |
1673e705c121SKalle Valo 					   IWL_HCMD_DFL_DUP)))
1674e705c121SKalle Valo 			continue;
1675e705c121SKalle Valo 		if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP)
1676e705c121SKalle Valo 			data = dup_buf;
1677e705c121SKalle Valo 		phys_addr = dma_map_single(trans->dev, (void *)data,
1678e705c121SKalle Valo 					   cmdlen[i], DMA_TO_DEVICE);
1679e705c121SKalle Valo 		if (dma_mapping_error(trans->dev, phys_addr)) {
1680bb98ecd4SSara Sharon 			iwl_pcie_tfd_unmap(trans, out_meta, txq,
1681bb98ecd4SSara Sharon 					   txq->write_ptr);
1682e705c121SKalle Valo 			idx = -ENOMEM;
1683e705c121SKalle Valo 			goto out;
1684e705c121SKalle Valo 		}
1685e705c121SKalle Valo 
1686e705c121SKalle Valo 		iwl_pcie_txq_build_tfd(trans, txq, phys_addr, cmdlen[i], false);
1687e705c121SKalle Valo 	}
1688e705c121SKalle Valo 
16893cd1980bSSara Sharon 	BUILD_BUG_ON(IWL_TFH_NUM_TBS > sizeof(out_meta->tbs) * BITS_PER_BYTE);
1690e705c121SKalle Valo 	out_meta->flags = cmd->flags;
1691e705c121SKalle Valo 	if (WARN_ON_ONCE(txq->entries[idx].free_buf))
1692e705c121SKalle Valo 		kzfree(txq->entries[idx].free_buf);
1693e705c121SKalle Valo 	txq->entries[idx].free_buf = dup_buf;
1694e705c121SKalle Valo 
1695e705c121SKalle Valo 	trace_iwlwifi_dev_hcmd(trans->dev, cmd, cmd_size, &out_cmd->hdr_wide);
1696e705c121SKalle Valo 
1697e705c121SKalle Valo 	/* start timer if queue currently empty */
1698bb98ecd4SSara Sharon 	if (txq->read_ptr == txq->write_ptr && txq->wd_timeout)
1699e705c121SKalle Valo 		mod_timer(&txq->stuck_timer, jiffies + txq->wd_timeout);
1700e705c121SKalle Valo 
1701e705c121SKalle Valo 	spin_lock_irqsave(&trans_pcie->reg_lock, flags);
1702e705c121SKalle Valo 	ret = iwl_pcie_set_cmd_in_flight(trans, cmd);
1703e705c121SKalle Valo 	if (ret < 0) {
1704e705c121SKalle Valo 		idx = ret;
1705e705c121SKalle Valo 		spin_unlock_irqrestore(&trans_pcie->reg_lock, flags);
1706e705c121SKalle Valo 		goto out;
1707e705c121SKalle Valo 	}
1708e705c121SKalle Valo 
1709e705c121SKalle Valo 	/* Increment and update queue's write index */
1710bb98ecd4SSara Sharon 	txq->write_ptr = iwl_queue_inc_wrap(txq->write_ptr);
1711e705c121SKalle Valo 	iwl_pcie_txq_inc_wr_ptr(trans, txq);
1712e705c121SKalle Valo 
1713e705c121SKalle Valo 	spin_unlock_irqrestore(&trans_pcie->reg_lock, flags);
1714e705c121SKalle Valo 
1715e705c121SKalle Valo  out:
1716e705c121SKalle Valo 	spin_unlock_bh(&txq->lock);
1717e705c121SKalle Valo  free_dup_buf:
1718e705c121SKalle Valo 	if (idx < 0)
1719e705c121SKalle Valo 		kfree(dup_buf);
1720e705c121SKalle Valo 	return idx;
1721e705c121SKalle Valo }
1722e705c121SKalle Valo 
1723e705c121SKalle Valo /*
1724e705c121SKalle Valo  * iwl_pcie_hcmd_complete - Pull unused buffers off the queue and reclaim them
1725e705c121SKalle Valo  * @rxb: Rx buffer to reclaim
1726e705c121SKalle Valo  */
1727e705c121SKalle Valo void iwl_pcie_hcmd_complete(struct iwl_trans *trans,
1728e705c121SKalle Valo 			    struct iwl_rx_cmd_buffer *rxb)
1729e705c121SKalle Valo {
1730e705c121SKalle Valo 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1731e705c121SKalle Valo 	u16 sequence = le16_to_cpu(pkt->hdr.sequence);
1732d490e097SJohannes Berg 	u8 group_id;
173339bdb17eSSharon Dvir 	u32 cmd_id;
1734e705c121SKalle Valo 	int txq_id = SEQ_TO_QUEUE(sequence);
1735e705c121SKalle Valo 	int index = SEQ_TO_INDEX(sequence);
1736e705c121SKalle Valo 	int cmd_index;
1737e705c121SKalle Valo 	struct iwl_device_cmd *cmd;
1738e705c121SKalle Valo 	struct iwl_cmd_meta *meta;
1739e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1740b2a3b1c1SSara Sharon 	struct iwl_txq *txq = trans_pcie->txq[trans_pcie->cmd_queue];
1741e705c121SKalle Valo 
1742e705c121SKalle Valo 	/* If a Tx command is being handled and it isn't in the actual
1743e705c121SKalle Valo 	 * command queue then there a command routing bug has been introduced
1744e705c121SKalle Valo 	 * in the queue management code. */
1745e705c121SKalle Valo 	if (WARN(txq_id != trans_pcie->cmd_queue,
1746e705c121SKalle Valo 		 "wrong command queue %d (should be %d), sequence 0x%X readp=%d writep=%d\n",
1747b2a3b1c1SSara Sharon 		 txq_id, trans_pcie->cmd_queue, sequence, txq->read_ptr,
1748b2a3b1c1SSara Sharon 		 txq->write_ptr)) {
1749e705c121SKalle Valo 		iwl_print_hex_error(trans, pkt, 32);
1750e705c121SKalle Valo 		return;
1751e705c121SKalle Valo 	}
1752e705c121SKalle Valo 
1753e705c121SKalle Valo 	spin_lock_bh(&txq->lock);
1754e705c121SKalle Valo 
1755bb98ecd4SSara Sharon 	cmd_index = get_cmd_index(txq, index);
1756e705c121SKalle Valo 	cmd = txq->entries[cmd_index].cmd;
1757e705c121SKalle Valo 	meta = &txq->entries[cmd_index].meta;
1758d490e097SJohannes Berg 	group_id = cmd->hdr.group_id;
175939bdb17eSSharon Dvir 	cmd_id = iwl_cmd_id(cmd->hdr.cmd, group_id, 0);
1760e705c121SKalle Valo 
17616983ba69SSara Sharon 	iwl_pcie_tfd_unmap(trans, meta, txq, index);
1762e705c121SKalle Valo 
1763e705c121SKalle Valo 	/* Input error checking is done when commands are added to queue. */
1764e705c121SKalle Valo 	if (meta->flags & CMD_WANT_SKB) {
1765e705c121SKalle Valo 		struct page *p = rxb_steal_page(rxb);
1766e705c121SKalle Valo 
1767e705c121SKalle Valo 		meta->source->resp_pkt = pkt;
1768e705c121SKalle Valo 		meta->source->_rx_page_addr = (unsigned long)page_address(p);
1769e705c121SKalle Valo 		meta->source->_rx_page_order = trans_pcie->rx_page_order;
1770e705c121SKalle Valo 	}
1771e705c121SKalle Valo 
1772dcbb4746SEmmanuel Grumbach 	if (meta->flags & CMD_WANT_ASYNC_CALLBACK)
1773dcbb4746SEmmanuel Grumbach 		iwl_op_mode_async_cb(trans->op_mode, cmd);
1774dcbb4746SEmmanuel Grumbach 
1775e705c121SKalle Valo 	iwl_pcie_cmdq_reclaim(trans, txq_id, index);
1776e705c121SKalle Valo 
1777e705c121SKalle Valo 	if (!(meta->flags & CMD_ASYNC)) {
1778e705c121SKalle Valo 		if (!test_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status)) {
1779e705c121SKalle Valo 			IWL_WARN(trans,
1780e705c121SKalle Valo 				 "HCMD_ACTIVE already clear for command %s\n",
178139bdb17eSSharon Dvir 				 iwl_get_cmd_string(trans, cmd_id));
1782e705c121SKalle Valo 		}
1783e705c121SKalle Valo 		clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
1784e705c121SKalle Valo 		IWL_DEBUG_INFO(trans, "Clearing HCMD_ACTIVE for command %s\n",
178539bdb17eSSharon Dvir 			       iwl_get_cmd_string(trans, cmd_id));
1786e705c121SKalle Valo 		wake_up(&trans_pcie->wait_command_queue);
1787e705c121SKalle Valo 	}
1788e705c121SKalle Valo 
17894cbb8e50SLuciano Coelho 	if (meta->flags & CMD_MAKE_TRANS_IDLE) {
17904cbb8e50SLuciano Coelho 		IWL_DEBUG_INFO(trans, "complete %s - mark trans as idle\n",
17914cbb8e50SLuciano Coelho 			       iwl_get_cmd_string(trans, cmd->hdr.cmd));
17924cbb8e50SLuciano Coelho 		set_bit(STATUS_TRANS_IDLE, &trans->status);
17934cbb8e50SLuciano Coelho 		wake_up(&trans_pcie->d0i3_waitq);
17944cbb8e50SLuciano Coelho 	}
17954cbb8e50SLuciano Coelho 
17964cbb8e50SLuciano Coelho 	if (meta->flags & CMD_WAKE_UP_TRANS) {
17974cbb8e50SLuciano Coelho 		IWL_DEBUG_INFO(trans, "complete %s - clear trans idle flag\n",
17984cbb8e50SLuciano Coelho 			       iwl_get_cmd_string(trans, cmd->hdr.cmd));
17994cbb8e50SLuciano Coelho 		clear_bit(STATUS_TRANS_IDLE, &trans->status);
18004cbb8e50SLuciano Coelho 		wake_up(&trans_pcie->d0i3_waitq);
18014cbb8e50SLuciano Coelho 	}
18024cbb8e50SLuciano Coelho 
1803e705c121SKalle Valo 	meta->flags = 0;
1804e705c121SKalle Valo 
1805e705c121SKalle Valo 	spin_unlock_bh(&txq->lock);
1806e705c121SKalle Valo }
1807e705c121SKalle Valo 
1808e705c121SKalle Valo #define HOST_COMPLETE_TIMEOUT	(2 * HZ)
1809e705c121SKalle Valo 
1810e705c121SKalle Valo static int iwl_pcie_send_hcmd_async(struct iwl_trans *trans,
1811e705c121SKalle Valo 				    struct iwl_host_cmd *cmd)
1812e705c121SKalle Valo {
1813e705c121SKalle Valo 	int ret;
1814e705c121SKalle Valo 
1815e705c121SKalle Valo 	/* An asynchronous command can not expect an SKB to be set. */
1816e705c121SKalle Valo 	if (WARN_ON(cmd->flags & CMD_WANT_SKB))
1817e705c121SKalle Valo 		return -EINVAL;
1818e705c121SKalle Valo 
1819e705c121SKalle Valo 	ret = iwl_pcie_enqueue_hcmd(trans, cmd);
1820e705c121SKalle Valo 	if (ret < 0) {
1821e705c121SKalle Valo 		IWL_ERR(trans,
1822e705c121SKalle Valo 			"Error sending %s: enqueue_hcmd failed: %d\n",
182339bdb17eSSharon Dvir 			iwl_get_cmd_string(trans, cmd->id), ret);
1824e705c121SKalle Valo 		return ret;
1825e705c121SKalle Valo 	}
1826e705c121SKalle Valo 	return 0;
1827e705c121SKalle Valo }
1828e705c121SKalle Valo 
1829e705c121SKalle Valo static int iwl_pcie_send_hcmd_sync(struct iwl_trans *trans,
1830e705c121SKalle Valo 				   struct iwl_host_cmd *cmd)
1831e705c121SKalle Valo {
1832e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1833b2a3b1c1SSara Sharon 	struct iwl_txq *txq = trans_pcie->txq[trans_pcie->cmd_queue];
1834e705c121SKalle Valo 	int cmd_idx;
1835e705c121SKalle Valo 	int ret;
1836e705c121SKalle Valo 
1837e705c121SKalle Valo 	IWL_DEBUG_INFO(trans, "Attempting to send sync command %s\n",
183839bdb17eSSharon Dvir 		       iwl_get_cmd_string(trans, cmd->id));
1839e705c121SKalle Valo 
1840e705c121SKalle Valo 	if (WARN(test_and_set_bit(STATUS_SYNC_HCMD_ACTIVE,
1841e705c121SKalle Valo 				  &trans->status),
1842e705c121SKalle Valo 		 "Command %s: a command is already active!\n",
184339bdb17eSSharon Dvir 		 iwl_get_cmd_string(trans, cmd->id)))
1844e705c121SKalle Valo 		return -EIO;
1845e705c121SKalle Valo 
1846e705c121SKalle Valo 	IWL_DEBUG_INFO(trans, "Setting HCMD_ACTIVE for command %s\n",
184739bdb17eSSharon Dvir 		       iwl_get_cmd_string(trans, cmd->id));
1848e705c121SKalle Valo 
184971b1230cSLuca Coelho 	if (pm_runtime_suspended(&trans_pcie->pci_dev->dev)) {
185071b1230cSLuca Coelho 		ret = wait_event_timeout(trans_pcie->d0i3_waitq,
185171b1230cSLuca Coelho 				 pm_runtime_active(&trans_pcie->pci_dev->dev),
185271b1230cSLuca Coelho 				 msecs_to_jiffies(IWL_TRANS_IDLE_TIMEOUT));
185371b1230cSLuca Coelho 		if (!ret) {
185471b1230cSLuca Coelho 			IWL_ERR(trans, "Timeout exiting D0i3 before hcmd\n");
185571b1230cSLuca Coelho 			return -ETIMEDOUT;
185671b1230cSLuca Coelho 		}
185771b1230cSLuca Coelho 	}
185871b1230cSLuca Coelho 
1859e705c121SKalle Valo 	cmd_idx = iwl_pcie_enqueue_hcmd(trans, cmd);
1860e705c121SKalle Valo 	if (cmd_idx < 0) {
1861e705c121SKalle Valo 		ret = cmd_idx;
1862e705c121SKalle Valo 		clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
1863e705c121SKalle Valo 		IWL_ERR(trans,
1864e705c121SKalle Valo 			"Error sending %s: enqueue_hcmd failed: %d\n",
186539bdb17eSSharon Dvir 			iwl_get_cmd_string(trans, cmd->id), ret);
1866e705c121SKalle Valo 		return ret;
1867e705c121SKalle Valo 	}
1868e705c121SKalle Valo 
1869e705c121SKalle Valo 	ret = wait_event_timeout(trans_pcie->wait_command_queue,
1870e705c121SKalle Valo 				 !test_bit(STATUS_SYNC_HCMD_ACTIVE,
1871e705c121SKalle Valo 					   &trans->status),
1872e705c121SKalle Valo 				 HOST_COMPLETE_TIMEOUT);
1873e705c121SKalle Valo 	if (!ret) {
1874e705c121SKalle Valo 		IWL_ERR(trans, "Error sending %s: time out after %dms.\n",
187539bdb17eSSharon Dvir 			iwl_get_cmd_string(trans, cmd->id),
1876e705c121SKalle Valo 			jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
1877e705c121SKalle Valo 
1878e705c121SKalle Valo 		IWL_ERR(trans, "Current CMD queue read_ptr %d write_ptr %d\n",
1879bb98ecd4SSara Sharon 			txq->read_ptr, txq->write_ptr);
1880e705c121SKalle Valo 
1881e705c121SKalle Valo 		clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
1882e705c121SKalle Valo 		IWL_DEBUG_INFO(trans, "Clearing HCMD_ACTIVE for command %s\n",
188339bdb17eSSharon Dvir 			       iwl_get_cmd_string(trans, cmd->id));
1884e705c121SKalle Valo 		ret = -ETIMEDOUT;
1885e705c121SKalle Valo 
1886e705c121SKalle Valo 		iwl_force_nmi(trans);
1887e705c121SKalle Valo 		iwl_trans_fw_error(trans);
1888e705c121SKalle Valo 
1889e705c121SKalle Valo 		goto cancel;
1890e705c121SKalle Valo 	}
1891e705c121SKalle Valo 
1892e705c121SKalle Valo 	if (test_bit(STATUS_FW_ERROR, &trans->status)) {
1893e705c121SKalle Valo 		IWL_ERR(trans, "FW error in SYNC CMD %s\n",
189439bdb17eSSharon Dvir 			iwl_get_cmd_string(trans, cmd->id));
1895e705c121SKalle Valo 		dump_stack();
1896e705c121SKalle Valo 		ret = -EIO;
1897e705c121SKalle Valo 		goto cancel;
1898e705c121SKalle Valo 	}
1899e705c121SKalle Valo 
1900e705c121SKalle Valo 	if (!(cmd->flags & CMD_SEND_IN_RFKILL) &&
1901326477e4SJohannes Berg 	    test_bit(STATUS_RFKILL_OPMODE, &trans->status)) {
1902e705c121SKalle Valo 		IWL_DEBUG_RF_KILL(trans, "RFKILL in SYNC CMD... no rsp\n");
1903e705c121SKalle Valo 		ret = -ERFKILL;
1904e705c121SKalle Valo 		goto cancel;
1905e705c121SKalle Valo 	}
1906e705c121SKalle Valo 
1907e705c121SKalle Valo 	if ((cmd->flags & CMD_WANT_SKB) && !cmd->resp_pkt) {
1908e705c121SKalle Valo 		IWL_ERR(trans, "Error: Response NULL in '%s'\n",
190939bdb17eSSharon Dvir 			iwl_get_cmd_string(trans, cmd->id));
1910e705c121SKalle Valo 		ret = -EIO;
1911e705c121SKalle Valo 		goto cancel;
1912e705c121SKalle Valo 	}
1913e705c121SKalle Valo 
1914e705c121SKalle Valo 	return 0;
1915e705c121SKalle Valo 
1916e705c121SKalle Valo cancel:
1917e705c121SKalle Valo 	if (cmd->flags & CMD_WANT_SKB) {
1918e705c121SKalle Valo 		/*
1919e705c121SKalle Valo 		 * Cancel the CMD_WANT_SKB flag for the cmd in the
1920e705c121SKalle Valo 		 * TX cmd queue. Otherwise in case the cmd comes
1921e705c121SKalle Valo 		 * in later, it will possibly set an invalid
1922e705c121SKalle Valo 		 * address (cmd->meta.source).
1923e705c121SKalle Valo 		 */
1924b2a3b1c1SSara Sharon 		txq->entries[cmd_idx].meta.flags &= ~CMD_WANT_SKB;
1925e705c121SKalle Valo 	}
1926e705c121SKalle Valo 
1927e705c121SKalle Valo 	if (cmd->resp_pkt) {
1928e705c121SKalle Valo 		iwl_free_resp(cmd);
1929e705c121SKalle Valo 		cmd->resp_pkt = NULL;
1930e705c121SKalle Valo 	}
1931e705c121SKalle Valo 
1932e705c121SKalle Valo 	return ret;
1933e705c121SKalle Valo }
1934e705c121SKalle Valo 
1935e705c121SKalle Valo int iwl_trans_pcie_send_hcmd(struct iwl_trans *trans, struct iwl_host_cmd *cmd)
1936e705c121SKalle Valo {
1937e705c121SKalle Valo 	if (!(cmd->flags & CMD_SEND_IN_RFKILL) &&
1938326477e4SJohannes Berg 	    test_bit(STATUS_RFKILL_OPMODE, &trans->status)) {
1939e705c121SKalle Valo 		IWL_DEBUG_RF_KILL(trans, "Dropping CMD 0x%x: RF KILL\n",
1940e705c121SKalle Valo 				  cmd->id);
1941e705c121SKalle Valo 		return -ERFKILL;
1942e705c121SKalle Valo 	}
1943e705c121SKalle Valo 
1944e705c121SKalle Valo 	if (cmd->flags & CMD_ASYNC)
1945e705c121SKalle Valo 		return iwl_pcie_send_hcmd_async(trans, cmd);
1946e705c121SKalle Valo 
1947e705c121SKalle Valo 	/* We still can fail on RFKILL that can be asserted while we wait */
1948e705c121SKalle Valo 	return iwl_pcie_send_hcmd_sync(trans, cmd);
1949e705c121SKalle Valo }
1950e705c121SKalle Valo 
19513a0b2a42SEmmanuel Grumbach static int iwl_fill_data_tbs(struct iwl_trans *trans, struct sk_buff *skb,
19523a0b2a42SEmmanuel Grumbach 			     struct iwl_txq *txq, u8 hdr_len,
19533a0b2a42SEmmanuel Grumbach 			     struct iwl_cmd_meta *out_meta,
19543a0b2a42SEmmanuel Grumbach 			     struct iwl_device_cmd *dev_cmd, u16 tb1_len)
19553a0b2a42SEmmanuel Grumbach {
19566983ba69SSara Sharon 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
19573a0b2a42SEmmanuel Grumbach 	u16 tb2_len;
19583a0b2a42SEmmanuel Grumbach 	int i;
19593a0b2a42SEmmanuel Grumbach 
19603a0b2a42SEmmanuel Grumbach 	/*
19613a0b2a42SEmmanuel Grumbach 	 * Set up TFD's third entry to point directly to remainder
19623a0b2a42SEmmanuel Grumbach 	 * of skb's head, if any
19633a0b2a42SEmmanuel Grumbach 	 */
19643a0b2a42SEmmanuel Grumbach 	tb2_len = skb_headlen(skb) - hdr_len;
19653a0b2a42SEmmanuel Grumbach 
19663a0b2a42SEmmanuel Grumbach 	if (tb2_len > 0) {
19673a0b2a42SEmmanuel Grumbach 		dma_addr_t tb2_phys = dma_map_single(trans->dev,
19683a0b2a42SEmmanuel Grumbach 						     skb->data + hdr_len,
19693a0b2a42SEmmanuel Grumbach 						     tb2_len, DMA_TO_DEVICE);
19703a0b2a42SEmmanuel Grumbach 		if (unlikely(dma_mapping_error(trans->dev, tb2_phys))) {
1971bb98ecd4SSara Sharon 			iwl_pcie_tfd_unmap(trans, out_meta, txq,
1972bb98ecd4SSara Sharon 					   txq->write_ptr);
19733a0b2a42SEmmanuel Grumbach 			return -EINVAL;
19743a0b2a42SEmmanuel Grumbach 		}
19753a0b2a42SEmmanuel Grumbach 		iwl_pcie_txq_build_tfd(trans, txq, tb2_phys, tb2_len, false);
19763a0b2a42SEmmanuel Grumbach 	}
19773a0b2a42SEmmanuel Grumbach 
19783a0b2a42SEmmanuel Grumbach 	/* set up the remaining entries to point to the data */
19793a0b2a42SEmmanuel Grumbach 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
19803a0b2a42SEmmanuel Grumbach 		const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
19813a0b2a42SEmmanuel Grumbach 		dma_addr_t tb_phys;
19823a0b2a42SEmmanuel Grumbach 		int tb_idx;
19833a0b2a42SEmmanuel Grumbach 
19843a0b2a42SEmmanuel Grumbach 		if (!skb_frag_size(frag))
19853a0b2a42SEmmanuel Grumbach 			continue;
19863a0b2a42SEmmanuel Grumbach 
19873a0b2a42SEmmanuel Grumbach 		tb_phys = skb_frag_dma_map(trans->dev, frag, 0,
19883a0b2a42SEmmanuel Grumbach 					   skb_frag_size(frag), DMA_TO_DEVICE);
19893a0b2a42SEmmanuel Grumbach 
19903a0b2a42SEmmanuel Grumbach 		if (unlikely(dma_mapping_error(trans->dev, tb_phys))) {
1991bb98ecd4SSara Sharon 			iwl_pcie_tfd_unmap(trans, out_meta, txq,
1992bb98ecd4SSara Sharon 					   txq->write_ptr);
19933a0b2a42SEmmanuel Grumbach 			return -EINVAL;
19943a0b2a42SEmmanuel Grumbach 		}
19953a0b2a42SEmmanuel Grumbach 		tb_idx = iwl_pcie_txq_build_tfd(trans, txq, tb_phys,
19963a0b2a42SEmmanuel Grumbach 						skb_frag_size(frag), false);
19973a0b2a42SEmmanuel Grumbach 
19983cd1980bSSara Sharon 		out_meta->tbs |= BIT(tb_idx);
19993a0b2a42SEmmanuel Grumbach 	}
20003a0b2a42SEmmanuel Grumbach 
20013a0b2a42SEmmanuel Grumbach 	trace_iwlwifi_dev_tx(trans->dev, skb,
2002bb98ecd4SSara Sharon 			     iwl_pcie_get_tfd(trans_pcie, txq, txq->write_ptr),
20036983ba69SSara Sharon 			     trans_pcie->tfd_size,
20048de437c7SSara Sharon 			     &dev_cmd->hdr, IWL_FIRST_TB_SIZE + tb1_len,
20058790fce4SJohannes Berg 			     hdr_len);
200678c1acf3SJohannes Berg 	trace_iwlwifi_dev_tx_data(trans->dev, skb, hdr_len);
20073a0b2a42SEmmanuel Grumbach 	return 0;
20083a0b2a42SEmmanuel Grumbach }
20093a0b2a42SEmmanuel Grumbach 
20106eb5e529SEmmanuel Grumbach #ifdef CONFIG_INET
20116ffe5de3SSara Sharon struct iwl_tso_hdr_page *get_page_hdr(struct iwl_trans *trans, size_t len)
20126eb5e529SEmmanuel Grumbach {
20136eb5e529SEmmanuel Grumbach 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
20146eb5e529SEmmanuel Grumbach 	struct iwl_tso_hdr_page *p = this_cpu_ptr(trans_pcie->tso_hdr_page);
20156eb5e529SEmmanuel Grumbach 
20166eb5e529SEmmanuel Grumbach 	if (!p->page)
20176eb5e529SEmmanuel Grumbach 		goto alloc;
20186eb5e529SEmmanuel Grumbach 
20196eb5e529SEmmanuel Grumbach 	/* enough room on this page */
20206eb5e529SEmmanuel Grumbach 	if (p->pos + len < (u8 *)page_address(p->page) + PAGE_SIZE)
20216eb5e529SEmmanuel Grumbach 		return p;
20226eb5e529SEmmanuel Grumbach 
20236eb5e529SEmmanuel Grumbach 	/* We don't have enough room on this page, get a new one. */
20246eb5e529SEmmanuel Grumbach 	__free_page(p->page);
20256eb5e529SEmmanuel Grumbach 
20266eb5e529SEmmanuel Grumbach alloc:
20276eb5e529SEmmanuel Grumbach 	p->page = alloc_page(GFP_ATOMIC);
20286eb5e529SEmmanuel Grumbach 	if (!p->page)
20296eb5e529SEmmanuel Grumbach 		return NULL;
20306eb5e529SEmmanuel Grumbach 	p->pos = page_address(p->page);
20316eb5e529SEmmanuel Grumbach 	return p;
20326eb5e529SEmmanuel Grumbach }
20336eb5e529SEmmanuel Grumbach 
20346eb5e529SEmmanuel Grumbach static void iwl_compute_pseudo_hdr_csum(void *iph, struct tcphdr *tcph,
20356eb5e529SEmmanuel Grumbach 					bool ipv6, unsigned int len)
20366eb5e529SEmmanuel Grumbach {
20376eb5e529SEmmanuel Grumbach 	if (ipv6) {
20386eb5e529SEmmanuel Grumbach 		struct ipv6hdr *iphv6 = iph;
20396eb5e529SEmmanuel Grumbach 
20406eb5e529SEmmanuel Grumbach 		tcph->check = ~csum_ipv6_magic(&iphv6->saddr, &iphv6->daddr,
20416eb5e529SEmmanuel Grumbach 					       len + tcph->doff * 4,
20426eb5e529SEmmanuel Grumbach 					       IPPROTO_TCP, 0);
20436eb5e529SEmmanuel Grumbach 	} else {
20446eb5e529SEmmanuel Grumbach 		struct iphdr *iphv4 = iph;
20456eb5e529SEmmanuel Grumbach 
20466eb5e529SEmmanuel Grumbach 		ip_send_check(iphv4);
20476eb5e529SEmmanuel Grumbach 		tcph->check = ~csum_tcpudp_magic(iphv4->saddr, iphv4->daddr,
20486eb5e529SEmmanuel Grumbach 						 len + tcph->doff * 4,
20496eb5e529SEmmanuel Grumbach 						 IPPROTO_TCP, 0);
20506eb5e529SEmmanuel Grumbach 	}
20516eb5e529SEmmanuel Grumbach }
20526eb5e529SEmmanuel Grumbach 
2053066fd29aSSara Sharon static int iwl_fill_data_tbs_amsdu(struct iwl_trans *trans, struct sk_buff *skb,
20546eb5e529SEmmanuel Grumbach 				   struct iwl_txq *txq, u8 hdr_len,
20556eb5e529SEmmanuel Grumbach 				   struct iwl_cmd_meta *out_meta,
20566eb5e529SEmmanuel Grumbach 				   struct iwl_device_cmd *dev_cmd, u16 tb1_len)
20576eb5e529SEmmanuel Grumbach {
205805e5a7e5SJohannes Berg 	struct iwl_tx_cmd *tx_cmd = (void *)dev_cmd->payload;
20596eb5e529SEmmanuel Grumbach 	struct iwl_trans_pcie *trans_pcie = txq->trans_pcie;
20606eb5e529SEmmanuel Grumbach 	struct ieee80211_hdr *hdr = (void *)skb->data;
20616eb5e529SEmmanuel Grumbach 	unsigned int snap_ip_tcp_hdrlen, ip_hdrlen, total_len, hdr_room;
20626eb5e529SEmmanuel Grumbach 	unsigned int mss = skb_shinfo(skb)->gso_size;
20636eb5e529SEmmanuel Grumbach 	u16 length, iv_len, amsdu_pad;
20646eb5e529SEmmanuel Grumbach 	u8 *start_hdr;
20656eb5e529SEmmanuel Grumbach 	struct iwl_tso_hdr_page *hdr_page;
206621cb3222SJohannes Berg 	struct page **page_ptr;
20676eb5e529SEmmanuel Grumbach 	int ret;
20686eb5e529SEmmanuel Grumbach 	struct tso_t tso;
20696eb5e529SEmmanuel Grumbach 
20706eb5e529SEmmanuel Grumbach 	/* if the packet is protected, then it must be CCMP or GCMP */
20716eb5e529SEmmanuel Grumbach 	BUILD_BUG_ON(IEEE80211_CCMP_HDR_LEN != IEEE80211_GCMP_HDR_LEN);
20726eb5e529SEmmanuel Grumbach 	iv_len = ieee80211_has_protected(hdr->frame_control) ?
20736eb5e529SEmmanuel Grumbach 		IEEE80211_CCMP_HDR_LEN : 0;
20746eb5e529SEmmanuel Grumbach 
20756eb5e529SEmmanuel Grumbach 	trace_iwlwifi_dev_tx(trans->dev, skb,
2076bb98ecd4SSara Sharon 			     iwl_pcie_get_tfd(trans_pcie, txq, txq->write_ptr),
20776983ba69SSara Sharon 			     trans_pcie->tfd_size,
20788790fce4SJohannes Berg 			     &dev_cmd->hdr, IWL_FIRST_TB_SIZE + tb1_len, 0);
20796eb5e529SEmmanuel Grumbach 
20806eb5e529SEmmanuel Grumbach 	ip_hdrlen = skb_transport_header(skb) - skb_network_header(skb);
20816eb5e529SEmmanuel Grumbach 	snap_ip_tcp_hdrlen = 8 + ip_hdrlen + tcp_hdrlen(skb);
20826eb5e529SEmmanuel Grumbach 	total_len = skb->len - snap_ip_tcp_hdrlen - hdr_len - iv_len;
20836eb5e529SEmmanuel Grumbach 	amsdu_pad = 0;
20846eb5e529SEmmanuel Grumbach 
20856eb5e529SEmmanuel Grumbach 	/* total amount of header we may need for this A-MSDU */
20866eb5e529SEmmanuel Grumbach 	hdr_room = DIV_ROUND_UP(total_len, mss) *
20876eb5e529SEmmanuel Grumbach 		(3 + snap_ip_tcp_hdrlen + sizeof(struct ethhdr)) + iv_len;
20886eb5e529SEmmanuel Grumbach 
20896eb5e529SEmmanuel Grumbach 	/* Our device supports 9 segments at most, it will fit in 1 page */
20906eb5e529SEmmanuel Grumbach 	hdr_page = get_page_hdr(trans, hdr_room);
20916eb5e529SEmmanuel Grumbach 	if (!hdr_page)
20926eb5e529SEmmanuel Grumbach 		return -ENOMEM;
20936eb5e529SEmmanuel Grumbach 
20946eb5e529SEmmanuel Grumbach 	get_page(hdr_page->page);
20956eb5e529SEmmanuel Grumbach 	start_hdr = hdr_page->pos;
209621cb3222SJohannes Berg 	page_ptr = (void *)((u8 *)skb->cb + trans_pcie->page_offs);
209721cb3222SJohannes Berg 	*page_ptr = hdr_page->page;
20986eb5e529SEmmanuel Grumbach 	memcpy(hdr_page->pos, skb->data + hdr_len, iv_len);
20996eb5e529SEmmanuel Grumbach 	hdr_page->pos += iv_len;
21006eb5e529SEmmanuel Grumbach 
21016eb5e529SEmmanuel Grumbach 	/*
21026eb5e529SEmmanuel Grumbach 	 * Pull the ieee80211 header + IV to be able to use TSO core,
21036eb5e529SEmmanuel Grumbach 	 * we will restore it for the tx_status flow.
21046eb5e529SEmmanuel Grumbach 	 */
21056eb5e529SEmmanuel Grumbach 	skb_pull(skb, hdr_len + iv_len);
21066eb5e529SEmmanuel Grumbach 
210705e5a7e5SJohannes Berg 	/*
210805e5a7e5SJohannes Berg 	 * Remove the length of all the headers that we don't actually
210905e5a7e5SJohannes Berg 	 * have in the MPDU by themselves, but that we duplicate into
211005e5a7e5SJohannes Berg 	 * all the different MSDUs inside the A-MSDU.
211105e5a7e5SJohannes Berg 	 */
211205e5a7e5SJohannes Berg 	le16_add_cpu(&tx_cmd->len, -snap_ip_tcp_hdrlen);
211305e5a7e5SJohannes Berg 
21146eb5e529SEmmanuel Grumbach 	tso_start(skb, &tso);
21156eb5e529SEmmanuel Grumbach 
21166eb5e529SEmmanuel Grumbach 	while (total_len) {
21176eb5e529SEmmanuel Grumbach 		/* this is the data left for this subframe */
21186eb5e529SEmmanuel Grumbach 		unsigned int data_left =
21196eb5e529SEmmanuel Grumbach 			min_t(unsigned int, mss, total_len);
21206eb5e529SEmmanuel Grumbach 		struct sk_buff *csum_skb = NULL;
21216eb5e529SEmmanuel Grumbach 		unsigned int hdr_tb_len;
21226eb5e529SEmmanuel Grumbach 		dma_addr_t hdr_tb_phys;
21236eb5e529SEmmanuel Grumbach 		struct tcphdr *tcph;
212405e5a7e5SJohannes Berg 		u8 *iph, *subf_hdrs_start = hdr_page->pos;
21256eb5e529SEmmanuel Grumbach 
21266eb5e529SEmmanuel Grumbach 		total_len -= data_left;
21276eb5e529SEmmanuel Grumbach 
21286eb5e529SEmmanuel Grumbach 		memset(hdr_page->pos, 0, amsdu_pad);
21296eb5e529SEmmanuel Grumbach 		hdr_page->pos += amsdu_pad;
21306eb5e529SEmmanuel Grumbach 		amsdu_pad = (4 - (sizeof(struct ethhdr) + snap_ip_tcp_hdrlen +
21316eb5e529SEmmanuel Grumbach 				  data_left)) & 0x3;
21326eb5e529SEmmanuel Grumbach 		ether_addr_copy(hdr_page->pos, ieee80211_get_DA(hdr));
21336eb5e529SEmmanuel Grumbach 		hdr_page->pos += ETH_ALEN;
21346eb5e529SEmmanuel Grumbach 		ether_addr_copy(hdr_page->pos, ieee80211_get_SA(hdr));
21356eb5e529SEmmanuel Grumbach 		hdr_page->pos += ETH_ALEN;
21366eb5e529SEmmanuel Grumbach 
21376eb5e529SEmmanuel Grumbach 		length = snap_ip_tcp_hdrlen + data_left;
21386eb5e529SEmmanuel Grumbach 		*((__be16 *)hdr_page->pos) = cpu_to_be16(length);
21396eb5e529SEmmanuel Grumbach 		hdr_page->pos += sizeof(length);
21406eb5e529SEmmanuel Grumbach 
21416eb5e529SEmmanuel Grumbach 		/*
21426eb5e529SEmmanuel Grumbach 		 * This will copy the SNAP as well which will be considered
21436eb5e529SEmmanuel Grumbach 		 * as MAC header.
21446eb5e529SEmmanuel Grumbach 		 */
21456eb5e529SEmmanuel Grumbach 		tso_build_hdr(skb, hdr_page->pos, &tso, data_left, !total_len);
21466eb5e529SEmmanuel Grumbach 		iph = hdr_page->pos + 8;
21476eb5e529SEmmanuel Grumbach 		tcph = (void *)(iph + ip_hdrlen);
21486eb5e529SEmmanuel Grumbach 
21496eb5e529SEmmanuel Grumbach 		/* For testing on current hardware only */
21506eb5e529SEmmanuel Grumbach 		if (trans_pcie->sw_csum_tx) {
21516eb5e529SEmmanuel Grumbach 			csum_skb = alloc_skb(data_left + tcp_hdrlen(skb),
21526eb5e529SEmmanuel Grumbach 					     GFP_ATOMIC);
21536eb5e529SEmmanuel Grumbach 			if (!csum_skb) {
21546eb5e529SEmmanuel Grumbach 				ret = -ENOMEM;
21556eb5e529SEmmanuel Grumbach 				goto out_unmap;
21566eb5e529SEmmanuel Grumbach 			}
21576eb5e529SEmmanuel Grumbach 
21586eb5e529SEmmanuel Grumbach 			iwl_compute_pseudo_hdr_csum(iph, tcph,
21596eb5e529SEmmanuel Grumbach 						    skb->protocol ==
21606eb5e529SEmmanuel Grumbach 							htons(ETH_P_IPV6),
21616eb5e529SEmmanuel Grumbach 						    data_left);
21626eb5e529SEmmanuel Grumbach 
216359ae1d12SJohannes Berg 			skb_put_data(csum_skb, tcph, tcp_hdrlen(skb));
2164a52a8a4dSZhang Shengju 			skb_reset_transport_header(csum_skb);
21656eb5e529SEmmanuel Grumbach 			csum_skb->csum_start =
21666eb5e529SEmmanuel Grumbach 				(unsigned char *)tcp_hdr(csum_skb) -
21676eb5e529SEmmanuel Grumbach 						 csum_skb->head;
21686eb5e529SEmmanuel Grumbach 		}
21696eb5e529SEmmanuel Grumbach 
21706eb5e529SEmmanuel Grumbach 		hdr_page->pos += snap_ip_tcp_hdrlen;
21716eb5e529SEmmanuel Grumbach 
21726eb5e529SEmmanuel Grumbach 		hdr_tb_len = hdr_page->pos - start_hdr;
21736eb5e529SEmmanuel Grumbach 		hdr_tb_phys = dma_map_single(trans->dev, start_hdr,
21746eb5e529SEmmanuel Grumbach 					     hdr_tb_len, DMA_TO_DEVICE);
21756eb5e529SEmmanuel Grumbach 		if (unlikely(dma_mapping_error(trans->dev, hdr_tb_phys))) {
21766eb5e529SEmmanuel Grumbach 			dev_kfree_skb(csum_skb);
21776eb5e529SEmmanuel Grumbach 			ret = -EINVAL;
21786eb5e529SEmmanuel Grumbach 			goto out_unmap;
21796eb5e529SEmmanuel Grumbach 		}
21806eb5e529SEmmanuel Grumbach 		iwl_pcie_txq_build_tfd(trans, txq, hdr_tb_phys,
21816eb5e529SEmmanuel Grumbach 				       hdr_tb_len, false);
21826eb5e529SEmmanuel Grumbach 		trace_iwlwifi_dev_tx_tso_chunk(trans->dev, start_hdr,
21836eb5e529SEmmanuel Grumbach 					       hdr_tb_len);
218405e5a7e5SJohannes Berg 		/* add this subframe's headers' length to the tx_cmd */
218505e5a7e5SJohannes Berg 		le16_add_cpu(&tx_cmd->len, hdr_page->pos - subf_hdrs_start);
21866eb5e529SEmmanuel Grumbach 
21876eb5e529SEmmanuel Grumbach 		/* prepare the start_hdr for the next subframe */
21886eb5e529SEmmanuel Grumbach 		start_hdr = hdr_page->pos;
21896eb5e529SEmmanuel Grumbach 
21906eb5e529SEmmanuel Grumbach 		/* put the payload */
21916eb5e529SEmmanuel Grumbach 		while (data_left) {
21926eb5e529SEmmanuel Grumbach 			unsigned int size = min_t(unsigned int, tso.size,
21936eb5e529SEmmanuel Grumbach 						  data_left);
21946eb5e529SEmmanuel Grumbach 			dma_addr_t tb_phys;
21956eb5e529SEmmanuel Grumbach 
21966eb5e529SEmmanuel Grumbach 			if (trans_pcie->sw_csum_tx)
219759ae1d12SJohannes Berg 				skb_put_data(csum_skb, tso.data, size);
21986eb5e529SEmmanuel Grumbach 
21996eb5e529SEmmanuel Grumbach 			tb_phys = dma_map_single(trans->dev, tso.data,
22006eb5e529SEmmanuel Grumbach 						 size, DMA_TO_DEVICE);
22016eb5e529SEmmanuel Grumbach 			if (unlikely(dma_mapping_error(trans->dev, tb_phys))) {
22026eb5e529SEmmanuel Grumbach 				dev_kfree_skb(csum_skb);
22036eb5e529SEmmanuel Grumbach 				ret = -EINVAL;
22046eb5e529SEmmanuel Grumbach 				goto out_unmap;
22056eb5e529SEmmanuel Grumbach 			}
22066eb5e529SEmmanuel Grumbach 
22076eb5e529SEmmanuel Grumbach 			iwl_pcie_txq_build_tfd(trans, txq, tb_phys,
22086eb5e529SEmmanuel Grumbach 					       size, false);
22096eb5e529SEmmanuel Grumbach 			trace_iwlwifi_dev_tx_tso_chunk(trans->dev, tso.data,
22106eb5e529SEmmanuel Grumbach 						       size);
22116eb5e529SEmmanuel Grumbach 
22126eb5e529SEmmanuel Grumbach 			data_left -= size;
22136eb5e529SEmmanuel Grumbach 			tso_build_data(skb, &tso, size);
22146eb5e529SEmmanuel Grumbach 		}
22156eb5e529SEmmanuel Grumbach 
22166eb5e529SEmmanuel Grumbach 		/* For testing on early hardware only */
22176eb5e529SEmmanuel Grumbach 		if (trans_pcie->sw_csum_tx) {
22186eb5e529SEmmanuel Grumbach 			__wsum csum;
22196eb5e529SEmmanuel Grumbach 
22206eb5e529SEmmanuel Grumbach 			csum = skb_checksum(csum_skb,
22216eb5e529SEmmanuel Grumbach 					    skb_checksum_start_offset(csum_skb),
22226eb5e529SEmmanuel Grumbach 					    csum_skb->len -
22236eb5e529SEmmanuel Grumbach 					    skb_checksum_start_offset(csum_skb),
22246eb5e529SEmmanuel Grumbach 					    0);
22256eb5e529SEmmanuel Grumbach 			dev_kfree_skb(csum_skb);
22266eb5e529SEmmanuel Grumbach 			dma_sync_single_for_cpu(trans->dev, hdr_tb_phys,
22276eb5e529SEmmanuel Grumbach 						hdr_tb_len, DMA_TO_DEVICE);
22286eb5e529SEmmanuel Grumbach 			tcph->check = csum_fold(csum);
22296eb5e529SEmmanuel Grumbach 			dma_sync_single_for_device(trans->dev, hdr_tb_phys,
22306eb5e529SEmmanuel Grumbach 						   hdr_tb_len, DMA_TO_DEVICE);
22316eb5e529SEmmanuel Grumbach 		}
22326eb5e529SEmmanuel Grumbach 	}
22336eb5e529SEmmanuel Grumbach 
22346eb5e529SEmmanuel Grumbach 	/* re -add the WiFi header and IV */
22356eb5e529SEmmanuel Grumbach 	skb_push(skb, hdr_len + iv_len);
22366eb5e529SEmmanuel Grumbach 
22376eb5e529SEmmanuel Grumbach 	return 0;
22386eb5e529SEmmanuel Grumbach 
22396eb5e529SEmmanuel Grumbach out_unmap:
2240bb98ecd4SSara Sharon 	iwl_pcie_tfd_unmap(trans, out_meta, txq, txq->write_ptr);
22416eb5e529SEmmanuel Grumbach 	return ret;
22426eb5e529SEmmanuel Grumbach }
22436eb5e529SEmmanuel Grumbach #else /* CONFIG_INET */
22446eb5e529SEmmanuel Grumbach static int iwl_fill_data_tbs_amsdu(struct iwl_trans *trans, struct sk_buff *skb,
22456eb5e529SEmmanuel Grumbach 				   struct iwl_txq *txq, u8 hdr_len,
22466eb5e529SEmmanuel Grumbach 				   struct iwl_cmd_meta *out_meta,
22476eb5e529SEmmanuel Grumbach 				   struct iwl_device_cmd *dev_cmd, u16 tb1_len)
22486eb5e529SEmmanuel Grumbach {
22496eb5e529SEmmanuel Grumbach 	/* No A-MSDU without CONFIG_INET */
22506eb5e529SEmmanuel Grumbach 	WARN_ON(1);
22516eb5e529SEmmanuel Grumbach 
22526eb5e529SEmmanuel Grumbach 	return -1;
22536eb5e529SEmmanuel Grumbach }
22546eb5e529SEmmanuel Grumbach #endif /* CONFIG_INET */
22556eb5e529SEmmanuel Grumbach 
2256e705c121SKalle Valo int iwl_trans_pcie_tx(struct iwl_trans *trans, struct sk_buff *skb,
2257e705c121SKalle Valo 		      struct iwl_device_cmd *dev_cmd, int txq_id)
2258e705c121SKalle Valo {
2259e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2260e705c121SKalle Valo 	struct ieee80211_hdr *hdr;
2261e705c121SKalle Valo 	struct iwl_tx_cmd *tx_cmd = (struct iwl_tx_cmd *)dev_cmd->payload;
2262e705c121SKalle Valo 	struct iwl_cmd_meta *out_meta;
2263e705c121SKalle Valo 	struct iwl_txq *txq;
2264e705c121SKalle Valo 	dma_addr_t tb0_phys, tb1_phys, scratch_phys;
2265e705c121SKalle Valo 	void *tb1_addr;
22664fe10bc6SSara Sharon 	void *tfd;
22673a0b2a42SEmmanuel Grumbach 	u16 len, tb1_len;
2268e705c121SKalle Valo 	bool wait_write_ptr;
2269e705c121SKalle Valo 	__le16 fc;
2270e705c121SKalle Valo 	u8 hdr_len;
2271e705c121SKalle Valo 	u16 wifi_seq;
2272c772a3d3SSara Sharon 	bool amsdu;
2273e705c121SKalle Valo 
2274b2a3b1c1SSara Sharon 	txq = trans_pcie->txq[txq_id];
2275e705c121SKalle Valo 
2276e705c121SKalle Valo 	if (WARN_ONCE(!test_bit(txq_id, trans_pcie->queue_used),
2277e705c121SKalle Valo 		      "TX on unused queue %d\n", txq_id))
2278e705c121SKalle Valo 		return -EINVAL;
2279e705c121SKalle Valo 
228041837ca9SEmmanuel Grumbach 	if (unlikely(trans_pcie->sw_csum_tx &&
228141837ca9SEmmanuel Grumbach 		     skb->ip_summed == CHECKSUM_PARTIAL)) {
228241837ca9SEmmanuel Grumbach 		int offs = skb_checksum_start_offset(skb);
228341837ca9SEmmanuel Grumbach 		int csum_offs = offs + skb->csum_offset;
228441837ca9SEmmanuel Grumbach 		__wsum csum;
228541837ca9SEmmanuel Grumbach 
228641837ca9SEmmanuel Grumbach 		if (skb_ensure_writable(skb, csum_offs + sizeof(__sum16)))
228741837ca9SEmmanuel Grumbach 			return -1;
228841837ca9SEmmanuel Grumbach 
228941837ca9SEmmanuel Grumbach 		csum = skb_checksum(skb, offs, skb->len - offs, 0);
229041837ca9SEmmanuel Grumbach 		*(__sum16 *)(skb->data + csum_offs) = csum_fold(csum);
22913955525dSEmmanuel Grumbach 
22923955525dSEmmanuel Grumbach 		skb->ip_summed = CHECKSUM_UNNECESSARY;
229341837ca9SEmmanuel Grumbach 	}
229441837ca9SEmmanuel Grumbach 
2295e705c121SKalle Valo 	if (skb_is_nonlinear(skb) &&
22963cd1980bSSara Sharon 	    skb_shinfo(skb)->nr_frags > IWL_PCIE_MAX_FRAGS(trans_pcie) &&
2297e705c121SKalle Valo 	    __skb_linearize(skb))
2298e705c121SKalle Valo 		return -ENOMEM;
2299e705c121SKalle Valo 
2300e705c121SKalle Valo 	/* mac80211 always puts the full header into the SKB's head,
2301e705c121SKalle Valo 	 * so there's no need to check if it's readable there
2302e705c121SKalle Valo 	 */
2303e705c121SKalle Valo 	hdr = (struct ieee80211_hdr *)skb->data;
2304e705c121SKalle Valo 	fc = hdr->frame_control;
2305e705c121SKalle Valo 	hdr_len = ieee80211_hdrlen(fc);
2306e705c121SKalle Valo 
2307e705c121SKalle Valo 	spin_lock(&txq->lock);
2308e705c121SKalle Valo 
2309bb98ecd4SSara Sharon 	if (iwl_queue_space(txq) < txq->high_mark) {
23103955525dSEmmanuel Grumbach 		iwl_stop_queue(trans, txq);
23113955525dSEmmanuel Grumbach 
23123955525dSEmmanuel Grumbach 		/* don't put the packet on the ring, if there is no room */
2313bb98ecd4SSara Sharon 		if (unlikely(iwl_queue_space(txq) < 3)) {
231421cb3222SJohannes Berg 			struct iwl_device_cmd **dev_cmd_ptr;
23153955525dSEmmanuel Grumbach 
231621cb3222SJohannes Berg 			dev_cmd_ptr = (void *)((u8 *)skb->cb +
231721cb3222SJohannes Berg 					       trans_pcie->dev_cmd_offs);
231821cb3222SJohannes Berg 
231921cb3222SJohannes Berg 			*dev_cmd_ptr = dev_cmd;
23203955525dSEmmanuel Grumbach 			__skb_queue_tail(&txq->overflow_q, skb);
23213955525dSEmmanuel Grumbach 
23223955525dSEmmanuel Grumbach 			spin_unlock(&txq->lock);
23233955525dSEmmanuel Grumbach 			return 0;
23243955525dSEmmanuel Grumbach 		}
23253955525dSEmmanuel Grumbach 	}
23263955525dSEmmanuel Grumbach 
2327e705c121SKalle Valo 	/* In AGG mode, the index in the ring must correspond to the WiFi
2328e705c121SKalle Valo 	 * sequence number. This is a HW requirements to help the SCD to parse
2329e705c121SKalle Valo 	 * the BA.
2330e705c121SKalle Valo 	 * Check here that the packets are in the right place on the ring.
2331e705c121SKalle Valo 	 */
2332e705c121SKalle Valo 	wifi_seq = IEEE80211_SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
2333e705c121SKalle Valo 	WARN_ONCE(txq->ampdu &&
2334bb98ecd4SSara Sharon 		  (wifi_seq & 0xff) != txq->write_ptr,
2335e705c121SKalle Valo 		  "Q: %d WiFi Seq %d tfdNum %d",
2336bb98ecd4SSara Sharon 		  txq_id, wifi_seq, txq->write_ptr);
2337e705c121SKalle Valo 
2338e705c121SKalle Valo 	/* Set up driver data for this TFD */
2339bb98ecd4SSara Sharon 	txq->entries[txq->write_ptr].skb = skb;
2340bb98ecd4SSara Sharon 	txq->entries[txq->write_ptr].cmd = dev_cmd;
2341e705c121SKalle Valo 
2342e705c121SKalle Valo 	dev_cmd->hdr.sequence =
2343e705c121SKalle Valo 		cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
2344bb98ecd4SSara Sharon 			    INDEX_TO_SEQ(txq->write_ptr)));
2345e705c121SKalle Valo 
2346bb98ecd4SSara Sharon 	tb0_phys = iwl_pcie_get_first_tb_dma(txq, txq->write_ptr);
2347e705c121SKalle Valo 	scratch_phys = tb0_phys + sizeof(struct iwl_cmd_header) +
2348e705c121SKalle Valo 		       offsetof(struct iwl_tx_cmd, scratch);
2349e705c121SKalle Valo 
2350e705c121SKalle Valo 	tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
2351e705c121SKalle Valo 	tx_cmd->dram_msb_ptr = iwl_get_dma_hi_addr(scratch_phys);
2352e705c121SKalle Valo 
2353e705c121SKalle Valo 	/* Set up first empty entry in queue's array of Tx/cmd buffers */
2354bb98ecd4SSara Sharon 	out_meta = &txq->entries[txq->write_ptr].meta;
2355e705c121SKalle Valo 	out_meta->flags = 0;
2356e705c121SKalle Valo 
2357e705c121SKalle Valo 	/*
2358e705c121SKalle Valo 	 * The second TB (tb1) points to the remainder of the TX command
2359e705c121SKalle Valo 	 * and the 802.11 header - dword aligned size
2360e705c121SKalle Valo 	 * (This calculation modifies the TX command, so do it before the
2361e705c121SKalle Valo 	 * setup of the first TB)
2362e705c121SKalle Valo 	 */
2363e705c121SKalle Valo 	len = sizeof(struct iwl_tx_cmd) + sizeof(struct iwl_cmd_header) +
23648de437c7SSara Sharon 	      hdr_len - IWL_FIRST_TB_SIZE;
2365c772a3d3SSara Sharon 	/* do not align A-MSDU to dword as the subframe header aligns it */
2366c772a3d3SSara Sharon 	amsdu = ieee80211_is_data_qos(fc) &&
2367c772a3d3SSara Sharon 		(*ieee80211_get_qos_ctl(hdr) &
2368c772a3d3SSara Sharon 		 IEEE80211_QOS_CTL_A_MSDU_PRESENT);
2369c772a3d3SSara Sharon 	if (trans_pcie->sw_csum_tx || !amsdu) {
2370e705c121SKalle Valo 		tb1_len = ALIGN(len, 4);
2371e705c121SKalle Valo 		/* Tell NIC about any 2-byte padding after MAC header */
2372e705c121SKalle Valo 		if (tb1_len != len)
2373e705c121SKalle Valo 			tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
2374c772a3d3SSara Sharon 	} else {
2375c772a3d3SSara Sharon 		tb1_len = len;
2376c772a3d3SSara Sharon 	}
2377e705c121SKalle Valo 
237805e5a7e5SJohannes Berg 	/*
237905e5a7e5SJohannes Berg 	 * The first TB points to bi-directional DMA data, we'll
238005e5a7e5SJohannes Berg 	 * memcpy the data into it later.
238105e5a7e5SJohannes Berg 	 */
2382e705c121SKalle Valo 	iwl_pcie_txq_build_tfd(trans, txq, tb0_phys,
23838de437c7SSara Sharon 			       IWL_FIRST_TB_SIZE, true);
2384e705c121SKalle Valo 
2385e705c121SKalle Valo 	/* there must be data left over for TB1 or this code must be changed */
23868de437c7SSara Sharon 	BUILD_BUG_ON(sizeof(struct iwl_tx_cmd) < IWL_FIRST_TB_SIZE);
2387e705c121SKalle Valo 
2388e705c121SKalle Valo 	/* map the data for TB1 */
23898de437c7SSara Sharon 	tb1_addr = ((u8 *)&dev_cmd->hdr) + IWL_FIRST_TB_SIZE;
2390e705c121SKalle Valo 	tb1_phys = dma_map_single(trans->dev, tb1_addr, tb1_len, DMA_TO_DEVICE);
2391e705c121SKalle Valo 	if (unlikely(dma_mapping_error(trans->dev, tb1_phys)))
2392e705c121SKalle Valo 		goto out_err;
2393e705c121SKalle Valo 	iwl_pcie_txq_build_tfd(trans, txq, tb1_phys, tb1_len, false);
2394e705c121SKalle Valo 
2395c772a3d3SSara Sharon 	if (amsdu) {
23966eb5e529SEmmanuel Grumbach 		if (unlikely(iwl_fill_data_tbs_amsdu(trans, skb, txq, hdr_len,
23976eb5e529SEmmanuel Grumbach 						     out_meta, dev_cmd,
23986eb5e529SEmmanuel Grumbach 						     tb1_len)))
2399e705c121SKalle Valo 			goto out_err;
24006eb5e529SEmmanuel Grumbach 	} else if (unlikely(iwl_fill_data_tbs(trans, skb, txq, hdr_len,
24016eb5e529SEmmanuel Grumbach 				       out_meta, dev_cmd, tb1_len))) {
24026eb5e529SEmmanuel Grumbach 		goto out_err;
24036eb5e529SEmmanuel Grumbach 	}
2404e705c121SKalle Valo 
240505e5a7e5SJohannes Berg 	/* building the A-MSDU might have changed this data, so memcpy it now */
240605e5a7e5SJohannes Berg 	memcpy(&txq->first_tb_bufs[txq->write_ptr], &dev_cmd->hdr,
240705e5a7e5SJohannes Berg 	       IWL_FIRST_TB_SIZE);
240805e5a7e5SJohannes Berg 
2409bb98ecd4SSara Sharon 	tfd = iwl_pcie_get_tfd(trans_pcie, txq, txq->write_ptr);
2410e705c121SKalle Valo 	/* Set up entry for this TFD in Tx byte-count array */
24114fe10bc6SSara Sharon 	iwl_pcie_txq_update_byte_cnt_tbl(trans, txq, le16_to_cpu(tx_cmd->len),
24124fe10bc6SSara Sharon 					 iwl_pcie_tfd_get_num_tbs(trans, tfd));
2413e705c121SKalle Valo 
2414e705c121SKalle Valo 	wait_write_ptr = ieee80211_has_morefrags(fc);
2415e705c121SKalle Valo 
2416e705c121SKalle Valo 	/* start timer if queue currently empty */
2417bb98ecd4SSara Sharon 	if (txq->read_ptr == txq->write_ptr) {
2418e705c121SKalle Valo 		if (txq->wd_timeout) {
2419e705c121SKalle Valo 			/*
2420e705c121SKalle Valo 			 * If the TXQ is active, then set the timer, if not,
2421e705c121SKalle Valo 			 * set the timer in remainder so that the timer will
2422e705c121SKalle Valo 			 * be armed with the right value when the station will
2423e705c121SKalle Valo 			 * wake up.
2424e705c121SKalle Valo 			 */
2425e705c121SKalle Valo 			if (!txq->frozen)
2426e705c121SKalle Valo 				mod_timer(&txq->stuck_timer,
2427e705c121SKalle Valo 					  jiffies + txq->wd_timeout);
2428e705c121SKalle Valo 			else
2429e705c121SKalle Valo 				txq->frozen_expiry_remainder = txq->wd_timeout;
2430e705c121SKalle Valo 		}
2431bb98ecd4SSara Sharon 		IWL_DEBUG_RPM(trans, "Q: %d first tx - take ref\n", txq->id);
2432c24c7f58SLuca Coelho 		iwl_trans_ref(trans);
2433e705c121SKalle Valo 	}
2434e705c121SKalle Valo 
2435e705c121SKalle Valo 	/* Tell device the write index *just past* this latest filled TFD */
2436bb98ecd4SSara Sharon 	txq->write_ptr = iwl_queue_inc_wrap(txq->write_ptr);
2437e705c121SKalle Valo 	if (!wait_write_ptr)
2438e705c121SKalle Valo 		iwl_pcie_txq_inc_wr_ptr(trans, txq);
2439e705c121SKalle Valo 
2440e705c121SKalle Valo 	/*
2441e705c121SKalle Valo 	 * At this point the frame is "transmitted" successfully
2442e705c121SKalle Valo 	 * and we will get a TX status notification eventually.
2443e705c121SKalle Valo 	 */
2444e705c121SKalle Valo 	spin_unlock(&txq->lock);
2445e705c121SKalle Valo 	return 0;
2446e705c121SKalle Valo out_err:
2447e705c121SKalle Valo 	spin_unlock(&txq->lock);
2448e705c121SKalle Valo 	return -1;
2449e705c121SKalle Valo }
2450