New upstream version 18.11-rc1
[deb_dpdk.git] / drivers / bus / dpaa / include / fsl_qman.h
1 /* SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0)
2  *
3  * Copyright 2008-2012 Freescale Semiconductor, Inc.
4  *
5  */
6
7 #ifndef __FSL_QMAN_H
8 #define __FSL_QMAN_H
9
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13
14 #include <dpaa_rbtree.h>
15 #include <rte_eventdev.h>
16
17 /* FQ lookups (turn this on for 64bit user-space) */
18 #if (__WORDSIZE == 64)
19 #define CONFIG_FSL_QMAN_FQ_LOOKUP
20 /* if FQ lookups are supported, this controls the number of initialised,
21  * s/w-consumed FQs that can be supported at any one time.
22  */
23 #define CONFIG_FSL_QMAN_FQ_LOOKUP_MAX (32 * 1024)
24 #endif
25
26 /* Last updated for v00.800 of the BG */
27
28 /* Hardware constants */
29 #define QM_CHANNEL_SWPORTAL0 0
30 #define QMAN_CHANNEL_POOL1 0x21
31 #define QMAN_CHANNEL_CAAM 0x80
32 #define QMAN_CHANNEL_PME 0xa0
33 #define QMAN_CHANNEL_POOL1_REV3 0x401
34 #define QMAN_CHANNEL_CAAM_REV3 0x840
35 #define QMAN_CHANNEL_PME_REV3 0x860
36 extern u16 qm_channel_pool1;
37 extern u16 qm_channel_caam;
38 extern u16 qm_channel_pme;
39 enum qm_dc_portal {
40         qm_dc_portal_fman0 = 0,
41         qm_dc_portal_fman1 = 1,
42         qm_dc_portal_caam = 2,
43         qm_dc_portal_pme = 3
44 };
45
46 /* Portal processing (interrupt) sources */
47 #define QM_PIRQ_CCSCI   0x00200000      /* CEETM Congestion State Change */
48 #define QM_PIRQ_CSCI    0x00100000      /* Congestion State Change */
49 #define QM_PIRQ_EQCI    0x00080000      /* Enqueue Command Committed */
50 #define QM_PIRQ_EQRI    0x00040000      /* EQCR Ring (below threshold) */
51 #define QM_PIRQ_DQRI    0x00020000      /* DQRR Ring (non-empty) */
52 #define QM_PIRQ_MRI     0x00010000      /* MR Ring (non-empty) */
53 /*
54  * This mask contains all the interrupt sources that need handling except DQRI,
55  * ie. that if present should trigger slow-path processing.
56  */
57 #define QM_PIRQ_SLOW    (QM_PIRQ_CSCI | QM_PIRQ_EQCI | QM_PIRQ_EQRI | \
58                         QM_PIRQ_MRI | QM_PIRQ_CCSCI)
59
60 /* For qman_static_dequeue_*** APIs */
61 #define QM_SDQCR_CHANNELS_POOL_MASK     0x00007fff
62 /* for n in [1,15] */
63 #define QM_SDQCR_CHANNELS_POOL(n)       (0x00008000 >> (n))
64 /* for conversion from n of qm_channel */
65 static inline u32 QM_SDQCR_CHANNELS_POOL_CONV(u16 channel)
66 {
67         return QM_SDQCR_CHANNELS_POOL(channel + 1 - qm_channel_pool1);
68 }
69
70 /* For qman_volatile_dequeue(); Choose one PRECEDENCE. EXACT is optional. Use
71  * NUMFRAMES(n) (6-bit) or NUMFRAMES_TILLEMPTY to fill in the frame-count. Use
72  * FQID(n) to fill in the frame queue ID.
73  */
74 #define QM_VDQCR_PRECEDENCE_VDQCR       0x0
75 #define QM_VDQCR_PRECEDENCE_SDQCR       0x80000000
76 #define QM_VDQCR_EXACT                  0x40000000
77 #define QM_VDQCR_NUMFRAMES_MASK         0x3f000000
78 #define QM_VDQCR_NUMFRAMES_SET(n)       (((n) & 0x3f) << 24)
79 #define QM_VDQCR_NUMFRAMES_GET(n)       (((n) >> 24) & 0x3f)
80 #define QM_VDQCR_NUMFRAMES_TILLEMPTY    QM_VDQCR_NUMFRAMES_SET(0)
81
82 /* --- QMan data structures (and associated constants) --- */
83
84 /* Represents s/w corenet portal mapped data structures */
85 struct qm_eqcr_entry;   /* EQCR (EnQueue Command Ring) entries */
86 struct qm_dqrr_entry;   /* DQRR (DeQueue Response Ring) entries */
87 struct qm_mr_entry;     /* MR (Message Ring) entries */
88 struct qm_mc_command;   /* MC (Management Command) command */
89 struct qm_mc_result;    /* MC result */
90
91 #define QM_FD_FORMAT_SG         0x4
92 #define QM_FD_FORMAT_LONG       0x2
93 #define QM_FD_FORMAT_COMPOUND   0x1
94 enum qm_fd_format {
95         /*
96          * 'contig' implies a contiguous buffer, whereas 'sg' implies a
97          * scatter-gather table. 'big' implies a 29-bit length with no offset
98          * field, otherwise length is 20-bit and offset is 9-bit. 'compound'
99          * implies a s/g-like table, where each entry itself represents a frame
100          * (contiguous or scatter-gather) and the 29-bit "length" is
101          * interpreted purely for congestion calculations, ie. a "congestion
102          * weight".
103          */
104         qm_fd_contig = 0,
105         qm_fd_contig_big = QM_FD_FORMAT_LONG,
106         qm_fd_sg = QM_FD_FORMAT_SG,
107         qm_fd_sg_big = QM_FD_FORMAT_SG | QM_FD_FORMAT_LONG,
108         qm_fd_compound = QM_FD_FORMAT_COMPOUND
109 };
110
111 /* Capitalised versions are un-typed but can be used in static expressions */
112 #define QM_FD_CONTIG    0
113 #define QM_FD_CONTIG_BIG QM_FD_FORMAT_LONG
114 #define QM_FD_SG        QM_FD_FORMAT_SG
115 #define QM_FD_SG_BIG    (QM_FD_FORMAT_SG | QM_FD_FORMAT_LONG)
116 #define QM_FD_COMPOUND  QM_FD_FORMAT_COMPOUND
117
118 /* "Frame Descriptor (FD)" */
119 struct qm_fd {
120         union {
121                 struct {
122 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
123                         u8 dd:2;        /* dynamic debug */
124                         u8 liodn_offset:6;
125                         u8 bpid:8;      /* Buffer Pool ID */
126                         u8 eliodn_offset:4;
127                         u8 __reserved:4;
128                         u8 addr_hi;     /* high 8-bits of 40-bit address */
129                         u32 addr_lo;    /* low 32-bits of 40-bit address */
130 #else
131                         u8 liodn_offset:6;
132                         u8 dd:2;        /* dynamic debug */
133                         u8 bpid:8;      /* Buffer Pool ID */
134                         u8 __reserved:4;
135                         u8 eliodn_offset:4;
136                         u8 addr_hi;     /* high 8-bits of 40-bit address */
137                         u32 addr_lo;    /* low 32-bits of 40-bit address */
138 #endif
139                 };
140                 struct {
141                         u64 __notaddress:24;
142                         /* More efficient address accessor */
143                         u64 addr:40;
144                 };
145                 u64 opaque_addr;
146         };
147         /* The 'format' field indicates the interpretation of the remaining 29
148          * bits of the 32-bit word. For packing reasons, it is duplicated in the
149          * other union elements. Note, union'd structs are difficult to use with
150          * static initialisation under gcc, in which case use the "opaque" form
151          * with one of the macros.
152          */
153         union {
154                 /* For easier/faster copying of this part of the fd (eg. from a
155                  * DQRR entry to an EQCR entry) copy 'opaque'
156                  */
157                 u32 opaque;
158                 /* If 'format' is _contig or _sg, 20b length and 9b offset */
159                 struct {
160 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
161                         enum qm_fd_format format:3;
162                         u16 offset:9;
163                         u32 length20:20;
164 #else
165                         u32 length20:20;
166                         u16 offset:9;
167                         enum qm_fd_format format:3;
168 #endif
169                 };
170                 /* If 'format' is _contig_big or _sg_big, 29b length */
171                 struct {
172 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
173                         enum qm_fd_format _format1:3;
174                         u32 length29:29;
175 #else
176                         u32 length29:29;
177                         enum qm_fd_format _format1:3;
178 #endif
179                 };
180                 /* If 'format' is _compound, 29b "congestion weight" */
181                 struct {
182 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
183                         enum qm_fd_format _format2:3;
184                         u32 cong_weight:29;
185 #else
186                         u32 cong_weight:29;
187                         enum qm_fd_format _format2:3;
188 #endif
189                 };
190         };
191         union {
192                 u32 cmd;
193                 u32 status;
194         };
195 } __attribute__((aligned(8)));
196 #define QM_FD_DD_NULL           0x00
197 #define QM_FD_PID_MASK          0x3f
198 static inline u64 qm_fd_addr_get64(const struct qm_fd *fd)
199 {
200         return fd->addr;
201 }
202
203 static inline dma_addr_t qm_fd_addr(const struct qm_fd *fd)
204 {
205         return (dma_addr_t)fd->addr;
206 }
207
208 /* Macro, so we compile better if 'v' isn't always 64-bit */
209 #define qm_fd_addr_set64(fd, v) \
210         do { \
211                 struct qm_fd *__fd931 = (fd); \
212                 __fd931->addr = v; \
213         } while (0)
214
215 /* Scatter/Gather table entry */
216 struct qm_sg_entry {
217         union {
218                 struct {
219 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
220                         u8 __reserved1[3];
221                         u8 addr_hi;     /* high 8-bits of 40-bit address */
222                         u32 addr_lo;    /* low 32-bits of 40-bit address */
223 #else
224                         u32 addr_lo;    /* low 32-bits of 40-bit address */
225                         u8 addr_hi;     /* high 8-bits of 40-bit address */
226                         u8 __reserved1[3];
227 #endif
228                 };
229                 struct {
230 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
231                         u64 __notaddress:24;
232                         u64 addr:40;
233 #else
234                         u64 addr:40;
235                         u64 __notaddress:24;
236 #endif
237                 };
238                 u64 opaque;
239         };
240         union {
241                 struct {
242 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
243                         u32 extension:1;        /* Extension bit */
244                         u32 final:1;            /* Final bit */
245                         u32 length:30;
246 #else
247                         u32 length:30;
248                         u32 final:1;            /* Final bit */
249                         u32 extension:1;        /* Extension bit */
250 #endif
251                 };
252                 u32 val;
253         };
254         u8 __reserved2;
255         u8 bpid;
256         union {
257                 struct {
258 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
259                         u16 __reserved3:3;
260                         u16 offset:13;
261 #else
262                         u16 offset:13;
263                         u16 __reserved3:3;
264 #endif
265                 };
266                 u16 val_off;
267         };
268 } __packed;
269 static inline u64 qm_sg_entry_get64(const struct qm_sg_entry *sg)
270 {
271         return sg->addr;
272 }
273
274 static inline dma_addr_t qm_sg_addr(const struct qm_sg_entry *sg)
275 {
276         return (dma_addr_t)sg->addr;
277 }
278
279 /* Macro, so we compile better if 'v' isn't always 64-bit */
280 #define qm_sg_entry_set64(sg, v) \
281         do { \
282                 struct qm_sg_entry *__sg931 = (sg); \
283                 __sg931->addr = v; \
284         } while (0)
285
286 /* See 1.5.8.1: "Enqueue Command" */
287 struct __rte_aligned(8) qm_eqcr_entry {
288         u8 __dont_write_directly__verb;
289         u8 dca;
290         u16 seqnum;
291         u32 orp;        /* 24-bit */
292         u32 fqid;       /* 24-bit */
293         u32 tag;
294         struct qm_fd fd; /* this has alignment 8 */
295         u8 __reserved3[32];
296 } __packed;
297
298
299 /* "Frame Dequeue Response" */
300 struct __rte_aligned(8) qm_dqrr_entry {
301         u8 verb;
302         u8 stat;
303         u16 seqnum;     /* 15-bit */
304         u8 tok;
305         u8 __reserved2[3];
306         u32 fqid;       /* 24-bit */
307         u32 contextB;
308         struct qm_fd fd; /* this has alignment 8 */
309         u8 __reserved4[32];
310 };
311
312 #define QM_DQRR_VERB_VBIT               0x80
313 #define QM_DQRR_VERB_MASK               0x7f    /* where the verb contains; */
314 #define QM_DQRR_VERB_FRAME_DEQUEUE      0x60    /* "this format" */
315 #define QM_DQRR_STAT_FQ_EMPTY           0x80    /* FQ empty */
316 #define QM_DQRR_STAT_FQ_HELDACTIVE      0x40    /* FQ held active */
317 #define QM_DQRR_STAT_FQ_FORCEELIGIBLE   0x20    /* FQ was force-eligible'd */
318 #define QM_DQRR_STAT_FD_VALID           0x10    /* has a non-NULL FD */
319 #define QM_DQRR_STAT_UNSCHEDULED        0x02    /* Unscheduled dequeue */
320 #define QM_DQRR_STAT_DQCR_EXPIRED       0x01    /* VDQCR or PDQCR expired*/
321
322
323 /* "ERN Message Response" */
324 /* "FQ State Change Notification" */
325 struct qm_mr_entry {
326         union {
327                 struct {
328                         u8 verb;
329                         u8 dca;
330                         u16 seqnum;
331                         u8 rc;          /* Rejection Code */
332                         u32 orp:24;
333                         u32 fqid;       /* 24-bit */
334                         u32 tag;
335                         struct qm_fd fd; /* this has alignment 8 */
336                 } __packed __rte_aligned(8) ern;
337                 struct {
338                         u8 verb;
339 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
340                         u8 colour:2;    /* See QM_MR_DCERN_COLOUR_* */
341                         u8 __reserved1:4;
342                         enum qm_dc_portal portal:2;
343 #else
344                         enum qm_dc_portal portal:3;
345                         u8 __reserved1:3;
346                         u8 colour:2;    /* See QM_MR_DCERN_COLOUR_* */
347 #endif
348                         u16 __reserved2;
349                         u8 rc;          /* Rejection Code */
350                         u32 __reserved3:24;
351                         u32 fqid;       /* 24-bit */
352                         u32 tag;
353                         struct qm_fd fd; /* this has alignment 8 */
354                 } __packed __rte_aligned(8) dcern;
355                 struct {
356                         u8 verb;
357                         u8 fqs;         /* Frame Queue Status */
358                         u8 __reserved1[6];
359                         u32 fqid;       /* 24-bit */
360                         u32 contextB;
361                         u8 __reserved2[16];
362                 } __packed __rte_aligned(8) fq; /* FQRN/FQRNI/FQRL/FQPN */
363         };
364         u8 __reserved2[32];
365 } __packed __rte_aligned(8);
366 #define QM_MR_VERB_VBIT                 0x80
367 /*
368  * ERNs originating from direct-connect portals ("dcern") use 0x20 as a verb
369  * which would be invalid as a s/w enqueue verb. A s/w ERN can be distinguished
370  * from the other MR types by noting if the 0x20 bit is unset.
371  */
372 #define QM_MR_VERB_TYPE_MASK            0x27
373 #define QM_MR_VERB_DC_ERN               0x20
374 #define QM_MR_VERB_FQRN                 0x21
375 #define QM_MR_VERB_FQRNI                0x22
376 #define QM_MR_VERB_FQRL                 0x23
377 #define QM_MR_VERB_FQPN                 0x24
378 #define QM_MR_RC_MASK                   0xf0    /* contains one of; */
379 #define QM_MR_RC_CGR_TAILDROP           0x00
380 #define QM_MR_RC_WRED                   0x10
381 #define QM_MR_RC_ERROR                  0x20
382 #define QM_MR_RC_ORPWINDOW_EARLY        0x30
383 #define QM_MR_RC_ORPWINDOW_LATE         0x40
384 #define QM_MR_RC_FQ_TAILDROP            0x50
385 #define QM_MR_RC_ORPWINDOW_RETIRED      0x60
386 #define QM_MR_RC_ORP_ZERO               0x70
387 #define QM_MR_FQS_ORLPRESENT            0x02    /* ORL fragments to come */
388 #define QM_MR_FQS_NOTEMPTY              0x01    /* FQ has enqueued frames */
389 #define QM_MR_DCERN_COLOUR_GREEN        0x00
390 #define QM_MR_DCERN_COLOUR_YELLOW       0x01
391 #define QM_MR_DCERN_COLOUR_RED          0x02
392 #define QM_MR_DCERN_COLOUR_OVERRIDE     0x03
393 /*
394  * An identical structure of FQD fields is present in the "Init FQ" command and
395  * the "Query FQ" result, it's suctioned out into the "struct qm_fqd" type.
396  * Within that, the 'stashing' and 'taildrop' pieces are also factored out, the
397  * latter has two inlines to assist with converting to/from the mant+exp
398  * representation.
399  */
400 struct qm_fqd_stashing {
401         /* See QM_STASHING_EXCL_<...> */
402 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
403         u8 exclusive;
404         u8 __reserved1:2;
405         /* Numbers of cachelines */
406         u8 annotation_cl:2;
407         u8 data_cl:2;
408         u8 context_cl:2;
409 #else
410         u8 context_cl:2;
411         u8 data_cl:2;
412         u8 annotation_cl:2;
413         u8 __reserved1:2;
414         u8 exclusive;
415 #endif
416 } __packed;
417 struct qm_fqd_taildrop {
418 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
419         u16 __reserved1:3;
420         u16 mant:8;
421         u16 exp:5;
422 #else
423         u16 exp:5;
424         u16 mant:8;
425         u16 __reserved1:3;
426 #endif
427 } __packed;
428 struct qm_fqd_oac {
429         /* "Overhead Accounting Control", see QM_OAC_<...> */
430 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
431         u8 oac:2; /* "Overhead Accounting Control" */
432         u8 __reserved1:6;
433 #else
434         u8 __reserved1:6;
435         u8 oac:2; /* "Overhead Accounting Control" */
436 #endif
437         /* Two's-complement value (-128 to +127) */
438         signed char oal; /* "Overhead Accounting Length" */
439 } __packed;
440 struct qm_fqd {
441         union {
442                 u8 orpc;
443                 struct {
444 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
445                         u8 __reserved1:2;
446                         u8 orprws:3;
447                         u8 oa:1;
448                         u8 olws:2;
449 #else
450                         u8 olws:2;
451                         u8 oa:1;
452                         u8 orprws:3;
453                         u8 __reserved1:2;
454 #endif
455                 } __packed;
456         };
457         u8 cgid;
458         u16 fq_ctrl;    /* See QM_FQCTRL_<...> */
459         union {
460                 u16 dest_wq;
461                 struct {
462 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
463                         u16 channel:13; /* qm_channel */
464                         u16 wq:3;
465 #else
466                         u16 wq:3;
467                         u16 channel:13; /* qm_channel */
468 #endif
469                 } __packed dest;
470         };
471 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
472         u16 __reserved2:1;
473         u16 ics_cred:15;
474 #else
475         u16 __reserved2:1;
476         u16 ics_cred:15;
477 #endif
478         /*
479          * For "Initialize Frame Queue" commands, the write-enable mask
480          * determines whether 'td' or 'oac_init' is observed. For query
481          * commands, this field is always 'td', and 'oac_query' (below) reflects
482          * the Overhead ACcounting values.
483          */
484         union {
485                 uint16_t opaque_td;
486                 struct qm_fqd_taildrop td;
487                 struct qm_fqd_oac oac_init;
488         };
489         u32 context_b;
490         union {
491                 /* Treat it as 64-bit opaque */
492                 u64 opaque;
493                 struct {
494 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
495                         u32 hi;
496                         u32 lo;
497 #else
498                         u32 lo;
499                         u32 hi;
500 #endif
501                 };
502                 /* Treat it as s/w portal stashing config */
503                 /* see "FQD Context_A field used for [...]" */
504                 struct {
505 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
506                         struct qm_fqd_stashing stashing;
507                         /*
508                          * 48-bit address of FQ context to
509                          * stash, must be cacheline-aligned
510                          */
511                         u16 context_hi;
512                         u32 context_lo;
513 #else
514                         u32 context_lo;
515                         u16 context_hi;
516                         struct qm_fqd_stashing stashing;
517 #endif
518                 } __packed;
519         } context_a;
520         struct qm_fqd_oac oac_query;
521 } __packed;
522 /* 64-bit converters for context_hi/lo */
523 static inline u64 qm_fqd_stashing_get64(const struct qm_fqd *fqd)
524 {
525         return ((u64)fqd->context_a.context_hi << 32) |
526                 (u64)fqd->context_a.context_lo;
527 }
528
529 static inline dma_addr_t qm_fqd_stashing_addr(const struct qm_fqd *fqd)
530 {
531         return (dma_addr_t)qm_fqd_stashing_get64(fqd);
532 }
533
534 static inline u64 qm_fqd_context_a_get64(const struct qm_fqd *fqd)
535 {
536         return ((u64)fqd->context_a.hi << 32) |
537                 (u64)fqd->context_a.lo;
538 }
539
540 static inline void qm_fqd_stashing_set64(struct qm_fqd *fqd, u64 addr)
541 {
542                 fqd->context_a.context_hi = upper_32_bits(addr);
543                 fqd->context_a.context_lo = lower_32_bits(addr);
544 }
545
546 static inline void qm_fqd_context_a_set64(struct qm_fqd *fqd, u64 addr)
547 {
548         fqd->context_a.hi = upper_32_bits(addr);
549         fqd->context_a.lo = lower_32_bits(addr);
550 }
551
552 /* convert a threshold value into mant+exp representation */
553 static inline int qm_fqd_taildrop_set(struct qm_fqd_taildrop *td, u32 val,
554                                       int roundup)
555 {
556         u32 e = 0;
557         int oddbit = 0;
558
559         if (val > 0xe0000000)
560                 return -ERANGE;
561         while (val > 0xff) {
562                 oddbit = val & 1;
563                 val >>= 1;
564                 e++;
565                 if (roundup && oddbit)
566                         val++;
567         }
568         td->exp = e;
569         td->mant = val;
570         return 0;
571 }
572
573 /* and the other direction */
574 static inline u32 qm_fqd_taildrop_get(const struct qm_fqd_taildrop *td)
575 {
576         return (u32)td->mant << td->exp;
577 }
578
579
580 /* See "Frame Queue Descriptor (FQD)" */
581 /* Frame Queue Descriptor (FQD) field 'fq_ctrl' uses these constants */
582 #define QM_FQCTRL_MASK          0x07ff  /* 'fq_ctrl' flags; */
583 #define QM_FQCTRL_CGE           0x0400  /* Congestion Group Enable */
584 #define QM_FQCTRL_TDE           0x0200  /* Tail-Drop Enable */
585 #define QM_FQCTRL_ORP           0x0100  /* ORP Enable */
586 #define QM_FQCTRL_CTXASTASHING  0x0080  /* Context-A stashing */
587 #define QM_FQCTRL_CPCSTASH      0x0040  /* CPC Stash Enable */
588 #define QM_FQCTRL_FORCESFDR     0x0008  /* High-priority SFDRs */
589 #define QM_FQCTRL_AVOIDBLOCK    0x0004  /* Don't block active */
590 #define QM_FQCTRL_HOLDACTIVE    0x0002  /* Hold active in portal */
591 #define QM_FQCTRL_PREFERINCACHE 0x0001  /* Aggressively cache FQD */
592 #define QM_FQCTRL_LOCKINCACHE   QM_FQCTRL_PREFERINCACHE /* older naming */
593
594 /* See "FQD Context_A field used for [...] */
595 /* Frame Queue Descriptor (FQD) field 'CONTEXT_A' uses these constants */
596 #define QM_STASHING_EXCL_ANNOTATION     0x04
597 #define QM_STASHING_EXCL_DATA           0x02
598 #define QM_STASHING_EXCL_CTX            0x01
599
600 /* See "Intra Class Scheduling" */
601 /* FQD field 'OAC' (Overhead ACcounting) uses these constants */
602 #define QM_OAC_ICS              0x2 /* Accounting for Intra-Class Scheduling */
603 #define QM_OAC_CG               0x1 /* Accounting for Congestion Groups */
604
605 /*
606  * This struct represents the 32-bit "WR_PARM_[GYR]" parameters in CGR fields
607  * and associated commands/responses. The WRED parameters are calculated from
608  * these fields as follows;
609  *   MaxTH = MA * (2 ^ Mn)
610  *   Slope = SA / (2 ^ Sn)
611  *    MaxP = 4 * (Pn + 1)
612  */
613 struct qm_cgr_wr_parm {
614         union {
615                 u32 word;
616                 struct {
617 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
618                         u32 MA:8;
619                         u32 Mn:5;
620                         u32 SA:7; /* must be between 64-127 */
621                         u32 Sn:6;
622                         u32 Pn:6;
623 #else
624                         u32 Pn:6;
625                         u32 Sn:6;
626                         u32 SA:7; /* must be between 64-127 */
627                         u32 Mn:5;
628                         u32 MA:8;
629 #endif
630                 } __packed;
631         };
632 } __packed;
633 /*
634  * This struct represents the 13-bit "CS_THRES" CGR field. In the corresponding
635  * management commands, this is padded to a 16-bit structure field, so that's
636  * how we represent it here. The congestion state threshold is calculated from
637  * these fields as follows;
638  *   CS threshold = TA * (2 ^ Tn)
639  */
640 struct qm_cgr_cs_thres {
641         union {
642                 u16 hword;
643                 struct {
644 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
645                         u16 __reserved:3;
646                         u16 TA:8;
647                         u16 Tn:5;
648 #else
649                         u16 Tn:5;
650                         u16 TA:8;
651                         u16 __reserved:3;
652 #endif
653                 } __packed;
654         };
655 } __packed;
656 /*
657  * This identical structure of CGR fields is present in the "Init/Modify CGR"
658  * commands and the "Query CGR" result. It's suctioned out here into its own
659  * struct.
660  */
661 struct __qm_mc_cgr {
662         struct qm_cgr_wr_parm wr_parm_g;
663         struct qm_cgr_wr_parm wr_parm_y;
664         struct qm_cgr_wr_parm wr_parm_r;
665         u8 wr_en_g;     /* boolean, use QM_CGR_EN */
666         u8 wr_en_y;     /* boolean, use QM_CGR_EN */
667         u8 wr_en_r;     /* boolean, use QM_CGR_EN */
668         u8 cscn_en;     /* boolean, use QM_CGR_EN */
669         union {
670                 struct {
671 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
672                         u16 cscn_targ_upd_ctrl; /* use QM_CSCN_TARG_UDP_ */
673                         u16 cscn_targ_dcp_low;  /* CSCN_TARG_DCP low-16bits */
674 #else
675                         u16 cscn_targ_dcp_low;  /* CSCN_TARG_DCP low-16bits */
676                         u16 cscn_targ_upd_ctrl; /* use QM_CSCN_TARG_UDP_ */
677 #endif
678                 };
679                 u32 cscn_targ;  /* use QM_CGR_TARG_* */
680         };
681         u8 cstd_en;     /* boolean, use QM_CGR_EN */
682         u8 cs;          /* boolean, only used in query response */
683         union {
684                 struct qm_cgr_cs_thres cs_thres;
685                 /* use qm_cgr_cs_thres_set64() */
686                 u16 __cs_thres;
687         };
688         u8 mode;        /* QMAN_CGR_MODE_FRAME not supported in rev1.0 */
689 } __packed;
690 #define QM_CGR_EN               0x01 /* For wr_en_*, cscn_en, cstd_en */
691 #define QM_CGR_TARG_UDP_CTRL_WRITE_BIT  0x8000 /* value written to portal bit*/
692 #define QM_CGR_TARG_UDP_CTRL_DCP        0x4000 /* 0: SWP, 1: DCP */
693 #define QM_CGR_TARG_PORTAL(n)   (0x80000000 >> (n)) /* s/w portal, 0-9 */
694 #define QM_CGR_TARG_FMAN0       0x00200000 /* direct-connect portal: fman0 */
695 #define QM_CGR_TARG_FMAN1       0x00100000 /*                      : fman1 */
696 /* Convert CGR thresholds to/from "cs_thres" format */
697 static inline u64 qm_cgr_cs_thres_get64(const struct qm_cgr_cs_thres *th)
698 {
699         return (u64)th->TA << th->Tn;
700 }
701
702 static inline int qm_cgr_cs_thres_set64(struct qm_cgr_cs_thres *th, u64 val,
703                                         int roundup)
704 {
705         u32 e = 0;
706         int oddbit = 0;
707
708         while (val > 0xff) {
709                 oddbit = val & 1;
710                 val >>= 1;
711                 e++;
712                 if (roundup && oddbit)
713                         val++;
714         }
715         th->Tn = e;
716         th->TA = val;
717         return 0;
718 }
719
720 /* See 1.5.8.5.1: "Initialize FQ" */
721 /* See 1.5.8.5.2: "Query FQ" */
722 /* See 1.5.8.5.3: "Query FQ Non-Programmable Fields" */
723 /* See 1.5.8.5.4: "Alter FQ State Commands " */
724 /* See 1.5.8.6.1: "Initialize/Modify CGR" */
725 /* See 1.5.8.6.2: "CGR Test Write" */
726 /* See 1.5.8.6.3: "Query CGR" */
727 /* See 1.5.8.6.4: "Query Congestion Group State" */
728 struct qm_mcc_initfq {
729         u8 __reserved1;
730         u16 we_mask;    /* Write Enable Mask */
731         u32 fqid;       /* 24-bit */
732         u16 count;      /* Initialises 'count+1' FQDs */
733         struct qm_fqd fqd; /* the FQD fields go here */
734         u8 __reserved3[30];
735 } __packed;
736 struct qm_mcc_queryfq {
737         u8 __reserved1[3];
738         u32 fqid;       /* 24-bit */
739         u8 __reserved2[56];
740 } __packed;
741 struct qm_mcc_queryfq_np {
742         u8 __reserved1[3];
743         u32 fqid;       /* 24-bit */
744         u8 __reserved2[56];
745 } __packed;
746 struct qm_mcc_alterfq {
747         u8 __reserved1[3];
748         u32 fqid;       /* 24-bit */
749         u8 __reserved2;
750         u8 count;       /* number of consecutive FQID */
751         u8 __reserved3[10];
752         u32 context_b;  /* frame queue context b */
753         u8 __reserved4[40];
754 } __packed;
755 struct qm_mcc_initcgr {
756         u8 __reserved1;
757         u16 we_mask;    /* Write Enable Mask */
758         struct __qm_mc_cgr cgr; /* CGR fields */
759         u8 __reserved2[2];
760         u8 cgid;
761         u8 __reserved4[32];
762 } __packed;
763 struct qm_mcc_cgrtestwrite {
764         u8 __reserved1[2];
765         u8 i_bcnt_hi:8;/* high 8-bits of 40-bit "Instant" */
766         u32 i_bcnt_lo;  /* low 32-bits of 40-bit */
767         u8 __reserved2[23];
768         u8 cgid;
769         u8 __reserved3[32];
770 } __packed;
771 struct qm_mcc_querycgr {
772         u8 __reserved1[30];
773         u8 cgid;
774         u8 __reserved2[32];
775 } __packed;
776 struct qm_mcc_querycongestion {
777         u8 __reserved[63];
778 } __packed;
779 struct qm_mcc_querywq {
780         u8 __reserved;
781         /* select channel if verb != QUERYWQ_DEDICATED */
782         union {
783                 u16 channel_wq; /* ignores wq (3 lsbits) */
784                 struct {
785 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
786                         u16 id:13; /* qm_channel */
787                         u16 __reserved1:3;
788 #else
789                         u16 __reserved1:3;
790                         u16 id:13; /* qm_channel */
791 #endif
792                 } __packed channel;
793         };
794         u8 __reserved2[60];
795 } __packed;
796
797 struct qm_mc_command {
798         u8 __dont_write_directly__verb;
799         union {
800                 struct qm_mcc_initfq initfq;
801                 struct qm_mcc_queryfq queryfq;
802                 struct qm_mcc_queryfq_np queryfq_np;
803                 struct qm_mcc_alterfq alterfq;
804                 struct qm_mcc_initcgr initcgr;
805                 struct qm_mcc_cgrtestwrite cgrtestwrite;
806                 struct qm_mcc_querycgr querycgr;
807                 struct qm_mcc_querycongestion querycongestion;
808                 struct qm_mcc_querywq querywq;
809         };
810 } __packed;
811
812 /* INITFQ-specific flags */
813 #define QM_INITFQ_WE_MASK               0x01ff  /* 'Write Enable' flags; */
814 #define QM_INITFQ_WE_OAC                0x0100
815 #define QM_INITFQ_WE_ORPC               0x0080
816 #define QM_INITFQ_WE_CGID               0x0040
817 #define QM_INITFQ_WE_FQCTRL             0x0020
818 #define QM_INITFQ_WE_DESTWQ             0x0010
819 #define QM_INITFQ_WE_ICSCRED            0x0008
820 #define QM_INITFQ_WE_TDTHRESH           0x0004
821 #define QM_INITFQ_WE_CONTEXTB           0x0002
822 #define QM_INITFQ_WE_CONTEXTA           0x0001
823 /* INITCGR/MODIFYCGR-specific flags */
824 #define QM_CGR_WE_MASK                  0x07ff  /* 'Write Enable Mask'; */
825 #define QM_CGR_WE_WR_PARM_G             0x0400
826 #define QM_CGR_WE_WR_PARM_Y             0x0200
827 #define QM_CGR_WE_WR_PARM_R             0x0100
828 #define QM_CGR_WE_WR_EN_G               0x0080
829 #define QM_CGR_WE_WR_EN_Y               0x0040
830 #define QM_CGR_WE_WR_EN_R               0x0020
831 #define QM_CGR_WE_CSCN_EN               0x0010
832 #define QM_CGR_WE_CSCN_TARG             0x0008
833 #define QM_CGR_WE_CSTD_EN               0x0004
834 #define QM_CGR_WE_CS_THRES              0x0002
835 #define QM_CGR_WE_MODE                  0x0001
836
837 struct qm_mcr_initfq {
838         u8 __reserved1[62];
839 } __packed;
840 struct qm_mcr_queryfq {
841         u8 __reserved1[8];
842         struct qm_fqd fqd;      /* the FQD fields are here */
843         u8 __reserved2[30];
844 } __packed;
845 struct qm_mcr_queryfq_np {
846         u8 __reserved1;
847         u8 state;       /* QM_MCR_NP_STATE_*** */
848 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
849         u8 __reserved2;
850         u32 fqd_link:24;
851         u16 __reserved3:2;
852         u16 odp_seq:14;
853         u16 __reserved4:2;
854         u16 orp_nesn:14;
855         u16 __reserved5:1;
856         u16 orp_ea_hseq:15;
857         u16 __reserved6:1;
858         u16 orp_ea_tseq:15;
859         u8 __reserved7;
860         u32 orp_ea_hptr:24;
861         u8 __reserved8;
862         u32 orp_ea_tptr:24;
863         u8 __reserved9;
864         u32 pfdr_hptr:24;
865         u8 __reserved10;
866         u32 pfdr_tptr:24;
867         u8 __reserved11[5];
868         u8 __reserved12:7;
869         u8 is:1;
870         u16 ics_surp;
871         u32 byte_cnt;
872         u8 __reserved13;
873         u32 frm_cnt:24;
874         u32 __reserved14;
875         u16 ra1_sfdr;   /* QM_MCR_NP_RA1_*** */
876         u16 ra2_sfdr;   /* QM_MCR_NP_RA2_*** */
877         u16 __reserved15;
878         u16 od1_sfdr;   /* QM_MCR_NP_OD1_*** */
879         u16 od2_sfdr;   /* QM_MCR_NP_OD2_*** */
880         u16 od3_sfdr;   /* QM_MCR_NP_OD3_*** */
881 #else
882         u8 __reserved2;
883         u32 fqd_link:24;
884
885         u16 odp_seq:14;
886         u16 __reserved3:2;
887
888         u16 orp_nesn:14;
889         u16 __reserved4:2;
890
891         u16 orp_ea_hseq:15;
892         u16 __reserved5:1;
893
894         u16 orp_ea_tseq:15;
895         u16 __reserved6:1;
896
897         u8 __reserved7;
898         u32 orp_ea_hptr:24;
899
900         u8 __reserved8;
901         u32 orp_ea_tptr:24;
902
903         u8 __reserved9;
904         u32 pfdr_hptr:24;
905
906         u8 __reserved10;
907         u32 pfdr_tptr:24;
908
909         u8 __reserved11[5];
910         u8 is:1;
911         u8 __reserved12:7;
912         u16 ics_surp;
913         u32 byte_cnt;
914         u8 __reserved13;
915         u32 frm_cnt:24;
916         u32 __reserved14;
917         u16 ra1_sfdr;   /* QM_MCR_NP_RA1_*** */
918         u16 ra2_sfdr;   /* QM_MCR_NP_RA2_*** */
919         u16 __reserved15;
920         u16 od1_sfdr;   /* QM_MCR_NP_OD1_*** */
921         u16 od2_sfdr;   /* QM_MCR_NP_OD2_*** */
922         u16 od3_sfdr;   /* QM_MCR_NP_OD3_*** */
923 #endif
924 } __packed;
925
926 struct qm_mcr_alterfq {
927         u8 fqs;         /* Frame Queue Status */
928         u8 __reserved1[61];
929 } __packed;
930 struct qm_mcr_initcgr {
931         u8 __reserved1[62];
932 } __packed;
933 struct qm_mcr_cgrtestwrite {
934         u16 __reserved1;
935         struct __qm_mc_cgr cgr; /* CGR fields */
936         u8 __reserved2[3];
937         u32 __reserved3:24;
938         u32 i_bcnt_hi:8;/* high 8-bits of 40-bit "Instant" */
939         u32 i_bcnt_lo;  /* low 32-bits of 40-bit */
940         u32 __reserved4:24;
941         u32 a_bcnt_hi:8;/* high 8-bits of 40-bit "Average" */
942         u32 a_bcnt_lo;  /* low 32-bits of 40-bit */
943         u16 lgt;        /* Last Group Tick */
944         u16 wr_prob_g;
945         u16 wr_prob_y;
946         u16 wr_prob_r;
947         u8 __reserved5[8];
948 } __packed;
949 struct qm_mcr_querycgr {
950         u16 __reserved1;
951         struct __qm_mc_cgr cgr; /* CGR fields */
952         u8 __reserved2[3];
953         union {
954                 struct {
955 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
956                         u32 __reserved3:24;
957                         u32 i_bcnt_hi:8;/* high 8-bits of 40-bit "Instant" */
958                         u32 i_bcnt_lo;  /* low 32-bits of 40-bit */
959 #else
960                         u32 i_bcnt_lo;  /* low 32-bits of 40-bit */
961                         u32 i_bcnt_hi:8;/* high 8-bits of 40-bit "Instant" */
962                         u32 __reserved3:24;
963 #endif
964                 };
965                 u64 i_bcnt;
966         };
967         union {
968                 struct {
969 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
970                         u32 __reserved4:24;
971                         u32 a_bcnt_hi:8;/* high 8-bits of 40-bit "Average" */
972                         u32 a_bcnt_lo;  /* low 32-bits of 40-bit */
973 #else
974                         u32 a_bcnt_lo;  /* low 32-bits of 40-bit */
975                         u32 a_bcnt_hi:8;/* high 8-bits of 40-bit "Average" */
976                         u32 __reserved4:24;
977 #endif
978                 };
979                 u64 a_bcnt;
980         };
981         union {
982                 u32 cscn_targ_swp[4];
983                 u8 __reserved5[16];
984         };
985 } __packed;
986
987 struct __qm_mcr_querycongestion {
988         u32 state[8];
989 };
990
991 struct qm_mcr_querycongestion {
992         u8 __reserved[30];
993         /* Access this struct using QM_MCR_QUERYCONGESTION() */
994         struct __qm_mcr_querycongestion state;
995 } __packed;
996 struct qm_mcr_querywq {
997         union {
998                 u16 channel_wq; /* ignores wq (3 lsbits) */
999                 struct {
1000 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
1001                         u16 id:13; /* qm_channel */
1002                         u16 __reserved:3;
1003 #else
1004                         u16 __reserved:3;
1005                         u16 id:13; /* qm_channel */
1006 #endif
1007                 } __packed channel;
1008         };
1009         u8 __reserved[28];
1010         u32 wq_len[8];
1011 } __packed;
1012
1013 struct qm_mc_result {
1014         u8 verb;
1015         u8 result;
1016         union {
1017                 struct qm_mcr_initfq initfq;
1018                 struct qm_mcr_queryfq queryfq;
1019                 struct qm_mcr_queryfq_np queryfq_np;
1020                 struct qm_mcr_alterfq alterfq;
1021                 struct qm_mcr_initcgr initcgr;
1022                 struct qm_mcr_cgrtestwrite cgrtestwrite;
1023                 struct qm_mcr_querycgr querycgr;
1024                 struct qm_mcr_querycongestion querycongestion;
1025                 struct qm_mcr_querywq querywq;
1026         };
1027 } __packed;
1028
1029 #define QM_MCR_VERB_RRID                0x80
1030 #define QM_MCR_VERB_MASK                QM_MCC_VERB_MASK
1031 #define QM_MCR_VERB_INITFQ_PARKED       QM_MCC_VERB_INITFQ_PARKED
1032 #define QM_MCR_VERB_INITFQ_SCHED        QM_MCC_VERB_INITFQ_SCHED
1033 #define QM_MCR_VERB_QUERYFQ             QM_MCC_VERB_QUERYFQ
1034 #define QM_MCR_VERB_QUERYFQ_NP          QM_MCC_VERB_QUERYFQ_NP
1035 #define QM_MCR_VERB_QUERYWQ             QM_MCC_VERB_QUERYWQ
1036 #define QM_MCR_VERB_QUERYWQ_DEDICATED   QM_MCC_VERB_QUERYWQ_DEDICATED
1037 #define QM_MCR_VERB_ALTER_SCHED         QM_MCC_VERB_ALTER_SCHED
1038 #define QM_MCR_VERB_ALTER_FE            QM_MCC_VERB_ALTER_FE
1039 #define QM_MCR_VERB_ALTER_RETIRE        QM_MCC_VERB_ALTER_RETIRE
1040 #define QM_MCR_VERB_ALTER_OOS           QM_MCC_VERB_ALTER_OOS
1041 #define QM_MCR_RESULT_NULL              0x00
1042 #define QM_MCR_RESULT_OK                0xf0
1043 #define QM_MCR_RESULT_ERR_FQID          0xf1
1044 #define QM_MCR_RESULT_ERR_FQSTATE       0xf2
1045 #define QM_MCR_RESULT_ERR_NOTEMPTY      0xf3    /* OOS fails if FQ is !empty */
1046 #define QM_MCR_RESULT_ERR_BADCHANNEL    0xf4
1047 #define QM_MCR_RESULT_PENDING           0xf8
1048 #define QM_MCR_RESULT_ERR_BADCOMMAND    0xff
1049 #define QM_MCR_NP_STATE_FE              0x10
1050 #define QM_MCR_NP_STATE_R               0x08
1051 #define QM_MCR_NP_STATE_MASK            0x07    /* Reads FQD::STATE; */
1052 #define QM_MCR_NP_STATE_OOS             0x00
1053 #define QM_MCR_NP_STATE_RETIRED         0x01
1054 #define QM_MCR_NP_STATE_TEN_SCHED       0x02
1055 #define QM_MCR_NP_STATE_TRU_SCHED       0x03
1056 #define QM_MCR_NP_STATE_PARKED          0x04
1057 #define QM_MCR_NP_STATE_ACTIVE          0x05
1058 #define QM_MCR_NP_PTR_MASK              0x07ff  /* for RA[12] & OD[123] */
1059 #define QM_MCR_NP_RA1_NRA(v)            (((v) >> 14) & 0x3)     /* FQD::NRA */
1060 #define QM_MCR_NP_RA2_IT(v)             (((v) >> 14) & 0x1)     /* FQD::IT */
1061 #define QM_MCR_NP_OD1_NOD(v)            (((v) >> 14) & 0x3)     /* FQD::NOD */
1062 #define QM_MCR_NP_OD3_NPC(v)            (((v) >> 14) & 0x3)     /* FQD::NPC */
1063 #define QM_MCR_FQS_ORLPRESENT           0x02    /* ORL fragments to come */
1064 #define QM_MCR_FQS_NOTEMPTY             0x01    /* FQ has enqueued frames */
1065 /* This extracts the state for congestion group 'n' from a query response.
1066  * Eg.
1067  *   u8 cgr = [...];
1068  *   struct qm_mc_result *res = [...];
1069  *   printf("congestion group %d congestion state: %d\n", cgr,
1070  *       QM_MCR_QUERYCONGESTION(&res->querycongestion.state, cgr));
1071  */
1072 #define __CGR_WORD(num)         (num >> 5)
1073 #define __CGR_SHIFT(num)        (num & 0x1f)
1074 #define __CGR_NUM               (sizeof(struct __qm_mcr_querycongestion) << 3)
1075 static inline int QM_MCR_QUERYCONGESTION(struct __qm_mcr_querycongestion *p,
1076                                          u8 cgr)
1077 {
1078         return p->state[__CGR_WORD(cgr)] & (0x80000000 >> __CGR_SHIFT(cgr));
1079 }
1080
1081         /* Portal and Frame Queues */
1082 /* Represents a managed portal */
1083 struct qman_portal;
1084
1085 /*
1086  * This object type represents QMan frame queue descriptors (FQD), it is
1087  * cacheline-aligned, and initialised by qman_create_fq(). The structure is
1088  * defined further down.
1089  */
1090 struct qman_fq;
1091
1092 /*
1093  * This object type represents a QMan congestion group, it is defined further
1094  * down.
1095  */
1096 struct qman_cgr;
1097
1098 /*
1099  * This enum, and the callback type that returns it, are used when handling
1100  * dequeued frames via DQRR. Note that for "null" callbacks registered with the
1101  * portal object (for handling dequeues that do not demux because context_b is
1102  * NULL), the return value *MUST* be qman_cb_dqrr_consume.
1103  */
1104 enum qman_cb_dqrr_result {
1105         /* DQRR entry can be consumed */
1106         qman_cb_dqrr_consume,
1107         /* Like _consume, but requests parking - FQ must be held-active */
1108         qman_cb_dqrr_park,
1109         /* Does not consume, for DCA mode only. This allows out-of-order
1110          * consumes by explicit calls to qman_dca() and/or the use of implicit
1111          * DCA via EQCR entries.
1112          */
1113         qman_cb_dqrr_defer,
1114         /*
1115          * Stop processing without consuming this ring entry. Exits the current
1116          * qman_p_poll_dqrr() or interrupt-handling, as appropriate. If within
1117          * an interrupt handler, the callback would typically call
1118          * qman_irqsource_remove(QM_PIRQ_DQRI) before returning this value,
1119          * otherwise the interrupt will reassert immediately.
1120          */
1121         qman_cb_dqrr_stop,
1122         /* Like qman_cb_dqrr_stop, but consumes the current entry. */
1123         qman_cb_dqrr_consume_stop
1124 };
1125
1126 typedef enum qman_cb_dqrr_result (*qman_cb_dqrr)(struct qman_portal *qm,
1127                                         struct qman_fq *fq,
1128                                         const struct qm_dqrr_entry *dqrr);
1129
1130 typedef enum qman_cb_dqrr_result (*qman_dpdk_cb_dqrr)(void *event,
1131                                         struct qman_portal *qm,
1132                                         struct qman_fq *fq,
1133                                         const struct qm_dqrr_entry *dqrr,
1134                                         void **bd);
1135
1136 /* This callback type is used when handling buffers in dpdk pull mode */
1137 typedef void (*qman_dpdk_pull_cb_dqrr)(struct qman_fq **fq,
1138                                         struct qm_dqrr_entry **dqrr,
1139                                         void **bufs,
1140                                         int num_bufs);
1141
1142 typedef void (*qman_dpdk_cb_prepare)(struct qm_dqrr_entry *dq, void **bufs);
1143
1144 /*
1145  * This callback type is used when handling ERNs, FQRNs and FQRLs via MR. They
1146  * are always consumed after the callback returns.
1147  */
1148 typedef void (*qman_cb_mr)(struct qman_portal *qm, struct qman_fq *fq,
1149                                 const struct qm_mr_entry *msg);
1150
1151 /* This callback type is used when handling DCP ERNs */
1152 typedef void (*qman_cb_dc_ern)(struct qman_portal *qm,
1153                                 const struct qm_mr_entry *msg);
1154 /*
1155  * s/w-visible states. Ie. tentatively scheduled + truly scheduled + active +
1156  * held-active + held-suspended are just "sched". Things like "retired" will not
1157  * be assumed until it is complete (ie. QMAN_FQ_STATE_CHANGING is set until
1158  * then, to indicate it's completing and to gate attempts to retry the retire
1159  * command). Note, park commands do not set QMAN_FQ_STATE_CHANGING because it's
1160  * technically impossible in the case of enqueue DCAs (which refer to DQRR ring
1161  * index rather than the FQ that ring entry corresponds to), so repeated park
1162  * commands are allowed (if you're silly enough to try) but won't change FQ
1163  * state, and the resulting park notifications move FQs from "sched" to
1164  * "parked".
1165  */
1166 enum qman_fq_state {
1167         qman_fq_state_oos,
1168         qman_fq_state_parked,
1169         qman_fq_state_sched,
1170         qman_fq_state_retired
1171 };
1172
1173
1174 /*
1175  * Frame queue objects (struct qman_fq) are stored within memory passed to
1176  * qman_create_fq(), as this allows stashing of caller-provided demux callback
1177  * pointers at no extra cost to stashing of (driver-internal) FQ state. If the
1178  * caller wishes to add per-FQ state and have it benefit from dequeue-stashing,
1179  * they should;
1180  *
1181  * (a) extend the qman_fq structure with their state; eg.
1182  *
1183  *     // myfq is allocated and driver_fq callbacks filled in;
1184  *     struct my_fq {
1185  *         struct qman_fq base;
1186  *         int an_extra_field;
1187  *         [ ... add other fields to be associated with each FQ ...]
1188  *     } *myfq = some_my_fq_allocator();
1189  *     struct qman_fq *fq = qman_create_fq(fqid, flags, &myfq->base);
1190  *
1191  *     // in a dequeue callback, access extra fields from 'fq' via a cast;
1192  *     struct my_fq *myfq = (struct my_fq *)fq;
1193  *     do_something_with(myfq->an_extra_field);
1194  *     [...]
1195  *
1196  * (b) when and if configuring the FQ for context stashing, specify how ever
1197  *     many cachelines are required to stash 'struct my_fq', to accelerate not
1198  *     only the QMan driver but the callback as well.
1199  */
1200
1201 struct qman_fq_cb {
1202         union { /* for dequeued frames */
1203                 qman_dpdk_cb_dqrr dqrr_dpdk_cb;
1204                 qman_dpdk_pull_cb_dqrr dqrr_dpdk_pull_cb;
1205                 qman_cb_dqrr dqrr;
1206         };
1207         qman_dpdk_cb_prepare dqrr_prepare;
1208         qman_cb_mr ern;         /* for s/w ERNs */
1209         qman_cb_mr fqs;         /* frame-queue state changes*/
1210 };
1211
1212 struct qman_fq {
1213         /* Caller of qman_create_fq() provides these demux callbacks */
1214         struct qman_fq_cb cb;
1215
1216         u32 fqid_le;
1217         u16 ch_id;
1218         u8 cgr_groupid;
1219         u8 is_static;
1220
1221         /* DPDK Interface */
1222         void *dpaa_intf;
1223
1224         struct rte_event ev;
1225         /* affined portal in case of static queue */
1226         struct qman_portal *qp;
1227
1228         volatile unsigned long flags;
1229
1230         enum qman_fq_state state;
1231         u32 fqid;
1232         spinlock_t fqlock;
1233
1234         struct rb_node node;
1235 #ifdef CONFIG_FSL_QMAN_FQ_LOOKUP
1236         u32 key;
1237 #endif
1238 };
1239
1240 /*
1241  * This callback type is used when handling congestion group entry/exit.
1242  * 'congested' is non-zero on congestion-entry, and zero on congestion-exit.
1243  */
1244 typedef void (*qman_cb_cgr)(struct qman_portal *qm,
1245                             struct qman_cgr *cgr, int congested);
1246
1247 struct qman_cgr {
1248         /* Set these prior to qman_create_cgr() */
1249         u32 cgrid; /* 0..255, but u32 to allow specials like -1, 256, etc.*/
1250         qman_cb_cgr cb;
1251         /* These are private to the driver */
1252         u16 chan; /* portal channel this object is created on */
1253         struct list_head node;
1254 };
1255
1256 /* Flags to qman_create_fq() */
1257 #define QMAN_FQ_FLAG_NO_ENQUEUE      0x00000001 /* can't enqueue */
1258 #define QMAN_FQ_FLAG_NO_MODIFY       0x00000002 /* can only enqueue */
1259 #define QMAN_FQ_FLAG_TO_DCPORTAL     0x00000004 /* consumed by CAAM/PME/Fman */
1260 #define QMAN_FQ_FLAG_LOCKED          0x00000008 /* multi-core locking */
1261 #define QMAN_FQ_FLAG_AS_IS           0x00000010 /* query h/w state */
1262 #define QMAN_FQ_FLAG_DYNAMIC_FQID    0x00000020 /* (de)allocate fqid */
1263
1264 /* Flags to qman_destroy_fq() */
1265 #define QMAN_FQ_DESTROY_PARKED       0x00000001 /* FQ can be parked or OOS */
1266
1267 /* Flags from qman_fq_state() */
1268 #define QMAN_FQ_STATE_CHANGING       0x80000000 /* 'state' is changing */
1269 #define QMAN_FQ_STATE_NE             0x40000000 /* retired FQ isn't empty */
1270 #define QMAN_FQ_STATE_ORL            0x20000000 /* retired FQ has ORL */
1271 #define QMAN_FQ_STATE_BLOCKOOS       0xe0000000 /* if any are set, no OOS */
1272 #define QMAN_FQ_STATE_CGR_EN         0x10000000 /* CGR enabled */
1273 #define QMAN_FQ_STATE_VDQCR          0x08000000 /* being volatile dequeued */
1274
1275 /* Flags to qman_init_fq() */
1276 #define QMAN_INITFQ_FLAG_SCHED       0x00000001 /* schedule rather than park */
1277 #define QMAN_INITFQ_FLAG_LOCAL       0x00000004 /* set dest portal */
1278
1279 /* Flags to qman_enqueue(). NB, the strange numbering is to align with hardware,
1280  * bit-wise. (NB: the PME API is sensitive to these precise numberings too, so
1281  * any change here should be audited in PME.)
1282  */
1283 #define QMAN_ENQUEUE_FLAG_WATCH_CGR  0x00080000 /* watch congestion state */
1284 #define QMAN_ENQUEUE_FLAG_DCA        0x00008000 /* perform enqueue-DCA */
1285 #define QMAN_ENQUEUE_FLAG_DCA_PARK   0x00004000 /* If DCA, requests park */
1286 #define QMAN_ENQUEUE_FLAG_DCA_PTR(p)            /* If DCA, p is DQRR entry */ \
1287                 (((u32)(p) << 2) & 0x00000f00)
1288 #define QMAN_ENQUEUE_FLAG_C_GREEN    0x00000000 /* choose one C_*** flag */
1289 #define QMAN_ENQUEUE_FLAG_C_YELLOW   0x00000008
1290 #define QMAN_ENQUEUE_FLAG_C_RED      0x00000010
1291 #define QMAN_ENQUEUE_FLAG_C_OVERRIDE 0x00000018
1292 /* For the ORP-specific qman_enqueue_orp() variant;
1293  * - this flag indicates "Not Last In Sequence", ie. all but the final fragment
1294  *   of a frame.
1295  */
1296 #define QMAN_ENQUEUE_FLAG_NLIS       0x01000000
1297 /* - this flag performs no enqueue but fills in an ORP sequence number that
1298  *   would otherwise block it (eg. if a frame has been dropped).
1299  */
1300 #define QMAN_ENQUEUE_FLAG_HOLE       0x02000000
1301 /* - this flag performs no enqueue but advances NESN to the given sequence
1302  *   number.
1303  */
1304 #define QMAN_ENQUEUE_FLAG_NESN       0x04000000
1305
1306 /* Flags to qman_modify_cgr() */
1307 #define QMAN_CGR_FLAG_USE_INIT       0x00000001
1308 #define QMAN_CGR_MODE_FRAME          0x00000001
1309
1310 /**
1311  * qman_get_portal_index - get portal configuration index
1312  */
1313 int qman_get_portal_index(void);
1314
1315 u32 qman_portal_dequeue(struct rte_event ev[], unsigned int poll_limit,
1316                         void **bufs);
1317
1318 /**
1319  * qman_irqsource_add - add processing sources to be interrupt-driven
1320  * @bits: bitmask of QM_PIRQ_**I processing sources
1321  *
1322  * Adds processing sources that should be interrupt-driven (rather than
1323  * processed via qman_poll_***() functions). Returns zero for success, or
1324  * -EINVAL if the current CPU is sharing a portal hosted on another CPU.
1325  */
1326 int qman_irqsource_add(u32 bits);
1327
1328 /**
1329  * qman_irqsource_remove - remove processing sources from being interrupt-driven
1330  * @bits: bitmask of QM_PIRQ_**I processing sources
1331  *
1332  * Removes processing sources from being interrupt-driven, so that they will
1333  * instead be processed via qman_poll_***() functions. Returns zero for success,
1334  * or -EINVAL if the current CPU is sharing a portal hosted on another CPU.
1335  */
1336 int qman_irqsource_remove(u32 bits);
1337
1338 /**
1339  * qman_affine_channel - return the channel ID of an portal
1340  * @cpu: the cpu whose affine portal is the subject of the query
1341  *
1342  * If @cpu is -1, the affine portal for the current CPU will be used. It is a
1343  * bug to call this function for any value of @cpu (other than -1) that is not a
1344  * member of the cpu mask.
1345  */
1346 u16 qman_affine_channel(int cpu);
1347
1348 unsigned int qman_portal_poll_rx(unsigned int poll_limit,
1349                                  void **bufs, struct qman_portal *q);
1350
1351 /**
1352  * qman_set_vdq - Issue a volatile dequeue command
1353  * @fq: Frame Queue on which the volatile dequeue command is issued
1354  * @num: Number of Frames requested for volatile dequeue
1355  * @vdqcr_flags: QM_VDQCR_EXACT flag to for VDQCR command
1356  *
1357  * This function will issue a volatile dequeue command to the QMAN.
1358  */
1359 int qman_set_vdq(struct qman_fq *fq, u16 num, uint32_t vdqcr_flags);
1360
1361 /**
1362  * qman_dequeue - Get the DQRR entry after volatile dequeue command
1363  * @fq: Frame Queue on which the volatile dequeue command is issued
1364  *
1365  * This function will return the DQRR entry after a volatile dequeue command
1366  * is issued. It will keep returning NULL until there is no packet available on
1367  * the DQRR.
1368  */
1369 struct qm_dqrr_entry *qman_dequeue(struct qman_fq *fq);
1370
1371 /**
1372  * qman_dqrr_consume - Consume the DQRR entriy after volatile dequeue
1373  * @fq: Frame Queue on which the volatile dequeue command is issued
1374  * @dq: DQRR entry to consume. This is the one which is provided by the
1375  *    'qbman_dequeue' command.
1376  *
1377  * This will consume the DQRR enrey and make it available for next volatile
1378  * dequeue.
1379  */
1380 void qman_dqrr_consume(struct qman_fq *fq,
1381                        struct qm_dqrr_entry *dq);
1382
1383 /**
1384  * qman_poll_dqrr - process DQRR (fast-path) entries
1385  * @limit: the maximum number of DQRR entries to process
1386  *
1387  * Use of this function requires that DQRR processing not be interrupt-driven.
1388  * Ie. the value returned by qman_irqsource_get() should not include
1389  * QM_PIRQ_DQRI. If the current CPU is sharing a portal hosted on another CPU,
1390  * this function will return -EINVAL, otherwise the return value is >=0 and
1391  * represents the number of DQRR entries processed.
1392  */
1393 int qman_poll_dqrr(unsigned int limit);
1394
1395 /**
1396  * qman_poll
1397  *
1398  * Dispatcher logic on a cpu can use this to trigger any maintenance of the
1399  * affine portal. There are two classes of portal processing in question;
1400  * fast-path (which involves demuxing dequeue ring (DQRR) entries and tracking
1401  * enqueue ring (EQCR) consumption), and slow-path (which involves EQCR
1402  * thresholds, congestion state changes, etc). This function does whatever
1403  * processing is not triggered by interrupts.
1404  *
1405  * Note, if DQRR and some slow-path processing are poll-driven (rather than
1406  * interrupt-driven) then this function uses a heuristic to determine how often
1407  * to run slow-path processing - as slow-path processing introduces at least a
1408  * minimum latency each time it is run, whereas fast-path (DQRR) processing is
1409  * close to zero-cost if there is no work to be done.
1410  */
1411 void qman_poll(void);
1412
1413 /**
1414  * qman_stop_dequeues - Stop h/w dequeuing to the s/w portal
1415  *
1416  * Disables DQRR processing of the portal. This is reference-counted, so
1417  * qman_start_dequeues() must be called as many times as qman_stop_dequeues() to
1418  * truly re-enable dequeuing.
1419  */
1420 void qman_stop_dequeues(void);
1421
1422 /**
1423  * qman_start_dequeues - (Re)start h/w dequeuing to the s/w portal
1424  *
1425  * Enables DQRR processing of the portal. This is reference-counted, so
1426  * qman_start_dequeues() must be called as many times as qman_stop_dequeues() to
1427  * truly re-enable dequeuing.
1428  */
1429 void qman_start_dequeues(void);
1430
1431 /**
1432  * qman_static_dequeue_add - Add pool channels to the portal SDQCR
1433  * @pools: bit-mask of pool channels, using QM_SDQCR_CHANNELS_POOL(n)
1434  *
1435  * Adds a set of pool channels to the portal's static dequeue command register
1436  * (SDQCR). The requested pools are limited to those the portal has dequeue
1437  * access to.
1438  */
1439 void qman_static_dequeue_add(u32 pools, struct qman_portal *qm);
1440
1441 /**
1442  * qman_static_dequeue_del - Remove pool channels from the portal SDQCR
1443  * @pools: bit-mask of pool channels, using QM_SDQCR_CHANNELS_POOL(n)
1444  *
1445  * Removes a set of pool channels from the portal's static dequeue command
1446  * register (SDQCR). The requested pools are limited to those the portal has
1447  * dequeue access to.
1448  */
1449 void qman_static_dequeue_del(u32 pools, struct qman_portal *qp);
1450
1451 /**
1452  * qman_static_dequeue_get - return the portal's current SDQCR
1453  *
1454  * Returns the portal's current static dequeue command register (SDQCR). The
1455  * entire register is returned, so if only the currently-enabled pool channels
1456  * are desired, mask the return value with QM_SDQCR_CHANNELS_POOL_MASK.
1457  */
1458 u32 qman_static_dequeue_get(struct qman_portal *qp);
1459
1460 /**
1461  * qman_dca - Perform a Discrete Consumption Acknowledgment
1462  * @dq: the DQRR entry to be consumed
1463  * @park_request: indicates whether the held-active @fq should be parked
1464  *
1465  * Only allowed in DCA-mode portals, for DQRR entries whose handler callback had
1466  * previously returned 'qman_cb_dqrr_defer'. NB, as with the other APIs, this
1467  * does not take a 'portal' argument but implies the core affine portal from the
1468  * cpu that is currently executing the function. For reasons of locking, this
1469  * function must be called from the same CPU as that which processed the DQRR
1470  * entry in the first place.
1471  */
1472 void qman_dca(const struct qm_dqrr_entry *dq, int park_request);
1473
1474 /**
1475  * qman_dca_index - Perform a Discrete Consumption Acknowledgment
1476  * @index: the DQRR index to be consumed
1477  * @park_request: indicates whether the held-active @fq should be parked
1478  *
1479  * Only allowed in DCA-mode portals, for DQRR entries whose handler callback had
1480  * previously returned 'qman_cb_dqrr_defer'. NB, as with the other APIs, this
1481  * does not take a 'portal' argument but implies the core affine portal from the
1482  * cpu that is currently executing the function. For reasons of locking, this
1483  * function must be called from the same CPU as that which processed the DQRR
1484  * entry in the first place.
1485  */
1486 void qman_dca_index(u8 index, int park_request);
1487
1488 /**
1489  * qman_eqcr_is_empty - Determine if portal's EQCR is empty
1490  *
1491  * For use in situations where a cpu-affine caller needs to determine when all
1492  * enqueues for the local portal have been processed by Qman but can't use the
1493  * QMAN_ENQUEUE_FLAG_WAIT_SYNC flag to do this from the final qman_enqueue().
1494  * The function forces tracking of EQCR consumption (which normally doesn't
1495  * happen until enqueue processing needs to find space to put new enqueue
1496  * commands), and returns zero if the ring still has unprocessed entries,
1497  * non-zero if it is empty.
1498  */
1499 int qman_eqcr_is_empty(void);
1500
1501 /**
1502  * qman_set_dc_ern - Set the handler for DCP enqueue rejection notifications
1503  * @handler: callback for processing DCP ERNs
1504  * @affine: whether this handler is specific to the locally affine portal
1505  *
1506  * If a hardware block's interface to Qman (ie. its direct-connect portal, or
1507  * DCP) is configured not to receive enqueue rejections, then any enqueues
1508  * through that DCP that are rejected will be sent to a given software portal.
1509  * If @affine is non-zero, then this handler will only be used for DCP ERNs
1510  * received on the portal affine to the current CPU. If multiple CPUs share a
1511  * portal and they all call this function, they will be setting the handler for
1512  * the same portal! If @affine is zero, then this handler will be global to all
1513  * portals handled by this instance of the driver. Only those portals that do
1514  * not have their own affine handler will use the global handler.
1515  */
1516 void qman_set_dc_ern(qman_cb_dc_ern handler, int affine);
1517
1518         /* FQ management */
1519         /* ------------- */
1520 /**
1521  * qman_create_fq - Allocates a FQ
1522  * @fqid: the index of the FQD to encapsulate, must be "Out of Service"
1523  * @flags: bit-mask of QMAN_FQ_FLAG_*** options
1524  * @fq: memory for storing the 'fq', with callbacks filled in
1525  *
1526  * Creates a frame queue object for the given @fqid, unless the
1527  * QMAN_FQ_FLAG_DYNAMIC_FQID flag is set in @flags, in which case a FQID is
1528  * dynamically allocated (or the function fails if none are available). Once
1529  * created, the caller should not touch the memory at 'fq' except as extended to
1530  * adjacent memory for user-defined fields (see the definition of "struct
1531  * qman_fq" for more info). NO_MODIFY is only intended for enqueuing to
1532  * pre-existing frame-queues that aren't to be otherwise interfered with, it
1533  * prevents all other modifications to the frame queue. The TO_DCPORTAL flag
1534  * causes the driver to honour any contextB modifications requested in the
1535  * qm_init_fq() API, as this indicates the frame queue will be consumed by a
1536  * direct-connect portal (PME, CAAM, or Fman). When frame queues are consumed by
1537  * software portals, the contextB field is controlled by the driver and can't be
1538  * modified by the caller. If the AS_IS flag is specified, management commands
1539  * will be used on portal @p to query state for frame queue @fqid and construct
1540  * a frame queue object based on that, rather than assuming/requiring that it be
1541  * Out of Service.
1542  */
1543 int qman_create_fq(u32 fqid, u32 flags, struct qman_fq *fq);
1544
1545 /**
1546  * qman_destroy_fq - Deallocates a FQ
1547  * @fq: the frame queue object to release
1548  * @flags: bit-mask of QMAN_FQ_FREE_*** options
1549  *
1550  * The memory for this frame queue object ('fq' provided in qman_create_fq()) is
1551  * not deallocated but the caller regains ownership, to do with as desired. The
1552  * FQ must be in the 'out-of-service' state unless the QMAN_FQ_FREE_PARKED flag
1553  * is specified, in which case it may also be in the 'parked' state.
1554  */
1555 void qman_destroy_fq(struct qman_fq *fq, u32 flags);
1556
1557 /**
1558  * qman_fq_fqid - Queries the frame queue ID of a FQ object
1559  * @fq: the frame queue object to query
1560  */
1561 u32 qman_fq_fqid(struct qman_fq *fq);
1562
1563 /**
1564  * qman_fq_state - Queries the state of a FQ object
1565  * @fq: the frame queue object to query
1566  * @state: pointer to state enum to return the FQ scheduling state
1567  * @flags: pointer to state flags to receive QMAN_FQ_STATE_*** bitmask
1568  *
1569  * Queries the state of the FQ object, without performing any h/w commands.
1570  * This captures the state, as seen by the driver, at the time the function
1571  * executes.
1572  */
1573 void qman_fq_state(struct qman_fq *fq, enum qman_fq_state *state, u32 *flags);
1574
1575 /**
1576  * qman_init_fq - Initialises FQ fields, leaves the FQ "parked" or "scheduled"
1577  * @fq: the frame queue object to modify, must be 'parked' or new.
1578  * @flags: bit-mask of QMAN_INITFQ_FLAG_*** options
1579  * @opts: the FQ-modification settings, as defined in the low-level API
1580  *
1581  * The @opts parameter comes from the low-level portal API. Select
1582  * QMAN_INITFQ_FLAG_SCHED in @flags to cause the frame queue to be scheduled
1583  * rather than parked. NB, @opts can be NULL.
1584  *
1585  * Note that some fields and options within @opts may be ignored or overwritten
1586  * by the driver;
1587  * 1. the 'count' and 'fqid' fields are always ignored (this operation only
1588  * affects one frame queue: @fq).
1589  * 2. the QM_INITFQ_WE_CONTEXTB option of the 'we_mask' field and the associated
1590  * 'fqd' structure's 'context_b' field are sometimes overwritten;
1591  *   - if @fq was not created with QMAN_FQ_FLAG_TO_DCPORTAL, then context_b is
1592  *     initialised to a value used by the driver for demux.
1593  *   - if context_b is initialised for demux, so is context_a in case stashing
1594  *     is requested (see item 4).
1595  * (So caller control of context_b is only possible for TO_DCPORTAL frame queue
1596  * objects.)
1597  * 3. if @flags contains QMAN_INITFQ_FLAG_LOCAL, the 'fqd' structure's
1598  * 'dest::channel' field will be overwritten to match the portal used to issue
1599  * the command. If the WE_DESTWQ write-enable bit had already been set by the
1600  * caller, the channel workqueue will be left as-is, otherwise the write-enable
1601  * bit is set and the workqueue is set to a default of 4. If the "LOCAL" flag
1602  * isn't set, the destination channel/workqueue fields and the write-enable bit
1603  * are left as-is.
1604  * 4. if the driver overwrites context_a/b for demux, then if
1605  * QM_INITFQ_WE_CONTEXTA is set, the driver will only overwrite
1606  * context_a.address fields and will leave the stashing fields provided by the
1607  * user alone, otherwise it will zero out the context_a.stashing fields.
1608  */
1609 int qman_init_fq(struct qman_fq *fq, u32 flags, struct qm_mcc_initfq *opts);
1610
1611 /**
1612  * qman_schedule_fq - Schedules a FQ
1613  * @fq: the frame queue object to schedule, must be 'parked'
1614  *
1615  * Schedules the frame queue, which must be Parked, which takes it to
1616  * Tentatively-Scheduled or Truly-Scheduled depending on its fill-level.
1617  */
1618 int qman_schedule_fq(struct qman_fq *fq);
1619
1620 /**
1621  * qman_retire_fq - Retires a FQ
1622  * @fq: the frame queue object to retire
1623  * @flags: FQ flags (as per qman_fq_state) if retirement completes immediately
1624  *
1625  * Retires the frame queue. This returns zero if it succeeds immediately, +1 if
1626  * the retirement was started asynchronously, otherwise it returns negative for
1627  * failure. When this function returns zero, @flags is set to indicate whether
1628  * the retired FQ is empty and/or whether it has any ORL fragments (to show up
1629  * as ERNs). Otherwise the corresponding flags will be known when a subsequent
1630  * FQRN message shows up on the portal's message ring.
1631  *
1632  * NB, if the retirement is asynchronous (the FQ was in the Truly Scheduled or
1633  * Active state), the completion will be via the message ring as a FQRN - but
1634  * the corresponding callback may occur before this function returns!! Ie. the
1635  * caller should be prepared to accept the callback as the function is called,
1636  * not only once it has returned.
1637  */
1638 int qman_retire_fq(struct qman_fq *fq, u32 *flags);
1639
1640 /**
1641  * qman_oos_fq - Puts a FQ "out of service"
1642  * @fq: the frame queue object to be put out-of-service, must be 'retired'
1643  *
1644  * The frame queue must be retired and empty, and if any order restoration list
1645  * was released as ERNs at the time of retirement, they must all be consumed.
1646  */
1647 int qman_oos_fq(struct qman_fq *fq);
1648
1649 /**
1650  * qman_fq_flow_control - Set the XON/XOFF state of a FQ
1651  * @fq: the frame queue object to be set to XON/XOFF state, must not be 'oos',
1652  * or 'retired' or 'parked' state
1653  * @xon: boolean to set fq in XON or XOFF state
1654  *
1655  * The frame should be in Tentatively Scheduled state or Truly Schedule sate,
1656  * otherwise the IFSI interrupt will be asserted.
1657  */
1658 int qman_fq_flow_control(struct qman_fq *fq, int xon);
1659
1660 /**
1661  * qman_query_fq - Queries FQD fields (via h/w query command)
1662  * @fq: the frame queue object to be queried
1663  * @fqd: storage for the queried FQD fields
1664  */
1665 int qman_query_fq(struct qman_fq *fq, struct qm_fqd *fqd);
1666
1667 /**
1668  * qman_query_fq_has_pkts - Queries non-programmable FQD fields and returns '1'
1669  * if packets are in the frame queue. If there are no packets on frame
1670  * queue '0' is returned.
1671  * @fq: the frame queue object to be queried
1672  */
1673 int qman_query_fq_has_pkts(struct qman_fq *fq);
1674
1675 /**
1676  * qman_query_fq_np - Queries non-programmable FQD fields
1677  * @fq: the frame queue object to be queried
1678  * @np: storage for the queried FQD fields
1679  */
1680 int qman_query_fq_np(struct qman_fq *fq, struct qm_mcr_queryfq_np *np);
1681
1682 /**
1683  * qman_query_fq_frmcnt - Queries fq frame count
1684  * @fq: the frame queue object to be queried
1685  * @frm_cnt: number of frames in the queue
1686  */
1687 int qman_query_fq_frm_cnt(struct qman_fq *fq, u32 *frm_cnt);
1688
1689 /**
1690  * qman_query_wq - Queries work queue lengths
1691  * @query_dedicated: If non-zero, query length of WQs in the channel dedicated
1692  *              to this software portal. Otherwise, query length of WQs in a
1693  *              channel  specified in wq.
1694  * @wq: storage for the queried WQs lengths. Also specified the channel to
1695  *      to query if query_dedicated is zero.
1696  */
1697 int qman_query_wq(u8 query_dedicated, struct qm_mcr_querywq *wq);
1698
1699 /**
1700  * qman_volatile_dequeue - Issue a volatile dequeue command
1701  * @fq: the frame queue object to dequeue from
1702  * @flags: a bit-mask of QMAN_VOLATILE_FLAG_*** options
1703  * @vdqcr: bit mask of QM_VDQCR_*** options, as per qm_dqrr_vdqcr_set()
1704  *
1705  * Attempts to lock access to the portal's VDQCR volatile dequeue functionality.
1706  * The function will block and sleep if QMAN_VOLATILE_FLAG_WAIT is specified and
1707  * the VDQCR is already in use, otherwise returns non-zero for failure. If
1708  * QMAN_VOLATILE_FLAG_FINISH is specified, the function will only return once
1709  * the VDQCR command has finished executing (ie. once the callback for the last
1710  * DQRR entry resulting from the VDQCR command has been called). If not using
1711  * the FINISH flag, completion can be determined either by detecting the
1712  * presence of the QM_DQRR_STAT_UNSCHEDULED and QM_DQRR_STAT_DQCR_EXPIRED bits
1713  * in the "stat" field of the "struct qm_dqrr_entry" passed to the FQ's dequeue
1714  * callback, or by waiting for the QMAN_FQ_STATE_VDQCR bit to disappear from the
1715  * "flags" retrieved from qman_fq_state().
1716  */
1717 int qman_volatile_dequeue(struct qman_fq *fq, u32 flags, u32 vdqcr);
1718
1719 /**
1720  * qman_enqueue - Enqueue a frame to a frame queue
1721  * @fq: the frame queue object to enqueue to
1722  * @fd: a descriptor of the frame to be enqueued
1723  * @flags: bit-mask of QMAN_ENQUEUE_FLAG_*** options
1724  *
1725  * Fills an entry in the EQCR of portal @qm to enqueue the frame described by
1726  * @fd. The descriptor details are copied from @fd to the EQCR entry, the 'pid'
1727  * field is ignored. The return value is non-zero on error, such as ring full
1728  * (and FLAG_WAIT not specified), congestion avoidance (FLAG_WATCH_CGR
1729  * specified), etc. If the ring is full and FLAG_WAIT is specified, this
1730  * function will block. If FLAG_INTERRUPT is set, the EQCI bit of the portal
1731  * interrupt will assert when Qman consumes the EQCR entry (subject to "status
1732  * disable", "enable", and "inhibit" registers). If FLAG_DCA is set, Qman will
1733  * perform an implied "discrete consumption acknowledgment" on the dequeue
1734  * ring's (DQRR) entry, at the ring index specified by the FLAG_DCA_IDX(x)
1735  * macro. (As an alternative to issuing explicit DCA actions on DQRR entries,
1736  * this implicit DCA can delay the release of a "held active" frame queue
1737  * corresponding to a DQRR entry until Qman consumes the EQCR entry - providing
1738  * order-preservation semantics in packet-forwarding scenarios.) If FLAG_DCA is
1739  * set, then FLAG_DCA_PARK can also be set to imply that the DQRR consumption
1740  * acknowledgment should "park request" the "held active" frame queue. Ie.
1741  * when the portal eventually releases that frame queue, it will be left in the
1742  * Parked state rather than Tentatively Scheduled or Truly Scheduled. If the
1743  * portal is watching congestion groups, the QMAN_ENQUEUE_FLAG_WATCH_CGR flag
1744  * is requested, and the FQ is a member of a congestion group, then this
1745  * function returns -EAGAIN if the congestion group is currently congested.
1746  * Note, this does not eliminate ERNs, as the async interface means we can be
1747  * sending enqueue commands to an un-congested FQ that becomes congested before
1748  * the enqueue commands are processed, but it does minimise needless thrashing
1749  * of an already busy hardware resource by throttling many of the to-be-dropped
1750  * enqueues "at the source".
1751  */
1752 int qman_enqueue(struct qman_fq *fq, const struct qm_fd *fd, u32 flags);
1753
1754 int qman_enqueue_multi(struct qman_fq *fq, const struct qm_fd *fd, u32 *flags,
1755                        int frames_to_send);
1756
1757 /**
1758  * qman_enqueue_multi_fq - Enqueue multiple frames to their respective frame
1759  * queues.
1760  * @fq[]: Array of frame queue objects to enqueue to
1761  * @fd: pointer to first descriptor of frame to be enqueued
1762  * @frames_to_send: number of frames to be sent.
1763  *
1764  * This API is similar to qman_enqueue_multi(), but it takes fd which needs
1765  * to be processed by different frame queues.
1766  */
1767 int
1768 qman_enqueue_multi_fq(struct qman_fq *fq[], const struct qm_fd *fd,
1769                       int frames_to_send);
1770
1771 typedef int (*qman_cb_precommit) (void *arg);
1772
1773 /**
1774  * qman_enqueue_orp - Enqueue a frame to a frame queue using an ORP
1775  * @fq: the frame queue object to enqueue to
1776  * @fd: a descriptor of the frame to be enqueued
1777  * @flags: bit-mask of QMAN_ENQUEUE_FLAG_*** options
1778  * @orp: the frame queue object used as an order restoration point.
1779  * @orp_seqnum: the sequence number of this frame in the order restoration path
1780  *
1781  * Similar to qman_enqueue(), but with the addition of an Order Restoration
1782  * Point (@orp) and corresponding sequence number (@orp_seqnum) for this
1783  * enqueue operation to employ order restoration. Each frame queue object acts
1784  * as an Order Definition Point (ODP) by providing each frame dequeued from it
1785  * with an incrementing sequence number, this value is generally ignored unless
1786  * that sequence of dequeued frames will need order restoration later. Each
1787  * frame queue object also encapsulates an Order Restoration Point (ORP), which
1788  * is a re-assembly context for re-ordering frames relative to their sequence
1789  * numbers as they are enqueued. The ORP does not have to be within the frame
1790  * queue that receives the enqueued frame, in fact it is usually the frame
1791  * queue from which the frames were originally dequeued. For the purposes of
1792  * order restoration, multiple frames (or "fragments") can be enqueued for a
1793  * single sequence number by setting the QMAN_ENQUEUE_FLAG_NLIS flag for all
1794  * enqueues except the final fragment of a given sequence number. Ordering
1795  * between sequence numbers is guaranteed, even if fragments of different
1796  * sequence numbers are interlaced with one another. Fragments of the same
1797  * sequence number will retain the order in which they are enqueued. If no
1798  * enqueue is to performed, QMAN_ENQUEUE_FLAG_HOLE indicates that the given
1799  * sequence number is to be "skipped" by the ORP logic (eg. if a frame has been
1800  * dropped from a sequence), or QMAN_ENQUEUE_FLAG_NESN indicates that the given
1801  * sequence number should become the ORP's "Next Expected Sequence Number".
1802  *
1803  * Side note: a frame queue object can be used purely as an ORP, without
1804  * carrying any frames at all. Care should be taken not to deallocate a frame
1805  * queue object that is being actively used as an ORP, as a future allocation
1806  * of the frame queue object may start using the internal ORP before the
1807  * previous use has finished.
1808  */
1809 int qman_enqueue_orp(struct qman_fq *fq, const struct qm_fd *fd, u32 flags,
1810                      struct qman_fq *orp, u16 orp_seqnum);
1811
1812 /**
1813  * qman_alloc_fqid_range - Allocate a contiguous range of FQIDs
1814  * @result: is set by the API to the base FQID of the allocated range
1815  * @count: the number of FQIDs required
1816  * @align: required alignment of the allocated range
1817  * @partial: non-zero if the API can return fewer than @count FQIDs
1818  *
1819  * Returns the number of frame queues allocated, or a negative error code. If
1820  * @partial is non zero, the allocation request may return a smaller range of
1821  * FQs than requested (though alignment will be as requested). If @partial is
1822  * zero, the return value will either be 'count' or negative.
1823  */
1824 int qman_alloc_fqid_range(u32 *result, u32 count, u32 align, int partial);
1825 static inline int qman_alloc_fqid(u32 *result)
1826 {
1827         int ret = qman_alloc_fqid_range(result, 1, 0, 0);
1828
1829         return (ret > 0) ? 0 : ret;
1830 }
1831
1832 /**
1833  * qman_release_fqid_range - Release the specified range of frame queue IDs
1834  * @fqid: the base FQID of the range to deallocate
1835  * @count: the number of FQIDs in the range
1836  *
1837  * This function can also be used to seed the allocator with ranges of FQIDs
1838  * that it can subsequently allocate from.
1839  */
1840 void qman_release_fqid_range(u32 fqid, unsigned int count);
1841 static inline void qman_release_fqid(u32 fqid)
1842 {
1843         qman_release_fqid_range(fqid, 1);
1844 }
1845
1846 void qman_seed_fqid_range(u32 fqid, unsigned int count);
1847
1848 int qman_shutdown_fq(u32 fqid);
1849
1850 /**
1851  * qman_reserve_fqid_range - Reserve the specified range of frame queue IDs
1852  * @fqid: the base FQID of the range to deallocate
1853  * @count: the number of FQIDs in the range
1854  */
1855 int qman_reserve_fqid_range(u32 fqid, unsigned int count);
1856 static inline int qman_reserve_fqid(u32 fqid)
1857 {
1858         return qman_reserve_fqid_range(fqid, 1);
1859 }
1860
1861 /* Pool-channel management */
1862 /**
1863  * qman_alloc_pool_range - Allocate a contiguous range of pool-channel IDs
1864  * @result: is set by the API to the base pool-channel ID of the allocated range
1865  * @count: the number of pool-channel IDs required
1866  * @align: required alignment of the allocated range
1867  * @partial: non-zero if the API can return fewer than @count
1868  *
1869  * Returns the number of pool-channel IDs allocated, or a negative error code.
1870  * If @partial is non zero, the allocation request may return a smaller range of
1871  * than requested (though alignment will be as requested). If @partial is zero,
1872  * the return value will either be 'count' or negative.
1873  */
1874 int qman_alloc_pool_range(u32 *result, u32 count, u32 align, int partial);
1875 static inline int qman_alloc_pool(u32 *result)
1876 {
1877         int ret = qman_alloc_pool_range(result, 1, 0, 0);
1878
1879         return (ret > 0) ? 0 : ret;
1880 }
1881
1882 /**
1883  * qman_release_pool_range - Release the specified range of pool-channel IDs
1884  * @id: the base pool-channel ID of the range to deallocate
1885  * @count: the number of pool-channel IDs in the range
1886  */
1887 void qman_release_pool_range(u32 id, unsigned int count);
1888 static inline void qman_release_pool(u32 id)
1889 {
1890         qman_release_pool_range(id, 1);
1891 }
1892
1893 /**
1894  * qman_reserve_pool_range - Reserve the specified range of pool-channel IDs
1895  * @id: the base pool-channel ID of the range to reserve
1896  * @count: the number of pool-channel IDs in the range
1897  */
1898 int qman_reserve_pool_range(u32 id, unsigned int count);
1899 static inline int qman_reserve_pool(u32 id)
1900 {
1901         return qman_reserve_pool_range(id, 1);
1902 }
1903
1904 void qman_seed_pool_range(u32 id, unsigned int count);
1905
1906         /* CGR management */
1907         /* -------------- */
1908 /**
1909  * qman_create_cgr - Register a congestion group object
1910  * @cgr: the 'cgr' object, with fields filled in
1911  * @flags: QMAN_CGR_FLAG_* values
1912  * @opts: optional state of CGR settings
1913  *
1914  * Registers this object to receiving congestion entry/exit callbacks on the
1915  * portal affine to the cpu portal on which this API is executed. If opts is
1916  * NULL then only the callback (cgr->cb) function is registered. If @flags
1917  * contains QMAN_CGR_FLAG_USE_INIT, then an init hw command (which will reset
1918  * any unspecified parameters) will be used rather than a modify hw hardware
1919  * (which only modifies the specified parameters).
1920  */
1921 int qman_create_cgr(struct qman_cgr *cgr, u32 flags,
1922                     struct qm_mcc_initcgr *opts);
1923
1924 /**
1925  * qman_create_cgr_to_dcp - Register a congestion group object to DCP portal
1926  * @cgr: the 'cgr' object, with fields filled in
1927  * @flags: QMAN_CGR_FLAG_* values
1928  * @dcp_portal: the DCP portal to which the cgr object is registered.
1929  * @opts: optional state of CGR settings
1930  *
1931  */
1932 int qman_create_cgr_to_dcp(struct qman_cgr *cgr, u32 flags, u16 dcp_portal,
1933                            struct qm_mcc_initcgr *opts);
1934
1935 /**
1936  * qman_delete_cgr - Deregisters a congestion group object
1937  * @cgr: the 'cgr' object to deregister
1938  *
1939  * "Unplugs" this CGR object from the portal affine to the cpu on which this API
1940  * is executed. This must be excuted on the same affine portal on which it was
1941  * created.
1942  */
1943 int qman_delete_cgr(struct qman_cgr *cgr);
1944
1945 /**
1946  * qman_modify_cgr - Modify CGR fields
1947  * @cgr: the 'cgr' object to modify
1948  * @flags: QMAN_CGR_FLAG_* values
1949  * @opts: the CGR-modification settings
1950  *
1951  * The @opts parameter comes from the low-level portal API, and can be NULL.
1952  * Note that some fields and options within @opts may be ignored or overwritten
1953  * by the driver, in particular the 'cgrid' field is ignored (this operation
1954  * only affects the given CGR object). If @flags contains
1955  * QMAN_CGR_FLAG_USE_INIT, then an init hw command (which will reset any
1956  * unspecified parameters) will be used rather than a modify hw hardware (which
1957  * only modifies the specified parameters).
1958  */
1959 int qman_modify_cgr(struct qman_cgr *cgr, u32 flags,
1960                     struct qm_mcc_initcgr *opts);
1961
1962 /**
1963  * qman_query_cgr - Queries CGR fields
1964  * @cgr: the 'cgr' object to query
1965  * @result: storage for the queried congestion group record
1966  */
1967 int qman_query_cgr(struct qman_cgr *cgr, struct qm_mcr_querycgr *result);
1968
1969 /**
1970  * qman_query_congestion - Queries the state of all congestion groups
1971  * @congestion: storage for the queried state of all congestion groups
1972  */
1973 int qman_query_congestion(struct qm_mcr_querycongestion *congestion);
1974
1975 /**
1976  * qman_alloc_cgrid_range - Allocate a contiguous range of CGR IDs
1977  * @result: is set by the API to the base CGR ID of the allocated range
1978  * @count: the number of CGR IDs required
1979  * @align: required alignment of the allocated range
1980  * @partial: non-zero if the API can return fewer than @count
1981  *
1982  * Returns the number of CGR IDs allocated, or a negative error code.
1983  * If @partial is non zero, the allocation request may return a smaller range of
1984  * than requested (though alignment will be as requested). If @partial is zero,
1985  * the return value will either be 'count' or negative.
1986  */
1987 int qman_alloc_cgrid_range(u32 *result, u32 count, u32 align, int partial);
1988 static inline int qman_alloc_cgrid(u32 *result)
1989 {
1990         int ret = qman_alloc_cgrid_range(result, 1, 0, 0);
1991
1992         return (ret > 0) ? 0 : ret;
1993 }
1994
1995 /**
1996  * qman_release_cgrid_range - Release the specified range of CGR IDs
1997  * @id: the base CGR ID of the range to deallocate
1998  * @count: the number of CGR IDs in the range
1999  */
2000 void qman_release_cgrid_range(u32 id, unsigned int count);
2001 static inline void qman_release_cgrid(u32 id)
2002 {
2003         qman_release_cgrid_range(id, 1);
2004 }
2005
2006 /**
2007  * qman_reserve_cgrid_range - Reserve the specified range of CGR ID
2008  * @id: the base CGR ID of the range to reserve
2009  * @count: the number of CGR IDs in the range
2010  */
2011 int qman_reserve_cgrid_range(u32 id, unsigned int count);
2012 static inline int qman_reserve_cgrid(u32 id)
2013 {
2014         return qman_reserve_cgrid_range(id, 1);
2015 }
2016
2017 void qman_seed_cgrid_range(u32 id, unsigned int count);
2018
2019         /* Helpers */
2020         /* ------- */
2021 /**
2022  * qman_poll_fq_for_init - Check if an FQ has been initialised from OOS
2023  * @fqid: the FQID that will be initialised by other s/w
2024  *
2025  * In many situations, a FQID is provided for communication between s/w
2026  * entities, and whilst the consumer is responsible for initialising and
2027  * scheduling the FQ, the producer(s) generally create a wrapper FQ object using
2028  * and only call qman_enqueue() (no FQ initialisation, scheduling, etc). Ie;
2029  *     qman_create_fq(..., QMAN_FQ_FLAG_NO_MODIFY, ...);
2030  * However, data can not be enqueued to the FQ until it is initialised out of
2031  * the OOS state - this function polls for that condition. It is particularly
2032  * useful for users of IPC functions - each endpoint's Rx FQ is the other
2033  * endpoint's Tx FQ, so each side can initialise and schedule their Rx FQ object
2034  * and then use this API on the (NO_MODIFY) Tx FQ object in order to
2035  * synchronise. The function returns zero for success, +1 if the FQ is still in
2036  * the OOS state, or negative if there was an error.
2037  */
2038 static inline int qman_poll_fq_for_init(struct qman_fq *fq)
2039 {
2040         struct qm_mcr_queryfq_np np;
2041         int err;
2042
2043         err = qman_query_fq_np(fq, &np);
2044         if (err)
2045                 return err;
2046         if ((np.state & QM_MCR_NP_STATE_MASK) == QM_MCR_NP_STATE_OOS)
2047                 return 1;
2048         return 0;
2049 }
2050
2051 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
2052 #define cpu_to_hw_sg(x)
2053 #define hw_sg_to_cpu(x)
2054 #else
2055 #define cpu_to_hw_sg(x)  __cpu_to_hw_sg(x)
2056 #define hw_sg_to_cpu(x)  __hw_sg_to_cpu(x)
2057
2058 static inline void __cpu_to_hw_sg(struct qm_sg_entry *sgentry)
2059 {
2060         sgentry->opaque = cpu_to_be64(sgentry->opaque);
2061         sgentry->val = cpu_to_be32(sgentry->val);
2062         sgentry->val_off = cpu_to_be16(sgentry->val_off);
2063 }
2064
2065 static inline void __hw_sg_to_cpu(struct qm_sg_entry *sgentry)
2066 {
2067         sgentry->opaque = be64_to_cpu(sgentry->opaque);
2068         sgentry->val = be32_to_cpu(sgentry->val);
2069         sgentry->val_off = be16_to_cpu(sgentry->val_off);
2070 }
2071 #endif
2072
2073 #ifdef __cplusplus
2074 }
2075 #endif
2076
2077 #endif /* __FSL_QMAN_H */