blob: 4a6c4d526f1b51e72dad933fed05558faf8f57e5 [file] [log] [blame]
Roland Dreier225c7b12007-05-08 18:00:38 -07001/*
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
Roland Dreier225c7b12007-05-08 18:00:38 -070033#include "mlx4.h"
34
35struct mlx4_device_context {
36 struct list_head list;
37 struct mlx4_interface *intf;
38 void *context;
39};
40
41static LIST_HEAD(intf_list);
42static LIST_HEAD(dev_list);
43static DEFINE_MUTEX(intf_mutex);
44
45static void mlx4_add_device(struct mlx4_interface *intf, struct mlx4_priv *priv)
46{
47 struct mlx4_device_context *dev_ctx;
48
49 dev_ctx = kmalloc(sizeof *dev_ctx, GFP_KERNEL);
50 if (!dev_ctx)
51 return;
52
53 dev_ctx->intf = intf;
54 dev_ctx->context = intf->add(&priv->dev);
55
56 if (dev_ctx->context) {
57 spin_lock_irq(&priv->ctx_lock);
58 list_add_tail(&dev_ctx->list, &priv->ctx_list);
59 spin_unlock_irq(&priv->ctx_lock);
60 } else
61 kfree(dev_ctx);
62}
63
64static void mlx4_remove_device(struct mlx4_interface *intf, struct mlx4_priv *priv)
65{
66 struct mlx4_device_context *dev_ctx;
67
68 list_for_each_entry(dev_ctx, &priv->ctx_list, list)
69 if (dev_ctx->intf == intf) {
70 spin_lock_irq(&priv->ctx_lock);
71 list_del(&dev_ctx->list);
72 spin_unlock_irq(&priv->ctx_lock);
73
74 intf->remove(&priv->dev, dev_ctx->context);
75 kfree(dev_ctx);
76 return;
77 }
78}
79
80int mlx4_register_interface(struct mlx4_interface *intf)
81{
82 struct mlx4_priv *priv;
83
84 if (!intf->add || !intf->remove)
85 return -EINVAL;
86
87 mutex_lock(&intf_mutex);
88
89 list_add_tail(&intf->list, &intf_list);
90 list_for_each_entry(priv, &dev_list, dev_list)
91 mlx4_add_device(intf, priv);
92
93 mutex_unlock(&intf_mutex);
94
95 return 0;
96}
97EXPORT_SYMBOL_GPL(mlx4_register_interface);
98
99void mlx4_unregister_interface(struct mlx4_interface *intf)
100{
101 struct mlx4_priv *priv;
102
103 mutex_lock(&intf_mutex);
104
105 list_for_each_entry(priv, &dev_list, dev_list)
106 mlx4_remove_device(intf, priv);
107
108 list_del(&intf->list);
109
110 mutex_unlock(&intf_mutex);
111}
112EXPORT_SYMBOL_GPL(mlx4_unregister_interface);
113
Roland Dreier37608ee2008-04-16 21:01:08 -0700114void mlx4_dispatch_event(struct mlx4_dev *dev, enum mlx4_dev_event type, int port)
Roland Dreier225c7b12007-05-08 18:00:38 -0700115{
116 struct mlx4_priv *priv = mlx4_priv(dev);
117 struct mlx4_device_context *dev_ctx;
118 unsigned long flags;
119
120 spin_lock_irqsave(&priv->ctx_lock, flags);
121
122 list_for_each_entry(dev_ctx, &priv->ctx_list, list)
123 if (dev_ctx->intf->event)
Roland Dreier37608ee2008-04-16 21:01:08 -0700124 dev_ctx->intf->event(dev, dev_ctx->context, type, port);
Roland Dreier225c7b12007-05-08 18:00:38 -0700125
126 spin_unlock_irqrestore(&priv->ctx_lock, flags);
127}
128
129int mlx4_register_device(struct mlx4_dev *dev)
130{
131 struct mlx4_priv *priv = mlx4_priv(dev);
132 struct mlx4_interface *intf;
133
Roland Dreier225c7b12007-05-08 18:00:38 -0700134 mutex_lock(&intf_mutex);
135
136 list_add_tail(&priv->dev_list, &dev_list);
137 list_for_each_entry(intf, &intf_list, list)
138 mlx4_add_device(intf, priv);
139
140 mutex_unlock(&intf_mutex);
Jack Morgensteinee49bd92007-07-12 17:50:45 +0300141 mlx4_start_catas_poll(dev);
Roland Dreier225c7b12007-05-08 18:00:38 -0700142
143 return 0;
144}
145
146void mlx4_unregister_device(struct mlx4_dev *dev)
147{
148 struct mlx4_priv *priv = mlx4_priv(dev);
149 struct mlx4_interface *intf;
150
Jack Morgensteinee49bd92007-07-12 17:50:45 +0300151 mlx4_stop_catas_poll(dev);
Roland Dreier225c7b12007-05-08 18:00:38 -0700152 mutex_lock(&intf_mutex);
153
154 list_for_each_entry(intf, &intf_list, list)
155 mlx4_remove_device(intf, priv);
156
157 list_del(&priv->dev_list);
158
159 mutex_unlock(&intf_mutex);
160}