|
| 1 | +/* |
| 2 | + * Copyright (C) 2017 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 | +#include <linux/bitops.h> |
| 35 | +#include <linux/bug.h> |
| 36 | +#include <linux/jiffies.h> |
| 37 | +#include <linux/skbuff.h> |
| 38 | +#include <linux/wait.h> |
| 39 | + |
| 40 | +#include "../nfp_app.h" |
| 41 | +#include "../nfp_net.h" |
| 42 | +#include "fw.h" |
| 43 | +#include "main.h" |
| 44 | + |
| 45 | +#define cmsg_warn(bpf, msg...) nn_dp_warn(&(bpf)->app->ctrl->dp, msg) |
| 46 | + |
| 47 | +#define NFP_BPF_TAG_ALLOC_SPAN (U16_MAX / 4) |
| 48 | + |
| 49 | +static bool nfp_bpf_all_tags_busy(struct nfp_app_bpf *bpf) |
| 50 | +{ |
| 51 | + u16 used_tags; |
| 52 | + |
| 53 | + used_tags = bpf->tag_alloc_next - bpf->tag_alloc_last; |
| 54 | + |
| 55 | + return used_tags > NFP_BPF_TAG_ALLOC_SPAN; |
| 56 | +} |
| 57 | + |
| 58 | +static int nfp_bpf_alloc_tag(struct nfp_app_bpf *bpf) |
| 59 | +{ |
| 60 | + /* All FW communication for BPF is request-reply. To make sure we |
| 61 | + * don't reuse the message ID too early after timeout - limit the |
| 62 | + * number of requests in flight. |
| 63 | + */ |
| 64 | + if (nfp_bpf_all_tags_busy(bpf)) { |
| 65 | + cmsg_warn(bpf, "all FW request contexts busy!\n"); |
| 66 | + return -EAGAIN; |
| 67 | + } |
| 68 | + |
| 69 | + WARN_ON(__test_and_set_bit(bpf->tag_alloc_next, bpf->tag_allocator)); |
| 70 | + return bpf->tag_alloc_next++; |
| 71 | +} |
| 72 | + |
| 73 | +static void nfp_bpf_free_tag(struct nfp_app_bpf *bpf, u16 tag) |
| 74 | +{ |
| 75 | + WARN_ON(!__test_and_clear_bit(tag, bpf->tag_allocator)); |
| 76 | + |
| 77 | + while (!test_bit(bpf->tag_alloc_last, bpf->tag_allocator) && |
| 78 | + bpf->tag_alloc_last != bpf->tag_alloc_next) |
| 79 | + bpf->tag_alloc_last++; |
| 80 | +} |
| 81 | + |
| 82 | +static unsigned int nfp_bpf_cmsg_get_tag(struct sk_buff *skb) |
| 83 | +{ |
| 84 | + struct cmsg_hdr *hdr; |
| 85 | + |
| 86 | + hdr = (struct cmsg_hdr *)skb->data; |
| 87 | + |
| 88 | + return be16_to_cpu(hdr->tag); |
| 89 | +} |
| 90 | + |
| 91 | +static struct sk_buff *__nfp_bpf_reply(struct nfp_app_bpf *bpf, u16 tag) |
| 92 | +{ |
| 93 | + unsigned int msg_tag; |
| 94 | + struct sk_buff *skb; |
| 95 | + |
| 96 | + skb_queue_walk(&bpf->cmsg_replies, skb) { |
| 97 | + msg_tag = nfp_bpf_cmsg_get_tag(skb); |
| 98 | + if (msg_tag == tag) { |
| 99 | + nfp_bpf_free_tag(bpf, tag); |
| 100 | + __skb_unlink(skb, &bpf->cmsg_replies); |
| 101 | + return skb; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return NULL; |
| 106 | +} |
| 107 | + |
| 108 | +static struct sk_buff *nfp_bpf_reply(struct nfp_app_bpf *bpf, u16 tag) |
| 109 | +{ |
| 110 | + struct sk_buff *skb; |
| 111 | + |
| 112 | + nfp_ctrl_lock(bpf->app->ctrl); |
| 113 | + skb = __nfp_bpf_reply(bpf, tag); |
| 114 | + nfp_ctrl_unlock(bpf->app->ctrl); |
| 115 | + |
| 116 | + return skb; |
| 117 | +} |
| 118 | + |
| 119 | +static struct sk_buff *nfp_bpf_reply_drop_tag(struct nfp_app_bpf *bpf, u16 tag) |
| 120 | +{ |
| 121 | + struct sk_buff *skb; |
| 122 | + |
| 123 | + nfp_ctrl_lock(bpf->app->ctrl); |
| 124 | + skb = __nfp_bpf_reply(bpf, tag); |
| 125 | + if (!skb) |
| 126 | + nfp_bpf_free_tag(bpf, tag); |
| 127 | + nfp_ctrl_unlock(bpf->app->ctrl); |
| 128 | + |
| 129 | + return skb; |
| 130 | +} |
| 131 | + |
| 132 | +static struct sk_buff * |
| 133 | +nfp_bpf_cmsg_wait_reply(struct nfp_app_bpf *bpf, enum nfp_bpf_cmsg_type type, |
| 134 | + int tag) |
| 135 | +{ |
| 136 | + struct sk_buff *skb; |
| 137 | + int err; |
| 138 | + |
| 139 | + err = wait_event_interruptible_timeout(bpf->cmsg_wq, |
| 140 | + skb = nfp_bpf_reply(bpf, tag), |
| 141 | + msecs_to_jiffies(5000)); |
| 142 | + /* We didn't get a response - try last time and atomically drop |
| 143 | + * the tag even if no response is matched. |
| 144 | + */ |
| 145 | + if (!skb) |
| 146 | + skb = nfp_bpf_reply_drop_tag(bpf, tag); |
| 147 | + if (err < 0) { |
| 148 | + cmsg_warn(bpf, "%s waiting for response to 0x%02x: %d\n", |
| 149 | + err == ERESTARTSYS ? "interrupted" : "error", |
| 150 | + type, err); |
| 151 | + return ERR_PTR(err); |
| 152 | + } |
| 153 | + if (!skb) { |
| 154 | + cmsg_warn(bpf, "timeout waiting for response to 0x%02x\n", |
| 155 | + type); |
| 156 | + return ERR_PTR(-ETIMEDOUT); |
| 157 | + } |
| 158 | + |
| 159 | + return skb; |
| 160 | +} |
| 161 | + |
| 162 | +struct sk_buff * |
| 163 | +nfp_bpf_cmsg_communicate(struct nfp_app_bpf *bpf, struct sk_buff *skb, |
| 164 | + enum nfp_bpf_cmsg_type type, unsigned int reply_size) |
| 165 | +{ |
| 166 | + struct cmsg_hdr *hdr; |
| 167 | + int tag; |
| 168 | + |
| 169 | + nfp_ctrl_lock(bpf->app->ctrl); |
| 170 | + tag = nfp_bpf_alloc_tag(bpf); |
| 171 | + if (tag < 0) { |
| 172 | + nfp_ctrl_unlock(bpf->app->ctrl); |
| 173 | + dev_kfree_skb_any(skb); |
| 174 | + return ERR_PTR(tag); |
| 175 | + } |
| 176 | + |
| 177 | + hdr = (void *)skb->data; |
| 178 | + hdr->ver = CMSG_MAP_ABI_VERSION; |
| 179 | + hdr->type = type; |
| 180 | + hdr->tag = cpu_to_be16(tag); |
| 181 | + |
| 182 | + __nfp_app_ctrl_tx(bpf->app, skb); |
| 183 | + |
| 184 | + nfp_ctrl_unlock(bpf->app->ctrl); |
| 185 | + |
| 186 | + skb = nfp_bpf_cmsg_wait_reply(bpf, type, tag); |
| 187 | + if (IS_ERR(skb)) |
| 188 | + return skb; |
| 189 | + |
| 190 | + hdr = (struct cmsg_hdr *)skb->data; |
| 191 | + /* 0 reply_size means caller will do the validation */ |
| 192 | + if (reply_size && skb->len != reply_size) { |
| 193 | + cmsg_warn(bpf, "cmsg drop - wrong size %d != %d!\n", |
| 194 | + skb->len, reply_size); |
| 195 | + goto err_free; |
| 196 | + } |
| 197 | + if (hdr->type != __CMSG_REPLY(type)) { |
| 198 | + cmsg_warn(bpf, "cmsg drop - wrong type 0x%02x != 0x%02lx!\n", |
| 199 | + hdr->type, __CMSG_REPLY(type)); |
| 200 | + goto err_free; |
| 201 | + } |
| 202 | + |
| 203 | + return skb; |
| 204 | +err_free: |
| 205 | + dev_kfree_skb_any(skb); |
| 206 | + return ERR_PTR(-EIO); |
| 207 | +} |
| 208 | + |
| 209 | +void nfp_bpf_ctrl_msg_rx(struct nfp_app *app, struct sk_buff *skb) |
| 210 | +{ |
| 211 | + struct nfp_app_bpf *bpf = app->priv; |
| 212 | + unsigned int tag; |
| 213 | + |
| 214 | + if (unlikely(skb->len < sizeof(struct cmsg_reply_map_simple))) { |
| 215 | + cmsg_warn(bpf, "cmsg drop - too short %d!\n", skb->len); |
| 216 | + goto err_free; |
| 217 | + } |
| 218 | + |
| 219 | + nfp_ctrl_lock(bpf->app->ctrl); |
| 220 | + |
| 221 | + tag = nfp_bpf_cmsg_get_tag(skb); |
| 222 | + if (unlikely(!test_bit(tag, bpf->tag_allocator))) { |
| 223 | + cmsg_warn(bpf, "cmsg drop - no one is waiting for tag %u!\n", |
| 224 | + tag); |
| 225 | + goto err_unlock; |
| 226 | + } |
| 227 | + |
| 228 | + __skb_queue_tail(&bpf->cmsg_replies, skb); |
| 229 | + wake_up_interruptible_all(&bpf->cmsg_wq); |
| 230 | + |
| 231 | + nfp_ctrl_unlock(bpf->app->ctrl); |
| 232 | + |
| 233 | + return; |
| 234 | +err_unlock: |
| 235 | + nfp_ctrl_unlock(bpf->app->ctrl); |
| 236 | +err_free: |
| 237 | + dev_kfree_skb_any(skb); |
| 238 | +} |
0 commit comments