xref: /linux/drivers/net/ipa/ipa_endpoint.c (revision 0f97fbd478587a4ac2d53a88e5a9aefd6632a251)
184f9bd12SAlex Elder // SPDX-License-Identifier: GPL-2.0
284f9bd12SAlex Elder 
384f9bd12SAlex Elder /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
4a4388da5SAlex Elder  * Copyright (C) 2019-2022 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 
3784f9bd12SAlex Elder /** enum ipa_status_opcode - status element opcode hardware values */
3884f9bd12SAlex Elder enum ipa_status_opcode {
3984f9bd12SAlex Elder 	IPA_STATUS_OPCODE_PACKET		= 0x01,
4084f9bd12SAlex Elder 	IPA_STATUS_OPCODE_DROPPED_PACKET	= 0x04,
4184f9bd12SAlex Elder 	IPA_STATUS_OPCODE_SUSPENDED_PACKET	= 0x08,
4284f9bd12SAlex Elder 	IPA_STATUS_OPCODE_PACKET_2ND_PASS	= 0x40,
4384f9bd12SAlex Elder };
4484f9bd12SAlex Elder 
4584f9bd12SAlex Elder /** enum ipa_status_exception - status element exception type */
4684f9bd12SAlex Elder enum ipa_status_exception {
4784f9bd12SAlex Elder 	/* 0 means no exception */
4884f9bd12SAlex Elder 	IPA_STATUS_EXCEPTION_DEAGGR		= 0x01,
4984f9bd12SAlex Elder };
5084f9bd12SAlex Elder 
5184f9bd12SAlex Elder /* Status element provided by hardware */
5284f9bd12SAlex Elder struct ipa_status {
5384f9bd12SAlex Elder 	u8 opcode;		/* enum ipa_status_opcode */
5484f9bd12SAlex Elder 	u8 exception;		/* enum ipa_status_exception */
5584f9bd12SAlex Elder 	__le16 mask;
5684f9bd12SAlex Elder 	__le16 pkt_len;
5784f9bd12SAlex Elder 	u8 endp_src_idx;
5884f9bd12SAlex Elder 	u8 endp_dst_idx;
5984f9bd12SAlex Elder 	__le32 metadata;
6084f9bd12SAlex Elder 	__le32 flags1;
6184f9bd12SAlex Elder 	__le64 flags2;
6284f9bd12SAlex Elder 	__le32 flags3;
6384f9bd12SAlex Elder 	__le32 flags4;
6484f9bd12SAlex Elder };
6584f9bd12SAlex Elder 
6684f9bd12SAlex Elder /* Field masks for struct ipa_status structure fields */
67f6aba7b5SAlex Elder #define IPA_STATUS_MASK_TAG_VALID_FMASK		GENMASK(4, 4)
68f6aba7b5SAlex Elder #define IPA_STATUS_SRC_IDX_FMASK		GENMASK(4, 0)
6984f9bd12SAlex Elder #define IPA_STATUS_DST_IDX_FMASK		GENMASK(4, 0)
7084f9bd12SAlex Elder #define IPA_STATUS_FLAGS1_RT_RULE_ID_FMASK	GENMASK(31, 22)
71f6aba7b5SAlex Elder #define IPA_STATUS_FLAGS2_TAG_FMASK		GENMASK_ULL(63, 16)
7284f9bd12SAlex Elder 
733cebb7c2SAlex Elder /* Compute the aggregation size value to use for a given buffer size */
743cebb7c2SAlex Elder static u32 ipa_aggr_size_kb(u32 rx_buffer_size, bool aggr_hard_limit)
753cebb7c2SAlex Elder {
763cebb7c2SAlex Elder 	/* A hard aggregation limit will not be crossed; aggregation closes
773cebb7c2SAlex Elder 	 * if saving incoming data would cross the hard byte limit boundary.
783cebb7c2SAlex Elder 	 *
793cebb7c2SAlex Elder 	 * With a soft limit, aggregation closes *after* the size boundary
803cebb7c2SAlex Elder 	 * has been crossed.  In that case the limit must leave enough space
813cebb7c2SAlex Elder 	 * after that limit to receive a full MTU of data plus overhead.
823cebb7c2SAlex Elder 	 */
833cebb7c2SAlex Elder 	if (!aggr_hard_limit)
843cebb7c2SAlex Elder 		rx_buffer_size -= IPA_MTU + IPA_RX_BUFFER_OVERHEAD;
853cebb7c2SAlex Elder 
863cebb7c2SAlex Elder 	/* The byte limit is encoded as a number of kilobytes */
873cebb7c2SAlex Elder 
883cebb7c2SAlex Elder 	return rx_buffer_size / SZ_1K;
893cebb7c2SAlex Elder }
903cebb7c2SAlex Elder 
9184f9bd12SAlex Elder static bool ipa_endpoint_data_valid_one(struct ipa *ipa, u32 count,
9284f9bd12SAlex Elder 			    const struct ipa_gsi_endpoint_data *all_data,
9384f9bd12SAlex Elder 			    const struct ipa_gsi_endpoint_data *data)
9484f9bd12SAlex Elder {
9584f9bd12SAlex Elder 	const struct ipa_gsi_endpoint_data *other_data;
9684f9bd12SAlex Elder 	struct device *dev = &ipa->pdev->dev;
9784f9bd12SAlex Elder 	enum ipa_endpoint_name other_name;
9884f9bd12SAlex Elder 
9984f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(data))
10084f9bd12SAlex Elder 		return true;
10184f9bd12SAlex Elder 
10284f9bd12SAlex Elder 	if (!data->toward_ipa) {
1033cebb7c2SAlex Elder 		const struct ipa_endpoint_rx *rx_config;
104216b409dSAlex Elder 		const struct ipa_reg *reg;
105ed23f026SAlex Elder 		u32 buffer_size;
1063cebb7c2SAlex Elder 		u32 aggr_size;
107ed23f026SAlex Elder 		u32 limit;
108ed23f026SAlex Elder 
10984f9bd12SAlex Elder 		if (data->endpoint.filter_support) {
11084f9bd12SAlex Elder 			dev_err(dev, "filtering not supported for "
11184f9bd12SAlex Elder 					"RX endpoint %u\n",
11284f9bd12SAlex Elder 				data->endpoint_id);
11384f9bd12SAlex Elder 			return false;
11484f9bd12SAlex Elder 		}
11584f9bd12SAlex Elder 
116ed23f026SAlex Elder 		/* Nothing more to check for non-AP RX */
117ed23f026SAlex Elder 		if (data->ee_id != GSI_EE_AP)
118ed23f026SAlex Elder 			return true;
119ed23f026SAlex Elder 
1203cebb7c2SAlex Elder 		rx_config = &data->endpoint.config.rx;
1213cebb7c2SAlex Elder 
122ed23f026SAlex Elder 		/* The buffer size must hold an MTU plus overhead */
1233cebb7c2SAlex Elder 		buffer_size = rx_config->buffer_size;
124ed23f026SAlex Elder 		limit = IPA_MTU + IPA_RX_BUFFER_OVERHEAD;
125ed23f026SAlex Elder 		if (buffer_size < limit) {
126ed23f026SAlex Elder 			dev_err(dev, "RX buffer size too small for RX endpoint %u (%u < %u)\n",
127ed23f026SAlex Elder 				data->endpoint_id, buffer_size, limit);
128ed23f026SAlex Elder 			return false;
129ed23f026SAlex Elder 		}
130ed23f026SAlex Elder 
1313cebb7c2SAlex Elder 		if (!data->endpoint.config.aggregation) {
1323cebb7c2SAlex Elder 			bool result = true;
1333cebb7c2SAlex Elder 
1343cebb7c2SAlex Elder 			/* No aggregation; check for bogus aggregation data */
135beb90cbaSAlex Elder 			if (rx_config->aggr_time_limit) {
136beb90cbaSAlex Elder 				dev_err(dev,
137beb90cbaSAlex Elder 					"time limit with no aggregation for RX endpoint %u\n",
138beb90cbaSAlex Elder 					data->endpoint_id);
139beb90cbaSAlex Elder 				result = false;
140beb90cbaSAlex Elder 			}
141beb90cbaSAlex Elder 
1423cebb7c2SAlex Elder 			if (rx_config->aggr_hard_limit) {
1433cebb7c2SAlex Elder 				dev_err(dev, "hard limit with no aggregation for RX endpoint %u\n",
1443cebb7c2SAlex Elder 					data->endpoint_id);
1453cebb7c2SAlex Elder 				result = false;
1463cebb7c2SAlex Elder 			}
1473cebb7c2SAlex Elder 
1483cebb7c2SAlex Elder 			if (rx_config->aggr_close_eof) {
1493cebb7c2SAlex Elder 				dev_err(dev, "close EOF with no aggregation for RX endpoint %u\n",
1503cebb7c2SAlex Elder 					data->endpoint_id);
1513cebb7c2SAlex Elder 				result = false;
1523cebb7c2SAlex Elder 			}
1533cebb7c2SAlex Elder 
1543cebb7c2SAlex Elder 			return result;	/* Nothing more to check */
1553cebb7c2SAlex Elder 		}
1563cebb7c2SAlex Elder 
1573cebb7c2SAlex Elder 		/* For an endpoint supporting receive aggregation, the byte
1583cebb7c2SAlex Elder 		 * limit defines the point at which aggregation closes.  This
1593cebb7c2SAlex Elder 		 * check ensures the receive buffer size doesn't result in a
1603cebb7c2SAlex Elder 		 * limit that exceeds what's representable in the aggregation
1613cebb7c2SAlex Elder 		 * byte limit field.
162ed23f026SAlex Elder 		 */
1633cebb7c2SAlex Elder 		aggr_size = ipa_aggr_size_kb(buffer_size - NET_SKB_PAD,
1643cebb7c2SAlex Elder 					     rx_config->aggr_hard_limit);
165216b409dSAlex Elder 		reg = ipa_reg(ipa, ENDP_INIT_AGGR);
166216b409dSAlex Elder 
167216b409dSAlex Elder 		limit = ipa_reg_field_max(reg, BYTE_LIMIT);
1683cebb7c2SAlex Elder 		if (aggr_size > limit) {
1693cebb7c2SAlex Elder 			dev_err(dev, "aggregated size too large for RX endpoint %u (%u KB > %u KB)\n",
1703cebb7c2SAlex Elder 				data->endpoint_id, aggr_size, limit);
171ed23f026SAlex Elder 
172ed23f026SAlex Elder 			return false;
173ed23f026SAlex Elder 		}
174ed23f026SAlex Elder 
17584f9bd12SAlex Elder 		return true;	/* Nothing more to check for RX */
17684f9bd12SAlex Elder 	}
17784f9bd12SAlex Elder 
178a14d5937SAlex Elder 	/* Starting with IPA v4.5 sequencer replication is obsolete */
179a14d5937SAlex Elder 	if (ipa->version >= IPA_VERSION_4_5) {
180a14d5937SAlex Elder 		if (data->endpoint.config.tx.seq_rep_type) {
181a14d5937SAlex Elder 			dev_err(dev, "no-zero seq_rep_type TX endpoint %u\n",
182a14d5937SAlex Elder 				data->endpoint_id);
183a14d5937SAlex Elder 			return false;
184a14d5937SAlex Elder 		}
185a14d5937SAlex Elder 	}
186a14d5937SAlex Elder 
18784f9bd12SAlex Elder 	if (data->endpoint.config.status_enable) {
18884f9bd12SAlex Elder 		other_name = data->endpoint.config.tx.status_endpoint;
18984f9bd12SAlex Elder 		if (other_name >= count) {
19084f9bd12SAlex Elder 			dev_err(dev, "status endpoint name %u out of range "
19184f9bd12SAlex Elder 					"for endpoint %u\n",
19284f9bd12SAlex Elder 				other_name, data->endpoint_id);
19384f9bd12SAlex Elder 			return false;
19484f9bd12SAlex Elder 		}
19584f9bd12SAlex Elder 
19684f9bd12SAlex Elder 		/* Status endpoint must be defined... */
19784f9bd12SAlex Elder 		other_data = &all_data[other_name];
19884f9bd12SAlex Elder 		if (ipa_gsi_endpoint_data_empty(other_data)) {
19984f9bd12SAlex Elder 			dev_err(dev, "DMA endpoint name %u undefined "
20084f9bd12SAlex Elder 					"for endpoint %u\n",
20184f9bd12SAlex Elder 				other_name, data->endpoint_id);
20284f9bd12SAlex Elder 			return false;
20384f9bd12SAlex Elder 		}
20484f9bd12SAlex Elder 
20584f9bd12SAlex Elder 		/* ...and has to be an RX endpoint... */
20684f9bd12SAlex Elder 		if (other_data->toward_ipa) {
20784f9bd12SAlex Elder 			dev_err(dev,
20884f9bd12SAlex Elder 				"status endpoint for endpoint %u not RX\n",
20984f9bd12SAlex Elder 				data->endpoint_id);
21084f9bd12SAlex Elder 			return false;
21184f9bd12SAlex Elder 		}
21284f9bd12SAlex Elder 
21384f9bd12SAlex Elder 		/* ...and if it's to be an AP endpoint... */
21484f9bd12SAlex Elder 		if (other_data->ee_id == GSI_EE_AP) {
21584f9bd12SAlex Elder 			/* ...make sure it has status enabled. */
21684f9bd12SAlex Elder 			if (!other_data->endpoint.config.status_enable) {
21784f9bd12SAlex Elder 				dev_err(dev,
21884f9bd12SAlex Elder 					"status not enabled for endpoint %u\n",
21984f9bd12SAlex Elder 					other_data->endpoint_id);
22084f9bd12SAlex Elder 				return false;
22184f9bd12SAlex Elder 			}
22284f9bd12SAlex Elder 		}
22384f9bd12SAlex Elder 	}
22484f9bd12SAlex Elder 
22584f9bd12SAlex Elder 	if (data->endpoint.config.dma_mode) {
22684f9bd12SAlex Elder 		other_name = data->endpoint.config.dma_endpoint;
22784f9bd12SAlex Elder 		if (other_name >= count) {
22884f9bd12SAlex Elder 			dev_err(dev, "DMA endpoint name %u out of range "
22984f9bd12SAlex Elder 					"for endpoint %u\n",
23084f9bd12SAlex Elder 				other_name, data->endpoint_id);
23184f9bd12SAlex Elder 			return false;
23284f9bd12SAlex Elder 		}
23384f9bd12SAlex Elder 
23484f9bd12SAlex Elder 		other_data = &all_data[other_name];
23584f9bd12SAlex Elder 		if (ipa_gsi_endpoint_data_empty(other_data)) {
23684f9bd12SAlex Elder 			dev_err(dev, "DMA endpoint name %u undefined "
23784f9bd12SAlex Elder 					"for endpoint %u\n",
23884f9bd12SAlex Elder 				other_name, data->endpoint_id);
23984f9bd12SAlex Elder 			return false;
24084f9bd12SAlex Elder 		}
24184f9bd12SAlex Elder 	}
24284f9bd12SAlex Elder 
24384f9bd12SAlex Elder 	return true;
24484f9bd12SAlex Elder }
24584f9bd12SAlex Elder 
2465274c715SAlex Elder /* Validate endpoint configuration data.  Return max defined endpoint ID */
2475274c715SAlex Elder static u32 ipa_endpoint_max(struct ipa *ipa, u32 count,
24884f9bd12SAlex Elder 			    const struct ipa_gsi_endpoint_data *data)
24984f9bd12SAlex Elder {
25084f9bd12SAlex Elder 	const struct ipa_gsi_endpoint_data *dp = data;
25184f9bd12SAlex Elder 	struct device *dev = &ipa->pdev->dev;
25284f9bd12SAlex Elder 	enum ipa_endpoint_name name;
2535274c715SAlex Elder 	u32 max;
25484f9bd12SAlex Elder 
25584f9bd12SAlex Elder 	if (count > IPA_ENDPOINT_COUNT) {
25684f9bd12SAlex Elder 		dev_err(dev, "too many endpoints specified (%u > %u)\n",
25784f9bd12SAlex Elder 			count, IPA_ENDPOINT_COUNT);
2585274c715SAlex Elder 		return 0;
25984f9bd12SAlex Elder 	}
26084f9bd12SAlex Elder 
26184f9bd12SAlex Elder 	/* Make sure needed endpoints have defined data */
26284f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_COMMAND_TX])) {
26384f9bd12SAlex Elder 		dev_err(dev, "command TX endpoint not defined\n");
2645274c715SAlex Elder 		return 0;
26584f9bd12SAlex Elder 	}
26684f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_LAN_RX])) {
26784f9bd12SAlex Elder 		dev_err(dev, "LAN RX endpoint not defined\n");
2685274c715SAlex Elder 		return 0;
26984f9bd12SAlex Elder 	}
27084f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_MODEM_TX])) {
27184f9bd12SAlex Elder 		dev_err(dev, "AP->modem TX endpoint not defined\n");
2725274c715SAlex Elder 		return 0;
27384f9bd12SAlex Elder 	}
27484f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_MODEM_RX])) {
27584f9bd12SAlex Elder 		dev_err(dev, "AP<-modem RX endpoint not defined\n");
2765274c715SAlex Elder 		return 0;
27784f9bd12SAlex Elder 	}
27884f9bd12SAlex Elder 
2795274c715SAlex Elder 	max = 0;
2805274c715SAlex Elder 	for (name = 0; name < count; name++, dp++) {
28184f9bd12SAlex Elder 		if (!ipa_endpoint_data_valid_one(ipa, count, data, dp))
2825274c715SAlex Elder 			return 0;
2835274c715SAlex Elder 		max = max_t(u32, max, dp->endpoint_id);
2845274c715SAlex Elder 	}
28584f9bd12SAlex Elder 
2865274c715SAlex Elder 	return max;
28784f9bd12SAlex Elder }
28884f9bd12SAlex Elder 
28984f9bd12SAlex Elder /* Allocate a transaction to use on a non-command endpoint */
29084f9bd12SAlex Elder static struct gsi_trans *ipa_endpoint_trans_alloc(struct ipa_endpoint *endpoint,
29184f9bd12SAlex Elder 						  u32 tre_count)
29284f9bd12SAlex Elder {
29384f9bd12SAlex Elder 	struct gsi *gsi = &endpoint->ipa->gsi;
29484f9bd12SAlex Elder 	u32 channel_id = endpoint->channel_id;
29584f9bd12SAlex Elder 	enum dma_data_direction direction;
29684f9bd12SAlex Elder 
29784f9bd12SAlex Elder 	direction = endpoint->toward_ipa ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
29884f9bd12SAlex Elder 
29984f9bd12SAlex Elder 	return gsi_channel_trans_alloc(gsi, channel_id, tre_count, direction);
30084f9bd12SAlex Elder }
30184f9bd12SAlex Elder 
30284f9bd12SAlex Elder /* suspend_delay represents suspend for RX, delay for TX endpoints.
3034c9d631aSAlex Elder  * Note that suspend is not supported starting with IPA v4.0, and
3044c9d631aSAlex Elder  * delay mode should not be used starting with IPA v4.2.
30584f9bd12SAlex Elder  */
3064900bf34SAlex Elder static bool
30784f9bd12SAlex Elder ipa_endpoint_init_ctrl(struct ipa_endpoint *endpoint, bool suspend_delay)
30884f9bd12SAlex Elder {
30984f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
3106a244b75SAlex Elder 	const struct ipa_reg *reg;
3114468a344SAlex Elder 	u32 field_id;
3126bfb7538SAlex Elder 	u32 offset;
3134900bf34SAlex Elder 	bool state;
31484f9bd12SAlex Elder 	u32 mask;
31584f9bd12SAlex Elder 	u32 val;
31684f9bd12SAlex Elder 
3175bc55884SAlex Elder 	if (endpoint->toward_ipa)
3184c9d631aSAlex Elder 		WARN_ON(ipa->version >= IPA_VERSION_4_2);
3195bc55884SAlex Elder 	else
3205bc55884SAlex Elder 		WARN_ON(ipa->version >= IPA_VERSION_4_0);
3215bc55884SAlex Elder 
3226a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_CTRL);
3236a244b75SAlex Elder 	offset = ipa_reg_n_offset(reg, endpoint->endpoint_id);
32484f9bd12SAlex Elder 	val = ioread32(ipa->reg_virt + offset);
3256a244b75SAlex Elder 
3264468a344SAlex Elder 	field_id = endpoint->toward_ipa ? ENDP_DELAY : ENDP_SUSPEND;
3274468a344SAlex Elder 	mask = ipa_reg_bit(reg, field_id);
3284468a344SAlex Elder 
3294900bf34SAlex Elder 	state = !!(val & mask);
3305bc55884SAlex Elder 
3315bc55884SAlex Elder 	/* Don't bother if it's already in the requested state */
3324900bf34SAlex Elder 	if (suspend_delay != state) {
33384f9bd12SAlex Elder 		val ^= mask;
33484f9bd12SAlex Elder 		iowrite32(val, ipa->reg_virt + offset);
3354900bf34SAlex Elder 	}
33684f9bd12SAlex Elder 
3374900bf34SAlex Elder 	return state;
33884f9bd12SAlex Elder }
33984f9bd12SAlex Elder 
3404c9d631aSAlex Elder /* We don't care what the previous state was for delay mode */
3414fa95248SAlex Elder static void
3424fa95248SAlex Elder ipa_endpoint_program_delay(struct ipa_endpoint *endpoint, bool enable)
3434fa95248SAlex Elder {
3444c9d631aSAlex Elder 	/* Delay mode should not be used for IPA v4.2+ */
3454c9d631aSAlex Elder 	WARN_ON(endpoint->ipa->version >= IPA_VERSION_4_2);
3465bc55884SAlex Elder 	WARN_ON(!endpoint->toward_ipa);
3474fa95248SAlex Elder 
3484fa95248SAlex Elder 	(void)ipa_endpoint_init_ctrl(endpoint, enable);
3494fa95248SAlex Elder }
3504fa95248SAlex Elder 
351fff89971SAlex Elder static bool ipa_endpoint_aggr_active(struct ipa_endpoint *endpoint)
352fff89971SAlex Elder {
3531d8f16dbSAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
354fff89971SAlex Elder 	struct ipa *ipa = endpoint->ipa;
3551d8f16dbSAlex Elder 	u32 unit = endpoint_id / 32;
3566a244b75SAlex Elder 	const struct ipa_reg *reg;
357fff89971SAlex Elder 	u32 val;
358fff89971SAlex Elder 
35988de7672SAlex Elder 	WARN_ON(!test_bit(endpoint_id, ipa->available));
3605bc55884SAlex Elder 
3616a244b75SAlex Elder 	reg = ipa_reg(ipa, STATE_AGGR_ACTIVE);
3621d8f16dbSAlex Elder 	val = ioread32(ipa->reg_virt + ipa_reg_n_offset(reg, unit));
363fff89971SAlex Elder 
36488de7672SAlex Elder 	return !!(val & BIT(endpoint_id % 32));
365fff89971SAlex Elder }
366fff89971SAlex Elder 
367fff89971SAlex Elder static void ipa_endpoint_force_close(struct ipa_endpoint *endpoint)
368fff89971SAlex Elder {
3691d8f16dbSAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
3701d8f16dbSAlex Elder 	u32 mask = BIT(endpoint_id % 32);
371fff89971SAlex Elder 	struct ipa *ipa = endpoint->ipa;
3721d8f16dbSAlex Elder 	u32 unit = endpoint_id / 32;
3736a244b75SAlex Elder 	const struct ipa_reg *reg;
374fff89971SAlex Elder 
37588de7672SAlex Elder 	WARN_ON(!test_bit(endpoint_id, ipa->available));
3765bc55884SAlex Elder 
3776a244b75SAlex Elder 	reg = ipa_reg(ipa, AGGR_FORCE_CLOSE);
3781d8f16dbSAlex Elder 	iowrite32(mask, ipa->reg_virt + ipa_reg_n_offset(reg, unit));
379fff89971SAlex Elder }
380fff89971SAlex Elder 
381fff89971SAlex Elder /**
382fff89971SAlex Elder  * ipa_endpoint_suspend_aggr() - Emulate suspend interrupt
383e3eea08eSAlex Elder  * @endpoint:	Endpoint on which to emulate a suspend
384fff89971SAlex Elder  *
385fff89971SAlex Elder  *  Emulate suspend IPA interrupt to unsuspend an endpoint suspended
386fff89971SAlex Elder  *  with an open aggregation frame.  This is to work around a hardware
387fff89971SAlex Elder  *  issue in IPA version 3.5.1 where the suspend interrupt will not be
388fff89971SAlex Elder  *  generated when it should be.
389fff89971SAlex Elder  */
390fff89971SAlex Elder static void ipa_endpoint_suspend_aggr(struct ipa_endpoint *endpoint)
391fff89971SAlex Elder {
392fff89971SAlex Elder 	struct ipa *ipa = endpoint->ipa;
393fff89971SAlex Elder 
394660e52d6SAlex Elder 	if (!endpoint->config.aggregation)
395fff89971SAlex Elder 		return;
396fff89971SAlex Elder 
397fff89971SAlex Elder 	/* Nothing to do if the endpoint doesn't have aggregation open */
398fff89971SAlex Elder 	if (!ipa_endpoint_aggr_active(endpoint))
399fff89971SAlex Elder 		return;
400fff89971SAlex Elder 
401fff89971SAlex Elder 	/* Force close aggregation */
402fff89971SAlex Elder 	ipa_endpoint_force_close(endpoint);
403fff89971SAlex Elder 
404fff89971SAlex Elder 	ipa_interrupt_simulate_suspend(ipa->interrupt);
405fff89971SAlex Elder }
406fff89971SAlex Elder 
407fff89971SAlex Elder /* Returns previous suspend state (true means suspend was enabled) */
4084fa95248SAlex Elder static bool
4094fa95248SAlex Elder ipa_endpoint_program_suspend(struct ipa_endpoint *endpoint, bool enable)
4104fa95248SAlex Elder {
411fff89971SAlex Elder 	bool suspended;
412fff89971SAlex Elder 
413d7f3087bSAlex Elder 	if (endpoint->ipa->version >= IPA_VERSION_4_0)
414b07f283eSAlex Elder 		return enable;	/* For IPA v4.0+, no change made */
415b07f283eSAlex Elder 
4165bc55884SAlex Elder 	WARN_ON(endpoint->toward_ipa);
4174fa95248SAlex Elder 
418fff89971SAlex Elder 	suspended = ipa_endpoint_init_ctrl(endpoint, enable);
419fff89971SAlex Elder 
420fff89971SAlex Elder 	/* A client suspended with an open aggregation frame will not
421fff89971SAlex Elder 	 * generate a SUSPEND IPA interrupt.  If enabling suspend, have
422fff89971SAlex Elder 	 * ipa_endpoint_suspend_aggr() handle this.
423fff89971SAlex Elder 	 */
424fff89971SAlex Elder 	if (enable && !suspended)
425fff89971SAlex Elder 		ipa_endpoint_suspend_aggr(endpoint);
426fff89971SAlex Elder 
427fff89971SAlex Elder 	return suspended;
4284fa95248SAlex Elder }
4294fa95248SAlex Elder 
4304c9d631aSAlex Elder /* Put all modem RX endpoints into suspend mode, and stop transmission
4314c9d631aSAlex Elder  * on all modem TX endpoints.  Prior to IPA v4.2, endpoint DELAY mode is
4324c9d631aSAlex Elder  * used for TX endpoints; starting with IPA v4.2 we use GSI channel flow
4334c9d631aSAlex Elder  * control instead.
4344c9d631aSAlex Elder  */
43584f9bd12SAlex Elder void ipa_endpoint_modem_pause_all(struct ipa *ipa, bool enable)
43684f9bd12SAlex Elder {
437e359ba89SAlex Elder 	u32 endpoint_id = 0;
43884f9bd12SAlex Elder 
439b7aaff0bSAlex Elder 	while (endpoint_id < ipa->endpoint_count) {
440e359ba89SAlex Elder 		struct ipa_endpoint *endpoint = &ipa->endpoint[endpoint_id++];
44184f9bd12SAlex Elder 
44284f9bd12SAlex Elder 		if (endpoint->ee_id != GSI_EE_MODEM)
44384f9bd12SAlex Elder 			continue;
44484f9bd12SAlex Elder 
4454c9d631aSAlex Elder 		if (!endpoint->toward_ipa)
4464c9d631aSAlex Elder 			(void)ipa_endpoint_program_suspend(endpoint, enable);
4474c9d631aSAlex Elder 		else if (ipa->version < IPA_VERSION_4_2)
4484fa95248SAlex Elder 			ipa_endpoint_program_delay(endpoint, enable);
449b07f283eSAlex Elder 		else
4504c9d631aSAlex Elder 			gsi_modem_channel_flow_control(&ipa->gsi,
4514c9d631aSAlex Elder 						       endpoint->channel_id,
4524c9d631aSAlex Elder 						       enable);
45384f9bd12SAlex Elder 	}
45484f9bd12SAlex Elder }
45584f9bd12SAlex Elder 
45684f9bd12SAlex Elder /* Reset all modem endpoints to use the default exception endpoint */
45784f9bd12SAlex Elder int ipa_endpoint_modem_exception_reset_all(struct ipa *ipa)
45884f9bd12SAlex Elder {
45984f9bd12SAlex Elder 	struct gsi_trans *trans;
4609a9f5129SAlex Elder 	u32 endpoint_id;
46184f9bd12SAlex Elder 	u32 count;
46284f9bd12SAlex Elder 
4632091c79aSAlex Elder 	/* We need one command per modem TX endpoint, plus the commands
4642091c79aSAlex Elder 	 * that clear the pipeline.
46584f9bd12SAlex Elder 	 */
4662091c79aSAlex Elder 	count = ipa->modem_tx_count + ipa_cmd_pipeline_clear_count();
46784f9bd12SAlex Elder 	trans = ipa_cmd_trans_alloc(ipa, count);
46884f9bd12SAlex Elder 	if (!trans) {
46984f9bd12SAlex Elder 		dev_err(&ipa->pdev->dev,
47084f9bd12SAlex Elder 			"no transaction to reset modem exception endpoints\n");
47184f9bd12SAlex Elder 		return -EBUSY;
47284f9bd12SAlex Elder 	}
47384f9bd12SAlex Elder 
4749a9f5129SAlex Elder 	for_each_set_bit(endpoint_id, ipa->defined, ipa->endpoint_count) {
47584f9bd12SAlex Elder 		struct ipa_endpoint *endpoint;
4766a244b75SAlex Elder 		const struct ipa_reg *reg;
47784f9bd12SAlex Elder 		u32 offset;
47884f9bd12SAlex Elder 
47984f9bd12SAlex Elder 		/* We only reset modem TX endpoints */
48084f9bd12SAlex Elder 		endpoint = &ipa->endpoint[endpoint_id];
48184f9bd12SAlex Elder 		if (!(endpoint->ee_id == GSI_EE_MODEM && endpoint->toward_ipa))
48284f9bd12SAlex Elder 			continue;
48384f9bd12SAlex Elder 
4846a244b75SAlex Elder 		reg = ipa_reg(ipa, ENDP_STATUS);
4856a244b75SAlex Elder 		offset = ipa_reg_n_offset(reg, endpoint_id);
48684f9bd12SAlex Elder 
48784f9bd12SAlex Elder 		/* Value written is 0, and all bits are updated.  That
48884f9bd12SAlex Elder 		 * means status is disabled on the endpoint, and as a
48984f9bd12SAlex Elder 		 * result all other fields in the register are ignored.
49084f9bd12SAlex Elder 		 */
49184f9bd12SAlex Elder 		ipa_cmd_register_write_add(trans, offset, 0, ~0, false);
49284f9bd12SAlex Elder 	}
49384f9bd12SAlex Elder 
494aa56e3e5SAlex Elder 	ipa_cmd_pipeline_clear_add(trans);
49584f9bd12SAlex Elder 
49684f9bd12SAlex Elder 	gsi_trans_commit_wait(trans);
49784f9bd12SAlex Elder 
49851c48ce2SAlex Elder 	ipa_cmd_pipeline_clear_wait(ipa);
49951c48ce2SAlex Elder 
50084f9bd12SAlex Elder 	return 0;
50184f9bd12SAlex Elder }
50284f9bd12SAlex Elder 
50384f9bd12SAlex Elder static void ipa_endpoint_init_cfg(struct ipa_endpoint *endpoint)
50484f9bd12SAlex Elder {
5056a244b75SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
5066bfb7538SAlex Elder 	struct ipa *ipa = endpoint->ipa;
5075567d4d9SAlex Elder 	enum ipa_cs_offload_en enabled;
5086a244b75SAlex Elder 	const struct ipa_reg *reg;
50984f9bd12SAlex Elder 	u32 val = 0;
5106bfb7538SAlex Elder 
5116a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_CFG);
51284f9bd12SAlex Elder 	/* FRAG_OFFLOAD_EN is 0 */
513660e52d6SAlex Elder 	if (endpoint->config.checksum) {
5146bfb7538SAlex Elder 		enum ipa_version version = ipa->version;
5155567d4d9SAlex Elder 
51684f9bd12SAlex Elder 		if (endpoint->toward_ipa) {
5179eefd2fbSAlex Elder 			u32 off;
51884f9bd12SAlex Elder 
51984f9bd12SAlex Elder 			/* Checksum header offset is in 4-byte units */
5204468a344SAlex Elder 			off = sizeof(struct rmnet_map_header) / sizeof(u32);
5214468a344SAlex Elder 			val |= ipa_reg_encode(reg, CS_METADATA_HDR_OFFSET, off);
5225567d4d9SAlex Elder 
5235567d4d9SAlex Elder 			enabled = version < IPA_VERSION_4_5
5245567d4d9SAlex Elder 					? IPA_CS_OFFLOAD_UL
5255567d4d9SAlex Elder 					: IPA_CS_OFFLOAD_INLINE;
52684f9bd12SAlex Elder 		} else {
5275567d4d9SAlex Elder 			enabled = version < IPA_VERSION_4_5
5285567d4d9SAlex Elder 					? IPA_CS_OFFLOAD_DL
5295567d4d9SAlex Elder 					: IPA_CS_OFFLOAD_INLINE;
53084f9bd12SAlex Elder 		}
53184f9bd12SAlex Elder 	} else {
5325567d4d9SAlex Elder 		enabled = IPA_CS_OFFLOAD_NONE;
53384f9bd12SAlex Elder 	}
5344468a344SAlex Elder 	val |= ipa_reg_encode(reg, CS_OFFLOAD_EN, enabled);
53584f9bd12SAlex Elder 	/* CS_GEN_QMB_MASTER_SEL is 0 */
53684f9bd12SAlex Elder 
5376a244b75SAlex Elder 	iowrite32(val, ipa->reg_virt + ipa_reg_n_offset(reg, endpoint_id));
53884f9bd12SAlex Elder }
53984f9bd12SAlex Elder 
540647a05f3SAlex Elder static void ipa_endpoint_init_nat(struct ipa_endpoint *endpoint)
541647a05f3SAlex Elder {
5426a244b75SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
5436bfb7538SAlex Elder 	struct ipa *ipa = endpoint->ipa;
5446a244b75SAlex Elder 	const struct ipa_reg *reg;
545647a05f3SAlex Elder 	u32 val;
546647a05f3SAlex Elder 
547647a05f3SAlex Elder 	if (!endpoint->toward_ipa)
548647a05f3SAlex Elder 		return;
549647a05f3SAlex Elder 
5506a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_NAT);
5514468a344SAlex Elder 	val = ipa_reg_encode(reg, NAT_EN, IPA_NAT_BYPASS);
552647a05f3SAlex Elder 
5536a244b75SAlex Elder 	iowrite32(val, ipa->reg_virt + ipa_reg_n_offset(reg, endpoint_id));
554647a05f3SAlex Elder }
555647a05f3SAlex Elder 
5565567d4d9SAlex Elder static u32
5575567d4d9SAlex Elder ipa_qmap_header_size(enum ipa_version version, struct ipa_endpoint *endpoint)
5585567d4d9SAlex Elder {
5595567d4d9SAlex Elder 	u32 header_size = sizeof(struct rmnet_map_header);
5605567d4d9SAlex Elder 
5615567d4d9SAlex Elder 	/* Without checksum offload, we just have the MAP header */
562660e52d6SAlex Elder 	if (!endpoint->config.checksum)
5635567d4d9SAlex Elder 		return header_size;
5645567d4d9SAlex Elder 
5655567d4d9SAlex Elder 	if (version < IPA_VERSION_4_5) {
5665567d4d9SAlex Elder 		/* Checksum header inserted for AP TX endpoints only */
5675567d4d9SAlex Elder 		if (endpoint->toward_ipa)
5685567d4d9SAlex Elder 			header_size += sizeof(struct rmnet_map_ul_csum_header);
5695567d4d9SAlex Elder 	} else {
5705567d4d9SAlex Elder 		/* Checksum header is used in both directions */
5715567d4d9SAlex Elder 		header_size += sizeof(struct rmnet_map_v5_csum_header);
5725567d4d9SAlex Elder 	}
5735567d4d9SAlex Elder 
5745567d4d9SAlex Elder 	return header_size;
5755567d4d9SAlex Elder }
5765567d4d9SAlex Elder 
5774468a344SAlex Elder /* Encoded value for ENDP_INIT_HDR register HDR_LEN* field(s) */
5784468a344SAlex Elder static u32 ipa_header_size_encode(enum ipa_version version,
5794468a344SAlex Elder 				  const struct ipa_reg *reg, u32 header_size)
5804468a344SAlex Elder {
5814468a344SAlex Elder 	u32 field_max = ipa_reg_field_max(reg, HDR_LEN);
5824468a344SAlex Elder 	u32 val;
5834468a344SAlex Elder 
5844468a344SAlex Elder 	/* We know field_max can be used as a mask (2^n - 1) */
5854468a344SAlex Elder 	val = ipa_reg_encode(reg, HDR_LEN, header_size & field_max);
5864468a344SAlex Elder 	if (version < IPA_VERSION_4_5) {
5874468a344SAlex Elder 		WARN_ON(header_size > field_max);
5884468a344SAlex Elder 		return val;
5894468a344SAlex Elder 	}
5904468a344SAlex Elder 
5914468a344SAlex Elder 	/* IPA v4.5 adds a few more most-significant bits */
5924468a344SAlex Elder 	header_size >>= hweight32(field_max);
5934468a344SAlex Elder 	WARN_ON(header_size > ipa_reg_field_max(reg, HDR_LEN_MSB));
5944468a344SAlex Elder 	val |= ipa_reg_encode(reg, HDR_LEN_MSB, header_size);
5954468a344SAlex Elder 
5964468a344SAlex Elder 	return val;
5974468a344SAlex Elder }
5984468a344SAlex Elder 
5994468a344SAlex Elder /* Encoded value for ENDP_INIT_HDR register OFST_METADATA* field(s) */
6004468a344SAlex Elder static u32 ipa_metadata_offset_encode(enum ipa_version version,
6014468a344SAlex Elder 				      const struct ipa_reg *reg, u32 offset)
6024468a344SAlex Elder {
6034468a344SAlex Elder 	u32 field_max = ipa_reg_field_max(reg, HDR_OFST_METADATA);
6044468a344SAlex Elder 	u32 val;
6054468a344SAlex Elder 
6064468a344SAlex Elder 	/* We know field_max can be used as a mask (2^n - 1) */
6074468a344SAlex Elder 	val = ipa_reg_encode(reg, HDR_OFST_METADATA, offset);
6084468a344SAlex Elder 	if (version < IPA_VERSION_4_5) {
6094468a344SAlex Elder 		WARN_ON(offset > field_max);
6104468a344SAlex Elder 		return val;
6114468a344SAlex Elder 	}
6124468a344SAlex Elder 
6134468a344SAlex Elder 	/* IPA v4.5 adds a few more most-significant bits */
6144468a344SAlex Elder 	offset >>= hweight32(field_max);
6154468a344SAlex Elder 	WARN_ON(offset > ipa_reg_field_max(reg, HDR_OFST_METADATA_MSB));
6164468a344SAlex Elder 	val |= ipa_reg_encode(reg, HDR_OFST_METADATA_MSB, offset);
6174468a344SAlex Elder 
6184468a344SAlex Elder 	return val;
6194468a344SAlex Elder }
6204468a344SAlex Elder 
6218730f45dSAlex Elder /**
622e3eea08eSAlex Elder  * ipa_endpoint_init_hdr() - Initialize HDR endpoint configuration register
623e3eea08eSAlex Elder  * @endpoint:	Endpoint pointer
624e3eea08eSAlex Elder  *
6258730f45dSAlex Elder  * We program QMAP endpoints so each packet received is preceded by a QMAP
6268730f45dSAlex Elder  * header structure.  The QMAP header contains a 1-byte mux_id and 2-byte
6278730f45dSAlex Elder  * packet size field, and we have the IPA hardware populate both for each
6288730f45dSAlex Elder  * received packet.  The header is configured (in the HDR_EXT register)
6298730f45dSAlex Elder  * to use big endian format.
6308730f45dSAlex Elder  *
6318730f45dSAlex Elder  * The packet size is written into the QMAP header's pkt_len field.  That
6328730f45dSAlex Elder  * location is defined here using the HDR_OFST_PKT_SIZE field.
6338730f45dSAlex Elder  *
6348730f45dSAlex Elder  * The mux_id comes from a 4-byte metadata value supplied with each packet
6358730f45dSAlex Elder  * by the modem.  It is *not* a QMAP header, but it does contain the mux_id
6368730f45dSAlex Elder  * value that we want, in its low-order byte.  A bitmask defined in the
6378730f45dSAlex Elder  * endpoint's METADATA_MASK register defines which byte within the modem
6388730f45dSAlex Elder  * metadata contains the mux_id.  And the OFST_METADATA field programmed
6398730f45dSAlex Elder  * here indicates where the extracted byte should be placed within the QMAP
6408730f45dSAlex Elder  * header.
6418730f45dSAlex Elder  */
64284f9bd12SAlex Elder static void ipa_endpoint_init_hdr(struct ipa_endpoint *endpoint)
64384f9bd12SAlex Elder {
6446a244b75SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
6451af15c2aSAlex Elder 	struct ipa *ipa = endpoint->ipa;
6466a244b75SAlex Elder 	const struct ipa_reg *reg;
64784f9bd12SAlex Elder 	u32 val = 0;
6486bfb7538SAlex Elder 
6496a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_HDR);
650660e52d6SAlex Elder 	if (endpoint->config.qmap) {
6511af15c2aSAlex Elder 		enum ipa_version version = ipa->version;
6525567d4d9SAlex Elder 		size_t header_size;
65384f9bd12SAlex Elder 
6545567d4d9SAlex Elder 		header_size = ipa_qmap_header_size(version, endpoint);
6554468a344SAlex Elder 		val = ipa_header_size_encode(version, reg, header_size);
65684f9bd12SAlex Elder 
657f330fda3SAlex Elder 		/* Define how to fill fields in a received QMAP header */
6588730f45dSAlex Elder 		if (!endpoint->toward_ipa) {
6599eefd2fbSAlex Elder 			u32 off;     /* Field offset within header */
6608730f45dSAlex Elder 
6618730f45dSAlex Elder 			/* Where IPA will write the metadata value */
6629eefd2fbSAlex Elder 			off = offsetof(struct rmnet_map_header, mux_id);
6634468a344SAlex Elder 			val |= ipa_metadata_offset_encode(version, reg, off);
6648730f45dSAlex Elder 
6658730f45dSAlex Elder 			/* Where IPA will write the length */
6669eefd2fbSAlex Elder 			off = offsetof(struct rmnet_map_header, pkt_len);
6671af15c2aSAlex Elder 			/* Upper bits are stored in HDR_EXT with IPA v4.5 */
668d7f3087bSAlex Elder 			if (version >= IPA_VERSION_4_5)
6694468a344SAlex Elder 				off &= ipa_reg_field_max(reg, HDR_OFST_PKT_SIZE);
6701af15c2aSAlex Elder 
6714468a344SAlex Elder 			val |= ipa_reg_bit(reg, HDR_OFST_PKT_SIZE_VALID);
6724468a344SAlex Elder 			val |= ipa_reg_encode(reg, HDR_OFST_PKT_SIZE, off);
67384f9bd12SAlex Elder 		}
6748730f45dSAlex Elder 		/* For QMAP TX, metadata offset is 0 (modem assumes this) */
6754468a344SAlex Elder 		val |= ipa_reg_bit(reg, HDR_OFST_METADATA_VALID);
6768730f45dSAlex Elder 
6778730f45dSAlex Elder 		/* HDR_ADDITIONAL_CONST_LEN is 0; (RX only) */
67884f9bd12SAlex Elder 		/* HDR_A5_MUX is 0 */
67984f9bd12SAlex Elder 		/* HDR_LEN_INC_DEAGG_HDR is 0 */
6808bfc4e21SAlex Elder 		/* HDR_METADATA_REG_VALID is 0 (TX only, version < v4.5) */
68184f9bd12SAlex Elder 	}
68284f9bd12SAlex Elder 
6836a244b75SAlex Elder 	iowrite32(val, ipa->reg_virt + ipa_reg_n_offset(reg, endpoint_id));
68484f9bd12SAlex Elder }
68584f9bd12SAlex Elder 
68684f9bd12SAlex Elder static void ipa_endpoint_init_hdr_ext(struct ipa_endpoint *endpoint)
68784f9bd12SAlex Elder {
688660e52d6SAlex Elder 	u32 pad_align = endpoint->config.rx.pad_align;
6896a244b75SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
6901af15c2aSAlex Elder 	struct ipa *ipa = endpoint->ipa;
6916a244b75SAlex Elder 	const struct ipa_reg *reg;
69284f9bd12SAlex Elder 	u32 val = 0;
6936bfb7538SAlex Elder 
6946a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_HDR_EXT);
695660e52d6SAlex Elder 	if (endpoint->config.qmap) {
696332ef7c8SAlex Elder 		/* We have a header, so we must specify its endianness */
6974468a344SAlex Elder 		val |= ipa_reg_bit(reg, HDR_ENDIANNESS);	/* big endian */
698f330fda3SAlex Elder 
699332ef7c8SAlex Elder 		/* A QMAP header contains a 6 bit pad field at offset 0.
700332ef7c8SAlex Elder 		 * The RMNet driver assumes this field is meaningful in
701332ef7c8SAlex Elder 		 * packets it receives, and assumes the header's payload
702332ef7c8SAlex Elder 		 * length includes that padding.  The RMNet driver does
703332ef7c8SAlex Elder 		 * *not* pad packets it sends, however, so the pad field
704332ef7c8SAlex Elder 		 * (although 0) should be ignored.
705f330fda3SAlex Elder 		 */
706332ef7c8SAlex Elder 		if (!endpoint->toward_ipa) {
7074468a344SAlex Elder 			val |= ipa_reg_bit(reg, HDR_TOTAL_LEN_OR_PAD_VALID);
70884f9bd12SAlex Elder 			/* HDR_TOTAL_LEN_OR_PAD is 0 (pad, not total_len) */
7094468a344SAlex Elder 			val |= ipa_reg_bit(reg, HDR_PAYLOAD_LEN_INC_PADDING);
71084f9bd12SAlex Elder 			/* HDR_TOTAL_LEN_OR_PAD_OFFSET is 0 */
711f330fda3SAlex Elder 		}
712332ef7c8SAlex Elder 	}
713f330fda3SAlex Elder 
714f330fda3SAlex Elder 	/* HDR_PAYLOAD_LEN_INC_PADDING is 0 */
71584f9bd12SAlex Elder 	if (!endpoint->toward_ipa)
7164468a344SAlex Elder 		val |= ipa_reg_encode(reg, HDR_PAD_TO_ALIGNMENT, pad_align);
71784f9bd12SAlex Elder 
7181af15c2aSAlex Elder 	/* IPA v4.5 adds some most-significant bits to a few fields,
7191af15c2aSAlex Elder 	 * two of which are defined in the HDR (not HDR_EXT) register.
7201af15c2aSAlex Elder 	 */
721d7f3087bSAlex Elder 	if (ipa->version >= IPA_VERSION_4_5) {
7221af15c2aSAlex Elder 		/* HDR_TOTAL_LEN_OR_PAD_OFFSET is 0, so MSB is 0 */
723660e52d6SAlex Elder 		if (endpoint->config.qmap && !endpoint->toward_ipa) {
7244468a344SAlex Elder 			u32 mask = ipa_reg_field_max(reg, HDR_OFST_PKT_SIZE);
7256bfb7538SAlex Elder 			u32 off;     /* Field offset within header */
72684f9bd12SAlex Elder 
7279eefd2fbSAlex Elder 			off = offsetof(struct rmnet_map_header, pkt_len);
7284468a344SAlex Elder 			/* Low bits are in the ENDP_INIT_HDR register */
7294468a344SAlex Elder 			off >>= hweight32(mask);
7304468a344SAlex Elder 			val |= ipa_reg_encode(reg, HDR_OFST_PKT_SIZE_MSB, off);
7311af15c2aSAlex Elder 			/* HDR_ADDITIONAL_CONST_LEN is 0 so MSB is 0 */
7321af15c2aSAlex Elder 		}
7331af15c2aSAlex Elder 	}
7346bfb7538SAlex Elder 
7356a244b75SAlex Elder 	iowrite32(val, ipa->reg_virt + ipa_reg_n_offset(reg, endpoint_id));
7361af15c2aSAlex Elder }
73784f9bd12SAlex Elder 
73884f9bd12SAlex Elder static void ipa_endpoint_init_hdr_metadata_mask(struct ipa_endpoint *endpoint)
73984f9bd12SAlex Elder {
74084f9bd12SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
7416bfb7538SAlex Elder 	struct ipa *ipa = endpoint->ipa;
7426a244b75SAlex Elder 	const struct ipa_reg *reg;
74384f9bd12SAlex Elder 	u32 val = 0;
74484f9bd12SAlex Elder 	u32 offset;
74584f9bd12SAlex Elder 
746fb57c3eaSAlex Elder 	if (endpoint->toward_ipa)
747fb57c3eaSAlex Elder 		return;		/* Register not valid for TX endpoints */
748fb57c3eaSAlex Elder 
7496a244b75SAlex Elder 	reg = ipa_reg(ipa,  ENDP_INIT_HDR_METADATA_MASK);
7506a244b75SAlex Elder 	offset = ipa_reg_n_offset(reg, endpoint_id);
75184f9bd12SAlex Elder 
7528730f45dSAlex Elder 	/* Note that HDR_ENDIANNESS indicates big endian header fields */
753660e52d6SAlex Elder 	if (endpoint->config.qmap)
754088f8a23SAlex Elder 		val = (__force u32)cpu_to_be32(IPA_ENDPOINT_QMAP_METADATA_MASK);
75584f9bd12SAlex Elder 
7566bfb7538SAlex Elder 	iowrite32(val, ipa->reg_virt + offset);
75784f9bd12SAlex Elder }
75884f9bd12SAlex Elder 
75984f9bd12SAlex Elder static void ipa_endpoint_init_mode(struct ipa_endpoint *endpoint)
76084f9bd12SAlex Elder {
7616bfb7538SAlex Elder 	struct ipa *ipa = endpoint->ipa;
7626a244b75SAlex Elder 	const struct ipa_reg *reg;
7636bfb7538SAlex Elder 	u32 offset;
76484f9bd12SAlex Elder 	u32 val;
76584f9bd12SAlex Elder 
766fb57c3eaSAlex Elder 	if (!endpoint->toward_ipa)
767fb57c3eaSAlex Elder 		return;		/* Register not valid for RX endpoints */
768fb57c3eaSAlex Elder 
7696a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_MODE);
770660e52d6SAlex Elder 	if (endpoint->config.dma_mode) {
771660e52d6SAlex Elder 		enum ipa_endpoint_name name = endpoint->config.dma_endpoint;
772216b409dSAlex Elder 		u32 dma_endpoint_id = ipa->name_map[name]->endpoint_id;
77384f9bd12SAlex Elder 
774216b409dSAlex Elder 		val = ipa_reg_encode(reg, ENDP_MODE, IPA_DMA);
775216b409dSAlex Elder 		val |= ipa_reg_encode(reg, DEST_PIPE_INDEX, dma_endpoint_id);
77684f9bd12SAlex Elder 	} else {
777216b409dSAlex Elder 		val = ipa_reg_encode(reg, ENDP_MODE, IPA_BASIC);
77884f9bd12SAlex Elder 	}
77900b9102aSAlex Elder 	/* All other bits unspecified (and 0) */
78084f9bd12SAlex Elder 
781216b409dSAlex Elder 	offset = ipa_reg_n_offset(reg, endpoint->endpoint_id);
7826bfb7538SAlex Elder 	iowrite32(val, ipa->reg_virt + offset);
78384f9bd12SAlex Elder }
78484f9bd12SAlex Elder 
7858be440e1SAlex Elder /* For IPA v4.5+, times are expressed using Qtime.  The AP uses one of two
7868be440e1SAlex Elder  * pulse generators (0 and 1) to measure elapsed time.  In ipa_qtime_config()
7878be440e1SAlex Elder  * they're configured to have granularity 100 usec and 1 msec, respectively.
7888be440e1SAlex Elder  *
7898be440e1SAlex Elder  * The return value is the positive or negative Qtime value to use to
7908be440e1SAlex Elder  * express the (microsecond) time provided.  A positive return value
7918be440e1SAlex Elder  * means pulse generator 0 can be used; otherwise use pulse generator 1.
7928be440e1SAlex Elder  */
7938be440e1SAlex Elder static int ipa_qtime_val(u32 microseconds, u32 max)
7948be440e1SAlex Elder {
7958be440e1SAlex Elder 	u32 val;
7968be440e1SAlex Elder 
7978be440e1SAlex Elder 	/* Use 100 microsecond granularity if possible */
7988be440e1SAlex Elder 	val = DIV_ROUND_CLOSEST(microseconds, 100);
7998be440e1SAlex Elder 	if (val <= max)
8008be440e1SAlex Elder 		return (int)val;
8018be440e1SAlex Elder 
8028be440e1SAlex Elder 	/* Have to use pulse generator 1 (millisecond granularity) */
8038be440e1SAlex Elder 	val = DIV_ROUND_CLOSEST(microseconds, 1000);
8048be440e1SAlex Elder 	WARN_ON(val > max);
8058be440e1SAlex Elder 
8068be440e1SAlex Elder 	return (int)-val;
8078be440e1SAlex Elder }
8088be440e1SAlex Elder 
80919547041SAlex Elder /* Encode the aggregation timer limit (microseconds) based on IPA version */
810216b409dSAlex Elder static u32 aggr_time_limit_encode(struct ipa *ipa, const struct ipa_reg *reg,
811216b409dSAlex Elder 				  u32 microseconds)
8126bf754c7SAlex Elder {
813216b409dSAlex Elder 	u32 max;
81419547041SAlex Elder 	u32 val;
81548395fa8SAlex Elder 
81648395fa8SAlex Elder 	if (!microseconds)
81748395fa8SAlex Elder 		return 0;	/* Nothing to compute if time limit is 0 */
81848395fa8SAlex Elder 
819216b409dSAlex Elder 	max = ipa_reg_field_max(reg, TIME_LIMIT);
820216b409dSAlex Elder 	if (ipa->version >= IPA_VERSION_4_5) {
82148395fa8SAlex Elder 		u32 gran_sel;
8228be440e1SAlex Elder 		int ret;
8236bf754c7SAlex Elder 
8248be440e1SAlex Elder 		/* Compute the Qtime limit value to use */
825216b409dSAlex Elder 		ret = ipa_qtime_val(microseconds, max);
8268be440e1SAlex Elder 		if (ret < 0) {
8278be440e1SAlex Elder 			val = -ret;
828216b409dSAlex Elder 			gran_sel = ipa_reg_bit(reg, AGGR_GRAN_SEL);
82919547041SAlex Elder 		} else {
8308be440e1SAlex Elder 			val = ret;
83119547041SAlex Elder 			gran_sel = 0;
83219547041SAlex Elder 		}
83319547041SAlex Elder 
834216b409dSAlex Elder 		return gran_sel | ipa_reg_encode(reg, TIME_LIMIT, val);
8356bf754c7SAlex Elder 	}
8366bf754c7SAlex Elder 
837216b409dSAlex Elder 	/* We program aggregation granularity in ipa_hardware_config() */
83848395fa8SAlex Elder 	val = DIV_ROUND_CLOSEST(microseconds, IPA_AGGR_GRANULARITY);
839216b409dSAlex Elder 	WARN(val > max, "aggr_time_limit too large (%u > %u usec)\n",
840216b409dSAlex Elder 	     microseconds, max * IPA_AGGR_GRANULARITY);
84148395fa8SAlex Elder 
842216b409dSAlex Elder 	return ipa_reg_encode(reg, TIME_LIMIT, val);
8436bf754c7SAlex Elder }
8446bf754c7SAlex Elder 
84584f9bd12SAlex Elder static void ipa_endpoint_init_aggr(struct ipa_endpoint *endpoint)
84684f9bd12SAlex Elder {
8476a244b75SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
8486bfb7538SAlex Elder 	struct ipa *ipa = endpoint->ipa;
8496a244b75SAlex Elder 	const struct ipa_reg *reg;
85084f9bd12SAlex Elder 	u32 val = 0;
8516bfb7538SAlex Elder 
8526a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_AGGR);
853660e52d6SAlex Elder 	if (endpoint->config.aggregation) {
85484f9bd12SAlex Elder 		if (!endpoint->toward_ipa) {
855cf4e73a1SAlex Elder 			const struct ipa_endpoint_rx *rx_config;
856c5794097SAlex Elder 			u32 buffer_size;
85784f9bd12SAlex Elder 			u32 limit;
85884f9bd12SAlex Elder 
859660e52d6SAlex Elder 			rx_config = &endpoint->config.rx;
860216b409dSAlex Elder 			val |= ipa_reg_encode(reg, AGGR_EN, IPA_ENABLE_AGGR);
861216b409dSAlex Elder 			val |= ipa_reg_encode(reg, AGGR_TYPE, IPA_GENERIC);
8629e88cb5fSAlex Elder 
863cf4e73a1SAlex Elder 			buffer_size = rx_config->buffer_size;
8643cebb7c2SAlex Elder 			limit = ipa_aggr_size_kb(buffer_size - NET_SKB_PAD,
8653cebb7c2SAlex Elder 						 rx_config->aggr_hard_limit);
866216b409dSAlex Elder 			val |= ipa_reg_encode(reg, BYTE_LIMIT, limit);
8671d86652bSAlex Elder 
868beb90cbaSAlex Elder 			limit = rx_config->aggr_time_limit;
869216b409dSAlex Elder 			val |= aggr_time_limit_encode(ipa, reg, limit);
8701d86652bSAlex Elder 
8719e88cb5fSAlex Elder 			/* AGGR_PKT_LIMIT is 0 (unlimited) */
8729e88cb5fSAlex Elder 
873216b409dSAlex Elder 			if (rx_config->aggr_close_eof)
874216b409dSAlex Elder 				val |= ipa_reg_bit(reg, SW_EOF_ACTIVE);
87584f9bd12SAlex Elder 		} else {
876216b409dSAlex Elder 			val |= ipa_reg_encode(reg, AGGR_EN, IPA_ENABLE_DEAGGR);
877216b409dSAlex Elder 			val |= ipa_reg_encode(reg, AGGR_TYPE, IPA_QCMAP);
87884f9bd12SAlex Elder 			/* other fields ignored */
87984f9bd12SAlex Elder 		}
88084f9bd12SAlex Elder 		/* AGGR_FORCE_CLOSE is 0 */
8818bfc4e21SAlex Elder 		/* AGGR_GRAN_SEL is 0 for IPA v4.5 */
88284f9bd12SAlex Elder 	} else {
883216b409dSAlex Elder 		val |= ipa_reg_encode(reg, AGGR_EN, IPA_BYPASS_AGGR);
88484f9bd12SAlex Elder 		/* other fields ignored */
88584f9bd12SAlex Elder 	}
88684f9bd12SAlex Elder 
8876a244b75SAlex Elder 	iowrite32(val, ipa->reg_virt + ipa_reg_n_offset(reg, endpoint_id));
88884f9bd12SAlex Elder }
88984f9bd12SAlex Elder 
89063e5afc8SAlex Elder /* The head-of-line blocking timer is defined as a tick count.  For
89163e5afc8SAlex Elder  * IPA version 4.5 the tick count is based on the Qtimer, which is
89263e5afc8SAlex Elder  * derived from the 19.2 MHz SoC XO clock.  For older IPA versions
89363e5afc8SAlex Elder  * each tick represents 128 cycles of the IPA core clock.
89463e5afc8SAlex Elder  *
8958be440e1SAlex Elder  * Return the encoded value representing the timeout period provided
8968be440e1SAlex Elder  * that should be written to the ENDP_INIT_HOL_BLOCK_TIMER register.
89763e5afc8SAlex Elder  */
898216b409dSAlex Elder static u32 hol_block_timer_encode(struct ipa *ipa, const struct ipa_reg *reg,
899216b409dSAlex Elder 				  u32 microseconds)
90084f9bd12SAlex Elder {
901f13a8c31SAlex Elder 	u32 width;
90284f9bd12SAlex Elder 	u32 scale;
903f13a8c31SAlex Elder 	u64 ticks;
904f13a8c31SAlex Elder 	u64 rate;
905f13a8c31SAlex Elder 	u32 high;
90684f9bd12SAlex Elder 	u32 val;
90784f9bd12SAlex Elder 
90884f9bd12SAlex Elder 	if (!microseconds)
909f13a8c31SAlex Elder 		return 0;	/* Nothing to compute if timer period is 0 */
91084f9bd12SAlex Elder 
91148395fa8SAlex Elder 	if (ipa->version >= IPA_VERSION_4_5) {
912216b409dSAlex Elder 		u32 max = ipa_reg_field_max(reg, TIMER_LIMIT);
91348395fa8SAlex Elder 		u32 gran_sel;
91448395fa8SAlex Elder 		int ret;
91548395fa8SAlex Elder 
91648395fa8SAlex Elder 		/* Compute the Qtime limit value to use */
917216b409dSAlex Elder 		ret = ipa_qtime_val(microseconds, max);
91848395fa8SAlex Elder 		if (ret < 0) {
91948395fa8SAlex Elder 			val = -ret;
920216b409dSAlex Elder 			gran_sel = ipa_reg_bit(reg, TIMER_GRAN_SEL);
92148395fa8SAlex Elder 		} else {
92248395fa8SAlex Elder 			val = ret;
92348395fa8SAlex Elder 			gran_sel = 0;
92448395fa8SAlex Elder 		}
92548395fa8SAlex Elder 
926216b409dSAlex Elder 		return gran_sel | ipa_reg_encode(reg, TIMER_LIMIT, val);
92748395fa8SAlex Elder 	}
92863e5afc8SAlex Elder 
929216b409dSAlex Elder 	/* Use 64 bit arithmetic to avoid overflow */
9307aa0e8b8SAlex Elder 	rate = ipa_core_clock_rate(ipa);
931f13a8c31SAlex Elder 	ticks = DIV_ROUND_CLOSEST(microseconds * rate, 128 * USEC_PER_SEC);
932216b409dSAlex Elder 
933216b409dSAlex Elder 	/* We still need the result to fit into the field */
934216b409dSAlex Elder 	WARN_ON(ticks > ipa_reg_field_max(reg, TIMER_BASE_VALUE));
93584f9bd12SAlex Elder 
9366833a096SAlex Elder 	/* IPA v3.5.1 through v4.1 just record the tick count */
9376833a096SAlex Elder 	if (ipa->version < IPA_VERSION_4_2)
938216b409dSAlex Elder 		return ipa_reg_encode(reg, TIMER_BASE_VALUE, (u32)ticks);
93984f9bd12SAlex Elder 
940f13a8c31SAlex Elder 	/* For IPA v4.2, the tick count is represented by base and
941f13a8c31SAlex Elder 	 * scale fields within the 32-bit timer register, where:
942f13a8c31SAlex Elder 	 *     ticks = base << scale;
943f13a8c31SAlex Elder 	 * The best precision is achieved when the base value is as
944f13a8c31SAlex Elder 	 * large as possible.  Find the highest set bit in the tick
945f13a8c31SAlex Elder 	 * count, and extract the number of bits in the base field
946497abc87SPeng Li 	 * such that high bit is included.
947f13a8c31SAlex Elder 	 */
948216b409dSAlex Elder 	high = fls(ticks);		/* 1..32 (or warning above) */
949216b409dSAlex Elder 	width = hweight32(ipa_reg_fmask(reg, TIMER_BASE_VALUE));
950f13a8c31SAlex Elder 	scale = high > width ? high - width : 0;
951f13a8c31SAlex Elder 	if (scale) {
952f13a8c31SAlex Elder 		/* If we're scaling, round up to get a closer result */
953f13a8c31SAlex Elder 		ticks += 1 << (scale - 1);
954f13a8c31SAlex Elder 		/* High bit was set, so rounding might have affected it */
955f13a8c31SAlex Elder 		if (fls(ticks) != high)
956f13a8c31SAlex Elder 			scale++;
957f13a8c31SAlex Elder 	}
95884f9bd12SAlex Elder 
959216b409dSAlex Elder 	val = ipa_reg_encode(reg, TIMER_SCALE, scale);
960216b409dSAlex Elder 	val |= ipa_reg_encode(reg, TIMER_BASE_VALUE, (u32)ticks >> scale);
96184f9bd12SAlex Elder 
96284f9bd12SAlex Elder 	return val;
96384f9bd12SAlex Elder }
96484f9bd12SAlex Elder 
965f13a8c31SAlex Elder /* If microseconds is 0, timeout is immediate */
966f13a8c31SAlex Elder static void ipa_endpoint_init_hol_block_timer(struct ipa_endpoint *endpoint,
96784f9bd12SAlex Elder 					      u32 microseconds)
96884f9bd12SAlex Elder {
96984f9bd12SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
97084f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
9716a244b75SAlex Elder 	const struct ipa_reg *reg;
97284f9bd12SAlex Elder 	u32 val;
97384f9bd12SAlex Elder 
974816316caSAlex Elder 	/* This should only be changed when HOL_BLOCK_EN is disabled */
9756a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_HOL_BLOCK_TIMER);
976216b409dSAlex Elder 	val = hol_block_timer_encode(ipa, reg, microseconds);
9776bfb7538SAlex Elder 
9786a244b75SAlex Elder 	iowrite32(val, ipa->reg_virt + ipa_reg_n_offset(reg, endpoint_id));
97984f9bd12SAlex Elder }
98084f9bd12SAlex Elder 
98184f9bd12SAlex Elder static void
982e6aab6b9SAlex Elder ipa_endpoint_init_hol_block_en(struct ipa_endpoint *endpoint, bool enable)
98384f9bd12SAlex Elder {
98484f9bd12SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
9856bfb7538SAlex Elder 	struct ipa *ipa = endpoint->ipa;
9866a244b75SAlex Elder 	const struct ipa_reg *reg;
98784f9bd12SAlex Elder 	u32 offset;
98884f9bd12SAlex Elder 	u32 val;
98984f9bd12SAlex Elder 
9906a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_HOL_BLOCK_EN);
9916a244b75SAlex Elder 	offset = ipa_reg_n_offset(reg, endpoint_id);
992216b409dSAlex Elder 	val = enable ? ipa_reg_bit(reg, HOL_BLOCK_EN) : 0;
9936bfb7538SAlex Elder 
9946bfb7538SAlex Elder 	iowrite32(val, ipa->reg_virt + offset);
9956bfb7538SAlex Elder 
9966e228d8cSAlex Elder 	/* When enabling, the register must be written twice for IPA v4.5+ */
9976bfb7538SAlex Elder 	if (enable && ipa->version >= IPA_VERSION_4_5)
9986bfb7538SAlex Elder 		iowrite32(val, ipa->reg_virt + offset);
99984f9bd12SAlex Elder }
100084f9bd12SAlex Elder 
1001e6aab6b9SAlex Elder /* Assumes HOL_BLOCK is in disabled state */
1002e6aab6b9SAlex Elder static void ipa_endpoint_init_hol_block_enable(struct ipa_endpoint *endpoint,
1003e6aab6b9SAlex Elder 					       u32 microseconds)
1004e6aab6b9SAlex Elder {
1005e6aab6b9SAlex Elder 	ipa_endpoint_init_hol_block_timer(endpoint, microseconds);
1006e6aab6b9SAlex Elder 	ipa_endpoint_init_hol_block_en(endpoint, true);
1007e6aab6b9SAlex Elder }
1008e6aab6b9SAlex Elder 
1009e6aab6b9SAlex Elder static void ipa_endpoint_init_hol_block_disable(struct ipa_endpoint *endpoint)
1010e6aab6b9SAlex Elder {
1011e6aab6b9SAlex Elder 	ipa_endpoint_init_hol_block_en(endpoint, false);
1012e6aab6b9SAlex Elder }
1013e6aab6b9SAlex Elder 
101484f9bd12SAlex Elder void ipa_endpoint_modem_hol_block_clear_all(struct ipa *ipa)
101584f9bd12SAlex Elder {
1016e359ba89SAlex Elder 	u32 endpoint_id = 0;
101784f9bd12SAlex Elder 
1018b7aaff0bSAlex Elder 	while (endpoint_id < ipa->endpoint_count) {
1019e359ba89SAlex Elder 		struct ipa_endpoint *endpoint = &ipa->endpoint[endpoint_id++];
102084f9bd12SAlex Elder 
1021f8d34dfdSAlex Elder 		if (endpoint->toward_ipa || endpoint->ee_id != GSI_EE_MODEM)
102284f9bd12SAlex Elder 			continue;
102384f9bd12SAlex Elder 
1024e6aab6b9SAlex Elder 		ipa_endpoint_init_hol_block_disable(endpoint);
1025e6aab6b9SAlex Elder 		ipa_endpoint_init_hol_block_enable(endpoint, 0);
102684f9bd12SAlex Elder 	}
102784f9bd12SAlex Elder }
102884f9bd12SAlex Elder 
102984f9bd12SAlex Elder static void ipa_endpoint_init_deaggr(struct ipa_endpoint *endpoint)
103084f9bd12SAlex Elder {
10316a244b75SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
10326bfb7538SAlex Elder 	struct ipa *ipa = endpoint->ipa;
10336a244b75SAlex Elder 	const struct ipa_reg *reg;
103484f9bd12SAlex Elder 	u32 val = 0;
103584f9bd12SAlex Elder 
1036fb57c3eaSAlex Elder 	if (!endpoint->toward_ipa)
1037fb57c3eaSAlex Elder 		return;		/* Register not valid for RX endpoints */
1038fb57c3eaSAlex Elder 
10396a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_DEAGGR);
104084f9bd12SAlex Elder 	/* DEAGGR_HDR_LEN is 0 */
104184f9bd12SAlex Elder 	/* PACKET_OFFSET_VALID is 0 */
104284f9bd12SAlex Elder 	/* PACKET_OFFSET_LOCATION is ignored (not valid) */
104384f9bd12SAlex Elder 	/* MAX_PACKET_LEN is 0 (not enforced) */
104484f9bd12SAlex Elder 
10456a244b75SAlex Elder 	iowrite32(val, ipa->reg_virt + ipa_reg_n_offset(reg, endpoint_id));
104684f9bd12SAlex Elder }
104784f9bd12SAlex Elder 
10482d265342SAlex Elder static void ipa_endpoint_init_rsrc_grp(struct ipa_endpoint *endpoint)
10492d265342SAlex Elder {
1050181ca020SAlex Elder 	u32 resource_group = endpoint->config.resource_group;
10516a244b75SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
10522d265342SAlex Elder 	struct ipa *ipa = endpoint->ipa;
10536a244b75SAlex Elder 	const struct ipa_reg *reg;
10542d265342SAlex Elder 	u32 val;
10552d265342SAlex Elder 
10566a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_RSRC_GRP);
1057181ca020SAlex Elder 	val = ipa_reg_encode(reg, ENDP_RSRC_GRP, resource_group);
10586bfb7538SAlex Elder 
10596a244b75SAlex Elder 	iowrite32(val, ipa->reg_virt + ipa_reg_n_offset(reg, endpoint_id));
10602d265342SAlex Elder }
10612d265342SAlex Elder 
106284f9bd12SAlex Elder static void ipa_endpoint_init_seq(struct ipa_endpoint *endpoint)
106384f9bd12SAlex Elder {
10646a244b75SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
10656bfb7538SAlex Elder 	struct ipa *ipa = endpoint->ipa;
10666a244b75SAlex Elder 	const struct ipa_reg *reg;
1067181ca020SAlex Elder 	u32 val;
106884f9bd12SAlex Elder 
1069fb57c3eaSAlex Elder 	if (!endpoint->toward_ipa)
1070fb57c3eaSAlex Elder 		return;		/* Register not valid for RX endpoints */
1071fb57c3eaSAlex Elder 
10726a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_SEQ);
10736bfb7538SAlex Elder 
10748ee5df65SAlex Elder 	/* Low-order byte configures primary packet processing */
1075181ca020SAlex Elder 	val = ipa_reg_encode(reg, SEQ_TYPE, endpoint->config.tx.seq_type);
10768ee5df65SAlex Elder 
1077a14d5937SAlex Elder 	/* Second byte (if supported) configures replicated packet processing */
10786bfb7538SAlex Elder 	if (ipa->version < IPA_VERSION_4_5)
1079181ca020SAlex Elder 		val |= ipa_reg_encode(reg, SEQ_REP_TYPE,
1080181ca020SAlex Elder 				      endpoint->config.tx.seq_rep_type);
108184f9bd12SAlex Elder 
10826a244b75SAlex Elder 	iowrite32(val, ipa->reg_virt + ipa_reg_n_offset(reg, endpoint_id));
108384f9bd12SAlex Elder }
108484f9bd12SAlex Elder 
108584f9bd12SAlex Elder /**
108684f9bd12SAlex Elder  * ipa_endpoint_skb_tx() - Transmit a socket buffer
108784f9bd12SAlex Elder  * @endpoint:	Endpoint pointer
108884f9bd12SAlex Elder  * @skb:	Socket buffer to send
108984f9bd12SAlex Elder  *
109084f9bd12SAlex Elder  * Returns:	0 if successful, or a negative error code
109184f9bd12SAlex Elder  */
109284f9bd12SAlex Elder int ipa_endpoint_skb_tx(struct ipa_endpoint *endpoint, struct sk_buff *skb)
109384f9bd12SAlex Elder {
109484f9bd12SAlex Elder 	struct gsi_trans *trans;
109584f9bd12SAlex Elder 	u32 nr_frags;
109684f9bd12SAlex Elder 	int ret;
109784f9bd12SAlex Elder 
109884f9bd12SAlex Elder 	/* Make sure source endpoint's TLV FIFO has enough entries to
109984f9bd12SAlex Elder 	 * hold the linear portion of the skb and all its fragments.
110084f9bd12SAlex Elder 	 * If not, see if we can linearize it before giving up.
110184f9bd12SAlex Elder 	 */
110284f9bd12SAlex Elder 	nr_frags = skb_shinfo(skb)->nr_frags;
1103317595d2SAlex Elder 	if (nr_frags > endpoint->skb_frag_max) {
110484f9bd12SAlex Elder 		if (skb_linearize(skb))
110584f9bd12SAlex Elder 			return -E2BIG;
110684f9bd12SAlex Elder 		nr_frags = 0;
110784f9bd12SAlex Elder 	}
110884f9bd12SAlex Elder 
110984f9bd12SAlex Elder 	trans = ipa_endpoint_trans_alloc(endpoint, 1 + nr_frags);
111084f9bd12SAlex Elder 	if (!trans)
111184f9bd12SAlex Elder 		return -EBUSY;
111284f9bd12SAlex Elder 
111384f9bd12SAlex Elder 	ret = gsi_trans_skb_add(trans, skb);
111484f9bd12SAlex Elder 	if (ret)
111584f9bd12SAlex Elder 		goto err_trans_free;
111684f9bd12SAlex Elder 	trans->data = skb;	/* transaction owns skb now */
111784f9bd12SAlex Elder 
111884f9bd12SAlex Elder 	gsi_trans_commit(trans, !netdev_xmit_more());
111984f9bd12SAlex Elder 
112084f9bd12SAlex Elder 	return 0;
112184f9bd12SAlex Elder 
112284f9bd12SAlex Elder err_trans_free:
112384f9bd12SAlex Elder 	gsi_trans_free(trans);
112484f9bd12SAlex Elder 
112584f9bd12SAlex Elder 	return -ENOMEM;
112684f9bd12SAlex Elder }
112784f9bd12SAlex Elder 
112884f9bd12SAlex Elder static void ipa_endpoint_status(struct ipa_endpoint *endpoint)
112984f9bd12SAlex Elder {
113084f9bd12SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
113184f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
11326a244b75SAlex Elder 	const struct ipa_reg *reg;
113384f9bd12SAlex Elder 	u32 val = 0;
113484f9bd12SAlex Elder 
11356a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_STATUS);
1136660e52d6SAlex Elder 	if (endpoint->config.status_enable) {
1137181ca020SAlex Elder 		val |= ipa_reg_bit(reg, STATUS_EN);
113884f9bd12SAlex Elder 		if (endpoint->toward_ipa) {
113984f9bd12SAlex Elder 			enum ipa_endpoint_name name;
114084f9bd12SAlex Elder 			u32 status_endpoint_id;
114184f9bd12SAlex Elder 
1142660e52d6SAlex Elder 			name = endpoint->config.tx.status_endpoint;
114384f9bd12SAlex Elder 			status_endpoint_id = ipa->name_map[name]->endpoint_id;
114484f9bd12SAlex Elder 
1145181ca020SAlex Elder 			val |= ipa_reg_encode(reg, STATUS_ENDP,
1146181ca020SAlex Elder 					      status_endpoint_id);
114784f9bd12SAlex Elder 		}
11488bfc4e21SAlex Elder 		/* STATUS_LOCATION is 0, meaning status element precedes
1149181ca020SAlex Elder 		 * packet (not present for IPA v4.5+)
11508bfc4e21SAlex Elder 		 */
1151181ca020SAlex Elder 		/* STATUS_PKT_SUPPRESS_FMASK is 0 (not present for v4.0+) */
115284f9bd12SAlex Elder 	}
115384f9bd12SAlex Elder 
11546a244b75SAlex Elder 	iowrite32(val, ipa->reg_virt + ipa_reg_n_offset(reg, endpoint_id));
115584f9bd12SAlex Elder }
115684f9bd12SAlex Elder 
11576a606b90SAlex Elder static int ipa_endpoint_replenish_one(struct ipa_endpoint *endpoint,
11586a606b90SAlex Elder 				      struct gsi_trans *trans)
115984f9bd12SAlex Elder {
116084f9bd12SAlex Elder 	struct page *page;
1161ed23f026SAlex Elder 	u32 buffer_size;
116284f9bd12SAlex Elder 	u32 offset;
116384f9bd12SAlex Elder 	u32 len;
116484f9bd12SAlex Elder 	int ret;
116584f9bd12SAlex Elder 
1166660e52d6SAlex Elder 	buffer_size = endpoint->config.rx.buffer_size;
1167ed23f026SAlex Elder 	page = dev_alloc_pages(get_order(buffer_size));
116884f9bd12SAlex Elder 	if (!page)
11696a606b90SAlex Elder 		return -ENOMEM;
117084f9bd12SAlex Elder 
117184f9bd12SAlex Elder 	/* Offset the buffer to make space for skb headroom */
117284f9bd12SAlex Elder 	offset = NET_SKB_PAD;
1173ed23f026SAlex Elder 	len = buffer_size - offset;
117484f9bd12SAlex Elder 
117584f9bd12SAlex Elder 	ret = gsi_trans_page_add(trans, page, len, offset);
117684f9bd12SAlex Elder 	if (ret)
117770132763SAlex Elder 		put_page(page);
11786a606b90SAlex Elder 	else
117984f9bd12SAlex Elder 		trans->data = page;	/* transaction owns page now */
118084f9bd12SAlex Elder 
11816a606b90SAlex Elder 	return ret;
118284f9bd12SAlex Elder }
118384f9bd12SAlex Elder 
118484f9bd12SAlex Elder /**
11859af5ccf3SAlex Elder  * ipa_endpoint_replenish() - Replenish endpoint receive buffers
1186e3eea08eSAlex Elder  * @endpoint:	Endpoint to be replenished
118784f9bd12SAlex Elder  *
11889af5ccf3SAlex Elder  * The IPA hardware can hold a fixed number of receive buffers for an RX
11899af5ccf3SAlex Elder  * endpoint, based on the number of entries in the underlying channel ring
11909af5ccf3SAlex Elder  * buffer.  If an endpoint's "backlog" is non-zero, it indicates how many
11919af5ccf3SAlex Elder  * more receive buffers can be supplied to the hardware.  Replenishing for
1192a9bec7aeSAlex Elder  * an endpoint can be disabled, in which case buffers are not queued to
1193a9bec7aeSAlex Elder  * the hardware.
119484f9bd12SAlex Elder  */
11954b22d841SAlex Elder static void ipa_endpoint_replenish(struct ipa_endpoint *endpoint)
119684f9bd12SAlex Elder {
11976a606b90SAlex Elder 	struct gsi_trans *trans;
119884f9bd12SAlex Elder 
11994b22d841SAlex Elder 	if (!test_bit(IPA_REPLENISH_ENABLED, endpoint->replenish_flags))
120084f9bd12SAlex Elder 		return;
120184f9bd12SAlex Elder 
12024b22d841SAlex Elder 	/* Skip it if it's already active */
12034b22d841SAlex Elder 	if (test_and_set_bit(IPA_REPLENISH_ACTIVE, endpoint->replenish_flags))
1204998c0bd2SAlex Elder 		return;
1205998c0bd2SAlex Elder 
1206d0ac30e7SAlex Elder 	while ((trans = ipa_endpoint_trans_alloc(endpoint, 1))) {
12079654d8c4SAlex Elder 		bool doorbell;
12089654d8c4SAlex Elder 
12096a606b90SAlex Elder 		if (ipa_endpoint_replenish_one(endpoint, trans))
12106a606b90SAlex Elder 			goto try_again_later;
1211b9dbabc5SAlex Elder 
1212b9dbabc5SAlex Elder 
1213b9dbabc5SAlex Elder 		/* Ring the doorbell if we've got a full batch */
12149654d8c4SAlex Elder 		doorbell = !(++endpoint->replenish_count % IPA_REPLENISH_BATCH);
12159654d8c4SAlex Elder 		gsi_trans_commit(trans, doorbell);
1216b9dbabc5SAlex Elder 	}
1217998c0bd2SAlex Elder 
1218998c0bd2SAlex Elder 	clear_bit(IPA_REPLENISH_ACTIVE, endpoint->replenish_flags);
1219998c0bd2SAlex Elder 
122084f9bd12SAlex Elder 	return;
122184f9bd12SAlex Elder 
122284f9bd12SAlex Elder try_again_later:
12236a606b90SAlex Elder 	gsi_trans_free(trans);
1224998c0bd2SAlex Elder 	clear_bit(IPA_REPLENISH_ACTIVE, endpoint->replenish_flags);
1225998c0bd2SAlex Elder 
122684f9bd12SAlex Elder 	/* Whenever a receive buffer transaction completes we'll try to
122784f9bd12SAlex Elder 	 * replenish again.  It's unlikely, but if we fail to supply even
122884f9bd12SAlex Elder 	 * one buffer, nothing will trigger another replenish attempt.
12295fc7f9baSAlex Elder 	 * If the hardware has no receive buffers queued, schedule work to
12305fc7f9baSAlex Elder 	 * try replenishing again.
123184f9bd12SAlex Elder 	 */
12325fc7f9baSAlex Elder 	if (gsi_channel_trans_idle(&endpoint->ipa->gsi, endpoint->channel_id))
123384f9bd12SAlex Elder 		schedule_delayed_work(&endpoint->replenish_work,
123484f9bd12SAlex Elder 				      msecs_to_jiffies(1));
123584f9bd12SAlex Elder }
123684f9bd12SAlex Elder 
123784f9bd12SAlex Elder static void ipa_endpoint_replenish_enable(struct ipa_endpoint *endpoint)
123884f9bd12SAlex Elder {
1239c1aaa01dSAlex Elder 	set_bit(IPA_REPLENISH_ENABLED, endpoint->replenish_flags);
124084f9bd12SAlex Elder 
124184f9bd12SAlex Elder 	/* Start replenishing if hardware currently has no buffers */
12425fc7f9baSAlex Elder 	if (gsi_channel_trans_idle(&endpoint->ipa->gsi, endpoint->channel_id))
12434b22d841SAlex Elder 		ipa_endpoint_replenish(endpoint);
124484f9bd12SAlex Elder }
124584f9bd12SAlex Elder 
124684f9bd12SAlex Elder static void ipa_endpoint_replenish_disable(struct ipa_endpoint *endpoint)
124784f9bd12SAlex Elder {
1248c1aaa01dSAlex Elder 	clear_bit(IPA_REPLENISH_ENABLED, endpoint->replenish_flags);
124984f9bd12SAlex Elder }
125084f9bd12SAlex Elder 
125184f9bd12SAlex Elder static void ipa_endpoint_replenish_work(struct work_struct *work)
125284f9bd12SAlex Elder {
125384f9bd12SAlex Elder 	struct delayed_work *dwork = to_delayed_work(work);
125484f9bd12SAlex Elder 	struct ipa_endpoint *endpoint;
125584f9bd12SAlex Elder 
125684f9bd12SAlex Elder 	endpoint = container_of(dwork, struct ipa_endpoint, replenish_work);
125784f9bd12SAlex Elder 
12584b22d841SAlex Elder 	ipa_endpoint_replenish(endpoint);
125984f9bd12SAlex Elder }
126084f9bd12SAlex Elder 
126184f9bd12SAlex Elder static void ipa_endpoint_skb_copy(struct ipa_endpoint *endpoint,
126284f9bd12SAlex Elder 				  void *data, u32 len, u32 extra)
126384f9bd12SAlex Elder {
126484f9bd12SAlex Elder 	struct sk_buff *skb;
126584f9bd12SAlex Elder 
12661b65bbccSAlex Elder 	if (!endpoint->netdev)
12671b65bbccSAlex Elder 		return;
12681b65bbccSAlex Elder 
126984f9bd12SAlex Elder 	skb = __dev_alloc_skb(len, GFP_ATOMIC);
127030b338ffSAlex Elder 	if (skb) {
12711b65bbccSAlex Elder 		/* Copy the data into the socket buffer and receive it */
127284f9bd12SAlex Elder 		skb_put(skb, len);
127384f9bd12SAlex Elder 		memcpy(skb->data, data, len);
127484f9bd12SAlex Elder 		skb->truesize += extra;
127530b338ffSAlex Elder 	}
127684f9bd12SAlex Elder 
127784f9bd12SAlex Elder 	ipa_modem_skb_rx(endpoint->netdev, skb);
127884f9bd12SAlex Elder }
127984f9bd12SAlex Elder 
128084f9bd12SAlex Elder static bool ipa_endpoint_skb_build(struct ipa_endpoint *endpoint,
128184f9bd12SAlex Elder 				   struct page *page, u32 len)
128284f9bd12SAlex Elder {
1283660e52d6SAlex Elder 	u32 buffer_size = endpoint->config.rx.buffer_size;
128484f9bd12SAlex Elder 	struct sk_buff *skb;
128584f9bd12SAlex Elder 
128684f9bd12SAlex Elder 	/* Nothing to do if there's no netdev */
128784f9bd12SAlex Elder 	if (!endpoint->netdev)
128884f9bd12SAlex Elder 		return false;
128984f9bd12SAlex Elder 
1290ed23f026SAlex Elder 	WARN_ON(len > SKB_WITH_OVERHEAD(buffer_size - NET_SKB_PAD));
12915bc55884SAlex Elder 
1292ed23f026SAlex Elder 	skb = build_skb(page_address(page), buffer_size);
129384f9bd12SAlex Elder 	if (skb) {
129484f9bd12SAlex Elder 		/* Reserve the headroom and account for the data */
129584f9bd12SAlex Elder 		skb_reserve(skb, NET_SKB_PAD);
129684f9bd12SAlex Elder 		skb_put(skb, len);
129784f9bd12SAlex Elder 	}
129884f9bd12SAlex Elder 
129984f9bd12SAlex Elder 	/* Receive the buffer (or record drop if unable to build it) */
130084f9bd12SAlex Elder 	ipa_modem_skb_rx(endpoint->netdev, skb);
130184f9bd12SAlex Elder 
130284f9bd12SAlex Elder 	return skb != NULL;
130384f9bd12SAlex Elder }
130484f9bd12SAlex Elder 
130584f9bd12SAlex Elder /* The format of a packet status element is the same for several status
130645921390SAlex Elder  * types (opcodes).  Other types aren't currently supported.
130784f9bd12SAlex Elder  */
130884f9bd12SAlex Elder static bool ipa_status_format_packet(enum ipa_status_opcode opcode)
130984f9bd12SAlex Elder {
131084f9bd12SAlex Elder 	switch (opcode) {
131184f9bd12SAlex Elder 	case IPA_STATUS_OPCODE_PACKET:
131284f9bd12SAlex Elder 	case IPA_STATUS_OPCODE_DROPPED_PACKET:
131384f9bd12SAlex Elder 	case IPA_STATUS_OPCODE_SUSPENDED_PACKET:
131484f9bd12SAlex Elder 	case IPA_STATUS_OPCODE_PACKET_2ND_PASS:
131584f9bd12SAlex Elder 		return true;
131684f9bd12SAlex Elder 	default:
131784f9bd12SAlex Elder 		return false;
131884f9bd12SAlex Elder 	}
131984f9bd12SAlex Elder }
132084f9bd12SAlex Elder 
132184f9bd12SAlex Elder static bool ipa_endpoint_status_skip(struct ipa_endpoint *endpoint,
132284f9bd12SAlex Elder 				     const struct ipa_status *status)
132384f9bd12SAlex Elder {
132484f9bd12SAlex Elder 	u32 endpoint_id;
132584f9bd12SAlex Elder 
132684f9bd12SAlex Elder 	if (!ipa_status_format_packet(status->opcode))
132784f9bd12SAlex Elder 		return true;
132884f9bd12SAlex Elder 	if (!status->pkt_len)
132984f9bd12SAlex Elder 		return true;
1330c13899f1SAlex Elder 	endpoint_id = u8_get_bits(status->endp_dst_idx,
133184f9bd12SAlex Elder 				  IPA_STATUS_DST_IDX_FMASK);
133284f9bd12SAlex Elder 	if (endpoint_id != endpoint->endpoint_id)
133384f9bd12SAlex Elder 		return true;
133484f9bd12SAlex Elder 
133584f9bd12SAlex Elder 	return false;	/* Don't skip this packet, process it */
133684f9bd12SAlex Elder }
133784f9bd12SAlex Elder 
1338f6aba7b5SAlex Elder static bool ipa_endpoint_status_tag(struct ipa_endpoint *endpoint,
1339f6aba7b5SAlex Elder 				    const struct ipa_status *status)
1340f6aba7b5SAlex Elder {
134151c48ce2SAlex Elder 	struct ipa_endpoint *command_endpoint;
134251c48ce2SAlex Elder 	struct ipa *ipa = endpoint->ipa;
134351c48ce2SAlex Elder 	u32 endpoint_id;
134451c48ce2SAlex Elder 
134551c48ce2SAlex Elder 	if (!le16_get_bits(status->mask, IPA_STATUS_MASK_TAG_VALID_FMASK))
134651c48ce2SAlex Elder 		return false;	/* No valid tag */
134751c48ce2SAlex Elder 
134851c48ce2SAlex Elder 	/* The status contains a valid tag.  We know the packet was sent to
134951c48ce2SAlex Elder 	 * this endpoint (already verified by ipa_endpoint_status_skip()).
135051c48ce2SAlex Elder 	 * If the packet came from the AP->command TX endpoint we know
135151c48ce2SAlex Elder 	 * this packet was sent as part of the pipeline clear process.
135251c48ce2SAlex Elder 	 */
135351c48ce2SAlex Elder 	endpoint_id = u8_get_bits(status->endp_src_idx,
135451c48ce2SAlex Elder 				  IPA_STATUS_SRC_IDX_FMASK);
135551c48ce2SAlex Elder 	command_endpoint = ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX];
135651c48ce2SAlex Elder 	if (endpoint_id == command_endpoint->endpoint_id) {
135751c48ce2SAlex Elder 		complete(&ipa->completion);
135851c48ce2SAlex Elder 	} else {
135951c48ce2SAlex Elder 		dev_err(&ipa->pdev->dev,
136051c48ce2SAlex Elder 			"unexpected tagged packet from endpoint %u\n",
136151c48ce2SAlex Elder 			endpoint_id);
136251c48ce2SAlex Elder 	}
136351c48ce2SAlex Elder 
136451c48ce2SAlex Elder 	return true;
1365f6aba7b5SAlex Elder }
1366f6aba7b5SAlex Elder 
136784f9bd12SAlex Elder /* Return whether the status indicates the packet should be dropped */
1368f6aba7b5SAlex Elder static bool ipa_endpoint_status_drop(struct ipa_endpoint *endpoint,
1369f6aba7b5SAlex Elder 				     const struct ipa_status *status)
137084f9bd12SAlex Elder {
137184f9bd12SAlex Elder 	u32 val;
137284f9bd12SAlex Elder 
1373f6aba7b5SAlex Elder 	/* If the status indicates a tagged transfer, we'll drop the packet */
1374f6aba7b5SAlex Elder 	if (ipa_endpoint_status_tag(endpoint, status))
1375f6aba7b5SAlex Elder 		return true;
1376f6aba7b5SAlex Elder 
1377ab4f71e5SAlex Elder 	/* Deaggregation exceptions we drop; all other types we consume */
137884f9bd12SAlex Elder 	if (status->exception)
137984f9bd12SAlex Elder 		return status->exception == IPA_STATUS_EXCEPTION_DEAGGR;
138084f9bd12SAlex Elder 
138184f9bd12SAlex Elder 	/* Drop the packet if it fails to match a routing rule; otherwise no */
138284f9bd12SAlex Elder 	val = le32_get_bits(status->flags1, IPA_STATUS_FLAGS1_RT_RULE_ID_FMASK);
138384f9bd12SAlex Elder 
138484f9bd12SAlex Elder 	return val == field_max(IPA_STATUS_FLAGS1_RT_RULE_ID_FMASK);
138584f9bd12SAlex Elder }
138684f9bd12SAlex Elder 
138784f9bd12SAlex Elder static void ipa_endpoint_status_parse(struct ipa_endpoint *endpoint,
138884f9bd12SAlex Elder 				      struct page *page, u32 total_len)
138984f9bd12SAlex Elder {
1390660e52d6SAlex Elder 	u32 buffer_size = endpoint->config.rx.buffer_size;
139184f9bd12SAlex Elder 	void *data = page_address(page) + NET_SKB_PAD;
1392ed23f026SAlex Elder 	u32 unused = buffer_size - total_len;
139384f9bd12SAlex Elder 	u32 resid = total_len;
139484f9bd12SAlex Elder 
139584f9bd12SAlex Elder 	while (resid) {
139684f9bd12SAlex Elder 		const struct ipa_status *status = data;
139784f9bd12SAlex Elder 		u32 align;
139884f9bd12SAlex Elder 		u32 len;
139984f9bd12SAlex Elder 
140084f9bd12SAlex Elder 		if (resid < sizeof(*status)) {
140184f9bd12SAlex Elder 			dev_err(&endpoint->ipa->pdev->dev,
140284f9bd12SAlex Elder 				"short message (%u bytes < %zu byte status)\n",
140384f9bd12SAlex Elder 				resid, sizeof(*status));
140484f9bd12SAlex Elder 			break;
140584f9bd12SAlex Elder 		}
140684f9bd12SAlex Elder 
140784f9bd12SAlex Elder 		/* Skip over status packets that lack packet data */
140884f9bd12SAlex Elder 		if (ipa_endpoint_status_skip(endpoint, status)) {
140984f9bd12SAlex Elder 			data += sizeof(*status);
141084f9bd12SAlex Elder 			resid -= sizeof(*status);
141184f9bd12SAlex Elder 			continue;
141284f9bd12SAlex Elder 		}
141384f9bd12SAlex Elder 
1414162fbc6fSAlex Elder 		/* Compute the amount of buffer space consumed by the packet,
1415162fbc6fSAlex Elder 		 * including the status element.  If the hardware is configured
1416162fbc6fSAlex Elder 		 * to pad packet data to an aligned boundary, account for that.
1417162fbc6fSAlex Elder 		 * And if checksum offload is enabled a trailer containing
1418162fbc6fSAlex Elder 		 * computed checksum information will be appended.
141984f9bd12SAlex Elder 		 */
1420660e52d6SAlex Elder 		align = endpoint->config.rx.pad_align ? : 1;
142184f9bd12SAlex Elder 		len = le16_to_cpu(status->pkt_len);
142284f9bd12SAlex Elder 		len = sizeof(*status) + ALIGN(len, align);
1423660e52d6SAlex Elder 		if (endpoint->config.checksum)
142484f9bd12SAlex Elder 			len += sizeof(struct rmnet_map_dl_csum_trailer);
142584f9bd12SAlex Elder 
1426f6aba7b5SAlex Elder 		if (!ipa_endpoint_status_drop(endpoint, status)) {
1427162fbc6fSAlex Elder 			void *data2;
1428162fbc6fSAlex Elder 			u32 extra;
1429162fbc6fSAlex Elder 			u32 len2;
143084f9bd12SAlex Elder 
143184f9bd12SAlex Elder 			/* Client receives only packet data (no status) */
1432162fbc6fSAlex Elder 			data2 = data + sizeof(*status);
1433162fbc6fSAlex Elder 			len2 = le16_to_cpu(status->pkt_len);
1434162fbc6fSAlex Elder 
1435162fbc6fSAlex Elder 			/* Have the true size reflect the extra unused space in
1436162fbc6fSAlex Elder 			 * the original receive buffer.  Distribute the "cost"
1437162fbc6fSAlex Elder 			 * proportionately across all aggregated packets in the
1438162fbc6fSAlex Elder 			 * buffer.
1439162fbc6fSAlex Elder 			 */
1440162fbc6fSAlex Elder 			extra = DIV_ROUND_CLOSEST(unused * len, total_len);
144184f9bd12SAlex Elder 			ipa_endpoint_skb_copy(endpoint, data2, len2, extra);
144284f9bd12SAlex Elder 		}
144384f9bd12SAlex Elder 
144484f9bd12SAlex Elder 		/* Consume status and the full packet it describes */
144584f9bd12SAlex Elder 		data += len;
144684f9bd12SAlex Elder 		resid -= len;
144784f9bd12SAlex Elder 	}
144884f9bd12SAlex Elder }
144984f9bd12SAlex Elder 
1450983a1a30SAlex Elder void ipa_endpoint_trans_complete(struct ipa_endpoint *endpoint,
145184f9bd12SAlex Elder 				 struct gsi_trans *trans)
145284f9bd12SAlex Elder {
145384f9bd12SAlex Elder 	struct page *page;
145484f9bd12SAlex Elder 
1455983a1a30SAlex Elder 	if (endpoint->toward_ipa)
1456983a1a30SAlex Elder 		return;
1457983a1a30SAlex Elder 
145884f9bd12SAlex Elder 	if (trans->cancelled)
14595d6ac24fSAlex Elder 		goto done;
146084f9bd12SAlex Elder 
146184f9bd12SAlex Elder 	/* Parse or build a socket buffer using the actual received length */
146284f9bd12SAlex Elder 	page = trans->data;
1463660e52d6SAlex Elder 	if (endpoint->config.status_enable)
146484f9bd12SAlex Elder 		ipa_endpoint_status_parse(endpoint, page, trans->len);
146584f9bd12SAlex Elder 	else if (ipa_endpoint_skb_build(endpoint, page, trans->len))
146684f9bd12SAlex Elder 		trans->data = NULL;	/* Pages have been consumed */
14675d6ac24fSAlex Elder done:
14685d6ac24fSAlex Elder 	ipa_endpoint_replenish(endpoint);
146984f9bd12SAlex Elder }
147084f9bd12SAlex Elder 
147184f9bd12SAlex Elder void ipa_endpoint_trans_release(struct ipa_endpoint *endpoint,
147284f9bd12SAlex Elder 				struct gsi_trans *trans)
147384f9bd12SAlex Elder {
147484f9bd12SAlex Elder 	if (endpoint->toward_ipa) {
147584f9bd12SAlex Elder 		struct ipa *ipa = endpoint->ipa;
147684f9bd12SAlex Elder 
147784f9bd12SAlex Elder 		/* Nothing to do for command transactions */
147884f9bd12SAlex Elder 		if (endpoint != ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]) {
147984f9bd12SAlex Elder 			struct sk_buff *skb = trans->data;
148084f9bd12SAlex Elder 
148184f9bd12SAlex Elder 			if (skb)
148284f9bd12SAlex Elder 				dev_kfree_skb_any(skb);
148384f9bd12SAlex Elder 		}
148484f9bd12SAlex Elder 	} else {
148584f9bd12SAlex Elder 		struct page *page = trans->data;
148684f9bd12SAlex Elder 
1487155c0c90SAlex Elder 		if (page)
1488155c0c90SAlex Elder 			put_page(page);
148984f9bd12SAlex Elder 	}
149084f9bd12SAlex Elder }
149184f9bd12SAlex Elder 
149284f9bd12SAlex Elder void ipa_endpoint_default_route_set(struct ipa *ipa, u32 endpoint_id)
149384f9bd12SAlex Elder {
14946a244b75SAlex Elder 	const struct ipa_reg *reg;
149584f9bd12SAlex Elder 	u32 val;
149684f9bd12SAlex Elder 
14976a244b75SAlex Elder 	reg = ipa_reg(ipa, ROUTE);
149884f9bd12SAlex Elder 	/* ROUTE_DIS is 0 */
1499479deb32SAlex Elder 	val = ipa_reg_encode(reg, ROUTE_DEF_PIPE, endpoint_id);
1500479deb32SAlex Elder 	val |= ipa_reg_bit(reg, ROUTE_DEF_HDR_TABLE);
1501479deb32SAlex Elder 	/* ROUTE_DEF_HDR_OFST is 0 */
1502479deb32SAlex Elder 	val |= ipa_reg_encode(reg, ROUTE_FRAG_DEF_PIPE, endpoint_id);
1503479deb32SAlex Elder 	val |= ipa_reg_bit(reg, ROUTE_DEF_RETAIN_HDR);
150484f9bd12SAlex Elder 
15056a244b75SAlex Elder 	iowrite32(val, ipa->reg_virt + ipa_reg_offset(reg));
150684f9bd12SAlex Elder }
150784f9bd12SAlex Elder 
150884f9bd12SAlex Elder void ipa_endpoint_default_route_clear(struct ipa *ipa)
150984f9bd12SAlex Elder {
151084f9bd12SAlex Elder 	ipa_endpoint_default_route_set(ipa, 0);
151184f9bd12SAlex Elder }
151284f9bd12SAlex Elder 
151384f9bd12SAlex Elder /**
151484f9bd12SAlex Elder  * ipa_endpoint_reset_rx_aggr() - Reset RX endpoint with aggregation active
151584f9bd12SAlex Elder  * @endpoint:	Endpoint to be reset
151684f9bd12SAlex Elder  *
151784f9bd12SAlex Elder  * If aggregation is active on an RX endpoint when a reset is performed
151884f9bd12SAlex Elder  * on its underlying GSI channel, a special sequence of actions must be
151984f9bd12SAlex Elder  * taken to ensure the IPA pipeline is properly cleared.
152084f9bd12SAlex Elder  *
1521e3eea08eSAlex Elder  * Return:	0 if successful, or a negative error code
152284f9bd12SAlex Elder  */
152384f9bd12SAlex Elder static int ipa_endpoint_reset_rx_aggr(struct ipa_endpoint *endpoint)
152484f9bd12SAlex Elder {
152584f9bd12SAlex Elder 	struct device *dev = &endpoint->ipa->pdev->dev;
152684f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
152784f9bd12SAlex Elder 	struct gsi *gsi = &ipa->gsi;
15284fa95248SAlex Elder 	bool suspended = false;
152984f9bd12SAlex Elder 	dma_addr_t addr;
153084f9bd12SAlex Elder 	u32 retries;
153184f9bd12SAlex Elder 	u32 len = 1;
153284f9bd12SAlex Elder 	void *virt;
153384f9bd12SAlex Elder 	int ret;
153484f9bd12SAlex Elder 
153584f9bd12SAlex Elder 	virt = kzalloc(len, GFP_KERNEL);
153684f9bd12SAlex Elder 	if (!virt)
153784f9bd12SAlex Elder 		return -ENOMEM;
153884f9bd12SAlex Elder 
153984f9bd12SAlex Elder 	addr = dma_map_single(dev, virt, len, DMA_FROM_DEVICE);
154084f9bd12SAlex Elder 	if (dma_mapping_error(dev, addr)) {
154184f9bd12SAlex Elder 		ret = -ENOMEM;
154284f9bd12SAlex Elder 		goto out_kfree;
154384f9bd12SAlex Elder 	}
154484f9bd12SAlex Elder 
154584f9bd12SAlex Elder 	/* Force close aggregation before issuing the reset */
154684f9bd12SAlex Elder 	ipa_endpoint_force_close(endpoint);
154784f9bd12SAlex Elder 
154884f9bd12SAlex Elder 	/* Reset and reconfigure the channel with the doorbell engine
154984f9bd12SAlex Elder 	 * disabled.  Then poll until we know aggregation is no longer
155084f9bd12SAlex Elder 	 * active.  We'll re-enable the doorbell (if appropriate) when
155184f9bd12SAlex Elder 	 * we reset again below.
155284f9bd12SAlex Elder 	 */
155384f9bd12SAlex Elder 	gsi_channel_reset(gsi, endpoint->channel_id, false);
155484f9bd12SAlex Elder 
155584f9bd12SAlex Elder 	/* Make sure the channel isn't suspended */
15564fa95248SAlex Elder 	suspended = ipa_endpoint_program_suspend(endpoint, false);
155784f9bd12SAlex Elder 
155884f9bd12SAlex Elder 	/* Start channel and do a 1 byte read */
155984f9bd12SAlex Elder 	ret = gsi_channel_start(gsi, endpoint->channel_id);
156084f9bd12SAlex Elder 	if (ret)
156184f9bd12SAlex Elder 		goto out_suspend_again;
156284f9bd12SAlex Elder 
156384f9bd12SAlex Elder 	ret = gsi_trans_read_byte(gsi, endpoint->channel_id, addr);
156484f9bd12SAlex Elder 	if (ret)
156584f9bd12SAlex Elder 		goto err_endpoint_stop;
156684f9bd12SAlex Elder 
156784f9bd12SAlex Elder 	/* Wait for aggregation to be closed on the channel */
156884f9bd12SAlex Elder 	retries = IPA_ENDPOINT_RESET_AGGR_RETRY_MAX;
156984f9bd12SAlex Elder 	do {
157084f9bd12SAlex Elder 		if (!ipa_endpoint_aggr_active(endpoint))
157184f9bd12SAlex Elder 			break;
157274401946SAlex Elder 		usleep_range(USEC_PER_MSEC, 2 * USEC_PER_MSEC);
157384f9bd12SAlex Elder 	} while (retries--);
157484f9bd12SAlex Elder 
157584f9bd12SAlex Elder 	/* Check one last time */
157684f9bd12SAlex Elder 	if (ipa_endpoint_aggr_active(endpoint))
157784f9bd12SAlex Elder 		dev_err(dev, "endpoint %u still active during reset\n",
157884f9bd12SAlex Elder 			endpoint->endpoint_id);
157984f9bd12SAlex Elder 
158084f9bd12SAlex Elder 	gsi_trans_read_byte_done(gsi, endpoint->channel_id);
158184f9bd12SAlex Elder 
1582f30dcb7dSAlex Elder 	ret = gsi_channel_stop(gsi, endpoint->channel_id);
158384f9bd12SAlex Elder 	if (ret)
158484f9bd12SAlex Elder 		goto out_suspend_again;
158584f9bd12SAlex Elder 
1586497abc87SPeng Li 	/* Finally, reset and reconfigure the channel again (re-enabling
158784f9bd12SAlex Elder 	 * the doorbell engine if appropriate).  Sleep for 1 millisecond to
158884f9bd12SAlex Elder 	 * complete the channel reset sequence.  Finish by suspending the
158984f9bd12SAlex Elder 	 * channel again (if necessary).
159084f9bd12SAlex Elder 	 */
1591ce54993dSAlex Elder 	gsi_channel_reset(gsi, endpoint->channel_id, true);
159284f9bd12SAlex Elder 
159374401946SAlex Elder 	usleep_range(USEC_PER_MSEC, 2 * USEC_PER_MSEC);
159484f9bd12SAlex Elder 
159584f9bd12SAlex Elder 	goto out_suspend_again;
159684f9bd12SAlex Elder 
159784f9bd12SAlex Elder err_endpoint_stop:
1598f30dcb7dSAlex Elder 	(void)gsi_channel_stop(gsi, endpoint->channel_id);
159984f9bd12SAlex Elder out_suspend_again:
16004fa95248SAlex Elder 	if (suspended)
16014fa95248SAlex Elder 		(void)ipa_endpoint_program_suspend(endpoint, true);
160284f9bd12SAlex Elder 	dma_unmap_single(dev, addr, len, DMA_FROM_DEVICE);
160384f9bd12SAlex Elder out_kfree:
160484f9bd12SAlex Elder 	kfree(virt);
160584f9bd12SAlex Elder 
160684f9bd12SAlex Elder 	return ret;
160784f9bd12SAlex Elder }
160884f9bd12SAlex Elder 
160984f9bd12SAlex Elder static void ipa_endpoint_reset(struct ipa_endpoint *endpoint)
161084f9bd12SAlex Elder {
161184f9bd12SAlex Elder 	u32 channel_id = endpoint->channel_id;
161284f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
161384f9bd12SAlex Elder 	bool special;
161484f9bd12SAlex Elder 	int ret = 0;
161584f9bd12SAlex Elder 
161684f9bd12SAlex Elder 	/* On IPA v3.5.1, if an RX endpoint is reset while aggregation
161784f9bd12SAlex Elder 	 * is active, we need to handle things specially to recover.
161884f9bd12SAlex Elder 	 * All other cases just need to reset the underlying GSI channel.
161984f9bd12SAlex Elder 	 */
1620d7f3087bSAlex Elder 	special = ipa->version < IPA_VERSION_4_0 && !endpoint->toward_ipa &&
1621660e52d6SAlex Elder 			endpoint->config.aggregation;
1622ce54993dSAlex Elder 	if (special && ipa_endpoint_aggr_active(endpoint))
162384f9bd12SAlex Elder 		ret = ipa_endpoint_reset_rx_aggr(endpoint);
162484f9bd12SAlex Elder 	else
1625ce54993dSAlex Elder 		gsi_channel_reset(&ipa->gsi, channel_id, true);
162684f9bd12SAlex Elder 
162784f9bd12SAlex Elder 	if (ret)
162884f9bd12SAlex Elder 		dev_err(&ipa->pdev->dev,
162984f9bd12SAlex Elder 			"error %d resetting channel %u for endpoint %u\n",
163084f9bd12SAlex Elder 			ret, endpoint->channel_id, endpoint->endpoint_id);
163184f9bd12SAlex Elder }
163284f9bd12SAlex Elder 
163384f9bd12SAlex Elder static void ipa_endpoint_program(struct ipa_endpoint *endpoint)
163484f9bd12SAlex Elder {
16354c9d631aSAlex Elder 	if (endpoint->toward_ipa) {
16364c9d631aSAlex Elder 		/* Newer versions of IPA use GSI channel flow control
16374c9d631aSAlex Elder 		 * instead of endpoint DELAY mode to prevent sending data.
16384c9d631aSAlex Elder 		 * Flow control is disabled for newly-allocated channels,
16394c9d631aSAlex Elder 		 * and we can assume flow control is not (ever) enabled
16404c9d631aSAlex Elder 		 * for AP TX channels.
16414c9d631aSAlex Elder 		 */
16424c9d631aSAlex Elder 		if (endpoint->ipa->version < IPA_VERSION_4_2)
1643a4dcad34SAlex Elder 			ipa_endpoint_program_delay(endpoint, false);
16444c9d631aSAlex Elder 	} else {
16454c9d631aSAlex Elder 		/* Ensure suspend mode is off on all AP RX endpoints */
1646fb57c3eaSAlex Elder 		(void)ipa_endpoint_program_suspend(endpoint, false);
16474c9d631aSAlex Elder 	}
1648fb57c3eaSAlex Elder 	ipa_endpoint_init_cfg(endpoint);
1649647a05f3SAlex Elder 	ipa_endpoint_init_nat(endpoint);
1650fb57c3eaSAlex Elder 	ipa_endpoint_init_hdr(endpoint);
165184f9bd12SAlex Elder 	ipa_endpoint_init_hdr_ext(endpoint);
1652fb57c3eaSAlex Elder 	ipa_endpoint_init_hdr_metadata_mask(endpoint);
1653fb57c3eaSAlex Elder 	ipa_endpoint_init_mode(endpoint);
165484f9bd12SAlex Elder 	ipa_endpoint_init_aggr(endpoint);
1655153213f0SAlex Elder 	if (!endpoint->toward_ipa) {
1656153213f0SAlex Elder 		if (endpoint->config.rx.holb_drop)
1657153213f0SAlex Elder 			ipa_endpoint_init_hol_block_enable(endpoint, 0);
1658153213f0SAlex Elder 		else
165901c36637SAlex Elder 			ipa_endpoint_init_hol_block_disable(endpoint);
1660153213f0SAlex Elder 	}
166184f9bd12SAlex Elder 	ipa_endpoint_init_deaggr(endpoint);
16622d265342SAlex Elder 	ipa_endpoint_init_rsrc_grp(endpoint);
166384f9bd12SAlex Elder 	ipa_endpoint_init_seq(endpoint);
166484f9bd12SAlex Elder 	ipa_endpoint_status(endpoint);
166584f9bd12SAlex Elder }
166684f9bd12SAlex Elder 
166784f9bd12SAlex Elder int ipa_endpoint_enable_one(struct ipa_endpoint *endpoint)
166884f9bd12SAlex Elder {
166984f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
167084f9bd12SAlex Elder 	struct gsi *gsi = &ipa->gsi;
167184f9bd12SAlex Elder 	int ret;
167284f9bd12SAlex Elder 
167384f9bd12SAlex Elder 	ret = gsi_channel_start(gsi, endpoint->channel_id);
167484f9bd12SAlex Elder 	if (ret) {
167584f9bd12SAlex Elder 		dev_err(&ipa->pdev->dev,
167684f9bd12SAlex Elder 			"error %d starting %cX channel %u for endpoint %u\n",
167784f9bd12SAlex Elder 			ret, endpoint->toward_ipa ? 'T' : 'R',
167884f9bd12SAlex Elder 			endpoint->channel_id, endpoint->endpoint_id);
167984f9bd12SAlex Elder 		return ret;
168084f9bd12SAlex Elder 	}
168184f9bd12SAlex Elder 
168284f9bd12SAlex Elder 	if (!endpoint->toward_ipa) {
168384f9bd12SAlex Elder 		ipa_interrupt_suspend_enable(ipa->interrupt,
168484f9bd12SAlex Elder 					     endpoint->endpoint_id);
168584f9bd12SAlex Elder 		ipa_endpoint_replenish_enable(endpoint);
168684f9bd12SAlex Elder 	}
168784f9bd12SAlex Elder 
168884f9bd12SAlex Elder 	ipa->enabled |= BIT(endpoint->endpoint_id);
168984f9bd12SAlex Elder 
169084f9bd12SAlex Elder 	return 0;
169184f9bd12SAlex Elder }
169284f9bd12SAlex Elder 
169384f9bd12SAlex Elder void ipa_endpoint_disable_one(struct ipa_endpoint *endpoint)
169484f9bd12SAlex Elder {
169584f9bd12SAlex Elder 	u32 mask = BIT(endpoint->endpoint_id);
169684f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
1697f30dcb7dSAlex Elder 	struct gsi *gsi = &ipa->gsi;
169884f9bd12SAlex Elder 	int ret;
169984f9bd12SAlex Elder 
1700f30dcb7dSAlex Elder 	if (!(ipa->enabled & mask))
170184f9bd12SAlex Elder 		return;
170284f9bd12SAlex Elder 
1703f30dcb7dSAlex Elder 	ipa->enabled ^= mask;
170484f9bd12SAlex Elder 
170584f9bd12SAlex Elder 	if (!endpoint->toward_ipa) {
170684f9bd12SAlex Elder 		ipa_endpoint_replenish_disable(endpoint);
170784f9bd12SAlex Elder 		ipa_interrupt_suspend_disable(ipa->interrupt,
170884f9bd12SAlex Elder 					      endpoint->endpoint_id);
170984f9bd12SAlex Elder 	}
171084f9bd12SAlex Elder 
171184f9bd12SAlex Elder 	/* Note that if stop fails, the channel's state is not well-defined */
1712f30dcb7dSAlex Elder 	ret = gsi_channel_stop(gsi, endpoint->channel_id);
171384f9bd12SAlex Elder 	if (ret)
171484f9bd12SAlex Elder 		dev_err(&ipa->pdev->dev,
171584f9bd12SAlex Elder 			"error %d attempting to stop endpoint %u\n", ret,
171684f9bd12SAlex Elder 			endpoint->endpoint_id);
171784f9bd12SAlex Elder }
171884f9bd12SAlex Elder 
171984f9bd12SAlex Elder void ipa_endpoint_suspend_one(struct ipa_endpoint *endpoint)
172084f9bd12SAlex Elder {
172184f9bd12SAlex Elder 	struct device *dev = &endpoint->ipa->pdev->dev;
172284f9bd12SAlex Elder 	struct gsi *gsi = &endpoint->ipa->gsi;
172384f9bd12SAlex Elder 	int ret;
172484f9bd12SAlex Elder 
172584f9bd12SAlex Elder 	if (!(endpoint->ipa->enabled & BIT(endpoint->endpoint_id)))
172684f9bd12SAlex Elder 		return;
172784f9bd12SAlex Elder 
1728ab4f71e5SAlex Elder 	if (!endpoint->toward_ipa) {
172984f9bd12SAlex Elder 		ipa_endpoint_replenish_disable(endpoint);
17304fa95248SAlex Elder 		(void)ipa_endpoint_program_suspend(endpoint, true);
1731ab4f71e5SAlex Elder 	}
173284f9bd12SAlex Elder 
1733decfef0fSAlex Elder 	ret = gsi_channel_suspend(gsi, endpoint->channel_id);
173484f9bd12SAlex Elder 	if (ret)
173584f9bd12SAlex Elder 		dev_err(dev, "error %d suspending channel %u\n", ret,
173684f9bd12SAlex Elder 			endpoint->channel_id);
173784f9bd12SAlex Elder }
173884f9bd12SAlex Elder 
173984f9bd12SAlex Elder void ipa_endpoint_resume_one(struct ipa_endpoint *endpoint)
174084f9bd12SAlex Elder {
174184f9bd12SAlex Elder 	struct device *dev = &endpoint->ipa->pdev->dev;
174284f9bd12SAlex Elder 	struct gsi *gsi = &endpoint->ipa->gsi;
174384f9bd12SAlex Elder 	int ret;
174484f9bd12SAlex Elder 
174584f9bd12SAlex Elder 	if (!(endpoint->ipa->enabled & BIT(endpoint->endpoint_id)))
174684f9bd12SAlex Elder 		return;
174784f9bd12SAlex Elder 
1748b07f283eSAlex Elder 	if (!endpoint->toward_ipa)
17494fa95248SAlex Elder 		(void)ipa_endpoint_program_suspend(endpoint, false);
175084f9bd12SAlex Elder 
1751decfef0fSAlex Elder 	ret = gsi_channel_resume(gsi, endpoint->channel_id);
175284f9bd12SAlex Elder 	if (ret)
175384f9bd12SAlex Elder 		dev_err(dev, "error %d resuming channel %u\n", ret,
175484f9bd12SAlex Elder 			endpoint->channel_id);
175584f9bd12SAlex Elder 	else if (!endpoint->toward_ipa)
175684f9bd12SAlex Elder 		ipa_endpoint_replenish_enable(endpoint);
175784f9bd12SAlex Elder }
175884f9bd12SAlex Elder 
175984f9bd12SAlex Elder void ipa_endpoint_suspend(struct ipa *ipa)
176084f9bd12SAlex Elder {
1761d1704382SAlex Elder 	if (!ipa->setup_complete)
1762d1704382SAlex Elder 		return;
1763d1704382SAlex Elder 
176484f9bd12SAlex Elder 	if (ipa->modem_netdev)
176584f9bd12SAlex Elder 		ipa_modem_suspend(ipa->modem_netdev);
176684f9bd12SAlex Elder 
176784f9bd12SAlex Elder 	ipa_endpoint_suspend_one(ipa->name_map[IPA_ENDPOINT_AP_LAN_RX]);
176884f9bd12SAlex Elder 	ipa_endpoint_suspend_one(ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]);
176984f9bd12SAlex Elder }
177084f9bd12SAlex Elder 
177184f9bd12SAlex Elder void ipa_endpoint_resume(struct ipa *ipa)
177284f9bd12SAlex Elder {
1773d1704382SAlex Elder 	if (!ipa->setup_complete)
1774d1704382SAlex Elder 		return;
1775d1704382SAlex Elder 
177684f9bd12SAlex Elder 	ipa_endpoint_resume_one(ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]);
177784f9bd12SAlex Elder 	ipa_endpoint_resume_one(ipa->name_map[IPA_ENDPOINT_AP_LAN_RX]);
177884f9bd12SAlex Elder 
177984f9bd12SAlex Elder 	if (ipa->modem_netdev)
178084f9bd12SAlex Elder 		ipa_modem_resume(ipa->modem_netdev);
178184f9bd12SAlex Elder }
178284f9bd12SAlex Elder 
178384f9bd12SAlex Elder static void ipa_endpoint_setup_one(struct ipa_endpoint *endpoint)
178484f9bd12SAlex Elder {
178584f9bd12SAlex Elder 	struct gsi *gsi = &endpoint->ipa->gsi;
178684f9bd12SAlex Elder 	u32 channel_id = endpoint->channel_id;
178784f9bd12SAlex Elder 
178884f9bd12SAlex Elder 	/* Only AP endpoints get set up */
178984f9bd12SAlex Elder 	if (endpoint->ee_id != GSI_EE_AP)
179084f9bd12SAlex Elder 		return;
179184f9bd12SAlex Elder 
1792317595d2SAlex Elder 	endpoint->skb_frag_max = gsi->channel[channel_id].trans_tre_max - 1;
179384f9bd12SAlex Elder 	if (!endpoint->toward_ipa) {
179484f9bd12SAlex Elder 		/* RX transactions require a single TRE, so the maximum
179584f9bd12SAlex Elder 		 * backlog is the same as the maximum outstanding TREs.
179684f9bd12SAlex Elder 		 */
1797c1aaa01dSAlex Elder 		clear_bit(IPA_REPLENISH_ENABLED, endpoint->replenish_flags);
1798998c0bd2SAlex Elder 		clear_bit(IPA_REPLENISH_ACTIVE, endpoint->replenish_flags);
179984f9bd12SAlex Elder 		INIT_DELAYED_WORK(&endpoint->replenish_work,
180084f9bd12SAlex Elder 				  ipa_endpoint_replenish_work);
180184f9bd12SAlex Elder 	}
180284f9bd12SAlex Elder 
180384f9bd12SAlex Elder 	ipa_endpoint_program(endpoint);
180484f9bd12SAlex Elder 
180584f9bd12SAlex Elder 	endpoint->ipa->set_up |= BIT(endpoint->endpoint_id);
180684f9bd12SAlex Elder }
180784f9bd12SAlex Elder 
180884f9bd12SAlex Elder static void ipa_endpoint_teardown_one(struct ipa_endpoint *endpoint)
180984f9bd12SAlex Elder {
181084f9bd12SAlex Elder 	endpoint->ipa->set_up &= ~BIT(endpoint->endpoint_id);
181184f9bd12SAlex Elder 
181284f9bd12SAlex Elder 	if (!endpoint->toward_ipa)
181384f9bd12SAlex Elder 		cancel_delayed_work_sync(&endpoint->replenish_work);
181484f9bd12SAlex Elder 
181584f9bd12SAlex Elder 	ipa_endpoint_reset(endpoint);
181684f9bd12SAlex Elder }
181784f9bd12SAlex Elder 
181884f9bd12SAlex Elder void ipa_endpoint_setup(struct ipa *ipa)
181984f9bd12SAlex Elder {
18209a9f5129SAlex Elder 	u32 endpoint_id;
182184f9bd12SAlex Elder 
182284f9bd12SAlex Elder 	ipa->set_up = 0;
18239a9f5129SAlex Elder 	for_each_set_bit(endpoint_id, ipa->defined, ipa->endpoint_count)
182484f9bd12SAlex Elder 		ipa_endpoint_setup_one(&ipa->endpoint[endpoint_id]);
182584f9bd12SAlex Elder }
182684f9bd12SAlex Elder 
182784f9bd12SAlex Elder void ipa_endpoint_teardown(struct ipa *ipa)
182884f9bd12SAlex Elder {
182984f9bd12SAlex Elder 	u32 set_up = ipa->set_up;
183084f9bd12SAlex Elder 
183184f9bd12SAlex Elder 	while (set_up) {
183284f9bd12SAlex Elder 		u32 endpoint_id = __fls(set_up);
183384f9bd12SAlex Elder 
183484f9bd12SAlex Elder 		set_up ^= BIT(endpoint_id);
183584f9bd12SAlex Elder 
183684f9bd12SAlex Elder 		ipa_endpoint_teardown_one(&ipa->endpoint[endpoint_id]);
183784f9bd12SAlex Elder 	}
183884f9bd12SAlex Elder 	ipa->set_up = 0;
183984f9bd12SAlex Elder }
184084f9bd12SAlex Elder 
184188de7672SAlex Elder void ipa_endpoint_deconfig(struct ipa *ipa)
184288de7672SAlex Elder {
184388de7672SAlex Elder 	ipa->available_count = 0;
184488de7672SAlex Elder 	bitmap_free(ipa->available);
184588de7672SAlex Elder 	ipa->available = NULL;
184688de7672SAlex Elder }
184788de7672SAlex Elder 
184884f9bd12SAlex Elder int ipa_endpoint_config(struct ipa *ipa)
184984f9bd12SAlex Elder {
185084f9bd12SAlex Elder 	struct device *dev = &ipa->pdev->dev;
18516a244b75SAlex Elder 	const struct ipa_reg *reg;
18529a9f5129SAlex Elder 	u32 endpoint_id;
18532b87d721SAlex Elder 	u32 tx_count;
18542b87d721SAlex Elder 	u32 rx_count;
185584f9bd12SAlex Elder 	u32 rx_base;
18562b87d721SAlex Elder 	u32 limit;
185784f9bd12SAlex Elder 	u32 val;
185884f9bd12SAlex Elder 
1859110971d1SAlex Elder 	/* Prior to IPA v3.5, the FLAVOR_0 register was not supported.
1860110971d1SAlex Elder 	 * Furthermore, the endpoints were not grouped such that TX
1861110971d1SAlex Elder 	 * endpoint numbers started with 0 and RX endpoints had numbers
1862110971d1SAlex Elder 	 * higher than all TX endpoints, so we can't do the simple
1863110971d1SAlex Elder 	 * direction check used for newer hardware below.
1864110971d1SAlex Elder 	 *
1865110971d1SAlex Elder 	 * For hardware that doesn't support the FLAVOR_0 register,
1866110971d1SAlex Elder 	 * just set the available mask to support any endpoint, and
1867110971d1SAlex Elder 	 * assume the configuration is valid.
1868110971d1SAlex Elder 	 */
1869110971d1SAlex Elder 	if (ipa->version < IPA_VERSION_3_5) {
187088de7672SAlex Elder 		ipa->available = bitmap_zalloc(IPA_ENDPOINT_MAX, GFP_KERNEL);
187188de7672SAlex Elder 		if (!ipa->available)
187288de7672SAlex Elder 			return -ENOMEM;
187388de7672SAlex Elder 		ipa->available_count = IPA_ENDPOINT_MAX;
187488de7672SAlex Elder 
187588de7672SAlex Elder 		bitmap_set(ipa->available, 0, IPA_ENDPOINT_MAX);
187688de7672SAlex Elder 
1877110971d1SAlex Elder 		return 0;
1878110971d1SAlex Elder 	}
1879110971d1SAlex Elder 
188084f9bd12SAlex Elder 	/* Find out about the endpoints supplied by the hardware, and ensure
18812b87d721SAlex Elder 	 * the highest one doesn't exceed the number supported by software.
188284f9bd12SAlex Elder 	 */
18836a244b75SAlex Elder 	reg = ipa_reg(ipa, FLAVOR_0);
18846a244b75SAlex Elder 	val = ioread32(ipa->reg_virt + ipa_reg_offset(reg));
188584f9bd12SAlex Elder 
18862b87d721SAlex Elder 	/* Our RX is an IPA producer; our TX is an IPA consumer. */
18872b87d721SAlex Elder 	tx_count = ipa_reg_decode(reg, MAX_CONS_PIPES, val);
18882b87d721SAlex Elder 	rx_count = ipa_reg_decode(reg, MAX_PROD_PIPES, val);
18899265a4f0SAlex Elder 	rx_base = ipa_reg_decode(reg, PROD_LOWEST, val);
18902b87d721SAlex Elder 
18912b87d721SAlex Elder 	limit = rx_base + rx_count;
18922b87d721SAlex Elder 	if (limit > IPA_ENDPOINT_MAX) {
18932b87d721SAlex Elder 		dev_err(dev, "too many endpoints, %u > %u\n",
18942b87d721SAlex Elder 			limit, IPA_ENDPOINT_MAX);
189584f9bd12SAlex Elder 		return -EINVAL;
189684f9bd12SAlex Elder 	}
189784f9bd12SAlex Elder 
189888de7672SAlex Elder 	/* Allocate and initialize the available endpoint bitmap */
189988de7672SAlex Elder 	ipa->available = bitmap_zalloc(limit, GFP_KERNEL);
190088de7672SAlex Elder 	if (!ipa->available)
190188de7672SAlex Elder 		return -ENOMEM;
190288de7672SAlex Elder 	ipa->available_count = limit;
190388de7672SAlex Elder 
19042b87d721SAlex Elder 	/* Mark all supported RX and TX endpoints as available */
190588de7672SAlex Elder 	bitmap_set(ipa->available, 0, tx_count);
190688de7672SAlex Elder 	bitmap_set(ipa->available, rx_base, rx_count);
190784f9bd12SAlex Elder 
19089a9f5129SAlex Elder 	for_each_set_bit(endpoint_id, ipa->defined, ipa->endpoint_count) {
190984f9bd12SAlex Elder 		struct ipa_endpoint *endpoint;
191084f9bd12SAlex Elder 
19112b87d721SAlex Elder 		if (endpoint_id >= limit) {
19122b87d721SAlex Elder 			dev_err(dev, "invalid endpoint id, %u > %u\n",
19132b87d721SAlex Elder 				endpoint_id, limit - 1);
191488de7672SAlex Elder 			goto err_free_bitmap;
191584f9bd12SAlex Elder 		}
191684f9bd12SAlex Elder 
191788de7672SAlex Elder 		if (!test_bit(endpoint_id, ipa->available)) {
19182b87d721SAlex Elder 			dev_err(dev, "unavailable endpoint id %u\n",
19192b87d721SAlex Elder 				endpoint_id);
192088de7672SAlex Elder 			goto err_free_bitmap;
19212b87d721SAlex Elder 		}
19222b87d721SAlex Elder 
19232b87d721SAlex Elder 		/* Make sure it's pointing in the right direction */
19242b87d721SAlex Elder 		endpoint = &ipa->endpoint[endpoint_id];
19252b87d721SAlex Elder 		if (endpoint->toward_ipa) {
19262b87d721SAlex Elder 			if (endpoint_id < tx_count)
19272b87d721SAlex Elder 				continue;
19282b87d721SAlex Elder 		} else if (endpoint_id >= rx_base) {
19292b87d721SAlex Elder 			continue;
19302b87d721SAlex Elder 		}
19312b87d721SAlex Elder 
19322b87d721SAlex Elder 		dev_err(dev, "endpoint id %u wrong direction\n", endpoint_id);
193388de7672SAlex Elder 		goto err_free_bitmap;
19342b87d721SAlex Elder 	}
19352b87d721SAlex Elder 
19362b87d721SAlex Elder 	return 0;
193784f9bd12SAlex Elder 
193888de7672SAlex Elder err_free_bitmap:
193988de7672SAlex Elder 	ipa_endpoint_deconfig(ipa);
194088de7672SAlex Elder 
194188de7672SAlex Elder 	return -EINVAL;
194284f9bd12SAlex Elder }
194384f9bd12SAlex Elder 
194484f9bd12SAlex Elder static void ipa_endpoint_init_one(struct ipa *ipa, enum ipa_endpoint_name name,
194584f9bd12SAlex Elder 				  const struct ipa_gsi_endpoint_data *data)
194684f9bd12SAlex Elder {
194784f9bd12SAlex Elder 	struct ipa_endpoint *endpoint;
194884f9bd12SAlex Elder 
194984f9bd12SAlex Elder 	endpoint = &ipa->endpoint[data->endpoint_id];
195084f9bd12SAlex Elder 
195184f9bd12SAlex Elder 	if (data->ee_id == GSI_EE_AP)
195284f9bd12SAlex Elder 		ipa->channel_map[data->channel_id] = endpoint;
195384f9bd12SAlex Elder 	ipa->name_map[name] = endpoint;
195484f9bd12SAlex Elder 
195584f9bd12SAlex Elder 	endpoint->ipa = ipa;
195684f9bd12SAlex Elder 	endpoint->ee_id = data->ee_id;
195784f9bd12SAlex Elder 	endpoint->channel_id = data->channel_id;
195884f9bd12SAlex Elder 	endpoint->endpoint_id = data->endpoint_id;
195984f9bd12SAlex Elder 	endpoint->toward_ipa = data->toward_ipa;
1960660e52d6SAlex Elder 	endpoint->config = data->endpoint.config;
196184f9bd12SAlex Elder 
19629a9f5129SAlex Elder 	__set_bit(endpoint->endpoint_id, ipa->defined);
196384f9bd12SAlex Elder }
196484f9bd12SAlex Elder 
1965602a1c76SAlex Elder static void ipa_endpoint_exit_one(struct ipa_endpoint *endpoint)
196684f9bd12SAlex Elder {
19679a9f5129SAlex Elder 	__clear_bit(endpoint->endpoint_id, endpoint->ipa->defined);
196884f9bd12SAlex Elder 
196984f9bd12SAlex Elder 	memset(endpoint, 0, sizeof(*endpoint));
197084f9bd12SAlex Elder }
197184f9bd12SAlex Elder 
197284f9bd12SAlex Elder void ipa_endpoint_exit(struct ipa *ipa)
197384f9bd12SAlex Elder {
19749a9f5129SAlex Elder 	u32 endpoint_id;
197584f9bd12SAlex Elder 
1976*0f97fbd4SAlex Elder 	ipa->filtered = 0;
1977*0f97fbd4SAlex Elder 
19789a9f5129SAlex Elder 	for_each_set_bit(endpoint_id, ipa->defined, ipa->endpoint_count)
197984f9bd12SAlex Elder 		ipa_endpoint_exit_one(&ipa->endpoint[endpoint_id]);
19809a9f5129SAlex Elder 
19819a9f5129SAlex Elder 	bitmap_free(ipa->defined);
19829a9f5129SAlex Elder 	ipa->defined = NULL;
19839a9f5129SAlex Elder 
198484f9bd12SAlex Elder 	memset(ipa->name_map, 0, sizeof(ipa->name_map));
198584f9bd12SAlex Elder 	memset(ipa->channel_map, 0, sizeof(ipa->channel_map));
198684f9bd12SAlex Elder }
198784f9bd12SAlex Elder 
198884f9bd12SAlex Elder /* Returns a bitmask of endpoints that support filtering, or 0 on error */
1989*0f97fbd4SAlex Elder int ipa_endpoint_init(struct ipa *ipa, u32 count,
199084f9bd12SAlex Elder 		      const struct ipa_gsi_endpoint_data *data)
199184f9bd12SAlex Elder {
199284f9bd12SAlex Elder 	enum ipa_endpoint_name name;
1993*0f97fbd4SAlex Elder 	u32 filtered;
199484f9bd12SAlex Elder 
19959654d8c4SAlex Elder 	BUILD_BUG_ON(!IPA_REPLENISH_BATCH);
19969654d8c4SAlex Elder 
1997b7aaff0bSAlex Elder 	/* Number of endpoints is one more than the maximum ID */
1998b7aaff0bSAlex Elder 	ipa->endpoint_count = ipa_endpoint_max(ipa, count, data) + 1;
1999b7aaff0bSAlex Elder 	if (!ipa->endpoint_count)
2000*0f97fbd4SAlex Elder 		return -EINVAL;
200184f9bd12SAlex Elder 
20029a9f5129SAlex Elder 	/* Initialize the defined endpoint bitmap */
20039a9f5129SAlex Elder 	ipa->defined = bitmap_zalloc(ipa->endpoint_count, GFP_KERNEL);
20049a9f5129SAlex Elder 	if (!ipa->defined)
2005*0f97fbd4SAlex Elder 		return -ENOMEM;
200684f9bd12SAlex Elder 
2007*0f97fbd4SAlex Elder 	filtered = 0;
200884f9bd12SAlex Elder 	for (name = 0; name < count; name++, data++) {
200984f9bd12SAlex Elder 		if (ipa_gsi_endpoint_data_empty(data))
201084f9bd12SAlex Elder 			continue;	/* Skip over empty slots */
201184f9bd12SAlex Elder 
201284f9bd12SAlex Elder 		ipa_endpoint_init_one(ipa, name, data);
201384f9bd12SAlex Elder 
201484f9bd12SAlex Elder 		if (data->endpoint.filter_support)
2015*0f97fbd4SAlex Elder 			filtered |= BIT(data->endpoint_id);
20162091c79aSAlex Elder 		if (data->ee_id == GSI_EE_MODEM && data->toward_ipa)
20172091c79aSAlex Elder 			ipa->modem_tx_count++;
201884f9bd12SAlex Elder 	}
201984f9bd12SAlex Elder 
2020*0f97fbd4SAlex Elder 	if (!ipa_filtered_valid(ipa, filtered))
202184f9bd12SAlex Elder 		goto err_endpoint_exit;
202284f9bd12SAlex Elder 
2023*0f97fbd4SAlex Elder 	ipa->filtered = filtered;
2024*0f97fbd4SAlex Elder 
2025*0f97fbd4SAlex Elder 	return 0;
202684f9bd12SAlex Elder 
202784f9bd12SAlex Elder err_endpoint_exit:
202884f9bd12SAlex Elder 	ipa_endpoint_exit(ipa);
202984f9bd12SAlex Elder 
2030*0f97fbd4SAlex Elder 	return -EINVAL;
203184f9bd12SAlex Elder }
2032