Linux Audio

Check our new training course

Loading...
v3.1
  1/*
  2 * Copyright (c) 2004 Topspin Communications.  All rights reserved.
  3 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
  4 *
  5 * This software is available to you under a choice of one of two
  6 * licenses.  You may choose to be licensed under the terms of the GNU
  7 * General Public License (GPL) Version 2, available from the file
  8 * COPYING in the main directory of this source tree, or the
  9 * OpenIB.org BSD license below:
 10 *
 11 *     Redistribution and use in source and binary forms, with or
 12 *     without modification, are permitted provided that the following
 13 *     conditions are met:
 14 *
 15 *      - Redistributions of source code must retain the above
 16 *        copyright notice, this list of conditions and the following
 17 *        disclaimer.
 18 *
 19 *      - Redistributions in binary form must reproduce the above
 20 *        copyright notice, this list of conditions and the following
 21 *        disclaimer in the documentation and/or other materials
 22 *        provided with the distribution.
 23 *
 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 31 * SOFTWARE.
 32 */
 33
 34#include <linux/module.h>
 35#include <linux/string.h>
 36#include <linux/errno.h>
 37#include <linux/kernel.h>
 38#include <linux/slab.h>
 39#include <linux/init.h>
 40#include <linux/mutex.h>
 
 
 
 41#include <rdma/rdma_netlink.h>
 
 
 42
 43#include "core_priv.h"
 44
 45MODULE_AUTHOR("Roland Dreier");
 46MODULE_DESCRIPTION("core kernel InfiniBand API");
 47MODULE_LICENSE("Dual BSD/GPL");
 48
 49struct ib_client_data {
 50	struct list_head  list;
 51	struct ib_client *client;
 52	void *            data;
 
 
 
 53};
 54
 
 55struct workqueue_struct *ib_wq;
 56EXPORT_SYMBOL_GPL(ib_wq);
 57
 
 
 
 58static LIST_HEAD(device_list);
 59static LIST_HEAD(client_list);
 60
 61/*
 62 * device_mutex protects access to both device_list and client_list.
 63 * There's no real point to using multiple locks or something fancier
 64 * like an rwsem: we always access both lists, and we're always
 65 * modifying one list or the other list.  In any case this is not a
 66 * hot path so there's no point in trying to optimize.
 
 
 
 
 67 */
 68static DEFINE_MUTEX(device_mutex);
 
 
 
 
 
 
 
 
 
 
 69
 70static int ib_device_check_mandatory(struct ib_device *device)
 71{
 72#define IB_MANDATORY_FUNC(x) { offsetof(struct ib_device, x), #x }
 73	static const struct {
 74		size_t offset;
 75		char  *name;
 76	} mandatory_table[] = {
 77		IB_MANDATORY_FUNC(query_device),
 78		IB_MANDATORY_FUNC(query_port),
 79		IB_MANDATORY_FUNC(query_pkey),
 80		IB_MANDATORY_FUNC(query_gid),
 81		IB_MANDATORY_FUNC(alloc_pd),
 82		IB_MANDATORY_FUNC(dealloc_pd),
 83		IB_MANDATORY_FUNC(create_ah),
 84		IB_MANDATORY_FUNC(destroy_ah),
 85		IB_MANDATORY_FUNC(create_qp),
 86		IB_MANDATORY_FUNC(modify_qp),
 87		IB_MANDATORY_FUNC(destroy_qp),
 88		IB_MANDATORY_FUNC(post_send),
 89		IB_MANDATORY_FUNC(post_recv),
 90		IB_MANDATORY_FUNC(create_cq),
 91		IB_MANDATORY_FUNC(destroy_cq),
 92		IB_MANDATORY_FUNC(poll_cq),
 93		IB_MANDATORY_FUNC(req_notify_cq),
 94		IB_MANDATORY_FUNC(get_dma_mr),
 95		IB_MANDATORY_FUNC(dereg_mr)
 
 96	};
 97	int i;
 98
 99	for (i = 0; i < ARRAY_SIZE(mandatory_table); ++i) {
100		if (!*(void **) ((void *) device + mandatory_table[i].offset)) {
101			printk(KERN_WARNING "Device %s is missing mandatory function %s\n",
102			       device->name, mandatory_table[i].name);
103			return -EINVAL;
104		}
105	}
106
107	return 0;
108}
109
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110static struct ib_device *__ib_device_get_by_name(const char *name)
111{
112	struct ib_device *device;
113
114	list_for_each_entry(device, &device_list, core_list)
115		if (!strncmp(name, device->name, IB_DEVICE_NAME_MAX))
116			return device;
117
118	return NULL;
119}
120
121
122static int alloc_name(char *name)
123{
124	unsigned long *inuse;
125	char buf[IB_DEVICE_NAME_MAX];
126	struct ib_device *device;
127	int i;
128
129	inuse = (unsigned long *) get_zeroed_page(GFP_KERNEL);
130	if (!inuse)
131		return -ENOMEM;
132
133	list_for_each_entry(device, &device_list, core_list) {
134		if (!sscanf(device->name, name, &i))
135			continue;
136		if (i < 0 || i >= PAGE_SIZE * 8)
137			continue;
138		snprintf(buf, sizeof buf, name, i);
139		if (!strncmp(buf, device->name, IB_DEVICE_NAME_MAX))
140			set_bit(i, inuse);
141	}
142
143	i = find_first_zero_bit(inuse, PAGE_SIZE * 8);
144	free_page((unsigned long) inuse);
145	snprintf(buf, sizeof buf, name, i);
146
147	if (__ib_device_get_by_name(buf))
148		return -ENFILE;
149
150	strlcpy(name, buf, IB_DEVICE_NAME_MAX);
151	return 0;
152}
153
154static int start_port(struct ib_device *device)
155{
156	return (device->node_type == RDMA_NODE_IB_SWITCH) ? 0 : 1;
157}
158
 
 
 
 
 
 
 
 
 
 
 
 
159
160static int end_port(struct ib_device *device)
 
161{
162	return (device->node_type == RDMA_NODE_IB_SWITCH) ?
163		0 : device->phys_port_cnt;
 
 
 
 
 
 
 
 
164}
165
 
 
 
 
 
 
166/**
167 * ib_alloc_device - allocate an IB device struct
168 * @size:size of structure to allocate
169 *
170 * Low-level drivers should use ib_alloc_device() to allocate &struct
171 * ib_device.  @size is the size of the structure to be allocated,
172 * including any private data used by the low-level driver.
173 * ib_dealloc_device() must be used to free structures allocated with
174 * ib_alloc_device().
175 */
176struct ib_device *ib_alloc_device(size_t size)
177{
178	BUG_ON(size < sizeof (struct ib_device));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
179
180	return kzalloc(size, GFP_KERNEL);
181}
182EXPORT_SYMBOL(ib_alloc_device);
183
184/**
185 * ib_dealloc_device - free an IB device struct
186 * @device:structure to free
187 *
188 * Free a structure allocated with ib_alloc_device().
189 */
190void ib_dealloc_device(struct ib_device *device)
191{
192	if (device->reg_state == IB_DEV_UNINITIALIZED) {
193		kfree(device);
194		return;
195	}
196
197	BUG_ON(device->reg_state != IB_DEV_UNREGISTERED);
198
199	kobject_put(&device->dev.kobj);
200}
201EXPORT_SYMBOL(ib_dealloc_device);
202
203static int add_client_context(struct ib_device *device, struct ib_client *client)
204{
205	struct ib_client_data *context;
206	unsigned long flags;
207
208	context = kmalloc(sizeof *context, GFP_KERNEL);
209	if (!context) {
210		printk(KERN_WARNING "Couldn't allocate client context for %s/%s\n",
211		       device->name, client->name);
212		return -ENOMEM;
213	}
214
215	context->client = client;
216	context->data   = NULL;
 
217
 
218	spin_lock_irqsave(&device->client_data_lock, flags);
219	list_add(&context->list, &device->client_data_list);
220	spin_unlock_irqrestore(&device->client_data_lock, flags);
 
221
222	return 0;
223}
224
225static int read_port_table_lengths(struct ib_device *device)
226{
227	struct ib_port_attr *tprops = NULL;
228	int num_ports, ret = -ENOMEM;
229	u8 port_index;
230
231	tprops = kmalloc(sizeof *tprops, GFP_KERNEL);
232	if (!tprops)
233		goto out;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
234
235	num_ports = end_port(device) - start_port(device) + 1;
 
 
 
 
236
237	device->pkey_tbl_len = kmalloc(sizeof *device->pkey_tbl_len * num_ports,
238				       GFP_KERNEL);
239	device->gid_tbl_len = kmalloc(sizeof *device->gid_tbl_len * num_ports,
240				      GFP_KERNEL);
241	if (!device->pkey_tbl_len || !device->gid_tbl_len)
242		goto err;
243
244	for (port_index = 0; port_index < num_ports; ++port_index) {
245		ret = ib_query_port(device, port_index + start_port(device),
246					tprops);
247		if (ret)
248			goto err;
249		device->pkey_tbl_len[port_index] = tprops->pkey_tbl_len;
250		device->gid_tbl_len[port_index]  = tprops->gid_tbl_len;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
251	}
252
253	ret = 0;
254	goto out;
255
256err:
257	kfree(device->gid_tbl_len);
258	kfree(device->pkey_tbl_len);
259out:
260	kfree(tprops);
261	return ret;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
262}
263
264/**
265 * ib_register_device - Register an IB device with IB core
266 * @device:Device to register
267 *
268 * Low-level drivers use ib_register_device() to register their
269 * devices with the IB core.  All registered clients will receive a
270 * callback for each device that is added. @device must be allocated
271 * with ib_alloc_device().
272 */
273int ib_register_device(struct ib_device *device,
274		       int (*port_callback)(struct ib_device *,
275					    u8, struct kobject *))
276{
277	int ret;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
278
279	mutex_lock(&device_mutex);
280
281	if (strchr(device->name, '%')) {
282		ret = alloc_name(device->name);
283		if (ret)
284			goto out;
285	}
286
287	if (ib_device_check_mandatory(device)) {
288		ret = -EINVAL;
289		goto out;
290	}
291
292	INIT_LIST_HEAD(&device->event_handler_list);
293	INIT_LIST_HEAD(&device->client_data_list);
294	spin_lock_init(&device->event_handler_lock);
295	spin_lock_init(&device->client_data_lock);
296
297	ret = read_port_table_lengths(device);
298	if (ret) {
299		printk(KERN_WARNING "Couldn't create table lengths cache for device %s\n",
300		       device->name);
301		goto out;
302	}
303
304	ret = ib_device_register_sysfs(device, port_callback);
305	if (ret) {
306		printk(KERN_WARNING "Couldn't register device %s with driver model\n",
307		       device->name);
308		kfree(device->gid_tbl_len);
309		kfree(device->pkey_tbl_len);
310		goto out;
311	}
312
313	list_add_tail(&device->core_list, &device_list);
 
 
 
 
314
315	device->reg_state = IB_DEV_REGISTERED;
 
 
 
 
316
317	{
318		struct ib_client *client;
 
 
 
 
319
320		list_for_each_entry(client, &client_list, list)
321			if (client->add && !add_client_context(device, client))
322				client->add(device);
 
 
323	}
324
325 out:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
326	mutex_unlock(&device_mutex);
327	return ret;
328}
329EXPORT_SYMBOL(ib_register_device);
330
331/**
332 * ib_unregister_device - Unregister an IB device
333 * @device:Device to unregister
334 *
335 * Unregister an IB device.  All clients will receive a remove callback.
336 */
337void ib_unregister_device(struct ib_device *device)
338{
339	struct ib_client *client;
340	struct ib_client_data *context, *tmp;
341	unsigned long flags;
342
343	mutex_lock(&device_mutex);
344
345	list_for_each_entry_reverse(client, &client_list, list)
346		if (client->remove)
347			client->remove(device);
348
349	list_del(&device->core_list);
 
 
 
 
 
350
351	kfree(device->gid_tbl_len);
352	kfree(device->pkey_tbl_len);
 
 
 
 
 
 
 
353
354	mutex_unlock(&device_mutex);
355
356	ib_device_unregister_sysfs(device);
357
 
 
 
 
358	spin_lock_irqsave(&device->client_data_lock, flags);
359	list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
360		kfree(context);
361	spin_unlock_irqrestore(&device->client_data_lock, flags);
 
362
363	device->reg_state = IB_DEV_UNREGISTERED;
364}
365EXPORT_SYMBOL(ib_unregister_device);
366
367/**
368 * ib_register_client - Register an IB client
369 * @client:Client to register
370 *
371 * Upper level users of the IB drivers can use ib_register_client() to
372 * register callbacks for IB device addition and removal.  When an IB
373 * device is added, each registered client's add method will be called
374 * (in the order the clients were registered), and when a device is
375 * removed, each client's remove method will be called (in the reverse
376 * order that clients were registered).  In addition, when
377 * ib_register_client() is called, the client will receive an add
378 * callback for all devices already registered.
379 */
380int ib_register_client(struct ib_client *client)
381{
382	struct ib_device *device;
383
384	mutex_lock(&device_mutex);
385
386	list_add_tail(&client->list, &client_list);
387	list_for_each_entry(device, &device_list, core_list)
388		if (client->add && !add_client_context(device, client))
389			client->add(device);
390
 
 
 
 
391	mutex_unlock(&device_mutex);
392
393	return 0;
394}
395EXPORT_SYMBOL(ib_register_client);
396
397/**
398 * ib_unregister_client - Unregister an IB client
399 * @client:Client to unregister
400 *
401 * Upper level users use ib_unregister_client() to remove their client
402 * registration.  When ib_unregister_client() is called, the client
403 * will receive a remove callback for each IB device still registered.
404 */
405void ib_unregister_client(struct ib_client *client)
406{
407	struct ib_client_data *context, *tmp;
408	struct ib_device *device;
409	unsigned long flags;
410
411	mutex_lock(&device_mutex);
412
 
 
 
 
413	list_for_each_entry(device, &device_list, core_list) {
414		if (client->remove)
415			client->remove(device);
416
 
417		spin_lock_irqsave(&device->client_data_lock, flags);
418		list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
419			if (context->client == client) {
420				list_del(&context->list);
421				kfree(context);
 
422			}
423		spin_unlock_irqrestore(&device->client_data_lock, flags);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
424	}
425	list_del(&client->list);
426
427	mutex_unlock(&device_mutex);
428}
429EXPORT_SYMBOL(ib_unregister_client);
430
431/**
432 * ib_get_client_data - Get IB client context
433 * @device:Device to get context for
434 * @client:Client to get context for
435 *
436 * ib_get_client_data() returns client context set with
437 * ib_set_client_data().
438 */
439void *ib_get_client_data(struct ib_device *device, struct ib_client *client)
440{
441	struct ib_client_data *context;
442	void *ret = NULL;
443	unsigned long flags;
444
445	spin_lock_irqsave(&device->client_data_lock, flags);
446	list_for_each_entry(context, &device->client_data_list, list)
447		if (context->client == client) {
448			ret = context->data;
449			break;
450		}
451	spin_unlock_irqrestore(&device->client_data_lock, flags);
452
453	return ret;
454}
455EXPORT_SYMBOL(ib_get_client_data);
456
457/**
458 * ib_set_client_data - Set IB client context
459 * @device:Device to set context for
460 * @client:Client to set context for
461 * @data:Context to set
462 *
463 * ib_set_client_data() sets client context that can be retrieved with
464 * ib_get_client_data().
465 */
466void ib_set_client_data(struct ib_device *device, struct ib_client *client,
467			void *data)
468{
469	struct ib_client_data *context;
470	unsigned long flags;
471
472	spin_lock_irqsave(&device->client_data_lock, flags);
473	list_for_each_entry(context, &device->client_data_list, list)
474		if (context->client == client) {
475			context->data = data;
476			goto out;
477		}
478
479	printk(KERN_WARNING "No client context found for %s/%s\n",
480	       device->name, client->name);
481
482out:
483	spin_unlock_irqrestore(&device->client_data_lock, flags);
484}
485EXPORT_SYMBOL(ib_set_client_data);
486
487/**
488 * ib_register_event_handler - Register an IB event handler
489 * @event_handler:Handler to register
490 *
491 * ib_register_event_handler() registers an event handler that will be
492 * called back when asynchronous IB events occur (as defined in
493 * chapter 11 of the InfiniBand Architecture Specification).  This
494 * callback may occur in interrupt context.
495 */
496int ib_register_event_handler  (struct ib_event_handler *event_handler)
497{
498	unsigned long flags;
499
500	spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
501	list_add_tail(&event_handler->list,
502		      &event_handler->device->event_handler_list);
503	spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
504
505	return 0;
506}
507EXPORT_SYMBOL(ib_register_event_handler);
508
509/**
510 * ib_unregister_event_handler - Unregister an event handler
511 * @event_handler:Handler to unregister
512 *
513 * Unregister an event handler registered with
514 * ib_register_event_handler().
515 */
516int ib_unregister_event_handler(struct ib_event_handler *event_handler)
517{
518	unsigned long flags;
519
520	spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
521	list_del(&event_handler->list);
522	spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
523
524	return 0;
525}
526EXPORT_SYMBOL(ib_unregister_event_handler);
527
528/**
529 * ib_dispatch_event - Dispatch an asynchronous event
530 * @event:Event to dispatch
531 *
532 * Low-level drivers must call ib_dispatch_event() to dispatch the
533 * event to all registered event handlers when an asynchronous event
534 * occurs.
535 */
536void ib_dispatch_event(struct ib_event *event)
537{
538	unsigned long flags;
539	struct ib_event_handler *handler;
540
541	spin_lock_irqsave(&event->device->event_handler_lock, flags);
542
543	list_for_each_entry(handler, &event->device->event_handler_list, list)
544		handler->handler(handler, event);
545
546	spin_unlock_irqrestore(&event->device->event_handler_lock, flags);
547}
548EXPORT_SYMBOL(ib_dispatch_event);
549
550/**
551 * ib_query_device - Query IB device attributes
552 * @device:Device to query
553 * @device_attr:Device attributes
554 *
555 * ib_query_device() returns the attributes of a device through the
556 * @device_attr pointer.
557 */
558int ib_query_device(struct ib_device *device,
559		    struct ib_device_attr *device_attr)
560{
561	return device->query_device(device, device_attr);
562}
563EXPORT_SYMBOL(ib_query_device);
564
565/**
566 * ib_query_port - Query IB port attributes
567 * @device:Device to query
568 * @port_num:Port number to query
569 * @port_attr:Port attributes
570 *
571 * ib_query_port() returns the attributes of a port through the
572 * @port_attr pointer.
573 */
574int ib_query_port(struct ib_device *device,
575		  u8 port_num,
576		  struct ib_port_attr *port_attr)
577{
578	if (port_num < start_port(device) || port_num > end_port(device))
 
 
 
579		return -EINVAL;
580
581	return device->query_port(device, port_num, port_attr);
 
 
 
 
 
 
 
 
 
 
 
 
 
582}
583EXPORT_SYMBOL(ib_query_port);
584
585/**
586 * ib_query_gid - Get GID table entry
587 * @device:Device to query
588 * @port_num:Port number to query
589 * @index:GID table index to query
590 * @gid:Returned GID
 
 
591 *
592 * ib_query_gid() fetches the specified GID table entry.
593 */
594int ib_query_gid(struct ib_device *device,
595		 u8 port_num, int index, union ib_gid *gid)
 
596{
597	return device->query_gid(device, port_num, index, gid);
598}
599EXPORT_SYMBOL(ib_query_gid);
600
601/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
602 * ib_query_pkey - Get P_Key table entry
603 * @device:Device to query
604 * @port_num:Port number to query
605 * @index:P_Key table index to query
606 * @pkey:Returned P_Key
607 *
608 * ib_query_pkey() fetches the specified P_Key table entry.
609 */
610int ib_query_pkey(struct ib_device *device,
611		  u8 port_num, u16 index, u16 *pkey)
612{
613	return device->query_pkey(device, port_num, index, pkey);
614}
615EXPORT_SYMBOL(ib_query_pkey);
616
617/**
618 * ib_modify_device - Change IB device attributes
619 * @device:Device to modify
620 * @device_modify_mask:Mask of attributes to change
621 * @device_modify:New attribute values
622 *
623 * ib_modify_device() changes a device's attributes as specified by
624 * the @device_modify_mask and @device_modify structure.
625 */
626int ib_modify_device(struct ib_device *device,
627		     int device_modify_mask,
628		     struct ib_device_modify *device_modify)
629{
630	if (!device->modify_device)
631		return -ENOSYS;
632
633	return device->modify_device(device, device_modify_mask,
634				     device_modify);
635}
636EXPORT_SYMBOL(ib_modify_device);
637
638/**
639 * ib_modify_port - Modifies the attributes for the specified port.
640 * @device: The device to modify.
641 * @port_num: The number of the port to modify.
642 * @port_modify_mask: Mask used to specify which attributes of the port
643 *   to change.
644 * @port_modify: New attribute values for the port.
645 *
646 * ib_modify_port() changes a port's attributes as specified by the
647 * @port_modify_mask and @port_modify structure.
648 */
649int ib_modify_port(struct ib_device *device,
650		   u8 port_num, int port_modify_mask,
651		   struct ib_port_modify *port_modify)
652{
653	if (!device->modify_port)
654		return -ENOSYS;
655
656	if (port_num < start_port(device) || port_num > end_port(device))
657		return -EINVAL;
658
659	return device->modify_port(device, port_num, port_modify_mask,
660				   port_modify);
 
 
 
 
661}
662EXPORT_SYMBOL(ib_modify_port);
663
664/**
665 * ib_find_gid - Returns the port number and GID table index where
666 *   a specified GID value occurs.
667 * @device: The device to query.
668 * @gid: The GID value to search for.
669 * @port_num: The port number of the device where the GID value was found.
670 * @index: The index into the GID table where the GID was found.  This
671 *   parameter may be NULL.
672 */
673int ib_find_gid(struct ib_device *device, union ib_gid *gid,
674		u8 *port_num, u16 *index)
675{
676	union ib_gid tmp_gid;
677	int ret, port, i;
678
679	for (port = start_port(device); port <= end_port(device); ++port) {
680		for (i = 0; i < device->gid_tbl_len[port - start_port(device)]; ++i) {
681			ret = ib_query_gid(device, port, i, &tmp_gid);
 
 
 
682			if (ret)
683				return ret;
684			if (!memcmp(&tmp_gid, gid, sizeof *gid)) {
685				*port_num = port;
686				if (index)
687					*index = i;
688				return 0;
689			}
690		}
691	}
692
693	return -ENOENT;
694}
695EXPORT_SYMBOL(ib_find_gid);
696
697/**
698 * ib_find_pkey - Returns the PKey table index where a specified
699 *   PKey value occurs.
700 * @device: The device to query.
701 * @port_num: The port number of the device to search for the PKey.
702 * @pkey: The PKey value to search for.
703 * @index: The index into the PKey table where the PKey was found.
704 */
705int ib_find_pkey(struct ib_device *device,
706		 u8 port_num, u16 pkey, u16 *index)
707{
708	int ret, i;
709	u16 tmp_pkey;
 
710
711	for (i = 0; i < device->pkey_tbl_len[port_num - start_port(device)]; ++i) {
712		ret = ib_query_pkey(device, port_num, i, &tmp_pkey);
713		if (ret)
714			return ret;
715
716		if ((pkey & 0x7fff) == (tmp_pkey & 0x7fff)) {
717			*index = i;
718			return 0;
 
 
 
 
 
719		}
720	}
721
 
 
 
 
 
722	return -ENOENT;
723}
724EXPORT_SYMBOL(ib_find_pkey);
725
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
726static int __init ib_core_init(void)
727{
728	int ret;
729
730	ib_wq = alloc_workqueue("infiniband", 0, 0);
731	if (!ib_wq)
732		return -ENOMEM;
733
734	ret = ib_sysfs_setup();
735	if (ret) {
736		printk(KERN_WARNING "Couldn't create InfiniBand device class\n");
 
737		goto err;
738	}
739
740	ret = ibnl_init();
 
 
 
 
 
 
741	if (ret) {
742		printk(KERN_WARNING "Couldn't init IB netlink interface\n");
743		goto err_sysfs;
744	}
745
746	ret = ib_cache_setup();
747	if (ret) {
748		printk(KERN_WARNING "Couldn't set up InfiniBand P_Key/GID cache\n");
749		goto err_nl;
750	}
751
752	return 0;
 
 
 
 
753
754err_nl:
755	ibnl_cleanup();
 
 
 
756
757err_sysfs:
758	ib_sysfs_cleanup();
 
 
 
759
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
760err:
761	destroy_workqueue(ib_wq);
762	return ret;
763}
764
765static void __exit ib_core_cleanup(void)
766{
767	ib_cache_cleanup();
768	ibnl_cleanup();
769	ib_sysfs_cleanup();
 
 
 
 
 
 
 
770	/* Make sure that any pending umem accounting work is done. */
771	destroy_workqueue(ib_wq);
772}
773
774module_init(ib_core_init);
 
 
775module_exit(ib_core_cleanup);
v4.17
   1/*
   2 * Copyright (c) 2004 Topspin Communications.  All rights reserved.
   3 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
   4 *
   5 * This software is available to you under a choice of one of two
   6 * licenses.  You may choose to be licensed under the terms of the GNU
   7 * General Public License (GPL) Version 2, available from the file
   8 * COPYING in the main directory of this source tree, or the
   9 * OpenIB.org BSD license below:
  10 *
  11 *     Redistribution and use in source and binary forms, with or
  12 *     without modification, are permitted provided that the following
  13 *     conditions are met:
  14 *
  15 *      - Redistributions of source code must retain the above
  16 *        copyright notice, this list of conditions and the following
  17 *        disclaimer.
  18 *
  19 *      - Redistributions in binary form must reproduce the above
  20 *        copyright notice, this list of conditions and the following
  21 *        disclaimer in the documentation and/or other materials
  22 *        provided with the distribution.
  23 *
  24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31 * SOFTWARE.
  32 */
  33
  34#include <linux/module.h>
  35#include <linux/string.h>
  36#include <linux/errno.h>
  37#include <linux/kernel.h>
  38#include <linux/slab.h>
  39#include <linux/init.h>
  40#include <linux/mutex.h>
  41#include <linux/netdevice.h>
  42#include <linux/security.h>
  43#include <linux/notifier.h>
  44#include <rdma/rdma_netlink.h>
  45#include <rdma/ib_addr.h>
  46#include <rdma/ib_cache.h>
  47
  48#include "core_priv.h"
  49
  50MODULE_AUTHOR("Roland Dreier");
  51MODULE_DESCRIPTION("core kernel InfiniBand API");
  52MODULE_LICENSE("Dual BSD/GPL");
  53
  54struct ib_client_data {
  55	struct list_head  list;
  56	struct ib_client *client;
  57	void *            data;
  58	/* The device or client is going down. Do not call client or device
  59	 * callbacks other than remove(). */
  60	bool		  going_down;
  61};
  62
  63struct workqueue_struct *ib_comp_wq;
  64struct workqueue_struct *ib_wq;
  65EXPORT_SYMBOL_GPL(ib_wq);
  66
  67/* The device_list and client_list contain devices and clients after their
  68 * registration has completed, and the devices and clients are removed
  69 * during unregistration. */
  70static LIST_HEAD(device_list);
  71static LIST_HEAD(client_list);
  72
  73/*
  74 * device_mutex and lists_rwsem protect access to both device_list and
  75 * client_list.  device_mutex protects writer access by device and client
  76 * registration / de-registration.  lists_rwsem protects reader access to
  77 * these lists.  Iterators of these lists must lock it for read, while updates
  78 * to the lists must be done with a write lock. A special case is when the
  79 * device_mutex is locked. In this case locking the lists for read access is
  80 * not necessary as the device_mutex implies it.
  81 *
  82 * lists_rwsem also protects access to the client data list.
  83 */
  84static DEFINE_MUTEX(device_mutex);
  85static DECLARE_RWSEM(lists_rwsem);
  86
  87static int ib_security_change(struct notifier_block *nb, unsigned long event,
  88			      void *lsm_data);
  89static void ib_policy_change_task(struct work_struct *work);
  90static DECLARE_WORK(ib_policy_change_work, ib_policy_change_task);
  91
  92static struct notifier_block ibdev_lsm_nb = {
  93	.notifier_call = ib_security_change,
  94};
  95
  96static int ib_device_check_mandatory(struct ib_device *device)
  97{
  98#define IB_MANDATORY_FUNC(x) { offsetof(struct ib_device, x), #x }
  99	static const struct {
 100		size_t offset;
 101		char  *name;
 102	} mandatory_table[] = {
 103		IB_MANDATORY_FUNC(query_device),
 104		IB_MANDATORY_FUNC(query_port),
 105		IB_MANDATORY_FUNC(query_pkey),
 
 106		IB_MANDATORY_FUNC(alloc_pd),
 107		IB_MANDATORY_FUNC(dealloc_pd),
 108		IB_MANDATORY_FUNC(create_ah),
 109		IB_MANDATORY_FUNC(destroy_ah),
 110		IB_MANDATORY_FUNC(create_qp),
 111		IB_MANDATORY_FUNC(modify_qp),
 112		IB_MANDATORY_FUNC(destroy_qp),
 113		IB_MANDATORY_FUNC(post_send),
 114		IB_MANDATORY_FUNC(post_recv),
 115		IB_MANDATORY_FUNC(create_cq),
 116		IB_MANDATORY_FUNC(destroy_cq),
 117		IB_MANDATORY_FUNC(poll_cq),
 118		IB_MANDATORY_FUNC(req_notify_cq),
 119		IB_MANDATORY_FUNC(get_dma_mr),
 120		IB_MANDATORY_FUNC(dereg_mr),
 121		IB_MANDATORY_FUNC(get_port_immutable)
 122	};
 123	int i;
 124
 125	for (i = 0; i < ARRAY_SIZE(mandatory_table); ++i) {
 126		if (!*(void **) ((void *) device + mandatory_table[i].offset)) {
 127			pr_warn("Device %s is missing mandatory function %s\n",
 128				device->name, mandatory_table[i].name);
 129			return -EINVAL;
 130		}
 131	}
 132
 133	return 0;
 134}
 135
 136static struct ib_device *__ib_device_get_by_index(u32 index)
 137{
 138	struct ib_device *device;
 139
 140	list_for_each_entry(device, &device_list, core_list)
 141		if (device->index == index)
 142			return device;
 143
 144	return NULL;
 145}
 146
 147/*
 148 * Caller is responsible to return refrerence count by calling put_device()
 149 */
 150struct ib_device *ib_device_get_by_index(u32 index)
 151{
 152	struct ib_device *device;
 153
 154	down_read(&lists_rwsem);
 155	device = __ib_device_get_by_index(index);
 156	if (device)
 157		get_device(&device->dev);
 158
 159	up_read(&lists_rwsem);
 160	return device;
 161}
 162
 163static struct ib_device *__ib_device_get_by_name(const char *name)
 164{
 165	struct ib_device *device;
 166
 167	list_for_each_entry(device, &device_list, core_list)
 168		if (!strncmp(name, device->name, IB_DEVICE_NAME_MAX))
 169			return device;
 170
 171	return NULL;
 172}
 173
 
 174static int alloc_name(char *name)
 175{
 176	unsigned long *inuse;
 177	char buf[IB_DEVICE_NAME_MAX];
 178	struct ib_device *device;
 179	int i;
 180
 181	inuse = (unsigned long *) get_zeroed_page(GFP_KERNEL);
 182	if (!inuse)
 183		return -ENOMEM;
 184
 185	list_for_each_entry(device, &device_list, core_list) {
 186		if (!sscanf(device->name, name, &i))
 187			continue;
 188		if (i < 0 || i >= PAGE_SIZE * 8)
 189			continue;
 190		snprintf(buf, sizeof buf, name, i);
 191		if (!strncmp(buf, device->name, IB_DEVICE_NAME_MAX))
 192			set_bit(i, inuse);
 193	}
 194
 195	i = find_first_zero_bit(inuse, PAGE_SIZE * 8);
 196	free_page((unsigned long) inuse);
 197	snprintf(buf, sizeof buf, name, i);
 198
 199	if (__ib_device_get_by_name(buf))
 200		return -ENFILE;
 201
 202	strlcpy(name, buf, IB_DEVICE_NAME_MAX);
 203	return 0;
 204}
 205
 206static void ib_device_release(struct device *device)
 207{
 208	struct ib_device *dev = container_of(device, struct ib_device, dev);
 
 209
 210	WARN_ON(dev->reg_state == IB_DEV_REGISTERED);
 211	if (dev->reg_state == IB_DEV_UNREGISTERED) {
 212		/*
 213		 * In IB_DEV_UNINITIALIZED state, cache or port table
 214		 * is not even created. Free cache and port table only when
 215		 * device reaches UNREGISTERED state.
 216		 */
 217		ib_cache_release_one(dev);
 218		kfree(dev->port_immutable);
 219	}
 220	kfree(dev);
 221}
 222
 223static int ib_device_uevent(struct device *device,
 224			    struct kobj_uevent_env *env)
 225{
 226	struct ib_device *dev = container_of(device, struct ib_device, dev);
 227
 228	if (add_uevent_var(env, "NAME=%s", dev->name))
 229		return -ENOMEM;
 230
 231	/*
 232	 * It would be nice to pass the node GUID with the event...
 233	 */
 234
 235	return 0;
 236}
 237
 238static struct class ib_class = {
 239	.name    = "infiniband",
 240	.dev_release = ib_device_release,
 241	.dev_uevent = ib_device_uevent,
 242};
 243
 244/**
 245 * ib_alloc_device - allocate an IB device struct
 246 * @size:size of structure to allocate
 247 *
 248 * Low-level drivers should use ib_alloc_device() to allocate &struct
 249 * ib_device.  @size is the size of the structure to be allocated,
 250 * including any private data used by the low-level driver.
 251 * ib_dealloc_device() must be used to free structures allocated with
 252 * ib_alloc_device().
 253 */
 254struct ib_device *ib_alloc_device(size_t size)
 255{
 256	struct ib_device *device;
 257
 258	if (WARN_ON(size < sizeof(struct ib_device)))
 259		return NULL;
 260
 261	device = kzalloc(size, GFP_KERNEL);
 262	if (!device)
 263		return NULL;
 264
 265	rdma_restrack_init(&device->res);
 266
 267	device->dev.class = &ib_class;
 268	device_initialize(&device->dev);
 269
 270	dev_set_drvdata(&device->dev, device);
 271
 272	INIT_LIST_HEAD(&device->event_handler_list);
 273	spin_lock_init(&device->event_handler_lock);
 274	spin_lock_init(&device->client_data_lock);
 275	INIT_LIST_HEAD(&device->client_data_list);
 276	INIT_LIST_HEAD(&device->port_list);
 277
 278	return device;
 279}
 280EXPORT_SYMBOL(ib_alloc_device);
 281
 282/**
 283 * ib_dealloc_device - free an IB device struct
 284 * @device:structure to free
 285 *
 286 * Free a structure allocated with ib_alloc_device().
 287 */
 288void ib_dealloc_device(struct ib_device *device)
 289{
 290	WARN_ON(device->reg_state != IB_DEV_UNREGISTERED &&
 291		device->reg_state != IB_DEV_UNINITIALIZED);
 292	rdma_restrack_clean(&device->res);
 293	put_device(&device->dev);
 
 
 
 
 294}
 295EXPORT_SYMBOL(ib_dealloc_device);
 296
 297static int add_client_context(struct ib_device *device, struct ib_client *client)
 298{
 299	struct ib_client_data *context;
 300	unsigned long flags;
 301
 302	context = kmalloc(sizeof *context, GFP_KERNEL);
 303	if (!context)
 
 
 304		return -ENOMEM;
 
 305
 306	context->client = client;
 307	context->data   = NULL;
 308	context->going_down = false;
 309
 310	down_write(&lists_rwsem);
 311	spin_lock_irqsave(&device->client_data_lock, flags);
 312	list_add(&context->list, &device->client_data_list);
 313	spin_unlock_irqrestore(&device->client_data_lock, flags);
 314	up_write(&lists_rwsem);
 315
 316	return 0;
 317}
 318
 319static int verify_immutable(const struct ib_device *dev, u8 port)
 320{
 321	return WARN_ON(!rdma_cap_ib_mad(dev, port) &&
 322			    rdma_max_mad_size(dev, port) != 0);
 323}
 324
 325static int read_port_immutable(struct ib_device *device)
 326{
 327	int ret;
 328	u8 start_port = rdma_start_port(device);
 329	u8 end_port = rdma_end_port(device);
 330	u8 port;
 331
 332	/**
 333	 * device->port_immutable is indexed directly by the port number to make
 334	 * access to this data as efficient as possible.
 335	 *
 336	 * Therefore port_immutable is declared as a 1 based array with
 337	 * potential empty slots at the beginning.
 338	 */
 339	device->port_immutable = kzalloc(sizeof(*device->port_immutable)
 340					 * (end_port + 1),
 341					 GFP_KERNEL);
 342	if (!device->port_immutable)
 343		return -ENOMEM;
 344
 345	for (port = start_port; port <= end_port; ++port) {
 346		ret = device->get_port_immutable(device, port,
 347						 &device->port_immutable[port]);
 348		if (ret)
 349			return ret;
 350
 351		if (verify_immutable(device, port))
 352			return -EINVAL;
 353	}
 354	return 0;
 355}
 
 356
 357void ib_get_device_fw_str(struct ib_device *dev, char *str)
 358{
 359	if (dev->get_dev_fw_str)
 360		dev->get_dev_fw_str(dev, str);
 361	else
 362		str[0] = '\0';
 363}
 364EXPORT_SYMBOL(ib_get_device_fw_str);
 365
 366static int setup_port_pkey_list(struct ib_device *device)
 367{
 368	int i;
 369
 370	/**
 371	 * device->port_pkey_list is indexed directly by the port number,
 372	 * Therefore it is declared as a 1 based array with potential empty
 373	 * slots at the beginning.
 374	 */
 375	device->port_pkey_list = kcalloc(rdma_end_port(device) + 1,
 376					 sizeof(*device->port_pkey_list),
 377					 GFP_KERNEL);
 378
 379	if (!device->port_pkey_list)
 380		return -ENOMEM;
 381
 382	for (i = 0; i < (rdma_end_port(device) + 1); i++) {
 383		spin_lock_init(&device->port_pkey_list[i].list_lock);
 384		INIT_LIST_HEAD(&device->port_pkey_list[i].pkey_list);
 385	}
 386
 387	return 0;
 388}
 389
 390static void ib_policy_change_task(struct work_struct *work)
 391{
 392	struct ib_device *dev;
 393
 394	down_read(&lists_rwsem);
 395	list_for_each_entry(dev, &device_list, core_list) {
 396		int i;
 397
 398		for (i = rdma_start_port(dev); i <= rdma_end_port(dev); i++) {
 399			u64 sp;
 400			int ret = ib_get_cached_subnet_prefix(dev,
 401							      i,
 402							      &sp);
 403
 404			WARN_ONCE(ret,
 405				  "ib_get_cached_subnet_prefix err: %d, this should never happen here\n",
 406				  ret);
 407			if (!ret)
 408				ib_security_cache_change(dev, i, sp);
 409		}
 410	}
 411	up_read(&lists_rwsem);
 412}
 413
 414static int ib_security_change(struct notifier_block *nb, unsigned long event,
 415			      void *lsm_data)
 416{
 417	if (event != LSM_POLICY_CHANGE)
 418		return NOTIFY_DONE;
 419
 420	schedule_work(&ib_policy_change_work);
 421
 422	return NOTIFY_OK;
 423}
 424
 425/**
 426 *	__dev_new_index	-	allocate an device index
 427 *
 428 *	Returns a suitable unique value for a new device interface
 429 *	number.  It assumes that there are less than 2^32-1 ib devices
 430 *	will be present in the system.
 431 */
 432static u32 __dev_new_index(void)
 433{
 434	/*
 435	 * The device index to allow stable naming.
 436	 * Similar to struct net -> ifindex.
 437	 */
 438	static u32 index;
 439
 440	for (;;) {
 441		if (!(++index))
 442			index = 1;
 443
 444		if (!__ib_device_get_by_index(index))
 445			return index;
 446	}
 447}
 448
 449/**
 450 * ib_register_device - Register an IB device with IB core
 451 * @device:Device to register
 452 *
 453 * Low-level drivers use ib_register_device() to register their
 454 * devices with the IB core.  All registered clients will receive a
 455 * callback for each device that is added. @device must be allocated
 456 * with ib_alloc_device().
 457 */
 458int ib_register_device(struct ib_device *device,
 459		       int (*port_callback)(struct ib_device *,
 460					    u8, struct kobject *))
 461{
 462	int ret;
 463	struct ib_client *client;
 464	struct ib_udata uhw = {.outlen = 0, .inlen = 0};
 465	struct device *parent = device->dev.parent;
 466
 467	WARN_ON_ONCE(device->dma_device);
 468	if (device->dev.dma_ops) {
 469		/*
 470		 * The caller provided custom DMA operations. Copy the
 471		 * DMA-related fields that are used by e.g. dma_alloc_coherent()
 472		 * into device->dev.
 473		 */
 474		device->dma_device = &device->dev;
 475		if (!device->dev.dma_mask) {
 476			if (parent)
 477				device->dev.dma_mask = parent->dma_mask;
 478			else
 479				WARN_ON_ONCE(true);
 480		}
 481		if (!device->dev.coherent_dma_mask) {
 482			if (parent)
 483				device->dev.coherent_dma_mask =
 484					parent->coherent_dma_mask;
 485			else
 486				WARN_ON_ONCE(true);
 487		}
 488	} else {
 489		/*
 490		 * The caller did not provide custom DMA operations. Use the
 491		 * DMA mapping operations of the parent device.
 492		 */
 493		WARN_ON_ONCE(!parent);
 494		device->dma_device = parent;
 495	}
 496
 497	mutex_lock(&device_mutex);
 498
 499	if (strchr(device->name, '%')) {
 500		ret = alloc_name(device->name);
 501		if (ret)
 502			goto out;
 503	}
 504
 505	if (ib_device_check_mandatory(device)) {
 506		ret = -EINVAL;
 507		goto out;
 508	}
 509
 510	ret = read_port_immutable(device);
 
 
 
 
 
 511	if (ret) {
 512		pr_warn("Couldn't create per port immutable data %s\n",
 513			device->name);
 514		goto out;
 515	}
 516
 517	ret = setup_port_pkey_list(device);
 518	if (ret) {
 519		pr_warn("Couldn't create per port_pkey_list\n");
 
 
 
 520		goto out;
 521	}
 522
 523	ret = ib_cache_setup_one(device);
 524	if (ret) {
 525		pr_warn("Couldn't set up InfiniBand P_Key/GID cache\n");
 526		goto port_cleanup;
 527	}
 528
 529	ret = ib_device_register_rdmacg(device);
 530	if (ret) {
 531		pr_warn("Couldn't register device with rdma cgroup\n");
 532		goto cache_cleanup;
 533	}
 534
 535	memset(&device->attrs, 0, sizeof(device->attrs));
 536	ret = device->query_device(device, &device->attrs, &uhw);
 537	if (ret) {
 538		pr_warn("Couldn't query the device attributes\n");
 539		goto cg_cleanup;
 540	}
 541
 542	ret = ib_device_register_sysfs(device, port_callback);
 543	if (ret) {
 544		pr_warn("Couldn't register device %s with driver model\n",
 545			device->name);
 546		goto cg_cleanup;
 547	}
 548
 549	device->reg_state = IB_DEV_REGISTERED;
 550
 551	list_for_each_entry(client, &client_list, list)
 552		if (!add_client_context(device, client) && client->add)
 553			client->add(device);
 554
 555	device->index = __dev_new_index();
 556	down_write(&lists_rwsem);
 557	list_add_tail(&device->core_list, &device_list);
 558	up_write(&lists_rwsem);
 559	mutex_unlock(&device_mutex);
 560	return 0;
 561
 562cg_cleanup:
 563	ib_device_unregister_rdmacg(device);
 564cache_cleanup:
 565	ib_cache_cleanup_one(device);
 566	ib_cache_release_one(device);
 567port_cleanup:
 568	kfree(device->port_immutable);
 569out:
 570	mutex_unlock(&device_mutex);
 571	return ret;
 572}
 573EXPORT_SYMBOL(ib_register_device);
 574
 575/**
 576 * ib_unregister_device - Unregister an IB device
 577 * @device:Device to unregister
 578 *
 579 * Unregister an IB device.  All clients will receive a remove callback.
 580 */
 581void ib_unregister_device(struct ib_device *device)
 582{
 
 583	struct ib_client_data *context, *tmp;
 584	unsigned long flags;
 585
 586	mutex_lock(&device_mutex);
 587
 588	down_write(&lists_rwsem);
 
 
 
 589	list_del(&device->core_list);
 590	spin_lock_irqsave(&device->client_data_lock, flags);
 591	list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
 592		context->going_down = true;
 593	spin_unlock_irqrestore(&device->client_data_lock, flags);
 594	downgrade_write(&lists_rwsem);
 595
 596	list_for_each_entry_safe(context, tmp, &device->client_data_list,
 597				 list) {
 598		if (context->client->remove)
 599			context->client->remove(device, context->data);
 600	}
 601	up_read(&lists_rwsem);
 602
 603	ib_device_unregister_rdmacg(device);
 604	ib_device_unregister_sysfs(device);
 605
 606	mutex_unlock(&device_mutex);
 607
 608	ib_cache_cleanup_one(device);
 609
 610	ib_security_destroy_port_pkey_list(device);
 611	kfree(device->port_pkey_list);
 612
 613	down_write(&lists_rwsem);
 614	spin_lock_irqsave(&device->client_data_lock, flags);
 615	list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
 616		kfree(context);
 617	spin_unlock_irqrestore(&device->client_data_lock, flags);
 618	up_write(&lists_rwsem);
 619
 620	device->reg_state = IB_DEV_UNREGISTERED;
 621}
 622EXPORT_SYMBOL(ib_unregister_device);
 623
 624/**
 625 * ib_register_client - Register an IB client
 626 * @client:Client to register
 627 *
 628 * Upper level users of the IB drivers can use ib_register_client() to
 629 * register callbacks for IB device addition and removal.  When an IB
 630 * device is added, each registered client's add method will be called
 631 * (in the order the clients were registered), and when a device is
 632 * removed, each client's remove method will be called (in the reverse
 633 * order that clients were registered).  In addition, when
 634 * ib_register_client() is called, the client will receive an add
 635 * callback for all devices already registered.
 636 */
 637int ib_register_client(struct ib_client *client)
 638{
 639	struct ib_device *device;
 640
 641	mutex_lock(&device_mutex);
 642
 
 643	list_for_each_entry(device, &device_list, core_list)
 644		if (!add_client_context(device, client) && client->add)
 645			client->add(device);
 646
 647	down_write(&lists_rwsem);
 648	list_add_tail(&client->list, &client_list);
 649	up_write(&lists_rwsem);
 650
 651	mutex_unlock(&device_mutex);
 652
 653	return 0;
 654}
 655EXPORT_SYMBOL(ib_register_client);
 656
 657/**
 658 * ib_unregister_client - Unregister an IB client
 659 * @client:Client to unregister
 660 *
 661 * Upper level users use ib_unregister_client() to remove their client
 662 * registration.  When ib_unregister_client() is called, the client
 663 * will receive a remove callback for each IB device still registered.
 664 */
 665void ib_unregister_client(struct ib_client *client)
 666{
 667	struct ib_client_data *context, *tmp;
 668	struct ib_device *device;
 669	unsigned long flags;
 670
 671	mutex_lock(&device_mutex);
 672
 673	down_write(&lists_rwsem);
 674	list_del(&client->list);
 675	up_write(&lists_rwsem);
 676
 677	list_for_each_entry(device, &device_list, core_list) {
 678		struct ib_client_data *found_context = NULL;
 
 679
 680		down_write(&lists_rwsem);
 681		spin_lock_irqsave(&device->client_data_lock, flags);
 682		list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
 683			if (context->client == client) {
 684				context->going_down = true;
 685				found_context = context;
 686				break;
 687			}
 688		spin_unlock_irqrestore(&device->client_data_lock, flags);
 689		up_write(&lists_rwsem);
 690
 691		if (client->remove)
 692			client->remove(device, found_context ?
 693					       found_context->data : NULL);
 694
 695		if (!found_context) {
 696			pr_warn("No client context found for %s/%s\n",
 697				device->name, client->name);
 698			continue;
 699		}
 700
 701		down_write(&lists_rwsem);
 702		spin_lock_irqsave(&device->client_data_lock, flags);
 703		list_del(&found_context->list);
 704		kfree(found_context);
 705		spin_unlock_irqrestore(&device->client_data_lock, flags);
 706		up_write(&lists_rwsem);
 707	}
 
 708
 709	mutex_unlock(&device_mutex);
 710}
 711EXPORT_SYMBOL(ib_unregister_client);
 712
 713/**
 714 * ib_get_client_data - Get IB client context
 715 * @device:Device to get context for
 716 * @client:Client to get context for
 717 *
 718 * ib_get_client_data() returns client context set with
 719 * ib_set_client_data().
 720 */
 721void *ib_get_client_data(struct ib_device *device, struct ib_client *client)
 722{
 723	struct ib_client_data *context;
 724	void *ret = NULL;
 725	unsigned long flags;
 726
 727	spin_lock_irqsave(&device->client_data_lock, flags);
 728	list_for_each_entry(context, &device->client_data_list, list)
 729		if (context->client == client) {
 730			ret = context->data;
 731			break;
 732		}
 733	spin_unlock_irqrestore(&device->client_data_lock, flags);
 734
 735	return ret;
 736}
 737EXPORT_SYMBOL(ib_get_client_data);
 738
 739/**
 740 * ib_set_client_data - Set IB client context
 741 * @device:Device to set context for
 742 * @client:Client to set context for
 743 * @data:Context to set
 744 *
 745 * ib_set_client_data() sets client context that can be retrieved with
 746 * ib_get_client_data().
 747 */
 748void ib_set_client_data(struct ib_device *device, struct ib_client *client,
 749			void *data)
 750{
 751	struct ib_client_data *context;
 752	unsigned long flags;
 753
 754	spin_lock_irqsave(&device->client_data_lock, flags);
 755	list_for_each_entry(context, &device->client_data_list, list)
 756		if (context->client == client) {
 757			context->data = data;
 758			goto out;
 759		}
 760
 761	pr_warn("No client context found for %s/%s\n",
 762		device->name, client->name);
 763
 764out:
 765	spin_unlock_irqrestore(&device->client_data_lock, flags);
 766}
 767EXPORT_SYMBOL(ib_set_client_data);
 768
 769/**
 770 * ib_register_event_handler - Register an IB event handler
 771 * @event_handler:Handler to register
 772 *
 773 * ib_register_event_handler() registers an event handler that will be
 774 * called back when asynchronous IB events occur (as defined in
 775 * chapter 11 of the InfiniBand Architecture Specification).  This
 776 * callback may occur in interrupt context.
 777 */
 778void ib_register_event_handler(struct ib_event_handler *event_handler)
 779{
 780	unsigned long flags;
 781
 782	spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
 783	list_add_tail(&event_handler->list,
 784		      &event_handler->device->event_handler_list);
 785	spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
 
 
 786}
 787EXPORT_SYMBOL(ib_register_event_handler);
 788
 789/**
 790 * ib_unregister_event_handler - Unregister an event handler
 791 * @event_handler:Handler to unregister
 792 *
 793 * Unregister an event handler registered with
 794 * ib_register_event_handler().
 795 */
 796void ib_unregister_event_handler(struct ib_event_handler *event_handler)
 797{
 798	unsigned long flags;
 799
 800	spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
 801	list_del(&event_handler->list);
 802	spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
 
 
 803}
 804EXPORT_SYMBOL(ib_unregister_event_handler);
 805
 806/**
 807 * ib_dispatch_event - Dispatch an asynchronous event
 808 * @event:Event to dispatch
 809 *
 810 * Low-level drivers must call ib_dispatch_event() to dispatch the
 811 * event to all registered event handlers when an asynchronous event
 812 * occurs.
 813 */
 814void ib_dispatch_event(struct ib_event *event)
 815{
 816	unsigned long flags;
 817	struct ib_event_handler *handler;
 818
 819	spin_lock_irqsave(&event->device->event_handler_lock, flags);
 820
 821	list_for_each_entry(handler, &event->device->event_handler_list, list)
 822		handler->handler(handler, event);
 823
 824	spin_unlock_irqrestore(&event->device->event_handler_lock, flags);
 825}
 826EXPORT_SYMBOL(ib_dispatch_event);
 827
 828/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 829 * ib_query_port - Query IB port attributes
 830 * @device:Device to query
 831 * @port_num:Port number to query
 832 * @port_attr:Port attributes
 833 *
 834 * ib_query_port() returns the attributes of a port through the
 835 * @port_attr pointer.
 836 */
 837int ib_query_port(struct ib_device *device,
 838		  u8 port_num,
 839		  struct ib_port_attr *port_attr)
 840{
 841	union ib_gid gid;
 842	int err;
 843
 844	if (!rdma_is_port_valid(device, port_num))
 845		return -EINVAL;
 846
 847	memset(port_attr, 0, sizeof(*port_attr));
 848	err = device->query_port(device, port_num, port_attr);
 849	if (err || port_attr->subnet_prefix)
 850		return err;
 851
 852	if (rdma_port_get_link_layer(device, port_num) != IB_LINK_LAYER_INFINIBAND)
 853		return 0;
 854
 855	err = device->query_gid(device, port_num, 0, &gid);
 856	if (err)
 857		return err;
 858
 859	port_attr->subnet_prefix = be64_to_cpu(gid.global.subnet_prefix);
 860	return 0;
 861}
 862EXPORT_SYMBOL(ib_query_port);
 863
 864/**
 865 * ib_query_gid - Get GID table entry
 866 * @device:Device to query
 867 * @port_num:Port number to query
 868 * @index:GID table index to query
 869 * @gid:Returned GID
 870 * @attr: Returned GID attributes related to this GID index (only in RoCE).
 871 *   NULL means ignore.
 872 *
 873 * ib_query_gid() fetches the specified GID table entry from the cache.
 874 */
 875int ib_query_gid(struct ib_device *device,
 876		 u8 port_num, int index, union ib_gid *gid,
 877		 struct ib_gid_attr *attr)
 878{
 879	return ib_get_cached_gid(device, port_num, index, gid, attr);
 880}
 881EXPORT_SYMBOL(ib_query_gid);
 882
 883/**
 884 * ib_enum_roce_netdev - enumerate all RoCE ports
 885 * @ib_dev : IB device we want to query
 886 * @filter: Should we call the callback?
 887 * @filter_cookie: Cookie passed to filter
 888 * @cb: Callback to call for each found RoCE ports
 889 * @cookie: Cookie passed back to the callback
 890 *
 891 * Enumerates all of the physical RoCE ports of ib_dev
 892 * which are related to netdevice and calls callback() on each
 893 * device for which filter() function returns non zero.
 894 */
 895void ib_enum_roce_netdev(struct ib_device *ib_dev,
 896			 roce_netdev_filter filter,
 897			 void *filter_cookie,
 898			 roce_netdev_callback cb,
 899			 void *cookie)
 900{
 901	u8 port;
 902
 903	for (port = rdma_start_port(ib_dev); port <= rdma_end_port(ib_dev);
 904	     port++)
 905		if (rdma_protocol_roce(ib_dev, port)) {
 906			struct net_device *idev = NULL;
 907
 908			if (ib_dev->get_netdev)
 909				idev = ib_dev->get_netdev(ib_dev, port);
 910
 911			if (idev &&
 912			    idev->reg_state >= NETREG_UNREGISTERED) {
 913				dev_put(idev);
 914				idev = NULL;
 915			}
 916
 917			if (filter(ib_dev, port, idev, filter_cookie))
 918				cb(ib_dev, port, idev, cookie);
 919
 920			if (idev)
 921				dev_put(idev);
 922		}
 923}
 924
 925/**
 926 * ib_enum_all_roce_netdevs - enumerate all RoCE devices
 927 * @filter: Should we call the callback?
 928 * @filter_cookie: Cookie passed to filter
 929 * @cb: Callback to call for each found RoCE ports
 930 * @cookie: Cookie passed back to the callback
 931 *
 932 * Enumerates all RoCE devices' physical ports which are related
 933 * to netdevices and calls callback() on each device for which
 934 * filter() function returns non zero.
 935 */
 936void ib_enum_all_roce_netdevs(roce_netdev_filter filter,
 937			      void *filter_cookie,
 938			      roce_netdev_callback cb,
 939			      void *cookie)
 940{
 941	struct ib_device *dev;
 942
 943	down_read(&lists_rwsem);
 944	list_for_each_entry(dev, &device_list, core_list)
 945		ib_enum_roce_netdev(dev, filter, filter_cookie, cb, cookie);
 946	up_read(&lists_rwsem);
 947}
 948
 949/**
 950 * ib_enum_all_devs - enumerate all ib_devices
 951 * @cb: Callback to call for each found ib_device
 952 *
 953 * Enumerates all ib_devices and calls callback() on each device.
 954 */
 955int ib_enum_all_devs(nldev_callback nldev_cb, struct sk_buff *skb,
 956		     struct netlink_callback *cb)
 957{
 958	struct ib_device *dev;
 959	unsigned int idx = 0;
 960	int ret = 0;
 961
 962	down_read(&lists_rwsem);
 963	list_for_each_entry(dev, &device_list, core_list) {
 964		ret = nldev_cb(dev, skb, cb, idx);
 965		if (ret)
 966			break;
 967		idx++;
 968	}
 969
 970	up_read(&lists_rwsem);
 971	return ret;
 972}
 973
 974/**
 975 * ib_query_pkey - Get P_Key table entry
 976 * @device:Device to query
 977 * @port_num:Port number to query
 978 * @index:P_Key table index to query
 979 * @pkey:Returned P_Key
 980 *
 981 * ib_query_pkey() fetches the specified P_Key table entry.
 982 */
 983int ib_query_pkey(struct ib_device *device,
 984		  u8 port_num, u16 index, u16 *pkey)
 985{
 986	return device->query_pkey(device, port_num, index, pkey);
 987}
 988EXPORT_SYMBOL(ib_query_pkey);
 989
 990/**
 991 * ib_modify_device - Change IB device attributes
 992 * @device:Device to modify
 993 * @device_modify_mask:Mask of attributes to change
 994 * @device_modify:New attribute values
 995 *
 996 * ib_modify_device() changes a device's attributes as specified by
 997 * the @device_modify_mask and @device_modify structure.
 998 */
 999int ib_modify_device(struct ib_device *device,
1000		     int device_modify_mask,
1001		     struct ib_device_modify *device_modify)
1002{
1003	if (!device->modify_device)
1004		return -ENOSYS;
1005
1006	return device->modify_device(device, device_modify_mask,
1007				     device_modify);
1008}
1009EXPORT_SYMBOL(ib_modify_device);
1010
1011/**
1012 * ib_modify_port - Modifies the attributes for the specified port.
1013 * @device: The device to modify.
1014 * @port_num: The number of the port to modify.
1015 * @port_modify_mask: Mask used to specify which attributes of the port
1016 *   to change.
1017 * @port_modify: New attribute values for the port.
1018 *
1019 * ib_modify_port() changes a port's attributes as specified by the
1020 * @port_modify_mask and @port_modify structure.
1021 */
1022int ib_modify_port(struct ib_device *device,
1023		   u8 port_num, int port_modify_mask,
1024		   struct ib_port_modify *port_modify)
1025{
1026	int rc;
 
1027
1028	if (!rdma_is_port_valid(device, port_num))
1029		return -EINVAL;
1030
1031	if (device->modify_port)
1032		rc = device->modify_port(device, port_num, port_modify_mask,
1033					   port_modify);
1034	else
1035		rc = rdma_protocol_roce(device, port_num) ? 0 : -ENOSYS;
1036	return rc;
1037}
1038EXPORT_SYMBOL(ib_modify_port);
1039
1040/**
1041 * ib_find_gid - Returns the port number and GID table index where
1042 *   a specified GID value occurs. Its searches only for IB link layer.
1043 * @device: The device to query.
1044 * @gid: The GID value to search for.
1045 * @port_num: The port number of the device where the GID value was found.
1046 * @index: The index into the GID table where the GID was found.  This
1047 *   parameter may be NULL.
1048 */
1049int ib_find_gid(struct ib_device *device, union ib_gid *gid,
1050		u8 *port_num, u16 *index)
1051{
1052	union ib_gid tmp_gid;
1053	int ret, port, i;
1054
1055	for (port = rdma_start_port(device); port <= rdma_end_port(device); ++port) {
1056		if (!rdma_protocol_ib(device, port))
1057			continue;
1058
1059		for (i = 0; i < device->port_immutable[port].gid_tbl_len; ++i) {
1060			ret = ib_query_gid(device, port, i, &tmp_gid, NULL);
1061			if (ret)
1062				return ret;
1063			if (!memcmp(&tmp_gid, gid, sizeof *gid)) {
1064				*port_num = port;
1065				if (index)
1066					*index = i;
1067				return 0;
1068			}
1069		}
1070	}
1071
1072	return -ENOENT;
1073}
1074EXPORT_SYMBOL(ib_find_gid);
1075
1076/**
1077 * ib_find_pkey - Returns the PKey table index where a specified
1078 *   PKey value occurs.
1079 * @device: The device to query.
1080 * @port_num: The port number of the device to search for the PKey.
1081 * @pkey: The PKey value to search for.
1082 * @index: The index into the PKey table where the PKey was found.
1083 */
1084int ib_find_pkey(struct ib_device *device,
1085		 u8 port_num, u16 pkey, u16 *index)
1086{
1087	int ret, i;
1088	u16 tmp_pkey;
1089	int partial_ix = -1;
1090
1091	for (i = 0; i < device->port_immutable[port_num].pkey_tbl_len; ++i) {
1092		ret = ib_query_pkey(device, port_num, i, &tmp_pkey);
1093		if (ret)
1094			return ret;
 
1095		if ((pkey & 0x7fff) == (tmp_pkey & 0x7fff)) {
1096			/* if there is full-member pkey take it.*/
1097			if (tmp_pkey & 0x8000) {
1098				*index = i;
1099				return 0;
1100			}
1101			if (partial_ix < 0)
1102				partial_ix = i;
1103		}
1104	}
1105
1106	/*no full-member, if exists take the limited*/
1107	if (partial_ix >= 0) {
1108		*index = partial_ix;
1109		return 0;
1110	}
1111	return -ENOENT;
1112}
1113EXPORT_SYMBOL(ib_find_pkey);
1114
1115/**
1116 * ib_get_net_dev_by_params() - Return the appropriate net_dev
1117 * for a received CM request
1118 * @dev:	An RDMA device on which the request has been received.
1119 * @port:	Port number on the RDMA device.
1120 * @pkey:	The Pkey the request came on.
1121 * @gid:	A GID that the net_dev uses to communicate.
1122 * @addr:	Contains the IP address that the request specified as its
1123 *		destination.
1124 */
1125struct net_device *ib_get_net_dev_by_params(struct ib_device *dev,
1126					    u8 port,
1127					    u16 pkey,
1128					    const union ib_gid *gid,
1129					    const struct sockaddr *addr)
1130{
1131	struct net_device *net_dev = NULL;
1132	struct ib_client_data *context;
1133
1134	if (!rdma_protocol_ib(dev, port))
1135		return NULL;
1136
1137	down_read(&lists_rwsem);
1138
1139	list_for_each_entry(context, &dev->client_data_list, list) {
1140		struct ib_client *client = context->client;
1141
1142		if (context->going_down)
1143			continue;
1144
1145		if (client->get_net_dev_by_params) {
1146			net_dev = client->get_net_dev_by_params(dev, port, pkey,
1147								gid, addr,
1148								context->data);
1149			if (net_dev)
1150				break;
1151		}
1152	}
1153
1154	up_read(&lists_rwsem);
1155
1156	return net_dev;
1157}
1158EXPORT_SYMBOL(ib_get_net_dev_by_params);
1159
1160static const struct rdma_nl_cbs ibnl_ls_cb_table[RDMA_NL_LS_NUM_OPS] = {
1161	[RDMA_NL_LS_OP_RESOLVE] = {
1162		.doit = ib_nl_handle_resolve_resp,
1163		.flags = RDMA_NL_ADMIN_PERM,
1164	},
1165	[RDMA_NL_LS_OP_SET_TIMEOUT] = {
1166		.doit = ib_nl_handle_set_timeout,
1167		.flags = RDMA_NL_ADMIN_PERM,
1168	},
1169	[RDMA_NL_LS_OP_IP_RESOLVE] = {
1170		.doit = ib_nl_handle_ip_res_resp,
1171		.flags = RDMA_NL_ADMIN_PERM,
1172	},
1173};
1174
1175static int __init ib_core_init(void)
1176{
1177	int ret;
1178
1179	ib_wq = alloc_workqueue("infiniband", 0, 0);
1180	if (!ib_wq)
1181		return -ENOMEM;
1182
1183	ib_comp_wq = alloc_workqueue("ib-comp-wq",
1184			WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
1185	if (!ib_comp_wq) {
1186		ret = -ENOMEM;
1187		goto err;
1188	}
1189
1190	ret = class_register(&ib_class);
1191	if (ret) {
1192		pr_warn("Couldn't create InfiniBand device class\n");
1193		goto err_comp;
1194	}
1195
1196	ret = rdma_nl_init();
1197	if (ret) {
1198		pr_warn("Couldn't init IB netlink interface: err %d\n", ret);
1199		goto err_sysfs;
1200	}
1201
1202	ret = addr_init();
1203	if (ret) {
1204		pr_warn("Could't init IB address resolution\n");
1205		goto err_ibnl;
1206	}
1207
1208	ret = ib_mad_init();
1209	if (ret) {
1210		pr_warn("Couldn't init IB MAD\n");
1211		goto err_addr;
1212	}
1213
1214	ret = ib_sa_init();
1215	if (ret) {
1216		pr_warn("Couldn't init SA\n");
1217		goto err_mad;
1218	}
1219
1220	ret = register_lsm_notifier(&ibdev_lsm_nb);
1221	if (ret) {
1222		pr_warn("Couldn't register LSM notifier. ret %d\n", ret);
1223		goto err_sa;
1224	}
1225
1226	nldev_init();
1227	rdma_nl_register(RDMA_NL_LS, ibnl_ls_cb_table);
1228	ib_cache_setup();
1229
1230	return 0;
1231
1232err_sa:
1233	ib_sa_cleanup();
1234err_mad:
1235	ib_mad_cleanup();
1236err_addr:
1237	addr_cleanup();
1238err_ibnl:
1239	rdma_nl_exit();
1240err_sysfs:
1241	class_unregister(&ib_class);
1242err_comp:
1243	destroy_workqueue(ib_comp_wq);
1244err:
1245	destroy_workqueue(ib_wq);
1246	return ret;
1247}
1248
1249static void __exit ib_core_cleanup(void)
1250{
1251	ib_cache_cleanup();
1252	nldev_exit();
1253	rdma_nl_unregister(RDMA_NL_LS);
1254	unregister_lsm_notifier(&ibdev_lsm_nb);
1255	ib_sa_cleanup();
1256	ib_mad_cleanup();
1257	addr_cleanup();
1258	rdma_nl_exit();
1259	class_unregister(&ib_class);
1260	destroy_workqueue(ib_comp_wq);
1261	/* Make sure that any pending umem accounting work is done. */
1262	destroy_workqueue(ib_wq);
1263}
1264
1265MODULE_ALIAS_RDMA_NETLINK(RDMA_NL_LS, 4);
1266
1267subsys_initcall(ib_core_init);
1268module_exit(ib_core_cleanup);