nfp: support port splitting via devlink
[linux-2.6-block.git] / drivers / net / ethernet / netronome / nfp / nfp_devlink.c
CommitLineData
1851f93f
SH
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
53e7a08f 34#include <linux/rtnetlink.h>
1851f93f
SH
35#include <net/devlink.h>
36
53e7a08f
JK
37#include "nfpcore/nfp_nsp.h"
38#include "nfp_app.h"
1851f93f 39#include "nfp_main.h"
53e7a08f
JK
40#include "nfp_port.h"
41
42static int
43nfp_devlink_fill_eth_port(struct nfp_port *port,
44 struct nfp_eth_table_port *copy)
45{
46 struct nfp_eth_table_port *eth_port;
47
48 eth_port = __nfp_port_get_eth_port(port);
49 if (!eth_port)
50 return -EINVAL;
51
52 memcpy(copy, eth_port, sizeof(*eth_port));
53
54 return 0;
55}
1851f93f 56
ec8b1fbe
JK
57static int
58nfp_devlink_fill_eth_port_from_id(struct nfp_pf *pf, unsigned int port_index,
59 struct nfp_eth_table_port *copy)
60{
61 struct nfp_port *port;
62
63 port = nfp_port_from_id(pf, NFP_PORT_PHYS_PORT, port_index);
64
65 return nfp_devlink_fill_eth_port(port, copy);
66}
67
68static int
69nfp_devlink_set_lanes(struct nfp_pf *pf, unsigned int idx, unsigned int lanes)
70{
71 struct nfp_nsp *nsp;
72 int ret;
73
74 nsp = nfp_eth_config_start(pf->cpp, idx);
75 if (IS_ERR(nsp))
76 return PTR_ERR(nsp);
77
78 ret = __nfp_eth_set_split(nsp, lanes);
79 if (ret) {
80 nfp_eth_config_cleanup_end(nsp);
81 return ret;
82 }
83
84 ret = nfp_eth_config_commit_end(nsp);
85 if (ret < 0)
86 return ret;
87 if (ret) /* no change */
88 return 0;
89
90 return nfp_net_refresh_port_table_sync(pf);
91}
92
93static int
94nfp_devlink_port_split(struct devlink *devlink, unsigned int port_index,
95 unsigned int count)
96{
97 struct nfp_pf *pf = devlink_priv(devlink);
98 struct nfp_eth_table_port eth_port;
99 int ret;
100
101 if (count < 2)
102 return -EINVAL;
103
104 mutex_lock(&pf->lock);
105
106 rtnl_lock();
107 ret = nfp_devlink_fill_eth_port_from_id(pf, port_index, &eth_port);
108 rtnl_unlock();
109 if (ret)
110 goto out;
111
112 if (eth_port.is_split || eth_port.port_lanes % count) {
113 ret = -EINVAL;
114 goto out;
115 }
116
117 ret = nfp_devlink_set_lanes(pf, eth_port.index,
118 eth_port.port_lanes / count);
119out:
120 mutex_unlock(&pf->lock);
121
122 return ret;
123}
124
125static int
126nfp_devlink_port_unsplit(struct devlink *devlink, unsigned int port_index)
127{
128 struct nfp_pf *pf = devlink_priv(devlink);
129 struct nfp_eth_table_port eth_port;
130 int ret;
131
132 mutex_lock(&pf->lock);
133
134 rtnl_lock();
135 ret = nfp_devlink_fill_eth_port_from_id(pf, port_index, &eth_port);
136 rtnl_unlock();
137 if (ret)
138 goto out;
139
140 if (!eth_port.is_split) {
141 ret = -EINVAL;
142 goto out;
143 }
144
145 ret = nfp_devlink_set_lanes(pf, eth_port.index, eth_port.port_lanes);
146out:
147 mutex_unlock(&pf->lock);
148
149 return ret;
150}
151
1851f93f 152const struct devlink_ops nfp_devlink_ops = {
ec8b1fbe
JK
153 .port_split = nfp_devlink_port_split,
154 .port_unsplit = nfp_devlink_port_unsplit,
1851f93f 155};
53e7a08f
JK
156
157int nfp_devlink_port_register(struct nfp_app *app, struct nfp_port *port)
158{
159 struct nfp_eth_table_port eth_port;
160 struct devlink *devlink;
161 int ret;
162
163 rtnl_lock();
164 ret = nfp_devlink_fill_eth_port(port, &eth_port);
165 rtnl_unlock();
166 if (ret)
167 return ret;
168
169 devlink_port_type_eth_set(&port->dl_port, port->netdev);
170 if (eth_port.is_split)
171 devlink_port_split_set(&port->dl_port, eth_port.label_port);
172
173 devlink = priv_to_devlink(app->pf);
174
175 return devlink_port_register(devlink, &port->dl_port, port->eth_id);
176}
177
178void nfp_devlink_port_unregister(struct nfp_port *port)
179{
180 devlink_port_unregister(&port->dl_port);
181}