]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/net/ethernet/netronome/nfp/nfp_app.c
c9ccb0f9460487bf7d2b3a4cbff56397cbf2e1b0
[karo-tx-linux.git] / drivers / net / ethernet / netronome / nfp / nfp_app.c
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/skbuff.h>
35 #include <linux/slab.h>
36
37 #include "nfpcore/nfp_cpp.h"
38 #include "nfpcore/nfp_nffw.h"
39 #include "nfp_app.h"
40 #include "nfp_main.h"
41 #include "nfp_net_repr.h"
42
43 static const struct nfp_app_type *apps[] = {
44         &app_nic,
45         &app_bpf,
46 };
47
48 const char *nfp_app_mip_name(struct nfp_app *app)
49 {
50         if (!app || !app->pf->mip)
51                 return "";
52         return nfp_mip_name(app->pf->mip);
53 }
54
55 struct sk_buff *nfp_app_ctrl_msg_alloc(struct nfp_app *app, unsigned int size)
56 {
57         struct sk_buff *skb;
58
59         if (nfp_app_ctrl_has_meta(app))
60                 size += 8;
61
62         skb = alloc_skb(size, GFP_ATOMIC);
63         if (!skb)
64                 return NULL;
65
66         if (nfp_app_ctrl_has_meta(app))
67                 skb_reserve(skb, 8);
68
69         return skb;
70 }
71
72 struct nfp_reprs *
73 nfp_app_reprs_set(struct nfp_app *app, enum nfp_repr_type type,
74                   struct nfp_reprs *reprs)
75 {
76         struct nfp_reprs *old;
77
78         old = rcu_dereference_protected(app->reprs[type],
79                                         lockdep_is_held(&app->pf->lock));
80         if (reprs && old) {
81                 old = ERR_PTR(-EBUSY);
82                 goto exit_unlock;
83         }
84
85         rcu_assign_pointer(app->reprs[type], reprs);
86
87 exit_unlock:
88         return old;
89 }
90
91 struct nfp_app *nfp_app_alloc(struct nfp_pf *pf, enum nfp_app_id id)
92 {
93         struct nfp_app *app;
94         unsigned int i;
95
96         for (i = 0; i < ARRAY_SIZE(apps); i++)
97                 if (apps[i]->id == id)
98                         break;
99         if (i == ARRAY_SIZE(apps)) {
100                 nfp_err(pf->cpp, "failed to find app with ID 0x%02hhx\n", id);
101                 return ERR_PTR(-EINVAL);
102         }
103
104         if (WARN_ON(!apps[i]->name || !apps[i]->vnic_init))
105                 return ERR_PTR(-EINVAL);
106
107         app = kzalloc(sizeof(*app), GFP_KERNEL);
108         if (!app)
109                 return ERR_PTR(-ENOMEM);
110
111         app->pf = pf;
112         app->cpp = pf->cpp;
113         app->pdev = pf->pdev;
114         app->type = apps[i];
115
116         return app;
117 }
118
119 void nfp_app_free(struct nfp_app *app)
120 {
121         kfree(app);
122 }