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