1 // SPDX-License-Identifier: GPL-2.0
3 /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
4 * Copyright (C) 2019-2020 Linaro Ltd.
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>
15 #include "gsi_trans.h"
18 #include "ipa_endpoint.h"
21 #include "ipa_modem.h"
22 #include "ipa_table.h"
25 #define atomic_dec_not_zero(v) atomic_add_unless((v), -1, 0)
27 #define IPA_REPLENISH_BATCH 16
29 /* RX buffer is 1 page (or a power-of-2 contiguous pages) */
30 #define IPA_RX_BUFFER_SIZE 8192 /* PAGE_SIZE > 4096 wastes a LOT */
32 /* The amount of RX buffer space consumed by standard skb overhead */
33 #define IPA_RX_BUFFER_OVERHEAD (PAGE_SIZE - SKB_MAX_ORDER(NET_SKB_PAD, 0))
35 /* Where to find the QMAP mux_id for a packet within modem-supplied metadata */
36 #define IPA_ENDPOINT_QMAP_METADATA_MASK 0x000000ff /* host byte order */
38 #define IPA_ENDPOINT_RESET_AGGR_RETRY_MAX 3
39 #define IPA_AGGR_TIME_LIMIT_DEFAULT 1000 /* microseconds */
41 /** enum ipa_status_opcode - status element opcode hardware values */
42 enum ipa_status_opcode {
43 IPA_STATUS_OPCODE_PACKET = 0x01,
44 IPA_STATUS_OPCODE_NEW_FRAG_RULE = 0x02,
45 IPA_STATUS_OPCODE_DROPPED_PACKET = 0x04,
46 IPA_STATUS_OPCODE_SUSPENDED_PACKET = 0x08,
47 IPA_STATUS_OPCODE_LOG = 0x10,
48 IPA_STATUS_OPCODE_DCMP = 0x20,
49 IPA_STATUS_OPCODE_PACKET_2ND_PASS = 0x40,
52 /** enum ipa_status_exception - status element exception type */
53 enum ipa_status_exception {
54 /* 0 means no exception */
55 IPA_STATUS_EXCEPTION_DEAGGR = 0x01,
56 IPA_STATUS_EXCEPTION_IPTYPE = 0x04,
57 IPA_STATUS_EXCEPTION_PACKET_LENGTH = 0x08,
58 IPA_STATUS_EXCEPTION_FRAG_RULE_MISS = 0x10,
59 IPA_STATUS_EXCEPTION_SW_FILT = 0x20,
60 /* The meaning of the next value depends on whether the IP version */
61 IPA_STATUS_EXCEPTION_NAT = 0x40, /* IPv4 */
62 IPA_STATUS_EXCEPTION_IPV6CT = IPA_STATUS_EXCEPTION_NAT,
65 /* Status element provided by hardware */
67 u8 opcode; /* enum ipa_status_opcode */
68 u8 exception; /* enum ipa_status_exception */
80 /* Field masks for struct ipa_status structure fields */
82 #define IPA_STATUS_SRC_IDX_FMASK GENMASK(4, 0)
84 #define IPA_STATUS_DST_IDX_FMASK GENMASK(4, 0)
86 #define IPA_STATUS_FLAGS1_FLT_LOCAL_FMASK GENMASK(0, 0)
87 #define IPA_STATUS_FLAGS1_FLT_HASH_FMASK GENMASK(1, 1)
88 #define IPA_STATUS_FLAGS1_FLT_GLOBAL_FMASK GENMASK(2, 2)
89 #define IPA_STATUS_FLAGS1_FLT_RET_HDR_FMASK GENMASK(3, 3)
90 #define IPA_STATUS_FLAGS1_FLT_RULE_ID_FMASK GENMASK(13, 4)
91 #define IPA_STATUS_FLAGS1_RT_LOCAL_FMASK GENMASK(14, 14)
92 #define IPA_STATUS_FLAGS1_RT_HASH_FMASK GENMASK(15, 15)
93 #define IPA_STATUS_FLAGS1_UCP_FMASK GENMASK(16, 16)
94 #define IPA_STATUS_FLAGS1_RT_TBL_IDX_FMASK GENMASK(21, 17)
95 #define IPA_STATUS_FLAGS1_RT_RULE_ID_FMASK GENMASK(31, 22)
97 #define IPA_STATUS_FLAGS2_NAT_HIT_FMASK GENMASK_ULL(0, 0)
98 #define IPA_STATUS_FLAGS2_NAT_ENTRY_IDX_FMASK GENMASK_ULL(13, 1)
99 #define IPA_STATUS_FLAGS2_NAT_TYPE_FMASK GENMASK_ULL(15, 14)
100 #define IPA_STATUS_FLAGS2_TAG_INFO_FMASK GENMASK_ULL(63, 16)
102 #define IPA_STATUS_FLAGS3_SEQ_NUM_FMASK GENMASK(7, 0)
103 #define IPA_STATUS_FLAGS3_TOD_CTR_FMASK GENMASK(31, 8)
105 #define IPA_STATUS_FLAGS4_HDR_LOCAL_FMASK GENMASK(0, 0)
106 #define IPA_STATUS_FLAGS4_HDR_OFFSET_FMASK GENMASK(10, 1)
107 #define IPA_STATUS_FLAGS4_FRAG_HIT_FMASK GENMASK(11, 11)
108 #define IPA_STATUS_FLAGS4_FRAG_RULE_FMASK GENMASK(15, 12)
109 #define IPA_STATUS_FLAGS4_HW_SPECIFIC_FMASK GENMASK(31, 16)
113 static void ipa_endpoint_validate_build(void)
115 /* The aggregation byte limit defines the point at which an
116 * aggregation window will close. It is programmed into the
117 * IPA hardware as a number of KB. We don't use "hard byte
118 * limit" aggregation, which means that we need to supply
119 * enough space in a receive buffer to hold a complete MTU
120 * plus normal skb overhead *after* that aggregation byte
121 * limit has been crossed.
123 * This check just ensures we don't define a receive buffer
124 * size that would exceed what we can represent in the field
125 * that is used to program its size.
127 BUILD_BUG_ON(IPA_RX_BUFFER_SIZE >
128 field_max(AGGR_BYTE_LIMIT_FMASK) * SZ_1K +
129 IPA_MTU + IPA_RX_BUFFER_OVERHEAD);
131 /* I honestly don't know where this requirement comes from. But
132 * it holds, and if we someday need to loosen the constraint we
133 * can try to track it down.
135 BUILD_BUG_ON(sizeof(struct ipa_status) % 4);
138 static bool ipa_endpoint_data_valid_one(struct ipa *ipa, u32 count,
139 const struct ipa_gsi_endpoint_data *all_data,
140 const struct ipa_gsi_endpoint_data *data)
142 const struct ipa_gsi_endpoint_data *other_data;
143 struct device *dev = &ipa->pdev->dev;
144 enum ipa_endpoint_name other_name;
146 if (ipa_gsi_endpoint_data_empty(data))
149 if (!data->toward_ipa) {
150 if (data->endpoint.filter_support) {
151 dev_err(dev, "filtering not supported for "
157 return true; /* Nothing more to check for RX */
160 if (data->endpoint.config.status_enable) {
161 other_name = data->endpoint.config.tx.status_endpoint;
162 if (other_name >= count) {
163 dev_err(dev, "status endpoint name %u out of range "
165 other_name, data->endpoint_id);
169 /* Status endpoint must be defined... */
170 other_data = &all_data[other_name];
171 if (ipa_gsi_endpoint_data_empty(other_data)) {
172 dev_err(dev, "DMA endpoint name %u undefined "
174 other_name, data->endpoint_id);
178 /* ...and has to be an RX endpoint... */
179 if (other_data->toward_ipa) {
181 "status endpoint for endpoint %u not RX\n",
186 /* ...and if it's to be an AP endpoint... */
187 if (other_data->ee_id == GSI_EE_AP) {
188 /* ...make sure it has status enabled. */
189 if (!other_data->endpoint.config.status_enable) {
191 "status not enabled for endpoint %u\n",
192 other_data->endpoint_id);
198 if (data->endpoint.config.dma_mode) {
199 other_name = data->endpoint.config.dma_endpoint;
200 if (other_name >= count) {
201 dev_err(dev, "DMA endpoint name %u out of range "
203 other_name, data->endpoint_id);
207 other_data = &all_data[other_name];
208 if (ipa_gsi_endpoint_data_empty(other_data)) {
209 dev_err(dev, "DMA endpoint name %u undefined "
211 other_name, data->endpoint_id);
219 static bool ipa_endpoint_data_valid(struct ipa *ipa, u32 count,
220 const struct ipa_gsi_endpoint_data *data)
222 const struct ipa_gsi_endpoint_data *dp = data;
223 struct device *dev = &ipa->pdev->dev;
224 enum ipa_endpoint_name name;
226 ipa_endpoint_validate_build();
228 if (count > IPA_ENDPOINT_COUNT) {
229 dev_err(dev, "too many endpoints specified (%u > %u)\n",
230 count, IPA_ENDPOINT_COUNT);
234 /* Make sure needed endpoints have defined data */
235 if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_COMMAND_TX])) {
236 dev_err(dev, "command TX endpoint not defined\n");
239 if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_LAN_RX])) {
240 dev_err(dev, "LAN RX endpoint not defined\n");
243 if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_MODEM_TX])) {
244 dev_err(dev, "AP->modem TX endpoint not defined\n");
247 if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_MODEM_RX])) {
248 dev_err(dev, "AP<-modem RX endpoint not defined\n");
252 for (name = 0; name < count; name++, dp++)
253 if (!ipa_endpoint_data_valid_one(ipa, count, data, dp))
259 #else /* !IPA_VALIDATE */
261 static bool ipa_endpoint_data_valid(struct ipa *ipa, u32 count,
262 const struct ipa_gsi_endpoint_data *data)
267 #endif /* !IPA_VALIDATE */
269 /* Allocate a transaction to use on a non-command endpoint */
270 static struct gsi_trans *ipa_endpoint_trans_alloc(struct ipa_endpoint *endpoint,
273 struct gsi *gsi = &endpoint->ipa->gsi;
274 u32 channel_id = endpoint->channel_id;
275 enum dma_data_direction direction;
277 direction = endpoint->toward_ipa ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
279 return gsi_channel_trans_alloc(gsi, channel_id, tre_count, direction);
282 /* suspend_delay represents suspend for RX, delay for TX endpoints.
283 * Note that suspend is not supported starting with IPA v4.0.
286 ipa_endpoint_init_ctrl(struct ipa_endpoint *endpoint, bool suspend_delay)
288 u32 offset = IPA_REG_ENDP_INIT_CTRL_N_OFFSET(endpoint->endpoint_id);
289 struct ipa *ipa = endpoint->ipa;
294 /* Suspend is not supported for IPA v4.0+. Delay doesn't work
295 * correctly on IPA v4.2.
297 * if (endpoint->toward_ipa)
298 * assert(ipa->version != IPA_VERSION_4.2);
300 * assert(ipa->version == IPA_VERSION_3_5_1);
302 mask = endpoint->toward_ipa ? ENDP_DELAY_FMASK : ENDP_SUSPEND_FMASK;
304 val = ioread32(ipa->reg_virt + offset);
305 /* Don't bother if it's already in the requested state */
306 state = !!(val & mask);
307 if (suspend_delay != state) {
309 iowrite32(val, ipa->reg_virt + offset);
315 /* We currently don't care what the previous state was for delay mode */
317 ipa_endpoint_program_delay(struct ipa_endpoint *endpoint, bool enable)
319 /* assert(endpoint->toward_ipa); */
321 (void)ipa_endpoint_init_ctrl(endpoint, enable);
324 /* Returns previous suspend state (true means it was enabled) */
326 ipa_endpoint_program_suspend(struct ipa_endpoint *endpoint, bool enable)
328 /* assert(!endpoint->toward_ipa); */
330 return ipa_endpoint_init_ctrl(endpoint, enable);
333 /* Enable or disable delay or suspend mode on all modem endpoints */
334 void ipa_endpoint_modem_pause_all(struct ipa *ipa, bool enable)
336 bool support_suspend;
339 /* DELAY mode doesn't work correctly on IPA v4.2 */
340 if (ipa->version == IPA_VERSION_4_2)
343 /* Only IPA v3.5.1 supports SUSPEND mode on RX endpoints */
344 support_suspend = ipa->version == IPA_VERSION_3_5_1;
346 for (endpoint_id = 0; endpoint_id < IPA_ENDPOINT_MAX; endpoint_id++) {
347 struct ipa_endpoint *endpoint = &ipa->endpoint[endpoint_id];
349 if (endpoint->ee_id != GSI_EE_MODEM)
352 /* Set TX delay mode, or for IPA v3.5.1 RX suspend mode */
353 if (endpoint->toward_ipa)
354 ipa_endpoint_program_delay(endpoint, enable);
355 else if (support_suspend)
356 (void)ipa_endpoint_program_suspend(endpoint, enable);
360 /* Reset all modem endpoints to use the default exception endpoint */
361 int ipa_endpoint_modem_exception_reset_all(struct ipa *ipa)
363 u32 initialized = ipa->initialized;
364 struct gsi_trans *trans;
367 /* We need one command per modem TX endpoint. We can get an upper
368 * bound on that by assuming all initialized endpoints are modem->IPA.
369 * That won't happen, and we could be more precise, but this is fine
370 * for now. We need to end the transaction with a "tag process."
372 count = hweight32(initialized) + ipa_cmd_tag_process_count();
373 trans = ipa_cmd_trans_alloc(ipa, count);
375 dev_err(&ipa->pdev->dev,
376 "no transaction to reset modem exception endpoints\n");
380 while (initialized) {
381 u32 endpoint_id = __ffs(initialized);
382 struct ipa_endpoint *endpoint;
385 initialized ^= BIT(endpoint_id);
387 /* We only reset modem TX endpoints */
388 endpoint = &ipa->endpoint[endpoint_id];
389 if (!(endpoint->ee_id == GSI_EE_MODEM && endpoint->toward_ipa))
392 offset = IPA_REG_ENDP_STATUS_N_OFFSET(endpoint_id);
394 /* Value written is 0, and all bits are updated. That
395 * means status is disabled on the endpoint, and as a
396 * result all other fields in the register are ignored.
398 ipa_cmd_register_write_add(trans, offset, 0, ~0, false);
401 ipa_cmd_tag_process_add(trans);
403 /* XXX This should have a 1 second timeout */
404 gsi_trans_commit_wait(trans);
409 static void ipa_endpoint_init_cfg(struct ipa_endpoint *endpoint)
411 u32 offset = IPA_REG_ENDP_INIT_CFG_N_OFFSET(endpoint->endpoint_id);
414 /* FRAG_OFFLOAD_EN is 0 */
415 if (endpoint->data->checksum) {
416 if (endpoint->toward_ipa) {
419 val |= u32_encode_bits(IPA_CS_OFFLOAD_UL,
420 CS_OFFLOAD_EN_FMASK);
421 /* Checksum header offset is in 4-byte units */
422 checksum_offset = sizeof(struct rmnet_map_header);
423 checksum_offset /= sizeof(u32);
424 val |= u32_encode_bits(checksum_offset,
425 CS_METADATA_HDR_OFFSET_FMASK);
427 val |= u32_encode_bits(IPA_CS_OFFLOAD_DL,
428 CS_OFFLOAD_EN_FMASK);
431 val |= u32_encode_bits(IPA_CS_OFFLOAD_NONE,
432 CS_OFFLOAD_EN_FMASK);
434 /* CS_GEN_QMB_MASTER_SEL is 0 */
436 iowrite32(val, endpoint->ipa->reg_virt + offset);
440 * We program QMAP endpoints so each packet received is preceded by a QMAP
441 * header structure. The QMAP header contains a 1-byte mux_id and 2-byte
442 * packet size field, and we have the IPA hardware populate both for each
443 * received packet. The header is configured (in the HDR_EXT register)
444 * to use big endian format.
446 * The packet size is written into the QMAP header's pkt_len field. That
447 * location is defined here using the HDR_OFST_PKT_SIZE field.
449 * The mux_id comes from a 4-byte metadata value supplied with each packet
450 * by the modem. It is *not* a QMAP header, but it does contain the mux_id
451 * value that we want, in its low-order byte. A bitmask defined in the
452 * endpoint's METADATA_MASK register defines which byte within the modem
453 * metadata contains the mux_id. And the OFST_METADATA field programmed
454 * here indicates where the extracted byte should be placed within the QMAP
457 static void ipa_endpoint_init_hdr(struct ipa_endpoint *endpoint)
459 u32 offset = IPA_REG_ENDP_INIT_HDR_N_OFFSET(endpoint->endpoint_id);
462 if (endpoint->data->qmap) {
463 size_t header_size = sizeof(struct rmnet_map_header);
465 /* We might supply a checksum header after the QMAP header */
466 if (endpoint->toward_ipa && endpoint->data->checksum)
467 header_size += sizeof(struct rmnet_map_ul_csum_header);
468 val |= u32_encode_bits(header_size, HDR_LEN_FMASK);
470 /* Define how to fill fields in a received QMAP header */
471 if (!endpoint->toward_ipa) {
472 u32 off; /* Field offset within header */
474 /* Where IPA will write the metadata value */
475 off = offsetof(struct rmnet_map_header, mux_id);
476 val |= u32_encode_bits(off, HDR_OFST_METADATA_FMASK);
478 /* Where IPA will write the length */
479 off = offsetof(struct rmnet_map_header, pkt_len);
480 val |= HDR_OFST_PKT_SIZE_VALID_FMASK;
481 val |= u32_encode_bits(off, HDR_OFST_PKT_SIZE_FMASK);
483 /* For QMAP TX, metadata offset is 0 (modem assumes this) */
484 val |= HDR_OFST_METADATA_VALID_FMASK;
486 /* HDR_ADDITIONAL_CONST_LEN is 0; (RX only) */
487 /* HDR_A5_MUX is 0 */
488 /* HDR_LEN_INC_DEAGG_HDR is 0 */
489 /* HDR_METADATA_REG_VALID is 0 (TX only) */
492 iowrite32(val, endpoint->ipa->reg_virt + offset);
495 static void ipa_endpoint_init_hdr_ext(struct ipa_endpoint *endpoint)
497 u32 offset = IPA_REG_ENDP_INIT_HDR_EXT_N_OFFSET(endpoint->endpoint_id);
498 u32 pad_align = endpoint->data->rx.pad_align;
501 val |= HDR_ENDIANNESS_FMASK; /* big endian */
503 /* A QMAP header contains a 6 bit pad field at offset 0. The RMNet
504 * driver assumes this field is meaningful in packets it receives,
505 * and assumes the header's payload length includes that padding.
506 * The RMNet driver does *not* pad packets it sends, however, so
507 * the pad field (although 0) should be ignored.
509 if (endpoint->data->qmap && !endpoint->toward_ipa) {
510 val |= HDR_TOTAL_LEN_OR_PAD_VALID_FMASK;
511 /* HDR_TOTAL_LEN_OR_PAD is 0 (pad, not total_len) */
512 val |= HDR_PAYLOAD_LEN_INC_PADDING_FMASK;
513 /* HDR_TOTAL_LEN_OR_PAD_OFFSET is 0 */
516 /* HDR_PAYLOAD_LEN_INC_PADDING is 0 */
517 if (!endpoint->toward_ipa)
518 val |= u32_encode_bits(pad_align, HDR_PAD_TO_ALIGNMENT_FMASK);
520 iowrite32(val, endpoint->ipa->reg_virt + offset);
524 static void ipa_endpoint_init_hdr_metadata_mask(struct ipa_endpoint *endpoint)
526 u32 endpoint_id = endpoint->endpoint_id;
530 offset = IPA_REG_ENDP_INIT_HDR_METADATA_MASK_N_OFFSET(endpoint_id);
532 /* Note that HDR_ENDIANNESS indicates big endian header fields */
533 if (!endpoint->toward_ipa && endpoint->data->qmap)
534 val = cpu_to_be32(IPA_ENDPOINT_QMAP_METADATA_MASK);
536 iowrite32(val, endpoint->ipa->reg_virt + offset);
539 static void ipa_endpoint_init_mode(struct ipa_endpoint *endpoint)
541 u32 offset = IPA_REG_ENDP_INIT_MODE_N_OFFSET(endpoint->endpoint_id);
544 if (endpoint->toward_ipa && endpoint->data->dma_mode) {
545 enum ipa_endpoint_name name = endpoint->data->dma_endpoint;
548 dma_endpoint_id = endpoint->ipa->name_map[name]->endpoint_id;
550 val = u32_encode_bits(IPA_DMA, MODE_FMASK);
551 val |= u32_encode_bits(dma_endpoint_id, DEST_PIPE_INDEX_FMASK);
553 val = u32_encode_bits(IPA_BASIC, MODE_FMASK);
555 /* Other bitfields unspecified (and 0) */
557 iowrite32(val, endpoint->ipa->reg_virt + offset);
560 /* Compute the aggregation size value to use for a given buffer size */
561 static u32 ipa_aggr_size_kb(u32 rx_buffer_size)
563 /* We don't use "hard byte limit" aggregation, so we define the
564 * aggregation limit such that our buffer has enough space *after*
565 * that limit to receive a full MTU of data, plus overhead.
567 rx_buffer_size -= IPA_MTU + IPA_RX_BUFFER_OVERHEAD;
569 return rx_buffer_size / SZ_1K;
572 static void ipa_endpoint_init_aggr(struct ipa_endpoint *endpoint)
574 u32 offset = IPA_REG_ENDP_INIT_AGGR_N_OFFSET(endpoint->endpoint_id);
577 if (endpoint->data->aggregation) {
578 if (!endpoint->toward_ipa) {
579 u32 aggr_size = ipa_aggr_size_kb(IPA_RX_BUFFER_SIZE);
582 val |= u32_encode_bits(IPA_ENABLE_AGGR, AGGR_EN_FMASK);
583 val |= u32_encode_bits(IPA_GENERIC, AGGR_TYPE_FMASK);
584 val |= u32_encode_bits(aggr_size,
585 AGGR_BYTE_LIMIT_FMASK);
586 limit = IPA_AGGR_TIME_LIMIT_DEFAULT;
587 val |= u32_encode_bits(limit / IPA_AGGR_GRANULARITY,
588 AGGR_TIME_LIMIT_FMASK);
589 val |= u32_encode_bits(0, AGGR_PKT_LIMIT_FMASK);
590 if (endpoint->data->rx.aggr_close_eof)
591 val |= AGGR_SW_EOF_ACTIVE_FMASK;
592 /* AGGR_HARD_BYTE_LIMIT_ENABLE is 0 */
594 val |= u32_encode_bits(IPA_ENABLE_DEAGGR,
596 val |= u32_encode_bits(IPA_QCMAP, AGGR_TYPE_FMASK);
597 /* other fields ignored */
599 /* AGGR_FORCE_CLOSE is 0 */
601 val |= u32_encode_bits(IPA_BYPASS_AGGR, AGGR_EN_FMASK);
602 /* other fields ignored */
605 iowrite32(val, endpoint->ipa->reg_virt + offset);
608 /* A return value of 0 indicates an error */
609 static u32 ipa_reg_init_hol_block_timer_val(struct ipa *ipa, u32 microseconds)
616 return 0; /* invalid delay */
618 /* Timer is represented in units of clock ticks. */
619 if (ipa->version < IPA_VERSION_4_2)
620 return microseconds; /* XXX Needs to be computed */
622 /* IPA v4.2 represents the tick count as base * scale */
623 scale = 1; /* XXX Needs to be computed */
624 if (scale > field_max(SCALE_FMASK))
625 return 0; /* scale too big */
627 base = DIV_ROUND_CLOSEST(microseconds, scale);
628 if (base > field_max(BASE_VALUE_FMASK))
629 return 0; /* microseconds too big */
631 val = u32_encode_bits(scale, SCALE_FMASK);
632 val |= u32_encode_bits(base, BASE_VALUE_FMASK);
637 static int ipa_endpoint_init_hol_block_timer(struct ipa_endpoint *endpoint,
640 u32 endpoint_id = endpoint->endpoint_id;
641 struct ipa *ipa = endpoint->ipa;
645 /* XXX We'll fix this when the register definition is clear */
647 struct device *dev = &ipa->pdev->dev;
649 dev_err(dev, "endpoint %u non-zero HOLB period (ignoring)\n",
655 val = ipa_reg_init_hol_block_timer_val(ipa, microseconds);
659 val = 0; /* timeout is immediate */
661 offset = IPA_REG_ENDP_INIT_HOL_BLOCK_TIMER_N_OFFSET(endpoint_id);
662 iowrite32(val, ipa->reg_virt + offset);
668 ipa_endpoint_init_hol_block_enable(struct ipa_endpoint *endpoint, bool enable)
670 u32 endpoint_id = endpoint->endpoint_id;
674 val = u32_encode_bits(enable ? 1 : 0, HOL_BLOCK_EN_FMASK);
675 offset = IPA_REG_ENDP_INIT_HOL_BLOCK_EN_N_OFFSET(endpoint_id);
676 iowrite32(val, endpoint->ipa->reg_virt + offset);
679 void ipa_endpoint_modem_hol_block_clear_all(struct ipa *ipa)
683 for (i = 0; i < IPA_ENDPOINT_MAX; i++) {
684 struct ipa_endpoint *endpoint = &ipa->endpoint[i];
686 if (endpoint->ee_id != GSI_EE_MODEM)
689 (void)ipa_endpoint_init_hol_block_timer(endpoint, 0);
690 ipa_endpoint_init_hol_block_enable(endpoint, true);
694 static void ipa_endpoint_init_deaggr(struct ipa_endpoint *endpoint)
696 u32 offset = IPA_REG_ENDP_INIT_DEAGGR_N_OFFSET(endpoint->endpoint_id);
699 /* DEAGGR_HDR_LEN is 0 */
700 /* PACKET_OFFSET_VALID is 0 */
701 /* PACKET_OFFSET_LOCATION is ignored (not valid) */
702 /* MAX_PACKET_LEN is 0 (not enforced) */
704 iowrite32(val, endpoint->ipa->reg_virt + offset);
707 static void ipa_endpoint_init_seq(struct ipa_endpoint *endpoint)
709 u32 offset = IPA_REG_ENDP_INIT_SEQ_N_OFFSET(endpoint->endpoint_id);
710 u32 seq_type = endpoint->seq_type;
713 /* Sequencer type is made up of four nibbles */
714 val |= u32_encode_bits(seq_type & 0xf, HPS_SEQ_TYPE_FMASK);
715 val |= u32_encode_bits((seq_type >> 4) & 0xf, DPS_SEQ_TYPE_FMASK);
716 /* The second two apply to replicated packets */
717 val |= u32_encode_bits((seq_type >> 8) & 0xf, HPS_REP_SEQ_TYPE_FMASK);
718 val |= u32_encode_bits((seq_type >> 12) & 0xf, DPS_REP_SEQ_TYPE_FMASK);
720 iowrite32(val, endpoint->ipa->reg_virt + offset);
724 * ipa_endpoint_skb_tx() - Transmit a socket buffer
725 * @endpoint: Endpoint pointer
726 * @skb: Socket buffer to send
728 * Returns: 0 if successful, or a negative error code
730 int ipa_endpoint_skb_tx(struct ipa_endpoint *endpoint, struct sk_buff *skb)
732 struct gsi_trans *trans;
736 /* Make sure source endpoint's TLV FIFO has enough entries to
737 * hold the linear portion of the skb and all its fragments.
738 * If not, see if we can linearize it before giving up.
740 nr_frags = skb_shinfo(skb)->nr_frags;
741 if (1 + nr_frags > endpoint->trans_tre_max) {
742 if (skb_linearize(skb))
747 trans = ipa_endpoint_trans_alloc(endpoint, 1 + nr_frags);
751 ret = gsi_trans_skb_add(trans, skb);
754 trans->data = skb; /* transaction owns skb now */
756 gsi_trans_commit(trans, !netdev_xmit_more());
761 gsi_trans_free(trans);
766 static void ipa_endpoint_status(struct ipa_endpoint *endpoint)
768 u32 endpoint_id = endpoint->endpoint_id;
769 struct ipa *ipa = endpoint->ipa;
773 offset = IPA_REG_ENDP_STATUS_N_OFFSET(endpoint_id);
775 if (endpoint->data->status_enable) {
776 val |= STATUS_EN_FMASK;
777 if (endpoint->toward_ipa) {
778 enum ipa_endpoint_name name;
779 u32 status_endpoint_id;
781 name = endpoint->data->tx.status_endpoint;
782 status_endpoint_id = ipa->name_map[name]->endpoint_id;
784 val |= u32_encode_bits(status_endpoint_id,
787 /* STATUS_LOCATION is 0 (status element precedes packet) */
788 /* The next field is present for IPA v4.0 and above */
789 /* STATUS_PKT_SUPPRESS_FMASK is 0 */
792 iowrite32(val, ipa->reg_virt + offset);
795 static int ipa_endpoint_replenish_one(struct ipa_endpoint *endpoint)
797 struct gsi_trans *trans;
798 bool doorbell = false;
804 page = dev_alloc_pages(get_order(IPA_RX_BUFFER_SIZE));
808 trans = ipa_endpoint_trans_alloc(endpoint, 1);
812 /* Offset the buffer to make space for skb headroom */
813 offset = NET_SKB_PAD;
814 len = IPA_RX_BUFFER_SIZE - offset;
816 ret = gsi_trans_page_add(trans, page, len, offset);
819 trans->data = page; /* transaction owns page now */
821 if (++endpoint->replenish_ready == IPA_REPLENISH_BATCH) {
823 endpoint->replenish_ready = 0;
826 gsi_trans_commit(trans, doorbell);
831 gsi_trans_free(trans);
833 __free_pages(page, get_order(IPA_RX_BUFFER_SIZE));
839 * ipa_endpoint_replenish() - Replenish the Rx packets cache.
841 * Allocate RX packet wrapper structures with maximal socket buffers
842 * for an endpoint. These are supplied to the hardware, which fills
843 * them with incoming data.
845 static void ipa_endpoint_replenish(struct ipa_endpoint *endpoint, u32 count)
850 if (!endpoint->replenish_enabled) {
852 atomic_add(count, &endpoint->replenish_saved);
857 while (atomic_dec_not_zero(&endpoint->replenish_backlog))
858 if (ipa_endpoint_replenish_one(endpoint))
859 goto try_again_later;
861 atomic_add(count, &endpoint->replenish_backlog);
866 /* The last one didn't succeed, so fix the backlog */
867 backlog = atomic_inc_return(&endpoint->replenish_backlog);
870 atomic_add(count, &endpoint->replenish_backlog);
872 /* Whenever a receive buffer transaction completes we'll try to
873 * replenish again. It's unlikely, but if we fail to supply even
874 * one buffer, nothing will trigger another replenish attempt.
875 * Receive buffer transactions use one TRE, so schedule work to
876 * try replenishing again if our backlog is *all* available TREs.
878 gsi = &endpoint->ipa->gsi;
879 if (backlog == gsi_channel_tre_max(gsi, endpoint->channel_id))
880 schedule_delayed_work(&endpoint->replenish_work,
881 msecs_to_jiffies(1));
884 static void ipa_endpoint_replenish_enable(struct ipa_endpoint *endpoint)
886 struct gsi *gsi = &endpoint->ipa->gsi;
890 endpoint->replenish_enabled = true;
891 while ((saved = atomic_xchg(&endpoint->replenish_saved, 0)))
892 atomic_add(saved, &endpoint->replenish_backlog);
894 /* Start replenishing if hardware currently has no buffers */
895 max_backlog = gsi_channel_tre_max(gsi, endpoint->channel_id);
896 if (atomic_read(&endpoint->replenish_backlog) == max_backlog)
897 ipa_endpoint_replenish(endpoint, 0);
900 static void ipa_endpoint_replenish_disable(struct ipa_endpoint *endpoint)
904 endpoint->replenish_enabled = false;
905 while ((backlog = atomic_xchg(&endpoint->replenish_backlog, 0)))
906 atomic_add(backlog, &endpoint->replenish_saved);
909 static void ipa_endpoint_replenish_work(struct work_struct *work)
911 struct delayed_work *dwork = to_delayed_work(work);
912 struct ipa_endpoint *endpoint;
914 endpoint = container_of(dwork, struct ipa_endpoint, replenish_work);
916 ipa_endpoint_replenish(endpoint, 0);
919 static void ipa_endpoint_skb_copy(struct ipa_endpoint *endpoint,
920 void *data, u32 len, u32 extra)
924 skb = __dev_alloc_skb(len, GFP_ATOMIC);
927 memcpy(skb->data, data, len);
928 skb->truesize += extra;
931 /* Now receive it, or drop it if there's no netdev */
932 if (endpoint->netdev)
933 ipa_modem_skb_rx(endpoint->netdev, skb);
935 dev_kfree_skb_any(skb);
938 static bool ipa_endpoint_skb_build(struct ipa_endpoint *endpoint,
939 struct page *page, u32 len)
943 /* Nothing to do if there's no netdev */
944 if (!endpoint->netdev)
947 /* assert(len <= SKB_WITH_OVERHEAD(IPA_RX_BUFFER_SIZE-NET_SKB_PAD)); */
948 skb = build_skb(page_address(page), IPA_RX_BUFFER_SIZE);
950 /* Reserve the headroom and account for the data */
951 skb_reserve(skb, NET_SKB_PAD);
955 /* Receive the buffer (or record drop if unable to build it) */
956 ipa_modem_skb_rx(endpoint->netdev, skb);
961 /* The format of a packet status element is the same for several status
962 * types (opcodes). The NEW_FRAG_RULE, LOG, DCMP (decompression) types
963 * aren't currently supported
965 static bool ipa_status_format_packet(enum ipa_status_opcode opcode)
968 case IPA_STATUS_OPCODE_PACKET:
969 case IPA_STATUS_OPCODE_DROPPED_PACKET:
970 case IPA_STATUS_OPCODE_SUSPENDED_PACKET:
971 case IPA_STATUS_OPCODE_PACKET_2ND_PASS:
978 static bool ipa_endpoint_status_skip(struct ipa_endpoint *endpoint,
979 const struct ipa_status *status)
983 if (!ipa_status_format_packet(status->opcode))
985 if (!status->pkt_len)
987 endpoint_id = u32_get_bits(status->endp_dst_idx,
988 IPA_STATUS_DST_IDX_FMASK);
989 if (endpoint_id != endpoint->endpoint_id)
992 return false; /* Don't skip this packet, process it */
995 /* Return whether the status indicates the packet should be dropped */
996 static bool ipa_status_drop_packet(const struct ipa_status *status)
1000 /* Deaggregation exceptions we drop; others we consume */
1001 if (status->exception)
1002 return status->exception == IPA_STATUS_EXCEPTION_DEAGGR;
1004 /* Drop the packet if it fails to match a routing rule; otherwise no */
1005 val = le32_get_bits(status->flags1, IPA_STATUS_FLAGS1_RT_RULE_ID_FMASK);
1007 return val == field_max(IPA_STATUS_FLAGS1_RT_RULE_ID_FMASK);
1010 static void ipa_endpoint_status_parse(struct ipa_endpoint *endpoint,
1011 struct page *page, u32 total_len)
1013 void *data = page_address(page) + NET_SKB_PAD;
1014 u32 unused = IPA_RX_BUFFER_SIZE - total_len;
1015 u32 resid = total_len;
1018 const struct ipa_status *status = data;
1022 if (resid < sizeof(*status)) {
1023 dev_err(&endpoint->ipa->pdev->dev,
1024 "short message (%u bytes < %zu byte status)\n",
1025 resid, sizeof(*status));
1029 /* Skip over status packets that lack packet data */
1030 if (ipa_endpoint_status_skip(endpoint, status)) {
1031 data += sizeof(*status);
1032 resid -= sizeof(*status);
1036 /* Compute the amount of buffer space consumed by the
1037 * packet, including the status element. If the hardware
1038 * is configured to pad packet data to an aligned boundary,
1039 * account for that. And if checksum offload is is enabled
1040 * a trailer containing computed checksum information will
1043 align = endpoint->data->rx.pad_align ? : 1;
1044 len = le16_to_cpu(status->pkt_len);
1045 len = sizeof(*status) + ALIGN(len, align);
1046 if (endpoint->data->checksum)
1047 len += sizeof(struct rmnet_map_dl_csum_trailer);
1049 /* Charge the new packet with a proportional fraction of
1050 * the unused space in the original receive buffer.
1051 * XXX Charge a proportion of the *whole* receive buffer?
1053 if (!ipa_status_drop_packet(status)) {
1054 u32 extra = unused * len / total_len;
1055 void *data2 = data + sizeof(*status);
1056 u32 len2 = le16_to_cpu(status->pkt_len);
1058 /* Client receives only packet data (no status) */
1059 ipa_endpoint_skb_copy(endpoint, data2, len2, extra);
1062 /* Consume status and the full packet it describes */
1068 /* Complete a TX transaction, command or from ipa_endpoint_skb_tx() */
1069 static void ipa_endpoint_tx_complete(struct ipa_endpoint *endpoint,
1070 struct gsi_trans *trans)
1074 /* Complete transaction initiated in ipa_endpoint_replenish_one() */
1075 static void ipa_endpoint_rx_complete(struct ipa_endpoint *endpoint,
1076 struct gsi_trans *trans)
1080 ipa_endpoint_replenish(endpoint, 1);
1082 if (trans->cancelled)
1085 /* Parse or build a socket buffer using the actual received length */
1087 if (endpoint->data->status_enable)
1088 ipa_endpoint_status_parse(endpoint, page, trans->len);
1089 else if (ipa_endpoint_skb_build(endpoint, page, trans->len))
1090 trans->data = NULL; /* Pages have been consumed */
1093 void ipa_endpoint_trans_complete(struct ipa_endpoint *endpoint,
1094 struct gsi_trans *trans)
1096 if (endpoint->toward_ipa)
1097 ipa_endpoint_tx_complete(endpoint, trans);
1099 ipa_endpoint_rx_complete(endpoint, trans);
1102 void ipa_endpoint_trans_release(struct ipa_endpoint *endpoint,
1103 struct gsi_trans *trans)
1105 if (endpoint->toward_ipa) {
1106 struct ipa *ipa = endpoint->ipa;
1108 /* Nothing to do for command transactions */
1109 if (endpoint != ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]) {
1110 struct sk_buff *skb = trans->data;
1113 dev_kfree_skb_any(skb);
1116 struct page *page = trans->data;
1119 __free_pages(page, get_order(IPA_RX_BUFFER_SIZE));
1123 void ipa_endpoint_default_route_set(struct ipa *ipa, u32 endpoint_id)
1127 /* ROUTE_DIS is 0 */
1128 val = u32_encode_bits(endpoint_id, ROUTE_DEF_PIPE_FMASK);
1129 val |= ROUTE_DEF_HDR_TABLE_FMASK;
1130 val |= u32_encode_bits(0, ROUTE_DEF_HDR_OFST_FMASK);
1131 val |= u32_encode_bits(endpoint_id, ROUTE_FRAG_DEF_PIPE_FMASK);
1132 val |= ROUTE_DEF_RETAIN_HDR_FMASK;
1134 iowrite32(val, ipa->reg_virt + IPA_REG_ROUTE_OFFSET);
1137 void ipa_endpoint_default_route_clear(struct ipa *ipa)
1139 ipa_endpoint_default_route_set(ipa, 0);
1142 static bool ipa_endpoint_aggr_active(struct ipa_endpoint *endpoint)
1144 u32 mask = BIT(endpoint->endpoint_id);
1145 struct ipa *ipa = endpoint->ipa;
1149 /* assert(mask & ipa->available); */
1150 offset = ipa_reg_state_aggr_active_offset(ipa->version);
1151 val = ioread32(ipa->reg_virt + offset);
1153 return !!(val & mask);
1156 static void ipa_endpoint_force_close(struct ipa_endpoint *endpoint)
1158 u32 mask = BIT(endpoint->endpoint_id);
1159 struct ipa *ipa = endpoint->ipa;
1161 /* assert(mask & ipa->available); */
1162 iowrite32(mask, ipa->reg_virt + IPA_REG_AGGR_FORCE_CLOSE_OFFSET);
1166 * ipa_endpoint_reset_rx_aggr() - Reset RX endpoint with aggregation active
1167 * @endpoint: Endpoint to be reset
1169 * If aggregation is active on an RX endpoint when a reset is performed
1170 * on its underlying GSI channel, a special sequence of actions must be
1171 * taken to ensure the IPA pipeline is properly cleared.
1173 * @Return: 0 if successful, or a negative error code
1175 static int ipa_endpoint_reset_rx_aggr(struct ipa_endpoint *endpoint)
1177 struct device *dev = &endpoint->ipa->pdev->dev;
1178 struct ipa *ipa = endpoint->ipa;
1179 struct gsi *gsi = &ipa->gsi;
1180 bool suspended = false;
1188 virt = kzalloc(len, GFP_KERNEL);
1192 addr = dma_map_single(dev, virt, len, DMA_FROM_DEVICE);
1193 if (dma_mapping_error(dev, addr)) {
1198 /* Force close aggregation before issuing the reset */
1199 ipa_endpoint_force_close(endpoint);
1201 /* Reset and reconfigure the channel with the doorbell engine
1202 * disabled. Then poll until we know aggregation is no longer
1203 * active. We'll re-enable the doorbell (if appropriate) when
1204 * we reset again below.
1206 gsi_channel_reset(gsi, endpoint->channel_id, false);
1208 /* Make sure the channel isn't suspended */
1209 if (endpoint->ipa->version == IPA_VERSION_3_5_1)
1210 suspended = ipa_endpoint_program_suspend(endpoint, false);
1212 /* Start channel and do a 1 byte read */
1213 ret = gsi_channel_start(gsi, endpoint->channel_id);
1215 goto out_suspend_again;
1217 ret = gsi_trans_read_byte(gsi, endpoint->channel_id, addr);
1219 goto err_endpoint_stop;
1221 /* Wait for aggregation to be closed on the channel */
1222 retries = IPA_ENDPOINT_RESET_AGGR_RETRY_MAX;
1224 if (!ipa_endpoint_aggr_active(endpoint))
1227 } while (retries--);
1229 /* Check one last time */
1230 if (ipa_endpoint_aggr_active(endpoint))
1231 dev_err(dev, "endpoint %u still active during reset\n",
1232 endpoint->endpoint_id);
1234 gsi_trans_read_byte_done(gsi, endpoint->channel_id);
1236 ret = gsi_channel_stop(gsi, endpoint->channel_id);
1238 goto out_suspend_again;
1240 /* Finally, reset and reconfigure the channel again (re-enabling the
1241 * the doorbell engine if appropriate). Sleep for 1 millisecond to
1242 * complete the channel reset sequence. Finish by suspending the
1243 * channel again (if necessary).
1245 legacy = ipa->version == IPA_VERSION_3_5_1;
1246 gsi_channel_reset(gsi, endpoint->channel_id, legacy);
1250 goto out_suspend_again;
1253 (void)gsi_channel_stop(gsi, endpoint->channel_id);
1256 (void)ipa_endpoint_program_suspend(endpoint, true);
1257 dma_unmap_single(dev, addr, len, DMA_FROM_DEVICE);
1264 static void ipa_endpoint_reset(struct ipa_endpoint *endpoint)
1266 u32 channel_id = endpoint->channel_id;
1267 struct ipa *ipa = endpoint->ipa;
1272 /* On IPA v3.5.1, if an RX endpoint is reset while aggregation
1273 * is active, we need to handle things specially to recover.
1274 * All other cases just need to reset the underlying GSI channel.
1276 * IPA v3.5.1 enables the doorbell engine. Newer versions do not.
1278 legacy = ipa->version == IPA_VERSION_3_5_1;
1279 special = !endpoint->toward_ipa && endpoint->data->aggregation;
1280 if (special && ipa_endpoint_aggr_active(endpoint))
1281 ret = ipa_endpoint_reset_rx_aggr(endpoint);
1283 gsi_channel_reset(&ipa->gsi, channel_id, legacy);
1286 dev_err(&ipa->pdev->dev,
1287 "error %d resetting channel %u for endpoint %u\n",
1288 ret, endpoint->channel_id, endpoint->endpoint_id);
1291 static void ipa_endpoint_program(struct ipa_endpoint *endpoint)
1293 if (endpoint->toward_ipa) {
1294 if (endpoint->ipa->version != IPA_VERSION_4_2)
1295 ipa_endpoint_program_delay(endpoint, false);
1296 ipa_endpoint_init_hdr_ext(endpoint);
1297 ipa_endpoint_init_aggr(endpoint);
1298 ipa_endpoint_init_deaggr(endpoint);
1299 ipa_endpoint_init_seq(endpoint);
1301 if (endpoint->ipa->version == IPA_VERSION_3_5_1)
1302 (void)ipa_endpoint_program_suspend(endpoint, false);
1303 ipa_endpoint_init_hdr_ext(endpoint);
1304 ipa_endpoint_init_aggr(endpoint);
1306 ipa_endpoint_init_cfg(endpoint);
1307 ipa_endpoint_init_hdr(endpoint);
1308 ipa_endpoint_init_hdr_metadata_mask(endpoint);
1309 ipa_endpoint_init_mode(endpoint);
1310 ipa_endpoint_status(endpoint);
1313 int ipa_endpoint_enable_one(struct ipa_endpoint *endpoint)
1315 struct ipa *ipa = endpoint->ipa;
1316 struct gsi *gsi = &ipa->gsi;
1319 ret = gsi_channel_start(gsi, endpoint->channel_id);
1321 dev_err(&ipa->pdev->dev,
1322 "error %d starting %cX channel %u for endpoint %u\n",
1323 ret, endpoint->toward_ipa ? 'T' : 'R',
1324 endpoint->channel_id, endpoint->endpoint_id);
1328 if (!endpoint->toward_ipa) {
1329 ipa_interrupt_suspend_enable(ipa->interrupt,
1330 endpoint->endpoint_id);
1331 ipa_endpoint_replenish_enable(endpoint);
1334 ipa->enabled |= BIT(endpoint->endpoint_id);
1339 void ipa_endpoint_disable_one(struct ipa_endpoint *endpoint)
1341 u32 mask = BIT(endpoint->endpoint_id);
1342 struct ipa *ipa = endpoint->ipa;
1343 struct gsi *gsi = &ipa->gsi;
1346 if (!(ipa->enabled & mask))
1349 ipa->enabled ^= mask;
1351 if (!endpoint->toward_ipa) {
1352 ipa_endpoint_replenish_disable(endpoint);
1353 ipa_interrupt_suspend_disable(ipa->interrupt,
1354 endpoint->endpoint_id);
1357 /* Note that if stop fails, the channel's state is not well-defined */
1358 ret = gsi_channel_stop(gsi, endpoint->channel_id);
1360 dev_err(&ipa->pdev->dev,
1361 "error %d attempting to stop endpoint %u\n", ret,
1362 endpoint->endpoint_id);
1366 * ipa_endpoint_suspend_aggr() - Emulate suspend interrupt
1367 * @endpoint_id: Endpoint on which to emulate a suspend
1369 * Emulate suspend IPA interrupt to unsuspend an endpoint suspended
1370 * with an open aggregation frame. This is to work around a hardware
1371 * issue in IPA version 3.5.1 where the suspend interrupt will not be
1372 * generated when it should be.
1374 static void ipa_endpoint_suspend_aggr(struct ipa_endpoint *endpoint)
1376 struct ipa *ipa = endpoint->ipa;
1378 /* assert(ipa->version == IPA_VERSION_3_5_1); */
1380 if (!endpoint->data->aggregation)
1383 /* Nothing to do if the endpoint doesn't have aggregation open */
1384 if (!ipa_endpoint_aggr_active(endpoint))
1387 /* Force close aggregation */
1388 ipa_endpoint_force_close(endpoint);
1390 ipa_interrupt_simulate_suspend(ipa->interrupt);
1393 void ipa_endpoint_suspend_one(struct ipa_endpoint *endpoint)
1395 struct device *dev = &endpoint->ipa->pdev->dev;
1396 struct gsi *gsi = &endpoint->ipa->gsi;
1400 if (!(endpoint->ipa->enabled & BIT(endpoint->endpoint_id)))
1403 if (!endpoint->toward_ipa)
1404 ipa_endpoint_replenish_disable(endpoint);
1406 /* IPA v3.5.1 doesn't use channel stop for suspend */
1407 stop_channel = endpoint->ipa->version != IPA_VERSION_3_5_1;
1408 if (!endpoint->toward_ipa && !stop_channel) {
1409 /* Due to a hardware bug, a client suspended with an open
1410 * aggregation frame will not generate a SUSPEND IPA
1411 * interrupt. We work around this by force-closing the
1412 * aggregation frame, then simulating the arrival of such
1415 (void)ipa_endpoint_program_suspend(endpoint, true);
1416 ipa_endpoint_suspend_aggr(endpoint);
1419 ret = gsi_channel_suspend(gsi, endpoint->channel_id, stop_channel);
1421 dev_err(dev, "error %d suspending channel %u\n", ret,
1422 endpoint->channel_id);
1425 void ipa_endpoint_resume_one(struct ipa_endpoint *endpoint)
1427 struct device *dev = &endpoint->ipa->pdev->dev;
1428 struct gsi *gsi = &endpoint->ipa->gsi;
1432 if (!(endpoint->ipa->enabled & BIT(endpoint->endpoint_id)))
1435 /* IPA v3.5.1 doesn't use channel start for resume */
1436 start_channel = endpoint->ipa->version != IPA_VERSION_3_5_1;
1437 if (!endpoint->toward_ipa && !start_channel)
1438 (void)ipa_endpoint_program_suspend(endpoint, false);
1440 ret = gsi_channel_resume(gsi, endpoint->channel_id, start_channel);
1442 dev_err(dev, "error %d resuming channel %u\n", ret,
1443 endpoint->channel_id);
1444 else if (!endpoint->toward_ipa)
1445 ipa_endpoint_replenish_enable(endpoint);
1448 void ipa_endpoint_suspend(struct ipa *ipa)
1450 if (ipa->modem_netdev)
1451 ipa_modem_suspend(ipa->modem_netdev);
1453 ipa_cmd_tag_process(ipa);
1455 ipa_endpoint_suspend_one(ipa->name_map[IPA_ENDPOINT_AP_LAN_RX]);
1456 ipa_endpoint_suspend_one(ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]);
1459 void ipa_endpoint_resume(struct ipa *ipa)
1461 ipa_endpoint_resume_one(ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]);
1462 ipa_endpoint_resume_one(ipa->name_map[IPA_ENDPOINT_AP_LAN_RX]);
1464 if (ipa->modem_netdev)
1465 ipa_modem_resume(ipa->modem_netdev);
1468 static void ipa_endpoint_setup_one(struct ipa_endpoint *endpoint)
1470 struct gsi *gsi = &endpoint->ipa->gsi;
1471 u32 channel_id = endpoint->channel_id;
1473 /* Only AP endpoints get set up */
1474 if (endpoint->ee_id != GSI_EE_AP)
1477 endpoint->trans_tre_max = gsi_channel_trans_tre_max(gsi, channel_id);
1478 if (!endpoint->toward_ipa) {
1479 /* RX transactions require a single TRE, so the maximum
1480 * backlog is the same as the maximum outstanding TREs.
1482 endpoint->replenish_enabled = false;
1483 atomic_set(&endpoint->replenish_saved,
1484 gsi_channel_tre_max(gsi, endpoint->channel_id));
1485 atomic_set(&endpoint->replenish_backlog, 0);
1486 INIT_DELAYED_WORK(&endpoint->replenish_work,
1487 ipa_endpoint_replenish_work);
1490 ipa_endpoint_program(endpoint);
1492 endpoint->ipa->set_up |= BIT(endpoint->endpoint_id);
1495 static void ipa_endpoint_teardown_one(struct ipa_endpoint *endpoint)
1497 endpoint->ipa->set_up &= ~BIT(endpoint->endpoint_id);
1499 if (!endpoint->toward_ipa)
1500 cancel_delayed_work_sync(&endpoint->replenish_work);
1502 ipa_endpoint_reset(endpoint);
1505 void ipa_endpoint_setup(struct ipa *ipa)
1507 u32 initialized = ipa->initialized;
1510 while (initialized) {
1511 u32 endpoint_id = __ffs(initialized);
1513 initialized ^= BIT(endpoint_id);
1515 ipa_endpoint_setup_one(&ipa->endpoint[endpoint_id]);
1519 void ipa_endpoint_teardown(struct ipa *ipa)
1521 u32 set_up = ipa->set_up;
1524 u32 endpoint_id = __fls(set_up);
1526 set_up ^= BIT(endpoint_id);
1528 ipa_endpoint_teardown_one(&ipa->endpoint[endpoint_id]);
1533 int ipa_endpoint_config(struct ipa *ipa)
1535 struct device *dev = &ipa->pdev->dev;
1544 /* Find out about the endpoints supplied by the hardware, and ensure
1545 * the highest one doesn't exceed the number we support.
1547 val = ioread32(ipa->reg_virt + IPA_REG_FLAVOR_0_OFFSET);
1549 /* Our RX is an IPA producer */
1550 rx_base = u32_get_bits(val, BAM_PROD_LOWEST_FMASK);
1551 max = rx_base + u32_get_bits(val, BAM_MAX_PROD_PIPES_FMASK);
1552 if (max > IPA_ENDPOINT_MAX) {
1553 dev_err(dev, "too many endpoints (%u > %u)\n",
1554 max, IPA_ENDPOINT_MAX);
1557 rx_mask = GENMASK(max - 1, rx_base);
1559 /* Our TX is an IPA consumer */
1560 max = u32_get_bits(val, BAM_MAX_CONS_PIPES_FMASK);
1561 tx_mask = GENMASK(max - 1, 0);
1563 ipa->available = rx_mask | tx_mask;
1565 /* Check for initialized endpoints not supported by the hardware */
1566 if (ipa->initialized & ~ipa->available) {
1567 dev_err(dev, "unavailable endpoint id(s) 0x%08x\n",
1568 ipa->initialized & ~ipa->available);
1569 ret = -EINVAL; /* Report other errors too */
1572 initialized = ipa->initialized;
1573 while (initialized) {
1574 u32 endpoint_id = __ffs(initialized);
1575 struct ipa_endpoint *endpoint;
1577 initialized ^= BIT(endpoint_id);
1579 /* Make sure it's pointing in the right direction */
1580 endpoint = &ipa->endpoint[endpoint_id];
1581 if ((endpoint_id < rx_base) != !!endpoint->toward_ipa) {
1582 dev_err(dev, "endpoint id %u wrong direction\n",
1591 void ipa_endpoint_deconfig(struct ipa *ipa)
1593 ipa->available = 0; /* Nothing more to do */
1596 static void ipa_endpoint_init_one(struct ipa *ipa, enum ipa_endpoint_name name,
1597 const struct ipa_gsi_endpoint_data *data)
1599 struct ipa_endpoint *endpoint;
1601 endpoint = &ipa->endpoint[data->endpoint_id];
1603 if (data->ee_id == GSI_EE_AP)
1604 ipa->channel_map[data->channel_id] = endpoint;
1605 ipa->name_map[name] = endpoint;
1607 endpoint->ipa = ipa;
1608 endpoint->ee_id = data->ee_id;
1609 endpoint->seq_type = data->endpoint.seq_type;
1610 endpoint->channel_id = data->channel_id;
1611 endpoint->endpoint_id = data->endpoint_id;
1612 endpoint->toward_ipa = data->toward_ipa;
1613 endpoint->data = &data->endpoint.config;
1615 ipa->initialized |= BIT(endpoint->endpoint_id);
1618 void ipa_endpoint_exit_one(struct ipa_endpoint *endpoint)
1620 endpoint->ipa->initialized &= ~BIT(endpoint->endpoint_id);
1622 memset(endpoint, 0, sizeof(*endpoint));
1625 void ipa_endpoint_exit(struct ipa *ipa)
1627 u32 initialized = ipa->initialized;
1629 while (initialized) {
1630 u32 endpoint_id = __fls(initialized);
1632 initialized ^= BIT(endpoint_id);
1634 ipa_endpoint_exit_one(&ipa->endpoint[endpoint_id]);
1636 memset(ipa->name_map, 0, sizeof(ipa->name_map));
1637 memset(ipa->channel_map, 0, sizeof(ipa->channel_map));
1640 /* Returns a bitmask of endpoints that support filtering, or 0 on error */
1641 u32 ipa_endpoint_init(struct ipa *ipa, u32 count,
1642 const struct ipa_gsi_endpoint_data *data)
1644 enum ipa_endpoint_name name;
1647 if (!ipa_endpoint_data_valid(ipa, count, data))
1648 return 0; /* Error */
1650 ipa->initialized = 0;
1653 for (name = 0; name < count; name++, data++) {
1654 if (ipa_gsi_endpoint_data_empty(data))
1655 continue; /* Skip over empty slots */
1657 ipa_endpoint_init_one(ipa, name, data);
1659 if (data->endpoint.filter_support)
1660 filter_map |= BIT(data->endpoint_id);
1663 if (!ipa_filter_map_valid(ipa, filter_map))
1664 goto err_endpoint_exit;
1666 return filter_map; /* Non-zero bitmask */
1669 ipa_endpoint_exit(ipa);
1671 return 0; /* Error */