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