xref: /linux/drivers/net/ipa/ipa_endpoint.c (revision ce54993d01b996cdf09e15882e77bea8c10903ca)
184f9bd12SAlex Elder // SPDX-License-Identifier: GPL-2.0
284f9bd12SAlex Elder 
384f9bd12SAlex Elder /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
484f9bd12SAlex Elder  * Copyright (C) 2019-2020 Linaro Ltd.
584f9bd12SAlex Elder  */
684f9bd12SAlex Elder 
784f9bd12SAlex Elder #include <linux/types.h>
884f9bd12SAlex Elder #include <linux/device.h>
984f9bd12SAlex Elder #include <linux/slab.h>
1084f9bd12SAlex Elder #include <linux/bitfield.h>
1184f9bd12SAlex Elder #include <linux/if_rmnet.h>
1284f9bd12SAlex Elder #include <linux/dma-direction.h>
1384f9bd12SAlex Elder 
1484f9bd12SAlex Elder #include "gsi.h"
1584f9bd12SAlex Elder #include "gsi_trans.h"
1684f9bd12SAlex Elder #include "ipa.h"
1784f9bd12SAlex Elder #include "ipa_data.h"
1884f9bd12SAlex Elder #include "ipa_endpoint.h"
1984f9bd12SAlex Elder #include "ipa_cmd.h"
2084f9bd12SAlex Elder #include "ipa_mem.h"
2184f9bd12SAlex Elder #include "ipa_modem.h"
2284f9bd12SAlex Elder #include "ipa_table.h"
2384f9bd12SAlex Elder #include "ipa_gsi.h"
24f13a8c31SAlex Elder #include "ipa_clock.h"
2584f9bd12SAlex Elder 
2684f9bd12SAlex Elder #define atomic_dec_not_zero(v)	atomic_add_unless((v), -1, 0)
2784f9bd12SAlex Elder 
2884f9bd12SAlex Elder #define IPA_REPLENISH_BATCH	16
2984f9bd12SAlex Elder 
306fcd4224SAlex Elder /* RX buffer is 1 page (or a power-of-2 contiguous pages) */
316fcd4224SAlex Elder #define IPA_RX_BUFFER_SIZE	8192	/* PAGE_SIZE > 4096 wastes a LOT */
3284f9bd12SAlex Elder 
3384f9bd12SAlex Elder /* The amount of RX buffer space consumed by standard skb overhead */
3484f9bd12SAlex Elder #define IPA_RX_BUFFER_OVERHEAD	(PAGE_SIZE - SKB_MAX_ORDER(NET_SKB_PAD, 0))
3584f9bd12SAlex Elder 
368730f45dSAlex Elder /* Where to find the QMAP mux_id for a packet within modem-supplied metadata */
378730f45dSAlex Elder #define IPA_ENDPOINT_QMAP_METADATA_MASK		0x000000ff /* host byte order */
388730f45dSAlex Elder 
3984f9bd12SAlex Elder #define IPA_ENDPOINT_RESET_AGGR_RETRY_MAX	3
401d86652bSAlex Elder #define IPA_AGGR_TIME_LIMIT_DEFAULT		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 */
7284f9bd12SAlex Elder #define IPA_STATUS_DST_IDX_FMASK		GENMASK(4, 0)
7384f9bd12SAlex Elder #define IPA_STATUS_FLAGS1_RT_RULE_ID_FMASK	GENMASK(31, 22)
7484f9bd12SAlex Elder 
7584f9bd12SAlex Elder #ifdef IPA_VALIDATE
7684f9bd12SAlex Elder 
7784f9bd12SAlex Elder static void ipa_endpoint_validate_build(void)
7884f9bd12SAlex Elder {
7984f9bd12SAlex Elder 	/* The aggregation byte limit defines the point at which an
8084f9bd12SAlex Elder 	 * aggregation window will close.  It is programmed into the
8184f9bd12SAlex Elder 	 * IPA hardware as a number of KB.  We don't use "hard byte
8284f9bd12SAlex Elder 	 * limit" aggregation, which means that we need to supply
8384f9bd12SAlex Elder 	 * enough space in a receive buffer to hold a complete MTU
8484f9bd12SAlex Elder 	 * plus normal skb overhead *after* that aggregation byte
8584f9bd12SAlex Elder 	 * limit has been crossed.
8684f9bd12SAlex Elder 	 *
8784f9bd12SAlex Elder 	 * This check just ensures we don't define a receive buffer
8884f9bd12SAlex Elder 	 * size that would exceed what we can represent in the field
8984f9bd12SAlex Elder 	 * that is used to program its size.
9084f9bd12SAlex Elder 	 */
9184f9bd12SAlex Elder 	BUILD_BUG_ON(IPA_RX_BUFFER_SIZE >
9284f9bd12SAlex Elder 		     field_max(AGGR_BYTE_LIMIT_FMASK) * SZ_1K +
9384f9bd12SAlex Elder 		     IPA_MTU + IPA_RX_BUFFER_OVERHEAD);
9484f9bd12SAlex Elder 
9584f9bd12SAlex Elder 	/* I honestly don't know where this requirement comes from.  But
9684f9bd12SAlex Elder 	 * it holds, and if we someday need to loosen the constraint we
9784f9bd12SAlex Elder 	 * can try to track it down.
9884f9bd12SAlex Elder 	 */
9984f9bd12SAlex Elder 	BUILD_BUG_ON(sizeof(struct ipa_status) % 4);
10084f9bd12SAlex Elder }
10184f9bd12SAlex Elder 
10284f9bd12SAlex Elder static bool ipa_endpoint_data_valid_one(struct ipa *ipa, u32 count,
10384f9bd12SAlex Elder 			    const struct ipa_gsi_endpoint_data *all_data,
10484f9bd12SAlex Elder 			    const struct ipa_gsi_endpoint_data *data)
10584f9bd12SAlex Elder {
10684f9bd12SAlex Elder 	const struct ipa_gsi_endpoint_data *other_data;
10784f9bd12SAlex Elder 	struct device *dev = &ipa->pdev->dev;
10884f9bd12SAlex Elder 	enum ipa_endpoint_name other_name;
10984f9bd12SAlex Elder 
11084f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(data))
11184f9bd12SAlex Elder 		return true;
11284f9bd12SAlex Elder 
11384f9bd12SAlex Elder 	if (!data->toward_ipa) {
11484f9bd12SAlex Elder 		if (data->endpoint.filter_support) {
11584f9bd12SAlex Elder 			dev_err(dev, "filtering not supported for "
11684f9bd12SAlex Elder 					"RX endpoint %u\n",
11784f9bd12SAlex Elder 				data->endpoint_id);
11884f9bd12SAlex Elder 			return false;
11984f9bd12SAlex Elder 		}
12084f9bd12SAlex Elder 
12184f9bd12SAlex Elder 		return true;	/* Nothing more to check for RX */
12284f9bd12SAlex Elder 	}
12384f9bd12SAlex Elder 
12484f9bd12SAlex Elder 	if (data->endpoint.config.status_enable) {
12584f9bd12SAlex Elder 		other_name = data->endpoint.config.tx.status_endpoint;
12684f9bd12SAlex Elder 		if (other_name >= count) {
12784f9bd12SAlex Elder 			dev_err(dev, "status endpoint name %u out of range "
12884f9bd12SAlex Elder 					"for endpoint %u\n",
12984f9bd12SAlex Elder 				other_name, data->endpoint_id);
13084f9bd12SAlex Elder 			return false;
13184f9bd12SAlex Elder 		}
13284f9bd12SAlex Elder 
13384f9bd12SAlex Elder 		/* Status endpoint must be defined... */
13484f9bd12SAlex Elder 		other_data = &all_data[other_name];
13584f9bd12SAlex Elder 		if (ipa_gsi_endpoint_data_empty(other_data)) {
13684f9bd12SAlex Elder 			dev_err(dev, "DMA endpoint name %u undefined "
13784f9bd12SAlex Elder 					"for endpoint %u\n",
13884f9bd12SAlex Elder 				other_name, data->endpoint_id);
13984f9bd12SAlex Elder 			return false;
14084f9bd12SAlex Elder 		}
14184f9bd12SAlex Elder 
14284f9bd12SAlex Elder 		/* ...and has to be an RX endpoint... */
14384f9bd12SAlex Elder 		if (other_data->toward_ipa) {
14484f9bd12SAlex Elder 			dev_err(dev,
14584f9bd12SAlex Elder 				"status endpoint for endpoint %u not RX\n",
14684f9bd12SAlex Elder 				data->endpoint_id);
14784f9bd12SAlex Elder 			return false;
14884f9bd12SAlex Elder 		}
14984f9bd12SAlex Elder 
15084f9bd12SAlex Elder 		/* ...and if it's to be an AP endpoint... */
15184f9bd12SAlex Elder 		if (other_data->ee_id == GSI_EE_AP) {
15284f9bd12SAlex Elder 			/* ...make sure it has status enabled. */
15384f9bd12SAlex Elder 			if (!other_data->endpoint.config.status_enable) {
15484f9bd12SAlex Elder 				dev_err(dev,
15584f9bd12SAlex Elder 					"status not enabled for endpoint %u\n",
15684f9bd12SAlex Elder 					other_data->endpoint_id);
15784f9bd12SAlex Elder 				return false;
15884f9bd12SAlex Elder 			}
15984f9bd12SAlex Elder 		}
16084f9bd12SAlex Elder 	}
16184f9bd12SAlex Elder 
16284f9bd12SAlex Elder 	if (data->endpoint.config.dma_mode) {
16384f9bd12SAlex Elder 		other_name = data->endpoint.config.dma_endpoint;
16484f9bd12SAlex Elder 		if (other_name >= count) {
16584f9bd12SAlex Elder 			dev_err(dev, "DMA endpoint name %u out of range "
16684f9bd12SAlex Elder 					"for endpoint %u\n",
16784f9bd12SAlex Elder 				other_name, data->endpoint_id);
16884f9bd12SAlex Elder 			return false;
16984f9bd12SAlex Elder 		}
17084f9bd12SAlex Elder 
17184f9bd12SAlex Elder 		other_data = &all_data[other_name];
17284f9bd12SAlex Elder 		if (ipa_gsi_endpoint_data_empty(other_data)) {
17384f9bd12SAlex Elder 			dev_err(dev, "DMA endpoint name %u undefined "
17484f9bd12SAlex Elder 					"for endpoint %u\n",
17584f9bd12SAlex Elder 				other_name, data->endpoint_id);
17684f9bd12SAlex Elder 			return false;
17784f9bd12SAlex Elder 		}
17884f9bd12SAlex Elder 	}
17984f9bd12SAlex Elder 
18084f9bd12SAlex Elder 	return true;
18184f9bd12SAlex Elder }
18284f9bd12SAlex Elder 
18384f9bd12SAlex Elder static bool ipa_endpoint_data_valid(struct ipa *ipa, u32 count,
18484f9bd12SAlex Elder 				    const struct ipa_gsi_endpoint_data *data)
18584f9bd12SAlex Elder {
18684f9bd12SAlex Elder 	const struct ipa_gsi_endpoint_data *dp = data;
18784f9bd12SAlex Elder 	struct device *dev = &ipa->pdev->dev;
18884f9bd12SAlex Elder 	enum ipa_endpoint_name name;
18984f9bd12SAlex Elder 
19084f9bd12SAlex Elder 	ipa_endpoint_validate_build();
19184f9bd12SAlex Elder 
19284f9bd12SAlex Elder 	if (count > IPA_ENDPOINT_COUNT) {
19384f9bd12SAlex Elder 		dev_err(dev, "too many endpoints specified (%u > %u)\n",
19484f9bd12SAlex Elder 			count, IPA_ENDPOINT_COUNT);
19584f9bd12SAlex Elder 		return false;
19684f9bd12SAlex Elder 	}
19784f9bd12SAlex Elder 
19884f9bd12SAlex Elder 	/* Make sure needed endpoints have defined data */
19984f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_COMMAND_TX])) {
20084f9bd12SAlex Elder 		dev_err(dev, "command TX endpoint not defined\n");
20184f9bd12SAlex Elder 		return false;
20284f9bd12SAlex Elder 	}
20384f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_LAN_RX])) {
20484f9bd12SAlex Elder 		dev_err(dev, "LAN RX endpoint not defined\n");
20584f9bd12SAlex Elder 		return false;
20684f9bd12SAlex Elder 	}
20784f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_MODEM_TX])) {
20884f9bd12SAlex Elder 		dev_err(dev, "AP->modem TX endpoint not defined\n");
20984f9bd12SAlex Elder 		return false;
21084f9bd12SAlex Elder 	}
21184f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_MODEM_RX])) {
21284f9bd12SAlex Elder 		dev_err(dev, "AP<-modem RX endpoint not defined\n");
21384f9bd12SAlex Elder 		return false;
21484f9bd12SAlex Elder 	}
21584f9bd12SAlex Elder 
21684f9bd12SAlex Elder 	for (name = 0; name < count; name++, dp++)
21784f9bd12SAlex Elder 		if (!ipa_endpoint_data_valid_one(ipa, count, data, dp))
21884f9bd12SAlex Elder 			return false;
21984f9bd12SAlex Elder 
22084f9bd12SAlex Elder 	return true;
22184f9bd12SAlex Elder }
22284f9bd12SAlex Elder 
22384f9bd12SAlex Elder #else /* !IPA_VALIDATE */
22484f9bd12SAlex Elder 
22584f9bd12SAlex Elder static bool ipa_endpoint_data_valid(struct ipa *ipa, u32 count,
22684f9bd12SAlex Elder 				    const struct ipa_gsi_endpoint_data *data)
22784f9bd12SAlex Elder {
22884f9bd12SAlex Elder 	return true;
22984f9bd12SAlex Elder }
23084f9bd12SAlex Elder 
23184f9bd12SAlex Elder #endif /* !IPA_VALIDATE */
23284f9bd12SAlex Elder 
23384f9bd12SAlex Elder /* Allocate a transaction to use on a non-command endpoint */
23484f9bd12SAlex Elder static struct gsi_trans *ipa_endpoint_trans_alloc(struct ipa_endpoint *endpoint,
23584f9bd12SAlex Elder 						  u32 tre_count)
23684f9bd12SAlex Elder {
23784f9bd12SAlex Elder 	struct gsi *gsi = &endpoint->ipa->gsi;
23884f9bd12SAlex Elder 	u32 channel_id = endpoint->channel_id;
23984f9bd12SAlex Elder 	enum dma_data_direction direction;
24084f9bd12SAlex Elder 
24184f9bd12SAlex Elder 	direction = endpoint->toward_ipa ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
24284f9bd12SAlex Elder 
24384f9bd12SAlex Elder 	return gsi_channel_trans_alloc(gsi, channel_id, tre_count, direction);
24484f9bd12SAlex Elder }
24584f9bd12SAlex Elder 
24684f9bd12SAlex Elder /* suspend_delay represents suspend for RX, delay for TX endpoints.
24784f9bd12SAlex Elder  * Note that suspend is not supported starting with IPA v4.0.
24884f9bd12SAlex Elder  */
2494900bf34SAlex Elder static bool
25084f9bd12SAlex Elder ipa_endpoint_init_ctrl(struct ipa_endpoint *endpoint, bool suspend_delay)
25184f9bd12SAlex Elder {
25284f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_CTRL_N_OFFSET(endpoint->endpoint_id);
25384f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
2544900bf34SAlex Elder 	bool state;
25584f9bd12SAlex Elder 	u32 mask;
25684f9bd12SAlex Elder 	u32 val;
25784f9bd12SAlex Elder 
2584fa95248SAlex Elder 	/* Suspend is not supported for IPA v4.0+.  Delay doesn't work
2594fa95248SAlex Elder 	 * correctly on IPA v4.2.
2604fa95248SAlex Elder 	 *
2614fa95248SAlex Elder 	 * if (endpoint->toward_ipa)
2624fa95248SAlex Elder 	 * 	assert(ipa->version != IPA_VERSION_4.2);
2634fa95248SAlex Elder 	 * else
2644fa95248SAlex Elder 	 * 	assert(ipa->version == IPA_VERSION_3_5_1);
2654fa95248SAlex Elder 	 */
26684f9bd12SAlex Elder 	mask = endpoint->toward_ipa ? ENDP_DELAY_FMASK : ENDP_SUSPEND_FMASK;
26784f9bd12SAlex Elder 
26884f9bd12SAlex Elder 	val = ioread32(ipa->reg_virt + offset);
2694900bf34SAlex Elder 	/* Don't bother if it's already in the requested state */
2704900bf34SAlex Elder 	state = !!(val & mask);
2714900bf34SAlex Elder 	if (suspend_delay != state) {
27284f9bd12SAlex Elder 		val ^= mask;
27384f9bd12SAlex Elder 		iowrite32(val, ipa->reg_virt + offset);
2744900bf34SAlex Elder 	}
27584f9bd12SAlex Elder 
2764900bf34SAlex Elder 	return state;
27784f9bd12SAlex Elder }
27884f9bd12SAlex Elder 
2794fa95248SAlex Elder /* We currently don't care what the previous state was for delay mode */
2804fa95248SAlex Elder static void
2814fa95248SAlex Elder ipa_endpoint_program_delay(struct ipa_endpoint *endpoint, bool enable)
2824fa95248SAlex Elder {
2834fa95248SAlex Elder 	/* assert(endpoint->toward_ipa); */
2844fa95248SAlex Elder 
28566eba767SAlex Elder 	/* Delay mode doesn't work properly for IPA v4.2 */
28666eba767SAlex Elder 	if (endpoint->ipa->version != IPA_VERSION_4_2)
2874fa95248SAlex Elder 		(void)ipa_endpoint_init_ctrl(endpoint, enable);
2884fa95248SAlex Elder }
2894fa95248SAlex Elder 
290fff89971SAlex Elder static bool ipa_endpoint_aggr_active(struct ipa_endpoint *endpoint)
291fff89971SAlex Elder {
292fff89971SAlex Elder 	u32 mask = BIT(endpoint->endpoint_id);
293fff89971SAlex Elder 	struct ipa *ipa = endpoint->ipa;
294fff89971SAlex Elder 	u32 offset;
295fff89971SAlex Elder 	u32 val;
296fff89971SAlex Elder 
297fff89971SAlex Elder 	/* assert(mask & ipa->available); */
298fff89971SAlex Elder 	offset = ipa_reg_state_aggr_active_offset(ipa->version);
299fff89971SAlex Elder 	val = ioread32(ipa->reg_virt + offset);
300fff89971SAlex Elder 
301fff89971SAlex Elder 	return !!(val & mask);
302fff89971SAlex Elder }
303fff89971SAlex Elder 
304fff89971SAlex Elder static void ipa_endpoint_force_close(struct ipa_endpoint *endpoint)
305fff89971SAlex Elder {
306fff89971SAlex Elder 	u32 mask = BIT(endpoint->endpoint_id);
307fff89971SAlex Elder 	struct ipa *ipa = endpoint->ipa;
308fff89971SAlex Elder 
309fff89971SAlex Elder 	/* assert(mask & ipa->available); */
310fff89971SAlex Elder 	iowrite32(mask, ipa->reg_virt + IPA_REG_AGGR_FORCE_CLOSE_OFFSET);
311fff89971SAlex Elder }
312fff89971SAlex Elder 
313fff89971SAlex Elder /**
314fff89971SAlex Elder  * ipa_endpoint_suspend_aggr() - Emulate suspend interrupt
315e3eea08eSAlex Elder  * @endpoint:	Endpoint on which to emulate a suspend
316fff89971SAlex Elder  *
317fff89971SAlex Elder  *  Emulate suspend IPA interrupt to unsuspend an endpoint suspended
318fff89971SAlex Elder  *  with an open aggregation frame.  This is to work around a hardware
319fff89971SAlex Elder  *  issue in IPA version 3.5.1 where the suspend interrupt will not be
320fff89971SAlex Elder  *  generated when it should be.
321fff89971SAlex Elder  */
322fff89971SAlex Elder static void ipa_endpoint_suspend_aggr(struct ipa_endpoint *endpoint)
323fff89971SAlex Elder {
324fff89971SAlex Elder 	struct ipa *ipa = endpoint->ipa;
325fff89971SAlex Elder 
326fff89971SAlex Elder 	if (!endpoint->data->aggregation)
327fff89971SAlex Elder 		return;
328fff89971SAlex Elder 
329fff89971SAlex Elder 	/* Nothing to do if the endpoint doesn't have aggregation open */
330fff89971SAlex Elder 	if (!ipa_endpoint_aggr_active(endpoint))
331fff89971SAlex Elder 		return;
332fff89971SAlex Elder 
333fff89971SAlex Elder 	/* Force close aggregation */
334fff89971SAlex Elder 	ipa_endpoint_force_close(endpoint);
335fff89971SAlex Elder 
336fff89971SAlex Elder 	ipa_interrupt_simulate_suspend(ipa->interrupt);
337fff89971SAlex Elder }
338fff89971SAlex Elder 
339fff89971SAlex Elder /* Returns previous suspend state (true means suspend was enabled) */
3404fa95248SAlex Elder static bool
3414fa95248SAlex Elder ipa_endpoint_program_suspend(struct ipa_endpoint *endpoint, bool enable)
3424fa95248SAlex Elder {
343fff89971SAlex Elder 	bool suspended;
344fff89971SAlex Elder 
345b07f283eSAlex Elder 	if (endpoint->ipa->version != IPA_VERSION_3_5_1)
346b07f283eSAlex Elder 		return enable;	/* For IPA v4.0+, no change made */
347b07f283eSAlex Elder 
3484fa95248SAlex Elder 	/* assert(!endpoint->toward_ipa); */
3494fa95248SAlex Elder 
350fff89971SAlex Elder 	suspended = ipa_endpoint_init_ctrl(endpoint, enable);
351fff89971SAlex Elder 
352fff89971SAlex Elder 	/* A client suspended with an open aggregation frame will not
353fff89971SAlex Elder 	 * generate a SUSPEND IPA interrupt.  If enabling suspend, have
354fff89971SAlex Elder 	 * ipa_endpoint_suspend_aggr() handle this.
355fff89971SAlex Elder 	 */
356fff89971SAlex Elder 	if (enable && !suspended)
357fff89971SAlex Elder 		ipa_endpoint_suspend_aggr(endpoint);
358fff89971SAlex Elder 
359fff89971SAlex Elder 	return suspended;
3604fa95248SAlex Elder }
3614fa95248SAlex Elder 
36284f9bd12SAlex Elder /* Enable or disable delay or suspend mode on all modem endpoints */
36384f9bd12SAlex Elder void ipa_endpoint_modem_pause_all(struct ipa *ipa, bool enable)
36484f9bd12SAlex Elder {
36584f9bd12SAlex Elder 	u32 endpoint_id;
36684f9bd12SAlex Elder 
3674fa95248SAlex Elder 	/* DELAY mode doesn't work correctly on IPA v4.2 */
36884f9bd12SAlex Elder 	if (ipa->version == IPA_VERSION_4_2)
36984f9bd12SAlex Elder 		return;
37084f9bd12SAlex Elder 
37184f9bd12SAlex Elder 	for (endpoint_id = 0; endpoint_id < IPA_ENDPOINT_MAX; endpoint_id++) {
37284f9bd12SAlex Elder 		struct ipa_endpoint *endpoint = &ipa->endpoint[endpoint_id];
37384f9bd12SAlex Elder 
37484f9bd12SAlex Elder 		if (endpoint->ee_id != GSI_EE_MODEM)
37584f9bd12SAlex Elder 			continue;
37684f9bd12SAlex Elder 
377b07f283eSAlex Elder 		/* Set TX delay mode or RX suspend mode */
3784fa95248SAlex Elder 		if (endpoint->toward_ipa)
3794fa95248SAlex Elder 			ipa_endpoint_program_delay(endpoint, enable);
380b07f283eSAlex Elder 		else
3814fa95248SAlex Elder 			(void)ipa_endpoint_program_suspend(endpoint, enable);
38284f9bd12SAlex Elder 	}
38384f9bd12SAlex Elder }
38484f9bd12SAlex Elder 
38584f9bd12SAlex Elder /* Reset all modem endpoints to use the default exception endpoint */
38684f9bd12SAlex Elder int ipa_endpoint_modem_exception_reset_all(struct ipa *ipa)
38784f9bd12SAlex Elder {
38884f9bd12SAlex Elder 	u32 initialized = ipa->initialized;
38984f9bd12SAlex Elder 	struct gsi_trans *trans;
39084f9bd12SAlex Elder 	u32 count;
39184f9bd12SAlex Elder 
39284f9bd12SAlex Elder 	/* We need one command per modem TX endpoint.  We can get an upper
39384f9bd12SAlex Elder 	 * bound on that by assuming all initialized endpoints are modem->IPA.
39484f9bd12SAlex Elder 	 * That won't happen, and we could be more precise, but this is fine
3958fa54b11SWang Wenhu 	 * for now.  We need to end the transaction with a "tag process."
39684f9bd12SAlex Elder 	 */
39784f9bd12SAlex Elder 	count = hweight32(initialized) + ipa_cmd_tag_process_count();
39884f9bd12SAlex Elder 	trans = ipa_cmd_trans_alloc(ipa, count);
39984f9bd12SAlex Elder 	if (!trans) {
40084f9bd12SAlex Elder 		dev_err(&ipa->pdev->dev,
40184f9bd12SAlex Elder 			"no transaction to reset modem exception endpoints\n");
40284f9bd12SAlex Elder 		return -EBUSY;
40384f9bd12SAlex Elder 	}
40484f9bd12SAlex Elder 
40584f9bd12SAlex Elder 	while (initialized) {
40684f9bd12SAlex Elder 		u32 endpoint_id = __ffs(initialized);
40784f9bd12SAlex Elder 		struct ipa_endpoint *endpoint;
40884f9bd12SAlex Elder 		u32 offset;
40984f9bd12SAlex Elder 
41084f9bd12SAlex Elder 		initialized ^= BIT(endpoint_id);
41184f9bd12SAlex Elder 
41284f9bd12SAlex Elder 		/* We only reset modem TX endpoints */
41384f9bd12SAlex Elder 		endpoint = &ipa->endpoint[endpoint_id];
41484f9bd12SAlex Elder 		if (!(endpoint->ee_id == GSI_EE_MODEM && endpoint->toward_ipa))
41584f9bd12SAlex Elder 			continue;
41684f9bd12SAlex Elder 
41784f9bd12SAlex Elder 		offset = IPA_REG_ENDP_STATUS_N_OFFSET(endpoint_id);
41884f9bd12SAlex Elder 
41984f9bd12SAlex Elder 		/* Value written is 0, and all bits are updated.  That
42084f9bd12SAlex Elder 		 * means status is disabled on the endpoint, and as a
42184f9bd12SAlex Elder 		 * result all other fields in the register are ignored.
42284f9bd12SAlex Elder 		 */
42384f9bd12SAlex Elder 		ipa_cmd_register_write_add(trans, offset, 0, ~0, false);
42484f9bd12SAlex Elder 	}
42584f9bd12SAlex Elder 
42684f9bd12SAlex Elder 	ipa_cmd_tag_process_add(trans);
42784f9bd12SAlex Elder 
42884f9bd12SAlex Elder 	/* XXX This should have a 1 second timeout */
42984f9bd12SAlex Elder 	gsi_trans_commit_wait(trans);
43084f9bd12SAlex Elder 
43184f9bd12SAlex Elder 	return 0;
43284f9bd12SAlex Elder }
43384f9bd12SAlex Elder 
43484f9bd12SAlex Elder static void ipa_endpoint_init_cfg(struct ipa_endpoint *endpoint)
43584f9bd12SAlex Elder {
43684f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_CFG_N_OFFSET(endpoint->endpoint_id);
43784f9bd12SAlex Elder 	u32 val = 0;
43884f9bd12SAlex Elder 
43984f9bd12SAlex Elder 	/* FRAG_OFFLOAD_EN is 0 */
44084f9bd12SAlex Elder 	if (endpoint->data->checksum) {
44184f9bd12SAlex Elder 		if (endpoint->toward_ipa) {
44284f9bd12SAlex Elder 			u32 checksum_offset;
44384f9bd12SAlex Elder 
44484f9bd12SAlex Elder 			val |= u32_encode_bits(IPA_CS_OFFLOAD_UL,
44584f9bd12SAlex Elder 					       CS_OFFLOAD_EN_FMASK);
44684f9bd12SAlex Elder 			/* Checksum header offset is in 4-byte units */
44784f9bd12SAlex Elder 			checksum_offset = sizeof(struct rmnet_map_header);
44884f9bd12SAlex Elder 			checksum_offset /= sizeof(u32);
44984f9bd12SAlex Elder 			val |= u32_encode_bits(checksum_offset,
45084f9bd12SAlex Elder 					       CS_METADATA_HDR_OFFSET_FMASK);
45184f9bd12SAlex Elder 		} else {
45284f9bd12SAlex Elder 			val |= u32_encode_bits(IPA_CS_OFFLOAD_DL,
45384f9bd12SAlex Elder 					       CS_OFFLOAD_EN_FMASK);
45484f9bd12SAlex Elder 		}
45584f9bd12SAlex Elder 	} else {
45684f9bd12SAlex Elder 		val |= u32_encode_bits(IPA_CS_OFFLOAD_NONE,
45784f9bd12SAlex Elder 				       CS_OFFLOAD_EN_FMASK);
45884f9bd12SAlex Elder 	}
45984f9bd12SAlex Elder 	/* CS_GEN_QMB_MASTER_SEL is 0 */
46084f9bd12SAlex Elder 
46184f9bd12SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
46284f9bd12SAlex Elder }
46384f9bd12SAlex Elder 
4648730f45dSAlex Elder /**
465e3eea08eSAlex Elder  * ipa_endpoint_init_hdr() - Initialize HDR endpoint configuration register
466e3eea08eSAlex Elder  * @endpoint:	Endpoint pointer
467e3eea08eSAlex Elder  *
4688730f45dSAlex Elder  * We program QMAP endpoints so each packet received is preceded by a QMAP
4698730f45dSAlex Elder  * header structure.  The QMAP header contains a 1-byte mux_id and 2-byte
4708730f45dSAlex Elder  * packet size field, and we have the IPA hardware populate both for each
4718730f45dSAlex Elder  * received packet.  The header is configured (in the HDR_EXT register)
4728730f45dSAlex Elder  * to use big endian format.
4738730f45dSAlex Elder  *
4748730f45dSAlex Elder  * The packet size is written into the QMAP header's pkt_len field.  That
4758730f45dSAlex Elder  * location is defined here using the HDR_OFST_PKT_SIZE field.
4768730f45dSAlex Elder  *
4778730f45dSAlex Elder  * The mux_id comes from a 4-byte metadata value supplied with each packet
4788730f45dSAlex Elder  * by the modem.  It is *not* a QMAP header, but it does contain the mux_id
4798730f45dSAlex Elder  * value that we want, in its low-order byte.  A bitmask defined in the
4808730f45dSAlex Elder  * endpoint's METADATA_MASK register defines which byte within the modem
4818730f45dSAlex Elder  * metadata contains the mux_id.  And the OFST_METADATA field programmed
4828730f45dSAlex Elder  * here indicates where the extracted byte should be placed within the QMAP
4838730f45dSAlex Elder  * header.
4848730f45dSAlex Elder  */
48584f9bd12SAlex Elder static void ipa_endpoint_init_hdr(struct ipa_endpoint *endpoint)
48684f9bd12SAlex Elder {
48784f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_HDR_N_OFFSET(endpoint->endpoint_id);
48884f9bd12SAlex Elder 	u32 val = 0;
48984f9bd12SAlex Elder 
49084f9bd12SAlex Elder 	if (endpoint->data->qmap) {
49184f9bd12SAlex Elder 		size_t header_size = sizeof(struct rmnet_map_header);
49284f9bd12SAlex Elder 
4938730f45dSAlex Elder 		/* We might supply a checksum header after the QMAP header */
49484f9bd12SAlex Elder 		if (endpoint->toward_ipa && endpoint->data->checksum)
49584f9bd12SAlex Elder 			header_size += sizeof(struct rmnet_map_ul_csum_header);
49684f9bd12SAlex Elder 		val |= u32_encode_bits(header_size, HDR_LEN_FMASK);
49784f9bd12SAlex Elder 
498f330fda3SAlex Elder 		/* Define how to fill fields in a received QMAP header */
4998730f45dSAlex Elder 		if (!endpoint->toward_ipa) {
5008730f45dSAlex Elder 			u32 off;	/* Field offset within header */
5018730f45dSAlex Elder 
5028730f45dSAlex Elder 			/* Where IPA will write the metadata value */
5038730f45dSAlex Elder 			off = offsetof(struct rmnet_map_header, mux_id);
5048730f45dSAlex Elder 			val |= u32_encode_bits(off, HDR_OFST_METADATA_FMASK);
5058730f45dSAlex Elder 
5068730f45dSAlex Elder 			/* Where IPA will write the length */
5078730f45dSAlex Elder 			off = offsetof(struct rmnet_map_header, pkt_len);
50884f9bd12SAlex Elder 			val |= HDR_OFST_PKT_SIZE_VALID_FMASK;
5098730f45dSAlex Elder 			val |= u32_encode_bits(off, HDR_OFST_PKT_SIZE_FMASK);
51084f9bd12SAlex Elder 		}
5118730f45dSAlex Elder 		/* For QMAP TX, metadata offset is 0 (modem assumes this) */
5128730f45dSAlex Elder 		val |= HDR_OFST_METADATA_VALID_FMASK;
5138730f45dSAlex Elder 
5148730f45dSAlex Elder 		/* HDR_ADDITIONAL_CONST_LEN is 0; (RX only) */
51584f9bd12SAlex Elder 		/* HDR_A5_MUX is 0 */
51684f9bd12SAlex Elder 		/* HDR_LEN_INC_DEAGG_HDR is 0 */
5178730f45dSAlex Elder 		/* HDR_METADATA_REG_VALID is 0 (TX only) */
51884f9bd12SAlex Elder 	}
51984f9bd12SAlex Elder 
52084f9bd12SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
52184f9bd12SAlex Elder }
52284f9bd12SAlex Elder 
52384f9bd12SAlex Elder static void ipa_endpoint_init_hdr_ext(struct ipa_endpoint *endpoint)
52484f9bd12SAlex Elder {
52584f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_HDR_EXT_N_OFFSET(endpoint->endpoint_id);
52684f9bd12SAlex Elder 	u32 pad_align = endpoint->data->rx.pad_align;
52784f9bd12SAlex Elder 	u32 val = 0;
52884f9bd12SAlex Elder 
52984f9bd12SAlex Elder 	val |= HDR_ENDIANNESS_FMASK;		/* big endian */
530f330fda3SAlex Elder 
531f330fda3SAlex Elder 	/* A QMAP header contains a 6 bit pad field at offset 0.  The RMNet
532f330fda3SAlex Elder 	 * driver assumes this field is meaningful in packets it receives,
533f330fda3SAlex Elder 	 * and assumes the header's payload length includes that padding.
534f330fda3SAlex Elder 	 * The RMNet driver does *not* pad packets it sends, however, so
535f330fda3SAlex Elder 	 * the pad field (although 0) should be ignored.
536f330fda3SAlex Elder 	 */
537f330fda3SAlex Elder 	if (endpoint->data->qmap && !endpoint->toward_ipa) {
53884f9bd12SAlex Elder 		val |= HDR_TOTAL_LEN_OR_PAD_VALID_FMASK;
53984f9bd12SAlex Elder 		/* HDR_TOTAL_LEN_OR_PAD is 0 (pad, not total_len) */
540f330fda3SAlex Elder 		val |= HDR_PAYLOAD_LEN_INC_PADDING_FMASK;
54184f9bd12SAlex Elder 		/* HDR_TOTAL_LEN_OR_PAD_OFFSET is 0 */
542f330fda3SAlex Elder 	}
543f330fda3SAlex Elder 
544f330fda3SAlex Elder 	/* HDR_PAYLOAD_LEN_INC_PADDING is 0 */
54584f9bd12SAlex Elder 	if (!endpoint->toward_ipa)
54684f9bd12SAlex Elder 		val |= u32_encode_bits(pad_align, HDR_PAD_TO_ALIGNMENT_FMASK);
54784f9bd12SAlex Elder 
54884f9bd12SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
54984f9bd12SAlex Elder }
55084f9bd12SAlex Elder 
55184f9bd12SAlex Elder 
55284f9bd12SAlex Elder static void ipa_endpoint_init_hdr_metadata_mask(struct ipa_endpoint *endpoint)
55384f9bd12SAlex Elder {
55484f9bd12SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
55584f9bd12SAlex Elder 	u32 val = 0;
55684f9bd12SAlex Elder 	u32 offset;
55784f9bd12SAlex Elder 
558fb57c3eaSAlex Elder 	if (endpoint->toward_ipa)
559fb57c3eaSAlex Elder 		return;		/* Register not valid for TX endpoints */
560fb57c3eaSAlex Elder 
56184f9bd12SAlex Elder 	offset = IPA_REG_ENDP_INIT_HDR_METADATA_MASK_N_OFFSET(endpoint_id);
56284f9bd12SAlex Elder 
5638730f45dSAlex Elder 	/* Note that HDR_ENDIANNESS indicates big endian header fields */
5649b63f093SAlex Elder 	if (endpoint->data->qmap)
5658730f45dSAlex Elder 		val = cpu_to_be32(IPA_ENDPOINT_QMAP_METADATA_MASK);
56684f9bd12SAlex Elder 
56784f9bd12SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
56884f9bd12SAlex Elder }
56984f9bd12SAlex Elder 
57084f9bd12SAlex Elder static void ipa_endpoint_init_mode(struct ipa_endpoint *endpoint)
57184f9bd12SAlex Elder {
57284f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_MODE_N_OFFSET(endpoint->endpoint_id);
57384f9bd12SAlex Elder 	u32 val;
57484f9bd12SAlex Elder 
575fb57c3eaSAlex Elder 	if (!endpoint->toward_ipa)
576fb57c3eaSAlex Elder 		return;		/* Register not valid for RX endpoints */
577fb57c3eaSAlex Elder 
57800b9102aSAlex Elder 	if (endpoint->data->dma_mode) {
57984f9bd12SAlex Elder 		enum ipa_endpoint_name name = endpoint->data->dma_endpoint;
58084f9bd12SAlex Elder 		u32 dma_endpoint_id;
58184f9bd12SAlex Elder 
58284f9bd12SAlex Elder 		dma_endpoint_id = endpoint->ipa->name_map[name]->endpoint_id;
58384f9bd12SAlex Elder 
58484f9bd12SAlex Elder 		val = u32_encode_bits(IPA_DMA, MODE_FMASK);
58584f9bd12SAlex Elder 		val |= u32_encode_bits(dma_endpoint_id, DEST_PIPE_INDEX_FMASK);
58684f9bd12SAlex Elder 	} else {
58784f9bd12SAlex Elder 		val = u32_encode_bits(IPA_BASIC, MODE_FMASK);
58884f9bd12SAlex Elder 	}
58900b9102aSAlex Elder 	/* All other bits unspecified (and 0) */
59084f9bd12SAlex Elder 
59184f9bd12SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
59284f9bd12SAlex Elder }
59384f9bd12SAlex Elder 
59484f9bd12SAlex Elder /* Compute the aggregation size value to use for a given buffer size */
59584f9bd12SAlex Elder static u32 ipa_aggr_size_kb(u32 rx_buffer_size)
59684f9bd12SAlex Elder {
59784f9bd12SAlex Elder 	/* We don't use "hard byte limit" aggregation, so we define the
59884f9bd12SAlex Elder 	 * aggregation limit such that our buffer has enough space *after*
59984f9bd12SAlex Elder 	 * that limit to receive a full MTU of data, plus overhead.
60084f9bd12SAlex Elder 	 */
60184f9bd12SAlex Elder 	rx_buffer_size -= IPA_MTU + IPA_RX_BUFFER_OVERHEAD;
60284f9bd12SAlex Elder 
60384f9bd12SAlex Elder 	return rx_buffer_size / SZ_1K;
60484f9bd12SAlex Elder }
60584f9bd12SAlex Elder 
60684f9bd12SAlex Elder static void ipa_endpoint_init_aggr(struct ipa_endpoint *endpoint)
60784f9bd12SAlex Elder {
60884f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_AGGR_N_OFFSET(endpoint->endpoint_id);
60984f9bd12SAlex Elder 	u32 val = 0;
61084f9bd12SAlex Elder 
61184f9bd12SAlex Elder 	if (endpoint->data->aggregation) {
61284f9bd12SAlex Elder 		if (!endpoint->toward_ipa) {
61384f9bd12SAlex Elder 			u32 limit;
61484f9bd12SAlex Elder 
61584f9bd12SAlex Elder 			val |= u32_encode_bits(IPA_ENABLE_AGGR, AGGR_EN_FMASK);
61684f9bd12SAlex Elder 			val |= u32_encode_bits(IPA_GENERIC, AGGR_TYPE_FMASK);
6179e88cb5fSAlex Elder 
6189e88cb5fSAlex Elder 			limit = ipa_aggr_size_kb(IPA_RX_BUFFER_SIZE);
6199e88cb5fSAlex Elder 			val |= u32_encode_bits(limit, AGGR_BYTE_LIMIT_FMASK);
6201d86652bSAlex Elder 
62184f9bd12SAlex Elder 			limit = IPA_AGGR_TIME_LIMIT_DEFAULT;
6221d86652bSAlex Elder 			limit = DIV_ROUND_CLOSEST(limit, IPA_AGGR_GRANULARITY);
6231d86652bSAlex Elder 			val |= u32_encode_bits(limit, AGGR_TIME_LIMIT_FMASK);
6241d86652bSAlex Elder 
6259e88cb5fSAlex Elder 			/* AGGR_PKT_LIMIT is 0 (unlimited) */
6269e88cb5fSAlex Elder 
62784f9bd12SAlex Elder 			if (endpoint->data->rx.aggr_close_eof)
62884f9bd12SAlex Elder 				val |= AGGR_SW_EOF_ACTIVE_FMASK;
62984f9bd12SAlex Elder 			/* AGGR_HARD_BYTE_LIMIT_ENABLE is 0 */
63084f9bd12SAlex Elder 		} else {
63184f9bd12SAlex Elder 			val |= u32_encode_bits(IPA_ENABLE_DEAGGR,
63284f9bd12SAlex Elder 					       AGGR_EN_FMASK);
63384f9bd12SAlex Elder 			val |= u32_encode_bits(IPA_QCMAP, AGGR_TYPE_FMASK);
63484f9bd12SAlex Elder 			/* other fields ignored */
63584f9bd12SAlex Elder 		}
63684f9bd12SAlex Elder 		/* AGGR_FORCE_CLOSE is 0 */
63784f9bd12SAlex Elder 	} else {
63884f9bd12SAlex Elder 		val |= u32_encode_bits(IPA_BYPASS_AGGR, AGGR_EN_FMASK);
63984f9bd12SAlex Elder 		/* other fields ignored */
64084f9bd12SAlex Elder 	}
64184f9bd12SAlex Elder 
64284f9bd12SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
64384f9bd12SAlex Elder }
64484f9bd12SAlex Elder 
645f13a8c31SAlex Elder /* The head-of-line blocking timer is defined as a tick count, where each
646f13a8c31SAlex Elder  * tick represents 128 cycles of the IPA core clock.  Return the value
647f13a8c31SAlex Elder  * that should be written to that register that represents the timeout
648f13a8c31SAlex Elder  * period provided.
649f13a8c31SAlex Elder  */
65084f9bd12SAlex Elder static u32 ipa_reg_init_hol_block_timer_val(struct ipa *ipa, u32 microseconds)
65184f9bd12SAlex Elder {
652f13a8c31SAlex Elder 	u32 width;
65384f9bd12SAlex Elder 	u32 scale;
654f13a8c31SAlex Elder 	u64 ticks;
655f13a8c31SAlex Elder 	u64 rate;
656f13a8c31SAlex Elder 	u32 high;
65784f9bd12SAlex Elder 	u32 val;
65884f9bd12SAlex Elder 
65984f9bd12SAlex Elder 	if (!microseconds)
660f13a8c31SAlex Elder 		return 0;	/* Nothing to compute if timer period is 0 */
66184f9bd12SAlex Elder 
662f13a8c31SAlex Elder 	/* Use 64 bit arithmetic to avoid overflow... */
663f13a8c31SAlex Elder 	rate = ipa_clock_rate(ipa);
664f13a8c31SAlex Elder 	ticks = DIV_ROUND_CLOSEST(microseconds * rate, 128 * USEC_PER_SEC);
665f13a8c31SAlex Elder 	/* ...but we still need to fit into a 32-bit register */
666f13a8c31SAlex Elder 	WARN_ON(ticks > U32_MAX);
66784f9bd12SAlex Elder 
668f13a8c31SAlex Elder 	/* IPA v3.5.1 just records the tick count */
669f13a8c31SAlex Elder 	if (ipa->version == IPA_VERSION_3_5_1)
670f13a8c31SAlex Elder 		return (u32)ticks;
67184f9bd12SAlex Elder 
672f13a8c31SAlex Elder 	/* For IPA v4.2, the tick count is represented by base and
673f13a8c31SAlex Elder 	 * scale fields within the 32-bit timer register, where:
674f13a8c31SAlex Elder 	 *     ticks = base << scale;
675f13a8c31SAlex Elder 	 * The best precision is achieved when the base value is as
676f13a8c31SAlex Elder 	 * large as possible.  Find the highest set bit in the tick
677f13a8c31SAlex Elder 	 * count, and extract the number of bits in the base field
678f13a8c31SAlex Elder 	 * such that that high bit is included.
679f13a8c31SAlex Elder 	 */
680f13a8c31SAlex Elder 	high = fls(ticks);		/* 1..32 */
681f13a8c31SAlex Elder 	width = HWEIGHT32(BASE_VALUE_FMASK);
682f13a8c31SAlex Elder 	scale = high > width ? high - width : 0;
683f13a8c31SAlex Elder 	if (scale) {
684f13a8c31SAlex Elder 		/* If we're scaling, round up to get a closer result */
685f13a8c31SAlex Elder 		ticks += 1 << (scale - 1);
686f13a8c31SAlex Elder 		/* High bit was set, so rounding might have affected it */
687f13a8c31SAlex Elder 		if (fls(ticks) != high)
688f13a8c31SAlex Elder 			scale++;
689f13a8c31SAlex Elder 	}
69084f9bd12SAlex Elder 
69184f9bd12SAlex Elder 	val = u32_encode_bits(scale, SCALE_FMASK);
692f13a8c31SAlex Elder 	val |= u32_encode_bits(ticks >> scale, BASE_VALUE_FMASK);
69384f9bd12SAlex Elder 
69484f9bd12SAlex Elder 	return val;
69584f9bd12SAlex Elder }
69684f9bd12SAlex Elder 
697f13a8c31SAlex Elder /* If microseconds is 0, timeout is immediate */
698f13a8c31SAlex Elder static void ipa_endpoint_init_hol_block_timer(struct ipa_endpoint *endpoint,
69984f9bd12SAlex Elder 					      u32 microseconds)
70084f9bd12SAlex Elder {
70184f9bd12SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
70284f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
70384f9bd12SAlex Elder 	u32 offset;
70484f9bd12SAlex Elder 	u32 val;
70584f9bd12SAlex Elder 
70684f9bd12SAlex Elder 	offset = IPA_REG_ENDP_INIT_HOL_BLOCK_TIMER_N_OFFSET(endpoint_id);
707f13a8c31SAlex Elder 	val = ipa_reg_init_hol_block_timer_val(ipa, microseconds);
70884f9bd12SAlex Elder 	iowrite32(val, ipa->reg_virt + offset);
70984f9bd12SAlex Elder }
71084f9bd12SAlex Elder 
71184f9bd12SAlex Elder static void
71284f9bd12SAlex Elder ipa_endpoint_init_hol_block_enable(struct ipa_endpoint *endpoint, bool enable)
71384f9bd12SAlex Elder {
71484f9bd12SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
71584f9bd12SAlex Elder 	u32 offset;
71684f9bd12SAlex Elder 	u32 val;
71784f9bd12SAlex Elder 
718547c8788SAlex Elder 	val = enable ? HOL_BLOCK_EN_FMASK : 0;
71984f9bd12SAlex Elder 	offset = IPA_REG_ENDP_INIT_HOL_BLOCK_EN_N_OFFSET(endpoint_id);
72084f9bd12SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
72184f9bd12SAlex Elder }
72284f9bd12SAlex Elder 
72384f9bd12SAlex Elder void ipa_endpoint_modem_hol_block_clear_all(struct ipa *ipa)
72484f9bd12SAlex Elder {
72584f9bd12SAlex Elder 	u32 i;
72684f9bd12SAlex Elder 
72784f9bd12SAlex Elder 	for (i = 0; i < IPA_ENDPOINT_MAX; i++) {
72884f9bd12SAlex Elder 		struct ipa_endpoint *endpoint = &ipa->endpoint[i];
72984f9bd12SAlex Elder 
730f8d34dfdSAlex Elder 		if (endpoint->toward_ipa || endpoint->ee_id != GSI_EE_MODEM)
73184f9bd12SAlex Elder 			continue;
73284f9bd12SAlex Elder 
733f13a8c31SAlex Elder 		ipa_endpoint_init_hol_block_timer(endpoint, 0);
73484f9bd12SAlex Elder 		ipa_endpoint_init_hol_block_enable(endpoint, true);
73584f9bd12SAlex Elder 	}
73684f9bd12SAlex Elder }
73784f9bd12SAlex Elder 
73884f9bd12SAlex Elder static void ipa_endpoint_init_deaggr(struct ipa_endpoint *endpoint)
73984f9bd12SAlex Elder {
74084f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_DEAGGR_N_OFFSET(endpoint->endpoint_id);
74184f9bd12SAlex Elder 	u32 val = 0;
74284f9bd12SAlex Elder 
743fb57c3eaSAlex Elder 	if (!endpoint->toward_ipa)
744fb57c3eaSAlex Elder 		return;		/* Register not valid for RX endpoints */
745fb57c3eaSAlex Elder 
74684f9bd12SAlex Elder 	/* DEAGGR_HDR_LEN is 0 */
74784f9bd12SAlex Elder 	/* PACKET_OFFSET_VALID is 0 */
74884f9bd12SAlex Elder 	/* PACKET_OFFSET_LOCATION is ignored (not valid) */
74984f9bd12SAlex Elder 	/* MAX_PACKET_LEN is 0 (not enforced) */
75084f9bd12SAlex Elder 
75184f9bd12SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
75284f9bd12SAlex Elder }
75384f9bd12SAlex Elder 
7542d265342SAlex Elder static void ipa_endpoint_init_rsrc_grp(struct ipa_endpoint *endpoint)
7552d265342SAlex Elder {
7562d265342SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_RSRC_GRP_N_OFFSET(endpoint->endpoint_id);
7572d265342SAlex Elder 	struct ipa *ipa = endpoint->ipa;
7582d265342SAlex Elder 	u32 val;
7592d265342SAlex Elder 
7602d265342SAlex Elder 	val = rsrc_grp_encoded(ipa->version, endpoint->data->resource_group);
7612d265342SAlex Elder 	iowrite32(val, ipa->reg_virt + offset);
7622d265342SAlex Elder }
7632d265342SAlex Elder 
76484f9bd12SAlex Elder static void ipa_endpoint_init_seq(struct ipa_endpoint *endpoint)
76584f9bd12SAlex Elder {
76684f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_SEQ_N_OFFSET(endpoint->endpoint_id);
76784f9bd12SAlex Elder 	u32 seq_type = endpoint->seq_type;
76884f9bd12SAlex Elder 	u32 val = 0;
76984f9bd12SAlex Elder 
770fb57c3eaSAlex Elder 	if (!endpoint->toward_ipa)
771fb57c3eaSAlex Elder 		return;		/* Register not valid for RX endpoints */
772fb57c3eaSAlex Elder 
773636edeaaSAlex Elder 	/* Sequencer type is made up of four nibbles */
77484f9bd12SAlex Elder 	val |= u32_encode_bits(seq_type & 0xf, HPS_SEQ_TYPE_FMASK);
77584f9bd12SAlex Elder 	val |= u32_encode_bits((seq_type >> 4) & 0xf, DPS_SEQ_TYPE_FMASK);
776636edeaaSAlex Elder 	/* The second two apply to replicated packets */
777636edeaaSAlex Elder 	val |= u32_encode_bits((seq_type >> 8) & 0xf, HPS_REP_SEQ_TYPE_FMASK);
778636edeaaSAlex Elder 	val |= u32_encode_bits((seq_type >> 12) & 0xf, DPS_REP_SEQ_TYPE_FMASK);
77984f9bd12SAlex Elder 
78084f9bd12SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
78184f9bd12SAlex Elder }
78284f9bd12SAlex Elder 
78384f9bd12SAlex Elder /**
78484f9bd12SAlex Elder  * ipa_endpoint_skb_tx() - Transmit a socket buffer
78584f9bd12SAlex Elder  * @endpoint:	Endpoint pointer
78684f9bd12SAlex Elder  * @skb:	Socket buffer to send
78784f9bd12SAlex Elder  *
78884f9bd12SAlex Elder  * Returns:	0 if successful, or a negative error code
78984f9bd12SAlex Elder  */
79084f9bd12SAlex Elder int ipa_endpoint_skb_tx(struct ipa_endpoint *endpoint, struct sk_buff *skb)
79184f9bd12SAlex Elder {
79284f9bd12SAlex Elder 	struct gsi_trans *trans;
79384f9bd12SAlex Elder 	u32 nr_frags;
79484f9bd12SAlex Elder 	int ret;
79584f9bd12SAlex Elder 
79684f9bd12SAlex Elder 	/* Make sure source endpoint's TLV FIFO has enough entries to
79784f9bd12SAlex Elder 	 * hold the linear portion of the skb and all its fragments.
79884f9bd12SAlex Elder 	 * If not, see if we can linearize it before giving up.
79984f9bd12SAlex Elder 	 */
80084f9bd12SAlex Elder 	nr_frags = skb_shinfo(skb)->nr_frags;
80184f9bd12SAlex Elder 	if (1 + nr_frags > endpoint->trans_tre_max) {
80284f9bd12SAlex Elder 		if (skb_linearize(skb))
80384f9bd12SAlex Elder 			return -E2BIG;
80484f9bd12SAlex Elder 		nr_frags = 0;
80584f9bd12SAlex Elder 	}
80684f9bd12SAlex Elder 
80784f9bd12SAlex Elder 	trans = ipa_endpoint_trans_alloc(endpoint, 1 + nr_frags);
80884f9bd12SAlex Elder 	if (!trans)
80984f9bd12SAlex Elder 		return -EBUSY;
81084f9bd12SAlex Elder 
81184f9bd12SAlex Elder 	ret = gsi_trans_skb_add(trans, skb);
81284f9bd12SAlex Elder 	if (ret)
81384f9bd12SAlex Elder 		goto err_trans_free;
81484f9bd12SAlex Elder 	trans->data = skb;	/* transaction owns skb now */
81584f9bd12SAlex Elder 
81684f9bd12SAlex Elder 	gsi_trans_commit(trans, !netdev_xmit_more());
81784f9bd12SAlex Elder 
81884f9bd12SAlex Elder 	return 0;
81984f9bd12SAlex Elder 
82084f9bd12SAlex Elder err_trans_free:
82184f9bd12SAlex Elder 	gsi_trans_free(trans);
82284f9bd12SAlex Elder 
82384f9bd12SAlex Elder 	return -ENOMEM;
82484f9bd12SAlex Elder }
82584f9bd12SAlex Elder 
82684f9bd12SAlex Elder static void ipa_endpoint_status(struct ipa_endpoint *endpoint)
82784f9bd12SAlex Elder {
82884f9bd12SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
82984f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
83084f9bd12SAlex Elder 	u32 val = 0;
83184f9bd12SAlex Elder 	u32 offset;
83284f9bd12SAlex Elder 
83384f9bd12SAlex Elder 	offset = IPA_REG_ENDP_STATUS_N_OFFSET(endpoint_id);
83484f9bd12SAlex Elder 
83584f9bd12SAlex Elder 	if (endpoint->data->status_enable) {
83684f9bd12SAlex Elder 		val |= STATUS_EN_FMASK;
83784f9bd12SAlex Elder 		if (endpoint->toward_ipa) {
83884f9bd12SAlex Elder 			enum ipa_endpoint_name name;
83984f9bd12SAlex Elder 			u32 status_endpoint_id;
84084f9bd12SAlex Elder 
84184f9bd12SAlex Elder 			name = endpoint->data->tx.status_endpoint;
84284f9bd12SAlex Elder 			status_endpoint_id = ipa->name_map[name]->endpoint_id;
84384f9bd12SAlex Elder 
84484f9bd12SAlex Elder 			val |= u32_encode_bits(status_endpoint_id,
84584f9bd12SAlex Elder 					       STATUS_ENDP_FMASK);
84684f9bd12SAlex Elder 		}
84784f9bd12SAlex Elder 		/* STATUS_LOCATION is 0 (status element precedes packet) */
84884f9bd12SAlex Elder 		/* The next field is present for IPA v4.0 and above */
84984f9bd12SAlex Elder 		/* STATUS_PKT_SUPPRESS_FMASK is 0 */
85084f9bd12SAlex Elder 	}
85184f9bd12SAlex Elder 
85284f9bd12SAlex Elder 	iowrite32(val, ipa->reg_virt + offset);
85384f9bd12SAlex Elder }
85484f9bd12SAlex Elder 
85584f9bd12SAlex Elder static int ipa_endpoint_replenish_one(struct ipa_endpoint *endpoint)
85684f9bd12SAlex Elder {
85784f9bd12SAlex Elder 	struct gsi_trans *trans;
85884f9bd12SAlex Elder 	bool doorbell = false;
85984f9bd12SAlex Elder 	struct page *page;
86084f9bd12SAlex Elder 	u32 offset;
86184f9bd12SAlex Elder 	u32 len;
86284f9bd12SAlex Elder 	int ret;
86384f9bd12SAlex Elder 
8646fcd4224SAlex Elder 	page = dev_alloc_pages(get_order(IPA_RX_BUFFER_SIZE));
86584f9bd12SAlex Elder 	if (!page)
86684f9bd12SAlex Elder 		return -ENOMEM;
86784f9bd12SAlex Elder 
86884f9bd12SAlex Elder 	trans = ipa_endpoint_trans_alloc(endpoint, 1);
86984f9bd12SAlex Elder 	if (!trans)
87084f9bd12SAlex Elder 		goto err_free_pages;
87184f9bd12SAlex Elder 
87284f9bd12SAlex Elder 	/* Offset the buffer to make space for skb headroom */
87384f9bd12SAlex Elder 	offset = NET_SKB_PAD;
87484f9bd12SAlex Elder 	len = IPA_RX_BUFFER_SIZE - offset;
87584f9bd12SAlex Elder 
87684f9bd12SAlex Elder 	ret = gsi_trans_page_add(trans, page, len, offset);
87784f9bd12SAlex Elder 	if (ret)
87884f9bd12SAlex Elder 		goto err_trans_free;
87984f9bd12SAlex Elder 	trans->data = page;	/* transaction owns page now */
88084f9bd12SAlex Elder 
88184f9bd12SAlex Elder 	if (++endpoint->replenish_ready == IPA_REPLENISH_BATCH) {
88284f9bd12SAlex Elder 		doorbell = true;
88384f9bd12SAlex Elder 		endpoint->replenish_ready = 0;
88484f9bd12SAlex Elder 	}
88584f9bd12SAlex Elder 
88684f9bd12SAlex Elder 	gsi_trans_commit(trans, doorbell);
88784f9bd12SAlex Elder 
88884f9bd12SAlex Elder 	return 0;
88984f9bd12SAlex Elder 
89084f9bd12SAlex Elder err_trans_free:
89184f9bd12SAlex Elder 	gsi_trans_free(trans);
89284f9bd12SAlex Elder err_free_pages:
8936fcd4224SAlex Elder 	__free_pages(page, get_order(IPA_RX_BUFFER_SIZE));
89484f9bd12SAlex Elder 
89584f9bd12SAlex Elder 	return -ENOMEM;
89684f9bd12SAlex Elder }
89784f9bd12SAlex Elder 
89884f9bd12SAlex Elder /**
89984f9bd12SAlex Elder  * ipa_endpoint_replenish() - Replenish the Rx packets cache.
900e3eea08eSAlex Elder  * @endpoint:	Endpoint to be replenished
901e3eea08eSAlex Elder  * @count:	Number of buffers to send to hardware
90284f9bd12SAlex Elder  *
90384f9bd12SAlex Elder  * Allocate RX packet wrapper structures with maximal socket buffers
90484f9bd12SAlex Elder  * for an endpoint.  These are supplied to the hardware, which fills
90584f9bd12SAlex Elder  * them with incoming data.
90684f9bd12SAlex Elder  */
90784f9bd12SAlex Elder static void ipa_endpoint_replenish(struct ipa_endpoint *endpoint, u32 count)
90884f9bd12SAlex Elder {
90984f9bd12SAlex Elder 	struct gsi *gsi;
91084f9bd12SAlex Elder 	u32 backlog;
91184f9bd12SAlex Elder 
91284f9bd12SAlex Elder 	if (!endpoint->replenish_enabled) {
91384f9bd12SAlex Elder 		if (count)
91484f9bd12SAlex Elder 			atomic_add(count, &endpoint->replenish_saved);
91584f9bd12SAlex Elder 		return;
91684f9bd12SAlex Elder 	}
91784f9bd12SAlex Elder 
91884f9bd12SAlex Elder 
91984f9bd12SAlex Elder 	while (atomic_dec_not_zero(&endpoint->replenish_backlog))
92084f9bd12SAlex Elder 		if (ipa_endpoint_replenish_one(endpoint))
92184f9bd12SAlex Elder 			goto try_again_later;
92284f9bd12SAlex Elder 	if (count)
92384f9bd12SAlex Elder 		atomic_add(count, &endpoint->replenish_backlog);
92484f9bd12SAlex Elder 
92584f9bd12SAlex Elder 	return;
92684f9bd12SAlex Elder 
92784f9bd12SAlex Elder try_again_later:
92884f9bd12SAlex Elder 	/* The last one didn't succeed, so fix the backlog */
92984f9bd12SAlex Elder 	backlog = atomic_inc_return(&endpoint->replenish_backlog);
93084f9bd12SAlex Elder 
93184f9bd12SAlex Elder 	if (count)
93284f9bd12SAlex Elder 		atomic_add(count, &endpoint->replenish_backlog);
93384f9bd12SAlex Elder 
93484f9bd12SAlex Elder 	/* Whenever a receive buffer transaction completes we'll try to
93584f9bd12SAlex Elder 	 * replenish again.  It's unlikely, but if we fail to supply even
93684f9bd12SAlex Elder 	 * one buffer, nothing will trigger another replenish attempt.
93784f9bd12SAlex Elder 	 * Receive buffer transactions use one TRE, so schedule work to
93884f9bd12SAlex Elder 	 * try replenishing again if our backlog is *all* available TREs.
93984f9bd12SAlex Elder 	 */
94084f9bd12SAlex Elder 	gsi = &endpoint->ipa->gsi;
94184f9bd12SAlex Elder 	if (backlog == gsi_channel_tre_max(gsi, endpoint->channel_id))
94284f9bd12SAlex Elder 		schedule_delayed_work(&endpoint->replenish_work,
94384f9bd12SAlex Elder 				      msecs_to_jiffies(1));
94484f9bd12SAlex Elder }
94584f9bd12SAlex Elder 
94684f9bd12SAlex Elder static void ipa_endpoint_replenish_enable(struct ipa_endpoint *endpoint)
94784f9bd12SAlex Elder {
94884f9bd12SAlex Elder 	struct gsi *gsi = &endpoint->ipa->gsi;
94984f9bd12SAlex Elder 	u32 max_backlog;
95084f9bd12SAlex Elder 	u32 saved;
95184f9bd12SAlex Elder 
95284f9bd12SAlex Elder 	endpoint->replenish_enabled = true;
95384f9bd12SAlex Elder 	while ((saved = atomic_xchg(&endpoint->replenish_saved, 0)))
95484f9bd12SAlex Elder 		atomic_add(saved, &endpoint->replenish_backlog);
95584f9bd12SAlex Elder 
95684f9bd12SAlex Elder 	/* Start replenishing if hardware currently has no buffers */
95784f9bd12SAlex Elder 	max_backlog = gsi_channel_tre_max(gsi, endpoint->channel_id);
95884f9bd12SAlex Elder 	if (atomic_read(&endpoint->replenish_backlog) == max_backlog)
95984f9bd12SAlex Elder 		ipa_endpoint_replenish(endpoint, 0);
96084f9bd12SAlex Elder }
96184f9bd12SAlex Elder 
96284f9bd12SAlex Elder static void ipa_endpoint_replenish_disable(struct ipa_endpoint *endpoint)
96384f9bd12SAlex Elder {
96484f9bd12SAlex Elder 	u32 backlog;
96584f9bd12SAlex Elder 
96684f9bd12SAlex Elder 	endpoint->replenish_enabled = false;
96784f9bd12SAlex Elder 	while ((backlog = atomic_xchg(&endpoint->replenish_backlog, 0)))
96884f9bd12SAlex Elder 		atomic_add(backlog, &endpoint->replenish_saved);
96984f9bd12SAlex Elder }
97084f9bd12SAlex Elder 
97184f9bd12SAlex Elder static void ipa_endpoint_replenish_work(struct work_struct *work)
97284f9bd12SAlex Elder {
97384f9bd12SAlex Elder 	struct delayed_work *dwork = to_delayed_work(work);
97484f9bd12SAlex Elder 	struct ipa_endpoint *endpoint;
97584f9bd12SAlex Elder 
97684f9bd12SAlex Elder 	endpoint = container_of(dwork, struct ipa_endpoint, replenish_work);
97784f9bd12SAlex Elder 
97884f9bd12SAlex Elder 	ipa_endpoint_replenish(endpoint, 0);
97984f9bd12SAlex Elder }
98084f9bd12SAlex Elder 
98184f9bd12SAlex Elder static void ipa_endpoint_skb_copy(struct ipa_endpoint *endpoint,
98284f9bd12SAlex Elder 				  void *data, u32 len, u32 extra)
98384f9bd12SAlex Elder {
98484f9bd12SAlex Elder 	struct sk_buff *skb;
98584f9bd12SAlex Elder 
98684f9bd12SAlex Elder 	skb = __dev_alloc_skb(len, GFP_ATOMIC);
98784f9bd12SAlex Elder 	if (skb) {
98884f9bd12SAlex Elder 		skb_put(skb, len);
98984f9bd12SAlex Elder 		memcpy(skb->data, data, len);
99084f9bd12SAlex Elder 		skb->truesize += extra;
99184f9bd12SAlex Elder 	}
99284f9bd12SAlex Elder 
99384f9bd12SAlex Elder 	/* Now receive it, or drop it if there's no netdev */
99484f9bd12SAlex Elder 	if (endpoint->netdev)
99584f9bd12SAlex Elder 		ipa_modem_skb_rx(endpoint->netdev, skb);
99684f9bd12SAlex Elder 	else if (skb)
99784f9bd12SAlex Elder 		dev_kfree_skb_any(skb);
99884f9bd12SAlex Elder }
99984f9bd12SAlex Elder 
100084f9bd12SAlex Elder static bool ipa_endpoint_skb_build(struct ipa_endpoint *endpoint,
100184f9bd12SAlex Elder 				   struct page *page, u32 len)
100284f9bd12SAlex Elder {
100384f9bd12SAlex Elder 	struct sk_buff *skb;
100484f9bd12SAlex Elder 
100584f9bd12SAlex Elder 	/* Nothing to do if there's no netdev */
100684f9bd12SAlex Elder 	if (!endpoint->netdev)
100784f9bd12SAlex Elder 		return false;
100884f9bd12SAlex Elder 
100984f9bd12SAlex Elder 	/* assert(len <= SKB_WITH_OVERHEAD(IPA_RX_BUFFER_SIZE-NET_SKB_PAD)); */
101084f9bd12SAlex Elder 	skb = build_skb(page_address(page), IPA_RX_BUFFER_SIZE);
101184f9bd12SAlex Elder 	if (skb) {
101284f9bd12SAlex Elder 		/* Reserve the headroom and account for the data */
101384f9bd12SAlex Elder 		skb_reserve(skb, NET_SKB_PAD);
101484f9bd12SAlex Elder 		skb_put(skb, len);
101584f9bd12SAlex Elder 	}
101684f9bd12SAlex Elder 
101784f9bd12SAlex Elder 	/* Receive the buffer (or record drop if unable to build it) */
101884f9bd12SAlex Elder 	ipa_modem_skb_rx(endpoint->netdev, skb);
101984f9bd12SAlex Elder 
102084f9bd12SAlex Elder 	return skb != NULL;
102184f9bd12SAlex Elder }
102284f9bd12SAlex Elder 
102384f9bd12SAlex Elder /* The format of a packet status element is the same for several status
102445921390SAlex Elder  * types (opcodes).  Other types aren't currently supported.
102584f9bd12SAlex Elder  */
102684f9bd12SAlex Elder static bool ipa_status_format_packet(enum ipa_status_opcode opcode)
102784f9bd12SAlex Elder {
102884f9bd12SAlex Elder 	switch (opcode) {
102984f9bd12SAlex Elder 	case IPA_STATUS_OPCODE_PACKET:
103084f9bd12SAlex Elder 	case IPA_STATUS_OPCODE_DROPPED_PACKET:
103184f9bd12SAlex Elder 	case IPA_STATUS_OPCODE_SUSPENDED_PACKET:
103284f9bd12SAlex Elder 	case IPA_STATUS_OPCODE_PACKET_2ND_PASS:
103384f9bd12SAlex Elder 		return true;
103484f9bd12SAlex Elder 	default:
103584f9bd12SAlex Elder 		return false;
103684f9bd12SAlex Elder 	}
103784f9bd12SAlex Elder }
103884f9bd12SAlex Elder 
103984f9bd12SAlex Elder static bool ipa_endpoint_status_skip(struct ipa_endpoint *endpoint,
104084f9bd12SAlex Elder 				     const struct ipa_status *status)
104184f9bd12SAlex Elder {
104284f9bd12SAlex Elder 	u32 endpoint_id;
104384f9bd12SAlex Elder 
104484f9bd12SAlex Elder 	if (!ipa_status_format_packet(status->opcode))
104584f9bd12SAlex Elder 		return true;
104684f9bd12SAlex Elder 	if (!status->pkt_len)
104784f9bd12SAlex Elder 		return true;
104884f9bd12SAlex Elder 	endpoint_id = u32_get_bits(status->endp_dst_idx,
104984f9bd12SAlex Elder 				   IPA_STATUS_DST_IDX_FMASK);
105084f9bd12SAlex Elder 	if (endpoint_id != endpoint->endpoint_id)
105184f9bd12SAlex Elder 		return true;
105284f9bd12SAlex Elder 
105384f9bd12SAlex Elder 	return false;	/* Don't skip this packet, process it */
105484f9bd12SAlex Elder }
105584f9bd12SAlex Elder 
105684f9bd12SAlex Elder /* Return whether the status indicates the packet should be dropped */
105784f9bd12SAlex Elder static bool ipa_status_drop_packet(const struct ipa_status *status)
105884f9bd12SAlex Elder {
105984f9bd12SAlex Elder 	u32 val;
106084f9bd12SAlex Elder 
1061ab4f71e5SAlex Elder 	/* Deaggregation exceptions we drop; all other types we consume */
106284f9bd12SAlex Elder 	if (status->exception)
106384f9bd12SAlex Elder 		return status->exception == IPA_STATUS_EXCEPTION_DEAGGR;
106484f9bd12SAlex Elder 
106584f9bd12SAlex Elder 	/* Drop the packet if it fails to match a routing rule; otherwise no */
106684f9bd12SAlex Elder 	val = le32_get_bits(status->flags1, IPA_STATUS_FLAGS1_RT_RULE_ID_FMASK);
106784f9bd12SAlex Elder 
106884f9bd12SAlex Elder 	return val == field_max(IPA_STATUS_FLAGS1_RT_RULE_ID_FMASK);
106984f9bd12SAlex Elder }
107084f9bd12SAlex Elder 
107184f9bd12SAlex Elder static void ipa_endpoint_status_parse(struct ipa_endpoint *endpoint,
107284f9bd12SAlex Elder 				      struct page *page, u32 total_len)
107384f9bd12SAlex Elder {
107484f9bd12SAlex Elder 	void *data = page_address(page) + NET_SKB_PAD;
107584f9bd12SAlex Elder 	u32 unused = IPA_RX_BUFFER_SIZE - total_len;
107684f9bd12SAlex Elder 	u32 resid = total_len;
107784f9bd12SAlex Elder 
107884f9bd12SAlex Elder 	while (resid) {
107984f9bd12SAlex Elder 		const struct ipa_status *status = data;
108084f9bd12SAlex Elder 		u32 align;
108184f9bd12SAlex Elder 		u32 len;
108284f9bd12SAlex Elder 
108384f9bd12SAlex Elder 		if (resid < sizeof(*status)) {
108484f9bd12SAlex Elder 			dev_err(&endpoint->ipa->pdev->dev,
108584f9bd12SAlex Elder 				"short message (%u bytes < %zu byte status)\n",
108684f9bd12SAlex Elder 				resid, sizeof(*status));
108784f9bd12SAlex Elder 			break;
108884f9bd12SAlex Elder 		}
108984f9bd12SAlex Elder 
109084f9bd12SAlex Elder 		/* Skip over status packets that lack packet data */
109184f9bd12SAlex Elder 		if (ipa_endpoint_status_skip(endpoint, status)) {
109284f9bd12SAlex Elder 			data += sizeof(*status);
109384f9bd12SAlex Elder 			resid -= sizeof(*status);
109484f9bd12SAlex Elder 			continue;
109584f9bd12SAlex Elder 		}
109684f9bd12SAlex Elder 
109784f9bd12SAlex Elder 		/* Compute the amount of buffer space consumed by the
109884f9bd12SAlex Elder 		 * packet, including the status element.  If the hardware
109984f9bd12SAlex Elder 		 * is configured to pad packet data to an aligned boundary,
110084f9bd12SAlex Elder 		 * account for that.  And if checksum offload is is enabled
110184f9bd12SAlex Elder 		 * a trailer containing computed checksum information will
110284f9bd12SAlex Elder 		 * be appended.
110384f9bd12SAlex Elder 		 */
110484f9bd12SAlex Elder 		align = endpoint->data->rx.pad_align ? : 1;
110584f9bd12SAlex Elder 		len = le16_to_cpu(status->pkt_len);
110684f9bd12SAlex Elder 		len = sizeof(*status) + ALIGN(len, align);
110784f9bd12SAlex Elder 		if (endpoint->data->checksum)
110884f9bd12SAlex Elder 			len += sizeof(struct rmnet_map_dl_csum_trailer);
110984f9bd12SAlex Elder 
111084f9bd12SAlex Elder 		/* Charge the new packet with a proportional fraction of
111184f9bd12SAlex Elder 		 * the unused space in the original receive buffer.
111284f9bd12SAlex Elder 		 * XXX Charge a proportion of the *whole* receive buffer?
111384f9bd12SAlex Elder 		 */
111484f9bd12SAlex Elder 		if (!ipa_status_drop_packet(status)) {
111584f9bd12SAlex Elder 			u32 extra = unused * len / total_len;
111684f9bd12SAlex Elder 			void *data2 = data + sizeof(*status);
111784f9bd12SAlex Elder 			u32 len2 = le16_to_cpu(status->pkt_len);
111884f9bd12SAlex Elder 
111984f9bd12SAlex Elder 			/* Client receives only packet data (no status) */
112084f9bd12SAlex Elder 			ipa_endpoint_skb_copy(endpoint, data2, len2, extra);
112184f9bd12SAlex Elder 		}
112284f9bd12SAlex Elder 
112384f9bd12SAlex Elder 		/* Consume status and the full packet it describes */
112484f9bd12SAlex Elder 		data += len;
112584f9bd12SAlex Elder 		resid -= len;
112684f9bd12SAlex Elder 	}
112784f9bd12SAlex Elder }
112884f9bd12SAlex Elder 
112984f9bd12SAlex Elder /* Complete a TX transaction, command or from ipa_endpoint_skb_tx() */
113084f9bd12SAlex Elder static void ipa_endpoint_tx_complete(struct ipa_endpoint *endpoint,
113184f9bd12SAlex Elder 				     struct gsi_trans *trans)
113284f9bd12SAlex Elder {
113384f9bd12SAlex Elder }
113484f9bd12SAlex Elder 
113584f9bd12SAlex Elder /* Complete transaction initiated in ipa_endpoint_replenish_one() */
113684f9bd12SAlex Elder static void ipa_endpoint_rx_complete(struct ipa_endpoint *endpoint,
113784f9bd12SAlex Elder 				     struct gsi_trans *trans)
113884f9bd12SAlex Elder {
113984f9bd12SAlex Elder 	struct page *page;
114084f9bd12SAlex Elder 
114184f9bd12SAlex Elder 	ipa_endpoint_replenish(endpoint, 1);
114284f9bd12SAlex Elder 
114384f9bd12SAlex Elder 	if (trans->cancelled)
114484f9bd12SAlex Elder 		return;
114584f9bd12SAlex Elder 
114684f9bd12SAlex Elder 	/* Parse or build a socket buffer using the actual received length */
114784f9bd12SAlex Elder 	page = trans->data;
114884f9bd12SAlex Elder 	if (endpoint->data->status_enable)
114984f9bd12SAlex Elder 		ipa_endpoint_status_parse(endpoint, page, trans->len);
115084f9bd12SAlex Elder 	else if (ipa_endpoint_skb_build(endpoint, page, trans->len))
115184f9bd12SAlex Elder 		trans->data = NULL;	/* Pages have been consumed */
115284f9bd12SAlex Elder }
115384f9bd12SAlex Elder 
115484f9bd12SAlex Elder void ipa_endpoint_trans_complete(struct ipa_endpoint *endpoint,
115584f9bd12SAlex Elder 				 struct gsi_trans *trans)
115684f9bd12SAlex Elder {
115784f9bd12SAlex Elder 	if (endpoint->toward_ipa)
115884f9bd12SAlex Elder 		ipa_endpoint_tx_complete(endpoint, trans);
115984f9bd12SAlex Elder 	else
116084f9bd12SAlex Elder 		ipa_endpoint_rx_complete(endpoint, trans);
116184f9bd12SAlex Elder }
116284f9bd12SAlex Elder 
116384f9bd12SAlex Elder void ipa_endpoint_trans_release(struct ipa_endpoint *endpoint,
116484f9bd12SAlex Elder 				struct gsi_trans *trans)
116584f9bd12SAlex Elder {
116684f9bd12SAlex Elder 	if (endpoint->toward_ipa) {
116784f9bd12SAlex Elder 		struct ipa *ipa = endpoint->ipa;
116884f9bd12SAlex Elder 
116984f9bd12SAlex Elder 		/* Nothing to do for command transactions */
117084f9bd12SAlex Elder 		if (endpoint != ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]) {
117184f9bd12SAlex Elder 			struct sk_buff *skb = trans->data;
117284f9bd12SAlex Elder 
117384f9bd12SAlex Elder 			if (skb)
117484f9bd12SAlex Elder 				dev_kfree_skb_any(skb);
117584f9bd12SAlex Elder 		}
117684f9bd12SAlex Elder 	} else {
117784f9bd12SAlex Elder 		struct page *page = trans->data;
117884f9bd12SAlex Elder 
117984f9bd12SAlex Elder 		if (page)
11806fcd4224SAlex Elder 			__free_pages(page, get_order(IPA_RX_BUFFER_SIZE));
118184f9bd12SAlex Elder 	}
118284f9bd12SAlex Elder }
118384f9bd12SAlex Elder 
118484f9bd12SAlex Elder void ipa_endpoint_default_route_set(struct ipa *ipa, u32 endpoint_id)
118584f9bd12SAlex Elder {
118684f9bd12SAlex Elder 	u32 val;
118784f9bd12SAlex Elder 
118884f9bd12SAlex Elder 	/* ROUTE_DIS is 0 */
118984f9bd12SAlex Elder 	val = u32_encode_bits(endpoint_id, ROUTE_DEF_PIPE_FMASK);
119084f9bd12SAlex Elder 	val |= ROUTE_DEF_HDR_TABLE_FMASK;
119184f9bd12SAlex Elder 	val |= u32_encode_bits(0, ROUTE_DEF_HDR_OFST_FMASK);
119284f9bd12SAlex Elder 	val |= u32_encode_bits(endpoint_id, ROUTE_FRAG_DEF_PIPE_FMASK);
119384f9bd12SAlex Elder 	val |= ROUTE_DEF_RETAIN_HDR_FMASK;
119484f9bd12SAlex Elder 
119584f9bd12SAlex Elder 	iowrite32(val, ipa->reg_virt + IPA_REG_ROUTE_OFFSET);
119684f9bd12SAlex Elder }
119784f9bd12SAlex Elder 
119884f9bd12SAlex Elder void ipa_endpoint_default_route_clear(struct ipa *ipa)
119984f9bd12SAlex Elder {
120084f9bd12SAlex Elder 	ipa_endpoint_default_route_set(ipa, 0);
120184f9bd12SAlex Elder }
120284f9bd12SAlex Elder 
120384f9bd12SAlex Elder /**
120484f9bd12SAlex Elder  * ipa_endpoint_reset_rx_aggr() - Reset RX endpoint with aggregation active
120584f9bd12SAlex Elder  * @endpoint:	Endpoint to be reset
120684f9bd12SAlex Elder  *
120784f9bd12SAlex Elder  * If aggregation is active on an RX endpoint when a reset is performed
120884f9bd12SAlex Elder  * on its underlying GSI channel, a special sequence of actions must be
120984f9bd12SAlex Elder  * taken to ensure the IPA pipeline is properly cleared.
121084f9bd12SAlex Elder  *
1211e3eea08eSAlex Elder  * Return:	0 if successful, or a negative error code
121284f9bd12SAlex Elder  */
121384f9bd12SAlex Elder static int ipa_endpoint_reset_rx_aggr(struct ipa_endpoint *endpoint)
121484f9bd12SAlex Elder {
121584f9bd12SAlex Elder 	struct device *dev = &endpoint->ipa->pdev->dev;
121684f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
121784f9bd12SAlex Elder 	struct gsi *gsi = &ipa->gsi;
12184fa95248SAlex Elder 	bool suspended = false;
121984f9bd12SAlex Elder 	dma_addr_t addr;
122084f9bd12SAlex Elder 	u32 retries;
122184f9bd12SAlex Elder 	u32 len = 1;
122284f9bd12SAlex Elder 	void *virt;
122384f9bd12SAlex Elder 	int ret;
122484f9bd12SAlex Elder 
122584f9bd12SAlex Elder 	virt = kzalloc(len, GFP_KERNEL);
122684f9bd12SAlex Elder 	if (!virt)
122784f9bd12SAlex Elder 		return -ENOMEM;
122884f9bd12SAlex Elder 
122984f9bd12SAlex Elder 	addr = dma_map_single(dev, virt, len, DMA_FROM_DEVICE);
123084f9bd12SAlex Elder 	if (dma_mapping_error(dev, addr)) {
123184f9bd12SAlex Elder 		ret = -ENOMEM;
123284f9bd12SAlex Elder 		goto out_kfree;
123384f9bd12SAlex Elder 	}
123484f9bd12SAlex Elder 
123584f9bd12SAlex Elder 	/* Force close aggregation before issuing the reset */
123684f9bd12SAlex Elder 	ipa_endpoint_force_close(endpoint);
123784f9bd12SAlex Elder 
123884f9bd12SAlex Elder 	/* Reset and reconfigure the channel with the doorbell engine
123984f9bd12SAlex Elder 	 * disabled.  Then poll until we know aggregation is no longer
124084f9bd12SAlex Elder 	 * active.  We'll re-enable the doorbell (if appropriate) when
124184f9bd12SAlex Elder 	 * we reset again below.
124284f9bd12SAlex Elder 	 */
124384f9bd12SAlex Elder 	gsi_channel_reset(gsi, endpoint->channel_id, false);
124484f9bd12SAlex Elder 
124584f9bd12SAlex Elder 	/* Make sure the channel isn't suspended */
12464fa95248SAlex Elder 	suspended = ipa_endpoint_program_suspend(endpoint, false);
124784f9bd12SAlex Elder 
124884f9bd12SAlex Elder 	/* Start channel and do a 1 byte read */
124984f9bd12SAlex Elder 	ret = gsi_channel_start(gsi, endpoint->channel_id);
125084f9bd12SAlex Elder 	if (ret)
125184f9bd12SAlex Elder 		goto out_suspend_again;
125284f9bd12SAlex Elder 
125384f9bd12SAlex Elder 	ret = gsi_trans_read_byte(gsi, endpoint->channel_id, addr);
125484f9bd12SAlex Elder 	if (ret)
125584f9bd12SAlex Elder 		goto err_endpoint_stop;
125684f9bd12SAlex Elder 
125784f9bd12SAlex Elder 	/* Wait for aggregation to be closed on the channel */
125884f9bd12SAlex Elder 	retries = IPA_ENDPOINT_RESET_AGGR_RETRY_MAX;
125984f9bd12SAlex Elder 	do {
126084f9bd12SAlex Elder 		if (!ipa_endpoint_aggr_active(endpoint))
126184f9bd12SAlex Elder 			break;
126284f9bd12SAlex Elder 		msleep(1);
126384f9bd12SAlex Elder 	} while (retries--);
126484f9bd12SAlex Elder 
126584f9bd12SAlex Elder 	/* Check one last time */
126684f9bd12SAlex Elder 	if (ipa_endpoint_aggr_active(endpoint))
126784f9bd12SAlex Elder 		dev_err(dev, "endpoint %u still active during reset\n",
126884f9bd12SAlex Elder 			endpoint->endpoint_id);
126984f9bd12SAlex Elder 
127084f9bd12SAlex Elder 	gsi_trans_read_byte_done(gsi, endpoint->channel_id);
127184f9bd12SAlex Elder 
1272f30dcb7dSAlex Elder 	ret = gsi_channel_stop(gsi, endpoint->channel_id);
127384f9bd12SAlex Elder 	if (ret)
127484f9bd12SAlex Elder 		goto out_suspend_again;
127584f9bd12SAlex Elder 
127684f9bd12SAlex Elder 	/* Finally, reset and reconfigure the channel again (re-enabling the
127784f9bd12SAlex Elder 	 * the doorbell engine if appropriate).  Sleep for 1 millisecond to
127884f9bd12SAlex Elder 	 * complete the channel reset sequence.  Finish by suspending the
127984f9bd12SAlex Elder 	 * channel again (if necessary).
128084f9bd12SAlex Elder 	 */
1281*ce54993dSAlex Elder 	gsi_channel_reset(gsi, endpoint->channel_id, true);
128284f9bd12SAlex Elder 
128384f9bd12SAlex Elder 	msleep(1);
128484f9bd12SAlex Elder 
128584f9bd12SAlex Elder 	goto out_suspend_again;
128684f9bd12SAlex Elder 
128784f9bd12SAlex Elder err_endpoint_stop:
1288f30dcb7dSAlex Elder 	(void)gsi_channel_stop(gsi, endpoint->channel_id);
128984f9bd12SAlex Elder out_suspend_again:
12904fa95248SAlex Elder 	if (suspended)
12914fa95248SAlex Elder 		(void)ipa_endpoint_program_suspend(endpoint, true);
129284f9bd12SAlex Elder 	dma_unmap_single(dev, addr, len, DMA_FROM_DEVICE);
129384f9bd12SAlex Elder out_kfree:
129484f9bd12SAlex Elder 	kfree(virt);
129584f9bd12SAlex Elder 
129684f9bd12SAlex Elder 	return ret;
129784f9bd12SAlex Elder }
129884f9bd12SAlex Elder 
129984f9bd12SAlex Elder static void ipa_endpoint_reset(struct ipa_endpoint *endpoint)
130084f9bd12SAlex Elder {
130184f9bd12SAlex Elder 	u32 channel_id = endpoint->channel_id;
130284f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
130384f9bd12SAlex Elder 	bool special;
130484f9bd12SAlex Elder 	int ret = 0;
130584f9bd12SAlex Elder 
130684f9bd12SAlex Elder 	/* On IPA v3.5.1, if an RX endpoint is reset while aggregation
130784f9bd12SAlex Elder 	 * is active, we need to handle things specially to recover.
130884f9bd12SAlex Elder 	 * All other cases just need to reset the underlying GSI channel.
130984f9bd12SAlex Elder 	 */
1310*ce54993dSAlex Elder 	special = ipa->version == IPA_VERSION_3_5_1 &&
1311*ce54993dSAlex Elder 			!endpoint->toward_ipa &&
1312*ce54993dSAlex Elder 			endpoint->data->aggregation;
1313*ce54993dSAlex Elder 	if (special && ipa_endpoint_aggr_active(endpoint))
131484f9bd12SAlex Elder 		ret = ipa_endpoint_reset_rx_aggr(endpoint);
131584f9bd12SAlex Elder 	else
1316*ce54993dSAlex Elder 		gsi_channel_reset(&ipa->gsi, channel_id, true);
131784f9bd12SAlex Elder 
131884f9bd12SAlex Elder 	if (ret)
131984f9bd12SAlex Elder 		dev_err(&ipa->pdev->dev,
132084f9bd12SAlex Elder 			"error %d resetting channel %u for endpoint %u\n",
132184f9bd12SAlex Elder 			ret, endpoint->channel_id, endpoint->endpoint_id);
132284f9bd12SAlex Elder }
132384f9bd12SAlex Elder 
132484f9bd12SAlex Elder static void ipa_endpoint_program(struct ipa_endpoint *endpoint)
132584f9bd12SAlex Elder {
1326fb57c3eaSAlex Elder 	if (endpoint->toward_ipa)
1327a4dcad34SAlex Elder 		ipa_endpoint_program_delay(endpoint, false);
1328fb57c3eaSAlex Elder 	else
1329fb57c3eaSAlex Elder 		(void)ipa_endpoint_program_suspend(endpoint, false);
1330fb57c3eaSAlex Elder 	ipa_endpoint_init_cfg(endpoint);
1331fb57c3eaSAlex Elder 	ipa_endpoint_init_hdr(endpoint);
133284f9bd12SAlex Elder 	ipa_endpoint_init_hdr_ext(endpoint);
1333fb57c3eaSAlex Elder 	ipa_endpoint_init_hdr_metadata_mask(endpoint);
1334fb57c3eaSAlex Elder 	ipa_endpoint_init_mode(endpoint);
133584f9bd12SAlex Elder 	ipa_endpoint_init_aggr(endpoint);
133684f9bd12SAlex Elder 	ipa_endpoint_init_deaggr(endpoint);
13372d265342SAlex Elder 	ipa_endpoint_init_rsrc_grp(endpoint);
133884f9bd12SAlex Elder 	ipa_endpoint_init_seq(endpoint);
133984f9bd12SAlex Elder 	ipa_endpoint_status(endpoint);
134084f9bd12SAlex Elder }
134184f9bd12SAlex Elder 
134284f9bd12SAlex Elder int ipa_endpoint_enable_one(struct ipa_endpoint *endpoint)
134384f9bd12SAlex Elder {
134484f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
134584f9bd12SAlex Elder 	struct gsi *gsi = &ipa->gsi;
134684f9bd12SAlex Elder 	int ret;
134784f9bd12SAlex Elder 
134884f9bd12SAlex Elder 	ret = gsi_channel_start(gsi, endpoint->channel_id);
134984f9bd12SAlex Elder 	if (ret) {
135084f9bd12SAlex Elder 		dev_err(&ipa->pdev->dev,
135184f9bd12SAlex Elder 			"error %d starting %cX channel %u for endpoint %u\n",
135284f9bd12SAlex Elder 			ret, endpoint->toward_ipa ? 'T' : 'R',
135384f9bd12SAlex Elder 			endpoint->channel_id, endpoint->endpoint_id);
135484f9bd12SAlex Elder 		return ret;
135584f9bd12SAlex Elder 	}
135684f9bd12SAlex Elder 
135784f9bd12SAlex Elder 	if (!endpoint->toward_ipa) {
135884f9bd12SAlex Elder 		ipa_interrupt_suspend_enable(ipa->interrupt,
135984f9bd12SAlex Elder 					     endpoint->endpoint_id);
136084f9bd12SAlex Elder 		ipa_endpoint_replenish_enable(endpoint);
136184f9bd12SAlex Elder 	}
136284f9bd12SAlex Elder 
136384f9bd12SAlex Elder 	ipa->enabled |= BIT(endpoint->endpoint_id);
136484f9bd12SAlex Elder 
136584f9bd12SAlex Elder 	return 0;
136684f9bd12SAlex Elder }
136784f9bd12SAlex Elder 
136884f9bd12SAlex Elder void ipa_endpoint_disable_one(struct ipa_endpoint *endpoint)
136984f9bd12SAlex Elder {
137084f9bd12SAlex Elder 	u32 mask = BIT(endpoint->endpoint_id);
137184f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
1372f30dcb7dSAlex Elder 	struct gsi *gsi = &ipa->gsi;
137384f9bd12SAlex Elder 	int ret;
137484f9bd12SAlex Elder 
1375f30dcb7dSAlex Elder 	if (!(ipa->enabled & mask))
137684f9bd12SAlex Elder 		return;
137784f9bd12SAlex Elder 
1378f30dcb7dSAlex Elder 	ipa->enabled ^= mask;
137984f9bd12SAlex Elder 
138084f9bd12SAlex Elder 	if (!endpoint->toward_ipa) {
138184f9bd12SAlex Elder 		ipa_endpoint_replenish_disable(endpoint);
138284f9bd12SAlex Elder 		ipa_interrupt_suspend_disable(ipa->interrupt,
138384f9bd12SAlex Elder 					      endpoint->endpoint_id);
138484f9bd12SAlex Elder 	}
138584f9bd12SAlex Elder 
138684f9bd12SAlex Elder 	/* Note that if stop fails, the channel's state is not well-defined */
1387f30dcb7dSAlex Elder 	ret = gsi_channel_stop(gsi, endpoint->channel_id);
138884f9bd12SAlex Elder 	if (ret)
138984f9bd12SAlex Elder 		dev_err(&ipa->pdev->dev,
139084f9bd12SAlex Elder 			"error %d attempting to stop endpoint %u\n", ret,
139184f9bd12SAlex Elder 			endpoint->endpoint_id);
139284f9bd12SAlex Elder }
139384f9bd12SAlex Elder 
139484f9bd12SAlex Elder void ipa_endpoint_suspend_one(struct ipa_endpoint *endpoint)
139584f9bd12SAlex Elder {
139684f9bd12SAlex Elder 	struct device *dev = &endpoint->ipa->pdev->dev;
139784f9bd12SAlex Elder 	struct gsi *gsi = &endpoint->ipa->gsi;
139884f9bd12SAlex Elder 	bool stop_channel;
139984f9bd12SAlex Elder 	int ret;
140084f9bd12SAlex Elder 
140184f9bd12SAlex Elder 	if (!(endpoint->ipa->enabled & BIT(endpoint->endpoint_id)))
140284f9bd12SAlex Elder 		return;
140384f9bd12SAlex Elder 
1404ab4f71e5SAlex Elder 	if (!endpoint->toward_ipa) {
140584f9bd12SAlex Elder 		ipa_endpoint_replenish_disable(endpoint);
14064fa95248SAlex Elder 		(void)ipa_endpoint_program_suspend(endpoint, true);
1407ab4f71e5SAlex Elder 	}
140884f9bd12SAlex Elder 
1409b07f283eSAlex Elder 	/* IPA v3.5.1 doesn't use channel stop for suspend */
1410b07f283eSAlex Elder 	stop_channel = endpoint->ipa->version != IPA_VERSION_3_5_1;
141184f9bd12SAlex Elder 	ret = gsi_channel_suspend(gsi, endpoint->channel_id, stop_channel);
141284f9bd12SAlex Elder 	if (ret)
141384f9bd12SAlex Elder 		dev_err(dev, "error %d suspending channel %u\n", ret,
141484f9bd12SAlex Elder 			endpoint->channel_id);
141584f9bd12SAlex Elder }
141684f9bd12SAlex Elder 
141784f9bd12SAlex Elder void ipa_endpoint_resume_one(struct ipa_endpoint *endpoint)
141884f9bd12SAlex Elder {
141984f9bd12SAlex Elder 	struct device *dev = &endpoint->ipa->pdev->dev;
142084f9bd12SAlex Elder 	struct gsi *gsi = &endpoint->ipa->gsi;
142184f9bd12SAlex Elder 	bool start_channel;
142284f9bd12SAlex Elder 	int ret;
142384f9bd12SAlex Elder 
142484f9bd12SAlex Elder 	if (!(endpoint->ipa->enabled & BIT(endpoint->endpoint_id)))
142584f9bd12SAlex Elder 		return;
142684f9bd12SAlex Elder 
1427b07f283eSAlex Elder 	if (!endpoint->toward_ipa)
14284fa95248SAlex Elder 		(void)ipa_endpoint_program_suspend(endpoint, false);
142984f9bd12SAlex Elder 
1430b07f283eSAlex Elder 	/* IPA v3.5.1 doesn't use channel start for resume */
1431b07f283eSAlex Elder 	start_channel = endpoint->ipa->version != IPA_VERSION_3_5_1;
143284f9bd12SAlex Elder 	ret = gsi_channel_resume(gsi, endpoint->channel_id, start_channel);
143384f9bd12SAlex Elder 	if (ret)
143484f9bd12SAlex Elder 		dev_err(dev, "error %d resuming channel %u\n", ret,
143584f9bd12SAlex Elder 			endpoint->channel_id);
143684f9bd12SAlex Elder 	else if (!endpoint->toward_ipa)
143784f9bd12SAlex Elder 		ipa_endpoint_replenish_enable(endpoint);
143884f9bd12SAlex Elder }
143984f9bd12SAlex Elder 
144084f9bd12SAlex Elder void ipa_endpoint_suspend(struct ipa *ipa)
144184f9bd12SAlex Elder {
1442d1704382SAlex Elder 	if (!ipa->setup_complete)
1443d1704382SAlex Elder 		return;
1444d1704382SAlex Elder 
144584f9bd12SAlex Elder 	if (ipa->modem_netdev)
144684f9bd12SAlex Elder 		ipa_modem_suspend(ipa->modem_netdev);
144784f9bd12SAlex Elder 
14486cb63ea6SAlex Elder 	ipa_cmd_tag_process(ipa);
14496cb63ea6SAlex Elder 
145084f9bd12SAlex Elder 	ipa_endpoint_suspend_one(ipa->name_map[IPA_ENDPOINT_AP_LAN_RX]);
145184f9bd12SAlex Elder 	ipa_endpoint_suspend_one(ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]);
145284f9bd12SAlex Elder }
145384f9bd12SAlex Elder 
145484f9bd12SAlex Elder void ipa_endpoint_resume(struct ipa *ipa)
145584f9bd12SAlex Elder {
1456d1704382SAlex Elder 	if (!ipa->setup_complete)
1457d1704382SAlex Elder 		return;
1458d1704382SAlex Elder 
145984f9bd12SAlex Elder 	ipa_endpoint_resume_one(ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]);
146084f9bd12SAlex Elder 	ipa_endpoint_resume_one(ipa->name_map[IPA_ENDPOINT_AP_LAN_RX]);
146184f9bd12SAlex Elder 
146284f9bd12SAlex Elder 	if (ipa->modem_netdev)
146384f9bd12SAlex Elder 		ipa_modem_resume(ipa->modem_netdev);
146484f9bd12SAlex Elder }
146584f9bd12SAlex Elder 
146684f9bd12SAlex Elder static void ipa_endpoint_setup_one(struct ipa_endpoint *endpoint)
146784f9bd12SAlex Elder {
146884f9bd12SAlex Elder 	struct gsi *gsi = &endpoint->ipa->gsi;
146984f9bd12SAlex Elder 	u32 channel_id = endpoint->channel_id;
147084f9bd12SAlex Elder 
147184f9bd12SAlex Elder 	/* Only AP endpoints get set up */
147284f9bd12SAlex Elder 	if (endpoint->ee_id != GSI_EE_AP)
147384f9bd12SAlex Elder 		return;
147484f9bd12SAlex Elder 
147584f9bd12SAlex Elder 	endpoint->trans_tre_max = gsi_channel_trans_tre_max(gsi, channel_id);
147684f9bd12SAlex Elder 	if (!endpoint->toward_ipa) {
147784f9bd12SAlex Elder 		/* RX transactions require a single TRE, so the maximum
147884f9bd12SAlex Elder 		 * backlog is the same as the maximum outstanding TREs.
147984f9bd12SAlex Elder 		 */
148084f9bd12SAlex Elder 		endpoint->replenish_enabled = false;
148184f9bd12SAlex Elder 		atomic_set(&endpoint->replenish_saved,
148284f9bd12SAlex Elder 			   gsi_channel_tre_max(gsi, endpoint->channel_id));
148384f9bd12SAlex Elder 		atomic_set(&endpoint->replenish_backlog, 0);
148484f9bd12SAlex Elder 		INIT_DELAYED_WORK(&endpoint->replenish_work,
148584f9bd12SAlex Elder 				  ipa_endpoint_replenish_work);
148684f9bd12SAlex Elder 	}
148784f9bd12SAlex Elder 
148884f9bd12SAlex Elder 	ipa_endpoint_program(endpoint);
148984f9bd12SAlex Elder 
149084f9bd12SAlex Elder 	endpoint->ipa->set_up |= BIT(endpoint->endpoint_id);
149184f9bd12SAlex Elder }
149284f9bd12SAlex Elder 
149384f9bd12SAlex Elder static void ipa_endpoint_teardown_one(struct ipa_endpoint *endpoint)
149484f9bd12SAlex Elder {
149584f9bd12SAlex Elder 	endpoint->ipa->set_up &= ~BIT(endpoint->endpoint_id);
149684f9bd12SAlex Elder 
149784f9bd12SAlex Elder 	if (!endpoint->toward_ipa)
149884f9bd12SAlex Elder 		cancel_delayed_work_sync(&endpoint->replenish_work);
149984f9bd12SAlex Elder 
150084f9bd12SAlex Elder 	ipa_endpoint_reset(endpoint);
150184f9bd12SAlex Elder }
150284f9bd12SAlex Elder 
150384f9bd12SAlex Elder void ipa_endpoint_setup(struct ipa *ipa)
150484f9bd12SAlex Elder {
150584f9bd12SAlex Elder 	u32 initialized = ipa->initialized;
150684f9bd12SAlex Elder 
150784f9bd12SAlex Elder 	ipa->set_up = 0;
150884f9bd12SAlex Elder 	while (initialized) {
150984f9bd12SAlex Elder 		u32 endpoint_id = __ffs(initialized);
151084f9bd12SAlex Elder 
151184f9bd12SAlex Elder 		initialized ^= BIT(endpoint_id);
151284f9bd12SAlex Elder 
151384f9bd12SAlex Elder 		ipa_endpoint_setup_one(&ipa->endpoint[endpoint_id]);
151484f9bd12SAlex Elder 	}
151584f9bd12SAlex Elder }
151684f9bd12SAlex Elder 
151784f9bd12SAlex Elder void ipa_endpoint_teardown(struct ipa *ipa)
151884f9bd12SAlex Elder {
151984f9bd12SAlex Elder 	u32 set_up = ipa->set_up;
152084f9bd12SAlex Elder 
152184f9bd12SAlex Elder 	while (set_up) {
152284f9bd12SAlex Elder 		u32 endpoint_id = __fls(set_up);
152384f9bd12SAlex Elder 
152484f9bd12SAlex Elder 		set_up ^= BIT(endpoint_id);
152584f9bd12SAlex Elder 
152684f9bd12SAlex Elder 		ipa_endpoint_teardown_one(&ipa->endpoint[endpoint_id]);
152784f9bd12SAlex Elder 	}
152884f9bd12SAlex Elder 	ipa->set_up = 0;
152984f9bd12SAlex Elder }
153084f9bd12SAlex Elder 
153184f9bd12SAlex Elder int ipa_endpoint_config(struct ipa *ipa)
153284f9bd12SAlex Elder {
153384f9bd12SAlex Elder 	struct device *dev = &ipa->pdev->dev;
153484f9bd12SAlex Elder 	u32 initialized;
153584f9bd12SAlex Elder 	u32 rx_base;
153684f9bd12SAlex Elder 	u32 rx_mask;
153784f9bd12SAlex Elder 	u32 tx_mask;
153884f9bd12SAlex Elder 	int ret = 0;
153984f9bd12SAlex Elder 	u32 max;
154084f9bd12SAlex Elder 	u32 val;
154184f9bd12SAlex Elder 
154284f9bd12SAlex Elder 	/* Find out about the endpoints supplied by the hardware, and ensure
154384f9bd12SAlex Elder 	 * the highest one doesn't exceed the number we support.
154484f9bd12SAlex Elder 	 */
154584f9bd12SAlex Elder 	val = ioread32(ipa->reg_virt + IPA_REG_FLAVOR_0_OFFSET);
154684f9bd12SAlex Elder 
154784f9bd12SAlex Elder 	/* Our RX is an IPA producer */
154884f9bd12SAlex Elder 	rx_base = u32_get_bits(val, BAM_PROD_LOWEST_FMASK);
154984f9bd12SAlex Elder 	max = rx_base + u32_get_bits(val, BAM_MAX_PROD_PIPES_FMASK);
155084f9bd12SAlex Elder 	if (max > IPA_ENDPOINT_MAX) {
155184f9bd12SAlex Elder 		dev_err(dev, "too many endpoints (%u > %u)\n",
155284f9bd12SAlex Elder 			max, IPA_ENDPOINT_MAX);
155384f9bd12SAlex Elder 		return -EINVAL;
155484f9bd12SAlex Elder 	}
155584f9bd12SAlex Elder 	rx_mask = GENMASK(max - 1, rx_base);
155684f9bd12SAlex Elder 
155784f9bd12SAlex Elder 	/* Our TX is an IPA consumer */
155884f9bd12SAlex Elder 	max = u32_get_bits(val, BAM_MAX_CONS_PIPES_FMASK);
155984f9bd12SAlex Elder 	tx_mask = GENMASK(max - 1, 0);
156084f9bd12SAlex Elder 
156184f9bd12SAlex Elder 	ipa->available = rx_mask | tx_mask;
156284f9bd12SAlex Elder 
156384f9bd12SAlex Elder 	/* Check for initialized endpoints not supported by the hardware */
156484f9bd12SAlex Elder 	if (ipa->initialized & ~ipa->available) {
156584f9bd12SAlex Elder 		dev_err(dev, "unavailable endpoint id(s) 0x%08x\n",
156684f9bd12SAlex Elder 			ipa->initialized & ~ipa->available);
156784f9bd12SAlex Elder 		ret = -EINVAL;		/* Report other errors too */
156884f9bd12SAlex Elder 	}
156984f9bd12SAlex Elder 
157084f9bd12SAlex Elder 	initialized = ipa->initialized;
157184f9bd12SAlex Elder 	while (initialized) {
157284f9bd12SAlex Elder 		u32 endpoint_id = __ffs(initialized);
157384f9bd12SAlex Elder 		struct ipa_endpoint *endpoint;
157484f9bd12SAlex Elder 
157584f9bd12SAlex Elder 		initialized ^= BIT(endpoint_id);
157684f9bd12SAlex Elder 
157784f9bd12SAlex Elder 		/* Make sure it's pointing in the right direction */
157884f9bd12SAlex Elder 		endpoint = &ipa->endpoint[endpoint_id];
157984f9bd12SAlex Elder 		if ((endpoint_id < rx_base) != !!endpoint->toward_ipa) {
158084f9bd12SAlex Elder 			dev_err(dev, "endpoint id %u wrong direction\n",
158184f9bd12SAlex Elder 				endpoint_id);
158284f9bd12SAlex Elder 			ret = -EINVAL;
158384f9bd12SAlex Elder 		}
158484f9bd12SAlex Elder 	}
158584f9bd12SAlex Elder 
158684f9bd12SAlex Elder 	return ret;
158784f9bd12SAlex Elder }
158884f9bd12SAlex Elder 
158984f9bd12SAlex Elder void ipa_endpoint_deconfig(struct ipa *ipa)
159084f9bd12SAlex Elder {
159184f9bd12SAlex Elder 	ipa->available = 0;	/* Nothing more to do */
159284f9bd12SAlex Elder }
159384f9bd12SAlex Elder 
159484f9bd12SAlex Elder static void ipa_endpoint_init_one(struct ipa *ipa, enum ipa_endpoint_name name,
159584f9bd12SAlex Elder 				  const struct ipa_gsi_endpoint_data *data)
159684f9bd12SAlex Elder {
159784f9bd12SAlex Elder 	struct ipa_endpoint *endpoint;
159884f9bd12SAlex Elder 
159984f9bd12SAlex Elder 	endpoint = &ipa->endpoint[data->endpoint_id];
160084f9bd12SAlex Elder 
160184f9bd12SAlex Elder 	if (data->ee_id == GSI_EE_AP)
160284f9bd12SAlex Elder 		ipa->channel_map[data->channel_id] = endpoint;
160384f9bd12SAlex Elder 	ipa->name_map[name] = endpoint;
160484f9bd12SAlex Elder 
160584f9bd12SAlex Elder 	endpoint->ipa = ipa;
160684f9bd12SAlex Elder 	endpoint->ee_id = data->ee_id;
160784f9bd12SAlex Elder 	endpoint->seq_type = data->endpoint.seq_type;
160884f9bd12SAlex Elder 	endpoint->channel_id = data->channel_id;
160984f9bd12SAlex Elder 	endpoint->endpoint_id = data->endpoint_id;
161084f9bd12SAlex Elder 	endpoint->toward_ipa = data->toward_ipa;
161184f9bd12SAlex Elder 	endpoint->data = &data->endpoint.config;
161284f9bd12SAlex Elder 
161384f9bd12SAlex Elder 	ipa->initialized |= BIT(endpoint->endpoint_id);
161484f9bd12SAlex Elder }
161584f9bd12SAlex Elder 
161684f9bd12SAlex Elder void ipa_endpoint_exit_one(struct ipa_endpoint *endpoint)
161784f9bd12SAlex Elder {
161884f9bd12SAlex Elder 	endpoint->ipa->initialized &= ~BIT(endpoint->endpoint_id);
161984f9bd12SAlex Elder 
162084f9bd12SAlex Elder 	memset(endpoint, 0, sizeof(*endpoint));
162184f9bd12SAlex Elder }
162284f9bd12SAlex Elder 
162384f9bd12SAlex Elder void ipa_endpoint_exit(struct ipa *ipa)
162484f9bd12SAlex Elder {
162584f9bd12SAlex Elder 	u32 initialized = ipa->initialized;
162684f9bd12SAlex Elder 
162784f9bd12SAlex Elder 	while (initialized) {
162884f9bd12SAlex Elder 		u32 endpoint_id = __fls(initialized);
162984f9bd12SAlex Elder 
163084f9bd12SAlex Elder 		initialized ^= BIT(endpoint_id);
163184f9bd12SAlex Elder 
163284f9bd12SAlex Elder 		ipa_endpoint_exit_one(&ipa->endpoint[endpoint_id]);
163384f9bd12SAlex Elder 	}
163484f9bd12SAlex Elder 	memset(ipa->name_map, 0, sizeof(ipa->name_map));
163584f9bd12SAlex Elder 	memset(ipa->channel_map, 0, sizeof(ipa->channel_map));
163684f9bd12SAlex Elder }
163784f9bd12SAlex Elder 
163884f9bd12SAlex Elder /* Returns a bitmask of endpoints that support filtering, or 0 on error */
163984f9bd12SAlex Elder u32 ipa_endpoint_init(struct ipa *ipa, u32 count,
164084f9bd12SAlex Elder 		      const struct ipa_gsi_endpoint_data *data)
164184f9bd12SAlex Elder {
164284f9bd12SAlex Elder 	enum ipa_endpoint_name name;
164384f9bd12SAlex Elder 	u32 filter_map;
164484f9bd12SAlex Elder 
164584f9bd12SAlex Elder 	if (!ipa_endpoint_data_valid(ipa, count, data))
164684f9bd12SAlex Elder 		return 0;	/* Error */
164784f9bd12SAlex Elder 
164884f9bd12SAlex Elder 	ipa->initialized = 0;
164984f9bd12SAlex Elder 
165084f9bd12SAlex Elder 	filter_map = 0;
165184f9bd12SAlex Elder 	for (name = 0; name < count; name++, data++) {
165284f9bd12SAlex Elder 		if (ipa_gsi_endpoint_data_empty(data))
165384f9bd12SAlex Elder 			continue;	/* Skip over empty slots */
165484f9bd12SAlex Elder 
165584f9bd12SAlex Elder 		ipa_endpoint_init_one(ipa, name, data);
165684f9bd12SAlex Elder 
165784f9bd12SAlex Elder 		if (data->endpoint.filter_support)
165884f9bd12SAlex Elder 			filter_map |= BIT(data->endpoint_id);
165984f9bd12SAlex Elder 	}
166084f9bd12SAlex Elder 
166184f9bd12SAlex Elder 	if (!ipa_filter_map_valid(ipa, filter_map))
166284f9bd12SAlex Elder 		goto err_endpoint_exit;
166384f9bd12SAlex Elder 
166484f9bd12SAlex Elder 	return filter_map;	/* Non-zero bitmask */
166584f9bd12SAlex Elder 
166684f9bd12SAlex Elder err_endpoint_exit:
166784f9bd12SAlex Elder 	ipa_endpoint_exit(ipa);
166884f9bd12SAlex Elder 
166984f9bd12SAlex Elder 	return 0;	/* Error */
167084f9bd12SAlex Elder }
1671