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 "en.h"
36 #include "en/params.h"
37 #include "ipoib.h"
38
39 #define IB_DEFAULT_Q_KEY 0xb1b
40 #define MLX5I_PARAMS_DEFAULT_LOG_RQ_SIZE 9
41
42 static int mlx5i_open(struct net_device *netdev);
43 static int mlx5i_close(struct net_device *netdev);
44 static int mlx5i_change_mtu(struct net_device *netdev, int new_mtu);
45
46 static const struct net_device_ops mlx5i_netdev_ops = {
47 .ndo_open = mlx5i_open,
48 .ndo_stop = mlx5i_close,
49 .ndo_get_stats64 = mlx5i_get_stats,
50 .ndo_init = mlx5i_dev_init,
51 .ndo_uninit = mlx5i_dev_cleanup,
52 .ndo_change_mtu = mlx5i_change_mtu,
53 .ndo_eth_ioctl = mlx5i_ioctl,
54 };
55
56 /* IPoIB mlx5 netdev profile */
mlx5i_build_nic_params(struct mlx5_core_dev * mdev,struct mlx5e_params * params)57 static void mlx5i_build_nic_params(struct mlx5_core_dev *mdev,
58 struct mlx5e_params *params)
59 {
60 /* Override RQ params as IPoIB supports only LINKED LIST RQ for now */
61 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_STRIDING_RQ, false);
62 mlx5e_set_rq_type(mdev, params);
63 mlx5e_init_rq_type_params(mdev, params);
64
65 /* RQ size in ipoib by default is 512 */
66 params->log_rq_mtu_frames = is_kdump_kernel() ?
67 MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE :
68 MLX5I_PARAMS_DEFAULT_LOG_RQ_SIZE;
69
70 params->packet_merge.type = MLX5E_PACKET_MERGE_NONE;
71 params->hard_mtu = MLX5_IB_GRH_BYTES + MLX5_IPOIB_HARD_LEN;
72 params->tunneled_offload_en = false;
73
74 /* CQE compression is not supported for IPoIB */
75 params->rx_cqe_compress_def = false;
76 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS, params->rx_cqe_compress_def);
77 }
78
79 /* Called directly after IPoIB netdevice was created to initialize SW structs */
mlx5i_init(struct mlx5_core_dev * mdev,struct net_device * netdev)80 int mlx5i_init(struct mlx5_core_dev *mdev, struct net_device *netdev)
81 {
82 struct mlx5e_priv *priv = mlx5i_epriv(netdev);
83
84 netif_carrier_off(netdev);
85 mlx5e_set_netdev_mtu_boundaries(priv);
86 netdev->mtu = netdev->max_mtu;
87
88 mlx5e_build_nic_params(priv, NULL, netdev->mtu);
89 mlx5i_build_nic_params(mdev, &priv->channels.params);
90
91 mlx5e_timestamp_init(priv);
92
93 /* netdev init */
94 netdev->hw_features |= NETIF_F_SG;
95 netdev->hw_features |= NETIF_F_IP_CSUM;
96 netdev->hw_features |= NETIF_F_IPV6_CSUM;
97 netdev->hw_features |= NETIF_F_GRO;
98 netdev->hw_features |= NETIF_F_TSO;
99 netdev->hw_features |= NETIF_F_TSO6;
100 netdev->hw_features |= NETIF_F_RXCSUM;
101 netdev->hw_features |= NETIF_F_RXHASH;
102
103 netdev->netdev_ops = &mlx5i_netdev_ops;
104 netdev->ethtool_ops = &mlx5i_ethtool_ops;
105
106 return 0;
107 }
108
109 /* Called directly before IPoIB netdevice is destroyed to cleanup SW structs */
mlx5i_cleanup(struct mlx5e_priv * priv)110 void mlx5i_cleanup(struct mlx5e_priv *priv)
111 {
112 mlx5e_priv_cleanup(priv);
113 }
114
mlx5i_grp_sw_update_stats(struct mlx5e_priv * priv)115 static void mlx5i_grp_sw_update_stats(struct mlx5e_priv *priv)
116 {
117 struct mlx5e_sw_stats s = { 0 };
118 int i, j;
119
120 for (i = 0; i < priv->stats_nch; i++) {
121 struct mlx5e_channel_stats *channel_stats;
122 struct mlx5e_rq_stats *rq_stats;
123
124 channel_stats = &priv->channel_stats[i];
125 rq_stats = &channel_stats->rq;
126
127 s.rx_packets += rq_stats->packets;
128 s.rx_bytes += rq_stats->bytes;
129
130 for (j = 0; j < priv->max_opened_tc; j++) {
131 struct mlx5e_sq_stats *sq_stats = &channel_stats->sq[j];
132
133 s.tx_packets += sq_stats->packets;
134 s.tx_bytes += sq_stats->bytes;
135 s.tx_queue_dropped += sq_stats->dropped;
136 }
137 }
138
139 memcpy(&priv->stats.sw, &s, sizeof(s));
140 }
141
mlx5i_get_stats(struct net_device * dev,struct rtnl_link_stats64 * stats)142 void mlx5i_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
143 {
144 struct mlx5e_priv *priv = mlx5i_epriv(dev);
145 struct mlx5e_sw_stats *sstats = &priv->stats.sw;
146
147 mlx5i_grp_sw_update_stats(priv);
148
149 stats->rx_packets = sstats->rx_packets;
150 stats->rx_bytes = sstats->rx_bytes;
151 stats->tx_packets = sstats->tx_packets;
152 stats->tx_bytes = sstats->tx_bytes;
153 stats->tx_dropped = sstats->tx_queue_dropped;
154 }
155
mlx5i_init_underlay_qp(struct mlx5e_priv * priv)156 int mlx5i_init_underlay_qp(struct mlx5e_priv *priv)
157 {
158 struct mlx5_core_dev *mdev = priv->mdev;
159 struct mlx5i_priv *ipriv = priv->ppriv;
160 int ret;
161
162 {
163 u32 in[MLX5_ST_SZ_DW(rst2init_qp_in)] = {};
164 u32 *qpc;
165
166 qpc = MLX5_ADDR_OF(rst2init_qp_in, in, qpc);
167
168 MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_MIGRATED);
169 MLX5_SET(qpc, qpc, primary_address_path.pkey_index,
170 ipriv->pkey_index);
171 MLX5_SET(qpc, qpc, primary_address_path.vhca_port_num, 1);
172 MLX5_SET(qpc, qpc, q_key, IB_DEFAULT_Q_KEY);
173
174 MLX5_SET(rst2init_qp_in, in, opcode, MLX5_CMD_OP_RST2INIT_QP);
175 MLX5_SET(rst2init_qp_in, in, qpn, ipriv->qpn);
176 ret = mlx5_cmd_exec_in(mdev, rst2init_qp, in);
177 if (ret)
178 goto err_qp_modify_to_err;
179 }
180 {
181 u32 in[MLX5_ST_SZ_DW(init2rtr_qp_in)] = {};
182
183 MLX5_SET(init2rtr_qp_in, in, opcode, MLX5_CMD_OP_INIT2RTR_QP);
184 MLX5_SET(init2rtr_qp_in, in, qpn, ipriv->qpn);
185 ret = mlx5_cmd_exec_in(mdev, init2rtr_qp, in);
186 if (ret)
187 goto err_qp_modify_to_err;
188 }
189 {
190 u32 in[MLX5_ST_SZ_DW(rtr2rts_qp_in)] = {};
191
192 MLX5_SET(rtr2rts_qp_in, in, opcode, MLX5_CMD_OP_RTR2RTS_QP);
193 MLX5_SET(rtr2rts_qp_in, in, qpn, ipriv->qpn);
194 ret = mlx5_cmd_exec_in(mdev, rtr2rts_qp, in);
195 if (ret)
196 goto err_qp_modify_to_err;
197 }
198 return 0;
199
200 err_qp_modify_to_err:
201 {
202 u32 in[MLX5_ST_SZ_DW(qp_2err_in)] = {};
203
204 MLX5_SET(qp_2err_in, in, opcode, MLX5_CMD_OP_2ERR_QP);
205 MLX5_SET(qp_2err_in, in, qpn, ipriv->qpn);
206 mlx5_cmd_exec_in(mdev, qp_2err, in);
207 }
208 return ret;
209 }
210
mlx5i_uninit_underlay_qp(struct mlx5e_priv * priv)211 void mlx5i_uninit_underlay_qp(struct mlx5e_priv *priv)
212 {
213 struct mlx5i_priv *ipriv = priv->ppriv;
214 struct mlx5_core_dev *mdev = priv->mdev;
215 u32 in[MLX5_ST_SZ_DW(qp_2rst_in)] = {};
216
217 MLX5_SET(qp_2rst_in, in, opcode, MLX5_CMD_OP_2RST_QP);
218 MLX5_SET(qp_2rst_in, in, qpn, ipriv->qpn);
219 mlx5_cmd_exec_in(mdev, qp_2rst, in);
220 }
221
222 #define MLX5_QP_ENHANCED_ULP_STATELESS_MODE 2
223
mlx5i_create_underlay_qp(struct mlx5e_priv * priv)224 int mlx5i_create_underlay_qp(struct mlx5e_priv *priv)
225 {
226 const unsigned char *dev_addr = priv->netdev->dev_addr;
227 u32 out[MLX5_ST_SZ_DW(create_qp_out)] = {};
228 u32 in[MLX5_ST_SZ_DW(create_qp_in)] = {};
229 struct mlx5i_priv *ipriv = priv->ppriv;
230 void *addr_path;
231 int qpn = 0;
232 int ret = 0;
233 void *qpc;
234
235 if (MLX5_CAP_GEN(priv->mdev, mkey_by_name)) {
236 qpn = (dev_addr[1] << 16) + (dev_addr[2] << 8) + dev_addr[3];
237 MLX5_SET(create_qp_in, in, input_qpn, qpn);
238 }
239
240 qpc = MLX5_ADDR_OF(create_qp_in, in, qpc);
241 MLX5_SET(qpc, qpc, ts_format, mlx5_get_qp_default_ts(priv->mdev));
242 MLX5_SET(qpc, qpc, st, MLX5_QP_ST_UD);
243 MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_MIGRATED);
244 MLX5_SET(qpc, qpc, ulp_stateless_offload_mode,
245 MLX5_QP_ENHANCED_ULP_STATELESS_MODE);
246
247 addr_path = MLX5_ADDR_OF(qpc, qpc, primary_address_path);
248 MLX5_SET(ads, addr_path, vhca_port_num, 1);
249 MLX5_SET(ads, addr_path, grh, 1);
250
251 MLX5_SET(create_qp_in, in, opcode, MLX5_CMD_OP_CREATE_QP);
252 ret = mlx5_cmd_exec_inout(priv->mdev, create_qp, in, out);
253 if (ret)
254 return ret;
255
256 ipriv->qpn = MLX5_GET(create_qp_out, out, qpn);
257
258 return 0;
259 }
260
mlx5i_destroy_underlay_qp(struct mlx5_core_dev * mdev,u32 qpn)261 void mlx5i_destroy_underlay_qp(struct mlx5_core_dev *mdev, u32 qpn)
262 {
263 u32 in[MLX5_ST_SZ_DW(destroy_qp_in)] = {};
264
265 MLX5_SET(destroy_qp_in, in, opcode, MLX5_CMD_OP_DESTROY_QP);
266 MLX5_SET(destroy_qp_in, in, qpn, qpn);
267 mlx5_cmd_exec_in(mdev, destroy_qp, in);
268 }
269
mlx5i_update_nic_rx(struct mlx5e_priv * priv)270 int mlx5i_update_nic_rx(struct mlx5e_priv *priv)
271 {
272 return mlx5e_refresh_tirs(priv, true, true);
273 }
274
mlx5i_create_tis(struct mlx5_core_dev * mdev,u32 underlay_qpn,u32 * tisn)275 int mlx5i_create_tis(struct mlx5_core_dev *mdev, u32 underlay_qpn, u32 *tisn)
276 {
277 u32 in[MLX5_ST_SZ_DW(create_tis_in)] = {};
278 void *tisc;
279
280 tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
281
282 MLX5_SET(tisc, tisc, underlay_qpn, underlay_qpn);
283
284 return mlx5e_create_tis(mdev, in, tisn);
285 }
286
mlx5i_init_tx(struct mlx5e_priv * priv)287 static int mlx5i_init_tx(struct mlx5e_priv *priv)
288 {
289 struct mlx5i_priv *ipriv = priv->ppriv;
290 int err;
291
292 err = mlx5i_create_underlay_qp(priv);
293 if (err) {
294 mlx5_core_warn(priv->mdev, "create underlay QP failed, %d\n", err);
295 return err;
296 }
297
298 err = mlx5i_create_tis(priv->mdev, ipriv->qpn, &priv->tisn[0][0]);
299 if (err) {
300 mlx5_core_warn(priv->mdev, "create tis failed, %d\n", err);
301 goto err_destroy_underlay_qp;
302 }
303
304 return 0;
305
306 err_destroy_underlay_qp:
307 mlx5i_destroy_underlay_qp(priv->mdev, ipriv->qpn);
308 return err;
309 }
310
mlx5i_cleanup_tx(struct mlx5e_priv * priv)311 static void mlx5i_cleanup_tx(struct mlx5e_priv *priv)
312 {
313 struct mlx5i_priv *ipriv = priv->ppriv;
314
315 mlx5e_destroy_tis(priv->mdev, priv->tisn[0][0]);
316 mlx5i_destroy_underlay_qp(priv->mdev, ipriv->qpn);
317 }
318
mlx5i_create_flow_steering(struct mlx5e_priv * priv)319 static int mlx5i_create_flow_steering(struct mlx5e_priv *priv)
320 {
321 int err;
322
323 priv->fs.ns = mlx5_get_flow_namespace(priv->mdev,
324 MLX5_FLOW_NAMESPACE_KERNEL);
325
326 if (!priv->fs.ns)
327 return -EINVAL;
328
329 err = mlx5e_arfs_create_tables(priv);
330 if (err) {
331 netdev_err(priv->netdev, "Failed to create arfs tables, err=%d\n",
332 err);
333 priv->netdev->hw_features &= ~NETIF_F_NTUPLE;
334 }
335
336 err = mlx5e_create_ttc_table(priv);
337 if (err) {
338 netdev_err(priv->netdev, "Failed to create ttc table, err=%d\n",
339 err);
340 goto err_destroy_arfs_tables;
341 }
342
343 return 0;
344
345 err_destroy_arfs_tables:
346 mlx5e_arfs_destroy_tables(priv);
347
348 return err;
349 }
350
mlx5i_destroy_flow_steering(struct mlx5e_priv * priv)351 static void mlx5i_destroy_flow_steering(struct mlx5e_priv *priv)
352 {
353 mlx5e_destroy_ttc_table(priv);
354 mlx5e_arfs_destroy_tables(priv);
355 }
356
mlx5i_init_rx(struct mlx5e_priv * priv)357 static int mlx5i_init_rx(struct mlx5e_priv *priv)
358 {
359 struct mlx5_core_dev *mdev = priv->mdev;
360 int err;
361
362 priv->rx_res = mlx5e_rx_res_alloc();
363 if (!priv->rx_res)
364 return -ENOMEM;
365
366 mlx5e_create_q_counters(priv);
367
368 err = mlx5e_open_drop_rq(priv, &priv->drop_rq);
369 if (err) {
370 mlx5_core_err(mdev, "open drop rq failed, %d\n", err);
371 goto err_destroy_q_counters;
372 }
373
374 err = mlx5e_rx_res_init(priv->rx_res, priv->mdev, 0,
375 priv->max_nch, priv->drop_rq.rqn,
376 &priv->channels.params.packet_merge,
377 priv->channels.params.num_channels);
378 if (err)
379 goto err_close_drop_rq;
380
381 err = mlx5i_create_flow_steering(priv);
382 if (err)
383 goto err_destroy_rx_res;
384
385 return 0;
386
387 err_destroy_rx_res:
388 mlx5e_rx_res_destroy(priv->rx_res);
389 err_close_drop_rq:
390 mlx5e_close_drop_rq(&priv->drop_rq);
391 err_destroy_q_counters:
392 mlx5e_destroy_q_counters(priv);
393 mlx5e_rx_res_free(priv->rx_res);
394 priv->rx_res = NULL;
395 return err;
396 }
397
mlx5i_cleanup_rx(struct mlx5e_priv * priv)398 static void mlx5i_cleanup_rx(struct mlx5e_priv *priv)
399 {
400 mlx5i_destroy_flow_steering(priv);
401 mlx5e_rx_res_destroy(priv->rx_res);
402 mlx5e_close_drop_rq(&priv->drop_rq);
403 mlx5e_destroy_q_counters(priv);
404 mlx5e_rx_res_free(priv->rx_res);
405 priv->rx_res = NULL;
406 }
407
408 /* The stats groups order is opposite to the update_stats() order calls */
409 static mlx5e_stats_grp_t mlx5i_stats_grps[] = {
410 &MLX5E_STATS_GRP(sw),
411 &MLX5E_STATS_GRP(qcnt),
412 &MLX5E_STATS_GRP(vnic_env),
413 &MLX5E_STATS_GRP(vport),
414 &MLX5E_STATS_GRP(802_3),
415 &MLX5E_STATS_GRP(2863),
416 &MLX5E_STATS_GRP(2819),
417 &MLX5E_STATS_GRP(phy),
418 &MLX5E_STATS_GRP(pcie),
419 &MLX5E_STATS_GRP(per_prio),
420 &MLX5E_STATS_GRP(pme),
421 &MLX5E_STATS_GRP(channels),
422 &MLX5E_STATS_GRP(per_port_buff_congest),
423 };
424
mlx5i_stats_grps_num(struct mlx5e_priv * priv)425 static unsigned int mlx5i_stats_grps_num(struct mlx5e_priv *priv)
426 {
427 return ARRAY_SIZE(mlx5i_stats_grps);
428 }
429
430 static const struct mlx5e_profile mlx5i_nic_profile = {
431 .init = mlx5i_init,
432 .cleanup = mlx5i_cleanup,
433 .init_tx = mlx5i_init_tx,
434 .cleanup_tx = mlx5i_cleanup_tx,
435 .init_rx = mlx5i_init_rx,
436 .cleanup_rx = mlx5i_cleanup_rx,
437 .enable = NULL, /* mlx5i_enable */
438 .disable = NULL, /* mlx5i_disable */
439 .update_rx = mlx5i_update_nic_rx,
440 .update_stats = NULL, /* mlx5i_update_stats */
441 .update_carrier = NULL, /* no HW update in IB link */
442 .rx_handlers = &mlx5i_rx_handlers,
443 .max_tc = MLX5I_MAX_NUM_TC,
444 .rq_groups = MLX5E_NUM_RQ_GROUPS(REGULAR),
445 .stats_grps = mlx5i_stats_grps,
446 .stats_grps_num = mlx5i_stats_grps_num,
447 .rx_ptp_support = false,
448 };
449
450 /* mlx5i netdev NDos */
451
mlx5i_change_mtu(struct net_device * netdev,int new_mtu)452 static int mlx5i_change_mtu(struct net_device *netdev, int new_mtu)
453 {
454 struct mlx5e_priv *priv = mlx5i_epriv(netdev);
455 struct mlx5e_params new_params;
456 int err = 0;
457
458 mutex_lock(&priv->state_lock);
459
460 new_params = priv->channels.params;
461 new_params.sw_mtu = new_mtu;
462
463 err = mlx5e_safe_switch_params(priv, &new_params, NULL, NULL, true);
464 if (err)
465 goto out;
466
467 netdev->mtu = new_params.sw_mtu;
468
469 out:
470 mutex_unlock(&priv->state_lock);
471 return err;
472 }
473
mlx5i_dev_init(struct net_device * dev)474 int mlx5i_dev_init(struct net_device *dev)
475 {
476 struct mlx5e_priv *priv = mlx5i_epriv(dev);
477 struct mlx5i_priv *ipriv = priv->ppriv;
478
479 /* Set dev address using underlay QP */
480 dev->dev_addr[1] = (ipriv->qpn >> 16) & 0xff;
481 dev->dev_addr[2] = (ipriv->qpn >> 8) & 0xff;
482 dev->dev_addr[3] = (ipriv->qpn) & 0xff;
483
484 /* Add QPN to net-device mapping to HT */
485 mlx5i_pkey_add_qpn(dev, ipriv->qpn);
486
487 return 0;
488 }
489
mlx5i_ioctl(struct net_device * dev,struct ifreq * ifr,int cmd)490 int mlx5i_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
491 {
492 struct mlx5e_priv *priv = mlx5i_epriv(dev);
493
494 switch (cmd) {
495 case SIOCSHWTSTAMP:
496 return mlx5e_hwstamp_set(priv, ifr);
497 case SIOCGHWTSTAMP:
498 return mlx5e_hwstamp_get(priv, ifr);
499 default:
500 return -EOPNOTSUPP;
501 }
502 }
503
mlx5i_dev_cleanup(struct net_device * dev)504 void mlx5i_dev_cleanup(struct net_device *dev)
505 {
506 struct mlx5e_priv *priv = mlx5i_epriv(dev);
507 struct mlx5i_priv *ipriv = priv->ppriv;
508
509 mlx5i_uninit_underlay_qp(priv);
510
511 /* Delete QPN to net-device mapping from HT */
512 mlx5i_pkey_del_qpn(dev, ipriv->qpn);
513 }
514
mlx5i_open(struct net_device * netdev)515 static int mlx5i_open(struct net_device *netdev)
516 {
517 struct mlx5e_priv *epriv = mlx5i_epriv(netdev);
518 struct mlx5i_priv *ipriv = epriv->ppriv;
519 struct mlx5_core_dev *mdev = epriv->mdev;
520 int err;
521
522 mutex_lock(&epriv->state_lock);
523
524 set_bit(MLX5E_STATE_OPENED, &epriv->state);
525
526 err = mlx5i_init_underlay_qp(epriv);
527 if (err) {
528 mlx5_core_warn(mdev, "prepare underlay qp state failed, %d\n", err);
529 goto err_clear_state_opened_flag;
530 }
531
532 err = mlx5_fs_add_rx_underlay_qpn(mdev, ipriv->qpn);
533 if (err) {
534 mlx5_core_warn(mdev, "attach underlay qp to ft failed, %d\n", err);
535 goto err_reset_qp;
536 }
537
538 err = mlx5e_open_channels(epriv, &epriv->channels);
539 if (err)
540 goto err_remove_fs_underlay_qp;
541
542 epriv->profile->update_rx(epriv);
543 mlx5e_activate_priv_channels(epriv);
544
545 mutex_unlock(&epriv->state_lock);
546 return 0;
547
548 err_remove_fs_underlay_qp:
549 mlx5_fs_remove_rx_underlay_qpn(mdev, ipriv->qpn);
550 err_reset_qp:
551 mlx5i_uninit_underlay_qp(epriv);
552 err_clear_state_opened_flag:
553 clear_bit(MLX5E_STATE_OPENED, &epriv->state);
554 mutex_unlock(&epriv->state_lock);
555 return err;
556 }
557
mlx5i_close(struct net_device * netdev)558 static int mlx5i_close(struct net_device *netdev)
559 {
560 struct mlx5e_priv *epriv = mlx5i_epriv(netdev);
561 struct mlx5i_priv *ipriv = epriv->ppriv;
562 struct mlx5_core_dev *mdev = epriv->mdev;
563
564 /* May already be CLOSED in case a previous configuration operation
565 * (e.g RX/TX queue size change) that involves close&open failed.
566 */
567 mutex_lock(&epriv->state_lock);
568
569 if (!test_bit(MLX5E_STATE_OPENED, &epriv->state))
570 goto unlock;
571
572 clear_bit(MLX5E_STATE_OPENED, &epriv->state);
573
574 netif_carrier_off(epriv->netdev);
575 mlx5_fs_remove_rx_underlay_qpn(mdev, ipriv->qpn);
576 mlx5e_deactivate_priv_channels(epriv);
577 mlx5e_close_channels(&epriv->channels);
578 mlx5i_uninit_underlay_qp(epriv);
579 unlock:
580 mutex_unlock(&epriv->state_lock);
581 return 0;
582 }
583
584 /* 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)585 static int mlx5i_attach_mcast(struct net_device *netdev, struct ib_device *hca,
586 union ib_gid *gid, u16 lid, int set_qkey,
587 u32 qkey)
588 {
589 struct mlx5e_priv *epriv = mlx5i_epriv(netdev);
590 struct mlx5_core_dev *mdev = epriv->mdev;
591 struct mlx5i_priv *ipriv = epriv->ppriv;
592 int err;
593
594 mlx5_core_dbg(mdev, "attaching QPN 0x%x, MGID %pI6\n", ipriv->qpn,
595 gid->raw);
596 err = mlx5_core_attach_mcg(mdev, gid, ipriv->qpn);
597 if (err)
598 mlx5_core_warn(mdev, "failed attaching QPN 0x%x, MGID %pI6\n",
599 ipriv->qpn, gid->raw);
600
601 if (set_qkey) {
602 mlx5_core_dbg(mdev, "%s setting qkey 0x%x\n",
603 netdev->name, qkey);
604 ipriv->qkey = qkey;
605 }
606
607 return err;
608 }
609
mlx5i_detach_mcast(struct net_device * netdev,struct ib_device * hca,union ib_gid * gid,u16 lid)610 static int mlx5i_detach_mcast(struct net_device *netdev, struct ib_device *hca,
611 union ib_gid *gid, u16 lid)
612 {
613 struct mlx5e_priv *epriv = mlx5i_epriv(netdev);
614 struct mlx5_core_dev *mdev = epriv->mdev;
615 struct mlx5i_priv *ipriv = epriv->ppriv;
616 int err;
617
618 mlx5_core_dbg(mdev, "detaching QPN 0x%x, MGID %pI6\n", ipriv->qpn,
619 gid->raw);
620
621 err = mlx5_core_detach_mcg(mdev, gid, ipriv->qpn);
622 if (err)
623 mlx5_core_dbg(mdev, "failed detaching QPN 0x%x, MGID %pI6\n",
624 ipriv->qpn, gid->raw);
625
626 return err;
627 }
628
mlx5i_xmit(struct net_device * dev,struct sk_buff * skb,struct ib_ah * address,u32 dqpn)629 static int mlx5i_xmit(struct net_device *dev, struct sk_buff *skb,
630 struct ib_ah *address, u32 dqpn)
631 {
632 struct mlx5e_priv *epriv = mlx5i_epriv(dev);
633 struct mlx5e_txqsq *sq = epriv->txq2sq[skb_get_queue_mapping(skb)];
634 struct mlx5_ib_ah *mah = to_mah(address);
635 struct mlx5i_priv *ipriv = epriv->ppriv;
636
637 mlx5i_sq_xmit(sq, skb, &mah->av, dqpn, ipriv->qkey, netdev_xmit_more());
638
639 return NETDEV_TX_OK;
640 }
641
mlx5i_set_pkey_index(struct net_device * netdev,int id)642 static void mlx5i_set_pkey_index(struct net_device *netdev, int id)
643 {
644 struct mlx5i_priv *ipriv = netdev_priv(netdev);
645
646 ipriv->pkey_index = (u16)id;
647 }
648
mlx5i_check_required_hca_cap(struct mlx5_core_dev * mdev)649 static int mlx5i_check_required_hca_cap(struct mlx5_core_dev *mdev)
650 {
651 if (MLX5_CAP_GEN(mdev, port_type) != MLX5_CAP_PORT_TYPE_IB)
652 return -EOPNOTSUPP;
653
654 if (!MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads)) {
655 mlx5_core_warn(mdev, "IPoIB enhanced offloads are not supported\n");
656 return -EOPNOTSUPP;
657 }
658
659 return 0;
660 }
661
mlx5_rdma_netdev_free(struct net_device * netdev)662 static void mlx5_rdma_netdev_free(struct net_device *netdev)
663 {
664 struct mlx5e_priv *priv = mlx5i_epriv(netdev);
665 struct mlx5_core_dev *mdev = priv->mdev;
666 struct mlx5i_priv *ipriv = priv->ppriv;
667 const struct mlx5e_profile *profile = priv->profile;
668
669 mlx5e_detach_netdev(priv);
670 profile->cleanup(priv);
671
672 if (!ipriv->sub_interface) {
673 mlx5i_pkey_qpn_ht_cleanup(netdev);
674 mlx5e_destroy_mdev_resources(mdev);
675 }
676 }
677
mlx5_is_sub_interface(struct mlx5_core_dev * mdev)678 static bool mlx5_is_sub_interface(struct mlx5_core_dev *mdev)
679 {
680 return mdev->mlx5e_res.hw_objs.pdn != 0;
681 }
682
mlx5_get_profile(struct mlx5_core_dev * mdev)683 static const struct mlx5e_profile *mlx5_get_profile(struct mlx5_core_dev *mdev)
684 {
685 if (mlx5_is_sub_interface(mdev))
686 return mlx5i_pkey_get_profile();
687 return &mlx5i_nic_profile;
688 }
689
mlx5_rdma_setup_rn(struct ib_device * ibdev,u32 port_num,struct net_device * netdev,void * param)690 static int mlx5_rdma_setup_rn(struct ib_device *ibdev, u32 port_num,
691 struct net_device *netdev, void *param)
692 {
693 struct mlx5_core_dev *mdev = (struct mlx5_core_dev *)param;
694 const struct mlx5e_profile *prof = mlx5_get_profile(mdev);
695 struct mlx5i_priv *ipriv;
696 struct mlx5e_priv *epriv;
697 struct rdma_netdev *rn;
698 int err;
699
700 ipriv = netdev_priv(netdev);
701 epriv = mlx5i_epriv(netdev);
702
703 ipriv->sub_interface = mlx5_is_sub_interface(mdev);
704 if (!ipriv->sub_interface) {
705 err = mlx5i_pkey_qpn_ht_init(netdev);
706 if (err) {
707 mlx5_core_warn(mdev, "allocate qpn_to_netdev ht failed\n");
708 return err;
709 }
710
711 /* This should only be called once per mdev */
712 err = mlx5e_create_mdev_resources(mdev);
713 if (err)
714 goto destroy_ht;
715 }
716
717 err = mlx5e_priv_init(epriv, prof, netdev, mdev);
718 if (err)
719 goto destroy_mdev_resources;
720
721 epriv->profile = prof;
722 epriv->ppriv = ipriv;
723
724 prof->init(mdev, netdev);
725
726 err = mlx5e_attach_netdev(epriv);
727 if (err)
728 goto detach;
729 netif_carrier_off(netdev);
730
731 /* set rdma_netdev func pointers */
732 rn = &ipriv->rn;
733 rn->hca = ibdev;
734 rn->send = mlx5i_xmit;
735 rn->attach_mcast = mlx5i_attach_mcast;
736 rn->detach_mcast = mlx5i_detach_mcast;
737 rn->set_id = mlx5i_set_pkey_index;
738
739 netdev->priv_destructor = mlx5_rdma_netdev_free;
740 netdev->needs_free_netdev = 1;
741
742 return 0;
743
744 detach:
745 prof->cleanup(epriv);
746 if (ipriv->sub_interface)
747 return err;
748 destroy_mdev_resources:
749 mlx5e_destroy_mdev_resources(mdev);
750 destroy_ht:
751 mlx5i_pkey_qpn_ht_cleanup(netdev);
752 return err;
753 }
754
mlx5_rdma_rn_get_params(struct mlx5_core_dev * mdev,struct ib_device * device,struct rdma_netdev_alloc_params * params)755 int mlx5_rdma_rn_get_params(struct mlx5_core_dev *mdev,
756 struct ib_device *device,
757 struct rdma_netdev_alloc_params *params)
758 {
759 int nch;
760 int rc;
761
762 rc = mlx5i_check_required_hca_cap(mdev);
763 if (rc)
764 return rc;
765
766 nch = mlx5e_get_max_num_channels(mdev);
767
768 *params = (struct rdma_netdev_alloc_params){
769 .sizeof_priv = sizeof(struct mlx5i_priv) +
770 sizeof(struct mlx5e_priv),
771 .txqs = nch * MLX5E_MAX_NUM_TC,
772 .rxqs = nch,
773 .param = mdev,
774 .initialize_rdma_netdev = mlx5_rdma_setup_rn,
775 };
776
777 return 0;
778 }
779 EXPORT_SYMBOL(mlx5_rdma_rn_get_params);
780