1*4882a593Smuzhiyun // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright 2014-2016 Freescale Semiconductor Inc.
4*4882a593Smuzhiyun * Copyright 2016-2019 NXP
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun #include <linux/types.h>
8*4882a593Smuzhiyun #include <linux/fsl/mc.h>
9*4882a593Smuzhiyun #include <soc/fsl/dpaa2-io.h>
10*4882a593Smuzhiyun #include <linux/init.h>
11*4882a593Smuzhiyun #include <linux/module.h>
12*4882a593Smuzhiyun #include <linux/platform_device.h>
13*4882a593Smuzhiyun #include <linux/interrupt.h>
14*4882a593Smuzhiyun #include <linux/dma-mapping.h>
15*4882a593Smuzhiyun #include <linux/slab.h>
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun #include "dpio.h"
18*4882a593Smuzhiyun #include "qbman-portal.h"
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun struct dpaa2_io {
21*4882a593Smuzhiyun struct dpaa2_io_desc dpio_desc;
22*4882a593Smuzhiyun struct qbman_swp_desc swp_desc;
23*4882a593Smuzhiyun struct qbman_swp *swp;
24*4882a593Smuzhiyun struct list_head node;
25*4882a593Smuzhiyun /* protect against multiple management commands */
26*4882a593Smuzhiyun spinlock_t lock_mgmt_cmd;
27*4882a593Smuzhiyun /* protect notifications list */
28*4882a593Smuzhiyun spinlock_t lock_notifications;
29*4882a593Smuzhiyun struct list_head notifications;
30*4882a593Smuzhiyun struct device *dev;
31*4882a593Smuzhiyun };
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun struct dpaa2_io_store {
34*4882a593Smuzhiyun unsigned int max;
35*4882a593Smuzhiyun dma_addr_t paddr;
36*4882a593Smuzhiyun struct dpaa2_dq *vaddr;
37*4882a593Smuzhiyun void *alloced_addr; /* unaligned value from kmalloc() */
38*4882a593Smuzhiyun unsigned int idx; /* position of the next-to-be-returned entry */
39*4882a593Smuzhiyun struct qbman_swp *swp; /* portal used to issue VDQCR */
40*4882a593Smuzhiyun struct device *dev; /* device used for DMA mapping */
41*4882a593Smuzhiyun };
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun /* keep a per cpu array of DPIOs for fast access */
44*4882a593Smuzhiyun static struct dpaa2_io *dpio_by_cpu[NR_CPUS];
45*4882a593Smuzhiyun static struct list_head dpio_list = LIST_HEAD_INIT(dpio_list);
46*4882a593Smuzhiyun static DEFINE_SPINLOCK(dpio_list_lock);
47*4882a593Smuzhiyun
service_select_by_cpu(struct dpaa2_io * d,int cpu)48*4882a593Smuzhiyun static inline struct dpaa2_io *service_select_by_cpu(struct dpaa2_io *d,
49*4882a593Smuzhiyun int cpu)
50*4882a593Smuzhiyun {
51*4882a593Smuzhiyun if (d)
52*4882a593Smuzhiyun return d;
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun if (cpu != DPAA2_IO_ANY_CPU && cpu >= num_possible_cpus())
55*4882a593Smuzhiyun return NULL;
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun /*
58*4882a593Smuzhiyun * If cpu == -1, choose the current cpu, with no guarantees about
59*4882a593Smuzhiyun * potentially being migrated away.
60*4882a593Smuzhiyun */
61*4882a593Smuzhiyun if (cpu < 0)
62*4882a593Smuzhiyun cpu = raw_smp_processor_id();
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun /* If a specific cpu was requested, pick it up immediately */
65*4882a593Smuzhiyun return dpio_by_cpu[cpu];
66*4882a593Smuzhiyun }
67*4882a593Smuzhiyun
service_select(struct dpaa2_io * d)68*4882a593Smuzhiyun static inline struct dpaa2_io *service_select(struct dpaa2_io *d)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun if (d)
71*4882a593Smuzhiyun return d;
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun d = service_select_by_cpu(d, -1);
74*4882a593Smuzhiyun if (d)
75*4882a593Smuzhiyun return d;
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun spin_lock(&dpio_list_lock);
78*4882a593Smuzhiyun d = list_entry(dpio_list.next, struct dpaa2_io, node);
79*4882a593Smuzhiyun list_del(&d->node);
80*4882a593Smuzhiyun list_add_tail(&d->node, &dpio_list);
81*4882a593Smuzhiyun spin_unlock(&dpio_list_lock);
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun return d;
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun /**
87*4882a593Smuzhiyun * dpaa2_io_service_select() - return a dpaa2_io service affined to this cpu
88*4882a593Smuzhiyun * @cpu: the cpu id
89*4882a593Smuzhiyun *
90*4882a593Smuzhiyun * Return the affine dpaa2_io service, or NULL if there is no service affined
91*4882a593Smuzhiyun * to the specified cpu. If DPAA2_IO_ANY_CPU is used, return the next available
92*4882a593Smuzhiyun * service.
93*4882a593Smuzhiyun */
dpaa2_io_service_select(int cpu)94*4882a593Smuzhiyun struct dpaa2_io *dpaa2_io_service_select(int cpu)
95*4882a593Smuzhiyun {
96*4882a593Smuzhiyun if (cpu == DPAA2_IO_ANY_CPU)
97*4882a593Smuzhiyun return service_select(NULL);
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun return service_select_by_cpu(NULL, cpu);
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dpaa2_io_service_select);
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun /**
104*4882a593Smuzhiyun * dpaa2_io_create() - create a dpaa2_io object.
105*4882a593Smuzhiyun * @desc: the dpaa2_io descriptor
106*4882a593Smuzhiyun * @dev: the actual DPIO device
107*4882a593Smuzhiyun *
108*4882a593Smuzhiyun * Activates a "struct dpaa2_io" corresponding to the given config of an actual
109*4882a593Smuzhiyun * DPIO object.
110*4882a593Smuzhiyun *
111*4882a593Smuzhiyun * Return a valid dpaa2_io object for success, or NULL for failure.
112*4882a593Smuzhiyun */
dpaa2_io_create(const struct dpaa2_io_desc * desc,struct device * dev)113*4882a593Smuzhiyun struct dpaa2_io *dpaa2_io_create(const struct dpaa2_io_desc *desc,
114*4882a593Smuzhiyun struct device *dev)
115*4882a593Smuzhiyun {
116*4882a593Smuzhiyun struct dpaa2_io *obj = kmalloc(sizeof(*obj), GFP_KERNEL);
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun if (!obj)
119*4882a593Smuzhiyun return NULL;
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun /* check if CPU is out of range (-1 means any cpu) */
122*4882a593Smuzhiyun if (desc->cpu != DPAA2_IO_ANY_CPU && desc->cpu >= num_possible_cpus()) {
123*4882a593Smuzhiyun kfree(obj);
124*4882a593Smuzhiyun return NULL;
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun obj->dpio_desc = *desc;
128*4882a593Smuzhiyun obj->swp_desc.cena_bar = obj->dpio_desc.regs_cena;
129*4882a593Smuzhiyun obj->swp_desc.cinh_bar = obj->dpio_desc.regs_cinh;
130*4882a593Smuzhiyun obj->swp_desc.qman_version = obj->dpio_desc.qman_version;
131*4882a593Smuzhiyun obj->swp = qbman_swp_init(&obj->swp_desc);
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun if (!obj->swp) {
134*4882a593Smuzhiyun kfree(obj);
135*4882a593Smuzhiyun return NULL;
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun INIT_LIST_HEAD(&obj->node);
139*4882a593Smuzhiyun spin_lock_init(&obj->lock_mgmt_cmd);
140*4882a593Smuzhiyun spin_lock_init(&obj->lock_notifications);
141*4882a593Smuzhiyun INIT_LIST_HEAD(&obj->notifications);
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun /* For now only enable DQRR interrupts */
144*4882a593Smuzhiyun qbman_swp_interrupt_set_trigger(obj->swp,
145*4882a593Smuzhiyun QBMAN_SWP_INTERRUPT_DQRI);
146*4882a593Smuzhiyun qbman_swp_interrupt_clear_status(obj->swp, 0xffffffff);
147*4882a593Smuzhiyun if (obj->dpio_desc.receives_notifications)
148*4882a593Smuzhiyun qbman_swp_push_set(obj->swp, 0, 1);
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun spin_lock(&dpio_list_lock);
151*4882a593Smuzhiyun list_add_tail(&obj->node, &dpio_list);
152*4882a593Smuzhiyun if (desc->cpu >= 0 && !dpio_by_cpu[desc->cpu])
153*4882a593Smuzhiyun dpio_by_cpu[desc->cpu] = obj;
154*4882a593Smuzhiyun spin_unlock(&dpio_list_lock);
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun obj->dev = dev;
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun return obj;
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun /**
162*4882a593Smuzhiyun * dpaa2_io_down() - release the dpaa2_io object.
163*4882a593Smuzhiyun * @d: the dpaa2_io object to be released.
164*4882a593Smuzhiyun *
165*4882a593Smuzhiyun * The "struct dpaa2_io" type can represent an individual DPIO object (as
166*4882a593Smuzhiyun * described by "struct dpaa2_io_desc") or an instance of a "DPIO service",
167*4882a593Smuzhiyun * which can be used to group/encapsulate multiple DPIO objects. In all cases,
168*4882a593Smuzhiyun * each handle obtained should be released using this function.
169*4882a593Smuzhiyun */
dpaa2_io_down(struct dpaa2_io * d)170*4882a593Smuzhiyun void dpaa2_io_down(struct dpaa2_io *d)
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun spin_lock(&dpio_list_lock);
173*4882a593Smuzhiyun dpio_by_cpu[d->dpio_desc.cpu] = NULL;
174*4882a593Smuzhiyun list_del(&d->node);
175*4882a593Smuzhiyun spin_unlock(&dpio_list_lock);
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun kfree(d);
178*4882a593Smuzhiyun }
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun #define DPAA_POLL_MAX 32
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun /**
183*4882a593Smuzhiyun * dpaa2_io_irq() - ISR for DPIO interrupts
184*4882a593Smuzhiyun *
185*4882a593Smuzhiyun * @obj: the given DPIO object.
186*4882a593Smuzhiyun *
187*4882a593Smuzhiyun * Return IRQ_HANDLED for success or IRQ_NONE if there
188*4882a593Smuzhiyun * were no pending interrupts.
189*4882a593Smuzhiyun */
dpaa2_io_irq(struct dpaa2_io * obj)190*4882a593Smuzhiyun irqreturn_t dpaa2_io_irq(struct dpaa2_io *obj)
191*4882a593Smuzhiyun {
192*4882a593Smuzhiyun const struct dpaa2_dq *dq;
193*4882a593Smuzhiyun int max = 0;
194*4882a593Smuzhiyun struct qbman_swp *swp;
195*4882a593Smuzhiyun u32 status;
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun swp = obj->swp;
198*4882a593Smuzhiyun status = qbman_swp_interrupt_read_status(swp);
199*4882a593Smuzhiyun if (!status)
200*4882a593Smuzhiyun return IRQ_NONE;
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun dq = qbman_swp_dqrr_next(swp);
203*4882a593Smuzhiyun while (dq) {
204*4882a593Smuzhiyun if (qbman_result_is_SCN(dq)) {
205*4882a593Smuzhiyun struct dpaa2_io_notification_ctx *ctx;
206*4882a593Smuzhiyun u64 q64;
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun q64 = qbman_result_SCN_ctx(dq);
209*4882a593Smuzhiyun ctx = (void *)(uintptr_t)q64;
210*4882a593Smuzhiyun ctx->cb(ctx);
211*4882a593Smuzhiyun } else {
212*4882a593Smuzhiyun pr_crit("fsl-mc-dpio: Unrecognised/ignored DQRR entry\n");
213*4882a593Smuzhiyun }
214*4882a593Smuzhiyun qbman_swp_dqrr_consume(swp, dq);
215*4882a593Smuzhiyun ++max;
216*4882a593Smuzhiyun if (max > DPAA_POLL_MAX)
217*4882a593Smuzhiyun goto done;
218*4882a593Smuzhiyun dq = qbman_swp_dqrr_next(swp);
219*4882a593Smuzhiyun }
220*4882a593Smuzhiyun done:
221*4882a593Smuzhiyun qbman_swp_interrupt_clear_status(swp, status);
222*4882a593Smuzhiyun qbman_swp_interrupt_set_inhibit(swp, 0);
223*4882a593Smuzhiyun return IRQ_HANDLED;
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun /**
227*4882a593Smuzhiyun * dpaa2_io_get_cpu() - get the cpu associated with a given DPIO object
228*4882a593Smuzhiyun *
229*4882a593Smuzhiyun * @d: the given DPIO object.
230*4882a593Smuzhiyun *
231*4882a593Smuzhiyun * Return the cpu associated with the DPIO object
232*4882a593Smuzhiyun */
dpaa2_io_get_cpu(struct dpaa2_io * d)233*4882a593Smuzhiyun int dpaa2_io_get_cpu(struct dpaa2_io *d)
234*4882a593Smuzhiyun {
235*4882a593Smuzhiyun return d->dpio_desc.cpu;
236*4882a593Smuzhiyun }
237*4882a593Smuzhiyun EXPORT_SYMBOL(dpaa2_io_get_cpu);
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun /**
240*4882a593Smuzhiyun * dpaa2_io_service_register() - Prepare for servicing of FQDAN or CDAN
241*4882a593Smuzhiyun * notifications on the given DPIO service.
242*4882a593Smuzhiyun * @d: the given DPIO service.
243*4882a593Smuzhiyun * @ctx: the notification context.
244*4882a593Smuzhiyun * @dev: the device that requests the register
245*4882a593Smuzhiyun *
246*4882a593Smuzhiyun * The caller should make the MC command to attach a DPAA2 object to
247*4882a593Smuzhiyun * a DPIO after this function completes successfully. In that way:
248*4882a593Smuzhiyun * (a) The DPIO service is "ready" to handle a notification arrival
249*4882a593Smuzhiyun * (which might happen before the "attach" command to MC has
250*4882a593Smuzhiyun * returned control of execution back to the caller)
251*4882a593Smuzhiyun * (b) The DPIO service can provide back to the caller the 'dpio_id' and
252*4882a593Smuzhiyun * 'qman64' parameters that it should pass along in the MC command
253*4882a593Smuzhiyun * in order for the object to be configured to produce the right
254*4882a593Smuzhiyun * notification fields to the DPIO service.
255*4882a593Smuzhiyun *
256*4882a593Smuzhiyun * Return 0 for success, or -ENODEV for failure.
257*4882a593Smuzhiyun */
dpaa2_io_service_register(struct dpaa2_io * d,struct dpaa2_io_notification_ctx * ctx,struct device * dev)258*4882a593Smuzhiyun int dpaa2_io_service_register(struct dpaa2_io *d,
259*4882a593Smuzhiyun struct dpaa2_io_notification_ctx *ctx,
260*4882a593Smuzhiyun struct device *dev)
261*4882a593Smuzhiyun {
262*4882a593Smuzhiyun struct device_link *link;
263*4882a593Smuzhiyun unsigned long irqflags;
264*4882a593Smuzhiyun
265*4882a593Smuzhiyun d = service_select_by_cpu(d, ctx->desired_cpu);
266*4882a593Smuzhiyun if (!d)
267*4882a593Smuzhiyun return -ENODEV;
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun link = device_link_add(dev, d->dev, DL_FLAG_AUTOREMOVE_CONSUMER);
270*4882a593Smuzhiyun if (!link)
271*4882a593Smuzhiyun return -EINVAL;
272*4882a593Smuzhiyun
273*4882a593Smuzhiyun ctx->dpio_id = d->dpio_desc.dpio_id;
274*4882a593Smuzhiyun ctx->qman64 = (u64)(uintptr_t)ctx;
275*4882a593Smuzhiyun ctx->dpio_private = d;
276*4882a593Smuzhiyun spin_lock_irqsave(&d->lock_notifications, irqflags);
277*4882a593Smuzhiyun list_add(&ctx->node, &d->notifications);
278*4882a593Smuzhiyun spin_unlock_irqrestore(&d->lock_notifications, irqflags);
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun /* Enable the generation of CDAN notifications */
281*4882a593Smuzhiyun if (ctx->is_cdan)
282*4882a593Smuzhiyun return qbman_swp_CDAN_set_context_enable(d->swp,
283*4882a593Smuzhiyun (u16)ctx->id,
284*4882a593Smuzhiyun ctx->qman64);
285*4882a593Smuzhiyun return 0;
286*4882a593Smuzhiyun }
287*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dpaa2_io_service_register);
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun /**
290*4882a593Smuzhiyun * dpaa2_io_service_deregister - The opposite of 'register'.
291*4882a593Smuzhiyun * @service: the given DPIO service.
292*4882a593Smuzhiyun * @ctx: the notification context.
293*4882a593Smuzhiyun * @dev: the device that requests to be deregistered
294*4882a593Smuzhiyun *
295*4882a593Smuzhiyun * This function should be called only after sending the MC command to
296*4882a593Smuzhiyun * to detach the notification-producing device from the DPIO.
297*4882a593Smuzhiyun */
dpaa2_io_service_deregister(struct dpaa2_io * service,struct dpaa2_io_notification_ctx * ctx,struct device * dev)298*4882a593Smuzhiyun void dpaa2_io_service_deregister(struct dpaa2_io *service,
299*4882a593Smuzhiyun struct dpaa2_io_notification_ctx *ctx,
300*4882a593Smuzhiyun struct device *dev)
301*4882a593Smuzhiyun {
302*4882a593Smuzhiyun struct dpaa2_io *d = ctx->dpio_private;
303*4882a593Smuzhiyun unsigned long irqflags;
304*4882a593Smuzhiyun
305*4882a593Smuzhiyun if (ctx->is_cdan)
306*4882a593Smuzhiyun qbman_swp_CDAN_disable(d->swp, (u16)ctx->id);
307*4882a593Smuzhiyun
308*4882a593Smuzhiyun spin_lock_irqsave(&d->lock_notifications, irqflags);
309*4882a593Smuzhiyun list_del(&ctx->node);
310*4882a593Smuzhiyun spin_unlock_irqrestore(&d->lock_notifications, irqflags);
311*4882a593Smuzhiyun
312*4882a593Smuzhiyun }
313*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dpaa2_io_service_deregister);
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun /**
316*4882a593Smuzhiyun * dpaa2_io_service_rearm() - Rearm the notification for the given DPIO service.
317*4882a593Smuzhiyun * @d: the given DPIO service.
318*4882a593Smuzhiyun * @ctx: the notification context.
319*4882a593Smuzhiyun *
320*4882a593Smuzhiyun * Once a FQDAN/CDAN has been produced, the corresponding FQ/channel is
321*4882a593Smuzhiyun * considered "disarmed". Ie. the user can issue pull dequeue operations on that
322*4882a593Smuzhiyun * traffic source for as long as it likes. Eventually it may wish to "rearm"
323*4882a593Smuzhiyun * that source to allow it to produce another FQDAN/CDAN, that's what this
324*4882a593Smuzhiyun * function achieves.
325*4882a593Smuzhiyun *
326*4882a593Smuzhiyun * Return 0 for success.
327*4882a593Smuzhiyun */
dpaa2_io_service_rearm(struct dpaa2_io * d,struct dpaa2_io_notification_ctx * ctx)328*4882a593Smuzhiyun int dpaa2_io_service_rearm(struct dpaa2_io *d,
329*4882a593Smuzhiyun struct dpaa2_io_notification_ctx *ctx)
330*4882a593Smuzhiyun {
331*4882a593Smuzhiyun unsigned long irqflags;
332*4882a593Smuzhiyun int err;
333*4882a593Smuzhiyun
334*4882a593Smuzhiyun d = service_select_by_cpu(d, ctx->desired_cpu);
335*4882a593Smuzhiyun if (!unlikely(d))
336*4882a593Smuzhiyun return -ENODEV;
337*4882a593Smuzhiyun
338*4882a593Smuzhiyun spin_lock_irqsave(&d->lock_mgmt_cmd, irqflags);
339*4882a593Smuzhiyun if (ctx->is_cdan)
340*4882a593Smuzhiyun err = qbman_swp_CDAN_enable(d->swp, (u16)ctx->id);
341*4882a593Smuzhiyun else
342*4882a593Smuzhiyun err = qbman_swp_fq_schedule(d->swp, ctx->id);
343*4882a593Smuzhiyun spin_unlock_irqrestore(&d->lock_mgmt_cmd, irqflags);
344*4882a593Smuzhiyun
345*4882a593Smuzhiyun return err;
346*4882a593Smuzhiyun }
347*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dpaa2_io_service_rearm);
348*4882a593Smuzhiyun
349*4882a593Smuzhiyun /**
350*4882a593Smuzhiyun * dpaa2_io_service_pull_fq() - pull dequeue functions from a fq.
351*4882a593Smuzhiyun * @d: the given DPIO service.
352*4882a593Smuzhiyun * @fqid: the given frame queue id.
353*4882a593Smuzhiyun * @s: the dpaa2_io_store object for the result.
354*4882a593Smuzhiyun *
355*4882a593Smuzhiyun * Return 0 for success, or error code for failure.
356*4882a593Smuzhiyun */
dpaa2_io_service_pull_fq(struct dpaa2_io * d,u32 fqid,struct dpaa2_io_store * s)357*4882a593Smuzhiyun int dpaa2_io_service_pull_fq(struct dpaa2_io *d, u32 fqid,
358*4882a593Smuzhiyun struct dpaa2_io_store *s)
359*4882a593Smuzhiyun {
360*4882a593Smuzhiyun struct qbman_pull_desc pd;
361*4882a593Smuzhiyun int err;
362*4882a593Smuzhiyun
363*4882a593Smuzhiyun qbman_pull_desc_clear(&pd);
364*4882a593Smuzhiyun qbman_pull_desc_set_storage(&pd, s->vaddr, s->paddr, 1);
365*4882a593Smuzhiyun qbman_pull_desc_set_numframes(&pd, (u8)s->max);
366*4882a593Smuzhiyun qbman_pull_desc_set_fq(&pd, fqid);
367*4882a593Smuzhiyun
368*4882a593Smuzhiyun d = service_select(d);
369*4882a593Smuzhiyun if (!d)
370*4882a593Smuzhiyun return -ENODEV;
371*4882a593Smuzhiyun s->swp = d->swp;
372*4882a593Smuzhiyun err = qbman_swp_pull(d->swp, &pd);
373*4882a593Smuzhiyun if (err)
374*4882a593Smuzhiyun s->swp = NULL;
375*4882a593Smuzhiyun
376*4882a593Smuzhiyun return err;
377*4882a593Smuzhiyun }
378*4882a593Smuzhiyun EXPORT_SYMBOL(dpaa2_io_service_pull_fq);
379*4882a593Smuzhiyun
380*4882a593Smuzhiyun /**
381*4882a593Smuzhiyun * dpaa2_io_service_pull_channel() - pull dequeue functions from a channel.
382*4882a593Smuzhiyun * @d: the given DPIO service.
383*4882a593Smuzhiyun * @channelid: the given channel id.
384*4882a593Smuzhiyun * @s: the dpaa2_io_store object for the result.
385*4882a593Smuzhiyun *
386*4882a593Smuzhiyun * Return 0 for success, or error code for failure.
387*4882a593Smuzhiyun */
dpaa2_io_service_pull_channel(struct dpaa2_io * d,u32 channelid,struct dpaa2_io_store * s)388*4882a593Smuzhiyun int dpaa2_io_service_pull_channel(struct dpaa2_io *d, u32 channelid,
389*4882a593Smuzhiyun struct dpaa2_io_store *s)
390*4882a593Smuzhiyun {
391*4882a593Smuzhiyun struct qbman_pull_desc pd;
392*4882a593Smuzhiyun int err;
393*4882a593Smuzhiyun
394*4882a593Smuzhiyun qbman_pull_desc_clear(&pd);
395*4882a593Smuzhiyun qbman_pull_desc_set_storage(&pd, s->vaddr, s->paddr, 1);
396*4882a593Smuzhiyun qbman_pull_desc_set_numframes(&pd, (u8)s->max);
397*4882a593Smuzhiyun qbman_pull_desc_set_channel(&pd, channelid, qbman_pull_type_prio);
398*4882a593Smuzhiyun
399*4882a593Smuzhiyun d = service_select(d);
400*4882a593Smuzhiyun if (!d)
401*4882a593Smuzhiyun return -ENODEV;
402*4882a593Smuzhiyun
403*4882a593Smuzhiyun s->swp = d->swp;
404*4882a593Smuzhiyun err = qbman_swp_pull(d->swp, &pd);
405*4882a593Smuzhiyun if (err)
406*4882a593Smuzhiyun s->swp = NULL;
407*4882a593Smuzhiyun
408*4882a593Smuzhiyun return err;
409*4882a593Smuzhiyun }
410*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dpaa2_io_service_pull_channel);
411*4882a593Smuzhiyun
412*4882a593Smuzhiyun /**
413*4882a593Smuzhiyun * dpaa2_io_service_enqueue_fq() - Enqueue a frame to a frame queue.
414*4882a593Smuzhiyun * @d: the given DPIO service.
415*4882a593Smuzhiyun * @fqid: the given frame queue id.
416*4882a593Smuzhiyun * @fd: the frame descriptor which is enqueued.
417*4882a593Smuzhiyun *
418*4882a593Smuzhiyun * Return 0 for successful enqueue, -EBUSY if the enqueue ring is not ready,
419*4882a593Smuzhiyun * or -ENODEV if there is no dpio service.
420*4882a593Smuzhiyun */
dpaa2_io_service_enqueue_fq(struct dpaa2_io * d,u32 fqid,const struct dpaa2_fd * fd)421*4882a593Smuzhiyun int dpaa2_io_service_enqueue_fq(struct dpaa2_io *d,
422*4882a593Smuzhiyun u32 fqid,
423*4882a593Smuzhiyun const struct dpaa2_fd *fd)
424*4882a593Smuzhiyun {
425*4882a593Smuzhiyun struct qbman_eq_desc ed;
426*4882a593Smuzhiyun
427*4882a593Smuzhiyun d = service_select(d);
428*4882a593Smuzhiyun if (!d)
429*4882a593Smuzhiyun return -ENODEV;
430*4882a593Smuzhiyun
431*4882a593Smuzhiyun qbman_eq_desc_clear(&ed);
432*4882a593Smuzhiyun qbman_eq_desc_set_no_orp(&ed, 0);
433*4882a593Smuzhiyun qbman_eq_desc_set_fq(&ed, fqid);
434*4882a593Smuzhiyun
435*4882a593Smuzhiyun return qbman_swp_enqueue(d->swp, &ed, fd);
436*4882a593Smuzhiyun }
437*4882a593Smuzhiyun EXPORT_SYMBOL(dpaa2_io_service_enqueue_fq);
438*4882a593Smuzhiyun
439*4882a593Smuzhiyun /**
440*4882a593Smuzhiyun * dpaa2_io_service_enqueue_multiple_fq() - Enqueue multiple frames
441*4882a593Smuzhiyun * to a frame queue using one fqid.
442*4882a593Smuzhiyun * @d: the given DPIO service.
443*4882a593Smuzhiyun * @fqid: the given frame queue id.
444*4882a593Smuzhiyun * @fd: the frame descriptor which is enqueued.
445*4882a593Smuzhiyun * @nb: number of frames to be enqueud
446*4882a593Smuzhiyun *
447*4882a593Smuzhiyun * Return 0 for successful enqueue, -EBUSY if the enqueue ring is not ready,
448*4882a593Smuzhiyun * or -ENODEV if there is no dpio service.
449*4882a593Smuzhiyun */
dpaa2_io_service_enqueue_multiple_fq(struct dpaa2_io * d,u32 fqid,const struct dpaa2_fd * fd,int nb)450*4882a593Smuzhiyun int dpaa2_io_service_enqueue_multiple_fq(struct dpaa2_io *d,
451*4882a593Smuzhiyun u32 fqid,
452*4882a593Smuzhiyun const struct dpaa2_fd *fd,
453*4882a593Smuzhiyun int nb)
454*4882a593Smuzhiyun {
455*4882a593Smuzhiyun struct qbman_eq_desc ed;
456*4882a593Smuzhiyun
457*4882a593Smuzhiyun d = service_select(d);
458*4882a593Smuzhiyun if (!d)
459*4882a593Smuzhiyun return -ENODEV;
460*4882a593Smuzhiyun
461*4882a593Smuzhiyun qbman_eq_desc_clear(&ed);
462*4882a593Smuzhiyun qbman_eq_desc_set_no_orp(&ed, 0);
463*4882a593Smuzhiyun qbman_eq_desc_set_fq(&ed, fqid);
464*4882a593Smuzhiyun
465*4882a593Smuzhiyun return qbman_swp_enqueue_multiple(d->swp, &ed, fd, 0, nb);
466*4882a593Smuzhiyun }
467*4882a593Smuzhiyun EXPORT_SYMBOL(dpaa2_io_service_enqueue_multiple_fq);
468*4882a593Smuzhiyun
469*4882a593Smuzhiyun /**
470*4882a593Smuzhiyun * dpaa2_io_service_enqueue_multiple_desc_fq() - Enqueue multiple frames
471*4882a593Smuzhiyun * to different frame queue using a list of fqids.
472*4882a593Smuzhiyun * @d: the given DPIO service.
473*4882a593Smuzhiyun * @fqid: the given list of frame queue ids.
474*4882a593Smuzhiyun * @fd: the frame descriptor which is enqueued.
475*4882a593Smuzhiyun * @nb: number of frames to be enqueud
476*4882a593Smuzhiyun *
477*4882a593Smuzhiyun * Return 0 for successful enqueue, -EBUSY if the enqueue ring is not ready,
478*4882a593Smuzhiyun * or -ENODEV if there is no dpio service.
479*4882a593Smuzhiyun */
dpaa2_io_service_enqueue_multiple_desc_fq(struct dpaa2_io * d,u32 * fqid,const struct dpaa2_fd * fd,int nb)480*4882a593Smuzhiyun int dpaa2_io_service_enqueue_multiple_desc_fq(struct dpaa2_io *d,
481*4882a593Smuzhiyun u32 *fqid,
482*4882a593Smuzhiyun const struct dpaa2_fd *fd,
483*4882a593Smuzhiyun int nb)
484*4882a593Smuzhiyun {
485*4882a593Smuzhiyun struct qbman_eq_desc *ed;
486*4882a593Smuzhiyun int i, ret;
487*4882a593Smuzhiyun
488*4882a593Smuzhiyun ed = kcalloc(sizeof(struct qbman_eq_desc), 32, GFP_KERNEL);
489*4882a593Smuzhiyun if (!ed)
490*4882a593Smuzhiyun return -ENOMEM;
491*4882a593Smuzhiyun
492*4882a593Smuzhiyun d = service_select(d);
493*4882a593Smuzhiyun if (!d) {
494*4882a593Smuzhiyun ret = -ENODEV;
495*4882a593Smuzhiyun goto out;
496*4882a593Smuzhiyun }
497*4882a593Smuzhiyun
498*4882a593Smuzhiyun for (i = 0; i < nb; i++) {
499*4882a593Smuzhiyun qbman_eq_desc_clear(&ed[i]);
500*4882a593Smuzhiyun qbman_eq_desc_set_no_orp(&ed[i], 0);
501*4882a593Smuzhiyun qbman_eq_desc_set_fq(&ed[i], fqid[i]);
502*4882a593Smuzhiyun }
503*4882a593Smuzhiyun
504*4882a593Smuzhiyun ret = qbman_swp_enqueue_multiple_desc(d->swp, &ed[0], fd, nb);
505*4882a593Smuzhiyun out:
506*4882a593Smuzhiyun kfree(ed);
507*4882a593Smuzhiyun return ret;
508*4882a593Smuzhiyun }
509*4882a593Smuzhiyun EXPORT_SYMBOL(dpaa2_io_service_enqueue_multiple_desc_fq);
510*4882a593Smuzhiyun
511*4882a593Smuzhiyun /**
512*4882a593Smuzhiyun * dpaa2_io_service_enqueue_qd() - Enqueue a frame to a QD.
513*4882a593Smuzhiyun * @d: the given DPIO service.
514*4882a593Smuzhiyun * @qdid: the given queuing destination id.
515*4882a593Smuzhiyun * @prio: the given queuing priority.
516*4882a593Smuzhiyun * @qdbin: the given queuing destination bin.
517*4882a593Smuzhiyun * @fd: the frame descriptor which is enqueued.
518*4882a593Smuzhiyun *
519*4882a593Smuzhiyun * Return 0 for successful enqueue, or -EBUSY if the enqueue ring is not ready,
520*4882a593Smuzhiyun * or -ENODEV if there is no dpio service.
521*4882a593Smuzhiyun */
dpaa2_io_service_enqueue_qd(struct dpaa2_io * d,u32 qdid,u8 prio,u16 qdbin,const struct dpaa2_fd * fd)522*4882a593Smuzhiyun int dpaa2_io_service_enqueue_qd(struct dpaa2_io *d,
523*4882a593Smuzhiyun u32 qdid, u8 prio, u16 qdbin,
524*4882a593Smuzhiyun const struct dpaa2_fd *fd)
525*4882a593Smuzhiyun {
526*4882a593Smuzhiyun struct qbman_eq_desc ed;
527*4882a593Smuzhiyun
528*4882a593Smuzhiyun d = service_select(d);
529*4882a593Smuzhiyun if (!d)
530*4882a593Smuzhiyun return -ENODEV;
531*4882a593Smuzhiyun
532*4882a593Smuzhiyun qbman_eq_desc_clear(&ed);
533*4882a593Smuzhiyun qbman_eq_desc_set_no_orp(&ed, 0);
534*4882a593Smuzhiyun qbman_eq_desc_set_qd(&ed, qdid, qdbin, prio);
535*4882a593Smuzhiyun
536*4882a593Smuzhiyun return qbman_swp_enqueue(d->swp, &ed, fd);
537*4882a593Smuzhiyun }
538*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dpaa2_io_service_enqueue_qd);
539*4882a593Smuzhiyun
540*4882a593Smuzhiyun /**
541*4882a593Smuzhiyun * dpaa2_io_service_release() - Release buffers to a buffer pool.
542*4882a593Smuzhiyun * @d: the given DPIO object.
543*4882a593Smuzhiyun * @bpid: the buffer pool id.
544*4882a593Smuzhiyun * @buffers: the buffers to be released.
545*4882a593Smuzhiyun * @num_buffers: the number of the buffers to be released.
546*4882a593Smuzhiyun *
547*4882a593Smuzhiyun * Return 0 for success, and negative error code for failure.
548*4882a593Smuzhiyun */
dpaa2_io_service_release(struct dpaa2_io * d,u16 bpid,const u64 * buffers,unsigned int num_buffers)549*4882a593Smuzhiyun int dpaa2_io_service_release(struct dpaa2_io *d,
550*4882a593Smuzhiyun u16 bpid,
551*4882a593Smuzhiyun const u64 *buffers,
552*4882a593Smuzhiyun unsigned int num_buffers)
553*4882a593Smuzhiyun {
554*4882a593Smuzhiyun struct qbman_release_desc rd;
555*4882a593Smuzhiyun
556*4882a593Smuzhiyun d = service_select(d);
557*4882a593Smuzhiyun if (!d)
558*4882a593Smuzhiyun return -ENODEV;
559*4882a593Smuzhiyun
560*4882a593Smuzhiyun qbman_release_desc_clear(&rd);
561*4882a593Smuzhiyun qbman_release_desc_set_bpid(&rd, bpid);
562*4882a593Smuzhiyun
563*4882a593Smuzhiyun return qbman_swp_release(d->swp, &rd, buffers, num_buffers);
564*4882a593Smuzhiyun }
565*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dpaa2_io_service_release);
566*4882a593Smuzhiyun
567*4882a593Smuzhiyun /**
568*4882a593Smuzhiyun * dpaa2_io_service_acquire() - Acquire buffers from a buffer pool.
569*4882a593Smuzhiyun * @d: the given DPIO object.
570*4882a593Smuzhiyun * @bpid: the buffer pool id.
571*4882a593Smuzhiyun * @buffers: the buffer addresses for acquired buffers.
572*4882a593Smuzhiyun * @num_buffers: the expected number of the buffers to acquire.
573*4882a593Smuzhiyun *
574*4882a593Smuzhiyun * Return a negative error code if the command failed, otherwise it returns
575*4882a593Smuzhiyun * the number of buffers acquired, which may be less than the number requested.
576*4882a593Smuzhiyun * Eg. if the buffer pool is empty, this will return zero.
577*4882a593Smuzhiyun */
dpaa2_io_service_acquire(struct dpaa2_io * d,u16 bpid,u64 * buffers,unsigned int num_buffers)578*4882a593Smuzhiyun int dpaa2_io_service_acquire(struct dpaa2_io *d,
579*4882a593Smuzhiyun u16 bpid,
580*4882a593Smuzhiyun u64 *buffers,
581*4882a593Smuzhiyun unsigned int num_buffers)
582*4882a593Smuzhiyun {
583*4882a593Smuzhiyun unsigned long irqflags;
584*4882a593Smuzhiyun int err;
585*4882a593Smuzhiyun
586*4882a593Smuzhiyun d = service_select(d);
587*4882a593Smuzhiyun if (!d)
588*4882a593Smuzhiyun return -ENODEV;
589*4882a593Smuzhiyun
590*4882a593Smuzhiyun spin_lock_irqsave(&d->lock_mgmt_cmd, irqflags);
591*4882a593Smuzhiyun err = qbman_swp_acquire(d->swp, bpid, buffers, num_buffers);
592*4882a593Smuzhiyun spin_unlock_irqrestore(&d->lock_mgmt_cmd, irqflags);
593*4882a593Smuzhiyun
594*4882a593Smuzhiyun return err;
595*4882a593Smuzhiyun }
596*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dpaa2_io_service_acquire);
597*4882a593Smuzhiyun
598*4882a593Smuzhiyun /*
599*4882a593Smuzhiyun * 'Stores' are reusable memory blocks for holding dequeue results, and to
600*4882a593Smuzhiyun * assist with parsing those results.
601*4882a593Smuzhiyun */
602*4882a593Smuzhiyun
603*4882a593Smuzhiyun /**
604*4882a593Smuzhiyun * dpaa2_io_store_create() - Create the dma memory storage for dequeue result.
605*4882a593Smuzhiyun * @max_frames: the maximum number of dequeued result for frames, must be <= 32.
606*4882a593Smuzhiyun * @dev: the device to allow mapping/unmapping the DMAable region.
607*4882a593Smuzhiyun *
608*4882a593Smuzhiyun * The size of the storage is "max_frames*sizeof(struct dpaa2_dq)".
609*4882a593Smuzhiyun * The 'dpaa2_io_store' returned is a DPIO service managed object.
610*4882a593Smuzhiyun *
611*4882a593Smuzhiyun * Return pointer to dpaa2_io_store struct for successfully created storage
612*4882a593Smuzhiyun * memory, or NULL on error.
613*4882a593Smuzhiyun */
dpaa2_io_store_create(unsigned int max_frames,struct device * dev)614*4882a593Smuzhiyun struct dpaa2_io_store *dpaa2_io_store_create(unsigned int max_frames,
615*4882a593Smuzhiyun struct device *dev)
616*4882a593Smuzhiyun {
617*4882a593Smuzhiyun struct dpaa2_io_store *ret;
618*4882a593Smuzhiyun size_t size;
619*4882a593Smuzhiyun
620*4882a593Smuzhiyun if (!max_frames || (max_frames > 32))
621*4882a593Smuzhiyun return NULL;
622*4882a593Smuzhiyun
623*4882a593Smuzhiyun ret = kmalloc(sizeof(*ret), GFP_KERNEL);
624*4882a593Smuzhiyun if (!ret)
625*4882a593Smuzhiyun return NULL;
626*4882a593Smuzhiyun
627*4882a593Smuzhiyun ret->max = max_frames;
628*4882a593Smuzhiyun size = max_frames * sizeof(struct dpaa2_dq) + 64;
629*4882a593Smuzhiyun ret->alloced_addr = kzalloc(size, GFP_KERNEL);
630*4882a593Smuzhiyun if (!ret->alloced_addr) {
631*4882a593Smuzhiyun kfree(ret);
632*4882a593Smuzhiyun return NULL;
633*4882a593Smuzhiyun }
634*4882a593Smuzhiyun
635*4882a593Smuzhiyun ret->vaddr = PTR_ALIGN(ret->alloced_addr, 64);
636*4882a593Smuzhiyun ret->paddr = dma_map_single(dev, ret->vaddr,
637*4882a593Smuzhiyun sizeof(struct dpaa2_dq) * max_frames,
638*4882a593Smuzhiyun DMA_FROM_DEVICE);
639*4882a593Smuzhiyun if (dma_mapping_error(dev, ret->paddr)) {
640*4882a593Smuzhiyun kfree(ret->alloced_addr);
641*4882a593Smuzhiyun kfree(ret);
642*4882a593Smuzhiyun return NULL;
643*4882a593Smuzhiyun }
644*4882a593Smuzhiyun
645*4882a593Smuzhiyun ret->idx = 0;
646*4882a593Smuzhiyun ret->dev = dev;
647*4882a593Smuzhiyun
648*4882a593Smuzhiyun return ret;
649*4882a593Smuzhiyun }
650*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dpaa2_io_store_create);
651*4882a593Smuzhiyun
652*4882a593Smuzhiyun /**
653*4882a593Smuzhiyun * dpaa2_io_store_destroy() - Frees the dma memory storage for dequeue
654*4882a593Smuzhiyun * result.
655*4882a593Smuzhiyun * @s: the storage memory to be destroyed.
656*4882a593Smuzhiyun */
dpaa2_io_store_destroy(struct dpaa2_io_store * s)657*4882a593Smuzhiyun void dpaa2_io_store_destroy(struct dpaa2_io_store *s)
658*4882a593Smuzhiyun {
659*4882a593Smuzhiyun dma_unmap_single(s->dev, s->paddr, sizeof(struct dpaa2_dq) * s->max,
660*4882a593Smuzhiyun DMA_FROM_DEVICE);
661*4882a593Smuzhiyun kfree(s->alloced_addr);
662*4882a593Smuzhiyun kfree(s);
663*4882a593Smuzhiyun }
664*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dpaa2_io_store_destroy);
665*4882a593Smuzhiyun
666*4882a593Smuzhiyun /**
667*4882a593Smuzhiyun * dpaa2_io_store_next() - Determine when the next dequeue result is available.
668*4882a593Smuzhiyun * @s: the dpaa2_io_store object.
669*4882a593Smuzhiyun * @is_last: indicate whether this is the last frame in the pull command.
670*4882a593Smuzhiyun *
671*4882a593Smuzhiyun * When an object driver performs dequeues to a dpaa2_io_store, this function
672*4882a593Smuzhiyun * can be used to determine when the next frame result is available. Once
673*4882a593Smuzhiyun * this function returns non-NULL, a subsequent call to it will try to find
674*4882a593Smuzhiyun * the next dequeue result.
675*4882a593Smuzhiyun *
676*4882a593Smuzhiyun * Note that if a pull-dequeue has a NULL result because the target FQ/channel
677*4882a593Smuzhiyun * was empty, then this function will also return NULL (rather than expecting
678*4882a593Smuzhiyun * the caller to always check for this. As such, "is_last" can be used to
679*4882a593Smuzhiyun * differentiate between "end-of-empty-dequeue" and "still-waiting".
680*4882a593Smuzhiyun *
681*4882a593Smuzhiyun * Return dequeue result for a valid dequeue result, or NULL for empty dequeue.
682*4882a593Smuzhiyun */
dpaa2_io_store_next(struct dpaa2_io_store * s,int * is_last)683*4882a593Smuzhiyun struct dpaa2_dq *dpaa2_io_store_next(struct dpaa2_io_store *s, int *is_last)
684*4882a593Smuzhiyun {
685*4882a593Smuzhiyun int match;
686*4882a593Smuzhiyun struct dpaa2_dq *ret = &s->vaddr[s->idx];
687*4882a593Smuzhiyun
688*4882a593Smuzhiyun match = qbman_result_has_new_result(s->swp, ret);
689*4882a593Smuzhiyun if (!match) {
690*4882a593Smuzhiyun *is_last = 0;
691*4882a593Smuzhiyun return NULL;
692*4882a593Smuzhiyun }
693*4882a593Smuzhiyun
694*4882a593Smuzhiyun s->idx++;
695*4882a593Smuzhiyun
696*4882a593Smuzhiyun if (dpaa2_dq_is_pull_complete(ret)) {
697*4882a593Smuzhiyun *is_last = 1;
698*4882a593Smuzhiyun s->idx = 0;
699*4882a593Smuzhiyun /*
700*4882a593Smuzhiyun * If we get an empty dequeue result to terminate a zero-results
701*4882a593Smuzhiyun * vdqcr, return NULL to the caller rather than expecting him to
702*4882a593Smuzhiyun * check non-NULL results every time.
703*4882a593Smuzhiyun */
704*4882a593Smuzhiyun if (!(dpaa2_dq_flags(ret) & DPAA2_DQ_STAT_VALIDFRAME))
705*4882a593Smuzhiyun ret = NULL;
706*4882a593Smuzhiyun } else {
707*4882a593Smuzhiyun prefetch(&s->vaddr[s->idx]);
708*4882a593Smuzhiyun *is_last = 0;
709*4882a593Smuzhiyun }
710*4882a593Smuzhiyun
711*4882a593Smuzhiyun return ret;
712*4882a593Smuzhiyun }
713*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dpaa2_io_store_next);
714*4882a593Smuzhiyun
715*4882a593Smuzhiyun /**
716*4882a593Smuzhiyun * dpaa2_io_query_fq_count() - Get the frame and byte count for a given fq.
717*4882a593Smuzhiyun * @d: the given DPIO object.
718*4882a593Smuzhiyun * @fqid: the id of frame queue to be queried.
719*4882a593Smuzhiyun * @fcnt: the queried frame count.
720*4882a593Smuzhiyun * @bcnt: the queried byte count.
721*4882a593Smuzhiyun *
722*4882a593Smuzhiyun * Knowing the FQ count at run-time can be useful in debugging situations.
723*4882a593Smuzhiyun * The instantaneous frame- and byte-count are hereby returned.
724*4882a593Smuzhiyun *
725*4882a593Smuzhiyun * Return 0 for a successful query, and negative error code if query fails.
726*4882a593Smuzhiyun */
dpaa2_io_query_fq_count(struct dpaa2_io * d,u32 fqid,u32 * fcnt,u32 * bcnt)727*4882a593Smuzhiyun int dpaa2_io_query_fq_count(struct dpaa2_io *d, u32 fqid,
728*4882a593Smuzhiyun u32 *fcnt, u32 *bcnt)
729*4882a593Smuzhiyun {
730*4882a593Smuzhiyun struct qbman_fq_query_np_rslt state;
731*4882a593Smuzhiyun struct qbman_swp *swp;
732*4882a593Smuzhiyun unsigned long irqflags;
733*4882a593Smuzhiyun int ret;
734*4882a593Smuzhiyun
735*4882a593Smuzhiyun d = service_select(d);
736*4882a593Smuzhiyun if (!d)
737*4882a593Smuzhiyun return -ENODEV;
738*4882a593Smuzhiyun
739*4882a593Smuzhiyun swp = d->swp;
740*4882a593Smuzhiyun spin_lock_irqsave(&d->lock_mgmt_cmd, irqflags);
741*4882a593Smuzhiyun ret = qbman_fq_query_state(swp, fqid, &state);
742*4882a593Smuzhiyun spin_unlock_irqrestore(&d->lock_mgmt_cmd, irqflags);
743*4882a593Smuzhiyun if (ret)
744*4882a593Smuzhiyun return ret;
745*4882a593Smuzhiyun *fcnt = qbman_fq_state_frame_count(&state);
746*4882a593Smuzhiyun *bcnt = qbman_fq_state_byte_count(&state);
747*4882a593Smuzhiyun
748*4882a593Smuzhiyun return 0;
749*4882a593Smuzhiyun }
750*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dpaa2_io_query_fq_count);
751*4882a593Smuzhiyun
752*4882a593Smuzhiyun /**
753*4882a593Smuzhiyun * dpaa2_io_query_bp_count() - Query the number of buffers currently in a
754*4882a593Smuzhiyun * buffer pool.
755*4882a593Smuzhiyun * @d: the given DPIO object.
756*4882a593Smuzhiyun * @bpid: the index of buffer pool to be queried.
757*4882a593Smuzhiyun * @num: the queried number of buffers in the buffer pool.
758*4882a593Smuzhiyun *
759*4882a593Smuzhiyun * Return 0 for a successful query, and negative error code if query fails.
760*4882a593Smuzhiyun */
dpaa2_io_query_bp_count(struct dpaa2_io * d,u16 bpid,u32 * num)761*4882a593Smuzhiyun int dpaa2_io_query_bp_count(struct dpaa2_io *d, u16 bpid, u32 *num)
762*4882a593Smuzhiyun {
763*4882a593Smuzhiyun struct qbman_bp_query_rslt state;
764*4882a593Smuzhiyun struct qbman_swp *swp;
765*4882a593Smuzhiyun unsigned long irqflags;
766*4882a593Smuzhiyun int ret;
767*4882a593Smuzhiyun
768*4882a593Smuzhiyun d = service_select(d);
769*4882a593Smuzhiyun if (!d)
770*4882a593Smuzhiyun return -ENODEV;
771*4882a593Smuzhiyun
772*4882a593Smuzhiyun swp = d->swp;
773*4882a593Smuzhiyun spin_lock_irqsave(&d->lock_mgmt_cmd, irqflags);
774*4882a593Smuzhiyun ret = qbman_bp_query(swp, bpid, &state);
775*4882a593Smuzhiyun spin_unlock_irqrestore(&d->lock_mgmt_cmd, irqflags);
776*4882a593Smuzhiyun if (ret)
777*4882a593Smuzhiyun return ret;
778*4882a593Smuzhiyun *num = qbman_bp_info_num_free_bufs(&state);
779*4882a593Smuzhiyun return 0;
780*4882a593Smuzhiyun }
781*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dpaa2_io_query_bp_count);
782