xref: /linux/drivers/infiniband/core/device.c (revision 1a7a05e88fa0e4e168f83585d1bb1937197a9745)
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 
50 MODULE_AUTHOR("Roland Dreier");
51 MODULE_DESCRIPTION("core kernel InfiniBand API");
52 MODULE_LICENSE("Dual BSD/GPL");
53 
54 struct 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 
63 struct workqueue_struct *ib_comp_wq;
64 struct workqueue_struct *ib_comp_unbound_wq;
65 struct workqueue_struct *ib_wq;
66 EXPORT_SYMBOL_GPL(ib_wq);
67 
68 /* The device_list and client_list contain devices and clients after their
69  * registration has completed, and the devices and clients are removed
70  * during unregistration. */
71 static LIST_HEAD(device_list);
72 static LIST_HEAD(client_list);
73 
74 /*
75  * device_mutex and lists_rwsem protect access to both device_list and
76  * client_list.  device_mutex protects writer access by device and client
77  * registration / de-registration.  lists_rwsem protects reader access to
78  * these lists.  Iterators of these lists must lock it for read, while updates
79  * to the lists must be done with a write lock. A special case is when the
80  * device_mutex is locked. In this case locking the lists for read access is
81  * not necessary as the device_mutex implies it.
82  *
83  * lists_rwsem also protects access to the client data list.
84  */
85 static DEFINE_MUTEX(device_mutex);
86 static DECLARE_RWSEM(lists_rwsem);
87 
88 static int ib_security_change(struct notifier_block *nb, unsigned long event,
89 			      void *lsm_data);
90 static void ib_policy_change_task(struct work_struct *work);
91 static DECLARE_WORK(ib_policy_change_work, ib_policy_change_task);
92 
93 static struct notifier_block ibdev_lsm_nb = {
94 	.notifier_call = ib_security_change,
95 };
96 
97 static int ib_device_check_mandatory(struct ib_device *device)
98 {
99 #define IB_MANDATORY_FUNC(x) { offsetof(struct ib_device_ops, x), #x }
100 	static const struct {
101 		size_t offset;
102 		char  *name;
103 	} mandatory_table[] = {
104 		IB_MANDATORY_FUNC(query_device),
105 		IB_MANDATORY_FUNC(query_port),
106 		IB_MANDATORY_FUNC(query_pkey),
107 		IB_MANDATORY_FUNC(alloc_pd),
108 		IB_MANDATORY_FUNC(dealloc_pd),
109 		IB_MANDATORY_FUNC(create_qp),
110 		IB_MANDATORY_FUNC(modify_qp),
111 		IB_MANDATORY_FUNC(destroy_qp),
112 		IB_MANDATORY_FUNC(post_send),
113 		IB_MANDATORY_FUNC(post_recv),
114 		IB_MANDATORY_FUNC(create_cq),
115 		IB_MANDATORY_FUNC(destroy_cq),
116 		IB_MANDATORY_FUNC(poll_cq),
117 		IB_MANDATORY_FUNC(req_notify_cq),
118 		IB_MANDATORY_FUNC(get_dma_mr),
119 		IB_MANDATORY_FUNC(dereg_mr),
120 		IB_MANDATORY_FUNC(get_port_immutable)
121 	};
122 	int i;
123 
124 	device->kverbs_provider = true;
125 	for (i = 0; i < ARRAY_SIZE(mandatory_table); ++i) {
126 		if (!*(void **) ((void *) &device->ops +
127 				 mandatory_table[i].offset)) {
128 			device->kverbs_provider = false;
129 			break;
130 		}
131 	}
132 
133 	return 0;
134 }
135 
136 static 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 must perform ib_device_put() to return the device reference count
149  * when ib_device_get_by_index() returns valid device pointer.
150  */
151 struct ib_device *ib_device_get_by_index(u32 index)
152 {
153 	struct ib_device *device;
154 
155 	down_read(&lists_rwsem);
156 	device = __ib_device_get_by_index(index);
157 	if (device) {
158 		if (!ib_device_try_get(device))
159 			device = NULL;
160 	}
161 	up_read(&lists_rwsem);
162 	return device;
163 }
164 
165 /**
166  * ib_device_put - Release IB device reference
167  * @device: device whose reference to be released
168  *
169  * ib_device_put() releases reference to the IB device to allow it to be
170  * unregistered and eventually free.
171  */
172 void ib_device_put(struct ib_device *device)
173 {
174 	if (refcount_dec_and_test(&device->refcount))
175 		complete(&device->unreg_completion);
176 }
177 EXPORT_SYMBOL(ib_device_put);
178 
179 static struct ib_device *__ib_device_get_by_name(const char *name)
180 {
181 	struct ib_device *device;
182 
183 	list_for_each_entry(device, &device_list, core_list)
184 		if (!strcmp(name, dev_name(&device->dev)))
185 			return device;
186 
187 	return NULL;
188 }
189 
190 int ib_device_rename(struct ib_device *ibdev, const char *name)
191 {
192 	int ret = 0;
193 
194 	if (!strcmp(name, dev_name(&ibdev->dev)))
195 		return ret;
196 
197 	mutex_lock(&device_mutex);
198 	if (__ib_device_get_by_name(name)) {
199 		ret = -EEXIST;
200 		goto out;
201 	}
202 
203 	ret = device_rename(&ibdev->dev, name);
204 	if (ret)
205 		goto out;
206 	strlcpy(ibdev->name, name, IB_DEVICE_NAME_MAX);
207 out:
208 	mutex_unlock(&device_mutex);
209 	return ret;
210 }
211 
212 static int alloc_name(struct ib_device *ibdev, const char *name)
213 {
214 	unsigned long *inuse;
215 	struct ib_device *device;
216 	int i;
217 
218 	inuse = (unsigned long *) get_zeroed_page(GFP_KERNEL);
219 	if (!inuse)
220 		return -ENOMEM;
221 
222 	list_for_each_entry(device, &device_list, core_list) {
223 		char buf[IB_DEVICE_NAME_MAX];
224 
225 		if (sscanf(dev_name(&device->dev), name, &i) != 1)
226 			continue;
227 		if (i < 0 || i >= PAGE_SIZE * 8)
228 			continue;
229 		snprintf(buf, sizeof buf, name, i);
230 		if (!strcmp(buf, dev_name(&device->dev)))
231 			set_bit(i, inuse);
232 	}
233 
234 	i = find_first_zero_bit(inuse, PAGE_SIZE * 8);
235 	free_page((unsigned long) inuse);
236 
237 	return dev_set_name(&ibdev->dev, name, i);
238 }
239 
240 static void ib_device_release(struct device *device)
241 {
242 	struct ib_device *dev = container_of(device, struct ib_device, dev);
243 
244 	WARN_ON(dev->reg_state == IB_DEV_REGISTERED);
245 	if (dev->reg_state == IB_DEV_UNREGISTERED) {
246 		/*
247 		 * In IB_DEV_UNINITIALIZED state, cache or port table
248 		 * is not even created. Free cache and port table only when
249 		 * device reaches UNREGISTERED state.
250 		 */
251 		ib_cache_release_one(dev);
252 		kfree(dev->port_immutable);
253 	}
254 	kfree(dev);
255 }
256 
257 static int ib_device_uevent(struct device *device,
258 			    struct kobj_uevent_env *env)
259 {
260 	if (add_uevent_var(env, "NAME=%s", dev_name(device)))
261 		return -ENOMEM;
262 
263 	/*
264 	 * It would be nice to pass the node GUID with the event...
265 	 */
266 
267 	return 0;
268 }
269 
270 static struct class ib_class = {
271 	.name    = "infiniband",
272 	.dev_release = ib_device_release,
273 	.dev_uevent = ib_device_uevent,
274 };
275 
276 /**
277  * _ib_alloc_device - allocate an IB device struct
278  * @size:size of structure to allocate
279  *
280  * Low-level drivers should use ib_alloc_device() to allocate &struct
281  * ib_device.  @size is the size of the structure to be allocated,
282  * including any private data used by the low-level driver.
283  * ib_dealloc_device() must be used to free structures allocated with
284  * ib_alloc_device().
285  */
286 struct ib_device *_ib_alloc_device(size_t size)
287 {
288 	struct ib_device *device;
289 
290 	if (WARN_ON(size < sizeof(struct ib_device)))
291 		return NULL;
292 
293 	device = kzalloc(size, GFP_KERNEL);
294 	if (!device)
295 		return NULL;
296 
297 	rdma_restrack_init(device);
298 
299 	device->dev.class = &ib_class;
300 	device_initialize(&device->dev);
301 
302 	INIT_LIST_HEAD(&device->event_handler_list);
303 	spin_lock_init(&device->event_handler_lock);
304 	rwlock_init(&device->client_data_lock);
305 	INIT_LIST_HEAD(&device->client_data_list);
306 	INIT_LIST_HEAD(&device->port_list);
307 	init_completion(&device->unreg_completion);
308 
309 	return device;
310 }
311 EXPORT_SYMBOL(_ib_alloc_device);
312 
313 /**
314  * ib_dealloc_device - free an IB device struct
315  * @device:structure to free
316  *
317  * Free a structure allocated with ib_alloc_device().
318  */
319 void ib_dealloc_device(struct ib_device *device)
320 {
321 	WARN_ON(!list_empty(&device->client_data_list));
322 	WARN_ON(device->reg_state != IB_DEV_UNREGISTERED &&
323 		device->reg_state != IB_DEV_UNINITIALIZED);
324 	rdma_restrack_clean(device);
325 	put_device(&device->dev);
326 }
327 EXPORT_SYMBOL(ib_dealloc_device);
328 
329 static int add_client_context(struct ib_device *device, struct ib_client *client)
330 {
331 	struct ib_client_data *context;
332 
333 	if (!device->kverbs_provider && !client->no_kverbs_req)
334 		return -EOPNOTSUPP;
335 
336 	context = kmalloc(sizeof(*context), GFP_KERNEL);
337 	if (!context)
338 		return -ENOMEM;
339 
340 	context->client = client;
341 	context->data   = NULL;
342 	context->going_down = false;
343 
344 	down_write(&lists_rwsem);
345 	write_lock_irq(&device->client_data_lock);
346 	list_add(&context->list, &device->client_data_list);
347 	write_unlock_irq(&device->client_data_lock);
348 	up_write(&lists_rwsem);
349 
350 	return 0;
351 }
352 
353 static int verify_immutable(const struct ib_device *dev, u8 port)
354 {
355 	return WARN_ON(!rdma_cap_ib_mad(dev, port) &&
356 			    rdma_max_mad_size(dev, port) != 0);
357 }
358 
359 static int read_port_immutable(struct ib_device *device)
360 {
361 	int ret;
362 	u8 start_port = rdma_start_port(device);
363 	u8 end_port = rdma_end_port(device);
364 	u8 port;
365 
366 	/**
367 	 * device->port_immutable is indexed directly by the port number to make
368 	 * access to this data as efficient as possible.
369 	 *
370 	 * Therefore port_immutable is declared as a 1 based array with
371 	 * potential empty slots at the beginning.
372 	 */
373 	device->port_immutable = kcalloc(end_port + 1,
374 					 sizeof(*device->port_immutable),
375 					 GFP_KERNEL);
376 	if (!device->port_immutable)
377 		return -ENOMEM;
378 
379 	for (port = start_port; port <= end_port; ++port) {
380 		ret = device->ops.get_port_immutable(
381 			device, port, &device->port_immutable[port]);
382 		if (ret)
383 			return ret;
384 
385 		if (verify_immutable(device, port))
386 			return -EINVAL;
387 	}
388 	return 0;
389 }
390 
391 void ib_get_device_fw_str(struct ib_device *dev, char *str)
392 {
393 	if (dev->ops.get_dev_fw_str)
394 		dev->ops.get_dev_fw_str(dev, str);
395 	else
396 		str[0] = '\0';
397 }
398 EXPORT_SYMBOL(ib_get_device_fw_str);
399 
400 static int setup_port_pkey_list(struct ib_device *device)
401 {
402 	int i;
403 
404 	/**
405 	 * device->port_pkey_list is indexed directly by the port number,
406 	 * Therefore it is declared as a 1 based array with potential empty
407 	 * slots at the beginning.
408 	 */
409 	device->port_pkey_list = kcalloc(rdma_end_port(device) + 1,
410 					 sizeof(*device->port_pkey_list),
411 					 GFP_KERNEL);
412 
413 	if (!device->port_pkey_list)
414 		return -ENOMEM;
415 
416 	for (i = 0; i < (rdma_end_port(device) + 1); i++) {
417 		spin_lock_init(&device->port_pkey_list[i].list_lock);
418 		INIT_LIST_HEAD(&device->port_pkey_list[i].pkey_list);
419 	}
420 
421 	return 0;
422 }
423 
424 static void ib_policy_change_task(struct work_struct *work)
425 {
426 	struct ib_device *dev;
427 
428 	down_read(&lists_rwsem);
429 	list_for_each_entry(dev, &device_list, core_list) {
430 		int i;
431 
432 		for (i = rdma_start_port(dev); i <= rdma_end_port(dev); i++) {
433 			u64 sp;
434 			int ret = ib_get_cached_subnet_prefix(dev,
435 							      i,
436 							      &sp);
437 
438 			WARN_ONCE(ret,
439 				  "ib_get_cached_subnet_prefix err: %d, this should never happen here\n",
440 				  ret);
441 			if (!ret)
442 				ib_security_cache_change(dev, i, sp);
443 		}
444 	}
445 	up_read(&lists_rwsem);
446 }
447 
448 static int ib_security_change(struct notifier_block *nb, unsigned long event,
449 			      void *lsm_data)
450 {
451 	if (event != LSM_POLICY_CHANGE)
452 		return NOTIFY_DONE;
453 
454 	schedule_work(&ib_policy_change_work);
455 
456 	return NOTIFY_OK;
457 }
458 
459 /**
460  *	__dev_new_index	-	allocate an device index
461  *
462  *	Returns a suitable unique value for a new device interface
463  *	number.  It assumes that there are less than 2^32-1 ib devices
464  *	will be present in the system.
465  */
466 static u32 __dev_new_index(void)
467 {
468 	/*
469 	 * The device index to allow stable naming.
470 	 * Similar to struct net -> ifindex.
471 	 */
472 	static u32 index;
473 
474 	for (;;) {
475 		if (!(++index))
476 			index = 1;
477 
478 		if (!__ib_device_get_by_index(index))
479 			return index;
480 	}
481 }
482 
483 static void setup_dma_device(struct ib_device *device)
484 {
485 	struct device *parent = device->dev.parent;
486 
487 	WARN_ON_ONCE(device->dma_device);
488 	if (device->dev.dma_ops) {
489 		/*
490 		 * The caller provided custom DMA operations. Copy the
491 		 * DMA-related fields that are used by e.g. dma_alloc_coherent()
492 		 * into device->dev.
493 		 */
494 		device->dma_device = &device->dev;
495 		if (!device->dev.dma_mask) {
496 			if (parent)
497 				device->dev.dma_mask = parent->dma_mask;
498 			else
499 				WARN_ON_ONCE(true);
500 		}
501 		if (!device->dev.coherent_dma_mask) {
502 			if (parent)
503 				device->dev.coherent_dma_mask =
504 					parent->coherent_dma_mask;
505 			else
506 				WARN_ON_ONCE(true);
507 		}
508 	} else {
509 		/*
510 		 * The caller did not provide custom DMA operations. Use the
511 		 * DMA mapping operations of the parent device.
512 		 */
513 		WARN_ON_ONCE(!parent);
514 		device->dma_device = parent;
515 	}
516 }
517 
518 static void cleanup_device(struct ib_device *device)
519 {
520 	ib_cache_cleanup_one(device);
521 	ib_cache_release_one(device);
522 	kfree(device->port_pkey_list);
523 	kfree(device->port_immutable);
524 }
525 
526 static int setup_device(struct ib_device *device)
527 {
528 	struct ib_udata uhw = {.outlen = 0, .inlen = 0};
529 	int ret;
530 
531 	ret = ib_device_check_mandatory(device);
532 	if (ret)
533 		return ret;
534 
535 	ret = read_port_immutable(device);
536 	if (ret) {
537 		dev_warn(&device->dev,
538 			 "Couldn't create per port immutable data\n");
539 		return ret;
540 	}
541 
542 	memset(&device->attrs, 0, sizeof(device->attrs));
543 	ret = device->ops.query_device(device, &device->attrs, &uhw);
544 	if (ret) {
545 		dev_warn(&device->dev,
546 			 "Couldn't query the device attributes\n");
547 		goto port_cleanup;
548 	}
549 
550 	ret = setup_port_pkey_list(device);
551 	if (ret) {
552 		dev_warn(&device->dev, "Couldn't create per port_pkey_list\n");
553 		goto port_cleanup;
554 	}
555 
556 	ret = ib_cache_setup_one(device);
557 	if (ret) {
558 		dev_warn(&device->dev,
559 			 "Couldn't set up InfiniBand P_Key/GID cache\n");
560 		goto pkey_cleanup;
561 	}
562 	return 0;
563 
564 pkey_cleanup:
565 	kfree(device->port_pkey_list);
566 port_cleanup:
567 	kfree(device->port_immutable);
568 	return ret;
569 }
570 
571 /**
572  * ib_register_device - Register an IB device with IB core
573  * @device:Device to register
574  *
575  * Low-level drivers use ib_register_device() to register their
576  * devices with the IB core.  All registered clients will receive a
577  * callback for each device that is added. @device must be allocated
578  * with ib_alloc_device().
579  */
580 int ib_register_device(struct ib_device *device, const char *name)
581 {
582 	int ret;
583 	struct ib_client *client;
584 
585 	setup_dma_device(device);
586 
587 	mutex_lock(&device_mutex);
588 
589 	if (strchr(name, '%')) {
590 		ret = alloc_name(device, name);
591 		if (ret)
592 			goto out;
593 	} else {
594 		ret = dev_set_name(&device->dev, name);
595 		if (ret)
596 			goto out;
597 	}
598 	if (__ib_device_get_by_name(dev_name(&device->dev))) {
599 		ret = -ENFILE;
600 		goto out;
601 	}
602 	strlcpy(device->name, dev_name(&device->dev), IB_DEVICE_NAME_MAX);
603 
604 	ret = setup_device(device);
605 	if (ret)
606 		goto out;
607 
608 	device->index = __dev_new_index();
609 
610 	ib_device_register_rdmacg(device);
611 
612 	ret = ib_device_register_sysfs(device);
613 	if (ret) {
614 		dev_warn(&device->dev,
615 			 "Couldn't register device with driver model\n");
616 		goto cg_cleanup;
617 	}
618 
619 	refcount_set(&device->refcount, 1);
620 	device->reg_state = IB_DEV_REGISTERED;
621 
622 	list_for_each_entry(client, &client_list, list)
623 		if (!add_client_context(device, client) && client->add)
624 			client->add(device);
625 
626 	down_write(&lists_rwsem);
627 	list_add_tail(&device->core_list, &device_list);
628 	up_write(&lists_rwsem);
629 	mutex_unlock(&device_mutex);
630 	return 0;
631 
632 cg_cleanup:
633 	ib_device_unregister_rdmacg(device);
634 	cleanup_device(device);
635 out:
636 	mutex_unlock(&device_mutex);
637 	return ret;
638 }
639 EXPORT_SYMBOL(ib_register_device);
640 
641 /**
642  * ib_unregister_device - Unregister an IB device
643  * @device:Device to unregister
644  *
645  * Unregister an IB device.  All clients will receive a remove callback.
646  */
647 void ib_unregister_device(struct ib_device *device)
648 {
649 	struct ib_client_data *context, *tmp;
650 	unsigned long flags;
651 
652 	/*
653 	 * Wait for all netlink command callers to finish working on the
654 	 * device.
655 	 */
656 	ib_device_put(device);
657 	wait_for_completion(&device->unreg_completion);
658 
659 	mutex_lock(&device_mutex);
660 
661 	down_write(&lists_rwsem);
662 	list_del(&device->core_list);
663 	write_lock_irq(&device->client_data_lock);
664 	list_for_each_entry(context, &device->client_data_list, list)
665 		context->going_down = true;
666 	write_unlock_irq(&device->client_data_lock);
667 	downgrade_write(&lists_rwsem);
668 
669 	list_for_each_entry(context, &device->client_data_list, list) {
670 		if (context->client->remove)
671 			context->client->remove(device, context->data);
672 	}
673 	up_read(&lists_rwsem);
674 
675 	ib_device_unregister_sysfs(device);
676 	ib_device_unregister_rdmacg(device);
677 
678 	mutex_unlock(&device_mutex);
679 
680 	ib_cache_cleanup_one(device);
681 
682 	ib_security_destroy_port_pkey_list(device);
683 	kfree(device->port_pkey_list);
684 
685 	down_write(&lists_rwsem);
686 	write_lock_irqsave(&device->client_data_lock, flags);
687 	list_for_each_entry_safe(context, tmp, &device->client_data_list,
688 				 list) {
689 		list_del(&context->list);
690 		kfree(context);
691 	}
692 	write_unlock_irqrestore(&device->client_data_lock, flags);
693 	up_write(&lists_rwsem);
694 
695 	device->reg_state = IB_DEV_UNREGISTERED;
696 }
697 EXPORT_SYMBOL(ib_unregister_device);
698 
699 /**
700  * ib_register_client - Register an IB client
701  * @client:Client to register
702  *
703  * Upper level users of the IB drivers can use ib_register_client() to
704  * register callbacks for IB device addition and removal.  When an IB
705  * device is added, each registered client's add method will be called
706  * (in the order the clients were registered), and when a device is
707  * removed, each client's remove method will be called (in the reverse
708  * order that clients were registered).  In addition, when
709  * ib_register_client() is called, the client will receive an add
710  * callback for all devices already registered.
711  */
712 int ib_register_client(struct ib_client *client)
713 {
714 	struct ib_device *device;
715 
716 	mutex_lock(&device_mutex);
717 
718 	list_for_each_entry(device, &device_list, core_list)
719 		if (!add_client_context(device, client) && client->add)
720 			client->add(device);
721 
722 	down_write(&lists_rwsem);
723 	list_add_tail(&client->list, &client_list);
724 	up_write(&lists_rwsem);
725 
726 	mutex_unlock(&device_mutex);
727 
728 	return 0;
729 }
730 EXPORT_SYMBOL(ib_register_client);
731 
732 /**
733  * ib_unregister_client - Unregister an IB client
734  * @client:Client to unregister
735  *
736  * Upper level users use ib_unregister_client() to remove their client
737  * registration.  When ib_unregister_client() is called, the client
738  * will receive a remove callback for each IB device still registered.
739  */
740 void ib_unregister_client(struct ib_client *client)
741 {
742 	struct ib_client_data *context;
743 	struct ib_device *device;
744 
745 	mutex_lock(&device_mutex);
746 
747 	down_write(&lists_rwsem);
748 	list_del(&client->list);
749 	up_write(&lists_rwsem);
750 
751 	list_for_each_entry(device, &device_list, core_list) {
752 		struct ib_client_data *found_context = NULL;
753 
754 		down_write(&lists_rwsem);
755 		write_lock_irq(&device->client_data_lock);
756 		list_for_each_entry(context, &device->client_data_list, list)
757 			if (context->client == client) {
758 				context->going_down = true;
759 				found_context = context;
760 				break;
761 			}
762 		write_unlock_irq(&device->client_data_lock);
763 		up_write(&lists_rwsem);
764 
765 		if (client->remove)
766 			client->remove(device, found_context ?
767 					       found_context->data : NULL);
768 
769 		if (!found_context) {
770 			dev_warn(&device->dev,
771 				 "No client context found for %s\n",
772 				 client->name);
773 			continue;
774 		}
775 
776 		down_write(&lists_rwsem);
777 		write_lock_irq(&device->client_data_lock);
778 		list_del(&found_context->list);
779 		write_unlock_irq(&device->client_data_lock);
780 		up_write(&lists_rwsem);
781 		kfree(found_context);
782 	}
783 
784 	mutex_unlock(&device_mutex);
785 }
786 EXPORT_SYMBOL(ib_unregister_client);
787 
788 /**
789  * ib_get_client_data - Get IB client context
790  * @device:Device to get context for
791  * @client:Client to get context for
792  *
793  * ib_get_client_data() returns client context set with
794  * ib_set_client_data().
795  */
796 void *ib_get_client_data(struct ib_device *device, struct ib_client *client)
797 {
798 	struct ib_client_data *context;
799 	void *ret = NULL;
800 	unsigned long flags;
801 
802 	read_lock_irqsave(&device->client_data_lock, flags);
803 	list_for_each_entry(context, &device->client_data_list, list)
804 		if (context->client == client) {
805 			ret = context->data;
806 			break;
807 		}
808 	read_unlock_irqrestore(&device->client_data_lock, flags);
809 
810 	return ret;
811 }
812 EXPORT_SYMBOL(ib_get_client_data);
813 
814 /**
815  * ib_set_client_data - Set IB client context
816  * @device:Device to set context for
817  * @client:Client to set context for
818  * @data:Context to set
819  *
820  * ib_set_client_data() sets client context that can be retrieved with
821  * ib_get_client_data().
822  */
823 void ib_set_client_data(struct ib_device *device, struct ib_client *client,
824 			void *data)
825 {
826 	struct ib_client_data *context;
827 	unsigned long flags;
828 
829 	write_lock_irqsave(&device->client_data_lock, flags);
830 	list_for_each_entry(context, &device->client_data_list, list)
831 		if (context->client == client) {
832 			context->data = data;
833 			goto out;
834 		}
835 
836 	dev_warn(&device->dev, "No client context found for %s\n",
837 		 client->name);
838 
839 out:
840 	write_unlock_irqrestore(&device->client_data_lock, flags);
841 }
842 EXPORT_SYMBOL(ib_set_client_data);
843 
844 /**
845  * ib_register_event_handler - Register an IB event handler
846  * @event_handler:Handler to register
847  *
848  * ib_register_event_handler() registers an event handler that will be
849  * called back when asynchronous IB events occur (as defined in
850  * chapter 11 of the InfiniBand Architecture Specification).  This
851  * callback may occur in interrupt context.
852  */
853 void ib_register_event_handler(struct ib_event_handler *event_handler)
854 {
855 	unsigned long flags;
856 
857 	spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
858 	list_add_tail(&event_handler->list,
859 		      &event_handler->device->event_handler_list);
860 	spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
861 }
862 EXPORT_SYMBOL(ib_register_event_handler);
863 
864 /**
865  * ib_unregister_event_handler - Unregister an event handler
866  * @event_handler:Handler to unregister
867  *
868  * Unregister an event handler registered with
869  * ib_register_event_handler().
870  */
871 void ib_unregister_event_handler(struct ib_event_handler *event_handler)
872 {
873 	unsigned long flags;
874 
875 	spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
876 	list_del(&event_handler->list);
877 	spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
878 }
879 EXPORT_SYMBOL(ib_unregister_event_handler);
880 
881 /**
882  * ib_dispatch_event - Dispatch an asynchronous event
883  * @event:Event to dispatch
884  *
885  * Low-level drivers must call ib_dispatch_event() to dispatch the
886  * event to all registered event handlers when an asynchronous event
887  * occurs.
888  */
889 void ib_dispatch_event(struct ib_event *event)
890 {
891 	unsigned long flags;
892 	struct ib_event_handler *handler;
893 
894 	spin_lock_irqsave(&event->device->event_handler_lock, flags);
895 
896 	list_for_each_entry(handler, &event->device->event_handler_list, list)
897 		handler->handler(handler, event);
898 
899 	spin_unlock_irqrestore(&event->device->event_handler_lock, flags);
900 }
901 EXPORT_SYMBOL(ib_dispatch_event);
902 
903 /**
904  * ib_query_port - Query IB port attributes
905  * @device:Device to query
906  * @port_num:Port number to query
907  * @port_attr:Port attributes
908  *
909  * ib_query_port() returns the attributes of a port through the
910  * @port_attr pointer.
911  */
912 int ib_query_port(struct ib_device *device,
913 		  u8 port_num,
914 		  struct ib_port_attr *port_attr)
915 {
916 	union ib_gid gid;
917 	int err;
918 
919 	if (!rdma_is_port_valid(device, port_num))
920 		return -EINVAL;
921 
922 	memset(port_attr, 0, sizeof(*port_attr));
923 	err = device->ops.query_port(device, port_num, port_attr);
924 	if (err || port_attr->subnet_prefix)
925 		return err;
926 
927 	if (rdma_port_get_link_layer(device, port_num) != IB_LINK_LAYER_INFINIBAND)
928 		return 0;
929 
930 	err = device->ops.query_gid(device, port_num, 0, &gid);
931 	if (err)
932 		return err;
933 
934 	port_attr->subnet_prefix = be64_to_cpu(gid.global.subnet_prefix);
935 	return 0;
936 }
937 EXPORT_SYMBOL(ib_query_port);
938 
939 /**
940  * ib_enum_roce_netdev - enumerate all RoCE ports
941  * @ib_dev : IB device we want to query
942  * @filter: Should we call the callback?
943  * @filter_cookie: Cookie passed to filter
944  * @cb: Callback to call for each found RoCE ports
945  * @cookie: Cookie passed back to the callback
946  *
947  * Enumerates all of the physical RoCE ports of ib_dev
948  * which are related to netdevice and calls callback() on each
949  * device for which filter() function returns non zero.
950  */
951 void ib_enum_roce_netdev(struct ib_device *ib_dev,
952 			 roce_netdev_filter filter,
953 			 void *filter_cookie,
954 			 roce_netdev_callback cb,
955 			 void *cookie)
956 {
957 	u8 port;
958 
959 	for (port = rdma_start_port(ib_dev); port <= rdma_end_port(ib_dev);
960 	     port++)
961 		if (rdma_protocol_roce(ib_dev, port)) {
962 			struct net_device *idev = NULL;
963 
964 			if (ib_dev->ops.get_netdev)
965 				idev = ib_dev->ops.get_netdev(ib_dev, port);
966 
967 			if (idev &&
968 			    idev->reg_state >= NETREG_UNREGISTERED) {
969 				dev_put(idev);
970 				idev = NULL;
971 			}
972 
973 			if (filter(ib_dev, port, idev, filter_cookie))
974 				cb(ib_dev, port, idev, cookie);
975 
976 			if (idev)
977 				dev_put(idev);
978 		}
979 }
980 
981 /**
982  * ib_enum_all_roce_netdevs - enumerate all RoCE devices
983  * @filter: Should we call the callback?
984  * @filter_cookie: Cookie passed to filter
985  * @cb: Callback to call for each found RoCE ports
986  * @cookie: Cookie passed back to the callback
987  *
988  * Enumerates all RoCE devices' physical ports which are related
989  * to netdevices and calls callback() on each device for which
990  * filter() function returns non zero.
991  */
992 void ib_enum_all_roce_netdevs(roce_netdev_filter filter,
993 			      void *filter_cookie,
994 			      roce_netdev_callback cb,
995 			      void *cookie)
996 {
997 	struct ib_device *dev;
998 
999 	down_read(&lists_rwsem);
1000 	list_for_each_entry(dev, &device_list, core_list)
1001 		ib_enum_roce_netdev(dev, filter, filter_cookie, cb, cookie);
1002 	up_read(&lists_rwsem);
1003 }
1004 
1005 /**
1006  * ib_enum_all_devs - enumerate all ib_devices
1007  * @cb: Callback to call for each found ib_device
1008  *
1009  * Enumerates all ib_devices and calls callback() on each device.
1010  */
1011 int ib_enum_all_devs(nldev_callback nldev_cb, struct sk_buff *skb,
1012 		     struct netlink_callback *cb)
1013 {
1014 	struct ib_device *dev;
1015 	unsigned int idx = 0;
1016 	int ret = 0;
1017 
1018 	down_read(&lists_rwsem);
1019 	list_for_each_entry(dev, &device_list, core_list) {
1020 		ret = nldev_cb(dev, skb, cb, idx);
1021 		if (ret)
1022 			break;
1023 		idx++;
1024 	}
1025 
1026 	up_read(&lists_rwsem);
1027 	return ret;
1028 }
1029 
1030 /**
1031  * ib_query_pkey - Get P_Key table entry
1032  * @device:Device to query
1033  * @port_num:Port number to query
1034  * @index:P_Key table index to query
1035  * @pkey:Returned P_Key
1036  *
1037  * ib_query_pkey() fetches the specified P_Key table entry.
1038  */
1039 int ib_query_pkey(struct ib_device *device,
1040 		  u8 port_num, u16 index, u16 *pkey)
1041 {
1042 	if (!rdma_is_port_valid(device, port_num))
1043 		return -EINVAL;
1044 
1045 	return device->ops.query_pkey(device, port_num, index, pkey);
1046 }
1047 EXPORT_SYMBOL(ib_query_pkey);
1048 
1049 /**
1050  * ib_modify_device - Change IB device attributes
1051  * @device:Device to modify
1052  * @device_modify_mask:Mask of attributes to change
1053  * @device_modify:New attribute values
1054  *
1055  * ib_modify_device() changes a device's attributes as specified by
1056  * the @device_modify_mask and @device_modify structure.
1057  */
1058 int ib_modify_device(struct ib_device *device,
1059 		     int device_modify_mask,
1060 		     struct ib_device_modify *device_modify)
1061 {
1062 	if (!device->ops.modify_device)
1063 		return -ENOSYS;
1064 
1065 	return device->ops.modify_device(device, device_modify_mask,
1066 					 device_modify);
1067 }
1068 EXPORT_SYMBOL(ib_modify_device);
1069 
1070 /**
1071  * ib_modify_port - Modifies the attributes for the specified port.
1072  * @device: The device to modify.
1073  * @port_num: The number of the port to modify.
1074  * @port_modify_mask: Mask used to specify which attributes of the port
1075  *   to change.
1076  * @port_modify: New attribute values for the port.
1077  *
1078  * ib_modify_port() changes a port's attributes as specified by the
1079  * @port_modify_mask and @port_modify structure.
1080  */
1081 int ib_modify_port(struct ib_device *device,
1082 		   u8 port_num, int port_modify_mask,
1083 		   struct ib_port_modify *port_modify)
1084 {
1085 	int rc;
1086 
1087 	if (!rdma_is_port_valid(device, port_num))
1088 		return -EINVAL;
1089 
1090 	if (device->ops.modify_port)
1091 		rc = device->ops.modify_port(device, port_num,
1092 					     port_modify_mask,
1093 					     port_modify);
1094 	else
1095 		rc = rdma_protocol_roce(device, port_num) ? 0 : -ENOSYS;
1096 	return rc;
1097 }
1098 EXPORT_SYMBOL(ib_modify_port);
1099 
1100 /**
1101  * ib_find_gid - Returns the port number and GID table index where
1102  *   a specified GID value occurs. Its searches only for IB link layer.
1103  * @device: The device to query.
1104  * @gid: The GID value to search for.
1105  * @port_num: The port number of the device where the GID value was found.
1106  * @index: The index into the GID table where the GID was found.  This
1107  *   parameter may be NULL.
1108  */
1109 int ib_find_gid(struct ib_device *device, union ib_gid *gid,
1110 		u8 *port_num, u16 *index)
1111 {
1112 	union ib_gid tmp_gid;
1113 	int ret, port, i;
1114 
1115 	for (port = rdma_start_port(device); port <= rdma_end_port(device); ++port) {
1116 		if (!rdma_protocol_ib(device, port))
1117 			continue;
1118 
1119 		for (i = 0; i < device->port_immutable[port].gid_tbl_len; ++i) {
1120 			ret = rdma_query_gid(device, port, i, &tmp_gid);
1121 			if (ret)
1122 				return ret;
1123 			if (!memcmp(&tmp_gid, gid, sizeof *gid)) {
1124 				*port_num = port;
1125 				if (index)
1126 					*index = i;
1127 				return 0;
1128 			}
1129 		}
1130 	}
1131 
1132 	return -ENOENT;
1133 }
1134 EXPORT_SYMBOL(ib_find_gid);
1135 
1136 /**
1137  * ib_find_pkey - Returns the PKey table index where a specified
1138  *   PKey value occurs.
1139  * @device: The device to query.
1140  * @port_num: The port number of the device to search for the PKey.
1141  * @pkey: The PKey value to search for.
1142  * @index: The index into the PKey table where the PKey was found.
1143  */
1144 int ib_find_pkey(struct ib_device *device,
1145 		 u8 port_num, u16 pkey, u16 *index)
1146 {
1147 	int ret, i;
1148 	u16 tmp_pkey;
1149 	int partial_ix = -1;
1150 
1151 	for (i = 0; i < device->port_immutable[port_num].pkey_tbl_len; ++i) {
1152 		ret = ib_query_pkey(device, port_num, i, &tmp_pkey);
1153 		if (ret)
1154 			return ret;
1155 		if ((pkey & 0x7fff) == (tmp_pkey & 0x7fff)) {
1156 			/* if there is full-member pkey take it.*/
1157 			if (tmp_pkey & 0x8000) {
1158 				*index = i;
1159 				return 0;
1160 			}
1161 			if (partial_ix < 0)
1162 				partial_ix = i;
1163 		}
1164 	}
1165 
1166 	/*no full-member, if exists take the limited*/
1167 	if (partial_ix >= 0) {
1168 		*index = partial_ix;
1169 		return 0;
1170 	}
1171 	return -ENOENT;
1172 }
1173 EXPORT_SYMBOL(ib_find_pkey);
1174 
1175 /**
1176  * ib_get_net_dev_by_params() - Return the appropriate net_dev
1177  * for a received CM request
1178  * @dev:	An RDMA device on which the request has been received.
1179  * @port:	Port number on the RDMA device.
1180  * @pkey:	The Pkey the request came on.
1181  * @gid:	A GID that the net_dev uses to communicate.
1182  * @addr:	Contains the IP address that the request specified as its
1183  *		destination.
1184  */
1185 struct net_device *ib_get_net_dev_by_params(struct ib_device *dev,
1186 					    u8 port,
1187 					    u16 pkey,
1188 					    const union ib_gid *gid,
1189 					    const struct sockaddr *addr)
1190 {
1191 	struct net_device *net_dev = NULL;
1192 	struct ib_client_data *context;
1193 
1194 	if (!rdma_protocol_ib(dev, port))
1195 		return NULL;
1196 
1197 	down_read(&lists_rwsem);
1198 
1199 	list_for_each_entry(context, &dev->client_data_list, list) {
1200 		struct ib_client *client = context->client;
1201 
1202 		if (context->going_down)
1203 			continue;
1204 
1205 		if (client->get_net_dev_by_params) {
1206 			net_dev = client->get_net_dev_by_params(dev, port, pkey,
1207 								gid, addr,
1208 								context->data);
1209 			if (net_dev)
1210 				break;
1211 		}
1212 	}
1213 
1214 	up_read(&lists_rwsem);
1215 
1216 	return net_dev;
1217 }
1218 EXPORT_SYMBOL(ib_get_net_dev_by_params);
1219 
1220 void ib_set_device_ops(struct ib_device *dev, const struct ib_device_ops *ops)
1221 {
1222 	struct ib_device_ops *dev_ops = &dev->ops;
1223 #define SET_DEVICE_OP(ptr, name)                                               \
1224 	do {                                                                   \
1225 		if (ops->name)                                                 \
1226 			if (!((ptr)->name))				       \
1227 				(ptr)->name = ops->name;                       \
1228 	} while (0)
1229 
1230 	SET_DEVICE_OP(dev_ops, add_gid);
1231 	SET_DEVICE_OP(dev_ops, advise_mr);
1232 	SET_DEVICE_OP(dev_ops, alloc_dm);
1233 	SET_DEVICE_OP(dev_ops, alloc_fmr);
1234 	SET_DEVICE_OP(dev_ops, alloc_hw_stats);
1235 	SET_DEVICE_OP(dev_ops, alloc_mr);
1236 	SET_DEVICE_OP(dev_ops, alloc_mw);
1237 	SET_DEVICE_OP(dev_ops, alloc_pd);
1238 	SET_DEVICE_OP(dev_ops, alloc_rdma_netdev);
1239 	SET_DEVICE_OP(dev_ops, alloc_ucontext);
1240 	SET_DEVICE_OP(dev_ops, alloc_xrcd);
1241 	SET_DEVICE_OP(dev_ops, attach_mcast);
1242 	SET_DEVICE_OP(dev_ops, check_mr_status);
1243 	SET_DEVICE_OP(dev_ops, create_ah);
1244 	SET_DEVICE_OP(dev_ops, create_counters);
1245 	SET_DEVICE_OP(dev_ops, create_cq);
1246 	SET_DEVICE_OP(dev_ops, create_flow);
1247 	SET_DEVICE_OP(dev_ops, create_flow_action_esp);
1248 	SET_DEVICE_OP(dev_ops, create_qp);
1249 	SET_DEVICE_OP(dev_ops, create_rwq_ind_table);
1250 	SET_DEVICE_OP(dev_ops, create_srq);
1251 	SET_DEVICE_OP(dev_ops, create_wq);
1252 	SET_DEVICE_OP(dev_ops, dealloc_dm);
1253 	SET_DEVICE_OP(dev_ops, dealloc_fmr);
1254 	SET_DEVICE_OP(dev_ops, dealloc_mw);
1255 	SET_DEVICE_OP(dev_ops, dealloc_pd);
1256 	SET_DEVICE_OP(dev_ops, dealloc_ucontext);
1257 	SET_DEVICE_OP(dev_ops, dealloc_xrcd);
1258 	SET_DEVICE_OP(dev_ops, del_gid);
1259 	SET_DEVICE_OP(dev_ops, dereg_mr);
1260 	SET_DEVICE_OP(dev_ops, destroy_ah);
1261 	SET_DEVICE_OP(dev_ops, destroy_counters);
1262 	SET_DEVICE_OP(dev_ops, destroy_cq);
1263 	SET_DEVICE_OP(dev_ops, destroy_flow);
1264 	SET_DEVICE_OP(dev_ops, destroy_flow_action);
1265 	SET_DEVICE_OP(dev_ops, destroy_qp);
1266 	SET_DEVICE_OP(dev_ops, destroy_rwq_ind_table);
1267 	SET_DEVICE_OP(dev_ops, destroy_srq);
1268 	SET_DEVICE_OP(dev_ops, destroy_wq);
1269 	SET_DEVICE_OP(dev_ops, detach_mcast);
1270 	SET_DEVICE_OP(dev_ops, disassociate_ucontext);
1271 	SET_DEVICE_OP(dev_ops, drain_rq);
1272 	SET_DEVICE_OP(dev_ops, drain_sq);
1273 	SET_DEVICE_OP(dev_ops, fill_res_entry);
1274 	SET_DEVICE_OP(dev_ops, get_dev_fw_str);
1275 	SET_DEVICE_OP(dev_ops, get_dma_mr);
1276 	SET_DEVICE_OP(dev_ops, get_hw_stats);
1277 	SET_DEVICE_OP(dev_ops, get_link_layer);
1278 	SET_DEVICE_OP(dev_ops, get_netdev);
1279 	SET_DEVICE_OP(dev_ops, get_port_immutable);
1280 	SET_DEVICE_OP(dev_ops, get_vector_affinity);
1281 	SET_DEVICE_OP(dev_ops, get_vf_config);
1282 	SET_DEVICE_OP(dev_ops, get_vf_stats);
1283 	SET_DEVICE_OP(dev_ops, init_port);
1284 	SET_DEVICE_OP(dev_ops, map_mr_sg);
1285 	SET_DEVICE_OP(dev_ops, map_phys_fmr);
1286 	SET_DEVICE_OP(dev_ops, mmap);
1287 	SET_DEVICE_OP(dev_ops, modify_ah);
1288 	SET_DEVICE_OP(dev_ops, modify_cq);
1289 	SET_DEVICE_OP(dev_ops, modify_device);
1290 	SET_DEVICE_OP(dev_ops, modify_flow_action_esp);
1291 	SET_DEVICE_OP(dev_ops, modify_port);
1292 	SET_DEVICE_OP(dev_ops, modify_qp);
1293 	SET_DEVICE_OP(dev_ops, modify_srq);
1294 	SET_DEVICE_OP(dev_ops, modify_wq);
1295 	SET_DEVICE_OP(dev_ops, peek_cq);
1296 	SET_DEVICE_OP(dev_ops, poll_cq);
1297 	SET_DEVICE_OP(dev_ops, post_recv);
1298 	SET_DEVICE_OP(dev_ops, post_send);
1299 	SET_DEVICE_OP(dev_ops, post_srq_recv);
1300 	SET_DEVICE_OP(dev_ops, process_mad);
1301 	SET_DEVICE_OP(dev_ops, query_ah);
1302 	SET_DEVICE_OP(dev_ops, query_device);
1303 	SET_DEVICE_OP(dev_ops, query_gid);
1304 	SET_DEVICE_OP(dev_ops, query_pkey);
1305 	SET_DEVICE_OP(dev_ops, query_port);
1306 	SET_DEVICE_OP(dev_ops, query_qp);
1307 	SET_DEVICE_OP(dev_ops, query_srq);
1308 	SET_DEVICE_OP(dev_ops, rdma_netdev_get_params);
1309 	SET_DEVICE_OP(dev_ops, read_counters);
1310 	SET_DEVICE_OP(dev_ops, reg_dm_mr);
1311 	SET_DEVICE_OP(dev_ops, reg_user_mr);
1312 	SET_DEVICE_OP(dev_ops, req_ncomp_notif);
1313 	SET_DEVICE_OP(dev_ops, req_notify_cq);
1314 	SET_DEVICE_OP(dev_ops, rereg_user_mr);
1315 	SET_DEVICE_OP(dev_ops, resize_cq);
1316 	SET_DEVICE_OP(dev_ops, set_vf_guid);
1317 	SET_DEVICE_OP(dev_ops, set_vf_link_state);
1318 	SET_DEVICE_OP(dev_ops, unmap_fmr);
1319 }
1320 EXPORT_SYMBOL(ib_set_device_ops);
1321 
1322 static const struct rdma_nl_cbs ibnl_ls_cb_table[RDMA_NL_LS_NUM_OPS] = {
1323 	[RDMA_NL_LS_OP_RESOLVE] = {
1324 		.doit = ib_nl_handle_resolve_resp,
1325 		.flags = RDMA_NL_ADMIN_PERM,
1326 	},
1327 	[RDMA_NL_LS_OP_SET_TIMEOUT] = {
1328 		.doit = ib_nl_handle_set_timeout,
1329 		.flags = RDMA_NL_ADMIN_PERM,
1330 	},
1331 	[RDMA_NL_LS_OP_IP_RESOLVE] = {
1332 		.doit = ib_nl_handle_ip_res_resp,
1333 		.flags = RDMA_NL_ADMIN_PERM,
1334 	},
1335 };
1336 
1337 static int __init ib_core_init(void)
1338 {
1339 	int ret;
1340 
1341 	ib_wq = alloc_workqueue("infiniband", 0, 0);
1342 	if (!ib_wq)
1343 		return -ENOMEM;
1344 
1345 	ib_comp_wq = alloc_workqueue("ib-comp-wq",
1346 			WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
1347 	if (!ib_comp_wq) {
1348 		ret = -ENOMEM;
1349 		goto err;
1350 	}
1351 
1352 	ib_comp_unbound_wq =
1353 		alloc_workqueue("ib-comp-unb-wq",
1354 				WQ_UNBOUND | WQ_HIGHPRI | WQ_MEM_RECLAIM |
1355 				WQ_SYSFS, WQ_UNBOUND_MAX_ACTIVE);
1356 	if (!ib_comp_unbound_wq) {
1357 		ret = -ENOMEM;
1358 		goto err_comp;
1359 	}
1360 
1361 	ret = class_register(&ib_class);
1362 	if (ret) {
1363 		pr_warn("Couldn't create InfiniBand device class\n");
1364 		goto err_comp_unbound;
1365 	}
1366 
1367 	ret = rdma_nl_init();
1368 	if (ret) {
1369 		pr_warn("Couldn't init IB netlink interface: err %d\n", ret);
1370 		goto err_sysfs;
1371 	}
1372 
1373 	ret = addr_init();
1374 	if (ret) {
1375 		pr_warn("Could't init IB address resolution\n");
1376 		goto err_ibnl;
1377 	}
1378 
1379 	ret = ib_mad_init();
1380 	if (ret) {
1381 		pr_warn("Couldn't init IB MAD\n");
1382 		goto err_addr;
1383 	}
1384 
1385 	ret = ib_sa_init();
1386 	if (ret) {
1387 		pr_warn("Couldn't init SA\n");
1388 		goto err_mad;
1389 	}
1390 
1391 	ret = register_lsm_notifier(&ibdev_lsm_nb);
1392 	if (ret) {
1393 		pr_warn("Couldn't register LSM notifier. ret %d\n", ret);
1394 		goto err_sa;
1395 	}
1396 
1397 	nldev_init();
1398 	rdma_nl_register(RDMA_NL_LS, ibnl_ls_cb_table);
1399 	roce_gid_mgmt_init();
1400 
1401 	return 0;
1402 
1403 err_sa:
1404 	ib_sa_cleanup();
1405 err_mad:
1406 	ib_mad_cleanup();
1407 err_addr:
1408 	addr_cleanup();
1409 err_ibnl:
1410 	rdma_nl_exit();
1411 err_sysfs:
1412 	class_unregister(&ib_class);
1413 err_comp_unbound:
1414 	destroy_workqueue(ib_comp_unbound_wq);
1415 err_comp:
1416 	destroy_workqueue(ib_comp_wq);
1417 err:
1418 	destroy_workqueue(ib_wq);
1419 	return ret;
1420 }
1421 
1422 static void __exit ib_core_cleanup(void)
1423 {
1424 	roce_gid_mgmt_cleanup();
1425 	nldev_exit();
1426 	rdma_nl_unregister(RDMA_NL_LS);
1427 	unregister_lsm_notifier(&ibdev_lsm_nb);
1428 	ib_sa_cleanup();
1429 	ib_mad_cleanup();
1430 	addr_cleanup();
1431 	rdma_nl_exit();
1432 	class_unregister(&ib_class);
1433 	destroy_workqueue(ib_comp_unbound_wq);
1434 	destroy_workqueue(ib_comp_wq);
1435 	/* Make sure that any pending umem accounting work is done. */
1436 	destroy_workqueue(ib_wq);
1437 }
1438 
1439 MODULE_ALIAS_RDMA_NETLINK(RDMA_NL_LS, 4);
1440 
1441 subsys_initcall(ib_core_init);
1442 module_exit(ib_core_cleanup);
1443