2 * Copyright (C) 2017 Netronome Systems, Inc.
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.
9 * The BSD 2-Clause License:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * 1. Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
34 #include <linux/rtnetlink.h>
35 #include <net/devlink.h>
37 #include "nfpcore/nfp_nsp.h"
43 nfp_devlink_fill_eth_port(struct nfp_port
*port
,
44 struct nfp_eth_table_port
*copy
)
46 struct nfp_eth_table_port
*eth_port
;
48 eth_port
= __nfp_port_get_eth_port(port
);
52 memcpy(copy
, eth_port
, sizeof(*eth_port
));
58 nfp_devlink_fill_eth_port_from_id(struct nfp_pf
*pf
, unsigned int port_index
,
59 struct nfp_eth_table_port
*copy
)
61 struct nfp_port
*port
;
63 port
= nfp_port_from_id(pf
, NFP_PORT_PHYS_PORT
, port_index
);
65 return nfp_devlink_fill_eth_port(port
, copy
);
69 nfp_devlink_set_lanes(struct nfp_pf
*pf
, unsigned int idx
, unsigned int lanes
)
74 nsp
= nfp_eth_config_start(pf
->cpp
, idx
);
78 ret
= __nfp_eth_set_split(nsp
, lanes
);
80 nfp_eth_config_cleanup_end(nsp
);
84 ret
= nfp_eth_config_commit_end(nsp
);
87 if (ret
) /* no change */
90 return nfp_net_refresh_port_table_sync(pf
);
94 nfp_devlink_port_split(struct devlink
*devlink
, unsigned int port_index
,
97 struct nfp_pf
*pf
= devlink_priv(devlink
);
98 struct nfp_eth_table_port eth_port
;
104 mutex_lock(&pf
->lock
);
107 ret
= nfp_devlink_fill_eth_port_from_id(pf
, port_index
, ð_port
);
112 if (eth_port
.is_split
|| eth_port
.port_lanes
% count
) {
117 ret
= nfp_devlink_set_lanes(pf
, eth_port
.index
,
118 eth_port
.port_lanes
/ count
);
120 mutex_unlock(&pf
->lock
);
126 nfp_devlink_port_unsplit(struct devlink
*devlink
, unsigned int port_index
)
128 struct nfp_pf
*pf
= devlink_priv(devlink
);
129 struct nfp_eth_table_port eth_port
;
132 mutex_lock(&pf
->lock
);
135 ret
= nfp_devlink_fill_eth_port_from_id(pf
, port_index
, ð_port
);
140 if (!eth_port
.is_split
) {
145 ret
= nfp_devlink_set_lanes(pf
, eth_port
.index
, eth_port
.port_lanes
);
147 mutex_unlock(&pf
->lock
);
152 static int nfp_devlink_eswitch_mode_get(struct devlink
*devlink
, u16
*mode
)
154 struct nfp_pf
*pf
= devlink_priv(devlink
);
157 mutex_lock(&pf
->lock
);
162 ret
= nfp_app_eswitch_mode_get(pf
->app
, mode
);
164 mutex_unlock(&pf
->lock
);
169 const struct devlink_ops nfp_devlink_ops
= {
170 .port_split
= nfp_devlink_port_split
,
171 .port_unsplit
= nfp_devlink_port_unsplit
,
172 .eswitch_mode_get
= nfp_devlink_eswitch_mode_get
,
175 int nfp_devlink_port_register(struct nfp_app
*app
, struct nfp_port
*port
)
177 struct nfp_eth_table_port eth_port
;
178 struct devlink
*devlink
;
182 ret
= nfp_devlink_fill_eth_port(port
, ð_port
);
187 devlink_port_type_eth_set(&port
->dl_port
, port
->netdev
);
188 if (eth_port
.is_split
)
189 devlink_port_split_set(&port
->dl_port
, eth_port
.label_port
);
191 devlink
= priv_to_devlink(app
->pf
);
193 return devlink_port_register(devlink
, &port
->dl_port
, port
->eth_id
);
196 void nfp_devlink_port_unregister(struct nfp_port
*port
)
198 devlink_port_unregister(&port
->dl_port
);