xref: /OK3568_Linux_fs/kernel/drivers/net/wireless/intel/iwlwifi/pcie/tx-gen2.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /******************************************************************************
2*4882a593Smuzhiyun  *
3*4882a593Smuzhiyun  * This file is provided under a dual BSD/GPLv2 license.  When using or
4*4882a593Smuzhiyun  * redistributing this file, you may do so under either license.
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * GPL LICENSE SUMMARY
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Copyright(c) 2017 Intel Deutschland GmbH
9*4882a593Smuzhiyun  * Copyright(c) 2018 - 2020 Intel Corporation
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * This program is free software; you can redistribute it and/or modify
12*4882a593Smuzhiyun  * it under the terms of version 2 of the GNU General Public License as
13*4882a593Smuzhiyun  * published by the Free Software Foundation.
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  * This program is distributed in the hope that it will be useful, but
16*4882a593Smuzhiyun  * WITHOUT ANY WARRANTY; without even the implied warranty of
17*4882a593Smuzhiyun  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18*4882a593Smuzhiyun  * General Public License for more details.
19*4882a593Smuzhiyun  *
20*4882a593Smuzhiyun  * BSD LICENSE
21*4882a593Smuzhiyun  *
22*4882a593Smuzhiyun  * Copyright(c) 2017 Intel Deutschland GmbH
23*4882a593Smuzhiyun  * Copyright(c) 2018 - 2020 Intel Corporation
24*4882a593Smuzhiyun  * All rights reserved.
25*4882a593Smuzhiyun  *
26*4882a593Smuzhiyun  * Redistribution and use in source and binary forms, with or without
27*4882a593Smuzhiyun  * modification, are permitted provided that the following conditions
28*4882a593Smuzhiyun  * are met:
29*4882a593Smuzhiyun  *
30*4882a593Smuzhiyun  *  * Redistributions of source code must retain the above copyright
31*4882a593Smuzhiyun  *    notice, this list of conditions and the following disclaimer.
32*4882a593Smuzhiyun  *  * Redistributions in binary form must reproduce the above copyright
33*4882a593Smuzhiyun  *    notice, this list of conditions and the following disclaimer in
34*4882a593Smuzhiyun  *    the documentation and/or other materials provided with the
35*4882a593Smuzhiyun  *    distribution.
36*4882a593Smuzhiyun  *  * Neither the name Intel Corporation nor the names of its
37*4882a593Smuzhiyun  *    contributors may be used to endorse or promote products derived
38*4882a593Smuzhiyun  *    from this software without specific prior written permission.
39*4882a593Smuzhiyun  *
40*4882a593Smuzhiyun  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
41*4882a593Smuzhiyun  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
42*4882a593Smuzhiyun  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
43*4882a593Smuzhiyun  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
44*4882a593Smuzhiyun  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45*4882a593Smuzhiyun  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
46*4882a593Smuzhiyun  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
47*4882a593Smuzhiyun  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
48*4882a593Smuzhiyun  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
49*4882a593Smuzhiyun  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
50*4882a593Smuzhiyun  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51*4882a593Smuzhiyun  *
52*4882a593Smuzhiyun  *****************************************************************************/
53*4882a593Smuzhiyun #include <net/tso.h>
54*4882a593Smuzhiyun #include <linux/tcp.h>
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun #include "iwl-debug.h"
57*4882a593Smuzhiyun #include "iwl-csr.h"
58*4882a593Smuzhiyun #include "iwl-io.h"
59*4882a593Smuzhiyun #include "internal.h"
60*4882a593Smuzhiyun #include "fw/api/tx.h"
61*4882a593Smuzhiyun #include "queue/tx.h"
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun /*************** HOST COMMAND QUEUE FUNCTIONS   *****/
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun /*
66*4882a593Smuzhiyun  * iwl_pcie_gen2_enqueue_hcmd - enqueue a uCode command
67*4882a593Smuzhiyun  * @priv: device private data point
68*4882a593Smuzhiyun  * @cmd: a pointer to the ucode command structure
69*4882a593Smuzhiyun  *
70*4882a593Smuzhiyun  * The function returns < 0 values to indicate the operation
71*4882a593Smuzhiyun  * failed. On success, it returns the index (>= 0) of command in the
72*4882a593Smuzhiyun  * command queue.
73*4882a593Smuzhiyun  */
iwl_pcie_gen2_enqueue_hcmd(struct iwl_trans * trans,struct iwl_host_cmd * cmd)74*4882a593Smuzhiyun static int iwl_pcie_gen2_enqueue_hcmd(struct iwl_trans *trans,
75*4882a593Smuzhiyun 				      struct iwl_host_cmd *cmd)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
78*4882a593Smuzhiyun 	struct iwl_txq *txq = trans->txqs.txq[trans->txqs.cmd.q_id];
79*4882a593Smuzhiyun 	struct iwl_device_cmd *out_cmd;
80*4882a593Smuzhiyun 	struct iwl_cmd_meta *out_meta;
81*4882a593Smuzhiyun 	void *dup_buf = NULL;
82*4882a593Smuzhiyun 	dma_addr_t phys_addr;
83*4882a593Smuzhiyun 	int i, cmd_pos, idx;
84*4882a593Smuzhiyun 	u16 copy_size, cmd_size, tb0_size;
85*4882a593Smuzhiyun 	bool had_nocopy = false;
86*4882a593Smuzhiyun 	u8 group_id = iwl_cmd_groupid(cmd->id);
87*4882a593Smuzhiyun 	const u8 *cmddata[IWL_MAX_CMD_TBS_PER_TFD];
88*4882a593Smuzhiyun 	u16 cmdlen[IWL_MAX_CMD_TBS_PER_TFD];
89*4882a593Smuzhiyun 	struct iwl_tfh_tfd *tfd;
90*4882a593Smuzhiyun 	unsigned long flags;
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	copy_size = sizeof(struct iwl_cmd_header_wide);
93*4882a593Smuzhiyun 	cmd_size = sizeof(struct iwl_cmd_header_wide);
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
96*4882a593Smuzhiyun 		cmddata[i] = cmd->data[i];
97*4882a593Smuzhiyun 		cmdlen[i] = cmd->len[i];
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 		if (!cmd->len[i])
100*4882a593Smuzhiyun 			continue;
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 		/* need at least IWL_FIRST_TB_SIZE copied */
103*4882a593Smuzhiyun 		if (copy_size < IWL_FIRST_TB_SIZE) {
104*4882a593Smuzhiyun 			int copy = IWL_FIRST_TB_SIZE - copy_size;
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 			if (copy > cmdlen[i])
107*4882a593Smuzhiyun 				copy = cmdlen[i];
108*4882a593Smuzhiyun 			cmdlen[i] -= copy;
109*4882a593Smuzhiyun 			cmddata[i] += copy;
110*4882a593Smuzhiyun 			copy_size += copy;
111*4882a593Smuzhiyun 		}
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 		if (cmd->dataflags[i] & IWL_HCMD_DFL_NOCOPY) {
114*4882a593Smuzhiyun 			had_nocopy = true;
115*4882a593Smuzhiyun 			if (WARN_ON(cmd->dataflags[i] & IWL_HCMD_DFL_DUP)) {
116*4882a593Smuzhiyun 				idx = -EINVAL;
117*4882a593Smuzhiyun 				goto free_dup_buf;
118*4882a593Smuzhiyun 			}
119*4882a593Smuzhiyun 		} else if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP) {
120*4882a593Smuzhiyun 			/*
121*4882a593Smuzhiyun 			 * This is also a chunk that isn't copied
122*4882a593Smuzhiyun 			 * to the static buffer so set had_nocopy.
123*4882a593Smuzhiyun 			 */
124*4882a593Smuzhiyun 			had_nocopy = true;
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun 			/* only allowed once */
127*4882a593Smuzhiyun 			if (WARN_ON(dup_buf)) {
128*4882a593Smuzhiyun 				idx = -EINVAL;
129*4882a593Smuzhiyun 				goto free_dup_buf;
130*4882a593Smuzhiyun 			}
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 			dup_buf = kmemdup(cmddata[i], cmdlen[i],
133*4882a593Smuzhiyun 					  GFP_ATOMIC);
134*4882a593Smuzhiyun 			if (!dup_buf)
135*4882a593Smuzhiyun 				return -ENOMEM;
136*4882a593Smuzhiyun 		} else {
137*4882a593Smuzhiyun 			/* NOCOPY must not be followed by normal! */
138*4882a593Smuzhiyun 			if (WARN_ON(had_nocopy)) {
139*4882a593Smuzhiyun 				idx = -EINVAL;
140*4882a593Smuzhiyun 				goto free_dup_buf;
141*4882a593Smuzhiyun 			}
142*4882a593Smuzhiyun 			copy_size += cmdlen[i];
143*4882a593Smuzhiyun 		}
144*4882a593Smuzhiyun 		cmd_size += cmd->len[i];
145*4882a593Smuzhiyun 	}
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	/*
148*4882a593Smuzhiyun 	 * If any of the command structures end up being larger than the
149*4882a593Smuzhiyun 	 * TFD_MAX_PAYLOAD_SIZE and they aren't dynamically allocated into
150*4882a593Smuzhiyun 	 * separate TFDs, then we will need to increase the size of the buffers
151*4882a593Smuzhiyun 	 */
152*4882a593Smuzhiyun 	if (WARN(copy_size > TFD_MAX_PAYLOAD_SIZE,
153*4882a593Smuzhiyun 		 "Command %s (%#x) is too large (%d bytes)\n",
154*4882a593Smuzhiyun 		 iwl_get_cmd_string(trans, cmd->id), cmd->id, copy_size)) {
155*4882a593Smuzhiyun 		idx = -EINVAL;
156*4882a593Smuzhiyun 		goto free_dup_buf;
157*4882a593Smuzhiyun 	}
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	spin_lock_irqsave(&txq->lock, flags);
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun 	idx = iwl_txq_get_cmd_index(txq, txq->write_ptr);
162*4882a593Smuzhiyun 	tfd = iwl_txq_get_tfd(trans, txq, txq->write_ptr);
163*4882a593Smuzhiyun 	memset(tfd, 0, sizeof(*tfd));
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	if (iwl_txq_space(trans, txq) < ((cmd->flags & CMD_ASYNC) ? 2 : 1)) {
166*4882a593Smuzhiyun 		spin_unlock_irqrestore(&txq->lock, flags);
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 		IWL_ERR(trans, "No space in command queue\n");
169*4882a593Smuzhiyun 		iwl_op_mode_cmd_queue_full(trans->op_mode);
170*4882a593Smuzhiyun 		idx = -ENOSPC;
171*4882a593Smuzhiyun 		goto free_dup_buf;
172*4882a593Smuzhiyun 	}
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 	out_cmd = txq->entries[idx].cmd;
175*4882a593Smuzhiyun 	out_meta = &txq->entries[idx].meta;
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 	/* re-initialize to NULL */
178*4882a593Smuzhiyun 	memset(out_meta, 0, sizeof(*out_meta));
179*4882a593Smuzhiyun 	if (cmd->flags & CMD_WANT_SKB)
180*4882a593Smuzhiyun 		out_meta->source = cmd;
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun 	/* set up the header */
183*4882a593Smuzhiyun 	out_cmd->hdr_wide.cmd = iwl_cmd_opcode(cmd->id);
184*4882a593Smuzhiyun 	out_cmd->hdr_wide.group_id = group_id;
185*4882a593Smuzhiyun 	out_cmd->hdr_wide.version = iwl_cmd_version(cmd->id);
186*4882a593Smuzhiyun 	out_cmd->hdr_wide.length =
187*4882a593Smuzhiyun 		cpu_to_le16(cmd_size - sizeof(struct iwl_cmd_header_wide));
188*4882a593Smuzhiyun 	out_cmd->hdr_wide.reserved = 0;
189*4882a593Smuzhiyun 	out_cmd->hdr_wide.sequence =
190*4882a593Smuzhiyun 		cpu_to_le16(QUEUE_TO_SEQ(trans->txqs.cmd.q_id) |
191*4882a593Smuzhiyun 					 INDEX_TO_SEQ(txq->write_ptr));
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	cmd_pos = sizeof(struct iwl_cmd_header_wide);
194*4882a593Smuzhiyun 	copy_size = sizeof(struct iwl_cmd_header_wide);
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 	/* and copy the data that needs to be copied */
197*4882a593Smuzhiyun 	for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
198*4882a593Smuzhiyun 		int copy;
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun 		if (!cmd->len[i])
201*4882a593Smuzhiyun 			continue;
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun 		/* copy everything if not nocopy/dup */
204*4882a593Smuzhiyun 		if (!(cmd->dataflags[i] & (IWL_HCMD_DFL_NOCOPY |
205*4882a593Smuzhiyun 					   IWL_HCMD_DFL_DUP))) {
206*4882a593Smuzhiyun 			copy = cmd->len[i];
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun 			memcpy((u8 *)out_cmd + cmd_pos, cmd->data[i], copy);
209*4882a593Smuzhiyun 			cmd_pos += copy;
210*4882a593Smuzhiyun 			copy_size += copy;
211*4882a593Smuzhiyun 			continue;
212*4882a593Smuzhiyun 		}
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 		/*
215*4882a593Smuzhiyun 		 * Otherwise we need at least IWL_FIRST_TB_SIZE copied
216*4882a593Smuzhiyun 		 * in total (for bi-directional DMA), but copy up to what
217*4882a593Smuzhiyun 		 * we can fit into the payload for debug dump purposes.
218*4882a593Smuzhiyun 		 */
219*4882a593Smuzhiyun 		copy = min_t(int, TFD_MAX_PAYLOAD_SIZE - cmd_pos, cmd->len[i]);
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun 		memcpy((u8 *)out_cmd + cmd_pos, cmd->data[i], copy);
222*4882a593Smuzhiyun 		cmd_pos += copy;
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 		/* However, treat copy_size the proper way, we need it below */
225*4882a593Smuzhiyun 		if (copy_size < IWL_FIRST_TB_SIZE) {
226*4882a593Smuzhiyun 			copy = IWL_FIRST_TB_SIZE - copy_size;
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun 			if (copy > cmd->len[i])
229*4882a593Smuzhiyun 				copy = cmd->len[i];
230*4882a593Smuzhiyun 			copy_size += copy;
231*4882a593Smuzhiyun 		}
232*4882a593Smuzhiyun 	}
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 	IWL_DEBUG_HC(trans,
235*4882a593Smuzhiyun 		     "Sending command %s (%.2x.%.2x), seq: 0x%04X, %d bytes at %d[%d]:%d\n",
236*4882a593Smuzhiyun 		     iwl_get_cmd_string(trans, cmd->id), group_id,
237*4882a593Smuzhiyun 		     out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
238*4882a593Smuzhiyun 		     cmd_size, txq->write_ptr, idx, trans->txqs.cmd.q_id);
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun 	/* start the TFD with the minimum copy bytes */
241*4882a593Smuzhiyun 	tb0_size = min_t(int, copy_size, IWL_FIRST_TB_SIZE);
242*4882a593Smuzhiyun 	memcpy(&txq->first_tb_bufs[idx], out_cmd, tb0_size);
243*4882a593Smuzhiyun 	iwl_txq_gen2_set_tb(trans, tfd, iwl_txq_get_first_tb_dma(txq, idx),
244*4882a593Smuzhiyun 			    tb0_size);
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun 	/* map first command fragment, if any remains */
247*4882a593Smuzhiyun 	if (copy_size > tb0_size) {
248*4882a593Smuzhiyun 		phys_addr = dma_map_single(trans->dev,
249*4882a593Smuzhiyun 					   (u8 *)out_cmd + tb0_size,
250*4882a593Smuzhiyun 					   copy_size - tb0_size,
251*4882a593Smuzhiyun 					   DMA_TO_DEVICE);
252*4882a593Smuzhiyun 		if (dma_mapping_error(trans->dev, phys_addr)) {
253*4882a593Smuzhiyun 			idx = -ENOMEM;
254*4882a593Smuzhiyun 			iwl_txq_gen2_tfd_unmap(trans, out_meta, tfd);
255*4882a593Smuzhiyun 			goto out;
256*4882a593Smuzhiyun 		}
257*4882a593Smuzhiyun 		iwl_txq_gen2_set_tb(trans, tfd, phys_addr,
258*4882a593Smuzhiyun 				    copy_size - tb0_size);
259*4882a593Smuzhiyun 	}
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun 	/* map the remaining (adjusted) nocopy/dup fragments */
262*4882a593Smuzhiyun 	for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
263*4882a593Smuzhiyun 		const void *data = cmddata[i];
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun 		if (!cmdlen[i])
266*4882a593Smuzhiyun 			continue;
267*4882a593Smuzhiyun 		if (!(cmd->dataflags[i] & (IWL_HCMD_DFL_NOCOPY |
268*4882a593Smuzhiyun 					   IWL_HCMD_DFL_DUP)))
269*4882a593Smuzhiyun 			continue;
270*4882a593Smuzhiyun 		if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP)
271*4882a593Smuzhiyun 			data = dup_buf;
272*4882a593Smuzhiyun 		phys_addr = dma_map_single(trans->dev, (void *)data,
273*4882a593Smuzhiyun 					   cmdlen[i], DMA_TO_DEVICE);
274*4882a593Smuzhiyun 		if (dma_mapping_error(trans->dev, phys_addr)) {
275*4882a593Smuzhiyun 			idx = -ENOMEM;
276*4882a593Smuzhiyun 			iwl_txq_gen2_tfd_unmap(trans, out_meta, tfd);
277*4882a593Smuzhiyun 			goto out;
278*4882a593Smuzhiyun 		}
279*4882a593Smuzhiyun 		iwl_txq_gen2_set_tb(trans, tfd, phys_addr, cmdlen[i]);
280*4882a593Smuzhiyun 	}
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun 	BUILD_BUG_ON(IWL_TFH_NUM_TBS > sizeof(out_meta->tbs) * BITS_PER_BYTE);
283*4882a593Smuzhiyun 	out_meta->flags = cmd->flags;
284*4882a593Smuzhiyun 	if (WARN_ON_ONCE(txq->entries[idx].free_buf))
285*4882a593Smuzhiyun 		kfree_sensitive(txq->entries[idx].free_buf);
286*4882a593Smuzhiyun 	txq->entries[idx].free_buf = dup_buf;
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun 	trace_iwlwifi_dev_hcmd(trans->dev, cmd, cmd_size, &out_cmd->hdr_wide);
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun 	/* start timer if queue currently empty */
291*4882a593Smuzhiyun 	if (txq->read_ptr == txq->write_ptr && txq->wd_timeout)
292*4882a593Smuzhiyun 		mod_timer(&txq->stuck_timer, jiffies + txq->wd_timeout);
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 	spin_lock(&trans_pcie->reg_lock);
295*4882a593Smuzhiyun 	/* Increment and update queue's write index */
296*4882a593Smuzhiyun 	txq->write_ptr = iwl_txq_inc_wrap(trans, txq->write_ptr);
297*4882a593Smuzhiyun 	iwl_txq_inc_wr_ptr(trans, txq);
298*4882a593Smuzhiyun 	spin_unlock(&trans_pcie->reg_lock);
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun out:
301*4882a593Smuzhiyun 	spin_unlock_irqrestore(&txq->lock, flags);
302*4882a593Smuzhiyun free_dup_buf:
303*4882a593Smuzhiyun 	if (idx < 0)
304*4882a593Smuzhiyun 		kfree(dup_buf);
305*4882a593Smuzhiyun 	return idx;
306*4882a593Smuzhiyun }
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun #define HOST_COMPLETE_TIMEOUT	(2 * HZ)
309*4882a593Smuzhiyun 
iwl_pcie_gen2_send_hcmd_sync(struct iwl_trans * trans,struct iwl_host_cmd * cmd)310*4882a593Smuzhiyun static int iwl_pcie_gen2_send_hcmd_sync(struct iwl_trans *trans,
311*4882a593Smuzhiyun 					struct iwl_host_cmd *cmd)
312*4882a593Smuzhiyun {
313*4882a593Smuzhiyun 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
314*4882a593Smuzhiyun 	const char *cmd_str = iwl_get_cmd_string(trans, cmd->id);
315*4882a593Smuzhiyun 	struct iwl_txq *txq = trans->txqs.txq[trans->txqs.cmd.q_id];
316*4882a593Smuzhiyun 	int cmd_idx;
317*4882a593Smuzhiyun 	int ret;
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun 	IWL_DEBUG_INFO(trans, "Attempting to send sync command %s\n", cmd_str);
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun 	if (WARN(test_and_set_bit(STATUS_SYNC_HCMD_ACTIVE,
322*4882a593Smuzhiyun 				  &trans->status),
323*4882a593Smuzhiyun 		 "Command %s: a command is already active!\n", cmd_str))
324*4882a593Smuzhiyun 		return -EIO;
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun 	IWL_DEBUG_INFO(trans, "Setting HCMD_ACTIVE for command %s\n", cmd_str);
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 	cmd_idx = iwl_pcie_gen2_enqueue_hcmd(trans, cmd);
329*4882a593Smuzhiyun 	if (cmd_idx < 0) {
330*4882a593Smuzhiyun 		ret = cmd_idx;
331*4882a593Smuzhiyun 		clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
332*4882a593Smuzhiyun 		IWL_ERR(trans, "Error sending %s: enqueue_hcmd failed: %d\n",
333*4882a593Smuzhiyun 			cmd_str, ret);
334*4882a593Smuzhiyun 		return ret;
335*4882a593Smuzhiyun 	}
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun 	ret = wait_event_timeout(trans_pcie->wait_command_queue,
338*4882a593Smuzhiyun 				 !test_bit(STATUS_SYNC_HCMD_ACTIVE,
339*4882a593Smuzhiyun 					   &trans->status),
340*4882a593Smuzhiyun 				 HOST_COMPLETE_TIMEOUT);
341*4882a593Smuzhiyun 	if (!ret) {
342*4882a593Smuzhiyun 		IWL_ERR(trans, "Error sending %s: time out after %dms.\n",
343*4882a593Smuzhiyun 			cmd_str, jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
344*4882a593Smuzhiyun 
345*4882a593Smuzhiyun 		IWL_ERR(trans, "Current CMD queue read_ptr %d write_ptr %d\n",
346*4882a593Smuzhiyun 			txq->read_ptr, txq->write_ptr);
347*4882a593Smuzhiyun 
348*4882a593Smuzhiyun 		clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
349*4882a593Smuzhiyun 		IWL_DEBUG_INFO(trans, "Clearing HCMD_ACTIVE for command %s\n",
350*4882a593Smuzhiyun 			       cmd_str);
351*4882a593Smuzhiyun 		ret = -ETIMEDOUT;
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun 		iwl_trans_pcie_sync_nmi(trans);
354*4882a593Smuzhiyun 		goto cancel;
355*4882a593Smuzhiyun 	}
356*4882a593Smuzhiyun 
357*4882a593Smuzhiyun 	if (test_bit(STATUS_FW_ERROR, &trans->status)) {
358*4882a593Smuzhiyun 		IWL_ERR(trans, "FW error in SYNC CMD %s\n", cmd_str);
359*4882a593Smuzhiyun 		dump_stack();
360*4882a593Smuzhiyun 		ret = -EIO;
361*4882a593Smuzhiyun 		goto cancel;
362*4882a593Smuzhiyun 	}
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun 	if (!(cmd->flags & CMD_SEND_IN_RFKILL) &&
365*4882a593Smuzhiyun 	    test_bit(STATUS_RFKILL_OPMODE, &trans->status)) {
366*4882a593Smuzhiyun 		IWL_DEBUG_RF_KILL(trans, "RFKILL in SYNC CMD... no rsp\n");
367*4882a593Smuzhiyun 		ret = -ERFKILL;
368*4882a593Smuzhiyun 		goto cancel;
369*4882a593Smuzhiyun 	}
370*4882a593Smuzhiyun 
371*4882a593Smuzhiyun 	if ((cmd->flags & CMD_WANT_SKB) && !cmd->resp_pkt) {
372*4882a593Smuzhiyun 		IWL_ERR(trans, "Error: Response NULL in '%s'\n", cmd_str);
373*4882a593Smuzhiyun 		ret = -EIO;
374*4882a593Smuzhiyun 		goto cancel;
375*4882a593Smuzhiyun 	}
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun 	return 0;
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun cancel:
380*4882a593Smuzhiyun 	if (cmd->flags & CMD_WANT_SKB) {
381*4882a593Smuzhiyun 		/*
382*4882a593Smuzhiyun 		 * Cancel the CMD_WANT_SKB flag for the cmd in the
383*4882a593Smuzhiyun 		 * TX cmd queue. Otherwise in case the cmd comes
384*4882a593Smuzhiyun 		 * in later, it will possibly set an invalid
385*4882a593Smuzhiyun 		 * address (cmd->meta.source).
386*4882a593Smuzhiyun 		 */
387*4882a593Smuzhiyun 		txq->entries[cmd_idx].meta.flags &= ~CMD_WANT_SKB;
388*4882a593Smuzhiyun 	}
389*4882a593Smuzhiyun 
390*4882a593Smuzhiyun 	if (cmd->resp_pkt) {
391*4882a593Smuzhiyun 		iwl_free_resp(cmd);
392*4882a593Smuzhiyun 		cmd->resp_pkt = NULL;
393*4882a593Smuzhiyun 	}
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun 	return ret;
396*4882a593Smuzhiyun }
397*4882a593Smuzhiyun 
iwl_trans_pcie_gen2_send_hcmd(struct iwl_trans * trans,struct iwl_host_cmd * cmd)398*4882a593Smuzhiyun int iwl_trans_pcie_gen2_send_hcmd(struct iwl_trans *trans,
399*4882a593Smuzhiyun 				  struct iwl_host_cmd *cmd)
400*4882a593Smuzhiyun {
401*4882a593Smuzhiyun 	if (!(cmd->flags & CMD_SEND_IN_RFKILL) &&
402*4882a593Smuzhiyun 	    test_bit(STATUS_RFKILL_OPMODE, &trans->status)) {
403*4882a593Smuzhiyun 		IWL_DEBUG_RF_KILL(trans, "Dropping CMD 0x%x: RF KILL\n",
404*4882a593Smuzhiyun 				  cmd->id);
405*4882a593Smuzhiyun 		return -ERFKILL;
406*4882a593Smuzhiyun 	}
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun 	if (cmd->flags & CMD_ASYNC) {
409*4882a593Smuzhiyun 		int ret;
410*4882a593Smuzhiyun 
411*4882a593Smuzhiyun 		/* An asynchronous command can not expect an SKB to be set. */
412*4882a593Smuzhiyun 		if (WARN_ON(cmd->flags & CMD_WANT_SKB))
413*4882a593Smuzhiyun 			return -EINVAL;
414*4882a593Smuzhiyun 
415*4882a593Smuzhiyun 		ret = iwl_pcie_gen2_enqueue_hcmd(trans, cmd);
416*4882a593Smuzhiyun 		if (ret < 0) {
417*4882a593Smuzhiyun 			IWL_ERR(trans,
418*4882a593Smuzhiyun 				"Error sending %s: enqueue_hcmd failed: %d\n",
419*4882a593Smuzhiyun 				iwl_get_cmd_string(trans, cmd->id), ret);
420*4882a593Smuzhiyun 			return ret;
421*4882a593Smuzhiyun 		}
422*4882a593Smuzhiyun 		return 0;
423*4882a593Smuzhiyun 	}
424*4882a593Smuzhiyun 
425*4882a593Smuzhiyun 	return iwl_pcie_gen2_send_hcmd_sync(trans, cmd);
426*4882a593Smuzhiyun }
427*4882a593Smuzhiyun 
428