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