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
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)-1, name, i);
144 buf[sizeof(buf)-1] = '\0';
145 if (!strncmp(buf, device->name, IB_DEVICE_NAME_MAX))
149 i = find_first_zero_bit((const unsigned long *)inuse, PAGE_SIZE * 8);
151 snprintf(buf, sizeof(buf)-1, name, i);
152 buf[sizeof(buf)-1] = '\0';
154 if (__ib_device_get_by_name(buf))
157 strlcpy(name, buf, IB_DEVICE_NAME_MAX);
161 static int add_client_context(struct ib_device *device, struct ib_client *client)
163 struct ib_client_data *context;
166 context = kmalloc(sizeof *context, GFP_KERNEL);
168 HCA_PRINT(TRACE_LEVEL_WARNING ,HCA_DBG_LOW,("Couldn't allocate client context for %s/%s\n",
169 device->name, client->name));
173 context->client = client;
174 context->data = NULL;
176 spin_lock_irqsave(&device->client_data_lock, &lh);
177 list_add(&context->list, &device->client_data_list);
178 spin_unlock_irqrestore(&lh);
184 * ib_register_device - Register an IB device with IB core
185 * @device:Device to register
187 * Low-level drivers use ib_register_device() to register their
188 * devices with the IB core. All registered clients will receive a
189 * callback for each device that is added. @device must be allocated
190 * with ib_alloc_device().
192 int ib_register_device(struct ib_device *device)
198 if (strchr(device->name, '%')) {
199 ret = alloc_name(device->name);
204 if (ib_device_check_mandatory(device)) {
209 INIT_LIST_HEAD(&device->event_handler_list);
210 INIT_LIST_HEAD(&device->client_data_list);
211 spin_lock_init(&device->event_handler_lock);
212 spin_lock_init(&device->client_data_lock);
214 list_add_tail(&device->core_list, &device_list);
217 struct ib_client *client;
219 list_for_each_entry(client, &client_list, list,struct ib_client)
220 if (client->add && !add_client_context(device, client))
231 * ib_unregister_device - Unregister an IB device
232 * @device:Device to unregister
234 * Unregister an IB device. All clients will receive a remove callback.
236 void ib_unregister_device(struct ib_device *device)
238 struct ib_client *client;
239 struct ib_client_data *context, *tmp;
244 list_for_each_entry_reverse(client, &client_list, list,struct ib_client)
246 client->remove(device);
248 list_del(&device->core_list);
252 spin_lock_irqsave(&device->client_data_lock, &lh);
253 list_for_each_entry_safe(context, tmp, &device->client_data_list, list,struct ib_client_data,struct ib_client_data)
255 spin_unlock_irqrestore(&lh);
261 * ib_register_client - Register an IB client
262 * @client:Client to register
264 * Upper level users of the IB drivers can use ib_register_client() to
265 * register callbacks for IB device addition and removal. When an IB
266 * device is added, each registered client's add method will be called
267 * (in the order the clients were registered), and when a device is
268 * removed, each client's remove method will be called (in the reverse
269 * order that clients were registered). In addition, when
270 * ib_register_client() is called, the client will receive an add
271 * callback for all devices already registered.
273 int ib_register_client(struct ib_client *client)
275 struct ib_device *device;
279 list_add_tail(&client->list, &client_list);
280 list_for_each_entry(device, &device_list, core_list,struct ib_device)
281 if (client->add && !add_client_context(device, client))
291 * ib_unregister_client - Unregister an IB client
292 * @client:Client to unregister
294 * Upper level users use ib_unregister_client() to remove their client
295 * registration. When ib_unregister_client() is called, the client
296 * will receive a remove callback for each IB device still registered.
298 void ib_unregister_client(struct ib_client *client)
300 struct ib_client_data *context, *tmp;
301 struct ib_device *device;
306 list_for_each_entry(device, &device_list, core_list,struct ib_device) {
308 client->remove(device);
310 spin_lock_irqsave(&device->client_data_lock, &lh);
311 list_for_each_entry_safe(context, tmp, &device->client_data_list, list,struct ib_client_data,struct ib_client_data)
312 if (context->client == client) {
313 list_del(&context->list);
316 spin_unlock_irqrestore(&lh);
318 list_del(&client->list);
325 * ib_get_client_data - Get IB client context
326 * @device:Device to get context for
327 * @client:Client to get context for
329 * ib_get_client_data() returns client context set with
330 * ib_set_client_data().
332 void *ib_get_client_data(struct ib_device *device, struct ib_client *client)
334 struct ib_client_data *context;
338 spin_lock_irqsave(&device->client_data_lock, &lh);
339 list_for_each_entry(context, &device->client_data_list, list,struct ib_client_data)
340 if (context->client == client) {
344 spin_unlock_irqrestore(&lh);
351 * ib_set_client_data - Get IB client context
352 * @device:Device to set context for
353 * @client:Client to set context for
354 * @data:Context to set
356 * ib_set_client_data() sets client context that can be retrieved with
357 * ib_get_client_data().
359 void ib_set_client_data(struct ib_device *device, struct ib_client *client,
362 struct ib_client_data *context;
365 spin_lock_irqsave(&device->client_data_lock, &lh);
366 list_for_each_entry(context, &device->client_data_list, list,struct ib_client_data)
367 if (context->client == client) {
368 context->data = data;
372 HCA_PRINT(TRACE_LEVEL_WARNING ,HCA_DBG_LOW ,("No client context found for %s/%s\n",
373 device->name, client->name));
376 spin_unlock_irqrestore(&lh);
381 * ib_register_event_handler - Register an IB event handler
382 * @event_handler:Handler to register
384 * ib_register_event_handler() registers an event handler that will be
385 * called back when asynchronous IB events occur (as defined in
386 * chapter 11 of the InfiniBand Architecture Specification). This
387 * callback may occur in interrupt context.
389 int ib_register_event_handler (struct ib_event_handler *event_handler)
393 spin_lock_irqsave(&event_handler->device->event_handler_lock, &lh);
394 list_add_tail(&event_handler->list,
395 &event_handler->device->event_handler_list);
396 spin_unlock_irqrestore(&lh);
403 * ib_unregister_event_handler - Unregister an event handler
404 * @event_handler:Handler to unregister
406 * Unregister an event handler registered with
407 * ib_register_event_handler().
409 int ib_unregister_event_handler(struct ib_event_handler *event_handler)
412 spin_lock_irqsave(&event_handler->device->event_handler_lock, &lh);
413 list_del(&event_handler->list);
414 spin_unlock_irqrestore(&lh);
421 * ib_dispatch_event - Dispatch an asynchronous event
422 * @event:Event to dispatch
424 * Low-level drivers must call ib_dispatch_event() to dispatch the
425 * event to all registered event handlers when an asynchronous event
428 void ib_dispatch_event(struct ib_event *event)
430 struct ib_event_handler *handler;
433 spin_lock_irqsave(&event->device->event_handler_lock, &lh);
435 list_for_each_entry(handler, &event->device->event_handler_list, list,struct ib_event_handler)
436 handler->handler(handler, event);
438 spin_unlock_irqrestore(&lh);
443 * ib_query_device - Query IB device attributes
444 * @device:Device to query
445 * @device_attr:Device attributes
447 * ib_query_device() returns the attributes of a device through the
448 * @device_attr pointer.
450 int ib_query_device(struct ib_device *device,
451 struct ib_device_attr *device_attr)
453 return device->query_device(device, device_attr);
458 * ib_query_port - Query IB port attributes
459 * @device:Device to query
460 * @port_num:Port number to query
461 * @port_attr:Port attributes
463 * ib_query_port() returns the attributes of a port through the
464 * @port_attr pointer.
466 int ib_query_port(struct ib_device *device,
468 struct ib_port_attr *port_attr)
470 if (port_num < start_port(device) || port_num > end_port(device))
472 return device->query_port(device, port_num, port_attr);
477 * ib_query_gid_chunk - Get a chunk of GID table entries
478 * @device:Device to query
479 * @port_num:Port number to query
480 * @index:GID table index to query
481 * @gid:Returned GIDs chunk
483 * ib_query_gid_chunk() fetches the specified GID table enties chunk.
485 int ib_query_gid_chunk(struct ib_device *device,
486 u8 port_num, int index, union ib_gid gid[8])
488 return device->query_gid_chunk(device, port_num, index, gid);
493 * ib_query_pkey_chunk - Get a chunk of P_Key table entries
494 * @device:Device to query
495 * @port_num:Port number to query
496 * @index:P_Key table index to query
497 * @pkey:Returned P_Keys chunk
499 * ib_query_pkey_chunk() fetches the specified P_Key table entries chunk.
501 int ib_query_pkey_chunk(struct ib_device *device,
502 u8 port_num, u16 index, u16 pkey[32])
504 return device->query_pkey_chunk(device, port_num, index, pkey);
509 * ib_modify_device - Change IB device attributes
510 * @device:Device to modify
511 * @device_modify_mask:Mask of attributes to change
512 * @device_modify:New attribute values
514 * ib_modify_device() changes a device's attributes as specified by
515 * the @device_modify_mask and @device_modify structure.
517 int ib_modify_device(struct ib_device *device,
518 int device_modify_mask,
519 struct ib_device_modify *device_modify)
521 return device->modify_device(device, device_modify_mask,
527 * ib_modify_port - Modifies the attributes for the specified port.
528 * @device: The device to modify.
529 * @port_num: The number of the port to modify.
530 * @port_modify_mask: Mask used to specify which attributes of the port
532 * @port_modify: New attribute values for the port.
534 * ib_modify_port() changes a port's attributes as specified by the
535 * @port_modify_mask and @port_modify structure.
537 int ib_modify_port(struct ib_device *device,
538 u8 port_num, int port_modify_mask,
539 struct ib_port_modify *port_modify)
541 if (port_num < start_port(device) || port_num > end_port(device))
544 return device->modify_port(device, port_num, port_modify_mask,
548 int ib_core_init(void)
552 /* leo: added because there is no static init of semaphore in Windows */
553 KeInitializeMutex(&device_mutex,0);
555 ret = ib_cache_setup();
557 HCA_PRINT(TRACE_LEVEL_WARNING ,HCA_DBG_LOW ,("Couldn't set up InfiniBand P_Key/GID cache\n"));
563 void ib_core_cleanup(void)