2 * Copyright (c) 2004 Topspin Communications. All rights reserved.
3 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
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:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
33 * $Id: device.c 1349 2004-12-16 21:09:43Z roland $
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/types.h>
40 #include <sys/param.h>
41 #include <sys/condvar.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
44 #include <sys/kernel.h>
45 #include <sys/libkern.h>
46 #include <sys/socket.h>
47 #include <sys/module.h>
49 #include <sys/mutex.h>
50 #include <sys/rwlock.h>
51 #include <sys/queue.h>
52 #include <sys/taskqueue.h>
54 #include <sys/syslog.h>
56 #include <contrib/rdma/core_priv.h>
58 struct ib_client_data {
59 TAILQ_ENTRY(ib_client_data) list;
60 struct ib_client *client;
64 static TAILQ_HEAD(, ib_device) device_list;
65 static TAILQ_HEAD(client_list_s, ib_client) client_list;
68 * device_mutex protects access to both device_list and client_list.
69 * There's no real point to using multiple locks or something fancier
70 * like an rwsem: we always access both lists, and we're always
71 * modifying one list or the other list. In any case this is not a
72 * hot path so there's no point in trying to optimize.
74 static struct mtx device_mutex;
76 static int ib_device_check_mandatory(struct ib_device *device)
78 #define IB_MANDATORY_FUNC(x) { offsetof(struct ib_device, x), #x }
79 #define MANDATORY_TABLE_DEPTH 19
83 } mandatory_table[] = {
84 IB_MANDATORY_FUNC(query_device),
85 IB_MANDATORY_FUNC(query_port),
86 IB_MANDATORY_FUNC(query_pkey),
87 IB_MANDATORY_FUNC(query_gid),
88 IB_MANDATORY_FUNC(alloc_pd),
89 IB_MANDATORY_FUNC(dealloc_pd),
90 IB_MANDATORY_FUNC(create_ah),
91 IB_MANDATORY_FUNC(destroy_ah),
92 IB_MANDATORY_FUNC(create_qp),
93 IB_MANDATORY_FUNC(modify_qp),
94 IB_MANDATORY_FUNC(destroy_qp),
95 IB_MANDATORY_FUNC(post_send),
96 IB_MANDATORY_FUNC(post_recv),
97 IB_MANDATORY_FUNC(create_cq),
98 IB_MANDATORY_FUNC(destroy_cq),
99 IB_MANDATORY_FUNC(poll_cq),
100 IB_MANDATORY_FUNC(req_notify_cq),
101 IB_MANDATORY_FUNC(get_dma_mr),
102 IB_MANDATORY_FUNC(dereg_mr)
106 for (i = 0; i < MANDATORY_TABLE_DEPTH; ++i) {
107 if (!*(void **) ((void *) ((unsigned long)device + mandatory_table[i].offset))) {
108 log(LOG_WARNING, "Device %s is missing mandatory function %s\n",
109 device->name, mandatory_table[i].name);
117 static struct ib_device *__ib_device_get_by_name(const char *name)
119 struct ib_device *device;
121 TAILQ_FOREACH(device, &device_list, core_list)
122 if (!strncmp(name, device->name, IB_DEVICE_NAME_MAX))
129 static int alloc_name(char *name)
132 char buf[IB_DEVICE_NAME_MAX];
133 struct ib_device *device;
136 inuse = malloc(PAGE_SIZE, M_DEVBUF, M_NOWAIT);
140 TAILQ_FOREACH(device, &device_list, core_list) {
141 if (!sscanf(device->name, name, &i))
143 if (i < 0 || i >= PAGE_SIZE * 8)
145 snprintf(buf, sizeof buf, name, i);
146 if (!strncmp(buf, device->name, IB_DEVICE_NAME_MAX))
150 i = find_first_zero_bit(inuse, PAGE_SIZE * 8);
151 free(inuse, M_DEVBUF);
152 snprintf(buf, sizeof buf, name, i);
154 if (__ib_device_get_by_name(buf))
157 strlcpy(name, buf, IB_DEVICE_NAME_MAX);
161 static int start_port(struct ib_device *device)
163 return (device->node_type == RDMA_NODE_IB_SWITCH) ? 0 : 1;
167 static int end_port(struct ib_device *device)
169 return (device->node_type == RDMA_NODE_IB_SWITCH) ?
170 0 : device->phys_port_cnt;
174 * ib_alloc_device - allocate an IB device struct
175 * @size:size of structure to allocate
177 * Low-level drivers should use ib_alloc_device() to allocate &struct
178 * ib_device. @size is the size of the structure to be allocated,
179 * including any private data used by the low-level driver.
180 * ib_dealloc_device() must be used to free structures allocated with
183 struct ib_device *ib_alloc_device(size_t size)
187 if (size < sizeof (struct ib_device))
188 panic("size=%zd < sizeof (struct ib_device)=%zd)",
189 size, sizeof (struct ib_device));
191 dev = malloc(size, M_DEVBUF, M_NOWAIT);
198 * ib_dealloc_device - free an IB device struct
199 * @device:structure to free
201 * Free a structure allocated with ib_alloc_device().
203 void ib_dealloc_device(struct ib_device *device)
205 if (device->reg_state == IB_DEV_UNINITIALIZED) {
206 free(device, M_DEVBUF);
210 if (device->reg_state != IB_DEV_UNREGISTERED)
211 panic("device->reg_state=%d != IB_DEV_UNREGISTERED)",
214 ib_device_unregister_sysfs(device);
218 static int add_client_context(struct ib_device *device, struct ib_client *client)
220 struct ib_client_data *context;
222 context = malloc(sizeof *context, M_DEVBUF, M_NOWAIT);
224 log(LOG_WARNING, "Couldn't allocate client context for %s/%s\n",
225 device->name, client->name);
229 context->client = client;
230 context->data = NULL;
232 mtx_lock(&device->client_data_lock);
233 TAILQ_INSERT_TAIL(&device->client_data_list, context, list);
234 mtx_unlock(&device->client_data_lock);
239 static int read_port_table_lengths(struct ib_device *device)
241 struct ib_port_attr *tprops = NULL;
242 int num_ports, ret = ENOMEM;
245 tprops = malloc(sizeof *tprops, M_DEVBUF, M_NOWAIT);
249 num_ports = end_port(device) - start_port(device) + 1;
251 device->pkey_tbl_len = malloc(sizeof *device->pkey_tbl_len * num_ports,
253 device->gid_tbl_len = malloc(sizeof *device->gid_tbl_len * num_ports,
255 if (!device->pkey_tbl_len || !device->gid_tbl_len)
258 for (port_index = 0; port_index < num_ports; ++port_index) {
259 ret = ib_query_port(device, port_index + start_port(device),
263 device->pkey_tbl_len[port_index] = tprops->pkey_tbl_len;
264 device->gid_tbl_len[port_index] = tprops->gid_tbl_len;
271 free(device->gid_tbl_len, M_DEVBUF);
272 free(device->pkey_tbl_len, M_DEVBUF);
274 free(tprops, M_DEVBUF);
279 * ib_register_device - Register an IB device with IB core
280 * @device:Device to register
282 * Low-level drivers use ib_register_device() to register their
283 * devices with the IB core. All registered clients will receive a
284 * callback for each device that is added. @device must be allocated
285 * with ib_alloc_device().
287 int ib_register_device(struct ib_device *device)
291 mtx_lock(&device_mutex);
293 if (strchr(device->name, '%')) {
294 ret = alloc_name(device->name);
299 if (ib_device_check_mandatory(device)) {
304 TAILQ_INIT(&device->event_handler_list);
305 TAILQ_INIT(&device->client_data_list);
306 mtx_init(&device->event_handler_lock, "ib event handler", NULL,
308 mtx_init(&device->client_data_lock, "ib client data", NULL,
311 ret = read_port_table_lengths(device);
313 log(LOG_WARNING, "Couldn't create table lengths cache for device %s\n",
319 ret = ib_device_register_sysfs(device);
321 log(LOG_WARNING, "Couldn't register device %s with driver model\n",
323 free(device->gid_tbl_len, M_DEVBUF);
324 free(device->pkey_tbl_len, M_DEVBUF);
329 TAILQ_INSERT_TAIL(&device_list, device, core_list);
331 device->reg_state = IB_DEV_REGISTERED;
334 struct ib_client *client;
336 TAILQ_FOREACH(client, &client_list, list)
337 if (client->add && !add_client_context(device, client))
342 mtx_unlock(&device_mutex);
347 * ib_unregister_device - Unregister an IB device
348 * @device:Device to unregister
350 * Unregister an IB device. All clients will receive a remove callback.
352 void ib_unregister_device(struct ib_device *device)
354 struct ib_client *client;
355 struct ib_client_data *context, *tmp;
357 mtx_lock(&device_mutex);
359 TAILQ_FOREACH_REVERSE(client, &client_list, client_list_s, list)
361 client->remove(device);
363 TAILQ_REMOVE(&device_list, device, core_list);
365 free(device->gid_tbl_len, M_DEVBUF);
366 free(device->pkey_tbl_len, M_DEVBUF);
368 mtx_unlock(&device_mutex);
370 mtx_lock(&device->client_data_lock);
371 TAILQ_FOREACH_SAFE(context, &device->client_data_list, list, tmp)
372 free(context, M_DEVBUF);
373 mtx_unlock(&device->client_data_lock);
375 device->reg_state = IB_DEV_UNREGISTERED;
379 * ib_register_client - Register an IB client
380 * @client:Client to register
382 * Upper level users of the IB drivers can use ib_register_client() to
383 * register callbacks for IB device addition and removal. When an IB
384 * device is added, each registered client's add method will be called
385 * (in the order the clients were registered), and when a device is
386 * removed, each client's remove method will be called (in the reverse
387 * order that clients were registered). In addition, when
388 * ib_register_client() is called, the client will receive an add
389 * callback for all devices already registered.
391 int ib_register_client(struct ib_client *client)
393 struct ib_device *device;
395 mtx_lock(&device_mutex);
397 TAILQ_INSERT_TAIL(&client_list, client, list);
398 TAILQ_FOREACH(device, &device_list, core_list)
399 if (client->add && !add_client_context(device, client))
402 mtx_unlock(&device_mutex);
408 * ib_unregister_client - Unregister an IB client
409 * @client:Client to unregister
411 * Upper level users use ib_unregister_client() to remove their client
412 * registration. When ib_unregister_client() is called, the client
413 * will receive a remove callback for each IB device still registered.
415 void ib_unregister_client(struct ib_client *client)
417 struct ib_client_data *context, *tmp;
418 struct ib_device *device;
420 mtx_lock(&device_mutex);
422 TAILQ_FOREACH(device, &device_list, core_list) {
424 client->remove(device);
426 mtx_lock(&device->client_data_lock);
427 TAILQ_FOREACH_SAFE(context, &device->client_data_list, list,tmp)
428 if (context->client == client) {
429 TAILQ_REMOVE(&device->client_data_list, context,
431 free(context, M_DEVBUF);
433 mtx_unlock(&device->client_data_lock);
435 TAILQ_REMOVE(&client_list, client, list);
437 mtx_unlock(&device_mutex);
441 * ib_get_client_data - Get IB client context
442 * @device:Device to get context for
443 * @client:Client to get context for
445 * ib_get_client_data() returns client context set with
446 * ib_set_client_data().
448 void *ib_get_client_data(struct ib_device *device, struct ib_client *client)
450 struct ib_client_data *context;
453 mtx_lock(&device->client_data_lock);
454 TAILQ_FOREACH(context, &device->client_data_list, list)
455 if (context->client == client) {
459 mtx_unlock(&device->client_data_lock);
465 * ib_set_client_data - Set IB client context
466 * @device:Device to set context for
467 * @client:Client to set context for
468 * @data:Context to set
470 * ib_set_client_data() sets client context that can be retrieved with
471 * ib_get_client_data().
473 void ib_set_client_data(struct ib_device *device, struct ib_client *client,
476 struct ib_client_data *context;
478 mtx_lock(&device->client_data_lock);
479 TAILQ_FOREACH(context, &device->client_data_list, list)
480 if (context->client == client) {
481 context->data = data;
485 log(LOG_WARNING, "No client context found for %s/%s\n",
486 device->name, client->name);
489 mtx_unlock(&device->client_data_lock);
493 * ib_register_event_handler - Register an IB event handler
494 * @event_handler:Handler to register
496 * ib_register_event_handler() registers an event handler that will be
497 * called back when asynchronous IB events occur (as defined in
498 * chapter 11 of the InfiniBand Architecture Specification). This
499 * callback may occur in interrupt context.
501 int ib_register_event_handler (struct ib_event_handler *event_handler)
503 mtx_lock(&event_handler->device->event_handler_lock);
504 TAILQ_INSERT_TAIL(&event_handler->device->event_handler_list,
505 event_handler, list);
506 mtx_unlock(&event_handler->device->event_handler_lock);
512 * ib_unregister_event_handler - Unregister an event handler
513 * @event_handler:Handler to unregister
515 * Unregister an event handler registered with
516 * ib_register_event_handler().
518 int ib_unregister_event_handler(struct ib_event_handler *event_handler)
520 mtx_lock(&event_handler->device->event_handler_lock);
521 TAILQ_REMOVE(&event_handler->device->event_handler_list, event_handler,
523 mtx_unlock(&event_handler->device->event_handler_lock);
529 * ib_dispatch_event - Dispatch an asynchronous event
530 * @event:Event to dispatch
532 * Low-level drivers must call ib_dispatch_event() to dispatch the
533 * event to all registered event handlers when an asynchronous event
536 void ib_dispatch_event(struct ib_event *event)
538 struct ib_event_handler *handler;
540 mtx_lock(&event->device->event_handler_lock);
542 TAILQ_FOREACH(handler, &event->device->event_handler_list, list)
543 handler->handler(handler, event);
545 mtx_unlock(&event->device->event_handler_lock);
549 * ib_query_device - Query IB device attributes
550 * @device:Device to query
551 * @device_attr:Device attributes
553 * ib_query_device() returns the attributes of a device through the
554 * @device_attr pointer.
556 int ib_query_device(struct ib_device *device,
557 struct ib_device_attr *device_attr)
559 return device->query_device(device, device_attr);
563 * ib_query_port - Query IB port attributes
564 * @device:Device to query
565 * @port_num:Port number to query
566 * @port_attr:Port attributes
568 * ib_query_port() returns the attributes of a port through the
569 * @port_attr pointer.
571 int ib_query_port(struct ib_device *device,
573 struct ib_port_attr *port_attr)
575 if (port_num < start_port(device) || port_num > end_port(device))
578 return device->query_port(device, port_num, port_attr);
582 * ib_query_gid - Get GID table entry
583 * @device:Device to query
584 * @port_num:Port number to query
585 * @index:GID table index to query
588 * ib_query_gid() fetches the specified GID table entry.
590 int ib_query_gid(struct ib_device *device,
591 u8 port_num, int index, union ib_gid *gid)
593 return device->query_gid(device, port_num, index, gid);
597 * ib_query_pkey - Get P_Key table entry
598 * @device:Device to query
599 * @port_num:Port number to query
600 * @index:P_Key table index to query
601 * @pkey:Returned P_Key
603 * ib_query_pkey() fetches the specified P_Key table entry.
605 int ib_query_pkey(struct ib_device *device,
606 u8 port_num, u16 index, u16 *pkey)
608 return device->query_pkey(device, port_num, index, pkey);
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
617 * ib_modify_device() changes a device's attributes as specified by
618 * the @device_modify_mask and @device_modify structure.
620 int ib_modify_device(struct ib_device *device,
621 int device_modify_mask,
622 struct ib_device_modify *device_modify)
624 return device->modify_device(device, device_modify_mask,
629 * ib_modify_port - Modifies the attributes for the specified port.
630 * @device: The device to modify.
631 * @port_num: The number of the port to modify.
632 * @port_modify_mask: Mask used to specify which attributes of the port
634 * @port_modify: New attribute values for the port.
636 * ib_modify_port() changes a port's attributes as specified by the
637 * @port_modify_mask and @port_modify structure.
639 int ib_modify_port(struct ib_device *device,
640 u8 port_num, int port_modify_mask,
641 struct ib_port_modify *port_modify)
643 if (port_num < start_port(device) || port_num > end_port(device))
646 return device->modify_port(device, port_num, port_modify_mask,
651 * ib_find_gid - Returns the port number and GID table index where
652 * a specified GID value occurs.
653 * @device: The device to query.
654 * @gid: The GID value to search for.
655 * @port_num: The port number of the device where the GID value was found.
656 * @index: The index into the GID table where the GID was found. This
657 * parameter may be NULL.
659 int ib_find_gid(struct ib_device *device, union ib_gid *gid,
660 u8 *port_num, u16 *index)
662 union ib_gid tmp_gid;
665 for (port = start_port(device); port <= end_port(device); ++port) {
666 for (i = 0; i < device->gid_tbl_len[port - start_port(device)]; ++i) {
667 ret = ib_query_gid(device, port, i, &tmp_gid);
670 if (!memcmp(&tmp_gid, gid, sizeof *gid)) {
683 * ib_find_pkey - Returns the PKey table index where a specified
685 * @device: The device to query.
686 * @port_num: The port number of the device to search for the PKey.
687 * @pkey: The PKey value to search for.
688 * @index: The index into the PKey table where the PKey was found.
690 int ib_find_pkey(struct ib_device *device,
691 u8 port_num, u16 pkey, u16 *index)
696 for (i = 0; i < device->pkey_tbl_len[port_num - start_port(device)]; ++i) {
697 ret = ib_query_pkey(device, port_num, i, &tmp_pkey);
701 if (pkey == tmp_pkey) {
710 static int rdma_core_init(void)
714 ret = ib_sysfs_setup();
716 log(LOG_WARNING, "Couldn't create InfiniBand device class\n");
719 mtx_init(&device_mutex, "rdma_device mutex", NULL, MTX_DEF);
720 TAILQ_INIT(&client_list);
721 TAILQ_INIT(&device_list);
722 ret = ib_cache_setup();
724 log(LOG_WARNING, "Couldn't set up InfiniBand P_Key/GID cache\n");
733 static void rdma_core_cleanup(void)
738 /* Make sure that any pending umem accounting work is done. */
739 flush_scheduled_work();
744 rdma_core_load(module_t mod, int cmd, void *arg)
750 printf("Loading rdma_core.\n");
756 printf("Unloading rdma_core.\n");
769 static moduledata_t mod_data = {
775 MODULE_VERSION(rdma_core, 1);
776 DECLARE_MODULE(rdma_core, mod_data, SI_SUB_EXEC, SI_ORDER_ANY);