xref: /linux/drivers/net/ipa/ipa_endpoint.c (revision 2b87d721999a7591af7a63c1908084b1d59ba4e5)
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 
24684f9bd12SAlex Elder static bool ipa_endpoint_data_valid(struct ipa *ipa, u32 count,
24784f9bd12SAlex Elder 				    const struct ipa_gsi_endpoint_data *data)
24884f9bd12SAlex Elder {
24984f9bd12SAlex Elder 	const struct ipa_gsi_endpoint_data *dp = data;
25084f9bd12SAlex Elder 	struct device *dev = &ipa->pdev->dev;
25184f9bd12SAlex Elder 	enum ipa_endpoint_name name;
25284f9bd12SAlex Elder 
25384f9bd12SAlex Elder 	if (count > IPA_ENDPOINT_COUNT) {
25484f9bd12SAlex Elder 		dev_err(dev, "too many endpoints specified (%u > %u)\n",
25584f9bd12SAlex Elder 			count, IPA_ENDPOINT_COUNT);
25684f9bd12SAlex Elder 		return false;
25784f9bd12SAlex Elder 	}
25884f9bd12SAlex Elder 
25984f9bd12SAlex Elder 	/* Make sure needed endpoints have defined data */
26084f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_COMMAND_TX])) {
26184f9bd12SAlex Elder 		dev_err(dev, "command TX endpoint not defined\n");
26284f9bd12SAlex Elder 		return false;
26384f9bd12SAlex Elder 	}
26484f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_LAN_RX])) {
26584f9bd12SAlex Elder 		dev_err(dev, "LAN RX endpoint not defined\n");
26684f9bd12SAlex Elder 		return false;
26784f9bd12SAlex Elder 	}
26884f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_MODEM_TX])) {
26984f9bd12SAlex Elder 		dev_err(dev, "AP->modem TX endpoint not defined\n");
27084f9bd12SAlex Elder 		return false;
27184f9bd12SAlex Elder 	}
27284f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_MODEM_RX])) {
27384f9bd12SAlex Elder 		dev_err(dev, "AP<-modem RX endpoint not defined\n");
27484f9bd12SAlex Elder 		return false;
27584f9bd12SAlex Elder 	}
27684f9bd12SAlex Elder 
27784f9bd12SAlex Elder 	for (name = 0; name < count; name++, dp++)
27884f9bd12SAlex Elder 		if (!ipa_endpoint_data_valid_one(ipa, count, data, dp))
27984f9bd12SAlex Elder 			return false;
28084f9bd12SAlex Elder 
28184f9bd12SAlex Elder 	return true;
28284f9bd12SAlex Elder }
28384f9bd12SAlex Elder 
28484f9bd12SAlex Elder /* Allocate a transaction to use on a non-command endpoint */
28584f9bd12SAlex Elder static struct gsi_trans *ipa_endpoint_trans_alloc(struct ipa_endpoint *endpoint,
28684f9bd12SAlex Elder 						  u32 tre_count)
28784f9bd12SAlex Elder {
28884f9bd12SAlex Elder 	struct gsi *gsi = &endpoint->ipa->gsi;
28984f9bd12SAlex Elder 	u32 channel_id = endpoint->channel_id;
29084f9bd12SAlex Elder 	enum dma_data_direction direction;
29184f9bd12SAlex Elder 
29284f9bd12SAlex Elder 	direction = endpoint->toward_ipa ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
29384f9bd12SAlex Elder 
29484f9bd12SAlex Elder 	return gsi_channel_trans_alloc(gsi, channel_id, tre_count, direction);
29584f9bd12SAlex Elder }
29684f9bd12SAlex Elder 
29784f9bd12SAlex Elder /* suspend_delay represents suspend for RX, delay for TX endpoints.
2984c9d631aSAlex Elder  * Note that suspend is not supported starting with IPA v4.0, and
2994c9d631aSAlex Elder  * delay mode should not be used starting with IPA v4.2.
30084f9bd12SAlex Elder  */
3014900bf34SAlex Elder static bool
30284f9bd12SAlex Elder ipa_endpoint_init_ctrl(struct ipa_endpoint *endpoint, bool suspend_delay)
30384f9bd12SAlex Elder {
30484f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
3056a244b75SAlex Elder 	const struct ipa_reg *reg;
3064468a344SAlex Elder 	u32 field_id;
3076bfb7538SAlex Elder 	u32 offset;
3084900bf34SAlex Elder 	bool state;
30984f9bd12SAlex Elder 	u32 mask;
31084f9bd12SAlex Elder 	u32 val;
31184f9bd12SAlex Elder 
3125bc55884SAlex Elder 	if (endpoint->toward_ipa)
3134c9d631aSAlex Elder 		WARN_ON(ipa->version >= IPA_VERSION_4_2);
3145bc55884SAlex Elder 	else
3155bc55884SAlex Elder 		WARN_ON(ipa->version >= IPA_VERSION_4_0);
3165bc55884SAlex Elder 
3176a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_CTRL);
3186a244b75SAlex Elder 	offset = ipa_reg_n_offset(reg, endpoint->endpoint_id);
31984f9bd12SAlex Elder 	val = ioread32(ipa->reg_virt + offset);
3206a244b75SAlex Elder 
3214468a344SAlex Elder 	field_id = endpoint->toward_ipa ? ENDP_DELAY : ENDP_SUSPEND;
3224468a344SAlex Elder 	mask = ipa_reg_bit(reg, field_id);
3234468a344SAlex Elder 
3244900bf34SAlex Elder 	state = !!(val & mask);
3255bc55884SAlex Elder 
3265bc55884SAlex Elder 	/* Don't bother if it's already in the requested state */
3274900bf34SAlex Elder 	if (suspend_delay != state) {
32884f9bd12SAlex Elder 		val ^= mask;
32984f9bd12SAlex Elder 		iowrite32(val, ipa->reg_virt + offset);
3304900bf34SAlex Elder 	}
33184f9bd12SAlex Elder 
3324900bf34SAlex Elder 	return state;
33384f9bd12SAlex Elder }
33484f9bd12SAlex Elder 
3354c9d631aSAlex Elder /* We don't care what the previous state was for delay mode */
3364fa95248SAlex Elder static void
3374fa95248SAlex Elder ipa_endpoint_program_delay(struct ipa_endpoint *endpoint, bool enable)
3384fa95248SAlex Elder {
3394c9d631aSAlex Elder 	/* Delay mode should not be used for IPA v4.2+ */
3404c9d631aSAlex Elder 	WARN_ON(endpoint->ipa->version >= IPA_VERSION_4_2);
3415bc55884SAlex Elder 	WARN_ON(!endpoint->toward_ipa);
3424fa95248SAlex Elder 
3434fa95248SAlex Elder 	(void)ipa_endpoint_init_ctrl(endpoint, enable);
3444fa95248SAlex Elder }
3454fa95248SAlex Elder 
346fff89971SAlex Elder static bool ipa_endpoint_aggr_active(struct ipa_endpoint *endpoint)
347fff89971SAlex Elder {
348fff89971SAlex Elder 	u32 mask = BIT(endpoint->endpoint_id);
349fff89971SAlex Elder 	struct ipa *ipa = endpoint->ipa;
3506a244b75SAlex Elder 	const struct ipa_reg *reg;
351fff89971SAlex Elder 	u32 val;
352fff89971SAlex Elder 
3535bc55884SAlex Elder 	WARN_ON(!(mask & ipa->available));
3545bc55884SAlex Elder 
3556a244b75SAlex Elder 	reg = ipa_reg(ipa, STATE_AGGR_ACTIVE);
3566a244b75SAlex Elder 	val = ioread32(ipa->reg_virt + ipa_reg_offset(reg));
357fff89971SAlex Elder 
358fff89971SAlex Elder 	return !!(val & mask);
359fff89971SAlex Elder }
360fff89971SAlex Elder 
361fff89971SAlex Elder static void ipa_endpoint_force_close(struct ipa_endpoint *endpoint)
362fff89971SAlex Elder {
363fff89971SAlex Elder 	u32 mask = BIT(endpoint->endpoint_id);
364fff89971SAlex Elder 	struct ipa *ipa = endpoint->ipa;
3656a244b75SAlex Elder 	const struct ipa_reg *reg;
366fff89971SAlex Elder 
3675bc55884SAlex Elder 	WARN_ON(!(mask & ipa->available));
3685bc55884SAlex Elder 
3696a244b75SAlex Elder 	reg = ipa_reg(ipa, AGGR_FORCE_CLOSE);
3706a244b75SAlex Elder 	iowrite32(mask, ipa->reg_virt + ipa_reg_offset(reg));
371fff89971SAlex Elder }
372fff89971SAlex Elder 
373fff89971SAlex Elder /**
374fff89971SAlex Elder  * ipa_endpoint_suspend_aggr() - Emulate suspend interrupt
375e3eea08eSAlex Elder  * @endpoint:	Endpoint on which to emulate a suspend
376fff89971SAlex Elder  *
377fff89971SAlex Elder  *  Emulate suspend IPA interrupt to unsuspend an endpoint suspended
378fff89971SAlex Elder  *  with an open aggregation frame.  This is to work around a hardware
379fff89971SAlex Elder  *  issue in IPA version 3.5.1 where the suspend interrupt will not be
380fff89971SAlex Elder  *  generated when it should be.
381fff89971SAlex Elder  */
382fff89971SAlex Elder static void ipa_endpoint_suspend_aggr(struct ipa_endpoint *endpoint)
383fff89971SAlex Elder {
384fff89971SAlex Elder 	struct ipa *ipa = endpoint->ipa;
385fff89971SAlex Elder 
386660e52d6SAlex Elder 	if (!endpoint->config.aggregation)
387fff89971SAlex Elder 		return;
388fff89971SAlex Elder 
389fff89971SAlex Elder 	/* Nothing to do if the endpoint doesn't have aggregation open */
390fff89971SAlex Elder 	if (!ipa_endpoint_aggr_active(endpoint))
391fff89971SAlex Elder 		return;
392fff89971SAlex Elder 
393fff89971SAlex Elder 	/* Force close aggregation */
394fff89971SAlex Elder 	ipa_endpoint_force_close(endpoint);
395fff89971SAlex Elder 
396fff89971SAlex Elder 	ipa_interrupt_simulate_suspend(ipa->interrupt);
397fff89971SAlex Elder }
398fff89971SAlex Elder 
399fff89971SAlex Elder /* Returns previous suspend state (true means suspend was enabled) */
4004fa95248SAlex Elder static bool
4014fa95248SAlex Elder ipa_endpoint_program_suspend(struct ipa_endpoint *endpoint, bool enable)
4024fa95248SAlex Elder {
403fff89971SAlex Elder 	bool suspended;
404fff89971SAlex Elder 
405d7f3087bSAlex Elder 	if (endpoint->ipa->version >= IPA_VERSION_4_0)
406b07f283eSAlex Elder 		return enable;	/* For IPA v4.0+, no change made */
407b07f283eSAlex Elder 
4085bc55884SAlex Elder 	WARN_ON(endpoint->toward_ipa);
4094fa95248SAlex Elder 
410fff89971SAlex Elder 	suspended = ipa_endpoint_init_ctrl(endpoint, enable);
411fff89971SAlex Elder 
412fff89971SAlex Elder 	/* A client suspended with an open aggregation frame will not
413fff89971SAlex Elder 	 * generate a SUSPEND IPA interrupt.  If enabling suspend, have
414fff89971SAlex Elder 	 * ipa_endpoint_suspend_aggr() handle this.
415fff89971SAlex Elder 	 */
416fff89971SAlex Elder 	if (enable && !suspended)
417fff89971SAlex Elder 		ipa_endpoint_suspend_aggr(endpoint);
418fff89971SAlex Elder 
419fff89971SAlex Elder 	return suspended;
4204fa95248SAlex Elder }
4214fa95248SAlex Elder 
4224c9d631aSAlex Elder /* Put all modem RX endpoints into suspend mode, and stop transmission
4234c9d631aSAlex Elder  * on all modem TX endpoints.  Prior to IPA v4.2, endpoint DELAY mode is
4244c9d631aSAlex Elder  * used for TX endpoints; starting with IPA v4.2 we use GSI channel flow
4254c9d631aSAlex Elder  * control instead.
4264c9d631aSAlex Elder  */
42784f9bd12SAlex Elder void ipa_endpoint_modem_pause_all(struct ipa *ipa, bool enable)
42884f9bd12SAlex Elder {
42984f9bd12SAlex Elder 	u32 endpoint_id;
43084f9bd12SAlex Elder 
43184f9bd12SAlex Elder 	for (endpoint_id = 0; endpoint_id < IPA_ENDPOINT_MAX; endpoint_id++) {
43284f9bd12SAlex Elder 		struct ipa_endpoint *endpoint = &ipa->endpoint[endpoint_id];
43384f9bd12SAlex Elder 
43484f9bd12SAlex Elder 		if (endpoint->ee_id != GSI_EE_MODEM)
43584f9bd12SAlex Elder 			continue;
43684f9bd12SAlex Elder 
4374c9d631aSAlex Elder 		if (!endpoint->toward_ipa)
4384c9d631aSAlex Elder 			(void)ipa_endpoint_program_suspend(endpoint, enable);
4394c9d631aSAlex Elder 		else if (ipa->version < IPA_VERSION_4_2)
4404fa95248SAlex Elder 			ipa_endpoint_program_delay(endpoint, enable);
441b07f283eSAlex Elder 		else
4424c9d631aSAlex Elder 			gsi_modem_channel_flow_control(&ipa->gsi,
4434c9d631aSAlex Elder 						       endpoint->channel_id,
4444c9d631aSAlex Elder 						       enable);
44584f9bd12SAlex Elder 	}
44684f9bd12SAlex Elder }
44784f9bd12SAlex Elder 
44884f9bd12SAlex Elder /* Reset all modem endpoints to use the default exception endpoint */
44984f9bd12SAlex Elder int ipa_endpoint_modem_exception_reset_all(struct ipa *ipa)
45084f9bd12SAlex Elder {
45184f9bd12SAlex Elder 	u32 initialized = ipa->initialized;
45284f9bd12SAlex Elder 	struct gsi_trans *trans;
45384f9bd12SAlex Elder 	u32 count;
45484f9bd12SAlex Elder 
4552091c79aSAlex Elder 	/* We need one command per modem TX endpoint, plus the commands
4562091c79aSAlex Elder 	 * that clear the pipeline.
45784f9bd12SAlex Elder 	 */
4582091c79aSAlex Elder 	count = ipa->modem_tx_count + ipa_cmd_pipeline_clear_count();
45984f9bd12SAlex Elder 	trans = ipa_cmd_trans_alloc(ipa, count);
46084f9bd12SAlex Elder 	if (!trans) {
46184f9bd12SAlex Elder 		dev_err(&ipa->pdev->dev,
46284f9bd12SAlex Elder 			"no transaction to reset modem exception endpoints\n");
46384f9bd12SAlex Elder 		return -EBUSY;
46484f9bd12SAlex Elder 	}
46584f9bd12SAlex Elder 
46684f9bd12SAlex Elder 	while (initialized) {
46784f9bd12SAlex Elder 		u32 endpoint_id = __ffs(initialized);
46884f9bd12SAlex Elder 		struct ipa_endpoint *endpoint;
4696a244b75SAlex Elder 		const struct ipa_reg *reg;
47084f9bd12SAlex Elder 		u32 offset;
47184f9bd12SAlex Elder 
47284f9bd12SAlex Elder 		initialized ^= BIT(endpoint_id);
47384f9bd12SAlex Elder 
47484f9bd12SAlex Elder 		/* We only reset modem TX endpoints */
47584f9bd12SAlex Elder 		endpoint = &ipa->endpoint[endpoint_id];
47684f9bd12SAlex Elder 		if (!(endpoint->ee_id == GSI_EE_MODEM && endpoint->toward_ipa))
47784f9bd12SAlex Elder 			continue;
47884f9bd12SAlex Elder 
4796a244b75SAlex Elder 		reg = ipa_reg(ipa, ENDP_STATUS);
4806a244b75SAlex Elder 		offset = ipa_reg_n_offset(reg, endpoint_id);
48184f9bd12SAlex Elder 
48284f9bd12SAlex Elder 		/* Value written is 0, and all bits are updated.  That
48384f9bd12SAlex Elder 		 * means status is disabled on the endpoint, and as a
48484f9bd12SAlex Elder 		 * result all other fields in the register are ignored.
48584f9bd12SAlex Elder 		 */
48684f9bd12SAlex Elder 		ipa_cmd_register_write_add(trans, offset, 0, ~0, false);
48784f9bd12SAlex Elder 	}
48884f9bd12SAlex Elder 
489aa56e3e5SAlex Elder 	ipa_cmd_pipeline_clear_add(trans);
49084f9bd12SAlex Elder 
49184f9bd12SAlex Elder 	gsi_trans_commit_wait(trans);
49284f9bd12SAlex Elder 
49351c48ce2SAlex Elder 	ipa_cmd_pipeline_clear_wait(ipa);
49451c48ce2SAlex Elder 
49584f9bd12SAlex Elder 	return 0;
49684f9bd12SAlex Elder }
49784f9bd12SAlex Elder 
49884f9bd12SAlex Elder static void ipa_endpoint_init_cfg(struct ipa_endpoint *endpoint)
49984f9bd12SAlex Elder {
5006a244b75SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
5016bfb7538SAlex Elder 	struct ipa *ipa = endpoint->ipa;
5025567d4d9SAlex Elder 	enum ipa_cs_offload_en enabled;
5036a244b75SAlex Elder 	const struct ipa_reg *reg;
50484f9bd12SAlex Elder 	u32 val = 0;
5056bfb7538SAlex Elder 
5066a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_CFG);
50784f9bd12SAlex Elder 	/* FRAG_OFFLOAD_EN is 0 */
508660e52d6SAlex Elder 	if (endpoint->config.checksum) {
5096bfb7538SAlex Elder 		enum ipa_version version = ipa->version;
5105567d4d9SAlex Elder 
51184f9bd12SAlex Elder 		if (endpoint->toward_ipa) {
5129eefd2fbSAlex Elder 			u32 off;
51384f9bd12SAlex Elder 
51484f9bd12SAlex Elder 			/* Checksum header offset is in 4-byte units */
5154468a344SAlex Elder 			off = sizeof(struct rmnet_map_header) / sizeof(u32);
5164468a344SAlex Elder 			val |= ipa_reg_encode(reg, CS_METADATA_HDR_OFFSET, off);
5175567d4d9SAlex Elder 
5185567d4d9SAlex Elder 			enabled = version < IPA_VERSION_4_5
5195567d4d9SAlex Elder 					? IPA_CS_OFFLOAD_UL
5205567d4d9SAlex Elder 					: IPA_CS_OFFLOAD_INLINE;
52184f9bd12SAlex Elder 		} else {
5225567d4d9SAlex Elder 			enabled = version < IPA_VERSION_4_5
5235567d4d9SAlex Elder 					? IPA_CS_OFFLOAD_DL
5245567d4d9SAlex Elder 					: IPA_CS_OFFLOAD_INLINE;
52584f9bd12SAlex Elder 		}
52684f9bd12SAlex Elder 	} else {
5275567d4d9SAlex Elder 		enabled = IPA_CS_OFFLOAD_NONE;
52884f9bd12SAlex Elder 	}
5294468a344SAlex Elder 	val |= ipa_reg_encode(reg, CS_OFFLOAD_EN, enabled);
53084f9bd12SAlex Elder 	/* CS_GEN_QMB_MASTER_SEL is 0 */
53184f9bd12SAlex Elder 
5326a244b75SAlex Elder 	iowrite32(val, ipa->reg_virt + ipa_reg_n_offset(reg, endpoint_id));
53384f9bd12SAlex Elder }
53484f9bd12SAlex Elder 
535647a05f3SAlex Elder static void ipa_endpoint_init_nat(struct ipa_endpoint *endpoint)
536647a05f3SAlex Elder {
5376a244b75SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
5386bfb7538SAlex Elder 	struct ipa *ipa = endpoint->ipa;
5396a244b75SAlex Elder 	const struct ipa_reg *reg;
540647a05f3SAlex Elder 	u32 val;
541647a05f3SAlex Elder 
542647a05f3SAlex Elder 	if (!endpoint->toward_ipa)
543647a05f3SAlex Elder 		return;
544647a05f3SAlex Elder 
5456a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_NAT);
5464468a344SAlex Elder 	val = ipa_reg_encode(reg, NAT_EN, IPA_NAT_BYPASS);
547647a05f3SAlex Elder 
5486a244b75SAlex Elder 	iowrite32(val, ipa->reg_virt + ipa_reg_n_offset(reg, endpoint_id));
549647a05f3SAlex Elder }
550647a05f3SAlex Elder 
5515567d4d9SAlex Elder static u32
5525567d4d9SAlex Elder ipa_qmap_header_size(enum ipa_version version, struct ipa_endpoint *endpoint)
5535567d4d9SAlex Elder {
5545567d4d9SAlex Elder 	u32 header_size = sizeof(struct rmnet_map_header);
5555567d4d9SAlex Elder 
5565567d4d9SAlex Elder 	/* Without checksum offload, we just have the MAP header */
557660e52d6SAlex Elder 	if (!endpoint->config.checksum)
5585567d4d9SAlex Elder 		return header_size;
5595567d4d9SAlex Elder 
5605567d4d9SAlex Elder 	if (version < IPA_VERSION_4_5) {
5615567d4d9SAlex Elder 		/* Checksum header inserted for AP TX endpoints only */
5625567d4d9SAlex Elder 		if (endpoint->toward_ipa)
5635567d4d9SAlex Elder 			header_size += sizeof(struct rmnet_map_ul_csum_header);
5645567d4d9SAlex Elder 	} else {
5655567d4d9SAlex Elder 		/* Checksum header is used in both directions */
5665567d4d9SAlex Elder 		header_size += sizeof(struct rmnet_map_v5_csum_header);
5675567d4d9SAlex Elder 	}
5685567d4d9SAlex Elder 
5695567d4d9SAlex Elder 	return header_size;
5705567d4d9SAlex Elder }
5715567d4d9SAlex Elder 
5724468a344SAlex Elder /* Encoded value for ENDP_INIT_HDR register HDR_LEN* field(s) */
5734468a344SAlex Elder static u32 ipa_header_size_encode(enum ipa_version version,
5744468a344SAlex Elder 				  const struct ipa_reg *reg, u32 header_size)
5754468a344SAlex Elder {
5764468a344SAlex Elder 	u32 field_max = ipa_reg_field_max(reg, HDR_LEN);
5774468a344SAlex Elder 	u32 val;
5784468a344SAlex Elder 
5794468a344SAlex Elder 	/* We know field_max can be used as a mask (2^n - 1) */
5804468a344SAlex Elder 	val = ipa_reg_encode(reg, HDR_LEN, header_size & field_max);
5814468a344SAlex Elder 	if (version < IPA_VERSION_4_5) {
5824468a344SAlex Elder 		WARN_ON(header_size > field_max);
5834468a344SAlex Elder 		return val;
5844468a344SAlex Elder 	}
5854468a344SAlex Elder 
5864468a344SAlex Elder 	/* IPA v4.5 adds a few more most-significant bits */
5874468a344SAlex Elder 	header_size >>= hweight32(field_max);
5884468a344SAlex Elder 	WARN_ON(header_size > ipa_reg_field_max(reg, HDR_LEN_MSB));
5894468a344SAlex Elder 	val |= ipa_reg_encode(reg, HDR_LEN_MSB, header_size);
5904468a344SAlex Elder 
5914468a344SAlex Elder 	return val;
5924468a344SAlex Elder }
5934468a344SAlex Elder 
5944468a344SAlex Elder /* Encoded value for ENDP_INIT_HDR register OFST_METADATA* field(s) */
5954468a344SAlex Elder static u32 ipa_metadata_offset_encode(enum ipa_version version,
5964468a344SAlex Elder 				      const struct ipa_reg *reg, u32 offset)
5974468a344SAlex Elder {
5984468a344SAlex Elder 	u32 field_max = ipa_reg_field_max(reg, HDR_OFST_METADATA);
5994468a344SAlex Elder 	u32 val;
6004468a344SAlex Elder 
6014468a344SAlex Elder 	/* We know field_max can be used as a mask (2^n - 1) */
6024468a344SAlex Elder 	val = ipa_reg_encode(reg, HDR_OFST_METADATA, offset);
6034468a344SAlex Elder 	if (version < IPA_VERSION_4_5) {
6044468a344SAlex Elder 		WARN_ON(offset > field_max);
6054468a344SAlex Elder 		return val;
6064468a344SAlex Elder 	}
6074468a344SAlex Elder 
6084468a344SAlex Elder 	/* IPA v4.5 adds a few more most-significant bits */
6094468a344SAlex Elder 	offset >>= hweight32(field_max);
6104468a344SAlex Elder 	WARN_ON(offset > ipa_reg_field_max(reg, HDR_OFST_METADATA_MSB));
6114468a344SAlex Elder 	val |= ipa_reg_encode(reg, HDR_OFST_METADATA_MSB, offset);
6124468a344SAlex Elder 
6134468a344SAlex Elder 	return val;
6144468a344SAlex Elder }
6154468a344SAlex Elder 
6168730f45dSAlex Elder /**
617e3eea08eSAlex Elder  * ipa_endpoint_init_hdr() - Initialize HDR endpoint configuration register
618e3eea08eSAlex Elder  * @endpoint:	Endpoint pointer
619e3eea08eSAlex Elder  *
6208730f45dSAlex Elder  * We program QMAP endpoints so each packet received is preceded by a QMAP
6218730f45dSAlex Elder  * header structure.  The QMAP header contains a 1-byte mux_id and 2-byte
6228730f45dSAlex Elder  * packet size field, and we have the IPA hardware populate both for each
6238730f45dSAlex Elder  * received packet.  The header is configured (in the HDR_EXT register)
6248730f45dSAlex Elder  * to use big endian format.
6258730f45dSAlex Elder  *
6268730f45dSAlex Elder  * The packet size is written into the QMAP header's pkt_len field.  That
6278730f45dSAlex Elder  * location is defined here using the HDR_OFST_PKT_SIZE field.
6288730f45dSAlex Elder  *
6298730f45dSAlex Elder  * The mux_id comes from a 4-byte metadata value supplied with each packet
6308730f45dSAlex Elder  * by the modem.  It is *not* a QMAP header, but it does contain the mux_id
6318730f45dSAlex Elder  * value that we want, in its low-order byte.  A bitmask defined in the
6328730f45dSAlex Elder  * endpoint's METADATA_MASK register defines which byte within the modem
6338730f45dSAlex Elder  * metadata contains the mux_id.  And the OFST_METADATA field programmed
6348730f45dSAlex Elder  * here indicates where the extracted byte should be placed within the QMAP
6358730f45dSAlex Elder  * header.
6368730f45dSAlex Elder  */
63784f9bd12SAlex Elder static void ipa_endpoint_init_hdr(struct ipa_endpoint *endpoint)
63884f9bd12SAlex Elder {
6396a244b75SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
6401af15c2aSAlex Elder 	struct ipa *ipa = endpoint->ipa;
6416a244b75SAlex Elder 	const struct ipa_reg *reg;
64284f9bd12SAlex Elder 	u32 val = 0;
6436bfb7538SAlex Elder 
6446a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_HDR);
645660e52d6SAlex Elder 	if (endpoint->config.qmap) {
6461af15c2aSAlex Elder 		enum ipa_version version = ipa->version;
6475567d4d9SAlex Elder 		size_t header_size;
64884f9bd12SAlex Elder 
6495567d4d9SAlex Elder 		header_size = ipa_qmap_header_size(version, endpoint);
6504468a344SAlex Elder 		val = ipa_header_size_encode(version, reg, header_size);
65184f9bd12SAlex Elder 
652f330fda3SAlex Elder 		/* Define how to fill fields in a received QMAP header */
6538730f45dSAlex Elder 		if (!endpoint->toward_ipa) {
6549eefd2fbSAlex Elder 			u32 off;     /* Field offset within header */
6558730f45dSAlex Elder 
6568730f45dSAlex Elder 			/* Where IPA will write the metadata value */
6579eefd2fbSAlex Elder 			off = offsetof(struct rmnet_map_header, mux_id);
6584468a344SAlex Elder 			val |= ipa_metadata_offset_encode(version, reg, off);
6598730f45dSAlex Elder 
6608730f45dSAlex Elder 			/* Where IPA will write the length */
6619eefd2fbSAlex Elder 			off = offsetof(struct rmnet_map_header, pkt_len);
6621af15c2aSAlex Elder 			/* Upper bits are stored in HDR_EXT with IPA v4.5 */
663d7f3087bSAlex Elder 			if (version >= IPA_VERSION_4_5)
6644468a344SAlex Elder 				off &= ipa_reg_field_max(reg, HDR_OFST_PKT_SIZE);
6651af15c2aSAlex Elder 
6664468a344SAlex Elder 			val |= ipa_reg_bit(reg, HDR_OFST_PKT_SIZE_VALID);
6674468a344SAlex Elder 			val |= ipa_reg_encode(reg, HDR_OFST_PKT_SIZE, off);
66884f9bd12SAlex Elder 		}
6698730f45dSAlex Elder 		/* For QMAP TX, metadata offset is 0 (modem assumes this) */
6704468a344SAlex Elder 		val |= ipa_reg_bit(reg, HDR_OFST_METADATA_VALID);
6718730f45dSAlex Elder 
6728730f45dSAlex Elder 		/* HDR_ADDITIONAL_CONST_LEN is 0; (RX only) */
67384f9bd12SAlex Elder 		/* HDR_A5_MUX is 0 */
67484f9bd12SAlex Elder 		/* HDR_LEN_INC_DEAGG_HDR is 0 */
6758bfc4e21SAlex Elder 		/* HDR_METADATA_REG_VALID is 0 (TX only, version < v4.5) */
67684f9bd12SAlex Elder 	}
67784f9bd12SAlex Elder 
6786a244b75SAlex Elder 	iowrite32(val, ipa->reg_virt + ipa_reg_n_offset(reg, endpoint_id));
67984f9bd12SAlex Elder }
68084f9bd12SAlex Elder 
68184f9bd12SAlex Elder static void ipa_endpoint_init_hdr_ext(struct ipa_endpoint *endpoint)
68284f9bd12SAlex Elder {
683660e52d6SAlex Elder 	u32 pad_align = endpoint->config.rx.pad_align;
6846a244b75SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
6851af15c2aSAlex Elder 	struct ipa *ipa = endpoint->ipa;
6866a244b75SAlex Elder 	const struct ipa_reg *reg;
68784f9bd12SAlex Elder 	u32 val = 0;
6886bfb7538SAlex Elder 
6896a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_HDR_EXT);
690660e52d6SAlex Elder 	if (endpoint->config.qmap) {
691332ef7c8SAlex Elder 		/* We have a header, so we must specify its endianness */
6924468a344SAlex Elder 		val |= ipa_reg_bit(reg, HDR_ENDIANNESS);	/* big endian */
693f330fda3SAlex Elder 
694332ef7c8SAlex Elder 		/* A QMAP header contains a 6 bit pad field at offset 0.
695332ef7c8SAlex Elder 		 * The RMNet driver assumes this field is meaningful in
696332ef7c8SAlex Elder 		 * packets it receives, and assumes the header's payload
697332ef7c8SAlex Elder 		 * length includes that padding.  The RMNet driver does
698332ef7c8SAlex Elder 		 * *not* pad packets it sends, however, so the pad field
699332ef7c8SAlex Elder 		 * (although 0) should be ignored.
700f330fda3SAlex Elder 		 */
701332ef7c8SAlex Elder 		if (!endpoint->toward_ipa) {
7024468a344SAlex Elder 			val |= ipa_reg_bit(reg, HDR_TOTAL_LEN_OR_PAD_VALID);
70384f9bd12SAlex Elder 			/* HDR_TOTAL_LEN_OR_PAD is 0 (pad, not total_len) */
7044468a344SAlex Elder 			val |= ipa_reg_bit(reg, HDR_PAYLOAD_LEN_INC_PADDING);
70584f9bd12SAlex Elder 			/* HDR_TOTAL_LEN_OR_PAD_OFFSET is 0 */
706f330fda3SAlex Elder 		}
707332ef7c8SAlex Elder 	}
708f330fda3SAlex Elder 
709f330fda3SAlex Elder 	/* HDR_PAYLOAD_LEN_INC_PADDING is 0 */
71084f9bd12SAlex Elder 	if (!endpoint->toward_ipa)
7114468a344SAlex Elder 		val |= ipa_reg_encode(reg, HDR_PAD_TO_ALIGNMENT, pad_align);
71284f9bd12SAlex Elder 
7131af15c2aSAlex Elder 	/* IPA v4.5 adds some most-significant bits to a few fields,
7141af15c2aSAlex Elder 	 * two of which are defined in the HDR (not HDR_EXT) register.
7151af15c2aSAlex Elder 	 */
716d7f3087bSAlex Elder 	if (ipa->version >= IPA_VERSION_4_5) {
7171af15c2aSAlex Elder 		/* HDR_TOTAL_LEN_OR_PAD_OFFSET is 0, so MSB is 0 */
718660e52d6SAlex Elder 		if (endpoint->config.qmap && !endpoint->toward_ipa) {
7194468a344SAlex Elder 			u32 mask = ipa_reg_field_max(reg, HDR_OFST_PKT_SIZE);
7206bfb7538SAlex Elder 			u32 off;     /* Field offset within header */
72184f9bd12SAlex Elder 
7229eefd2fbSAlex Elder 			off = offsetof(struct rmnet_map_header, pkt_len);
7234468a344SAlex Elder 			/* Low bits are in the ENDP_INIT_HDR register */
7244468a344SAlex Elder 			off >>= hweight32(mask);
7254468a344SAlex Elder 			val |= ipa_reg_encode(reg, HDR_OFST_PKT_SIZE_MSB, off);
7261af15c2aSAlex Elder 			/* HDR_ADDITIONAL_CONST_LEN is 0 so MSB is 0 */
7271af15c2aSAlex Elder 		}
7281af15c2aSAlex Elder 	}
7296bfb7538SAlex Elder 
7306a244b75SAlex Elder 	iowrite32(val, ipa->reg_virt + ipa_reg_n_offset(reg, endpoint_id));
7311af15c2aSAlex Elder }
73284f9bd12SAlex Elder 
73384f9bd12SAlex Elder static void ipa_endpoint_init_hdr_metadata_mask(struct ipa_endpoint *endpoint)
73484f9bd12SAlex Elder {
73584f9bd12SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
7366bfb7538SAlex Elder 	struct ipa *ipa = endpoint->ipa;
7376a244b75SAlex Elder 	const struct ipa_reg *reg;
73884f9bd12SAlex Elder 	u32 val = 0;
73984f9bd12SAlex Elder 	u32 offset;
74084f9bd12SAlex Elder 
741fb57c3eaSAlex Elder 	if (endpoint->toward_ipa)
742fb57c3eaSAlex Elder 		return;		/* Register not valid for TX endpoints */
743fb57c3eaSAlex Elder 
7446a244b75SAlex Elder 	reg = ipa_reg(ipa,  ENDP_INIT_HDR_METADATA_MASK);
7456a244b75SAlex Elder 	offset = ipa_reg_n_offset(reg, endpoint_id);
74684f9bd12SAlex Elder 
7478730f45dSAlex Elder 	/* Note that HDR_ENDIANNESS indicates big endian header fields */
748660e52d6SAlex Elder 	if (endpoint->config.qmap)
749088f8a23SAlex Elder 		val = (__force u32)cpu_to_be32(IPA_ENDPOINT_QMAP_METADATA_MASK);
75084f9bd12SAlex Elder 
7516bfb7538SAlex Elder 	iowrite32(val, ipa->reg_virt + offset);
75284f9bd12SAlex Elder }
75384f9bd12SAlex Elder 
75484f9bd12SAlex Elder static void ipa_endpoint_init_mode(struct ipa_endpoint *endpoint)
75584f9bd12SAlex Elder {
7566bfb7538SAlex Elder 	struct ipa *ipa = endpoint->ipa;
7576a244b75SAlex Elder 	const struct ipa_reg *reg;
7586bfb7538SAlex Elder 	u32 offset;
75984f9bd12SAlex Elder 	u32 val;
76084f9bd12SAlex Elder 
761fb57c3eaSAlex Elder 	if (!endpoint->toward_ipa)
762fb57c3eaSAlex Elder 		return;		/* Register not valid for RX endpoints */
763fb57c3eaSAlex Elder 
7646a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_MODE);
765660e52d6SAlex Elder 	if (endpoint->config.dma_mode) {
766660e52d6SAlex Elder 		enum ipa_endpoint_name name = endpoint->config.dma_endpoint;
767216b409dSAlex Elder 		u32 dma_endpoint_id = ipa->name_map[name]->endpoint_id;
76884f9bd12SAlex Elder 
769216b409dSAlex Elder 		val = ipa_reg_encode(reg, ENDP_MODE, IPA_DMA);
770216b409dSAlex Elder 		val |= ipa_reg_encode(reg, DEST_PIPE_INDEX, dma_endpoint_id);
77184f9bd12SAlex Elder 	} else {
772216b409dSAlex Elder 		val = ipa_reg_encode(reg, ENDP_MODE, IPA_BASIC);
77384f9bd12SAlex Elder 	}
77400b9102aSAlex Elder 	/* All other bits unspecified (and 0) */
77584f9bd12SAlex Elder 
776216b409dSAlex Elder 	offset = ipa_reg_n_offset(reg, endpoint->endpoint_id);
7776bfb7538SAlex Elder 	iowrite32(val, ipa->reg_virt + offset);
77884f9bd12SAlex Elder }
77984f9bd12SAlex Elder 
7808be440e1SAlex Elder /* For IPA v4.5+, times are expressed using Qtime.  The AP uses one of two
7818be440e1SAlex Elder  * pulse generators (0 and 1) to measure elapsed time.  In ipa_qtime_config()
7828be440e1SAlex Elder  * they're configured to have granularity 100 usec and 1 msec, respectively.
7838be440e1SAlex Elder  *
7848be440e1SAlex Elder  * The return value is the positive or negative Qtime value to use to
7858be440e1SAlex Elder  * express the (microsecond) time provided.  A positive return value
7868be440e1SAlex Elder  * means pulse generator 0 can be used; otherwise use pulse generator 1.
7878be440e1SAlex Elder  */
7888be440e1SAlex Elder static int ipa_qtime_val(u32 microseconds, u32 max)
7898be440e1SAlex Elder {
7908be440e1SAlex Elder 	u32 val;
7918be440e1SAlex Elder 
7928be440e1SAlex Elder 	/* Use 100 microsecond granularity if possible */
7938be440e1SAlex Elder 	val = DIV_ROUND_CLOSEST(microseconds, 100);
7948be440e1SAlex Elder 	if (val <= max)
7958be440e1SAlex Elder 		return (int)val;
7968be440e1SAlex Elder 
7978be440e1SAlex Elder 	/* Have to use pulse generator 1 (millisecond granularity) */
7988be440e1SAlex Elder 	val = DIV_ROUND_CLOSEST(microseconds, 1000);
7998be440e1SAlex Elder 	WARN_ON(val > max);
8008be440e1SAlex Elder 
8018be440e1SAlex Elder 	return (int)-val;
8028be440e1SAlex Elder }
8038be440e1SAlex Elder 
80419547041SAlex Elder /* Encode the aggregation timer limit (microseconds) based on IPA version */
805216b409dSAlex Elder static u32 aggr_time_limit_encode(struct ipa *ipa, const struct ipa_reg *reg,
806216b409dSAlex Elder 				  u32 microseconds)
8076bf754c7SAlex Elder {
808216b409dSAlex Elder 	u32 max;
80919547041SAlex Elder 	u32 val;
81048395fa8SAlex Elder 
81148395fa8SAlex Elder 	if (!microseconds)
81248395fa8SAlex Elder 		return 0;	/* Nothing to compute if time limit is 0 */
81348395fa8SAlex Elder 
814216b409dSAlex Elder 	max = ipa_reg_field_max(reg, TIME_LIMIT);
815216b409dSAlex Elder 	if (ipa->version >= IPA_VERSION_4_5) {
81648395fa8SAlex Elder 		u32 gran_sel;
8178be440e1SAlex Elder 		int ret;
8186bf754c7SAlex Elder 
8198be440e1SAlex Elder 		/* Compute the Qtime limit value to use */
820216b409dSAlex Elder 		ret = ipa_qtime_val(microseconds, max);
8218be440e1SAlex Elder 		if (ret < 0) {
8228be440e1SAlex Elder 			val = -ret;
823216b409dSAlex Elder 			gran_sel = ipa_reg_bit(reg, AGGR_GRAN_SEL);
82419547041SAlex Elder 		} else {
8258be440e1SAlex Elder 			val = ret;
82619547041SAlex Elder 			gran_sel = 0;
82719547041SAlex Elder 		}
82819547041SAlex Elder 
829216b409dSAlex Elder 		return gran_sel | ipa_reg_encode(reg, TIME_LIMIT, val);
8306bf754c7SAlex Elder 	}
8316bf754c7SAlex Elder 
832216b409dSAlex Elder 	/* We program aggregation granularity in ipa_hardware_config() */
83348395fa8SAlex Elder 	val = DIV_ROUND_CLOSEST(microseconds, IPA_AGGR_GRANULARITY);
834216b409dSAlex Elder 	WARN(val > max, "aggr_time_limit too large (%u > %u usec)\n",
835216b409dSAlex Elder 	     microseconds, max * IPA_AGGR_GRANULARITY);
83648395fa8SAlex Elder 
837216b409dSAlex Elder 	return ipa_reg_encode(reg, TIME_LIMIT, val);
8386bf754c7SAlex Elder }
8396bf754c7SAlex Elder 
84084f9bd12SAlex Elder static void ipa_endpoint_init_aggr(struct ipa_endpoint *endpoint)
84184f9bd12SAlex Elder {
8426a244b75SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
8436bfb7538SAlex Elder 	struct ipa *ipa = endpoint->ipa;
8446a244b75SAlex Elder 	const struct ipa_reg *reg;
84584f9bd12SAlex Elder 	u32 val = 0;
8466bfb7538SAlex Elder 
8476a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_AGGR);
848660e52d6SAlex Elder 	if (endpoint->config.aggregation) {
84984f9bd12SAlex Elder 		if (!endpoint->toward_ipa) {
850cf4e73a1SAlex Elder 			const struct ipa_endpoint_rx *rx_config;
851c5794097SAlex Elder 			u32 buffer_size;
85284f9bd12SAlex Elder 			u32 limit;
85384f9bd12SAlex Elder 
854660e52d6SAlex Elder 			rx_config = &endpoint->config.rx;
855216b409dSAlex Elder 			val |= ipa_reg_encode(reg, AGGR_EN, IPA_ENABLE_AGGR);
856216b409dSAlex Elder 			val |= ipa_reg_encode(reg, AGGR_TYPE, IPA_GENERIC);
8579e88cb5fSAlex Elder 
858cf4e73a1SAlex Elder 			buffer_size = rx_config->buffer_size;
8593cebb7c2SAlex Elder 			limit = ipa_aggr_size_kb(buffer_size - NET_SKB_PAD,
8603cebb7c2SAlex Elder 						 rx_config->aggr_hard_limit);
861216b409dSAlex Elder 			val |= ipa_reg_encode(reg, BYTE_LIMIT, limit);
8621d86652bSAlex Elder 
863beb90cbaSAlex Elder 			limit = rx_config->aggr_time_limit;
864216b409dSAlex Elder 			val |= aggr_time_limit_encode(ipa, reg, limit);
8651d86652bSAlex Elder 
8669e88cb5fSAlex Elder 			/* AGGR_PKT_LIMIT is 0 (unlimited) */
8679e88cb5fSAlex Elder 
868216b409dSAlex Elder 			if (rx_config->aggr_close_eof)
869216b409dSAlex Elder 				val |= ipa_reg_bit(reg, SW_EOF_ACTIVE);
87084f9bd12SAlex Elder 		} else {
871216b409dSAlex Elder 			val |= ipa_reg_encode(reg, AGGR_EN, IPA_ENABLE_DEAGGR);
872216b409dSAlex Elder 			val |= ipa_reg_encode(reg, AGGR_TYPE, IPA_QCMAP);
87384f9bd12SAlex Elder 			/* other fields ignored */
87484f9bd12SAlex Elder 		}
87584f9bd12SAlex Elder 		/* AGGR_FORCE_CLOSE is 0 */
8768bfc4e21SAlex Elder 		/* AGGR_GRAN_SEL is 0 for IPA v4.5 */
87784f9bd12SAlex Elder 	} else {
878216b409dSAlex Elder 		val |= ipa_reg_encode(reg, AGGR_EN, IPA_BYPASS_AGGR);
87984f9bd12SAlex Elder 		/* other fields ignored */
88084f9bd12SAlex Elder 	}
88184f9bd12SAlex Elder 
8826a244b75SAlex Elder 	iowrite32(val, ipa->reg_virt + ipa_reg_n_offset(reg, endpoint_id));
88384f9bd12SAlex Elder }
88484f9bd12SAlex Elder 
88563e5afc8SAlex Elder /* The head-of-line blocking timer is defined as a tick count.  For
88663e5afc8SAlex Elder  * IPA version 4.5 the tick count is based on the Qtimer, which is
88763e5afc8SAlex Elder  * derived from the 19.2 MHz SoC XO clock.  For older IPA versions
88863e5afc8SAlex Elder  * each tick represents 128 cycles of the IPA core clock.
88963e5afc8SAlex Elder  *
8908be440e1SAlex Elder  * Return the encoded value representing the timeout period provided
8918be440e1SAlex Elder  * that should be written to the ENDP_INIT_HOL_BLOCK_TIMER register.
89263e5afc8SAlex Elder  */
893216b409dSAlex Elder static u32 hol_block_timer_encode(struct ipa *ipa, const struct ipa_reg *reg,
894216b409dSAlex Elder 				  u32 microseconds)
89584f9bd12SAlex Elder {
896f13a8c31SAlex Elder 	u32 width;
89784f9bd12SAlex Elder 	u32 scale;
898f13a8c31SAlex Elder 	u64 ticks;
899f13a8c31SAlex Elder 	u64 rate;
900f13a8c31SAlex Elder 	u32 high;
90184f9bd12SAlex Elder 	u32 val;
90284f9bd12SAlex Elder 
90384f9bd12SAlex Elder 	if (!microseconds)
904f13a8c31SAlex Elder 		return 0;	/* Nothing to compute if timer period is 0 */
90584f9bd12SAlex Elder 
90648395fa8SAlex Elder 	if (ipa->version >= IPA_VERSION_4_5) {
907216b409dSAlex Elder 		u32 max = ipa_reg_field_max(reg, TIMER_LIMIT);
90848395fa8SAlex Elder 		u32 gran_sel;
90948395fa8SAlex Elder 		int ret;
91048395fa8SAlex Elder 
91148395fa8SAlex Elder 		/* Compute the Qtime limit value to use */
912216b409dSAlex Elder 		ret = ipa_qtime_val(microseconds, max);
91348395fa8SAlex Elder 		if (ret < 0) {
91448395fa8SAlex Elder 			val = -ret;
915216b409dSAlex Elder 			gran_sel = ipa_reg_bit(reg, TIMER_GRAN_SEL);
91648395fa8SAlex Elder 		} else {
91748395fa8SAlex Elder 			val = ret;
91848395fa8SAlex Elder 			gran_sel = 0;
91948395fa8SAlex Elder 		}
92048395fa8SAlex Elder 
921216b409dSAlex Elder 		return gran_sel | ipa_reg_encode(reg, TIMER_LIMIT, val);
92248395fa8SAlex Elder 	}
92363e5afc8SAlex Elder 
924216b409dSAlex Elder 	/* Use 64 bit arithmetic to avoid overflow */
9257aa0e8b8SAlex Elder 	rate = ipa_core_clock_rate(ipa);
926f13a8c31SAlex Elder 	ticks = DIV_ROUND_CLOSEST(microseconds * rate, 128 * USEC_PER_SEC);
927216b409dSAlex Elder 
928216b409dSAlex Elder 	/* We still need the result to fit into the field */
929216b409dSAlex Elder 	WARN_ON(ticks > ipa_reg_field_max(reg, TIMER_BASE_VALUE));
93084f9bd12SAlex Elder 
9316833a096SAlex Elder 	/* IPA v3.5.1 through v4.1 just record the tick count */
9326833a096SAlex Elder 	if (ipa->version < IPA_VERSION_4_2)
933216b409dSAlex Elder 		return ipa_reg_encode(reg, TIMER_BASE_VALUE, (u32)ticks);
93484f9bd12SAlex Elder 
935f13a8c31SAlex Elder 	/* For IPA v4.2, the tick count is represented by base and
936f13a8c31SAlex Elder 	 * scale fields within the 32-bit timer register, where:
937f13a8c31SAlex Elder 	 *     ticks = base << scale;
938f13a8c31SAlex Elder 	 * The best precision is achieved when the base value is as
939f13a8c31SAlex Elder 	 * large as possible.  Find the highest set bit in the tick
940f13a8c31SAlex Elder 	 * count, and extract the number of bits in the base field
941497abc87SPeng Li 	 * such that high bit is included.
942f13a8c31SAlex Elder 	 */
943216b409dSAlex Elder 	high = fls(ticks);		/* 1..32 (or warning above) */
944216b409dSAlex Elder 	width = hweight32(ipa_reg_fmask(reg, TIMER_BASE_VALUE));
945f13a8c31SAlex Elder 	scale = high > width ? high - width : 0;
946f13a8c31SAlex Elder 	if (scale) {
947f13a8c31SAlex Elder 		/* If we're scaling, round up to get a closer result */
948f13a8c31SAlex Elder 		ticks += 1 << (scale - 1);
949f13a8c31SAlex Elder 		/* High bit was set, so rounding might have affected it */
950f13a8c31SAlex Elder 		if (fls(ticks) != high)
951f13a8c31SAlex Elder 			scale++;
952f13a8c31SAlex Elder 	}
95384f9bd12SAlex Elder 
954216b409dSAlex Elder 	val = ipa_reg_encode(reg, TIMER_SCALE, scale);
955216b409dSAlex Elder 	val |= ipa_reg_encode(reg, TIMER_BASE_VALUE, (u32)ticks >> scale);
95684f9bd12SAlex Elder 
95784f9bd12SAlex Elder 	return val;
95884f9bd12SAlex Elder }
95984f9bd12SAlex Elder 
960f13a8c31SAlex Elder /* If microseconds is 0, timeout is immediate */
961f13a8c31SAlex Elder static void ipa_endpoint_init_hol_block_timer(struct ipa_endpoint *endpoint,
96284f9bd12SAlex Elder 					      u32 microseconds)
96384f9bd12SAlex Elder {
96484f9bd12SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
96584f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
9666a244b75SAlex Elder 	const struct ipa_reg *reg;
96784f9bd12SAlex Elder 	u32 val;
96884f9bd12SAlex Elder 
969816316caSAlex Elder 	/* This should only be changed when HOL_BLOCK_EN is disabled */
9706a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_HOL_BLOCK_TIMER);
971216b409dSAlex Elder 	val = hol_block_timer_encode(ipa, reg, microseconds);
9726bfb7538SAlex Elder 
9736a244b75SAlex Elder 	iowrite32(val, ipa->reg_virt + ipa_reg_n_offset(reg, endpoint_id));
97484f9bd12SAlex Elder }
97584f9bd12SAlex Elder 
97684f9bd12SAlex Elder static void
977e6aab6b9SAlex Elder ipa_endpoint_init_hol_block_en(struct ipa_endpoint *endpoint, bool enable)
97884f9bd12SAlex Elder {
97984f9bd12SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
9806bfb7538SAlex Elder 	struct ipa *ipa = endpoint->ipa;
9816a244b75SAlex Elder 	const struct ipa_reg *reg;
98284f9bd12SAlex Elder 	u32 offset;
98384f9bd12SAlex Elder 	u32 val;
98484f9bd12SAlex Elder 
9856a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_HOL_BLOCK_EN);
9866a244b75SAlex Elder 	offset = ipa_reg_n_offset(reg, endpoint_id);
987216b409dSAlex Elder 	val = enable ? ipa_reg_bit(reg, HOL_BLOCK_EN) : 0;
9886bfb7538SAlex Elder 
9896bfb7538SAlex Elder 	iowrite32(val, ipa->reg_virt + offset);
9906bfb7538SAlex Elder 
9916e228d8cSAlex Elder 	/* When enabling, the register must be written twice for IPA v4.5+ */
9926bfb7538SAlex Elder 	if (enable && ipa->version >= IPA_VERSION_4_5)
9936bfb7538SAlex Elder 		iowrite32(val, ipa->reg_virt + offset);
99484f9bd12SAlex Elder }
99584f9bd12SAlex Elder 
996e6aab6b9SAlex Elder /* Assumes HOL_BLOCK is in disabled state */
997e6aab6b9SAlex Elder static void ipa_endpoint_init_hol_block_enable(struct ipa_endpoint *endpoint,
998e6aab6b9SAlex Elder 					       u32 microseconds)
999e6aab6b9SAlex Elder {
1000e6aab6b9SAlex Elder 	ipa_endpoint_init_hol_block_timer(endpoint, microseconds);
1001e6aab6b9SAlex Elder 	ipa_endpoint_init_hol_block_en(endpoint, true);
1002e6aab6b9SAlex Elder }
1003e6aab6b9SAlex Elder 
1004e6aab6b9SAlex Elder static void ipa_endpoint_init_hol_block_disable(struct ipa_endpoint *endpoint)
1005e6aab6b9SAlex Elder {
1006e6aab6b9SAlex Elder 	ipa_endpoint_init_hol_block_en(endpoint, false);
1007e6aab6b9SAlex Elder }
1008e6aab6b9SAlex Elder 
100984f9bd12SAlex Elder void ipa_endpoint_modem_hol_block_clear_all(struct ipa *ipa)
101084f9bd12SAlex Elder {
101184f9bd12SAlex Elder 	u32 i;
101284f9bd12SAlex Elder 
101384f9bd12SAlex Elder 	for (i = 0; i < IPA_ENDPOINT_MAX; i++) {
101484f9bd12SAlex Elder 		struct ipa_endpoint *endpoint = &ipa->endpoint[i];
101584f9bd12SAlex Elder 
1016f8d34dfdSAlex Elder 		if (endpoint->toward_ipa || endpoint->ee_id != GSI_EE_MODEM)
101784f9bd12SAlex Elder 			continue;
101884f9bd12SAlex Elder 
1019e6aab6b9SAlex Elder 		ipa_endpoint_init_hol_block_disable(endpoint);
1020e6aab6b9SAlex Elder 		ipa_endpoint_init_hol_block_enable(endpoint, 0);
102184f9bd12SAlex Elder 	}
102284f9bd12SAlex Elder }
102384f9bd12SAlex Elder 
102484f9bd12SAlex Elder static void ipa_endpoint_init_deaggr(struct ipa_endpoint *endpoint)
102584f9bd12SAlex Elder {
10266a244b75SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
10276bfb7538SAlex Elder 	struct ipa *ipa = endpoint->ipa;
10286a244b75SAlex Elder 	const struct ipa_reg *reg;
102984f9bd12SAlex Elder 	u32 val = 0;
103084f9bd12SAlex Elder 
1031fb57c3eaSAlex Elder 	if (!endpoint->toward_ipa)
1032fb57c3eaSAlex Elder 		return;		/* Register not valid for RX endpoints */
1033fb57c3eaSAlex Elder 
10346a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_DEAGGR);
103584f9bd12SAlex Elder 	/* DEAGGR_HDR_LEN is 0 */
103684f9bd12SAlex Elder 	/* PACKET_OFFSET_VALID is 0 */
103784f9bd12SAlex Elder 	/* PACKET_OFFSET_LOCATION is ignored (not valid) */
103884f9bd12SAlex Elder 	/* MAX_PACKET_LEN is 0 (not enforced) */
103984f9bd12SAlex Elder 
10406a244b75SAlex Elder 	iowrite32(val, ipa->reg_virt + ipa_reg_n_offset(reg, endpoint_id));
104184f9bd12SAlex Elder }
104284f9bd12SAlex Elder 
10432d265342SAlex Elder static void ipa_endpoint_init_rsrc_grp(struct ipa_endpoint *endpoint)
10442d265342SAlex Elder {
1045181ca020SAlex Elder 	u32 resource_group = endpoint->config.resource_group;
10466a244b75SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
10472d265342SAlex Elder 	struct ipa *ipa = endpoint->ipa;
10486a244b75SAlex Elder 	const struct ipa_reg *reg;
10492d265342SAlex Elder 	u32 val;
10502d265342SAlex Elder 
10516a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_RSRC_GRP);
1052181ca020SAlex Elder 	val = ipa_reg_encode(reg, ENDP_RSRC_GRP, resource_group);
10536bfb7538SAlex Elder 
10546a244b75SAlex Elder 	iowrite32(val, ipa->reg_virt + ipa_reg_n_offset(reg, endpoint_id));
10552d265342SAlex Elder }
10562d265342SAlex Elder 
105784f9bd12SAlex Elder static void ipa_endpoint_init_seq(struct ipa_endpoint *endpoint)
105884f9bd12SAlex Elder {
10596a244b75SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
10606bfb7538SAlex Elder 	struct ipa *ipa = endpoint->ipa;
10616a244b75SAlex Elder 	const struct ipa_reg *reg;
1062181ca020SAlex Elder 	u32 val;
106384f9bd12SAlex Elder 
1064fb57c3eaSAlex Elder 	if (!endpoint->toward_ipa)
1065fb57c3eaSAlex Elder 		return;		/* Register not valid for RX endpoints */
1066fb57c3eaSAlex Elder 
10676a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_SEQ);
10686bfb7538SAlex Elder 
10698ee5df65SAlex Elder 	/* Low-order byte configures primary packet processing */
1070181ca020SAlex Elder 	val = ipa_reg_encode(reg, SEQ_TYPE, endpoint->config.tx.seq_type);
10718ee5df65SAlex Elder 
1072a14d5937SAlex Elder 	/* Second byte (if supported) configures replicated packet processing */
10736bfb7538SAlex Elder 	if (ipa->version < IPA_VERSION_4_5)
1074181ca020SAlex Elder 		val |= ipa_reg_encode(reg, SEQ_REP_TYPE,
1075181ca020SAlex Elder 				      endpoint->config.tx.seq_rep_type);
107684f9bd12SAlex Elder 
10776a244b75SAlex Elder 	iowrite32(val, ipa->reg_virt + ipa_reg_n_offset(reg, endpoint_id));
107884f9bd12SAlex Elder }
107984f9bd12SAlex Elder 
108084f9bd12SAlex Elder /**
108184f9bd12SAlex Elder  * ipa_endpoint_skb_tx() - Transmit a socket buffer
108284f9bd12SAlex Elder  * @endpoint:	Endpoint pointer
108384f9bd12SAlex Elder  * @skb:	Socket buffer to send
108484f9bd12SAlex Elder  *
108584f9bd12SAlex Elder  * Returns:	0 if successful, or a negative error code
108684f9bd12SAlex Elder  */
108784f9bd12SAlex Elder int ipa_endpoint_skb_tx(struct ipa_endpoint *endpoint, struct sk_buff *skb)
108884f9bd12SAlex Elder {
108984f9bd12SAlex Elder 	struct gsi_trans *trans;
109084f9bd12SAlex Elder 	u32 nr_frags;
109184f9bd12SAlex Elder 	int ret;
109284f9bd12SAlex Elder 
109384f9bd12SAlex Elder 	/* Make sure source endpoint's TLV FIFO has enough entries to
109484f9bd12SAlex Elder 	 * hold the linear portion of the skb and all its fragments.
109584f9bd12SAlex Elder 	 * If not, see if we can linearize it before giving up.
109684f9bd12SAlex Elder 	 */
109784f9bd12SAlex Elder 	nr_frags = skb_shinfo(skb)->nr_frags;
1098317595d2SAlex Elder 	if (nr_frags > endpoint->skb_frag_max) {
109984f9bd12SAlex Elder 		if (skb_linearize(skb))
110084f9bd12SAlex Elder 			return -E2BIG;
110184f9bd12SAlex Elder 		nr_frags = 0;
110284f9bd12SAlex Elder 	}
110384f9bd12SAlex Elder 
110484f9bd12SAlex Elder 	trans = ipa_endpoint_trans_alloc(endpoint, 1 + nr_frags);
110584f9bd12SAlex Elder 	if (!trans)
110684f9bd12SAlex Elder 		return -EBUSY;
110784f9bd12SAlex Elder 
110884f9bd12SAlex Elder 	ret = gsi_trans_skb_add(trans, skb);
110984f9bd12SAlex Elder 	if (ret)
111084f9bd12SAlex Elder 		goto err_trans_free;
111184f9bd12SAlex Elder 	trans->data = skb;	/* transaction owns skb now */
111284f9bd12SAlex Elder 
111384f9bd12SAlex Elder 	gsi_trans_commit(trans, !netdev_xmit_more());
111484f9bd12SAlex Elder 
111584f9bd12SAlex Elder 	return 0;
111684f9bd12SAlex Elder 
111784f9bd12SAlex Elder err_trans_free:
111884f9bd12SAlex Elder 	gsi_trans_free(trans);
111984f9bd12SAlex Elder 
112084f9bd12SAlex Elder 	return -ENOMEM;
112184f9bd12SAlex Elder }
112284f9bd12SAlex Elder 
112384f9bd12SAlex Elder static void ipa_endpoint_status(struct ipa_endpoint *endpoint)
112484f9bd12SAlex Elder {
112584f9bd12SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
112684f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
11276a244b75SAlex Elder 	const struct ipa_reg *reg;
112884f9bd12SAlex Elder 	u32 val = 0;
112984f9bd12SAlex Elder 
11306a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_STATUS);
1131660e52d6SAlex Elder 	if (endpoint->config.status_enable) {
1132181ca020SAlex Elder 		val |= ipa_reg_bit(reg, STATUS_EN);
113384f9bd12SAlex Elder 		if (endpoint->toward_ipa) {
113484f9bd12SAlex Elder 			enum ipa_endpoint_name name;
113584f9bd12SAlex Elder 			u32 status_endpoint_id;
113684f9bd12SAlex Elder 
1137660e52d6SAlex Elder 			name = endpoint->config.tx.status_endpoint;
113884f9bd12SAlex Elder 			status_endpoint_id = ipa->name_map[name]->endpoint_id;
113984f9bd12SAlex Elder 
1140181ca020SAlex Elder 			val |= ipa_reg_encode(reg, STATUS_ENDP,
1141181ca020SAlex Elder 					      status_endpoint_id);
114284f9bd12SAlex Elder 		}
11438bfc4e21SAlex Elder 		/* STATUS_LOCATION is 0, meaning status element precedes
1144181ca020SAlex Elder 		 * packet (not present for IPA v4.5+)
11458bfc4e21SAlex Elder 		 */
1146181ca020SAlex Elder 		/* STATUS_PKT_SUPPRESS_FMASK is 0 (not present for v4.0+) */
114784f9bd12SAlex Elder 	}
114884f9bd12SAlex Elder 
11496a244b75SAlex Elder 	iowrite32(val, ipa->reg_virt + ipa_reg_n_offset(reg, endpoint_id));
115084f9bd12SAlex Elder }
115184f9bd12SAlex Elder 
11526a606b90SAlex Elder static int ipa_endpoint_replenish_one(struct ipa_endpoint *endpoint,
11536a606b90SAlex Elder 				      struct gsi_trans *trans)
115484f9bd12SAlex Elder {
115584f9bd12SAlex Elder 	struct page *page;
1156ed23f026SAlex Elder 	u32 buffer_size;
115784f9bd12SAlex Elder 	u32 offset;
115884f9bd12SAlex Elder 	u32 len;
115984f9bd12SAlex Elder 	int ret;
116084f9bd12SAlex Elder 
1161660e52d6SAlex Elder 	buffer_size = endpoint->config.rx.buffer_size;
1162ed23f026SAlex Elder 	page = dev_alloc_pages(get_order(buffer_size));
116384f9bd12SAlex Elder 	if (!page)
11646a606b90SAlex Elder 		return -ENOMEM;
116584f9bd12SAlex Elder 
116684f9bd12SAlex Elder 	/* Offset the buffer to make space for skb headroom */
116784f9bd12SAlex Elder 	offset = NET_SKB_PAD;
1168ed23f026SAlex Elder 	len = buffer_size - offset;
116984f9bd12SAlex Elder 
117084f9bd12SAlex Elder 	ret = gsi_trans_page_add(trans, page, len, offset);
117184f9bd12SAlex Elder 	if (ret)
117270132763SAlex Elder 		put_page(page);
11736a606b90SAlex Elder 	else
117484f9bd12SAlex Elder 		trans->data = page;	/* transaction owns page now */
117584f9bd12SAlex Elder 
11766a606b90SAlex Elder 	return ret;
117784f9bd12SAlex Elder }
117884f9bd12SAlex Elder 
117984f9bd12SAlex Elder /**
11809af5ccf3SAlex Elder  * ipa_endpoint_replenish() - Replenish endpoint receive buffers
1181e3eea08eSAlex Elder  * @endpoint:	Endpoint to be replenished
118284f9bd12SAlex Elder  *
11839af5ccf3SAlex Elder  * The IPA hardware can hold a fixed number of receive buffers for an RX
11849af5ccf3SAlex Elder  * endpoint, based on the number of entries in the underlying channel ring
11859af5ccf3SAlex Elder  * buffer.  If an endpoint's "backlog" is non-zero, it indicates how many
11869af5ccf3SAlex Elder  * more receive buffers can be supplied to the hardware.  Replenishing for
1187a9bec7aeSAlex Elder  * an endpoint can be disabled, in which case buffers are not queued to
1188a9bec7aeSAlex Elder  * the hardware.
118984f9bd12SAlex Elder  */
11904b22d841SAlex Elder static void ipa_endpoint_replenish(struct ipa_endpoint *endpoint)
119184f9bd12SAlex Elder {
11926a606b90SAlex Elder 	struct gsi_trans *trans;
119384f9bd12SAlex Elder 
11944b22d841SAlex Elder 	if (!test_bit(IPA_REPLENISH_ENABLED, endpoint->replenish_flags))
119584f9bd12SAlex Elder 		return;
119684f9bd12SAlex Elder 
11974b22d841SAlex Elder 	/* Skip it if it's already active */
11984b22d841SAlex Elder 	if (test_and_set_bit(IPA_REPLENISH_ACTIVE, endpoint->replenish_flags))
1199998c0bd2SAlex Elder 		return;
1200998c0bd2SAlex Elder 
1201d0ac30e7SAlex Elder 	while ((trans = ipa_endpoint_trans_alloc(endpoint, 1))) {
12029654d8c4SAlex Elder 		bool doorbell;
12039654d8c4SAlex Elder 
12046a606b90SAlex Elder 		if (ipa_endpoint_replenish_one(endpoint, trans))
12056a606b90SAlex Elder 			goto try_again_later;
1206b9dbabc5SAlex Elder 
1207b9dbabc5SAlex Elder 
1208b9dbabc5SAlex Elder 		/* Ring the doorbell if we've got a full batch */
12099654d8c4SAlex Elder 		doorbell = !(++endpoint->replenish_count % IPA_REPLENISH_BATCH);
12109654d8c4SAlex Elder 		gsi_trans_commit(trans, doorbell);
1211b9dbabc5SAlex Elder 	}
1212998c0bd2SAlex Elder 
1213998c0bd2SAlex Elder 	clear_bit(IPA_REPLENISH_ACTIVE, endpoint->replenish_flags);
1214998c0bd2SAlex Elder 
121584f9bd12SAlex Elder 	return;
121684f9bd12SAlex Elder 
121784f9bd12SAlex Elder try_again_later:
12186a606b90SAlex Elder 	gsi_trans_free(trans);
1219998c0bd2SAlex Elder 	clear_bit(IPA_REPLENISH_ACTIVE, endpoint->replenish_flags);
1220998c0bd2SAlex Elder 
122184f9bd12SAlex Elder 	/* Whenever a receive buffer transaction completes we'll try to
122284f9bd12SAlex Elder 	 * replenish again.  It's unlikely, but if we fail to supply even
122384f9bd12SAlex Elder 	 * one buffer, nothing will trigger another replenish attempt.
12245fc7f9baSAlex Elder 	 * If the hardware has no receive buffers queued, schedule work to
12255fc7f9baSAlex Elder 	 * try replenishing again.
122684f9bd12SAlex Elder 	 */
12275fc7f9baSAlex Elder 	if (gsi_channel_trans_idle(&endpoint->ipa->gsi, endpoint->channel_id))
122884f9bd12SAlex Elder 		schedule_delayed_work(&endpoint->replenish_work,
122984f9bd12SAlex Elder 				      msecs_to_jiffies(1));
123084f9bd12SAlex Elder }
123184f9bd12SAlex Elder 
123284f9bd12SAlex Elder static void ipa_endpoint_replenish_enable(struct ipa_endpoint *endpoint)
123384f9bd12SAlex Elder {
1234c1aaa01dSAlex Elder 	set_bit(IPA_REPLENISH_ENABLED, endpoint->replenish_flags);
123584f9bd12SAlex Elder 
123684f9bd12SAlex Elder 	/* Start replenishing if hardware currently has no buffers */
12375fc7f9baSAlex Elder 	if (gsi_channel_trans_idle(&endpoint->ipa->gsi, endpoint->channel_id))
12384b22d841SAlex Elder 		ipa_endpoint_replenish(endpoint);
123984f9bd12SAlex Elder }
124084f9bd12SAlex Elder 
124184f9bd12SAlex Elder static void ipa_endpoint_replenish_disable(struct ipa_endpoint *endpoint)
124284f9bd12SAlex Elder {
1243c1aaa01dSAlex Elder 	clear_bit(IPA_REPLENISH_ENABLED, endpoint->replenish_flags);
124484f9bd12SAlex Elder }
124584f9bd12SAlex Elder 
124684f9bd12SAlex Elder static void ipa_endpoint_replenish_work(struct work_struct *work)
124784f9bd12SAlex Elder {
124884f9bd12SAlex Elder 	struct delayed_work *dwork = to_delayed_work(work);
124984f9bd12SAlex Elder 	struct ipa_endpoint *endpoint;
125084f9bd12SAlex Elder 
125184f9bd12SAlex Elder 	endpoint = container_of(dwork, struct ipa_endpoint, replenish_work);
125284f9bd12SAlex Elder 
12534b22d841SAlex Elder 	ipa_endpoint_replenish(endpoint);
125484f9bd12SAlex Elder }
125584f9bd12SAlex Elder 
125684f9bd12SAlex Elder static void ipa_endpoint_skb_copy(struct ipa_endpoint *endpoint,
125784f9bd12SAlex Elder 				  void *data, u32 len, u32 extra)
125884f9bd12SAlex Elder {
125984f9bd12SAlex Elder 	struct sk_buff *skb;
126084f9bd12SAlex Elder 
12611b65bbccSAlex Elder 	if (!endpoint->netdev)
12621b65bbccSAlex Elder 		return;
12631b65bbccSAlex Elder 
126484f9bd12SAlex Elder 	skb = __dev_alloc_skb(len, GFP_ATOMIC);
126530b338ffSAlex Elder 	if (skb) {
12661b65bbccSAlex Elder 		/* Copy the data into the socket buffer and receive it */
126784f9bd12SAlex Elder 		skb_put(skb, len);
126884f9bd12SAlex Elder 		memcpy(skb->data, data, len);
126984f9bd12SAlex Elder 		skb->truesize += extra;
127030b338ffSAlex Elder 	}
127184f9bd12SAlex Elder 
127284f9bd12SAlex Elder 	ipa_modem_skb_rx(endpoint->netdev, skb);
127384f9bd12SAlex Elder }
127484f9bd12SAlex Elder 
127584f9bd12SAlex Elder static bool ipa_endpoint_skb_build(struct ipa_endpoint *endpoint,
127684f9bd12SAlex Elder 				   struct page *page, u32 len)
127784f9bd12SAlex Elder {
1278660e52d6SAlex Elder 	u32 buffer_size = endpoint->config.rx.buffer_size;
127984f9bd12SAlex Elder 	struct sk_buff *skb;
128084f9bd12SAlex Elder 
128184f9bd12SAlex Elder 	/* Nothing to do if there's no netdev */
128284f9bd12SAlex Elder 	if (!endpoint->netdev)
128384f9bd12SAlex Elder 		return false;
128484f9bd12SAlex Elder 
1285ed23f026SAlex Elder 	WARN_ON(len > SKB_WITH_OVERHEAD(buffer_size - NET_SKB_PAD));
12865bc55884SAlex Elder 
1287ed23f026SAlex Elder 	skb = build_skb(page_address(page), buffer_size);
128884f9bd12SAlex Elder 	if (skb) {
128984f9bd12SAlex Elder 		/* Reserve the headroom and account for the data */
129084f9bd12SAlex Elder 		skb_reserve(skb, NET_SKB_PAD);
129184f9bd12SAlex Elder 		skb_put(skb, len);
129284f9bd12SAlex Elder 	}
129384f9bd12SAlex Elder 
129484f9bd12SAlex Elder 	/* Receive the buffer (or record drop if unable to build it) */
129584f9bd12SAlex Elder 	ipa_modem_skb_rx(endpoint->netdev, skb);
129684f9bd12SAlex Elder 
129784f9bd12SAlex Elder 	return skb != NULL;
129884f9bd12SAlex Elder }
129984f9bd12SAlex Elder 
130084f9bd12SAlex Elder /* The format of a packet status element is the same for several status
130145921390SAlex Elder  * types (opcodes).  Other types aren't currently supported.
130284f9bd12SAlex Elder  */
130384f9bd12SAlex Elder static bool ipa_status_format_packet(enum ipa_status_opcode opcode)
130484f9bd12SAlex Elder {
130584f9bd12SAlex Elder 	switch (opcode) {
130684f9bd12SAlex Elder 	case IPA_STATUS_OPCODE_PACKET:
130784f9bd12SAlex Elder 	case IPA_STATUS_OPCODE_DROPPED_PACKET:
130884f9bd12SAlex Elder 	case IPA_STATUS_OPCODE_SUSPENDED_PACKET:
130984f9bd12SAlex Elder 	case IPA_STATUS_OPCODE_PACKET_2ND_PASS:
131084f9bd12SAlex Elder 		return true;
131184f9bd12SAlex Elder 	default:
131284f9bd12SAlex Elder 		return false;
131384f9bd12SAlex Elder 	}
131484f9bd12SAlex Elder }
131584f9bd12SAlex Elder 
131684f9bd12SAlex Elder static bool ipa_endpoint_status_skip(struct ipa_endpoint *endpoint,
131784f9bd12SAlex Elder 				     const struct ipa_status *status)
131884f9bd12SAlex Elder {
131984f9bd12SAlex Elder 	u32 endpoint_id;
132084f9bd12SAlex Elder 
132184f9bd12SAlex Elder 	if (!ipa_status_format_packet(status->opcode))
132284f9bd12SAlex Elder 		return true;
132384f9bd12SAlex Elder 	if (!status->pkt_len)
132484f9bd12SAlex Elder 		return true;
1325c13899f1SAlex Elder 	endpoint_id = u8_get_bits(status->endp_dst_idx,
132684f9bd12SAlex Elder 				  IPA_STATUS_DST_IDX_FMASK);
132784f9bd12SAlex Elder 	if (endpoint_id != endpoint->endpoint_id)
132884f9bd12SAlex Elder 		return true;
132984f9bd12SAlex Elder 
133084f9bd12SAlex Elder 	return false;	/* Don't skip this packet, process it */
133184f9bd12SAlex Elder }
133284f9bd12SAlex Elder 
1333f6aba7b5SAlex Elder static bool ipa_endpoint_status_tag(struct ipa_endpoint *endpoint,
1334f6aba7b5SAlex Elder 				    const struct ipa_status *status)
1335f6aba7b5SAlex Elder {
133651c48ce2SAlex Elder 	struct ipa_endpoint *command_endpoint;
133751c48ce2SAlex Elder 	struct ipa *ipa = endpoint->ipa;
133851c48ce2SAlex Elder 	u32 endpoint_id;
133951c48ce2SAlex Elder 
134051c48ce2SAlex Elder 	if (!le16_get_bits(status->mask, IPA_STATUS_MASK_TAG_VALID_FMASK))
134151c48ce2SAlex Elder 		return false;	/* No valid tag */
134251c48ce2SAlex Elder 
134351c48ce2SAlex Elder 	/* The status contains a valid tag.  We know the packet was sent to
134451c48ce2SAlex Elder 	 * this endpoint (already verified by ipa_endpoint_status_skip()).
134551c48ce2SAlex Elder 	 * If the packet came from the AP->command TX endpoint we know
134651c48ce2SAlex Elder 	 * this packet was sent as part of the pipeline clear process.
134751c48ce2SAlex Elder 	 */
134851c48ce2SAlex Elder 	endpoint_id = u8_get_bits(status->endp_src_idx,
134951c48ce2SAlex Elder 				  IPA_STATUS_SRC_IDX_FMASK);
135051c48ce2SAlex Elder 	command_endpoint = ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX];
135151c48ce2SAlex Elder 	if (endpoint_id == command_endpoint->endpoint_id) {
135251c48ce2SAlex Elder 		complete(&ipa->completion);
135351c48ce2SAlex Elder 	} else {
135451c48ce2SAlex Elder 		dev_err(&ipa->pdev->dev,
135551c48ce2SAlex Elder 			"unexpected tagged packet from endpoint %u\n",
135651c48ce2SAlex Elder 			endpoint_id);
135751c48ce2SAlex Elder 	}
135851c48ce2SAlex Elder 
135951c48ce2SAlex Elder 	return true;
1360f6aba7b5SAlex Elder }
1361f6aba7b5SAlex Elder 
136284f9bd12SAlex Elder /* Return whether the status indicates the packet should be dropped */
1363f6aba7b5SAlex Elder static bool ipa_endpoint_status_drop(struct ipa_endpoint *endpoint,
1364f6aba7b5SAlex Elder 				     const struct ipa_status *status)
136584f9bd12SAlex Elder {
136684f9bd12SAlex Elder 	u32 val;
136784f9bd12SAlex Elder 
1368f6aba7b5SAlex Elder 	/* If the status indicates a tagged transfer, we'll drop the packet */
1369f6aba7b5SAlex Elder 	if (ipa_endpoint_status_tag(endpoint, status))
1370f6aba7b5SAlex Elder 		return true;
1371f6aba7b5SAlex Elder 
1372ab4f71e5SAlex Elder 	/* Deaggregation exceptions we drop; all other types we consume */
137384f9bd12SAlex Elder 	if (status->exception)
137484f9bd12SAlex Elder 		return status->exception == IPA_STATUS_EXCEPTION_DEAGGR;
137584f9bd12SAlex Elder 
137684f9bd12SAlex Elder 	/* Drop the packet if it fails to match a routing rule; otherwise no */
137784f9bd12SAlex Elder 	val = le32_get_bits(status->flags1, IPA_STATUS_FLAGS1_RT_RULE_ID_FMASK);
137884f9bd12SAlex Elder 
137984f9bd12SAlex Elder 	return val == field_max(IPA_STATUS_FLAGS1_RT_RULE_ID_FMASK);
138084f9bd12SAlex Elder }
138184f9bd12SAlex Elder 
138284f9bd12SAlex Elder static void ipa_endpoint_status_parse(struct ipa_endpoint *endpoint,
138384f9bd12SAlex Elder 				      struct page *page, u32 total_len)
138484f9bd12SAlex Elder {
1385660e52d6SAlex Elder 	u32 buffer_size = endpoint->config.rx.buffer_size;
138684f9bd12SAlex Elder 	void *data = page_address(page) + NET_SKB_PAD;
1387ed23f026SAlex Elder 	u32 unused = buffer_size - total_len;
138884f9bd12SAlex Elder 	u32 resid = total_len;
138984f9bd12SAlex Elder 
139084f9bd12SAlex Elder 	while (resid) {
139184f9bd12SAlex Elder 		const struct ipa_status *status = data;
139284f9bd12SAlex Elder 		u32 align;
139384f9bd12SAlex Elder 		u32 len;
139484f9bd12SAlex Elder 
139584f9bd12SAlex Elder 		if (resid < sizeof(*status)) {
139684f9bd12SAlex Elder 			dev_err(&endpoint->ipa->pdev->dev,
139784f9bd12SAlex Elder 				"short message (%u bytes < %zu byte status)\n",
139884f9bd12SAlex Elder 				resid, sizeof(*status));
139984f9bd12SAlex Elder 			break;
140084f9bd12SAlex Elder 		}
140184f9bd12SAlex Elder 
140284f9bd12SAlex Elder 		/* Skip over status packets that lack packet data */
140384f9bd12SAlex Elder 		if (ipa_endpoint_status_skip(endpoint, status)) {
140484f9bd12SAlex Elder 			data += sizeof(*status);
140584f9bd12SAlex Elder 			resid -= sizeof(*status);
140684f9bd12SAlex Elder 			continue;
140784f9bd12SAlex Elder 		}
140884f9bd12SAlex Elder 
1409162fbc6fSAlex Elder 		/* Compute the amount of buffer space consumed by the packet,
1410162fbc6fSAlex Elder 		 * including the status element.  If the hardware is configured
1411162fbc6fSAlex Elder 		 * to pad packet data to an aligned boundary, account for that.
1412162fbc6fSAlex Elder 		 * And if checksum offload is enabled a trailer containing
1413162fbc6fSAlex Elder 		 * computed checksum information will be appended.
141484f9bd12SAlex Elder 		 */
1415660e52d6SAlex Elder 		align = endpoint->config.rx.pad_align ? : 1;
141684f9bd12SAlex Elder 		len = le16_to_cpu(status->pkt_len);
141784f9bd12SAlex Elder 		len = sizeof(*status) + ALIGN(len, align);
1418660e52d6SAlex Elder 		if (endpoint->config.checksum)
141984f9bd12SAlex Elder 			len += sizeof(struct rmnet_map_dl_csum_trailer);
142084f9bd12SAlex Elder 
1421f6aba7b5SAlex Elder 		if (!ipa_endpoint_status_drop(endpoint, status)) {
1422162fbc6fSAlex Elder 			void *data2;
1423162fbc6fSAlex Elder 			u32 extra;
1424162fbc6fSAlex Elder 			u32 len2;
142584f9bd12SAlex Elder 
142684f9bd12SAlex Elder 			/* Client receives only packet data (no status) */
1427162fbc6fSAlex Elder 			data2 = data + sizeof(*status);
1428162fbc6fSAlex Elder 			len2 = le16_to_cpu(status->pkt_len);
1429162fbc6fSAlex Elder 
1430162fbc6fSAlex Elder 			/* Have the true size reflect the extra unused space in
1431162fbc6fSAlex Elder 			 * the original receive buffer.  Distribute the "cost"
1432162fbc6fSAlex Elder 			 * proportionately across all aggregated packets in the
1433162fbc6fSAlex Elder 			 * buffer.
1434162fbc6fSAlex Elder 			 */
1435162fbc6fSAlex Elder 			extra = DIV_ROUND_CLOSEST(unused * len, total_len);
143684f9bd12SAlex Elder 			ipa_endpoint_skb_copy(endpoint, data2, len2, extra);
143784f9bd12SAlex Elder 		}
143884f9bd12SAlex Elder 
143984f9bd12SAlex Elder 		/* Consume status and the full packet it describes */
144084f9bd12SAlex Elder 		data += len;
144184f9bd12SAlex Elder 		resid -= len;
144284f9bd12SAlex Elder 	}
144384f9bd12SAlex Elder }
144484f9bd12SAlex Elder 
1445983a1a30SAlex Elder void ipa_endpoint_trans_complete(struct ipa_endpoint *endpoint,
144684f9bd12SAlex Elder 				 struct gsi_trans *trans)
144784f9bd12SAlex Elder {
144884f9bd12SAlex Elder 	struct page *page;
144984f9bd12SAlex Elder 
1450983a1a30SAlex Elder 	if (endpoint->toward_ipa)
1451983a1a30SAlex Elder 		return;
1452983a1a30SAlex Elder 
145384f9bd12SAlex Elder 	if (trans->cancelled)
14545d6ac24fSAlex Elder 		goto done;
145584f9bd12SAlex Elder 
145684f9bd12SAlex Elder 	/* Parse or build a socket buffer using the actual received length */
145784f9bd12SAlex Elder 	page = trans->data;
1458660e52d6SAlex Elder 	if (endpoint->config.status_enable)
145984f9bd12SAlex Elder 		ipa_endpoint_status_parse(endpoint, page, trans->len);
146084f9bd12SAlex Elder 	else if (ipa_endpoint_skb_build(endpoint, page, trans->len))
146184f9bd12SAlex Elder 		trans->data = NULL;	/* Pages have been consumed */
14625d6ac24fSAlex Elder done:
14635d6ac24fSAlex Elder 	ipa_endpoint_replenish(endpoint);
146484f9bd12SAlex Elder }
146584f9bd12SAlex Elder 
146684f9bd12SAlex Elder void ipa_endpoint_trans_release(struct ipa_endpoint *endpoint,
146784f9bd12SAlex Elder 				struct gsi_trans *trans)
146884f9bd12SAlex Elder {
146984f9bd12SAlex Elder 	if (endpoint->toward_ipa) {
147084f9bd12SAlex Elder 		struct ipa *ipa = endpoint->ipa;
147184f9bd12SAlex Elder 
147284f9bd12SAlex Elder 		/* Nothing to do for command transactions */
147384f9bd12SAlex Elder 		if (endpoint != ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]) {
147484f9bd12SAlex Elder 			struct sk_buff *skb = trans->data;
147584f9bd12SAlex Elder 
147684f9bd12SAlex Elder 			if (skb)
147784f9bd12SAlex Elder 				dev_kfree_skb_any(skb);
147884f9bd12SAlex Elder 		}
147984f9bd12SAlex Elder 	} else {
148084f9bd12SAlex Elder 		struct page *page = trans->data;
148184f9bd12SAlex Elder 
1482155c0c90SAlex Elder 		if (page)
1483155c0c90SAlex Elder 			put_page(page);
148484f9bd12SAlex Elder 	}
148584f9bd12SAlex Elder }
148684f9bd12SAlex Elder 
148784f9bd12SAlex Elder void ipa_endpoint_default_route_set(struct ipa *ipa, u32 endpoint_id)
148884f9bd12SAlex Elder {
14896a244b75SAlex Elder 	const struct ipa_reg *reg;
149084f9bd12SAlex Elder 	u32 val;
149184f9bd12SAlex Elder 
14926a244b75SAlex Elder 	reg = ipa_reg(ipa, ROUTE);
149384f9bd12SAlex Elder 	/* ROUTE_DIS is 0 */
1494479deb32SAlex Elder 	val = ipa_reg_encode(reg, ROUTE_DEF_PIPE, endpoint_id);
1495479deb32SAlex Elder 	val |= ipa_reg_bit(reg, ROUTE_DEF_HDR_TABLE);
1496479deb32SAlex Elder 	/* ROUTE_DEF_HDR_OFST is 0 */
1497479deb32SAlex Elder 	val |= ipa_reg_encode(reg, ROUTE_FRAG_DEF_PIPE, endpoint_id);
1498479deb32SAlex Elder 	val |= ipa_reg_bit(reg, ROUTE_DEF_RETAIN_HDR);
149984f9bd12SAlex Elder 
15006a244b75SAlex Elder 	iowrite32(val, ipa->reg_virt + ipa_reg_offset(reg));
150184f9bd12SAlex Elder }
150284f9bd12SAlex Elder 
150384f9bd12SAlex Elder void ipa_endpoint_default_route_clear(struct ipa *ipa)
150484f9bd12SAlex Elder {
150584f9bd12SAlex Elder 	ipa_endpoint_default_route_set(ipa, 0);
150684f9bd12SAlex Elder }
150784f9bd12SAlex Elder 
150884f9bd12SAlex Elder /**
150984f9bd12SAlex Elder  * ipa_endpoint_reset_rx_aggr() - Reset RX endpoint with aggregation active
151084f9bd12SAlex Elder  * @endpoint:	Endpoint to be reset
151184f9bd12SAlex Elder  *
151284f9bd12SAlex Elder  * If aggregation is active on an RX endpoint when a reset is performed
151384f9bd12SAlex Elder  * on its underlying GSI channel, a special sequence of actions must be
151484f9bd12SAlex Elder  * taken to ensure the IPA pipeline is properly cleared.
151584f9bd12SAlex Elder  *
1516e3eea08eSAlex Elder  * Return:	0 if successful, or a negative error code
151784f9bd12SAlex Elder  */
151884f9bd12SAlex Elder static int ipa_endpoint_reset_rx_aggr(struct ipa_endpoint *endpoint)
151984f9bd12SAlex Elder {
152084f9bd12SAlex Elder 	struct device *dev = &endpoint->ipa->pdev->dev;
152184f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
152284f9bd12SAlex Elder 	struct gsi *gsi = &ipa->gsi;
15234fa95248SAlex Elder 	bool suspended = false;
152484f9bd12SAlex Elder 	dma_addr_t addr;
152584f9bd12SAlex Elder 	u32 retries;
152684f9bd12SAlex Elder 	u32 len = 1;
152784f9bd12SAlex Elder 	void *virt;
152884f9bd12SAlex Elder 	int ret;
152984f9bd12SAlex Elder 
153084f9bd12SAlex Elder 	virt = kzalloc(len, GFP_KERNEL);
153184f9bd12SAlex Elder 	if (!virt)
153284f9bd12SAlex Elder 		return -ENOMEM;
153384f9bd12SAlex Elder 
153484f9bd12SAlex Elder 	addr = dma_map_single(dev, virt, len, DMA_FROM_DEVICE);
153584f9bd12SAlex Elder 	if (dma_mapping_error(dev, addr)) {
153684f9bd12SAlex Elder 		ret = -ENOMEM;
153784f9bd12SAlex Elder 		goto out_kfree;
153884f9bd12SAlex Elder 	}
153984f9bd12SAlex Elder 
154084f9bd12SAlex Elder 	/* Force close aggregation before issuing the reset */
154184f9bd12SAlex Elder 	ipa_endpoint_force_close(endpoint);
154284f9bd12SAlex Elder 
154384f9bd12SAlex Elder 	/* Reset and reconfigure the channel with the doorbell engine
154484f9bd12SAlex Elder 	 * disabled.  Then poll until we know aggregation is no longer
154584f9bd12SAlex Elder 	 * active.  We'll re-enable the doorbell (if appropriate) when
154684f9bd12SAlex Elder 	 * we reset again below.
154784f9bd12SAlex Elder 	 */
154884f9bd12SAlex Elder 	gsi_channel_reset(gsi, endpoint->channel_id, false);
154984f9bd12SAlex Elder 
155084f9bd12SAlex Elder 	/* Make sure the channel isn't suspended */
15514fa95248SAlex Elder 	suspended = ipa_endpoint_program_suspend(endpoint, false);
155284f9bd12SAlex Elder 
155384f9bd12SAlex Elder 	/* Start channel and do a 1 byte read */
155484f9bd12SAlex Elder 	ret = gsi_channel_start(gsi, endpoint->channel_id);
155584f9bd12SAlex Elder 	if (ret)
155684f9bd12SAlex Elder 		goto out_suspend_again;
155784f9bd12SAlex Elder 
155884f9bd12SAlex Elder 	ret = gsi_trans_read_byte(gsi, endpoint->channel_id, addr);
155984f9bd12SAlex Elder 	if (ret)
156084f9bd12SAlex Elder 		goto err_endpoint_stop;
156184f9bd12SAlex Elder 
156284f9bd12SAlex Elder 	/* Wait for aggregation to be closed on the channel */
156384f9bd12SAlex Elder 	retries = IPA_ENDPOINT_RESET_AGGR_RETRY_MAX;
156484f9bd12SAlex Elder 	do {
156584f9bd12SAlex Elder 		if (!ipa_endpoint_aggr_active(endpoint))
156684f9bd12SAlex Elder 			break;
156774401946SAlex Elder 		usleep_range(USEC_PER_MSEC, 2 * USEC_PER_MSEC);
156884f9bd12SAlex Elder 	} while (retries--);
156984f9bd12SAlex Elder 
157084f9bd12SAlex Elder 	/* Check one last time */
157184f9bd12SAlex Elder 	if (ipa_endpoint_aggr_active(endpoint))
157284f9bd12SAlex Elder 		dev_err(dev, "endpoint %u still active during reset\n",
157384f9bd12SAlex Elder 			endpoint->endpoint_id);
157484f9bd12SAlex Elder 
157584f9bd12SAlex Elder 	gsi_trans_read_byte_done(gsi, endpoint->channel_id);
157684f9bd12SAlex Elder 
1577f30dcb7dSAlex Elder 	ret = gsi_channel_stop(gsi, endpoint->channel_id);
157884f9bd12SAlex Elder 	if (ret)
157984f9bd12SAlex Elder 		goto out_suspend_again;
158084f9bd12SAlex Elder 
1581497abc87SPeng Li 	/* Finally, reset and reconfigure the channel again (re-enabling
158284f9bd12SAlex Elder 	 * the doorbell engine if appropriate).  Sleep for 1 millisecond to
158384f9bd12SAlex Elder 	 * complete the channel reset sequence.  Finish by suspending the
158484f9bd12SAlex Elder 	 * channel again (if necessary).
158584f9bd12SAlex Elder 	 */
1586ce54993dSAlex Elder 	gsi_channel_reset(gsi, endpoint->channel_id, true);
158784f9bd12SAlex Elder 
158874401946SAlex Elder 	usleep_range(USEC_PER_MSEC, 2 * USEC_PER_MSEC);
158984f9bd12SAlex Elder 
159084f9bd12SAlex Elder 	goto out_suspend_again;
159184f9bd12SAlex Elder 
159284f9bd12SAlex Elder err_endpoint_stop:
1593f30dcb7dSAlex Elder 	(void)gsi_channel_stop(gsi, endpoint->channel_id);
159484f9bd12SAlex Elder out_suspend_again:
15954fa95248SAlex Elder 	if (suspended)
15964fa95248SAlex Elder 		(void)ipa_endpoint_program_suspend(endpoint, true);
159784f9bd12SAlex Elder 	dma_unmap_single(dev, addr, len, DMA_FROM_DEVICE);
159884f9bd12SAlex Elder out_kfree:
159984f9bd12SAlex Elder 	kfree(virt);
160084f9bd12SAlex Elder 
160184f9bd12SAlex Elder 	return ret;
160284f9bd12SAlex Elder }
160384f9bd12SAlex Elder 
160484f9bd12SAlex Elder static void ipa_endpoint_reset(struct ipa_endpoint *endpoint)
160584f9bd12SAlex Elder {
160684f9bd12SAlex Elder 	u32 channel_id = endpoint->channel_id;
160784f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
160884f9bd12SAlex Elder 	bool special;
160984f9bd12SAlex Elder 	int ret = 0;
161084f9bd12SAlex Elder 
161184f9bd12SAlex Elder 	/* On IPA v3.5.1, if an RX endpoint is reset while aggregation
161284f9bd12SAlex Elder 	 * is active, we need to handle things specially to recover.
161384f9bd12SAlex Elder 	 * All other cases just need to reset the underlying GSI channel.
161484f9bd12SAlex Elder 	 */
1615d7f3087bSAlex Elder 	special = ipa->version < IPA_VERSION_4_0 && !endpoint->toward_ipa &&
1616660e52d6SAlex Elder 			endpoint->config.aggregation;
1617ce54993dSAlex Elder 	if (special && ipa_endpoint_aggr_active(endpoint))
161884f9bd12SAlex Elder 		ret = ipa_endpoint_reset_rx_aggr(endpoint);
161984f9bd12SAlex Elder 	else
1620ce54993dSAlex Elder 		gsi_channel_reset(&ipa->gsi, channel_id, true);
162184f9bd12SAlex Elder 
162284f9bd12SAlex Elder 	if (ret)
162384f9bd12SAlex Elder 		dev_err(&ipa->pdev->dev,
162484f9bd12SAlex Elder 			"error %d resetting channel %u for endpoint %u\n",
162584f9bd12SAlex Elder 			ret, endpoint->channel_id, endpoint->endpoint_id);
162684f9bd12SAlex Elder }
162784f9bd12SAlex Elder 
162884f9bd12SAlex Elder static void ipa_endpoint_program(struct ipa_endpoint *endpoint)
162984f9bd12SAlex Elder {
16304c9d631aSAlex Elder 	if (endpoint->toward_ipa) {
16314c9d631aSAlex Elder 		/* Newer versions of IPA use GSI channel flow control
16324c9d631aSAlex Elder 		 * instead of endpoint DELAY mode to prevent sending data.
16334c9d631aSAlex Elder 		 * Flow control is disabled for newly-allocated channels,
16344c9d631aSAlex Elder 		 * and we can assume flow control is not (ever) enabled
16354c9d631aSAlex Elder 		 * for AP TX channels.
16364c9d631aSAlex Elder 		 */
16374c9d631aSAlex Elder 		if (endpoint->ipa->version < IPA_VERSION_4_2)
1638a4dcad34SAlex Elder 			ipa_endpoint_program_delay(endpoint, false);
16394c9d631aSAlex Elder 	} else {
16404c9d631aSAlex Elder 		/* Ensure suspend mode is off on all AP RX endpoints */
1641fb57c3eaSAlex Elder 		(void)ipa_endpoint_program_suspend(endpoint, false);
16424c9d631aSAlex Elder 	}
1643fb57c3eaSAlex Elder 	ipa_endpoint_init_cfg(endpoint);
1644647a05f3SAlex Elder 	ipa_endpoint_init_nat(endpoint);
1645fb57c3eaSAlex Elder 	ipa_endpoint_init_hdr(endpoint);
164684f9bd12SAlex Elder 	ipa_endpoint_init_hdr_ext(endpoint);
1647fb57c3eaSAlex Elder 	ipa_endpoint_init_hdr_metadata_mask(endpoint);
1648fb57c3eaSAlex Elder 	ipa_endpoint_init_mode(endpoint);
164984f9bd12SAlex Elder 	ipa_endpoint_init_aggr(endpoint);
1650153213f0SAlex Elder 	if (!endpoint->toward_ipa) {
1651153213f0SAlex Elder 		if (endpoint->config.rx.holb_drop)
1652153213f0SAlex Elder 			ipa_endpoint_init_hol_block_enable(endpoint, 0);
1653153213f0SAlex Elder 		else
165401c36637SAlex Elder 			ipa_endpoint_init_hol_block_disable(endpoint);
1655153213f0SAlex Elder 	}
165684f9bd12SAlex Elder 	ipa_endpoint_init_deaggr(endpoint);
16572d265342SAlex Elder 	ipa_endpoint_init_rsrc_grp(endpoint);
165884f9bd12SAlex Elder 	ipa_endpoint_init_seq(endpoint);
165984f9bd12SAlex Elder 	ipa_endpoint_status(endpoint);
166084f9bd12SAlex Elder }
166184f9bd12SAlex Elder 
166284f9bd12SAlex Elder int ipa_endpoint_enable_one(struct ipa_endpoint *endpoint)
166384f9bd12SAlex Elder {
166484f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
166584f9bd12SAlex Elder 	struct gsi *gsi = &ipa->gsi;
166684f9bd12SAlex Elder 	int ret;
166784f9bd12SAlex Elder 
166884f9bd12SAlex Elder 	ret = gsi_channel_start(gsi, endpoint->channel_id);
166984f9bd12SAlex Elder 	if (ret) {
167084f9bd12SAlex Elder 		dev_err(&ipa->pdev->dev,
167184f9bd12SAlex Elder 			"error %d starting %cX channel %u for endpoint %u\n",
167284f9bd12SAlex Elder 			ret, endpoint->toward_ipa ? 'T' : 'R',
167384f9bd12SAlex Elder 			endpoint->channel_id, endpoint->endpoint_id);
167484f9bd12SAlex Elder 		return ret;
167584f9bd12SAlex Elder 	}
167684f9bd12SAlex Elder 
167784f9bd12SAlex Elder 	if (!endpoint->toward_ipa) {
167884f9bd12SAlex Elder 		ipa_interrupt_suspend_enable(ipa->interrupt,
167984f9bd12SAlex Elder 					     endpoint->endpoint_id);
168084f9bd12SAlex Elder 		ipa_endpoint_replenish_enable(endpoint);
168184f9bd12SAlex Elder 	}
168284f9bd12SAlex Elder 
168384f9bd12SAlex Elder 	ipa->enabled |= BIT(endpoint->endpoint_id);
168484f9bd12SAlex Elder 
168584f9bd12SAlex Elder 	return 0;
168684f9bd12SAlex Elder }
168784f9bd12SAlex Elder 
168884f9bd12SAlex Elder void ipa_endpoint_disable_one(struct ipa_endpoint *endpoint)
168984f9bd12SAlex Elder {
169084f9bd12SAlex Elder 	u32 mask = BIT(endpoint->endpoint_id);
169184f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
1692f30dcb7dSAlex Elder 	struct gsi *gsi = &ipa->gsi;
169384f9bd12SAlex Elder 	int ret;
169484f9bd12SAlex Elder 
1695f30dcb7dSAlex Elder 	if (!(ipa->enabled & mask))
169684f9bd12SAlex Elder 		return;
169784f9bd12SAlex Elder 
1698f30dcb7dSAlex Elder 	ipa->enabled ^= mask;
169984f9bd12SAlex Elder 
170084f9bd12SAlex Elder 	if (!endpoint->toward_ipa) {
170184f9bd12SAlex Elder 		ipa_endpoint_replenish_disable(endpoint);
170284f9bd12SAlex Elder 		ipa_interrupt_suspend_disable(ipa->interrupt,
170384f9bd12SAlex Elder 					      endpoint->endpoint_id);
170484f9bd12SAlex Elder 	}
170584f9bd12SAlex Elder 
170684f9bd12SAlex Elder 	/* Note that if stop fails, the channel's state is not well-defined */
1707f30dcb7dSAlex Elder 	ret = gsi_channel_stop(gsi, endpoint->channel_id);
170884f9bd12SAlex Elder 	if (ret)
170984f9bd12SAlex Elder 		dev_err(&ipa->pdev->dev,
171084f9bd12SAlex Elder 			"error %d attempting to stop endpoint %u\n", ret,
171184f9bd12SAlex Elder 			endpoint->endpoint_id);
171284f9bd12SAlex Elder }
171384f9bd12SAlex Elder 
171484f9bd12SAlex Elder void ipa_endpoint_suspend_one(struct ipa_endpoint *endpoint)
171584f9bd12SAlex Elder {
171684f9bd12SAlex Elder 	struct device *dev = &endpoint->ipa->pdev->dev;
171784f9bd12SAlex Elder 	struct gsi *gsi = &endpoint->ipa->gsi;
171884f9bd12SAlex Elder 	int ret;
171984f9bd12SAlex Elder 
172084f9bd12SAlex Elder 	if (!(endpoint->ipa->enabled & BIT(endpoint->endpoint_id)))
172184f9bd12SAlex Elder 		return;
172284f9bd12SAlex Elder 
1723ab4f71e5SAlex Elder 	if (!endpoint->toward_ipa) {
172484f9bd12SAlex Elder 		ipa_endpoint_replenish_disable(endpoint);
17254fa95248SAlex Elder 		(void)ipa_endpoint_program_suspend(endpoint, true);
1726ab4f71e5SAlex Elder 	}
172784f9bd12SAlex Elder 
1728decfef0fSAlex Elder 	ret = gsi_channel_suspend(gsi, endpoint->channel_id);
172984f9bd12SAlex Elder 	if (ret)
173084f9bd12SAlex Elder 		dev_err(dev, "error %d suspending channel %u\n", ret,
173184f9bd12SAlex Elder 			endpoint->channel_id);
173284f9bd12SAlex Elder }
173384f9bd12SAlex Elder 
173484f9bd12SAlex Elder void ipa_endpoint_resume_one(struct ipa_endpoint *endpoint)
173584f9bd12SAlex Elder {
173684f9bd12SAlex Elder 	struct device *dev = &endpoint->ipa->pdev->dev;
173784f9bd12SAlex Elder 	struct gsi *gsi = &endpoint->ipa->gsi;
173884f9bd12SAlex Elder 	int ret;
173984f9bd12SAlex Elder 
174084f9bd12SAlex Elder 	if (!(endpoint->ipa->enabled & BIT(endpoint->endpoint_id)))
174184f9bd12SAlex Elder 		return;
174284f9bd12SAlex Elder 
1743b07f283eSAlex Elder 	if (!endpoint->toward_ipa)
17444fa95248SAlex Elder 		(void)ipa_endpoint_program_suspend(endpoint, false);
174584f9bd12SAlex Elder 
1746decfef0fSAlex Elder 	ret = gsi_channel_resume(gsi, endpoint->channel_id);
174784f9bd12SAlex Elder 	if (ret)
174884f9bd12SAlex Elder 		dev_err(dev, "error %d resuming channel %u\n", ret,
174984f9bd12SAlex Elder 			endpoint->channel_id);
175084f9bd12SAlex Elder 	else if (!endpoint->toward_ipa)
175184f9bd12SAlex Elder 		ipa_endpoint_replenish_enable(endpoint);
175284f9bd12SAlex Elder }
175384f9bd12SAlex Elder 
175484f9bd12SAlex Elder void ipa_endpoint_suspend(struct ipa *ipa)
175584f9bd12SAlex Elder {
1756d1704382SAlex Elder 	if (!ipa->setup_complete)
1757d1704382SAlex Elder 		return;
1758d1704382SAlex Elder 
175984f9bd12SAlex Elder 	if (ipa->modem_netdev)
176084f9bd12SAlex Elder 		ipa_modem_suspend(ipa->modem_netdev);
176184f9bd12SAlex Elder 
176284f9bd12SAlex Elder 	ipa_endpoint_suspend_one(ipa->name_map[IPA_ENDPOINT_AP_LAN_RX]);
176384f9bd12SAlex Elder 	ipa_endpoint_suspend_one(ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]);
176484f9bd12SAlex Elder }
176584f9bd12SAlex Elder 
176684f9bd12SAlex Elder void ipa_endpoint_resume(struct ipa *ipa)
176784f9bd12SAlex Elder {
1768d1704382SAlex Elder 	if (!ipa->setup_complete)
1769d1704382SAlex Elder 		return;
1770d1704382SAlex Elder 
177184f9bd12SAlex Elder 	ipa_endpoint_resume_one(ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]);
177284f9bd12SAlex Elder 	ipa_endpoint_resume_one(ipa->name_map[IPA_ENDPOINT_AP_LAN_RX]);
177384f9bd12SAlex Elder 
177484f9bd12SAlex Elder 	if (ipa->modem_netdev)
177584f9bd12SAlex Elder 		ipa_modem_resume(ipa->modem_netdev);
177684f9bd12SAlex Elder }
177784f9bd12SAlex Elder 
177884f9bd12SAlex Elder static void ipa_endpoint_setup_one(struct ipa_endpoint *endpoint)
177984f9bd12SAlex Elder {
178084f9bd12SAlex Elder 	struct gsi *gsi = &endpoint->ipa->gsi;
178184f9bd12SAlex Elder 	u32 channel_id = endpoint->channel_id;
178284f9bd12SAlex Elder 
178384f9bd12SAlex Elder 	/* Only AP endpoints get set up */
178484f9bd12SAlex Elder 	if (endpoint->ee_id != GSI_EE_AP)
178584f9bd12SAlex Elder 		return;
178684f9bd12SAlex Elder 
1787317595d2SAlex Elder 	endpoint->skb_frag_max = gsi->channel[channel_id].trans_tre_max - 1;
178884f9bd12SAlex Elder 	if (!endpoint->toward_ipa) {
178984f9bd12SAlex Elder 		/* RX transactions require a single TRE, so the maximum
179084f9bd12SAlex Elder 		 * backlog is the same as the maximum outstanding TREs.
179184f9bd12SAlex Elder 		 */
1792c1aaa01dSAlex Elder 		clear_bit(IPA_REPLENISH_ENABLED, endpoint->replenish_flags);
1793998c0bd2SAlex Elder 		clear_bit(IPA_REPLENISH_ACTIVE, endpoint->replenish_flags);
179484f9bd12SAlex Elder 		INIT_DELAYED_WORK(&endpoint->replenish_work,
179584f9bd12SAlex Elder 				  ipa_endpoint_replenish_work);
179684f9bd12SAlex Elder 	}
179784f9bd12SAlex Elder 
179884f9bd12SAlex Elder 	ipa_endpoint_program(endpoint);
179984f9bd12SAlex Elder 
180084f9bd12SAlex Elder 	endpoint->ipa->set_up |= BIT(endpoint->endpoint_id);
180184f9bd12SAlex Elder }
180284f9bd12SAlex Elder 
180384f9bd12SAlex Elder static void ipa_endpoint_teardown_one(struct ipa_endpoint *endpoint)
180484f9bd12SAlex Elder {
180584f9bd12SAlex Elder 	endpoint->ipa->set_up &= ~BIT(endpoint->endpoint_id);
180684f9bd12SAlex Elder 
180784f9bd12SAlex Elder 	if (!endpoint->toward_ipa)
180884f9bd12SAlex Elder 		cancel_delayed_work_sync(&endpoint->replenish_work);
180984f9bd12SAlex Elder 
181084f9bd12SAlex Elder 	ipa_endpoint_reset(endpoint);
181184f9bd12SAlex Elder }
181284f9bd12SAlex Elder 
181384f9bd12SAlex Elder void ipa_endpoint_setup(struct ipa *ipa)
181484f9bd12SAlex Elder {
181584f9bd12SAlex Elder 	u32 initialized = ipa->initialized;
181684f9bd12SAlex Elder 
181784f9bd12SAlex Elder 	ipa->set_up = 0;
181884f9bd12SAlex Elder 	while (initialized) {
181984f9bd12SAlex Elder 		u32 endpoint_id = __ffs(initialized);
182084f9bd12SAlex Elder 
182184f9bd12SAlex Elder 		initialized ^= BIT(endpoint_id);
182284f9bd12SAlex Elder 
182384f9bd12SAlex Elder 		ipa_endpoint_setup_one(&ipa->endpoint[endpoint_id]);
182484f9bd12SAlex Elder 	}
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 
184184f9bd12SAlex Elder int ipa_endpoint_config(struct ipa *ipa)
184284f9bd12SAlex Elder {
184384f9bd12SAlex Elder 	struct device *dev = &ipa->pdev->dev;
18446a244b75SAlex Elder 	const struct ipa_reg *reg;
184584f9bd12SAlex Elder 	u32 initialized;
1846*2b87d721SAlex Elder 	u32 tx_count;
1847*2b87d721SAlex Elder 	u32 rx_count;
184884f9bd12SAlex Elder 	u32 rx_base;
1849*2b87d721SAlex Elder 	u32 limit;
185084f9bd12SAlex Elder 	u32 val;
185184f9bd12SAlex Elder 
1852110971d1SAlex Elder 	/* Prior to IPA v3.5, the FLAVOR_0 register was not supported.
1853110971d1SAlex Elder 	 * Furthermore, the endpoints were not grouped such that TX
1854110971d1SAlex Elder 	 * endpoint numbers started with 0 and RX endpoints had numbers
1855110971d1SAlex Elder 	 * higher than all TX endpoints, so we can't do the simple
1856110971d1SAlex Elder 	 * direction check used for newer hardware below.
1857110971d1SAlex Elder 	 *
1858110971d1SAlex Elder 	 * For hardware that doesn't support the FLAVOR_0 register,
1859110971d1SAlex Elder 	 * just set the available mask to support any endpoint, and
1860110971d1SAlex Elder 	 * assume the configuration is valid.
1861110971d1SAlex Elder 	 */
1862110971d1SAlex Elder 	if (ipa->version < IPA_VERSION_3_5) {
1863110971d1SAlex Elder 		ipa->available = ~0;
1864110971d1SAlex Elder 		return 0;
1865110971d1SAlex Elder 	}
1866110971d1SAlex Elder 
186784f9bd12SAlex Elder 	/* Find out about the endpoints supplied by the hardware, and ensure
1868*2b87d721SAlex Elder 	 * the highest one doesn't exceed the number supported by software.
186984f9bd12SAlex Elder 	 */
18706a244b75SAlex Elder 	reg = ipa_reg(ipa, FLAVOR_0);
18716a244b75SAlex Elder 	val = ioread32(ipa->reg_virt + ipa_reg_offset(reg));
187284f9bd12SAlex Elder 
1873*2b87d721SAlex Elder 	/* Our RX is an IPA producer; our TX is an IPA consumer. */
1874*2b87d721SAlex Elder 	tx_count = ipa_reg_decode(reg, MAX_CONS_PIPES, val);
1875*2b87d721SAlex Elder 	rx_count = ipa_reg_decode(reg, MAX_PROD_PIPES, val);
18769265a4f0SAlex Elder 	rx_base = ipa_reg_decode(reg, PROD_LOWEST, val);
1877*2b87d721SAlex Elder 
1878*2b87d721SAlex Elder 	limit = rx_base + rx_count;
1879*2b87d721SAlex Elder 	if (limit > IPA_ENDPOINT_MAX) {
1880*2b87d721SAlex Elder 		dev_err(dev, "too many endpoints, %u > %u\n",
1881*2b87d721SAlex Elder 			limit, IPA_ENDPOINT_MAX);
188284f9bd12SAlex Elder 		return -EINVAL;
188384f9bd12SAlex Elder 	}
188484f9bd12SAlex Elder 
1885*2b87d721SAlex Elder 	/* Mark all supported RX and TX endpoints as available */
1886*2b87d721SAlex Elder 	ipa->available = GENMASK(limit - 1, rx_base) | GENMASK(tx_count - 1, 0);
188784f9bd12SAlex Elder 
188884f9bd12SAlex Elder 	initialized = ipa->initialized;
188984f9bd12SAlex Elder 	while (initialized) {
189084f9bd12SAlex Elder 		u32 endpoint_id = __ffs(initialized);
189184f9bd12SAlex Elder 		struct ipa_endpoint *endpoint;
189284f9bd12SAlex Elder 
189384f9bd12SAlex Elder 		initialized ^= BIT(endpoint_id);
189484f9bd12SAlex Elder 
1895*2b87d721SAlex Elder 		if (endpoint_id >= limit) {
1896*2b87d721SAlex Elder 			dev_err(dev, "invalid endpoint id, %u > %u\n",
1897*2b87d721SAlex Elder 				endpoint_id, limit - 1);
1898*2b87d721SAlex Elder 			return -EINVAL;
189984f9bd12SAlex Elder 		}
190084f9bd12SAlex Elder 
1901*2b87d721SAlex Elder 		if (!(BIT(endpoint_id) & ipa->available)) {
1902*2b87d721SAlex Elder 			dev_err(dev, "unavailable endpoint id %u\n",
1903*2b87d721SAlex Elder 				endpoint_id);
1904*2b87d721SAlex Elder 			return -EINVAL;
1905*2b87d721SAlex Elder 		}
1906*2b87d721SAlex Elder 
1907*2b87d721SAlex Elder 		/* Make sure it's pointing in the right direction */
1908*2b87d721SAlex Elder 		endpoint = &ipa->endpoint[endpoint_id];
1909*2b87d721SAlex Elder 		if (endpoint->toward_ipa) {
1910*2b87d721SAlex Elder 			if (endpoint_id < tx_count)
1911*2b87d721SAlex Elder 				continue;
1912*2b87d721SAlex Elder 		} else if (endpoint_id >= rx_base) {
1913*2b87d721SAlex Elder 			continue;
1914*2b87d721SAlex Elder 		}
1915*2b87d721SAlex Elder 
1916*2b87d721SAlex Elder 		dev_err(dev, "endpoint id %u wrong direction\n", endpoint_id);
1917*2b87d721SAlex Elder 		return -EINVAL;
1918*2b87d721SAlex Elder 	}
1919*2b87d721SAlex Elder 
1920*2b87d721SAlex Elder 	return 0;
192184f9bd12SAlex Elder }
192284f9bd12SAlex Elder 
192384f9bd12SAlex Elder void ipa_endpoint_deconfig(struct ipa *ipa)
192484f9bd12SAlex Elder {
192584f9bd12SAlex Elder 	ipa->available = 0;	/* Nothing more to do */
192684f9bd12SAlex Elder }
192784f9bd12SAlex Elder 
192884f9bd12SAlex Elder static void ipa_endpoint_init_one(struct ipa *ipa, enum ipa_endpoint_name name,
192984f9bd12SAlex Elder 				  const struct ipa_gsi_endpoint_data *data)
193084f9bd12SAlex Elder {
193184f9bd12SAlex Elder 	struct ipa_endpoint *endpoint;
193284f9bd12SAlex Elder 
193384f9bd12SAlex Elder 	endpoint = &ipa->endpoint[data->endpoint_id];
193484f9bd12SAlex Elder 
193584f9bd12SAlex Elder 	if (data->ee_id == GSI_EE_AP)
193684f9bd12SAlex Elder 		ipa->channel_map[data->channel_id] = endpoint;
193784f9bd12SAlex Elder 	ipa->name_map[name] = endpoint;
193884f9bd12SAlex Elder 
193984f9bd12SAlex Elder 	endpoint->ipa = ipa;
194084f9bd12SAlex Elder 	endpoint->ee_id = data->ee_id;
194184f9bd12SAlex Elder 	endpoint->channel_id = data->channel_id;
194284f9bd12SAlex Elder 	endpoint->endpoint_id = data->endpoint_id;
194384f9bd12SAlex Elder 	endpoint->toward_ipa = data->toward_ipa;
1944660e52d6SAlex Elder 	endpoint->config = data->endpoint.config;
194584f9bd12SAlex Elder 
194684f9bd12SAlex Elder 	ipa->initialized |= BIT(endpoint->endpoint_id);
194784f9bd12SAlex Elder }
194884f9bd12SAlex Elder 
1949602a1c76SAlex Elder static void ipa_endpoint_exit_one(struct ipa_endpoint *endpoint)
195084f9bd12SAlex Elder {
195184f9bd12SAlex Elder 	endpoint->ipa->initialized &= ~BIT(endpoint->endpoint_id);
195284f9bd12SAlex Elder 
195384f9bd12SAlex Elder 	memset(endpoint, 0, sizeof(*endpoint));
195484f9bd12SAlex Elder }
195584f9bd12SAlex Elder 
195684f9bd12SAlex Elder void ipa_endpoint_exit(struct ipa *ipa)
195784f9bd12SAlex Elder {
195884f9bd12SAlex Elder 	u32 initialized = ipa->initialized;
195984f9bd12SAlex Elder 
196084f9bd12SAlex Elder 	while (initialized) {
196184f9bd12SAlex Elder 		u32 endpoint_id = __fls(initialized);
196284f9bd12SAlex Elder 
196384f9bd12SAlex Elder 		initialized ^= BIT(endpoint_id);
196484f9bd12SAlex Elder 
196584f9bd12SAlex Elder 		ipa_endpoint_exit_one(&ipa->endpoint[endpoint_id]);
196684f9bd12SAlex Elder 	}
196784f9bd12SAlex Elder 	memset(ipa->name_map, 0, sizeof(ipa->name_map));
196884f9bd12SAlex Elder 	memset(ipa->channel_map, 0, sizeof(ipa->channel_map));
196984f9bd12SAlex Elder }
197084f9bd12SAlex Elder 
197184f9bd12SAlex Elder /* Returns a bitmask of endpoints that support filtering, or 0 on error */
197284f9bd12SAlex Elder u32 ipa_endpoint_init(struct ipa *ipa, u32 count,
197384f9bd12SAlex Elder 		      const struct ipa_gsi_endpoint_data *data)
197484f9bd12SAlex Elder {
197584f9bd12SAlex Elder 	enum ipa_endpoint_name name;
197684f9bd12SAlex Elder 	u32 filter_map;
197784f9bd12SAlex Elder 
19789654d8c4SAlex Elder 	BUILD_BUG_ON(!IPA_REPLENISH_BATCH);
19799654d8c4SAlex Elder 
198084f9bd12SAlex Elder 	if (!ipa_endpoint_data_valid(ipa, count, data))
198184f9bd12SAlex Elder 		return 0;	/* Error */
198284f9bd12SAlex Elder 
198384f9bd12SAlex Elder 	ipa->initialized = 0;
198484f9bd12SAlex Elder 
198584f9bd12SAlex Elder 	filter_map = 0;
198684f9bd12SAlex Elder 	for (name = 0; name < count; name++, data++) {
198784f9bd12SAlex Elder 		if (ipa_gsi_endpoint_data_empty(data))
198884f9bd12SAlex Elder 			continue;	/* Skip over empty slots */
198984f9bd12SAlex Elder 
199084f9bd12SAlex Elder 		ipa_endpoint_init_one(ipa, name, data);
199184f9bd12SAlex Elder 
199284f9bd12SAlex Elder 		if (data->endpoint.filter_support)
199384f9bd12SAlex Elder 			filter_map |= BIT(data->endpoint_id);
19942091c79aSAlex Elder 		if (data->ee_id == GSI_EE_MODEM && data->toward_ipa)
19952091c79aSAlex Elder 			ipa->modem_tx_count++;
199684f9bd12SAlex Elder 	}
199784f9bd12SAlex Elder 
199884f9bd12SAlex Elder 	if (!ipa_filter_map_valid(ipa, filter_map))
199984f9bd12SAlex Elder 		goto err_endpoint_exit;
200084f9bd12SAlex Elder 
200184f9bd12SAlex Elder 	return filter_map;	/* Non-zero bitmask */
200284f9bd12SAlex Elder 
200384f9bd12SAlex Elder err_endpoint_exit:
200484f9bd12SAlex Elder 	ipa_endpoint_exit(ipa);
200584f9bd12SAlex Elder 
200684f9bd12SAlex Elder 	return 0;	/* Error */
200784f9bd12SAlex Elder }
2008