1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /*
3  * Copyright (c) 2018 Mellanox Technologies. All rights reserved.
4  */
5 
6 #include <linux/mlx5/vport.h>
7 #include "ib_rep.h"
8 #include "srq.h"
9 
10 static int
11 mlx5_ib_set_vport_rep(struct mlx5_core_dev *dev, struct mlx5_eswitch_rep *rep)
12 {
13 	struct mlx5_ib_dev *ibdev;
14 	int vport_index;
15 
16 	ibdev = mlx5_eswitch_uplink_get_proto_dev(dev->priv.eswitch, REP_IB);
17 	vport_index = rep->vport_index;
18 
19 	ibdev->port[vport_index].rep = rep;
20 	rep->rep_data[REP_IB].priv = ibdev;
21 	write_lock(&ibdev->port[vport_index].roce.netdev_lock);
22 	ibdev->port[vport_index].roce.netdev =
23 		mlx5_ib_get_rep_netdev(rep->esw, rep->vport);
24 	write_unlock(&ibdev->port[vport_index].roce.netdev_lock);
25 
26 	return 0;
27 }
28 
29 static int
30 mlx5_ib_vport_rep_load(struct mlx5_core_dev *dev, struct mlx5_eswitch_rep *rep)
31 {
32 	u32 num_ports = mlx5_eswitch_get_total_vports(dev);
33 	const struct mlx5_ib_profile *profile;
34 	struct mlx5_ib_dev *ibdev;
35 	int vport_index;
36 	int ret;
37 
38 	if (rep->vport == MLX5_VPORT_UPLINK)
39 		profile = &raw_eth_profile;
40 	else
41 		return mlx5_ib_set_vport_rep(dev, rep);
42 
43 	ibdev = ib_alloc_device(mlx5_ib_dev, ib_dev);
44 	if (!ibdev)
45 		return -ENOMEM;
46 
47 	ibdev->port = kcalloc(num_ports, sizeof(*ibdev->port),
48 			      GFP_KERNEL);
49 	if (!ibdev->port) {
50 		ret = -ENOMEM;
51 		goto fail_port;
52 	}
53 
54 	ibdev->is_rep = true;
55 	vport_index = rep->vport_index;
56 	ibdev->port[vport_index].rep = rep;
57 	ibdev->port[vport_index].roce.netdev =
58 		mlx5_ib_get_rep_netdev(dev->priv.eswitch, rep->vport);
59 	ibdev->mdev = dev;
60 	ibdev->num_ports = num_ports;
61 
62 	ret = __mlx5_ib_add(ibdev, profile);
63 	if (ret)
64 		goto fail_add;
65 
66 	rep->rep_data[REP_IB].priv = ibdev;
67 
68 	return 0;
69 
70 fail_add:
71 	kfree(ibdev->port);
72 fail_port:
73 	ib_dealloc_device(&ibdev->ib_dev);
74 	return ret;
75 }
76 
77 static void *mlx5_ib_rep_to_dev(struct mlx5_eswitch_rep *rep)
78 {
79 	return rep->rep_data[REP_IB].priv;
80 }
81 
82 static void
83 mlx5_ib_vport_rep_unload(struct mlx5_eswitch_rep *rep)
84 {
85 	struct mlx5_ib_dev *dev = mlx5_ib_rep_to_dev(rep);
86 	struct mlx5_ib_port *port;
87 
88 	port = &dev->port[rep->vport_index];
89 	write_lock(&port->roce.netdev_lock);
90 	port->roce.netdev = NULL;
91 	write_unlock(&port->roce.netdev_lock);
92 	rep->rep_data[REP_IB].priv = NULL;
93 	port->rep = NULL;
94 
95 	if (rep->vport == MLX5_VPORT_UPLINK)
96 		__mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX);
97 }
98 
99 static const struct mlx5_eswitch_rep_ops rep_ops = {
100 	.load = mlx5_ib_vport_rep_load,
101 	.unload = mlx5_ib_vport_rep_unload,
102 	.get_proto_dev = mlx5_ib_rep_to_dev,
103 };
104 
105 struct net_device *mlx5_ib_get_rep_netdev(struct mlx5_eswitch *esw,
106 					  u16 vport_num)
107 {
108 	return mlx5_eswitch_get_proto_dev(esw, vport_num, REP_ETH);
109 }
110 
111 struct mlx5_flow_handle *create_flow_rule_vport_sq(struct mlx5_ib_dev *dev,
112 						   struct mlx5_ib_sq *sq,
113 						   u32 port)
114 {
115 	struct mlx5_eswitch *esw = dev->mdev->priv.eswitch;
116 	struct mlx5_eswitch_rep *rep;
117 
118 	if (!dev->is_rep || !port)
119 		return NULL;
120 
121 	if (!dev->port[port - 1].rep)
122 		return ERR_PTR(-EINVAL);
123 
124 	rep = dev->port[port - 1].rep;
125 
126 	return mlx5_eswitch_add_send_to_vport_rule(esw, rep, sq->base.mqp.qpn);
127 }
128 
129 static int mlx5r_rep_probe(struct auxiliary_device *adev,
130 			   const struct auxiliary_device_id *id)
131 {
132 	struct mlx5_adev *idev = container_of(adev, struct mlx5_adev, adev);
133 	struct mlx5_core_dev *mdev = idev->mdev;
134 	struct mlx5_eswitch *esw;
135 
136 	esw = mdev->priv.eswitch;
137 	mlx5_eswitch_register_vport_reps(esw, &rep_ops, REP_IB);
138 	return 0;
139 }
140 
141 static void mlx5r_rep_remove(struct auxiliary_device *adev)
142 {
143 	struct mlx5_adev *idev = container_of(adev, struct mlx5_adev, adev);
144 	struct mlx5_core_dev *mdev = idev->mdev;
145 	struct mlx5_eswitch *esw;
146 
147 	esw = mdev->priv.eswitch;
148 	mlx5_eswitch_unregister_vport_reps(esw, REP_IB);
149 }
150 
151 static const struct auxiliary_device_id mlx5r_rep_id_table[] = {
152 	{ .name = MLX5_ADEV_NAME ".rdma-rep", },
153 	{},
154 };
155 
156 MODULE_DEVICE_TABLE(auxiliary, mlx5r_rep_id_table);
157 
158 static struct auxiliary_driver mlx5r_rep_driver = {
159 	.name = "rep",
160 	.probe = mlx5r_rep_probe,
161 	.remove = mlx5r_rep_remove,
162 	.id_table = mlx5r_rep_id_table,
163 };
164 
165 int mlx5r_rep_init(void)
166 {
167 	return auxiliary_driver_register(&mlx5r_rep_driver);
168 }
169 
170 void mlx5r_rep_cleanup(void)
171 {
172 	auxiliary_driver_unregister(&mlx5r_rep_driver);
173 }
174