2016-09-21 11:44:01 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2016 Netronome Systems, Inc.
|
|
|
|
|
*
|
|
|
|
|
* This software is dual licensed under the GNU General License Version 2,
|
|
|
|
|
* June 1991 as shown in the file COPYING in the top-level directory of this
|
|
|
|
|
* source tree or the BSD 2-Clause License provided below. You have the
|
|
|
|
|
* option to license this software under the complete terms of either license.
|
|
|
|
|
*
|
|
|
|
|
* The BSD 2-Clause License:
|
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms, with or
|
|
|
|
|
* without modification, are permitted provided that the following
|
|
|
|
|
* conditions are met:
|
|
|
|
|
*
|
|
|
|
|
* 1. Redistributions of source code must retain the above
|
|
|
|
|
* copyright notice, this list of conditions and the following
|
|
|
|
|
* disclaimer.
|
|
|
|
|
*
|
|
|
|
|
* 2. Redistributions in binary form must reproduce the above
|
|
|
|
|
* copyright notice, this list of conditions and the following
|
|
|
|
|
* disclaimer in the documentation and/or other materials
|
|
|
|
|
* provided with the distribution.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
|
|
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* nfp_net_offload.c
|
|
|
|
|
* Netronome network device driver: TC offload functions for PF and VF
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
|
#include <linux/netdevice.h>
|
|
|
|
|
#include <linux/pci.h>
|
|
|
|
|
#include <linux/jiffies.h>
|
|
|
|
|
#include <linux/timer.h>
|
|
|
|
|
#include <linux/list.h>
|
|
|
|
|
|
|
|
|
|
#include <net/pkt_cls.h>
|
|
|
|
|
#include <net/tc_act/tc_gact.h>
|
|
|
|
|
#include <net/tc_act/tc_mirred.h>
|
|
|
|
|
|
2017-05-31 08:06:48 -07:00
|
|
|
#include "main.h"
|
|
|
|
|
#include "../nfp_net_ctrl.h"
|
|
|
|
|
#include "../nfp_net.h"
|
2016-09-21 11:44:01 +01:00
|
|
|
|
|
|
|
|
static int
|
2017-11-03 13:56:25 -07:00
|
|
|
nfp_net_bpf_offload_prepare(struct nfp_net *nn, struct bpf_prog *prog,
|
2016-09-21 11:44:01 +01:00
|
|
|
struct nfp_bpf_result *res,
|
|
|
|
|
void **code, dma_addr_t *dma_addr, u16 max_instr)
|
|
|
|
|
{
|
|
|
|
|
unsigned int code_sz = max_instr * sizeof(u64);
|
2017-10-23 11:58:08 -07:00
|
|
|
unsigned int stack_size;
|
2016-09-21 11:44:01 +01:00
|
|
|
u16 start_off, done_off;
|
|
|
|
|
unsigned int max_mtu;
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
|
max_mtu = nn_readb(nn, NFP_NET_CFG_BPF_INL_MTU) * 64 - 32;
|
2017-03-10 10:38:27 -08:00
|
|
|
if (max_mtu < nn->dp.netdev->mtu) {
|
2016-09-21 11:44:01 +01:00
|
|
|
nn_info(nn, "BPF offload not supported with MTU larger than HW packet split boundary\n");
|
2017-04-27 21:06:15 -07:00
|
|
|
return -EOPNOTSUPP;
|
2016-09-21 11:44:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
start_off = nn_readw(nn, NFP_NET_CFG_BPF_START);
|
|
|
|
|
done_off = nn_readw(nn, NFP_NET_CFG_BPF_DONE);
|
|
|
|
|
|
2017-10-23 11:58:08 -07:00
|
|
|
stack_size = nn_readb(nn, NFP_NET_CFG_BPF_STACK_SZ) * 64;
|
2017-11-03 13:56:25 -07:00
|
|
|
if (prog->aux->stack_depth > stack_size) {
|
2017-10-23 11:58:08 -07:00
|
|
|
nn_info(nn, "stack too large: program %dB > FW stack %dB\n",
|
2017-11-03 13:56:25 -07:00
|
|
|
prog->aux->stack_depth, stack_size);
|
2017-10-23 11:58:08 -07:00
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-10 10:38:27 -08:00
|
|
|
*code = dma_zalloc_coherent(nn->dp.dev, code_sz, dma_addr, GFP_KERNEL);
|
2016-09-21 11:44:01 +01:00
|
|
|
if (!*code)
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
2017-11-03 13:56:25 -07:00
|
|
|
ret = nfp_bpf_jit(prog, *code, start_off, done_off, max_instr, res);
|
2016-09-21 11:44:01 +01:00
|
|
|
if (ret)
|
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
out:
|
2017-03-10 10:38:27 -08:00
|
|
|
dma_free_coherent(nn->dp.dev, code_sz, *code, *dma_addr);
|
2016-09-21 11:44:01 +01:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2017-11-03 13:56:26 -07:00
|
|
|
nfp_net_bpf_load(struct nfp_net *nn, void *code, dma_addr_t dma_addr,
|
|
|
|
|
unsigned int code_sz, unsigned int n_instr)
|
2016-09-21 11:44:01 +01:00
|
|
|
{
|
|
|
|
|
int err;
|
|
|
|
|
|
|
|
|
|
nn_writew(nn, NFP_NET_CFG_BPF_SIZE, n_instr);
|
2017-11-03 13:56:23 -07:00
|
|
|
nn_writeq(nn, NFP_NET_CFG_BPF_ADDR, dma_addr);
|
2016-09-21 11:44:01 +01:00
|
|
|
|
|
|
|
|
/* Load up the JITed code */
|
|
|
|
|
err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_BPF);
|
|
|
|
|
if (err)
|
|
|
|
|
nn_err(nn, "FW command error while loading BPF: %d\n", err);
|
|
|
|
|
|
2017-11-03 13:56:26 -07:00
|
|
|
dma_free_coherent(nn->dp.dev, code_sz, code, dma_addr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void nfp_net_bpf_start(struct nfp_net *nn)
|
|
|
|
|
{
|
|
|
|
|
int err;
|
|
|
|
|
|
2016-09-21 11:44:01 +01:00
|
|
|
/* Enable passing packets through BPF function */
|
2017-03-10 10:38:27 -08:00
|
|
|
nn->dp.ctrl |= NFP_NET_CFG_CTRL_BPF;
|
|
|
|
|
nn_writel(nn, NFP_NET_CFG_CTRL, nn->dp.ctrl);
|
2016-09-21 11:44:01 +01:00
|
|
|
err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_GEN);
|
|
|
|
|
if (err)
|
|
|
|
|
nn_err(nn, "FW command error while enabling BPF: %d\n", err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int nfp_net_bpf_stop(struct nfp_net *nn)
|
|
|
|
|
{
|
2017-03-10 10:38:27 -08:00
|
|
|
if (!(nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF))
|
2016-09-21 11:44:01 +01:00
|
|
|
return 0;
|
|
|
|
|
|
2017-03-10 10:38:27 -08:00
|
|
|
nn->dp.ctrl &= ~NFP_NET_CFG_CTRL_BPF;
|
|
|
|
|
nn_writel(nn, NFP_NET_CFG_CTRL, nn->dp.ctrl);
|
2016-09-21 11:44:01 +01:00
|
|
|
|
|
|
|
|
return nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_GEN);
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-03 13:56:25 -07:00
|
|
|
int nfp_net_bpf_offload(struct nfp_net *nn, struct bpf_prog *prog,
|
2017-11-03 13:56:26 -07:00
|
|
|
bool old_prog)
|
2016-09-21 11:44:01 +01:00
|
|
|
{
|
|
|
|
|
struct nfp_bpf_result res;
|
|
|
|
|
dma_addr_t dma_addr;
|
|
|
|
|
u16 max_instr;
|
|
|
|
|
void *code;
|
|
|
|
|
int err;
|
|
|
|
|
|
2017-11-03 13:56:26 -07:00
|
|
|
if (prog && old_prog) {
|
|
|
|
|
u8 cap;
|
|
|
|
|
|
|
|
|
|
cap = nn_readb(nn, NFP_NET_CFG_BPF_CAP);
|
|
|
|
|
if (!(cap & NFP_NET_BPF_CAP_RELO)) {
|
|
|
|
|
nn_err(nn, "FW does not support live reload\n");
|
|
|
|
|
return -EBUSY;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-03 13:56:25 -07:00
|
|
|
|
|
|
|
|
/* Something else is loaded, different program type? */
|
|
|
|
|
if (!old_prog && nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF)
|
|
|
|
|
return -EBUSY;
|
|
|
|
|
|
2017-11-03 13:56:26 -07:00
|
|
|
if (old_prog && !prog)
|
|
|
|
|
return nfp_net_bpf_stop(nn);
|
2016-09-21 11:44:01 +01:00
|
|
|
|
2017-11-03 13:56:26 -07:00
|
|
|
max_instr = nn_readw(nn, NFP_NET_CFG_BPF_MAX_LEN);
|
2016-09-21 11:44:01 +01:00
|
|
|
|
2017-11-03 13:56:26 -07:00
|
|
|
err = nfp_net_bpf_offload_prepare(nn, prog, &res, &code, &dma_addr,
|
|
|
|
|
max_instr);
|
|
|
|
|
if (err)
|
|
|
|
|
return err;
|
2016-09-21 11:44:01 +01:00
|
|
|
|
2017-11-03 13:56:26 -07:00
|
|
|
nfp_net_bpf_load(nn, code, dma_addr, max_instr * sizeof(u64),
|
|
|
|
|
res.n_instr);
|
|
|
|
|
if (!old_prog)
|
|
|
|
|
nfp_net_bpf_start(nn);
|
2016-09-21 11:44:01 +01:00
|
|
|
|
2017-11-03 13:56:25 -07:00
|
|
|
return 0;
|
2016-09-21 11:44:01 +01:00
|
|
|
}
|