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