xref: /linux/drivers/net/ipa/ipa_endpoint.c (revision 4468a3448b6aa1c00f25ce1162c57d4a7c2e7ba2)
184f9bd12SAlex Elder // SPDX-License-Identifier: GPL-2.0
284f9bd12SAlex Elder 
384f9bd12SAlex Elder /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
4647a05f3SAlex Elder  * Copyright (C) 2019-2021 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 
2684f9bd12SAlex Elder #define atomic_dec_not_zero(v)	atomic_add_unless((v), -1, 0)
2784f9bd12SAlex Elder 
289654d8c4SAlex Elder /* Hardware is told about receive buffers once a "batch" has been queued */
299654d8c4SAlex Elder #define IPA_REPLENISH_BATCH	16		/* Must be non-zero */
3084f9bd12SAlex Elder 
3184f9bd12SAlex Elder /* The amount of RX buffer space consumed by standard skb overhead */
3284f9bd12SAlex Elder #define IPA_RX_BUFFER_OVERHEAD	(PAGE_SIZE - SKB_MAX_ORDER(NET_SKB_PAD, 0))
3384f9bd12SAlex Elder 
348730f45dSAlex Elder /* Where to find the QMAP mux_id for a packet within modem-supplied metadata */
358730f45dSAlex Elder #define IPA_ENDPOINT_QMAP_METADATA_MASK		0x000000ff /* host byte order */
368730f45dSAlex Elder 
3784f9bd12SAlex Elder #define IPA_ENDPOINT_RESET_AGGR_RETRY_MAX	3
3884f9bd12SAlex Elder 
3984f9bd12SAlex Elder /** enum ipa_status_opcode - status element opcode hardware values */
4084f9bd12SAlex Elder enum ipa_status_opcode {
4184f9bd12SAlex Elder 	IPA_STATUS_OPCODE_PACKET		= 0x01,
4284f9bd12SAlex Elder 	IPA_STATUS_OPCODE_DROPPED_PACKET	= 0x04,
4384f9bd12SAlex Elder 	IPA_STATUS_OPCODE_SUSPENDED_PACKET	= 0x08,
4484f9bd12SAlex Elder 	IPA_STATUS_OPCODE_PACKET_2ND_PASS	= 0x40,
4584f9bd12SAlex Elder };
4684f9bd12SAlex Elder 
4784f9bd12SAlex Elder /** enum ipa_status_exception - status element exception type */
4884f9bd12SAlex Elder enum ipa_status_exception {
4984f9bd12SAlex Elder 	/* 0 means no exception */
5084f9bd12SAlex Elder 	IPA_STATUS_EXCEPTION_DEAGGR		= 0x01,
5184f9bd12SAlex Elder };
5284f9bd12SAlex Elder 
5384f9bd12SAlex Elder /* Status element provided by hardware */
5484f9bd12SAlex Elder struct ipa_status {
5584f9bd12SAlex Elder 	u8 opcode;		/* enum ipa_status_opcode */
5684f9bd12SAlex Elder 	u8 exception;		/* enum ipa_status_exception */
5784f9bd12SAlex Elder 	__le16 mask;
5884f9bd12SAlex Elder 	__le16 pkt_len;
5984f9bd12SAlex Elder 	u8 endp_src_idx;
6084f9bd12SAlex Elder 	u8 endp_dst_idx;
6184f9bd12SAlex Elder 	__le32 metadata;
6284f9bd12SAlex Elder 	__le32 flags1;
6384f9bd12SAlex Elder 	__le64 flags2;
6484f9bd12SAlex Elder 	__le32 flags3;
6584f9bd12SAlex Elder 	__le32 flags4;
6684f9bd12SAlex Elder };
6784f9bd12SAlex Elder 
6884f9bd12SAlex Elder /* Field masks for struct ipa_status structure fields */
69f6aba7b5SAlex Elder #define IPA_STATUS_MASK_TAG_VALID_FMASK		GENMASK(4, 4)
70f6aba7b5SAlex Elder #define IPA_STATUS_SRC_IDX_FMASK		GENMASK(4, 0)
7184f9bd12SAlex Elder #define IPA_STATUS_DST_IDX_FMASK		GENMASK(4, 0)
7284f9bd12SAlex Elder #define IPA_STATUS_FLAGS1_RT_RULE_ID_FMASK	GENMASK(31, 22)
73f6aba7b5SAlex Elder #define IPA_STATUS_FLAGS2_TAG_FMASK		GENMASK_ULL(63, 16)
7484f9bd12SAlex Elder 
75ed23f026SAlex Elder static u32 aggr_byte_limit_max(enum ipa_version version)
76ed23f026SAlex Elder {
77ed23f026SAlex Elder 	if (version < IPA_VERSION_4_5)
78ed23f026SAlex Elder 		return field_max(aggr_byte_limit_fmask(true));
79ed23f026SAlex Elder 
80ed23f026SAlex Elder 	return field_max(aggr_byte_limit_fmask(false));
81ed23f026SAlex Elder }
82ed23f026SAlex Elder 
833cebb7c2SAlex Elder /* Compute the aggregation size value to use for a given buffer size */
843cebb7c2SAlex Elder static u32 ipa_aggr_size_kb(u32 rx_buffer_size, bool aggr_hard_limit)
853cebb7c2SAlex Elder {
863cebb7c2SAlex Elder 	/* A hard aggregation limit will not be crossed; aggregation closes
873cebb7c2SAlex Elder 	 * if saving incoming data would cross the hard byte limit boundary.
883cebb7c2SAlex Elder 	 *
893cebb7c2SAlex Elder 	 * With a soft limit, aggregation closes *after* the size boundary
903cebb7c2SAlex Elder 	 * has been crossed.  In that case the limit must leave enough space
913cebb7c2SAlex Elder 	 * after that limit to receive a full MTU of data plus overhead.
923cebb7c2SAlex Elder 	 */
933cebb7c2SAlex Elder 	if (!aggr_hard_limit)
943cebb7c2SAlex Elder 		rx_buffer_size -= IPA_MTU + IPA_RX_BUFFER_OVERHEAD;
953cebb7c2SAlex Elder 
963cebb7c2SAlex Elder 	/* The byte limit is encoded as a number of kilobytes */
973cebb7c2SAlex Elder 
983cebb7c2SAlex Elder 	return rx_buffer_size / SZ_1K;
993cebb7c2SAlex Elder }
1003cebb7c2SAlex Elder 
10184f9bd12SAlex Elder static bool ipa_endpoint_data_valid_one(struct ipa *ipa, u32 count,
10284f9bd12SAlex Elder 			    const struct ipa_gsi_endpoint_data *all_data,
10384f9bd12SAlex Elder 			    const struct ipa_gsi_endpoint_data *data)
10484f9bd12SAlex Elder {
10584f9bd12SAlex Elder 	const struct ipa_gsi_endpoint_data *other_data;
10684f9bd12SAlex Elder 	struct device *dev = &ipa->pdev->dev;
10784f9bd12SAlex Elder 	enum ipa_endpoint_name other_name;
10884f9bd12SAlex Elder 
10984f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(data))
11084f9bd12SAlex Elder 		return true;
11184f9bd12SAlex Elder 
11284f9bd12SAlex Elder 	if (!data->toward_ipa) {
1133cebb7c2SAlex Elder 		const struct ipa_endpoint_rx *rx_config;
114ed23f026SAlex Elder 		u32 buffer_size;
1153cebb7c2SAlex Elder 		u32 aggr_size;
116ed23f026SAlex Elder 		u32 limit;
117ed23f026SAlex Elder 
11884f9bd12SAlex Elder 		if (data->endpoint.filter_support) {
11984f9bd12SAlex Elder 			dev_err(dev, "filtering not supported for "
12084f9bd12SAlex Elder 					"RX endpoint %u\n",
12184f9bd12SAlex Elder 				data->endpoint_id);
12284f9bd12SAlex Elder 			return false;
12384f9bd12SAlex Elder 		}
12484f9bd12SAlex Elder 
125ed23f026SAlex Elder 		/* Nothing more to check for non-AP RX */
126ed23f026SAlex Elder 		if (data->ee_id != GSI_EE_AP)
127ed23f026SAlex Elder 			return true;
128ed23f026SAlex Elder 
1293cebb7c2SAlex Elder 		rx_config = &data->endpoint.config.rx;
1303cebb7c2SAlex Elder 
131ed23f026SAlex Elder 		/* The buffer size must hold an MTU plus overhead */
1323cebb7c2SAlex Elder 		buffer_size = rx_config->buffer_size;
133ed23f026SAlex Elder 		limit = IPA_MTU + IPA_RX_BUFFER_OVERHEAD;
134ed23f026SAlex Elder 		if (buffer_size < limit) {
135ed23f026SAlex Elder 			dev_err(dev, "RX buffer size too small for RX endpoint %u (%u < %u)\n",
136ed23f026SAlex Elder 				data->endpoint_id, buffer_size, limit);
137ed23f026SAlex Elder 			return false;
138ed23f026SAlex Elder 		}
139ed23f026SAlex Elder 
1403cebb7c2SAlex Elder 		if (!data->endpoint.config.aggregation) {
1413cebb7c2SAlex Elder 			bool result = true;
1423cebb7c2SAlex Elder 
1433cebb7c2SAlex Elder 			/* No aggregation; check for bogus aggregation data */
144beb90cbaSAlex Elder 			if (rx_config->aggr_time_limit) {
145beb90cbaSAlex Elder 				dev_err(dev,
146beb90cbaSAlex Elder 					"time limit with no aggregation for RX endpoint %u\n",
147beb90cbaSAlex Elder 					data->endpoint_id);
148beb90cbaSAlex Elder 				result = false;
149beb90cbaSAlex Elder 			}
150beb90cbaSAlex Elder 
1513cebb7c2SAlex Elder 			if (rx_config->aggr_hard_limit) {
1523cebb7c2SAlex Elder 				dev_err(dev, "hard limit with no aggregation for RX endpoint %u\n",
1533cebb7c2SAlex Elder 					data->endpoint_id);
1543cebb7c2SAlex Elder 				result = false;
1553cebb7c2SAlex Elder 			}
1563cebb7c2SAlex Elder 
1573cebb7c2SAlex Elder 			if (rx_config->aggr_close_eof) {
1583cebb7c2SAlex Elder 				dev_err(dev, "close EOF with no aggregation for RX endpoint %u\n",
1593cebb7c2SAlex Elder 					data->endpoint_id);
1603cebb7c2SAlex Elder 				result = false;
1613cebb7c2SAlex Elder 			}
1623cebb7c2SAlex Elder 
1633cebb7c2SAlex Elder 			return result;	/* Nothing more to check */
1643cebb7c2SAlex Elder 		}
1653cebb7c2SAlex Elder 
1663cebb7c2SAlex Elder 		/* For an endpoint supporting receive aggregation, the byte
1673cebb7c2SAlex Elder 		 * limit defines the point at which aggregation closes.  This
1683cebb7c2SAlex Elder 		 * check ensures the receive buffer size doesn't result in a
1693cebb7c2SAlex Elder 		 * limit that exceeds what's representable in the aggregation
1703cebb7c2SAlex Elder 		 * byte limit field.
171ed23f026SAlex Elder 		 */
1723cebb7c2SAlex Elder 		aggr_size = ipa_aggr_size_kb(buffer_size - NET_SKB_PAD,
1733cebb7c2SAlex Elder 					     rx_config->aggr_hard_limit);
1743cebb7c2SAlex Elder 		limit = aggr_byte_limit_max(ipa->version);
1753cebb7c2SAlex Elder 		if (aggr_size > limit) {
1763cebb7c2SAlex Elder 			dev_err(dev, "aggregated size too large for RX endpoint %u (%u KB > %u KB)\n",
1773cebb7c2SAlex Elder 				data->endpoint_id, aggr_size, limit);
178ed23f026SAlex Elder 
179ed23f026SAlex Elder 			return false;
180ed23f026SAlex Elder 		}
181ed23f026SAlex Elder 
18284f9bd12SAlex Elder 		return true;	/* Nothing more to check for RX */
18384f9bd12SAlex Elder 	}
18484f9bd12SAlex Elder 
185a14d5937SAlex Elder 	/* Starting with IPA v4.5 sequencer replication is obsolete */
186a14d5937SAlex Elder 	if (ipa->version >= IPA_VERSION_4_5) {
187a14d5937SAlex Elder 		if (data->endpoint.config.tx.seq_rep_type) {
188a14d5937SAlex Elder 			dev_err(dev, "no-zero seq_rep_type TX endpoint %u\n",
189a14d5937SAlex Elder 				data->endpoint_id);
190a14d5937SAlex Elder 			return false;
191a14d5937SAlex Elder 		}
192a14d5937SAlex Elder 	}
193a14d5937SAlex Elder 
19484f9bd12SAlex Elder 	if (data->endpoint.config.status_enable) {
19584f9bd12SAlex Elder 		other_name = data->endpoint.config.tx.status_endpoint;
19684f9bd12SAlex Elder 		if (other_name >= count) {
19784f9bd12SAlex Elder 			dev_err(dev, "status endpoint name %u out of range "
19884f9bd12SAlex Elder 					"for endpoint %u\n",
19984f9bd12SAlex Elder 				other_name, data->endpoint_id);
20084f9bd12SAlex Elder 			return false;
20184f9bd12SAlex Elder 		}
20284f9bd12SAlex Elder 
20384f9bd12SAlex Elder 		/* Status endpoint must be defined... */
20484f9bd12SAlex Elder 		other_data = &all_data[other_name];
20584f9bd12SAlex Elder 		if (ipa_gsi_endpoint_data_empty(other_data)) {
20684f9bd12SAlex Elder 			dev_err(dev, "DMA endpoint name %u undefined "
20784f9bd12SAlex Elder 					"for endpoint %u\n",
20884f9bd12SAlex Elder 				other_name, data->endpoint_id);
20984f9bd12SAlex Elder 			return false;
21084f9bd12SAlex Elder 		}
21184f9bd12SAlex Elder 
21284f9bd12SAlex Elder 		/* ...and has to be an RX endpoint... */
21384f9bd12SAlex Elder 		if (other_data->toward_ipa) {
21484f9bd12SAlex Elder 			dev_err(dev,
21584f9bd12SAlex Elder 				"status endpoint for endpoint %u not RX\n",
21684f9bd12SAlex Elder 				data->endpoint_id);
21784f9bd12SAlex Elder 			return false;
21884f9bd12SAlex Elder 		}
21984f9bd12SAlex Elder 
22084f9bd12SAlex Elder 		/* ...and if it's to be an AP endpoint... */
22184f9bd12SAlex Elder 		if (other_data->ee_id == GSI_EE_AP) {
22284f9bd12SAlex Elder 			/* ...make sure it has status enabled. */
22384f9bd12SAlex Elder 			if (!other_data->endpoint.config.status_enable) {
22484f9bd12SAlex Elder 				dev_err(dev,
22584f9bd12SAlex Elder 					"status not enabled for endpoint %u\n",
22684f9bd12SAlex Elder 					other_data->endpoint_id);
22784f9bd12SAlex Elder 				return false;
22884f9bd12SAlex Elder 			}
22984f9bd12SAlex Elder 		}
23084f9bd12SAlex Elder 	}
23184f9bd12SAlex Elder 
23284f9bd12SAlex Elder 	if (data->endpoint.config.dma_mode) {
23384f9bd12SAlex Elder 		other_name = data->endpoint.config.dma_endpoint;
23484f9bd12SAlex Elder 		if (other_name >= count) {
23584f9bd12SAlex Elder 			dev_err(dev, "DMA endpoint name %u out of range "
23684f9bd12SAlex Elder 					"for endpoint %u\n",
23784f9bd12SAlex Elder 				other_name, data->endpoint_id);
23884f9bd12SAlex Elder 			return false;
23984f9bd12SAlex Elder 		}
24084f9bd12SAlex Elder 
24184f9bd12SAlex Elder 		other_data = &all_data[other_name];
24284f9bd12SAlex Elder 		if (ipa_gsi_endpoint_data_empty(other_data)) {
24384f9bd12SAlex Elder 			dev_err(dev, "DMA endpoint name %u undefined "
24484f9bd12SAlex Elder 					"for endpoint %u\n",
24584f9bd12SAlex Elder 				other_name, data->endpoint_id);
24684f9bd12SAlex Elder 			return false;
24784f9bd12SAlex Elder 		}
24884f9bd12SAlex Elder 	}
24984f9bd12SAlex Elder 
25084f9bd12SAlex Elder 	return true;
25184f9bd12SAlex Elder }
25284f9bd12SAlex Elder 
25384f9bd12SAlex Elder static bool ipa_endpoint_data_valid(struct ipa *ipa, u32 count,
25484f9bd12SAlex Elder 				    const struct ipa_gsi_endpoint_data *data)
25584f9bd12SAlex Elder {
25684f9bd12SAlex Elder 	const struct ipa_gsi_endpoint_data *dp = data;
25784f9bd12SAlex Elder 	struct device *dev = &ipa->pdev->dev;
25884f9bd12SAlex Elder 	enum ipa_endpoint_name name;
25984f9bd12SAlex Elder 
26084f9bd12SAlex Elder 	if (count > IPA_ENDPOINT_COUNT) {
26184f9bd12SAlex Elder 		dev_err(dev, "too many endpoints specified (%u > %u)\n",
26284f9bd12SAlex Elder 			count, IPA_ENDPOINT_COUNT);
26384f9bd12SAlex Elder 		return false;
26484f9bd12SAlex Elder 	}
26584f9bd12SAlex Elder 
26684f9bd12SAlex Elder 	/* Make sure needed endpoints have defined data */
26784f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_COMMAND_TX])) {
26884f9bd12SAlex Elder 		dev_err(dev, "command TX endpoint not defined\n");
26984f9bd12SAlex Elder 		return false;
27084f9bd12SAlex Elder 	}
27184f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_LAN_RX])) {
27284f9bd12SAlex Elder 		dev_err(dev, "LAN RX endpoint not defined\n");
27384f9bd12SAlex Elder 		return false;
27484f9bd12SAlex Elder 	}
27584f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_MODEM_TX])) {
27684f9bd12SAlex Elder 		dev_err(dev, "AP->modem TX endpoint not defined\n");
27784f9bd12SAlex Elder 		return false;
27884f9bd12SAlex Elder 	}
27984f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_MODEM_RX])) {
28084f9bd12SAlex Elder 		dev_err(dev, "AP<-modem RX endpoint not defined\n");
28184f9bd12SAlex Elder 		return false;
28284f9bd12SAlex Elder 	}
28384f9bd12SAlex Elder 
28484f9bd12SAlex Elder 	for (name = 0; name < count; name++, dp++)
28584f9bd12SAlex Elder 		if (!ipa_endpoint_data_valid_one(ipa, count, data, dp))
28684f9bd12SAlex Elder 			return false;
28784f9bd12SAlex Elder 
28884f9bd12SAlex Elder 	return true;
28984f9bd12SAlex Elder }
29084f9bd12SAlex Elder 
29184f9bd12SAlex Elder /* Allocate a transaction to use on a non-command endpoint */
29284f9bd12SAlex Elder static struct gsi_trans *ipa_endpoint_trans_alloc(struct ipa_endpoint *endpoint,
29384f9bd12SAlex Elder 						  u32 tre_count)
29484f9bd12SAlex Elder {
29584f9bd12SAlex Elder 	struct gsi *gsi = &endpoint->ipa->gsi;
29684f9bd12SAlex Elder 	u32 channel_id = endpoint->channel_id;
29784f9bd12SAlex Elder 	enum dma_data_direction direction;
29884f9bd12SAlex Elder 
29984f9bd12SAlex Elder 	direction = endpoint->toward_ipa ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
30084f9bd12SAlex Elder 
30184f9bd12SAlex Elder 	return gsi_channel_trans_alloc(gsi, channel_id, tre_count, direction);
30284f9bd12SAlex Elder }
30384f9bd12SAlex Elder 
30484f9bd12SAlex Elder /* suspend_delay represents suspend for RX, delay for TX endpoints.
3054c9d631aSAlex Elder  * Note that suspend is not supported starting with IPA v4.0, and
3064c9d631aSAlex Elder  * delay mode should not be used starting with IPA v4.2.
30784f9bd12SAlex Elder  */
3084900bf34SAlex Elder static bool
30984f9bd12SAlex Elder ipa_endpoint_init_ctrl(struct ipa_endpoint *endpoint, bool suspend_delay)
31084f9bd12SAlex Elder {
31184f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
3126a244b75SAlex Elder 	const struct ipa_reg *reg;
313*4468a344SAlex Elder 	u32 field_id;
3146bfb7538SAlex Elder 	u32 offset;
3154900bf34SAlex Elder 	bool state;
31684f9bd12SAlex Elder 	u32 mask;
31784f9bd12SAlex Elder 	u32 val;
31884f9bd12SAlex Elder 
3195bc55884SAlex Elder 	if (endpoint->toward_ipa)
3204c9d631aSAlex Elder 		WARN_ON(ipa->version >= IPA_VERSION_4_2);
3215bc55884SAlex Elder 	else
3225bc55884SAlex Elder 		WARN_ON(ipa->version >= IPA_VERSION_4_0);
3235bc55884SAlex Elder 
3246a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_CTRL);
3256a244b75SAlex Elder 	offset = ipa_reg_n_offset(reg, endpoint->endpoint_id);
32684f9bd12SAlex Elder 	val = ioread32(ipa->reg_virt + offset);
3276a244b75SAlex Elder 
328*4468a344SAlex Elder 	field_id = endpoint->toward_ipa ? ENDP_DELAY : ENDP_SUSPEND;
329*4468a344SAlex Elder 	mask = ipa_reg_bit(reg, field_id);
330*4468a344SAlex Elder 
3314900bf34SAlex Elder 	state = !!(val & mask);
3325bc55884SAlex Elder 
3335bc55884SAlex Elder 	/* Don't bother if it's already in the requested state */
3344900bf34SAlex Elder 	if (suspend_delay != state) {
33584f9bd12SAlex Elder 		val ^= mask;
33684f9bd12SAlex Elder 		iowrite32(val, ipa->reg_virt + offset);
3374900bf34SAlex Elder 	}
33884f9bd12SAlex Elder 
3394900bf34SAlex Elder 	return state;
34084f9bd12SAlex Elder }
34184f9bd12SAlex Elder 
3424c9d631aSAlex Elder /* We don't care what the previous state was for delay mode */
3434fa95248SAlex Elder static void
3444fa95248SAlex Elder ipa_endpoint_program_delay(struct ipa_endpoint *endpoint, bool enable)
3454fa95248SAlex Elder {
3464c9d631aSAlex Elder 	/* Delay mode should not be used for IPA v4.2+ */
3474c9d631aSAlex Elder 	WARN_ON(endpoint->ipa->version >= IPA_VERSION_4_2);
3485bc55884SAlex Elder 	WARN_ON(!endpoint->toward_ipa);
3494fa95248SAlex Elder 
3504fa95248SAlex Elder 	(void)ipa_endpoint_init_ctrl(endpoint, enable);
3514fa95248SAlex Elder }
3524fa95248SAlex Elder 
353fff89971SAlex Elder static bool ipa_endpoint_aggr_active(struct ipa_endpoint *endpoint)
354fff89971SAlex Elder {
355fff89971SAlex Elder 	u32 mask = BIT(endpoint->endpoint_id);
356fff89971SAlex Elder 	struct ipa *ipa = endpoint->ipa;
3576a244b75SAlex Elder 	const struct ipa_reg *reg;
358fff89971SAlex Elder 	u32 val;
359fff89971SAlex Elder 
3605bc55884SAlex Elder 	WARN_ON(!(mask & ipa->available));
3615bc55884SAlex Elder 
3626a244b75SAlex Elder 	reg = ipa_reg(ipa, STATE_AGGR_ACTIVE);
3636a244b75SAlex Elder 	val = ioread32(ipa->reg_virt + ipa_reg_offset(reg));
364fff89971SAlex Elder 
365fff89971SAlex Elder 	return !!(val & mask);
366fff89971SAlex Elder }
367fff89971SAlex Elder 
368fff89971SAlex Elder static void ipa_endpoint_force_close(struct ipa_endpoint *endpoint)
369fff89971SAlex Elder {
370fff89971SAlex Elder 	u32 mask = BIT(endpoint->endpoint_id);
371fff89971SAlex Elder 	struct ipa *ipa = endpoint->ipa;
3726a244b75SAlex Elder 	const struct ipa_reg *reg;
373fff89971SAlex Elder 
3745bc55884SAlex Elder 	WARN_ON(!(mask & ipa->available));
3755bc55884SAlex Elder 
3766a244b75SAlex Elder 	reg = ipa_reg(ipa, AGGR_FORCE_CLOSE);
3776a244b75SAlex Elder 	iowrite32(mask, ipa->reg_virt + ipa_reg_offset(reg));
378fff89971SAlex Elder }
379fff89971SAlex Elder 
380fff89971SAlex Elder /**
381fff89971SAlex Elder  * ipa_endpoint_suspend_aggr() - Emulate suspend interrupt
382e3eea08eSAlex Elder  * @endpoint:	Endpoint on which to emulate a suspend
383fff89971SAlex Elder  *
384fff89971SAlex Elder  *  Emulate suspend IPA interrupt to unsuspend an endpoint suspended
385fff89971SAlex Elder  *  with an open aggregation frame.  This is to work around a hardware
386fff89971SAlex Elder  *  issue in IPA version 3.5.1 where the suspend interrupt will not be
387fff89971SAlex Elder  *  generated when it should be.
388fff89971SAlex Elder  */
389fff89971SAlex Elder static void ipa_endpoint_suspend_aggr(struct ipa_endpoint *endpoint)
390fff89971SAlex Elder {
391fff89971SAlex Elder 	struct ipa *ipa = endpoint->ipa;
392fff89971SAlex Elder 
393660e52d6SAlex Elder 	if (!endpoint->config.aggregation)
394fff89971SAlex Elder 		return;
395fff89971SAlex Elder 
396fff89971SAlex Elder 	/* Nothing to do if the endpoint doesn't have aggregation open */
397fff89971SAlex Elder 	if (!ipa_endpoint_aggr_active(endpoint))
398fff89971SAlex Elder 		return;
399fff89971SAlex Elder 
400fff89971SAlex Elder 	/* Force close aggregation */
401fff89971SAlex Elder 	ipa_endpoint_force_close(endpoint);
402fff89971SAlex Elder 
403fff89971SAlex Elder 	ipa_interrupt_simulate_suspend(ipa->interrupt);
404fff89971SAlex Elder }
405fff89971SAlex Elder 
406fff89971SAlex Elder /* Returns previous suspend state (true means suspend was enabled) */
4074fa95248SAlex Elder static bool
4084fa95248SAlex Elder ipa_endpoint_program_suspend(struct ipa_endpoint *endpoint, bool enable)
4094fa95248SAlex Elder {
410fff89971SAlex Elder 	bool suspended;
411fff89971SAlex Elder 
412d7f3087bSAlex Elder 	if (endpoint->ipa->version >= IPA_VERSION_4_0)
413b07f283eSAlex Elder 		return enable;	/* For IPA v4.0+, no change made */
414b07f283eSAlex Elder 
4155bc55884SAlex Elder 	WARN_ON(endpoint->toward_ipa);
4164fa95248SAlex Elder 
417fff89971SAlex Elder 	suspended = ipa_endpoint_init_ctrl(endpoint, enable);
418fff89971SAlex Elder 
419fff89971SAlex Elder 	/* A client suspended with an open aggregation frame will not
420fff89971SAlex Elder 	 * generate a SUSPEND IPA interrupt.  If enabling suspend, have
421fff89971SAlex Elder 	 * ipa_endpoint_suspend_aggr() handle this.
422fff89971SAlex Elder 	 */
423fff89971SAlex Elder 	if (enable && !suspended)
424fff89971SAlex Elder 		ipa_endpoint_suspend_aggr(endpoint);
425fff89971SAlex Elder 
426fff89971SAlex Elder 	return suspended;
4274fa95248SAlex Elder }
4284fa95248SAlex Elder 
4294c9d631aSAlex Elder /* Put all modem RX endpoints into suspend mode, and stop transmission
4304c9d631aSAlex Elder  * on all modem TX endpoints.  Prior to IPA v4.2, endpoint DELAY mode is
4314c9d631aSAlex Elder  * used for TX endpoints; starting with IPA v4.2 we use GSI channel flow
4324c9d631aSAlex Elder  * control instead.
4334c9d631aSAlex Elder  */
43484f9bd12SAlex Elder void ipa_endpoint_modem_pause_all(struct ipa *ipa, bool enable)
43584f9bd12SAlex Elder {
43684f9bd12SAlex Elder 	u32 endpoint_id;
43784f9bd12SAlex Elder 
43884f9bd12SAlex Elder 	for (endpoint_id = 0; endpoint_id < IPA_ENDPOINT_MAX; endpoint_id++) {
43984f9bd12SAlex Elder 		struct ipa_endpoint *endpoint = &ipa->endpoint[endpoint_id];
44084f9bd12SAlex Elder 
44184f9bd12SAlex Elder 		if (endpoint->ee_id != GSI_EE_MODEM)
44284f9bd12SAlex Elder 			continue;
44384f9bd12SAlex Elder 
4444c9d631aSAlex Elder 		if (!endpoint->toward_ipa)
4454c9d631aSAlex Elder 			(void)ipa_endpoint_program_suspend(endpoint, enable);
4464c9d631aSAlex Elder 		else if (ipa->version < IPA_VERSION_4_2)
4474fa95248SAlex Elder 			ipa_endpoint_program_delay(endpoint, enable);
448b07f283eSAlex Elder 		else
4494c9d631aSAlex Elder 			gsi_modem_channel_flow_control(&ipa->gsi,
4504c9d631aSAlex Elder 						       endpoint->channel_id,
4514c9d631aSAlex Elder 						       enable);
45284f9bd12SAlex Elder 	}
45384f9bd12SAlex Elder }
45484f9bd12SAlex Elder 
45584f9bd12SAlex Elder /* Reset all modem endpoints to use the default exception endpoint */
45684f9bd12SAlex Elder int ipa_endpoint_modem_exception_reset_all(struct ipa *ipa)
45784f9bd12SAlex Elder {
45884f9bd12SAlex Elder 	u32 initialized = ipa->initialized;
45984f9bd12SAlex Elder 	struct gsi_trans *trans;
46084f9bd12SAlex Elder 	u32 count;
46184f9bd12SAlex Elder 
4622091c79aSAlex Elder 	/* We need one command per modem TX endpoint, plus the commands
4632091c79aSAlex Elder 	 * that clear the pipeline.
46484f9bd12SAlex Elder 	 */
4652091c79aSAlex Elder 	count = ipa->modem_tx_count + ipa_cmd_pipeline_clear_count();
46684f9bd12SAlex Elder 	trans = ipa_cmd_trans_alloc(ipa, count);
46784f9bd12SAlex Elder 	if (!trans) {
46884f9bd12SAlex Elder 		dev_err(&ipa->pdev->dev,
46984f9bd12SAlex Elder 			"no transaction to reset modem exception endpoints\n");
47084f9bd12SAlex Elder 		return -EBUSY;
47184f9bd12SAlex Elder 	}
47284f9bd12SAlex Elder 
47384f9bd12SAlex Elder 	while (initialized) {
47484f9bd12SAlex Elder 		u32 endpoint_id = __ffs(initialized);
47584f9bd12SAlex Elder 		struct ipa_endpoint *endpoint;
4766a244b75SAlex Elder 		const struct ipa_reg *reg;
47784f9bd12SAlex Elder 		u32 offset;
47884f9bd12SAlex Elder 
47984f9bd12SAlex Elder 		initialized ^= BIT(endpoint_id);
48084f9bd12SAlex Elder 
48184f9bd12SAlex Elder 		/* We only reset modem TX endpoints */
48284f9bd12SAlex Elder 		endpoint = &ipa->endpoint[endpoint_id];
48384f9bd12SAlex Elder 		if (!(endpoint->ee_id == GSI_EE_MODEM && endpoint->toward_ipa))
48484f9bd12SAlex Elder 			continue;
48584f9bd12SAlex Elder 
4866a244b75SAlex Elder 		reg = ipa_reg(ipa, ENDP_STATUS);
4876a244b75SAlex Elder 		offset = ipa_reg_n_offset(reg, endpoint_id);
48884f9bd12SAlex Elder 
48984f9bd12SAlex Elder 		/* Value written is 0, and all bits are updated.  That
49084f9bd12SAlex Elder 		 * means status is disabled on the endpoint, and as a
49184f9bd12SAlex Elder 		 * result all other fields in the register are ignored.
49284f9bd12SAlex Elder 		 */
49384f9bd12SAlex Elder 		ipa_cmd_register_write_add(trans, offset, 0, ~0, false);
49484f9bd12SAlex Elder 	}
49584f9bd12SAlex Elder 
496aa56e3e5SAlex Elder 	ipa_cmd_pipeline_clear_add(trans);
49784f9bd12SAlex Elder 
49884f9bd12SAlex Elder 	gsi_trans_commit_wait(trans);
49984f9bd12SAlex Elder 
50051c48ce2SAlex Elder 	ipa_cmd_pipeline_clear_wait(ipa);
50151c48ce2SAlex Elder 
50284f9bd12SAlex Elder 	return 0;
50384f9bd12SAlex Elder }
50484f9bd12SAlex Elder 
50584f9bd12SAlex Elder static void ipa_endpoint_init_cfg(struct ipa_endpoint *endpoint)
50684f9bd12SAlex Elder {
5076a244b75SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
5086bfb7538SAlex Elder 	struct ipa *ipa = endpoint->ipa;
5095567d4d9SAlex Elder 	enum ipa_cs_offload_en enabled;
5106a244b75SAlex Elder 	const struct ipa_reg *reg;
51184f9bd12SAlex Elder 	u32 val = 0;
5126bfb7538SAlex Elder 
5136a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_CFG);
51484f9bd12SAlex Elder 	/* FRAG_OFFLOAD_EN is 0 */
515660e52d6SAlex Elder 	if (endpoint->config.checksum) {
5166bfb7538SAlex Elder 		enum ipa_version version = ipa->version;
5175567d4d9SAlex Elder 
51884f9bd12SAlex Elder 		if (endpoint->toward_ipa) {
5199eefd2fbSAlex Elder 			u32 off;
52084f9bd12SAlex Elder 
52184f9bd12SAlex Elder 			/* Checksum header offset is in 4-byte units */
522*4468a344SAlex Elder 			off = sizeof(struct rmnet_map_header) / sizeof(u32);
523*4468a344SAlex Elder 			val |= ipa_reg_encode(reg, CS_METADATA_HDR_OFFSET, off);
5245567d4d9SAlex Elder 
5255567d4d9SAlex Elder 			enabled = version < IPA_VERSION_4_5
5265567d4d9SAlex Elder 					? IPA_CS_OFFLOAD_UL
5275567d4d9SAlex Elder 					: IPA_CS_OFFLOAD_INLINE;
52884f9bd12SAlex Elder 		} else {
5295567d4d9SAlex Elder 			enabled = version < IPA_VERSION_4_5
5305567d4d9SAlex Elder 					? IPA_CS_OFFLOAD_DL
5315567d4d9SAlex Elder 					: IPA_CS_OFFLOAD_INLINE;
53284f9bd12SAlex Elder 		}
53384f9bd12SAlex Elder 	} else {
5345567d4d9SAlex Elder 		enabled = IPA_CS_OFFLOAD_NONE;
53584f9bd12SAlex Elder 	}
536*4468a344SAlex Elder 	val |= ipa_reg_encode(reg, CS_OFFLOAD_EN, enabled);
53784f9bd12SAlex Elder 	/* CS_GEN_QMB_MASTER_SEL is 0 */
53884f9bd12SAlex Elder 
5396a244b75SAlex Elder 	iowrite32(val, ipa->reg_virt + ipa_reg_n_offset(reg, endpoint_id));
54084f9bd12SAlex Elder }
54184f9bd12SAlex Elder 
542647a05f3SAlex Elder static void ipa_endpoint_init_nat(struct ipa_endpoint *endpoint)
543647a05f3SAlex Elder {
5446a244b75SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
5456bfb7538SAlex Elder 	struct ipa *ipa = endpoint->ipa;
5466a244b75SAlex Elder 	const struct ipa_reg *reg;
547647a05f3SAlex Elder 	u32 val;
548647a05f3SAlex Elder 
549647a05f3SAlex Elder 	if (!endpoint->toward_ipa)
550647a05f3SAlex Elder 		return;
551647a05f3SAlex Elder 
5526a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_NAT);
553*4468a344SAlex Elder 	val = ipa_reg_encode(reg, NAT_EN, IPA_NAT_BYPASS);
554647a05f3SAlex Elder 
5556a244b75SAlex Elder 	iowrite32(val, ipa->reg_virt + ipa_reg_n_offset(reg, endpoint_id));
556647a05f3SAlex Elder }
557647a05f3SAlex Elder 
5585567d4d9SAlex Elder static u32
5595567d4d9SAlex Elder ipa_qmap_header_size(enum ipa_version version, struct ipa_endpoint *endpoint)
5605567d4d9SAlex Elder {
5615567d4d9SAlex Elder 	u32 header_size = sizeof(struct rmnet_map_header);
5625567d4d9SAlex Elder 
5635567d4d9SAlex Elder 	/* Without checksum offload, we just have the MAP header */
564660e52d6SAlex Elder 	if (!endpoint->config.checksum)
5655567d4d9SAlex Elder 		return header_size;
5665567d4d9SAlex Elder 
5675567d4d9SAlex Elder 	if (version < IPA_VERSION_4_5) {
5685567d4d9SAlex Elder 		/* Checksum header inserted for AP TX endpoints only */
5695567d4d9SAlex Elder 		if (endpoint->toward_ipa)
5705567d4d9SAlex Elder 			header_size += sizeof(struct rmnet_map_ul_csum_header);
5715567d4d9SAlex Elder 	} else {
5725567d4d9SAlex Elder 		/* Checksum header is used in both directions */
5735567d4d9SAlex Elder 		header_size += sizeof(struct rmnet_map_v5_csum_header);
5745567d4d9SAlex Elder 	}
5755567d4d9SAlex Elder 
5765567d4d9SAlex Elder 	return header_size;
5775567d4d9SAlex Elder }
5785567d4d9SAlex Elder 
579*4468a344SAlex Elder /* Encoded value for ENDP_INIT_HDR register HDR_LEN* field(s) */
580*4468a344SAlex Elder static u32 ipa_header_size_encode(enum ipa_version version,
581*4468a344SAlex Elder 				  const struct ipa_reg *reg, u32 header_size)
582*4468a344SAlex Elder {
583*4468a344SAlex Elder 	u32 field_max = ipa_reg_field_max(reg, HDR_LEN);
584*4468a344SAlex Elder 	u32 val;
585*4468a344SAlex Elder 
586*4468a344SAlex Elder 	/* We know field_max can be used as a mask (2^n - 1) */
587*4468a344SAlex Elder 	val = ipa_reg_encode(reg, HDR_LEN, header_size & field_max);
588*4468a344SAlex Elder 	if (version < IPA_VERSION_4_5) {
589*4468a344SAlex Elder 		WARN_ON(header_size > field_max);
590*4468a344SAlex Elder 		return val;
591*4468a344SAlex Elder 	}
592*4468a344SAlex Elder 
593*4468a344SAlex Elder 	/* IPA v4.5 adds a few more most-significant bits */
594*4468a344SAlex Elder 	header_size >>= hweight32(field_max);
595*4468a344SAlex Elder 	WARN_ON(header_size > ipa_reg_field_max(reg, HDR_LEN_MSB));
596*4468a344SAlex Elder 	val |= ipa_reg_encode(reg, HDR_LEN_MSB, header_size);
597*4468a344SAlex Elder 
598*4468a344SAlex Elder 	return val;
599*4468a344SAlex Elder }
600*4468a344SAlex Elder 
601*4468a344SAlex Elder /* Encoded value for ENDP_INIT_HDR register OFST_METADATA* field(s) */
602*4468a344SAlex Elder static u32 ipa_metadata_offset_encode(enum ipa_version version,
603*4468a344SAlex Elder 				      const struct ipa_reg *reg, u32 offset)
604*4468a344SAlex Elder {
605*4468a344SAlex Elder 	u32 field_max = ipa_reg_field_max(reg, HDR_OFST_METADATA);
606*4468a344SAlex Elder 	u32 val;
607*4468a344SAlex Elder 
608*4468a344SAlex Elder 	/* We know field_max can be used as a mask (2^n - 1) */
609*4468a344SAlex Elder 	val = ipa_reg_encode(reg, HDR_OFST_METADATA, offset);
610*4468a344SAlex Elder 	if (version < IPA_VERSION_4_5) {
611*4468a344SAlex Elder 		WARN_ON(offset > field_max);
612*4468a344SAlex Elder 		return val;
613*4468a344SAlex Elder 	}
614*4468a344SAlex Elder 
615*4468a344SAlex Elder 	/* IPA v4.5 adds a few more most-significant bits */
616*4468a344SAlex Elder 	offset >>= hweight32(field_max);
617*4468a344SAlex Elder 	WARN_ON(offset > ipa_reg_field_max(reg, HDR_OFST_METADATA_MSB));
618*4468a344SAlex Elder 	val |= ipa_reg_encode(reg, HDR_OFST_METADATA_MSB, offset);
619*4468a344SAlex Elder 
620*4468a344SAlex Elder 	return val;
621*4468a344SAlex Elder }
622*4468a344SAlex Elder 
6238730f45dSAlex Elder /**
624e3eea08eSAlex Elder  * ipa_endpoint_init_hdr() - Initialize HDR endpoint configuration register
625e3eea08eSAlex Elder  * @endpoint:	Endpoint pointer
626e3eea08eSAlex Elder  *
6278730f45dSAlex Elder  * We program QMAP endpoints so each packet received is preceded by a QMAP
6288730f45dSAlex Elder  * header structure.  The QMAP header contains a 1-byte mux_id and 2-byte
6298730f45dSAlex Elder  * packet size field, and we have the IPA hardware populate both for each
6308730f45dSAlex Elder  * received packet.  The header is configured (in the HDR_EXT register)
6318730f45dSAlex Elder  * to use big endian format.
6328730f45dSAlex Elder  *
6338730f45dSAlex Elder  * The packet size is written into the QMAP header's pkt_len field.  That
6348730f45dSAlex Elder  * location is defined here using the HDR_OFST_PKT_SIZE field.
6358730f45dSAlex Elder  *
6368730f45dSAlex Elder  * The mux_id comes from a 4-byte metadata value supplied with each packet
6378730f45dSAlex Elder  * by the modem.  It is *not* a QMAP header, but it does contain the mux_id
6388730f45dSAlex Elder  * value that we want, in its low-order byte.  A bitmask defined in the
6398730f45dSAlex Elder  * endpoint's METADATA_MASK register defines which byte within the modem
6408730f45dSAlex Elder  * metadata contains the mux_id.  And the OFST_METADATA field programmed
6418730f45dSAlex Elder  * here indicates where the extracted byte should be placed within the QMAP
6428730f45dSAlex Elder  * header.
6438730f45dSAlex Elder  */
64484f9bd12SAlex Elder static void ipa_endpoint_init_hdr(struct ipa_endpoint *endpoint)
64584f9bd12SAlex Elder {
6466a244b75SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
6471af15c2aSAlex Elder 	struct ipa *ipa = endpoint->ipa;
6486a244b75SAlex Elder 	const struct ipa_reg *reg;
64984f9bd12SAlex Elder 	u32 val = 0;
6506bfb7538SAlex Elder 
6516a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_HDR);
652660e52d6SAlex Elder 	if (endpoint->config.qmap) {
6531af15c2aSAlex Elder 		enum ipa_version version = ipa->version;
6545567d4d9SAlex Elder 		size_t header_size;
65584f9bd12SAlex Elder 
6565567d4d9SAlex Elder 		header_size = ipa_qmap_header_size(version, endpoint);
657*4468a344SAlex Elder 		val = ipa_header_size_encode(version, reg, header_size);
65884f9bd12SAlex Elder 
659f330fda3SAlex Elder 		/* Define how to fill fields in a received QMAP header */
6608730f45dSAlex Elder 		if (!endpoint->toward_ipa) {
6619eefd2fbSAlex Elder 			u32 off;     /* Field offset within header */
6628730f45dSAlex Elder 
6638730f45dSAlex Elder 			/* Where IPA will write the metadata value */
6649eefd2fbSAlex Elder 			off = offsetof(struct rmnet_map_header, mux_id);
665*4468a344SAlex Elder 			val |= ipa_metadata_offset_encode(version, reg, off);
6668730f45dSAlex Elder 
6678730f45dSAlex Elder 			/* Where IPA will write the length */
6689eefd2fbSAlex Elder 			off = offsetof(struct rmnet_map_header, pkt_len);
6691af15c2aSAlex Elder 			/* Upper bits are stored in HDR_EXT with IPA v4.5 */
670d7f3087bSAlex Elder 			if (version >= IPA_VERSION_4_5)
671*4468a344SAlex Elder 				off &= ipa_reg_field_max(reg, HDR_OFST_PKT_SIZE);
6721af15c2aSAlex Elder 
673*4468a344SAlex Elder 			val |= ipa_reg_bit(reg, HDR_OFST_PKT_SIZE_VALID);
674*4468a344SAlex Elder 			val |= ipa_reg_encode(reg, HDR_OFST_PKT_SIZE, off);
67584f9bd12SAlex Elder 		}
6768730f45dSAlex Elder 		/* For QMAP TX, metadata offset is 0 (modem assumes this) */
677*4468a344SAlex Elder 		val |= ipa_reg_bit(reg, HDR_OFST_METADATA_VALID);
6788730f45dSAlex Elder 
6798730f45dSAlex Elder 		/* HDR_ADDITIONAL_CONST_LEN is 0; (RX only) */
68084f9bd12SAlex Elder 		/* HDR_A5_MUX is 0 */
68184f9bd12SAlex Elder 		/* HDR_LEN_INC_DEAGG_HDR is 0 */
6828bfc4e21SAlex Elder 		/* HDR_METADATA_REG_VALID is 0 (TX only, version < v4.5) */
68384f9bd12SAlex Elder 	}
68484f9bd12SAlex Elder 
6856a244b75SAlex Elder 	iowrite32(val, ipa->reg_virt + ipa_reg_n_offset(reg, endpoint_id));
68684f9bd12SAlex Elder }
68784f9bd12SAlex Elder 
68884f9bd12SAlex Elder static void ipa_endpoint_init_hdr_ext(struct ipa_endpoint *endpoint)
68984f9bd12SAlex Elder {
690660e52d6SAlex Elder 	u32 pad_align = endpoint->config.rx.pad_align;
6916a244b75SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
6921af15c2aSAlex Elder 	struct ipa *ipa = endpoint->ipa;
6936a244b75SAlex Elder 	const struct ipa_reg *reg;
69484f9bd12SAlex Elder 	u32 val = 0;
6956bfb7538SAlex Elder 
6966a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_HDR_EXT);
697660e52d6SAlex Elder 	if (endpoint->config.qmap) {
698332ef7c8SAlex Elder 		/* We have a header, so we must specify its endianness */
699*4468a344SAlex Elder 		val |= ipa_reg_bit(reg, HDR_ENDIANNESS);	/* big endian */
700f330fda3SAlex Elder 
701332ef7c8SAlex Elder 		/* A QMAP header contains a 6 bit pad field at offset 0.
702332ef7c8SAlex Elder 		 * The RMNet driver assumes this field is meaningful in
703332ef7c8SAlex Elder 		 * packets it receives, and assumes the header's payload
704332ef7c8SAlex Elder 		 * length includes that padding.  The RMNet driver does
705332ef7c8SAlex Elder 		 * *not* pad packets it sends, however, so the pad field
706332ef7c8SAlex Elder 		 * (although 0) should be ignored.
707f330fda3SAlex Elder 		 */
708332ef7c8SAlex Elder 		if (!endpoint->toward_ipa) {
709*4468a344SAlex Elder 			val |= ipa_reg_bit(reg, HDR_TOTAL_LEN_OR_PAD_VALID);
71084f9bd12SAlex Elder 			/* HDR_TOTAL_LEN_OR_PAD is 0 (pad, not total_len) */
711*4468a344SAlex Elder 			val |= ipa_reg_bit(reg, HDR_PAYLOAD_LEN_INC_PADDING);
71284f9bd12SAlex Elder 			/* HDR_TOTAL_LEN_OR_PAD_OFFSET is 0 */
713f330fda3SAlex Elder 		}
714332ef7c8SAlex Elder 	}
715f330fda3SAlex Elder 
716f330fda3SAlex Elder 	/* HDR_PAYLOAD_LEN_INC_PADDING is 0 */
71784f9bd12SAlex Elder 	if (!endpoint->toward_ipa)
718*4468a344SAlex Elder 		val |= ipa_reg_encode(reg, HDR_PAD_TO_ALIGNMENT, pad_align);
71984f9bd12SAlex Elder 
7201af15c2aSAlex Elder 	/* IPA v4.5 adds some most-significant bits to a few fields,
7211af15c2aSAlex Elder 	 * two of which are defined in the HDR (not HDR_EXT) register.
7221af15c2aSAlex Elder 	 */
723d7f3087bSAlex Elder 	if (ipa->version >= IPA_VERSION_4_5) {
7241af15c2aSAlex Elder 		/* HDR_TOTAL_LEN_OR_PAD_OFFSET is 0, so MSB is 0 */
725660e52d6SAlex Elder 		if (endpoint->config.qmap && !endpoint->toward_ipa) {
726*4468a344SAlex Elder 			u32 mask = ipa_reg_field_max(reg, HDR_OFST_PKT_SIZE);
7276bfb7538SAlex Elder 			u32 off;     /* Field offset within header */
72884f9bd12SAlex Elder 
7299eefd2fbSAlex Elder 			off = offsetof(struct rmnet_map_header, pkt_len);
730*4468a344SAlex Elder 			/* Low bits are in the ENDP_INIT_HDR register */
731*4468a344SAlex Elder 			off >>= hweight32(mask);
732*4468a344SAlex Elder 			val |= ipa_reg_encode(reg, HDR_OFST_PKT_SIZE_MSB, off);
7331af15c2aSAlex Elder 			/* HDR_ADDITIONAL_CONST_LEN is 0 so MSB is 0 */
7341af15c2aSAlex Elder 		}
7351af15c2aSAlex Elder 	}
7366bfb7538SAlex Elder 
7376a244b75SAlex Elder 	iowrite32(val, ipa->reg_virt + ipa_reg_n_offset(reg, endpoint_id));
7381af15c2aSAlex Elder }
73984f9bd12SAlex Elder 
74084f9bd12SAlex Elder static void ipa_endpoint_init_hdr_metadata_mask(struct ipa_endpoint *endpoint)
74184f9bd12SAlex Elder {
74284f9bd12SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
7436bfb7538SAlex Elder 	struct ipa *ipa = endpoint->ipa;
7446a244b75SAlex Elder 	const struct ipa_reg *reg;
74584f9bd12SAlex Elder 	u32 val = 0;
74684f9bd12SAlex Elder 	u32 offset;
74784f9bd12SAlex Elder 
748fb57c3eaSAlex Elder 	if (endpoint->toward_ipa)
749fb57c3eaSAlex Elder 		return;		/* Register not valid for TX endpoints */
750fb57c3eaSAlex Elder 
7516a244b75SAlex Elder 	reg = ipa_reg(ipa,  ENDP_INIT_HDR_METADATA_MASK);
7526a244b75SAlex Elder 	offset = ipa_reg_n_offset(reg, endpoint_id);
75384f9bd12SAlex Elder 
7548730f45dSAlex Elder 	/* Note that HDR_ENDIANNESS indicates big endian header fields */
755660e52d6SAlex Elder 	if (endpoint->config.qmap)
756088f8a23SAlex Elder 		val = (__force u32)cpu_to_be32(IPA_ENDPOINT_QMAP_METADATA_MASK);
75784f9bd12SAlex Elder 
7586bfb7538SAlex Elder 	iowrite32(val, ipa->reg_virt + offset);
75984f9bd12SAlex Elder }
76084f9bd12SAlex Elder 
76184f9bd12SAlex Elder static void ipa_endpoint_init_mode(struct ipa_endpoint *endpoint)
76284f9bd12SAlex Elder {
7636bfb7538SAlex Elder 	struct ipa *ipa = endpoint->ipa;
7646a244b75SAlex Elder 	const struct ipa_reg *reg;
7656bfb7538SAlex Elder 	u32 offset;
76684f9bd12SAlex Elder 	u32 val;
76784f9bd12SAlex Elder 
768fb57c3eaSAlex Elder 	if (!endpoint->toward_ipa)
769fb57c3eaSAlex Elder 		return;		/* Register not valid for RX endpoints */
770fb57c3eaSAlex Elder 
7716a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_MODE);
7726a244b75SAlex Elder 	offset = ipa_reg_n_offset(reg, endpoint->endpoint_id);
773660e52d6SAlex Elder 	if (endpoint->config.dma_mode) {
774660e52d6SAlex Elder 		enum ipa_endpoint_name name = endpoint->config.dma_endpoint;
77584f9bd12SAlex Elder 		u32 dma_endpoint_id;
77684f9bd12SAlex Elder 
7776bfb7538SAlex Elder 		dma_endpoint_id = ipa->name_map[name]->endpoint_id;
77884f9bd12SAlex Elder 
77984f9bd12SAlex Elder 		val = u32_encode_bits(IPA_DMA, MODE_FMASK);
78084f9bd12SAlex Elder 		val |= u32_encode_bits(dma_endpoint_id, DEST_PIPE_INDEX_FMASK);
78184f9bd12SAlex Elder 	} else {
78284f9bd12SAlex Elder 		val = u32_encode_bits(IPA_BASIC, MODE_FMASK);
78384f9bd12SAlex Elder 	}
78400b9102aSAlex Elder 	/* All other bits unspecified (and 0) */
78584f9bd12SAlex Elder 
7866bfb7538SAlex Elder 	iowrite32(val, ipa->reg_virt + offset);
78784f9bd12SAlex Elder }
78884f9bd12SAlex Elder 
7896bf754c7SAlex Elder /* Encoded values for AGGR endpoint register fields */
7906bf754c7SAlex Elder static u32 aggr_byte_limit_encoded(enum ipa_version version, u32 limit)
7916bf754c7SAlex Elder {
7926bf754c7SAlex Elder 	if (version < IPA_VERSION_4_5)
7936bf754c7SAlex Elder 		return u32_encode_bits(limit, aggr_byte_limit_fmask(true));
7946bf754c7SAlex Elder 
7956bf754c7SAlex Elder 	return u32_encode_bits(limit, aggr_byte_limit_fmask(false));
7966bf754c7SAlex Elder }
7976bf754c7SAlex Elder 
7988be440e1SAlex Elder /* For IPA v4.5+, times are expressed using Qtime.  The AP uses one of two
7998be440e1SAlex Elder  * pulse generators (0 and 1) to measure elapsed time.  In ipa_qtime_config()
8008be440e1SAlex Elder  * they're configured to have granularity 100 usec and 1 msec, respectively.
8018be440e1SAlex Elder  *
8028be440e1SAlex Elder  * The return value is the positive or negative Qtime value to use to
8038be440e1SAlex Elder  * express the (microsecond) time provided.  A positive return value
8048be440e1SAlex Elder  * means pulse generator 0 can be used; otherwise use pulse generator 1.
8058be440e1SAlex Elder  */
8068be440e1SAlex Elder static int ipa_qtime_val(u32 microseconds, u32 max)
8078be440e1SAlex Elder {
8088be440e1SAlex Elder 	u32 val;
8098be440e1SAlex Elder 
8108be440e1SAlex Elder 	/* Use 100 microsecond granularity if possible */
8118be440e1SAlex Elder 	val = DIV_ROUND_CLOSEST(microseconds, 100);
8128be440e1SAlex Elder 	if (val <= max)
8138be440e1SAlex Elder 		return (int)val;
8148be440e1SAlex Elder 
8158be440e1SAlex Elder 	/* Have to use pulse generator 1 (millisecond granularity) */
8168be440e1SAlex Elder 	val = DIV_ROUND_CLOSEST(microseconds, 1000);
8178be440e1SAlex Elder 	WARN_ON(val > max);
8188be440e1SAlex Elder 
8198be440e1SAlex Elder 	return (int)-val;
8208be440e1SAlex Elder }
8218be440e1SAlex Elder 
82219547041SAlex Elder /* Encode the aggregation timer limit (microseconds) based on IPA version */
8238be440e1SAlex Elder static u32 aggr_time_limit_encode(enum ipa_version version, u32 microseconds)
8246bf754c7SAlex Elder {
82519547041SAlex Elder 	u32 fmask;
82619547041SAlex Elder 	u32 val;
82748395fa8SAlex Elder 
82848395fa8SAlex Elder 	if (!microseconds)
82948395fa8SAlex Elder 		return 0;	/* Nothing to compute if time limit is 0 */
83048395fa8SAlex Elder 
83148395fa8SAlex Elder 	if (version >= IPA_VERSION_4_5) {
83248395fa8SAlex Elder 		u32 gran_sel;
8338be440e1SAlex Elder 		int ret;
8346bf754c7SAlex Elder 
8358be440e1SAlex Elder 		/* Compute the Qtime limit value to use */
83619547041SAlex Elder 		fmask = aggr_time_limit_fmask(false);
8378be440e1SAlex Elder 		ret = ipa_qtime_val(microseconds, field_max(fmask));
8388be440e1SAlex Elder 		if (ret < 0) {
8398be440e1SAlex Elder 			val = -ret;
84019547041SAlex Elder 			gran_sel = AGGR_GRAN_SEL_FMASK;
84119547041SAlex Elder 		} else {
8428be440e1SAlex Elder 			val = ret;
84319547041SAlex Elder 			gran_sel = 0;
84419547041SAlex Elder 		}
84519547041SAlex Elder 
84619547041SAlex Elder 		return gran_sel | u32_encode_bits(val, fmask);
8476bf754c7SAlex Elder 	}
8486bf754c7SAlex Elder 
84948395fa8SAlex Elder 	/* We set aggregation granularity in ipa_hardware_config() */
85048395fa8SAlex Elder 	fmask = aggr_time_limit_fmask(true);
85148395fa8SAlex Elder 	val = DIV_ROUND_CLOSEST(microseconds, IPA_AGGR_GRANULARITY);
85248395fa8SAlex Elder 	WARN(val > field_max(fmask),
85348395fa8SAlex Elder 	     "aggr_time_limit too large (%u > %u usec)\n",
85448395fa8SAlex Elder 	     val, field_max(fmask) * IPA_AGGR_GRANULARITY);
85548395fa8SAlex Elder 
85648395fa8SAlex Elder 	return u32_encode_bits(val, fmask);
85748395fa8SAlex Elder }
85848395fa8SAlex Elder 
8596bf754c7SAlex Elder static u32 aggr_sw_eof_active_encoded(enum ipa_version version, bool enabled)
8606bf754c7SAlex Elder {
8616bf754c7SAlex Elder 	u32 val = enabled ? 1 : 0;
8626bf754c7SAlex Elder 
8636bf754c7SAlex Elder 	if (version < IPA_VERSION_4_5)
8646bf754c7SAlex Elder 		return u32_encode_bits(val, aggr_sw_eof_active_fmask(true));
8656bf754c7SAlex Elder 
8666bf754c7SAlex Elder 	return u32_encode_bits(val, aggr_sw_eof_active_fmask(false));
8676bf754c7SAlex Elder }
8686bf754c7SAlex Elder 
86984f9bd12SAlex Elder static void ipa_endpoint_init_aggr(struct ipa_endpoint *endpoint)
87084f9bd12SAlex Elder {
8716a244b75SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
8726bfb7538SAlex Elder 	struct ipa *ipa = endpoint->ipa;
8736a244b75SAlex Elder 	const struct ipa_reg *reg;
87484f9bd12SAlex Elder 	u32 val = 0;
8756bfb7538SAlex Elder 
8766a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_AGGR);
877660e52d6SAlex Elder 	if (endpoint->config.aggregation) {
87884f9bd12SAlex Elder 		if (!endpoint->toward_ipa) {
879cf4e73a1SAlex Elder 			const struct ipa_endpoint_rx *rx_config;
8806bfb7538SAlex Elder 			enum ipa_version version = ipa->version;
881c5794097SAlex Elder 			u32 buffer_size;
8826bf754c7SAlex Elder 			bool close_eof;
88384f9bd12SAlex Elder 			u32 limit;
88484f9bd12SAlex Elder 
885660e52d6SAlex Elder 			rx_config = &endpoint->config.rx;
88684f9bd12SAlex Elder 			val |= u32_encode_bits(IPA_ENABLE_AGGR, AGGR_EN_FMASK);
88784f9bd12SAlex Elder 			val |= u32_encode_bits(IPA_GENERIC, AGGR_TYPE_FMASK);
8889e88cb5fSAlex Elder 
889cf4e73a1SAlex Elder 			buffer_size = rx_config->buffer_size;
8903cebb7c2SAlex Elder 			limit = ipa_aggr_size_kb(buffer_size - NET_SKB_PAD,
8913cebb7c2SAlex Elder 						 rx_config->aggr_hard_limit);
8926bf754c7SAlex Elder 			val |= aggr_byte_limit_encoded(version, limit);
8931d86652bSAlex Elder 
894beb90cbaSAlex Elder 			limit = rx_config->aggr_time_limit;
8958be440e1SAlex Elder 			val |= aggr_time_limit_encode(version, limit);
8961d86652bSAlex Elder 
8979e88cb5fSAlex Elder 			/* AGGR_PKT_LIMIT is 0 (unlimited) */
8989e88cb5fSAlex Elder 
899cf4e73a1SAlex Elder 			close_eof = rx_config->aggr_close_eof;
9006bf754c7SAlex Elder 			val |= aggr_sw_eof_active_encoded(version, close_eof);
90184f9bd12SAlex Elder 		} else {
90284f9bd12SAlex Elder 			val |= u32_encode_bits(IPA_ENABLE_DEAGGR,
90384f9bd12SAlex Elder 					       AGGR_EN_FMASK);
90484f9bd12SAlex Elder 			val |= u32_encode_bits(IPA_QCMAP, AGGR_TYPE_FMASK);
90584f9bd12SAlex Elder 			/* other fields ignored */
90684f9bd12SAlex Elder 		}
90784f9bd12SAlex Elder 		/* AGGR_FORCE_CLOSE is 0 */
9088bfc4e21SAlex Elder 		/* AGGR_GRAN_SEL is 0 for IPA v4.5 */
90984f9bd12SAlex Elder 	} else {
91084f9bd12SAlex Elder 		val |= u32_encode_bits(IPA_BYPASS_AGGR, AGGR_EN_FMASK);
91184f9bd12SAlex Elder 		/* other fields ignored */
91284f9bd12SAlex Elder 	}
91384f9bd12SAlex Elder 
9146a244b75SAlex Elder 	iowrite32(val, ipa->reg_virt + ipa_reg_n_offset(reg, endpoint_id));
91584f9bd12SAlex Elder }
91684f9bd12SAlex Elder 
91763e5afc8SAlex Elder /* The head-of-line blocking timer is defined as a tick count.  For
91863e5afc8SAlex Elder  * IPA version 4.5 the tick count is based on the Qtimer, which is
91963e5afc8SAlex Elder  * derived from the 19.2 MHz SoC XO clock.  For older IPA versions
92063e5afc8SAlex Elder  * each tick represents 128 cycles of the IPA core clock.
92163e5afc8SAlex Elder  *
9228be440e1SAlex Elder  * Return the encoded value representing the timeout period provided
9238be440e1SAlex Elder  * that should be written to the ENDP_INIT_HOL_BLOCK_TIMER register.
92463e5afc8SAlex Elder  */
9258be440e1SAlex Elder static u32 hol_block_timer_encode(struct ipa *ipa, u32 microseconds)
92684f9bd12SAlex Elder {
927f13a8c31SAlex Elder 	u32 width;
92884f9bd12SAlex Elder 	u32 scale;
929f13a8c31SAlex Elder 	u64 ticks;
930f13a8c31SAlex Elder 	u64 rate;
931f13a8c31SAlex Elder 	u32 high;
93284f9bd12SAlex Elder 	u32 val;
93384f9bd12SAlex Elder 
93484f9bd12SAlex Elder 	if (!microseconds)
935f13a8c31SAlex Elder 		return 0;	/* Nothing to compute if timer period is 0 */
93684f9bd12SAlex Elder 
93748395fa8SAlex Elder 	if (ipa->version >= IPA_VERSION_4_5) {
93848395fa8SAlex Elder 		u32 gran_sel;
93948395fa8SAlex Elder 		int ret;
94048395fa8SAlex Elder 
94148395fa8SAlex Elder 		/* Compute the Qtime limit value to use */
94248395fa8SAlex Elder 		ret = ipa_qtime_val(microseconds, field_max(TIME_LIMIT_FMASK));
94348395fa8SAlex Elder 		if (ret < 0) {
94448395fa8SAlex Elder 			val = -ret;
94548395fa8SAlex Elder 			gran_sel = GRAN_SEL_FMASK;
94648395fa8SAlex Elder 		} else {
94748395fa8SAlex Elder 			val = ret;
94848395fa8SAlex Elder 			gran_sel = 0;
94948395fa8SAlex Elder 		}
95048395fa8SAlex Elder 
95148395fa8SAlex Elder 		return gran_sel | u32_encode_bits(val, TIME_LIMIT_FMASK);
95248395fa8SAlex Elder 	}
95363e5afc8SAlex Elder 
954f13a8c31SAlex Elder 	/* Use 64 bit arithmetic to avoid overflow... */
9557aa0e8b8SAlex Elder 	rate = ipa_core_clock_rate(ipa);
956f13a8c31SAlex Elder 	ticks = DIV_ROUND_CLOSEST(microseconds * rate, 128 * USEC_PER_SEC);
957f13a8c31SAlex Elder 	/* ...but we still need to fit into a 32-bit register */
958f13a8c31SAlex Elder 	WARN_ON(ticks > U32_MAX);
95984f9bd12SAlex Elder 
9606833a096SAlex Elder 	/* IPA v3.5.1 through v4.1 just record the tick count */
9616833a096SAlex Elder 	if (ipa->version < IPA_VERSION_4_2)
962f13a8c31SAlex Elder 		return (u32)ticks;
96384f9bd12SAlex Elder 
964f13a8c31SAlex Elder 	/* For IPA v4.2, the tick count is represented by base and
965f13a8c31SAlex Elder 	 * scale fields within the 32-bit timer register, where:
966f13a8c31SAlex Elder 	 *     ticks = base << scale;
967f13a8c31SAlex Elder 	 * The best precision is achieved when the base value is as
968f13a8c31SAlex Elder 	 * large as possible.  Find the highest set bit in the tick
969f13a8c31SAlex Elder 	 * count, and extract the number of bits in the base field
970497abc87SPeng Li 	 * such that high bit is included.
971f13a8c31SAlex Elder 	 */
972f13a8c31SAlex Elder 	high = fls(ticks);		/* 1..32 */
973f13a8c31SAlex Elder 	width = HWEIGHT32(BASE_VALUE_FMASK);
974f13a8c31SAlex Elder 	scale = high > width ? high - width : 0;
975f13a8c31SAlex Elder 	if (scale) {
976f13a8c31SAlex Elder 		/* If we're scaling, round up to get a closer result */
977f13a8c31SAlex Elder 		ticks += 1 << (scale - 1);
978f13a8c31SAlex Elder 		/* High bit was set, so rounding might have affected it */
979f13a8c31SAlex Elder 		if (fls(ticks) != high)
980f13a8c31SAlex Elder 			scale++;
981f13a8c31SAlex Elder 	}
98284f9bd12SAlex Elder 
98384f9bd12SAlex Elder 	val = u32_encode_bits(scale, SCALE_FMASK);
984f13a8c31SAlex Elder 	val |= u32_encode_bits(ticks >> scale, BASE_VALUE_FMASK);
98584f9bd12SAlex Elder 
98684f9bd12SAlex Elder 	return val;
98784f9bd12SAlex Elder }
98884f9bd12SAlex Elder 
989f13a8c31SAlex Elder /* If microseconds is 0, timeout is immediate */
990f13a8c31SAlex Elder static void ipa_endpoint_init_hol_block_timer(struct ipa_endpoint *endpoint,
99184f9bd12SAlex Elder 					      u32 microseconds)
99284f9bd12SAlex Elder {
99384f9bd12SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
99484f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
9956a244b75SAlex Elder 	const struct ipa_reg *reg;
99684f9bd12SAlex Elder 	u32 val;
99784f9bd12SAlex Elder 
998816316caSAlex Elder 	/* This should only be changed when HOL_BLOCK_EN is disabled */
9996a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_HOL_BLOCK_TIMER);
10008be440e1SAlex Elder 	val = hol_block_timer_encode(ipa, microseconds);
10016bfb7538SAlex Elder 
10026a244b75SAlex Elder 	iowrite32(val, ipa->reg_virt + ipa_reg_n_offset(reg, endpoint_id));
100384f9bd12SAlex Elder }
100484f9bd12SAlex Elder 
100584f9bd12SAlex Elder static void
1006e6aab6b9SAlex Elder ipa_endpoint_init_hol_block_en(struct ipa_endpoint *endpoint, bool enable)
100784f9bd12SAlex Elder {
100884f9bd12SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
10096bfb7538SAlex Elder 	struct ipa *ipa = endpoint->ipa;
10106a244b75SAlex Elder 	const struct ipa_reg *reg;
101184f9bd12SAlex Elder 	u32 offset;
101284f9bd12SAlex Elder 	u32 val;
101384f9bd12SAlex Elder 
10146a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_HOL_BLOCK_EN);
10156a244b75SAlex Elder 	offset = ipa_reg_n_offset(reg, endpoint_id);
1016547c8788SAlex Elder 	val = enable ? HOL_BLOCK_EN_FMASK : 0;
10176bfb7538SAlex Elder 
10186bfb7538SAlex Elder 	iowrite32(val, ipa->reg_virt + offset);
10196bfb7538SAlex Elder 
10206e228d8cSAlex Elder 	/* When enabling, the register must be written twice for IPA v4.5+ */
10216bfb7538SAlex Elder 	if (enable && ipa->version >= IPA_VERSION_4_5)
10226bfb7538SAlex Elder 		iowrite32(val, ipa->reg_virt + offset);
102384f9bd12SAlex Elder }
102484f9bd12SAlex Elder 
1025e6aab6b9SAlex Elder /* Assumes HOL_BLOCK is in disabled state */
1026e6aab6b9SAlex Elder static void ipa_endpoint_init_hol_block_enable(struct ipa_endpoint *endpoint,
1027e6aab6b9SAlex Elder 					       u32 microseconds)
1028e6aab6b9SAlex Elder {
1029e6aab6b9SAlex Elder 	ipa_endpoint_init_hol_block_timer(endpoint, microseconds);
1030e6aab6b9SAlex Elder 	ipa_endpoint_init_hol_block_en(endpoint, true);
1031e6aab6b9SAlex Elder }
1032e6aab6b9SAlex Elder 
1033e6aab6b9SAlex Elder static void ipa_endpoint_init_hol_block_disable(struct ipa_endpoint *endpoint)
1034e6aab6b9SAlex Elder {
1035e6aab6b9SAlex Elder 	ipa_endpoint_init_hol_block_en(endpoint, false);
1036e6aab6b9SAlex Elder }
1037e6aab6b9SAlex Elder 
103884f9bd12SAlex Elder void ipa_endpoint_modem_hol_block_clear_all(struct ipa *ipa)
103984f9bd12SAlex Elder {
104084f9bd12SAlex Elder 	u32 i;
104184f9bd12SAlex Elder 
104284f9bd12SAlex Elder 	for (i = 0; i < IPA_ENDPOINT_MAX; i++) {
104384f9bd12SAlex Elder 		struct ipa_endpoint *endpoint = &ipa->endpoint[i];
104484f9bd12SAlex Elder 
1045f8d34dfdSAlex Elder 		if (endpoint->toward_ipa || endpoint->ee_id != GSI_EE_MODEM)
104684f9bd12SAlex Elder 			continue;
104784f9bd12SAlex Elder 
1048e6aab6b9SAlex Elder 		ipa_endpoint_init_hol_block_disable(endpoint);
1049e6aab6b9SAlex Elder 		ipa_endpoint_init_hol_block_enable(endpoint, 0);
105084f9bd12SAlex Elder 	}
105184f9bd12SAlex Elder }
105284f9bd12SAlex Elder 
105384f9bd12SAlex Elder static void ipa_endpoint_init_deaggr(struct ipa_endpoint *endpoint)
105484f9bd12SAlex Elder {
10556a244b75SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
10566bfb7538SAlex Elder 	struct ipa *ipa = endpoint->ipa;
10576a244b75SAlex Elder 	const struct ipa_reg *reg;
105884f9bd12SAlex Elder 	u32 val = 0;
105984f9bd12SAlex Elder 
1060fb57c3eaSAlex Elder 	if (!endpoint->toward_ipa)
1061fb57c3eaSAlex Elder 		return;		/* Register not valid for RX endpoints */
1062fb57c3eaSAlex Elder 
10636a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_DEAGGR);
106484f9bd12SAlex Elder 	/* DEAGGR_HDR_LEN is 0 */
106584f9bd12SAlex Elder 	/* PACKET_OFFSET_VALID is 0 */
106684f9bd12SAlex Elder 	/* PACKET_OFFSET_LOCATION is ignored (not valid) */
106784f9bd12SAlex Elder 	/* MAX_PACKET_LEN is 0 (not enforced) */
106884f9bd12SAlex Elder 
10696a244b75SAlex Elder 	iowrite32(val, ipa->reg_virt + ipa_reg_n_offset(reg, endpoint_id));
107084f9bd12SAlex Elder }
107184f9bd12SAlex Elder 
10722d265342SAlex Elder static void ipa_endpoint_init_rsrc_grp(struct ipa_endpoint *endpoint)
10732d265342SAlex Elder {
10746a244b75SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
10752d265342SAlex Elder 	struct ipa *ipa = endpoint->ipa;
10766a244b75SAlex Elder 	const struct ipa_reg *reg;
10772d265342SAlex Elder 	u32 val;
10782d265342SAlex Elder 
10796a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_RSRC_GRP);
1080660e52d6SAlex Elder 	val = rsrc_grp_encoded(ipa->version, endpoint->config.resource_group);
10816bfb7538SAlex Elder 
10826a244b75SAlex Elder 	iowrite32(val, ipa->reg_virt + ipa_reg_n_offset(reg, endpoint_id));
10832d265342SAlex Elder }
10842d265342SAlex Elder 
108584f9bd12SAlex Elder static void ipa_endpoint_init_seq(struct ipa_endpoint *endpoint)
108684f9bd12SAlex Elder {
10876a244b75SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
10886bfb7538SAlex Elder 	struct ipa *ipa = endpoint->ipa;
10896a244b75SAlex Elder 	const struct ipa_reg *reg;
109084f9bd12SAlex Elder 	u32 val = 0;
109184f9bd12SAlex Elder 
1092fb57c3eaSAlex Elder 	if (!endpoint->toward_ipa)
1093fb57c3eaSAlex Elder 		return;		/* Register not valid for RX endpoints */
1094fb57c3eaSAlex Elder 
10956a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_INIT_SEQ);
10966bfb7538SAlex Elder 
10978ee5df65SAlex Elder 	/* Low-order byte configures primary packet processing */
1098660e52d6SAlex Elder 	val |= u32_encode_bits(endpoint->config.tx.seq_type, SEQ_TYPE_FMASK);
10998ee5df65SAlex Elder 
1100a14d5937SAlex Elder 	/* Second byte (if supported) configures replicated packet processing */
11016bfb7538SAlex Elder 	if (ipa->version < IPA_VERSION_4_5)
1102660e52d6SAlex Elder 		val |= u32_encode_bits(endpoint->config.tx.seq_rep_type,
11031690d8a7SAlex Elder 				       SEQ_REP_TYPE_FMASK);
110484f9bd12SAlex Elder 
11056a244b75SAlex Elder 	iowrite32(val, ipa->reg_virt + ipa_reg_n_offset(reg, endpoint_id));
110684f9bd12SAlex Elder }
110784f9bd12SAlex Elder 
110884f9bd12SAlex Elder /**
110984f9bd12SAlex Elder  * ipa_endpoint_skb_tx() - Transmit a socket buffer
111084f9bd12SAlex Elder  * @endpoint:	Endpoint pointer
111184f9bd12SAlex Elder  * @skb:	Socket buffer to send
111284f9bd12SAlex Elder  *
111384f9bd12SAlex Elder  * Returns:	0 if successful, or a negative error code
111484f9bd12SAlex Elder  */
111584f9bd12SAlex Elder int ipa_endpoint_skb_tx(struct ipa_endpoint *endpoint, struct sk_buff *skb)
111684f9bd12SAlex Elder {
111784f9bd12SAlex Elder 	struct gsi_trans *trans;
111884f9bd12SAlex Elder 	u32 nr_frags;
111984f9bd12SAlex Elder 	int ret;
112084f9bd12SAlex Elder 
112184f9bd12SAlex Elder 	/* Make sure source endpoint's TLV FIFO has enough entries to
112284f9bd12SAlex Elder 	 * hold the linear portion of the skb and all its fragments.
112384f9bd12SAlex Elder 	 * If not, see if we can linearize it before giving up.
112484f9bd12SAlex Elder 	 */
112584f9bd12SAlex Elder 	nr_frags = skb_shinfo(skb)->nr_frags;
1126317595d2SAlex Elder 	if (nr_frags > endpoint->skb_frag_max) {
112784f9bd12SAlex Elder 		if (skb_linearize(skb))
112884f9bd12SAlex Elder 			return -E2BIG;
112984f9bd12SAlex Elder 		nr_frags = 0;
113084f9bd12SAlex Elder 	}
113184f9bd12SAlex Elder 
113284f9bd12SAlex Elder 	trans = ipa_endpoint_trans_alloc(endpoint, 1 + nr_frags);
113384f9bd12SAlex Elder 	if (!trans)
113484f9bd12SAlex Elder 		return -EBUSY;
113584f9bd12SAlex Elder 
113684f9bd12SAlex Elder 	ret = gsi_trans_skb_add(trans, skb);
113784f9bd12SAlex Elder 	if (ret)
113884f9bd12SAlex Elder 		goto err_trans_free;
113984f9bd12SAlex Elder 	trans->data = skb;	/* transaction owns skb now */
114084f9bd12SAlex Elder 
114184f9bd12SAlex Elder 	gsi_trans_commit(trans, !netdev_xmit_more());
114284f9bd12SAlex Elder 
114384f9bd12SAlex Elder 	return 0;
114484f9bd12SAlex Elder 
114584f9bd12SAlex Elder err_trans_free:
114684f9bd12SAlex Elder 	gsi_trans_free(trans);
114784f9bd12SAlex Elder 
114884f9bd12SAlex Elder 	return -ENOMEM;
114984f9bd12SAlex Elder }
115084f9bd12SAlex Elder 
115184f9bd12SAlex Elder static void ipa_endpoint_status(struct ipa_endpoint *endpoint)
115284f9bd12SAlex Elder {
115384f9bd12SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
115484f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
11556a244b75SAlex Elder 	const struct ipa_reg *reg;
115684f9bd12SAlex Elder 	u32 val = 0;
115784f9bd12SAlex Elder 
11586a244b75SAlex Elder 	reg = ipa_reg(ipa, ENDP_STATUS);
1159660e52d6SAlex Elder 	if (endpoint->config.status_enable) {
116084f9bd12SAlex Elder 		val |= STATUS_EN_FMASK;
116184f9bd12SAlex Elder 		if (endpoint->toward_ipa) {
116284f9bd12SAlex Elder 			enum ipa_endpoint_name name;
116384f9bd12SAlex Elder 			u32 status_endpoint_id;
116484f9bd12SAlex Elder 
1165660e52d6SAlex Elder 			name = endpoint->config.tx.status_endpoint;
116684f9bd12SAlex Elder 			status_endpoint_id = ipa->name_map[name]->endpoint_id;
116784f9bd12SAlex Elder 
116884f9bd12SAlex Elder 			val |= u32_encode_bits(status_endpoint_id,
116984f9bd12SAlex Elder 					       STATUS_ENDP_FMASK);
117084f9bd12SAlex Elder 		}
11718bfc4e21SAlex Elder 		/* STATUS_LOCATION is 0, meaning status element precedes
11728bfc4e21SAlex Elder 		 * packet (not present for IPA v4.5)
11738bfc4e21SAlex Elder 		 */
11748bfc4e21SAlex Elder 		/* STATUS_PKT_SUPPRESS_FMASK is 0 (not present for v3.5.1) */
117584f9bd12SAlex Elder 	}
117684f9bd12SAlex Elder 
11776a244b75SAlex Elder 	iowrite32(val, ipa->reg_virt + ipa_reg_n_offset(reg, endpoint_id));
117884f9bd12SAlex Elder }
117984f9bd12SAlex Elder 
11806a606b90SAlex Elder static int ipa_endpoint_replenish_one(struct ipa_endpoint *endpoint,
11816a606b90SAlex Elder 				      struct gsi_trans *trans)
118284f9bd12SAlex Elder {
118384f9bd12SAlex Elder 	struct page *page;
1184ed23f026SAlex Elder 	u32 buffer_size;
118584f9bd12SAlex Elder 	u32 offset;
118684f9bd12SAlex Elder 	u32 len;
118784f9bd12SAlex Elder 	int ret;
118884f9bd12SAlex Elder 
1189660e52d6SAlex Elder 	buffer_size = endpoint->config.rx.buffer_size;
1190ed23f026SAlex Elder 	page = dev_alloc_pages(get_order(buffer_size));
119184f9bd12SAlex Elder 	if (!page)
11926a606b90SAlex Elder 		return -ENOMEM;
119384f9bd12SAlex Elder 
119484f9bd12SAlex Elder 	/* Offset the buffer to make space for skb headroom */
119584f9bd12SAlex Elder 	offset = NET_SKB_PAD;
1196ed23f026SAlex Elder 	len = buffer_size - offset;
119784f9bd12SAlex Elder 
119884f9bd12SAlex Elder 	ret = gsi_trans_page_add(trans, page, len, offset);
119984f9bd12SAlex Elder 	if (ret)
120070132763SAlex Elder 		put_page(page);
12016a606b90SAlex Elder 	else
120284f9bd12SAlex Elder 		trans->data = page;	/* transaction owns page now */
120384f9bd12SAlex Elder 
12046a606b90SAlex Elder 	return ret;
120584f9bd12SAlex Elder }
120684f9bd12SAlex Elder 
120784f9bd12SAlex Elder /**
12089af5ccf3SAlex Elder  * ipa_endpoint_replenish() - Replenish endpoint receive buffers
1209e3eea08eSAlex Elder  * @endpoint:	Endpoint to be replenished
121084f9bd12SAlex Elder  *
12119af5ccf3SAlex Elder  * The IPA hardware can hold a fixed number of receive buffers for an RX
12129af5ccf3SAlex Elder  * endpoint, based on the number of entries in the underlying channel ring
12139af5ccf3SAlex Elder  * buffer.  If an endpoint's "backlog" is non-zero, it indicates how many
12149af5ccf3SAlex Elder  * more receive buffers can be supplied to the hardware.  Replenishing for
1215a9bec7aeSAlex Elder  * an endpoint can be disabled, in which case buffers are not queued to
1216a9bec7aeSAlex Elder  * the hardware.
121784f9bd12SAlex Elder  */
12184b22d841SAlex Elder static void ipa_endpoint_replenish(struct ipa_endpoint *endpoint)
121984f9bd12SAlex Elder {
12206a606b90SAlex Elder 	struct gsi_trans *trans;
122184f9bd12SAlex Elder 
12224b22d841SAlex Elder 	if (!test_bit(IPA_REPLENISH_ENABLED, endpoint->replenish_flags))
122384f9bd12SAlex Elder 		return;
122484f9bd12SAlex Elder 
12254b22d841SAlex Elder 	/* Skip it if it's already active */
12264b22d841SAlex Elder 	if (test_and_set_bit(IPA_REPLENISH_ACTIVE, endpoint->replenish_flags))
1227998c0bd2SAlex Elder 		return;
1228998c0bd2SAlex Elder 
1229d0ac30e7SAlex Elder 	while ((trans = ipa_endpoint_trans_alloc(endpoint, 1))) {
12309654d8c4SAlex Elder 		bool doorbell;
12319654d8c4SAlex Elder 
12326a606b90SAlex Elder 		if (ipa_endpoint_replenish_one(endpoint, trans))
12336a606b90SAlex Elder 			goto try_again_later;
1234b9dbabc5SAlex Elder 
1235b9dbabc5SAlex Elder 
1236b9dbabc5SAlex Elder 		/* Ring the doorbell if we've got a full batch */
12379654d8c4SAlex Elder 		doorbell = !(++endpoint->replenish_count % IPA_REPLENISH_BATCH);
12389654d8c4SAlex Elder 		gsi_trans_commit(trans, doorbell);
1239b9dbabc5SAlex Elder 	}
1240998c0bd2SAlex Elder 
1241998c0bd2SAlex Elder 	clear_bit(IPA_REPLENISH_ACTIVE, endpoint->replenish_flags);
1242998c0bd2SAlex Elder 
124384f9bd12SAlex Elder 	return;
124484f9bd12SAlex Elder 
124584f9bd12SAlex Elder try_again_later:
12466a606b90SAlex Elder 	gsi_trans_free(trans);
1247998c0bd2SAlex Elder 	clear_bit(IPA_REPLENISH_ACTIVE, endpoint->replenish_flags);
1248998c0bd2SAlex Elder 
124984f9bd12SAlex Elder 	/* Whenever a receive buffer transaction completes we'll try to
125084f9bd12SAlex Elder 	 * replenish again.  It's unlikely, but if we fail to supply even
125184f9bd12SAlex Elder 	 * one buffer, nothing will trigger another replenish attempt.
12525fc7f9baSAlex Elder 	 * If the hardware has no receive buffers queued, schedule work to
12535fc7f9baSAlex Elder 	 * try replenishing again.
125484f9bd12SAlex Elder 	 */
12555fc7f9baSAlex Elder 	if (gsi_channel_trans_idle(&endpoint->ipa->gsi, endpoint->channel_id))
125684f9bd12SAlex Elder 		schedule_delayed_work(&endpoint->replenish_work,
125784f9bd12SAlex Elder 				      msecs_to_jiffies(1));
125884f9bd12SAlex Elder }
125984f9bd12SAlex Elder 
126084f9bd12SAlex Elder static void ipa_endpoint_replenish_enable(struct ipa_endpoint *endpoint)
126184f9bd12SAlex Elder {
1262c1aaa01dSAlex Elder 	set_bit(IPA_REPLENISH_ENABLED, endpoint->replenish_flags);
126384f9bd12SAlex Elder 
126484f9bd12SAlex Elder 	/* Start replenishing if hardware currently has no buffers */
12655fc7f9baSAlex Elder 	if (gsi_channel_trans_idle(&endpoint->ipa->gsi, endpoint->channel_id))
12664b22d841SAlex Elder 		ipa_endpoint_replenish(endpoint);
126784f9bd12SAlex Elder }
126884f9bd12SAlex Elder 
126984f9bd12SAlex Elder static void ipa_endpoint_replenish_disable(struct ipa_endpoint *endpoint)
127084f9bd12SAlex Elder {
1271c1aaa01dSAlex Elder 	clear_bit(IPA_REPLENISH_ENABLED, endpoint->replenish_flags);
127284f9bd12SAlex Elder }
127384f9bd12SAlex Elder 
127484f9bd12SAlex Elder static void ipa_endpoint_replenish_work(struct work_struct *work)
127584f9bd12SAlex Elder {
127684f9bd12SAlex Elder 	struct delayed_work *dwork = to_delayed_work(work);
127784f9bd12SAlex Elder 	struct ipa_endpoint *endpoint;
127884f9bd12SAlex Elder 
127984f9bd12SAlex Elder 	endpoint = container_of(dwork, struct ipa_endpoint, replenish_work);
128084f9bd12SAlex Elder 
12814b22d841SAlex Elder 	ipa_endpoint_replenish(endpoint);
128284f9bd12SAlex Elder }
128384f9bd12SAlex Elder 
128484f9bd12SAlex Elder static void ipa_endpoint_skb_copy(struct ipa_endpoint *endpoint,
128584f9bd12SAlex Elder 				  void *data, u32 len, u32 extra)
128684f9bd12SAlex Elder {
128784f9bd12SAlex Elder 	struct sk_buff *skb;
128884f9bd12SAlex Elder 
12891b65bbccSAlex Elder 	if (!endpoint->netdev)
12901b65bbccSAlex Elder 		return;
12911b65bbccSAlex Elder 
129284f9bd12SAlex Elder 	skb = __dev_alloc_skb(len, GFP_ATOMIC);
129330b338ffSAlex Elder 	if (skb) {
12941b65bbccSAlex Elder 		/* Copy the data into the socket buffer and receive it */
129584f9bd12SAlex Elder 		skb_put(skb, len);
129684f9bd12SAlex Elder 		memcpy(skb->data, data, len);
129784f9bd12SAlex Elder 		skb->truesize += extra;
129830b338ffSAlex Elder 	}
129984f9bd12SAlex Elder 
130084f9bd12SAlex Elder 	ipa_modem_skb_rx(endpoint->netdev, skb);
130184f9bd12SAlex Elder }
130284f9bd12SAlex Elder 
130384f9bd12SAlex Elder static bool ipa_endpoint_skb_build(struct ipa_endpoint *endpoint,
130484f9bd12SAlex Elder 				   struct page *page, u32 len)
130584f9bd12SAlex Elder {
1306660e52d6SAlex Elder 	u32 buffer_size = endpoint->config.rx.buffer_size;
130784f9bd12SAlex Elder 	struct sk_buff *skb;
130884f9bd12SAlex Elder 
130984f9bd12SAlex Elder 	/* Nothing to do if there's no netdev */
131084f9bd12SAlex Elder 	if (!endpoint->netdev)
131184f9bd12SAlex Elder 		return false;
131284f9bd12SAlex Elder 
1313ed23f026SAlex Elder 	WARN_ON(len > SKB_WITH_OVERHEAD(buffer_size - NET_SKB_PAD));
13145bc55884SAlex Elder 
1315ed23f026SAlex Elder 	skb = build_skb(page_address(page), buffer_size);
131684f9bd12SAlex Elder 	if (skb) {
131784f9bd12SAlex Elder 		/* Reserve the headroom and account for the data */
131884f9bd12SAlex Elder 		skb_reserve(skb, NET_SKB_PAD);
131984f9bd12SAlex Elder 		skb_put(skb, len);
132084f9bd12SAlex Elder 	}
132184f9bd12SAlex Elder 
132284f9bd12SAlex Elder 	/* Receive the buffer (or record drop if unable to build it) */
132384f9bd12SAlex Elder 	ipa_modem_skb_rx(endpoint->netdev, skb);
132484f9bd12SAlex Elder 
132584f9bd12SAlex Elder 	return skb != NULL;
132684f9bd12SAlex Elder }
132784f9bd12SAlex Elder 
132884f9bd12SAlex Elder /* The format of a packet status element is the same for several status
132945921390SAlex Elder  * types (opcodes).  Other types aren't currently supported.
133084f9bd12SAlex Elder  */
133184f9bd12SAlex Elder static bool ipa_status_format_packet(enum ipa_status_opcode opcode)
133284f9bd12SAlex Elder {
133384f9bd12SAlex Elder 	switch (opcode) {
133484f9bd12SAlex Elder 	case IPA_STATUS_OPCODE_PACKET:
133584f9bd12SAlex Elder 	case IPA_STATUS_OPCODE_DROPPED_PACKET:
133684f9bd12SAlex Elder 	case IPA_STATUS_OPCODE_SUSPENDED_PACKET:
133784f9bd12SAlex Elder 	case IPA_STATUS_OPCODE_PACKET_2ND_PASS:
133884f9bd12SAlex Elder 		return true;
133984f9bd12SAlex Elder 	default:
134084f9bd12SAlex Elder 		return false;
134184f9bd12SAlex Elder 	}
134284f9bd12SAlex Elder }
134384f9bd12SAlex Elder 
134484f9bd12SAlex Elder static bool ipa_endpoint_status_skip(struct ipa_endpoint *endpoint,
134584f9bd12SAlex Elder 				     const struct ipa_status *status)
134684f9bd12SAlex Elder {
134784f9bd12SAlex Elder 	u32 endpoint_id;
134884f9bd12SAlex Elder 
134984f9bd12SAlex Elder 	if (!ipa_status_format_packet(status->opcode))
135084f9bd12SAlex Elder 		return true;
135184f9bd12SAlex Elder 	if (!status->pkt_len)
135284f9bd12SAlex Elder 		return true;
1353c13899f1SAlex Elder 	endpoint_id = u8_get_bits(status->endp_dst_idx,
135484f9bd12SAlex Elder 				  IPA_STATUS_DST_IDX_FMASK);
135584f9bd12SAlex Elder 	if (endpoint_id != endpoint->endpoint_id)
135684f9bd12SAlex Elder 		return true;
135784f9bd12SAlex Elder 
135884f9bd12SAlex Elder 	return false;	/* Don't skip this packet, process it */
135984f9bd12SAlex Elder }
136084f9bd12SAlex Elder 
1361f6aba7b5SAlex Elder static bool ipa_endpoint_status_tag(struct ipa_endpoint *endpoint,
1362f6aba7b5SAlex Elder 				    const struct ipa_status *status)
1363f6aba7b5SAlex Elder {
136451c48ce2SAlex Elder 	struct ipa_endpoint *command_endpoint;
136551c48ce2SAlex Elder 	struct ipa *ipa = endpoint->ipa;
136651c48ce2SAlex Elder 	u32 endpoint_id;
136751c48ce2SAlex Elder 
136851c48ce2SAlex Elder 	if (!le16_get_bits(status->mask, IPA_STATUS_MASK_TAG_VALID_FMASK))
136951c48ce2SAlex Elder 		return false;	/* No valid tag */
137051c48ce2SAlex Elder 
137151c48ce2SAlex Elder 	/* The status contains a valid tag.  We know the packet was sent to
137251c48ce2SAlex Elder 	 * this endpoint (already verified by ipa_endpoint_status_skip()).
137351c48ce2SAlex Elder 	 * If the packet came from the AP->command TX endpoint we know
137451c48ce2SAlex Elder 	 * this packet was sent as part of the pipeline clear process.
137551c48ce2SAlex Elder 	 */
137651c48ce2SAlex Elder 	endpoint_id = u8_get_bits(status->endp_src_idx,
137751c48ce2SAlex Elder 				  IPA_STATUS_SRC_IDX_FMASK);
137851c48ce2SAlex Elder 	command_endpoint = ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX];
137951c48ce2SAlex Elder 	if (endpoint_id == command_endpoint->endpoint_id) {
138051c48ce2SAlex Elder 		complete(&ipa->completion);
138151c48ce2SAlex Elder 	} else {
138251c48ce2SAlex Elder 		dev_err(&ipa->pdev->dev,
138351c48ce2SAlex Elder 			"unexpected tagged packet from endpoint %u\n",
138451c48ce2SAlex Elder 			endpoint_id);
138551c48ce2SAlex Elder 	}
138651c48ce2SAlex Elder 
138751c48ce2SAlex Elder 	return true;
1388f6aba7b5SAlex Elder }
1389f6aba7b5SAlex Elder 
139084f9bd12SAlex Elder /* Return whether the status indicates the packet should be dropped */
1391f6aba7b5SAlex Elder static bool ipa_endpoint_status_drop(struct ipa_endpoint *endpoint,
1392f6aba7b5SAlex Elder 				     const struct ipa_status *status)
139384f9bd12SAlex Elder {
139484f9bd12SAlex Elder 	u32 val;
139584f9bd12SAlex Elder 
1396f6aba7b5SAlex Elder 	/* If the status indicates a tagged transfer, we'll drop the packet */
1397f6aba7b5SAlex Elder 	if (ipa_endpoint_status_tag(endpoint, status))
1398f6aba7b5SAlex Elder 		return true;
1399f6aba7b5SAlex Elder 
1400ab4f71e5SAlex Elder 	/* Deaggregation exceptions we drop; all other types we consume */
140184f9bd12SAlex Elder 	if (status->exception)
140284f9bd12SAlex Elder 		return status->exception == IPA_STATUS_EXCEPTION_DEAGGR;
140384f9bd12SAlex Elder 
140484f9bd12SAlex Elder 	/* Drop the packet if it fails to match a routing rule; otherwise no */
140584f9bd12SAlex Elder 	val = le32_get_bits(status->flags1, IPA_STATUS_FLAGS1_RT_RULE_ID_FMASK);
140684f9bd12SAlex Elder 
140784f9bd12SAlex Elder 	return val == field_max(IPA_STATUS_FLAGS1_RT_RULE_ID_FMASK);
140884f9bd12SAlex Elder }
140984f9bd12SAlex Elder 
141084f9bd12SAlex Elder static void ipa_endpoint_status_parse(struct ipa_endpoint *endpoint,
141184f9bd12SAlex Elder 				      struct page *page, u32 total_len)
141284f9bd12SAlex Elder {
1413660e52d6SAlex Elder 	u32 buffer_size = endpoint->config.rx.buffer_size;
141484f9bd12SAlex Elder 	void *data = page_address(page) + NET_SKB_PAD;
1415ed23f026SAlex Elder 	u32 unused = buffer_size - total_len;
141684f9bd12SAlex Elder 	u32 resid = total_len;
141784f9bd12SAlex Elder 
141884f9bd12SAlex Elder 	while (resid) {
141984f9bd12SAlex Elder 		const struct ipa_status *status = data;
142084f9bd12SAlex Elder 		u32 align;
142184f9bd12SAlex Elder 		u32 len;
142284f9bd12SAlex Elder 
142384f9bd12SAlex Elder 		if (resid < sizeof(*status)) {
142484f9bd12SAlex Elder 			dev_err(&endpoint->ipa->pdev->dev,
142584f9bd12SAlex Elder 				"short message (%u bytes < %zu byte status)\n",
142684f9bd12SAlex Elder 				resid, sizeof(*status));
142784f9bd12SAlex Elder 			break;
142884f9bd12SAlex Elder 		}
142984f9bd12SAlex Elder 
143084f9bd12SAlex Elder 		/* Skip over status packets that lack packet data */
143184f9bd12SAlex Elder 		if (ipa_endpoint_status_skip(endpoint, status)) {
143284f9bd12SAlex Elder 			data += sizeof(*status);
143384f9bd12SAlex Elder 			resid -= sizeof(*status);
143484f9bd12SAlex Elder 			continue;
143584f9bd12SAlex Elder 		}
143684f9bd12SAlex Elder 
1437162fbc6fSAlex Elder 		/* Compute the amount of buffer space consumed by the packet,
1438162fbc6fSAlex Elder 		 * including the status element.  If the hardware is configured
1439162fbc6fSAlex Elder 		 * to pad packet data to an aligned boundary, account for that.
1440162fbc6fSAlex Elder 		 * And if checksum offload is enabled a trailer containing
1441162fbc6fSAlex Elder 		 * computed checksum information will be appended.
144284f9bd12SAlex Elder 		 */
1443660e52d6SAlex Elder 		align = endpoint->config.rx.pad_align ? : 1;
144484f9bd12SAlex Elder 		len = le16_to_cpu(status->pkt_len);
144584f9bd12SAlex Elder 		len = sizeof(*status) + ALIGN(len, align);
1446660e52d6SAlex Elder 		if (endpoint->config.checksum)
144784f9bd12SAlex Elder 			len += sizeof(struct rmnet_map_dl_csum_trailer);
144884f9bd12SAlex Elder 
1449f6aba7b5SAlex Elder 		if (!ipa_endpoint_status_drop(endpoint, status)) {
1450162fbc6fSAlex Elder 			void *data2;
1451162fbc6fSAlex Elder 			u32 extra;
1452162fbc6fSAlex Elder 			u32 len2;
145384f9bd12SAlex Elder 
145484f9bd12SAlex Elder 			/* Client receives only packet data (no status) */
1455162fbc6fSAlex Elder 			data2 = data + sizeof(*status);
1456162fbc6fSAlex Elder 			len2 = le16_to_cpu(status->pkt_len);
1457162fbc6fSAlex Elder 
1458162fbc6fSAlex Elder 			/* Have the true size reflect the extra unused space in
1459162fbc6fSAlex Elder 			 * the original receive buffer.  Distribute the "cost"
1460162fbc6fSAlex Elder 			 * proportionately across all aggregated packets in the
1461162fbc6fSAlex Elder 			 * buffer.
1462162fbc6fSAlex Elder 			 */
1463162fbc6fSAlex Elder 			extra = DIV_ROUND_CLOSEST(unused * len, total_len);
146484f9bd12SAlex Elder 			ipa_endpoint_skb_copy(endpoint, data2, len2, extra);
146584f9bd12SAlex Elder 		}
146684f9bd12SAlex Elder 
146784f9bd12SAlex Elder 		/* Consume status and the full packet it describes */
146884f9bd12SAlex Elder 		data += len;
146984f9bd12SAlex Elder 		resid -= len;
147084f9bd12SAlex Elder 	}
147184f9bd12SAlex Elder }
147284f9bd12SAlex Elder 
1473983a1a30SAlex Elder void ipa_endpoint_trans_complete(struct ipa_endpoint *endpoint,
147484f9bd12SAlex Elder 				 struct gsi_trans *trans)
147584f9bd12SAlex Elder {
147684f9bd12SAlex Elder 	struct page *page;
147784f9bd12SAlex Elder 
1478983a1a30SAlex Elder 	if (endpoint->toward_ipa)
1479983a1a30SAlex Elder 		return;
1480983a1a30SAlex Elder 
148184f9bd12SAlex Elder 	if (trans->cancelled)
14825d6ac24fSAlex Elder 		goto done;
148384f9bd12SAlex Elder 
148484f9bd12SAlex Elder 	/* Parse or build a socket buffer using the actual received length */
148584f9bd12SAlex Elder 	page = trans->data;
1486660e52d6SAlex Elder 	if (endpoint->config.status_enable)
148784f9bd12SAlex Elder 		ipa_endpoint_status_parse(endpoint, page, trans->len);
148884f9bd12SAlex Elder 	else if (ipa_endpoint_skb_build(endpoint, page, trans->len))
148984f9bd12SAlex Elder 		trans->data = NULL;	/* Pages have been consumed */
14905d6ac24fSAlex Elder done:
14915d6ac24fSAlex Elder 	ipa_endpoint_replenish(endpoint);
149284f9bd12SAlex Elder }
149384f9bd12SAlex Elder 
149484f9bd12SAlex Elder void ipa_endpoint_trans_release(struct ipa_endpoint *endpoint,
149584f9bd12SAlex Elder 				struct gsi_trans *trans)
149684f9bd12SAlex Elder {
149784f9bd12SAlex Elder 	if (endpoint->toward_ipa) {
149884f9bd12SAlex Elder 		struct ipa *ipa = endpoint->ipa;
149984f9bd12SAlex Elder 
150084f9bd12SAlex Elder 		/* Nothing to do for command transactions */
150184f9bd12SAlex Elder 		if (endpoint != ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]) {
150284f9bd12SAlex Elder 			struct sk_buff *skb = trans->data;
150384f9bd12SAlex Elder 
150484f9bd12SAlex Elder 			if (skb)
150584f9bd12SAlex Elder 				dev_kfree_skb_any(skb);
150684f9bd12SAlex Elder 		}
150784f9bd12SAlex Elder 	} else {
150884f9bd12SAlex Elder 		struct page *page = trans->data;
150984f9bd12SAlex Elder 
1510155c0c90SAlex Elder 		if (page)
1511155c0c90SAlex Elder 			put_page(page);
151284f9bd12SAlex Elder 	}
151384f9bd12SAlex Elder }
151484f9bd12SAlex Elder 
151584f9bd12SAlex Elder void ipa_endpoint_default_route_set(struct ipa *ipa, u32 endpoint_id)
151684f9bd12SAlex Elder {
15176a244b75SAlex Elder 	const struct ipa_reg *reg;
151884f9bd12SAlex Elder 	u32 val;
151984f9bd12SAlex Elder 
15206a244b75SAlex Elder 	reg = ipa_reg(ipa, ROUTE);
152184f9bd12SAlex Elder 	/* ROUTE_DIS is 0 */
1522479deb32SAlex Elder 	val = ipa_reg_encode(reg, ROUTE_DEF_PIPE, endpoint_id);
1523479deb32SAlex Elder 	val |= ipa_reg_bit(reg, ROUTE_DEF_HDR_TABLE);
1524479deb32SAlex Elder 	/* ROUTE_DEF_HDR_OFST is 0 */
1525479deb32SAlex Elder 	val |= ipa_reg_encode(reg, ROUTE_FRAG_DEF_PIPE, endpoint_id);
1526479deb32SAlex Elder 	val |= ipa_reg_bit(reg, ROUTE_DEF_RETAIN_HDR);
152784f9bd12SAlex Elder 
15286a244b75SAlex Elder 	iowrite32(val, ipa->reg_virt + ipa_reg_offset(reg));
152984f9bd12SAlex Elder }
153084f9bd12SAlex Elder 
153184f9bd12SAlex Elder void ipa_endpoint_default_route_clear(struct ipa *ipa)
153284f9bd12SAlex Elder {
153384f9bd12SAlex Elder 	ipa_endpoint_default_route_set(ipa, 0);
153484f9bd12SAlex Elder }
153584f9bd12SAlex Elder 
153684f9bd12SAlex Elder /**
153784f9bd12SAlex Elder  * ipa_endpoint_reset_rx_aggr() - Reset RX endpoint with aggregation active
153884f9bd12SAlex Elder  * @endpoint:	Endpoint to be reset
153984f9bd12SAlex Elder  *
154084f9bd12SAlex Elder  * If aggregation is active on an RX endpoint when a reset is performed
154184f9bd12SAlex Elder  * on its underlying GSI channel, a special sequence of actions must be
154284f9bd12SAlex Elder  * taken to ensure the IPA pipeline is properly cleared.
154384f9bd12SAlex Elder  *
1544e3eea08eSAlex Elder  * Return:	0 if successful, or a negative error code
154584f9bd12SAlex Elder  */
154684f9bd12SAlex Elder static int ipa_endpoint_reset_rx_aggr(struct ipa_endpoint *endpoint)
154784f9bd12SAlex Elder {
154884f9bd12SAlex Elder 	struct device *dev = &endpoint->ipa->pdev->dev;
154984f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
155084f9bd12SAlex Elder 	struct gsi *gsi = &ipa->gsi;
15514fa95248SAlex Elder 	bool suspended = false;
155284f9bd12SAlex Elder 	dma_addr_t addr;
155384f9bd12SAlex Elder 	u32 retries;
155484f9bd12SAlex Elder 	u32 len = 1;
155584f9bd12SAlex Elder 	void *virt;
155684f9bd12SAlex Elder 	int ret;
155784f9bd12SAlex Elder 
155884f9bd12SAlex Elder 	virt = kzalloc(len, GFP_KERNEL);
155984f9bd12SAlex Elder 	if (!virt)
156084f9bd12SAlex Elder 		return -ENOMEM;
156184f9bd12SAlex Elder 
156284f9bd12SAlex Elder 	addr = dma_map_single(dev, virt, len, DMA_FROM_DEVICE);
156384f9bd12SAlex Elder 	if (dma_mapping_error(dev, addr)) {
156484f9bd12SAlex Elder 		ret = -ENOMEM;
156584f9bd12SAlex Elder 		goto out_kfree;
156684f9bd12SAlex Elder 	}
156784f9bd12SAlex Elder 
156884f9bd12SAlex Elder 	/* Force close aggregation before issuing the reset */
156984f9bd12SAlex Elder 	ipa_endpoint_force_close(endpoint);
157084f9bd12SAlex Elder 
157184f9bd12SAlex Elder 	/* Reset and reconfigure the channel with the doorbell engine
157284f9bd12SAlex Elder 	 * disabled.  Then poll until we know aggregation is no longer
157384f9bd12SAlex Elder 	 * active.  We'll re-enable the doorbell (if appropriate) when
157484f9bd12SAlex Elder 	 * we reset again below.
157584f9bd12SAlex Elder 	 */
157684f9bd12SAlex Elder 	gsi_channel_reset(gsi, endpoint->channel_id, false);
157784f9bd12SAlex Elder 
157884f9bd12SAlex Elder 	/* Make sure the channel isn't suspended */
15794fa95248SAlex Elder 	suspended = ipa_endpoint_program_suspend(endpoint, false);
158084f9bd12SAlex Elder 
158184f9bd12SAlex Elder 	/* Start channel and do a 1 byte read */
158284f9bd12SAlex Elder 	ret = gsi_channel_start(gsi, endpoint->channel_id);
158384f9bd12SAlex Elder 	if (ret)
158484f9bd12SAlex Elder 		goto out_suspend_again;
158584f9bd12SAlex Elder 
158684f9bd12SAlex Elder 	ret = gsi_trans_read_byte(gsi, endpoint->channel_id, addr);
158784f9bd12SAlex Elder 	if (ret)
158884f9bd12SAlex Elder 		goto err_endpoint_stop;
158984f9bd12SAlex Elder 
159084f9bd12SAlex Elder 	/* Wait for aggregation to be closed on the channel */
159184f9bd12SAlex Elder 	retries = IPA_ENDPOINT_RESET_AGGR_RETRY_MAX;
159284f9bd12SAlex Elder 	do {
159384f9bd12SAlex Elder 		if (!ipa_endpoint_aggr_active(endpoint))
159484f9bd12SAlex Elder 			break;
159574401946SAlex Elder 		usleep_range(USEC_PER_MSEC, 2 * USEC_PER_MSEC);
159684f9bd12SAlex Elder 	} while (retries--);
159784f9bd12SAlex Elder 
159884f9bd12SAlex Elder 	/* Check one last time */
159984f9bd12SAlex Elder 	if (ipa_endpoint_aggr_active(endpoint))
160084f9bd12SAlex Elder 		dev_err(dev, "endpoint %u still active during reset\n",
160184f9bd12SAlex Elder 			endpoint->endpoint_id);
160284f9bd12SAlex Elder 
160384f9bd12SAlex Elder 	gsi_trans_read_byte_done(gsi, endpoint->channel_id);
160484f9bd12SAlex Elder 
1605f30dcb7dSAlex Elder 	ret = gsi_channel_stop(gsi, endpoint->channel_id);
160684f9bd12SAlex Elder 	if (ret)
160784f9bd12SAlex Elder 		goto out_suspend_again;
160884f9bd12SAlex Elder 
1609497abc87SPeng Li 	/* Finally, reset and reconfigure the channel again (re-enabling
161084f9bd12SAlex Elder 	 * the doorbell engine if appropriate).  Sleep for 1 millisecond to
161184f9bd12SAlex Elder 	 * complete the channel reset sequence.  Finish by suspending the
161284f9bd12SAlex Elder 	 * channel again (if necessary).
161384f9bd12SAlex Elder 	 */
1614ce54993dSAlex Elder 	gsi_channel_reset(gsi, endpoint->channel_id, true);
161584f9bd12SAlex Elder 
161674401946SAlex Elder 	usleep_range(USEC_PER_MSEC, 2 * USEC_PER_MSEC);
161784f9bd12SAlex Elder 
161884f9bd12SAlex Elder 	goto out_suspend_again;
161984f9bd12SAlex Elder 
162084f9bd12SAlex Elder err_endpoint_stop:
1621f30dcb7dSAlex Elder 	(void)gsi_channel_stop(gsi, endpoint->channel_id);
162284f9bd12SAlex Elder out_suspend_again:
16234fa95248SAlex Elder 	if (suspended)
16244fa95248SAlex Elder 		(void)ipa_endpoint_program_suspend(endpoint, true);
162584f9bd12SAlex Elder 	dma_unmap_single(dev, addr, len, DMA_FROM_DEVICE);
162684f9bd12SAlex Elder out_kfree:
162784f9bd12SAlex Elder 	kfree(virt);
162884f9bd12SAlex Elder 
162984f9bd12SAlex Elder 	return ret;
163084f9bd12SAlex Elder }
163184f9bd12SAlex Elder 
163284f9bd12SAlex Elder static void ipa_endpoint_reset(struct ipa_endpoint *endpoint)
163384f9bd12SAlex Elder {
163484f9bd12SAlex Elder 	u32 channel_id = endpoint->channel_id;
163584f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
163684f9bd12SAlex Elder 	bool special;
163784f9bd12SAlex Elder 	int ret = 0;
163884f9bd12SAlex Elder 
163984f9bd12SAlex Elder 	/* On IPA v3.5.1, if an RX endpoint is reset while aggregation
164084f9bd12SAlex Elder 	 * is active, we need to handle things specially to recover.
164184f9bd12SAlex Elder 	 * All other cases just need to reset the underlying GSI channel.
164284f9bd12SAlex Elder 	 */
1643d7f3087bSAlex Elder 	special = ipa->version < IPA_VERSION_4_0 && !endpoint->toward_ipa &&
1644660e52d6SAlex Elder 			endpoint->config.aggregation;
1645ce54993dSAlex Elder 	if (special && ipa_endpoint_aggr_active(endpoint))
164684f9bd12SAlex Elder 		ret = ipa_endpoint_reset_rx_aggr(endpoint);
164784f9bd12SAlex Elder 	else
1648ce54993dSAlex Elder 		gsi_channel_reset(&ipa->gsi, channel_id, true);
164984f9bd12SAlex Elder 
165084f9bd12SAlex Elder 	if (ret)
165184f9bd12SAlex Elder 		dev_err(&ipa->pdev->dev,
165284f9bd12SAlex Elder 			"error %d resetting channel %u for endpoint %u\n",
165384f9bd12SAlex Elder 			ret, endpoint->channel_id, endpoint->endpoint_id);
165484f9bd12SAlex Elder }
165584f9bd12SAlex Elder 
165684f9bd12SAlex Elder static void ipa_endpoint_program(struct ipa_endpoint *endpoint)
165784f9bd12SAlex Elder {
16584c9d631aSAlex Elder 	if (endpoint->toward_ipa) {
16594c9d631aSAlex Elder 		/* Newer versions of IPA use GSI channel flow control
16604c9d631aSAlex Elder 		 * instead of endpoint DELAY mode to prevent sending data.
16614c9d631aSAlex Elder 		 * Flow control is disabled for newly-allocated channels,
16624c9d631aSAlex Elder 		 * and we can assume flow control is not (ever) enabled
16634c9d631aSAlex Elder 		 * for AP TX channels.
16644c9d631aSAlex Elder 		 */
16654c9d631aSAlex Elder 		if (endpoint->ipa->version < IPA_VERSION_4_2)
1666a4dcad34SAlex Elder 			ipa_endpoint_program_delay(endpoint, false);
16674c9d631aSAlex Elder 	} else {
16684c9d631aSAlex Elder 		/* Ensure suspend mode is off on all AP RX endpoints */
1669fb57c3eaSAlex Elder 		(void)ipa_endpoint_program_suspend(endpoint, false);
16704c9d631aSAlex Elder 	}
1671fb57c3eaSAlex Elder 	ipa_endpoint_init_cfg(endpoint);
1672647a05f3SAlex Elder 	ipa_endpoint_init_nat(endpoint);
1673fb57c3eaSAlex Elder 	ipa_endpoint_init_hdr(endpoint);
167484f9bd12SAlex Elder 	ipa_endpoint_init_hdr_ext(endpoint);
1675fb57c3eaSAlex Elder 	ipa_endpoint_init_hdr_metadata_mask(endpoint);
1676fb57c3eaSAlex Elder 	ipa_endpoint_init_mode(endpoint);
167784f9bd12SAlex Elder 	ipa_endpoint_init_aggr(endpoint);
1678153213f0SAlex Elder 	if (!endpoint->toward_ipa) {
1679153213f0SAlex Elder 		if (endpoint->config.rx.holb_drop)
1680153213f0SAlex Elder 			ipa_endpoint_init_hol_block_enable(endpoint, 0);
1681153213f0SAlex Elder 		else
168201c36637SAlex Elder 			ipa_endpoint_init_hol_block_disable(endpoint);
1683153213f0SAlex Elder 	}
168484f9bd12SAlex Elder 	ipa_endpoint_init_deaggr(endpoint);
16852d265342SAlex Elder 	ipa_endpoint_init_rsrc_grp(endpoint);
168684f9bd12SAlex Elder 	ipa_endpoint_init_seq(endpoint);
168784f9bd12SAlex Elder 	ipa_endpoint_status(endpoint);
168884f9bd12SAlex Elder }
168984f9bd12SAlex Elder 
169084f9bd12SAlex Elder int ipa_endpoint_enable_one(struct ipa_endpoint *endpoint)
169184f9bd12SAlex Elder {
169284f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
169384f9bd12SAlex Elder 	struct gsi *gsi = &ipa->gsi;
169484f9bd12SAlex Elder 	int ret;
169584f9bd12SAlex Elder 
169684f9bd12SAlex Elder 	ret = gsi_channel_start(gsi, endpoint->channel_id);
169784f9bd12SAlex Elder 	if (ret) {
169884f9bd12SAlex Elder 		dev_err(&ipa->pdev->dev,
169984f9bd12SAlex Elder 			"error %d starting %cX channel %u for endpoint %u\n",
170084f9bd12SAlex Elder 			ret, endpoint->toward_ipa ? 'T' : 'R',
170184f9bd12SAlex Elder 			endpoint->channel_id, endpoint->endpoint_id);
170284f9bd12SAlex Elder 		return ret;
170384f9bd12SAlex Elder 	}
170484f9bd12SAlex Elder 
170584f9bd12SAlex Elder 	if (!endpoint->toward_ipa) {
170684f9bd12SAlex Elder 		ipa_interrupt_suspend_enable(ipa->interrupt,
170784f9bd12SAlex Elder 					     endpoint->endpoint_id);
170884f9bd12SAlex Elder 		ipa_endpoint_replenish_enable(endpoint);
170984f9bd12SAlex Elder 	}
171084f9bd12SAlex Elder 
171184f9bd12SAlex Elder 	ipa->enabled |= BIT(endpoint->endpoint_id);
171284f9bd12SAlex Elder 
171384f9bd12SAlex Elder 	return 0;
171484f9bd12SAlex Elder }
171584f9bd12SAlex Elder 
171684f9bd12SAlex Elder void ipa_endpoint_disable_one(struct ipa_endpoint *endpoint)
171784f9bd12SAlex Elder {
171884f9bd12SAlex Elder 	u32 mask = BIT(endpoint->endpoint_id);
171984f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
1720f30dcb7dSAlex Elder 	struct gsi *gsi = &ipa->gsi;
172184f9bd12SAlex Elder 	int ret;
172284f9bd12SAlex Elder 
1723f30dcb7dSAlex Elder 	if (!(ipa->enabled & mask))
172484f9bd12SAlex Elder 		return;
172584f9bd12SAlex Elder 
1726f30dcb7dSAlex Elder 	ipa->enabled ^= mask;
172784f9bd12SAlex Elder 
172884f9bd12SAlex Elder 	if (!endpoint->toward_ipa) {
172984f9bd12SAlex Elder 		ipa_endpoint_replenish_disable(endpoint);
173084f9bd12SAlex Elder 		ipa_interrupt_suspend_disable(ipa->interrupt,
173184f9bd12SAlex Elder 					      endpoint->endpoint_id);
173284f9bd12SAlex Elder 	}
173384f9bd12SAlex Elder 
173484f9bd12SAlex Elder 	/* Note that if stop fails, the channel's state is not well-defined */
1735f30dcb7dSAlex Elder 	ret = gsi_channel_stop(gsi, endpoint->channel_id);
173684f9bd12SAlex Elder 	if (ret)
173784f9bd12SAlex Elder 		dev_err(&ipa->pdev->dev,
173884f9bd12SAlex Elder 			"error %d attempting to stop endpoint %u\n", ret,
173984f9bd12SAlex Elder 			endpoint->endpoint_id);
174084f9bd12SAlex Elder }
174184f9bd12SAlex Elder 
174284f9bd12SAlex Elder void ipa_endpoint_suspend_one(struct ipa_endpoint *endpoint)
174384f9bd12SAlex Elder {
174484f9bd12SAlex Elder 	struct device *dev = &endpoint->ipa->pdev->dev;
174584f9bd12SAlex Elder 	struct gsi *gsi = &endpoint->ipa->gsi;
174684f9bd12SAlex Elder 	int ret;
174784f9bd12SAlex Elder 
174884f9bd12SAlex Elder 	if (!(endpoint->ipa->enabled & BIT(endpoint->endpoint_id)))
174984f9bd12SAlex Elder 		return;
175084f9bd12SAlex Elder 
1751ab4f71e5SAlex Elder 	if (!endpoint->toward_ipa) {
175284f9bd12SAlex Elder 		ipa_endpoint_replenish_disable(endpoint);
17534fa95248SAlex Elder 		(void)ipa_endpoint_program_suspend(endpoint, true);
1754ab4f71e5SAlex Elder 	}
175584f9bd12SAlex Elder 
1756decfef0fSAlex Elder 	ret = gsi_channel_suspend(gsi, endpoint->channel_id);
175784f9bd12SAlex Elder 	if (ret)
175884f9bd12SAlex Elder 		dev_err(dev, "error %d suspending channel %u\n", ret,
175984f9bd12SAlex Elder 			endpoint->channel_id);
176084f9bd12SAlex Elder }
176184f9bd12SAlex Elder 
176284f9bd12SAlex Elder void ipa_endpoint_resume_one(struct ipa_endpoint *endpoint)
176384f9bd12SAlex Elder {
176484f9bd12SAlex Elder 	struct device *dev = &endpoint->ipa->pdev->dev;
176584f9bd12SAlex Elder 	struct gsi *gsi = &endpoint->ipa->gsi;
176684f9bd12SAlex Elder 	int ret;
176784f9bd12SAlex Elder 
176884f9bd12SAlex Elder 	if (!(endpoint->ipa->enabled & BIT(endpoint->endpoint_id)))
176984f9bd12SAlex Elder 		return;
177084f9bd12SAlex Elder 
1771b07f283eSAlex Elder 	if (!endpoint->toward_ipa)
17724fa95248SAlex Elder 		(void)ipa_endpoint_program_suspend(endpoint, false);
177384f9bd12SAlex Elder 
1774decfef0fSAlex Elder 	ret = gsi_channel_resume(gsi, endpoint->channel_id);
177584f9bd12SAlex Elder 	if (ret)
177684f9bd12SAlex Elder 		dev_err(dev, "error %d resuming channel %u\n", ret,
177784f9bd12SAlex Elder 			endpoint->channel_id);
177884f9bd12SAlex Elder 	else if (!endpoint->toward_ipa)
177984f9bd12SAlex Elder 		ipa_endpoint_replenish_enable(endpoint);
178084f9bd12SAlex Elder }
178184f9bd12SAlex Elder 
178284f9bd12SAlex Elder void ipa_endpoint_suspend(struct ipa *ipa)
178384f9bd12SAlex Elder {
1784d1704382SAlex Elder 	if (!ipa->setup_complete)
1785d1704382SAlex Elder 		return;
1786d1704382SAlex Elder 
178784f9bd12SAlex Elder 	if (ipa->modem_netdev)
178884f9bd12SAlex Elder 		ipa_modem_suspend(ipa->modem_netdev);
178984f9bd12SAlex Elder 
179084f9bd12SAlex Elder 	ipa_endpoint_suspend_one(ipa->name_map[IPA_ENDPOINT_AP_LAN_RX]);
179184f9bd12SAlex Elder 	ipa_endpoint_suspend_one(ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]);
179284f9bd12SAlex Elder }
179384f9bd12SAlex Elder 
179484f9bd12SAlex Elder void ipa_endpoint_resume(struct ipa *ipa)
179584f9bd12SAlex Elder {
1796d1704382SAlex Elder 	if (!ipa->setup_complete)
1797d1704382SAlex Elder 		return;
1798d1704382SAlex Elder 
179984f9bd12SAlex Elder 	ipa_endpoint_resume_one(ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]);
180084f9bd12SAlex Elder 	ipa_endpoint_resume_one(ipa->name_map[IPA_ENDPOINT_AP_LAN_RX]);
180184f9bd12SAlex Elder 
180284f9bd12SAlex Elder 	if (ipa->modem_netdev)
180384f9bd12SAlex Elder 		ipa_modem_resume(ipa->modem_netdev);
180484f9bd12SAlex Elder }
180584f9bd12SAlex Elder 
180684f9bd12SAlex Elder static void ipa_endpoint_setup_one(struct ipa_endpoint *endpoint)
180784f9bd12SAlex Elder {
180884f9bd12SAlex Elder 	struct gsi *gsi = &endpoint->ipa->gsi;
180984f9bd12SAlex Elder 	u32 channel_id = endpoint->channel_id;
181084f9bd12SAlex Elder 
181184f9bd12SAlex Elder 	/* Only AP endpoints get set up */
181284f9bd12SAlex Elder 	if (endpoint->ee_id != GSI_EE_AP)
181384f9bd12SAlex Elder 		return;
181484f9bd12SAlex Elder 
1815317595d2SAlex Elder 	endpoint->skb_frag_max = gsi->channel[channel_id].trans_tre_max - 1;
181684f9bd12SAlex Elder 	if (!endpoint->toward_ipa) {
181784f9bd12SAlex Elder 		/* RX transactions require a single TRE, so the maximum
181884f9bd12SAlex Elder 		 * backlog is the same as the maximum outstanding TREs.
181984f9bd12SAlex Elder 		 */
1820c1aaa01dSAlex Elder 		clear_bit(IPA_REPLENISH_ENABLED, endpoint->replenish_flags);
1821998c0bd2SAlex Elder 		clear_bit(IPA_REPLENISH_ACTIVE, endpoint->replenish_flags);
182284f9bd12SAlex Elder 		INIT_DELAYED_WORK(&endpoint->replenish_work,
182384f9bd12SAlex Elder 				  ipa_endpoint_replenish_work);
182484f9bd12SAlex Elder 	}
182584f9bd12SAlex Elder 
182684f9bd12SAlex Elder 	ipa_endpoint_program(endpoint);
182784f9bd12SAlex Elder 
182884f9bd12SAlex Elder 	endpoint->ipa->set_up |= BIT(endpoint->endpoint_id);
182984f9bd12SAlex Elder }
183084f9bd12SAlex Elder 
183184f9bd12SAlex Elder static void ipa_endpoint_teardown_one(struct ipa_endpoint *endpoint)
183284f9bd12SAlex Elder {
183384f9bd12SAlex Elder 	endpoint->ipa->set_up &= ~BIT(endpoint->endpoint_id);
183484f9bd12SAlex Elder 
183584f9bd12SAlex Elder 	if (!endpoint->toward_ipa)
183684f9bd12SAlex Elder 		cancel_delayed_work_sync(&endpoint->replenish_work);
183784f9bd12SAlex Elder 
183884f9bd12SAlex Elder 	ipa_endpoint_reset(endpoint);
183984f9bd12SAlex Elder }
184084f9bd12SAlex Elder 
184184f9bd12SAlex Elder void ipa_endpoint_setup(struct ipa *ipa)
184284f9bd12SAlex Elder {
184384f9bd12SAlex Elder 	u32 initialized = ipa->initialized;
184484f9bd12SAlex Elder 
184584f9bd12SAlex Elder 	ipa->set_up = 0;
184684f9bd12SAlex Elder 	while (initialized) {
184784f9bd12SAlex Elder 		u32 endpoint_id = __ffs(initialized);
184884f9bd12SAlex Elder 
184984f9bd12SAlex Elder 		initialized ^= BIT(endpoint_id);
185084f9bd12SAlex Elder 
185184f9bd12SAlex Elder 		ipa_endpoint_setup_one(&ipa->endpoint[endpoint_id]);
185284f9bd12SAlex Elder 	}
185384f9bd12SAlex Elder }
185484f9bd12SAlex Elder 
185584f9bd12SAlex Elder void ipa_endpoint_teardown(struct ipa *ipa)
185684f9bd12SAlex Elder {
185784f9bd12SAlex Elder 	u32 set_up = ipa->set_up;
185884f9bd12SAlex Elder 
185984f9bd12SAlex Elder 	while (set_up) {
186084f9bd12SAlex Elder 		u32 endpoint_id = __fls(set_up);
186184f9bd12SAlex Elder 
186284f9bd12SAlex Elder 		set_up ^= BIT(endpoint_id);
186384f9bd12SAlex Elder 
186484f9bd12SAlex Elder 		ipa_endpoint_teardown_one(&ipa->endpoint[endpoint_id]);
186584f9bd12SAlex Elder 	}
186684f9bd12SAlex Elder 	ipa->set_up = 0;
186784f9bd12SAlex Elder }
186884f9bd12SAlex Elder 
186984f9bd12SAlex Elder int ipa_endpoint_config(struct ipa *ipa)
187084f9bd12SAlex Elder {
187184f9bd12SAlex Elder 	struct device *dev = &ipa->pdev->dev;
18726a244b75SAlex Elder 	const struct ipa_reg *reg;
187384f9bd12SAlex Elder 	u32 initialized;
187484f9bd12SAlex Elder 	u32 rx_base;
187584f9bd12SAlex Elder 	u32 rx_mask;
187684f9bd12SAlex Elder 	u32 tx_mask;
187784f9bd12SAlex Elder 	int ret = 0;
187884f9bd12SAlex Elder 	u32 max;
187984f9bd12SAlex Elder 	u32 val;
188084f9bd12SAlex Elder 
1881110971d1SAlex Elder 	/* Prior to IPAv3.5, the FLAVOR_0 register was not supported.
1882110971d1SAlex Elder 	 * Furthermore, the endpoints were not grouped such that TX
1883110971d1SAlex Elder 	 * endpoint numbers started with 0 and RX endpoints had numbers
1884110971d1SAlex Elder 	 * higher than all TX endpoints, so we can't do the simple
1885110971d1SAlex Elder 	 * direction check used for newer hardware below.
1886110971d1SAlex Elder 	 *
1887110971d1SAlex Elder 	 * For hardware that doesn't support the FLAVOR_0 register,
1888110971d1SAlex Elder 	 * just set the available mask to support any endpoint, and
1889110971d1SAlex Elder 	 * assume the configuration is valid.
1890110971d1SAlex Elder 	 */
1891110971d1SAlex Elder 	if (ipa->version < IPA_VERSION_3_5) {
1892110971d1SAlex Elder 		ipa->available = ~0;
1893110971d1SAlex Elder 		return 0;
1894110971d1SAlex Elder 	}
1895110971d1SAlex Elder 
189684f9bd12SAlex Elder 	/* Find out about the endpoints supplied by the hardware, and ensure
189784f9bd12SAlex Elder 	 * the highest one doesn't exceed the number we support.
189884f9bd12SAlex Elder 	 */
18996a244b75SAlex Elder 	reg = ipa_reg(ipa, FLAVOR_0);
19006a244b75SAlex Elder 	val = ioread32(ipa->reg_virt + ipa_reg_offset(reg));
190184f9bd12SAlex Elder 
190284f9bd12SAlex Elder 	/* Our RX is an IPA producer */
19039265a4f0SAlex Elder 	rx_base = ipa_reg_decode(reg, PROD_LOWEST, val);
19049265a4f0SAlex Elder 	max = rx_base + ipa_reg_decode(reg, MAX_PROD_PIPES, val);
190584f9bd12SAlex Elder 	if (max > IPA_ENDPOINT_MAX) {
190684f9bd12SAlex Elder 		dev_err(dev, "too many endpoints (%u > %u)\n",
190784f9bd12SAlex Elder 			max, IPA_ENDPOINT_MAX);
190884f9bd12SAlex Elder 		return -EINVAL;
190984f9bd12SAlex Elder 	}
191084f9bd12SAlex Elder 	rx_mask = GENMASK(max - 1, rx_base);
191184f9bd12SAlex Elder 
191284f9bd12SAlex Elder 	/* Our TX is an IPA consumer */
19139265a4f0SAlex Elder 	max = ipa_reg_decode(reg, MAX_CONS_PIPES, val);
191484f9bd12SAlex Elder 	tx_mask = GENMASK(max - 1, 0);
191584f9bd12SAlex Elder 
191684f9bd12SAlex Elder 	ipa->available = rx_mask | tx_mask;
191784f9bd12SAlex Elder 
191884f9bd12SAlex Elder 	/* Check for initialized endpoints not supported by the hardware */
191984f9bd12SAlex Elder 	if (ipa->initialized & ~ipa->available) {
192084f9bd12SAlex Elder 		dev_err(dev, "unavailable endpoint id(s) 0x%08x\n",
192184f9bd12SAlex Elder 			ipa->initialized & ~ipa->available);
192284f9bd12SAlex Elder 		ret = -EINVAL;		/* Report other errors too */
192384f9bd12SAlex Elder 	}
192484f9bd12SAlex Elder 
192584f9bd12SAlex Elder 	initialized = ipa->initialized;
192684f9bd12SAlex Elder 	while (initialized) {
192784f9bd12SAlex Elder 		u32 endpoint_id = __ffs(initialized);
192884f9bd12SAlex Elder 		struct ipa_endpoint *endpoint;
192984f9bd12SAlex Elder 
193084f9bd12SAlex Elder 		initialized ^= BIT(endpoint_id);
193184f9bd12SAlex Elder 
193284f9bd12SAlex Elder 		/* Make sure it's pointing in the right direction */
193384f9bd12SAlex Elder 		endpoint = &ipa->endpoint[endpoint_id];
1934602a1c76SAlex Elder 		if ((endpoint_id < rx_base) != endpoint->toward_ipa) {
193584f9bd12SAlex Elder 			dev_err(dev, "endpoint id %u wrong direction\n",
193684f9bd12SAlex Elder 				endpoint_id);
193784f9bd12SAlex Elder 			ret = -EINVAL;
193884f9bd12SAlex Elder 		}
193984f9bd12SAlex Elder 	}
194084f9bd12SAlex Elder 
194184f9bd12SAlex Elder 	return ret;
194284f9bd12SAlex Elder }
194384f9bd12SAlex Elder 
194484f9bd12SAlex Elder void ipa_endpoint_deconfig(struct ipa *ipa)
194584f9bd12SAlex Elder {
194684f9bd12SAlex Elder 	ipa->available = 0;	/* Nothing more to do */
194784f9bd12SAlex Elder }
194884f9bd12SAlex Elder 
194984f9bd12SAlex Elder static void ipa_endpoint_init_one(struct ipa *ipa, enum ipa_endpoint_name name,
195084f9bd12SAlex Elder 				  const struct ipa_gsi_endpoint_data *data)
195184f9bd12SAlex Elder {
195284f9bd12SAlex Elder 	struct ipa_endpoint *endpoint;
195384f9bd12SAlex Elder 
195484f9bd12SAlex Elder 	endpoint = &ipa->endpoint[data->endpoint_id];
195584f9bd12SAlex Elder 
195684f9bd12SAlex Elder 	if (data->ee_id == GSI_EE_AP)
195784f9bd12SAlex Elder 		ipa->channel_map[data->channel_id] = endpoint;
195884f9bd12SAlex Elder 	ipa->name_map[name] = endpoint;
195984f9bd12SAlex Elder 
196084f9bd12SAlex Elder 	endpoint->ipa = ipa;
196184f9bd12SAlex Elder 	endpoint->ee_id = data->ee_id;
196284f9bd12SAlex Elder 	endpoint->channel_id = data->channel_id;
196384f9bd12SAlex Elder 	endpoint->endpoint_id = data->endpoint_id;
196484f9bd12SAlex Elder 	endpoint->toward_ipa = data->toward_ipa;
1965660e52d6SAlex Elder 	endpoint->config = data->endpoint.config;
196684f9bd12SAlex Elder 
196784f9bd12SAlex Elder 	ipa->initialized |= BIT(endpoint->endpoint_id);
196884f9bd12SAlex Elder }
196984f9bd12SAlex Elder 
1970602a1c76SAlex Elder static void ipa_endpoint_exit_one(struct ipa_endpoint *endpoint)
197184f9bd12SAlex Elder {
197284f9bd12SAlex Elder 	endpoint->ipa->initialized &= ~BIT(endpoint->endpoint_id);
197384f9bd12SAlex Elder 
197484f9bd12SAlex Elder 	memset(endpoint, 0, sizeof(*endpoint));
197584f9bd12SAlex Elder }
197684f9bd12SAlex Elder 
197784f9bd12SAlex Elder void ipa_endpoint_exit(struct ipa *ipa)
197884f9bd12SAlex Elder {
197984f9bd12SAlex Elder 	u32 initialized = ipa->initialized;
198084f9bd12SAlex Elder 
198184f9bd12SAlex Elder 	while (initialized) {
198284f9bd12SAlex Elder 		u32 endpoint_id = __fls(initialized);
198384f9bd12SAlex Elder 
198484f9bd12SAlex Elder 		initialized ^= BIT(endpoint_id);
198584f9bd12SAlex Elder 
198684f9bd12SAlex Elder 		ipa_endpoint_exit_one(&ipa->endpoint[endpoint_id]);
198784f9bd12SAlex Elder 	}
198884f9bd12SAlex Elder 	memset(ipa->name_map, 0, sizeof(ipa->name_map));
198984f9bd12SAlex Elder 	memset(ipa->channel_map, 0, sizeof(ipa->channel_map));
199084f9bd12SAlex Elder }
199184f9bd12SAlex Elder 
199284f9bd12SAlex Elder /* Returns a bitmask of endpoints that support filtering, or 0 on error */
199384f9bd12SAlex Elder u32 ipa_endpoint_init(struct ipa *ipa, u32 count,
199484f9bd12SAlex Elder 		      const struct ipa_gsi_endpoint_data *data)
199584f9bd12SAlex Elder {
199684f9bd12SAlex Elder 	enum ipa_endpoint_name name;
199784f9bd12SAlex Elder 	u32 filter_map;
199884f9bd12SAlex Elder 
19999654d8c4SAlex Elder 	BUILD_BUG_ON(!IPA_REPLENISH_BATCH);
20009654d8c4SAlex Elder 
200184f9bd12SAlex Elder 	if (!ipa_endpoint_data_valid(ipa, count, data))
200284f9bd12SAlex Elder 		return 0;	/* Error */
200384f9bd12SAlex Elder 
200484f9bd12SAlex Elder 	ipa->initialized = 0;
200584f9bd12SAlex Elder 
200684f9bd12SAlex Elder 	filter_map = 0;
200784f9bd12SAlex Elder 	for (name = 0; name < count; name++, data++) {
200884f9bd12SAlex Elder 		if (ipa_gsi_endpoint_data_empty(data))
200984f9bd12SAlex Elder 			continue;	/* Skip over empty slots */
201084f9bd12SAlex Elder 
201184f9bd12SAlex Elder 		ipa_endpoint_init_one(ipa, name, data);
201284f9bd12SAlex Elder 
201384f9bd12SAlex Elder 		if (data->endpoint.filter_support)
201484f9bd12SAlex Elder 			filter_map |= BIT(data->endpoint_id);
20152091c79aSAlex Elder 		if (data->ee_id == GSI_EE_MODEM && data->toward_ipa)
20162091c79aSAlex Elder 			ipa->modem_tx_count++;
201784f9bd12SAlex Elder 	}
201884f9bd12SAlex Elder 
201984f9bd12SAlex Elder 	if (!ipa_filter_map_valid(ipa, filter_map))
202084f9bd12SAlex Elder 		goto err_endpoint_exit;
202184f9bd12SAlex Elder 
202284f9bd12SAlex Elder 	return filter_map;	/* Non-zero bitmask */
202384f9bd12SAlex Elder 
202484f9bd12SAlex Elder err_endpoint_exit:
202584f9bd12SAlex Elder 	ipa_endpoint_exit(ipa);
202684f9bd12SAlex Elder 
202784f9bd12SAlex Elder 	return 0;	/* Error */
202884f9bd12SAlex Elder }
2029