device.c 20 KB

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