xref: /linux/drivers/net/ipa/ipa_endpoint.c (revision 70132763d5d2e94cd185e3aa92ac6a3ba89068fa)
184f9bd12SAlex Elder // SPDX-License-Identifier: GPL-2.0
284f9bd12SAlex Elder 
384f9bd12SAlex Elder /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
4647a05f3SAlex Elder  * Copyright (C) 2019-2021 Linaro Ltd.
584f9bd12SAlex Elder  */
684f9bd12SAlex Elder 
784f9bd12SAlex Elder #include <linux/types.h>
884f9bd12SAlex Elder #include <linux/device.h>
984f9bd12SAlex Elder #include <linux/slab.h>
1084f9bd12SAlex Elder #include <linux/bitfield.h>
1184f9bd12SAlex Elder #include <linux/if_rmnet.h>
1284f9bd12SAlex Elder #include <linux/dma-direction.h>
1384f9bd12SAlex Elder 
1484f9bd12SAlex Elder #include "gsi.h"
1584f9bd12SAlex Elder #include "gsi_trans.h"
1684f9bd12SAlex Elder #include "ipa.h"
1784f9bd12SAlex Elder #include "ipa_data.h"
1884f9bd12SAlex Elder #include "ipa_endpoint.h"
1984f9bd12SAlex Elder #include "ipa_cmd.h"
2084f9bd12SAlex Elder #include "ipa_mem.h"
2184f9bd12SAlex Elder #include "ipa_modem.h"
2284f9bd12SAlex Elder #include "ipa_table.h"
2384f9bd12SAlex Elder #include "ipa_gsi.h"
242775cbc5SAlex Elder #include "ipa_power.h"
2584f9bd12SAlex Elder 
2684f9bd12SAlex Elder #define atomic_dec_not_zero(v)	atomic_add_unless((v), -1, 0)
2784f9bd12SAlex Elder 
289654d8c4SAlex Elder /* Hardware is told about receive buffers once a "batch" has been queued */
299654d8c4SAlex Elder #define IPA_REPLENISH_BATCH	16		/* Must be non-zero */
3084f9bd12SAlex Elder 
3184f9bd12SAlex Elder /* The amount of RX buffer space consumed by standard skb overhead */
3284f9bd12SAlex Elder #define IPA_RX_BUFFER_OVERHEAD	(PAGE_SIZE - SKB_MAX_ORDER(NET_SKB_PAD, 0))
3384f9bd12SAlex Elder 
348730f45dSAlex Elder /* Where to find the QMAP mux_id for a packet within modem-supplied metadata */
358730f45dSAlex Elder #define IPA_ENDPOINT_QMAP_METADATA_MASK		0x000000ff /* host byte order */
368730f45dSAlex Elder 
3784f9bd12SAlex Elder #define IPA_ENDPOINT_RESET_AGGR_RETRY_MAX	3
3884f9bd12SAlex Elder 
3984f9bd12SAlex Elder /** enum ipa_status_opcode - status element opcode hardware values */
4084f9bd12SAlex Elder enum ipa_status_opcode {
4184f9bd12SAlex Elder 	IPA_STATUS_OPCODE_PACKET		= 0x01,
4284f9bd12SAlex Elder 	IPA_STATUS_OPCODE_DROPPED_PACKET	= 0x04,
4384f9bd12SAlex Elder 	IPA_STATUS_OPCODE_SUSPENDED_PACKET	= 0x08,
4484f9bd12SAlex Elder 	IPA_STATUS_OPCODE_PACKET_2ND_PASS	= 0x40,
4584f9bd12SAlex Elder };
4684f9bd12SAlex Elder 
4784f9bd12SAlex Elder /** enum ipa_status_exception - status element exception type */
4884f9bd12SAlex Elder enum ipa_status_exception {
4984f9bd12SAlex Elder 	/* 0 means no exception */
5084f9bd12SAlex Elder 	IPA_STATUS_EXCEPTION_DEAGGR		= 0x01,
5184f9bd12SAlex Elder };
5284f9bd12SAlex Elder 
5384f9bd12SAlex Elder /* Status element provided by hardware */
5484f9bd12SAlex Elder struct ipa_status {
5584f9bd12SAlex Elder 	u8 opcode;		/* enum ipa_status_opcode */
5684f9bd12SAlex Elder 	u8 exception;		/* enum ipa_status_exception */
5784f9bd12SAlex Elder 	__le16 mask;
5884f9bd12SAlex Elder 	__le16 pkt_len;
5984f9bd12SAlex Elder 	u8 endp_src_idx;
6084f9bd12SAlex Elder 	u8 endp_dst_idx;
6184f9bd12SAlex Elder 	__le32 metadata;
6284f9bd12SAlex Elder 	__le32 flags1;
6384f9bd12SAlex Elder 	__le64 flags2;
6484f9bd12SAlex Elder 	__le32 flags3;
6584f9bd12SAlex Elder 	__le32 flags4;
6684f9bd12SAlex Elder };
6784f9bd12SAlex Elder 
6884f9bd12SAlex Elder /* Field masks for struct ipa_status structure fields */
69f6aba7b5SAlex Elder #define IPA_STATUS_MASK_TAG_VALID_FMASK		GENMASK(4, 4)
70f6aba7b5SAlex Elder #define IPA_STATUS_SRC_IDX_FMASK		GENMASK(4, 0)
7184f9bd12SAlex Elder #define IPA_STATUS_DST_IDX_FMASK		GENMASK(4, 0)
7284f9bd12SAlex Elder #define IPA_STATUS_FLAGS1_RT_RULE_ID_FMASK	GENMASK(31, 22)
73f6aba7b5SAlex Elder #define IPA_STATUS_FLAGS2_TAG_FMASK		GENMASK_ULL(63, 16)
7484f9bd12SAlex Elder 
75ed23f026SAlex Elder static u32 aggr_byte_limit_max(enum ipa_version version)
76ed23f026SAlex Elder {
77ed23f026SAlex Elder 	if (version < IPA_VERSION_4_5)
78ed23f026SAlex Elder 		return field_max(aggr_byte_limit_fmask(true));
79ed23f026SAlex Elder 
80ed23f026SAlex Elder 	return field_max(aggr_byte_limit_fmask(false));
81ed23f026SAlex Elder }
82ed23f026SAlex Elder 
833cebb7c2SAlex Elder /* Compute the aggregation size value to use for a given buffer size */
843cebb7c2SAlex Elder static u32 ipa_aggr_size_kb(u32 rx_buffer_size, bool aggr_hard_limit)
853cebb7c2SAlex Elder {
863cebb7c2SAlex Elder 	/* A hard aggregation limit will not be crossed; aggregation closes
873cebb7c2SAlex Elder 	 * if saving incoming data would cross the hard byte limit boundary.
883cebb7c2SAlex Elder 	 *
893cebb7c2SAlex Elder 	 * With a soft limit, aggregation closes *after* the size boundary
903cebb7c2SAlex Elder 	 * has been crossed.  In that case the limit must leave enough space
913cebb7c2SAlex Elder 	 * after that limit to receive a full MTU of data plus overhead.
923cebb7c2SAlex Elder 	 */
933cebb7c2SAlex Elder 	if (!aggr_hard_limit)
943cebb7c2SAlex Elder 		rx_buffer_size -= IPA_MTU + IPA_RX_BUFFER_OVERHEAD;
953cebb7c2SAlex Elder 
963cebb7c2SAlex Elder 	/* The byte limit is encoded as a number of kilobytes */
973cebb7c2SAlex Elder 
983cebb7c2SAlex Elder 	return rx_buffer_size / SZ_1K;
993cebb7c2SAlex Elder }
1003cebb7c2SAlex Elder 
10184f9bd12SAlex Elder static bool ipa_endpoint_data_valid_one(struct ipa *ipa, u32 count,
10284f9bd12SAlex Elder 			    const struct ipa_gsi_endpoint_data *all_data,
10384f9bd12SAlex Elder 			    const struct ipa_gsi_endpoint_data *data)
10484f9bd12SAlex Elder {
10584f9bd12SAlex Elder 	const struct ipa_gsi_endpoint_data *other_data;
10684f9bd12SAlex Elder 	struct device *dev = &ipa->pdev->dev;
10784f9bd12SAlex Elder 	enum ipa_endpoint_name other_name;
10884f9bd12SAlex Elder 
10984f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(data))
11084f9bd12SAlex Elder 		return true;
11184f9bd12SAlex Elder 
11284f9bd12SAlex Elder 	if (!data->toward_ipa) {
1133cebb7c2SAlex Elder 		const struct ipa_endpoint_rx *rx_config;
114ed23f026SAlex Elder 		u32 buffer_size;
1153cebb7c2SAlex Elder 		u32 aggr_size;
116ed23f026SAlex Elder 		u32 limit;
117ed23f026SAlex Elder 
11884f9bd12SAlex Elder 		if (data->endpoint.filter_support) {
11984f9bd12SAlex Elder 			dev_err(dev, "filtering not supported for "
12084f9bd12SAlex Elder 					"RX endpoint %u\n",
12184f9bd12SAlex Elder 				data->endpoint_id);
12284f9bd12SAlex Elder 			return false;
12384f9bd12SAlex Elder 		}
12484f9bd12SAlex Elder 
125ed23f026SAlex Elder 		/* Nothing more to check for non-AP RX */
126ed23f026SAlex Elder 		if (data->ee_id != GSI_EE_AP)
127ed23f026SAlex Elder 			return true;
128ed23f026SAlex Elder 
1293cebb7c2SAlex Elder 		rx_config = &data->endpoint.config.rx;
1303cebb7c2SAlex Elder 
131ed23f026SAlex Elder 		/* The buffer size must hold an MTU plus overhead */
1323cebb7c2SAlex Elder 		buffer_size = rx_config->buffer_size;
133ed23f026SAlex Elder 		limit = IPA_MTU + IPA_RX_BUFFER_OVERHEAD;
134ed23f026SAlex Elder 		if (buffer_size < limit) {
135ed23f026SAlex Elder 			dev_err(dev, "RX buffer size too small for RX endpoint %u (%u < %u)\n",
136ed23f026SAlex Elder 				data->endpoint_id, buffer_size, limit);
137ed23f026SAlex Elder 			return false;
138ed23f026SAlex Elder 		}
139ed23f026SAlex Elder 
1403cebb7c2SAlex Elder 		if (!data->endpoint.config.aggregation) {
1413cebb7c2SAlex Elder 			bool result = true;
1423cebb7c2SAlex Elder 
1433cebb7c2SAlex Elder 			/* No aggregation; check for bogus aggregation data */
144beb90cbaSAlex Elder 			if (rx_config->aggr_time_limit) {
145beb90cbaSAlex Elder 				dev_err(dev,
146beb90cbaSAlex Elder 					"time limit with no aggregation for RX endpoint %u\n",
147beb90cbaSAlex Elder 					data->endpoint_id);
148beb90cbaSAlex Elder 				result = false;
149beb90cbaSAlex Elder 			}
150beb90cbaSAlex Elder 
1513cebb7c2SAlex Elder 			if (rx_config->aggr_hard_limit) {
1523cebb7c2SAlex Elder 				dev_err(dev, "hard limit with no aggregation for RX endpoint %u\n",
1533cebb7c2SAlex Elder 					data->endpoint_id);
1543cebb7c2SAlex Elder 				result = false;
1553cebb7c2SAlex Elder 			}
1563cebb7c2SAlex Elder 
1573cebb7c2SAlex Elder 			if (rx_config->aggr_close_eof) {
1583cebb7c2SAlex Elder 				dev_err(dev, "close EOF with no aggregation for RX endpoint %u\n",
1593cebb7c2SAlex Elder 					data->endpoint_id);
1603cebb7c2SAlex Elder 				result = false;
1613cebb7c2SAlex Elder 			}
1623cebb7c2SAlex Elder 
1633cebb7c2SAlex Elder 			return result;	/* Nothing more to check */
1643cebb7c2SAlex Elder 		}
1653cebb7c2SAlex Elder 
1663cebb7c2SAlex Elder 		/* For an endpoint supporting receive aggregation, the byte
1673cebb7c2SAlex Elder 		 * limit defines the point at which aggregation closes.  This
1683cebb7c2SAlex Elder 		 * check ensures the receive buffer size doesn't result in a
1693cebb7c2SAlex Elder 		 * limit that exceeds what's representable in the aggregation
1703cebb7c2SAlex Elder 		 * byte limit field.
171ed23f026SAlex Elder 		 */
1723cebb7c2SAlex Elder 		aggr_size = ipa_aggr_size_kb(buffer_size - NET_SKB_PAD,
1733cebb7c2SAlex Elder 					     rx_config->aggr_hard_limit);
1743cebb7c2SAlex Elder 		limit = aggr_byte_limit_max(ipa->version);
1753cebb7c2SAlex Elder 		if (aggr_size > limit) {
1763cebb7c2SAlex Elder 			dev_err(dev, "aggregated size too large for RX endpoint %u (%u KB > %u KB)\n",
1773cebb7c2SAlex Elder 				data->endpoint_id, aggr_size, limit);
178ed23f026SAlex Elder 
179ed23f026SAlex Elder 			return false;
180ed23f026SAlex Elder 		}
181ed23f026SAlex Elder 
18284f9bd12SAlex Elder 		return true;	/* Nothing more to check for RX */
18384f9bd12SAlex Elder 	}
18484f9bd12SAlex Elder 
18584f9bd12SAlex Elder 	if (data->endpoint.config.status_enable) {
18684f9bd12SAlex Elder 		other_name = data->endpoint.config.tx.status_endpoint;
18784f9bd12SAlex Elder 		if (other_name >= count) {
18884f9bd12SAlex Elder 			dev_err(dev, "status endpoint name %u out of range "
18984f9bd12SAlex Elder 					"for endpoint %u\n",
19084f9bd12SAlex Elder 				other_name, data->endpoint_id);
19184f9bd12SAlex Elder 			return false;
19284f9bd12SAlex Elder 		}
19384f9bd12SAlex Elder 
19484f9bd12SAlex Elder 		/* Status endpoint must be defined... */
19584f9bd12SAlex Elder 		other_data = &all_data[other_name];
19684f9bd12SAlex Elder 		if (ipa_gsi_endpoint_data_empty(other_data)) {
19784f9bd12SAlex Elder 			dev_err(dev, "DMA endpoint name %u undefined "
19884f9bd12SAlex Elder 					"for endpoint %u\n",
19984f9bd12SAlex Elder 				other_name, data->endpoint_id);
20084f9bd12SAlex Elder 			return false;
20184f9bd12SAlex Elder 		}
20284f9bd12SAlex Elder 
20384f9bd12SAlex Elder 		/* ...and has to be an RX endpoint... */
20484f9bd12SAlex Elder 		if (other_data->toward_ipa) {
20584f9bd12SAlex Elder 			dev_err(dev,
20684f9bd12SAlex Elder 				"status endpoint for endpoint %u not RX\n",
20784f9bd12SAlex Elder 				data->endpoint_id);
20884f9bd12SAlex Elder 			return false;
20984f9bd12SAlex Elder 		}
21084f9bd12SAlex Elder 
21184f9bd12SAlex Elder 		/* ...and if it's to be an AP endpoint... */
21284f9bd12SAlex Elder 		if (other_data->ee_id == GSI_EE_AP) {
21384f9bd12SAlex Elder 			/* ...make sure it has status enabled. */
21484f9bd12SAlex Elder 			if (!other_data->endpoint.config.status_enable) {
21584f9bd12SAlex Elder 				dev_err(dev,
21684f9bd12SAlex Elder 					"status not enabled for endpoint %u\n",
21784f9bd12SAlex Elder 					other_data->endpoint_id);
21884f9bd12SAlex Elder 				return false;
21984f9bd12SAlex Elder 			}
22084f9bd12SAlex Elder 		}
22184f9bd12SAlex Elder 	}
22284f9bd12SAlex Elder 
22384f9bd12SAlex Elder 	if (data->endpoint.config.dma_mode) {
22484f9bd12SAlex Elder 		other_name = data->endpoint.config.dma_endpoint;
22584f9bd12SAlex Elder 		if (other_name >= count) {
22684f9bd12SAlex Elder 			dev_err(dev, "DMA endpoint name %u out of range "
22784f9bd12SAlex Elder 					"for endpoint %u\n",
22884f9bd12SAlex Elder 				other_name, data->endpoint_id);
22984f9bd12SAlex Elder 			return false;
23084f9bd12SAlex Elder 		}
23184f9bd12SAlex Elder 
23284f9bd12SAlex Elder 		other_data = &all_data[other_name];
23384f9bd12SAlex Elder 		if (ipa_gsi_endpoint_data_empty(other_data)) {
23484f9bd12SAlex Elder 			dev_err(dev, "DMA endpoint name %u undefined "
23584f9bd12SAlex Elder 					"for endpoint %u\n",
23684f9bd12SAlex Elder 				other_name, data->endpoint_id);
23784f9bd12SAlex Elder 			return false;
23884f9bd12SAlex Elder 		}
23984f9bd12SAlex Elder 	}
24084f9bd12SAlex Elder 
24184f9bd12SAlex Elder 	return true;
24284f9bd12SAlex Elder }
24384f9bd12SAlex Elder 
24484f9bd12SAlex Elder static bool ipa_endpoint_data_valid(struct ipa *ipa, u32 count,
24584f9bd12SAlex Elder 				    const struct ipa_gsi_endpoint_data *data)
24684f9bd12SAlex Elder {
24784f9bd12SAlex Elder 	const struct ipa_gsi_endpoint_data *dp = data;
24884f9bd12SAlex Elder 	struct device *dev = &ipa->pdev->dev;
24984f9bd12SAlex Elder 	enum ipa_endpoint_name name;
25084f9bd12SAlex Elder 
25184f9bd12SAlex Elder 	if (count > IPA_ENDPOINT_COUNT) {
25284f9bd12SAlex Elder 		dev_err(dev, "too many endpoints specified (%u > %u)\n",
25384f9bd12SAlex Elder 			count, IPA_ENDPOINT_COUNT);
25484f9bd12SAlex Elder 		return false;
25584f9bd12SAlex Elder 	}
25684f9bd12SAlex Elder 
25784f9bd12SAlex Elder 	/* Make sure needed endpoints have defined data */
25884f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_COMMAND_TX])) {
25984f9bd12SAlex Elder 		dev_err(dev, "command TX endpoint not defined\n");
26084f9bd12SAlex Elder 		return false;
26184f9bd12SAlex Elder 	}
26284f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_LAN_RX])) {
26384f9bd12SAlex Elder 		dev_err(dev, "LAN RX endpoint not defined\n");
26484f9bd12SAlex Elder 		return false;
26584f9bd12SAlex Elder 	}
26684f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_MODEM_TX])) {
26784f9bd12SAlex Elder 		dev_err(dev, "AP->modem TX endpoint not defined\n");
26884f9bd12SAlex Elder 		return false;
26984f9bd12SAlex Elder 	}
27084f9bd12SAlex Elder 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_MODEM_RX])) {
27184f9bd12SAlex Elder 		dev_err(dev, "AP<-modem RX endpoint not defined\n");
27284f9bd12SAlex Elder 		return false;
27384f9bd12SAlex Elder 	}
27484f9bd12SAlex Elder 
27584f9bd12SAlex Elder 	for (name = 0; name < count; name++, dp++)
27684f9bd12SAlex Elder 		if (!ipa_endpoint_data_valid_one(ipa, count, data, dp))
27784f9bd12SAlex Elder 			return false;
27884f9bd12SAlex Elder 
27984f9bd12SAlex Elder 	return true;
28084f9bd12SAlex Elder }
28184f9bd12SAlex Elder 
28284f9bd12SAlex Elder /* Allocate a transaction to use on a non-command endpoint */
28384f9bd12SAlex Elder static struct gsi_trans *ipa_endpoint_trans_alloc(struct ipa_endpoint *endpoint,
28484f9bd12SAlex Elder 						  u32 tre_count)
28584f9bd12SAlex Elder {
28684f9bd12SAlex Elder 	struct gsi *gsi = &endpoint->ipa->gsi;
28784f9bd12SAlex Elder 	u32 channel_id = endpoint->channel_id;
28884f9bd12SAlex Elder 	enum dma_data_direction direction;
28984f9bd12SAlex Elder 
29084f9bd12SAlex Elder 	direction = endpoint->toward_ipa ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
29184f9bd12SAlex Elder 
29284f9bd12SAlex Elder 	return gsi_channel_trans_alloc(gsi, channel_id, tre_count, direction);
29384f9bd12SAlex Elder }
29484f9bd12SAlex Elder 
29584f9bd12SAlex Elder /* suspend_delay represents suspend for RX, delay for TX endpoints.
2964c9d631aSAlex Elder  * Note that suspend is not supported starting with IPA v4.0, and
2974c9d631aSAlex Elder  * delay mode should not be used starting with IPA v4.2.
29884f9bd12SAlex Elder  */
2994900bf34SAlex Elder static bool
30084f9bd12SAlex Elder ipa_endpoint_init_ctrl(struct ipa_endpoint *endpoint, bool suspend_delay)
30184f9bd12SAlex Elder {
30284f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_CTRL_N_OFFSET(endpoint->endpoint_id);
30384f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
3044900bf34SAlex Elder 	bool state;
30584f9bd12SAlex Elder 	u32 mask;
30684f9bd12SAlex Elder 	u32 val;
30784f9bd12SAlex Elder 
3085bc55884SAlex Elder 	if (endpoint->toward_ipa)
3094c9d631aSAlex Elder 		WARN_ON(ipa->version >= IPA_VERSION_4_2);
3105bc55884SAlex Elder 	else
3115bc55884SAlex Elder 		WARN_ON(ipa->version >= IPA_VERSION_4_0);
3125bc55884SAlex Elder 
31384f9bd12SAlex Elder 	mask = endpoint->toward_ipa ? ENDP_DELAY_FMASK : ENDP_SUSPEND_FMASK;
31484f9bd12SAlex Elder 
31584f9bd12SAlex Elder 	val = ioread32(ipa->reg_virt + offset);
3164900bf34SAlex Elder 	state = !!(val & mask);
3175bc55884SAlex Elder 
3185bc55884SAlex Elder 	/* Don't bother if it's already in the requested state */
3194900bf34SAlex Elder 	if (suspend_delay != state) {
32084f9bd12SAlex Elder 		val ^= mask;
32184f9bd12SAlex Elder 		iowrite32(val, ipa->reg_virt + offset);
3224900bf34SAlex Elder 	}
32384f9bd12SAlex Elder 
3244900bf34SAlex Elder 	return state;
32584f9bd12SAlex Elder }
32684f9bd12SAlex Elder 
3274c9d631aSAlex Elder /* We don't care what the previous state was for delay mode */
3284fa95248SAlex Elder static void
3294fa95248SAlex Elder ipa_endpoint_program_delay(struct ipa_endpoint *endpoint, bool enable)
3304fa95248SAlex Elder {
3314c9d631aSAlex Elder 	/* Delay mode should not be used for IPA v4.2+ */
3324c9d631aSAlex Elder 	WARN_ON(endpoint->ipa->version >= IPA_VERSION_4_2);
3335bc55884SAlex Elder 	WARN_ON(!endpoint->toward_ipa);
3344fa95248SAlex Elder 
3354fa95248SAlex Elder 	(void)ipa_endpoint_init_ctrl(endpoint, enable);
3364fa95248SAlex Elder }
3374fa95248SAlex Elder 
338fff89971SAlex Elder static bool ipa_endpoint_aggr_active(struct ipa_endpoint *endpoint)
339fff89971SAlex Elder {
340fff89971SAlex Elder 	u32 mask = BIT(endpoint->endpoint_id);
341fff89971SAlex Elder 	struct ipa *ipa = endpoint->ipa;
342fff89971SAlex Elder 	u32 offset;
343fff89971SAlex Elder 	u32 val;
344fff89971SAlex Elder 
3455bc55884SAlex Elder 	WARN_ON(!(mask & ipa->available));
3465bc55884SAlex Elder 
347fff89971SAlex Elder 	offset = ipa_reg_state_aggr_active_offset(ipa->version);
348fff89971SAlex Elder 	val = ioread32(ipa->reg_virt + offset);
349fff89971SAlex Elder 
350fff89971SAlex Elder 	return !!(val & mask);
351fff89971SAlex Elder }
352fff89971SAlex Elder 
353fff89971SAlex Elder static void ipa_endpoint_force_close(struct ipa_endpoint *endpoint)
354fff89971SAlex Elder {
355fff89971SAlex Elder 	u32 mask = BIT(endpoint->endpoint_id);
356fff89971SAlex Elder 	struct ipa *ipa = endpoint->ipa;
357fff89971SAlex Elder 
3585bc55884SAlex Elder 	WARN_ON(!(mask & ipa->available));
3595bc55884SAlex Elder 
360fff89971SAlex Elder 	iowrite32(mask, ipa->reg_virt + IPA_REG_AGGR_FORCE_CLOSE_OFFSET);
361fff89971SAlex Elder }
362fff89971SAlex Elder 
363fff89971SAlex Elder /**
364fff89971SAlex Elder  * ipa_endpoint_suspend_aggr() - Emulate suspend interrupt
365e3eea08eSAlex Elder  * @endpoint:	Endpoint on which to emulate a suspend
366fff89971SAlex Elder  *
367fff89971SAlex Elder  *  Emulate suspend IPA interrupt to unsuspend an endpoint suspended
368fff89971SAlex Elder  *  with an open aggregation frame.  This is to work around a hardware
369fff89971SAlex Elder  *  issue in IPA version 3.5.1 where the suspend interrupt will not be
370fff89971SAlex Elder  *  generated when it should be.
371fff89971SAlex Elder  */
372fff89971SAlex Elder static void ipa_endpoint_suspend_aggr(struct ipa_endpoint *endpoint)
373fff89971SAlex Elder {
374fff89971SAlex Elder 	struct ipa *ipa = endpoint->ipa;
375fff89971SAlex Elder 
376660e52d6SAlex Elder 	if (!endpoint->config.aggregation)
377fff89971SAlex Elder 		return;
378fff89971SAlex Elder 
379fff89971SAlex Elder 	/* Nothing to do if the endpoint doesn't have aggregation open */
380fff89971SAlex Elder 	if (!ipa_endpoint_aggr_active(endpoint))
381fff89971SAlex Elder 		return;
382fff89971SAlex Elder 
383fff89971SAlex Elder 	/* Force close aggregation */
384fff89971SAlex Elder 	ipa_endpoint_force_close(endpoint);
385fff89971SAlex Elder 
386fff89971SAlex Elder 	ipa_interrupt_simulate_suspend(ipa->interrupt);
387fff89971SAlex Elder }
388fff89971SAlex Elder 
389fff89971SAlex Elder /* Returns previous suspend state (true means suspend was enabled) */
3904fa95248SAlex Elder static bool
3914fa95248SAlex Elder ipa_endpoint_program_suspend(struct ipa_endpoint *endpoint, bool enable)
3924fa95248SAlex Elder {
393fff89971SAlex Elder 	bool suspended;
394fff89971SAlex Elder 
395d7f3087bSAlex Elder 	if (endpoint->ipa->version >= IPA_VERSION_4_0)
396b07f283eSAlex Elder 		return enable;	/* For IPA v4.0+, no change made */
397b07f283eSAlex Elder 
3985bc55884SAlex Elder 	WARN_ON(endpoint->toward_ipa);
3994fa95248SAlex Elder 
400fff89971SAlex Elder 	suspended = ipa_endpoint_init_ctrl(endpoint, enable);
401fff89971SAlex Elder 
402fff89971SAlex Elder 	/* A client suspended with an open aggregation frame will not
403fff89971SAlex Elder 	 * generate a SUSPEND IPA interrupt.  If enabling suspend, have
404fff89971SAlex Elder 	 * ipa_endpoint_suspend_aggr() handle this.
405fff89971SAlex Elder 	 */
406fff89971SAlex Elder 	if (enable && !suspended)
407fff89971SAlex Elder 		ipa_endpoint_suspend_aggr(endpoint);
408fff89971SAlex Elder 
409fff89971SAlex Elder 	return suspended;
4104fa95248SAlex Elder }
4114fa95248SAlex Elder 
4124c9d631aSAlex Elder /* Put all modem RX endpoints into suspend mode, and stop transmission
4134c9d631aSAlex Elder  * on all modem TX endpoints.  Prior to IPA v4.2, endpoint DELAY mode is
4144c9d631aSAlex Elder  * used for TX endpoints; starting with IPA v4.2 we use GSI channel flow
4154c9d631aSAlex Elder  * control instead.
4164c9d631aSAlex Elder  */
41784f9bd12SAlex Elder void ipa_endpoint_modem_pause_all(struct ipa *ipa, bool enable)
41884f9bd12SAlex Elder {
41984f9bd12SAlex Elder 	u32 endpoint_id;
42084f9bd12SAlex Elder 
42184f9bd12SAlex Elder 	for (endpoint_id = 0; endpoint_id < IPA_ENDPOINT_MAX; endpoint_id++) {
42284f9bd12SAlex Elder 		struct ipa_endpoint *endpoint = &ipa->endpoint[endpoint_id];
42384f9bd12SAlex Elder 
42484f9bd12SAlex Elder 		if (endpoint->ee_id != GSI_EE_MODEM)
42584f9bd12SAlex Elder 			continue;
42684f9bd12SAlex Elder 
4274c9d631aSAlex Elder 		if (!endpoint->toward_ipa)
4284c9d631aSAlex Elder 			(void)ipa_endpoint_program_suspend(endpoint, enable);
4294c9d631aSAlex Elder 		else if (ipa->version < IPA_VERSION_4_2)
4304fa95248SAlex Elder 			ipa_endpoint_program_delay(endpoint, enable);
431b07f283eSAlex Elder 		else
4324c9d631aSAlex Elder 			gsi_modem_channel_flow_control(&ipa->gsi,
4334c9d631aSAlex Elder 						       endpoint->channel_id,
4344c9d631aSAlex Elder 						       enable);
43584f9bd12SAlex Elder 	}
43684f9bd12SAlex Elder }
43784f9bd12SAlex Elder 
43884f9bd12SAlex Elder /* Reset all modem endpoints to use the default exception endpoint */
43984f9bd12SAlex Elder int ipa_endpoint_modem_exception_reset_all(struct ipa *ipa)
44084f9bd12SAlex Elder {
44184f9bd12SAlex Elder 	u32 initialized = ipa->initialized;
44284f9bd12SAlex Elder 	struct gsi_trans *trans;
44384f9bd12SAlex Elder 	u32 count;
44484f9bd12SAlex Elder 
4452091c79aSAlex Elder 	/* We need one command per modem TX endpoint, plus the commands
4462091c79aSAlex Elder 	 * that clear the pipeline.
44784f9bd12SAlex Elder 	 */
4482091c79aSAlex Elder 	count = ipa->modem_tx_count + ipa_cmd_pipeline_clear_count();
44984f9bd12SAlex Elder 	trans = ipa_cmd_trans_alloc(ipa, count);
45084f9bd12SAlex Elder 	if (!trans) {
45184f9bd12SAlex Elder 		dev_err(&ipa->pdev->dev,
45284f9bd12SAlex Elder 			"no transaction to reset modem exception endpoints\n");
45384f9bd12SAlex Elder 		return -EBUSY;
45484f9bd12SAlex Elder 	}
45584f9bd12SAlex Elder 
45684f9bd12SAlex Elder 	while (initialized) {
45784f9bd12SAlex Elder 		u32 endpoint_id = __ffs(initialized);
45884f9bd12SAlex Elder 		struct ipa_endpoint *endpoint;
45984f9bd12SAlex Elder 		u32 offset;
46084f9bd12SAlex Elder 
46184f9bd12SAlex Elder 		initialized ^= BIT(endpoint_id);
46284f9bd12SAlex Elder 
46384f9bd12SAlex Elder 		/* We only reset modem TX endpoints */
46484f9bd12SAlex Elder 		endpoint = &ipa->endpoint[endpoint_id];
46584f9bd12SAlex Elder 		if (!(endpoint->ee_id == GSI_EE_MODEM && endpoint->toward_ipa))
46684f9bd12SAlex Elder 			continue;
46784f9bd12SAlex Elder 
46884f9bd12SAlex Elder 		offset = IPA_REG_ENDP_STATUS_N_OFFSET(endpoint_id);
46984f9bd12SAlex Elder 
47084f9bd12SAlex Elder 		/* Value written is 0, and all bits are updated.  That
47184f9bd12SAlex Elder 		 * means status is disabled on the endpoint, and as a
47284f9bd12SAlex Elder 		 * result all other fields in the register are ignored.
47384f9bd12SAlex Elder 		 */
47484f9bd12SAlex Elder 		ipa_cmd_register_write_add(trans, offset, 0, ~0, false);
47584f9bd12SAlex Elder 	}
47684f9bd12SAlex Elder 
477aa56e3e5SAlex Elder 	ipa_cmd_pipeline_clear_add(trans);
47884f9bd12SAlex Elder 
47984f9bd12SAlex Elder 	gsi_trans_commit_wait(trans);
48084f9bd12SAlex Elder 
48151c48ce2SAlex Elder 	ipa_cmd_pipeline_clear_wait(ipa);
48251c48ce2SAlex Elder 
48384f9bd12SAlex Elder 	return 0;
48484f9bd12SAlex Elder }
48584f9bd12SAlex Elder 
48684f9bd12SAlex Elder static void ipa_endpoint_init_cfg(struct ipa_endpoint *endpoint)
48784f9bd12SAlex Elder {
48884f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_CFG_N_OFFSET(endpoint->endpoint_id);
4895567d4d9SAlex Elder 	enum ipa_cs_offload_en enabled;
49084f9bd12SAlex Elder 	u32 val = 0;
49184f9bd12SAlex Elder 
49284f9bd12SAlex Elder 	/* FRAG_OFFLOAD_EN is 0 */
493660e52d6SAlex Elder 	if (endpoint->config.checksum) {
4945567d4d9SAlex Elder 		enum ipa_version version = endpoint->ipa->version;
4955567d4d9SAlex Elder 
49684f9bd12SAlex Elder 		if (endpoint->toward_ipa) {
49784f9bd12SAlex Elder 			u32 checksum_offset;
49884f9bd12SAlex Elder 
49984f9bd12SAlex Elder 			/* Checksum header offset is in 4-byte units */
50084f9bd12SAlex Elder 			checksum_offset = sizeof(struct rmnet_map_header);
50184f9bd12SAlex Elder 			checksum_offset /= sizeof(u32);
50284f9bd12SAlex Elder 			val |= u32_encode_bits(checksum_offset,
50384f9bd12SAlex Elder 					       CS_METADATA_HDR_OFFSET_FMASK);
5045567d4d9SAlex Elder 
5055567d4d9SAlex Elder 			enabled = version < IPA_VERSION_4_5
5065567d4d9SAlex Elder 					? IPA_CS_OFFLOAD_UL
5075567d4d9SAlex Elder 					: IPA_CS_OFFLOAD_INLINE;
50884f9bd12SAlex Elder 		} else {
5095567d4d9SAlex Elder 			enabled = version < IPA_VERSION_4_5
5105567d4d9SAlex Elder 					? IPA_CS_OFFLOAD_DL
5115567d4d9SAlex Elder 					: IPA_CS_OFFLOAD_INLINE;
51284f9bd12SAlex Elder 		}
51384f9bd12SAlex Elder 	} else {
5145567d4d9SAlex Elder 		enabled = IPA_CS_OFFLOAD_NONE;
51584f9bd12SAlex Elder 	}
5165567d4d9SAlex Elder 	val |= u32_encode_bits(enabled, CS_OFFLOAD_EN_FMASK);
51784f9bd12SAlex Elder 	/* CS_GEN_QMB_MASTER_SEL is 0 */
51884f9bd12SAlex Elder 
51984f9bd12SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
52084f9bd12SAlex Elder }
52184f9bd12SAlex Elder 
522647a05f3SAlex Elder static void ipa_endpoint_init_nat(struct ipa_endpoint *endpoint)
523647a05f3SAlex Elder {
524647a05f3SAlex Elder 	u32 offset;
525647a05f3SAlex Elder 	u32 val;
526647a05f3SAlex Elder 
527647a05f3SAlex Elder 	if (!endpoint->toward_ipa)
528647a05f3SAlex Elder 		return;
529647a05f3SAlex Elder 
530647a05f3SAlex Elder 	offset = IPA_REG_ENDP_INIT_NAT_N_OFFSET(endpoint->endpoint_id);
531647a05f3SAlex Elder 	val = u32_encode_bits(IPA_NAT_BYPASS, NAT_EN_FMASK);
532647a05f3SAlex Elder 
533647a05f3SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
534647a05f3SAlex Elder }
535647a05f3SAlex Elder 
5365567d4d9SAlex Elder static u32
5375567d4d9SAlex Elder ipa_qmap_header_size(enum ipa_version version, struct ipa_endpoint *endpoint)
5385567d4d9SAlex Elder {
5395567d4d9SAlex Elder 	u32 header_size = sizeof(struct rmnet_map_header);
5405567d4d9SAlex Elder 
5415567d4d9SAlex Elder 	/* Without checksum offload, we just have the MAP header */
542660e52d6SAlex Elder 	if (!endpoint->config.checksum)
5435567d4d9SAlex Elder 		return header_size;
5445567d4d9SAlex Elder 
5455567d4d9SAlex Elder 	if (version < IPA_VERSION_4_5) {
5465567d4d9SAlex Elder 		/* Checksum header inserted for AP TX endpoints only */
5475567d4d9SAlex Elder 		if (endpoint->toward_ipa)
5485567d4d9SAlex Elder 			header_size += sizeof(struct rmnet_map_ul_csum_header);
5495567d4d9SAlex Elder 	} else {
5505567d4d9SAlex Elder 		/* Checksum header is used in both directions */
5515567d4d9SAlex Elder 		header_size += sizeof(struct rmnet_map_v5_csum_header);
5525567d4d9SAlex Elder 	}
5535567d4d9SAlex Elder 
5545567d4d9SAlex Elder 	return header_size;
5555567d4d9SAlex Elder }
5565567d4d9SAlex Elder 
5578730f45dSAlex Elder /**
558e3eea08eSAlex Elder  * ipa_endpoint_init_hdr() - Initialize HDR endpoint configuration register
559e3eea08eSAlex Elder  * @endpoint:	Endpoint pointer
560e3eea08eSAlex Elder  *
5618730f45dSAlex Elder  * We program QMAP endpoints so each packet received is preceded by a QMAP
5628730f45dSAlex Elder  * header structure.  The QMAP header contains a 1-byte mux_id and 2-byte
5638730f45dSAlex Elder  * packet size field, and we have the IPA hardware populate both for each
5648730f45dSAlex Elder  * received packet.  The header is configured (in the HDR_EXT register)
5658730f45dSAlex Elder  * to use big endian format.
5668730f45dSAlex Elder  *
5678730f45dSAlex Elder  * The packet size is written into the QMAP header's pkt_len field.  That
5688730f45dSAlex Elder  * location is defined here using the HDR_OFST_PKT_SIZE field.
5698730f45dSAlex Elder  *
5708730f45dSAlex Elder  * The mux_id comes from a 4-byte metadata value supplied with each packet
5718730f45dSAlex Elder  * by the modem.  It is *not* a QMAP header, but it does contain the mux_id
5728730f45dSAlex Elder  * value that we want, in its low-order byte.  A bitmask defined in the
5738730f45dSAlex Elder  * endpoint's METADATA_MASK register defines which byte within the modem
5748730f45dSAlex Elder  * metadata contains the mux_id.  And the OFST_METADATA field programmed
5758730f45dSAlex Elder  * here indicates where the extracted byte should be placed within the QMAP
5768730f45dSAlex Elder  * header.
5778730f45dSAlex Elder  */
57884f9bd12SAlex Elder static void ipa_endpoint_init_hdr(struct ipa_endpoint *endpoint)
57984f9bd12SAlex Elder {
58084f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_HDR_N_OFFSET(endpoint->endpoint_id);
5811af15c2aSAlex Elder 	struct ipa *ipa = endpoint->ipa;
58284f9bd12SAlex Elder 	u32 val = 0;
58384f9bd12SAlex Elder 
584660e52d6SAlex Elder 	if (endpoint->config.qmap) {
5851af15c2aSAlex Elder 		enum ipa_version version = ipa->version;
5865567d4d9SAlex Elder 		size_t header_size;
58784f9bd12SAlex Elder 
5885567d4d9SAlex Elder 		header_size = ipa_qmap_header_size(version, endpoint);
5895567d4d9SAlex Elder 		val = ipa_header_size_encoded(version, header_size);
59084f9bd12SAlex Elder 
591f330fda3SAlex Elder 		/* Define how to fill fields in a received QMAP header */
5928730f45dSAlex Elder 		if (!endpoint->toward_ipa) {
5931af15c2aSAlex Elder 			u32 offset;	/* Field offset within header */
5948730f45dSAlex Elder 
5958730f45dSAlex Elder 			/* Where IPA will write the metadata value */
5961af15c2aSAlex Elder 			offset = offsetof(struct rmnet_map_header, mux_id);
5971af15c2aSAlex Elder 			val |= ipa_metadata_offset_encoded(version, offset);
5988730f45dSAlex Elder 
5998730f45dSAlex Elder 			/* Where IPA will write the length */
6001af15c2aSAlex Elder 			offset = offsetof(struct rmnet_map_header, pkt_len);
6011af15c2aSAlex Elder 			/* Upper bits are stored in HDR_EXT with IPA v4.5 */
602d7f3087bSAlex Elder 			if (version >= IPA_VERSION_4_5)
6031af15c2aSAlex Elder 				offset &= field_mask(HDR_OFST_PKT_SIZE_FMASK);
6041af15c2aSAlex Elder 
60584f9bd12SAlex Elder 			val |= HDR_OFST_PKT_SIZE_VALID_FMASK;
6061af15c2aSAlex Elder 			val |= u32_encode_bits(offset, HDR_OFST_PKT_SIZE_FMASK);
60784f9bd12SAlex Elder 		}
6088730f45dSAlex Elder 		/* For QMAP TX, metadata offset is 0 (modem assumes this) */
6098730f45dSAlex Elder 		val |= HDR_OFST_METADATA_VALID_FMASK;
6108730f45dSAlex Elder 
6118730f45dSAlex Elder 		/* HDR_ADDITIONAL_CONST_LEN is 0; (RX only) */
61284f9bd12SAlex Elder 		/* HDR_A5_MUX is 0 */
61384f9bd12SAlex Elder 		/* HDR_LEN_INC_DEAGG_HDR is 0 */
6148bfc4e21SAlex Elder 		/* HDR_METADATA_REG_VALID is 0 (TX only, version < v4.5) */
61584f9bd12SAlex Elder 	}
61684f9bd12SAlex Elder 
6171af15c2aSAlex Elder 	iowrite32(val, ipa->reg_virt + offset);
61884f9bd12SAlex Elder }
61984f9bd12SAlex Elder 
62084f9bd12SAlex Elder static void ipa_endpoint_init_hdr_ext(struct ipa_endpoint *endpoint)
62184f9bd12SAlex Elder {
62284f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_HDR_EXT_N_OFFSET(endpoint->endpoint_id);
623660e52d6SAlex Elder 	u32 pad_align = endpoint->config.rx.pad_align;
6241af15c2aSAlex Elder 	struct ipa *ipa = endpoint->ipa;
62584f9bd12SAlex Elder 	u32 val = 0;
62684f9bd12SAlex Elder 
627660e52d6SAlex Elder 	if (endpoint->config.qmap) {
628332ef7c8SAlex Elder 		/* We have a header, so we must specify its endianness */
62984f9bd12SAlex Elder 		val |= HDR_ENDIANNESS_FMASK;	/* big endian */
630f330fda3SAlex Elder 
631332ef7c8SAlex Elder 		/* A QMAP header contains a 6 bit pad field at offset 0.
632332ef7c8SAlex Elder 		 * The RMNet driver assumes this field is meaningful in
633332ef7c8SAlex Elder 		 * packets it receives, and assumes the header's payload
634332ef7c8SAlex Elder 		 * length includes that padding.  The RMNet driver does
635332ef7c8SAlex Elder 		 * *not* pad packets it sends, however, so the pad field
636332ef7c8SAlex Elder 		 * (although 0) should be ignored.
637f330fda3SAlex Elder 		 */
638332ef7c8SAlex Elder 		if (!endpoint->toward_ipa) {
63984f9bd12SAlex Elder 			val |= HDR_TOTAL_LEN_OR_PAD_VALID_FMASK;
64084f9bd12SAlex Elder 			/* HDR_TOTAL_LEN_OR_PAD is 0 (pad, not total_len) */
641f330fda3SAlex Elder 			val |= HDR_PAYLOAD_LEN_INC_PADDING_FMASK;
64284f9bd12SAlex Elder 			/* HDR_TOTAL_LEN_OR_PAD_OFFSET is 0 */
643f330fda3SAlex Elder 		}
644332ef7c8SAlex Elder 	}
645f330fda3SAlex Elder 
646f330fda3SAlex Elder 	/* HDR_PAYLOAD_LEN_INC_PADDING is 0 */
64784f9bd12SAlex Elder 	if (!endpoint->toward_ipa)
64884f9bd12SAlex Elder 		val |= u32_encode_bits(pad_align, HDR_PAD_TO_ALIGNMENT_FMASK);
64984f9bd12SAlex Elder 
6501af15c2aSAlex Elder 	/* IPA v4.5 adds some most-significant bits to a few fields,
6511af15c2aSAlex Elder 	 * two of which are defined in the HDR (not HDR_EXT) register.
6521af15c2aSAlex Elder 	 */
653d7f3087bSAlex Elder 	if (ipa->version >= IPA_VERSION_4_5) {
6541af15c2aSAlex Elder 		/* HDR_TOTAL_LEN_OR_PAD_OFFSET is 0, so MSB is 0 */
655660e52d6SAlex Elder 		if (endpoint->config.qmap && !endpoint->toward_ipa) {
6561af15c2aSAlex Elder 			u32 offset;
65784f9bd12SAlex Elder 
6581af15c2aSAlex Elder 			offset = offsetof(struct rmnet_map_header, pkt_len);
6591af15c2aSAlex Elder 			offset >>= hweight32(HDR_OFST_PKT_SIZE_FMASK);
6601af15c2aSAlex Elder 			val |= u32_encode_bits(offset,
6611af15c2aSAlex Elder 					       HDR_OFST_PKT_SIZE_MSB_FMASK);
6621af15c2aSAlex Elder 			/* HDR_ADDITIONAL_CONST_LEN is 0 so MSB is 0 */
6631af15c2aSAlex Elder 		}
6641af15c2aSAlex Elder 	}
6651af15c2aSAlex Elder 	iowrite32(val, ipa->reg_virt + offset);
6661af15c2aSAlex Elder }
66784f9bd12SAlex Elder 
66884f9bd12SAlex Elder static void ipa_endpoint_init_hdr_metadata_mask(struct ipa_endpoint *endpoint)
66984f9bd12SAlex Elder {
67084f9bd12SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
67184f9bd12SAlex Elder 	u32 val = 0;
67284f9bd12SAlex Elder 	u32 offset;
67384f9bd12SAlex Elder 
674fb57c3eaSAlex Elder 	if (endpoint->toward_ipa)
675fb57c3eaSAlex Elder 		return;		/* Register not valid for TX endpoints */
676fb57c3eaSAlex Elder 
67784f9bd12SAlex Elder 	offset = IPA_REG_ENDP_INIT_HDR_METADATA_MASK_N_OFFSET(endpoint_id);
67884f9bd12SAlex Elder 
6798730f45dSAlex Elder 	/* Note that HDR_ENDIANNESS indicates big endian header fields */
680660e52d6SAlex Elder 	if (endpoint->config.qmap)
681088f8a23SAlex Elder 		val = (__force u32)cpu_to_be32(IPA_ENDPOINT_QMAP_METADATA_MASK);
68284f9bd12SAlex Elder 
68384f9bd12SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
68484f9bd12SAlex Elder }
68584f9bd12SAlex Elder 
68684f9bd12SAlex Elder static void ipa_endpoint_init_mode(struct ipa_endpoint *endpoint)
68784f9bd12SAlex Elder {
68884f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_MODE_N_OFFSET(endpoint->endpoint_id);
68984f9bd12SAlex Elder 	u32 val;
69084f9bd12SAlex Elder 
691fb57c3eaSAlex Elder 	if (!endpoint->toward_ipa)
692fb57c3eaSAlex Elder 		return;		/* Register not valid for RX endpoints */
693fb57c3eaSAlex Elder 
694660e52d6SAlex Elder 	if (endpoint->config.dma_mode) {
695660e52d6SAlex Elder 		enum ipa_endpoint_name name = endpoint->config.dma_endpoint;
69684f9bd12SAlex Elder 		u32 dma_endpoint_id;
69784f9bd12SAlex Elder 
69884f9bd12SAlex Elder 		dma_endpoint_id = endpoint->ipa->name_map[name]->endpoint_id;
69984f9bd12SAlex Elder 
70084f9bd12SAlex Elder 		val = u32_encode_bits(IPA_DMA, MODE_FMASK);
70184f9bd12SAlex Elder 		val |= u32_encode_bits(dma_endpoint_id, DEST_PIPE_INDEX_FMASK);
70284f9bd12SAlex Elder 	} else {
70384f9bd12SAlex Elder 		val = u32_encode_bits(IPA_BASIC, MODE_FMASK);
70484f9bd12SAlex Elder 	}
70500b9102aSAlex Elder 	/* All other bits unspecified (and 0) */
70684f9bd12SAlex Elder 
70784f9bd12SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
70884f9bd12SAlex Elder }
70984f9bd12SAlex Elder 
7106bf754c7SAlex Elder /* Encoded values for AGGR endpoint register fields */
7116bf754c7SAlex Elder static u32 aggr_byte_limit_encoded(enum ipa_version version, u32 limit)
7126bf754c7SAlex Elder {
7136bf754c7SAlex Elder 	if (version < IPA_VERSION_4_5)
7146bf754c7SAlex Elder 		return u32_encode_bits(limit, aggr_byte_limit_fmask(true));
7156bf754c7SAlex Elder 
7166bf754c7SAlex Elder 	return u32_encode_bits(limit, aggr_byte_limit_fmask(false));
7176bf754c7SAlex Elder }
7186bf754c7SAlex Elder 
71919547041SAlex Elder /* Encode the aggregation timer limit (microseconds) based on IPA version */
7206bf754c7SAlex Elder static u32 aggr_time_limit_encoded(enum ipa_version version, u32 limit)
7216bf754c7SAlex Elder {
72219547041SAlex Elder 	u32 gran_sel;
72319547041SAlex Elder 	u32 fmask;
72419547041SAlex Elder 	u32 val;
7256bf754c7SAlex Elder 
72619547041SAlex Elder 	if (version < IPA_VERSION_4_5) {
72719547041SAlex Elder 		/* We set aggregation granularity in ipa_hardware_config() */
728beb90cbaSAlex Elder 		fmask = aggr_time_limit_fmask(true);
729beb90cbaSAlex Elder 		val = DIV_ROUND_CLOSEST(limit, IPA_AGGR_GRANULARITY);
730beb90cbaSAlex Elder 		WARN(val > field_max(fmask),
731beb90cbaSAlex Elder 		     "aggr_time_limit too large (%u > %u usec)\n",
732beb90cbaSAlex Elder 		     val, field_max(fmask) * IPA_AGGR_GRANULARITY);
73319547041SAlex Elder 
734beb90cbaSAlex Elder 		return u32_encode_bits(val, fmask);
73519547041SAlex Elder 	}
73619547041SAlex Elder 
73719547041SAlex Elder 	/* IPA v4.5 expresses the time limit using Qtime.  The AP has
73819547041SAlex Elder 	 * pulse generators 0 and 1 available, which were configured
73919547041SAlex Elder 	 * in ipa_qtime_config() to have granularity 100 usec and
74019547041SAlex Elder 	 * 1 msec, respectively.  Use pulse generator 0 if possible,
74119547041SAlex Elder 	 * otherwise fall back to pulse generator 1.
74219547041SAlex Elder 	 */
74319547041SAlex Elder 	fmask = aggr_time_limit_fmask(false);
74419547041SAlex Elder 	val = DIV_ROUND_CLOSEST(limit, 100);
74519547041SAlex Elder 	if (val > field_max(fmask)) {
74619547041SAlex Elder 		/* Have to use pulse generator 1 (millisecond granularity) */
74719547041SAlex Elder 		gran_sel = AGGR_GRAN_SEL_FMASK;
74819547041SAlex Elder 		val = DIV_ROUND_CLOSEST(limit, 1000);
749beb90cbaSAlex Elder 		WARN(val > field_max(fmask),
750beb90cbaSAlex Elder 		     "aggr_time_limit too large (%u > %u usec)\n",
751beb90cbaSAlex Elder 		     limit, field_max(fmask) * 1000);
75219547041SAlex Elder 	} else {
75319547041SAlex Elder 		/* We can use pulse generator 0 (100 usec granularity) */
75419547041SAlex Elder 		gran_sel = 0;
75519547041SAlex Elder 	}
75619547041SAlex Elder 
75719547041SAlex Elder 	return gran_sel | u32_encode_bits(val, fmask);
7586bf754c7SAlex Elder }
7596bf754c7SAlex Elder 
7606bf754c7SAlex Elder static u32 aggr_sw_eof_active_encoded(enum ipa_version version, bool enabled)
7616bf754c7SAlex Elder {
7626bf754c7SAlex Elder 	u32 val = enabled ? 1 : 0;
7636bf754c7SAlex Elder 
7646bf754c7SAlex Elder 	if (version < IPA_VERSION_4_5)
7656bf754c7SAlex Elder 		return u32_encode_bits(val, aggr_sw_eof_active_fmask(true));
7666bf754c7SAlex Elder 
7676bf754c7SAlex Elder 	return u32_encode_bits(val, aggr_sw_eof_active_fmask(false));
7686bf754c7SAlex Elder }
7696bf754c7SAlex Elder 
77084f9bd12SAlex Elder static void ipa_endpoint_init_aggr(struct ipa_endpoint *endpoint)
77184f9bd12SAlex Elder {
77284f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_AGGR_N_OFFSET(endpoint->endpoint_id);
7736bf754c7SAlex Elder 	enum ipa_version version = endpoint->ipa->version;
77484f9bd12SAlex Elder 	u32 val = 0;
77584f9bd12SAlex Elder 
776660e52d6SAlex Elder 	if (endpoint->config.aggregation) {
77784f9bd12SAlex Elder 		if (!endpoint->toward_ipa) {
778cf4e73a1SAlex Elder 			const struct ipa_endpoint_rx *rx_config;
779c5794097SAlex Elder 			u32 buffer_size;
7806bf754c7SAlex Elder 			bool close_eof;
78184f9bd12SAlex Elder 			u32 limit;
78284f9bd12SAlex Elder 
783660e52d6SAlex Elder 			rx_config = &endpoint->config.rx;
78484f9bd12SAlex Elder 			val |= u32_encode_bits(IPA_ENABLE_AGGR, AGGR_EN_FMASK);
78584f9bd12SAlex Elder 			val |= u32_encode_bits(IPA_GENERIC, AGGR_TYPE_FMASK);
7869e88cb5fSAlex Elder 
787cf4e73a1SAlex Elder 			buffer_size = rx_config->buffer_size;
7883cebb7c2SAlex Elder 			limit = ipa_aggr_size_kb(buffer_size - NET_SKB_PAD,
7893cebb7c2SAlex Elder 						 rx_config->aggr_hard_limit);
7906bf754c7SAlex Elder 			val |= aggr_byte_limit_encoded(version, limit);
7911d86652bSAlex Elder 
792beb90cbaSAlex Elder 			limit = rx_config->aggr_time_limit;
7936bf754c7SAlex Elder 			val |= aggr_time_limit_encoded(version, limit);
7941d86652bSAlex Elder 
7959e88cb5fSAlex Elder 			/* AGGR_PKT_LIMIT is 0 (unlimited) */
7969e88cb5fSAlex Elder 
797cf4e73a1SAlex Elder 			close_eof = rx_config->aggr_close_eof;
7986bf754c7SAlex Elder 			val |= aggr_sw_eof_active_encoded(version, close_eof);
79984f9bd12SAlex Elder 		} else {
80084f9bd12SAlex Elder 			val |= u32_encode_bits(IPA_ENABLE_DEAGGR,
80184f9bd12SAlex Elder 					       AGGR_EN_FMASK);
80284f9bd12SAlex Elder 			val |= u32_encode_bits(IPA_QCMAP, AGGR_TYPE_FMASK);
80384f9bd12SAlex Elder 			/* other fields ignored */
80484f9bd12SAlex Elder 		}
80584f9bd12SAlex Elder 		/* AGGR_FORCE_CLOSE is 0 */
8068bfc4e21SAlex Elder 		/* AGGR_GRAN_SEL is 0 for IPA v4.5 */
80784f9bd12SAlex Elder 	} else {
80884f9bd12SAlex Elder 		val |= u32_encode_bits(IPA_BYPASS_AGGR, AGGR_EN_FMASK);
80984f9bd12SAlex Elder 		/* other fields ignored */
81084f9bd12SAlex Elder 	}
81184f9bd12SAlex Elder 
81284f9bd12SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
81384f9bd12SAlex Elder }
81484f9bd12SAlex Elder 
81563e5afc8SAlex Elder /* Return the Qtime-based head-of-line blocking timer value that
81663e5afc8SAlex Elder  * represents the given number of microseconds.  The result
81763e5afc8SAlex Elder  * includes both the timer value and the selected timer granularity.
818f13a8c31SAlex Elder  */
81963e5afc8SAlex Elder static u32 hol_block_timer_qtime_val(struct ipa *ipa, u32 microseconds)
82063e5afc8SAlex Elder {
82163e5afc8SAlex Elder 	u32 gran_sel;
82263e5afc8SAlex Elder 	u32 val;
82363e5afc8SAlex Elder 
82463e5afc8SAlex Elder 	/* IPA v4.5 expresses time limits using Qtime.  The AP has
82563e5afc8SAlex Elder 	 * pulse generators 0 and 1 available, which were configured
82663e5afc8SAlex Elder 	 * in ipa_qtime_config() to have granularity 100 usec and
82763e5afc8SAlex Elder 	 * 1 msec, respectively.  Use pulse generator 0 if possible,
82863e5afc8SAlex Elder 	 * otherwise fall back to pulse generator 1.
82963e5afc8SAlex Elder 	 */
83063e5afc8SAlex Elder 	val = DIV_ROUND_CLOSEST(microseconds, 100);
83163e5afc8SAlex Elder 	if (val > field_max(TIME_LIMIT_FMASK)) {
83263e5afc8SAlex Elder 		/* Have to use pulse generator 1 (millisecond granularity) */
83363e5afc8SAlex Elder 		gran_sel = GRAN_SEL_FMASK;
83463e5afc8SAlex Elder 		val = DIV_ROUND_CLOSEST(microseconds, 1000);
83563e5afc8SAlex Elder 	} else {
83663e5afc8SAlex Elder 		/* We can use pulse generator 0 (100 usec granularity) */
83763e5afc8SAlex Elder 		gran_sel = 0;
83863e5afc8SAlex Elder 	}
83963e5afc8SAlex Elder 
84063e5afc8SAlex Elder 	return gran_sel | u32_encode_bits(val, TIME_LIMIT_FMASK);
84163e5afc8SAlex Elder }
84263e5afc8SAlex Elder 
84363e5afc8SAlex Elder /* The head-of-line blocking timer is defined as a tick count.  For
84463e5afc8SAlex Elder  * IPA version 4.5 the tick count is based on the Qtimer, which is
84563e5afc8SAlex Elder  * derived from the 19.2 MHz SoC XO clock.  For older IPA versions
84663e5afc8SAlex Elder  * each tick represents 128 cycles of the IPA core clock.
84763e5afc8SAlex Elder  *
84863e5afc8SAlex Elder  * Return the encoded value that should be written to that register
84963e5afc8SAlex Elder  * that represents the timeout period provided.  For IPA v4.2 this
85063e5afc8SAlex Elder  * encodes a base and scale value, while for earlier versions the
85163e5afc8SAlex Elder  * value is a simple tick count.
85263e5afc8SAlex Elder  */
85363e5afc8SAlex Elder static u32 hol_block_timer_val(struct ipa *ipa, u32 microseconds)
85484f9bd12SAlex Elder {
855f13a8c31SAlex Elder 	u32 width;
85684f9bd12SAlex Elder 	u32 scale;
857f13a8c31SAlex Elder 	u64 ticks;
858f13a8c31SAlex Elder 	u64 rate;
859f13a8c31SAlex Elder 	u32 high;
86084f9bd12SAlex Elder 	u32 val;
86184f9bd12SAlex Elder 
86284f9bd12SAlex Elder 	if (!microseconds)
863f13a8c31SAlex Elder 		return 0;	/* Nothing to compute if timer period is 0 */
86484f9bd12SAlex Elder 
865d7f3087bSAlex Elder 	if (ipa->version >= IPA_VERSION_4_5)
86663e5afc8SAlex Elder 		return hol_block_timer_qtime_val(ipa, microseconds);
86763e5afc8SAlex Elder 
868f13a8c31SAlex Elder 	/* Use 64 bit arithmetic to avoid overflow... */
8697aa0e8b8SAlex Elder 	rate = ipa_core_clock_rate(ipa);
870f13a8c31SAlex Elder 	ticks = DIV_ROUND_CLOSEST(microseconds * rate, 128 * USEC_PER_SEC);
871f13a8c31SAlex Elder 	/* ...but we still need to fit into a 32-bit register */
872f13a8c31SAlex Elder 	WARN_ON(ticks > U32_MAX);
87384f9bd12SAlex Elder 
8746833a096SAlex Elder 	/* IPA v3.5.1 through v4.1 just record the tick count */
8756833a096SAlex Elder 	if (ipa->version < IPA_VERSION_4_2)
876f13a8c31SAlex Elder 		return (u32)ticks;
87784f9bd12SAlex Elder 
878f13a8c31SAlex Elder 	/* For IPA v4.2, the tick count is represented by base and
879f13a8c31SAlex Elder 	 * scale fields within the 32-bit timer register, where:
880f13a8c31SAlex Elder 	 *     ticks = base << scale;
881f13a8c31SAlex Elder 	 * The best precision is achieved when the base value is as
882f13a8c31SAlex Elder 	 * large as possible.  Find the highest set bit in the tick
883f13a8c31SAlex Elder 	 * count, and extract the number of bits in the base field
884497abc87SPeng Li 	 * such that high bit is included.
885f13a8c31SAlex Elder 	 */
886f13a8c31SAlex Elder 	high = fls(ticks);		/* 1..32 */
887f13a8c31SAlex Elder 	width = HWEIGHT32(BASE_VALUE_FMASK);
888f13a8c31SAlex Elder 	scale = high > width ? high - width : 0;
889f13a8c31SAlex Elder 	if (scale) {
890f13a8c31SAlex Elder 		/* If we're scaling, round up to get a closer result */
891f13a8c31SAlex Elder 		ticks += 1 << (scale - 1);
892f13a8c31SAlex Elder 		/* High bit was set, so rounding might have affected it */
893f13a8c31SAlex Elder 		if (fls(ticks) != high)
894f13a8c31SAlex Elder 			scale++;
895f13a8c31SAlex Elder 	}
89684f9bd12SAlex Elder 
89784f9bd12SAlex Elder 	val = u32_encode_bits(scale, SCALE_FMASK);
898f13a8c31SAlex Elder 	val |= u32_encode_bits(ticks >> scale, BASE_VALUE_FMASK);
89984f9bd12SAlex Elder 
90084f9bd12SAlex Elder 	return val;
90184f9bd12SAlex Elder }
90284f9bd12SAlex Elder 
903f13a8c31SAlex Elder /* If microseconds is 0, timeout is immediate */
904f13a8c31SAlex Elder static void ipa_endpoint_init_hol_block_timer(struct ipa_endpoint *endpoint,
90584f9bd12SAlex Elder 					      u32 microseconds)
90684f9bd12SAlex Elder {
90784f9bd12SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
90884f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
90984f9bd12SAlex Elder 	u32 offset;
91084f9bd12SAlex Elder 	u32 val;
91184f9bd12SAlex Elder 
912816316caSAlex Elder 	/* This should only be changed when HOL_BLOCK_EN is disabled */
91384f9bd12SAlex Elder 	offset = IPA_REG_ENDP_INIT_HOL_BLOCK_TIMER_N_OFFSET(endpoint_id);
91463e5afc8SAlex Elder 	val = hol_block_timer_val(ipa, microseconds);
91584f9bd12SAlex Elder 	iowrite32(val, ipa->reg_virt + offset);
91684f9bd12SAlex Elder }
91784f9bd12SAlex Elder 
91884f9bd12SAlex Elder static void
919e6aab6b9SAlex Elder ipa_endpoint_init_hol_block_en(struct ipa_endpoint *endpoint, bool enable)
92084f9bd12SAlex Elder {
92184f9bd12SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
92284f9bd12SAlex Elder 	u32 offset;
92384f9bd12SAlex Elder 	u32 val;
92484f9bd12SAlex Elder 
925547c8788SAlex Elder 	val = enable ? HOL_BLOCK_EN_FMASK : 0;
92684f9bd12SAlex Elder 	offset = IPA_REG_ENDP_INIT_HOL_BLOCK_EN_N_OFFSET(endpoint_id);
92784f9bd12SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
9286e228d8cSAlex Elder 	/* When enabling, the register must be written twice for IPA v4.5+ */
9296e228d8cSAlex Elder 	if (enable && endpoint->ipa->version >= IPA_VERSION_4_5)
9306e228d8cSAlex Elder 		iowrite32(val, endpoint->ipa->reg_virt + offset);
93184f9bd12SAlex Elder }
93284f9bd12SAlex Elder 
933e6aab6b9SAlex Elder /* Assumes HOL_BLOCK is in disabled state */
934e6aab6b9SAlex Elder static void ipa_endpoint_init_hol_block_enable(struct ipa_endpoint *endpoint,
935e6aab6b9SAlex Elder 					       u32 microseconds)
936e6aab6b9SAlex Elder {
937e6aab6b9SAlex Elder 	ipa_endpoint_init_hol_block_timer(endpoint, microseconds);
938e6aab6b9SAlex Elder 	ipa_endpoint_init_hol_block_en(endpoint, true);
939e6aab6b9SAlex Elder }
940e6aab6b9SAlex Elder 
941e6aab6b9SAlex Elder static void ipa_endpoint_init_hol_block_disable(struct ipa_endpoint *endpoint)
942e6aab6b9SAlex Elder {
943e6aab6b9SAlex Elder 	ipa_endpoint_init_hol_block_en(endpoint, false);
944e6aab6b9SAlex Elder }
945e6aab6b9SAlex Elder 
94684f9bd12SAlex Elder void ipa_endpoint_modem_hol_block_clear_all(struct ipa *ipa)
94784f9bd12SAlex Elder {
94884f9bd12SAlex Elder 	u32 i;
94984f9bd12SAlex Elder 
95084f9bd12SAlex Elder 	for (i = 0; i < IPA_ENDPOINT_MAX; i++) {
95184f9bd12SAlex Elder 		struct ipa_endpoint *endpoint = &ipa->endpoint[i];
95284f9bd12SAlex Elder 
953f8d34dfdSAlex Elder 		if (endpoint->toward_ipa || endpoint->ee_id != GSI_EE_MODEM)
95484f9bd12SAlex Elder 			continue;
95584f9bd12SAlex Elder 
956e6aab6b9SAlex Elder 		ipa_endpoint_init_hol_block_disable(endpoint);
957e6aab6b9SAlex Elder 		ipa_endpoint_init_hol_block_enable(endpoint, 0);
95884f9bd12SAlex Elder 	}
95984f9bd12SAlex Elder }
96084f9bd12SAlex Elder 
96184f9bd12SAlex Elder static void ipa_endpoint_init_deaggr(struct ipa_endpoint *endpoint)
96284f9bd12SAlex Elder {
96384f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_DEAGGR_N_OFFSET(endpoint->endpoint_id);
96484f9bd12SAlex Elder 	u32 val = 0;
96584f9bd12SAlex Elder 
966fb57c3eaSAlex Elder 	if (!endpoint->toward_ipa)
967fb57c3eaSAlex Elder 		return;		/* Register not valid for RX endpoints */
968fb57c3eaSAlex Elder 
96984f9bd12SAlex Elder 	/* DEAGGR_HDR_LEN is 0 */
97084f9bd12SAlex Elder 	/* PACKET_OFFSET_VALID is 0 */
97184f9bd12SAlex Elder 	/* PACKET_OFFSET_LOCATION is ignored (not valid) */
97284f9bd12SAlex Elder 	/* MAX_PACKET_LEN is 0 (not enforced) */
97384f9bd12SAlex Elder 
97484f9bd12SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
97584f9bd12SAlex Elder }
97684f9bd12SAlex Elder 
9772d265342SAlex Elder static void ipa_endpoint_init_rsrc_grp(struct ipa_endpoint *endpoint)
9782d265342SAlex Elder {
9792d265342SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_RSRC_GRP_N_OFFSET(endpoint->endpoint_id);
9802d265342SAlex Elder 	struct ipa *ipa = endpoint->ipa;
9812d265342SAlex Elder 	u32 val;
9822d265342SAlex Elder 
983660e52d6SAlex Elder 	val = rsrc_grp_encoded(ipa->version, endpoint->config.resource_group);
9842d265342SAlex Elder 	iowrite32(val, ipa->reg_virt + offset);
9852d265342SAlex Elder }
9862d265342SAlex Elder 
98784f9bd12SAlex Elder static void ipa_endpoint_init_seq(struct ipa_endpoint *endpoint)
98884f9bd12SAlex Elder {
98984f9bd12SAlex Elder 	u32 offset = IPA_REG_ENDP_INIT_SEQ_N_OFFSET(endpoint->endpoint_id);
99084f9bd12SAlex Elder 	u32 val = 0;
99184f9bd12SAlex Elder 
992fb57c3eaSAlex Elder 	if (!endpoint->toward_ipa)
993fb57c3eaSAlex Elder 		return;		/* Register not valid for RX endpoints */
994fb57c3eaSAlex Elder 
9958ee5df65SAlex Elder 	/* Low-order byte configures primary packet processing */
996660e52d6SAlex Elder 	val |= u32_encode_bits(endpoint->config.tx.seq_type, SEQ_TYPE_FMASK);
9978ee5df65SAlex Elder 
9988ee5df65SAlex Elder 	/* Second byte configures replicated packet processing */
999660e52d6SAlex Elder 	val |= u32_encode_bits(endpoint->config.tx.seq_rep_type,
10001690d8a7SAlex Elder 			       SEQ_REP_TYPE_FMASK);
100184f9bd12SAlex Elder 
100284f9bd12SAlex Elder 	iowrite32(val, endpoint->ipa->reg_virt + offset);
100384f9bd12SAlex Elder }
100484f9bd12SAlex Elder 
100584f9bd12SAlex Elder /**
100684f9bd12SAlex Elder  * ipa_endpoint_skb_tx() - Transmit a socket buffer
100784f9bd12SAlex Elder  * @endpoint:	Endpoint pointer
100884f9bd12SAlex Elder  * @skb:	Socket buffer to send
100984f9bd12SAlex Elder  *
101084f9bd12SAlex Elder  * Returns:	0 if successful, or a negative error code
101184f9bd12SAlex Elder  */
101284f9bd12SAlex Elder int ipa_endpoint_skb_tx(struct ipa_endpoint *endpoint, struct sk_buff *skb)
101384f9bd12SAlex Elder {
101484f9bd12SAlex Elder 	struct gsi_trans *trans;
101584f9bd12SAlex Elder 	u32 nr_frags;
101684f9bd12SAlex Elder 	int ret;
101784f9bd12SAlex Elder 
101884f9bd12SAlex Elder 	/* Make sure source endpoint's TLV FIFO has enough entries to
101984f9bd12SAlex Elder 	 * hold the linear portion of the skb and all its fragments.
102084f9bd12SAlex Elder 	 * If not, see if we can linearize it before giving up.
102184f9bd12SAlex Elder 	 */
102284f9bd12SAlex Elder 	nr_frags = skb_shinfo(skb)->nr_frags;
102384f9bd12SAlex Elder 	if (1 + nr_frags > endpoint->trans_tre_max) {
102484f9bd12SAlex Elder 		if (skb_linearize(skb))
102584f9bd12SAlex Elder 			return -E2BIG;
102684f9bd12SAlex Elder 		nr_frags = 0;
102784f9bd12SAlex Elder 	}
102884f9bd12SAlex Elder 
102984f9bd12SAlex Elder 	trans = ipa_endpoint_trans_alloc(endpoint, 1 + nr_frags);
103084f9bd12SAlex Elder 	if (!trans)
103184f9bd12SAlex Elder 		return -EBUSY;
103284f9bd12SAlex Elder 
103384f9bd12SAlex Elder 	ret = gsi_trans_skb_add(trans, skb);
103484f9bd12SAlex Elder 	if (ret)
103584f9bd12SAlex Elder 		goto err_trans_free;
103684f9bd12SAlex Elder 	trans->data = skb;	/* transaction owns skb now */
103784f9bd12SAlex Elder 
103884f9bd12SAlex Elder 	gsi_trans_commit(trans, !netdev_xmit_more());
103984f9bd12SAlex Elder 
104084f9bd12SAlex Elder 	return 0;
104184f9bd12SAlex Elder 
104284f9bd12SAlex Elder err_trans_free:
104384f9bd12SAlex Elder 	gsi_trans_free(trans);
104484f9bd12SAlex Elder 
104584f9bd12SAlex Elder 	return -ENOMEM;
104684f9bd12SAlex Elder }
104784f9bd12SAlex Elder 
104884f9bd12SAlex Elder static void ipa_endpoint_status(struct ipa_endpoint *endpoint)
104984f9bd12SAlex Elder {
105084f9bd12SAlex Elder 	u32 endpoint_id = endpoint->endpoint_id;
105184f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
105284f9bd12SAlex Elder 	u32 val = 0;
105384f9bd12SAlex Elder 	u32 offset;
105484f9bd12SAlex Elder 
105584f9bd12SAlex Elder 	offset = IPA_REG_ENDP_STATUS_N_OFFSET(endpoint_id);
105684f9bd12SAlex Elder 
1057660e52d6SAlex Elder 	if (endpoint->config.status_enable) {
105884f9bd12SAlex Elder 		val |= STATUS_EN_FMASK;
105984f9bd12SAlex Elder 		if (endpoint->toward_ipa) {
106084f9bd12SAlex Elder 			enum ipa_endpoint_name name;
106184f9bd12SAlex Elder 			u32 status_endpoint_id;
106284f9bd12SAlex Elder 
1063660e52d6SAlex Elder 			name = endpoint->config.tx.status_endpoint;
106484f9bd12SAlex Elder 			status_endpoint_id = ipa->name_map[name]->endpoint_id;
106584f9bd12SAlex Elder 
106684f9bd12SAlex Elder 			val |= u32_encode_bits(status_endpoint_id,
106784f9bd12SAlex Elder 					       STATUS_ENDP_FMASK);
106884f9bd12SAlex Elder 		}
10698bfc4e21SAlex Elder 		/* STATUS_LOCATION is 0, meaning status element precedes
10708bfc4e21SAlex Elder 		 * packet (not present for IPA v4.5)
10718bfc4e21SAlex Elder 		 */
10728bfc4e21SAlex Elder 		/* STATUS_PKT_SUPPRESS_FMASK is 0 (not present for v3.5.1) */
107384f9bd12SAlex Elder 	}
107484f9bd12SAlex Elder 
107584f9bd12SAlex Elder 	iowrite32(val, ipa->reg_virt + offset);
107684f9bd12SAlex Elder }
107784f9bd12SAlex Elder 
10786a606b90SAlex Elder static int ipa_endpoint_replenish_one(struct ipa_endpoint *endpoint,
10796a606b90SAlex Elder 				      struct gsi_trans *trans)
108084f9bd12SAlex Elder {
108184f9bd12SAlex Elder 	struct page *page;
1082ed23f026SAlex Elder 	u32 buffer_size;
108384f9bd12SAlex Elder 	u32 offset;
108484f9bd12SAlex Elder 	u32 len;
108584f9bd12SAlex Elder 	int ret;
108684f9bd12SAlex Elder 
1087660e52d6SAlex Elder 	buffer_size = endpoint->config.rx.buffer_size;
1088ed23f026SAlex Elder 	page = dev_alloc_pages(get_order(buffer_size));
108984f9bd12SAlex Elder 	if (!page)
10906a606b90SAlex Elder 		return -ENOMEM;
109184f9bd12SAlex Elder 
109284f9bd12SAlex Elder 	/* Offset the buffer to make space for skb headroom */
109384f9bd12SAlex Elder 	offset = NET_SKB_PAD;
1094ed23f026SAlex Elder 	len = buffer_size - offset;
109584f9bd12SAlex Elder 
109684f9bd12SAlex Elder 	ret = gsi_trans_page_add(trans, page, len, offset);
109784f9bd12SAlex Elder 	if (ret)
1098*70132763SAlex Elder 		put_page(page);
10996a606b90SAlex Elder 	else
110084f9bd12SAlex Elder 		trans->data = page;	/* transaction owns page now */
110184f9bd12SAlex Elder 
11026a606b90SAlex Elder 	return ret;
110384f9bd12SAlex Elder }
110484f9bd12SAlex Elder 
110584f9bd12SAlex Elder /**
11069af5ccf3SAlex Elder  * ipa_endpoint_replenish() - Replenish endpoint receive buffers
1107e3eea08eSAlex Elder  * @endpoint:	Endpoint to be replenished
110884f9bd12SAlex Elder  *
11099af5ccf3SAlex Elder  * The IPA hardware can hold a fixed number of receive buffers for an RX
11109af5ccf3SAlex Elder  * endpoint, based on the number of entries in the underlying channel ring
11119af5ccf3SAlex Elder  * buffer.  If an endpoint's "backlog" is non-zero, it indicates how many
11129af5ccf3SAlex Elder  * more receive buffers can be supplied to the hardware.  Replenishing for
1113a9bec7aeSAlex Elder  * an endpoint can be disabled, in which case buffers are not queued to
1114a9bec7aeSAlex Elder  * the hardware.
111584f9bd12SAlex Elder  */
11164b22d841SAlex Elder static void ipa_endpoint_replenish(struct ipa_endpoint *endpoint)
111784f9bd12SAlex Elder {
11186a606b90SAlex Elder 	struct gsi_trans *trans;
111984f9bd12SAlex Elder 
11204b22d841SAlex Elder 	if (!test_bit(IPA_REPLENISH_ENABLED, endpoint->replenish_flags))
112184f9bd12SAlex Elder 		return;
112284f9bd12SAlex Elder 
11234b22d841SAlex Elder 	/* Skip it if it's already active */
11244b22d841SAlex Elder 	if (test_and_set_bit(IPA_REPLENISH_ACTIVE, endpoint->replenish_flags))
1125998c0bd2SAlex Elder 		return;
1126998c0bd2SAlex Elder 
1127d0ac30e7SAlex Elder 	while ((trans = ipa_endpoint_trans_alloc(endpoint, 1))) {
11289654d8c4SAlex Elder 		bool doorbell;
11299654d8c4SAlex Elder 
11306a606b90SAlex Elder 		if (ipa_endpoint_replenish_one(endpoint, trans))
11316a606b90SAlex Elder 			goto try_again_later;
1132b9dbabc5SAlex Elder 
1133b9dbabc5SAlex Elder 
1134b9dbabc5SAlex Elder 		/* Ring the doorbell if we've got a full batch */
11359654d8c4SAlex Elder 		doorbell = !(++endpoint->replenish_count % IPA_REPLENISH_BATCH);
11369654d8c4SAlex Elder 		gsi_trans_commit(trans, doorbell);
1137b9dbabc5SAlex Elder 	}
1138998c0bd2SAlex Elder 
1139998c0bd2SAlex Elder 	clear_bit(IPA_REPLENISH_ACTIVE, endpoint->replenish_flags);
1140998c0bd2SAlex Elder 
114184f9bd12SAlex Elder 	return;
114284f9bd12SAlex Elder 
114384f9bd12SAlex Elder try_again_later:
11446a606b90SAlex Elder 	gsi_trans_free(trans);
1145998c0bd2SAlex Elder 	clear_bit(IPA_REPLENISH_ACTIVE, endpoint->replenish_flags);
1146998c0bd2SAlex Elder 
114784f9bd12SAlex Elder 	/* Whenever a receive buffer transaction completes we'll try to
114884f9bd12SAlex Elder 	 * replenish again.  It's unlikely, but if we fail to supply even
114984f9bd12SAlex Elder 	 * one buffer, nothing will trigger another replenish attempt.
11505fc7f9baSAlex Elder 	 * If the hardware has no receive buffers queued, schedule work to
11515fc7f9baSAlex Elder 	 * try replenishing again.
115284f9bd12SAlex Elder 	 */
11535fc7f9baSAlex Elder 	if (gsi_channel_trans_idle(&endpoint->ipa->gsi, endpoint->channel_id))
115484f9bd12SAlex Elder 		schedule_delayed_work(&endpoint->replenish_work,
115584f9bd12SAlex Elder 				      msecs_to_jiffies(1));
115684f9bd12SAlex Elder }
115784f9bd12SAlex Elder 
115884f9bd12SAlex Elder static void ipa_endpoint_replenish_enable(struct ipa_endpoint *endpoint)
115984f9bd12SAlex Elder {
1160c1aaa01dSAlex Elder 	set_bit(IPA_REPLENISH_ENABLED, endpoint->replenish_flags);
116184f9bd12SAlex Elder 
116284f9bd12SAlex Elder 	/* Start replenishing if hardware currently has no buffers */
11635fc7f9baSAlex Elder 	if (gsi_channel_trans_idle(&endpoint->ipa->gsi, endpoint->channel_id))
11644b22d841SAlex Elder 		ipa_endpoint_replenish(endpoint);
116584f9bd12SAlex Elder }
116684f9bd12SAlex Elder 
116784f9bd12SAlex Elder static void ipa_endpoint_replenish_disable(struct ipa_endpoint *endpoint)
116884f9bd12SAlex Elder {
1169c1aaa01dSAlex Elder 	clear_bit(IPA_REPLENISH_ENABLED, endpoint->replenish_flags);
117084f9bd12SAlex Elder }
117184f9bd12SAlex Elder 
117284f9bd12SAlex Elder static void ipa_endpoint_replenish_work(struct work_struct *work)
117384f9bd12SAlex Elder {
117484f9bd12SAlex Elder 	struct delayed_work *dwork = to_delayed_work(work);
117584f9bd12SAlex Elder 	struct ipa_endpoint *endpoint;
117684f9bd12SAlex Elder 
117784f9bd12SAlex Elder 	endpoint = container_of(dwork, struct ipa_endpoint, replenish_work);
117884f9bd12SAlex Elder 
11794b22d841SAlex Elder 	ipa_endpoint_replenish(endpoint);
118084f9bd12SAlex Elder }
118184f9bd12SAlex Elder 
118284f9bd12SAlex Elder static void ipa_endpoint_skb_copy(struct ipa_endpoint *endpoint,
118384f9bd12SAlex Elder 				  void *data, u32 len, u32 extra)
118484f9bd12SAlex Elder {
118584f9bd12SAlex Elder 	struct sk_buff *skb;
118684f9bd12SAlex Elder 
11871b65bbccSAlex Elder 	if (!endpoint->netdev)
11881b65bbccSAlex Elder 		return;
11891b65bbccSAlex Elder 
119084f9bd12SAlex Elder 	skb = __dev_alloc_skb(len, GFP_ATOMIC);
119130b338ffSAlex Elder 	if (skb) {
11921b65bbccSAlex Elder 		/* Copy the data into the socket buffer and receive it */
119384f9bd12SAlex Elder 		skb_put(skb, len);
119484f9bd12SAlex Elder 		memcpy(skb->data, data, len);
119584f9bd12SAlex Elder 		skb->truesize += extra;
119630b338ffSAlex Elder 	}
119784f9bd12SAlex Elder 
119884f9bd12SAlex Elder 	ipa_modem_skb_rx(endpoint->netdev, skb);
119984f9bd12SAlex Elder }
120084f9bd12SAlex Elder 
120184f9bd12SAlex Elder static bool ipa_endpoint_skb_build(struct ipa_endpoint *endpoint,
120284f9bd12SAlex Elder 				   struct page *page, u32 len)
120384f9bd12SAlex Elder {
1204660e52d6SAlex Elder 	u32 buffer_size = endpoint->config.rx.buffer_size;
120584f9bd12SAlex Elder 	struct sk_buff *skb;
120684f9bd12SAlex Elder 
120784f9bd12SAlex Elder 	/* Nothing to do if there's no netdev */
120884f9bd12SAlex Elder 	if (!endpoint->netdev)
120984f9bd12SAlex Elder 		return false;
121084f9bd12SAlex Elder 
1211ed23f026SAlex Elder 	WARN_ON(len > SKB_WITH_OVERHEAD(buffer_size - NET_SKB_PAD));
12125bc55884SAlex Elder 
1213ed23f026SAlex Elder 	skb = build_skb(page_address(page), buffer_size);
121484f9bd12SAlex Elder 	if (skb) {
121584f9bd12SAlex Elder 		/* Reserve the headroom and account for the data */
121684f9bd12SAlex Elder 		skb_reserve(skb, NET_SKB_PAD);
121784f9bd12SAlex Elder 		skb_put(skb, len);
121884f9bd12SAlex Elder 	}
121984f9bd12SAlex Elder 
122084f9bd12SAlex Elder 	/* Receive the buffer (or record drop if unable to build it) */
122184f9bd12SAlex Elder 	ipa_modem_skb_rx(endpoint->netdev, skb);
122284f9bd12SAlex Elder 
122384f9bd12SAlex Elder 	return skb != NULL;
122484f9bd12SAlex Elder }
122584f9bd12SAlex Elder 
122684f9bd12SAlex Elder /* The format of a packet status element is the same for several status
122745921390SAlex Elder  * types (opcodes).  Other types aren't currently supported.
122884f9bd12SAlex Elder  */
122984f9bd12SAlex Elder static bool ipa_status_format_packet(enum ipa_status_opcode opcode)
123084f9bd12SAlex Elder {
123184f9bd12SAlex Elder 	switch (opcode) {
123284f9bd12SAlex Elder 	case IPA_STATUS_OPCODE_PACKET:
123384f9bd12SAlex Elder 	case IPA_STATUS_OPCODE_DROPPED_PACKET:
123484f9bd12SAlex Elder 	case IPA_STATUS_OPCODE_SUSPENDED_PACKET:
123584f9bd12SAlex Elder 	case IPA_STATUS_OPCODE_PACKET_2ND_PASS:
123684f9bd12SAlex Elder 		return true;
123784f9bd12SAlex Elder 	default:
123884f9bd12SAlex Elder 		return false;
123984f9bd12SAlex Elder 	}
124084f9bd12SAlex Elder }
124184f9bd12SAlex Elder 
124284f9bd12SAlex Elder static bool ipa_endpoint_status_skip(struct ipa_endpoint *endpoint,
124384f9bd12SAlex Elder 				     const struct ipa_status *status)
124484f9bd12SAlex Elder {
124584f9bd12SAlex Elder 	u32 endpoint_id;
124684f9bd12SAlex Elder 
124784f9bd12SAlex Elder 	if (!ipa_status_format_packet(status->opcode))
124884f9bd12SAlex Elder 		return true;
124984f9bd12SAlex Elder 	if (!status->pkt_len)
125084f9bd12SAlex Elder 		return true;
1251c13899f1SAlex Elder 	endpoint_id = u8_get_bits(status->endp_dst_idx,
125284f9bd12SAlex Elder 				  IPA_STATUS_DST_IDX_FMASK);
125384f9bd12SAlex Elder 	if (endpoint_id != endpoint->endpoint_id)
125484f9bd12SAlex Elder 		return true;
125584f9bd12SAlex Elder 
125684f9bd12SAlex Elder 	return false;	/* Don't skip this packet, process it */
125784f9bd12SAlex Elder }
125884f9bd12SAlex Elder 
1259f6aba7b5SAlex Elder static bool ipa_endpoint_status_tag(struct ipa_endpoint *endpoint,
1260f6aba7b5SAlex Elder 				    const struct ipa_status *status)
1261f6aba7b5SAlex Elder {
126251c48ce2SAlex Elder 	struct ipa_endpoint *command_endpoint;
126351c48ce2SAlex Elder 	struct ipa *ipa = endpoint->ipa;
126451c48ce2SAlex Elder 	u32 endpoint_id;
126551c48ce2SAlex Elder 
126651c48ce2SAlex Elder 	if (!le16_get_bits(status->mask, IPA_STATUS_MASK_TAG_VALID_FMASK))
126751c48ce2SAlex Elder 		return false;	/* No valid tag */
126851c48ce2SAlex Elder 
126951c48ce2SAlex Elder 	/* The status contains a valid tag.  We know the packet was sent to
127051c48ce2SAlex Elder 	 * this endpoint (already verified by ipa_endpoint_status_skip()).
127151c48ce2SAlex Elder 	 * If the packet came from the AP->command TX endpoint we know
127251c48ce2SAlex Elder 	 * this packet was sent as part of the pipeline clear process.
127351c48ce2SAlex Elder 	 */
127451c48ce2SAlex Elder 	endpoint_id = u8_get_bits(status->endp_src_idx,
127551c48ce2SAlex Elder 				  IPA_STATUS_SRC_IDX_FMASK);
127651c48ce2SAlex Elder 	command_endpoint = ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX];
127751c48ce2SAlex Elder 	if (endpoint_id == command_endpoint->endpoint_id) {
127851c48ce2SAlex Elder 		complete(&ipa->completion);
127951c48ce2SAlex Elder 	} else {
128051c48ce2SAlex Elder 		dev_err(&ipa->pdev->dev,
128151c48ce2SAlex Elder 			"unexpected tagged packet from endpoint %u\n",
128251c48ce2SAlex Elder 			endpoint_id);
128351c48ce2SAlex Elder 	}
128451c48ce2SAlex Elder 
128551c48ce2SAlex Elder 	return true;
1286f6aba7b5SAlex Elder }
1287f6aba7b5SAlex Elder 
128884f9bd12SAlex Elder /* Return whether the status indicates the packet should be dropped */
1289f6aba7b5SAlex Elder static bool ipa_endpoint_status_drop(struct ipa_endpoint *endpoint,
1290f6aba7b5SAlex Elder 				     const struct ipa_status *status)
129184f9bd12SAlex Elder {
129284f9bd12SAlex Elder 	u32 val;
129384f9bd12SAlex Elder 
1294f6aba7b5SAlex Elder 	/* If the status indicates a tagged transfer, we'll drop the packet */
1295f6aba7b5SAlex Elder 	if (ipa_endpoint_status_tag(endpoint, status))
1296f6aba7b5SAlex Elder 		return true;
1297f6aba7b5SAlex Elder 
1298ab4f71e5SAlex Elder 	/* Deaggregation exceptions we drop; all other types we consume */
129984f9bd12SAlex Elder 	if (status->exception)
130084f9bd12SAlex Elder 		return status->exception == IPA_STATUS_EXCEPTION_DEAGGR;
130184f9bd12SAlex Elder 
130284f9bd12SAlex Elder 	/* Drop the packet if it fails to match a routing rule; otherwise no */
130384f9bd12SAlex Elder 	val = le32_get_bits(status->flags1, IPA_STATUS_FLAGS1_RT_RULE_ID_FMASK);
130484f9bd12SAlex Elder 
130584f9bd12SAlex Elder 	return val == field_max(IPA_STATUS_FLAGS1_RT_RULE_ID_FMASK);
130684f9bd12SAlex Elder }
130784f9bd12SAlex Elder 
130884f9bd12SAlex Elder static void ipa_endpoint_status_parse(struct ipa_endpoint *endpoint,
130984f9bd12SAlex Elder 				      struct page *page, u32 total_len)
131084f9bd12SAlex Elder {
1311660e52d6SAlex Elder 	u32 buffer_size = endpoint->config.rx.buffer_size;
131284f9bd12SAlex Elder 	void *data = page_address(page) + NET_SKB_PAD;
1313ed23f026SAlex Elder 	u32 unused = buffer_size - total_len;
131484f9bd12SAlex Elder 	u32 resid = total_len;
131584f9bd12SAlex Elder 
131684f9bd12SAlex Elder 	while (resid) {
131784f9bd12SAlex Elder 		const struct ipa_status *status = data;
131884f9bd12SAlex Elder 		u32 align;
131984f9bd12SAlex Elder 		u32 len;
132084f9bd12SAlex Elder 
132184f9bd12SAlex Elder 		if (resid < sizeof(*status)) {
132284f9bd12SAlex Elder 			dev_err(&endpoint->ipa->pdev->dev,
132384f9bd12SAlex Elder 				"short message (%u bytes < %zu byte status)\n",
132484f9bd12SAlex Elder 				resid, sizeof(*status));
132584f9bd12SAlex Elder 			break;
132684f9bd12SAlex Elder 		}
132784f9bd12SAlex Elder 
132884f9bd12SAlex Elder 		/* Skip over status packets that lack packet data */
132984f9bd12SAlex Elder 		if (ipa_endpoint_status_skip(endpoint, status)) {
133084f9bd12SAlex Elder 			data += sizeof(*status);
133184f9bd12SAlex Elder 			resid -= sizeof(*status);
133284f9bd12SAlex Elder 			continue;
133384f9bd12SAlex Elder 		}
133484f9bd12SAlex Elder 
1335162fbc6fSAlex Elder 		/* Compute the amount of buffer space consumed by the packet,
1336162fbc6fSAlex Elder 		 * including the status element.  If the hardware is configured
1337162fbc6fSAlex Elder 		 * to pad packet data to an aligned boundary, account for that.
1338162fbc6fSAlex Elder 		 * And if checksum offload is enabled a trailer containing
1339162fbc6fSAlex Elder 		 * computed checksum information will be appended.
134084f9bd12SAlex Elder 		 */
1341660e52d6SAlex Elder 		align = endpoint->config.rx.pad_align ? : 1;
134284f9bd12SAlex Elder 		len = le16_to_cpu(status->pkt_len);
134384f9bd12SAlex Elder 		len = sizeof(*status) + ALIGN(len, align);
1344660e52d6SAlex Elder 		if (endpoint->config.checksum)
134584f9bd12SAlex Elder 			len += sizeof(struct rmnet_map_dl_csum_trailer);
134684f9bd12SAlex Elder 
1347f6aba7b5SAlex Elder 		if (!ipa_endpoint_status_drop(endpoint, status)) {
1348162fbc6fSAlex Elder 			void *data2;
1349162fbc6fSAlex Elder 			u32 extra;
1350162fbc6fSAlex Elder 			u32 len2;
135184f9bd12SAlex Elder 
135284f9bd12SAlex Elder 			/* Client receives only packet data (no status) */
1353162fbc6fSAlex Elder 			data2 = data + sizeof(*status);
1354162fbc6fSAlex Elder 			len2 = le16_to_cpu(status->pkt_len);
1355162fbc6fSAlex Elder 
1356162fbc6fSAlex Elder 			/* Have the true size reflect the extra unused space in
1357162fbc6fSAlex Elder 			 * the original receive buffer.  Distribute the "cost"
1358162fbc6fSAlex Elder 			 * proportionately across all aggregated packets in the
1359162fbc6fSAlex Elder 			 * buffer.
1360162fbc6fSAlex Elder 			 */
1361162fbc6fSAlex Elder 			extra = DIV_ROUND_CLOSEST(unused * len, total_len);
136284f9bd12SAlex Elder 			ipa_endpoint_skb_copy(endpoint, data2, len2, extra);
136384f9bd12SAlex Elder 		}
136484f9bd12SAlex Elder 
136584f9bd12SAlex Elder 		/* Consume status and the full packet it describes */
136684f9bd12SAlex Elder 		data += len;
136784f9bd12SAlex Elder 		resid -= len;
136884f9bd12SAlex Elder 	}
136984f9bd12SAlex Elder }
137084f9bd12SAlex Elder 
137184f9bd12SAlex Elder /* Complete a TX transaction, command or from ipa_endpoint_skb_tx() */
137284f9bd12SAlex Elder static void ipa_endpoint_tx_complete(struct ipa_endpoint *endpoint,
137384f9bd12SAlex Elder 				     struct gsi_trans *trans)
137484f9bd12SAlex Elder {
137584f9bd12SAlex Elder }
137684f9bd12SAlex Elder 
137784f9bd12SAlex Elder /* Complete transaction initiated in ipa_endpoint_replenish_one() */
137884f9bd12SAlex Elder static void ipa_endpoint_rx_complete(struct ipa_endpoint *endpoint,
137984f9bd12SAlex Elder 				     struct gsi_trans *trans)
138084f9bd12SAlex Elder {
138184f9bd12SAlex Elder 	struct page *page;
138284f9bd12SAlex Elder 
138384f9bd12SAlex Elder 	if (trans->cancelled)
13845d6ac24fSAlex Elder 		goto done;
138584f9bd12SAlex Elder 
138684f9bd12SAlex Elder 	/* Parse or build a socket buffer using the actual received length */
138784f9bd12SAlex Elder 	page = trans->data;
1388660e52d6SAlex Elder 	if (endpoint->config.status_enable)
138984f9bd12SAlex Elder 		ipa_endpoint_status_parse(endpoint, page, trans->len);
139084f9bd12SAlex Elder 	else if (ipa_endpoint_skb_build(endpoint, page, trans->len))
139184f9bd12SAlex Elder 		trans->data = NULL;	/* Pages have been consumed */
13925d6ac24fSAlex Elder done:
13935d6ac24fSAlex Elder 	ipa_endpoint_replenish(endpoint);
139484f9bd12SAlex Elder }
139584f9bd12SAlex Elder 
139684f9bd12SAlex Elder void ipa_endpoint_trans_complete(struct ipa_endpoint *endpoint,
139784f9bd12SAlex Elder 				 struct gsi_trans *trans)
139884f9bd12SAlex Elder {
139984f9bd12SAlex Elder 	if (endpoint->toward_ipa)
140084f9bd12SAlex Elder 		ipa_endpoint_tx_complete(endpoint, trans);
140184f9bd12SAlex Elder 	else
140284f9bd12SAlex Elder 		ipa_endpoint_rx_complete(endpoint, trans);
140384f9bd12SAlex Elder }
140484f9bd12SAlex Elder 
140584f9bd12SAlex Elder void ipa_endpoint_trans_release(struct ipa_endpoint *endpoint,
140684f9bd12SAlex Elder 				struct gsi_trans *trans)
140784f9bd12SAlex Elder {
140884f9bd12SAlex Elder 	if (endpoint->toward_ipa) {
140984f9bd12SAlex Elder 		struct ipa *ipa = endpoint->ipa;
141084f9bd12SAlex Elder 
141184f9bd12SAlex Elder 		/* Nothing to do for command transactions */
141284f9bd12SAlex Elder 		if (endpoint != ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]) {
141384f9bd12SAlex Elder 			struct sk_buff *skb = trans->data;
141484f9bd12SAlex Elder 
141584f9bd12SAlex Elder 			if (skb)
141684f9bd12SAlex Elder 				dev_kfree_skb_any(skb);
141784f9bd12SAlex Elder 		}
141884f9bd12SAlex Elder 	} else {
141984f9bd12SAlex Elder 		struct page *page = trans->data;
142084f9bd12SAlex Elder 
1421155c0c90SAlex Elder 		if (page)
1422155c0c90SAlex Elder 			put_page(page);
142384f9bd12SAlex Elder 	}
142484f9bd12SAlex Elder }
142584f9bd12SAlex Elder 
142684f9bd12SAlex Elder void ipa_endpoint_default_route_set(struct ipa *ipa, u32 endpoint_id)
142784f9bd12SAlex Elder {
142884f9bd12SAlex Elder 	u32 val;
142984f9bd12SAlex Elder 
143084f9bd12SAlex Elder 	/* ROUTE_DIS is 0 */
143184f9bd12SAlex Elder 	val = u32_encode_bits(endpoint_id, ROUTE_DEF_PIPE_FMASK);
143284f9bd12SAlex Elder 	val |= ROUTE_DEF_HDR_TABLE_FMASK;
143384f9bd12SAlex Elder 	val |= u32_encode_bits(0, ROUTE_DEF_HDR_OFST_FMASK);
143484f9bd12SAlex Elder 	val |= u32_encode_bits(endpoint_id, ROUTE_FRAG_DEF_PIPE_FMASK);
143584f9bd12SAlex Elder 	val |= ROUTE_DEF_RETAIN_HDR_FMASK;
143684f9bd12SAlex Elder 
143784f9bd12SAlex Elder 	iowrite32(val, ipa->reg_virt + IPA_REG_ROUTE_OFFSET);
143884f9bd12SAlex Elder }
143984f9bd12SAlex Elder 
144084f9bd12SAlex Elder void ipa_endpoint_default_route_clear(struct ipa *ipa)
144184f9bd12SAlex Elder {
144284f9bd12SAlex Elder 	ipa_endpoint_default_route_set(ipa, 0);
144384f9bd12SAlex Elder }
144484f9bd12SAlex Elder 
144584f9bd12SAlex Elder /**
144684f9bd12SAlex Elder  * ipa_endpoint_reset_rx_aggr() - Reset RX endpoint with aggregation active
144784f9bd12SAlex Elder  * @endpoint:	Endpoint to be reset
144884f9bd12SAlex Elder  *
144984f9bd12SAlex Elder  * If aggregation is active on an RX endpoint when a reset is performed
145084f9bd12SAlex Elder  * on its underlying GSI channel, a special sequence of actions must be
145184f9bd12SAlex Elder  * taken to ensure the IPA pipeline is properly cleared.
145284f9bd12SAlex Elder  *
1453e3eea08eSAlex Elder  * Return:	0 if successful, or a negative error code
145484f9bd12SAlex Elder  */
145584f9bd12SAlex Elder static int ipa_endpoint_reset_rx_aggr(struct ipa_endpoint *endpoint)
145684f9bd12SAlex Elder {
145784f9bd12SAlex Elder 	struct device *dev = &endpoint->ipa->pdev->dev;
145884f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
145984f9bd12SAlex Elder 	struct gsi *gsi = &ipa->gsi;
14604fa95248SAlex Elder 	bool suspended = false;
146184f9bd12SAlex Elder 	dma_addr_t addr;
146284f9bd12SAlex Elder 	u32 retries;
146384f9bd12SAlex Elder 	u32 len = 1;
146484f9bd12SAlex Elder 	void *virt;
146584f9bd12SAlex Elder 	int ret;
146684f9bd12SAlex Elder 
146784f9bd12SAlex Elder 	virt = kzalloc(len, GFP_KERNEL);
146884f9bd12SAlex Elder 	if (!virt)
146984f9bd12SAlex Elder 		return -ENOMEM;
147084f9bd12SAlex Elder 
147184f9bd12SAlex Elder 	addr = dma_map_single(dev, virt, len, DMA_FROM_DEVICE);
147284f9bd12SAlex Elder 	if (dma_mapping_error(dev, addr)) {
147384f9bd12SAlex Elder 		ret = -ENOMEM;
147484f9bd12SAlex Elder 		goto out_kfree;
147584f9bd12SAlex Elder 	}
147684f9bd12SAlex Elder 
147784f9bd12SAlex Elder 	/* Force close aggregation before issuing the reset */
147884f9bd12SAlex Elder 	ipa_endpoint_force_close(endpoint);
147984f9bd12SAlex Elder 
148084f9bd12SAlex Elder 	/* Reset and reconfigure the channel with the doorbell engine
148184f9bd12SAlex Elder 	 * disabled.  Then poll until we know aggregation is no longer
148284f9bd12SAlex Elder 	 * active.  We'll re-enable the doorbell (if appropriate) when
148384f9bd12SAlex Elder 	 * we reset again below.
148484f9bd12SAlex Elder 	 */
148584f9bd12SAlex Elder 	gsi_channel_reset(gsi, endpoint->channel_id, false);
148684f9bd12SAlex Elder 
148784f9bd12SAlex Elder 	/* Make sure the channel isn't suspended */
14884fa95248SAlex Elder 	suspended = ipa_endpoint_program_suspend(endpoint, false);
148984f9bd12SAlex Elder 
149084f9bd12SAlex Elder 	/* Start channel and do a 1 byte read */
149184f9bd12SAlex Elder 	ret = gsi_channel_start(gsi, endpoint->channel_id);
149284f9bd12SAlex Elder 	if (ret)
149384f9bd12SAlex Elder 		goto out_suspend_again;
149484f9bd12SAlex Elder 
149584f9bd12SAlex Elder 	ret = gsi_trans_read_byte(gsi, endpoint->channel_id, addr);
149684f9bd12SAlex Elder 	if (ret)
149784f9bd12SAlex Elder 		goto err_endpoint_stop;
149884f9bd12SAlex Elder 
149984f9bd12SAlex Elder 	/* Wait for aggregation to be closed on the channel */
150084f9bd12SAlex Elder 	retries = IPA_ENDPOINT_RESET_AGGR_RETRY_MAX;
150184f9bd12SAlex Elder 	do {
150284f9bd12SAlex Elder 		if (!ipa_endpoint_aggr_active(endpoint))
150384f9bd12SAlex Elder 			break;
150474401946SAlex Elder 		usleep_range(USEC_PER_MSEC, 2 * USEC_PER_MSEC);
150584f9bd12SAlex Elder 	} while (retries--);
150684f9bd12SAlex Elder 
150784f9bd12SAlex Elder 	/* Check one last time */
150884f9bd12SAlex Elder 	if (ipa_endpoint_aggr_active(endpoint))
150984f9bd12SAlex Elder 		dev_err(dev, "endpoint %u still active during reset\n",
151084f9bd12SAlex Elder 			endpoint->endpoint_id);
151184f9bd12SAlex Elder 
151284f9bd12SAlex Elder 	gsi_trans_read_byte_done(gsi, endpoint->channel_id);
151384f9bd12SAlex Elder 
1514f30dcb7dSAlex Elder 	ret = gsi_channel_stop(gsi, endpoint->channel_id);
151584f9bd12SAlex Elder 	if (ret)
151684f9bd12SAlex Elder 		goto out_suspend_again;
151784f9bd12SAlex Elder 
1518497abc87SPeng Li 	/* Finally, reset and reconfigure the channel again (re-enabling
151984f9bd12SAlex Elder 	 * the doorbell engine if appropriate).  Sleep for 1 millisecond to
152084f9bd12SAlex Elder 	 * complete the channel reset sequence.  Finish by suspending the
152184f9bd12SAlex Elder 	 * channel again (if necessary).
152284f9bd12SAlex Elder 	 */
1523ce54993dSAlex Elder 	gsi_channel_reset(gsi, endpoint->channel_id, true);
152484f9bd12SAlex Elder 
152574401946SAlex Elder 	usleep_range(USEC_PER_MSEC, 2 * USEC_PER_MSEC);
152684f9bd12SAlex Elder 
152784f9bd12SAlex Elder 	goto out_suspend_again;
152884f9bd12SAlex Elder 
152984f9bd12SAlex Elder err_endpoint_stop:
1530f30dcb7dSAlex Elder 	(void)gsi_channel_stop(gsi, endpoint->channel_id);
153184f9bd12SAlex Elder out_suspend_again:
15324fa95248SAlex Elder 	if (suspended)
15334fa95248SAlex Elder 		(void)ipa_endpoint_program_suspend(endpoint, true);
153484f9bd12SAlex Elder 	dma_unmap_single(dev, addr, len, DMA_FROM_DEVICE);
153584f9bd12SAlex Elder out_kfree:
153684f9bd12SAlex Elder 	kfree(virt);
153784f9bd12SAlex Elder 
153884f9bd12SAlex Elder 	return ret;
153984f9bd12SAlex Elder }
154084f9bd12SAlex Elder 
154184f9bd12SAlex Elder static void ipa_endpoint_reset(struct ipa_endpoint *endpoint)
154284f9bd12SAlex Elder {
154384f9bd12SAlex Elder 	u32 channel_id = endpoint->channel_id;
154484f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
154584f9bd12SAlex Elder 	bool special;
154684f9bd12SAlex Elder 	int ret = 0;
154784f9bd12SAlex Elder 
154884f9bd12SAlex Elder 	/* On IPA v3.5.1, if an RX endpoint is reset while aggregation
154984f9bd12SAlex Elder 	 * is active, we need to handle things specially to recover.
155084f9bd12SAlex Elder 	 * All other cases just need to reset the underlying GSI channel.
155184f9bd12SAlex Elder 	 */
1552d7f3087bSAlex Elder 	special = ipa->version < IPA_VERSION_4_0 && !endpoint->toward_ipa &&
1553660e52d6SAlex Elder 			endpoint->config.aggregation;
1554ce54993dSAlex Elder 	if (special && ipa_endpoint_aggr_active(endpoint))
155584f9bd12SAlex Elder 		ret = ipa_endpoint_reset_rx_aggr(endpoint);
155684f9bd12SAlex Elder 	else
1557ce54993dSAlex Elder 		gsi_channel_reset(&ipa->gsi, channel_id, true);
155884f9bd12SAlex Elder 
155984f9bd12SAlex Elder 	if (ret)
156084f9bd12SAlex Elder 		dev_err(&ipa->pdev->dev,
156184f9bd12SAlex Elder 			"error %d resetting channel %u for endpoint %u\n",
156284f9bd12SAlex Elder 			ret, endpoint->channel_id, endpoint->endpoint_id);
156384f9bd12SAlex Elder }
156484f9bd12SAlex Elder 
156584f9bd12SAlex Elder static void ipa_endpoint_program(struct ipa_endpoint *endpoint)
156684f9bd12SAlex Elder {
15674c9d631aSAlex Elder 	if (endpoint->toward_ipa) {
15684c9d631aSAlex Elder 		/* Newer versions of IPA use GSI channel flow control
15694c9d631aSAlex Elder 		 * instead of endpoint DELAY mode to prevent sending data.
15704c9d631aSAlex Elder 		 * Flow control is disabled for newly-allocated channels,
15714c9d631aSAlex Elder 		 * and we can assume flow control is not (ever) enabled
15724c9d631aSAlex Elder 		 * for AP TX channels.
15734c9d631aSAlex Elder 		 */
15744c9d631aSAlex Elder 		if (endpoint->ipa->version < IPA_VERSION_4_2)
1575a4dcad34SAlex Elder 			ipa_endpoint_program_delay(endpoint, false);
15764c9d631aSAlex Elder 	} else {
15774c9d631aSAlex Elder 		/* Ensure suspend mode is off on all AP RX endpoints */
1578fb57c3eaSAlex Elder 		(void)ipa_endpoint_program_suspend(endpoint, false);
15794c9d631aSAlex Elder 	}
1580fb57c3eaSAlex Elder 	ipa_endpoint_init_cfg(endpoint);
1581647a05f3SAlex Elder 	ipa_endpoint_init_nat(endpoint);
1582fb57c3eaSAlex Elder 	ipa_endpoint_init_hdr(endpoint);
158384f9bd12SAlex Elder 	ipa_endpoint_init_hdr_ext(endpoint);
1584fb57c3eaSAlex Elder 	ipa_endpoint_init_hdr_metadata_mask(endpoint);
1585fb57c3eaSAlex Elder 	ipa_endpoint_init_mode(endpoint);
158684f9bd12SAlex Elder 	ipa_endpoint_init_aggr(endpoint);
1587153213f0SAlex Elder 	if (!endpoint->toward_ipa) {
1588153213f0SAlex Elder 		if (endpoint->config.rx.holb_drop)
1589153213f0SAlex Elder 			ipa_endpoint_init_hol_block_enable(endpoint, 0);
1590153213f0SAlex Elder 		else
159101c36637SAlex Elder 			ipa_endpoint_init_hol_block_disable(endpoint);
1592153213f0SAlex Elder 	}
159384f9bd12SAlex Elder 	ipa_endpoint_init_deaggr(endpoint);
15942d265342SAlex Elder 	ipa_endpoint_init_rsrc_grp(endpoint);
159584f9bd12SAlex Elder 	ipa_endpoint_init_seq(endpoint);
159684f9bd12SAlex Elder 	ipa_endpoint_status(endpoint);
159784f9bd12SAlex Elder }
159884f9bd12SAlex Elder 
159984f9bd12SAlex Elder int ipa_endpoint_enable_one(struct ipa_endpoint *endpoint)
160084f9bd12SAlex Elder {
160184f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
160284f9bd12SAlex Elder 	struct gsi *gsi = &ipa->gsi;
160384f9bd12SAlex Elder 	int ret;
160484f9bd12SAlex Elder 
160584f9bd12SAlex Elder 	ret = gsi_channel_start(gsi, endpoint->channel_id);
160684f9bd12SAlex Elder 	if (ret) {
160784f9bd12SAlex Elder 		dev_err(&ipa->pdev->dev,
160884f9bd12SAlex Elder 			"error %d starting %cX channel %u for endpoint %u\n",
160984f9bd12SAlex Elder 			ret, endpoint->toward_ipa ? 'T' : 'R',
161084f9bd12SAlex Elder 			endpoint->channel_id, endpoint->endpoint_id);
161184f9bd12SAlex Elder 		return ret;
161284f9bd12SAlex Elder 	}
161384f9bd12SAlex Elder 
161484f9bd12SAlex Elder 	if (!endpoint->toward_ipa) {
161584f9bd12SAlex Elder 		ipa_interrupt_suspend_enable(ipa->interrupt,
161684f9bd12SAlex Elder 					     endpoint->endpoint_id);
161784f9bd12SAlex Elder 		ipa_endpoint_replenish_enable(endpoint);
161884f9bd12SAlex Elder 	}
161984f9bd12SAlex Elder 
162084f9bd12SAlex Elder 	ipa->enabled |= BIT(endpoint->endpoint_id);
162184f9bd12SAlex Elder 
162284f9bd12SAlex Elder 	return 0;
162384f9bd12SAlex Elder }
162484f9bd12SAlex Elder 
162584f9bd12SAlex Elder void ipa_endpoint_disable_one(struct ipa_endpoint *endpoint)
162684f9bd12SAlex Elder {
162784f9bd12SAlex Elder 	u32 mask = BIT(endpoint->endpoint_id);
162884f9bd12SAlex Elder 	struct ipa *ipa = endpoint->ipa;
1629f30dcb7dSAlex Elder 	struct gsi *gsi = &ipa->gsi;
163084f9bd12SAlex Elder 	int ret;
163184f9bd12SAlex Elder 
1632f30dcb7dSAlex Elder 	if (!(ipa->enabled & mask))
163384f9bd12SAlex Elder 		return;
163484f9bd12SAlex Elder 
1635f30dcb7dSAlex Elder 	ipa->enabled ^= mask;
163684f9bd12SAlex Elder 
163784f9bd12SAlex Elder 	if (!endpoint->toward_ipa) {
163884f9bd12SAlex Elder 		ipa_endpoint_replenish_disable(endpoint);
163984f9bd12SAlex Elder 		ipa_interrupt_suspend_disable(ipa->interrupt,
164084f9bd12SAlex Elder 					      endpoint->endpoint_id);
164184f9bd12SAlex Elder 	}
164284f9bd12SAlex Elder 
164384f9bd12SAlex Elder 	/* Note that if stop fails, the channel's state is not well-defined */
1644f30dcb7dSAlex Elder 	ret = gsi_channel_stop(gsi, endpoint->channel_id);
164584f9bd12SAlex Elder 	if (ret)
164684f9bd12SAlex Elder 		dev_err(&ipa->pdev->dev,
164784f9bd12SAlex Elder 			"error %d attempting to stop endpoint %u\n", ret,
164884f9bd12SAlex Elder 			endpoint->endpoint_id);
164984f9bd12SAlex Elder }
165084f9bd12SAlex Elder 
165184f9bd12SAlex Elder void ipa_endpoint_suspend_one(struct ipa_endpoint *endpoint)
165284f9bd12SAlex Elder {
165384f9bd12SAlex Elder 	struct device *dev = &endpoint->ipa->pdev->dev;
165484f9bd12SAlex Elder 	struct gsi *gsi = &endpoint->ipa->gsi;
165584f9bd12SAlex Elder 	int ret;
165684f9bd12SAlex Elder 
165784f9bd12SAlex Elder 	if (!(endpoint->ipa->enabled & BIT(endpoint->endpoint_id)))
165884f9bd12SAlex Elder 		return;
165984f9bd12SAlex Elder 
1660ab4f71e5SAlex Elder 	if (!endpoint->toward_ipa) {
166184f9bd12SAlex Elder 		ipa_endpoint_replenish_disable(endpoint);
16624fa95248SAlex Elder 		(void)ipa_endpoint_program_suspend(endpoint, true);
1663ab4f71e5SAlex Elder 	}
166484f9bd12SAlex Elder 
1665decfef0fSAlex Elder 	ret = gsi_channel_suspend(gsi, endpoint->channel_id);
166684f9bd12SAlex Elder 	if (ret)
166784f9bd12SAlex Elder 		dev_err(dev, "error %d suspending channel %u\n", ret,
166884f9bd12SAlex Elder 			endpoint->channel_id);
166984f9bd12SAlex Elder }
167084f9bd12SAlex Elder 
167184f9bd12SAlex Elder void ipa_endpoint_resume_one(struct ipa_endpoint *endpoint)
167284f9bd12SAlex Elder {
167384f9bd12SAlex Elder 	struct device *dev = &endpoint->ipa->pdev->dev;
167484f9bd12SAlex Elder 	struct gsi *gsi = &endpoint->ipa->gsi;
167584f9bd12SAlex Elder 	int ret;
167684f9bd12SAlex Elder 
167784f9bd12SAlex Elder 	if (!(endpoint->ipa->enabled & BIT(endpoint->endpoint_id)))
167884f9bd12SAlex Elder 		return;
167984f9bd12SAlex Elder 
1680b07f283eSAlex Elder 	if (!endpoint->toward_ipa)
16814fa95248SAlex Elder 		(void)ipa_endpoint_program_suspend(endpoint, false);
168284f9bd12SAlex Elder 
1683decfef0fSAlex Elder 	ret = gsi_channel_resume(gsi, endpoint->channel_id);
168484f9bd12SAlex Elder 	if (ret)
168584f9bd12SAlex Elder 		dev_err(dev, "error %d resuming channel %u\n", ret,
168684f9bd12SAlex Elder 			endpoint->channel_id);
168784f9bd12SAlex Elder 	else if (!endpoint->toward_ipa)
168884f9bd12SAlex Elder 		ipa_endpoint_replenish_enable(endpoint);
168984f9bd12SAlex Elder }
169084f9bd12SAlex Elder 
169184f9bd12SAlex Elder void ipa_endpoint_suspend(struct ipa *ipa)
169284f9bd12SAlex Elder {
1693d1704382SAlex Elder 	if (!ipa->setup_complete)
1694d1704382SAlex Elder 		return;
1695d1704382SAlex Elder 
169684f9bd12SAlex Elder 	if (ipa->modem_netdev)
169784f9bd12SAlex Elder 		ipa_modem_suspend(ipa->modem_netdev);
169884f9bd12SAlex Elder 
169984f9bd12SAlex Elder 	ipa_endpoint_suspend_one(ipa->name_map[IPA_ENDPOINT_AP_LAN_RX]);
170084f9bd12SAlex Elder 	ipa_endpoint_suspend_one(ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]);
170184f9bd12SAlex Elder }
170284f9bd12SAlex Elder 
170384f9bd12SAlex Elder void ipa_endpoint_resume(struct ipa *ipa)
170484f9bd12SAlex Elder {
1705d1704382SAlex Elder 	if (!ipa->setup_complete)
1706d1704382SAlex Elder 		return;
1707d1704382SAlex Elder 
170884f9bd12SAlex Elder 	ipa_endpoint_resume_one(ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]);
170984f9bd12SAlex Elder 	ipa_endpoint_resume_one(ipa->name_map[IPA_ENDPOINT_AP_LAN_RX]);
171084f9bd12SAlex Elder 
171184f9bd12SAlex Elder 	if (ipa->modem_netdev)
171284f9bd12SAlex Elder 		ipa_modem_resume(ipa->modem_netdev);
171384f9bd12SAlex Elder }
171484f9bd12SAlex Elder 
171584f9bd12SAlex Elder static void ipa_endpoint_setup_one(struct ipa_endpoint *endpoint)
171684f9bd12SAlex Elder {
171784f9bd12SAlex Elder 	struct gsi *gsi = &endpoint->ipa->gsi;
171884f9bd12SAlex Elder 	u32 channel_id = endpoint->channel_id;
171984f9bd12SAlex Elder 
172084f9bd12SAlex Elder 	/* Only AP endpoints get set up */
172184f9bd12SAlex Elder 	if (endpoint->ee_id != GSI_EE_AP)
172284f9bd12SAlex Elder 		return;
172384f9bd12SAlex Elder 
172484f9bd12SAlex Elder 	endpoint->trans_tre_max = gsi_channel_trans_tre_max(gsi, channel_id);
172584f9bd12SAlex Elder 	if (!endpoint->toward_ipa) {
172684f9bd12SAlex Elder 		/* RX transactions require a single TRE, so the maximum
172784f9bd12SAlex Elder 		 * backlog is the same as the maximum outstanding TREs.
172884f9bd12SAlex Elder 		 */
1729c1aaa01dSAlex Elder 		clear_bit(IPA_REPLENISH_ENABLED, endpoint->replenish_flags);
1730998c0bd2SAlex Elder 		clear_bit(IPA_REPLENISH_ACTIVE, endpoint->replenish_flags);
173184f9bd12SAlex Elder 		INIT_DELAYED_WORK(&endpoint->replenish_work,
173284f9bd12SAlex Elder 				  ipa_endpoint_replenish_work);
173384f9bd12SAlex Elder 	}
173484f9bd12SAlex Elder 
173584f9bd12SAlex Elder 	ipa_endpoint_program(endpoint);
173684f9bd12SAlex Elder 
173784f9bd12SAlex Elder 	endpoint->ipa->set_up |= BIT(endpoint->endpoint_id);
173884f9bd12SAlex Elder }
173984f9bd12SAlex Elder 
174084f9bd12SAlex Elder static void ipa_endpoint_teardown_one(struct ipa_endpoint *endpoint)
174184f9bd12SAlex Elder {
174284f9bd12SAlex Elder 	endpoint->ipa->set_up &= ~BIT(endpoint->endpoint_id);
174384f9bd12SAlex Elder 
174484f9bd12SAlex Elder 	if (!endpoint->toward_ipa)
174584f9bd12SAlex Elder 		cancel_delayed_work_sync(&endpoint->replenish_work);
174684f9bd12SAlex Elder 
174784f9bd12SAlex Elder 	ipa_endpoint_reset(endpoint);
174884f9bd12SAlex Elder }
174984f9bd12SAlex Elder 
175084f9bd12SAlex Elder void ipa_endpoint_setup(struct ipa *ipa)
175184f9bd12SAlex Elder {
175284f9bd12SAlex Elder 	u32 initialized = ipa->initialized;
175384f9bd12SAlex Elder 
175484f9bd12SAlex Elder 	ipa->set_up = 0;
175584f9bd12SAlex Elder 	while (initialized) {
175684f9bd12SAlex Elder 		u32 endpoint_id = __ffs(initialized);
175784f9bd12SAlex Elder 
175884f9bd12SAlex Elder 		initialized ^= BIT(endpoint_id);
175984f9bd12SAlex Elder 
176084f9bd12SAlex Elder 		ipa_endpoint_setup_one(&ipa->endpoint[endpoint_id]);
176184f9bd12SAlex Elder 	}
176284f9bd12SAlex Elder }
176384f9bd12SAlex Elder 
176484f9bd12SAlex Elder void ipa_endpoint_teardown(struct ipa *ipa)
176584f9bd12SAlex Elder {
176684f9bd12SAlex Elder 	u32 set_up = ipa->set_up;
176784f9bd12SAlex Elder 
176884f9bd12SAlex Elder 	while (set_up) {
176984f9bd12SAlex Elder 		u32 endpoint_id = __fls(set_up);
177084f9bd12SAlex Elder 
177184f9bd12SAlex Elder 		set_up ^= BIT(endpoint_id);
177284f9bd12SAlex Elder 
177384f9bd12SAlex Elder 		ipa_endpoint_teardown_one(&ipa->endpoint[endpoint_id]);
177484f9bd12SAlex Elder 	}
177584f9bd12SAlex Elder 	ipa->set_up = 0;
177684f9bd12SAlex Elder }
177784f9bd12SAlex Elder 
177884f9bd12SAlex Elder int ipa_endpoint_config(struct ipa *ipa)
177984f9bd12SAlex Elder {
178084f9bd12SAlex Elder 	struct device *dev = &ipa->pdev->dev;
178184f9bd12SAlex Elder 	u32 initialized;
178284f9bd12SAlex Elder 	u32 rx_base;
178384f9bd12SAlex Elder 	u32 rx_mask;
178484f9bd12SAlex Elder 	u32 tx_mask;
178584f9bd12SAlex Elder 	int ret = 0;
178684f9bd12SAlex Elder 	u32 max;
178784f9bd12SAlex Elder 	u32 val;
178884f9bd12SAlex Elder 
1789110971d1SAlex Elder 	/* Prior to IPAv3.5, the FLAVOR_0 register was not supported.
1790110971d1SAlex Elder 	 * Furthermore, the endpoints were not grouped such that TX
1791110971d1SAlex Elder 	 * endpoint numbers started with 0 and RX endpoints had numbers
1792110971d1SAlex Elder 	 * higher than all TX endpoints, so we can't do the simple
1793110971d1SAlex Elder 	 * direction check used for newer hardware below.
1794110971d1SAlex Elder 	 *
1795110971d1SAlex Elder 	 * For hardware that doesn't support the FLAVOR_0 register,
1796110971d1SAlex Elder 	 * just set the available mask to support any endpoint, and
1797110971d1SAlex Elder 	 * assume the configuration is valid.
1798110971d1SAlex Elder 	 */
1799110971d1SAlex Elder 	if (ipa->version < IPA_VERSION_3_5) {
1800110971d1SAlex Elder 		ipa->available = ~0;
1801110971d1SAlex Elder 		return 0;
1802110971d1SAlex Elder 	}
1803110971d1SAlex Elder 
180484f9bd12SAlex Elder 	/* Find out about the endpoints supplied by the hardware, and ensure
180584f9bd12SAlex Elder 	 * the highest one doesn't exceed the number we support.
180684f9bd12SAlex Elder 	 */
180784f9bd12SAlex Elder 	val = ioread32(ipa->reg_virt + IPA_REG_FLAVOR_0_OFFSET);
180884f9bd12SAlex Elder 
180984f9bd12SAlex Elder 	/* Our RX is an IPA producer */
1810716a115bSAlex Elder 	rx_base = u32_get_bits(val, IPA_PROD_LOWEST_FMASK);
1811716a115bSAlex Elder 	max = rx_base + u32_get_bits(val, IPA_MAX_PROD_PIPES_FMASK);
181284f9bd12SAlex Elder 	if (max > IPA_ENDPOINT_MAX) {
181384f9bd12SAlex Elder 		dev_err(dev, "too many endpoints (%u > %u)\n",
181484f9bd12SAlex Elder 			max, IPA_ENDPOINT_MAX);
181584f9bd12SAlex Elder 		return -EINVAL;
181684f9bd12SAlex Elder 	}
181784f9bd12SAlex Elder 	rx_mask = GENMASK(max - 1, rx_base);
181884f9bd12SAlex Elder 
181984f9bd12SAlex Elder 	/* Our TX is an IPA consumer */
1820716a115bSAlex Elder 	max = u32_get_bits(val, IPA_MAX_CONS_PIPES_FMASK);
182184f9bd12SAlex Elder 	tx_mask = GENMASK(max - 1, 0);
182284f9bd12SAlex Elder 
182384f9bd12SAlex Elder 	ipa->available = rx_mask | tx_mask;
182484f9bd12SAlex Elder 
182584f9bd12SAlex Elder 	/* Check for initialized endpoints not supported by the hardware */
182684f9bd12SAlex Elder 	if (ipa->initialized & ~ipa->available) {
182784f9bd12SAlex Elder 		dev_err(dev, "unavailable endpoint id(s) 0x%08x\n",
182884f9bd12SAlex Elder 			ipa->initialized & ~ipa->available);
182984f9bd12SAlex Elder 		ret = -EINVAL;		/* Report other errors too */
183084f9bd12SAlex Elder 	}
183184f9bd12SAlex Elder 
183284f9bd12SAlex Elder 	initialized = ipa->initialized;
183384f9bd12SAlex Elder 	while (initialized) {
183484f9bd12SAlex Elder 		u32 endpoint_id = __ffs(initialized);
183584f9bd12SAlex Elder 		struct ipa_endpoint *endpoint;
183684f9bd12SAlex Elder 
183784f9bd12SAlex Elder 		initialized ^= BIT(endpoint_id);
183884f9bd12SAlex Elder 
183984f9bd12SAlex Elder 		/* Make sure it's pointing in the right direction */
184084f9bd12SAlex Elder 		endpoint = &ipa->endpoint[endpoint_id];
1841602a1c76SAlex Elder 		if ((endpoint_id < rx_base) != endpoint->toward_ipa) {
184284f9bd12SAlex Elder 			dev_err(dev, "endpoint id %u wrong direction\n",
184384f9bd12SAlex Elder 				endpoint_id);
184484f9bd12SAlex Elder 			ret = -EINVAL;
184584f9bd12SAlex Elder 		}
184684f9bd12SAlex Elder 	}
184784f9bd12SAlex Elder 
184884f9bd12SAlex Elder 	return ret;
184984f9bd12SAlex Elder }
185084f9bd12SAlex Elder 
185184f9bd12SAlex Elder void ipa_endpoint_deconfig(struct ipa *ipa)
185284f9bd12SAlex Elder {
185384f9bd12SAlex Elder 	ipa->available = 0;	/* Nothing more to do */
185484f9bd12SAlex Elder }
185584f9bd12SAlex Elder 
185684f9bd12SAlex Elder static void ipa_endpoint_init_one(struct ipa *ipa, enum ipa_endpoint_name name,
185784f9bd12SAlex Elder 				  const struct ipa_gsi_endpoint_data *data)
185884f9bd12SAlex Elder {
185984f9bd12SAlex Elder 	struct ipa_endpoint *endpoint;
186084f9bd12SAlex Elder 
186184f9bd12SAlex Elder 	endpoint = &ipa->endpoint[data->endpoint_id];
186284f9bd12SAlex Elder 
186384f9bd12SAlex Elder 	if (data->ee_id == GSI_EE_AP)
186484f9bd12SAlex Elder 		ipa->channel_map[data->channel_id] = endpoint;
186584f9bd12SAlex Elder 	ipa->name_map[name] = endpoint;
186684f9bd12SAlex Elder 
186784f9bd12SAlex Elder 	endpoint->ipa = ipa;
186884f9bd12SAlex Elder 	endpoint->ee_id = data->ee_id;
186984f9bd12SAlex Elder 	endpoint->channel_id = data->channel_id;
187084f9bd12SAlex Elder 	endpoint->endpoint_id = data->endpoint_id;
187184f9bd12SAlex Elder 	endpoint->toward_ipa = data->toward_ipa;
1872660e52d6SAlex Elder 	endpoint->config = data->endpoint.config;
187384f9bd12SAlex Elder 
187484f9bd12SAlex Elder 	ipa->initialized |= BIT(endpoint->endpoint_id);
187584f9bd12SAlex Elder }
187684f9bd12SAlex Elder 
1877602a1c76SAlex Elder static void ipa_endpoint_exit_one(struct ipa_endpoint *endpoint)
187884f9bd12SAlex Elder {
187984f9bd12SAlex Elder 	endpoint->ipa->initialized &= ~BIT(endpoint->endpoint_id);
188084f9bd12SAlex Elder 
188184f9bd12SAlex Elder 	memset(endpoint, 0, sizeof(*endpoint));
188284f9bd12SAlex Elder }
188384f9bd12SAlex Elder 
188484f9bd12SAlex Elder void ipa_endpoint_exit(struct ipa *ipa)
188584f9bd12SAlex Elder {
188684f9bd12SAlex Elder 	u32 initialized = ipa->initialized;
188784f9bd12SAlex Elder 
188884f9bd12SAlex Elder 	while (initialized) {
188984f9bd12SAlex Elder 		u32 endpoint_id = __fls(initialized);
189084f9bd12SAlex Elder 
189184f9bd12SAlex Elder 		initialized ^= BIT(endpoint_id);
189284f9bd12SAlex Elder 
189384f9bd12SAlex Elder 		ipa_endpoint_exit_one(&ipa->endpoint[endpoint_id]);
189484f9bd12SAlex Elder 	}
189584f9bd12SAlex Elder 	memset(ipa->name_map, 0, sizeof(ipa->name_map));
189684f9bd12SAlex Elder 	memset(ipa->channel_map, 0, sizeof(ipa->channel_map));
189784f9bd12SAlex Elder }
189884f9bd12SAlex Elder 
189984f9bd12SAlex Elder /* Returns a bitmask of endpoints that support filtering, or 0 on error */
190084f9bd12SAlex Elder u32 ipa_endpoint_init(struct ipa *ipa, u32 count,
190184f9bd12SAlex Elder 		      const struct ipa_gsi_endpoint_data *data)
190284f9bd12SAlex Elder {
190384f9bd12SAlex Elder 	enum ipa_endpoint_name name;
190484f9bd12SAlex Elder 	u32 filter_map;
190584f9bd12SAlex Elder 
19069654d8c4SAlex Elder 	BUILD_BUG_ON(!IPA_REPLENISH_BATCH);
19079654d8c4SAlex Elder 
190884f9bd12SAlex Elder 	if (!ipa_endpoint_data_valid(ipa, count, data))
190984f9bd12SAlex Elder 		return 0;	/* Error */
191084f9bd12SAlex Elder 
191184f9bd12SAlex Elder 	ipa->initialized = 0;
191284f9bd12SAlex Elder 
191384f9bd12SAlex Elder 	filter_map = 0;
191484f9bd12SAlex Elder 	for (name = 0; name < count; name++, data++) {
191584f9bd12SAlex Elder 		if (ipa_gsi_endpoint_data_empty(data))
191684f9bd12SAlex Elder 			continue;	/* Skip over empty slots */
191784f9bd12SAlex Elder 
191884f9bd12SAlex Elder 		ipa_endpoint_init_one(ipa, name, data);
191984f9bd12SAlex Elder 
192084f9bd12SAlex Elder 		if (data->endpoint.filter_support)
192184f9bd12SAlex Elder 			filter_map |= BIT(data->endpoint_id);
19222091c79aSAlex Elder 		if (data->ee_id == GSI_EE_MODEM && data->toward_ipa)
19232091c79aSAlex Elder 			ipa->modem_tx_count++;
192484f9bd12SAlex Elder 	}
192584f9bd12SAlex Elder 
192684f9bd12SAlex Elder 	if (!ipa_filter_map_valid(ipa, filter_map))
192784f9bd12SAlex Elder 		goto err_endpoint_exit;
192884f9bd12SAlex Elder 
192984f9bd12SAlex Elder 	return filter_map;	/* Non-zero bitmask */
193084f9bd12SAlex Elder 
193184f9bd12SAlex Elder err_endpoint_exit:
193284f9bd12SAlex Elder 	ipa_endpoint_exit(ipa);
193384f9bd12SAlex Elder 
193484f9bd12SAlex Elder 	return 0;	/* Error */
193584f9bd12SAlex Elder }
1936