blob: f4b9a46c844dfd988e3b878252ff3f2f9df39b6e [file] [log] [blame]
Jakub Kicinski7533fdc2016-09-21 11:44:01 +01001/*
2 * Copyright (C) 2016 Netronome Systems, Inc.
3 *
4 * This software is dual licensed under the GNU General License Version 2,
5 * June 1991 as shown in the file COPYING in the top-level directory of this
6 * source tree or the BSD 2-Clause License provided below. You have the
7 * option to license this software under the complete terms of either license.
8 *
9 * The BSD 2-Clause License:
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 *
15 * 1. Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
19 * 2. Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
32 */
33
34/*
35 * nfp_net_offload.c
36 * Netronome network device driver: TC offload functions for PF and VF
37 */
38
39#include <linux/kernel.h>
40#include <linux/netdevice.h>
41#include <linux/pci.h>
42#include <linux/jiffies.h>
43#include <linux/timer.h>
44#include <linux/list.h>
45
46#include <net/pkt_cls.h>
47#include <net/tc_act/tc_gact.h>
48#include <net/tc_act/tc_mirred.h>
49
Jakub Kicinskid9ae7f22017-05-31 08:06:48 -070050#include "main.h"
51#include "../nfp_net_ctrl.h"
52#include "../nfp_net.h"
Jakub Kicinski7533fdc2016-09-21 11:44:01 +010053
Jakub Kicinski7533fdc2016-09-21 11:44:01 +010054static int
Jakub Kicinski9ce7a952017-11-03 13:56:25 -070055nfp_net_bpf_offload_prepare(struct nfp_net *nn, struct bpf_prog *prog,
Jakub Kicinski7533fdc2016-09-21 11:44:01 +010056 struct nfp_bpf_result *res,
57 void **code, dma_addr_t *dma_addr, u16 max_instr)
58{
59 unsigned int code_sz = max_instr * sizeof(u64);
Jakub Kicinskiee9133a2017-10-23 11:58:08 -070060 unsigned int stack_size;
Jakub Kicinski7533fdc2016-09-21 11:44:01 +010061 u16 start_off, done_off;
62 unsigned int max_mtu;
63 int ret;
64
Jakub Kicinski7533fdc2016-09-21 11:44:01 +010065 max_mtu = nn_readb(nn, NFP_NET_CFG_BPF_INL_MTU) * 64 - 32;
Jakub Kicinski79c12a72017-03-10 10:38:27 -080066 if (max_mtu < nn->dp.netdev->mtu) {
Jakub Kicinski7533fdc2016-09-21 11:44:01 +010067 nn_info(nn, "BPF offload not supported with MTU larger than HW packet split boundary\n");
Jakub Kicinski46c50512017-04-27 21:06:15 -070068 return -EOPNOTSUPP;
Jakub Kicinski7533fdc2016-09-21 11:44:01 +010069 }
70
71 start_off = nn_readw(nn, NFP_NET_CFG_BPF_START);
72 done_off = nn_readw(nn, NFP_NET_CFG_BPF_DONE);
73
Jakub Kicinskiee9133a2017-10-23 11:58:08 -070074 stack_size = nn_readb(nn, NFP_NET_CFG_BPF_STACK_SZ) * 64;
Jakub Kicinski9ce7a952017-11-03 13:56:25 -070075 if (prog->aux->stack_depth > stack_size) {
Jakub Kicinskiee9133a2017-10-23 11:58:08 -070076 nn_info(nn, "stack too large: program %dB > FW stack %dB\n",
Jakub Kicinski9ce7a952017-11-03 13:56:25 -070077 prog->aux->stack_depth, stack_size);
Jakub Kicinskiee9133a2017-10-23 11:58:08 -070078 return -EOPNOTSUPP;
79 }
80
Jakub Kicinski79c12a72017-03-10 10:38:27 -080081 *code = dma_zalloc_coherent(nn->dp.dev, code_sz, dma_addr, GFP_KERNEL);
Jakub Kicinski7533fdc2016-09-21 11:44:01 +010082 if (!*code)
83 return -ENOMEM;
84
Jakub Kicinski9ce7a952017-11-03 13:56:25 -070085 ret = nfp_bpf_jit(prog, *code, start_off, done_off, max_instr, res);
Jakub Kicinski7533fdc2016-09-21 11:44:01 +010086 if (ret)
87 goto out;
88
89 return 0;
90
91out:
Jakub Kicinski79c12a72017-03-10 10:38:27 -080092 dma_free_coherent(nn->dp.dev, code_sz, *code, *dma_addr);
Jakub Kicinski7533fdc2016-09-21 11:44:01 +010093 return ret;
94}
95
96static void
Jakub Kicinskie4a91cd2017-11-03 13:56:26 -070097nfp_net_bpf_load(struct nfp_net *nn, void *code, dma_addr_t dma_addr,
98 unsigned int code_sz, unsigned int n_instr)
Jakub Kicinski7533fdc2016-09-21 11:44:01 +010099{
Jakub Kicinski7533fdc2016-09-21 11:44:01 +0100100 int err;
101
Jakub Kicinski7533fdc2016-09-21 11:44:01 +0100102 nn_writew(nn, NFP_NET_CFG_BPF_SIZE, n_instr);
Jakub Kicinski94508432017-11-03 13:56:23 -0700103 nn_writeq(nn, NFP_NET_CFG_BPF_ADDR, dma_addr);
Jakub Kicinski7533fdc2016-09-21 11:44:01 +0100104
105 /* Load up the JITed code */
106 err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_BPF);
107 if (err)
108 nn_err(nn, "FW command error while loading BPF: %d\n", err);
109
Jakub Kicinskie4a91cd2017-11-03 13:56:26 -0700110 dma_free_coherent(nn->dp.dev, code_sz, code, dma_addr);
111}
112
113static void nfp_net_bpf_start(struct nfp_net *nn)
114{
115 int err;
116
Jakub Kicinski7533fdc2016-09-21 11:44:01 +0100117 /* Enable passing packets through BPF function */
Jakub Kicinski79c12a72017-03-10 10:38:27 -0800118 nn->dp.ctrl |= NFP_NET_CFG_CTRL_BPF;
119 nn_writel(nn, NFP_NET_CFG_CTRL, nn->dp.ctrl);
Jakub Kicinski7533fdc2016-09-21 11:44:01 +0100120 err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_GEN);
121 if (err)
122 nn_err(nn, "FW command error while enabling BPF: %d\n", err);
Jakub Kicinski7533fdc2016-09-21 11:44:01 +0100123}
124
125static int nfp_net_bpf_stop(struct nfp_net *nn)
126{
Jakub Kicinski79c12a72017-03-10 10:38:27 -0800127 if (!(nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF))
Jakub Kicinski7533fdc2016-09-21 11:44:01 +0100128 return 0;
129
Jakub Kicinski79c12a72017-03-10 10:38:27 -0800130 nn->dp.ctrl &= ~NFP_NET_CFG_CTRL_BPF;
Jakub Kicinski79c12a72017-03-10 10:38:27 -0800131 nn_writel(nn, NFP_NET_CFG_CTRL, nn->dp.ctrl);
Jakub Kicinski7533fdc2016-09-21 11:44:01 +0100132
133 return nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_GEN);
134}
135
Jakub Kicinski9ce7a952017-11-03 13:56:25 -0700136int nfp_net_bpf_offload(struct nfp_net *nn, struct bpf_prog *prog,
Jakub Kicinskie4a91cd2017-11-03 13:56:26 -0700137 bool old_prog)
Jakub Kicinski7533fdc2016-09-21 11:44:01 +0100138{
139 struct nfp_bpf_result res;
140 dma_addr_t dma_addr;
141 u16 max_instr;
142 void *code;
143 int err;
144
Jakub Kicinskie4a91cd2017-11-03 13:56:26 -0700145 if (prog && old_prog) {
146 u8 cap;
147
148 cap = nn_readb(nn, NFP_NET_CFG_BPF_CAP);
149 if (!(cap & NFP_NET_BPF_CAP_RELO)) {
150 nn_err(nn, "FW does not support live reload\n");
151 return -EBUSY;
152 }
153 }
Jakub Kicinski9ce7a952017-11-03 13:56:25 -0700154
155 /* Something else is loaded, different program type? */
156 if (!old_prog && nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF)
157 return -EBUSY;
158
Jakub Kicinskie4a91cd2017-11-03 13:56:26 -0700159 if (old_prog && !prog)
160 return nfp_net_bpf_stop(nn);
161
Jakub Kicinski7533fdc2016-09-21 11:44:01 +0100162 max_instr = nn_readw(nn, NFP_NET_CFG_BPF_MAX_LEN);
163
Jakub Kicinskie4a91cd2017-11-03 13:56:26 -0700164 err = nfp_net_bpf_offload_prepare(nn, prog, &res, &code, &dma_addr,
165 max_instr);
166 if (err)
167 return err;
Jakub Kicinski9ce7a952017-11-03 13:56:25 -0700168
Jakub Kicinskie4a91cd2017-11-03 13:56:26 -0700169 nfp_net_bpf_load(nn, code, dma_addr, max_instr * sizeof(u64),
170 res.n_instr);
171 if (!old_prog)
172 nfp_net_bpf_start(nn);
Jakub Kicinski9ce7a952017-11-03 13:56:25 -0700173
174 return 0;
Jakub Kicinski7533fdc2016-09-21 11:44:01 +0100175}