xref: /linux/drivers/net/ipa/ipa_endpoint.c (revision 65d2dbb300197839eafc4171cfeb57a14c452724)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
4  * Copyright (C) 2019-2020 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/if_rmnet.h>
12 #include <linux/dma-direction.h>
13 
14 #include "gsi.h"
15 #include "gsi_trans.h"
16 #include "ipa.h"
17 #include "ipa_data.h"
18 #include "ipa_endpoint.h"
19 #include "ipa_cmd.h"
20 #include "ipa_mem.h"
21 #include "ipa_modem.h"
22 #include "ipa_table.h"
23 #include "ipa_gsi.h"
24 #include "ipa_clock.h"
25 
26 #define atomic_dec_not_zero(v)	atomic_add_unless((v), -1, 0)
27 
28 #define IPA_REPLENISH_BATCH	16
29 
30 /* RX buffer is 1 page (or a power-of-2 contiguous pages) */
31 #define IPA_RX_BUFFER_SIZE	8192	/* PAGE_SIZE > 4096 wastes a LOT */
32 
33 /* The amount of RX buffer space consumed by standard skb overhead */
34 #define IPA_RX_BUFFER_OVERHEAD	(PAGE_SIZE - SKB_MAX_ORDER(NET_SKB_PAD, 0))
35 
36 /* Where to find the QMAP mux_id for a packet within modem-supplied metadata */
37 #define IPA_ENDPOINT_QMAP_METADATA_MASK		0x000000ff /* host byte order */
38 
39 #define IPA_ENDPOINT_RESET_AGGR_RETRY_MAX	3
40 #define IPA_AGGR_TIME_LIMIT			500	/* microseconds */
41 
42 /** enum ipa_status_opcode - status element opcode hardware values */
43 enum ipa_status_opcode {
44 	IPA_STATUS_OPCODE_PACKET		= 0x01,
45 	IPA_STATUS_OPCODE_DROPPED_PACKET	= 0x04,
46 	IPA_STATUS_OPCODE_SUSPENDED_PACKET	= 0x08,
47 	IPA_STATUS_OPCODE_PACKET_2ND_PASS	= 0x40,
48 };
49 
50 /** enum ipa_status_exception - status element exception type */
51 enum ipa_status_exception {
52 	/* 0 means no exception */
53 	IPA_STATUS_EXCEPTION_DEAGGR		= 0x01,
54 };
55 
56 /* Status element provided by hardware */
57 struct ipa_status {
58 	u8 opcode;		/* enum ipa_status_opcode */
59 	u8 exception;		/* enum ipa_status_exception */
60 	__le16 mask;
61 	__le16 pkt_len;
62 	u8 endp_src_idx;
63 	u8 endp_dst_idx;
64 	__le32 metadata;
65 	__le32 flags1;
66 	__le64 flags2;
67 	__le32 flags3;
68 	__le32 flags4;
69 };
70 
71 /* Field masks for struct ipa_status structure fields */
72 #define IPA_STATUS_MASK_TAG_VALID_FMASK		GENMASK(4, 4)
73 #define IPA_STATUS_SRC_IDX_FMASK		GENMASK(4, 0)
74 #define IPA_STATUS_DST_IDX_FMASK		GENMASK(4, 0)
75 #define IPA_STATUS_FLAGS1_RT_RULE_ID_FMASK	GENMASK(31, 22)
76 #define IPA_STATUS_FLAGS2_TAG_FMASK		GENMASK_ULL(63, 16)
77 
78 #ifdef IPA_VALIDATE
79 
80 static bool ipa_endpoint_data_valid_one(struct ipa *ipa, u32 count,
81 			    const struct ipa_gsi_endpoint_data *all_data,
82 			    const struct ipa_gsi_endpoint_data *data)
83 {
84 	const struct ipa_gsi_endpoint_data *other_data;
85 	struct device *dev = &ipa->pdev->dev;
86 	enum ipa_endpoint_name other_name;
87 
88 	if (ipa_gsi_endpoint_data_empty(data))
89 		return true;
90 
91 	if (!data->toward_ipa) {
92 		if (data->endpoint.filter_support) {
93 			dev_err(dev, "filtering not supported for "
94 					"RX endpoint %u\n",
95 				data->endpoint_id);
96 			return false;
97 		}
98 
99 		return true;	/* Nothing more to check for RX */
100 	}
101 
102 	if (data->endpoint.config.status_enable) {
103 		other_name = data->endpoint.config.tx.status_endpoint;
104 		if (other_name >= count) {
105 			dev_err(dev, "status endpoint name %u out of range "
106 					"for endpoint %u\n",
107 				other_name, data->endpoint_id);
108 			return false;
109 		}
110 
111 		/* Status endpoint must be defined... */
112 		other_data = &all_data[other_name];
113 		if (ipa_gsi_endpoint_data_empty(other_data)) {
114 			dev_err(dev, "DMA endpoint name %u undefined "
115 					"for endpoint %u\n",
116 				other_name, data->endpoint_id);
117 			return false;
118 		}
119 
120 		/* ...and has to be an RX endpoint... */
121 		if (other_data->toward_ipa) {
122 			dev_err(dev,
123 				"status endpoint for endpoint %u not RX\n",
124 				data->endpoint_id);
125 			return false;
126 		}
127 
128 		/* ...and if it's to be an AP endpoint... */
129 		if (other_data->ee_id == GSI_EE_AP) {
130 			/* ...make sure it has status enabled. */
131 			if (!other_data->endpoint.config.status_enable) {
132 				dev_err(dev,
133 					"status not enabled for endpoint %u\n",
134 					other_data->endpoint_id);
135 				return false;
136 			}
137 		}
138 	}
139 
140 	if (data->endpoint.config.dma_mode) {
141 		other_name = data->endpoint.config.dma_endpoint;
142 		if (other_name >= count) {
143 			dev_err(dev, "DMA endpoint name %u out of range "
144 					"for endpoint %u\n",
145 				other_name, data->endpoint_id);
146 			return false;
147 		}
148 
149 		other_data = &all_data[other_name];
150 		if (ipa_gsi_endpoint_data_empty(other_data)) {
151 			dev_err(dev, "DMA endpoint name %u undefined "
152 					"for endpoint %u\n",
153 				other_name, data->endpoint_id);
154 			return false;
155 		}
156 	}
157 
158 	return true;
159 }
160 
161 static u32 aggr_byte_limit_max(enum ipa_version version)
162 {
163 	if (version < IPA_VERSION_4_5)
164 		return field_max(aggr_byte_limit_fmask(true));
165 
166 	return field_max(aggr_byte_limit_fmask(false));
167 }
168 
169 static bool ipa_endpoint_data_valid(struct ipa *ipa, u32 count,
170 				    const struct ipa_gsi_endpoint_data *data)
171 {
172 	const struct ipa_gsi_endpoint_data *dp = data;
173 	struct device *dev = &ipa->pdev->dev;
174 	enum ipa_endpoint_name name;
175 	u32 limit;
176 
177 	if (count > IPA_ENDPOINT_COUNT) {
178 		dev_err(dev, "too many endpoints specified (%u > %u)\n",
179 			count, IPA_ENDPOINT_COUNT);
180 		return false;
181 	}
182 
183 	/* The aggregation byte limit defines the point at which an
184 	 * aggregation window will close.  It is programmed into the
185 	 * IPA hardware as a number of KB.  We don't use "hard byte
186 	 * limit" aggregation, which means that we need to supply
187 	 * enough space in a receive buffer to hold a complete MTU
188 	 * plus normal skb overhead *after* that aggregation byte
189 	 * limit has been crossed.
190 	 *
191 	 * This check ensures we don't define a receive buffer size
192 	 * that would exceed what we can represent in the field that
193 	 * is used to program its size.
194 	 */
195 	limit = aggr_byte_limit_max(ipa->version) * SZ_1K;
196 	limit += IPA_MTU + IPA_RX_BUFFER_OVERHEAD;
197 	if (limit < IPA_RX_BUFFER_SIZE) {
198 		dev_err(dev, "buffer size too big for aggregation (%u > %u)\n",
199 			IPA_RX_BUFFER_SIZE, limit);
200 		return false;
201 	}
202 
203 	/* Make sure needed endpoints have defined data */
204 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_COMMAND_TX])) {
205 		dev_err(dev, "command TX endpoint not defined\n");
206 		return false;
207 	}
208 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_LAN_RX])) {
209 		dev_err(dev, "LAN RX endpoint not defined\n");
210 		return false;
211 	}
212 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_MODEM_TX])) {
213 		dev_err(dev, "AP->modem TX endpoint not defined\n");
214 		return false;
215 	}
216 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_MODEM_RX])) {
217 		dev_err(dev, "AP<-modem RX endpoint not defined\n");
218 		return false;
219 	}
220 
221 	for (name = 0; name < count; name++, dp++)
222 		if (!ipa_endpoint_data_valid_one(ipa, count, data, dp))
223 			return false;
224 
225 	return true;
226 }
227 
228 #else /* !IPA_VALIDATE */
229 
230 static bool ipa_endpoint_data_valid(struct ipa *ipa, u32 count,
231 				    const struct ipa_gsi_endpoint_data *data)
232 {
233 	return true;
234 }
235 
236 #endif /* !IPA_VALIDATE */
237 
238 /* Allocate a transaction to use on a non-command endpoint */
239 static struct gsi_trans *ipa_endpoint_trans_alloc(struct ipa_endpoint *endpoint,
240 						  u32 tre_count)
241 {
242 	struct gsi *gsi = &endpoint->ipa->gsi;
243 	u32 channel_id = endpoint->channel_id;
244 	enum dma_data_direction direction;
245 
246 	direction = endpoint->toward_ipa ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
247 
248 	return gsi_channel_trans_alloc(gsi, channel_id, tre_count, direction);
249 }
250 
251 /* suspend_delay represents suspend for RX, delay for TX endpoints.
252  * Note that suspend is not supported starting with IPA v4.0.
253  */
254 static bool
255 ipa_endpoint_init_ctrl(struct ipa_endpoint *endpoint, bool suspend_delay)
256 {
257 	u32 offset = IPA_REG_ENDP_INIT_CTRL_N_OFFSET(endpoint->endpoint_id);
258 	struct ipa *ipa = endpoint->ipa;
259 	bool state;
260 	u32 mask;
261 	u32 val;
262 
263 	/* Suspend is not supported for IPA v4.0+.  Delay doesn't work
264 	 * correctly on IPA v4.2.
265 	 *
266 	 * if (endpoint->toward_ipa)
267 	 * 	assert(ipa->version != IPA_VERSION_4.2);
268 	 * else
269 	 * 	assert(ipa->version == IPA_VERSION_3_5_1);
270 	 */
271 	mask = endpoint->toward_ipa ? ENDP_DELAY_FMASK : ENDP_SUSPEND_FMASK;
272 
273 	val = ioread32(ipa->reg_virt + offset);
274 	/* Don't bother if it's already in the requested state */
275 	state = !!(val & mask);
276 	if (suspend_delay != state) {
277 		val ^= mask;
278 		iowrite32(val, ipa->reg_virt + offset);
279 	}
280 
281 	return state;
282 }
283 
284 /* We currently don't care what the previous state was for delay mode */
285 static void
286 ipa_endpoint_program_delay(struct ipa_endpoint *endpoint, bool enable)
287 {
288 	/* assert(endpoint->toward_ipa); */
289 
290 	/* Delay mode doesn't work properly for IPA v4.2 */
291 	if (endpoint->ipa->version != IPA_VERSION_4_2)
292 		(void)ipa_endpoint_init_ctrl(endpoint, enable);
293 }
294 
295 static bool ipa_endpoint_aggr_active(struct ipa_endpoint *endpoint)
296 {
297 	u32 mask = BIT(endpoint->endpoint_id);
298 	struct ipa *ipa = endpoint->ipa;
299 	u32 offset;
300 	u32 val;
301 
302 	/* assert(mask & ipa->available); */
303 	offset = ipa_reg_state_aggr_active_offset(ipa->version);
304 	val = ioread32(ipa->reg_virt + offset);
305 
306 	return !!(val & mask);
307 }
308 
309 static void ipa_endpoint_force_close(struct ipa_endpoint *endpoint)
310 {
311 	u32 mask = BIT(endpoint->endpoint_id);
312 	struct ipa *ipa = endpoint->ipa;
313 
314 	/* assert(mask & ipa->available); */
315 	iowrite32(mask, ipa->reg_virt + IPA_REG_AGGR_FORCE_CLOSE_OFFSET);
316 }
317 
318 /**
319  * ipa_endpoint_suspend_aggr() - Emulate suspend interrupt
320  * @endpoint:	Endpoint on which to emulate a suspend
321  *
322  *  Emulate suspend IPA interrupt to unsuspend an endpoint suspended
323  *  with an open aggregation frame.  This is to work around a hardware
324  *  issue in IPA version 3.5.1 where the suspend interrupt will not be
325  *  generated when it should be.
326  */
327 static void ipa_endpoint_suspend_aggr(struct ipa_endpoint *endpoint)
328 {
329 	struct ipa *ipa = endpoint->ipa;
330 
331 	if (!endpoint->data->aggregation)
332 		return;
333 
334 	/* Nothing to do if the endpoint doesn't have aggregation open */
335 	if (!ipa_endpoint_aggr_active(endpoint))
336 		return;
337 
338 	/* Force close aggregation */
339 	ipa_endpoint_force_close(endpoint);
340 
341 	ipa_interrupt_simulate_suspend(ipa->interrupt);
342 }
343 
344 /* Returns previous suspend state (true means suspend was enabled) */
345 static bool
346 ipa_endpoint_program_suspend(struct ipa_endpoint *endpoint, bool enable)
347 {
348 	bool suspended;
349 
350 	if (endpoint->ipa->version != IPA_VERSION_3_5_1)
351 		return enable;	/* For IPA v4.0+, no change made */
352 
353 	/* assert(!endpoint->toward_ipa); */
354 
355 	suspended = ipa_endpoint_init_ctrl(endpoint, enable);
356 
357 	/* A client suspended with an open aggregation frame will not
358 	 * generate a SUSPEND IPA interrupt.  If enabling suspend, have
359 	 * ipa_endpoint_suspend_aggr() handle this.
360 	 */
361 	if (enable && !suspended)
362 		ipa_endpoint_suspend_aggr(endpoint);
363 
364 	return suspended;
365 }
366 
367 /* Enable or disable delay or suspend mode on all modem endpoints */
368 void ipa_endpoint_modem_pause_all(struct ipa *ipa, bool enable)
369 {
370 	u32 endpoint_id;
371 
372 	/* DELAY mode doesn't work correctly on IPA v4.2 */
373 	if (ipa->version == IPA_VERSION_4_2)
374 		return;
375 
376 	for (endpoint_id = 0; endpoint_id < IPA_ENDPOINT_MAX; endpoint_id++) {
377 		struct ipa_endpoint *endpoint = &ipa->endpoint[endpoint_id];
378 
379 		if (endpoint->ee_id != GSI_EE_MODEM)
380 			continue;
381 
382 		/* Set TX delay mode or RX suspend mode */
383 		if (endpoint->toward_ipa)
384 			ipa_endpoint_program_delay(endpoint, enable);
385 		else
386 			(void)ipa_endpoint_program_suspend(endpoint, enable);
387 	}
388 }
389 
390 /* Reset all modem endpoints to use the default exception endpoint */
391 int ipa_endpoint_modem_exception_reset_all(struct ipa *ipa)
392 {
393 	u32 initialized = ipa->initialized;
394 	struct gsi_trans *trans;
395 	u32 count;
396 
397 	/* We need one command per modem TX endpoint.  We can get an upper
398 	 * bound on that by assuming all initialized endpoints are modem->IPA.
399 	 * That won't happen, and we could be more precise, but this is fine
400 	 * for now.  We need to end the transaction with a "tag process."
401 	 */
402 	count = hweight32(initialized) + ipa_cmd_pipeline_clear_count();
403 	trans = ipa_cmd_trans_alloc(ipa, count);
404 	if (!trans) {
405 		dev_err(&ipa->pdev->dev,
406 			"no transaction to reset modem exception endpoints\n");
407 		return -EBUSY;
408 	}
409 
410 	while (initialized) {
411 		u32 endpoint_id = __ffs(initialized);
412 		struct ipa_endpoint *endpoint;
413 		u32 offset;
414 
415 		initialized ^= BIT(endpoint_id);
416 
417 		/* We only reset modem TX endpoints */
418 		endpoint = &ipa->endpoint[endpoint_id];
419 		if (!(endpoint->ee_id == GSI_EE_MODEM && endpoint->toward_ipa))
420 			continue;
421 
422 		offset = IPA_REG_ENDP_STATUS_N_OFFSET(endpoint_id);
423 
424 		/* Value written is 0, and all bits are updated.  That
425 		 * means status is disabled on the endpoint, and as a
426 		 * result all other fields in the register are ignored.
427 		 */
428 		ipa_cmd_register_write_add(trans, offset, 0, ~0, false);
429 	}
430 
431 	ipa_cmd_pipeline_clear_add(trans);
432 
433 	/* XXX This should have a 1 second timeout */
434 	gsi_trans_commit_wait(trans);
435 
436 	ipa_cmd_pipeline_clear_wait(ipa);
437 
438 	return 0;
439 }
440 
441 static void ipa_endpoint_init_cfg(struct ipa_endpoint *endpoint)
442 {
443 	u32 offset = IPA_REG_ENDP_INIT_CFG_N_OFFSET(endpoint->endpoint_id);
444 	u32 val = 0;
445 
446 	/* FRAG_OFFLOAD_EN is 0 */
447 	if (endpoint->data->checksum) {
448 		if (endpoint->toward_ipa) {
449 			u32 checksum_offset;
450 
451 			val |= u32_encode_bits(IPA_CS_OFFLOAD_UL,
452 					       CS_OFFLOAD_EN_FMASK);
453 			/* Checksum header offset is in 4-byte units */
454 			checksum_offset = sizeof(struct rmnet_map_header);
455 			checksum_offset /= sizeof(u32);
456 			val |= u32_encode_bits(checksum_offset,
457 					       CS_METADATA_HDR_OFFSET_FMASK);
458 		} else {
459 			val |= u32_encode_bits(IPA_CS_OFFLOAD_DL,
460 					       CS_OFFLOAD_EN_FMASK);
461 		}
462 	} else {
463 		val |= u32_encode_bits(IPA_CS_OFFLOAD_NONE,
464 				       CS_OFFLOAD_EN_FMASK);
465 	}
466 	/* CS_GEN_QMB_MASTER_SEL is 0 */
467 
468 	iowrite32(val, endpoint->ipa->reg_virt + offset);
469 }
470 
471 /**
472  * ipa_endpoint_init_hdr() - Initialize HDR endpoint configuration register
473  * @endpoint:	Endpoint pointer
474  *
475  * We program QMAP endpoints so each packet received is preceded by a QMAP
476  * header structure.  The QMAP header contains a 1-byte mux_id and 2-byte
477  * packet size field, and we have the IPA hardware populate both for each
478  * received packet.  The header is configured (in the HDR_EXT register)
479  * to use big endian format.
480  *
481  * The packet size is written into the QMAP header's pkt_len field.  That
482  * location is defined here using the HDR_OFST_PKT_SIZE field.
483  *
484  * The mux_id comes from a 4-byte metadata value supplied with each packet
485  * by the modem.  It is *not* a QMAP header, but it does contain the mux_id
486  * value that we want, in its low-order byte.  A bitmask defined in the
487  * endpoint's METADATA_MASK register defines which byte within the modem
488  * metadata contains the mux_id.  And the OFST_METADATA field programmed
489  * here indicates where the extracted byte should be placed within the QMAP
490  * header.
491  */
492 static void ipa_endpoint_init_hdr(struct ipa_endpoint *endpoint)
493 {
494 	u32 offset = IPA_REG_ENDP_INIT_HDR_N_OFFSET(endpoint->endpoint_id);
495 	struct ipa *ipa = endpoint->ipa;
496 	u32 val = 0;
497 
498 	if (endpoint->data->qmap) {
499 		size_t header_size = sizeof(struct rmnet_map_header);
500 		enum ipa_version version = ipa->version;
501 
502 		/* We might supply a checksum header after the QMAP header */
503 		if (endpoint->toward_ipa && endpoint->data->checksum)
504 			header_size += sizeof(struct rmnet_map_ul_csum_header);
505 		val |= ipa_header_size_encoded(version, header_size);
506 
507 		/* Define how to fill fields in a received QMAP header */
508 		if (!endpoint->toward_ipa) {
509 			u32 offset;	/* Field offset within header */
510 
511 			/* Where IPA will write the metadata value */
512 			offset = offsetof(struct rmnet_map_header, mux_id);
513 			val |= ipa_metadata_offset_encoded(version, offset);
514 
515 			/* Where IPA will write the length */
516 			offset = offsetof(struct rmnet_map_header, pkt_len);
517 			/* Upper bits are stored in HDR_EXT with IPA v4.5 */
518 			if (version == IPA_VERSION_4_5)
519 				offset &= field_mask(HDR_OFST_PKT_SIZE_FMASK);
520 
521 			val |= HDR_OFST_PKT_SIZE_VALID_FMASK;
522 			val |= u32_encode_bits(offset, HDR_OFST_PKT_SIZE_FMASK);
523 		}
524 		/* For QMAP TX, metadata offset is 0 (modem assumes this) */
525 		val |= HDR_OFST_METADATA_VALID_FMASK;
526 
527 		/* HDR_ADDITIONAL_CONST_LEN is 0; (RX only) */
528 		/* HDR_A5_MUX is 0 */
529 		/* HDR_LEN_INC_DEAGG_HDR is 0 */
530 		/* HDR_METADATA_REG_VALID is 0 (TX only, version < v4.5) */
531 	}
532 
533 	iowrite32(val, ipa->reg_virt + offset);
534 }
535 
536 static void ipa_endpoint_init_hdr_ext(struct ipa_endpoint *endpoint)
537 {
538 	u32 offset = IPA_REG_ENDP_INIT_HDR_EXT_N_OFFSET(endpoint->endpoint_id);
539 	u32 pad_align = endpoint->data->rx.pad_align;
540 	struct ipa *ipa = endpoint->ipa;
541 	u32 val = 0;
542 
543 	val |= HDR_ENDIANNESS_FMASK;		/* big endian */
544 
545 	/* A QMAP header contains a 6 bit pad field at offset 0.  The RMNet
546 	 * driver assumes this field is meaningful in packets it receives,
547 	 * and assumes the header's payload length includes that padding.
548 	 * The RMNet driver does *not* pad packets it sends, however, so
549 	 * the pad field (although 0) should be ignored.
550 	 */
551 	if (endpoint->data->qmap && !endpoint->toward_ipa) {
552 		val |= HDR_TOTAL_LEN_OR_PAD_VALID_FMASK;
553 		/* HDR_TOTAL_LEN_OR_PAD is 0 (pad, not total_len) */
554 		val |= HDR_PAYLOAD_LEN_INC_PADDING_FMASK;
555 		/* HDR_TOTAL_LEN_OR_PAD_OFFSET is 0 */
556 	}
557 
558 	/* HDR_PAYLOAD_LEN_INC_PADDING is 0 */
559 	if (!endpoint->toward_ipa)
560 		val |= u32_encode_bits(pad_align, HDR_PAD_TO_ALIGNMENT_FMASK);
561 
562 	/* IPA v4.5 adds some most-significant bits to a few fields,
563 	 * two of which are defined in the HDR (not HDR_EXT) register.
564 	 */
565 	if (ipa->version == IPA_VERSION_4_5) {
566 		/* HDR_TOTAL_LEN_OR_PAD_OFFSET is 0, so MSB is 0 */
567 		if (endpoint->data->qmap && !endpoint->toward_ipa) {
568 			u32 offset;
569 
570 			offset = offsetof(struct rmnet_map_header, pkt_len);
571 			offset >>= hweight32(HDR_OFST_PKT_SIZE_FMASK);
572 			val |= u32_encode_bits(offset,
573 					       HDR_OFST_PKT_SIZE_MSB_FMASK);
574 			/* HDR_ADDITIONAL_CONST_LEN is 0 so MSB is 0 */
575 		}
576 	}
577 	iowrite32(val, ipa->reg_virt + offset);
578 }
579 
580 static void ipa_endpoint_init_hdr_metadata_mask(struct ipa_endpoint *endpoint)
581 {
582 	u32 endpoint_id = endpoint->endpoint_id;
583 	u32 val = 0;
584 	u32 offset;
585 
586 	if (endpoint->toward_ipa)
587 		return;		/* Register not valid for TX endpoints */
588 
589 	offset = IPA_REG_ENDP_INIT_HDR_METADATA_MASK_N_OFFSET(endpoint_id);
590 
591 	/* Note that HDR_ENDIANNESS indicates big endian header fields */
592 	if (endpoint->data->qmap)
593 		val = (__force u32)cpu_to_be32(IPA_ENDPOINT_QMAP_METADATA_MASK);
594 
595 	iowrite32(val, endpoint->ipa->reg_virt + offset);
596 }
597 
598 static void ipa_endpoint_init_mode(struct ipa_endpoint *endpoint)
599 {
600 	u32 offset = IPA_REG_ENDP_INIT_MODE_N_OFFSET(endpoint->endpoint_id);
601 	u32 val;
602 
603 	if (!endpoint->toward_ipa)
604 		return;		/* Register not valid for RX endpoints */
605 
606 	if (endpoint->data->dma_mode) {
607 		enum ipa_endpoint_name name = endpoint->data->dma_endpoint;
608 		u32 dma_endpoint_id;
609 
610 		dma_endpoint_id = endpoint->ipa->name_map[name]->endpoint_id;
611 
612 		val = u32_encode_bits(IPA_DMA, MODE_FMASK);
613 		val |= u32_encode_bits(dma_endpoint_id, DEST_PIPE_INDEX_FMASK);
614 	} else {
615 		val = u32_encode_bits(IPA_BASIC, MODE_FMASK);
616 	}
617 	/* All other bits unspecified (and 0) */
618 
619 	iowrite32(val, endpoint->ipa->reg_virt + offset);
620 }
621 
622 /* Compute the aggregation size value to use for a given buffer size */
623 static u32 ipa_aggr_size_kb(u32 rx_buffer_size)
624 {
625 	/* We don't use "hard byte limit" aggregation, so we define the
626 	 * aggregation limit such that our buffer has enough space *after*
627 	 * that limit to receive a full MTU of data, plus overhead.
628 	 */
629 	rx_buffer_size -= IPA_MTU + IPA_RX_BUFFER_OVERHEAD;
630 
631 	return rx_buffer_size / SZ_1K;
632 }
633 
634 /* Encoded values for AGGR endpoint register fields */
635 static u32 aggr_byte_limit_encoded(enum ipa_version version, u32 limit)
636 {
637 	if (version < IPA_VERSION_4_5)
638 		return u32_encode_bits(limit, aggr_byte_limit_fmask(true));
639 
640 	return u32_encode_bits(limit, aggr_byte_limit_fmask(false));
641 }
642 
643 /* Encode the aggregation timer limit (microseconds) based on IPA version */
644 static u32 aggr_time_limit_encoded(enum ipa_version version, u32 limit)
645 {
646 	u32 gran_sel;
647 	u32 fmask;
648 	u32 val;
649 
650 	if (version < IPA_VERSION_4_5) {
651 		/* We set aggregation granularity in ipa_hardware_config() */
652 		limit = DIV_ROUND_CLOSEST(limit, IPA_AGGR_GRANULARITY);
653 
654 		return u32_encode_bits(limit, aggr_time_limit_fmask(true));
655 	}
656 
657 	/* IPA v4.5 expresses the time limit using Qtime.  The AP has
658 	 * pulse generators 0 and 1 available, which were configured
659 	 * in ipa_qtime_config() to have granularity 100 usec and
660 	 * 1 msec, respectively.  Use pulse generator 0 if possible,
661 	 * otherwise fall back to pulse generator 1.
662 	 */
663 	fmask = aggr_time_limit_fmask(false);
664 	val = DIV_ROUND_CLOSEST(limit, 100);
665 	if (val > field_max(fmask)) {
666 		/* Have to use pulse generator 1 (millisecond granularity) */
667 		gran_sel = AGGR_GRAN_SEL_FMASK;
668 		val = DIV_ROUND_CLOSEST(limit, 1000);
669 	} else {
670 		/* We can use pulse generator 0 (100 usec granularity) */
671 		gran_sel = 0;
672 	}
673 
674 	return gran_sel | u32_encode_bits(val, fmask);
675 }
676 
677 static u32 aggr_sw_eof_active_encoded(enum ipa_version version, bool enabled)
678 {
679 	u32 val = enabled ? 1 : 0;
680 
681 	if (version < IPA_VERSION_4_5)
682 		return u32_encode_bits(val, aggr_sw_eof_active_fmask(true));
683 
684 	return u32_encode_bits(val, aggr_sw_eof_active_fmask(false));
685 }
686 
687 static void ipa_endpoint_init_aggr(struct ipa_endpoint *endpoint)
688 {
689 	u32 offset = IPA_REG_ENDP_INIT_AGGR_N_OFFSET(endpoint->endpoint_id);
690 	enum ipa_version version = endpoint->ipa->version;
691 	u32 val = 0;
692 
693 	if (endpoint->data->aggregation) {
694 		if (!endpoint->toward_ipa) {
695 			bool close_eof;
696 			u32 limit;
697 
698 			val |= u32_encode_bits(IPA_ENABLE_AGGR, AGGR_EN_FMASK);
699 			val |= u32_encode_bits(IPA_GENERIC, AGGR_TYPE_FMASK);
700 
701 			limit = ipa_aggr_size_kb(IPA_RX_BUFFER_SIZE);
702 			val |= aggr_byte_limit_encoded(version, limit);
703 
704 			limit = IPA_AGGR_TIME_LIMIT;
705 			val |= aggr_time_limit_encoded(version, limit);
706 
707 			/* AGGR_PKT_LIMIT is 0 (unlimited) */
708 
709 			close_eof = endpoint->data->rx.aggr_close_eof;
710 			val |= aggr_sw_eof_active_encoded(version, close_eof);
711 
712 			/* AGGR_HARD_BYTE_LIMIT_ENABLE is 0 */
713 		} else {
714 			val |= u32_encode_bits(IPA_ENABLE_DEAGGR,
715 					       AGGR_EN_FMASK);
716 			val |= u32_encode_bits(IPA_QCMAP, AGGR_TYPE_FMASK);
717 			/* other fields ignored */
718 		}
719 		/* AGGR_FORCE_CLOSE is 0 */
720 		/* AGGR_GRAN_SEL is 0 for IPA v4.5 */
721 	} else {
722 		val |= u32_encode_bits(IPA_BYPASS_AGGR, AGGR_EN_FMASK);
723 		/* other fields ignored */
724 	}
725 
726 	iowrite32(val, endpoint->ipa->reg_virt + offset);
727 }
728 
729 /* Return the Qtime-based head-of-line blocking timer value that
730  * represents the given number of microseconds.  The result
731  * includes both the timer value and the selected timer granularity.
732  */
733 static u32 hol_block_timer_qtime_val(struct ipa *ipa, u32 microseconds)
734 {
735 	u32 gran_sel;
736 	u32 val;
737 
738 	/* IPA v4.5 expresses time limits using Qtime.  The AP has
739 	 * pulse generators 0 and 1 available, which were configured
740 	 * in ipa_qtime_config() to have granularity 100 usec and
741 	 * 1 msec, respectively.  Use pulse generator 0 if possible,
742 	 * otherwise fall back to pulse generator 1.
743 	 */
744 	val = DIV_ROUND_CLOSEST(microseconds, 100);
745 	if (val > field_max(TIME_LIMIT_FMASK)) {
746 		/* Have to use pulse generator 1 (millisecond granularity) */
747 		gran_sel = GRAN_SEL_FMASK;
748 		val = DIV_ROUND_CLOSEST(microseconds, 1000);
749 	} else {
750 		/* We can use pulse generator 0 (100 usec granularity) */
751 		gran_sel = 0;
752 	}
753 
754 	return gran_sel | u32_encode_bits(val, TIME_LIMIT_FMASK);
755 }
756 
757 /* The head-of-line blocking timer is defined as a tick count.  For
758  * IPA version 4.5 the tick count is based on the Qtimer, which is
759  * derived from the 19.2 MHz SoC XO clock.  For older IPA versions
760  * each tick represents 128 cycles of the IPA core clock.
761  *
762  * Return the encoded value that should be written to that register
763  * that represents the timeout period provided.  For IPA v4.2 this
764  * encodes a base and scale value, while for earlier versions the
765  * value is a simple tick count.
766  */
767 static u32 hol_block_timer_val(struct ipa *ipa, u32 microseconds)
768 {
769 	u32 width;
770 	u32 scale;
771 	u64 ticks;
772 	u64 rate;
773 	u32 high;
774 	u32 val;
775 
776 	if (!microseconds)
777 		return 0;	/* Nothing to compute if timer period is 0 */
778 
779 	if (ipa->version == IPA_VERSION_4_5)
780 		return hol_block_timer_qtime_val(ipa, microseconds);
781 
782 	/* Use 64 bit arithmetic to avoid overflow... */
783 	rate = ipa_clock_rate(ipa);
784 	ticks = DIV_ROUND_CLOSEST(microseconds * rate, 128 * USEC_PER_SEC);
785 	/* ...but we still need to fit into a 32-bit register */
786 	WARN_ON(ticks > U32_MAX);
787 
788 	/* IPA v3.5.1 through v4.1 just record the tick count */
789 	if (ipa->version < IPA_VERSION_4_2)
790 		return (u32)ticks;
791 
792 	/* For IPA v4.2, the tick count is represented by base and
793 	 * scale fields within the 32-bit timer register, where:
794 	 *     ticks = base << scale;
795 	 * The best precision is achieved when the base value is as
796 	 * large as possible.  Find the highest set bit in the tick
797 	 * count, and extract the number of bits in the base field
798 	 * such that that high bit is included.
799 	 */
800 	high = fls(ticks);		/* 1..32 */
801 	width = HWEIGHT32(BASE_VALUE_FMASK);
802 	scale = high > width ? high - width : 0;
803 	if (scale) {
804 		/* If we're scaling, round up to get a closer result */
805 		ticks += 1 << (scale - 1);
806 		/* High bit was set, so rounding might have affected it */
807 		if (fls(ticks) != high)
808 			scale++;
809 	}
810 
811 	val = u32_encode_bits(scale, SCALE_FMASK);
812 	val |= u32_encode_bits(ticks >> scale, BASE_VALUE_FMASK);
813 
814 	return val;
815 }
816 
817 /* If microseconds is 0, timeout is immediate */
818 static void ipa_endpoint_init_hol_block_timer(struct ipa_endpoint *endpoint,
819 					      u32 microseconds)
820 {
821 	u32 endpoint_id = endpoint->endpoint_id;
822 	struct ipa *ipa = endpoint->ipa;
823 	u32 offset;
824 	u32 val;
825 
826 	offset = IPA_REG_ENDP_INIT_HOL_BLOCK_TIMER_N_OFFSET(endpoint_id);
827 	val = hol_block_timer_val(ipa, microseconds);
828 	iowrite32(val, ipa->reg_virt + offset);
829 }
830 
831 static void
832 ipa_endpoint_init_hol_block_enable(struct ipa_endpoint *endpoint, bool enable)
833 {
834 	u32 endpoint_id = endpoint->endpoint_id;
835 	u32 offset;
836 	u32 val;
837 
838 	val = enable ? HOL_BLOCK_EN_FMASK : 0;
839 	offset = IPA_REG_ENDP_INIT_HOL_BLOCK_EN_N_OFFSET(endpoint_id);
840 	iowrite32(val, endpoint->ipa->reg_virt + offset);
841 }
842 
843 void ipa_endpoint_modem_hol_block_clear_all(struct ipa *ipa)
844 {
845 	u32 i;
846 
847 	for (i = 0; i < IPA_ENDPOINT_MAX; i++) {
848 		struct ipa_endpoint *endpoint = &ipa->endpoint[i];
849 
850 		if (endpoint->toward_ipa || endpoint->ee_id != GSI_EE_MODEM)
851 			continue;
852 
853 		ipa_endpoint_init_hol_block_timer(endpoint, 0);
854 		ipa_endpoint_init_hol_block_enable(endpoint, true);
855 	}
856 }
857 
858 static void ipa_endpoint_init_deaggr(struct ipa_endpoint *endpoint)
859 {
860 	u32 offset = IPA_REG_ENDP_INIT_DEAGGR_N_OFFSET(endpoint->endpoint_id);
861 	u32 val = 0;
862 
863 	if (!endpoint->toward_ipa)
864 		return;		/* Register not valid for RX endpoints */
865 
866 	/* DEAGGR_HDR_LEN is 0 */
867 	/* PACKET_OFFSET_VALID is 0 */
868 	/* PACKET_OFFSET_LOCATION is ignored (not valid) */
869 	/* MAX_PACKET_LEN is 0 (not enforced) */
870 
871 	iowrite32(val, endpoint->ipa->reg_virt + offset);
872 }
873 
874 static void ipa_endpoint_init_rsrc_grp(struct ipa_endpoint *endpoint)
875 {
876 	u32 offset = IPA_REG_ENDP_INIT_RSRC_GRP_N_OFFSET(endpoint->endpoint_id);
877 	struct ipa *ipa = endpoint->ipa;
878 	u32 val;
879 
880 	val = rsrc_grp_encoded(ipa->version, endpoint->data->resource_group);
881 	iowrite32(val, ipa->reg_virt + offset);
882 }
883 
884 static void ipa_endpoint_init_seq(struct ipa_endpoint *endpoint)
885 {
886 	u32 offset = IPA_REG_ENDP_INIT_SEQ_N_OFFSET(endpoint->endpoint_id);
887 	u32 val = 0;
888 
889 	if (!endpoint->toward_ipa)
890 		return;		/* Register not valid for RX endpoints */
891 
892 	/* Low-order byte configures primary packet processing */
893 	val |= u32_encode_bits(endpoint->data->tx.seq_type, SEQ_TYPE_FMASK);
894 
895 	/* Second byte configures replicated packet processing */
896 	val |= u32_encode_bits(endpoint->data->tx.seq_rep_type,
897 			       SEQ_REP_TYPE_FMASK);
898 
899 	iowrite32(val, endpoint->ipa->reg_virt + offset);
900 }
901 
902 /**
903  * ipa_endpoint_skb_tx() - Transmit a socket buffer
904  * @endpoint:	Endpoint pointer
905  * @skb:	Socket buffer to send
906  *
907  * Returns:	0 if successful, or a negative error code
908  */
909 int ipa_endpoint_skb_tx(struct ipa_endpoint *endpoint, struct sk_buff *skb)
910 {
911 	struct gsi_trans *trans;
912 	u32 nr_frags;
913 	int ret;
914 
915 	/* Make sure source endpoint's TLV FIFO has enough entries to
916 	 * hold the linear portion of the skb and all its fragments.
917 	 * If not, see if we can linearize it before giving up.
918 	 */
919 	nr_frags = skb_shinfo(skb)->nr_frags;
920 	if (1 + nr_frags > endpoint->trans_tre_max) {
921 		if (skb_linearize(skb))
922 			return -E2BIG;
923 		nr_frags = 0;
924 	}
925 
926 	trans = ipa_endpoint_trans_alloc(endpoint, 1 + nr_frags);
927 	if (!trans)
928 		return -EBUSY;
929 
930 	ret = gsi_trans_skb_add(trans, skb);
931 	if (ret)
932 		goto err_trans_free;
933 	trans->data = skb;	/* transaction owns skb now */
934 
935 	gsi_trans_commit(trans, !netdev_xmit_more());
936 
937 	return 0;
938 
939 err_trans_free:
940 	gsi_trans_free(trans);
941 
942 	return -ENOMEM;
943 }
944 
945 static void ipa_endpoint_status(struct ipa_endpoint *endpoint)
946 {
947 	u32 endpoint_id = endpoint->endpoint_id;
948 	struct ipa *ipa = endpoint->ipa;
949 	u32 val = 0;
950 	u32 offset;
951 
952 	offset = IPA_REG_ENDP_STATUS_N_OFFSET(endpoint_id);
953 
954 	if (endpoint->data->status_enable) {
955 		val |= STATUS_EN_FMASK;
956 		if (endpoint->toward_ipa) {
957 			enum ipa_endpoint_name name;
958 			u32 status_endpoint_id;
959 
960 			name = endpoint->data->tx.status_endpoint;
961 			status_endpoint_id = ipa->name_map[name]->endpoint_id;
962 
963 			val |= u32_encode_bits(status_endpoint_id,
964 					       STATUS_ENDP_FMASK);
965 		}
966 		/* STATUS_LOCATION is 0, meaning status element precedes
967 		 * packet (not present for IPA v4.5)
968 		 */
969 		/* STATUS_PKT_SUPPRESS_FMASK is 0 (not present for v3.5.1) */
970 	}
971 
972 	iowrite32(val, ipa->reg_virt + offset);
973 }
974 
975 static int ipa_endpoint_replenish_one(struct ipa_endpoint *endpoint)
976 {
977 	struct gsi_trans *trans;
978 	bool doorbell = false;
979 	struct page *page;
980 	u32 offset;
981 	u32 len;
982 	int ret;
983 
984 	page = dev_alloc_pages(get_order(IPA_RX_BUFFER_SIZE));
985 	if (!page)
986 		return -ENOMEM;
987 
988 	trans = ipa_endpoint_trans_alloc(endpoint, 1);
989 	if (!trans)
990 		goto err_free_pages;
991 
992 	/* Offset the buffer to make space for skb headroom */
993 	offset = NET_SKB_PAD;
994 	len = IPA_RX_BUFFER_SIZE - offset;
995 
996 	ret = gsi_trans_page_add(trans, page, len, offset);
997 	if (ret)
998 		goto err_trans_free;
999 	trans->data = page;	/* transaction owns page now */
1000 
1001 	if (++endpoint->replenish_ready == IPA_REPLENISH_BATCH) {
1002 		doorbell = true;
1003 		endpoint->replenish_ready = 0;
1004 	}
1005 
1006 	gsi_trans_commit(trans, doorbell);
1007 
1008 	return 0;
1009 
1010 err_trans_free:
1011 	gsi_trans_free(trans);
1012 err_free_pages:
1013 	__free_pages(page, get_order(IPA_RX_BUFFER_SIZE));
1014 
1015 	return -ENOMEM;
1016 }
1017 
1018 /**
1019  * ipa_endpoint_replenish() - Replenish endpoint receive buffers
1020  * @endpoint:	Endpoint to be replenished
1021  * @add_one:	Whether this is replacing a just-consumed buffer
1022  *
1023  * The IPA hardware can hold a fixed number of receive buffers for an RX
1024  * endpoint, based on the number of entries in the underlying channel ring
1025  * buffer.  If an endpoint's "backlog" is non-zero, it indicates how many
1026  * more receive buffers can be supplied to the hardware.  Replenishing for
1027  * an endpoint can be disabled, in which case requests to replenish a
1028  * buffer are "saved", and transferred to the backlog once it is re-enabled
1029  * again.
1030  */
1031 static void ipa_endpoint_replenish(struct ipa_endpoint *endpoint, bool add_one)
1032 {
1033 	struct gsi *gsi;
1034 	u32 backlog;
1035 
1036 	if (!endpoint->replenish_enabled) {
1037 		if (add_one)
1038 			atomic_inc(&endpoint->replenish_saved);
1039 		return;
1040 	}
1041 
1042 	while (atomic_dec_not_zero(&endpoint->replenish_backlog))
1043 		if (ipa_endpoint_replenish_one(endpoint))
1044 			goto try_again_later;
1045 	if (add_one)
1046 		atomic_inc(&endpoint->replenish_backlog);
1047 
1048 	return;
1049 
1050 try_again_later:
1051 	/* The last one didn't succeed, so fix the backlog */
1052 	backlog = atomic_inc_return(&endpoint->replenish_backlog);
1053 
1054 	if (add_one)
1055 		atomic_inc(&endpoint->replenish_backlog);
1056 
1057 	/* Whenever a receive buffer transaction completes we'll try to
1058 	 * replenish again.  It's unlikely, but if we fail to supply even
1059 	 * one buffer, nothing will trigger another replenish attempt.
1060 	 * Receive buffer transactions use one TRE, so schedule work to
1061 	 * try replenishing again if our backlog is *all* available TREs.
1062 	 */
1063 	gsi = &endpoint->ipa->gsi;
1064 	if (backlog == gsi_channel_tre_max(gsi, endpoint->channel_id))
1065 		schedule_delayed_work(&endpoint->replenish_work,
1066 				      msecs_to_jiffies(1));
1067 }
1068 
1069 static void ipa_endpoint_replenish_enable(struct ipa_endpoint *endpoint)
1070 {
1071 	struct gsi *gsi = &endpoint->ipa->gsi;
1072 	u32 max_backlog;
1073 	u32 saved;
1074 
1075 	endpoint->replenish_enabled = true;
1076 	while ((saved = atomic_xchg(&endpoint->replenish_saved, 0)))
1077 		atomic_add(saved, &endpoint->replenish_backlog);
1078 
1079 	/* Start replenishing if hardware currently has no buffers */
1080 	max_backlog = gsi_channel_tre_max(gsi, endpoint->channel_id);
1081 	if (atomic_read(&endpoint->replenish_backlog) == max_backlog)
1082 		ipa_endpoint_replenish(endpoint, false);
1083 }
1084 
1085 static void ipa_endpoint_replenish_disable(struct ipa_endpoint *endpoint)
1086 {
1087 	u32 backlog;
1088 
1089 	endpoint->replenish_enabled = false;
1090 	while ((backlog = atomic_xchg(&endpoint->replenish_backlog, 0)))
1091 		atomic_add(backlog, &endpoint->replenish_saved);
1092 }
1093 
1094 static void ipa_endpoint_replenish_work(struct work_struct *work)
1095 {
1096 	struct delayed_work *dwork = to_delayed_work(work);
1097 	struct ipa_endpoint *endpoint;
1098 
1099 	endpoint = container_of(dwork, struct ipa_endpoint, replenish_work);
1100 
1101 	ipa_endpoint_replenish(endpoint, false);
1102 }
1103 
1104 static void ipa_endpoint_skb_copy(struct ipa_endpoint *endpoint,
1105 				  void *data, u32 len, u32 extra)
1106 {
1107 	struct sk_buff *skb;
1108 
1109 	skb = __dev_alloc_skb(len, GFP_ATOMIC);
1110 	if (skb) {
1111 		skb_put(skb, len);
1112 		memcpy(skb->data, data, len);
1113 		skb->truesize += extra;
1114 	}
1115 
1116 	/* Now receive it, or drop it if there's no netdev */
1117 	if (endpoint->netdev)
1118 		ipa_modem_skb_rx(endpoint->netdev, skb);
1119 	else if (skb)
1120 		dev_kfree_skb_any(skb);
1121 }
1122 
1123 static bool ipa_endpoint_skb_build(struct ipa_endpoint *endpoint,
1124 				   struct page *page, u32 len)
1125 {
1126 	struct sk_buff *skb;
1127 
1128 	/* Nothing to do if there's no netdev */
1129 	if (!endpoint->netdev)
1130 		return false;
1131 
1132 	/* assert(len <= SKB_WITH_OVERHEAD(IPA_RX_BUFFER_SIZE-NET_SKB_PAD)); */
1133 	skb = build_skb(page_address(page), IPA_RX_BUFFER_SIZE);
1134 	if (skb) {
1135 		/* Reserve the headroom and account for the data */
1136 		skb_reserve(skb, NET_SKB_PAD);
1137 		skb_put(skb, len);
1138 	}
1139 
1140 	/* Receive the buffer (or record drop if unable to build it) */
1141 	ipa_modem_skb_rx(endpoint->netdev, skb);
1142 
1143 	return skb != NULL;
1144 }
1145 
1146 /* The format of a packet status element is the same for several status
1147  * types (opcodes).  Other types aren't currently supported.
1148  */
1149 static bool ipa_status_format_packet(enum ipa_status_opcode opcode)
1150 {
1151 	switch (opcode) {
1152 	case IPA_STATUS_OPCODE_PACKET:
1153 	case IPA_STATUS_OPCODE_DROPPED_PACKET:
1154 	case IPA_STATUS_OPCODE_SUSPENDED_PACKET:
1155 	case IPA_STATUS_OPCODE_PACKET_2ND_PASS:
1156 		return true;
1157 	default:
1158 		return false;
1159 	}
1160 }
1161 
1162 static bool ipa_endpoint_status_skip(struct ipa_endpoint *endpoint,
1163 				     const struct ipa_status *status)
1164 {
1165 	u32 endpoint_id;
1166 
1167 	if (!ipa_status_format_packet(status->opcode))
1168 		return true;
1169 	if (!status->pkt_len)
1170 		return true;
1171 	endpoint_id = u8_get_bits(status->endp_dst_idx,
1172 				  IPA_STATUS_DST_IDX_FMASK);
1173 	if (endpoint_id != endpoint->endpoint_id)
1174 		return true;
1175 
1176 	return false;	/* Don't skip this packet, process it */
1177 }
1178 
1179 static bool ipa_endpoint_status_tag(struct ipa_endpoint *endpoint,
1180 				    const struct ipa_status *status)
1181 {
1182 	struct ipa_endpoint *command_endpoint;
1183 	struct ipa *ipa = endpoint->ipa;
1184 	u32 endpoint_id;
1185 
1186 	if (!le16_get_bits(status->mask, IPA_STATUS_MASK_TAG_VALID_FMASK))
1187 		return false;	/* No valid tag */
1188 
1189 	/* The status contains a valid tag.  We know the packet was sent to
1190 	 * this endpoint (already verified by ipa_endpoint_status_skip()).
1191 	 * If the packet came from the AP->command TX endpoint we know
1192 	 * this packet was sent as part of the pipeline clear process.
1193 	 */
1194 	endpoint_id = u8_get_bits(status->endp_src_idx,
1195 				  IPA_STATUS_SRC_IDX_FMASK);
1196 	command_endpoint = ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX];
1197 	if (endpoint_id == command_endpoint->endpoint_id) {
1198 		complete(&ipa->completion);
1199 	} else {
1200 		dev_err(&ipa->pdev->dev,
1201 			"unexpected tagged packet from endpoint %u\n",
1202 			endpoint_id);
1203 	}
1204 
1205 	return true;
1206 }
1207 
1208 /* Return whether the status indicates the packet should be dropped */
1209 static bool ipa_endpoint_status_drop(struct ipa_endpoint *endpoint,
1210 				     const struct ipa_status *status)
1211 {
1212 	u32 val;
1213 
1214 	/* If the status indicates a tagged transfer, we'll drop the packet */
1215 	if (ipa_endpoint_status_tag(endpoint, status))
1216 		return true;
1217 
1218 	/* Deaggregation exceptions we drop; all other types we consume */
1219 	if (status->exception)
1220 		return status->exception == IPA_STATUS_EXCEPTION_DEAGGR;
1221 
1222 	/* Drop the packet if it fails to match a routing rule; otherwise no */
1223 	val = le32_get_bits(status->flags1, IPA_STATUS_FLAGS1_RT_RULE_ID_FMASK);
1224 
1225 	return val == field_max(IPA_STATUS_FLAGS1_RT_RULE_ID_FMASK);
1226 }
1227 
1228 static void ipa_endpoint_status_parse(struct ipa_endpoint *endpoint,
1229 				      struct page *page, u32 total_len)
1230 {
1231 	void *data = page_address(page) + NET_SKB_PAD;
1232 	u32 unused = IPA_RX_BUFFER_SIZE - total_len;
1233 	u32 resid = total_len;
1234 
1235 	while (resid) {
1236 		const struct ipa_status *status = data;
1237 		u32 align;
1238 		u32 len;
1239 
1240 		if (resid < sizeof(*status)) {
1241 			dev_err(&endpoint->ipa->pdev->dev,
1242 				"short message (%u bytes < %zu byte status)\n",
1243 				resid, sizeof(*status));
1244 			break;
1245 		}
1246 
1247 		/* Skip over status packets that lack packet data */
1248 		if (ipa_endpoint_status_skip(endpoint, status)) {
1249 			data += sizeof(*status);
1250 			resid -= sizeof(*status);
1251 			continue;
1252 		}
1253 
1254 		/* Compute the amount of buffer space consumed by the packet,
1255 		 * including the status element.  If the hardware is configured
1256 		 * to pad packet data to an aligned boundary, account for that.
1257 		 * And if checksum offload is enabled a trailer containing
1258 		 * computed checksum information will be appended.
1259 		 */
1260 		align = endpoint->data->rx.pad_align ? : 1;
1261 		len = le16_to_cpu(status->pkt_len);
1262 		len = sizeof(*status) + ALIGN(len, align);
1263 		if (endpoint->data->checksum)
1264 			len += sizeof(struct rmnet_map_dl_csum_trailer);
1265 
1266 		if (!ipa_endpoint_status_drop(endpoint, status)) {
1267 			void *data2;
1268 			u32 extra;
1269 			u32 len2;
1270 
1271 			/* Client receives only packet data (no status) */
1272 			data2 = data + sizeof(*status);
1273 			len2 = le16_to_cpu(status->pkt_len);
1274 
1275 			/* Have the true size reflect the extra unused space in
1276 			 * the original receive buffer.  Distribute the "cost"
1277 			 * proportionately across all aggregated packets in the
1278 			 * buffer.
1279 			 */
1280 			extra = DIV_ROUND_CLOSEST(unused * len, total_len);
1281 			ipa_endpoint_skb_copy(endpoint, data2, len2, extra);
1282 		}
1283 
1284 		/* Consume status and the full packet it describes */
1285 		data += len;
1286 		resid -= len;
1287 	}
1288 }
1289 
1290 /* Complete a TX transaction, command or from ipa_endpoint_skb_tx() */
1291 static void ipa_endpoint_tx_complete(struct ipa_endpoint *endpoint,
1292 				     struct gsi_trans *trans)
1293 {
1294 }
1295 
1296 /* Complete transaction initiated in ipa_endpoint_replenish_one() */
1297 static void ipa_endpoint_rx_complete(struct ipa_endpoint *endpoint,
1298 				     struct gsi_trans *trans)
1299 {
1300 	struct page *page;
1301 
1302 	ipa_endpoint_replenish(endpoint, true);
1303 
1304 	if (trans->cancelled)
1305 		return;
1306 
1307 	/* Parse or build a socket buffer using the actual received length */
1308 	page = trans->data;
1309 	if (endpoint->data->status_enable)
1310 		ipa_endpoint_status_parse(endpoint, page, trans->len);
1311 	else if (ipa_endpoint_skb_build(endpoint, page, trans->len))
1312 		trans->data = NULL;	/* Pages have been consumed */
1313 }
1314 
1315 void ipa_endpoint_trans_complete(struct ipa_endpoint *endpoint,
1316 				 struct gsi_trans *trans)
1317 {
1318 	if (endpoint->toward_ipa)
1319 		ipa_endpoint_tx_complete(endpoint, trans);
1320 	else
1321 		ipa_endpoint_rx_complete(endpoint, trans);
1322 }
1323 
1324 void ipa_endpoint_trans_release(struct ipa_endpoint *endpoint,
1325 				struct gsi_trans *trans)
1326 {
1327 	if (endpoint->toward_ipa) {
1328 		struct ipa *ipa = endpoint->ipa;
1329 
1330 		/* Nothing to do for command transactions */
1331 		if (endpoint != ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]) {
1332 			struct sk_buff *skb = trans->data;
1333 
1334 			if (skb)
1335 				dev_kfree_skb_any(skb);
1336 		}
1337 	} else {
1338 		struct page *page = trans->data;
1339 
1340 		if (page)
1341 			__free_pages(page, get_order(IPA_RX_BUFFER_SIZE));
1342 	}
1343 }
1344 
1345 void ipa_endpoint_default_route_set(struct ipa *ipa, u32 endpoint_id)
1346 {
1347 	u32 val;
1348 
1349 	/* ROUTE_DIS is 0 */
1350 	val = u32_encode_bits(endpoint_id, ROUTE_DEF_PIPE_FMASK);
1351 	val |= ROUTE_DEF_HDR_TABLE_FMASK;
1352 	val |= u32_encode_bits(0, ROUTE_DEF_HDR_OFST_FMASK);
1353 	val |= u32_encode_bits(endpoint_id, ROUTE_FRAG_DEF_PIPE_FMASK);
1354 	val |= ROUTE_DEF_RETAIN_HDR_FMASK;
1355 
1356 	iowrite32(val, ipa->reg_virt + IPA_REG_ROUTE_OFFSET);
1357 }
1358 
1359 void ipa_endpoint_default_route_clear(struct ipa *ipa)
1360 {
1361 	ipa_endpoint_default_route_set(ipa, 0);
1362 }
1363 
1364 /**
1365  * ipa_endpoint_reset_rx_aggr() - Reset RX endpoint with aggregation active
1366  * @endpoint:	Endpoint to be reset
1367  *
1368  * If aggregation is active on an RX endpoint when a reset is performed
1369  * on its underlying GSI channel, a special sequence of actions must be
1370  * taken to ensure the IPA pipeline is properly cleared.
1371  *
1372  * Return:	0 if successful, or a negative error code
1373  */
1374 static int ipa_endpoint_reset_rx_aggr(struct ipa_endpoint *endpoint)
1375 {
1376 	struct device *dev = &endpoint->ipa->pdev->dev;
1377 	struct ipa *ipa = endpoint->ipa;
1378 	struct gsi *gsi = &ipa->gsi;
1379 	bool suspended = false;
1380 	dma_addr_t addr;
1381 	u32 retries;
1382 	u32 len = 1;
1383 	void *virt;
1384 	int ret;
1385 
1386 	virt = kzalloc(len, GFP_KERNEL);
1387 	if (!virt)
1388 		return -ENOMEM;
1389 
1390 	addr = dma_map_single(dev, virt, len, DMA_FROM_DEVICE);
1391 	if (dma_mapping_error(dev, addr)) {
1392 		ret = -ENOMEM;
1393 		goto out_kfree;
1394 	}
1395 
1396 	/* Force close aggregation before issuing the reset */
1397 	ipa_endpoint_force_close(endpoint);
1398 
1399 	/* Reset and reconfigure the channel with the doorbell engine
1400 	 * disabled.  Then poll until we know aggregation is no longer
1401 	 * active.  We'll re-enable the doorbell (if appropriate) when
1402 	 * we reset again below.
1403 	 */
1404 	gsi_channel_reset(gsi, endpoint->channel_id, false);
1405 
1406 	/* Make sure the channel isn't suspended */
1407 	suspended = ipa_endpoint_program_suspend(endpoint, false);
1408 
1409 	/* Start channel and do a 1 byte read */
1410 	ret = gsi_channel_start(gsi, endpoint->channel_id);
1411 	if (ret)
1412 		goto out_suspend_again;
1413 
1414 	ret = gsi_trans_read_byte(gsi, endpoint->channel_id, addr);
1415 	if (ret)
1416 		goto err_endpoint_stop;
1417 
1418 	/* Wait for aggregation to be closed on the channel */
1419 	retries = IPA_ENDPOINT_RESET_AGGR_RETRY_MAX;
1420 	do {
1421 		if (!ipa_endpoint_aggr_active(endpoint))
1422 			break;
1423 		usleep_range(USEC_PER_MSEC, 2 * USEC_PER_MSEC);
1424 	} while (retries--);
1425 
1426 	/* Check one last time */
1427 	if (ipa_endpoint_aggr_active(endpoint))
1428 		dev_err(dev, "endpoint %u still active during reset\n",
1429 			endpoint->endpoint_id);
1430 
1431 	gsi_trans_read_byte_done(gsi, endpoint->channel_id);
1432 
1433 	ret = gsi_channel_stop(gsi, endpoint->channel_id);
1434 	if (ret)
1435 		goto out_suspend_again;
1436 
1437 	/* Finally, reset and reconfigure the channel again (re-enabling the
1438 	 * the doorbell engine if appropriate).  Sleep for 1 millisecond to
1439 	 * complete the channel reset sequence.  Finish by suspending the
1440 	 * channel again (if necessary).
1441 	 */
1442 	gsi_channel_reset(gsi, endpoint->channel_id, true);
1443 
1444 	usleep_range(USEC_PER_MSEC, 2 * USEC_PER_MSEC);
1445 
1446 	goto out_suspend_again;
1447 
1448 err_endpoint_stop:
1449 	(void)gsi_channel_stop(gsi, endpoint->channel_id);
1450 out_suspend_again:
1451 	if (suspended)
1452 		(void)ipa_endpoint_program_suspend(endpoint, true);
1453 	dma_unmap_single(dev, addr, len, DMA_FROM_DEVICE);
1454 out_kfree:
1455 	kfree(virt);
1456 
1457 	return ret;
1458 }
1459 
1460 static void ipa_endpoint_reset(struct ipa_endpoint *endpoint)
1461 {
1462 	u32 channel_id = endpoint->channel_id;
1463 	struct ipa *ipa = endpoint->ipa;
1464 	bool special;
1465 	int ret = 0;
1466 
1467 	/* On IPA v3.5.1, if an RX endpoint is reset while aggregation
1468 	 * is active, we need to handle things specially to recover.
1469 	 * All other cases just need to reset the underlying GSI channel.
1470 	 */
1471 	special = ipa->version == IPA_VERSION_3_5_1 &&
1472 			!endpoint->toward_ipa &&
1473 			endpoint->data->aggregation;
1474 	if (special && ipa_endpoint_aggr_active(endpoint))
1475 		ret = ipa_endpoint_reset_rx_aggr(endpoint);
1476 	else
1477 		gsi_channel_reset(&ipa->gsi, channel_id, true);
1478 
1479 	if (ret)
1480 		dev_err(&ipa->pdev->dev,
1481 			"error %d resetting channel %u for endpoint %u\n",
1482 			ret, endpoint->channel_id, endpoint->endpoint_id);
1483 }
1484 
1485 static void ipa_endpoint_program(struct ipa_endpoint *endpoint)
1486 {
1487 	if (endpoint->toward_ipa)
1488 		ipa_endpoint_program_delay(endpoint, false);
1489 	else
1490 		(void)ipa_endpoint_program_suspend(endpoint, false);
1491 	ipa_endpoint_init_cfg(endpoint);
1492 	ipa_endpoint_init_hdr(endpoint);
1493 	ipa_endpoint_init_hdr_ext(endpoint);
1494 	ipa_endpoint_init_hdr_metadata_mask(endpoint);
1495 	ipa_endpoint_init_mode(endpoint);
1496 	ipa_endpoint_init_aggr(endpoint);
1497 	ipa_endpoint_init_deaggr(endpoint);
1498 	ipa_endpoint_init_rsrc_grp(endpoint);
1499 	ipa_endpoint_init_seq(endpoint);
1500 	ipa_endpoint_status(endpoint);
1501 }
1502 
1503 int ipa_endpoint_enable_one(struct ipa_endpoint *endpoint)
1504 {
1505 	struct ipa *ipa = endpoint->ipa;
1506 	struct gsi *gsi = &ipa->gsi;
1507 	int ret;
1508 
1509 	ret = gsi_channel_start(gsi, endpoint->channel_id);
1510 	if (ret) {
1511 		dev_err(&ipa->pdev->dev,
1512 			"error %d starting %cX channel %u for endpoint %u\n",
1513 			ret, endpoint->toward_ipa ? 'T' : 'R',
1514 			endpoint->channel_id, endpoint->endpoint_id);
1515 		return ret;
1516 	}
1517 
1518 	if (!endpoint->toward_ipa) {
1519 		ipa_interrupt_suspend_enable(ipa->interrupt,
1520 					     endpoint->endpoint_id);
1521 		ipa_endpoint_replenish_enable(endpoint);
1522 	}
1523 
1524 	ipa->enabled |= BIT(endpoint->endpoint_id);
1525 
1526 	return 0;
1527 }
1528 
1529 void ipa_endpoint_disable_one(struct ipa_endpoint *endpoint)
1530 {
1531 	u32 mask = BIT(endpoint->endpoint_id);
1532 	struct ipa *ipa = endpoint->ipa;
1533 	struct gsi *gsi = &ipa->gsi;
1534 	int ret;
1535 
1536 	if (!(ipa->enabled & mask))
1537 		return;
1538 
1539 	ipa->enabled ^= mask;
1540 
1541 	if (!endpoint->toward_ipa) {
1542 		ipa_endpoint_replenish_disable(endpoint);
1543 		ipa_interrupt_suspend_disable(ipa->interrupt,
1544 					      endpoint->endpoint_id);
1545 	}
1546 
1547 	/* Note that if stop fails, the channel's state is not well-defined */
1548 	ret = gsi_channel_stop(gsi, endpoint->channel_id);
1549 	if (ret)
1550 		dev_err(&ipa->pdev->dev,
1551 			"error %d attempting to stop endpoint %u\n", ret,
1552 			endpoint->endpoint_id);
1553 }
1554 
1555 void ipa_endpoint_suspend_one(struct ipa_endpoint *endpoint)
1556 {
1557 	struct device *dev = &endpoint->ipa->pdev->dev;
1558 	struct gsi *gsi = &endpoint->ipa->gsi;
1559 	bool stop_channel;
1560 	int ret;
1561 
1562 	if (!(endpoint->ipa->enabled & BIT(endpoint->endpoint_id)))
1563 		return;
1564 
1565 	if (!endpoint->toward_ipa) {
1566 		ipa_endpoint_replenish_disable(endpoint);
1567 		(void)ipa_endpoint_program_suspend(endpoint, true);
1568 	}
1569 
1570 	/* IPA v3.5.1 doesn't use channel stop for suspend */
1571 	stop_channel = endpoint->ipa->version != IPA_VERSION_3_5_1;
1572 	ret = gsi_channel_suspend(gsi, endpoint->channel_id, stop_channel);
1573 	if (ret)
1574 		dev_err(dev, "error %d suspending channel %u\n", ret,
1575 			endpoint->channel_id);
1576 }
1577 
1578 void ipa_endpoint_resume_one(struct ipa_endpoint *endpoint)
1579 {
1580 	struct device *dev = &endpoint->ipa->pdev->dev;
1581 	struct gsi *gsi = &endpoint->ipa->gsi;
1582 	bool start_channel;
1583 	int ret;
1584 
1585 	if (!(endpoint->ipa->enabled & BIT(endpoint->endpoint_id)))
1586 		return;
1587 
1588 	if (!endpoint->toward_ipa)
1589 		(void)ipa_endpoint_program_suspend(endpoint, false);
1590 
1591 	/* IPA v3.5.1 doesn't use channel start for resume */
1592 	start_channel = endpoint->ipa->version != IPA_VERSION_3_5_1;
1593 	ret = gsi_channel_resume(gsi, endpoint->channel_id, start_channel);
1594 	if (ret)
1595 		dev_err(dev, "error %d resuming channel %u\n", ret,
1596 			endpoint->channel_id);
1597 	else if (!endpoint->toward_ipa)
1598 		ipa_endpoint_replenish_enable(endpoint);
1599 }
1600 
1601 void ipa_endpoint_suspend(struct ipa *ipa)
1602 {
1603 	if (!ipa->setup_complete)
1604 		return;
1605 
1606 	if (ipa->modem_netdev)
1607 		ipa_modem_suspend(ipa->modem_netdev);
1608 
1609 	ipa_cmd_pipeline_clear(ipa);
1610 
1611 	ipa_endpoint_suspend_one(ipa->name_map[IPA_ENDPOINT_AP_LAN_RX]);
1612 	ipa_endpoint_suspend_one(ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]);
1613 }
1614 
1615 void ipa_endpoint_resume(struct ipa *ipa)
1616 {
1617 	if (!ipa->setup_complete)
1618 		return;
1619 
1620 	ipa_endpoint_resume_one(ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]);
1621 	ipa_endpoint_resume_one(ipa->name_map[IPA_ENDPOINT_AP_LAN_RX]);
1622 
1623 	if (ipa->modem_netdev)
1624 		ipa_modem_resume(ipa->modem_netdev);
1625 }
1626 
1627 static void ipa_endpoint_setup_one(struct ipa_endpoint *endpoint)
1628 {
1629 	struct gsi *gsi = &endpoint->ipa->gsi;
1630 	u32 channel_id = endpoint->channel_id;
1631 
1632 	/* Only AP endpoints get set up */
1633 	if (endpoint->ee_id != GSI_EE_AP)
1634 		return;
1635 
1636 	endpoint->trans_tre_max = gsi_channel_trans_tre_max(gsi, channel_id);
1637 	if (!endpoint->toward_ipa) {
1638 		/* RX transactions require a single TRE, so the maximum
1639 		 * backlog is the same as the maximum outstanding TREs.
1640 		 */
1641 		endpoint->replenish_enabled = false;
1642 		atomic_set(&endpoint->replenish_saved,
1643 			   gsi_channel_tre_max(gsi, endpoint->channel_id));
1644 		atomic_set(&endpoint->replenish_backlog, 0);
1645 		INIT_DELAYED_WORK(&endpoint->replenish_work,
1646 				  ipa_endpoint_replenish_work);
1647 	}
1648 
1649 	ipa_endpoint_program(endpoint);
1650 
1651 	endpoint->ipa->set_up |= BIT(endpoint->endpoint_id);
1652 }
1653 
1654 static void ipa_endpoint_teardown_one(struct ipa_endpoint *endpoint)
1655 {
1656 	endpoint->ipa->set_up &= ~BIT(endpoint->endpoint_id);
1657 
1658 	if (!endpoint->toward_ipa)
1659 		cancel_delayed_work_sync(&endpoint->replenish_work);
1660 
1661 	ipa_endpoint_reset(endpoint);
1662 }
1663 
1664 void ipa_endpoint_setup(struct ipa *ipa)
1665 {
1666 	u32 initialized = ipa->initialized;
1667 
1668 	ipa->set_up = 0;
1669 	while (initialized) {
1670 		u32 endpoint_id = __ffs(initialized);
1671 
1672 		initialized ^= BIT(endpoint_id);
1673 
1674 		ipa_endpoint_setup_one(&ipa->endpoint[endpoint_id]);
1675 	}
1676 }
1677 
1678 void ipa_endpoint_teardown(struct ipa *ipa)
1679 {
1680 	u32 set_up = ipa->set_up;
1681 
1682 	while (set_up) {
1683 		u32 endpoint_id = __fls(set_up);
1684 
1685 		set_up ^= BIT(endpoint_id);
1686 
1687 		ipa_endpoint_teardown_one(&ipa->endpoint[endpoint_id]);
1688 	}
1689 	ipa->set_up = 0;
1690 }
1691 
1692 int ipa_endpoint_config(struct ipa *ipa)
1693 {
1694 	struct device *dev = &ipa->pdev->dev;
1695 	u32 initialized;
1696 	u32 rx_base;
1697 	u32 rx_mask;
1698 	u32 tx_mask;
1699 	int ret = 0;
1700 	u32 max;
1701 	u32 val;
1702 
1703 	/* Find out about the endpoints supplied by the hardware, and ensure
1704 	 * the highest one doesn't exceed the number we support.
1705 	 */
1706 	val = ioread32(ipa->reg_virt + IPA_REG_FLAVOR_0_OFFSET);
1707 
1708 	/* Our RX is an IPA producer */
1709 	rx_base = u32_get_bits(val, IPA_PROD_LOWEST_FMASK);
1710 	max = rx_base + u32_get_bits(val, IPA_MAX_PROD_PIPES_FMASK);
1711 	if (max > IPA_ENDPOINT_MAX) {
1712 		dev_err(dev, "too many endpoints (%u > %u)\n",
1713 			max, IPA_ENDPOINT_MAX);
1714 		return -EINVAL;
1715 	}
1716 	rx_mask = GENMASK(max - 1, rx_base);
1717 
1718 	/* Our TX is an IPA consumer */
1719 	max = u32_get_bits(val, IPA_MAX_CONS_PIPES_FMASK);
1720 	tx_mask = GENMASK(max - 1, 0);
1721 
1722 	ipa->available = rx_mask | tx_mask;
1723 
1724 	/* Check for initialized endpoints not supported by the hardware */
1725 	if (ipa->initialized & ~ipa->available) {
1726 		dev_err(dev, "unavailable endpoint id(s) 0x%08x\n",
1727 			ipa->initialized & ~ipa->available);
1728 		ret = -EINVAL;		/* Report other errors too */
1729 	}
1730 
1731 	initialized = ipa->initialized;
1732 	while (initialized) {
1733 		u32 endpoint_id = __ffs(initialized);
1734 		struct ipa_endpoint *endpoint;
1735 
1736 		initialized ^= BIT(endpoint_id);
1737 
1738 		/* Make sure it's pointing in the right direction */
1739 		endpoint = &ipa->endpoint[endpoint_id];
1740 		if ((endpoint_id < rx_base) != !!endpoint->toward_ipa) {
1741 			dev_err(dev, "endpoint id %u wrong direction\n",
1742 				endpoint_id);
1743 			ret = -EINVAL;
1744 		}
1745 	}
1746 
1747 	return ret;
1748 }
1749 
1750 void ipa_endpoint_deconfig(struct ipa *ipa)
1751 {
1752 	ipa->available = 0;	/* Nothing more to do */
1753 }
1754 
1755 static void ipa_endpoint_init_one(struct ipa *ipa, enum ipa_endpoint_name name,
1756 				  const struct ipa_gsi_endpoint_data *data)
1757 {
1758 	struct ipa_endpoint *endpoint;
1759 
1760 	endpoint = &ipa->endpoint[data->endpoint_id];
1761 
1762 	if (data->ee_id == GSI_EE_AP)
1763 		ipa->channel_map[data->channel_id] = endpoint;
1764 	ipa->name_map[name] = endpoint;
1765 
1766 	endpoint->ipa = ipa;
1767 	endpoint->ee_id = data->ee_id;
1768 	endpoint->channel_id = data->channel_id;
1769 	endpoint->endpoint_id = data->endpoint_id;
1770 	endpoint->toward_ipa = data->toward_ipa;
1771 	endpoint->data = &data->endpoint.config;
1772 
1773 	ipa->initialized |= BIT(endpoint->endpoint_id);
1774 }
1775 
1776 void ipa_endpoint_exit_one(struct ipa_endpoint *endpoint)
1777 {
1778 	endpoint->ipa->initialized &= ~BIT(endpoint->endpoint_id);
1779 
1780 	memset(endpoint, 0, sizeof(*endpoint));
1781 }
1782 
1783 void ipa_endpoint_exit(struct ipa *ipa)
1784 {
1785 	u32 initialized = ipa->initialized;
1786 
1787 	while (initialized) {
1788 		u32 endpoint_id = __fls(initialized);
1789 
1790 		initialized ^= BIT(endpoint_id);
1791 
1792 		ipa_endpoint_exit_one(&ipa->endpoint[endpoint_id]);
1793 	}
1794 	memset(ipa->name_map, 0, sizeof(ipa->name_map));
1795 	memset(ipa->channel_map, 0, sizeof(ipa->channel_map));
1796 }
1797 
1798 /* Returns a bitmask of endpoints that support filtering, or 0 on error */
1799 u32 ipa_endpoint_init(struct ipa *ipa, u32 count,
1800 		      const struct ipa_gsi_endpoint_data *data)
1801 {
1802 	enum ipa_endpoint_name name;
1803 	u32 filter_map;
1804 
1805 	if (!ipa_endpoint_data_valid(ipa, count, data))
1806 		return 0;	/* Error */
1807 
1808 	ipa->initialized = 0;
1809 
1810 	filter_map = 0;
1811 	for (name = 0; name < count; name++, data++) {
1812 		if (ipa_gsi_endpoint_data_empty(data))
1813 			continue;	/* Skip over empty slots */
1814 
1815 		ipa_endpoint_init_one(ipa, name, data);
1816 
1817 		if (data->endpoint.filter_support)
1818 			filter_map |= BIT(data->endpoint_id);
1819 	}
1820 
1821 	if (!ipa_filter_map_valid(ipa, filter_map))
1822 		goto err_endpoint_exit;
1823 
1824 	return filter_map;	/* Non-zero bitmask */
1825 
1826 err_endpoint_exit:
1827 	ipa_endpoint_exit(ipa);
1828 
1829 	return 0;	/* Error */
1830 }
1831