xref: /linux/drivers/net/ipa/ipa_endpoint.c (revision 5245f4fd28d126cc13e32e77abc8a8fd287167b0)
184f9bd12SAlex Elder // SPDX-License-Identifier: GPL-2.0
284f9bd12SAlex Elder 
384f9bd12SAlex Elder /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
407abde54SAlex Elder  * Copyright (C) 2019-2023 Linaro Ltd.
584f9bd12SAlex Elder  */
684f9bd12SAlex Elder 
784f9bd12SAlex Elder #include <linux/types.h>
884f9bd12SAlex Elder #include <linux/device.h>
984f9bd12SAlex Elder #include <linux/slab.h>
1084f9bd12SAlex Elder #include <linux/bitfield.h>
1184f9bd12SAlex Elder #include <linux/if_rmnet.h>
1284f9bd12SAlex Elder #include <linux/dma-direction.h>
1384f9bd12SAlex Elder 
1484f9bd12SAlex Elder #include "gsi.h"
1584f9bd12SAlex Elder #include "gsi_trans.h"
1684f9bd12SAlex Elder #include "ipa.h"
1784f9bd12SAlex Elder #include "ipa_data.h"
1884f9bd12SAlex Elder #include "ipa_endpoint.h"
1984f9bd12SAlex Elder #include "ipa_cmd.h"
2084f9bd12SAlex Elder #include "ipa_mem.h"
2184f9bd12SAlex Elder #include "ipa_modem.h"
2284f9bd12SAlex Elder #include "ipa_table.h"
2384f9bd12SAlex Elder #include "ipa_gsi.h"
242775cbc5SAlex Elder #include "ipa_power.h"
2584f9bd12SAlex Elder 
269654d8c4SAlex Elder /* Hardware is told about receive buffers once a "batch" has been queued */
279654d8c4SAlex Elder #define IPA_REPLENISH_BATCH	16		/* Must be non-zero */
2884f9bd12SAlex Elder 
2984f9bd12SAlex Elder /* The amount of RX buffer space consumed by standard skb overhead */
3084f9bd12SAlex Elder #define IPA_RX_BUFFER_OVERHEAD	(PAGE_SIZE - SKB_MAX_ORDER(NET_SKB_PAD, 0))
3184f9bd12SAlex Elder 
328730f45dSAlex Elder /* Where to find the QMAP mux_id for a packet within modem-supplied metadata */
338730f45dSAlex Elder #define IPA_ENDPOINT_QMAP_METADATA_MASK		0x000000ff /* host byte order */
348730f45dSAlex Elder 
3584f9bd12SAlex Elder #define IPA_ENDPOINT_RESET_AGGR_RETRY_MAX	3
3684f9bd12SAlex Elder 
37ec4c24f6SAlex Elder /** enum ipa_status_opcode - IPA status opcode field hardware values */
38ec4c24f6SAlex Elder enum ipa_status_opcode {				/* *Not* a bitmask */
39ec4c24f6SAlex Elder 	IPA_STATUS_OPCODE_PACKET		= 1,
40ec4c24f6SAlex Elder 	IPA_STATUS_OPCODE_NEW_RULE_PACKET	= 2,
41ec4c24f6SAlex Elder 	IPA_STATUS_OPCODE_DROPPED_PACKET	= 4,
42ec4c24f6SAlex Elder 	IPA_STATUS_OPCODE_SUSPENDED_PACKET	= 8,
43ec4c24f6SAlex Elder 	IPA_STATUS_OPCODE_LOG			= 16,
44ec4c24f6SAlex Elder 	IPA_STATUS_OPCODE_DCMP			= 32,
45ec4c24f6SAlex Elder 	IPA_STATUS_OPCODE_PACKET_2ND_PASS	= 64,
4684f9bd12SAlex Elder };
4784f9bd12SAlex Elder 
48ec4c24f6SAlex Elder /** enum ipa_status_exception - IPA status exception field hardware values */
49ec4c24f6SAlex Elder enum ipa_status_exception {				/* *Not* a bitmask */
5084f9bd12SAlex Elder 	/* 0 means no exception */
51ec4c24f6SAlex Elder 	IPA_STATUS_EXCEPTION_DEAGGR		= 1,
52ec4c24f6SAlex Elder 	IPA_STATUS_EXCEPTION_IPTYPE		= 4,
53ec4c24f6SAlex Elder 	IPA_STATUS_EXCEPTION_PACKET_LENGTH	= 8,
54ec4c24f6SAlex Elder 	IPA_STATUS_EXCEPTION_FRAG_RULE_MISS	= 16,
55ec4c24f6SAlex Elder 	IPA_STATUS_EXCEPTION_SW_FILTER		= 32,
56ec4c24f6SAlex Elder 	IPA_STATUS_EXCEPTION_NAT		= 64,		/* IPv4 */
57ec4c24f6SAlex Elder 	IPA_STATUS_EXCEPTION_IPV6_CONN_TRACK	= 64,		/* IPv6 */
58ec4c24f6SAlex Elder 	IPA_STATUS_EXCEPTION_UC			= 128,
59ec4c24f6SAlex Elder 	IPA_STATUS_EXCEPTION_INVALID_ENDPOINT	= 129,
60ec4c24f6SAlex Elder 	IPA_STATUS_EXCEPTION_HEADER_INSERT	= 136,
61ec4c24f6SAlex Elder 	IPA_STATUS_EXCEPTION_CHEKCSUM		= 229,
6284f9bd12SAlex Elder };
6384f9bd12SAlex Elder 
648e71708bSAlex Elder /** enum ipa_status_mask - IPA status mask field bitmask hardware values */
658e71708bSAlex Elder enum ipa_status_mask {
668e71708bSAlex Elder 	IPA_STATUS_MASK_FRAG_PROCESS		= BIT(0),
678e71708bSAlex Elder 	IPA_STATUS_MASK_FILT_PROCESS		= BIT(1),
688e71708bSAlex Elder 	IPA_STATUS_MASK_NAT_PROCESS		= BIT(2),
698e71708bSAlex Elder 	IPA_STATUS_MASK_ROUTE_PROCESS		= BIT(3),
708e71708bSAlex Elder 	IPA_STATUS_MASK_TAG_VALID		= BIT(4),
718e71708bSAlex Elder 	IPA_STATUS_MASK_FRAGMENT		= BIT(5),
728e71708bSAlex Elder 	IPA_STATUS_MASK_FIRST_FRAGMENT		= BIT(6),
738e71708bSAlex Elder 	IPA_STATUS_MASK_V4			= BIT(7),
748e71708bSAlex Elder 	IPA_STATUS_MASK_CKSUM_PROCESS		= BIT(8),
758e71708bSAlex Elder 	IPA_STATUS_MASK_AGGR_PROCESS		= BIT(9),
768e71708bSAlex Elder 	IPA_STATUS_MASK_DEST_EOT		= BIT(10),
778e71708bSAlex Elder 	IPA_STATUS_MASK_DEAGGR_PROCESS		= BIT(11),
788e71708bSAlex Elder 	IPA_STATUS_MASK_DEAGG_FIRST		= BIT(12),
798e71708bSAlex Elder 	IPA_STATUS_MASK_SRC_EOT			= BIT(13),
808e71708bSAlex Elder 	IPA_STATUS_MASK_PREV_EOT		= BIT(14),
818e71708bSAlex Elder 	IPA_STATUS_MASK_BYTE_LIMIT		= BIT(15),
828e71708bSAlex Elder };
838e71708bSAlex Elder 
84ebd2a82eSAlex Elder /* Special IPA filter/router rule field value indicating "rule miss" */
85ebd2a82eSAlex Elder #define IPA_STATUS_RULE_MISS	0x3ff	/* 10-bit filter/router rule fields */
86ebd2a82eSAlex Elder 
87cbea4761SAlex Elder /** The IPA status nat_type field uses enum ipa_nat_type hardware values */
88cbea4761SAlex Elder 
89ebd2a82eSAlex Elder /* enum ipa_status_field_id - IPA packet status structure field identifiers */
90ebd2a82eSAlex Elder enum ipa_status_field_id {
91ebd2a82eSAlex Elder 	STATUS_OPCODE,			/* enum ipa_status_opcode */
92ebd2a82eSAlex Elder 	STATUS_EXCEPTION,		/* enum ipa_status_exception */
93ebd2a82eSAlex Elder 	STATUS_MASK,			/* enum ipa_status_mask (bitmask) */
94ebd2a82eSAlex Elder 	STATUS_LENGTH,
95ebd2a82eSAlex Elder 	STATUS_SRC_ENDPOINT,
96ebd2a82eSAlex Elder 	STATUS_DST_ENDPOINT,
97ebd2a82eSAlex Elder 	STATUS_METADATA,
98ebd2a82eSAlex Elder 	STATUS_FILTER_LOCAL,		/* Boolean */
99ebd2a82eSAlex Elder 	STATUS_FILTER_HASH,		/* Boolean */
100ebd2a82eSAlex Elder 	STATUS_FILTER_GLOBAL,		/* Boolean */
101ebd2a82eSAlex Elder 	STATUS_FILTER_RETAIN,		/* Boolean */
102ebd2a82eSAlex Elder 	STATUS_FILTER_RULE_INDEX,
103ebd2a82eSAlex Elder 	STATUS_ROUTER_LOCAL,		/* Boolean */
104ebd2a82eSAlex Elder 	STATUS_ROUTER_HASH,		/* Boolean */
105ebd2a82eSAlex Elder 	STATUS_UCP,			/* Boolean */
106ebd2a82eSAlex Elder 	STATUS_ROUTER_TABLE,
107ebd2a82eSAlex Elder 	STATUS_ROUTER_RULE_INDEX,
108ebd2a82eSAlex Elder 	STATUS_NAT_HIT,			/* Boolean */
109ebd2a82eSAlex Elder 	STATUS_NAT_INDEX,
110ebd2a82eSAlex Elder 	STATUS_NAT_TYPE,		/* enum ipa_nat_type */
111ebd2a82eSAlex Elder 	STATUS_TAG_LOW32,		/* Low-order 32 bits of 48-bit tag */
112ebd2a82eSAlex Elder 	STATUS_TAG_HIGH16,		/* High-order 16 bits of 48-bit tag */
113ebd2a82eSAlex Elder 	STATUS_SEQUENCE,
114ebd2a82eSAlex Elder 	STATUS_TIME_OF_DAY,
115ebd2a82eSAlex Elder 	STATUS_HEADER_LOCAL,		/* Boolean */
116ebd2a82eSAlex Elder 	STATUS_HEADER_OFFSET,
117ebd2a82eSAlex Elder 	STATUS_FRAG_HIT,		/* Boolean */
118ebd2a82eSAlex Elder 	STATUS_FRAG_RULE_INDEX,
11984f9bd12SAlex Elder };
12084f9bd12SAlex Elder 
121b8dc7d0eSAlex Elder /* Size in bytes of an IPA packet status structure */
122be7f8012SBert Karwatzki #define IPA_STATUS_SIZE			sizeof(__le32[8])
123b8dc7d0eSAlex Elder 
124ebd2a82eSAlex Elder /* IPA status structure decoder; looks up field values for a structure */
12555c6eae7SAlex Elder static u32 ipa_status_extract(struct ipa *ipa, const void *data,
12655c6eae7SAlex Elder 			      enum ipa_status_field_id field)
127ebd2a82eSAlex Elder {
12855c6eae7SAlex Elder 	enum ipa_version version = ipa->version;
129ebd2a82eSAlex Elder 	const __le32 *word = data;
130ebd2a82eSAlex Elder 
131ebd2a82eSAlex Elder 	switch (field) {
132ebd2a82eSAlex Elder 	case STATUS_OPCODE:
133ebd2a82eSAlex Elder 		return le32_get_bits(word[0], GENMASK(7, 0));
134ebd2a82eSAlex Elder 	case STATUS_EXCEPTION:
135ebd2a82eSAlex Elder 		return le32_get_bits(word[0], GENMASK(15, 8));
136ebd2a82eSAlex Elder 	case STATUS_MASK:
137ebd2a82eSAlex Elder 		return le32_get_bits(word[0], GENMASK(31, 16));
138ebd2a82eSAlex Elder 	case STATUS_LENGTH:
139ebd2a82eSAlex Elder 		return le32_get_bits(word[1], GENMASK(15, 0));
140ebd2a82eSAlex Elder 	case STATUS_SRC_ENDPOINT:
14155c6eae7SAlex Elder 		if (version < IPA_VERSION_5_0)
142ebd2a82eSAlex Elder 			return le32_get_bits(word[1], GENMASK(20, 16));
14355c6eae7SAlex Elder 		return le32_get_bits(word[1], GENMASK(23, 16));
14455c6eae7SAlex Elder 	/* Status word 1, bits 21-23 are reserved (not IPA v5.0+) */
14555c6eae7SAlex Elder 	/* Status word 1, bits 24-26 are reserved (IPA v5.0+) */
146ebd2a82eSAlex Elder 	case STATUS_DST_ENDPOINT:
14755c6eae7SAlex Elder 		if (version < IPA_VERSION_5_0)
148ebd2a82eSAlex Elder 			return le32_get_bits(word[1], GENMASK(28, 24));
14955c6eae7SAlex Elder 		return le32_get_bits(word[7], GENMASK(23, 16));
150ebd2a82eSAlex Elder 	/* Status word 1, bits 29-31 are reserved */
151ebd2a82eSAlex Elder 	case STATUS_METADATA:
152ebd2a82eSAlex Elder 		return le32_to_cpu(word[2]);
153ebd2a82eSAlex Elder 	case STATUS_FILTER_LOCAL:
154ebd2a82eSAlex Elder 		return le32_get_bits(word[3], GENMASK(0, 0));
155ebd2a82eSAlex Elder 	case STATUS_FILTER_HASH:
156ebd2a82eSAlex Elder 		return le32_get_bits(word[3], GENMASK(1, 1));
157ebd2a82eSAlex Elder 	case STATUS_FILTER_GLOBAL:
158ebd2a82eSAlex Elder 		return le32_get_bits(word[3], GENMASK(2, 2));
159ebd2a82eSAlex Elder 	case STATUS_FILTER_RETAIN:
160ebd2a82eSAlex Elder 		return le32_get_bits(word[3], GENMASK(3, 3));
161ebd2a82eSAlex Elder 	case STATUS_FILTER_RULE_INDEX:
162ebd2a82eSAlex Elder 		return le32_get_bits(word[3], GENMASK(13, 4));
16355c6eae7SAlex Elder 	/* ROUTER_TABLE is in word 3, bits 14-21 (IPA v5.0+) */
164ebd2a82eSAlex Elder 	case STATUS_ROUTER_LOCAL:
16555c6eae7SAlex Elder 		if (version < IPA_VERSION_5_0)
166ebd2a82eSAlex Elder 			return le32_get_bits(word[3], GENMASK(14, 14));
16755c6eae7SAlex Elder 		return le32_get_bits(word[1], GENMASK(27, 27));
168ebd2a82eSAlex Elder 	case STATUS_ROUTER_HASH:
16955c6eae7SAlex Elder 		if (version < IPA_VERSION_5_0)
170ebd2a82eSAlex Elder 			return le32_get_bits(word[3], GENMASK(15, 15));
17155c6eae7SAlex Elder 		return le32_get_bits(word[1], GENMASK(28, 28));
172ebd2a82eSAlex Elder 	case STATUS_UCP:
17355c6eae7SAlex Elder 		if (version < IPA_VERSION_5_0)
174ebd2a82eSAlex Elder 			return le32_get_bits(word[3], GENMASK(16, 16));
17555c6eae7SAlex Elder 		return le32_get_bits(word[7], GENMASK(31, 31));
176ebd2a82eSAlex Elder 	case STATUS_ROUTER_TABLE:
17755c6eae7SAlex Elder 		if (version < IPA_VERSION_5_0)
178ebd2a82eSAlex Elder 			return le32_get_bits(word[3], GENMASK(21, 17));
17955c6eae7SAlex Elder 		return le32_get_bits(word[3], GENMASK(21, 14));
180ebd2a82eSAlex Elder 	case STATUS_ROUTER_RULE_INDEX:
181ebd2a82eSAlex Elder 		return le32_get_bits(word[3], GENMASK(31, 22));
182ebd2a82eSAlex Elder 	case STATUS_NAT_HIT:
183ebd2a82eSAlex Elder 		return le32_get_bits(word[4], GENMASK(0, 0));
184ebd2a82eSAlex Elder 	case STATUS_NAT_INDEX:
185ebd2a82eSAlex Elder 		return le32_get_bits(word[4], GENMASK(13, 1));
186ebd2a82eSAlex Elder 	case STATUS_NAT_TYPE:
187ebd2a82eSAlex Elder 		return le32_get_bits(word[4], GENMASK(15, 14));
188ebd2a82eSAlex Elder 	case STATUS_TAG_LOW32:
189ebd2a82eSAlex Elder 		return le32_get_bits(word[4], GENMASK(31, 16)) |
190ebd2a82eSAlex Elder 			(le32_get_bits(word[5], GENMASK(15, 0)) << 16);
191ebd2a82eSAlex Elder 	case STATUS_TAG_HIGH16:
192ebd2a82eSAlex Elder 		return le32_get_bits(word[5], GENMASK(31, 16));
193ebd2a82eSAlex Elder 	case STATUS_SEQUENCE:
194ebd2a82eSAlex Elder 		return le32_get_bits(word[6], GENMASK(7, 0));
195ebd2a82eSAlex Elder 	case STATUS_TIME_OF_DAY:
196ebd2a82eSAlex Elder 		return le32_get_bits(word[6], GENMASK(31, 8));
197ebd2a82eSAlex Elder 	case STATUS_HEADER_LOCAL:
198ebd2a82eSAlex Elder 		return le32_get_bits(word[7], GENMASK(0, 0));
199ebd2a82eSAlex Elder 	case STATUS_HEADER_OFFSET:
200ebd2a82eSAlex Elder 		return le32_get_bits(word[7], GENMASK(10, 1));
201ebd2a82eSAlex Elder 	case STATUS_FRAG_HIT:
202ebd2a82eSAlex Elder 		return le32_get_bits(word[7], GENMASK(11, 11));
203ebd2a82eSAlex Elder 	case STATUS_FRAG_RULE_INDEX:
204ebd2a82eSAlex Elder 		return le32_get_bits(word[7], GENMASK(15, 12));
20555c6eae7SAlex Elder 	/* Status word 7, bits 16-30 are reserved */
20655c6eae7SAlex Elder 	/* Status word 7, bit 31 is reserved (not IPA v5.0+) */
207ebd2a82eSAlex Elder 	default:
208ebd2a82eSAlex Elder 		WARN(true, "%s: bad field_id %u\n", __func__, field);
209ebd2a82eSAlex Elder 		return 0;
210ebd2a82eSAlex Elder 	}
211ebd2a82eSAlex Elder }
212ebd2a82eSAlex Elder 
2133cebb7c2SAlex Elder /* Compute the aggregation size value to use for a given buffer size */
2143cebb7c2SAlex Elder static u32 ipa_aggr_size_kb(u32 rx_buffer_size, bool aggr_hard_limit)
2153cebb7c2SAlex Elder {
2163cebb7c2SAlex Elder 	/* A hard aggregation limit will not be crossed; aggregation closes
2173cebb7c2SAlex Elder 	 * if saving incoming data would cross the hard byte limit boundary.
2183cebb7c2SAlex Elder 	 *
2193cebb7c2SAlex Elder 	 * With a soft limit, aggregation closes *after* the size boundary
2203cebb7c2SAlex Elder 	 * has been crossed.  In that case the limit must leave enough space
2213cebb7c2SAlex Elder 	 * after that limit to receive a full MTU of data plus overhead.
2223cebb7c2SAlex Elder 	 */
2233cebb7c2SAlex Elder 	if (!aggr_hard_limit)
2243cebb7c2SAlex Elder 		rx_buffer_size -= IPA_MTU + IPA_RX_BUFFER_OVERHEAD;
2253cebb7c2SAlex Elder 
2263cebb7c2SAlex Elder 	/* The byte limit is encoded as a number of kilobytes */
2273cebb7c2SAlex Elder 
2283cebb7c2SAlex Elder 	return rx_buffer_size / SZ_1K;
2293cebb7c2SAlex Elder }
2303cebb7c2SAlex Elder 
23184f9bd12SAlex Elder static bool ipa_endpoint_data_valid_one(struct ipa *ipa, u32 count,
23284f9bd12SAlex Elder 			    const struct ipa_gsi_endpoint_data *all_data,
23384f9bd12SAlex Elder 			    const struct ipa_gsi_endpoint_data *data)
23484f9bd12SAlex Elder {
23584f9bd12SAlex Elder 	const struct ipa_gsi_endpoint_data *other_data;
23684f9bd12SAlex Elder 	enum ipa_endpoint_name other_name;
237*5245f4fdSAlex Elder 	struct device *dev = ipa->dev;
23884f9bd12SAlex Elder 
23984f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(data))
24084f9bd12SAlex Elder 		return true;
24184f9bd12SAlex Elder 
24284f9bd12SAlex Elder 	if (!data->toward_ipa) {
2433cebb7c2SAlex Elder 		const struct ipa_endpoint_rx *rx_config;
24481772e44SAlex Elder 		const struct reg *reg;
245ed23f026SAlex Elder 		u32 buffer_size;
2463cebb7c2SAlex Elder 		u32 aggr_size;
247ed23f026SAlex Elder 		u32 limit;
248ed23f026SAlex Elder 
24984f9bd12SAlex Elder 		if (data->endpoint.filter_support) {
25084f9bd12SAlex Elder 			dev_err(dev, "filtering not supported for "
25184f9bd12SAlex Elder 					"RX endpoint %u\n",
25284f9bd12SAlex Elder 				data->endpoint_id);
25384f9bd12SAlex Elder 			return false;
25484f9bd12SAlex Elder 		}
25584f9bd12SAlex Elder 
256ed23f026SAlex Elder 		/* Nothing more to check for non-AP RX */
257ed23f026SAlex Elder 		if (data->ee_id != GSI_EE_AP)
258ed23f026SAlex Elder 			return true;
259ed23f026SAlex Elder 
2603cebb7c2SAlex Elder 		rx_config = &data->endpoint.config.rx;
2613cebb7c2SAlex Elder 
262ed23f026SAlex Elder 		/* The buffer size must hold an MTU plus overhead */
2633cebb7c2SAlex Elder 		buffer_size = rx_config->buffer_size;
264ed23f026SAlex Elder 		limit = IPA_MTU + IPA_RX_BUFFER_OVERHEAD;
265ed23f026SAlex Elder 		if (buffer_size < limit) {
266ed23f026SAlex Elder 			dev_err(dev, "RX buffer size too small for RX endpoint %u (%u < %u)\n",
267ed23f026SAlex Elder 				data->endpoint_id, buffer_size, limit);
268ed23f026SAlex Elder 			return false;
269ed23f026SAlex Elder 		}
270ed23f026SAlex Elder 
2713cebb7c2SAlex Elder 		if (!data->endpoint.config.aggregation) {
2723cebb7c2SAlex Elder 			bool result = true;
2733cebb7c2SAlex Elder 
2743cebb7c2SAlex Elder 			/* No aggregation; check for bogus aggregation data */
275beb90cbaSAlex Elder 			if (rx_config->aggr_time_limit) {
276beb90cbaSAlex Elder 				dev_err(dev,
277beb90cbaSAlex Elder 					"time limit with no aggregation for RX endpoint %u\n",
278beb90cbaSAlex Elder 					data->endpoint_id);
279beb90cbaSAlex Elder 				result = false;
280beb90cbaSAlex Elder 			}
281beb90cbaSAlex Elder 
2823cebb7c2SAlex Elder 			if (rx_config->aggr_hard_limit) {
2833cebb7c2SAlex Elder 				dev_err(dev, "hard limit with no aggregation for RX endpoint %u\n",
2843cebb7c2SAlex Elder 					data->endpoint_id);
2853cebb7c2SAlex Elder 				result = false;
2863cebb7c2SAlex Elder 			}
2873cebb7c2SAlex Elder 
2883cebb7c2SAlex Elder 			if (rx_config->aggr_close_eof) {
2893cebb7c2SAlex Elder 				dev_err(dev, "close EOF with no aggregation for RX endpoint %u\n",
2903cebb7c2SAlex Elder 					data->endpoint_id);
2913cebb7c2SAlex Elder 				result = false;
2923cebb7c2SAlex Elder 			}
2933cebb7c2SAlex Elder 
2943cebb7c2SAlex Elder 			return result;	/* Nothing more to check */
2953cebb7c2SAlex Elder 		}
2963cebb7c2SAlex Elder 
2973cebb7c2SAlex Elder 		/* For an endpoint supporting receive aggregation, the byte
2983cebb7c2SAlex Elder 		 * limit defines the point at which aggregation closes.  This
2993cebb7c2SAlex Elder 		 * check ensures the receive buffer size doesn't result in a
3003cebb7c2SAlex Elder 		 * limit that exceeds what's representable in the aggregation
3013cebb7c2SAlex Elder 		 * byte limit field.
302ed23f026SAlex Elder 		 */
3033cebb7c2SAlex Elder 		aggr_size = ipa_aggr_size_kb(buffer_size - NET_SKB_PAD,
3043cebb7c2SAlex Elder 					     rx_config->aggr_hard_limit);
305216b409dSAlex Elder 		reg = ipa_reg(ipa, ENDP_INIT_AGGR);
306216b409dSAlex Elder 
307f1470fd7SAlex Elder 		limit = reg_field_max(reg, BYTE_LIMIT);
3083cebb7c2SAlex Elder 		if (aggr_size > limit) {
3093cebb7c2SAlex Elder 			dev_err(dev, "aggregated size too large for RX endpoint %u (%u KB > %u KB)\n",
3103cebb7c2SAlex Elder 				data->endpoint_id, aggr_size, limit);
311ed23f026SAlex Elder 
312ed23f026SAlex Elder 			return false;
313ed23f026SAlex Elder 		}
314ed23f026SAlex Elder 
31584f9bd12SAlex Elder 		return true;	/* Nothing more to check for RX */
31684f9bd12SAlex Elder 	}
31784f9bd12SAlex Elder 
318a14d5937SAlex Elder 	/* Starting with IPA v4.5 sequencer replication is obsolete */
319a14d5937SAlex Elder 	if (ipa->version >= IPA_VERSION_4_5) {
320a14d5937SAlex Elder 		if (data->endpoint.config.tx.seq_rep_type) {
321a14d5937SAlex Elder 			dev_err(dev, "no-zero seq_rep_type TX endpoint %u\n",
322a14d5937SAlex Elder 				data->endpoint_id);
323a14d5937SAlex Elder 			return false;
324a14d5937SAlex Elder 		}
325a14d5937SAlex Elder 	}
326a14d5937SAlex Elder 
32784f9bd12SAlex Elder 	if (data->endpoint.config.status_enable) {
32884f9bd12SAlex Elder 		other_name = data->endpoint.config.tx.status_endpoint;
32984f9bd12SAlex Elder 		if (other_name >= count) {
33084f9bd12SAlex Elder 			dev_err(dev, "status endpoint name %u out of range "
33184f9bd12SAlex Elder 					"for endpoint %u\n",
33284f9bd12SAlex Elder 				other_name, data->endpoint_id);
33384f9bd12SAlex Elder 			return false;
33484f9bd12SAlex Elder 		}
33584f9bd12SAlex Elder 
33684f9bd12SAlex Elder 		/* Status endpoint must be defined... */
33784f9bd12SAlex Elder 		other_data = &all_data[other_name];
33884f9bd12SAlex Elder 		if (ipa_gsi_endpoint_data_empty(other_data)) {
33984f9bd12SAlex Elder 			dev_err(dev, "DMA endpoint name %u undefined "
34084f9bd12SAlex Elder 					"for endpoint %u\n",
34184f9bd12SAlex Elder 				other_name, data->endpoint_id);
34284f9bd12SAlex Elder 			return false;
34384f9bd12SAlex Elder 		}
34484f9bd12SAlex Elder 
34584f9bd12SAlex Elder 		/* ...and has to be an RX endpoint... */
34684f9bd12SAlex Elder 		if (other_data->toward_ipa) {
34784f9bd12SAlex Elder 			dev_err(dev,
34884f9bd12SAlex Elder 				"status endpoint for endpoint %u not RX\n",
34984f9bd12SAlex Elder 				data->endpoint_id);
35084f9bd12SAlex Elder 			return false;
35184f9bd12SAlex Elder 		}
35284f9bd12SAlex Elder 
35384f9bd12SAlex Elder 		/* ...and if it's to be an AP endpoint... */
35484f9bd12SAlex Elder 		if (other_data->ee_id == GSI_EE_AP) {
35584f9bd12SAlex Elder 			/* ...make sure it has status enabled. */
35684f9bd12SAlex Elder 			if (!other_data->endpoint.config.status_enable) {
35784f9bd12SAlex Elder 				dev_err(dev,
35884f9bd12SAlex Elder 					"status not enabled for endpoint %u\n",
35984f9bd12SAlex Elder 					other_data->endpoint_id);
36084f9bd12SAlex Elder 				return false;
36184f9bd12SAlex Elder 			}
36284f9bd12SAlex Elder 		}
36384f9bd12SAlex Elder 	}
36484f9bd12SAlex Elder 
36584f9bd12SAlex Elder 	if (data->endpoint.config.dma_mode) {
36684f9bd12SAlex Elder 		other_name = data->endpoint.config.dma_endpoint;
36784f9bd12SAlex Elder 		if (other_name >= count) {
36884f9bd12SAlex Elder 			dev_err(dev, "DMA endpoint name %u out of range "
36984f9bd12SAlex Elder 					"for endpoint %u\n",
37084f9bd12SAlex Elder 				other_name, data->endpoint_id);
37184f9bd12SAlex Elder 			return false;
37284f9bd12SAlex Elder 		}
37384f9bd12SAlex Elder 
37484f9bd12SAlex Elder 		other_data = &all_data[other_name];
37584f9bd12SAlex Elder 		if (ipa_gsi_endpoint_data_empty(other_data)) {
37684f9bd12SAlex Elder 			dev_err(dev, "DMA endpoint name %u undefined "
37784f9bd12SAlex Elder 					"for endpoint %u\n",
37884f9bd12SAlex Elder 				other_name, data->endpoint_id);
37984f9bd12SAlex Elder 			return false;
38084f9bd12SAlex Elder 		}
38184f9bd12SAlex Elder 	}
38284f9bd12SAlex Elder 
38384f9bd12SAlex Elder 	return true;
38484f9bd12SAlex Elder }
38584f9bd12SAlex Elder 
3865274c715SAlex Elder /* Validate endpoint configuration data.  Return max defined endpoint ID */
3875274c715SAlex Elder static u32 ipa_endpoint_max(struct ipa *ipa, u32 count,
38884f9bd12SAlex Elder 			    const struct ipa_gsi_endpoint_data *data)
38984f9bd12SAlex Elder {
39084f9bd12SAlex Elder 	const struct ipa_gsi_endpoint_data *dp = data;
391*5245f4fdSAlex Elder 	struct device *dev = ipa->dev;
39284f9bd12SAlex Elder 	enum ipa_endpoint_name name;
3935274c715SAlex Elder 	u32 max;
39484f9bd12SAlex Elder 
39584f9bd12SAlex Elder 	if (count > IPA_ENDPOINT_COUNT) {
39684f9bd12SAlex Elder 		dev_err(dev, "too many endpoints specified (%u > %u)\n",
39784f9bd12SAlex Elder 			count, IPA_ENDPOINT_COUNT);
3985274c715SAlex Elder 		return 0;
39984f9bd12SAlex Elder 	}
40084f9bd12SAlex Elder 
40184f9bd12SAlex Elder 	/* Make sure needed endpoints have defined data */
40284f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_COMMAND_TX])) {
40384f9bd12SAlex Elder 		dev_err(dev, "command TX endpoint not defined\n");
4045274c715SAlex Elder 		return 0;
40584f9bd12SAlex Elder 	}
40684f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_LAN_RX])) {
40784f9bd12SAlex Elder 		dev_err(dev, "LAN RX endpoint not defined\n");
4085274c715SAlex Elder 		return 0;
40984f9bd12SAlex Elder 	}
41084f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_MODEM_TX])) {
41184f9bd12SAlex Elder 		dev_err(dev, "AP->modem TX endpoint not defined\n");
4125274c715SAlex Elder 		return 0;
41384f9bd12SAlex Elder 	}
41484f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_MODEM_RX])) {
41584f9bd12SAlex Elder 		dev_err(dev, "AP<-modem RX endpoint not defined\n");
4165274c715SAlex Elder 		return 0;
41784f9bd12SAlex Elder 	}
41884f9bd12SAlex Elder 
4195274c715SAlex Elder 	max = 0;
4205274c715SAlex Elder 	for (name = 0; name < count; name++, dp++) {
42184f9bd12SAlex Elder 		if (!ipa_endpoint_data_valid_one(ipa, count, data, dp))
4225274c715SAlex Elder 			return 0;
4235274c715SAlex Elder 		max = max_t(u32, max, dp->endpoint_id);
4245274c715SAlex Elder 	}
42584f9bd12SAlex Elder 
4265274c715SAlex Elder 	return max;
42784f9bd12SAlex Elder }
42884f9bd12SAlex Elder 
42984f9bd12SAlex Elder /* Allocate a transaction to use on a non-command endpoint */
43084f9bd12SAlex Elder static struct gsi_trans *ipa_endpoint_trans_alloc(struct ipa_endpoint *endpoint,
43184f9bd12SAlex Elder 						  u32 tre_count)
43284f9bd12SAlex Elder {
43384f9bd12SAlex Elder 	struct gsi *gsi = &endpoint->ipa->gsi;
43484f9bd12SAlex Elder 	u32 channel_id = endpoint->channel_id;
43584f9bd12SAlex Elder 	enum dma_data_direction direction;
43684f9bd12SAlex Elder 
43784f9bd12SAlex Elder 	direction = endpoint->toward_ipa ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
43884f9bd12SAlex Elder 
43984f9bd12SAlex Elder 	return gsi_channel_trans_alloc(gsi, channel_id, tre_count, direction);
44084f9bd12SAlex Elder }
44184f9bd12SAlex Elder 
44284f9bd12SAlex Elder /* suspend_delay represents suspend for RX, delay for TX endpoints.
4434c9d631aSAlex Elder  * Note that suspend is not supported starting with IPA v4.0, and
4444c9d631aSAlex Elder  * delay mode should not be used starting with IPA v4.2.
44584f9bd12SAlex Elder  */
4464900bf34SAlex Elder static bool
44784f9bd12SAlex Elder ipa_endpoint_init_ctrl(struct ipa_endpoint *endpoint, bool suspend_delay)
44884f9bd12SAlex Elder {
44984f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
45081772e44SAlex Elder 	const struct reg *reg;
4514468a344SAlex Elder 	u32 field_id;
4526bfb7538SAlex Elder 	u32 offset;
4534900bf34SAlex Elder 	bool state;
45484f9bd12SAlex Elder 	u32 mask;
45584f9bd12SAlex Elder 	u32 val;
45684f9bd12SAlex Elder 
4575bc55884SAlex Elder 	if (endpoint->toward_ipa)
4584c9d631aSAlex Elder 		WARN_ON(ipa->version >= IPA_VERSION_4_2);
4595bc55884SAlex Elder 	else
4605bc55884SAlex Elder 		WARN_ON(ipa->version >= IPA_VERSION_4_0);
4615bc55884SAlex Elder 
4626a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_CTRL);
463fc4cecf7SAlex Elder 	offset = reg_n_offset(reg, endpoint->endpoint_id);
46484f9bd12SAlex Elder 	val = ioread32(ipa->reg_virt + offset);
4656a244b75SAlex Elder 
4664468a344SAlex Elder 	field_id = endpoint->toward_ipa ? ENDP_DELAY : ENDP_SUSPEND;
467f1470fd7SAlex Elder 	mask = reg_bit(reg, field_id);
4684468a344SAlex Elder 
4694900bf34SAlex Elder 	state = !!(val & mask);
4705bc55884SAlex Elder 
4715bc55884SAlex Elder 	/* Don't bother if it's already in the requested state */
4724900bf34SAlex Elder 	if (suspend_delay != state) {
47384f9bd12SAlex Elder 		val ^= mask;
47484f9bd12SAlex Elder 		iowrite32(val, ipa->reg_virt + offset);
4754900bf34SAlex Elder 	}
47684f9bd12SAlex Elder 
4774900bf34SAlex Elder 	return state;
47884f9bd12SAlex Elder }
47984f9bd12SAlex Elder 
4804c9d631aSAlex Elder /* We don't care what the previous state was for delay mode */
4814fa95248SAlex Elder static void
4824fa95248SAlex Elder ipa_endpoint_program_delay(struct ipa_endpoint *endpoint, bool enable)
4834fa95248SAlex Elder {
4844c9d631aSAlex Elder 	/* Delay mode should not be used for IPA v4.2+ */
4854c9d631aSAlex Elder 	WARN_ON(endpoint->ipa->version >= IPA_VERSION_4_2);
4865bc55884SAlex Elder 	WARN_ON(!endpoint->toward_ipa);
4874fa95248SAlex Elder 
4884fa95248SAlex Elder 	(void)ipa_endpoint_init_ctrl(endpoint, enable);
4894fa95248SAlex Elder }
4904fa95248SAlex Elder 
491fff89971SAlex Elder static bool ipa_endpoint_aggr_active(struct ipa_endpoint *endpoint)
492fff89971SAlex Elder {
4931d8f16dbSAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
494fff89971SAlex Elder 	struct ipa *ipa = endpoint->ipa;
4951d8f16dbSAlex Elder 	u32 unit = endpoint_id / 32;
49681772e44SAlex Elder 	const struct reg *reg;
497fff89971SAlex Elder 	u32 val;
498fff89971SAlex Elder 
49988de7672SAlex Elder 	WARN_ON(!test_bit(endpoint_id, ipa->available));
5005bc55884SAlex Elder 
5016a244b75SAlex Elder 	reg = ipa_reg(ipa, STATE_AGGR_ACTIVE);
502fc4cecf7SAlex Elder 	val = ioread32(ipa->reg_virt + reg_n_offset(reg, unit));
503fff89971SAlex Elder 
50488de7672SAlex Elder 	return !!(val & BIT(endpoint_id % 32));
505fff89971SAlex Elder }
506fff89971SAlex Elder 
507fff89971SAlex Elder static void ipa_endpoint_force_close(struct ipa_endpoint *endpoint)
508fff89971SAlex Elder {
5091d8f16dbSAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
5101d8f16dbSAlex Elder 	u32 mask = BIT(endpoint_id % 32);
511fff89971SAlex Elder 	struct ipa *ipa = endpoint->ipa;
5121d8f16dbSAlex Elder 	u32 unit = endpoint_id / 32;
51381772e44SAlex Elder 	const struct reg *reg;
514fff89971SAlex Elder 
51588de7672SAlex Elder 	WARN_ON(!test_bit(endpoint_id, ipa->available));
5165bc55884SAlex Elder 
5176a244b75SAlex Elder 	reg = ipa_reg(ipa, AGGR_FORCE_CLOSE);
518fc4cecf7SAlex Elder 	iowrite32(mask, ipa->reg_virt + reg_n_offset(reg, unit));
519fff89971SAlex Elder }
520fff89971SAlex Elder 
521fff89971SAlex Elder /**
522fff89971SAlex Elder  * ipa_endpoint_suspend_aggr() - Emulate suspend interrupt
523e3eea08eSAlex Elder  * @endpoint:	Endpoint on which to emulate a suspend
524fff89971SAlex Elder  *
525fff89971SAlex Elder  *  Emulate suspend IPA interrupt to unsuspend an endpoint suspended
526fff89971SAlex Elder  *  with an open aggregation frame.  This is to work around a hardware
527fff89971SAlex Elder  *  issue in IPA version 3.5.1 where the suspend interrupt will not be
528fff89971SAlex Elder  *  generated when it should be.
529fff89971SAlex Elder  */
530fff89971SAlex Elder static void ipa_endpoint_suspend_aggr(struct ipa_endpoint *endpoint)
531fff89971SAlex Elder {
532fff89971SAlex Elder 	struct ipa *ipa = endpoint->ipa;
533fff89971SAlex Elder 
534660e52d6SAlex Elder 	if (!endpoint->config.aggregation)
535fff89971SAlex Elder 		return;
536fff89971SAlex Elder 
537fff89971SAlex Elder 	/* Nothing to do if the endpoint doesn't have aggregation open */
538fff89971SAlex Elder 	if (!ipa_endpoint_aggr_active(endpoint))
539fff89971SAlex Elder 		return;
540fff89971SAlex Elder 
541fff89971SAlex Elder 	/* Force close aggregation */
542fff89971SAlex Elder 	ipa_endpoint_force_close(endpoint);
543fff89971SAlex Elder 
544fff89971SAlex Elder 	ipa_interrupt_simulate_suspend(ipa->interrupt);
545fff89971SAlex Elder }
546fff89971SAlex Elder 
547fff89971SAlex Elder /* Returns previous suspend state (true means suspend was enabled) */
5484fa95248SAlex Elder static bool
5494fa95248SAlex Elder ipa_endpoint_program_suspend(struct ipa_endpoint *endpoint, bool enable)
5504fa95248SAlex Elder {
551fff89971SAlex Elder 	bool suspended;
552fff89971SAlex Elder 
553d7f3087bSAlex Elder 	if (endpoint->ipa->version >= IPA_VERSION_4_0)
554b07f283eSAlex Elder 		return enable;	/* For IPA v4.0+, no change made */
555b07f283eSAlex Elder 
5565bc55884SAlex Elder 	WARN_ON(endpoint->toward_ipa);
5574fa95248SAlex Elder 
558fff89971SAlex Elder 	suspended = ipa_endpoint_init_ctrl(endpoint, enable);
559fff89971SAlex Elder 
560fff89971SAlex Elder 	/* A client suspended with an open aggregation frame will not
561fff89971SAlex Elder 	 * generate a SUSPEND IPA interrupt.  If enabling suspend, have
562fff89971SAlex Elder 	 * ipa_endpoint_suspend_aggr() handle this.
563fff89971SAlex Elder 	 */
564fff89971SAlex Elder 	if (enable && !suspended)
565fff89971SAlex Elder 		ipa_endpoint_suspend_aggr(endpoint);
566fff89971SAlex Elder 
567fff89971SAlex Elder 	return suspended;
5684fa95248SAlex Elder }
5694fa95248SAlex Elder 
5704c9d631aSAlex Elder /* Put all modem RX endpoints into suspend mode, and stop transmission
5714c9d631aSAlex Elder  * on all modem TX endpoints.  Prior to IPA v4.2, endpoint DELAY mode is
5724c9d631aSAlex Elder  * used for TX endpoints; starting with IPA v4.2 we use GSI channel flow
5734c9d631aSAlex Elder  * control instead.
5744c9d631aSAlex Elder  */
57584f9bd12SAlex Elder void ipa_endpoint_modem_pause_all(struct ipa *ipa, bool enable)
57684f9bd12SAlex Elder {
577e359ba89SAlex Elder 	u32 endpoint_id = 0;
57884f9bd12SAlex Elder 
579b7aaff0bSAlex Elder 	while (endpoint_id < ipa->endpoint_count) {
580e359ba89SAlex Elder 		struct ipa_endpoint *endpoint = &ipa->endpoint[endpoint_id++];
58184f9bd12SAlex Elder 
58284f9bd12SAlex Elder 		if (endpoint->ee_id != GSI_EE_MODEM)
58384f9bd12SAlex Elder 			continue;
58484f9bd12SAlex Elder 
5854c9d631aSAlex Elder 		if (!endpoint->toward_ipa)
5864c9d631aSAlex Elder 			(void)ipa_endpoint_program_suspend(endpoint, enable);
5874c9d631aSAlex Elder 		else if (ipa->version < IPA_VERSION_4_2)
5884fa95248SAlex Elder 			ipa_endpoint_program_delay(endpoint, enable);
589b07f283eSAlex Elder 		else
5904c9d631aSAlex Elder 			gsi_modem_channel_flow_control(&ipa->gsi,
5914c9d631aSAlex Elder 						       endpoint->channel_id,
5924c9d631aSAlex Elder 						       enable);
59384f9bd12SAlex Elder 	}
59484f9bd12SAlex Elder }
59584f9bd12SAlex Elder 
59684f9bd12SAlex Elder /* Reset all modem endpoints to use the default exception endpoint */
59784f9bd12SAlex Elder int ipa_endpoint_modem_exception_reset_all(struct ipa *ipa)
59884f9bd12SAlex Elder {
59984f9bd12SAlex Elder 	struct gsi_trans *trans;
6009a9f5129SAlex Elder 	u32 endpoint_id;
60184f9bd12SAlex Elder 	u32 count;
60284f9bd12SAlex Elder 
6032091c79aSAlex Elder 	/* We need one command per modem TX endpoint, plus the commands
6042091c79aSAlex Elder 	 * that clear the pipeline.
60584f9bd12SAlex Elder 	 */
6062091c79aSAlex Elder 	count = ipa->modem_tx_count + ipa_cmd_pipeline_clear_count();
60784f9bd12SAlex Elder 	trans = ipa_cmd_trans_alloc(ipa, count);
60884f9bd12SAlex Elder 	if (!trans) {
609*5245f4fdSAlex Elder 		dev_err(ipa->dev,
61084f9bd12SAlex Elder 			"no transaction to reset modem exception endpoints\n");
61184f9bd12SAlex Elder 		return -EBUSY;
61284f9bd12SAlex Elder 	}
61384f9bd12SAlex Elder 
6149a9f5129SAlex Elder 	for_each_set_bit(endpoint_id, ipa->defined, ipa->endpoint_count) {
61584f9bd12SAlex Elder 		struct ipa_endpoint *endpoint;
61681772e44SAlex Elder 		const struct reg *reg;
61784f9bd12SAlex Elder 		u32 offset;
61884f9bd12SAlex Elder 
61984f9bd12SAlex Elder 		/* We only reset modem TX endpoints */
62084f9bd12SAlex Elder 		endpoint = &ipa->endpoint[endpoint_id];
62184f9bd12SAlex Elder 		if (!(endpoint->ee_id == GSI_EE_MODEM && endpoint->toward_ipa))
62284f9bd12SAlex Elder 			continue;
62384f9bd12SAlex Elder 
6246a244b75SAlex Elder 		reg = ipa_reg(ipa, ENDP_STATUS);
625fc4cecf7SAlex Elder 		offset = reg_n_offset(reg, endpoint_id);
62684f9bd12SAlex Elder 
62784f9bd12SAlex Elder 		/* Value written is 0, and all bits are updated.  That
62884f9bd12SAlex Elder 		 * means status is disabled on the endpoint, and as a
62984f9bd12SAlex Elder 		 * result all other fields in the register are ignored.
63084f9bd12SAlex Elder 		 */
63184f9bd12SAlex Elder 		ipa_cmd_register_write_add(trans, offset, 0, ~0, false);
63284f9bd12SAlex Elder 	}
63384f9bd12SAlex Elder 
634aa56e3e5SAlex Elder 	ipa_cmd_pipeline_clear_add(trans);
63584f9bd12SAlex Elder 
63684f9bd12SAlex Elder 	gsi_trans_commit_wait(trans);
63784f9bd12SAlex Elder 
63851c48ce2SAlex Elder 	ipa_cmd_pipeline_clear_wait(ipa);
63951c48ce2SAlex Elder 
64084f9bd12SAlex Elder 	return 0;
64184f9bd12SAlex Elder }
64284f9bd12SAlex Elder 
64384f9bd12SAlex Elder static void ipa_endpoint_init_cfg(struct ipa_endpoint *endpoint)
64484f9bd12SAlex Elder {
6456a244b75SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
6466bfb7538SAlex Elder 	struct ipa *ipa = endpoint->ipa;
6475567d4d9SAlex Elder 	enum ipa_cs_offload_en enabled;
64881772e44SAlex Elder 	const struct reg *reg;
64984f9bd12SAlex Elder 	u32 val = 0;
6506bfb7538SAlex Elder 
6516a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_CFG);
65284f9bd12SAlex Elder 	/* FRAG_OFFLOAD_EN is 0 */
653660e52d6SAlex Elder 	if (endpoint->config.checksum) {
6546bfb7538SAlex Elder 		enum ipa_version version = ipa->version;
6555567d4d9SAlex Elder 
65684f9bd12SAlex Elder 		if (endpoint->toward_ipa) {
6579eefd2fbSAlex Elder 			u32 off;
65884f9bd12SAlex Elder 
65984f9bd12SAlex Elder 			/* Checksum header offset is in 4-byte units */
6604468a344SAlex Elder 			off = sizeof(struct rmnet_map_header) / sizeof(u32);
661f1470fd7SAlex Elder 			val |= reg_encode(reg, CS_METADATA_HDR_OFFSET, off);
6625567d4d9SAlex Elder 
6635567d4d9SAlex Elder 			enabled = version < IPA_VERSION_4_5
6645567d4d9SAlex Elder 					? IPA_CS_OFFLOAD_UL
6655567d4d9SAlex Elder 					: IPA_CS_OFFLOAD_INLINE;
66684f9bd12SAlex Elder 		} else {
6675567d4d9SAlex Elder 			enabled = version < IPA_VERSION_4_5
6685567d4d9SAlex Elder 					? IPA_CS_OFFLOAD_DL
6695567d4d9SAlex Elder 					: IPA_CS_OFFLOAD_INLINE;
67084f9bd12SAlex Elder 		}
67184f9bd12SAlex Elder 	} else {
6725567d4d9SAlex Elder 		enabled = IPA_CS_OFFLOAD_NONE;
67384f9bd12SAlex Elder 	}
674f1470fd7SAlex Elder 	val |= reg_encode(reg, CS_OFFLOAD_EN, enabled);
67584f9bd12SAlex Elder 	/* CS_GEN_QMB_MASTER_SEL is 0 */
67684f9bd12SAlex Elder 
677fc4cecf7SAlex Elder 	iowrite32(val, ipa->reg_virt + reg_n_offset(reg, endpoint_id));
67884f9bd12SAlex Elder }
67984f9bd12SAlex Elder 
680647a05f3SAlex Elder static void ipa_endpoint_init_nat(struct ipa_endpoint *endpoint)
681647a05f3SAlex Elder {
6826a244b75SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
6836bfb7538SAlex Elder 	struct ipa *ipa = endpoint->ipa;
68481772e44SAlex Elder 	const struct reg *reg;
685647a05f3SAlex Elder 	u32 val;
686647a05f3SAlex Elder 
687647a05f3SAlex Elder 	if (!endpoint->toward_ipa)
688647a05f3SAlex Elder 		return;
689647a05f3SAlex Elder 
6906a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_NAT);
691f1470fd7SAlex Elder 	val = reg_encode(reg, NAT_EN, IPA_NAT_TYPE_BYPASS);
692647a05f3SAlex Elder 
693fc4cecf7SAlex Elder 	iowrite32(val, ipa->reg_virt + reg_n_offset(reg, endpoint_id));
694647a05f3SAlex Elder }
695647a05f3SAlex Elder 
6965567d4d9SAlex Elder static u32
6975567d4d9SAlex Elder ipa_qmap_header_size(enum ipa_version version, struct ipa_endpoint *endpoint)
6985567d4d9SAlex Elder {
6995567d4d9SAlex Elder 	u32 header_size = sizeof(struct rmnet_map_header);
7005567d4d9SAlex Elder 
7015567d4d9SAlex Elder 	/* Without checksum offload, we just have the MAP header */
702660e52d6SAlex Elder 	if (!endpoint->config.checksum)
7035567d4d9SAlex Elder 		return header_size;
7045567d4d9SAlex Elder 
7055567d4d9SAlex Elder 	if (version < IPA_VERSION_4_5) {
7065567d4d9SAlex Elder 		/* Checksum header inserted for AP TX endpoints only */
7075567d4d9SAlex Elder 		if (endpoint->toward_ipa)
7085567d4d9SAlex Elder 			header_size += sizeof(struct rmnet_map_ul_csum_header);
7095567d4d9SAlex Elder 	} else {
7105567d4d9SAlex Elder 		/* Checksum header is used in both directions */
7115567d4d9SAlex Elder 		header_size += sizeof(struct rmnet_map_v5_csum_header);
7125567d4d9SAlex Elder 	}
7135567d4d9SAlex Elder 
7145567d4d9SAlex Elder 	return header_size;
7155567d4d9SAlex Elder }
7165567d4d9SAlex Elder 
7174468a344SAlex Elder /* Encoded value for ENDP_INIT_HDR register HDR_LEN* field(s) */
7184468a344SAlex Elder static u32 ipa_header_size_encode(enum ipa_version version,
71981772e44SAlex Elder 				  const struct reg *reg, u32 header_size)
7204468a344SAlex Elder {
721f1470fd7SAlex Elder 	u32 field_max = reg_field_max(reg, HDR_LEN);
7224468a344SAlex Elder 	u32 val;
7234468a344SAlex Elder 
7244468a344SAlex Elder 	/* We know field_max can be used as a mask (2^n - 1) */
725f1470fd7SAlex Elder 	val = reg_encode(reg, HDR_LEN, header_size & field_max);
7264468a344SAlex Elder 	if (version < IPA_VERSION_4_5) {
7274468a344SAlex Elder 		WARN_ON(header_size > field_max);
7284468a344SAlex Elder 		return val;
7294468a344SAlex Elder 	}
7304468a344SAlex Elder 
7314468a344SAlex Elder 	/* IPA v4.5 adds a few more most-significant bits */
7324468a344SAlex Elder 	header_size >>= hweight32(field_max);
733f1470fd7SAlex Elder 	WARN_ON(header_size > reg_field_max(reg, HDR_LEN_MSB));
734f1470fd7SAlex Elder 	val |= reg_encode(reg, HDR_LEN_MSB, header_size);
7354468a344SAlex Elder 
7364468a344SAlex Elder 	return val;
7374468a344SAlex Elder }
7384468a344SAlex Elder 
7394468a344SAlex Elder /* Encoded value for ENDP_INIT_HDR register OFST_METADATA* field(s) */
7404468a344SAlex Elder static u32 ipa_metadata_offset_encode(enum ipa_version version,
74181772e44SAlex Elder 				      const struct reg *reg, u32 offset)
7424468a344SAlex Elder {
743f1470fd7SAlex Elder 	u32 field_max = reg_field_max(reg, HDR_OFST_METADATA);
7444468a344SAlex Elder 	u32 val;
7454468a344SAlex Elder 
7464468a344SAlex Elder 	/* We know field_max can be used as a mask (2^n - 1) */
747f1470fd7SAlex Elder 	val = reg_encode(reg, HDR_OFST_METADATA, offset);
7484468a344SAlex Elder 	if (version < IPA_VERSION_4_5) {
7494468a344SAlex Elder 		WARN_ON(offset > field_max);
7504468a344SAlex Elder 		return val;
7514468a344SAlex Elder 	}
7524468a344SAlex Elder 
7534468a344SAlex Elder 	/* IPA v4.5 adds a few more most-significant bits */
7544468a344SAlex Elder 	offset >>= hweight32(field_max);
755f1470fd7SAlex Elder 	WARN_ON(offset > reg_field_max(reg, HDR_OFST_METADATA_MSB));
756f1470fd7SAlex Elder 	val |= reg_encode(reg, HDR_OFST_METADATA_MSB, offset);
7574468a344SAlex Elder 
7584468a344SAlex Elder 	return val;
7594468a344SAlex Elder }
7604468a344SAlex Elder 
7618730f45dSAlex Elder /**
762e3eea08eSAlex Elder  * ipa_endpoint_init_hdr() - Initialize HDR endpoint configuration register
763e3eea08eSAlex Elder  * @endpoint:	Endpoint pointer
764e3eea08eSAlex Elder  *
7658730f45dSAlex Elder  * We program QMAP endpoints so each packet received is preceded by a QMAP
7668730f45dSAlex Elder  * header structure.  The QMAP header contains a 1-byte mux_id and 2-byte
7678730f45dSAlex Elder  * packet size field, and we have the IPA hardware populate both for each
7688730f45dSAlex Elder  * received packet.  The header is configured (in the HDR_EXT register)
7698730f45dSAlex Elder  * to use big endian format.
7708730f45dSAlex Elder  *
7718730f45dSAlex Elder  * The packet size is written into the QMAP header's pkt_len field.  That
7728730f45dSAlex Elder  * location is defined here using the HDR_OFST_PKT_SIZE field.
7738730f45dSAlex Elder  *
7748730f45dSAlex Elder  * The mux_id comes from a 4-byte metadata value supplied with each packet
7758730f45dSAlex Elder  * by the modem.  It is *not* a QMAP header, but it does contain the mux_id
7768730f45dSAlex Elder  * value that we want, in its low-order byte.  A bitmask defined in the
7778730f45dSAlex Elder  * endpoint's METADATA_MASK register defines which byte within the modem
7788730f45dSAlex Elder  * metadata contains the mux_id.  And the OFST_METADATA field programmed
7798730f45dSAlex Elder  * here indicates where the extracted byte should be placed within the QMAP
7808730f45dSAlex Elder  * header.
7818730f45dSAlex Elder  */
78284f9bd12SAlex Elder static void ipa_endpoint_init_hdr(struct ipa_endpoint *endpoint)
78384f9bd12SAlex Elder {
7846a244b75SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
7851af15c2aSAlex Elder 	struct ipa *ipa = endpoint->ipa;
78681772e44SAlex Elder 	const struct reg *reg;
78784f9bd12SAlex Elder 	u32 val = 0;
7886bfb7538SAlex Elder 
7896a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_HDR);
790660e52d6SAlex Elder 	if (endpoint->config.qmap) {
7911af15c2aSAlex Elder 		enum ipa_version version = ipa->version;
7925567d4d9SAlex Elder 		size_t header_size;
79384f9bd12SAlex Elder 
7945567d4d9SAlex Elder 		header_size = ipa_qmap_header_size(version, endpoint);
7954468a344SAlex Elder 		val = ipa_header_size_encode(version, reg, header_size);
79684f9bd12SAlex Elder 
797f330fda3SAlex Elder 		/* Define how to fill fields in a received QMAP header */
7988730f45dSAlex Elder 		if (!endpoint->toward_ipa) {
7999eefd2fbSAlex Elder 			u32 off;     /* Field offset within header */
8008730f45dSAlex Elder 
8018730f45dSAlex Elder 			/* Where IPA will write the metadata value */
8029eefd2fbSAlex Elder 			off = offsetof(struct rmnet_map_header, mux_id);
8034468a344SAlex Elder 			val |= ipa_metadata_offset_encode(version, reg, off);
8048730f45dSAlex Elder 
8058730f45dSAlex Elder 			/* Where IPA will write the length */
8069eefd2fbSAlex Elder 			off = offsetof(struct rmnet_map_header, pkt_len);
8071af15c2aSAlex Elder 			/* Upper bits are stored in HDR_EXT with IPA v4.5 */
808d7f3087bSAlex Elder 			if (version >= IPA_VERSION_4_5)
809f1470fd7SAlex Elder 				off &= reg_field_max(reg, HDR_OFST_PKT_SIZE);
8101af15c2aSAlex Elder 
811f1470fd7SAlex Elder 			val |= reg_bit(reg, HDR_OFST_PKT_SIZE_VALID);
812f1470fd7SAlex Elder 			val |= reg_encode(reg, HDR_OFST_PKT_SIZE, off);
81384f9bd12SAlex Elder 		}
8148730f45dSAlex Elder 		/* For QMAP TX, metadata offset is 0 (modem assumes this) */
815f1470fd7SAlex Elder 		val |= reg_bit(reg, HDR_OFST_METADATA_VALID);
8168730f45dSAlex Elder 
8178730f45dSAlex Elder 		/* HDR_ADDITIONAL_CONST_LEN is 0; (RX only) */
81884f9bd12SAlex Elder 		/* HDR_A5_MUX is 0 */
81984f9bd12SAlex Elder 		/* HDR_LEN_INC_DEAGG_HDR is 0 */
8208bfc4e21SAlex Elder 		/* HDR_METADATA_REG_VALID is 0 (TX only, version < v4.5) */
82184f9bd12SAlex Elder 	}
82284f9bd12SAlex Elder 
823fc4cecf7SAlex Elder 	iowrite32(val, ipa->reg_virt + reg_n_offset(reg, endpoint_id));
82484f9bd12SAlex Elder }
82584f9bd12SAlex Elder 
82684f9bd12SAlex Elder static void ipa_endpoint_init_hdr_ext(struct ipa_endpoint *endpoint)
82784f9bd12SAlex Elder {
828660e52d6SAlex Elder 	u32 pad_align = endpoint->config.rx.pad_align;
8296a244b75SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
8301af15c2aSAlex Elder 	struct ipa *ipa = endpoint->ipa;
83181772e44SAlex Elder 	const struct reg *reg;
83284f9bd12SAlex Elder 	u32 val = 0;
8336bfb7538SAlex Elder 
8346a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_HDR_EXT);
835660e52d6SAlex Elder 	if (endpoint->config.qmap) {
836332ef7c8SAlex Elder 		/* We have a header, so we must specify its endianness */
837f1470fd7SAlex Elder 		val |= reg_bit(reg, HDR_ENDIANNESS);	/* big endian */
838f330fda3SAlex Elder 
839332ef7c8SAlex Elder 		/* A QMAP header contains a 6 bit pad field at offset 0.
840332ef7c8SAlex Elder 		 * The RMNet driver assumes this field is meaningful in
841332ef7c8SAlex Elder 		 * packets it receives, and assumes the header's payload
842332ef7c8SAlex Elder 		 * length includes that padding.  The RMNet driver does
843332ef7c8SAlex Elder 		 * *not* pad packets it sends, however, so the pad field
844332ef7c8SAlex Elder 		 * (although 0) should be ignored.
845f330fda3SAlex Elder 		 */
846332ef7c8SAlex Elder 		if (!endpoint->toward_ipa) {
847f1470fd7SAlex Elder 			val |= reg_bit(reg, HDR_TOTAL_LEN_OR_PAD_VALID);
84884f9bd12SAlex Elder 			/* HDR_TOTAL_LEN_OR_PAD is 0 (pad, not total_len) */
849f1470fd7SAlex Elder 			val |= reg_bit(reg, HDR_PAYLOAD_LEN_INC_PADDING);
85084f9bd12SAlex Elder 			/* HDR_TOTAL_LEN_OR_PAD_OFFSET is 0 */
851f330fda3SAlex Elder 		}
852332ef7c8SAlex Elder 	}
853f330fda3SAlex Elder 
854f330fda3SAlex Elder 	/* HDR_PAYLOAD_LEN_INC_PADDING is 0 */
85584f9bd12SAlex Elder 	if (!endpoint->toward_ipa)
856f1470fd7SAlex Elder 		val |= reg_encode(reg, HDR_PAD_TO_ALIGNMENT, pad_align);
85784f9bd12SAlex Elder 
8581af15c2aSAlex Elder 	/* IPA v4.5 adds some most-significant bits to a few fields,
8591af15c2aSAlex Elder 	 * two of which are defined in the HDR (not HDR_EXT) register.
8601af15c2aSAlex Elder 	 */
861d7f3087bSAlex Elder 	if (ipa->version >= IPA_VERSION_4_5) {
8621af15c2aSAlex Elder 		/* HDR_TOTAL_LEN_OR_PAD_OFFSET is 0, so MSB is 0 */
863660e52d6SAlex Elder 		if (endpoint->config.qmap && !endpoint->toward_ipa) {
864f1470fd7SAlex Elder 			u32 mask = reg_field_max(reg, HDR_OFST_PKT_SIZE);
8656bfb7538SAlex Elder 			u32 off;     /* Field offset within header */
86684f9bd12SAlex Elder 
8679eefd2fbSAlex Elder 			off = offsetof(struct rmnet_map_header, pkt_len);
8684468a344SAlex Elder 			/* Low bits are in the ENDP_INIT_HDR register */
8694468a344SAlex Elder 			off >>= hweight32(mask);
870f1470fd7SAlex Elder 			val |= reg_encode(reg, HDR_OFST_PKT_SIZE_MSB, off);
8711af15c2aSAlex Elder 			/* HDR_ADDITIONAL_CONST_LEN is 0 so MSB is 0 */
8721af15c2aSAlex Elder 		}
8731af15c2aSAlex Elder 	}
8746bfb7538SAlex Elder 
875fc4cecf7SAlex Elder 	iowrite32(val, ipa->reg_virt + reg_n_offset(reg, endpoint_id));
8761af15c2aSAlex Elder }
87784f9bd12SAlex Elder 
87884f9bd12SAlex Elder static void ipa_endpoint_init_hdr_metadata_mask(struct ipa_endpoint *endpoint)
87984f9bd12SAlex Elder {
88084f9bd12SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
8816bfb7538SAlex Elder 	struct ipa *ipa = endpoint->ipa;
88281772e44SAlex Elder 	const struct reg *reg;
88384f9bd12SAlex Elder 	u32 val = 0;
88484f9bd12SAlex Elder 	u32 offset;
88584f9bd12SAlex Elder 
886fb57c3eaSAlex Elder 	if (endpoint->toward_ipa)
887fb57c3eaSAlex Elder 		return;		/* Register not valid for TX endpoints */
888fb57c3eaSAlex Elder 
8896a244b75SAlex Elder 	reg = ipa_reg(ipa,  ENDP_INIT_HDR_METADATA_MASK);
890fc4cecf7SAlex Elder 	offset = reg_n_offset(reg, endpoint_id);
89184f9bd12SAlex Elder 
8928730f45dSAlex Elder 	/* Note that HDR_ENDIANNESS indicates big endian header fields */
893660e52d6SAlex Elder 	if (endpoint->config.qmap)
894088f8a23SAlex Elder 		val = (__force u32)cpu_to_be32(IPA_ENDPOINT_QMAP_METADATA_MASK);
89584f9bd12SAlex Elder 
8966bfb7538SAlex Elder 	iowrite32(val, ipa->reg_virt + offset);
89784f9bd12SAlex Elder }
89884f9bd12SAlex Elder 
89984f9bd12SAlex Elder static void ipa_endpoint_init_mode(struct ipa_endpoint *endpoint)
90084f9bd12SAlex Elder {
9016bfb7538SAlex Elder 	struct ipa *ipa = endpoint->ipa;
90281772e44SAlex Elder 	const struct reg *reg;
9036bfb7538SAlex Elder 	u32 offset;
90484f9bd12SAlex Elder 	u32 val;
90584f9bd12SAlex Elder 
906fb57c3eaSAlex Elder 	if (!endpoint->toward_ipa)
907fb57c3eaSAlex Elder 		return;		/* Register not valid for RX endpoints */
908fb57c3eaSAlex Elder 
9096a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_MODE);
910660e52d6SAlex Elder 	if (endpoint->config.dma_mode) {
911660e52d6SAlex Elder 		enum ipa_endpoint_name name = endpoint->config.dma_endpoint;
912216b409dSAlex Elder 		u32 dma_endpoint_id = ipa->name_map[name]->endpoint_id;
91384f9bd12SAlex Elder 
914f1470fd7SAlex Elder 		val = reg_encode(reg, ENDP_MODE, IPA_DMA);
915f1470fd7SAlex Elder 		val |= reg_encode(reg, DEST_PIPE_INDEX, dma_endpoint_id);
91684f9bd12SAlex Elder 	} else {
917f1470fd7SAlex Elder 		val = reg_encode(reg, ENDP_MODE, IPA_BASIC);
91884f9bd12SAlex Elder 	}
91900b9102aSAlex Elder 	/* All other bits unspecified (and 0) */
92084f9bd12SAlex Elder 
921fc4cecf7SAlex Elder 	offset = reg_n_offset(reg, endpoint->endpoint_id);
9226bfb7538SAlex Elder 	iowrite32(val, ipa->reg_virt + offset);
92384f9bd12SAlex Elder }
92484f9bd12SAlex Elder 
9252cdbcbfdSAlex Elder /* For IPA v4.5+, times are expressed using Qtime.  A time is represented
9262cdbcbfdSAlex Elder  * at one of several available granularities, which are configured in
9272cdbcbfdSAlex Elder  * ipa_qtime_config().  Three (or, starting with IPA v5.0, four) pulse
9282cdbcbfdSAlex Elder  * generators are set up with different "tick" periods.  A Qtime value
9292cdbcbfdSAlex Elder  * encodes a tick count along with an indication of a pulse generator
9302cdbcbfdSAlex Elder  * (which has a fixed tick period).  Two pulse generators are always
9312cdbcbfdSAlex Elder  * available to the AP; a third is available starting with IPA v5.0.
9322cdbcbfdSAlex Elder  * This function determines which pulse generator most accurately
9332cdbcbfdSAlex Elder  * represents the time period provided, and returns the tick count to
9342cdbcbfdSAlex Elder  * use to represent that time.
9358be440e1SAlex Elder  */
9362cdbcbfdSAlex Elder static u32
9372cdbcbfdSAlex Elder ipa_qtime_val(struct ipa *ipa, u32 microseconds, u32 max, u32 *select)
9388be440e1SAlex Elder {
9392cdbcbfdSAlex Elder 	u32 which = 0;
9402cdbcbfdSAlex Elder 	u32 ticks;
9418be440e1SAlex Elder 
9422cdbcbfdSAlex Elder 	/* Pulse generator 0 has 100 microsecond granularity */
9432cdbcbfdSAlex Elder 	ticks = DIV_ROUND_CLOSEST(microseconds, 100);
9442cdbcbfdSAlex Elder 	if (ticks <= max)
9452cdbcbfdSAlex Elder 		goto out;
9468be440e1SAlex Elder 
9472cdbcbfdSAlex Elder 	/* Pulse generator 1 has millisecond granularity */
9482cdbcbfdSAlex Elder 	which = 1;
9492cdbcbfdSAlex Elder 	ticks = DIV_ROUND_CLOSEST(microseconds, 1000);
9502cdbcbfdSAlex Elder 	if (ticks <= max)
9512cdbcbfdSAlex Elder 		goto out;
9528be440e1SAlex Elder 
9532cdbcbfdSAlex Elder 	if (ipa->version >= IPA_VERSION_5_0) {
9542cdbcbfdSAlex Elder 		/* Pulse generator 2 has 10 millisecond granularity */
9552cdbcbfdSAlex Elder 		which = 2;
9562cdbcbfdSAlex Elder 		ticks = DIV_ROUND_CLOSEST(microseconds, 100);
9572cdbcbfdSAlex Elder 	}
9582cdbcbfdSAlex Elder 	WARN_ON(ticks > max);
9592cdbcbfdSAlex Elder out:
9602cdbcbfdSAlex Elder 	*select = which;
9612cdbcbfdSAlex Elder 
9622cdbcbfdSAlex Elder 	return ticks;
9638be440e1SAlex Elder }
9648be440e1SAlex Elder 
96519547041SAlex Elder /* Encode the aggregation timer limit (microseconds) based on IPA version */
96681772e44SAlex Elder static u32 aggr_time_limit_encode(struct ipa *ipa, const struct reg *reg,
967216b409dSAlex Elder 				  u32 microseconds)
9686bf754c7SAlex Elder {
9692cdbcbfdSAlex Elder 	u32 ticks;
970216b409dSAlex Elder 	u32 max;
97148395fa8SAlex Elder 
97248395fa8SAlex Elder 	if (!microseconds)
97348395fa8SAlex Elder 		return 0;	/* Nothing to compute if time limit is 0 */
97448395fa8SAlex Elder 
975f1470fd7SAlex Elder 	max = reg_field_max(reg, TIME_LIMIT);
976216b409dSAlex Elder 	if (ipa->version >= IPA_VERSION_4_5) {
9772cdbcbfdSAlex Elder 		u32 select;
9786bf754c7SAlex Elder 
9792cdbcbfdSAlex Elder 		ticks = ipa_qtime_val(ipa, microseconds, max, &select);
98019547041SAlex Elder 
981f1470fd7SAlex Elder 		return reg_encode(reg, AGGR_GRAN_SEL, select) |
982f1470fd7SAlex Elder 		       reg_encode(reg, TIME_LIMIT, ticks);
9836bf754c7SAlex Elder 	}
9846bf754c7SAlex Elder 
985216b409dSAlex Elder 	/* We program aggregation granularity in ipa_hardware_config() */
9862cdbcbfdSAlex Elder 	ticks = DIV_ROUND_CLOSEST(microseconds, IPA_AGGR_GRANULARITY);
9872cdbcbfdSAlex Elder 	WARN(ticks > max, "aggr_time_limit too large (%u > %u usec)\n",
988216b409dSAlex Elder 	     microseconds, max * IPA_AGGR_GRANULARITY);
98948395fa8SAlex Elder 
990f1470fd7SAlex Elder 	return reg_encode(reg, TIME_LIMIT, ticks);
9916bf754c7SAlex Elder }
9926bf754c7SAlex Elder 
99384f9bd12SAlex Elder static void ipa_endpoint_init_aggr(struct ipa_endpoint *endpoint)
99484f9bd12SAlex Elder {
9956a244b75SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
9966bfb7538SAlex Elder 	struct ipa *ipa = endpoint->ipa;
99781772e44SAlex Elder 	const struct reg *reg;
99884f9bd12SAlex Elder 	u32 val = 0;
9996bfb7538SAlex Elder 
10006a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_AGGR);
1001660e52d6SAlex Elder 	if (endpoint->config.aggregation) {
100284f9bd12SAlex Elder 		if (!endpoint->toward_ipa) {
1003cf4e73a1SAlex Elder 			const struct ipa_endpoint_rx *rx_config;
1004c5794097SAlex Elder 			u32 buffer_size;
100584f9bd12SAlex Elder 			u32 limit;
100684f9bd12SAlex Elder 
1007660e52d6SAlex Elder 			rx_config = &endpoint->config.rx;
1008f1470fd7SAlex Elder 			val |= reg_encode(reg, AGGR_EN, IPA_ENABLE_AGGR);
1009f1470fd7SAlex Elder 			val |= reg_encode(reg, AGGR_TYPE, IPA_GENERIC);
10109e88cb5fSAlex Elder 
1011cf4e73a1SAlex Elder 			buffer_size = rx_config->buffer_size;
10123cebb7c2SAlex Elder 			limit = ipa_aggr_size_kb(buffer_size - NET_SKB_PAD,
10133cebb7c2SAlex Elder 						 rx_config->aggr_hard_limit);
1014f1470fd7SAlex Elder 			val |= reg_encode(reg, BYTE_LIMIT, limit);
10151d86652bSAlex Elder 
1016beb90cbaSAlex Elder 			limit = rx_config->aggr_time_limit;
1017216b409dSAlex Elder 			val |= aggr_time_limit_encode(ipa, reg, limit);
10181d86652bSAlex Elder 
10199e88cb5fSAlex Elder 			/* AGGR_PKT_LIMIT is 0 (unlimited) */
10209e88cb5fSAlex Elder 
1021216b409dSAlex Elder 			if (rx_config->aggr_close_eof)
1022f1470fd7SAlex Elder 				val |= reg_bit(reg, SW_EOF_ACTIVE);
102384f9bd12SAlex Elder 		} else {
1024f1470fd7SAlex Elder 			val |= reg_encode(reg, AGGR_EN, IPA_ENABLE_DEAGGR);
1025f1470fd7SAlex Elder 			val |= reg_encode(reg, AGGR_TYPE, IPA_QCMAP);
102684f9bd12SAlex Elder 			/* other fields ignored */
102784f9bd12SAlex Elder 		}
102884f9bd12SAlex Elder 		/* AGGR_FORCE_CLOSE is 0 */
10298bfc4e21SAlex Elder 		/* AGGR_GRAN_SEL is 0 for IPA v4.5 */
103084f9bd12SAlex Elder 	} else {
1031f1470fd7SAlex Elder 		val |= reg_encode(reg, AGGR_EN, IPA_BYPASS_AGGR);
103284f9bd12SAlex Elder 		/* other fields ignored */
103384f9bd12SAlex Elder 	}
103484f9bd12SAlex Elder 
1035fc4cecf7SAlex Elder 	iowrite32(val, ipa->reg_virt + reg_n_offset(reg, endpoint_id));
103684f9bd12SAlex Elder }
103784f9bd12SAlex Elder 
103863e5afc8SAlex Elder /* The head-of-line blocking timer is defined as a tick count.  For
103963e5afc8SAlex Elder  * IPA version 4.5 the tick count is based on the Qtimer, which is
104063e5afc8SAlex Elder  * derived from the 19.2 MHz SoC XO clock.  For older IPA versions
104163e5afc8SAlex Elder  * each tick represents 128 cycles of the IPA core clock.
104263e5afc8SAlex Elder  *
10438be440e1SAlex Elder  * Return the encoded value representing the timeout period provided
10448be440e1SAlex Elder  * that should be written to the ENDP_INIT_HOL_BLOCK_TIMER register.
104563e5afc8SAlex Elder  */
104681772e44SAlex Elder static u32 hol_block_timer_encode(struct ipa *ipa, const struct reg *reg,
1047216b409dSAlex Elder 				  u32 microseconds)
104884f9bd12SAlex Elder {
1049f13a8c31SAlex Elder 	u32 width;
105084f9bd12SAlex Elder 	u32 scale;
1051f13a8c31SAlex Elder 	u64 ticks;
1052f13a8c31SAlex Elder 	u64 rate;
1053f13a8c31SAlex Elder 	u32 high;
105484f9bd12SAlex Elder 	u32 val;
105584f9bd12SAlex Elder 
105684f9bd12SAlex Elder 	if (!microseconds)
1057f13a8c31SAlex Elder 		return 0;	/* Nothing to compute if timer period is 0 */
105884f9bd12SAlex Elder 
105948395fa8SAlex Elder 	if (ipa->version >= IPA_VERSION_4_5) {
1060f1470fd7SAlex Elder 		u32 max = reg_field_max(reg, TIMER_LIMIT);
10612cdbcbfdSAlex Elder 		u32 select;
10622cdbcbfdSAlex Elder 		u32 ticks;
106348395fa8SAlex Elder 
10642cdbcbfdSAlex Elder 		ticks = ipa_qtime_val(ipa, microseconds, max, &select);
106548395fa8SAlex Elder 
1066f1470fd7SAlex Elder 		return reg_encode(reg, TIMER_GRAN_SEL, 1) |
1067f1470fd7SAlex Elder 		       reg_encode(reg, TIMER_LIMIT, ticks);
106848395fa8SAlex Elder 	}
106963e5afc8SAlex Elder 
1070216b409dSAlex Elder 	/* Use 64 bit arithmetic to avoid overflow */
10717aa0e8b8SAlex Elder 	rate = ipa_core_clock_rate(ipa);
1072f13a8c31SAlex Elder 	ticks = DIV_ROUND_CLOSEST(microseconds * rate, 128 * USEC_PER_SEC);
1073216b409dSAlex Elder 
1074216b409dSAlex Elder 	/* We still need the result to fit into the field */
1075f1470fd7SAlex Elder 	WARN_ON(ticks > reg_field_max(reg, TIMER_BASE_VALUE));
107684f9bd12SAlex Elder 
10776833a096SAlex Elder 	/* IPA v3.5.1 through v4.1 just record the tick count */
10786833a096SAlex Elder 	if (ipa->version < IPA_VERSION_4_2)
1079f1470fd7SAlex Elder 		return reg_encode(reg, TIMER_BASE_VALUE, (u32)ticks);
108084f9bd12SAlex Elder 
1081f13a8c31SAlex Elder 	/* For IPA v4.2, the tick count is represented by base and
1082f13a8c31SAlex Elder 	 * scale fields within the 32-bit timer register, where:
1083f13a8c31SAlex Elder 	 *     ticks = base << scale;
1084f13a8c31SAlex Elder 	 * The best precision is achieved when the base value is as
1085f13a8c31SAlex Elder 	 * large as possible.  Find the highest set bit in the tick
1086f13a8c31SAlex Elder 	 * count, and extract the number of bits in the base field
1087497abc87SPeng Li 	 * such that high bit is included.
1088f13a8c31SAlex Elder 	 */
1089216b409dSAlex Elder 	high = fls(ticks);		/* 1..32 (or warning above) */
1090f1470fd7SAlex Elder 	width = hweight32(reg_fmask(reg, TIMER_BASE_VALUE));
1091f13a8c31SAlex Elder 	scale = high > width ? high - width : 0;
1092f13a8c31SAlex Elder 	if (scale) {
1093f13a8c31SAlex Elder 		/* If we're scaling, round up to get a closer result */
1094f13a8c31SAlex Elder 		ticks += 1 << (scale - 1);
1095f13a8c31SAlex Elder 		/* High bit was set, so rounding might have affected it */
1096f13a8c31SAlex Elder 		if (fls(ticks) != high)
1097f13a8c31SAlex Elder 			scale++;
1098f13a8c31SAlex Elder 	}
109984f9bd12SAlex Elder 
1100f1470fd7SAlex Elder 	val = reg_encode(reg, TIMER_SCALE, scale);
1101f1470fd7SAlex Elder 	val |= reg_encode(reg, TIMER_BASE_VALUE, (u32)ticks >> scale);
110284f9bd12SAlex Elder 
110384f9bd12SAlex Elder 	return val;
110484f9bd12SAlex Elder }
110584f9bd12SAlex Elder 
1106f13a8c31SAlex Elder /* If microseconds is 0, timeout is immediate */
1107f13a8c31SAlex Elder static void ipa_endpoint_init_hol_block_timer(struct ipa_endpoint *endpoint,
110884f9bd12SAlex Elder 					      u32 microseconds)
110984f9bd12SAlex Elder {
111084f9bd12SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
111184f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
111281772e44SAlex Elder 	const struct reg *reg;
111384f9bd12SAlex Elder 	u32 val;
111484f9bd12SAlex Elder 
1115816316caSAlex Elder 	/* This should only be changed when HOL_BLOCK_EN is disabled */
11166a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_HOL_BLOCK_TIMER);
1117216b409dSAlex Elder 	val = hol_block_timer_encode(ipa, reg, microseconds);
11186bfb7538SAlex Elder 
1119fc4cecf7SAlex Elder 	iowrite32(val, ipa->reg_virt + reg_n_offset(reg, endpoint_id));
112084f9bd12SAlex Elder }
112184f9bd12SAlex Elder 
112284f9bd12SAlex Elder static void
1123e6aab6b9SAlex Elder ipa_endpoint_init_hol_block_en(struct ipa_endpoint *endpoint, bool enable)
112484f9bd12SAlex Elder {
112584f9bd12SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
11266bfb7538SAlex Elder 	struct ipa *ipa = endpoint->ipa;
112781772e44SAlex Elder 	const struct reg *reg;
112884f9bd12SAlex Elder 	u32 offset;
112984f9bd12SAlex Elder 	u32 val;
113084f9bd12SAlex Elder 
11316a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_HOL_BLOCK_EN);
1132fc4cecf7SAlex Elder 	offset = reg_n_offset(reg, endpoint_id);
1133f1470fd7SAlex Elder 	val = enable ? reg_bit(reg, HOL_BLOCK_EN) : 0;
11346bfb7538SAlex Elder 
11356bfb7538SAlex Elder 	iowrite32(val, ipa->reg_virt + offset);
11366bfb7538SAlex Elder 
11376e228d8cSAlex Elder 	/* When enabling, the register must be written twice for IPA v4.5+ */
11386bfb7538SAlex Elder 	if (enable && ipa->version >= IPA_VERSION_4_5)
11396bfb7538SAlex Elder 		iowrite32(val, ipa->reg_virt + offset);
114084f9bd12SAlex Elder }
114184f9bd12SAlex Elder 
1142e6aab6b9SAlex Elder /* Assumes HOL_BLOCK is in disabled state */
1143e6aab6b9SAlex Elder static void ipa_endpoint_init_hol_block_enable(struct ipa_endpoint *endpoint,
1144e6aab6b9SAlex Elder 					       u32 microseconds)
1145e6aab6b9SAlex Elder {
1146e6aab6b9SAlex Elder 	ipa_endpoint_init_hol_block_timer(endpoint, microseconds);
1147e6aab6b9SAlex Elder 	ipa_endpoint_init_hol_block_en(endpoint, true);
1148e6aab6b9SAlex Elder }
1149e6aab6b9SAlex Elder 
1150e6aab6b9SAlex Elder static void ipa_endpoint_init_hol_block_disable(struct ipa_endpoint *endpoint)
1151e6aab6b9SAlex Elder {
1152e6aab6b9SAlex Elder 	ipa_endpoint_init_hol_block_en(endpoint, false);
1153e6aab6b9SAlex Elder }
1154e6aab6b9SAlex Elder 
115584f9bd12SAlex Elder void ipa_endpoint_modem_hol_block_clear_all(struct ipa *ipa)
115684f9bd12SAlex Elder {
1157e359ba89SAlex Elder 	u32 endpoint_id = 0;
115884f9bd12SAlex Elder 
1159b7aaff0bSAlex Elder 	while (endpoint_id < ipa->endpoint_count) {
1160e359ba89SAlex Elder 		struct ipa_endpoint *endpoint = &ipa->endpoint[endpoint_id++];
116184f9bd12SAlex Elder 
1162f8d34dfdSAlex Elder 		if (endpoint->toward_ipa || endpoint->ee_id != GSI_EE_MODEM)
116384f9bd12SAlex Elder 			continue;
116484f9bd12SAlex Elder 
1165e6aab6b9SAlex Elder 		ipa_endpoint_init_hol_block_disable(endpoint);
1166e6aab6b9SAlex Elder 		ipa_endpoint_init_hol_block_enable(endpoint, 0);
116784f9bd12SAlex Elder 	}
116884f9bd12SAlex Elder }
116984f9bd12SAlex Elder 
117084f9bd12SAlex Elder static void ipa_endpoint_init_deaggr(struct ipa_endpoint *endpoint)
117184f9bd12SAlex Elder {
11726a244b75SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
11736bfb7538SAlex Elder 	struct ipa *ipa = endpoint->ipa;
117481772e44SAlex Elder 	const struct reg *reg;
117584f9bd12SAlex Elder 	u32 val = 0;
117684f9bd12SAlex Elder 
1177fb57c3eaSAlex Elder 	if (!endpoint->toward_ipa)
1178fb57c3eaSAlex Elder 		return;		/* Register not valid for RX endpoints */
1179fb57c3eaSAlex Elder 
11806a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_DEAGGR);
118184f9bd12SAlex Elder 	/* DEAGGR_HDR_LEN is 0 */
118284f9bd12SAlex Elder 	/* PACKET_OFFSET_VALID is 0 */
118384f9bd12SAlex Elder 	/* PACKET_OFFSET_LOCATION is ignored (not valid) */
118484f9bd12SAlex Elder 	/* MAX_PACKET_LEN is 0 (not enforced) */
118584f9bd12SAlex Elder 
1186fc4cecf7SAlex Elder 	iowrite32(val, ipa->reg_virt + reg_n_offset(reg, endpoint_id));
118784f9bd12SAlex Elder }
118884f9bd12SAlex Elder 
11892d265342SAlex Elder static void ipa_endpoint_init_rsrc_grp(struct ipa_endpoint *endpoint)
11902d265342SAlex Elder {
1191181ca020SAlex Elder 	u32 resource_group = endpoint->config.resource_group;
11926a244b75SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
11932d265342SAlex Elder 	struct ipa *ipa = endpoint->ipa;
119481772e44SAlex Elder 	const struct reg *reg;
11952d265342SAlex Elder 	u32 val;
11962d265342SAlex Elder 
11976a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_RSRC_GRP);
1198f1470fd7SAlex Elder 	val = reg_encode(reg, ENDP_RSRC_GRP, resource_group);
11996bfb7538SAlex Elder 
1200fc4cecf7SAlex Elder 	iowrite32(val, ipa->reg_virt + reg_n_offset(reg, endpoint_id));
12012d265342SAlex Elder }
12022d265342SAlex Elder 
120384f9bd12SAlex Elder static void ipa_endpoint_init_seq(struct ipa_endpoint *endpoint)
120484f9bd12SAlex Elder {
12056a244b75SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
12066bfb7538SAlex Elder 	struct ipa *ipa = endpoint->ipa;
120781772e44SAlex Elder 	const struct reg *reg;
1208181ca020SAlex Elder 	u32 val;
120984f9bd12SAlex Elder 
1210fb57c3eaSAlex Elder 	if (!endpoint->toward_ipa)
1211fb57c3eaSAlex Elder 		return;		/* Register not valid for RX endpoints */
1212fb57c3eaSAlex Elder 
12136a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_SEQ);
12146bfb7538SAlex Elder 
12158ee5df65SAlex Elder 	/* Low-order byte configures primary packet processing */
1216f1470fd7SAlex Elder 	val = reg_encode(reg, SEQ_TYPE, endpoint->config.tx.seq_type);
12178ee5df65SAlex Elder 
1218a14d5937SAlex Elder 	/* Second byte (if supported) configures replicated packet processing */
12196bfb7538SAlex Elder 	if (ipa->version < IPA_VERSION_4_5)
1220f1470fd7SAlex Elder 		val |= reg_encode(reg, SEQ_REP_TYPE,
1221181ca020SAlex Elder 				  endpoint->config.tx.seq_rep_type);
122284f9bd12SAlex Elder 
1223fc4cecf7SAlex Elder 	iowrite32(val, ipa->reg_virt + reg_n_offset(reg, endpoint_id));
122484f9bd12SAlex Elder }
122584f9bd12SAlex Elder 
122684f9bd12SAlex Elder /**
122784f9bd12SAlex Elder  * ipa_endpoint_skb_tx() - Transmit a socket buffer
122884f9bd12SAlex Elder  * @endpoint:	Endpoint pointer
122984f9bd12SAlex Elder  * @skb:	Socket buffer to send
123084f9bd12SAlex Elder  *
123184f9bd12SAlex Elder  * Returns:	0 if successful, or a negative error code
123284f9bd12SAlex Elder  */
123384f9bd12SAlex Elder int ipa_endpoint_skb_tx(struct ipa_endpoint *endpoint, struct sk_buff *skb)
123484f9bd12SAlex Elder {
123584f9bd12SAlex Elder 	struct gsi_trans *trans;
123684f9bd12SAlex Elder 	u32 nr_frags;
123784f9bd12SAlex Elder 	int ret;
123884f9bd12SAlex Elder 
123984f9bd12SAlex Elder 	/* Make sure source endpoint's TLV FIFO has enough entries to
124084f9bd12SAlex Elder 	 * hold the linear portion of the skb and all its fragments.
124184f9bd12SAlex Elder 	 * If not, see if we can linearize it before giving up.
124284f9bd12SAlex Elder 	 */
124384f9bd12SAlex Elder 	nr_frags = skb_shinfo(skb)->nr_frags;
1244317595d2SAlex Elder 	if (nr_frags > endpoint->skb_frag_max) {
124584f9bd12SAlex Elder 		if (skb_linearize(skb))
124684f9bd12SAlex Elder 			return -E2BIG;
124784f9bd12SAlex Elder 		nr_frags = 0;
124884f9bd12SAlex Elder 	}
124984f9bd12SAlex Elder 
125084f9bd12SAlex Elder 	trans = ipa_endpoint_trans_alloc(endpoint, 1 + nr_frags);
125184f9bd12SAlex Elder 	if (!trans)
125284f9bd12SAlex Elder 		return -EBUSY;
125384f9bd12SAlex Elder 
125484f9bd12SAlex Elder 	ret = gsi_trans_skb_add(trans, skb);
125584f9bd12SAlex Elder 	if (ret)
125684f9bd12SAlex Elder 		goto err_trans_free;
125784f9bd12SAlex Elder 	trans->data = skb;	/* transaction owns skb now */
125884f9bd12SAlex Elder 
125984f9bd12SAlex Elder 	gsi_trans_commit(trans, !netdev_xmit_more());
126084f9bd12SAlex Elder 
126184f9bd12SAlex Elder 	return 0;
126284f9bd12SAlex Elder 
126384f9bd12SAlex Elder err_trans_free:
126484f9bd12SAlex Elder 	gsi_trans_free(trans);
126584f9bd12SAlex Elder 
126684f9bd12SAlex Elder 	return -ENOMEM;
126784f9bd12SAlex Elder }
126884f9bd12SAlex Elder 
126984f9bd12SAlex Elder static void ipa_endpoint_status(struct ipa_endpoint *endpoint)
127084f9bd12SAlex Elder {
127184f9bd12SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
127284f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
127381772e44SAlex Elder 	const struct reg *reg;
127484f9bd12SAlex Elder 	u32 val = 0;
127584f9bd12SAlex Elder 
12766a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_STATUS);
1277660e52d6SAlex Elder 	if (endpoint->config.status_enable) {
1278f1470fd7SAlex Elder 		val |= reg_bit(reg, STATUS_EN);
127984f9bd12SAlex Elder 		if (endpoint->toward_ipa) {
128084f9bd12SAlex Elder 			enum ipa_endpoint_name name;
128184f9bd12SAlex Elder 			u32 status_endpoint_id;
128284f9bd12SAlex Elder 
1283660e52d6SAlex Elder 			name = endpoint->config.tx.status_endpoint;
128484f9bd12SAlex Elder 			status_endpoint_id = ipa->name_map[name]->endpoint_id;
128584f9bd12SAlex Elder 
1286f1470fd7SAlex Elder 			val |= reg_encode(reg, STATUS_ENDP, status_endpoint_id);
128784f9bd12SAlex Elder 		}
128802c50774SAlex Elder 		/* STATUS_LOCATION is 0, meaning IPA packet status
128902c50774SAlex Elder 		 * precedes the packet (not present for IPA v4.5+)
12908bfc4e21SAlex Elder 		 */
1291181ca020SAlex Elder 		/* STATUS_PKT_SUPPRESS_FMASK is 0 (not present for v4.0+) */
129284f9bd12SAlex Elder 	}
129384f9bd12SAlex Elder 
1294fc4cecf7SAlex Elder 	iowrite32(val, ipa->reg_virt + reg_n_offset(reg, endpoint_id));
129584f9bd12SAlex Elder }
129684f9bd12SAlex Elder 
12976a606b90SAlex Elder static int ipa_endpoint_replenish_one(struct ipa_endpoint *endpoint,
12986a606b90SAlex Elder 				      struct gsi_trans *trans)
129984f9bd12SAlex Elder {
130084f9bd12SAlex Elder 	struct page *page;
1301ed23f026SAlex Elder 	u32 buffer_size;
130284f9bd12SAlex Elder 	u32 offset;
130384f9bd12SAlex Elder 	u32 len;
130484f9bd12SAlex Elder 	int ret;
130584f9bd12SAlex Elder 
1306660e52d6SAlex Elder 	buffer_size = endpoint->config.rx.buffer_size;
1307ed23f026SAlex Elder 	page = dev_alloc_pages(get_order(buffer_size));
130884f9bd12SAlex Elder 	if (!page)
13096a606b90SAlex Elder 		return -ENOMEM;
131084f9bd12SAlex Elder 
131184f9bd12SAlex Elder 	/* Offset the buffer to make space for skb headroom */
131284f9bd12SAlex Elder 	offset = NET_SKB_PAD;
1313ed23f026SAlex Elder 	len = buffer_size - offset;
131484f9bd12SAlex Elder 
131584f9bd12SAlex Elder 	ret = gsi_trans_page_add(trans, page, len, offset);
131684f9bd12SAlex Elder 	if (ret)
131770132763SAlex Elder 		put_page(page);
13186a606b90SAlex Elder 	else
131984f9bd12SAlex Elder 		trans->data = page;	/* transaction owns page now */
132084f9bd12SAlex Elder 
13216a606b90SAlex Elder 	return ret;
132284f9bd12SAlex Elder }
132384f9bd12SAlex Elder 
132484f9bd12SAlex Elder /**
13259af5ccf3SAlex Elder  * ipa_endpoint_replenish() - Replenish endpoint receive buffers
1326e3eea08eSAlex Elder  * @endpoint:	Endpoint to be replenished
132784f9bd12SAlex Elder  *
13289af5ccf3SAlex Elder  * The IPA hardware can hold a fixed number of receive buffers for an RX
13299af5ccf3SAlex Elder  * endpoint, based on the number of entries in the underlying channel ring
13309af5ccf3SAlex Elder  * buffer.  If an endpoint's "backlog" is non-zero, it indicates how many
13319af5ccf3SAlex Elder  * more receive buffers can be supplied to the hardware.  Replenishing for
1332a9bec7aeSAlex Elder  * an endpoint can be disabled, in which case buffers are not queued to
1333a9bec7aeSAlex Elder  * the hardware.
133484f9bd12SAlex Elder  */
13354b22d841SAlex Elder static void ipa_endpoint_replenish(struct ipa_endpoint *endpoint)
133684f9bd12SAlex Elder {
13376a606b90SAlex Elder 	struct gsi_trans *trans;
133884f9bd12SAlex Elder 
13394b22d841SAlex Elder 	if (!test_bit(IPA_REPLENISH_ENABLED, endpoint->replenish_flags))
134084f9bd12SAlex Elder 		return;
134184f9bd12SAlex Elder 
13424b22d841SAlex Elder 	/* Skip it if it's already active */
13434b22d841SAlex Elder 	if (test_and_set_bit(IPA_REPLENISH_ACTIVE, endpoint->replenish_flags))
1344998c0bd2SAlex Elder 		return;
1345998c0bd2SAlex Elder 
1346d0ac30e7SAlex Elder 	while ((trans = ipa_endpoint_trans_alloc(endpoint, 1))) {
13479654d8c4SAlex Elder 		bool doorbell;
13489654d8c4SAlex Elder 
13496a606b90SAlex Elder 		if (ipa_endpoint_replenish_one(endpoint, trans))
13506a606b90SAlex Elder 			goto try_again_later;
1351b9dbabc5SAlex Elder 
1352b9dbabc5SAlex Elder 
1353b9dbabc5SAlex Elder 		/* Ring the doorbell if we've got a full batch */
13549654d8c4SAlex Elder 		doorbell = !(++endpoint->replenish_count % IPA_REPLENISH_BATCH);
13559654d8c4SAlex Elder 		gsi_trans_commit(trans, doorbell);
1356b9dbabc5SAlex Elder 	}
1357998c0bd2SAlex Elder 
1358998c0bd2SAlex Elder 	clear_bit(IPA_REPLENISH_ACTIVE, endpoint->replenish_flags);
1359998c0bd2SAlex Elder 
136084f9bd12SAlex Elder 	return;
136184f9bd12SAlex Elder 
136284f9bd12SAlex Elder try_again_later:
13636a606b90SAlex Elder 	gsi_trans_free(trans);
1364998c0bd2SAlex Elder 	clear_bit(IPA_REPLENISH_ACTIVE, endpoint->replenish_flags);
1365998c0bd2SAlex Elder 
136684f9bd12SAlex Elder 	/* Whenever a receive buffer transaction completes we'll try to
136784f9bd12SAlex Elder 	 * replenish again.  It's unlikely, but if we fail to supply even
136884f9bd12SAlex Elder 	 * one buffer, nothing will trigger another replenish attempt.
13695fc7f9baSAlex Elder 	 * If the hardware has no receive buffers queued, schedule work to
13705fc7f9baSAlex Elder 	 * try replenishing again.
137184f9bd12SAlex Elder 	 */
13725fc7f9baSAlex Elder 	if (gsi_channel_trans_idle(&endpoint->ipa->gsi, endpoint->channel_id))
137384f9bd12SAlex Elder 		schedule_delayed_work(&endpoint->replenish_work,
137484f9bd12SAlex Elder 				      msecs_to_jiffies(1));
137584f9bd12SAlex Elder }
137684f9bd12SAlex Elder 
137784f9bd12SAlex Elder static void ipa_endpoint_replenish_enable(struct ipa_endpoint *endpoint)
137884f9bd12SAlex Elder {
1379c1aaa01dSAlex Elder 	set_bit(IPA_REPLENISH_ENABLED, endpoint->replenish_flags);
138084f9bd12SAlex Elder 
138184f9bd12SAlex Elder 	/* Start replenishing if hardware currently has no buffers */
13825fc7f9baSAlex Elder 	if (gsi_channel_trans_idle(&endpoint->ipa->gsi, endpoint->channel_id))
13834b22d841SAlex Elder 		ipa_endpoint_replenish(endpoint);
138484f9bd12SAlex Elder }
138584f9bd12SAlex Elder 
138684f9bd12SAlex Elder static void ipa_endpoint_replenish_disable(struct ipa_endpoint *endpoint)
138784f9bd12SAlex Elder {
1388c1aaa01dSAlex Elder 	clear_bit(IPA_REPLENISH_ENABLED, endpoint->replenish_flags);
138984f9bd12SAlex Elder }
139084f9bd12SAlex Elder 
139184f9bd12SAlex Elder static void ipa_endpoint_replenish_work(struct work_struct *work)
139284f9bd12SAlex Elder {
139384f9bd12SAlex Elder 	struct delayed_work *dwork = to_delayed_work(work);
139484f9bd12SAlex Elder 	struct ipa_endpoint *endpoint;
139584f9bd12SAlex Elder 
139684f9bd12SAlex Elder 	endpoint = container_of(dwork, struct ipa_endpoint, replenish_work);
139784f9bd12SAlex Elder 
13984b22d841SAlex Elder 	ipa_endpoint_replenish(endpoint);
139984f9bd12SAlex Elder }
140084f9bd12SAlex Elder 
140184f9bd12SAlex Elder static void ipa_endpoint_skb_copy(struct ipa_endpoint *endpoint,
140284f9bd12SAlex Elder 				  void *data, u32 len, u32 extra)
140384f9bd12SAlex Elder {
140484f9bd12SAlex Elder 	struct sk_buff *skb;
140584f9bd12SAlex Elder 
14061b65bbccSAlex Elder 	if (!endpoint->netdev)
14071b65bbccSAlex Elder 		return;
14081b65bbccSAlex Elder 
140984f9bd12SAlex Elder 	skb = __dev_alloc_skb(len, GFP_ATOMIC);
141030b338ffSAlex Elder 	if (skb) {
14111b65bbccSAlex Elder 		/* Copy the data into the socket buffer and receive it */
141284f9bd12SAlex Elder 		skb_put(skb, len);
141384f9bd12SAlex Elder 		memcpy(skb->data, data, len);
141484f9bd12SAlex Elder 		skb->truesize += extra;
141530b338ffSAlex Elder 	}
141684f9bd12SAlex Elder 
141784f9bd12SAlex Elder 	ipa_modem_skb_rx(endpoint->netdev, skb);
141884f9bd12SAlex Elder }
141984f9bd12SAlex Elder 
142084f9bd12SAlex Elder static bool ipa_endpoint_skb_build(struct ipa_endpoint *endpoint,
142184f9bd12SAlex Elder 				   struct page *page, u32 len)
142284f9bd12SAlex Elder {
1423660e52d6SAlex Elder 	u32 buffer_size = endpoint->config.rx.buffer_size;
142484f9bd12SAlex Elder 	struct sk_buff *skb;
142584f9bd12SAlex Elder 
142684f9bd12SAlex Elder 	/* Nothing to do if there's no netdev */
142784f9bd12SAlex Elder 	if (!endpoint->netdev)
142884f9bd12SAlex Elder 		return false;
142984f9bd12SAlex Elder 
1430ed23f026SAlex Elder 	WARN_ON(len > SKB_WITH_OVERHEAD(buffer_size - NET_SKB_PAD));
14315bc55884SAlex Elder 
1432ed23f026SAlex Elder 	skb = build_skb(page_address(page), buffer_size);
143384f9bd12SAlex Elder 	if (skb) {
143484f9bd12SAlex Elder 		/* Reserve the headroom and account for the data */
143584f9bd12SAlex Elder 		skb_reserve(skb, NET_SKB_PAD);
143684f9bd12SAlex Elder 		skb_put(skb, len);
143784f9bd12SAlex Elder 	}
143884f9bd12SAlex Elder 
143984f9bd12SAlex Elder 	/* Receive the buffer (or record drop if unable to build it) */
144084f9bd12SAlex Elder 	ipa_modem_skb_rx(endpoint->netdev, skb);
144184f9bd12SAlex Elder 
144284f9bd12SAlex Elder 	return skb != NULL;
144384f9bd12SAlex Elder }
144484f9bd12SAlex Elder 
144502c50774SAlex Elder  /* The format of an IPA packet status structure is the same for several
144602c50774SAlex Elder   * status types (opcodes).  Other types aren't currently supported.
144784f9bd12SAlex Elder  */
144884f9bd12SAlex Elder static bool ipa_status_format_packet(enum ipa_status_opcode opcode)
144984f9bd12SAlex Elder {
145084f9bd12SAlex Elder 	switch (opcode) {
145184f9bd12SAlex Elder 	case IPA_STATUS_OPCODE_PACKET:
145284f9bd12SAlex Elder 	case IPA_STATUS_OPCODE_DROPPED_PACKET:
145384f9bd12SAlex Elder 	case IPA_STATUS_OPCODE_SUSPENDED_PACKET:
145484f9bd12SAlex Elder 	case IPA_STATUS_OPCODE_PACKET_2ND_PASS:
145584f9bd12SAlex Elder 		return true;
145684f9bd12SAlex Elder 	default:
145784f9bd12SAlex Elder 		return false;
145884f9bd12SAlex Elder 	}
145984f9bd12SAlex Elder }
146084f9bd12SAlex Elder 
1461ebd2a82eSAlex Elder static bool
1462ebd2a82eSAlex Elder ipa_endpoint_status_skip(struct ipa_endpoint *endpoint, const void *data)
146384f9bd12SAlex Elder {
146455c6eae7SAlex Elder 	struct ipa *ipa = endpoint->ipa;
146502c50774SAlex Elder 	enum ipa_status_opcode opcode;
146684f9bd12SAlex Elder 	u32 endpoint_id;
146784f9bd12SAlex Elder 
146855c6eae7SAlex Elder 	opcode = ipa_status_extract(ipa, data, STATUS_OPCODE);
146902c50774SAlex Elder 	if (!ipa_status_format_packet(opcode))
147084f9bd12SAlex Elder 		return true;
147163a560b5SAlex Elder 
147255c6eae7SAlex Elder 	endpoint_id = ipa_status_extract(ipa, data, STATUS_DST_ENDPOINT);
147384f9bd12SAlex Elder 	if (endpoint_id != endpoint->endpoint_id)
147484f9bd12SAlex Elder 		return true;
147584f9bd12SAlex Elder 
147684f9bd12SAlex Elder 	return false;	/* Don't skip this packet, process it */
147784f9bd12SAlex Elder }
147884f9bd12SAlex Elder 
1479ebd2a82eSAlex Elder static bool
1480ebd2a82eSAlex Elder ipa_endpoint_status_tag_valid(struct ipa_endpoint *endpoint, const void *data)
1481f6aba7b5SAlex Elder {
148251c48ce2SAlex Elder 	struct ipa_endpoint *command_endpoint;
148302c50774SAlex Elder 	enum ipa_status_mask status_mask;
148451c48ce2SAlex Elder 	struct ipa *ipa = endpoint->ipa;
148551c48ce2SAlex Elder 	u32 endpoint_id;
148651c48ce2SAlex Elder 
148755c6eae7SAlex Elder 	status_mask = ipa_status_extract(ipa, data, STATUS_MASK);
148802c50774SAlex Elder 	if (!status_mask)
148951c48ce2SAlex Elder 		return false;	/* No valid tag */
149051c48ce2SAlex Elder 
149151c48ce2SAlex Elder 	/* The status contains a valid tag.  We know the packet was sent to
149251c48ce2SAlex Elder 	 * this endpoint (already verified by ipa_endpoint_status_skip()).
149351c48ce2SAlex Elder 	 * If the packet came from the AP->command TX endpoint we know
149451c48ce2SAlex Elder 	 * this packet was sent as part of the pipeline clear process.
149551c48ce2SAlex Elder 	 */
149655c6eae7SAlex Elder 	endpoint_id = ipa_status_extract(ipa, data, STATUS_SRC_ENDPOINT);
149751c48ce2SAlex Elder 	command_endpoint = ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX];
149851c48ce2SAlex Elder 	if (endpoint_id == command_endpoint->endpoint_id) {
149951c48ce2SAlex Elder 		complete(&ipa->completion);
150051c48ce2SAlex Elder 	} else {
1501*5245f4fdSAlex Elder 		dev_err(ipa->dev, "unexpected tagged packet from endpoint %u\n",
150251c48ce2SAlex Elder 			endpoint_id);
150351c48ce2SAlex Elder 	}
150451c48ce2SAlex Elder 
150551c48ce2SAlex Elder 	return true;
1506f6aba7b5SAlex Elder }
1507f6aba7b5SAlex Elder 
150884f9bd12SAlex Elder /* Return whether the status indicates the packet should be dropped */
1509ebd2a82eSAlex Elder static bool
1510ebd2a82eSAlex Elder ipa_endpoint_status_drop(struct ipa_endpoint *endpoint, const void *data)
151184f9bd12SAlex Elder {
151202c50774SAlex Elder 	enum ipa_status_exception exception;
151355c6eae7SAlex Elder 	struct ipa *ipa = endpoint->ipa;
151402c50774SAlex Elder 	u32 rule;
151584f9bd12SAlex Elder 
1516f6aba7b5SAlex Elder 	/* If the status indicates a tagged transfer, we'll drop the packet */
1517ebd2a82eSAlex Elder 	if (ipa_endpoint_status_tag_valid(endpoint, data))
1518f6aba7b5SAlex Elder 		return true;
1519f6aba7b5SAlex Elder 
1520ab4f71e5SAlex Elder 	/* Deaggregation exceptions we drop; all other types we consume */
152155c6eae7SAlex Elder 	exception = ipa_status_extract(ipa, data, STATUS_EXCEPTION);
152202c50774SAlex Elder 	if (exception)
152302c50774SAlex Elder 		return exception == IPA_STATUS_EXCEPTION_DEAGGR;
152484f9bd12SAlex Elder 
152584f9bd12SAlex Elder 	/* Drop the packet if it fails to match a routing rule; otherwise no */
152655c6eae7SAlex Elder 	rule = ipa_status_extract(ipa, data, STATUS_ROUTER_RULE_INDEX);
152784f9bd12SAlex Elder 
1528ebd2a82eSAlex Elder 	return rule == IPA_STATUS_RULE_MISS;
152984f9bd12SAlex Elder }
153084f9bd12SAlex Elder 
153184f9bd12SAlex Elder static void ipa_endpoint_status_parse(struct ipa_endpoint *endpoint,
153284f9bd12SAlex Elder 				      struct page *page, u32 total_len)
153384f9bd12SAlex Elder {
1534660e52d6SAlex Elder 	u32 buffer_size = endpoint->config.rx.buffer_size;
153584f9bd12SAlex Elder 	void *data = page_address(page) + NET_SKB_PAD;
1536ed23f026SAlex Elder 	u32 unused = buffer_size - total_len;
153755c6eae7SAlex Elder 	struct ipa *ipa = endpoint->ipa;
1538*5245f4fdSAlex Elder 	struct device *dev = ipa->dev;
153984f9bd12SAlex Elder 	u32 resid = total_len;
154084f9bd12SAlex Elder 
154184f9bd12SAlex Elder 	while (resid) {
154263a560b5SAlex Elder 		u32 length;
154384f9bd12SAlex Elder 		u32 align;
154484f9bd12SAlex Elder 		u32 len;
154584f9bd12SAlex Elder 
1546b8dc7d0eSAlex Elder 		if (resid < IPA_STATUS_SIZE) {
1547*5245f4fdSAlex Elder 			dev_err(dev,
154884f9bd12SAlex Elder 				"short message (%u bytes < %zu byte status)\n",
1549b8dc7d0eSAlex Elder 				resid, IPA_STATUS_SIZE);
155084f9bd12SAlex Elder 			break;
155184f9bd12SAlex Elder 		}
155284f9bd12SAlex Elder 
155384f9bd12SAlex Elder 		/* Skip over status packets that lack packet data */
155455c6eae7SAlex Elder 		length = ipa_status_extract(ipa, data, STATUS_LENGTH);
155502c50774SAlex Elder 		if (!length || ipa_endpoint_status_skip(endpoint, data)) {
1556b8dc7d0eSAlex Elder 			data += IPA_STATUS_SIZE;
1557b8dc7d0eSAlex Elder 			resid -= IPA_STATUS_SIZE;
155884f9bd12SAlex Elder 			continue;
155984f9bd12SAlex Elder 		}
156084f9bd12SAlex Elder 
1561162fbc6fSAlex Elder 		/* Compute the amount of buffer space consumed by the packet,
156202c50774SAlex Elder 		 * including the status.  If the hardware is configured to
156302c50774SAlex Elder 		 * pad packet data to an aligned boundary, account for that.
1564162fbc6fSAlex Elder 		 * And if checksum offload is enabled a trailer containing
1565162fbc6fSAlex Elder 		 * computed checksum information will be appended.
156684f9bd12SAlex Elder 		 */
1567660e52d6SAlex Elder 		align = endpoint->config.rx.pad_align ? : 1;
1568b8dc7d0eSAlex Elder 		len = IPA_STATUS_SIZE + ALIGN(length, align);
1569660e52d6SAlex Elder 		if (endpoint->config.checksum)
157084f9bd12SAlex Elder 			len += sizeof(struct rmnet_map_dl_csum_trailer);
157184f9bd12SAlex Elder 
157202c50774SAlex Elder 		if (!ipa_endpoint_status_drop(endpoint, data)) {
1573162fbc6fSAlex Elder 			void *data2;
1574162fbc6fSAlex Elder 			u32 extra;
157584f9bd12SAlex Elder 
157684f9bd12SAlex Elder 			/* Client receives only packet data (no status) */
1577b8dc7d0eSAlex Elder 			data2 = data + IPA_STATUS_SIZE;
1578162fbc6fSAlex Elder 
1579162fbc6fSAlex Elder 			/* Have the true size reflect the extra unused space in
1580162fbc6fSAlex Elder 			 * the original receive buffer.  Distribute the "cost"
1581162fbc6fSAlex Elder 			 * proportionately across all aggregated packets in the
1582162fbc6fSAlex Elder 			 * buffer.
1583162fbc6fSAlex Elder 			 */
1584162fbc6fSAlex Elder 			extra = DIV_ROUND_CLOSEST(unused * len, total_len);
158563a560b5SAlex Elder 			ipa_endpoint_skb_copy(endpoint, data2, length, extra);
158684f9bd12SAlex Elder 		}
158784f9bd12SAlex Elder 
158884f9bd12SAlex Elder 		/* Consume status and the full packet it describes */
158984f9bd12SAlex Elder 		data += len;
159084f9bd12SAlex Elder 		resid -= len;
159184f9bd12SAlex Elder 	}
159284f9bd12SAlex Elder }
159384f9bd12SAlex Elder 
1594983a1a30SAlex Elder void ipa_endpoint_trans_complete(struct ipa_endpoint *endpoint,
159584f9bd12SAlex Elder 				 struct gsi_trans *trans)
159684f9bd12SAlex Elder {
159784f9bd12SAlex Elder 	struct page *page;
159884f9bd12SAlex Elder 
1599983a1a30SAlex Elder 	if (endpoint->toward_ipa)
1600983a1a30SAlex Elder 		return;
1601983a1a30SAlex Elder 
160284f9bd12SAlex Elder 	if (trans->cancelled)
16035d6ac24fSAlex Elder 		goto done;
160484f9bd12SAlex Elder 
160584f9bd12SAlex Elder 	/* Parse or build a socket buffer using the actual received length */
160684f9bd12SAlex Elder 	page = trans->data;
1607660e52d6SAlex Elder 	if (endpoint->config.status_enable)
160884f9bd12SAlex Elder 		ipa_endpoint_status_parse(endpoint, page, trans->len);
160984f9bd12SAlex Elder 	else if (ipa_endpoint_skb_build(endpoint, page, trans->len))
161084f9bd12SAlex Elder 		trans->data = NULL;	/* Pages have been consumed */
16115d6ac24fSAlex Elder done:
16125d6ac24fSAlex Elder 	ipa_endpoint_replenish(endpoint);
161384f9bd12SAlex Elder }
161484f9bd12SAlex Elder 
161584f9bd12SAlex Elder void ipa_endpoint_trans_release(struct ipa_endpoint *endpoint,
161684f9bd12SAlex Elder 				struct gsi_trans *trans)
161784f9bd12SAlex Elder {
161884f9bd12SAlex Elder 	if (endpoint->toward_ipa) {
161984f9bd12SAlex Elder 		struct ipa *ipa = endpoint->ipa;
162084f9bd12SAlex Elder 
162184f9bd12SAlex Elder 		/* Nothing to do for command transactions */
162284f9bd12SAlex Elder 		if (endpoint != ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]) {
162384f9bd12SAlex Elder 			struct sk_buff *skb = trans->data;
162484f9bd12SAlex Elder 
162584f9bd12SAlex Elder 			if (skb)
162684f9bd12SAlex Elder 				dev_kfree_skb_any(skb);
162784f9bd12SAlex Elder 		}
162884f9bd12SAlex Elder 	} else {
162984f9bd12SAlex Elder 		struct page *page = trans->data;
163084f9bd12SAlex Elder 
1631155c0c90SAlex Elder 		if (page)
1632155c0c90SAlex Elder 			put_page(page);
163384f9bd12SAlex Elder 	}
163484f9bd12SAlex Elder }
163584f9bd12SAlex Elder 
163684f9bd12SAlex Elder void ipa_endpoint_default_route_set(struct ipa *ipa, u32 endpoint_id)
163784f9bd12SAlex Elder {
163881772e44SAlex Elder 	const struct reg *reg;
163984f9bd12SAlex Elder 	u32 val;
164084f9bd12SAlex Elder 
16416a244b75SAlex Elder 	reg = ipa_reg(ipa, ROUTE);
164284f9bd12SAlex Elder 	/* ROUTE_DIS is 0 */
1643f1470fd7SAlex Elder 	val = reg_encode(reg, ROUTE_DEF_PIPE, endpoint_id);
1644f1470fd7SAlex Elder 	val |= reg_bit(reg, ROUTE_DEF_HDR_TABLE);
1645479deb32SAlex Elder 	/* ROUTE_DEF_HDR_OFST is 0 */
1646f1470fd7SAlex Elder 	val |= reg_encode(reg, ROUTE_FRAG_DEF_PIPE, endpoint_id);
1647f1470fd7SAlex Elder 	val |= reg_bit(reg, ROUTE_DEF_RETAIN_HDR);
164884f9bd12SAlex Elder 
1649fc4cecf7SAlex Elder 	iowrite32(val, ipa->reg_virt + reg_offset(reg));
165084f9bd12SAlex Elder }
165184f9bd12SAlex Elder 
165284f9bd12SAlex Elder void ipa_endpoint_default_route_clear(struct ipa *ipa)
165384f9bd12SAlex Elder {
165484f9bd12SAlex Elder 	ipa_endpoint_default_route_set(ipa, 0);
165584f9bd12SAlex Elder }
165684f9bd12SAlex Elder 
165784f9bd12SAlex Elder /**
165884f9bd12SAlex Elder  * ipa_endpoint_reset_rx_aggr() - Reset RX endpoint with aggregation active
165984f9bd12SAlex Elder  * @endpoint:	Endpoint to be reset
166084f9bd12SAlex Elder  *
166184f9bd12SAlex Elder  * If aggregation is active on an RX endpoint when a reset is performed
166284f9bd12SAlex Elder  * on its underlying GSI channel, a special sequence of actions must be
166384f9bd12SAlex Elder  * taken to ensure the IPA pipeline is properly cleared.
166484f9bd12SAlex Elder  *
1665e3eea08eSAlex Elder  * Return:	0 if successful, or a negative error code
166684f9bd12SAlex Elder  */
166784f9bd12SAlex Elder static int ipa_endpoint_reset_rx_aggr(struct ipa_endpoint *endpoint)
166884f9bd12SAlex Elder {
166984f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
1670*5245f4fdSAlex Elder 	struct device *dev = ipa->dev;
167184f9bd12SAlex Elder 	struct gsi *gsi = &ipa->gsi;
16724fa95248SAlex Elder 	bool suspended = false;
167384f9bd12SAlex Elder 	dma_addr_t addr;
167484f9bd12SAlex Elder 	u32 retries;
167584f9bd12SAlex Elder 	u32 len = 1;
167684f9bd12SAlex Elder 	void *virt;
167784f9bd12SAlex Elder 	int ret;
167884f9bd12SAlex Elder 
167984f9bd12SAlex Elder 	virt = kzalloc(len, GFP_KERNEL);
168084f9bd12SAlex Elder 	if (!virt)
168184f9bd12SAlex Elder 		return -ENOMEM;
168284f9bd12SAlex Elder 
168384f9bd12SAlex Elder 	addr = dma_map_single(dev, virt, len, DMA_FROM_DEVICE);
168484f9bd12SAlex Elder 	if (dma_mapping_error(dev, addr)) {
168584f9bd12SAlex Elder 		ret = -ENOMEM;
168684f9bd12SAlex Elder 		goto out_kfree;
168784f9bd12SAlex Elder 	}
168884f9bd12SAlex Elder 
168984f9bd12SAlex Elder 	/* Force close aggregation before issuing the reset */
169084f9bd12SAlex Elder 	ipa_endpoint_force_close(endpoint);
169184f9bd12SAlex Elder 
169284f9bd12SAlex Elder 	/* Reset and reconfigure the channel with the doorbell engine
169384f9bd12SAlex Elder 	 * disabled.  Then poll until we know aggregation is no longer
169484f9bd12SAlex Elder 	 * active.  We'll re-enable the doorbell (if appropriate) when
169584f9bd12SAlex Elder 	 * we reset again below.
169684f9bd12SAlex Elder 	 */
169784f9bd12SAlex Elder 	gsi_channel_reset(gsi, endpoint->channel_id, false);
169884f9bd12SAlex Elder 
169984f9bd12SAlex Elder 	/* Make sure the channel isn't suspended */
17004fa95248SAlex Elder 	suspended = ipa_endpoint_program_suspend(endpoint, false);
170184f9bd12SAlex Elder 
170284f9bd12SAlex Elder 	/* Start channel and do a 1 byte read */
170384f9bd12SAlex Elder 	ret = gsi_channel_start(gsi, endpoint->channel_id);
170484f9bd12SAlex Elder 	if (ret)
170584f9bd12SAlex Elder 		goto out_suspend_again;
170684f9bd12SAlex Elder 
170784f9bd12SAlex Elder 	ret = gsi_trans_read_byte(gsi, endpoint->channel_id, addr);
170884f9bd12SAlex Elder 	if (ret)
170984f9bd12SAlex Elder 		goto err_endpoint_stop;
171084f9bd12SAlex Elder 
171184f9bd12SAlex Elder 	/* Wait for aggregation to be closed on the channel */
171284f9bd12SAlex Elder 	retries = IPA_ENDPOINT_RESET_AGGR_RETRY_MAX;
171384f9bd12SAlex Elder 	do {
171484f9bd12SAlex Elder 		if (!ipa_endpoint_aggr_active(endpoint))
171584f9bd12SAlex Elder 			break;
171674401946SAlex Elder 		usleep_range(USEC_PER_MSEC, 2 * USEC_PER_MSEC);
171784f9bd12SAlex Elder 	} while (retries--);
171884f9bd12SAlex Elder 
171984f9bd12SAlex Elder 	/* Check one last time */
172084f9bd12SAlex Elder 	if (ipa_endpoint_aggr_active(endpoint))
172184f9bd12SAlex Elder 		dev_err(dev, "endpoint %u still active during reset\n",
172284f9bd12SAlex Elder 			endpoint->endpoint_id);
172384f9bd12SAlex Elder 
172484f9bd12SAlex Elder 	gsi_trans_read_byte_done(gsi, endpoint->channel_id);
172584f9bd12SAlex Elder 
1726f30dcb7dSAlex Elder 	ret = gsi_channel_stop(gsi, endpoint->channel_id);
172784f9bd12SAlex Elder 	if (ret)
172884f9bd12SAlex Elder 		goto out_suspend_again;
172984f9bd12SAlex Elder 
1730497abc87SPeng Li 	/* Finally, reset and reconfigure the channel again (re-enabling
173184f9bd12SAlex Elder 	 * the doorbell engine if appropriate).  Sleep for 1 millisecond to
173284f9bd12SAlex Elder 	 * complete the channel reset sequence.  Finish by suspending the
173384f9bd12SAlex Elder 	 * channel again (if necessary).
173484f9bd12SAlex Elder 	 */
1735ce54993dSAlex Elder 	gsi_channel_reset(gsi, endpoint->channel_id, true);
173684f9bd12SAlex Elder 
173774401946SAlex Elder 	usleep_range(USEC_PER_MSEC, 2 * USEC_PER_MSEC);
173884f9bd12SAlex Elder 
173984f9bd12SAlex Elder 	goto out_suspend_again;
174084f9bd12SAlex Elder 
174184f9bd12SAlex Elder err_endpoint_stop:
1742f30dcb7dSAlex Elder 	(void)gsi_channel_stop(gsi, endpoint->channel_id);
174384f9bd12SAlex Elder out_suspend_again:
17444fa95248SAlex Elder 	if (suspended)
17454fa95248SAlex Elder 		(void)ipa_endpoint_program_suspend(endpoint, true);
174684f9bd12SAlex Elder 	dma_unmap_single(dev, addr, len, DMA_FROM_DEVICE);
174784f9bd12SAlex Elder out_kfree:
174884f9bd12SAlex Elder 	kfree(virt);
174984f9bd12SAlex Elder 
175084f9bd12SAlex Elder 	return ret;
175184f9bd12SAlex Elder }
175284f9bd12SAlex Elder 
175384f9bd12SAlex Elder static void ipa_endpoint_reset(struct ipa_endpoint *endpoint)
175484f9bd12SAlex Elder {
175584f9bd12SAlex Elder 	u32 channel_id = endpoint->channel_id;
175684f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
175784f9bd12SAlex Elder 	bool special;
175884f9bd12SAlex Elder 	int ret = 0;
175984f9bd12SAlex Elder 
176084f9bd12SAlex Elder 	/* On IPA v3.5.1, if an RX endpoint is reset while aggregation
176184f9bd12SAlex Elder 	 * is active, we need to handle things specially to recover.
176284f9bd12SAlex Elder 	 * All other cases just need to reset the underlying GSI channel.
176384f9bd12SAlex Elder 	 */
1764d7f3087bSAlex Elder 	special = ipa->version < IPA_VERSION_4_0 && !endpoint->toward_ipa &&
1765660e52d6SAlex Elder 			endpoint->config.aggregation;
1766ce54993dSAlex Elder 	if (special && ipa_endpoint_aggr_active(endpoint))
176784f9bd12SAlex Elder 		ret = ipa_endpoint_reset_rx_aggr(endpoint);
176884f9bd12SAlex Elder 	else
1769ce54993dSAlex Elder 		gsi_channel_reset(&ipa->gsi, channel_id, true);
177084f9bd12SAlex Elder 
177184f9bd12SAlex Elder 	if (ret)
1772*5245f4fdSAlex Elder 		dev_err(ipa->dev,
177384f9bd12SAlex Elder 			"error %d resetting channel %u for endpoint %u\n",
177484f9bd12SAlex Elder 			ret, endpoint->channel_id, endpoint->endpoint_id);
177584f9bd12SAlex Elder }
177684f9bd12SAlex Elder 
177784f9bd12SAlex Elder static void ipa_endpoint_program(struct ipa_endpoint *endpoint)
177884f9bd12SAlex Elder {
17794c9d631aSAlex Elder 	if (endpoint->toward_ipa) {
17804c9d631aSAlex Elder 		/* Newer versions of IPA use GSI channel flow control
17814c9d631aSAlex Elder 		 * instead of endpoint DELAY mode to prevent sending data.
17824c9d631aSAlex Elder 		 * Flow control is disabled for newly-allocated channels,
17834c9d631aSAlex Elder 		 * and we can assume flow control is not (ever) enabled
17844c9d631aSAlex Elder 		 * for AP TX channels.
17854c9d631aSAlex Elder 		 */
17864c9d631aSAlex Elder 		if (endpoint->ipa->version < IPA_VERSION_4_2)
1787a4dcad34SAlex Elder 			ipa_endpoint_program_delay(endpoint, false);
17884c9d631aSAlex Elder 	} else {
17894c9d631aSAlex Elder 		/* Ensure suspend mode is off on all AP RX endpoints */
1790fb57c3eaSAlex Elder 		(void)ipa_endpoint_program_suspend(endpoint, false);
17914c9d631aSAlex Elder 	}
1792fb57c3eaSAlex Elder 	ipa_endpoint_init_cfg(endpoint);
1793647a05f3SAlex Elder 	ipa_endpoint_init_nat(endpoint);
1794fb57c3eaSAlex Elder 	ipa_endpoint_init_hdr(endpoint);
179584f9bd12SAlex Elder 	ipa_endpoint_init_hdr_ext(endpoint);
1796fb57c3eaSAlex Elder 	ipa_endpoint_init_hdr_metadata_mask(endpoint);
1797fb57c3eaSAlex Elder 	ipa_endpoint_init_mode(endpoint);
179884f9bd12SAlex Elder 	ipa_endpoint_init_aggr(endpoint);
1799153213f0SAlex Elder 	if (!endpoint->toward_ipa) {
1800153213f0SAlex Elder 		if (endpoint->config.rx.holb_drop)
1801153213f0SAlex Elder 			ipa_endpoint_init_hol_block_enable(endpoint, 0);
1802153213f0SAlex Elder 		else
180301c36637SAlex Elder 			ipa_endpoint_init_hol_block_disable(endpoint);
1804153213f0SAlex Elder 	}
180584f9bd12SAlex Elder 	ipa_endpoint_init_deaggr(endpoint);
18062d265342SAlex Elder 	ipa_endpoint_init_rsrc_grp(endpoint);
180784f9bd12SAlex Elder 	ipa_endpoint_init_seq(endpoint);
180884f9bd12SAlex Elder 	ipa_endpoint_status(endpoint);
180984f9bd12SAlex Elder }
181084f9bd12SAlex Elder 
181184f9bd12SAlex Elder int ipa_endpoint_enable_one(struct ipa_endpoint *endpoint)
181284f9bd12SAlex Elder {
18139b7a0065SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
181484f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
181584f9bd12SAlex Elder 	struct gsi *gsi = &ipa->gsi;
181684f9bd12SAlex Elder 	int ret;
181784f9bd12SAlex Elder 
181884f9bd12SAlex Elder 	ret = gsi_channel_start(gsi, endpoint->channel_id);
181984f9bd12SAlex Elder 	if (ret) {
1820*5245f4fdSAlex Elder 		dev_err(ipa->dev,
182184f9bd12SAlex Elder 			"error %d starting %cX channel %u for endpoint %u\n",
182284f9bd12SAlex Elder 			ret, endpoint->toward_ipa ? 'T' : 'R',
18239b7a0065SAlex Elder 			endpoint->channel_id, endpoint_id);
182484f9bd12SAlex Elder 		return ret;
182584f9bd12SAlex Elder 	}
182684f9bd12SAlex Elder 
182784f9bd12SAlex Elder 	if (!endpoint->toward_ipa) {
18289b7a0065SAlex Elder 		ipa_interrupt_suspend_enable(ipa->interrupt, endpoint_id);
182984f9bd12SAlex Elder 		ipa_endpoint_replenish_enable(endpoint);
183084f9bd12SAlex Elder 	}
183184f9bd12SAlex Elder 
18329b7a0065SAlex Elder 	__set_bit(endpoint_id, ipa->enabled);
183384f9bd12SAlex Elder 
183484f9bd12SAlex Elder 	return 0;
183584f9bd12SAlex Elder }
183684f9bd12SAlex Elder 
183784f9bd12SAlex Elder void ipa_endpoint_disable_one(struct ipa_endpoint *endpoint)
183884f9bd12SAlex Elder {
18399b7a0065SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
184084f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
1841f30dcb7dSAlex Elder 	struct gsi *gsi = &ipa->gsi;
184284f9bd12SAlex Elder 	int ret;
184384f9bd12SAlex Elder 
18449b7a0065SAlex Elder 	if (!test_bit(endpoint_id, ipa->enabled))
184584f9bd12SAlex Elder 		return;
184684f9bd12SAlex Elder 
18479b7a0065SAlex Elder 	__clear_bit(endpoint_id, endpoint->ipa->enabled);
184884f9bd12SAlex Elder 
184984f9bd12SAlex Elder 	if (!endpoint->toward_ipa) {
185084f9bd12SAlex Elder 		ipa_endpoint_replenish_disable(endpoint);
18519b7a0065SAlex Elder 		ipa_interrupt_suspend_disable(ipa->interrupt, endpoint_id);
185284f9bd12SAlex Elder 	}
185384f9bd12SAlex Elder 
185484f9bd12SAlex Elder 	/* Note that if stop fails, the channel's state is not well-defined */
1855f30dcb7dSAlex Elder 	ret = gsi_channel_stop(gsi, endpoint->channel_id);
185684f9bd12SAlex Elder 	if (ret)
1857*5245f4fdSAlex Elder 		dev_err(ipa->dev, "error %d attempting to stop endpoint %u\n",
1858*5245f4fdSAlex Elder 			ret, endpoint_id);
185984f9bd12SAlex Elder }
186084f9bd12SAlex Elder 
186184f9bd12SAlex Elder void ipa_endpoint_suspend_one(struct ipa_endpoint *endpoint)
186284f9bd12SAlex Elder {
1863*5245f4fdSAlex Elder 	struct device *dev = endpoint->ipa->dev;
186484f9bd12SAlex Elder 	struct gsi *gsi = &endpoint->ipa->gsi;
186584f9bd12SAlex Elder 	int ret;
186684f9bd12SAlex Elder 
18679b7a0065SAlex Elder 	if (!test_bit(endpoint->endpoint_id, endpoint->ipa->enabled))
186884f9bd12SAlex Elder 		return;
186984f9bd12SAlex Elder 
1870ab4f71e5SAlex Elder 	if (!endpoint->toward_ipa) {
187184f9bd12SAlex Elder 		ipa_endpoint_replenish_disable(endpoint);
18724fa95248SAlex Elder 		(void)ipa_endpoint_program_suspend(endpoint, true);
1873ab4f71e5SAlex Elder 	}
187484f9bd12SAlex Elder 
1875decfef0fSAlex Elder 	ret = gsi_channel_suspend(gsi, endpoint->channel_id);
187684f9bd12SAlex Elder 	if (ret)
187784f9bd12SAlex Elder 		dev_err(dev, "error %d suspending channel %u\n", ret,
187884f9bd12SAlex Elder 			endpoint->channel_id);
187984f9bd12SAlex Elder }
188084f9bd12SAlex Elder 
188184f9bd12SAlex Elder void ipa_endpoint_resume_one(struct ipa_endpoint *endpoint)
188284f9bd12SAlex Elder {
1883*5245f4fdSAlex Elder 	struct device *dev = endpoint->ipa->dev;
188484f9bd12SAlex Elder 	struct gsi *gsi = &endpoint->ipa->gsi;
188584f9bd12SAlex Elder 	int ret;
188684f9bd12SAlex Elder 
18879b7a0065SAlex Elder 	if (!test_bit(endpoint->endpoint_id, endpoint->ipa->enabled))
188884f9bd12SAlex Elder 		return;
188984f9bd12SAlex Elder 
1890b07f283eSAlex Elder 	if (!endpoint->toward_ipa)
18914fa95248SAlex Elder 		(void)ipa_endpoint_program_suspend(endpoint, false);
189284f9bd12SAlex Elder 
1893decfef0fSAlex Elder 	ret = gsi_channel_resume(gsi, endpoint->channel_id);
189484f9bd12SAlex Elder 	if (ret)
189584f9bd12SAlex Elder 		dev_err(dev, "error %d resuming channel %u\n", ret,
189684f9bd12SAlex Elder 			endpoint->channel_id);
189784f9bd12SAlex Elder 	else if (!endpoint->toward_ipa)
189884f9bd12SAlex Elder 		ipa_endpoint_replenish_enable(endpoint);
189984f9bd12SAlex Elder }
190084f9bd12SAlex Elder 
190184f9bd12SAlex Elder void ipa_endpoint_suspend(struct ipa *ipa)
190284f9bd12SAlex Elder {
1903d1704382SAlex Elder 	if (!ipa->setup_complete)
1904d1704382SAlex Elder 		return;
1905d1704382SAlex Elder 
190684f9bd12SAlex Elder 	if (ipa->modem_netdev)
190784f9bd12SAlex Elder 		ipa_modem_suspend(ipa->modem_netdev);
190884f9bd12SAlex Elder 
190984f9bd12SAlex Elder 	ipa_endpoint_suspend_one(ipa->name_map[IPA_ENDPOINT_AP_LAN_RX]);
191084f9bd12SAlex Elder 	ipa_endpoint_suspend_one(ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]);
191184f9bd12SAlex Elder }
191284f9bd12SAlex Elder 
191384f9bd12SAlex Elder void ipa_endpoint_resume(struct ipa *ipa)
191484f9bd12SAlex Elder {
1915d1704382SAlex Elder 	if (!ipa->setup_complete)
1916d1704382SAlex Elder 		return;
1917d1704382SAlex Elder 
191884f9bd12SAlex Elder 	ipa_endpoint_resume_one(ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]);
191984f9bd12SAlex Elder 	ipa_endpoint_resume_one(ipa->name_map[IPA_ENDPOINT_AP_LAN_RX]);
192084f9bd12SAlex Elder 
192184f9bd12SAlex Elder 	if (ipa->modem_netdev)
192284f9bd12SAlex Elder 		ipa_modem_resume(ipa->modem_netdev);
192384f9bd12SAlex Elder }
192484f9bd12SAlex Elder 
192584f9bd12SAlex Elder static void ipa_endpoint_setup_one(struct ipa_endpoint *endpoint)
192684f9bd12SAlex Elder {
192784f9bd12SAlex Elder 	struct gsi *gsi = &endpoint->ipa->gsi;
192884f9bd12SAlex Elder 	u32 channel_id = endpoint->channel_id;
192984f9bd12SAlex Elder 
193084f9bd12SAlex Elder 	/* Only AP endpoints get set up */
193184f9bd12SAlex Elder 	if (endpoint->ee_id != GSI_EE_AP)
193284f9bd12SAlex Elder 		return;
193384f9bd12SAlex Elder 
1934317595d2SAlex Elder 	endpoint->skb_frag_max = gsi->channel[channel_id].trans_tre_max - 1;
193584f9bd12SAlex Elder 	if (!endpoint->toward_ipa) {
193684f9bd12SAlex Elder 		/* RX transactions require a single TRE, so the maximum
193784f9bd12SAlex Elder 		 * backlog is the same as the maximum outstanding TREs.
193884f9bd12SAlex Elder 		 */
1939c1aaa01dSAlex Elder 		clear_bit(IPA_REPLENISH_ENABLED, endpoint->replenish_flags);
1940998c0bd2SAlex Elder 		clear_bit(IPA_REPLENISH_ACTIVE, endpoint->replenish_flags);
194184f9bd12SAlex Elder 		INIT_DELAYED_WORK(&endpoint->replenish_work,
194284f9bd12SAlex Elder 				  ipa_endpoint_replenish_work);
194384f9bd12SAlex Elder 	}
194484f9bd12SAlex Elder 
194584f9bd12SAlex Elder 	ipa_endpoint_program(endpoint);
194684f9bd12SAlex Elder 
1947ae5108e9SAlex Elder 	__set_bit(endpoint->endpoint_id, endpoint->ipa->set_up);
194884f9bd12SAlex Elder }
194984f9bd12SAlex Elder 
195084f9bd12SAlex Elder static void ipa_endpoint_teardown_one(struct ipa_endpoint *endpoint)
195184f9bd12SAlex Elder {
1952ae5108e9SAlex Elder 	__clear_bit(endpoint->endpoint_id, endpoint->ipa->set_up);
195384f9bd12SAlex Elder 
195484f9bd12SAlex Elder 	if (!endpoint->toward_ipa)
195584f9bd12SAlex Elder 		cancel_delayed_work_sync(&endpoint->replenish_work);
195684f9bd12SAlex Elder 
195784f9bd12SAlex Elder 	ipa_endpoint_reset(endpoint);
195884f9bd12SAlex Elder }
195984f9bd12SAlex Elder 
196084f9bd12SAlex Elder void ipa_endpoint_setup(struct ipa *ipa)
196184f9bd12SAlex Elder {
19629a9f5129SAlex Elder 	u32 endpoint_id;
196384f9bd12SAlex Elder 
19649a9f5129SAlex Elder 	for_each_set_bit(endpoint_id, ipa->defined, ipa->endpoint_count)
196584f9bd12SAlex Elder 		ipa_endpoint_setup_one(&ipa->endpoint[endpoint_id]);
196684f9bd12SAlex Elder }
196784f9bd12SAlex Elder 
196884f9bd12SAlex Elder void ipa_endpoint_teardown(struct ipa *ipa)
196984f9bd12SAlex Elder {
1970ae5108e9SAlex Elder 	u32 endpoint_id;
197184f9bd12SAlex Elder 
1972ae5108e9SAlex Elder 	for_each_set_bit(endpoint_id, ipa->set_up, ipa->endpoint_count)
197384f9bd12SAlex Elder 		ipa_endpoint_teardown_one(&ipa->endpoint[endpoint_id]);
197484f9bd12SAlex Elder }
197584f9bd12SAlex Elder 
197688de7672SAlex Elder void ipa_endpoint_deconfig(struct ipa *ipa)
197788de7672SAlex Elder {
197888de7672SAlex Elder 	ipa->available_count = 0;
197988de7672SAlex Elder 	bitmap_free(ipa->available);
198088de7672SAlex Elder 	ipa->available = NULL;
198188de7672SAlex Elder }
198288de7672SAlex Elder 
198384f9bd12SAlex Elder int ipa_endpoint_config(struct ipa *ipa)
198484f9bd12SAlex Elder {
1985*5245f4fdSAlex Elder 	struct device *dev = ipa->dev;
198681772e44SAlex Elder 	const struct reg *reg;
19879a9f5129SAlex Elder 	u32 endpoint_id;
198807abde54SAlex Elder 	u32 hw_limit;
19892b87d721SAlex Elder 	u32 tx_count;
19902b87d721SAlex Elder 	u32 rx_count;
199184f9bd12SAlex Elder 	u32 rx_base;
19922b87d721SAlex Elder 	u32 limit;
199384f9bd12SAlex Elder 	u32 val;
199484f9bd12SAlex Elder 
1995110971d1SAlex Elder 	/* Prior to IPA v3.5, the FLAVOR_0 register was not supported.
1996110971d1SAlex Elder 	 * Furthermore, the endpoints were not grouped such that TX
1997110971d1SAlex Elder 	 * endpoint numbers started with 0 and RX endpoints had numbers
1998110971d1SAlex Elder 	 * higher than all TX endpoints, so we can't do the simple
1999110971d1SAlex Elder 	 * direction check used for newer hardware below.
2000110971d1SAlex Elder 	 *
2001110971d1SAlex Elder 	 * For hardware that doesn't support the FLAVOR_0 register,
2002110971d1SAlex Elder 	 * just set the available mask to support any endpoint, and
2003110971d1SAlex Elder 	 * assume the configuration is valid.
2004110971d1SAlex Elder 	 */
2005110971d1SAlex Elder 	if (ipa->version < IPA_VERSION_3_5) {
200688de7672SAlex Elder 		ipa->available = bitmap_zalloc(IPA_ENDPOINT_MAX, GFP_KERNEL);
200788de7672SAlex Elder 		if (!ipa->available)
200888de7672SAlex Elder 			return -ENOMEM;
200988de7672SAlex Elder 		ipa->available_count = IPA_ENDPOINT_MAX;
201088de7672SAlex Elder 
201188de7672SAlex Elder 		bitmap_set(ipa->available, 0, IPA_ENDPOINT_MAX);
201288de7672SAlex Elder 
2013110971d1SAlex Elder 		return 0;
2014110971d1SAlex Elder 	}
2015110971d1SAlex Elder 
201684f9bd12SAlex Elder 	/* Find out about the endpoints supplied by the hardware, and ensure
20172b87d721SAlex Elder 	 * the highest one doesn't exceed the number supported by software.
201884f9bd12SAlex Elder 	 */
20196a244b75SAlex Elder 	reg = ipa_reg(ipa, FLAVOR_0);
2020fc4cecf7SAlex Elder 	val = ioread32(ipa->reg_virt + reg_offset(reg));
202184f9bd12SAlex Elder 
20222b87d721SAlex Elder 	/* Our RX is an IPA producer; our TX is an IPA consumer. */
2023f1470fd7SAlex Elder 	tx_count = reg_decode(reg, MAX_CONS_PIPES, val);
2024f1470fd7SAlex Elder 	rx_count = reg_decode(reg, MAX_PROD_PIPES, val);
2025f1470fd7SAlex Elder 	rx_base = reg_decode(reg, PROD_LOWEST, val);
20262b87d721SAlex Elder 
20272b87d721SAlex Elder 	limit = rx_base + rx_count;
20282b87d721SAlex Elder 	if (limit > IPA_ENDPOINT_MAX) {
20292b87d721SAlex Elder 		dev_err(dev, "too many endpoints, %u > %u\n",
20302b87d721SAlex Elder 			limit, IPA_ENDPOINT_MAX);
203184f9bd12SAlex Elder 		return -EINVAL;
203284f9bd12SAlex Elder 	}
203384f9bd12SAlex Elder 
203407abde54SAlex Elder 	/* Until IPA v5.0, the max endpoint ID was 32 */
203507abde54SAlex Elder 	hw_limit = ipa->version < IPA_VERSION_5_0 ? 32 : U8_MAX + 1;
203607abde54SAlex Elder 	if (limit > hw_limit) {
203707abde54SAlex Elder 		dev_err(dev, "unexpected endpoint count, %u > %u\n",
203807abde54SAlex Elder 			limit, hw_limit);
203907abde54SAlex Elder 		return -EINVAL;
204007abde54SAlex Elder 	}
204107abde54SAlex Elder 
204288de7672SAlex Elder 	/* Allocate and initialize the available endpoint bitmap */
204388de7672SAlex Elder 	ipa->available = bitmap_zalloc(limit, GFP_KERNEL);
204488de7672SAlex Elder 	if (!ipa->available)
204588de7672SAlex Elder 		return -ENOMEM;
204688de7672SAlex Elder 	ipa->available_count = limit;
204788de7672SAlex Elder 
20482b87d721SAlex Elder 	/* Mark all supported RX and TX endpoints as available */
204988de7672SAlex Elder 	bitmap_set(ipa->available, 0, tx_count);
205088de7672SAlex Elder 	bitmap_set(ipa->available, rx_base, rx_count);
205184f9bd12SAlex Elder 
20529a9f5129SAlex Elder 	for_each_set_bit(endpoint_id, ipa->defined, ipa->endpoint_count) {
205384f9bd12SAlex Elder 		struct ipa_endpoint *endpoint;
205484f9bd12SAlex Elder 
20552b87d721SAlex Elder 		if (endpoint_id >= limit) {
20562b87d721SAlex Elder 			dev_err(dev, "invalid endpoint id, %u > %u\n",
20572b87d721SAlex Elder 				endpoint_id, limit - 1);
205888de7672SAlex Elder 			goto err_free_bitmap;
205984f9bd12SAlex Elder 		}
206084f9bd12SAlex Elder 
206188de7672SAlex Elder 		if (!test_bit(endpoint_id, ipa->available)) {
20622b87d721SAlex Elder 			dev_err(dev, "unavailable endpoint id %u\n",
20632b87d721SAlex Elder 				endpoint_id);
206488de7672SAlex Elder 			goto err_free_bitmap;
20652b87d721SAlex Elder 		}
20662b87d721SAlex Elder 
20672b87d721SAlex Elder 		/* Make sure it's pointing in the right direction */
20682b87d721SAlex Elder 		endpoint = &ipa->endpoint[endpoint_id];
20692b87d721SAlex Elder 		if (endpoint->toward_ipa) {
20702b87d721SAlex Elder 			if (endpoint_id < tx_count)
20712b87d721SAlex Elder 				continue;
20722b87d721SAlex Elder 		} else if (endpoint_id >= rx_base) {
20732b87d721SAlex Elder 			continue;
20742b87d721SAlex Elder 		}
20752b87d721SAlex Elder 
20762b87d721SAlex Elder 		dev_err(dev, "endpoint id %u wrong direction\n", endpoint_id);
207788de7672SAlex Elder 		goto err_free_bitmap;
20782b87d721SAlex Elder 	}
20792b87d721SAlex Elder 
20802b87d721SAlex Elder 	return 0;
208184f9bd12SAlex Elder 
208288de7672SAlex Elder err_free_bitmap:
208388de7672SAlex Elder 	ipa_endpoint_deconfig(ipa);
208488de7672SAlex Elder 
208588de7672SAlex Elder 	return -EINVAL;
208684f9bd12SAlex Elder }
208784f9bd12SAlex Elder 
208884f9bd12SAlex Elder static void ipa_endpoint_init_one(struct ipa *ipa, enum ipa_endpoint_name name,
208984f9bd12SAlex Elder 				  const struct ipa_gsi_endpoint_data *data)
209084f9bd12SAlex Elder {
209184f9bd12SAlex Elder 	struct ipa_endpoint *endpoint;
209284f9bd12SAlex Elder 
209384f9bd12SAlex Elder 	endpoint = &ipa->endpoint[data->endpoint_id];
209484f9bd12SAlex Elder 
209584f9bd12SAlex Elder 	if (data->ee_id == GSI_EE_AP)
209684f9bd12SAlex Elder 		ipa->channel_map[data->channel_id] = endpoint;
209784f9bd12SAlex Elder 	ipa->name_map[name] = endpoint;
209884f9bd12SAlex Elder 
209984f9bd12SAlex Elder 	endpoint->ipa = ipa;
210084f9bd12SAlex Elder 	endpoint->ee_id = data->ee_id;
210184f9bd12SAlex Elder 	endpoint->channel_id = data->channel_id;
210284f9bd12SAlex Elder 	endpoint->endpoint_id = data->endpoint_id;
210384f9bd12SAlex Elder 	endpoint->toward_ipa = data->toward_ipa;
2104660e52d6SAlex Elder 	endpoint->config = data->endpoint.config;
210584f9bd12SAlex Elder 
21069a9f5129SAlex Elder 	__set_bit(endpoint->endpoint_id, ipa->defined);
210784f9bd12SAlex Elder }
210884f9bd12SAlex Elder 
2109602a1c76SAlex Elder static void ipa_endpoint_exit_one(struct ipa_endpoint *endpoint)
211084f9bd12SAlex Elder {
21119a9f5129SAlex Elder 	__clear_bit(endpoint->endpoint_id, endpoint->ipa->defined);
211284f9bd12SAlex Elder 
211384f9bd12SAlex Elder 	memset(endpoint, 0, sizeof(*endpoint));
211484f9bd12SAlex Elder }
211584f9bd12SAlex Elder 
211684f9bd12SAlex Elder void ipa_endpoint_exit(struct ipa *ipa)
211784f9bd12SAlex Elder {
21189a9f5129SAlex Elder 	u32 endpoint_id;
211984f9bd12SAlex Elder 
21200f97fbd4SAlex Elder 	ipa->filtered = 0;
21210f97fbd4SAlex Elder 
21229a9f5129SAlex Elder 	for_each_set_bit(endpoint_id, ipa->defined, ipa->endpoint_count)
212384f9bd12SAlex Elder 		ipa_endpoint_exit_one(&ipa->endpoint[endpoint_id]);
21249a9f5129SAlex Elder 
21259b7a0065SAlex Elder 	bitmap_free(ipa->enabled);
21269b7a0065SAlex Elder 	ipa->enabled = NULL;
2127ae5108e9SAlex Elder 	bitmap_free(ipa->set_up);
2128ae5108e9SAlex Elder 	ipa->set_up = NULL;
21299a9f5129SAlex Elder 	bitmap_free(ipa->defined);
21309a9f5129SAlex Elder 	ipa->defined = NULL;
21319a9f5129SAlex Elder 
213284f9bd12SAlex Elder 	memset(ipa->name_map, 0, sizeof(ipa->name_map));
213384f9bd12SAlex Elder 	memset(ipa->channel_map, 0, sizeof(ipa->channel_map));
213484f9bd12SAlex Elder }
213584f9bd12SAlex Elder 
213684f9bd12SAlex Elder /* Returns a bitmask of endpoints that support filtering, or 0 on error */
21370f97fbd4SAlex Elder int ipa_endpoint_init(struct ipa *ipa, u32 count,
213884f9bd12SAlex Elder 		      const struct ipa_gsi_endpoint_data *data)
213984f9bd12SAlex Elder {
214084f9bd12SAlex Elder 	enum ipa_endpoint_name name;
21410f97fbd4SAlex Elder 	u32 filtered;
214284f9bd12SAlex Elder 
21439654d8c4SAlex Elder 	BUILD_BUG_ON(!IPA_REPLENISH_BATCH);
21449654d8c4SAlex Elder 
2145b7aaff0bSAlex Elder 	/* Number of endpoints is one more than the maximum ID */
2146b7aaff0bSAlex Elder 	ipa->endpoint_count = ipa_endpoint_max(ipa, count, data) + 1;
2147b7aaff0bSAlex Elder 	if (!ipa->endpoint_count)
21480f97fbd4SAlex Elder 		return -EINVAL;
214984f9bd12SAlex Elder 
2150ae5108e9SAlex Elder 	/* Initialize endpoint state bitmaps */
21519a9f5129SAlex Elder 	ipa->defined = bitmap_zalloc(ipa->endpoint_count, GFP_KERNEL);
21529a9f5129SAlex Elder 	if (!ipa->defined)
21530f97fbd4SAlex Elder 		return -ENOMEM;
215484f9bd12SAlex Elder 
2155ae5108e9SAlex Elder 	ipa->set_up = bitmap_zalloc(ipa->endpoint_count, GFP_KERNEL);
2156ae5108e9SAlex Elder 	if (!ipa->set_up)
2157ae5108e9SAlex Elder 		goto err_free_defined;
2158ae5108e9SAlex Elder 
21599b7a0065SAlex Elder 	ipa->enabled = bitmap_zalloc(ipa->endpoint_count, GFP_KERNEL);
21609b7a0065SAlex Elder 	if (!ipa->enabled)
21619b7a0065SAlex Elder 		goto err_free_set_up;
21629b7a0065SAlex Elder 
21630f97fbd4SAlex Elder 	filtered = 0;
216484f9bd12SAlex Elder 	for (name = 0; name < count; name++, data++) {
216584f9bd12SAlex Elder 		if (ipa_gsi_endpoint_data_empty(data))
216684f9bd12SAlex Elder 			continue;	/* Skip over empty slots */
216784f9bd12SAlex Elder 
216884f9bd12SAlex Elder 		ipa_endpoint_init_one(ipa, name, data);
216984f9bd12SAlex Elder 
217084f9bd12SAlex Elder 		if (data->endpoint.filter_support)
21710f97fbd4SAlex Elder 			filtered |= BIT(data->endpoint_id);
21722091c79aSAlex Elder 		if (data->ee_id == GSI_EE_MODEM && data->toward_ipa)
21732091c79aSAlex Elder 			ipa->modem_tx_count++;
217484f9bd12SAlex Elder 	}
217584f9bd12SAlex Elder 
2176ae5108e9SAlex Elder 	/* Make sure the set of filtered endpoints is valid */
2177ae5108e9SAlex Elder 	if (!ipa_filtered_valid(ipa, filtered)) {
2178ae5108e9SAlex Elder 		ipa_endpoint_exit(ipa);
2179ae5108e9SAlex Elder 
2180ae5108e9SAlex Elder 		return -EINVAL;
2181ae5108e9SAlex Elder 	}
218284f9bd12SAlex Elder 
21830f97fbd4SAlex Elder 	ipa->filtered = filtered;
21840f97fbd4SAlex Elder 
21850f97fbd4SAlex Elder 	return 0;
218684f9bd12SAlex Elder 
21879b7a0065SAlex Elder err_free_set_up:
21889b7a0065SAlex Elder 	bitmap_free(ipa->set_up);
21899b7a0065SAlex Elder 	ipa->set_up = NULL;
2190ae5108e9SAlex Elder err_free_defined:
2191ae5108e9SAlex Elder 	bitmap_free(ipa->defined);
2192ae5108e9SAlex Elder 	ipa->defined = NULL;
219384f9bd12SAlex Elder 
2194ae5108e9SAlex Elder 	return -ENOMEM;
219584f9bd12SAlex Elder }
2196