7fc78cd1252a417be0133f3ff11c196c7ee412af
[dpdk.git] / drivers / bus / fslmc / qbman / qbman_portal.c
1 /*-
2  *   BSD LICENSE
3  *
4  * Copyright (C) 2014-2016 Freescale Semiconductor, Inc.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *     * Redistributions of source code must retain the above copyright
9  *       notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above copyright
11  *       notice, this list of conditions and the following disclaimer in the
12  *       documentation and/or other materials provided with the distribution.
13  *     * Neither the name of Freescale Semiconductor nor the
14  *       names of its contributors may be used to endorse or promote products
15  *       derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include "qbman_portal.h"
30
31 /* QBMan portal management command codes */
32 #define QBMAN_MC_ACQUIRE       0x30
33 #define QBMAN_WQCHAN_CONFIGURE 0x46
34
35 /* CINH register offsets */
36 #define QBMAN_CINH_SWP_EQCR_PI 0x800
37 #define QBMAN_CINH_SWP_EQCR_CI 0x840
38 #define QBMAN_CINH_SWP_EQAR    0x8c0
39 #define QBMAN_CINH_SWP_DQPI    0xa00
40 #define QBMAN_CINH_SWP_DCAP    0xac0
41 #define QBMAN_CINH_SWP_SDQCR   0xb00
42 #define QBMAN_CINH_SWP_RAR     0xcc0
43 #define QBMAN_CINH_SWP_ISR     0xe00
44 #define QBMAN_CINH_SWP_IER     0xe40
45 #define QBMAN_CINH_SWP_ISDR    0xe80
46 #define QBMAN_CINH_SWP_IIR     0xec0
47 #define QBMAN_CINH_SWP_DQRR_ITR    0xa80
48 #define QBMAN_CINH_SWP_ITPR    0xf40
49
50 /* CENA register offsets */
51 #define QBMAN_CENA_SWP_EQCR(n) (0x000 + ((uint32_t)(n) << 6))
52 #define QBMAN_CENA_SWP_DQRR(n) (0x200 + ((uint32_t)(n) << 6))
53 #define QBMAN_CENA_SWP_RCR(n)  (0x400 + ((uint32_t)(n) << 6))
54 #define QBMAN_CENA_SWP_CR      0x600
55 #define QBMAN_CENA_SWP_RR(vb)  (0x700 + ((uint32_t)(vb) >> 1))
56 #define QBMAN_CENA_SWP_VDQCR   0x780
57 #define QBMAN_CENA_SWP_EQCR_CI 0x840
58
59 /* Reverse mapping of QBMAN_CENA_SWP_DQRR() */
60 #define QBMAN_IDX_FROM_DQRR(p) (((unsigned long)p & 0x1ff) >> 6)
61
62 /* QBMan FQ management command codes */
63 #define QBMAN_FQ_SCHEDULE       0x48
64 #define QBMAN_FQ_FORCE          0x49
65 #define QBMAN_FQ_XON            0x4d
66 #define QBMAN_FQ_XOFF           0x4e
67
68 /*******************************/
69 /* Pre-defined attribute codes */
70 /*******************************/
71
72 struct qb_attr_code code_generic_verb = QB_CODE(0, 0, 7);
73 struct qb_attr_code code_generic_rslt = QB_CODE(0, 8, 8);
74
75 /*************************/
76 /* SDQCR attribute codes */
77 /*************************/
78
79 /* we put these here because at least some of them are required by
80  * qbman_swp_init()
81  */
82 struct qb_attr_code code_sdqcr_dct = QB_CODE(0, 24, 2);
83 struct qb_attr_code code_sdqcr_fc = QB_CODE(0, 29, 1);
84 struct qb_attr_code code_sdqcr_tok = QB_CODE(0, 16, 8);
85 static struct qb_attr_code code_eq_dca_idx;
86 #define CODE_SDQCR_DQSRC(n) QB_CODE(0, n, 1)
87 enum qbman_sdqcr_dct {
88         qbman_sdqcr_dct_null = 0,
89         qbman_sdqcr_dct_prio_ics,
90         qbman_sdqcr_dct_active_ics,
91         qbman_sdqcr_dct_active
92 };
93
94 enum qbman_sdqcr_fc {
95         qbman_sdqcr_fc_one = 0,
96         qbman_sdqcr_fc_up_to_3 = 1
97 };
98
99 struct qb_attr_code code_sdqcr_dqsrc = QB_CODE(0, 0, 16);
100
101 /* We need to keep track of which SWP triggered a pull command
102  * so keep an array of portal IDs and use the token field to
103  * be able to find the proper portal
104  */
105 #define MAX_QBMAN_PORTALS  35
106 static struct qbman_swp *portal_idx_map[MAX_QBMAN_PORTALS];
107
108 /*********************************/
109 /* Portal constructor/destructor */
110 /*********************************/
111
112 /* Software portals should always be in the power-on state when we initialise,
113  * due to the CCSR-based portal reset functionality that MC has.
114  *
115  * Erk! Turns out that QMan versions prior to 4.1 do not correctly reset DQRR
116  * valid-bits, so we need to support a workaround where we don't trust
117  * valid-bits when detecting new entries until any stale ring entries have been
118  * overwritten at least once. The idea is that we read PI for the first few
119  * entries, then switch to valid-bit after that. The trick is to clear the
120  * bug-work-around boolean once the PI wraps around the ring for the first time.
121  *
122  * Note: this still carries a slight additional cost once the decrementer hits
123  * zero.
124  */
125 struct qbman_swp *qbman_swp_init(const struct qbman_swp_desc *d)
126 {
127         int ret;
128         uint32_t eqcr_pi;
129         struct qbman_swp *p = kmalloc(sizeof(*p), GFP_KERNEL);
130
131         if (!p)
132                 return NULL;
133         p->desc = *d;
134 #ifdef QBMAN_CHECKING
135         p->mc.check = swp_mc_can_start;
136 #endif
137         p->mc.valid_bit = QB_VALID_BIT;
138         p->sdq = 0;
139         qb_attr_code_encode(&code_sdqcr_dct, &p->sdq, qbman_sdqcr_dct_prio_ics);
140         qb_attr_code_encode(&code_sdqcr_fc, &p->sdq, qbman_sdqcr_fc_up_to_3);
141         qb_attr_code_encode(&code_sdqcr_tok, &p->sdq, 0xbb);
142         atomic_set(&p->vdq.busy, 1);
143         p->vdq.valid_bit = QB_VALID_BIT;
144         p->dqrr.next_idx = 0;
145         p->dqrr.valid_bit = QB_VALID_BIT;
146         qman_version = p->desc.qman_version;
147         if ((qman_version & 0xFFFF0000) < QMAN_REV_4100) {
148                 p->dqrr.dqrr_size = 4;
149                 p->dqrr.reset_bug = 1;
150                 /* Set size of DQRR to 4, encoded in 2 bits */
151                 code_eq_dca_idx = (struct qb_attr_code)QB_CODE(0, 8, 2);
152         } else {
153                 p->dqrr.dqrr_size = 8;
154                 p->dqrr.reset_bug = 0;
155                 /* Set size of DQRR to 8, encoded in 3 bits */
156                 code_eq_dca_idx = (struct qb_attr_code)QB_CODE(0, 8, 3);
157         }
158
159         ret = qbman_swp_sys_init(&p->sys, d, p->dqrr.dqrr_size);
160         if (ret) {
161                 kfree(p);
162                 pr_err("qbman_swp_sys_init() failed %d\n", ret);
163                 return NULL;
164         }
165         /* SDQCR needs to be initialized to 0 when no channels are
166          * being dequeued from or else the QMan HW will indicate an
167          * error.  The values that were calculated above will be
168          * applied when dequeues from a specific channel are enabled
169          */
170         qbman_cinh_write(&p->sys, QBMAN_CINH_SWP_SDQCR, 0);
171         eqcr_pi = qbman_cinh_read(&p->sys, QBMAN_CINH_SWP_EQCR_PI);
172         p->eqcr.pi = eqcr_pi & 0xF;
173         p->eqcr.pi_vb = eqcr_pi & QB_VALID_BIT;
174         p->eqcr.ci = qbman_cinh_read(&p->sys, QBMAN_CINH_SWP_EQCR_CI) & 0xF;
175         p->eqcr.available = QBMAN_EQCR_SIZE - qm_cyc_diff(QBMAN_EQCR_SIZE,
176                                                 p->eqcr.ci, p->eqcr.pi);
177
178         portal_idx_map[p->desc.idx] = p;
179         return p;
180 }
181
182 void qbman_swp_finish(struct qbman_swp *p)
183 {
184 #ifdef QBMAN_CHECKING
185         QBMAN_BUG_ON(p->mc.check != swp_mc_can_start);
186 #endif
187         qbman_swp_sys_finish(&p->sys);
188         portal_idx_map[p->desc.idx] = NULL;
189         kfree(p);
190 }
191
192 const struct qbman_swp_desc *qbman_swp_get_desc(struct qbman_swp *p)
193 {
194         return &p->desc;
195 }
196
197 /**************/
198 /* Interrupts */
199 /**************/
200
201 uint32_t qbman_swp_interrupt_get_vanish(struct qbman_swp *p)
202 {
203         return qbman_cinh_read(&p->sys, QBMAN_CINH_SWP_ISDR);
204 }
205
206 void qbman_swp_interrupt_set_vanish(struct qbman_swp *p, uint32_t mask)
207 {
208         qbman_cinh_write(&p->sys, QBMAN_CINH_SWP_ISDR, mask);
209 }
210
211 uint32_t qbman_swp_interrupt_read_status(struct qbman_swp *p)
212 {
213         return qbman_cinh_read(&p->sys, QBMAN_CINH_SWP_ISR);
214 }
215
216 void qbman_swp_interrupt_clear_status(struct qbman_swp *p, uint32_t mask)
217 {
218         qbman_cinh_write(&p->sys, QBMAN_CINH_SWP_ISR, mask);
219 }
220
221 uint32_t qbman_swp_dqrr_thrshld_read_status(struct qbman_swp *p)
222 {
223         return qbman_cinh_read(&p->sys, QBMAN_CINH_SWP_DQRR_ITR);
224 }
225
226 void qbman_swp_dqrr_thrshld_write(struct qbman_swp *p, uint32_t mask)
227 {
228         qbman_cinh_write(&p->sys, QBMAN_CINH_SWP_DQRR_ITR, mask);
229 }
230
231 uint32_t qbman_swp_intr_timeout_read_status(struct qbman_swp *p)
232 {
233         return qbman_cinh_read(&p->sys, QBMAN_CINH_SWP_ITPR);
234 }
235
236 void qbman_swp_intr_timeout_write(struct qbman_swp *p, uint32_t mask)
237 {
238         qbman_cinh_write(&p->sys, QBMAN_CINH_SWP_ITPR, mask);
239 }
240
241 uint32_t qbman_swp_interrupt_get_trigger(struct qbman_swp *p)
242 {
243         return qbman_cinh_read(&p->sys, QBMAN_CINH_SWP_IER);
244 }
245
246 void qbman_swp_interrupt_set_trigger(struct qbman_swp *p, uint32_t mask)
247 {
248         qbman_cinh_write(&p->sys, QBMAN_CINH_SWP_IER, mask);
249 }
250
251 int qbman_swp_interrupt_get_inhibit(struct qbman_swp *p)
252 {
253         return qbman_cinh_read(&p->sys, QBMAN_CINH_SWP_IIR);
254 }
255
256 void qbman_swp_interrupt_set_inhibit(struct qbman_swp *p, int inhibit)
257 {
258         qbman_cinh_write(&p->sys, QBMAN_CINH_SWP_IIR, inhibit ? 0xffffffff : 0);
259 }
260
261 /***********************/
262 /* Management commands */
263 /***********************/
264
265 /*
266  * Internal code common to all types of management commands.
267  */
268
269 void *qbman_swp_mc_start(struct qbman_swp *p)
270 {
271         void *ret;
272 #ifdef QBMAN_CHECKING
273         QBMAN_BUG_ON(p->mc.check != swp_mc_can_start);
274 #endif
275         ret = qbman_cena_write_start(&p->sys, QBMAN_CENA_SWP_CR);
276 #ifdef QBMAN_CHECKING
277         if (!ret)
278                 p->mc.check = swp_mc_can_submit;
279 #endif
280         return ret;
281 }
282
283 void qbman_swp_mc_submit(struct qbman_swp *p, void *cmd, uint32_t cmd_verb)
284 {
285         uint32_t *v = cmd;
286 #ifdef QBMAN_CHECKING
287         QBMAN_BUG_ON(!(p->mc.check != swp_mc_can_submit));
288 #endif
289         /* TBD: "|=" is going to hurt performance. Need to move as many fields
290          * out of word zero, and for those that remain, the "OR" needs to occur
291          * at the caller side. This debug check helps to catch cases where the
292          * caller wants to OR but has forgotten to do so.
293          */
294         QBMAN_BUG_ON((*v & cmd_verb) != *v);
295         *v = cmd_verb | p->mc.valid_bit;
296         qbman_cena_write_complete(&p->sys, QBMAN_CENA_SWP_CR, cmd);
297 #ifdef QBMAN_CHECKING
298         p->mc.check = swp_mc_can_poll;
299 #endif
300 }
301
302 void *qbman_swp_mc_result(struct qbman_swp *p)
303 {
304         uint32_t *ret, verb;
305 #ifdef QBMAN_CHECKING
306         QBMAN_BUG_ON(p->mc.check != swp_mc_can_poll);
307 #endif
308         qbman_cena_invalidate_prefetch(&p->sys,
309                                        QBMAN_CENA_SWP_RR(p->mc.valid_bit));
310         ret = qbman_cena_read(&p->sys, QBMAN_CENA_SWP_RR(p->mc.valid_bit));
311         /* Remove the valid-bit - command completed if the rest is non-zero */
312         verb = ret[0] & ~QB_VALID_BIT;
313         if (!verb)
314                 return NULL;
315 #ifdef QBMAN_CHECKING
316         p->mc.check = swp_mc_can_start;
317 #endif
318         p->mc.valid_bit ^= QB_VALID_BIT;
319         return ret;
320 }
321
322 /***********/
323 /* Enqueue */
324 /***********/
325
326 /* These should be const, eventually */
327 static struct qb_attr_code code_eq_cmd = QB_CODE(0, 0, 2);
328 static struct qb_attr_code code_eq_eqdi = QB_CODE(0, 3, 1);
329 static struct qb_attr_code code_eq_dca_en = QB_CODE(0, 15, 1);
330 static struct qb_attr_code code_eq_dca_pk = QB_CODE(0, 14, 1);
331 /* Can't set code_eq_dca_idx width. Need qman version. Read at runtime */
332 static struct qb_attr_code code_eq_orp_en = QB_CODE(0, 2, 1);
333 static struct qb_attr_code code_eq_orp_is_nesn = QB_CODE(0, 31, 1);
334 static struct qb_attr_code code_eq_orp_nlis = QB_CODE(0, 30, 1);
335 static struct qb_attr_code code_eq_orp_seqnum = QB_CODE(0, 16, 14);
336 static struct qb_attr_code code_eq_opr_id = QB_CODE(1, 0, 16);
337 static struct qb_attr_code code_eq_tgt_id = QB_CODE(2, 0, 24);
338 /* static struct qb_attr_code code_eq_tag = QB_CODE(3, 0, 32); */
339 static struct qb_attr_code code_eq_qd_en = QB_CODE(0, 4, 1);
340 static struct qb_attr_code code_eq_qd_bin = QB_CODE(4, 0, 16);
341 static struct qb_attr_code code_eq_qd_pri = QB_CODE(4, 16, 4);
342 static struct qb_attr_code code_eq_rsp_stash = QB_CODE(5, 16, 1);
343 static struct qb_attr_code code_eq_rsp_id = QB_CODE(5, 24, 8);
344 static struct qb_attr_code code_eq_rsp_lo = QB_CODE(6, 0, 32);
345
346 enum qbman_eq_cmd_e {
347         /* No enqueue, primarily for plugging ORP gaps for dropped frames */
348         qbman_eq_cmd_empty,
349         /* DMA an enqueue response once complete */
350         qbman_eq_cmd_respond,
351         /* DMA an enqueue response only if the enqueue fails */
352         qbman_eq_cmd_respond_reject
353 };
354
355 void qbman_eq_desc_clear(struct qbman_eq_desc *d)
356 {
357         memset(d, 0, sizeof(*d));
358 }
359
360 void qbman_eq_desc_set_no_orp(struct qbman_eq_desc *d, int respond_success)
361 {
362         uint32_t *cl = qb_cl(d);
363
364         qb_attr_code_encode(&code_eq_orp_en, cl, 0);
365         qb_attr_code_encode(&code_eq_cmd, cl,
366                             respond_success ? qbman_eq_cmd_respond :
367                                               qbman_eq_cmd_respond_reject);
368 }
369
370 void qbman_eq_desc_set_orp(struct qbman_eq_desc *d, int respond_success,
371                            uint32_t opr_id, uint32_t seqnum, int incomplete)
372 {
373         uint32_t *cl = qb_cl(d);
374
375         qb_attr_code_encode(&code_eq_orp_en, cl, 1);
376         qb_attr_code_encode(&code_eq_cmd, cl,
377                             respond_success ? qbman_eq_cmd_respond :
378                                               qbman_eq_cmd_respond_reject);
379         qb_attr_code_encode(&code_eq_opr_id, cl, opr_id);
380         qb_attr_code_encode(&code_eq_orp_seqnum, cl, seqnum);
381         qb_attr_code_encode(&code_eq_orp_nlis, cl, !!incomplete);
382 }
383
384 void qbman_eq_desc_set_orp_hole(struct qbman_eq_desc *d, uint32_t opr_id,
385                                 uint32_t seqnum)
386 {
387         uint32_t *cl = qb_cl(d);
388
389         qb_attr_code_encode(&code_eq_orp_en, cl, 1);
390         qb_attr_code_encode(&code_eq_cmd, cl, qbman_eq_cmd_empty);
391         qb_attr_code_encode(&code_eq_opr_id, cl, opr_id);
392         qb_attr_code_encode(&code_eq_orp_seqnum, cl, seqnum);
393         qb_attr_code_encode(&code_eq_orp_nlis, cl, 0);
394         qb_attr_code_encode(&code_eq_orp_is_nesn, cl, 0);
395 }
396
397 void qbman_eq_desc_set_orp_nesn(struct qbman_eq_desc *d, uint32_t opr_id,
398                                 uint32_t seqnum)
399 {
400         uint32_t *cl = qb_cl(d);
401
402         qb_attr_code_encode(&code_eq_orp_en, cl, 1);
403         qb_attr_code_encode(&code_eq_cmd, cl, qbman_eq_cmd_empty);
404         qb_attr_code_encode(&code_eq_opr_id, cl, opr_id);
405         qb_attr_code_encode(&code_eq_orp_seqnum, cl, seqnum);
406         qb_attr_code_encode(&code_eq_orp_nlis, cl, 0);
407         qb_attr_code_encode(&code_eq_orp_is_nesn, cl, 1);
408 }
409
410 void qbman_eq_desc_set_response(struct qbman_eq_desc *d,
411                                 dma_addr_t storage_phys,
412                                 int stash)
413 {
414         uint32_t *cl = qb_cl(d);
415
416         qb_attr_code_encode_64(&code_eq_rsp_lo, (uint64_t *)cl, storage_phys);
417         qb_attr_code_encode(&code_eq_rsp_stash, cl, !!stash);
418 }
419
420 void qbman_eq_desc_set_token(struct qbman_eq_desc *d, uint8_t token)
421 {
422         uint32_t *cl = qb_cl(d);
423
424         qb_attr_code_encode(&code_eq_rsp_id, cl, (uint32_t)token);
425 }
426
427 void qbman_eq_desc_set_fq(struct qbman_eq_desc *d, uint32_t fqid)
428 {
429         uint32_t *cl = qb_cl(d);
430
431         qb_attr_code_encode(&code_eq_qd_en, cl, 0);
432         qb_attr_code_encode(&code_eq_tgt_id, cl, fqid);
433 }
434
435 void qbman_eq_desc_set_qd(struct qbman_eq_desc *d, uint32_t qdid,
436                           uint32_t qd_bin, uint32_t qd_prio)
437 {
438         uint32_t *cl = qb_cl(d);
439
440         qb_attr_code_encode(&code_eq_qd_en, cl, 1);
441         qb_attr_code_encode(&code_eq_tgt_id, cl, qdid);
442         qb_attr_code_encode(&code_eq_qd_bin, cl, qd_bin);
443         qb_attr_code_encode(&code_eq_qd_pri, cl, qd_prio);
444 }
445
446 void qbman_eq_desc_set_eqdi(struct qbman_eq_desc *d, int enable)
447 {
448         uint32_t *cl = qb_cl(d);
449
450         qb_attr_code_encode(&code_eq_eqdi, cl, !!enable);
451 }
452
453 void qbman_eq_desc_set_dca(struct qbman_eq_desc *d, int enable,
454                            uint32_t dqrr_idx, int park)
455 {
456         uint32_t *cl = qb_cl(d);
457
458         qb_attr_code_encode(&code_eq_dca_en, cl, !!enable);
459         if (enable) {
460                 qb_attr_code_encode(&code_eq_dca_pk, cl, !!park);
461                 qb_attr_code_encode(&code_eq_dca_idx, cl, dqrr_idx);
462         }
463 }
464
465 #define EQAR_IDX(eqar)     ((eqar) & 0x7)
466 #define EQAR_VB(eqar)      ((eqar) & 0x80)
467 #define EQAR_SUCCESS(eqar) ((eqar) & 0x100)
468 static int qbman_swp_enqueue_array_mode(struct qbman_swp *s,
469                                         const struct qbman_eq_desc *d,
470                                  const struct qbman_fd *fd)
471 {
472         uint32_t *p;
473         const uint32_t *cl = qb_cl(d);
474         uint32_t eqar = qbman_cinh_read(&s->sys, QBMAN_CINH_SWP_EQAR);
475
476         pr_debug("EQAR=%08x\n", eqar);
477         if (!EQAR_SUCCESS(eqar))
478                 return -EBUSY;
479         p = qbman_cena_write_start_wo_shadow(&s->sys,
480                         QBMAN_CENA_SWP_EQCR(EQAR_IDX(eqar)));
481         memcpy(&p[1], &cl[1], 28);
482         memcpy(&p[8], fd, sizeof(*fd));
483         /* Set the verb byte, have to substitute in the valid-bit */
484         lwsync();
485         p[0] = cl[0] | EQAR_VB(eqar);
486         qbman_cena_write_complete_wo_shadow(&s->sys,
487                         QBMAN_CENA_SWP_EQCR(EQAR_IDX(eqar)));
488         return 0;
489 }
490
491 static int qbman_swp_enqueue_ring_mode(struct qbman_swp *s,
492                                        const struct qbman_eq_desc *d,
493                                 const struct qbman_fd *fd)
494 {
495         uint32_t *p;
496         const uint32_t *cl = qb_cl(d);
497         uint32_t eqcr_ci;
498         uint8_t diff;
499
500         if (!s->eqcr.available) {
501                 eqcr_ci = s->eqcr.ci;
502                 s->eqcr.ci = qbman_cena_read_reg(&s->sys,
503                                 QBMAN_CENA_SWP_EQCR_CI) & 0xF;
504                 diff = qm_cyc_diff(QBMAN_EQCR_SIZE,
505                                    eqcr_ci, s->eqcr.ci);
506                 s->eqcr.available += diff;
507                 if (!diff)
508                         return -EBUSY;
509         }
510
511         p = qbman_cena_write_start_wo_shadow(&s->sys,
512                 QBMAN_CENA_SWP_EQCR(s->eqcr.pi & 7));
513         memcpy(&p[1], &cl[1], 28);
514         memcpy(&p[8], fd, sizeof(*fd));
515         lwsync();
516         /* Set the verb byte, have to substitute in the valid-bit */
517         p[0] = cl[0] | s->eqcr.pi_vb;
518         qbman_cena_write_complete_wo_shadow(&s->sys,
519                 QBMAN_CENA_SWP_EQCR(s->eqcr.pi & 7));
520         s->eqcr.pi++;
521         s->eqcr.pi &= 0xF;
522         s->eqcr.available--;
523         if (!(s->eqcr.pi & 7))
524                 s->eqcr.pi_vb ^= QB_VALID_BIT;
525         return 0;
526 }
527
528 int qbman_swp_enqueue(struct qbman_swp *s, const struct qbman_eq_desc *d,
529                       const struct qbman_fd *fd)
530 {
531         if (s->sys.eqcr_mode == qman_eqcr_vb_array)
532                 return qbman_swp_enqueue_array_mode(s, d, fd);
533         else    /* Use ring mode by default */
534                 return qbman_swp_enqueue_ring_mode(s, d, fd);
535 }
536
537 int qbman_swp_enqueue_multiple(struct qbman_swp *s,
538                                const struct qbman_eq_desc *d,
539                                const struct qbman_fd *fd,
540                                int num_frames)
541 {
542         uint32_t *p;
543         const uint32_t *cl = qb_cl(d);
544         uint32_t eqcr_ci, eqcr_pi;
545         uint8_t diff;
546         int i, num_enqueued = 0;
547         uint64_t addr_cena;
548
549         if (!s->eqcr.available) {
550                 eqcr_ci = s->eqcr.ci;
551                 s->eqcr.ci = qbman_cena_read_reg(&s->sys,
552                                 QBMAN_CENA_SWP_EQCR_CI) & 0xF;
553                 diff = qm_cyc_diff(QBMAN_EQCR_SIZE,
554                                    eqcr_ci, s->eqcr.ci);
555                 s->eqcr.available += diff;
556                 if (!diff)
557                         return 0;
558         }
559
560         eqcr_pi = s->eqcr.pi;
561         num_enqueued = (s->eqcr.available < num_frames) ?
562                         s->eqcr.available : num_frames;
563         s->eqcr.available -= num_enqueued;
564         /* Fill in the EQCR ring */
565         for (i = 0; i < num_enqueued; i++) {
566                 p = qbman_cena_write_start_wo_shadow(&s->sys,
567                                         QBMAN_CENA_SWP_EQCR(eqcr_pi & 7));
568                 memcpy(&p[1], &cl[1], 28);
569                 memcpy(&p[8], &fd[i], sizeof(*fd));
570                 eqcr_pi++;
571                 eqcr_pi &= 0xF;
572         }
573
574         lwsync();
575
576         /* Set the verb byte, have to substitute in the valid-bit */
577         eqcr_pi = s->eqcr.pi;
578         for (i = 0; i < num_enqueued; i++) {
579                 p = qbman_cena_write_start_wo_shadow(&s->sys,
580                                         QBMAN_CENA_SWP_EQCR(eqcr_pi & 7));
581                 p[0] = cl[0] | s->eqcr.pi_vb;
582                 eqcr_pi++;
583                 eqcr_pi &= 0xF;
584                 if (!(eqcr_pi & 7))
585                         s->eqcr.pi_vb ^= QB_VALID_BIT;
586         }
587
588         /* Flush all the cacheline without load/store in between */
589         eqcr_pi = s->eqcr.pi;
590         addr_cena = (uint64_t)s->sys.addr_cena;
591         for (i = 0; i < num_enqueued; i++) {
592                 dcbf((uint64_t *)(addr_cena +
593                                 QBMAN_CENA_SWP_EQCR(eqcr_pi & 7)));
594                 eqcr_pi++;
595                 eqcr_pi &= 0xF;
596         }
597         s->eqcr.pi = eqcr_pi;
598
599         return num_enqueued;
600 }
601
602 int qbman_swp_enqueue_multiple_desc(struct qbman_swp *s,
603                                     const struct qbman_eq_desc *d,
604                                     const struct qbman_fd *fd,
605                                     int num_frames)
606 {
607         uint32_t *p;
608         const uint32_t *cl;
609         uint32_t eqcr_ci, eqcr_pi;
610         uint8_t diff;
611         int i, num_enqueued = 0;
612         uint64_t addr_cena;
613
614         if (!s->eqcr.available) {
615                 eqcr_ci = s->eqcr.ci;
616                 s->eqcr.ci = qbman_cena_read_reg(&s->sys,
617                                 QBMAN_CENA_SWP_EQCR_CI) & 0xF;
618                 diff = qm_cyc_diff(QBMAN_EQCR_SIZE,
619                                    eqcr_ci, s->eqcr.ci);
620                 s->eqcr.available += diff;
621                 if (!diff)
622                         return 0;
623         }
624
625         eqcr_pi = s->eqcr.pi;
626         num_enqueued = (s->eqcr.available < num_frames) ?
627                         s->eqcr.available : num_frames;
628         s->eqcr.available -= num_enqueued;
629         /* Fill in the EQCR ring */
630         for (i = 0; i < num_enqueued; i++) {
631                 p = qbman_cena_write_start_wo_shadow(&s->sys,
632                                         QBMAN_CENA_SWP_EQCR(eqcr_pi & 7));
633                 cl = qb_cl(&d[i]);
634                 memcpy(&p[1], &cl[1], 28);
635                 memcpy(&p[8], &fd[i], sizeof(*fd));
636                 eqcr_pi++;
637                 eqcr_pi &= 0xF;
638         }
639
640         lwsync();
641
642         /* Set the verb byte, have to substitute in the valid-bit */
643         eqcr_pi = s->eqcr.pi;
644         for (i = 0; i < num_enqueued; i++) {
645                 p = qbman_cena_write_start_wo_shadow(&s->sys,
646                                         QBMAN_CENA_SWP_EQCR(eqcr_pi & 7));
647                 cl = qb_cl(&d[i]);
648                 p[0] = cl[0] | s->eqcr.pi_vb;
649                 eqcr_pi++;
650                 eqcr_pi &= 0xF;
651                 if (!(eqcr_pi & 7))
652                         s->eqcr.pi_vb ^= QB_VALID_BIT;
653         }
654
655         /* Flush all the cacheline without load/store in between */
656         eqcr_pi = s->eqcr.pi;
657         addr_cena = (uint64_t)s->sys.addr_cena;
658         for (i = 0; i < num_enqueued; i++) {
659                 dcbf((uint64_t *)(addr_cena +
660                                 QBMAN_CENA_SWP_EQCR(eqcr_pi & 7)));
661                 eqcr_pi++;
662                 eqcr_pi &= 0xF;
663         }
664         s->eqcr.pi = eqcr_pi;
665
666         return num_enqueued;
667 }
668
669 /*************************/
670 /* Static (push) dequeue */
671 /*************************/
672
673 void qbman_swp_push_get(struct qbman_swp *s, uint8_t channel_idx, int *enabled)
674 {
675         struct qb_attr_code code = CODE_SDQCR_DQSRC(channel_idx);
676
677         QBMAN_BUG_ON(channel_idx > 15);
678         *enabled = (int)qb_attr_code_decode(&code, &s->sdq);
679 }
680
681 void qbman_swp_push_set(struct qbman_swp *s, uint8_t channel_idx, int enable)
682 {
683         uint16_t dqsrc;
684         struct qb_attr_code code = CODE_SDQCR_DQSRC(channel_idx);
685
686         QBMAN_BUG_ON(channel_idx > 15);
687         qb_attr_code_encode(&code, &s->sdq, !!enable);
688         /* Read make the complete src map.  If no channels are enabled
689          * the SDQCR must be 0 or else QMan will assert errors
690          */
691         dqsrc = (uint16_t)qb_attr_code_decode(&code_sdqcr_dqsrc, &s->sdq);
692         if (dqsrc != 0)
693                 qbman_cinh_write(&s->sys, QBMAN_CINH_SWP_SDQCR, s->sdq);
694         else
695                 qbman_cinh_write(&s->sys, QBMAN_CINH_SWP_SDQCR, 0);
696 }
697
698 /***************************/
699 /* Volatile (pull) dequeue */
700 /***************************/
701
702 /* These should be const, eventually */
703 static struct qb_attr_code code_pull_dct = QB_CODE(0, 0, 2);
704 static struct qb_attr_code code_pull_dt = QB_CODE(0, 2, 2);
705 static struct qb_attr_code code_pull_rls = QB_CODE(0, 4, 1);
706 static struct qb_attr_code code_pull_stash = QB_CODE(0, 5, 1);
707 static struct qb_attr_code code_pull_numframes = QB_CODE(0, 8, 5);
708 static struct qb_attr_code code_pull_token = QB_CODE(0, 16, 8);
709 static struct qb_attr_code code_pull_dqsource = QB_CODE(1, 0, 24);
710 static struct qb_attr_code code_pull_rsp_lo = QB_CODE(2, 0, 32);
711
712 enum qb_pull_dt_e {
713         qb_pull_dt_channel,
714         qb_pull_dt_workqueue,
715         qb_pull_dt_framequeue
716 };
717
718 void qbman_pull_desc_clear(struct qbman_pull_desc *d)
719 {
720         memset(d, 0, sizeof(*d));
721 }
722
723 void qbman_pull_desc_set_storage(struct qbman_pull_desc *d,
724                                  struct qbman_result *storage,
725                                  dma_addr_t storage_phys,
726                                  int stash)
727 {
728         uint32_t *cl = qb_cl(d);
729         /* Squiggle the pointer 'storage' into the extra 2 words of the
730          * descriptor (which aren't copied to the hw command)
731          */
732         *(void **)&cl[4] = storage;
733         if (!storage) {
734                 qb_attr_code_encode(&code_pull_rls, cl, 0);
735                 return;
736         }
737         qb_attr_code_encode(&code_pull_rls, cl, 1);
738         qb_attr_code_encode(&code_pull_stash, cl, !!stash);
739         qb_attr_code_encode_64(&code_pull_rsp_lo, (uint64_t *)cl, storage_phys);
740 }
741
742 void qbman_pull_desc_set_numframes(struct qbman_pull_desc *d, uint8_t numframes)
743 {
744         uint32_t *cl = qb_cl(d);
745
746         qb_attr_code_encode(&code_pull_numframes, cl,
747                             (uint32_t)(numframes - 1));
748 }
749
750 void qbman_pull_desc_set_token(struct qbman_pull_desc *d, uint8_t token)
751 {
752         uint32_t *cl = qb_cl(d);
753
754         qb_attr_code_encode(&code_pull_token, cl, token);
755 }
756
757 void qbman_pull_desc_set_fq(struct qbman_pull_desc *d, uint32_t fqid)
758 {
759         uint32_t *cl = qb_cl(d);
760
761         qb_attr_code_encode(&code_pull_dct, cl, 1);
762         qb_attr_code_encode(&code_pull_dt, cl, qb_pull_dt_framequeue);
763         qb_attr_code_encode(&code_pull_dqsource, cl, fqid);
764 }
765
766 void qbman_pull_desc_set_wq(struct qbman_pull_desc *d, uint32_t wqid,
767                             enum qbman_pull_type_e dct)
768 {
769         uint32_t *cl = qb_cl(d);
770
771         qb_attr_code_encode(&code_pull_dct, cl, dct);
772         qb_attr_code_encode(&code_pull_dt, cl, qb_pull_dt_workqueue);
773         qb_attr_code_encode(&code_pull_dqsource, cl, wqid);
774 }
775
776 void qbman_pull_desc_set_channel(struct qbman_pull_desc *d, uint32_t chid,
777                                  enum qbman_pull_type_e dct)
778 {
779         uint32_t *cl = qb_cl(d);
780
781         qb_attr_code_encode(&code_pull_dct, cl, dct);
782         qb_attr_code_encode(&code_pull_dt, cl, qb_pull_dt_channel);
783         qb_attr_code_encode(&code_pull_dqsource, cl, chid);
784 }
785
786 int qbman_swp_pull(struct qbman_swp *s, struct qbman_pull_desc *d)
787 {
788         uint32_t *p;
789         uint32_t *cl = qb_cl(d);
790
791         if (!atomic_dec_and_test(&s->vdq.busy)) {
792                 atomic_inc(&s->vdq.busy);
793                 return -EBUSY;
794         }
795         s->vdq.storage = *(void **)&cl[4];
796         /* We use portal index +1 as token so that 0 still indicates
797          * that the result isn't valid yet.
798          */
799         qb_attr_code_encode(&code_pull_token, cl, s->desc.idx + 1);
800         p = qbman_cena_write_start_wo_shadow(&s->sys, QBMAN_CENA_SWP_VDQCR);
801         memcpy(&p[1], &cl[1], 12);
802         /* Set the verb byte, have to substitute in the valid-bit */
803         lwsync();
804         p[0] = cl[0] | s->vdq.valid_bit;
805         s->vdq.valid_bit ^= QB_VALID_BIT;
806         qbman_cena_write_complete_wo_shadow(&s->sys, QBMAN_CENA_SWP_VDQCR);
807         return 0;
808 }
809
810 /****************/
811 /* Polling DQRR */
812 /****************/
813
814 static struct qb_attr_code code_dqrr_verb = QB_CODE(0, 0, 8);
815 static struct qb_attr_code code_dqrr_response = QB_CODE(0, 0, 7);
816 static struct qb_attr_code code_dqrr_stat = QB_CODE(0, 8, 8);
817 static struct qb_attr_code code_dqrr_seqnum = QB_CODE(0, 16, 14);
818 static struct qb_attr_code code_dqrr_odpid = QB_CODE(1, 0, 16);
819 /* static struct qb_attr_code code_dqrr_tok = QB_CODE(1, 24, 8); */
820 static struct qb_attr_code code_dqrr_fqid = QB_CODE(2, 0, 24);
821 static struct qb_attr_code code_dqrr_byte_count = QB_CODE(4, 0, 32);
822 static struct qb_attr_code code_dqrr_frame_count = QB_CODE(5, 0, 24);
823 static struct qb_attr_code code_dqrr_ctx_lo = QB_CODE(6, 0, 32);
824
825 #define QBMAN_RESULT_DQ        0x60
826 #define QBMAN_RESULT_FQRN      0x21
827 #define QBMAN_RESULT_FQRNI     0x22
828 #define QBMAN_RESULT_FQPN      0x24
829 #define QBMAN_RESULT_FQDAN     0x25
830 #define QBMAN_RESULT_CDAN      0x26
831 #define QBMAN_RESULT_CSCN_MEM  0x27
832 #define QBMAN_RESULT_CGCU      0x28
833 #define QBMAN_RESULT_BPSCN     0x29
834 #define QBMAN_RESULT_CSCN_WQ   0x2a
835
836 static struct qb_attr_code code_dqpi_pi = QB_CODE(0, 0, 4);
837
838 /* NULL return if there are no unconsumed DQRR entries. Returns a DQRR entry
839  * only once, so repeated calls can return a sequence of DQRR entries, without
840  * requiring they be consumed immediately or in any particular order.
841  */
842 const struct qbman_result *qbman_swp_dqrr_next(struct qbman_swp *s)
843 {
844         uint32_t verb;
845         uint32_t response_verb;
846         uint32_t flags;
847         const struct qbman_result *dq;
848         const uint32_t *p;
849
850         /* Before using valid-bit to detect if something is there, we have to
851          * handle the case of the DQRR reset bug...
852          */
853         if (unlikely(s->dqrr.reset_bug)) {
854                 /* We pick up new entries by cache-inhibited producer index,
855                  * which means that a non-coherent mapping would require us to
856                  * invalidate and read *only* once that PI has indicated that
857                  * there's an entry here. The first trip around the DQRR ring
858                  * will be much less efficient than all subsequent trips around
859                  * it...
860                  */
861                 uint32_t dqpi = qbman_cinh_read(&s->sys, QBMAN_CINH_SWP_DQPI);
862                 uint32_t pi = qb_attr_code_decode(&code_dqpi_pi, &dqpi);
863                 /* there are new entries if pi != next_idx */
864                 if (pi == s->dqrr.next_idx)
865                         return NULL;
866                 /* if next_idx is/was the last ring index, and 'pi' is
867                  * different, we can disable the workaround as all the ring
868                  * entries have now been DMA'd to so valid-bit checking is
869                  * repaired. Note: this logic needs to be based on next_idx
870                  * (which increments one at a time), rather than on pi (which
871                  * can burst and wrap-around between our snapshots of it).
872                  */
873                 QBMAN_BUG_ON((s->dqrr.dqrr_size - 1) < 0);
874                 if (s->dqrr.next_idx == (s->dqrr.dqrr_size - 1u)) {
875                         pr_debug("DEBUG: next_idx=%d, pi=%d, clear reset bug\n",
876                                  s->dqrr.next_idx, pi);
877                         s->dqrr.reset_bug = 0;
878                 }
879                 qbman_cena_invalidate_prefetch(&s->sys,
880                                 QBMAN_CENA_SWP_DQRR(s->dqrr.next_idx));
881         }
882         dq = qbman_cena_read_wo_shadow(&s->sys,
883                                        QBMAN_CENA_SWP_DQRR(s->dqrr.next_idx));
884         p = qb_cl(dq);
885         verb = qb_attr_code_decode(&code_dqrr_verb, p);
886         /* If the valid-bit isn't of the expected polarity, nothing there. Note,
887          * in the DQRR reset bug workaround, we shouldn't need to skip these
888          * check, because we've already determined that a new entry is available
889          * and we've invalidated the cacheline before reading it, so the
890          * valid-bit behaviour is repaired and should tell us what we already
891          * knew from reading PI.
892          */
893         if ((verb & QB_VALID_BIT) != s->dqrr.valid_bit)
894                 return NULL;
895
896         /* There's something there. Move "next_idx" attention to the next ring
897          * entry (and prefetch it) before returning what we found.
898          */
899         s->dqrr.next_idx++;
900         if (s->dqrr.next_idx == s->dqrr.dqrr_size) {
901                 s->dqrr.next_idx = 0;
902                 s->dqrr.valid_bit ^= QB_VALID_BIT;
903         }
904         /* If this is the final response to a volatile dequeue command
905          * indicate that the vdq is no longer busy.
906          */
907         flags = qbman_result_DQ_flags(dq);
908         response_verb = qb_attr_code_decode(&code_dqrr_response, &verb);
909         if ((response_verb == QBMAN_RESULT_DQ) &&
910             (flags & QBMAN_DQ_STAT_VOLATILE) &&
911             (flags & QBMAN_DQ_STAT_EXPIRED))
912                 atomic_inc(&s->vdq.busy);
913
914         return dq;
915 }
916
917 /* Consume DQRR entries previously returned from qbman_swp_dqrr_next(). */
918 void qbman_swp_dqrr_consume(struct qbman_swp *s,
919                             const struct qbman_result *dq)
920 {
921         qbman_cinh_write(&s->sys, QBMAN_CINH_SWP_DCAP, QBMAN_IDX_FROM_DQRR(dq));
922 }
923
924 /*********************************/
925 /* Polling user-provided storage */
926 /*********************************/
927
928 int qbman_result_has_new_result(__attribute__((unused)) struct qbman_swp *s,
929                                 const struct qbman_result *dq)
930 {
931         /* To avoid converting the little-endian DQ entry to host-endian prior
932          * to us knowing whether there is a valid entry or not (and run the
933          * risk of corrupting the incoming hardware LE write), we detect in
934          * hardware endianness rather than host. This means we need a different
935          * "code" depending on whether we are BE or LE in software, which is
936          * where DQRR_TOK_OFFSET comes in...
937          */
938         static struct qb_attr_code code_dqrr_tok_detect =
939                                         QB_CODE(0, DQRR_TOK_OFFSET, 8);
940         /* The user trying to poll for a result treats "dq" as const. It is
941          * however the same address that was provided to us non-const in the
942          * first place, for directing hardware DMA to. So we can cast away the
943          * const because it is mutable from our perspective.
944          */
945         uint32_t *p = (uint32_t *)(unsigned long)qb_cl(dq);
946         uint32_t token;
947
948         token = qb_attr_code_decode(&code_dqrr_tok_detect, &p[1]);
949         if (token == 0)
950                 return 0;
951         /* Entry is valid - overwrite token back to 0 so
952          * a) If this memory is reused tokesn will be 0
953          * b) If someone calls "has_new_result()" again on this entry it
954          *    will not appear to be new
955          */
956         qb_attr_code_encode(&code_dqrr_tok_detect, &p[1], 0);
957
958         /* Only now do we convert from hardware to host endianness. Also, as we
959          * are returning success, the user has promised not to call us again, so
960          * there's no risk of us converting the endianness twice...
961          */
962         make_le32_n(p, 16);
963         return 1;
964 }
965
966 int qbman_check_command_complete(struct qbman_swp *s,
967                                  const struct qbman_result *dq)
968 {
969         /* To avoid converting the little-endian DQ entry to host-endian prior
970          * to us knowing whether there is a valid entry or not (and run the
971          * risk of corrupting the incoming hardware LE write), we detect in
972          * hardware endianness rather than host. This means we need a different
973          * "code" depending on whether we are BE or LE in software, which is
974          * where DQRR_TOK_OFFSET comes in...
975          */
976         static struct qb_attr_code code_dqrr_tok_detect =
977                                         QB_CODE(0, DQRR_TOK_OFFSET, 8);
978         /* The user trying to poll for a result treats "dq" as const. It is
979          * however the same address that was provided to us non-const in the
980          * first place, for directing hardware DMA to. So we can cast away the
981          * const because it is mutable from our perspective.
982          */
983         uint32_t *p = (uint32_t *)(unsigned long)qb_cl(dq);
984         uint32_t token;
985
986         token = qb_attr_code_decode(&code_dqrr_tok_detect, &p[1]);
987         if (token == 0)
988                 return 0;
989         /* TODO: Remove qbman_swp from parameters and make it a local
990          * once we've tested the reserve portal map change
991          */
992         s = portal_idx_map[token - 1];
993         /* When token is set it indicates that VDQ command has been fetched
994          * by qbman and is working on it. It is safe for software to issue
995          * another VDQ command, so incrementing the busy variable.
996          */
997         if (s->vdq.storage == dq) {
998                 s->vdq.storage = NULL;
999                 atomic_inc(&s->vdq.busy);
1000         }
1001         return 1;
1002 }
1003
1004 /********************************/
1005 /* Categorising qbman results   */
1006 /********************************/
1007
1008 static struct qb_attr_code code_result_in_mem =
1009                         QB_CODE(0, QBMAN_RESULT_VERB_OFFSET_IN_MEM, 7);
1010
1011 static inline int __qbman_result_is_x(const struct qbman_result *dq,
1012                                       uint32_t x)
1013 {
1014         const uint32_t *p = qb_cl(dq);
1015         uint32_t response_verb = qb_attr_code_decode(&code_dqrr_response, p);
1016
1017         return (response_verb == x);
1018 }
1019
1020 static inline int __qbman_result_is_x_in_mem(const struct qbman_result *dq,
1021                                              uint32_t x)
1022 {
1023         const uint32_t *p = qb_cl(dq);
1024         uint32_t response_verb = qb_attr_code_decode(&code_result_in_mem, p);
1025
1026         return (response_verb == x);
1027 }
1028
1029 int qbman_result_is_DQ(const struct qbman_result *dq)
1030 {
1031         return __qbman_result_is_x(dq, QBMAN_RESULT_DQ);
1032 }
1033
1034 int qbman_result_is_FQDAN(const struct qbman_result *dq)
1035 {
1036         return __qbman_result_is_x(dq, QBMAN_RESULT_FQDAN);
1037 }
1038
1039 int qbman_result_is_CDAN(const struct qbman_result *dq)
1040 {
1041         return __qbman_result_is_x(dq, QBMAN_RESULT_CDAN);
1042 }
1043
1044 int qbman_result_is_CSCN(const struct qbman_result *dq)
1045 {
1046         return __qbman_result_is_x_in_mem(dq, QBMAN_RESULT_CSCN_MEM) ||
1047                 __qbman_result_is_x(dq, QBMAN_RESULT_CSCN_WQ);
1048 }
1049
1050 int qbman_result_is_BPSCN(const struct qbman_result *dq)
1051 {
1052         return __qbman_result_is_x_in_mem(dq, QBMAN_RESULT_BPSCN);
1053 }
1054
1055 int qbman_result_is_CGCU(const struct qbman_result *dq)
1056 {
1057         return __qbman_result_is_x_in_mem(dq, QBMAN_RESULT_CGCU);
1058 }
1059
1060 int qbman_result_is_FQRN(const struct qbman_result *dq)
1061 {
1062         return __qbman_result_is_x_in_mem(dq, QBMAN_RESULT_FQRN);
1063 }
1064
1065 int qbman_result_is_FQRNI(const struct qbman_result *dq)
1066 {
1067         return __qbman_result_is_x_in_mem(dq, QBMAN_RESULT_FQRNI);
1068 }
1069
1070 int qbman_result_is_FQPN(const struct qbman_result *dq)
1071 {
1072         return __qbman_result_is_x(dq, QBMAN_RESULT_FQPN);
1073 }
1074
1075 /*********************************/
1076 /* Parsing frame dequeue results */
1077 /*********************************/
1078
1079 /* These APIs assume qbman_result_is_DQ() is TRUE */
1080
1081 uint32_t qbman_result_DQ_flags(const struct qbman_result *dq)
1082 {
1083         const uint32_t *p = qb_cl(dq);
1084
1085         return qb_attr_code_decode(&code_dqrr_stat, p);
1086 }
1087
1088 uint16_t qbman_result_DQ_seqnum(const struct qbman_result *dq)
1089 {
1090         const uint32_t *p = qb_cl(dq);
1091
1092         return (uint16_t)qb_attr_code_decode(&code_dqrr_seqnum, p);
1093 }
1094
1095 uint16_t qbman_result_DQ_odpid(const struct qbman_result *dq)
1096 {
1097         const uint32_t *p = qb_cl(dq);
1098
1099         return (uint16_t)qb_attr_code_decode(&code_dqrr_odpid, p);
1100 }
1101
1102 uint32_t qbman_result_DQ_fqid(const struct qbman_result *dq)
1103 {
1104         const uint32_t *p = qb_cl(dq);
1105
1106         return qb_attr_code_decode(&code_dqrr_fqid, p);
1107 }
1108
1109 uint32_t qbman_result_DQ_byte_count(const struct qbman_result *dq)
1110 {
1111         const uint32_t *p = qb_cl(dq);
1112
1113         return qb_attr_code_decode(&code_dqrr_byte_count, p);
1114 }
1115
1116 uint32_t qbman_result_DQ_frame_count(const struct qbman_result *dq)
1117 {
1118         const uint32_t *p = qb_cl(dq);
1119
1120         return qb_attr_code_decode(&code_dqrr_frame_count, p);
1121 }
1122
1123 uint64_t qbman_result_DQ_fqd_ctx(const struct qbman_result *dq)
1124 {
1125         const uint64_t *p = (const uint64_t *)qb_cl(dq);
1126
1127         return qb_attr_code_decode_64(&code_dqrr_ctx_lo, p);
1128 }
1129
1130 const struct qbman_fd *qbman_result_DQ_fd(const struct qbman_result *dq)
1131 {
1132         const uint32_t *p = qb_cl(dq);
1133
1134         return (const struct qbman_fd *)&p[8];
1135 }
1136
1137 /**************************************/
1138 /* Parsing state-change notifications */
1139 /**************************************/
1140
1141 static struct qb_attr_code code_scn_state = QB_CODE(0, 16, 8);
1142 static struct qb_attr_code code_scn_rid = QB_CODE(1, 0, 24);
1143 static struct qb_attr_code code_scn_state_in_mem =
1144                         QB_CODE(0, SCN_STATE_OFFSET_IN_MEM, 8);
1145 static struct qb_attr_code code_scn_rid_in_mem =
1146                         QB_CODE(1, SCN_RID_OFFSET_IN_MEM, 24);
1147 static struct qb_attr_code code_scn_ctx_lo = QB_CODE(2, 0, 32);
1148
1149 uint8_t qbman_result_SCN_state(const struct qbman_result *scn)
1150 {
1151         const uint32_t *p = qb_cl(scn);
1152
1153         return (uint8_t)qb_attr_code_decode(&code_scn_state, p);
1154 }
1155
1156 uint32_t qbman_result_SCN_rid(const struct qbman_result *scn)
1157 {
1158         const uint32_t *p = qb_cl(scn);
1159
1160         return qb_attr_code_decode(&code_scn_rid, p);
1161 }
1162
1163 uint64_t qbman_result_SCN_ctx(const struct qbman_result *scn)
1164 {
1165         const uint64_t *p = (const uint64_t *)qb_cl(scn);
1166
1167         return qb_attr_code_decode_64(&code_scn_ctx_lo, p);
1168 }
1169
1170 uint8_t qbman_result_SCN_state_in_mem(const struct qbman_result *scn)
1171 {
1172         const uint32_t *p = qb_cl(scn);
1173
1174         return (uint8_t)qb_attr_code_decode(&code_scn_state_in_mem, p);
1175 }
1176
1177 uint32_t qbman_result_SCN_rid_in_mem(const struct qbman_result *scn)
1178 {
1179         const uint32_t *p = qb_cl(scn);
1180         uint32_t result_rid;
1181
1182         result_rid = qb_attr_code_decode(&code_scn_rid_in_mem, p);
1183         return make_le24(result_rid);
1184 }
1185
1186 /*****************/
1187 /* Parsing BPSCN */
1188 /*****************/
1189 uint16_t qbman_result_bpscn_bpid(const struct qbman_result *scn)
1190 {
1191         return (uint16_t)qbman_result_SCN_rid_in_mem(scn) & 0x3FFF;
1192 }
1193
1194 int qbman_result_bpscn_has_free_bufs(const struct qbman_result *scn)
1195 {
1196         return !(int)(qbman_result_SCN_state_in_mem(scn) & 0x1);
1197 }
1198
1199 int qbman_result_bpscn_is_depleted(const struct qbman_result *scn)
1200 {
1201         return (int)(qbman_result_SCN_state_in_mem(scn) & 0x2);
1202 }
1203
1204 int qbman_result_bpscn_is_surplus(const struct qbman_result *scn)
1205 {
1206         return (int)(qbman_result_SCN_state_in_mem(scn) & 0x4);
1207 }
1208
1209 uint64_t qbman_result_bpscn_ctx(const struct qbman_result *scn)
1210 {
1211         uint64_t ctx;
1212         uint32_t ctx_hi, ctx_lo;
1213
1214         ctx = qbman_result_SCN_ctx(scn);
1215         ctx_hi = upper32(ctx);
1216         ctx_lo = lower32(ctx);
1217         return ((uint64_t)make_le32(ctx_hi) << 32 |
1218                 (uint64_t)make_le32(ctx_lo));
1219 }
1220
1221 /*****************/
1222 /* Parsing CGCU  */
1223 /*****************/
1224 uint16_t qbman_result_cgcu_cgid(const struct qbman_result *scn)
1225 {
1226         return (uint16_t)qbman_result_SCN_rid_in_mem(scn) & 0xFFFF;
1227 }
1228
1229 uint64_t qbman_result_cgcu_icnt(const struct qbman_result *scn)
1230 {
1231         uint64_t ctx;
1232         uint32_t ctx_hi, ctx_lo;
1233
1234         ctx = qbman_result_SCN_ctx(scn);
1235         ctx_hi = upper32(ctx);
1236         ctx_lo = lower32(ctx);
1237         return ((uint64_t)(make_le32(ctx_hi) & 0xFF) << 32) |
1238                 (uint64_t)make_le32(ctx_lo);
1239 }
1240
1241 /******************/
1242 /* Buffer release */
1243 /******************/
1244
1245 /* These should be const, eventually */
1246 /* static struct qb_attr_code code_release_num = QB_CODE(0, 0, 3); */
1247 static struct qb_attr_code code_release_set_me = QB_CODE(0, 5, 1);
1248 static struct qb_attr_code code_release_rcdi = QB_CODE(0, 6, 1);
1249 static struct qb_attr_code code_release_bpid = QB_CODE(0, 16, 16);
1250
1251 void qbman_release_desc_clear(struct qbman_release_desc *d)
1252 {
1253         uint32_t *cl;
1254
1255         memset(d, 0, sizeof(*d));
1256         cl = qb_cl(d);
1257         qb_attr_code_encode(&code_release_set_me, cl, 1);
1258 }
1259
1260 void qbman_release_desc_set_bpid(struct qbman_release_desc *d, uint32_t bpid)
1261 {
1262         uint32_t *cl = qb_cl(d);
1263
1264         qb_attr_code_encode(&code_release_bpid, cl, bpid);
1265 }
1266
1267 void qbman_release_desc_set_rcdi(struct qbman_release_desc *d, int enable)
1268 {
1269         uint32_t *cl = qb_cl(d);
1270
1271         qb_attr_code_encode(&code_release_rcdi, cl, !!enable);
1272 }
1273
1274 #define RAR_IDX(rar)     ((rar) & 0x7)
1275 #define RAR_VB(rar)      ((rar) & 0x80)
1276 #define RAR_SUCCESS(rar) ((rar) & 0x100)
1277
1278 int qbman_swp_release(struct qbman_swp *s, const struct qbman_release_desc *d,
1279                       const uint64_t *buffers, unsigned int num_buffers)
1280 {
1281         uint32_t *p;
1282         const uint32_t *cl = qb_cl(d);
1283         uint32_t rar = qbman_cinh_read(&s->sys, QBMAN_CINH_SWP_RAR);
1284
1285         pr_debug("RAR=%08x\n", rar);
1286         if (!RAR_SUCCESS(rar))
1287                 return -EBUSY;
1288         QBMAN_BUG_ON(!num_buffers || (num_buffers > 7));
1289         /* Start the release command */
1290         p = qbman_cena_write_start_wo_shadow(&s->sys,
1291                                              QBMAN_CENA_SWP_RCR(RAR_IDX(rar)));
1292         /* Copy the caller's buffer pointers to the command */
1293         u64_to_le32_copy(&p[2], buffers, num_buffers);
1294         /* Set the verb byte, have to substitute in the valid-bit and the number
1295          * of buffers.
1296          */
1297         lwsync();
1298         p[0] = cl[0] | RAR_VB(rar) | num_buffers;
1299         qbman_cena_write_complete_wo_shadow(&s->sys,
1300                                             QBMAN_CENA_SWP_RCR(RAR_IDX(rar)));
1301         return 0;
1302 }
1303
1304 /*******************/
1305 /* Buffer acquires */
1306 /*******************/
1307
1308 /* These should be const, eventually */
1309 static struct qb_attr_code code_acquire_bpid = QB_CODE(0, 16, 16);
1310 static struct qb_attr_code code_acquire_num = QB_CODE(1, 0, 3);
1311 static struct qb_attr_code code_acquire_r_num = QB_CODE(1, 0, 3);
1312
1313 int qbman_swp_acquire(struct qbman_swp *s, uint32_t bpid, uint64_t *buffers,
1314                       unsigned int num_buffers)
1315 {
1316         uint32_t *p;
1317         uint32_t rslt, num;
1318
1319         QBMAN_BUG_ON(!num_buffers || (num_buffers > 7));
1320
1321         /* Start the management command */
1322         p = qbman_swp_mc_start(s);
1323
1324         if (!p)
1325                 return -EBUSY;
1326
1327         /* Encode the caller-provided attributes */
1328         qb_attr_code_encode(&code_acquire_bpid, p, bpid);
1329         qb_attr_code_encode(&code_acquire_num, p, num_buffers);
1330
1331         /* Complete the management command */
1332         p = qbman_swp_mc_complete(s, p, p[0] | QBMAN_MC_ACQUIRE);
1333
1334         /* Decode the outcome */
1335         rslt = qb_attr_code_decode(&code_generic_rslt, p);
1336         num = qb_attr_code_decode(&code_acquire_r_num, p);
1337         QBMAN_BUG_ON(qb_attr_code_decode(&code_generic_verb, p) !=
1338                      QBMAN_MC_ACQUIRE);
1339
1340         /* Determine success or failure */
1341         if (unlikely(rslt != QBMAN_MC_RSLT_OK)) {
1342                 pr_err("Acquire buffers from BPID 0x%x failed, code=0x%02x\n",
1343                        bpid, rslt);
1344                 return -EIO;
1345         }
1346         QBMAN_BUG_ON(num > num_buffers);
1347         /* Copy the acquired buffers to the caller's array */
1348         u64_from_le32_copy(buffers, &p[2], num);
1349         return (int)num;
1350 }
1351
1352 /*****************/
1353 /* FQ management */
1354 /*****************/
1355
1356 static struct qb_attr_code code_fqalt_fqid = QB_CODE(1, 0, 32);
1357
1358 static int qbman_swp_alt_fq_state(struct qbman_swp *s, uint32_t fqid,
1359                                   uint8_t alt_fq_verb)
1360 {
1361         uint32_t *p;
1362         uint32_t rslt;
1363
1364         /* Start the management command */
1365         p = qbman_swp_mc_start(s);
1366         if (!p)
1367                 return -EBUSY;
1368
1369         qb_attr_code_encode(&code_fqalt_fqid, p, fqid);
1370         /* Complete the management command */
1371         p = qbman_swp_mc_complete(s, p, p[0] | alt_fq_verb);
1372
1373         /* Decode the outcome */
1374         rslt = qb_attr_code_decode(&code_generic_rslt, p);
1375         QBMAN_BUG_ON(qb_attr_code_decode(&code_generic_verb, p) != alt_fq_verb);
1376
1377         /* Determine success or failure */
1378         if (unlikely(rslt != QBMAN_MC_RSLT_OK)) {
1379                 pr_err("ALT FQID %d failed: verb = 0x%08x, code = 0x%02x\n",
1380                        fqid, alt_fq_verb, rslt);
1381                 return -EIO;
1382         }
1383
1384         return 0;
1385 }
1386
1387 int qbman_swp_fq_schedule(struct qbman_swp *s, uint32_t fqid)
1388 {
1389         return qbman_swp_alt_fq_state(s, fqid, QBMAN_FQ_SCHEDULE);
1390 }
1391
1392 int qbman_swp_fq_force(struct qbman_swp *s, uint32_t fqid)
1393 {
1394         return qbman_swp_alt_fq_state(s, fqid, QBMAN_FQ_FORCE);
1395 }
1396
1397 int qbman_swp_fq_xon(struct qbman_swp *s, uint32_t fqid)
1398 {
1399         return qbman_swp_alt_fq_state(s, fqid, QBMAN_FQ_XON);
1400 }
1401
1402 int qbman_swp_fq_xoff(struct qbman_swp *s, uint32_t fqid)
1403 {
1404         return qbman_swp_alt_fq_state(s, fqid, QBMAN_FQ_XOFF);
1405 }
1406
1407 /**********************/
1408 /* Channel management */
1409 /**********************/
1410
1411 static struct qb_attr_code code_cdan_cid = QB_CODE(0, 16, 12);
1412 static struct qb_attr_code code_cdan_we = QB_CODE(1, 0, 8);
1413 static struct qb_attr_code code_cdan_en = QB_CODE(1, 8, 1);
1414 static struct qb_attr_code code_cdan_ctx_lo = QB_CODE(2, 0, 32);
1415
1416 /* Hide "ICD" for now as we don't use it, don't set it, and don't test it, so it
1417  * would be irresponsible to expose it.
1418  */
1419 #define CODE_CDAN_WE_EN    0x1
1420 #define CODE_CDAN_WE_CTX   0x4
1421
1422 static int qbman_swp_CDAN_set(struct qbman_swp *s, uint16_t channelid,
1423                               uint8_t we_mask, uint8_t cdan_en,
1424                               uint64_t ctx)
1425 {
1426         uint32_t *p;
1427         uint32_t rslt;
1428
1429         /* Start the management command */
1430         p = qbman_swp_mc_start(s);
1431         if (!p)
1432                 return -EBUSY;
1433
1434         /* Encode the caller-provided attributes */
1435         qb_attr_code_encode(&code_cdan_cid, p, channelid);
1436         qb_attr_code_encode(&code_cdan_we, p, we_mask);
1437         qb_attr_code_encode(&code_cdan_en, p, cdan_en);
1438         qb_attr_code_encode_64(&code_cdan_ctx_lo, (uint64_t *)p, ctx);
1439         /* Complete the management command */
1440         p = qbman_swp_mc_complete(s, p, p[0] | QBMAN_WQCHAN_CONFIGURE);
1441
1442         /* Decode the outcome */
1443         rslt = qb_attr_code_decode(&code_generic_rslt, p);
1444         QBMAN_BUG_ON(qb_attr_code_decode(&code_generic_verb, p)
1445                                         != QBMAN_WQCHAN_CONFIGURE);
1446
1447         /* Determine success or failure */
1448         if (unlikely(rslt != QBMAN_MC_RSLT_OK)) {
1449                 pr_err("CDAN cQID %d failed: code = 0x%02x\n",
1450                        channelid, rslt);
1451                 return -EIO;
1452         }
1453
1454         return 0;
1455 }
1456
1457 int qbman_swp_CDAN_set_context(struct qbman_swp *s, uint16_t channelid,
1458                                uint64_t ctx)
1459 {
1460         return qbman_swp_CDAN_set(s, channelid,
1461                                   CODE_CDAN_WE_CTX,
1462                                   0, ctx);
1463 }
1464
1465 int qbman_swp_CDAN_enable(struct qbman_swp *s, uint16_t channelid)
1466 {
1467         return qbman_swp_CDAN_set(s, channelid,
1468                                   CODE_CDAN_WE_EN,
1469                                   1, 0);
1470 }
1471
1472 int qbman_swp_CDAN_disable(struct qbman_swp *s, uint16_t channelid)
1473 {
1474         return qbman_swp_CDAN_set(s, channelid,
1475                                   CODE_CDAN_WE_EN,
1476                                   0, 0);
1477 }
1478
1479 int qbman_swp_CDAN_set_context_enable(struct qbman_swp *s, uint16_t channelid,
1480                                       uint64_t ctx)
1481 {
1482         return qbman_swp_CDAN_set(s, channelid,
1483                                   CODE_CDAN_WE_EN | CODE_CDAN_WE_CTX,
1484                                   1, ctx);
1485 }
1486
1487 uint8_t qbman_get_dqrr_idx(const struct qbman_result *dqrr)
1488 {
1489         return QBMAN_IDX_FROM_DQRR(dqrr);
1490 }
1491
1492 struct qbman_result *qbman_get_dqrr_from_idx(struct qbman_swp *s, uint8_t idx)
1493 {
1494         struct qbman_result *dq;
1495
1496         dq = qbman_cena_read(&s->sys, QBMAN_CENA_SWP_DQRR(idx));
1497         return dq;
1498 }