| 1 |
// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB |
| 2 |
/* Copyright (c) 2019 Mellanox Technologies. */ |
| 3 |
|
| 4 |
#include "rx.h" |
| 5 |
#include "en/xdp.h" |
| 6 |
#include <net/xdp_sock_drv.h> |
| 7 |
#include <linux/filter.h> |
| 8 |
|
| 9 |
/* RX data path */ |
| 10 |
|
| 11 |
static struct mlx5e_xdp_buff *xsk_buff_to_mxbuf(struct xdp_buff *xdp) |
| 12 |
{ |
| 13 |
/* mlx5e_xdp_buff shares its layout with xdp_buff_xsk |
| 14 |
* and private mlx5e_xdp_buff fields fall into xdp_buff_xsk->cb |
| 15 |
*/ |
| 16 |
return (struct mlx5e_xdp_buff *)xdp; |
| 17 |
} |
| 18 |
|
| 19 |
int mlx5e_xsk_alloc_rx_mpwqe(struct mlx5e_rq *rq, u16 ix) |
| 20 |
{ |
| 21 |
struct mlx5e_mpw_info *wi = mlx5e_get_mpw_info(rq, ix); |
| 22 |
struct mlx5e_icosq *icosq = rq->icosq; |
| 23 |
struct mlx5_wq_cyc *wq = &icosq->wq; |
| 24 |
struct mlx5e_umr_wqe *umr_wqe; |
| 25 |
struct xdp_buff **xsk_buffs; |
| 26 |
bool sync_locked; |
| 27 |
int batch, i; |
| 28 |
u32 offset; /* 17-bit value with MTT. */ |
| 29 |
u16 pi; |
| 30 |
|
| 31 |
if (unlikely(!xsk_buff_can_alloc(rq->xsk_pool, rq->mpwqe.pages_per_wqe))) |
| 32 |
goto err; |
| 33 |
|
| 34 |
XSK_CHECK_PRIV_TYPE(struct mlx5e_xdp_buff); |
| 35 |
xsk_buffs = (struct xdp_buff **)wi->alloc_units.xsk_buffs; |
| 36 |
batch = xsk_buff_alloc_batch(rq->xsk_pool, xsk_buffs, |
| 37 |
rq->mpwqe.pages_per_wqe); |
| 38 |
|
| 39 |
/* If batch < pages_per_wqe, either: |
| 40 |
* 1. Some (or all) descriptors were invalid. |
| 41 |
* 2. dma_need_sync is true, and it fell back to allocating one frame. |
| 42 |
* In either case, try to continue allocating frames one by one, until |
| 43 |
* the first error, which will mean there are no more valid descriptors. |
| 44 |
*/ |
| 45 |
for (; batch < rq->mpwqe.pages_per_wqe; batch++) { |
| 46 |
xsk_buffs[batch] = xsk_buff_alloc(rq->xsk_pool); |
| 47 |
if (unlikely(!xsk_buffs[batch])) |
| 48 |
goto err_reuse_batch; |
| 49 |
} |
| 50 |
|
| 51 |
sync_locked = mlx5e_icosq_sync_lock(icosq); |
| 52 |
pi = mlx5e_icosq_get_next_pi(icosq, rq->mpwqe.umr_wqebbs); |
| 53 |
umr_wqe = mlx5_wq_cyc_get_wqe(wq, pi); |
| 54 |
memcpy(umr_wqe, &rq->mpwqe.umr_wqe, sizeof(struct mlx5e_umr_wqe)); |
| 55 |
|
| 56 |
if (likely(rq->mpwqe.umr_mode == MLX5E_MPWRQ_UMR_MODE_ALIGNED)) { |
| 57 |
for (i = 0; i < batch; i++) { |
| 58 |
struct mlx5e_xdp_buff *mxbuf = xsk_buff_to_mxbuf(xsk_buffs[i]); |
| 59 |
dma_addr_t addr = xsk_buff_xdp_get_frame_dma(xsk_buffs[i]); |
| 60 |
|
| 61 |
umr_wqe->inline_mtts[i] = (struct mlx5_mtt) { |
| 62 |
.ptag = cpu_to_be64(addr | MLX5_EN_WR), |
| 63 |
}; |
| 64 |
mxbuf->rq = rq; |
| 65 |
} |
| 66 |
} else if (unlikely(rq->mpwqe.umr_mode == MLX5E_MPWRQ_UMR_MODE_UNALIGNED)) { |
| 67 |
for (i = 0; i < batch; i++) { |
| 68 |
struct mlx5e_xdp_buff *mxbuf = xsk_buff_to_mxbuf(xsk_buffs[i]); |
| 69 |
dma_addr_t addr = xsk_buff_xdp_get_frame_dma(xsk_buffs[i]); |
| 70 |
|
| 71 |
umr_wqe->inline_ksms[i] = (struct mlx5_ksm) { |
| 72 |
.key = rq->mkey_be, |
| 73 |
.va = cpu_to_be64(addr), |
| 74 |
}; |
| 75 |
mxbuf->rq = rq; |
| 76 |
} |
| 77 |
} else if (likely(rq->mpwqe.umr_mode == MLX5E_MPWRQ_UMR_MODE_TRIPLE)) { |
| 78 |
u32 mapping_size = 1 << (rq->mpwqe.page_shift - 2); |
| 79 |
|
| 80 |
for (i = 0; i < batch; i++) { |
| 81 |
struct mlx5e_xdp_buff *mxbuf = xsk_buff_to_mxbuf(xsk_buffs[i]); |
| 82 |
dma_addr_t addr = xsk_buff_xdp_get_frame_dma(xsk_buffs[i]); |
| 83 |
|
| 84 |
umr_wqe->inline_ksms[i << 2] = (struct mlx5_ksm) { |
| 85 |
.key = rq->mkey_be, |
| 86 |
.va = cpu_to_be64(addr), |
| 87 |
}; |
| 88 |
umr_wqe->inline_ksms[(i << 2) + 1] = (struct mlx5_ksm) { |
| 89 |
.key = rq->mkey_be, |
| 90 |
.va = cpu_to_be64(addr + mapping_size), |
| 91 |
}; |
| 92 |
umr_wqe->inline_ksms[(i << 2) + 2] = (struct mlx5_ksm) { |
| 93 |
.key = rq->mkey_be, |
| 94 |
.va = cpu_to_be64(addr + mapping_size * 2), |
| 95 |
}; |
| 96 |
umr_wqe->inline_ksms[(i << 2) + 3] = (struct mlx5_ksm) { |
| 97 |
.key = rq->mkey_be, |
| 98 |
.va = cpu_to_be64(rq->wqe_overflow.addr), |
| 99 |
}; |
| 100 |
mxbuf->rq = rq; |
| 101 |
} |
| 102 |
} else { |
| 103 |
__be32 pad_size = cpu_to_be32((1 << rq->mpwqe.page_shift) - |
| 104 |
rq->xsk_pool->chunk_size); |
| 105 |
__be32 frame_size = cpu_to_be32(rq->xsk_pool->chunk_size); |
| 106 |
|
| 107 |
for (i = 0; i < batch; i++) { |
| 108 |
struct mlx5e_xdp_buff *mxbuf = xsk_buff_to_mxbuf(xsk_buffs[i]); |
| 109 |
dma_addr_t addr = xsk_buff_xdp_get_frame_dma(xsk_buffs[i]); |
| 110 |
|
| 111 |
umr_wqe->inline_klms[i << 1] = (struct mlx5_klm) { |
| 112 |
.key = rq->mkey_be, |
| 113 |
.va = cpu_to_be64(addr), |
| 114 |
.bcount = frame_size, |
| 115 |
}; |
| 116 |
umr_wqe->inline_klms[(i << 1) + 1] = (struct mlx5_klm) { |
| 117 |
.key = rq->mkey_be, |
| 118 |
.va = cpu_to_be64(rq->wqe_overflow.addr), |
| 119 |
.bcount = pad_size, |
| 120 |
}; |
| 121 |
mxbuf->rq = rq; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
bitmap_zero(wi->skip_release_bitmap, rq->mpwqe.pages_per_wqe); |
| 126 |
wi->consumed_strides = 0; |
| 127 |
|
| 128 |
umr_wqe->hdr.ctrl.opmod_idx_opcode = |
| 129 |
cpu_to_be32((icosq->pc << MLX5_WQE_CTRL_WQE_INDEX_SHIFT) | MLX5_OPCODE_UMR); |
| 130 |
|
| 131 |
/* Optimized for speed: keep in sync with mlx5e_mpwrq_umr_entry_size. */ |
| 132 |
offset = ix * rq->mpwqe.mtts_per_wqe; |
| 133 |
if (likely(rq->mpwqe.umr_mode == MLX5E_MPWRQ_UMR_MODE_ALIGNED)) |
| 134 |
offset = offset * sizeof(struct mlx5_mtt) / MLX5_OCTWORD; |
| 135 |
else if (unlikely(rq->mpwqe.umr_mode == MLX5E_MPWRQ_UMR_MODE_OVERSIZED)) |
| 136 |
offset = offset * sizeof(struct mlx5_klm) * 2 / MLX5_OCTWORD; |
| 137 |
else if (unlikely(rq->mpwqe.umr_mode == MLX5E_MPWRQ_UMR_MODE_TRIPLE)) |
| 138 |
offset = offset * sizeof(struct mlx5_ksm) * 4 / MLX5_OCTWORD; |
| 139 |
umr_wqe->hdr.uctrl.xlt_offset = cpu_to_be16(offset); |
| 140 |
|
| 141 |
icosq->db.wqe_info[pi] = (struct mlx5e_icosq_wqe_info) { |
| 142 |
.wqe_type = MLX5E_ICOSQ_WQE_UMR_RX, |
| 143 |
.num_wqebbs = rq->mpwqe.umr_wqebbs, |
| 144 |
.umr.rq = rq, |
| 145 |
}; |
| 146 |
|
| 147 |
icosq->pc += rq->mpwqe.umr_wqebbs; |
| 148 |
mlx5e_icosq_sync_unlock(icosq, sync_locked); |
| 149 |
|
| 150 |
icosq->doorbell_cseg = &umr_wqe->hdr.ctrl; |
| 151 |
|
| 152 |
return 0; |
| 153 |
|
| 154 |
err_reuse_batch: |
| 155 |
while (--batch >= 0) |
| 156 |
xsk_buff_free(xsk_buffs[batch]); |
| 157 |
|
| 158 |
err: |
| 159 |
rq->stats->buff_alloc_err++; |
| 160 |
return -ENOMEM; |
| 161 |
} |
| 162 |
|
| 163 |
int mlx5e_xsk_alloc_rx_wqes_batched(struct mlx5e_rq *rq, u16 ix, int wqe_bulk) |
| 164 |
{ |
| 165 |
struct mlx5_wq_cyc *wq = &rq->wqe.wq; |
| 166 |
struct xdp_buff **buffs; |
| 167 |
u32 contig, alloc; |
| 168 |
int i; |
| 169 |
|
| 170 |
/* Each rq->wqe.frags->xskp is 1:1 mapped to an element inside the |
| 171 |
* rq->wqe.alloc_units->xsk_buffs array allocated here. |
| 172 |
*/ |
| 173 |
buffs = rq->wqe.alloc_units->xsk_buffs; |
| 174 |
contig = mlx5_wq_cyc_get_size(wq) - ix; |
| 175 |
if (wqe_bulk <= contig) { |
| 176 |
alloc = xsk_buff_alloc_batch(rq->xsk_pool, buffs + ix, wqe_bulk); |
| 177 |
} else { |
| 178 |
alloc = xsk_buff_alloc_batch(rq->xsk_pool, buffs + ix, contig); |
| 179 |
if (likely(alloc == contig)) |
| 180 |
alloc += xsk_buff_alloc_batch(rq->xsk_pool, buffs, wqe_bulk - contig); |
| 181 |
} |
| 182 |
|
| 183 |
for (i = 0; i < alloc; i++) { |
| 184 |
int j = mlx5_wq_cyc_ctr2ix(wq, ix + i); |
| 185 |
struct mlx5e_wqe_frag_info *frag; |
| 186 |
struct mlx5e_rx_wqe_cyc *wqe; |
| 187 |
dma_addr_t addr; |
| 188 |
|
| 189 |
wqe = mlx5_wq_cyc_get_wqe(wq, j); |
| 190 |
/* Assumes log_num_frags == 0. */ |
| 191 |
frag = &rq->wqe.frags[j]; |
| 192 |
|
| 193 |
addr = xsk_buff_xdp_get_frame_dma(*frag->xskp); |
| 194 |
wqe->data[0].addr = cpu_to_be64(addr + rq->buff.headroom); |
| 195 |
frag->flags &= ~BIT(MLX5E_WQE_FRAG_SKIP_RELEASE); |
| 196 |
} |
| 197 |
|
| 198 |
return alloc; |
| 199 |
} |
| 200 |
|
| 201 |
int mlx5e_xsk_alloc_rx_wqes(struct mlx5e_rq *rq, u16 ix, int wqe_bulk) |
| 202 |
{ |
| 203 |
struct mlx5_wq_cyc *wq = &rq->wqe.wq; |
| 204 |
int i; |
| 205 |
|
| 206 |
for (i = 0; i < wqe_bulk; i++) { |
| 207 |
int j = mlx5_wq_cyc_ctr2ix(wq, ix + i); |
| 208 |
struct mlx5e_wqe_frag_info *frag; |
| 209 |
struct mlx5e_rx_wqe_cyc *wqe; |
| 210 |
dma_addr_t addr; |
| 211 |
|
| 212 |
wqe = mlx5_wq_cyc_get_wqe(wq, j); |
| 213 |
/* Assumes log_num_frags == 0. */ |
| 214 |
frag = &rq->wqe.frags[j]; |
| 215 |
|
| 216 |
*frag->xskp = xsk_buff_alloc(rq->xsk_pool); |
| 217 |
if (unlikely(!*frag->xskp)) |
| 218 |
return i; |
| 219 |
|
| 220 |
addr = xsk_buff_xdp_get_frame_dma(*frag->xskp); |
| 221 |
wqe->data[0].addr = cpu_to_be64(addr + rq->buff.headroom); |
| 222 |
frag->flags &= ~BIT(MLX5E_WQE_FRAG_SKIP_RELEASE); |
| 223 |
} |
| 224 |
|
| 225 |
return wqe_bulk; |
| 226 |
} |
| 227 |
|
| 228 |
static struct sk_buff *mlx5e_xsk_construct_skb(struct mlx5e_rq *rq, struct xdp_buff *xdp) |
| 229 |
{ |
| 230 |
u32 totallen = xdp->data_end - xdp->data_meta; |
| 231 |
u32 metalen = xdp->data - xdp->data_meta; |
| 232 |
struct sk_buff *skb; |
| 233 |
|
| 234 |
skb = napi_alloc_skb(rq->cq.napi, totallen); |
| 235 |
if (unlikely(!skb)) { |
| 236 |
rq->stats->buff_alloc_err++; |
| 237 |
return NULL; |
| 238 |
} |
| 239 |
|
| 240 |
skb_put_data(skb, xdp->data_meta, totallen); |
| 241 |
|
| 242 |
if (metalen) { |
| 243 |
skb_metadata_set(skb, metalen); |
| 244 |
__skb_pull(skb, metalen); |
| 245 |
} |
| 246 |
|
| 247 |
return skb; |
| 248 |
} |
| 249 |
|
| 250 |
struct sk_buff *mlx5e_xsk_skb_from_cqe_mpwrq_linear(struct mlx5e_rq *rq, |
| 251 |
struct mlx5e_mpw_info *wi, |
| 252 |
struct mlx5_cqe64 *cqe, |
| 253 |
u16 cqe_bcnt, |
| 254 |
u32 head_offset, |
| 255 |
u32 page_idx) |
| 256 |
{ |
| 257 |
struct mlx5e_xdp_buff *mxbuf = xsk_buff_to_mxbuf(wi->alloc_units.xsk_buffs[page_idx]); |
| 258 |
struct bpf_prog *prog; |
| 259 |
|
| 260 |
/* Check packet size. Note LRO doesn't use linear SKB */ |
| 261 |
if (unlikely(cqe_bcnt > rq->hw_mtu)) { |
| 262 |
rq->stats->oversize_pkts_sw_drop++; |
| 263 |
return NULL; |
| 264 |
} |
| 265 |
|
| 266 |
/* head_offset is not used in this function, because xdp->data and the |
| 267 |
* DMA address point directly to the necessary place. Furthermore, in |
| 268 |
* the current implementation, UMR pages are mapped to XSK frames, so |
| 269 |
* head_offset should always be 0. |
| 270 |
*/ |
| 271 |
WARN_ON_ONCE(head_offset); |
| 272 |
|
| 273 |
/* mxbuf->rq is set on allocation, but cqe is per-packet so set it here */ |
| 274 |
mxbuf->cqe = cqe; |
| 275 |
xsk_buff_set_size(&mxbuf->xdp, cqe_bcnt); |
| 276 |
xsk_buff_dma_sync_for_cpu(&mxbuf->xdp); |
| 277 |
net_prefetch(mxbuf->xdp.data); |
| 278 |
|
| 279 |
/* Possible flows: |
| 280 |
* - XDP_REDIRECT to XSKMAP: |
| 281 |
* The page is owned by the userspace from now. |
| 282 |
* - XDP_TX and other XDP_REDIRECTs: |
| 283 |
* The page was returned by ZCA and recycled. |
| 284 |
* - XDP_DROP: |
| 285 |
* Recycle the page. |
| 286 |
* - XDP_PASS: |
| 287 |
* Allocate an SKB, copy the data and recycle the page. |
| 288 |
* |
| 289 |
* Pages to be recycled go to the Reuse Ring on MPWQE deallocation. Its |
| 290 |
* size is the same as the Driver RX Ring's size, and pages for WQEs are |
| 291 |
* allocated first from the Reuse Ring, so it has enough space. |
| 292 |
*/ |
| 293 |
|
| 294 |
prog = rcu_dereference(rq->xdp_prog); |
| 295 |
if (likely(prog && mlx5e_xdp_handle(rq, prog, mxbuf))) { |
| 296 |
if (likely(__test_and_clear_bit(MLX5E_RQ_FLAG_XDP_XMIT, rq->flags))) |
| 297 |
__set_bit(page_idx, wi->skip_release_bitmap); /* non-atomic */ |
| 298 |
return NULL; /* page/packet was consumed by XDP */ |
| 299 |
} |
| 300 |
|
| 301 |
/* XDP_PASS: copy the data from the UMEM to a new SKB and reuse the |
| 302 |
* frame. On SKB allocation failure, NULL is returned. |
| 303 |
*/ |
| 304 |
return mlx5e_xsk_construct_skb(rq, &mxbuf->xdp); |
| 305 |
} |
| 306 |
|
| 307 |
struct sk_buff *mlx5e_xsk_skb_from_cqe_linear(struct mlx5e_rq *rq, |
| 308 |
struct mlx5e_wqe_frag_info *wi, |
| 309 |
struct mlx5_cqe64 *cqe, |
| 310 |
u32 cqe_bcnt) |
| 311 |
{ |
| 312 |
struct mlx5e_xdp_buff *mxbuf = xsk_buff_to_mxbuf(*wi->xskp); |
| 313 |
struct bpf_prog *prog; |
| 314 |
|
| 315 |
/* wi->offset is not used in this function, because xdp->data and the |
| 316 |
* DMA address point directly to the necessary place. Furthermore, the |
| 317 |
* XSK allocator allocates frames per packet, instead of pages, so |
| 318 |
* wi->offset should always be 0. |
| 319 |
*/ |
| 320 |
WARN_ON_ONCE(wi->offset); |
| 321 |
|
| 322 |
/* mxbuf->rq is set on allocation, but cqe is per-packet so set it here */ |
| 323 |
mxbuf->cqe = cqe; |
| 324 |
xsk_buff_set_size(&mxbuf->xdp, cqe_bcnt); |
| 325 |
xsk_buff_dma_sync_for_cpu(&mxbuf->xdp); |
| 326 |
net_prefetch(mxbuf->xdp.data); |
| 327 |
|
| 328 |
prog = rcu_dereference(rq->xdp_prog); |
| 329 |
if (likely(prog && mlx5e_xdp_handle(rq, prog, mxbuf))) { |
| 330 |
if (likely(__test_and_clear_bit(MLX5E_RQ_FLAG_XDP_XMIT, rq->flags))) |
| 331 |
wi->flags |= BIT(MLX5E_WQE_FRAG_SKIP_RELEASE); |
| 332 |
return NULL; /* page/packet was consumed by XDP */ |
| 333 |
} |
| 334 |
|
| 335 |
/* XDP_PASS: copy the data from the UMEM to a new SKB. The frame reuse |
| 336 |
* will be handled by mlx5e_free_rx_wqe. |
| 337 |
* On SKB allocation failure, NULL is returned. |
| 338 |
*/ |
| 339 |
return mlx5e_xsk_construct_skb(rq, &mxbuf->xdp); |
| 340 |
} |