blob: 54d8180317ec4a2df9226fca29ee22d30db7ac5b [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
152static void nfp_flower_stop(struct nfp_app *app)
153{
154 nfp_reprs_clean_and_free_by_type(app, NFP_REPR_TYPE_PHYS_PORT);
155}
156
157static int nfp_flower_start(struct nfp_app *app)
158{
159 struct nfp_eth_table *eth_tbl = app->pf->eth_tbl;
160 struct nfp_flower_priv *priv = app->priv;
161 struct nfp_reprs *reprs, *old_reprs;
162 unsigned int i;
163 int err;
164
165 reprs = nfp_reprs_alloc(eth_tbl->max_index + 1);
166 if (!reprs)
167 return -ENOMEM;
168
169 for (i = 0; i < eth_tbl->count; i++) {
170 int phys_port = eth_tbl->ports[i].index;
171 struct nfp_port *port;
172 u32 cmsg_port_id;
173
174 reprs->reprs[phys_port] = nfp_repr_alloc(app);
175 if (!reprs->reprs[phys_port]) {
176 err = -ENOMEM;
177 goto err_reprs_clean;
178 }
179
180 port = nfp_port_alloc(app, NFP_PORT_PHYS_PORT,
181 reprs->reprs[phys_port]);
182 if (IS_ERR(port)) {
183 err = PTR_ERR(port);
184 goto err_reprs_clean;
185 }
186 err = nfp_port_init_phy_port(app->pf, app, port, i);
187 if (err) {
188 nfp_port_free(port);
189 goto err_reprs_clean;
190 }
191
192 SET_NETDEV_DEV(reprs->reprs[phys_port], &priv->nn->pdev->dev);
193 nfp_net_get_mac_addr(app->pf, port,
194 eth_tbl->ports[i].eth_index);
195
196 cmsg_port_id = nfp_flower_cmsg_phys_port(phys_port);
197 err = nfp_repr_init(app, reprs->reprs[phys_port],
198 &nfp_flower_repr_netdev_ops,
199 cmsg_port_id, port, priv->nn->dp.netdev);
200 if (err) {
201 nfp_port_free(port);
202 goto err_reprs_clean;
203 }
204
205 nfp_info(app->cpp, "Phys Port %d Representor(%s) created\n",
206 phys_port, reprs->reprs[phys_port]->name);
207 }
208
209 old_reprs = nfp_app_reprs_set(app, NFP_REPR_TYPE_PHYS_PORT, reprs);
210 if (IS_ERR(old_reprs)) {
211 err = PTR_ERR(old_reprs);
212 goto err_reprs_clean;
213 }
214
215 return 0;
216err_reprs_clean:
217 nfp_reprs_clean_and_free(reprs);
218 return err;
219}
220
221static void nfp_flower_vnic_clean(struct nfp_app *app, struct nfp_net *nn)
222{
223 kfree(app->priv);
224 app->priv = NULL;
225}
226
227static int nfp_flower_vnic_init(struct nfp_app *app, struct nfp_net *nn,
228 unsigned int id)
229{
230 struct nfp_flower_priv *priv;
231
232 if (id > 0) {
233 nfp_warn(app->cpp, "FlowerNIC doesn't support more than one data vNIC\n");
234 goto err_invalid_port;
235 }
236
237 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
238 if (!priv)
239 return -ENOMEM;
240 app->priv = priv;
241 priv->nn = nn;
242
243 eth_hw_addr_random(nn->dp.netdev);
244 netif_keep_dst(nn->dp.netdev);
245
246 return 0;
247
248err_invalid_port:
249 nn->port = nfp_port_alloc(app, NFP_PORT_INVALID, nn->dp.netdev);
250 return PTR_ERR_OR_ZERO(nn->port);
251}
252
253static int nfp_flower_init(struct nfp_app *app)
254{
255 const struct nfp_pf *pf = app->pf;
256
257 if (!pf->eth_tbl) {
258 nfp_warn(app->cpp, "FlowerNIC requires eth table\n");
259 return -EINVAL;
260 }
261
262 if (!pf->mac_stats_bar) {
263 nfp_warn(app->cpp, "FlowerNIC requires mac_stats BAR\n");
264 return -EINVAL;
265 }
266
267 if (!pf->vf_cfg_bar) {
268 nfp_warn(app->cpp, "FlowerNIC requires vf_cfg BAR\n");
269 return -EINVAL;
270 }
271
272 return 0;
273}
274
275const struct nfp_app_type app_flower = {
276 .id = NFP_APP_FLOWER_NIC,
277 .name = "flower",
278 .ctrl_has_meta = true,
279
280 .extra_cap = nfp_flower_extra_cap,
281
282 .init = nfp_flower_init,
283
284 .vnic_init = nfp_flower_vnic_init,
285 .vnic_clean = nfp_flower_vnic_clean,
286
287 .start = nfp_flower_start,
288 .stop = nfp_flower_stop,
289
290 .ctrl_msg_rx = nfp_flower_cmsg_rx,
291
292 .eswitch_mode_get = eswitch_mode_get,
293 .repr_get = nfp_flower_repr_get,
294};