1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
3 * Copyright (c) 2018 Mellanox Technologies. All rights reserved.
6 #include <linux/mlx5/vport.h>
11 mlx5_ib_set_vport_rep(struct mlx5_core_dev *dev,
12 struct mlx5_eswitch_rep *rep,
15 struct mlx5_ib_dev *ibdev;
16 struct net_device *ndev;
18 ibdev = mlx5_eswitch_uplink_get_proto_dev(dev->priv.eswitch, REP_IB);
22 ibdev->port[vport_index].rep = rep;
23 rep->rep_data[REP_IB].priv = ibdev;
24 ndev = mlx5_ib_get_rep_netdev(rep->esw, rep->vport);
26 return ib_device_set_netdev(&ibdev->ib_dev, ndev, vport_index + 1);
29 static void mlx5_ib_register_peer_vport_reps(struct mlx5_core_dev *mdev);
31 static void mlx5_ib_num_ports_update(struct mlx5_core_dev *dev, u32 *num_ports)
33 struct mlx5_core_dev *peer_dev;
36 mlx5_lag_for_each_peer_mdev(dev, peer_dev, i) {
37 u32 peer_num_ports = mlx5_eswitch_get_total_vports(peer_dev);
39 if (mlx5_lag_is_mpesw(peer_dev))
40 *num_ports += peer_num_ports;
42 /* Only 1 ib port is the representor for all uplinks */
43 *num_ports += peer_num_ports - 1;
48 mlx5_ib_vport_rep_load(struct mlx5_core_dev *dev, struct mlx5_eswitch_rep *rep)
50 u32 num_ports = mlx5_eswitch_get_total_vports(dev);
51 struct mlx5_core_dev *lag_master = dev;
52 const struct mlx5_ib_profile *profile;
53 struct mlx5_core_dev *peer_dev;
54 struct mlx5_ib_dev *ibdev;
55 int new_uplink = false;
60 vport_index = rep->vport_index;
62 if (mlx5_lag_is_shared_fdb(dev)) {
63 if (mlx5_lag_is_master(dev)) {
64 mlx5_ib_num_ports_update(dev, &num_ports);
66 if (rep->vport == MLX5_VPORT_UPLINK) {
67 if (!mlx5_lag_is_mpesw(dev))
71 mlx5_lag_for_each_peer_mdev(dev, peer_dev, i) {
72 u32 peer_n_ports = mlx5_eswitch_get_total_vports(peer_dev);
74 if (mlx5_lag_is_master(peer_dev))
75 lag_master = peer_dev;
76 else if (!mlx5_lag_is_mpesw(dev))
77 /* Only 1 ib port is the representor for all uplinks */
80 if (mlx5_get_dev_index(peer_dev) < mlx5_get_dev_index(dev))
81 vport_index += peer_n_ports;
86 if (rep->vport == MLX5_VPORT_UPLINK && !new_uplink)
87 profile = &raw_eth_profile;
89 return mlx5_ib_set_vport_rep(lag_master, rep, vport_index);
91 ibdev = ib_alloc_device(mlx5_ib_dev, ib_dev);
95 ibdev->port = kcalloc(num_ports, sizeof(*ibdev->port),
102 ibdev->is_rep = true;
103 vport_index = rep->vport_index;
104 ibdev->port[vport_index].rep = rep;
105 ibdev->mdev = lag_master;
106 ibdev->num_ports = num_ports;
107 ibdev->ib_dev.phys_port_cnt = num_ports;
108 ret = ib_device_set_netdev(&ibdev->ib_dev,
109 mlx5_ib_get_rep_netdev(lag_master->priv.eswitch,
115 ret = __mlx5_ib_add(ibdev, profile);
119 rep->rep_data[REP_IB].priv = ibdev;
120 if (mlx5_lag_is_shared_fdb(lag_master))
121 mlx5_ib_register_peer_vport_reps(lag_master);
128 ib_dealloc_device(&ibdev->ib_dev);
132 static void *mlx5_ib_rep_to_dev(struct mlx5_eswitch_rep *rep)
134 return rep->rep_data[REP_IB].priv;
138 mlx5_ib_vport_rep_unload(struct mlx5_eswitch_rep *rep)
140 struct mlx5_core_dev *mdev = mlx5_eswitch_get_core_dev(rep->esw);
141 struct mlx5_ib_dev *dev = mlx5_ib_rep_to_dev(rep);
142 int vport_index = rep->vport_index;
143 struct mlx5_ib_port *port;
152 if (mlx5_lag_is_shared_fdb(mdev) &&
153 !mlx5_lag_is_master(mdev)) {
154 if (rep->vport == MLX5_VPORT_UPLINK && !mlx5_lag_is_mpesw(mdev))
156 for (i = 0; i < dev->num_ports; i++) {
157 if (dev->port[i].rep == rep)
160 if (WARN_ON(i == dev->num_ports))
165 port = &dev->port[vport_index];
167 ib_device_set_netdev(&dev->ib_dev, NULL, vport_index + 1);
168 rep->rep_data[REP_IB].priv = NULL;
171 if (rep->vport == MLX5_VPORT_UPLINK) {
173 if (mlx5_lag_is_shared_fdb(mdev) && !mlx5_lag_is_master(mdev))
176 if (mlx5_lag_is_shared_fdb(mdev)) {
177 struct mlx5_core_dev *peer_mdev;
178 struct mlx5_eswitch *esw;
180 mlx5_lag_for_each_peer_mdev(mdev, peer_mdev, i) {
181 esw = peer_mdev->priv.eswitch;
182 mlx5_eswitch_unregister_vport_reps(esw, REP_IB);
185 __mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX);
189 static const struct mlx5_eswitch_rep_ops rep_ops = {
190 .load = mlx5_ib_vport_rep_load,
191 .unload = mlx5_ib_vport_rep_unload,
192 .get_proto_dev = mlx5_ib_rep_to_dev,
195 static void mlx5_ib_register_peer_vport_reps(struct mlx5_core_dev *mdev)
197 struct mlx5_core_dev *peer_mdev;
198 struct mlx5_eswitch *esw;
201 mlx5_lag_for_each_peer_mdev(mdev, peer_mdev, i) {
202 esw = peer_mdev->priv.eswitch;
203 mlx5_eswitch_register_vport_reps(esw, &rep_ops, REP_IB);
207 struct net_device *mlx5_ib_get_rep_netdev(struct mlx5_eswitch *esw,
210 return mlx5_eswitch_get_proto_dev(esw, vport_num, REP_ETH);
213 struct mlx5_flow_handle *create_flow_rule_vport_sq(struct mlx5_ib_dev *dev,
214 struct mlx5_ib_sq *sq,
217 struct mlx5_eswitch *esw = dev->mdev->priv.eswitch;
218 struct mlx5_eswitch_rep *rep;
220 if (!dev->is_rep || !port)
223 if (!dev->port[port - 1].rep)
224 return ERR_PTR(-EINVAL);
226 rep = dev->port[port - 1].rep;
228 return mlx5_eswitch_add_send_to_vport_rule(esw, esw, rep, sq->base.mqp.qpn);
231 static int mlx5r_rep_probe(struct auxiliary_device *adev,
232 const struct auxiliary_device_id *id)
234 struct mlx5_adev *idev = container_of(adev, struct mlx5_adev, adev);
235 struct mlx5_core_dev *mdev = idev->mdev;
236 struct mlx5_eswitch *esw;
238 esw = mdev->priv.eswitch;
239 mlx5_eswitch_register_vport_reps(esw, &rep_ops, REP_IB);
243 static void mlx5r_rep_remove(struct auxiliary_device *adev)
245 struct mlx5_adev *idev = container_of(adev, struct mlx5_adev, adev);
246 struct mlx5_core_dev *mdev = idev->mdev;
247 struct mlx5_eswitch *esw;
249 esw = mdev->priv.eswitch;
250 mlx5_eswitch_unregister_vport_reps(esw, REP_IB);
253 static const struct auxiliary_device_id mlx5r_rep_id_table[] = {
254 { .name = MLX5_ADEV_NAME ".rdma-rep", },
258 MODULE_DEVICE_TABLE(auxiliary, mlx5r_rep_id_table);
260 static struct auxiliary_driver mlx5r_rep_driver = {
262 .probe = mlx5r_rep_probe,
263 .remove = mlx5r_rep_remove,
264 .id_table = mlx5r_rep_id_table,
267 int mlx5r_rep_init(void)
269 return auxiliary_driver_register(&mlx5r_rep_driver);
272 void mlx5r_rep_cleanup(void)
274 auxiliary_driver_unregister(&mlx5r_rep_driver);