xref: /linux/drivers/net/ipa/ipa_endpoint.c (revision 6a606b90153b821915daade0b8f253d01d443d75)
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 
2884f9bd12SAlex Elder #define IPA_REPLENISH_BATCH	16
2984f9bd12SAlex Elder 
3084f9bd12SAlex Elder /* The amount of RX buffer space consumed by standard skb overhead */
3184f9bd12SAlex Elder #define IPA_RX_BUFFER_OVERHEAD	(PAGE_SIZE - SKB_MAX_ORDER(NET_SKB_PAD, 0))
3284f9bd12SAlex Elder 
338730f45dSAlex Elder /* Where to find the QMAP mux_id for a packet within modem-supplied metadata */
348730f45dSAlex Elder #define IPA_ENDPOINT_QMAP_METADATA_MASK		0x000000ff /* host byte order */
358730f45dSAlex Elder 
3684f9bd12SAlex Elder #define IPA_ENDPOINT_RESET_AGGR_RETRY_MAX	3
376bf754c7SAlex Elder #define IPA_AGGR_TIME_LIMIT			500	/* microseconds */
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 
8384f9bd12SAlex Elder static bool ipa_endpoint_data_valid_one(struct ipa *ipa, u32 count,
8484f9bd12SAlex Elder 			    const struct ipa_gsi_endpoint_data *all_data,
8584f9bd12SAlex Elder 			    const struct ipa_gsi_endpoint_data *data)
8684f9bd12SAlex Elder {
8784f9bd12SAlex Elder 	const struct ipa_gsi_endpoint_data *other_data;
8884f9bd12SAlex Elder 	struct device *dev = &ipa->pdev->dev;
8984f9bd12SAlex Elder 	enum ipa_endpoint_name other_name;
9084f9bd12SAlex Elder 
9184f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(data))
9284f9bd12SAlex Elder 		return true;
9384f9bd12SAlex Elder 
9484f9bd12SAlex Elder 	if (!data->toward_ipa) {
95ed23f026SAlex Elder 		u32 buffer_size;
96ed23f026SAlex Elder 		u32 limit;
97ed23f026SAlex Elder 
9884f9bd12SAlex Elder 		if (data->endpoint.filter_support) {
9984f9bd12SAlex Elder 			dev_err(dev, "filtering not supported for "
10084f9bd12SAlex Elder 					"RX endpoint %u\n",
10184f9bd12SAlex Elder 				data->endpoint_id);
10284f9bd12SAlex Elder 			return false;
10384f9bd12SAlex Elder 		}
10484f9bd12SAlex Elder 
105ed23f026SAlex Elder 		/* Nothing more to check for non-AP RX */
106ed23f026SAlex Elder 		if (data->ee_id != GSI_EE_AP)
107ed23f026SAlex Elder 			return true;
108ed23f026SAlex Elder 
109ed23f026SAlex Elder 		buffer_size = data->endpoint.config.rx.buffer_size;
110ed23f026SAlex Elder 		/* The buffer size must hold an MTU plus overhead */
111ed23f026SAlex Elder 		limit = IPA_MTU + IPA_RX_BUFFER_OVERHEAD;
112ed23f026SAlex Elder 		if (buffer_size < limit) {
113ed23f026SAlex Elder 			dev_err(dev, "RX buffer size too small for RX endpoint %u (%u < %u)\n",
114ed23f026SAlex Elder 				data->endpoint_id, buffer_size, limit);
115ed23f026SAlex Elder 			return false;
116ed23f026SAlex Elder 		}
117ed23f026SAlex Elder 
118ed23f026SAlex Elder 		/* For an endpoint supporting receive aggregation, the
119ed23f026SAlex Elder 		 * aggregation byte limit defines the point at which an
120ed23f026SAlex Elder 		 * aggregation window will close.  It is programmed into the
121ed23f026SAlex Elder 		 * IPA hardware as a number of KB.  We don't use "hard byte
122ed23f026SAlex Elder 		 * limit" aggregation, so we need to supply enough space in
123ed23f026SAlex Elder 		 * a receive buffer to hold a complete MTU plus normal skb
124ed23f026SAlex Elder 		 * overhead *after* that aggregation byte limit has been
125ed23f026SAlex Elder 		 * crossed.
126ed23f026SAlex Elder 		 *
127ed23f026SAlex Elder 		 * This check just ensures the receive buffer size doesn't
128ed23f026SAlex Elder 		 * exceed what's representable in the aggregation limit field.
129ed23f026SAlex Elder 		 */
130ed23f026SAlex Elder 		if (data->endpoint.config.aggregation) {
131ed23f026SAlex Elder 			limit += SZ_1K * aggr_byte_limit_max(ipa->version);
132ed23f026SAlex Elder 			if (buffer_size > limit) {
133ed23f026SAlex Elder 				dev_err(dev, "RX buffer size too large for aggregated RX endpoint %u (%u > %u)\n",
134ed23f026SAlex Elder 					data->endpoint_id, buffer_size, limit);
135ed23f026SAlex Elder 
136ed23f026SAlex Elder 				return false;
137ed23f026SAlex Elder 			}
138ed23f026SAlex Elder 		}
139ed23f026SAlex Elder 
14084f9bd12SAlex Elder 		return true;	/* Nothing more to check for RX */
14184f9bd12SAlex Elder 	}
14284f9bd12SAlex Elder 
14384f9bd12SAlex Elder 	if (data->endpoint.config.status_enable) {
14484f9bd12SAlex Elder 		other_name = data->endpoint.config.tx.status_endpoint;
14584f9bd12SAlex Elder 		if (other_name >= count) {
14684f9bd12SAlex Elder 			dev_err(dev, "status endpoint name %u out of range "
14784f9bd12SAlex Elder 					"for endpoint %u\n",
14884f9bd12SAlex Elder 				other_name, data->endpoint_id);
14984f9bd12SAlex Elder 			return false;
15084f9bd12SAlex Elder 		}
15184f9bd12SAlex Elder 
15284f9bd12SAlex Elder 		/* Status endpoint must be defined... */
15384f9bd12SAlex Elder 		other_data = &all_data[other_name];
15484f9bd12SAlex Elder 		if (ipa_gsi_endpoint_data_empty(other_data)) {
15584f9bd12SAlex Elder 			dev_err(dev, "DMA endpoint name %u undefined "
15684f9bd12SAlex Elder 					"for endpoint %u\n",
15784f9bd12SAlex Elder 				other_name, data->endpoint_id);
15884f9bd12SAlex Elder 			return false;
15984f9bd12SAlex Elder 		}
16084f9bd12SAlex Elder 
16184f9bd12SAlex Elder 		/* ...and has to be an RX endpoint... */
16284f9bd12SAlex Elder 		if (other_data->toward_ipa) {
16384f9bd12SAlex Elder 			dev_err(dev,
16484f9bd12SAlex Elder 				"status endpoint for endpoint %u not RX\n",
16584f9bd12SAlex Elder 				data->endpoint_id);
16684f9bd12SAlex Elder 			return false;
16784f9bd12SAlex Elder 		}
16884f9bd12SAlex Elder 
16984f9bd12SAlex Elder 		/* ...and if it's to be an AP endpoint... */
17084f9bd12SAlex Elder 		if (other_data->ee_id == GSI_EE_AP) {
17184f9bd12SAlex Elder 			/* ...make sure it has status enabled. */
17284f9bd12SAlex Elder 			if (!other_data->endpoint.config.status_enable) {
17384f9bd12SAlex Elder 				dev_err(dev,
17484f9bd12SAlex Elder 					"status not enabled for endpoint %u\n",
17584f9bd12SAlex Elder 					other_data->endpoint_id);
17684f9bd12SAlex Elder 				return false;
17784f9bd12SAlex Elder 			}
17884f9bd12SAlex Elder 		}
17984f9bd12SAlex Elder 	}
18084f9bd12SAlex Elder 
18184f9bd12SAlex Elder 	if (data->endpoint.config.dma_mode) {
18284f9bd12SAlex Elder 		other_name = data->endpoint.config.dma_endpoint;
18384f9bd12SAlex Elder 		if (other_name >= count) {
18484f9bd12SAlex Elder 			dev_err(dev, "DMA endpoint name %u out of range "
18584f9bd12SAlex Elder 					"for endpoint %u\n",
18684f9bd12SAlex Elder 				other_name, data->endpoint_id);
18784f9bd12SAlex Elder 			return false;
18884f9bd12SAlex Elder 		}
18984f9bd12SAlex Elder 
19084f9bd12SAlex Elder 		other_data = &all_data[other_name];
19184f9bd12SAlex Elder 		if (ipa_gsi_endpoint_data_empty(other_data)) {
19284f9bd12SAlex Elder 			dev_err(dev, "DMA endpoint name %u undefined "
19384f9bd12SAlex Elder 					"for endpoint %u\n",
19484f9bd12SAlex Elder 				other_name, data->endpoint_id);
19584f9bd12SAlex Elder 			return false;
19684f9bd12SAlex Elder 		}
19784f9bd12SAlex Elder 	}
19884f9bd12SAlex Elder 
19984f9bd12SAlex Elder 	return true;
20084f9bd12SAlex Elder }
20184f9bd12SAlex Elder 
20284f9bd12SAlex Elder static bool ipa_endpoint_data_valid(struct ipa *ipa, u32 count,
20384f9bd12SAlex Elder 				    const struct ipa_gsi_endpoint_data *data)
20484f9bd12SAlex Elder {
20584f9bd12SAlex Elder 	const struct ipa_gsi_endpoint_data *dp = data;
20684f9bd12SAlex Elder 	struct device *dev = &ipa->pdev->dev;
20784f9bd12SAlex Elder 	enum ipa_endpoint_name name;
20884f9bd12SAlex Elder 
20984f9bd12SAlex Elder 	if (count > IPA_ENDPOINT_COUNT) {
21084f9bd12SAlex Elder 		dev_err(dev, "too many endpoints specified (%u > %u)\n",
21184f9bd12SAlex Elder 			count, IPA_ENDPOINT_COUNT);
21284f9bd12SAlex Elder 		return false;
21384f9bd12SAlex Elder 	}
21484f9bd12SAlex Elder 
21584f9bd12SAlex Elder 	/* Make sure needed endpoints have defined data */
21684f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_COMMAND_TX])) {
21784f9bd12SAlex Elder 		dev_err(dev, "command TX endpoint not defined\n");
21884f9bd12SAlex Elder 		return false;
21984f9bd12SAlex Elder 	}
22084f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_LAN_RX])) {
22184f9bd12SAlex Elder 		dev_err(dev, "LAN RX endpoint not defined\n");
22284f9bd12SAlex Elder 		return false;
22384f9bd12SAlex Elder 	}
22484f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_MODEM_TX])) {
22584f9bd12SAlex Elder 		dev_err(dev, "AP->modem TX endpoint not defined\n");
22684f9bd12SAlex Elder 		return false;
22784f9bd12SAlex Elder 	}
22884f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_MODEM_RX])) {
22984f9bd12SAlex Elder 		dev_err(dev, "AP<-modem RX endpoint not defined\n");
23084f9bd12SAlex Elder 		return false;
23184f9bd12SAlex Elder 	}
23284f9bd12SAlex Elder 
23384f9bd12SAlex Elder 	for (name = 0; name < count; name++, dp++)
23484f9bd12SAlex Elder 		if (!ipa_endpoint_data_valid_one(ipa, count, data, dp))
23584f9bd12SAlex Elder 			return false;
23684f9bd12SAlex Elder 
23784f9bd12SAlex Elder 	return true;
23884f9bd12SAlex Elder }
23984f9bd12SAlex Elder 
24084f9bd12SAlex Elder /* Allocate a transaction to use on a non-command endpoint */
24184f9bd12SAlex Elder static struct gsi_trans *ipa_endpoint_trans_alloc(struct ipa_endpoint *endpoint,
24284f9bd12SAlex Elder 						  u32 tre_count)
24384f9bd12SAlex Elder {
24484f9bd12SAlex Elder 	struct gsi *gsi = &endpoint->ipa->gsi;
24584f9bd12SAlex Elder 	u32 channel_id = endpoint->channel_id;
24684f9bd12SAlex Elder 	enum dma_data_direction direction;
24784f9bd12SAlex Elder 
24884f9bd12SAlex Elder 	direction = endpoint->toward_ipa ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
24984f9bd12SAlex Elder 
25084f9bd12SAlex Elder 	return gsi_channel_trans_alloc(gsi, channel_id, tre_count, direction);
25184f9bd12SAlex Elder }
25284f9bd12SAlex Elder 
25384f9bd12SAlex Elder /* suspend_delay represents suspend for RX, delay for TX endpoints.
2544c9d631aSAlex Elder  * Note that suspend is not supported starting with IPA v4.0, and
2554c9d631aSAlex Elder  * delay mode should not be used starting with IPA v4.2.
25684f9bd12SAlex Elder  */
2574900bf34SAlex Elder static bool
25884f9bd12SAlex Elder ipa_endpoint_init_ctrl(struct ipa_endpoint *endpoint, bool suspend_delay)
25984f9bd12SAlex Elder {
26084f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_CTRL_N_OFFSET(endpoint->endpoint_id);
26184f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
2624900bf34SAlex Elder 	bool state;
26384f9bd12SAlex Elder 	u32 mask;
26484f9bd12SAlex Elder 	u32 val;
26584f9bd12SAlex Elder 
2665bc55884SAlex Elder 	if (endpoint->toward_ipa)
2674c9d631aSAlex Elder 		WARN_ON(ipa->version >= IPA_VERSION_4_2);
2685bc55884SAlex Elder 	else
2695bc55884SAlex Elder 		WARN_ON(ipa->version >= IPA_VERSION_4_0);
2705bc55884SAlex Elder 
27184f9bd12SAlex Elder 	mask = endpoint->toward_ipa ? ENDP_DELAY_FMASK : ENDP_SUSPEND_FMASK;
27284f9bd12SAlex Elder 
27384f9bd12SAlex Elder 	val = ioread32(ipa->reg_virt + offset);
2744900bf34SAlex Elder 	state = !!(val & mask);
2755bc55884SAlex Elder 
2765bc55884SAlex Elder 	/* Don't bother if it's already in the requested state */
2774900bf34SAlex Elder 	if (suspend_delay != state) {
27884f9bd12SAlex Elder 		val ^= mask;
27984f9bd12SAlex Elder 		iowrite32(val, ipa->reg_virt + offset);
2804900bf34SAlex Elder 	}
28184f9bd12SAlex Elder 
2824900bf34SAlex Elder 	return state;
28384f9bd12SAlex Elder }
28484f9bd12SAlex Elder 
2854c9d631aSAlex Elder /* We don't care what the previous state was for delay mode */
2864fa95248SAlex Elder static void
2874fa95248SAlex Elder ipa_endpoint_program_delay(struct ipa_endpoint *endpoint, bool enable)
2884fa95248SAlex Elder {
2894c9d631aSAlex Elder 	/* Delay mode should not be used for IPA v4.2+ */
2904c9d631aSAlex Elder 	WARN_ON(endpoint->ipa->version >= IPA_VERSION_4_2);
2915bc55884SAlex Elder 	WARN_ON(!endpoint->toward_ipa);
2924fa95248SAlex Elder 
2934fa95248SAlex Elder 	(void)ipa_endpoint_init_ctrl(endpoint, enable);
2944fa95248SAlex Elder }
2954fa95248SAlex Elder 
296fff89971SAlex Elder static bool ipa_endpoint_aggr_active(struct ipa_endpoint *endpoint)
297fff89971SAlex Elder {
298fff89971SAlex Elder 	u32 mask = BIT(endpoint->endpoint_id);
299fff89971SAlex Elder 	struct ipa *ipa = endpoint->ipa;
300fff89971SAlex Elder 	u32 offset;
301fff89971SAlex Elder 	u32 val;
302fff89971SAlex Elder 
3035bc55884SAlex Elder 	WARN_ON(!(mask & ipa->available));
3045bc55884SAlex Elder 
305fff89971SAlex Elder 	offset = ipa_reg_state_aggr_active_offset(ipa->version);
306fff89971SAlex Elder 	val = ioread32(ipa->reg_virt + offset);
307fff89971SAlex Elder 
308fff89971SAlex Elder 	return !!(val & mask);
309fff89971SAlex Elder }
310fff89971SAlex Elder 
311fff89971SAlex Elder static void ipa_endpoint_force_close(struct ipa_endpoint *endpoint)
312fff89971SAlex Elder {
313fff89971SAlex Elder 	u32 mask = BIT(endpoint->endpoint_id);
314fff89971SAlex Elder 	struct ipa *ipa = endpoint->ipa;
315fff89971SAlex Elder 
3165bc55884SAlex Elder 	WARN_ON(!(mask & ipa->available));
3175bc55884SAlex Elder 
318fff89971SAlex Elder 	iowrite32(mask, ipa->reg_virt + IPA_REG_AGGR_FORCE_CLOSE_OFFSET);
319fff89971SAlex Elder }
320fff89971SAlex Elder 
321fff89971SAlex Elder /**
322fff89971SAlex Elder  * ipa_endpoint_suspend_aggr() - Emulate suspend interrupt
323e3eea08eSAlex Elder  * @endpoint:	Endpoint on which to emulate a suspend
324fff89971SAlex Elder  *
325fff89971SAlex Elder  *  Emulate suspend IPA interrupt to unsuspend an endpoint suspended
326fff89971SAlex Elder  *  with an open aggregation frame.  This is to work around a hardware
327fff89971SAlex Elder  *  issue in IPA version 3.5.1 where the suspend interrupt will not be
328fff89971SAlex Elder  *  generated when it should be.
329fff89971SAlex Elder  */
330fff89971SAlex Elder static void ipa_endpoint_suspend_aggr(struct ipa_endpoint *endpoint)
331fff89971SAlex Elder {
332fff89971SAlex Elder 	struct ipa *ipa = endpoint->ipa;
333fff89971SAlex Elder 
334fff89971SAlex Elder 	if (!endpoint->data->aggregation)
335fff89971SAlex Elder 		return;
336fff89971SAlex Elder 
337fff89971SAlex Elder 	/* Nothing to do if the endpoint doesn't have aggregation open */
338fff89971SAlex Elder 	if (!ipa_endpoint_aggr_active(endpoint))
339fff89971SAlex Elder 		return;
340fff89971SAlex Elder 
341fff89971SAlex Elder 	/* Force close aggregation */
342fff89971SAlex Elder 	ipa_endpoint_force_close(endpoint);
343fff89971SAlex Elder 
344fff89971SAlex Elder 	ipa_interrupt_simulate_suspend(ipa->interrupt);
345fff89971SAlex Elder }
346fff89971SAlex Elder 
347fff89971SAlex Elder /* Returns previous suspend state (true means suspend was enabled) */
3484fa95248SAlex Elder static bool
3494fa95248SAlex Elder ipa_endpoint_program_suspend(struct ipa_endpoint *endpoint, bool enable)
3504fa95248SAlex Elder {
351fff89971SAlex Elder 	bool suspended;
352fff89971SAlex Elder 
353d7f3087bSAlex Elder 	if (endpoint->ipa->version >= IPA_VERSION_4_0)
354b07f283eSAlex Elder 		return enable;	/* For IPA v4.0+, no change made */
355b07f283eSAlex Elder 
3565bc55884SAlex Elder 	WARN_ON(endpoint->toward_ipa);
3574fa95248SAlex Elder 
358fff89971SAlex Elder 	suspended = ipa_endpoint_init_ctrl(endpoint, enable);
359fff89971SAlex Elder 
360fff89971SAlex Elder 	/* A client suspended with an open aggregation frame will not
361fff89971SAlex Elder 	 * generate a SUSPEND IPA interrupt.  If enabling suspend, have
362fff89971SAlex Elder 	 * ipa_endpoint_suspend_aggr() handle this.
363fff89971SAlex Elder 	 */
364fff89971SAlex Elder 	if (enable && !suspended)
365fff89971SAlex Elder 		ipa_endpoint_suspend_aggr(endpoint);
366fff89971SAlex Elder 
367fff89971SAlex Elder 	return suspended;
3684fa95248SAlex Elder }
3694fa95248SAlex Elder 
3704c9d631aSAlex Elder /* Put all modem RX endpoints into suspend mode, and stop transmission
3714c9d631aSAlex Elder  * on all modem TX endpoints.  Prior to IPA v4.2, endpoint DELAY mode is
3724c9d631aSAlex Elder  * used for TX endpoints; starting with IPA v4.2 we use GSI channel flow
3734c9d631aSAlex Elder  * control instead.
3744c9d631aSAlex Elder  */
37584f9bd12SAlex Elder void ipa_endpoint_modem_pause_all(struct ipa *ipa, bool enable)
37684f9bd12SAlex Elder {
37784f9bd12SAlex Elder 	u32 endpoint_id;
37884f9bd12SAlex Elder 
37984f9bd12SAlex Elder 	for (endpoint_id = 0; endpoint_id < IPA_ENDPOINT_MAX; endpoint_id++) {
38084f9bd12SAlex Elder 		struct ipa_endpoint *endpoint = &ipa->endpoint[endpoint_id];
38184f9bd12SAlex Elder 
38284f9bd12SAlex Elder 		if (endpoint->ee_id != GSI_EE_MODEM)
38384f9bd12SAlex Elder 			continue;
38484f9bd12SAlex Elder 
3854c9d631aSAlex Elder 		if (!endpoint->toward_ipa)
3864c9d631aSAlex Elder 			(void)ipa_endpoint_program_suspend(endpoint, enable);
3874c9d631aSAlex Elder 		else if (ipa->version < IPA_VERSION_4_2)
3884fa95248SAlex Elder 			ipa_endpoint_program_delay(endpoint, enable);
389b07f283eSAlex Elder 		else
3904c9d631aSAlex Elder 			gsi_modem_channel_flow_control(&ipa->gsi,
3914c9d631aSAlex Elder 						       endpoint->channel_id,
3924c9d631aSAlex Elder 						       enable);
39384f9bd12SAlex Elder 	}
39484f9bd12SAlex Elder }
39584f9bd12SAlex Elder 
39684f9bd12SAlex Elder /* Reset all modem endpoints to use the default exception endpoint */
39784f9bd12SAlex Elder int ipa_endpoint_modem_exception_reset_all(struct ipa *ipa)
39884f9bd12SAlex Elder {
39984f9bd12SAlex Elder 	u32 initialized = ipa->initialized;
40084f9bd12SAlex Elder 	struct gsi_trans *trans;
40184f9bd12SAlex Elder 	u32 count;
40284f9bd12SAlex Elder 
40384f9bd12SAlex Elder 	/* We need one command per modem TX endpoint.  We can get an upper
40484f9bd12SAlex Elder 	 * bound on that by assuming all initialized endpoints are modem->IPA.
40584f9bd12SAlex Elder 	 * That won't happen, and we could be more precise, but this is fine
406602a1c76SAlex Elder 	 * for now.  End the transaction with commands to clear the pipeline.
40784f9bd12SAlex Elder 	 */
408aa56e3e5SAlex Elder 	count = hweight32(initialized) + ipa_cmd_pipeline_clear_count();
40984f9bd12SAlex Elder 	trans = ipa_cmd_trans_alloc(ipa, count);
41084f9bd12SAlex Elder 	if (!trans) {
41184f9bd12SAlex Elder 		dev_err(&ipa->pdev->dev,
41284f9bd12SAlex Elder 			"no transaction to reset modem exception endpoints\n");
41384f9bd12SAlex Elder 		return -EBUSY;
41484f9bd12SAlex Elder 	}
41584f9bd12SAlex Elder 
41684f9bd12SAlex Elder 	while (initialized) {
41784f9bd12SAlex Elder 		u32 endpoint_id = __ffs(initialized);
41884f9bd12SAlex Elder 		struct ipa_endpoint *endpoint;
41984f9bd12SAlex Elder 		u32 offset;
42084f9bd12SAlex Elder 
42184f9bd12SAlex Elder 		initialized ^= BIT(endpoint_id);
42284f9bd12SAlex Elder 
42384f9bd12SAlex Elder 		/* We only reset modem TX endpoints */
42484f9bd12SAlex Elder 		endpoint = &ipa->endpoint[endpoint_id];
42584f9bd12SAlex Elder 		if (!(endpoint->ee_id == GSI_EE_MODEM && endpoint->toward_ipa))
42684f9bd12SAlex Elder 			continue;
42784f9bd12SAlex Elder 
42884f9bd12SAlex Elder 		offset = IPA_REG_ENDP_STATUS_N_OFFSET(endpoint_id);
42984f9bd12SAlex Elder 
43084f9bd12SAlex Elder 		/* Value written is 0, and all bits are updated.  That
43184f9bd12SAlex Elder 		 * means status is disabled on the endpoint, and as a
43284f9bd12SAlex Elder 		 * result all other fields in the register are ignored.
43384f9bd12SAlex Elder 		 */
43484f9bd12SAlex Elder 		ipa_cmd_register_write_add(trans, offset, 0, ~0, false);
43584f9bd12SAlex Elder 	}
43684f9bd12SAlex Elder 
437aa56e3e5SAlex Elder 	ipa_cmd_pipeline_clear_add(trans);
43884f9bd12SAlex Elder 
43984f9bd12SAlex Elder 	/* XXX This should have a 1 second timeout */
44084f9bd12SAlex Elder 	gsi_trans_commit_wait(trans);
44184f9bd12SAlex Elder 
44251c48ce2SAlex Elder 	ipa_cmd_pipeline_clear_wait(ipa);
44351c48ce2SAlex Elder 
44484f9bd12SAlex Elder 	return 0;
44584f9bd12SAlex Elder }
44684f9bd12SAlex Elder 
44784f9bd12SAlex Elder static void ipa_endpoint_init_cfg(struct ipa_endpoint *endpoint)
44884f9bd12SAlex Elder {
44984f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_CFG_N_OFFSET(endpoint->endpoint_id);
4505567d4d9SAlex Elder 	enum ipa_cs_offload_en enabled;
45184f9bd12SAlex Elder 	u32 val = 0;
45284f9bd12SAlex Elder 
45384f9bd12SAlex Elder 	/* FRAG_OFFLOAD_EN is 0 */
45484f9bd12SAlex Elder 	if (endpoint->data->checksum) {
4555567d4d9SAlex Elder 		enum ipa_version version = endpoint->ipa->version;
4565567d4d9SAlex Elder 
45784f9bd12SAlex Elder 		if (endpoint->toward_ipa) {
45884f9bd12SAlex Elder 			u32 checksum_offset;
45984f9bd12SAlex Elder 
46084f9bd12SAlex Elder 			/* Checksum header offset is in 4-byte units */
46184f9bd12SAlex Elder 			checksum_offset = sizeof(struct rmnet_map_header);
46284f9bd12SAlex Elder 			checksum_offset /= sizeof(u32);
46384f9bd12SAlex Elder 			val |= u32_encode_bits(checksum_offset,
46484f9bd12SAlex Elder 					       CS_METADATA_HDR_OFFSET_FMASK);
4655567d4d9SAlex Elder 
4665567d4d9SAlex Elder 			enabled = version < IPA_VERSION_4_5
4675567d4d9SAlex Elder 					? IPA_CS_OFFLOAD_UL
4685567d4d9SAlex Elder 					: IPA_CS_OFFLOAD_INLINE;
46984f9bd12SAlex Elder 		} else {
4705567d4d9SAlex Elder 			enabled = version < IPA_VERSION_4_5
4715567d4d9SAlex Elder 					? IPA_CS_OFFLOAD_DL
4725567d4d9SAlex Elder 					: IPA_CS_OFFLOAD_INLINE;
47384f9bd12SAlex Elder 		}
47484f9bd12SAlex Elder 	} else {
4755567d4d9SAlex Elder 		enabled = IPA_CS_OFFLOAD_NONE;
47684f9bd12SAlex Elder 	}
4775567d4d9SAlex Elder 	val |= u32_encode_bits(enabled, CS_OFFLOAD_EN_FMASK);
47884f9bd12SAlex Elder 	/* CS_GEN_QMB_MASTER_SEL is 0 */
47984f9bd12SAlex Elder 
48084f9bd12SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
48184f9bd12SAlex Elder }
48284f9bd12SAlex Elder 
483647a05f3SAlex Elder static void ipa_endpoint_init_nat(struct ipa_endpoint *endpoint)
484647a05f3SAlex Elder {
485647a05f3SAlex Elder 	u32 offset;
486647a05f3SAlex Elder 	u32 val;
487647a05f3SAlex Elder 
488647a05f3SAlex Elder 	if (!endpoint->toward_ipa)
489647a05f3SAlex Elder 		return;
490647a05f3SAlex Elder 
491647a05f3SAlex Elder 	offset = IPA_REG_ENDP_INIT_NAT_N_OFFSET(endpoint->endpoint_id);
492647a05f3SAlex Elder 	val = u32_encode_bits(IPA_NAT_BYPASS, NAT_EN_FMASK);
493647a05f3SAlex Elder 
494647a05f3SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
495647a05f3SAlex Elder }
496647a05f3SAlex Elder 
4975567d4d9SAlex Elder static u32
4985567d4d9SAlex Elder ipa_qmap_header_size(enum ipa_version version, struct ipa_endpoint *endpoint)
4995567d4d9SAlex Elder {
5005567d4d9SAlex Elder 	u32 header_size = sizeof(struct rmnet_map_header);
5015567d4d9SAlex Elder 
5025567d4d9SAlex Elder 	/* Without checksum offload, we just have the MAP header */
5035567d4d9SAlex Elder 	if (!endpoint->data->checksum)
5045567d4d9SAlex Elder 		return header_size;
5055567d4d9SAlex Elder 
5065567d4d9SAlex Elder 	if (version < IPA_VERSION_4_5) {
5075567d4d9SAlex Elder 		/* Checksum header inserted for AP TX endpoints only */
5085567d4d9SAlex Elder 		if (endpoint->toward_ipa)
5095567d4d9SAlex Elder 			header_size += sizeof(struct rmnet_map_ul_csum_header);
5105567d4d9SAlex Elder 	} else {
5115567d4d9SAlex Elder 		/* Checksum header is used in both directions */
5125567d4d9SAlex Elder 		header_size += sizeof(struct rmnet_map_v5_csum_header);
5135567d4d9SAlex Elder 	}
5145567d4d9SAlex Elder 
5155567d4d9SAlex Elder 	return header_size;
5165567d4d9SAlex Elder }
5175567d4d9SAlex Elder 
5188730f45dSAlex Elder /**
519e3eea08eSAlex Elder  * ipa_endpoint_init_hdr() - Initialize HDR endpoint configuration register
520e3eea08eSAlex Elder  * @endpoint:	Endpoint pointer
521e3eea08eSAlex Elder  *
5228730f45dSAlex Elder  * We program QMAP endpoints so each packet received is preceded by a QMAP
5238730f45dSAlex Elder  * header structure.  The QMAP header contains a 1-byte mux_id and 2-byte
5248730f45dSAlex Elder  * packet size field, and we have the IPA hardware populate both for each
5258730f45dSAlex Elder  * received packet.  The header is configured (in the HDR_EXT register)
5268730f45dSAlex Elder  * to use big endian format.
5278730f45dSAlex Elder  *
5288730f45dSAlex Elder  * The packet size is written into the QMAP header's pkt_len field.  That
5298730f45dSAlex Elder  * location is defined here using the HDR_OFST_PKT_SIZE field.
5308730f45dSAlex Elder  *
5318730f45dSAlex Elder  * The mux_id comes from a 4-byte metadata value supplied with each packet
5328730f45dSAlex Elder  * by the modem.  It is *not* a QMAP header, but it does contain the mux_id
5338730f45dSAlex Elder  * value that we want, in its low-order byte.  A bitmask defined in the
5348730f45dSAlex Elder  * endpoint's METADATA_MASK register defines which byte within the modem
5358730f45dSAlex Elder  * metadata contains the mux_id.  And the OFST_METADATA field programmed
5368730f45dSAlex Elder  * here indicates where the extracted byte should be placed within the QMAP
5378730f45dSAlex Elder  * header.
5388730f45dSAlex Elder  */
53984f9bd12SAlex Elder static void ipa_endpoint_init_hdr(struct ipa_endpoint *endpoint)
54084f9bd12SAlex Elder {
54184f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_HDR_N_OFFSET(endpoint->endpoint_id);
5421af15c2aSAlex Elder 	struct ipa *ipa = endpoint->ipa;
54384f9bd12SAlex Elder 	u32 val = 0;
54484f9bd12SAlex Elder 
54584f9bd12SAlex Elder 	if (endpoint->data->qmap) {
5461af15c2aSAlex Elder 		enum ipa_version version = ipa->version;
5475567d4d9SAlex Elder 		size_t header_size;
54884f9bd12SAlex Elder 
5495567d4d9SAlex Elder 		header_size = ipa_qmap_header_size(version, endpoint);
5505567d4d9SAlex Elder 		val = ipa_header_size_encoded(version, header_size);
55184f9bd12SAlex Elder 
552f330fda3SAlex Elder 		/* Define how to fill fields in a received QMAP header */
5538730f45dSAlex Elder 		if (!endpoint->toward_ipa) {
5541af15c2aSAlex Elder 			u32 offset;	/* Field offset within header */
5558730f45dSAlex Elder 
5568730f45dSAlex Elder 			/* Where IPA will write the metadata value */
5571af15c2aSAlex Elder 			offset = offsetof(struct rmnet_map_header, mux_id);
5581af15c2aSAlex Elder 			val |= ipa_metadata_offset_encoded(version, offset);
5598730f45dSAlex Elder 
5608730f45dSAlex Elder 			/* Where IPA will write the length */
5611af15c2aSAlex Elder 			offset = offsetof(struct rmnet_map_header, pkt_len);
5621af15c2aSAlex Elder 			/* Upper bits are stored in HDR_EXT with IPA v4.5 */
563d7f3087bSAlex Elder 			if (version >= IPA_VERSION_4_5)
5641af15c2aSAlex Elder 				offset &= field_mask(HDR_OFST_PKT_SIZE_FMASK);
5651af15c2aSAlex Elder 
56684f9bd12SAlex Elder 			val |= HDR_OFST_PKT_SIZE_VALID_FMASK;
5671af15c2aSAlex Elder 			val |= u32_encode_bits(offset, HDR_OFST_PKT_SIZE_FMASK);
56884f9bd12SAlex Elder 		}
5698730f45dSAlex Elder 		/* For QMAP TX, metadata offset is 0 (modem assumes this) */
5708730f45dSAlex Elder 		val |= HDR_OFST_METADATA_VALID_FMASK;
5718730f45dSAlex Elder 
5728730f45dSAlex Elder 		/* HDR_ADDITIONAL_CONST_LEN is 0; (RX only) */
57384f9bd12SAlex Elder 		/* HDR_A5_MUX is 0 */
57484f9bd12SAlex Elder 		/* HDR_LEN_INC_DEAGG_HDR is 0 */
5758bfc4e21SAlex Elder 		/* HDR_METADATA_REG_VALID is 0 (TX only, version < v4.5) */
57684f9bd12SAlex Elder 	}
57784f9bd12SAlex Elder 
5781af15c2aSAlex Elder 	iowrite32(val, ipa->reg_virt + offset);
57984f9bd12SAlex Elder }
58084f9bd12SAlex Elder 
58184f9bd12SAlex Elder static void ipa_endpoint_init_hdr_ext(struct ipa_endpoint *endpoint)
58284f9bd12SAlex Elder {
58384f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_HDR_EXT_N_OFFSET(endpoint->endpoint_id);
58484f9bd12SAlex Elder 	u32 pad_align = endpoint->data->rx.pad_align;
5851af15c2aSAlex Elder 	struct ipa *ipa = endpoint->ipa;
58684f9bd12SAlex Elder 	u32 val = 0;
58784f9bd12SAlex Elder 
58884f9bd12SAlex Elder 	val |= HDR_ENDIANNESS_FMASK;		/* big endian */
589f330fda3SAlex Elder 
590f330fda3SAlex Elder 	/* A QMAP header contains a 6 bit pad field at offset 0.  The RMNet
591f330fda3SAlex Elder 	 * driver assumes this field is meaningful in packets it receives,
592f330fda3SAlex Elder 	 * and assumes the header's payload length includes that padding.
593f330fda3SAlex Elder 	 * The RMNet driver does *not* pad packets it sends, however, so
594f330fda3SAlex Elder 	 * the pad field (although 0) should be ignored.
595f330fda3SAlex Elder 	 */
596f330fda3SAlex Elder 	if (endpoint->data->qmap && !endpoint->toward_ipa) {
59784f9bd12SAlex Elder 		val |= HDR_TOTAL_LEN_OR_PAD_VALID_FMASK;
59884f9bd12SAlex Elder 		/* HDR_TOTAL_LEN_OR_PAD is 0 (pad, not total_len) */
599f330fda3SAlex Elder 		val |= HDR_PAYLOAD_LEN_INC_PADDING_FMASK;
60084f9bd12SAlex Elder 		/* HDR_TOTAL_LEN_OR_PAD_OFFSET is 0 */
601f330fda3SAlex Elder 	}
602f330fda3SAlex Elder 
603f330fda3SAlex Elder 	/* HDR_PAYLOAD_LEN_INC_PADDING is 0 */
60484f9bd12SAlex Elder 	if (!endpoint->toward_ipa)
60584f9bd12SAlex Elder 		val |= u32_encode_bits(pad_align, HDR_PAD_TO_ALIGNMENT_FMASK);
60684f9bd12SAlex Elder 
6071af15c2aSAlex Elder 	/* IPA v4.5 adds some most-significant bits to a few fields,
6081af15c2aSAlex Elder 	 * two of which are defined in the HDR (not HDR_EXT) register.
6091af15c2aSAlex Elder 	 */
610d7f3087bSAlex Elder 	if (ipa->version >= IPA_VERSION_4_5) {
6111af15c2aSAlex Elder 		/* HDR_TOTAL_LEN_OR_PAD_OFFSET is 0, so MSB is 0 */
6121af15c2aSAlex Elder 		if (endpoint->data->qmap && !endpoint->toward_ipa) {
6131af15c2aSAlex Elder 			u32 offset;
61484f9bd12SAlex Elder 
6151af15c2aSAlex Elder 			offset = offsetof(struct rmnet_map_header, pkt_len);
6161af15c2aSAlex Elder 			offset >>= hweight32(HDR_OFST_PKT_SIZE_FMASK);
6171af15c2aSAlex Elder 			val |= u32_encode_bits(offset,
6181af15c2aSAlex Elder 					       HDR_OFST_PKT_SIZE_MSB_FMASK);
6191af15c2aSAlex Elder 			/* HDR_ADDITIONAL_CONST_LEN is 0 so MSB is 0 */
6201af15c2aSAlex Elder 		}
6211af15c2aSAlex Elder 	}
6221af15c2aSAlex Elder 	iowrite32(val, ipa->reg_virt + offset);
6231af15c2aSAlex Elder }
62484f9bd12SAlex Elder 
62584f9bd12SAlex Elder static void ipa_endpoint_init_hdr_metadata_mask(struct ipa_endpoint *endpoint)
62684f9bd12SAlex Elder {
62784f9bd12SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
62884f9bd12SAlex Elder 	u32 val = 0;
62984f9bd12SAlex Elder 	u32 offset;
63084f9bd12SAlex Elder 
631fb57c3eaSAlex Elder 	if (endpoint->toward_ipa)
632fb57c3eaSAlex Elder 		return;		/* Register not valid for TX endpoints */
633fb57c3eaSAlex Elder 
63484f9bd12SAlex Elder 	offset = IPA_REG_ENDP_INIT_HDR_METADATA_MASK_N_OFFSET(endpoint_id);
63584f9bd12SAlex Elder 
6368730f45dSAlex Elder 	/* Note that HDR_ENDIANNESS indicates big endian header fields */
6379b63f093SAlex Elder 	if (endpoint->data->qmap)
638088f8a23SAlex Elder 		val = (__force u32)cpu_to_be32(IPA_ENDPOINT_QMAP_METADATA_MASK);
63984f9bd12SAlex Elder 
64084f9bd12SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
64184f9bd12SAlex Elder }
64284f9bd12SAlex Elder 
64384f9bd12SAlex Elder static void ipa_endpoint_init_mode(struct ipa_endpoint *endpoint)
64484f9bd12SAlex Elder {
64584f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_MODE_N_OFFSET(endpoint->endpoint_id);
64684f9bd12SAlex Elder 	u32 val;
64784f9bd12SAlex Elder 
648fb57c3eaSAlex Elder 	if (!endpoint->toward_ipa)
649fb57c3eaSAlex Elder 		return;		/* Register not valid for RX endpoints */
650fb57c3eaSAlex Elder 
65100b9102aSAlex Elder 	if (endpoint->data->dma_mode) {
65284f9bd12SAlex Elder 		enum ipa_endpoint_name name = endpoint->data->dma_endpoint;
65384f9bd12SAlex Elder 		u32 dma_endpoint_id;
65484f9bd12SAlex Elder 
65584f9bd12SAlex Elder 		dma_endpoint_id = endpoint->ipa->name_map[name]->endpoint_id;
65684f9bd12SAlex Elder 
65784f9bd12SAlex Elder 		val = u32_encode_bits(IPA_DMA, MODE_FMASK);
65884f9bd12SAlex Elder 		val |= u32_encode_bits(dma_endpoint_id, DEST_PIPE_INDEX_FMASK);
65984f9bd12SAlex Elder 	} else {
66084f9bd12SAlex Elder 		val = u32_encode_bits(IPA_BASIC, MODE_FMASK);
66184f9bd12SAlex Elder 	}
66200b9102aSAlex Elder 	/* All other bits unspecified (and 0) */
66384f9bd12SAlex Elder 
66484f9bd12SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
66584f9bd12SAlex Elder }
66684f9bd12SAlex Elder 
66784f9bd12SAlex Elder /* Compute the aggregation size value to use for a given buffer size */
66884f9bd12SAlex Elder static u32 ipa_aggr_size_kb(u32 rx_buffer_size)
66984f9bd12SAlex Elder {
67084f9bd12SAlex Elder 	/* We don't use "hard byte limit" aggregation, so we define the
67184f9bd12SAlex Elder 	 * aggregation limit such that our buffer has enough space *after*
67284f9bd12SAlex Elder 	 * that limit to receive a full MTU of data, plus overhead.
67384f9bd12SAlex Elder 	 */
67484f9bd12SAlex Elder 	rx_buffer_size -= IPA_MTU + IPA_RX_BUFFER_OVERHEAD;
67584f9bd12SAlex Elder 
67684f9bd12SAlex Elder 	return rx_buffer_size / SZ_1K;
67784f9bd12SAlex Elder }
67884f9bd12SAlex Elder 
6796bf754c7SAlex Elder /* Encoded values for AGGR endpoint register fields */
6806bf754c7SAlex Elder static u32 aggr_byte_limit_encoded(enum ipa_version version, u32 limit)
6816bf754c7SAlex Elder {
6826bf754c7SAlex Elder 	if (version < IPA_VERSION_4_5)
6836bf754c7SAlex Elder 		return u32_encode_bits(limit, aggr_byte_limit_fmask(true));
6846bf754c7SAlex Elder 
6856bf754c7SAlex Elder 	return u32_encode_bits(limit, aggr_byte_limit_fmask(false));
6866bf754c7SAlex Elder }
6876bf754c7SAlex Elder 
68819547041SAlex Elder /* Encode the aggregation timer limit (microseconds) based on IPA version */
6896bf754c7SAlex Elder static u32 aggr_time_limit_encoded(enum ipa_version version, u32 limit)
6906bf754c7SAlex Elder {
69119547041SAlex Elder 	u32 gran_sel;
69219547041SAlex Elder 	u32 fmask;
69319547041SAlex Elder 	u32 val;
6946bf754c7SAlex Elder 
69519547041SAlex Elder 	if (version < IPA_VERSION_4_5) {
69619547041SAlex Elder 		/* We set aggregation granularity in ipa_hardware_config() */
69719547041SAlex Elder 		limit = DIV_ROUND_CLOSEST(limit, IPA_AGGR_GRANULARITY);
69819547041SAlex Elder 
69919547041SAlex Elder 		return u32_encode_bits(limit, aggr_time_limit_fmask(true));
70019547041SAlex Elder 	}
70119547041SAlex Elder 
70219547041SAlex Elder 	/* IPA v4.5 expresses the time limit using Qtime.  The AP has
70319547041SAlex Elder 	 * pulse generators 0 and 1 available, which were configured
70419547041SAlex Elder 	 * in ipa_qtime_config() to have granularity 100 usec and
70519547041SAlex Elder 	 * 1 msec, respectively.  Use pulse generator 0 if possible,
70619547041SAlex Elder 	 * otherwise fall back to pulse generator 1.
70719547041SAlex Elder 	 */
70819547041SAlex Elder 	fmask = aggr_time_limit_fmask(false);
70919547041SAlex Elder 	val = DIV_ROUND_CLOSEST(limit, 100);
71019547041SAlex Elder 	if (val > field_max(fmask)) {
71119547041SAlex Elder 		/* Have to use pulse generator 1 (millisecond granularity) */
71219547041SAlex Elder 		gran_sel = AGGR_GRAN_SEL_FMASK;
71319547041SAlex Elder 		val = DIV_ROUND_CLOSEST(limit, 1000);
71419547041SAlex Elder 	} else {
71519547041SAlex Elder 		/* We can use pulse generator 0 (100 usec granularity) */
71619547041SAlex Elder 		gran_sel = 0;
71719547041SAlex Elder 	}
71819547041SAlex Elder 
71919547041SAlex Elder 	return gran_sel | u32_encode_bits(val, fmask);
7206bf754c7SAlex Elder }
7216bf754c7SAlex Elder 
7226bf754c7SAlex Elder static u32 aggr_sw_eof_active_encoded(enum ipa_version version, bool enabled)
7236bf754c7SAlex Elder {
7246bf754c7SAlex Elder 	u32 val = enabled ? 1 : 0;
7256bf754c7SAlex Elder 
7266bf754c7SAlex Elder 	if (version < IPA_VERSION_4_5)
7276bf754c7SAlex Elder 		return u32_encode_bits(val, aggr_sw_eof_active_fmask(true));
7286bf754c7SAlex Elder 
7296bf754c7SAlex Elder 	return u32_encode_bits(val, aggr_sw_eof_active_fmask(false));
7306bf754c7SAlex Elder }
7316bf754c7SAlex Elder 
73284f9bd12SAlex Elder static void ipa_endpoint_init_aggr(struct ipa_endpoint *endpoint)
73384f9bd12SAlex Elder {
73484f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_AGGR_N_OFFSET(endpoint->endpoint_id);
7356bf754c7SAlex Elder 	enum ipa_version version = endpoint->ipa->version;
73684f9bd12SAlex Elder 	u32 val = 0;
73784f9bd12SAlex Elder 
73884f9bd12SAlex Elder 	if (endpoint->data->aggregation) {
73984f9bd12SAlex Elder 		if (!endpoint->toward_ipa) {
740ed23f026SAlex Elder 			const struct ipa_endpoint_rx_data *rx_data;
7416bf754c7SAlex Elder 			bool close_eof;
74284f9bd12SAlex Elder 			u32 limit;
74384f9bd12SAlex Elder 
744ed23f026SAlex Elder 			rx_data = &endpoint->data->rx;
74584f9bd12SAlex Elder 			val |= u32_encode_bits(IPA_ENABLE_AGGR, AGGR_EN_FMASK);
74684f9bd12SAlex Elder 			val |= u32_encode_bits(IPA_GENERIC, AGGR_TYPE_FMASK);
7479e88cb5fSAlex Elder 
748ed23f026SAlex Elder 			limit = ipa_aggr_size_kb(rx_data->buffer_size);
7496bf754c7SAlex Elder 			val |= aggr_byte_limit_encoded(version, limit);
7501d86652bSAlex Elder 
7516bf754c7SAlex Elder 			limit = IPA_AGGR_TIME_LIMIT;
7526bf754c7SAlex Elder 			val |= aggr_time_limit_encoded(version, limit);
7531d86652bSAlex Elder 
7549e88cb5fSAlex Elder 			/* AGGR_PKT_LIMIT is 0 (unlimited) */
7559e88cb5fSAlex Elder 
756ed23f026SAlex Elder 			close_eof = rx_data->aggr_close_eof;
7576bf754c7SAlex Elder 			val |= aggr_sw_eof_active_encoded(version, close_eof);
7586bf754c7SAlex Elder 
75984f9bd12SAlex Elder 			/* AGGR_HARD_BYTE_LIMIT_ENABLE is 0 */
76084f9bd12SAlex Elder 		} else {
76184f9bd12SAlex Elder 			val |= u32_encode_bits(IPA_ENABLE_DEAGGR,
76284f9bd12SAlex Elder 					       AGGR_EN_FMASK);
76384f9bd12SAlex Elder 			val |= u32_encode_bits(IPA_QCMAP, AGGR_TYPE_FMASK);
76484f9bd12SAlex Elder 			/* other fields ignored */
76584f9bd12SAlex Elder 		}
76684f9bd12SAlex Elder 		/* AGGR_FORCE_CLOSE is 0 */
7678bfc4e21SAlex Elder 		/* AGGR_GRAN_SEL is 0 for IPA v4.5 */
76884f9bd12SAlex Elder 	} else {
76984f9bd12SAlex Elder 		val |= u32_encode_bits(IPA_BYPASS_AGGR, AGGR_EN_FMASK);
77084f9bd12SAlex Elder 		/* other fields ignored */
77184f9bd12SAlex Elder 	}
77284f9bd12SAlex Elder 
77384f9bd12SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
77484f9bd12SAlex Elder }
77584f9bd12SAlex Elder 
77663e5afc8SAlex Elder /* Return the Qtime-based head-of-line blocking timer value that
77763e5afc8SAlex Elder  * represents the given number of microseconds.  The result
77863e5afc8SAlex Elder  * includes both the timer value and the selected timer granularity.
779f13a8c31SAlex Elder  */
78063e5afc8SAlex Elder static u32 hol_block_timer_qtime_val(struct ipa *ipa, u32 microseconds)
78163e5afc8SAlex Elder {
78263e5afc8SAlex Elder 	u32 gran_sel;
78363e5afc8SAlex Elder 	u32 val;
78463e5afc8SAlex Elder 
78563e5afc8SAlex Elder 	/* IPA v4.5 expresses time limits using Qtime.  The AP has
78663e5afc8SAlex Elder 	 * pulse generators 0 and 1 available, which were configured
78763e5afc8SAlex Elder 	 * in ipa_qtime_config() to have granularity 100 usec and
78863e5afc8SAlex Elder 	 * 1 msec, respectively.  Use pulse generator 0 if possible,
78963e5afc8SAlex Elder 	 * otherwise fall back to pulse generator 1.
79063e5afc8SAlex Elder 	 */
79163e5afc8SAlex Elder 	val = DIV_ROUND_CLOSEST(microseconds, 100);
79263e5afc8SAlex Elder 	if (val > field_max(TIME_LIMIT_FMASK)) {
79363e5afc8SAlex Elder 		/* Have to use pulse generator 1 (millisecond granularity) */
79463e5afc8SAlex Elder 		gran_sel = GRAN_SEL_FMASK;
79563e5afc8SAlex Elder 		val = DIV_ROUND_CLOSEST(microseconds, 1000);
79663e5afc8SAlex Elder 	} else {
79763e5afc8SAlex Elder 		/* We can use pulse generator 0 (100 usec granularity) */
79863e5afc8SAlex Elder 		gran_sel = 0;
79963e5afc8SAlex Elder 	}
80063e5afc8SAlex Elder 
80163e5afc8SAlex Elder 	return gran_sel | u32_encode_bits(val, TIME_LIMIT_FMASK);
80263e5afc8SAlex Elder }
80363e5afc8SAlex Elder 
80463e5afc8SAlex Elder /* The head-of-line blocking timer is defined as a tick count.  For
80563e5afc8SAlex Elder  * IPA version 4.5 the tick count is based on the Qtimer, which is
80663e5afc8SAlex Elder  * derived from the 19.2 MHz SoC XO clock.  For older IPA versions
80763e5afc8SAlex Elder  * each tick represents 128 cycles of the IPA core clock.
80863e5afc8SAlex Elder  *
80963e5afc8SAlex Elder  * Return the encoded value that should be written to that register
81063e5afc8SAlex Elder  * that represents the timeout period provided.  For IPA v4.2 this
81163e5afc8SAlex Elder  * encodes a base and scale value, while for earlier versions the
81263e5afc8SAlex Elder  * value is a simple tick count.
81363e5afc8SAlex Elder  */
81463e5afc8SAlex Elder static u32 hol_block_timer_val(struct ipa *ipa, u32 microseconds)
81584f9bd12SAlex Elder {
816f13a8c31SAlex Elder 	u32 width;
81784f9bd12SAlex Elder 	u32 scale;
818f13a8c31SAlex Elder 	u64 ticks;
819f13a8c31SAlex Elder 	u64 rate;
820f13a8c31SAlex Elder 	u32 high;
82184f9bd12SAlex Elder 	u32 val;
82284f9bd12SAlex Elder 
82384f9bd12SAlex Elder 	if (!microseconds)
824f13a8c31SAlex Elder 		return 0;	/* Nothing to compute if timer period is 0 */
82584f9bd12SAlex Elder 
826d7f3087bSAlex Elder 	if (ipa->version >= IPA_VERSION_4_5)
82763e5afc8SAlex Elder 		return hol_block_timer_qtime_val(ipa, microseconds);
82863e5afc8SAlex Elder 
829f13a8c31SAlex Elder 	/* Use 64 bit arithmetic to avoid overflow... */
8307aa0e8b8SAlex Elder 	rate = ipa_core_clock_rate(ipa);
831f13a8c31SAlex Elder 	ticks = DIV_ROUND_CLOSEST(microseconds * rate, 128 * USEC_PER_SEC);
832f13a8c31SAlex Elder 	/* ...but we still need to fit into a 32-bit register */
833f13a8c31SAlex Elder 	WARN_ON(ticks > U32_MAX);
83484f9bd12SAlex Elder 
8356833a096SAlex Elder 	/* IPA v3.5.1 through v4.1 just record the tick count */
8366833a096SAlex Elder 	if (ipa->version < IPA_VERSION_4_2)
837f13a8c31SAlex Elder 		return (u32)ticks;
83884f9bd12SAlex Elder 
839f13a8c31SAlex Elder 	/* For IPA v4.2, the tick count is represented by base and
840f13a8c31SAlex Elder 	 * scale fields within the 32-bit timer register, where:
841f13a8c31SAlex Elder 	 *     ticks = base << scale;
842f13a8c31SAlex Elder 	 * The best precision is achieved when the base value is as
843f13a8c31SAlex Elder 	 * large as possible.  Find the highest set bit in the tick
844f13a8c31SAlex Elder 	 * count, and extract the number of bits in the base field
845497abc87SPeng Li 	 * such that high bit is included.
846f13a8c31SAlex Elder 	 */
847f13a8c31SAlex Elder 	high = fls(ticks);		/* 1..32 */
848f13a8c31SAlex Elder 	width = HWEIGHT32(BASE_VALUE_FMASK);
849f13a8c31SAlex Elder 	scale = high > width ? high - width : 0;
850f13a8c31SAlex Elder 	if (scale) {
851f13a8c31SAlex Elder 		/* If we're scaling, round up to get a closer result */
852f13a8c31SAlex Elder 		ticks += 1 << (scale - 1);
853f13a8c31SAlex Elder 		/* High bit was set, so rounding might have affected it */
854f13a8c31SAlex Elder 		if (fls(ticks) != high)
855f13a8c31SAlex Elder 			scale++;
856f13a8c31SAlex Elder 	}
85784f9bd12SAlex Elder 
85884f9bd12SAlex Elder 	val = u32_encode_bits(scale, SCALE_FMASK);
859f13a8c31SAlex Elder 	val |= u32_encode_bits(ticks >> scale, BASE_VALUE_FMASK);
86084f9bd12SAlex Elder 
86184f9bd12SAlex Elder 	return val;
86284f9bd12SAlex Elder }
86384f9bd12SAlex Elder 
864f13a8c31SAlex Elder /* If microseconds is 0, timeout is immediate */
865f13a8c31SAlex Elder static void ipa_endpoint_init_hol_block_timer(struct ipa_endpoint *endpoint,
86684f9bd12SAlex Elder 					      u32 microseconds)
86784f9bd12SAlex Elder {
86884f9bd12SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
86984f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
87084f9bd12SAlex Elder 	u32 offset;
87184f9bd12SAlex Elder 	u32 val;
87284f9bd12SAlex Elder 
873816316caSAlex Elder 	/* This should only be changed when HOL_BLOCK_EN is disabled */
87484f9bd12SAlex Elder 	offset = IPA_REG_ENDP_INIT_HOL_BLOCK_TIMER_N_OFFSET(endpoint_id);
87563e5afc8SAlex Elder 	val = hol_block_timer_val(ipa, microseconds);
87684f9bd12SAlex Elder 	iowrite32(val, ipa->reg_virt + offset);
87784f9bd12SAlex Elder }
87884f9bd12SAlex Elder 
87984f9bd12SAlex Elder static void
880e6aab6b9SAlex Elder ipa_endpoint_init_hol_block_en(struct ipa_endpoint *endpoint, bool enable)
88184f9bd12SAlex Elder {
88284f9bd12SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
88384f9bd12SAlex Elder 	u32 offset;
88484f9bd12SAlex Elder 	u32 val;
88584f9bd12SAlex Elder 
886547c8788SAlex Elder 	val = enable ? HOL_BLOCK_EN_FMASK : 0;
88784f9bd12SAlex Elder 	offset = IPA_REG_ENDP_INIT_HOL_BLOCK_EN_N_OFFSET(endpoint_id);
88884f9bd12SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
8896e228d8cSAlex Elder 	/* When enabling, the register must be written twice for IPA v4.5+ */
8906e228d8cSAlex Elder 	if (enable && endpoint->ipa->version >= IPA_VERSION_4_5)
8916e228d8cSAlex Elder 		iowrite32(val, endpoint->ipa->reg_virt + offset);
89284f9bd12SAlex Elder }
89384f9bd12SAlex Elder 
894e6aab6b9SAlex Elder /* Assumes HOL_BLOCK is in disabled state */
895e6aab6b9SAlex Elder static void ipa_endpoint_init_hol_block_enable(struct ipa_endpoint *endpoint,
896e6aab6b9SAlex Elder 					       u32 microseconds)
897e6aab6b9SAlex Elder {
898e6aab6b9SAlex Elder 	ipa_endpoint_init_hol_block_timer(endpoint, microseconds);
899e6aab6b9SAlex Elder 	ipa_endpoint_init_hol_block_en(endpoint, true);
900e6aab6b9SAlex Elder }
901e6aab6b9SAlex Elder 
902e6aab6b9SAlex Elder static void ipa_endpoint_init_hol_block_disable(struct ipa_endpoint *endpoint)
903e6aab6b9SAlex Elder {
904e6aab6b9SAlex Elder 	ipa_endpoint_init_hol_block_en(endpoint, false);
905e6aab6b9SAlex Elder }
906e6aab6b9SAlex Elder 
90784f9bd12SAlex Elder void ipa_endpoint_modem_hol_block_clear_all(struct ipa *ipa)
90884f9bd12SAlex Elder {
90984f9bd12SAlex Elder 	u32 i;
91084f9bd12SAlex Elder 
91184f9bd12SAlex Elder 	for (i = 0; i < IPA_ENDPOINT_MAX; i++) {
91284f9bd12SAlex Elder 		struct ipa_endpoint *endpoint = &ipa->endpoint[i];
91384f9bd12SAlex Elder 
914f8d34dfdSAlex Elder 		if (endpoint->toward_ipa || endpoint->ee_id != GSI_EE_MODEM)
91584f9bd12SAlex Elder 			continue;
91684f9bd12SAlex Elder 
917e6aab6b9SAlex Elder 		ipa_endpoint_init_hol_block_disable(endpoint);
918e6aab6b9SAlex Elder 		ipa_endpoint_init_hol_block_enable(endpoint, 0);
91984f9bd12SAlex Elder 	}
92084f9bd12SAlex Elder }
92184f9bd12SAlex Elder 
92284f9bd12SAlex Elder static void ipa_endpoint_init_deaggr(struct ipa_endpoint *endpoint)
92384f9bd12SAlex Elder {
92484f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_DEAGGR_N_OFFSET(endpoint->endpoint_id);
92584f9bd12SAlex Elder 	u32 val = 0;
92684f9bd12SAlex Elder 
927fb57c3eaSAlex Elder 	if (!endpoint->toward_ipa)
928fb57c3eaSAlex Elder 		return;		/* Register not valid for RX endpoints */
929fb57c3eaSAlex Elder 
93084f9bd12SAlex Elder 	/* DEAGGR_HDR_LEN is 0 */
93184f9bd12SAlex Elder 	/* PACKET_OFFSET_VALID is 0 */
93284f9bd12SAlex Elder 	/* PACKET_OFFSET_LOCATION is ignored (not valid) */
93384f9bd12SAlex Elder 	/* MAX_PACKET_LEN is 0 (not enforced) */
93484f9bd12SAlex Elder 
93584f9bd12SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
93684f9bd12SAlex Elder }
93784f9bd12SAlex Elder 
9382d265342SAlex Elder static void ipa_endpoint_init_rsrc_grp(struct ipa_endpoint *endpoint)
9392d265342SAlex Elder {
9402d265342SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_RSRC_GRP_N_OFFSET(endpoint->endpoint_id);
9412d265342SAlex Elder 	struct ipa *ipa = endpoint->ipa;
9422d265342SAlex Elder 	u32 val;
9432d265342SAlex Elder 
9442d265342SAlex Elder 	val = rsrc_grp_encoded(ipa->version, endpoint->data->resource_group);
9452d265342SAlex Elder 	iowrite32(val, ipa->reg_virt + offset);
9462d265342SAlex Elder }
9472d265342SAlex Elder 
94884f9bd12SAlex Elder static void ipa_endpoint_init_seq(struct ipa_endpoint *endpoint)
94984f9bd12SAlex Elder {
95084f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_SEQ_N_OFFSET(endpoint->endpoint_id);
95184f9bd12SAlex Elder 	u32 val = 0;
95284f9bd12SAlex Elder 
953fb57c3eaSAlex Elder 	if (!endpoint->toward_ipa)
954fb57c3eaSAlex Elder 		return;		/* Register not valid for RX endpoints */
955fb57c3eaSAlex Elder 
9568ee5df65SAlex Elder 	/* Low-order byte configures primary packet processing */
9571690d8a7SAlex Elder 	val |= u32_encode_bits(endpoint->data->tx.seq_type, SEQ_TYPE_FMASK);
9588ee5df65SAlex Elder 
9598ee5df65SAlex Elder 	/* Second byte configures replicated packet processing */
9601690d8a7SAlex Elder 	val |= u32_encode_bits(endpoint->data->tx.seq_rep_type,
9611690d8a7SAlex Elder 			       SEQ_REP_TYPE_FMASK);
96284f9bd12SAlex Elder 
96384f9bd12SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
96484f9bd12SAlex Elder }
96584f9bd12SAlex Elder 
96684f9bd12SAlex Elder /**
96784f9bd12SAlex Elder  * ipa_endpoint_skb_tx() - Transmit a socket buffer
96884f9bd12SAlex Elder  * @endpoint:	Endpoint pointer
96984f9bd12SAlex Elder  * @skb:	Socket buffer to send
97084f9bd12SAlex Elder  *
97184f9bd12SAlex Elder  * Returns:	0 if successful, or a negative error code
97284f9bd12SAlex Elder  */
97384f9bd12SAlex Elder int ipa_endpoint_skb_tx(struct ipa_endpoint *endpoint, struct sk_buff *skb)
97484f9bd12SAlex Elder {
97584f9bd12SAlex Elder 	struct gsi_trans *trans;
97684f9bd12SAlex Elder 	u32 nr_frags;
97784f9bd12SAlex Elder 	int ret;
97884f9bd12SAlex Elder 
97984f9bd12SAlex Elder 	/* Make sure source endpoint's TLV FIFO has enough entries to
98084f9bd12SAlex Elder 	 * hold the linear portion of the skb and all its fragments.
98184f9bd12SAlex Elder 	 * If not, see if we can linearize it before giving up.
98284f9bd12SAlex Elder 	 */
98384f9bd12SAlex Elder 	nr_frags = skb_shinfo(skb)->nr_frags;
98484f9bd12SAlex Elder 	if (1 + nr_frags > endpoint->trans_tre_max) {
98584f9bd12SAlex Elder 		if (skb_linearize(skb))
98684f9bd12SAlex Elder 			return -E2BIG;
98784f9bd12SAlex Elder 		nr_frags = 0;
98884f9bd12SAlex Elder 	}
98984f9bd12SAlex Elder 
99084f9bd12SAlex Elder 	trans = ipa_endpoint_trans_alloc(endpoint, 1 + nr_frags);
99184f9bd12SAlex Elder 	if (!trans)
99284f9bd12SAlex Elder 		return -EBUSY;
99384f9bd12SAlex Elder 
99484f9bd12SAlex Elder 	ret = gsi_trans_skb_add(trans, skb);
99584f9bd12SAlex Elder 	if (ret)
99684f9bd12SAlex Elder 		goto err_trans_free;
99784f9bd12SAlex Elder 	trans->data = skb;	/* transaction owns skb now */
99884f9bd12SAlex Elder 
99984f9bd12SAlex Elder 	gsi_trans_commit(trans, !netdev_xmit_more());
100084f9bd12SAlex Elder 
100184f9bd12SAlex Elder 	return 0;
100284f9bd12SAlex Elder 
100384f9bd12SAlex Elder err_trans_free:
100484f9bd12SAlex Elder 	gsi_trans_free(trans);
100584f9bd12SAlex Elder 
100684f9bd12SAlex Elder 	return -ENOMEM;
100784f9bd12SAlex Elder }
100884f9bd12SAlex Elder 
100984f9bd12SAlex Elder static void ipa_endpoint_status(struct ipa_endpoint *endpoint)
101084f9bd12SAlex Elder {
101184f9bd12SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
101284f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
101384f9bd12SAlex Elder 	u32 val = 0;
101484f9bd12SAlex Elder 	u32 offset;
101584f9bd12SAlex Elder 
101684f9bd12SAlex Elder 	offset = IPA_REG_ENDP_STATUS_N_OFFSET(endpoint_id);
101784f9bd12SAlex Elder 
101884f9bd12SAlex Elder 	if (endpoint->data->status_enable) {
101984f9bd12SAlex Elder 		val |= STATUS_EN_FMASK;
102084f9bd12SAlex Elder 		if (endpoint->toward_ipa) {
102184f9bd12SAlex Elder 			enum ipa_endpoint_name name;
102284f9bd12SAlex Elder 			u32 status_endpoint_id;
102384f9bd12SAlex Elder 
102484f9bd12SAlex Elder 			name = endpoint->data->tx.status_endpoint;
102584f9bd12SAlex Elder 			status_endpoint_id = ipa->name_map[name]->endpoint_id;
102684f9bd12SAlex Elder 
102784f9bd12SAlex Elder 			val |= u32_encode_bits(status_endpoint_id,
102884f9bd12SAlex Elder 					       STATUS_ENDP_FMASK);
102984f9bd12SAlex Elder 		}
10308bfc4e21SAlex Elder 		/* STATUS_LOCATION is 0, meaning status element precedes
10318bfc4e21SAlex Elder 		 * packet (not present for IPA v4.5)
10328bfc4e21SAlex Elder 		 */
10338bfc4e21SAlex Elder 		/* STATUS_PKT_SUPPRESS_FMASK is 0 (not present for v3.5.1) */
103484f9bd12SAlex Elder 	}
103584f9bd12SAlex Elder 
103684f9bd12SAlex Elder 	iowrite32(val, ipa->reg_virt + offset);
103784f9bd12SAlex Elder }
103884f9bd12SAlex Elder 
1039*6a606b90SAlex Elder static int ipa_endpoint_replenish_one(struct ipa_endpoint *endpoint,
1040*6a606b90SAlex Elder 				      struct gsi_trans *trans)
104184f9bd12SAlex Elder {
104284f9bd12SAlex Elder 	struct page *page;
1043ed23f026SAlex Elder 	u32 buffer_size;
104484f9bd12SAlex Elder 	u32 offset;
104584f9bd12SAlex Elder 	u32 len;
104684f9bd12SAlex Elder 	int ret;
104784f9bd12SAlex Elder 
1048ed23f026SAlex Elder 	buffer_size = endpoint->data->rx.buffer_size;
1049ed23f026SAlex Elder 	page = dev_alloc_pages(get_order(buffer_size));
105084f9bd12SAlex Elder 	if (!page)
1051*6a606b90SAlex Elder 		return -ENOMEM;
105284f9bd12SAlex Elder 
105384f9bd12SAlex Elder 	/* Offset the buffer to make space for skb headroom */
105484f9bd12SAlex Elder 	offset = NET_SKB_PAD;
1055ed23f026SAlex Elder 	len = buffer_size - offset;
105684f9bd12SAlex Elder 
105784f9bd12SAlex Elder 	ret = gsi_trans_page_add(trans, page, len, offset);
105884f9bd12SAlex Elder 	if (ret)
1059*6a606b90SAlex Elder 		__free_pages(page, get_order(buffer_size));
1060*6a606b90SAlex Elder 	else
106184f9bd12SAlex Elder 		trans->data = page;	/* transaction owns page now */
106284f9bd12SAlex Elder 
1063*6a606b90SAlex Elder 	return ret;
106484f9bd12SAlex Elder }
106584f9bd12SAlex Elder 
106684f9bd12SAlex Elder /**
10679af5ccf3SAlex Elder  * ipa_endpoint_replenish() - Replenish endpoint receive buffers
1068e3eea08eSAlex Elder  * @endpoint:	Endpoint to be replenished
106984f9bd12SAlex Elder  *
10709af5ccf3SAlex Elder  * The IPA hardware can hold a fixed number of receive buffers for an RX
10719af5ccf3SAlex Elder  * endpoint, based on the number of entries in the underlying channel ring
10729af5ccf3SAlex Elder  * buffer.  If an endpoint's "backlog" is non-zero, it indicates how many
10739af5ccf3SAlex Elder  * more receive buffers can be supplied to the hardware.  Replenishing for
1074a9bec7aeSAlex Elder  * an endpoint can be disabled, in which case buffers are not queued to
1075a9bec7aeSAlex Elder  * the hardware.
107684f9bd12SAlex Elder  */
10774b22d841SAlex Elder static void ipa_endpoint_replenish(struct ipa_endpoint *endpoint)
107884f9bd12SAlex Elder {
1079*6a606b90SAlex Elder 	struct gsi_trans *trans;
108084f9bd12SAlex Elder 	struct gsi *gsi;
108184f9bd12SAlex Elder 	u32 backlog;
108284f9bd12SAlex Elder 
10834b22d841SAlex Elder 	if (!test_bit(IPA_REPLENISH_ENABLED, endpoint->replenish_flags))
108484f9bd12SAlex Elder 		return;
108584f9bd12SAlex Elder 
10864b22d841SAlex Elder 	/* Skip it if it's already active */
10874b22d841SAlex Elder 	if (test_and_set_bit(IPA_REPLENISH_ACTIVE, endpoint->replenish_flags))
1088998c0bd2SAlex Elder 		return;
1089998c0bd2SAlex Elder 
1090b9dbabc5SAlex Elder 	while (atomic_dec_not_zero(&endpoint->replenish_backlog)) {
1091*6a606b90SAlex Elder 		trans = ipa_endpoint_trans_alloc(endpoint, 1);
1092*6a606b90SAlex Elder 		if (!trans)
1093*6a606b90SAlex Elder 			break;
1094*6a606b90SAlex Elder 
1095*6a606b90SAlex Elder 		if (ipa_endpoint_replenish_one(endpoint, trans))
1096*6a606b90SAlex Elder 			goto try_again_later;
1097b9dbabc5SAlex Elder 
1098b9dbabc5SAlex Elder 		if (++endpoint->replenish_ready == IPA_REPLENISH_BATCH)
1099b9dbabc5SAlex Elder 			endpoint->replenish_ready = 0;
1100b9dbabc5SAlex Elder 
1101b9dbabc5SAlex Elder 		/* Ring the doorbell if we've got a full batch */
1102*6a606b90SAlex Elder 		gsi_trans_commit(trans, !endpoint->replenish_ready);
1103b9dbabc5SAlex Elder 	}
1104998c0bd2SAlex Elder 
1105998c0bd2SAlex Elder 	clear_bit(IPA_REPLENISH_ACTIVE, endpoint->replenish_flags);
1106998c0bd2SAlex Elder 
110784f9bd12SAlex Elder 	return;
110884f9bd12SAlex Elder 
110984f9bd12SAlex Elder try_again_later:
1110*6a606b90SAlex Elder 	gsi_trans_free(trans);
1111998c0bd2SAlex Elder 	clear_bit(IPA_REPLENISH_ACTIVE, endpoint->replenish_flags);
1112998c0bd2SAlex Elder 
111384f9bd12SAlex Elder 	/* The last one didn't succeed, so fix the backlog */
11144b22d841SAlex Elder 	backlog = atomic_inc_return(&endpoint->replenish_backlog);
111584f9bd12SAlex Elder 
111684f9bd12SAlex Elder 	/* Whenever a receive buffer transaction completes we'll try to
111784f9bd12SAlex Elder 	 * replenish again.  It's unlikely, but if we fail to supply even
111884f9bd12SAlex Elder 	 * one buffer, nothing will trigger another replenish attempt.
111984f9bd12SAlex Elder 	 * Receive buffer transactions use one TRE, so schedule work to
112084f9bd12SAlex Elder 	 * try replenishing again if our backlog is *all* available TREs.
112184f9bd12SAlex Elder 	 */
112284f9bd12SAlex Elder 	gsi = &endpoint->ipa->gsi;
112384f9bd12SAlex Elder 	if (backlog == gsi_channel_tre_max(gsi, endpoint->channel_id))
112484f9bd12SAlex Elder 		schedule_delayed_work(&endpoint->replenish_work,
112584f9bd12SAlex Elder 				      msecs_to_jiffies(1));
112684f9bd12SAlex Elder }
112784f9bd12SAlex Elder 
112884f9bd12SAlex Elder static void ipa_endpoint_replenish_enable(struct ipa_endpoint *endpoint)
112984f9bd12SAlex Elder {
113084f9bd12SAlex Elder 	struct gsi *gsi = &endpoint->ipa->gsi;
113184f9bd12SAlex Elder 	u32 max_backlog;
113284f9bd12SAlex Elder 
1133c1aaa01dSAlex Elder 	set_bit(IPA_REPLENISH_ENABLED, endpoint->replenish_flags);
113484f9bd12SAlex Elder 
113584f9bd12SAlex Elder 	/* Start replenishing if hardware currently has no buffers */
113684f9bd12SAlex Elder 	max_backlog = gsi_channel_tre_max(gsi, endpoint->channel_id);
113784f9bd12SAlex Elder 	if (atomic_read(&endpoint->replenish_backlog) == max_backlog)
11384b22d841SAlex Elder 		ipa_endpoint_replenish(endpoint);
113984f9bd12SAlex Elder }
114084f9bd12SAlex Elder 
114184f9bd12SAlex Elder static void ipa_endpoint_replenish_disable(struct ipa_endpoint *endpoint)
114284f9bd12SAlex Elder {
1143c1aaa01dSAlex Elder 	clear_bit(IPA_REPLENISH_ENABLED, endpoint->replenish_flags);
114484f9bd12SAlex Elder }
114584f9bd12SAlex Elder 
114684f9bd12SAlex Elder static void ipa_endpoint_replenish_work(struct work_struct *work)
114784f9bd12SAlex Elder {
114884f9bd12SAlex Elder 	struct delayed_work *dwork = to_delayed_work(work);
114984f9bd12SAlex Elder 	struct ipa_endpoint *endpoint;
115084f9bd12SAlex Elder 
115184f9bd12SAlex Elder 	endpoint = container_of(dwork, struct ipa_endpoint, replenish_work);
115284f9bd12SAlex Elder 
11534b22d841SAlex Elder 	ipa_endpoint_replenish(endpoint);
115484f9bd12SAlex Elder }
115584f9bd12SAlex Elder 
115684f9bd12SAlex Elder static void ipa_endpoint_skb_copy(struct ipa_endpoint *endpoint,
115784f9bd12SAlex Elder 				  void *data, u32 len, u32 extra)
115884f9bd12SAlex Elder {
115984f9bd12SAlex Elder 	struct sk_buff *skb;
116084f9bd12SAlex Elder 
11611b65bbccSAlex Elder 	if (!endpoint->netdev)
11621b65bbccSAlex Elder 		return;
11631b65bbccSAlex Elder 
116484f9bd12SAlex Elder 	skb = __dev_alloc_skb(len, GFP_ATOMIC);
11651b65bbccSAlex Elder 	if (!skb)
11661b65bbccSAlex Elder 		return;
11671b65bbccSAlex Elder 
11681b65bbccSAlex Elder 	/* Copy the data into the socket buffer and receive it */
116984f9bd12SAlex Elder 	skb_put(skb, len);
117084f9bd12SAlex Elder 	memcpy(skb->data, data, len);
117184f9bd12SAlex Elder 	skb->truesize += extra;
117284f9bd12SAlex Elder 
117384f9bd12SAlex Elder 	ipa_modem_skb_rx(endpoint->netdev, skb);
117484f9bd12SAlex Elder }
117584f9bd12SAlex Elder 
117684f9bd12SAlex Elder static bool ipa_endpoint_skb_build(struct ipa_endpoint *endpoint,
117784f9bd12SAlex Elder 				   struct page *page, u32 len)
117884f9bd12SAlex Elder {
1179ed23f026SAlex Elder 	u32 buffer_size = endpoint->data->rx.buffer_size;
118084f9bd12SAlex Elder 	struct sk_buff *skb;
118184f9bd12SAlex Elder 
118284f9bd12SAlex Elder 	/* Nothing to do if there's no netdev */
118384f9bd12SAlex Elder 	if (!endpoint->netdev)
118484f9bd12SAlex Elder 		return false;
118584f9bd12SAlex Elder 
1186ed23f026SAlex Elder 	WARN_ON(len > SKB_WITH_OVERHEAD(buffer_size - NET_SKB_PAD));
11875bc55884SAlex Elder 
1188ed23f026SAlex Elder 	skb = build_skb(page_address(page), buffer_size);
118984f9bd12SAlex Elder 	if (skb) {
119084f9bd12SAlex Elder 		/* Reserve the headroom and account for the data */
119184f9bd12SAlex Elder 		skb_reserve(skb, NET_SKB_PAD);
119284f9bd12SAlex Elder 		skb_put(skb, len);
119384f9bd12SAlex Elder 	}
119484f9bd12SAlex Elder 
119584f9bd12SAlex Elder 	/* Receive the buffer (or record drop if unable to build it) */
119684f9bd12SAlex Elder 	ipa_modem_skb_rx(endpoint->netdev, skb);
119784f9bd12SAlex Elder 
119884f9bd12SAlex Elder 	return skb != NULL;
119984f9bd12SAlex Elder }
120084f9bd12SAlex Elder 
120184f9bd12SAlex Elder /* The format of a packet status element is the same for several status
120245921390SAlex Elder  * types (opcodes).  Other types aren't currently supported.
120384f9bd12SAlex Elder  */
120484f9bd12SAlex Elder static bool ipa_status_format_packet(enum ipa_status_opcode opcode)
120584f9bd12SAlex Elder {
120684f9bd12SAlex Elder 	switch (opcode) {
120784f9bd12SAlex Elder 	case IPA_STATUS_OPCODE_PACKET:
120884f9bd12SAlex Elder 	case IPA_STATUS_OPCODE_DROPPED_PACKET:
120984f9bd12SAlex Elder 	case IPA_STATUS_OPCODE_SUSPENDED_PACKET:
121084f9bd12SAlex Elder 	case IPA_STATUS_OPCODE_PACKET_2ND_PASS:
121184f9bd12SAlex Elder 		return true;
121284f9bd12SAlex Elder 	default:
121384f9bd12SAlex Elder 		return false;
121484f9bd12SAlex Elder 	}
121584f9bd12SAlex Elder }
121684f9bd12SAlex Elder 
121784f9bd12SAlex Elder static bool ipa_endpoint_status_skip(struct ipa_endpoint *endpoint,
121884f9bd12SAlex Elder 				     const struct ipa_status *status)
121984f9bd12SAlex Elder {
122084f9bd12SAlex Elder 	u32 endpoint_id;
122184f9bd12SAlex Elder 
122284f9bd12SAlex Elder 	if (!ipa_status_format_packet(status->opcode))
122384f9bd12SAlex Elder 		return true;
122484f9bd12SAlex Elder 	if (!status->pkt_len)
122584f9bd12SAlex Elder 		return true;
1226c13899f1SAlex Elder 	endpoint_id = u8_get_bits(status->endp_dst_idx,
122784f9bd12SAlex Elder 				  IPA_STATUS_DST_IDX_FMASK);
122884f9bd12SAlex Elder 	if (endpoint_id != endpoint->endpoint_id)
122984f9bd12SAlex Elder 		return true;
123084f9bd12SAlex Elder 
123184f9bd12SAlex Elder 	return false;	/* Don't skip this packet, process it */
123284f9bd12SAlex Elder }
123384f9bd12SAlex Elder 
1234f6aba7b5SAlex Elder static bool ipa_endpoint_status_tag(struct ipa_endpoint *endpoint,
1235f6aba7b5SAlex Elder 				    const struct ipa_status *status)
1236f6aba7b5SAlex Elder {
123751c48ce2SAlex Elder 	struct ipa_endpoint *command_endpoint;
123851c48ce2SAlex Elder 	struct ipa *ipa = endpoint->ipa;
123951c48ce2SAlex Elder 	u32 endpoint_id;
124051c48ce2SAlex Elder 
124151c48ce2SAlex Elder 	if (!le16_get_bits(status->mask, IPA_STATUS_MASK_TAG_VALID_FMASK))
124251c48ce2SAlex Elder 		return false;	/* No valid tag */
124351c48ce2SAlex Elder 
124451c48ce2SAlex Elder 	/* The status contains a valid tag.  We know the packet was sent to
124551c48ce2SAlex Elder 	 * this endpoint (already verified by ipa_endpoint_status_skip()).
124651c48ce2SAlex Elder 	 * If the packet came from the AP->command TX endpoint we know
124751c48ce2SAlex Elder 	 * this packet was sent as part of the pipeline clear process.
124851c48ce2SAlex Elder 	 */
124951c48ce2SAlex Elder 	endpoint_id = u8_get_bits(status->endp_src_idx,
125051c48ce2SAlex Elder 				  IPA_STATUS_SRC_IDX_FMASK);
125151c48ce2SAlex Elder 	command_endpoint = ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX];
125251c48ce2SAlex Elder 	if (endpoint_id == command_endpoint->endpoint_id) {
125351c48ce2SAlex Elder 		complete(&ipa->completion);
125451c48ce2SAlex Elder 	} else {
125551c48ce2SAlex Elder 		dev_err(&ipa->pdev->dev,
125651c48ce2SAlex Elder 			"unexpected tagged packet from endpoint %u\n",
125751c48ce2SAlex Elder 			endpoint_id);
125851c48ce2SAlex Elder 	}
125951c48ce2SAlex Elder 
126051c48ce2SAlex Elder 	return true;
1261f6aba7b5SAlex Elder }
1262f6aba7b5SAlex Elder 
126384f9bd12SAlex Elder /* Return whether the status indicates the packet should be dropped */
1264f6aba7b5SAlex Elder static bool ipa_endpoint_status_drop(struct ipa_endpoint *endpoint,
1265f6aba7b5SAlex Elder 				     const struct ipa_status *status)
126684f9bd12SAlex Elder {
126784f9bd12SAlex Elder 	u32 val;
126884f9bd12SAlex Elder 
1269f6aba7b5SAlex Elder 	/* If the status indicates a tagged transfer, we'll drop the packet */
1270f6aba7b5SAlex Elder 	if (ipa_endpoint_status_tag(endpoint, status))
1271f6aba7b5SAlex Elder 		return true;
1272f6aba7b5SAlex Elder 
1273ab4f71e5SAlex Elder 	/* Deaggregation exceptions we drop; all other types we consume */
127484f9bd12SAlex Elder 	if (status->exception)
127584f9bd12SAlex Elder 		return status->exception == IPA_STATUS_EXCEPTION_DEAGGR;
127684f9bd12SAlex Elder 
127784f9bd12SAlex Elder 	/* Drop the packet if it fails to match a routing rule; otherwise no */
127884f9bd12SAlex Elder 	val = le32_get_bits(status->flags1, IPA_STATUS_FLAGS1_RT_RULE_ID_FMASK);
127984f9bd12SAlex Elder 
128084f9bd12SAlex Elder 	return val == field_max(IPA_STATUS_FLAGS1_RT_RULE_ID_FMASK);
128184f9bd12SAlex Elder }
128284f9bd12SAlex Elder 
128384f9bd12SAlex Elder static void ipa_endpoint_status_parse(struct ipa_endpoint *endpoint,
128484f9bd12SAlex Elder 				      struct page *page, u32 total_len)
128584f9bd12SAlex Elder {
1286ed23f026SAlex Elder 	u32 buffer_size = endpoint->data->rx.buffer_size;
128784f9bd12SAlex Elder 	void *data = page_address(page) + NET_SKB_PAD;
1288ed23f026SAlex Elder 	u32 unused = buffer_size - total_len;
128984f9bd12SAlex Elder 	u32 resid = total_len;
129084f9bd12SAlex Elder 
129184f9bd12SAlex Elder 	while (resid) {
129284f9bd12SAlex Elder 		const struct ipa_status *status = data;
129384f9bd12SAlex Elder 		u32 align;
129484f9bd12SAlex Elder 		u32 len;
129584f9bd12SAlex Elder 
129684f9bd12SAlex Elder 		if (resid < sizeof(*status)) {
129784f9bd12SAlex Elder 			dev_err(&endpoint->ipa->pdev->dev,
129884f9bd12SAlex Elder 				"short message (%u bytes < %zu byte status)\n",
129984f9bd12SAlex Elder 				resid, sizeof(*status));
130084f9bd12SAlex Elder 			break;
130184f9bd12SAlex Elder 		}
130284f9bd12SAlex Elder 
130384f9bd12SAlex Elder 		/* Skip over status packets that lack packet data */
130484f9bd12SAlex Elder 		if (ipa_endpoint_status_skip(endpoint, status)) {
130584f9bd12SAlex Elder 			data += sizeof(*status);
130684f9bd12SAlex Elder 			resid -= sizeof(*status);
130784f9bd12SAlex Elder 			continue;
130884f9bd12SAlex Elder 		}
130984f9bd12SAlex Elder 
1310162fbc6fSAlex Elder 		/* Compute the amount of buffer space consumed by the packet,
1311162fbc6fSAlex Elder 		 * including the status element.  If the hardware is configured
1312162fbc6fSAlex Elder 		 * to pad packet data to an aligned boundary, account for that.
1313162fbc6fSAlex Elder 		 * And if checksum offload is enabled a trailer containing
1314162fbc6fSAlex Elder 		 * computed checksum information will be appended.
131584f9bd12SAlex Elder 		 */
131684f9bd12SAlex Elder 		align = endpoint->data->rx.pad_align ? : 1;
131784f9bd12SAlex Elder 		len = le16_to_cpu(status->pkt_len);
131884f9bd12SAlex Elder 		len = sizeof(*status) + ALIGN(len, align);
131984f9bd12SAlex Elder 		if (endpoint->data->checksum)
132084f9bd12SAlex Elder 			len += sizeof(struct rmnet_map_dl_csum_trailer);
132184f9bd12SAlex Elder 
1322f6aba7b5SAlex Elder 		if (!ipa_endpoint_status_drop(endpoint, status)) {
1323162fbc6fSAlex Elder 			void *data2;
1324162fbc6fSAlex Elder 			u32 extra;
1325162fbc6fSAlex Elder 			u32 len2;
132684f9bd12SAlex Elder 
132784f9bd12SAlex Elder 			/* Client receives only packet data (no status) */
1328162fbc6fSAlex Elder 			data2 = data + sizeof(*status);
1329162fbc6fSAlex Elder 			len2 = le16_to_cpu(status->pkt_len);
1330162fbc6fSAlex Elder 
1331162fbc6fSAlex Elder 			/* Have the true size reflect the extra unused space in
1332162fbc6fSAlex Elder 			 * the original receive buffer.  Distribute the "cost"
1333162fbc6fSAlex Elder 			 * proportionately across all aggregated packets in the
1334162fbc6fSAlex Elder 			 * buffer.
1335162fbc6fSAlex Elder 			 */
1336162fbc6fSAlex Elder 			extra = DIV_ROUND_CLOSEST(unused * len, total_len);
133784f9bd12SAlex Elder 			ipa_endpoint_skb_copy(endpoint, data2, len2, extra);
133884f9bd12SAlex Elder 		}
133984f9bd12SAlex Elder 
134084f9bd12SAlex Elder 		/* Consume status and the full packet it describes */
134184f9bd12SAlex Elder 		data += len;
134284f9bd12SAlex Elder 		resid -= len;
134384f9bd12SAlex Elder 	}
134484f9bd12SAlex Elder }
134584f9bd12SAlex Elder 
134684f9bd12SAlex Elder /* Complete a TX transaction, command or from ipa_endpoint_skb_tx() */
134784f9bd12SAlex Elder static void ipa_endpoint_tx_complete(struct ipa_endpoint *endpoint,
134884f9bd12SAlex Elder 				     struct gsi_trans *trans)
134984f9bd12SAlex Elder {
135084f9bd12SAlex Elder }
135184f9bd12SAlex Elder 
135284f9bd12SAlex Elder /* Complete transaction initiated in ipa_endpoint_replenish_one() */
135384f9bd12SAlex Elder static void ipa_endpoint_rx_complete(struct ipa_endpoint *endpoint,
135484f9bd12SAlex Elder 				     struct gsi_trans *trans)
135584f9bd12SAlex Elder {
135684f9bd12SAlex Elder 	struct page *page;
135784f9bd12SAlex Elder 
13584b22d841SAlex Elder 	ipa_endpoint_replenish(endpoint);
13594b22d841SAlex Elder 	atomic_inc(&endpoint->replenish_backlog);
136084f9bd12SAlex Elder 
136184f9bd12SAlex Elder 	if (trans->cancelled)
136284f9bd12SAlex Elder 		return;
136384f9bd12SAlex Elder 
136484f9bd12SAlex Elder 	/* Parse or build a socket buffer using the actual received length */
136584f9bd12SAlex Elder 	page = trans->data;
136684f9bd12SAlex Elder 	if (endpoint->data->status_enable)
136784f9bd12SAlex Elder 		ipa_endpoint_status_parse(endpoint, page, trans->len);
136884f9bd12SAlex Elder 	else if (ipa_endpoint_skb_build(endpoint, page, trans->len))
136984f9bd12SAlex Elder 		trans->data = NULL;	/* Pages have been consumed */
137084f9bd12SAlex Elder }
137184f9bd12SAlex Elder 
137284f9bd12SAlex Elder void ipa_endpoint_trans_complete(struct ipa_endpoint *endpoint,
137384f9bd12SAlex Elder 				 struct gsi_trans *trans)
137484f9bd12SAlex Elder {
137584f9bd12SAlex Elder 	if (endpoint->toward_ipa)
137684f9bd12SAlex Elder 		ipa_endpoint_tx_complete(endpoint, trans);
137784f9bd12SAlex Elder 	else
137884f9bd12SAlex Elder 		ipa_endpoint_rx_complete(endpoint, trans);
137984f9bd12SAlex Elder }
138084f9bd12SAlex Elder 
138184f9bd12SAlex Elder void ipa_endpoint_trans_release(struct ipa_endpoint *endpoint,
138284f9bd12SAlex Elder 				struct gsi_trans *trans)
138384f9bd12SAlex Elder {
138484f9bd12SAlex Elder 	if (endpoint->toward_ipa) {
138584f9bd12SAlex Elder 		struct ipa *ipa = endpoint->ipa;
138684f9bd12SAlex Elder 
138784f9bd12SAlex Elder 		/* Nothing to do for command transactions */
138884f9bd12SAlex Elder 		if (endpoint != ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]) {
138984f9bd12SAlex Elder 			struct sk_buff *skb = trans->data;
139084f9bd12SAlex Elder 
139184f9bd12SAlex Elder 			if (skb)
139284f9bd12SAlex Elder 				dev_kfree_skb_any(skb);
139384f9bd12SAlex Elder 		}
139484f9bd12SAlex Elder 	} else {
139584f9bd12SAlex Elder 		struct page *page = trans->data;
139684f9bd12SAlex Elder 
1397ed23f026SAlex Elder 		if (page) {
1398ed23f026SAlex Elder 			u32 buffer_size = endpoint->data->rx.buffer_size;
1399ed23f026SAlex Elder 
1400ed23f026SAlex Elder 			__free_pages(page, get_order(buffer_size));
1401ed23f026SAlex Elder 		}
140284f9bd12SAlex Elder 	}
140384f9bd12SAlex Elder }
140484f9bd12SAlex Elder 
140584f9bd12SAlex Elder void ipa_endpoint_default_route_set(struct ipa *ipa, u32 endpoint_id)
140684f9bd12SAlex Elder {
140784f9bd12SAlex Elder 	u32 val;
140884f9bd12SAlex Elder 
140984f9bd12SAlex Elder 	/* ROUTE_DIS is 0 */
141084f9bd12SAlex Elder 	val = u32_encode_bits(endpoint_id, ROUTE_DEF_PIPE_FMASK);
141184f9bd12SAlex Elder 	val |= ROUTE_DEF_HDR_TABLE_FMASK;
141284f9bd12SAlex Elder 	val |= u32_encode_bits(0, ROUTE_DEF_HDR_OFST_FMASK);
141384f9bd12SAlex Elder 	val |= u32_encode_bits(endpoint_id, ROUTE_FRAG_DEF_PIPE_FMASK);
141484f9bd12SAlex Elder 	val |= ROUTE_DEF_RETAIN_HDR_FMASK;
141584f9bd12SAlex Elder 
141684f9bd12SAlex Elder 	iowrite32(val, ipa->reg_virt + IPA_REG_ROUTE_OFFSET);
141784f9bd12SAlex Elder }
141884f9bd12SAlex Elder 
141984f9bd12SAlex Elder void ipa_endpoint_default_route_clear(struct ipa *ipa)
142084f9bd12SAlex Elder {
142184f9bd12SAlex Elder 	ipa_endpoint_default_route_set(ipa, 0);
142284f9bd12SAlex Elder }
142384f9bd12SAlex Elder 
142484f9bd12SAlex Elder /**
142584f9bd12SAlex Elder  * ipa_endpoint_reset_rx_aggr() - Reset RX endpoint with aggregation active
142684f9bd12SAlex Elder  * @endpoint:	Endpoint to be reset
142784f9bd12SAlex Elder  *
142884f9bd12SAlex Elder  * If aggregation is active on an RX endpoint when a reset is performed
142984f9bd12SAlex Elder  * on its underlying GSI channel, a special sequence of actions must be
143084f9bd12SAlex Elder  * taken to ensure the IPA pipeline is properly cleared.
143184f9bd12SAlex Elder  *
1432e3eea08eSAlex Elder  * Return:	0 if successful, or a negative error code
143384f9bd12SAlex Elder  */
143484f9bd12SAlex Elder static int ipa_endpoint_reset_rx_aggr(struct ipa_endpoint *endpoint)
143584f9bd12SAlex Elder {
143684f9bd12SAlex Elder 	struct device *dev = &endpoint->ipa->pdev->dev;
143784f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
143884f9bd12SAlex Elder 	struct gsi *gsi = &ipa->gsi;
14394fa95248SAlex Elder 	bool suspended = false;
144084f9bd12SAlex Elder 	dma_addr_t addr;
144184f9bd12SAlex Elder 	u32 retries;
144284f9bd12SAlex Elder 	u32 len = 1;
144384f9bd12SAlex Elder 	void *virt;
144484f9bd12SAlex Elder 	int ret;
144584f9bd12SAlex Elder 
144684f9bd12SAlex Elder 	virt = kzalloc(len, GFP_KERNEL);
144784f9bd12SAlex Elder 	if (!virt)
144884f9bd12SAlex Elder 		return -ENOMEM;
144984f9bd12SAlex Elder 
145084f9bd12SAlex Elder 	addr = dma_map_single(dev, virt, len, DMA_FROM_DEVICE);
145184f9bd12SAlex Elder 	if (dma_mapping_error(dev, addr)) {
145284f9bd12SAlex Elder 		ret = -ENOMEM;
145384f9bd12SAlex Elder 		goto out_kfree;
145484f9bd12SAlex Elder 	}
145584f9bd12SAlex Elder 
145684f9bd12SAlex Elder 	/* Force close aggregation before issuing the reset */
145784f9bd12SAlex Elder 	ipa_endpoint_force_close(endpoint);
145884f9bd12SAlex Elder 
145984f9bd12SAlex Elder 	/* Reset and reconfigure the channel with the doorbell engine
146084f9bd12SAlex Elder 	 * disabled.  Then poll until we know aggregation is no longer
146184f9bd12SAlex Elder 	 * active.  We'll re-enable the doorbell (if appropriate) when
146284f9bd12SAlex Elder 	 * we reset again below.
146384f9bd12SAlex Elder 	 */
146484f9bd12SAlex Elder 	gsi_channel_reset(gsi, endpoint->channel_id, false);
146584f9bd12SAlex Elder 
146684f9bd12SAlex Elder 	/* Make sure the channel isn't suspended */
14674fa95248SAlex Elder 	suspended = ipa_endpoint_program_suspend(endpoint, false);
146884f9bd12SAlex Elder 
146984f9bd12SAlex Elder 	/* Start channel and do a 1 byte read */
147084f9bd12SAlex Elder 	ret = gsi_channel_start(gsi, endpoint->channel_id);
147184f9bd12SAlex Elder 	if (ret)
147284f9bd12SAlex Elder 		goto out_suspend_again;
147384f9bd12SAlex Elder 
147484f9bd12SAlex Elder 	ret = gsi_trans_read_byte(gsi, endpoint->channel_id, addr);
147584f9bd12SAlex Elder 	if (ret)
147684f9bd12SAlex Elder 		goto err_endpoint_stop;
147784f9bd12SAlex Elder 
147884f9bd12SAlex Elder 	/* Wait for aggregation to be closed on the channel */
147984f9bd12SAlex Elder 	retries = IPA_ENDPOINT_RESET_AGGR_RETRY_MAX;
148084f9bd12SAlex Elder 	do {
148184f9bd12SAlex Elder 		if (!ipa_endpoint_aggr_active(endpoint))
148284f9bd12SAlex Elder 			break;
148374401946SAlex Elder 		usleep_range(USEC_PER_MSEC, 2 * USEC_PER_MSEC);
148484f9bd12SAlex Elder 	} while (retries--);
148584f9bd12SAlex Elder 
148684f9bd12SAlex Elder 	/* Check one last time */
148784f9bd12SAlex Elder 	if (ipa_endpoint_aggr_active(endpoint))
148884f9bd12SAlex Elder 		dev_err(dev, "endpoint %u still active during reset\n",
148984f9bd12SAlex Elder 			endpoint->endpoint_id);
149084f9bd12SAlex Elder 
149184f9bd12SAlex Elder 	gsi_trans_read_byte_done(gsi, endpoint->channel_id);
149284f9bd12SAlex Elder 
1493f30dcb7dSAlex Elder 	ret = gsi_channel_stop(gsi, endpoint->channel_id);
149484f9bd12SAlex Elder 	if (ret)
149584f9bd12SAlex Elder 		goto out_suspend_again;
149684f9bd12SAlex Elder 
1497497abc87SPeng Li 	/* Finally, reset and reconfigure the channel again (re-enabling
149884f9bd12SAlex Elder 	 * the doorbell engine if appropriate).  Sleep for 1 millisecond to
149984f9bd12SAlex Elder 	 * complete the channel reset sequence.  Finish by suspending the
150084f9bd12SAlex Elder 	 * channel again (if necessary).
150184f9bd12SAlex Elder 	 */
1502ce54993dSAlex Elder 	gsi_channel_reset(gsi, endpoint->channel_id, true);
150384f9bd12SAlex Elder 
150474401946SAlex Elder 	usleep_range(USEC_PER_MSEC, 2 * USEC_PER_MSEC);
150584f9bd12SAlex Elder 
150684f9bd12SAlex Elder 	goto out_suspend_again;
150784f9bd12SAlex Elder 
150884f9bd12SAlex Elder err_endpoint_stop:
1509f30dcb7dSAlex Elder 	(void)gsi_channel_stop(gsi, endpoint->channel_id);
151084f9bd12SAlex Elder out_suspend_again:
15114fa95248SAlex Elder 	if (suspended)
15124fa95248SAlex Elder 		(void)ipa_endpoint_program_suspend(endpoint, true);
151384f9bd12SAlex Elder 	dma_unmap_single(dev, addr, len, DMA_FROM_DEVICE);
151484f9bd12SAlex Elder out_kfree:
151584f9bd12SAlex Elder 	kfree(virt);
151684f9bd12SAlex Elder 
151784f9bd12SAlex Elder 	return ret;
151884f9bd12SAlex Elder }
151984f9bd12SAlex Elder 
152084f9bd12SAlex Elder static void ipa_endpoint_reset(struct ipa_endpoint *endpoint)
152184f9bd12SAlex Elder {
152284f9bd12SAlex Elder 	u32 channel_id = endpoint->channel_id;
152384f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
152484f9bd12SAlex Elder 	bool special;
152584f9bd12SAlex Elder 	int ret = 0;
152684f9bd12SAlex Elder 
152784f9bd12SAlex Elder 	/* On IPA v3.5.1, if an RX endpoint is reset while aggregation
152884f9bd12SAlex Elder 	 * is active, we need to handle things specially to recover.
152984f9bd12SAlex Elder 	 * All other cases just need to reset the underlying GSI channel.
153084f9bd12SAlex Elder 	 */
1531d7f3087bSAlex Elder 	special = ipa->version < IPA_VERSION_4_0 && !endpoint->toward_ipa &&
1532ce54993dSAlex Elder 			endpoint->data->aggregation;
1533ce54993dSAlex Elder 	if (special && ipa_endpoint_aggr_active(endpoint))
153484f9bd12SAlex Elder 		ret = ipa_endpoint_reset_rx_aggr(endpoint);
153584f9bd12SAlex Elder 	else
1536ce54993dSAlex Elder 		gsi_channel_reset(&ipa->gsi, channel_id, true);
153784f9bd12SAlex Elder 
153884f9bd12SAlex Elder 	if (ret)
153984f9bd12SAlex Elder 		dev_err(&ipa->pdev->dev,
154084f9bd12SAlex Elder 			"error %d resetting channel %u for endpoint %u\n",
154184f9bd12SAlex Elder 			ret, endpoint->channel_id, endpoint->endpoint_id);
154284f9bd12SAlex Elder }
154384f9bd12SAlex Elder 
154484f9bd12SAlex Elder static void ipa_endpoint_program(struct ipa_endpoint *endpoint)
154584f9bd12SAlex Elder {
15464c9d631aSAlex Elder 	if (endpoint->toward_ipa) {
15474c9d631aSAlex Elder 		/* Newer versions of IPA use GSI channel flow control
15484c9d631aSAlex Elder 		 * instead of endpoint DELAY mode to prevent sending data.
15494c9d631aSAlex Elder 		 * Flow control is disabled for newly-allocated channels,
15504c9d631aSAlex Elder 		 * and we can assume flow control is not (ever) enabled
15514c9d631aSAlex Elder 		 * for AP TX channels.
15524c9d631aSAlex Elder 		 */
15534c9d631aSAlex Elder 		if (endpoint->ipa->version < IPA_VERSION_4_2)
1554a4dcad34SAlex Elder 			ipa_endpoint_program_delay(endpoint, false);
15554c9d631aSAlex Elder 	} else {
15564c9d631aSAlex Elder 		/* Ensure suspend mode is off on all AP RX endpoints */
1557fb57c3eaSAlex Elder 		(void)ipa_endpoint_program_suspend(endpoint, false);
15584c9d631aSAlex Elder 	}
1559fb57c3eaSAlex Elder 	ipa_endpoint_init_cfg(endpoint);
1560647a05f3SAlex Elder 	ipa_endpoint_init_nat(endpoint);
1561fb57c3eaSAlex Elder 	ipa_endpoint_init_hdr(endpoint);
156284f9bd12SAlex Elder 	ipa_endpoint_init_hdr_ext(endpoint);
1563fb57c3eaSAlex Elder 	ipa_endpoint_init_hdr_metadata_mask(endpoint);
1564fb57c3eaSAlex Elder 	ipa_endpoint_init_mode(endpoint);
156584f9bd12SAlex Elder 	ipa_endpoint_init_aggr(endpoint);
156601c36637SAlex Elder 	if (!endpoint->toward_ipa)
156701c36637SAlex Elder 		ipa_endpoint_init_hol_block_disable(endpoint);
156884f9bd12SAlex Elder 	ipa_endpoint_init_deaggr(endpoint);
15692d265342SAlex Elder 	ipa_endpoint_init_rsrc_grp(endpoint);
157084f9bd12SAlex Elder 	ipa_endpoint_init_seq(endpoint);
157184f9bd12SAlex Elder 	ipa_endpoint_status(endpoint);
157284f9bd12SAlex Elder }
157384f9bd12SAlex Elder 
157484f9bd12SAlex Elder int ipa_endpoint_enable_one(struct ipa_endpoint *endpoint)
157584f9bd12SAlex Elder {
157684f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
157784f9bd12SAlex Elder 	struct gsi *gsi = &ipa->gsi;
157884f9bd12SAlex Elder 	int ret;
157984f9bd12SAlex Elder 
158084f9bd12SAlex Elder 	ret = gsi_channel_start(gsi, endpoint->channel_id);
158184f9bd12SAlex Elder 	if (ret) {
158284f9bd12SAlex Elder 		dev_err(&ipa->pdev->dev,
158384f9bd12SAlex Elder 			"error %d starting %cX channel %u for endpoint %u\n",
158484f9bd12SAlex Elder 			ret, endpoint->toward_ipa ? 'T' : 'R',
158584f9bd12SAlex Elder 			endpoint->channel_id, endpoint->endpoint_id);
158684f9bd12SAlex Elder 		return ret;
158784f9bd12SAlex Elder 	}
158884f9bd12SAlex Elder 
158984f9bd12SAlex Elder 	if (!endpoint->toward_ipa) {
159084f9bd12SAlex Elder 		ipa_interrupt_suspend_enable(ipa->interrupt,
159184f9bd12SAlex Elder 					     endpoint->endpoint_id);
159284f9bd12SAlex Elder 		ipa_endpoint_replenish_enable(endpoint);
159384f9bd12SAlex Elder 	}
159484f9bd12SAlex Elder 
159584f9bd12SAlex Elder 	ipa->enabled |= BIT(endpoint->endpoint_id);
159684f9bd12SAlex Elder 
159784f9bd12SAlex Elder 	return 0;
159884f9bd12SAlex Elder }
159984f9bd12SAlex Elder 
160084f9bd12SAlex Elder void ipa_endpoint_disable_one(struct ipa_endpoint *endpoint)
160184f9bd12SAlex Elder {
160284f9bd12SAlex Elder 	u32 mask = BIT(endpoint->endpoint_id);
160384f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
1604f30dcb7dSAlex Elder 	struct gsi *gsi = &ipa->gsi;
160584f9bd12SAlex Elder 	int ret;
160684f9bd12SAlex Elder 
1607f30dcb7dSAlex Elder 	if (!(ipa->enabled & mask))
160884f9bd12SAlex Elder 		return;
160984f9bd12SAlex Elder 
1610f30dcb7dSAlex Elder 	ipa->enabled ^= mask;
161184f9bd12SAlex Elder 
161284f9bd12SAlex Elder 	if (!endpoint->toward_ipa) {
161384f9bd12SAlex Elder 		ipa_endpoint_replenish_disable(endpoint);
161484f9bd12SAlex Elder 		ipa_interrupt_suspend_disable(ipa->interrupt,
161584f9bd12SAlex Elder 					      endpoint->endpoint_id);
161684f9bd12SAlex Elder 	}
161784f9bd12SAlex Elder 
161884f9bd12SAlex Elder 	/* Note that if stop fails, the channel's state is not well-defined */
1619f30dcb7dSAlex Elder 	ret = gsi_channel_stop(gsi, endpoint->channel_id);
162084f9bd12SAlex Elder 	if (ret)
162184f9bd12SAlex Elder 		dev_err(&ipa->pdev->dev,
162284f9bd12SAlex Elder 			"error %d attempting to stop endpoint %u\n", ret,
162384f9bd12SAlex Elder 			endpoint->endpoint_id);
162484f9bd12SAlex Elder }
162584f9bd12SAlex Elder 
162684f9bd12SAlex Elder void ipa_endpoint_suspend_one(struct ipa_endpoint *endpoint)
162784f9bd12SAlex Elder {
162884f9bd12SAlex Elder 	struct device *dev = &endpoint->ipa->pdev->dev;
162984f9bd12SAlex Elder 	struct gsi *gsi = &endpoint->ipa->gsi;
163084f9bd12SAlex Elder 	int ret;
163184f9bd12SAlex Elder 
163284f9bd12SAlex Elder 	if (!(endpoint->ipa->enabled & BIT(endpoint->endpoint_id)))
163384f9bd12SAlex Elder 		return;
163484f9bd12SAlex Elder 
1635ab4f71e5SAlex Elder 	if (!endpoint->toward_ipa) {
163684f9bd12SAlex Elder 		ipa_endpoint_replenish_disable(endpoint);
16374fa95248SAlex Elder 		(void)ipa_endpoint_program_suspend(endpoint, true);
1638ab4f71e5SAlex Elder 	}
163984f9bd12SAlex Elder 
1640decfef0fSAlex Elder 	ret = gsi_channel_suspend(gsi, endpoint->channel_id);
164184f9bd12SAlex Elder 	if (ret)
164284f9bd12SAlex Elder 		dev_err(dev, "error %d suspending channel %u\n", ret,
164384f9bd12SAlex Elder 			endpoint->channel_id);
164484f9bd12SAlex Elder }
164584f9bd12SAlex Elder 
164684f9bd12SAlex Elder void ipa_endpoint_resume_one(struct ipa_endpoint *endpoint)
164784f9bd12SAlex Elder {
164884f9bd12SAlex Elder 	struct device *dev = &endpoint->ipa->pdev->dev;
164984f9bd12SAlex Elder 	struct gsi *gsi = &endpoint->ipa->gsi;
165084f9bd12SAlex Elder 	int ret;
165184f9bd12SAlex Elder 
165284f9bd12SAlex Elder 	if (!(endpoint->ipa->enabled & BIT(endpoint->endpoint_id)))
165384f9bd12SAlex Elder 		return;
165484f9bd12SAlex Elder 
1655b07f283eSAlex Elder 	if (!endpoint->toward_ipa)
16564fa95248SAlex Elder 		(void)ipa_endpoint_program_suspend(endpoint, false);
165784f9bd12SAlex Elder 
1658decfef0fSAlex Elder 	ret = gsi_channel_resume(gsi, endpoint->channel_id);
165984f9bd12SAlex Elder 	if (ret)
166084f9bd12SAlex Elder 		dev_err(dev, "error %d resuming channel %u\n", ret,
166184f9bd12SAlex Elder 			endpoint->channel_id);
166284f9bd12SAlex Elder 	else if (!endpoint->toward_ipa)
166384f9bd12SAlex Elder 		ipa_endpoint_replenish_enable(endpoint);
166484f9bd12SAlex Elder }
166584f9bd12SAlex Elder 
166684f9bd12SAlex Elder void ipa_endpoint_suspend(struct ipa *ipa)
166784f9bd12SAlex Elder {
1668d1704382SAlex Elder 	if (!ipa->setup_complete)
1669d1704382SAlex Elder 		return;
1670d1704382SAlex Elder 
167184f9bd12SAlex Elder 	if (ipa->modem_netdev)
167284f9bd12SAlex Elder 		ipa_modem_suspend(ipa->modem_netdev);
167384f9bd12SAlex Elder 
167484f9bd12SAlex Elder 	ipa_endpoint_suspend_one(ipa->name_map[IPA_ENDPOINT_AP_LAN_RX]);
167584f9bd12SAlex Elder 	ipa_endpoint_suspend_one(ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]);
167684f9bd12SAlex Elder }
167784f9bd12SAlex Elder 
167884f9bd12SAlex Elder void ipa_endpoint_resume(struct ipa *ipa)
167984f9bd12SAlex Elder {
1680d1704382SAlex Elder 	if (!ipa->setup_complete)
1681d1704382SAlex Elder 		return;
1682d1704382SAlex Elder 
168384f9bd12SAlex Elder 	ipa_endpoint_resume_one(ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]);
168484f9bd12SAlex Elder 	ipa_endpoint_resume_one(ipa->name_map[IPA_ENDPOINT_AP_LAN_RX]);
168584f9bd12SAlex Elder 
168684f9bd12SAlex Elder 	if (ipa->modem_netdev)
168784f9bd12SAlex Elder 		ipa_modem_resume(ipa->modem_netdev);
168884f9bd12SAlex Elder }
168984f9bd12SAlex Elder 
169084f9bd12SAlex Elder static void ipa_endpoint_setup_one(struct ipa_endpoint *endpoint)
169184f9bd12SAlex Elder {
169284f9bd12SAlex Elder 	struct gsi *gsi = &endpoint->ipa->gsi;
169384f9bd12SAlex Elder 	u32 channel_id = endpoint->channel_id;
169484f9bd12SAlex Elder 
169584f9bd12SAlex Elder 	/* Only AP endpoints get set up */
169684f9bd12SAlex Elder 	if (endpoint->ee_id != GSI_EE_AP)
169784f9bd12SAlex Elder 		return;
169884f9bd12SAlex Elder 
169984f9bd12SAlex Elder 	endpoint->trans_tre_max = gsi_channel_trans_tre_max(gsi, channel_id);
170084f9bd12SAlex Elder 	if (!endpoint->toward_ipa) {
170184f9bd12SAlex Elder 		/* RX transactions require a single TRE, so the maximum
170284f9bd12SAlex Elder 		 * backlog is the same as the maximum outstanding TREs.
170384f9bd12SAlex Elder 		 */
1704c1aaa01dSAlex Elder 		clear_bit(IPA_REPLENISH_ENABLED, endpoint->replenish_flags);
1705998c0bd2SAlex Elder 		clear_bit(IPA_REPLENISH_ACTIVE, endpoint->replenish_flags);
1706a9bec7aeSAlex Elder 		atomic_set(&endpoint->replenish_backlog,
170784f9bd12SAlex Elder 			   gsi_channel_tre_max(gsi, endpoint->channel_id));
170884f9bd12SAlex Elder 		INIT_DELAYED_WORK(&endpoint->replenish_work,
170984f9bd12SAlex Elder 				  ipa_endpoint_replenish_work);
171084f9bd12SAlex Elder 	}
171184f9bd12SAlex Elder 
171284f9bd12SAlex Elder 	ipa_endpoint_program(endpoint);
171384f9bd12SAlex Elder 
171484f9bd12SAlex Elder 	endpoint->ipa->set_up |= BIT(endpoint->endpoint_id);
171584f9bd12SAlex Elder }
171684f9bd12SAlex Elder 
171784f9bd12SAlex Elder static void ipa_endpoint_teardown_one(struct ipa_endpoint *endpoint)
171884f9bd12SAlex Elder {
171984f9bd12SAlex Elder 	endpoint->ipa->set_up &= ~BIT(endpoint->endpoint_id);
172084f9bd12SAlex Elder 
172184f9bd12SAlex Elder 	if (!endpoint->toward_ipa)
172284f9bd12SAlex Elder 		cancel_delayed_work_sync(&endpoint->replenish_work);
172384f9bd12SAlex Elder 
172484f9bd12SAlex Elder 	ipa_endpoint_reset(endpoint);
172584f9bd12SAlex Elder }
172684f9bd12SAlex Elder 
172784f9bd12SAlex Elder void ipa_endpoint_setup(struct ipa *ipa)
172884f9bd12SAlex Elder {
172984f9bd12SAlex Elder 	u32 initialized = ipa->initialized;
173084f9bd12SAlex Elder 
173184f9bd12SAlex Elder 	ipa->set_up = 0;
173284f9bd12SAlex Elder 	while (initialized) {
173384f9bd12SAlex Elder 		u32 endpoint_id = __ffs(initialized);
173484f9bd12SAlex Elder 
173584f9bd12SAlex Elder 		initialized ^= BIT(endpoint_id);
173684f9bd12SAlex Elder 
173784f9bd12SAlex Elder 		ipa_endpoint_setup_one(&ipa->endpoint[endpoint_id]);
173884f9bd12SAlex Elder 	}
173984f9bd12SAlex Elder }
174084f9bd12SAlex Elder 
174184f9bd12SAlex Elder void ipa_endpoint_teardown(struct ipa *ipa)
174284f9bd12SAlex Elder {
174384f9bd12SAlex Elder 	u32 set_up = ipa->set_up;
174484f9bd12SAlex Elder 
174584f9bd12SAlex Elder 	while (set_up) {
174684f9bd12SAlex Elder 		u32 endpoint_id = __fls(set_up);
174784f9bd12SAlex Elder 
174884f9bd12SAlex Elder 		set_up ^= BIT(endpoint_id);
174984f9bd12SAlex Elder 
175084f9bd12SAlex Elder 		ipa_endpoint_teardown_one(&ipa->endpoint[endpoint_id]);
175184f9bd12SAlex Elder 	}
175284f9bd12SAlex Elder 	ipa->set_up = 0;
175384f9bd12SAlex Elder }
175484f9bd12SAlex Elder 
175584f9bd12SAlex Elder int ipa_endpoint_config(struct ipa *ipa)
175684f9bd12SAlex Elder {
175784f9bd12SAlex Elder 	struct device *dev = &ipa->pdev->dev;
175884f9bd12SAlex Elder 	u32 initialized;
175984f9bd12SAlex Elder 	u32 rx_base;
176084f9bd12SAlex Elder 	u32 rx_mask;
176184f9bd12SAlex Elder 	u32 tx_mask;
176284f9bd12SAlex Elder 	int ret = 0;
176384f9bd12SAlex Elder 	u32 max;
176484f9bd12SAlex Elder 	u32 val;
176584f9bd12SAlex Elder 
1766110971d1SAlex Elder 	/* Prior to IPAv3.5, the FLAVOR_0 register was not supported.
1767110971d1SAlex Elder 	 * Furthermore, the endpoints were not grouped such that TX
1768110971d1SAlex Elder 	 * endpoint numbers started with 0 and RX endpoints had numbers
1769110971d1SAlex Elder 	 * higher than all TX endpoints, so we can't do the simple
1770110971d1SAlex Elder 	 * direction check used for newer hardware below.
1771110971d1SAlex Elder 	 *
1772110971d1SAlex Elder 	 * For hardware that doesn't support the FLAVOR_0 register,
1773110971d1SAlex Elder 	 * just set the available mask to support any endpoint, and
1774110971d1SAlex Elder 	 * assume the configuration is valid.
1775110971d1SAlex Elder 	 */
1776110971d1SAlex Elder 	if (ipa->version < IPA_VERSION_3_5) {
1777110971d1SAlex Elder 		ipa->available = ~0;
1778110971d1SAlex Elder 		return 0;
1779110971d1SAlex Elder 	}
1780110971d1SAlex Elder 
178184f9bd12SAlex Elder 	/* Find out about the endpoints supplied by the hardware, and ensure
178284f9bd12SAlex Elder 	 * the highest one doesn't exceed the number we support.
178384f9bd12SAlex Elder 	 */
178484f9bd12SAlex Elder 	val = ioread32(ipa->reg_virt + IPA_REG_FLAVOR_0_OFFSET);
178584f9bd12SAlex Elder 
178684f9bd12SAlex Elder 	/* Our RX is an IPA producer */
1787716a115bSAlex Elder 	rx_base = u32_get_bits(val, IPA_PROD_LOWEST_FMASK);
1788716a115bSAlex Elder 	max = rx_base + u32_get_bits(val, IPA_MAX_PROD_PIPES_FMASK);
178984f9bd12SAlex Elder 	if (max > IPA_ENDPOINT_MAX) {
179084f9bd12SAlex Elder 		dev_err(dev, "too many endpoints (%u > %u)\n",
179184f9bd12SAlex Elder 			max, IPA_ENDPOINT_MAX);
179284f9bd12SAlex Elder 		return -EINVAL;
179384f9bd12SAlex Elder 	}
179484f9bd12SAlex Elder 	rx_mask = GENMASK(max - 1, rx_base);
179584f9bd12SAlex Elder 
179684f9bd12SAlex Elder 	/* Our TX is an IPA consumer */
1797716a115bSAlex Elder 	max = u32_get_bits(val, IPA_MAX_CONS_PIPES_FMASK);
179884f9bd12SAlex Elder 	tx_mask = GENMASK(max - 1, 0);
179984f9bd12SAlex Elder 
180084f9bd12SAlex Elder 	ipa->available = rx_mask | tx_mask;
180184f9bd12SAlex Elder 
180284f9bd12SAlex Elder 	/* Check for initialized endpoints not supported by the hardware */
180384f9bd12SAlex Elder 	if (ipa->initialized & ~ipa->available) {
180484f9bd12SAlex Elder 		dev_err(dev, "unavailable endpoint id(s) 0x%08x\n",
180584f9bd12SAlex Elder 			ipa->initialized & ~ipa->available);
180684f9bd12SAlex Elder 		ret = -EINVAL;		/* Report other errors too */
180784f9bd12SAlex Elder 	}
180884f9bd12SAlex Elder 
180984f9bd12SAlex Elder 	initialized = ipa->initialized;
181084f9bd12SAlex Elder 	while (initialized) {
181184f9bd12SAlex Elder 		u32 endpoint_id = __ffs(initialized);
181284f9bd12SAlex Elder 		struct ipa_endpoint *endpoint;
181384f9bd12SAlex Elder 
181484f9bd12SAlex Elder 		initialized ^= BIT(endpoint_id);
181584f9bd12SAlex Elder 
181684f9bd12SAlex Elder 		/* Make sure it's pointing in the right direction */
181784f9bd12SAlex Elder 		endpoint = &ipa->endpoint[endpoint_id];
1818602a1c76SAlex Elder 		if ((endpoint_id < rx_base) != endpoint->toward_ipa) {
181984f9bd12SAlex Elder 			dev_err(dev, "endpoint id %u wrong direction\n",
182084f9bd12SAlex Elder 				endpoint_id);
182184f9bd12SAlex Elder 			ret = -EINVAL;
182284f9bd12SAlex Elder 		}
182384f9bd12SAlex Elder 	}
182484f9bd12SAlex Elder 
182584f9bd12SAlex Elder 	return ret;
182684f9bd12SAlex Elder }
182784f9bd12SAlex Elder 
182884f9bd12SAlex Elder void ipa_endpoint_deconfig(struct ipa *ipa)
182984f9bd12SAlex Elder {
183084f9bd12SAlex Elder 	ipa->available = 0;	/* Nothing more to do */
183184f9bd12SAlex Elder }
183284f9bd12SAlex Elder 
183384f9bd12SAlex Elder static void ipa_endpoint_init_one(struct ipa *ipa, enum ipa_endpoint_name name,
183484f9bd12SAlex Elder 				  const struct ipa_gsi_endpoint_data *data)
183584f9bd12SAlex Elder {
183684f9bd12SAlex Elder 	struct ipa_endpoint *endpoint;
183784f9bd12SAlex Elder 
183884f9bd12SAlex Elder 	endpoint = &ipa->endpoint[data->endpoint_id];
183984f9bd12SAlex Elder 
184084f9bd12SAlex Elder 	if (data->ee_id == GSI_EE_AP)
184184f9bd12SAlex Elder 		ipa->channel_map[data->channel_id] = endpoint;
184284f9bd12SAlex Elder 	ipa->name_map[name] = endpoint;
184384f9bd12SAlex Elder 
184484f9bd12SAlex Elder 	endpoint->ipa = ipa;
184584f9bd12SAlex Elder 	endpoint->ee_id = data->ee_id;
184684f9bd12SAlex Elder 	endpoint->channel_id = data->channel_id;
184784f9bd12SAlex Elder 	endpoint->endpoint_id = data->endpoint_id;
184884f9bd12SAlex Elder 	endpoint->toward_ipa = data->toward_ipa;
184984f9bd12SAlex Elder 	endpoint->data = &data->endpoint.config;
185084f9bd12SAlex Elder 
185184f9bd12SAlex Elder 	ipa->initialized |= BIT(endpoint->endpoint_id);
185284f9bd12SAlex Elder }
185384f9bd12SAlex Elder 
1854602a1c76SAlex Elder static void ipa_endpoint_exit_one(struct ipa_endpoint *endpoint)
185584f9bd12SAlex Elder {
185684f9bd12SAlex Elder 	endpoint->ipa->initialized &= ~BIT(endpoint->endpoint_id);
185784f9bd12SAlex Elder 
185884f9bd12SAlex Elder 	memset(endpoint, 0, sizeof(*endpoint));
185984f9bd12SAlex Elder }
186084f9bd12SAlex Elder 
186184f9bd12SAlex Elder void ipa_endpoint_exit(struct ipa *ipa)
186284f9bd12SAlex Elder {
186384f9bd12SAlex Elder 	u32 initialized = ipa->initialized;
186484f9bd12SAlex Elder 
186584f9bd12SAlex Elder 	while (initialized) {
186684f9bd12SAlex Elder 		u32 endpoint_id = __fls(initialized);
186784f9bd12SAlex Elder 
186884f9bd12SAlex Elder 		initialized ^= BIT(endpoint_id);
186984f9bd12SAlex Elder 
187084f9bd12SAlex Elder 		ipa_endpoint_exit_one(&ipa->endpoint[endpoint_id]);
187184f9bd12SAlex Elder 	}
187284f9bd12SAlex Elder 	memset(ipa->name_map, 0, sizeof(ipa->name_map));
187384f9bd12SAlex Elder 	memset(ipa->channel_map, 0, sizeof(ipa->channel_map));
187484f9bd12SAlex Elder }
187584f9bd12SAlex Elder 
187684f9bd12SAlex Elder /* Returns a bitmask of endpoints that support filtering, or 0 on error */
187784f9bd12SAlex Elder u32 ipa_endpoint_init(struct ipa *ipa, u32 count,
187884f9bd12SAlex Elder 		      const struct ipa_gsi_endpoint_data *data)
187984f9bd12SAlex Elder {
188084f9bd12SAlex Elder 	enum ipa_endpoint_name name;
188184f9bd12SAlex Elder 	u32 filter_map;
188284f9bd12SAlex Elder 
188384f9bd12SAlex Elder 	if (!ipa_endpoint_data_valid(ipa, count, data))
188484f9bd12SAlex Elder 		return 0;	/* Error */
188584f9bd12SAlex Elder 
188684f9bd12SAlex Elder 	ipa->initialized = 0;
188784f9bd12SAlex Elder 
188884f9bd12SAlex Elder 	filter_map = 0;
188984f9bd12SAlex Elder 	for (name = 0; name < count; name++, data++) {
189084f9bd12SAlex Elder 		if (ipa_gsi_endpoint_data_empty(data))
189184f9bd12SAlex Elder 			continue;	/* Skip over empty slots */
189284f9bd12SAlex Elder 
189384f9bd12SAlex Elder 		ipa_endpoint_init_one(ipa, name, data);
189484f9bd12SAlex Elder 
189584f9bd12SAlex Elder 		if (data->endpoint.filter_support)
189684f9bd12SAlex Elder 			filter_map |= BIT(data->endpoint_id);
189784f9bd12SAlex Elder 	}
189884f9bd12SAlex Elder 
189984f9bd12SAlex Elder 	if (!ipa_filter_map_valid(ipa, filter_map))
190084f9bd12SAlex Elder 		goto err_endpoint_exit;
190184f9bd12SAlex Elder 
190284f9bd12SAlex Elder 	return filter_map;	/* Non-zero bitmask */
190384f9bd12SAlex Elder 
190484f9bd12SAlex Elder err_endpoint_exit:
190584f9bd12SAlex Elder 	ipa_endpoint_exit(ipa);
190684f9bd12SAlex Elder 
190784f9bd12SAlex Elder 	return 0;	/* Error */
190884f9bd12SAlex Elder }
1909