1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
3 * Copyright (c) 2018 Mellanox Technologies. All rights reserved.
8 static const struct mlx5_ib_profile rep_profile = {
9 STAGE_CREATE(MLX5_IB_STAGE_INIT,
10 mlx5_ib_stage_init_init,
11 mlx5_ib_stage_init_cleanup),
12 STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
13 mlx5_ib_stage_rep_flow_db_init,
15 STAGE_CREATE(MLX5_IB_STAGE_CAPS,
16 mlx5_ib_stage_caps_init,
18 STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
19 mlx5_ib_stage_rep_non_default_cb,
21 STAGE_CREATE(MLX5_IB_STAGE_ROCE,
22 mlx5_ib_stage_rep_roce_init,
23 mlx5_ib_stage_rep_roce_cleanup),
24 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
25 mlx5_ib_stage_dev_res_init,
26 mlx5_ib_stage_dev_res_cleanup),
27 STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
28 mlx5_ib_stage_counters_init,
29 mlx5_ib_stage_counters_cleanup),
30 STAGE_CREATE(MLX5_IB_STAGE_BFREG,
31 mlx5_ib_stage_bfrag_init,
32 mlx5_ib_stage_bfrag_cleanup),
33 STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
35 mlx5_ib_stage_pre_ib_reg_umr_cleanup),
36 STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
37 mlx5_ib_stage_ib_reg_init,
38 mlx5_ib_stage_ib_reg_cleanup),
39 STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
40 mlx5_ib_stage_post_ib_reg_umr_init,
42 STAGE_CREATE(MLX5_IB_STAGE_CLASS_ATTR,
43 mlx5_ib_stage_class_attr_init,
48 mlx5_ib_nic_rep_load(struct mlx5_core_dev *dev, struct mlx5_eswitch_rep *rep)
54 mlx5_ib_nic_rep_unload(struct mlx5_eswitch_rep *rep)
56 rep->rep_if[REP_IB].priv = NULL;
60 mlx5_ib_vport_rep_load(struct mlx5_core_dev *dev, struct mlx5_eswitch_rep *rep)
62 struct mlx5_ib_dev *ibdev;
64 ibdev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*ibdev));
70 ibdev->num_ports = max(MLX5_CAP_GEN(dev, num_ports),
71 MLX5_CAP_GEN(dev, num_vhca_ports));
72 if (!__mlx5_ib_add(ibdev, &rep_profile)) {
73 ib_dealloc_device(&ibdev->ib_dev);
77 rep->rep_if[REP_IB].priv = ibdev;
83 mlx5_ib_vport_rep_unload(struct mlx5_eswitch_rep *rep)
85 struct mlx5_ib_dev *dev;
87 if (!rep->rep_if[REP_IB].priv)
90 dev = mlx5_ib_rep_to_dev(rep);
91 __mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX);
92 rep->rep_if[REP_IB].priv = NULL;
95 static void *mlx5_ib_vport_get_proto_dev(struct mlx5_eswitch_rep *rep)
97 return mlx5_ib_rep_to_dev(rep);
100 static void mlx5_ib_rep_register_vf_vports(struct mlx5_ib_dev *dev)
102 struct mlx5_eswitch *esw = dev->mdev->priv.eswitch;
103 int total_vfs = MLX5_TOTAL_VPORTS(dev->mdev);
106 for (vport = 1; vport < total_vfs; vport++) {
107 struct mlx5_eswitch_rep_if rep_if = {};
109 rep_if.load = mlx5_ib_vport_rep_load;
110 rep_if.unload = mlx5_ib_vport_rep_unload;
111 rep_if.get_proto_dev = mlx5_ib_vport_get_proto_dev;
112 mlx5_eswitch_register_vport_rep(esw, vport, &rep_if, REP_IB);
116 static void mlx5_ib_rep_unregister_vf_vports(struct mlx5_ib_dev *dev)
118 struct mlx5_eswitch *esw = dev->mdev->priv.eswitch;
119 int total_vfs = MLX5_TOTAL_VPORTS(dev->mdev);
122 for (vport = 1; vport < total_vfs; vport++)
123 mlx5_eswitch_unregister_vport_rep(esw, vport, REP_IB);
126 void mlx5_ib_register_vport_reps(struct mlx5_ib_dev *dev)
128 struct mlx5_eswitch *esw = dev->mdev->priv.eswitch;
129 struct mlx5_eswitch_rep_if rep_if = {};
131 rep_if.load = mlx5_ib_nic_rep_load;
132 rep_if.unload = mlx5_ib_nic_rep_unload;
133 rep_if.get_proto_dev = mlx5_ib_vport_get_proto_dev;
136 mlx5_eswitch_register_vport_rep(esw, 0, &rep_if, REP_IB);
138 mlx5_ib_rep_register_vf_vports(dev);
141 void mlx5_ib_unregister_vport_reps(struct mlx5_ib_dev *dev)
143 struct mlx5_eswitch *esw = dev->mdev->priv.eswitch;
145 mlx5_ib_rep_unregister_vf_vports(dev); /* VFs vports */
146 mlx5_eswitch_unregister_vport_rep(esw, 0, REP_IB); /* UPLINK PF*/
149 u8 mlx5_ib_eswitch_mode(struct mlx5_eswitch *esw)
151 return mlx5_eswitch_mode(esw);
154 struct mlx5_ib_dev *mlx5_ib_get_rep_ibdev(struct mlx5_eswitch *esw,
157 return mlx5_eswitch_get_proto_dev(esw, vport_index, REP_IB);
160 struct net_device *mlx5_ib_get_rep_netdev(struct mlx5_eswitch *esw,
163 return mlx5_eswitch_get_proto_dev(esw, vport_index, REP_ETH);
166 struct mlx5_ib_dev *mlx5_ib_get_uplink_ibdev(struct mlx5_eswitch *esw)
168 return mlx5_eswitch_uplink_get_proto_dev(esw, REP_IB);
171 struct mlx5_eswitch_rep *mlx5_ib_vport_rep(struct mlx5_eswitch *esw, int vport)
173 return mlx5_eswitch_vport_rep(esw, vport);
176 int create_flow_rule_vport_sq(struct mlx5_ib_dev *dev,
177 struct mlx5_ib_sq *sq)
179 struct mlx5_flow_handle *flow_rule;
180 struct mlx5_eswitch *esw = dev->mdev->priv.eswitch;
186 mlx5_eswitch_add_send_to_vport_rule(esw,
189 if (IS_ERR(flow_rule))
190 return PTR_ERR(flow_rule);
191 sq->flow_rule = flow_rule;