18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright 2014-2016 Freescale Semiconductor Inc.
48c2ecf20Sopenharmony_ci * Copyright 2016-2019 NXP
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci#include <linux/types.h>
88c2ecf20Sopenharmony_ci#include <linux/fsl/mc.h>
98c2ecf20Sopenharmony_ci#include <soc/fsl/dpaa2-io.h>
108c2ecf20Sopenharmony_ci#include <linux/init.h>
118c2ecf20Sopenharmony_ci#include <linux/module.h>
128c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
138c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
148c2ecf20Sopenharmony_ci#include <linux/dma-mapping.h>
158c2ecf20Sopenharmony_ci#include <linux/slab.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include "dpio.h"
188c2ecf20Sopenharmony_ci#include "qbman-portal.h"
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cistruct dpaa2_io {
218c2ecf20Sopenharmony_ci	struct dpaa2_io_desc dpio_desc;
228c2ecf20Sopenharmony_ci	struct qbman_swp_desc swp_desc;
238c2ecf20Sopenharmony_ci	struct qbman_swp *swp;
248c2ecf20Sopenharmony_ci	struct list_head node;
258c2ecf20Sopenharmony_ci	/* protect against multiple management commands */
268c2ecf20Sopenharmony_ci	spinlock_t lock_mgmt_cmd;
278c2ecf20Sopenharmony_ci	/* protect notifications list */
288c2ecf20Sopenharmony_ci	spinlock_t lock_notifications;
298c2ecf20Sopenharmony_ci	struct list_head notifications;
308c2ecf20Sopenharmony_ci	struct device *dev;
318c2ecf20Sopenharmony_ci};
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_cistruct dpaa2_io_store {
348c2ecf20Sopenharmony_ci	unsigned int max;
358c2ecf20Sopenharmony_ci	dma_addr_t paddr;
368c2ecf20Sopenharmony_ci	struct dpaa2_dq *vaddr;
378c2ecf20Sopenharmony_ci	void *alloced_addr;    /* unaligned value from kmalloc() */
388c2ecf20Sopenharmony_ci	unsigned int idx;      /* position of the next-to-be-returned entry */
398c2ecf20Sopenharmony_ci	struct qbman_swp *swp; /* portal used to issue VDQCR */
408c2ecf20Sopenharmony_ci	struct device *dev;    /* device used for DMA mapping */
418c2ecf20Sopenharmony_ci};
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci/* keep a per cpu array of DPIOs for fast access */
448c2ecf20Sopenharmony_cistatic struct dpaa2_io *dpio_by_cpu[NR_CPUS];
458c2ecf20Sopenharmony_cistatic struct list_head dpio_list = LIST_HEAD_INIT(dpio_list);
468c2ecf20Sopenharmony_cistatic DEFINE_SPINLOCK(dpio_list_lock);
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cistatic inline struct dpaa2_io *service_select_by_cpu(struct dpaa2_io *d,
498c2ecf20Sopenharmony_ci						     int cpu)
508c2ecf20Sopenharmony_ci{
518c2ecf20Sopenharmony_ci	if (d)
528c2ecf20Sopenharmony_ci		return d;
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	if (cpu != DPAA2_IO_ANY_CPU && cpu >= num_possible_cpus())
558c2ecf20Sopenharmony_ci		return NULL;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	/*
588c2ecf20Sopenharmony_ci	 * If cpu == -1, choose the current cpu, with no guarantees about
598c2ecf20Sopenharmony_ci	 * potentially being migrated away.
608c2ecf20Sopenharmony_ci	 */
618c2ecf20Sopenharmony_ci	if (cpu < 0)
628c2ecf20Sopenharmony_ci		cpu = raw_smp_processor_id();
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	/* If a specific cpu was requested, pick it up immediately */
658c2ecf20Sopenharmony_ci	return dpio_by_cpu[cpu];
668c2ecf20Sopenharmony_ci}
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cistatic inline struct dpaa2_io *service_select(struct dpaa2_io *d)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	if (d)
718c2ecf20Sopenharmony_ci		return d;
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	d = service_select_by_cpu(d, -1);
748c2ecf20Sopenharmony_ci	if (d)
758c2ecf20Sopenharmony_ci		return d;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	spin_lock(&dpio_list_lock);
788c2ecf20Sopenharmony_ci	d = list_entry(dpio_list.next, struct dpaa2_io, node);
798c2ecf20Sopenharmony_ci	list_del(&d->node);
808c2ecf20Sopenharmony_ci	list_add_tail(&d->node, &dpio_list);
818c2ecf20Sopenharmony_ci	spin_unlock(&dpio_list_lock);
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	return d;
848c2ecf20Sopenharmony_ci}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci/**
878c2ecf20Sopenharmony_ci * dpaa2_io_service_select() - return a dpaa2_io service affined to this cpu
888c2ecf20Sopenharmony_ci * @cpu: the cpu id
898c2ecf20Sopenharmony_ci *
908c2ecf20Sopenharmony_ci * Return the affine dpaa2_io service, or NULL if there is no service affined
918c2ecf20Sopenharmony_ci * to the specified cpu. If DPAA2_IO_ANY_CPU is used, return the next available
928c2ecf20Sopenharmony_ci * service.
938c2ecf20Sopenharmony_ci */
948c2ecf20Sopenharmony_cistruct dpaa2_io *dpaa2_io_service_select(int cpu)
958c2ecf20Sopenharmony_ci{
968c2ecf20Sopenharmony_ci	if (cpu == DPAA2_IO_ANY_CPU)
978c2ecf20Sopenharmony_ci		return service_select(NULL);
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	return service_select_by_cpu(NULL, cpu);
1008c2ecf20Sopenharmony_ci}
1018c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(dpaa2_io_service_select);
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci/**
1048c2ecf20Sopenharmony_ci * dpaa2_io_create() - create a dpaa2_io object.
1058c2ecf20Sopenharmony_ci * @desc: the dpaa2_io descriptor
1068c2ecf20Sopenharmony_ci * @dev: the actual DPIO device
1078c2ecf20Sopenharmony_ci *
1088c2ecf20Sopenharmony_ci * Activates a "struct dpaa2_io" corresponding to the given config of an actual
1098c2ecf20Sopenharmony_ci * DPIO object.
1108c2ecf20Sopenharmony_ci *
1118c2ecf20Sopenharmony_ci * Return a valid dpaa2_io object for success, or NULL for failure.
1128c2ecf20Sopenharmony_ci */
1138c2ecf20Sopenharmony_cistruct dpaa2_io *dpaa2_io_create(const struct dpaa2_io_desc *desc,
1148c2ecf20Sopenharmony_ci				 struct device *dev)
1158c2ecf20Sopenharmony_ci{
1168c2ecf20Sopenharmony_ci	struct dpaa2_io *obj = kmalloc(sizeof(*obj), GFP_KERNEL);
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	if (!obj)
1198c2ecf20Sopenharmony_ci		return NULL;
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	/* check if CPU is out of range (-1 means any cpu) */
1228c2ecf20Sopenharmony_ci	if (desc->cpu != DPAA2_IO_ANY_CPU && desc->cpu >= num_possible_cpus()) {
1238c2ecf20Sopenharmony_ci		kfree(obj);
1248c2ecf20Sopenharmony_ci		return NULL;
1258c2ecf20Sopenharmony_ci	}
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	obj->dpio_desc = *desc;
1288c2ecf20Sopenharmony_ci	obj->swp_desc.cena_bar = obj->dpio_desc.regs_cena;
1298c2ecf20Sopenharmony_ci	obj->swp_desc.cinh_bar = obj->dpio_desc.regs_cinh;
1308c2ecf20Sopenharmony_ci	obj->swp_desc.qman_version = obj->dpio_desc.qman_version;
1318c2ecf20Sopenharmony_ci	obj->swp = qbman_swp_init(&obj->swp_desc);
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	if (!obj->swp) {
1348c2ecf20Sopenharmony_ci		kfree(obj);
1358c2ecf20Sopenharmony_ci		return NULL;
1368c2ecf20Sopenharmony_ci	}
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&obj->node);
1398c2ecf20Sopenharmony_ci	spin_lock_init(&obj->lock_mgmt_cmd);
1408c2ecf20Sopenharmony_ci	spin_lock_init(&obj->lock_notifications);
1418c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&obj->notifications);
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	/* For now only enable DQRR interrupts */
1448c2ecf20Sopenharmony_ci	qbman_swp_interrupt_set_trigger(obj->swp,
1458c2ecf20Sopenharmony_ci					QBMAN_SWP_INTERRUPT_DQRI);
1468c2ecf20Sopenharmony_ci	qbman_swp_interrupt_clear_status(obj->swp, 0xffffffff);
1478c2ecf20Sopenharmony_ci	if (obj->dpio_desc.receives_notifications)
1488c2ecf20Sopenharmony_ci		qbman_swp_push_set(obj->swp, 0, 1);
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	spin_lock(&dpio_list_lock);
1518c2ecf20Sopenharmony_ci	list_add_tail(&obj->node, &dpio_list);
1528c2ecf20Sopenharmony_ci	if (desc->cpu >= 0 && !dpio_by_cpu[desc->cpu])
1538c2ecf20Sopenharmony_ci		dpio_by_cpu[desc->cpu] = obj;
1548c2ecf20Sopenharmony_ci	spin_unlock(&dpio_list_lock);
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	obj->dev = dev;
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	return obj;
1598c2ecf20Sopenharmony_ci}
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci/**
1628c2ecf20Sopenharmony_ci * dpaa2_io_down() - release the dpaa2_io object.
1638c2ecf20Sopenharmony_ci * @d: the dpaa2_io object to be released.
1648c2ecf20Sopenharmony_ci *
1658c2ecf20Sopenharmony_ci * The "struct dpaa2_io" type can represent an individual DPIO object (as
1668c2ecf20Sopenharmony_ci * described by "struct dpaa2_io_desc") or an instance of a "DPIO service",
1678c2ecf20Sopenharmony_ci * which can be used to group/encapsulate multiple DPIO objects. In all cases,
1688c2ecf20Sopenharmony_ci * each handle obtained should be released using this function.
1698c2ecf20Sopenharmony_ci */
1708c2ecf20Sopenharmony_civoid dpaa2_io_down(struct dpaa2_io *d)
1718c2ecf20Sopenharmony_ci{
1728c2ecf20Sopenharmony_ci	spin_lock(&dpio_list_lock);
1738c2ecf20Sopenharmony_ci	dpio_by_cpu[d->dpio_desc.cpu] = NULL;
1748c2ecf20Sopenharmony_ci	list_del(&d->node);
1758c2ecf20Sopenharmony_ci	spin_unlock(&dpio_list_lock);
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	kfree(d);
1788c2ecf20Sopenharmony_ci}
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci#define DPAA_POLL_MAX 32
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci/**
1838c2ecf20Sopenharmony_ci * dpaa2_io_irq() - ISR for DPIO interrupts
1848c2ecf20Sopenharmony_ci *
1858c2ecf20Sopenharmony_ci * @obj: the given DPIO object.
1868c2ecf20Sopenharmony_ci *
1878c2ecf20Sopenharmony_ci * Return IRQ_HANDLED for success or IRQ_NONE if there
1888c2ecf20Sopenharmony_ci * were no pending interrupts.
1898c2ecf20Sopenharmony_ci */
1908c2ecf20Sopenharmony_ciirqreturn_t dpaa2_io_irq(struct dpaa2_io *obj)
1918c2ecf20Sopenharmony_ci{
1928c2ecf20Sopenharmony_ci	const struct dpaa2_dq *dq;
1938c2ecf20Sopenharmony_ci	int max = 0;
1948c2ecf20Sopenharmony_ci	struct qbman_swp *swp;
1958c2ecf20Sopenharmony_ci	u32 status;
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	swp = obj->swp;
1988c2ecf20Sopenharmony_ci	status = qbman_swp_interrupt_read_status(swp);
1998c2ecf20Sopenharmony_ci	if (!status)
2008c2ecf20Sopenharmony_ci		return IRQ_NONE;
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	dq = qbman_swp_dqrr_next(swp);
2038c2ecf20Sopenharmony_ci	while (dq) {
2048c2ecf20Sopenharmony_ci		if (qbman_result_is_SCN(dq)) {
2058c2ecf20Sopenharmony_ci			struct dpaa2_io_notification_ctx *ctx;
2068c2ecf20Sopenharmony_ci			u64 q64;
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci			q64 = qbman_result_SCN_ctx(dq);
2098c2ecf20Sopenharmony_ci			ctx = (void *)(uintptr_t)q64;
2108c2ecf20Sopenharmony_ci			ctx->cb(ctx);
2118c2ecf20Sopenharmony_ci		} else {
2128c2ecf20Sopenharmony_ci			pr_crit("fsl-mc-dpio: Unrecognised/ignored DQRR entry\n");
2138c2ecf20Sopenharmony_ci		}
2148c2ecf20Sopenharmony_ci		qbman_swp_dqrr_consume(swp, dq);
2158c2ecf20Sopenharmony_ci		++max;
2168c2ecf20Sopenharmony_ci		if (max > DPAA_POLL_MAX)
2178c2ecf20Sopenharmony_ci			goto done;
2188c2ecf20Sopenharmony_ci		dq = qbman_swp_dqrr_next(swp);
2198c2ecf20Sopenharmony_ci	}
2208c2ecf20Sopenharmony_cidone:
2218c2ecf20Sopenharmony_ci	qbman_swp_interrupt_clear_status(swp, status);
2228c2ecf20Sopenharmony_ci	qbman_swp_interrupt_set_inhibit(swp, 0);
2238c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
2248c2ecf20Sopenharmony_ci}
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci/**
2278c2ecf20Sopenharmony_ci * dpaa2_io_get_cpu() - get the cpu associated with a given DPIO object
2288c2ecf20Sopenharmony_ci *
2298c2ecf20Sopenharmony_ci * @d: the given DPIO object.
2308c2ecf20Sopenharmony_ci *
2318c2ecf20Sopenharmony_ci * Return the cpu associated with the DPIO object
2328c2ecf20Sopenharmony_ci */
2338c2ecf20Sopenharmony_ciint dpaa2_io_get_cpu(struct dpaa2_io *d)
2348c2ecf20Sopenharmony_ci{
2358c2ecf20Sopenharmony_ci	return d->dpio_desc.cpu;
2368c2ecf20Sopenharmony_ci}
2378c2ecf20Sopenharmony_ciEXPORT_SYMBOL(dpaa2_io_get_cpu);
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci/**
2408c2ecf20Sopenharmony_ci * dpaa2_io_service_register() - Prepare for servicing of FQDAN or CDAN
2418c2ecf20Sopenharmony_ci *                               notifications on the given DPIO service.
2428c2ecf20Sopenharmony_ci * @d:   the given DPIO service.
2438c2ecf20Sopenharmony_ci * @ctx: the notification context.
2448c2ecf20Sopenharmony_ci * @dev: the device that requests the register
2458c2ecf20Sopenharmony_ci *
2468c2ecf20Sopenharmony_ci * The caller should make the MC command to attach a DPAA2 object to
2478c2ecf20Sopenharmony_ci * a DPIO after this function completes successfully.  In that way:
2488c2ecf20Sopenharmony_ci *    (a) The DPIO service is "ready" to handle a notification arrival
2498c2ecf20Sopenharmony_ci *        (which might happen before the "attach" command to MC has
2508c2ecf20Sopenharmony_ci *        returned control of execution back to the caller)
2518c2ecf20Sopenharmony_ci *    (b) The DPIO service can provide back to the caller the 'dpio_id' and
2528c2ecf20Sopenharmony_ci *        'qman64' parameters that it should pass along in the MC command
2538c2ecf20Sopenharmony_ci *        in order for the object to be configured to produce the right
2548c2ecf20Sopenharmony_ci *        notification fields to the DPIO service.
2558c2ecf20Sopenharmony_ci *
2568c2ecf20Sopenharmony_ci * Return 0 for success, or -ENODEV for failure.
2578c2ecf20Sopenharmony_ci */
2588c2ecf20Sopenharmony_ciint dpaa2_io_service_register(struct dpaa2_io *d,
2598c2ecf20Sopenharmony_ci			      struct dpaa2_io_notification_ctx *ctx,
2608c2ecf20Sopenharmony_ci			      struct device *dev)
2618c2ecf20Sopenharmony_ci{
2628c2ecf20Sopenharmony_ci	struct device_link *link;
2638c2ecf20Sopenharmony_ci	unsigned long irqflags;
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	d = service_select_by_cpu(d, ctx->desired_cpu);
2668c2ecf20Sopenharmony_ci	if (!d)
2678c2ecf20Sopenharmony_ci		return -ENODEV;
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	link = device_link_add(dev, d->dev, DL_FLAG_AUTOREMOVE_CONSUMER);
2708c2ecf20Sopenharmony_ci	if (!link)
2718c2ecf20Sopenharmony_ci		return -EINVAL;
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	ctx->dpio_id = d->dpio_desc.dpio_id;
2748c2ecf20Sopenharmony_ci	ctx->qman64 = (u64)(uintptr_t)ctx;
2758c2ecf20Sopenharmony_ci	ctx->dpio_private = d;
2768c2ecf20Sopenharmony_ci	spin_lock_irqsave(&d->lock_notifications, irqflags);
2778c2ecf20Sopenharmony_ci	list_add(&ctx->node, &d->notifications);
2788c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&d->lock_notifications, irqflags);
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci	/* Enable the generation of CDAN notifications */
2818c2ecf20Sopenharmony_ci	if (ctx->is_cdan)
2828c2ecf20Sopenharmony_ci		return qbman_swp_CDAN_set_context_enable(d->swp,
2838c2ecf20Sopenharmony_ci							 (u16)ctx->id,
2848c2ecf20Sopenharmony_ci							 ctx->qman64);
2858c2ecf20Sopenharmony_ci	return 0;
2868c2ecf20Sopenharmony_ci}
2878c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(dpaa2_io_service_register);
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci/**
2908c2ecf20Sopenharmony_ci * dpaa2_io_service_deregister - The opposite of 'register'.
2918c2ecf20Sopenharmony_ci * @service: the given DPIO service.
2928c2ecf20Sopenharmony_ci * @ctx: the notification context.
2938c2ecf20Sopenharmony_ci * @dev: the device that requests to be deregistered
2948c2ecf20Sopenharmony_ci *
2958c2ecf20Sopenharmony_ci * This function should be called only after sending the MC command to
2968c2ecf20Sopenharmony_ci * to detach the notification-producing device from the DPIO.
2978c2ecf20Sopenharmony_ci */
2988c2ecf20Sopenharmony_civoid dpaa2_io_service_deregister(struct dpaa2_io *service,
2998c2ecf20Sopenharmony_ci				 struct dpaa2_io_notification_ctx *ctx,
3008c2ecf20Sopenharmony_ci				 struct device *dev)
3018c2ecf20Sopenharmony_ci{
3028c2ecf20Sopenharmony_ci	struct dpaa2_io *d = ctx->dpio_private;
3038c2ecf20Sopenharmony_ci	unsigned long irqflags;
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	if (ctx->is_cdan)
3068c2ecf20Sopenharmony_ci		qbman_swp_CDAN_disable(d->swp, (u16)ctx->id);
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	spin_lock_irqsave(&d->lock_notifications, irqflags);
3098c2ecf20Sopenharmony_ci	list_del(&ctx->node);
3108c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&d->lock_notifications, irqflags);
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci}
3138c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(dpaa2_io_service_deregister);
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci/**
3168c2ecf20Sopenharmony_ci * dpaa2_io_service_rearm() - Rearm the notification for the given DPIO service.
3178c2ecf20Sopenharmony_ci * @d: the given DPIO service.
3188c2ecf20Sopenharmony_ci * @ctx: the notification context.
3198c2ecf20Sopenharmony_ci *
3208c2ecf20Sopenharmony_ci * Once a FQDAN/CDAN has been produced, the corresponding FQ/channel is
3218c2ecf20Sopenharmony_ci * considered "disarmed". Ie. the user can issue pull dequeue operations on that
3228c2ecf20Sopenharmony_ci * traffic source for as long as it likes. Eventually it may wish to "rearm"
3238c2ecf20Sopenharmony_ci * that source to allow it to produce another FQDAN/CDAN, that's what this
3248c2ecf20Sopenharmony_ci * function achieves.
3258c2ecf20Sopenharmony_ci *
3268c2ecf20Sopenharmony_ci * Return 0 for success.
3278c2ecf20Sopenharmony_ci */
3288c2ecf20Sopenharmony_ciint dpaa2_io_service_rearm(struct dpaa2_io *d,
3298c2ecf20Sopenharmony_ci			   struct dpaa2_io_notification_ctx *ctx)
3308c2ecf20Sopenharmony_ci{
3318c2ecf20Sopenharmony_ci	unsigned long irqflags;
3328c2ecf20Sopenharmony_ci	int err;
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	d = service_select_by_cpu(d, ctx->desired_cpu);
3358c2ecf20Sopenharmony_ci	if (!unlikely(d))
3368c2ecf20Sopenharmony_ci		return -ENODEV;
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci	spin_lock_irqsave(&d->lock_mgmt_cmd, irqflags);
3398c2ecf20Sopenharmony_ci	if (ctx->is_cdan)
3408c2ecf20Sopenharmony_ci		err = qbman_swp_CDAN_enable(d->swp, (u16)ctx->id);
3418c2ecf20Sopenharmony_ci	else
3428c2ecf20Sopenharmony_ci		err = qbman_swp_fq_schedule(d->swp, ctx->id);
3438c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&d->lock_mgmt_cmd, irqflags);
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci	return err;
3468c2ecf20Sopenharmony_ci}
3478c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(dpaa2_io_service_rearm);
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci/**
3508c2ecf20Sopenharmony_ci * dpaa2_io_service_pull_fq() - pull dequeue functions from a fq.
3518c2ecf20Sopenharmony_ci * @d: the given DPIO service.
3528c2ecf20Sopenharmony_ci * @fqid: the given frame queue id.
3538c2ecf20Sopenharmony_ci * @s: the dpaa2_io_store object for the result.
3548c2ecf20Sopenharmony_ci *
3558c2ecf20Sopenharmony_ci * Return 0 for success, or error code for failure.
3568c2ecf20Sopenharmony_ci */
3578c2ecf20Sopenharmony_ciint dpaa2_io_service_pull_fq(struct dpaa2_io *d, u32 fqid,
3588c2ecf20Sopenharmony_ci			     struct dpaa2_io_store *s)
3598c2ecf20Sopenharmony_ci{
3608c2ecf20Sopenharmony_ci	struct qbman_pull_desc pd;
3618c2ecf20Sopenharmony_ci	int err;
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	qbman_pull_desc_clear(&pd);
3648c2ecf20Sopenharmony_ci	qbman_pull_desc_set_storage(&pd, s->vaddr, s->paddr, 1);
3658c2ecf20Sopenharmony_ci	qbman_pull_desc_set_numframes(&pd, (u8)s->max);
3668c2ecf20Sopenharmony_ci	qbman_pull_desc_set_fq(&pd, fqid);
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci	d = service_select(d);
3698c2ecf20Sopenharmony_ci	if (!d)
3708c2ecf20Sopenharmony_ci		return -ENODEV;
3718c2ecf20Sopenharmony_ci	s->swp = d->swp;
3728c2ecf20Sopenharmony_ci	err = qbman_swp_pull(d->swp, &pd);
3738c2ecf20Sopenharmony_ci	if (err)
3748c2ecf20Sopenharmony_ci		s->swp = NULL;
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	return err;
3778c2ecf20Sopenharmony_ci}
3788c2ecf20Sopenharmony_ciEXPORT_SYMBOL(dpaa2_io_service_pull_fq);
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci/**
3818c2ecf20Sopenharmony_ci * dpaa2_io_service_pull_channel() - pull dequeue functions from a channel.
3828c2ecf20Sopenharmony_ci * @d: the given DPIO service.
3838c2ecf20Sopenharmony_ci * @channelid: the given channel id.
3848c2ecf20Sopenharmony_ci * @s: the dpaa2_io_store object for the result.
3858c2ecf20Sopenharmony_ci *
3868c2ecf20Sopenharmony_ci * Return 0 for success, or error code for failure.
3878c2ecf20Sopenharmony_ci */
3888c2ecf20Sopenharmony_ciint dpaa2_io_service_pull_channel(struct dpaa2_io *d, u32 channelid,
3898c2ecf20Sopenharmony_ci				  struct dpaa2_io_store *s)
3908c2ecf20Sopenharmony_ci{
3918c2ecf20Sopenharmony_ci	struct qbman_pull_desc pd;
3928c2ecf20Sopenharmony_ci	int err;
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci	qbman_pull_desc_clear(&pd);
3958c2ecf20Sopenharmony_ci	qbman_pull_desc_set_storage(&pd, s->vaddr, s->paddr, 1);
3968c2ecf20Sopenharmony_ci	qbman_pull_desc_set_numframes(&pd, (u8)s->max);
3978c2ecf20Sopenharmony_ci	qbman_pull_desc_set_channel(&pd, channelid, qbman_pull_type_prio);
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci	d = service_select(d);
4008c2ecf20Sopenharmony_ci	if (!d)
4018c2ecf20Sopenharmony_ci		return -ENODEV;
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci	s->swp = d->swp;
4048c2ecf20Sopenharmony_ci	err = qbman_swp_pull(d->swp, &pd);
4058c2ecf20Sopenharmony_ci	if (err)
4068c2ecf20Sopenharmony_ci		s->swp = NULL;
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci	return err;
4098c2ecf20Sopenharmony_ci}
4108c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(dpaa2_io_service_pull_channel);
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci/**
4138c2ecf20Sopenharmony_ci * dpaa2_io_service_enqueue_fq() - Enqueue a frame to a frame queue.
4148c2ecf20Sopenharmony_ci * @d: the given DPIO service.
4158c2ecf20Sopenharmony_ci * @fqid: the given frame queue id.
4168c2ecf20Sopenharmony_ci * @fd: the frame descriptor which is enqueued.
4178c2ecf20Sopenharmony_ci *
4188c2ecf20Sopenharmony_ci * Return 0 for successful enqueue, -EBUSY if the enqueue ring is not ready,
4198c2ecf20Sopenharmony_ci * or -ENODEV if there is no dpio service.
4208c2ecf20Sopenharmony_ci */
4218c2ecf20Sopenharmony_ciint dpaa2_io_service_enqueue_fq(struct dpaa2_io *d,
4228c2ecf20Sopenharmony_ci				u32 fqid,
4238c2ecf20Sopenharmony_ci				const struct dpaa2_fd *fd)
4248c2ecf20Sopenharmony_ci{
4258c2ecf20Sopenharmony_ci	struct qbman_eq_desc ed;
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ci	d = service_select(d);
4288c2ecf20Sopenharmony_ci	if (!d)
4298c2ecf20Sopenharmony_ci		return -ENODEV;
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci	qbman_eq_desc_clear(&ed);
4328c2ecf20Sopenharmony_ci	qbman_eq_desc_set_no_orp(&ed, 0);
4338c2ecf20Sopenharmony_ci	qbman_eq_desc_set_fq(&ed, fqid);
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_ci	return qbman_swp_enqueue(d->swp, &ed, fd);
4368c2ecf20Sopenharmony_ci}
4378c2ecf20Sopenharmony_ciEXPORT_SYMBOL(dpaa2_io_service_enqueue_fq);
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci/**
4408c2ecf20Sopenharmony_ci * dpaa2_io_service_enqueue_multiple_fq() - Enqueue multiple frames
4418c2ecf20Sopenharmony_ci * to a frame queue using one fqid.
4428c2ecf20Sopenharmony_ci * @d: the given DPIO service.
4438c2ecf20Sopenharmony_ci * @fqid: the given frame queue id.
4448c2ecf20Sopenharmony_ci * @fd: the frame descriptor which is enqueued.
4458c2ecf20Sopenharmony_ci * @nb: number of frames to be enqueud
4468c2ecf20Sopenharmony_ci *
4478c2ecf20Sopenharmony_ci * Return 0 for successful enqueue, -EBUSY if the enqueue ring is not ready,
4488c2ecf20Sopenharmony_ci * or -ENODEV if there is no dpio service.
4498c2ecf20Sopenharmony_ci */
4508c2ecf20Sopenharmony_ciint dpaa2_io_service_enqueue_multiple_fq(struct dpaa2_io *d,
4518c2ecf20Sopenharmony_ci				u32 fqid,
4528c2ecf20Sopenharmony_ci				const struct dpaa2_fd *fd,
4538c2ecf20Sopenharmony_ci				int nb)
4548c2ecf20Sopenharmony_ci{
4558c2ecf20Sopenharmony_ci	struct qbman_eq_desc ed;
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_ci	d = service_select(d);
4588c2ecf20Sopenharmony_ci	if (!d)
4598c2ecf20Sopenharmony_ci		return -ENODEV;
4608c2ecf20Sopenharmony_ci
4618c2ecf20Sopenharmony_ci	qbman_eq_desc_clear(&ed);
4628c2ecf20Sopenharmony_ci	qbman_eq_desc_set_no_orp(&ed, 0);
4638c2ecf20Sopenharmony_ci	qbman_eq_desc_set_fq(&ed, fqid);
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_ci	return qbman_swp_enqueue_multiple(d->swp, &ed, fd, 0, nb);
4668c2ecf20Sopenharmony_ci}
4678c2ecf20Sopenharmony_ciEXPORT_SYMBOL(dpaa2_io_service_enqueue_multiple_fq);
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci/**
4708c2ecf20Sopenharmony_ci * dpaa2_io_service_enqueue_multiple_desc_fq() - Enqueue multiple frames
4718c2ecf20Sopenharmony_ci * to different frame queue using a list of fqids.
4728c2ecf20Sopenharmony_ci * @d: the given DPIO service.
4738c2ecf20Sopenharmony_ci * @fqid: the given list of frame queue ids.
4748c2ecf20Sopenharmony_ci * @fd: the frame descriptor which is enqueued.
4758c2ecf20Sopenharmony_ci * @nb: number of frames to be enqueud
4768c2ecf20Sopenharmony_ci *
4778c2ecf20Sopenharmony_ci * Return 0 for successful enqueue, -EBUSY if the enqueue ring is not ready,
4788c2ecf20Sopenharmony_ci * or -ENODEV if there is no dpio service.
4798c2ecf20Sopenharmony_ci */
4808c2ecf20Sopenharmony_ciint dpaa2_io_service_enqueue_multiple_desc_fq(struct dpaa2_io *d,
4818c2ecf20Sopenharmony_ci				u32 *fqid,
4828c2ecf20Sopenharmony_ci				const struct dpaa2_fd *fd,
4838c2ecf20Sopenharmony_ci				int nb)
4848c2ecf20Sopenharmony_ci{
4858c2ecf20Sopenharmony_ci	struct qbman_eq_desc *ed;
4868c2ecf20Sopenharmony_ci	int i, ret;
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ci	ed = kcalloc(sizeof(struct qbman_eq_desc), 32, GFP_KERNEL);
4898c2ecf20Sopenharmony_ci	if (!ed)
4908c2ecf20Sopenharmony_ci		return -ENOMEM;
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci	d = service_select(d);
4938c2ecf20Sopenharmony_ci	if (!d) {
4948c2ecf20Sopenharmony_ci		ret = -ENODEV;
4958c2ecf20Sopenharmony_ci		goto out;
4968c2ecf20Sopenharmony_ci	}
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci	for (i = 0; i < nb; i++) {
4998c2ecf20Sopenharmony_ci		qbman_eq_desc_clear(&ed[i]);
5008c2ecf20Sopenharmony_ci		qbman_eq_desc_set_no_orp(&ed[i], 0);
5018c2ecf20Sopenharmony_ci		qbman_eq_desc_set_fq(&ed[i], fqid[i]);
5028c2ecf20Sopenharmony_ci	}
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci	ret = qbman_swp_enqueue_multiple_desc(d->swp, &ed[0], fd, nb);
5058c2ecf20Sopenharmony_ciout:
5068c2ecf20Sopenharmony_ci	kfree(ed);
5078c2ecf20Sopenharmony_ci	return ret;
5088c2ecf20Sopenharmony_ci}
5098c2ecf20Sopenharmony_ciEXPORT_SYMBOL(dpaa2_io_service_enqueue_multiple_desc_fq);
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ci/**
5128c2ecf20Sopenharmony_ci * dpaa2_io_service_enqueue_qd() - Enqueue a frame to a QD.
5138c2ecf20Sopenharmony_ci * @d: the given DPIO service.
5148c2ecf20Sopenharmony_ci * @qdid: the given queuing destination id.
5158c2ecf20Sopenharmony_ci * @prio: the given queuing priority.
5168c2ecf20Sopenharmony_ci * @qdbin: the given queuing destination bin.
5178c2ecf20Sopenharmony_ci * @fd: the frame descriptor which is enqueued.
5188c2ecf20Sopenharmony_ci *
5198c2ecf20Sopenharmony_ci * Return 0 for successful enqueue, or -EBUSY if the enqueue ring is not ready,
5208c2ecf20Sopenharmony_ci * or -ENODEV if there is no dpio service.
5218c2ecf20Sopenharmony_ci */
5228c2ecf20Sopenharmony_ciint dpaa2_io_service_enqueue_qd(struct dpaa2_io *d,
5238c2ecf20Sopenharmony_ci				u32 qdid, u8 prio, u16 qdbin,
5248c2ecf20Sopenharmony_ci				const struct dpaa2_fd *fd)
5258c2ecf20Sopenharmony_ci{
5268c2ecf20Sopenharmony_ci	struct qbman_eq_desc ed;
5278c2ecf20Sopenharmony_ci
5288c2ecf20Sopenharmony_ci	d = service_select(d);
5298c2ecf20Sopenharmony_ci	if (!d)
5308c2ecf20Sopenharmony_ci		return -ENODEV;
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci	qbman_eq_desc_clear(&ed);
5338c2ecf20Sopenharmony_ci	qbman_eq_desc_set_no_orp(&ed, 0);
5348c2ecf20Sopenharmony_ci	qbman_eq_desc_set_qd(&ed, qdid, qdbin, prio);
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci	return qbman_swp_enqueue(d->swp, &ed, fd);
5378c2ecf20Sopenharmony_ci}
5388c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(dpaa2_io_service_enqueue_qd);
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_ci/**
5418c2ecf20Sopenharmony_ci * dpaa2_io_service_release() - Release buffers to a buffer pool.
5428c2ecf20Sopenharmony_ci * @d: the given DPIO object.
5438c2ecf20Sopenharmony_ci * @bpid: the buffer pool id.
5448c2ecf20Sopenharmony_ci * @buffers: the buffers to be released.
5458c2ecf20Sopenharmony_ci * @num_buffers: the number of the buffers to be released.
5468c2ecf20Sopenharmony_ci *
5478c2ecf20Sopenharmony_ci * Return 0 for success, and negative error code for failure.
5488c2ecf20Sopenharmony_ci */
5498c2ecf20Sopenharmony_ciint dpaa2_io_service_release(struct dpaa2_io *d,
5508c2ecf20Sopenharmony_ci			     u16 bpid,
5518c2ecf20Sopenharmony_ci			     const u64 *buffers,
5528c2ecf20Sopenharmony_ci			     unsigned int num_buffers)
5538c2ecf20Sopenharmony_ci{
5548c2ecf20Sopenharmony_ci	struct qbman_release_desc rd;
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_ci	d = service_select(d);
5578c2ecf20Sopenharmony_ci	if (!d)
5588c2ecf20Sopenharmony_ci		return -ENODEV;
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci	qbman_release_desc_clear(&rd);
5618c2ecf20Sopenharmony_ci	qbman_release_desc_set_bpid(&rd, bpid);
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci	return qbman_swp_release(d->swp, &rd, buffers, num_buffers);
5648c2ecf20Sopenharmony_ci}
5658c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(dpaa2_io_service_release);
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_ci/**
5688c2ecf20Sopenharmony_ci * dpaa2_io_service_acquire() - Acquire buffers from a buffer pool.
5698c2ecf20Sopenharmony_ci * @d: the given DPIO object.
5708c2ecf20Sopenharmony_ci * @bpid: the buffer pool id.
5718c2ecf20Sopenharmony_ci * @buffers: the buffer addresses for acquired buffers.
5728c2ecf20Sopenharmony_ci * @num_buffers: the expected number of the buffers to acquire.
5738c2ecf20Sopenharmony_ci *
5748c2ecf20Sopenharmony_ci * Return a negative error code if the command failed, otherwise it returns
5758c2ecf20Sopenharmony_ci * the number of buffers acquired, which may be less than the number requested.
5768c2ecf20Sopenharmony_ci * Eg. if the buffer pool is empty, this will return zero.
5778c2ecf20Sopenharmony_ci */
5788c2ecf20Sopenharmony_ciint dpaa2_io_service_acquire(struct dpaa2_io *d,
5798c2ecf20Sopenharmony_ci			     u16 bpid,
5808c2ecf20Sopenharmony_ci			     u64 *buffers,
5818c2ecf20Sopenharmony_ci			     unsigned int num_buffers)
5828c2ecf20Sopenharmony_ci{
5838c2ecf20Sopenharmony_ci	unsigned long irqflags;
5848c2ecf20Sopenharmony_ci	int err;
5858c2ecf20Sopenharmony_ci
5868c2ecf20Sopenharmony_ci	d = service_select(d);
5878c2ecf20Sopenharmony_ci	if (!d)
5888c2ecf20Sopenharmony_ci		return -ENODEV;
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci	spin_lock_irqsave(&d->lock_mgmt_cmd, irqflags);
5918c2ecf20Sopenharmony_ci	err = qbman_swp_acquire(d->swp, bpid, buffers, num_buffers);
5928c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&d->lock_mgmt_cmd, irqflags);
5938c2ecf20Sopenharmony_ci
5948c2ecf20Sopenharmony_ci	return err;
5958c2ecf20Sopenharmony_ci}
5968c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(dpaa2_io_service_acquire);
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_ci/*
5998c2ecf20Sopenharmony_ci * 'Stores' are reusable memory blocks for holding dequeue results, and to
6008c2ecf20Sopenharmony_ci * assist with parsing those results.
6018c2ecf20Sopenharmony_ci */
6028c2ecf20Sopenharmony_ci
6038c2ecf20Sopenharmony_ci/**
6048c2ecf20Sopenharmony_ci * dpaa2_io_store_create() - Create the dma memory storage for dequeue result.
6058c2ecf20Sopenharmony_ci * @max_frames: the maximum number of dequeued result for frames, must be <= 32.
6068c2ecf20Sopenharmony_ci * @dev:        the device to allow mapping/unmapping the DMAable region.
6078c2ecf20Sopenharmony_ci *
6088c2ecf20Sopenharmony_ci * The size of the storage is "max_frames*sizeof(struct dpaa2_dq)".
6098c2ecf20Sopenharmony_ci * The 'dpaa2_io_store' returned is a DPIO service managed object.
6108c2ecf20Sopenharmony_ci *
6118c2ecf20Sopenharmony_ci * Return pointer to dpaa2_io_store struct for successfully created storage
6128c2ecf20Sopenharmony_ci * memory, or NULL on error.
6138c2ecf20Sopenharmony_ci */
6148c2ecf20Sopenharmony_cistruct dpaa2_io_store *dpaa2_io_store_create(unsigned int max_frames,
6158c2ecf20Sopenharmony_ci					     struct device *dev)
6168c2ecf20Sopenharmony_ci{
6178c2ecf20Sopenharmony_ci	struct dpaa2_io_store *ret;
6188c2ecf20Sopenharmony_ci	size_t size;
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_ci	if (!max_frames || (max_frames > 32))
6218c2ecf20Sopenharmony_ci		return NULL;
6228c2ecf20Sopenharmony_ci
6238c2ecf20Sopenharmony_ci	ret = kmalloc(sizeof(*ret), GFP_KERNEL);
6248c2ecf20Sopenharmony_ci	if (!ret)
6258c2ecf20Sopenharmony_ci		return NULL;
6268c2ecf20Sopenharmony_ci
6278c2ecf20Sopenharmony_ci	ret->max = max_frames;
6288c2ecf20Sopenharmony_ci	size = max_frames * sizeof(struct dpaa2_dq) + 64;
6298c2ecf20Sopenharmony_ci	ret->alloced_addr = kzalloc(size, GFP_KERNEL);
6308c2ecf20Sopenharmony_ci	if (!ret->alloced_addr) {
6318c2ecf20Sopenharmony_ci		kfree(ret);
6328c2ecf20Sopenharmony_ci		return NULL;
6338c2ecf20Sopenharmony_ci	}
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci	ret->vaddr = PTR_ALIGN(ret->alloced_addr, 64);
6368c2ecf20Sopenharmony_ci	ret->paddr = dma_map_single(dev, ret->vaddr,
6378c2ecf20Sopenharmony_ci				    sizeof(struct dpaa2_dq) * max_frames,
6388c2ecf20Sopenharmony_ci				    DMA_FROM_DEVICE);
6398c2ecf20Sopenharmony_ci	if (dma_mapping_error(dev, ret->paddr)) {
6408c2ecf20Sopenharmony_ci		kfree(ret->alloced_addr);
6418c2ecf20Sopenharmony_ci		kfree(ret);
6428c2ecf20Sopenharmony_ci		return NULL;
6438c2ecf20Sopenharmony_ci	}
6448c2ecf20Sopenharmony_ci
6458c2ecf20Sopenharmony_ci	ret->idx = 0;
6468c2ecf20Sopenharmony_ci	ret->dev = dev;
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_ci	return ret;
6498c2ecf20Sopenharmony_ci}
6508c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(dpaa2_io_store_create);
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_ci/**
6538c2ecf20Sopenharmony_ci * dpaa2_io_store_destroy() - Frees the dma memory storage for dequeue
6548c2ecf20Sopenharmony_ci *                            result.
6558c2ecf20Sopenharmony_ci * @s: the storage memory to be destroyed.
6568c2ecf20Sopenharmony_ci */
6578c2ecf20Sopenharmony_civoid dpaa2_io_store_destroy(struct dpaa2_io_store *s)
6588c2ecf20Sopenharmony_ci{
6598c2ecf20Sopenharmony_ci	dma_unmap_single(s->dev, s->paddr, sizeof(struct dpaa2_dq) * s->max,
6608c2ecf20Sopenharmony_ci			 DMA_FROM_DEVICE);
6618c2ecf20Sopenharmony_ci	kfree(s->alloced_addr);
6628c2ecf20Sopenharmony_ci	kfree(s);
6638c2ecf20Sopenharmony_ci}
6648c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(dpaa2_io_store_destroy);
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_ci/**
6678c2ecf20Sopenharmony_ci * dpaa2_io_store_next() - Determine when the next dequeue result is available.
6688c2ecf20Sopenharmony_ci * @s: the dpaa2_io_store object.
6698c2ecf20Sopenharmony_ci * @is_last: indicate whether this is the last frame in the pull command.
6708c2ecf20Sopenharmony_ci *
6718c2ecf20Sopenharmony_ci * When an object driver performs dequeues to a dpaa2_io_store, this function
6728c2ecf20Sopenharmony_ci * can be used to determine when the next frame result is available. Once
6738c2ecf20Sopenharmony_ci * this function returns non-NULL, a subsequent call to it will try to find
6748c2ecf20Sopenharmony_ci * the next dequeue result.
6758c2ecf20Sopenharmony_ci *
6768c2ecf20Sopenharmony_ci * Note that if a pull-dequeue has a NULL result because the target FQ/channel
6778c2ecf20Sopenharmony_ci * was empty, then this function will also return NULL (rather than expecting
6788c2ecf20Sopenharmony_ci * the caller to always check for this. As such, "is_last" can be used to
6798c2ecf20Sopenharmony_ci * differentiate between "end-of-empty-dequeue" and "still-waiting".
6808c2ecf20Sopenharmony_ci *
6818c2ecf20Sopenharmony_ci * Return dequeue result for a valid dequeue result, or NULL for empty dequeue.
6828c2ecf20Sopenharmony_ci */
6838c2ecf20Sopenharmony_cistruct dpaa2_dq *dpaa2_io_store_next(struct dpaa2_io_store *s, int *is_last)
6848c2ecf20Sopenharmony_ci{
6858c2ecf20Sopenharmony_ci	int match;
6868c2ecf20Sopenharmony_ci	struct dpaa2_dq *ret = &s->vaddr[s->idx];
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_ci	match = qbman_result_has_new_result(s->swp, ret);
6898c2ecf20Sopenharmony_ci	if (!match) {
6908c2ecf20Sopenharmony_ci		*is_last = 0;
6918c2ecf20Sopenharmony_ci		return NULL;
6928c2ecf20Sopenharmony_ci	}
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_ci	s->idx++;
6958c2ecf20Sopenharmony_ci
6968c2ecf20Sopenharmony_ci	if (dpaa2_dq_is_pull_complete(ret)) {
6978c2ecf20Sopenharmony_ci		*is_last = 1;
6988c2ecf20Sopenharmony_ci		s->idx = 0;
6998c2ecf20Sopenharmony_ci		/*
7008c2ecf20Sopenharmony_ci		 * If we get an empty dequeue result to terminate a zero-results
7018c2ecf20Sopenharmony_ci		 * vdqcr, return NULL to the caller rather than expecting him to
7028c2ecf20Sopenharmony_ci		 * check non-NULL results every time.
7038c2ecf20Sopenharmony_ci		 */
7048c2ecf20Sopenharmony_ci		if (!(dpaa2_dq_flags(ret) & DPAA2_DQ_STAT_VALIDFRAME))
7058c2ecf20Sopenharmony_ci			ret = NULL;
7068c2ecf20Sopenharmony_ci	} else {
7078c2ecf20Sopenharmony_ci		prefetch(&s->vaddr[s->idx]);
7088c2ecf20Sopenharmony_ci		*is_last = 0;
7098c2ecf20Sopenharmony_ci	}
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_ci	return ret;
7128c2ecf20Sopenharmony_ci}
7138c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(dpaa2_io_store_next);
7148c2ecf20Sopenharmony_ci
7158c2ecf20Sopenharmony_ci/**
7168c2ecf20Sopenharmony_ci * dpaa2_io_query_fq_count() - Get the frame and byte count for a given fq.
7178c2ecf20Sopenharmony_ci * @d: the given DPIO object.
7188c2ecf20Sopenharmony_ci * @fqid: the id of frame queue to be queried.
7198c2ecf20Sopenharmony_ci * @fcnt: the queried frame count.
7208c2ecf20Sopenharmony_ci * @bcnt: the queried byte count.
7218c2ecf20Sopenharmony_ci *
7228c2ecf20Sopenharmony_ci * Knowing the FQ count at run-time can be useful in debugging situations.
7238c2ecf20Sopenharmony_ci * The instantaneous frame- and byte-count are hereby returned.
7248c2ecf20Sopenharmony_ci *
7258c2ecf20Sopenharmony_ci * Return 0 for a successful query, and negative error code if query fails.
7268c2ecf20Sopenharmony_ci */
7278c2ecf20Sopenharmony_ciint dpaa2_io_query_fq_count(struct dpaa2_io *d, u32 fqid,
7288c2ecf20Sopenharmony_ci			    u32 *fcnt, u32 *bcnt)
7298c2ecf20Sopenharmony_ci{
7308c2ecf20Sopenharmony_ci	struct qbman_fq_query_np_rslt state;
7318c2ecf20Sopenharmony_ci	struct qbman_swp *swp;
7328c2ecf20Sopenharmony_ci	unsigned long irqflags;
7338c2ecf20Sopenharmony_ci	int ret;
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_ci	d = service_select(d);
7368c2ecf20Sopenharmony_ci	if (!d)
7378c2ecf20Sopenharmony_ci		return -ENODEV;
7388c2ecf20Sopenharmony_ci
7398c2ecf20Sopenharmony_ci	swp = d->swp;
7408c2ecf20Sopenharmony_ci	spin_lock_irqsave(&d->lock_mgmt_cmd, irqflags);
7418c2ecf20Sopenharmony_ci	ret = qbman_fq_query_state(swp, fqid, &state);
7428c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&d->lock_mgmt_cmd, irqflags);
7438c2ecf20Sopenharmony_ci	if (ret)
7448c2ecf20Sopenharmony_ci		return ret;
7458c2ecf20Sopenharmony_ci	*fcnt = qbman_fq_state_frame_count(&state);
7468c2ecf20Sopenharmony_ci	*bcnt = qbman_fq_state_byte_count(&state);
7478c2ecf20Sopenharmony_ci
7488c2ecf20Sopenharmony_ci	return 0;
7498c2ecf20Sopenharmony_ci}
7508c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(dpaa2_io_query_fq_count);
7518c2ecf20Sopenharmony_ci
7528c2ecf20Sopenharmony_ci/**
7538c2ecf20Sopenharmony_ci * dpaa2_io_query_bp_count() - Query the number of buffers currently in a
7548c2ecf20Sopenharmony_ci * buffer pool.
7558c2ecf20Sopenharmony_ci * @d: the given DPIO object.
7568c2ecf20Sopenharmony_ci * @bpid: the index of buffer pool to be queried.
7578c2ecf20Sopenharmony_ci * @num: the queried number of buffers in the buffer pool.
7588c2ecf20Sopenharmony_ci *
7598c2ecf20Sopenharmony_ci * Return 0 for a successful query, and negative error code if query fails.
7608c2ecf20Sopenharmony_ci */
7618c2ecf20Sopenharmony_ciint dpaa2_io_query_bp_count(struct dpaa2_io *d, u16 bpid, u32 *num)
7628c2ecf20Sopenharmony_ci{
7638c2ecf20Sopenharmony_ci	struct qbman_bp_query_rslt state;
7648c2ecf20Sopenharmony_ci	struct qbman_swp *swp;
7658c2ecf20Sopenharmony_ci	unsigned long irqflags;
7668c2ecf20Sopenharmony_ci	int ret;
7678c2ecf20Sopenharmony_ci
7688c2ecf20Sopenharmony_ci	d = service_select(d);
7698c2ecf20Sopenharmony_ci	if (!d)
7708c2ecf20Sopenharmony_ci		return -ENODEV;
7718c2ecf20Sopenharmony_ci
7728c2ecf20Sopenharmony_ci	swp = d->swp;
7738c2ecf20Sopenharmony_ci	spin_lock_irqsave(&d->lock_mgmt_cmd, irqflags);
7748c2ecf20Sopenharmony_ci	ret = qbman_bp_query(swp, bpid, &state);
7758c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&d->lock_mgmt_cmd, irqflags);
7768c2ecf20Sopenharmony_ci	if (ret)
7778c2ecf20Sopenharmony_ci		return ret;
7788c2ecf20Sopenharmony_ci	*num = qbman_bp_info_num_free_bufs(&state);
7798c2ecf20Sopenharmony_ci	return 0;
7808c2ecf20Sopenharmony_ci}
7818c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(dpaa2_io_query_bp_count);
782