GNU Linux-libre 4.14.290-gnu1
[releases.git] / drivers / staging / fsl-mc / bus / dpio / qbman-portal.c
1 /*
2  * Copyright (C) 2014-2016 Freescale Semiconductor, Inc.
3  * Copyright 2016 NXP
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of Freescale Semiconductor nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  * ALTERNATIVELY, this software may be distributed under the terms of the
17  * GNU General Public License ("GPL") as published by the Free Software
18  * Foundation, either version 2 of that License or (at your option) any
19  * later version.
20  *
21  * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
22  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24  * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
25  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <asm/cacheflush.h>
34 #include <linux/io.h>
35 #include <linux/slab.h>
36 #include "../../include/dpaa2-global.h"
37
38 #include "qbman-portal.h"
39
40 #define QMAN_REV_4000   0x04000000
41 #define QMAN_REV_4100   0x04010000
42 #define QMAN_REV_4101   0x04010001
43 #define QMAN_REV_MASK   0xffff0000
44
45 /* All QBMan command and result structures use this "valid bit" encoding */
46 #define QB_VALID_BIT ((u32)0x80)
47
48 /* QBMan portal management command codes */
49 #define QBMAN_MC_ACQUIRE       0x30
50 #define QBMAN_WQCHAN_CONFIGURE 0x46
51
52 /* CINH register offsets */
53 #define QBMAN_CINH_SWP_EQAR    0x8c0
54 #define QBMAN_CINH_SWP_DQPI    0xa00
55 #define QBMAN_CINH_SWP_DCAP    0xac0
56 #define QBMAN_CINH_SWP_SDQCR   0xb00
57 #define QBMAN_CINH_SWP_RAR     0xcc0
58 #define QBMAN_CINH_SWP_ISR     0xe00
59 #define QBMAN_CINH_SWP_IER     0xe40
60 #define QBMAN_CINH_SWP_ISDR    0xe80
61 #define QBMAN_CINH_SWP_IIR     0xec0
62
63 /* CENA register offsets */
64 #define QBMAN_CENA_SWP_EQCR(n) (0x000 + ((u32)(n) << 6))
65 #define QBMAN_CENA_SWP_DQRR(n) (0x200 + ((u32)(n) << 6))
66 #define QBMAN_CENA_SWP_RCR(n)  (0x400 + ((u32)(n) << 6))
67 #define QBMAN_CENA_SWP_CR      0x600
68 #define QBMAN_CENA_SWP_RR(vb)  (0x700 + ((u32)(vb) >> 1))
69 #define QBMAN_CENA_SWP_VDQCR   0x780
70
71 /* Reverse mapping of QBMAN_CENA_SWP_DQRR() */
72 #define QBMAN_IDX_FROM_DQRR(p) (((unsigned long)(p) & 0x1ff) >> 6)
73
74 /* Define token used to determine if response written to memory is valid */
75 #define QMAN_DQ_TOKEN_VALID 1
76
77 /* SDQCR attribute codes */
78 #define QB_SDQCR_FC_SHIFT   29
79 #define QB_SDQCR_FC_MASK    0x1
80 #define QB_SDQCR_DCT_SHIFT  24
81 #define QB_SDQCR_DCT_MASK   0x3
82 #define QB_SDQCR_TOK_SHIFT  16
83 #define QB_SDQCR_TOK_MASK   0xff
84 #define QB_SDQCR_SRC_SHIFT  0
85 #define QB_SDQCR_SRC_MASK   0xffff
86
87 /* opaque token for static dequeues */
88 #define QMAN_SDQCR_TOKEN    0xbb
89
90 enum qbman_sdqcr_dct {
91         qbman_sdqcr_dct_null = 0,
92         qbman_sdqcr_dct_prio_ics,
93         qbman_sdqcr_dct_active_ics,
94         qbman_sdqcr_dct_active
95 };
96
97 enum qbman_sdqcr_fc {
98         qbman_sdqcr_fc_one = 0,
99         qbman_sdqcr_fc_up_to_3 = 1
100 };
101
102 /* Portal Access */
103
104 static inline u32 qbman_read_register(struct qbman_swp *p, u32 offset)
105 {
106         return readl_relaxed(p->addr_cinh + offset);
107 }
108
109 static inline void qbman_write_register(struct qbman_swp *p, u32 offset,
110                                         u32 value)
111 {
112         writel_relaxed(value, p->addr_cinh + offset);
113 }
114
115 static inline void *qbman_get_cmd(struct qbman_swp *p, u32 offset)
116 {
117         return p->addr_cena + offset;
118 }
119
120 #define QBMAN_CINH_SWP_CFG   0xd00
121
122 #define SWP_CFG_DQRR_MF_SHIFT 20
123 #define SWP_CFG_EST_SHIFT     16
124 #define SWP_CFG_WN_SHIFT      14
125 #define SWP_CFG_RPM_SHIFT     12
126 #define SWP_CFG_DCM_SHIFT     10
127 #define SWP_CFG_EPM_SHIFT     8
128 #define SWP_CFG_SD_SHIFT      5
129 #define SWP_CFG_SP_SHIFT      4
130 #define SWP_CFG_SE_SHIFT      3
131 #define SWP_CFG_DP_SHIFT      2
132 #define SWP_CFG_DE_SHIFT      1
133 #define SWP_CFG_EP_SHIFT      0
134
135 static inline u32 qbman_set_swp_cfg(u8 max_fill, u8 wn, u8 est, u8 rpm, u8 dcm,
136                                     u8 epm, int sd, int sp, int se,
137                                     int dp, int de, int ep)
138 {
139         return (max_fill << SWP_CFG_DQRR_MF_SHIFT |
140                 est << SWP_CFG_EST_SHIFT |
141                 wn << SWP_CFG_WN_SHIFT |
142                 rpm << SWP_CFG_RPM_SHIFT |
143                 dcm << SWP_CFG_DCM_SHIFT |
144                 epm << SWP_CFG_EPM_SHIFT |
145                 sd << SWP_CFG_SD_SHIFT |
146                 sp << SWP_CFG_SP_SHIFT |
147                 se << SWP_CFG_SE_SHIFT |
148                 dp << SWP_CFG_DP_SHIFT |
149                 de << SWP_CFG_DE_SHIFT |
150                 ep << SWP_CFG_EP_SHIFT);
151 }
152
153 /**
154  * qbman_swp_init() - Create a functional object representing the given
155  *                    QBMan portal descriptor.
156  * @d: the given qbman swp descriptor
157  *
158  * Return qbman_swp portal for success, NULL if the object cannot
159  * be created.
160  */
161 struct qbman_swp *qbman_swp_init(const struct qbman_swp_desc *d)
162 {
163         struct qbman_swp *p = kmalloc(sizeof(*p), GFP_KERNEL);
164         u32 reg;
165
166         if (!p)
167                 return NULL;
168         p->desc = d;
169         p->mc.valid_bit = QB_VALID_BIT;
170         p->sdq = 0;
171         p->sdq |= qbman_sdqcr_dct_prio_ics << QB_SDQCR_DCT_SHIFT;
172         p->sdq |= qbman_sdqcr_fc_up_to_3 << QB_SDQCR_FC_SHIFT;
173         p->sdq |= QMAN_SDQCR_TOKEN << QB_SDQCR_TOK_SHIFT;
174
175         atomic_set(&p->vdq.available, 1);
176         p->vdq.valid_bit = QB_VALID_BIT;
177         p->dqrr.next_idx = 0;
178         p->dqrr.valid_bit = QB_VALID_BIT;
179
180         if ((p->desc->qman_version & QMAN_REV_MASK) < QMAN_REV_4100) {
181                 p->dqrr.dqrr_size = 4;
182                 p->dqrr.reset_bug = 1;
183         } else {
184                 p->dqrr.dqrr_size = 8;
185                 p->dqrr.reset_bug = 0;
186         }
187
188         p->addr_cena = d->cena_bar;
189         p->addr_cinh = d->cinh_bar;
190
191         reg = qbman_set_swp_cfg(p->dqrr.dqrr_size,
192                                 1, /* Writes Non-cacheable */
193                                 0, /* EQCR_CI stashing threshold */
194                                 3, /* RPM: Valid bit mode, RCR in array mode */
195                                 2, /* DCM: Discrete consumption ack mode */
196                                 3, /* EPM: Valid bit mode, EQCR in array mode */
197                                 0, /* mem stashing drop enable == FALSE */
198                                 1, /* mem stashing priority == TRUE */
199                                 0, /* mem stashing enable == FALSE */
200                                 1, /* dequeue stashing priority == TRUE */
201                                 0, /* dequeue stashing enable == FALSE */
202                                 0); /* EQCR_CI stashing priority == FALSE */
203
204         qbman_write_register(p, QBMAN_CINH_SWP_CFG, reg);
205         reg = qbman_read_register(p, QBMAN_CINH_SWP_CFG);
206         if (!reg) {
207                 pr_err("qbman: the portal is not enabled!\n");
208                 return NULL;
209         }
210
211         /*
212          * SDQCR needs to be initialized to 0 when no channels are
213          * being dequeued from or else the QMan HW will indicate an
214          * error.  The values that were calculated above will be
215          * applied when dequeues from a specific channel are enabled.
216          */
217         qbman_write_register(p, QBMAN_CINH_SWP_SDQCR, 0);
218         return p;
219 }
220
221 /**
222  * qbman_swp_finish() - Create and destroy a functional object representing
223  *                      the given QBMan portal descriptor.
224  * @p: the qbman_swp object to be destroyed
225  */
226 void qbman_swp_finish(struct qbman_swp *p)
227 {
228         kfree(p);
229 }
230
231 /**
232  * qbman_swp_interrupt_read_status()
233  * @p: the given software portal
234  *
235  * Return the value in the SWP_ISR register.
236  */
237 u32 qbman_swp_interrupt_read_status(struct qbman_swp *p)
238 {
239         return qbman_read_register(p, QBMAN_CINH_SWP_ISR);
240 }
241
242 /**
243  * qbman_swp_interrupt_clear_status()
244  * @p: the given software portal
245  * @mask: The mask to clear in SWP_ISR register
246  */
247 void qbman_swp_interrupt_clear_status(struct qbman_swp *p, u32 mask)
248 {
249         qbman_write_register(p, QBMAN_CINH_SWP_ISR, mask);
250 }
251
252 /**
253  * qbman_swp_interrupt_get_trigger() - read interrupt enable register
254  * @p: the given software portal
255  *
256  * Return the value in the SWP_IER register.
257  */
258 u32 qbman_swp_interrupt_get_trigger(struct qbman_swp *p)
259 {
260         return qbman_read_register(p, QBMAN_CINH_SWP_IER);
261 }
262
263 /**
264  * qbman_swp_interrupt_set_trigger() - enable interrupts for a swp
265  * @p: the given software portal
266  * @mask: The mask of bits to enable in SWP_IER
267  */
268 void qbman_swp_interrupt_set_trigger(struct qbman_swp *p, u32 mask)
269 {
270         qbman_write_register(p, QBMAN_CINH_SWP_IER, mask);
271 }
272
273 /**
274  * qbman_swp_interrupt_get_inhibit() - read interrupt mask register
275  * @p: the given software portal object
276  *
277  * Return the value in the SWP_IIR register.
278  */
279 int qbman_swp_interrupt_get_inhibit(struct qbman_swp *p)
280 {
281         return qbman_read_register(p, QBMAN_CINH_SWP_IIR);
282 }
283
284 /**
285  * qbman_swp_interrupt_set_inhibit() - write interrupt mask register
286  * @p: the given software portal object
287  * @mask: The mask to set in SWP_IIR register
288  */
289 void qbman_swp_interrupt_set_inhibit(struct qbman_swp *p, int inhibit)
290 {
291         qbman_write_register(p, QBMAN_CINH_SWP_IIR, inhibit ? 0xffffffff : 0);
292 }
293
294 /*
295  * Different management commands all use this common base layer of code to issue
296  * commands and poll for results.
297  */
298
299 /*
300  * Returns a pointer to where the caller should fill in their management command
301  * (caller should ignore the verb byte)
302  */
303 void *qbman_swp_mc_start(struct qbman_swp *p)
304 {
305         return qbman_get_cmd(p, QBMAN_CENA_SWP_CR);
306 }
307
308 /*
309  * Commits merges in the caller-supplied command verb (which should not include
310  * the valid-bit) and submits the command to hardware
311  */
312 void qbman_swp_mc_submit(struct qbman_swp *p, void *cmd, u8 cmd_verb)
313 {
314         u8 *v = cmd;
315
316         dma_wmb();
317         *v = cmd_verb | p->mc.valid_bit;
318 }
319
320 /*
321  * Checks for a completed response (returns non-NULL if only if the response
322  * is complete).
323  */
324 void *qbman_swp_mc_result(struct qbman_swp *p)
325 {
326         u32 *ret, verb;
327
328         ret = qbman_get_cmd(p, QBMAN_CENA_SWP_RR(p->mc.valid_bit));
329
330         /* Remove the valid-bit - command completed if the rest is non-zero */
331         verb = ret[0] & ~QB_VALID_BIT;
332         if (!verb)
333                 return NULL;
334         p->mc.valid_bit ^= QB_VALID_BIT;
335         return ret;
336 }
337
338 #define QB_ENQUEUE_CMD_OPTIONS_SHIFT    0
339 enum qb_enqueue_commands {
340         enqueue_empty = 0,
341         enqueue_response_always = 1,
342         enqueue_rejects_to_fq = 2
343 };
344
345 #define QB_ENQUEUE_CMD_ORP_ENABLE_SHIFT      2
346 #define QB_ENQUEUE_CMD_IRQ_ON_DISPATCH_SHIFT 3
347 #define QB_ENQUEUE_CMD_TARGET_TYPE_SHIFT     4
348
349 /**
350  * qbman_eq_desc_clear() - Clear the contents of a descriptor to
351  *                         default/starting state.
352  */
353 void qbman_eq_desc_clear(struct qbman_eq_desc *d)
354 {
355         memset(d, 0, sizeof(*d));
356 }
357
358 /**
359  * qbman_eq_desc_set_no_orp() - Set enqueue descriptor without orp
360  * @d:                the enqueue descriptor.
361  * @response_success: 1 = enqueue with response always; 0 = enqueue with
362  *                    rejections returned on a FQ.
363  */
364 void qbman_eq_desc_set_no_orp(struct qbman_eq_desc *d, int respond_success)
365 {
366         d->verb &= ~(1 << QB_ENQUEUE_CMD_ORP_ENABLE_SHIFT);
367         if (respond_success)
368                 d->verb |= enqueue_response_always;
369         else
370                 d->verb |= enqueue_rejects_to_fq;
371 }
372
373 /*
374  * Exactly one of the following descriptor "targets" should be set. (Calling any
375  * one of these will replace the effect of any prior call to one of these.)
376  *   -enqueue to a frame queue
377  *   -enqueue to a queuing destination
378  */
379
380 /**
381  * qbman_eq_desc_set_fq() - set the FQ for the enqueue command
382  * @d:    the enqueue descriptor
383  * @fqid: the id of the frame queue to be enqueued
384  */
385 void qbman_eq_desc_set_fq(struct qbman_eq_desc *d, u32 fqid)
386 {
387         d->verb &= ~(1 << QB_ENQUEUE_CMD_TARGET_TYPE_SHIFT);
388         d->tgtid = cpu_to_le32(fqid);
389 }
390
391 /**
392  * qbman_eq_desc_set_qd() - Set Queuing Destination for the enqueue command
393  * @d:       the enqueue descriptor
394  * @qdid:    the id of the queuing destination to be enqueued
395  * @qd_bin:  the queuing destination bin
396  * @qd_prio: the queuing destination priority
397  */
398 void qbman_eq_desc_set_qd(struct qbman_eq_desc *d, u32 qdid,
399                           u32 qd_bin, u32 qd_prio)
400 {
401         d->verb |= 1 << QB_ENQUEUE_CMD_TARGET_TYPE_SHIFT;
402         d->tgtid = cpu_to_le32(qdid);
403         d->qdbin = cpu_to_le16(qd_bin);
404         d->qpri = qd_prio;
405 }
406
407 #define EQAR_IDX(eqar)     ((eqar) & 0x7)
408 #define EQAR_VB(eqar)      ((eqar) & 0x80)
409 #define EQAR_SUCCESS(eqar) ((eqar) & 0x100)
410
411 /**
412  * qbman_swp_enqueue() - Issue an enqueue command
413  * @s:  the software portal used for enqueue
414  * @d:  the enqueue descriptor
415  * @fd: the frame descriptor to be enqueued
416  *
417  * Please note that 'fd' should only be NULL if the "action" of the
418  * descriptor is "orp_hole" or "orp_nesn".
419  *
420  * Return 0 for successful enqueue, -EBUSY if the EQCR is not ready.
421  */
422 int qbman_swp_enqueue(struct qbman_swp *s, const struct qbman_eq_desc *d,
423                       const struct dpaa2_fd *fd)
424 {
425         struct qbman_eq_desc *p;
426         u32 eqar = qbman_read_register(s, QBMAN_CINH_SWP_EQAR);
427
428         if (!EQAR_SUCCESS(eqar))
429                 return -EBUSY;
430
431         p = qbman_get_cmd(s, QBMAN_CENA_SWP_EQCR(EQAR_IDX(eqar)));
432         memcpy(&p->dca, &d->dca, 31);
433         memcpy(&p->fd, fd, sizeof(*fd));
434
435         /* Set the verb byte, have to substitute in the valid-bit */
436         dma_wmb();
437         p->verb = d->verb | EQAR_VB(eqar);
438
439         return 0;
440 }
441
442 /* Static (push) dequeue */
443
444 /**
445  * qbman_swp_push_get() - Get the push dequeue setup
446  * @p:           the software portal object
447  * @channel_idx: the channel index to query
448  * @enabled:     returned boolean to show whether the push dequeue is enabled
449  *               for the given channel
450  */
451 void qbman_swp_push_get(struct qbman_swp *s, u8 channel_idx, int *enabled)
452 {
453         u16 src = (s->sdq >> QB_SDQCR_SRC_SHIFT) & QB_SDQCR_SRC_MASK;
454
455         WARN_ON(channel_idx > 15);
456         *enabled = src | (1 << channel_idx);
457 }
458
459 /**
460  * qbman_swp_push_set() - Enable or disable push dequeue
461  * @p:           the software portal object
462  * @channel_idx: the channel index (0 to 15)
463  * @enable:      enable or disable push dequeue
464  */
465 void qbman_swp_push_set(struct qbman_swp *s, u8 channel_idx, int enable)
466 {
467         u16 dqsrc;
468
469         WARN_ON(channel_idx > 15);
470         if (enable)
471                 s->sdq |= 1 << channel_idx;
472         else
473                 s->sdq &= ~(1 << channel_idx);
474
475         /* Read make the complete src map.  If no channels are enabled
476          * the SDQCR must be 0 or else QMan will assert errors
477          */
478         dqsrc = (s->sdq >> QB_SDQCR_SRC_SHIFT) & QB_SDQCR_SRC_MASK;
479         if (dqsrc != 0)
480                 qbman_write_register(s, QBMAN_CINH_SWP_SDQCR, s->sdq);
481         else
482                 qbman_write_register(s, QBMAN_CINH_SWP_SDQCR, 0);
483 }
484
485 #define QB_VDQCR_VERB_DCT_SHIFT    0
486 #define QB_VDQCR_VERB_DT_SHIFT     2
487 #define QB_VDQCR_VERB_RLS_SHIFT    4
488 #define QB_VDQCR_VERB_WAE_SHIFT    5
489
490 enum qb_pull_dt_e {
491         qb_pull_dt_channel,
492         qb_pull_dt_workqueue,
493         qb_pull_dt_framequeue
494 };
495
496 /**
497  * qbman_pull_desc_clear() - Clear the contents of a descriptor to
498  *                           default/starting state
499  * @d: the pull dequeue descriptor to be cleared
500  */
501 void qbman_pull_desc_clear(struct qbman_pull_desc *d)
502 {
503         memset(d, 0, sizeof(*d));
504 }
505
506 /**
507  * qbman_pull_desc_set_storage()- Set the pull dequeue storage
508  * @d:            the pull dequeue descriptor to be set
509  * @storage:      the pointer of the memory to store the dequeue result
510  * @storage_phys: the physical address of the storage memory
511  * @stash:        to indicate whether write allocate is enabled
512  *
513  * If not called, or if called with 'storage' as NULL, the result pull dequeues
514  * will produce results to DQRR. If 'storage' is non-NULL, then results are
515  * produced to the given memory location (using the DMA address which
516  * the caller provides in 'storage_phys'), and 'stash' controls whether or not
517  * those writes to main-memory express a cache-warming attribute.
518  */
519 void qbman_pull_desc_set_storage(struct qbman_pull_desc *d,
520                                  struct dpaa2_dq *storage,
521                                  dma_addr_t storage_phys,
522                                  int stash)
523 {
524         /* save the virtual address */
525         d->rsp_addr_virt = (u64)storage;
526
527         if (!storage) {
528                 d->verb &= ~(1 << QB_VDQCR_VERB_RLS_SHIFT);
529                 return;
530         }
531         d->verb |= 1 << QB_VDQCR_VERB_RLS_SHIFT;
532         if (stash)
533                 d->verb |= 1 << QB_VDQCR_VERB_WAE_SHIFT;
534         else
535                 d->verb &= ~(1 << QB_VDQCR_VERB_WAE_SHIFT);
536
537         d->rsp_addr = cpu_to_le64(storage_phys);
538 }
539
540 /**
541  * qbman_pull_desc_set_numframes() - Set the number of frames to be dequeued
542  * @d:         the pull dequeue descriptor to be set
543  * @numframes: number of frames to be set, must be between 1 and 16, inclusive
544  */
545 void qbman_pull_desc_set_numframes(struct qbman_pull_desc *d, u8 numframes)
546 {
547         d->numf = numframes - 1;
548 }
549
550 void qbman_pull_desc_set_token(struct qbman_pull_desc *d, u8 token)
551 {
552         d->tok = token;
553 }
554
555 /*
556  * Exactly one of the following descriptor "actions" should be set. (Calling any
557  * one of these will replace the effect of any prior call to one of these.)
558  * - pull dequeue from the given frame queue (FQ)
559  * - pull dequeue from any FQ in the given work queue (WQ)
560  * - pull dequeue from any FQ in any WQ in the given channel
561  */
562
563 /**
564  * qbman_pull_desc_set_fq() - Set fqid from which the dequeue command dequeues
565  * @fqid: the frame queue index of the given FQ
566  */
567 void qbman_pull_desc_set_fq(struct qbman_pull_desc *d, u32 fqid)
568 {
569         d->verb |= 1 << QB_VDQCR_VERB_DCT_SHIFT;
570         d->verb |= qb_pull_dt_framequeue << QB_VDQCR_VERB_DT_SHIFT;
571         d->dq_src = cpu_to_le32(fqid);
572 }
573
574 /**
575  * qbman_pull_desc_set_wq() - Set wqid from which the dequeue command dequeues
576  * @wqid: composed of channel id and wqid within the channel
577  * @dct:  the dequeue command type
578  */
579 void qbman_pull_desc_set_wq(struct qbman_pull_desc *d, u32 wqid,
580                             enum qbman_pull_type_e dct)
581 {
582         d->verb |= dct << QB_VDQCR_VERB_DCT_SHIFT;
583         d->verb |= qb_pull_dt_workqueue << QB_VDQCR_VERB_DT_SHIFT;
584         d->dq_src = cpu_to_le32(wqid);
585 }
586
587 /**
588  * qbman_pull_desc_set_channel() - Set channelid from which the dequeue command
589  *                                 dequeues
590  * @chid: the channel id to be dequeued
591  * @dct:  the dequeue command type
592  */
593 void qbman_pull_desc_set_channel(struct qbman_pull_desc *d, u32 chid,
594                                  enum qbman_pull_type_e dct)
595 {
596         d->verb |= dct << QB_VDQCR_VERB_DCT_SHIFT;
597         d->verb |= qb_pull_dt_channel << QB_VDQCR_VERB_DT_SHIFT;
598         d->dq_src = cpu_to_le32(chid);
599 }
600
601 /**
602  * qbman_swp_pull() - Issue the pull dequeue command
603  * @s: the software portal object
604  * @d: the software portal descriptor which has been configured with
605  *     the set of qbman_pull_desc_set_*() calls
606  *
607  * Return 0 for success, and -EBUSY if the software portal is not ready
608  * to do pull dequeue.
609  */
610 int qbman_swp_pull(struct qbman_swp *s, struct qbman_pull_desc *d)
611 {
612         struct qbman_pull_desc *p;
613
614         if (!atomic_dec_and_test(&s->vdq.available)) {
615                 atomic_inc(&s->vdq.available);
616                 return -EBUSY;
617         }
618         s->vdq.storage = (void *)d->rsp_addr_virt;
619         p = qbman_get_cmd(s, QBMAN_CENA_SWP_VDQCR);
620         p->numf = d->numf;
621         p->tok = QMAN_DQ_TOKEN_VALID;
622         p->dq_src = d->dq_src;
623         p->rsp_addr = d->rsp_addr;
624         p->rsp_addr_virt = d->rsp_addr_virt;
625         dma_wmb();
626
627         /* Set the verb byte, have to substitute in the valid-bit */
628         p->verb = d->verb | s->vdq.valid_bit;
629         s->vdq.valid_bit ^= QB_VALID_BIT;
630
631         return 0;
632 }
633
634 #define QMAN_DQRR_PI_MASK   0xf
635
636 /**
637  * qbman_swp_dqrr_next() - Get an valid DQRR entry
638  * @s: the software portal object
639  *
640  * Return NULL if there are no unconsumed DQRR entries. Return a DQRR entry
641  * only once, so repeated calls can return a sequence of DQRR entries, without
642  * requiring they be consumed immediately or in any particular order.
643  */
644 const struct dpaa2_dq *qbman_swp_dqrr_next(struct qbman_swp *s)
645 {
646         u32 verb;
647         u32 response_verb;
648         u32 flags;
649         struct dpaa2_dq *p;
650
651         /* Before using valid-bit to detect if something is there, we have to
652          * handle the case of the DQRR reset bug...
653          */
654         if (unlikely(s->dqrr.reset_bug)) {
655                 /*
656                  * We pick up new entries by cache-inhibited producer index,
657                  * which means that a non-coherent mapping would require us to
658                  * invalidate and read *only* once that PI has indicated that
659                  * there's an entry here. The first trip around the DQRR ring
660                  * will be much less efficient than all subsequent trips around
661                  * it...
662                  */
663                 u8 pi = qbman_read_register(s, QBMAN_CINH_SWP_DQPI) &
664                         QMAN_DQRR_PI_MASK;
665
666                 /* there are new entries if pi != next_idx */
667                 if (pi == s->dqrr.next_idx)
668                         return NULL;
669
670                 /*
671                  * if next_idx is/was the last ring index, and 'pi' is
672                  * different, we can disable the workaround as all the ring
673                  * entries have now been DMA'd to so valid-bit checking is
674                  * repaired. Note: this logic needs to be based on next_idx
675                  * (which increments one at a time), rather than on pi (which
676                  * can burst and wrap-around between our snapshots of it).
677                  */
678                 if (s->dqrr.next_idx == (s->dqrr.dqrr_size - 1)) {
679                         pr_debug("next_idx=%d, pi=%d, clear reset bug\n",
680                                  s->dqrr.next_idx, pi);
681                         s->dqrr.reset_bug = 0;
682                 }
683                 prefetch(qbman_get_cmd(s,
684                                        QBMAN_CENA_SWP_DQRR(s->dqrr.next_idx)));
685         }
686
687         p = qbman_get_cmd(s, QBMAN_CENA_SWP_DQRR(s->dqrr.next_idx));
688         verb = p->dq.verb;
689
690         /*
691          * If the valid-bit isn't of the expected polarity, nothing there. Note,
692          * in the DQRR reset bug workaround, we shouldn't need to skip these
693          * check, because we've already determined that a new entry is available
694          * and we've invalidated the cacheline before reading it, so the
695          * valid-bit behaviour is repaired and should tell us what we already
696          * knew from reading PI.
697          */
698         if ((verb & QB_VALID_BIT) != s->dqrr.valid_bit) {
699                 prefetch(qbman_get_cmd(s,
700                                        QBMAN_CENA_SWP_DQRR(s->dqrr.next_idx)));
701                 return NULL;
702         }
703         /*
704          * There's something there. Move "next_idx" attention to the next ring
705          * entry (and prefetch it) before returning what we found.
706          */
707         s->dqrr.next_idx++;
708         s->dqrr.next_idx &= s->dqrr.dqrr_size - 1; /* Wrap around */
709         if (!s->dqrr.next_idx)
710                 s->dqrr.valid_bit ^= QB_VALID_BIT;
711
712         /*
713          * If this is the final response to a volatile dequeue command
714          * indicate that the vdq is available
715          */
716         flags = p->dq.stat;
717         response_verb = verb & QBMAN_RESULT_MASK;
718         if ((response_verb == QBMAN_RESULT_DQ) &&
719             (flags & DPAA2_DQ_STAT_VOLATILE) &&
720             (flags & DPAA2_DQ_STAT_EXPIRED))
721                 atomic_inc(&s->vdq.available);
722
723         prefetch(qbman_get_cmd(s, QBMAN_CENA_SWP_DQRR(s->dqrr.next_idx)));
724
725         return p;
726 }
727
728 /**
729  * qbman_swp_dqrr_consume() -  Consume DQRR entries previously returned from
730  *                             qbman_swp_dqrr_next().
731  * @s: the software portal object
732  * @dq: the DQRR entry to be consumed
733  */
734 void qbman_swp_dqrr_consume(struct qbman_swp *s, const struct dpaa2_dq *dq)
735 {
736         qbman_write_register(s, QBMAN_CINH_SWP_DCAP, QBMAN_IDX_FROM_DQRR(dq));
737 }
738
739 /**
740  * qbman_result_has_new_result() - Check and get the dequeue response from the
741  *                                 dq storage memory set in pull dequeue command
742  * @s: the software portal object
743  * @dq: the dequeue result read from the memory
744  *
745  * Return 1 for getting a valid dequeue result, or 0 for not getting a valid
746  * dequeue result.
747  *
748  * Only used for user-provided storage of dequeue results, not DQRR. For
749  * efficiency purposes, the driver will perform any required endianness
750  * conversion to ensure that the user's dequeue result storage is in host-endian
751  * format. As such, once the user has called qbman_result_has_new_result() and
752  * been returned a valid dequeue result, they should not call it again on
753  * the same memory location (except of course if another dequeue command has
754  * been executed to produce a new result to that location).
755  */
756 int qbman_result_has_new_result(struct qbman_swp *s, const struct dpaa2_dq *dq)
757 {
758         if (dq->dq.tok != QMAN_DQ_TOKEN_VALID)
759                 return 0;
760
761         /*
762          * Set token to be 0 so we will detect change back to 1
763          * next time the looping is traversed. Const is cast away here
764          * as we want users to treat the dequeue responses as read only.
765          */
766         ((struct dpaa2_dq *)dq)->dq.tok = 0;
767
768         /*
769          * Determine whether VDQCR is available based on whether the
770          * current result is sitting in the first storage location of
771          * the busy command.
772          */
773         if (s->vdq.storage == dq) {
774                 s->vdq.storage = NULL;
775                 atomic_inc(&s->vdq.available);
776         }
777
778         return 1;
779 }
780
781 /**
782  * qbman_release_desc_clear() - Clear the contents of a descriptor to
783  *                              default/starting state.
784  */
785 void qbman_release_desc_clear(struct qbman_release_desc *d)
786 {
787         memset(d, 0, sizeof(*d));
788         d->verb = 1 << 5; /* Release Command Valid */
789 }
790
791 /**
792  * qbman_release_desc_set_bpid() - Set the ID of the buffer pool to release to
793  */
794 void qbman_release_desc_set_bpid(struct qbman_release_desc *d, u16 bpid)
795 {
796         d->bpid = cpu_to_le16(bpid);
797 }
798
799 /**
800  * qbman_release_desc_set_rcdi() - Determines whether or not the portal's RCDI
801  * interrupt source should be asserted after the release command is completed.
802  */
803 void qbman_release_desc_set_rcdi(struct qbman_release_desc *d, int enable)
804 {
805         if (enable)
806                 d->verb |= 1 << 6;
807         else
808                 d->verb &= ~(1 << 6);
809 }
810
811 #define RAR_IDX(rar)     ((rar) & 0x7)
812 #define RAR_VB(rar)      ((rar) & 0x80)
813 #define RAR_SUCCESS(rar) ((rar) & 0x100)
814
815 /**
816  * qbman_swp_release() - Issue a buffer release command
817  * @s:           the software portal object
818  * @d:           the release descriptor
819  * @buffers:     a pointer pointing to the buffer address to be released
820  * @num_buffers: number of buffers to be released,  must be less than 8
821  *
822  * Return 0 for success, -EBUSY if the release command ring is not ready.
823  */
824 int qbman_swp_release(struct qbman_swp *s, const struct qbman_release_desc *d,
825                       const u64 *buffers, unsigned int num_buffers)
826 {
827         int i;
828         struct qbman_release_desc *p;
829         u32 rar;
830
831         if (!num_buffers || (num_buffers > 7))
832                 return -EINVAL;
833
834         rar = qbman_read_register(s, QBMAN_CINH_SWP_RAR);
835         if (!RAR_SUCCESS(rar))
836                 return -EBUSY;
837
838         /* Start the release command */
839         p = qbman_get_cmd(s, QBMAN_CENA_SWP_RCR(RAR_IDX(rar)));
840         /* Copy the caller's buffer pointers to the command */
841         for (i = 0; i < num_buffers; i++)
842                 p->buf[i] = cpu_to_le64(buffers[i]);
843         p->bpid = d->bpid;
844
845         /*
846          * Set the verb byte, have to substitute in the valid-bit and the number
847          * of buffers.
848          */
849         dma_wmb();
850         p->verb = d->verb | RAR_VB(rar) | num_buffers;
851
852         return 0;
853 }
854
855 struct qbman_acquire_desc {
856         u8 verb;
857         u8 reserved;
858         u16 bpid;
859         u8 num;
860         u8 reserved2[59];
861 };
862
863 struct qbman_acquire_rslt {
864         u8 verb;
865         u8 rslt;
866         u16 reserved;
867         u8 num;
868         u8 reserved2[3];
869         u64 buf[7];
870 };
871
872 /**
873  * qbman_swp_acquire() - Issue a buffer acquire command
874  * @s:           the software portal object
875  * @bpid:        the buffer pool index
876  * @buffers:     a pointer pointing to the acquired buffer addresses
877  * @num_buffers: number of buffers to be acquired, must be less than 8
878  *
879  * Return 0 for success, or negative error code if the acquire command
880  * fails.
881  */
882 int qbman_swp_acquire(struct qbman_swp *s, u16 bpid, u64 *buffers,
883                       unsigned int num_buffers)
884 {
885         struct qbman_acquire_desc *p;
886         struct qbman_acquire_rslt *r;
887         int i;
888
889         if (!num_buffers || (num_buffers > 7))
890                 return -EINVAL;
891
892         /* Start the management command */
893         p = qbman_swp_mc_start(s);
894
895         if (!p)
896                 return -EBUSY;
897
898         /* Encode the caller-provided attributes */
899         p->bpid = cpu_to_le16(bpid);
900         p->num = num_buffers;
901
902         /* Complete the management command */
903         r = qbman_swp_mc_complete(s, p, QBMAN_MC_ACQUIRE);
904         if (unlikely(!r)) {
905                 pr_err("qbman: acquire from BPID %d failed, no response\n",
906                        bpid);
907                 return -EIO;
908         }
909
910         /* Decode the outcome */
911         WARN_ON((r->verb & 0x7f) != QBMAN_MC_ACQUIRE);
912
913         /* Determine success or failure */
914         if (unlikely(r->rslt != QBMAN_MC_RSLT_OK)) {
915                 pr_err("qbman: acquire from BPID 0x%x failed, code=0x%02x\n",
916                        bpid, r->rslt);
917                 return -EIO;
918         }
919
920         WARN_ON(r->num > num_buffers);
921
922         /* Copy the acquired buffers to the caller's array */
923         for (i = 0; i < r->num; i++)
924                 buffers[i] = le64_to_cpu(r->buf[i]);
925
926         return (int)r->num;
927 }
928
929 struct qbman_alt_fq_state_desc {
930         u8 verb;
931         u8 reserved[3];
932         u32 fqid;
933         u8 reserved2[56];
934 };
935
936 struct qbman_alt_fq_state_rslt {
937         u8 verb;
938         u8 rslt;
939         u8 reserved[62];
940 };
941
942 #define ALT_FQ_FQID_MASK 0x00FFFFFF
943
944 int qbman_swp_alt_fq_state(struct qbman_swp *s, u32 fqid,
945                            u8 alt_fq_verb)
946 {
947         struct qbman_alt_fq_state_desc *p;
948         struct qbman_alt_fq_state_rslt *r;
949
950         /* Start the management command */
951         p = qbman_swp_mc_start(s);
952         if (!p)
953                 return -EBUSY;
954
955         p->fqid = cpu_to_le32(fqid) & ALT_FQ_FQID_MASK;
956
957         /* Complete the management command */
958         r = qbman_swp_mc_complete(s, p, alt_fq_verb);
959         if (unlikely(!r)) {
960                 pr_err("qbman: mgmt cmd failed, no response (verb=0x%x)\n",
961                        alt_fq_verb);
962                 return -EIO;
963         }
964
965         /* Decode the outcome */
966         WARN_ON((r->verb & QBMAN_RESULT_MASK) != alt_fq_verb);
967
968         /* Determine success or failure */
969         if (unlikely(r->rslt != QBMAN_MC_RSLT_OK)) {
970                 pr_err("qbman: ALT FQID %d failed: verb = 0x%08x code = 0x%02x\n",
971                        fqid, r->verb, r->rslt);
972                 return -EIO;
973         }
974
975         return 0;
976 }
977
978 struct qbman_cdan_ctrl_desc {
979         u8 verb;
980         u8 reserved;
981         u16 ch;
982         u8 we;
983         u8 ctrl;
984         u16 reserved2;
985         u64 cdan_ctx;
986         u8 reserved3[48];
987
988 };
989
990 struct qbman_cdan_ctrl_rslt {
991         u8 verb;
992         u8 rslt;
993         u16 ch;
994         u8 reserved[60];
995 };
996
997 int qbman_swp_CDAN_set(struct qbman_swp *s, u16 channelid,
998                        u8 we_mask, u8 cdan_en,
999                        u64 ctx)
1000 {
1001         struct qbman_cdan_ctrl_desc *p = NULL;
1002         struct qbman_cdan_ctrl_rslt *r = NULL;
1003
1004         /* Start the management command */
1005         p = qbman_swp_mc_start(s);
1006         if (!p)
1007                 return -EBUSY;
1008
1009         /* Encode the caller-provided attributes */
1010         p->ch = cpu_to_le16(channelid);
1011         p->we = we_mask;
1012         if (cdan_en)
1013                 p->ctrl = 1;
1014         else
1015                 p->ctrl = 0;
1016         p->cdan_ctx = cpu_to_le64(ctx);
1017
1018         /* Complete the management command */
1019         r = qbman_swp_mc_complete(s, p, QBMAN_WQCHAN_CONFIGURE);
1020         if (unlikely(!r)) {
1021                 pr_err("qbman: wqchan config failed, no response\n");
1022                 return -EIO;
1023         }
1024
1025         WARN_ON((r->verb & 0x7f) != QBMAN_WQCHAN_CONFIGURE);
1026
1027         /* Determine success or failure */
1028         if (unlikely(r->rslt != QBMAN_MC_RSLT_OK)) {
1029                 pr_err("qbman: CDAN cQID %d failed: code = 0x%02x\n",
1030                        channelid, r->rslt);
1031                 return -EIO;
1032         }
1033
1034         return 0;
1035 }