xref: /linux/drivers/net/ipa/ipa_cmd.c (revision 8defab8bdfb1d0dc4e4e3c687cfde33b596896f7)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
4  * Copyright (C) 2019-2022 Linaro Ltd.
5  */
6 
7 #include <linux/types.h>
8 #include <linux/device.h>
9 #include <linux/slab.h>
10 #include <linux/bitfield.h>
11 #include <linux/dma-direction.h>
12 
13 #include "gsi.h"
14 #include "gsi_trans.h"
15 #include "ipa.h"
16 #include "ipa_endpoint.h"
17 #include "ipa_table.h"
18 #include "ipa_cmd.h"
19 #include "ipa_mem.h"
20 
21 /**
22  * DOC:  IPA Immediate Commands
23  *
24  * The AP command TX endpoint is used to issue immediate commands to the IPA.
25  * An immediate command is generally used to request the IPA do something
26  * other than data transfer to another endpoint.
27  *
28  * Immediate commands are represented by GSI transactions just like other
29  * transfer requests, and use a single GSI TRE.  Each immediate command
30  * has a well-defined format, having a payload of a known length.  This
31  * allows the transfer element's length field to be used to hold an
32  * immediate command's opcode.  The payload for a command resides in AP
33  * memory and is described by a single scatterlist entry in its transaction.
34  * Commands do not require a transaction completion callback, and are
35  * always issued using gsi_trans_commit_wait().
36  */
37 
38 /* Some commands can wait until indicated pipeline stages are clear */
39 enum pipeline_clear_options {
40 	pipeline_clear_hps		= 0x0,
41 	pipeline_clear_src_grp		= 0x1,
42 	pipeline_clear_full		= 0x2,
43 };
44 
45 /* IPA_CMD_IP_V{4,6}_{FILTER,ROUTING}_INIT */
46 
47 struct ipa_cmd_hw_ip_fltrt_init {
48 	__le64 hash_rules_addr;
49 	__le64 flags;
50 	__le64 nhash_rules_addr;
51 };
52 
53 /* Field masks for ipa_cmd_hw_ip_fltrt_init structure fields */
54 #define IP_FLTRT_FLAGS_HASH_SIZE_FMASK			GENMASK_ULL(11, 0)
55 #define IP_FLTRT_FLAGS_HASH_ADDR_FMASK			GENMASK_ULL(27, 12)
56 #define IP_FLTRT_FLAGS_NHASH_SIZE_FMASK			GENMASK_ULL(39, 28)
57 #define IP_FLTRT_FLAGS_NHASH_ADDR_FMASK			GENMASK_ULL(55, 40)
58 
59 /* IPA_CMD_HDR_INIT_LOCAL */
60 
61 struct ipa_cmd_hw_hdr_init_local {
62 	__le64 hdr_table_addr;
63 	__le32 flags;
64 	__le32 reserved;
65 };
66 
67 /* Field masks for ipa_cmd_hw_hdr_init_local structure fields */
68 #define HDR_INIT_LOCAL_FLAGS_TABLE_SIZE_FMASK		GENMASK(11, 0)
69 #define HDR_INIT_LOCAL_FLAGS_HDR_ADDR_FMASK		GENMASK(27, 12)
70 
71 /* IPA_CMD_REGISTER_WRITE */
72 
73 /* For IPA v4.0+, the pipeline clear options are encoded in the opcode */
74 #define REGISTER_WRITE_OPCODE_SKIP_CLEAR_FMASK		GENMASK(8, 8)
75 #define REGISTER_WRITE_OPCODE_CLEAR_OPTION_FMASK	GENMASK(10, 9)
76 
77 struct ipa_cmd_register_write {
78 	__le16 flags;		/* Unused/reserved prior to IPA v4.0 */
79 	__le16 offset;
80 	__le32 value;
81 	__le32 value_mask;
82 	__le32 clear_options;	/* Unused/reserved for IPA v4.0+ */
83 };
84 
85 /* Field masks for ipa_cmd_register_write structure fields */
86 /* The next field is present for IPA v4.0+ */
87 #define REGISTER_WRITE_FLAGS_OFFSET_HIGH_FMASK		GENMASK(14, 11)
88 /* The next field is not present for IPA v4.0+ */
89 #define REGISTER_WRITE_FLAGS_SKIP_CLEAR_FMASK		GENMASK(15, 15)
90 
91 /* The next field and its values are not present for IPA v4.0+ */
92 #define REGISTER_WRITE_CLEAR_OPTIONS_FMASK		GENMASK(1, 0)
93 
94 /* IPA_CMD_IP_PACKET_INIT */
95 
96 struct ipa_cmd_ip_packet_init {
97 	u8 dest_endpoint;
98 	u8 reserved[7];
99 };
100 
101 /* Field masks for ipa_cmd_ip_packet_init dest_endpoint field */
102 #define IPA_PACKET_INIT_DEST_ENDPOINT_FMASK		GENMASK(4, 0)
103 
104 /* IPA_CMD_DMA_SHARED_MEM */
105 
106 /* For IPA v4.0+, this opcode gets modified with pipeline clear options */
107 
108 #define DMA_SHARED_MEM_OPCODE_SKIP_CLEAR_FMASK		GENMASK(8, 8)
109 #define DMA_SHARED_MEM_OPCODE_CLEAR_OPTION_FMASK	GENMASK(10, 9)
110 
111 struct ipa_cmd_hw_dma_mem_mem {
112 	__le16 clear_after_read; /* 0 or DMA_SHARED_MEM_CLEAR_AFTER_READ */
113 	__le16 size;
114 	__le16 local_addr;
115 	__le16 flags;
116 	__le64 system_addr;
117 };
118 
119 /* Flag allowing atomic clear of target region after reading data (v4.0+)*/
120 #define DMA_SHARED_MEM_CLEAR_AFTER_READ			GENMASK(15, 15)
121 
122 /* Field masks for ipa_cmd_hw_dma_mem_mem structure fields */
123 #define DMA_SHARED_MEM_FLAGS_DIRECTION_FMASK		GENMASK(0, 0)
124 /* The next two fields are not present for IPA v4.0+ */
125 #define DMA_SHARED_MEM_FLAGS_SKIP_CLEAR_FMASK		GENMASK(1, 1)
126 #define DMA_SHARED_MEM_FLAGS_CLEAR_OPTIONS_FMASK	GENMASK(3, 2)
127 
128 /* IPA_CMD_IP_PACKET_TAG_STATUS */
129 
130 struct ipa_cmd_ip_packet_tag_status {
131 	__le64 tag;
132 };
133 
134 #define IP_PACKET_TAG_STATUS_TAG_FMASK			GENMASK_ULL(63, 16)
135 
136 /* Immediate command payload */
137 union ipa_cmd_payload {
138 	struct ipa_cmd_hw_ip_fltrt_init table_init;
139 	struct ipa_cmd_hw_hdr_init_local hdr_init_local;
140 	struct ipa_cmd_register_write register_write;
141 	struct ipa_cmd_ip_packet_init ip_packet_init;
142 	struct ipa_cmd_hw_dma_mem_mem dma_shared_mem;
143 	struct ipa_cmd_ip_packet_tag_status ip_packet_tag_status;
144 };
145 
146 static void ipa_cmd_validate_build(void)
147 {
148 	/* The size of a filter table needs to fit into fields in the
149 	 * ipa_cmd_hw_ip_fltrt_init structure.  Although hashed tables
150 	 * might not be used, non-hashed and hashed tables have the same
151 	 * maximum size.  IPv4 and IPv6 filter tables have the same number
152 	 * of entries.
153 	 */
154 #define TABLE_SIZE	(IPA_FILTER_COUNT_MAX * sizeof(__le64))
155 	BUILD_BUG_ON(TABLE_SIZE > field_max(IP_FLTRT_FLAGS_HASH_SIZE_FMASK));
156 	BUILD_BUG_ON(TABLE_SIZE > field_max(IP_FLTRT_FLAGS_NHASH_SIZE_FMASK));
157 #undef TABLE_SIZE
158 
159 	/* Hashed and non-hashed fields are assumed to be the same size */
160 	BUILD_BUG_ON(field_max(IP_FLTRT_FLAGS_HASH_SIZE_FMASK) !=
161 		     field_max(IP_FLTRT_FLAGS_NHASH_SIZE_FMASK));
162 	BUILD_BUG_ON(field_max(IP_FLTRT_FLAGS_HASH_ADDR_FMASK) !=
163 		     field_max(IP_FLTRT_FLAGS_NHASH_ADDR_FMASK));
164 
165 	/* Valid endpoint numbers must fit in the IP packet init command */
166 	BUILD_BUG_ON(field_max(IPA_PACKET_INIT_DEST_ENDPOINT_FMASK) <
167 		     IPA_ENDPOINT_MAX - 1);
168 }
169 
170 /* Validate a memory region holding a table */
171 bool ipa_cmd_table_init_valid(struct ipa *ipa, const struct ipa_mem *mem,
172 			      bool route)
173 {
174 	u32 offset_max = field_max(IP_FLTRT_FLAGS_NHASH_ADDR_FMASK);
175 	u32 size_max = field_max(IP_FLTRT_FLAGS_NHASH_SIZE_FMASK);
176 	const char *table = route ? "route" : "filter";
177 	struct device *dev = &ipa->pdev->dev;
178 	u32 size;
179 
180 	size = route ? ipa->route_count * sizeof(__le64) : mem->size;
181 
182 	/* Size must fit in the immediate command field that holds it */
183 	if (size > size_max) {
184 		dev_err(dev, "%s table region size too large\n", table);
185 		dev_err(dev, "    (0x%04x > 0x%04x)\n", size, size_max);
186 
187 		return false;
188 	}
189 
190 	/* Offset must fit in the immediate command field that holds it */
191 	if (mem->offset > offset_max ||
192 	    ipa->mem_offset > offset_max - mem->offset) {
193 		dev_err(dev, "%s table region offset too large\n", table);
194 		dev_err(dev, "    (0x%04x + 0x%04x > 0x%04x)\n",
195 			ipa->mem_offset, mem->offset, offset_max);
196 
197 		return false;
198 	}
199 
200 	return true;
201 }
202 
203 /* Validate the memory region that holds headers */
204 static bool ipa_cmd_header_init_local_valid(struct ipa *ipa)
205 {
206 	struct device *dev = &ipa->pdev->dev;
207 	const struct ipa_mem *mem;
208 	u32 offset_max;
209 	u32 size_max;
210 	u32 offset;
211 	u32 size;
212 
213 	/* In ipa_cmd_hdr_init_local_add() we record the offset and size of
214 	 * the header table memory area in an immediate command.  Make sure
215 	 * the offset and size fit in the fields that need to hold them, and
216 	 * that the entire range is within the overall IPA memory range.
217 	 */
218 	offset_max = field_max(HDR_INIT_LOCAL_FLAGS_HDR_ADDR_FMASK);
219 	size_max = field_max(HDR_INIT_LOCAL_FLAGS_TABLE_SIZE_FMASK);
220 
221 	/* The header memory area contains both the modem and AP header
222 	 * regions.  The modem portion defines the address of the region.
223 	 */
224 	mem = ipa_mem_find(ipa, IPA_MEM_MODEM_HEADER);
225 	offset = mem->offset;
226 	size = mem->size;
227 
228 	/* Make sure the offset fits in the IPA command */
229 	if (offset > offset_max || ipa->mem_offset > offset_max - offset) {
230 		dev_err(dev, "header table region offset too large\n");
231 		dev_err(dev, "    (0x%04x + 0x%04x > 0x%04x)\n",
232 			ipa->mem_offset, offset, offset_max);
233 
234 		return false;
235 	}
236 
237 	/* Add the size of the AP portion (if defined) to the combined size */
238 	mem = ipa_mem_find(ipa, IPA_MEM_AP_HEADER);
239 	if (mem)
240 		size += mem->size;
241 
242 	/* Make sure the combined size fits in the IPA command */
243 	if (size > size_max) {
244 		dev_err(dev, "header table region size too large\n");
245 		dev_err(dev, "    (0x%04x > 0x%08x)\n", size, size_max);
246 
247 		return false;
248 	}
249 
250 	return true;
251 }
252 
253 /* Indicate whether an offset can be used with a register_write command */
254 static bool ipa_cmd_register_write_offset_valid(struct ipa *ipa,
255 						const char *name, u32 offset)
256 {
257 	struct ipa_cmd_register_write *payload;
258 	struct device *dev = &ipa->pdev->dev;
259 	u32 offset_max;
260 	u32 bit_count;
261 
262 	/* The maximum offset in a register_write immediate command depends
263 	 * on the version of IPA.  A 16 bit offset is always supported,
264 	 * but starting with IPA v4.0 some additional high-order bits are
265 	 * allowed.
266 	 */
267 	bit_count = BITS_PER_BYTE * sizeof(payload->offset);
268 	if (ipa->version >= IPA_VERSION_4_0)
269 		bit_count += hweight32(REGISTER_WRITE_FLAGS_OFFSET_HIGH_FMASK);
270 	BUILD_BUG_ON(bit_count > 32);
271 	offset_max = ~0U >> (32 - bit_count);
272 
273 	/* Make sure the offset can be represented by the field(s)
274 	 * that holds it.  Also make sure the offset is not outside
275 	 * the overall IPA memory range.
276 	 */
277 	if (offset > offset_max || ipa->mem_offset > offset_max - offset) {
278 		dev_err(dev, "%s offset too large 0x%04x + 0x%04x > 0x%04x)\n",
279 			name, ipa->mem_offset, offset, offset_max);
280 		return false;
281 	}
282 
283 	return true;
284 }
285 
286 /* Check whether offsets passed to register_write are valid */
287 static bool ipa_cmd_register_write_valid(struct ipa *ipa)
288 {
289 	const struct ipa_reg *reg;
290 	const char *name;
291 	u32 offset;
292 
293 	/* If hashed tables are supported, ensure the hash flush register
294 	 * offset will fit in a register write IPA immediate command.
295 	 */
296 	if (ipa_table_hash_support(ipa)) {
297 		reg = ipa_reg(ipa, FILT_ROUT_HASH_FLUSH);
298 		offset = ipa_reg_offset(reg);
299 		name = "filter/route hash flush";
300 		if (!ipa_cmd_register_write_offset_valid(ipa, name, offset))
301 			return false;
302 	}
303 
304 	/* Each endpoint can have a status endpoint associated with it,
305 	 * and this is recorded in an endpoint register.  If the modem
306 	 * crashes, we reset the status endpoint for all modem endpoints
307 	 * using a register write IPA immediate command.  Make sure the
308 	 * worst case (highest endpoint number) offset of that endpoint
309 	 * fits in the register write command field(s) that must hold it.
310 	 */
311 	reg = ipa_reg(ipa, ENDP_STATUS);
312 	offset = ipa_reg_n_offset(reg, IPA_ENDPOINT_COUNT - 1);
313 	name = "maximal endpoint status";
314 	if (!ipa_cmd_register_write_offset_valid(ipa, name, offset))
315 		return false;
316 
317 	return true;
318 }
319 
320 int ipa_cmd_pool_init(struct gsi_channel *channel, u32 tre_max)
321 {
322 	struct gsi_trans_info *trans_info = &channel->trans_info;
323 	struct device *dev = channel->gsi->dev;
324 
325 	/* Command payloads are allocated one at a time, but a single
326 	 * transaction can require up to the maximum supported by the
327 	 * channel; treat them as if they were allocated all at once.
328 	 */
329 	return gsi_trans_pool_init_dma(dev, &trans_info->cmd_pool,
330 				       sizeof(union ipa_cmd_payload),
331 				       tre_max, channel->trans_tre_max);
332 }
333 
334 void ipa_cmd_pool_exit(struct gsi_channel *channel)
335 {
336 	struct gsi_trans_info *trans_info = &channel->trans_info;
337 	struct device *dev = channel->gsi->dev;
338 
339 	gsi_trans_pool_exit_dma(dev, &trans_info->cmd_pool);
340 }
341 
342 static union ipa_cmd_payload *
343 ipa_cmd_payload_alloc(struct ipa *ipa, dma_addr_t *addr)
344 {
345 	struct gsi_trans_info *trans_info;
346 	struct ipa_endpoint *endpoint;
347 
348 	endpoint = ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX];
349 	trans_info = &ipa->gsi.channel[endpoint->channel_id].trans_info;
350 
351 	return gsi_trans_pool_alloc_dma(&trans_info->cmd_pool, addr);
352 }
353 
354 /* If hash_size is 0, hash_offset and hash_addr ignored. */
355 void ipa_cmd_table_init_add(struct gsi_trans *trans,
356 			    enum ipa_cmd_opcode opcode, u16 size, u32 offset,
357 			    dma_addr_t addr, u16 hash_size, u32 hash_offset,
358 			    dma_addr_t hash_addr)
359 {
360 	struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
361 	struct ipa_cmd_hw_ip_fltrt_init *payload;
362 	union ipa_cmd_payload *cmd_payload;
363 	dma_addr_t payload_addr;
364 	u64 val;
365 
366 	/* Record the non-hash table offset and size */
367 	offset += ipa->mem_offset;
368 	val = u64_encode_bits(offset, IP_FLTRT_FLAGS_NHASH_ADDR_FMASK);
369 	val |= u64_encode_bits(size, IP_FLTRT_FLAGS_NHASH_SIZE_FMASK);
370 
371 	/* The hash table offset and address are zero if its size is 0 */
372 	if (hash_size) {
373 		/* Record the hash table offset and size */
374 		hash_offset += ipa->mem_offset;
375 		val |= u64_encode_bits(hash_offset,
376 				       IP_FLTRT_FLAGS_HASH_ADDR_FMASK);
377 		val |= u64_encode_bits(hash_size,
378 				       IP_FLTRT_FLAGS_HASH_SIZE_FMASK);
379 	}
380 
381 	cmd_payload = ipa_cmd_payload_alloc(ipa, &payload_addr);
382 	payload = &cmd_payload->table_init;
383 
384 	/* Fill in all offsets and sizes and the non-hash table address */
385 	if (hash_size)
386 		payload->hash_rules_addr = cpu_to_le64(hash_addr);
387 	payload->flags = cpu_to_le64(val);
388 	payload->nhash_rules_addr = cpu_to_le64(addr);
389 
390 	gsi_trans_cmd_add(trans, payload, sizeof(*payload), payload_addr,
391 			  opcode);
392 }
393 
394 /* Initialize header space in IPA-local memory */
395 void ipa_cmd_hdr_init_local_add(struct gsi_trans *trans, u32 offset, u16 size,
396 				dma_addr_t addr)
397 {
398 	struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
399 	enum ipa_cmd_opcode opcode = IPA_CMD_HDR_INIT_LOCAL;
400 	struct ipa_cmd_hw_hdr_init_local *payload;
401 	union ipa_cmd_payload *cmd_payload;
402 	dma_addr_t payload_addr;
403 	u32 flags;
404 
405 	offset += ipa->mem_offset;
406 
407 	/* With this command we tell the IPA where in its local memory the
408 	 * header tables reside.  The content of the buffer provided is
409 	 * also written via DMA into that space.  The IPA hardware owns
410 	 * the table, but the AP must initialize it.
411 	 */
412 	cmd_payload = ipa_cmd_payload_alloc(ipa, &payload_addr);
413 	payload = &cmd_payload->hdr_init_local;
414 
415 	payload->hdr_table_addr = cpu_to_le64(addr);
416 	flags = u32_encode_bits(size, HDR_INIT_LOCAL_FLAGS_TABLE_SIZE_FMASK);
417 	flags |= u32_encode_bits(offset, HDR_INIT_LOCAL_FLAGS_HDR_ADDR_FMASK);
418 	payload->flags = cpu_to_le32(flags);
419 
420 	gsi_trans_cmd_add(trans, payload, sizeof(*payload), payload_addr,
421 			  opcode);
422 }
423 
424 void ipa_cmd_register_write_add(struct gsi_trans *trans, u32 offset, u32 value,
425 				u32 mask, bool clear_full)
426 {
427 	struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
428 	struct ipa_cmd_register_write *payload;
429 	union ipa_cmd_payload *cmd_payload;
430 	u32 opcode = IPA_CMD_REGISTER_WRITE;
431 	dma_addr_t payload_addr;
432 	u32 clear_option;
433 	u32 options;
434 	u16 flags;
435 
436 	/* pipeline_clear_src_grp is not used */
437 	clear_option = clear_full ? pipeline_clear_full : pipeline_clear_hps;
438 
439 	/* IPA v4.0+ represents the pipeline clear options in the opcode.  It
440 	 * also supports a larger offset by encoding additional high-order
441 	 * bits in the payload flags field.
442 	 */
443 	if (ipa->version >= IPA_VERSION_4_0) {
444 		u16 offset_high;
445 		u32 val;
446 
447 		/* Opcode encodes pipeline clear options */
448 		/* SKIP_CLEAR is always 0 (don't skip pipeline clear) */
449 		val = u16_encode_bits(clear_option,
450 				      REGISTER_WRITE_OPCODE_CLEAR_OPTION_FMASK);
451 		opcode |= val;
452 
453 		/* Extract the high 4 bits from the offset */
454 		offset_high = (u16)u32_get_bits(offset, GENMASK(19, 16));
455 		offset &= (1 << 16) - 1;
456 
457 		/* Extract the top 4 bits and encode it into the flags field */
458 		flags = u16_encode_bits(offset_high,
459 				REGISTER_WRITE_FLAGS_OFFSET_HIGH_FMASK);
460 		options = 0;	/* reserved */
461 
462 	} else {
463 		flags = 0;	/* SKIP_CLEAR flag is always 0 */
464 		options = u16_encode_bits(clear_option,
465 					  REGISTER_WRITE_CLEAR_OPTIONS_FMASK);
466 	}
467 
468 	cmd_payload = ipa_cmd_payload_alloc(ipa, &payload_addr);
469 	payload = &cmd_payload->register_write;
470 
471 	payload->flags = cpu_to_le16(flags);
472 	payload->offset = cpu_to_le16((u16)offset);
473 	payload->value = cpu_to_le32(value);
474 	payload->value_mask = cpu_to_le32(mask);
475 	payload->clear_options = cpu_to_le32(options);
476 
477 	gsi_trans_cmd_add(trans, payload, sizeof(*payload), payload_addr,
478 			  opcode);
479 }
480 
481 /* Skip IP packet processing on the next data transfer on a TX channel */
482 static void ipa_cmd_ip_packet_init_add(struct gsi_trans *trans, u8 endpoint_id)
483 {
484 	struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
485 	enum ipa_cmd_opcode opcode = IPA_CMD_IP_PACKET_INIT;
486 	struct ipa_cmd_ip_packet_init *payload;
487 	union ipa_cmd_payload *cmd_payload;
488 	dma_addr_t payload_addr;
489 
490 	cmd_payload = ipa_cmd_payload_alloc(ipa, &payload_addr);
491 	payload = &cmd_payload->ip_packet_init;
492 
493 	payload->dest_endpoint = u8_encode_bits(endpoint_id,
494 					IPA_PACKET_INIT_DEST_ENDPOINT_FMASK);
495 
496 	gsi_trans_cmd_add(trans, payload, sizeof(*payload), payload_addr,
497 			  opcode);
498 }
499 
500 /* Use a DMA command to read or write a block of IPA-resident memory */
501 void ipa_cmd_dma_shared_mem_add(struct gsi_trans *trans, u32 offset, u16 size,
502 				dma_addr_t addr, bool toward_ipa)
503 {
504 	struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
505 	enum ipa_cmd_opcode opcode = IPA_CMD_DMA_SHARED_MEM;
506 	struct ipa_cmd_hw_dma_mem_mem *payload;
507 	union ipa_cmd_payload *cmd_payload;
508 	dma_addr_t payload_addr;
509 	u16 flags;
510 
511 	/* size and offset must fit in 16 bit fields */
512 	WARN_ON(!size);
513 	WARN_ON(size > U16_MAX);
514 	WARN_ON(offset > U16_MAX || ipa->mem_offset > U16_MAX - offset);
515 
516 	offset += ipa->mem_offset;
517 
518 	cmd_payload = ipa_cmd_payload_alloc(ipa, &payload_addr);
519 	payload = &cmd_payload->dma_shared_mem;
520 
521 	/* payload->clear_after_read was reserved prior to IPA v4.0.  It's
522 	 * never needed for current code, so it's 0 regardless of version.
523 	 */
524 	payload->size = cpu_to_le16(size);
525 	payload->local_addr = cpu_to_le16(offset);
526 	/* payload->flags:
527 	 *   direction:		0 = write to IPA, 1 read from IPA
528 	 * Starting at v4.0 these are reserved; either way, all zero:
529 	 *   pipeline clear:	0 = wait for pipeline clear (don't skip)
530 	 *   clear_options:	0 = pipeline_clear_hps
531 	 * Instead, for v4.0+ these are encoded in the opcode.  But again
532 	 * since both values are 0 we won't bother OR'ing them in.
533 	 */
534 	flags = toward_ipa ? 0 : DMA_SHARED_MEM_FLAGS_DIRECTION_FMASK;
535 	payload->flags = cpu_to_le16(flags);
536 	payload->system_addr = cpu_to_le64(addr);
537 
538 	gsi_trans_cmd_add(trans, payload, sizeof(*payload), payload_addr,
539 			  opcode);
540 }
541 
542 static void ipa_cmd_ip_tag_status_add(struct gsi_trans *trans)
543 {
544 	struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
545 	enum ipa_cmd_opcode opcode = IPA_CMD_IP_PACKET_TAG_STATUS;
546 	struct ipa_cmd_ip_packet_tag_status *payload;
547 	union ipa_cmd_payload *cmd_payload;
548 	dma_addr_t payload_addr;
549 
550 	cmd_payload = ipa_cmd_payload_alloc(ipa, &payload_addr);
551 	payload = &cmd_payload->ip_packet_tag_status;
552 
553 	payload->tag = le64_encode_bits(0, IP_PACKET_TAG_STATUS_TAG_FMASK);
554 
555 	gsi_trans_cmd_add(trans, payload, sizeof(*payload), payload_addr,
556 			  opcode);
557 }
558 
559 /* Issue a small command TX data transfer */
560 static void ipa_cmd_transfer_add(struct gsi_trans *trans)
561 {
562 	struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
563 	enum ipa_cmd_opcode opcode = IPA_CMD_NONE;
564 	union ipa_cmd_payload *payload;
565 	dma_addr_t payload_addr;
566 
567 	/* Just transfer a zero-filled payload structure */
568 	payload = ipa_cmd_payload_alloc(ipa, &payload_addr);
569 
570 	gsi_trans_cmd_add(trans, payload, sizeof(*payload), payload_addr,
571 			  opcode);
572 }
573 
574 /* Add immediate commands to a transaction to clear the hardware pipeline */
575 void ipa_cmd_pipeline_clear_add(struct gsi_trans *trans)
576 {
577 	struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
578 	struct ipa_endpoint *endpoint;
579 
580 	/* This will complete when the transfer is received */
581 	reinit_completion(&ipa->completion);
582 
583 	/* Issue a no-op register write command (mask 0 means no write) */
584 	ipa_cmd_register_write_add(trans, 0, 0, 0, true);
585 
586 	/* Send a data packet through the IPA pipeline.  The packet_init
587 	 * command says to send the next packet directly to the exception
588 	 * endpoint without any other IPA processing.  The tag_status
589 	 * command requests that status be generated on completion of
590 	 * that transfer, and that it will be tagged with a value.
591 	 * Finally, the transfer command sends a small packet of data
592 	 * (instead of a command) using the command endpoint.
593 	 */
594 	endpoint = ipa->name_map[IPA_ENDPOINT_AP_LAN_RX];
595 	ipa_cmd_ip_packet_init_add(trans, endpoint->endpoint_id);
596 	ipa_cmd_ip_tag_status_add(trans);
597 	ipa_cmd_transfer_add(trans);
598 }
599 
600 /* Returns the number of commands required to clear the pipeline */
601 u32 ipa_cmd_pipeline_clear_count(void)
602 {
603 	return 4;
604 }
605 
606 void ipa_cmd_pipeline_clear_wait(struct ipa *ipa)
607 {
608 	wait_for_completion(&ipa->completion);
609 }
610 
611 /* Allocate a transaction for the command TX endpoint */
612 struct gsi_trans *ipa_cmd_trans_alloc(struct ipa *ipa, u32 tre_count)
613 {
614 	struct ipa_endpoint *endpoint;
615 
616 	if (WARN_ON(tre_count > IPA_COMMAND_TRANS_TRE_MAX))
617 		return NULL;
618 
619 	endpoint = ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX];
620 
621 	return gsi_channel_trans_alloc(&ipa->gsi, endpoint->channel_id,
622 				       tre_count, DMA_NONE);
623 }
624 
625 /* Init function for immediate commands; there is no ipa_cmd_exit() */
626 int ipa_cmd_init(struct ipa *ipa)
627 {
628 	ipa_cmd_validate_build();
629 
630 	if (!ipa_cmd_header_init_local_valid(ipa))
631 		return -EINVAL;
632 
633 	if (!ipa_cmd_register_write_valid(ipa))
634 		return -EINVAL;
635 
636 	return 0;
637 }
638