xref: /linux/drivers/net/ipa/ipa_endpoint.c (revision 01c36637aeaf5d3e74be4d8e00e8f4e62857dac0)
184f9bd12SAlex Elder // SPDX-License-Identifier: GPL-2.0
284f9bd12SAlex Elder 
384f9bd12SAlex Elder /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
4647a05f3SAlex Elder  * Copyright (C) 2019-2021 Linaro Ltd.
584f9bd12SAlex Elder  */
684f9bd12SAlex Elder 
784f9bd12SAlex Elder #include <linux/types.h>
884f9bd12SAlex Elder #include <linux/device.h>
984f9bd12SAlex Elder #include <linux/slab.h>
1084f9bd12SAlex Elder #include <linux/bitfield.h>
1184f9bd12SAlex Elder #include <linux/if_rmnet.h>
1284f9bd12SAlex Elder #include <linux/dma-direction.h>
1384f9bd12SAlex Elder 
1484f9bd12SAlex Elder #include "gsi.h"
1584f9bd12SAlex Elder #include "gsi_trans.h"
1684f9bd12SAlex Elder #include "ipa.h"
1784f9bd12SAlex Elder #include "ipa_data.h"
1884f9bd12SAlex Elder #include "ipa_endpoint.h"
1984f9bd12SAlex Elder #include "ipa_cmd.h"
2084f9bd12SAlex Elder #include "ipa_mem.h"
2184f9bd12SAlex Elder #include "ipa_modem.h"
2284f9bd12SAlex Elder #include "ipa_table.h"
2384f9bd12SAlex Elder #include "ipa_gsi.h"
242775cbc5SAlex Elder #include "ipa_power.h"
2584f9bd12SAlex Elder 
2684f9bd12SAlex Elder #define atomic_dec_not_zero(v)	atomic_add_unless((v), -1, 0)
2784f9bd12SAlex Elder 
2884f9bd12SAlex Elder #define IPA_REPLENISH_BATCH	16
2984f9bd12SAlex Elder 
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 */
72f6aba7b5SAlex Elder #define IPA_STATUS_MASK_TAG_VALID_FMASK		GENMASK(4, 4)
73f6aba7b5SAlex 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)
76f6aba7b5SAlex Elder #define IPA_STATUS_FLAGS2_TAG_FMASK		GENMASK_ULL(63, 16)
7784f9bd12SAlex Elder 
7884f9bd12SAlex Elder static bool ipa_endpoint_data_valid_one(struct ipa *ipa, u32 count,
7984f9bd12SAlex Elder 			    const struct ipa_gsi_endpoint_data *all_data,
8084f9bd12SAlex Elder 			    const struct ipa_gsi_endpoint_data *data)
8184f9bd12SAlex Elder {
8284f9bd12SAlex Elder 	const struct ipa_gsi_endpoint_data *other_data;
8384f9bd12SAlex Elder 	struct device *dev = &ipa->pdev->dev;
8484f9bd12SAlex Elder 	enum ipa_endpoint_name other_name;
8584f9bd12SAlex Elder 
8684f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(data))
8784f9bd12SAlex Elder 		return true;
8884f9bd12SAlex Elder 
8984f9bd12SAlex Elder 	if (!data->toward_ipa) {
9084f9bd12SAlex Elder 		if (data->endpoint.filter_support) {
9184f9bd12SAlex Elder 			dev_err(dev, "filtering not supported for "
9284f9bd12SAlex Elder 					"RX endpoint %u\n",
9384f9bd12SAlex Elder 				data->endpoint_id);
9484f9bd12SAlex Elder 			return false;
9584f9bd12SAlex Elder 		}
9684f9bd12SAlex Elder 
9784f9bd12SAlex Elder 		return true;	/* Nothing more to check for RX */
9884f9bd12SAlex Elder 	}
9984f9bd12SAlex Elder 
10084f9bd12SAlex Elder 	if (data->endpoint.config.status_enable) {
10184f9bd12SAlex Elder 		other_name = data->endpoint.config.tx.status_endpoint;
10284f9bd12SAlex Elder 		if (other_name >= count) {
10384f9bd12SAlex Elder 			dev_err(dev, "status endpoint name %u out of range "
10484f9bd12SAlex Elder 					"for endpoint %u\n",
10584f9bd12SAlex Elder 				other_name, data->endpoint_id);
10684f9bd12SAlex Elder 			return false;
10784f9bd12SAlex Elder 		}
10884f9bd12SAlex Elder 
10984f9bd12SAlex Elder 		/* Status endpoint must be defined... */
11084f9bd12SAlex Elder 		other_data = &all_data[other_name];
11184f9bd12SAlex Elder 		if (ipa_gsi_endpoint_data_empty(other_data)) {
11284f9bd12SAlex Elder 			dev_err(dev, "DMA endpoint name %u undefined "
11384f9bd12SAlex Elder 					"for endpoint %u\n",
11484f9bd12SAlex Elder 				other_name, data->endpoint_id);
11584f9bd12SAlex Elder 			return false;
11684f9bd12SAlex Elder 		}
11784f9bd12SAlex Elder 
11884f9bd12SAlex Elder 		/* ...and has to be an RX endpoint... */
11984f9bd12SAlex Elder 		if (other_data->toward_ipa) {
12084f9bd12SAlex Elder 			dev_err(dev,
12184f9bd12SAlex Elder 				"status endpoint for endpoint %u not RX\n",
12284f9bd12SAlex Elder 				data->endpoint_id);
12384f9bd12SAlex Elder 			return false;
12484f9bd12SAlex Elder 		}
12584f9bd12SAlex Elder 
12684f9bd12SAlex Elder 		/* ...and if it's to be an AP endpoint... */
12784f9bd12SAlex Elder 		if (other_data->ee_id == GSI_EE_AP) {
12884f9bd12SAlex Elder 			/* ...make sure it has status enabled. */
12984f9bd12SAlex Elder 			if (!other_data->endpoint.config.status_enable) {
13084f9bd12SAlex Elder 				dev_err(dev,
13184f9bd12SAlex Elder 					"status not enabled for endpoint %u\n",
13284f9bd12SAlex Elder 					other_data->endpoint_id);
13384f9bd12SAlex Elder 				return false;
13484f9bd12SAlex Elder 			}
13584f9bd12SAlex Elder 		}
13684f9bd12SAlex Elder 	}
13784f9bd12SAlex Elder 
13884f9bd12SAlex Elder 	if (data->endpoint.config.dma_mode) {
13984f9bd12SAlex Elder 		other_name = data->endpoint.config.dma_endpoint;
14084f9bd12SAlex Elder 		if (other_name >= count) {
14184f9bd12SAlex Elder 			dev_err(dev, "DMA endpoint name %u out of range "
14284f9bd12SAlex Elder 					"for endpoint %u\n",
14384f9bd12SAlex Elder 				other_name, data->endpoint_id);
14484f9bd12SAlex Elder 			return false;
14584f9bd12SAlex Elder 		}
14684f9bd12SAlex Elder 
14784f9bd12SAlex Elder 		other_data = &all_data[other_name];
14884f9bd12SAlex Elder 		if (ipa_gsi_endpoint_data_empty(other_data)) {
14984f9bd12SAlex Elder 			dev_err(dev, "DMA endpoint name %u undefined "
15084f9bd12SAlex Elder 					"for endpoint %u\n",
15184f9bd12SAlex Elder 				other_name, data->endpoint_id);
15284f9bd12SAlex Elder 			return false;
15384f9bd12SAlex Elder 		}
15484f9bd12SAlex Elder 	}
15584f9bd12SAlex Elder 
15684f9bd12SAlex Elder 	return true;
15784f9bd12SAlex Elder }
15884f9bd12SAlex Elder 
1596bf754c7SAlex Elder static u32 aggr_byte_limit_max(enum ipa_version version)
1606bf754c7SAlex Elder {
1616bf754c7SAlex Elder 	if (version < IPA_VERSION_4_5)
1626bf754c7SAlex Elder 		return field_max(aggr_byte_limit_fmask(true));
1636bf754c7SAlex Elder 
1646bf754c7SAlex Elder 	return field_max(aggr_byte_limit_fmask(false));
1656bf754c7SAlex Elder }
1666bf754c7SAlex Elder 
16784f9bd12SAlex Elder static bool ipa_endpoint_data_valid(struct ipa *ipa, u32 count,
16884f9bd12SAlex Elder 				    const struct ipa_gsi_endpoint_data *data)
16984f9bd12SAlex Elder {
17084f9bd12SAlex Elder 	const struct ipa_gsi_endpoint_data *dp = data;
17184f9bd12SAlex Elder 	struct device *dev = &ipa->pdev->dev;
17284f9bd12SAlex Elder 	enum ipa_endpoint_name name;
1736bf754c7SAlex Elder 	u32 limit;
17484f9bd12SAlex Elder 
17584f9bd12SAlex Elder 	if (count > IPA_ENDPOINT_COUNT) {
17684f9bd12SAlex Elder 		dev_err(dev, "too many endpoints specified (%u > %u)\n",
17784f9bd12SAlex Elder 			count, IPA_ENDPOINT_COUNT);
17884f9bd12SAlex Elder 		return false;
17984f9bd12SAlex Elder 	}
18084f9bd12SAlex Elder 
1816bf754c7SAlex Elder 	/* The aggregation byte limit defines the point at which an
1826bf754c7SAlex Elder 	 * aggregation window will close.  It is programmed into the
1836bf754c7SAlex Elder 	 * IPA hardware as a number of KB.  We don't use "hard byte
1846bf754c7SAlex Elder 	 * limit" aggregation, which means that we need to supply
1856bf754c7SAlex Elder 	 * enough space in a receive buffer to hold a complete MTU
1866bf754c7SAlex Elder 	 * plus normal skb overhead *after* that aggregation byte
1876bf754c7SAlex Elder 	 * limit has been crossed.
1886bf754c7SAlex Elder 	 *
1896bf754c7SAlex Elder 	 * This check ensures we don't define a receive buffer size
1906bf754c7SAlex Elder 	 * that would exceed what we can represent in the field that
1916bf754c7SAlex Elder 	 * is used to program its size.
1926bf754c7SAlex Elder 	 */
1936bf754c7SAlex Elder 	limit = aggr_byte_limit_max(ipa->version) * SZ_1K;
1946bf754c7SAlex Elder 	limit += IPA_MTU + IPA_RX_BUFFER_OVERHEAD;
1956bf754c7SAlex Elder 	if (limit < IPA_RX_BUFFER_SIZE) {
1966bf754c7SAlex Elder 		dev_err(dev, "buffer size too big for aggregation (%u > %u)\n",
1976bf754c7SAlex Elder 			IPA_RX_BUFFER_SIZE, limit);
1986bf754c7SAlex Elder 		return false;
1996bf754c7SAlex Elder 	}
2006bf754c7SAlex Elder 
20184f9bd12SAlex Elder 	/* Make sure needed endpoints have defined data */
20284f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_COMMAND_TX])) {
20384f9bd12SAlex Elder 		dev_err(dev, "command TX endpoint not defined\n");
20484f9bd12SAlex Elder 		return false;
20584f9bd12SAlex Elder 	}
20684f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_LAN_RX])) {
20784f9bd12SAlex Elder 		dev_err(dev, "LAN RX endpoint not defined\n");
20884f9bd12SAlex Elder 		return false;
20984f9bd12SAlex Elder 	}
21084f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_MODEM_TX])) {
21184f9bd12SAlex Elder 		dev_err(dev, "AP->modem TX endpoint not defined\n");
21284f9bd12SAlex Elder 		return false;
21384f9bd12SAlex Elder 	}
21484f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_MODEM_RX])) {
21584f9bd12SAlex Elder 		dev_err(dev, "AP<-modem RX endpoint not defined\n");
21684f9bd12SAlex Elder 		return false;
21784f9bd12SAlex Elder 	}
21884f9bd12SAlex Elder 
21984f9bd12SAlex Elder 	for (name = 0; name < count; name++, dp++)
22084f9bd12SAlex Elder 		if (!ipa_endpoint_data_valid_one(ipa, count, data, dp))
22184f9bd12SAlex Elder 			return false;
22284f9bd12SAlex Elder 
22384f9bd12SAlex Elder 	return true;
22484f9bd12SAlex Elder }
22584f9bd12SAlex Elder 
22684f9bd12SAlex Elder /* Allocate a transaction to use on a non-command endpoint */
22784f9bd12SAlex Elder static struct gsi_trans *ipa_endpoint_trans_alloc(struct ipa_endpoint *endpoint,
22884f9bd12SAlex Elder 						  u32 tre_count)
22984f9bd12SAlex Elder {
23084f9bd12SAlex Elder 	struct gsi *gsi = &endpoint->ipa->gsi;
23184f9bd12SAlex Elder 	u32 channel_id = endpoint->channel_id;
23284f9bd12SAlex Elder 	enum dma_data_direction direction;
23384f9bd12SAlex Elder 
23484f9bd12SAlex Elder 	direction = endpoint->toward_ipa ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
23584f9bd12SAlex Elder 
23684f9bd12SAlex Elder 	return gsi_channel_trans_alloc(gsi, channel_id, tre_count, direction);
23784f9bd12SAlex Elder }
23884f9bd12SAlex Elder 
23984f9bd12SAlex Elder /* suspend_delay represents suspend for RX, delay for TX endpoints.
24084f9bd12SAlex Elder  * Note that suspend is not supported starting with IPA v4.0.
24184f9bd12SAlex Elder  */
2424900bf34SAlex Elder static bool
24384f9bd12SAlex Elder ipa_endpoint_init_ctrl(struct ipa_endpoint *endpoint, bool suspend_delay)
24484f9bd12SAlex Elder {
24584f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_CTRL_N_OFFSET(endpoint->endpoint_id);
24684f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
2474900bf34SAlex Elder 	bool state;
24884f9bd12SAlex Elder 	u32 mask;
24984f9bd12SAlex Elder 	u32 val;
25084f9bd12SAlex Elder 
2514fa95248SAlex Elder 	/* Suspend is not supported for IPA v4.0+.  Delay doesn't work
2524fa95248SAlex Elder 	 * correctly on IPA v4.2.
2534fa95248SAlex Elder 	 */
2545bc55884SAlex Elder 	if (endpoint->toward_ipa)
2555bc55884SAlex Elder 		WARN_ON(ipa->version == IPA_VERSION_4_2);
2565bc55884SAlex Elder 	else
2575bc55884SAlex Elder 		WARN_ON(ipa->version >= IPA_VERSION_4_0);
2585bc55884SAlex Elder 
25984f9bd12SAlex Elder 	mask = endpoint->toward_ipa ? ENDP_DELAY_FMASK : ENDP_SUSPEND_FMASK;
26084f9bd12SAlex Elder 
26184f9bd12SAlex Elder 	val = ioread32(ipa->reg_virt + offset);
2624900bf34SAlex Elder 	state = !!(val & mask);
2635bc55884SAlex Elder 
2645bc55884SAlex Elder 	/* Don't bother if it's already in the requested state */
2654900bf34SAlex Elder 	if (suspend_delay != state) {
26684f9bd12SAlex Elder 		val ^= mask;
26784f9bd12SAlex Elder 		iowrite32(val, ipa->reg_virt + offset);
2684900bf34SAlex Elder 	}
26984f9bd12SAlex Elder 
2704900bf34SAlex Elder 	return state;
27184f9bd12SAlex Elder }
27284f9bd12SAlex Elder 
2734fa95248SAlex Elder /* We currently don't care what the previous state was for delay mode */
2744fa95248SAlex Elder static void
2754fa95248SAlex Elder ipa_endpoint_program_delay(struct ipa_endpoint *endpoint, bool enable)
2764fa95248SAlex Elder {
2775bc55884SAlex Elder 	WARN_ON(!endpoint->toward_ipa);
2784fa95248SAlex Elder 
27966eba767SAlex Elder 	/* Delay mode doesn't work properly for IPA v4.2 */
28066eba767SAlex Elder 	if (endpoint->ipa->version != IPA_VERSION_4_2)
2814fa95248SAlex Elder 		(void)ipa_endpoint_init_ctrl(endpoint, enable);
2824fa95248SAlex Elder }
2834fa95248SAlex Elder 
284fff89971SAlex Elder static bool ipa_endpoint_aggr_active(struct ipa_endpoint *endpoint)
285fff89971SAlex Elder {
286fff89971SAlex Elder 	u32 mask = BIT(endpoint->endpoint_id);
287fff89971SAlex Elder 	struct ipa *ipa = endpoint->ipa;
288fff89971SAlex Elder 	u32 offset;
289fff89971SAlex Elder 	u32 val;
290fff89971SAlex Elder 
2915bc55884SAlex Elder 	WARN_ON(!(mask & ipa->available));
2925bc55884SAlex Elder 
293fff89971SAlex Elder 	offset = ipa_reg_state_aggr_active_offset(ipa->version);
294fff89971SAlex Elder 	val = ioread32(ipa->reg_virt + offset);
295fff89971SAlex Elder 
296fff89971SAlex Elder 	return !!(val & mask);
297fff89971SAlex Elder }
298fff89971SAlex Elder 
299fff89971SAlex Elder static void ipa_endpoint_force_close(struct ipa_endpoint *endpoint)
300fff89971SAlex Elder {
301fff89971SAlex Elder 	u32 mask = BIT(endpoint->endpoint_id);
302fff89971SAlex Elder 	struct ipa *ipa = endpoint->ipa;
303fff89971SAlex Elder 
3045bc55884SAlex Elder 	WARN_ON(!(mask & ipa->available));
3055bc55884SAlex Elder 
306fff89971SAlex Elder 	iowrite32(mask, ipa->reg_virt + IPA_REG_AGGR_FORCE_CLOSE_OFFSET);
307fff89971SAlex Elder }
308fff89971SAlex Elder 
309fff89971SAlex Elder /**
310fff89971SAlex Elder  * ipa_endpoint_suspend_aggr() - Emulate suspend interrupt
311e3eea08eSAlex Elder  * @endpoint:	Endpoint on which to emulate a suspend
312fff89971SAlex Elder  *
313fff89971SAlex Elder  *  Emulate suspend IPA interrupt to unsuspend an endpoint suspended
314fff89971SAlex Elder  *  with an open aggregation frame.  This is to work around a hardware
315fff89971SAlex Elder  *  issue in IPA version 3.5.1 where the suspend interrupt will not be
316fff89971SAlex Elder  *  generated when it should be.
317fff89971SAlex Elder  */
318fff89971SAlex Elder static void ipa_endpoint_suspend_aggr(struct ipa_endpoint *endpoint)
319fff89971SAlex Elder {
320fff89971SAlex Elder 	struct ipa *ipa = endpoint->ipa;
321fff89971SAlex Elder 
322fff89971SAlex Elder 	if (!endpoint->data->aggregation)
323fff89971SAlex Elder 		return;
324fff89971SAlex Elder 
325fff89971SAlex Elder 	/* Nothing to do if the endpoint doesn't have aggregation open */
326fff89971SAlex Elder 	if (!ipa_endpoint_aggr_active(endpoint))
327fff89971SAlex Elder 		return;
328fff89971SAlex Elder 
329fff89971SAlex Elder 	/* Force close aggregation */
330fff89971SAlex Elder 	ipa_endpoint_force_close(endpoint);
331fff89971SAlex Elder 
332fff89971SAlex Elder 	ipa_interrupt_simulate_suspend(ipa->interrupt);
333fff89971SAlex Elder }
334fff89971SAlex Elder 
335fff89971SAlex Elder /* Returns previous suspend state (true means suspend was enabled) */
3364fa95248SAlex Elder static bool
3374fa95248SAlex Elder ipa_endpoint_program_suspend(struct ipa_endpoint *endpoint, bool enable)
3384fa95248SAlex Elder {
339fff89971SAlex Elder 	bool suspended;
340fff89971SAlex Elder 
341d7f3087bSAlex Elder 	if (endpoint->ipa->version >= IPA_VERSION_4_0)
342b07f283eSAlex Elder 		return enable;	/* For IPA v4.0+, no change made */
343b07f283eSAlex Elder 
3445bc55884SAlex Elder 	WARN_ON(endpoint->toward_ipa);
3454fa95248SAlex Elder 
346fff89971SAlex Elder 	suspended = ipa_endpoint_init_ctrl(endpoint, enable);
347fff89971SAlex Elder 
348fff89971SAlex Elder 	/* A client suspended with an open aggregation frame will not
349fff89971SAlex Elder 	 * generate a SUSPEND IPA interrupt.  If enabling suspend, have
350fff89971SAlex Elder 	 * ipa_endpoint_suspend_aggr() handle this.
351fff89971SAlex Elder 	 */
352fff89971SAlex Elder 	if (enable && !suspended)
353fff89971SAlex Elder 		ipa_endpoint_suspend_aggr(endpoint);
354fff89971SAlex Elder 
355fff89971SAlex Elder 	return suspended;
3564fa95248SAlex Elder }
3574fa95248SAlex Elder 
35884f9bd12SAlex Elder /* Enable or disable delay or suspend mode on all modem endpoints */
35984f9bd12SAlex Elder void ipa_endpoint_modem_pause_all(struct ipa *ipa, bool enable)
36084f9bd12SAlex Elder {
36184f9bd12SAlex Elder 	u32 endpoint_id;
36284f9bd12SAlex Elder 
3634fa95248SAlex Elder 	/* DELAY mode doesn't work correctly on IPA v4.2 */
36484f9bd12SAlex Elder 	if (ipa->version == IPA_VERSION_4_2)
36584f9bd12SAlex Elder 		return;
36684f9bd12SAlex Elder 
36784f9bd12SAlex Elder 	for (endpoint_id = 0; endpoint_id < IPA_ENDPOINT_MAX; endpoint_id++) {
36884f9bd12SAlex Elder 		struct ipa_endpoint *endpoint = &ipa->endpoint[endpoint_id];
36984f9bd12SAlex Elder 
37084f9bd12SAlex Elder 		if (endpoint->ee_id != GSI_EE_MODEM)
37184f9bd12SAlex Elder 			continue;
37284f9bd12SAlex Elder 
373b07f283eSAlex Elder 		/* Set TX delay mode or RX suspend mode */
3744fa95248SAlex Elder 		if (endpoint->toward_ipa)
3754fa95248SAlex Elder 			ipa_endpoint_program_delay(endpoint, enable);
376b07f283eSAlex Elder 		else
3774fa95248SAlex Elder 			(void)ipa_endpoint_program_suspend(endpoint, enable);
37884f9bd12SAlex Elder 	}
37984f9bd12SAlex Elder }
38084f9bd12SAlex Elder 
38184f9bd12SAlex Elder /* Reset all modem endpoints to use the default exception endpoint */
38284f9bd12SAlex Elder int ipa_endpoint_modem_exception_reset_all(struct ipa *ipa)
38384f9bd12SAlex Elder {
38484f9bd12SAlex Elder 	u32 initialized = ipa->initialized;
38584f9bd12SAlex Elder 	struct gsi_trans *trans;
38684f9bd12SAlex Elder 	u32 count;
38784f9bd12SAlex Elder 
38884f9bd12SAlex Elder 	/* We need one command per modem TX endpoint.  We can get an upper
38984f9bd12SAlex Elder 	 * bound on that by assuming all initialized endpoints are modem->IPA.
39084f9bd12SAlex Elder 	 * That won't happen, and we could be more precise, but this is fine
391602a1c76SAlex Elder 	 * for now.  End the transaction with commands to clear the pipeline.
39284f9bd12SAlex Elder 	 */
393aa56e3e5SAlex Elder 	count = hweight32(initialized) + ipa_cmd_pipeline_clear_count();
39484f9bd12SAlex Elder 	trans = ipa_cmd_trans_alloc(ipa, count);
39584f9bd12SAlex Elder 	if (!trans) {
39684f9bd12SAlex Elder 		dev_err(&ipa->pdev->dev,
39784f9bd12SAlex Elder 			"no transaction to reset modem exception endpoints\n");
39884f9bd12SAlex Elder 		return -EBUSY;
39984f9bd12SAlex Elder 	}
40084f9bd12SAlex Elder 
40184f9bd12SAlex Elder 	while (initialized) {
40284f9bd12SAlex Elder 		u32 endpoint_id = __ffs(initialized);
40384f9bd12SAlex Elder 		struct ipa_endpoint *endpoint;
40484f9bd12SAlex Elder 		u32 offset;
40584f9bd12SAlex Elder 
40684f9bd12SAlex Elder 		initialized ^= BIT(endpoint_id);
40784f9bd12SAlex Elder 
40884f9bd12SAlex Elder 		/* We only reset modem TX endpoints */
40984f9bd12SAlex Elder 		endpoint = &ipa->endpoint[endpoint_id];
41084f9bd12SAlex Elder 		if (!(endpoint->ee_id == GSI_EE_MODEM && endpoint->toward_ipa))
41184f9bd12SAlex Elder 			continue;
41284f9bd12SAlex Elder 
41384f9bd12SAlex Elder 		offset = IPA_REG_ENDP_STATUS_N_OFFSET(endpoint_id);
41484f9bd12SAlex Elder 
41584f9bd12SAlex Elder 		/* Value written is 0, and all bits are updated.  That
41684f9bd12SAlex Elder 		 * means status is disabled on the endpoint, and as a
41784f9bd12SAlex Elder 		 * result all other fields in the register are ignored.
41884f9bd12SAlex Elder 		 */
41984f9bd12SAlex Elder 		ipa_cmd_register_write_add(trans, offset, 0, ~0, false);
42084f9bd12SAlex Elder 	}
42184f9bd12SAlex Elder 
422aa56e3e5SAlex Elder 	ipa_cmd_pipeline_clear_add(trans);
42384f9bd12SAlex Elder 
42484f9bd12SAlex Elder 	/* XXX This should have a 1 second timeout */
42584f9bd12SAlex Elder 	gsi_trans_commit_wait(trans);
42684f9bd12SAlex Elder 
42751c48ce2SAlex Elder 	ipa_cmd_pipeline_clear_wait(ipa);
42851c48ce2SAlex Elder 
42984f9bd12SAlex Elder 	return 0;
43084f9bd12SAlex Elder }
43184f9bd12SAlex Elder 
43284f9bd12SAlex Elder static void ipa_endpoint_init_cfg(struct ipa_endpoint *endpoint)
43384f9bd12SAlex Elder {
43484f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_CFG_N_OFFSET(endpoint->endpoint_id);
4355567d4d9SAlex Elder 	enum ipa_cs_offload_en enabled;
43684f9bd12SAlex Elder 	u32 val = 0;
43784f9bd12SAlex Elder 
43884f9bd12SAlex Elder 	/* FRAG_OFFLOAD_EN is 0 */
43984f9bd12SAlex Elder 	if (endpoint->data->checksum) {
4405567d4d9SAlex Elder 		enum ipa_version version = endpoint->ipa->version;
4415567d4d9SAlex Elder 
44284f9bd12SAlex Elder 		if (endpoint->toward_ipa) {
44384f9bd12SAlex Elder 			u32 checksum_offset;
44484f9bd12SAlex Elder 
44584f9bd12SAlex Elder 			/* Checksum header offset is in 4-byte units */
44684f9bd12SAlex Elder 			checksum_offset = sizeof(struct rmnet_map_header);
44784f9bd12SAlex Elder 			checksum_offset /= sizeof(u32);
44884f9bd12SAlex Elder 			val |= u32_encode_bits(checksum_offset,
44984f9bd12SAlex Elder 					       CS_METADATA_HDR_OFFSET_FMASK);
4505567d4d9SAlex Elder 
4515567d4d9SAlex Elder 			enabled = version < IPA_VERSION_4_5
4525567d4d9SAlex Elder 					? IPA_CS_OFFLOAD_UL
4535567d4d9SAlex Elder 					: IPA_CS_OFFLOAD_INLINE;
45484f9bd12SAlex Elder 		} else {
4555567d4d9SAlex Elder 			enabled = version < IPA_VERSION_4_5
4565567d4d9SAlex Elder 					? IPA_CS_OFFLOAD_DL
4575567d4d9SAlex Elder 					: IPA_CS_OFFLOAD_INLINE;
45884f9bd12SAlex Elder 		}
45984f9bd12SAlex Elder 	} else {
4605567d4d9SAlex Elder 		enabled = IPA_CS_OFFLOAD_NONE;
46184f9bd12SAlex Elder 	}
4625567d4d9SAlex Elder 	val |= u32_encode_bits(enabled, CS_OFFLOAD_EN_FMASK);
46384f9bd12SAlex Elder 	/* CS_GEN_QMB_MASTER_SEL is 0 */
46484f9bd12SAlex Elder 
46584f9bd12SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
46684f9bd12SAlex Elder }
46784f9bd12SAlex Elder 
468647a05f3SAlex Elder static void ipa_endpoint_init_nat(struct ipa_endpoint *endpoint)
469647a05f3SAlex Elder {
470647a05f3SAlex Elder 	u32 offset;
471647a05f3SAlex Elder 	u32 val;
472647a05f3SAlex Elder 
473647a05f3SAlex Elder 	if (!endpoint->toward_ipa)
474647a05f3SAlex Elder 		return;
475647a05f3SAlex Elder 
476647a05f3SAlex Elder 	offset = IPA_REG_ENDP_INIT_NAT_N_OFFSET(endpoint->endpoint_id);
477647a05f3SAlex Elder 	val = u32_encode_bits(IPA_NAT_BYPASS, NAT_EN_FMASK);
478647a05f3SAlex Elder 
479647a05f3SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
480647a05f3SAlex Elder }
481647a05f3SAlex Elder 
4825567d4d9SAlex Elder static u32
4835567d4d9SAlex Elder ipa_qmap_header_size(enum ipa_version version, struct ipa_endpoint *endpoint)
4845567d4d9SAlex Elder {
4855567d4d9SAlex Elder 	u32 header_size = sizeof(struct rmnet_map_header);
4865567d4d9SAlex Elder 
4875567d4d9SAlex Elder 	/* Without checksum offload, we just have the MAP header */
4885567d4d9SAlex Elder 	if (!endpoint->data->checksum)
4895567d4d9SAlex Elder 		return header_size;
4905567d4d9SAlex Elder 
4915567d4d9SAlex Elder 	if (version < IPA_VERSION_4_5) {
4925567d4d9SAlex Elder 		/* Checksum header inserted for AP TX endpoints only */
4935567d4d9SAlex Elder 		if (endpoint->toward_ipa)
4945567d4d9SAlex Elder 			header_size += sizeof(struct rmnet_map_ul_csum_header);
4955567d4d9SAlex Elder 	} else {
4965567d4d9SAlex Elder 		/* Checksum header is used in both directions */
4975567d4d9SAlex Elder 		header_size += sizeof(struct rmnet_map_v5_csum_header);
4985567d4d9SAlex Elder 	}
4995567d4d9SAlex Elder 
5005567d4d9SAlex Elder 	return header_size;
5015567d4d9SAlex Elder }
5025567d4d9SAlex Elder 
5038730f45dSAlex Elder /**
504e3eea08eSAlex Elder  * ipa_endpoint_init_hdr() - Initialize HDR endpoint configuration register
505e3eea08eSAlex Elder  * @endpoint:	Endpoint pointer
506e3eea08eSAlex Elder  *
5078730f45dSAlex Elder  * We program QMAP endpoints so each packet received is preceded by a QMAP
5088730f45dSAlex Elder  * header structure.  The QMAP header contains a 1-byte mux_id and 2-byte
5098730f45dSAlex Elder  * packet size field, and we have the IPA hardware populate both for each
5108730f45dSAlex Elder  * received packet.  The header is configured (in the HDR_EXT register)
5118730f45dSAlex Elder  * to use big endian format.
5128730f45dSAlex Elder  *
5138730f45dSAlex Elder  * The packet size is written into the QMAP header's pkt_len field.  That
5148730f45dSAlex Elder  * location is defined here using the HDR_OFST_PKT_SIZE field.
5158730f45dSAlex Elder  *
5168730f45dSAlex Elder  * The mux_id comes from a 4-byte metadata value supplied with each packet
5178730f45dSAlex Elder  * by the modem.  It is *not* a QMAP header, but it does contain the mux_id
5188730f45dSAlex Elder  * value that we want, in its low-order byte.  A bitmask defined in the
5198730f45dSAlex Elder  * endpoint's METADATA_MASK register defines which byte within the modem
5208730f45dSAlex Elder  * metadata contains the mux_id.  And the OFST_METADATA field programmed
5218730f45dSAlex Elder  * here indicates where the extracted byte should be placed within the QMAP
5228730f45dSAlex Elder  * header.
5238730f45dSAlex Elder  */
52484f9bd12SAlex Elder static void ipa_endpoint_init_hdr(struct ipa_endpoint *endpoint)
52584f9bd12SAlex Elder {
52684f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_HDR_N_OFFSET(endpoint->endpoint_id);
5271af15c2aSAlex Elder 	struct ipa *ipa = endpoint->ipa;
52884f9bd12SAlex Elder 	u32 val = 0;
52984f9bd12SAlex Elder 
53084f9bd12SAlex Elder 	if (endpoint->data->qmap) {
5311af15c2aSAlex Elder 		enum ipa_version version = ipa->version;
5325567d4d9SAlex Elder 		size_t header_size;
53384f9bd12SAlex Elder 
5345567d4d9SAlex Elder 		header_size = ipa_qmap_header_size(version, endpoint);
5355567d4d9SAlex Elder 		val = ipa_header_size_encoded(version, header_size);
53684f9bd12SAlex Elder 
537f330fda3SAlex Elder 		/* Define how to fill fields in a received QMAP header */
5388730f45dSAlex Elder 		if (!endpoint->toward_ipa) {
5391af15c2aSAlex Elder 			u32 offset;	/* Field offset within header */
5408730f45dSAlex Elder 
5418730f45dSAlex Elder 			/* Where IPA will write the metadata value */
5421af15c2aSAlex Elder 			offset = offsetof(struct rmnet_map_header, mux_id);
5431af15c2aSAlex Elder 			val |= ipa_metadata_offset_encoded(version, offset);
5448730f45dSAlex Elder 
5458730f45dSAlex Elder 			/* Where IPA will write the length */
5461af15c2aSAlex Elder 			offset = offsetof(struct rmnet_map_header, pkt_len);
5471af15c2aSAlex Elder 			/* Upper bits are stored in HDR_EXT with IPA v4.5 */
548d7f3087bSAlex Elder 			if (version >= IPA_VERSION_4_5)
5491af15c2aSAlex Elder 				offset &= field_mask(HDR_OFST_PKT_SIZE_FMASK);
5501af15c2aSAlex Elder 
55184f9bd12SAlex Elder 			val |= HDR_OFST_PKT_SIZE_VALID_FMASK;
5521af15c2aSAlex Elder 			val |= u32_encode_bits(offset, HDR_OFST_PKT_SIZE_FMASK);
55384f9bd12SAlex Elder 		}
5548730f45dSAlex Elder 		/* For QMAP TX, metadata offset is 0 (modem assumes this) */
5558730f45dSAlex Elder 		val |= HDR_OFST_METADATA_VALID_FMASK;
5568730f45dSAlex Elder 
5578730f45dSAlex Elder 		/* HDR_ADDITIONAL_CONST_LEN is 0; (RX only) */
55884f9bd12SAlex Elder 		/* HDR_A5_MUX is 0 */
55984f9bd12SAlex Elder 		/* HDR_LEN_INC_DEAGG_HDR is 0 */
5608bfc4e21SAlex Elder 		/* HDR_METADATA_REG_VALID is 0 (TX only, version < v4.5) */
56184f9bd12SAlex Elder 	}
56284f9bd12SAlex Elder 
5631af15c2aSAlex Elder 	iowrite32(val, ipa->reg_virt + offset);
56484f9bd12SAlex Elder }
56584f9bd12SAlex Elder 
56684f9bd12SAlex Elder static void ipa_endpoint_init_hdr_ext(struct ipa_endpoint *endpoint)
56784f9bd12SAlex Elder {
56884f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_HDR_EXT_N_OFFSET(endpoint->endpoint_id);
56984f9bd12SAlex Elder 	u32 pad_align = endpoint->data->rx.pad_align;
5701af15c2aSAlex Elder 	struct ipa *ipa = endpoint->ipa;
57184f9bd12SAlex Elder 	u32 val = 0;
57284f9bd12SAlex Elder 
57384f9bd12SAlex Elder 	val |= HDR_ENDIANNESS_FMASK;		/* big endian */
574f330fda3SAlex Elder 
575f330fda3SAlex Elder 	/* A QMAP header contains a 6 bit pad field at offset 0.  The RMNet
576f330fda3SAlex Elder 	 * driver assumes this field is meaningful in packets it receives,
577f330fda3SAlex Elder 	 * and assumes the header's payload length includes that padding.
578f330fda3SAlex Elder 	 * The RMNet driver does *not* pad packets it sends, however, so
579f330fda3SAlex Elder 	 * the pad field (although 0) should be ignored.
580f330fda3SAlex Elder 	 */
581f330fda3SAlex Elder 	if (endpoint->data->qmap && !endpoint->toward_ipa) {
58284f9bd12SAlex Elder 		val |= HDR_TOTAL_LEN_OR_PAD_VALID_FMASK;
58384f9bd12SAlex Elder 		/* HDR_TOTAL_LEN_OR_PAD is 0 (pad, not total_len) */
584f330fda3SAlex Elder 		val |= HDR_PAYLOAD_LEN_INC_PADDING_FMASK;
58584f9bd12SAlex Elder 		/* HDR_TOTAL_LEN_OR_PAD_OFFSET is 0 */
586f330fda3SAlex Elder 	}
587f330fda3SAlex Elder 
588f330fda3SAlex Elder 	/* HDR_PAYLOAD_LEN_INC_PADDING is 0 */
58984f9bd12SAlex Elder 	if (!endpoint->toward_ipa)
59084f9bd12SAlex Elder 		val |= u32_encode_bits(pad_align, HDR_PAD_TO_ALIGNMENT_FMASK);
59184f9bd12SAlex Elder 
5921af15c2aSAlex Elder 	/* IPA v4.5 adds some most-significant bits to a few fields,
5931af15c2aSAlex Elder 	 * two of which are defined in the HDR (not HDR_EXT) register.
5941af15c2aSAlex Elder 	 */
595d7f3087bSAlex Elder 	if (ipa->version >= IPA_VERSION_4_5) {
5961af15c2aSAlex Elder 		/* HDR_TOTAL_LEN_OR_PAD_OFFSET is 0, so MSB is 0 */
5971af15c2aSAlex Elder 		if (endpoint->data->qmap && !endpoint->toward_ipa) {
5981af15c2aSAlex Elder 			u32 offset;
59984f9bd12SAlex Elder 
6001af15c2aSAlex Elder 			offset = offsetof(struct rmnet_map_header, pkt_len);
6011af15c2aSAlex Elder 			offset >>= hweight32(HDR_OFST_PKT_SIZE_FMASK);
6021af15c2aSAlex Elder 			val |= u32_encode_bits(offset,
6031af15c2aSAlex Elder 					       HDR_OFST_PKT_SIZE_MSB_FMASK);
6041af15c2aSAlex Elder 			/* HDR_ADDITIONAL_CONST_LEN is 0 so MSB is 0 */
6051af15c2aSAlex Elder 		}
6061af15c2aSAlex Elder 	}
6071af15c2aSAlex Elder 	iowrite32(val, ipa->reg_virt + offset);
6081af15c2aSAlex Elder }
60984f9bd12SAlex Elder 
61084f9bd12SAlex Elder static void ipa_endpoint_init_hdr_metadata_mask(struct ipa_endpoint *endpoint)
61184f9bd12SAlex Elder {
61284f9bd12SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
61384f9bd12SAlex Elder 	u32 val = 0;
61484f9bd12SAlex Elder 	u32 offset;
61584f9bd12SAlex Elder 
616fb57c3eaSAlex Elder 	if (endpoint->toward_ipa)
617fb57c3eaSAlex Elder 		return;		/* Register not valid for TX endpoints */
618fb57c3eaSAlex Elder 
61984f9bd12SAlex Elder 	offset = IPA_REG_ENDP_INIT_HDR_METADATA_MASK_N_OFFSET(endpoint_id);
62084f9bd12SAlex Elder 
6218730f45dSAlex Elder 	/* Note that HDR_ENDIANNESS indicates big endian header fields */
6229b63f093SAlex Elder 	if (endpoint->data->qmap)
623088f8a23SAlex Elder 		val = (__force u32)cpu_to_be32(IPA_ENDPOINT_QMAP_METADATA_MASK);
62484f9bd12SAlex Elder 
62584f9bd12SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
62684f9bd12SAlex Elder }
62784f9bd12SAlex Elder 
62884f9bd12SAlex Elder static void ipa_endpoint_init_mode(struct ipa_endpoint *endpoint)
62984f9bd12SAlex Elder {
63084f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_MODE_N_OFFSET(endpoint->endpoint_id);
63184f9bd12SAlex Elder 	u32 val;
63284f9bd12SAlex Elder 
633fb57c3eaSAlex Elder 	if (!endpoint->toward_ipa)
634fb57c3eaSAlex Elder 		return;		/* Register not valid for RX endpoints */
635fb57c3eaSAlex Elder 
63600b9102aSAlex Elder 	if (endpoint->data->dma_mode) {
63784f9bd12SAlex Elder 		enum ipa_endpoint_name name = endpoint->data->dma_endpoint;
63884f9bd12SAlex Elder 		u32 dma_endpoint_id;
63984f9bd12SAlex Elder 
64084f9bd12SAlex Elder 		dma_endpoint_id = endpoint->ipa->name_map[name]->endpoint_id;
64184f9bd12SAlex Elder 
64284f9bd12SAlex Elder 		val = u32_encode_bits(IPA_DMA, MODE_FMASK);
64384f9bd12SAlex Elder 		val |= u32_encode_bits(dma_endpoint_id, DEST_PIPE_INDEX_FMASK);
64484f9bd12SAlex Elder 	} else {
64584f9bd12SAlex Elder 		val = u32_encode_bits(IPA_BASIC, MODE_FMASK);
64684f9bd12SAlex Elder 	}
64700b9102aSAlex Elder 	/* All other bits unspecified (and 0) */
64884f9bd12SAlex Elder 
64984f9bd12SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
65084f9bd12SAlex Elder }
65184f9bd12SAlex Elder 
65284f9bd12SAlex Elder /* Compute the aggregation size value to use for a given buffer size */
65384f9bd12SAlex Elder static u32 ipa_aggr_size_kb(u32 rx_buffer_size)
65484f9bd12SAlex Elder {
65584f9bd12SAlex Elder 	/* We don't use "hard byte limit" aggregation, so we define the
65684f9bd12SAlex Elder 	 * aggregation limit such that our buffer has enough space *after*
65784f9bd12SAlex Elder 	 * that limit to receive a full MTU of data, plus overhead.
65884f9bd12SAlex Elder 	 */
65984f9bd12SAlex Elder 	rx_buffer_size -= IPA_MTU + IPA_RX_BUFFER_OVERHEAD;
66084f9bd12SAlex Elder 
66184f9bd12SAlex Elder 	return rx_buffer_size / SZ_1K;
66284f9bd12SAlex Elder }
66384f9bd12SAlex Elder 
6646bf754c7SAlex Elder /* Encoded values for AGGR endpoint register fields */
6656bf754c7SAlex Elder static u32 aggr_byte_limit_encoded(enum ipa_version version, u32 limit)
6666bf754c7SAlex Elder {
6676bf754c7SAlex Elder 	if (version < IPA_VERSION_4_5)
6686bf754c7SAlex Elder 		return u32_encode_bits(limit, aggr_byte_limit_fmask(true));
6696bf754c7SAlex Elder 
6706bf754c7SAlex Elder 	return u32_encode_bits(limit, aggr_byte_limit_fmask(false));
6716bf754c7SAlex Elder }
6726bf754c7SAlex Elder 
67319547041SAlex Elder /* Encode the aggregation timer limit (microseconds) based on IPA version */
6746bf754c7SAlex Elder static u32 aggr_time_limit_encoded(enum ipa_version version, u32 limit)
6756bf754c7SAlex Elder {
67619547041SAlex Elder 	u32 gran_sel;
67719547041SAlex Elder 	u32 fmask;
67819547041SAlex Elder 	u32 val;
6796bf754c7SAlex Elder 
68019547041SAlex Elder 	if (version < IPA_VERSION_4_5) {
68119547041SAlex Elder 		/* We set aggregation granularity in ipa_hardware_config() */
68219547041SAlex Elder 		limit = DIV_ROUND_CLOSEST(limit, IPA_AGGR_GRANULARITY);
68319547041SAlex Elder 
68419547041SAlex Elder 		return u32_encode_bits(limit, aggr_time_limit_fmask(true));
68519547041SAlex Elder 	}
68619547041SAlex Elder 
68719547041SAlex Elder 	/* IPA v4.5 expresses the time limit using Qtime.  The AP has
68819547041SAlex Elder 	 * pulse generators 0 and 1 available, which were configured
68919547041SAlex Elder 	 * in ipa_qtime_config() to have granularity 100 usec and
69019547041SAlex Elder 	 * 1 msec, respectively.  Use pulse generator 0 if possible,
69119547041SAlex Elder 	 * otherwise fall back to pulse generator 1.
69219547041SAlex Elder 	 */
69319547041SAlex Elder 	fmask = aggr_time_limit_fmask(false);
69419547041SAlex Elder 	val = DIV_ROUND_CLOSEST(limit, 100);
69519547041SAlex Elder 	if (val > field_max(fmask)) {
69619547041SAlex Elder 		/* Have to use pulse generator 1 (millisecond granularity) */
69719547041SAlex Elder 		gran_sel = AGGR_GRAN_SEL_FMASK;
69819547041SAlex Elder 		val = DIV_ROUND_CLOSEST(limit, 1000);
69919547041SAlex Elder 	} else {
70019547041SAlex Elder 		/* We can use pulse generator 0 (100 usec granularity) */
70119547041SAlex Elder 		gran_sel = 0;
70219547041SAlex Elder 	}
70319547041SAlex Elder 
70419547041SAlex Elder 	return gran_sel | u32_encode_bits(val, fmask);
7056bf754c7SAlex Elder }
7066bf754c7SAlex Elder 
7076bf754c7SAlex Elder static u32 aggr_sw_eof_active_encoded(enum ipa_version version, bool enabled)
7086bf754c7SAlex Elder {
7096bf754c7SAlex Elder 	u32 val = enabled ? 1 : 0;
7106bf754c7SAlex Elder 
7116bf754c7SAlex Elder 	if (version < IPA_VERSION_4_5)
7126bf754c7SAlex Elder 		return u32_encode_bits(val, aggr_sw_eof_active_fmask(true));
7136bf754c7SAlex Elder 
7146bf754c7SAlex Elder 	return u32_encode_bits(val, aggr_sw_eof_active_fmask(false));
7156bf754c7SAlex Elder }
7166bf754c7SAlex Elder 
71784f9bd12SAlex Elder static void ipa_endpoint_init_aggr(struct ipa_endpoint *endpoint)
71884f9bd12SAlex Elder {
71984f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_AGGR_N_OFFSET(endpoint->endpoint_id);
7206bf754c7SAlex Elder 	enum ipa_version version = endpoint->ipa->version;
72184f9bd12SAlex Elder 	u32 val = 0;
72284f9bd12SAlex Elder 
72384f9bd12SAlex Elder 	if (endpoint->data->aggregation) {
72484f9bd12SAlex Elder 		if (!endpoint->toward_ipa) {
7256bf754c7SAlex Elder 			bool close_eof;
72684f9bd12SAlex Elder 			u32 limit;
72784f9bd12SAlex Elder 
72884f9bd12SAlex Elder 			val |= u32_encode_bits(IPA_ENABLE_AGGR, AGGR_EN_FMASK);
72984f9bd12SAlex Elder 			val |= u32_encode_bits(IPA_GENERIC, AGGR_TYPE_FMASK);
7309e88cb5fSAlex Elder 
7319e88cb5fSAlex Elder 			limit = ipa_aggr_size_kb(IPA_RX_BUFFER_SIZE);
7326bf754c7SAlex Elder 			val |= aggr_byte_limit_encoded(version, limit);
7331d86652bSAlex Elder 
7346bf754c7SAlex Elder 			limit = IPA_AGGR_TIME_LIMIT;
7356bf754c7SAlex Elder 			val |= aggr_time_limit_encoded(version, limit);
7361d86652bSAlex Elder 
7379e88cb5fSAlex Elder 			/* AGGR_PKT_LIMIT is 0 (unlimited) */
7389e88cb5fSAlex Elder 
7396bf754c7SAlex Elder 			close_eof = endpoint->data->rx.aggr_close_eof;
7406bf754c7SAlex Elder 			val |= aggr_sw_eof_active_encoded(version, close_eof);
7416bf754c7SAlex Elder 
74284f9bd12SAlex Elder 			/* AGGR_HARD_BYTE_LIMIT_ENABLE is 0 */
74384f9bd12SAlex Elder 		} else {
74484f9bd12SAlex Elder 			val |= u32_encode_bits(IPA_ENABLE_DEAGGR,
74584f9bd12SAlex Elder 					       AGGR_EN_FMASK);
74684f9bd12SAlex Elder 			val |= u32_encode_bits(IPA_QCMAP, AGGR_TYPE_FMASK);
74784f9bd12SAlex Elder 			/* other fields ignored */
74884f9bd12SAlex Elder 		}
74984f9bd12SAlex Elder 		/* AGGR_FORCE_CLOSE is 0 */
7508bfc4e21SAlex Elder 		/* AGGR_GRAN_SEL is 0 for IPA v4.5 */
75184f9bd12SAlex Elder 	} else {
75284f9bd12SAlex Elder 		val |= u32_encode_bits(IPA_BYPASS_AGGR, AGGR_EN_FMASK);
75384f9bd12SAlex Elder 		/* other fields ignored */
75484f9bd12SAlex Elder 	}
75584f9bd12SAlex Elder 
75684f9bd12SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
75784f9bd12SAlex Elder }
75884f9bd12SAlex Elder 
75963e5afc8SAlex Elder /* Return the Qtime-based head-of-line blocking timer value that
76063e5afc8SAlex Elder  * represents the given number of microseconds.  The result
76163e5afc8SAlex Elder  * includes both the timer value and the selected timer granularity.
762f13a8c31SAlex Elder  */
76363e5afc8SAlex Elder static u32 hol_block_timer_qtime_val(struct ipa *ipa, u32 microseconds)
76463e5afc8SAlex Elder {
76563e5afc8SAlex Elder 	u32 gran_sel;
76663e5afc8SAlex Elder 	u32 val;
76763e5afc8SAlex Elder 
76863e5afc8SAlex Elder 	/* IPA v4.5 expresses time limits using Qtime.  The AP has
76963e5afc8SAlex Elder 	 * pulse generators 0 and 1 available, which were configured
77063e5afc8SAlex Elder 	 * in ipa_qtime_config() to have granularity 100 usec and
77163e5afc8SAlex Elder 	 * 1 msec, respectively.  Use pulse generator 0 if possible,
77263e5afc8SAlex Elder 	 * otherwise fall back to pulse generator 1.
77363e5afc8SAlex Elder 	 */
77463e5afc8SAlex Elder 	val = DIV_ROUND_CLOSEST(microseconds, 100);
77563e5afc8SAlex Elder 	if (val > field_max(TIME_LIMIT_FMASK)) {
77663e5afc8SAlex Elder 		/* Have to use pulse generator 1 (millisecond granularity) */
77763e5afc8SAlex Elder 		gran_sel = GRAN_SEL_FMASK;
77863e5afc8SAlex Elder 		val = DIV_ROUND_CLOSEST(microseconds, 1000);
77963e5afc8SAlex Elder 	} else {
78063e5afc8SAlex Elder 		/* We can use pulse generator 0 (100 usec granularity) */
78163e5afc8SAlex Elder 		gran_sel = 0;
78263e5afc8SAlex Elder 	}
78363e5afc8SAlex Elder 
78463e5afc8SAlex Elder 	return gran_sel | u32_encode_bits(val, TIME_LIMIT_FMASK);
78563e5afc8SAlex Elder }
78663e5afc8SAlex Elder 
78763e5afc8SAlex Elder /* The head-of-line blocking timer is defined as a tick count.  For
78863e5afc8SAlex Elder  * IPA version 4.5 the tick count is based on the Qtimer, which is
78963e5afc8SAlex Elder  * derived from the 19.2 MHz SoC XO clock.  For older IPA versions
79063e5afc8SAlex Elder  * each tick represents 128 cycles of the IPA core clock.
79163e5afc8SAlex Elder  *
79263e5afc8SAlex Elder  * Return the encoded value that should be written to that register
79363e5afc8SAlex Elder  * that represents the timeout period provided.  For IPA v4.2 this
79463e5afc8SAlex Elder  * encodes a base and scale value, while for earlier versions the
79563e5afc8SAlex Elder  * value is a simple tick count.
79663e5afc8SAlex Elder  */
79763e5afc8SAlex Elder static u32 hol_block_timer_val(struct ipa *ipa, u32 microseconds)
79884f9bd12SAlex Elder {
799f13a8c31SAlex Elder 	u32 width;
80084f9bd12SAlex Elder 	u32 scale;
801f13a8c31SAlex Elder 	u64 ticks;
802f13a8c31SAlex Elder 	u64 rate;
803f13a8c31SAlex Elder 	u32 high;
80484f9bd12SAlex Elder 	u32 val;
80584f9bd12SAlex Elder 
80684f9bd12SAlex Elder 	if (!microseconds)
807f13a8c31SAlex Elder 		return 0;	/* Nothing to compute if timer period is 0 */
80884f9bd12SAlex Elder 
809d7f3087bSAlex Elder 	if (ipa->version >= IPA_VERSION_4_5)
81063e5afc8SAlex Elder 		return hol_block_timer_qtime_val(ipa, microseconds);
81163e5afc8SAlex Elder 
812f13a8c31SAlex Elder 	/* Use 64 bit arithmetic to avoid overflow... */
8137aa0e8b8SAlex Elder 	rate = ipa_core_clock_rate(ipa);
814f13a8c31SAlex Elder 	ticks = DIV_ROUND_CLOSEST(microseconds * rate, 128 * USEC_PER_SEC);
815f13a8c31SAlex Elder 	/* ...but we still need to fit into a 32-bit register */
816f13a8c31SAlex Elder 	WARN_ON(ticks > U32_MAX);
81784f9bd12SAlex Elder 
8186833a096SAlex Elder 	/* IPA v3.5.1 through v4.1 just record the tick count */
8196833a096SAlex Elder 	if (ipa->version < IPA_VERSION_4_2)
820f13a8c31SAlex Elder 		return (u32)ticks;
82184f9bd12SAlex Elder 
822f13a8c31SAlex Elder 	/* For IPA v4.2, the tick count is represented by base and
823f13a8c31SAlex Elder 	 * scale fields within the 32-bit timer register, where:
824f13a8c31SAlex Elder 	 *     ticks = base << scale;
825f13a8c31SAlex Elder 	 * The best precision is achieved when the base value is as
826f13a8c31SAlex Elder 	 * large as possible.  Find the highest set bit in the tick
827f13a8c31SAlex Elder 	 * count, and extract the number of bits in the base field
828497abc87SPeng Li 	 * such that high bit is included.
829f13a8c31SAlex Elder 	 */
830f13a8c31SAlex Elder 	high = fls(ticks);		/* 1..32 */
831f13a8c31SAlex Elder 	width = HWEIGHT32(BASE_VALUE_FMASK);
832f13a8c31SAlex Elder 	scale = high > width ? high - width : 0;
833f13a8c31SAlex Elder 	if (scale) {
834f13a8c31SAlex Elder 		/* If we're scaling, round up to get a closer result */
835f13a8c31SAlex Elder 		ticks += 1 << (scale - 1);
836f13a8c31SAlex Elder 		/* High bit was set, so rounding might have affected it */
837f13a8c31SAlex Elder 		if (fls(ticks) != high)
838f13a8c31SAlex Elder 			scale++;
839f13a8c31SAlex Elder 	}
84084f9bd12SAlex Elder 
84184f9bd12SAlex Elder 	val = u32_encode_bits(scale, SCALE_FMASK);
842f13a8c31SAlex Elder 	val |= u32_encode_bits(ticks >> scale, BASE_VALUE_FMASK);
84384f9bd12SAlex Elder 
84484f9bd12SAlex Elder 	return val;
84584f9bd12SAlex Elder }
84684f9bd12SAlex Elder 
847f13a8c31SAlex Elder /* If microseconds is 0, timeout is immediate */
848f13a8c31SAlex Elder static void ipa_endpoint_init_hol_block_timer(struct ipa_endpoint *endpoint,
84984f9bd12SAlex Elder 					      u32 microseconds)
85084f9bd12SAlex Elder {
85184f9bd12SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
85284f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
85384f9bd12SAlex Elder 	u32 offset;
85484f9bd12SAlex Elder 	u32 val;
85584f9bd12SAlex Elder 
856816316caSAlex Elder 	/* This should only be changed when HOL_BLOCK_EN is disabled */
85784f9bd12SAlex Elder 	offset = IPA_REG_ENDP_INIT_HOL_BLOCK_TIMER_N_OFFSET(endpoint_id);
85863e5afc8SAlex Elder 	val = hol_block_timer_val(ipa, microseconds);
85984f9bd12SAlex Elder 	iowrite32(val, ipa->reg_virt + offset);
86084f9bd12SAlex Elder }
86184f9bd12SAlex Elder 
86284f9bd12SAlex Elder static void
863e6aab6b9SAlex Elder ipa_endpoint_init_hol_block_en(struct ipa_endpoint *endpoint, bool enable)
86484f9bd12SAlex Elder {
86584f9bd12SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
86684f9bd12SAlex Elder 	u32 offset;
86784f9bd12SAlex Elder 	u32 val;
86884f9bd12SAlex Elder 
869547c8788SAlex Elder 	val = enable ? HOL_BLOCK_EN_FMASK : 0;
87084f9bd12SAlex Elder 	offset = IPA_REG_ENDP_INIT_HOL_BLOCK_EN_N_OFFSET(endpoint_id);
87184f9bd12SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
8726e228d8cSAlex Elder 	/* When enabling, the register must be written twice for IPA v4.5+ */
8736e228d8cSAlex Elder 	if (enable && endpoint->ipa->version >= IPA_VERSION_4_5)
8746e228d8cSAlex Elder 		iowrite32(val, endpoint->ipa->reg_virt + offset);
87584f9bd12SAlex Elder }
87684f9bd12SAlex Elder 
877e6aab6b9SAlex Elder /* Assumes HOL_BLOCK is in disabled state */
878e6aab6b9SAlex Elder static void ipa_endpoint_init_hol_block_enable(struct ipa_endpoint *endpoint,
879e6aab6b9SAlex Elder 					       u32 microseconds)
880e6aab6b9SAlex Elder {
881e6aab6b9SAlex Elder 	ipa_endpoint_init_hol_block_timer(endpoint, microseconds);
882e6aab6b9SAlex Elder 	ipa_endpoint_init_hol_block_en(endpoint, true);
883e6aab6b9SAlex Elder }
884e6aab6b9SAlex Elder 
885e6aab6b9SAlex Elder static void ipa_endpoint_init_hol_block_disable(struct ipa_endpoint *endpoint)
886e6aab6b9SAlex Elder {
887e6aab6b9SAlex Elder 	ipa_endpoint_init_hol_block_en(endpoint, false);
888e6aab6b9SAlex Elder }
889e6aab6b9SAlex Elder 
89084f9bd12SAlex Elder void ipa_endpoint_modem_hol_block_clear_all(struct ipa *ipa)
89184f9bd12SAlex Elder {
89284f9bd12SAlex Elder 	u32 i;
89384f9bd12SAlex Elder 
89484f9bd12SAlex Elder 	for (i = 0; i < IPA_ENDPOINT_MAX; i++) {
89584f9bd12SAlex Elder 		struct ipa_endpoint *endpoint = &ipa->endpoint[i];
89684f9bd12SAlex Elder 
897f8d34dfdSAlex Elder 		if (endpoint->toward_ipa || endpoint->ee_id != GSI_EE_MODEM)
89884f9bd12SAlex Elder 			continue;
89984f9bd12SAlex Elder 
900e6aab6b9SAlex Elder 		ipa_endpoint_init_hol_block_disable(endpoint);
901e6aab6b9SAlex Elder 		ipa_endpoint_init_hol_block_enable(endpoint, 0);
90284f9bd12SAlex Elder 	}
90384f9bd12SAlex Elder }
90484f9bd12SAlex Elder 
90584f9bd12SAlex Elder static void ipa_endpoint_init_deaggr(struct ipa_endpoint *endpoint)
90684f9bd12SAlex Elder {
90784f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_DEAGGR_N_OFFSET(endpoint->endpoint_id);
90884f9bd12SAlex Elder 	u32 val = 0;
90984f9bd12SAlex Elder 
910fb57c3eaSAlex Elder 	if (!endpoint->toward_ipa)
911fb57c3eaSAlex Elder 		return;		/* Register not valid for RX endpoints */
912fb57c3eaSAlex Elder 
91384f9bd12SAlex Elder 	/* DEAGGR_HDR_LEN is 0 */
91484f9bd12SAlex Elder 	/* PACKET_OFFSET_VALID is 0 */
91584f9bd12SAlex Elder 	/* PACKET_OFFSET_LOCATION is ignored (not valid) */
91684f9bd12SAlex Elder 	/* MAX_PACKET_LEN is 0 (not enforced) */
91784f9bd12SAlex Elder 
91884f9bd12SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
91984f9bd12SAlex Elder }
92084f9bd12SAlex Elder 
9212d265342SAlex Elder static void ipa_endpoint_init_rsrc_grp(struct ipa_endpoint *endpoint)
9222d265342SAlex Elder {
9232d265342SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_RSRC_GRP_N_OFFSET(endpoint->endpoint_id);
9242d265342SAlex Elder 	struct ipa *ipa = endpoint->ipa;
9252d265342SAlex Elder 	u32 val;
9262d265342SAlex Elder 
9272d265342SAlex Elder 	val = rsrc_grp_encoded(ipa->version, endpoint->data->resource_group);
9282d265342SAlex Elder 	iowrite32(val, ipa->reg_virt + offset);
9292d265342SAlex Elder }
9302d265342SAlex Elder 
93184f9bd12SAlex Elder static void ipa_endpoint_init_seq(struct ipa_endpoint *endpoint)
93284f9bd12SAlex Elder {
93384f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_SEQ_N_OFFSET(endpoint->endpoint_id);
93484f9bd12SAlex Elder 	u32 val = 0;
93584f9bd12SAlex Elder 
936fb57c3eaSAlex Elder 	if (!endpoint->toward_ipa)
937fb57c3eaSAlex Elder 		return;		/* Register not valid for RX endpoints */
938fb57c3eaSAlex Elder 
9398ee5df65SAlex Elder 	/* Low-order byte configures primary packet processing */
9401690d8a7SAlex Elder 	val |= u32_encode_bits(endpoint->data->tx.seq_type, SEQ_TYPE_FMASK);
9418ee5df65SAlex Elder 
9428ee5df65SAlex Elder 	/* Second byte configures replicated packet processing */
9431690d8a7SAlex Elder 	val |= u32_encode_bits(endpoint->data->tx.seq_rep_type,
9441690d8a7SAlex Elder 			       SEQ_REP_TYPE_FMASK);
94584f9bd12SAlex Elder 
94684f9bd12SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
94784f9bd12SAlex Elder }
94884f9bd12SAlex Elder 
94984f9bd12SAlex Elder /**
95084f9bd12SAlex Elder  * ipa_endpoint_skb_tx() - Transmit a socket buffer
95184f9bd12SAlex Elder  * @endpoint:	Endpoint pointer
95284f9bd12SAlex Elder  * @skb:	Socket buffer to send
95384f9bd12SAlex Elder  *
95484f9bd12SAlex Elder  * Returns:	0 if successful, or a negative error code
95584f9bd12SAlex Elder  */
95684f9bd12SAlex Elder int ipa_endpoint_skb_tx(struct ipa_endpoint *endpoint, struct sk_buff *skb)
95784f9bd12SAlex Elder {
95884f9bd12SAlex Elder 	struct gsi_trans *trans;
95984f9bd12SAlex Elder 	u32 nr_frags;
96084f9bd12SAlex Elder 	int ret;
96184f9bd12SAlex Elder 
96284f9bd12SAlex Elder 	/* Make sure source endpoint's TLV FIFO has enough entries to
96384f9bd12SAlex Elder 	 * hold the linear portion of the skb and all its fragments.
96484f9bd12SAlex Elder 	 * If not, see if we can linearize it before giving up.
96584f9bd12SAlex Elder 	 */
96684f9bd12SAlex Elder 	nr_frags = skb_shinfo(skb)->nr_frags;
96784f9bd12SAlex Elder 	if (1 + nr_frags > endpoint->trans_tre_max) {
96884f9bd12SAlex Elder 		if (skb_linearize(skb))
96984f9bd12SAlex Elder 			return -E2BIG;
97084f9bd12SAlex Elder 		nr_frags = 0;
97184f9bd12SAlex Elder 	}
97284f9bd12SAlex Elder 
97384f9bd12SAlex Elder 	trans = ipa_endpoint_trans_alloc(endpoint, 1 + nr_frags);
97484f9bd12SAlex Elder 	if (!trans)
97584f9bd12SAlex Elder 		return -EBUSY;
97684f9bd12SAlex Elder 
97784f9bd12SAlex Elder 	ret = gsi_trans_skb_add(trans, skb);
97884f9bd12SAlex Elder 	if (ret)
97984f9bd12SAlex Elder 		goto err_trans_free;
98084f9bd12SAlex Elder 	trans->data = skb;	/* transaction owns skb now */
98184f9bd12SAlex Elder 
98284f9bd12SAlex Elder 	gsi_trans_commit(trans, !netdev_xmit_more());
98384f9bd12SAlex Elder 
98484f9bd12SAlex Elder 	return 0;
98584f9bd12SAlex Elder 
98684f9bd12SAlex Elder err_trans_free:
98784f9bd12SAlex Elder 	gsi_trans_free(trans);
98884f9bd12SAlex Elder 
98984f9bd12SAlex Elder 	return -ENOMEM;
99084f9bd12SAlex Elder }
99184f9bd12SAlex Elder 
99284f9bd12SAlex Elder static void ipa_endpoint_status(struct ipa_endpoint *endpoint)
99384f9bd12SAlex Elder {
99484f9bd12SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
99584f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
99684f9bd12SAlex Elder 	u32 val = 0;
99784f9bd12SAlex Elder 	u32 offset;
99884f9bd12SAlex Elder 
99984f9bd12SAlex Elder 	offset = IPA_REG_ENDP_STATUS_N_OFFSET(endpoint_id);
100084f9bd12SAlex Elder 
100184f9bd12SAlex Elder 	if (endpoint->data->status_enable) {
100284f9bd12SAlex Elder 		val |= STATUS_EN_FMASK;
100384f9bd12SAlex Elder 		if (endpoint->toward_ipa) {
100484f9bd12SAlex Elder 			enum ipa_endpoint_name name;
100584f9bd12SAlex Elder 			u32 status_endpoint_id;
100684f9bd12SAlex Elder 
100784f9bd12SAlex Elder 			name = endpoint->data->tx.status_endpoint;
100884f9bd12SAlex Elder 			status_endpoint_id = ipa->name_map[name]->endpoint_id;
100984f9bd12SAlex Elder 
101084f9bd12SAlex Elder 			val |= u32_encode_bits(status_endpoint_id,
101184f9bd12SAlex Elder 					       STATUS_ENDP_FMASK);
101284f9bd12SAlex Elder 		}
10138bfc4e21SAlex Elder 		/* STATUS_LOCATION is 0, meaning status element precedes
10148bfc4e21SAlex Elder 		 * packet (not present for IPA v4.5)
10158bfc4e21SAlex Elder 		 */
10168bfc4e21SAlex Elder 		/* STATUS_PKT_SUPPRESS_FMASK is 0 (not present for v3.5.1) */
101784f9bd12SAlex Elder 	}
101884f9bd12SAlex Elder 
101984f9bd12SAlex Elder 	iowrite32(val, ipa->reg_virt + offset);
102084f9bd12SAlex Elder }
102184f9bd12SAlex Elder 
102284f9bd12SAlex Elder static int ipa_endpoint_replenish_one(struct ipa_endpoint *endpoint)
102384f9bd12SAlex Elder {
102484f9bd12SAlex Elder 	struct gsi_trans *trans;
102584f9bd12SAlex Elder 	bool doorbell = false;
102684f9bd12SAlex Elder 	struct page *page;
102784f9bd12SAlex Elder 	u32 offset;
102884f9bd12SAlex Elder 	u32 len;
102984f9bd12SAlex Elder 	int ret;
103084f9bd12SAlex Elder 
10316fcd4224SAlex Elder 	page = dev_alloc_pages(get_order(IPA_RX_BUFFER_SIZE));
103284f9bd12SAlex Elder 	if (!page)
103384f9bd12SAlex Elder 		return -ENOMEM;
103484f9bd12SAlex Elder 
103584f9bd12SAlex Elder 	trans = ipa_endpoint_trans_alloc(endpoint, 1);
103684f9bd12SAlex Elder 	if (!trans)
103784f9bd12SAlex Elder 		goto err_free_pages;
103884f9bd12SAlex Elder 
103984f9bd12SAlex Elder 	/* Offset the buffer to make space for skb headroom */
104084f9bd12SAlex Elder 	offset = NET_SKB_PAD;
104184f9bd12SAlex Elder 	len = IPA_RX_BUFFER_SIZE - offset;
104284f9bd12SAlex Elder 
104384f9bd12SAlex Elder 	ret = gsi_trans_page_add(trans, page, len, offset);
104484f9bd12SAlex Elder 	if (ret)
104584f9bd12SAlex Elder 		goto err_trans_free;
104684f9bd12SAlex Elder 	trans->data = page;	/* transaction owns page now */
104784f9bd12SAlex Elder 
104884f9bd12SAlex Elder 	if (++endpoint->replenish_ready == IPA_REPLENISH_BATCH) {
104984f9bd12SAlex Elder 		doorbell = true;
105084f9bd12SAlex Elder 		endpoint->replenish_ready = 0;
105184f9bd12SAlex Elder 	}
105284f9bd12SAlex Elder 
105384f9bd12SAlex Elder 	gsi_trans_commit(trans, doorbell);
105484f9bd12SAlex Elder 
105584f9bd12SAlex Elder 	return 0;
105684f9bd12SAlex Elder 
105784f9bd12SAlex Elder err_trans_free:
105884f9bd12SAlex Elder 	gsi_trans_free(trans);
105984f9bd12SAlex Elder err_free_pages:
10606fcd4224SAlex Elder 	__free_pages(page, get_order(IPA_RX_BUFFER_SIZE));
106184f9bd12SAlex Elder 
106284f9bd12SAlex Elder 	return -ENOMEM;
106384f9bd12SAlex Elder }
106484f9bd12SAlex Elder 
106584f9bd12SAlex Elder /**
10669af5ccf3SAlex Elder  * ipa_endpoint_replenish() - Replenish endpoint receive buffers
1067e3eea08eSAlex Elder  * @endpoint:	Endpoint to be replenished
10689af5ccf3SAlex Elder  * @add_one:	Whether this is replacing a just-consumed buffer
106984f9bd12SAlex Elder  *
10709af5ccf3SAlex Elder  * The IPA hardware can hold a fixed number of receive buffers for an RX
10719af5ccf3SAlex Elder  * endpoint, based on the number of entries in the underlying channel ring
10729af5ccf3SAlex Elder  * buffer.  If an endpoint's "backlog" is non-zero, it indicates how many
10739af5ccf3SAlex Elder  * more receive buffers can be supplied to the hardware.  Replenishing for
10749af5ccf3SAlex Elder  * an endpoint can be disabled, in which case requests to replenish a
10759af5ccf3SAlex Elder  * buffer are "saved", and transferred to the backlog once it is re-enabled
10769af5ccf3SAlex Elder  * again.
107784f9bd12SAlex Elder  */
10789af5ccf3SAlex Elder static void ipa_endpoint_replenish(struct ipa_endpoint *endpoint, bool add_one)
107984f9bd12SAlex Elder {
108084f9bd12SAlex Elder 	struct gsi *gsi;
108184f9bd12SAlex Elder 	u32 backlog;
108284f9bd12SAlex Elder 
108384f9bd12SAlex Elder 	if (!endpoint->replenish_enabled) {
10849af5ccf3SAlex Elder 		if (add_one)
10859af5ccf3SAlex Elder 			atomic_inc(&endpoint->replenish_saved);
108684f9bd12SAlex Elder 		return;
108784f9bd12SAlex Elder 	}
108884f9bd12SAlex Elder 
108984f9bd12SAlex Elder 	while (atomic_dec_not_zero(&endpoint->replenish_backlog))
109084f9bd12SAlex Elder 		if (ipa_endpoint_replenish_one(endpoint))
109184f9bd12SAlex Elder 			goto try_again_later;
10929af5ccf3SAlex Elder 	if (add_one)
10939af5ccf3SAlex Elder 		atomic_inc(&endpoint->replenish_backlog);
109484f9bd12SAlex Elder 
109584f9bd12SAlex Elder 	return;
109684f9bd12SAlex Elder 
109784f9bd12SAlex Elder try_again_later:
109884f9bd12SAlex Elder 	/* The last one didn't succeed, so fix the backlog */
109984f9bd12SAlex Elder 	backlog = atomic_inc_return(&endpoint->replenish_backlog);
110084f9bd12SAlex Elder 
11019af5ccf3SAlex Elder 	if (add_one)
11029af5ccf3SAlex Elder 		atomic_inc(&endpoint->replenish_backlog);
110384f9bd12SAlex Elder 
110484f9bd12SAlex Elder 	/* Whenever a receive buffer transaction completes we'll try to
110584f9bd12SAlex Elder 	 * replenish again.  It's unlikely, but if we fail to supply even
110684f9bd12SAlex Elder 	 * one buffer, nothing will trigger another replenish attempt.
110784f9bd12SAlex Elder 	 * Receive buffer transactions use one TRE, so schedule work to
110884f9bd12SAlex Elder 	 * try replenishing again if our backlog is *all* available TREs.
110984f9bd12SAlex Elder 	 */
111084f9bd12SAlex Elder 	gsi = &endpoint->ipa->gsi;
111184f9bd12SAlex Elder 	if (backlog == gsi_channel_tre_max(gsi, endpoint->channel_id))
111284f9bd12SAlex Elder 		schedule_delayed_work(&endpoint->replenish_work,
111384f9bd12SAlex Elder 				      msecs_to_jiffies(1));
111484f9bd12SAlex Elder }
111584f9bd12SAlex Elder 
111684f9bd12SAlex Elder static void ipa_endpoint_replenish_enable(struct ipa_endpoint *endpoint)
111784f9bd12SAlex Elder {
111884f9bd12SAlex Elder 	struct gsi *gsi = &endpoint->ipa->gsi;
111984f9bd12SAlex Elder 	u32 max_backlog;
112084f9bd12SAlex Elder 	u32 saved;
112184f9bd12SAlex Elder 
112284f9bd12SAlex Elder 	endpoint->replenish_enabled = true;
112384f9bd12SAlex Elder 	while ((saved = atomic_xchg(&endpoint->replenish_saved, 0)))
112484f9bd12SAlex Elder 		atomic_add(saved, &endpoint->replenish_backlog);
112584f9bd12SAlex Elder 
112684f9bd12SAlex Elder 	/* Start replenishing if hardware currently has no buffers */
112784f9bd12SAlex Elder 	max_backlog = gsi_channel_tre_max(gsi, endpoint->channel_id);
112884f9bd12SAlex Elder 	if (atomic_read(&endpoint->replenish_backlog) == max_backlog)
11299af5ccf3SAlex Elder 		ipa_endpoint_replenish(endpoint, false);
113084f9bd12SAlex Elder }
113184f9bd12SAlex Elder 
113284f9bd12SAlex Elder static void ipa_endpoint_replenish_disable(struct ipa_endpoint *endpoint)
113384f9bd12SAlex Elder {
113484f9bd12SAlex Elder 	u32 backlog;
113584f9bd12SAlex Elder 
113684f9bd12SAlex Elder 	endpoint->replenish_enabled = false;
113784f9bd12SAlex Elder 	while ((backlog = atomic_xchg(&endpoint->replenish_backlog, 0)))
113884f9bd12SAlex Elder 		atomic_add(backlog, &endpoint->replenish_saved);
113984f9bd12SAlex Elder }
114084f9bd12SAlex Elder 
114184f9bd12SAlex Elder static void ipa_endpoint_replenish_work(struct work_struct *work)
114284f9bd12SAlex Elder {
114384f9bd12SAlex Elder 	struct delayed_work *dwork = to_delayed_work(work);
114484f9bd12SAlex Elder 	struct ipa_endpoint *endpoint;
114584f9bd12SAlex Elder 
114684f9bd12SAlex Elder 	endpoint = container_of(dwork, struct ipa_endpoint, replenish_work);
114784f9bd12SAlex Elder 
11489af5ccf3SAlex Elder 	ipa_endpoint_replenish(endpoint, false);
114984f9bd12SAlex Elder }
115084f9bd12SAlex Elder 
115184f9bd12SAlex Elder static void ipa_endpoint_skb_copy(struct ipa_endpoint *endpoint,
115284f9bd12SAlex Elder 				  void *data, u32 len, u32 extra)
115384f9bd12SAlex Elder {
115484f9bd12SAlex Elder 	struct sk_buff *skb;
115584f9bd12SAlex Elder 
115684f9bd12SAlex Elder 	skb = __dev_alloc_skb(len, GFP_ATOMIC);
115784f9bd12SAlex Elder 	if (skb) {
115884f9bd12SAlex Elder 		skb_put(skb, len);
115984f9bd12SAlex Elder 		memcpy(skb->data, data, len);
116084f9bd12SAlex Elder 		skb->truesize += extra;
116184f9bd12SAlex Elder 	}
116284f9bd12SAlex Elder 
116384f9bd12SAlex Elder 	/* Now receive it, or drop it if there's no netdev */
116484f9bd12SAlex Elder 	if (endpoint->netdev)
116584f9bd12SAlex Elder 		ipa_modem_skb_rx(endpoint->netdev, skb);
116684f9bd12SAlex Elder 	else if (skb)
116784f9bd12SAlex Elder 		dev_kfree_skb_any(skb);
116884f9bd12SAlex Elder }
116984f9bd12SAlex Elder 
117084f9bd12SAlex Elder static bool ipa_endpoint_skb_build(struct ipa_endpoint *endpoint,
117184f9bd12SAlex Elder 				   struct page *page, u32 len)
117284f9bd12SAlex Elder {
117384f9bd12SAlex Elder 	struct sk_buff *skb;
117484f9bd12SAlex Elder 
117584f9bd12SAlex Elder 	/* Nothing to do if there's no netdev */
117684f9bd12SAlex Elder 	if (!endpoint->netdev)
117784f9bd12SAlex Elder 		return false;
117884f9bd12SAlex Elder 
11795bc55884SAlex Elder 	WARN_ON(len > SKB_WITH_OVERHEAD(IPA_RX_BUFFER_SIZE - NET_SKB_PAD));
11805bc55884SAlex Elder 
118184f9bd12SAlex Elder 	skb = build_skb(page_address(page), IPA_RX_BUFFER_SIZE);
118284f9bd12SAlex Elder 	if (skb) {
118384f9bd12SAlex Elder 		/* Reserve the headroom and account for the data */
118484f9bd12SAlex Elder 		skb_reserve(skb, NET_SKB_PAD);
118584f9bd12SAlex Elder 		skb_put(skb, len);
118684f9bd12SAlex Elder 	}
118784f9bd12SAlex Elder 
118884f9bd12SAlex Elder 	/* Receive the buffer (or record drop if unable to build it) */
118984f9bd12SAlex Elder 	ipa_modem_skb_rx(endpoint->netdev, skb);
119084f9bd12SAlex Elder 
119184f9bd12SAlex Elder 	return skb != NULL;
119284f9bd12SAlex Elder }
119384f9bd12SAlex Elder 
119484f9bd12SAlex Elder /* The format of a packet status element is the same for several status
119545921390SAlex Elder  * types (opcodes).  Other types aren't currently supported.
119684f9bd12SAlex Elder  */
119784f9bd12SAlex Elder static bool ipa_status_format_packet(enum ipa_status_opcode opcode)
119884f9bd12SAlex Elder {
119984f9bd12SAlex Elder 	switch (opcode) {
120084f9bd12SAlex Elder 	case IPA_STATUS_OPCODE_PACKET:
120184f9bd12SAlex Elder 	case IPA_STATUS_OPCODE_DROPPED_PACKET:
120284f9bd12SAlex Elder 	case IPA_STATUS_OPCODE_SUSPENDED_PACKET:
120384f9bd12SAlex Elder 	case IPA_STATUS_OPCODE_PACKET_2ND_PASS:
120484f9bd12SAlex Elder 		return true;
120584f9bd12SAlex Elder 	default:
120684f9bd12SAlex Elder 		return false;
120784f9bd12SAlex Elder 	}
120884f9bd12SAlex Elder }
120984f9bd12SAlex Elder 
121084f9bd12SAlex Elder static bool ipa_endpoint_status_skip(struct ipa_endpoint *endpoint,
121184f9bd12SAlex Elder 				     const struct ipa_status *status)
121284f9bd12SAlex Elder {
121384f9bd12SAlex Elder 	u32 endpoint_id;
121484f9bd12SAlex Elder 
121584f9bd12SAlex Elder 	if (!ipa_status_format_packet(status->opcode))
121684f9bd12SAlex Elder 		return true;
121784f9bd12SAlex Elder 	if (!status->pkt_len)
121884f9bd12SAlex Elder 		return true;
1219c13899f1SAlex Elder 	endpoint_id = u8_get_bits(status->endp_dst_idx,
122084f9bd12SAlex Elder 				  IPA_STATUS_DST_IDX_FMASK);
122184f9bd12SAlex Elder 	if (endpoint_id != endpoint->endpoint_id)
122284f9bd12SAlex Elder 		return true;
122384f9bd12SAlex Elder 
122484f9bd12SAlex Elder 	return false;	/* Don't skip this packet, process it */
122584f9bd12SAlex Elder }
122684f9bd12SAlex Elder 
1227f6aba7b5SAlex Elder static bool ipa_endpoint_status_tag(struct ipa_endpoint *endpoint,
1228f6aba7b5SAlex Elder 				    const struct ipa_status *status)
1229f6aba7b5SAlex Elder {
123051c48ce2SAlex Elder 	struct ipa_endpoint *command_endpoint;
123151c48ce2SAlex Elder 	struct ipa *ipa = endpoint->ipa;
123251c48ce2SAlex Elder 	u32 endpoint_id;
123351c48ce2SAlex Elder 
123451c48ce2SAlex Elder 	if (!le16_get_bits(status->mask, IPA_STATUS_MASK_TAG_VALID_FMASK))
123551c48ce2SAlex Elder 		return false;	/* No valid tag */
123651c48ce2SAlex Elder 
123751c48ce2SAlex Elder 	/* The status contains a valid tag.  We know the packet was sent to
123851c48ce2SAlex Elder 	 * this endpoint (already verified by ipa_endpoint_status_skip()).
123951c48ce2SAlex Elder 	 * If the packet came from the AP->command TX endpoint we know
124051c48ce2SAlex Elder 	 * this packet was sent as part of the pipeline clear process.
124151c48ce2SAlex Elder 	 */
124251c48ce2SAlex Elder 	endpoint_id = u8_get_bits(status->endp_src_idx,
124351c48ce2SAlex Elder 				  IPA_STATUS_SRC_IDX_FMASK);
124451c48ce2SAlex Elder 	command_endpoint = ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX];
124551c48ce2SAlex Elder 	if (endpoint_id == command_endpoint->endpoint_id) {
124651c48ce2SAlex Elder 		complete(&ipa->completion);
124751c48ce2SAlex Elder 	} else {
124851c48ce2SAlex Elder 		dev_err(&ipa->pdev->dev,
124951c48ce2SAlex Elder 			"unexpected tagged packet from endpoint %u\n",
125051c48ce2SAlex Elder 			endpoint_id);
125151c48ce2SAlex Elder 	}
125251c48ce2SAlex Elder 
125351c48ce2SAlex Elder 	return true;
1254f6aba7b5SAlex Elder }
1255f6aba7b5SAlex Elder 
125684f9bd12SAlex Elder /* Return whether the status indicates the packet should be dropped */
1257f6aba7b5SAlex Elder static bool ipa_endpoint_status_drop(struct ipa_endpoint *endpoint,
1258f6aba7b5SAlex Elder 				     const struct ipa_status *status)
125984f9bd12SAlex Elder {
126084f9bd12SAlex Elder 	u32 val;
126184f9bd12SAlex Elder 
1262f6aba7b5SAlex Elder 	/* If the status indicates a tagged transfer, we'll drop the packet */
1263f6aba7b5SAlex Elder 	if (ipa_endpoint_status_tag(endpoint, status))
1264f6aba7b5SAlex Elder 		return true;
1265f6aba7b5SAlex Elder 
1266ab4f71e5SAlex Elder 	/* Deaggregation exceptions we drop; all other types we consume */
126784f9bd12SAlex Elder 	if (status->exception)
126884f9bd12SAlex Elder 		return status->exception == IPA_STATUS_EXCEPTION_DEAGGR;
126984f9bd12SAlex Elder 
127084f9bd12SAlex Elder 	/* Drop the packet if it fails to match a routing rule; otherwise no */
127184f9bd12SAlex Elder 	val = le32_get_bits(status->flags1, IPA_STATUS_FLAGS1_RT_RULE_ID_FMASK);
127284f9bd12SAlex Elder 
127384f9bd12SAlex Elder 	return val == field_max(IPA_STATUS_FLAGS1_RT_RULE_ID_FMASK);
127484f9bd12SAlex Elder }
127584f9bd12SAlex Elder 
127684f9bd12SAlex Elder static void ipa_endpoint_status_parse(struct ipa_endpoint *endpoint,
127784f9bd12SAlex Elder 				      struct page *page, u32 total_len)
127884f9bd12SAlex Elder {
127984f9bd12SAlex Elder 	void *data = page_address(page) + NET_SKB_PAD;
128084f9bd12SAlex Elder 	u32 unused = IPA_RX_BUFFER_SIZE - total_len;
128184f9bd12SAlex Elder 	u32 resid = total_len;
128284f9bd12SAlex Elder 
128384f9bd12SAlex Elder 	while (resid) {
128484f9bd12SAlex Elder 		const struct ipa_status *status = data;
128584f9bd12SAlex Elder 		u32 align;
128684f9bd12SAlex Elder 		u32 len;
128784f9bd12SAlex Elder 
128884f9bd12SAlex Elder 		if (resid < sizeof(*status)) {
128984f9bd12SAlex Elder 			dev_err(&endpoint->ipa->pdev->dev,
129084f9bd12SAlex Elder 				"short message (%u bytes < %zu byte status)\n",
129184f9bd12SAlex Elder 				resid, sizeof(*status));
129284f9bd12SAlex Elder 			break;
129384f9bd12SAlex Elder 		}
129484f9bd12SAlex Elder 
129584f9bd12SAlex Elder 		/* Skip over status packets that lack packet data */
129684f9bd12SAlex Elder 		if (ipa_endpoint_status_skip(endpoint, status)) {
129784f9bd12SAlex Elder 			data += sizeof(*status);
129884f9bd12SAlex Elder 			resid -= sizeof(*status);
129984f9bd12SAlex Elder 			continue;
130084f9bd12SAlex Elder 		}
130184f9bd12SAlex Elder 
1302162fbc6fSAlex Elder 		/* Compute the amount of buffer space consumed by the packet,
1303162fbc6fSAlex Elder 		 * including the status element.  If the hardware is configured
1304162fbc6fSAlex Elder 		 * to pad packet data to an aligned boundary, account for that.
1305162fbc6fSAlex Elder 		 * And if checksum offload is enabled a trailer containing
1306162fbc6fSAlex Elder 		 * computed checksum information will be appended.
130784f9bd12SAlex Elder 		 */
130884f9bd12SAlex Elder 		align = endpoint->data->rx.pad_align ? : 1;
130984f9bd12SAlex Elder 		len = le16_to_cpu(status->pkt_len);
131084f9bd12SAlex Elder 		len = sizeof(*status) + ALIGN(len, align);
131184f9bd12SAlex Elder 		if (endpoint->data->checksum)
131284f9bd12SAlex Elder 			len += sizeof(struct rmnet_map_dl_csum_trailer);
131384f9bd12SAlex Elder 
1314f6aba7b5SAlex Elder 		if (!ipa_endpoint_status_drop(endpoint, status)) {
1315162fbc6fSAlex Elder 			void *data2;
1316162fbc6fSAlex Elder 			u32 extra;
1317162fbc6fSAlex Elder 			u32 len2;
131884f9bd12SAlex Elder 
131984f9bd12SAlex Elder 			/* Client receives only packet data (no status) */
1320162fbc6fSAlex Elder 			data2 = data + sizeof(*status);
1321162fbc6fSAlex Elder 			len2 = le16_to_cpu(status->pkt_len);
1322162fbc6fSAlex Elder 
1323162fbc6fSAlex Elder 			/* Have the true size reflect the extra unused space in
1324162fbc6fSAlex Elder 			 * the original receive buffer.  Distribute the "cost"
1325162fbc6fSAlex Elder 			 * proportionately across all aggregated packets in the
1326162fbc6fSAlex Elder 			 * buffer.
1327162fbc6fSAlex Elder 			 */
1328162fbc6fSAlex Elder 			extra = DIV_ROUND_CLOSEST(unused * len, total_len);
132984f9bd12SAlex Elder 			ipa_endpoint_skb_copy(endpoint, data2, len2, extra);
133084f9bd12SAlex Elder 		}
133184f9bd12SAlex Elder 
133284f9bd12SAlex Elder 		/* Consume status and the full packet it describes */
133384f9bd12SAlex Elder 		data += len;
133484f9bd12SAlex Elder 		resid -= len;
133584f9bd12SAlex Elder 	}
133684f9bd12SAlex Elder }
133784f9bd12SAlex Elder 
133884f9bd12SAlex Elder /* Complete a TX transaction, command or from ipa_endpoint_skb_tx() */
133984f9bd12SAlex Elder static void ipa_endpoint_tx_complete(struct ipa_endpoint *endpoint,
134084f9bd12SAlex Elder 				     struct gsi_trans *trans)
134184f9bd12SAlex Elder {
134284f9bd12SAlex Elder }
134384f9bd12SAlex Elder 
134484f9bd12SAlex Elder /* Complete transaction initiated in ipa_endpoint_replenish_one() */
134584f9bd12SAlex Elder static void ipa_endpoint_rx_complete(struct ipa_endpoint *endpoint,
134684f9bd12SAlex Elder 				     struct gsi_trans *trans)
134784f9bd12SAlex Elder {
134884f9bd12SAlex Elder 	struct page *page;
134984f9bd12SAlex Elder 
13509af5ccf3SAlex Elder 	ipa_endpoint_replenish(endpoint, true);
135184f9bd12SAlex Elder 
135284f9bd12SAlex Elder 	if (trans->cancelled)
135384f9bd12SAlex Elder 		return;
135484f9bd12SAlex Elder 
135584f9bd12SAlex Elder 	/* Parse or build a socket buffer using the actual received length */
135684f9bd12SAlex Elder 	page = trans->data;
135784f9bd12SAlex Elder 	if (endpoint->data->status_enable)
135884f9bd12SAlex Elder 		ipa_endpoint_status_parse(endpoint, page, trans->len);
135984f9bd12SAlex Elder 	else if (ipa_endpoint_skb_build(endpoint, page, trans->len))
136084f9bd12SAlex Elder 		trans->data = NULL;	/* Pages have been consumed */
136184f9bd12SAlex Elder }
136284f9bd12SAlex Elder 
136384f9bd12SAlex Elder void ipa_endpoint_trans_complete(struct ipa_endpoint *endpoint,
136484f9bd12SAlex Elder 				 struct gsi_trans *trans)
136584f9bd12SAlex Elder {
136684f9bd12SAlex Elder 	if (endpoint->toward_ipa)
136784f9bd12SAlex Elder 		ipa_endpoint_tx_complete(endpoint, trans);
136884f9bd12SAlex Elder 	else
136984f9bd12SAlex Elder 		ipa_endpoint_rx_complete(endpoint, trans);
137084f9bd12SAlex Elder }
137184f9bd12SAlex Elder 
137284f9bd12SAlex Elder void ipa_endpoint_trans_release(struct ipa_endpoint *endpoint,
137384f9bd12SAlex Elder 				struct gsi_trans *trans)
137484f9bd12SAlex Elder {
137584f9bd12SAlex Elder 	if (endpoint->toward_ipa) {
137684f9bd12SAlex Elder 		struct ipa *ipa = endpoint->ipa;
137784f9bd12SAlex Elder 
137884f9bd12SAlex Elder 		/* Nothing to do for command transactions */
137984f9bd12SAlex Elder 		if (endpoint != ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]) {
138084f9bd12SAlex Elder 			struct sk_buff *skb = trans->data;
138184f9bd12SAlex Elder 
138284f9bd12SAlex Elder 			if (skb)
138384f9bd12SAlex Elder 				dev_kfree_skb_any(skb);
138484f9bd12SAlex Elder 		}
138584f9bd12SAlex Elder 	} else {
138684f9bd12SAlex Elder 		struct page *page = trans->data;
138784f9bd12SAlex Elder 
138884f9bd12SAlex Elder 		if (page)
13896fcd4224SAlex Elder 			__free_pages(page, get_order(IPA_RX_BUFFER_SIZE));
139084f9bd12SAlex Elder 	}
139184f9bd12SAlex Elder }
139284f9bd12SAlex Elder 
139384f9bd12SAlex Elder void ipa_endpoint_default_route_set(struct ipa *ipa, u32 endpoint_id)
139484f9bd12SAlex Elder {
139584f9bd12SAlex Elder 	u32 val;
139684f9bd12SAlex Elder 
139784f9bd12SAlex Elder 	/* ROUTE_DIS is 0 */
139884f9bd12SAlex Elder 	val = u32_encode_bits(endpoint_id, ROUTE_DEF_PIPE_FMASK);
139984f9bd12SAlex Elder 	val |= ROUTE_DEF_HDR_TABLE_FMASK;
140084f9bd12SAlex Elder 	val |= u32_encode_bits(0, ROUTE_DEF_HDR_OFST_FMASK);
140184f9bd12SAlex Elder 	val |= u32_encode_bits(endpoint_id, ROUTE_FRAG_DEF_PIPE_FMASK);
140284f9bd12SAlex Elder 	val |= ROUTE_DEF_RETAIN_HDR_FMASK;
140384f9bd12SAlex Elder 
140484f9bd12SAlex Elder 	iowrite32(val, ipa->reg_virt + IPA_REG_ROUTE_OFFSET);
140584f9bd12SAlex Elder }
140684f9bd12SAlex Elder 
140784f9bd12SAlex Elder void ipa_endpoint_default_route_clear(struct ipa *ipa)
140884f9bd12SAlex Elder {
140984f9bd12SAlex Elder 	ipa_endpoint_default_route_set(ipa, 0);
141084f9bd12SAlex Elder }
141184f9bd12SAlex Elder 
141284f9bd12SAlex Elder /**
141384f9bd12SAlex Elder  * ipa_endpoint_reset_rx_aggr() - Reset RX endpoint with aggregation active
141484f9bd12SAlex Elder  * @endpoint:	Endpoint to be reset
141584f9bd12SAlex Elder  *
141684f9bd12SAlex Elder  * If aggregation is active on an RX endpoint when a reset is performed
141784f9bd12SAlex Elder  * on its underlying GSI channel, a special sequence of actions must be
141884f9bd12SAlex Elder  * taken to ensure the IPA pipeline is properly cleared.
141984f9bd12SAlex Elder  *
1420e3eea08eSAlex Elder  * Return:	0 if successful, or a negative error code
142184f9bd12SAlex Elder  */
142284f9bd12SAlex Elder static int ipa_endpoint_reset_rx_aggr(struct ipa_endpoint *endpoint)
142384f9bd12SAlex Elder {
142484f9bd12SAlex Elder 	struct device *dev = &endpoint->ipa->pdev->dev;
142584f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
142684f9bd12SAlex Elder 	struct gsi *gsi = &ipa->gsi;
14274fa95248SAlex Elder 	bool suspended = false;
142884f9bd12SAlex Elder 	dma_addr_t addr;
142984f9bd12SAlex Elder 	u32 retries;
143084f9bd12SAlex Elder 	u32 len = 1;
143184f9bd12SAlex Elder 	void *virt;
143284f9bd12SAlex Elder 	int ret;
143384f9bd12SAlex Elder 
143484f9bd12SAlex Elder 	virt = kzalloc(len, GFP_KERNEL);
143584f9bd12SAlex Elder 	if (!virt)
143684f9bd12SAlex Elder 		return -ENOMEM;
143784f9bd12SAlex Elder 
143884f9bd12SAlex Elder 	addr = dma_map_single(dev, virt, len, DMA_FROM_DEVICE);
143984f9bd12SAlex Elder 	if (dma_mapping_error(dev, addr)) {
144084f9bd12SAlex Elder 		ret = -ENOMEM;
144184f9bd12SAlex Elder 		goto out_kfree;
144284f9bd12SAlex Elder 	}
144384f9bd12SAlex Elder 
144484f9bd12SAlex Elder 	/* Force close aggregation before issuing the reset */
144584f9bd12SAlex Elder 	ipa_endpoint_force_close(endpoint);
144684f9bd12SAlex Elder 
144784f9bd12SAlex Elder 	/* Reset and reconfigure the channel with the doorbell engine
144884f9bd12SAlex Elder 	 * disabled.  Then poll until we know aggregation is no longer
144984f9bd12SAlex Elder 	 * active.  We'll re-enable the doorbell (if appropriate) when
145084f9bd12SAlex Elder 	 * we reset again below.
145184f9bd12SAlex Elder 	 */
145284f9bd12SAlex Elder 	gsi_channel_reset(gsi, endpoint->channel_id, false);
145384f9bd12SAlex Elder 
145484f9bd12SAlex Elder 	/* Make sure the channel isn't suspended */
14554fa95248SAlex Elder 	suspended = ipa_endpoint_program_suspend(endpoint, false);
145684f9bd12SAlex Elder 
145784f9bd12SAlex Elder 	/* Start channel and do a 1 byte read */
145884f9bd12SAlex Elder 	ret = gsi_channel_start(gsi, endpoint->channel_id);
145984f9bd12SAlex Elder 	if (ret)
146084f9bd12SAlex Elder 		goto out_suspend_again;
146184f9bd12SAlex Elder 
146284f9bd12SAlex Elder 	ret = gsi_trans_read_byte(gsi, endpoint->channel_id, addr);
146384f9bd12SAlex Elder 	if (ret)
146484f9bd12SAlex Elder 		goto err_endpoint_stop;
146584f9bd12SAlex Elder 
146684f9bd12SAlex Elder 	/* Wait for aggregation to be closed on the channel */
146784f9bd12SAlex Elder 	retries = IPA_ENDPOINT_RESET_AGGR_RETRY_MAX;
146884f9bd12SAlex Elder 	do {
146984f9bd12SAlex Elder 		if (!ipa_endpoint_aggr_active(endpoint))
147084f9bd12SAlex Elder 			break;
147174401946SAlex Elder 		usleep_range(USEC_PER_MSEC, 2 * USEC_PER_MSEC);
147284f9bd12SAlex Elder 	} while (retries--);
147384f9bd12SAlex Elder 
147484f9bd12SAlex Elder 	/* Check one last time */
147584f9bd12SAlex Elder 	if (ipa_endpoint_aggr_active(endpoint))
147684f9bd12SAlex Elder 		dev_err(dev, "endpoint %u still active during reset\n",
147784f9bd12SAlex Elder 			endpoint->endpoint_id);
147884f9bd12SAlex Elder 
147984f9bd12SAlex Elder 	gsi_trans_read_byte_done(gsi, endpoint->channel_id);
148084f9bd12SAlex Elder 
1481f30dcb7dSAlex Elder 	ret = gsi_channel_stop(gsi, endpoint->channel_id);
148284f9bd12SAlex Elder 	if (ret)
148384f9bd12SAlex Elder 		goto out_suspend_again;
148484f9bd12SAlex Elder 
1485497abc87SPeng Li 	/* Finally, reset and reconfigure the channel again (re-enabling
148684f9bd12SAlex Elder 	 * the doorbell engine if appropriate).  Sleep for 1 millisecond to
148784f9bd12SAlex Elder 	 * complete the channel reset sequence.  Finish by suspending the
148884f9bd12SAlex Elder 	 * channel again (if necessary).
148984f9bd12SAlex Elder 	 */
1490ce54993dSAlex Elder 	gsi_channel_reset(gsi, endpoint->channel_id, true);
149184f9bd12SAlex Elder 
149274401946SAlex Elder 	usleep_range(USEC_PER_MSEC, 2 * USEC_PER_MSEC);
149384f9bd12SAlex Elder 
149484f9bd12SAlex Elder 	goto out_suspend_again;
149584f9bd12SAlex Elder 
149684f9bd12SAlex Elder err_endpoint_stop:
1497f30dcb7dSAlex Elder 	(void)gsi_channel_stop(gsi, endpoint->channel_id);
149884f9bd12SAlex Elder out_suspend_again:
14994fa95248SAlex Elder 	if (suspended)
15004fa95248SAlex Elder 		(void)ipa_endpoint_program_suspend(endpoint, true);
150184f9bd12SAlex Elder 	dma_unmap_single(dev, addr, len, DMA_FROM_DEVICE);
150284f9bd12SAlex Elder out_kfree:
150384f9bd12SAlex Elder 	kfree(virt);
150484f9bd12SAlex Elder 
150584f9bd12SAlex Elder 	return ret;
150684f9bd12SAlex Elder }
150784f9bd12SAlex Elder 
150884f9bd12SAlex Elder static void ipa_endpoint_reset(struct ipa_endpoint *endpoint)
150984f9bd12SAlex Elder {
151084f9bd12SAlex Elder 	u32 channel_id = endpoint->channel_id;
151184f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
151284f9bd12SAlex Elder 	bool special;
151384f9bd12SAlex Elder 	int ret = 0;
151484f9bd12SAlex Elder 
151584f9bd12SAlex Elder 	/* On IPA v3.5.1, if an RX endpoint is reset while aggregation
151684f9bd12SAlex Elder 	 * is active, we need to handle things specially to recover.
151784f9bd12SAlex Elder 	 * All other cases just need to reset the underlying GSI channel.
151884f9bd12SAlex Elder 	 */
1519d7f3087bSAlex Elder 	special = ipa->version < IPA_VERSION_4_0 && !endpoint->toward_ipa &&
1520ce54993dSAlex Elder 			endpoint->data->aggregation;
1521ce54993dSAlex Elder 	if (special && ipa_endpoint_aggr_active(endpoint))
152284f9bd12SAlex Elder 		ret = ipa_endpoint_reset_rx_aggr(endpoint);
152384f9bd12SAlex Elder 	else
1524ce54993dSAlex Elder 		gsi_channel_reset(&ipa->gsi, channel_id, true);
152584f9bd12SAlex Elder 
152684f9bd12SAlex Elder 	if (ret)
152784f9bd12SAlex Elder 		dev_err(&ipa->pdev->dev,
152884f9bd12SAlex Elder 			"error %d resetting channel %u for endpoint %u\n",
152984f9bd12SAlex Elder 			ret, endpoint->channel_id, endpoint->endpoint_id);
153084f9bd12SAlex Elder }
153184f9bd12SAlex Elder 
153284f9bd12SAlex Elder static void ipa_endpoint_program(struct ipa_endpoint *endpoint)
153384f9bd12SAlex Elder {
1534fb57c3eaSAlex Elder 	if (endpoint->toward_ipa)
1535a4dcad34SAlex Elder 		ipa_endpoint_program_delay(endpoint, false);
1536fb57c3eaSAlex Elder 	else
1537fb57c3eaSAlex Elder 		(void)ipa_endpoint_program_suspend(endpoint, false);
1538fb57c3eaSAlex Elder 	ipa_endpoint_init_cfg(endpoint);
1539647a05f3SAlex Elder 	ipa_endpoint_init_nat(endpoint);
1540fb57c3eaSAlex Elder 	ipa_endpoint_init_hdr(endpoint);
154184f9bd12SAlex Elder 	ipa_endpoint_init_hdr_ext(endpoint);
1542fb57c3eaSAlex Elder 	ipa_endpoint_init_hdr_metadata_mask(endpoint);
1543fb57c3eaSAlex Elder 	ipa_endpoint_init_mode(endpoint);
154484f9bd12SAlex Elder 	ipa_endpoint_init_aggr(endpoint);
1545*01c36637SAlex Elder 	if (!endpoint->toward_ipa)
1546*01c36637SAlex Elder 		ipa_endpoint_init_hol_block_disable(endpoint);
154784f9bd12SAlex Elder 	ipa_endpoint_init_deaggr(endpoint);
15482d265342SAlex Elder 	ipa_endpoint_init_rsrc_grp(endpoint);
154984f9bd12SAlex Elder 	ipa_endpoint_init_seq(endpoint);
155084f9bd12SAlex Elder 	ipa_endpoint_status(endpoint);
155184f9bd12SAlex Elder }
155284f9bd12SAlex Elder 
155384f9bd12SAlex Elder int ipa_endpoint_enable_one(struct ipa_endpoint *endpoint)
155484f9bd12SAlex Elder {
155584f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
155684f9bd12SAlex Elder 	struct gsi *gsi = &ipa->gsi;
155784f9bd12SAlex Elder 	int ret;
155884f9bd12SAlex Elder 
155984f9bd12SAlex Elder 	ret = gsi_channel_start(gsi, endpoint->channel_id);
156084f9bd12SAlex Elder 	if (ret) {
156184f9bd12SAlex Elder 		dev_err(&ipa->pdev->dev,
156284f9bd12SAlex Elder 			"error %d starting %cX channel %u for endpoint %u\n",
156384f9bd12SAlex Elder 			ret, endpoint->toward_ipa ? 'T' : 'R',
156484f9bd12SAlex Elder 			endpoint->channel_id, endpoint->endpoint_id);
156584f9bd12SAlex Elder 		return ret;
156684f9bd12SAlex Elder 	}
156784f9bd12SAlex Elder 
156884f9bd12SAlex Elder 	if (!endpoint->toward_ipa) {
156984f9bd12SAlex Elder 		ipa_interrupt_suspend_enable(ipa->interrupt,
157084f9bd12SAlex Elder 					     endpoint->endpoint_id);
157184f9bd12SAlex Elder 		ipa_endpoint_replenish_enable(endpoint);
157284f9bd12SAlex Elder 	}
157384f9bd12SAlex Elder 
157484f9bd12SAlex Elder 	ipa->enabled |= BIT(endpoint->endpoint_id);
157584f9bd12SAlex Elder 
157684f9bd12SAlex Elder 	return 0;
157784f9bd12SAlex Elder }
157884f9bd12SAlex Elder 
157984f9bd12SAlex Elder void ipa_endpoint_disable_one(struct ipa_endpoint *endpoint)
158084f9bd12SAlex Elder {
158184f9bd12SAlex Elder 	u32 mask = BIT(endpoint->endpoint_id);
158284f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
1583f30dcb7dSAlex Elder 	struct gsi *gsi = &ipa->gsi;
158484f9bd12SAlex Elder 	int ret;
158584f9bd12SAlex Elder 
1586f30dcb7dSAlex Elder 	if (!(ipa->enabled & mask))
158784f9bd12SAlex Elder 		return;
158884f9bd12SAlex Elder 
1589f30dcb7dSAlex Elder 	ipa->enabled ^= mask;
159084f9bd12SAlex Elder 
159184f9bd12SAlex Elder 	if (!endpoint->toward_ipa) {
159284f9bd12SAlex Elder 		ipa_endpoint_replenish_disable(endpoint);
159384f9bd12SAlex Elder 		ipa_interrupt_suspend_disable(ipa->interrupt,
159484f9bd12SAlex Elder 					      endpoint->endpoint_id);
159584f9bd12SAlex Elder 	}
159684f9bd12SAlex Elder 
159784f9bd12SAlex Elder 	/* Note that if stop fails, the channel's state is not well-defined */
1598f30dcb7dSAlex Elder 	ret = gsi_channel_stop(gsi, endpoint->channel_id);
159984f9bd12SAlex Elder 	if (ret)
160084f9bd12SAlex Elder 		dev_err(&ipa->pdev->dev,
160184f9bd12SAlex Elder 			"error %d attempting to stop endpoint %u\n", ret,
160284f9bd12SAlex Elder 			endpoint->endpoint_id);
160384f9bd12SAlex Elder }
160484f9bd12SAlex Elder 
160584f9bd12SAlex Elder void ipa_endpoint_suspend_one(struct ipa_endpoint *endpoint)
160684f9bd12SAlex Elder {
160784f9bd12SAlex Elder 	struct device *dev = &endpoint->ipa->pdev->dev;
160884f9bd12SAlex Elder 	struct gsi *gsi = &endpoint->ipa->gsi;
160984f9bd12SAlex Elder 	int ret;
161084f9bd12SAlex Elder 
161184f9bd12SAlex Elder 	if (!(endpoint->ipa->enabled & BIT(endpoint->endpoint_id)))
161284f9bd12SAlex Elder 		return;
161384f9bd12SAlex Elder 
1614ab4f71e5SAlex Elder 	if (!endpoint->toward_ipa) {
161584f9bd12SAlex Elder 		ipa_endpoint_replenish_disable(endpoint);
16164fa95248SAlex Elder 		(void)ipa_endpoint_program_suspend(endpoint, true);
1617ab4f71e5SAlex Elder 	}
161884f9bd12SAlex Elder 
1619decfef0fSAlex Elder 	ret = gsi_channel_suspend(gsi, endpoint->channel_id);
162084f9bd12SAlex Elder 	if (ret)
162184f9bd12SAlex Elder 		dev_err(dev, "error %d suspending channel %u\n", ret,
162284f9bd12SAlex Elder 			endpoint->channel_id);
162384f9bd12SAlex Elder }
162484f9bd12SAlex Elder 
162584f9bd12SAlex Elder void ipa_endpoint_resume_one(struct ipa_endpoint *endpoint)
162684f9bd12SAlex Elder {
162784f9bd12SAlex Elder 	struct device *dev = &endpoint->ipa->pdev->dev;
162884f9bd12SAlex Elder 	struct gsi *gsi = &endpoint->ipa->gsi;
162984f9bd12SAlex Elder 	int ret;
163084f9bd12SAlex Elder 
163184f9bd12SAlex Elder 	if (!(endpoint->ipa->enabled & BIT(endpoint->endpoint_id)))
163284f9bd12SAlex Elder 		return;
163384f9bd12SAlex Elder 
1634b07f283eSAlex Elder 	if (!endpoint->toward_ipa)
16354fa95248SAlex Elder 		(void)ipa_endpoint_program_suspend(endpoint, false);
163684f9bd12SAlex Elder 
1637decfef0fSAlex Elder 	ret = gsi_channel_resume(gsi, endpoint->channel_id);
163884f9bd12SAlex Elder 	if (ret)
163984f9bd12SAlex Elder 		dev_err(dev, "error %d resuming channel %u\n", ret,
164084f9bd12SAlex Elder 			endpoint->channel_id);
164184f9bd12SAlex Elder 	else if (!endpoint->toward_ipa)
164284f9bd12SAlex Elder 		ipa_endpoint_replenish_enable(endpoint);
164384f9bd12SAlex Elder }
164484f9bd12SAlex Elder 
164584f9bd12SAlex Elder void ipa_endpoint_suspend(struct ipa *ipa)
164684f9bd12SAlex Elder {
1647d1704382SAlex Elder 	if (!ipa->setup_complete)
1648d1704382SAlex Elder 		return;
1649d1704382SAlex Elder 
165084f9bd12SAlex Elder 	if (ipa->modem_netdev)
165184f9bd12SAlex Elder 		ipa_modem_suspend(ipa->modem_netdev);
165284f9bd12SAlex Elder 
1653aa56e3e5SAlex Elder 	ipa_cmd_pipeline_clear(ipa);
16546cb63ea6SAlex Elder 
165584f9bd12SAlex Elder 	ipa_endpoint_suspend_one(ipa->name_map[IPA_ENDPOINT_AP_LAN_RX]);
165684f9bd12SAlex Elder 	ipa_endpoint_suspend_one(ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]);
165784f9bd12SAlex Elder }
165884f9bd12SAlex Elder 
165984f9bd12SAlex Elder void ipa_endpoint_resume(struct ipa *ipa)
166084f9bd12SAlex Elder {
1661d1704382SAlex Elder 	if (!ipa->setup_complete)
1662d1704382SAlex Elder 		return;
1663d1704382SAlex Elder 
166484f9bd12SAlex Elder 	ipa_endpoint_resume_one(ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]);
166584f9bd12SAlex Elder 	ipa_endpoint_resume_one(ipa->name_map[IPA_ENDPOINT_AP_LAN_RX]);
166684f9bd12SAlex Elder 
166784f9bd12SAlex Elder 	if (ipa->modem_netdev)
166884f9bd12SAlex Elder 		ipa_modem_resume(ipa->modem_netdev);
166984f9bd12SAlex Elder }
167084f9bd12SAlex Elder 
167184f9bd12SAlex Elder static void ipa_endpoint_setup_one(struct ipa_endpoint *endpoint)
167284f9bd12SAlex Elder {
167384f9bd12SAlex Elder 	struct gsi *gsi = &endpoint->ipa->gsi;
167484f9bd12SAlex Elder 	u32 channel_id = endpoint->channel_id;
167584f9bd12SAlex Elder 
167684f9bd12SAlex Elder 	/* Only AP endpoints get set up */
167784f9bd12SAlex Elder 	if (endpoint->ee_id != GSI_EE_AP)
167884f9bd12SAlex Elder 		return;
167984f9bd12SAlex Elder 
168084f9bd12SAlex Elder 	endpoint->trans_tre_max = gsi_channel_trans_tre_max(gsi, channel_id);
168184f9bd12SAlex Elder 	if (!endpoint->toward_ipa) {
168284f9bd12SAlex Elder 		/* RX transactions require a single TRE, so the maximum
168384f9bd12SAlex Elder 		 * backlog is the same as the maximum outstanding TREs.
168484f9bd12SAlex Elder 		 */
168584f9bd12SAlex Elder 		endpoint->replenish_enabled = false;
168684f9bd12SAlex Elder 		atomic_set(&endpoint->replenish_saved,
168784f9bd12SAlex Elder 			   gsi_channel_tre_max(gsi, endpoint->channel_id));
168884f9bd12SAlex Elder 		atomic_set(&endpoint->replenish_backlog, 0);
168984f9bd12SAlex Elder 		INIT_DELAYED_WORK(&endpoint->replenish_work,
169084f9bd12SAlex Elder 				  ipa_endpoint_replenish_work);
169184f9bd12SAlex Elder 	}
169284f9bd12SAlex Elder 
169384f9bd12SAlex Elder 	ipa_endpoint_program(endpoint);
169484f9bd12SAlex Elder 
169584f9bd12SAlex Elder 	endpoint->ipa->set_up |= BIT(endpoint->endpoint_id);
169684f9bd12SAlex Elder }
169784f9bd12SAlex Elder 
169884f9bd12SAlex Elder static void ipa_endpoint_teardown_one(struct ipa_endpoint *endpoint)
169984f9bd12SAlex Elder {
170084f9bd12SAlex Elder 	endpoint->ipa->set_up &= ~BIT(endpoint->endpoint_id);
170184f9bd12SAlex Elder 
170284f9bd12SAlex Elder 	if (!endpoint->toward_ipa)
170384f9bd12SAlex Elder 		cancel_delayed_work_sync(&endpoint->replenish_work);
170484f9bd12SAlex Elder 
170584f9bd12SAlex Elder 	ipa_endpoint_reset(endpoint);
170684f9bd12SAlex Elder }
170784f9bd12SAlex Elder 
170884f9bd12SAlex Elder void ipa_endpoint_setup(struct ipa *ipa)
170984f9bd12SAlex Elder {
171084f9bd12SAlex Elder 	u32 initialized = ipa->initialized;
171184f9bd12SAlex Elder 
171284f9bd12SAlex Elder 	ipa->set_up = 0;
171384f9bd12SAlex Elder 	while (initialized) {
171484f9bd12SAlex Elder 		u32 endpoint_id = __ffs(initialized);
171584f9bd12SAlex Elder 
171684f9bd12SAlex Elder 		initialized ^= BIT(endpoint_id);
171784f9bd12SAlex Elder 
171884f9bd12SAlex Elder 		ipa_endpoint_setup_one(&ipa->endpoint[endpoint_id]);
171984f9bd12SAlex Elder 	}
172084f9bd12SAlex Elder }
172184f9bd12SAlex Elder 
172284f9bd12SAlex Elder void ipa_endpoint_teardown(struct ipa *ipa)
172384f9bd12SAlex Elder {
172484f9bd12SAlex Elder 	u32 set_up = ipa->set_up;
172584f9bd12SAlex Elder 
172684f9bd12SAlex Elder 	while (set_up) {
172784f9bd12SAlex Elder 		u32 endpoint_id = __fls(set_up);
172884f9bd12SAlex Elder 
172984f9bd12SAlex Elder 		set_up ^= BIT(endpoint_id);
173084f9bd12SAlex Elder 
173184f9bd12SAlex Elder 		ipa_endpoint_teardown_one(&ipa->endpoint[endpoint_id]);
173284f9bd12SAlex Elder 	}
173384f9bd12SAlex Elder 	ipa->set_up = 0;
173484f9bd12SAlex Elder }
173584f9bd12SAlex Elder 
173684f9bd12SAlex Elder int ipa_endpoint_config(struct ipa *ipa)
173784f9bd12SAlex Elder {
173884f9bd12SAlex Elder 	struct device *dev = &ipa->pdev->dev;
173984f9bd12SAlex Elder 	u32 initialized;
174084f9bd12SAlex Elder 	u32 rx_base;
174184f9bd12SAlex Elder 	u32 rx_mask;
174284f9bd12SAlex Elder 	u32 tx_mask;
174384f9bd12SAlex Elder 	int ret = 0;
174484f9bd12SAlex Elder 	u32 max;
174584f9bd12SAlex Elder 	u32 val;
174684f9bd12SAlex Elder 
1747110971d1SAlex Elder 	/* Prior to IPAv3.5, the FLAVOR_0 register was not supported.
1748110971d1SAlex Elder 	 * Furthermore, the endpoints were not grouped such that TX
1749110971d1SAlex Elder 	 * endpoint numbers started with 0 and RX endpoints had numbers
1750110971d1SAlex Elder 	 * higher than all TX endpoints, so we can't do the simple
1751110971d1SAlex Elder 	 * direction check used for newer hardware below.
1752110971d1SAlex Elder 	 *
1753110971d1SAlex Elder 	 * For hardware that doesn't support the FLAVOR_0 register,
1754110971d1SAlex Elder 	 * just set the available mask to support any endpoint, and
1755110971d1SAlex Elder 	 * assume the configuration is valid.
1756110971d1SAlex Elder 	 */
1757110971d1SAlex Elder 	if (ipa->version < IPA_VERSION_3_5) {
1758110971d1SAlex Elder 		ipa->available = ~0;
1759110971d1SAlex Elder 		return 0;
1760110971d1SAlex Elder 	}
1761110971d1SAlex Elder 
176284f9bd12SAlex Elder 	/* Find out about the endpoints supplied by the hardware, and ensure
176384f9bd12SAlex Elder 	 * the highest one doesn't exceed the number we support.
176484f9bd12SAlex Elder 	 */
176584f9bd12SAlex Elder 	val = ioread32(ipa->reg_virt + IPA_REG_FLAVOR_0_OFFSET);
176684f9bd12SAlex Elder 
176784f9bd12SAlex Elder 	/* Our RX is an IPA producer */
1768716a115bSAlex Elder 	rx_base = u32_get_bits(val, IPA_PROD_LOWEST_FMASK);
1769716a115bSAlex Elder 	max = rx_base + u32_get_bits(val, IPA_MAX_PROD_PIPES_FMASK);
177084f9bd12SAlex Elder 	if (max > IPA_ENDPOINT_MAX) {
177184f9bd12SAlex Elder 		dev_err(dev, "too many endpoints (%u > %u)\n",
177284f9bd12SAlex Elder 			max, IPA_ENDPOINT_MAX);
177384f9bd12SAlex Elder 		return -EINVAL;
177484f9bd12SAlex Elder 	}
177584f9bd12SAlex Elder 	rx_mask = GENMASK(max - 1, rx_base);
177684f9bd12SAlex Elder 
177784f9bd12SAlex Elder 	/* Our TX is an IPA consumer */
1778716a115bSAlex Elder 	max = u32_get_bits(val, IPA_MAX_CONS_PIPES_FMASK);
177984f9bd12SAlex Elder 	tx_mask = GENMASK(max - 1, 0);
178084f9bd12SAlex Elder 
178184f9bd12SAlex Elder 	ipa->available = rx_mask | tx_mask;
178284f9bd12SAlex Elder 
178384f9bd12SAlex Elder 	/* Check for initialized endpoints not supported by the hardware */
178484f9bd12SAlex Elder 	if (ipa->initialized & ~ipa->available) {
178584f9bd12SAlex Elder 		dev_err(dev, "unavailable endpoint id(s) 0x%08x\n",
178684f9bd12SAlex Elder 			ipa->initialized & ~ipa->available);
178784f9bd12SAlex Elder 		ret = -EINVAL;		/* Report other errors too */
178884f9bd12SAlex Elder 	}
178984f9bd12SAlex Elder 
179084f9bd12SAlex Elder 	initialized = ipa->initialized;
179184f9bd12SAlex Elder 	while (initialized) {
179284f9bd12SAlex Elder 		u32 endpoint_id = __ffs(initialized);
179384f9bd12SAlex Elder 		struct ipa_endpoint *endpoint;
179484f9bd12SAlex Elder 
179584f9bd12SAlex Elder 		initialized ^= BIT(endpoint_id);
179684f9bd12SAlex Elder 
179784f9bd12SAlex Elder 		/* Make sure it's pointing in the right direction */
179884f9bd12SAlex Elder 		endpoint = &ipa->endpoint[endpoint_id];
1799602a1c76SAlex Elder 		if ((endpoint_id < rx_base) != endpoint->toward_ipa) {
180084f9bd12SAlex Elder 			dev_err(dev, "endpoint id %u wrong direction\n",
180184f9bd12SAlex Elder 				endpoint_id);
180284f9bd12SAlex Elder 			ret = -EINVAL;
180384f9bd12SAlex Elder 		}
180484f9bd12SAlex Elder 	}
180584f9bd12SAlex Elder 
180684f9bd12SAlex Elder 	return ret;
180784f9bd12SAlex Elder }
180884f9bd12SAlex Elder 
180984f9bd12SAlex Elder void ipa_endpoint_deconfig(struct ipa *ipa)
181084f9bd12SAlex Elder {
181184f9bd12SAlex Elder 	ipa->available = 0;	/* Nothing more to do */
181284f9bd12SAlex Elder }
181384f9bd12SAlex Elder 
181484f9bd12SAlex Elder static void ipa_endpoint_init_one(struct ipa *ipa, enum ipa_endpoint_name name,
181584f9bd12SAlex Elder 				  const struct ipa_gsi_endpoint_data *data)
181684f9bd12SAlex Elder {
181784f9bd12SAlex Elder 	struct ipa_endpoint *endpoint;
181884f9bd12SAlex Elder 
181984f9bd12SAlex Elder 	endpoint = &ipa->endpoint[data->endpoint_id];
182084f9bd12SAlex Elder 
182184f9bd12SAlex Elder 	if (data->ee_id == GSI_EE_AP)
182284f9bd12SAlex Elder 		ipa->channel_map[data->channel_id] = endpoint;
182384f9bd12SAlex Elder 	ipa->name_map[name] = endpoint;
182484f9bd12SAlex Elder 
182584f9bd12SAlex Elder 	endpoint->ipa = ipa;
182684f9bd12SAlex Elder 	endpoint->ee_id = data->ee_id;
182784f9bd12SAlex Elder 	endpoint->channel_id = data->channel_id;
182884f9bd12SAlex Elder 	endpoint->endpoint_id = data->endpoint_id;
182984f9bd12SAlex Elder 	endpoint->toward_ipa = data->toward_ipa;
183084f9bd12SAlex Elder 	endpoint->data = &data->endpoint.config;
183184f9bd12SAlex Elder 
183284f9bd12SAlex Elder 	ipa->initialized |= BIT(endpoint->endpoint_id);
183384f9bd12SAlex Elder }
183484f9bd12SAlex Elder 
1835602a1c76SAlex Elder static void ipa_endpoint_exit_one(struct ipa_endpoint *endpoint)
183684f9bd12SAlex Elder {
183784f9bd12SAlex Elder 	endpoint->ipa->initialized &= ~BIT(endpoint->endpoint_id);
183884f9bd12SAlex Elder 
183984f9bd12SAlex Elder 	memset(endpoint, 0, sizeof(*endpoint));
184084f9bd12SAlex Elder }
184184f9bd12SAlex Elder 
184284f9bd12SAlex Elder void ipa_endpoint_exit(struct ipa *ipa)
184384f9bd12SAlex Elder {
184484f9bd12SAlex Elder 	u32 initialized = ipa->initialized;
184584f9bd12SAlex Elder 
184684f9bd12SAlex Elder 	while (initialized) {
184784f9bd12SAlex Elder 		u32 endpoint_id = __fls(initialized);
184884f9bd12SAlex Elder 
184984f9bd12SAlex Elder 		initialized ^= BIT(endpoint_id);
185084f9bd12SAlex Elder 
185184f9bd12SAlex Elder 		ipa_endpoint_exit_one(&ipa->endpoint[endpoint_id]);
185284f9bd12SAlex Elder 	}
185384f9bd12SAlex Elder 	memset(ipa->name_map, 0, sizeof(ipa->name_map));
185484f9bd12SAlex Elder 	memset(ipa->channel_map, 0, sizeof(ipa->channel_map));
185584f9bd12SAlex Elder }
185684f9bd12SAlex Elder 
185784f9bd12SAlex Elder /* Returns a bitmask of endpoints that support filtering, or 0 on error */
185884f9bd12SAlex Elder u32 ipa_endpoint_init(struct ipa *ipa, u32 count,
185984f9bd12SAlex Elder 		      const struct ipa_gsi_endpoint_data *data)
186084f9bd12SAlex Elder {
186184f9bd12SAlex Elder 	enum ipa_endpoint_name name;
186284f9bd12SAlex Elder 	u32 filter_map;
186384f9bd12SAlex Elder 
186484f9bd12SAlex Elder 	if (!ipa_endpoint_data_valid(ipa, count, data))
186584f9bd12SAlex Elder 		return 0;	/* Error */
186684f9bd12SAlex Elder 
186784f9bd12SAlex Elder 	ipa->initialized = 0;
186884f9bd12SAlex Elder 
186984f9bd12SAlex Elder 	filter_map = 0;
187084f9bd12SAlex Elder 	for (name = 0; name < count; name++, data++) {
187184f9bd12SAlex Elder 		if (ipa_gsi_endpoint_data_empty(data))
187284f9bd12SAlex Elder 			continue;	/* Skip over empty slots */
187384f9bd12SAlex Elder 
187484f9bd12SAlex Elder 		ipa_endpoint_init_one(ipa, name, data);
187584f9bd12SAlex Elder 
187684f9bd12SAlex Elder 		if (data->endpoint.filter_support)
187784f9bd12SAlex Elder 			filter_map |= BIT(data->endpoint_id);
187884f9bd12SAlex Elder 	}
187984f9bd12SAlex Elder 
188084f9bd12SAlex Elder 	if (!ipa_filter_map_valid(ipa, filter_map))
188184f9bd12SAlex Elder 		goto err_endpoint_exit;
188284f9bd12SAlex Elder 
188384f9bd12SAlex Elder 	return filter_map;	/* Non-zero bitmask */
188484f9bd12SAlex Elder 
188584f9bd12SAlex Elder err_endpoint_exit:
188684f9bd12SAlex Elder 	ipa_endpoint_exit(ipa);
188784f9bd12SAlex Elder 
188884f9bd12SAlex Elder 	return 0;	/* Error */
188984f9bd12SAlex Elder }
1890