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