xref: /linux/drivers/net/ipa/ipa_endpoint.c (revision f6aba7b5199ad2a73a3deb6c042dee770650719c)
184f9bd12SAlex Elder // SPDX-License-Identifier: GPL-2.0
284f9bd12SAlex Elder 
384f9bd12SAlex Elder /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
484f9bd12SAlex Elder  * Copyright (C) 2019-2020 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"
24f13a8c31SAlex Elder #include "ipa_clock.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 
306fcd4224SAlex Elder /* RX buffer is 1 page (or a power-of-2 contiguous pages) */
316fcd4224SAlex Elder #define IPA_RX_BUFFER_SIZE	8192	/* PAGE_SIZE > 4096 wastes a LOT */
3284f9bd12SAlex Elder 
3384f9bd12SAlex Elder /* The amount of RX buffer space consumed by standard skb overhead */
3484f9bd12SAlex Elder #define IPA_RX_BUFFER_OVERHEAD	(PAGE_SIZE - SKB_MAX_ORDER(NET_SKB_PAD, 0))
3584f9bd12SAlex Elder 
368730f45dSAlex Elder /* Where to find the QMAP mux_id for a packet within modem-supplied metadata */
378730f45dSAlex Elder #define IPA_ENDPOINT_QMAP_METADATA_MASK		0x000000ff /* host byte order */
388730f45dSAlex Elder 
3984f9bd12SAlex Elder #define IPA_ENDPOINT_RESET_AGGR_RETRY_MAX	3
406bf754c7SAlex Elder #define IPA_AGGR_TIME_LIMIT			500	/* microseconds */
4184f9bd12SAlex Elder 
4284f9bd12SAlex Elder /** enum ipa_status_opcode - status element opcode hardware values */
4384f9bd12SAlex Elder enum ipa_status_opcode {
4484f9bd12SAlex Elder 	IPA_STATUS_OPCODE_PACKET		= 0x01,
4584f9bd12SAlex Elder 	IPA_STATUS_OPCODE_DROPPED_PACKET	= 0x04,
4684f9bd12SAlex Elder 	IPA_STATUS_OPCODE_SUSPENDED_PACKET	= 0x08,
4784f9bd12SAlex Elder 	IPA_STATUS_OPCODE_PACKET_2ND_PASS	= 0x40,
4884f9bd12SAlex Elder };
4984f9bd12SAlex Elder 
5084f9bd12SAlex Elder /** enum ipa_status_exception - status element exception type */
5184f9bd12SAlex Elder enum ipa_status_exception {
5284f9bd12SAlex Elder 	/* 0 means no exception */
5384f9bd12SAlex Elder 	IPA_STATUS_EXCEPTION_DEAGGR		= 0x01,
5484f9bd12SAlex Elder };
5584f9bd12SAlex Elder 
5684f9bd12SAlex Elder /* Status element provided by hardware */
5784f9bd12SAlex Elder struct ipa_status {
5884f9bd12SAlex Elder 	u8 opcode;		/* enum ipa_status_opcode */
5984f9bd12SAlex Elder 	u8 exception;		/* enum ipa_status_exception */
6084f9bd12SAlex Elder 	__le16 mask;
6184f9bd12SAlex Elder 	__le16 pkt_len;
6284f9bd12SAlex Elder 	u8 endp_src_idx;
6384f9bd12SAlex Elder 	u8 endp_dst_idx;
6484f9bd12SAlex Elder 	__le32 metadata;
6584f9bd12SAlex Elder 	__le32 flags1;
6684f9bd12SAlex Elder 	__le64 flags2;
6784f9bd12SAlex Elder 	__le32 flags3;
6884f9bd12SAlex Elder 	__le32 flags4;
6984f9bd12SAlex Elder };
7084f9bd12SAlex Elder 
7184f9bd12SAlex Elder /* Field masks for struct ipa_status structure fields */
72*f6aba7b5SAlex Elder #define IPA_STATUS_MASK_TAG_VALID_FMASK		GENMASK(4, 4)
73*f6aba7b5SAlex Elder #define IPA_STATUS_SRC_IDX_FMASK		GENMASK(4, 0)
7484f9bd12SAlex Elder #define IPA_STATUS_DST_IDX_FMASK		GENMASK(4, 0)
7584f9bd12SAlex Elder #define IPA_STATUS_FLAGS1_RT_RULE_ID_FMASK	GENMASK(31, 22)
76*f6aba7b5SAlex Elder #define IPA_STATUS_FLAGS2_TAG_FMASK		GENMASK_ULL(63, 16)
7784f9bd12SAlex Elder 
7884f9bd12SAlex Elder #ifdef IPA_VALIDATE
7984f9bd12SAlex Elder 
8084f9bd12SAlex Elder static bool ipa_endpoint_data_valid_one(struct ipa *ipa, u32 count,
8184f9bd12SAlex Elder 			    const struct ipa_gsi_endpoint_data *all_data,
8284f9bd12SAlex Elder 			    const struct ipa_gsi_endpoint_data *data)
8384f9bd12SAlex Elder {
8484f9bd12SAlex Elder 	const struct ipa_gsi_endpoint_data *other_data;
8584f9bd12SAlex Elder 	struct device *dev = &ipa->pdev->dev;
8684f9bd12SAlex Elder 	enum ipa_endpoint_name other_name;
8784f9bd12SAlex Elder 
8884f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(data))
8984f9bd12SAlex Elder 		return true;
9084f9bd12SAlex Elder 
9184f9bd12SAlex Elder 	if (!data->toward_ipa) {
9284f9bd12SAlex Elder 		if (data->endpoint.filter_support) {
9384f9bd12SAlex Elder 			dev_err(dev, "filtering not supported for "
9484f9bd12SAlex Elder 					"RX endpoint %u\n",
9584f9bd12SAlex Elder 				data->endpoint_id);
9684f9bd12SAlex Elder 			return false;
9784f9bd12SAlex Elder 		}
9884f9bd12SAlex Elder 
9984f9bd12SAlex Elder 		return true;	/* Nothing more to check for RX */
10084f9bd12SAlex Elder 	}
10184f9bd12SAlex Elder 
10284f9bd12SAlex Elder 	if (data->endpoint.config.status_enable) {
10384f9bd12SAlex Elder 		other_name = data->endpoint.config.tx.status_endpoint;
10484f9bd12SAlex Elder 		if (other_name >= count) {
10584f9bd12SAlex Elder 			dev_err(dev, "status endpoint name %u out of range "
10684f9bd12SAlex Elder 					"for endpoint %u\n",
10784f9bd12SAlex Elder 				other_name, data->endpoint_id);
10884f9bd12SAlex Elder 			return false;
10984f9bd12SAlex Elder 		}
11084f9bd12SAlex Elder 
11184f9bd12SAlex Elder 		/* Status endpoint must be defined... */
11284f9bd12SAlex Elder 		other_data = &all_data[other_name];
11384f9bd12SAlex Elder 		if (ipa_gsi_endpoint_data_empty(other_data)) {
11484f9bd12SAlex Elder 			dev_err(dev, "DMA endpoint name %u undefined "
11584f9bd12SAlex Elder 					"for endpoint %u\n",
11684f9bd12SAlex Elder 				other_name, data->endpoint_id);
11784f9bd12SAlex Elder 			return false;
11884f9bd12SAlex Elder 		}
11984f9bd12SAlex Elder 
12084f9bd12SAlex Elder 		/* ...and has to be an RX endpoint... */
12184f9bd12SAlex Elder 		if (other_data->toward_ipa) {
12284f9bd12SAlex Elder 			dev_err(dev,
12384f9bd12SAlex Elder 				"status endpoint for endpoint %u not RX\n",
12484f9bd12SAlex Elder 				data->endpoint_id);
12584f9bd12SAlex Elder 			return false;
12684f9bd12SAlex Elder 		}
12784f9bd12SAlex Elder 
12884f9bd12SAlex Elder 		/* ...and if it's to be an AP endpoint... */
12984f9bd12SAlex Elder 		if (other_data->ee_id == GSI_EE_AP) {
13084f9bd12SAlex Elder 			/* ...make sure it has status enabled. */
13184f9bd12SAlex Elder 			if (!other_data->endpoint.config.status_enable) {
13284f9bd12SAlex Elder 				dev_err(dev,
13384f9bd12SAlex Elder 					"status not enabled for endpoint %u\n",
13484f9bd12SAlex Elder 					other_data->endpoint_id);
13584f9bd12SAlex Elder 				return false;
13684f9bd12SAlex Elder 			}
13784f9bd12SAlex Elder 		}
13884f9bd12SAlex Elder 	}
13984f9bd12SAlex Elder 
14084f9bd12SAlex Elder 	if (data->endpoint.config.dma_mode) {
14184f9bd12SAlex Elder 		other_name = data->endpoint.config.dma_endpoint;
14284f9bd12SAlex Elder 		if (other_name >= count) {
14384f9bd12SAlex Elder 			dev_err(dev, "DMA endpoint name %u out of range "
14484f9bd12SAlex Elder 					"for endpoint %u\n",
14584f9bd12SAlex Elder 				other_name, data->endpoint_id);
14684f9bd12SAlex Elder 			return false;
14784f9bd12SAlex Elder 		}
14884f9bd12SAlex Elder 
14984f9bd12SAlex Elder 		other_data = &all_data[other_name];
15084f9bd12SAlex Elder 		if (ipa_gsi_endpoint_data_empty(other_data)) {
15184f9bd12SAlex Elder 			dev_err(dev, "DMA endpoint name %u undefined "
15284f9bd12SAlex Elder 					"for endpoint %u\n",
15384f9bd12SAlex Elder 				other_name, data->endpoint_id);
15484f9bd12SAlex Elder 			return false;
15584f9bd12SAlex Elder 		}
15684f9bd12SAlex Elder 	}
15784f9bd12SAlex Elder 
15884f9bd12SAlex Elder 	return true;
15984f9bd12SAlex Elder }
16084f9bd12SAlex Elder 
1616bf754c7SAlex Elder static u32 aggr_byte_limit_max(enum ipa_version version)
1626bf754c7SAlex Elder {
1636bf754c7SAlex Elder 	if (version < IPA_VERSION_4_5)
1646bf754c7SAlex Elder 		return field_max(aggr_byte_limit_fmask(true));
1656bf754c7SAlex Elder 
1666bf754c7SAlex Elder 	return field_max(aggr_byte_limit_fmask(false));
1676bf754c7SAlex Elder }
1686bf754c7SAlex Elder 
16984f9bd12SAlex Elder static bool ipa_endpoint_data_valid(struct ipa *ipa, u32 count,
17084f9bd12SAlex Elder 				    const struct ipa_gsi_endpoint_data *data)
17184f9bd12SAlex Elder {
17284f9bd12SAlex Elder 	const struct ipa_gsi_endpoint_data *dp = data;
17384f9bd12SAlex Elder 	struct device *dev = &ipa->pdev->dev;
17484f9bd12SAlex Elder 	enum ipa_endpoint_name name;
1756bf754c7SAlex Elder 	u32 limit;
17684f9bd12SAlex Elder 
1776bf754c7SAlex Elder 	/* Not sure where this constraint come from... */
1786bf754c7SAlex Elder 	BUILD_BUG_ON(sizeof(struct ipa_status) % 4);
17984f9bd12SAlex Elder 
18084f9bd12SAlex Elder 	if (count > IPA_ENDPOINT_COUNT) {
18184f9bd12SAlex Elder 		dev_err(dev, "too many endpoints specified (%u > %u)\n",
18284f9bd12SAlex Elder 			count, IPA_ENDPOINT_COUNT);
18384f9bd12SAlex Elder 		return false;
18484f9bd12SAlex Elder 	}
18584f9bd12SAlex Elder 
1866bf754c7SAlex Elder 	/* The aggregation byte limit defines the point at which an
1876bf754c7SAlex Elder 	 * aggregation window will close.  It is programmed into the
1886bf754c7SAlex Elder 	 * IPA hardware as a number of KB.  We don't use "hard byte
1896bf754c7SAlex Elder 	 * limit" aggregation, which means that we need to supply
1906bf754c7SAlex Elder 	 * enough space in a receive buffer to hold a complete MTU
1916bf754c7SAlex Elder 	 * plus normal skb overhead *after* that aggregation byte
1926bf754c7SAlex Elder 	 * limit has been crossed.
1936bf754c7SAlex Elder 	 *
1946bf754c7SAlex Elder 	 * This check ensures we don't define a receive buffer size
1956bf754c7SAlex Elder 	 * that would exceed what we can represent in the field that
1966bf754c7SAlex Elder 	 * is used to program its size.
1976bf754c7SAlex Elder 	 */
1986bf754c7SAlex Elder 	limit = aggr_byte_limit_max(ipa->version) * SZ_1K;
1996bf754c7SAlex Elder 	limit += IPA_MTU + IPA_RX_BUFFER_OVERHEAD;
2006bf754c7SAlex Elder 	if (limit < IPA_RX_BUFFER_SIZE) {
2016bf754c7SAlex Elder 		dev_err(dev, "buffer size too big for aggregation (%u > %u)\n",
2026bf754c7SAlex Elder 			IPA_RX_BUFFER_SIZE, limit);
2036bf754c7SAlex Elder 		return false;
2046bf754c7SAlex Elder 	}
2056bf754c7SAlex Elder 
20684f9bd12SAlex Elder 	/* Make sure needed endpoints have defined data */
20784f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_COMMAND_TX])) {
20884f9bd12SAlex Elder 		dev_err(dev, "command TX endpoint not defined\n");
20984f9bd12SAlex Elder 		return false;
21084f9bd12SAlex Elder 	}
21184f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_LAN_RX])) {
21284f9bd12SAlex Elder 		dev_err(dev, "LAN RX endpoint not defined\n");
21384f9bd12SAlex Elder 		return false;
21484f9bd12SAlex Elder 	}
21584f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_MODEM_TX])) {
21684f9bd12SAlex Elder 		dev_err(dev, "AP->modem TX endpoint not defined\n");
21784f9bd12SAlex Elder 		return false;
21884f9bd12SAlex Elder 	}
21984f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_MODEM_RX])) {
22084f9bd12SAlex Elder 		dev_err(dev, "AP<-modem RX endpoint not defined\n");
22184f9bd12SAlex Elder 		return false;
22284f9bd12SAlex Elder 	}
22384f9bd12SAlex Elder 
22484f9bd12SAlex Elder 	for (name = 0; name < count; name++, dp++)
22584f9bd12SAlex Elder 		if (!ipa_endpoint_data_valid_one(ipa, count, data, dp))
22684f9bd12SAlex Elder 			return false;
22784f9bd12SAlex Elder 
22884f9bd12SAlex Elder 	return true;
22984f9bd12SAlex Elder }
23084f9bd12SAlex Elder 
23184f9bd12SAlex Elder #else /* !IPA_VALIDATE */
23284f9bd12SAlex Elder 
23384f9bd12SAlex Elder static bool ipa_endpoint_data_valid(struct ipa *ipa, u32 count,
23484f9bd12SAlex Elder 				    const struct ipa_gsi_endpoint_data *data)
23584f9bd12SAlex Elder {
23684f9bd12SAlex Elder 	return true;
23784f9bd12SAlex Elder }
23884f9bd12SAlex Elder 
23984f9bd12SAlex Elder #endif /* !IPA_VALIDATE */
24084f9bd12SAlex Elder 
24184f9bd12SAlex Elder /* Allocate a transaction to use on a non-command endpoint */
24284f9bd12SAlex Elder static struct gsi_trans *ipa_endpoint_trans_alloc(struct ipa_endpoint *endpoint,
24384f9bd12SAlex Elder 						  u32 tre_count)
24484f9bd12SAlex Elder {
24584f9bd12SAlex Elder 	struct gsi *gsi = &endpoint->ipa->gsi;
24684f9bd12SAlex Elder 	u32 channel_id = endpoint->channel_id;
24784f9bd12SAlex Elder 	enum dma_data_direction direction;
24884f9bd12SAlex Elder 
24984f9bd12SAlex Elder 	direction = endpoint->toward_ipa ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
25084f9bd12SAlex Elder 
25184f9bd12SAlex Elder 	return gsi_channel_trans_alloc(gsi, channel_id, tre_count, direction);
25284f9bd12SAlex Elder }
25384f9bd12SAlex Elder 
25484f9bd12SAlex Elder /* suspend_delay represents suspend for RX, delay for TX endpoints.
25584f9bd12SAlex Elder  * Note that suspend is not supported starting with IPA v4.0.
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 
2664fa95248SAlex Elder 	/* Suspend is not supported for IPA v4.0+.  Delay doesn't work
2674fa95248SAlex Elder 	 * correctly on IPA v4.2.
2684fa95248SAlex Elder 	 *
2694fa95248SAlex Elder 	 * if (endpoint->toward_ipa)
2704fa95248SAlex Elder 	 * 	assert(ipa->version != IPA_VERSION_4.2);
2714fa95248SAlex Elder 	 * else
2724fa95248SAlex Elder 	 * 	assert(ipa->version == IPA_VERSION_3_5_1);
2734fa95248SAlex Elder 	 */
27484f9bd12SAlex Elder 	mask = endpoint->toward_ipa ? ENDP_DELAY_FMASK : ENDP_SUSPEND_FMASK;
27584f9bd12SAlex Elder 
27684f9bd12SAlex Elder 	val = ioread32(ipa->reg_virt + offset);
2774900bf34SAlex Elder 	/* Don't bother if it's already in the requested state */
2784900bf34SAlex Elder 	state = !!(val & mask);
2794900bf34SAlex Elder 	if (suspend_delay != state) {
28084f9bd12SAlex Elder 		val ^= mask;
28184f9bd12SAlex Elder 		iowrite32(val, ipa->reg_virt + offset);
2824900bf34SAlex Elder 	}
28384f9bd12SAlex Elder 
2844900bf34SAlex Elder 	return state;
28584f9bd12SAlex Elder }
28684f9bd12SAlex Elder 
2874fa95248SAlex Elder /* We currently don't care what the previous state was for delay mode */
2884fa95248SAlex Elder static void
2894fa95248SAlex Elder ipa_endpoint_program_delay(struct ipa_endpoint *endpoint, bool enable)
2904fa95248SAlex Elder {
2914fa95248SAlex Elder 	/* assert(endpoint->toward_ipa); */
2924fa95248SAlex Elder 
29366eba767SAlex Elder 	/* Delay mode doesn't work properly for IPA v4.2 */
29466eba767SAlex Elder 	if (endpoint->ipa->version != IPA_VERSION_4_2)
2954fa95248SAlex Elder 		(void)ipa_endpoint_init_ctrl(endpoint, enable);
2964fa95248SAlex Elder }
2974fa95248SAlex Elder 
298fff89971SAlex Elder static bool ipa_endpoint_aggr_active(struct ipa_endpoint *endpoint)
299fff89971SAlex Elder {
300fff89971SAlex Elder 	u32 mask = BIT(endpoint->endpoint_id);
301fff89971SAlex Elder 	struct ipa *ipa = endpoint->ipa;
302fff89971SAlex Elder 	u32 offset;
303fff89971SAlex Elder 	u32 val;
304fff89971SAlex Elder 
305fff89971SAlex Elder 	/* assert(mask & ipa->available); */
306fff89971SAlex Elder 	offset = ipa_reg_state_aggr_active_offset(ipa->version);
307fff89971SAlex Elder 	val = ioread32(ipa->reg_virt + offset);
308fff89971SAlex Elder 
309fff89971SAlex Elder 	return !!(val & mask);
310fff89971SAlex Elder }
311fff89971SAlex Elder 
312fff89971SAlex Elder static void ipa_endpoint_force_close(struct ipa_endpoint *endpoint)
313fff89971SAlex Elder {
314fff89971SAlex Elder 	u32 mask = BIT(endpoint->endpoint_id);
315fff89971SAlex Elder 	struct ipa *ipa = endpoint->ipa;
316fff89971SAlex Elder 
317fff89971SAlex Elder 	/* assert(mask & ipa->available); */
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 
353b07f283eSAlex Elder 	if (endpoint->ipa->version != IPA_VERSION_3_5_1)
354b07f283eSAlex Elder 		return enable;	/* For IPA v4.0+, no change made */
355b07f283eSAlex Elder 
3564fa95248SAlex Elder 	/* assert(!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 
37084f9bd12SAlex Elder /* Enable or disable delay or suspend mode on all modem endpoints */
37184f9bd12SAlex Elder void ipa_endpoint_modem_pause_all(struct ipa *ipa, bool enable)
37284f9bd12SAlex Elder {
37384f9bd12SAlex Elder 	u32 endpoint_id;
37484f9bd12SAlex Elder 
3754fa95248SAlex Elder 	/* DELAY mode doesn't work correctly on IPA v4.2 */
37684f9bd12SAlex Elder 	if (ipa->version == IPA_VERSION_4_2)
37784f9bd12SAlex Elder 		return;
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 
385b07f283eSAlex Elder 		/* Set TX delay mode or RX suspend mode */
3864fa95248SAlex Elder 		if (endpoint->toward_ipa)
3874fa95248SAlex Elder 			ipa_endpoint_program_delay(endpoint, enable);
388b07f283eSAlex Elder 		else
3894fa95248SAlex Elder 			(void)ipa_endpoint_program_suspend(endpoint, enable);
39084f9bd12SAlex Elder 	}
39184f9bd12SAlex Elder }
39284f9bd12SAlex Elder 
39384f9bd12SAlex Elder /* Reset all modem endpoints to use the default exception endpoint */
39484f9bd12SAlex Elder int ipa_endpoint_modem_exception_reset_all(struct ipa *ipa)
39584f9bd12SAlex Elder {
39684f9bd12SAlex Elder 	u32 initialized = ipa->initialized;
39784f9bd12SAlex Elder 	struct gsi_trans *trans;
39884f9bd12SAlex Elder 	u32 count;
39984f9bd12SAlex Elder 
40084f9bd12SAlex Elder 	/* We need one command per modem TX endpoint.  We can get an upper
40184f9bd12SAlex Elder 	 * bound on that by assuming all initialized endpoints are modem->IPA.
40284f9bd12SAlex Elder 	 * That won't happen, and we could be more precise, but this is fine
4038fa54b11SWang Wenhu 	 * for now.  We need to end the transaction with a "tag process."
40484f9bd12SAlex Elder 	 */
405aa56e3e5SAlex Elder 	count = hweight32(initialized) + ipa_cmd_pipeline_clear_count();
40684f9bd12SAlex Elder 	trans = ipa_cmd_trans_alloc(ipa, count);
40784f9bd12SAlex Elder 	if (!trans) {
40884f9bd12SAlex Elder 		dev_err(&ipa->pdev->dev,
40984f9bd12SAlex Elder 			"no transaction to reset modem exception endpoints\n");
41084f9bd12SAlex Elder 		return -EBUSY;
41184f9bd12SAlex Elder 	}
41284f9bd12SAlex Elder 
41384f9bd12SAlex Elder 	while (initialized) {
41484f9bd12SAlex Elder 		u32 endpoint_id = __ffs(initialized);
41584f9bd12SAlex Elder 		struct ipa_endpoint *endpoint;
41684f9bd12SAlex Elder 		u32 offset;
41784f9bd12SAlex Elder 
41884f9bd12SAlex Elder 		initialized ^= BIT(endpoint_id);
41984f9bd12SAlex Elder 
42084f9bd12SAlex Elder 		/* We only reset modem TX endpoints */
42184f9bd12SAlex Elder 		endpoint = &ipa->endpoint[endpoint_id];
42284f9bd12SAlex Elder 		if (!(endpoint->ee_id == GSI_EE_MODEM && endpoint->toward_ipa))
42384f9bd12SAlex Elder 			continue;
42484f9bd12SAlex Elder 
42584f9bd12SAlex Elder 		offset = IPA_REG_ENDP_STATUS_N_OFFSET(endpoint_id);
42684f9bd12SAlex Elder 
42784f9bd12SAlex Elder 		/* Value written is 0, and all bits are updated.  That
42884f9bd12SAlex Elder 		 * means status is disabled on the endpoint, and as a
42984f9bd12SAlex Elder 		 * result all other fields in the register are ignored.
43084f9bd12SAlex Elder 		 */
43184f9bd12SAlex Elder 		ipa_cmd_register_write_add(trans, offset, 0, ~0, false);
43284f9bd12SAlex Elder 	}
43384f9bd12SAlex Elder 
434aa56e3e5SAlex Elder 	ipa_cmd_pipeline_clear_add(trans);
43584f9bd12SAlex Elder 
43684f9bd12SAlex Elder 	/* XXX This should have a 1 second timeout */
43784f9bd12SAlex Elder 	gsi_trans_commit_wait(trans);
43884f9bd12SAlex Elder 
43984f9bd12SAlex Elder 	return 0;
44084f9bd12SAlex Elder }
44184f9bd12SAlex Elder 
44284f9bd12SAlex Elder static void ipa_endpoint_init_cfg(struct ipa_endpoint *endpoint)
44384f9bd12SAlex Elder {
44484f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_CFG_N_OFFSET(endpoint->endpoint_id);
44584f9bd12SAlex Elder 	u32 val = 0;
44684f9bd12SAlex Elder 
44784f9bd12SAlex Elder 	/* FRAG_OFFLOAD_EN is 0 */
44884f9bd12SAlex Elder 	if (endpoint->data->checksum) {
44984f9bd12SAlex Elder 		if (endpoint->toward_ipa) {
45084f9bd12SAlex Elder 			u32 checksum_offset;
45184f9bd12SAlex Elder 
45284f9bd12SAlex Elder 			val |= u32_encode_bits(IPA_CS_OFFLOAD_UL,
45384f9bd12SAlex Elder 					       CS_OFFLOAD_EN_FMASK);
45484f9bd12SAlex Elder 			/* Checksum header offset is in 4-byte units */
45584f9bd12SAlex Elder 			checksum_offset = sizeof(struct rmnet_map_header);
45684f9bd12SAlex Elder 			checksum_offset /= sizeof(u32);
45784f9bd12SAlex Elder 			val |= u32_encode_bits(checksum_offset,
45884f9bd12SAlex Elder 					       CS_METADATA_HDR_OFFSET_FMASK);
45984f9bd12SAlex Elder 		} else {
46084f9bd12SAlex Elder 			val |= u32_encode_bits(IPA_CS_OFFLOAD_DL,
46184f9bd12SAlex Elder 					       CS_OFFLOAD_EN_FMASK);
46284f9bd12SAlex Elder 		}
46384f9bd12SAlex Elder 	} else {
46484f9bd12SAlex Elder 		val |= u32_encode_bits(IPA_CS_OFFLOAD_NONE,
46584f9bd12SAlex Elder 				       CS_OFFLOAD_EN_FMASK);
46684f9bd12SAlex Elder 	}
46784f9bd12SAlex Elder 	/* CS_GEN_QMB_MASTER_SEL is 0 */
46884f9bd12SAlex Elder 
46984f9bd12SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
47084f9bd12SAlex Elder }
47184f9bd12SAlex Elder 
4728730f45dSAlex Elder /**
473e3eea08eSAlex Elder  * ipa_endpoint_init_hdr() - Initialize HDR endpoint configuration register
474e3eea08eSAlex Elder  * @endpoint:	Endpoint pointer
475e3eea08eSAlex Elder  *
4768730f45dSAlex Elder  * We program QMAP endpoints so each packet received is preceded by a QMAP
4778730f45dSAlex Elder  * header structure.  The QMAP header contains a 1-byte mux_id and 2-byte
4788730f45dSAlex Elder  * packet size field, and we have the IPA hardware populate both for each
4798730f45dSAlex Elder  * received packet.  The header is configured (in the HDR_EXT register)
4808730f45dSAlex Elder  * to use big endian format.
4818730f45dSAlex Elder  *
4828730f45dSAlex Elder  * The packet size is written into the QMAP header's pkt_len field.  That
4838730f45dSAlex Elder  * location is defined here using the HDR_OFST_PKT_SIZE field.
4848730f45dSAlex Elder  *
4858730f45dSAlex Elder  * The mux_id comes from a 4-byte metadata value supplied with each packet
4868730f45dSAlex Elder  * by the modem.  It is *not* a QMAP header, but it does contain the mux_id
4878730f45dSAlex Elder  * value that we want, in its low-order byte.  A bitmask defined in the
4888730f45dSAlex Elder  * endpoint's METADATA_MASK register defines which byte within the modem
4898730f45dSAlex Elder  * metadata contains the mux_id.  And the OFST_METADATA field programmed
4908730f45dSAlex Elder  * here indicates where the extracted byte should be placed within the QMAP
4918730f45dSAlex Elder  * header.
4928730f45dSAlex Elder  */
49384f9bd12SAlex Elder static void ipa_endpoint_init_hdr(struct ipa_endpoint *endpoint)
49484f9bd12SAlex Elder {
49584f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_HDR_N_OFFSET(endpoint->endpoint_id);
4961af15c2aSAlex Elder 	struct ipa *ipa = endpoint->ipa;
49784f9bd12SAlex Elder 	u32 val = 0;
49884f9bd12SAlex Elder 
49984f9bd12SAlex Elder 	if (endpoint->data->qmap) {
50084f9bd12SAlex Elder 		size_t header_size = sizeof(struct rmnet_map_header);
5011af15c2aSAlex Elder 		enum ipa_version version = ipa->version;
50284f9bd12SAlex Elder 
5038730f45dSAlex Elder 		/* We might supply a checksum header after the QMAP header */
50484f9bd12SAlex Elder 		if (endpoint->toward_ipa && endpoint->data->checksum)
50584f9bd12SAlex Elder 			header_size += sizeof(struct rmnet_map_ul_csum_header);
5061af15c2aSAlex Elder 		val |= ipa_header_size_encoded(version, header_size);
50784f9bd12SAlex Elder 
508f330fda3SAlex Elder 		/* Define how to fill fields in a received QMAP header */
5098730f45dSAlex Elder 		if (!endpoint->toward_ipa) {
5101af15c2aSAlex Elder 			u32 offset;	/* Field offset within header */
5118730f45dSAlex Elder 
5128730f45dSAlex Elder 			/* Where IPA will write the metadata value */
5131af15c2aSAlex Elder 			offset = offsetof(struct rmnet_map_header, mux_id);
5141af15c2aSAlex Elder 			val |= ipa_metadata_offset_encoded(version, offset);
5158730f45dSAlex Elder 
5168730f45dSAlex Elder 			/* Where IPA will write the length */
5171af15c2aSAlex Elder 			offset = offsetof(struct rmnet_map_header, pkt_len);
5181af15c2aSAlex Elder 			/* Upper bits are stored in HDR_EXT with IPA v4.5 */
5191af15c2aSAlex Elder 			if (version == IPA_VERSION_4_5)
5201af15c2aSAlex Elder 				offset &= field_mask(HDR_OFST_PKT_SIZE_FMASK);
5211af15c2aSAlex Elder 
52284f9bd12SAlex Elder 			val |= HDR_OFST_PKT_SIZE_VALID_FMASK;
5231af15c2aSAlex Elder 			val |= u32_encode_bits(offset, HDR_OFST_PKT_SIZE_FMASK);
52484f9bd12SAlex Elder 		}
5258730f45dSAlex Elder 		/* For QMAP TX, metadata offset is 0 (modem assumes this) */
5268730f45dSAlex Elder 		val |= HDR_OFST_METADATA_VALID_FMASK;
5278730f45dSAlex Elder 
5288730f45dSAlex Elder 		/* HDR_ADDITIONAL_CONST_LEN is 0; (RX only) */
52984f9bd12SAlex Elder 		/* HDR_A5_MUX is 0 */
53084f9bd12SAlex Elder 		/* HDR_LEN_INC_DEAGG_HDR is 0 */
5318bfc4e21SAlex Elder 		/* HDR_METADATA_REG_VALID is 0 (TX only, version < v4.5) */
53284f9bd12SAlex Elder 	}
53384f9bd12SAlex Elder 
5341af15c2aSAlex Elder 	iowrite32(val, ipa->reg_virt + offset);
53584f9bd12SAlex Elder }
53684f9bd12SAlex Elder 
53784f9bd12SAlex Elder static void ipa_endpoint_init_hdr_ext(struct ipa_endpoint *endpoint)
53884f9bd12SAlex Elder {
53984f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_HDR_EXT_N_OFFSET(endpoint->endpoint_id);
54084f9bd12SAlex Elder 	u32 pad_align = endpoint->data->rx.pad_align;
5411af15c2aSAlex Elder 	struct ipa *ipa = endpoint->ipa;
54284f9bd12SAlex Elder 	u32 val = 0;
54384f9bd12SAlex Elder 
54484f9bd12SAlex Elder 	val |= HDR_ENDIANNESS_FMASK;		/* big endian */
545f330fda3SAlex Elder 
546f330fda3SAlex Elder 	/* A QMAP header contains a 6 bit pad field at offset 0.  The RMNet
547f330fda3SAlex Elder 	 * driver assumes this field is meaningful in packets it receives,
548f330fda3SAlex Elder 	 * and assumes the header's payload length includes that padding.
549f330fda3SAlex Elder 	 * The RMNet driver does *not* pad packets it sends, however, so
550f330fda3SAlex Elder 	 * the pad field (although 0) should be ignored.
551f330fda3SAlex Elder 	 */
552f330fda3SAlex Elder 	if (endpoint->data->qmap && !endpoint->toward_ipa) {
55384f9bd12SAlex Elder 		val |= HDR_TOTAL_LEN_OR_PAD_VALID_FMASK;
55484f9bd12SAlex Elder 		/* HDR_TOTAL_LEN_OR_PAD is 0 (pad, not total_len) */
555f330fda3SAlex Elder 		val |= HDR_PAYLOAD_LEN_INC_PADDING_FMASK;
55684f9bd12SAlex Elder 		/* HDR_TOTAL_LEN_OR_PAD_OFFSET is 0 */
557f330fda3SAlex Elder 	}
558f330fda3SAlex Elder 
559f330fda3SAlex Elder 	/* HDR_PAYLOAD_LEN_INC_PADDING is 0 */
56084f9bd12SAlex Elder 	if (!endpoint->toward_ipa)
56184f9bd12SAlex Elder 		val |= u32_encode_bits(pad_align, HDR_PAD_TO_ALIGNMENT_FMASK);
56284f9bd12SAlex Elder 
5631af15c2aSAlex Elder 	/* IPA v4.5 adds some most-significant bits to a few fields,
5641af15c2aSAlex Elder 	 * two of which are defined in the HDR (not HDR_EXT) register.
5651af15c2aSAlex Elder 	 */
5661af15c2aSAlex Elder 	if (ipa->version == IPA_VERSION_4_5) {
5671af15c2aSAlex Elder 		/* HDR_TOTAL_LEN_OR_PAD_OFFSET is 0, so MSB is 0 */
5681af15c2aSAlex Elder 		if (endpoint->data->qmap && !endpoint->toward_ipa) {
5691af15c2aSAlex Elder 			u32 offset;
57084f9bd12SAlex Elder 
5711af15c2aSAlex Elder 			offset = offsetof(struct rmnet_map_header, pkt_len);
5721af15c2aSAlex Elder 			offset >>= hweight32(HDR_OFST_PKT_SIZE_FMASK);
5731af15c2aSAlex Elder 			val |= u32_encode_bits(offset,
5741af15c2aSAlex Elder 					       HDR_OFST_PKT_SIZE_MSB_FMASK);
5751af15c2aSAlex Elder 			/* HDR_ADDITIONAL_CONST_LEN is 0 so MSB is 0 */
5761af15c2aSAlex Elder 		}
5771af15c2aSAlex Elder 	}
5781af15c2aSAlex Elder 	iowrite32(val, ipa->reg_virt + offset);
5791af15c2aSAlex Elder }
58084f9bd12SAlex Elder 
58184f9bd12SAlex Elder static void ipa_endpoint_init_hdr_metadata_mask(struct ipa_endpoint *endpoint)
58284f9bd12SAlex Elder {
58384f9bd12SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
58484f9bd12SAlex Elder 	u32 val = 0;
58584f9bd12SAlex Elder 	u32 offset;
58684f9bd12SAlex Elder 
587fb57c3eaSAlex Elder 	if (endpoint->toward_ipa)
588fb57c3eaSAlex Elder 		return;		/* Register not valid for TX endpoints */
589fb57c3eaSAlex Elder 
59084f9bd12SAlex Elder 	offset = IPA_REG_ENDP_INIT_HDR_METADATA_MASK_N_OFFSET(endpoint_id);
59184f9bd12SAlex Elder 
5928730f45dSAlex Elder 	/* Note that HDR_ENDIANNESS indicates big endian header fields */
5939b63f093SAlex Elder 	if (endpoint->data->qmap)
5948730f45dSAlex Elder 		val = cpu_to_be32(IPA_ENDPOINT_QMAP_METADATA_MASK);
59584f9bd12SAlex Elder 
59684f9bd12SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
59784f9bd12SAlex Elder }
59884f9bd12SAlex Elder 
59984f9bd12SAlex Elder static void ipa_endpoint_init_mode(struct ipa_endpoint *endpoint)
60084f9bd12SAlex Elder {
60184f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_MODE_N_OFFSET(endpoint->endpoint_id);
60284f9bd12SAlex Elder 	u32 val;
60384f9bd12SAlex Elder 
604fb57c3eaSAlex Elder 	if (!endpoint->toward_ipa)
605fb57c3eaSAlex Elder 		return;		/* Register not valid for RX endpoints */
606fb57c3eaSAlex Elder 
60700b9102aSAlex Elder 	if (endpoint->data->dma_mode) {
60884f9bd12SAlex Elder 		enum ipa_endpoint_name name = endpoint->data->dma_endpoint;
60984f9bd12SAlex Elder 		u32 dma_endpoint_id;
61084f9bd12SAlex Elder 
61184f9bd12SAlex Elder 		dma_endpoint_id = endpoint->ipa->name_map[name]->endpoint_id;
61284f9bd12SAlex Elder 
61384f9bd12SAlex Elder 		val = u32_encode_bits(IPA_DMA, MODE_FMASK);
61484f9bd12SAlex Elder 		val |= u32_encode_bits(dma_endpoint_id, DEST_PIPE_INDEX_FMASK);
61584f9bd12SAlex Elder 	} else {
61684f9bd12SAlex Elder 		val = u32_encode_bits(IPA_BASIC, MODE_FMASK);
61784f9bd12SAlex Elder 	}
61800b9102aSAlex Elder 	/* All other bits unspecified (and 0) */
61984f9bd12SAlex Elder 
62084f9bd12SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
62184f9bd12SAlex Elder }
62284f9bd12SAlex Elder 
62384f9bd12SAlex Elder /* Compute the aggregation size value to use for a given buffer size */
62484f9bd12SAlex Elder static u32 ipa_aggr_size_kb(u32 rx_buffer_size)
62584f9bd12SAlex Elder {
62684f9bd12SAlex Elder 	/* We don't use "hard byte limit" aggregation, so we define the
62784f9bd12SAlex Elder 	 * aggregation limit such that our buffer has enough space *after*
62884f9bd12SAlex Elder 	 * that limit to receive a full MTU of data, plus overhead.
62984f9bd12SAlex Elder 	 */
63084f9bd12SAlex Elder 	rx_buffer_size -= IPA_MTU + IPA_RX_BUFFER_OVERHEAD;
63184f9bd12SAlex Elder 
63284f9bd12SAlex Elder 	return rx_buffer_size / SZ_1K;
63384f9bd12SAlex Elder }
63484f9bd12SAlex Elder 
6356bf754c7SAlex Elder /* Encoded values for AGGR endpoint register fields */
6366bf754c7SAlex Elder static u32 aggr_byte_limit_encoded(enum ipa_version version, u32 limit)
6376bf754c7SAlex Elder {
6386bf754c7SAlex Elder 	if (version < IPA_VERSION_4_5)
6396bf754c7SAlex Elder 		return u32_encode_bits(limit, aggr_byte_limit_fmask(true));
6406bf754c7SAlex Elder 
6416bf754c7SAlex Elder 	return u32_encode_bits(limit, aggr_byte_limit_fmask(false));
6426bf754c7SAlex Elder }
6436bf754c7SAlex Elder 
64419547041SAlex Elder /* Encode the aggregation timer limit (microseconds) based on IPA version */
6456bf754c7SAlex Elder static u32 aggr_time_limit_encoded(enum ipa_version version, u32 limit)
6466bf754c7SAlex Elder {
64719547041SAlex Elder 	u32 gran_sel;
64819547041SAlex Elder 	u32 fmask;
64919547041SAlex Elder 	u32 val;
6506bf754c7SAlex Elder 
65119547041SAlex Elder 	if (version < IPA_VERSION_4_5) {
65219547041SAlex Elder 		/* We set aggregation granularity in ipa_hardware_config() */
65319547041SAlex Elder 		limit = DIV_ROUND_CLOSEST(limit, IPA_AGGR_GRANULARITY);
65419547041SAlex Elder 
65519547041SAlex Elder 		return u32_encode_bits(limit, aggr_time_limit_fmask(true));
65619547041SAlex Elder 	}
65719547041SAlex Elder 
65819547041SAlex Elder 	/* IPA v4.5 expresses the time limit using Qtime.  The AP has
65919547041SAlex Elder 	 * pulse generators 0 and 1 available, which were configured
66019547041SAlex Elder 	 * in ipa_qtime_config() to have granularity 100 usec and
66119547041SAlex Elder 	 * 1 msec, respectively.  Use pulse generator 0 if possible,
66219547041SAlex Elder 	 * otherwise fall back to pulse generator 1.
66319547041SAlex Elder 	 */
66419547041SAlex Elder 	fmask = aggr_time_limit_fmask(false);
66519547041SAlex Elder 	val = DIV_ROUND_CLOSEST(limit, 100);
66619547041SAlex Elder 	if (val > field_max(fmask)) {
66719547041SAlex Elder 		/* Have to use pulse generator 1 (millisecond granularity) */
66819547041SAlex Elder 		gran_sel = AGGR_GRAN_SEL_FMASK;
66919547041SAlex Elder 		val = DIV_ROUND_CLOSEST(limit, 1000);
67019547041SAlex Elder 	} else {
67119547041SAlex Elder 		/* We can use pulse generator 0 (100 usec granularity) */
67219547041SAlex Elder 		gran_sel = 0;
67319547041SAlex Elder 	}
67419547041SAlex Elder 
67519547041SAlex Elder 	return gran_sel | u32_encode_bits(val, fmask);
6766bf754c7SAlex Elder }
6776bf754c7SAlex Elder 
6786bf754c7SAlex Elder static u32 aggr_sw_eof_active_encoded(enum ipa_version version, bool enabled)
6796bf754c7SAlex Elder {
6806bf754c7SAlex Elder 	u32 val = enabled ? 1 : 0;
6816bf754c7SAlex Elder 
6826bf754c7SAlex Elder 	if (version < IPA_VERSION_4_5)
6836bf754c7SAlex Elder 		return u32_encode_bits(val, aggr_sw_eof_active_fmask(true));
6846bf754c7SAlex Elder 
6856bf754c7SAlex Elder 	return u32_encode_bits(val, aggr_sw_eof_active_fmask(false));
6866bf754c7SAlex Elder }
6876bf754c7SAlex Elder 
68884f9bd12SAlex Elder static void ipa_endpoint_init_aggr(struct ipa_endpoint *endpoint)
68984f9bd12SAlex Elder {
69084f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_AGGR_N_OFFSET(endpoint->endpoint_id);
6916bf754c7SAlex Elder 	enum ipa_version version = endpoint->ipa->version;
69284f9bd12SAlex Elder 	u32 val = 0;
69384f9bd12SAlex Elder 
69484f9bd12SAlex Elder 	if (endpoint->data->aggregation) {
69584f9bd12SAlex Elder 		if (!endpoint->toward_ipa) {
6966bf754c7SAlex Elder 			bool close_eof;
69784f9bd12SAlex Elder 			u32 limit;
69884f9bd12SAlex Elder 
69984f9bd12SAlex Elder 			val |= u32_encode_bits(IPA_ENABLE_AGGR, AGGR_EN_FMASK);
70084f9bd12SAlex Elder 			val |= u32_encode_bits(IPA_GENERIC, AGGR_TYPE_FMASK);
7019e88cb5fSAlex Elder 
7029e88cb5fSAlex Elder 			limit = ipa_aggr_size_kb(IPA_RX_BUFFER_SIZE);
7036bf754c7SAlex Elder 			val |= aggr_byte_limit_encoded(version, limit);
7041d86652bSAlex Elder 
7056bf754c7SAlex Elder 			limit = IPA_AGGR_TIME_LIMIT;
7066bf754c7SAlex Elder 			val |= aggr_time_limit_encoded(version, limit);
7071d86652bSAlex Elder 
7089e88cb5fSAlex Elder 			/* AGGR_PKT_LIMIT is 0 (unlimited) */
7099e88cb5fSAlex Elder 
7106bf754c7SAlex Elder 			close_eof = endpoint->data->rx.aggr_close_eof;
7116bf754c7SAlex Elder 			val |= aggr_sw_eof_active_encoded(version, close_eof);
7126bf754c7SAlex Elder 
71384f9bd12SAlex Elder 			/* AGGR_HARD_BYTE_LIMIT_ENABLE is 0 */
71484f9bd12SAlex Elder 		} else {
71584f9bd12SAlex Elder 			val |= u32_encode_bits(IPA_ENABLE_DEAGGR,
71684f9bd12SAlex Elder 					       AGGR_EN_FMASK);
71784f9bd12SAlex Elder 			val |= u32_encode_bits(IPA_QCMAP, AGGR_TYPE_FMASK);
71884f9bd12SAlex Elder 			/* other fields ignored */
71984f9bd12SAlex Elder 		}
72084f9bd12SAlex Elder 		/* AGGR_FORCE_CLOSE is 0 */
7218bfc4e21SAlex Elder 		/* AGGR_GRAN_SEL is 0 for IPA v4.5 */
72284f9bd12SAlex Elder 	} else {
72384f9bd12SAlex Elder 		val |= u32_encode_bits(IPA_BYPASS_AGGR, AGGR_EN_FMASK);
72484f9bd12SAlex Elder 		/* other fields ignored */
72584f9bd12SAlex Elder 	}
72684f9bd12SAlex Elder 
72784f9bd12SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
72884f9bd12SAlex Elder }
72984f9bd12SAlex Elder 
73063e5afc8SAlex Elder /* Return the Qtime-based head-of-line blocking timer value that
73163e5afc8SAlex Elder  * represents the given number of microseconds.  The result
73263e5afc8SAlex Elder  * includes both the timer value and the selected timer granularity.
733f13a8c31SAlex Elder  */
73463e5afc8SAlex Elder static u32 hol_block_timer_qtime_val(struct ipa *ipa, u32 microseconds)
73563e5afc8SAlex Elder {
73663e5afc8SAlex Elder 	u32 gran_sel;
73763e5afc8SAlex Elder 	u32 val;
73863e5afc8SAlex Elder 
73963e5afc8SAlex Elder 	/* IPA v4.5 expresses time limits using Qtime.  The AP has
74063e5afc8SAlex Elder 	 * pulse generators 0 and 1 available, which were configured
74163e5afc8SAlex Elder 	 * in ipa_qtime_config() to have granularity 100 usec and
74263e5afc8SAlex Elder 	 * 1 msec, respectively.  Use pulse generator 0 if possible,
74363e5afc8SAlex Elder 	 * otherwise fall back to pulse generator 1.
74463e5afc8SAlex Elder 	 */
74563e5afc8SAlex Elder 	val = DIV_ROUND_CLOSEST(microseconds, 100);
74663e5afc8SAlex Elder 	if (val > field_max(TIME_LIMIT_FMASK)) {
74763e5afc8SAlex Elder 		/* Have to use pulse generator 1 (millisecond granularity) */
74863e5afc8SAlex Elder 		gran_sel = GRAN_SEL_FMASK;
74963e5afc8SAlex Elder 		val = DIV_ROUND_CLOSEST(microseconds, 1000);
75063e5afc8SAlex Elder 	} else {
75163e5afc8SAlex Elder 		/* We can use pulse generator 0 (100 usec granularity) */
75263e5afc8SAlex Elder 		gran_sel = 0;
75363e5afc8SAlex Elder 	}
75463e5afc8SAlex Elder 
75563e5afc8SAlex Elder 	return gran_sel | u32_encode_bits(val, TIME_LIMIT_FMASK);
75663e5afc8SAlex Elder }
75763e5afc8SAlex Elder 
75863e5afc8SAlex Elder /* The head-of-line blocking timer is defined as a tick count.  For
75963e5afc8SAlex Elder  * IPA version 4.5 the tick count is based on the Qtimer, which is
76063e5afc8SAlex Elder  * derived from the 19.2 MHz SoC XO clock.  For older IPA versions
76163e5afc8SAlex Elder  * each tick represents 128 cycles of the IPA core clock.
76263e5afc8SAlex Elder  *
76363e5afc8SAlex Elder  * Return the encoded value that should be written to that register
76463e5afc8SAlex Elder  * that represents the timeout period provided.  For IPA v4.2 this
76563e5afc8SAlex Elder  * encodes a base and scale value, while for earlier versions the
76663e5afc8SAlex Elder  * value is a simple tick count.
76763e5afc8SAlex Elder  */
76863e5afc8SAlex Elder static u32 hol_block_timer_val(struct ipa *ipa, u32 microseconds)
76984f9bd12SAlex Elder {
770f13a8c31SAlex Elder 	u32 width;
77184f9bd12SAlex Elder 	u32 scale;
772f13a8c31SAlex Elder 	u64 ticks;
773f13a8c31SAlex Elder 	u64 rate;
774f13a8c31SAlex Elder 	u32 high;
77584f9bd12SAlex Elder 	u32 val;
77684f9bd12SAlex Elder 
77784f9bd12SAlex Elder 	if (!microseconds)
778f13a8c31SAlex Elder 		return 0;	/* Nothing to compute if timer period is 0 */
77984f9bd12SAlex Elder 
78063e5afc8SAlex Elder 	if (ipa->version == IPA_VERSION_4_5)
78163e5afc8SAlex Elder 		return hol_block_timer_qtime_val(ipa, microseconds);
78263e5afc8SAlex Elder 
783f13a8c31SAlex Elder 	/* Use 64 bit arithmetic to avoid overflow... */
784f13a8c31SAlex Elder 	rate = ipa_clock_rate(ipa);
785f13a8c31SAlex Elder 	ticks = DIV_ROUND_CLOSEST(microseconds * rate, 128 * USEC_PER_SEC);
786f13a8c31SAlex Elder 	/* ...but we still need to fit into a 32-bit register */
787f13a8c31SAlex Elder 	WARN_ON(ticks > U32_MAX);
78884f9bd12SAlex Elder 
7896833a096SAlex Elder 	/* IPA v3.5.1 through v4.1 just record the tick count */
7906833a096SAlex Elder 	if (ipa->version < IPA_VERSION_4_2)
791f13a8c31SAlex Elder 		return (u32)ticks;
79284f9bd12SAlex Elder 
793f13a8c31SAlex Elder 	/* For IPA v4.2, the tick count is represented by base and
794f13a8c31SAlex Elder 	 * scale fields within the 32-bit timer register, where:
795f13a8c31SAlex Elder 	 *     ticks = base << scale;
796f13a8c31SAlex Elder 	 * The best precision is achieved when the base value is as
797f13a8c31SAlex Elder 	 * large as possible.  Find the highest set bit in the tick
798f13a8c31SAlex Elder 	 * count, and extract the number of bits in the base field
799f13a8c31SAlex Elder 	 * such that that high bit is included.
800f13a8c31SAlex Elder 	 */
801f13a8c31SAlex Elder 	high = fls(ticks);		/* 1..32 */
802f13a8c31SAlex Elder 	width = HWEIGHT32(BASE_VALUE_FMASK);
803f13a8c31SAlex Elder 	scale = high > width ? high - width : 0;
804f13a8c31SAlex Elder 	if (scale) {
805f13a8c31SAlex Elder 		/* If we're scaling, round up to get a closer result */
806f13a8c31SAlex Elder 		ticks += 1 << (scale - 1);
807f13a8c31SAlex Elder 		/* High bit was set, so rounding might have affected it */
808f13a8c31SAlex Elder 		if (fls(ticks) != high)
809f13a8c31SAlex Elder 			scale++;
810f13a8c31SAlex Elder 	}
81184f9bd12SAlex Elder 
81284f9bd12SAlex Elder 	val = u32_encode_bits(scale, SCALE_FMASK);
813f13a8c31SAlex Elder 	val |= u32_encode_bits(ticks >> scale, BASE_VALUE_FMASK);
81484f9bd12SAlex Elder 
81584f9bd12SAlex Elder 	return val;
81684f9bd12SAlex Elder }
81784f9bd12SAlex Elder 
818f13a8c31SAlex Elder /* If microseconds is 0, timeout is immediate */
819f13a8c31SAlex Elder static void ipa_endpoint_init_hol_block_timer(struct ipa_endpoint *endpoint,
82084f9bd12SAlex Elder 					      u32 microseconds)
82184f9bd12SAlex Elder {
82284f9bd12SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
82384f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
82484f9bd12SAlex Elder 	u32 offset;
82584f9bd12SAlex Elder 	u32 val;
82684f9bd12SAlex Elder 
82784f9bd12SAlex Elder 	offset = IPA_REG_ENDP_INIT_HOL_BLOCK_TIMER_N_OFFSET(endpoint_id);
82863e5afc8SAlex Elder 	val = hol_block_timer_val(ipa, microseconds);
82984f9bd12SAlex Elder 	iowrite32(val, ipa->reg_virt + offset);
83084f9bd12SAlex Elder }
83184f9bd12SAlex Elder 
83284f9bd12SAlex Elder static void
83384f9bd12SAlex Elder ipa_endpoint_init_hol_block_enable(struct ipa_endpoint *endpoint, bool enable)
83484f9bd12SAlex Elder {
83584f9bd12SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
83684f9bd12SAlex Elder 	u32 offset;
83784f9bd12SAlex Elder 	u32 val;
83884f9bd12SAlex Elder 
839547c8788SAlex Elder 	val = enable ? HOL_BLOCK_EN_FMASK : 0;
84084f9bd12SAlex Elder 	offset = IPA_REG_ENDP_INIT_HOL_BLOCK_EN_N_OFFSET(endpoint_id);
84184f9bd12SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
84284f9bd12SAlex Elder }
84384f9bd12SAlex Elder 
84484f9bd12SAlex Elder void ipa_endpoint_modem_hol_block_clear_all(struct ipa *ipa)
84584f9bd12SAlex Elder {
84684f9bd12SAlex Elder 	u32 i;
84784f9bd12SAlex Elder 
84884f9bd12SAlex Elder 	for (i = 0; i < IPA_ENDPOINT_MAX; i++) {
84984f9bd12SAlex Elder 		struct ipa_endpoint *endpoint = &ipa->endpoint[i];
85084f9bd12SAlex Elder 
851f8d34dfdSAlex Elder 		if (endpoint->toward_ipa || endpoint->ee_id != GSI_EE_MODEM)
85284f9bd12SAlex Elder 			continue;
85384f9bd12SAlex Elder 
854f13a8c31SAlex Elder 		ipa_endpoint_init_hol_block_timer(endpoint, 0);
85584f9bd12SAlex Elder 		ipa_endpoint_init_hol_block_enable(endpoint, true);
85684f9bd12SAlex Elder 	}
85784f9bd12SAlex Elder }
85884f9bd12SAlex Elder 
85984f9bd12SAlex Elder static void ipa_endpoint_init_deaggr(struct ipa_endpoint *endpoint)
86084f9bd12SAlex Elder {
86184f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_DEAGGR_N_OFFSET(endpoint->endpoint_id);
86284f9bd12SAlex Elder 	u32 val = 0;
86384f9bd12SAlex Elder 
864fb57c3eaSAlex Elder 	if (!endpoint->toward_ipa)
865fb57c3eaSAlex Elder 		return;		/* Register not valid for RX endpoints */
866fb57c3eaSAlex Elder 
86784f9bd12SAlex Elder 	/* DEAGGR_HDR_LEN is 0 */
86884f9bd12SAlex Elder 	/* PACKET_OFFSET_VALID is 0 */
86984f9bd12SAlex Elder 	/* PACKET_OFFSET_LOCATION is ignored (not valid) */
87084f9bd12SAlex Elder 	/* MAX_PACKET_LEN is 0 (not enforced) */
87184f9bd12SAlex Elder 
87284f9bd12SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
87384f9bd12SAlex Elder }
87484f9bd12SAlex Elder 
8752d265342SAlex Elder static void ipa_endpoint_init_rsrc_grp(struct ipa_endpoint *endpoint)
8762d265342SAlex Elder {
8772d265342SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_RSRC_GRP_N_OFFSET(endpoint->endpoint_id);
8782d265342SAlex Elder 	struct ipa *ipa = endpoint->ipa;
8792d265342SAlex Elder 	u32 val;
8802d265342SAlex Elder 
8812d265342SAlex Elder 	val = rsrc_grp_encoded(ipa->version, endpoint->data->resource_group);
8822d265342SAlex Elder 	iowrite32(val, ipa->reg_virt + offset);
8832d265342SAlex Elder }
8842d265342SAlex Elder 
88584f9bd12SAlex Elder static void ipa_endpoint_init_seq(struct ipa_endpoint *endpoint)
88684f9bd12SAlex Elder {
88784f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_SEQ_N_OFFSET(endpoint->endpoint_id);
88884f9bd12SAlex Elder 	u32 seq_type = endpoint->seq_type;
88984f9bd12SAlex Elder 	u32 val = 0;
89084f9bd12SAlex Elder 
891fb57c3eaSAlex Elder 	if (!endpoint->toward_ipa)
892fb57c3eaSAlex Elder 		return;		/* Register not valid for RX endpoints */
893fb57c3eaSAlex Elder 
894636edeaaSAlex Elder 	/* Sequencer type is made up of four nibbles */
89584f9bd12SAlex Elder 	val |= u32_encode_bits(seq_type & 0xf, HPS_SEQ_TYPE_FMASK);
89684f9bd12SAlex Elder 	val |= u32_encode_bits((seq_type >> 4) & 0xf, DPS_SEQ_TYPE_FMASK);
897636edeaaSAlex Elder 	/* The second two apply to replicated packets */
898636edeaaSAlex Elder 	val |= u32_encode_bits((seq_type >> 8) & 0xf, HPS_REP_SEQ_TYPE_FMASK);
899636edeaaSAlex Elder 	val |= u32_encode_bits((seq_type >> 12) & 0xf, DPS_REP_SEQ_TYPE_FMASK);
90084f9bd12SAlex Elder 
90184f9bd12SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
90284f9bd12SAlex Elder }
90384f9bd12SAlex Elder 
90484f9bd12SAlex Elder /**
90584f9bd12SAlex Elder  * ipa_endpoint_skb_tx() - Transmit a socket buffer
90684f9bd12SAlex Elder  * @endpoint:	Endpoint pointer
90784f9bd12SAlex Elder  * @skb:	Socket buffer to send
90884f9bd12SAlex Elder  *
90984f9bd12SAlex Elder  * Returns:	0 if successful, or a negative error code
91084f9bd12SAlex Elder  */
91184f9bd12SAlex Elder int ipa_endpoint_skb_tx(struct ipa_endpoint *endpoint, struct sk_buff *skb)
91284f9bd12SAlex Elder {
91384f9bd12SAlex Elder 	struct gsi_trans *trans;
91484f9bd12SAlex Elder 	u32 nr_frags;
91584f9bd12SAlex Elder 	int ret;
91684f9bd12SAlex Elder 
91784f9bd12SAlex Elder 	/* Make sure source endpoint's TLV FIFO has enough entries to
91884f9bd12SAlex Elder 	 * hold the linear portion of the skb and all its fragments.
91984f9bd12SAlex Elder 	 * If not, see if we can linearize it before giving up.
92084f9bd12SAlex Elder 	 */
92184f9bd12SAlex Elder 	nr_frags = skb_shinfo(skb)->nr_frags;
92284f9bd12SAlex Elder 	if (1 + nr_frags > endpoint->trans_tre_max) {
92384f9bd12SAlex Elder 		if (skb_linearize(skb))
92484f9bd12SAlex Elder 			return -E2BIG;
92584f9bd12SAlex Elder 		nr_frags = 0;
92684f9bd12SAlex Elder 	}
92784f9bd12SAlex Elder 
92884f9bd12SAlex Elder 	trans = ipa_endpoint_trans_alloc(endpoint, 1 + nr_frags);
92984f9bd12SAlex Elder 	if (!trans)
93084f9bd12SAlex Elder 		return -EBUSY;
93184f9bd12SAlex Elder 
93284f9bd12SAlex Elder 	ret = gsi_trans_skb_add(trans, skb);
93384f9bd12SAlex Elder 	if (ret)
93484f9bd12SAlex Elder 		goto err_trans_free;
93584f9bd12SAlex Elder 	trans->data = skb;	/* transaction owns skb now */
93684f9bd12SAlex Elder 
93784f9bd12SAlex Elder 	gsi_trans_commit(trans, !netdev_xmit_more());
93884f9bd12SAlex Elder 
93984f9bd12SAlex Elder 	return 0;
94084f9bd12SAlex Elder 
94184f9bd12SAlex Elder err_trans_free:
94284f9bd12SAlex Elder 	gsi_trans_free(trans);
94384f9bd12SAlex Elder 
94484f9bd12SAlex Elder 	return -ENOMEM;
94584f9bd12SAlex Elder }
94684f9bd12SAlex Elder 
94784f9bd12SAlex Elder static void ipa_endpoint_status(struct ipa_endpoint *endpoint)
94884f9bd12SAlex Elder {
94984f9bd12SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
95084f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
95184f9bd12SAlex Elder 	u32 val = 0;
95284f9bd12SAlex Elder 	u32 offset;
95384f9bd12SAlex Elder 
95484f9bd12SAlex Elder 	offset = IPA_REG_ENDP_STATUS_N_OFFSET(endpoint_id);
95584f9bd12SAlex Elder 
95684f9bd12SAlex Elder 	if (endpoint->data->status_enable) {
95784f9bd12SAlex Elder 		val |= STATUS_EN_FMASK;
95884f9bd12SAlex Elder 		if (endpoint->toward_ipa) {
95984f9bd12SAlex Elder 			enum ipa_endpoint_name name;
96084f9bd12SAlex Elder 			u32 status_endpoint_id;
96184f9bd12SAlex Elder 
96284f9bd12SAlex Elder 			name = endpoint->data->tx.status_endpoint;
96384f9bd12SAlex Elder 			status_endpoint_id = ipa->name_map[name]->endpoint_id;
96484f9bd12SAlex Elder 
96584f9bd12SAlex Elder 			val |= u32_encode_bits(status_endpoint_id,
96684f9bd12SAlex Elder 					       STATUS_ENDP_FMASK);
96784f9bd12SAlex Elder 		}
9688bfc4e21SAlex Elder 		/* STATUS_LOCATION is 0, meaning status element precedes
9698bfc4e21SAlex Elder 		 * packet (not present for IPA v4.5)
9708bfc4e21SAlex Elder 		 */
9718bfc4e21SAlex Elder 		/* STATUS_PKT_SUPPRESS_FMASK is 0 (not present for v3.5.1) */
97284f9bd12SAlex Elder 	}
97384f9bd12SAlex Elder 
97484f9bd12SAlex Elder 	iowrite32(val, ipa->reg_virt + offset);
97584f9bd12SAlex Elder }
97684f9bd12SAlex Elder 
97784f9bd12SAlex Elder static int ipa_endpoint_replenish_one(struct ipa_endpoint *endpoint)
97884f9bd12SAlex Elder {
97984f9bd12SAlex Elder 	struct gsi_trans *trans;
98084f9bd12SAlex Elder 	bool doorbell = false;
98184f9bd12SAlex Elder 	struct page *page;
98284f9bd12SAlex Elder 	u32 offset;
98384f9bd12SAlex Elder 	u32 len;
98484f9bd12SAlex Elder 	int ret;
98584f9bd12SAlex Elder 
9866fcd4224SAlex Elder 	page = dev_alloc_pages(get_order(IPA_RX_BUFFER_SIZE));
98784f9bd12SAlex Elder 	if (!page)
98884f9bd12SAlex Elder 		return -ENOMEM;
98984f9bd12SAlex Elder 
99084f9bd12SAlex Elder 	trans = ipa_endpoint_trans_alloc(endpoint, 1);
99184f9bd12SAlex Elder 	if (!trans)
99284f9bd12SAlex Elder 		goto err_free_pages;
99384f9bd12SAlex Elder 
99484f9bd12SAlex Elder 	/* Offset the buffer to make space for skb headroom */
99584f9bd12SAlex Elder 	offset = NET_SKB_PAD;
99684f9bd12SAlex Elder 	len = IPA_RX_BUFFER_SIZE - offset;
99784f9bd12SAlex Elder 
99884f9bd12SAlex Elder 	ret = gsi_trans_page_add(trans, page, len, offset);
99984f9bd12SAlex Elder 	if (ret)
100084f9bd12SAlex Elder 		goto err_trans_free;
100184f9bd12SAlex Elder 	trans->data = page;	/* transaction owns page now */
100284f9bd12SAlex Elder 
100384f9bd12SAlex Elder 	if (++endpoint->replenish_ready == IPA_REPLENISH_BATCH) {
100484f9bd12SAlex Elder 		doorbell = true;
100584f9bd12SAlex Elder 		endpoint->replenish_ready = 0;
100684f9bd12SAlex Elder 	}
100784f9bd12SAlex Elder 
100884f9bd12SAlex Elder 	gsi_trans_commit(trans, doorbell);
100984f9bd12SAlex Elder 
101084f9bd12SAlex Elder 	return 0;
101184f9bd12SAlex Elder 
101284f9bd12SAlex Elder err_trans_free:
101384f9bd12SAlex Elder 	gsi_trans_free(trans);
101484f9bd12SAlex Elder err_free_pages:
10156fcd4224SAlex Elder 	__free_pages(page, get_order(IPA_RX_BUFFER_SIZE));
101684f9bd12SAlex Elder 
101784f9bd12SAlex Elder 	return -ENOMEM;
101884f9bd12SAlex Elder }
101984f9bd12SAlex Elder 
102084f9bd12SAlex Elder /**
102184f9bd12SAlex Elder  * ipa_endpoint_replenish() - Replenish the Rx packets cache.
1022e3eea08eSAlex Elder  * @endpoint:	Endpoint to be replenished
1023e3eea08eSAlex Elder  * @count:	Number of buffers to send to hardware
102484f9bd12SAlex Elder  *
102584f9bd12SAlex Elder  * Allocate RX packet wrapper structures with maximal socket buffers
102684f9bd12SAlex Elder  * for an endpoint.  These are supplied to the hardware, which fills
102784f9bd12SAlex Elder  * them with incoming data.
102884f9bd12SAlex Elder  */
102984f9bd12SAlex Elder static void ipa_endpoint_replenish(struct ipa_endpoint *endpoint, u32 count)
103084f9bd12SAlex Elder {
103184f9bd12SAlex Elder 	struct gsi *gsi;
103284f9bd12SAlex Elder 	u32 backlog;
103384f9bd12SAlex Elder 
103484f9bd12SAlex Elder 	if (!endpoint->replenish_enabled) {
103584f9bd12SAlex Elder 		if (count)
103684f9bd12SAlex Elder 			atomic_add(count, &endpoint->replenish_saved);
103784f9bd12SAlex Elder 		return;
103884f9bd12SAlex Elder 	}
103984f9bd12SAlex Elder 
104084f9bd12SAlex Elder 
104184f9bd12SAlex Elder 	while (atomic_dec_not_zero(&endpoint->replenish_backlog))
104284f9bd12SAlex Elder 		if (ipa_endpoint_replenish_one(endpoint))
104384f9bd12SAlex Elder 			goto try_again_later;
104484f9bd12SAlex Elder 	if (count)
104584f9bd12SAlex Elder 		atomic_add(count, &endpoint->replenish_backlog);
104684f9bd12SAlex Elder 
104784f9bd12SAlex Elder 	return;
104884f9bd12SAlex Elder 
104984f9bd12SAlex Elder try_again_later:
105084f9bd12SAlex Elder 	/* The last one didn't succeed, so fix the backlog */
105184f9bd12SAlex Elder 	backlog = atomic_inc_return(&endpoint->replenish_backlog);
105284f9bd12SAlex Elder 
105384f9bd12SAlex Elder 	if (count)
105484f9bd12SAlex Elder 		atomic_add(count, &endpoint->replenish_backlog);
105584f9bd12SAlex Elder 
105684f9bd12SAlex Elder 	/* Whenever a receive buffer transaction completes we'll try to
105784f9bd12SAlex Elder 	 * replenish again.  It's unlikely, but if we fail to supply even
105884f9bd12SAlex Elder 	 * one buffer, nothing will trigger another replenish attempt.
105984f9bd12SAlex Elder 	 * Receive buffer transactions use one TRE, so schedule work to
106084f9bd12SAlex Elder 	 * try replenishing again if our backlog is *all* available TREs.
106184f9bd12SAlex Elder 	 */
106284f9bd12SAlex Elder 	gsi = &endpoint->ipa->gsi;
106384f9bd12SAlex Elder 	if (backlog == gsi_channel_tre_max(gsi, endpoint->channel_id))
106484f9bd12SAlex Elder 		schedule_delayed_work(&endpoint->replenish_work,
106584f9bd12SAlex Elder 				      msecs_to_jiffies(1));
106684f9bd12SAlex Elder }
106784f9bd12SAlex Elder 
106884f9bd12SAlex Elder static void ipa_endpoint_replenish_enable(struct ipa_endpoint *endpoint)
106984f9bd12SAlex Elder {
107084f9bd12SAlex Elder 	struct gsi *gsi = &endpoint->ipa->gsi;
107184f9bd12SAlex Elder 	u32 max_backlog;
107284f9bd12SAlex Elder 	u32 saved;
107384f9bd12SAlex Elder 
107484f9bd12SAlex Elder 	endpoint->replenish_enabled = true;
107584f9bd12SAlex Elder 	while ((saved = atomic_xchg(&endpoint->replenish_saved, 0)))
107684f9bd12SAlex Elder 		atomic_add(saved, &endpoint->replenish_backlog);
107784f9bd12SAlex Elder 
107884f9bd12SAlex Elder 	/* Start replenishing if hardware currently has no buffers */
107984f9bd12SAlex Elder 	max_backlog = gsi_channel_tre_max(gsi, endpoint->channel_id);
108084f9bd12SAlex Elder 	if (atomic_read(&endpoint->replenish_backlog) == max_backlog)
108184f9bd12SAlex Elder 		ipa_endpoint_replenish(endpoint, 0);
108284f9bd12SAlex Elder }
108384f9bd12SAlex Elder 
108484f9bd12SAlex Elder static void ipa_endpoint_replenish_disable(struct ipa_endpoint *endpoint)
108584f9bd12SAlex Elder {
108684f9bd12SAlex Elder 	u32 backlog;
108784f9bd12SAlex Elder 
108884f9bd12SAlex Elder 	endpoint->replenish_enabled = false;
108984f9bd12SAlex Elder 	while ((backlog = atomic_xchg(&endpoint->replenish_backlog, 0)))
109084f9bd12SAlex Elder 		atomic_add(backlog, &endpoint->replenish_saved);
109184f9bd12SAlex Elder }
109284f9bd12SAlex Elder 
109384f9bd12SAlex Elder static void ipa_endpoint_replenish_work(struct work_struct *work)
109484f9bd12SAlex Elder {
109584f9bd12SAlex Elder 	struct delayed_work *dwork = to_delayed_work(work);
109684f9bd12SAlex Elder 	struct ipa_endpoint *endpoint;
109784f9bd12SAlex Elder 
109884f9bd12SAlex Elder 	endpoint = container_of(dwork, struct ipa_endpoint, replenish_work);
109984f9bd12SAlex Elder 
110084f9bd12SAlex Elder 	ipa_endpoint_replenish(endpoint, 0);
110184f9bd12SAlex Elder }
110284f9bd12SAlex Elder 
110384f9bd12SAlex Elder static void ipa_endpoint_skb_copy(struct ipa_endpoint *endpoint,
110484f9bd12SAlex Elder 				  void *data, u32 len, u32 extra)
110584f9bd12SAlex Elder {
110684f9bd12SAlex Elder 	struct sk_buff *skb;
110784f9bd12SAlex Elder 
110884f9bd12SAlex Elder 	skb = __dev_alloc_skb(len, GFP_ATOMIC);
110984f9bd12SAlex Elder 	if (skb) {
111084f9bd12SAlex Elder 		skb_put(skb, len);
111184f9bd12SAlex Elder 		memcpy(skb->data, data, len);
111284f9bd12SAlex Elder 		skb->truesize += extra;
111384f9bd12SAlex Elder 	}
111484f9bd12SAlex Elder 
111584f9bd12SAlex Elder 	/* Now receive it, or drop it if there's no netdev */
111684f9bd12SAlex Elder 	if (endpoint->netdev)
111784f9bd12SAlex Elder 		ipa_modem_skb_rx(endpoint->netdev, skb);
111884f9bd12SAlex Elder 	else if (skb)
111984f9bd12SAlex Elder 		dev_kfree_skb_any(skb);
112084f9bd12SAlex Elder }
112184f9bd12SAlex Elder 
112284f9bd12SAlex Elder static bool ipa_endpoint_skb_build(struct ipa_endpoint *endpoint,
112384f9bd12SAlex Elder 				   struct page *page, u32 len)
112484f9bd12SAlex Elder {
112584f9bd12SAlex Elder 	struct sk_buff *skb;
112684f9bd12SAlex Elder 
112784f9bd12SAlex Elder 	/* Nothing to do if there's no netdev */
112884f9bd12SAlex Elder 	if (!endpoint->netdev)
112984f9bd12SAlex Elder 		return false;
113084f9bd12SAlex Elder 
113184f9bd12SAlex Elder 	/* assert(len <= SKB_WITH_OVERHEAD(IPA_RX_BUFFER_SIZE-NET_SKB_PAD)); */
113284f9bd12SAlex Elder 	skb = build_skb(page_address(page), IPA_RX_BUFFER_SIZE);
113384f9bd12SAlex Elder 	if (skb) {
113484f9bd12SAlex Elder 		/* Reserve the headroom and account for the data */
113584f9bd12SAlex Elder 		skb_reserve(skb, NET_SKB_PAD);
113684f9bd12SAlex Elder 		skb_put(skb, len);
113784f9bd12SAlex Elder 	}
113884f9bd12SAlex Elder 
113984f9bd12SAlex Elder 	/* Receive the buffer (or record drop if unable to build it) */
114084f9bd12SAlex Elder 	ipa_modem_skb_rx(endpoint->netdev, skb);
114184f9bd12SAlex Elder 
114284f9bd12SAlex Elder 	return skb != NULL;
114384f9bd12SAlex Elder }
114484f9bd12SAlex Elder 
114584f9bd12SAlex Elder /* The format of a packet status element is the same for several status
114645921390SAlex Elder  * types (opcodes).  Other types aren't currently supported.
114784f9bd12SAlex Elder  */
114884f9bd12SAlex Elder static bool ipa_status_format_packet(enum ipa_status_opcode opcode)
114984f9bd12SAlex Elder {
115084f9bd12SAlex Elder 	switch (opcode) {
115184f9bd12SAlex Elder 	case IPA_STATUS_OPCODE_PACKET:
115284f9bd12SAlex Elder 	case IPA_STATUS_OPCODE_DROPPED_PACKET:
115384f9bd12SAlex Elder 	case IPA_STATUS_OPCODE_SUSPENDED_PACKET:
115484f9bd12SAlex Elder 	case IPA_STATUS_OPCODE_PACKET_2ND_PASS:
115584f9bd12SAlex Elder 		return true;
115684f9bd12SAlex Elder 	default:
115784f9bd12SAlex Elder 		return false;
115884f9bd12SAlex Elder 	}
115984f9bd12SAlex Elder }
116084f9bd12SAlex Elder 
116184f9bd12SAlex Elder static bool ipa_endpoint_status_skip(struct ipa_endpoint *endpoint,
116284f9bd12SAlex Elder 				     const struct ipa_status *status)
116384f9bd12SAlex Elder {
116484f9bd12SAlex Elder 	u32 endpoint_id;
116584f9bd12SAlex Elder 
116684f9bd12SAlex Elder 	if (!ipa_status_format_packet(status->opcode))
116784f9bd12SAlex Elder 		return true;
116884f9bd12SAlex Elder 	if (!status->pkt_len)
116984f9bd12SAlex Elder 		return true;
117084f9bd12SAlex Elder 	endpoint_id = u32_get_bits(status->endp_dst_idx,
117184f9bd12SAlex Elder 				   IPA_STATUS_DST_IDX_FMASK);
117284f9bd12SAlex Elder 	if (endpoint_id != endpoint->endpoint_id)
117384f9bd12SAlex Elder 		return true;
117484f9bd12SAlex Elder 
117584f9bd12SAlex Elder 	return false;	/* Don't skip this packet, process it */
117684f9bd12SAlex Elder }
117784f9bd12SAlex Elder 
1178*f6aba7b5SAlex Elder static bool ipa_endpoint_status_tag(struct ipa_endpoint *endpoint,
1179*f6aba7b5SAlex Elder 				    const struct ipa_status *status)
1180*f6aba7b5SAlex Elder {
1181*f6aba7b5SAlex Elder 	return !!le16_get_bits(status->mask, IPA_STATUS_MASK_TAG_VALID_FMASK);
1182*f6aba7b5SAlex Elder }
1183*f6aba7b5SAlex Elder 
118484f9bd12SAlex Elder /* Return whether the status indicates the packet should be dropped */
1185*f6aba7b5SAlex Elder static bool ipa_endpoint_status_drop(struct ipa_endpoint *endpoint,
1186*f6aba7b5SAlex Elder 				     const struct ipa_status *status)
118784f9bd12SAlex Elder {
118884f9bd12SAlex Elder 	u32 val;
118984f9bd12SAlex Elder 
1190*f6aba7b5SAlex Elder 	/* If the status indicates a tagged transfer, we'll drop the packet */
1191*f6aba7b5SAlex Elder 	if (ipa_endpoint_status_tag(endpoint, status))
1192*f6aba7b5SAlex Elder 		return true;
1193*f6aba7b5SAlex Elder 
1194ab4f71e5SAlex Elder 	/* Deaggregation exceptions we drop; all other types we consume */
119584f9bd12SAlex Elder 	if (status->exception)
119684f9bd12SAlex Elder 		return status->exception == IPA_STATUS_EXCEPTION_DEAGGR;
119784f9bd12SAlex Elder 
119884f9bd12SAlex Elder 	/* Drop the packet if it fails to match a routing rule; otherwise no */
119984f9bd12SAlex Elder 	val = le32_get_bits(status->flags1, IPA_STATUS_FLAGS1_RT_RULE_ID_FMASK);
120084f9bd12SAlex Elder 
120184f9bd12SAlex Elder 	return val == field_max(IPA_STATUS_FLAGS1_RT_RULE_ID_FMASK);
120284f9bd12SAlex Elder }
120384f9bd12SAlex Elder 
120484f9bd12SAlex Elder static void ipa_endpoint_status_parse(struct ipa_endpoint *endpoint,
120584f9bd12SAlex Elder 				      struct page *page, u32 total_len)
120684f9bd12SAlex Elder {
120784f9bd12SAlex Elder 	void *data = page_address(page) + NET_SKB_PAD;
120884f9bd12SAlex Elder 	u32 unused = IPA_RX_BUFFER_SIZE - total_len;
120984f9bd12SAlex Elder 	u32 resid = total_len;
121084f9bd12SAlex Elder 
121184f9bd12SAlex Elder 	while (resid) {
121284f9bd12SAlex Elder 		const struct ipa_status *status = data;
121384f9bd12SAlex Elder 		u32 align;
121484f9bd12SAlex Elder 		u32 len;
121584f9bd12SAlex Elder 
121684f9bd12SAlex Elder 		if (resid < sizeof(*status)) {
121784f9bd12SAlex Elder 			dev_err(&endpoint->ipa->pdev->dev,
121884f9bd12SAlex Elder 				"short message (%u bytes < %zu byte status)\n",
121984f9bd12SAlex Elder 				resid, sizeof(*status));
122084f9bd12SAlex Elder 			break;
122184f9bd12SAlex Elder 		}
122284f9bd12SAlex Elder 
122384f9bd12SAlex Elder 		/* Skip over status packets that lack packet data */
122484f9bd12SAlex Elder 		if (ipa_endpoint_status_skip(endpoint, status)) {
122584f9bd12SAlex Elder 			data += sizeof(*status);
122684f9bd12SAlex Elder 			resid -= sizeof(*status);
122784f9bd12SAlex Elder 			continue;
122884f9bd12SAlex Elder 		}
122984f9bd12SAlex Elder 
1230162fbc6fSAlex Elder 		/* Compute the amount of buffer space consumed by the packet,
1231162fbc6fSAlex Elder 		 * including the status element.  If the hardware is configured
1232162fbc6fSAlex Elder 		 * to pad packet data to an aligned boundary, account for that.
1233162fbc6fSAlex Elder 		 * And if checksum offload is enabled a trailer containing
1234162fbc6fSAlex Elder 		 * computed checksum information will be appended.
123584f9bd12SAlex Elder 		 */
123684f9bd12SAlex Elder 		align = endpoint->data->rx.pad_align ? : 1;
123784f9bd12SAlex Elder 		len = le16_to_cpu(status->pkt_len);
123884f9bd12SAlex Elder 		len = sizeof(*status) + ALIGN(len, align);
123984f9bd12SAlex Elder 		if (endpoint->data->checksum)
124084f9bd12SAlex Elder 			len += sizeof(struct rmnet_map_dl_csum_trailer);
124184f9bd12SAlex Elder 
1242*f6aba7b5SAlex Elder 		if (!ipa_endpoint_status_drop(endpoint, status)) {
1243162fbc6fSAlex Elder 			void *data2;
1244162fbc6fSAlex Elder 			u32 extra;
1245162fbc6fSAlex Elder 			u32 len2;
124684f9bd12SAlex Elder 
124784f9bd12SAlex Elder 			/* Client receives only packet data (no status) */
1248162fbc6fSAlex Elder 			data2 = data + sizeof(*status);
1249162fbc6fSAlex Elder 			len2 = le16_to_cpu(status->pkt_len);
1250162fbc6fSAlex Elder 
1251162fbc6fSAlex Elder 			/* Have the true size reflect the extra unused space in
1252162fbc6fSAlex Elder 			 * the original receive buffer.  Distribute the "cost"
1253162fbc6fSAlex Elder 			 * proportionately across all aggregated packets in the
1254162fbc6fSAlex Elder 			 * buffer.
1255162fbc6fSAlex Elder 			 */
1256162fbc6fSAlex Elder 			extra = DIV_ROUND_CLOSEST(unused * len, total_len);
125784f9bd12SAlex Elder 			ipa_endpoint_skb_copy(endpoint, data2, len2, extra);
125884f9bd12SAlex Elder 		}
125984f9bd12SAlex Elder 
126084f9bd12SAlex Elder 		/* Consume status and the full packet it describes */
126184f9bd12SAlex Elder 		data += len;
126284f9bd12SAlex Elder 		resid -= len;
126384f9bd12SAlex Elder 	}
126484f9bd12SAlex Elder }
126584f9bd12SAlex Elder 
126684f9bd12SAlex Elder /* Complete a TX transaction, command or from ipa_endpoint_skb_tx() */
126784f9bd12SAlex Elder static void ipa_endpoint_tx_complete(struct ipa_endpoint *endpoint,
126884f9bd12SAlex Elder 				     struct gsi_trans *trans)
126984f9bd12SAlex Elder {
127084f9bd12SAlex Elder }
127184f9bd12SAlex Elder 
127284f9bd12SAlex Elder /* Complete transaction initiated in ipa_endpoint_replenish_one() */
127384f9bd12SAlex Elder static void ipa_endpoint_rx_complete(struct ipa_endpoint *endpoint,
127484f9bd12SAlex Elder 				     struct gsi_trans *trans)
127584f9bd12SAlex Elder {
127684f9bd12SAlex Elder 	struct page *page;
127784f9bd12SAlex Elder 
127884f9bd12SAlex Elder 	ipa_endpoint_replenish(endpoint, 1);
127984f9bd12SAlex Elder 
128084f9bd12SAlex Elder 	if (trans->cancelled)
128184f9bd12SAlex Elder 		return;
128284f9bd12SAlex Elder 
128384f9bd12SAlex Elder 	/* Parse or build a socket buffer using the actual received length */
128484f9bd12SAlex Elder 	page = trans->data;
128584f9bd12SAlex Elder 	if (endpoint->data->status_enable)
128684f9bd12SAlex Elder 		ipa_endpoint_status_parse(endpoint, page, trans->len);
128784f9bd12SAlex Elder 	else if (ipa_endpoint_skb_build(endpoint, page, trans->len))
128884f9bd12SAlex Elder 		trans->data = NULL;	/* Pages have been consumed */
128984f9bd12SAlex Elder }
129084f9bd12SAlex Elder 
129184f9bd12SAlex Elder void ipa_endpoint_trans_complete(struct ipa_endpoint *endpoint,
129284f9bd12SAlex Elder 				 struct gsi_trans *trans)
129384f9bd12SAlex Elder {
129484f9bd12SAlex Elder 	if (endpoint->toward_ipa)
129584f9bd12SAlex Elder 		ipa_endpoint_tx_complete(endpoint, trans);
129684f9bd12SAlex Elder 	else
129784f9bd12SAlex Elder 		ipa_endpoint_rx_complete(endpoint, trans);
129884f9bd12SAlex Elder }
129984f9bd12SAlex Elder 
130084f9bd12SAlex Elder void ipa_endpoint_trans_release(struct ipa_endpoint *endpoint,
130184f9bd12SAlex Elder 				struct gsi_trans *trans)
130284f9bd12SAlex Elder {
130384f9bd12SAlex Elder 	if (endpoint->toward_ipa) {
130484f9bd12SAlex Elder 		struct ipa *ipa = endpoint->ipa;
130584f9bd12SAlex Elder 
130684f9bd12SAlex Elder 		/* Nothing to do for command transactions */
130784f9bd12SAlex Elder 		if (endpoint != ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]) {
130884f9bd12SAlex Elder 			struct sk_buff *skb = trans->data;
130984f9bd12SAlex Elder 
131084f9bd12SAlex Elder 			if (skb)
131184f9bd12SAlex Elder 				dev_kfree_skb_any(skb);
131284f9bd12SAlex Elder 		}
131384f9bd12SAlex Elder 	} else {
131484f9bd12SAlex Elder 		struct page *page = trans->data;
131584f9bd12SAlex Elder 
131684f9bd12SAlex Elder 		if (page)
13176fcd4224SAlex Elder 			__free_pages(page, get_order(IPA_RX_BUFFER_SIZE));
131884f9bd12SAlex Elder 	}
131984f9bd12SAlex Elder }
132084f9bd12SAlex Elder 
132184f9bd12SAlex Elder void ipa_endpoint_default_route_set(struct ipa *ipa, u32 endpoint_id)
132284f9bd12SAlex Elder {
132384f9bd12SAlex Elder 	u32 val;
132484f9bd12SAlex Elder 
132584f9bd12SAlex Elder 	/* ROUTE_DIS is 0 */
132684f9bd12SAlex Elder 	val = u32_encode_bits(endpoint_id, ROUTE_DEF_PIPE_FMASK);
132784f9bd12SAlex Elder 	val |= ROUTE_DEF_HDR_TABLE_FMASK;
132884f9bd12SAlex Elder 	val |= u32_encode_bits(0, ROUTE_DEF_HDR_OFST_FMASK);
132984f9bd12SAlex Elder 	val |= u32_encode_bits(endpoint_id, ROUTE_FRAG_DEF_PIPE_FMASK);
133084f9bd12SAlex Elder 	val |= ROUTE_DEF_RETAIN_HDR_FMASK;
133184f9bd12SAlex Elder 
133284f9bd12SAlex Elder 	iowrite32(val, ipa->reg_virt + IPA_REG_ROUTE_OFFSET);
133384f9bd12SAlex Elder }
133484f9bd12SAlex Elder 
133584f9bd12SAlex Elder void ipa_endpoint_default_route_clear(struct ipa *ipa)
133684f9bd12SAlex Elder {
133784f9bd12SAlex Elder 	ipa_endpoint_default_route_set(ipa, 0);
133884f9bd12SAlex Elder }
133984f9bd12SAlex Elder 
134084f9bd12SAlex Elder /**
134184f9bd12SAlex Elder  * ipa_endpoint_reset_rx_aggr() - Reset RX endpoint with aggregation active
134284f9bd12SAlex Elder  * @endpoint:	Endpoint to be reset
134384f9bd12SAlex Elder  *
134484f9bd12SAlex Elder  * If aggregation is active on an RX endpoint when a reset is performed
134584f9bd12SAlex Elder  * on its underlying GSI channel, a special sequence of actions must be
134684f9bd12SAlex Elder  * taken to ensure the IPA pipeline is properly cleared.
134784f9bd12SAlex Elder  *
1348e3eea08eSAlex Elder  * Return:	0 if successful, or a negative error code
134984f9bd12SAlex Elder  */
135084f9bd12SAlex Elder static int ipa_endpoint_reset_rx_aggr(struct ipa_endpoint *endpoint)
135184f9bd12SAlex Elder {
135284f9bd12SAlex Elder 	struct device *dev = &endpoint->ipa->pdev->dev;
135384f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
135484f9bd12SAlex Elder 	struct gsi *gsi = &ipa->gsi;
13554fa95248SAlex Elder 	bool suspended = false;
135684f9bd12SAlex Elder 	dma_addr_t addr;
135784f9bd12SAlex Elder 	u32 retries;
135884f9bd12SAlex Elder 	u32 len = 1;
135984f9bd12SAlex Elder 	void *virt;
136084f9bd12SAlex Elder 	int ret;
136184f9bd12SAlex Elder 
136284f9bd12SAlex Elder 	virt = kzalloc(len, GFP_KERNEL);
136384f9bd12SAlex Elder 	if (!virt)
136484f9bd12SAlex Elder 		return -ENOMEM;
136584f9bd12SAlex Elder 
136684f9bd12SAlex Elder 	addr = dma_map_single(dev, virt, len, DMA_FROM_DEVICE);
136784f9bd12SAlex Elder 	if (dma_mapping_error(dev, addr)) {
136884f9bd12SAlex Elder 		ret = -ENOMEM;
136984f9bd12SAlex Elder 		goto out_kfree;
137084f9bd12SAlex Elder 	}
137184f9bd12SAlex Elder 
137284f9bd12SAlex Elder 	/* Force close aggregation before issuing the reset */
137384f9bd12SAlex Elder 	ipa_endpoint_force_close(endpoint);
137484f9bd12SAlex Elder 
137584f9bd12SAlex Elder 	/* Reset and reconfigure the channel with the doorbell engine
137684f9bd12SAlex Elder 	 * disabled.  Then poll until we know aggregation is no longer
137784f9bd12SAlex Elder 	 * active.  We'll re-enable the doorbell (if appropriate) when
137884f9bd12SAlex Elder 	 * we reset again below.
137984f9bd12SAlex Elder 	 */
138084f9bd12SAlex Elder 	gsi_channel_reset(gsi, endpoint->channel_id, false);
138184f9bd12SAlex Elder 
138284f9bd12SAlex Elder 	/* Make sure the channel isn't suspended */
13834fa95248SAlex Elder 	suspended = ipa_endpoint_program_suspend(endpoint, false);
138484f9bd12SAlex Elder 
138584f9bd12SAlex Elder 	/* Start channel and do a 1 byte read */
138684f9bd12SAlex Elder 	ret = gsi_channel_start(gsi, endpoint->channel_id);
138784f9bd12SAlex Elder 	if (ret)
138884f9bd12SAlex Elder 		goto out_suspend_again;
138984f9bd12SAlex Elder 
139084f9bd12SAlex Elder 	ret = gsi_trans_read_byte(gsi, endpoint->channel_id, addr);
139184f9bd12SAlex Elder 	if (ret)
139284f9bd12SAlex Elder 		goto err_endpoint_stop;
139384f9bd12SAlex Elder 
139484f9bd12SAlex Elder 	/* Wait for aggregation to be closed on the channel */
139584f9bd12SAlex Elder 	retries = IPA_ENDPOINT_RESET_AGGR_RETRY_MAX;
139684f9bd12SAlex Elder 	do {
139784f9bd12SAlex Elder 		if (!ipa_endpoint_aggr_active(endpoint))
139884f9bd12SAlex Elder 			break;
139974401946SAlex Elder 		usleep_range(USEC_PER_MSEC, 2 * USEC_PER_MSEC);
140084f9bd12SAlex Elder 	} while (retries--);
140184f9bd12SAlex Elder 
140284f9bd12SAlex Elder 	/* Check one last time */
140384f9bd12SAlex Elder 	if (ipa_endpoint_aggr_active(endpoint))
140484f9bd12SAlex Elder 		dev_err(dev, "endpoint %u still active during reset\n",
140584f9bd12SAlex Elder 			endpoint->endpoint_id);
140684f9bd12SAlex Elder 
140784f9bd12SAlex Elder 	gsi_trans_read_byte_done(gsi, endpoint->channel_id);
140884f9bd12SAlex Elder 
1409f30dcb7dSAlex Elder 	ret = gsi_channel_stop(gsi, endpoint->channel_id);
141084f9bd12SAlex Elder 	if (ret)
141184f9bd12SAlex Elder 		goto out_suspend_again;
141284f9bd12SAlex Elder 
141384f9bd12SAlex Elder 	/* Finally, reset and reconfigure the channel again (re-enabling the
141484f9bd12SAlex Elder 	 * the doorbell engine if appropriate).  Sleep for 1 millisecond to
141584f9bd12SAlex Elder 	 * complete the channel reset sequence.  Finish by suspending the
141684f9bd12SAlex Elder 	 * channel again (if necessary).
141784f9bd12SAlex Elder 	 */
1418ce54993dSAlex Elder 	gsi_channel_reset(gsi, endpoint->channel_id, true);
141984f9bd12SAlex Elder 
142074401946SAlex Elder 	usleep_range(USEC_PER_MSEC, 2 * USEC_PER_MSEC);
142184f9bd12SAlex Elder 
142284f9bd12SAlex Elder 	goto out_suspend_again;
142384f9bd12SAlex Elder 
142484f9bd12SAlex Elder err_endpoint_stop:
1425f30dcb7dSAlex Elder 	(void)gsi_channel_stop(gsi, endpoint->channel_id);
142684f9bd12SAlex Elder out_suspend_again:
14274fa95248SAlex Elder 	if (suspended)
14284fa95248SAlex Elder 		(void)ipa_endpoint_program_suspend(endpoint, true);
142984f9bd12SAlex Elder 	dma_unmap_single(dev, addr, len, DMA_FROM_DEVICE);
143084f9bd12SAlex Elder out_kfree:
143184f9bd12SAlex Elder 	kfree(virt);
143284f9bd12SAlex Elder 
143384f9bd12SAlex Elder 	return ret;
143484f9bd12SAlex Elder }
143584f9bd12SAlex Elder 
143684f9bd12SAlex Elder static void ipa_endpoint_reset(struct ipa_endpoint *endpoint)
143784f9bd12SAlex Elder {
143884f9bd12SAlex Elder 	u32 channel_id = endpoint->channel_id;
143984f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
144084f9bd12SAlex Elder 	bool special;
144184f9bd12SAlex Elder 	int ret = 0;
144284f9bd12SAlex Elder 
144384f9bd12SAlex Elder 	/* On IPA v3.5.1, if an RX endpoint is reset while aggregation
144484f9bd12SAlex Elder 	 * is active, we need to handle things specially to recover.
144584f9bd12SAlex Elder 	 * All other cases just need to reset the underlying GSI channel.
144684f9bd12SAlex Elder 	 */
1447ce54993dSAlex Elder 	special = ipa->version == IPA_VERSION_3_5_1 &&
1448ce54993dSAlex Elder 			!endpoint->toward_ipa &&
1449ce54993dSAlex Elder 			endpoint->data->aggregation;
1450ce54993dSAlex Elder 	if (special && ipa_endpoint_aggr_active(endpoint))
145184f9bd12SAlex Elder 		ret = ipa_endpoint_reset_rx_aggr(endpoint);
145284f9bd12SAlex Elder 	else
1453ce54993dSAlex Elder 		gsi_channel_reset(&ipa->gsi, channel_id, true);
145484f9bd12SAlex Elder 
145584f9bd12SAlex Elder 	if (ret)
145684f9bd12SAlex Elder 		dev_err(&ipa->pdev->dev,
145784f9bd12SAlex Elder 			"error %d resetting channel %u for endpoint %u\n",
145884f9bd12SAlex Elder 			ret, endpoint->channel_id, endpoint->endpoint_id);
145984f9bd12SAlex Elder }
146084f9bd12SAlex Elder 
146184f9bd12SAlex Elder static void ipa_endpoint_program(struct ipa_endpoint *endpoint)
146284f9bd12SAlex Elder {
1463fb57c3eaSAlex Elder 	if (endpoint->toward_ipa)
1464a4dcad34SAlex Elder 		ipa_endpoint_program_delay(endpoint, false);
1465fb57c3eaSAlex Elder 	else
1466fb57c3eaSAlex Elder 		(void)ipa_endpoint_program_suspend(endpoint, false);
1467fb57c3eaSAlex Elder 	ipa_endpoint_init_cfg(endpoint);
1468fb57c3eaSAlex Elder 	ipa_endpoint_init_hdr(endpoint);
146984f9bd12SAlex Elder 	ipa_endpoint_init_hdr_ext(endpoint);
1470fb57c3eaSAlex Elder 	ipa_endpoint_init_hdr_metadata_mask(endpoint);
1471fb57c3eaSAlex Elder 	ipa_endpoint_init_mode(endpoint);
147284f9bd12SAlex Elder 	ipa_endpoint_init_aggr(endpoint);
147384f9bd12SAlex Elder 	ipa_endpoint_init_deaggr(endpoint);
14742d265342SAlex Elder 	ipa_endpoint_init_rsrc_grp(endpoint);
147584f9bd12SAlex Elder 	ipa_endpoint_init_seq(endpoint);
147684f9bd12SAlex Elder 	ipa_endpoint_status(endpoint);
147784f9bd12SAlex Elder }
147884f9bd12SAlex Elder 
147984f9bd12SAlex Elder int ipa_endpoint_enable_one(struct ipa_endpoint *endpoint)
148084f9bd12SAlex Elder {
148184f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
148284f9bd12SAlex Elder 	struct gsi *gsi = &ipa->gsi;
148384f9bd12SAlex Elder 	int ret;
148484f9bd12SAlex Elder 
148584f9bd12SAlex Elder 	ret = gsi_channel_start(gsi, endpoint->channel_id);
148684f9bd12SAlex Elder 	if (ret) {
148784f9bd12SAlex Elder 		dev_err(&ipa->pdev->dev,
148884f9bd12SAlex Elder 			"error %d starting %cX channel %u for endpoint %u\n",
148984f9bd12SAlex Elder 			ret, endpoint->toward_ipa ? 'T' : 'R',
149084f9bd12SAlex Elder 			endpoint->channel_id, endpoint->endpoint_id);
149184f9bd12SAlex Elder 		return ret;
149284f9bd12SAlex Elder 	}
149384f9bd12SAlex Elder 
149484f9bd12SAlex Elder 	if (!endpoint->toward_ipa) {
149584f9bd12SAlex Elder 		ipa_interrupt_suspend_enable(ipa->interrupt,
149684f9bd12SAlex Elder 					     endpoint->endpoint_id);
149784f9bd12SAlex Elder 		ipa_endpoint_replenish_enable(endpoint);
149884f9bd12SAlex Elder 	}
149984f9bd12SAlex Elder 
150084f9bd12SAlex Elder 	ipa->enabled |= BIT(endpoint->endpoint_id);
150184f9bd12SAlex Elder 
150284f9bd12SAlex Elder 	return 0;
150384f9bd12SAlex Elder }
150484f9bd12SAlex Elder 
150584f9bd12SAlex Elder void ipa_endpoint_disable_one(struct ipa_endpoint *endpoint)
150684f9bd12SAlex Elder {
150784f9bd12SAlex Elder 	u32 mask = BIT(endpoint->endpoint_id);
150884f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
1509f30dcb7dSAlex Elder 	struct gsi *gsi = &ipa->gsi;
151084f9bd12SAlex Elder 	int ret;
151184f9bd12SAlex Elder 
1512f30dcb7dSAlex Elder 	if (!(ipa->enabled & mask))
151384f9bd12SAlex Elder 		return;
151484f9bd12SAlex Elder 
1515f30dcb7dSAlex Elder 	ipa->enabled ^= mask;
151684f9bd12SAlex Elder 
151784f9bd12SAlex Elder 	if (!endpoint->toward_ipa) {
151884f9bd12SAlex Elder 		ipa_endpoint_replenish_disable(endpoint);
151984f9bd12SAlex Elder 		ipa_interrupt_suspend_disable(ipa->interrupt,
152084f9bd12SAlex Elder 					      endpoint->endpoint_id);
152184f9bd12SAlex Elder 	}
152284f9bd12SAlex Elder 
152384f9bd12SAlex Elder 	/* Note that if stop fails, the channel's state is not well-defined */
1524f30dcb7dSAlex Elder 	ret = gsi_channel_stop(gsi, endpoint->channel_id);
152584f9bd12SAlex Elder 	if (ret)
152684f9bd12SAlex Elder 		dev_err(&ipa->pdev->dev,
152784f9bd12SAlex Elder 			"error %d attempting to stop endpoint %u\n", ret,
152884f9bd12SAlex Elder 			endpoint->endpoint_id);
152984f9bd12SAlex Elder }
153084f9bd12SAlex Elder 
153184f9bd12SAlex Elder void ipa_endpoint_suspend_one(struct ipa_endpoint *endpoint)
153284f9bd12SAlex Elder {
153384f9bd12SAlex Elder 	struct device *dev = &endpoint->ipa->pdev->dev;
153484f9bd12SAlex Elder 	struct gsi *gsi = &endpoint->ipa->gsi;
153584f9bd12SAlex Elder 	bool stop_channel;
153684f9bd12SAlex Elder 	int ret;
153784f9bd12SAlex Elder 
153884f9bd12SAlex Elder 	if (!(endpoint->ipa->enabled & BIT(endpoint->endpoint_id)))
153984f9bd12SAlex Elder 		return;
154084f9bd12SAlex Elder 
1541ab4f71e5SAlex Elder 	if (!endpoint->toward_ipa) {
154284f9bd12SAlex Elder 		ipa_endpoint_replenish_disable(endpoint);
15434fa95248SAlex Elder 		(void)ipa_endpoint_program_suspend(endpoint, true);
1544ab4f71e5SAlex Elder 	}
154584f9bd12SAlex Elder 
1546b07f283eSAlex Elder 	/* IPA v3.5.1 doesn't use channel stop for suspend */
1547b07f283eSAlex Elder 	stop_channel = endpoint->ipa->version != IPA_VERSION_3_5_1;
154884f9bd12SAlex Elder 	ret = gsi_channel_suspend(gsi, endpoint->channel_id, stop_channel);
154984f9bd12SAlex Elder 	if (ret)
155084f9bd12SAlex Elder 		dev_err(dev, "error %d suspending channel %u\n", ret,
155184f9bd12SAlex Elder 			endpoint->channel_id);
155284f9bd12SAlex Elder }
155384f9bd12SAlex Elder 
155484f9bd12SAlex Elder void ipa_endpoint_resume_one(struct ipa_endpoint *endpoint)
155584f9bd12SAlex Elder {
155684f9bd12SAlex Elder 	struct device *dev = &endpoint->ipa->pdev->dev;
155784f9bd12SAlex Elder 	struct gsi *gsi = &endpoint->ipa->gsi;
155884f9bd12SAlex Elder 	bool start_channel;
155984f9bd12SAlex Elder 	int ret;
156084f9bd12SAlex Elder 
156184f9bd12SAlex Elder 	if (!(endpoint->ipa->enabled & BIT(endpoint->endpoint_id)))
156284f9bd12SAlex Elder 		return;
156384f9bd12SAlex Elder 
1564b07f283eSAlex Elder 	if (!endpoint->toward_ipa)
15654fa95248SAlex Elder 		(void)ipa_endpoint_program_suspend(endpoint, false);
156684f9bd12SAlex Elder 
1567b07f283eSAlex Elder 	/* IPA v3.5.1 doesn't use channel start for resume */
1568b07f283eSAlex Elder 	start_channel = endpoint->ipa->version != IPA_VERSION_3_5_1;
156984f9bd12SAlex Elder 	ret = gsi_channel_resume(gsi, endpoint->channel_id, start_channel);
157084f9bd12SAlex Elder 	if (ret)
157184f9bd12SAlex Elder 		dev_err(dev, "error %d resuming channel %u\n", ret,
157284f9bd12SAlex Elder 			endpoint->channel_id);
157384f9bd12SAlex Elder 	else if (!endpoint->toward_ipa)
157484f9bd12SAlex Elder 		ipa_endpoint_replenish_enable(endpoint);
157584f9bd12SAlex Elder }
157684f9bd12SAlex Elder 
157784f9bd12SAlex Elder void ipa_endpoint_suspend(struct ipa *ipa)
157884f9bd12SAlex Elder {
1579d1704382SAlex Elder 	if (!ipa->setup_complete)
1580d1704382SAlex Elder 		return;
1581d1704382SAlex Elder 
158284f9bd12SAlex Elder 	if (ipa->modem_netdev)
158384f9bd12SAlex Elder 		ipa_modem_suspend(ipa->modem_netdev);
158484f9bd12SAlex Elder 
1585aa56e3e5SAlex Elder 	ipa_cmd_pipeline_clear(ipa);
15866cb63ea6SAlex Elder 
158784f9bd12SAlex Elder 	ipa_endpoint_suspend_one(ipa->name_map[IPA_ENDPOINT_AP_LAN_RX]);
158884f9bd12SAlex Elder 	ipa_endpoint_suspend_one(ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]);
158984f9bd12SAlex Elder }
159084f9bd12SAlex Elder 
159184f9bd12SAlex Elder void ipa_endpoint_resume(struct ipa *ipa)
159284f9bd12SAlex Elder {
1593d1704382SAlex Elder 	if (!ipa->setup_complete)
1594d1704382SAlex Elder 		return;
1595d1704382SAlex Elder 
159684f9bd12SAlex Elder 	ipa_endpoint_resume_one(ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]);
159784f9bd12SAlex Elder 	ipa_endpoint_resume_one(ipa->name_map[IPA_ENDPOINT_AP_LAN_RX]);
159884f9bd12SAlex Elder 
159984f9bd12SAlex Elder 	if (ipa->modem_netdev)
160084f9bd12SAlex Elder 		ipa_modem_resume(ipa->modem_netdev);
160184f9bd12SAlex Elder }
160284f9bd12SAlex Elder 
160384f9bd12SAlex Elder static void ipa_endpoint_setup_one(struct ipa_endpoint *endpoint)
160484f9bd12SAlex Elder {
160584f9bd12SAlex Elder 	struct gsi *gsi = &endpoint->ipa->gsi;
160684f9bd12SAlex Elder 	u32 channel_id = endpoint->channel_id;
160784f9bd12SAlex Elder 
160884f9bd12SAlex Elder 	/* Only AP endpoints get set up */
160984f9bd12SAlex Elder 	if (endpoint->ee_id != GSI_EE_AP)
161084f9bd12SAlex Elder 		return;
161184f9bd12SAlex Elder 
161284f9bd12SAlex Elder 	endpoint->trans_tre_max = gsi_channel_trans_tre_max(gsi, channel_id);
161384f9bd12SAlex Elder 	if (!endpoint->toward_ipa) {
161484f9bd12SAlex Elder 		/* RX transactions require a single TRE, so the maximum
161584f9bd12SAlex Elder 		 * backlog is the same as the maximum outstanding TREs.
161684f9bd12SAlex Elder 		 */
161784f9bd12SAlex Elder 		endpoint->replenish_enabled = false;
161884f9bd12SAlex Elder 		atomic_set(&endpoint->replenish_saved,
161984f9bd12SAlex Elder 			   gsi_channel_tre_max(gsi, endpoint->channel_id));
162084f9bd12SAlex Elder 		atomic_set(&endpoint->replenish_backlog, 0);
162184f9bd12SAlex Elder 		INIT_DELAYED_WORK(&endpoint->replenish_work,
162284f9bd12SAlex Elder 				  ipa_endpoint_replenish_work);
162384f9bd12SAlex Elder 	}
162484f9bd12SAlex Elder 
162584f9bd12SAlex Elder 	ipa_endpoint_program(endpoint);
162684f9bd12SAlex Elder 
162784f9bd12SAlex Elder 	endpoint->ipa->set_up |= BIT(endpoint->endpoint_id);
162884f9bd12SAlex Elder }
162984f9bd12SAlex Elder 
163084f9bd12SAlex Elder static void ipa_endpoint_teardown_one(struct ipa_endpoint *endpoint)
163184f9bd12SAlex Elder {
163284f9bd12SAlex Elder 	endpoint->ipa->set_up &= ~BIT(endpoint->endpoint_id);
163384f9bd12SAlex Elder 
163484f9bd12SAlex Elder 	if (!endpoint->toward_ipa)
163584f9bd12SAlex Elder 		cancel_delayed_work_sync(&endpoint->replenish_work);
163684f9bd12SAlex Elder 
163784f9bd12SAlex Elder 	ipa_endpoint_reset(endpoint);
163884f9bd12SAlex Elder }
163984f9bd12SAlex Elder 
164084f9bd12SAlex Elder void ipa_endpoint_setup(struct ipa *ipa)
164184f9bd12SAlex Elder {
164284f9bd12SAlex Elder 	u32 initialized = ipa->initialized;
164384f9bd12SAlex Elder 
164484f9bd12SAlex Elder 	ipa->set_up = 0;
164584f9bd12SAlex Elder 	while (initialized) {
164684f9bd12SAlex Elder 		u32 endpoint_id = __ffs(initialized);
164784f9bd12SAlex Elder 
164884f9bd12SAlex Elder 		initialized ^= BIT(endpoint_id);
164984f9bd12SAlex Elder 
165084f9bd12SAlex Elder 		ipa_endpoint_setup_one(&ipa->endpoint[endpoint_id]);
165184f9bd12SAlex Elder 	}
165284f9bd12SAlex Elder }
165384f9bd12SAlex Elder 
165484f9bd12SAlex Elder void ipa_endpoint_teardown(struct ipa *ipa)
165584f9bd12SAlex Elder {
165684f9bd12SAlex Elder 	u32 set_up = ipa->set_up;
165784f9bd12SAlex Elder 
165884f9bd12SAlex Elder 	while (set_up) {
165984f9bd12SAlex Elder 		u32 endpoint_id = __fls(set_up);
166084f9bd12SAlex Elder 
166184f9bd12SAlex Elder 		set_up ^= BIT(endpoint_id);
166284f9bd12SAlex Elder 
166384f9bd12SAlex Elder 		ipa_endpoint_teardown_one(&ipa->endpoint[endpoint_id]);
166484f9bd12SAlex Elder 	}
166584f9bd12SAlex Elder 	ipa->set_up = 0;
166684f9bd12SAlex Elder }
166784f9bd12SAlex Elder 
166884f9bd12SAlex Elder int ipa_endpoint_config(struct ipa *ipa)
166984f9bd12SAlex Elder {
167084f9bd12SAlex Elder 	struct device *dev = &ipa->pdev->dev;
167184f9bd12SAlex Elder 	u32 initialized;
167284f9bd12SAlex Elder 	u32 rx_base;
167384f9bd12SAlex Elder 	u32 rx_mask;
167484f9bd12SAlex Elder 	u32 tx_mask;
167584f9bd12SAlex Elder 	int ret = 0;
167684f9bd12SAlex Elder 	u32 max;
167784f9bd12SAlex Elder 	u32 val;
167884f9bd12SAlex Elder 
167984f9bd12SAlex Elder 	/* Find out about the endpoints supplied by the hardware, and ensure
168084f9bd12SAlex Elder 	 * the highest one doesn't exceed the number we support.
168184f9bd12SAlex Elder 	 */
168284f9bd12SAlex Elder 	val = ioread32(ipa->reg_virt + IPA_REG_FLAVOR_0_OFFSET);
168384f9bd12SAlex Elder 
168484f9bd12SAlex Elder 	/* Our RX is an IPA producer */
1685716a115bSAlex Elder 	rx_base = u32_get_bits(val, IPA_PROD_LOWEST_FMASK);
1686716a115bSAlex Elder 	max = rx_base + u32_get_bits(val, IPA_MAX_PROD_PIPES_FMASK);
168784f9bd12SAlex Elder 	if (max > IPA_ENDPOINT_MAX) {
168884f9bd12SAlex Elder 		dev_err(dev, "too many endpoints (%u > %u)\n",
168984f9bd12SAlex Elder 			max, IPA_ENDPOINT_MAX);
169084f9bd12SAlex Elder 		return -EINVAL;
169184f9bd12SAlex Elder 	}
169284f9bd12SAlex Elder 	rx_mask = GENMASK(max - 1, rx_base);
169384f9bd12SAlex Elder 
169484f9bd12SAlex Elder 	/* Our TX is an IPA consumer */
1695716a115bSAlex Elder 	max = u32_get_bits(val, IPA_MAX_CONS_PIPES_FMASK);
169684f9bd12SAlex Elder 	tx_mask = GENMASK(max - 1, 0);
169784f9bd12SAlex Elder 
169884f9bd12SAlex Elder 	ipa->available = rx_mask | tx_mask;
169984f9bd12SAlex Elder 
170084f9bd12SAlex Elder 	/* Check for initialized endpoints not supported by the hardware */
170184f9bd12SAlex Elder 	if (ipa->initialized & ~ipa->available) {
170284f9bd12SAlex Elder 		dev_err(dev, "unavailable endpoint id(s) 0x%08x\n",
170384f9bd12SAlex Elder 			ipa->initialized & ~ipa->available);
170484f9bd12SAlex Elder 		ret = -EINVAL;		/* Report other errors too */
170584f9bd12SAlex Elder 	}
170684f9bd12SAlex Elder 
170784f9bd12SAlex Elder 	initialized = ipa->initialized;
170884f9bd12SAlex Elder 	while (initialized) {
170984f9bd12SAlex Elder 		u32 endpoint_id = __ffs(initialized);
171084f9bd12SAlex Elder 		struct ipa_endpoint *endpoint;
171184f9bd12SAlex Elder 
171284f9bd12SAlex Elder 		initialized ^= BIT(endpoint_id);
171384f9bd12SAlex Elder 
171484f9bd12SAlex Elder 		/* Make sure it's pointing in the right direction */
171584f9bd12SAlex Elder 		endpoint = &ipa->endpoint[endpoint_id];
171684f9bd12SAlex Elder 		if ((endpoint_id < rx_base) != !!endpoint->toward_ipa) {
171784f9bd12SAlex Elder 			dev_err(dev, "endpoint id %u wrong direction\n",
171884f9bd12SAlex Elder 				endpoint_id);
171984f9bd12SAlex Elder 			ret = -EINVAL;
172084f9bd12SAlex Elder 		}
172184f9bd12SAlex Elder 	}
172284f9bd12SAlex Elder 
172384f9bd12SAlex Elder 	return ret;
172484f9bd12SAlex Elder }
172584f9bd12SAlex Elder 
172684f9bd12SAlex Elder void ipa_endpoint_deconfig(struct ipa *ipa)
172784f9bd12SAlex Elder {
172884f9bd12SAlex Elder 	ipa->available = 0;	/* Nothing more to do */
172984f9bd12SAlex Elder }
173084f9bd12SAlex Elder 
173184f9bd12SAlex Elder static void ipa_endpoint_init_one(struct ipa *ipa, enum ipa_endpoint_name name,
173284f9bd12SAlex Elder 				  const struct ipa_gsi_endpoint_data *data)
173384f9bd12SAlex Elder {
173484f9bd12SAlex Elder 	struct ipa_endpoint *endpoint;
173584f9bd12SAlex Elder 
173684f9bd12SAlex Elder 	endpoint = &ipa->endpoint[data->endpoint_id];
173784f9bd12SAlex Elder 
173884f9bd12SAlex Elder 	if (data->ee_id == GSI_EE_AP)
173984f9bd12SAlex Elder 		ipa->channel_map[data->channel_id] = endpoint;
174084f9bd12SAlex Elder 	ipa->name_map[name] = endpoint;
174184f9bd12SAlex Elder 
174284f9bd12SAlex Elder 	endpoint->ipa = ipa;
174384f9bd12SAlex Elder 	endpoint->ee_id = data->ee_id;
174484f9bd12SAlex Elder 	endpoint->seq_type = data->endpoint.seq_type;
174584f9bd12SAlex Elder 	endpoint->channel_id = data->channel_id;
174684f9bd12SAlex Elder 	endpoint->endpoint_id = data->endpoint_id;
174784f9bd12SAlex Elder 	endpoint->toward_ipa = data->toward_ipa;
174884f9bd12SAlex Elder 	endpoint->data = &data->endpoint.config;
174984f9bd12SAlex Elder 
175084f9bd12SAlex Elder 	ipa->initialized |= BIT(endpoint->endpoint_id);
175184f9bd12SAlex Elder }
175284f9bd12SAlex Elder 
175384f9bd12SAlex Elder void ipa_endpoint_exit_one(struct ipa_endpoint *endpoint)
175484f9bd12SAlex Elder {
175584f9bd12SAlex Elder 	endpoint->ipa->initialized &= ~BIT(endpoint->endpoint_id);
175684f9bd12SAlex Elder 
175784f9bd12SAlex Elder 	memset(endpoint, 0, sizeof(*endpoint));
175884f9bd12SAlex Elder }
175984f9bd12SAlex Elder 
176084f9bd12SAlex Elder void ipa_endpoint_exit(struct ipa *ipa)
176184f9bd12SAlex Elder {
176284f9bd12SAlex Elder 	u32 initialized = ipa->initialized;
176384f9bd12SAlex Elder 
176484f9bd12SAlex Elder 	while (initialized) {
176584f9bd12SAlex Elder 		u32 endpoint_id = __fls(initialized);
176684f9bd12SAlex Elder 
176784f9bd12SAlex Elder 		initialized ^= BIT(endpoint_id);
176884f9bd12SAlex Elder 
176984f9bd12SAlex Elder 		ipa_endpoint_exit_one(&ipa->endpoint[endpoint_id]);
177084f9bd12SAlex Elder 	}
177184f9bd12SAlex Elder 	memset(ipa->name_map, 0, sizeof(ipa->name_map));
177284f9bd12SAlex Elder 	memset(ipa->channel_map, 0, sizeof(ipa->channel_map));
177384f9bd12SAlex Elder }
177484f9bd12SAlex Elder 
177584f9bd12SAlex Elder /* Returns a bitmask of endpoints that support filtering, or 0 on error */
177684f9bd12SAlex Elder u32 ipa_endpoint_init(struct ipa *ipa, u32 count,
177784f9bd12SAlex Elder 		      const struct ipa_gsi_endpoint_data *data)
177884f9bd12SAlex Elder {
177984f9bd12SAlex Elder 	enum ipa_endpoint_name name;
178084f9bd12SAlex Elder 	u32 filter_map;
178184f9bd12SAlex Elder 
178284f9bd12SAlex Elder 	if (!ipa_endpoint_data_valid(ipa, count, data))
178384f9bd12SAlex Elder 		return 0;	/* Error */
178484f9bd12SAlex Elder 
178584f9bd12SAlex Elder 	ipa->initialized = 0;
178684f9bd12SAlex Elder 
178784f9bd12SAlex Elder 	filter_map = 0;
178884f9bd12SAlex Elder 	for (name = 0; name < count; name++, data++) {
178984f9bd12SAlex Elder 		if (ipa_gsi_endpoint_data_empty(data))
179084f9bd12SAlex Elder 			continue;	/* Skip over empty slots */
179184f9bd12SAlex Elder 
179284f9bd12SAlex Elder 		ipa_endpoint_init_one(ipa, name, data);
179384f9bd12SAlex Elder 
179484f9bd12SAlex Elder 		if (data->endpoint.filter_support)
179584f9bd12SAlex Elder 			filter_map |= BIT(data->endpoint_id);
179684f9bd12SAlex Elder 	}
179784f9bd12SAlex Elder 
179884f9bd12SAlex Elder 	if (!ipa_filter_map_valid(ipa, filter_map))
179984f9bd12SAlex Elder 		goto err_endpoint_exit;
180084f9bd12SAlex Elder 
180184f9bd12SAlex Elder 	return filter_map;	/* Non-zero bitmask */
180284f9bd12SAlex Elder 
180384f9bd12SAlex Elder err_endpoint_exit:
180484f9bd12SAlex Elder 	ipa_endpoint_exit(ipa);
180584f9bd12SAlex Elder 
180684f9bd12SAlex Elder 	return 0;	/* Error */
180784f9bd12SAlex Elder }
1808