ia64/kvm: compilation fix. export account_system_vtime.
[pv_ops_mirror.git] / drivers / net / mlx4 / intf.c
blob4a6c4d526f1b51e72dad933fed05558faf8f57e5
1 /*
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
12 * conditions are met:
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
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
30 * SOFTWARE.
33 #include "mlx4.h"
35 struct mlx4_device_context {
36 struct list_head list;
37 struct mlx4_interface *intf;
38 void *context;
41 static LIST_HEAD(intf_list);
42 static LIST_HEAD(dev_list);
43 static DEFINE_MUTEX(intf_mutex);
45 static void mlx4_add_device(struct mlx4_interface *intf, struct mlx4_priv *priv)
47 struct mlx4_device_context *dev_ctx;
49 dev_ctx = kmalloc(sizeof *dev_ctx, GFP_KERNEL);
50 if (!dev_ctx)
51 return;
53 dev_ctx->intf = intf;
54 dev_ctx->context = intf->add(&priv->dev);
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);
64 static void mlx4_remove_device(struct mlx4_interface *intf, struct mlx4_priv *priv)
66 struct mlx4_device_context *dev_ctx;
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);
74 intf->remove(&priv->dev, dev_ctx->context);
75 kfree(dev_ctx);
76 return;
80 int mlx4_register_interface(struct mlx4_interface *intf)
82 struct mlx4_priv *priv;
84 if (!intf->add || !intf->remove)
85 return -EINVAL;
87 mutex_lock(&intf_mutex);
89 list_add_tail(&intf->list, &intf_list);
90 list_for_each_entry(priv, &dev_list, dev_list)
91 mlx4_add_device(intf, priv);
93 mutex_unlock(&intf_mutex);
95 return 0;
97 EXPORT_SYMBOL_GPL(mlx4_register_interface);
99 void mlx4_unregister_interface(struct mlx4_interface *intf)
101 struct mlx4_priv *priv;
103 mutex_lock(&intf_mutex);
105 list_for_each_entry(priv, &dev_list, dev_list)
106 mlx4_remove_device(intf, priv);
108 list_del(&intf->list);
110 mutex_unlock(&intf_mutex);
112 EXPORT_SYMBOL_GPL(mlx4_unregister_interface);
114 void mlx4_dispatch_event(struct mlx4_dev *dev, enum mlx4_dev_event type, int port)
116 struct mlx4_priv *priv = mlx4_priv(dev);
117 struct mlx4_device_context *dev_ctx;
118 unsigned long flags;
120 spin_lock_irqsave(&priv->ctx_lock, flags);
122 list_for_each_entry(dev_ctx, &priv->ctx_list, list)
123 if (dev_ctx->intf->event)
124 dev_ctx->intf->event(dev, dev_ctx->context, type, port);
126 spin_unlock_irqrestore(&priv->ctx_lock, flags);
129 int mlx4_register_device(struct mlx4_dev *dev)
131 struct mlx4_priv *priv = mlx4_priv(dev);
132 struct mlx4_interface *intf;
134 mutex_lock(&intf_mutex);
136 list_add_tail(&priv->dev_list, &dev_list);
137 list_for_each_entry(intf, &intf_list, list)
138 mlx4_add_device(intf, priv);
140 mutex_unlock(&intf_mutex);
141 mlx4_start_catas_poll(dev);
143 return 0;
146 void mlx4_unregister_device(struct mlx4_dev *dev)
148 struct mlx4_priv *priv = mlx4_priv(dev);
149 struct mlx4_interface *intf;
151 mlx4_stop_catas_poll(dev);
152 mutex_lock(&intf_mutex);
154 list_for_each_entry(intf, &intf_list, list)
155 mlx4_remove_device(intf, priv);
157 list_del(&priv->dev_list);
159 mutex_unlock(&intf_mutex);