xref: /linux/drivers/net/ethernet/mellanox/mlx5/core/ipoib/ipoib.c (revision fab183d632628381b466a41479489541ac0e29a0)
1 /*
2  * Copyright (c) 2017, Mellanox Technologies. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32 
33 #include <rdma/ib_verbs.h>
34 #include <linux/mlx5/fs.h>
35 #include <net/netdev_lock.h>
36 #include "en.h"
37 #include "en/params.h"
38 #include "ipoib.h"
39 #include "en/fs_ethtool.h"
40 
41 #define IB_DEFAULT_Q_KEY   0xb1b
42 #define MLX5I_PARAMS_DEFAULT_LOG_RQ_SIZE 9
43 
44 static int mlx5i_open(struct net_device *netdev);
45 static int mlx5i_close(struct net_device *netdev);
46 static int mlx5i_change_mtu(struct net_device *netdev, int new_mtu);
47 
mlx5i_hwtstamp_set(struct net_device * dev,struct kernel_hwtstamp_config * config,struct netlink_ext_ack * extack)48 int mlx5i_hwtstamp_set(struct net_device *dev,
49 		       struct kernel_hwtstamp_config *config,
50 		       struct netlink_ext_ack *extack)
51 {
52 	struct mlx5e_priv *epriv = mlx5i_epriv(dev);
53 
54 	return mlx5e_hwtstamp_set(epriv, config, extack);
55 }
56 
mlx5i_hwtstamp_get(struct net_device * dev,struct kernel_hwtstamp_config * config)57 int mlx5i_hwtstamp_get(struct net_device *dev,
58 		       struct kernel_hwtstamp_config *config)
59 {
60 	struct mlx5e_priv *epriv = mlx5i_epriv(dev);
61 
62 	return mlx5e_hwtstamp_get(epriv, config);
63 }
64 
65 static const struct net_device_ops mlx5i_netdev_ops = {
66 	.ndo_open                = mlx5i_open,
67 	.ndo_stop                = mlx5i_close,
68 	.ndo_get_stats64         = mlx5i_get_stats,
69 	.ndo_init                = mlx5i_dev_init,
70 	.ndo_uninit              = mlx5i_dev_cleanup,
71 	.ndo_change_mtu          = mlx5i_change_mtu,
72 	.ndo_hwtstamp_get        = mlx5i_hwtstamp_get,
73 	.ndo_hwtstamp_set        = mlx5i_hwtstamp_set,
74 };
75 
76 /* IPoIB mlx5 netdev profile */
mlx5i_build_nic_params(struct mlx5_core_dev * mdev,struct mlx5e_params * params)77 static void mlx5i_build_nic_params(struct mlx5_core_dev *mdev,
78 				   struct mlx5e_params *params)
79 {
80 	/* Override RQ params as IPoIB supports only LINKED LIST RQ for now */
81 	MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_STRIDING_RQ, false);
82 	mlx5e_set_rq_type(mdev, params);
83 	mlx5e_init_rq_type_params(mdev, params);
84 
85 	/* RQ size in ipoib by default is 512 */
86 	params->log_rq_mtu_frames = is_kdump_kernel() ?
87 		MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE :
88 		MLX5I_PARAMS_DEFAULT_LOG_RQ_SIZE;
89 
90 	params->packet_merge.type = MLX5E_PACKET_MERGE_NONE;
91 	params->hard_mtu = MLX5_IB_GRH_BYTES + MLX5_IPOIB_HARD_LEN;
92 
93 	/* CQE compression is not supported for IPoIB */
94 	params->rx_cqe_compress_def = false;
95 	MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS, params->rx_cqe_compress_def);
96 }
97 
98 /* Called directly after IPoIB netdevice was created to initialize SW structs */
mlx5i_init(struct mlx5_core_dev * mdev,struct net_device * netdev)99 int mlx5i_init(struct mlx5_core_dev *mdev, struct net_device *netdev)
100 {
101 	struct mlx5e_priv *priv  = mlx5i_epriv(netdev);
102 
103 	netif_carrier_off(netdev);
104 	mlx5e_set_netdev_mtu_boundaries(priv);
105 	netdev->mtu = netdev->max_mtu;
106 
107 	mlx5e_build_nic_params(priv, NULL, netdev->mtu);
108 	mlx5i_build_nic_params(mdev, &priv->channels.params);
109 
110 	mlx5e_timestamp_init(priv);
111 
112 	/* netdev init */
113 	netdev->hw_features    |= NETIF_F_SG;
114 	netdev->hw_features    |= NETIF_F_IP_CSUM;
115 	netdev->hw_features    |= NETIF_F_IPV6_CSUM;
116 	netdev->hw_features    |= NETIF_F_GRO;
117 	netdev->hw_features    |= NETIF_F_TSO;
118 	netdev->hw_features    |= NETIF_F_TSO6;
119 	netdev->hw_features    |= NETIF_F_RXCSUM;
120 	netdev->hw_features    |= NETIF_F_RXHASH;
121 
122 	netdev->netdev_ops = &mlx5i_netdev_ops;
123 	netdev->ethtool_ops = &mlx5i_ethtool_ops;
124 	netdev->request_ops_lock = true;
125 	netdev_lockdep_set_classes(netdev);
126 
127 	return 0;
128 }
129 
130 /* Called directly before IPoIB netdevice is destroyed to cleanup SW structs */
mlx5i_cleanup(struct mlx5e_priv * priv)131 void mlx5i_cleanup(struct mlx5e_priv *priv)
132 {
133 	mlx5e_priv_cleanup(priv);
134 }
135 
mlx5i_grp_sw_update_stats(struct mlx5e_priv * priv)136 static void mlx5i_grp_sw_update_stats(struct mlx5e_priv *priv)
137 {
138 	u16 nch = mlx5e_stats_nch_read(priv);
139 	struct rtnl_link_stats64 s = {};
140 	int i, j;
141 
142 	for (i = 0; i < nch; i++) {
143 		struct mlx5e_channel_stats *channel_stats;
144 		struct mlx5e_rq_stats *rq_stats;
145 
146 		channel_stats = priv->channel_stats[i];
147 		rq_stats = &channel_stats->rq;
148 
149 		s.rx_packets += rq_stats->packets;
150 		s.rx_bytes   += rq_stats->bytes;
151 
152 		for (j = 0; j < priv->max_opened_tc; j++) {
153 			struct mlx5e_sq_stats *sq_stats = &channel_stats->sq[j];
154 
155 			s.tx_packets           += sq_stats->packets;
156 			s.tx_bytes             += sq_stats->bytes;
157 			s.tx_dropped           += sq_stats->dropped;
158 		}
159 	}
160 
161 	memset(&priv->stats.sw, 0, sizeof(s));
162 
163 	priv->stats.sw.rx_packets = s.rx_packets;
164 	priv->stats.sw.rx_bytes = s.rx_bytes;
165 	priv->stats.sw.tx_packets = s.tx_packets;
166 	priv->stats.sw.tx_bytes = s.tx_bytes;
167 	priv->stats.sw.tx_queue_dropped = s.tx_dropped;
168 }
169 
mlx5i_get_stats(struct net_device * dev,struct rtnl_link_stats64 * stats)170 void mlx5i_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
171 {
172 	struct mlx5e_priv     *priv   = mlx5i_epriv(dev);
173 	struct mlx5e_sw_stats *sstats = &priv->stats.sw;
174 
175 	mlx5i_grp_sw_update_stats(priv);
176 
177 	stats->rx_packets = sstats->rx_packets;
178 	stats->rx_bytes   = sstats->rx_bytes;
179 	stats->tx_packets = sstats->tx_packets;
180 	stats->tx_bytes   = sstats->tx_bytes;
181 	stats->tx_dropped = sstats->tx_queue_dropped;
182 }
183 
mlx5i_parent_get(struct net_device * netdev)184 struct net_device *mlx5i_parent_get(struct net_device *netdev)
185 {
186 	struct mlx5e_priv *priv = mlx5i_epriv(netdev);
187 	struct mlx5i_priv *ipriv, *parent_ipriv;
188 	struct net_device *parent_dev;
189 	int parent_ifindex;
190 
191 	ipriv = priv->ppriv;
192 
193 	parent_ifindex = netdev->netdev_ops->ndo_get_iflink(netdev);
194 	parent_dev = dev_get_by_index(dev_net(netdev), parent_ifindex);
195 	if (!parent_dev)
196 		return NULL;
197 
198 	parent_ipriv = netdev_priv(parent_dev);
199 
200 	ASSERT_RTNL();
201 	parent_ipriv->num_sub_interfaces++;
202 
203 	ipriv->parent_dev = parent_dev;
204 
205 	return parent_dev;
206 }
207 
mlx5i_parent_put(struct net_device * netdev)208 void mlx5i_parent_put(struct net_device *netdev)
209 {
210 	struct mlx5e_priv *priv = mlx5i_epriv(netdev);
211 	struct mlx5i_priv *ipriv, *parent_ipriv;
212 
213 	ipriv = priv->ppriv;
214 	parent_ipriv = netdev_priv(ipriv->parent_dev);
215 
216 	ASSERT_RTNL();
217 	parent_ipriv->num_sub_interfaces--;
218 
219 	dev_put(ipriv->parent_dev);
220 }
221 
mlx5i_init_underlay_qp(struct mlx5e_priv * priv)222 int mlx5i_init_underlay_qp(struct mlx5e_priv *priv)
223 {
224 	struct mlx5_core_dev *mdev = priv->mdev;
225 	struct mlx5i_priv *ipriv = priv->ppriv;
226 	int ret;
227 
228 	{
229 		u32 in[MLX5_ST_SZ_DW(rst2init_qp_in)] = {};
230 		u32 *qpc;
231 
232 		qpc = MLX5_ADDR_OF(rst2init_qp_in, in, qpc);
233 
234 		MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_MIGRATED);
235 		MLX5_SET(qpc, qpc, primary_address_path.pkey_index,
236 			 ipriv->pkey_index);
237 		MLX5_SET(qpc, qpc, primary_address_path.vhca_port_num, 1);
238 		MLX5_SET(qpc, qpc, q_key, IB_DEFAULT_Q_KEY);
239 
240 		MLX5_SET(rst2init_qp_in, in, opcode, MLX5_CMD_OP_RST2INIT_QP);
241 		MLX5_SET(rst2init_qp_in, in, qpn, ipriv->qpn);
242 		ret = mlx5_cmd_exec_in(mdev, rst2init_qp, in);
243 		if (ret)
244 			goto err_qp_modify_to_err;
245 	}
246 	{
247 		u32 in[MLX5_ST_SZ_DW(init2rtr_qp_in)] = {};
248 
249 		MLX5_SET(init2rtr_qp_in, in, opcode, MLX5_CMD_OP_INIT2RTR_QP);
250 		MLX5_SET(init2rtr_qp_in, in, qpn, ipriv->qpn);
251 		ret = mlx5_cmd_exec_in(mdev, init2rtr_qp, in);
252 		if (ret)
253 			goto err_qp_modify_to_err;
254 	}
255 	{
256 		u32 in[MLX5_ST_SZ_DW(rtr2rts_qp_in)] = {};
257 
258 		MLX5_SET(rtr2rts_qp_in, in, opcode, MLX5_CMD_OP_RTR2RTS_QP);
259 		MLX5_SET(rtr2rts_qp_in, in, qpn, ipriv->qpn);
260 		ret = mlx5_cmd_exec_in(mdev, rtr2rts_qp, in);
261 		if (ret)
262 			goto err_qp_modify_to_err;
263 	}
264 	return 0;
265 
266 err_qp_modify_to_err:
267 	{
268 		u32 in[MLX5_ST_SZ_DW(qp_2err_in)] = {};
269 
270 		MLX5_SET(qp_2err_in, in, opcode, MLX5_CMD_OP_2ERR_QP);
271 		MLX5_SET(qp_2err_in, in, qpn, ipriv->qpn);
272 		mlx5_cmd_exec_in(mdev, qp_2err, in);
273 	}
274 	return ret;
275 }
276 
mlx5i_uninit_underlay_qp(struct mlx5e_priv * priv)277 void mlx5i_uninit_underlay_qp(struct mlx5e_priv *priv)
278 {
279 	struct mlx5i_priv *ipriv = priv->ppriv;
280 	struct mlx5_core_dev *mdev = priv->mdev;
281 	u32 in[MLX5_ST_SZ_DW(qp_2rst_in)] = {};
282 
283 	MLX5_SET(qp_2rst_in, in, opcode, MLX5_CMD_OP_2RST_QP);
284 	MLX5_SET(qp_2rst_in, in, qpn, ipriv->qpn);
285 	mlx5_cmd_exec_in(mdev, qp_2rst, in);
286 }
287 
288 #define MLX5_QP_ENHANCED_ULP_STATELESS_MODE 2
289 
mlx5i_create_underlay_qp(struct mlx5e_priv * priv)290 int mlx5i_create_underlay_qp(struct mlx5e_priv *priv)
291 {
292 	const unsigned char *dev_addr = priv->netdev->dev_addr;
293 	u32 out[MLX5_ST_SZ_DW(create_qp_out)] = {};
294 	u32 in[MLX5_ST_SZ_DW(create_qp_in)] = {};
295 	struct mlx5i_priv *ipriv = priv->ppriv;
296 	void *addr_path;
297 	int qpn = 0;
298 	int ret = 0;
299 	void *qpc;
300 
301 	if (MLX5_CAP_GEN(priv->mdev, mkey_by_name)) {
302 		qpn = (dev_addr[1] << 16) + (dev_addr[2] << 8) + dev_addr[3];
303 		MLX5_SET(create_qp_in, in, input_qpn, qpn);
304 	}
305 
306 	qpc = MLX5_ADDR_OF(create_qp_in, in, qpc);
307 	MLX5_SET(qpc, qpc, ts_format, mlx5_get_qp_default_ts(priv->mdev));
308 	MLX5_SET(qpc, qpc, st, MLX5_QP_ST_UD);
309 	MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_MIGRATED);
310 	MLX5_SET(qpc, qpc, ulp_stateless_offload_mode,
311 		 MLX5_QP_ENHANCED_ULP_STATELESS_MODE);
312 
313 	addr_path = MLX5_ADDR_OF(qpc, qpc, primary_address_path);
314 	MLX5_SET(ads, addr_path, vhca_port_num, 1);
315 	MLX5_SET(ads, addr_path, grh, 1);
316 
317 	MLX5_SET(create_qp_in, in, opcode, MLX5_CMD_OP_CREATE_QP);
318 	ret = mlx5_cmd_exec_inout(priv->mdev, create_qp, in, out);
319 	if (ret)
320 		return ret;
321 
322 	ipriv->qpn = MLX5_GET(create_qp_out, out, qpn);
323 
324 	return 0;
325 }
326 
mlx5i_destroy_underlay_qp(struct mlx5_core_dev * mdev,u32 qpn)327 void mlx5i_destroy_underlay_qp(struct mlx5_core_dev *mdev, u32 qpn)
328 {
329 	u32 in[MLX5_ST_SZ_DW(destroy_qp_in)] = {};
330 
331 	MLX5_SET(destroy_qp_in, in, opcode, MLX5_CMD_OP_DESTROY_QP);
332 	MLX5_SET(destroy_qp_in, in, qpn, qpn);
333 	mlx5_cmd_exec_in(mdev, destroy_qp, in);
334 }
335 
mlx5i_update_nic_rx(struct mlx5e_priv * priv)336 int mlx5i_update_nic_rx(struct mlx5e_priv *priv)
337 {
338 	return mlx5e_refresh_tirs(priv->mdev, true, true);
339 }
340 
mlx5i_create_tis(struct mlx5_core_dev * mdev,u32 underlay_qpn,u32 * tisn)341 int mlx5i_create_tis(struct mlx5_core_dev *mdev, u32 underlay_qpn, u32 *tisn)
342 {
343 	u32 in[MLX5_ST_SZ_DW(create_tis_in)] = {};
344 	void *tisc;
345 
346 	tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
347 
348 	MLX5_SET(tisc, tisc, underlay_qpn, underlay_qpn);
349 
350 	return mlx5e_create_tis(mdev, in, tisn);
351 }
352 
mlx5i_init_tx(struct mlx5e_priv * priv)353 static int mlx5i_init_tx(struct mlx5e_priv *priv)
354 {
355 	struct mlx5i_priv *ipriv = priv->ppriv;
356 	int err;
357 
358 	err = mlx5i_create_underlay_qp(priv);
359 	if (err) {
360 		mlx5_core_warn(priv->mdev, "create underlay QP failed, %d\n", err);
361 		return err;
362 	}
363 
364 	err = mlx5i_create_tis(priv->mdev, ipriv->qpn, &ipriv->tisn);
365 	if (err) {
366 		mlx5_core_warn(priv->mdev, "create tis failed, %d\n", err);
367 		goto err_destroy_underlay_qp;
368 	}
369 
370 	return 0;
371 
372 err_destroy_underlay_qp:
373 	mlx5i_destroy_underlay_qp(priv->mdev, ipriv->qpn);
374 	return err;
375 }
376 
mlx5i_cleanup_tx(struct mlx5e_priv * priv)377 static void mlx5i_cleanup_tx(struct mlx5e_priv *priv)
378 {
379 	struct mlx5i_priv *ipriv = priv->ppriv;
380 
381 	mlx5e_destroy_tis(priv->mdev, ipriv->tisn);
382 	mlx5i_destroy_underlay_qp(priv->mdev, ipriv->qpn);
383 }
384 
mlx5i_create_flow_steering(struct mlx5e_priv * priv)385 static int mlx5i_create_flow_steering(struct mlx5e_priv *priv)
386 {
387 	struct mlx5_flow_namespace *ns =
388 		mlx5_get_flow_namespace(priv->mdev, MLX5_FLOW_NAMESPACE_KERNEL);
389 	int err;
390 
391 
392 	if (!ns)
393 		return -EINVAL;
394 
395 	mlx5e_fs_set_ns(priv->fs, ns, false);
396 	err = mlx5e_arfs_create_tables(priv->fs, priv->rx_res,
397 				       mlx5e_fs_has_arfs(priv->netdev));
398 	if (err) {
399 		netdev_err(priv->netdev, "Failed to create arfs tables, err=%d\n",
400 			   err);
401 		priv->netdev->hw_features &= ~NETIF_F_NTUPLE;
402 	}
403 
404 	err = mlx5e_create_ttc_table(priv->fs, priv->rx_res);
405 	if (err) {
406 		netdev_err(priv->netdev, "Failed to create ttc table, err=%d\n",
407 			   err);
408 		goto err_destroy_arfs_tables;
409 	}
410 
411 	mlx5e_ethtool_init_steering(priv->fs);
412 
413 	return 0;
414 
415 err_destroy_arfs_tables:
416 	mlx5e_arfs_destroy_tables(priv->fs, mlx5e_fs_has_arfs(priv->netdev));
417 
418 	return err;
419 }
420 
mlx5i_destroy_flow_steering(struct mlx5e_priv * priv)421 static void mlx5i_destroy_flow_steering(struct mlx5e_priv *priv)
422 {
423 	mlx5e_destroy_ttc_table(priv->fs);
424 	mlx5e_arfs_destroy_tables(priv->fs, mlx5e_fs_has_arfs(priv->netdev));
425 	mlx5e_ethtool_cleanup_steering(priv->fs);
426 }
427 
mlx5i_init_rx(struct mlx5e_priv * priv)428 static int mlx5i_init_rx(struct mlx5e_priv *priv)
429 {
430 	struct mlx5_core_dev *mdev = priv->mdev;
431 	enum mlx5e_rx_res_features features;
432 	int err;
433 
434 	priv->fs = mlx5e_fs_init(priv->profile, mdev,
435 				 !test_bit(MLX5E_STATE_DESTROYING, &priv->state),
436 				 priv->dfs_root);
437 	if (!priv->fs) {
438 		netdev_err(priv->netdev, "FS allocation failed\n");
439 		return -ENOMEM;
440 	}
441 
442 	mlx5e_create_q_counters(priv);
443 
444 	err = mlx5e_open_drop_rq(priv, &priv->drop_rq);
445 	if (err) {
446 		mlx5_core_err(mdev, "open drop rq failed, %d\n", err);
447 		goto err_destroy_q_counters;
448 	}
449 
450 	features = MLX5E_RX_RES_FEATURE_SELF_LB_BLOCK;
451 	priv->rx_res = mlx5e_rx_res_create(priv->mdev, features, priv->max_nch,
452 					   priv->drop_rq.rqn,
453 					   &priv->channels.params.packet_merge,
454 					   priv->channels.params.num_channels);
455 	if (IS_ERR(priv->rx_res)) {
456 		err = PTR_ERR(priv->rx_res);
457 		goto err_close_drop_rq;
458 	}
459 
460 	err = mlx5i_create_flow_steering(priv);
461 	if (err)
462 		goto err_destroy_rx_res;
463 
464 	return 0;
465 
466 err_destroy_rx_res:
467 	mlx5e_rx_res_destroy(priv->rx_res);
468 	priv->rx_res = ERR_PTR(-EINVAL);
469 err_close_drop_rq:
470 	mlx5e_close_drop_rq(&priv->drop_rq);
471 err_destroy_q_counters:
472 	mlx5e_destroy_q_counters(priv);
473 	mlx5e_fs_cleanup(priv->fs);
474 	return err;
475 }
476 
mlx5i_cleanup_rx(struct mlx5e_priv * priv)477 static void mlx5i_cleanup_rx(struct mlx5e_priv *priv)
478 {
479 	mlx5i_destroy_flow_steering(priv);
480 	mlx5e_rx_res_destroy(priv->rx_res);
481 	priv->rx_res = ERR_PTR(-EINVAL);
482 	mlx5e_close_drop_rq(&priv->drop_rq);
483 	mlx5e_destroy_q_counters(priv);
484 	mlx5e_fs_cleanup(priv->fs);
485 }
486 
487 /* The stats groups order is opposite to the update_stats() order calls */
488 static mlx5e_stats_grp_t mlx5i_stats_grps[] = {
489 	&MLX5E_STATS_GRP(sw),
490 	&MLX5E_STATS_GRP(qcnt),
491 	&MLX5E_STATS_GRP(vnic_env),
492 	&MLX5E_STATS_GRP(vport),
493 	&MLX5E_STATS_GRP(802_3),
494 	&MLX5E_STATS_GRP(2863),
495 	&MLX5E_STATS_GRP(2819),
496 	&MLX5E_STATS_GRP(phy),
497 	&MLX5E_STATS_GRP(pcie),
498 	&MLX5E_STATS_GRP(per_prio),
499 	&MLX5E_STATS_GRP(pme),
500 	&MLX5E_STATS_GRP(channels),
501 	&MLX5E_STATS_GRP(per_port_buff_congest),
502 };
503 
mlx5i_stats_grps_num(struct mlx5e_priv * priv)504 static unsigned int mlx5i_stats_grps_num(struct mlx5e_priv *priv)
505 {
506 	return ARRAY_SIZE(mlx5i_stats_grps);
507 }
508 
mlx5i_get_tisn(struct mlx5_core_dev * mdev,struct mlx5e_priv * priv,u8 lag_port,u8 tc)509 u32 mlx5i_get_tisn(struct mlx5_core_dev *mdev, struct mlx5e_priv *priv, u8 lag_port, u8 tc)
510 {
511 	struct mlx5i_priv *ipriv = priv->ppriv;
512 
513 	if (WARN(lag_port || tc,
514 		 "IPoIB unexpected non-zero value: lag_port (%u), tc (%u)\n",
515 		 lag_port, tc))
516 		return 0;
517 
518 	return ipriv->tisn;
519 }
520 
521 static const struct mlx5e_profile mlx5i_nic_profile = {
522 	.init		   = mlx5i_init,
523 	.cleanup	   = mlx5i_cleanup,
524 	.init_tx	   = mlx5i_init_tx,
525 	.cleanup_tx	   = mlx5i_cleanup_tx,
526 	.init_rx	   = mlx5i_init_rx,
527 	.cleanup_rx	   = mlx5i_cleanup_rx,
528 	.enable		   = NULL, /* mlx5i_enable */
529 	.disable	   = NULL, /* mlx5i_disable */
530 	.update_rx	   = mlx5i_update_nic_rx,
531 	.update_stats	   = NULL, /* mlx5i_update_stats */
532 	.update_carrier    = NULL, /* no HW update in IB link */
533 	.rx_handlers       = &mlx5i_rx_handlers,
534 	.max_tc		   = MLX5I_MAX_NUM_TC,
535 	.stats_grps        = mlx5i_stats_grps,
536 	.stats_grps_num    = mlx5i_stats_grps_num,
537 	.get_tisn          = mlx5i_get_tisn,
538 };
539 
540 /* mlx5i netdev NDos */
541 
mlx5i_change_mtu(struct net_device * netdev,int new_mtu)542 static int mlx5i_change_mtu(struct net_device *netdev, int new_mtu)
543 {
544 	struct mlx5e_priv *priv = mlx5i_epriv(netdev);
545 	struct mlx5e_params new_params;
546 	int err = 0;
547 
548 	mutex_lock(&priv->state_lock);
549 
550 	new_params = priv->channels.params;
551 	new_params.sw_mtu = new_mtu;
552 
553 	err = mlx5e_safe_switch_params(priv, &new_params, NULL, NULL, true);
554 	if (err)
555 		goto out;
556 
557 	WRITE_ONCE(netdev->mtu, new_params.sw_mtu);
558 
559 out:
560 	mutex_unlock(&priv->state_lock);
561 	return err;
562 }
563 
mlx5i_dev_init(struct net_device * dev)564 int mlx5i_dev_init(struct net_device *dev)
565 {
566 	struct mlx5e_priv    *priv   = mlx5i_epriv(dev);
567 	struct mlx5i_priv    *ipriv  = priv->ppriv;
568 	u8 addr_mod[3];
569 
570 	/* Set dev address using underlay QP */
571 	addr_mod[0] = (ipriv->qpn >> 16) & 0xff;
572 	addr_mod[1] = (ipriv->qpn >>  8) & 0xff;
573 	addr_mod[2] = (ipriv->qpn) & 0xff;
574 	dev_addr_mod(dev, 1, addr_mod, sizeof(addr_mod));
575 
576 	/* Add QPN to net-device mapping to HT */
577 	mlx5i_pkey_add_qpn(dev, ipriv->qpn);
578 
579 	return 0;
580 }
581 
mlx5i_dev_cleanup(struct net_device * dev)582 void mlx5i_dev_cleanup(struct net_device *dev)
583 {
584 	struct mlx5e_priv    *priv   = mlx5i_epriv(dev);
585 	struct mlx5i_priv    *ipriv = priv->ppriv;
586 
587 	mlx5i_uninit_underlay_qp(priv);
588 
589 	/* Delete QPN to net-device mapping from HT */
590 	mlx5i_pkey_del_qpn(dev, ipriv->qpn);
591 }
592 
mlx5i_open(struct net_device * netdev)593 static int mlx5i_open(struct net_device *netdev)
594 {
595 	struct mlx5e_priv *epriv = mlx5i_epriv(netdev);
596 	struct mlx5i_priv *ipriv = epriv->ppriv;
597 	struct mlx5_core_dev *mdev = epriv->mdev;
598 	int err;
599 
600 	mutex_lock(&epriv->state_lock);
601 
602 	set_bit(MLX5E_STATE_OPENED, &epriv->state);
603 
604 	err = mlx5i_init_underlay_qp(epriv);
605 	if (err) {
606 		mlx5_core_warn(mdev, "prepare underlay qp state failed, %d\n", err);
607 		goto err_clear_state_opened_flag;
608 	}
609 
610 	err = mlx5_fs_add_rx_underlay_qpn(mdev, ipriv->qpn);
611 	if (err) {
612 		mlx5_core_warn(mdev, "attach underlay qp to ft failed, %d\n", err);
613 		goto err_reset_qp;
614 	}
615 
616 	err = mlx5e_open_channels(epriv, &epriv->channels);
617 	if (err)
618 		goto err_remove_fs_underlay_qp;
619 
620 	err = epriv->profile->update_rx(epriv);
621 	if (err)
622 		goto err_close_channels;
623 
624 	mlx5e_activate_priv_channels(epriv);
625 
626 	mutex_unlock(&epriv->state_lock);
627 	return 0;
628 
629 err_close_channels:
630 	mlx5e_close_channels(&epriv->channels);
631 err_remove_fs_underlay_qp:
632 	mlx5_fs_remove_rx_underlay_qpn(mdev, ipriv->qpn);
633 err_reset_qp:
634 	mlx5i_uninit_underlay_qp(epriv);
635 err_clear_state_opened_flag:
636 	clear_bit(MLX5E_STATE_OPENED, &epriv->state);
637 	mutex_unlock(&epriv->state_lock);
638 	return err;
639 }
640 
mlx5i_close(struct net_device * netdev)641 static int mlx5i_close(struct net_device *netdev)
642 {
643 	struct mlx5e_priv *epriv = mlx5i_epriv(netdev);
644 	struct mlx5i_priv *ipriv = epriv->ppriv;
645 	struct mlx5_core_dev *mdev = epriv->mdev;
646 
647 	/* May already be CLOSED in case a previous configuration operation
648 	 * (e.g RX/TX queue size change) that involves close&open failed.
649 	 */
650 	mutex_lock(&epriv->state_lock);
651 
652 	if (!test_bit(MLX5E_STATE_OPENED, &epriv->state))
653 		goto unlock;
654 
655 	clear_bit(MLX5E_STATE_OPENED, &epriv->state);
656 
657 	netif_carrier_off(epriv->netdev);
658 	mlx5_fs_remove_rx_underlay_qpn(mdev, ipriv->qpn);
659 	mlx5e_deactivate_priv_channels(epriv);
660 	mlx5e_close_channels(&epriv->channels);
661 	mlx5i_uninit_underlay_qp(epriv);
662 unlock:
663 	mutex_unlock(&epriv->state_lock);
664 	return 0;
665 }
666 
667 /* IPoIB RDMA netdev callbacks */
mlx5i_attach_mcast(struct net_device * netdev,struct ib_device * hca,union ib_gid * gid,u16 lid,int set_qkey,u32 qkey)668 static int mlx5i_attach_mcast(struct net_device *netdev, struct ib_device *hca,
669 			      union ib_gid *gid, u16 lid, int set_qkey,
670 			      u32 qkey)
671 {
672 	struct mlx5e_priv    *epriv = mlx5i_epriv(netdev);
673 	struct mlx5_core_dev *mdev  = epriv->mdev;
674 	struct mlx5i_priv    *ipriv = epriv->ppriv;
675 	int err;
676 
677 	mlx5_core_dbg(mdev, "attaching QPN 0x%x, MGID %pI6\n", ipriv->qpn,
678 		      gid->raw);
679 	err = mlx5_core_attach_mcg(mdev, gid, ipriv->qpn);
680 	if (err)
681 		mlx5_core_warn(mdev, "failed attaching QPN 0x%x, MGID %pI6\n",
682 			       ipriv->qpn, gid->raw);
683 
684 	if (set_qkey) {
685 		mlx5_core_dbg(mdev, "%s setting qkey 0x%x\n",
686 			      netdev->name, qkey);
687 		ipriv->qkey = qkey;
688 	}
689 
690 	return err;
691 }
692 
mlx5i_detach_mcast(struct net_device * netdev,struct ib_device * hca,union ib_gid * gid,u16 lid)693 static int mlx5i_detach_mcast(struct net_device *netdev, struct ib_device *hca,
694 			      union ib_gid *gid, u16 lid)
695 {
696 	struct mlx5e_priv    *epriv = mlx5i_epriv(netdev);
697 	struct mlx5_core_dev *mdev  = epriv->mdev;
698 	struct mlx5i_priv    *ipriv = epriv->ppriv;
699 	int err;
700 
701 	mlx5_core_dbg(mdev, "detaching QPN 0x%x, MGID %pI6\n", ipriv->qpn,
702 		      gid->raw);
703 
704 	err = mlx5_core_detach_mcg(mdev, gid, ipriv->qpn);
705 	if (err)
706 		mlx5_core_dbg(mdev, "failed detaching QPN 0x%x, MGID %pI6\n",
707 			      ipriv->qpn, gid->raw);
708 
709 	return err;
710 }
711 
mlx5i_xmit(struct net_device * dev,struct sk_buff * skb,struct ib_ah * address,u32 dqpn)712 static int mlx5i_xmit(struct net_device *dev, struct sk_buff *skb,
713 		      struct ib_ah *address, u32 dqpn)
714 {
715 	struct mlx5e_priv *epriv = mlx5i_epriv(dev);
716 	struct mlx5e_txqsq *sq   = epriv->txq2sq[skb_get_queue_mapping(skb)];
717 	struct mlx5_ib_ah *mah   = to_mah(address);
718 	struct mlx5i_priv *ipriv = epriv->ppriv;
719 
720 	mlx5i_sq_xmit(sq, skb, &mah->av, dqpn, ipriv->qkey, netdev_xmit_more());
721 
722 	return NETDEV_TX_OK;
723 }
724 
mlx5i_set_pkey_index(struct net_device * netdev,int id)725 static void mlx5i_set_pkey_index(struct net_device *netdev, int id)
726 {
727 	struct mlx5i_priv *ipriv = netdev_priv(netdev);
728 
729 	ipriv->pkey_index = (u16)id;
730 }
731 
mlx5i_check_required_hca_cap(struct mlx5_core_dev * mdev)732 static int mlx5i_check_required_hca_cap(struct mlx5_core_dev *mdev)
733 {
734 	if (MLX5_CAP_GEN(mdev, port_type) != MLX5_CAP_PORT_TYPE_IB)
735 		return -EOPNOTSUPP;
736 
737 	if (!MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads)) {
738 		mlx5_core_warn(mdev, "IPoIB enhanced offloads are not supported\n");
739 		return -EOPNOTSUPP;
740 	}
741 
742 	return 0;
743 }
744 
mlx5_rdma_netdev_free(struct net_device * netdev)745 static void mlx5_rdma_netdev_free(struct net_device *netdev)
746 {
747 	struct mlx5e_priv *priv = mlx5i_epriv(netdev);
748 	struct mlx5_core_dev *mdev = priv->mdev;
749 	struct mlx5i_priv *ipriv = priv->ppriv;
750 	const struct mlx5e_profile *profile = priv->profile;
751 
752 	mlx5e_detach_netdev(priv);
753 	profile->cleanup(priv);
754 
755 	if (!ipriv->sub_interface) {
756 		mlx5i_pkey_qpn_ht_cleanup(netdev);
757 		mlx5e_destroy_mdev_resources(mdev);
758 	}
759 }
760 
mlx5_is_sub_interface(struct mlx5_core_dev * mdev)761 static bool mlx5_is_sub_interface(struct mlx5_core_dev *mdev)
762 {
763 	return mdev->mlx5e_res.hw_objs.pdn != 0;
764 }
765 
mlx5_get_profile(struct mlx5_core_dev * mdev)766 static const struct mlx5e_profile *mlx5_get_profile(struct mlx5_core_dev *mdev)
767 {
768 	if (mlx5_is_sub_interface(mdev))
769 		return mlx5i_pkey_get_profile();
770 	return &mlx5i_nic_profile;
771 }
772 
mlx5_rdma_setup_rn(struct ib_device * ibdev,u32 port_num,struct net_device * netdev,void * param)773 static int mlx5_rdma_setup_rn(struct ib_device *ibdev, u32 port_num,
774 			      struct net_device *netdev, void *param)
775 {
776 	struct mlx5_core_dev *mdev = (struct mlx5_core_dev *)param;
777 	const struct mlx5e_profile *prof = mlx5_get_profile(mdev);
778 	struct mlx5i_priv *ipriv;
779 	struct mlx5e_priv *epriv;
780 	struct rdma_netdev *rn;
781 	int err;
782 
783 	ipriv = netdev_priv(netdev);
784 	epriv = mlx5i_epriv(netdev);
785 
786 	ipriv->sub_interface = mlx5_is_sub_interface(mdev);
787 	if (!ipriv->sub_interface) {
788 		err = mlx5i_pkey_qpn_ht_init(netdev);
789 		if (err) {
790 			mlx5_core_warn(mdev, "allocate qpn_to_netdev ht failed\n");
791 			return err;
792 		}
793 
794 		/* This should only be called once per mdev */
795 		err = mlx5e_create_mdev_resources(mdev, false);
796 		if (err)
797 			goto destroy_ht;
798 	}
799 
800 	err = mlx5e_priv_init(epriv, prof, netdev, mdev);
801 	if (err)
802 		goto destroy_mdev_resources;
803 
804 	epriv->profile = prof;
805 	epriv->ppriv = ipriv;
806 
807 	prof->init(mdev, netdev);
808 
809 	err = mlx5e_attach_netdev(epriv);
810 	if (err)
811 		goto detach;
812 	netif_carrier_off(netdev);
813 
814 	/* set rdma_netdev func pointers */
815 	rn = &ipriv->rn;
816 	rn->hca  = ibdev;
817 	rn->send = mlx5i_xmit;
818 	rn->attach_mcast = mlx5i_attach_mcast;
819 	rn->detach_mcast = mlx5i_detach_mcast;
820 	rn->set_id = mlx5i_set_pkey_index;
821 
822 	netdev->priv_destructor = mlx5_rdma_netdev_free;
823 	netdev->needs_free_netdev = 1;
824 
825 	return 0;
826 
827 detach:
828 	prof->cleanup(epriv);
829 	if (ipriv->sub_interface)
830 		return err;
831 destroy_mdev_resources:
832 	mlx5e_destroy_mdev_resources(mdev);
833 destroy_ht:
834 	mlx5i_pkey_qpn_ht_cleanup(netdev);
835 	return err;
836 }
837 
mlx5_rdma_rn_get_params(struct mlx5_core_dev * mdev,struct ib_device * device,struct rdma_netdev_alloc_params * params)838 int mlx5_rdma_rn_get_params(struct mlx5_core_dev *mdev,
839 			    struct ib_device *device,
840 			    struct rdma_netdev_alloc_params *params)
841 {
842 	int nch;
843 	int rc;
844 
845 	rc = mlx5i_check_required_hca_cap(mdev);
846 	if (rc)
847 		return rc;
848 
849 	nch = mlx5e_get_max_num_channels(mdev);
850 
851 	*params = (struct rdma_netdev_alloc_params){
852 		.sizeof_priv = sizeof(struct mlx5i_priv) +
853 			       sizeof(struct mlx5e_priv),
854 		.txqs = nch * MLX5_MAX_NUM_TC,
855 		.rxqs = nch,
856 		.param = mdev,
857 		.initialize_rdma_netdev = mlx5_rdma_setup_rn,
858 	};
859 
860 	return 0;
861 }
862 EXPORT_SYMBOL(mlx5_rdma_rn_get_params);
863