blob: 54d42a7f0d75fa40e398c632e9b6c4559ec5899b [file] [log] [blame]
Simon Horman10253512017-06-23 22:12:08 +02001/*
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/etherdevice.h>
35#include <linux/pci.h>
36#include <linux/skbuff.h>
37#include <net/devlink.h>
38#include <net/dst_metadata.h>
39
40#include "../nfpcore/nfp_cpp.h"
41#include "../nfpcore/nfp_nsp.h"
42#include "../nfp_app.h"
43#include "../nfp_main.h"
44#include "../nfp_net.h"
45#include "../nfp_net_repr.h"
46#include "../nfp_port.h"
47#include "./cmsg.h"
48
49/**
50 * struct nfp_flower_priv - Flower APP per-vNIC priv data
51 * @nn: Pointer to vNIC
52 */
53struct nfp_flower_priv {
54 struct nfp_net *nn;
55};
56
57static const char *nfp_flower_extra_cap(struct nfp_app *app, struct nfp_net *nn)
58{
59 return "FLOWER";
60}
61
62static enum devlink_eswitch_mode eswitch_mode_get(struct nfp_app *app)
63{
64 return DEVLINK_ESWITCH_MODE_SWITCHDEV;
65}
66
67static enum nfp_repr_type
68nfp_flower_repr_get_type_and_port(struct nfp_app *app, u32 port_id, u8 *port)
69{
70 switch (FIELD_GET(NFP_FLOWER_CMSG_PORT_TYPE, port_id)) {
71 case NFP_FLOWER_CMSG_PORT_TYPE_PHYS_PORT:
72 *port = FIELD_GET(NFP_FLOWER_CMSG_PORT_PHYS_PORT_NUM,
73 port_id);
74 return NFP_REPR_TYPE_PHYS_PORT;
75
76 case NFP_FLOWER_CMSG_PORT_TYPE_PCIE_PORT:
77 *port = FIELD_GET(NFP_FLOWER_CMSG_PORT_VNIC, port_id);
78 if (FIELD_GET(NFP_FLOWER_CMSG_PORT_VNIC_TYPE, port_id) ==
79 NFP_FLOWER_CMSG_PORT_VNIC_TYPE_PF)
80 return NFP_REPR_TYPE_PF;
81 else
82 return NFP_REPR_TYPE_VF;
83 }
84
85 return NFP_FLOWER_CMSG_PORT_TYPE_UNSPEC;
86}
87
88static struct net_device *
89nfp_flower_repr_get(struct nfp_app *app, u32 port_id)
90{
91 enum nfp_repr_type repr_type;
92 struct nfp_reprs *reprs;
93 u8 port = 0;
94
95 repr_type = nfp_flower_repr_get_type_and_port(app, port_id, &port);
96
97 reprs = rcu_dereference(app->reprs[repr_type]);
98 if (!reprs)
99 return NULL;
100
101 if (port >= reprs->num_reprs)
102 return NULL;
103
104 return reprs->reprs[port];
105}
106
107static void
108nfp_flower_repr_netdev_get_stats64(struct net_device *netdev,
109 struct rtnl_link_stats64 *stats)
110{
111 struct nfp_repr *repr = netdev_priv(netdev);
112 enum nfp_repr_type type;
113 u32 port_id;
114 u8 port = 0;
115
116 port_id = repr->dst->u.port_info.port_id;
117 type = nfp_flower_repr_get_type_and_port(repr->app, port_id, &port);
118 nfp_repr_get_stats64(repr->app, type, port, stats);
119}
120
121static int nfp_flower_repr_netdev_open(struct net_device *netdev)
122{
123 int err;
124
125 err = nfp_flower_cmsg_portmod(netdev, true);
126 if (err)
127 return err;
128
129 netif_carrier_on(netdev);
130 netif_tx_wake_all_queues(netdev);
131
132 return 0;
133}
134
135static int nfp_flower_repr_netdev_stop(struct net_device *netdev)
136{
137 netif_carrier_off(netdev);
138 netif_tx_disable(netdev);
139
140 return nfp_flower_cmsg_portmod(netdev, false);
141}
142
143static const struct net_device_ops nfp_flower_repr_netdev_ops = {
144 .ndo_open = nfp_flower_repr_netdev_open,
145 .ndo_stop = nfp_flower_repr_netdev_stop,
146 .ndo_start_xmit = nfp_repr_xmit,
147 .ndo_get_stats64 = nfp_flower_repr_netdev_get_stats64,
148 .ndo_has_offload_stats = nfp_repr_has_offload_stats,
149 .ndo_get_offload_stats = nfp_repr_get_offload_stats,
150};
151
Simon Horman24a021e2017-06-23 22:12:09 +0200152static void nfp_flower_sriov_disable(struct nfp_app *app)
Simon Horman10253512017-06-23 22:12:08 +0200153{
Simon Horman24a021e2017-06-23 22:12:09 +0200154 nfp_reprs_clean_and_free_by_type(app, NFP_REPR_TYPE_VF);
Simon Horman10253512017-06-23 22:12:08 +0200155}
156
Simon Horman24a021e2017-06-23 22:12:09 +0200157static int
158nfp_flower_spawn_vnic_reprs(struct nfp_app *app,
159 enum nfp_flower_cmsg_port_vnic_type vnic_type,
160 enum nfp_repr_type repr_type, unsigned int cnt)
161{
162 u8 nfp_pcie = nfp_cppcore_pcie_unit(app->pf->cpp);
163 struct nfp_flower_priv *priv = app->priv;
164 struct nfp_reprs *reprs, *old_reprs;
165 const u8 queue = 0;
166 int i, err;
167
168 reprs = nfp_reprs_alloc(cnt);
169 if (!reprs)
170 return -ENOMEM;
171
172 for (i = 0; i < cnt; i++) {
173 u32 port_id;
174
175 reprs->reprs[i] = nfp_repr_alloc(app);
176 if (!reprs->reprs[i]) {
177 err = -ENOMEM;
178 goto err_reprs_clean;
179 }
180
181 eth_hw_addr_random(reprs->reprs[i]);
182
183 port_id = nfp_flower_cmsg_pcie_port(nfp_pcie, vnic_type,
184 i, queue);
185 err = nfp_repr_init(app, reprs->reprs[i],
186 &nfp_flower_repr_netdev_ops,
187 port_id, NULL, priv->nn->dp.netdev);
188 if (err)
189 goto err_reprs_clean;
190
191 nfp_info(app->cpp, "%s%d Representor(%s) created\n",
192 repr_type == NFP_REPR_TYPE_PF ? "PF" : "VF", i,
193 reprs->reprs[i]->name);
194 }
195
196 old_reprs = nfp_app_reprs_set(app, repr_type, reprs);
197 if (IS_ERR(old_reprs)) {
198 err = PTR_ERR(old_reprs);
199 goto err_reprs_clean;
200 }
201
202 return 0;
203err_reprs_clean:
204 nfp_reprs_clean_and_free(reprs);
205 return err;
206}
207
208static int nfp_flower_sriov_enable(struct nfp_app *app, int num_vfs)
209{
210 return nfp_flower_spawn_vnic_reprs(app,
211 NFP_FLOWER_CMSG_PORT_VNIC_TYPE_VF,
212 NFP_REPR_TYPE_VF, num_vfs);
213}
214
215static void nfp_flower_stop(struct nfp_app *app)
216{
217 nfp_reprs_clean_and_free_by_type(app, NFP_REPR_TYPE_PF);
218 nfp_reprs_clean_and_free_by_type(app, NFP_REPR_TYPE_PHYS_PORT);
219
220}
221
222static int
223nfp_flower_spawn_phy_reprs(struct nfp_app *app, struct nfp_flower_priv *priv)
Simon Horman10253512017-06-23 22:12:08 +0200224{
225 struct nfp_eth_table *eth_tbl = app->pf->eth_tbl;
Simon Horman10253512017-06-23 22:12:08 +0200226 struct nfp_reprs *reprs, *old_reprs;
227 unsigned int i;
228 int err;
229
230 reprs = nfp_reprs_alloc(eth_tbl->max_index + 1);
231 if (!reprs)
232 return -ENOMEM;
233
234 for (i = 0; i < eth_tbl->count; i++) {
235 int phys_port = eth_tbl->ports[i].index;
236 struct nfp_port *port;
237 u32 cmsg_port_id;
238
239 reprs->reprs[phys_port] = nfp_repr_alloc(app);
240 if (!reprs->reprs[phys_port]) {
241 err = -ENOMEM;
242 goto err_reprs_clean;
243 }
244
245 port = nfp_port_alloc(app, NFP_PORT_PHYS_PORT,
246 reprs->reprs[phys_port]);
247 if (IS_ERR(port)) {
248 err = PTR_ERR(port);
249 goto err_reprs_clean;
250 }
251 err = nfp_port_init_phy_port(app->pf, app, port, i);
252 if (err) {
253 nfp_port_free(port);
254 goto err_reprs_clean;
255 }
256
257 SET_NETDEV_DEV(reprs->reprs[phys_port], &priv->nn->pdev->dev);
258 nfp_net_get_mac_addr(app->pf, port,
259 eth_tbl->ports[i].eth_index);
260
261 cmsg_port_id = nfp_flower_cmsg_phys_port(phys_port);
262 err = nfp_repr_init(app, reprs->reprs[phys_port],
263 &nfp_flower_repr_netdev_ops,
264 cmsg_port_id, port, priv->nn->dp.netdev);
265 if (err) {
266 nfp_port_free(port);
267 goto err_reprs_clean;
268 }
269
270 nfp_info(app->cpp, "Phys Port %d Representor(%s) created\n",
271 phys_port, reprs->reprs[phys_port]->name);
272 }
273
274 old_reprs = nfp_app_reprs_set(app, NFP_REPR_TYPE_PHYS_PORT, reprs);
275 if (IS_ERR(old_reprs)) {
276 err = PTR_ERR(old_reprs);
277 goto err_reprs_clean;
278 }
279
280 return 0;
281err_reprs_clean:
282 nfp_reprs_clean_and_free(reprs);
283 return err;
284}
285
Simon Horman24a021e2017-06-23 22:12:09 +0200286static int nfp_flower_start(struct nfp_app *app)
287{
288 int err;
289
290 err = nfp_flower_spawn_phy_reprs(app, app->priv);
291 if (err)
292 return err;
293
294 return nfp_flower_spawn_vnic_reprs(app,
295 NFP_FLOWER_CMSG_PORT_VNIC_TYPE_PF,
296 NFP_REPR_TYPE_PF, 1);
297}
298
Simon Horman10253512017-06-23 22:12:08 +0200299static int nfp_flower_vnic_init(struct nfp_app *app, struct nfp_net *nn,
300 unsigned int id)
301{
Jakub Kicinski9ce6bbb2017-06-27 00:50:19 -0700302 struct nfp_flower_priv *priv = app->priv;
Simon Horman10253512017-06-23 22:12:08 +0200303
304 if (id > 0) {
305 nfp_warn(app->cpp, "FlowerNIC doesn't support more than one data vNIC\n");
306 goto err_invalid_port;
307 }
308
Simon Horman10253512017-06-23 22:12:08 +0200309 priv->nn = nn;
310
311 eth_hw_addr_random(nn->dp.netdev);
312 netif_keep_dst(nn->dp.netdev);
313
314 return 0;
315
316err_invalid_port:
317 nn->port = nfp_port_alloc(app, NFP_PORT_INVALID, nn->dp.netdev);
318 return PTR_ERR_OR_ZERO(nn->port);
319}
320
321static int nfp_flower_init(struct nfp_app *app)
322{
323 const struct nfp_pf *pf = app->pf;
324
325 if (!pf->eth_tbl) {
326 nfp_warn(app->cpp, "FlowerNIC requires eth table\n");
327 return -EINVAL;
328 }
329
330 if (!pf->mac_stats_bar) {
331 nfp_warn(app->cpp, "FlowerNIC requires mac_stats BAR\n");
332 return -EINVAL;
333 }
334
335 if (!pf->vf_cfg_bar) {
336 nfp_warn(app->cpp, "FlowerNIC requires vf_cfg BAR\n");
337 return -EINVAL;
338 }
339
Jakub Kicinski9ce6bbb2017-06-27 00:50:19 -0700340 app->priv = kzalloc(sizeof(struct nfp_flower_priv), GFP_KERNEL);
341 if (!app->priv)
342 return -ENOMEM;
343
Simon Horman10253512017-06-23 22:12:08 +0200344 return 0;
345}
346
Jakub Kicinski9ce6bbb2017-06-27 00:50:19 -0700347static void nfp_flower_clean(struct nfp_app *app)
348{
349 kfree(app->priv);
350 app->priv = NULL;
351}
352
Simon Horman10253512017-06-23 22:12:08 +0200353const struct nfp_app_type app_flower = {
354 .id = NFP_APP_FLOWER_NIC,
355 .name = "flower",
356 .ctrl_has_meta = true,
357
358 .extra_cap = nfp_flower_extra_cap,
359
360 .init = nfp_flower_init,
Jakub Kicinski9ce6bbb2017-06-27 00:50:19 -0700361 .clean = nfp_flower_clean,
Simon Horman10253512017-06-23 22:12:08 +0200362
363 .vnic_init = nfp_flower_vnic_init,
Simon Horman10253512017-06-23 22:12:08 +0200364
365 .start = nfp_flower_start,
366 .stop = nfp_flower_stop,
367
368 .ctrl_msg_rx = nfp_flower_cmsg_rx,
369
Simon Horman24a021e2017-06-23 22:12:09 +0200370 .sriov_enable = nfp_flower_sriov_enable,
371 .sriov_disable = nfp_flower_sriov_disable,
372
Simon Horman10253512017-06-23 22:12:08 +0200373 .eswitch_mode_get = eswitch_mode_get,
374 .repr_get = nfp_flower_repr_get,
375};