net/mlx4_core: Enable device recovery flow with SRIOV
[cascardo/linux.git] / drivers / net / ethernet / mellanox / mlx4 / intf.c
1 /*
2  * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved.
3  * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
4  *
5  * This software is available to you under a choice of one of two
6  * licenses.  You may choose to be licensed under the terms of the GNU
7  * General Public License (GPL) Version 2, available from the file
8  * COPYING in the main directory of this source tree, or the
9  * OpenIB.org BSD license below:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      - Redistributions of source code must retain the above
16  *        copyright notice, this list of conditions and the following
17  *        disclaimer.
18  *
19  *      - Redistributions in binary form must reproduce the above
20  *        copyright notice, this list of conditions and the following
21  *        disclaimer in the documentation and/or other materials
22  *        provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33
34 #include <linux/slab.h>
35 #include <linux/export.h>
36
37 #include "mlx4.h"
38
39 struct mlx4_device_context {
40         struct list_head        list;
41         struct mlx4_interface  *intf;
42         void                   *context;
43 };
44
45 static LIST_HEAD(intf_list);
46 static LIST_HEAD(dev_list);
47 static DEFINE_MUTEX(intf_mutex);
48
49 static void mlx4_add_device(struct mlx4_interface *intf, struct mlx4_priv *priv)
50 {
51         struct mlx4_device_context *dev_ctx;
52
53         dev_ctx = kmalloc(sizeof *dev_ctx, GFP_KERNEL);
54         if (!dev_ctx)
55                 return;
56
57         dev_ctx->intf    = intf;
58         dev_ctx->context = intf->add(&priv->dev);
59
60         if (dev_ctx->context) {
61                 spin_lock_irq(&priv->ctx_lock);
62                 list_add_tail(&dev_ctx->list, &priv->ctx_list);
63                 spin_unlock_irq(&priv->ctx_lock);
64         } else
65                 kfree(dev_ctx);
66 }
67
68 static void mlx4_remove_device(struct mlx4_interface *intf, struct mlx4_priv *priv)
69 {
70         struct mlx4_device_context *dev_ctx;
71
72         list_for_each_entry(dev_ctx, &priv->ctx_list, list)
73                 if (dev_ctx->intf == intf) {
74                         spin_lock_irq(&priv->ctx_lock);
75                         list_del(&dev_ctx->list);
76                         spin_unlock_irq(&priv->ctx_lock);
77
78                         intf->remove(&priv->dev, dev_ctx->context);
79                         kfree(dev_ctx);
80                         return;
81                 }
82 }
83
84 int mlx4_register_interface(struct mlx4_interface *intf)
85 {
86         struct mlx4_priv *priv;
87
88         if (!intf->add || !intf->remove)
89                 return -EINVAL;
90
91         mutex_lock(&intf_mutex);
92
93         list_add_tail(&intf->list, &intf_list);
94         list_for_each_entry(priv, &dev_list, dev_list)
95                 mlx4_add_device(intf, priv);
96
97         mutex_unlock(&intf_mutex);
98
99         return 0;
100 }
101 EXPORT_SYMBOL_GPL(mlx4_register_interface);
102
103 void mlx4_unregister_interface(struct mlx4_interface *intf)
104 {
105         struct mlx4_priv *priv;
106
107         mutex_lock(&intf_mutex);
108
109         list_for_each_entry(priv, &dev_list, dev_list)
110                 mlx4_remove_device(intf, priv);
111
112         list_del(&intf->list);
113
114         mutex_unlock(&intf_mutex);
115 }
116 EXPORT_SYMBOL_GPL(mlx4_unregister_interface);
117
118 void mlx4_dispatch_event(struct mlx4_dev *dev, enum mlx4_dev_event type,
119                          unsigned long param)
120 {
121         struct mlx4_priv *priv = mlx4_priv(dev);
122         struct mlx4_device_context *dev_ctx;
123         unsigned long flags;
124
125         spin_lock_irqsave(&priv->ctx_lock, flags);
126
127         list_for_each_entry(dev_ctx, &priv->ctx_list, list)
128                 if (dev_ctx->intf->event)
129                         dev_ctx->intf->event(dev, dev_ctx->context, type, param);
130
131         spin_unlock_irqrestore(&priv->ctx_lock, flags);
132 }
133
134 int mlx4_register_device(struct mlx4_dev *dev)
135 {
136         struct mlx4_priv *priv = mlx4_priv(dev);
137         struct mlx4_interface *intf;
138
139         mutex_lock(&intf_mutex);
140
141         dev->persist->interface_state |= MLX4_INTERFACE_STATE_UP;
142         list_add_tail(&priv->dev_list, &dev_list);
143         list_for_each_entry(intf, &intf_list, list)
144                 mlx4_add_device(intf, priv);
145
146         mutex_unlock(&intf_mutex);
147         mlx4_start_catas_poll(dev);
148
149         return 0;
150 }
151
152 void mlx4_unregister_device(struct mlx4_dev *dev)
153 {
154         struct mlx4_priv *priv = mlx4_priv(dev);
155         struct mlx4_interface *intf;
156
157         mlx4_stop_catas_poll(dev);
158         mutex_lock(&intf_mutex);
159
160         list_for_each_entry(intf, &intf_list, list)
161                 mlx4_remove_device(intf, priv);
162
163         list_del(&priv->dev_list);
164         dev->persist->interface_state &= ~MLX4_INTERFACE_STATE_UP;
165
166         mutex_unlock(&intf_mutex);
167 }
168
169 void *mlx4_get_protocol_dev(struct mlx4_dev *dev, enum mlx4_protocol proto, int port)
170 {
171         struct mlx4_priv *priv = mlx4_priv(dev);
172         struct mlx4_device_context *dev_ctx;
173         unsigned long flags;
174         void *result = NULL;
175
176         spin_lock_irqsave(&priv->ctx_lock, flags);
177
178         list_for_each_entry(dev_ctx, &priv->ctx_list, list)
179                 if (dev_ctx->intf->protocol == proto && dev_ctx->intf->get_dev) {
180                         result = dev_ctx->intf->get_dev(dev, dev_ctx->context, port);
181                         break;
182                 }
183
184         spin_unlock_irqrestore(&priv->ctx_lock, flags);
185
186         return result;
187 }
188 EXPORT_SYMBOL_GPL(mlx4_get_protocol_dev);