i40e: xsk: Move tmp desc array from driver to pool
[platform/kernel/linux-rpi.git] / include / net / xsk_buff_pool.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright(c) 2020 Intel Corporation. */
3
4 #ifndef XSK_BUFF_POOL_H_
5 #define XSK_BUFF_POOL_H_
6
7 #include <linux/if_xdp.h>
8 #include <linux/types.h>
9 #include <linux/dma-mapping.h>
10 #include <net/xdp.h>
11
12 struct xsk_buff_pool;
13 struct xdp_rxq_info;
14 struct xsk_queue;
15 struct xdp_desc;
16 struct xdp_umem;
17 struct xdp_sock;
18 struct device;
19 struct page;
20
21 struct xdp_buff_xsk {
22         struct xdp_buff xdp;
23         dma_addr_t dma;
24         dma_addr_t frame_dma;
25         struct xsk_buff_pool *pool;
26         bool unaligned;
27         u64 orig_addr;
28         struct list_head free_list_node;
29 };
30
31 struct xsk_dma_map {
32         dma_addr_t *dma_pages;
33         struct device *dev;
34         struct net_device *netdev;
35         refcount_t users;
36         struct list_head list; /* Protected by the RTNL_LOCK */
37         u32 dma_pages_cnt;
38         bool dma_need_sync;
39 };
40
41 struct xsk_buff_pool {
42         /* Members only used in the control path first. */
43         struct device *dev;
44         struct net_device *netdev;
45         struct list_head xsk_tx_list;
46         /* Protects modifications to the xsk_tx_list */
47         spinlock_t xsk_tx_list_lock;
48         refcount_t users;
49         struct xdp_umem *umem;
50         struct work_struct work;
51         struct list_head free_list;
52         u32 heads_cnt;
53         u16 queue_id;
54
55         /* Data path members as close to free_heads at the end as possible. */
56         struct xsk_queue *fq ____cacheline_aligned_in_smp;
57         struct xsk_queue *cq;
58         /* For performance reasons, each buff pool has its own array of dma_pages
59          * even when they are identical.
60          */
61         dma_addr_t *dma_pages;
62         struct xdp_buff_xsk *heads;
63         struct xdp_desc *tx_descs;
64         u64 chunk_mask;
65         u64 addrs_cnt;
66         u32 free_list_cnt;
67         u32 dma_pages_cnt;
68         u32 free_heads_cnt;
69         u32 headroom;
70         u32 chunk_size;
71         u32 frame_len;
72         u8 cached_need_wakeup;
73         bool uses_need_wakeup;
74         bool dma_need_sync;
75         bool unaligned;
76         void *addrs;
77         /* Mutual exclusion of the completion ring in the SKB mode. Two cases to protect:
78          * NAPI TX thread and sendmsg error paths in the SKB destructor callback and when
79          * sockets share a single cq when the same netdev and queue id is shared.
80          */
81         spinlock_t cq_lock;
82         struct xdp_buff_xsk *free_heads[];
83 };
84
85 /* AF_XDP core. */
86 struct xsk_buff_pool *xp_create_and_assign_umem(struct xdp_sock *xs,
87                                                 struct xdp_umem *umem);
88 int xp_assign_dev(struct xsk_buff_pool *pool, struct net_device *dev,
89                   u16 queue_id, u16 flags);
90 int xp_assign_dev_shared(struct xsk_buff_pool *pool, struct xdp_umem *umem,
91                          struct net_device *dev, u16 queue_id);
92 void xp_destroy(struct xsk_buff_pool *pool);
93 void xp_release(struct xdp_buff_xsk *xskb);
94 void xp_get_pool(struct xsk_buff_pool *pool);
95 bool xp_put_pool(struct xsk_buff_pool *pool);
96 void xp_clear_dev(struct xsk_buff_pool *pool);
97 void xp_add_xsk(struct xsk_buff_pool *pool, struct xdp_sock *xs);
98 void xp_del_xsk(struct xsk_buff_pool *pool, struct xdp_sock *xs);
99
100 /* AF_XDP, and XDP core. */
101 void xp_free(struct xdp_buff_xsk *xskb);
102
103 /* AF_XDP ZC drivers, via xdp_sock_buff.h */
104 void xp_set_rxq_info(struct xsk_buff_pool *pool, struct xdp_rxq_info *rxq);
105 int xp_dma_map(struct xsk_buff_pool *pool, struct device *dev,
106                unsigned long attrs, struct page **pages, u32 nr_pages);
107 void xp_dma_unmap(struct xsk_buff_pool *pool, unsigned long attrs);
108 struct xdp_buff *xp_alloc(struct xsk_buff_pool *pool);
109 bool xp_can_alloc(struct xsk_buff_pool *pool, u32 count);
110 void *xp_raw_get_data(struct xsk_buff_pool *pool, u64 addr);
111 dma_addr_t xp_raw_get_dma(struct xsk_buff_pool *pool, u64 addr);
112 static inline dma_addr_t xp_get_dma(struct xdp_buff_xsk *xskb)
113 {
114         return xskb->dma;
115 }
116
117 static inline dma_addr_t xp_get_frame_dma(struct xdp_buff_xsk *xskb)
118 {
119         return xskb->frame_dma;
120 }
121
122 void xp_dma_sync_for_cpu_slow(struct xdp_buff_xsk *xskb);
123 static inline void xp_dma_sync_for_cpu(struct xdp_buff_xsk *xskb)
124 {
125         xp_dma_sync_for_cpu_slow(xskb);
126 }
127
128 void xp_dma_sync_for_device_slow(struct xsk_buff_pool *pool, dma_addr_t dma,
129                                  size_t size);
130 static inline void xp_dma_sync_for_device(struct xsk_buff_pool *pool,
131                                           dma_addr_t dma, size_t size)
132 {
133         if (!pool->dma_need_sync)
134                 return;
135
136         xp_dma_sync_for_device_slow(pool, dma, size);
137 }
138
139 /* Masks for xdp_umem_page flags.
140  * The low 12-bits of the addr will be 0 since this is the page address, so we
141  * can use them for flags.
142  */
143 #define XSK_NEXT_PG_CONTIG_SHIFT 0
144 #define XSK_NEXT_PG_CONTIG_MASK BIT_ULL(XSK_NEXT_PG_CONTIG_SHIFT)
145
146 static inline bool xp_desc_crosses_non_contig_pg(struct xsk_buff_pool *pool,
147                                                  u64 addr, u32 len)
148 {
149         bool cross_pg = (addr & (PAGE_SIZE - 1)) + len > PAGE_SIZE;
150
151         if (likely(!cross_pg))
152                 return false;
153
154         if (pool->dma_pages_cnt) {
155                 return !(pool->dma_pages[addr >> PAGE_SHIFT] &
156                          XSK_NEXT_PG_CONTIG_MASK);
157         }
158
159         /* skb path */
160         return addr + len > pool->addrs_cnt;
161 }
162
163 static inline u64 xp_aligned_extract_addr(struct xsk_buff_pool *pool, u64 addr)
164 {
165         return addr & pool->chunk_mask;
166 }
167
168 static inline u64 xp_unaligned_extract_addr(u64 addr)
169 {
170         return addr & XSK_UNALIGNED_BUF_ADDR_MASK;
171 }
172
173 static inline u64 xp_unaligned_extract_offset(u64 addr)
174 {
175         return addr >> XSK_UNALIGNED_BUF_OFFSET_SHIFT;
176 }
177
178 static inline u64 xp_unaligned_add_offset_to_addr(u64 addr)
179 {
180         return xp_unaligned_extract_addr(addr) +
181                 xp_unaligned_extract_offset(addr);
182 }
183
184 #endif /* XSK_BUFF_POOL_H_ */