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