device.c 20 KB

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