18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/* Copyright(c) 2020 Intel Corporation. */
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci#ifndef XSK_BUFF_POOL_H_
58c2ecf20Sopenharmony_ci#define XSK_BUFF_POOL_H_
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/if_xdp.h>
88c2ecf20Sopenharmony_ci#include <linux/types.h>
98c2ecf20Sopenharmony_ci#include <linux/dma-mapping.h>
108c2ecf20Sopenharmony_ci#include <net/xdp.h>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_cistruct xsk_buff_pool;
138c2ecf20Sopenharmony_cistruct xdp_rxq_info;
148c2ecf20Sopenharmony_cistruct xsk_queue;
158c2ecf20Sopenharmony_cistruct xdp_desc;
168c2ecf20Sopenharmony_cistruct xdp_umem;
178c2ecf20Sopenharmony_cistruct xdp_sock;
188c2ecf20Sopenharmony_cistruct device;
198c2ecf20Sopenharmony_cistruct page;
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_cistruct xdp_buff_xsk {
228c2ecf20Sopenharmony_ci	struct xdp_buff xdp;
238c2ecf20Sopenharmony_ci	dma_addr_t dma;
248c2ecf20Sopenharmony_ci	dma_addr_t frame_dma;
258c2ecf20Sopenharmony_ci	struct xsk_buff_pool *pool;
268c2ecf20Sopenharmony_ci	bool unaligned;
278c2ecf20Sopenharmony_ci	u64 orig_addr;
288c2ecf20Sopenharmony_ci	struct list_head free_list_node;
298c2ecf20Sopenharmony_ci};
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_cistruct xsk_dma_map {
328c2ecf20Sopenharmony_ci	dma_addr_t *dma_pages;
338c2ecf20Sopenharmony_ci	struct device *dev;
348c2ecf20Sopenharmony_ci	struct net_device *netdev;
358c2ecf20Sopenharmony_ci	refcount_t users;
368c2ecf20Sopenharmony_ci	struct list_head list; /* Protected by the RTNL_LOCK */
378c2ecf20Sopenharmony_ci	u32 dma_pages_cnt;
388c2ecf20Sopenharmony_ci	bool dma_need_sync;
398c2ecf20Sopenharmony_ci};
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_cistruct xsk_buff_pool {
428c2ecf20Sopenharmony_ci	/* Members only used in the control path first. */
438c2ecf20Sopenharmony_ci	struct device *dev;
448c2ecf20Sopenharmony_ci	struct net_device *netdev;
458c2ecf20Sopenharmony_ci	struct list_head xsk_tx_list;
468c2ecf20Sopenharmony_ci	/* Protects modifications to the xsk_tx_list */
478c2ecf20Sopenharmony_ci	spinlock_t xsk_tx_list_lock;
488c2ecf20Sopenharmony_ci	refcount_t users;
498c2ecf20Sopenharmony_ci	struct xdp_umem *umem;
508c2ecf20Sopenharmony_ci	struct work_struct work;
518c2ecf20Sopenharmony_ci	struct list_head free_list;
528c2ecf20Sopenharmony_ci	u32 heads_cnt;
538c2ecf20Sopenharmony_ci	u16 queue_id;
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	/* Data path members as close to free_heads at the end as possible. */
568c2ecf20Sopenharmony_ci	struct xsk_queue *fq ____cacheline_aligned_in_smp;
578c2ecf20Sopenharmony_ci	struct xsk_queue *cq;
588c2ecf20Sopenharmony_ci	/* For performance reasons, each buff pool has its own array of dma_pages
598c2ecf20Sopenharmony_ci	 * even when they are identical.
608c2ecf20Sopenharmony_ci	 */
618c2ecf20Sopenharmony_ci	dma_addr_t *dma_pages;
628c2ecf20Sopenharmony_ci	struct xdp_buff_xsk *heads;
638c2ecf20Sopenharmony_ci	u64 chunk_mask;
648c2ecf20Sopenharmony_ci	u64 addrs_cnt;
658c2ecf20Sopenharmony_ci	u32 free_list_cnt;
668c2ecf20Sopenharmony_ci	u32 dma_pages_cnt;
678c2ecf20Sopenharmony_ci	u32 free_heads_cnt;
688c2ecf20Sopenharmony_ci	u32 headroom;
698c2ecf20Sopenharmony_ci	u32 chunk_size;
708c2ecf20Sopenharmony_ci	u32 frame_len;
718c2ecf20Sopenharmony_ci	u8 cached_need_wakeup;
728c2ecf20Sopenharmony_ci	bool uses_need_wakeup;
738c2ecf20Sopenharmony_ci	bool dma_need_sync;
748c2ecf20Sopenharmony_ci	bool unaligned;
758c2ecf20Sopenharmony_ci	void *addrs;
768c2ecf20Sopenharmony_ci	/* Mutual exclusion of the completion ring in the SKB mode. Two cases to protect:
778c2ecf20Sopenharmony_ci	 * NAPI TX thread and sendmsg error paths in the SKB destructor callback and when
788c2ecf20Sopenharmony_ci	 * sockets share a single cq when the same netdev and queue id is shared.
798c2ecf20Sopenharmony_ci	 */
808c2ecf20Sopenharmony_ci	spinlock_t cq_lock;
818c2ecf20Sopenharmony_ci	struct xdp_buff_xsk *free_heads[];
828c2ecf20Sopenharmony_ci};
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci/* AF_XDP core. */
858c2ecf20Sopenharmony_cistruct xsk_buff_pool *xp_create_and_assign_umem(struct xdp_sock *xs,
868c2ecf20Sopenharmony_ci						struct xdp_umem *umem);
878c2ecf20Sopenharmony_ciint xp_assign_dev(struct xsk_buff_pool *pool, struct net_device *dev,
888c2ecf20Sopenharmony_ci		  u16 queue_id, u16 flags);
898c2ecf20Sopenharmony_ciint xp_assign_dev_shared(struct xsk_buff_pool *pool, struct xdp_sock *umem_xs,
908c2ecf20Sopenharmony_ci			 struct net_device *dev, u16 queue_id);
918c2ecf20Sopenharmony_civoid xp_destroy(struct xsk_buff_pool *pool);
928c2ecf20Sopenharmony_civoid xp_release(struct xdp_buff_xsk *xskb);
938c2ecf20Sopenharmony_civoid xp_get_pool(struct xsk_buff_pool *pool);
948c2ecf20Sopenharmony_cibool xp_put_pool(struct xsk_buff_pool *pool);
958c2ecf20Sopenharmony_civoid xp_clear_dev(struct xsk_buff_pool *pool);
968c2ecf20Sopenharmony_civoid xp_add_xsk(struct xsk_buff_pool *pool, struct xdp_sock *xs);
978c2ecf20Sopenharmony_civoid xp_del_xsk(struct xsk_buff_pool *pool, struct xdp_sock *xs);
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci/* AF_XDP, and XDP core. */
1008c2ecf20Sopenharmony_civoid xp_free(struct xdp_buff_xsk *xskb);
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci/* AF_XDP ZC drivers, via xdp_sock_buff.h */
1038c2ecf20Sopenharmony_civoid xp_set_rxq_info(struct xsk_buff_pool *pool, struct xdp_rxq_info *rxq);
1048c2ecf20Sopenharmony_ciint xp_dma_map(struct xsk_buff_pool *pool, struct device *dev,
1058c2ecf20Sopenharmony_ci	       unsigned long attrs, struct page **pages, u32 nr_pages);
1068c2ecf20Sopenharmony_civoid xp_dma_unmap(struct xsk_buff_pool *pool, unsigned long attrs);
1078c2ecf20Sopenharmony_cistruct xdp_buff *xp_alloc(struct xsk_buff_pool *pool);
1088c2ecf20Sopenharmony_cibool xp_can_alloc(struct xsk_buff_pool *pool, u32 count);
1098c2ecf20Sopenharmony_civoid *xp_raw_get_data(struct xsk_buff_pool *pool, u64 addr);
1108c2ecf20Sopenharmony_cidma_addr_t xp_raw_get_dma(struct xsk_buff_pool *pool, u64 addr);
1118c2ecf20Sopenharmony_cistatic inline dma_addr_t xp_get_dma(struct xdp_buff_xsk *xskb)
1128c2ecf20Sopenharmony_ci{
1138c2ecf20Sopenharmony_ci	return xskb->dma;
1148c2ecf20Sopenharmony_ci}
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cistatic inline dma_addr_t xp_get_frame_dma(struct xdp_buff_xsk *xskb)
1178c2ecf20Sopenharmony_ci{
1188c2ecf20Sopenharmony_ci	return xskb->frame_dma;
1198c2ecf20Sopenharmony_ci}
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_civoid xp_dma_sync_for_cpu_slow(struct xdp_buff_xsk *xskb);
1228c2ecf20Sopenharmony_cistatic inline void xp_dma_sync_for_cpu(struct xdp_buff_xsk *xskb)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	xp_dma_sync_for_cpu_slow(xskb);
1258c2ecf20Sopenharmony_ci}
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_civoid xp_dma_sync_for_device_slow(struct xsk_buff_pool *pool, dma_addr_t dma,
1288c2ecf20Sopenharmony_ci				 size_t size);
1298c2ecf20Sopenharmony_cistatic inline void xp_dma_sync_for_device(struct xsk_buff_pool *pool,
1308c2ecf20Sopenharmony_ci					  dma_addr_t dma, size_t size)
1318c2ecf20Sopenharmony_ci{
1328c2ecf20Sopenharmony_ci	if (!pool->dma_need_sync)
1338c2ecf20Sopenharmony_ci		return;
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	xp_dma_sync_for_device_slow(pool, dma, size);
1368c2ecf20Sopenharmony_ci}
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci/* Masks for xdp_umem_page flags.
1398c2ecf20Sopenharmony_ci * The low 12-bits of the addr will be 0 since this is the page address, so we
1408c2ecf20Sopenharmony_ci * can use them for flags.
1418c2ecf20Sopenharmony_ci */
1428c2ecf20Sopenharmony_ci#define XSK_NEXT_PG_CONTIG_SHIFT 0
1438c2ecf20Sopenharmony_ci#define XSK_NEXT_PG_CONTIG_MASK BIT_ULL(XSK_NEXT_PG_CONTIG_SHIFT)
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_cistatic inline bool xp_desc_crosses_non_contig_pg(struct xsk_buff_pool *pool,
1468c2ecf20Sopenharmony_ci						 u64 addr, u32 len)
1478c2ecf20Sopenharmony_ci{
1488c2ecf20Sopenharmony_ci	bool cross_pg = (addr & (PAGE_SIZE - 1)) + len > PAGE_SIZE;
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	if (likely(!cross_pg))
1518c2ecf20Sopenharmony_ci		return false;
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	return pool->dma_pages_cnt &&
1548c2ecf20Sopenharmony_ci	       !(pool->dma_pages[addr >> PAGE_SHIFT] & XSK_NEXT_PG_CONTIG_MASK);
1558c2ecf20Sopenharmony_ci}
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_cistatic inline u64 xp_aligned_extract_addr(struct xsk_buff_pool *pool, u64 addr)
1588c2ecf20Sopenharmony_ci{
1598c2ecf20Sopenharmony_ci	return addr & pool->chunk_mask;
1608c2ecf20Sopenharmony_ci}
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_cistatic inline u64 xp_unaligned_extract_addr(u64 addr)
1638c2ecf20Sopenharmony_ci{
1648c2ecf20Sopenharmony_ci	return addr & XSK_UNALIGNED_BUF_ADDR_MASK;
1658c2ecf20Sopenharmony_ci}
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_cistatic inline u64 xp_unaligned_extract_offset(u64 addr)
1688c2ecf20Sopenharmony_ci{
1698c2ecf20Sopenharmony_ci	return addr >> XSK_UNALIGNED_BUF_OFFSET_SHIFT;
1708c2ecf20Sopenharmony_ci}
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_cistatic inline u64 xp_unaligned_add_offset_to_addr(u64 addr)
1738c2ecf20Sopenharmony_ci{
1748c2ecf20Sopenharmony_ci	return xp_unaligned_extract_addr(addr) +
1758c2ecf20Sopenharmony_ci		xp_unaligned_extract_offset(addr);
1768c2ecf20Sopenharmony_ci}
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci#endif /* XSK_BUFF_POOL_H_ */
179