xref: /openbmc/linux/drivers/net/ipa/ipa_endpoint.c (revision 8dd3cdea)
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 		return;
1155 
1156 	/* Copy the data into the socket buffer and receive it */
1157 	skb_put(skb, len);
1158 	memcpy(skb->data, data, len);
1159 	skb->truesize += extra;
1160 
1161 	ipa_modem_skb_rx(endpoint->netdev, skb);
1162 }
1163 
1164 static bool ipa_endpoint_skb_build(struct ipa_endpoint *endpoint,
1165 				   struct page *page, u32 len)
1166 {
1167 	u32 buffer_size = endpoint->data->rx.buffer_size;
1168 	struct sk_buff *skb;
1169 
1170 	/* Nothing to do if there's no netdev */
1171 	if (!endpoint->netdev)
1172 		return false;
1173 
1174 	WARN_ON(len > SKB_WITH_OVERHEAD(buffer_size - NET_SKB_PAD));
1175 
1176 	skb = build_skb(page_address(page), buffer_size);
1177 	if (skb) {
1178 		/* Reserve the headroom and account for the data */
1179 		skb_reserve(skb, NET_SKB_PAD);
1180 		skb_put(skb, len);
1181 	}
1182 
1183 	/* Receive the buffer (or record drop if unable to build it) */
1184 	ipa_modem_skb_rx(endpoint->netdev, skb);
1185 
1186 	return skb != NULL;
1187 }
1188 
1189 /* The format of a packet status element is the same for several status
1190  * types (opcodes).  Other types aren't currently supported.
1191  */
1192 static bool ipa_status_format_packet(enum ipa_status_opcode opcode)
1193 {
1194 	switch (opcode) {
1195 	case IPA_STATUS_OPCODE_PACKET:
1196 	case IPA_STATUS_OPCODE_DROPPED_PACKET:
1197 	case IPA_STATUS_OPCODE_SUSPENDED_PACKET:
1198 	case IPA_STATUS_OPCODE_PACKET_2ND_PASS:
1199 		return true;
1200 	default:
1201 		return false;
1202 	}
1203 }
1204 
1205 static bool ipa_endpoint_status_skip(struct ipa_endpoint *endpoint,
1206 				     const struct ipa_status *status)
1207 {
1208 	u32 endpoint_id;
1209 
1210 	if (!ipa_status_format_packet(status->opcode))
1211 		return true;
1212 	if (!status->pkt_len)
1213 		return true;
1214 	endpoint_id = u8_get_bits(status->endp_dst_idx,
1215 				  IPA_STATUS_DST_IDX_FMASK);
1216 	if (endpoint_id != endpoint->endpoint_id)
1217 		return true;
1218 
1219 	return false;	/* Don't skip this packet, process it */
1220 }
1221 
1222 static bool ipa_endpoint_status_tag(struct ipa_endpoint *endpoint,
1223 				    const struct ipa_status *status)
1224 {
1225 	struct ipa_endpoint *command_endpoint;
1226 	struct ipa *ipa = endpoint->ipa;
1227 	u32 endpoint_id;
1228 
1229 	if (!le16_get_bits(status->mask, IPA_STATUS_MASK_TAG_VALID_FMASK))
1230 		return false;	/* No valid tag */
1231 
1232 	/* The status contains a valid tag.  We know the packet was sent to
1233 	 * this endpoint (already verified by ipa_endpoint_status_skip()).
1234 	 * If the packet came from the AP->command TX endpoint we know
1235 	 * this packet was sent as part of the pipeline clear process.
1236 	 */
1237 	endpoint_id = u8_get_bits(status->endp_src_idx,
1238 				  IPA_STATUS_SRC_IDX_FMASK);
1239 	command_endpoint = ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX];
1240 	if (endpoint_id == command_endpoint->endpoint_id) {
1241 		complete(&ipa->completion);
1242 	} else {
1243 		dev_err(&ipa->pdev->dev,
1244 			"unexpected tagged packet from endpoint %u\n",
1245 			endpoint_id);
1246 	}
1247 
1248 	return true;
1249 }
1250 
1251 /* Return whether the status indicates the packet should be dropped */
1252 static bool ipa_endpoint_status_drop(struct ipa_endpoint *endpoint,
1253 				     const struct ipa_status *status)
1254 {
1255 	u32 val;
1256 
1257 	/* If the status indicates a tagged transfer, we'll drop the packet */
1258 	if (ipa_endpoint_status_tag(endpoint, status))
1259 		return true;
1260 
1261 	/* Deaggregation exceptions we drop; all other types we consume */
1262 	if (status->exception)
1263 		return status->exception == IPA_STATUS_EXCEPTION_DEAGGR;
1264 
1265 	/* Drop the packet if it fails to match a routing rule; otherwise no */
1266 	val = le32_get_bits(status->flags1, IPA_STATUS_FLAGS1_RT_RULE_ID_FMASK);
1267 
1268 	return val == field_max(IPA_STATUS_FLAGS1_RT_RULE_ID_FMASK);
1269 }
1270 
1271 static void ipa_endpoint_status_parse(struct ipa_endpoint *endpoint,
1272 				      struct page *page, u32 total_len)
1273 {
1274 	u32 buffer_size = endpoint->data->rx.buffer_size;
1275 	void *data = page_address(page) + NET_SKB_PAD;
1276 	u32 unused = buffer_size - total_len;
1277 	u32 resid = total_len;
1278 
1279 	while (resid) {
1280 		const struct ipa_status *status = data;
1281 		u32 align;
1282 		u32 len;
1283 
1284 		if (resid < sizeof(*status)) {
1285 			dev_err(&endpoint->ipa->pdev->dev,
1286 				"short message (%u bytes < %zu byte status)\n",
1287 				resid, sizeof(*status));
1288 			break;
1289 		}
1290 
1291 		/* Skip over status packets that lack packet data */
1292 		if (ipa_endpoint_status_skip(endpoint, status)) {
1293 			data += sizeof(*status);
1294 			resid -= sizeof(*status);
1295 			continue;
1296 		}
1297 
1298 		/* Compute the amount of buffer space consumed by the packet,
1299 		 * including the status element.  If the hardware is configured
1300 		 * to pad packet data to an aligned boundary, account for that.
1301 		 * And if checksum offload is enabled a trailer containing
1302 		 * computed checksum information will be appended.
1303 		 */
1304 		align = endpoint->data->rx.pad_align ? : 1;
1305 		len = le16_to_cpu(status->pkt_len);
1306 		len = sizeof(*status) + ALIGN(len, align);
1307 		if (endpoint->data->checksum)
1308 			len += sizeof(struct rmnet_map_dl_csum_trailer);
1309 
1310 		if (!ipa_endpoint_status_drop(endpoint, status)) {
1311 			void *data2;
1312 			u32 extra;
1313 			u32 len2;
1314 
1315 			/* Client receives only packet data (no status) */
1316 			data2 = data + sizeof(*status);
1317 			len2 = le16_to_cpu(status->pkt_len);
1318 
1319 			/* Have the true size reflect the extra unused space in
1320 			 * the original receive buffer.  Distribute the "cost"
1321 			 * proportionately across all aggregated packets in the
1322 			 * buffer.
1323 			 */
1324 			extra = DIV_ROUND_CLOSEST(unused * len, total_len);
1325 			ipa_endpoint_skb_copy(endpoint, data2, len2, extra);
1326 		}
1327 
1328 		/* Consume status and the full packet it describes */
1329 		data += len;
1330 		resid -= len;
1331 	}
1332 }
1333 
1334 /* Complete a TX transaction, command or from ipa_endpoint_skb_tx() */
1335 static void ipa_endpoint_tx_complete(struct ipa_endpoint *endpoint,
1336 				     struct gsi_trans *trans)
1337 {
1338 }
1339 
1340 /* Complete transaction initiated in ipa_endpoint_replenish_one() */
1341 static void ipa_endpoint_rx_complete(struct ipa_endpoint *endpoint,
1342 				     struct gsi_trans *trans)
1343 {
1344 	struct page *page;
1345 
1346 	if (trans->cancelled)
1347 		goto done;
1348 
1349 	/* Parse or build a socket buffer using the actual received length */
1350 	page = trans->data;
1351 	if (endpoint->data->status_enable)
1352 		ipa_endpoint_status_parse(endpoint, page, trans->len);
1353 	else if (ipa_endpoint_skb_build(endpoint, page, trans->len))
1354 		trans->data = NULL;	/* Pages have been consumed */
1355 done:
1356 	ipa_endpoint_replenish(endpoint);
1357 }
1358 
1359 void ipa_endpoint_trans_complete(struct ipa_endpoint *endpoint,
1360 				 struct gsi_trans *trans)
1361 {
1362 	if (endpoint->toward_ipa)
1363 		ipa_endpoint_tx_complete(endpoint, trans);
1364 	else
1365 		ipa_endpoint_rx_complete(endpoint, trans);
1366 }
1367 
1368 void ipa_endpoint_trans_release(struct ipa_endpoint *endpoint,
1369 				struct gsi_trans *trans)
1370 {
1371 	if (endpoint->toward_ipa) {
1372 		struct ipa *ipa = endpoint->ipa;
1373 
1374 		/* Nothing to do for command transactions */
1375 		if (endpoint != ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]) {
1376 			struct sk_buff *skb = trans->data;
1377 
1378 			if (skb)
1379 				dev_kfree_skb_any(skb);
1380 		}
1381 	} else {
1382 		struct page *page = trans->data;
1383 
1384 		if (page) {
1385 			u32 buffer_size = endpoint->data->rx.buffer_size;
1386 
1387 			__free_pages(page, get_order(buffer_size));
1388 		}
1389 	}
1390 }
1391 
1392 void ipa_endpoint_default_route_set(struct ipa *ipa, u32 endpoint_id)
1393 {
1394 	u32 val;
1395 
1396 	/* ROUTE_DIS is 0 */
1397 	val = u32_encode_bits(endpoint_id, ROUTE_DEF_PIPE_FMASK);
1398 	val |= ROUTE_DEF_HDR_TABLE_FMASK;
1399 	val |= u32_encode_bits(0, ROUTE_DEF_HDR_OFST_FMASK);
1400 	val |= u32_encode_bits(endpoint_id, ROUTE_FRAG_DEF_PIPE_FMASK);
1401 	val |= ROUTE_DEF_RETAIN_HDR_FMASK;
1402 
1403 	iowrite32(val, ipa->reg_virt + IPA_REG_ROUTE_OFFSET);
1404 }
1405 
1406 void ipa_endpoint_default_route_clear(struct ipa *ipa)
1407 {
1408 	ipa_endpoint_default_route_set(ipa, 0);
1409 }
1410 
1411 /**
1412  * ipa_endpoint_reset_rx_aggr() - Reset RX endpoint with aggregation active
1413  * @endpoint:	Endpoint to be reset
1414  *
1415  * If aggregation is active on an RX endpoint when a reset is performed
1416  * on its underlying GSI channel, a special sequence of actions must be
1417  * taken to ensure the IPA pipeline is properly cleared.
1418  *
1419  * Return:	0 if successful, or a negative error code
1420  */
1421 static int ipa_endpoint_reset_rx_aggr(struct ipa_endpoint *endpoint)
1422 {
1423 	struct device *dev = &endpoint->ipa->pdev->dev;
1424 	struct ipa *ipa = endpoint->ipa;
1425 	struct gsi *gsi = &ipa->gsi;
1426 	bool suspended = false;
1427 	dma_addr_t addr;
1428 	u32 retries;
1429 	u32 len = 1;
1430 	void *virt;
1431 	int ret;
1432 
1433 	virt = kzalloc(len, GFP_KERNEL);
1434 	if (!virt)
1435 		return -ENOMEM;
1436 
1437 	addr = dma_map_single(dev, virt, len, DMA_FROM_DEVICE);
1438 	if (dma_mapping_error(dev, addr)) {
1439 		ret = -ENOMEM;
1440 		goto out_kfree;
1441 	}
1442 
1443 	/* Force close aggregation before issuing the reset */
1444 	ipa_endpoint_force_close(endpoint);
1445 
1446 	/* Reset and reconfigure the channel with the doorbell engine
1447 	 * disabled.  Then poll until we know aggregation is no longer
1448 	 * active.  We'll re-enable the doorbell (if appropriate) when
1449 	 * we reset again below.
1450 	 */
1451 	gsi_channel_reset(gsi, endpoint->channel_id, false);
1452 
1453 	/* Make sure the channel isn't suspended */
1454 	suspended = ipa_endpoint_program_suspend(endpoint, false);
1455 
1456 	/* Start channel and do a 1 byte read */
1457 	ret = gsi_channel_start(gsi, endpoint->channel_id);
1458 	if (ret)
1459 		goto out_suspend_again;
1460 
1461 	ret = gsi_trans_read_byte(gsi, endpoint->channel_id, addr);
1462 	if (ret)
1463 		goto err_endpoint_stop;
1464 
1465 	/* Wait for aggregation to be closed on the channel */
1466 	retries = IPA_ENDPOINT_RESET_AGGR_RETRY_MAX;
1467 	do {
1468 		if (!ipa_endpoint_aggr_active(endpoint))
1469 			break;
1470 		usleep_range(USEC_PER_MSEC, 2 * USEC_PER_MSEC);
1471 	} while (retries--);
1472 
1473 	/* Check one last time */
1474 	if (ipa_endpoint_aggr_active(endpoint))
1475 		dev_err(dev, "endpoint %u still active during reset\n",
1476 			endpoint->endpoint_id);
1477 
1478 	gsi_trans_read_byte_done(gsi, endpoint->channel_id);
1479 
1480 	ret = gsi_channel_stop(gsi, endpoint->channel_id);
1481 	if (ret)
1482 		goto out_suspend_again;
1483 
1484 	/* Finally, reset and reconfigure the channel again (re-enabling
1485 	 * the doorbell engine if appropriate).  Sleep for 1 millisecond to
1486 	 * complete the channel reset sequence.  Finish by suspending the
1487 	 * channel again (if necessary).
1488 	 */
1489 	gsi_channel_reset(gsi, endpoint->channel_id, true);
1490 
1491 	usleep_range(USEC_PER_MSEC, 2 * USEC_PER_MSEC);
1492 
1493 	goto out_suspend_again;
1494 
1495 err_endpoint_stop:
1496 	(void)gsi_channel_stop(gsi, endpoint->channel_id);
1497 out_suspend_again:
1498 	if (suspended)
1499 		(void)ipa_endpoint_program_suspend(endpoint, true);
1500 	dma_unmap_single(dev, addr, len, DMA_FROM_DEVICE);
1501 out_kfree:
1502 	kfree(virt);
1503 
1504 	return ret;
1505 }
1506 
1507 static void ipa_endpoint_reset(struct ipa_endpoint *endpoint)
1508 {
1509 	u32 channel_id = endpoint->channel_id;
1510 	struct ipa *ipa = endpoint->ipa;
1511 	bool special;
1512 	int ret = 0;
1513 
1514 	/* On IPA v3.5.1, if an RX endpoint is reset while aggregation
1515 	 * is active, we need to handle things specially to recover.
1516 	 * All other cases just need to reset the underlying GSI channel.
1517 	 */
1518 	special = ipa->version < IPA_VERSION_4_0 && !endpoint->toward_ipa &&
1519 			endpoint->data->aggregation;
1520 	if (special && ipa_endpoint_aggr_active(endpoint))
1521 		ret = ipa_endpoint_reset_rx_aggr(endpoint);
1522 	else
1523 		gsi_channel_reset(&ipa->gsi, channel_id, true);
1524 
1525 	if (ret)
1526 		dev_err(&ipa->pdev->dev,
1527 			"error %d resetting channel %u for endpoint %u\n",
1528 			ret, endpoint->channel_id, endpoint->endpoint_id);
1529 }
1530 
1531 static void ipa_endpoint_program(struct ipa_endpoint *endpoint)
1532 {
1533 	if (endpoint->toward_ipa) {
1534 		/* Newer versions of IPA use GSI channel flow control
1535 		 * instead of endpoint DELAY mode to prevent sending data.
1536 		 * Flow control is disabled for newly-allocated channels,
1537 		 * and we can assume flow control is not (ever) enabled
1538 		 * for AP TX channels.
1539 		 */
1540 		if (endpoint->ipa->version < IPA_VERSION_4_2)
1541 			ipa_endpoint_program_delay(endpoint, false);
1542 	} else {
1543 		/* Ensure suspend mode is off on all AP RX endpoints */
1544 		(void)ipa_endpoint_program_suspend(endpoint, false);
1545 	}
1546 	ipa_endpoint_init_cfg(endpoint);
1547 	ipa_endpoint_init_nat(endpoint);
1548 	ipa_endpoint_init_hdr(endpoint);
1549 	ipa_endpoint_init_hdr_ext(endpoint);
1550 	ipa_endpoint_init_hdr_metadata_mask(endpoint);
1551 	ipa_endpoint_init_mode(endpoint);
1552 	ipa_endpoint_init_aggr(endpoint);
1553 	if (!endpoint->toward_ipa)
1554 		ipa_endpoint_init_hol_block_disable(endpoint);
1555 	ipa_endpoint_init_deaggr(endpoint);
1556 	ipa_endpoint_init_rsrc_grp(endpoint);
1557 	ipa_endpoint_init_seq(endpoint);
1558 	ipa_endpoint_status(endpoint);
1559 }
1560 
1561 int ipa_endpoint_enable_one(struct ipa_endpoint *endpoint)
1562 {
1563 	struct ipa *ipa = endpoint->ipa;
1564 	struct gsi *gsi = &ipa->gsi;
1565 	int ret;
1566 
1567 	ret = gsi_channel_start(gsi, endpoint->channel_id);
1568 	if (ret) {
1569 		dev_err(&ipa->pdev->dev,
1570 			"error %d starting %cX channel %u for endpoint %u\n",
1571 			ret, endpoint->toward_ipa ? 'T' : 'R',
1572 			endpoint->channel_id, endpoint->endpoint_id);
1573 		return ret;
1574 	}
1575 
1576 	if (!endpoint->toward_ipa) {
1577 		ipa_interrupt_suspend_enable(ipa->interrupt,
1578 					     endpoint->endpoint_id);
1579 		ipa_endpoint_replenish_enable(endpoint);
1580 	}
1581 
1582 	ipa->enabled |= BIT(endpoint->endpoint_id);
1583 
1584 	return 0;
1585 }
1586 
1587 void ipa_endpoint_disable_one(struct ipa_endpoint *endpoint)
1588 {
1589 	u32 mask = BIT(endpoint->endpoint_id);
1590 	struct ipa *ipa = endpoint->ipa;
1591 	struct gsi *gsi = &ipa->gsi;
1592 	int ret;
1593 
1594 	if (!(ipa->enabled & mask))
1595 		return;
1596 
1597 	ipa->enabled ^= mask;
1598 
1599 	if (!endpoint->toward_ipa) {
1600 		ipa_endpoint_replenish_disable(endpoint);
1601 		ipa_interrupt_suspend_disable(ipa->interrupt,
1602 					      endpoint->endpoint_id);
1603 	}
1604 
1605 	/* Note that if stop fails, the channel's state is not well-defined */
1606 	ret = gsi_channel_stop(gsi, endpoint->channel_id);
1607 	if (ret)
1608 		dev_err(&ipa->pdev->dev,
1609 			"error %d attempting to stop endpoint %u\n", ret,
1610 			endpoint->endpoint_id);
1611 }
1612 
1613 void ipa_endpoint_suspend_one(struct ipa_endpoint *endpoint)
1614 {
1615 	struct device *dev = &endpoint->ipa->pdev->dev;
1616 	struct gsi *gsi = &endpoint->ipa->gsi;
1617 	int ret;
1618 
1619 	if (!(endpoint->ipa->enabled & BIT(endpoint->endpoint_id)))
1620 		return;
1621 
1622 	if (!endpoint->toward_ipa) {
1623 		ipa_endpoint_replenish_disable(endpoint);
1624 		(void)ipa_endpoint_program_suspend(endpoint, true);
1625 	}
1626 
1627 	ret = gsi_channel_suspend(gsi, endpoint->channel_id);
1628 	if (ret)
1629 		dev_err(dev, "error %d suspending channel %u\n", ret,
1630 			endpoint->channel_id);
1631 }
1632 
1633 void ipa_endpoint_resume_one(struct ipa_endpoint *endpoint)
1634 {
1635 	struct device *dev = &endpoint->ipa->pdev->dev;
1636 	struct gsi *gsi = &endpoint->ipa->gsi;
1637 	int ret;
1638 
1639 	if (!(endpoint->ipa->enabled & BIT(endpoint->endpoint_id)))
1640 		return;
1641 
1642 	if (!endpoint->toward_ipa)
1643 		(void)ipa_endpoint_program_suspend(endpoint, false);
1644 
1645 	ret = gsi_channel_resume(gsi, endpoint->channel_id);
1646 	if (ret)
1647 		dev_err(dev, "error %d resuming channel %u\n", ret,
1648 			endpoint->channel_id);
1649 	else if (!endpoint->toward_ipa)
1650 		ipa_endpoint_replenish_enable(endpoint);
1651 }
1652 
1653 void ipa_endpoint_suspend(struct ipa *ipa)
1654 {
1655 	if (!ipa->setup_complete)
1656 		return;
1657 
1658 	if (ipa->modem_netdev)
1659 		ipa_modem_suspend(ipa->modem_netdev);
1660 
1661 	ipa_endpoint_suspend_one(ipa->name_map[IPA_ENDPOINT_AP_LAN_RX]);
1662 	ipa_endpoint_suspend_one(ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]);
1663 }
1664 
1665 void ipa_endpoint_resume(struct ipa *ipa)
1666 {
1667 	if (!ipa->setup_complete)
1668 		return;
1669 
1670 	ipa_endpoint_resume_one(ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]);
1671 	ipa_endpoint_resume_one(ipa->name_map[IPA_ENDPOINT_AP_LAN_RX]);
1672 
1673 	if (ipa->modem_netdev)
1674 		ipa_modem_resume(ipa->modem_netdev);
1675 }
1676 
1677 static void ipa_endpoint_setup_one(struct ipa_endpoint *endpoint)
1678 {
1679 	struct gsi *gsi = &endpoint->ipa->gsi;
1680 	u32 channel_id = endpoint->channel_id;
1681 
1682 	/* Only AP endpoints get set up */
1683 	if (endpoint->ee_id != GSI_EE_AP)
1684 		return;
1685 
1686 	endpoint->trans_tre_max = gsi_channel_trans_tre_max(gsi, channel_id);
1687 	if (!endpoint->toward_ipa) {
1688 		/* RX transactions require a single TRE, so the maximum
1689 		 * backlog is the same as the maximum outstanding TREs.
1690 		 */
1691 		clear_bit(IPA_REPLENISH_ENABLED, endpoint->replenish_flags);
1692 		clear_bit(IPA_REPLENISH_ACTIVE, endpoint->replenish_flags);
1693 		INIT_DELAYED_WORK(&endpoint->replenish_work,
1694 				  ipa_endpoint_replenish_work);
1695 	}
1696 
1697 	ipa_endpoint_program(endpoint);
1698 
1699 	endpoint->ipa->set_up |= BIT(endpoint->endpoint_id);
1700 }
1701 
1702 static void ipa_endpoint_teardown_one(struct ipa_endpoint *endpoint)
1703 {
1704 	endpoint->ipa->set_up &= ~BIT(endpoint->endpoint_id);
1705 
1706 	if (!endpoint->toward_ipa)
1707 		cancel_delayed_work_sync(&endpoint->replenish_work);
1708 
1709 	ipa_endpoint_reset(endpoint);
1710 }
1711 
1712 void ipa_endpoint_setup(struct ipa *ipa)
1713 {
1714 	u32 initialized = ipa->initialized;
1715 
1716 	ipa->set_up = 0;
1717 	while (initialized) {
1718 		u32 endpoint_id = __ffs(initialized);
1719 
1720 		initialized ^= BIT(endpoint_id);
1721 
1722 		ipa_endpoint_setup_one(&ipa->endpoint[endpoint_id]);
1723 	}
1724 }
1725 
1726 void ipa_endpoint_teardown(struct ipa *ipa)
1727 {
1728 	u32 set_up = ipa->set_up;
1729 
1730 	while (set_up) {
1731 		u32 endpoint_id = __fls(set_up);
1732 
1733 		set_up ^= BIT(endpoint_id);
1734 
1735 		ipa_endpoint_teardown_one(&ipa->endpoint[endpoint_id]);
1736 	}
1737 	ipa->set_up = 0;
1738 }
1739 
1740 int ipa_endpoint_config(struct ipa *ipa)
1741 {
1742 	struct device *dev = &ipa->pdev->dev;
1743 	u32 initialized;
1744 	u32 rx_base;
1745 	u32 rx_mask;
1746 	u32 tx_mask;
1747 	int ret = 0;
1748 	u32 max;
1749 	u32 val;
1750 
1751 	/* Prior to IPAv3.5, the FLAVOR_0 register was not supported.
1752 	 * Furthermore, the endpoints were not grouped such that TX
1753 	 * endpoint numbers started with 0 and RX endpoints had numbers
1754 	 * higher than all TX endpoints, so we can't do the simple
1755 	 * direction check used for newer hardware below.
1756 	 *
1757 	 * For hardware that doesn't support the FLAVOR_0 register,
1758 	 * just set the available mask to support any endpoint, and
1759 	 * assume the configuration is valid.
1760 	 */
1761 	if (ipa->version < IPA_VERSION_3_5) {
1762 		ipa->available = ~0;
1763 		return 0;
1764 	}
1765 
1766 	/* Find out about the endpoints supplied by the hardware, and ensure
1767 	 * the highest one doesn't exceed the number we support.
1768 	 */
1769 	val = ioread32(ipa->reg_virt + IPA_REG_FLAVOR_0_OFFSET);
1770 
1771 	/* Our RX is an IPA producer */
1772 	rx_base = u32_get_bits(val, IPA_PROD_LOWEST_FMASK);
1773 	max = rx_base + u32_get_bits(val, IPA_MAX_PROD_PIPES_FMASK);
1774 	if (max > IPA_ENDPOINT_MAX) {
1775 		dev_err(dev, "too many endpoints (%u > %u)\n",
1776 			max, IPA_ENDPOINT_MAX);
1777 		return -EINVAL;
1778 	}
1779 	rx_mask = GENMASK(max - 1, rx_base);
1780 
1781 	/* Our TX is an IPA consumer */
1782 	max = u32_get_bits(val, IPA_MAX_CONS_PIPES_FMASK);
1783 	tx_mask = GENMASK(max - 1, 0);
1784 
1785 	ipa->available = rx_mask | tx_mask;
1786 
1787 	/* Check for initialized endpoints not supported by the hardware */
1788 	if (ipa->initialized & ~ipa->available) {
1789 		dev_err(dev, "unavailable endpoint id(s) 0x%08x\n",
1790 			ipa->initialized & ~ipa->available);
1791 		ret = -EINVAL;		/* Report other errors too */
1792 	}
1793 
1794 	initialized = ipa->initialized;
1795 	while (initialized) {
1796 		u32 endpoint_id = __ffs(initialized);
1797 		struct ipa_endpoint *endpoint;
1798 
1799 		initialized ^= BIT(endpoint_id);
1800 
1801 		/* Make sure it's pointing in the right direction */
1802 		endpoint = &ipa->endpoint[endpoint_id];
1803 		if ((endpoint_id < rx_base) != endpoint->toward_ipa) {
1804 			dev_err(dev, "endpoint id %u wrong direction\n",
1805 				endpoint_id);
1806 			ret = -EINVAL;
1807 		}
1808 	}
1809 
1810 	return ret;
1811 }
1812 
1813 void ipa_endpoint_deconfig(struct ipa *ipa)
1814 {
1815 	ipa->available = 0;	/* Nothing more to do */
1816 }
1817 
1818 static void ipa_endpoint_init_one(struct ipa *ipa, enum ipa_endpoint_name name,
1819 				  const struct ipa_gsi_endpoint_data *data)
1820 {
1821 	struct ipa_endpoint *endpoint;
1822 
1823 	endpoint = &ipa->endpoint[data->endpoint_id];
1824 
1825 	if (data->ee_id == GSI_EE_AP)
1826 		ipa->channel_map[data->channel_id] = endpoint;
1827 	ipa->name_map[name] = endpoint;
1828 
1829 	endpoint->ipa = ipa;
1830 	endpoint->ee_id = data->ee_id;
1831 	endpoint->channel_id = data->channel_id;
1832 	endpoint->endpoint_id = data->endpoint_id;
1833 	endpoint->toward_ipa = data->toward_ipa;
1834 	endpoint->data = &data->endpoint.config;
1835 
1836 	ipa->initialized |= BIT(endpoint->endpoint_id);
1837 }
1838 
1839 static void ipa_endpoint_exit_one(struct ipa_endpoint *endpoint)
1840 {
1841 	endpoint->ipa->initialized &= ~BIT(endpoint->endpoint_id);
1842 
1843 	memset(endpoint, 0, sizeof(*endpoint));
1844 }
1845 
1846 void ipa_endpoint_exit(struct ipa *ipa)
1847 {
1848 	u32 initialized = ipa->initialized;
1849 
1850 	while (initialized) {
1851 		u32 endpoint_id = __fls(initialized);
1852 
1853 		initialized ^= BIT(endpoint_id);
1854 
1855 		ipa_endpoint_exit_one(&ipa->endpoint[endpoint_id]);
1856 	}
1857 	memset(ipa->name_map, 0, sizeof(ipa->name_map));
1858 	memset(ipa->channel_map, 0, sizeof(ipa->channel_map));
1859 }
1860 
1861 /* Returns a bitmask of endpoints that support filtering, or 0 on error */
1862 u32 ipa_endpoint_init(struct ipa *ipa, u32 count,
1863 		      const struct ipa_gsi_endpoint_data *data)
1864 {
1865 	enum ipa_endpoint_name name;
1866 	u32 filter_map;
1867 
1868 	BUILD_BUG_ON(!IPA_REPLENISH_BATCH);
1869 
1870 	if (!ipa_endpoint_data_valid(ipa, count, data))
1871 		return 0;	/* Error */
1872 
1873 	ipa->initialized = 0;
1874 
1875 	filter_map = 0;
1876 	for (name = 0; name < count; name++, data++) {
1877 		if (ipa_gsi_endpoint_data_empty(data))
1878 			continue;	/* Skip over empty slots */
1879 
1880 		ipa_endpoint_init_one(ipa, name, data);
1881 
1882 		if (data->endpoint.filter_support)
1883 			filter_map |= BIT(data->endpoint_id);
1884 	}
1885 
1886 	if (!ipa_filter_map_valid(ipa, filter_map))
1887 		goto err_endpoint_exit;
1888 
1889 	return filter_map;	/* Non-zero bitmask */
1890 
1891 err_endpoint_exit:
1892 	ipa_endpoint_exit(ipa);
1893 
1894 	return 0;	/* Error */
1895 }
1896