1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2017-2018 Netronome Systems, Inc. */
5 #include <linux/lockdep.h>
6 #include <linux/rcupdate.h>
7 #include <linux/skbuff.h>
8 #include <linux/slab.h>
10 #include "nfpcore/nfp_cpp.h"
11 #include "nfpcore/nfp_nffw.h"
15 #include "nfp_net_repr.h"
18 static const struct nfp_app_type
*apps
[] = {
19 [NFP_APP_CORE_NIC
] = &app_nic
,
20 #ifdef CONFIG_BPF_SYSCALL
21 [NFP_APP_BPF_NIC
] = &app_bpf
,
23 [NFP_APP_BPF_NIC
] = &app_nic
,
25 #ifdef CONFIG_NFP_APP_FLOWER
26 [NFP_APP_FLOWER_NIC
] = &app_flower
,
28 #ifdef CONFIG_NFP_APP_ABM_NIC
29 [NFP_APP_ACTIVE_BUFFER_MGMT_NIC
] = &app_abm
,
33 void nfp_check_rhashtable_empty(void *ptr
, void *arg
)
38 struct nfp_app
*nfp_app_from_netdev(struct net_device
*netdev
)
40 if (nfp_netdev_is_nfp_net(netdev
)) {
41 struct nfp_net
*nn
= netdev_priv(netdev
);
46 if (nfp_netdev_is_nfp_repr(netdev
)) {
47 struct nfp_repr
*repr
= netdev_priv(netdev
);
52 WARN(1, "Unknown netdev type for nfp_app\n");
57 const char *nfp_app_mip_name(struct nfp_app
*app
)
59 if (!app
|| !app
->pf
->mip
)
61 return nfp_mip_name(app
->pf
->mip
);
64 int nfp_app_ndo_init(struct net_device
*netdev
)
66 struct nfp_app
*app
= nfp_app_from_netdev(netdev
);
68 if (!app
|| !app
->type
->ndo_init
)
70 return app
->type
->ndo_init(app
, netdev
);
73 void nfp_app_ndo_uninit(struct net_device
*netdev
)
75 struct nfp_app
*app
= nfp_app_from_netdev(netdev
);
77 if (app
&& app
->type
->ndo_uninit
)
78 app
->type
->ndo_uninit(app
, netdev
);
81 u64
*nfp_app_port_get_stats(struct nfp_port
*port
, u64
*data
)
83 if (!port
|| !port
->app
|| !port
->app
->type
->port_get_stats
)
85 return port
->app
->type
->port_get_stats(port
->app
, port
, data
);
88 int nfp_app_port_get_stats_count(struct nfp_port
*port
)
90 if (!port
|| !port
->app
|| !port
->app
->type
->port_get_stats_count
)
92 return port
->app
->type
->port_get_stats_count(port
->app
, port
);
95 u8
*nfp_app_port_get_stats_strings(struct nfp_port
*port
, u8
*data
)
97 if (!port
|| !port
->app
|| !port
->app
->type
->port_get_stats_strings
)
99 return port
->app
->type
->port_get_stats_strings(port
->app
, port
, data
);
103 nfp_app_ctrl_msg_alloc(struct nfp_app
*app
, unsigned int size
, gfp_t priority
)
107 if (nfp_app_ctrl_has_meta(app
))
110 skb
= alloc_skb(size
, priority
);
114 if (nfp_app_ctrl_has_meta(app
))
121 nfp_reprs_get_locked(struct nfp_app
*app
, enum nfp_repr_type type
)
123 return rcu_dereference_protected(app
->reprs
[type
],
124 lockdep_is_held(&app
->pf
->lock
));
128 nfp_app_reprs_set(struct nfp_app
*app
, enum nfp_repr_type type
,
129 struct nfp_reprs
*reprs
)
131 struct nfp_reprs
*old
;
133 old
= nfp_reprs_get_locked(app
, type
);
135 rcu_assign_pointer(app
->reprs
[type
], reprs
);
142 nfp_app_netdev_feat_change(struct nfp_app
*app
, struct net_device
*netdev
)
147 if (!nfp_netdev_is_nfp_net(netdev
))
149 nn
= netdev_priv(netdev
);
153 for (type
= 0; type
< __NFP_REPR_TYPE_MAX
; type
++) {
154 struct nfp_reprs
*reprs
;
157 reprs
= rtnl_dereference(app
->reprs
[type
]);
161 for (i
= 0; i
< reprs
->num_reprs
; i
++) {
162 struct net_device
*repr
;
164 repr
= rtnl_dereference(reprs
->reprs
[i
]);
168 nfp_repr_transfer_features(repr
, netdev
);
174 nfp_app_netdev_event(struct notifier_block
*nb
, unsigned long event
, void *ptr
)
176 struct net_device
*netdev
;
179 netdev
= netdev_notifier_info_to_dev(ptr
);
180 app
= container_of(nb
, struct nfp_app
, netdev_nb
);
182 /* Handle events common code is interested in */
184 case NETDEV_FEAT_CHANGE
:
185 nfp_app_netdev_feat_change(app
, netdev
);
189 /* Call offload specific handlers */
190 if (app
->type
->netdev_event
)
191 return app
->type
->netdev_event(app
, netdev
, event
, ptr
);
195 int nfp_app_start(struct nfp_app
*app
, struct nfp_net
*ctrl
)
201 if (app
->type
->start
) {
202 err
= app
->type
->start(app
);
207 app
->netdev_nb
.notifier_call
= nfp_app_netdev_event
;
208 err
= register_netdevice_notifier(&app
->netdev_nb
);
216 app
->type
->stop(app
);
220 void nfp_app_stop(struct nfp_app
*app
)
222 unregister_netdevice_notifier(&app
->netdev_nb
);
225 app
->type
->stop(app
);
228 struct nfp_app
*nfp_app_alloc(struct nfp_pf
*pf
, enum nfp_app_id id
)
232 if (id
>= ARRAY_SIZE(apps
) || !apps
[id
]) {
233 nfp_err(pf
->cpp
, "unknown FW app ID 0x%02hhx, driver too old or support for FW not built in\n", id
);
234 return ERR_PTR(-EINVAL
);
237 if (WARN_ON(!apps
[id
]->name
|| !apps
[id
]->vnic_alloc
))
238 return ERR_PTR(-EINVAL
);
239 if (WARN_ON(!apps
[id
]->ctrl_msg_rx
&& apps
[id
]->ctrl_msg_rx_raw
))
240 return ERR_PTR(-EINVAL
);
242 app
= kzalloc(sizeof(*app
), GFP_KERNEL
);
244 return ERR_PTR(-ENOMEM
);
248 app
->pdev
= pf
->pdev
;
249 app
->type
= apps
[id
];
254 void nfp_app_free(struct nfp_app
*app
)