184f9bd12SAlex Elder // SPDX-License-Identifier: GPL-2.0 284f9bd12SAlex Elder 384f9bd12SAlex Elder /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved. 4ff39eefdSAlex Elder * Copyright (C) 2019-2024 Linaro Ltd. 584f9bd12SAlex Elder */ 684f9bd12SAlex Elder 784f9bd12SAlex Elder #include <linux/bitfield.h> 8*88412277SAlex Elder #include <linux/bits.h> 9*88412277SAlex Elder #include <linux/device.h> 1084f9bd12SAlex Elder #include <linux/dma-direction.h> 11*88412277SAlex Elder #include <linux/if_rmnet.h> 12*88412277SAlex Elder #include <linux/types.h> 1384f9bd12SAlex Elder 1484f9bd12SAlex Elder #include "gsi.h" 1584f9bd12SAlex Elder #include "gsi_trans.h" 1684f9bd12SAlex Elder #include "ipa.h" 17*88412277SAlex Elder #include "ipa_cmd.h" 1884f9bd12SAlex Elder #include "ipa_data.h" 1984f9bd12SAlex Elder #include "ipa_endpoint.h" 20*88412277SAlex Elder #include "ipa_gsi.h" 21*88412277SAlex Elder #include "ipa_interrupt.h" 2284f9bd12SAlex Elder #include "ipa_mem.h" 2384f9bd12SAlex Elder #include "ipa_modem.h" 24*88412277SAlex Elder #include "ipa_power.h" 25a53c85f3SAlex Elder #include "ipa_reg.h" 2684f9bd12SAlex Elder #include "ipa_table.h" 27f60e5fb6SAlex Elder #include "ipa_version.h" 2884f9bd12SAlex Elder 299654d8c4SAlex Elder /* Hardware is told about receive buffers once a "batch" has been queued */ 309654d8c4SAlex Elder #define IPA_REPLENISH_BATCH 16 /* Must be non-zero */ 3184f9bd12SAlex Elder 3284f9bd12SAlex Elder /* The amount of RX buffer space consumed by standard skb overhead */ 3384f9bd12SAlex Elder #define IPA_RX_BUFFER_OVERHEAD (PAGE_SIZE - SKB_MAX_ORDER(NET_SKB_PAD, 0)) 3484f9bd12SAlex Elder 358730f45dSAlex Elder /* Where to find the QMAP mux_id for a packet within modem-supplied metadata */ 368730f45dSAlex Elder #define IPA_ENDPOINT_QMAP_METADATA_MASK 0x000000ff /* host byte order */ 378730f45dSAlex Elder 3884f9bd12SAlex Elder #define IPA_ENDPOINT_RESET_AGGR_RETRY_MAX 3 3984f9bd12SAlex Elder 40ec4c24f6SAlex Elder /** enum ipa_status_opcode - IPA status opcode field hardware values */ 41ec4c24f6SAlex Elder enum ipa_status_opcode { /* *Not* a bitmask */ 42ec4c24f6SAlex Elder IPA_STATUS_OPCODE_PACKET = 1, 43ec4c24f6SAlex Elder IPA_STATUS_OPCODE_NEW_RULE_PACKET = 2, 44ec4c24f6SAlex Elder IPA_STATUS_OPCODE_DROPPED_PACKET = 4, 45ec4c24f6SAlex Elder IPA_STATUS_OPCODE_SUSPENDED_PACKET = 8, 46ec4c24f6SAlex Elder IPA_STATUS_OPCODE_LOG = 16, 47ec4c24f6SAlex Elder IPA_STATUS_OPCODE_DCMP = 32, 48ec4c24f6SAlex Elder IPA_STATUS_OPCODE_PACKET_2ND_PASS = 64, 4984f9bd12SAlex Elder }; 5084f9bd12SAlex Elder 51ec4c24f6SAlex Elder /** enum ipa_status_exception - IPA status exception field hardware values */ 52ec4c24f6SAlex Elder enum ipa_status_exception { /* *Not* a bitmask */ 5384f9bd12SAlex Elder /* 0 means no exception */ 54ec4c24f6SAlex Elder IPA_STATUS_EXCEPTION_DEAGGR = 1, 55ec4c24f6SAlex Elder IPA_STATUS_EXCEPTION_IPTYPE = 4, 56ec4c24f6SAlex Elder IPA_STATUS_EXCEPTION_PACKET_LENGTH = 8, 57ec4c24f6SAlex Elder IPA_STATUS_EXCEPTION_FRAG_RULE_MISS = 16, 58ec4c24f6SAlex Elder IPA_STATUS_EXCEPTION_SW_FILTER = 32, 59ec4c24f6SAlex Elder IPA_STATUS_EXCEPTION_NAT = 64, /* IPv4 */ 60ec4c24f6SAlex Elder IPA_STATUS_EXCEPTION_IPV6_CONN_TRACK = 64, /* IPv6 */ 61ec4c24f6SAlex Elder IPA_STATUS_EXCEPTION_UC = 128, 62ec4c24f6SAlex Elder IPA_STATUS_EXCEPTION_INVALID_ENDPOINT = 129, 63ec4c24f6SAlex Elder IPA_STATUS_EXCEPTION_HEADER_INSERT = 136, 64ec4c24f6SAlex Elder IPA_STATUS_EXCEPTION_CHEKCSUM = 229, 6584f9bd12SAlex Elder }; 6684f9bd12SAlex Elder 678e71708bSAlex Elder /** enum ipa_status_mask - IPA status mask field bitmask hardware values */ 688e71708bSAlex Elder enum ipa_status_mask { 698e71708bSAlex Elder IPA_STATUS_MASK_FRAG_PROCESS = BIT(0), 708e71708bSAlex Elder IPA_STATUS_MASK_FILT_PROCESS = BIT(1), 718e71708bSAlex Elder IPA_STATUS_MASK_NAT_PROCESS = BIT(2), 728e71708bSAlex Elder IPA_STATUS_MASK_ROUTE_PROCESS = BIT(3), 738e71708bSAlex Elder IPA_STATUS_MASK_TAG_VALID = BIT(4), 748e71708bSAlex Elder IPA_STATUS_MASK_FRAGMENT = BIT(5), 758e71708bSAlex Elder IPA_STATUS_MASK_FIRST_FRAGMENT = BIT(6), 768e71708bSAlex Elder IPA_STATUS_MASK_V4 = BIT(7), 778e71708bSAlex Elder IPA_STATUS_MASK_CKSUM_PROCESS = BIT(8), 788e71708bSAlex Elder IPA_STATUS_MASK_AGGR_PROCESS = BIT(9), 798e71708bSAlex Elder IPA_STATUS_MASK_DEST_EOT = BIT(10), 808e71708bSAlex Elder IPA_STATUS_MASK_DEAGGR_PROCESS = BIT(11), 818e71708bSAlex Elder IPA_STATUS_MASK_DEAGG_FIRST = BIT(12), 828e71708bSAlex Elder IPA_STATUS_MASK_SRC_EOT = BIT(13), 838e71708bSAlex Elder IPA_STATUS_MASK_PREV_EOT = BIT(14), 848e71708bSAlex Elder IPA_STATUS_MASK_BYTE_LIMIT = BIT(15), 858e71708bSAlex Elder }; 868e71708bSAlex Elder 87ebd2a82eSAlex Elder /* Special IPA filter/router rule field value indicating "rule miss" */ 88ebd2a82eSAlex Elder #define IPA_STATUS_RULE_MISS 0x3ff /* 10-bit filter/router rule fields */ 89ebd2a82eSAlex Elder 90cbea4761SAlex Elder /** The IPA status nat_type field uses enum ipa_nat_type hardware values */ 91cbea4761SAlex Elder 92ebd2a82eSAlex Elder /* enum ipa_status_field_id - IPA packet status structure field identifiers */ 93ebd2a82eSAlex Elder enum ipa_status_field_id { 94ebd2a82eSAlex Elder STATUS_OPCODE, /* enum ipa_status_opcode */ 95ebd2a82eSAlex Elder STATUS_EXCEPTION, /* enum ipa_status_exception */ 96ebd2a82eSAlex Elder STATUS_MASK, /* enum ipa_status_mask (bitmask) */ 97ebd2a82eSAlex Elder STATUS_LENGTH, 98ebd2a82eSAlex Elder STATUS_SRC_ENDPOINT, 99ebd2a82eSAlex Elder STATUS_DST_ENDPOINT, 100ebd2a82eSAlex Elder STATUS_METADATA, 101ebd2a82eSAlex Elder STATUS_FILTER_LOCAL, /* Boolean */ 102ebd2a82eSAlex Elder STATUS_FILTER_HASH, /* Boolean */ 103ebd2a82eSAlex Elder STATUS_FILTER_GLOBAL, /* Boolean */ 104ebd2a82eSAlex Elder STATUS_FILTER_RETAIN, /* Boolean */ 105ebd2a82eSAlex Elder STATUS_FILTER_RULE_INDEX, 106ebd2a82eSAlex Elder STATUS_ROUTER_LOCAL, /* Boolean */ 107ebd2a82eSAlex Elder STATUS_ROUTER_HASH, /* Boolean */ 108ebd2a82eSAlex Elder STATUS_UCP, /* Boolean */ 109ebd2a82eSAlex Elder STATUS_ROUTER_TABLE, 110ebd2a82eSAlex Elder STATUS_ROUTER_RULE_INDEX, 111ebd2a82eSAlex Elder STATUS_NAT_HIT, /* Boolean */ 112ebd2a82eSAlex Elder STATUS_NAT_INDEX, 113ebd2a82eSAlex Elder STATUS_NAT_TYPE, /* enum ipa_nat_type */ 114ebd2a82eSAlex Elder STATUS_TAG_LOW32, /* Low-order 32 bits of 48-bit tag */ 115ebd2a82eSAlex Elder STATUS_TAG_HIGH16, /* High-order 16 bits of 48-bit tag */ 116ebd2a82eSAlex Elder STATUS_SEQUENCE, 117ebd2a82eSAlex Elder STATUS_TIME_OF_DAY, 118ebd2a82eSAlex Elder STATUS_HEADER_LOCAL, /* Boolean */ 119ebd2a82eSAlex Elder STATUS_HEADER_OFFSET, 120ebd2a82eSAlex Elder STATUS_FRAG_HIT, /* Boolean */ 121ebd2a82eSAlex Elder STATUS_FRAG_RULE_INDEX, 12284f9bd12SAlex Elder }; 12384f9bd12SAlex Elder 124b8dc7d0eSAlex Elder /* Size in bytes of an IPA packet status structure */ 125be7f8012SBert Karwatzki #define IPA_STATUS_SIZE sizeof(__le32[8]) 126b8dc7d0eSAlex Elder 127ebd2a82eSAlex Elder /* IPA status structure decoder; looks up field values for a structure */ 12855c6eae7SAlex Elder static u32 ipa_status_extract(struct ipa *ipa, const void *data, 12955c6eae7SAlex Elder enum ipa_status_field_id field) 130ebd2a82eSAlex Elder { 13155c6eae7SAlex Elder enum ipa_version version = ipa->version; 132ebd2a82eSAlex Elder const __le32 *word = data; 133ebd2a82eSAlex Elder 134ebd2a82eSAlex Elder switch (field) { 135ebd2a82eSAlex Elder case STATUS_OPCODE: 136ebd2a82eSAlex Elder return le32_get_bits(word[0], GENMASK(7, 0)); 137ebd2a82eSAlex Elder case STATUS_EXCEPTION: 138ebd2a82eSAlex Elder return le32_get_bits(word[0], GENMASK(15, 8)); 139ebd2a82eSAlex Elder case STATUS_MASK: 140ebd2a82eSAlex Elder return le32_get_bits(word[0], GENMASK(31, 16)); 141ebd2a82eSAlex Elder case STATUS_LENGTH: 142ebd2a82eSAlex Elder return le32_get_bits(word[1], GENMASK(15, 0)); 143ebd2a82eSAlex Elder case STATUS_SRC_ENDPOINT: 14455c6eae7SAlex Elder if (version < IPA_VERSION_5_0) 145ebd2a82eSAlex Elder return le32_get_bits(word[1], GENMASK(20, 16)); 14655c6eae7SAlex Elder return le32_get_bits(word[1], GENMASK(23, 16)); 14755c6eae7SAlex Elder /* Status word 1, bits 21-23 are reserved (not IPA v5.0+) */ 14855c6eae7SAlex Elder /* Status word 1, bits 24-26 are reserved (IPA v5.0+) */ 149ebd2a82eSAlex Elder case STATUS_DST_ENDPOINT: 15055c6eae7SAlex Elder if (version < IPA_VERSION_5_0) 151ebd2a82eSAlex Elder return le32_get_bits(word[1], GENMASK(28, 24)); 15255c6eae7SAlex Elder return le32_get_bits(word[7], GENMASK(23, 16)); 153ebd2a82eSAlex Elder /* Status word 1, bits 29-31 are reserved */ 154ebd2a82eSAlex Elder case STATUS_METADATA: 155ebd2a82eSAlex Elder return le32_to_cpu(word[2]); 156ebd2a82eSAlex Elder case STATUS_FILTER_LOCAL: 157ebd2a82eSAlex Elder return le32_get_bits(word[3], GENMASK(0, 0)); 158ebd2a82eSAlex Elder case STATUS_FILTER_HASH: 159ebd2a82eSAlex Elder return le32_get_bits(word[3], GENMASK(1, 1)); 160ebd2a82eSAlex Elder case STATUS_FILTER_GLOBAL: 161ebd2a82eSAlex Elder return le32_get_bits(word[3], GENMASK(2, 2)); 162ebd2a82eSAlex Elder case STATUS_FILTER_RETAIN: 163ebd2a82eSAlex Elder return le32_get_bits(word[3], GENMASK(3, 3)); 164ebd2a82eSAlex Elder case STATUS_FILTER_RULE_INDEX: 165ebd2a82eSAlex Elder return le32_get_bits(word[3], GENMASK(13, 4)); 16655c6eae7SAlex Elder /* ROUTER_TABLE is in word 3, bits 14-21 (IPA v5.0+) */ 167ebd2a82eSAlex Elder case STATUS_ROUTER_LOCAL: 16855c6eae7SAlex Elder if (version < IPA_VERSION_5_0) 169ebd2a82eSAlex Elder return le32_get_bits(word[3], GENMASK(14, 14)); 17055c6eae7SAlex Elder return le32_get_bits(word[1], GENMASK(27, 27)); 171ebd2a82eSAlex Elder case STATUS_ROUTER_HASH: 17255c6eae7SAlex Elder if (version < IPA_VERSION_5_0) 173ebd2a82eSAlex Elder return le32_get_bits(word[3], GENMASK(15, 15)); 17455c6eae7SAlex Elder return le32_get_bits(word[1], GENMASK(28, 28)); 175ebd2a82eSAlex Elder case STATUS_UCP: 17655c6eae7SAlex Elder if (version < IPA_VERSION_5_0) 177ebd2a82eSAlex Elder return le32_get_bits(word[3], GENMASK(16, 16)); 17855c6eae7SAlex Elder return le32_get_bits(word[7], GENMASK(31, 31)); 179ebd2a82eSAlex Elder case STATUS_ROUTER_TABLE: 18055c6eae7SAlex Elder if (version < IPA_VERSION_5_0) 181ebd2a82eSAlex Elder return le32_get_bits(word[3], GENMASK(21, 17)); 18255c6eae7SAlex Elder return le32_get_bits(word[3], GENMASK(21, 14)); 183ebd2a82eSAlex Elder case STATUS_ROUTER_RULE_INDEX: 184ebd2a82eSAlex Elder return le32_get_bits(word[3], GENMASK(31, 22)); 185ebd2a82eSAlex Elder case STATUS_NAT_HIT: 186ebd2a82eSAlex Elder return le32_get_bits(word[4], GENMASK(0, 0)); 187ebd2a82eSAlex Elder case STATUS_NAT_INDEX: 188ebd2a82eSAlex Elder return le32_get_bits(word[4], GENMASK(13, 1)); 189ebd2a82eSAlex Elder case STATUS_NAT_TYPE: 190ebd2a82eSAlex Elder return le32_get_bits(word[4], GENMASK(15, 14)); 191ebd2a82eSAlex Elder case STATUS_TAG_LOW32: 192ebd2a82eSAlex Elder return le32_get_bits(word[4], GENMASK(31, 16)) | 193ebd2a82eSAlex Elder (le32_get_bits(word[5], GENMASK(15, 0)) << 16); 194ebd2a82eSAlex Elder case STATUS_TAG_HIGH16: 195ebd2a82eSAlex Elder return le32_get_bits(word[5], GENMASK(31, 16)); 196ebd2a82eSAlex Elder case STATUS_SEQUENCE: 197ebd2a82eSAlex Elder return le32_get_bits(word[6], GENMASK(7, 0)); 198ebd2a82eSAlex Elder case STATUS_TIME_OF_DAY: 199ebd2a82eSAlex Elder return le32_get_bits(word[6], GENMASK(31, 8)); 200ebd2a82eSAlex Elder case STATUS_HEADER_LOCAL: 201ebd2a82eSAlex Elder return le32_get_bits(word[7], GENMASK(0, 0)); 202ebd2a82eSAlex Elder case STATUS_HEADER_OFFSET: 203ebd2a82eSAlex Elder return le32_get_bits(word[7], GENMASK(10, 1)); 204ebd2a82eSAlex Elder case STATUS_FRAG_HIT: 205ebd2a82eSAlex Elder return le32_get_bits(word[7], GENMASK(11, 11)); 206ebd2a82eSAlex Elder case STATUS_FRAG_RULE_INDEX: 207ebd2a82eSAlex Elder return le32_get_bits(word[7], GENMASK(15, 12)); 20855c6eae7SAlex Elder /* Status word 7, bits 16-30 are reserved */ 20955c6eae7SAlex Elder /* Status word 7, bit 31 is reserved (not IPA v5.0+) */ 210ebd2a82eSAlex Elder default: 211ebd2a82eSAlex Elder WARN(true, "%s: bad field_id %u\n", __func__, field); 212ebd2a82eSAlex Elder return 0; 213ebd2a82eSAlex Elder } 214ebd2a82eSAlex Elder } 215ebd2a82eSAlex Elder 2163cebb7c2SAlex Elder /* Compute the aggregation size value to use for a given buffer size */ 2173cebb7c2SAlex Elder static u32 ipa_aggr_size_kb(u32 rx_buffer_size, bool aggr_hard_limit) 2183cebb7c2SAlex Elder { 2193cebb7c2SAlex Elder /* A hard aggregation limit will not be crossed; aggregation closes 2203cebb7c2SAlex Elder * if saving incoming data would cross the hard byte limit boundary. 2213cebb7c2SAlex Elder * 2223cebb7c2SAlex Elder * With a soft limit, aggregation closes *after* the size boundary 2233cebb7c2SAlex Elder * has been crossed. In that case the limit must leave enough space 2243cebb7c2SAlex Elder * after that limit to receive a full MTU of data plus overhead. 2253cebb7c2SAlex Elder */ 2263cebb7c2SAlex Elder if (!aggr_hard_limit) 2273cebb7c2SAlex Elder rx_buffer_size -= IPA_MTU + IPA_RX_BUFFER_OVERHEAD; 2283cebb7c2SAlex Elder 2293cebb7c2SAlex Elder /* The byte limit is encoded as a number of kilobytes */ 2303cebb7c2SAlex Elder 2313cebb7c2SAlex Elder return rx_buffer_size / SZ_1K; 2323cebb7c2SAlex Elder } 2333cebb7c2SAlex Elder 23484f9bd12SAlex Elder static bool ipa_endpoint_data_valid_one(struct ipa *ipa, u32 count, 23584f9bd12SAlex Elder const struct ipa_gsi_endpoint_data *all_data, 23684f9bd12SAlex Elder const struct ipa_gsi_endpoint_data *data) 23784f9bd12SAlex Elder { 23884f9bd12SAlex Elder const struct ipa_gsi_endpoint_data *other_data; 23984f9bd12SAlex Elder enum ipa_endpoint_name other_name; 2405245f4fdSAlex Elder struct device *dev = ipa->dev; 24184f9bd12SAlex Elder 24284f9bd12SAlex Elder if (ipa_gsi_endpoint_data_empty(data)) 24384f9bd12SAlex Elder return true; 24484f9bd12SAlex Elder 24584f9bd12SAlex Elder if (!data->toward_ipa) { 2463cebb7c2SAlex Elder const struct ipa_endpoint_rx *rx_config; 24781772e44SAlex Elder const struct reg *reg; 248ed23f026SAlex Elder u32 buffer_size; 2493cebb7c2SAlex Elder u32 aggr_size; 250ed23f026SAlex Elder u32 limit; 251ed23f026SAlex Elder 25284f9bd12SAlex Elder if (data->endpoint.filter_support) { 25384f9bd12SAlex Elder dev_err(dev, "filtering not supported for " 25484f9bd12SAlex Elder "RX endpoint %u\n", 25584f9bd12SAlex Elder data->endpoint_id); 25684f9bd12SAlex Elder return false; 25784f9bd12SAlex Elder } 25884f9bd12SAlex Elder 259ed23f026SAlex Elder /* Nothing more to check for non-AP RX */ 260ed23f026SAlex Elder if (data->ee_id != GSI_EE_AP) 261ed23f026SAlex Elder return true; 262ed23f026SAlex Elder 2633cebb7c2SAlex Elder rx_config = &data->endpoint.config.rx; 2643cebb7c2SAlex Elder 265ed23f026SAlex Elder /* The buffer size must hold an MTU plus overhead */ 2663cebb7c2SAlex Elder buffer_size = rx_config->buffer_size; 267ed23f026SAlex Elder limit = IPA_MTU + IPA_RX_BUFFER_OVERHEAD; 268ed23f026SAlex Elder if (buffer_size < limit) { 269ed23f026SAlex Elder dev_err(dev, "RX buffer size too small for RX endpoint %u (%u < %u)\n", 270ed23f026SAlex Elder data->endpoint_id, buffer_size, limit); 271ed23f026SAlex Elder return false; 272ed23f026SAlex Elder } 273ed23f026SAlex Elder 2743cebb7c2SAlex Elder if (!data->endpoint.config.aggregation) { 2753cebb7c2SAlex Elder bool result = true; 2763cebb7c2SAlex Elder 2773cebb7c2SAlex Elder /* No aggregation; check for bogus aggregation data */ 278beb90cbaSAlex Elder if (rx_config->aggr_time_limit) { 279beb90cbaSAlex Elder dev_err(dev, 280beb90cbaSAlex Elder "time limit with no aggregation for RX endpoint %u\n", 281beb90cbaSAlex Elder data->endpoint_id); 282beb90cbaSAlex Elder result = false; 283beb90cbaSAlex Elder } 284beb90cbaSAlex Elder 2853cebb7c2SAlex Elder if (rx_config->aggr_hard_limit) { 2863cebb7c2SAlex Elder dev_err(dev, "hard limit with no aggregation for RX endpoint %u\n", 2873cebb7c2SAlex Elder data->endpoint_id); 2883cebb7c2SAlex Elder result = false; 2893cebb7c2SAlex Elder } 2903cebb7c2SAlex Elder 2913cebb7c2SAlex Elder if (rx_config->aggr_close_eof) { 2923cebb7c2SAlex Elder dev_err(dev, "close EOF with no aggregation for RX endpoint %u\n", 2933cebb7c2SAlex Elder data->endpoint_id); 2943cebb7c2SAlex Elder result = false; 2953cebb7c2SAlex Elder } 2963cebb7c2SAlex Elder 2973cebb7c2SAlex Elder return result; /* Nothing more to check */ 2983cebb7c2SAlex Elder } 2993cebb7c2SAlex Elder 3003cebb7c2SAlex Elder /* For an endpoint supporting receive aggregation, the byte 3013cebb7c2SAlex Elder * limit defines the point at which aggregation closes. This 3023cebb7c2SAlex Elder * check ensures the receive buffer size doesn't result in a 3033cebb7c2SAlex Elder * limit that exceeds what's representable in the aggregation 3043cebb7c2SAlex Elder * byte limit field. 305ed23f026SAlex Elder */ 3063cebb7c2SAlex Elder aggr_size = ipa_aggr_size_kb(buffer_size - NET_SKB_PAD, 3073cebb7c2SAlex Elder rx_config->aggr_hard_limit); 308216b409dSAlex Elder reg = ipa_reg(ipa, ENDP_INIT_AGGR); 309216b409dSAlex Elder 310f1470fd7SAlex Elder limit = reg_field_max(reg, BYTE_LIMIT); 3113cebb7c2SAlex Elder if (aggr_size > limit) { 3123cebb7c2SAlex Elder dev_err(dev, "aggregated size too large for RX endpoint %u (%u KB > %u KB)\n", 3133cebb7c2SAlex Elder data->endpoint_id, aggr_size, limit); 314ed23f026SAlex Elder 315ed23f026SAlex Elder return false; 316ed23f026SAlex Elder } 317ed23f026SAlex Elder 31884f9bd12SAlex Elder return true; /* Nothing more to check for RX */ 31984f9bd12SAlex Elder } 32084f9bd12SAlex Elder 321a14d5937SAlex Elder /* Starting with IPA v4.5 sequencer replication is obsolete */ 322a14d5937SAlex Elder if (ipa->version >= IPA_VERSION_4_5) { 323a14d5937SAlex Elder if (data->endpoint.config.tx.seq_rep_type) { 324a14d5937SAlex Elder dev_err(dev, "no-zero seq_rep_type TX endpoint %u\n", 325a14d5937SAlex Elder data->endpoint_id); 326a14d5937SAlex Elder return false; 327a14d5937SAlex Elder } 328a14d5937SAlex Elder } 329a14d5937SAlex Elder 33084f9bd12SAlex Elder if (data->endpoint.config.status_enable) { 33184f9bd12SAlex Elder other_name = data->endpoint.config.tx.status_endpoint; 33284f9bd12SAlex Elder if (other_name >= count) { 33384f9bd12SAlex Elder dev_err(dev, "status endpoint name %u out of range " 33484f9bd12SAlex Elder "for endpoint %u\n", 33584f9bd12SAlex Elder other_name, data->endpoint_id); 33684f9bd12SAlex Elder return false; 33784f9bd12SAlex Elder } 33884f9bd12SAlex Elder 33984f9bd12SAlex Elder /* Status endpoint must be defined... */ 34084f9bd12SAlex Elder other_data = &all_data[other_name]; 34184f9bd12SAlex Elder if (ipa_gsi_endpoint_data_empty(other_data)) { 34284f9bd12SAlex Elder dev_err(dev, "DMA endpoint name %u undefined " 34384f9bd12SAlex Elder "for endpoint %u\n", 34484f9bd12SAlex Elder other_name, data->endpoint_id); 34584f9bd12SAlex Elder return false; 34684f9bd12SAlex Elder } 34784f9bd12SAlex Elder 34884f9bd12SAlex Elder /* ...and has to be an RX endpoint... */ 34984f9bd12SAlex Elder if (other_data->toward_ipa) { 35084f9bd12SAlex Elder dev_err(dev, 35184f9bd12SAlex Elder "status endpoint for endpoint %u not RX\n", 35284f9bd12SAlex Elder data->endpoint_id); 35384f9bd12SAlex Elder return false; 35484f9bd12SAlex Elder } 35584f9bd12SAlex Elder 35684f9bd12SAlex Elder /* ...and if it's to be an AP endpoint... */ 35784f9bd12SAlex Elder if (other_data->ee_id == GSI_EE_AP) { 35884f9bd12SAlex Elder /* ...make sure it has status enabled. */ 35984f9bd12SAlex Elder if (!other_data->endpoint.config.status_enable) { 36084f9bd12SAlex Elder dev_err(dev, 36184f9bd12SAlex Elder "status not enabled for endpoint %u\n", 36284f9bd12SAlex Elder other_data->endpoint_id); 36384f9bd12SAlex Elder return false; 36484f9bd12SAlex Elder } 36584f9bd12SAlex Elder } 36684f9bd12SAlex Elder } 36784f9bd12SAlex Elder 36884f9bd12SAlex Elder if (data->endpoint.config.dma_mode) { 36984f9bd12SAlex Elder other_name = data->endpoint.config.dma_endpoint; 37084f9bd12SAlex Elder if (other_name >= count) { 37184f9bd12SAlex Elder dev_err(dev, "DMA endpoint name %u out of range " 37284f9bd12SAlex Elder "for endpoint %u\n", 37384f9bd12SAlex Elder other_name, data->endpoint_id); 37484f9bd12SAlex Elder return false; 37584f9bd12SAlex Elder } 37684f9bd12SAlex Elder 37784f9bd12SAlex Elder other_data = &all_data[other_name]; 37884f9bd12SAlex Elder if (ipa_gsi_endpoint_data_empty(other_data)) { 37984f9bd12SAlex Elder dev_err(dev, "DMA endpoint name %u undefined " 38084f9bd12SAlex Elder "for endpoint %u\n", 38184f9bd12SAlex Elder other_name, data->endpoint_id); 38284f9bd12SAlex Elder return false; 38384f9bd12SAlex Elder } 38484f9bd12SAlex Elder } 38584f9bd12SAlex Elder 38684f9bd12SAlex Elder return true; 38784f9bd12SAlex Elder } 38884f9bd12SAlex Elder 3895274c715SAlex Elder /* Validate endpoint configuration data. Return max defined endpoint ID */ 3905274c715SAlex Elder static u32 ipa_endpoint_max(struct ipa *ipa, u32 count, 39184f9bd12SAlex Elder const struct ipa_gsi_endpoint_data *data) 39284f9bd12SAlex Elder { 39384f9bd12SAlex Elder const struct ipa_gsi_endpoint_data *dp = data; 3945245f4fdSAlex Elder struct device *dev = ipa->dev; 39584f9bd12SAlex Elder enum ipa_endpoint_name name; 3965274c715SAlex Elder u32 max; 39784f9bd12SAlex Elder 39884f9bd12SAlex Elder if (count > IPA_ENDPOINT_COUNT) { 39984f9bd12SAlex Elder dev_err(dev, "too many endpoints specified (%u > %u)\n", 40084f9bd12SAlex Elder count, IPA_ENDPOINT_COUNT); 4015274c715SAlex Elder return 0; 40284f9bd12SAlex Elder } 40384f9bd12SAlex Elder 40484f9bd12SAlex Elder /* Make sure needed endpoints have defined data */ 40584f9bd12SAlex Elder if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_COMMAND_TX])) { 40684f9bd12SAlex Elder dev_err(dev, "command TX endpoint not defined\n"); 4075274c715SAlex Elder return 0; 40884f9bd12SAlex Elder } 40984f9bd12SAlex Elder if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_LAN_RX])) { 41084f9bd12SAlex Elder dev_err(dev, "LAN RX endpoint not defined\n"); 4115274c715SAlex Elder return 0; 41284f9bd12SAlex Elder } 41384f9bd12SAlex Elder if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_MODEM_TX])) { 41484f9bd12SAlex Elder dev_err(dev, "AP->modem TX endpoint not defined\n"); 4155274c715SAlex Elder return 0; 41684f9bd12SAlex Elder } 41784f9bd12SAlex Elder if (ipa_gsi_endpoint_data_empty(&data[IPA_ENDPOINT_AP_MODEM_RX])) { 41884f9bd12SAlex Elder dev_err(dev, "AP<-modem RX endpoint not defined\n"); 4195274c715SAlex Elder return 0; 42084f9bd12SAlex Elder } 42184f9bd12SAlex Elder 4225274c715SAlex Elder max = 0; 4235274c715SAlex Elder for (name = 0; name < count; name++, dp++) { 42484f9bd12SAlex Elder if (!ipa_endpoint_data_valid_one(ipa, count, data, dp)) 4255274c715SAlex Elder return 0; 4265274c715SAlex Elder max = max_t(u32, max, dp->endpoint_id); 4275274c715SAlex Elder } 42884f9bd12SAlex Elder 4295274c715SAlex Elder return max; 43084f9bd12SAlex Elder } 43184f9bd12SAlex Elder 43284f9bd12SAlex Elder /* Allocate a transaction to use on a non-command endpoint */ 43384f9bd12SAlex Elder static struct gsi_trans *ipa_endpoint_trans_alloc(struct ipa_endpoint *endpoint, 43484f9bd12SAlex Elder u32 tre_count) 43584f9bd12SAlex Elder { 43684f9bd12SAlex Elder struct gsi *gsi = &endpoint->ipa->gsi; 43784f9bd12SAlex Elder u32 channel_id = endpoint->channel_id; 43884f9bd12SAlex Elder enum dma_data_direction direction; 43984f9bd12SAlex Elder 44084f9bd12SAlex Elder direction = endpoint->toward_ipa ? DMA_TO_DEVICE : DMA_FROM_DEVICE; 44184f9bd12SAlex Elder 44284f9bd12SAlex Elder return gsi_channel_trans_alloc(gsi, channel_id, tre_count, direction); 44384f9bd12SAlex Elder } 44484f9bd12SAlex Elder 44584f9bd12SAlex Elder /* suspend_delay represents suspend for RX, delay for TX endpoints. 4464c9d631aSAlex Elder * Note that suspend is not supported starting with IPA v4.0, and 4474c9d631aSAlex Elder * delay mode should not be used starting with IPA v4.2. 44884f9bd12SAlex Elder */ 4494900bf34SAlex Elder static bool 45084f9bd12SAlex Elder ipa_endpoint_init_ctrl(struct ipa_endpoint *endpoint, bool suspend_delay) 45184f9bd12SAlex Elder { 45284f9bd12SAlex Elder struct ipa *ipa = endpoint->ipa; 45381772e44SAlex Elder const struct reg *reg; 4544468a344SAlex Elder u32 field_id; 4556bfb7538SAlex Elder u32 offset; 4564900bf34SAlex Elder bool state; 45784f9bd12SAlex Elder u32 mask; 45884f9bd12SAlex Elder u32 val; 45984f9bd12SAlex Elder 4605bc55884SAlex Elder if (endpoint->toward_ipa) 4614c9d631aSAlex Elder WARN_ON(ipa->version >= IPA_VERSION_4_2); 4625bc55884SAlex Elder else 4635bc55884SAlex Elder WARN_ON(ipa->version >= IPA_VERSION_4_0); 4645bc55884SAlex Elder 4656a244b75SAlex Elder reg = ipa_reg(ipa, ENDP_INIT_CTRL); 466fc4cecf7SAlex Elder offset = reg_n_offset(reg, endpoint->endpoint_id); 46784f9bd12SAlex Elder val = ioread32(ipa->reg_virt + offset); 4686a244b75SAlex Elder 4694468a344SAlex Elder field_id = endpoint->toward_ipa ? ENDP_DELAY : ENDP_SUSPEND; 470f1470fd7SAlex Elder mask = reg_bit(reg, field_id); 4714468a344SAlex Elder 4724900bf34SAlex Elder state = !!(val & mask); 4735bc55884SAlex Elder 4745bc55884SAlex Elder /* Don't bother if it's already in the requested state */ 4754900bf34SAlex Elder if (suspend_delay != state) { 47684f9bd12SAlex Elder val ^= mask; 47784f9bd12SAlex Elder iowrite32(val, ipa->reg_virt + offset); 4784900bf34SAlex Elder } 47984f9bd12SAlex Elder 4804900bf34SAlex Elder return state; 48184f9bd12SAlex Elder } 48284f9bd12SAlex Elder 4834c9d631aSAlex Elder /* We don't care what the previous state was for delay mode */ 4844fa95248SAlex Elder static void 4854fa95248SAlex Elder ipa_endpoint_program_delay(struct ipa_endpoint *endpoint, bool enable) 4864fa95248SAlex Elder { 4874c9d631aSAlex Elder /* Delay mode should not be used for IPA v4.2+ */ 4884c9d631aSAlex Elder WARN_ON(endpoint->ipa->version >= IPA_VERSION_4_2); 4895bc55884SAlex Elder WARN_ON(!endpoint->toward_ipa); 4904fa95248SAlex Elder 4914fa95248SAlex Elder (void)ipa_endpoint_init_ctrl(endpoint, enable); 4924fa95248SAlex Elder } 4934fa95248SAlex Elder 494fff89971SAlex Elder static bool ipa_endpoint_aggr_active(struct ipa_endpoint *endpoint) 495fff89971SAlex Elder { 4961d8f16dbSAlex Elder u32 endpoint_id = endpoint->endpoint_id; 497fff89971SAlex Elder struct ipa *ipa = endpoint->ipa; 4981d8f16dbSAlex Elder u32 unit = endpoint_id / 32; 49981772e44SAlex Elder const struct reg *reg; 500fff89971SAlex Elder u32 val; 501fff89971SAlex Elder 50288de7672SAlex Elder WARN_ON(!test_bit(endpoint_id, ipa->available)); 5035bc55884SAlex Elder 5046a244b75SAlex Elder reg = ipa_reg(ipa, STATE_AGGR_ACTIVE); 505fc4cecf7SAlex Elder val = ioread32(ipa->reg_virt + reg_n_offset(reg, unit)); 506fff89971SAlex Elder 50788de7672SAlex Elder return !!(val & BIT(endpoint_id % 32)); 508fff89971SAlex Elder } 509fff89971SAlex Elder 510fff89971SAlex Elder static void ipa_endpoint_force_close(struct ipa_endpoint *endpoint) 511fff89971SAlex Elder { 5121d8f16dbSAlex Elder u32 endpoint_id = endpoint->endpoint_id; 5131d8f16dbSAlex Elder u32 mask = BIT(endpoint_id % 32); 514fff89971SAlex Elder struct ipa *ipa = endpoint->ipa; 5151d8f16dbSAlex Elder u32 unit = endpoint_id / 32; 51681772e44SAlex Elder const struct reg *reg; 517fff89971SAlex Elder 51888de7672SAlex Elder WARN_ON(!test_bit(endpoint_id, ipa->available)); 5195bc55884SAlex Elder 5206a244b75SAlex Elder reg = ipa_reg(ipa, AGGR_FORCE_CLOSE); 521fc4cecf7SAlex Elder iowrite32(mask, ipa->reg_virt + reg_n_offset(reg, unit)); 522fff89971SAlex Elder } 523fff89971SAlex Elder 524fff89971SAlex Elder /** 525fff89971SAlex Elder * ipa_endpoint_suspend_aggr() - Emulate suspend interrupt 526e3eea08eSAlex Elder * @endpoint: Endpoint on which to emulate a suspend 527fff89971SAlex Elder * 528fff89971SAlex Elder * Emulate suspend IPA interrupt to unsuspend an endpoint suspended 529fff89971SAlex Elder * with an open aggregation frame. This is to work around a hardware 530fff89971SAlex Elder * issue in IPA version 3.5.1 where the suspend interrupt will not be 531fff89971SAlex Elder * generated when it should be. 532fff89971SAlex Elder */ 533fff89971SAlex Elder static void ipa_endpoint_suspend_aggr(struct ipa_endpoint *endpoint) 534fff89971SAlex Elder { 535fff89971SAlex Elder struct ipa *ipa = endpoint->ipa; 536fff89971SAlex Elder 537660e52d6SAlex Elder if (!endpoint->config.aggregation) 538fff89971SAlex Elder return; 539fff89971SAlex Elder 540fff89971SAlex Elder /* Nothing to do if the endpoint doesn't have aggregation open */ 541fff89971SAlex Elder if (!ipa_endpoint_aggr_active(endpoint)) 542fff89971SAlex Elder return; 543fff89971SAlex Elder 544fff89971SAlex Elder /* Force close aggregation */ 545fff89971SAlex Elder ipa_endpoint_force_close(endpoint); 546fff89971SAlex Elder 547fff89971SAlex Elder ipa_interrupt_simulate_suspend(ipa->interrupt); 548fff89971SAlex Elder } 549fff89971SAlex Elder 550fff89971SAlex Elder /* Returns previous suspend state (true means suspend was enabled) */ 5514fa95248SAlex Elder static bool 5524fa95248SAlex Elder ipa_endpoint_program_suspend(struct ipa_endpoint *endpoint, bool enable) 5534fa95248SAlex Elder { 554fff89971SAlex Elder bool suspended; 555fff89971SAlex Elder 556d7f3087bSAlex Elder if (endpoint->ipa->version >= IPA_VERSION_4_0) 557b07f283eSAlex Elder return enable; /* For IPA v4.0+, no change made */ 558b07f283eSAlex Elder 5595bc55884SAlex Elder WARN_ON(endpoint->toward_ipa); 5604fa95248SAlex Elder 561fff89971SAlex Elder suspended = ipa_endpoint_init_ctrl(endpoint, enable); 562fff89971SAlex Elder 563fff89971SAlex Elder /* A client suspended with an open aggregation frame will not 564fff89971SAlex Elder * generate a SUSPEND IPA interrupt. If enabling suspend, have 565fff89971SAlex Elder * ipa_endpoint_suspend_aggr() handle this. 566fff89971SAlex Elder */ 567fff89971SAlex Elder if (enable && !suspended) 568fff89971SAlex Elder ipa_endpoint_suspend_aggr(endpoint); 569fff89971SAlex Elder 570fff89971SAlex Elder return suspended; 5714fa95248SAlex Elder } 5724fa95248SAlex Elder 5734c9d631aSAlex Elder /* Put all modem RX endpoints into suspend mode, and stop transmission 5744c9d631aSAlex Elder * on all modem TX endpoints. Prior to IPA v4.2, endpoint DELAY mode is 5754c9d631aSAlex Elder * used for TX endpoints; starting with IPA v4.2 we use GSI channel flow 5764c9d631aSAlex Elder * control instead. 5774c9d631aSAlex Elder */ 57884f9bd12SAlex Elder void ipa_endpoint_modem_pause_all(struct ipa *ipa, bool enable) 57984f9bd12SAlex Elder { 580e359ba89SAlex Elder u32 endpoint_id = 0; 58184f9bd12SAlex Elder 582b7aaff0bSAlex Elder while (endpoint_id < ipa->endpoint_count) { 583e359ba89SAlex Elder struct ipa_endpoint *endpoint = &ipa->endpoint[endpoint_id++]; 58484f9bd12SAlex Elder 58584f9bd12SAlex Elder if (endpoint->ee_id != GSI_EE_MODEM) 58684f9bd12SAlex Elder continue; 58784f9bd12SAlex Elder 5884c9d631aSAlex Elder if (!endpoint->toward_ipa) 5894c9d631aSAlex Elder (void)ipa_endpoint_program_suspend(endpoint, enable); 5904c9d631aSAlex Elder else if (ipa->version < IPA_VERSION_4_2) 5914fa95248SAlex Elder ipa_endpoint_program_delay(endpoint, enable); 592b07f283eSAlex Elder else 5934c9d631aSAlex Elder gsi_modem_channel_flow_control(&ipa->gsi, 5944c9d631aSAlex Elder endpoint->channel_id, 5954c9d631aSAlex Elder enable); 59684f9bd12SAlex Elder } 59784f9bd12SAlex Elder } 59884f9bd12SAlex Elder 59984f9bd12SAlex Elder /* Reset all modem endpoints to use the default exception endpoint */ 60084f9bd12SAlex Elder int ipa_endpoint_modem_exception_reset_all(struct ipa *ipa) 60184f9bd12SAlex Elder { 60284f9bd12SAlex Elder struct gsi_trans *trans; 6039a9f5129SAlex Elder u32 endpoint_id; 60484f9bd12SAlex Elder u32 count; 60584f9bd12SAlex Elder 6062091c79aSAlex Elder /* We need one command per modem TX endpoint, plus the commands 6072091c79aSAlex Elder * that clear the pipeline. 60884f9bd12SAlex Elder */ 6092091c79aSAlex Elder count = ipa->modem_tx_count + ipa_cmd_pipeline_clear_count(); 61084f9bd12SAlex Elder trans = ipa_cmd_trans_alloc(ipa, count); 61184f9bd12SAlex Elder if (!trans) { 6125245f4fdSAlex Elder dev_err(ipa->dev, 61384f9bd12SAlex Elder "no transaction to reset modem exception endpoints\n"); 61484f9bd12SAlex Elder return -EBUSY; 61584f9bd12SAlex Elder } 61684f9bd12SAlex Elder 6179a9f5129SAlex Elder for_each_set_bit(endpoint_id, ipa->defined, ipa->endpoint_count) { 61884f9bd12SAlex Elder struct ipa_endpoint *endpoint; 61981772e44SAlex Elder const struct reg *reg; 62084f9bd12SAlex Elder u32 offset; 62184f9bd12SAlex Elder 62284f9bd12SAlex Elder /* We only reset modem TX endpoints */ 62384f9bd12SAlex Elder endpoint = &ipa->endpoint[endpoint_id]; 62484f9bd12SAlex Elder if (!(endpoint->ee_id == GSI_EE_MODEM && endpoint->toward_ipa)) 62584f9bd12SAlex Elder continue; 62684f9bd12SAlex Elder 6276a244b75SAlex Elder reg = ipa_reg(ipa, ENDP_STATUS); 628fc4cecf7SAlex Elder offset = reg_n_offset(reg, endpoint_id); 62984f9bd12SAlex Elder 63084f9bd12SAlex Elder /* Value written is 0, and all bits are updated. That 63184f9bd12SAlex Elder * means status is disabled on the endpoint, and as a 63284f9bd12SAlex Elder * result all other fields in the register are ignored. 63384f9bd12SAlex Elder */ 63484f9bd12SAlex Elder ipa_cmd_register_write_add(trans, offset, 0, ~0, false); 63584f9bd12SAlex Elder } 63684f9bd12SAlex Elder 637aa56e3e5SAlex Elder ipa_cmd_pipeline_clear_add(trans); 63884f9bd12SAlex Elder 63984f9bd12SAlex Elder gsi_trans_commit_wait(trans); 64084f9bd12SAlex Elder 64151c48ce2SAlex Elder ipa_cmd_pipeline_clear_wait(ipa); 64251c48ce2SAlex Elder 64384f9bd12SAlex Elder return 0; 64484f9bd12SAlex Elder } 64584f9bd12SAlex Elder 64684f9bd12SAlex Elder static void ipa_endpoint_init_cfg(struct ipa_endpoint *endpoint) 64784f9bd12SAlex Elder { 6486a244b75SAlex Elder u32 endpoint_id = endpoint->endpoint_id; 6496bfb7538SAlex Elder struct ipa *ipa = endpoint->ipa; 6505567d4d9SAlex Elder enum ipa_cs_offload_en enabled; 65181772e44SAlex Elder const struct reg *reg; 65284f9bd12SAlex Elder u32 val = 0; 6536bfb7538SAlex Elder 6546a244b75SAlex Elder reg = ipa_reg(ipa, ENDP_INIT_CFG); 65584f9bd12SAlex Elder /* FRAG_OFFLOAD_EN is 0 */ 656660e52d6SAlex Elder if (endpoint->config.checksum) { 6576bfb7538SAlex Elder enum ipa_version version = ipa->version; 6585567d4d9SAlex Elder 65984f9bd12SAlex Elder if (endpoint->toward_ipa) { 6609eefd2fbSAlex Elder u32 off; 66184f9bd12SAlex Elder 66284f9bd12SAlex Elder /* Checksum header offset is in 4-byte units */ 6634468a344SAlex Elder off = sizeof(struct rmnet_map_header) / sizeof(u32); 664f1470fd7SAlex Elder val |= reg_encode(reg, CS_METADATA_HDR_OFFSET, off); 6655567d4d9SAlex Elder 6665567d4d9SAlex Elder enabled = version < IPA_VERSION_4_5 6675567d4d9SAlex Elder ? IPA_CS_OFFLOAD_UL 6685567d4d9SAlex Elder : IPA_CS_OFFLOAD_INLINE; 66984f9bd12SAlex Elder } else { 6705567d4d9SAlex Elder enabled = version < IPA_VERSION_4_5 6715567d4d9SAlex Elder ? IPA_CS_OFFLOAD_DL 6725567d4d9SAlex Elder : IPA_CS_OFFLOAD_INLINE; 67384f9bd12SAlex Elder } 67484f9bd12SAlex Elder } else { 6755567d4d9SAlex Elder enabled = IPA_CS_OFFLOAD_NONE; 67684f9bd12SAlex Elder } 677f1470fd7SAlex Elder val |= reg_encode(reg, CS_OFFLOAD_EN, enabled); 67884f9bd12SAlex Elder /* CS_GEN_QMB_MASTER_SEL is 0 */ 67984f9bd12SAlex Elder 680fc4cecf7SAlex Elder iowrite32(val, ipa->reg_virt + reg_n_offset(reg, endpoint_id)); 68184f9bd12SAlex Elder } 68284f9bd12SAlex Elder 683647a05f3SAlex Elder static void ipa_endpoint_init_nat(struct ipa_endpoint *endpoint) 684647a05f3SAlex Elder { 6856a244b75SAlex Elder u32 endpoint_id = endpoint->endpoint_id; 6866bfb7538SAlex Elder struct ipa *ipa = endpoint->ipa; 68781772e44SAlex Elder const struct reg *reg; 688647a05f3SAlex Elder u32 val; 689647a05f3SAlex Elder 690647a05f3SAlex Elder if (!endpoint->toward_ipa) 691647a05f3SAlex Elder return; 692647a05f3SAlex Elder 6936a244b75SAlex Elder reg = ipa_reg(ipa, ENDP_INIT_NAT); 694f1470fd7SAlex Elder val = reg_encode(reg, NAT_EN, IPA_NAT_TYPE_BYPASS); 695647a05f3SAlex Elder 696fc4cecf7SAlex Elder iowrite32(val, ipa->reg_virt + reg_n_offset(reg, endpoint_id)); 697647a05f3SAlex Elder } 698647a05f3SAlex Elder 6995567d4d9SAlex Elder static u32 7005567d4d9SAlex Elder ipa_qmap_header_size(enum ipa_version version, struct ipa_endpoint *endpoint) 7015567d4d9SAlex Elder { 7025567d4d9SAlex Elder u32 header_size = sizeof(struct rmnet_map_header); 7035567d4d9SAlex Elder 7045567d4d9SAlex Elder /* Without checksum offload, we just have the MAP header */ 705660e52d6SAlex Elder if (!endpoint->config.checksum) 7065567d4d9SAlex Elder return header_size; 7075567d4d9SAlex Elder 7085567d4d9SAlex Elder if (version < IPA_VERSION_4_5) { 7095567d4d9SAlex Elder /* Checksum header inserted for AP TX endpoints only */ 7105567d4d9SAlex Elder if (endpoint->toward_ipa) 7115567d4d9SAlex Elder header_size += sizeof(struct rmnet_map_ul_csum_header); 7125567d4d9SAlex Elder } else { 7135567d4d9SAlex Elder /* Checksum header is used in both directions */ 7145567d4d9SAlex Elder header_size += sizeof(struct rmnet_map_v5_csum_header); 7155567d4d9SAlex Elder } 7165567d4d9SAlex Elder 7175567d4d9SAlex Elder return header_size; 7185567d4d9SAlex Elder } 7195567d4d9SAlex Elder 7204468a344SAlex Elder /* Encoded value for ENDP_INIT_HDR register HDR_LEN* field(s) */ 7214468a344SAlex Elder static u32 ipa_header_size_encode(enum ipa_version version, 72281772e44SAlex Elder const struct reg *reg, u32 header_size) 7234468a344SAlex Elder { 724f1470fd7SAlex Elder u32 field_max = reg_field_max(reg, HDR_LEN); 7254468a344SAlex Elder u32 val; 7264468a344SAlex Elder 7274468a344SAlex Elder /* We know field_max can be used as a mask (2^n - 1) */ 728f1470fd7SAlex Elder val = reg_encode(reg, HDR_LEN, header_size & field_max); 7294468a344SAlex Elder if (version < IPA_VERSION_4_5) { 7304468a344SAlex Elder WARN_ON(header_size > field_max); 7314468a344SAlex Elder return val; 7324468a344SAlex Elder } 7334468a344SAlex Elder 7344468a344SAlex Elder /* IPA v4.5 adds a few more most-significant bits */ 7354468a344SAlex Elder header_size >>= hweight32(field_max); 736f1470fd7SAlex Elder WARN_ON(header_size > reg_field_max(reg, HDR_LEN_MSB)); 737f1470fd7SAlex Elder val |= reg_encode(reg, HDR_LEN_MSB, header_size); 7384468a344SAlex Elder 7394468a344SAlex Elder return val; 7404468a344SAlex Elder } 7414468a344SAlex Elder 7424468a344SAlex Elder /* Encoded value for ENDP_INIT_HDR register OFST_METADATA* field(s) */ 7434468a344SAlex Elder static u32 ipa_metadata_offset_encode(enum ipa_version version, 74481772e44SAlex Elder const struct reg *reg, u32 offset) 7454468a344SAlex Elder { 746f1470fd7SAlex Elder u32 field_max = reg_field_max(reg, HDR_OFST_METADATA); 7474468a344SAlex Elder u32 val; 7484468a344SAlex Elder 7494468a344SAlex Elder /* We know field_max can be used as a mask (2^n - 1) */ 750f1470fd7SAlex Elder val = reg_encode(reg, HDR_OFST_METADATA, offset); 7514468a344SAlex Elder if (version < IPA_VERSION_4_5) { 7524468a344SAlex Elder WARN_ON(offset > field_max); 7534468a344SAlex Elder return val; 7544468a344SAlex Elder } 7554468a344SAlex Elder 7564468a344SAlex Elder /* IPA v4.5 adds a few more most-significant bits */ 7574468a344SAlex Elder offset >>= hweight32(field_max); 758f1470fd7SAlex Elder WARN_ON(offset > reg_field_max(reg, HDR_OFST_METADATA_MSB)); 759f1470fd7SAlex Elder val |= reg_encode(reg, HDR_OFST_METADATA_MSB, offset); 7604468a344SAlex Elder 7614468a344SAlex Elder return val; 7624468a344SAlex Elder } 7634468a344SAlex Elder 7648730f45dSAlex Elder /** 765e3eea08eSAlex Elder * ipa_endpoint_init_hdr() - Initialize HDR endpoint configuration register 766e3eea08eSAlex Elder * @endpoint: Endpoint pointer 767e3eea08eSAlex Elder * 7688730f45dSAlex Elder * We program QMAP endpoints so each packet received is preceded by a QMAP 7698730f45dSAlex Elder * header structure. The QMAP header contains a 1-byte mux_id and 2-byte 7708730f45dSAlex Elder * packet size field, and we have the IPA hardware populate both for each 7718730f45dSAlex Elder * received packet. The header is configured (in the HDR_EXT register) 7728730f45dSAlex Elder * to use big endian format. 7738730f45dSAlex Elder * 7748730f45dSAlex Elder * The packet size is written into the QMAP header's pkt_len field. That 7758730f45dSAlex Elder * location is defined here using the HDR_OFST_PKT_SIZE field. 7768730f45dSAlex Elder * 7778730f45dSAlex Elder * The mux_id comes from a 4-byte metadata value supplied with each packet 7788730f45dSAlex Elder * by the modem. It is *not* a QMAP header, but it does contain the mux_id 7798730f45dSAlex Elder * value that we want, in its low-order byte. A bitmask defined in the 7808730f45dSAlex Elder * endpoint's METADATA_MASK register defines which byte within the modem 7818730f45dSAlex Elder * metadata contains the mux_id. And the OFST_METADATA field programmed 7828730f45dSAlex Elder * here indicates where the extracted byte should be placed within the QMAP 7838730f45dSAlex Elder * header. 7848730f45dSAlex Elder */ 78584f9bd12SAlex Elder static void ipa_endpoint_init_hdr(struct ipa_endpoint *endpoint) 78684f9bd12SAlex Elder { 7876a244b75SAlex Elder u32 endpoint_id = endpoint->endpoint_id; 7881af15c2aSAlex Elder struct ipa *ipa = endpoint->ipa; 78981772e44SAlex Elder const struct reg *reg; 79084f9bd12SAlex Elder u32 val = 0; 7916bfb7538SAlex Elder 7926a244b75SAlex Elder reg = ipa_reg(ipa, ENDP_INIT_HDR); 793660e52d6SAlex Elder if (endpoint->config.qmap) { 7941af15c2aSAlex Elder enum ipa_version version = ipa->version; 7955567d4d9SAlex Elder size_t header_size; 79684f9bd12SAlex Elder 7975567d4d9SAlex Elder header_size = ipa_qmap_header_size(version, endpoint); 7984468a344SAlex Elder val = ipa_header_size_encode(version, reg, header_size); 79984f9bd12SAlex Elder 800f330fda3SAlex Elder /* Define how to fill fields in a received QMAP header */ 8018730f45dSAlex Elder if (!endpoint->toward_ipa) { 8029eefd2fbSAlex Elder u32 off; /* Field offset within header */ 8038730f45dSAlex Elder 8048730f45dSAlex Elder /* Where IPA will write the metadata value */ 8059eefd2fbSAlex Elder off = offsetof(struct rmnet_map_header, mux_id); 8064468a344SAlex Elder val |= ipa_metadata_offset_encode(version, reg, off); 8078730f45dSAlex Elder 8088730f45dSAlex Elder /* Where IPA will write the length */ 8099eefd2fbSAlex Elder off = offsetof(struct rmnet_map_header, pkt_len); 8101af15c2aSAlex Elder /* Upper bits are stored in HDR_EXT with IPA v4.5 */ 811d7f3087bSAlex Elder if (version >= IPA_VERSION_4_5) 812f1470fd7SAlex Elder off &= reg_field_max(reg, HDR_OFST_PKT_SIZE); 8131af15c2aSAlex Elder 814f1470fd7SAlex Elder val |= reg_bit(reg, HDR_OFST_PKT_SIZE_VALID); 815f1470fd7SAlex Elder val |= reg_encode(reg, HDR_OFST_PKT_SIZE, off); 81684f9bd12SAlex Elder } 8178730f45dSAlex Elder /* For QMAP TX, metadata offset is 0 (modem assumes this) */ 818f1470fd7SAlex Elder val |= reg_bit(reg, HDR_OFST_METADATA_VALID); 8198730f45dSAlex Elder 8208730f45dSAlex Elder /* HDR_ADDITIONAL_CONST_LEN is 0; (RX only) */ 82184f9bd12SAlex Elder /* HDR_A5_MUX is 0 */ 82284f9bd12SAlex Elder /* HDR_LEN_INC_DEAGG_HDR is 0 */ 8238bfc4e21SAlex Elder /* HDR_METADATA_REG_VALID is 0 (TX only, version < v4.5) */ 82484f9bd12SAlex Elder } 82584f9bd12SAlex Elder 826fc4cecf7SAlex Elder iowrite32(val, ipa->reg_virt + reg_n_offset(reg, endpoint_id)); 82784f9bd12SAlex Elder } 82884f9bd12SAlex Elder 82984f9bd12SAlex Elder static void ipa_endpoint_init_hdr_ext(struct ipa_endpoint *endpoint) 83084f9bd12SAlex Elder { 831660e52d6SAlex Elder u32 pad_align = endpoint->config.rx.pad_align; 8326a244b75SAlex Elder u32 endpoint_id = endpoint->endpoint_id; 8331af15c2aSAlex Elder struct ipa *ipa = endpoint->ipa; 83481772e44SAlex Elder const struct reg *reg; 83584f9bd12SAlex Elder u32 val = 0; 8366bfb7538SAlex Elder 8376a244b75SAlex Elder reg = ipa_reg(ipa, ENDP_INIT_HDR_EXT); 838660e52d6SAlex Elder if (endpoint->config.qmap) { 839332ef7c8SAlex Elder /* We have a header, so we must specify its endianness */ 840f1470fd7SAlex Elder val |= reg_bit(reg, HDR_ENDIANNESS); /* big endian */ 841f330fda3SAlex Elder 842332ef7c8SAlex Elder /* A QMAP header contains a 6 bit pad field at offset 0. 843332ef7c8SAlex Elder * The RMNet driver assumes this field is meaningful in 844332ef7c8SAlex Elder * packets it receives, and assumes the header's payload 845332ef7c8SAlex Elder * length includes that padding. The RMNet driver does 846332ef7c8SAlex Elder * *not* pad packets it sends, however, so the pad field 847332ef7c8SAlex Elder * (although 0) should be ignored. 848f330fda3SAlex Elder */ 849332ef7c8SAlex Elder if (!endpoint->toward_ipa) { 850f1470fd7SAlex Elder val |= reg_bit(reg, HDR_TOTAL_LEN_OR_PAD_VALID); 85184f9bd12SAlex Elder /* HDR_TOTAL_LEN_OR_PAD is 0 (pad, not total_len) */ 852f1470fd7SAlex Elder val |= reg_bit(reg, HDR_PAYLOAD_LEN_INC_PADDING); 85384f9bd12SAlex Elder /* HDR_TOTAL_LEN_OR_PAD_OFFSET is 0 */ 854f330fda3SAlex Elder } 855332ef7c8SAlex Elder } 856f330fda3SAlex Elder 857f330fda3SAlex Elder /* HDR_PAYLOAD_LEN_INC_PADDING is 0 */ 85884f9bd12SAlex Elder if (!endpoint->toward_ipa) 859f1470fd7SAlex Elder val |= reg_encode(reg, HDR_PAD_TO_ALIGNMENT, pad_align); 86084f9bd12SAlex Elder 8611af15c2aSAlex Elder /* IPA v4.5 adds some most-significant bits to a few fields, 8621af15c2aSAlex Elder * two of which are defined in the HDR (not HDR_EXT) register. 8631af15c2aSAlex Elder */ 864d7f3087bSAlex Elder if (ipa->version >= IPA_VERSION_4_5) { 8651af15c2aSAlex Elder /* HDR_TOTAL_LEN_OR_PAD_OFFSET is 0, so MSB is 0 */ 866660e52d6SAlex Elder if (endpoint->config.qmap && !endpoint->toward_ipa) { 867f1470fd7SAlex Elder u32 mask = reg_field_max(reg, HDR_OFST_PKT_SIZE); 8686bfb7538SAlex Elder u32 off; /* Field offset within header */ 86984f9bd12SAlex Elder 8709eefd2fbSAlex Elder off = offsetof(struct rmnet_map_header, pkt_len); 8714468a344SAlex Elder /* Low bits are in the ENDP_INIT_HDR register */ 8724468a344SAlex Elder off >>= hweight32(mask); 873f1470fd7SAlex Elder val |= reg_encode(reg, HDR_OFST_PKT_SIZE_MSB, off); 8741af15c2aSAlex Elder /* HDR_ADDITIONAL_CONST_LEN is 0 so MSB is 0 */ 8751af15c2aSAlex Elder } 8761af15c2aSAlex Elder } 8776bfb7538SAlex Elder 878fc4cecf7SAlex Elder iowrite32(val, ipa->reg_virt + reg_n_offset(reg, endpoint_id)); 8791af15c2aSAlex Elder } 88084f9bd12SAlex Elder 88184f9bd12SAlex Elder static void ipa_endpoint_init_hdr_metadata_mask(struct ipa_endpoint *endpoint) 88284f9bd12SAlex Elder { 88384f9bd12SAlex Elder u32 endpoint_id = endpoint->endpoint_id; 8846bfb7538SAlex Elder struct ipa *ipa = endpoint->ipa; 88581772e44SAlex Elder const struct reg *reg; 88684f9bd12SAlex Elder u32 val = 0; 88784f9bd12SAlex Elder u32 offset; 88884f9bd12SAlex Elder 889fb57c3eaSAlex Elder if (endpoint->toward_ipa) 890fb57c3eaSAlex Elder return; /* Register not valid for TX endpoints */ 891fb57c3eaSAlex Elder 8926a244b75SAlex Elder reg = ipa_reg(ipa, ENDP_INIT_HDR_METADATA_MASK); 893fc4cecf7SAlex Elder offset = reg_n_offset(reg, endpoint_id); 89484f9bd12SAlex Elder 8958730f45dSAlex Elder /* Note that HDR_ENDIANNESS indicates big endian header fields */ 896660e52d6SAlex Elder if (endpoint->config.qmap) 897088f8a23SAlex Elder val = (__force u32)cpu_to_be32(IPA_ENDPOINT_QMAP_METADATA_MASK); 89884f9bd12SAlex Elder 8996bfb7538SAlex Elder iowrite32(val, ipa->reg_virt + offset); 90084f9bd12SAlex Elder } 90184f9bd12SAlex Elder 90284f9bd12SAlex Elder static void ipa_endpoint_init_mode(struct ipa_endpoint *endpoint) 90384f9bd12SAlex Elder { 9046bfb7538SAlex Elder struct ipa *ipa = endpoint->ipa; 90581772e44SAlex Elder const struct reg *reg; 9066bfb7538SAlex Elder u32 offset; 90784f9bd12SAlex Elder u32 val; 90884f9bd12SAlex Elder 909fb57c3eaSAlex Elder if (!endpoint->toward_ipa) 910fb57c3eaSAlex Elder return; /* Register not valid for RX endpoints */ 911fb57c3eaSAlex Elder 9126a244b75SAlex Elder reg = ipa_reg(ipa, ENDP_INIT_MODE); 913660e52d6SAlex Elder if (endpoint->config.dma_mode) { 914660e52d6SAlex Elder enum ipa_endpoint_name name = endpoint->config.dma_endpoint; 915216b409dSAlex Elder u32 dma_endpoint_id = ipa->name_map[name]->endpoint_id; 91684f9bd12SAlex Elder 917f1470fd7SAlex Elder val = reg_encode(reg, ENDP_MODE, IPA_DMA); 918f1470fd7SAlex Elder val |= reg_encode(reg, DEST_PIPE_INDEX, dma_endpoint_id); 91984f9bd12SAlex Elder } else { 920f1470fd7SAlex Elder val = reg_encode(reg, ENDP_MODE, IPA_BASIC); 92184f9bd12SAlex Elder } 92200b9102aSAlex Elder /* All other bits unspecified (and 0) */ 92384f9bd12SAlex Elder 924fc4cecf7SAlex Elder offset = reg_n_offset(reg, endpoint->endpoint_id); 9256bfb7538SAlex Elder iowrite32(val, ipa->reg_virt + offset); 92684f9bd12SAlex Elder } 92784f9bd12SAlex Elder 9282cdbcbfdSAlex Elder /* For IPA v4.5+, times are expressed using Qtime. A time is represented 9292cdbcbfdSAlex Elder * at one of several available granularities, which are configured in 9302cdbcbfdSAlex Elder * ipa_qtime_config(). Three (or, starting with IPA v5.0, four) pulse 9312cdbcbfdSAlex Elder * generators are set up with different "tick" periods. A Qtime value 9322cdbcbfdSAlex Elder * encodes a tick count along with an indication of a pulse generator 9332cdbcbfdSAlex Elder * (which has a fixed tick period). Two pulse generators are always 9342cdbcbfdSAlex Elder * available to the AP; a third is available starting with IPA v5.0. 9352cdbcbfdSAlex Elder * This function determines which pulse generator most accurately 9362cdbcbfdSAlex Elder * represents the time period provided, and returns the tick count to 9372cdbcbfdSAlex Elder * use to represent that time. 9388be440e1SAlex Elder */ 9392cdbcbfdSAlex Elder static u32 9402cdbcbfdSAlex Elder ipa_qtime_val(struct ipa *ipa, u32 microseconds, u32 max, u32 *select) 9418be440e1SAlex Elder { 9422cdbcbfdSAlex Elder u32 which = 0; 9432cdbcbfdSAlex Elder u32 ticks; 9448be440e1SAlex Elder 9452cdbcbfdSAlex Elder /* Pulse generator 0 has 100 microsecond granularity */ 9462cdbcbfdSAlex Elder ticks = DIV_ROUND_CLOSEST(microseconds, 100); 9472cdbcbfdSAlex Elder if (ticks <= max) 9482cdbcbfdSAlex Elder goto out; 9498be440e1SAlex Elder 9502cdbcbfdSAlex Elder /* Pulse generator 1 has millisecond granularity */ 9512cdbcbfdSAlex Elder which = 1; 9522cdbcbfdSAlex Elder ticks = DIV_ROUND_CLOSEST(microseconds, 1000); 9532cdbcbfdSAlex Elder if (ticks <= max) 9542cdbcbfdSAlex Elder goto out; 9558be440e1SAlex Elder 9562cdbcbfdSAlex Elder if (ipa->version >= IPA_VERSION_5_0) { 9572cdbcbfdSAlex Elder /* Pulse generator 2 has 10 millisecond granularity */ 9582cdbcbfdSAlex Elder which = 2; 9592cdbcbfdSAlex Elder ticks = DIV_ROUND_CLOSEST(microseconds, 100); 9602cdbcbfdSAlex Elder } 9612cdbcbfdSAlex Elder WARN_ON(ticks > max); 9622cdbcbfdSAlex Elder out: 9632cdbcbfdSAlex Elder *select = which; 9642cdbcbfdSAlex Elder 9652cdbcbfdSAlex Elder return ticks; 9668be440e1SAlex Elder } 9678be440e1SAlex Elder 96819547041SAlex Elder /* Encode the aggregation timer limit (microseconds) based on IPA version */ 96981772e44SAlex Elder static u32 aggr_time_limit_encode(struct ipa *ipa, const struct reg *reg, 970216b409dSAlex Elder u32 microseconds) 9716bf754c7SAlex Elder { 9722cdbcbfdSAlex Elder u32 ticks; 973216b409dSAlex Elder u32 max; 97448395fa8SAlex Elder 97548395fa8SAlex Elder if (!microseconds) 97648395fa8SAlex Elder return 0; /* Nothing to compute if time limit is 0 */ 97748395fa8SAlex Elder 978f1470fd7SAlex Elder max = reg_field_max(reg, TIME_LIMIT); 979216b409dSAlex Elder if (ipa->version >= IPA_VERSION_4_5) { 9802cdbcbfdSAlex Elder u32 select; 9816bf754c7SAlex Elder 9822cdbcbfdSAlex Elder ticks = ipa_qtime_val(ipa, microseconds, max, &select); 98319547041SAlex Elder 984f1470fd7SAlex Elder return reg_encode(reg, AGGR_GRAN_SEL, select) | 985f1470fd7SAlex Elder reg_encode(reg, TIME_LIMIT, ticks); 9866bf754c7SAlex Elder } 9876bf754c7SAlex Elder 988216b409dSAlex Elder /* We program aggregation granularity in ipa_hardware_config() */ 9892cdbcbfdSAlex Elder ticks = DIV_ROUND_CLOSEST(microseconds, IPA_AGGR_GRANULARITY); 9902cdbcbfdSAlex Elder WARN(ticks > max, "aggr_time_limit too large (%u > %u usec)\n", 991216b409dSAlex Elder microseconds, max * IPA_AGGR_GRANULARITY); 99248395fa8SAlex Elder 993f1470fd7SAlex Elder return reg_encode(reg, TIME_LIMIT, ticks); 9946bf754c7SAlex Elder } 9956bf754c7SAlex Elder 99684f9bd12SAlex Elder static void ipa_endpoint_init_aggr(struct ipa_endpoint *endpoint) 99784f9bd12SAlex Elder { 9986a244b75SAlex Elder u32 endpoint_id = endpoint->endpoint_id; 9996bfb7538SAlex Elder struct ipa *ipa = endpoint->ipa; 100081772e44SAlex Elder const struct reg *reg; 100184f9bd12SAlex Elder u32 val = 0; 10026bfb7538SAlex Elder 10036a244b75SAlex Elder reg = ipa_reg(ipa, ENDP_INIT_AGGR); 1004660e52d6SAlex Elder if (endpoint->config.aggregation) { 100584f9bd12SAlex Elder if (!endpoint->toward_ipa) { 1006cf4e73a1SAlex Elder const struct ipa_endpoint_rx *rx_config; 1007c5794097SAlex Elder u32 buffer_size; 100884f9bd12SAlex Elder u32 limit; 100984f9bd12SAlex Elder 1010660e52d6SAlex Elder rx_config = &endpoint->config.rx; 1011f1470fd7SAlex Elder val |= reg_encode(reg, AGGR_EN, IPA_ENABLE_AGGR); 1012f1470fd7SAlex Elder val |= reg_encode(reg, AGGR_TYPE, IPA_GENERIC); 10139e88cb5fSAlex Elder 1014cf4e73a1SAlex Elder buffer_size = rx_config->buffer_size; 10153cebb7c2SAlex Elder limit = ipa_aggr_size_kb(buffer_size - NET_SKB_PAD, 10163cebb7c2SAlex Elder rx_config->aggr_hard_limit); 1017f1470fd7SAlex Elder val |= reg_encode(reg, BYTE_LIMIT, limit); 10181d86652bSAlex Elder 1019beb90cbaSAlex Elder limit = rx_config->aggr_time_limit; 1020216b409dSAlex Elder val |= aggr_time_limit_encode(ipa, reg, limit); 10211d86652bSAlex Elder 10229e88cb5fSAlex Elder /* AGGR_PKT_LIMIT is 0 (unlimited) */ 10239e88cb5fSAlex Elder 1024216b409dSAlex Elder if (rx_config->aggr_close_eof) 1025f1470fd7SAlex Elder val |= reg_bit(reg, SW_EOF_ACTIVE); 102684f9bd12SAlex Elder } else { 1027f1470fd7SAlex Elder val |= reg_encode(reg, AGGR_EN, IPA_ENABLE_DEAGGR); 1028f1470fd7SAlex Elder val |= reg_encode(reg, AGGR_TYPE, IPA_QCMAP); 102984f9bd12SAlex Elder /* other fields ignored */ 103084f9bd12SAlex Elder } 103184f9bd12SAlex Elder /* AGGR_FORCE_CLOSE is 0 */ 10328bfc4e21SAlex Elder /* AGGR_GRAN_SEL is 0 for IPA v4.5 */ 103384f9bd12SAlex Elder } else { 1034f1470fd7SAlex Elder val |= reg_encode(reg, AGGR_EN, IPA_BYPASS_AGGR); 103584f9bd12SAlex Elder /* other fields ignored */ 103684f9bd12SAlex Elder } 103784f9bd12SAlex Elder 1038fc4cecf7SAlex Elder iowrite32(val, ipa->reg_virt + reg_n_offset(reg, endpoint_id)); 103984f9bd12SAlex Elder } 104084f9bd12SAlex Elder 104163e5afc8SAlex Elder /* The head-of-line blocking timer is defined as a tick count. For 104263e5afc8SAlex Elder * IPA version 4.5 the tick count is based on the Qtimer, which is 104363e5afc8SAlex Elder * derived from the 19.2 MHz SoC XO clock. For older IPA versions 104463e5afc8SAlex Elder * each tick represents 128 cycles of the IPA core clock. 104563e5afc8SAlex Elder * 10468be440e1SAlex Elder * Return the encoded value representing the timeout period provided 10478be440e1SAlex Elder * that should be written to the ENDP_INIT_HOL_BLOCK_TIMER register. 104863e5afc8SAlex Elder */ 104981772e44SAlex Elder static u32 hol_block_timer_encode(struct ipa *ipa, const struct reg *reg, 1050216b409dSAlex Elder u32 microseconds) 105184f9bd12SAlex Elder { 1052f13a8c31SAlex Elder u32 width; 105384f9bd12SAlex Elder u32 scale; 1054f13a8c31SAlex Elder u64 ticks; 1055f13a8c31SAlex Elder u64 rate; 1056f13a8c31SAlex Elder u32 high; 105784f9bd12SAlex Elder u32 val; 105884f9bd12SAlex Elder 105984f9bd12SAlex Elder if (!microseconds) 1060f13a8c31SAlex Elder return 0; /* Nothing to compute if timer period is 0 */ 106184f9bd12SAlex Elder 106248395fa8SAlex Elder if (ipa->version >= IPA_VERSION_4_5) { 1063f1470fd7SAlex Elder u32 max = reg_field_max(reg, TIMER_LIMIT); 10642cdbcbfdSAlex Elder u32 select; 10652cdbcbfdSAlex Elder u32 ticks; 106648395fa8SAlex Elder 10672cdbcbfdSAlex Elder ticks = ipa_qtime_val(ipa, microseconds, max, &select); 106848395fa8SAlex Elder 1069f1470fd7SAlex Elder return reg_encode(reg, TIMER_GRAN_SEL, 1) | 1070f1470fd7SAlex Elder reg_encode(reg, TIMER_LIMIT, ticks); 107148395fa8SAlex Elder } 107263e5afc8SAlex Elder 1073216b409dSAlex Elder /* Use 64 bit arithmetic to avoid overflow */ 10747aa0e8b8SAlex Elder rate = ipa_core_clock_rate(ipa); 1075f13a8c31SAlex Elder ticks = DIV_ROUND_CLOSEST(microseconds * rate, 128 * USEC_PER_SEC); 1076216b409dSAlex Elder 1077216b409dSAlex Elder /* We still need the result to fit into the field */ 1078f1470fd7SAlex Elder WARN_ON(ticks > reg_field_max(reg, TIMER_BASE_VALUE)); 107984f9bd12SAlex Elder 10806833a096SAlex Elder /* IPA v3.5.1 through v4.1 just record the tick count */ 10816833a096SAlex Elder if (ipa->version < IPA_VERSION_4_2) 1082f1470fd7SAlex Elder return reg_encode(reg, TIMER_BASE_VALUE, (u32)ticks); 108384f9bd12SAlex Elder 1084f13a8c31SAlex Elder /* For IPA v4.2, the tick count is represented by base and 1085f13a8c31SAlex Elder * scale fields within the 32-bit timer register, where: 1086f13a8c31SAlex Elder * ticks = base << scale; 1087f13a8c31SAlex Elder * The best precision is achieved when the base value is as 1088f13a8c31SAlex Elder * large as possible. Find the highest set bit in the tick 1089f13a8c31SAlex Elder * count, and extract the number of bits in the base field 1090497abc87SPeng Li * such that high bit is included. 1091f13a8c31SAlex Elder */ 1092216b409dSAlex Elder high = fls(ticks); /* 1..32 (or warning above) */ 1093f1470fd7SAlex Elder width = hweight32(reg_fmask(reg, TIMER_BASE_VALUE)); 1094f13a8c31SAlex Elder scale = high > width ? high - width : 0; 1095f13a8c31SAlex Elder if (scale) { 1096f13a8c31SAlex Elder /* If we're scaling, round up to get a closer result */ 1097f13a8c31SAlex Elder ticks += 1 << (scale - 1); 1098f13a8c31SAlex Elder /* High bit was set, so rounding might have affected it */ 1099f13a8c31SAlex Elder if (fls(ticks) != high) 1100f13a8c31SAlex Elder scale++; 1101f13a8c31SAlex Elder } 110284f9bd12SAlex Elder 1103f1470fd7SAlex Elder val = reg_encode(reg, TIMER_SCALE, scale); 1104f1470fd7SAlex Elder val |= reg_encode(reg, TIMER_BASE_VALUE, (u32)ticks >> scale); 110584f9bd12SAlex Elder 110684f9bd12SAlex Elder return val; 110784f9bd12SAlex Elder } 110884f9bd12SAlex Elder 1109f13a8c31SAlex Elder /* If microseconds is 0, timeout is immediate */ 1110f13a8c31SAlex Elder static void ipa_endpoint_init_hol_block_timer(struct ipa_endpoint *endpoint, 111184f9bd12SAlex Elder u32 microseconds) 111284f9bd12SAlex Elder { 111384f9bd12SAlex Elder u32 endpoint_id = endpoint->endpoint_id; 111484f9bd12SAlex Elder struct ipa *ipa = endpoint->ipa; 111581772e44SAlex Elder const struct reg *reg; 111684f9bd12SAlex Elder u32 val; 111784f9bd12SAlex Elder 1118816316caSAlex Elder /* This should only be changed when HOL_BLOCK_EN is disabled */ 11196a244b75SAlex Elder reg = ipa_reg(ipa, ENDP_INIT_HOL_BLOCK_TIMER); 1120216b409dSAlex Elder val = hol_block_timer_encode(ipa, reg, microseconds); 11216bfb7538SAlex Elder 1122fc4cecf7SAlex Elder iowrite32(val, ipa->reg_virt + reg_n_offset(reg, endpoint_id)); 112384f9bd12SAlex Elder } 112484f9bd12SAlex Elder 112584f9bd12SAlex Elder static void 1126e6aab6b9SAlex Elder ipa_endpoint_init_hol_block_en(struct ipa_endpoint *endpoint, bool enable) 112784f9bd12SAlex Elder { 112884f9bd12SAlex Elder u32 endpoint_id = endpoint->endpoint_id; 11296bfb7538SAlex Elder struct ipa *ipa = endpoint->ipa; 113081772e44SAlex Elder const struct reg *reg; 113184f9bd12SAlex Elder u32 offset; 113284f9bd12SAlex Elder u32 val; 113384f9bd12SAlex Elder 11346a244b75SAlex Elder reg = ipa_reg(ipa, ENDP_INIT_HOL_BLOCK_EN); 1135fc4cecf7SAlex Elder offset = reg_n_offset(reg, endpoint_id); 1136f1470fd7SAlex Elder val = enable ? reg_bit(reg, HOL_BLOCK_EN) : 0; 11376bfb7538SAlex Elder 11386bfb7538SAlex Elder iowrite32(val, ipa->reg_virt + offset); 11396bfb7538SAlex Elder 11406e228d8cSAlex Elder /* When enabling, the register must be written twice for IPA v4.5+ */ 11416bfb7538SAlex Elder if (enable && ipa->version >= IPA_VERSION_4_5) 11426bfb7538SAlex Elder iowrite32(val, ipa->reg_virt + offset); 114384f9bd12SAlex Elder } 114484f9bd12SAlex Elder 1145e6aab6b9SAlex Elder /* Assumes HOL_BLOCK is in disabled state */ 1146e6aab6b9SAlex Elder static void ipa_endpoint_init_hol_block_enable(struct ipa_endpoint *endpoint, 1147e6aab6b9SAlex Elder u32 microseconds) 1148e6aab6b9SAlex Elder { 1149e6aab6b9SAlex Elder ipa_endpoint_init_hol_block_timer(endpoint, microseconds); 1150e6aab6b9SAlex Elder ipa_endpoint_init_hol_block_en(endpoint, true); 1151e6aab6b9SAlex Elder } 1152e6aab6b9SAlex Elder 1153e6aab6b9SAlex Elder static void ipa_endpoint_init_hol_block_disable(struct ipa_endpoint *endpoint) 1154e6aab6b9SAlex Elder { 1155e6aab6b9SAlex Elder ipa_endpoint_init_hol_block_en(endpoint, false); 1156e6aab6b9SAlex Elder } 1157e6aab6b9SAlex Elder 115884f9bd12SAlex Elder void ipa_endpoint_modem_hol_block_clear_all(struct ipa *ipa) 115984f9bd12SAlex Elder { 1160e359ba89SAlex Elder u32 endpoint_id = 0; 116184f9bd12SAlex Elder 1162b7aaff0bSAlex Elder while (endpoint_id < ipa->endpoint_count) { 1163e359ba89SAlex Elder struct ipa_endpoint *endpoint = &ipa->endpoint[endpoint_id++]; 116484f9bd12SAlex Elder 1165f8d34dfdSAlex Elder if (endpoint->toward_ipa || endpoint->ee_id != GSI_EE_MODEM) 116684f9bd12SAlex Elder continue; 116784f9bd12SAlex Elder 1168e6aab6b9SAlex Elder ipa_endpoint_init_hol_block_disable(endpoint); 1169e6aab6b9SAlex Elder ipa_endpoint_init_hol_block_enable(endpoint, 0); 117084f9bd12SAlex Elder } 117184f9bd12SAlex Elder } 117284f9bd12SAlex Elder 117384f9bd12SAlex Elder static void ipa_endpoint_init_deaggr(struct ipa_endpoint *endpoint) 117484f9bd12SAlex Elder { 11756a244b75SAlex Elder u32 endpoint_id = endpoint->endpoint_id; 11766bfb7538SAlex Elder struct ipa *ipa = endpoint->ipa; 117781772e44SAlex Elder const struct reg *reg; 117884f9bd12SAlex Elder u32 val = 0; 117984f9bd12SAlex Elder 1180fb57c3eaSAlex Elder if (!endpoint->toward_ipa) 1181fb57c3eaSAlex Elder return; /* Register not valid for RX endpoints */ 1182fb57c3eaSAlex Elder 11836a244b75SAlex Elder reg = ipa_reg(ipa, ENDP_INIT_DEAGGR); 118484f9bd12SAlex Elder /* DEAGGR_HDR_LEN is 0 */ 118584f9bd12SAlex Elder /* PACKET_OFFSET_VALID is 0 */ 118684f9bd12SAlex Elder /* PACKET_OFFSET_LOCATION is ignored (not valid) */ 118784f9bd12SAlex Elder /* MAX_PACKET_LEN is 0 (not enforced) */ 118884f9bd12SAlex Elder 1189fc4cecf7SAlex Elder iowrite32(val, ipa->reg_virt + reg_n_offset(reg, endpoint_id)); 119084f9bd12SAlex Elder } 119184f9bd12SAlex Elder 11922d265342SAlex Elder static void ipa_endpoint_init_rsrc_grp(struct ipa_endpoint *endpoint) 11932d265342SAlex Elder { 1194181ca020SAlex Elder u32 resource_group = endpoint->config.resource_group; 11956a244b75SAlex Elder u32 endpoint_id = endpoint->endpoint_id; 11962d265342SAlex Elder struct ipa *ipa = endpoint->ipa; 119781772e44SAlex Elder const struct reg *reg; 11982d265342SAlex Elder u32 val; 11992d265342SAlex Elder 12006a244b75SAlex Elder reg = ipa_reg(ipa, ENDP_INIT_RSRC_GRP); 1201f1470fd7SAlex Elder val = reg_encode(reg, ENDP_RSRC_GRP, resource_group); 12026bfb7538SAlex Elder 1203fc4cecf7SAlex Elder iowrite32(val, ipa->reg_virt + reg_n_offset(reg, endpoint_id)); 12042d265342SAlex Elder } 12052d265342SAlex Elder 120684f9bd12SAlex Elder static void ipa_endpoint_init_seq(struct ipa_endpoint *endpoint) 120784f9bd12SAlex Elder { 12086a244b75SAlex Elder u32 endpoint_id = endpoint->endpoint_id; 12096bfb7538SAlex Elder struct ipa *ipa = endpoint->ipa; 121081772e44SAlex Elder const struct reg *reg; 1211181ca020SAlex Elder u32 val; 121284f9bd12SAlex Elder 1213fb57c3eaSAlex Elder if (!endpoint->toward_ipa) 1214fb57c3eaSAlex Elder return; /* Register not valid for RX endpoints */ 1215fb57c3eaSAlex Elder 12166a244b75SAlex Elder reg = ipa_reg(ipa, ENDP_INIT_SEQ); 12176bfb7538SAlex Elder 12188ee5df65SAlex Elder /* Low-order byte configures primary packet processing */ 1219f1470fd7SAlex Elder val = reg_encode(reg, SEQ_TYPE, endpoint->config.tx.seq_type); 12208ee5df65SAlex Elder 1221a14d5937SAlex Elder /* Second byte (if supported) configures replicated packet processing */ 12226bfb7538SAlex Elder if (ipa->version < IPA_VERSION_4_5) 1223f1470fd7SAlex Elder val |= reg_encode(reg, SEQ_REP_TYPE, 1224181ca020SAlex Elder endpoint->config.tx.seq_rep_type); 122584f9bd12SAlex Elder 1226fc4cecf7SAlex Elder iowrite32(val, ipa->reg_virt + reg_n_offset(reg, endpoint_id)); 122784f9bd12SAlex Elder } 122884f9bd12SAlex Elder 122984f9bd12SAlex Elder /** 123084f9bd12SAlex Elder * ipa_endpoint_skb_tx() - Transmit a socket buffer 123184f9bd12SAlex Elder * @endpoint: Endpoint pointer 123284f9bd12SAlex Elder * @skb: Socket buffer to send 123384f9bd12SAlex Elder * 123484f9bd12SAlex Elder * Returns: 0 if successful, or a negative error code 123584f9bd12SAlex Elder */ 123684f9bd12SAlex Elder int ipa_endpoint_skb_tx(struct ipa_endpoint *endpoint, struct sk_buff *skb) 123784f9bd12SAlex Elder { 123884f9bd12SAlex Elder struct gsi_trans *trans; 123984f9bd12SAlex Elder u32 nr_frags; 124084f9bd12SAlex Elder int ret; 124184f9bd12SAlex Elder 124284f9bd12SAlex Elder /* Make sure source endpoint's TLV FIFO has enough entries to 124384f9bd12SAlex Elder * hold the linear portion of the skb and all its fragments. 124484f9bd12SAlex Elder * If not, see if we can linearize it before giving up. 124584f9bd12SAlex Elder */ 124684f9bd12SAlex Elder nr_frags = skb_shinfo(skb)->nr_frags; 1247317595d2SAlex Elder if (nr_frags > endpoint->skb_frag_max) { 124884f9bd12SAlex Elder if (skb_linearize(skb)) 124984f9bd12SAlex Elder return -E2BIG; 125084f9bd12SAlex Elder nr_frags = 0; 125184f9bd12SAlex Elder } 125284f9bd12SAlex Elder 125384f9bd12SAlex Elder trans = ipa_endpoint_trans_alloc(endpoint, 1 + nr_frags); 125484f9bd12SAlex Elder if (!trans) 125584f9bd12SAlex Elder return -EBUSY; 125684f9bd12SAlex Elder 125784f9bd12SAlex Elder ret = gsi_trans_skb_add(trans, skb); 125884f9bd12SAlex Elder if (ret) 125984f9bd12SAlex Elder goto err_trans_free; 126084f9bd12SAlex Elder trans->data = skb; /* transaction owns skb now */ 126184f9bd12SAlex Elder 126284f9bd12SAlex Elder gsi_trans_commit(trans, !netdev_xmit_more()); 126384f9bd12SAlex Elder 126484f9bd12SAlex Elder return 0; 126584f9bd12SAlex Elder 126684f9bd12SAlex Elder err_trans_free: 126784f9bd12SAlex Elder gsi_trans_free(trans); 126884f9bd12SAlex Elder 126984f9bd12SAlex Elder return -ENOMEM; 127084f9bd12SAlex Elder } 127184f9bd12SAlex Elder 127284f9bd12SAlex Elder static void ipa_endpoint_status(struct ipa_endpoint *endpoint) 127384f9bd12SAlex Elder { 127484f9bd12SAlex Elder u32 endpoint_id = endpoint->endpoint_id; 127584f9bd12SAlex Elder struct ipa *ipa = endpoint->ipa; 127681772e44SAlex Elder const struct reg *reg; 127784f9bd12SAlex Elder u32 val = 0; 127884f9bd12SAlex Elder 12796a244b75SAlex Elder reg = ipa_reg(ipa, ENDP_STATUS); 1280660e52d6SAlex Elder if (endpoint->config.status_enable) { 1281f1470fd7SAlex Elder val |= reg_bit(reg, STATUS_EN); 128284f9bd12SAlex Elder if (endpoint->toward_ipa) { 128384f9bd12SAlex Elder enum ipa_endpoint_name name; 128484f9bd12SAlex Elder u32 status_endpoint_id; 128584f9bd12SAlex Elder 1286660e52d6SAlex Elder name = endpoint->config.tx.status_endpoint; 128784f9bd12SAlex Elder status_endpoint_id = ipa->name_map[name]->endpoint_id; 128884f9bd12SAlex Elder 1289f1470fd7SAlex Elder val |= reg_encode(reg, STATUS_ENDP, status_endpoint_id); 129084f9bd12SAlex Elder } 129102c50774SAlex Elder /* STATUS_LOCATION is 0, meaning IPA packet status 129202c50774SAlex Elder * precedes the packet (not present for IPA v4.5+) 12938bfc4e21SAlex Elder */ 1294181ca020SAlex Elder /* STATUS_PKT_SUPPRESS_FMASK is 0 (not present for v4.0+) */ 129584f9bd12SAlex Elder } 129684f9bd12SAlex Elder 1297fc4cecf7SAlex Elder iowrite32(val, ipa->reg_virt + reg_n_offset(reg, endpoint_id)); 129884f9bd12SAlex Elder } 129984f9bd12SAlex Elder 13006a606b90SAlex Elder static int ipa_endpoint_replenish_one(struct ipa_endpoint *endpoint, 13016a606b90SAlex Elder struct gsi_trans *trans) 130284f9bd12SAlex Elder { 130384f9bd12SAlex Elder struct page *page; 1304ed23f026SAlex Elder u32 buffer_size; 130584f9bd12SAlex Elder u32 offset; 130684f9bd12SAlex Elder u32 len; 130784f9bd12SAlex Elder int ret; 130884f9bd12SAlex Elder 1309660e52d6SAlex Elder buffer_size = endpoint->config.rx.buffer_size; 1310ed23f026SAlex Elder page = dev_alloc_pages(get_order(buffer_size)); 131184f9bd12SAlex Elder if (!page) 13126a606b90SAlex Elder return -ENOMEM; 131384f9bd12SAlex Elder 131484f9bd12SAlex Elder /* Offset the buffer to make space for skb headroom */ 131584f9bd12SAlex Elder offset = NET_SKB_PAD; 1316ed23f026SAlex Elder len = buffer_size - offset; 131784f9bd12SAlex Elder 131884f9bd12SAlex Elder ret = gsi_trans_page_add(trans, page, len, offset); 131984f9bd12SAlex Elder if (ret) 132070132763SAlex Elder put_page(page); 13216a606b90SAlex Elder else 132284f9bd12SAlex Elder trans->data = page; /* transaction owns page now */ 132384f9bd12SAlex Elder 13246a606b90SAlex Elder return ret; 132584f9bd12SAlex Elder } 132684f9bd12SAlex Elder 132784f9bd12SAlex Elder /** 13289af5ccf3SAlex Elder * ipa_endpoint_replenish() - Replenish endpoint receive buffers 1329e3eea08eSAlex Elder * @endpoint: Endpoint to be replenished 133084f9bd12SAlex Elder * 13319af5ccf3SAlex Elder * The IPA hardware can hold a fixed number of receive buffers for an RX 13329af5ccf3SAlex Elder * endpoint, based on the number of entries in the underlying channel ring 13339af5ccf3SAlex Elder * buffer. If an endpoint's "backlog" is non-zero, it indicates how many 13349af5ccf3SAlex Elder * more receive buffers can be supplied to the hardware. Replenishing for 1335a9bec7aeSAlex Elder * an endpoint can be disabled, in which case buffers are not queued to 1336a9bec7aeSAlex Elder * the hardware. 133784f9bd12SAlex Elder */ 13384b22d841SAlex Elder static void ipa_endpoint_replenish(struct ipa_endpoint *endpoint) 133984f9bd12SAlex Elder { 13406a606b90SAlex Elder struct gsi_trans *trans; 134184f9bd12SAlex Elder 13424b22d841SAlex Elder if (!test_bit(IPA_REPLENISH_ENABLED, endpoint->replenish_flags)) 134384f9bd12SAlex Elder return; 134484f9bd12SAlex Elder 13454b22d841SAlex Elder /* Skip it if it's already active */ 13464b22d841SAlex Elder if (test_and_set_bit(IPA_REPLENISH_ACTIVE, endpoint->replenish_flags)) 1347998c0bd2SAlex Elder return; 1348998c0bd2SAlex Elder 1349d0ac30e7SAlex Elder while ((trans = ipa_endpoint_trans_alloc(endpoint, 1))) { 13509654d8c4SAlex Elder bool doorbell; 13519654d8c4SAlex Elder 13526a606b90SAlex Elder if (ipa_endpoint_replenish_one(endpoint, trans)) 13536a606b90SAlex Elder goto try_again_later; 1354b9dbabc5SAlex Elder 1355b9dbabc5SAlex Elder 1356b9dbabc5SAlex Elder /* Ring the doorbell if we've got a full batch */ 13579654d8c4SAlex Elder doorbell = !(++endpoint->replenish_count % IPA_REPLENISH_BATCH); 13589654d8c4SAlex Elder gsi_trans_commit(trans, doorbell); 1359b9dbabc5SAlex Elder } 1360998c0bd2SAlex Elder 1361998c0bd2SAlex Elder clear_bit(IPA_REPLENISH_ACTIVE, endpoint->replenish_flags); 1362998c0bd2SAlex Elder 136384f9bd12SAlex Elder return; 136484f9bd12SAlex Elder 136584f9bd12SAlex Elder try_again_later: 13666a606b90SAlex Elder gsi_trans_free(trans); 1367998c0bd2SAlex Elder clear_bit(IPA_REPLENISH_ACTIVE, endpoint->replenish_flags); 1368998c0bd2SAlex Elder 136984f9bd12SAlex Elder /* Whenever a receive buffer transaction completes we'll try to 137084f9bd12SAlex Elder * replenish again. It's unlikely, but if we fail to supply even 137184f9bd12SAlex Elder * one buffer, nothing will trigger another replenish attempt. 13725fc7f9baSAlex Elder * If the hardware has no receive buffers queued, schedule work to 13735fc7f9baSAlex Elder * try replenishing again. 137484f9bd12SAlex Elder */ 13755fc7f9baSAlex Elder if (gsi_channel_trans_idle(&endpoint->ipa->gsi, endpoint->channel_id)) 137684f9bd12SAlex Elder schedule_delayed_work(&endpoint->replenish_work, 137784f9bd12SAlex Elder msecs_to_jiffies(1)); 137884f9bd12SAlex Elder } 137984f9bd12SAlex Elder 138084f9bd12SAlex Elder static void ipa_endpoint_replenish_enable(struct ipa_endpoint *endpoint) 138184f9bd12SAlex Elder { 1382c1aaa01dSAlex Elder set_bit(IPA_REPLENISH_ENABLED, endpoint->replenish_flags); 138384f9bd12SAlex Elder 138484f9bd12SAlex Elder /* Start replenishing if hardware currently has no buffers */ 13855fc7f9baSAlex Elder if (gsi_channel_trans_idle(&endpoint->ipa->gsi, endpoint->channel_id)) 13864b22d841SAlex Elder ipa_endpoint_replenish(endpoint); 138784f9bd12SAlex Elder } 138884f9bd12SAlex Elder 138984f9bd12SAlex Elder static void ipa_endpoint_replenish_disable(struct ipa_endpoint *endpoint) 139084f9bd12SAlex Elder { 1391c1aaa01dSAlex Elder clear_bit(IPA_REPLENISH_ENABLED, endpoint->replenish_flags); 139284f9bd12SAlex Elder } 139384f9bd12SAlex Elder 139484f9bd12SAlex Elder static void ipa_endpoint_replenish_work(struct work_struct *work) 139584f9bd12SAlex Elder { 139684f9bd12SAlex Elder struct delayed_work *dwork = to_delayed_work(work); 139784f9bd12SAlex Elder struct ipa_endpoint *endpoint; 139884f9bd12SAlex Elder 139984f9bd12SAlex Elder endpoint = container_of(dwork, struct ipa_endpoint, replenish_work); 140084f9bd12SAlex Elder 14014b22d841SAlex Elder ipa_endpoint_replenish(endpoint); 140284f9bd12SAlex Elder } 140384f9bd12SAlex Elder 140484f9bd12SAlex Elder static void ipa_endpoint_skb_copy(struct ipa_endpoint *endpoint, 140584f9bd12SAlex Elder void *data, u32 len, u32 extra) 140684f9bd12SAlex Elder { 140784f9bd12SAlex Elder struct sk_buff *skb; 140884f9bd12SAlex Elder 14091b65bbccSAlex Elder if (!endpoint->netdev) 14101b65bbccSAlex Elder return; 14111b65bbccSAlex Elder 141284f9bd12SAlex Elder skb = __dev_alloc_skb(len, GFP_ATOMIC); 141330b338ffSAlex Elder if (skb) { 14141b65bbccSAlex Elder /* Copy the data into the socket buffer and receive it */ 141584f9bd12SAlex Elder skb_put(skb, len); 141684f9bd12SAlex Elder memcpy(skb->data, data, len); 141784f9bd12SAlex Elder skb->truesize += extra; 141830b338ffSAlex Elder } 141984f9bd12SAlex Elder 142084f9bd12SAlex Elder ipa_modem_skb_rx(endpoint->netdev, skb); 142184f9bd12SAlex Elder } 142284f9bd12SAlex Elder 142384f9bd12SAlex Elder static bool ipa_endpoint_skb_build(struct ipa_endpoint *endpoint, 142484f9bd12SAlex Elder struct page *page, u32 len) 142584f9bd12SAlex Elder { 1426660e52d6SAlex Elder u32 buffer_size = endpoint->config.rx.buffer_size; 142784f9bd12SAlex Elder struct sk_buff *skb; 142884f9bd12SAlex Elder 142984f9bd12SAlex Elder /* Nothing to do if there's no netdev */ 143084f9bd12SAlex Elder if (!endpoint->netdev) 143184f9bd12SAlex Elder return false; 143284f9bd12SAlex Elder 1433ed23f026SAlex Elder WARN_ON(len > SKB_WITH_OVERHEAD(buffer_size - NET_SKB_PAD)); 14345bc55884SAlex Elder 1435ed23f026SAlex Elder skb = build_skb(page_address(page), buffer_size); 143684f9bd12SAlex Elder if (skb) { 143784f9bd12SAlex Elder /* Reserve the headroom and account for the data */ 143884f9bd12SAlex Elder skb_reserve(skb, NET_SKB_PAD); 143984f9bd12SAlex Elder skb_put(skb, len); 144084f9bd12SAlex Elder } 144184f9bd12SAlex Elder 144284f9bd12SAlex Elder /* Receive the buffer (or record drop if unable to build it) */ 144384f9bd12SAlex Elder ipa_modem_skb_rx(endpoint->netdev, skb); 144484f9bd12SAlex Elder 144584f9bd12SAlex Elder return skb != NULL; 144684f9bd12SAlex Elder } 144784f9bd12SAlex Elder 144802c50774SAlex Elder /* The format of an IPA packet status structure is the same for several 144902c50774SAlex Elder * status types (opcodes). Other types aren't currently supported. 145084f9bd12SAlex Elder */ 145184f9bd12SAlex Elder static bool ipa_status_format_packet(enum ipa_status_opcode opcode) 145284f9bd12SAlex Elder { 145384f9bd12SAlex Elder switch (opcode) { 145484f9bd12SAlex Elder case IPA_STATUS_OPCODE_PACKET: 145584f9bd12SAlex Elder case IPA_STATUS_OPCODE_DROPPED_PACKET: 145684f9bd12SAlex Elder case IPA_STATUS_OPCODE_SUSPENDED_PACKET: 145784f9bd12SAlex Elder case IPA_STATUS_OPCODE_PACKET_2ND_PASS: 145884f9bd12SAlex Elder return true; 145984f9bd12SAlex Elder default: 146084f9bd12SAlex Elder return false; 146184f9bd12SAlex Elder } 146284f9bd12SAlex Elder } 146384f9bd12SAlex Elder 1464ebd2a82eSAlex Elder static bool 1465ebd2a82eSAlex Elder ipa_endpoint_status_skip(struct ipa_endpoint *endpoint, const void *data) 146684f9bd12SAlex Elder { 146755c6eae7SAlex Elder struct ipa *ipa = endpoint->ipa; 146802c50774SAlex Elder enum ipa_status_opcode opcode; 146984f9bd12SAlex Elder u32 endpoint_id; 147084f9bd12SAlex Elder 147155c6eae7SAlex Elder opcode = ipa_status_extract(ipa, data, STATUS_OPCODE); 147202c50774SAlex Elder if (!ipa_status_format_packet(opcode)) 147384f9bd12SAlex Elder return true; 147463a560b5SAlex Elder 147555c6eae7SAlex Elder endpoint_id = ipa_status_extract(ipa, data, STATUS_DST_ENDPOINT); 147684f9bd12SAlex Elder if (endpoint_id != endpoint->endpoint_id) 147784f9bd12SAlex Elder return true; 147884f9bd12SAlex Elder 147984f9bd12SAlex Elder return false; /* Don't skip this packet, process it */ 148084f9bd12SAlex Elder } 148184f9bd12SAlex Elder 1482ebd2a82eSAlex Elder static bool 1483ebd2a82eSAlex Elder ipa_endpoint_status_tag_valid(struct ipa_endpoint *endpoint, const void *data) 1484f6aba7b5SAlex Elder { 148551c48ce2SAlex Elder struct ipa_endpoint *command_endpoint; 148602c50774SAlex Elder enum ipa_status_mask status_mask; 148751c48ce2SAlex Elder struct ipa *ipa = endpoint->ipa; 148851c48ce2SAlex Elder u32 endpoint_id; 148951c48ce2SAlex Elder 149055c6eae7SAlex Elder status_mask = ipa_status_extract(ipa, data, STATUS_MASK); 149102c50774SAlex Elder if (!status_mask) 149251c48ce2SAlex Elder return false; /* No valid tag */ 149351c48ce2SAlex Elder 149451c48ce2SAlex Elder /* The status contains a valid tag. We know the packet was sent to 149551c48ce2SAlex Elder * this endpoint (already verified by ipa_endpoint_status_skip()). 149651c48ce2SAlex Elder * If the packet came from the AP->command TX endpoint we know 149751c48ce2SAlex Elder * this packet was sent as part of the pipeline clear process. 149851c48ce2SAlex Elder */ 149955c6eae7SAlex Elder endpoint_id = ipa_status_extract(ipa, data, STATUS_SRC_ENDPOINT); 150051c48ce2SAlex Elder command_endpoint = ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]; 150151c48ce2SAlex Elder if (endpoint_id == command_endpoint->endpoint_id) { 150251c48ce2SAlex Elder complete(&ipa->completion); 150351c48ce2SAlex Elder } else { 15045245f4fdSAlex Elder dev_err(ipa->dev, "unexpected tagged packet from endpoint %u\n", 150551c48ce2SAlex Elder endpoint_id); 150651c48ce2SAlex Elder } 150751c48ce2SAlex Elder 150851c48ce2SAlex Elder return true; 1509f6aba7b5SAlex Elder } 1510f6aba7b5SAlex Elder 151184f9bd12SAlex Elder /* Return whether the status indicates the packet should be dropped */ 1512ebd2a82eSAlex Elder static bool 1513ebd2a82eSAlex Elder ipa_endpoint_status_drop(struct ipa_endpoint *endpoint, const void *data) 151484f9bd12SAlex Elder { 151502c50774SAlex Elder enum ipa_status_exception exception; 151655c6eae7SAlex Elder struct ipa *ipa = endpoint->ipa; 151702c50774SAlex Elder u32 rule; 151884f9bd12SAlex Elder 1519f6aba7b5SAlex Elder /* If the status indicates a tagged transfer, we'll drop the packet */ 1520ebd2a82eSAlex Elder if (ipa_endpoint_status_tag_valid(endpoint, data)) 1521f6aba7b5SAlex Elder return true; 1522f6aba7b5SAlex Elder 1523ab4f71e5SAlex Elder /* Deaggregation exceptions we drop; all other types we consume */ 152455c6eae7SAlex Elder exception = ipa_status_extract(ipa, data, STATUS_EXCEPTION); 152502c50774SAlex Elder if (exception) 152602c50774SAlex Elder return exception == IPA_STATUS_EXCEPTION_DEAGGR; 152784f9bd12SAlex Elder 152884f9bd12SAlex Elder /* Drop the packet if it fails to match a routing rule; otherwise no */ 152955c6eae7SAlex Elder rule = ipa_status_extract(ipa, data, STATUS_ROUTER_RULE_INDEX); 153084f9bd12SAlex Elder 1531ebd2a82eSAlex Elder return rule == IPA_STATUS_RULE_MISS; 153284f9bd12SAlex Elder } 153384f9bd12SAlex Elder 153484f9bd12SAlex Elder static void ipa_endpoint_status_parse(struct ipa_endpoint *endpoint, 153584f9bd12SAlex Elder struct page *page, u32 total_len) 153684f9bd12SAlex Elder { 1537660e52d6SAlex Elder u32 buffer_size = endpoint->config.rx.buffer_size; 153884f9bd12SAlex Elder void *data = page_address(page) + NET_SKB_PAD; 1539ed23f026SAlex Elder u32 unused = buffer_size - total_len; 154055c6eae7SAlex Elder struct ipa *ipa = endpoint->ipa; 15415245f4fdSAlex Elder struct device *dev = ipa->dev; 154284f9bd12SAlex Elder u32 resid = total_len; 154384f9bd12SAlex Elder 154484f9bd12SAlex Elder while (resid) { 154563a560b5SAlex Elder u32 length; 154684f9bd12SAlex Elder u32 align; 154784f9bd12SAlex Elder u32 len; 154884f9bd12SAlex Elder 1549b8dc7d0eSAlex Elder if (resid < IPA_STATUS_SIZE) { 15505245f4fdSAlex Elder dev_err(dev, 155184f9bd12SAlex Elder "short message (%u bytes < %zu byte status)\n", 1552b8dc7d0eSAlex Elder resid, IPA_STATUS_SIZE); 155384f9bd12SAlex Elder break; 155484f9bd12SAlex Elder } 155584f9bd12SAlex Elder 155684f9bd12SAlex Elder /* Skip over status packets that lack packet data */ 155755c6eae7SAlex Elder length = ipa_status_extract(ipa, data, STATUS_LENGTH); 155802c50774SAlex Elder if (!length || ipa_endpoint_status_skip(endpoint, data)) { 1559b8dc7d0eSAlex Elder data += IPA_STATUS_SIZE; 1560b8dc7d0eSAlex Elder resid -= IPA_STATUS_SIZE; 156184f9bd12SAlex Elder continue; 156284f9bd12SAlex Elder } 156384f9bd12SAlex Elder 1564162fbc6fSAlex Elder /* Compute the amount of buffer space consumed by the packet, 156502c50774SAlex Elder * including the status. If the hardware is configured to 156602c50774SAlex Elder * pad packet data to an aligned boundary, account for that. 1567162fbc6fSAlex Elder * And if checksum offload is enabled a trailer containing 1568162fbc6fSAlex Elder * computed checksum information will be appended. 156984f9bd12SAlex Elder */ 1570660e52d6SAlex Elder align = endpoint->config.rx.pad_align ? : 1; 1571b8dc7d0eSAlex Elder len = IPA_STATUS_SIZE + ALIGN(length, align); 1572660e52d6SAlex Elder if (endpoint->config.checksum) 157384f9bd12SAlex Elder len += sizeof(struct rmnet_map_dl_csum_trailer); 157484f9bd12SAlex Elder 157502c50774SAlex Elder if (!ipa_endpoint_status_drop(endpoint, data)) { 1576162fbc6fSAlex Elder void *data2; 1577162fbc6fSAlex Elder u32 extra; 157884f9bd12SAlex Elder 157984f9bd12SAlex Elder /* Client receives only packet data (no status) */ 1580b8dc7d0eSAlex Elder data2 = data + IPA_STATUS_SIZE; 1581162fbc6fSAlex Elder 1582162fbc6fSAlex Elder /* Have the true size reflect the extra unused space in 1583162fbc6fSAlex Elder * the original receive buffer. Distribute the "cost" 1584162fbc6fSAlex Elder * proportionately across all aggregated packets in the 1585162fbc6fSAlex Elder * buffer. 1586162fbc6fSAlex Elder */ 1587162fbc6fSAlex Elder extra = DIV_ROUND_CLOSEST(unused * len, total_len); 158863a560b5SAlex Elder ipa_endpoint_skb_copy(endpoint, data2, length, extra); 158984f9bd12SAlex Elder } 159084f9bd12SAlex Elder 159184f9bd12SAlex Elder /* Consume status and the full packet it describes */ 159284f9bd12SAlex Elder data += len; 159384f9bd12SAlex Elder resid -= len; 159484f9bd12SAlex Elder } 159584f9bd12SAlex Elder } 159684f9bd12SAlex Elder 1597983a1a30SAlex Elder void ipa_endpoint_trans_complete(struct ipa_endpoint *endpoint, 159884f9bd12SAlex Elder struct gsi_trans *trans) 159984f9bd12SAlex Elder { 160084f9bd12SAlex Elder struct page *page; 160184f9bd12SAlex Elder 1602983a1a30SAlex Elder if (endpoint->toward_ipa) 1603983a1a30SAlex Elder return; 1604983a1a30SAlex Elder 160584f9bd12SAlex Elder if (trans->cancelled) 16065d6ac24fSAlex Elder goto done; 160784f9bd12SAlex Elder 160884f9bd12SAlex Elder /* Parse or build a socket buffer using the actual received length */ 160984f9bd12SAlex Elder page = trans->data; 1610660e52d6SAlex Elder if (endpoint->config.status_enable) 161184f9bd12SAlex Elder ipa_endpoint_status_parse(endpoint, page, trans->len); 161284f9bd12SAlex Elder else if (ipa_endpoint_skb_build(endpoint, page, trans->len)) 161384f9bd12SAlex Elder trans->data = NULL; /* Pages have been consumed */ 16145d6ac24fSAlex Elder done: 16155d6ac24fSAlex Elder ipa_endpoint_replenish(endpoint); 161684f9bd12SAlex Elder } 161784f9bd12SAlex Elder 161884f9bd12SAlex Elder void ipa_endpoint_trans_release(struct ipa_endpoint *endpoint, 161984f9bd12SAlex Elder struct gsi_trans *trans) 162084f9bd12SAlex Elder { 162184f9bd12SAlex Elder if (endpoint->toward_ipa) { 162284f9bd12SAlex Elder struct ipa *ipa = endpoint->ipa; 162384f9bd12SAlex Elder 162484f9bd12SAlex Elder /* Nothing to do for command transactions */ 162584f9bd12SAlex Elder if (endpoint != ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]) { 162684f9bd12SAlex Elder struct sk_buff *skb = trans->data; 162784f9bd12SAlex Elder 162884f9bd12SAlex Elder if (skb) 162984f9bd12SAlex Elder dev_kfree_skb_any(skb); 163084f9bd12SAlex Elder } 163184f9bd12SAlex Elder } else { 163284f9bd12SAlex Elder struct page *page = trans->data; 163384f9bd12SAlex Elder 1634155c0c90SAlex Elder if (page) 1635155c0c90SAlex Elder put_page(page); 163684f9bd12SAlex Elder } 163784f9bd12SAlex Elder } 163884f9bd12SAlex Elder 163984f9bd12SAlex Elder void ipa_endpoint_default_route_set(struct ipa *ipa, u32 endpoint_id) 164084f9bd12SAlex Elder { 164181772e44SAlex Elder const struct reg *reg; 164284f9bd12SAlex Elder u32 val; 164384f9bd12SAlex Elder 16446a244b75SAlex Elder reg = ipa_reg(ipa, ROUTE); 164584f9bd12SAlex Elder /* ROUTE_DIS is 0 */ 1646f1470fd7SAlex Elder val = reg_encode(reg, ROUTE_DEF_PIPE, endpoint_id); 1647f1470fd7SAlex Elder val |= reg_bit(reg, ROUTE_DEF_HDR_TABLE); 1648479deb32SAlex Elder /* ROUTE_DEF_HDR_OFST is 0 */ 1649f1470fd7SAlex Elder val |= reg_encode(reg, ROUTE_FRAG_DEF_PIPE, endpoint_id); 1650f1470fd7SAlex Elder val |= reg_bit(reg, ROUTE_DEF_RETAIN_HDR); 165184f9bd12SAlex Elder 1652fc4cecf7SAlex Elder iowrite32(val, ipa->reg_virt + reg_offset(reg)); 165384f9bd12SAlex Elder } 165484f9bd12SAlex Elder 165584f9bd12SAlex Elder void ipa_endpoint_default_route_clear(struct ipa *ipa) 165684f9bd12SAlex Elder { 165784f9bd12SAlex Elder ipa_endpoint_default_route_set(ipa, 0); 165884f9bd12SAlex Elder } 165984f9bd12SAlex Elder 166084f9bd12SAlex Elder /** 166184f9bd12SAlex Elder * ipa_endpoint_reset_rx_aggr() - Reset RX endpoint with aggregation active 166284f9bd12SAlex Elder * @endpoint: Endpoint to be reset 166384f9bd12SAlex Elder * 166484f9bd12SAlex Elder * If aggregation is active on an RX endpoint when a reset is performed 166584f9bd12SAlex Elder * on its underlying GSI channel, a special sequence of actions must be 166684f9bd12SAlex Elder * taken to ensure the IPA pipeline is properly cleared. 166784f9bd12SAlex Elder * 1668e3eea08eSAlex Elder * Return: 0 if successful, or a negative error code 166984f9bd12SAlex Elder */ 167084f9bd12SAlex Elder static int ipa_endpoint_reset_rx_aggr(struct ipa_endpoint *endpoint) 167184f9bd12SAlex Elder { 167284f9bd12SAlex Elder struct ipa *ipa = endpoint->ipa; 16735245f4fdSAlex Elder struct device *dev = ipa->dev; 167484f9bd12SAlex Elder struct gsi *gsi = &ipa->gsi; 16754fa95248SAlex Elder bool suspended = false; 167684f9bd12SAlex Elder dma_addr_t addr; 167784f9bd12SAlex Elder u32 retries; 167884f9bd12SAlex Elder u32 len = 1; 167984f9bd12SAlex Elder void *virt; 168084f9bd12SAlex Elder int ret; 168184f9bd12SAlex Elder 168284f9bd12SAlex Elder virt = kzalloc(len, GFP_KERNEL); 168384f9bd12SAlex Elder if (!virt) 168484f9bd12SAlex Elder return -ENOMEM; 168584f9bd12SAlex Elder 168684f9bd12SAlex Elder addr = dma_map_single(dev, virt, len, DMA_FROM_DEVICE); 168784f9bd12SAlex Elder if (dma_mapping_error(dev, addr)) { 168884f9bd12SAlex Elder ret = -ENOMEM; 168984f9bd12SAlex Elder goto out_kfree; 169084f9bd12SAlex Elder } 169184f9bd12SAlex Elder 169284f9bd12SAlex Elder /* Force close aggregation before issuing the reset */ 169384f9bd12SAlex Elder ipa_endpoint_force_close(endpoint); 169484f9bd12SAlex Elder 169584f9bd12SAlex Elder /* Reset and reconfigure the channel with the doorbell engine 169684f9bd12SAlex Elder * disabled. Then poll until we know aggregation is no longer 169784f9bd12SAlex Elder * active. We'll re-enable the doorbell (if appropriate) when 169884f9bd12SAlex Elder * we reset again below. 169984f9bd12SAlex Elder */ 170084f9bd12SAlex Elder gsi_channel_reset(gsi, endpoint->channel_id, false); 170184f9bd12SAlex Elder 170284f9bd12SAlex Elder /* Make sure the channel isn't suspended */ 17034fa95248SAlex Elder suspended = ipa_endpoint_program_suspend(endpoint, false); 170484f9bd12SAlex Elder 170584f9bd12SAlex Elder /* Start channel and do a 1 byte read */ 170684f9bd12SAlex Elder ret = gsi_channel_start(gsi, endpoint->channel_id); 170784f9bd12SAlex Elder if (ret) 170884f9bd12SAlex Elder goto out_suspend_again; 170984f9bd12SAlex Elder 171084f9bd12SAlex Elder ret = gsi_trans_read_byte(gsi, endpoint->channel_id, addr); 171184f9bd12SAlex Elder if (ret) 171284f9bd12SAlex Elder goto err_endpoint_stop; 171384f9bd12SAlex Elder 171484f9bd12SAlex Elder /* Wait for aggregation to be closed on the channel */ 171584f9bd12SAlex Elder retries = IPA_ENDPOINT_RESET_AGGR_RETRY_MAX; 171684f9bd12SAlex Elder do { 171784f9bd12SAlex Elder if (!ipa_endpoint_aggr_active(endpoint)) 171884f9bd12SAlex Elder break; 171974401946SAlex Elder usleep_range(USEC_PER_MSEC, 2 * USEC_PER_MSEC); 172084f9bd12SAlex Elder } while (retries--); 172184f9bd12SAlex Elder 172284f9bd12SAlex Elder /* Check one last time */ 172384f9bd12SAlex Elder if (ipa_endpoint_aggr_active(endpoint)) 172484f9bd12SAlex Elder dev_err(dev, "endpoint %u still active during reset\n", 172584f9bd12SAlex Elder endpoint->endpoint_id); 172684f9bd12SAlex Elder 172784f9bd12SAlex Elder gsi_trans_read_byte_done(gsi, endpoint->channel_id); 172884f9bd12SAlex Elder 1729f30dcb7dSAlex Elder ret = gsi_channel_stop(gsi, endpoint->channel_id); 173084f9bd12SAlex Elder if (ret) 173184f9bd12SAlex Elder goto out_suspend_again; 173284f9bd12SAlex Elder 1733497abc87SPeng Li /* Finally, reset and reconfigure the channel again (re-enabling 173484f9bd12SAlex Elder * the doorbell engine if appropriate). Sleep for 1 millisecond to 173584f9bd12SAlex Elder * complete the channel reset sequence. Finish by suspending the 173684f9bd12SAlex Elder * channel again (if necessary). 173784f9bd12SAlex Elder */ 1738ce54993dSAlex Elder gsi_channel_reset(gsi, endpoint->channel_id, true); 173984f9bd12SAlex Elder 174074401946SAlex Elder usleep_range(USEC_PER_MSEC, 2 * USEC_PER_MSEC); 174184f9bd12SAlex Elder 174284f9bd12SAlex Elder goto out_suspend_again; 174384f9bd12SAlex Elder 174484f9bd12SAlex Elder err_endpoint_stop: 1745f30dcb7dSAlex Elder (void)gsi_channel_stop(gsi, endpoint->channel_id); 174684f9bd12SAlex Elder out_suspend_again: 17474fa95248SAlex Elder if (suspended) 17484fa95248SAlex Elder (void)ipa_endpoint_program_suspend(endpoint, true); 174984f9bd12SAlex Elder dma_unmap_single(dev, addr, len, DMA_FROM_DEVICE); 175084f9bd12SAlex Elder out_kfree: 175184f9bd12SAlex Elder kfree(virt); 175284f9bd12SAlex Elder 175384f9bd12SAlex Elder return ret; 175484f9bd12SAlex Elder } 175584f9bd12SAlex Elder 175684f9bd12SAlex Elder static void ipa_endpoint_reset(struct ipa_endpoint *endpoint) 175784f9bd12SAlex Elder { 175884f9bd12SAlex Elder u32 channel_id = endpoint->channel_id; 175984f9bd12SAlex Elder struct ipa *ipa = endpoint->ipa; 176084f9bd12SAlex Elder bool special; 176184f9bd12SAlex Elder int ret = 0; 176284f9bd12SAlex Elder 176384f9bd12SAlex Elder /* On IPA v3.5.1, if an RX endpoint is reset while aggregation 176484f9bd12SAlex Elder * is active, we need to handle things specially to recover. 176584f9bd12SAlex Elder * All other cases just need to reset the underlying GSI channel. 176684f9bd12SAlex Elder */ 1767d7f3087bSAlex Elder special = ipa->version < IPA_VERSION_4_0 && !endpoint->toward_ipa && 1768660e52d6SAlex Elder endpoint->config.aggregation; 1769ce54993dSAlex Elder if (special && ipa_endpoint_aggr_active(endpoint)) 177084f9bd12SAlex Elder ret = ipa_endpoint_reset_rx_aggr(endpoint); 177184f9bd12SAlex Elder else 1772ce54993dSAlex Elder gsi_channel_reset(&ipa->gsi, channel_id, true); 177384f9bd12SAlex Elder 177484f9bd12SAlex Elder if (ret) 17755245f4fdSAlex Elder dev_err(ipa->dev, 177684f9bd12SAlex Elder "error %d resetting channel %u for endpoint %u\n", 177784f9bd12SAlex Elder ret, endpoint->channel_id, endpoint->endpoint_id); 177884f9bd12SAlex Elder } 177984f9bd12SAlex Elder 178084f9bd12SAlex Elder static void ipa_endpoint_program(struct ipa_endpoint *endpoint) 178184f9bd12SAlex Elder { 17824c9d631aSAlex Elder if (endpoint->toward_ipa) { 17834c9d631aSAlex Elder /* Newer versions of IPA use GSI channel flow control 17844c9d631aSAlex Elder * instead of endpoint DELAY mode to prevent sending data. 17854c9d631aSAlex Elder * Flow control is disabled for newly-allocated channels, 17864c9d631aSAlex Elder * and we can assume flow control is not (ever) enabled 17874c9d631aSAlex Elder * for AP TX channels. 17884c9d631aSAlex Elder */ 17894c9d631aSAlex Elder if (endpoint->ipa->version < IPA_VERSION_4_2) 1790a4dcad34SAlex Elder ipa_endpoint_program_delay(endpoint, false); 17914c9d631aSAlex Elder } else { 17924c9d631aSAlex Elder /* Ensure suspend mode is off on all AP RX endpoints */ 1793fb57c3eaSAlex Elder (void)ipa_endpoint_program_suspend(endpoint, false); 17944c9d631aSAlex Elder } 1795fb57c3eaSAlex Elder ipa_endpoint_init_cfg(endpoint); 1796647a05f3SAlex Elder ipa_endpoint_init_nat(endpoint); 1797fb57c3eaSAlex Elder ipa_endpoint_init_hdr(endpoint); 179884f9bd12SAlex Elder ipa_endpoint_init_hdr_ext(endpoint); 1799fb57c3eaSAlex Elder ipa_endpoint_init_hdr_metadata_mask(endpoint); 1800fb57c3eaSAlex Elder ipa_endpoint_init_mode(endpoint); 180184f9bd12SAlex Elder ipa_endpoint_init_aggr(endpoint); 1802153213f0SAlex Elder if (!endpoint->toward_ipa) { 1803153213f0SAlex Elder if (endpoint->config.rx.holb_drop) 1804153213f0SAlex Elder ipa_endpoint_init_hol_block_enable(endpoint, 0); 1805153213f0SAlex Elder else 180601c36637SAlex Elder ipa_endpoint_init_hol_block_disable(endpoint); 1807153213f0SAlex Elder } 180884f9bd12SAlex Elder ipa_endpoint_init_deaggr(endpoint); 18092d265342SAlex Elder ipa_endpoint_init_rsrc_grp(endpoint); 181084f9bd12SAlex Elder ipa_endpoint_init_seq(endpoint); 181184f9bd12SAlex Elder ipa_endpoint_status(endpoint); 181284f9bd12SAlex Elder } 181384f9bd12SAlex Elder 181484f9bd12SAlex Elder int ipa_endpoint_enable_one(struct ipa_endpoint *endpoint) 181584f9bd12SAlex Elder { 18169b7a0065SAlex Elder u32 endpoint_id = endpoint->endpoint_id; 181784f9bd12SAlex Elder struct ipa *ipa = endpoint->ipa; 181884f9bd12SAlex Elder struct gsi *gsi = &ipa->gsi; 181984f9bd12SAlex Elder int ret; 182084f9bd12SAlex Elder 182184f9bd12SAlex Elder ret = gsi_channel_start(gsi, endpoint->channel_id); 182284f9bd12SAlex Elder if (ret) { 18235245f4fdSAlex Elder dev_err(ipa->dev, 182484f9bd12SAlex Elder "error %d starting %cX channel %u for endpoint %u\n", 182584f9bd12SAlex Elder ret, endpoint->toward_ipa ? 'T' : 'R', 18269b7a0065SAlex Elder endpoint->channel_id, endpoint_id); 182784f9bd12SAlex Elder return ret; 182884f9bd12SAlex Elder } 182984f9bd12SAlex Elder 183084f9bd12SAlex Elder if (!endpoint->toward_ipa) { 18319b7a0065SAlex Elder ipa_interrupt_suspend_enable(ipa->interrupt, endpoint_id); 183284f9bd12SAlex Elder ipa_endpoint_replenish_enable(endpoint); 183384f9bd12SAlex Elder } 183484f9bd12SAlex Elder 18359b7a0065SAlex Elder __set_bit(endpoint_id, ipa->enabled); 183684f9bd12SAlex Elder 183784f9bd12SAlex Elder return 0; 183884f9bd12SAlex Elder } 183984f9bd12SAlex Elder 184084f9bd12SAlex Elder void ipa_endpoint_disable_one(struct ipa_endpoint *endpoint) 184184f9bd12SAlex Elder { 18429b7a0065SAlex Elder u32 endpoint_id = endpoint->endpoint_id; 184384f9bd12SAlex Elder struct ipa *ipa = endpoint->ipa; 1844f30dcb7dSAlex Elder struct gsi *gsi = &ipa->gsi; 184584f9bd12SAlex Elder int ret; 184684f9bd12SAlex Elder 18479b7a0065SAlex Elder if (!test_bit(endpoint_id, ipa->enabled)) 184884f9bd12SAlex Elder return; 184984f9bd12SAlex Elder 18509b7a0065SAlex Elder __clear_bit(endpoint_id, endpoint->ipa->enabled); 185184f9bd12SAlex Elder 185284f9bd12SAlex Elder if (!endpoint->toward_ipa) { 185384f9bd12SAlex Elder ipa_endpoint_replenish_disable(endpoint); 18549b7a0065SAlex Elder ipa_interrupt_suspend_disable(ipa->interrupt, endpoint_id); 185584f9bd12SAlex Elder } 185684f9bd12SAlex Elder 185784f9bd12SAlex Elder /* Note that if stop fails, the channel's state is not well-defined */ 1858f30dcb7dSAlex Elder ret = gsi_channel_stop(gsi, endpoint->channel_id); 185984f9bd12SAlex Elder if (ret) 18605245f4fdSAlex Elder dev_err(ipa->dev, "error %d attempting to stop endpoint %u\n", 18615245f4fdSAlex Elder ret, endpoint_id); 186284f9bd12SAlex Elder } 186384f9bd12SAlex Elder 186484f9bd12SAlex Elder void ipa_endpoint_suspend_one(struct ipa_endpoint *endpoint) 186584f9bd12SAlex Elder { 18665245f4fdSAlex Elder struct device *dev = endpoint->ipa->dev; 186784f9bd12SAlex Elder struct gsi *gsi = &endpoint->ipa->gsi; 186884f9bd12SAlex Elder int ret; 186984f9bd12SAlex Elder 18709b7a0065SAlex Elder if (!test_bit(endpoint->endpoint_id, endpoint->ipa->enabled)) 187184f9bd12SAlex Elder return; 187284f9bd12SAlex Elder 1873ab4f71e5SAlex Elder if (!endpoint->toward_ipa) { 187484f9bd12SAlex Elder ipa_endpoint_replenish_disable(endpoint); 18754fa95248SAlex Elder (void)ipa_endpoint_program_suspend(endpoint, true); 1876ab4f71e5SAlex Elder } 187784f9bd12SAlex Elder 1878decfef0fSAlex Elder ret = gsi_channel_suspend(gsi, endpoint->channel_id); 187984f9bd12SAlex Elder if (ret) 188084f9bd12SAlex Elder dev_err(dev, "error %d suspending channel %u\n", ret, 188184f9bd12SAlex Elder endpoint->channel_id); 188284f9bd12SAlex Elder } 188384f9bd12SAlex Elder 188484f9bd12SAlex Elder void ipa_endpoint_resume_one(struct ipa_endpoint *endpoint) 188584f9bd12SAlex Elder { 18865245f4fdSAlex Elder struct device *dev = endpoint->ipa->dev; 188784f9bd12SAlex Elder struct gsi *gsi = &endpoint->ipa->gsi; 188884f9bd12SAlex Elder int ret; 188984f9bd12SAlex Elder 18909b7a0065SAlex Elder if (!test_bit(endpoint->endpoint_id, endpoint->ipa->enabled)) 189184f9bd12SAlex Elder return; 189284f9bd12SAlex Elder 1893b07f283eSAlex Elder if (!endpoint->toward_ipa) 18944fa95248SAlex Elder (void)ipa_endpoint_program_suspend(endpoint, false); 189584f9bd12SAlex Elder 1896decfef0fSAlex Elder ret = gsi_channel_resume(gsi, endpoint->channel_id); 189784f9bd12SAlex Elder if (ret) 189884f9bd12SAlex Elder dev_err(dev, "error %d resuming channel %u\n", ret, 189984f9bd12SAlex Elder endpoint->channel_id); 190084f9bd12SAlex Elder else if (!endpoint->toward_ipa) 190184f9bd12SAlex Elder ipa_endpoint_replenish_enable(endpoint); 190284f9bd12SAlex Elder } 190384f9bd12SAlex Elder 190484f9bd12SAlex Elder void ipa_endpoint_suspend(struct ipa *ipa) 190584f9bd12SAlex Elder { 1906d1704382SAlex Elder if (!ipa->setup_complete) 1907d1704382SAlex Elder return; 1908d1704382SAlex Elder 190984f9bd12SAlex Elder if (ipa->modem_netdev) 191084f9bd12SAlex Elder ipa_modem_suspend(ipa->modem_netdev); 191184f9bd12SAlex Elder 191284f9bd12SAlex Elder ipa_endpoint_suspend_one(ipa->name_map[IPA_ENDPOINT_AP_LAN_RX]); 191384f9bd12SAlex Elder ipa_endpoint_suspend_one(ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]); 191484f9bd12SAlex Elder } 191584f9bd12SAlex Elder 191684f9bd12SAlex Elder void ipa_endpoint_resume(struct ipa *ipa) 191784f9bd12SAlex Elder { 1918d1704382SAlex Elder if (!ipa->setup_complete) 1919d1704382SAlex Elder return; 1920d1704382SAlex Elder 192184f9bd12SAlex Elder ipa_endpoint_resume_one(ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]); 192284f9bd12SAlex Elder ipa_endpoint_resume_one(ipa->name_map[IPA_ENDPOINT_AP_LAN_RX]); 192384f9bd12SAlex Elder 192484f9bd12SAlex Elder if (ipa->modem_netdev) 192584f9bd12SAlex Elder ipa_modem_resume(ipa->modem_netdev); 192684f9bd12SAlex Elder } 192784f9bd12SAlex Elder 192884f9bd12SAlex Elder static void ipa_endpoint_setup_one(struct ipa_endpoint *endpoint) 192984f9bd12SAlex Elder { 193084f9bd12SAlex Elder struct gsi *gsi = &endpoint->ipa->gsi; 193184f9bd12SAlex Elder u32 channel_id = endpoint->channel_id; 193284f9bd12SAlex Elder 193384f9bd12SAlex Elder /* Only AP endpoints get set up */ 193484f9bd12SAlex Elder if (endpoint->ee_id != GSI_EE_AP) 193584f9bd12SAlex Elder return; 193684f9bd12SAlex Elder 1937317595d2SAlex Elder endpoint->skb_frag_max = gsi->channel[channel_id].trans_tre_max - 1; 193884f9bd12SAlex Elder if (!endpoint->toward_ipa) { 193984f9bd12SAlex Elder /* RX transactions require a single TRE, so the maximum 194084f9bd12SAlex Elder * backlog is the same as the maximum outstanding TREs. 194184f9bd12SAlex Elder */ 1942c1aaa01dSAlex Elder clear_bit(IPA_REPLENISH_ENABLED, endpoint->replenish_flags); 1943998c0bd2SAlex Elder clear_bit(IPA_REPLENISH_ACTIVE, endpoint->replenish_flags); 194484f9bd12SAlex Elder INIT_DELAYED_WORK(&endpoint->replenish_work, 194584f9bd12SAlex Elder ipa_endpoint_replenish_work); 194684f9bd12SAlex Elder } 194784f9bd12SAlex Elder 194884f9bd12SAlex Elder ipa_endpoint_program(endpoint); 194984f9bd12SAlex Elder 1950ae5108e9SAlex Elder __set_bit(endpoint->endpoint_id, endpoint->ipa->set_up); 195184f9bd12SAlex Elder } 195284f9bd12SAlex Elder 195384f9bd12SAlex Elder static void ipa_endpoint_teardown_one(struct ipa_endpoint *endpoint) 195484f9bd12SAlex Elder { 1955ae5108e9SAlex Elder __clear_bit(endpoint->endpoint_id, endpoint->ipa->set_up); 195684f9bd12SAlex Elder 195784f9bd12SAlex Elder if (!endpoint->toward_ipa) 195884f9bd12SAlex Elder cancel_delayed_work_sync(&endpoint->replenish_work); 195984f9bd12SAlex Elder 196084f9bd12SAlex Elder ipa_endpoint_reset(endpoint); 196184f9bd12SAlex Elder } 196284f9bd12SAlex Elder 196384f9bd12SAlex Elder void ipa_endpoint_setup(struct ipa *ipa) 196484f9bd12SAlex Elder { 19659a9f5129SAlex Elder u32 endpoint_id; 196684f9bd12SAlex Elder 19679a9f5129SAlex Elder for_each_set_bit(endpoint_id, ipa->defined, ipa->endpoint_count) 196884f9bd12SAlex Elder ipa_endpoint_setup_one(&ipa->endpoint[endpoint_id]); 196984f9bd12SAlex Elder } 197084f9bd12SAlex Elder 197184f9bd12SAlex Elder void ipa_endpoint_teardown(struct ipa *ipa) 197284f9bd12SAlex Elder { 1973ae5108e9SAlex Elder u32 endpoint_id; 197484f9bd12SAlex Elder 1975ae5108e9SAlex Elder for_each_set_bit(endpoint_id, ipa->set_up, ipa->endpoint_count) 197684f9bd12SAlex Elder ipa_endpoint_teardown_one(&ipa->endpoint[endpoint_id]); 197784f9bd12SAlex Elder } 197884f9bd12SAlex Elder 197988de7672SAlex Elder void ipa_endpoint_deconfig(struct ipa *ipa) 198088de7672SAlex Elder { 198188de7672SAlex Elder ipa->available_count = 0; 198288de7672SAlex Elder bitmap_free(ipa->available); 198388de7672SAlex Elder ipa->available = NULL; 198488de7672SAlex Elder } 198588de7672SAlex Elder 198684f9bd12SAlex Elder int ipa_endpoint_config(struct ipa *ipa) 198784f9bd12SAlex Elder { 19885245f4fdSAlex Elder struct device *dev = ipa->dev; 198981772e44SAlex Elder const struct reg *reg; 19909a9f5129SAlex Elder u32 endpoint_id; 199107abde54SAlex Elder u32 hw_limit; 19922b87d721SAlex Elder u32 tx_count; 19932b87d721SAlex Elder u32 rx_count; 199484f9bd12SAlex Elder u32 rx_base; 19952b87d721SAlex Elder u32 limit; 199684f9bd12SAlex Elder u32 val; 199784f9bd12SAlex Elder 1998110971d1SAlex Elder /* Prior to IPA v3.5, the FLAVOR_0 register was not supported. 1999110971d1SAlex Elder * Furthermore, the endpoints were not grouped such that TX 2000110971d1SAlex Elder * endpoint numbers started with 0 and RX endpoints had numbers 2001110971d1SAlex Elder * higher than all TX endpoints, so we can't do the simple 2002110971d1SAlex Elder * direction check used for newer hardware below. 2003110971d1SAlex Elder * 2004110971d1SAlex Elder * For hardware that doesn't support the FLAVOR_0 register, 2005110971d1SAlex Elder * just set the available mask to support any endpoint, and 2006110971d1SAlex Elder * assume the configuration is valid. 2007110971d1SAlex Elder */ 2008110971d1SAlex Elder if (ipa->version < IPA_VERSION_3_5) { 200988de7672SAlex Elder ipa->available = bitmap_zalloc(IPA_ENDPOINT_MAX, GFP_KERNEL); 201088de7672SAlex Elder if (!ipa->available) 201188de7672SAlex Elder return -ENOMEM; 201288de7672SAlex Elder ipa->available_count = IPA_ENDPOINT_MAX; 201388de7672SAlex Elder 201488de7672SAlex Elder bitmap_set(ipa->available, 0, IPA_ENDPOINT_MAX); 201588de7672SAlex Elder 2016110971d1SAlex Elder return 0; 2017110971d1SAlex Elder } 2018110971d1SAlex Elder 201984f9bd12SAlex Elder /* Find out about the endpoints supplied by the hardware, and ensure 20202b87d721SAlex Elder * the highest one doesn't exceed the number supported by software. 202184f9bd12SAlex Elder */ 20226a244b75SAlex Elder reg = ipa_reg(ipa, FLAVOR_0); 2023fc4cecf7SAlex Elder val = ioread32(ipa->reg_virt + reg_offset(reg)); 202484f9bd12SAlex Elder 20252b87d721SAlex Elder /* Our RX is an IPA producer; our TX is an IPA consumer. */ 2026f1470fd7SAlex Elder tx_count = reg_decode(reg, MAX_CONS_PIPES, val); 2027f1470fd7SAlex Elder rx_count = reg_decode(reg, MAX_PROD_PIPES, val); 2028f1470fd7SAlex Elder rx_base = reg_decode(reg, PROD_LOWEST, val); 20292b87d721SAlex Elder 20302b87d721SAlex Elder limit = rx_base + rx_count; 20312b87d721SAlex Elder if (limit > IPA_ENDPOINT_MAX) { 20322b87d721SAlex Elder dev_err(dev, "too many endpoints, %u > %u\n", 20332b87d721SAlex Elder limit, IPA_ENDPOINT_MAX); 203484f9bd12SAlex Elder return -EINVAL; 203584f9bd12SAlex Elder } 203684f9bd12SAlex Elder 203707abde54SAlex Elder /* Until IPA v5.0, the max endpoint ID was 32 */ 203807abde54SAlex Elder hw_limit = ipa->version < IPA_VERSION_5_0 ? 32 : U8_MAX + 1; 203907abde54SAlex Elder if (limit > hw_limit) { 204007abde54SAlex Elder dev_err(dev, "unexpected endpoint count, %u > %u\n", 204107abde54SAlex Elder limit, hw_limit); 204207abde54SAlex Elder return -EINVAL; 204307abde54SAlex Elder } 204407abde54SAlex Elder 204588de7672SAlex Elder /* Allocate and initialize the available endpoint bitmap */ 204688de7672SAlex Elder ipa->available = bitmap_zalloc(limit, GFP_KERNEL); 204788de7672SAlex Elder if (!ipa->available) 204888de7672SAlex Elder return -ENOMEM; 204988de7672SAlex Elder ipa->available_count = limit; 205088de7672SAlex Elder 20512b87d721SAlex Elder /* Mark all supported RX and TX endpoints as available */ 205288de7672SAlex Elder bitmap_set(ipa->available, 0, tx_count); 205388de7672SAlex Elder bitmap_set(ipa->available, rx_base, rx_count); 205484f9bd12SAlex Elder 20559a9f5129SAlex Elder for_each_set_bit(endpoint_id, ipa->defined, ipa->endpoint_count) { 205684f9bd12SAlex Elder struct ipa_endpoint *endpoint; 205784f9bd12SAlex Elder 20582b87d721SAlex Elder if (endpoint_id >= limit) { 20592b87d721SAlex Elder dev_err(dev, "invalid endpoint id, %u > %u\n", 20602b87d721SAlex Elder endpoint_id, limit - 1); 206188de7672SAlex Elder goto err_free_bitmap; 206284f9bd12SAlex Elder } 206384f9bd12SAlex Elder 206488de7672SAlex Elder if (!test_bit(endpoint_id, ipa->available)) { 20652b87d721SAlex Elder dev_err(dev, "unavailable endpoint id %u\n", 20662b87d721SAlex Elder endpoint_id); 206788de7672SAlex Elder goto err_free_bitmap; 20682b87d721SAlex Elder } 20692b87d721SAlex Elder 20702b87d721SAlex Elder /* Make sure it's pointing in the right direction */ 20712b87d721SAlex Elder endpoint = &ipa->endpoint[endpoint_id]; 20722b87d721SAlex Elder if (endpoint->toward_ipa) { 20732b87d721SAlex Elder if (endpoint_id < tx_count) 20742b87d721SAlex Elder continue; 20752b87d721SAlex Elder } else if (endpoint_id >= rx_base) { 20762b87d721SAlex Elder continue; 20772b87d721SAlex Elder } 20782b87d721SAlex Elder 20792b87d721SAlex Elder dev_err(dev, "endpoint id %u wrong direction\n", endpoint_id); 208088de7672SAlex Elder goto err_free_bitmap; 20812b87d721SAlex Elder } 20822b87d721SAlex Elder 20832b87d721SAlex Elder return 0; 208484f9bd12SAlex Elder 208588de7672SAlex Elder err_free_bitmap: 208688de7672SAlex Elder ipa_endpoint_deconfig(ipa); 208788de7672SAlex Elder 208888de7672SAlex Elder return -EINVAL; 208984f9bd12SAlex Elder } 209084f9bd12SAlex Elder 209184f9bd12SAlex Elder static void ipa_endpoint_init_one(struct ipa *ipa, enum ipa_endpoint_name name, 209284f9bd12SAlex Elder const struct ipa_gsi_endpoint_data *data) 209384f9bd12SAlex Elder { 209484f9bd12SAlex Elder struct ipa_endpoint *endpoint; 209584f9bd12SAlex Elder 209684f9bd12SAlex Elder endpoint = &ipa->endpoint[data->endpoint_id]; 209784f9bd12SAlex Elder 209884f9bd12SAlex Elder if (data->ee_id == GSI_EE_AP) 209984f9bd12SAlex Elder ipa->channel_map[data->channel_id] = endpoint; 210084f9bd12SAlex Elder ipa->name_map[name] = endpoint; 210184f9bd12SAlex Elder 210284f9bd12SAlex Elder endpoint->ipa = ipa; 210384f9bd12SAlex Elder endpoint->ee_id = data->ee_id; 210484f9bd12SAlex Elder endpoint->channel_id = data->channel_id; 210584f9bd12SAlex Elder endpoint->endpoint_id = data->endpoint_id; 210684f9bd12SAlex Elder endpoint->toward_ipa = data->toward_ipa; 2107660e52d6SAlex Elder endpoint->config = data->endpoint.config; 210884f9bd12SAlex Elder 21099a9f5129SAlex Elder __set_bit(endpoint->endpoint_id, ipa->defined); 211084f9bd12SAlex Elder } 211184f9bd12SAlex Elder 2112602a1c76SAlex Elder static void ipa_endpoint_exit_one(struct ipa_endpoint *endpoint) 211384f9bd12SAlex Elder { 21149a9f5129SAlex Elder __clear_bit(endpoint->endpoint_id, endpoint->ipa->defined); 211584f9bd12SAlex Elder 211684f9bd12SAlex Elder memset(endpoint, 0, sizeof(*endpoint)); 211784f9bd12SAlex Elder } 211884f9bd12SAlex Elder 211984f9bd12SAlex Elder void ipa_endpoint_exit(struct ipa *ipa) 212084f9bd12SAlex Elder { 21219a9f5129SAlex Elder u32 endpoint_id; 212284f9bd12SAlex Elder 21230f97fbd4SAlex Elder ipa->filtered = 0; 21240f97fbd4SAlex Elder 21259a9f5129SAlex Elder for_each_set_bit(endpoint_id, ipa->defined, ipa->endpoint_count) 212684f9bd12SAlex Elder ipa_endpoint_exit_one(&ipa->endpoint[endpoint_id]); 21279a9f5129SAlex Elder 21289b7a0065SAlex Elder bitmap_free(ipa->enabled); 21299b7a0065SAlex Elder ipa->enabled = NULL; 2130ae5108e9SAlex Elder bitmap_free(ipa->set_up); 2131ae5108e9SAlex Elder ipa->set_up = NULL; 21329a9f5129SAlex Elder bitmap_free(ipa->defined); 21339a9f5129SAlex Elder ipa->defined = NULL; 21349a9f5129SAlex Elder 213584f9bd12SAlex Elder memset(ipa->name_map, 0, sizeof(ipa->name_map)); 213684f9bd12SAlex Elder memset(ipa->channel_map, 0, sizeof(ipa->channel_map)); 213784f9bd12SAlex Elder } 213884f9bd12SAlex Elder 213984f9bd12SAlex Elder /* Returns a bitmask of endpoints that support filtering, or 0 on error */ 21400f97fbd4SAlex Elder int ipa_endpoint_init(struct ipa *ipa, u32 count, 214184f9bd12SAlex Elder const struct ipa_gsi_endpoint_data *data) 214284f9bd12SAlex Elder { 214384f9bd12SAlex Elder enum ipa_endpoint_name name; 21440f97fbd4SAlex Elder u32 filtered; 214584f9bd12SAlex Elder 21469654d8c4SAlex Elder BUILD_BUG_ON(!IPA_REPLENISH_BATCH); 21479654d8c4SAlex Elder 2148b7aaff0bSAlex Elder /* Number of endpoints is one more than the maximum ID */ 2149b7aaff0bSAlex Elder ipa->endpoint_count = ipa_endpoint_max(ipa, count, data) + 1; 2150b7aaff0bSAlex Elder if (!ipa->endpoint_count) 21510f97fbd4SAlex Elder return -EINVAL; 215284f9bd12SAlex Elder 2153ae5108e9SAlex Elder /* Initialize endpoint state bitmaps */ 21549a9f5129SAlex Elder ipa->defined = bitmap_zalloc(ipa->endpoint_count, GFP_KERNEL); 21559a9f5129SAlex Elder if (!ipa->defined) 21560f97fbd4SAlex Elder return -ENOMEM; 215784f9bd12SAlex Elder 2158ae5108e9SAlex Elder ipa->set_up = bitmap_zalloc(ipa->endpoint_count, GFP_KERNEL); 2159ae5108e9SAlex Elder if (!ipa->set_up) 2160ae5108e9SAlex Elder goto err_free_defined; 2161ae5108e9SAlex Elder 21629b7a0065SAlex Elder ipa->enabled = bitmap_zalloc(ipa->endpoint_count, GFP_KERNEL); 21639b7a0065SAlex Elder if (!ipa->enabled) 21649b7a0065SAlex Elder goto err_free_set_up; 21659b7a0065SAlex Elder 21660f97fbd4SAlex Elder filtered = 0; 216784f9bd12SAlex Elder for (name = 0; name < count; name++, data++) { 216884f9bd12SAlex Elder if (ipa_gsi_endpoint_data_empty(data)) 216984f9bd12SAlex Elder continue; /* Skip over empty slots */ 217084f9bd12SAlex Elder 217184f9bd12SAlex Elder ipa_endpoint_init_one(ipa, name, data); 217284f9bd12SAlex Elder 217384f9bd12SAlex Elder if (data->endpoint.filter_support) 21740f97fbd4SAlex Elder filtered |= BIT(data->endpoint_id); 21752091c79aSAlex Elder if (data->ee_id == GSI_EE_MODEM && data->toward_ipa) 21762091c79aSAlex Elder ipa->modem_tx_count++; 217784f9bd12SAlex Elder } 217884f9bd12SAlex Elder 2179ae5108e9SAlex Elder /* Make sure the set of filtered endpoints is valid */ 2180ae5108e9SAlex Elder if (!ipa_filtered_valid(ipa, filtered)) { 2181ae5108e9SAlex Elder ipa_endpoint_exit(ipa); 2182ae5108e9SAlex Elder 2183ae5108e9SAlex Elder return -EINVAL; 2184ae5108e9SAlex Elder } 218584f9bd12SAlex Elder 21860f97fbd4SAlex Elder ipa->filtered = filtered; 21870f97fbd4SAlex Elder 21880f97fbd4SAlex Elder return 0; 218984f9bd12SAlex Elder 21909b7a0065SAlex Elder err_free_set_up: 21919b7a0065SAlex Elder bitmap_free(ipa->set_up); 21929b7a0065SAlex Elder ipa->set_up = NULL; 2193ae5108e9SAlex Elder err_free_defined: 2194ae5108e9SAlex Elder bitmap_free(ipa->defined); 2195ae5108e9SAlex Elder ipa->defined = NULL; 219684f9bd12SAlex Elder 2197ae5108e9SAlex Elder return -ENOMEM; 219884f9bd12SAlex Elder } 2199