2 * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
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:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
36 struct mlx4_device_context
{
37 struct list_head list
;
38 struct mlx4_interface
*intf
;
42 static LIST_HEAD(intf_list
);
43 static LIST_HEAD(dev_list
);
44 static DEFINE_MUTEX(intf_mutex
);
46 static void mlx4_add_device(struct mlx4_interface
*intf
, struct mlx4_priv
*priv
)
48 struct mlx4_device_context
*dev_ctx
;
50 dev_ctx
= kmalloc(sizeof *dev_ctx
, GFP_KERNEL
);
55 dev_ctx
->context
= intf
->add(&priv
->dev
);
57 if (dev_ctx
->context
) {
58 spin_lock_irq(&priv
->ctx_lock
);
59 list_add_tail(&dev_ctx
->list
, &priv
->ctx_list
);
60 spin_unlock_irq(&priv
->ctx_lock
);
65 static void mlx4_remove_device(struct mlx4_interface
*intf
, struct mlx4_priv
*priv
)
67 struct mlx4_device_context
*dev_ctx
;
69 list_for_each_entry(dev_ctx
, &priv
->ctx_list
, list
)
70 if (dev_ctx
->intf
== intf
) {
71 spin_lock_irq(&priv
->ctx_lock
);
72 list_del(&dev_ctx
->list
);
73 spin_unlock_irq(&priv
->ctx_lock
);
75 intf
->remove(&priv
->dev
, dev_ctx
->context
);
81 int mlx4_register_interface(struct mlx4_interface
*intf
)
83 struct mlx4_priv
*priv
;
85 if (!intf
->add
|| !intf
->remove
)
88 mutex_lock(&intf_mutex
);
90 list_add_tail(&intf
->list
, &intf_list
);
91 list_for_each_entry(priv
, &dev_list
, dev_list
)
92 mlx4_add_device(intf
, priv
);
94 mutex_unlock(&intf_mutex
);
98 EXPORT_SYMBOL_GPL(mlx4_register_interface
);
100 void mlx4_unregister_interface(struct mlx4_interface
*intf
)
102 struct mlx4_priv
*priv
;
104 mutex_lock(&intf_mutex
);
106 list_for_each_entry(priv
, &dev_list
, dev_list
)
107 mlx4_remove_device(intf
, priv
);
109 list_del(&intf
->list
);
111 mutex_unlock(&intf_mutex
);
113 EXPORT_SYMBOL_GPL(mlx4_unregister_interface
);
115 void mlx4_dispatch_event(struct mlx4_dev
*dev
, enum mlx4_dev_event type
, int port
)
117 struct mlx4_priv
*priv
= mlx4_priv(dev
);
118 struct mlx4_device_context
*dev_ctx
;
121 spin_lock_irqsave(&priv
->ctx_lock
, flags
);
123 list_for_each_entry(dev_ctx
, &priv
->ctx_list
, list
)
124 if (dev_ctx
->intf
->event
)
125 dev_ctx
->intf
->event(dev
, dev_ctx
->context
, type
, port
);
127 spin_unlock_irqrestore(&priv
->ctx_lock
, flags
);
130 int mlx4_register_device(struct mlx4_dev
*dev
)
132 struct mlx4_priv
*priv
= mlx4_priv(dev
);
133 struct mlx4_interface
*intf
;
135 mutex_lock(&intf_mutex
);
137 list_add_tail(&priv
->dev_list
, &dev_list
);
138 list_for_each_entry(intf
, &intf_list
, list
)
139 mlx4_add_device(intf
, priv
);
141 mutex_unlock(&intf_mutex
);
142 mlx4_start_catas_poll(dev
);
147 void mlx4_unregister_device(struct mlx4_dev
*dev
)
149 struct mlx4_priv
*priv
= mlx4_priv(dev
);
150 struct mlx4_interface
*intf
;
152 mlx4_stop_catas_poll(dev
);
153 mutex_lock(&intf_mutex
);
155 list_for_each_entry(intf
, &intf_list
, list
)
156 mlx4_remove_device(intf
, priv
);
158 list_del(&priv
->dev_list
);
160 mutex_unlock(&intf_mutex
);