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);
116 return ERR_PTR(-ENOMEM);
120 list_add(&priv->list, &devcom_list);
125 /* Must be called with intf_mutex held */
126 void mlx5_devcom_unregister_device(struct mlx5_devcom *devcom)
128 struct mlx5_devcom_list *priv;
131 if (IS_ERR_OR_NULL(devcom))
135 priv->devs[devcom->idx] = NULL;
139 for (i = 0; i < MLX5_DEVCOM_PORTS_SUPPORTED; i++)
143 if (i != MLX5_DEVCOM_PORTS_SUPPORTED)
146 list_del(&priv->list);
150 void mlx5_devcom_register_component(struct mlx5_devcom *devcom,
151 enum mlx5_devcom_components id,
152 mlx5_devcom_event_handler_t handler,
155 struct mlx5_devcom_component *comp;
157 if (IS_ERR_OR_NULL(devcom))
162 comp = &devcom->priv->components[id];
163 down_write(&comp->sem);
164 comp->handler = handler;
165 comp->device[devcom->idx].data = data;
166 up_write(&comp->sem);
169 void mlx5_devcom_unregister_component(struct mlx5_devcom *devcom,
170 enum mlx5_devcom_components id)
172 struct mlx5_devcom_component *comp;
174 if (IS_ERR_OR_NULL(devcom))
177 comp = &devcom->priv->components[id];
178 down_write(&comp->sem);
179 comp->device[devcom->idx].data = NULL;
180 up_write(&comp->sem);
183 int mlx5_devcom_send_event(struct mlx5_devcom *devcom,
184 enum mlx5_devcom_components id,
188 struct mlx5_devcom_component *comp;
189 int err = -ENODEV, i;
191 if (IS_ERR_OR_NULL(devcom))
194 comp = &devcom->priv->components[id];
195 down_write(&comp->sem);
196 for (i = 0; i < MLX5_DEVCOM_PORTS_SUPPORTED; i++)
197 if (i != devcom->idx && comp->device[i].data) {
198 err = comp->handler(event, comp->device[i].data,
203 up_write(&comp->sem);
207 void mlx5_devcom_set_paired(struct mlx5_devcom *devcom,
208 enum mlx5_devcom_components id,
211 struct mlx5_devcom_component *comp;
213 comp = &devcom->priv->components[id];
214 WARN_ON(!rwsem_is_locked(&comp->sem));
216 comp->paired = paired;
219 bool mlx5_devcom_is_paired(struct mlx5_devcom *devcom,
220 enum mlx5_devcom_components id)
222 if (IS_ERR_OR_NULL(devcom))
225 return devcom->priv->components[id].paired;
228 void *mlx5_devcom_get_peer_data(struct mlx5_devcom *devcom,
229 enum mlx5_devcom_components id)
231 struct mlx5_devcom_component *comp;
234 if (IS_ERR_OR_NULL(devcom))
237 comp = &devcom->priv->components[id];
238 down_read(&comp->sem);
244 for (i = 0; i < MLX5_DEVCOM_PORTS_SUPPORTED; i++)
245 if (i != devcom->idx)
248 return comp->device[i].data;
251 void mlx5_devcom_release_peer_data(struct mlx5_devcom *devcom,
252 enum mlx5_devcom_components id)
254 struct mlx5_devcom_component *comp = &devcom->priv->components[id];