xref: /linux/drivers/net/ipa/ipa_endpoint.c (revision 1f8d99de1d1b4b3764203ae02db57041475dab84)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
4  * Copyright (C) 2019-2021 Linaro Ltd.
5  */
6 
7 #include <linux/types.h>
8 #include <linux/device.h>
9 #include <linux/slab.h>
10 #include <linux/bitfield.h>
11 #include <linux/if_rmnet.h>
12 #include <linux/dma-direction.h>
13 
14 #include "gsi.h"
15 #include "gsi_trans.h"
16 #include "ipa.h"
17 #include "ipa_data.h"
18 #include "ipa_endpoint.h"
19 #include "ipa_cmd.h"
20 #include "ipa_mem.h"
21 #include "ipa_modem.h"
22 #include "ipa_table.h"
23 #include "ipa_gsi.h"
24 #include "ipa_power.h"
25 
26 #define atomic_dec_not_zero(v)	atomic_add_unless((v), -1, 0)
27 
28 #define IPA_REPLENISH_BATCH	16
29 
30 /* The amount of RX buffer space consumed by standard skb overhead */
31 #define IPA_RX_BUFFER_OVERHEAD	(PAGE_SIZE - SKB_MAX_ORDER(NET_SKB_PAD, 0))
32 
33 /* Where to find the QMAP mux_id for a packet within modem-supplied metadata */
34 #define IPA_ENDPOINT_QMAP_METADATA_MASK		0x000000ff /* host byte order */
35 
36 #define IPA_ENDPOINT_RESET_AGGR_RETRY_MAX	3
37 #define IPA_AGGR_TIME_LIMIT			500	/* microseconds */
38 
39 /** enum ipa_status_opcode - status element opcode hardware values */
40 enum ipa_status_opcode {
41 	IPA_STATUS_OPCODE_PACKET		= 0x01,
42 	IPA_STATUS_OPCODE_DROPPED_PACKET	= 0x04,
43 	IPA_STATUS_OPCODE_SUSPENDED_PACKET	= 0x08,
44 	IPA_STATUS_OPCODE_PACKET_2ND_PASS	= 0x40,
45 };
46 
47 /** enum ipa_status_exception - status element exception type */
48 enum ipa_status_exception {
49 	/* 0 means no exception */
50 	IPA_STATUS_EXCEPTION_DEAGGR		= 0x01,
51 };
52 
53 /* Status element provided by hardware */
54 struct ipa_status {
55 	u8 opcode;		/* enum ipa_status_opcode */
56 	u8 exception;		/* enum ipa_status_exception */
57 	__le16 mask;
58 	__le16 pkt_len;
59 	u8 endp_src_idx;
60 	u8 endp_dst_idx;
61 	__le32 metadata;
62 	__le32 flags1;
63 	__le64 flags2;
64 	__le32 flags3;
65 	__le32 flags4;
66 };
67 
68 /* Field masks for struct ipa_status structure fields */
69 #define IPA_STATUS_MASK_TAG_VALID_FMASK		GENMASK(4, 4)
70 #define IPA_STATUS_SRC_IDX_FMASK		GENMASK(4, 0)
71 #define IPA_STATUS_DST_IDX_FMASK		GENMASK(4, 0)
72 #define IPA_STATUS_FLAGS1_RT_RULE_ID_FMASK	GENMASK(31, 22)
73 #define IPA_STATUS_FLAGS2_TAG_FMASK		GENMASK_ULL(63, 16)
74 
75 static u32 aggr_byte_limit_max(enum ipa_version version)
76 {
77 	if (version < IPA_VERSION_4_5)
78 		return field_max(aggr_byte_limit_fmask(true));
79 
80 	return field_max(aggr_byte_limit_fmask(false));
81 }
82 
83 static bool ipa_endpoint_data_valid_one(struct ipa *ipa, u32 count,
84 			    const struct ipa_gsi_endpoint_data *all_data,
85 			    const struct ipa_gsi_endpoint_data *data)
86 {
87 	const struct ipa_gsi_endpoint_data *other_data;
88 	struct device *dev = &ipa->pdev->dev;
89 	enum ipa_endpoint_name other_name;
90 
91 	if (ipa_gsi_endpoint_data_empty(data))
92 		return true;
93 
94 	if (!data->toward_ipa) {
95 		u32 buffer_size;
96 		u32 limit;
97 
98 		if (data->endpoint.filter_support) {
99 			dev_err(dev, "filtering not supported for "
100 					"RX endpoint %u\n",
101 				data->endpoint_id);
102 			return false;
103 		}
104 
105 		/* Nothing more to check for non-AP RX */
106 		if (data->ee_id != GSI_EE_AP)
107 			return true;
108 
109 		buffer_size = data->endpoint.config.rx.buffer_size;
110 		/* The buffer size must hold an MTU plus overhead */
111 		limit = IPA_MTU + IPA_RX_BUFFER_OVERHEAD;
112 		if (buffer_size < limit) {
113 			dev_err(dev, "RX buffer size too small for RX endpoint %u (%u < %u)\n",
114 				data->endpoint_id, buffer_size, limit);
115 			return false;
116 		}
117 
118 		/* For an endpoint supporting receive aggregation, the
119 		 * aggregation byte limit defines the point at which an
120 		 * aggregation window will close.  It is programmed into the
121 		 * IPA hardware as a number of KB.  We don't use "hard byte
122 		 * limit" aggregation, so we need to supply enough space in
123 		 * a receive buffer to hold a complete MTU plus normal skb
124 		 * overhead *after* that aggregation byte limit has been
125 		 * crossed.
126 		 *
127 		 * This check just ensures the receive buffer size doesn't
128 		 * exceed what's representable in the aggregation limit field.
129 		 */
130 		if (data->endpoint.config.aggregation) {
131 			limit += SZ_1K * aggr_byte_limit_max(ipa->version);
132 			if (buffer_size > limit) {
133 				dev_err(dev, "RX buffer size too large for aggregated RX endpoint %u (%u > %u)\n",
134 					data->endpoint_id, buffer_size, limit);
135 
136 				return false;
137 			}
138 		}
139 
140 		return true;	/* Nothing more to check for RX */
141 	}
142 
143 	if (data->endpoint.config.status_enable) {
144 		other_name = data->endpoint.config.tx.status_endpoint;
145 		if (other_name >= count) {
146 			dev_err(dev, "status endpoint name %u out of range "
147 					"for endpoint %u\n",
148 				other_name, data->endpoint_id);
149 			return false;
150 		}
151 
152 		/* Status endpoint must be defined... */
153 		other_data = &all_data[other_name];
154 		if (ipa_gsi_endpoint_data_empty(other_data)) {
155 			dev_err(dev, "DMA endpoint name %u undefined "
156 					"for endpoint %u\n",
157 				other_name, data->endpoint_id);
158 			return false;
159 		}
160 
161 		/* ...and has to be an RX endpoint... */
162 		if (other_data->toward_ipa) {
163 			dev_err(dev,
164 				"status endpoint for endpoint %u not RX\n",
165 				data->endpoint_id);
166 			return false;
167 		}
168 
169 		/* ...and if it's to be an AP endpoint... */
170 		if (other_data->ee_id == GSI_EE_AP) {
171 			/* ...make sure it has status enabled. */
172 			if (!other_data->endpoint.config.status_enable) {
173 				dev_err(dev,
174 					"status not enabled for endpoint %u\n",
175 					other_data->endpoint_id);
176 				return false;
177 			}
178 		}
179 	}
180 
181 	if (data->endpoint.config.dma_mode) {
182 		other_name = data->endpoint.config.dma_endpoint;
183 		if (other_name >= count) {
184 			dev_err(dev, "DMA endpoint name %u out of range "
185 					"for endpoint %u\n",
186 				other_name, data->endpoint_id);
187 			return false;
188 		}
189 
190 		other_data = &all_data[other_name];
191 		if (ipa_gsi_endpoint_data_empty(other_data)) {
192 			dev_err(dev, "DMA endpoint name %u undefined "
193 					"for endpoint %u\n",
194 				other_name, data->endpoint_id);
195 			return false;
196 		}
197 	}
198 
199 	return true;
200 }
201 
202 static bool ipa_endpoint_data_valid(struct ipa *ipa, u32 count,
203 				    const struct ipa_gsi_endpoint_data *data)
204 {
205 	const struct ipa_gsi_endpoint_data *dp = data;
206 	struct device *dev = &ipa->pdev->dev;
207 	enum ipa_endpoint_name name;
208 
209 	if (count > IPA_ENDPOINT_COUNT) {
210 		dev_err(dev, "too many endpoints specified (%u > %u)\n",
211 			count, IPA_ENDPOINT_COUNT);
212 		return false;
213 	}
214 
215 	/* Make sure needed endpoints have defined data */
216 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_COMMAND_TX])) {
217 		dev_err(dev, "command TX endpoint not defined\n");
218 		return false;
219 	}
220 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_LAN_RX])) {
221 		dev_err(dev, "LAN RX endpoint not defined\n");
222 		return false;
223 	}
224 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_MODEM_TX])) {
225 		dev_err(dev, "AP->modem TX endpoint not defined\n");
226 		return false;
227 	}
228 	if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_MODEM_RX])) {
229 		dev_err(dev, "AP<-modem RX endpoint not defined\n");
230 		return false;
231 	}
232 
233 	for (name = 0; name < count; name++, dp++)
234 		if (!ipa_endpoint_data_valid_one(ipa, count, data, dp))
235 			return false;
236 
237 	return true;
238 }
239 
240 /* Allocate a transaction to use on a non-command endpoint */
241 static struct gsi_trans *ipa_endpoint_trans_alloc(struct ipa_endpoint *endpoint,
242 						  u32 tre_count)
243 {
244 	struct gsi *gsi = &endpoint->ipa->gsi;
245 	u32 channel_id = endpoint->channel_id;
246 	enum dma_data_direction direction;
247 
248 	direction = endpoint->toward_ipa ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
249 
250 	return gsi_channel_trans_alloc(gsi, channel_id, tre_count, direction);
251 }
252 
253 /* suspend_delay represents suspend for RX, delay for TX endpoints.
254  * Note that suspend is not supported starting with IPA v4.0, and
255  * delay mode should not be used starting with IPA v4.2.
256  */
257 static bool
258 ipa_endpoint_init_ctrl(struct ipa_endpoint *endpoint, bool suspend_delay)
259 {
260 	u32 offset = IPA_REG_ENDP_INIT_CTRL_N_OFFSET(endpoint->endpoint_id);
261 	struct ipa *ipa = endpoint->ipa;
262 	bool state;
263 	u32 mask;
264 	u32 val;
265 
266 	if (endpoint->toward_ipa)
267 		WARN_ON(ipa->version >= IPA_VERSION_4_2);
268 	else
269 		WARN_ON(ipa->version >= IPA_VERSION_4_0);
270 
271 	mask = endpoint->toward_ipa ? ENDP_DELAY_FMASK : ENDP_SUSPEND_FMASK;
272 
273 	val = ioread32(ipa->reg_virt + offset);
274 	state = !!(val & mask);
275 
276 	/* Don't bother if it's already in the requested state */
277 	if (suspend_delay != state) {
278 		val ^= mask;
279 		iowrite32(val, ipa->reg_virt + offset);
280 	}
281 
282 	return state;
283 }
284 
285 /* We don't care what the previous state was for delay mode */
286 static void
287 ipa_endpoint_program_delay(struct ipa_endpoint *endpoint, bool enable)
288 {
289 	/* Delay mode should not be used for IPA v4.2+ */
290 	WARN_ON(endpoint->ipa->version >= IPA_VERSION_4_2);
291 	WARN_ON(!endpoint->toward_ipa);
292 
293 	(void)ipa_endpoint_init_ctrl(endpoint, enable);
294 }
295 
296 static bool ipa_endpoint_aggr_active(struct ipa_endpoint *endpoint)
297 {
298 	u32 mask = BIT(endpoint->endpoint_id);
299 	struct ipa *ipa = endpoint->ipa;
300 	u32 offset;
301 	u32 val;
302 
303 	WARN_ON(!(mask & ipa->available));
304 
305 	offset = ipa_reg_state_aggr_active_offset(ipa->version);
306 	val = ioread32(ipa->reg_virt + offset);
307 
308 	return !!(val & mask);
309 }
310 
311 static void ipa_endpoint_force_close(struct ipa_endpoint *endpoint)
312 {
313 	u32 mask = BIT(endpoint->endpoint_id);
314 	struct ipa *ipa = endpoint->ipa;
315 
316 	WARN_ON(!(mask & ipa->available));
317 
318 	iowrite32(mask, ipa->reg_virt + IPA_REG_AGGR_FORCE_CLOSE_OFFSET);
319 }
320 
321 /**
322  * ipa_endpoint_suspend_aggr() - Emulate suspend interrupt
323  * @endpoint:	Endpoint on which to emulate a suspend
324  *
325  *  Emulate suspend IPA interrupt to unsuspend an endpoint suspended
326  *  with an open aggregation frame.  This is to work around a hardware
327  *  issue in IPA version 3.5.1 where the suspend interrupt will not be
328  *  generated when it should be.
329  */
330 static void ipa_endpoint_suspend_aggr(struct ipa_endpoint *endpoint)
331 {
332 	struct ipa *ipa = endpoint->ipa;
333 
334 	if (!endpoint->data->aggregation)
335 		return;
336 
337 	/* Nothing to do if the endpoint doesn't have aggregation open */
338 	if (!ipa_endpoint_aggr_active(endpoint))
339 		return;
340 
341 	/* Force close aggregation */
342 	ipa_endpoint_force_close(endpoint);
343 
344 	ipa_interrupt_simulate_suspend(ipa->interrupt);
345 }
346 
347 /* Returns previous suspend state (true means suspend was enabled) */
348 static bool
349 ipa_endpoint_program_suspend(struct ipa_endpoint *endpoint, bool enable)
350 {
351 	bool suspended;
352 
353 	if (endpoint->ipa->version >= IPA_VERSION_4_0)
354 		return enable;	/* For IPA v4.0+, no change made */
355 
356 	WARN_ON(endpoint->toward_ipa);
357 
358 	suspended = ipa_endpoint_init_ctrl(endpoint, enable);
359 
360 	/* A client suspended with an open aggregation frame will not
361 	 * generate a SUSPEND IPA interrupt.  If enabling suspend, have
362 	 * ipa_endpoint_suspend_aggr() handle this.
363 	 */
364 	if (enable && !suspended)
365 		ipa_endpoint_suspend_aggr(endpoint);
366 
367 	return suspended;
368 }
369 
370 /* Put all modem RX endpoints into suspend mode, and stop transmission
371  * on all modem TX endpoints.  Prior to IPA v4.2, endpoint DELAY mode is
372  * used for TX endpoints; starting with IPA v4.2 we use GSI channel flow
373  * control instead.
374  */
375 void ipa_endpoint_modem_pause_all(struct ipa *ipa, bool enable)
376 {
377 	u32 endpoint_id;
378 
379 	for (endpoint_id = 0; endpoint_id < IPA_ENDPOINT_MAX; endpoint_id++) {
380 		struct ipa_endpoint *endpoint = &ipa->endpoint[endpoint_id];
381 
382 		if (endpoint->ee_id != GSI_EE_MODEM)
383 			continue;
384 
385 		if (!endpoint->toward_ipa)
386 			(void)ipa_endpoint_program_suspend(endpoint, enable);
387 		else if (ipa->version < IPA_VERSION_4_2)
388 			ipa_endpoint_program_delay(endpoint, enable);
389 		else
390 			gsi_modem_channel_flow_control(&ipa->gsi,
391 						       endpoint->channel_id,
392 						       enable);
393 	}
394 }
395 
396 /* Reset all modem endpoints to use the default exception endpoint */
397 int ipa_endpoint_modem_exception_reset_all(struct ipa *ipa)
398 {
399 	u32 initialized = ipa->initialized;
400 	struct gsi_trans *trans;
401 	u32 count;
402 
403 	/* We need one command per modem TX endpoint.  We can get an upper
404 	 * bound on that by assuming all initialized endpoints are modem->IPA.
405 	 * That won't happen, and we could be more precise, but this is fine
406 	 * for now.  End the transaction with commands to clear the pipeline.
407 	 */
408 	count = hweight32(initialized) + ipa_cmd_pipeline_clear_count();
409 	trans = ipa_cmd_trans_alloc(ipa, count);
410 	if (!trans) {
411 		dev_err(&ipa->pdev->dev,
412 			"no transaction to reset modem exception endpoints\n");
413 		return -EBUSY;
414 	}
415 
416 	while (initialized) {
417 		u32 endpoint_id = __ffs(initialized);
418 		struct ipa_endpoint *endpoint;
419 		u32 offset;
420 
421 		initialized ^= BIT(endpoint_id);
422 
423 		/* We only reset modem TX endpoints */
424 		endpoint = &ipa->endpoint[endpoint_id];
425 		if (!(endpoint->ee_id == GSI_EE_MODEM && endpoint->toward_ipa))
426 			continue;
427 
428 		offset = IPA_REG_ENDP_STATUS_N_OFFSET(endpoint_id);
429 
430 		/* Value written is 0, and all bits are updated.  That
431 		 * means status is disabled on the endpoint, and as a
432 		 * result all other fields in the register are ignored.
433 		 */
434 		ipa_cmd_register_write_add(trans, offset, 0, ~0, false);
435 	}
436 
437 	ipa_cmd_pipeline_clear_add(trans);
438 
439 	/* XXX This should have a 1 second timeout */
440 	gsi_trans_commit_wait(trans);
441 
442 	ipa_cmd_pipeline_clear_wait(ipa);
443 
444 	return 0;
445 }
446 
447 static void ipa_endpoint_init_cfg(struct ipa_endpoint *endpoint)
448 {
449 	u32 offset = IPA_REG_ENDP_INIT_CFG_N_OFFSET(endpoint->endpoint_id);
450 	enum ipa_cs_offload_en enabled;
451 	u32 val = 0;
452 
453 	/* FRAG_OFFLOAD_EN is 0 */
454 	if (endpoint->data->checksum) {
455 		enum ipa_version version = endpoint->ipa->version;
456 
457 		if (endpoint->toward_ipa) {
458 			u32 checksum_offset;
459 
460 			/* Checksum header offset is in 4-byte units */
461 			checksum_offset = sizeof(struct rmnet_map_header);
462 			checksum_offset /= sizeof(u32);
463 			val |= u32_encode_bits(checksum_offset,
464 					       CS_METADATA_HDR_OFFSET_FMASK);
465 
466 			enabled = version < IPA_VERSION_4_5
467 					? IPA_CS_OFFLOAD_UL
468 					: IPA_CS_OFFLOAD_INLINE;
469 		} else {
470 			enabled = version < IPA_VERSION_4_5
471 					? IPA_CS_OFFLOAD_DL
472 					: IPA_CS_OFFLOAD_INLINE;
473 		}
474 	} else {
475 		enabled = IPA_CS_OFFLOAD_NONE;
476 	}
477 	val |= u32_encode_bits(enabled, CS_OFFLOAD_EN_FMASK);
478 	/* CS_GEN_QMB_MASTER_SEL is 0 */
479 
480 	iowrite32(val, endpoint->ipa->reg_virt + offset);
481 }
482 
483 static void ipa_endpoint_init_nat(struct ipa_endpoint *endpoint)
484 {
485 	u32 offset;
486 	u32 val;
487 
488 	if (!endpoint->toward_ipa)
489 		return;
490 
491 	offset = IPA_REG_ENDP_INIT_NAT_N_OFFSET(endpoint->endpoint_id);
492 	val = u32_encode_bits(IPA_NAT_BYPASS, NAT_EN_FMASK);
493 
494 	iowrite32(val, endpoint->ipa->reg_virt + offset);
495 }
496 
497 static u32
498 ipa_qmap_header_size(enum ipa_version version, struct ipa_endpoint *endpoint)
499 {
500 	u32 header_size = sizeof(struct rmnet_map_header);
501 
502 	/* Without checksum offload, we just have the MAP header */
503 	if (!endpoint->data->checksum)
504 		return header_size;
505 
506 	if (version < IPA_VERSION_4_5) {
507 		/* Checksum header inserted for AP TX endpoints only */
508 		if (endpoint->toward_ipa)
509 			header_size += sizeof(struct rmnet_map_ul_csum_header);
510 	} else {
511 		/* Checksum header is used in both directions */
512 		header_size += sizeof(struct rmnet_map_v5_csum_header);
513 	}
514 
515 	return header_size;
516 }
517 
518 /**
519  * ipa_endpoint_init_hdr() - Initialize HDR endpoint configuration register
520  * @endpoint:	Endpoint pointer
521  *
522  * We program QMAP endpoints so each packet received is preceded by a QMAP
523  * header structure.  The QMAP header contains a 1-byte mux_id and 2-byte
524  * packet size field, and we have the IPA hardware populate both for each
525  * received packet.  The header is configured (in the HDR_EXT register)
526  * to use big endian format.
527  *
528  * The packet size is written into the QMAP header's pkt_len field.  That
529  * location is defined here using the HDR_OFST_PKT_SIZE field.
530  *
531  * The mux_id comes from a 4-byte metadata value supplied with each packet
532  * by the modem.  It is *not* a QMAP header, but it does contain the mux_id
533  * value that we want, in its low-order byte.  A bitmask defined in the
534  * endpoint's METADATA_MASK register defines which byte within the modem
535  * metadata contains the mux_id.  And the OFST_METADATA field programmed
536  * here indicates where the extracted byte should be placed within the QMAP
537  * header.
538  */
539 static void ipa_endpoint_init_hdr(struct ipa_endpoint *endpoint)
540 {
541 	u32 offset = IPA_REG_ENDP_INIT_HDR_N_OFFSET(endpoint->endpoint_id);
542 	struct ipa *ipa = endpoint->ipa;
543 	u32 val = 0;
544 
545 	if (endpoint->data->qmap) {
546 		enum ipa_version version = ipa->version;
547 		size_t header_size;
548 
549 		header_size = ipa_qmap_header_size(version, endpoint);
550 		val = ipa_header_size_encoded(version, header_size);
551 
552 		/* Define how to fill fields in a received QMAP header */
553 		if (!endpoint->toward_ipa) {
554 			u32 offset;	/* Field offset within header */
555 
556 			/* Where IPA will write the metadata value */
557 			offset = offsetof(struct rmnet_map_header, mux_id);
558 			val |= ipa_metadata_offset_encoded(version, offset);
559 
560 			/* Where IPA will write the length */
561 			offset = offsetof(struct rmnet_map_header, pkt_len);
562 			/* Upper bits are stored in HDR_EXT with IPA v4.5 */
563 			if (version >= IPA_VERSION_4_5)
564 				offset &= field_mask(HDR_OFST_PKT_SIZE_FMASK);
565 
566 			val |= HDR_OFST_PKT_SIZE_VALID_FMASK;
567 			val |= u32_encode_bits(offset, HDR_OFST_PKT_SIZE_FMASK);
568 		}
569 		/* For QMAP TX, metadata offset is 0 (modem assumes this) */
570 		val |= HDR_OFST_METADATA_VALID_FMASK;
571 
572 		/* HDR_ADDITIONAL_CONST_LEN is 0; (RX only) */
573 		/* HDR_A5_MUX is 0 */
574 		/* HDR_LEN_INC_DEAGG_HDR is 0 */
575 		/* HDR_METADATA_REG_VALID is 0 (TX only, version < v4.5) */
576 	}
577 
578 	iowrite32(val, ipa->reg_virt + offset);
579 }
580 
581 static void ipa_endpoint_init_hdr_ext(struct ipa_endpoint *endpoint)
582 {
583 	u32 offset = IPA_REG_ENDP_INIT_HDR_EXT_N_OFFSET(endpoint->endpoint_id);
584 	u32 pad_align = endpoint->data->rx.pad_align;
585 	struct ipa *ipa = endpoint->ipa;
586 	u32 val = 0;
587 
588 	val |= HDR_ENDIANNESS_FMASK;		/* big endian */
589 
590 	/* A QMAP header contains a 6 bit pad field at offset 0.  The RMNet
591 	 * driver assumes this field is meaningful in packets it receives,
592 	 * and assumes the header's payload length includes that padding.
593 	 * The RMNet driver does *not* pad packets it sends, however, so
594 	 * the pad field (although 0) should be ignored.
595 	 */
596 	if (endpoint->data->qmap && !endpoint->toward_ipa) {
597 		val |= HDR_TOTAL_LEN_OR_PAD_VALID_FMASK;
598 		/* HDR_TOTAL_LEN_OR_PAD is 0 (pad, not total_len) */
599 		val |= HDR_PAYLOAD_LEN_INC_PADDING_FMASK;
600 		/* HDR_TOTAL_LEN_OR_PAD_OFFSET is 0 */
601 	}
602 
603 	/* HDR_PAYLOAD_LEN_INC_PADDING is 0 */
604 	if (!endpoint->toward_ipa)
605 		val |= u32_encode_bits(pad_align, HDR_PAD_TO_ALIGNMENT_FMASK);
606 
607 	/* IPA v4.5 adds some most-significant bits to a few fields,
608 	 * two of which are defined in the HDR (not HDR_EXT) register.
609 	 */
610 	if (ipa->version >= IPA_VERSION_4_5) {
611 		/* HDR_TOTAL_LEN_OR_PAD_OFFSET is 0, so MSB is 0 */
612 		if (endpoint->data->qmap && !endpoint->toward_ipa) {
613 			u32 offset;
614 
615 			offset = offsetof(struct rmnet_map_header, pkt_len);
616 			offset >>= hweight32(HDR_OFST_PKT_SIZE_FMASK);
617 			val |= u32_encode_bits(offset,
618 					       HDR_OFST_PKT_SIZE_MSB_FMASK);
619 			/* HDR_ADDITIONAL_CONST_LEN is 0 so MSB is 0 */
620 		}
621 	}
622 	iowrite32(val, ipa->reg_virt + offset);
623 }
624 
625 static void ipa_endpoint_init_hdr_metadata_mask(struct ipa_endpoint *endpoint)
626 {
627 	u32 endpoint_id = endpoint->endpoint_id;
628 	u32 val = 0;
629 	u32 offset;
630 
631 	if (endpoint->toward_ipa)
632 		return;		/* Register not valid for TX endpoints */
633 
634 	offset = IPA_REG_ENDP_INIT_HDR_METADATA_MASK_N_OFFSET(endpoint_id);
635 
636 	/* Note that HDR_ENDIANNESS indicates big endian header fields */
637 	if (endpoint->data->qmap)
638 		val = (__force u32)cpu_to_be32(IPA_ENDPOINT_QMAP_METADATA_MASK);
639 
640 	iowrite32(val, endpoint->ipa->reg_virt + offset);
641 }
642 
643 static void ipa_endpoint_init_mode(struct ipa_endpoint *endpoint)
644 {
645 	u32 offset = IPA_REG_ENDP_INIT_MODE_N_OFFSET(endpoint->endpoint_id);
646 	u32 val;
647 
648 	if (!endpoint->toward_ipa)
649 		return;		/* Register not valid for RX endpoints */
650 
651 	if (endpoint->data->dma_mode) {
652 		enum ipa_endpoint_name name = endpoint->data->dma_endpoint;
653 		u32 dma_endpoint_id;
654 
655 		dma_endpoint_id = endpoint->ipa->name_map[name]->endpoint_id;
656 
657 		val = u32_encode_bits(IPA_DMA, MODE_FMASK);
658 		val |= u32_encode_bits(dma_endpoint_id, DEST_PIPE_INDEX_FMASK);
659 	} else {
660 		val = u32_encode_bits(IPA_BASIC, MODE_FMASK);
661 	}
662 	/* All other bits unspecified (and 0) */
663 
664 	iowrite32(val, endpoint->ipa->reg_virt + offset);
665 }
666 
667 /* Compute the aggregation size value to use for a given buffer size */
668 static u32 ipa_aggr_size_kb(u32 rx_buffer_size)
669 {
670 	/* We don't use "hard byte limit" aggregation, so we define the
671 	 * aggregation limit such that our buffer has enough space *after*
672 	 * that limit to receive a full MTU of data, plus overhead.
673 	 */
674 	rx_buffer_size -= IPA_MTU + IPA_RX_BUFFER_OVERHEAD;
675 
676 	return rx_buffer_size / SZ_1K;
677 }
678 
679 /* Encoded values for AGGR endpoint register fields */
680 static u32 aggr_byte_limit_encoded(enum ipa_version version, u32 limit)
681 {
682 	if (version < IPA_VERSION_4_5)
683 		return u32_encode_bits(limit, aggr_byte_limit_fmask(true));
684 
685 	return u32_encode_bits(limit, aggr_byte_limit_fmask(false));
686 }
687 
688 /* Encode the aggregation timer limit (microseconds) based on IPA version */
689 static u32 aggr_time_limit_encoded(enum ipa_version version, u32 limit)
690 {
691 	u32 gran_sel;
692 	u32 fmask;
693 	u32 val;
694 
695 	if (version < IPA_VERSION_4_5) {
696 		/* We set aggregation granularity in ipa_hardware_config() */
697 		limit = DIV_ROUND_CLOSEST(limit, IPA_AGGR_GRANULARITY);
698 
699 		return u32_encode_bits(limit, aggr_time_limit_fmask(true));
700 	}
701 
702 	/* IPA v4.5 expresses the time limit using Qtime.  The AP has
703 	 * pulse generators 0 and 1 available, which were configured
704 	 * in ipa_qtime_config() to have granularity 100 usec and
705 	 * 1 msec, respectively.  Use pulse generator 0 if possible,
706 	 * otherwise fall back to pulse generator 1.
707 	 */
708 	fmask = aggr_time_limit_fmask(false);
709 	val = DIV_ROUND_CLOSEST(limit, 100);
710 	if (val > field_max(fmask)) {
711 		/* Have to use pulse generator 1 (millisecond granularity) */
712 		gran_sel = AGGR_GRAN_SEL_FMASK;
713 		val = DIV_ROUND_CLOSEST(limit, 1000);
714 	} else {
715 		/* We can use pulse generator 0 (100 usec granularity) */
716 		gran_sel = 0;
717 	}
718 
719 	return gran_sel | u32_encode_bits(val, fmask);
720 }
721 
722 static u32 aggr_sw_eof_active_encoded(enum ipa_version version, bool enabled)
723 {
724 	u32 val = enabled ? 1 : 0;
725 
726 	if (version < IPA_VERSION_4_5)
727 		return u32_encode_bits(val, aggr_sw_eof_active_fmask(true));
728 
729 	return u32_encode_bits(val, aggr_sw_eof_active_fmask(false));
730 }
731 
732 static void ipa_endpoint_init_aggr(struct ipa_endpoint *endpoint)
733 {
734 	u32 offset = IPA_REG_ENDP_INIT_AGGR_N_OFFSET(endpoint->endpoint_id);
735 	enum ipa_version version = endpoint->ipa->version;
736 	u32 val = 0;
737 
738 	if (endpoint->data->aggregation) {
739 		if (!endpoint->toward_ipa) {
740 			const struct ipa_endpoint_rx_data *rx_data;
741 			bool close_eof;
742 			u32 limit;
743 
744 			rx_data = &endpoint->data->rx;
745 			val |= u32_encode_bits(IPA_ENABLE_AGGR, AGGR_EN_FMASK);
746 			val |= u32_encode_bits(IPA_GENERIC, AGGR_TYPE_FMASK);
747 
748 			limit = ipa_aggr_size_kb(rx_data->buffer_size);
749 			val |= aggr_byte_limit_encoded(version, limit);
750 
751 			limit = IPA_AGGR_TIME_LIMIT;
752 			val |= aggr_time_limit_encoded(version, limit);
753 
754 			/* AGGR_PKT_LIMIT is 0 (unlimited) */
755 
756 			close_eof = rx_data->aggr_close_eof;
757 			val |= aggr_sw_eof_active_encoded(version, close_eof);
758 
759 			/* AGGR_HARD_BYTE_LIMIT_ENABLE is 0 */
760 		} else {
761 			val |= u32_encode_bits(IPA_ENABLE_DEAGGR,
762 					       AGGR_EN_FMASK);
763 			val |= u32_encode_bits(IPA_QCMAP, AGGR_TYPE_FMASK);
764 			/* other fields ignored */
765 		}
766 		/* AGGR_FORCE_CLOSE is 0 */
767 		/* AGGR_GRAN_SEL is 0 for IPA v4.5 */
768 	} else {
769 		val |= u32_encode_bits(IPA_BYPASS_AGGR, AGGR_EN_FMASK);
770 		/* other fields ignored */
771 	}
772 
773 	iowrite32(val, endpoint->ipa->reg_virt + offset);
774 }
775 
776 /* Return the Qtime-based head-of-line blocking timer value that
777  * represents the given number of microseconds.  The result
778  * includes both the timer value and the selected timer granularity.
779  */
780 static u32 hol_block_timer_qtime_val(struct ipa *ipa, u32 microseconds)
781 {
782 	u32 gran_sel;
783 	u32 val;
784 
785 	/* IPA v4.5 expresses time limits using Qtime.  The AP has
786 	 * pulse generators 0 and 1 available, which were configured
787 	 * in ipa_qtime_config() to have granularity 100 usec and
788 	 * 1 msec, respectively.  Use pulse generator 0 if possible,
789 	 * otherwise fall back to pulse generator 1.
790 	 */
791 	val = DIV_ROUND_CLOSEST(microseconds, 100);
792 	if (val > field_max(TIME_LIMIT_FMASK)) {
793 		/* Have to use pulse generator 1 (millisecond granularity) */
794 		gran_sel = GRAN_SEL_FMASK;
795 		val = DIV_ROUND_CLOSEST(microseconds, 1000);
796 	} else {
797 		/* We can use pulse generator 0 (100 usec granularity) */
798 		gran_sel = 0;
799 	}
800 
801 	return gran_sel | u32_encode_bits(val, TIME_LIMIT_FMASK);
802 }
803 
804 /* The head-of-line blocking timer is defined as a tick count.  For
805  * IPA version 4.5 the tick count is based on the Qtimer, which is
806  * derived from the 19.2 MHz SoC XO clock.  For older IPA versions
807  * each tick represents 128 cycles of the IPA core clock.
808  *
809  * Return the encoded value that should be written to that register
810  * that represents the timeout period provided.  For IPA v4.2 this
811  * encodes a base and scale value, while for earlier versions the
812  * value is a simple tick count.
813  */
814 static u32 hol_block_timer_val(struct ipa *ipa, u32 microseconds)
815 {
816 	u32 width;
817 	u32 scale;
818 	u64 ticks;
819 	u64 rate;
820 	u32 high;
821 	u32 val;
822 
823 	if (!microseconds)
824 		return 0;	/* Nothing to compute if timer period is 0 */
825 
826 	if (ipa->version >= IPA_VERSION_4_5)
827 		return hol_block_timer_qtime_val(ipa, microseconds);
828 
829 	/* Use 64 bit arithmetic to avoid overflow... */
830 	rate = ipa_core_clock_rate(ipa);
831 	ticks = DIV_ROUND_CLOSEST(microseconds * rate, 128 * USEC_PER_SEC);
832 	/* ...but we still need to fit into a 32-bit register */
833 	WARN_ON(ticks > U32_MAX);
834 
835 	/* IPA v3.5.1 through v4.1 just record the tick count */
836 	if (ipa->version < IPA_VERSION_4_2)
837 		return (u32)ticks;
838 
839 	/* For IPA v4.2, the tick count is represented by base and
840 	 * scale fields within the 32-bit timer register, where:
841 	 *     ticks = base << scale;
842 	 * The best precision is achieved when the base value is as
843 	 * large as possible.  Find the highest set bit in the tick
844 	 * count, and extract the number of bits in the base field
845 	 * such that high bit is included.
846 	 */
847 	high = fls(ticks);		/* 1..32 */
848 	width = HWEIGHT32(BASE_VALUE_FMASK);
849 	scale = high > width ? high - width : 0;
850 	if (scale) {
851 		/* If we're scaling, round up to get a closer result */
852 		ticks += 1 << (scale - 1);
853 		/* High bit was set, so rounding might have affected it */
854 		if (fls(ticks) != high)
855 			scale++;
856 	}
857 
858 	val = u32_encode_bits(scale, SCALE_FMASK);
859 	val |= u32_encode_bits(ticks >> scale, BASE_VALUE_FMASK);
860 
861 	return val;
862 }
863 
864 /* If microseconds is 0, timeout is immediate */
865 static void ipa_endpoint_init_hol_block_timer(struct ipa_endpoint *endpoint,
866 					      u32 microseconds)
867 {
868 	u32 endpoint_id = endpoint->endpoint_id;
869 	struct ipa *ipa = endpoint->ipa;
870 	u32 offset;
871 	u32 val;
872 
873 	/* This should only be changed when HOL_BLOCK_EN is disabled */
874 	offset = IPA_REG_ENDP_INIT_HOL_BLOCK_TIMER_N_OFFSET(endpoint_id);
875 	val = hol_block_timer_val(ipa, microseconds);
876 	iowrite32(val, ipa->reg_virt + offset);
877 }
878 
879 static void
880 ipa_endpoint_init_hol_block_en(struct ipa_endpoint *endpoint, bool enable)
881 {
882 	u32 endpoint_id = endpoint->endpoint_id;
883 	u32 offset;
884 	u32 val;
885 
886 	val = enable ? HOL_BLOCK_EN_FMASK : 0;
887 	offset = IPA_REG_ENDP_INIT_HOL_BLOCK_EN_N_OFFSET(endpoint_id);
888 	iowrite32(val, endpoint->ipa->reg_virt + offset);
889 	/* When enabling, the register must be written twice for IPA v4.5+ */
890 	if (enable && endpoint->ipa->version >= IPA_VERSION_4_5)
891 		iowrite32(val, endpoint->ipa->reg_virt + offset);
892 }
893 
894 /* Assumes HOL_BLOCK is in disabled state */
895 static void ipa_endpoint_init_hol_block_enable(struct ipa_endpoint *endpoint,
896 					       u32 microseconds)
897 {
898 	ipa_endpoint_init_hol_block_timer(endpoint, microseconds);
899 	ipa_endpoint_init_hol_block_en(endpoint, true);
900 }
901 
902 static void ipa_endpoint_init_hol_block_disable(struct ipa_endpoint *endpoint)
903 {
904 	ipa_endpoint_init_hol_block_en(endpoint, false);
905 }
906 
907 void ipa_endpoint_modem_hol_block_clear_all(struct ipa *ipa)
908 {
909 	u32 i;
910 
911 	for (i = 0; i < IPA_ENDPOINT_MAX; i++) {
912 		struct ipa_endpoint *endpoint = &ipa->endpoint[i];
913 
914 		if (endpoint->toward_ipa || endpoint->ee_id != GSI_EE_MODEM)
915 			continue;
916 
917 		ipa_endpoint_init_hol_block_disable(endpoint);
918 		ipa_endpoint_init_hol_block_enable(endpoint, 0);
919 	}
920 }
921 
922 static void ipa_endpoint_init_deaggr(struct ipa_endpoint *endpoint)
923 {
924 	u32 offset = IPA_REG_ENDP_INIT_DEAGGR_N_OFFSET(endpoint->endpoint_id);
925 	u32 val = 0;
926 
927 	if (!endpoint->toward_ipa)
928 		return;		/* Register not valid for RX endpoints */
929 
930 	/* DEAGGR_HDR_LEN is 0 */
931 	/* PACKET_OFFSET_VALID is 0 */
932 	/* PACKET_OFFSET_LOCATION is ignored (not valid) */
933 	/* MAX_PACKET_LEN is 0 (not enforced) */
934 
935 	iowrite32(val, endpoint->ipa->reg_virt + offset);
936 }
937 
938 static void ipa_endpoint_init_rsrc_grp(struct ipa_endpoint *endpoint)
939 {
940 	u32 offset = IPA_REG_ENDP_INIT_RSRC_GRP_N_OFFSET(endpoint->endpoint_id);
941 	struct ipa *ipa = endpoint->ipa;
942 	u32 val;
943 
944 	val = rsrc_grp_encoded(ipa->version, endpoint->data->resource_group);
945 	iowrite32(val, ipa->reg_virt + offset);
946 }
947 
948 static void ipa_endpoint_init_seq(struct ipa_endpoint *endpoint)
949 {
950 	u32 offset = IPA_REG_ENDP_INIT_SEQ_N_OFFSET(endpoint->endpoint_id);
951 	u32 val = 0;
952 
953 	if (!endpoint->toward_ipa)
954 		return;		/* Register not valid for RX endpoints */
955 
956 	/* Low-order byte configures primary packet processing */
957 	val |= u32_encode_bits(endpoint->data->tx.seq_type, SEQ_TYPE_FMASK);
958 
959 	/* Second byte configures replicated packet processing */
960 	val |= u32_encode_bits(endpoint->data->tx.seq_rep_type,
961 			       SEQ_REP_TYPE_FMASK);
962 
963 	iowrite32(val, endpoint->ipa->reg_virt + offset);
964 }
965 
966 /**
967  * ipa_endpoint_skb_tx() - Transmit a socket buffer
968  * @endpoint:	Endpoint pointer
969  * @skb:	Socket buffer to send
970  *
971  * Returns:	0 if successful, or a negative error code
972  */
973 int ipa_endpoint_skb_tx(struct ipa_endpoint *endpoint, struct sk_buff *skb)
974 {
975 	struct gsi_trans *trans;
976 	u32 nr_frags;
977 	int ret;
978 
979 	/* Make sure source endpoint's TLV FIFO has enough entries to
980 	 * hold the linear portion of the skb and all its fragments.
981 	 * If not, see if we can linearize it before giving up.
982 	 */
983 	nr_frags = skb_shinfo(skb)->nr_frags;
984 	if (1 + nr_frags > endpoint->trans_tre_max) {
985 		if (skb_linearize(skb))
986 			return -E2BIG;
987 		nr_frags = 0;
988 	}
989 
990 	trans = ipa_endpoint_trans_alloc(endpoint, 1 + nr_frags);
991 	if (!trans)
992 		return -EBUSY;
993 
994 	ret = gsi_trans_skb_add(trans, skb);
995 	if (ret)
996 		goto err_trans_free;
997 	trans->data = skb;	/* transaction owns skb now */
998 
999 	gsi_trans_commit(trans, !netdev_xmit_more());
1000 
1001 	return 0;
1002 
1003 err_trans_free:
1004 	gsi_trans_free(trans);
1005 
1006 	return -ENOMEM;
1007 }
1008 
1009 static void ipa_endpoint_status(struct ipa_endpoint *endpoint)
1010 {
1011 	u32 endpoint_id = endpoint->endpoint_id;
1012 	struct ipa *ipa = endpoint->ipa;
1013 	u32 val = 0;
1014 	u32 offset;
1015 
1016 	offset = IPA_REG_ENDP_STATUS_N_OFFSET(endpoint_id);
1017 
1018 	if (endpoint->data->status_enable) {
1019 		val |= STATUS_EN_FMASK;
1020 		if (endpoint->toward_ipa) {
1021 			enum ipa_endpoint_name name;
1022 			u32 status_endpoint_id;
1023 
1024 			name = endpoint->data->tx.status_endpoint;
1025 			status_endpoint_id = ipa->name_map[name]->endpoint_id;
1026 
1027 			val |= u32_encode_bits(status_endpoint_id,
1028 					       STATUS_ENDP_FMASK);
1029 		}
1030 		/* STATUS_LOCATION is 0, meaning status element precedes
1031 		 * packet (not present for IPA v4.5)
1032 		 */
1033 		/* STATUS_PKT_SUPPRESS_FMASK is 0 (not present for v3.5.1) */
1034 	}
1035 
1036 	iowrite32(val, ipa->reg_virt + offset);
1037 }
1038 
1039 static int ipa_endpoint_replenish_one(struct ipa_endpoint *endpoint)
1040 {
1041 	struct gsi_trans *trans;
1042 	bool doorbell = false;
1043 	struct page *page;
1044 	u32 buffer_size;
1045 	u32 offset;
1046 	u32 len;
1047 	int ret;
1048 
1049 	buffer_size = endpoint->data->rx.buffer_size;
1050 	page = dev_alloc_pages(get_order(buffer_size));
1051 	if (!page)
1052 		return -ENOMEM;
1053 
1054 	trans = ipa_endpoint_trans_alloc(endpoint, 1);
1055 	if (!trans)
1056 		goto err_free_pages;
1057 
1058 	/* Offset the buffer to make space for skb headroom */
1059 	offset = NET_SKB_PAD;
1060 	len = buffer_size - offset;
1061 
1062 	ret = gsi_trans_page_add(trans, page, len, offset);
1063 	if (ret)
1064 		goto err_trans_free;
1065 	trans->data = page;	/* transaction owns page now */
1066 
1067 	if (++endpoint->replenish_ready == IPA_REPLENISH_BATCH) {
1068 		doorbell = true;
1069 		endpoint->replenish_ready = 0;
1070 	}
1071 
1072 	gsi_trans_commit(trans, doorbell);
1073 
1074 	return 0;
1075 
1076 err_trans_free:
1077 	gsi_trans_free(trans);
1078 err_free_pages:
1079 	__free_pages(page, get_order(buffer_size));
1080 
1081 	return -ENOMEM;
1082 }
1083 
1084 /**
1085  * ipa_endpoint_replenish() - Replenish endpoint receive buffers
1086  * @endpoint:	Endpoint to be replenished
1087  * @add_one:	Whether this is replacing a just-consumed buffer
1088  *
1089  * The IPA hardware can hold a fixed number of receive buffers for an RX
1090  * endpoint, based on the number of entries in the underlying channel ring
1091  * buffer.  If an endpoint's "backlog" is non-zero, it indicates how many
1092  * more receive buffers can be supplied to the hardware.  Replenishing for
1093  * an endpoint can be disabled, in which case requests to replenish a
1094  * buffer are "saved", and transferred to the backlog once it is re-enabled
1095  * again.
1096  */
1097 static void ipa_endpoint_replenish(struct ipa_endpoint *endpoint, bool add_one)
1098 {
1099 	struct gsi *gsi;
1100 	u32 backlog;
1101 	int delta;
1102 
1103 	if (!test_bit(IPA_REPLENISH_ENABLED, endpoint->replenish_flags)) {
1104 		if (add_one)
1105 			atomic_inc(&endpoint->replenish_saved);
1106 		return;
1107 	}
1108 
1109 	/* If already active, just update the backlog */
1110 	if (test_and_set_bit(IPA_REPLENISH_ACTIVE, endpoint->replenish_flags)) {
1111 		if (add_one)
1112 			atomic_inc(&endpoint->replenish_backlog);
1113 		return;
1114 	}
1115 
1116 	while (atomic_dec_not_zero(&endpoint->replenish_backlog))
1117 		if (ipa_endpoint_replenish_one(endpoint))
1118 			goto try_again_later;
1119 
1120 	clear_bit(IPA_REPLENISH_ACTIVE, endpoint->replenish_flags);
1121 
1122 	if (add_one)
1123 		atomic_inc(&endpoint->replenish_backlog);
1124 
1125 	return;
1126 
1127 try_again_later:
1128 	clear_bit(IPA_REPLENISH_ACTIVE, endpoint->replenish_flags);
1129 
1130 	/* The last one didn't succeed, so fix the backlog */
1131 	delta = add_one ? 2 : 1;
1132 	backlog = atomic_add_return(delta, &endpoint->replenish_backlog);
1133 
1134 	/* Whenever a receive buffer transaction completes we'll try to
1135 	 * replenish again.  It's unlikely, but if we fail to supply even
1136 	 * one buffer, nothing will trigger another replenish attempt.
1137 	 * Receive buffer transactions use one TRE, so schedule work to
1138 	 * try replenishing again if our backlog is *all* available TREs.
1139 	 */
1140 	gsi = &endpoint->ipa->gsi;
1141 	if (backlog == gsi_channel_tre_max(gsi, endpoint->channel_id))
1142 		schedule_delayed_work(&endpoint->replenish_work,
1143 				      msecs_to_jiffies(1));
1144 }
1145 
1146 static void ipa_endpoint_replenish_enable(struct ipa_endpoint *endpoint)
1147 {
1148 	struct gsi *gsi = &endpoint->ipa->gsi;
1149 	u32 max_backlog;
1150 	u32 saved;
1151 
1152 	set_bit(IPA_REPLENISH_ENABLED, endpoint->replenish_flags);
1153 	while ((saved = atomic_xchg(&endpoint->replenish_saved, 0)))
1154 		atomic_add(saved, &endpoint->replenish_backlog);
1155 
1156 	/* Start replenishing if hardware currently has no buffers */
1157 	max_backlog = gsi_channel_tre_max(gsi, endpoint->channel_id);
1158 	if (atomic_read(&endpoint->replenish_backlog) == max_backlog)
1159 		ipa_endpoint_replenish(endpoint, false);
1160 }
1161 
1162 static void ipa_endpoint_replenish_disable(struct ipa_endpoint *endpoint)
1163 {
1164 	u32 backlog;
1165 
1166 	clear_bit(IPA_REPLENISH_ENABLED, endpoint->replenish_flags);
1167 	while ((backlog = atomic_xchg(&endpoint->replenish_backlog, 0)))
1168 		atomic_add(backlog, &endpoint->replenish_saved);
1169 }
1170 
1171 static void ipa_endpoint_replenish_work(struct work_struct *work)
1172 {
1173 	struct delayed_work *dwork = to_delayed_work(work);
1174 	struct ipa_endpoint *endpoint;
1175 
1176 	endpoint = container_of(dwork, struct ipa_endpoint, replenish_work);
1177 
1178 	ipa_endpoint_replenish(endpoint, false);
1179 }
1180 
1181 static void ipa_endpoint_skb_copy(struct ipa_endpoint *endpoint,
1182 				  void *data, u32 len, u32 extra)
1183 {
1184 	struct sk_buff *skb;
1185 
1186 	if (!endpoint->netdev)
1187 		return;
1188 
1189 	skb = __dev_alloc_skb(len, GFP_ATOMIC);
1190 	if (!skb)
1191 		return;
1192 
1193 	/* Copy the data into the socket buffer and receive it */
1194 	skb_put(skb, len);
1195 	memcpy(skb->data, data, len);
1196 	skb->truesize += extra;
1197 
1198 	ipa_modem_skb_rx(endpoint->netdev, skb);
1199 }
1200 
1201 static bool ipa_endpoint_skb_build(struct ipa_endpoint *endpoint,
1202 				   struct page *page, u32 len)
1203 {
1204 	u32 buffer_size = endpoint->data->rx.buffer_size;
1205 	struct sk_buff *skb;
1206 
1207 	/* Nothing to do if there's no netdev */
1208 	if (!endpoint->netdev)
1209 		return false;
1210 
1211 	WARN_ON(len > SKB_WITH_OVERHEAD(buffer_size - NET_SKB_PAD));
1212 
1213 	skb = build_skb(page_address(page), buffer_size);
1214 	if (skb) {
1215 		/* Reserve the headroom and account for the data */
1216 		skb_reserve(skb, NET_SKB_PAD);
1217 		skb_put(skb, len);
1218 	}
1219 
1220 	/* Receive the buffer (or record drop if unable to build it) */
1221 	ipa_modem_skb_rx(endpoint->netdev, skb);
1222 
1223 	return skb != NULL;
1224 }
1225 
1226 /* The format of a packet status element is the same for several status
1227  * types (opcodes).  Other types aren't currently supported.
1228  */
1229 static bool ipa_status_format_packet(enum ipa_status_opcode opcode)
1230 {
1231 	switch (opcode) {
1232 	case IPA_STATUS_OPCODE_PACKET:
1233 	case IPA_STATUS_OPCODE_DROPPED_PACKET:
1234 	case IPA_STATUS_OPCODE_SUSPENDED_PACKET:
1235 	case IPA_STATUS_OPCODE_PACKET_2ND_PASS:
1236 		return true;
1237 	default:
1238 		return false;
1239 	}
1240 }
1241 
1242 static bool ipa_endpoint_status_skip(struct ipa_endpoint *endpoint,
1243 				     const struct ipa_status *status)
1244 {
1245 	u32 endpoint_id;
1246 
1247 	if (!ipa_status_format_packet(status->opcode))
1248 		return true;
1249 	if (!status->pkt_len)
1250 		return true;
1251 	endpoint_id = u8_get_bits(status->endp_dst_idx,
1252 				  IPA_STATUS_DST_IDX_FMASK);
1253 	if (endpoint_id != endpoint->endpoint_id)
1254 		return true;
1255 
1256 	return false;	/* Don't skip this packet, process it */
1257 }
1258 
1259 static bool ipa_endpoint_status_tag(struct ipa_endpoint *endpoint,
1260 				    const struct ipa_status *status)
1261 {
1262 	struct ipa_endpoint *command_endpoint;
1263 	struct ipa *ipa = endpoint->ipa;
1264 	u32 endpoint_id;
1265 
1266 	if (!le16_get_bits(status->mask, IPA_STATUS_MASK_TAG_VALID_FMASK))
1267 		return false;	/* No valid tag */
1268 
1269 	/* The status contains a valid tag.  We know the packet was sent to
1270 	 * this endpoint (already verified by ipa_endpoint_status_skip()).
1271 	 * If the packet came from the AP->command TX endpoint we know
1272 	 * this packet was sent as part of the pipeline clear process.
1273 	 */
1274 	endpoint_id = u8_get_bits(status->endp_src_idx,
1275 				  IPA_STATUS_SRC_IDX_FMASK);
1276 	command_endpoint = ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX];
1277 	if (endpoint_id == command_endpoint->endpoint_id) {
1278 		complete(&ipa->completion);
1279 	} else {
1280 		dev_err(&ipa->pdev->dev,
1281 			"unexpected tagged packet from endpoint %u\n",
1282 			endpoint_id);
1283 	}
1284 
1285 	return true;
1286 }
1287 
1288 /* Return whether the status indicates the packet should be dropped */
1289 static bool ipa_endpoint_status_drop(struct ipa_endpoint *endpoint,
1290 				     const struct ipa_status *status)
1291 {
1292 	u32 val;
1293 
1294 	/* If the status indicates a tagged transfer, we'll drop the packet */
1295 	if (ipa_endpoint_status_tag(endpoint, status))
1296 		return true;
1297 
1298 	/* Deaggregation exceptions we drop; all other types we consume */
1299 	if (status->exception)
1300 		return status->exception == IPA_STATUS_EXCEPTION_DEAGGR;
1301 
1302 	/* Drop the packet if it fails to match a routing rule; otherwise no */
1303 	val = le32_get_bits(status->flags1, IPA_STATUS_FLAGS1_RT_RULE_ID_FMASK);
1304 
1305 	return val == field_max(IPA_STATUS_FLAGS1_RT_RULE_ID_FMASK);
1306 }
1307 
1308 static void ipa_endpoint_status_parse(struct ipa_endpoint *endpoint,
1309 				      struct page *page, u32 total_len)
1310 {
1311 	u32 buffer_size = endpoint->data->rx.buffer_size;
1312 	void *data = page_address(page) + NET_SKB_PAD;
1313 	u32 unused = buffer_size - total_len;
1314 	u32 resid = total_len;
1315 
1316 	while (resid) {
1317 		const struct ipa_status *status = data;
1318 		u32 align;
1319 		u32 len;
1320 
1321 		if (resid < sizeof(*status)) {
1322 			dev_err(&endpoint->ipa->pdev->dev,
1323 				"short message (%u bytes < %zu byte status)\n",
1324 				resid, sizeof(*status));
1325 			break;
1326 		}
1327 
1328 		/* Skip over status packets that lack packet data */
1329 		if (ipa_endpoint_status_skip(endpoint, status)) {
1330 			data += sizeof(*status);
1331 			resid -= sizeof(*status);
1332 			continue;
1333 		}
1334 
1335 		/* Compute the amount of buffer space consumed by the packet,
1336 		 * including the status element.  If the hardware is configured
1337 		 * to pad packet data to an aligned boundary, account for that.
1338 		 * And if checksum offload is enabled a trailer containing
1339 		 * computed checksum information will be appended.
1340 		 */
1341 		align = endpoint->data->rx.pad_align ? : 1;
1342 		len = le16_to_cpu(status->pkt_len);
1343 		len = sizeof(*status) + ALIGN(len, align);
1344 		if (endpoint->data->checksum)
1345 			len += sizeof(struct rmnet_map_dl_csum_trailer);
1346 
1347 		if (!ipa_endpoint_status_drop(endpoint, status)) {
1348 			void *data2;
1349 			u32 extra;
1350 			u32 len2;
1351 
1352 			/* Client receives only packet data (no status) */
1353 			data2 = data + sizeof(*status);
1354 			len2 = le16_to_cpu(status->pkt_len);
1355 
1356 			/* Have the true size reflect the extra unused space in
1357 			 * the original receive buffer.  Distribute the "cost"
1358 			 * proportionately across all aggregated packets in the
1359 			 * buffer.
1360 			 */
1361 			extra = DIV_ROUND_CLOSEST(unused * len, total_len);
1362 			ipa_endpoint_skb_copy(endpoint, data2, len2, extra);
1363 		}
1364 
1365 		/* Consume status and the full packet it describes */
1366 		data += len;
1367 		resid -= len;
1368 	}
1369 }
1370 
1371 /* Complete a TX transaction, command or from ipa_endpoint_skb_tx() */
1372 static void ipa_endpoint_tx_complete(struct ipa_endpoint *endpoint,
1373 				     struct gsi_trans *trans)
1374 {
1375 }
1376 
1377 /* Complete transaction initiated in ipa_endpoint_replenish_one() */
1378 static void ipa_endpoint_rx_complete(struct ipa_endpoint *endpoint,
1379 				     struct gsi_trans *trans)
1380 {
1381 	struct page *page;
1382 
1383 	ipa_endpoint_replenish(endpoint, true);
1384 
1385 	if (trans->cancelled)
1386 		return;
1387 
1388 	/* Parse or build a socket buffer using the actual received length */
1389 	page = trans->data;
1390 	if (endpoint->data->status_enable)
1391 		ipa_endpoint_status_parse(endpoint, page, trans->len);
1392 	else if (ipa_endpoint_skb_build(endpoint, page, trans->len))
1393 		trans->data = NULL;	/* Pages have been consumed */
1394 }
1395 
1396 void ipa_endpoint_trans_complete(struct ipa_endpoint *endpoint,
1397 				 struct gsi_trans *trans)
1398 {
1399 	if (endpoint->toward_ipa)
1400 		ipa_endpoint_tx_complete(endpoint, trans);
1401 	else
1402 		ipa_endpoint_rx_complete(endpoint, trans);
1403 }
1404 
1405 void ipa_endpoint_trans_release(struct ipa_endpoint *endpoint,
1406 				struct gsi_trans *trans)
1407 {
1408 	if (endpoint->toward_ipa) {
1409 		struct ipa *ipa = endpoint->ipa;
1410 
1411 		/* Nothing to do for command transactions */
1412 		if (endpoint != ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]) {
1413 			struct sk_buff *skb = trans->data;
1414 
1415 			if (skb)
1416 				dev_kfree_skb_any(skb);
1417 		}
1418 	} else {
1419 		struct page *page = trans->data;
1420 
1421 		if (page) {
1422 			u32 buffer_size = endpoint->data->rx.buffer_size;
1423 
1424 			__free_pages(page, get_order(buffer_size));
1425 		}
1426 	}
1427 }
1428 
1429 void ipa_endpoint_default_route_set(struct ipa *ipa, u32 endpoint_id)
1430 {
1431 	u32 val;
1432 
1433 	/* ROUTE_DIS is 0 */
1434 	val = u32_encode_bits(endpoint_id, ROUTE_DEF_PIPE_FMASK);
1435 	val |= ROUTE_DEF_HDR_TABLE_FMASK;
1436 	val |= u32_encode_bits(0, ROUTE_DEF_HDR_OFST_FMASK);
1437 	val |= u32_encode_bits(endpoint_id, ROUTE_FRAG_DEF_PIPE_FMASK);
1438 	val |= ROUTE_DEF_RETAIN_HDR_FMASK;
1439 
1440 	iowrite32(val, ipa->reg_virt + IPA_REG_ROUTE_OFFSET);
1441 }
1442 
1443 void ipa_endpoint_default_route_clear(struct ipa *ipa)
1444 {
1445 	ipa_endpoint_default_route_set(ipa, 0);
1446 }
1447 
1448 /**
1449  * ipa_endpoint_reset_rx_aggr() - Reset RX endpoint with aggregation active
1450  * @endpoint:	Endpoint to be reset
1451  *
1452  * If aggregation is active on an RX endpoint when a reset is performed
1453  * on its underlying GSI channel, a special sequence of actions must be
1454  * taken to ensure the IPA pipeline is properly cleared.
1455  *
1456  * Return:	0 if successful, or a negative error code
1457  */
1458 static int ipa_endpoint_reset_rx_aggr(struct ipa_endpoint *endpoint)
1459 {
1460 	struct device *dev = &endpoint->ipa->pdev->dev;
1461 	struct ipa *ipa = endpoint->ipa;
1462 	struct gsi *gsi = &ipa->gsi;
1463 	bool suspended = false;
1464 	dma_addr_t addr;
1465 	u32 retries;
1466 	u32 len = 1;
1467 	void *virt;
1468 	int ret;
1469 
1470 	virt = kzalloc(len, GFP_KERNEL);
1471 	if (!virt)
1472 		return -ENOMEM;
1473 
1474 	addr = dma_map_single(dev, virt, len, DMA_FROM_DEVICE);
1475 	if (dma_mapping_error(dev, addr)) {
1476 		ret = -ENOMEM;
1477 		goto out_kfree;
1478 	}
1479 
1480 	/* Force close aggregation before issuing the reset */
1481 	ipa_endpoint_force_close(endpoint);
1482 
1483 	/* Reset and reconfigure the channel with the doorbell engine
1484 	 * disabled.  Then poll until we know aggregation is no longer
1485 	 * active.  We'll re-enable the doorbell (if appropriate) when
1486 	 * we reset again below.
1487 	 */
1488 	gsi_channel_reset(gsi, endpoint->channel_id, false);
1489 
1490 	/* Make sure the channel isn't suspended */
1491 	suspended = ipa_endpoint_program_suspend(endpoint, false);
1492 
1493 	/* Start channel and do a 1 byte read */
1494 	ret = gsi_channel_start(gsi, endpoint->channel_id);
1495 	if (ret)
1496 		goto out_suspend_again;
1497 
1498 	ret = gsi_trans_read_byte(gsi, endpoint->channel_id, addr);
1499 	if (ret)
1500 		goto err_endpoint_stop;
1501 
1502 	/* Wait for aggregation to be closed on the channel */
1503 	retries = IPA_ENDPOINT_RESET_AGGR_RETRY_MAX;
1504 	do {
1505 		if (!ipa_endpoint_aggr_active(endpoint))
1506 			break;
1507 		usleep_range(USEC_PER_MSEC, 2 * USEC_PER_MSEC);
1508 	} while (retries--);
1509 
1510 	/* Check one last time */
1511 	if (ipa_endpoint_aggr_active(endpoint))
1512 		dev_err(dev, "endpoint %u still active during reset\n",
1513 			endpoint->endpoint_id);
1514 
1515 	gsi_trans_read_byte_done(gsi, endpoint->channel_id);
1516 
1517 	ret = gsi_channel_stop(gsi, endpoint->channel_id);
1518 	if (ret)
1519 		goto out_suspend_again;
1520 
1521 	/* Finally, reset and reconfigure the channel again (re-enabling
1522 	 * the doorbell engine if appropriate).  Sleep for 1 millisecond to
1523 	 * complete the channel reset sequence.  Finish by suspending the
1524 	 * channel again (if necessary).
1525 	 */
1526 	gsi_channel_reset(gsi, endpoint->channel_id, true);
1527 
1528 	usleep_range(USEC_PER_MSEC, 2 * USEC_PER_MSEC);
1529 
1530 	goto out_suspend_again;
1531 
1532 err_endpoint_stop:
1533 	(void)gsi_channel_stop(gsi, endpoint->channel_id);
1534 out_suspend_again:
1535 	if (suspended)
1536 		(void)ipa_endpoint_program_suspend(endpoint, true);
1537 	dma_unmap_single(dev, addr, len, DMA_FROM_DEVICE);
1538 out_kfree:
1539 	kfree(virt);
1540 
1541 	return ret;
1542 }
1543 
1544 static void ipa_endpoint_reset(struct ipa_endpoint *endpoint)
1545 {
1546 	u32 channel_id = endpoint->channel_id;
1547 	struct ipa *ipa = endpoint->ipa;
1548 	bool special;
1549 	int ret = 0;
1550 
1551 	/* On IPA v3.5.1, if an RX endpoint is reset while aggregation
1552 	 * is active, we need to handle things specially to recover.
1553 	 * All other cases just need to reset the underlying GSI channel.
1554 	 */
1555 	special = ipa->version < IPA_VERSION_4_0 && !endpoint->toward_ipa &&
1556 			endpoint->data->aggregation;
1557 	if (special && ipa_endpoint_aggr_active(endpoint))
1558 		ret = ipa_endpoint_reset_rx_aggr(endpoint);
1559 	else
1560 		gsi_channel_reset(&ipa->gsi, channel_id, true);
1561 
1562 	if (ret)
1563 		dev_err(&ipa->pdev->dev,
1564 			"error %d resetting channel %u for endpoint %u\n",
1565 			ret, endpoint->channel_id, endpoint->endpoint_id);
1566 }
1567 
1568 static void ipa_endpoint_program(struct ipa_endpoint *endpoint)
1569 {
1570 	if (endpoint->toward_ipa) {
1571 		/* Newer versions of IPA use GSI channel flow control
1572 		 * instead of endpoint DELAY mode to prevent sending data.
1573 		 * Flow control is disabled for newly-allocated channels,
1574 		 * and we can assume flow control is not (ever) enabled
1575 		 * for AP TX channels.
1576 		 */
1577 		if (endpoint->ipa->version < IPA_VERSION_4_2)
1578 			ipa_endpoint_program_delay(endpoint, false);
1579 	} else {
1580 		/* Ensure suspend mode is off on all AP RX endpoints */
1581 		(void)ipa_endpoint_program_suspend(endpoint, false);
1582 	}
1583 	ipa_endpoint_init_cfg(endpoint);
1584 	ipa_endpoint_init_nat(endpoint);
1585 	ipa_endpoint_init_hdr(endpoint);
1586 	ipa_endpoint_init_hdr_ext(endpoint);
1587 	ipa_endpoint_init_hdr_metadata_mask(endpoint);
1588 	ipa_endpoint_init_mode(endpoint);
1589 	ipa_endpoint_init_aggr(endpoint);
1590 	if (!endpoint->toward_ipa)
1591 		ipa_endpoint_init_hol_block_disable(endpoint);
1592 	ipa_endpoint_init_deaggr(endpoint);
1593 	ipa_endpoint_init_rsrc_grp(endpoint);
1594 	ipa_endpoint_init_seq(endpoint);
1595 	ipa_endpoint_status(endpoint);
1596 }
1597 
1598 int ipa_endpoint_enable_one(struct ipa_endpoint *endpoint)
1599 {
1600 	struct ipa *ipa = endpoint->ipa;
1601 	struct gsi *gsi = &ipa->gsi;
1602 	int ret;
1603 
1604 	ret = gsi_channel_start(gsi, endpoint->channel_id);
1605 	if (ret) {
1606 		dev_err(&ipa->pdev->dev,
1607 			"error %d starting %cX channel %u for endpoint %u\n",
1608 			ret, endpoint->toward_ipa ? 'T' : 'R',
1609 			endpoint->channel_id, endpoint->endpoint_id);
1610 		return ret;
1611 	}
1612 
1613 	if (!endpoint->toward_ipa) {
1614 		ipa_interrupt_suspend_enable(ipa->interrupt,
1615 					     endpoint->endpoint_id);
1616 		ipa_endpoint_replenish_enable(endpoint);
1617 	}
1618 
1619 	ipa->enabled |= BIT(endpoint->endpoint_id);
1620 
1621 	return 0;
1622 }
1623 
1624 void ipa_endpoint_disable_one(struct ipa_endpoint *endpoint)
1625 {
1626 	u32 mask = BIT(endpoint->endpoint_id);
1627 	struct ipa *ipa = endpoint->ipa;
1628 	struct gsi *gsi = &ipa->gsi;
1629 	int ret;
1630 
1631 	if (!(ipa->enabled & mask))
1632 		return;
1633 
1634 	ipa->enabled ^= mask;
1635 
1636 	if (!endpoint->toward_ipa) {
1637 		ipa_endpoint_replenish_disable(endpoint);
1638 		ipa_interrupt_suspend_disable(ipa->interrupt,
1639 					      endpoint->endpoint_id);
1640 	}
1641 
1642 	/* Note that if stop fails, the channel's state is not well-defined */
1643 	ret = gsi_channel_stop(gsi, endpoint->channel_id);
1644 	if (ret)
1645 		dev_err(&ipa->pdev->dev,
1646 			"error %d attempting to stop endpoint %u\n", ret,
1647 			endpoint->endpoint_id);
1648 }
1649 
1650 void ipa_endpoint_suspend_one(struct ipa_endpoint *endpoint)
1651 {
1652 	struct device *dev = &endpoint->ipa->pdev->dev;
1653 	struct gsi *gsi = &endpoint->ipa->gsi;
1654 	int ret;
1655 
1656 	if (!(endpoint->ipa->enabled & BIT(endpoint->endpoint_id)))
1657 		return;
1658 
1659 	if (!endpoint->toward_ipa) {
1660 		ipa_endpoint_replenish_disable(endpoint);
1661 		(void)ipa_endpoint_program_suspend(endpoint, true);
1662 	}
1663 
1664 	ret = gsi_channel_suspend(gsi, endpoint->channel_id);
1665 	if (ret)
1666 		dev_err(dev, "error %d suspending channel %u\n", ret,
1667 			endpoint->channel_id);
1668 }
1669 
1670 void ipa_endpoint_resume_one(struct ipa_endpoint *endpoint)
1671 {
1672 	struct device *dev = &endpoint->ipa->pdev->dev;
1673 	struct gsi *gsi = &endpoint->ipa->gsi;
1674 	int ret;
1675 
1676 	if (!(endpoint->ipa->enabled & BIT(endpoint->endpoint_id)))
1677 		return;
1678 
1679 	if (!endpoint->toward_ipa)
1680 		(void)ipa_endpoint_program_suspend(endpoint, false);
1681 
1682 	ret = gsi_channel_resume(gsi, endpoint->channel_id);
1683 	if (ret)
1684 		dev_err(dev, "error %d resuming channel %u\n", ret,
1685 			endpoint->channel_id);
1686 	else if (!endpoint->toward_ipa)
1687 		ipa_endpoint_replenish_enable(endpoint);
1688 }
1689 
1690 void ipa_endpoint_suspend(struct ipa *ipa)
1691 {
1692 	if (!ipa->setup_complete)
1693 		return;
1694 
1695 	if (ipa->modem_netdev)
1696 		ipa_modem_suspend(ipa->modem_netdev);
1697 
1698 	ipa_endpoint_suspend_one(ipa->name_map[IPA_ENDPOINT_AP_LAN_RX]);
1699 	ipa_endpoint_suspend_one(ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]);
1700 }
1701 
1702 void ipa_endpoint_resume(struct ipa *ipa)
1703 {
1704 	if (!ipa->setup_complete)
1705 		return;
1706 
1707 	ipa_endpoint_resume_one(ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]);
1708 	ipa_endpoint_resume_one(ipa->name_map[IPA_ENDPOINT_AP_LAN_RX]);
1709 
1710 	if (ipa->modem_netdev)
1711 		ipa_modem_resume(ipa->modem_netdev);
1712 }
1713 
1714 static void ipa_endpoint_setup_one(struct ipa_endpoint *endpoint)
1715 {
1716 	struct gsi *gsi = &endpoint->ipa->gsi;
1717 	u32 channel_id = endpoint->channel_id;
1718 
1719 	/* Only AP endpoints get set up */
1720 	if (endpoint->ee_id != GSI_EE_AP)
1721 		return;
1722 
1723 	endpoint->trans_tre_max = gsi_channel_trans_tre_max(gsi, channel_id);
1724 	if (!endpoint->toward_ipa) {
1725 		/* RX transactions require a single TRE, so the maximum
1726 		 * backlog is the same as the maximum outstanding TREs.
1727 		 */
1728 		clear_bit(IPA_REPLENISH_ENABLED, endpoint->replenish_flags);
1729 		clear_bit(IPA_REPLENISH_ACTIVE, endpoint->replenish_flags);
1730 		atomic_set(&endpoint->replenish_saved,
1731 			   gsi_channel_tre_max(gsi, endpoint->channel_id));
1732 		atomic_set(&endpoint->replenish_backlog, 0);
1733 		INIT_DELAYED_WORK(&endpoint->replenish_work,
1734 				  ipa_endpoint_replenish_work);
1735 	}
1736 
1737 	ipa_endpoint_program(endpoint);
1738 
1739 	endpoint->ipa->set_up |= BIT(endpoint->endpoint_id);
1740 }
1741 
1742 static void ipa_endpoint_teardown_one(struct ipa_endpoint *endpoint)
1743 {
1744 	endpoint->ipa->set_up &= ~BIT(endpoint->endpoint_id);
1745 
1746 	if (!endpoint->toward_ipa)
1747 		cancel_delayed_work_sync(&endpoint->replenish_work);
1748 
1749 	ipa_endpoint_reset(endpoint);
1750 }
1751 
1752 void ipa_endpoint_setup(struct ipa *ipa)
1753 {
1754 	u32 initialized = ipa->initialized;
1755 
1756 	ipa->set_up = 0;
1757 	while (initialized) {
1758 		u32 endpoint_id = __ffs(initialized);
1759 
1760 		initialized ^= BIT(endpoint_id);
1761 
1762 		ipa_endpoint_setup_one(&ipa->endpoint[endpoint_id]);
1763 	}
1764 }
1765 
1766 void ipa_endpoint_teardown(struct ipa *ipa)
1767 {
1768 	u32 set_up = ipa->set_up;
1769 
1770 	while (set_up) {
1771 		u32 endpoint_id = __fls(set_up);
1772 
1773 		set_up ^= BIT(endpoint_id);
1774 
1775 		ipa_endpoint_teardown_one(&ipa->endpoint[endpoint_id]);
1776 	}
1777 	ipa->set_up = 0;
1778 }
1779 
1780 int ipa_endpoint_config(struct ipa *ipa)
1781 {
1782 	struct device *dev = &ipa->pdev->dev;
1783 	u32 initialized;
1784 	u32 rx_base;
1785 	u32 rx_mask;
1786 	u32 tx_mask;
1787 	int ret = 0;
1788 	u32 max;
1789 	u32 val;
1790 
1791 	/* Prior to IPAv3.5, the FLAVOR_0 register was not supported.
1792 	 * Furthermore, the endpoints were not grouped such that TX
1793 	 * endpoint numbers started with 0 and RX endpoints had numbers
1794 	 * higher than all TX endpoints, so we can't do the simple
1795 	 * direction check used for newer hardware below.
1796 	 *
1797 	 * For hardware that doesn't support the FLAVOR_0 register,
1798 	 * just set the available mask to support any endpoint, and
1799 	 * assume the configuration is valid.
1800 	 */
1801 	if (ipa->version < IPA_VERSION_3_5) {
1802 		ipa->available = ~0;
1803 		return 0;
1804 	}
1805 
1806 	/* Find out about the endpoints supplied by the hardware, and ensure
1807 	 * the highest one doesn't exceed the number we support.
1808 	 */
1809 	val = ioread32(ipa->reg_virt + IPA_REG_FLAVOR_0_OFFSET);
1810 
1811 	/* Our RX is an IPA producer */
1812 	rx_base = u32_get_bits(val, IPA_PROD_LOWEST_FMASK);
1813 	max = rx_base + u32_get_bits(val, IPA_MAX_PROD_PIPES_FMASK);
1814 	if (max > IPA_ENDPOINT_MAX) {
1815 		dev_err(dev, "too many endpoints (%u > %u)\n",
1816 			max, IPA_ENDPOINT_MAX);
1817 		return -EINVAL;
1818 	}
1819 	rx_mask = GENMASK(max - 1, rx_base);
1820 
1821 	/* Our TX is an IPA consumer */
1822 	max = u32_get_bits(val, IPA_MAX_CONS_PIPES_FMASK);
1823 	tx_mask = GENMASK(max - 1, 0);
1824 
1825 	ipa->available = rx_mask | tx_mask;
1826 
1827 	/* Check for initialized endpoints not supported by the hardware */
1828 	if (ipa->initialized & ~ipa->available) {
1829 		dev_err(dev, "unavailable endpoint id(s) 0x%08x\n",
1830 			ipa->initialized & ~ipa->available);
1831 		ret = -EINVAL;		/* Report other errors too */
1832 	}
1833 
1834 	initialized = ipa->initialized;
1835 	while (initialized) {
1836 		u32 endpoint_id = __ffs(initialized);
1837 		struct ipa_endpoint *endpoint;
1838 
1839 		initialized ^= BIT(endpoint_id);
1840 
1841 		/* Make sure it's pointing in the right direction */
1842 		endpoint = &ipa->endpoint[endpoint_id];
1843 		if ((endpoint_id < rx_base) != endpoint->toward_ipa) {
1844 			dev_err(dev, "endpoint id %u wrong direction\n",
1845 				endpoint_id);
1846 			ret = -EINVAL;
1847 		}
1848 	}
1849 
1850 	return ret;
1851 }
1852 
1853 void ipa_endpoint_deconfig(struct ipa *ipa)
1854 {
1855 	ipa->available = 0;	/* Nothing more to do */
1856 }
1857 
1858 static void ipa_endpoint_init_one(struct ipa *ipa, enum ipa_endpoint_name name,
1859 				  const struct ipa_gsi_endpoint_data *data)
1860 {
1861 	struct ipa_endpoint *endpoint;
1862 
1863 	endpoint = &ipa->endpoint[data->endpoint_id];
1864 
1865 	if (data->ee_id == GSI_EE_AP)
1866 		ipa->channel_map[data->channel_id] = endpoint;
1867 	ipa->name_map[name] = endpoint;
1868 
1869 	endpoint->ipa = ipa;
1870 	endpoint->ee_id = data->ee_id;
1871 	endpoint->channel_id = data->channel_id;
1872 	endpoint->endpoint_id = data->endpoint_id;
1873 	endpoint->toward_ipa = data->toward_ipa;
1874 	endpoint->data = &data->endpoint.config;
1875 
1876 	ipa->initialized |= BIT(endpoint->endpoint_id);
1877 }
1878 
1879 static void ipa_endpoint_exit_one(struct ipa_endpoint *endpoint)
1880 {
1881 	endpoint->ipa->initialized &= ~BIT(endpoint->endpoint_id);
1882 
1883 	memset(endpoint, 0, sizeof(*endpoint));
1884 }
1885 
1886 void ipa_endpoint_exit(struct ipa *ipa)
1887 {
1888 	u32 initialized = ipa->initialized;
1889 
1890 	while (initialized) {
1891 		u32 endpoint_id = __fls(initialized);
1892 
1893 		initialized ^= BIT(endpoint_id);
1894 
1895 		ipa_endpoint_exit_one(&ipa->endpoint[endpoint_id]);
1896 	}
1897 	memset(ipa->name_map, 0, sizeof(ipa->name_map));
1898 	memset(ipa->channel_map, 0, sizeof(ipa->channel_map));
1899 }
1900 
1901 /* Returns a bitmask of endpoints that support filtering, or 0 on error */
1902 u32 ipa_endpoint_init(struct ipa *ipa, u32 count,
1903 		      const struct ipa_gsi_endpoint_data *data)
1904 {
1905 	enum ipa_endpoint_name name;
1906 	u32 filter_map;
1907 
1908 	if (!ipa_endpoint_data_valid(ipa, count, data))
1909 		return 0;	/* Error */
1910 
1911 	ipa->initialized = 0;
1912 
1913 	filter_map = 0;
1914 	for (name = 0; name < count; name++, data++) {
1915 		if (ipa_gsi_endpoint_data_empty(data))
1916 			continue;	/* Skip over empty slots */
1917 
1918 		ipa_endpoint_init_one(ipa, name, data);
1919 
1920 		if (data->endpoint.filter_support)
1921 			filter_map |= BIT(data->endpoint_id);
1922 	}
1923 
1924 	if (!ipa_filter_map_valid(ipa, filter_map))
1925 		goto err_endpoint_exit;
1926 
1927 	return filter_map;	/* Non-zero bitmask */
1928 
1929 err_endpoint_exit:
1930 	ipa_endpoint_exit(ipa);
1931 
1932 	return 0;	/* Error */
1933 }
1934