Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
0002 /* Copyright (C) 2017-2018 Netronome Systems, Inc. */
0003 
0004 #include <linux/bug.h>
0005 #include <linux/lockdep.h>
0006 #include <linux/rcupdate.h>
0007 #include <linux/skbuff.h>
0008 #include <linux/slab.h>
0009 
0010 #include "nfpcore/nfp_cpp.h"
0011 #include "nfpcore/nfp_nffw.h"
0012 #include "nfp_app.h"
0013 #include "nfp_main.h"
0014 #include "nfp_net.h"
0015 #include "nfp_net_repr.h"
0016 #include "nfp_port.h"
0017 
0018 static const struct nfp_app_type *apps[] = {
0019     [NFP_APP_CORE_NIC]  = &app_nic,
0020 #ifdef CONFIG_BPF_SYSCALL
0021     [NFP_APP_BPF_NIC]   = &app_bpf,
0022 #else
0023     [NFP_APP_BPF_NIC]   = &app_nic,
0024 #endif
0025 #ifdef CONFIG_NFP_APP_FLOWER
0026     [NFP_APP_FLOWER_NIC]    = &app_flower,
0027 #endif
0028 #ifdef CONFIG_NFP_APP_ABM_NIC
0029     [NFP_APP_ACTIVE_BUFFER_MGMT_NIC] = &app_abm,
0030 #endif
0031 };
0032 
0033 void nfp_check_rhashtable_empty(void *ptr, void *arg)
0034 {
0035     WARN_ON_ONCE(1);
0036 }
0037 
0038 struct nfp_app *nfp_app_from_netdev(struct net_device *netdev)
0039 {
0040     if (nfp_netdev_is_nfp_net(netdev)) {
0041         struct nfp_net *nn = netdev_priv(netdev);
0042 
0043         return nn->app;
0044     }
0045 
0046     if (nfp_netdev_is_nfp_repr(netdev)) {
0047         struct nfp_repr *repr = netdev_priv(netdev);
0048 
0049         return repr->app;
0050     }
0051 
0052     WARN(1, "Unknown netdev type for nfp_app\n");
0053 
0054     return NULL;
0055 }
0056 
0057 const char *nfp_app_mip_name(struct nfp_app *app)
0058 {
0059     if (!app || !app->pf->mip)
0060         return "";
0061     return nfp_mip_name(app->pf->mip);
0062 }
0063 
0064 int nfp_app_ndo_init(struct net_device *netdev)
0065 {
0066     struct nfp_app *app = nfp_app_from_netdev(netdev);
0067 
0068     if (!app || !app->type->ndo_init)
0069         return 0;
0070     return app->type->ndo_init(app, netdev);
0071 }
0072 
0073 void nfp_app_ndo_uninit(struct net_device *netdev)
0074 {
0075     struct nfp_app *app = nfp_app_from_netdev(netdev);
0076 
0077     if (app && app->type->ndo_uninit)
0078         app->type->ndo_uninit(app, netdev);
0079 }
0080 
0081 u64 *nfp_app_port_get_stats(struct nfp_port *port, u64 *data)
0082 {
0083     if (!port || !port->app || !port->app->type->port_get_stats)
0084         return data;
0085     return port->app->type->port_get_stats(port->app, port, data);
0086 }
0087 
0088 int nfp_app_port_get_stats_count(struct nfp_port *port)
0089 {
0090     if (!port || !port->app || !port->app->type->port_get_stats_count)
0091         return 0;
0092     return port->app->type->port_get_stats_count(port->app, port);
0093 }
0094 
0095 u8 *nfp_app_port_get_stats_strings(struct nfp_port *port, u8 *data)
0096 {
0097     if (!port || !port->app || !port->app->type->port_get_stats_strings)
0098         return data;
0099     return port->app->type->port_get_stats_strings(port->app, port, data);
0100 }
0101 
0102 struct sk_buff *
0103 nfp_app_ctrl_msg_alloc(struct nfp_app *app, unsigned int size, gfp_t priority)
0104 {
0105     struct sk_buff *skb;
0106 
0107     if (nfp_app_ctrl_has_meta(app))
0108         size += 8;
0109 
0110     skb = alloc_skb(size, priority);
0111     if (!skb)
0112         return NULL;
0113 
0114     if (nfp_app_ctrl_has_meta(app))
0115         skb_reserve(skb, 8);
0116 
0117     return skb;
0118 }
0119 
0120 struct nfp_reprs *
0121 nfp_reprs_get_locked(struct nfp_app *app, enum nfp_repr_type type)
0122 {
0123     return rcu_dereference_protected(app->reprs[type],
0124                      nfp_app_is_locked(app));
0125 }
0126 
0127 struct nfp_reprs *
0128 nfp_app_reprs_set(struct nfp_app *app, enum nfp_repr_type type,
0129           struct nfp_reprs *reprs)
0130 {
0131     struct nfp_reprs *old;
0132 
0133     old = nfp_reprs_get_locked(app, type);
0134     rtnl_lock();
0135     rcu_assign_pointer(app->reprs[type], reprs);
0136     rtnl_unlock();
0137 
0138     return old;
0139 }
0140 
0141 static void
0142 nfp_app_netdev_feat_change(struct nfp_app *app, struct net_device *netdev)
0143 {
0144     struct nfp_net *nn;
0145     unsigned int type;
0146 
0147     if (!nfp_netdev_is_nfp_net(netdev))
0148         return;
0149     nn = netdev_priv(netdev);
0150     if (nn->app != app)
0151         return;
0152 
0153     for (type = 0; type < __NFP_REPR_TYPE_MAX; type++) {
0154         struct nfp_reprs *reprs;
0155         unsigned int i;
0156 
0157         reprs = rtnl_dereference(app->reprs[type]);
0158         if (!reprs)
0159             continue;
0160 
0161         for (i = 0; i < reprs->num_reprs; i++) {
0162             struct net_device *repr;
0163 
0164             repr = rtnl_dereference(reprs->reprs[i]);
0165             if (!repr)
0166                 continue;
0167 
0168             nfp_repr_transfer_features(repr, netdev);
0169         }
0170     }
0171 }
0172 
0173 static int
0174 nfp_app_netdev_event(struct notifier_block *nb, unsigned long event, void *ptr)
0175 {
0176     struct net_device *netdev;
0177     struct nfp_app *app;
0178 
0179     netdev = netdev_notifier_info_to_dev(ptr);
0180     app = container_of(nb, struct nfp_app, netdev_nb);
0181 
0182     /* Handle events common code is interested in */
0183     switch (event) {
0184     case NETDEV_FEAT_CHANGE:
0185         nfp_app_netdev_feat_change(app, netdev);
0186         break;
0187     }
0188 
0189     /* Call offload specific handlers */
0190     if (app->type->netdev_event)
0191         return app->type->netdev_event(app, netdev, event, ptr);
0192     return NOTIFY_DONE;
0193 }
0194 
0195 int nfp_app_start(struct nfp_app *app, struct nfp_net *ctrl)
0196 {
0197     int err;
0198 
0199     app->ctrl = ctrl;
0200 
0201     if (app->type->start) {
0202         err = app->type->start(app);
0203         if (err)
0204             return err;
0205     }
0206 
0207     app->netdev_nb.notifier_call = nfp_app_netdev_event;
0208     err = register_netdevice_notifier(&app->netdev_nb);
0209     if (err)
0210         goto err_app_stop;
0211 
0212     return 0;
0213 
0214 err_app_stop:
0215     if (app->type->stop)
0216         app->type->stop(app);
0217     return err;
0218 }
0219 
0220 void nfp_app_stop(struct nfp_app *app)
0221 {
0222     unregister_netdevice_notifier(&app->netdev_nb);
0223 
0224     if (app->type->stop)
0225         app->type->stop(app);
0226 }
0227 
0228 struct nfp_app *nfp_app_alloc(struct nfp_pf *pf, enum nfp_app_id id)
0229 {
0230     struct nfp_app *app;
0231 
0232     if (id >= ARRAY_SIZE(apps) || !apps[id]) {
0233         nfp_err(pf->cpp, "unknown FW app ID 0x%02x, driver too old or support for FW not built in\n", id);
0234         return ERR_PTR(-EINVAL);
0235     }
0236 
0237     if (WARN_ON(!apps[id]->name || !apps[id]->vnic_alloc))
0238         return ERR_PTR(-EINVAL);
0239     if (WARN_ON(!apps[id]->ctrl_msg_rx && apps[id]->ctrl_msg_rx_raw))
0240         return ERR_PTR(-EINVAL);
0241 
0242     app = kzalloc(sizeof(*app), GFP_KERNEL);
0243     if (!app)
0244         return ERR_PTR(-ENOMEM);
0245 
0246     app->pf = pf;
0247     app->cpp = pf->cpp;
0248     app->pdev = pf->pdev;
0249     app->type = apps[id];
0250 
0251     return app;
0252 }
0253 
0254 void nfp_app_free(struct nfp_app *app)
0255 {
0256     kfree(app);
0257 }