1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /* Copyright (c) 2018 Mellanox Technologies */
4 #include <linux/mlx5/vport.h>
5 #include "lib/devcom.h"
7 static LIST_HEAD(devcom_list);
9 #define devcom_for_each_component(priv, comp, iter) \
11 comp = &(priv)->components[iter], iter < MLX5_DEVCOM_NUM_COMPONENTS; \
14 struct mlx5_devcom_component {
17 } device[MLX5_DEVCOM_PORTS_SUPPORTED];
19 mlx5_devcom_event_handler_t handler;
20 struct rw_semaphore sem;
24 struct mlx5_devcom_list {
25 struct list_head list;
27 struct mlx5_devcom_component components[MLX5_DEVCOM_NUM_COMPONENTS];
28 struct mlx5_core_dev *devs[MLX5_DEVCOM_PORTS_SUPPORTED];
32 struct mlx5_devcom_list *priv;
36 static struct mlx5_devcom_list *mlx5_devcom_list_alloc(void)
38 struct mlx5_devcom_component *comp;
39 struct mlx5_devcom_list *priv;
42 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
46 devcom_for_each_component(priv, comp, i)
47 init_rwsem(&comp->sem);
52 static struct mlx5_devcom *mlx5_devcom_alloc(struct mlx5_devcom_list *priv,
55 struct mlx5_devcom *devcom;
57 devcom = kzalloc(sizeof(*devcom), GFP_KERNEL);
66 /* Must be called with intf_mutex held */
67 struct mlx5_devcom *mlx5_devcom_register_device(struct mlx5_core_dev *dev)
69 struct mlx5_devcom_list *priv = NULL, *iter;
70 struct mlx5_devcom *devcom = NULL;
71 bool new_priv = false;
75 if (!mlx5_core_is_pf(dev))
77 if (MLX5_CAP_GEN(dev, num_lag_ports) != MLX5_DEVCOM_PORTS_SUPPORTED)
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;
85 for (i = 0; i < MLX5_DEVCOM_PORTS_SUPPORTED; i++) {
87 tmp_dev = iter->devs[i];
95 sguid1 = mlx5_query_nic_system_image_guid(tmp_dev);
104 priv = mlx5_devcom_list_alloc();
106 return ERR_PTR(-ENOMEM);
112 priv->devs[idx] = dev;
113 devcom = mlx5_devcom_alloc(priv, idx);
117 return ERR_PTR(-ENOMEM);
121 list_add(&priv->list, &devcom_list);
126 /* Must be called with intf_mutex held */
127 void mlx5_devcom_unregister_device(struct mlx5_devcom *devcom)
129 struct mlx5_devcom_list *priv;
132 if (IS_ERR_OR_NULL(devcom))
136 priv->devs[devcom->idx] = NULL;
140 for (i = 0; i < MLX5_DEVCOM_PORTS_SUPPORTED; i++)
144 if (i != MLX5_DEVCOM_PORTS_SUPPORTED)
147 list_del(&priv->list);
151 void mlx5_devcom_register_component(struct mlx5_devcom *devcom,
152 enum mlx5_devcom_components id,
153 mlx5_devcom_event_handler_t handler,
156 struct mlx5_devcom_component *comp;
158 if (IS_ERR_OR_NULL(devcom))
163 comp = &devcom->priv->components[id];
164 down_write(&comp->sem);
165 comp->handler = handler;
166 rcu_assign_pointer(comp->device[devcom->idx].data, data);
167 up_write(&comp->sem);
170 void mlx5_devcom_unregister_component(struct mlx5_devcom *devcom,
171 enum mlx5_devcom_components id)
173 struct mlx5_devcom_component *comp;
175 if (IS_ERR_OR_NULL(devcom))
178 comp = &devcom->priv->components[id];
179 down_write(&comp->sem);
180 RCU_INIT_POINTER(comp->device[devcom->idx].data, NULL);
181 up_write(&comp->sem);
185 int mlx5_devcom_send_event(struct mlx5_devcom *devcom,
186 enum mlx5_devcom_components id,
190 struct mlx5_devcom_component *comp;
191 int err = -ENODEV, i;
193 if (IS_ERR_OR_NULL(devcom))
196 comp = &devcom->priv->components[id];
197 down_write(&comp->sem);
198 for (i = 0; i < MLX5_DEVCOM_PORTS_SUPPORTED; i++) {
199 void *data = rcu_dereference_protected(comp->device[i].data,
200 lockdep_is_held(&comp->sem));
202 if (i != devcom->idx && data) {
203 err = comp->handler(event, data, event_data);
208 up_write(&comp->sem);
212 void mlx5_devcom_set_paired(struct mlx5_devcom *devcom,
213 enum mlx5_devcom_components id,
216 struct mlx5_devcom_component *comp;
218 comp = &devcom->priv->components[id];
219 WARN_ON(!rwsem_is_locked(&comp->sem));
221 WRITE_ONCE(comp->paired, paired);
224 bool mlx5_devcom_is_paired(struct mlx5_devcom *devcom,
225 enum mlx5_devcom_components id)
227 if (IS_ERR_OR_NULL(devcom))
230 return READ_ONCE(devcom->priv->components[id].paired);
233 void *mlx5_devcom_get_peer_data(struct mlx5_devcom *devcom,
234 enum mlx5_devcom_components id)
236 struct mlx5_devcom_component *comp;
239 if (IS_ERR_OR_NULL(devcom))
242 comp = &devcom->priv->components[id];
243 down_read(&comp->sem);
244 if (!READ_ONCE(comp->paired)) {
249 for (i = 0; i < MLX5_DEVCOM_PORTS_SUPPORTED; i++)
250 if (i != devcom->idx)
253 return rcu_dereference_protected(comp->device[i].data, lockdep_is_held(&comp->sem));
256 void *mlx5_devcom_get_peer_data_rcu(struct mlx5_devcom *devcom, enum mlx5_devcom_components id)
258 struct mlx5_devcom_component *comp;
261 if (IS_ERR_OR_NULL(devcom))
264 for (i = 0; i < MLX5_DEVCOM_PORTS_SUPPORTED; i++)
265 if (i != devcom->idx)
268 comp = &devcom->priv->components[id];
269 /* This can change concurrently, however 'data' pointer will remain
270 * valid for the duration of RCU read section.
272 if (!READ_ONCE(comp->paired))
275 return rcu_dereference(comp->device[i].data);
278 void mlx5_devcom_release_peer_data(struct mlx5_devcom *devcom,
279 enum mlx5_devcom_components id)
281 struct mlx5_devcom_component *comp = &devcom->priv->components[id];