device.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  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. #include <linux/module.h>
  34. #include <linux/string.h>
  35. #include <linux/errno.h>
  36. #include <linux/kernel.h>
  37. #include <linux/slab.h>
  38. #include <linux/init.h>
  39. #include <linux/mutex.h>
  40. #include <linux/workqueue.h>
  41. #include "core_priv.h"
  42. MODULE_AUTHOR("Roland Dreier");
  43. MODULE_DESCRIPTION("core kernel InfiniBand API");
  44. MODULE_LICENSE("Dual BSD/GPL");
  45. struct ib_client_data {
  46. struct list_head list;
  47. struct ib_client *client;
  48. void * data;
  49. };
  50. static LIST_HEAD(device_list);
  51. static LIST_HEAD(client_list);
  52. /*
  53. * device_mutex protects access to both device_list and client_list.
  54. * There's no real point to using multiple locks or something fancier
  55. * like an rwsem: we always access both lists, and we're always
  56. * modifying one list or the other list. In any case this is not a
  57. * hot path so there's no point in trying to optimize.
  58. */
  59. static DEFINE_MUTEX(device_mutex);
  60. static int ib_device_check_mandatory(struct ib_device *device)
  61. {
  62. #define IB_MANDATORY_FUNC(x) { offsetof(struct ib_device, x), #x }
  63. static const struct {
  64. size_t offset;
  65. char *name;
  66. } mandatory_table[] = {
  67. IB_MANDATORY_FUNC(query_device),
  68. IB_MANDATORY_FUNC(query_port),
  69. IB_MANDATORY_FUNC(query_pkey),
  70. IB_MANDATORY_FUNC(query_gid),
  71. IB_MANDATORY_FUNC(alloc_pd),
  72. IB_MANDATORY_FUNC(dealloc_pd),
  73. IB_MANDATORY_FUNC(create_ah),
  74. IB_MANDATORY_FUNC(destroy_ah),
  75. IB_MANDATORY_FUNC(create_qp),
  76. IB_MANDATORY_FUNC(modify_qp),
  77. IB_MANDATORY_FUNC(destroy_qp),
  78. IB_MANDATORY_FUNC(post_send),
  79. IB_MANDATORY_FUNC(post_recv),
  80. IB_MANDATORY_FUNC(create_cq),
  81. IB_MANDATORY_FUNC(destroy_cq),
  82. IB_MANDATORY_FUNC(poll_cq),
  83. IB_MANDATORY_FUNC(req_notify_cq),
  84. IB_MANDATORY_FUNC(get_dma_mr),
  85. IB_MANDATORY_FUNC(dereg_mr)
  86. };
  87. int i;
  88. for (i = 0; i < ARRAY_SIZE(mandatory_table); ++i) {
  89. if (!*(void **) ((void *) device + mandatory_table[i].offset)) {
  90. printk(KERN_WARNING "Device %s is missing mandatory function %s\n",
  91. device->name, mandatory_table[i].name);
  92. return -EINVAL;
  93. }
  94. }
  95. return 0;
  96. }
  97. static struct ib_device *__ib_device_get_by_name(const char *name)
  98. {
  99. struct ib_device *device;
  100. list_for_each_entry(device, &device_list, core_list)
  101. if (!strncmp(name, device->name, IB_DEVICE_NAME_MAX))
  102. return device;
  103. return NULL;
  104. }
  105. static int alloc_name(char *name)
  106. {
  107. unsigned long *inuse;
  108. char buf[IB_DEVICE_NAME_MAX];
  109. struct ib_device *device;
  110. int i;
  111. inuse = (unsigned long *) get_zeroed_page(GFP_KERNEL);
  112. if (!inuse)
  113. return -ENOMEM;
  114. list_for_each_entry(device, &device_list, core_list) {
  115. if (!sscanf(device->name, name, &i))
  116. continue;
  117. if (i < 0 || i >= PAGE_SIZE * 8)
  118. continue;
  119. snprintf(buf, sizeof buf, name, i);
  120. if (!strncmp(buf, device->name, IB_DEVICE_NAME_MAX))
  121. set_bit(i, inuse);
  122. }
  123. i = find_first_zero_bit(inuse, PAGE_SIZE * 8);
  124. free_page((unsigned long) inuse);
  125. snprintf(buf, sizeof buf, name, i);
  126. if (__ib_device_get_by_name(buf))
  127. return -ENFILE;
  128. strlcpy(name, buf, IB_DEVICE_NAME_MAX);
  129. return 0;
  130. }
  131. static int start_port(struct ib_device *device)
  132. {
  133. return (device->node_type == RDMA_NODE_IB_SWITCH) ? 0 : 1;
  134. }
  135. static int end_port(struct ib_device *device)
  136. {
  137. return (device->node_type == RDMA_NODE_IB_SWITCH) ?
  138. 0 : device->phys_port_cnt;
  139. }
  140. /**
  141. * ib_alloc_device - allocate an IB device struct
  142. * @size:size of structure to allocate
  143. *
  144. * Low-level drivers should use ib_alloc_device() to allocate &struct
  145. * ib_device. @size is the size of the structure to be allocated,
  146. * including any private data used by the low-level driver.
  147. * ib_dealloc_device() must be used to free structures allocated with
  148. * ib_alloc_device().
  149. */
  150. struct ib_device *ib_alloc_device(size_t size)
  151. {
  152. BUG_ON(size < sizeof (struct ib_device));
  153. return kzalloc(size, GFP_KERNEL);
  154. }
  155. EXPORT_SYMBOL(ib_alloc_device);
  156. /**
  157. * ib_dealloc_device - free an IB device struct
  158. * @device:structure to free
  159. *
  160. * Free a structure allocated with ib_alloc_device().
  161. */
  162. void ib_dealloc_device(struct ib_device *device)
  163. {
  164. if (device->reg_state == IB_DEV_UNINITIALIZED) {
  165. kfree(device);
  166. return;
  167. }
  168. BUG_ON(device->reg_state != IB_DEV_UNREGISTERED);
  169. kobject_put(&device->dev.kobj);
  170. }
  171. EXPORT_SYMBOL(ib_dealloc_device);
  172. static int add_client_context(struct ib_device *device, struct ib_client *client)
  173. {
  174. struct ib_client_data *context;
  175. unsigned long flags;
  176. context = kmalloc(sizeof *context, GFP_KERNEL);
  177. if (!context) {
  178. printk(KERN_WARNING "Couldn't allocate client context for %s/%s\n",
  179. device->name, client->name);
  180. return -ENOMEM;
  181. }
  182. context->client = client;
  183. context->data = NULL;
  184. spin_lock_irqsave(&device->client_data_lock, flags);
  185. list_add(&context->list, &device->client_data_list);
  186. spin_unlock_irqrestore(&device->client_data_lock, flags);
  187. return 0;
  188. }
  189. static int read_port_table_lengths(struct ib_device *device)
  190. {
  191. struct ib_port_attr *tprops = NULL;
  192. int num_ports, ret = -ENOMEM;
  193. u8 port_index;
  194. tprops = kmalloc(sizeof *tprops, GFP_KERNEL);
  195. if (!tprops)
  196. goto out;
  197. num_ports = end_port(device) - start_port(device) + 1;
  198. device->pkey_tbl_len = kmalloc(sizeof *device->pkey_tbl_len * num_ports,
  199. GFP_KERNEL);
  200. device->gid_tbl_len = kmalloc(sizeof *device->gid_tbl_len * num_ports,
  201. GFP_KERNEL);
  202. if (!device->pkey_tbl_len || !device->gid_tbl_len)
  203. goto err;
  204. for (port_index = 0; port_index < num_ports; ++port_index) {
  205. ret = ib_query_port(device, port_index + start_port(device),
  206. tprops);
  207. if (ret)
  208. goto err;
  209. device->pkey_tbl_len[port_index] = tprops->pkey_tbl_len;
  210. device->gid_tbl_len[port_index] = tprops->gid_tbl_len;
  211. }
  212. ret = 0;
  213. goto out;
  214. err:
  215. kfree(device->gid_tbl_len);
  216. kfree(device->pkey_tbl_len);
  217. out:
  218. kfree(tprops);
  219. return ret;
  220. }
  221. /**
  222. * ib_register_device - Register an IB device with IB core
  223. * @device:Device to register
  224. *
  225. * Low-level drivers use ib_register_device() to register their
  226. * devices with the IB core. All registered clients will receive a
  227. * callback for each device that is added. @device must be allocated
  228. * with ib_alloc_device().
  229. */
  230. int ib_register_device(struct ib_device *device,
  231. int (*port_callback)(struct ib_device *,
  232. u8, struct kobject *))
  233. {
  234. int ret;
  235. mutex_lock(&device_mutex);
  236. if (strchr(device->name, '%')) {
  237. ret = alloc_name(device->name);
  238. if (ret)
  239. goto out;
  240. }
  241. if (ib_device_check_mandatory(device)) {
  242. ret = -EINVAL;
  243. goto out;
  244. }
  245. INIT_LIST_HEAD(&device->event_handler_list);
  246. INIT_LIST_HEAD(&device->client_data_list);
  247. spin_lock_init(&device->event_handler_lock);
  248. spin_lock_init(&device->client_data_lock);
  249. ret = read_port_table_lengths(device);
  250. if (ret) {
  251. printk(KERN_WARNING "Couldn't create table lengths cache for device %s\n",
  252. device->name);
  253. goto out;
  254. }
  255. ret = ib_device_register_sysfs(device, port_callback);
  256. if (ret) {
  257. printk(KERN_WARNING "Couldn't register device %s with driver model\n",
  258. device->name);
  259. kfree(device->gid_tbl_len);
  260. kfree(device->pkey_tbl_len);
  261. goto out;
  262. }
  263. list_add_tail(&device->core_list, &device_list);
  264. device->reg_state = IB_DEV_REGISTERED;
  265. {
  266. struct ib_client *client;
  267. list_for_each_entry(client, &client_list, list)
  268. if (client->add && !add_client_context(device, client))
  269. client->add(device);
  270. }
  271. out:
  272. mutex_unlock(&device_mutex);
  273. return ret;
  274. }
  275. EXPORT_SYMBOL(ib_register_device);
  276. /**
  277. * ib_unregister_device - Unregister an IB device
  278. * @device:Device to unregister
  279. *
  280. * Unregister an IB device. All clients will receive a remove callback.
  281. */
  282. void ib_unregister_device(struct ib_device *device)
  283. {
  284. struct ib_client *client;
  285. struct ib_client_data *context, *tmp;
  286. unsigned long flags;
  287. mutex_lock(&device_mutex);
  288. list_for_each_entry_reverse(client, &client_list, list)
  289. if (client->remove)
  290. client->remove(device);
  291. list_del(&device->core_list);
  292. kfree(device->gid_tbl_len);
  293. kfree(device->pkey_tbl_len);
  294. mutex_unlock(&device_mutex);
  295. ib_device_unregister_sysfs(device);
  296. spin_lock_irqsave(&device->client_data_lock, flags);
  297. list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
  298. kfree(context);
  299. spin_unlock_irqrestore(&device->client_data_lock, flags);
  300. device->reg_state = IB_DEV_UNREGISTERED;
  301. }
  302. EXPORT_SYMBOL(ib_unregister_device);
  303. /**
  304. * ib_register_client - Register an IB client
  305. * @client:Client to register
  306. *
  307. * Upper level users of the IB drivers can use ib_register_client() to
  308. * register callbacks for IB device addition and removal. When an IB
  309. * device is added, each registered client's add method will be called
  310. * (in the order the clients were registered), and when a device is
  311. * removed, each client's remove method will be called (in the reverse
  312. * order that clients were registered). In addition, when
  313. * ib_register_client() is called, the client will receive an add
  314. * callback for all devices already registered.
  315. */
  316. int ib_register_client(struct ib_client *client)
  317. {
  318. struct ib_device *device;
  319. mutex_lock(&device_mutex);
  320. list_add_tail(&client->list, &client_list);
  321. list_for_each_entry(device, &device_list, core_list)
  322. if (client->add && !add_client_context(device, client))
  323. client->add(device);
  324. mutex_unlock(&device_mutex);
  325. return 0;
  326. }
  327. EXPORT_SYMBOL(ib_register_client);
  328. /**
  329. * ib_unregister_client - Unregister an IB client
  330. * @client:Client to unregister
  331. *
  332. * Upper level users use ib_unregister_client() to remove their client
  333. * registration. When ib_unregister_client() is called, the client
  334. * will receive a remove callback for each IB device still registered.
  335. */
  336. void ib_unregister_client(struct ib_client *client)
  337. {
  338. struct ib_client_data *context, *tmp;
  339. struct ib_device *device;
  340. unsigned long flags;
  341. mutex_lock(&device_mutex);
  342. list_for_each_entry(device, &device_list, core_list) {
  343. if (client->remove)
  344. client->remove(device);
  345. spin_lock_irqsave(&device->client_data_lock, flags);
  346. list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
  347. if (context->client == client) {
  348. list_del(&context->list);
  349. kfree(context);
  350. }
  351. spin_unlock_irqrestore(&device->client_data_lock, flags);
  352. }
  353. list_del(&client->list);
  354. mutex_unlock(&device_mutex);
  355. }
  356. EXPORT_SYMBOL(ib_unregister_client);
  357. /**
  358. * ib_get_client_data - Get IB client context
  359. * @device:Device to get context for
  360. * @client:Client to get context for
  361. *
  362. * ib_get_client_data() returns client context set with
  363. * ib_set_client_data().
  364. */
  365. void *ib_get_client_data(struct ib_device *device, struct ib_client *client)
  366. {
  367. struct ib_client_data *context;
  368. void *ret = NULL;
  369. unsigned long flags;
  370. spin_lock_irqsave(&device->client_data_lock, flags);
  371. list_for_each_entry(context, &device->client_data_list, list)
  372. if (context->client == client) {
  373. ret = context->data;
  374. break;
  375. }
  376. spin_unlock_irqrestore(&device->client_data_lock, flags);
  377. return ret;
  378. }
  379. EXPORT_SYMBOL(ib_get_client_data);
  380. /**
  381. * ib_set_client_data - Set IB client context
  382. * @device:Device to set context for
  383. * @client:Client to set context for
  384. * @data:Context to set
  385. *
  386. * ib_set_client_data() sets client context that can be retrieved with
  387. * ib_get_client_data().
  388. */
  389. void ib_set_client_data(struct ib_device *device, struct ib_client *client,
  390. void *data)
  391. {
  392. struct ib_client_data *context;
  393. unsigned long flags;
  394. spin_lock_irqsave(&device->client_data_lock, flags);
  395. list_for_each_entry(context, &device->client_data_list, list)
  396. if (context->client == client) {
  397. context->data = data;
  398. goto out;
  399. }
  400. printk(KERN_WARNING "No client context found for %s/%s\n",
  401. device->name, client->name);
  402. out:
  403. spin_unlock_irqrestore(&device->client_data_lock, flags);
  404. }
  405. EXPORT_SYMBOL(ib_set_client_data);
  406. /**
  407. * ib_register_event_handler - Register an IB event handler
  408. * @event_handler:Handler to register
  409. *
  410. * ib_register_event_handler() registers an event handler that will be
  411. * called back when asynchronous IB events occur (as defined in
  412. * chapter 11 of the InfiniBand Architecture Specification). This
  413. * callback may occur in interrupt context.
  414. */
  415. int ib_register_event_handler (struct ib_event_handler *event_handler)
  416. {
  417. unsigned long flags;
  418. spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
  419. list_add_tail(&event_handler->list,
  420. &event_handler->device->event_handler_list);
  421. spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
  422. return 0;
  423. }
  424. EXPORT_SYMBOL(ib_register_event_handler);
  425. /**
  426. * ib_unregister_event_handler - Unregister an event handler
  427. * @event_handler:Handler to unregister
  428. *
  429. * Unregister an event handler registered with
  430. * ib_register_event_handler().
  431. */
  432. int ib_unregister_event_handler(struct ib_event_handler *event_handler)
  433. {
  434. unsigned long flags;
  435. spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
  436. list_del(&event_handler->list);
  437. spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
  438. return 0;
  439. }
  440. EXPORT_SYMBOL(ib_unregister_event_handler);
  441. /**
  442. * ib_dispatch_event - Dispatch an asynchronous event
  443. * @event:Event to dispatch
  444. *
  445. * Low-level drivers must call ib_dispatch_event() to dispatch the
  446. * event to all registered event handlers when an asynchronous event
  447. * occurs.
  448. */
  449. void ib_dispatch_event(struct ib_event *event)
  450. {
  451. unsigned long flags;
  452. struct ib_event_handler *handler;
  453. spin_lock_irqsave(&event->device->event_handler_lock, flags);
  454. list_for_each_entry(handler, &event->device->event_handler_list, list)
  455. handler->handler(handler, event);
  456. spin_unlock_irqrestore(&event->device->event_handler_lock, flags);
  457. }
  458. EXPORT_SYMBOL(ib_dispatch_event);
  459. /**
  460. * ib_query_device - Query IB device attributes
  461. * @device:Device to query
  462. * @device_attr:Device attributes
  463. *
  464. * ib_query_device() returns the attributes of a device through the
  465. * @device_attr pointer.
  466. */
  467. int ib_query_device(struct ib_device *device,
  468. struct ib_device_attr *device_attr)
  469. {
  470. return device->query_device(device, device_attr);
  471. }
  472. EXPORT_SYMBOL(ib_query_device);
  473. /**
  474. * ib_query_port - Query IB port attributes
  475. * @device:Device to query
  476. * @port_num:Port number to query
  477. * @port_attr:Port attributes
  478. *
  479. * ib_query_port() returns the attributes of a port through the
  480. * @port_attr pointer.
  481. */
  482. int ib_query_port(struct ib_device *device,
  483. u8 port_num,
  484. struct ib_port_attr *port_attr)
  485. {
  486. if (port_num < start_port(device) || port_num > end_port(device))
  487. return -EINVAL;
  488. return device->query_port(device, port_num, port_attr);
  489. }
  490. EXPORT_SYMBOL(ib_query_port);
  491. /**
  492. * ib_query_gid - Get GID table entry
  493. * @device:Device to query
  494. * @port_num:Port number to query
  495. * @index:GID table index to query
  496. * @gid:Returned GID
  497. *
  498. * ib_query_gid() fetches the specified GID table entry.
  499. */
  500. int ib_query_gid(struct ib_device *device,
  501. u8 port_num, int index, union ib_gid *gid)
  502. {
  503. return device->query_gid(device, port_num, index, gid);
  504. }
  505. EXPORT_SYMBOL(ib_query_gid);
  506. /**
  507. * ib_query_pkey - Get P_Key table entry
  508. * @device:Device to query
  509. * @port_num:Port number to query
  510. * @index:P_Key table index to query
  511. * @pkey:Returned P_Key
  512. *
  513. * ib_query_pkey() fetches the specified P_Key table entry.
  514. */
  515. int ib_query_pkey(struct ib_device *device,
  516. u8 port_num, u16 index, u16 *pkey)
  517. {
  518. return device->query_pkey(device, port_num, index, pkey);
  519. }
  520. EXPORT_SYMBOL(ib_query_pkey);
  521. /**
  522. * ib_modify_device - Change IB device attributes
  523. * @device:Device to modify
  524. * @device_modify_mask:Mask of attributes to change
  525. * @device_modify:New attribute values
  526. *
  527. * ib_modify_device() changes a device's attributes as specified by
  528. * the @device_modify_mask and @device_modify structure.
  529. */
  530. int ib_modify_device(struct ib_device *device,
  531. int device_modify_mask,
  532. struct ib_device_modify *device_modify)
  533. {
  534. return device->modify_device(device, device_modify_mask,
  535. device_modify);
  536. }
  537. EXPORT_SYMBOL(ib_modify_device);
  538. /**
  539. * ib_modify_port - Modifies the attributes for the specified port.
  540. * @device: The device to modify.
  541. * @port_num: The number of the port to modify.
  542. * @port_modify_mask: Mask used to specify which attributes of the port
  543. * to change.
  544. * @port_modify: New attribute values for the port.
  545. *
  546. * ib_modify_port() changes a port's attributes as specified by the
  547. * @port_modify_mask and @port_modify structure.
  548. */
  549. int ib_modify_port(struct ib_device *device,
  550. u8 port_num, int port_modify_mask,
  551. struct ib_port_modify *port_modify)
  552. {
  553. if (port_num < start_port(device) || port_num > end_port(device))
  554. return -EINVAL;
  555. return device->modify_port(device, port_num, port_modify_mask,
  556. port_modify);
  557. }
  558. EXPORT_SYMBOL(ib_modify_port);
  559. /**
  560. * ib_find_gid - Returns the port number and GID table index where
  561. * a specified GID value occurs.
  562. * @device: The device to query.
  563. * @gid: The GID value to search for.
  564. * @port_num: The port number of the device where the GID value was found.
  565. * @index: The index into the GID table where the GID was found. This
  566. * parameter may be NULL.
  567. */
  568. int ib_find_gid(struct ib_device *device, union ib_gid *gid,
  569. u8 *port_num, u16 *index)
  570. {
  571. union ib_gid tmp_gid;
  572. int ret, port, i;
  573. for (port = start_port(device); port <= end_port(device); ++port) {
  574. for (i = 0; i < device->gid_tbl_len[port - start_port(device)]; ++i) {
  575. ret = ib_query_gid(device, port, i, &tmp_gid);
  576. if (ret)
  577. return ret;
  578. if (!memcmp(&tmp_gid, gid, sizeof *gid)) {
  579. *port_num = port;
  580. if (index)
  581. *index = i;
  582. return 0;
  583. }
  584. }
  585. }
  586. return -ENOENT;
  587. }
  588. EXPORT_SYMBOL(ib_find_gid);
  589. /**
  590. * ib_find_pkey - Returns the PKey table index where a specified
  591. * PKey value occurs.
  592. * @device: The device to query.
  593. * @port_num: The port number of the device to search for the PKey.
  594. * @pkey: The PKey value to search for.
  595. * @index: The index into the PKey table where the PKey was found.
  596. */
  597. int ib_find_pkey(struct ib_device *device,
  598. u8 port_num, u16 pkey, u16 *index)
  599. {
  600. int ret, i;
  601. u16 tmp_pkey;
  602. for (i = 0; i < device->pkey_tbl_len[port_num - start_port(device)]; ++i) {
  603. ret = ib_query_pkey(device, port_num, i, &tmp_pkey);
  604. if (ret)
  605. return ret;
  606. if ((pkey & 0x7fff) == (tmp_pkey & 0x7fff)) {
  607. *index = i;
  608. return 0;
  609. }
  610. }
  611. return -ENOENT;
  612. }
  613. EXPORT_SYMBOL(ib_find_pkey);
  614. static int __init ib_core_init(void)
  615. {
  616. int ret;
  617. ret = ib_sysfs_setup();
  618. if (ret)
  619. printk(KERN_WARNING "Couldn't create InfiniBand device class\n");
  620. ret = ib_cache_setup();
  621. if (ret) {
  622. printk(KERN_WARNING "Couldn't set up InfiniBand P_Key/GID cache\n");
  623. ib_sysfs_cleanup();
  624. }
  625. return ret;
  626. }
  627. static void __exit ib_core_cleanup(void)
  628. {
  629. ib_cache_cleanup();
  630. ib_sysfs_cleanup();
  631. /* Make sure that any pending umem accounting work is done. */
  632. flush_scheduled_work();
  633. }
  634. module_init(ib_core_init);
  635. module_exit(ib_core_cleanup);