xref: /linux/drivers/net/ethernet/netronome/nfp/nfp_devlink.c (revision 7cc2d504daa0454d2c7c1a0c15a6a21df0ac3c8a)
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2017-2018 Netronome Systems, Inc. */
3 
4 #include <linux/rtnetlink.h>
5 #include <net/devlink.h>
6 
7 #include "nfpcore/nfp_nsp.h"
8 #include "nfp_app.h"
9 #include "nfp_main.h"
10 #include "nfp_port.h"
11 
12 static int
13 nfp_devlink_fill_eth_port(struct nfp_port *port,
14 			  struct nfp_eth_table_port *copy)
15 {
16 	struct nfp_eth_table_port *eth_port;
17 
18 	eth_port = __nfp_port_get_eth_port(port);
19 	if (!eth_port)
20 		return -EINVAL;
21 
22 	memcpy(copy, eth_port, sizeof(*eth_port));
23 
24 	return 0;
25 }
26 
27 static int
28 nfp_devlink_fill_eth_port_from_id(struct nfp_pf *pf, unsigned int port_index,
29 				  struct nfp_eth_table_port *copy)
30 {
31 	struct nfp_port *port;
32 
33 	port = nfp_port_from_id(pf, NFP_PORT_PHYS_PORT, port_index);
34 
35 	return nfp_devlink_fill_eth_port(port, copy);
36 }
37 
38 static int
39 nfp_devlink_set_lanes(struct nfp_pf *pf, unsigned int idx, unsigned int lanes)
40 {
41 	struct nfp_nsp *nsp;
42 	int ret;
43 
44 	nsp = nfp_eth_config_start(pf->cpp, idx);
45 	if (IS_ERR(nsp))
46 		return PTR_ERR(nsp);
47 
48 	ret = __nfp_eth_set_split(nsp, lanes);
49 	if (ret) {
50 		nfp_eth_config_cleanup_end(nsp);
51 		return ret;
52 	}
53 
54 	ret = nfp_eth_config_commit_end(nsp);
55 	if (ret < 0)
56 		return ret;
57 	if (ret) /* no change */
58 		return 0;
59 
60 	return nfp_net_refresh_port_table_sync(pf);
61 }
62 
63 static int
64 nfp_devlink_port_split(struct devlink *devlink, unsigned int port_index,
65 		       unsigned int count, struct netlink_ext_ack *extack)
66 {
67 	struct nfp_pf *pf = devlink_priv(devlink);
68 	struct nfp_eth_table_port eth_port;
69 	int ret;
70 
71 	if (count < 2)
72 		return -EINVAL;
73 
74 	mutex_lock(&pf->lock);
75 
76 	rtnl_lock();
77 	ret = nfp_devlink_fill_eth_port_from_id(pf, port_index, &eth_port);
78 	rtnl_unlock();
79 	if (ret)
80 		goto out;
81 
82 	if (eth_port.is_split || eth_port.port_lanes % count) {
83 		ret = -EINVAL;
84 		goto out;
85 	}
86 
87 	ret = nfp_devlink_set_lanes(pf, eth_port.index,
88 				    eth_port.port_lanes / count);
89 out:
90 	mutex_unlock(&pf->lock);
91 
92 	return ret;
93 }
94 
95 static int
96 nfp_devlink_port_unsplit(struct devlink *devlink, unsigned int port_index,
97 			 struct netlink_ext_ack *extack)
98 {
99 	struct nfp_pf *pf = devlink_priv(devlink);
100 	struct nfp_eth_table_port eth_port;
101 	int ret;
102 
103 	mutex_lock(&pf->lock);
104 
105 	rtnl_lock();
106 	ret = nfp_devlink_fill_eth_port_from_id(pf, port_index, &eth_port);
107 	rtnl_unlock();
108 	if (ret)
109 		goto out;
110 
111 	if (!eth_port.is_split) {
112 		ret = -EINVAL;
113 		goto out;
114 	}
115 
116 	ret = nfp_devlink_set_lanes(pf, eth_port.index, eth_port.port_lanes);
117 out:
118 	mutex_unlock(&pf->lock);
119 
120 	return ret;
121 }
122 
123 static int
124 nfp_devlink_sb_pool_get(struct devlink *devlink, unsigned int sb_index,
125 			u16 pool_index, struct devlink_sb_pool_info *pool_info)
126 {
127 	struct nfp_pf *pf = devlink_priv(devlink);
128 
129 	return nfp_shared_buf_pool_get(pf, sb_index, pool_index, pool_info);
130 }
131 
132 static int
133 nfp_devlink_sb_pool_set(struct devlink *devlink, unsigned int sb_index,
134 			u16 pool_index,
135 			u32 size, enum devlink_sb_threshold_type threshold_type)
136 {
137 	struct nfp_pf *pf = devlink_priv(devlink);
138 
139 	return nfp_shared_buf_pool_set(pf, sb_index, pool_index,
140 				       size, threshold_type);
141 }
142 
143 static int nfp_devlink_eswitch_mode_get(struct devlink *devlink, u16 *mode)
144 {
145 	struct nfp_pf *pf = devlink_priv(devlink);
146 
147 	return nfp_app_eswitch_mode_get(pf->app, mode);
148 }
149 
150 static int nfp_devlink_eswitch_mode_set(struct devlink *devlink, u16 mode,
151 					struct netlink_ext_ack *extack)
152 {
153 	struct nfp_pf *pf = devlink_priv(devlink);
154 	int ret;
155 
156 	mutex_lock(&pf->lock);
157 	ret = nfp_app_eswitch_mode_set(pf->app, mode);
158 	mutex_unlock(&pf->lock);
159 
160 	return ret;
161 }
162 
163 const struct devlink_ops nfp_devlink_ops = {
164 	.port_split		= nfp_devlink_port_split,
165 	.port_unsplit		= nfp_devlink_port_unsplit,
166 	.sb_pool_get		= nfp_devlink_sb_pool_get,
167 	.sb_pool_set		= nfp_devlink_sb_pool_set,
168 	.eswitch_mode_get	= nfp_devlink_eswitch_mode_get,
169 	.eswitch_mode_set	= nfp_devlink_eswitch_mode_set,
170 };
171 
172 int nfp_devlink_port_register(struct nfp_app *app, struct nfp_port *port)
173 {
174 	struct nfp_eth_table_port eth_port;
175 	struct devlink *devlink;
176 	int ret;
177 
178 	rtnl_lock();
179 	ret = nfp_devlink_fill_eth_port(port, &eth_port);
180 	rtnl_unlock();
181 	if (ret)
182 		return ret;
183 
184 	devlink_port_type_eth_set(&port->dl_port, port->netdev);
185 	devlink_port_attrs_set(&port->dl_port, DEVLINK_PORT_FLAVOUR_PHYSICAL,
186 			       eth_port.label_port, eth_port.is_split,
187 			       eth_port.label_subport);
188 
189 	devlink = priv_to_devlink(app->pf);
190 
191 	return devlink_port_register(devlink, &port->dl_port, port->eth_id);
192 }
193 
194 void nfp_devlink_port_unregister(struct nfp_port *port)
195 {
196 	devlink_port_unregister(&port->dl_port);
197 }
198