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 2730 2005-06-28 16:43:03Z sean.hefty $
36 #include "hca_driver.h"
37 #if defined(EVENT_TRACING)
41 #include "mt_device.tmh"
46 struct ib_client_data {
47 struct list_head list;
48 struct ib_client *client;
52 static LIST_HEAD(device_list);
53 static LIST_HEAD(client_list);
56 * device_mutex protects access to both device_list and client_list.
57 * There's no real point to using multiple locks or something fancier
58 * like an rwsem: we always access both lists, and we're always
59 * modifying one list or the other list. In any case this is not a
60 * hot path so there's no point in trying to optimize.
64 static int ib_device_check_mandatory(struct ib_device *device)
66 #define IB_MANDATORY_FUNC(x) { offsetof(struct ib_device, x), #x }
70 } mandatory_table[] = {
71 IB_MANDATORY_FUNC(query_device),
72 IB_MANDATORY_FUNC(query_port),
73 IB_MANDATORY_FUNC(query_pkey_chunk),
74 IB_MANDATORY_FUNC(query_gid_chunk),
75 IB_MANDATORY_FUNC(alloc_pd),
76 IB_MANDATORY_FUNC(dealloc_pd),
77 IB_MANDATORY_FUNC(create_ah),
78 IB_MANDATORY_FUNC(destroy_ah),
79 IB_MANDATORY_FUNC(create_qp),
80 IB_MANDATORY_FUNC(modify_qp),
81 IB_MANDATORY_FUNC(destroy_qp),
82 IB_MANDATORY_FUNC(post_send),
83 IB_MANDATORY_FUNC(post_recv),
84 IB_MANDATORY_FUNC(create_cq),
85 IB_MANDATORY_FUNC(destroy_cq),
86 IB_MANDATORY_FUNC(poll_cq),
87 IB_MANDATORY_FUNC(req_notify_cq),
88 IB_MANDATORY_FUNC(get_dma_mr),
89 IB_MANDATORY_FUNC(dereg_mr)
93 for (i = 0; i < sizeof mandatory_table / sizeof mandatory_table[0]; ++i) {
94 if (!*(void **) ((u8 *) device + mandatory_table[i].offset)) {
95 HCA_PRINT(TRACE_LEVEL_WARNING ,HCA_DBG_LOW,("Device %s is missing mandatory function %s\n",
96 device->name, mandatory_table[i].name));
104 static struct ib_device *__ib_device_get_by_name(const char *name)
106 struct ib_device *device;
108 list_for_each_entry(device, &device_list, core_list,struct ib_device)
109 if (!strncmp(name, device->name, IB_DEVICE_NAME_MAX))
115 static int __extract_number(char *dest_str, const char *format, int *num)
118 UNREFERENCED_PARAMETER(format);
119 for (ptr = dest_str; *ptr; ptr++) {
120 if (*ptr >= '0' && *ptr <= '9') {
127 static int alloc_name(char *name)
130 char buf[IB_DEVICE_NAME_MAX];
131 struct ib_device *device;
134 inuse = (long *) get_zeroed_page(GFP_KERNEL);
138 list_for_each_entry(device, &device_list, core_list,struct ib_device) {
139 if (!__extract_number(device->name, name, &i))
141 if (i < 0 || i >= PAGE_SIZE * 8)
143 snprintf(buf, sizeof buf, name, i);
144 if (!strncmp(buf, device->name, IB_DEVICE_NAME_MAX))
148 i = find_first_zero_bit((const unsigned long *)inuse, PAGE_SIZE * 8);
150 snprintf(buf, sizeof buf, name, i);
152 if (__ib_device_get_by_name(buf))
155 strlcpy(name, buf, IB_DEVICE_NAME_MAX);
159 static int add_client_context(struct ib_device *device, struct ib_client *client)
161 struct ib_client_data *context;
164 context = kmalloc(sizeof *context, GFP_KERNEL);
166 HCA_PRINT(TRACE_LEVEL_WARNING ,HCA_DBG_LOW,("Couldn't allocate client context for %s/%s\n",
167 device->name, client->name));
171 context->client = client;
172 context->data = NULL;
174 spin_lock_irqsave(&device->client_data_lock, &lh);
175 list_add(&context->list, &device->client_data_list);
176 spin_unlock_irqrestore(&lh);
182 * ib_register_device - Register an IB device with IB core
183 * @device:Device to register
185 * Low-level drivers use ib_register_device() to register their
186 * devices with the IB core. All registered clients will receive a
187 * callback for each device that is added. @device must be allocated
188 * with ib_alloc_device().
190 int ib_register_device(struct ib_device *device)
196 if (strchr(device->name, '%')) {
197 ret = alloc_name(device->name);
202 if (ib_device_check_mandatory(device)) {
207 INIT_LIST_HEAD(&device->event_handler_list);
208 INIT_LIST_HEAD(&device->client_data_list);
209 spin_lock_init(&device->event_handler_lock);
210 spin_lock_init(&device->client_data_lock);
212 list_add_tail(&device->core_list, &device_list);
215 struct ib_client *client;
217 list_for_each_entry(client, &client_list, list,struct ib_client)
218 if (client->add && !add_client_context(device, client))
229 * ib_unregister_device - Unregister an IB device
230 * @device:Device to unregister
232 * Unregister an IB device. All clients will receive a remove callback.
234 void ib_unregister_device(struct ib_device *device)
236 struct ib_client *client;
237 struct ib_client_data *context, *tmp;
242 list_for_each_entry_reverse(client, &client_list, list,struct ib_client)
244 client->remove(device);
246 list_del(&device->core_list);
250 spin_lock_irqsave(&device->client_data_lock, &lh);
251 list_for_each_entry_safe(context, tmp, &device->client_data_list, list,struct ib_client_data,struct ib_client_data)
253 spin_unlock_irqrestore(&lh);
259 * ib_register_client - Register an IB client
260 * @client:Client to register
262 * Upper level users of the IB drivers can use ib_register_client() to
263 * register callbacks for IB device addition and removal. When an IB
264 * device is added, each registered client's add method will be called
265 * (in the order the clients were registered), and when a device is
266 * removed, each client's remove method will be called (in the reverse
267 * order that clients were registered). In addition, when
268 * ib_register_client() is called, the client will receive an add
269 * callback for all devices already registered.
271 int ib_register_client(struct ib_client *client)
273 struct ib_device *device;
277 list_add_tail(&client->list, &client_list);
278 list_for_each_entry(device, &device_list, core_list,struct ib_device)
279 if (client->add && !add_client_context(device, client))
289 * ib_unregister_client - Unregister an IB client
290 * @client:Client to unregister
292 * Upper level users use ib_unregister_client() to remove their client
293 * registration. When ib_unregister_client() is called, the client
294 * will receive a remove callback for each IB device still registered.
296 void ib_unregister_client(struct ib_client *client)
298 struct ib_client_data *context, *tmp;
299 struct ib_device *device;
304 list_for_each_entry(device, &device_list, core_list,struct ib_device) {
306 client->remove(device);
308 spin_lock_irqsave(&device->client_data_lock, &lh);
309 list_for_each_entry_safe(context, tmp, &device->client_data_list, list,struct ib_client_data,struct ib_client_data)
310 if (context->client == client) {
311 list_del(&context->list);
314 spin_unlock_irqrestore(&lh);
316 list_del(&client->list);
323 * ib_get_client_data - Get IB client context
324 * @device:Device to get context for
325 * @client:Client to get context for
327 * ib_get_client_data() returns client context set with
328 * ib_set_client_data().
330 void *ib_get_client_data(struct ib_device *device, struct ib_client *client)
332 struct ib_client_data *context;
336 spin_lock_irqsave(&device->client_data_lock, &lh);
337 list_for_each_entry(context, &device->client_data_list, list,struct ib_client_data)
338 if (context->client == client) {
342 spin_unlock_irqrestore(&lh);
349 * ib_set_client_data - Get IB client context
350 * @device:Device to set context for
351 * @client:Client to set context for
352 * @data:Context to set
354 * ib_set_client_data() sets client context that can be retrieved with
355 * ib_get_client_data().
357 void ib_set_client_data(struct ib_device *device, struct ib_client *client,
360 struct ib_client_data *context;
363 spin_lock_irqsave(&device->client_data_lock, &lh);
364 list_for_each_entry(context, &device->client_data_list, list,struct ib_client_data)
365 if (context->client == client) {
366 context->data = data;
370 HCA_PRINT(TRACE_LEVEL_WARNING ,HCA_DBG_LOW ,("No client context found for %s/%s\n",
371 device->name, client->name));
374 spin_unlock_irqrestore(&lh);
379 * ib_register_event_handler - Register an IB event handler
380 * @event_handler:Handler to register
382 * ib_register_event_handler() registers an event handler that will be
383 * called back when asynchronous IB events occur (as defined in
384 * chapter 11 of the InfiniBand Architecture Specification). This
385 * callback may occur in interrupt context.
387 int ib_register_event_handler (struct ib_event_handler *event_handler)
391 spin_lock_irqsave(&event_handler->device->event_handler_lock, &lh);
392 list_add_tail(&event_handler->list,
393 &event_handler->device->event_handler_list);
394 spin_unlock_irqrestore(&lh);
401 * ib_unregister_event_handler - Unregister an event handler
402 * @event_handler:Handler to unregister
404 * Unregister an event handler registered with
405 * ib_register_event_handler().
407 int ib_unregister_event_handler(struct ib_event_handler *event_handler)
410 spin_lock_irqsave(&event_handler->device->event_handler_lock, &lh);
411 list_del(&event_handler->list);
412 spin_unlock_irqrestore(&lh);
419 * ib_dispatch_event - Dispatch an asynchronous event
420 * @event:Event to dispatch
422 * Low-level drivers must call ib_dispatch_event() to dispatch the
423 * event to all registered event handlers when an asynchronous event
426 void ib_dispatch_event(struct ib_event *event)
428 struct ib_event_handler *handler;
431 spin_lock_irqsave(&event->device->event_handler_lock, &lh);
433 list_for_each_entry(handler, &event->device->event_handler_list, list,struct ib_event_handler)
434 handler->handler(handler, event);
436 spin_unlock_irqrestore(&lh);
441 * ib_query_device - Query IB device attributes
442 * @device:Device to query
443 * @device_attr:Device attributes
445 * ib_query_device() returns the attributes of a device through the
446 * @device_attr pointer.
448 int ib_query_device(struct ib_device *device,
449 struct ib_device_attr *device_attr)
451 return device->query_device(device, device_attr);
456 * ib_query_port - Query IB port attributes
457 * @device:Device to query
458 * @port_num:Port number to query
459 * @port_attr:Port attributes
461 * ib_query_port() returns the attributes of a port through the
462 * @port_attr pointer.
464 int ib_query_port(struct ib_device *device,
466 struct ib_port_attr *port_attr)
468 if (port_num < start_port(device) || port_num > end_port(device))
470 return device->query_port(device, port_num, port_attr);
475 * ib_query_gid_chunk - Get a chunk of GID table entries
476 * @device:Device to query
477 * @port_num:Port number to query
478 * @index:GID table index to query
479 * @gid:Returned GIDs chunk
481 * ib_query_gid_chunk() fetches the specified GID table enties chunk.
483 int ib_query_gid_chunk(struct ib_device *device,
484 u8 port_num, int index, union ib_gid gid[8])
486 return device->query_gid_chunk(device, port_num, index, gid);
491 * ib_query_pkey_chunk - Get a chunk of P_Key table entries
492 * @device:Device to query
493 * @port_num:Port number to query
494 * @index:P_Key table index to query
495 * @pkey:Returned P_Keys chunk
497 * ib_query_pkey_chunk() fetches the specified P_Key table entries chunk.
499 int ib_query_pkey_chunk(struct ib_device *device,
500 u8 port_num, u16 index, u16 pkey[32])
502 return device->query_pkey_chunk(device, port_num, index, pkey);
507 * ib_modify_device - Change IB device attributes
508 * @device:Device to modify
509 * @device_modify_mask:Mask of attributes to change
510 * @device_modify:New attribute values
512 * ib_modify_device() changes a device's attributes as specified by
513 * the @device_modify_mask and @device_modify structure.
515 int ib_modify_device(struct ib_device *device,
516 int device_modify_mask,
517 struct ib_device_modify *device_modify)
519 return device->modify_device(device, device_modify_mask,
525 * ib_modify_port - Modifies the attributes for the specified port.
526 * @device: The device to modify.
527 * @port_num: The number of the port to modify.
528 * @port_modify_mask: Mask used to specify which attributes of the port
530 * @port_modify: New attribute values for the port.
532 * ib_modify_port() changes a port's attributes as specified by the
533 * @port_modify_mask and @port_modify structure.
535 int ib_modify_port(struct ib_device *device,
536 u8 port_num, int port_modify_mask,
537 struct ib_port_modify *port_modify)
539 if (port_num < start_port(device) || port_num > end_port(device))
542 return device->modify_port(device, port_num, port_modify_mask,
546 int ib_core_init(void)
550 /* leo: added because there is no static init of semaphore in Windows */
551 KeInitializeMutex(&device_mutex,0);
553 ret = ib_cache_setup();
555 HCA_PRINT(TRACE_LEVEL_WARNING ,HCA_DBG_LOW ,("Couldn't set up InfiniBand P_Key/GID cache\n"));
561 void ib_core_cleanup(void)