xref: /linux/drivers/net/wireless/intel/iwlwifi/pcie/tx-gen2.c (revision ae22a94997b8a03dcb3c922857c203246711f9d4)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2017 Intel Deutschland GmbH
4  * Copyright (C) 2018-2020, 2023 Intel Corporation
5  */
6 #include <net/tso.h>
7 #include <linux/tcp.h>
8 
9 #include "iwl-debug.h"
10 #include "iwl-csr.h"
11 #include "iwl-io.h"
12 #include "internal.h"
13 #include "fw/api/tx.h"
14 #include "queue/tx.h"
15 
16 /*************** HOST COMMAND QUEUE FUNCTIONS   *****/
17 
18 /*
19  * iwl_pcie_gen2_enqueue_hcmd - enqueue a uCode command
20  * @priv: device private data point
21  * @cmd: a pointer to the ucode command structure
22  *
23  * The function returns < 0 values to indicate the operation
24  * failed. On success, it returns the index (>= 0) of command in the
25  * command queue.
26  */
27 int iwl_pcie_gen2_enqueue_hcmd(struct iwl_trans *trans,
28 			       struct iwl_host_cmd *cmd)
29 {
30 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
31 	struct iwl_txq *txq = trans->txqs.txq[trans->txqs.cmd.q_id];
32 	struct iwl_device_cmd *out_cmd;
33 	struct iwl_cmd_meta *out_meta;
34 	void *dup_buf = NULL;
35 	dma_addr_t phys_addr;
36 	int i, cmd_pos, idx;
37 	u16 copy_size, cmd_size, tb0_size;
38 	bool had_nocopy = false;
39 	u8 group_id = iwl_cmd_groupid(cmd->id);
40 	const u8 *cmddata[IWL_MAX_CMD_TBS_PER_TFD];
41 	u16 cmdlen[IWL_MAX_CMD_TBS_PER_TFD];
42 	struct iwl_tfh_tfd *tfd;
43 	unsigned long flags;
44 
45 	if (WARN_ON(cmd->flags & CMD_BLOCK_TXQS))
46 		return -EINVAL;
47 
48 	copy_size = sizeof(struct iwl_cmd_header_wide);
49 	cmd_size = sizeof(struct iwl_cmd_header_wide);
50 
51 	for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
52 		cmddata[i] = cmd->data[i];
53 		cmdlen[i] = cmd->len[i];
54 
55 		if (!cmd->len[i])
56 			continue;
57 
58 		/* need at least IWL_FIRST_TB_SIZE copied */
59 		if (copy_size < IWL_FIRST_TB_SIZE) {
60 			int copy = IWL_FIRST_TB_SIZE - copy_size;
61 
62 			if (copy > cmdlen[i])
63 				copy = cmdlen[i];
64 			cmdlen[i] -= copy;
65 			cmddata[i] += copy;
66 			copy_size += copy;
67 		}
68 
69 		if (cmd->dataflags[i] & IWL_HCMD_DFL_NOCOPY) {
70 			had_nocopy = true;
71 			if (WARN_ON(cmd->dataflags[i] & IWL_HCMD_DFL_DUP)) {
72 				idx = -EINVAL;
73 				goto free_dup_buf;
74 			}
75 		} else if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP) {
76 			/*
77 			 * This is also a chunk that isn't copied
78 			 * to the static buffer so set had_nocopy.
79 			 */
80 			had_nocopy = true;
81 
82 			/* only allowed once */
83 			if (WARN_ON(dup_buf)) {
84 				idx = -EINVAL;
85 				goto free_dup_buf;
86 			}
87 
88 			dup_buf = kmemdup(cmddata[i], cmdlen[i],
89 					  GFP_ATOMIC);
90 			if (!dup_buf)
91 				return -ENOMEM;
92 		} else {
93 			/* NOCOPY must not be followed by normal! */
94 			if (WARN_ON(had_nocopy)) {
95 				idx = -EINVAL;
96 				goto free_dup_buf;
97 			}
98 			copy_size += cmdlen[i];
99 		}
100 		cmd_size += cmd->len[i];
101 	}
102 
103 	/*
104 	 * If any of the command structures end up being larger than the
105 	 * TFD_MAX_PAYLOAD_SIZE and they aren't dynamically allocated into
106 	 * separate TFDs, then we will need to increase the size of the buffers
107 	 */
108 	if (WARN(copy_size > TFD_MAX_PAYLOAD_SIZE,
109 		 "Command %s (%#x) is too large (%d bytes)\n",
110 		 iwl_get_cmd_string(trans, cmd->id), cmd->id, copy_size)) {
111 		idx = -EINVAL;
112 		goto free_dup_buf;
113 	}
114 
115 	spin_lock_irqsave(&txq->lock, flags);
116 
117 	idx = iwl_txq_get_cmd_index(txq, txq->write_ptr);
118 	tfd = iwl_txq_get_tfd(trans, txq, txq->write_ptr);
119 	memset(tfd, 0, sizeof(*tfd));
120 
121 	if (iwl_txq_space(trans, txq) < ((cmd->flags & CMD_ASYNC) ? 2 : 1)) {
122 		spin_unlock_irqrestore(&txq->lock, flags);
123 
124 		IWL_ERR(trans, "No space in command queue\n");
125 		iwl_op_mode_cmd_queue_full(trans->op_mode);
126 		idx = -ENOSPC;
127 		goto free_dup_buf;
128 	}
129 
130 	out_cmd = txq->entries[idx].cmd;
131 	out_meta = &txq->entries[idx].meta;
132 
133 	/* re-initialize to NULL */
134 	memset(out_meta, 0, sizeof(*out_meta));
135 	if (cmd->flags & CMD_WANT_SKB)
136 		out_meta->source = cmd;
137 
138 	/* set up the header */
139 	out_cmd->hdr_wide.cmd = iwl_cmd_opcode(cmd->id);
140 	out_cmd->hdr_wide.group_id = group_id;
141 	out_cmd->hdr_wide.version = iwl_cmd_version(cmd->id);
142 	out_cmd->hdr_wide.length =
143 		cpu_to_le16(cmd_size - sizeof(struct iwl_cmd_header_wide));
144 	out_cmd->hdr_wide.reserved = 0;
145 	out_cmd->hdr_wide.sequence =
146 		cpu_to_le16(QUEUE_TO_SEQ(trans->txqs.cmd.q_id) |
147 					 INDEX_TO_SEQ(txq->write_ptr));
148 
149 	cmd_pos = sizeof(struct iwl_cmd_header_wide);
150 	copy_size = sizeof(struct iwl_cmd_header_wide);
151 
152 	/* and copy the data that needs to be copied */
153 	for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
154 		int copy;
155 
156 		if (!cmd->len[i])
157 			continue;
158 
159 		/* copy everything if not nocopy/dup */
160 		if (!(cmd->dataflags[i] & (IWL_HCMD_DFL_NOCOPY |
161 					   IWL_HCMD_DFL_DUP))) {
162 			copy = cmd->len[i];
163 
164 			memcpy((u8 *)out_cmd + cmd_pos, cmd->data[i], copy);
165 			cmd_pos += copy;
166 			copy_size += copy;
167 			continue;
168 		}
169 
170 		/*
171 		 * Otherwise we need at least IWL_FIRST_TB_SIZE copied
172 		 * in total (for bi-directional DMA), but copy up to what
173 		 * we can fit into the payload for debug dump purposes.
174 		 */
175 		copy = min_t(int, TFD_MAX_PAYLOAD_SIZE - cmd_pos, cmd->len[i]);
176 
177 		memcpy((u8 *)out_cmd + cmd_pos, cmd->data[i], copy);
178 		cmd_pos += copy;
179 
180 		/* However, treat copy_size the proper way, we need it below */
181 		if (copy_size < IWL_FIRST_TB_SIZE) {
182 			copy = IWL_FIRST_TB_SIZE - copy_size;
183 
184 			if (copy > cmd->len[i])
185 				copy = cmd->len[i];
186 			copy_size += copy;
187 		}
188 	}
189 
190 	IWL_DEBUG_HC(trans,
191 		     "Sending command %s (%.2x.%.2x), seq: 0x%04X, %d bytes at %d[%d]:%d\n",
192 		     iwl_get_cmd_string(trans, cmd->id), group_id,
193 		     out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
194 		     cmd_size, txq->write_ptr, idx, trans->txqs.cmd.q_id);
195 
196 	/* start the TFD with the minimum copy bytes */
197 	tb0_size = min_t(int, copy_size, IWL_FIRST_TB_SIZE);
198 	memcpy(&txq->first_tb_bufs[idx], out_cmd, tb0_size);
199 	iwl_txq_gen2_set_tb(trans, tfd, iwl_txq_get_first_tb_dma(txq, idx),
200 			    tb0_size);
201 
202 	/* map first command fragment, if any remains */
203 	if (copy_size > tb0_size) {
204 		phys_addr = dma_map_single(trans->dev,
205 					   (u8 *)out_cmd + tb0_size,
206 					   copy_size - tb0_size,
207 					   DMA_TO_DEVICE);
208 		if (dma_mapping_error(trans->dev, phys_addr)) {
209 			idx = -ENOMEM;
210 			iwl_txq_gen2_tfd_unmap(trans, out_meta, tfd);
211 			goto out;
212 		}
213 		iwl_txq_gen2_set_tb(trans, tfd, phys_addr,
214 				    copy_size - tb0_size);
215 	}
216 
217 	/* map the remaining (adjusted) nocopy/dup fragments */
218 	for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
219 		void *data = (void *)(uintptr_t)cmddata[i];
220 
221 		if (!cmdlen[i])
222 			continue;
223 		if (!(cmd->dataflags[i] & (IWL_HCMD_DFL_NOCOPY |
224 					   IWL_HCMD_DFL_DUP)))
225 			continue;
226 		if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP)
227 			data = dup_buf;
228 		phys_addr = dma_map_single(trans->dev, data,
229 					   cmdlen[i], DMA_TO_DEVICE);
230 		if (dma_mapping_error(trans->dev, phys_addr)) {
231 			idx = -ENOMEM;
232 			iwl_txq_gen2_tfd_unmap(trans, out_meta, tfd);
233 			goto out;
234 		}
235 		iwl_txq_gen2_set_tb(trans, tfd, phys_addr, cmdlen[i]);
236 	}
237 
238 	BUILD_BUG_ON(IWL_TFH_NUM_TBS > sizeof(out_meta->tbs) * BITS_PER_BYTE);
239 	out_meta->flags = cmd->flags;
240 	if (WARN_ON_ONCE(txq->entries[idx].free_buf))
241 		kfree_sensitive(txq->entries[idx].free_buf);
242 	txq->entries[idx].free_buf = dup_buf;
243 
244 	trace_iwlwifi_dev_hcmd(trans->dev, cmd, cmd_size, &out_cmd->hdr_wide);
245 
246 	/* start timer if queue currently empty */
247 	if (txq->read_ptr == txq->write_ptr && txq->wd_timeout)
248 		mod_timer(&txq->stuck_timer, jiffies + txq->wd_timeout);
249 
250 	spin_lock(&trans_pcie->reg_lock);
251 	/* Increment and update queue's write index */
252 	txq->write_ptr = iwl_txq_inc_wrap(trans, txq->write_ptr);
253 	iwl_txq_inc_wr_ptr(trans, txq);
254 	spin_unlock(&trans_pcie->reg_lock);
255 
256 out:
257 	spin_unlock_irqrestore(&txq->lock, flags);
258 free_dup_buf:
259 	if (idx < 0)
260 		kfree(dup_buf);
261 	return idx;
262 }
263