1219820Sjeff/*
2219820Sjeff * Copyright (c) 2004 Topspin Communications.  All rights reserved.
3219820Sjeff * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
4219820Sjeff *
5219820Sjeff * This software is available to you under a choice of one of two
6219820Sjeff * licenses.  You may choose to be licensed under the terms of the GNU
7219820Sjeff * General Public License (GPL) Version 2, available from the file
8219820Sjeff * COPYING in the main directory of this source tree, or the
9219820Sjeff * OpenIB.org BSD license below:
10219820Sjeff *
11219820Sjeff *     Redistribution and use in source and binary forms, with or
12219820Sjeff *     without modification, are permitted provided that the following
13219820Sjeff *     conditions are met:
14219820Sjeff *
15219820Sjeff *      - Redistributions of source code must retain the above
16219820Sjeff *        copyright notice, this list of conditions and the following
17219820Sjeff *        disclaimer.
18219820Sjeff *
19219820Sjeff *      - Redistributions in binary form must reproduce the above
20219820Sjeff *        copyright notice, this list of conditions and the following
21219820Sjeff *        disclaimer in the documentation and/or other materials
22219820Sjeff *        provided with the distribution.
23219820Sjeff *
24219820Sjeff * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25219820Sjeff * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26219820Sjeff * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27219820Sjeff * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28219820Sjeff * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29219820Sjeff * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30219820Sjeff * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31219820Sjeff * SOFTWARE.
32219820Sjeff */
33219820Sjeff
34219820Sjeff#include <linux/module.h>
35219820Sjeff#include <linux/string.h>
36219820Sjeff#include <linux/errno.h>
37219820Sjeff#include <linux/kernel.h>
38219820Sjeff#include <linux/slab.h>
39219820Sjeff#include <linux/mutex.h>
40219820Sjeff#include <linux/workqueue.h>
41219820Sjeff
42219820Sjeff#include "core_priv.h"
43219820Sjeff
44219820SjeffMODULE_AUTHOR("Roland Dreier");
45219820SjeffMODULE_DESCRIPTION("core kernel InfiniBand API");
46219820SjeffMODULE_LICENSE("Dual BSD/GPL");
47219820Sjeff
48219820Sjeff#ifdef __ia64__
49219820Sjeff/* workaround for a bug in hp chipset that would cause kernel
50219820Sjeff   panic when dma resources are exhaused */
51219820Sjeffint dma_map_sg_hp_wa = 0;
52219820Sjeff#endif
53219820Sjeff
54219820Sjeffstruct ib_client_data {
55219820Sjeff	struct list_head  list;
56219820Sjeff	struct ib_client *client;
57219820Sjeff	void *            data;
58219820Sjeff};
59219820Sjeff
60219820Sjeffstatic LIST_HEAD(device_list);
61219820Sjeffstatic LIST_HEAD(client_list);
62219820Sjeff
63219820Sjeff/*
64219820Sjeff * device_mutex protects access to both device_list and client_list.
65219820Sjeff * There's no real point to using multiple locks or something fancier
66219820Sjeff * like an rwsem: we always access both lists, and we're always
67219820Sjeff * modifying one list or the other list.  In any case this is not a
68219820Sjeff * hot path so there's no point in trying to optimize.
69219820Sjeff */
70219820Sjeffstatic DEFINE_MUTEX(device_mutex);
71219820Sjeff
72219820Sjeffstatic int ib_device_check_mandatory(struct ib_device *device)
73219820Sjeff{
74219820Sjeff#define IB_MANDATORY_FUNC(x) { offsetof(struct ib_device, x), #x }
75219820Sjeff	static const struct {
76219820Sjeff		size_t offset;
77219820Sjeff		char  *name;
78219820Sjeff	} mandatory_table[] = {
79219820Sjeff		IB_MANDATORY_FUNC(query_device),
80219820Sjeff		IB_MANDATORY_FUNC(query_port),
81219820Sjeff		IB_MANDATORY_FUNC(query_pkey),
82219820Sjeff		IB_MANDATORY_FUNC(query_gid),
83219820Sjeff		IB_MANDATORY_FUNC(alloc_pd),
84219820Sjeff		IB_MANDATORY_FUNC(dealloc_pd),
85219820Sjeff		IB_MANDATORY_FUNC(create_ah),
86219820Sjeff		IB_MANDATORY_FUNC(destroy_ah),
87219820Sjeff		IB_MANDATORY_FUNC(create_qp),
88219820Sjeff		IB_MANDATORY_FUNC(modify_qp),
89219820Sjeff		IB_MANDATORY_FUNC(destroy_qp),
90219820Sjeff		IB_MANDATORY_FUNC(post_send),
91219820Sjeff		IB_MANDATORY_FUNC(post_recv),
92219820Sjeff		IB_MANDATORY_FUNC(create_cq),
93219820Sjeff		IB_MANDATORY_FUNC(destroy_cq),
94219820Sjeff		IB_MANDATORY_FUNC(poll_cq),
95219820Sjeff		IB_MANDATORY_FUNC(req_notify_cq),
96219820Sjeff		IB_MANDATORY_FUNC(get_dma_mr),
97325611Shselasky		IB_MANDATORY_FUNC(dereg_mr),
98325611Shselasky		IB_MANDATORY_FUNC(get_port_immutable)
99219820Sjeff	};
100219820Sjeff	int i;
101219820Sjeff
102219820Sjeff	for (i = 0; i < ARRAY_SIZE(mandatory_table); ++i) {
103219820Sjeff		if (!*(void **) ((u_char *) device + mandatory_table[i].offset)) {
104219820Sjeff			printk(KERN_WARNING "Device %s is missing mandatory function %s\n",
105219820Sjeff			       device->name, mandatory_table[i].name);
106219820Sjeff			return -EINVAL;
107219820Sjeff		}
108219820Sjeff	}
109219820Sjeff
110219820Sjeff	return 0;
111219820Sjeff}
112219820Sjeff
113219820Sjeffstatic struct ib_device *__ib_device_get_by_name(const char *name)
114219820Sjeff{
115219820Sjeff	struct ib_device *device;
116219820Sjeff
117219820Sjeff	list_for_each_entry(device, &device_list, core_list)
118219820Sjeff		if (!strncmp(name, device->name, IB_DEVICE_NAME_MAX))
119219820Sjeff			return device;
120219820Sjeff
121219820Sjeff	return NULL;
122219820Sjeff}
123219820Sjeff
124219820Sjeff
125219820Sjeffstatic int alloc_name(char *name)
126219820Sjeff{
127219820Sjeff	unsigned long *inuse;
128219820Sjeff	char buf[IB_DEVICE_NAME_MAX];
129219820Sjeff	struct ib_device *device;
130219820Sjeff	int i;
131219820Sjeff
132219820Sjeff	inuse = (unsigned long *) get_zeroed_page(GFP_KERNEL);
133219820Sjeff	if (!inuse)
134219820Sjeff		return -ENOMEM;
135219820Sjeff
136219820Sjeff	list_for_each_entry(device, &device_list, core_list) {
137219820Sjeff		if (!sscanf(device->name, name, &i))
138219820Sjeff			continue;
139219820Sjeff		if (i < 0 || i >= PAGE_SIZE * 8)
140219820Sjeff			continue;
141219820Sjeff		snprintf(buf, sizeof buf, name, i);
142219820Sjeff		if (!strncmp(buf, device->name, IB_DEVICE_NAME_MAX))
143219820Sjeff			set_bit(i, inuse);
144219820Sjeff	}
145219820Sjeff
146219820Sjeff	i = find_first_zero_bit(inuse, PAGE_SIZE * 8);
147219820Sjeff	free_page((unsigned long) inuse);
148219820Sjeff	snprintf(buf, sizeof buf, name, i);
149219820Sjeff
150219820Sjeff	if (__ib_device_get_by_name(buf))
151219820Sjeff		return -ENFILE;
152219820Sjeff
153219820Sjeff	strlcpy(name, buf, IB_DEVICE_NAME_MAX);
154219820Sjeff	return 0;
155219820Sjeff}
156219820Sjeff
157325611Shselaskystatic int rdma_start_port(struct ib_device *device)
158219820Sjeff{
159219820Sjeff	return (device->node_type == RDMA_NODE_IB_SWITCH) ? 0 : 1;
160219820Sjeff}
161219820Sjeff
162219820Sjeff
163325611Shselaskystatic int rdma_end_port(struct ib_device *device)
164219820Sjeff{
165219820Sjeff	return (device->node_type == RDMA_NODE_IB_SWITCH) ?
166219820Sjeff		0 : device->phys_port_cnt;
167219820Sjeff}
168219820Sjeff
169219820Sjeff/**
170219820Sjeff * ib_alloc_device - allocate an IB device struct
171219820Sjeff * @size:size of structure to allocate
172219820Sjeff *
173219820Sjeff * Low-level drivers should use ib_alloc_device() to allocate &struct
174219820Sjeff * ib_device.  @size is the size of the structure to be allocated,
175219820Sjeff * including any private data used by the low-level driver.
176219820Sjeff * ib_dealloc_device() must be used to free structures allocated with
177219820Sjeff * ib_alloc_device().
178219820Sjeff */
179219820Sjeffstruct ib_device *ib_alloc_device(size_t size)
180219820Sjeff{
181219820Sjeff	BUG_ON(size < sizeof (struct ib_device));
182219820Sjeff
183219820Sjeff	return kzalloc(size, GFP_KERNEL);
184219820Sjeff}
185219820SjeffEXPORT_SYMBOL(ib_alloc_device);
186219820Sjeff
187219820Sjeff/**
188219820Sjeff * ib_dealloc_device - free an IB device struct
189219820Sjeff * @device:structure to free
190219820Sjeff *
191219820Sjeff * Free a structure allocated with ib_alloc_device().
192219820Sjeff */
193219820Sjeffvoid ib_dealloc_device(struct ib_device *device)
194219820Sjeff{
195219820Sjeff	if (device->reg_state == IB_DEV_UNINITIALIZED) {
196325611Shselasky		kfree(device->port_immutable);
197219820Sjeff		kfree(device);
198219820Sjeff		return;
199219820Sjeff	}
200219820Sjeff
201219820Sjeff	BUG_ON(device->reg_state != IB_DEV_UNREGISTERED);
202219820Sjeff
203219820Sjeff	kobject_put(&device->dev.kobj);
204219820Sjeff}
205219820SjeffEXPORT_SYMBOL(ib_dealloc_device);
206219820Sjeff
207219820Sjeffstatic int add_client_context(struct ib_device *device, struct ib_client *client)
208219820Sjeff{
209219820Sjeff	struct ib_client_data *context;
210219820Sjeff	unsigned long flags;
211219820Sjeff
212219820Sjeff	context = kmalloc(sizeof *context, GFP_KERNEL);
213219820Sjeff	if (!context) {
214219820Sjeff		printk(KERN_WARNING "Couldn't allocate client context for %s/%s\n",
215219820Sjeff		       device->name, client->name);
216219820Sjeff		return -ENOMEM;
217219820Sjeff	}
218219820Sjeff
219219820Sjeff	context->client = client;
220219820Sjeff	context->data   = NULL;
221219820Sjeff
222219820Sjeff	spin_lock_irqsave(&device->client_data_lock, flags);
223219820Sjeff	list_add(&context->list, &device->client_data_list);
224219820Sjeff	spin_unlock_irqrestore(&device->client_data_lock, flags);
225219820Sjeff
226219820Sjeff	return 0;
227219820Sjeff}
228219820Sjeff
229325611Shselaskystatic int verify_immutable(const struct ib_device *dev, u8 port)
230219820Sjeff{
231325611Shselasky	return WARN_ON(!rdma_cap_ib_mad(dev, port) &&
232325611Shselasky			    rdma_max_mad_size(dev, port) != 0);
233325611Shselasky}
234219820Sjeff
235325611Shselaskystatic int read_port_immutable(struct ib_device *device)
236325611Shselasky{
237325611Shselasky	int ret;
238325611Shselasky	u8 start_port = rdma_start_port(device);
239325611Shselasky	u8 end_port = rdma_end_port(device);
240325611Shselasky	u8 port;
241219820Sjeff
242325611Shselasky	/**
243325611Shselasky	 * device->port_immutable is indexed directly by the port number to make
244325611Shselasky	 * access to this data as efficient as possible.
245325611Shselasky	 *
246325611Shselasky	 * Therefore port_immutable is declared as a 1 based array with
247325611Shselasky	 * potential empty slots at the beginning.
248325611Shselasky	 */
249325611Shselasky	device->port_immutable = kzalloc(sizeof(*device->port_immutable)
250325611Shselasky					 * (end_port + 1),
251325611Shselasky					 GFP_KERNEL);
252325611Shselasky	if (!device->port_immutable)
253325611Shselasky		return -ENOMEM;
254219820Sjeff
255325611Shselasky	for (port = start_port; port <= end_port; ++port) {
256325611Shselasky		ret = device->get_port_immutable(device, port,
257325611Shselasky						 &device->port_immutable[port]);
258325611Shselasky		if (ret)
259325611Shselasky			return ret;
260219820Sjeff
261325611Shselasky		if (verify_immutable(device, port))
262325611Shselasky			return -EINVAL;
263219820Sjeff	}
264325611Shselasky	return 0;
265219820Sjeff}
266219820Sjeff
267219820Sjeff/**
268219820Sjeff * ib_register_device - Register an IB device with IB core
269219820Sjeff * @device:Device to register
270219820Sjeff *
271219820Sjeff * Low-level drivers use ib_register_device() to register their
272219820Sjeff * devices with the IB core.  All registered clients will receive a
273219820Sjeff * callback for each device that is added. @device must be allocated
274219820Sjeff * with ib_alloc_device().
275219820Sjeff */
276255932Salfredint ib_register_device(struct ib_device *device,
277255932Salfred		       int (*port_callback)(struct ib_device *,
278255932Salfred					    u8, struct kobject *))
279219820Sjeff{
280219820Sjeff	int ret;
281219820Sjeff
282219820Sjeff	mutex_lock(&device_mutex);
283219820Sjeff
284219820Sjeff	if (strchr(device->name, '%')) {
285219820Sjeff		ret = alloc_name(device->name);
286219820Sjeff		if (ret)
287219820Sjeff			goto out;
288219820Sjeff	}
289219820Sjeff
290219820Sjeff	if (ib_device_check_mandatory(device)) {
291219820Sjeff		ret = -EINVAL;
292219820Sjeff		goto out;
293219820Sjeff	}
294219820Sjeff
295219820Sjeff	INIT_LIST_HEAD(&device->event_handler_list);
296219820Sjeff	INIT_LIST_HEAD(&device->client_data_list);
297219820Sjeff	spin_lock_init(&device->event_handler_lock);
298219820Sjeff	spin_lock_init(&device->client_data_lock);
299255973Salfred	device->ib_uverbs_xrcd_table = RB_ROOT;
300255973Salfred	mutex_init(&device->xrcd_table_mutex);
301219820Sjeff
302325611Shselasky
303325611Shselasky	ret = read_port_immutable(device);
304219820Sjeff	if (ret) {
305325611Shselasky		printk(KERN_WARNING "Couldn't create per port immutable data %s\n",
306325611Shselasky			device->name);
307219820Sjeff		goto out;
308219820Sjeff	}
309219820Sjeff
310255932Salfred	ret = ib_device_register_sysfs(device, port_callback);
311219820Sjeff	if (ret) {
312219820Sjeff		printk(KERN_WARNING "Couldn't register device %s with driver model\n",
313219820Sjeff		       device->name);
314325611Shselasky		kfree(device->port_immutable);
315219820Sjeff		goto out;
316219820Sjeff	}
317219820Sjeff
318219820Sjeff	list_add_tail(&device->core_list, &device_list);
319219820Sjeff
320219820Sjeff	device->reg_state = IB_DEV_REGISTERED;
321219820Sjeff
322219820Sjeff	{
323219820Sjeff		struct ib_client *client;
324219820Sjeff
325219820Sjeff		list_for_each_entry(client, &client_list, list)
326219820Sjeff			if (client->add && !add_client_context(device, client))
327219820Sjeff				client->add(device);
328219820Sjeff	}
329219820Sjeff
330219820Sjeff out:
331219820Sjeff	mutex_unlock(&device_mutex);
332219820Sjeff	return ret;
333219820Sjeff}
334219820SjeffEXPORT_SYMBOL(ib_register_device);
335219820Sjeff
336219820Sjeff/**
337219820Sjeff * ib_unregister_device - Unregister an IB device
338219820Sjeff * @device:Device to unregister
339219820Sjeff *
340219820Sjeff * Unregister an IB device.  All clients will receive a remove callback.
341219820Sjeff */
342219820Sjeffvoid ib_unregister_device(struct ib_device *device)
343219820Sjeff{
344219820Sjeff	struct ib_client *client;
345219820Sjeff	struct ib_client_data *context, *tmp;
346219820Sjeff	unsigned long flags;
347219820Sjeff
348219820Sjeff	mutex_lock(&device_mutex);
349219820Sjeff
350219820Sjeff	list_for_each_entry_reverse(client, &client_list, list)
351219820Sjeff		if (client->remove)
352219820Sjeff			client->remove(device);
353219820Sjeff
354219820Sjeff	list_del(&device->core_list);
355219820Sjeff
356219820Sjeff	mutex_unlock(&device_mutex);
357219820Sjeff
358219820Sjeff	ib_device_unregister_sysfs(device);
359219820Sjeff
360219820Sjeff	spin_lock_irqsave(&device->client_data_lock, flags);
361219820Sjeff	list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
362219820Sjeff		kfree(context);
363219820Sjeff	spin_unlock_irqrestore(&device->client_data_lock, flags);
364219820Sjeff
365219820Sjeff	device->reg_state = IB_DEV_UNREGISTERED;
366219820Sjeff}
367219820SjeffEXPORT_SYMBOL(ib_unregister_device);
368219820Sjeff
369219820Sjeff/**
370219820Sjeff * ib_register_client - Register an IB client
371219820Sjeff * @client:Client to register
372219820Sjeff *
373219820Sjeff * Upper level users of the IB drivers can use ib_register_client() to
374219820Sjeff * register callbacks for IB device addition and removal.  When an IB
375219820Sjeff * device is added, each registered client's add method will be called
376219820Sjeff * (in the order the clients were registered), and when a device is
377219820Sjeff * removed, each client's remove method will be called (in the reverse
378219820Sjeff * order that clients were registered).  In addition, when
379219820Sjeff * ib_register_client() is called, the client will receive an add
380219820Sjeff * callback for all devices already registered.
381219820Sjeff */
382219820Sjeffint ib_register_client(struct ib_client *client)
383219820Sjeff{
384219820Sjeff	struct ib_device *device;
385219820Sjeff
386219820Sjeff	mutex_lock(&device_mutex);
387219820Sjeff
388219820Sjeff	list_add_tail(&client->list, &client_list);
389219820Sjeff	list_for_each_entry(device, &device_list, core_list)
390219820Sjeff		if (client->add && !add_client_context(device, client))
391219820Sjeff			client->add(device);
392219820Sjeff
393219820Sjeff	mutex_unlock(&device_mutex);
394219820Sjeff
395219820Sjeff	return 0;
396219820Sjeff}
397219820SjeffEXPORT_SYMBOL(ib_register_client);
398219820Sjeff
399219820Sjeff/**
400219820Sjeff * ib_unregister_client - Unregister an IB client
401219820Sjeff * @client:Client to unregister
402219820Sjeff *
403219820Sjeff * Upper level users use ib_unregister_client() to remove their client
404219820Sjeff * registration.  When ib_unregister_client() is called, the client
405219820Sjeff * will receive a remove callback for each IB device still registered.
406219820Sjeff */
407219820Sjeffvoid ib_unregister_client(struct ib_client *client)
408219820Sjeff{
409219820Sjeff	struct ib_client_data *context, *tmp;
410219820Sjeff	struct ib_device *device;
411219820Sjeff	unsigned long flags;
412219820Sjeff
413219820Sjeff	mutex_lock(&device_mutex);
414219820Sjeff
415219820Sjeff	list_for_each_entry(device, &device_list, core_list) {
416219820Sjeff		if (client->remove)
417219820Sjeff			client->remove(device);
418219820Sjeff
419219820Sjeff		spin_lock_irqsave(&device->client_data_lock, flags);
420219820Sjeff		list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
421219820Sjeff			if (context->client == client) {
422219820Sjeff				list_del(&context->list);
423219820Sjeff				kfree(context);
424219820Sjeff			}
425219820Sjeff		spin_unlock_irqrestore(&device->client_data_lock, flags);
426219820Sjeff	}
427219820Sjeff	list_del(&client->list);
428219820Sjeff
429219820Sjeff	mutex_unlock(&device_mutex);
430219820Sjeff}
431219820SjeffEXPORT_SYMBOL(ib_unregister_client);
432219820Sjeff
433219820Sjeff/**
434219820Sjeff * ib_get_client_data - Get IB client context
435219820Sjeff * @device:Device to get context for
436219820Sjeff * @client:Client to get context for
437219820Sjeff *
438219820Sjeff * ib_get_client_data() returns client context set with
439219820Sjeff * ib_set_client_data().
440219820Sjeff */
441219820Sjeffvoid *ib_get_client_data(struct ib_device *device, struct ib_client *client)
442219820Sjeff{
443219820Sjeff	struct ib_client_data *context;
444219820Sjeff	void *ret = NULL;
445219820Sjeff	unsigned long flags;
446219820Sjeff
447219820Sjeff	spin_lock_irqsave(&device->client_data_lock, flags);
448219820Sjeff	list_for_each_entry(context, &device->client_data_list, list)
449219820Sjeff		if (context->client == client) {
450219820Sjeff			ret = context->data;
451219820Sjeff			break;
452219820Sjeff		}
453219820Sjeff	spin_unlock_irqrestore(&device->client_data_lock, flags);
454219820Sjeff
455219820Sjeff	return ret;
456219820Sjeff}
457219820SjeffEXPORT_SYMBOL(ib_get_client_data);
458219820Sjeff
459219820Sjeff/**
460219820Sjeff * ib_set_client_data - Set IB client context
461219820Sjeff * @device:Device to set context for
462219820Sjeff * @client:Client to set context for
463219820Sjeff * @data:Context to set
464219820Sjeff *
465219820Sjeff * ib_set_client_data() sets client context that can be retrieved with
466219820Sjeff * ib_get_client_data().
467219820Sjeff */
468219820Sjeffvoid ib_set_client_data(struct ib_device *device, struct ib_client *client,
469219820Sjeff			void *data)
470219820Sjeff{
471219820Sjeff	struct ib_client_data *context;
472219820Sjeff	unsigned long flags;
473219820Sjeff
474219820Sjeff	spin_lock_irqsave(&device->client_data_lock, flags);
475219820Sjeff	list_for_each_entry(context, &device->client_data_list, list)
476219820Sjeff		if (context->client == client) {
477219820Sjeff			context->data = data;
478219820Sjeff			goto out;
479219820Sjeff		}
480219820Sjeff
481219820Sjeff	printk(KERN_WARNING "No client context found for %s/%s\n",
482219820Sjeff	       device->name, client->name);
483219820Sjeff
484219820Sjeffout:
485219820Sjeff	spin_unlock_irqrestore(&device->client_data_lock, flags);
486219820Sjeff}
487219820SjeffEXPORT_SYMBOL(ib_set_client_data);
488219820Sjeff
489219820Sjeff/**
490219820Sjeff * ib_register_event_handler - Register an IB event handler
491219820Sjeff * @event_handler:Handler to register
492219820Sjeff *
493219820Sjeff * ib_register_event_handler() registers an event handler that will be
494219820Sjeff * called back when asynchronous IB events occur (as defined in
495219820Sjeff * chapter 11 of the InfiniBand Architecture Specification).  This
496219820Sjeff * callback may occur in interrupt context.
497219820Sjeff */
498219820Sjeffint ib_register_event_handler  (struct ib_event_handler *event_handler)
499219820Sjeff{
500219820Sjeff	unsigned long flags;
501219820Sjeff
502219820Sjeff	spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
503219820Sjeff	list_add_tail(&event_handler->list,
504219820Sjeff		      &event_handler->device->event_handler_list);
505219820Sjeff	spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
506219820Sjeff
507219820Sjeff	return 0;
508219820Sjeff}
509219820SjeffEXPORT_SYMBOL(ib_register_event_handler);
510219820Sjeff
511219820Sjeff/**
512219820Sjeff * ib_unregister_event_handler - Unregister an event handler
513219820Sjeff * @event_handler:Handler to unregister
514219820Sjeff *
515219820Sjeff * Unregister an event handler registered with
516219820Sjeff * ib_register_event_handler().
517219820Sjeff */
518219820Sjeffint ib_unregister_event_handler(struct ib_event_handler *event_handler)
519219820Sjeff{
520219820Sjeff	unsigned long flags;
521219820Sjeff
522219820Sjeff	spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
523219820Sjeff	list_del(&event_handler->list);
524219820Sjeff	spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
525219820Sjeff
526219820Sjeff	return 0;
527219820Sjeff}
528219820SjeffEXPORT_SYMBOL(ib_unregister_event_handler);
529219820Sjeff
530219820Sjeff/**
531219820Sjeff * ib_dispatch_event - Dispatch an asynchronous event
532219820Sjeff * @event:Event to dispatch
533219820Sjeff *
534219820Sjeff * Low-level drivers must call ib_dispatch_event() to dispatch the
535219820Sjeff * event to all registered event handlers when an asynchronous event
536219820Sjeff * occurs.
537219820Sjeff */
538219820Sjeffvoid ib_dispatch_event(struct ib_event *event)
539219820Sjeff{
540219820Sjeff	unsigned long flags;
541219820Sjeff	struct ib_event_handler *handler;
542219820Sjeff
543219820Sjeff	spin_lock_irqsave(&event->device->event_handler_lock, flags);
544219820Sjeff
545219820Sjeff	list_for_each_entry(handler, &event->device->event_handler_list, list)
546219820Sjeff		handler->handler(handler, event);
547219820Sjeff
548219820Sjeff	spin_unlock_irqrestore(&event->device->event_handler_lock, flags);
549219820Sjeff}
550219820SjeffEXPORT_SYMBOL(ib_dispatch_event);
551219820Sjeff
552219820Sjeff/**
553219820Sjeff * ib_query_device - Query IB device attributes
554219820Sjeff * @device:Device to query
555219820Sjeff * @device_attr:Device attributes
556219820Sjeff *
557219820Sjeff * ib_query_device() returns the attributes of a device through the
558219820Sjeff * @device_attr pointer.
559219820Sjeff */
560219820Sjeffint ib_query_device(struct ib_device *device,
561219820Sjeff		    struct ib_device_attr *device_attr)
562219820Sjeff{
563219820Sjeff	return device->query_device(device, device_attr);
564219820Sjeff}
565219820SjeffEXPORT_SYMBOL(ib_query_device);
566219820Sjeff
567219820Sjeff/**
568219820Sjeff * ib_query_port - Query IB port attributes
569219820Sjeff * @device:Device to query
570219820Sjeff * @port_num:Port number to query
571219820Sjeff * @port_attr:Port attributes
572219820Sjeff *
573219820Sjeff * ib_query_port() returns the attributes of a port through the
574219820Sjeff * @port_attr pointer.
575219820Sjeff */
576219820Sjeffint ib_query_port(struct ib_device *device,
577219820Sjeff		  u8 port_num,
578219820Sjeff		  struct ib_port_attr *port_attr)
579219820Sjeff{
580325611Shselasky	if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
581219820Sjeff		return -EINVAL;
582219820Sjeff
583219820Sjeff	return device->query_port(device, port_num, port_attr);
584219820Sjeff}
585219820SjeffEXPORT_SYMBOL(ib_query_port);
586219820Sjeff
587219820Sjeff/**
588219820Sjeff * ib_query_gid - Get GID table entry
589219820Sjeff * @device:Device to query
590219820Sjeff * @port_num:Port number to query
591219820Sjeff * @index:GID table index to query
592219820Sjeff * @gid:Returned GID
593219820Sjeff *
594219820Sjeff * ib_query_gid() fetches the specified GID table entry.
595219820Sjeff */
596219820Sjeffint ib_query_gid(struct ib_device *device,
597219820Sjeff		 u8 port_num, int index, union ib_gid *gid)
598219820Sjeff{
599219820Sjeff	return device->query_gid(device, port_num, index, gid);
600219820Sjeff}
601219820SjeffEXPORT_SYMBOL(ib_query_gid);
602219820Sjeff
603219820Sjeff/**
604219820Sjeff * ib_query_pkey - Get P_Key table entry
605219820Sjeff * @device:Device to query
606219820Sjeff * @port_num:Port number to query
607219820Sjeff * @index:P_Key table index to query
608219820Sjeff * @pkey:Returned P_Key
609219820Sjeff *
610219820Sjeff * ib_query_pkey() fetches the specified P_Key table entry.
611219820Sjeff */
612219820Sjeffint ib_query_pkey(struct ib_device *device,
613219820Sjeff		  u8 port_num, u16 index, u16 *pkey)
614219820Sjeff{
615219820Sjeff	return device->query_pkey(device, port_num, index, pkey);
616219820Sjeff}
617219820SjeffEXPORT_SYMBOL(ib_query_pkey);
618219820Sjeff
619219820Sjeff/**
620219820Sjeff * ib_modify_device - Change IB device attributes
621219820Sjeff * @device:Device to modify
622219820Sjeff * @device_modify_mask:Mask of attributes to change
623219820Sjeff * @device_modify:New attribute values
624219820Sjeff *
625219820Sjeff * ib_modify_device() changes a device's attributes as specified by
626219820Sjeff * the @device_modify_mask and @device_modify structure.
627219820Sjeff */
628219820Sjeffint ib_modify_device(struct ib_device *device,
629219820Sjeff		     int device_modify_mask,
630219820Sjeff		     struct ib_device_modify *device_modify)
631219820Sjeff{
632219820Sjeff	return device->modify_device(device, device_modify_mask,
633219820Sjeff				     device_modify);
634219820Sjeff}
635219820SjeffEXPORT_SYMBOL(ib_modify_device);
636219820Sjeff
637219820Sjeff/**
638219820Sjeff * ib_modify_port - Modifies the attributes for the specified port.
639219820Sjeff * @device: The device to modify.
640219820Sjeff * @port_num: The number of the port to modify.
641219820Sjeff * @port_modify_mask: Mask used to specify which attributes of the port
642219820Sjeff *   to change.
643219820Sjeff * @port_modify: New attribute values for the port.
644219820Sjeff *
645219820Sjeff * ib_modify_port() changes a port's attributes as specified by the
646219820Sjeff * @port_modify_mask and @port_modify structure.
647219820Sjeff */
648219820Sjeffint ib_modify_port(struct ib_device *device,
649219820Sjeff		   u8 port_num, int port_modify_mask,
650219820Sjeff		   struct ib_port_modify *port_modify)
651219820Sjeff{
652325611Shselasky	if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
653219820Sjeff		return -EINVAL;
654219820Sjeff
655219820Sjeff	return device->modify_port(device, port_num, port_modify_mask,
656219820Sjeff				   port_modify);
657219820Sjeff}
658219820SjeffEXPORT_SYMBOL(ib_modify_port);
659219820Sjeff
660219820Sjeff/**
661219820Sjeff * ib_find_gid - Returns the port number and GID table index where
662219820Sjeff *   a specified GID value occurs.
663219820Sjeff * @device: The device to query.
664219820Sjeff * @gid: The GID value to search for.
665219820Sjeff * @port_num: The port number of the device where the GID value was found.
666219820Sjeff * @index: The index into the GID table where the GID was found.  This
667219820Sjeff *   parameter may be NULL.
668219820Sjeff */
669219820Sjeffint ib_find_gid(struct ib_device *device, union ib_gid *gid,
670219820Sjeff		u8 *port_num, u16 *index)
671219820Sjeff{
672219820Sjeff	union ib_gid tmp_gid;
673219820Sjeff	int ret, port, i;
674219820Sjeff
675325611Shselasky	for (port = rdma_start_port(device); port <= rdma_end_port(device); ++port) {
676325611Shselasky		for (i = 0; i < device->port_immutable[port].gid_tbl_len; ++i) {
677219820Sjeff			ret = ib_query_gid(device, port, i, &tmp_gid);
678219820Sjeff			if (ret)
679219820Sjeff				return ret;
680219820Sjeff			if (!memcmp(&tmp_gid, gid, sizeof *gid)) {
681219820Sjeff				*port_num = port;
682219820Sjeff				if (index)
683219820Sjeff					*index = i;
684219820Sjeff				return 0;
685219820Sjeff			}
686219820Sjeff		}
687219820Sjeff	}
688219820Sjeff
689219820Sjeff	return -ENOENT;
690219820Sjeff}
691219820SjeffEXPORT_SYMBOL(ib_find_gid);
692219820Sjeff
693219820Sjeff/**
694219820Sjeff * ib_find_pkey - Returns the PKey table index where a specified
695219820Sjeff *   PKey value occurs.
696219820Sjeff * @device: The device to query.
697219820Sjeff * @port_num: The port number of the device to search for the PKey.
698219820Sjeff * @pkey: The PKey value to search for.
699219820Sjeff * @index: The index into the PKey table where the PKey was found.
700219820Sjeff */
701219820Sjeffint ib_find_pkey(struct ib_device *device,
702219820Sjeff		 u8 port_num, u16 pkey, u16 *index)
703219820Sjeff{
704219820Sjeff	int ret, i;
705219820Sjeff	u16 tmp_pkey;
706219820Sjeff
707325611Shselasky	for (i = 0; i < device->port_immutable[port_num].pkey_tbl_len; ++i) {
708219820Sjeff		ret = ib_query_pkey(device, port_num, i, &tmp_pkey);
709219820Sjeff		if (ret)
710219820Sjeff			return ret;
711219820Sjeff
712219820Sjeff		if ((pkey & 0x7fff) == (tmp_pkey & 0x7fff)) {
713219820Sjeff			*index = i;
714219820Sjeff			return 0;
715219820Sjeff		}
716219820Sjeff	}
717219820Sjeff
718219820Sjeff	return -ENOENT;
719219820Sjeff}
720219820SjeffEXPORT_SYMBOL(ib_find_pkey);
721219820Sjeff
722219820Sjeffstatic int __init ib_core_init(void)
723219820Sjeff{
724219820Sjeff	int ret;
725219820Sjeff
726219820Sjeff#ifdef __ia64__
727219820Sjeff	if (ia64_platform_is("hpzx1"))
728219820Sjeff		dma_map_sg_hp_wa = 1;
729219820Sjeff#endif
730219820Sjeff
731219820Sjeff	ret = ib_sysfs_setup();
732219820Sjeff	if (ret)
733219820Sjeff		printk(KERN_WARNING "Couldn't create InfiniBand device class\n");
734219820Sjeff
735219820Sjeff	ret = ib_cache_setup();
736219820Sjeff	if (ret) {
737219820Sjeff		printk(KERN_WARNING "Couldn't set up InfiniBand P_Key/GID cache\n");
738219820Sjeff		ib_sysfs_cleanup();
739219820Sjeff	}
740219820Sjeff
741219820Sjeff	return ret;
742219820Sjeff}
743219820Sjeff
744219820Sjeffstatic void __exit ib_core_cleanup(void)
745219820Sjeff{
746219820Sjeff	ib_cache_cleanup();
747219820Sjeff	ib_sysfs_cleanup();
748219820Sjeff	/* Make sure that any pending umem accounting work is done. */
749219820Sjeff	flush_scheduled_work();
750219820Sjeff}
751219820Sjeff
752219820Sjeffmodule_init(ib_core_init);
753219820Sjeffmodule_exit(ib_core_cleanup);
754255932Salfred
755255932Salfred#undef MODULE_VERSION
756255932Salfred#include <sys/module.h>
757255932Salfredstatic int
758255932Salfredibcore_evhand(module_t mod, int event, void *arg)
759255932Salfred{
760255932Salfred	return (0);
761255932Salfred}
762255932Salfred
763255932Salfredstatic moduledata_t ibcore_mod = {
764255932Salfred	.name = "ibcore",
765255932Salfred	.evhand = ibcore_evhand,
766255932Salfred};
767255932Salfred
768255932SalfredMODULE_VERSION(ibcore, 1);
769255932SalfredDECLARE_MODULE(ibcore, ibcore_mod, SI_SUB_SMP, SI_ORDER_ANY);
770325940ShselaskyMODULE_DEPEND(ibcore, toecore, 1, 1, 1);
771