2 * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
33 #include <linux/mlx4/driver.h>
37 struct mlx4_device_context {
38 struct list_head list;
39 struct mlx4_interface *intf;
43 static LIST_HEAD(intf_list);
44 static LIST_HEAD(dev_list);
45 static DEFINE_MUTEX(intf_mutex);
47 static void mlx4_add_device(struct mlx4_interface *intf, struct mlx4_priv *priv)
49 struct mlx4_device_context *dev_ctx;
51 dev_ctx = kmalloc(sizeof *dev_ctx, GFP_KERNEL);
56 dev_ctx->context = intf->add(&priv->dev);
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);
66 static void mlx4_remove_device(struct mlx4_interface *intf, struct mlx4_priv *priv)
68 struct mlx4_device_context *dev_ctx;
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);
76 intf->remove(&priv->dev, dev_ctx->context);
82 int mlx4_register_interface(struct mlx4_interface *intf)
84 struct mlx4_priv *priv;
86 if (!intf->add || !intf->remove)
89 mutex_lock(&intf_mutex);
91 list_add_tail(&intf->list, &intf_list);
92 list_for_each_entry(priv, &dev_list, dev_list)
93 mlx4_add_device(intf, priv);
95 mutex_unlock(&intf_mutex);
99 EXPORT_SYMBOL_GPL(mlx4_register_interface);
101 void mlx4_unregister_interface(struct mlx4_interface *intf)
103 struct mlx4_priv *priv;
105 mutex_lock(&intf_mutex);
107 list_for_each_entry(priv, &dev_list, dev_list)
108 mlx4_remove_device(intf, priv);
110 list_del(&intf->list);
112 mutex_unlock(&intf_mutex);
114 EXPORT_SYMBOL_GPL(mlx4_unregister_interface);
116 void mlx4_dispatch_event(struct mlx4_dev *dev, enum mlx4_event type,
117 int subtype, int port)
119 struct mlx4_priv *priv = mlx4_priv(dev);
120 struct mlx4_device_context *dev_ctx;
123 spin_lock_irqsave(&priv->ctx_lock, flags);
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,
130 spin_unlock_irqrestore(&priv->ctx_lock, flags);
133 int mlx4_register_device(struct mlx4_dev *dev)
135 struct mlx4_priv *priv = mlx4_priv(dev);
136 struct mlx4_interface *intf;
138 mutex_lock(&intf_mutex);
140 list_add_tail(&priv->dev_list, &dev_list);
141 list_for_each_entry(intf, &intf_list, list)
142 mlx4_add_device(intf, priv);
144 mutex_unlock(&intf_mutex);
145 mlx4_start_catas_poll(dev);
150 void mlx4_unregister_device(struct mlx4_dev *dev)
152 struct mlx4_priv *priv = mlx4_priv(dev);
153 struct mlx4_interface *intf;
155 mlx4_stop_catas_poll(dev);
156 mutex_lock(&intf_mutex);
158 list_for_each_entry(intf, &intf_list, list)
159 mlx4_remove_device(intf, priv);
161 list_del(&priv->dev_list);
163 mutex_unlock(&intf_mutex);