Merge branch 'upstream'
[sfrench/cifs-2.6.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  * $Id: device.c 1349 2004-12-16 21:09:43Z roland $
34  */
35
36 #include <linux/module.h>
37 #include <linux/string.h>
38 #include <linux/errno.h>
39 #include <linux/slab.h>
40 #include <linux/init.h>
41
42 #include <asm/semaphore.h>
43
44 #include "core_priv.h"
45
46 MODULE_AUTHOR("Roland Dreier");
47 MODULE_DESCRIPTION("core kernel InfiniBand API");
48 MODULE_LICENSE("Dual BSD/GPL");
49
50 struct ib_client_data {
51         struct list_head  list;
52         struct ib_client *client;
53         void *            data;
54 };
55
56 static LIST_HEAD(device_list);
57 static LIST_HEAD(client_list);
58
59 /*
60  * device_sem protects access to both device_list and client_list.
61  * There's no real point to using multiple locks or something fancier
62  * like an rwsem: we always access both lists, and we're always
63  * modifying one list or the other list.  In any case this is not a
64  * hot path so there's no point in trying to optimize.
65  */
66 static DECLARE_MUTEX(device_sem);
67
68 static int ib_device_check_mandatory(struct ib_device *device)
69 {
70 #define IB_MANDATORY_FUNC(x) { offsetof(struct ib_device, x), #x }
71         static const struct {
72                 size_t offset;
73                 char  *name;
74         } mandatory_table[] = {
75                 IB_MANDATORY_FUNC(query_device),
76                 IB_MANDATORY_FUNC(query_port),
77                 IB_MANDATORY_FUNC(query_pkey),
78                 IB_MANDATORY_FUNC(query_gid),
79                 IB_MANDATORY_FUNC(alloc_pd),
80                 IB_MANDATORY_FUNC(dealloc_pd),
81                 IB_MANDATORY_FUNC(create_ah),
82                 IB_MANDATORY_FUNC(destroy_ah),
83                 IB_MANDATORY_FUNC(create_qp),
84                 IB_MANDATORY_FUNC(modify_qp),
85                 IB_MANDATORY_FUNC(destroy_qp),
86                 IB_MANDATORY_FUNC(post_send),
87                 IB_MANDATORY_FUNC(post_recv),
88                 IB_MANDATORY_FUNC(create_cq),
89                 IB_MANDATORY_FUNC(destroy_cq),
90                 IB_MANDATORY_FUNC(poll_cq),
91                 IB_MANDATORY_FUNC(req_notify_cq),
92                 IB_MANDATORY_FUNC(get_dma_mr),
93                 IB_MANDATORY_FUNC(dereg_mr)
94         };
95         int i;
96
97         for (i = 0; i < sizeof mandatory_table / sizeof mandatory_table[0]; ++i) {
98                 if (!*(void **) ((void *) device + mandatory_table[i].offset)) {
99                         printk(KERN_WARNING "Device %s is missing mandatory function %s\n",
100                                device->name, mandatory_table[i].name);
101                         return -EINVAL;
102                 }
103         }
104
105         return 0;
106 }
107
108 static struct ib_device *__ib_device_get_by_name(const char *name)
109 {
110         struct ib_device *device;
111
112         list_for_each_entry(device, &device_list, core_list)
113                 if (!strncmp(name, device->name, IB_DEVICE_NAME_MAX))
114                         return device;
115
116         return NULL;
117 }
118
119
120 static int alloc_name(char *name)
121 {
122         long *inuse;
123         char buf[IB_DEVICE_NAME_MAX];
124         struct ib_device *device;
125         int i;
126
127         inuse = (long *) get_zeroed_page(GFP_KERNEL);
128         if (!inuse)
129                 return -ENOMEM;
130
131         list_for_each_entry(device, &device_list, core_list) {
132                 if (!sscanf(device->name, name, &i))
133                         continue;
134                 if (i < 0 || i >= PAGE_SIZE * 8)
135                         continue;
136                 snprintf(buf, sizeof buf, name, i);
137                 if (!strncmp(buf, device->name, IB_DEVICE_NAME_MAX))
138                         set_bit(i, inuse);
139         }
140
141         i = find_first_zero_bit(inuse, PAGE_SIZE * 8);
142         free_page((unsigned long) inuse);
143         snprintf(buf, sizeof buf, name, i);
144
145         if (__ib_device_get_by_name(buf))
146                 return -ENFILE;
147
148         strlcpy(name, buf, IB_DEVICE_NAME_MAX);
149         return 0;
150 }
151
152 /**
153  * ib_alloc_device - allocate an IB device struct
154  * @size:size of structure to allocate
155  *
156  * Low-level drivers should use ib_alloc_device() to allocate &struct
157  * ib_device.  @size is the size of the structure to be allocated,
158  * including any private data used by the low-level driver.
159  * ib_dealloc_device() must be used to free structures allocated with
160  * ib_alloc_device().
161  */
162 struct ib_device *ib_alloc_device(size_t size)
163 {
164         void *dev;
165
166         BUG_ON(size < sizeof (struct ib_device));
167
168         dev = kmalloc(size, GFP_KERNEL);
169         if (!dev)
170                 return NULL;
171
172         memset(dev, 0, size);
173
174         return dev;
175 }
176 EXPORT_SYMBOL(ib_alloc_device);
177
178 /**
179  * ib_dealloc_device - free an IB device struct
180  * @device:structure to free
181  *
182  * Free a structure allocated with ib_alloc_device().
183  */
184 void ib_dealloc_device(struct ib_device *device)
185 {
186         if (device->reg_state == IB_DEV_UNINITIALIZED) {
187                 kfree(device);
188                 return;
189         }
190
191         BUG_ON(device->reg_state != IB_DEV_UNREGISTERED);
192
193         ib_device_unregister_sysfs(device);
194 }
195 EXPORT_SYMBOL(ib_dealloc_device);
196
197 static int add_client_context(struct ib_device *device, struct ib_client *client)
198 {
199         struct ib_client_data *context;
200         unsigned long flags;
201
202         context = kmalloc(sizeof *context, GFP_KERNEL);
203         if (!context) {
204                 printk(KERN_WARNING "Couldn't allocate client context for %s/%s\n",
205                        device->name, client->name);
206                 return -ENOMEM;
207         }
208
209         context->client = client;
210         context->data   = NULL;
211
212         spin_lock_irqsave(&device->client_data_lock, flags);
213         list_add(&context->list, &device->client_data_list);
214         spin_unlock_irqrestore(&device->client_data_lock, flags);
215
216         return 0;
217 }
218
219 /**
220  * ib_register_device - Register an IB device with IB core
221  * @device:Device to register
222  *
223  * Low-level drivers use ib_register_device() to register their
224  * devices with the IB core.  All registered clients will receive a
225  * callback for each device that is added. @device must be allocated
226  * with ib_alloc_device().
227  */
228 int ib_register_device(struct ib_device *device)
229 {
230         int ret;
231
232         down(&device_sem);
233
234         if (strchr(device->name, '%')) {
235                 ret = alloc_name(device->name);
236                 if (ret)
237                         goto out;
238         }
239
240         if (ib_device_check_mandatory(device)) {
241                 ret = -EINVAL;
242                 goto out;
243         }
244
245         INIT_LIST_HEAD(&device->event_handler_list);
246         INIT_LIST_HEAD(&device->client_data_list);
247         spin_lock_init(&device->event_handler_lock);
248         spin_lock_init(&device->client_data_lock);
249
250         ret = ib_device_register_sysfs(device);
251         if (ret) {
252                 printk(KERN_WARNING "Couldn't register device %s with driver model\n",
253                        device->name);
254                 goto out;
255         }
256
257         list_add_tail(&device->core_list, &device_list);
258
259         device->reg_state = IB_DEV_REGISTERED;
260
261         {
262                 struct ib_client *client;
263
264                 list_for_each_entry(client, &client_list, list)
265                         if (client->add && !add_client_context(device, client))
266                                 client->add(device);
267         }
268
269  out:
270         up(&device_sem);
271         return ret;
272 }
273 EXPORT_SYMBOL(ib_register_device);
274
275 /**
276  * ib_unregister_device - Unregister an IB device
277  * @device:Device to unregister
278  *
279  * Unregister an IB device.  All clients will receive a remove callback.
280  */
281 void ib_unregister_device(struct ib_device *device)
282 {
283         struct ib_client *client;
284         struct ib_client_data *context, *tmp;
285         unsigned long flags;
286
287         down(&device_sem);
288
289         list_for_each_entry_reverse(client, &client_list, list)
290                 if (client->remove)
291                         client->remove(device);
292
293         list_del(&device->core_list);
294
295         up(&device_sem);
296
297         spin_lock_irqsave(&device->client_data_lock, flags);
298         list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
299                 kfree(context);
300         spin_unlock_irqrestore(&device->client_data_lock, flags);
301
302         device->reg_state = IB_DEV_UNREGISTERED;
303 }
304 EXPORT_SYMBOL(ib_unregister_device);
305
306 /**
307  * ib_register_client - Register an IB client
308  * @client:Client to register
309  *
310  * Upper level users of the IB drivers can use ib_register_client() to
311  * register callbacks for IB device addition and removal.  When an IB
312  * device is added, each registered client's add method will be called
313  * (in the order the clients were registered), and when a device is
314  * removed, each client's remove method will be called (in the reverse
315  * order that clients were registered).  In addition, when
316  * ib_register_client() is called, the client will receive an add
317  * callback for all devices already registered.
318  */
319 int ib_register_client(struct ib_client *client)
320 {
321         struct ib_device *device;
322
323         down(&device_sem);
324
325         list_add_tail(&client->list, &client_list);
326         list_for_each_entry(device, &device_list, core_list)
327                 if (client->add && !add_client_context(device, client))
328                         client->add(device);
329
330         up(&device_sem);
331
332         return 0;
333 }
334 EXPORT_SYMBOL(ib_register_client);
335
336 /**
337  * ib_unregister_client - Unregister an IB client
338  * @client:Client to unregister
339  *
340  * Upper level users use ib_unregister_client() to remove their client
341  * registration.  When ib_unregister_client() is called, the client
342  * will receive a remove callback for each IB device still registered.
343  */
344 void ib_unregister_client(struct ib_client *client)
345 {
346         struct ib_client_data *context, *tmp;
347         struct ib_device *device;
348         unsigned long flags;
349
350         down(&device_sem);
351
352         list_for_each_entry(device, &device_list, core_list) {
353                 if (client->remove)
354                         client->remove(device);
355
356                 spin_lock_irqsave(&device->client_data_lock, flags);
357                 list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
358                         if (context->client == client) {
359                                 list_del(&context->list);
360                                 kfree(context);
361                         }
362                 spin_unlock_irqrestore(&device->client_data_lock, flags);
363         }
364         list_del(&client->list);
365
366         up(&device_sem);
367 }
368 EXPORT_SYMBOL(ib_unregister_client);
369
370 /**
371  * ib_get_client_data - Get IB client context
372  * @device:Device to get context for
373  * @client:Client to get context for
374  *
375  * ib_get_client_data() returns client context set with
376  * ib_set_client_data().
377  */
378 void *ib_get_client_data(struct ib_device *device, struct ib_client *client)
379 {
380         struct ib_client_data *context;
381         void *ret = NULL;
382         unsigned long flags;
383
384         spin_lock_irqsave(&device->client_data_lock, flags);
385         list_for_each_entry(context, &device->client_data_list, list)
386                 if (context->client == client) {
387                         ret = context->data;
388                         break;
389                 }
390         spin_unlock_irqrestore(&device->client_data_lock, flags);
391
392         return ret;
393 }
394 EXPORT_SYMBOL(ib_get_client_data);
395
396 /**
397  * ib_set_client_data - Get IB client context
398  * @device:Device to set context for
399  * @client:Client to set context for
400  * @data:Context to set
401  *
402  * ib_set_client_data() sets client context that can be retrieved with
403  * ib_get_client_data().
404  */
405 void ib_set_client_data(struct ib_device *device, struct ib_client *client,
406                         void *data)
407 {
408         struct ib_client_data *context;
409         unsigned long flags;
410
411         spin_lock_irqsave(&device->client_data_lock, flags);
412         list_for_each_entry(context, &device->client_data_list, list)
413                 if (context->client == client) {
414                         context->data = data;
415                         goto out;
416                 }
417
418         printk(KERN_WARNING "No client context found for %s/%s\n",
419                device->name, client->name);
420
421 out:
422         spin_unlock_irqrestore(&device->client_data_lock, flags);
423 }
424 EXPORT_SYMBOL(ib_set_client_data);
425
426 /**
427  * ib_register_event_handler - Register an IB event handler
428  * @event_handler:Handler to register
429  *
430  * ib_register_event_handler() registers an event handler that will be
431  * called back when asynchronous IB events occur (as defined in
432  * chapter 11 of the InfiniBand Architecture Specification).  This
433  * callback may occur in interrupt context.
434  */
435 int ib_register_event_handler  (struct ib_event_handler *event_handler)
436 {
437         unsigned long flags;
438
439         spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
440         list_add_tail(&event_handler->list,
441                       &event_handler->device->event_handler_list);
442         spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
443
444         return 0;
445 }
446 EXPORT_SYMBOL(ib_register_event_handler);
447
448 /**
449  * ib_unregister_event_handler - Unregister an event handler
450  * @event_handler:Handler to unregister
451  *
452  * Unregister an event handler registered with
453  * ib_register_event_handler().
454  */
455 int ib_unregister_event_handler(struct ib_event_handler *event_handler)
456 {
457         unsigned long flags;
458
459         spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
460         list_del(&event_handler->list);
461         spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
462
463         return 0;
464 }
465 EXPORT_SYMBOL(ib_unregister_event_handler);
466
467 /**
468  * ib_dispatch_event - Dispatch an asynchronous event
469  * @event:Event to dispatch
470  *
471  * Low-level drivers must call ib_dispatch_event() to dispatch the
472  * event to all registered event handlers when an asynchronous event
473  * occurs.
474  */
475 void ib_dispatch_event(struct ib_event *event)
476 {
477         unsigned long flags;
478         struct ib_event_handler *handler;
479
480         spin_lock_irqsave(&event->device->event_handler_lock, flags);
481
482         list_for_each_entry(handler, &event->device->event_handler_list, list)
483                 handler->handler(handler, event);
484
485         spin_unlock_irqrestore(&event->device->event_handler_lock, flags);
486 }
487 EXPORT_SYMBOL(ib_dispatch_event);
488
489 /**
490  * ib_query_device - Query IB device attributes
491  * @device:Device to query
492  * @device_attr:Device attributes
493  *
494  * ib_query_device() returns the attributes of a device through the
495  * @device_attr pointer.
496  */
497 int ib_query_device(struct ib_device *device,
498                     struct ib_device_attr *device_attr)
499 {
500         return device->query_device(device, device_attr);
501 }
502 EXPORT_SYMBOL(ib_query_device);
503
504 /**
505  * ib_query_port - Query IB port attributes
506  * @device:Device to query
507  * @port_num:Port number to query
508  * @port_attr:Port attributes
509  *
510  * ib_query_port() returns the attributes of a port through the
511  * @port_attr pointer.
512  */
513 int ib_query_port(struct ib_device *device,
514                   u8 port_num,
515                   struct ib_port_attr *port_attr)
516 {
517         if (device->node_type == IB_NODE_SWITCH) {
518                 if (port_num)
519                         return -EINVAL;
520         } else if (port_num < 1 || port_num > device->phys_port_cnt)
521                 return -EINVAL;
522
523         return device->query_port(device, port_num, port_attr);
524 }
525 EXPORT_SYMBOL(ib_query_port);
526
527 /**
528  * ib_query_gid - Get GID table entry
529  * @device:Device to query
530  * @port_num:Port number to query
531  * @index:GID table index to query
532  * @gid:Returned GID
533  *
534  * ib_query_gid() fetches the specified GID table entry.
535  */
536 int ib_query_gid(struct ib_device *device,
537                  u8 port_num, int index, union ib_gid *gid)
538 {
539         return device->query_gid(device, port_num, index, gid);
540 }
541 EXPORT_SYMBOL(ib_query_gid);
542
543 /**
544  * ib_query_pkey - Get P_Key table entry
545  * @device:Device to query
546  * @port_num:Port number to query
547  * @index:P_Key table index to query
548  * @pkey:Returned P_Key
549  *
550  * ib_query_pkey() fetches the specified P_Key table entry.
551  */
552 int ib_query_pkey(struct ib_device *device,
553                   u8 port_num, u16 index, u16 *pkey)
554 {
555         return device->query_pkey(device, port_num, index, pkey);
556 }
557 EXPORT_SYMBOL(ib_query_pkey);
558
559 /**
560  * ib_modify_device - Change IB device attributes
561  * @device:Device to modify
562  * @device_modify_mask:Mask of attributes to change
563  * @device_modify:New attribute values
564  *
565  * ib_modify_device() changes a device's attributes as specified by
566  * the @device_modify_mask and @device_modify structure.
567  */
568 int ib_modify_device(struct ib_device *device,
569                      int device_modify_mask,
570                      struct ib_device_modify *device_modify)
571 {
572         return device->modify_device(device, device_modify_mask,
573                                      device_modify);
574 }
575 EXPORT_SYMBOL(ib_modify_device);
576
577 /**
578  * ib_modify_port - Modifies the attributes for the specified port.
579  * @device: The device to modify.
580  * @port_num: The number of the port to modify.
581  * @port_modify_mask: Mask used to specify which attributes of the port
582  *   to change.
583  * @port_modify: New attribute values for the port.
584  *
585  * ib_modify_port() changes a port's attributes as specified by the
586  * @port_modify_mask and @port_modify structure.
587  */
588 int ib_modify_port(struct ib_device *device,
589                    u8 port_num, int port_modify_mask,
590                    struct ib_port_modify *port_modify)
591 {
592         if (device->node_type == IB_NODE_SWITCH) {
593                 if (port_num)
594                         return -EINVAL;
595         } else if (port_num < 1 || port_num > device->phys_port_cnt)
596                 return -EINVAL;
597
598         return device->modify_port(device, port_num, port_modify_mask,
599                                    port_modify);
600 }
601 EXPORT_SYMBOL(ib_modify_port);
602
603 static int __init ib_core_init(void)
604 {
605         int ret;
606
607         ret = ib_sysfs_setup();
608         if (ret)
609                 printk(KERN_WARNING "Couldn't create InfiniBand device class\n");
610
611         ret = ib_cache_setup();
612         if (ret) {
613                 printk(KERN_WARNING "Couldn't set up InfiniBand P_Key/GID cache\n");
614                 ib_sysfs_cleanup();
615         }
616
617         return ret;
618 }
619
620 static void __exit ib_core_cleanup(void)
621 {
622         ib_cache_cleanup();
623         ib_sysfs_cleanup();
624 }
625
626 module_init(ib_core_init);
627 module_exit(ib_core_cleanup);