blob: 93449366d7d851332eb1f68bbdc32451d4e21b2a [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"
24
25#define atomic_dec_not_zero(v) atomic_add_unless((v), -1, 0)
26
27#define IPA_REPLENISH_BATCH 16
28
Alex Elder6fcd4222020-03-20 11:02:20 -050029/* 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 */
Alex Elder84f9bd12020-03-05 22:28:24 -060031
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
Alex Elder8730f452020-06-11 14:48:30 -050035/* 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
Alex Elder84f9bd12020-03-05 22:28:24 -060038#define IPA_ENDPOINT_RESET_AGGR_RETRY_MAX 3
Alex Elder1d866522020-06-29 16:55:22 -050039#define IPA_AGGR_TIME_LIMIT_DEFAULT 500 /* microseconds */
Alex Elder84f9bd12020-03-05 22:28:24 -060040
Alex Elder84f9bd12020-03-05 22:28:24 -060041/** enum ipa_status_opcode - status element opcode hardware values */
42enum 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 */
53enum 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 */
66struct 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
113static 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
138static 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
219static 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
261static 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 */
270static 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 */
Alex Elder4900bf32020-05-04 18:37:11 -0500285static bool
Alex Elder84f9bd12020-03-05 22:28:24 -0600286ipa_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;
Alex Elder4900bf32020-05-04 18:37:11 -0500290 bool state;
Alex Elder84f9bd12020-03-05 22:28:24 -0600291 u32 mask;
292 u32 val;
293
Alex Elder4fa95242020-05-04 18:37:12 -0500294 /* 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 */
Alex Elder84f9bd12020-03-05 22:28:24 -0600302 mask = endpoint->toward_ipa ? ENDP_DELAY_FMASK : ENDP_SUSPEND_FMASK;
303
304 val = ioread32(ipa->reg_virt + offset);
Alex Elder4900bf32020-05-04 18:37:11 -0500305 /* 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 }
Alex Elder84f9bd12020-03-05 22:28:24 -0600311
Alex Elder4900bf32020-05-04 18:37:11 -0500312 return state;
Alex Elder84f9bd12020-03-05 22:28:24 -0600313}
314
Alex Elder4fa95242020-05-04 18:37:12 -0500315/* We currently don't care what the previous state was for delay mode */
316static void
317ipa_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) */
325static bool
326ipa_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
Alex Elder84f9bd12020-03-05 22:28:24 -0600333/* Enable or disable delay or suspend mode on all modem endpoints */
334void ipa_endpoint_modem_pause_all(struct ipa *ipa, bool enable)
335{
336 bool support_suspend;
337 u32 endpoint_id;
338
Alex Elder4fa95242020-05-04 18:37:12 -0500339 /* DELAY mode doesn't work correctly on IPA v4.2 */
Alex Elder84f9bd12020-03-05 22:28:24 -0600340 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 */
Alex Elder4fa95242020-05-04 18:37:12 -0500353 if (endpoint->toward_ipa)
354 ipa_endpoint_program_delay(endpoint, enable);
355 else if (support_suspend)
356 (void)ipa_endpoint_program_suspend(endpoint, enable);
Alex Elder84f9bd12020-03-05 22:28:24 -0600357 }
358}
359
360/* Reset all modem endpoints to use the default exception endpoint */
361int 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
Wang Wenhu8fa54b12020-05-26 20:19:24 -0700370 * for now. We need to end the transaction with a "tag process."
Alex Elder84f9bd12020-03-05 22:28:24 -0600371 */
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
409static 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
Alex Elder8730f452020-06-11 14:48:30 -0500439/**
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 */
Alex Elder84f9bd12020-03-05 22:28:24 -0600457static 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
Alex Elder8730f452020-06-11 14:48:30 -0500465 /* We might supply a checksum header after the QMAP header */
Alex Elder84f9bd12020-03-05 22:28:24 -0600466 if (endpoint->toward_ipa && endpoint->data->checksum)
467 header_size += sizeof(struct rmnet_map_ul_csum_header);
Alex Elder84f9bd12020-03-05 22:28:24 -0600468 val |= u32_encode_bits(header_size, HDR_LEN_FMASK);
Alex Elder84f9bd12020-03-05 22:28:24 -0600469
Alex Elderf330fda2020-06-11 14:48:33 -0500470 /* Define how to fill fields in a received QMAP header */
Alex Elder8730f452020-06-11 14:48:30 -0500471 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);
Alex Elder84f9bd12020-03-05 22:28:24 -0600480 val |= HDR_OFST_PKT_SIZE_VALID_FMASK;
Alex Elder8730f452020-06-11 14:48:30 -0500481 val |= u32_encode_bits(off, HDR_OFST_PKT_SIZE_FMASK);
Alex Elder84f9bd12020-03-05 22:28:24 -0600482 }
Alex Elder8730f452020-06-11 14:48:30 -0500483 /* 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) */
Alex Elder84f9bd12020-03-05 22:28:24 -0600487 /* HDR_A5_MUX is 0 */
488 /* HDR_LEN_INC_DEAGG_HDR is 0 */
Alex Elder8730f452020-06-11 14:48:30 -0500489 /* HDR_METADATA_REG_VALID is 0 (TX only) */
Alex Elder84f9bd12020-03-05 22:28:24 -0600490 }
491
492 iowrite32(val, endpoint->ipa->reg_virt + offset);
493}
494
495static 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 */
Alex Elderf330fda2020-06-11 14:48:33 -0500502
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
Alex Elder84f9bd12020-03-05 22:28:24 -0600516 /* HDR_PAYLOAD_LEN_INC_PADDING is 0 */
Alex Elder84f9bd12020-03-05 22:28:24 -0600517 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
Alex Elder84f9bd12020-03-05 22:28:24 -0600523
524static 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
Alex Elder8730f452020-06-11 14:48:30 -0500532 /* Note that HDR_ENDIANNESS indicates big endian header fields */
Alex Elder84f9bd12020-03-05 22:28:24 -0600533 if (!endpoint->toward_ipa && endpoint->data->qmap)
Alex Elder8730f452020-06-11 14:48:30 -0500534 val = cpu_to_be32(IPA_ENDPOINT_QMAP_METADATA_MASK);
Alex Elder84f9bd12020-03-05 22:28:24 -0600535
536 iowrite32(val, endpoint->ipa->reg_virt + offset);
537}
538
539static 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 */
561static 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
572static 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);
Alex Elder1d866522020-06-29 16:55:22 -0500586
Alex Elder84f9bd12020-03-05 22:28:24 -0600587 limit = IPA_AGGR_TIME_LIMIT_DEFAULT;
Alex Elder1d866522020-06-29 16:55:22 -0500588 limit = DIV_ROUND_CLOSEST(limit, IPA_AGGR_GRANULARITY);
589 val |= u32_encode_bits(limit, AGGR_TIME_LIMIT_FMASK);
590
Alex Elder84f9bd12020-03-05 22:28:24 -0600591 val |= u32_encode_bits(0, AGGR_PKT_LIMIT_FMASK);
592 if (endpoint->data->rx.aggr_close_eof)
593 val |= AGGR_SW_EOF_ACTIVE_FMASK;
594 /* AGGR_HARD_BYTE_LIMIT_ENABLE is 0 */
595 } else {
596 val |= u32_encode_bits(IPA_ENABLE_DEAGGR,
597 AGGR_EN_FMASK);
598 val |= u32_encode_bits(IPA_QCMAP, AGGR_TYPE_FMASK);
599 /* other fields ignored */
600 }
601 /* AGGR_FORCE_CLOSE is 0 */
602 } else {
603 val |= u32_encode_bits(IPA_BYPASS_AGGR, AGGR_EN_FMASK);
604 /* other fields ignored */
605 }
606
607 iowrite32(val, endpoint->ipa->reg_virt + offset);
608}
609
610/* A return value of 0 indicates an error */
611static u32 ipa_reg_init_hol_block_timer_val(struct ipa *ipa, u32 microseconds)
612{
613 u32 scale;
614 u32 base;
615 u32 val;
616
617 if (!microseconds)
618 return 0; /* invalid delay */
619
620 /* Timer is represented in units of clock ticks. */
621 if (ipa->version < IPA_VERSION_4_2)
622 return microseconds; /* XXX Needs to be computed */
623
624 /* IPA v4.2 represents the tick count as base * scale */
625 scale = 1; /* XXX Needs to be computed */
626 if (scale > field_max(SCALE_FMASK))
627 return 0; /* scale too big */
628
629 base = DIV_ROUND_CLOSEST(microseconds, scale);
630 if (base > field_max(BASE_VALUE_FMASK))
631 return 0; /* microseconds too big */
632
633 val = u32_encode_bits(scale, SCALE_FMASK);
634 val |= u32_encode_bits(base, BASE_VALUE_FMASK);
635
636 return val;
637}
638
639static int ipa_endpoint_init_hol_block_timer(struct ipa_endpoint *endpoint,
640 u32 microseconds)
641{
642 u32 endpoint_id = endpoint->endpoint_id;
643 struct ipa *ipa = endpoint->ipa;
644 u32 offset;
645 u32 val;
646
647 /* XXX We'll fix this when the register definition is clear */
648 if (microseconds) {
649 struct device *dev = &ipa->pdev->dev;
650
651 dev_err(dev, "endpoint %u non-zero HOLB period (ignoring)\n",
652 endpoint_id);
653 microseconds = 0;
654 }
655
656 if (microseconds) {
657 val = ipa_reg_init_hol_block_timer_val(ipa, microseconds);
658 if (!val)
659 return -EINVAL;
660 } else {
661 val = 0; /* timeout is immediate */
662 }
663 offset = IPA_REG_ENDP_INIT_HOL_BLOCK_TIMER_N_OFFSET(endpoint_id);
664 iowrite32(val, ipa->reg_virt + offset);
665
666 return 0;
667}
668
669static void
670ipa_endpoint_init_hol_block_enable(struct ipa_endpoint *endpoint, bool enable)
671{
672 u32 endpoint_id = endpoint->endpoint_id;
673 u32 offset;
674 u32 val;
675
676 val = u32_encode_bits(enable ? 1 : 0, HOL_BLOCK_EN_FMASK);
677 offset = IPA_REG_ENDP_INIT_HOL_BLOCK_EN_N_OFFSET(endpoint_id);
678 iowrite32(val, endpoint->ipa->reg_virt + offset);
679}
680
681void ipa_endpoint_modem_hol_block_clear_all(struct ipa *ipa)
682{
683 u32 i;
684
685 for (i = 0; i < IPA_ENDPOINT_MAX; i++) {
686 struct ipa_endpoint *endpoint = &ipa->endpoint[i];
687
688 if (endpoint->ee_id != GSI_EE_MODEM)
689 continue;
690
691 (void)ipa_endpoint_init_hol_block_timer(endpoint, 0);
692 ipa_endpoint_init_hol_block_enable(endpoint, true);
693 }
694}
695
696static void ipa_endpoint_init_deaggr(struct ipa_endpoint *endpoint)
697{
698 u32 offset = IPA_REG_ENDP_INIT_DEAGGR_N_OFFSET(endpoint->endpoint_id);
699 u32 val = 0;
700
701 /* DEAGGR_HDR_LEN is 0 */
702 /* PACKET_OFFSET_VALID is 0 */
703 /* PACKET_OFFSET_LOCATION is ignored (not valid) */
704 /* MAX_PACKET_LEN is 0 (not enforced) */
705
706 iowrite32(val, endpoint->ipa->reg_virt + offset);
707}
708
709static void ipa_endpoint_init_seq(struct ipa_endpoint *endpoint)
710{
711 u32 offset = IPA_REG_ENDP_INIT_SEQ_N_OFFSET(endpoint->endpoint_id);
712 u32 seq_type = endpoint->seq_type;
713 u32 val = 0;
714
Alex Elder636edea2020-06-11 14:48:32 -0500715 /* Sequencer type is made up of four nibbles */
Alex Elder84f9bd12020-03-05 22:28:24 -0600716 val |= u32_encode_bits(seq_type & 0xf, HPS_SEQ_TYPE_FMASK);
717 val |= u32_encode_bits((seq_type >> 4) & 0xf, DPS_SEQ_TYPE_FMASK);
Alex Elder636edea2020-06-11 14:48:32 -0500718 /* The second two apply to replicated packets */
719 val |= u32_encode_bits((seq_type >> 8) & 0xf, HPS_REP_SEQ_TYPE_FMASK);
720 val |= u32_encode_bits((seq_type >> 12) & 0xf, DPS_REP_SEQ_TYPE_FMASK);
Alex Elder84f9bd12020-03-05 22:28:24 -0600721
722 iowrite32(val, endpoint->ipa->reg_virt + offset);
723}
724
725/**
726 * ipa_endpoint_skb_tx() - Transmit a socket buffer
727 * @endpoint: Endpoint pointer
728 * @skb: Socket buffer to send
729 *
730 * Returns: 0 if successful, or a negative error code
731 */
732int ipa_endpoint_skb_tx(struct ipa_endpoint *endpoint, struct sk_buff *skb)
733{
734 struct gsi_trans *trans;
735 u32 nr_frags;
736 int ret;
737
738 /* Make sure source endpoint's TLV FIFO has enough entries to
739 * hold the linear portion of the skb and all its fragments.
740 * If not, see if we can linearize it before giving up.
741 */
742 nr_frags = skb_shinfo(skb)->nr_frags;
743 if (1 + nr_frags > endpoint->trans_tre_max) {
744 if (skb_linearize(skb))
745 return -E2BIG;
746 nr_frags = 0;
747 }
748
749 trans = ipa_endpoint_trans_alloc(endpoint, 1 + nr_frags);
750 if (!trans)
751 return -EBUSY;
752
753 ret = gsi_trans_skb_add(trans, skb);
754 if (ret)
755 goto err_trans_free;
756 trans->data = skb; /* transaction owns skb now */
757
758 gsi_trans_commit(trans, !netdev_xmit_more());
759
760 return 0;
761
762err_trans_free:
763 gsi_trans_free(trans);
764
765 return -ENOMEM;
766}
767
768static void ipa_endpoint_status(struct ipa_endpoint *endpoint)
769{
770 u32 endpoint_id = endpoint->endpoint_id;
771 struct ipa *ipa = endpoint->ipa;
772 u32 val = 0;
773 u32 offset;
774
775 offset = IPA_REG_ENDP_STATUS_N_OFFSET(endpoint_id);
776
777 if (endpoint->data->status_enable) {
778 val |= STATUS_EN_FMASK;
779 if (endpoint->toward_ipa) {
780 enum ipa_endpoint_name name;
781 u32 status_endpoint_id;
782
783 name = endpoint->data->tx.status_endpoint;
784 status_endpoint_id = ipa->name_map[name]->endpoint_id;
785
786 val |= u32_encode_bits(status_endpoint_id,
787 STATUS_ENDP_FMASK);
788 }
789 /* STATUS_LOCATION is 0 (status element precedes packet) */
790 /* The next field is present for IPA v4.0 and above */
791 /* STATUS_PKT_SUPPRESS_FMASK is 0 */
792 }
793
794 iowrite32(val, ipa->reg_virt + offset);
795}
796
797static int ipa_endpoint_replenish_one(struct ipa_endpoint *endpoint)
798{
799 struct gsi_trans *trans;
800 bool doorbell = false;
801 struct page *page;
802 u32 offset;
803 u32 len;
804 int ret;
805
Alex Elder6fcd4222020-03-20 11:02:20 -0500806 page = dev_alloc_pages(get_order(IPA_RX_BUFFER_SIZE));
Alex Elder84f9bd12020-03-05 22:28:24 -0600807 if (!page)
808 return -ENOMEM;
809
810 trans = ipa_endpoint_trans_alloc(endpoint, 1);
811 if (!trans)
812 goto err_free_pages;
813
814 /* Offset the buffer to make space for skb headroom */
815 offset = NET_SKB_PAD;
816 len = IPA_RX_BUFFER_SIZE - offset;
817
818 ret = gsi_trans_page_add(trans, page, len, offset);
819 if (ret)
820 goto err_trans_free;
821 trans->data = page; /* transaction owns page now */
822
823 if (++endpoint->replenish_ready == IPA_REPLENISH_BATCH) {
824 doorbell = true;
825 endpoint->replenish_ready = 0;
826 }
827
828 gsi_trans_commit(trans, doorbell);
829
830 return 0;
831
832err_trans_free:
833 gsi_trans_free(trans);
834err_free_pages:
Alex Elder6fcd4222020-03-20 11:02:20 -0500835 __free_pages(page, get_order(IPA_RX_BUFFER_SIZE));
Alex Elder84f9bd12020-03-05 22:28:24 -0600836
837 return -ENOMEM;
838}
839
840/**
841 * ipa_endpoint_replenish() - Replenish the Rx packets cache.
842 *
843 * Allocate RX packet wrapper structures with maximal socket buffers
844 * for an endpoint. These are supplied to the hardware, which fills
845 * them with incoming data.
846 */
847static void ipa_endpoint_replenish(struct ipa_endpoint *endpoint, u32 count)
848{
849 struct gsi *gsi;
850 u32 backlog;
851
852 if (!endpoint->replenish_enabled) {
853 if (count)
854 atomic_add(count, &endpoint->replenish_saved);
855 return;
856 }
857
858
859 while (atomic_dec_not_zero(&endpoint->replenish_backlog))
860 if (ipa_endpoint_replenish_one(endpoint))
861 goto try_again_later;
862 if (count)
863 atomic_add(count, &endpoint->replenish_backlog);
864
865 return;
866
867try_again_later:
868 /* The last one didn't succeed, so fix the backlog */
869 backlog = atomic_inc_return(&endpoint->replenish_backlog);
870
871 if (count)
872 atomic_add(count, &endpoint->replenish_backlog);
873
874 /* Whenever a receive buffer transaction completes we'll try to
875 * replenish again. It's unlikely, but if we fail to supply even
876 * one buffer, nothing will trigger another replenish attempt.
877 * Receive buffer transactions use one TRE, so schedule work to
878 * try replenishing again if our backlog is *all* available TREs.
879 */
880 gsi = &endpoint->ipa->gsi;
881 if (backlog == gsi_channel_tre_max(gsi, endpoint->channel_id))
882 schedule_delayed_work(&endpoint->replenish_work,
883 msecs_to_jiffies(1));
884}
885
886static void ipa_endpoint_replenish_enable(struct ipa_endpoint *endpoint)
887{
888 struct gsi *gsi = &endpoint->ipa->gsi;
889 u32 max_backlog;
890 u32 saved;
891
892 endpoint->replenish_enabled = true;
893 while ((saved = atomic_xchg(&endpoint->replenish_saved, 0)))
894 atomic_add(saved, &endpoint->replenish_backlog);
895
896 /* Start replenishing if hardware currently has no buffers */
897 max_backlog = gsi_channel_tre_max(gsi, endpoint->channel_id);
898 if (atomic_read(&endpoint->replenish_backlog) == max_backlog)
899 ipa_endpoint_replenish(endpoint, 0);
900}
901
902static void ipa_endpoint_replenish_disable(struct ipa_endpoint *endpoint)
903{
904 u32 backlog;
905
906 endpoint->replenish_enabled = false;
907 while ((backlog = atomic_xchg(&endpoint->replenish_backlog, 0)))
908 atomic_add(backlog, &endpoint->replenish_saved);
909}
910
911static void ipa_endpoint_replenish_work(struct work_struct *work)
912{
913 struct delayed_work *dwork = to_delayed_work(work);
914 struct ipa_endpoint *endpoint;
915
916 endpoint = container_of(dwork, struct ipa_endpoint, replenish_work);
917
918 ipa_endpoint_replenish(endpoint, 0);
919}
920
921static void ipa_endpoint_skb_copy(struct ipa_endpoint *endpoint,
922 void *data, u32 len, u32 extra)
923{
924 struct sk_buff *skb;
925
926 skb = __dev_alloc_skb(len, GFP_ATOMIC);
927 if (skb) {
928 skb_put(skb, len);
929 memcpy(skb->data, data, len);
930 skb->truesize += extra;
931 }
932
933 /* Now receive it, or drop it if there's no netdev */
934 if (endpoint->netdev)
935 ipa_modem_skb_rx(endpoint->netdev, skb);
936 else if (skb)
937 dev_kfree_skb_any(skb);
938}
939
940static bool ipa_endpoint_skb_build(struct ipa_endpoint *endpoint,
941 struct page *page, u32 len)
942{
943 struct sk_buff *skb;
944
945 /* Nothing to do if there's no netdev */
946 if (!endpoint->netdev)
947 return false;
948
949 /* assert(len <= SKB_WITH_OVERHEAD(IPA_RX_BUFFER_SIZE-NET_SKB_PAD)); */
950 skb = build_skb(page_address(page), IPA_RX_BUFFER_SIZE);
951 if (skb) {
952 /* Reserve the headroom and account for the data */
953 skb_reserve(skb, NET_SKB_PAD);
954 skb_put(skb, len);
955 }
956
957 /* Receive the buffer (or record drop if unable to build it) */
958 ipa_modem_skb_rx(endpoint->netdev, skb);
959
960 return skb != NULL;
961}
962
963/* The format of a packet status element is the same for several status
964 * types (opcodes). The NEW_FRAG_RULE, LOG, DCMP (decompression) types
965 * aren't currently supported
966 */
967static bool ipa_status_format_packet(enum ipa_status_opcode opcode)
968{
969 switch (opcode) {
970 case IPA_STATUS_OPCODE_PACKET:
971 case IPA_STATUS_OPCODE_DROPPED_PACKET:
972 case IPA_STATUS_OPCODE_SUSPENDED_PACKET:
973 case IPA_STATUS_OPCODE_PACKET_2ND_PASS:
974 return true;
975 default:
976 return false;
977 }
978}
979
980static bool ipa_endpoint_status_skip(struct ipa_endpoint *endpoint,
981 const struct ipa_status *status)
982{
983 u32 endpoint_id;
984
985 if (!ipa_status_format_packet(status->opcode))
986 return true;
987 if (!status->pkt_len)
988 return true;
989 endpoint_id = u32_get_bits(status->endp_dst_idx,
990 IPA_STATUS_DST_IDX_FMASK);
991 if (endpoint_id != endpoint->endpoint_id)
992 return true;
993
994 return false; /* Don't skip this packet, process it */
995}
996
997/* Return whether the status indicates the packet should be dropped */
998static bool ipa_status_drop_packet(const struct ipa_status *status)
999{
1000 u32 val;
1001
1002 /* Deaggregation exceptions we drop; others we consume */
1003 if (status->exception)
1004 return status->exception == IPA_STATUS_EXCEPTION_DEAGGR;
1005
1006 /* Drop the packet if it fails to match a routing rule; otherwise no */
1007 val = le32_get_bits(status->flags1, IPA_STATUS_FLAGS1_RT_RULE_ID_FMASK);
1008
1009 return val == field_max(IPA_STATUS_FLAGS1_RT_RULE_ID_FMASK);
1010}
1011
1012static void ipa_endpoint_status_parse(struct ipa_endpoint *endpoint,
1013 struct page *page, u32 total_len)
1014{
1015 void *data = page_address(page) + NET_SKB_PAD;
1016 u32 unused = IPA_RX_BUFFER_SIZE - total_len;
1017 u32 resid = total_len;
1018
1019 while (resid) {
1020 const struct ipa_status *status = data;
1021 u32 align;
1022 u32 len;
1023
1024 if (resid < sizeof(*status)) {
1025 dev_err(&endpoint->ipa->pdev->dev,
1026 "short message (%u bytes < %zu byte status)\n",
1027 resid, sizeof(*status));
1028 break;
1029 }
1030
1031 /* Skip over status packets that lack packet data */
1032 if (ipa_endpoint_status_skip(endpoint, status)) {
1033 data += sizeof(*status);
1034 resid -= sizeof(*status);
1035 continue;
1036 }
1037
1038 /* Compute the amount of buffer space consumed by the
1039 * packet, including the status element. If the hardware
1040 * is configured to pad packet data to an aligned boundary,
1041 * account for that. And if checksum offload is is enabled
1042 * a trailer containing computed checksum information will
1043 * be appended.
1044 */
1045 align = endpoint->data->rx.pad_align ? : 1;
1046 len = le16_to_cpu(status->pkt_len);
1047 len = sizeof(*status) + ALIGN(len, align);
1048 if (endpoint->data->checksum)
1049 len += sizeof(struct rmnet_map_dl_csum_trailer);
1050
1051 /* Charge the new packet with a proportional fraction of
1052 * the unused space in the original receive buffer.
1053 * XXX Charge a proportion of the *whole* receive buffer?
1054 */
1055 if (!ipa_status_drop_packet(status)) {
1056 u32 extra = unused * len / total_len;
1057 void *data2 = data + sizeof(*status);
1058 u32 len2 = le16_to_cpu(status->pkt_len);
1059
1060 /* Client receives only packet data (no status) */
1061 ipa_endpoint_skb_copy(endpoint, data2, len2, extra);
1062 }
1063
1064 /* Consume status and the full packet it describes */
1065 data += len;
1066 resid -= len;
1067 }
1068}
1069
1070/* Complete a TX transaction, command or from ipa_endpoint_skb_tx() */
1071static void ipa_endpoint_tx_complete(struct ipa_endpoint *endpoint,
1072 struct gsi_trans *trans)
1073{
1074}
1075
1076/* Complete transaction initiated in ipa_endpoint_replenish_one() */
1077static void ipa_endpoint_rx_complete(struct ipa_endpoint *endpoint,
1078 struct gsi_trans *trans)
1079{
1080 struct page *page;
1081
1082 ipa_endpoint_replenish(endpoint, 1);
1083
1084 if (trans->cancelled)
1085 return;
1086
1087 /* Parse or build a socket buffer using the actual received length */
1088 page = trans->data;
1089 if (endpoint->data->status_enable)
1090 ipa_endpoint_status_parse(endpoint, page, trans->len);
1091 else if (ipa_endpoint_skb_build(endpoint, page, trans->len))
1092 trans->data = NULL; /* Pages have been consumed */
1093}
1094
1095void ipa_endpoint_trans_complete(struct ipa_endpoint *endpoint,
1096 struct gsi_trans *trans)
1097{
1098 if (endpoint->toward_ipa)
1099 ipa_endpoint_tx_complete(endpoint, trans);
1100 else
1101 ipa_endpoint_rx_complete(endpoint, trans);
1102}
1103
1104void ipa_endpoint_trans_release(struct ipa_endpoint *endpoint,
1105 struct gsi_trans *trans)
1106{
1107 if (endpoint->toward_ipa) {
1108 struct ipa *ipa = endpoint->ipa;
1109
1110 /* Nothing to do for command transactions */
1111 if (endpoint != ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]) {
1112 struct sk_buff *skb = trans->data;
1113
1114 if (skb)
1115 dev_kfree_skb_any(skb);
1116 }
1117 } else {
1118 struct page *page = trans->data;
1119
1120 if (page)
Alex Elder6fcd4222020-03-20 11:02:20 -05001121 __free_pages(page, get_order(IPA_RX_BUFFER_SIZE));
Alex Elder84f9bd12020-03-05 22:28:24 -06001122 }
1123}
1124
1125void ipa_endpoint_default_route_set(struct ipa *ipa, u32 endpoint_id)
1126{
1127 u32 val;
1128
1129 /* ROUTE_DIS is 0 */
1130 val = u32_encode_bits(endpoint_id, ROUTE_DEF_PIPE_FMASK);
1131 val |= ROUTE_DEF_HDR_TABLE_FMASK;
1132 val |= u32_encode_bits(0, ROUTE_DEF_HDR_OFST_FMASK);
1133 val |= u32_encode_bits(endpoint_id, ROUTE_FRAG_DEF_PIPE_FMASK);
1134 val |= ROUTE_DEF_RETAIN_HDR_FMASK;
1135
1136 iowrite32(val, ipa->reg_virt + IPA_REG_ROUTE_OFFSET);
1137}
1138
1139void ipa_endpoint_default_route_clear(struct ipa *ipa)
1140{
1141 ipa_endpoint_default_route_set(ipa, 0);
1142}
1143
1144static bool ipa_endpoint_aggr_active(struct ipa_endpoint *endpoint)
1145{
1146 u32 mask = BIT(endpoint->endpoint_id);
1147 struct ipa *ipa = endpoint->ipa;
1148 u32 offset;
1149 u32 val;
1150
1151 /* assert(mask & ipa->available); */
1152 offset = ipa_reg_state_aggr_active_offset(ipa->version);
1153 val = ioread32(ipa->reg_virt + offset);
1154
1155 return !!(val & mask);
1156}
1157
1158static void ipa_endpoint_force_close(struct ipa_endpoint *endpoint)
1159{
1160 u32 mask = BIT(endpoint->endpoint_id);
1161 struct ipa *ipa = endpoint->ipa;
1162
1163 /* assert(mask & ipa->available); */
1164 iowrite32(mask, ipa->reg_virt + IPA_REG_AGGR_FORCE_CLOSE_OFFSET);
1165}
1166
1167/**
1168 * ipa_endpoint_reset_rx_aggr() - Reset RX endpoint with aggregation active
1169 * @endpoint: Endpoint to be reset
1170 *
1171 * If aggregation is active on an RX endpoint when a reset is performed
1172 * on its underlying GSI channel, a special sequence of actions must be
1173 * taken to ensure the IPA pipeline is properly cleared.
1174 *
1175 * @Return: 0 if successful, or a negative error code
1176 */
1177static int ipa_endpoint_reset_rx_aggr(struct ipa_endpoint *endpoint)
1178{
1179 struct device *dev = &endpoint->ipa->pdev->dev;
1180 struct ipa *ipa = endpoint->ipa;
Alex Elder84f9bd12020-03-05 22:28:24 -06001181 struct gsi *gsi = &ipa->gsi;
Alex Elder4fa95242020-05-04 18:37:12 -05001182 bool suspended = false;
Alex Elder84f9bd12020-03-05 22:28:24 -06001183 dma_addr_t addr;
Alex Elderf86a1902020-05-04 18:30:02 -05001184 bool legacy;
Alex Elder84f9bd12020-03-05 22:28:24 -06001185 u32 retries;
1186 u32 len = 1;
1187 void *virt;
1188 int ret;
1189
1190 virt = kzalloc(len, GFP_KERNEL);
1191 if (!virt)
1192 return -ENOMEM;
1193
1194 addr = dma_map_single(dev, virt, len, DMA_FROM_DEVICE);
1195 if (dma_mapping_error(dev, addr)) {
1196 ret = -ENOMEM;
1197 goto out_kfree;
1198 }
1199
1200 /* Force close aggregation before issuing the reset */
1201 ipa_endpoint_force_close(endpoint);
1202
1203 /* Reset and reconfigure the channel with the doorbell engine
1204 * disabled. Then poll until we know aggregation is no longer
1205 * active. We'll re-enable the doorbell (if appropriate) when
1206 * we reset again below.
1207 */
1208 gsi_channel_reset(gsi, endpoint->channel_id, false);
1209
1210 /* Make sure the channel isn't suspended */
1211 if (endpoint->ipa->version == IPA_VERSION_3_5_1)
Alex Elder4fa95242020-05-04 18:37:12 -05001212 suspended = ipa_endpoint_program_suspend(endpoint, false);
Alex Elder84f9bd12020-03-05 22:28:24 -06001213
1214 /* Start channel and do a 1 byte read */
1215 ret = gsi_channel_start(gsi, endpoint->channel_id);
1216 if (ret)
1217 goto out_suspend_again;
1218
1219 ret = gsi_trans_read_byte(gsi, endpoint->channel_id, addr);
1220 if (ret)
1221 goto err_endpoint_stop;
1222
1223 /* Wait for aggregation to be closed on the channel */
1224 retries = IPA_ENDPOINT_RESET_AGGR_RETRY_MAX;
1225 do {
1226 if (!ipa_endpoint_aggr_active(endpoint))
1227 break;
1228 msleep(1);
1229 } while (retries--);
1230
1231 /* Check one last time */
1232 if (ipa_endpoint_aggr_active(endpoint))
1233 dev_err(dev, "endpoint %u still active during reset\n",
1234 endpoint->endpoint_id);
1235
1236 gsi_trans_read_byte_done(gsi, endpoint->channel_id);
1237
Alex Elderf30dcb72020-05-04 18:53:44 -05001238 ret = gsi_channel_stop(gsi, endpoint->channel_id);
Alex Elder84f9bd12020-03-05 22:28:24 -06001239 if (ret)
1240 goto out_suspend_again;
1241
1242 /* Finally, reset and reconfigure the channel again (re-enabling the
1243 * the doorbell engine if appropriate). Sleep for 1 millisecond to
1244 * complete the channel reset sequence. Finish by suspending the
1245 * channel again (if necessary).
1246 */
Alex Elderf86a1902020-05-04 18:30:02 -05001247 legacy = ipa->version == IPA_VERSION_3_5_1;
1248 gsi_channel_reset(gsi, endpoint->channel_id, legacy);
Alex Elder84f9bd12020-03-05 22:28:24 -06001249
1250 msleep(1);
1251
1252 goto out_suspend_again;
1253
1254err_endpoint_stop:
Alex Elderf30dcb72020-05-04 18:53:44 -05001255 (void)gsi_channel_stop(gsi, endpoint->channel_id);
Alex Elder84f9bd12020-03-05 22:28:24 -06001256out_suspend_again:
Alex Elder4fa95242020-05-04 18:37:12 -05001257 if (suspended)
1258 (void)ipa_endpoint_program_suspend(endpoint, true);
Alex Elder84f9bd12020-03-05 22:28:24 -06001259 dma_unmap_single(dev, addr, len, DMA_FROM_DEVICE);
1260out_kfree:
1261 kfree(virt);
1262
1263 return ret;
1264}
1265
1266static void ipa_endpoint_reset(struct ipa_endpoint *endpoint)
1267{
1268 u32 channel_id = endpoint->channel_id;
1269 struct ipa *ipa = endpoint->ipa;
Alex Elder84f9bd12020-03-05 22:28:24 -06001270 bool special;
Alex Elderf86a1902020-05-04 18:30:02 -05001271 bool legacy;
Alex Elder84f9bd12020-03-05 22:28:24 -06001272 int ret = 0;
1273
1274 /* On IPA v3.5.1, if an RX endpoint is reset while aggregation
1275 * is active, we need to handle things specially to recover.
1276 * All other cases just need to reset the underlying GSI channel.
1277 *
1278 * IPA v3.5.1 enables the doorbell engine. Newer versions do not.
1279 */
Alex Elderf86a1902020-05-04 18:30:02 -05001280 legacy = ipa->version == IPA_VERSION_3_5_1;
Alex Elder84f9bd12020-03-05 22:28:24 -06001281 special = !endpoint->toward_ipa && endpoint->data->aggregation;
1282 if (special && ipa_endpoint_aggr_active(endpoint))
1283 ret = ipa_endpoint_reset_rx_aggr(endpoint);
1284 else
Alex Elderf86a1902020-05-04 18:30:02 -05001285 gsi_channel_reset(&ipa->gsi, channel_id, legacy);
Alex Elder84f9bd12020-03-05 22:28:24 -06001286
1287 if (ret)
1288 dev_err(&ipa->pdev->dev,
1289 "error %d resetting channel %u for endpoint %u\n",
1290 ret, endpoint->channel_id, endpoint->endpoint_id);
1291}
1292
Alex Elder84f9bd12020-03-05 22:28:24 -06001293static void ipa_endpoint_program(struct ipa_endpoint *endpoint)
1294{
Alex Elder84f9bd12020-03-05 22:28:24 -06001295 if (endpoint->toward_ipa) {
Alex Elder4fa95242020-05-04 18:37:12 -05001296 if (endpoint->ipa->version != IPA_VERSION_4_2)
Alex Eldera4dcad32020-05-04 18:37:13 -05001297 ipa_endpoint_program_delay(endpoint, false);
Alex Elder84f9bd12020-03-05 22:28:24 -06001298 ipa_endpoint_init_hdr_ext(endpoint);
1299 ipa_endpoint_init_aggr(endpoint);
1300 ipa_endpoint_init_deaggr(endpoint);
1301 ipa_endpoint_init_seq(endpoint);
1302 } else {
Alex Elder4fa95242020-05-04 18:37:12 -05001303 if (endpoint->ipa->version == IPA_VERSION_3_5_1)
1304 (void)ipa_endpoint_program_suspend(endpoint, false);
Alex Elder84f9bd12020-03-05 22:28:24 -06001305 ipa_endpoint_init_hdr_ext(endpoint);
1306 ipa_endpoint_init_aggr(endpoint);
1307 }
1308 ipa_endpoint_init_cfg(endpoint);
1309 ipa_endpoint_init_hdr(endpoint);
1310 ipa_endpoint_init_hdr_metadata_mask(endpoint);
1311 ipa_endpoint_init_mode(endpoint);
1312 ipa_endpoint_status(endpoint);
1313}
1314
1315int ipa_endpoint_enable_one(struct ipa_endpoint *endpoint)
1316{
1317 struct ipa *ipa = endpoint->ipa;
1318 struct gsi *gsi = &ipa->gsi;
1319 int ret;
1320
1321 ret = gsi_channel_start(gsi, endpoint->channel_id);
1322 if (ret) {
1323 dev_err(&ipa->pdev->dev,
1324 "error %d starting %cX channel %u for endpoint %u\n",
1325 ret, endpoint->toward_ipa ? 'T' : 'R',
1326 endpoint->channel_id, endpoint->endpoint_id);
1327 return ret;
1328 }
1329
1330 if (!endpoint->toward_ipa) {
1331 ipa_interrupt_suspend_enable(ipa->interrupt,
1332 endpoint->endpoint_id);
1333 ipa_endpoint_replenish_enable(endpoint);
1334 }
1335
1336 ipa->enabled |= BIT(endpoint->endpoint_id);
1337
1338 return 0;
1339}
1340
1341void ipa_endpoint_disable_one(struct ipa_endpoint *endpoint)
1342{
1343 u32 mask = BIT(endpoint->endpoint_id);
1344 struct ipa *ipa = endpoint->ipa;
Alex Elderf30dcb72020-05-04 18:53:44 -05001345 struct gsi *gsi = &ipa->gsi;
Alex Elder84f9bd12020-03-05 22:28:24 -06001346 int ret;
1347
Alex Elderf30dcb72020-05-04 18:53:44 -05001348 if (!(ipa->enabled & mask))
Alex Elder84f9bd12020-03-05 22:28:24 -06001349 return;
1350
Alex Elderf30dcb72020-05-04 18:53:44 -05001351 ipa->enabled ^= mask;
Alex Elder84f9bd12020-03-05 22:28:24 -06001352
1353 if (!endpoint->toward_ipa) {
1354 ipa_endpoint_replenish_disable(endpoint);
1355 ipa_interrupt_suspend_disable(ipa->interrupt,
1356 endpoint->endpoint_id);
1357 }
1358
1359 /* Note that if stop fails, the channel's state is not well-defined */
Alex Elderf30dcb72020-05-04 18:53:44 -05001360 ret = gsi_channel_stop(gsi, endpoint->channel_id);
Alex Elder84f9bd12020-03-05 22:28:24 -06001361 if (ret)
1362 dev_err(&ipa->pdev->dev,
1363 "error %d attempting to stop endpoint %u\n", ret,
1364 endpoint->endpoint_id);
1365}
1366
1367/**
1368 * ipa_endpoint_suspend_aggr() - Emulate suspend interrupt
1369 * @endpoint_id: Endpoint on which to emulate a suspend
1370 *
1371 * Emulate suspend IPA interrupt to unsuspend an endpoint suspended
1372 * with an open aggregation frame. This is to work around a hardware
1373 * issue in IPA version 3.5.1 where the suspend interrupt will not be
1374 * generated when it should be.
1375 */
1376static void ipa_endpoint_suspend_aggr(struct ipa_endpoint *endpoint)
1377{
1378 struct ipa *ipa = endpoint->ipa;
1379
1380 /* assert(ipa->version == IPA_VERSION_3_5_1); */
1381
1382 if (!endpoint->data->aggregation)
1383 return;
1384
1385 /* Nothing to do if the endpoint doesn't have aggregation open */
1386 if (!ipa_endpoint_aggr_active(endpoint))
1387 return;
1388
1389 /* Force close aggregation */
1390 ipa_endpoint_force_close(endpoint);
1391
1392 ipa_interrupt_simulate_suspend(ipa->interrupt);
1393}
1394
1395void ipa_endpoint_suspend_one(struct ipa_endpoint *endpoint)
1396{
1397 struct device *dev = &endpoint->ipa->pdev->dev;
1398 struct gsi *gsi = &endpoint->ipa->gsi;
1399 bool stop_channel;
1400 int ret;
1401
1402 if (!(endpoint->ipa->enabled & BIT(endpoint->endpoint_id)))
1403 return;
1404
1405 if (!endpoint->toward_ipa)
1406 ipa_endpoint_replenish_disable(endpoint);
1407
1408 /* IPA v3.5.1 doesn't use channel stop for suspend */
1409 stop_channel = endpoint->ipa->version != IPA_VERSION_3_5_1;
1410 if (!endpoint->toward_ipa && !stop_channel) {
1411 /* Due to a hardware bug, a client suspended with an open
1412 * aggregation frame will not generate a SUSPEND IPA
1413 * interrupt. We work around this by force-closing the
1414 * aggregation frame, then simulating the arrival of such
1415 * an interrupt.
1416 */
Alex Elder4fa95242020-05-04 18:37:12 -05001417 (void)ipa_endpoint_program_suspend(endpoint, true);
Alex Elder84f9bd12020-03-05 22:28:24 -06001418 ipa_endpoint_suspend_aggr(endpoint);
1419 }
1420
1421 ret = gsi_channel_suspend(gsi, endpoint->channel_id, stop_channel);
1422 if (ret)
1423 dev_err(dev, "error %d suspending channel %u\n", ret,
1424 endpoint->channel_id);
1425}
1426
1427void ipa_endpoint_resume_one(struct ipa_endpoint *endpoint)
1428{
1429 struct device *dev = &endpoint->ipa->pdev->dev;
1430 struct gsi *gsi = &endpoint->ipa->gsi;
1431 bool start_channel;
1432 int ret;
1433
1434 if (!(endpoint->ipa->enabled & BIT(endpoint->endpoint_id)))
1435 return;
1436
1437 /* IPA v3.5.1 doesn't use channel start for resume */
1438 start_channel = endpoint->ipa->version != IPA_VERSION_3_5_1;
1439 if (!endpoint->toward_ipa && !start_channel)
Alex Elder4fa95242020-05-04 18:37:12 -05001440 (void)ipa_endpoint_program_suspend(endpoint, false);
Alex Elder84f9bd12020-03-05 22:28:24 -06001441
1442 ret = gsi_channel_resume(gsi, endpoint->channel_id, start_channel);
1443 if (ret)
1444 dev_err(dev, "error %d resuming channel %u\n", ret,
1445 endpoint->channel_id);
1446 else if (!endpoint->toward_ipa)
1447 ipa_endpoint_replenish_enable(endpoint);
1448}
1449
1450void ipa_endpoint_suspend(struct ipa *ipa)
1451{
1452 if (ipa->modem_netdev)
1453 ipa_modem_suspend(ipa->modem_netdev);
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
1459void 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
1468static 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
1495static 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
1505void 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
1519void 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
1533int 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
1591void ipa_endpoint_deconfig(struct ipa *ipa)
1592{
1593 ipa->available = 0; /* Nothing more to do */
1594}
1595
1596static 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
1618void 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
1625void 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 */
1641u32 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
1668err_endpoint_exit:
1669 ipa_endpoint_exit(ipa);
1670
1671 return 0; /* Error */
1672}