xref: /linux/drivers/net/ipa/ipa_endpoint.c (revision beb90cba607ff060c325e6717d2d5e7ff58abf11)
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 */
144*beb90cbaSAlex Elder 			if (rx_config->aggr_time_limit) {
145*beb90cbaSAlex Elder 				dev_err(dev,
146*beb90cbaSAlex Elder 					"time limit with no aggregation for RX endpoint %u\n",
147*beb90cbaSAlex Elder 					data->endpoint_id);
148*beb90cbaSAlex Elder 				result = false;
149*beb90cbaSAlex Elder 			}
150*beb90cbaSAlex 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 
18584f9bd12SAlex Elder 	if (data->endpoint.config.status_enable) {
18684f9bd12SAlex Elder 		other_name = data->endpoint.config.tx.status_endpoint;
18784f9bd12SAlex Elder 		if (other_name >= count) {
18884f9bd12SAlex Elder 			dev_err(dev, "status endpoint name %u out of range "
18984f9bd12SAlex Elder 					"for endpoint %u\n",
19084f9bd12SAlex Elder 				other_name, data->endpoint_id);
19184f9bd12SAlex Elder 			return false;
19284f9bd12SAlex Elder 		}
19384f9bd12SAlex Elder 
19484f9bd12SAlex Elder 		/* Status endpoint must be defined... */
19584f9bd12SAlex Elder 		other_data = &all_data[other_name];
19684f9bd12SAlex Elder 		if (ipa_gsi_endpoint_data_empty(other_data)) {
19784f9bd12SAlex Elder 			dev_err(dev, "DMA endpoint name %u undefined "
19884f9bd12SAlex Elder 					"for endpoint %u\n",
19984f9bd12SAlex Elder 				other_name, data->endpoint_id);
20084f9bd12SAlex Elder 			return false;
20184f9bd12SAlex Elder 		}
20284f9bd12SAlex Elder 
20384f9bd12SAlex Elder 		/* ...and has to be an RX endpoint... */
20484f9bd12SAlex Elder 		if (other_data->toward_ipa) {
20584f9bd12SAlex Elder 			dev_err(dev,
20684f9bd12SAlex Elder 				"status endpoint for endpoint %u not RX\n",
20784f9bd12SAlex Elder 				data->endpoint_id);
20884f9bd12SAlex Elder 			return false;
20984f9bd12SAlex Elder 		}
21084f9bd12SAlex Elder 
21184f9bd12SAlex Elder 		/* ...and if it's to be an AP endpoint... */
21284f9bd12SAlex Elder 		if (other_data->ee_id == GSI_EE_AP) {
21384f9bd12SAlex Elder 			/* ...make sure it has status enabled. */
21484f9bd12SAlex Elder 			if (!other_data->endpoint.config.status_enable) {
21584f9bd12SAlex Elder 				dev_err(dev,
21684f9bd12SAlex Elder 					"status not enabled for endpoint %u\n",
21784f9bd12SAlex Elder 					other_data->endpoint_id);
21884f9bd12SAlex Elder 				return false;
21984f9bd12SAlex Elder 			}
22084f9bd12SAlex Elder 		}
22184f9bd12SAlex Elder 	}
22284f9bd12SAlex Elder 
22384f9bd12SAlex Elder 	if (data->endpoint.config.dma_mode) {
22484f9bd12SAlex Elder 		other_name = data->endpoint.config.dma_endpoint;
22584f9bd12SAlex Elder 		if (other_name >= count) {
22684f9bd12SAlex Elder 			dev_err(dev, "DMA endpoint name %u out of range "
22784f9bd12SAlex Elder 					"for endpoint %u\n",
22884f9bd12SAlex Elder 				other_name, data->endpoint_id);
22984f9bd12SAlex Elder 			return false;
23084f9bd12SAlex Elder 		}
23184f9bd12SAlex Elder 
23284f9bd12SAlex Elder 		other_data = &all_data[other_name];
23384f9bd12SAlex Elder 		if (ipa_gsi_endpoint_data_empty(other_data)) {
23484f9bd12SAlex Elder 			dev_err(dev, "DMA endpoint name %u undefined "
23584f9bd12SAlex Elder 					"for endpoint %u\n",
23684f9bd12SAlex Elder 				other_name, data->endpoint_id);
23784f9bd12SAlex Elder 			return false;
23884f9bd12SAlex Elder 		}
23984f9bd12SAlex Elder 	}
24084f9bd12SAlex Elder 
24184f9bd12SAlex Elder 	return true;
24284f9bd12SAlex Elder }
24384f9bd12SAlex Elder 
24484f9bd12SAlex Elder static bool ipa_endpoint_data_valid(struct ipa *ipa, u32 count,
24584f9bd12SAlex Elder 				    const struct ipa_gsi_endpoint_data *data)
24684f9bd12SAlex Elder {
24784f9bd12SAlex Elder 	const struct ipa_gsi_endpoint_data *dp = data;
24884f9bd12SAlex Elder 	struct device *dev = &ipa->pdev->dev;
24984f9bd12SAlex Elder 	enum ipa_endpoint_name name;
25084f9bd12SAlex Elder 
25184f9bd12SAlex Elder 	if (count > IPA_ENDPOINT_COUNT) {
25284f9bd12SAlex Elder 		dev_err(dev, "too many endpoints specified (%u > %u)\n",
25384f9bd12SAlex Elder 			count, IPA_ENDPOINT_COUNT);
25484f9bd12SAlex Elder 		return false;
25584f9bd12SAlex Elder 	}
25684f9bd12SAlex Elder 
25784f9bd12SAlex Elder 	/* Make sure needed endpoints have defined data */
25884f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_COMMAND_TX])) {
25984f9bd12SAlex Elder 		dev_err(dev, "command TX endpoint not defined\n");
26084f9bd12SAlex Elder 		return false;
26184f9bd12SAlex Elder 	}
26284f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_LAN_RX])) {
26384f9bd12SAlex Elder 		dev_err(dev, "LAN RX endpoint not defined\n");
26484f9bd12SAlex Elder 		return false;
26584f9bd12SAlex Elder 	}
26684f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_MODEM_TX])) {
26784f9bd12SAlex Elder 		dev_err(dev, "AP->modem TX endpoint not defined\n");
26884f9bd12SAlex Elder 		return false;
26984f9bd12SAlex Elder 	}
27084f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_MODEM_RX])) {
27184f9bd12SAlex Elder 		dev_err(dev, "AP<-modem RX endpoint not defined\n");
27284f9bd12SAlex Elder 		return false;
27384f9bd12SAlex Elder 	}
27484f9bd12SAlex Elder 
27584f9bd12SAlex Elder 	for (name = 0; name < count; name++, dp++)
27684f9bd12SAlex Elder 		if (!ipa_endpoint_data_valid_one(ipa, count, data, dp))
27784f9bd12SAlex Elder 			return false;
27884f9bd12SAlex Elder 
27984f9bd12SAlex Elder 	return true;
28084f9bd12SAlex Elder }
28184f9bd12SAlex Elder 
28284f9bd12SAlex Elder /* Allocate a transaction to use on a non-command endpoint */
28384f9bd12SAlex Elder static struct gsi_trans *ipa_endpoint_trans_alloc(struct ipa_endpoint *endpoint,
28484f9bd12SAlex Elder 						  u32 tre_count)
28584f9bd12SAlex Elder {
28684f9bd12SAlex Elder 	struct gsi *gsi = &endpoint->ipa->gsi;
28784f9bd12SAlex Elder 	u32 channel_id = endpoint->channel_id;
28884f9bd12SAlex Elder 	enum dma_data_direction direction;
28984f9bd12SAlex Elder 
29084f9bd12SAlex Elder 	direction = endpoint->toward_ipa ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
29184f9bd12SAlex Elder 
29284f9bd12SAlex Elder 	return gsi_channel_trans_alloc(gsi, channel_id, tre_count, direction);
29384f9bd12SAlex Elder }
29484f9bd12SAlex Elder 
29584f9bd12SAlex Elder /* suspend_delay represents suspend for RX, delay for TX endpoints.
2964c9d631aSAlex Elder  * Note that suspend is not supported starting with IPA v4.0, and
2974c9d631aSAlex Elder  * delay mode should not be used starting with IPA v4.2.
29884f9bd12SAlex Elder  */
2994900bf34SAlex Elder static bool
30084f9bd12SAlex Elder ipa_endpoint_init_ctrl(struct ipa_endpoint *endpoint, bool suspend_delay)
30184f9bd12SAlex Elder {
30284f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_CTRL_N_OFFSET(endpoint->endpoint_id);
30384f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
3044900bf34SAlex Elder 	bool state;
30584f9bd12SAlex Elder 	u32 mask;
30684f9bd12SAlex Elder 	u32 val;
30784f9bd12SAlex Elder 
3085bc55884SAlex Elder 	if (endpoint->toward_ipa)
3094c9d631aSAlex Elder 		WARN_ON(ipa->version >= IPA_VERSION_4_2);
3105bc55884SAlex Elder 	else
3115bc55884SAlex Elder 		WARN_ON(ipa->version >= IPA_VERSION_4_0);
3125bc55884SAlex Elder 
31384f9bd12SAlex Elder 	mask = endpoint->toward_ipa ? ENDP_DELAY_FMASK : ENDP_SUSPEND_FMASK;
31484f9bd12SAlex Elder 
31584f9bd12SAlex Elder 	val = ioread32(ipa->reg_virt + offset);
3164900bf34SAlex Elder 	state = !!(val & mask);
3175bc55884SAlex Elder 
3185bc55884SAlex Elder 	/* Don't bother if it's already in the requested state */
3194900bf34SAlex Elder 	if (suspend_delay != state) {
32084f9bd12SAlex Elder 		val ^= mask;
32184f9bd12SAlex Elder 		iowrite32(val, ipa->reg_virt + offset);
3224900bf34SAlex Elder 	}
32384f9bd12SAlex Elder 
3244900bf34SAlex Elder 	return state;
32584f9bd12SAlex Elder }
32684f9bd12SAlex Elder 
3274c9d631aSAlex Elder /* We don't care what the previous state was for delay mode */
3284fa95248SAlex Elder static void
3294fa95248SAlex Elder ipa_endpoint_program_delay(struct ipa_endpoint *endpoint, bool enable)
3304fa95248SAlex Elder {
3314c9d631aSAlex Elder 	/* Delay mode should not be used for IPA v4.2+ */
3324c9d631aSAlex Elder 	WARN_ON(endpoint->ipa->version >= IPA_VERSION_4_2);
3335bc55884SAlex Elder 	WARN_ON(!endpoint->toward_ipa);
3344fa95248SAlex Elder 
3354fa95248SAlex Elder 	(void)ipa_endpoint_init_ctrl(endpoint, enable);
3364fa95248SAlex Elder }
3374fa95248SAlex Elder 
338fff89971SAlex Elder static bool ipa_endpoint_aggr_active(struct ipa_endpoint *endpoint)
339fff89971SAlex Elder {
340fff89971SAlex Elder 	u32 mask = BIT(endpoint->endpoint_id);
341fff89971SAlex Elder 	struct ipa *ipa = endpoint->ipa;
342fff89971SAlex Elder 	u32 offset;
343fff89971SAlex Elder 	u32 val;
344fff89971SAlex Elder 
3455bc55884SAlex Elder 	WARN_ON(!(mask & ipa->available));
3465bc55884SAlex Elder 
347fff89971SAlex Elder 	offset = ipa_reg_state_aggr_active_offset(ipa->version);
348fff89971SAlex Elder 	val = ioread32(ipa->reg_virt + offset);
349fff89971SAlex Elder 
350fff89971SAlex Elder 	return !!(val & mask);
351fff89971SAlex Elder }
352fff89971SAlex Elder 
353fff89971SAlex Elder static void ipa_endpoint_force_close(struct ipa_endpoint *endpoint)
354fff89971SAlex Elder {
355fff89971SAlex Elder 	u32 mask = BIT(endpoint->endpoint_id);
356fff89971SAlex Elder 	struct ipa *ipa = endpoint->ipa;
357fff89971SAlex Elder 
3585bc55884SAlex Elder 	WARN_ON(!(mask & ipa->available));
3595bc55884SAlex Elder 
360fff89971SAlex Elder 	iowrite32(mask, ipa->reg_virt + IPA_REG_AGGR_FORCE_CLOSE_OFFSET);
361fff89971SAlex Elder }
362fff89971SAlex Elder 
363fff89971SAlex Elder /**
364fff89971SAlex Elder  * ipa_endpoint_suspend_aggr() - Emulate suspend interrupt
365e3eea08eSAlex Elder  * @endpoint:	Endpoint on which to emulate a suspend
366fff89971SAlex Elder  *
367fff89971SAlex Elder  *  Emulate suspend IPA interrupt to unsuspend an endpoint suspended
368fff89971SAlex Elder  *  with an open aggregation frame.  This is to work around a hardware
369fff89971SAlex Elder  *  issue in IPA version 3.5.1 where the suspend interrupt will not be
370fff89971SAlex Elder  *  generated when it should be.
371fff89971SAlex Elder  */
372fff89971SAlex Elder static void ipa_endpoint_suspend_aggr(struct ipa_endpoint *endpoint)
373fff89971SAlex Elder {
374fff89971SAlex Elder 	struct ipa *ipa = endpoint->ipa;
375fff89971SAlex Elder 
376660e52d6SAlex Elder 	if (!endpoint->config.aggregation)
377fff89971SAlex Elder 		return;
378fff89971SAlex Elder 
379fff89971SAlex Elder 	/* Nothing to do if the endpoint doesn't have aggregation open */
380fff89971SAlex Elder 	if (!ipa_endpoint_aggr_active(endpoint))
381fff89971SAlex Elder 		return;
382fff89971SAlex Elder 
383fff89971SAlex Elder 	/* Force close aggregation */
384fff89971SAlex Elder 	ipa_endpoint_force_close(endpoint);
385fff89971SAlex Elder 
386fff89971SAlex Elder 	ipa_interrupt_simulate_suspend(ipa->interrupt);
387fff89971SAlex Elder }
388fff89971SAlex Elder 
389fff89971SAlex Elder /* Returns previous suspend state (true means suspend was enabled) */
3904fa95248SAlex Elder static bool
3914fa95248SAlex Elder ipa_endpoint_program_suspend(struct ipa_endpoint *endpoint, bool enable)
3924fa95248SAlex Elder {
393fff89971SAlex Elder 	bool suspended;
394fff89971SAlex Elder 
395d7f3087bSAlex Elder 	if (endpoint->ipa->version >= IPA_VERSION_4_0)
396b07f283eSAlex Elder 		return enable;	/* For IPA v4.0+, no change made */
397b07f283eSAlex Elder 
3985bc55884SAlex Elder 	WARN_ON(endpoint->toward_ipa);
3994fa95248SAlex Elder 
400fff89971SAlex Elder 	suspended = ipa_endpoint_init_ctrl(endpoint, enable);
401fff89971SAlex Elder 
402fff89971SAlex Elder 	/* A client suspended with an open aggregation frame will not
403fff89971SAlex Elder 	 * generate a SUSPEND IPA interrupt.  If enabling suspend, have
404fff89971SAlex Elder 	 * ipa_endpoint_suspend_aggr() handle this.
405fff89971SAlex Elder 	 */
406fff89971SAlex Elder 	if (enable && !suspended)
407fff89971SAlex Elder 		ipa_endpoint_suspend_aggr(endpoint);
408fff89971SAlex Elder 
409fff89971SAlex Elder 	return suspended;
4104fa95248SAlex Elder }
4114fa95248SAlex Elder 
4124c9d631aSAlex Elder /* Put all modem RX endpoints into suspend mode, and stop transmission
4134c9d631aSAlex Elder  * on all modem TX endpoints.  Prior to IPA v4.2, endpoint DELAY mode is
4144c9d631aSAlex Elder  * used for TX endpoints; starting with IPA v4.2 we use GSI channel flow
4154c9d631aSAlex Elder  * control instead.
4164c9d631aSAlex Elder  */
41784f9bd12SAlex Elder void ipa_endpoint_modem_pause_all(struct ipa *ipa, bool enable)
41884f9bd12SAlex Elder {
41984f9bd12SAlex Elder 	u32 endpoint_id;
42084f9bd12SAlex Elder 
42184f9bd12SAlex Elder 	for (endpoint_id = 0; endpoint_id < IPA_ENDPOINT_MAX; endpoint_id++) {
42284f9bd12SAlex Elder 		struct ipa_endpoint *endpoint = &ipa->endpoint[endpoint_id];
42384f9bd12SAlex Elder 
42484f9bd12SAlex Elder 		if (endpoint->ee_id != GSI_EE_MODEM)
42584f9bd12SAlex Elder 			continue;
42684f9bd12SAlex Elder 
4274c9d631aSAlex Elder 		if (!endpoint->toward_ipa)
4284c9d631aSAlex Elder 			(void)ipa_endpoint_program_suspend(endpoint, enable);
4294c9d631aSAlex Elder 		else if (ipa->version < IPA_VERSION_4_2)
4304fa95248SAlex Elder 			ipa_endpoint_program_delay(endpoint, enable);
431b07f283eSAlex Elder 		else
4324c9d631aSAlex Elder 			gsi_modem_channel_flow_control(&ipa->gsi,
4334c9d631aSAlex Elder 						       endpoint->channel_id,
4344c9d631aSAlex Elder 						       enable);
43584f9bd12SAlex Elder 	}
43684f9bd12SAlex Elder }
43784f9bd12SAlex Elder 
43884f9bd12SAlex Elder /* Reset all modem endpoints to use the default exception endpoint */
43984f9bd12SAlex Elder int ipa_endpoint_modem_exception_reset_all(struct ipa *ipa)
44084f9bd12SAlex Elder {
44184f9bd12SAlex Elder 	u32 initialized = ipa->initialized;
44284f9bd12SAlex Elder 	struct gsi_trans *trans;
44384f9bd12SAlex Elder 	u32 count;
44484f9bd12SAlex Elder 
44584f9bd12SAlex Elder 	/* We need one command per modem TX endpoint.  We can get an upper
44684f9bd12SAlex Elder 	 * bound on that by assuming all initialized endpoints are modem->IPA.
44784f9bd12SAlex Elder 	 * That won't happen, and we could be more precise, but this is fine
448602a1c76SAlex Elder 	 * for now.  End the transaction with commands to clear the pipeline.
44984f9bd12SAlex Elder 	 */
450aa56e3e5SAlex Elder 	count = hweight32(initialized) + ipa_cmd_pipeline_clear_count();
45184f9bd12SAlex Elder 	trans = ipa_cmd_trans_alloc(ipa, count);
45284f9bd12SAlex Elder 	if (!trans) {
45384f9bd12SAlex Elder 		dev_err(&ipa->pdev->dev,
45484f9bd12SAlex Elder 			"no transaction to reset modem exception endpoints\n");
45584f9bd12SAlex Elder 		return -EBUSY;
45684f9bd12SAlex Elder 	}
45784f9bd12SAlex Elder 
45884f9bd12SAlex Elder 	while (initialized) {
45984f9bd12SAlex Elder 		u32 endpoint_id = __ffs(initialized);
46084f9bd12SAlex Elder 		struct ipa_endpoint *endpoint;
46184f9bd12SAlex Elder 		u32 offset;
46284f9bd12SAlex Elder 
46384f9bd12SAlex Elder 		initialized ^= BIT(endpoint_id);
46484f9bd12SAlex Elder 
46584f9bd12SAlex Elder 		/* We only reset modem TX endpoints */
46684f9bd12SAlex Elder 		endpoint = &ipa->endpoint[endpoint_id];
46784f9bd12SAlex Elder 		if (!(endpoint->ee_id == GSI_EE_MODEM && endpoint->toward_ipa))
46884f9bd12SAlex Elder 			continue;
46984f9bd12SAlex Elder 
47084f9bd12SAlex Elder 		offset = IPA_REG_ENDP_STATUS_N_OFFSET(endpoint_id);
47184f9bd12SAlex Elder 
47284f9bd12SAlex Elder 		/* Value written is 0, and all bits are updated.  That
47384f9bd12SAlex Elder 		 * means status is disabled on the endpoint, and as a
47484f9bd12SAlex Elder 		 * result all other fields in the register are ignored.
47584f9bd12SAlex Elder 		 */
47684f9bd12SAlex Elder 		ipa_cmd_register_write_add(trans, offset, 0, ~0, false);
47784f9bd12SAlex Elder 	}
47884f9bd12SAlex Elder 
479aa56e3e5SAlex Elder 	ipa_cmd_pipeline_clear_add(trans);
48084f9bd12SAlex Elder 
48184f9bd12SAlex Elder 	/* XXX This should have a 1 second timeout */
48284f9bd12SAlex Elder 	gsi_trans_commit_wait(trans);
48384f9bd12SAlex Elder 
48451c48ce2SAlex Elder 	ipa_cmd_pipeline_clear_wait(ipa);
48551c48ce2SAlex Elder 
48684f9bd12SAlex Elder 	return 0;
48784f9bd12SAlex Elder }
48884f9bd12SAlex Elder 
48984f9bd12SAlex Elder static void ipa_endpoint_init_cfg(struct ipa_endpoint *endpoint)
49084f9bd12SAlex Elder {
49184f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_CFG_N_OFFSET(endpoint->endpoint_id);
4925567d4d9SAlex Elder 	enum ipa_cs_offload_en enabled;
49384f9bd12SAlex Elder 	u32 val = 0;
49484f9bd12SAlex Elder 
49584f9bd12SAlex Elder 	/* FRAG_OFFLOAD_EN is 0 */
496660e52d6SAlex Elder 	if (endpoint->config.checksum) {
4975567d4d9SAlex Elder 		enum ipa_version version = endpoint->ipa->version;
4985567d4d9SAlex Elder 
49984f9bd12SAlex Elder 		if (endpoint->toward_ipa) {
50084f9bd12SAlex Elder 			u32 checksum_offset;
50184f9bd12SAlex Elder 
50284f9bd12SAlex Elder 			/* Checksum header offset is in 4-byte units */
50384f9bd12SAlex Elder 			checksum_offset = sizeof(struct rmnet_map_header);
50484f9bd12SAlex Elder 			checksum_offset /= sizeof(u32);
50584f9bd12SAlex Elder 			val |= u32_encode_bits(checksum_offset,
50684f9bd12SAlex Elder 					       CS_METADATA_HDR_OFFSET_FMASK);
5075567d4d9SAlex Elder 
5085567d4d9SAlex Elder 			enabled = version < IPA_VERSION_4_5
5095567d4d9SAlex Elder 					? IPA_CS_OFFLOAD_UL
5105567d4d9SAlex Elder 					: IPA_CS_OFFLOAD_INLINE;
51184f9bd12SAlex Elder 		} else {
5125567d4d9SAlex Elder 			enabled = version < IPA_VERSION_4_5
5135567d4d9SAlex Elder 					? IPA_CS_OFFLOAD_DL
5145567d4d9SAlex Elder 					: IPA_CS_OFFLOAD_INLINE;
51584f9bd12SAlex Elder 		}
51684f9bd12SAlex Elder 	} else {
5175567d4d9SAlex Elder 		enabled = IPA_CS_OFFLOAD_NONE;
51884f9bd12SAlex Elder 	}
5195567d4d9SAlex Elder 	val |= u32_encode_bits(enabled, CS_OFFLOAD_EN_FMASK);
52084f9bd12SAlex Elder 	/* CS_GEN_QMB_MASTER_SEL is 0 */
52184f9bd12SAlex Elder 
52284f9bd12SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
52384f9bd12SAlex Elder }
52484f9bd12SAlex Elder 
525647a05f3SAlex Elder static void ipa_endpoint_init_nat(struct ipa_endpoint *endpoint)
526647a05f3SAlex Elder {
527647a05f3SAlex Elder 	u32 offset;
528647a05f3SAlex Elder 	u32 val;
529647a05f3SAlex Elder 
530647a05f3SAlex Elder 	if (!endpoint->toward_ipa)
531647a05f3SAlex Elder 		return;
532647a05f3SAlex Elder 
533647a05f3SAlex Elder 	offset = IPA_REG_ENDP_INIT_NAT_N_OFFSET(endpoint->endpoint_id);
534647a05f3SAlex Elder 	val = u32_encode_bits(IPA_NAT_BYPASS, NAT_EN_FMASK);
535647a05f3SAlex Elder 
536647a05f3SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
537647a05f3SAlex Elder }
538647a05f3SAlex Elder 
5395567d4d9SAlex Elder static u32
5405567d4d9SAlex Elder ipa_qmap_header_size(enum ipa_version version, struct ipa_endpoint *endpoint)
5415567d4d9SAlex Elder {
5425567d4d9SAlex Elder 	u32 header_size = sizeof(struct rmnet_map_header);
5435567d4d9SAlex Elder 
5445567d4d9SAlex Elder 	/* Without checksum offload, we just have the MAP header */
545660e52d6SAlex Elder 	if (!endpoint->config.checksum)
5465567d4d9SAlex Elder 		return header_size;
5475567d4d9SAlex Elder 
5485567d4d9SAlex Elder 	if (version < IPA_VERSION_4_5) {
5495567d4d9SAlex Elder 		/* Checksum header inserted for AP TX endpoints only */
5505567d4d9SAlex Elder 		if (endpoint->toward_ipa)
5515567d4d9SAlex Elder 			header_size += sizeof(struct rmnet_map_ul_csum_header);
5525567d4d9SAlex Elder 	} else {
5535567d4d9SAlex Elder 		/* Checksum header is used in both directions */
5545567d4d9SAlex Elder 		header_size += sizeof(struct rmnet_map_v5_csum_header);
5555567d4d9SAlex Elder 	}
5565567d4d9SAlex Elder 
5575567d4d9SAlex Elder 	return header_size;
5585567d4d9SAlex Elder }
5595567d4d9SAlex Elder 
5608730f45dSAlex Elder /**
561e3eea08eSAlex Elder  * ipa_endpoint_init_hdr() - Initialize HDR endpoint configuration register
562e3eea08eSAlex Elder  * @endpoint:	Endpoint pointer
563e3eea08eSAlex Elder  *
5648730f45dSAlex Elder  * We program QMAP endpoints so each packet received is preceded by a QMAP
5658730f45dSAlex Elder  * header structure.  The QMAP header contains a 1-byte mux_id and 2-byte
5668730f45dSAlex Elder  * packet size field, and we have the IPA hardware populate both for each
5678730f45dSAlex Elder  * received packet.  The header is configured (in the HDR_EXT register)
5688730f45dSAlex Elder  * to use big endian format.
5698730f45dSAlex Elder  *
5708730f45dSAlex Elder  * The packet size is written into the QMAP header's pkt_len field.  That
5718730f45dSAlex Elder  * location is defined here using the HDR_OFST_PKT_SIZE field.
5728730f45dSAlex Elder  *
5738730f45dSAlex Elder  * The mux_id comes from a 4-byte metadata value supplied with each packet
5748730f45dSAlex Elder  * by the modem.  It is *not* a QMAP header, but it does contain the mux_id
5758730f45dSAlex Elder  * value that we want, in its low-order byte.  A bitmask defined in the
5768730f45dSAlex Elder  * endpoint's METADATA_MASK register defines which byte within the modem
5778730f45dSAlex Elder  * metadata contains the mux_id.  And the OFST_METADATA field programmed
5788730f45dSAlex Elder  * here indicates where the extracted byte should be placed within the QMAP
5798730f45dSAlex Elder  * header.
5808730f45dSAlex Elder  */
58184f9bd12SAlex Elder static void ipa_endpoint_init_hdr(struct ipa_endpoint *endpoint)
58284f9bd12SAlex Elder {
58384f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_HDR_N_OFFSET(endpoint->endpoint_id);
5841af15c2aSAlex Elder 	struct ipa *ipa = endpoint->ipa;
58584f9bd12SAlex Elder 	u32 val = 0;
58684f9bd12SAlex Elder 
587660e52d6SAlex Elder 	if (endpoint->config.qmap) {
5881af15c2aSAlex Elder 		enum ipa_version version = ipa->version;
5895567d4d9SAlex Elder 		size_t header_size;
59084f9bd12SAlex Elder 
5915567d4d9SAlex Elder 		header_size = ipa_qmap_header_size(version, endpoint);
5925567d4d9SAlex Elder 		val = ipa_header_size_encoded(version, header_size);
59384f9bd12SAlex Elder 
594f330fda3SAlex Elder 		/* Define how to fill fields in a received QMAP header */
5958730f45dSAlex Elder 		if (!endpoint->toward_ipa) {
5961af15c2aSAlex Elder 			u32 offset;	/* Field offset within header */
5978730f45dSAlex Elder 
5988730f45dSAlex Elder 			/* Where IPA will write the metadata value */
5991af15c2aSAlex Elder 			offset = offsetof(struct rmnet_map_header, mux_id);
6001af15c2aSAlex Elder 			val |= ipa_metadata_offset_encoded(version, offset);
6018730f45dSAlex Elder 
6028730f45dSAlex Elder 			/* Where IPA will write the length */
6031af15c2aSAlex Elder 			offset = offsetof(struct rmnet_map_header, pkt_len);
6041af15c2aSAlex Elder 			/* Upper bits are stored in HDR_EXT with IPA v4.5 */
605d7f3087bSAlex Elder 			if (version >= IPA_VERSION_4_5)
6061af15c2aSAlex Elder 				offset &= field_mask(HDR_OFST_PKT_SIZE_FMASK);
6071af15c2aSAlex Elder 
60884f9bd12SAlex Elder 			val |= HDR_OFST_PKT_SIZE_VALID_FMASK;
6091af15c2aSAlex Elder 			val |= u32_encode_bits(offset, HDR_OFST_PKT_SIZE_FMASK);
61084f9bd12SAlex Elder 		}
6118730f45dSAlex Elder 		/* For QMAP TX, metadata offset is 0 (modem assumes this) */
6128730f45dSAlex Elder 		val |= HDR_OFST_METADATA_VALID_FMASK;
6138730f45dSAlex Elder 
6148730f45dSAlex Elder 		/* HDR_ADDITIONAL_CONST_LEN is 0; (RX only) */
61584f9bd12SAlex Elder 		/* HDR_A5_MUX is 0 */
61684f9bd12SAlex Elder 		/* HDR_LEN_INC_DEAGG_HDR is 0 */
6178bfc4e21SAlex Elder 		/* HDR_METADATA_REG_VALID is 0 (TX only, version < v4.5) */
61884f9bd12SAlex Elder 	}
61984f9bd12SAlex Elder 
6201af15c2aSAlex Elder 	iowrite32(val, ipa->reg_virt + offset);
62184f9bd12SAlex Elder }
62284f9bd12SAlex Elder 
62384f9bd12SAlex Elder static void ipa_endpoint_init_hdr_ext(struct ipa_endpoint *endpoint)
62484f9bd12SAlex Elder {
62584f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_HDR_EXT_N_OFFSET(endpoint->endpoint_id);
626660e52d6SAlex Elder 	u32 pad_align = endpoint->config.rx.pad_align;
6271af15c2aSAlex Elder 	struct ipa *ipa = endpoint->ipa;
62884f9bd12SAlex Elder 	u32 val = 0;
62984f9bd12SAlex Elder 
630660e52d6SAlex Elder 	if (endpoint->config.qmap) {
631332ef7c8SAlex Elder 		/* We have a header, so we must specify its endianness */
63284f9bd12SAlex Elder 		val |= HDR_ENDIANNESS_FMASK;	/* big endian */
633f330fda3SAlex Elder 
634332ef7c8SAlex Elder 		/* A QMAP header contains a 6 bit pad field at offset 0.
635332ef7c8SAlex Elder 		 * The RMNet driver assumes this field is meaningful in
636332ef7c8SAlex Elder 		 * packets it receives, and assumes the header's payload
637332ef7c8SAlex Elder 		 * length includes that padding.  The RMNet driver does
638332ef7c8SAlex Elder 		 * *not* pad packets it sends, however, so the pad field
639332ef7c8SAlex Elder 		 * (although 0) should be ignored.
640f330fda3SAlex Elder 		 */
641332ef7c8SAlex Elder 		if (!endpoint->toward_ipa) {
64284f9bd12SAlex Elder 			val |= HDR_TOTAL_LEN_OR_PAD_VALID_FMASK;
64384f9bd12SAlex Elder 			/* HDR_TOTAL_LEN_OR_PAD is 0 (pad, not total_len) */
644f330fda3SAlex Elder 			val |= HDR_PAYLOAD_LEN_INC_PADDING_FMASK;
64584f9bd12SAlex Elder 			/* HDR_TOTAL_LEN_OR_PAD_OFFSET is 0 */
646f330fda3SAlex Elder 		}
647332ef7c8SAlex Elder 	}
648f330fda3SAlex Elder 
649f330fda3SAlex Elder 	/* HDR_PAYLOAD_LEN_INC_PADDING is 0 */
65084f9bd12SAlex Elder 	if (!endpoint->toward_ipa)
65184f9bd12SAlex Elder 		val |= u32_encode_bits(pad_align, HDR_PAD_TO_ALIGNMENT_FMASK);
65284f9bd12SAlex Elder 
6531af15c2aSAlex Elder 	/* IPA v4.5 adds some most-significant bits to a few fields,
6541af15c2aSAlex Elder 	 * two of which are defined in the HDR (not HDR_EXT) register.
6551af15c2aSAlex Elder 	 */
656d7f3087bSAlex Elder 	if (ipa->version >= IPA_VERSION_4_5) {
6571af15c2aSAlex Elder 		/* HDR_TOTAL_LEN_OR_PAD_OFFSET is 0, so MSB is 0 */
658660e52d6SAlex Elder 		if (endpoint->config.qmap && !endpoint->toward_ipa) {
6591af15c2aSAlex Elder 			u32 offset;
66084f9bd12SAlex Elder 
6611af15c2aSAlex Elder 			offset = offsetof(struct rmnet_map_header, pkt_len);
6621af15c2aSAlex Elder 			offset >>= hweight32(HDR_OFST_PKT_SIZE_FMASK);
6631af15c2aSAlex Elder 			val |= u32_encode_bits(offset,
6641af15c2aSAlex Elder 					       HDR_OFST_PKT_SIZE_MSB_FMASK);
6651af15c2aSAlex Elder 			/* HDR_ADDITIONAL_CONST_LEN is 0 so MSB is 0 */
6661af15c2aSAlex Elder 		}
6671af15c2aSAlex Elder 	}
6681af15c2aSAlex Elder 	iowrite32(val, ipa->reg_virt + offset);
6691af15c2aSAlex Elder }
67084f9bd12SAlex Elder 
67184f9bd12SAlex Elder static void ipa_endpoint_init_hdr_metadata_mask(struct ipa_endpoint *endpoint)
67284f9bd12SAlex Elder {
67384f9bd12SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
67484f9bd12SAlex Elder 	u32 val = 0;
67584f9bd12SAlex Elder 	u32 offset;
67684f9bd12SAlex Elder 
677fb57c3eaSAlex Elder 	if (endpoint->toward_ipa)
678fb57c3eaSAlex Elder 		return;		/* Register not valid for TX endpoints */
679fb57c3eaSAlex Elder 
68084f9bd12SAlex Elder 	offset = IPA_REG_ENDP_INIT_HDR_METADATA_MASK_N_OFFSET(endpoint_id);
68184f9bd12SAlex Elder 
6828730f45dSAlex Elder 	/* Note that HDR_ENDIANNESS indicates big endian header fields */
683660e52d6SAlex Elder 	if (endpoint->config.qmap)
684088f8a23SAlex Elder 		val = (__force u32)cpu_to_be32(IPA_ENDPOINT_QMAP_METADATA_MASK);
68584f9bd12SAlex Elder 
68684f9bd12SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
68784f9bd12SAlex Elder }
68884f9bd12SAlex Elder 
68984f9bd12SAlex Elder static void ipa_endpoint_init_mode(struct ipa_endpoint *endpoint)
69084f9bd12SAlex Elder {
69184f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_MODE_N_OFFSET(endpoint->endpoint_id);
69284f9bd12SAlex Elder 	u32 val;
69384f9bd12SAlex Elder 
694fb57c3eaSAlex Elder 	if (!endpoint->toward_ipa)
695fb57c3eaSAlex Elder 		return;		/* Register not valid for RX endpoints */
696fb57c3eaSAlex Elder 
697660e52d6SAlex Elder 	if (endpoint->config.dma_mode) {
698660e52d6SAlex Elder 		enum ipa_endpoint_name name = endpoint->config.dma_endpoint;
69984f9bd12SAlex Elder 		u32 dma_endpoint_id;
70084f9bd12SAlex Elder 
70184f9bd12SAlex Elder 		dma_endpoint_id = endpoint->ipa->name_map[name]->endpoint_id;
70284f9bd12SAlex Elder 
70384f9bd12SAlex Elder 		val = u32_encode_bits(IPA_DMA, MODE_FMASK);
70484f9bd12SAlex Elder 		val |= u32_encode_bits(dma_endpoint_id, DEST_PIPE_INDEX_FMASK);
70584f9bd12SAlex Elder 	} else {
70684f9bd12SAlex Elder 		val = u32_encode_bits(IPA_BASIC, MODE_FMASK);
70784f9bd12SAlex Elder 	}
70800b9102aSAlex Elder 	/* All other bits unspecified (and 0) */
70984f9bd12SAlex Elder 
71084f9bd12SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
71184f9bd12SAlex Elder }
71284f9bd12SAlex Elder 
7136bf754c7SAlex Elder /* Encoded values for AGGR endpoint register fields */
7146bf754c7SAlex Elder static u32 aggr_byte_limit_encoded(enum ipa_version version, u32 limit)
7156bf754c7SAlex Elder {
7166bf754c7SAlex Elder 	if (version < IPA_VERSION_4_5)
7176bf754c7SAlex Elder 		return u32_encode_bits(limit, aggr_byte_limit_fmask(true));
7186bf754c7SAlex Elder 
7196bf754c7SAlex Elder 	return u32_encode_bits(limit, aggr_byte_limit_fmask(false));
7206bf754c7SAlex Elder }
7216bf754c7SAlex Elder 
72219547041SAlex Elder /* Encode the aggregation timer limit (microseconds) based on IPA version */
7236bf754c7SAlex Elder static u32 aggr_time_limit_encoded(enum ipa_version version, u32 limit)
7246bf754c7SAlex Elder {
72519547041SAlex Elder 	u32 gran_sel;
72619547041SAlex Elder 	u32 fmask;
72719547041SAlex Elder 	u32 val;
7286bf754c7SAlex Elder 
72919547041SAlex Elder 	if (version < IPA_VERSION_4_5) {
73019547041SAlex Elder 		/* We set aggregation granularity in ipa_hardware_config() */
731*beb90cbaSAlex Elder 		fmask = aggr_time_limit_fmask(true);
732*beb90cbaSAlex Elder 		val = DIV_ROUND_CLOSEST(limit, IPA_AGGR_GRANULARITY);
733*beb90cbaSAlex Elder 		WARN(val > field_max(fmask),
734*beb90cbaSAlex Elder 		     "aggr_time_limit too large (%u > %u usec)\n",
735*beb90cbaSAlex Elder 		     val, field_max(fmask) * IPA_AGGR_GRANULARITY);
73619547041SAlex Elder 
737*beb90cbaSAlex Elder 		return u32_encode_bits(val, fmask);
73819547041SAlex Elder 	}
73919547041SAlex Elder 
74019547041SAlex Elder 	/* IPA v4.5 expresses the time limit using Qtime.  The AP has
74119547041SAlex Elder 	 * pulse generators 0 and 1 available, which were configured
74219547041SAlex Elder 	 * in ipa_qtime_config() to have granularity 100 usec and
74319547041SAlex Elder 	 * 1 msec, respectively.  Use pulse generator 0 if possible,
74419547041SAlex Elder 	 * otherwise fall back to pulse generator 1.
74519547041SAlex Elder 	 */
74619547041SAlex Elder 	fmask = aggr_time_limit_fmask(false);
74719547041SAlex Elder 	val = DIV_ROUND_CLOSEST(limit, 100);
74819547041SAlex Elder 	if (val > field_max(fmask)) {
74919547041SAlex Elder 		/* Have to use pulse generator 1 (millisecond granularity) */
75019547041SAlex Elder 		gran_sel = AGGR_GRAN_SEL_FMASK;
75119547041SAlex Elder 		val = DIV_ROUND_CLOSEST(limit, 1000);
752*beb90cbaSAlex Elder 		WARN(val > field_max(fmask),
753*beb90cbaSAlex Elder 		     "aggr_time_limit too large (%u > %u usec)\n",
754*beb90cbaSAlex Elder 		     limit, field_max(fmask) * 1000);
75519547041SAlex Elder 	} else {
75619547041SAlex Elder 		/* We can use pulse generator 0 (100 usec granularity) */
75719547041SAlex Elder 		gran_sel = 0;
75819547041SAlex Elder 	}
75919547041SAlex Elder 
76019547041SAlex Elder 	return gran_sel | u32_encode_bits(val, fmask);
7616bf754c7SAlex Elder }
7626bf754c7SAlex Elder 
7636bf754c7SAlex Elder static u32 aggr_sw_eof_active_encoded(enum ipa_version version, bool enabled)
7646bf754c7SAlex Elder {
7656bf754c7SAlex Elder 	u32 val = enabled ? 1 : 0;
7666bf754c7SAlex Elder 
7676bf754c7SAlex Elder 	if (version < IPA_VERSION_4_5)
7686bf754c7SAlex Elder 		return u32_encode_bits(val, aggr_sw_eof_active_fmask(true));
7696bf754c7SAlex Elder 
7706bf754c7SAlex Elder 	return u32_encode_bits(val, aggr_sw_eof_active_fmask(false));
7716bf754c7SAlex Elder }
7726bf754c7SAlex Elder 
77384f9bd12SAlex Elder static void ipa_endpoint_init_aggr(struct ipa_endpoint *endpoint)
77484f9bd12SAlex Elder {
77584f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_AGGR_N_OFFSET(endpoint->endpoint_id);
7766bf754c7SAlex Elder 	enum ipa_version version = endpoint->ipa->version;
77784f9bd12SAlex Elder 	u32 val = 0;
77884f9bd12SAlex Elder 
779660e52d6SAlex Elder 	if (endpoint->config.aggregation) {
78084f9bd12SAlex Elder 		if (!endpoint->toward_ipa) {
781cf4e73a1SAlex Elder 			const struct ipa_endpoint_rx *rx_config;
782c5794097SAlex Elder 			u32 buffer_size;
7836bf754c7SAlex Elder 			bool close_eof;
78484f9bd12SAlex Elder 			u32 limit;
78584f9bd12SAlex Elder 
786660e52d6SAlex Elder 			rx_config = &endpoint->config.rx;
78784f9bd12SAlex Elder 			val |= u32_encode_bits(IPA_ENABLE_AGGR, AGGR_EN_FMASK);
78884f9bd12SAlex Elder 			val |= u32_encode_bits(IPA_GENERIC, AGGR_TYPE_FMASK);
7899e88cb5fSAlex Elder 
790cf4e73a1SAlex Elder 			buffer_size = rx_config->buffer_size;
7913cebb7c2SAlex Elder 			limit = ipa_aggr_size_kb(buffer_size - NET_SKB_PAD,
7923cebb7c2SAlex Elder 						 rx_config->aggr_hard_limit);
7936bf754c7SAlex Elder 			val |= aggr_byte_limit_encoded(version, limit);
7941d86652bSAlex Elder 
795*beb90cbaSAlex Elder 			limit = rx_config->aggr_time_limit;
7966bf754c7SAlex Elder 			val |= aggr_time_limit_encoded(version, limit);
7971d86652bSAlex Elder 
7989e88cb5fSAlex Elder 			/* AGGR_PKT_LIMIT is 0 (unlimited) */
7999e88cb5fSAlex Elder 
800cf4e73a1SAlex Elder 			close_eof = rx_config->aggr_close_eof;
8016bf754c7SAlex Elder 			val |= aggr_sw_eof_active_encoded(version, close_eof);
80284f9bd12SAlex Elder 		} else {
80384f9bd12SAlex Elder 			val |= u32_encode_bits(IPA_ENABLE_DEAGGR,
80484f9bd12SAlex Elder 					       AGGR_EN_FMASK);
80584f9bd12SAlex Elder 			val |= u32_encode_bits(IPA_QCMAP, AGGR_TYPE_FMASK);
80684f9bd12SAlex Elder 			/* other fields ignored */
80784f9bd12SAlex Elder 		}
80884f9bd12SAlex Elder 		/* AGGR_FORCE_CLOSE is 0 */
8098bfc4e21SAlex Elder 		/* AGGR_GRAN_SEL is 0 for IPA v4.5 */
81084f9bd12SAlex Elder 	} else {
81184f9bd12SAlex Elder 		val |= u32_encode_bits(IPA_BYPASS_AGGR, AGGR_EN_FMASK);
81284f9bd12SAlex Elder 		/* other fields ignored */
81384f9bd12SAlex Elder 	}
81484f9bd12SAlex Elder 
81584f9bd12SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
81684f9bd12SAlex Elder }
81784f9bd12SAlex Elder 
81863e5afc8SAlex Elder /* Return the Qtime-based head-of-line blocking timer value that
81963e5afc8SAlex Elder  * represents the given number of microseconds.  The result
82063e5afc8SAlex Elder  * includes both the timer value and the selected timer granularity.
821f13a8c31SAlex Elder  */
82263e5afc8SAlex Elder static u32 hol_block_timer_qtime_val(struct ipa *ipa, u32 microseconds)
82363e5afc8SAlex Elder {
82463e5afc8SAlex Elder 	u32 gran_sel;
82563e5afc8SAlex Elder 	u32 val;
82663e5afc8SAlex Elder 
82763e5afc8SAlex Elder 	/* IPA v4.5 expresses time limits using Qtime.  The AP has
82863e5afc8SAlex Elder 	 * pulse generators 0 and 1 available, which were configured
82963e5afc8SAlex Elder 	 * in ipa_qtime_config() to have granularity 100 usec and
83063e5afc8SAlex Elder 	 * 1 msec, respectively.  Use pulse generator 0 if possible,
83163e5afc8SAlex Elder 	 * otherwise fall back to pulse generator 1.
83263e5afc8SAlex Elder 	 */
83363e5afc8SAlex Elder 	val = DIV_ROUND_CLOSEST(microseconds, 100);
83463e5afc8SAlex Elder 	if (val > field_max(TIME_LIMIT_FMASK)) {
83563e5afc8SAlex Elder 		/* Have to use pulse generator 1 (millisecond granularity) */
83663e5afc8SAlex Elder 		gran_sel = GRAN_SEL_FMASK;
83763e5afc8SAlex Elder 		val = DIV_ROUND_CLOSEST(microseconds, 1000);
83863e5afc8SAlex Elder 	} else {
83963e5afc8SAlex Elder 		/* We can use pulse generator 0 (100 usec granularity) */
84063e5afc8SAlex Elder 		gran_sel = 0;
84163e5afc8SAlex Elder 	}
84263e5afc8SAlex Elder 
84363e5afc8SAlex Elder 	return gran_sel | u32_encode_bits(val, TIME_LIMIT_FMASK);
84463e5afc8SAlex Elder }
84563e5afc8SAlex Elder 
84663e5afc8SAlex Elder /* The head-of-line blocking timer is defined as a tick count.  For
84763e5afc8SAlex Elder  * IPA version 4.5 the tick count is based on the Qtimer, which is
84863e5afc8SAlex Elder  * derived from the 19.2 MHz SoC XO clock.  For older IPA versions
84963e5afc8SAlex Elder  * each tick represents 128 cycles of the IPA core clock.
85063e5afc8SAlex Elder  *
85163e5afc8SAlex Elder  * Return the encoded value that should be written to that register
85263e5afc8SAlex Elder  * that represents the timeout period provided.  For IPA v4.2 this
85363e5afc8SAlex Elder  * encodes a base and scale value, while for earlier versions the
85463e5afc8SAlex Elder  * value is a simple tick count.
85563e5afc8SAlex Elder  */
85663e5afc8SAlex Elder static u32 hol_block_timer_val(struct ipa *ipa, u32 microseconds)
85784f9bd12SAlex Elder {
858f13a8c31SAlex Elder 	u32 width;
85984f9bd12SAlex Elder 	u32 scale;
860f13a8c31SAlex Elder 	u64 ticks;
861f13a8c31SAlex Elder 	u64 rate;
862f13a8c31SAlex Elder 	u32 high;
86384f9bd12SAlex Elder 	u32 val;
86484f9bd12SAlex Elder 
86584f9bd12SAlex Elder 	if (!microseconds)
866f13a8c31SAlex Elder 		return 0;	/* Nothing to compute if timer period is 0 */
86784f9bd12SAlex Elder 
868d7f3087bSAlex Elder 	if (ipa->version >= IPA_VERSION_4_5)
86963e5afc8SAlex Elder 		return hol_block_timer_qtime_val(ipa, microseconds);
87063e5afc8SAlex Elder 
871f13a8c31SAlex Elder 	/* Use 64 bit arithmetic to avoid overflow... */
8727aa0e8b8SAlex Elder 	rate = ipa_core_clock_rate(ipa);
873f13a8c31SAlex Elder 	ticks = DIV_ROUND_CLOSEST(microseconds * rate, 128 * USEC_PER_SEC);
874f13a8c31SAlex Elder 	/* ...but we still need to fit into a 32-bit register */
875f13a8c31SAlex Elder 	WARN_ON(ticks > U32_MAX);
87684f9bd12SAlex Elder 
8776833a096SAlex Elder 	/* IPA v3.5.1 through v4.1 just record the tick count */
8786833a096SAlex Elder 	if (ipa->version < IPA_VERSION_4_2)
879f13a8c31SAlex Elder 		return (u32)ticks;
88084f9bd12SAlex Elder 
881f13a8c31SAlex Elder 	/* For IPA v4.2, the tick count is represented by base and
882f13a8c31SAlex Elder 	 * scale fields within the 32-bit timer register, where:
883f13a8c31SAlex Elder 	 *     ticks = base << scale;
884f13a8c31SAlex Elder 	 * The best precision is achieved when the base value is as
885f13a8c31SAlex Elder 	 * large as possible.  Find the highest set bit in the tick
886f13a8c31SAlex Elder 	 * count, and extract the number of bits in the base field
887497abc87SPeng Li 	 * such that high bit is included.
888f13a8c31SAlex Elder 	 */
889f13a8c31SAlex Elder 	high = fls(ticks);		/* 1..32 */
890f13a8c31SAlex Elder 	width = HWEIGHT32(BASE_VALUE_FMASK);
891f13a8c31SAlex Elder 	scale = high > width ? high - width : 0;
892f13a8c31SAlex Elder 	if (scale) {
893f13a8c31SAlex Elder 		/* If we're scaling, round up to get a closer result */
894f13a8c31SAlex Elder 		ticks += 1 << (scale - 1);
895f13a8c31SAlex Elder 		/* High bit was set, so rounding might have affected it */
896f13a8c31SAlex Elder 		if (fls(ticks) != high)
897f13a8c31SAlex Elder 			scale++;
898f13a8c31SAlex Elder 	}
89984f9bd12SAlex Elder 
90084f9bd12SAlex Elder 	val = u32_encode_bits(scale, SCALE_FMASK);
901f13a8c31SAlex Elder 	val |= u32_encode_bits(ticks >> scale, BASE_VALUE_FMASK);
90284f9bd12SAlex Elder 
90384f9bd12SAlex Elder 	return val;
90484f9bd12SAlex Elder }
90584f9bd12SAlex Elder 
906f13a8c31SAlex Elder /* If microseconds is 0, timeout is immediate */
907f13a8c31SAlex Elder static void ipa_endpoint_init_hol_block_timer(struct ipa_endpoint *endpoint,
90884f9bd12SAlex Elder 					      u32 microseconds)
90984f9bd12SAlex Elder {
91084f9bd12SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
91184f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
91284f9bd12SAlex Elder 	u32 offset;
91384f9bd12SAlex Elder 	u32 val;
91484f9bd12SAlex Elder 
915816316caSAlex Elder 	/* This should only be changed when HOL_BLOCK_EN is disabled */
91684f9bd12SAlex Elder 	offset = IPA_REG_ENDP_INIT_HOL_BLOCK_TIMER_N_OFFSET(endpoint_id);
91763e5afc8SAlex Elder 	val = hol_block_timer_val(ipa, microseconds);
91884f9bd12SAlex Elder 	iowrite32(val, ipa->reg_virt + offset);
91984f9bd12SAlex Elder }
92084f9bd12SAlex Elder 
92184f9bd12SAlex Elder static void
922e6aab6b9SAlex Elder ipa_endpoint_init_hol_block_en(struct ipa_endpoint *endpoint, bool enable)
92384f9bd12SAlex Elder {
92484f9bd12SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
92584f9bd12SAlex Elder 	u32 offset;
92684f9bd12SAlex Elder 	u32 val;
92784f9bd12SAlex Elder 
928547c8788SAlex Elder 	val = enable ? HOL_BLOCK_EN_FMASK : 0;
92984f9bd12SAlex Elder 	offset = IPA_REG_ENDP_INIT_HOL_BLOCK_EN_N_OFFSET(endpoint_id);
93084f9bd12SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
9316e228d8cSAlex Elder 	/* When enabling, the register must be written twice for IPA v4.5+ */
9326e228d8cSAlex Elder 	if (enable && endpoint->ipa->version >= IPA_VERSION_4_5)
9336e228d8cSAlex Elder 		iowrite32(val, endpoint->ipa->reg_virt + offset);
93484f9bd12SAlex Elder }
93584f9bd12SAlex Elder 
936e6aab6b9SAlex Elder /* Assumes HOL_BLOCK is in disabled state */
937e6aab6b9SAlex Elder static void ipa_endpoint_init_hol_block_enable(struct ipa_endpoint *endpoint,
938e6aab6b9SAlex Elder 					       u32 microseconds)
939e6aab6b9SAlex Elder {
940e6aab6b9SAlex Elder 	ipa_endpoint_init_hol_block_timer(endpoint, microseconds);
941e6aab6b9SAlex Elder 	ipa_endpoint_init_hol_block_en(endpoint, true);
942e6aab6b9SAlex Elder }
943e6aab6b9SAlex Elder 
944e6aab6b9SAlex Elder static void ipa_endpoint_init_hol_block_disable(struct ipa_endpoint *endpoint)
945e6aab6b9SAlex Elder {
946e6aab6b9SAlex Elder 	ipa_endpoint_init_hol_block_en(endpoint, false);
947e6aab6b9SAlex Elder }
948e6aab6b9SAlex Elder 
94984f9bd12SAlex Elder void ipa_endpoint_modem_hol_block_clear_all(struct ipa *ipa)
95084f9bd12SAlex Elder {
95184f9bd12SAlex Elder 	u32 i;
95284f9bd12SAlex Elder 
95384f9bd12SAlex Elder 	for (i = 0; i < IPA_ENDPOINT_MAX; i++) {
95484f9bd12SAlex Elder 		struct ipa_endpoint *endpoint = &ipa->endpoint[i];
95584f9bd12SAlex Elder 
956f8d34dfdSAlex Elder 		if (endpoint->toward_ipa || endpoint->ee_id != GSI_EE_MODEM)
95784f9bd12SAlex Elder 			continue;
95884f9bd12SAlex Elder 
959e6aab6b9SAlex Elder 		ipa_endpoint_init_hol_block_disable(endpoint);
960e6aab6b9SAlex Elder 		ipa_endpoint_init_hol_block_enable(endpoint, 0);
96184f9bd12SAlex Elder 	}
96284f9bd12SAlex Elder }
96384f9bd12SAlex Elder 
96484f9bd12SAlex Elder static void ipa_endpoint_init_deaggr(struct ipa_endpoint *endpoint)
96584f9bd12SAlex Elder {
96684f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_DEAGGR_N_OFFSET(endpoint->endpoint_id);
96784f9bd12SAlex Elder 	u32 val = 0;
96884f9bd12SAlex Elder 
969fb57c3eaSAlex Elder 	if (!endpoint->toward_ipa)
970fb57c3eaSAlex Elder 		return;		/* Register not valid for RX endpoints */
971fb57c3eaSAlex Elder 
97284f9bd12SAlex Elder 	/* DEAGGR_HDR_LEN is 0 */
97384f9bd12SAlex Elder 	/* PACKET_OFFSET_VALID is 0 */
97484f9bd12SAlex Elder 	/* PACKET_OFFSET_LOCATION is ignored (not valid) */
97584f9bd12SAlex Elder 	/* MAX_PACKET_LEN is 0 (not enforced) */
97684f9bd12SAlex Elder 
97784f9bd12SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
97884f9bd12SAlex Elder }
97984f9bd12SAlex Elder 
9802d265342SAlex Elder static void ipa_endpoint_init_rsrc_grp(struct ipa_endpoint *endpoint)
9812d265342SAlex Elder {
9822d265342SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_RSRC_GRP_N_OFFSET(endpoint->endpoint_id);
9832d265342SAlex Elder 	struct ipa *ipa = endpoint->ipa;
9842d265342SAlex Elder 	u32 val;
9852d265342SAlex Elder 
986660e52d6SAlex Elder 	val = rsrc_grp_encoded(ipa->version, endpoint->config.resource_group);
9872d265342SAlex Elder 	iowrite32(val, ipa->reg_virt + offset);
9882d265342SAlex Elder }
9892d265342SAlex Elder 
99084f9bd12SAlex Elder static void ipa_endpoint_init_seq(struct ipa_endpoint *endpoint)
99184f9bd12SAlex Elder {
99284f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_SEQ_N_OFFSET(endpoint->endpoint_id);
99384f9bd12SAlex Elder 	u32 val = 0;
99484f9bd12SAlex Elder 
995fb57c3eaSAlex Elder 	if (!endpoint->toward_ipa)
996fb57c3eaSAlex Elder 		return;		/* Register not valid for RX endpoints */
997fb57c3eaSAlex Elder 
9988ee5df65SAlex Elder 	/* Low-order byte configures primary packet processing */
999660e52d6SAlex Elder 	val |= u32_encode_bits(endpoint->config.tx.seq_type, SEQ_TYPE_FMASK);
10008ee5df65SAlex Elder 
10018ee5df65SAlex Elder 	/* Second byte configures replicated packet processing */
1002660e52d6SAlex Elder 	val |= u32_encode_bits(endpoint->config.tx.seq_rep_type,
10031690d8a7SAlex Elder 			       SEQ_REP_TYPE_FMASK);
100484f9bd12SAlex Elder 
100584f9bd12SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
100684f9bd12SAlex Elder }
100784f9bd12SAlex Elder 
100884f9bd12SAlex Elder /**
100984f9bd12SAlex Elder  * ipa_endpoint_skb_tx() - Transmit a socket buffer
101084f9bd12SAlex Elder  * @endpoint:	Endpoint pointer
101184f9bd12SAlex Elder  * @skb:	Socket buffer to send
101284f9bd12SAlex Elder  *
101384f9bd12SAlex Elder  * Returns:	0 if successful, or a negative error code
101484f9bd12SAlex Elder  */
101584f9bd12SAlex Elder int ipa_endpoint_skb_tx(struct ipa_endpoint *endpoint, struct sk_buff *skb)
101684f9bd12SAlex Elder {
101784f9bd12SAlex Elder 	struct gsi_trans *trans;
101884f9bd12SAlex Elder 	u32 nr_frags;
101984f9bd12SAlex Elder 	int ret;
102084f9bd12SAlex Elder 
102184f9bd12SAlex Elder 	/* Make sure source endpoint's TLV FIFO has enough entries to
102284f9bd12SAlex Elder 	 * hold the linear portion of the skb and all its fragments.
102384f9bd12SAlex Elder 	 * If not, see if we can linearize it before giving up.
102484f9bd12SAlex Elder 	 */
102584f9bd12SAlex Elder 	nr_frags = skb_shinfo(skb)->nr_frags;
102684f9bd12SAlex Elder 	if (1 + nr_frags > endpoint->trans_tre_max) {
102784f9bd12SAlex Elder 		if (skb_linearize(skb))
102884f9bd12SAlex Elder 			return -E2BIG;
102984f9bd12SAlex Elder 		nr_frags = 0;
103084f9bd12SAlex Elder 	}
103184f9bd12SAlex Elder 
103284f9bd12SAlex Elder 	trans = ipa_endpoint_trans_alloc(endpoint, 1 + nr_frags);
103384f9bd12SAlex Elder 	if (!trans)
103484f9bd12SAlex Elder 		return -EBUSY;
103584f9bd12SAlex Elder 
103684f9bd12SAlex Elder 	ret = gsi_trans_skb_add(trans, skb);
103784f9bd12SAlex Elder 	if (ret)
103884f9bd12SAlex Elder 		goto err_trans_free;
103984f9bd12SAlex Elder 	trans->data = skb;	/* transaction owns skb now */
104084f9bd12SAlex Elder 
104184f9bd12SAlex Elder 	gsi_trans_commit(trans, !netdev_xmit_more());
104284f9bd12SAlex Elder 
104384f9bd12SAlex Elder 	return 0;
104484f9bd12SAlex Elder 
104584f9bd12SAlex Elder err_trans_free:
104684f9bd12SAlex Elder 	gsi_trans_free(trans);
104784f9bd12SAlex Elder 
104884f9bd12SAlex Elder 	return -ENOMEM;
104984f9bd12SAlex Elder }
105084f9bd12SAlex Elder 
105184f9bd12SAlex Elder static void ipa_endpoint_status(struct ipa_endpoint *endpoint)
105284f9bd12SAlex Elder {
105384f9bd12SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
105484f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
105584f9bd12SAlex Elder 	u32 val = 0;
105684f9bd12SAlex Elder 	u32 offset;
105784f9bd12SAlex Elder 
105884f9bd12SAlex Elder 	offset = IPA_REG_ENDP_STATUS_N_OFFSET(endpoint_id);
105984f9bd12SAlex Elder 
1060660e52d6SAlex Elder 	if (endpoint->config.status_enable) {
106184f9bd12SAlex Elder 		val |= STATUS_EN_FMASK;
106284f9bd12SAlex Elder 		if (endpoint->toward_ipa) {
106384f9bd12SAlex Elder 			enum ipa_endpoint_name name;
106484f9bd12SAlex Elder 			u32 status_endpoint_id;
106584f9bd12SAlex Elder 
1066660e52d6SAlex Elder 			name = endpoint->config.tx.status_endpoint;
106784f9bd12SAlex Elder 			status_endpoint_id = ipa->name_map[name]->endpoint_id;
106884f9bd12SAlex Elder 
106984f9bd12SAlex Elder 			val |= u32_encode_bits(status_endpoint_id,
107084f9bd12SAlex Elder 					       STATUS_ENDP_FMASK);
107184f9bd12SAlex Elder 		}
10728bfc4e21SAlex Elder 		/* STATUS_LOCATION is 0, meaning status element precedes
10738bfc4e21SAlex Elder 		 * packet (not present for IPA v4.5)
10748bfc4e21SAlex Elder 		 */
10758bfc4e21SAlex Elder 		/* STATUS_PKT_SUPPRESS_FMASK is 0 (not present for v3.5.1) */
107684f9bd12SAlex Elder 	}
107784f9bd12SAlex Elder 
107884f9bd12SAlex Elder 	iowrite32(val, ipa->reg_virt + offset);
107984f9bd12SAlex Elder }
108084f9bd12SAlex Elder 
10816a606b90SAlex Elder static int ipa_endpoint_replenish_one(struct ipa_endpoint *endpoint,
10826a606b90SAlex Elder 				      struct gsi_trans *trans)
108384f9bd12SAlex Elder {
108484f9bd12SAlex Elder 	struct page *page;
1085ed23f026SAlex Elder 	u32 buffer_size;
108684f9bd12SAlex Elder 	u32 offset;
108784f9bd12SAlex Elder 	u32 len;
108884f9bd12SAlex Elder 	int ret;
108984f9bd12SAlex Elder 
1090660e52d6SAlex Elder 	buffer_size = endpoint->config.rx.buffer_size;
1091ed23f026SAlex Elder 	page = dev_alloc_pages(get_order(buffer_size));
109284f9bd12SAlex Elder 	if (!page)
10936a606b90SAlex Elder 		return -ENOMEM;
109484f9bd12SAlex Elder 
109584f9bd12SAlex Elder 	/* Offset the buffer to make space for skb headroom */
109684f9bd12SAlex Elder 	offset = NET_SKB_PAD;
1097ed23f026SAlex Elder 	len = buffer_size - offset;
109884f9bd12SAlex Elder 
109984f9bd12SAlex Elder 	ret = gsi_trans_page_add(trans, page, len, offset);
110084f9bd12SAlex Elder 	if (ret)
11016a606b90SAlex Elder 		__free_pages(page, get_order(buffer_size));
11026a606b90SAlex Elder 	else
110384f9bd12SAlex Elder 		trans->data = page;	/* transaction owns page now */
110484f9bd12SAlex Elder 
11056a606b90SAlex Elder 	return ret;
110684f9bd12SAlex Elder }
110784f9bd12SAlex Elder 
110884f9bd12SAlex Elder /**
11099af5ccf3SAlex Elder  * ipa_endpoint_replenish() - Replenish endpoint receive buffers
1110e3eea08eSAlex Elder  * @endpoint:	Endpoint to be replenished
111184f9bd12SAlex Elder  *
11129af5ccf3SAlex Elder  * The IPA hardware can hold a fixed number of receive buffers for an RX
11139af5ccf3SAlex Elder  * endpoint, based on the number of entries in the underlying channel ring
11149af5ccf3SAlex Elder  * buffer.  If an endpoint's "backlog" is non-zero, it indicates how many
11159af5ccf3SAlex Elder  * more receive buffers can be supplied to the hardware.  Replenishing for
1116a9bec7aeSAlex Elder  * an endpoint can be disabled, in which case buffers are not queued to
1117a9bec7aeSAlex Elder  * the hardware.
111884f9bd12SAlex Elder  */
11194b22d841SAlex Elder static void ipa_endpoint_replenish(struct ipa_endpoint *endpoint)
112084f9bd12SAlex Elder {
11216a606b90SAlex Elder 	struct gsi_trans *trans;
112284f9bd12SAlex Elder 
11234b22d841SAlex Elder 	if (!test_bit(IPA_REPLENISH_ENABLED, endpoint->replenish_flags))
112484f9bd12SAlex Elder 		return;
112584f9bd12SAlex Elder 
11264b22d841SAlex Elder 	/* Skip it if it's already active */
11274b22d841SAlex Elder 	if (test_and_set_bit(IPA_REPLENISH_ACTIVE, endpoint->replenish_flags))
1128998c0bd2SAlex Elder 		return;
1129998c0bd2SAlex Elder 
1130d0ac30e7SAlex Elder 	while ((trans = ipa_endpoint_trans_alloc(endpoint, 1))) {
11319654d8c4SAlex Elder 		bool doorbell;
11329654d8c4SAlex Elder 
11336a606b90SAlex Elder 		if (ipa_endpoint_replenish_one(endpoint, trans))
11346a606b90SAlex Elder 			goto try_again_later;
1135b9dbabc5SAlex Elder 
1136b9dbabc5SAlex Elder 
1137b9dbabc5SAlex Elder 		/* Ring the doorbell if we've got a full batch */
11389654d8c4SAlex Elder 		doorbell = !(++endpoint->replenish_count % IPA_REPLENISH_BATCH);
11399654d8c4SAlex Elder 		gsi_trans_commit(trans, doorbell);
1140b9dbabc5SAlex Elder 	}
1141998c0bd2SAlex Elder 
1142998c0bd2SAlex Elder 	clear_bit(IPA_REPLENISH_ACTIVE, endpoint->replenish_flags);
1143998c0bd2SAlex Elder 
114484f9bd12SAlex Elder 	return;
114584f9bd12SAlex Elder 
114684f9bd12SAlex Elder try_again_later:
11476a606b90SAlex Elder 	gsi_trans_free(trans);
1148998c0bd2SAlex Elder 	clear_bit(IPA_REPLENISH_ACTIVE, endpoint->replenish_flags);
1149998c0bd2SAlex Elder 
115084f9bd12SAlex Elder 	/* Whenever a receive buffer transaction completes we'll try to
115184f9bd12SAlex Elder 	 * replenish again.  It's unlikely, but if we fail to supply even
115284f9bd12SAlex Elder 	 * one buffer, nothing will trigger another replenish attempt.
11535fc7f9baSAlex Elder 	 * If the hardware has no receive buffers queued, schedule work to
11545fc7f9baSAlex Elder 	 * try replenishing again.
115584f9bd12SAlex Elder 	 */
11565fc7f9baSAlex Elder 	if (gsi_channel_trans_idle(&endpoint->ipa->gsi, endpoint->channel_id))
115784f9bd12SAlex Elder 		schedule_delayed_work(&endpoint->replenish_work,
115884f9bd12SAlex Elder 				      msecs_to_jiffies(1));
115984f9bd12SAlex Elder }
116084f9bd12SAlex Elder 
116184f9bd12SAlex Elder static void ipa_endpoint_replenish_enable(struct ipa_endpoint *endpoint)
116284f9bd12SAlex Elder {
1163c1aaa01dSAlex Elder 	set_bit(IPA_REPLENISH_ENABLED, endpoint->replenish_flags);
116484f9bd12SAlex Elder 
116584f9bd12SAlex Elder 	/* Start replenishing if hardware currently has no buffers */
11665fc7f9baSAlex Elder 	if (gsi_channel_trans_idle(&endpoint->ipa->gsi, endpoint->channel_id))
11674b22d841SAlex Elder 		ipa_endpoint_replenish(endpoint);
116884f9bd12SAlex Elder }
116984f9bd12SAlex Elder 
117084f9bd12SAlex Elder static void ipa_endpoint_replenish_disable(struct ipa_endpoint *endpoint)
117184f9bd12SAlex Elder {
1172c1aaa01dSAlex Elder 	clear_bit(IPA_REPLENISH_ENABLED, endpoint->replenish_flags);
117384f9bd12SAlex Elder }
117484f9bd12SAlex Elder 
117584f9bd12SAlex Elder static void ipa_endpoint_replenish_work(struct work_struct *work)
117684f9bd12SAlex Elder {
117784f9bd12SAlex Elder 	struct delayed_work *dwork = to_delayed_work(work);
117884f9bd12SAlex Elder 	struct ipa_endpoint *endpoint;
117984f9bd12SAlex Elder 
118084f9bd12SAlex Elder 	endpoint = container_of(dwork, struct ipa_endpoint, replenish_work);
118184f9bd12SAlex Elder 
11824b22d841SAlex Elder 	ipa_endpoint_replenish(endpoint);
118384f9bd12SAlex Elder }
118484f9bd12SAlex Elder 
118584f9bd12SAlex Elder static void ipa_endpoint_skb_copy(struct ipa_endpoint *endpoint,
118684f9bd12SAlex Elder 				  void *data, u32 len, u32 extra)
118784f9bd12SAlex Elder {
118884f9bd12SAlex Elder 	struct sk_buff *skb;
118984f9bd12SAlex Elder 
11901b65bbccSAlex Elder 	if (!endpoint->netdev)
11911b65bbccSAlex Elder 		return;
11921b65bbccSAlex Elder 
119384f9bd12SAlex Elder 	skb = __dev_alloc_skb(len, GFP_ATOMIC);
119430b338ffSAlex Elder 	if (skb) {
11951b65bbccSAlex Elder 		/* Copy the data into the socket buffer and receive it */
119684f9bd12SAlex Elder 		skb_put(skb, len);
119784f9bd12SAlex Elder 		memcpy(skb->data, data, len);
119884f9bd12SAlex Elder 		skb->truesize += extra;
119930b338ffSAlex Elder 	}
120084f9bd12SAlex Elder 
120184f9bd12SAlex Elder 	ipa_modem_skb_rx(endpoint->netdev, skb);
120284f9bd12SAlex Elder }
120384f9bd12SAlex Elder 
120484f9bd12SAlex Elder static bool ipa_endpoint_skb_build(struct ipa_endpoint *endpoint,
120584f9bd12SAlex Elder 				   struct page *page, u32 len)
120684f9bd12SAlex Elder {
1207660e52d6SAlex Elder 	u32 buffer_size = endpoint->config.rx.buffer_size;
120884f9bd12SAlex Elder 	struct sk_buff *skb;
120984f9bd12SAlex Elder 
121084f9bd12SAlex Elder 	/* Nothing to do if there's no netdev */
121184f9bd12SAlex Elder 	if (!endpoint->netdev)
121284f9bd12SAlex Elder 		return false;
121384f9bd12SAlex Elder 
1214ed23f026SAlex Elder 	WARN_ON(len > SKB_WITH_OVERHEAD(buffer_size - NET_SKB_PAD));
12155bc55884SAlex Elder 
1216ed23f026SAlex Elder 	skb = build_skb(page_address(page), buffer_size);
121784f9bd12SAlex Elder 	if (skb) {
121884f9bd12SAlex Elder 		/* Reserve the headroom and account for the data */
121984f9bd12SAlex Elder 		skb_reserve(skb, NET_SKB_PAD);
122084f9bd12SAlex Elder 		skb_put(skb, len);
122184f9bd12SAlex Elder 	}
122284f9bd12SAlex Elder 
122384f9bd12SAlex Elder 	/* Receive the buffer (or record drop if unable to build it) */
122484f9bd12SAlex Elder 	ipa_modem_skb_rx(endpoint->netdev, skb);
122584f9bd12SAlex Elder 
122684f9bd12SAlex Elder 	return skb != NULL;
122784f9bd12SAlex Elder }
122884f9bd12SAlex Elder 
122984f9bd12SAlex Elder /* The format of a packet status element is the same for several status
123045921390SAlex Elder  * types (opcodes).  Other types aren't currently supported.
123184f9bd12SAlex Elder  */
123284f9bd12SAlex Elder static bool ipa_status_format_packet(enum ipa_status_opcode opcode)
123384f9bd12SAlex Elder {
123484f9bd12SAlex Elder 	switch (opcode) {
123584f9bd12SAlex Elder 	case IPA_STATUS_OPCODE_PACKET:
123684f9bd12SAlex Elder 	case IPA_STATUS_OPCODE_DROPPED_PACKET:
123784f9bd12SAlex Elder 	case IPA_STATUS_OPCODE_SUSPENDED_PACKET:
123884f9bd12SAlex Elder 	case IPA_STATUS_OPCODE_PACKET_2ND_PASS:
123984f9bd12SAlex Elder 		return true;
124084f9bd12SAlex Elder 	default:
124184f9bd12SAlex Elder 		return false;
124284f9bd12SAlex Elder 	}
124384f9bd12SAlex Elder }
124484f9bd12SAlex Elder 
124584f9bd12SAlex Elder static bool ipa_endpoint_status_skip(struct ipa_endpoint *endpoint,
124684f9bd12SAlex Elder 				     const struct ipa_status *status)
124784f9bd12SAlex Elder {
124884f9bd12SAlex Elder 	u32 endpoint_id;
124984f9bd12SAlex Elder 
125084f9bd12SAlex Elder 	if (!ipa_status_format_packet(status->opcode))
125184f9bd12SAlex Elder 		return true;
125284f9bd12SAlex Elder 	if (!status->pkt_len)
125384f9bd12SAlex Elder 		return true;
1254c13899f1SAlex Elder 	endpoint_id = u8_get_bits(status->endp_dst_idx,
125584f9bd12SAlex Elder 				  IPA_STATUS_DST_IDX_FMASK);
125684f9bd12SAlex Elder 	if (endpoint_id != endpoint->endpoint_id)
125784f9bd12SAlex Elder 		return true;
125884f9bd12SAlex Elder 
125984f9bd12SAlex Elder 	return false;	/* Don't skip this packet, process it */
126084f9bd12SAlex Elder }
126184f9bd12SAlex Elder 
1262f6aba7b5SAlex Elder static bool ipa_endpoint_status_tag(struct ipa_endpoint *endpoint,
1263f6aba7b5SAlex Elder 				    const struct ipa_status *status)
1264f6aba7b5SAlex Elder {
126551c48ce2SAlex Elder 	struct ipa_endpoint *command_endpoint;
126651c48ce2SAlex Elder 	struct ipa *ipa = endpoint->ipa;
126751c48ce2SAlex Elder 	u32 endpoint_id;
126851c48ce2SAlex Elder 
126951c48ce2SAlex Elder 	if (!le16_get_bits(status->mask, IPA_STATUS_MASK_TAG_VALID_FMASK))
127051c48ce2SAlex Elder 		return false;	/* No valid tag */
127151c48ce2SAlex Elder 
127251c48ce2SAlex Elder 	/* The status contains a valid tag.  We know the packet was sent to
127351c48ce2SAlex Elder 	 * this endpoint (already verified by ipa_endpoint_status_skip()).
127451c48ce2SAlex Elder 	 * If the packet came from the AP->command TX endpoint we know
127551c48ce2SAlex Elder 	 * this packet was sent as part of the pipeline clear process.
127651c48ce2SAlex Elder 	 */
127751c48ce2SAlex Elder 	endpoint_id = u8_get_bits(status->endp_src_idx,
127851c48ce2SAlex Elder 				  IPA_STATUS_SRC_IDX_FMASK);
127951c48ce2SAlex Elder 	command_endpoint = ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX];
128051c48ce2SAlex Elder 	if (endpoint_id == command_endpoint->endpoint_id) {
128151c48ce2SAlex Elder 		complete(&ipa->completion);
128251c48ce2SAlex Elder 	} else {
128351c48ce2SAlex Elder 		dev_err(&ipa->pdev->dev,
128451c48ce2SAlex Elder 			"unexpected tagged packet from endpoint %u\n",
128551c48ce2SAlex Elder 			endpoint_id);
128651c48ce2SAlex Elder 	}
128751c48ce2SAlex Elder 
128851c48ce2SAlex Elder 	return true;
1289f6aba7b5SAlex Elder }
1290f6aba7b5SAlex Elder 
129184f9bd12SAlex Elder /* Return whether the status indicates the packet should be dropped */
1292f6aba7b5SAlex Elder static bool ipa_endpoint_status_drop(struct ipa_endpoint *endpoint,
1293f6aba7b5SAlex Elder 				     const struct ipa_status *status)
129484f9bd12SAlex Elder {
129584f9bd12SAlex Elder 	u32 val;
129684f9bd12SAlex Elder 
1297f6aba7b5SAlex Elder 	/* If the status indicates a tagged transfer, we'll drop the packet */
1298f6aba7b5SAlex Elder 	if (ipa_endpoint_status_tag(endpoint, status))
1299f6aba7b5SAlex Elder 		return true;
1300f6aba7b5SAlex Elder 
1301ab4f71e5SAlex Elder 	/* Deaggregation exceptions we drop; all other types we consume */
130284f9bd12SAlex Elder 	if (status->exception)
130384f9bd12SAlex Elder 		return status->exception == IPA_STATUS_EXCEPTION_DEAGGR;
130484f9bd12SAlex Elder 
130584f9bd12SAlex Elder 	/* Drop the packet if it fails to match a routing rule; otherwise no */
130684f9bd12SAlex Elder 	val = le32_get_bits(status->flags1, IPA_STATUS_FLAGS1_RT_RULE_ID_FMASK);
130784f9bd12SAlex Elder 
130884f9bd12SAlex Elder 	return val == field_max(IPA_STATUS_FLAGS1_RT_RULE_ID_FMASK);
130984f9bd12SAlex Elder }
131084f9bd12SAlex Elder 
131184f9bd12SAlex Elder static void ipa_endpoint_status_parse(struct ipa_endpoint *endpoint,
131284f9bd12SAlex Elder 				      struct page *page, u32 total_len)
131384f9bd12SAlex Elder {
1314660e52d6SAlex Elder 	u32 buffer_size = endpoint->config.rx.buffer_size;
131584f9bd12SAlex Elder 	void *data = page_address(page) + NET_SKB_PAD;
1316ed23f026SAlex Elder 	u32 unused = buffer_size - total_len;
131784f9bd12SAlex Elder 	u32 resid = total_len;
131884f9bd12SAlex Elder 
131984f9bd12SAlex Elder 	while (resid) {
132084f9bd12SAlex Elder 		const struct ipa_status *status = data;
132184f9bd12SAlex Elder 		u32 align;
132284f9bd12SAlex Elder 		u32 len;
132384f9bd12SAlex Elder 
132484f9bd12SAlex Elder 		if (resid < sizeof(*status)) {
132584f9bd12SAlex Elder 			dev_err(&endpoint->ipa->pdev->dev,
132684f9bd12SAlex Elder 				"short message (%u bytes < %zu byte status)\n",
132784f9bd12SAlex Elder 				resid, sizeof(*status));
132884f9bd12SAlex Elder 			break;
132984f9bd12SAlex Elder 		}
133084f9bd12SAlex Elder 
133184f9bd12SAlex Elder 		/* Skip over status packets that lack packet data */
133284f9bd12SAlex Elder 		if (ipa_endpoint_status_skip(endpoint, status)) {
133384f9bd12SAlex Elder 			data += sizeof(*status);
133484f9bd12SAlex Elder 			resid -= sizeof(*status);
133584f9bd12SAlex Elder 			continue;
133684f9bd12SAlex Elder 		}
133784f9bd12SAlex Elder 
1338162fbc6fSAlex Elder 		/* Compute the amount of buffer space consumed by the packet,
1339162fbc6fSAlex Elder 		 * including the status element.  If the hardware is configured
1340162fbc6fSAlex Elder 		 * to pad packet data to an aligned boundary, account for that.
1341162fbc6fSAlex Elder 		 * And if checksum offload is enabled a trailer containing
1342162fbc6fSAlex Elder 		 * computed checksum information will be appended.
134384f9bd12SAlex Elder 		 */
1344660e52d6SAlex Elder 		align = endpoint->config.rx.pad_align ? : 1;
134584f9bd12SAlex Elder 		len = le16_to_cpu(status->pkt_len);
134684f9bd12SAlex Elder 		len = sizeof(*status) + ALIGN(len, align);
1347660e52d6SAlex Elder 		if (endpoint->config.checksum)
134884f9bd12SAlex Elder 			len += sizeof(struct rmnet_map_dl_csum_trailer);
134984f9bd12SAlex Elder 
1350f6aba7b5SAlex Elder 		if (!ipa_endpoint_status_drop(endpoint, status)) {
1351162fbc6fSAlex Elder 			void *data2;
1352162fbc6fSAlex Elder 			u32 extra;
1353162fbc6fSAlex Elder 			u32 len2;
135484f9bd12SAlex Elder 
135584f9bd12SAlex Elder 			/* Client receives only packet data (no status) */
1356162fbc6fSAlex Elder 			data2 = data + sizeof(*status);
1357162fbc6fSAlex Elder 			len2 = le16_to_cpu(status->pkt_len);
1358162fbc6fSAlex Elder 
1359162fbc6fSAlex Elder 			/* Have the true size reflect the extra unused space in
1360162fbc6fSAlex Elder 			 * the original receive buffer.  Distribute the "cost"
1361162fbc6fSAlex Elder 			 * proportionately across all aggregated packets in the
1362162fbc6fSAlex Elder 			 * buffer.
1363162fbc6fSAlex Elder 			 */
1364162fbc6fSAlex Elder 			extra = DIV_ROUND_CLOSEST(unused * len, total_len);
136584f9bd12SAlex Elder 			ipa_endpoint_skb_copy(endpoint, data2, len2, extra);
136684f9bd12SAlex Elder 		}
136784f9bd12SAlex Elder 
136884f9bd12SAlex Elder 		/* Consume status and the full packet it describes */
136984f9bd12SAlex Elder 		data += len;
137084f9bd12SAlex Elder 		resid -= len;
137184f9bd12SAlex Elder 	}
137284f9bd12SAlex Elder }
137384f9bd12SAlex Elder 
137484f9bd12SAlex Elder /* Complete a TX transaction, command or from ipa_endpoint_skb_tx() */
137584f9bd12SAlex Elder static void ipa_endpoint_tx_complete(struct ipa_endpoint *endpoint,
137684f9bd12SAlex Elder 				     struct gsi_trans *trans)
137784f9bd12SAlex Elder {
137884f9bd12SAlex Elder }
137984f9bd12SAlex Elder 
138084f9bd12SAlex Elder /* Complete transaction initiated in ipa_endpoint_replenish_one() */
138184f9bd12SAlex Elder static void ipa_endpoint_rx_complete(struct ipa_endpoint *endpoint,
138284f9bd12SAlex Elder 				     struct gsi_trans *trans)
138384f9bd12SAlex Elder {
138484f9bd12SAlex Elder 	struct page *page;
138584f9bd12SAlex Elder 
138684f9bd12SAlex Elder 	if (trans->cancelled)
13875d6ac24fSAlex Elder 		goto done;
138884f9bd12SAlex Elder 
138984f9bd12SAlex Elder 	/* Parse or build a socket buffer using the actual received length */
139084f9bd12SAlex Elder 	page = trans->data;
1391660e52d6SAlex Elder 	if (endpoint->config.status_enable)
139284f9bd12SAlex Elder 		ipa_endpoint_status_parse(endpoint, page, trans->len);
139384f9bd12SAlex Elder 	else if (ipa_endpoint_skb_build(endpoint, page, trans->len))
139484f9bd12SAlex Elder 		trans->data = NULL;	/* Pages have been consumed */
13955d6ac24fSAlex Elder done:
13965d6ac24fSAlex Elder 	ipa_endpoint_replenish(endpoint);
139784f9bd12SAlex Elder }
139884f9bd12SAlex Elder 
139984f9bd12SAlex Elder void ipa_endpoint_trans_complete(struct ipa_endpoint *endpoint,
140084f9bd12SAlex Elder 				 struct gsi_trans *trans)
140184f9bd12SAlex Elder {
140284f9bd12SAlex Elder 	if (endpoint->toward_ipa)
140384f9bd12SAlex Elder 		ipa_endpoint_tx_complete(endpoint, trans);
140484f9bd12SAlex Elder 	else
140584f9bd12SAlex Elder 		ipa_endpoint_rx_complete(endpoint, trans);
140684f9bd12SAlex Elder }
140784f9bd12SAlex Elder 
140884f9bd12SAlex Elder void ipa_endpoint_trans_release(struct ipa_endpoint *endpoint,
140984f9bd12SAlex Elder 				struct gsi_trans *trans)
141084f9bd12SAlex Elder {
141184f9bd12SAlex Elder 	if (endpoint->toward_ipa) {
141284f9bd12SAlex Elder 		struct ipa *ipa = endpoint->ipa;
141384f9bd12SAlex Elder 
141484f9bd12SAlex Elder 		/* Nothing to do for command transactions */
141584f9bd12SAlex Elder 		if (endpoint != ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]) {
141684f9bd12SAlex Elder 			struct sk_buff *skb = trans->data;
141784f9bd12SAlex Elder 
141884f9bd12SAlex Elder 			if (skb)
141984f9bd12SAlex Elder 				dev_kfree_skb_any(skb);
142084f9bd12SAlex Elder 		}
142184f9bd12SAlex Elder 	} else {
142284f9bd12SAlex Elder 		struct page *page = trans->data;
142384f9bd12SAlex Elder 
1424ed23f026SAlex Elder 		if (page) {
1425660e52d6SAlex Elder 			u32 buffer_size = endpoint->config.rx.buffer_size;
1426ed23f026SAlex Elder 
1427ed23f026SAlex Elder 			__free_pages(page, get_order(buffer_size));
1428ed23f026SAlex Elder 		}
142984f9bd12SAlex Elder 	}
143084f9bd12SAlex Elder }
143184f9bd12SAlex Elder 
143284f9bd12SAlex Elder void ipa_endpoint_default_route_set(struct ipa *ipa, u32 endpoint_id)
143384f9bd12SAlex Elder {
143484f9bd12SAlex Elder 	u32 val;
143584f9bd12SAlex Elder 
143684f9bd12SAlex Elder 	/* ROUTE_DIS is 0 */
143784f9bd12SAlex Elder 	val = u32_encode_bits(endpoint_id, ROUTE_DEF_PIPE_FMASK);
143884f9bd12SAlex Elder 	val |= ROUTE_DEF_HDR_TABLE_FMASK;
143984f9bd12SAlex Elder 	val |= u32_encode_bits(0, ROUTE_DEF_HDR_OFST_FMASK);
144084f9bd12SAlex Elder 	val |= u32_encode_bits(endpoint_id, ROUTE_FRAG_DEF_PIPE_FMASK);
144184f9bd12SAlex Elder 	val |= ROUTE_DEF_RETAIN_HDR_FMASK;
144284f9bd12SAlex Elder 
144384f9bd12SAlex Elder 	iowrite32(val, ipa->reg_virt + IPA_REG_ROUTE_OFFSET);
144484f9bd12SAlex Elder }
144584f9bd12SAlex Elder 
144684f9bd12SAlex Elder void ipa_endpoint_default_route_clear(struct ipa *ipa)
144784f9bd12SAlex Elder {
144884f9bd12SAlex Elder 	ipa_endpoint_default_route_set(ipa, 0);
144984f9bd12SAlex Elder }
145084f9bd12SAlex Elder 
145184f9bd12SAlex Elder /**
145284f9bd12SAlex Elder  * ipa_endpoint_reset_rx_aggr() - Reset RX endpoint with aggregation active
145384f9bd12SAlex Elder  * @endpoint:	Endpoint to be reset
145484f9bd12SAlex Elder  *
145584f9bd12SAlex Elder  * If aggregation is active on an RX endpoint when a reset is performed
145684f9bd12SAlex Elder  * on its underlying GSI channel, a special sequence of actions must be
145784f9bd12SAlex Elder  * taken to ensure the IPA pipeline is properly cleared.
145884f9bd12SAlex Elder  *
1459e3eea08eSAlex Elder  * Return:	0 if successful, or a negative error code
146084f9bd12SAlex Elder  */
146184f9bd12SAlex Elder static int ipa_endpoint_reset_rx_aggr(struct ipa_endpoint *endpoint)
146284f9bd12SAlex Elder {
146384f9bd12SAlex Elder 	struct device *dev = &endpoint->ipa->pdev->dev;
146484f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
146584f9bd12SAlex Elder 	struct gsi *gsi = &ipa->gsi;
14664fa95248SAlex Elder 	bool suspended = false;
146784f9bd12SAlex Elder 	dma_addr_t addr;
146884f9bd12SAlex Elder 	u32 retries;
146984f9bd12SAlex Elder 	u32 len = 1;
147084f9bd12SAlex Elder 	void *virt;
147184f9bd12SAlex Elder 	int ret;
147284f9bd12SAlex Elder 
147384f9bd12SAlex Elder 	virt = kzalloc(len, GFP_KERNEL);
147484f9bd12SAlex Elder 	if (!virt)
147584f9bd12SAlex Elder 		return -ENOMEM;
147684f9bd12SAlex Elder 
147784f9bd12SAlex Elder 	addr = dma_map_single(dev, virt, len, DMA_FROM_DEVICE);
147884f9bd12SAlex Elder 	if (dma_mapping_error(dev, addr)) {
147984f9bd12SAlex Elder 		ret = -ENOMEM;
148084f9bd12SAlex Elder 		goto out_kfree;
148184f9bd12SAlex Elder 	}
148284f9bd12SAlex Elder 
148384f9bd12SAlex Elder 	/* Force close aggregation before issuing the reset */
148484f9bd12SAlex Elder 	ipa_endpoint_force_close(endpoint);
148584f9bd12SAlex Elder 
148684f9bd12SAlex Elder 	/* Reset and reconfigure the channel with the doorbell engine
148784f9bd12SAlex Elder 	 * disabled.  Then poll until we know aggregation is no longer
148884f9bd12SAlex Elder 	 * active.  We'll re-enable the doorbell (if appropriate) when
148984f9bd12SAlex Elder 	 * we reset again below.
149084f9bd12SAlex Elder 	 */
149184f9bd12SAlex Elder 	gsi_channel_reset(gsi, endpoint->channel_id, false);
149284f9bd12SAlex Elder 
149384f9bd12SAlex Elder 	/* Make sure the channel isn't suspended */
14944fa95248SAlex Elder 	suspended = ipa_endpoint_program_suspend(endpoint, false);
149584f9bd12SAlex Elder 
149684f9bd12SAlex Elder 	/* Start channel and do a 1 byte read */
149784f9bd12SAlex Elder 	ret = gsi_channel_start(gsi, endpoint->channel_id);
149884f9bd12SAlex Elder 	if (ret)
149984f9bd12SAlex Elder 		goto out_suspend_again;
150084f9bd12SAlex Elder 
150184f9bd12SAlex Elder 	ret = gsi_trans_read_byte(gsi, endpoint->channel_id, addr);
150284f9bd12SAlex Elder 	if (ret)
150384f9bd12SAlex Elder 		goto err_endpoint_stop;
150484f9bd12SAlex Elder 
150584f9bd12SAlex Elder 	/* Wait for aggregation to be closed on the channel */
150684f9bd12SAlex Elder 	retries = IPA_ENDPOINT_RESET_AGGR_RETRY_MAX;
150784f9bd12SAlex Elder 	do {
150884f9bd12SAlex Elder 		if (!ipa_endpoint_aggr_active(endpoint))
150984f9bd12SAlex Elder 			break;
151074401946SAlex Elder 		usleep_range(USEC_PER_MSEC, 2 * USEC_PER_MSEC);
151184f9bd12SAlex Elder 	} while (retries--);
151284f9bd12SAlex Elder 
151384f9bd12SAlex Elder 	/* Check one last time */
151484f9bd12SAlex Elder 	if (ipa_endpoint_aggr_active(endpoint))
151584f9bd12SAlex Elder 		dev_err(dev, "endpoint %u still active during reset\n",
151684f9bd12SAlex Elder 			endpoint->endpoint_id);
151784f9bd12SAlex Elder 
151884f9bd12SAlex Elder 	gsi_trans_read_byte_done(gsi, endpoint->channel_id);
151984f9bd12SAlex Elder 
1520f30dcb7dSAlex Elder 	ret = gsi_channel_stop(gsi, endpoint->channel_id);
152184f9bd12SAlex Elder 	if (ret)
152284f9bd12SAlex Elder 		goto out_suspend_again;
152384f9bd12SAlex Elder 
1524497abc87SPeng Li 	/* Finally, reset and reconfigure the channel again (re-enabling
152584f9bd12SAlex Elder 	 * the doorbell engine if appropriate).  Sleep for 1 millisecond to
152684f9bd12SAlex Elder 	 * complete the channel reset sequence.  Finish by suspending the
152784f9bd12SAlex Elder 	 * channel again (if necessary).
152884f9bd12SAlex Elder 	 */
1529ce54993dSAlex Elder 	gsi_channel_reset(gsi, endpoint->channel_id, true);
153084f9bd12SAlex Elder 
153174401946SAlex Elder 	usleep_range(USEC_PER_MSEC, 2 * USEC_PER_MSEC);
153284f9bd12SAlex Elder 
153384f9bd12SAlex Elder 	goto out_suspend_again;
153484f9bd12SAlex Elder 
153584f9bd12SAlex Elder err_endpoint_stop:
1536f30dcb7dSAlex Elder 	(void)gsi_channel_stop(gsi, endpoint->channel_id);
153784f9bd12SAlex Elder out_suspend_again:
15384fa95248SAlex Elder 	if (suspended)
15394fa95248SAlex Elder 		(void)ipa_endpoint_program_suspend(endpoint, true);
154084f9bd12SAlex Elder 	dma_unmap_single(dev, addr, len, DMA_FROM_DEVICE);
154184f9bd12SAlex Elder out_kfree:
154284f9bd12SAlex Elder 	kfree(virt);
154384f9bd12SAlex Elder 
154484f9bd12SAlex Elder 	return ret;
154584f9bd12SAlex Elder }
154684f9bd12SAlex Elder 
154784f9bd12SAlex Elder static void ipa_endpoint_reset(struct ipa_endpoint *endpoint)
154884f9bd12SAlex Elder {
154984f9bd12SAlex Elder 	u32 channel_id = endpoint->channel_id;
155084f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
155184f9bd12SAlex Elder 	bool special;
155284f9bd12SAlex Elder 	int ret = 0;
155384f9bd12SAlex Elder 
155484f9bd12SAlex Elder 	/* On IPA v3.5.1, if an RX endpoint is reset while aggregation
155584f9bd12SAlex Elder 	 * is active, we need to handle things specially to recover.
155684f9bd12SAlex Elder 	 * All other cases just need to reset the underlying GSI channel.
155784f9bd12SAlex Elder 	 */
1558d7f3087bSAlex Elder 	special = ipa->version < IPA_VERSION_4_0 && !endpoint->toward_ipa &&
1559660e52d6SAlex Elder 			endpoint->config.aggregation;
1560ce54993dSAlex Elder 	if (special && ipa_endpoint_aggr_active(endpoint))
156184f9bd12SAlex Elder 		ret = ipa_endpoint_reset_rx_aggr(endpoint);
156284f9bd12SAlex Elder 	else
1563ce54993dSAlex Elder 		gsi_channel_reset(&ipa->gsi, channel_id, true);
156484f9bd12SAlex Elder 
156584f9bd12SAlex Elder 	if (ret)
156684f9bd12SAlex Elder 		dev_err(&ipa->pdev->dev,
156784f9bd12SAlex Elder 			"error %d resetting channel %u for endpoint %u\n",
156884f9bd12SAlex Elder 			ret, endpoint->channel_id, endpoint->endpoint_id);
156984f9bd12SAlex Elder }
157084f9bd12SAlex Elder 
157184f9bd12SAlex Elder static void ipa_endpoint_program(struct ipa_endpoint *endpoint)
157284f9bd12SAlex Elder {
15734c9d631aSAlex Elder 	if (endpoint->toward_ipa) {
15744c9d631aSAlex Elder 		/* Newer versions of IPA use GSI channel flow control
15754c9d631aSAlex Elder 		 * instead of endpoint DELAY mode to prevent sending data.
15764c9d631aSAlex Elder 		 * Flow control is disabled for newly-allocated channels,
15774c9d631aSAlex Elder 		 * and we can assume flow control is not (ever) enabled
15784c9d631aSAlex Elder 		 * for AP TX channels.
15794c9d631aSAlex Elder 		 */
15804c9d631aSAlex Elder 		if (endpoint->ipa->version < IPA_VERSION_4_2)
1581a4dcad34SAlex Elder 			ipa_endpoint_program_delay(endpoint, false);
15824c9d631aSAlex Elder 	} else {
15834c9d631aSAlex Elder 		/* Ensure suspend mode is off on all AP RX endpoints */
1584fb57c3eaSAlex Elder 		(void)ipa_endpoint_program_suspend(endpoint, false);
15854c9d631aSAlex Elder 	}
1586fb57c3eaSAlex Elder 	ipa_endpoint_init_cfg(endpoint);
1587647a05f3SAlex Elder 	ipa_endpoint_init_nat(endpoint);
1588fb57c3eaSAlex Elder 	ipa_endpoint_init_hdr(endpoint);
158984f9bd12SAlex Elder 	ipa_endpoint_init_hdr_ext(endpoint);
1590fb57c3eaSAlex Elder 	ipa_endpoint_init_hdr_metadata_mask(endpoint);
1591fb57c3eaSAlex Elder 	ipa_endpoint_init_mode(endpoint);
159284f9bd12SAlex Elder 	ipa_endpoint_init_aggr(endpoint);
1593153213f0SAlex Elder 	if (!endpoint->toward_ipa) {
1594153213f0SAlex Elder 		if (endpoint->config.rx.holb_drop)
1595153213f0SAlex Elder 			ipa_endpoint_init_hol_block_enable(endpoint, 0);
1596153213f0SAlex Elder 		else
159701c36637SAlex Elder 			ipa_endpoint_init_hol_block_disable(endpoint);
1598153213f0SAlex Elder 	}
159984f9bd12SAlex Elder 	ipa_endpoint_init_deaggr(endpoint);
16002d265342SAlex Elder 	ipa_endpoint_init_rsrc_grp(endpoint);
160184f9bd12SAlex Elder 	ipa_endpoint_init_seq(endpoint);
160284f9bd12SAlex Elder 	ipa_endpoint_status(endpoint);
160384f9bd12SAlex Elder }
160484f9bd12SAlex Elder 
160584f9bd12SAlex Elder int ipa_endpoint_enable_one(struct ipa_endpoint *endpoint)
160684f9bd12SAlex Elder {
160784f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
160884f9bd12SAlex Elder 	struct gsi *gsi = &ipa->gsi;
160984f9bd12SAlex Elder 	int ret;
161084f9bd12SAlex Elder 
161184f9bd12SAlex Elder 	ret = gsi_channel_start(gsi, endpoint->channel_id);
161284f9bd12SAlex Elder 	if (ret) {
161384f9bd12SAlex Elder 		dev_err(&ipa->pdev->dev,
161484f9bd12SAlex Elder 			"error %d starting %cX channel %u for endpoint %u\n",
161584f9bd12SAlex Elder 			ret, endpoint->toward_ipa ? 'T' : 'R',
161684f9bd12SAlex Elder 			endpoint->channel_id, endpoint->endpoint_id);
161784f9bd12SAlex Elder 		return ret;
161884f9bd12SAlex Elder 	}
161984f9bd12SAlex Elder 
162084f9bd12SAlex Elder 	if (!endpoint->toward_ipa) {
162184f9bd12SAlex Elder 		ipa_interrupt_suspend_enable(ipa->interrupt,
162284f9bd12SAlex Elder 					     endpoint->endpoint_id);
162384f9bd12SAlex Elder 		ipa_endpoint_replenish_enable(endpoint);
162484f9bd12SAlex Elder 	}
162584f9bd12SAlex Elder 
162684f9bd12SAlex Elder 	ipa->enabled |= BIT(endpoint->endpoint_id);
162784f9bd12SAlex Elder 
162884f9bd12SAlex Elder 	return 0;
162984f9bd12SAlex Elder }
163084f9bd12SAlex Elder 
163184f9bd12SAlex Elder void ipa_endpoint_disable_one(struct ipa_endpoint *endpoint)
163284f9bd12SAlex Elder {
163384f9bd12SAlex Elder 	u32 mask = BIT(endpoint->endpoint_id);
163484f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
1635f30dcb7dSAlex Elder 	struct gsi *gsi = &ipa->gsi;
163684f9bd12SAlex Elder 	int ret;
163784f9bd12SAlex Elder 
1638f30dcb7dSAlex Elder 	if (!(ipa->enabled & mask))
163984f9bd12SAlex Elder 		return;
164084f9bd12SAlex Elder 
1641f30dcb7dSAlex Elder 	ipa->enabled ^= mask;
164284f9bd12SAlex Elder 
164384f9bd12SAlex Elder 	if (!endpoint->toward_ipa) {
164484f9bd12SAlex Elder 		ipa_endpoint_replenish_disable(endpoint);
164584f9bd12SAlex Elder 		ipa_interrupt_suspend_disable(ipa->interrupt,
164684f9bd12SAlex Elder 					      endpoint->endpoint_id);
164784f9bd12SAlex Elder 	}
164884f9bd12SAlex Elder 
164984f9bd12SAlex Elder 	/* Note that if stop fails, the channel's state is not well-defined */
1650f30dcb7dSAlex Elder 	ret = gsi_channel_stop(gsi, endpoint->channel_id);
165184f9bd12SAlex Elder 	if (ret)
165284f9bd12SAlex Elder 		dev_err(&ipa->pdev->dev,
165384f9bd12SAlex Elder 			"error %d attempting to stop endpoint %u\n", ret,
165484f9bd12SAlex Elder 			endpoint->endpoint_id);
165584f9bd12SAlex Elder }
165684f9bd12SAlex Elder 
165784f9bd12SAlex Elder void ipa_endpoint_suspend_one(struct ipa_endpoint *endpoint)
165884f9bd12SAlex Elder {
165984f9bd12SAlex Elder 	struct device *dev = &endpoint->ipa->pdev->dev;
166084f9bd12SAlex Elder 	struct gsi *gsi = &endpoint->ipa->gsi;
166184f9bd12SAlex Elder 	int ret;
166284f9bd12SAlex Elder 
166384f9bd12SAlex Elder 	if (!(endpoint->ipa->enabled & BIT(endpoint->endpoint_id)))
166484f9bd12SAlex Elder 		return;
166584f9bd12SAlex Elder 
1666ab4f71e5SAlex Elder 	if (!endpoint->toward_ipa) {
166784f9bd12SAlex Elder 		ipa_endpoint_replenish_disable(endpoint);
16684fa95248SAlex Elder 		(void)ipa_endpoint_program_suspend(endpoint, true);
1669ab4f71e5SAlex Elder 	}
167084f9bd12SAlex Elder 
1671decfef0fSAlex Elder 	ret = gsi_channel_suspend(gsi, endpoint->channel_id);
167284f9bd12SAlex Elder 	if (ret)
167384f9bd12SAlex Elder 		dev_err(dev, "error %d suspending channel %u\n", ret,
167484f9bd12SAlex Elder 			endpoint->channel_id);
167584f9bd12SAlex Elder }
167684f9bd12SAlex Elder 
167784f9bd12SAlex Elder void ipa_endpoint_resume_one(struct ipa_endpoint *endpoint)
167884f9bd12SAlex Elder {
167984f9bd12SAlex Elder 	struct device *dev = &endpoint->ipa->pdev->dev;
168084f9bd12SAlex Elder 	struct gsi *gsi = &endpoint->ipa->gsi;
168184f9bd12SAlex Elder 	int ret;
168284f9bd12SAlex Elder 
168384f9bd12SAlex Elder 	if (!(endpoint->ipa->enabled & BIT(endpoint->endpoint_id)))
168484f9bd12SAlex Elder 		return;
168584f9bd12SAlex Elder 
1686b07f283eSAlex Elder 	if (!endpoint->toward_ipa)
16874fa95248SAlex Elder 		(void)ipa_endpoint_program_suspend(endpoint, false);
168884f9bd12SAlex Elder 
1689decfef0fSAlex Elder 	ret = gsi_channel_resume(gsi, endpoint->channel_id);
169084f9bd12SAlex Elder 	if (ret)
169184f9bd12SAlex Elder 		dev_err(dev, "error %d resuming channel %u\n", ret,
169284f9bd12SAlex Elder 			endpoint->channel_id);
169384f9bd12SAlex Elder 	else if (!endpoint->toward_ipa)
169484f9bd12SAlex Elder 		ipa_endpoint_replenish_enable(endpoint);
169584f9bd12SAlex Elder }
169684f9bd12SAlex Elder 
169784f9bd12SAlex Elder void ipa_endpoint_suspend(struct ipa *ipa)
169884f9bd12SAlex Elder {
1699d1704382SAlex Elder 	if (!ipa->setup_complete)
1700d1704382SAlex Elder 		return;
1701d1704382SAlex Elder 
170284f9bd12SAlex Elder 	if (ipa->modem_netdev)
170384f9bd12SAlex Elder 		ipa_modem_suspend(ipa->modem_netdev);
170484f9bd12SAlex Elder 
170584f9bd12SAlex Elder 	ipa_endpoint_suspend_one(ipa->name_map[IPA_ENDPOINT_AP_LAN_RX]);
170684f9bd12SAlex Elder 	ipa_endpoint_suspend_one(ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]);
170784f9bd12SAlex Elder }
170884f9bd12SAlex Elder 
170984f9bd12SAlex Elder void ipa_endpoint_resume(struct ipa *ipa)
171084f9bd12SAlex Elder {
1711d1704382SAlex Elder 	if (!ipa->setup_complete)
1712d1704382SAlex Elder 		return;
1713d1704382SAlex Elder 
171484f9bd12SAlex Elder 	ipa_endpoint_resume_one(ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]);
171584f9bd12SAlex Elder 	ipa_endpoint_resume_one(ipa->name_map[IPA_ENDPOINT_AP_LAN_RX]);
171684f9bd12SAlex Elder 
171784f9bd12SAlex Elder 	if (ipa->modem_netdev)
171884f9bd12SAlex Elder 		ipa_modem_resume(ipa->modem_netdev);
171984f9bd12SAlex Elder }
172084f9bd12SAlex Elder 
172184f9bd12SAlex Elder static void ipa_endpoint_setup_one(struct ipa_endpoint *endpoint)
172284f9bd12SAlex Elder {
172384f9bd12SAlex Elder 	struct gsi *gsi = &endpoint->ipa->gsi;
172484f9bd12SAlex Elder 	u32 channel_id = endpoint->channel_id;
172584f9bd12SAlex Elder 
172684f9bd12SAlex Elder 	/* Only AP endpoints get set up */
172784f9bd12SAlex Elder 	if (endpoint->ee_id != GSI_EE_AP)
172884f9bd12SAlex Elder 		return;
172984f9bd12SAlex Elder 
173084f9bd12SAlex Elder 	endpoint->trans_tre_max = gsi_channel_trans_tre_max(gsi, channel_id);
173184f9bd12SAlex Elder 	if (!endpoint->toward_ipa) {
173284f9bd12SAlex Elder 		/* RX transactions require a single TRE, so the maximum
173384f9bd12SAlex Elder 		 * backlog is the same as the maximum outstanding TREs.
173484f9bd12SAlex Elder 		 */
1735c1aaa01dSAlex Elder 		clear_bit(IPA_REPLENISH_ENABLED, endpoint->replenish_flags);
1736998c0bd2SAlex Elder 		clear_bit(IPA_REPLENISH_ACTIVE, endpoint->replenish_flags);
173784f9bd12SAlex Elder 		INIT_DELAYED_WORK(&endpoint->replenish_work,
173884f9bd12SAlex Elder 				  ipa_endpoint_replenish_work);
173984f9bd12SAlex Elder 	}
174084f9bd12SAlex Elder 
174184f9bd12SAlex Elder 	ipa_endpoint_program(endpoint);
174284f9bd12SAlex Elder 
174384f9bd12SAlex Elder 	endpoint->ipa->set_up |= BIT(endpoint->endpoint_id);
174484f9bd12SAlex Elder }
174584f9bd12SAlex Elder 
174684f9bd12SAlex Elder static void ipa_endpoint_teardown_one(struct ipa_endpoint *endpoint)
174784f9bd12SAlex Elder {
174884f9bd12SAlex Elder 	endpoint->ipa->set_up &= ~BIT(endpoint->endpoint_id);
174984f9bd12SAlex Elder 
175084f9bd12SAlex Elder 	if (!endpoint->toward_ipa)
175184f9bd12SAlex Elder 		cancel_delayed_work_sync(&endpoint->replenish_work);
175284f9bd12SAlex Elder 
175384f9bd12SAlex Elder 	ipa_endpoint_reset(endpoint);
175484f9bd12SAlex Elder }
175584f9bd12SAlex Elder 
175684f9bd12SAlex Elder void ipa_endpoint_setup(struct ipa *ipa)
175784f9bd12SAlex Elder {
175884f9bd12SAlex Elder 	u32 initialized = ipa->initialized;
175984f9bd12SAlex Elder 
176084f9bd12SAlex Elder 	ipa->set_up = 0;
176184f9bd12SAlex Elder 	while (initialized) {
176284f9bd12SAlex Elder 		u32 endpoint_id = __ffs(initialized);
176384f9bd12SAlex Elder 
176484f9bd12SAlex Elder 		initialized ^= BIT(endpoint_id);
176584f9bd12SAlex Elder 
176684f9bd12SAlex Elder 		ipa_endpoint_setup_one(&ipa->endpoint[endpoint_id]);
176784f9bd12SAlex Elder 	}
176884f9bd12SAlex Elder }
176984f9bd12SAlex Elder 
177084f9bd12SAlex Elder void ipa_endpoint_teardown(struct ipa *ipa)
177184f9bd12SAlex Elder {
177284f9bd12SAlex Elder 	u32 set_up = ipa->set_up;
177384f9bd12SAlex Elder 
177484f9bd12SAlex Elder 	while (set_up) {
177584f9bd12SAlex Elder 		u32 endpoint_id = __fls(set_up);
177684f9bd12SAlex Elder 
177784f9bd12SAlex Elder 		set_up ^= BIT(endpoint_id);
177884f9bd12SAlex Elder 
177984f9bd12SAlex Elder 		ipa_endpoint_teardown_one(&ipa->endpoint[endpoint_id]);
178084f9bd12SAlex Elder 	}
178184f9bd12SAlex Elder 	ipa->set_up = 0;
178284f9bd12SAlex Elder }
178384f9bd12SAlex Elder 
178484f9bd12SAlex Elder int ipa_endpoint_config(struct ipa *ipa)
178584f9bd12SAlex Elder {
178684f9bd12SAlex Elder 	struct device *dev = &ipa->pdev->dev;
178784f9bd12SAlex Elder 	u32 initialized;
178884f9bd12SAlex Elder 	u32 rx_base;
178984f9bd12SAlex Elder 	u32 rx_mask;
179084f9bd12SAlex Elder 	u32 tx_mask;
179184f9bd12SAlex Elder 	int ret = 0;
179284f9bd12SAlex Elder 	u32 max;
179384f9bd12SAlex Elder 	u32 val;
179484f9bd12SAlex Elder 
1795110971d1SAlex Elder 	/* Prior to IPAv3.5, the FLAVOR_0 register was not supported.
1796110971d1SAlex Elder 	 * Furthermore, the endpoints were not grouped such that TX
1797110971d1SAlex Elder 	 * endpoint numbers started with 0 and RX endpoints had numbers
1798110971d1SAlex Elder 	 * higher than all TX endpoints, so we can't do the simple
1799110971d1SAlex Elder 	 * direction check used for newer hardware below.
1800110971d1SAlex Elder 	 *
1801110971d1SAlex Elder 	 * For hardware that doesn't support the FLAVOR_0 register,
1802110971d1SAlex Elder 	 * just set the available mask to support any endpoint, and
1803110971d1SAlex Elder 	 * assume the configuration is valid.
1804110971d1SAlex Elder 	 */
1805110971d1SAlex Elder 	if (ipa->version < IPA_VERSION_3_5) {
1806110971d1SAlex Elder 		ipa->available = ~0;
1807110971d1SAlex Elder 		return 0;
1808110971d1SAlex Elder 	}
1809110971d1SAlex Elder 
181084f9bd12SAlex Elder 	/* Find out about the endpoints supplied by the hardware, and ensure
181184f9bd12SAlex Elder 	 * the highest one doesn't exceed the number we support.
181284f9bd12SAlex Elder 	 */
181384f9bd12SAlex Elder 	val = ioread32(ipa->reg_virt + IPA_REG_FLAVOR_0_OFFSET);
181484f9bd12SAlex Elder 
181584f9bd12SAlex Elder 	/* Our RX is an IPA producer */
1816716a115bSAlex Elder 	rx_base = u32_get_bits(val, IPA_PROD_LOWEST_FMASK);
1817716a115bSAlex Elder 	max = rx_base + u32_get_bits(val, IPA_MAX_PROD_PIPES_FMASK);
181884f9bd12SAlex Elder 	if (max > IPA_ENDPOINT_MAX) {
181984f9bd12SAlex Elder 		dev_err(dev, "too many endpoints (%u > %u)\n",
182084f9bd12SAlex Elder 			max, IPA_ENDPOINT_MAX);
182184f9bd12SAlex Elder 		return -EINVAL;
182284f9bd12SAlex Elder 	}
182384f9bd12SAlex Elder 	rx_mask = GENMASK(max - 1, rx_base);
182484f9bd12SAlex Elder 
182584f9bd12SAlex Elder 	/* Our TX is an IPA consumer */
1826716a115bSAlex Elder 	max = u32_get_bits(val, IPA_MAX_CONS_PIPES_FMASK);
182784f9bd12SAlex Elder 	tx_mask = GENMASK(max - 1, 0);
182884f9bd12SAlex Elder 
182984f9bd12SAlex Elder 	ipa->available = rx_mask | tx_mask;
183084f9bd12SAlex Elder 
183184f9bd12SAlex Elder 	/* Check for initialized endpoints not supported by the hardware */
183284f9bd12SAlex Elder 	if (ipa->initialized & ~ipa->available) {
183384f9bd12SAlex Elder 		dev_err(dev, "unavailable endpoint id(s) 0x%08x\n",
183484f9bd12SAlex Elder 			ipa->initialized & ~ipa->available);
183584f9bd12SAlex Elder 		ret = -EINVAL;		/* Report other errors too */
183684f9bd12SAlex Elder 	}
183784f9bd12SAlex Elder 
183884f9bd12SAlex Elder 	initialized = ipa->initialized;
183984f9bd12SAlex Elder 	while (initialized) {
184084f9bd12SAlex Elder 		u32 endpoint_id = __ffs(initialized);
184184f9bd12SAlex Elder 		struct ipa_endpoint *endpoint;
184284f9bd12SAlex Elder 
184384f9bd12SAlex Elder 		initialized ^= BIT(endpoint_id);
184484f9bd12SAlex Elder 
184584f9bd12SAlex Elder 		/* Make sure it's pointing in the right direction */
184684f9bd12SAlex Elder 		endpoint = &ipa->endpoint[endpoint_id];
1847602a1c76SAlex Elder 		if ((endpoint_id < rx_base) != endpoint->toward_ipa) {
184884f9bd12SAlex Elder 			dev_err(dev, "endpoint id %u wrong direction\n",
184984f9bd12SAlex Elder 				endpoint_id);
185084f9bd12SAlex Elder 			ret = -EINVAL;
185184f9bd12SAlex Elder 		}
185284f9bd12SAlex Elder 	}
185384f9bd12SAlex Elder 
185484f9bd12SAlex Elder 	return ret;
185584f9bd12SAlex Elder }
185684f9bd12SAlex Elder 
185784f9bd12SAlex Elder void ipa_endpoint_deconfig(struct ipa *ipa)
185884f9bd12SAlex Elder {
185984f9bd12SAlex Elder 	ipa->available = 0;	/* Nothing more to do */
186084f9bd12SAlex Elder }
186184f9bd12SAlex Elder 
186284f9bd12SAlex Elder static void ipa_endpoint_init_one(struct ipa *ipa, enum ipa_endpoint_name name,
186384f9bd12SAlex Elder 				  const struct ipa_gsi_endpoint_data *data)
186484f9bd12SAlex Elder {
186584f9bd12SAlex Elder 	struct ipa_endpoint *endpoint;
186684f9bd12SAlex Elder 
186784f9bd12SAlex Elder 	endpoint = &ipa->endpoint[data->endpoint_id];
186884f9bd12SAlex Elder 
186984f9bd12SAlex Elder 	if (data->ee_id == GSI_EE_AP)
187084f9bd12SAlex Elder 		ipa->channel_map[data->channel_id] = endpoint;
187184f9bd12SAlex Elder 	ipa->name_map[name] = endpoint;
187284f9bd12SAlex Elder 
187384f9bd12SAlex Elder 	endpoint->ipa = ipa;
187484f9bd12SAlex Elder 	endpoint->ee_id = data->ee_id;
187584f9bd12SAlex Elder 	endpoint->channel_id = data->channel_id;
187684f9bd12SAlex Elder 	endpoint->endpoint_id = data->endpoint_id;
187784f9bd12SAlex Elder 	endpoint->toward_ipa = data->toward_ipa;
1878660e52d6SAlex Elder 	endpoint->config = data->endpoint.config;
187984f9bd12SAlex Elder 
188084f9bd12SAlex Elder 	ipa->initialized |= BIT(endpoint->endpoint_id);
188184f9bd12SAlex Elder }
188284f9bd12SAlex Elder 
1883602a1c76SAlex Elder static void ipa_endpoint_exit_one(struct ipa_endpoint *endpoint)
188484f9bd12SAlex Elder {
188584f9bd12SAlex Elder 	endpoint->ipa->initialized &= ~BIT(endpoint->endpoint_id);
188684f9bd12SAlex Elder 
188784f9bd12SAlex Elder 	memset(endpoint, 0, sizeof(*endpoint));
188884f9bd12SAlex Elder }
188984f9bd12SAlex Elder 
189084f9bd12SAlex Elder void ipa_endpoint_exit(struct ipa *ipa)
189184f9bd12SAlex Elder {
189284f9bd12SAlex Elder 	u32 initialized = ipa->initialized;
189384f9bd12SAlex Elder 
189484f9bd12SAlex Elder 	while (initialized) {
189584f9bd12SAlex Elder 		u32 endpoint_id = __fls(initialized);
189684f9bd12SAlex Elder 
189784f9bd12SAlex Elder 		initialized ^= BIT(endpoint_id);
189884f9bd12SAlex Elder 
189984f9bd12SAlex Elder 		ipa_endpoint_exit_one(&ipa->endpoint[endpoint_id]);
190084f9bd12SAlex Elder 	}
190184f9bd12SAlex Elder 	memset(ipa->name_map, 0, sizeof(ipa->name_map));
190284f9bd12SAlex Elder 	memset(ipa->channel_map, 0, sizeof(ipa->channel_map));
190384f9bd12SAlex Elder }
190484f9bd12SAlex Elder 
190584f9bd12SAlex Elder /* Returns a bitmask of endpoints that support filtering, or 0 on error */
190684f9bd12SAlex Elder u32 ipa_endpoint_init(struct ipa *ipa, u32 count,
190784f9bd12SAlex Elder 		      const struct ipa_gsi_endpoint_data *data)
190884f9bd12SAlex Elder {
190984f9bd12SAlex Elder 	enum ipa_endpoint_name name;
191084f9bd12SAlex Elder 	u32 filter_map;
191184f9bd12SAlex Elder 
19129654d8c4SAlex Elder 	BUILD_BUG_ON(!IPA_REPLENISH_BATCH);
19139654d8c4SAlex Elder 
191484f9bd12SAlex Elder 	if (!ipa_endpoint_data_valid(ipa, count, data))
191584f9bd12SAlex Elder 		return 0;	/* Error */
191684f9bd12SAlex Elder 
191784f9bd12SAlex Elder 	ipa->initialized = 0;
191884f9bd12SAlex Elder 
191984f9bd12SAlex Elder 	filter_map = 0;
192084f9bd12SAlex Elder 	for (name = 0; name < count; name++, data++) {
192184f9bd12SAlex Elder 		if (ipa_gsi_endpoint_data_empty(data))
192284f9bd12SAlex Elder 			continue;	/* Skip over empty slots */
192384f9bd12SAlex Elder 
192484f9bd12SAlex Elder 		ipa_endpoint_init_one(ipa, name, data);
192584f9bd12SAlex Elder 
192684f9bd12SAlex Elder 		if (data->endpoint.filter_support)
192784f9bd12SAlex Elder 			filter_map |= BIT(data->endpoint_id);
192884f9bd12SAlex Elder 	}
192984f9bd12SAlex Elder 
193084f9bd12SAlex Elder 	if (!ipa_filter_map_valid(ipa, filter_map))
193184f9bd12SAlex Elder 		goto err_endpoint_exit;
193284f9bd12SAlex Elder 
193384f9bd12SAlex Elder 	return filter_map;	/* Non-zero bitmask */
193484f9bd12SAlex Elder 
193584f9bd12SAlex Elder err_endpoint_exit:
193684f9bd12SAlex Elder 	ipa_endpoint_exit(ipa);
193784f9bd12SAlex Elder 
193884f9bd12SAlex Elder 	return 0;	/* Error */
193984f9bd12SAlex Elder }
1940