blob: 3eeee200051eb56259c222520ac1499098f3f3ea [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 Kicinskic1c88ea2017-11-03 13:56:27 -070054int
55nfp_prog_prepare(struct nfp_prog *nfp_prog, const struct bpf_insn *prog,
56 unsigned int cnt)
57{
58 unsigned int i;
59
60 for (i = 0; i < cnt; i++) {
61 struct nfp_insn_meta *meta;
62
63 meta = kzalloc(sizeof(*meta), GFP_KERNEL);
64 if (!meta)
65 return -ENOMEM;
66
67 meta->insn = prog[i];
68 meta->n = i;
69
70 list_add_tail(&meta->l, &nfp_prog->insns);
71 }
72
73 return 0;
74}
75
76void nfp_prog_free(struct nfp_prog *nfp_prog)
77{
78 struct nfp_insn_meta *meta, *tmp;
79
80 list_for_each_entry_safe(meta, tmp, &nfp_prog->insns, l) {
81 list_del(&meta->l);
82 kfree(meta);
83 }
84 kfree(nfp_prog);
85}
86
Jakub Kicinski7533fdc2016-09-21 11:44:01 +010087static int
Jakub Kicinski9ce7a952017-11-03 13:56:25 -070088nfp_net_bpf_offload_prepare(struct nfp_net *nn, struct bpf_prog *prog,
Jakub Kicinski7533fdc2016-09-21 11:44:01 +010089 struct nfp_bpf_result *res,
90 void **code, dma_addr_t *dma_addr, u16 max_instr)
91{
92 unsigned int code_sz = max_instr * sizeof(u64);
Jakub Kicinskiee9133a2017-10-23 11:58:08 -070093 unsigned int stack_size;
Jakub Kicinski7533fdc2016-09-21 11:44:01 +010094 u16 start_off, done_off;
95 unsigned int max_mtu;
96 int ret;
97
Jakub Kicinski7533fdc2016-09-21 11:44:01 +010098 max_mtu = nn_readb(nn, NFP_NET_CFG_BPF_INL_MTU) * 64 - 32;
Jakub Kicinski79c12a72017-03-10 10:38:27 -080099 if (max_mtu < nn->dp.netdev->mtu) {
Jakub Kicinski7533fdc2016-09-21 11:44:01 +0100100 nn_info(nn, "BPF offload not supported with MTU larger than HW packet split boundary\n");
Jakub Kicinski46c50512017-04-27 21:06:15 -0700101 return -EOPNOTSUPP;
Jakub Kicinski7533fdc2016-09-21 11:44:01 +0100102 }
103
104 start_off = nn_readw(nn, NFP_NET_CFG_BPF_START);
105 done_off = nn_readw(nn, NFP_NET_CFG_BPF_DONE);
106
Jakub Kicinskiee9133a2017-10-23 11:58:08 -0700107 stack_size = nn_readb(nn, NFP_NET_CFG_BPF_STACK_SZ) * 64;
Jakub Kicinski9ce7a952017-11-03 13:56:25 -0700108 if (prog->aux->stack_depth > stack_size) {
Jakub Kicinskiee9133a2017-10-23 11:58:08 -0700109 nn_info(nn, "stack too large: program %dB > FW stack %dB\n",
Jakub Kicinski9ce7a952017-11-03 13:56:25 -0700110 prog->aux->stack_depth, stack_size);
Jakub Kicinskiee9133a2017-10-23 11:58:08 -0700111 return -EOPNOTSUPP;
112 }
113
Jakub Kicinski79c12a72017-03-10 10:38:27 -0800114 *code = dma_zalloc_coherent(nn->dp.dev, code_sz, dma_addr, GFP_KERNEL);
Jakub Kicinski7533fdc2016-09-21 11:44:01 +0100115 if (!*code)
116 return -ENOMEM;
117
Jakub Kicinski9ce7a952017-11-03 13:56:25 -0700118 ret = nfp_bpf_jit(prog, *code, start_off, done_off, max_instr, res);
Jakub Kicinski7533fdc2016-09-21 11:44:01 +0100119 if (ret)
120 goto out;
121
122 return 0;
123
124out:
Jakub Kicinski79c12a72017-03-10 10:38:27 -0800125 dma_free_coherent(nn->dp.dev, code_sz, *code, *dma_addr);
Jakub Kicinski7533fdc2016-09-21 11:44:01 +0100126 return ret;
127}
128
129static void
Jakub Kicinskie4a91cd2017-11-03 13:56:26 -0700130nfp_net_bpf_load(struct nfp_net *nn, void *code, dma_addr_t dma_addr,
131 unsigned int code_sz, unsigned int n_instr)
Jakub Kicinski7533fdc2016-09-21 11:44:01 +0100132{
Jakub Kicinski7533fdc2016-09-21 11:44:01 +0100133 int err;
134
Jakub Kicinski7533fdc2016-09-21 11:44:01 +0100135 nn_writew(nn, NFP_NET_CFG_BPF_SIZE, n_instr);
Jakub Kicinski94508432017-11-03 13:56:23 -0700136 nn_writeq(nn, NFP_NET_CFG_BPF_ADDR, dma_addr);
Jakub Kicinski7533fdc2016-09-21 11:44:01 +0100137
138 /* Load up the JITed code */
139 err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_BPF);
140 if (err)
141 nn_err(nn, "FW command error while loading BPF: %d\n", err);
142
Jakub Kicinskie4a91cd2017-11-03 13:56:26 -0700143 dma_free_coherent(nn->dp.dev, code_sz, code, dma_addr);
144}
145
146static void nfp_net_bpf_start(struct nfp_net *nn)
147{
148 int err;
149
Jakub Kicinski7533fdc2016-09-21 11:44:01 +0100150 /* Enable passing packets through BPF function */
Jakub Kicinski79c12a72017-03-10 10:38:27 -0800151 nn->dp.ctrl |= NFP_NET_CFG_CTRL_BPF;
152 nn_writel(nn, NFP_NET_CFG_CTRL, nn->dp.ctrl);
Jakub Kicinski7533fdc2016-09-21 11:44:01 +0100153 err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_GEN);
154 if (err)
155 nn_err(nn, "FW command error while enabling BPF: %d\n", err);
Jakub Kicinski7533fdc2016-09-21 11:44:01 +0100156}
157
158static int nfp_net_bpf_stop(struct nfp_net *nn)
159{
Jakub Kicinski79c12a72017-03-10 10:38:27 -0800160 if (!(nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF))
Jakub Kicinski7533fdc2016-09-21 11:44:01 +0100161 return 0;
162
Jakub Kicinski79c12a72017-03-10 10:38:27 -0800163 nn->dp.ctrl &= ~NFP_NET_CFG_CTRL_BPF;
Jakub Kicinski79c12a72017-03-10 10:38:27 -0800164 nn_writel(nn, NFP_NET_CFG_CTRL, nn->dp.ctrl);
Jakub Kicinski7533fdc2016-09-21 11:44:01 +0100165
166 return nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_GEN);
167}
168
Jakub Kicinski9ce7a952017-11-03 13:56:25 -0700169int nfp_net_bpf_offload(struct nfp_net *nn, struct bpf_prog *prog,
Jakub Kicinskie4a91cd2017-11-03 13:56:26 -0700170 bool old_prog)
Jakub Kicinski7533fdc2016-09-21 11:44:01 +0100171{
172 struct nfp_bpf_result res;
173 dma_addr_t dma_addr;
174 u16 max_instr;
175 void *code;
176 int err;
177
Jakub Kicinskie4a91cd2017-11-03 13:56:26 -0700178 if (prog && old_prog) {
179 u8 cap;
180
181 cap = nn_readb(nn, NFP_NET_CFG_BPF_CAP);
182 if (!(cap & NFP_NET_BPF_CAP_RELO)) {
183 nn_err(nn, "FW does not support live reload\n");
184 return -EBUSY;
185 }
186 }
Jakub Kicinski9ce7a952017-11-03 13:56:25 -0700187
188 /* Something else is loaded, different program type? */
189 if (!old_prog && nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF)
190 return -EBUSY;
191
Jakub Kicinskie4a91cd2017-11-03 13:56:26 -0700192 if (old_prog && !prog)
193 return nfp_net_bpf_stop(nn);
194
Jakub Kicinski7533fdc2016-09-21 11:44:01 +0100195 max_instr = nn_readw(nn, NFP_NET_CFG_BPF_MAX_LEN);
196
Jakub Kicinskie4a91cd2017-11-03 13:56:26 -0700197 err = nfp_net_bpf_offload_prepare(nn, prog, &res, &code, &dma_addr,
198 max_instr);
199 if (err)
200 return err;
Jakub Kicinski9ce7a952017-11-03 13:56:25 -0700201
Jakub Kicinskie4a91cd2017-11-03 13:56:26 -0700202 nfp_net_bpf_load(nn, code, dma_addr, max_instr * sizeof(u64),
203 res.n_instr);
204 if (!old_prog)
205 nfp_net_bpf_start(nn);
Jakub Kicinski9ce7a952017-11-03 13:56:25 -0700206
207 return 0;
Jakub Kicinski7533fdc2016-09-21 11:44:01 +0100208}