]> git.itanic.dy.fi Git - linux-stable/blob - drivers/net/ethernet/mellanox/mlx5/core/lib/devcom.c
070d55f134196a0e680422626d8778eb6c6d358e
[linux-stable] / drivers / net / ethernet / mellanox / mlx5 / core / lib / devcom.c
1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /* Copyright (c) 2018 Mellanox Technologies */
3
4 #include <linux/mlx5/vport.h>
5 #include "lib/devcom.h"
6
7 static LIST_HEAD(devcom_list);
8
9 #define devcom_for_each_component(priv, comp, iter) \
10         for (iter = 0; \
11              comp = &(priv)->components[iter], iter < MLX5_DEVCOM_NUM_COMPONENTS; \
12              iter++)
13
14 struct mlx5_devcom_component {
15         struct {
16                 void __rcu *data;
17         } device[MLX5_DEVCOM_PORTS_SUPPORTED];
18
19         mlx5_devcom_event_handler_t handler;
20         struct rw_semaphore sem;
21         bool paired;
22 };
23
24 struct mlx5_devcom_list {
25         struct list_head list;
26
27         struct mlx5_devcom_component components[MLX5_DEVCOM_NUM_COMPONENTS];
28         struct mlx5_core_dev *devs[MLX5_DEVCOM_PORTS_SUPPORTED];
29 };
30
31 struct mlx5_devcom {
32         struct mlx5_devcom_list *priv;
33         int idx;
34 };
35
36 static struct mlx5_devcom_list *mlx5_devcom_list_alloc(void)
37 {
38         struct mlx5_devcom_component *comp;
39         struct mlx5_devcom_list *priv;
40         int i;
41
42         priv = kzalloc(sizeof(*priv), GFP_KERNEL);
43         if (!priv)
44                 return NULL;
45
46         devcom_for_each_component(priv, comp, i)
47                 init_rwsem(&comp->sem);
48
49         return priv;
50 }
51
52 static struct mlx5_devcom *mlx5_devcom_alloc(struct mlx5_devcom_list *priv,
53                                              u8 idx)
54 {
55         struct mlx5_devcom *devcom;
56
57         devcom = kzalloc(sizeof(*devcom), GFP_KERNEL);
58         if (!devcom)
59                 return NULL;
60
61         devcom->priv = priv;
62         devcom->idx = idx;
63         return devcom;
64 }
65
66 /* Must be called with intf_mutex held */
67 struct mlx5_devcom *mlx5_devcom_register_device(struct mlx5_core_dev *dev)
68 {
69         struct mlx5_devcom_list *priv = NULL, *iter;
70         struct mlx5_devcom *devcom = NULL;
71         bool new_priv = false;
72         u64 sguid0, sguid1;
73         int idx, i;
74
75         if (!mlx5_core_is_pf(dev))
76                 return NULL;
77         if (MLX5_CAP_GEN(dev, num_lag_ports) != MLX5_DEVCOM_PORTS_SUPPORTED)
78                 return NULL;
79
80         sguid0 = mlx5_query_nic_system_image_guid(dev);
81         list_for_each_entry(iter, &devcom_list, list) {
82                 struct mlx5_core_dev *tmp_dev = NULL;
83
84                 idx = -1;
85                 for (i = 0; i < MLX5_DEVCOM_PORTS_SUPPORTED; i++) {
86                         if (iter->devs[i])
87                                 tmp_dev = iter->devs[i];
88                         else
89                                 idx = i;
90                 }
91
92                 if (idx == -1)
93                         continue;
94
95                 sguid1 = mlx5_query_nic_system_image_guid(tmp_dev);
96                 if (sguid0 != sguid1)
97                         continue;
98
99                 priv = iter;
100                 break;
101         }
102
103         if (!priv) {
104                 priv = mlx5_devcom_list_alloc();
105                 if (!priv)
106                         return ERR_PTR(-ENOMEM);
107
108                 idx = 0;
109                 new_priv = true;
110         }
111
112         priv->devs[idx] = dev;
113         devcom = mlx5_devcom_alloc(priv, idx);
114         if (!devcom) {
115                 kfree(priv);
116                 return ERR_PTR(-ENOMEM);
117         }
118
119         if (new_priv)
120                 list_add(&priv->list, &devcom_list);
121
122         return devcom;
123 }
124
125 /* Must be called with intf_mutex held */
126 void mlx5_devcom_unregister_device(struct mlx5_devcom *devcom)
127 {
128         struct mlx5_devcom_list *priv;
129         int i;
130
131         if (IS_ERR_OR_NULL(devcom))
132                 return;
133
134         priv = devcom->priv;
135         priv->devs[devcom->idx] = NULL;
136
137         kfree(devcom);
138
139         for (i = 0; i < MLX5_DEVCOM_PORTS_SUPPORTED; i++)
140                 if (priv->devs[i])
141                         break;
142
143         if (i != MLX5_DEVCOM_PORTS_SUPPORTED)
144                 return;
145
146         list_del(&priv->list);
147         kfree(priv);
148 }
149
150 void mlx5_devcom_register_component(struct mlx5_devcom *devcom,
151                                     enum mlx5_devcom_components id,
152                                     mlx5_devcom_event_handler_t handler,
153                                     void *data)
154 {
155         struct mlx5_devcom_component *comp;
156
157         if (IS_ERR_OR_NULL(devcom))
158                 return;
159
160         WARN_ON(!data);
161
162         comp = &devcom->priv->components[id];
163         down_write(&comp->sem);
164         comp->handler = handler;
165         rcu_assign_pointer(comp->device[devcom->idx].data, data);
166         up_write(&comp->sem);
167 }
168
169 void mlx5_devcom_unregister_component(struct mlx5_devcom *devcom,
170                                       enum mlx5_devcom_components id)
171 {
172         struct mlx5_devcom_component *comp;
173
174         if (IS_ERR_OR_NULL(devcom))
175                 return;
176
177         comp = &devcom->priv->components[id];
178         down_write(&comp->sem);
179         RCU_INIT_POINTER(comp->device[devcom->idx].data, NULL);
180         up_write(&comp->sem);
181         synchronize_rcu();
182 }
183
184 int mlx5_devcom_send_event(struct mlx5_devcom *devcom,
185                            enum mlx5_devcom_components id,
186                            int event,
187                            void *event_data)
188 {
189         struct mlx5_devcom_component *comp;
190         int err = -ENODEV, i;
191
192         if (IS_ERR_OR_NULL(devcom))
193                 return err;
194
195         comp = &devcom->priv->components[id];
196         down_write(&comp->sem);
197         for (i = 0; i < MLX5_DEVCOM_PORTS_SUPPORTED; i++) {
198                 void *data = rcu_dereference_protected(comp->device[i].data,
199                                                        lockdep_is_held(&comp->sem));
200
201                 if (i != devcom->idx && data) {
202                         err = comp->handler(event, data, event_data);
203                         break;
204                 }
205         }
206
207         up_write(&comp->sem);
208         return err;
209 }
210
211 void mlx5_devcom_set_paired(struct mlx5_devcom *devcom,
212                             enum mlx5_devcom_components id,
213                             bool paired)
214 {
215         struct mlx5_devcom_component *comp;
216
217         comp = &devcom->priv->components[id];
218         WARN_ON(!rwsem_is_locked(&comp->sem));
219
220         WRITE_ONCE(comp->paired, paired);
221 }
222
223 bool mlx5_devcom_is_paired(struct mlx5_devcom *devcom,
224                            enum mlx5_devcom_components id)
225 {
226         if (IS_ERR_OR_NULL(devcom))
227                 return false;
228
229         return READ_ONCE(devcom->priv->components[id].paired);
230 }
231
232 void *mlx5_devcom_get_peer_data(struct mlx5_devcom *devcom,
233                                 enum mlx5_devcom_components id)
234 {
235         struct mlx5_devcom_component *comp;
236         int i;
237
238         if (IS_ERR_OR_NULL(devcom))
239                 return NULL;
240
241         comp = &devcom->priv->components[id];
242         down_read(&comp->sem);
243         if (!READ_ONCE(comp->paired)) {
244                 up_read(&comp->sem);
245                 return NULL;
246         }
247
248         for (i = 0; i < MLX5_DEVCOM_PORTS_SUPPORTED; i++)
249                 if (i != devcom->idx)
250                         break;
251
252         return rcu_dereference_protected(comp->device[i].data, lockdep_is_held(&comp->sem));
253 }
254
255 void *mlx5_devcom_get_peer_data_rcu(struct mlx5_devcom *devcom, enum mlx5_devcom_components id)
256 {
257         struct mlx5_devcom_component *comp;
258         int i;
259
260         if (IS_ERR_OR_NULL(devcom))
261                 return NULL;
262
263         for (i = 0; i < MLX5_DEVCOM_PORTS_SUPPORTED; i++)
264                 if (i != devcom->idx)
265                         break;
266
267         comp = &devcom->priv->components[id];
268         /* This can change concurrently, however 'data' pointer will remain
269          * valid for the duration of RCU read section.
270          */
271         if (!READ_ONCE(comp->paired))
272                 return NULL;
273
274         return rcu_dereference(comp->device[i].data);
275 }
276
277 void mlx5_devcom_release_peer_data(struct mlx5_devcom *devcom,
278                                    enum mlx5_devcom_components id)
279 {
280         struct mlx5_devcom_component *comp = &devcom->priv->components[id];
281
282         up_read(&comp->sem);
283 }