device.c revision 255973
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/init.h>
40219820Sjeff#include <linux/mutex.h>
41219820Sjeff#include <linux/workqueue.h>
42219820Sjeff
43219820Sjeff#include "core_priv.h"
44219820Sjeff
45219820SjeffMODULE_AUTHOR("Roland Dreier");
46219820SjeffMODULE_DESCRIPTION("core kernel InfiniBand API");
47219820SjeffMODULE_LICENSE("Dual BSD/GPL");
48219820Sjeff
49219820Sjeff#ifdef __ia64__
50219820Sjeff/* workaround for a bug in hp chipset that would cause kernel
51219820Sjeff   panic when dma resources are exhaused */
52219820Sjeffint dma_map_sg_hp_wa = 0;
53219820Sjeff#endif
54219820Sjeff
55219820Sjeffstruct ib_client_data {
56219820Sjeff	struct list_head  list;
57219820Sjeff	struct ib_client *client;
58219820Sjeff	void *            data;
59219820Sjeff};
60219820Sjeff
61219820Sjeffstatic LIST_HEAD(device_list);
62219820Sjeffstatic LIST_HEAD(client_list);
63219820Sjeff
64219820Sjeff/*
65219820Sjeff * device_mutex protects access to both device_list and client_list.
66219820Sjeff * There's no real point to using multiple locks or something fancier
67219820Sjeff * like an rwsem: we always access both lists, and we're always
68219820Sjeff * modifying one list or the other list.  In any case this is not a
69219820Sjeff * hot path so there's no point in trying to optimize.
70219820Sjeff */
71219820Sjeffstatic DEFINE_MUTEX(device_mutex);
72219820Sjeff
73219820Sjeffstatic int ib_device_check_mandatory(struct ib_device *device)
74219820Sjeff{
75219820Sjeff#define IB_MANDATORY_FUNC(x) { offsetof(struct ib_device, x), #x }
76219820Sjeff	static const struct {
77219820Sjeff		size_t offset;
78219820Sjeff		char  *name;
79219820Sjeff	} mandatory_table[] = {
80219820Sjeff		IB_MANDATORY_FUNC(query_device),
81219820Sjeff		IB_MANDATORY_FUNC(query_port),
82219820Sjeff		IB_MANDATORY_FUNC(query_pkey),
83219820Sjeff		IB_MANDATORY_FUNC(query_gid),
84219820Sjeff		IB_MANDATORY_FUNC(alloc_pd),
85219820Sjeff		IB_MANDATORY_FUNC(dealloc_pd),
86219820Sjeff		IB_MANDATORY_FUNC(create_ah),
87219820Sjeff		IB_MANDATORY_FUNC(destroy_ah),
88219820Sjeff		IB_MANDATORY_FUNC(create_qp),
89219820Sjeff		IB_MANDATORY_FUNC(modify_qp),
90219820Sjeff		IB_MANDATORY_FUNC(destroy_qp),
91219820Sjeff		IB_MANDATORY_FUNC(post_send),
92219820Sjeff		IB_MANDATORY_FUNC(post_recv),
93219820Sjeff		IB_MANDATORY_FUNC(create_cq),
94219820Sjeff		IB_MANDATORY_FUNC(destroy_cq),
95219820Sjeff		IB_MANDATORY_FUNC(poll_cq),
96219820Sjeff		IB_MANDATORY_FUNC(req_notify_cq),
97219820Sjeff		IB_MANDATORY_FUNC(get_dma_mr),
98219820Sjeff		IB_MANDATORY_FUNC(dereg_mr)
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
157219820Sjeffstatic int start_port(struct ib_device *device)
158219820Sjeff{
159219820Sjeff	return (device->node_type == RDMA_NODE_IB_SWITCH) ? 0 : 1;
160219820Sjeff}
161219820Sjeff
162219820Sjeff
163219820Sjeffstatic int 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) {
196219820Sjeff		kfree(device);
197219820Sjeff		return;
198219820Sjeff	}
199219820Sjeff
200219820Sjeff	BUG_ON(device->reg_state != IB_DEV_UNREGISTERED);
201219820Sjeff
202219820Sjeff	kobject_put(&device->dev.kobj);
203219820Sjeff}
204219820SjeffEXPORT_SYMBOL(ib_dealloc_device);
205219820Sjeff
206219820Sjeffstatic int add_client_context(struct ib_device *device, struct ib_client *client)
207219820Sjeff{
208219820Sjeff	struct ib_client_data *context;
209219820Sjeff	unsigned long flags;
210219820Sjeff
211219820Sjeff	context = kmalloc(sizeof *context, GFP_KERNEL);
212219820Sjeff	if (!context) {
213219820Sjeff		printk(KERN_WARNING "Couldn't allocate client context for %s/%s\n",
214219820Sjeff		       device->name, client->name);
215219820Sjeff		return -ENOMEM;
216219820Sjeff	}
217219820Sjeff
218219820Sjeff	context->client = client;
219219820Sjeff	context->data   = NULL;
220219820Sjeff
221219820Sjeff	spin_lock_irqsave(&device->client_data_lock, flags);
222219820Sjeff	list_add(&context->list, &device->client_data_list);
223219820Sjeff	spin_unlock_irqrestore(&device->client_data_lock, flags);
224219820Sjeff
225219820Sjeff	return 0;
226219820Sjeff}
227219820Sjeff
228219820Sjeffstatic int read_port_table_lengths(struct ib_device *device)
229219820Sjeff{
230219820Sjeff	struct ib_port_attr *tprops = NULL;
231219820Sjeff	int num_ports, ret = -ENOMEM;
232219820Sjeff	u8 port_index;
233219820Sjeff
234219820Sjeff	tprops = kmalloc(sizeof *tprops, GFP_KERNEL);
235219820Sjeff	if (!tprops)
236219820Sjeff		goto out;
237219820Sjeff
238219820Sjeff	num_ports = end_port(device) - start_port(device) + 1;
239219820Sjeff
240219820Sjeff	device->pkey_tbl_len = kmalloc(sizeof *device->pkey_tbl_len * num_ports,
241219820Sjeff				       GFP_KERNEL);
242219820Sjeff	device->gid_tbl_len = kmalloc(sizeof *device->gid_tbl_len * num_ports,
243219820Sjeff				      GFP_KERNEL);
244219820Sjeff	if (!device->pkey_tbl_len || !device->gid_tbl_len)
245219820Sjeff		goto err;
246219820Sjeff
247219820Sjeff	for (port_index = 0; port_index < num_ports; ++port_index) {
248219820Sjeff		ret = ib_query_port(device, port_index + start_port(device),
249219820Sjeff					tprops);
250219820Sjeff		if (ret)
251219820Sjeff			goto err;
252219820Sjeff		device->pkey_tbl_len[port_index] = tprops->pkey_tbl_len;
253219820Sjeff		device->gid_tbl_len[port_index]  = tprops->gid_tbl_len;
254219820Sjeff	}
255219820Sjeff
256219820Sjeff	ret = 0;
257219820Sjeff	goto out;
258219820Sjeff
259219820Sjefferr:
260219820Sjeff	kfree(device->gid_tbl_len);
261219820Sjeff	kfree(device->pkey_tbl_len);
262219820Sjeffout:
263219820Sjeff	kfree(tprops);
264219820Sjeff	return ret;
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
302219820Sjeff	ret = read_port_table_lengths(device);
303219820Sjeff	if (ret) {
304219820Sjeff		printk(KERN_WARNING "Couldn't create table lengths cache for device %s\n",
305219820Sjeff		       device->name);
306219820Sjeff		goto out;
307219820Sjeff	}
308219820Sjeff
309255932Salfred	ret = ib_device_register_sysfs(device, port_callback);
310219820Sjeff	if (ret) {
311219820Sjeff		printk(KERN_WARNING "Couldn't register device %s with driver model\n",
312219820Sjeff		       device->name);
313219820Sjeff		kfree(device->gid_tbl_len);
314219820Sjeff		kfree(device->pkey_tbl_len);
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	kfree(device->gid_tbl_len);
357219820Sjeff	kfree(device->pkey_tbl_len);
358219820Sjeff
359219820Sjeff	mutex_unlock(&device_mutex);
360219820Sjeff
361219820Sjeff	ib_device_unregister_sysfs(device);
362219820Sjeff
363219820Sjeff	spin_lock_irqsave(&device->client_data_lock, flags);
364219820Sjeff	list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
365219820Sjeff		kfree(context);
366219820Sjeff	spin_unlock_irqrestore(&device->client_data_lock, flags);
367219820Sjeff
368219820Sjeff	device->reg_state = IB_DEV_UNREGISTERED;
369219820Sjeff}
370219820SjeffEXPORT_SYMBOL(ib_unregister_device);
371219820Sjeff
372219820Sjeff/**
373219820Sjeff * ib_register_client - Register an IB client
374219820Sjeff * @client:Client to register
375219820Sjeff *
376219820Sjeff * Upper level users of the IB drivers can use ib_register_client() to
377219820Sjeff * register callbacks for IB device addition and removal.  When an IB
378219820Sjeff * device is added, each registered client's add method will be called
379219820Sjeff * (in the order the clients were registered), and when a device is
380219820Sjeff * removed, each client's remove method will be called (in the reverse
381219820Sjeff * order that clients were registered).  In addition, when
382219820Sjeff * ib_register_client() is called, the client will receive an add
383219820Sjeff * callback for all devices already registered.
384219820Sjeff */
385219820Sjeffint ib_register_client(struct ib_client *client)
386219820Sjeff{
387219820Sjeff	struct ib_device *device;
388219820Sjeff
389219820Sjeff	mutex_lock(&device_mutex);
390219820Sjeff
391219820Sjeff	list_add_tail(&client->list, &client_list);
392219820Sjeff	list_for_each_entry(device, &device_list, core_list)
393219820Sjeff		if (client->add && !add_client_context(device, client))
394219820Sjeff			client->add(device);
395219820Sjeff
396219820Sjeff	mutex_unlock(&device_mutex);
397219820Sjeff
398219820Sjeff	return 0;
399219820Sjeff}
400219820SjeffEXPORT_SYMBOL(ib_register_client);
401219820Sjeff
402219820Sjeff/**
403219820Sjeff * ib_unregister_client - Unregister an IB client
404219820Sjeff * @client:Client to unregister
405219820Sjeff *
406219820Sjeff * Upper level users use ib_unregister_client() to remove their client
407219820Sjeff * registration.  When ib_unregister_client() is called, the client
408219820Sjeff * will receive a remove callback for each IB device still registered.
409219820Sjeff */
410219820Sjeffvoid ib_unregister_client(struct ib_client *client)
411219820Sjeff{
412219820Sjeff	struct ib_client_data *context, *tmp;
413219820Sjeff	struct ib_device *device;
414219820Sjeff	unsigned long flags;
415219820Sjeff
416219820Sjeff	mutex_lock(&device_mutex);
417219820Sjeff
418219820Sjeff	list_for_each_entry(device, &device_list, core_list) {
419219820Sjeff		if (client->remove)
420219820Sjeff			client->remove(device);
421219820Sjeff
422219820Sjeff		spin_lock_irqsave(&device->client_data_lock, flags);
423219820Sjeff		list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
424219820Sjeff			if (context->client == client) {
425219820Sjeff				list_del(&context->list);
426219820Sjeff				kfree(context);
427219820Sjeff			}
428219820Sjeff		spin_unlock_irqrestore(&device->client_data_lock, flags);
429219820Sjeff	}
430219820Sjeff	list_del(&client->list);
431219820Sjeff
432219820Sjeff	mutex_unlock(&device_mutex);
433219820Sjeff}
434219820SjeffEXPORT_SYMBOL(ib_unregister_client);
435219820Sjeff
436219820Sjeff/**
437219820Sjeff * ib_get_client_data - Get IB client context
438219820Sjeff * @device:Device to get context for
439219820Sjeff * @client:Client to get context for
440219820Sjeff *
441219820Sjeff * ib_get_client_data() returns client context set with
442219820Sjeff * ib_set_client_data().
443219820Sjeff */
444219820Sjeffvoid *ib_get_client_data(struct ib_device *device, struct ib_client *client)
445219820Sjeff{
446219820Sjeff	struct ib_client_data *context;
447219820Sjeff	void *ret = NULL;
448219820Sjeff	unsigned long flags;
449219820Sjeff
450219820Sjeff	spin_lock_irqsave(&device->client_data_lock, flags);
451219820Sjeff	list_for_each_entry(context, &device->client_data_list, list)
452219820Sjeff		if (context->client == client) {
453219820Sjeff			ret = context->data;
454219820Sjeff			break;
455219820Sjeff		}
456219820Sjeff	spin_unlock_irqrestore(&device->client_data_lock, flags);
457219820Sjeff
458219820Sjeff	return ret;
459219820Sjeff}
460219820SjeffEXPORT_SYMBOL(ib_get_client_data);
461219820Sjeff
462219820Sjeff/**
463219820Sjeff * ib_set_client_data - Set IB client context
464219820Sjeff * @device:Device to set context for
465219820Sjeff * @client:Client to set context for
466219820Sjeff * @data:Context to set
467219820Sjeff *
468219820Sjeff * ib_set_client_data() sets client context that can be retrieved with
469219820Sjeff * ib_get_client_data().
470219820Sjeff */
471219820Sjeffvoid ib_set_client_data(struct ib_device *device, struct ib_client *client,
472219820Sjeff			void *data)
473219820Sjeff{
474219820Sjeff	struct ib_client_data *context;
475219820Sjeff	unsigned long flags;
476219820Sjeff
477219820Sjeff	spin_lock_irqsave(&device->client_data_lock, flags);
478219820Sjeff	list_for_each_entry(context, &device->client_data_list, list)
479219820Sjeff		if (context->client == client) {
480219820Sjeff			context->data = data;
481219820Sjeff			goto out;
482219820Sjeff		}
483219820Sjeff
484219820Sjeff	printk(KERN_WARNING "No client context found for %s/%s\n",
485219820Sjeff	       device->name, client->name);
486219820Sjeff
487219820Sjeffout:
488219820Sjeff	spin_unlock_irqrestore(&device->client_data_lock, flags);
489219820Sjeff}
490219820SjeffEXPORT_SYMBOL(ib_set_client_data);
491219820Sjeff
492219820Sjeff/**
493219820Sjeff * ib_register_event_handler - Register an IB event handler
494219820Sjeff * @event_handler:Handler to register
495219820Sjeff *
496219820Sjeff * ib_register_event_handler() registers an event handler that will be
497219820Sjeff * called back when asynchronous IB events occur (as defined in
498219820Sjeff * chapter 11 of the InfiniBand Architecture Specification).  This
499219820Sjeff * callback may occur in interrupt context.
500219820Sjeff */
501219820Sjeffint ib_register_event_handler  (struct ib_event_handler *event_handler)
502219820Sjeff{
503219820Sjeff	unsigned long flags;
504219820Sjeff
505219820Sjeff	spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
506219820Sjeff	list_add_tail(&event_handler->list,
507219820Sjeff		      &event_handler->device->event_handler_list);
508219820Sjeff	spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
509219820Sjeff
510219820Sjeff	return 0;
511219820Sjeff}
512219820SjeffEXPORT_SYMBOL(ib_register_event_handler);
513219820Sjeff
514219820Sjeff/**
515219820Sjeff * ib_unregister_event_handler - Unregister an event handler
516219820Sjeff * @event_handler:Handler to unregister
517219820Sjeff *
518219820Sjeff * Unregister an event handler registered with
519219820Sjeff * ib_register_event_handler().
520219820Sjeff */
521219820Sjeffint ib_unregister_event_handler(struct ib_event_handler *event_handler)
522219820Sjeff{
523219820Sjeff	unsigned long flags;
524219820Sjeff
525219820Sjeff	spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
526219820Sjeff	list_del(&event_handler->list);
527219820Sjeff	spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
528219820Sjeff
529219820Sjeff	return 0;
530219820Sjeff}
531219820SjeffEXPORT_SYMBOL(ib_unregister_event_handler);
532219820Sjeff
533219820Sjeff/**
534219820Sjeff * ib_dispatch_event - Dispatch an asynchronous event
535219820Sjeff * @event:Event to dispatch
536219820Sjeff *
537219820Sjeff * Low-level drivers must call ib_dispatch_event() to dispatch the
538219820Sjeff * event to all registered event handlers when an asynchronous event
539219820Sjeff * occurs.
540219820Sjeff */
541219820Sjeffvoid ib_dispatch_event(struct ib_event *event)
542219820Sjeff{
543219820Sjeff	unsigned long flags;
544219820Sjeff	struct ib_event_handler *handler;
545219820Sjeff
546219820Sjeff	spin_lock_irqsave(&event->device->event_handler_lock, flags);
547219820Sjeff
548219820Sjeff	list_for_each_entry(handler, &event->device->event_handler_list, list)
549219820Sjeff		handler->handler(handler, event);
550219820Sjeff
551219820Sjeff	spin_unlock_irqrestore(&event->device->event_handler_lock, flags);
552219820Sjeff}
553219820SjeffEXPORT_SYMBOL(ib_dispatch_event);
554219820Sjeff
555219820Sjeff/**
556219820Sjeff * ib_query_device - Query IB device attributes
557219820Sjeff * @device:Device to query
558219820Sjeff * @device_attr:Device attributes
559219820Sjeff *
560219820Sjeff * ib_query_device() returns the attributes of a device through the
561219820Sjeff * @device_attr pointer.
562219820Sjeff */
563219820Sjeffint ib_query_device(struct ib_device *device,
564219820Sjeff		    struct ib_device_attr *device_attr)
565219820Sjeff{
566219820Sjeff	return device->query_device(device, device_attr);
567219820Sjeff}
568219820SjeffEXPORT_SYMBOL(ib_query_device);
569219820Sjeff
570219820Sjeff/**
571219820Sjeff * ib_query_port - Query IB port attributes
572219820Sjeff * @device:Device to query
573219820Sjeff * @port_num:Port number to query
574219820Sjeff * @port_attr:Port attributes
575219820Sjeff *
576219820Sjeff * ib_query_port() returns the attributes of a port through the
577219820Sjeff * @port_attr pointer.
578219820Sjeff */
579219820Sjeffint ib_query_port(struct ib_device *device,
580219820Sjeff		  u8 port_num,
581219820Sjeff		  struct ib_port_attr *port_attr)
582219820Sjeff{
583219820Sjeff	if (port_num < start_port(device) || port_num > end_port(device))
584219820Sjeff		return -EINVAL;
585219820Sjeff
586219820Sjeff	return device->query_port(device, port_num, port_attr);
587219820Sjeff}
588219820SjeffEXPORT_SYMBOL(ib_query_port);
589219820Sjeff
590219820Sjeff/**
591219820Sjeff * ib_query_gid - Get GID table entry
592219820Sjeff * @device:Device to query
593219820Sjeff * @port_num:Port number to query
594219820Sjeff * @index:GID table index to query
595219820Sjeff * @gid:Returned GID
596219820Sjeff *
597219820Sjeff * ib_query_gid() fetches the specified GID table entry.
598219820Sjeff */
599219820Sjeffint ib_query_gid(struct ib_device *device,
600219820Sjeff		 u8 port_num, int index, union ib_gid *gid)
601219820Sjeff{
602219820Sjeff	return device->query_gid(device, port_num, index, gid);
603219820Sjeff}
604219820SjeffEXPORT_SYMBOL(ib_query_gid);
605219820Sjeff
606219820Sjeff/**
607219820Sjeff * ib_query_pkey - Get P_Key table entry
608219820Sjeff * @device:Device to query
609219820Sjeff * @port_num:Port number to query
610219820Sjeff * @index:P_Key table index to query
611219820Sjeff * @pkey:Returned P_Key
612219820Sjeff *
613219820Sjeff * ib_query_pkey() fetches the specified P_Key table entry.
614219820Sjeff */
615219820Sjeffint ib_query_pkey(struct ib_device *device,
616219820Sjeff		  u8 port_num, u16 index, u16 *pkey)
617219820Sjeff{
618219820Sjeff	return device->query_pkey(device, port_num, index, pkey);
619219820Sjeff}
620219820SjeffEXPORT_SYMBOL(ib_query_pkey);
621219820Sjeff
622219820Sjeff/**
623219820Sjeff * ib_modify_device - Change IB device attributes
624219820Sjeff * @device:Device to modify
625219820Sjeff * @device_modify_mask:Mask of attributes to change
626219820Sjeff * @device_modify:New attribute values
627219820Sjeff *
628219820Sjeff * ib_modify_device() changes a device's attributes as specified by
629219820Sjeff * the @device_modify_mask and @device_modify structure.
630219820Sjeff */
631219820Sjeffint ib_modify_device(struct ib_device *device,
632219820Sjeff		     int device_modify_mask,
633219820Sjeff		     struct ib_device_modify *device_modify)
634219820Sjeff{
635219820Sjeff	return device->modify_device(device, device_modify_mask,
636219820Sjeff				     device_modify);
637219820Sjeff}
638219820SjeffEXPORT_SYMBOL(ib_modify_device);
639219820Sjeff
640219820Sjeff/**
641219820Sjeff * ib_modify_port - Modifies the attributes for the specified port.
642219820Sjeff * @device: The device to modify.
643219820Sjeff * @port_num: The number of the port to modify.
644219820Sjeff * @port_modify_mask: Mask used to specify which attributes of the port
645219820Sjeff *   to change.
646219820Sjeff * @port_modify: New attribute values for the port.
647219820Sjeff *
648219820Sjeff * ib_modify_port() changes a port's attributes as specified by the
649219820Sjeff * @port_modify_mask and @port_modify structure.
650219820Sjeff */
651219820Sjeffint ib_modify_port(struct ib_device *device,
652219820Sjeff		   u8 port_num, int port_modify_mask,
653219820Sjeff		   struct ib_port_modify *port_modify)
654219820Sjeff{
655219820Sjeff	if (port_num < start_port(device) || port_num > end_port(device))
656219820Sjeff		return -EINVAL;
657219820Sjeff
658219820Sjeff	return device->modify_port(device, port_num, port_modify_mask,
659219820Sjeff				   port_modify);
660219820Sjeff}
661219820SjeffEXPORT_SYMBOL(ib_modify_port);
662219820Sjeff
663219820Sjeff/**
664219820Sjeff * ib_find_gid - Returns the port number and GID table index where
665219820Sjeff *   a specified GID value occurs.
666219820Sjeff * @device: The device to query.
667219820Sjeff * @gid: The GID value to search for.
668219820Sjeff * @port_num: The port number of the device where the GID value was found.
669219820Sjeff * @index: The index into the GID table where the GID was found.  This
670219820Sjeff *   parameter may be NULL.
671219820Sjeff */
672219820Sjeffint ib_find_gid(struct ib_device *device, union ib_gid *gid,
673219820Sjeff		u8 *port_num, u16 *index)
674219820Sjeff{
675219820Sjeff	union ib_gid tmp_gid;
676219820Sjeff	int ret, port, i;
677219820Sjeff
678219820Sjeff	for (port = start_port(device); port <= end_port(device); ++port) {
679219820Sjeff		for (i = 0; i < device->gid_tbl_len[port - start_port(device)]; ++i) {
680219820Sjeff			ret = ib_query_gid(device, port, i, &tmp_gid);
681219820Sjeff			if (ret)
682219820Sjeff				return ret;
683219820Sjeff			if (!memcmp(&tmp_gid, gid, sizeof *gid)) {
684219820Sjeff				*port_num = port;
685219820Sjeff				if (index)
686219820Sjeff					*index = i;
687219820Sjeff				return 0;
688219820Sjeff			}
689219820Sjeff		}
690219820Sjeff	}
691219820Sjeff
692219820Sjeff	return -ENOENT;
693219820Sjeff}
694219820SjeffEXPORT_SYMBOL(ib_find_gid);
695219820Sjeff
696219820Sjeff/**
697219820Sjeff * ib_find_pkey - Returns the PKey table index where a specified
698219820Sjeff *   PKey value occurs.
699219820Sjeff * @device: The device to query.
700219820Sjeff * @port_num: The port number of the device to search for the PKey.
701219820Sjeff * @pkey: The PKey value to search for.
702219820Sjeff * @index: The index into the PKey table where the PKey was found.
703219820Sjeff */
704219820Sjeffint ib_find_pkey(struct ib_device *device,
705219820Sjeff		 u8 port_num, u16 pkey, u16 *index)
706219820Sjeff{
707219820Sjeff	int ret, i;
708219820Sjeff	u16 tmp_pkey;
709219820Sjeff
710219820Sjeff	for (i = 0; i < device->pkey_tbl_len[port_num - start_port(device)]; ++i) {
711219820Sjeff		ret = ib_query_pkey(device, port_num, i, &tmp_pkey);
712219820Sjeff		if (ret)
713219820Sjeff			return ret;
714219820Sjeff
715219820Sjeff		if ((pkey & 0x7fff) == (tmp_pkey & 0x7fff)) {
716219820Sjeff			*index = i;
717219820Sjeff			return 0;
718219820Sjeff		}
719219820Sjeff	}
720219820Sjeff
721219820Sjeff	return -ENOENT;
722219820Sjeff}
723219820SjeffEXPORT_SYMBOL(ib_find_pkey);
724219820Sjeff
725219820Sjeffstatic int __init ib_core_init(void)
726219820Sjeff{
727219820Sjeff	int ret;
728219820Sjeff
729219820Sjeff#ifdef __ia64__
730219820Sjeff	if (ia64_platform_is("hpzx1"))
731219820Sjeff		dma_map_sg_hp_wa = 1;
732219820Sjeff#endif
733219820Sjeff
734219820Sjeff	ret = ib_sysfs_setup();
735219820Sjeff	if (ret)
736219820Sjeff		printk(KERN_WARNING "Couldn't create InfiniBand device class\n");
737219820Sjeff
738219820Sjeff	ret = ib_cache_setup();
739219820Sjeff	if (ret) {
740219820Sjeff		printk(KERN_WARNING "Couldn't set up InfiniBand P_Key/GID cache\n");
741219820Sjeff		ib_sysfs_cleanup();
742219820Sjeff	}
743219820Sjeff
744219820Sjeff	return ret;
745219820Sjeff}
746219820Sjeff
747219820Sjeffstatic void __exit ib_core_cleanup(void)
748219820Sjeff{
749219820Sjeff	ib_cache_cleanup();
750219820Sjeff	ib_sysfs_cleanup();
751219820Sjeff	/* Make sure that any pending umem accounting work is done. */
752219820Sjeff	flush_scheduled_work();
753219820Sjeff}
754219820Sjeff
755219820Sjeffmodule_init(ib_core_init);
756219820Sjeffmodule_exit(ib_core_cleanup);
757255932Salfred
758255932Salfred#undef MODULE_VERSION
759255932Salfred#include <sys/module.h>
760255932Salfredstatic int
761255932Salfredibcore_evhand(module_t mod, int event, void *arg)
762255932Salfred{
763255932Salfred	return (0);
764255932Salfred}
765255932Salfred
766255932Salfredstatic moduledata_t ibcore_mod = {
767255932Salfred	.name = "ibcore",
768255932Salfred	.evhand = ibcore_evhand,
769255932Salfred};
770255932Salfred
771255932SalfredMODULE_VERSION(ibcore, 1);
772255932SalfredDECLARE_MODULE(ibcore, ibcore_mod, SI_SUB_SMP, SI_ORDER_ANY);
773