1/*
2 * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
4 *
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:
10 *
11 *     Redistribution and use in source and binary forms, with or
12 *     without modification, are permitted provided that the following
13 *     conditions are met:
14 *
15 *      - Redistributions of source code must retain the above
16 *        copyright notice, this list of conditions and the following
17 *        disclaimer.
18 *
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.
23 *
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
31 * SOFTWARE.
32 */
33
34#include "mlx4.h"
35
36struct mlx4_device_context {
37	struct list_head	list;
38	struct mlx4_interface  *intf;
39	void		       *context;
40};
41
42static LIST_HEAD(intf_list);
43static LIST_HEAD(dev_list);
44static DEFINE_MUTEX(intf_mutex);
45
46static void mlx4_add_device(struct mlx4_interface *intf, struct mlx4_priv *priv)
47{
48	struct mlx4_device_context *dev_ctx;
49
50	dev_ctx = kmalloc(sizeof *dev_ctx, GFP_KERNEL);
51	if (!dev_ctx)
52		return;
53
54	dev_ctx->intf    = intf;
55	dev_ctx->context = intf->add(&priv->dev);
56
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);
61	} else
62		kfree(dev_ctx);
63}
64
65static void mlx4_remove_device(struct mlx4_interface *intf, struct mlx4_priv *priv)
66{
67	struct mlx4_device_context *dev_ctx;
68
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);
74
75			intf->remove(&priv->dev, dev_ctx->context);
76			kfree(dev_ctx);
77			return;
78		}
79}
80
81int mlx4_register_interface(struct mlx4_interface *intf)
82{
83	struct mlx4_priv *priv;
84
85	if (!intf->add || !intf->remove)
86		return -EINVAL;
87
88	mutex_lock(&intf_mutex);
89
90	list_add_tail(&intf->list, &intf_list);
91	list_for_each_entry(priv, &dev_list, dev_list)
92		mlx4_add_device(intf, priv);
93
94	mutex_unlock(&intf_mutex);
95
96	return 0;
97}
98EXPORT_SYMBOL_GPL(mlx4_register_interface);
99
100void mlx4_unregister_interface(struct mlx4_interface *intf)
101{
102	struct mlx4_priv *priv;
103
104	mutex_lock(&intf_mutex);
105
106	list_for_each_entry(priv, &dev_list, dev_list)
107		mlx4_remove_device(intf, priv);
108
109	list_del(&intf->list);
110
111	mutex_unlock(&intf_mutex);
112}
113EXPORT_SYMBOL_GPL(mlx4_unregister_interface);
114
115struct mlx4_dev *mlx4_query_interface(void *int_dev, int *port)
116{
117	struct mlx4_priv *priv;
118	struct mlx4_device_context *dev_ctx;
119	enum mlx4_query_reply r;
120	unsigned long flags;
121
122	mutex_lock(&intf_mutex);
123
124	list_for_each_entry(priv, &dev_list, dev_list) {
125		spin_lock_irqsave(&priv->ctx_lock, flags);
126		list_for_each_entry(dev_ctx, &priv->ctx_list, list) {
127			if (!dev_ctx->intf->query)
128				continue;
129			r = dev_ctx->intf->query(dev_ctx->context, int_dev);
130			if (r != MLX4_QUERY_NOT_MINE) {
131				*port = r;
132				spin_unlock_irqrestore(&priv->ctx_lock, flags);
133				mutex_unlock(&intf_mutex);
134				return &priv->dev;
135			}
136		}
137		spin_unlock_irqrestore(&priv->ctx_lock, flags);
138	}
139
140	mutex_unlock(&intf_mutex);
141	return NULL;
142}
143EXPORT_SYMBOL_GPL(mlx4_query_interface);
144
145void mlx4_dispatch_event(struct mlx4_dev *dev, enum mlx4_dev_event type, int port)
146{
147	struct mlx4_priv *priv = mlx4_priv(dev);
148	struct mlx4_device_context *dev_ctx;
149	unsigned long flags;
150
151	spin_lock_irqsave(&priv->ctx_lock, flags);
152
153	list_for_each_entry(dev_ctx, &priv->ctx_list, list)
154		if (dev_ctx->intf->event)
155			dev_ctx->intf->event(dev, dev_ctx->context, type, port);
156
157	spin_unlock_irqrestore(&priv->ctx_lock, flags);
158}
159
160int mlx4_register_device(struct mlx4_dev *dev)
161{
162	struct mlx4_priv *priv = mlx4_priv(dev);
163	struct mlx4_interface *intf;
164
165	mutex_lock(&intf_mutex);
166
167	list_add_tail(&priv->dev_list, &dev_list);
168	list_for_each_entry(intf, &intf_list, list)
169		mlx4_add_device(intf, priv);
170
171	mutex_unlock(&intf_mutex);
172	mlx4_start_catas_poll(dev);
173
174	return 0;
175}
176
177void mlx4_unregister_device(struct mlx4_dev *dev)
178{
179	struct mlx4_priv *priv = mlx4_priv(dev);
180	struct mlx4_interface *intf;
181
182	mlx4_stop_catas_poll(dev);
183	mutex_lock(&intf_mutex);
184
185	list_for_each_entry(intf, &intf_list, list)
186		mlx4_remove_device(intf, priv);
187
188	list_del(&priv->dev_list);
189
190	mutex_unlock(&intf_mutex);
191}
192
193void *mlx4_find_get_prot_dev(struct mlx4_dev *dev, enum mlx4_prot proto, int port)
194{
195	struct mlx4_priv *priv = mlx4_priv(dev);
196	struct mlx4_device_context *dev_ctx;
197	unsigned long flags;
198	void *result = NULL;
199
200	spin_lock_irqsave(&priv->ctx_lock, flags);
201
202	list_for_each_entry(dev_ctx, &priv->ctx_list, list)
203		if (dev_ctx->intf->protocol == proto && dev_ctx->intf->get_prot_dev) {
204			result = dev_ctx->intf->get_prot_dev(dev, dev_ctx->context, port);
205			break;
206	}
207
208	spin_unlock_irqrestore(&priv->ctx_lock, flags);
209
210	return result;
211}
212
213