1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
3  *
4  * Copyright (c) 2004 Topspin Communications.  All rights reserved.
5  * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
6  *
7  * This software is available to you under a choice of one of two
8  * licenses.  You may choose to be licensed under the terms of the GNU
9  * General Public License (GPL) Version 2, available from the file
10  * COPYING in the main directory of this source tree, or the
11  * OpenIB.org BSD license below:
12  *
13  *     Redistribution and use in source and binary forms, with or
14  *     without modification, are permitted provided that the following
15  *     conditions are met:
16  *
17  *      - Redistributions of source code must retain the above
18  *        copyright notice, this list of conditions and the following
19  *        disclaimer.
20  *
21  *      - Redistributions in binary form must reproduce the above
22  *        copyright notice, this list of conditions and the following
23  *        disclaimer in the documentation and/or other materials
24  *        provided with the distribution.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33  * SOFTWARE.
34  *
35  * $FreeBSD$
36  */
37 
38 #include <linux/module.h>
39 #include <linux/string.h>
40 #include <linux/errno.h>
41 #include <linux/kernel.h>
42 #include <linux/slab.h>
43 #include <linux/mutex.h>
44 #include <linux/netdevice.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_wq;
65 EXPORT_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. */
70 static LIST_HEAD(device_list);
71 static 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  */
84 static DEFINE_MUTEX(device_mutex);
85 static DECLARE_RWSEM(lists_rwsem);
86 
87 
88 static int ib_device_check_mandatory(struct ib_device *device)
89 {
90 #define IB_MANDATORY_FUNC(x) { offsetof(struct ib_device, x), #x }
91 	static const struct {
92 		size_t offset;
93 		char  *name;
94 	} mandatory_table[] = {
95 		IB_MANDATORY_FUNC(query_device),
96 		IB_MANDATORY_FUNC(query_port),
97 		IB_MANDATORY_FUNC(query_pkey),
98 		IB_MANDATORY_FUNC(query_gid),
99 		IB_MANDATORY_FUNC(alloc_pd),
100 		IB_MANDATORY_FUNC(dealloc_pd),
101 		IB_MANDATORY_FUNC(create_ah),
102 		IB_MANDATORY_FUNC(destroy_ah),
103 		IB_MANDATORY_FUNC(create_qp),
104 		IB_MANDATORY_FUNC(modify_qp),
105 		IB_MANDATORY_FUNC(destroy_qp),
106 		IB_MANDATORY_FUNC(post_send),
107 		IB_MANDATORY_FUNC(post_recv),
108 		IB_MANDATORY_FUNC(create_cq),
109 		IB_MANDATORY_FUNC(destroy_cq),
110 		IB_MANDATORY_FUNC(poll_cq),
111 		IB_MANDATORY_FUNC(req_notify_cq),
112 		IB_MANDATORY_FUNC(get_dma_mr),
113 		IB_MANDATORY_FUNC(dereg_mr),
114 		IB_MANDATORY_FUNC(get_port_immutable)
115 	};
116 	int i;
117 
118 	for (i = 0; i < ARRAY_SIZE(mandatory_table); ++i) {
119 		if (!*(void **) ((char *) device + mandatory_table[i].offset)) {
120 			pr_warn("Device %s is missing mandatory function %s\n",
121 				device->name, mandatory_table[i].name);
122 			return -EINVAL;
123 		}
124 	}
125 
126 	return 0;
127 }
128 
129 static struct ib_device *__ib_device_get_by_name(const char *name)
130 {
131 	struct ib_device *device;
132 
133 	list_for_each_entry(device, &device_list, core_list)
134 		if (!strncmp(name, device->name, IB_DEVICE_NAME_MAX))
135 			return device;
136 
137 	return NULL;
138 }
139 
140 
141 static int alloc_name(char *name)
142 {
143 	unsigned long *inuse;
144 	char buf[IB_DEVICE_NAME_MAX];
145 	struct ib_device *device;
146 	int i;
147 
148 	inuse = (unsigned long *) get_zeroed_page(GFP_KERNEL);
149 	if (!inuse)
150 		return -ENOMEM;
151 
152 	list_for_each_entry(device, &device_list, core_list) {
153 		if (!sscanf(device->name, name, &i))
154 			continue;
155 		if (i < 0 || i >= PAGE_SIZE * 8)
156 			continue;
157 		snprintf(buf, sizeof buf, name, i);
158 		if (!strncmp(buf, device->name, IB_DEVICE_NAME_MAX))
159 			set_bit(i, inuse);
160 	}
161 
162 	i = find_first_zero_bit(inuse, PAGE_SIZE * 8);
163 	free_page((unsigned long) inuse);
164 	snprintf(buf, sizeof buf, name, i);
165 
166 	if (__ib_device_get_by_name(buf))
167 		return -ENFILE;
168 
169 	strlcpy(name, buf, IB_DEVICE_NAME_MAX);
170 	return 0;
171 }
172 
173 static void ib_device_release(struct device *device)
174 {
175 	struct ib_device *dev = container_of(device, struct ib_device, dev);
176 
177 	ib_cache_release_one(dev);
178 	kfree(dev->port_immutable);
179 	kfree(dev);
180 }
181 
182 static struct class ib_class = {
183 	.name    = "infiniband",
184 	.dev_release = ib_device_release,
185 };
186 
187 /**
188  * ib_alloc_device - allocate an IB device struct
189  * @size:size of structure to allocate
190  *
191  * Low-level drivers should use ib_alloc_device() to allocate &struct
192  * ib_device.  @size is the size of the structure to be allocated,
193  * including any private data used by the low-level driver.
194  * ib_dealloc_device() must be used to free structures allocated with
195  * ib_alloc_device().
196  */
197 struct ib_device *ib_alloc_device(size_t size)
198 {
199 	struct ib_device *device;
200 
201 	if (WARN_ON(size < sizeof(struct ib_device)))
202 		return NULL;
203 
204 	device = kzalloc(size, GFP_KERNEL);
205 	if (!device)
206 		return NULL;
207 
208 	device->dev.parent = &linux_root_device;
209 	device->dev.class = &ib_class;
210 	device_initialize(&device->dev);
211 
212 	dev_set_drvdata(&device->dev, device);
213 
214 	INIT_LIST_HEAD(&device->event_handler_list);
215 	spin_lock_init(&device->event_handler_lock);
216 	spin_lock_init(&device->client_data_lock);
217 	INIT_LIST_HEAD(&device->client_data_list);
218 	INIT_LIST_HEAD(&device->port_list);
219 
220 	return device;
221 }
222 EXPORT_SYMBOL(ib_alloc_device);
223 
224 /**
225  * ib_dealloc_device - free an IB device struct
226  * @device:structure to free
227  *
228  * Free a structure allocated with ib_alloc_device().
229  */
230 void ib_dealloc_device(struct ib_device *device)
231 {
232 	WARN_ON(device->reg_state != IB_DEV_UNREGISTERED &&
233 		device->reg_state != IB_DEV_UNINITIALIZED);
234 	kobject_put(&device->dev.kobj);
235 }
236 EXPORT_SYMBOL(ib_dealloc_device);
237 
238 static int add_client_context(struct ib_device *device, struct ib_client *client)
239 {
240 	struct ib_client_data *context;
241 	unsigned long flags;
242 
243 	context = kmalloc(sizeof *context, GFP_KERNEL);
244 	if (!context) {
245 		pr_warn("Couldn't allocate client context for %s/%s\n",
246 			device->name, client->name);
247 		return -ENOMEM;
248 	}
249 
250 	context->client = client;
251 	context->data   = NULL;
252 	context->going_down = false;
253 
254 	down_write(&lists_rwsem);
255 	spin_lock_irqsave(&device->client_data_lock, flags);
256 	list_add(&context->list, &device->client_data_list);
257 	spin_unlock_irqrestore(&device->client_data_lock, flags);
258 	up_write(&lists_rwsem);
259 
260 	return 0;
261 }
262 
263 static int verify_immutable(const struct ib_device *dev, u8 port)
264 {
265 	return WARN_ON(!rdma_cap_ib_mad(dev, port) &&
266 			    rdma_max_mad_size(dev, port) != 0);
267 }
268 
269 static int read_port_immutable(struct ib_device *device)
270 {
271 	int ret;
272 	u8 start_port = rdma_start_port(device);
273 	u8 end_port = rdma_end_port(device);
274 	u8 port;
275 
276 	/**
277 	 * device->port_immutable is indexed directly by the port number to make
278 	 * access to this data as efficient as possible.
279 	 *
280 	 * Therefore port_immutable is declared as a 1 based array with
281 	 * potential empty slots at the beginning.
282 	 */
283 	device->port_immutable = kzalloc(sizeof(*device->port_immutable)
284 					 * (end_port + 1),
285 					 GFP_KERNEL);
286 	if (!device->port_immutable)
287 		return -ENOMEM;
288 
289 	for (port = start_port; port <= end_port; ++port) {
290 		ret = device->get_port_immutable(device, port,
291 						 &device->port_immutable[port]);
292 		if (ret)
293 			return ret;
294 
295 		if (verify_immutable(device, port))
296 			return -EINVAL;
297 	}
298 	return 0;
299 }
300 
301 void ib_get_device_fw_str(struct ib_device *dev, char *str, size_t str_len)
302 {
303 	if (dev->get_dev_fw_str)
304 		dev->get_dev_fw_str(dev, str, str_len);
305 	else
306 		str[0] = '\0';
307 }
308 EXPORT_SYMBOL(ib_get_device_fw_str);
309 
310 /**
311  * ib_register_device - Register an IB device with IB core
312  * @device:Device to register
313  *
314  * Low-level drivers use ib_register_device() to register their
315  * devices with the IB core.  All registered clients will receive a
316  * callback for each device that is added. @device must be allocated
317  * with ib_alloc_device().
318  */
319 int ib_register_device(struct ib_device *device,
320 		       int (*port_callback)(struct ib_device *,
321 					    u8, struct kobject *))
322 {
323 	int ret;
324 	struct ib_client *client;
325 	struct ib_udata uhw = {.outlen = 0, .inlen = 0};
326 
327 	mutex_lock(&device_mutex);
328 
329 	if (strchr(device->name, '%')) {
330 		ret = alloc_name(device->name);
331 		if (ret)
332 			goto out;
333 	}
334 
335 	if (ib_device_check_mandatory(device)) {
336 		ret = -EINVAL;
337 		goto out;
338 	}
339 
340 	ret = read_port_immutable(device);
341 	if (ret) {
342 		pr_warn("Couldn't create per port immutable data %s\n",
343 			device->name);
344 		goto out;
345 	}
346 
347 	ret = ib_cache_setup_one(device);
348 	if (ret) {
349 		pr_warn("Couldn't set up InfiniBand P_Key/GID cache\n");
350 		goto out;
351 	}
352 
353 	memset(&device->attrs, 0, sizeof(device->attrs));
354 	ret = device->query_device(device, &device->attrs, &uhw);
355 	if (ret) {
356 		pr_warn("Couldn't query the device attributes\n");
357 		ib_cache_cleanup_one(device);
358 		goto out;
359 	}
360 
361 	ret = ib_device_register_sysfs(device, port_callback);
362 	if (ret) {
363 		pr_warn("Couldn't register device %s with driver model\n",
364 			device->name);
365 		ib_cache_cleanup_one(device);
366 		goto out;
367 	}
368 
369 	device->reg_state = IB_DEV_REGISTERED;
370 
371 	list_for_each_entry(client, &client_list, list)
372 		if (client->add && !add_client_context(device, client))
373 			client->add(device);
374 
375 	down_write(&lists_rwsem);
376 	list_add_tail(&device->core_list, &device_list);
377 	up_write(&lists_rwsem);
378 out:
379 	mutex_unlock(&device_mutex);
380 	return ret;
381 }
382 EXPORT_SYMBOL(ib_register_device);
383 
384 /**
385  * ib_unregister_device - Unregister an IB device
386  * @device:Device to unregister
387  *
388  * Unregister an IB device.  All clients will receive a remove callback.
389  */
390 void ib_unregister_device(struct ib_device *device)
391 {
392 	struct ib_client_data *context, *tmp;
393 	unsigned long flags;
394 
395 	mutex_lock(&device_mutex);
396 
397 	down_write(&lists_rwsem);
398 	list_del(&device->core_list);
399 	spin_lock_irqsave(&device->client_data_lock, flags);
400 	list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
401 		context->going_down = true;
402 	spin_unlock_irqrestore(&device->client_data_lock, flags);
403 	downgrade_write(&lists_rwsem);
404 
405 	list_for_each_entry_safe(context, tmp, &device->client_data_list,
406 				 list) {
407 		if (context->client->remove)
408 			context->client->remove(device, context->data);
409 	}
410 	up_read(&lists_rwsem);
411 
412 	mutex_unlock(&device_mutex);
413 
414 	ib_device_unregister_sysfs(device);
415 	ib_cache_cleanup_one(device);
416 
417 	down_write(&lists_rwsem);
418 	spin_lock_irqsave(&device->client_data_lock, flags);
419 	list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
420 		kfree(context);
421 	spin_unlock_irqrestore(&device->client_data_lock, flags);
422 	up_write(&lists_rwsem);
423 
424 	device->reg_state = IB_DEV_UNREGISTERED;
425 }
426 EXPORT_SYMBOL(ib_unregister_device);
427 
428 /**
429  * ib_register_client - Register an IB client
430  * @client:Client to register
431  *
432  * Upper level users of the IB drivers can use ib_register_client() to
433  * register callbacks for IB device addition and removal.  When an IB
434  * device is added, each registered client's add method will be called
435  * (in the order the clients were registered), and when a device is
436  * removed, each client's remove method will be called (in the reverse
437  * order that clients were registered).  In addition, when
438  * ib_register_client() is called, the client will receive an add
439  * callback for all devices already registered.
440  */
441 int ib_register_client(struct ib_client *client)
442 {
443 	struct ib_device *device;
444 
445 	mutex_lock(&device_mutex);
446 
447 	list_for_each_entry(device, &device_list, core_list)
448 		if (client->add && !add_client_context(device, client))
449 			client->add(device);
450 
451 	down_write(&lists_rwsem);
452 	list_add_tail(&client->list, &client_list);
453 	up_write(&lists_rwsem);
454 
455 	mutex_unlock(&device_mutex);
456 
457 	return 0;
458 }
459 EXPORT_SYMBOL(ib_register_client);
460 
461 /**
462  * ib_unregister_client - Unregister an IB client
463  * @client:Client to unregister
464  *
465  * Upper level users use ib_unregister_client() to remove their client
466  * registration.  When ib_unregister_client() is called, the client
467  * will receive a remove callback for each IB device still registered.
468  */
469 void ib_unregister_client(struct ib_client *client)
470 {
471 	struct ib_client_data *context, *tmp;
472 	struct ib_device *device;
473 	unsigned long flags;
474 
475 	mutex_lock(&device_mutex);
476 
477 	down_write(&lists_rwsem);
478 	list_del(&client->list);
479 	up_write(&lists_rwsem);
480 
481 	list_for_each_entry(device, &device_list, core_list) {
482 		struct ib_client_data *found_context = NULL;
483 
484 		down_write(&lists_rwsem);
485 		spin_lock_irqsave(&device->client_data_lock, flags);
486 		list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
487 			if (context->client == client) {
488 				context->going_down = true;
489 				found_context = context;
490 				break;
491 			}
492 		spin_unlock_irqrestore(&device->client_data_lock, flags);
493 		up_write(&lists_rwsem);
494 
495 		if (client->remove)
496 			client->remove(device, found_context ?
497 					       found_context->data : NULL);
498 
499 		if (!found_context) {
500 			pr_warn("No client context found for %s/%s\n",
501 				device->name, client->name);
502 			continue;
503 		}
504 
505 		down_write(&lists_rwsem);
506 		spin_lock_irqsave(&device->client_data_lock, flags);
507 		list_del(&found_context->list);
508 		kfree(found_context);
509 		spin_unlock_irqrestore(&device->client_data_lock, flags);
510 		up_write(&lists_rwsem);
511 	}
512 
513 	mutex_unlock(&device_mutex);
514 }
515 EXPORT_SYMBOL(ib_unregister_client);
516 
517 /**
518  * ib_get_client_data - Get IB client context
519  * @device:Device to get context for
520  * @client:Client to get context for
521  *
522  * ib_get_client_data() returns client context set with
523  * ib_set_client_data().
524  */
525 void *ib_get_client_data(struct ib_device *device, struct ib_client *client)
526 {
527 	struct ib_client_data *context;
528 	void *ret = NULL;
529 	unsigned long flags;
530 
531 	spin_lock_irqsave(&device->client_data_lock, flags);
532 	list_for_each_entry(context, &device->client_data_list, list)
533 		if (context->client == client) {
534 			ret = context->data;
535 			break;
536 		}
537 	spin_unlock_irqrestore(&device->client_data_lock, flags);
538 
539 	return ret;
540 }
541 EXPORT_SYMBOL(ib_get_client_data);
542 
543 /**
544  * ib_set_client_data - Set IB client context
545  * @device:Device to set context for
546  * @client:Client to set context for
547  * @data:Context to set
548  *
549  * ib_set_client_data() sets client context that can be retrieved with
550  * ib_get_client_data().
551  */
552 void ib_set_client_data(struct ib_device *device, struct ib_client *client,
553 			void *data)
554 {
555 	struct ib_client_data *context;
556 	unsigned long flags;
557 
558 	spin_lock_irqsave(&device->client_data_lock, flags);
559 	list_for_each_entry(context, &device->client_data_list, list)
560 		if (context->client == client) {
561 			context->data = data;
562 			goto out;
563 		}
564 
565 	pr_warn("No client context found for %s/%s\n",
566 		device->name, client->name);
567 
568 out:
569 	spin_unlock_irqrestore(&device->client_data_lock, flags);
570 }
571 EXPORT_SYMBOL(ib_set_client_data);
572 
573 /**
574  * ib_register_event_handler - Register an IB event handler
575  * @event_handler:Handler to register
576  *
577  * ib_register_event_handler() registers an event handler that will be
578  * called back when asynchronous IB events occur (as defined in
579  * chapter 11 of the InfiniBand Architecture Specification).  This
580  * callback may occur in interrupt context.
581  */
582 int ib_register_event_handler  (struct ib_event_handler *event_handler)
583 {
584 	unsigned long flags;
585 
586 	spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
587 	list_add_tail(&event_handler->list,
588 		      &event_handler->device->event_handler_list);
589 	spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
590 
591 	return 0;
592 }
593 EXPORT_SYMBOL(ib_register_event_handler);
594 
595 /**
596  * ib_unregister_event_handler - Unregister an event handler
597  * @event_handler:Handler to unregister
598  *
599  * Unregister an event handler registered with
600  * ib_register_event_handler().
601  */
602 int ib_unregister_event_handler(struct ib_event_handler *event_handler)
603 {
604 	unsigned long flags;
605 
606 	spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
607 	list_del(&event_handler->list);
608 	spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
609 
610 	return 0;
611 }
612 EXPORT_SYMBOL(ib_unregister_event_handler);
613 
614 /**
615  * ib_dispatch_event - Dispatch an asynchronous event
616  * @event:Event to dispatch
617  *
618  * Low-level drivers must call ib_dispatch_event() to dispatch the
619  * event to all registered event handlers when an asynchronous event
620  * occurs.
621  */
622 void ib_dispatch_event(struct ib_event *event)
623 {
624 	unsigned long flags;
625 	struct ib_event_handler *handler;
626 
627 	spin_lock_irqsave(&event->device->event_handler_lock, flags);
628 
629 	list_for_each_entry(handler, &event->device->event_handler_list, list)
630 		handler->handler(handler, event);
631 
632 	spin_unlock_irqrestore(&event->device->event_handler_lock, flags);
633 }
634 EXPORT_SYMBOL(ib_dispatch_event);
635 
636 /**
637  * ib_query_port - Query IB port attributes
638  * @device:Device to query
639  * @port_num:Port number to query
640  * @port_attr:Port attributes
641  *
642  * ib_query_port() returns the attributes of a port through the
643  * @port_attr pointer.
644  */
645 int ib_query_port(struct ib_device *device,
646 		  u8 port_num,
647 		  struct ib_port_attr *port_attr)
648 {
649 	union ib_gid gid;
650 	int err;
651 
652 	if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
653 		return -EINVAL;
654 
655 	memset(port_attr, 0, sizeof(*port_attr));
656 	err = device->query_port(device, port_num, port_attr);
657 	if (err || port_attr->subnet_prefix)
658 		return err;
659 
660 	if (rdma_port_get_link_layer(device, port_num) != IB_LINK_LAYER_INFINIBAND)
661 		return 0;
662 
663 	err = ib_query_gid(device, port_num, 0, &gid, NULL);
664 	if (err)
665 		return err;
666 
667 	port_attr->subnet_prefix = be64_to_cpu(gid.global.subnet_prefix);
668 	return 0;
669 }
670 EXPORT_SYMBOL(ib_query_port);
671 
672 /**
673  * ib_query_gid - Get GID table entry
674  * @device:Device to query
675  * @port_num:Port number to query
676  * @index:GID table index to query
677  * @gid:Returned GID
678  * @attr: Returned GID attributes related to this GID index (only in RoCE).
679  *   NULL means ignore.
680  *
681  * ib_query_gid() fetches the specified GID table entry.
682  */
683 int ib_query_gid(struct ib_device *device,
684 		 u8 port_num, int index, union ib_gid *gid,
685 		 struct ib_gid_attr *attr)
686 {
687 	if (rdma_cap_roce_gid_table(device, port_num))
688 		return ib_get_cached_gid(device, port_num, index, gid, attr);
689 
690 	if (attr)
691 		return -EINVAL;
692 
693 	return device->query_gid(device, port_num, index, gid);
694 }
695 EXPORT_SYMBOL(ib_query_gid);
696 
697 /**
698  * ib_enum_roce_netdev - enumerate all RoCE ports
699  * @ib_dev : IB device we want to query
700  * @filter: Should we call the callback?
701  * @filter_cookie: Cookie passed to filter
702  * @cb: Callback to call for each found RoCE ports
703  * @cookie: Cookie passed back to the callback
704  *
705  * Enumerates all of the physical RoCE ports of ib_dev
706  * which are related to netdevice and calls callback() on each
707  * device for which filter() function returns non zero.
708  */
709 void ib_enum_roce_netdev(struct ib_device *ib_dev,
710 			 roce_netdev_filter filter,
711 			 void *filter_cookie,
712 			 roce_netdev_callback cb,
713 			 void *cookie)
714 {
715 	u8 port;
716 
717 	for (port = rdma_start_port(ib_dev); port <= rdma_end_port(ib_dev);
718 	     port++)
719 		if (rdma_protocol_roce(ib_dev, port)) {
720 			struct net_device *idev = NULL;
721 
722 			if (ib_dev->get_netdev)
723 				idev = ib_dev->get_netdev(ib_dev, port);
724 
725 			if (idev && (idev->if_flags & IFF_DYING)) {
726 				dev_put(idev);
727 				idev = NULL;
728 			}
729 
730 			if (filter(ib_dev, port, idev, filter_cookie))
731 				cb(ib_dev, port, idev, cookie);
732 
733 			if (idev)
734 				dev_put(idev);
735 		}
736 }
737 
738 /**
739  * ib_enum_all_roce_netdevs - enumerate all RoCE devices
740  * @filter: Should we call the callback?
741  * @filter_cookie: Cookie passed to filter
742  * @cb: Callback to call for each found RoCE ports
743  * @cookie: Cookie passed back to the callback
744  *
745  * Enumerates all RoCE devices' physical ports which are related
746  * to netdevices and calls callback() on each device for which
747  * filter() function returns non zero.
748  */
749 void ib_enum_all_roce_netdevs(roce_netdev_filter filter,
750 			      void *filter_cookie,
751 			      roce_netdev_callback cb,
752 			      void *cookie)
753 {
754 	struct ib_device *dev;
755 
756 	down_read(&lists_rwsem);
757 	list_for_each_entry(dev, &device_list, core_list)
758 		ib_enum_roce_netdev(dev, filter, filter_cookie, cb, cookie);
759 	up_read(&lists_rwsem);
760 }
761 
762 /**
763  * ib_cache_gid_del_all_by_netdev - delete GIDs belonging a netdevice
764  *
765  * @ndev: Pointer to netdevice
766  */
767 void ib_cache_gid_del_all_by_netdev(struct net_device *ndev)
768 {
769 	struct ib_device *ib_dev;
770 	u8 port;
771 
772 	down_read(&lists_rwsem);
773 	list_for_each_entry(ib_dev, &device_list, core_list) {
774 		for (port = rdma_start_port(ib_dev);
775 		     port <= rdma_end_port(ib_dev);
776 		     port++) {
777 			if (rdma_protocol_roce(ib_dev, port) == 0)
778 				continue;
779 			(void) ib_cache_gid_del_all_netdev_gids(ib_dev, port, ndev);
780 		}
781 	}
782 	up_read(&lists_rwsem);
783 }
784 
785 /**
786  * ib_query_pkey - Get P_Key table entry
787  * @device:Device to query
788  * @port_num:Port number to query
789  * @index:P_Key table index to query
790  * @pkey:Returned P_Key
791  *
792  * ib_query_pkey() fetches the specified P_Key table entry.
793  */
794 int ib_query_pkey(struct ib_device *device,
795 		  u8 port_num, u16 index, u16 *pkey)
796 {
797 	return device->query_pkey(device, port_num, index, pkey);
798 }
799 EXPORT_SYMBOL(ib_query_pkey);
800 
801 /**
802  * ib_modify_device - Change IB device attributes
803  * @device:Device to modify
804  * @device_modify_mask:Mask of attributes to change
805  * @device_modify:New attribute values
806  *
807  * ib_modify_device() changes a device's attributes as specified by
808  * the @device_modify_mask and @device_modify structure.
809  */
810 int ib_modify_device(struct ib_device *device,
811 		     int device_modify_mask,
812 		     struct ib_device_modify *device_modify)
813 {
814 	if (!device->modify_device)
815 		return -ENOSYS;
816 
817 	return device->modify_device(device, device_modify_mask,
818 				     device_modify);
819 }
820 EXPORT_SYMBOL(ib_modify_device);
821 
822 /**
823  * ib_modify_port - Modifies the attributes for the specified port.
824  * @device: The device to modify.
825  * @port_num: The number of the port to modify.
826  * @port_modify_mask: Mask used to specify which attributes of the port
827  *   to change.
828  * @port_modify: New attribute values for the port.
829  *
830  * ib_modify_port() changes a port's attributes as specified by the
831  * @port_modify_mask and @port_modify structure.
832  */
833 int ib_modify_port(struct ib_device *device,
834 		   u8 port_num, int port_modify_mask,
835 		   struct ib_port_modify *port_modify)
836 {
837 	if (!device->modify_port)
838 		return -ENOSYS;
839 
840 	if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
841 		return -EINVAL;
842 
843 	return device->modify_port(device, port_num, port_modify_mask,
844 				   port_modify);
845 }
846 EXPORT_SYMBOL(ib_modify_port);
847 
848 /**
849  * ib_find_gid - Returns the port number and GID table index where
850  *   a specified GID value occurs.
851  * @device: The device to query.
852  * @gid: The GID value to search for.
853  * @gid_type: Type of GID.
854  * @ndev: The ndev related to the GID to search for.
855  * @port_num: The port number of the device where the GID value was found.
856  * @index: The index into the GID table where the GID was found.  This
857  *   parameter may be NULL.
858  */
859 int ib_find_gid(struct ib_device *device, union ib_gid *gid,
860 		enum ib_gid_type gid_type, struct net_device *ndev,
861 		u8 *port_num, u16 *index)
862 {
863 	union ib_gid tmp_gid;
864 	int ret, port, i;
865 
866 	for (port = rdma_start_port(device); port <= rdma_end_port(device); ++port) {
867 		if (rdma_cap_roce_gid_table(device, port)) {
868 			if (!ib_find_cached_gid_by_port(device, gid, gid_type, port,
869 							ndev, index)) {
870 				*port_num = port;
871 				return 0;
872 			}
873 		}
874 
875 		if (gid_type != IB_GID_TYPE_IB)
876 			continue;
877 
878 		for (i = 0; i < device->port_immutable[port].gid_tbl_len; ++i) {
879 			ret = ib_query_gid(device, port, i, &tmp_gid, NULL);
880 			if (ret)
881 				return ret;
882 			if (!memcmp(&tmp_gid, gid, sizeof *gid)) {
883 				*port_num = port;
884 				if (index)
885 					*index = i;
886 				return 0;
887 			}
888 		}
889 	}
890 
891 	return -ENOENT;
892 }
893 EXPORT_SYMBOL(ib_find_gid);
894 
895 /**
896  * ib_find_pkey - Returns the PKey table index where a specified
897  *   PKey value occurs.
898  * @device: The device to query.
899  * @port_num: The port number of the device to search for the PKey.
900  * @pkey: The PKey value to search for.
901  * @index: The index into the PKey table where the PKey was found.
902  */
903 int ib_find_pkey(struct ib_device *device,
904 		 u8 port_num, u16 pkey, u16 *index)
905 {
906 	int ret, i;
907 	u16 tmp_pkey;
908 	int partial_ix = -1;
909 
910 	for (i = 0; i < device->port_immutable[port_num].pkey_tbl_len; ++i) {
911 		ret = ib_query_pkey(device, port_num, i, &tmp_pkey);
912 		if (ret)
913 			return ret;
914 		if ((pkey & 0x7fff) == (tmp_pkey & 0x7fff)) {
915 			/* if there is full-member pkey take it.*/
916 			if (tmp_pkey & 0x8000) {
917 				*index = i;
918 				return 0;
919 			}
920 			if (partial_ix < 0)
921 				partial_ix = i;
922 		}
923 	}
924 
925 	/*no full-member, if exists take the limited*/
926 	if (partial_ix >= 0) {
927 		*index = partial_ix;
928 		return 0;
929 	}
930 	return -ENOENT;
931 }
932 EXPORT_SYMBOL(ib_find_pkey);
933 
934 /**
935  * ib_get_net_dev_by_params() - Return the appropriate net_dev
936  * for a received CM request
937  * @dev:	An RDMA device on which the request has been received.
938  * @port:	Port number on the RDMA device.
939  * @pkey:	The Pkey the request came on.
940  * @gid:	A GID that the net_dev uses to communicate.
941  * @addr:	Contains the IP address that the request specified as its
942  *		destination.
943  */
944 struct net_device *ib_get_net_dev_by_params(struct ib_device *dev,
945 					    u8 port,
946 					    u16 pkey,
947 					    const union ib_gid *gid,
948 					    const struct sockaddr *addr)
949 {
950 	struct net_device *net_dev = NULL;
951 	struct ib_client_data *context;
952 
953 	if (!rdma_protocol_ib(dev, port))
954 		return NULL;
955 
956 	down_read(&lists_rwsem);
957 
958 	list_for_each_entry(context, &dev->client_data_list, list) {
959 		struct ib_client *client = context->client;
960 
961 		if (context->going_down)
962 			continue;
963 
964 		if (client->get_net_dev_by_params) {
965 			net_dev = client->get_net_dev_by_params(dev, port, pkey,
966 								gid, addr,
967 								context->data);
968 			if (net_dev)
969 				break;
970 		}
971 	}
972 
973 	up_read(&lists_rwsem);
974 
975 	return net_dev;
976 }
977 EXPORT_SYMBOL(ib_get_net_dev_by_params);
978 
979 static int __init ib_core_init(void)
980 {
981 	int ret;
982 
983 	ib_wq = alloc_workqueue("infiniband", 0, 0);
984 	if (!ib_wq)
985 		return -ENOMEM;
986 
987 	ib_comp_wq = alloc_workqueue("ib-comp-wq",
988 			WQ_UNBOUND | WQ_HIGHPRI | WQ_MEM_RECLAIM,
989 			mp_ncpus * 4 /* WQ_UNBOUND_MAX_ACTIVE */);
990 	if (!ib_comp_wq) {
991 		ret = -ENOMEM;
992 		goto err;
993 	}
994 
995 	ret = class_register(&ib_class);
996 	if (ret) {
997 		pr_warn("Couldn't create InfiniBand device class\n");
998 		goto err_comp;
999 	}
1000 
1001 	ret = addr_init();
1002 	if (ret) {
1003 		pr_warn("Could't init IB address resolution\n");
1004 		goto err_sysfs;
1005 	}
1006 
1007 	ret = ib_mad_init();
1008 	if (ret) {
1009 		pr_warn("Couldn't init IB MAD\n");
1010 		goto err_addr;
1011 	}
1012 
1013 	ret = ib_sa_init();
1014 	if (ret) {
1015 		pr_warn("Couldn't init SA\n");
1016 		goto err_mad;
1017 	}
1018 
1019 	ib_cache_setup();
1020 
1021 	return 0;
1022 
1023 err_mad:
1024 	ib_mad_cleanup();
1025 err_addr:
1026 	addr_cleanup();
1027 err_sysfs:
1028 	class_unregister(&ib_class);
1029 err_comp:
1030 	destroy_workqueue(ib_comp_wq);
1031 err:
1032 	destroy_workqueue(ib_wq);
1033 	return ret;
1034 }
1035 
1036 static void __exit ib_core_cleanup(void)
1037 {
1038 	ib_cache_cleanup();
1039 	ib_sa_cleanup();
1040 	ib_mad_cleanup();
1041 	addr_cleanup();
1042 	class_unregister(&ib_class);
1043 	destroy_workqueue(ib_comp_wq);
1044 	/* Make sure that any pending umem accounting work is done. */
1045 	destroy_workqueue(ib_wq);
1046 }
1047 
1048 module_init(ib_core_init);
1049 module_exit(ib_core_cleanup);
1050 
1051 MODULE_VERSION(ibcore, 1);
1052 MODULE_DEPEND(ibcore, linuxkpi, 1, 1, 1);
1053