9ae951bf6aa681116bc914db52699d62e6857d19
[linux-block.git] / drivers / net / mlx4 / intf.c
1 /*
2  * Copyright (c) 2006, 2007 Cisco Systems, Inc. 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/mlx4/driver.h>
34
35 #include "mlx4.h"
36
37 struct mlx4_device_context {
38         struct list_head        list;
39         struct mlx4_interface  *intf;
40         void                   *context;
41 };
42
43 static LIST_HEAD(intf_list);
44 static LIST_HEAD(dev_list);
45 static DEFINE_MUTEX(intf_mutex);
46
47 static void mlx4_add_device(struct mlx4_interface *intf, struct mlx4_priv *priv)
48 {
49         struct mlx4_device_context *dev_ctx;
50
51         dev_ctx = kmalloc(sizeof *dev_ctx, GFP_KERNEL);
52         if (!dev_ctx)
53                 return;
54
55         dev_ctx->intf    = intf;
56         dev_ctx->context = intf->add(&priv->dev);
57
58         if (dev_ctx->context) {
59                 spin_lock_irq(&priv->ctx_lock);
60                 list_add_tail(&dev_ctx->list, &priv->ctx_list);
61                 spin_unlock_irq(&priv->ctx_lock);
62         } else
63                 kfree(dev_ctx);
64 }
65
66 static void mlx4_remove_device(struct mlx4_interface *intf, struct mlx4_priv *priv)
67 {
68         struct mlx4_device_context *dev_ctx;
69
70         list_for_each_entry(dev_ctx, &priv->ctx_list, list)
71                 if (dev_ctx->intf == intf) {
72                         spin_lock_irq(&priv->ctx_lock);
73                         list_del(&dev_ctx->list);
74                         spin_unlock_irq(&priv->ctx_lock);
75
76                         intf->remove(&priv->dev, dev_ctx->context);
77                         kfree(dev_ctx);
78                         return;
79                 }
80 }
81
82 int mlx4_register_interface(struct mlx4_interface *intf)
83 {
84         struct mlx4_priv *priv;
85
86         if (!intf->add || !intf->remove)
87                 return -EINVAL;
88
89         mutex_lock(&intf_mutex);
90
91         list_add_tail(&intf->list, &intf_list);
92         list_for_each_entry(priv, &dev_list, dev_list)
93                 mlx4_add_device(intf, priv);
94
95         mutex_unlock(&intf_mutex);
96
97         return 0;
98 }
99 EXPORT_SYMBOL_GPL(mlx4_register_interface);
100
101 void mlx4_unregister_interface(struct mlx4_interface *intf)
102 {
103         struct mlx4_priv *priv;
104
105         mutex_lock(&intf_mutex);
106
107         list_for_each_entry(priv, &dev_list, dev_list)
108                 mlx4_remove_device(intf, priv);
109
110         list_del(&intf->list);
111
112         mutex_unlock(&intf_mutex);
113 }
114 EXPORT_SYMBOL_GPL(mlx4_unregister_interface);
115
116 void mlx4_dispatch_event(struct mlx4_dev *dev, enum mlx4_event type,
117                          int subtype, int port)
118 {
119         struct mlx4_priv *priv = mlx4_priv(dev);
120         struct mlx4_device_context *dev_ctx;
121         unsigned long flags;
122
123         spin_lock_irqsave(&priv->ctx_lock, flags);
124
125         list_for_each_entry(dev_ctx, &priv->ctx_list, list)
126                 if (dev_ctx->intf->event)
127                         dev_ctx->intf->event(dev, dev_ctx->context, type,
128                                              subtype, port);
129
130         spin_unlock_irqrestore(&priv->ctx_lock, flags);
131 }
132
133 int mlx4_register_device(struct mlx4_dev *dev)
134 {
135         struct mlx4_priv *priv = mlx4_priv(dev);
136         struct mlx4_interface *intf;
137
138         mutex_lock(&intf_mutex);
139
140         list_add_tail(&priv->dev_list, &dev_list);
141         list_for_each_entry(intf, &intf_list, list)
142                 mlx4_add_device(intf, priv);
143
144         mutex_unlock(&intf_mutex);
145
146         return 0;
147 }
148
149 void mlx4_unregister_device(struct mlx4_dev *dev)
150 {
151         struct mlx4_priv *priv = mlx4_priv(dev);
152         struct mlx4_interface *intf;
153
154         mutex_lock(&intf_mutex);
155
156         list_for_each_entry(intf, &intf_list, list)
157                 mlx4_remove_device(intf, priv);
158
159         list_del(&priv->dev_list);
160
161         mutex_unlock(&intf_mutex);
162 }