Jakub Kicinski | 7533fdc | 2016-09-21 11:44:01 +0100 | [diff] [blame] | 1 | /* |
| 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 Kicinski | d9ae7f2 | 2017-05-31 08:06:48 -0700 | [diff] [blame] | 50 | #include "main.h" |
| 51 | #include "../nfp_net_ctrl.h" |
| 52 | #include "../nfp_net.h" |
Jakub Kicinski | 7533fdc | 2016-09-21 11:44:01 +0100 | [diff] [blame] | 53 | |
Jakub Kicinski | 7533fdc | 2016-09-21 11:44:01 +0100 | [diff] [blame] | 54 | static int |
Jakub Kicinski | 9ce7a95 | 2017-11-03 13:56:25 -0700 | [diff] [blame] | 55 | nfp_net_bpf_offload_prepare(struct nfp_net *nn, struct bpf_prog *prog, |
Jakub Kicinski | 7533fdc | 2016-09-21 11:44:01 +0100 | [diff] [blame] | 56 | 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 Kicinski | ee9133a | 2017-10-23 11:58:08 -0700 | [diff] [blame] | 60 | unsigned int stack_size; |
Jakub Kicinski | 7533fdc | 2016-09-21 11:44:01 +0100 | [diff] [blame] | 61 | u16 start_off, done_off; |
| 62 | unsigned int max_mtu; |
| 63 | int ret; |
| 64 | |
Jakub Kicinski | 7533fdc | 2016-09-21 11:44:01 +0100 | [diff] [blame] | 65 | max_mtu = nn_readb(nn, NFP_NET_CFG_BPF_INL_MTU) * 64 - 32; |
Jakub Kicinski | 79c12a7 | 2017-03-10 10:38:27 -0800 | [diff] [blame] | 66 | if (max_mtu < nn->dp.netdev->mtu) { |
Jakub Kicinski | 7533fdc | 2016-09-21 11:44:01 +0100 | [diff] [blame] | 67 | nn_info(nn, "BPF offload not supported with MTU larger than HW packet split boundary\n"); |
Jakub Kicinski | 46c5051 | 2017-04-27 21:06:15 -0700 | [diff] [blame] | 68 | return -EOPNOTSUPP; |
Jakub Kicinski | 7533fdc | 2016-09-21 11:44:01 +0100 | [diff] [blame] | 69 | } |
| 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 Kicinski | ee9133a | 2017-10-23 11:58:08 -0700 | [diff] [blame] | 74 | stack_size = nn_readb(nn, NFP_NET_CFG_BPF_STACK_SZ) * 64; |
Jakub Kicinski | 9ce7a95 | 2017-11-03 13:56:25 -0700 | [diff] [blame] | 75 | if (prog->aux->stack_depth > stack_size) { |
Jakub Kicinski | ee9133a | 2017-10-23 11:58:08 -0700 | [diff] [blame] | 76 | nn_info(nn, "stack too large: program %dB > FW stack %dB\n", |
Jakub Kicinski | 9ce7a95 | 2017-11-03 13:56:25 -0700 | [diff] [blame] | 77 | prog->aux->stack_depth, stack_size); |
Jakub Kicinski | ee9133a | 2017-10-23 11:58:08 -0700 | [diff] [blame] | 78 | return -EOPNOTSUPP; |
| 79 | } |
| 80 | |
Jakub Kicinski | 79c12a7 | 2017-03-10 10:38:27 -0800 | [diff] [blame] | 81 | *code = dma_zalloc_coherent(nn->dp.dev, code_sz, dma_addr, GFP_KERNEL); |
Jakub Kicinski | 7533fdc | 2016-09-21 11:44:01 +0100 | [diff] [blame] | 82 | if (!*code) |
| 83 | return -ENOMEM; |
| 84 | |
Jakub Kicinski | 9ce7a95 | 2017-11-03 13:56:25 -0700 | [diff] [blame] | 85 | ret = nfp_bpf_jit(prog, *code, start_off, done_off, max_instr, res); |
Jakub Kicinski | 7533fdc | 2016-09-21 11:44:01 +0100 | [diff] [blame] | 86 | if (ret) |
| 87 | goto out; |
| 88 | |
| 89 | return 0; |
| 90 | |
| 91 | out: |
Jakub Kicinski | 79c12a7 | 2017-03-10 10:38:27 -0800 | [diff] [blame] | 92 | dma_free_coherent(nn->dp.dev, code_sz, *code, *dma_addr); |
Jakub Kicinski | 7533fdc | 2016-09-21 11:44:01 +0100 | [diff] [blame] | 93 | return ret; |
| 94 | } |
| 95 | |
| 96 | static void |
Jakub Kicinski | e4a91cd | 2017-11-03 13:56:26 -0700 | [diff] [blame^] | 97 | nfp_net_bpf_load(struct nfp_net *nn, void *code, dma_addr_t dma_addr, |
| 98 | unsigned int code_sz, unsigned int n_instr) |
Jakub Kicinski | 7533fdc | 2016-09-21 11:44:01 +0100 | [diff] [blame] | 99 | { |
Jakub Kicinski | 7533fdc | 2016-09-21 11:44:01 +0100 | [diff] [blame] | 100 | int err; |
| 101 | |
Jakub Kicinski | 7533fdc | 2016-09-21 11:44:01 +0100 | [diff] [blame] | 102 | nn_writew(nn, NFP_NET_CFG_BPF_SIZE, n_instr); |
Jakub Kicinski | 9450843 | 2017-11-03 13:56:23 -0700 | [diff] [blame] | 103 | nn_writeq(nn, NFP_NET_CFG_BPF_ADDR, dma_addr); |
Jakub Kicinski | 7533fdc | 2016-09-21 11:44:01 +0100 | [diff] [blame] | 104 | |
| 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 Kicinski | e4a91cd | 2017-11-03 13:56:26 -0700 | [diff] [blame^] | 110 | dma_free_coherent(nn->dp.dev, code_sz, code, dma_addr); |
| 111 | } |
| 112 | |
| 113 | static void nfp_net_bpf_start(struct nfp_net *nn) |
| 114 | { |
| 115 | int err; |
| 116 | |
Jakub Kicinski | 7533fdc | 2016-09-21 11:44:01 +0100 | [diff] [blame] | 117 | /* Enable passing packets through BPF function */ |
Jakub Kicinski | 79c12a7 | 2017-03-10 10:38:27 -0800 | [diff] [blame] | 118 | nn->dp.ctrl |= NFP_NET_CFG_CTRL_BPF; |
| 119 | nn_writel(nn, NFP_NET_CFG_CTRL, nn->dp.ctrl); |
Jakub Kicinski | 7533fdc | 2016-09-21 11:44:01 +0100 | [diff] [blame] | 120 | 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 Kicinski | 7533fdc | 2016-09-21 11:44:01 +0100 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | static int nfp_net_bpf_stop(struct nfp_net *nn) |
| 126 | { |
Jakub Kicinski | 79c12a7 | 2017-03-10 10:38:27 -0800 | [diff] [blame] | 127 | if (!(nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF)) |
Jakub Kicinski | 7533fdc | 2016-09-21 11:44:01 +0100 | [diff] [blame] | 128 | return 0; |
| 129 | |
Jakub Kicinski | 79c12a7 | 2017-03-10 10:38:27 -0800 | [diff] [blame] | 130 | nn->dp.ctrl &= ~NFP_NET_CFG_CTRL_BPF; |
Jakub Kicinski | 79c12a7 | 2017-03-10 10:38:27 -0800 | [diff] [blame] | 131 | nn_writel(nn, NFP_NET_CFG_CTRL, nn->dp.ctrl); |
Jakub Kicinski | 7533fdc | 2016-09-21 11:44:01 +0100 | [diff] [blame] | 132 | |
| 133 | return nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_GEN); |
| 134 | } |
| 135 | |
Jakub Kicinski | 9ce7a95 | 2017-11-03 13:56:25 -0700 | [diff] [blame] | 136 | int nfp_net_bpf_offload(struct nfp_net *nn, struct bpf_prog *prog, |
Jakub Kicinski | e4a91cd | 2017-11-03 13:56:26 -0700 | [diff] [blame^] | 137 | bool old_prog) |
Jakub Kicinski | 7533fdc | 2016-09-21 11:44:01 +0100 | [diff] [blame] | 138 | { |
| 139 | struct nfp_bpf_result res; |
| 140 | dma_addr_t dma_addr; |
| 141 | u16 max_instr; |
| 142 | void *code; |
| 143 | int err; |
| 144 | |
Jakub Kicinski | e4a91cd | 2017-11-03 13:56:26 -0700 | [diff] [blame^] | 145 | 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 Kicinski | 9ce7a95 | 2017-11-03 13:56:25 -0700 | [diff] [blame] | 154 | |
| 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 Kicinski | e4a91cd | 2017-11-03 13:56:26 -0700 | [diff] [blame^] | 159 | if (old_prog && !prog) |
| 160 | return nfp_net_bpf_stop(nn); |
| 161 | |
Jakub Kicinski | 7533fdc | 2016-09-21 11:44:01 +0100 | [diff] [blame] | 162 | max_instr = nn_readw(nn, NFP_NET_CFG_BPF_MAX_LEN); |
| 163 | |
Jakub Kicinski | e4a91cd | 2017-11-03 13:56:26 -0700 | [diff] [blame^] | 164 | err = nfp_net_bpf_offload_prepare(nn, prog, &res, &code, &dma_addr, |
| 165 | max_instr); |
| 166 | if (err) |
| 167 | return err; |
Jakub Kicinski | 9ce7a95 | 2017-11-03 13:56:25 -0700 | [diff] [blame] | 168 | |
Jakub Kicinski | e4a91cd | 2017-11-03 13:56:26 -0700 | [diff] [blame^] | 169 | 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 Kicinski | 9ce7a95 | 2017-11-03 13:56:25 -0700 | [diff] [blame] | 173 | |
| 174 | return 0; |
Jakub Kicinski | 7533fdc | 2016-09-21 11:44:01 +0100 | [diff] [blame] | 175 | } |