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