IB/core: Use start_port() and end_port()
[GitHub/mt8127/android_kernel_alcatel_ttab.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/kernel.h>
40 #include <linux/slab.h>
41 #include <linux/init.h>
42 #include <linux/mutex.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_mutex 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 DEFINE_MUTEX(device_mutex);
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 < ARRAY_SIZE(mandatory_table); ++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 static int start_port(struct ib_device *device)
153 {
154 return (device->node_type == RDMA_NODE_IB_SWITCH) ? 0 : 1;
155 }
156
157
158 static int end_port(struct ib_device *device)
159 {
160 return (device->node_type == RDMA_NODE_IB_SWITCH) ?
161 0 : device->phys_port_cnt;
162 }
163
164 /**
165 * ib_alloc_device - allocate an IB device struct
166 * @size:size of structure to allocate
167 *
168 * Low-level drivers should use ib_alloc_device() to allocate &struct
169 * ib_device. @size is the size of the structure to be allocated,
170 * including any private data used by the low-level driver.
171 * ib_dealloc_device() must be used to free structures allocated with
172 * ib_alloc_device().
173 */
174 struct ib_device *ib_alloc_device(size_t size)
175 {
176 BUG_ON(size < sizeof (struct ib_device));
177
178 return kzalloc(size, GFP_KERNEL);
179 }
180 EXPORT_SYMBOL(ib_alloc_device);
181
182 /**
183 * ib_dealloc_device - free an IB device struct
184 * @device:structure to free
185 *
186 * Free a structure allocated with ib_alloc_device().
187 */
188 void ib_dealloc_device(struct ib_device *device)
189 {
190 if (device->reg_state == IB_DEV_UNINITIALIZED) {
191 kfree(device);
192 return;
193 }
194
195 BUG_ON(device->reg_state != IB_DEV_UNREGISTERED);
196
197 ib_device_unregister_sysfs(device);
198 }
199 EXPORT_SYMBOL(ib_dealloc_device);
200
201 static int add_client_context(struct ib_device *device, struct ib_client *client)
202 {
203 struct ib_client_data *context;
204 unsigned long flags;
205
206 context = kmalloc(sizeof *context, GFP_KERNEL);
207 if (!context) {
208 printk(KERN_WARNING "Couldn't allocate client context for %s/%s\n",
209 device->name, client->name);
210 return -ENOMEM;
211 }
212
213 context->client = client;
214 context->data = NULL;
215
216 spin_lock_irqsave(&device->client_data_lock, flags);
217 list_add(&context->list, &device->client_data_list);
218 spin_unlock_irqrestore(&device->client_data_lock, flags);
219
220 return 0;
221 }
222
223 static int read_port_table_lengths(struct ib_device *device)
224 {
225 struct ib_port_attr *tprops = NULL;
226 int num_ports, ret = -ENOMEM;
227 u8 port_index;
228
229 tprops = kmalloc(sizeof *tprops, GFP_KERNEL);
230 if (!tprops)
231 goto out;
232
233 num_ports = end_port(device) - start_port(device) + 1;
234
235 device->pkey_tbl_len = kmalloc(sizeof *device->pkey_tbl_len * num_ports,
236 GFP_KERNEL);
237 device->gid_tbl_len = kmalloc(sizeof *device->gid_tbl_len * num_ports,
238 GFP_KERNEL);
239 if (!device->pkey_tbl_len || !device->gid_tbl_len)
240 goto err;
241
242 for (port_index = 0; port_index < num_ports; ++port_index) {
243 ret = ib_query_port(device, port_index + start_port(device),
244 tprops);
245 if (ret)
246 goto err;
247 device->pkey_tbl_len[port_index] = tprops->pkey_tbl_len;
248 device->gid_tbl_len[port_index] = tprops->gid_tbl_len;
249 }
250
251 ret = 0;
252 goto out;
253
254 err:
255 kfree(device->gid_tbl_len);
256 kfree(device->pkey_tbl_len);
257 out:
258 kfree(tprops);
259 return ret;
260 }
261
262 /**
263 * ib_register_device - Register an IB device with IB core
264 * @device:Device to register
265 *
266 * Low-level drivers use ib_register_device() to register their
267 * devices with the IB core. All registered clients will receive a
268 * callback for each device that is added. @device must be allocated
269 * with ib_alloc_device().
270 */
271 int ib_register_device(struct ib_device *device)
272 {
273 int ret;
274
275 mutex_lock(&device_mutex);
276
277 if (strchr(device->name, '%')) {
278 ret = alloc_name(device->name);
279 if (ret)
280 goto out;
281 }
282
283 if (ib_device_check_mandatory(device)) {
284 ret = -EINVAL;
285 goto out;
286 }
287
288 INIT_LIST_HEAD(&device->event_handler_list);
289 INIT_LIST_HEAD(&device->client_data_list);
290 spin_lock_init(&device->event_handler_lock);
291 spin_lock_init(&device->client_data_lock);
292
293 ret = read_port_table_lengths(device);
294 if (ret) {
295 printk(KERN_WARNING "Couldn't create table lengths cache for device %s\n",
296 device->name);
297 goto out;
298 }
299
300 ret = ib_device_register_sysfs(device);
301 if (ret) {
302 printk(KERN_WARNING "Couldn't register device %s with driver model\n",
303 device->name);
304 kfree(device->gid_tbl_len);
305 kfree(device->pkey_tbl_len);
306 goto out;
307 }
308
309 list_add_tail(&device->core_list, &device_list);
310
311 device->reg_state = IB_DEV_REGISTERED;
312
313 {
314 struct ib_client *client;
315
316 list_for_each_entry(client, &client_list, list)
317 if (client->add && !add_client_context(device, client))
318 client->add(device);
319 }
320
321 out:
322 mutex_unlock(&device_mutex);
323 return ret;
324 }
325 EXPORT_SYMBOL(ib_register_device);
326
327 /**
328 * ib_unregister_device - Unregister an IB device
329 * @device:Device to unregister
330 *
331 * Unregister an IB device. All clients will receive a remove callback.
332 */
333 void ib_unregister_device(struct ib_device *device)
334 {
335 struct ib_client *client;
336 struct ib_client_data *context, *tmp;
337 unsigned long flags;
338
339 mutex_lock(&device_mutex);
340
341 list_for_each_entry_reverse(client, &client_list, list)
342 if (client->remove)
343 client->remove(device);
344
345 list_del(&device->core_list);
346
347 kfree(device->gid_tbl_len);
348 kfree(device->pkey_tbl_len);
349
350 mutex_unlock(&device_mutex);
351
352 spin_lock_irqsave(&device->client_data_lock, flags);
353 list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
354 kfree(context);
355 spin_unlock_irqrestore(&device->client_data_lock, flags);
356
357 device->reg_state = IB_DEV_UNREGISTERED;
358 }
359 EXPORT_SYMBOL(ib_unregister_device);
360
361 /**
362 * ib_register_client - Register an IB client
363 * @client:Client to register
364 *
365 * Upper level users of the IB drivers can use ib_register_client() to
366 * register callbacks for IB device addition and removal. When an IB
367 * device is added, each registered client's add method will be called
368 * (in the order the clients were registered), and when a device is
369 * removed, each client's remove method will be called (in the reverse
370 * order that clients were registered). In addition, when
371 * ib_register_client() is called, the client will receive an add
372 * callback for all devices already registered.
373 */
374 int ib_register_client(struct ib_client *client)
375 {
376 struct ib_device *device;
377
378 mutex_lock(&device_mutex);
379
380 list_add_tail(&client->list, &client_list);
381 list_for_each_entry(device, &device_list, core_list)
382 if (client->add && !add_client_context(device, client))
383 client->add(device);
384
385 mutex_unlock(&device_mutex);
386
387 return 0;
388 }
389 EXPORT_SYMBOL(ib_register_client);
390
391 /**
392 * ib_unregister_client - Unregister an IB client
393 * @client:Client to unregister
394 *
395 * Upper level users use ib_unregister_client() to remove their client
396 * registration. When ib_unregister_client() is called, the client
397 * will receive a remove callback for each IB device still registered.
398 */
399 void ib_unregister_client(struct ib_client *client)
400 {
401 struct ib_client_data *context, *tmp;
402 struct ib_device *device;
403 unsigned long flags;
404
405 mutex_lock(&device_mutex);
406
407 list_for_each_entry(device, &device_list, core_list) {
408 if (client->remove)
409 client->remove(device);
410
411 spin_lock_irqsave(&device->client_data_lock, flags);
412 list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
413 if (context->client == client) {
414 list_del(&context->list);
415 kfree(context);
416 }
417 spin_unlock_irqrestore(&device->client_data_lock, flags);
418 }
419 list_del(&client->list);
420
421 mutex_unlock(&device_mutex);
422 }
423 EXPORT_SYMBOL(ib_unregister_client);
424
425 /**
426 * ib_get_client_data - Get IB client context
427 * @device:Device to get context for
428 * @client:Client to get context for
429 *
430 * ib_get_client_data() returns client context set with
431 * ib_set_client_data().
432 */
433 void *ib_get_client_data(struct ib_device *device, struct ib_client *client)
434 {
435 struct ib_client_data *context;
436 void *ret = NULL;
437 unsigned long flags;
438
439 spin_lock_irqsave(&device->client_data_lock, flags);
440 list_for_each_entry(context, &device->client_data_list, list)
441 if (context->client == client) {
442 ret = context->data;
443 break;
444 }
445 spin_unlock_irqrestore(&device->client_data_lock, flags);
446
447 return ret;
448 }
449 EXPORT_SYMBOL(ib_get_client_data);
450
451 /**
452 * ib_set_client_data - Set IB client context
453 * @device:Device to set context for
454 * @client:Client to set context for
455 * @data:Context to set
456 *
457 * ib_set_client_data() sets client context that can be retrieved with
458 * ib_get_client_data().
459 */
460 void ib_set_client_data(struct ib_device *device, struct ib_client *client,
461 void *data)
462 {
463 struct ib_client_data *context;
464 unsigned long flags;
465
466 spin_lock_irqsave(&device->client_data_lock, flags);
467 list_for_each_entry(context, &device->client_data_list, list)
468 if (context->client == client) {
469 context->data = data;
470 goto out;
471 }
472
473 printk(KERN_WARNING "No client context found for %s/%s\n",
474 device->name, client->name);
475
476 out:
477 spin_unlock_irqrestore(&device->client_data_lock, flags);
478 }
479 EXPORT_SYMBOL(ib_set_client_data);
480
481 /**
482 * ib_register_event_handler - Register an IB event handler
483 * @event_handler:Handler to register
484 *
485 * ib_register_event_handler() registers an event handler that will be
486 * called back when asynchronous IB events occur (as defined in
487 * chapter 11 of the InfiniBand Architecture Specification). This
488 * callback may occur in interrupt context.
489 */
490 int ib_register_event_handler (struct ib_event_handler *event_handler)
491 {
492 unsigned long flags;
493
494 spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
495 list_add_tail(&event_handler->list,
496 &event_handler->device->event_handler_list);
497 spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
498
499 return 0;
500 }
501 EXPORT_SYMBOL(ib_register_event_handler);
502
503 /**
504 * ib_unregister_event_handler - Unregister an event handler
505 * @event_handler:Handler to unregister
506 *
507 * Unregister an event handler registered with
508 * ib_register_event_handler().
509 */
510 int ib_unregister_event_handler(struct ib_event_handler *event_handler)
511 {
512 unsigned long flags;
513
514 spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
515 list_del(&event_handler->list);
516 spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
517
518 return 0;
519 }
520 EXPORT_SYMBOL(ib_unregister_event_handler);
521
522 /**
523 * ib_dispatch_event - Dispatch an asynchronous event
524 * @event:Event to dispatch
525 *
526 * Low-level drivers must call ib_dispatch_event() to dispatch the
527 * event to all registered event handlers when an asynchronous event
528 * occurs.
529 */
530 void ib_dispatch_event(struct ib_event *event)
531 {
532 unsigned long flags;
533 struct ib_event_handler *handler;
534
535 spin_lock_irqsave(&event->device->event_handler_lock, flags);
536
537 list_for_each_entry(handler, &event->device->event_handler_list, list)
538 handler->handler(handler, event);
539
540 spin_unlock_irqrestore(&event->device->event_handler_lock, flags);
541 }
542 EXPORT_SYMBOL(ib_dispatch_event);
543
544 /**
545 * ib_query_device - Query IB device attributes
546 * @device:Device to query
547 * @device_attr:Device attributes
548 *
549 * ib_query_device() returns the attributes of a device through the
550 * @device_attr pointer.
551 */
552 int ib_query_device(struct ib_device *device,
553 struct ib_device_attr *device_attr)
554 {
555 return device->query_device(device, device_attr);
556 }
557 EXPORT_SYMBOL(ib_query_device);
558
559 /**
560 * ib_query_port - Query IB port attributes
561 * @device:Device to query
562 * @port_num:Port number to query
563 * @port_attr:Port attributes
564 *
565 * ib_query_port() returns the attributes of a port through the
566 * @port_attr pointer.
567 */
568 int ib_query_port(struct ib_device *device,
569 u8 port_num,
570 struct ib_port_attr *port_attr)
571 {
572 if (port_num < start_port(device) || port_num > end_port(device))
573 return -EINVAL;
574
575 return device->query_port(device, port_num, port_attr);
576 }
577 EXPORT_SYMBOL(ib_query_port);
578
579 /**
580 * ib_query_gid - Get GID table entry
581 * @device:Device to query
582 * @port_num:Port number to query
583 * @index:GID table index to query
584 * @gid:Returned GID
585 *
586 * ib_query_gid() fetches the specified GID table entry.
587 */
588 int ib_query_gid(struct ib_device *device,
589 u8 port_num, int index, union ib_gid *gid)
590 {
591 return device->query_gid(device, port_num, index, gid);
592 }
593 EXPORT_SYMBOL(ib_query_gid);
594
595 /**
596 * ib_query_pkey - Get P_Key table entry
597 * @device:Device to query
598 * @port_num:Port number to query
599 * @index:P_Key table index to query
600 * @pkey:Returned P_Key
601 *
602 * ib_query_pkey() fetches the specified P_Key table entry.
603 */
604 int ib_query_pkey(struct ib_device *device,
605 u8 port_num, u16 index, u16 *pkey)
606 {
607 return device->query_pkey(device, port_num, index, pkey);
608 }
609 EXPORT_SYMBOL(ib_query_pkey);
610
611 /**
612 * ib_modify_device - Change IB device attributes
613 * @device:Device to modify
614 * @device_modify_mask:Mask of attributes to change
615 * @device_modify:New attribute values
616 *
617 * ib_modify_device() changes a device's attributes as specified by
618 * the @device_modify_mask and @device_modify structure.
619 */
620 int ib_modify_device(struct ib_device *device,
621 int device_modify_mask,
622 struct ib_device_modify *device_modify)
623 {
624 return device->modify_device(device, device_modify_mask,
625 device_modify);
626 }
627 EXPORT_SYMBOL(ib_modify_device);
628
629 /**
630 * ib_modify_port - Modifies the attributes for the specified port.
631 * @device: The device to modify.
632 * @port_num: The number of the port to modify.
633 * @port_modify_mask: Mask used to specify which attributes of the port
634 * to change.
635 * @port_modify: New attribute values for the port.
636 *
637 * ib_modify_port() changes a port's attributes as specified by the
638 * @port_modify_mask and @port_modify structure.
639 */
640 int ib_modify_port(struct ib_device *device,
641 u8 port_num, int port_modify_mask,
642 struct ib_port_modify *port_modify)
643 {
644 if (port_num < start_port(device) || port_num > end_port(device))
645 return -EINVAL;
646
647 return device->modify_port(device, port_num, port_modify_mask,
648 port_modify);
649 }
650 EXPORT_SYMBOL(ib_modify_port);
651
652 /**
653 * ib_find_gid - Returns the port number and GID table index where
654 * a specified GID value occurs.
655 * @device: The device to query.
656 * @gid: The GID value to search for.
657 * @port_num: The port number of the device where the GID value was found.
658 * @index: The index into the GID table where the GID was found. This
659 * parameter may be NULL.
660 */
661 int ib_find_gid(struct ib_device *device, union ib_gid *gid,
662 u8 *port_num, u16 *index)
663 {
664 union ib_gid tmp_gid;
665 int ret, port, i;
666
667 for (port = start_port(device); port <= end_port(device); ++port) {
668 for (i = 0; i < device->gid_tbl_len[port - start_port(device)]; ++i) {
669 ret = ib_query_gid(device, port, i, &tmp_gid);
670 if (ret)
671 return ret;
672 if (!memcmp(&tmp_gid, gid, sizeof *gid)) {
673 *port_num = port;
674 if (index)
675 *index = i;
676 return 0;
677 }
678 }
679 }
680
681 return -ENOENT;
682 }
683 EXPORT_SYMBOL(ib_find_gid);
684
685 /**
686 * ib_find_pkey - Returns the PKey table index where a specified
687 * PKey value occurs.
688 * @device: The device to query.
689 * @port_num: The port number of the device to search for the PKey.
690 * @pkey: The PKey value to search for.
691 * @index: The index into the PKey table where the PKey was found.
692 */
693 int ib_find_pkey(struct ib_device *device,
694 u8 port_num, u16 pkey, u16 *index)
695 {
696 int ret, i;
697 u16 tmp_pkey;
698
699 for (i = 0; i < device->pkey_tbl_len[port_num - start_port(device)]; ++i) {
700 ret = ib_query_pkey(device, port_num, i, &tmp_pkey);
701 if (ret)
702 return ret;
703
704 if (pkey == tmp_pkey) {
705 *index = i;
706 return 0;
707 }
708 }
709
710 return -ENOENT;
711 }
712 EXPORT_SYMBOL(ib_find_pkey);
713
714 static int __init ib_core_init(void)
715 {
716 int ret;
717
718 ret = ib_sysfs_setup();
719 if (ret)
720 printk(KERN_WARNING "Couldn't create InfiniBand device class\n");
721
722 ret = ib_cache_setup();
723 if (ret) {
724 printk(KERN_WARNING "Couldn't set up InfiniBand P_Key/GID cache\n");
725 ib_sysfs_cleanup();
726 }
727
728 return ret;
729 }
730
731 static void __exit ib_core_cleanup(void)
732 {
733 ib_cache_cleanup();
734 ib_sysfs_cleanup();
735 /* Make sure that any pending umem accounting work is done. */
736 flush_scheduled_work();
737 }
738
739 module_init(ib_core_init);
740 module_exit(ib_core_cleanup);