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),
97219820Sjeff		IB_MANDATORY_FUNC(dereg_mr)
98219820Sjeff	};
99219820Sjeff	int i;
100219820Sjeff
101219820Sjeff	for (i = 0; i < ARRAY_SIZE(mandatory_table); ++i) {
102219820Sjeff		if (!*(void **) ((u_char *) device + mandatory_table[i].offset)) {
103219820Sjeff			printk(KERN_WARNING "Device %s is missing mandatory function %s\n",
104219820Sjeff			       device->name, mandatory_table[i].name);
105219820Sjeff			return -EINVAL;
106219820Sjeff		}
107219820Sjeff	}
108219820Sjeff
109219820Sjeff	return 0;
110219820Sjeff}
111219820Sjeff
112219820Sjeffstatic struct ib_device *__ib_device_get_by_name(const char *name)
113219820Sjeff{
114219820Sjeff	struct ib_device *device;
115219820Sjeff
116219820Sjeff	list_for_each_entry(device, &device_list, core_list)
117219820Sjeff		if (!strncmp(name, device->name, IB_DEVICE_NAME_MAX))
118219820Sjeff			return device;
119219820Sjeff
120219820Sjeff	return NULL;
121219820Sjeff}
122219820Sjeff
123219820Sjeff
124219820Sjeffstatic int alloc_name(char *name)
125219820Sjeff{
126219820Sjeff	unsigned long *inuse;
127219820Sjeff	char buf[IB_DEVICE_NAME_MAX];
128219820Sjeff	struct ib_device *device;
129219820Sjeff	int i;
130219820Sjeff
131219820Sjeff	inuse = (unsigned long *) get_zeroed_page(GFP_KERNEL);
132219820Sjeff	if (!inuse)
133219820Sjeff		return -ENOMEM;
134219820Sjeff
135219820Sjeff	list_for_each_entry(device, &device_list, core_list) {
136219820Sjeff		if (!sscanf(device->name, name, &i))
137219820Sjeff			continue;
138219820Sjeff		if (i < 0 || i >= PAGE_SIZE * 8)
139219820Sjeff			continue;
140219820Sjeff		snprintf(buf, sizeof buf, name, i);
141219820Sjeff		if (!strncmp(buf, device->name, IB_DEVICE_NAME_MAX))
142219820Sjeff			set_bit(i, inuse);
143219820Sjeff	}
144219820Sjeff
145219820Sjeff	i = find_first_zero_bit(inuse, PAGE_SIZE * 8);
146219820Sjeff	free_page((unsigned long) inuse);
147219820Sjeff	snprintf(buf, sizeof buf, name, i);
148219820Sjeff
149219820Sjeff	if (__ib_device_get_by_name(buf))
150219820Sjeff		return -ENFILE;
151219820Sjeff
152219820Sjeff	strlcpy(name, buf, IB_DEVICE_NAME_MAX);
153219820Sjeff	return 0;
154219820Sjeff}
155219820Sjeff
156219820Sjeffstatic int start_port(struct ib_device *device)
157219820Sjeff{
158219820Sjeff	return (device->node_type == RDMA_NODE_IB_SWITCH) ? 0 : 1;
159219820Sjeff}
160219820Sjeff
161219820Sjeff
162219820Sjeffstatic int end_port(struct ib_device *device)
163219820Sjeff{
164219820Sjeff	return (device->node_type == RDMA_NODE_IB_SWITCH) ?
165219820Sjeff		0 : device->phys_port_cnt;
166219820Sjeff}
167219820Sjeff
168219820Sjeff/**
169219820Sjeff * ib_alloc_device - allocate an IB device struct
170219820Sjeff * @size:size of structure to allocate
171219820Sjeff *
172219820Sjeff * Low-level drivers should use ib_alloc_device() to allocate &struct
173219820Sjeff * ib_device.  @size is the size of the structure to be allocated,
174219820Sjeff * including any private data used by the low-level driver.
175219820Sjeff * ib_dealloc_device() must be used to free structures allocated with
176219820Sjeff * ib_alloc_device().
177219820Sjeff */
178219820Sjeffstruct ib_device *ib_alloc_device(size_t size)
179219820Sjeff{
180219820Sjeff	BUG_ON(size < sizeof (struct ib_device));
181219820Sjeff
182219820Sjeff	return kzalloc(size, GFP_KERNEL);
183219820Sjeff}
184219820SjeffEXPORT_SYMBOL(ib_alloc_device);
185219820Sjeff
186219820Sjeff/**
187219820Sjeff * ib_dealloc_device - free an IB device struct
188219820Sjeff * @device:structure to free
189219820Sjeff *
190219820Sjeff * Free a structure allocated with ib_alloc_device().
191219820Sjeff */
192219820Sjeffvoid ib_dealloc_device(struct ib_device *device)
193219820Sjeff{
194219820Sjeff	if (device->reg_state == IB_DEV_UNINITIALIZED) {
195219820Sjeff		kfree(device);
196219820Sjeff		return;
197219820Sjeff	}
198219820Sjeff
199219820Sjeff	BUG_ON(device->reg_state != IB_DEV_UNREGISTERED);
200219820Sjeff
201219820Sjeff	kobject_put(&device->dev.kobj);
202219820Sjeff}
203219820SjeffEXPORT_SYMBOL(ib_dealloc_device);
204219820Sjeff
205219820Sjeffstatic int add_client_context(struct ib_device *device, struct ib_client *client)
206219820Sjeff{
207219820Sjeff	struct ib_client_data *context;
208219820Sjeff	unsigned long flags;
209219820Sjeff
210219820Sjeff	context = kmalloc(sizeof *context, GFP_KERNEL);
211219820Sjeff	if (!context) {
212219820Sjeff		printk(KERN_WARNING "Couldn't allocate client context for %s/%s\n",
213219820Sjeff		       device->name, client->name);
214219820Sjeff		return -ENOMEM;
215219820Sjeff	}
216219820Sjeff
217219820Sjeff	context->client = client;
218219820Sjeff	context->data   = NULL;
219219820Sjeff
220219820Sjeff	spin_lock_irqsave(&device->client_data_lock, flags);
221219820Sjeff	list_add(&context->list, &device->client_data_list);
222219820Sjeff	spin_unlock_irqrestore(&device->client_data_lock, flags);
223219820Sjeff
224219820Sjeff	return 0;
225219820Sjeff}
226219820Sjeff
227219820Sjeffstatic int read_port_table_lengths(struct ib_device *device)
228219820Sjeff{
229219820Sjeff	struct ib_port_attr *tprops = NULL;
230219820Sjeff	int num_ports, ret = -ENOMEM;
231219820Sjeff	u8 port_index;
232219820Sjeff
233219820Sjeff	tprops = kmalloc(sizeof *tprops, GFP_KERNEL);
234219820Sjeff	if (!tprops)
235219820Sjeff		goto out;
236219820Sjeff
237219820Sjeff	num_ports = end_port(device) - start_port(device) + 1;
238219820Sjeff
239219820Sjeff	device->pkey_tbl_len = kmalloc(sizeof *device->pkey_tbl_len * num_ports,
240219820Sjeff				       GFP_KERNEL);
241219820Sjeff	device->gid_tbl_len = kmalloc(sizeof *device->gid_tbl_len * num_ports,
242219820Sjeff				      GFP_KERNEL);
243219820Sjeff	if (!device->pkey_tbl_len || !device->gid_tbl_len)
244219820Sjeff		goto err;
245219820Sjeff
246219820Sjeff	for (port_index = 0; port_index < num_ports; ++port_index) {
247219820Sjeff		ret = ib_query_port(device, port_index + start_port(device),
248219820Sjeff					tprops);
249219820Sjeff		if (ret)
250219820Sjeff			goto err;
251219820Sjeff		device->pkey_tbl_len[port_index] = tprops->pkey_tbl_len;
252219820Sjeff		device->gid_tbl_len[port_index]  = tprops->gid_tbl_len;
253219820Sjeff	}
254219820Sjeff
255219820Sjeff	ret = 0;
256219820Sjeff	goto out;
257219820Sjeff
258219820Sjefferr:
259219820Sjeff	kfree(device->gid_tbl_len);
260219820Sjeff	kfree(device->pkey_tbl_len);
261219820Sjeffout:
262219820Sjeff	kfree(tprops);
263219820Sjeff	return ret;
264219820Sjeff}
265219820Sjeff
266219820Sjeff/**
267219820Sjeff * ib_register_device - Register an IB device with IB core
268219820Sjeff * @device:Device to register
269219820Sjeff *
270219820Sjeff * Low-level drivers use ib_register_device() to register their
271219820Sjeff * devices with the IB core.  All registered clients will receive a
272219820Sjeff * callback for each device that is added. @device must be allocated
273219820Sjeff * with ib_alloc_device().
274219820Sjeff */
275255932Salfredint ib_register_device(struct ib_device *device,
276255932Salfred		       int (*port_callback)(struct ib_device *,
277255932Salfred					    u8, struct kobject *))
278219820Sjeff{
279219820Sjeff	int ret;
280219820Sjeff
281219820Sjeff	mutex_lock(&device_mutex);
282219820Sjeff
283219820Sjeff	if (strchr(device->name, '%')) {
284219820Sjeff		ret = alloc_name(device->name);
285219820Sjeff		if (ret)
286219820Sjeff			goto out;
287219820Sjeff	}
288219820Sjeff
289219820Sjeff	if (ib_device_check_mandatory(device)) {
290219820Sjeff		ret = -EINVAL;
291219820Sjeff		goto out;
292219820Sjeff	}
293219820Sjeff
294219820Sjeff	INIT_LIST_HEAD(&device->event_handler_list);
295219820Sjeff	INIT_LIST_HEAD(&device->client_data_list);
296219820Sjeff	spin_lock_init(&device->event_handler_lock);
297219820Sjeff	spin_lock_init(&device->client_data_lock);
298255973Salfred	device->ib_uverbs_xrcd_table = RB_ROOT;
299255973Salfred	mutex_init(&device->xrcd_table_mutex);
300219820Sjeff
301219820Sjeff	ret = read_port_table_lengths(device);
302219820Sjeff	if (ret) {
303219820Sjeff		printk(KERN_WARNING "Couldn't create table lengths cache for device %s\n",
304219820Sjeff		       device->name);
305219820Sjeff		goto out;
306219820Sjeff	}
307219820Sjeff
308255932Salfred	ret = ib_device_register_sysfs(device, port_callback);
309219820Sjeff	if (ret) {
310219820Sjeff		printk(KERN_WARNING "Couldn't register device %s with driver model\n",
311219820Sjeff		       device->name);
312219820Sjeff		kfree(device->gid_tbl_len);
313219820Sjeff		kfree(device->pkey_tbl_len);
314219820Sjeff		goto out;
315219820Sjeff	}
316219820Sjeff
317219820Sjeff	list_add_tail(&device->core_list, &device_list);
318219820Sjeff
319219820Sjeff	device->reg_state = IB_DEV_REGISTERED;
320219820Sjeff
321219820Sjeff	{
322219820Sjeff		struct ib_client *client;
323219820Sjeff
324219820Sjeff		list_for_each_entry(client, &client_list, list)
325219820Sjeff			if (client->add && !add_client_context(device, client))
326219820Sjeff				client->add(device);
327219820Sjeff	}
328219820Sjeff
329219820Sjeff out:
330219820Sjeff	mutex_unlock(&device_mutex);
331219820Sjeff	return ret;
332219820Sjeff}
333219820SjeffEXPORT_SYMBOL(ib_register_device);
334219820Sjeff
335219820Sjeff/**
336219820Sjeff * ib_unregister_device - Unregister an IB device
337219820Sjeff * @device:Device to unregister
338219820Sjeff *
339219820Sjeff * Unregister an IB device.  All clients will receive a remove callback.
340219820Sjeff */
341219820Sjeffvoid ib_unregister_device(struct ib_device *device)
342219820Sjeff{
343219820Sjeff	struct ib_client *client;
344219820Sjeff	struct ib_client_data *context, *tmp;
345219820Sjeff	unsigned long flags;
346219820Sjeff
347219820Sjeff	mutex_lock(&device_mutex);
348219820Sjeff
349219820Sjeff	list_for_each_entry_reverse(client, &client_list, list)
350219820Sjeff		if (client->remove)
351219820Sjeff			client->remove(device);
352219820Sjeff
353219820Sjeff	list_del(&device->core_list);
354219820Sjeff
355219820Sjeff	kfree(device->gid_tbl_len);
356219820Sjeff	kfree(device->pkey_tbl_len);
357219820Sjeff
358219820Sjeff	mutex_unlock(&device_mutex);
359219820Sjeff
360219820Sjeff	ib_device_unregister_sysfs(device);
361219820Sjeff
362219820Sjeff	spin_lock_irqsave(&device->client_data_lock, flags);
363219820Sjeff	list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
364219820Sjeff		kfree(context);
365219820Sjeff	spin_unlock_irqrestore(&device->client_data_lock, flags);
366219820Sjeff
367219820Sjeff	device->reg_state = IB_DEV_UNREGISTERED;
368219820Sjeff}
369219820SjeffEXPORT_SYMBOL(ib_unregister_device);
370219820Sjeff
371219820Sjeff/**
372219820Sjeff * ib_register_client - Register an IB client
373219820Sjeff * @client:Client to register
374219820Sjeff *
375219820Sjeff * Upper level users of the IB drivers can use ib_register_client() to
376219820Sjeff * register callbacks for IB device addition and removal.  When an IB
377219820Sjeff * device is added, each registered client's add method will be called
378219820Sjeff * (in the order the clients were registered), and when a device is
379219820Sjeff * removed, each client's remove method will be called (in the reverse
380219820Sjeff * order that clients were registered).  In addition, when
381219820Sjeff * ib_register_client() is called, the client will receive an add
382219820Sjeff * callback for all devices already registered.
383219820Sjeff */
384219820Sjeffint ib_register_client(struct ib_client *client)
385219820Sjeff{
386219820Sjeff	struct ib_device *device;
387219820Sjeff
388219820Sjeff	mutex_lock(&device_mutex);
389219820Sjeff
390219820Sjeff	list_add_tail(&client->list, &client_list);
391219820Sjeff	list_for_each_entry(device, &device_list, core_list)
392219820Sjeff		if (client->add && !add_client_context(device, client))
393219820Sjeff			client->add(device);
394219820Sjeff
395219820Sjeff	mutex_unlock(&device_mutex);
396219820Sjeff
397219820Sjeff	return 0;
398219820Sjeff}
399219820SjeffEXPORT_SYMBOL(ib_register_client);
400219820Sjeff
401219820Sjeff/**
402219820Sjeff * ib_unregister_client - Unregister an IB client
403219820Sjeff * @client:Client to unregister
404219820Sjeff *
405219820Sjeff * Upper level users use ib_unregister_client() to remove their client
406219820Sjeff * registration.  When ib_unregister_client() is called, the client
407219820Sjeff * will receive a remove callback for each IB device still registered.
408219820Sjeff */
409219820Sjeffvoid ib_unregister_client(struct ib_client *client)
410219820Sjeff{
411219820Sjeff	struct ib_client_data *context, *tmp;
412219820Sjeff	struct ib_device *device;
413219820Sjeff	unsigned long flags;
414219820Sjeff
415219820Sjeff	mutex_lock(&device_mutex);
416219820Sjeff
417219820Sjeff	list_for_each_entry(device, &device_list, core_list) {
418219820Sjeff		if (client->remove)
419219820Sjeff			client->remove(device);
420219820Sjeff
421219820Sjeff		spin_lock_irqsave(&device->client_data_lock, flags);
422219820Sjeff		list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
423219820Sjeff			if (context->client == client) {
424219820Sjeff				list_del(&context->list);
425219820Sjeff				kfree(context);
426219820Sjeff			}
427219820Sjeff		spin_unlock_irqrestore(&device->client_data_lock, flags);
428219820Sjeff	}
429219820Sjeff	list_del(&client->list);
430219820Sjeff
431219820Sjeff	mutex_unlock(&device_mutex);
432219820Sjeff}
433219820SjeffEXPORT_SYMBOL(ib_unregister_client);
434219820Sjeff
435219820Sjeff/**
436219820Sjeff * ib_get_client_data - Get IB client context
437219820Sjeff * @device:Device to get context for
438219820Sjeff * @client:Client to get context for
439219820Sjeff *
440219820Sjeff * ib_get_client_data() returns client context set with
441219820Sjeff * ib_set_client_data().
442219820Sjeff */
443219820Sjeffvoid *ib_get_client_data(struct ib_device *device, struct ib_client *client)
444219820Sjeff{
445219820Sjeff	struct ib_client_data *context;
446219820Sjeff	void *ret = NULL;
447219820Sjeff	unsigned long flags;
448219820Sjeff
449219820Sjeff	spin_lock_irqsave(&device->client_data_lock, flags);
450219820Sjeff	list_for_each_entry(context, &device->client_data_list, list)
451219820Sjeff		if (context->client == client) {
452219820Sjeff			ret = context->data;
453219820Sjeff			break;
454219820Sjeff		}
455219820Sjeff	spin_unlock_irqrestore(&device->client_data_lock, flags);
456219820Sjeff
457219820Sjeff	return ret;
458219820Sjeff}
459219820SjeffEXPORT_SYMBOL(ib_get_client_data);
460219820Sjeff
461219820Sjeff/**
462219820Sjeff * ib_set_client_data - Set IB client context
463219820Sjeff * @device:Device to set context for
464219820Sjeff * @client:Client to set context for
465219820Sjeff * @data:Context to set
466219820Sjeff *
467219820Sjeff * ib_set_client_data() sets client context that can be retrieved with
468219820Sjeff * ib_get_client_data().
469219820Sjeff */
470219820Sjeffvoid ib_set_client_data(struct ib_device *device, struct ib_client *client,
471219820Sjeff			void *data)
472219820Sjeff{
473219820Sjeff	struct ib_client_data *context;
474219820Sjeff	unsigned long flags;
475219820Sjeff
476219820Sjeff	spin_lock_irqsave(&device->client_data_lock, flags);
477219820Sjeff	list_for_each_entry(context, &device->client_data_list, list)
478219820Sjeff		if (context->client == client) {
479219820Sjeff			context->data = data;
480219820Sjeff			goto out;
481219820Sjeff		}
482219820Sjeff
483219820Sjeff	printk(KERN_WARNING "No client context found for %s/%s\n",
484219820Sjeff	       device->name, client->name);
485219820Sjeff
486219820Sjeffout:
487219820Sjeff	spin_unlock_irqrestore(&device->client_data_lock, flags);
488219820Sjeff}
489219820SjeffEXPORT_SYMBOL(ib_set_client_data);
490219820Sjeff
491219820Sjeff/**
492219820Sjeff * ib_register_event_handler - Register an IB event handler
493219820Sjeff * @event_handler:Handler to register
494219820Sjeff *
495219820Sjeff * ib_register_event_handler() registers an event handler that will be
496219820Sjeff * called back when asynchronous IB events occur (as defined in
497219820Sjeff * chapter 11 of the InfiniBand Architecture Specification).  This
498219820Sjeff * callback may occur in interrupt context.
499219820Sjeff */
500219820Sjeffint ib_register_event_handler  (struct ib_event_handler *event_handler)
501219820Sjeff{
502219820Sjeff	unsigned long flags;
503219820Sjeff
504219820Sjeff	spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
505219820Sjeff	list_add_tail(&event_handler->list,
506219820Sjeff		      &event_handler->device->event_handler_list);
507219820Sjeff	spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
508219820Sjeff
509219820Sjeff	return 0;
510219820Sjeff}
511219820SjeffEXPORT_SYMBOL(ib_register_event_handler);
512219820Sjeff
513219820Sjeff/**
514219820Sjeff * ib_unregister_event_handler - Unregister an event handler
515219820Sjeff * @event_handler:Handler to unregister
516219820Sjeff *
517219820Sjeff * Unregister an event handler registered with
518219820Sjeff * ib_register_event_handler().
519219820Sjeff */
520219820Sjeffint ib_unregister_event_handler(struct ib_event_handler *event_handler)
521219820Sjeff{
522219820Sjeff	unsigned long flags;
523219820Sjeff
524219820Sjeff	spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
525219820Sjeff	list_del(&event_handler->list);
526219820Sjeff	spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
527219820Sjeff
528219820Sjeff	return 0;
529219820Sjeff}
530219820SjeffEXPORT_SYMBOL(ib_unregister_event_handler);
531219820Sjeff
532219820Sjeff/**
533219820Sjeff * ib_dispatch_event - Dispatch an asynchronous event
534219820Sjeff * @event:Event to dispatch
535219820Sjeff *
536219820Sjeff * Low-level drivers must call ib_dispatch_event() to dispatch the
537219820Sjeff * event to all registered event handlers when an asynchronous event
538219820Sjeff * occurs.
539219820Sjeff */
540219820Sjeffvoid ib_dispatch_event(struct ib_event *event)
541219820Sjeff{
542219820Sjeff	unsigned long flags;
543219820Sjeff	struct ib_event_handler *handler;
544219820Sjeff
545219820Sjeff	spin_lock_irqsave(&event->device->event_handler_lock, flags);
546219820Sjeff
547219820Sjeff	list_for_each_entry(handler, &event->device->event_handler_list, list)
548219820Sjeff		handler->handler(handler, event);
549219820Sjeff
550219820Sjeff	spin_unlock_irqrestore(&event->device->event_handler_lock, flags);
551219820Sjeff}
552219820SjeffEXPORT_SYMBOL(ib_dispatch_event);
553219820Sjeff
554219820Sjeff/**
555219820Sjeff * ib_query_device - Query IB device attributes
556219820Sjeff * @device:Device to query
557219820Sjeff * @device_attr:Device attributes
558219820Sjeff *
559219820Sjeff * ib_query_device() returns the attributes of a device through the
560219820Sjeff * @device_attr pointer.
561219820Sjeff */
562219820Sjeffint ib_query_device(struct ib_device *device,
563219820Sjeff		    struct ib_device_attr *device_attr)
564219820Sjeff{
565219820Sjeff	return device->query_device(device, device_attr);
566219820Sjeff}
567219820SjeffEXPORT_SYMBOL(ib_query_device);
568219820Sjeff
569219820Sjeff/**
570219820Sjeff * ib_query_port - Query IB port attributes
571219820Sjeff * @device:Device to query
572219820Sjeff * @port_num:Port number to query
573219820Sjeff * @port_attr:Port attributes
574219820Sjeff *
575219820Sjeff * ib_query_port() returns the attributes of a port through the
576219820Sjeff * @port_attr pointer.
577219820Sjeff */
578219820Sjeffint ib_query_port(struct ib_device *device,
579219820Sjeff		  u8 port_num,
580219820Sjeff		  struct ib_port_attr *port_attr)
581219820Sjeff{
582219820Sjeff	if (port_num < start_port(device) || port_num > end_port(device))
583219820Sjeff		return -EINVAL;
584219820Sjeff
585219820Sjeff	return device->query_port(device, port_num, port_attr);
586219820Sjeff}
587219820SjeffEXPORT_SYMBOL(ib_query_port);
588219820Sjeff
589219820Sjeff/**
590219820Sjeff * ib_query_gid - Get GID table entry
591219820Sjeff * @device:Device to query
592219820Sjeff * @port_num:Port number to query
593219820Sjeff * @index:GID table index to query
594219820Sjeff * @gid:Returned GID
595219820Sjeff *
596219820Sjeff * ib_query_gid() fetches the specified GID table entry.
597219820Sjeff */
598219820Sjeffint ib_query_gid(struct ib_device *device,
599219820Sjeff		 u8 port_num, int index, union ib_gid *gid)
600219820Sjeff{
601219820Sjeff	return device->query_gid(device, port_num, index, gid);
602219820Sjeff}
603219820SjeffEXPORT_SYMBOL(ib_query_gid);
604219820Sjeff
605219820Sjeff/**
606219820Sjeff * ib_query_pkey - Get P_Key table entry
607219820Sjeff * @device:Device to query
608219820Sjeff * @port_num:Port number to query
609219820Sjeff * @index:P_Key table index to query
610219820Sjeff * @pkey:Returned P_Key
611219820Sjeff *
612219820Sjeff * ib_query_pkey() fetches the specified P_Key table entry.
613219820Sjeff */
614219820Sjeffint ib_query_pkey(struct ib_device *device,
615219820Sjeff		  u8 port_num, u16 index, u16 *pkey)
616219820Sjeff{
617219820Sjeff	return device->query_pkey(device, port_num, index, pkey);
618219820Sjeff}
619219820SjeffEXPORT_SYMBOL(ib_query_pkey);
620219820Sjeff
621219820Sjeff/**
622219820Sjeff * ib_modify_device - Change IB device attributes
623219820Sjeff * @device:Device to modify
624219820Sjeff * @device_modify_mask:Mask of attributes to change
625219820Sjeff * @device_modify:New attribute values
626219820Sjeff *
627219820Sjeff * ib_modify_device() changes a device's attributes as specified by
628219820Sjeff * the @device_modify_mask and @device_modify structure.
629219820Sjeff */
630219820Sjeffint ib_modify_device(struct ib_device *device,
631219820Sjeff		     int device_modify_mask,
632219820Sjeff		     struct ib_device_modify *device_modify)
633219820Sjeff{
634219820Sjeff	return device->modify_device(device, device_modify_mask,
635219820Sjeff				     device_modify);
636219820Sjeff}
637219820SjeffEXPORT_SYMBOL(ib_modify_device);
638219820Sjeff
639219820Sjeff/**
640219820Sjeff * ib_modify_port - Modifies the attributes for the specified port.
641219820Sjeff * @device: The device to modify.
642219820Sjeff * @port_num: The number of the port to modify.
643219820Sjeff * @port_modify_mask: Mask used to specify which attributes of the port
644219820Sjeff *   to change.
645219820Sjeff * @port_modify: New attribute values for the port.
646219820Sjeff *
647219820Sjeff * ib_modify_port() changes a port's attributes as specified by the
648219820Sjeff * @port_modify_mask and @port_modify structure.
649219820Sjeff */
650219820Sjeffint ib_modify_port(struct ib_device *device,
651219820Sjeff		   u8 port_num, int port_modify_mask,
652219820Sjeff		   struct ib_port_modify *port_modify)
653219820Sjeff{
654219820Sjeff	if (port_num < start_port(device) || port_num > end_port(device))
655219820Sjeff		return -EINVAL;
656219820Sjeff
657219820Sjeff	return device->modify_port(device, port_num, port_modify_mask,
658219820Sjeff				   port_modify);
659219820Sjeff}
660219820SjeffEXPORT_SYMBOL(ib_modify_port);
661219820Sjeff
662219820Sjeff/**
663219820Sjeff * ib_find_gid - Returns the port number and GID table index where
664219820Sjeff *   a specified GID value occurs.
665219820Sjeff * @device: The device to query.
666219820Sjeff * @gid: The GID value to search for.
667219820Sjeff * @port_num: The port number of the device where the GID value was found.
668219820Sjeff * @index: The index into the GID table where the GID was found.  This
669219820Sjeff *   parameter may be NULL.
670219820Sjeff */
671219820Sjeffint ib_find_gid(struct ib_device *device, union ib_gid *gid,
672219820Sjeff		u8 *port_num, u16 *index)
673219820Sjeff{
674219820Sjeff	union ib_gid tmp_gid;
675219820Sjeff	int ret, port, i;
676219820Sjeff
677219820Sjeff	for (port = start_port(device); port <= end_port(device); ++port) {
678219820Sjeff		for (i = 0; i < device->gid_tbl_len[port - start_port(device)]; ++i) {
679219820Sjeff			ret = ib_query_gid(device, port, i, &tmp_gid);
680219820Sjeff			if (ret)
681219820Sjeff				return ret;
682219820Sjeff			if (!memcmp(&tmp_gid, gid, sizeof *gid)) {
683219820Sjeff				*port_num = port;
684219820Sjeff				if (index)
685219820Sjeff					*index = i;
686219820Sjeff				return 0;
687219820Sjeff			}
688219820Sjeff		}
689219820Sjeff	}
690219820Sjeff
691219820Sjeff	return -ENOENT;
692219820Sjeff}
693219820SjeffEXPORT_SYMBOL(ib_find_gid);
694219820Sjeff
695219820Sjeff/**
696219820Sjeff * ib_find_pkey - Returns the PKey table index where a specified
697219820Sjeff *   PKey value occurs.
698219820Sjeff * @device: The device to query.
699219820Sjeff * @port_num: The port number of the device to search for the PKey.
700219820Sjeff * @pkey: The PKey value to search for.
701219820Sjeff * @index: The index into the PKey table where the PKey was found.
702219820Sjeff */
703219820Sjeffint ib_find_pkey(struct ib_device *device,
704219820Sjeff		 u8 port_num, u16 pkey, u16 *index)
705219820Sjeff{
706219820Sjeff	int ret, i;
707219820Sjeff	u16 tmp_pkey;
708219820Sjeff
709219820Sjeff	for (i = 0; i < device->pkey_tbl_len[port_num - start_port(device)]; ++i) {
710219820Sjeff		ret = ib_query_pkey(device, port_num, i, &tmp_pkey);
711219820Sjeff		if (ret)
712219820Sjeff			return ret;
713219820Sjeff
714219820Sjeff		if ((pkey & 0x7fff) == (tmp_pkey & 0x7fff)) {
715219820Sjeff			*index = i;
716219820Sjeff			return 0;
717219820Sjeff		}
718219820Sjeff	}
719219820Sjeff
720219820Sjeff	return -ENOENT;
721219820Sjeff}
722219820SjeffEXPORT_SYMBOL(ib_find_pkey);
723219820Sjeff
724219820Sjeffstatic int __init ib_core_init(void)
725219820Sjeff{
726219820Sjeff	int ret;
727219820Sjeff
728219820Sjeff#ifdef __ia64__
729219820Sjeff	if (ia64_platform_is("hpzx1"))
730219820Sjeff		dma_map_sg_hp_wa = 1;
731219820Sjeff#endif
732219820Sjeff
733219820Sjeff	ret = ib_sysfs_setup();
734219820Sjeff	if (ret)
735219820Sjeff		printk(KERN_WARNING "Couldn't create InfiniBand device class\n");
736219820Sjeff
737219820Sjeff	ret = ib_cache_setup();
738219820Sjeff	if (ret) {
739219820Sjeff		printk(KERN_WARNING "Couldn't set up InfiniBand P_Key/GID cache\n");
740219820Sjeff		ib_sysfs_cleanup();
741219820Sjeff	}
742219820Sjeff
743219820Sjeff	return ret;
744219820Sjeff}
745219820Sjeff
746219820Sjeffstatic void __exit ib_core_cleanup(void)
747219820Sjeff{
748219820Sjeff	ib_cache_cleanup();
749219820Sjeff	ib_sysfs_cleanup();
750219820Sjeff	/* Make sure that any pending umem accounting work is done. */
751219820Sjeff	flush_scheduled_work();
752219820Sjeff}
753219820Sjeff
754219820Sjeffmodule_init(ib_core_init);
755219820Sjeffmodule_exit(ib_core_cleanup);
756255932Salfred
757255932Salfred#undef MODULE_VERSION
758255932Salfred#include <sys/module.h>
759255932Salfredstatic int
760255932Salfredibcore_evhand(module_t mod, int event, void *arg)
761255932Salfred{
762255932Salfred	return (0);
763255932Salfred}
764255932Salfred
765255932Salfredstatic moduledata_t ibcore_mod = {
766255932Salfred	.name = "ibcore",
767255932Salfred	.evhand = ibcore_evhand,
768255932Salfred};
769255932Salfred
770255932SalfredMODULE_VERSION(ibcore, 1);
771255932SalfredDECLARE_MODULE(ibcore, ibcore_mod, SI_SUB_SMP, SI_ORDER_ANY);
772