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