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