xref: /openbmc/linux/drivers/net/ipa/ipa_endpoint.c (revision c606970d)
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 seq_type = endpoint->seq_type;
888 	u32 val = 0;
889 
890 	if (!endpoint->toward_ipa)
891 		return;		/* Register not valid for RX endpoints */
892 
893 	/* Sequencer type is made up of four nibbles */
894 	val |= u32_encode_bits(seq_type & 0xf, HPS_SEQ_TYPE_FMASK);
895 	val |= u32_encode_bits((seq_type >> 4) & 0xf, DPS_SEQ_TYPE_FMASK);
896 	/* The second two apply to replicated packets */
897 	val |= u32_encode_bits((seq_type >> 8) & 0xf, HPS_REP_SEQ_TYPE_FMASK);
898 	val |= u32_encode_bits((seq_type >> 12) & 0xf, DPS_REP_SEQ_TYPE_FMASK);
899 
900 	iowrite32(val, endpoint->ipa->reg_virt + offset);
901 }
902 
903 /**
904  * ipa_endpoint_skb_tx() - Transmit a socket buffer
905  * @endpoint:	Endpoint pointer
906  * @skb:	Socket buffer to send
907  *
908  * Returns:	0 if successful, or a negative error code
909  */
910 int ipa_endpoint_skb_tx(struct ipa_endpoint *endpoint, struct sk_buff *skb)
911 {
912 	struct gsi_trans *trans;
913 	u32 nr_frags;
914 	int ret;
915 
916 	/* Make sure source endpoint's TLV FIFO has enough entries to
917 	 * hold the linear portion of the skb and all its fragments.
918 	 * If not, see if we can linearize it before giving up.
919 	 */
920 	nr_frags = skb_shinfo(skb)->nr_frags;
921 	if (1 + nr_frags > endpoint->trans_tre_max) {
922 		if (skb_linearize(skb))
923 			return -E2BIG;
924 		nr_frags = 0;
925 	}
926 
927 	trans = ipa_endpoint_trans_alloc(endpoint, 1 + nr_frags);
928 	if (!trans)
929 		return -EBUSY;
930 
931 	ret = gsi_trans_skb_add(trans, skb);
932 	if (ret)
933 		goto err_trans_free;
934 	trans->data = skb;	/* transaction owns skb now */
935 
936 	gsi_trans_commit(trans, !netdev_xmit_more());
937 
938 	return 0;
939 
940 err_trans_free:
941 	gsi_trans_free(trans);
942 
943 	return -ENOMEM;
944 }
945 
946 static void ipa_endpoint_status(struct ipa_endpoint *endpoint)
947 {
948 	u32 endpoint_id = endpoint->endpoint_id;
949 	struct ipa *ipa = endpoint->ipa;
950 	u32 val = 0;
951 	u32 offset;
952 
953 	offset = IPA_REG_ENDP_STATUS_N_OFFSET(endpoint_id);
954 
955 	if (endpoint->data->status_enable) {
956 		val |= STATUS_EN_FMASK;
957 		if (endpoint->toward_ipa) {
958 			enum ipa_endpoint_name name;
959 			u32 status_endpoint_id;
960 
961 			name = endpoint->data->tx.status_endpoint;
962 			status_endpoint_id = ipa->name_map[name]->endpoint_id;
963 
964 			val |= u32_encode_bits(status_endpoint_id,
965 					       STATUS_ENDP_FMASK);
966 		}
967 		/* STATUS_LOCATION is 0, meaning status element precedes
968 		 * packet (not present for IPA v4.5)
969 		 */
970 		/* STATUS_PKT_SUPPRESS_FMASK is 0 (not present for v3.5.1) */
971 	}
972 
973 	iowrite32(val, ipa->reg_virt + offset);
974 }
975 
976 static int ipa_endpoint_replenish_one(struct ipa_endpoint *endpoint)
977 {
978 	struct gsi_trans *trans;
979 	bool doorbell = false;
980 	struct page *page;
981 	u32 offset;
982 	u32 len;
983 	int ret;
984 
985 	page = dev_alloc_pages(get_order(IPA_RX_BUFFER_SIZE));
986 	if (!page)
987 		return -ENOMEM;
988 
989 	trans = ipa_endpoint_trans_alloc(endpoint, 1);
990 	if (!trans)
991 		goto err_free_pages;
992 
993 	/* Offset the buffer to make space for skb headroom */
994 	offset = NET_SKB_PAD;
995 	len = IPA_RX_BUFFER_SIZE - offset;
996 
997 	ret = gsi_trans_page_add(trans, page, len, offset);
998 	if (ret)
999 		goto err_trans_free;
1000 	trans->data = page;	/* transaction owns page now */
1001 
1002 	if (++endpoint->replenish_ready == IPA_REPLENISH_BATCH) {
1003 		doorbell = true;
1004 		endpoint->replenish_ready = 0;
1005 	}
1006 
1007 	gsi_trans_commit(trans, doorbell);
1008 
1009 	return 0;
1010 
1011 err_trans_free:
1012 	gsi_trans_free(trans);
1013 err_free_pages:
1014 	__free_pages(page, get_order(IPA_RX_BUFFER_SIZE));
1015 
1016 	return -ENOMEM;
1017 }
1018 
1019 /**
1020  * ipa_endpoint_replenish() - Replenish endpoint receive buffers
1021  * @endpoint:	Endpoint to be replenished
1022  * @add_one:	Whether this is replacing a just-consumed buffer
1023  *
1024  * The IPA hardware can hold a fixed number of receive buffers for an RX
1025  * endpoint, based on the number of entries in the underlying channel ring
1026  * buffer.  If an endpoint's "backlog" is non-zero, it indicates how many
1027  * more receive buffers can be supplied to the hardware.  Replenishing for
1028  * an endpoint can be disabled, in which case requests to replenish a
1029  * buffer are "saved", and transferred to the backlog once it is re-enabled
1030  * again.
1031  */
1032 static void ipa_endpoint_replenish(struct ipa_endpoint *endpoint, bool add_one)
1033 {
1034 	struct gsi *gsi;
1035 	u32 backlog;
1036 
1037 	if (!endpoint->replenish_enabled) {
1038 		if (add_one)
1039 			atomic_inc(&endpoint->replenish_saved);
1040 		return;
1041 	}
1042 
1043 	while (atomic_dec_not_zero(&endpoint->replenish_backlog))
1044 		if (ipa_endpoint_replenish_one(endpoint))
1045 			goto try_again_later;
1046 	if (add_one)
1047 		atomic_inc(&endpoint->replenish_backlog);
1048 
1049 	return;
1050 
1051 try_again_later:
1052 	/* The last one didn't succeed, so fix the backlog */
1053 	backlog = atomic_inc_return(&endpoint->replenish_backlog);
1054 
1055 	if (add_one)
1056 		atomic_inc(&endpoint->replenish_backlog);
1057 
1058 	/* Whenever a receive buffer transaction completes we'll try to
1059 	 * replenish again.  It's unlikely, but if we fail to supply even
1060 	 * one buffer, nothing will trigger another replenish attempt.
1061 	 * Receive buffer transactions use one TRE, so schedule work to
1062 	 * try replenishing again if our backlog is *all* available TREs.
1063 	 */
1064 	gsi = &endpoint->ipa->gsi;
1065 	if (backlog == gsi_channel_tre_max(gsi, endpoint->channel_id))
1066 		schedule_delayed_work(&endpoint->replenish_work,
1067 				      msecs_to_jiffies(1));
1068 }
1069 
1070 static void ipa_endpoint_replenish_enable(struct ipa_endpoint *endpoint)
1071 {
1072 	struct gsi *gsi = &endpoint->ipa->gsi;
1073 	u32 max_backlog;
1074 	u32 saved;
1075 
1076 	endpoint->replenish_enabled = true;
1077 	while ((saved = atomic_xchg(&endpoint->replenish_saved, 0)))
1078 		atomic_add(saved, &endpoint->replenish_backlog);
1079 
1080 	/* Start replenishing if hardware currently has no buffers */
1081 	max_backlog = gsi_channel_tre_max(gsi, endpoint->channel_id);
1082 	if (atomic_read(&endpoint->replenish_backlog) == max_backlog)
1083 		ipa_endpoint_replenish(endpoint, false);
1084 }
1085 
1086 static void ipa_endpoint_replenish_disable(struct ipa_endpoint *endpoint)
1087 {
1088 	u32 backlog;
1089 
1090 	endpoint->replenish_enabled = false;
1091 	while ((backlog = atomic_xchg(&endpoint->replenish_backlog, 0)))
1092 		atomic_add(backlog, &endpoint->replenish_saved);
1093 }
1094 
1095 static void ipa_endpoint_replenish_work(struct work_struct *work)
1096 {
1097 	struct delayed_work *dwork = to_delayed_work(work);
1098 	struct ipa_endpoint *endpoint;
1099 
1100 	endpoint = container_of(dwork, struct ipa_endpoint, replenish_work);
1101 
1102 	ipa_endpoint_replenish(endpoint, false);
1103 }
1104 
1105 static void ipa_endpoint_skb_copy(struct ipa_endpoint *endpoint,
1106 				  void *data, u32 len, u32 extra)
1107 {
1108 	struct sk_buff *skb;
1109 
1110 	skb = __dev_alloc_skb(len, GFP_ATOMIC);
1111 	if (skb) {
1112 		skb_put(skb, len);
1113 		memcpy(skb->data, data, len);
1114 		skb->truesize += extra;
1115 	}
1116 
1117 	/* Now receive it, or drop it if there's no netdev */
1118 	if (endpoint->netdev)
1119 		ipa_modem_skb_rx(endpoint->netdev, skb);
1120 	else if (skb)
1121 		dev_kfree_skb_any(skb);
1122 }
1123 
1124 static bool ipa_endpoint_skb_build(struct ipa_endpoint *endpoint,
1125 				   struct page *page, u32 len)
1126 {
1127 	struct sk_buff *skb;
1128 
1129 	/* Nothing to do if there's no netdev */
1130 	if (!endpoint->netdev)
1131 		return false;
1132 
1133 	/* assert(len <= SKB_WITH_OVERHEAD(IPA_RX_BUFFER_SIZE-NET_SKB_PAD)); */
1134 	skb = build_skb(page_address(page), IPA_RX_BUFFER_SIZE);
1135 	if (skb) {
1136 		/* Reserve the headroom and account for the data */
1137 		skb_reserve(skb, NET_SKB_PAD);
1138 		skb_put(skb, len);
1139 	}
1140 
1141 	/* Receive the buffer (or record drop if unable to build it) */
1142 	ipa_modem_skb_rx(endpoint->netdev, skb);
1143 
1144 	return skb != NULL;
1145 }
1146 
1147 /* The format of a packet status element is the same for several status
1148  * types (opcodes).  Other types aren't currently supported.
1149  */
1150 static bool ipa_status_format_packet(enum ipa_status_opcode opcode)
1151 {
1152 	switch (opcode) {
1153 	case IPA_STATUS_OPCODE_PACKET:
1154 	case IPA_STATUS_OPCODE_DROPPED_PACKET:
1155 	case IPA_STATUS_OPCODE_SUSPENDED_PACKET:
1156 	case IPA_STATUS_OPCODE_PACKET_2ND_PASS:
1157 		return true;
1158 	default:
1159 		return false;
1160 	}
1161 }
1162 
1163 static bool ipa_endpoint_status_skip(struct ipa_endpoint *endpoint,
1164 				     const struct ipa_status *status)
1165 {
1166 	u32 endpoint_id;
1167 
1168 	if (!ipa_status_format_packet(status->opcode))
1169 		return true;
1170 	if (!status->pkt_len)
1171 		return true;
1172 	endpoint_id = u8_get_bits(status->endp_dst_idx,
1173 				  IPA_STATUS_DST_IDX_FMASK);
1174 	if (endpoint_id != endpoint->endpoint_id)
1175 		return true;
1176 
1177 	return false;	/* Don't skip this packet, process it */
1178 }
1179 
1180 static bool ipa_endpoint_status_tag(struct ipa_endpoint *endpoint,
1181 				    const struct ipa_status *status)
1182 {
1183 	struct ipa_endpoint *command_endpoint;
1184 	struct ipa *ipa = endpoint->ipa;
1185 	u32 endpoint_id;
1186 
1187 	if (!le16_get_bits(status->mask, IPA_STATUS_MASK_TAG_VALID_FMASK))
1188 		return false;	/* No valid tag */
1189 
1190 	/* The status contains a valid tag.  We know the packet was sent to
1191 	 * this endpoint (already verified by ipa_endpoint_status_skip()).
1192 	 * If the packet came from the AP->command TX endpoint we know
1193 	 * this packet was sent as part of the pipeline clear process.
1194 	 */
1195 	endpoint_id = u8_get_bits(status->endp_src_idx,
1196 				  IPA_STATUS_SRC_IDX_FMASK);
1197 	command_endpoint = ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX];
1198 	if (endpoint_id == command_endpoint->endpoint_id) {
1199 		complete(&ipa->completion);
1200 	} else {
1201 		dev_err(&ipa->pdev->dev,
1202 			"unexpected tagged packet from endpoint %u\n",
1203 			endpoint_id);
1204 	}
1205 
1206 	return true;
1207 }
1208 
1209 /* Return whether the status indicates the packet should be dropped */
1210 static bool ipa_endpoint_status_drop(struct ipa_endpoint *endpoint,
1211 				     const struct ipa_status *status)
1212 {
1213 	u32 val;
1214 
1215 	/* If the status indicates a tagged transfer, we'll drop the packet */
1216 	if (ipa_endpoint_status_tag(endpoint, status))
1217 		return true;
1218 
1219 	/* Deaggregation exceptions we drop; all other types we consume */
1220 	if (status->exception)
1221 		return status->exception == IPA_STATUS_EXCEPTION_DEAGGR;
1222 
1223 	/* Drop the packet if it fails to match a routing rule; otherwise no */
1224 	val = le32_get_bits(status->flags1, IPA_STATUS_FLAGS1_RT_RULE_ID_FMASK);
1225 
1226 	return val == field_max(IPA_STATUS_FLAGS1_RT_RULE_ID_FMASK);
1227 }
1228 
1229 static void ipa_endpoint_status_parse(struct ipa_endpoint *endpoint,
1230 				      struct page *page, u32 total_len)
1231 {
1232 	void *data = page_address(page) + NET_SKB_PAD;
1233 	u32 unused = IPA_RX_BUFFER_SIZE - total_len;
1234 	u32 resid = total_len;
1235 
1236 	while (resid) {
1237 		const struct ipa_status *status = data;
1238 		u32 align;
1239 		u32 len;
1240 
1241 		if (resid < sizeof(*status)) {
1242 			dev_err(&endpoint->ipa->pdev->dev,
1243 				"short message (%u bytes < %zu byte status)\n",
1244 				resid, sizeof(*status));
1245 			break;
1246 		}
1247 
1248 		/* Skip over status packets that lack packet data */
1249 		if (ipa_endpoint_status_skip(endpoint, status)) {
1250 			data += sizeof(*status);
1251 			resid -= sizeof(*status);
1252 			continue;
1253 		}
1254 
1255 		/* Compute the amount of buffer space consumed by the packet,
1256 		 * including the status element.  If the hardware is configured
1257 		 * to pad packet data to an aligned boundary, account for that.
1258 		 * And if checksum offload is enabled a trailer containing
1259 		 * computed checksum information will be appended.
1260 		 */
1261 		align = endpoint->data->rx.pad_align ? : 1;
1262 		len = le16_to_cpu(status->pkt_len);
1263 		len = sizeof(*status) + ALIGN(len, align);
1264 		if (endpoint->data->checksum)
1265 			len += sizeof(struct rmnet_map_dl_csum_trailer);
1266 
1267 		if (!ipa_endpoint_status_drop(endpoint, status)) {
1268 			void *data2;
1269 			u32 extra;
1270 			u32 len2;
1271 
1272 			/* Client receives only packet data (no status) */
1273 			data2 = data + sizeof(*status);
1274 			len2 = le16_to_cpu(status->pkt_len);
1275 
1276 			/* Have the true size reflect the extra unused space in
1277 			 * the original receive buffer.  Distribute the "cost"
1278 			 * proportionately across all aggregated packets in the
1279 			 * buffer.
1280 			 */
1281 			extra = DIV_ROUND_CLOSEST(unused * len, total_len);
1282 			ipa_endpoint_skb_copy(endpoint, data2, len2, extra);
1283 		}
1284 
1285 		/* Consume status and the full packet it describes */
1286 		data += len;
1287 		resid -= len;
1288 	}
1289 }
1290 
1291 /* Complete a TX transaction, command or from ipa_endpoint_skb_tx() */
1292 static void ipa_endpoint_tx_complete(struct ipa_endpoint *endpoint,
1293 				     struct gsi_trans *trans)
1294 {
1295 }
1296 
1297 /* Complete transaction initiated in ipa_endpoint_replenish_one() */
1298 static void ipa_endpoint_rx_complete(struct ipa_endpoint *endpoint,
1299 				     struct gsi_trans *trans)
1300 {
1301 	struct page *page;
1302 
1303 	ipa_endpoint_replenish(endpoint, true);
1304 
1305 	if (trans->cancelled)
1306 		return;
1307 
1308 	/* Parse or build a socket buffer using the actual received length */
1309 	page = trans->data;
1310 	if (endpoint->data->status_enable)
1311 		ipa_endpoint_status_parse(endpoint, page, trans->len);
1312 	else if (ipa_endpoint_skb_build(endpoint, page, trans->len))
1313 		trans->data = NULL;	/* Pages have been consumed */
1314 }
1315 
1316 void ipa_endpoint_trans_complete(struct ipa_endpoint *endpoint,
1317 				 struct gsi_trans *trans)
1318 {
1319 	if (endpoint->toward_ipa)
1320 		ipa_endpoint_tx_complete(endpoint, trans);
1321 	else
1322 		ipa_endpoint_rx_complete(endpoint, trans);
1323 }
1324 
1325 void ipa_endpoint_trans_release(struct ipa_endpoint *endpoint,
1326 				struct gsi_trans *trans)
1327 {
1328 	if (endpoint->toward_ipa) {
1329 		struct ipa *ipa = endpoint->ipa;
1330 
1331 		/* Nothing to do for command transactions */
1332 		if (endpoint != ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]) {
1333 			struct sk_buff *skb = trans->data;
1334 
1335 			if (skb)
1336 				dev_kfree_skb_any(skb);
1337 		}
1338 	} else {
1339 		struct page *page = trans->data;
1340 
1341 		if (page)
1342 			__free_pages(page, get_order(IPA_RX_BUFFER_SIZE));
1343 	}
1344 }
1345 
1346 void ipa_endpoint_default_route_set(struct ipa *ipa, u32 endpoint_id)
1347 {
1348 	u32 val;
1349 
1350 	/* ROUTE_DIS is 0 */
1351 	val = u32_encode_bits(endpoint_id, ROUTE_DEF_PIPE_FMASK);
1352 	val |= ROUTE_DEF_HDR_TABLE_FMASK;
1353 	val |= u32_encode_bits(0, ROUTE_DEF_HDR_OFST_FMASK);
1354 	val |= u32_encode_bits(endpoint_id, ROUTE_FRAG_DEF_PIPE_FMASK);
1355 	val |= ROUTE_DEF_RETAIN_HDR_FMASK;
1356 
1357 	iowrite32(val, ipa->reg_virt + IPA_REG_ROUTE_OFFSET);
1358 }
1359 
1360 void ipa_endpoint_default_route_clear(struct ipa *ipa)
1361 {
1362 	ipa_endpoint_default_route_set(ipa, 0);
1363 }
1364 
1365 /**
1366  * ipa_endpoint_reset_rx_aggr() - Reset RX endpoint with aggregation active
1367  * @endpoint:	Endpoint to be reset
1368  *
1369  * If aggregation is active on an RX endpoint when a reset is performed
1370  * on its underlying GSI channel, a special sequence of actions must be
1371  * taken to ensure the IPA pipeline is properly cleared.
1372  *
1373  * Return:	0 if successful, or a negative error code
1374  */
1375 static int ipa_endpoint_reset_rx_aggr(struct ipa_endpoint *endpoint)
1376 {
1377 	struct device *dev = &endpoint->ipa->pdev->dev;
1378 	struct ipa *ipa = endpoint->ipa;
1379 	struct gsi *gsi = &ipa->gsi;
1380 	bool suspended = false;
1381 	dma_addr_t addr;
1382 	u32 retries;
1383 	u32 len = 1;
1384 	void *virt;
1385 	int ret;
1386 
1387 	virt = kzalloc(len, GFP_KERNEL);
1388 	if (!virt)
1389 		return -ENOMEM;
1390 
1391 	addr = dma_map_single(dev, virt, len, DMA_FROM_DEVICE);
1392 	if (dma_mapping_error(dev, addr)) {
1393 		ret = -ENOMEM;
1394 		goto out_kfree;
1395 	}
1396 
1397 	/* Force close aggregation before issuing the reset */
1398 	ipa_endpoint_force_close(endpoint);
1399 
1400 	/* Reset and reconfigure the channel with the doorbell engine
1401 	 * disabled.  Then poll until we know aggregation is no longer
1402 	 * active.  We'll re-enable the doorbell (if appropriate) when
1403 	 * we reset again below.
1404 	 */
1405 	gsi_channel_reset(gsi, endpoint->channel_id, false);
1406 
1407 	/* Make sure the channel isn't suspended */
1408 	suspended = ipa_endpoint_program_suspend(endpoint, false);
1409 
1410 	/* Start channel and do a 1 byte read */
1411 	ret = gsi_channel_start(gsi, endpoint->channel_id);
1412 	if (ret)
1413 		goto out_suspend_again;
1414 
1415 	ret = gsi_trans_read_byte(gsi, endpoint->channel_id, addr);
1416 	if (ret)
1417 		goto err_endpoint_stop;
1418 
1419 	/* Wait for aggregation to be closed on the channel */
1420 	retries = IPA_ENDPOINT_RESET_AGGR_RETRY_MAX;
1421 	do {
1422 		if (!ipa_endpoint_aggr_active(endpoint))
1423 			break;
1424 		usleep_range(USEC_PER_MSEC, 2 * USEC_PER_MSEC);
1425 	} while (retries--);
1426 
1427 	/* Check one last time */
1428 	if (ipa_endpoint_aggr_active(endpoint))
1429 		dev_err(dev, "endpoint %u still active during reset\n",
1430 			endpoint->endpoint_id);
1431 
1432 	gsi_trans_read_byte_done(gsi, endpoint->channel_id);
1433 
1434 	ret = gsi_channel_stop(gsi, endpoint->channel_id);
1435 	if (ret)
1436 		goto out_suspend_again;
1437 
1438 	/* Finally, reset and reconfigure the channel again (re-enabling the
1439 	 * the doorbell engine if appropriate).  Sleep for 1 millisecond to
1440 	 * complete the channel reset sequence.  Finish by suspending the
1441 	 * channel again (if necessary).
1442 	 */
1443 	gsi_channel_reset(gsi, endpoint->channel_id, true);
1444 
1445 	usleep_range(USEC_PER_MSEC, 2 * USEC_PER_MSEC);
1446 
1447 	goto out_suspend_again;
1448 
1449 err_endpoint_stop:
1450 	(void)gsi_channel_stop(gsi, endpoint->channel_id);
1451 out_suspend_again:
1452 	if (suspended)
1453 		(void)ipa_endpoint_program_suspend(endpoint, true);
1454 	dma_unmap_single(dev, addr, len, DMA_FROM_DEVICE);
1455 out_kfree:
1456 	kfree(virt);
1457 
1458 	return ret;
1459 }
1460 
1461 static void ipa_endpoint_reset(struct ipa_endpoint *endpoint)
1462 {
1463 	u32 channel_id = endpoint->channel_id;
1464 	struct ipa *ipa = endpoint->ipa;
1465 	bool special;
1466 	int ret = 0;
1467 
1468 	/* On IPA v3.5.1, if an RX endpoint is reset while aggregation
1469 	 * is active, we need to handle things specially to recover.
1470 	 * All other cases just need to reset the underlying GSI channel.
1471 	 */
1472 	special = ipa->version == IPA_VERSION_3_5_1 &&
1473 			!endpoint->toward_ipa &&
1474 			endpoint->data->aggregation;
1475 	if (special && ipa_endpoint_aggr_active(endpoint))
1476 		ret = ipa_endpoint_reset_rx_aggr(endpoint);
1477 	else
1478 		gsi_channel_reset(&ipa->gsi, channel_id, true);
1479 
1480 	if (ret)
1481 		dev_err(&ipa->pdev->dev,
1482 			"error %d resetting channel %u for endpoint %u\n",
1483 			ret, endpoint->channel_id, endpoint->endpoint_id);
1484 }
1485 
1486 static void ipa_endpoint_program(struct ipa_endpoint *endpoint)
1487 {
1488 	if (endpoint->toward_ipa)
1489 		ipa_endpoint_program_delay(endpoint, false);
1490 	else
1491 		(void)ipa_endpoint_program_suspend(endpoint, false);
1492 	ipa_endpoint_init_cfg(endpoint);
1493 	ipa_endpoint_init_hdr(endpoint);
1494 	ipa_endpoint_init_hdr_ext(endpoint);
1495 	ipa_endpoint_init_hdr_metadata_mask(endpoint);
1496 	ipa_endpoint_init_mode(endpoint);
1497 	ipa_endpoint_init_aggr(endpoint);
1498 	ipa_endpoint_init_deaggr(endpoint);
1499 	ipa_endpoint_init_rsrc_grp(endpoint);
1500 	ipa_endpoint_init_seq(endpoint);
1501 	ipa_endpoint_status(endpoint);
1502 }
1503 
1504 int ipa_endpoint_enable_one(struct ipa_endpoint *endpoint)
1505 {
1506 	struct ipa *ipa = endpoint->ipa;
1507 	struct gsi *gsi = &ipa->gsi;
1508 	int ret;
1509 
1510 	ret = gsi_channel_start(gsi, endpoint->channel_id);
1511 	if (ret) {
1512 		dev_err(&ipa->pdev->dev,
1513 			"error %d starting %cX channel %u for endpoint %u\n",
1514 			ret, endpoint->toward_ipa ? 'T' : 'R',
1515 			endpoint->channel_id, endpoint->endpoint_id);
1516 		return ret;
1517 	}
1518 
1519 	if (!endpoint->toward_ipa) {
1520 		ipa_interrupt_suspend_enable(ipa->interrupt,
1521 					     endpoint->endpoint_id);
1522 		ipa_endpoint_replenish_enable(endpoint);
1523 	}
1524 
1525 	ipa->enabled |= BIT(endpoint->endpoint_id);
1526 
1527 	return 0;
1528 }
1529 
1530 void ipa_endpoint_disable_one(struct ipa_endpoint *endpoint)
1531 {
1532 	u32 mask = BIT(endpoint->endpoint_id);
1533 	struct ipa *ipa = endpoint->ipa;
1534 	struct gsi *gsi = &ipa->gsi;
1535 	int ret;
1536 
1537 	if (!(ipa->enabled & mask))
1538 		return;
1539 
1540 	ipa->enabled ^= mask;
1541 
1542 	if (!endpoint->toward_ipa) {
1543 		ipa_endpoint_replenish_disable(endpoint);
1544 		ipa_interrupt_suspend_disable(ipa->interrupt,
1545 					      endpoint->endpoint_id);
1546 	}
1547 
1548 	/* Note that if stop fails, the channel's state is not well-defined */
1549 	ret = gsi_channel_stop(gsi, endpoint->channel_id);
1550 	if (ret)
1551 		dev_err(&ipa->pdev->dev,
1552 			"error %d attempting to stop endpoint %u\n", ret,
1553 			endpoint->endpoint_id);
1554 }
1555 
1556 void ipa_endpoint_suspend_one(struct ipa_endpoint *endpoint)
1557 {
1558 	struct device *dev = &endpoint->ipa->pdev->dev;
1559 	struct gsi *gsi = &endpoint->ipa->gsi;
1560 	bool stop_channel;
1561 	int ret;
1562 
1563 	if (!(endpoint->ipa->enabled & BIT(endpoint->endpoint_id)))
1564 		return;
1565 
1566 	if (!endpoint->toward_ipa) {
1567 		ipa_endpoint_replenish_disable(endpoint);
1568 		(void)ipa_endpoint_program_suspend(endpoint, true);
1569 	}
1570 
1571 	/* IPA v3.5.1 doesn't use channel stop for suspend */
1572 	stop_channel = endpoint->ipa->version != IPA_VERSION_3_5_1;
1573 	ret = gsi_channel_suspend(gsi, endpoint->channel_id, stop_channel);
1574 	if (ret)
1575 		dev_err(dev, "error %d suspending channel %u\n", ret,
1576 			endpoint->channel_id);
1577 }
1578 
1579 void ipa_endpoint_resume_one(struct ipa_endpoint *endpoint)
1580 {
1581 	struct device *dev = &endpoint->ipa->pdev->dev;
1582 	struct gsi *gsi = &endpoint->ipa->gsi;
1583 	bool start_channel;
1584 	int ret;
1585 
1586 	if (!(endpoint->ipa->enabled & BIT(endpoint->endpoint_id)))
1587 		return;
1588 
1589 	if (!endpoint->toward_ipa)
1590 		(void)ipa_endpoint_program_suspend(endpoint, false);
1591 
1592 	/* IPA v3.5.1 doesn't use channel start for resume */
1593 	start_channel = endpoint->ipa->version != IPA_VERSION_3_5_1;
1594 	ret = gsi_channel_resume(gsi, endpoint->channel_id, start_channel);
1595 	if (ret)
1596 		dev_err(dev, "error %d resuming channel %u\n", ret,
1597 			endpoint->channel_id);
1598 	else if (!endpoint->toward_ipa)
1599 		ipa_endpoint_replenish_enable(endpoint);
1600 }
1601 
1602 void ipa_endpoint_suspend(struct ipa *ipa)
1603 {
1604 	if (!ipa->setup_complete)
1605 		return;
1606 
1607 	if (ipa->modem_netdev)
1608 		ipa_modem_suspend(ipa->modem_netdev);
1609 
1610 	ipa_cmd_pipeline_clear(ipa);
1611 
1612 	ipa_endpoint_suspend_one(ipa->name_map[IPA_ENDPOINT_AP_LAN_RX]);
1613 	ipa_endpoint_suspend_one(ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]);
1614 }
1615 
1616 void ipa_endpoint_resume(struct ipa *ipa)
1617 {
1618 	if (!ipa->setup_complete)
1619 		return;
1620 
1621 	ipa_endpoint_resume_one(ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]);
1622 	ipa_endpoint_resume_one(ipa->name_map[IPA_ENDPOINT_AP_LAN_RX]);
1623 
1624 	if (ipa->modem_netdev)
1625 		ipa_modem_resume(ipa->modem_netdev);
1626 }
1627 
1628 static void ipa_endpoint_setup_one(struct ipa_endpoint *endpoint)
1629 {
1630 	struct gsi *gsi = &endpoint->ipa->gsi;
1631 	u32 channel_id = endpoint->channel_id;
1632 
1633 	/* Only AP endpoints get set up */
1634 	if (endpoint->ee_id != GSI_EE_AP)
1635 		return;
1636 
1637 	endpoint->trans_tre_max = gsi_channel_trans_tre_max(gsi, channel_id);
1638 	if (!endpoint->toward_ipa) {
1639 		/* RX transactions require a single TRE, so the maximum
1640 		 * backlog is the same as the maximum outstanding TREs.
1641 		 */
1642 		endpoint->replenish_enabled = false;
1643 		atomic_set(&endpoint->replenish_saved,
1644 			   gsi_channel_tre_max(gsi, endpoint->channel_id));
1645 		atomic_set(&endpoint->replenish_backlog, 0);
1646 		INIT_DELAYED_WORK(&endpoint->replenish_work,
1647 				  ipa_endpoint_replenish_work);
1648 	}
1649 
1650 	ipa_endpoint_program(endpoint);
1651 
1652 	endpoint->ipa->set_up |= BIT(endpoint->endpoint_id);
1653 }
1654 
1655 static void ipa_endpoint_teardown_one(struct ipa_endpoint *endpoint)
1656 {
1657 	endpoint->ipa->set_up &= ~BIT(endpoint->endpoint_id);
1658 
1659 	if (!endpoint->toward_ipa)
1660 		cancel_delayed_work_sync(&endpoint->replenish_work);
1661 
1662 	ipa_endpoint_reset(endpoint);
1663 }
1664 
1665 void ipa_endpoint_setup(struct ipa *ipa)
1666 {
1667 	u32 initialized = ipa->initialized;
1668 
1669 	ipa->set_up = 0;
1670 	while (initialized) {
1671 		u32 endpoint_id = __ffs(initialized);
1672 
1673 		initialized ^= BIT(endpoint_id);
1674 
1675 		ipa_endpoint_setup_one(&ipa->endpoint[endpoint_id]);
1676 	}
1677 }
1678 
1679 void ipa_endpoint_teardown(struct ipa *ipa)
1680 {
1681 	u32 set_up = ipa->set_up;
1682 
1683 	while (set_up) {
1684 		u32 endpoint_id = __fls(set_up);
1685 
1686 		set_up ^= BIT(endpoint_id);
1687 
1688 		ipa_endpoint_teardown_one(&ipa->endpoint[endpoint_id]);
1689 	}
1690 	ipa->set_up = 0;
1691 }
1692 
1693 int ipa_endpoint_config(struct ipa *ipa)
1694 {
1695 	struct device *dev = &ipa->pdev->dev;
1696 	u32 initialized;
1697 	u32 rx_base;
1698 	u32 rx_mask;
1699 	u32 tx_mask;
1700 	int ret = 0;
1701 	u32 max;
1702 	u32 val;
1703 
1704 	/* Find out about the endpoints supplied by the hardware, and ensure
1705 	 * the highest one doesn't exceed the number we support.
1706 	 */
1707 	val = ioread32(ipa->reg_virt + IPA_REG_FLAVOR_0_OFFSET);
1708 
1709 	/* Our RX is an IPA producer */
1710 	rx_base = u32_get_bits(val, IPA_PROD_LOWEST_FMASK);
1711 	max = rx_base + u32_get_bits(val, IPA_MAX_PROD_PIPES_FMASK);
1712 	if (max > IPA_ENDPOINT_MAX) {
1713 		dev_err(dev, "too many endpoints (%u > %u)\n",
1714 			max, IPA_ENDPOINT_MAX);
1715 		return -EINVAL;
1716 	}
1717 	rx_mask = GENMASK(max - 1, rx_base);
1718 
1719 	/* Our TX is an IPA consumer */
1720 	max = u32_get_bits(val, IPA_MAX_CONS_PIPES_FMASK);
1721 	tx_mask = GENMASK(max - 1, 0);
1722 
1723 	ipa->available = rx_mask | tx_mask;
1724 
1725 	/* Check for initialized endpoints not supported by the hardware */
1726 	if (ipa->initialized & ~ipa->available) {
1727 		dev_err(dev, "unavailable endpoint id(s) 0x%08x\n",
1728 			ipa->initialized & ~ipa->available);
1729 		ret = -EINVAL;		/* Report other errors too */
1730 	}
1731 
1732 	initialized = ipa->initialized;
1733 	while (initialized) {
1734 		u32 endpoint_id = __ffs(initialized);
1735 		struct ipa_endpoint *endpoint;
1736 
1737 		initialized ^= BIT(endpoint_id);
1738 
1739 		/* Make sure it's pointing in the right direction */
1740 		endpoint = &ipa->endpoint[endpoint_id];
1741 		if ((endpoint_id < rx_base) != !!endpoint->toward_ipa) {
1742 			dev_err(dev, "endpoint id %u wrong direction\n",
1743 				endpoint_id);
1744 			ret = -EINVAL;
1745 		}
1746 	}
1747 
1748 	return ret;
1749 }
1750 
1751 void ipa_endpoint_deconfig(struct ipa *ipa)
1752 {
1753 	ipa->available = 0;	/* Nothing more to do */
1754 }
1755 
1756 static void ipa_endpoint_init_one(struct ipa *ipa, enum ipa_endpoint_name name,
1757 				  const struct ipa_gsi_endpoint_data *data)
1758 {
1759 	struct ipa_endpoint *endpoint;
1760 
1761 	endpoint = &ipa->endpoint[data->endpoint_id];
1762 
1763 	if (data->ee_id == GSI_EE_AP)
1764 		ipa->channel_map[data->channel_id] = endpoint;
1765 	ipa->name_map[name] = endpoint;
1766 
1767 	endpoint->ipa = ipa;
1768 	endpoint->ee_id = data->ee_id;
1769 	endpoint->seq_type = data->endpoint.seq_type;
1770 	endpoint->channel_id = data->channel_id;
1771 	endpoint->endpoint_id = data->endpoint_id;
1772 	endpoint->toward_ipa = data->toward_ipa;
1773 	endpoint->data = &data->endpoint.config;
1774 
1775 	ipa->initialized |= BIT(endpoint->endpoint_id);
1776 }
1777 
1778 void ipa_endpoint_exit_one(struct ipa_endpoint *endpoint)
1779 {
1780 	endpoint->ipa->initialized &= ~BIT(endpoint->endpoint_id);
1781 
1782 	memset(endpoint, 0, sizeof(*endpoint));
1783 }
1784 
1785 void ipa_endpoint_exit(struct ipa *ipa)
1786 {
1787 	u32 initialized = ipa->initialized;
1788 
1789 	while (initialized) {
1790 		u32 endpoint_id = __fls(initialized);
1791 
1792 		initialized ^= BIT(endpoint_id);
1793 
1794 		ipa_endpoint_exit_one(&ipa->endpoint[endpoint_id]);
1795 	}
1796 	memset(ipa->name_map, 0, sizeof(ipa->name_map));
1797 	memset(ipa->channel_map, 0, sizeof(ipa->channel_map));
1798 }
1799 
1800 /* Returns a bitmask of endpoints that support filtering, or 0 on error */
1801 u32 ipa_endpoint_init(struct ipa *ipa, u32 count,
1802 		      const struct ipa_gsi_endpoint_data *data)
1803 {
1804 	enum ipa_endpoint_name name;
1805 	u32 filter_map;
1806 
1807 	if (!ipa_endpoint_data_valid(ipa, count, data))
1808 		return 0;	/* Error */
1809 
1810 	ipa->initialized = 0;
1811 
1812 	filter_map = 0;
1813 	for (name = 0; name < count; name++, data++) {
1814 		if (ipa_gsi_endpoint_data_empty(data))
1815 			continue;	/* Skip over empty slots */
1816 
1817 		ipa_endpoint_init_one(ipa, name, data);
1818 
1819 		if (data->endpoint.filter_support)
1820 			filter_map |= BIT(data->endpoint_id);
1821 	}
1822 
1823 	if (!ipa_filter_map_valid(ipa, filter_map))
1824 		goto err_endpoint_exit;
1825 
1826 	return filter_map;	/* Non-zero bitmask */
1827 
1828 err_endpoint_exit:
1829 	ipa_endpoint_exit(ipa);
1830 
1831 	return 0;	/* Error */
1832 }
1833