Commit | Line | Data |
---|---|---|
f1ee87fe MHY |
1 | /* |
2 | * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved. | |
3 | * | |
4 | * This software is available to you under a choice of one of two | |
5 | * licenses. You may choose to be licensed under the terms of the GNU | |
6 | * General Public License (GPL) Version 2, available from the file | |
7 | * COPYING in the main directory of this source tree, or the | |
8 | * OpenIB.org BSD license below: | |
9 | * | |
10 | * Redistribution and use in source and binary forms, with or | |
11 | * without modification, are permitted provided that the following | |
12 | * conditions are met: | |
13 | * | |
14 | * - Redistributions of source code must retain the above | |
15 | * copyright notice, this list of conditions and the following | |
16 | * disclaimer. | |
17 | * | |
18 | * - Redistributions in binary form must reproduce the above | |
19 | * copyright notice, this list of conditions and the following | |
20 | * disclaimer in the documentation and/or other materials | |
21 | * provided with the distribution. | |
22 | * | |
23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
24 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
25 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
26 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | |
27 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | |
28 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
29 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
30 | * SOFTWARE. | |
31 | */ | |
32 | ||
33 | #include <linux/mlx5/driver.h> | |
34 | #include "mlx5_core.h" | |
35 | ||
36 | static LIST_HEAD(intf_list); | |
37 | static LIST_HEAD(mlx5_dev_list); | |
38 | /* intf dev list mutex */ | |
39 | static DEFINE_MUTEX(mlx5_intf_mutex); | |
40 | ||
41 | struct mlx5_device_context { | |
42 | struct list_head list; | |
43 | struct mlx5_interface *intf; | |
44 | void *context; | |
45 | unsigned long state; | |
46 | }; | |
47 | ||
48 | enum { | |
49 | MLX5_INTERFACE_ADDED, | |
50 | MLX5_INTERFACE_ATTACHED, | |
51 | }; | |
52 | ||
97834eba | 53 | |
f1ee87fe MHY |
54 | void mlx5_add_device(struct mlx5_interface *intf, struct mlx5_priv *priv) |
55 | { | |
56 | struct mlx5_device_context *dev_ctx; | |
57 | struct mlx5_core_dev *dev = container_of(priv, struct mlx5_core_dev, priv); | |
58 | ||
59 | if (!mlx5_lag_intf_add(intf, priv)) | |
60 | return; | |
61 | ||
62 | dev_ctx = kzalloc(sizeof(*dev_ctx), GFP_KERNEL); | |
63 | if (!dev_ctx) | |
64 | return; | |
65 | ||
66 | dev_ctx->intf = intf; | |
97834eba | 67 | |
f1ee87fe | 68 | dev_ctx->context = intf->add(dev); |
f1ee87fe | 69 | if (dev_ctx->context) { |
47bc94b8 HN |
70 | set_bit(MLX5_INTERFACE_ADDED, &dev_ctx->state); |
71 | if (intf->attach) | |
72 | set_bit(MLX5_INTERFACE_ATTACHED, &dev_ctx->state); | |
73 | ||
f1ee87fe MHY |
74 | spin_lock_irq(&priv->ctx_lock); |
75 | list_add_tail(&dev_ctx->list, &priv->ctx_list); | |
76 | spin_unlock_irq(&priv->ctx_lock); | |
f1ee87fe | 77 | } |
4ca637a2 | 78 | |
4ca637a2 HN |
79 | if (!dev_ctx->context) |
80 | kfree(dev_ctx); | |
f1ee87fe MHY |
81 | } |
82 | ||
83 | static struct mlx5_device_context *mlx5_get_device(struct mlx5_interface *intf, | |
84 | struct mlx5_priv *priv) | |
85 | { | |
86 | struct mlx5_device_context *dev_ctx; | |
87 | ||
88 | list_for_each_entry(dev_ctx, &priv->ctx_list, list) | |
89 | if (dev_ctx->intf == intf) | |
90 | return dev_ctx; | |
91 | return NULL; | |
92 | } | |
93 | ||
94 | void mlx5_remove_device(struct mlx5_interface *intf, struct mlx5_priv *priv) | |
95 | { | |
96 | struct mlx5_device_context *dev_ctx; | |
97 | struct mlx5_core_dev *dev = container_of(priv, struct mlx5_core_dev, priv); | |
98 | ||
99 | dev_ctx = mlx5_get_device(intf, priv); | |
100 | if (!dev_ctx) | |
101 | return; | |
102 | ||
103 | spin_lock_irq(&priv->ctx_lock); | |
104 | list_del(&dev_ctx->list); | |
105 | spin_unlock_irq(&priv->ctx_lock); | |
106 | ||
107 | if (test_bit(MLX5_INTERFACE_ADDED, &dev_ctx->state)) | |
108 | intf->remove(dev, dev_ctx->context); | |
109 | ||
110 | kfree(dev_ctx); | |
111 | } | |
112 | ||
113 | static void mlx5_attach_interface(struct mlx5_interface *intf, struct mlx5_priv *priv) | |
114 | { | |
115 | struct mlx5_device_context *dev_ctx; | |
116 | struct mlx5_core_dev *dev = container_of(priv, struct mlx5_core_dev, priv); | |
117 | ||
118 | dev_ctx = mlx5_get_device(intf, priv); | |
119 | if (!dev_ctx) | |
120 | return; | |
121 | ||
122 | if (intf->attach) { | |
123 | if (test_bit(MLX5_INTERFACE_ATTACHED, &dev_ctx->state)) | |
02039fb6 | 124 | return; |
47bc94b8 | 125 | if (intf->attach(dev, dev_ctx->context)) |
02039fb6 | 126 | return; |
f1ee87fe MHY |
127 | set_bit(MLX5_INTERFACE_ATTACHED, &dev_ctx->state); |
128 | } else { | |
129 | if (test_bit(MLX5_INTERFACE_ADDED, &dev_ctx->state)) | |
02039fb6 | 130 | return; |
f1ee87fe | 131 | dev_ctx->context = intf->add(dev); |
47bc94b8 | 132 | if (!dev_ctx->context) |
02039fb6 | 133 | return; |
f1ee87fe MHY |
134 | set_bit(MLX5_INTERFACE_ADDED, &dev_ctx->state); |
135 | } | |
136 | } | |
137 | ||
138 | void mlx5_attach_device(struct mlx5_core_dev *dev) | |
139 | { | |
140 | struct mlx5_priv *priv = &dev->priv; | |
141 | struct mlx5_interface *intf; | |
142 | ||
143 | mutex_lock(&mlx5_intf_mutex); | |
144 | list_for_each_entry(intf, &intf_list, list) | |
145 | mlx5_attach_interface(intf, priv); | |
146 | mutex_unlock(&mlx5_intf_mutex); | |
147 | } | |
148 | ||
149 | static void mlx5_detach_interface(struct mlx5_interface *intf, struct mlx5_priv *priv) | |
150 | { | |
151 | struct mlx5_device_context *dev_ctx; | |
152 | struct mlx5_core_dev *dev = container_of(priv, struct mlx5_core_dev, priv); | |
153 | ||
154 | dev_ctx = mlx5_get_device(intf, priv); | |
155 | if (!dev_ctx) | |
156 | return; | |
157 | ||
158 | if (intf->detach) { | |
159 | if (!test_bit(MLX5_INTERFACE_ATTACHED, &dev_ctx->state)) | |
160 | return; | |
161 | intf->detach(dev, dev_ctx->context); | |
162 | clear_bit(MLX5_INTERFACE_ATTACHED, &dev_ctx->state); | |
163 | } else { | |
164 | if (!test_bit(MLX5_INTERFACE_ADDED, &dev_ctx->state)) | |
165 | return; | |
166 | intf->remove(dev, dev_ctx->context); | |
167 | clear_bit(MLX5_INTERFACE_ADDED, &dev_ctx->state); | |
168 | } | |
169 | } | |
170 | ||
171 | void mlx5_detach_device(struct mlx5_core_dev *dev) | |
172 | { | |
173 | struct mlx5_priv *priv = &dev->priv; | |
174 | struct mlx5_interface *intf; | |
175 | ||
176 | mutex_lock(&mlx5_intf_mutex); | |
177 | list_for_each_entry(intf, &intf_list, list) | |
178 | mlx5_detach_interface(intf, priv); | |
179 | mutex_unlock(&mlx5_intf_mutex); | |
180 | } | |
181 | ||
182 | bool mlx5_device_registered(struct mlx5_core_dev *dev) | |
183 | { | |
184 | struct mlx5_priv *priv; | |
185 | bool found = false; | |
186 | ||
187 | mutex_lock(&mlx5_intf_mutex); | |
188 | list_for_each_entry(priv, &mlx5_dev_list, dev_list) | |
189 | if (priv == &dev->priv) | |
190 | found = true; | |
191 | mutex_unlock(&mlx5_intf_mutex); | |
192 | ||
193 | return found; | |
194 | } | |
195 | ||
196 | int mlx5_register_device(struct mlx5_core_dev *dev) | |
197 | { | |
198 | struct mlx5_priv *priv = &dev->priv; | |
199 | struct mlx5_interface *intf; | |
200 | ||
201 | mutex_lock(&mlx5_intf_mutex); | |
202 | list_add_tail(&priv->dev_list, &mlx5_dev_list); | |
203 | list_for_each_entry(intf, &intf_list, list) | |
204 | mlx5_add_device(intf, priv); | |
205 | mutex_unlock(&mlx5_intf_mutex); | |
206 | ||
207 | return 0; | |
208 | } | |
209 | ||
210 | void mlx5_unregister_device(struct mlx5_core_dev *dev) | |
211 | { | |
212 | struct mlx5_priv *priv = &dev->priv; | |
213 | struct mlx5_interface *intf; | |
214 | ||
215 | mutex_lock(&mlx5_intf_mutex); | |
08aa5e7d | 216 | list_for_each_entry_reverse(intf, &intf_list, list) |
f1ee87fe MHY |
217 | mlx5_remove_device(intf, priv); |
218 | list_del(&priv->dev_list); | |
219 | mutex_unlock(&mlx5_intf_mutex); | |
220 | } | |
221 | ||
222 | int mlx5_register_interface(struct mlx5_interface *intf) | |
223 | { | |
224 | struct mlx5_priv *priv; | |
225 | ||
226 | if (!intf->add || !intf->remove) | |
227 | return -EINVAL; | |
228 | ||
229 | mutex_lock(&mlx5_intf_mutex); | |
230 | list_add_tail(&intf->list, &intf_list); | |
231 | list_for_each_entry(priv, &mlx5_dev_list, dev_list) | |
232 | mlx5_add_device(intf, priv); | |
233 | mutex_unlock(&mlx5_intf_mutex); | |
234 | ||
235 | return 0; | |
236 | } | |
237 | EXPORT_SYMBOL(mlx5_register_interface); | |
238 | ||
239 | void mlx5_unregister_interface(struct mlx5_interface *intf) | |
240 | { | |
241 | struct mlx5_priv *priv; | |
242 | ||
243 | mutex_lock(&mlx5_intf_mutex); | |
244 | list_for_each_entry(priv, &mlx5_dev_list, dev_list) | |
245 | mlx5_remove_device(intf, priv); | |
246 | list_del(&intf->list); | |
247 | mutex_unlock(&mlx5_intf_mutex); | |
248 | } | |
249 | EXPORT_SYMBOL(mlx5_unregister_interface); | |
250 | ||
dd80857b AH |
251 | /* Must be called with intf_mutex held */ |
252 | static bool mlx5_has_added_dev_by_protocol(struct mlx5_core_dev *mdev, int protocol) | |
253 | { | |
254 | struct mlx5_device_context *dev_ctx; | |
255 | struct mlx5_interface *intf; | |
256 | bool found = false; | |
257 | ||
258 | list_for_each_entry(intf, &intf_list, list) { | |
259 | if (intf->protocol == protocol) { | |
260 | dev_ctx = mlx5_get_device(intf, &mdev->priv); | |
261 | if (dev_ctx && test_bit(MLX5_INTERFACE_ADDED, &dev_ctx->state)) | |
262 | found = true; | |
263 | break; | |
264 | } | |
265 | } | |
266 | ||
267 | return found; | |
268 | } | |
269 | ||
c5447c70 MB |
270 | void mlx5_reload_interface(struct mlx5_core_dev *mdev, int protocol) |
271 | { | |
272 | mutex_lock(&mlx5_intf_mutex); | |
dd80857b AH |
273 | if (mlx5_has_added_dev_by_protocol(mdev, protocol)) { |
274 | mlx5_remove_dev_by_protocol(mdev, protocol); | |
275 | mlx5_add_dev_by_protocol(mdev, protocol); | |
276 | } | |
c5447c70 MB |
277 | mutex_unlock(&mlx5_intf_mutex); |
278 | } | |
279 | ||
f1ee87fe MHY |
280 | /* Must be called with intf_mutex held */ |
281 | void mlx5_add_dev_by_protocol(struct mlx5_core_dev *dev, int protocol) | |
282 | { | |
283 | struct mlx5_interface *intf; | |
284 | ||
285 | list_for_each_entry(intf, &intf_list, list) | |
286 | if (intf->protocol == protocol) { | |
287 | mlx5_add_device(intf, &dev->priv); | |
288 | break; | |
289 | } | |
290 | } | |
291 | ||
292 | /* Must be called with intf_mutex held */ | |
293 | void mlx5_remove_dev_by_protocol(struct mlx5_core_dev *dev, int protocol) | |
294 | { | |
295 | struct mlx5_interface *intf; | |
296 | ||
297 | list_for_each_entry(intf, &intf_list, list) | |
298 | if (intf->protocol == protocol) { | |
299 | mlx5_remove_device(intf, &dev->priv); | |
300 | break; | |
301 | } | |
302 | } | |
303 | ||
df7ddb23 | 304 | static u32 mlx5_gen_pci_id(struct mlx5_core_dev *dev) |
f1ee87fe | 305 | { |
df7ddb23 DJ |
306 | return (u32)((pci_domain_nr(dev->pdev->bus) << 16) | |
307 | (dev->pdev->bus->number << 8) | | |
f1ee87fe MHY |
308 | PCI_SLOT(dev->pdev->devfn)); |
309 | } | |
310 | ||
311 | /* Must be called with intf_mutex held */ | |
312 | struct mlx5_core_dev *mlx5_get_next_phys_dev(struct mlx5_core_dev *dev) | |
313 | { | |
f1ee87fe MHY |
314 | struct mlx5_core_dev *res = NULL; |
315 | struct mlx5_core_dev *tmp_dev; | |
316 | struct mlx5_priv *priv; | |
7e26dac2 | 317 | u32 pci_id; |
f1ee87fe | 318 | |
7e26dac2 PP |
319 | if (!mlx5_core_is_pf(dev)) |
320 | return NULL; | |
321 | ||
322 | pci_id = mlx5_gen_pci_id(dev); | |
f1ee87fe MHY |
323 | list_for_each_entry(priv, &mlx5_dev_list, dev_list) { |
324 | tmp_dev = container_of(priv, struct mlx5_core_dev, priv); | |
7e26dac2 PP |
325 | if (!mlx5_core_is_pf(tmp_dev)) |
326 | continue; | |
327 | ||
f1ee87fe MHY |
328 | if ((dev != tmp_dev) && (mlx5_gen_pci_id(tmp_dev) == pci_id)) { |
329 | res = tmp_dev; | |
330 | break; | |
331 | } | |
332 | } | |
333 | ||
334 | return res; | |
335 | } | |
336 | ||
f1ee87fe MHY |
337 | |
338 | void mlx5_dev_list_lock(void) | |
339 | { | |
340 | mutex_lock(&mlx5_intf_mutex); | |
341 | } | |
342 | ||
343 | void mlx5_dev_list_unlock(void) | |
344 | { | |
345 | mutex_unlock(&mlx5_intf_mutex); | |
346 | } | |
347 | ||
348 | int mlx5_dev_list_trylock(void) | |
349 | { | |
350 | return mutex_trylock(&mlx5_intf_mutex); | |
351 | } |