
     ˆ¿Ãƒh¹ÂÁ      Unknown
  *   ˆl–Ü4ý( ©³( 	Å€fàB¦-ÿÄƒ|Bo_†I|¥�+ÄÃ $	    /* SPDX-License-Identifier: GPL-2.0 */
/* Interface for implementing AF_XDP zero-copy support in drivers.
 * Copyright(c) 2020 Intel Corporation.
 */

#ifndef _LINUX_XDP_SOCK_DRV_H
#define _LINUX_XDP_SOCK_DRV_H

#include <net/xdp_sock.h>
#include <net/xsk_buff_pool.h>

#define XDP_UMEM_MIN_CHUNK_SHIFT 11
#define XDP_UMEM_MIN_CHUNK_SIZE (1 << XDP_UMEM_MIN_CHUNK_SHIFT)

#ifdef CONFIG_XDP_SOCKETS

struct xsk_cb_desc {
	void *src;
	u8 off;
	u8 bytes;
};

void xsk_tx_completed(struct xsk_buff_pool *pool, u32 nb_entries);
bool xsk_tx_peek_desc(struct xsk_buff_pool *pool, struct xdp_desc *desc);
u32 xsk_tx_peek_release_desc_batch(struct xsk_buff_pool *pool, u32 max);
void xsk_tx_release(struct xsk_buff_pool *pool);
struct xsk_buff_pool *xsk_get_pool_from_qid(struct net_device *dev,
					    u16 queue_id);
void xsk_set_rx_need_wakeup(struct xsk_buff_pool *pool);
void xsk_set_tx_need_wakeup(struct xsk_buff_pool *pool);
void xsk_clear_rx_need_wakeup(struct xsk_buff_pool *pool);
void xsk_clear_tx_need_wakeup(struct xsk_buff_pool *pool);
bool xsk_uses_need_wakeup(struct xsk_buff_pool *pool);

static inline u32 xsk_pool_get_headroom(struct xsk_buff_pool *pool)
{
	return XDP_PACKET_HEADROOM + pool->headroom;
}

static inline u32 xsk_pool_get_chunk_size(struct xsk_buff_pool *pool)
{
	return pool->chunk_size;
}

static inline u32 xsk_pool_get_rx_frame_size(struct xsk_buff_pool *pool)
{
	return xsk_pool_get_chunk_size(pool) - xsk_pool_get_headroom(pool);
}

static inline void xsk_pool_set_rxq_info(struct xsk_buff_pool *pool,
					 struct xdp_rxq_info *rxq)
{
	xp_set_rxq_info(pool, rxq);
}

static inline void xsk_pool_fill_cb(struct xsk_buff_pool *pool,
				    struct xsk_cb_desc *desc)
{
	xp_fill_cb(pool, desc);
}

static inline void xsk_pool_dma_unmap(struct xsk_buff_pool *pool,
				      unsigned long attrs)
{
	xp_dma_unmap(pool, attrs);
}

static inline int xsk_pool_dma_map(struct xsk_buff_pool *pool,
				   struct device *dev, unsigned long attrs)
{
	struct xdp_umem *umem = pool->umem;

	return xp_dma_map(pool, dev, attrs, umem->pgs, umem->npgs);
}

static inline dma_addr_t xsk_buff_xdp_get_dma(struct xdp_buff *xdp)
{
	struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);

	return xp_get_dma(xskb);
}

static inline dma_addr_t xsk_buff_xdp_get_frame_dma(struct xdp_buff *xdp)
{
	struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);

	return xp_get_frame_dma(xskb);
}

static inline struct xdp_buff *xsk_buff_alloc(struct xsk_buff_pool *pool)
{
	return xp_alloc(pool);
}

static inline bool xsk_is_eop_desc(struct xdp_desc *desc)
{
	return !xp_mb_desc(desc);
}

/* Returns as many entries as possible up to max. 0 <= N <= max. */
static inline u32 xsk_buff_alloc_batch(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max)
{
	return xp_alloc_batch(pool, xdp, max);
}

static inline bool xsk_buff_can_alloc(struct xsk_buff_pool *pool, u32 count)
{
	return xp_can_alloc(pool, count);
}

static inline void xsk_buff_free(struct xdp_buff *xdp)
{
	struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
	struct list_head *xskb_list = &xskb->pool->xskb_list;
	struct xdp_buff_xsk *pos, *tmp;

	if (likely(!xdp_buff_has_frags(xdp)))
		goto out;

	list_for_each_entry_safe(pos, tmp, xskb_list, xskb_list_node) {
		list_del(&pos->xskb_list_node);
		xp_free(pos);
	}

	xdp_get_shared_info_from_buff(xdp)->nr_frags = 0;
out:
	xp_free(xskb);
}

static inline void xsk_buff_add_frag(struct xdp_buff *xdp)
{
	struct xdp_buff_xsk *frag = container_of(xdp, struct xdp_buff_xsk, xdp);

	list_add_tail(&frag->xskb_list_node, &frag->pool->xskb_list);
}

static inline struct xdp_buff *xsk_buff_get_frag(struct xdp_buff *first)
{
	struct xdp_buff_xsk *xskb = container_of(first, struct xdp_buff_xsk, xdp);
	struct xdp_buff *ret = NULL;
	struct xdp_buff_xsk *frag;

	frag = list_first_entry_or_null(&xskb->pool->xskb_list,
					struct xdp_buff_xsk, xskb_list_node);
	if (frag) {
		list_del(&frag->xskb_list_node);
		ret = &frag->xd