device.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  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/netdevice.h>
  41. #include <rdma/rdma_netlink.h>
  42. #include <rdma/ib_addr.h>
  43. #include <rdma/ib_cache.h>
  44. #include "core_priv.h"
  45. MODULE_AUTHOR("Roland Dreier");
  46. MODULE_DESCRIPTION("core kernel InfiniBand API");
  47. MODULE_LICENSE("Dual BSD/GPL");
  48. struct ib_client_data {
  49. struct list_head list;
  50. struct ib_client *client;
  51. void * data;
  52. /* The device or client is going down. Do not call client or device
  53. * callbacks other than remove(). */
  54. bool going_down;
  55. };
  56. struct workqueue_struct *ib_comp_wq;
  57. struct workqueue_struct *ib_wq;
  58. EXPORT_SYMBOL_GPL(ib_wq);
  59. /* The device_list and client_list contain devices and clients after their
  60. * registration has completed, and the devices and clients are removed
  61. * during unregistration. */
  62. static LIST_HEAD(device_list);
  63. static LIST_HEAD(client_list);
  64. /*
  65. * device_mutex and lists_rwsem protect access to both device_list and
  66. * client_list. device_mutex protects writer access by device and client
  67. * registration / de-registration. lists_rwsem protects reader access to
  68. * these lists. Iterators of these lists must lock it for read, while updates
  69. * to the lists must be done with a write lock. A special case is when the
  70. * device_mutex is locked. In this case locking the lists for read access is
  71. * not necessary as the device_mutex implies it.
  72. *
  73. * lists_rwsem also protects access to the client data list.
  74. */
  75. static DEFINE_MUTEX(device_mutex);
  76. static DECLARE_RWSEM(lists_rwsem);
  77. static int ib_device_check_mandatory(struct ib_device *device)
  78. {
  79. #define IB_MANDATORY_FUNC(x) { offsetof(struct ib_device, x), #x }
  80. static const struct {
  81. size_t offset;
  82. char *name;
  83. } mandatory_table[] = {
  84. IB_MANDATORY_FUNC(query_device),
  85. IB_MANDATORY_FUNC(query_port),
  86. IB_MANDATORY_FUNC(query_pkey),
  87. IB_MANDATORY_FUNC(query_gid),
  88. IB_MANDATORY_FUNC(alloc_pd),
  89. IB_MANDATORY_FUNC(dealloc_pd),
  90. IB_MANDATORY_FUNC(create_ah),
  91. IB_MANDATORY_FUNC(destroy_ah),
  92. IB_MANDATORY_FUNC(create_qp),
  93. IB_MANDATORY_FUNC(modify_qp),
  94. IB_MANDATORY_FUNC(destroy_qp),
  95. IB_MANDATORY_FUNC(post_send),
  96. IB_MANDATORY_FUNC(post_recv),
  97. IB_MANDATORY_FUNC(create_cq),
  98. IB_MANDATORY_FUNC(destroy_cq),
  99. IB_MANDATORY_FUNC(poll_cq),
  100. IB_MANDATORY_FUNC(req_notify_cq),
  101. IB_MANDATORY_FUNC(get_dma_mr),
  102. IB_MANDATORY_FUNC(dereg_mr),
  103. IB_MANDATORY_FUNC(get_port_immutable)
  104. };
  105. int i;
  106. for (i = 0; i < ARRAY_SIZE(mandatory_table); ++i) {
  107. if (!*(void **) ((void *) device + mandatory_table[i].offset)) {
  108. pr_warn("Device %s is missing mandatory function %s\n",
  109. device->name, mandatory_table[i].name);
  110. return -EINVAL;
  111. }
  112. }
  113. return 0;
  114. }
  115. static struct ib_device *__ib_device_get_by_name(const char *name)
  116. {
  117. struct ib_device *device;
  118. list_for_each_entry(device, &device_list, core_list)
  119. if (!strncmp(name, device->name, IB_DEVICE_NAME_MAX))
  120. return device;
  121. return NULL;
  122. }
  123. static int alloc_name(char *name)
  124. {
  125. unsigned long *inuse;
  126. char buf[IB_DEVICE_NAME_MAX];
  127. struct ib_device *device;
  128. int i;
  129. inuse = (unsigned long *) get_zeroed_page(GFP_KERNEL);
  130. if (!inuse)
  131. return -ENOMEM;
  132. list_for_each_entry(device, &device_list, core_list) {
  133. if (!sscanf(device->name, name, &i))
  134. continue;
  135. if (i < 0 || i >= PAGE_SIZE * 8)
  136. continue;
  137. snprintf(buf, sizeof buf, name, i);
  138. if (!strncmp(buf, device->name, IB_DEVICE_NAME_MAX))
  139. set_bit(i, inuse);
  140. }
  141. i = find_first_zero_bit(inuse, PAGE_SIZE * 8);
  142. free_page((unsigned long) inuse);
  143. snprintf(buf, sizeof buf, name, i);
  144. if (__ib_device_get_by_name(buf))
  145. return -ENFILE;
  146. strlcpy(name, buf, IB_DEVICE_NAME_MAX);
  147. return 0;
  148. }
  149. static void ib_device_release(struct device *device)
  150. {
  151. struct ib_device *dev = container_of(device, struct ib_device, dev);
  152. ib_cache_release_one(dev);
  153. kfree(dev->port_immutable);
  154. kfree(dev);
  155. }
  156. static int ib_device_uevent(struct device *device,
  157. struct kobj_uevent_env *env)
  158. {
  159. struct ib_device *dev = container_of(device, struct ib_device, dev);
  160. if (add_uevent_var(env, "NAME=%s", dev->name))
  161. return -ENOMEM;
  162. /*
  163. * It would be nice to pass the node GUID with the event...
  164. */
  165. return 0;
  166. }
  167. static struct class ib_class = {
  168. .name = "infiniband",
  169. .dev_release = ib_device_release,
  170. .dev_uevent = ib_device_uevent,
  171. };
  172. /**
  173. * ib_alloc_device - allocate an IB device struct
  174. * @size:size of structure to allocate
  175. *
  176. * Low-level drivers should use ib_alloc_device() to allocate &struct
  177. * ib_device. @size is the size of the structure to be allocated,
  178. * including any private data used by the low-level driver.
  179. * ib_dealloc_device() must be used to free structures allocated with
  180. * ib_alloc_device().
  181. */
  182. struct ib_device *ib_alloc_device(size_t size)
  183. {
  184. struct ib_device *device;
  185. if (WARN_ON(size < sizeof(struct ib_device)))
  186. return NULL;
  187. device = kzalloc(size, GFP_KERNEL);
  188. if (!device)
  189. return NULL;
  190. device->dev.class = &ib_class;
  191. device_initialize(&device->dev);
  192. dev_set_drvdata(&device->dev, device);
  193. INIT_LIST_HEAD(&device->event_handler_list);
  194. spin_lock_init(&device->event_handler_lock);
  195. spin_lock_init(&device->client_data_lock);
  196. INIT_LIST_HEAD(&device->client_data_list);
  197. INIT_LIST_HEAD(&device->port_list);
  198. return device;
  199. }
  200. EXPORT_SYMBOL(ib_alloc_device);
  201. /**
  202. * ib_dealloc_device - free an IB device struct
  203. * @device:structure to free
  204. *
  205. * Free a structure allocated with ib_alloc_device().
  206. */
  207. void ib_dealloc_device(struct ib_device *device)
  208. {
  209. WARN_ON(device->reg_state != IB_DEV_UNREGISTERED &&
  210. device->reg_state != IB_DEV_UNINITIALIZED);
  211. kobject_put(&device->dev.kobj);
  212. }
  213. EXPORT_SYMBOL(ib_dealloc_device);
  214. static int add_client_context(struct ib_device *device, struct ib_client *client)
  215. {
  216. struct ib_client_data *context;
  217. unsigned long flags;
  218. context = kmalloc(sizeof *context, GFP_KERNEL);
  219. if (!context)
  220. return -ENOMEM;
  221. context->client = client;
  222. context->data = NULL;
  223. context->going_down = false;
  224. down_write(&lists_rwsem);
  225. spin_lock_irqsave(&device->client_data_lock, flags);
  226. list_add(&context->list, &device->client_data_list);
  227. spin_unlock_irqrestore(&device->client_data_lock, flags);
  228. up_write(&lists_rwsem);
  229. return 0;
  230. }
  231. static int verify_immutable(const struct ib_device *dev, u8 port)
  232. {
  233. return WARN_ON(!rdma_cap_ib_mad(dev, port) &&
  234. rdma_max_mad_size(dev, port) != 0);
  235. }
  236. static int read_port_immutable(struct ib_device *device)
  237. {
  238. int ret;
  239. u8 start_port = rdma_start_port(device);
  240. u8 end_port = rdma_end_port(device);
  241. u8 port;
  242. /**
  243. * device->port_immutable is indexed directly by the port number to make
  244. * access to this data as efficient as possible.
  245. *
  246. * Therefore port_immutable is declared as a 1 based array with
  247. * potential empty slots at the beginning.
  248. */
  249. device->port_immutable = kzalloc(sizeof(*device->port_immutable)
  250. * (end_port + 1),
  251. GFP_KERNEL);
  252. if (!device->port_immutable)
  253. return -ENOMEM;
  254. for (port = start_port; port <= end_port; ++port) {
  255. ret = device->get_port_immutable(device, port,
  256. &device->port_immutable[port]);
  257. if (ret)
  258. return ret;
  259. if (verify_immutable(device, port))
  260. return -EINVAL;
  261. }
  262. return 0;
  263. }
  264. void ib_get_device_fw_str(struct ib_device *dev, char *str, size_t str_len)
  265. {
  266. if (dev->get_dev_fw_str)
  267. dev->get_dev_fw_str(dev, str, str_len);
  268. else
  269. str[0] = '\0';
  270. }
  271. EXPORT_SYMBOL(ib_get_device_fw_str);
  272. /**
  273. * ib_register_device - Register an IB device with IB core
  274. * @device:Device to register
  275. *
  276. * Low-level drivers use ib_register_device() to register their
  277. * devices with the IB core. All registered clients will receive a
  278. * callback for each device that is added. @device must be allocated
  279. * with ib_alloc_device().
  280. */
  281. int ib_register_device(struct ib_device *device,
  282. int (*port_callback)(struct ib_device *,
  283. u8, struct kobject *))
  284. {
  285. int ret;
  286. struct ib_client *client;
  287. struct ib_udata uhw = {.outlen = 0, .inlen = 0};
  288. mutex_lock(&device_mutex);
  289. if (strchr(device->name, '%')) {
  290. ret = alloc_name(device->name);
  291. if (ret)
  292. goto out;
  293. }
  294. if (ib_device_check_mandatory(device)) {
  295. ret = -EINVAL;
  296. goto out;
  297. }
  298. ret = read_port_immutable(device);
  299. if (ret) {
  300. pr_warn("Couldn't create per port immutable data %s\n",
  301. device->name);
  302. goto out;
  303. }
  304. ret = ib_cache_setup_one(device);
  305. if (ret) {
  306. pr_warn("Couldn't set up InfiniBand P_Key/GID cache\n");
  307. goto out;
  308. }
  309. memset(&device->attrs, 0, sizeof(device->attrs));
  310. ret = device->query_device(device, &device->attrs, &uhw);
  311. if (ret) {
  312. pr_warn("Couldn't query the device attributes\n");
  313. ib_cache_cleanup_one(device);
  314. goto out;
  315. }
  316. ret = ib_device_register_sysfs(device, port_callback);
  317. if (ret) {
  318. pr_warn("Couldn't register device %s with driver model\n",
  319. device->name);
  320. ib_cache_cleanup_one(device);
  321. goto out;
  322. }
  323. device->reg_state = IB_DEV_REGISTERED;
  324. list_for_each_entry(client, &client_list, list)
  325. if (client->add && !add_client_context(device, client))
  326. client->add(device);
  327. down_write(&lists_rwsem);
  328. list_add_tail(&device->core_list, &device_list);
  329. up_write(&lists_rwsem);
  330. out:
  331. mutex_unlock(&device_mutex);
  332. return ret;
  333. }
  334. EXPORT_SYMBOL(ib_register_device);
  335. /**
  336. * ib_unregister_device - Unregister an IB device
  337. * @device:Device to unregister
  338. *
  339. * Unregister an IB device. All clients will receive a remove callback.
  340. */
  341. void ib_unregister_device(struct ib_device *device)
  342. {
  343. struct ib_client_data *context, *tmp;
  344. unsigned long flags;
  345. mutex_lock(&device_mutex);
  346. down_write(&lists_rwsem);
  347. list_del(&device->core_list);
  348. spin_lock_irqsave(&device->client_data_lock, flags);
  349. list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
  350. context->going_down = true;
  351. spin_unlock_irqrestore(&device->client_data_lock, flags);
  352. downgrade_write(&lists_rwsem);
  353. list_for_each_entry_safe(context, tmp, &device->client_data_list,
  354. list) {
  355. if (context->client->remove)
  356. context->client->remove(device, context->data);
  357. }
  358. up_read(&lists_rwsem);
  359. mutex_unlock(&device_mutex);
  360. ib_device_unregister_sysfs(device);
  361. ib_cache_cleanup_one(device);
  362. down_write(&lists_rwsem);
  363. spin_lock_irqsave(&device->client_data_lock, flags);
  364. list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
  365. kfree(context);
  366. spin_unlock_irqrestore(&device->client_data_lock, flags);
  367. up_write(&lists_rwsem);
  368. device->reg_state = IB_DEV_UNREGISTERED;
  369. }
  370. EXPORT_SYMBOL(ib_unregister_device);
  371. /**
  372. * ib_register_client - Register an IB client
  373. * @client:Client to register
  374. *
  375. * Upper level users of the IB drivers can use ib_register_client() to
  376. * register callbacks for IB device addition and removal. When an IB
  377. * device is added, each registered client's add method will be called
  378. * (in the order the clients were registered), and when a device is
  379. * removed, each client's remove method will be called (in the reverse
  380. * order that clients were registered). In addition, when
  381. * ib_register_client() is called, the client will receive an add
  382. * callback for all devices already registered.
  383. */
  384. int ib_register_client(struct ib_client *client)
  385. {
  386. struct ib_device *device;
  387. mutex_lock(&device_mutex);
  388. list_for_each_entry(device, &device_list, core_list)
  389. if (client->add && !add_client_context(device, client))
  390. client->add(device);
  391. down_write(&lists_rwsem);
  392. list_add_tail(&client->list, &client_list);
  393. up_write(&lists_rwsem);
  394. mutex_unlock(&device_mutex);
  395. return 0;
  396. }
  397. EXPORT_SYMBOL(ib_register_client);
  398. /**
  399. * ib_unregister_client - Unregister an IB client
  400. * @client:Client to unregister
  401. *
  402. * Upper level users use ib_unregister_client() to remove their client
  403. * registration. When ib_unregister_client() is called, the client
  404. * will receive a remove callback for each IB device still registered.
  405. */
  406. void ib_unregister_client(struct ib_client *client)
  407. {
  408. struct ib_client_data *context, *tmp;
  409. struct ib_device *device;
  410. unsigned long flags;
  411. mutex_lock(&device_mutex);
  412. down_write(&lists_rwsem);
  413. list_del(&client->list);
  414. up_write(&lists_rwsem);
  415. list_for_each_entry(device, &device_list, core_list) {
  416. struct ib_client_data *found_context = NULL;
  417. down_write(&lists_rwsem);
  418. spin_lock_irqsave(&device->client_data_lock, flags);
  419. list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
  420. if (context->client == client) {
  421. context->going_down = true;
  422. found_context = context;
  423. break;
  424. }
  425. spin_unlock_irqrestore(&device->client_data_lock, flags);
  426. up_write(&lists_rwsem);
  427. if (client->remove)
  428. client->remove(device, found_context ?
  429. found_context->data : NULL);
  430. if (!found_context) {
  431. pr_warn("No client context found for %s/%s\n",
  432. device->name, client->name);
  433. continue;
  434. }
  435. down_write(&lists_rwsem);
  436. spin_lock_irqsave(&device->client_data_lock, flags);
  437. list_del(&found_context->list);
  438. kfree(found_context);
  439. spin_unlock_irqrestore(&device->client_data_lock, flags);
  440. up_write(&lists_rwsem);
  441. }
  442. mutex_unlock(&device_mutex);
  443. }
  444. EXPORT_SYMBOL(ib_unregister_client);
  445. /**
  446. * ib_get_client_data - Get IB client context
  447. * @device:Device to get context for
  448. * @client:Client to get context for
  449. *
  450. * ib_get_client_data() returns client context set with
  451. * ib_set_client_data().
  452. */
  453. void *ib_get_client_data(struct ib_device *device, struct ib_client *client)
  454. {
  455. struct ib_client_data *context;
  456. void *ret = NULL;
  457. unsigned long flags;
  458. spin_lock_irqsave(&device->client_data_lock, flags);
  459. list_for_each_entry(context, &device->client_data_list, list)
  460. if (context->client == client) {
  461. ret = context->data;
  462. break;
  463. }
  464. spin_unlock_irqrestore(&device->client_data_lock, flags);
  465. return ret;
  466. }
  467. EXPORT_SYMBOL(ib_get_client_data);
  468. /**
  469. * ib_set_client_data - Set IB client context
  470. * @device:Device to set context for
  471. * @client:Client to set context for
  472. * @data:Context to set
  473. *
  474. * ib_set_client_data() sets client context that can be retrieved with
  475. * ib_get_client_data().
  476. */
  477. void ib_set_client_data(struct ib_device *device, struct ib_client *client,
  478. void *data)
  479. {
  480. struct ib_client_data *context;
  481. unsigned long flags;
  482. spin_lock_irqsave(&device->client_data_lock, flags);
  483. list_for_each_entry(context, &device->client_data_list, list)
  484. if (context->client == client) {
  485. context->data = data;
  486. goto out;
  487. }
  488. pr_warn("No client context found for %s/%s\n",
  489. device->name, client->name);
  490. out:
  491. spin_unlock_irqrestore(&device->client_data_lock, flags);
  492. }
  493. EXPORT_SYMBOL(ib_set_client_data);
  494. /**
  495. * ib_register_event_handler - Register an IB event handler
  496. * @event_handler:Handler to register
  497. *
  498. * ib_register_event_handler() registers an event handler that will be
  499. * called back when asynchronous IB events occur (as defined in
  500. * chapter 11 of the InfiniBand Architecture Specification). This
  501. * callback may occur in interrupt context.
  502. */
  503. int ib_register_event_handler (struct ib_event_handler *event_handler)
  504. {
  505. unsigned long flags;
  506. spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
  507. list_add_tail(&event_handler->list,
  508. &event_handler->device->event_handler_list);
  509. spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
  510. return 0;
  511. }
  512. EXPORT_SYMBOL(ib_register_event_handler);
  513. /**
  514. * ib_unregister_event_handler - Unregister an event handler
  515. * @event_handler:Handler to unregister
  516. *
  517. * Unregister an event handler registered with
  518. * ib_register_event_handler().
  519. */
  520. int ib_unregister_event_handler(struct ib_event_handler *event_handler)
  521. {
  522. unsigned long flags;
  523. spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
  524. list_del(&event_handler->list);
  525. spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
  526. return 0;
  527. }
  528. EXPORT_SYMBOL(ib_unregister_event_handler);
  529. /**
  530. * ib_dispatch_event - Dispatch an asynchronous event
  531. * @event:Event to dispatch
  532. *
  533. * Low-level drivers must call ib_dispatch_event() to dispatch the
  534. * event to all registered event handlers when an asynchronous event
  535. * occurs.
  536. */
  537. void ib_dispatch_event(struct ib_event *event)
  538. {
  539. unsigned long flags;
  540. struct ib_event_handler *handler;
  541. spin_lock_irqsave(&event->device->event_handler_lock, flags);
  542. list_for_each_entry(handler, &event->device->event_handler_list, list)
  543. handler->handler(handler, event);
  544. spin_unlock_irqrestore(&event->device->event_handler_lock, flags);
  545. }
  546. EXPORT_SYMBOL(ib_dispatch_event);
  547. /**
  548. * ib_query_port - Query IB port attributes
  549. * @device:Device to query
  550. * @port_num:Port number to query
  551. * @port_attr:Port attributes
  552. *
  553. * ib_query_port() returns the attributes of a port through the
  554. * @port_attr pointer.
  555. */
  556. int ib_query_port(struct ib_device *device,
  557. u8 port_num,
  558. struct ib_port_attr *port_attr)
  559. {
  560. union ib_gid gid;
  561. int err;
  562. if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
  563. return -EINVAL;
  564. memset(port_attr, 0, sizeof(*port_attr));
  565. err = device->query_port(device, port_num, port_attr);
  566. if (err || port_attr->subnet_prefix)
  567. return err;
  568. if (rdma_port_get_link_layer(device, port_num) != IB_LINK_LAYER_INFINIBAND)
  569. return 0;
  570. err = ib_query_gid(device, port_num, 0, &gid, NULL);
  571. if (err)
  572. return err;
  573. port_attr->subnet_prefix = be64_to_cpu(gid.global.subnet_prefix);
  574. return 0;
  575. }
  576. EXPORT_SYMBOL(ib_query_port);
  577. /**
  578. * ib_query_gid - Get GID table entry
  579. * @device:Device to query
  580. * @port_num:Port number to query
  581. * @index:GID table index to query
  582. * @gid:Returned GID
  583. * @attr: Returned GID attributes related to this GID index (only in RoCE).
  584. * NULL means ignore.
  585. *
  586. * ib_query_gid() fetches the specified GID table entry.
  587. */
  588. int ib_query_gid(struct ib_device *device,
  589. u8 port_num, int index, union ib_gid *gid,
  590. struct ib_gid_attr *attr)
  591. {
  592. if (rdma_cap_roce_gid_table(device, port_num))
  593. return ib_get_cached_gid(device, port_num, index, gid, attr);
  594. if (attr)
  595. return -EINVAL;
  596. return device->query_gid(device, port_num, index, gid);
  597. }
  598. EXPORT_SYMBOL(ib_query_gid);
  599. /**
  600. * ib_enum_roce_netdev - enumerate all RoCE ports
  601. * @ib_dev : IB device we want to query
  602. * @filter: Should we call the callback?
  603. * @filter_cookie: Cookie passed to filter
  604. * @cb: Callback to call for each found RoCE ports
  605. * @cookie: Cookie passed back to the callback
  606. *
  607. * Enumerates all of the physical RoCE ports of ib_dev
  608. * which are related to netdevice and calls callback() on each
  609. * device for which filter() function returns non zero.
  610. */
  611. void ib_enum_roce_netdev(struct ib_device *ib_dev,
  612. roce_netdev_filter filter,
  613. void *filter_cookie,
  614. roce_netdev_callback cb,
  615. void *cookie)
  616. {
  617. u8 port;
  618. for (port = rdma_start_port(ib_dev); port <= rdma_end_port(ib_dev);
  619. port++)
  620. if (rdma_protocol_roce(ib_dev, port)) {
  621. struct net_device *idev = NULL;
  622. if (ib_dev->get_netdev)
  623. idev = ib_dev->get_netdev(ib_dev, port);
  624. if (idev &&
  625. idev->reg_state >= NETREG_UNREGISTERED) {
  626. dev_put(idev);
  627. idev = NULL;
  628. }
  629. if (filter(ib_dev, port, idev, filter_cookie))
  630. cb(ib_dev, port, idev, cookie);
  631. if (idev)
  632. dev_put(idev);
  633. }
  634. }
  635. /**
  636. * ib_enum_all_roce_netdevs - enumerate all RoCE devices
  637. * @filter: Should we call the callback?
  638. * @filter_cookie: Cookie passed to filter
  639. * @cb: Callback to call for each found RoCE ports
  640. * @cookie: Cookie passed back to the callback
  641. *
  642. * Enumerates all RoCE devices' physical ports which are related
  643. * to netdevices and calls callback() on each device for which
  644. * filter() function returns non zero.
  645. */
  646. void ib_enum_all_roce_netdevs(roce_netdev_filter filter,
  647. void *filter_cookie,
  648. roce_netdev_callback cb,
  649. void *cookie)
  650. {
  651. struct ib_device *dev;
  652. down_read(&lists_rwsem);
  653. list_for_each_entry(dev, &device_list, core_list)
  654. ib_enum_roce_netdev(dev, filter, filter_cookie, cb, cookie);
  655. up_read(&lists_rwsem);
  656. }
  657. /**
  658. * ib_query_pkey - Get P_Key table entry
  659. * @device:Device to query
  660. * @port_num:Port number to query
  661. * @index:P_Key table index to query
  662. * @pkey:Returned P_Key
  663. *
  664. * ib_query_pkey() fetches the specified P_Key table entry.
  665. */
  666. int ib_query_pkey(struct ib_device *device,
  667. u8 port_num, u16 index, u16 *pkey)
  668. {
  669. return device->query_pkey(device, port_num, index, pkey);
  670. }
  671. EXPORT_SYMBOL(ib_query_pkey);
  672. /**
  673. * ib_modify_device - Change IB device attributes
  674. * @device:Device to modify
  675. * @device_modify_mask:Mask of attributes to change
  676. * @device_modify:New attribute values
  677. *
  678. * ib_modify_device() changes a device's attributes as specified by
  679. * the @device_modify_mask and @device_modify structure.
  680. */
  681. int ib_modify_device(struct ib_device *device,
  682. int device_modify_mask,
  683. struct ib_device_modify *device_modify)
  684. {
  685. if (!device->modify_device)
  686. return -ENOSYS;
  687. return device->modify_device(device, device_modify_mask,
  688. device_modify);
  689. }
  690. EXPORT_SYMBOL(ib_modify_device);
  691. /**
  692. * ib_modify_port - Modifies the attributes for the specified port.
  693. * @device: The device to modify.
  694. * @port_num: The number of the port to modify.
  695. * @port_modify_mask: Mask used to specify which attributes of the port
  696. * to change.
  697. * @port_modify: New attribute values for the port.
  698. *
  699. * ib_modify_port() changes a port's attributes as specified by the
  700. * @port_modify_mask and @port_modify structure.
  701. */
  702. int ib_modify_port(struct ib_device *device,
  703. u8 port_num, int port_modify_mask,
  704. struct ib_port_modify *port_modify)
  705. {
  706. if (!device->modify_port)
  707. return -ENOSYS;
  708. if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
  709. return -EINVAL;
  710. return device->modify_port(device, port_num, port_modify_mask,
  711. port_modify);
  712. }
  713. EXPORT_SYMBOL(ib_modify_port);
  714. /**
  715. * ib_find_gid - Returns the port number and GID table index where
  716. * a specified GID value occurs.
  717. * @device: The device to query.
  718. * @gid: The GID value to search for.
  719. * @gid_type: Type of GID.
  720. * @ndev: The ndev related to the GID to search for.
  721. * @port_num: The port number of the device where the GID value was found.
  722. * @index: The index into the GID table where the GID was found. This
  723. * parameter may be NULL.
  724. */
  725. int ib_find_gid(struct ib_device *device, union ib_gid *gid,
  726. enum ib_gid_type gid_type, struct net_device *ndev,
  727. u8 *port_num, u16 *index)
  728. {
  729. union ib_gid tmp_gid;
  730. int ret, port, i;
  731. for (port = rdma_start_port(device); port <= rdma_end_port(device); ++port) {
  732. if (rdma_cap_roce_gid_table(device, port)) {
  733. if (!ib_find_cached_gid_by_port(device, gid, gid_type, port,
  734. ndev, index)) {
  735. *port_num = port;
  736. return 0;
  737. }
  738. }
  739. if (gid_type != IB_GID_TYPE_IB)
  740. continue;
  741. for (i = 0; i < device->port_immutable[port].gid_tbl_len; ++i) {
  742. ret = ib_query_gid(device, port, i, &tmp_gid, NULL);
  743. if (ret)
  744. return ret;
  745. if (!memcmp(&tmp_gid, gid, sizeof *gid)) {
  746. *port_num = port;
  747. if (index)
  748. *index = i;
  749. return 0;
  750. }
  751. }
  752. }
  753. return -ENOENT;
  754. }
  755. EXPORT_SYMBOL(ib_find_gid);
  756. /**
  757. * ib_find_pkey - Returns the PKey table index where a specified
  758. * PKey value occurs.
  759. * @device: The device to query.
  760. * @port_num: The port number of the device to search for the PKey.
  761. * @pkey: The PKey value to search for.
  762. * @index: The index into the PKey table where the PKey was found.
  763. */
  764. int ib_find_pkey(struct ib_device *device,
  765. u8 port_num, u16 pkey, u16 *index)
  766. {
  767. int ret, i;
  768. u16 tmp_pkey;
  769. int partial_ix = -1;
  770. for (i = 0; i < device->port_immutable[port_num].pkey_tbl_len; ++i) {
  771. ret = ib_query_pkey(device, port_num, i, &tmp_pkey);
  772. if (ret)
  773. return ret;
  774. if ((pkey & 0x7fff) == (tmp_pkey & 0x7fff)) {
  775. /* if there is full-member pkey take it.*/
  776. if (tmp_pkey & 0x8000) {
  777. *index = i;
  778. return 0;
  779. }
  780. if (partial_ix < 0)
  781. partial_ix = i;
  782. }
  783. }
  784. /*no full-member, if exists take the limited*/
  785. if (partial_ix >= 0) {
  786. *index = partial_ix;
  787. return 0;
  788. }
  789. return -ENOENT;
  790. }
  791. EXPORT_SYMBOL(ib_find_pkey);
  792. /**
  793. * ib_get_net_dev_by_params() - Return the appropriate net_dev
  794. * for a received CM request
  795. * @dev: An RDMA device on which the request has been received.
  796. * @port: Port number on the RDMA device.
  797. * @pkey: The Pkey the request came on.
  798. * @gid: A GID that the net_dev uses to communicate.
  799. * @addr: Contains the IP address that the request specified as its
  800. * destination.
  801. */
  802. struct net_device *ib_get_net_dev_by_params(struct ib_device *dev,
  803. u8 port,
  804. u16 pkey,
  805. const union ib_gid *gid,
  806. const struct sockaddr *addr)
  807. {
  808. struct net_device *net_dev = NULL;
  809. struct ib_client_data *context;
  810. if (!rdma_protocol_ib(dev, port))
  811. return NULL;
  812. down_read(&lists_rwsem);
  813. list_for_each_entry(context, &dev->client_data_list, list) {
  814. struct ib_client *client = context->client;
  815. if (context->going_down)
  816. continue;
  817. if (client->get_net_dev_by_params) {
  818. net_dev = client->get_net_dev_by_params(dev, port, pkey,
  819. gid, addr,
  820. context->data);
  821. if (net_dev)
  822. break;
  823. }
  824. }
  825. up_read(&lists_rwsem);
  826. return net_dev;
  827. }
  828. EXPORT_SYMBOL(ib_get_net_dev_by_params);
  829. static struct ibnl_client_cbs ibnl_ls_cb_table[] = {
  830. [RDMA_NL_LS_OP_RESOLVE] = {
  831. .dump = ib_nl_handle_resolve_resp,
  832. .module = THIS_MODULE },
  833. [RDMA_NL_LS_OP_SET_TIMEOUT] = {
  834. .dump = ib_nl_handle_set_timeout,
  835. .module = THIS_MODULE },
  836. [RDMA_NL_LS_OP_IP_RESOLVE] = {
  837. .dump = ib_nl_handle_ip_res_resp,
  838. .module = THIS_MODULE },
  839. };
  840. static int ib_add_ibnl_clients(void)
  841. {
  842. return ibnl_add_client(RDMA_NL_LS, ARRAY_SIZE(ibnl_ls_cb_table),
  843. ibnl_ls_cb_table);
  844. }
  845. static void ib_remove_ibnl_clients(void)
  846. {
  847. ibnl_remove_client(RDMA_NL_LS);
  848. }
  849. static int __init ib_core_init(void)
  850. {
  851. int ret;
  852. ib_wq = alloc_workqueue("infiniband", 0, 0);
  853. if (!ib_wq)
  854. return -ENOMEM;
  855. ib_comp_wq = alloc_workqueue("ib-comp-wq",
  856. WQ_UNBOUND | WQ_HIGHPRI | WQ_MEM_RECLAIM,
  857. WQ_UNBOUND_MAX_ACTIVE);
  858. if (!ib_comp_wq) {
  859. ret = -ENOMEM;
  860. goto err;
  861. }
  862. ret = class_register(&ib_class);
  863. if (ret) {
  864. pr_warn("Couldn't create InfiniBand device class\n");
  865. goto err_comp;
  866. }
  867. ret = ibnl_init();
  868. if (ret) {
  869. pr_warn("Couldn't init IB netlink interface\n");
  870. goto err_sysfs;
  871. }
  872. ret = addr_init();
  873. if (ret) {
  874. pr_warn("Could't init IB address resolution\n");
  875. goto err_ibnl;
  876. }
  877. ret = ib_mad_init();
  878. if (ret) {
  879. pr_warn("Couldn't init IB MAD\n");
  880. goto err_addr;
  881. }
  882. ret = ib_sa_init();
  883. if (ret) {
  884. pr_warn("Couldn't init SA\n");
  885. goto err_mad;
  886. }
  887. ret = ib_add_ibnl_clients();
  888. if (ret) {
  889. pr_warn("Couldn't register ibnl clients\n");
  890. goto err_sa;
  891. }
  892. ib_cache_setup();
  893. return 0;
  894. err_sa:
  895. ib_sa_cleanup();
  896. err_mad:
  897. ib_mad_cleanup();
  898. err_addr:
  899. addr_cleanup();
  900. err_ibnl:
  901. ibnl_cleanup();
  902. err_sysfs:
  903. class_unregister(&ib_class);
  904. err_comp:
  905. destroy_workqueue(ib_comp_wq);
  906. err:
  907. destroy_workqueue(ib_wq);
  908. return ret;
  909. }
  910. static void __exit ib_core_cleanup(void)
  911. {
  912. ib_cache_cleanup();
  913. ib_remove_ibnl_clients();
  914. ib_sa_cleanup();
  915. ib_mad_cleanup();
  916. addr_cleanup();
  917. ibnl_cleanup();
  918. class_unregister(&ib_class);
  919. destroy_workqueue(ib_comp_wq);
  920. /* Make sure that any pending umem accounting work is done. */
  921. destroy_workqueue(ib_wq);
  922. }
  923. module_init(ib_core_init);
  924. module_exit(ib_core_cleanup);