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 if (RtlStringCbPrintfA(buf, sizeof buf, name, i))
146 if (!strncmp(buf, device->name, IB_DEVICE_NAME_MAX))
150 i = find_first_zero_bit((const unsigned long *)inuse, PAGE_SIZE * 8);
152 if (RtlStringCbPrintfA(buf, sizeof buf, name, i))
156 if (__ib_device_get_by_name(buf))
159 strlcpy(name, buf, IB_DEVICE_NAME_MAX);
163 static int add_client_context(struct ib_device *device, struct ib_client *client)
165 struct ib_client_data *context;
168 context = kmalloc(sizeof *context, GFP_KERNEL);
170 HCA_PRINT(TRACE_LEVEL_WARNING ,HCA_DBG_LOW,("Couldn't allocate client context for %s/%s\n",
171 device->name, client->name));
175 context->client = client;
176 context->data = NULL;
178 spin_lock_irqsave(&device->client_data_lock, &lh);
179 list_add(&context->list, &device->client_data_list);
180 spin_unlock_irqrestore(&lh);
186 * ib_register_device - Register an IB device with IB core
187 * @device:Device to register
189 * Low-level drivers use ib_register_device() to register their
190 * devices with the IB core. All registered clients will receive a
191 * callback for each device that is added. @device must be allocated
192 * with ib_alloc_device().
194 int ib_register_device(struct ib_device *device)
200 if (strchr(device->name, '%')) {
201 ret = alloc_name(device->name);
206 if (ib_device_check_mandatory(device)) {
211 INIT_LIST_HEAD(&device->event_handler_list);
212 INIT_LIST_HEAD(&device->client_data_list);
213 spin_lock_init(&device->event_handler_lock);
214 spin_lock_init(&device->client_data_lock);
216 list_add_tail(&device->core_list, &device_list);
219 struct ib_client *client;
221 list_for_each_entry(client, &client_list, list,struct ib_client)
222 if (client->add && !add_client_context(device, client))
233 * ib_unregister_device - Unregister an IB device
234 * @device:Device to unregister
236 * Unregister an IB device. All clients will receive a remove callback.
238 void ib_unregister_device(struct ib_device *device)
240 struct ib_client *client;
241 struct ib_client_data *context, *tmp;
246 list_for_each_entry_reverse(client, &client_list, list,struct ib_client)
248 client->remove(device);
250 list_del(&device->core_list);
254 spin_lock_irqsave(&device->client_data_lock, &lh);
255 list_for_each_entry_safe(context, tmp, &device->client_data_list, list,struct ib_client_data,struct ib_client_data)
257 spin_unlock_irqrestore(&lh);
263 * ib_register_client - Register an IB client
264 * @client:Client to register
266 * Upper level users of the IB drivers can use ib_register_client() to
267 * register callbacks for IB device addition and removal. When an IB
268 * device is added, each registered client's add method will be called
269 * (in the order the clients were registered), and when a device is
270 * removed, each client's remove method will be called (in the reverse
271 * order that clients were registered). In addition, when
272 * ib_register_client() is called, the client will receive an add
273 * callback for all devices already registered.
275 int ib_register_client(struct ib_client *client)
277 struct ib_device *device;
281 list_add_tail(&client->list, &client_list);
282 list_for_each_entry(device, &device_list, core_list,struct ib_device)
283 if (client->add && !add_client_context(device, client))
293 * ib_unregister_client - Unregister an IB client
294 * @client:Client to unregister
296 * Upper level users use ib_unregister_client() to remove their client
297 * registration. When ib_unregister_client() is called, the client
298 * will receive a remove callback for each IB device still registered.
300 void ib_unregister_client(struct ib_client *client)
302 struct ib_client_data *context, *tmp;
303 struct ib_device *device;
308 list_for_each_entry(device, &device_list, core_list,struct ib_device) {
310 client->remove(device);
312 spin_lock_irqsave(&device->client_data_lock, &lh);
313 list_for_each_entry_safe(context, tmp, &device->client_data_list, list,struct ib_client_data,struct ib_client_data)
314 if (context->client == client) {
315 list_del(&context->list);
318 spin_unlock_irqrestore(&lh);
320 list_del(&client->list);
327 * ib_get_client_data - Get IB client context
328 * @device:Device to get context for
329 * @client:Client to get context for
331 * ib_get_client_data() returns client context set with
332 * ib_set_client_data().
334 void *ib_get_client_data(struct ib_device *device, struct ib_client *client)
336 struct ib_client_data *context;
340 spin_lock_irqsave(&device->client_data_lock, &lh);
341 list_for_each_entry(context, &device->client_data_list, list,struct ib_client_data)
342 if (context->client == client) {
346 spin_unlock_irqrestore(&lh);
353 * ib_set_client_data - Get IB client context
354 * @device:Device to set context for
355 * @client:Client to set context for
356 * @data:Context to set
358 * ib_set_client_data() sets client context that can be retrieved with
359 * ib_get_client_data().
361 void ib_set_client_data(struct ib_device *device, struct ib_client *client,
364 struct ib_client_data *context;
367 spin_lock_irqsave(&device->client_data_lock, &lh);
368 list_for_each_entry(context, &device->client_data_list, list,struct ib_client_data)
369 if (context->client == client) {
370 context->data = data;
374 HCA_PRINT(TRACE_LEVEL_WARNING ,HCA_DBG_LOW ,("No client context found for %s/%s\n",
375 device->name, client->name));
378 spin_unlock_irqrestore(&lh);
383 * ib_register_event_handler - Register an IB event handler
384 * @event_handler:Handler to register
386 * ib_register_event_handler() registers an event handler that will be
387 * called back when asynchronous IB events occur (as defined in
388 * chapter 11 of the InfiniBand Architecture Specification). This
389 * callback may occur in interrupt context.
391 int ib_register_event_handler (struct ib_event_handler *event_handler)
395 spin_lock_irqsave(&event_handler->device->event_handler_lock, &lh);
396 list_add_tail(&event_handler->list,
397 &event_handler->device->event_handler_list);
398 spin_unlock_irqrestore(&lh);
405 * ib_unregister_event_handler - Unregister an event handler
406 * @event_handler:Handler to unregister
408 * Unregister an event handler registered with
409 * ib_register_event_handler().
411 int ib_unregister_event_handler(struct ib_event_handler *event_handler)
414 spin_lock_irqsave(&event_handler->device->event_handler_lock, &lh);
415 list_del(&event_handler->list);
416 spin_unlock_irqrestore(&lh);
423 * ib_dispatch_event - Dispatch an asynchronous event
424 * @event:Event to dispatch
426 * Low-level drivers must call ib_dispatch_event() to dispatch the
427 * event to all registered event handlers when an asynchronous event
430 void ib_dispatch_event(struct ib_event *event)
432 struct ib_event_handler *handler;
435 spin_lock_irqsave(&event->device->event_handler_lock, &lh);
437 list_for_each_entry(handler, &event->device->event_handler_list, list,struct ib_event_handler)
438 handler->handler(handler, event);
440 spin_unlock_irqrestore(&lh);
445 * ib_query_device - Query IB device attributes
446 * @device:Device to query
447 * @device_attr:Device attributes
449 * ib_query_device() returns the attributes of a device through the
450 * @device_attr pointer.
452 int ib_query_device(struct ib_device *device,
453 struct ib_device_attr *device_attr)
455 return device->query_device(device, device_attr);
460 * ib_query_port - Query IB port attributes
461 * @device:Device to query
462 * @port_num:Port number to query
463 * @port_attr:Port attributes
465 * ib_query_port() returns the attributes of a port through the
466 * @port_attr pointer.
468 int ib_query_port(struct ib_device *device,
470 struct ib_port_attr *port_attr)
472 if (port_num < start_port(device) || port_num > end_port(device))
474 return device->query_port(device, port_num, port_attr);
479 * ib_query_gid_chunk - Get a chunk of GID table entries
480 * @device:Device to query
481 * @port_num:Port number to query
482 * @index:GID table index to query
483 * @gid:Returned GIDs chunk
485 * ib_query_gid_chunk() fetches the specified GID table enties chunk.
487 int ib_query_gid_chunk(struct ib_device *device,
488 u8 port_num, int index, union ib_gid gid[8])
490 return device->query_gid_chunk(device, port_num, index, gid);
495 * ib_query_pkey_chunk - Get a chunk of P_Key table entries
496 * @device:Device to query
497 * @port_num:Port number to query
498 * @index:P_Key table index to query
499 * @pkey:Returned P_Keys chunk
501 * ib_query_pkey_chunk() fetches the specified P_Key table entries chunk.
503 int ib_query_pkey_chunk(struct ib_device *device,
504 u8 port_num, u16 index, __be16 pkey[32])
506 return device->query_pkey_chunk(device, port_num, index, pkey);
511 * ib_modify_device - Change IB device attributes
512 * @device:Device to modify
513 * @device_modify_mask:Mask of attributes to change
514 * @device_modify:New attribute values
516 * ib_modify_device() changes a device's attributes as specified by
517 * the @device_modify_mask and @device_modify structure.
519 int ib_modify_device(struct ib_device *device,
520 int device_modify_mask,
521 struct ib_device_modify *device_modify)
523 return device->modify_device(device, device_modify_mask,
529 * ib_modify_port - Modifies the attributes for the specified port.
530 * @device: The device to modify.
531 * @port_num: The number of the port to modify.
532 * @port_modify_mask: Mask used to specify which attributes of the port
534 * @port_modify: New attribute values for the port.
536 * ib_modify_port() changes a port's attributes as specified by the
537 * @port_modify_mask and @port_modify structure.
539 int ib_modify_port(struct ib_device *device,
540 u8 port_num, int port_modify_mask,
541 struct ib_port_modify *port_modify)
543 if (port_num < start_port(device) || port_num > end_port(device))
546 return device->modify_port(device, port_num, port_modify_mask,
550 int ib_core_init(void)
554 /* leo: added because there is no static init of semaphore in Windows */
555 KeInitializeMutex(&device_mutex,0);
557 ret = ib_cache_setup();
559 HCA_PRINT(TRACE_LEVEL_WARNING ,HCA_DBG_LOW ,("Couldn't set up InfiniBand P_Key/GID cache\n"));
565 void ib_core_cleanup(void)