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
34 #include <linux/rtnetlink.h>
35 #include <net/devlink.h>
36
37 #include "nfpcore/nfp_nsp.h"
38 #include "nfp_app.h"
39 #include "nfp_main.h"
40 #include "nfp_port.h"
41
42 static int
nfp_devlink_fill_eth_port(struct nfp_port * port,struct nfp_eth_table_port * copy)43 nfp_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 }
56
57 static int
nfp_devlink_fill_eth_port_from_id(struct nfp_pf * pf,unsigned int port_index,struct nfp_eth_table_port * copy)58 nfp_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
68 static int
nfp_devlink_set_lanes(struct nfp_pf * pf,unsigned int idx,unsigned int lanes)69 nfp_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
93 static int
nfp_devlink_port_split(struct devlink * devlink,unsigned int port_index,unsigned int count,struct netlink_ext_ack * extack)94 nfp_devlink_port_split(struct devlink *devlink, unsigned int port_index,
95 unsigned int count, struct netlink_ext_ack *extack)
96 {
97 struct nfp_pf *pf = devlink_priv(devlink);
98 struct nfp_eth_table_port eth_port;
99 unsigned int lanes;
100 int ret;
101
102 if (count < 2)
103 return -EINVAL;
104
105 mutex_lock(&pf->lock);
106
107 rtnl_lock();
108 ret = nfp_devlink_fill_eth_port_from_id(pf, port_index, ð_port);
109 rtnl_unlock();
110 if (ret)
111 goto out;
112
113 if (eth_port.is_split || eth_port.port_lanes % count) {
114 ret = -EINVAL;
115 goto out;
116 }
117
118 /* Special case the 100G CXP -> 2x40G split */
119 lanes = eth_port.port_lanes / count;
120 if (eth_port.lanes == 10 && count == 2)
121 lanes = 8 / count;
122
123 ret = nfp_devlink_set_lanes(pf, eth_port.index, lanes);
124 out:
125 mutex_unlock(&pf->lock);
126
127 return ret;
128 }
129
130 static int
nfp_devlink_port_unsplit(struct devlink * devlink,unsigned int port_index,struct netlink_ext_ack * extack)131 nfp_devlink_port_unsplit(struct devlink *devlink, unsigned int port_index,
132 struct netlink_ext_ack *extack)
133 {
134 struct nfp_pf *pf = devlink_priv(devlink);
135 struct nfp_eth_table_port eth_port;
136 unsigned int lanes;
137 int ret;
138
139 mutex_lock(&pf->lock);
140
141 rtnl_lock();
142 ret = nfp_devlink_fill_eth_port_from_id(pf, port_index, ð_port);
143 rtnl_unlock();
144 if (ret)
145 goto out;
146
147 if (!eth_port.is_split) {
148 ret = -EINVAL;
149 goto out;
150 }
151
152 /* Special case the 100G CXP -> 2x40G unsplit */
153 lanes = eth_port.port_lanes;
154 if (eth_port.port_lanes == 8)
155 lanes = 10;
156
157 ret = nfp_devlink_set_lanes(pf, eth_port.index, lanes);
158 out:
159 mutex_unlock(&pf->lock);
160
161 return ret;
162 }
163
164 static int
nfp_devlink_sb_pool_get(struct devlink * devlink,unsigned int sb_index,u16 pool_index,struct devlink_sb_pool_info * pool_info)165 nfp_devlink_sb_pool_get(struct devlink *devlink, unsigned int sb_index,
166 u16 pool_index, struct devlink_sb_pool_info *pool_info)
167 {
168 struct nfp_pf *pf = devlink_priv(devlink);
169
170 return nfp_shared_buf_pool_get(pf, sb_index, pool_index, pool_info);
171 }
172
173 static int
nfp_devlink_sb_pool_set(struct devlink * devlink,unsigned int sb_index,u16 pool_index,u32 size,enum devlink_sb_threshold_type threshold_type)174 nfp_devlink_sb_pool_set(struct devlink *devlink, unsigned int sb_index,
175 u16 pool_index,
176 u32 size, enum devlink_sb_threshold_type threshold_type)
177 {
178 struct nfp_pf *pf = devlink_priv(devlink);
179
180 return nfp_shared_buf_pool_set(pf, sb_index, pool_index,
181 size, threshold_type);
182 }
183
nfp_devlink_eswitch_mode_get(struct devlink * devlink,u16 * mode)184 static int nfp_devlink_eswitch_mode_get(struct devlink *devlink, u16 *mode)
185 {
186 struct nfp_pf *pf = devlink_priv(devlink);
187
188 return nfp_app_eswitch_mode_get(pf->app, mode);
189 }
190
nfp_devlink_eswitch_mode_set(struct devlink * devlink,u16 mode)191 static int nfp_devlink_eswitch_mode_set(struct devlink *devlink, u16 mode)
192 {
193 struct nfp_pf *pf = devlink_priv(devlink);
194 int ret;
195
196 mutex_lock(&pf->lock);
197 ret = nfp_app_eswitch_mode_set(pf->app, mode);
198 mutex_unlock(&pf->lock);
199
200 return ret;
201 }
202
203 const struct devlink_ops nfp_devlink_ops = {
204 .port_split = nfp_devlink_port_split,
205 .port_unsplit = nfp_devlink_port_unsplit,
206 .sb_pool_get = nfp_devlink_sb_pool_get,
207 .sb_pool_set = nfp_devlink_sb_pool_set,
208 .eswitch_mode_get = nfp_devlink_eswitch_mode_get,
209 .eswitch_mode_set = nfp_devlink_eswitch_mode_set,
210 };
211
nfp_devlink_port_register(struct nfp_app * app,struct nfp_port * port)212 int nfp_devlink_port_register(struct nfp_app *app, struct nfp_port *port)
213 {
214 struct nfp_eth_table_port eth_port;
215 struct devlink *devlink;
216 int ret;
217
218 rtnl_lock();
219 ret = nfp_devlink_fill_eth_port(port, ð_port);
220 rtnl_unlock();
221 if (ret)
222 return ret;
223
224 devlink_port_type_eth_set(&port->dl_port, port->netdev);
225 devlink_port_attrs_set(&port->dl_port, DEVLINK_PORT_FLAVOUR_PHYSICAL,
226 eth_port.label_port, eth_port.is_split,
227 eth_port.label_subport);
228
229 devlink = priv_to_devlink(app->pf);
230
231 return devlink_port_register(devlink, &port->dl_port, port->eth_id);
232 }
233
nfp_devlink_port_unregister(struct nfp_port * port)234 void nfp_devlink_port_unregister(struct nfp_port *port)
235 {
236 devlink_port_unregister(&port->dl_port);
237 }
238