device.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  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. pr_warn("Couldn't allocate client context for %s/%s\n",
  221. device->name, client->name);
  222. return -ENOMEM;
  223. }
  224. context->client = client;
  225. context->data = NULL;
  226. context->going_down = false;
  227. down_write(&lists_rwsem);
  228. spin_lock_irqsave(&device->client_data_lock, flags);
  229. list_add(&context->list, &device->client_data_list);
  230. spin_unlock_irqrestore(&device->client_data_lock, flags);
  231. up_write(&lists_rwsem);
  232. return 0;
  233. }
  234. static int verify_immutable(const struct ib_device *dev, u8 port)
  235. {
  236. return WARN_ON(!rdma_cap_ib_mad(dev, port) &&
  237. rdma_max_mad_size(dev, port) != 0);
  238. }
  239. static int read_port_immutable(struct ib_device *device)
  240. {
  241. int ret;
  242. u8 start_port = rdma_start_port(device);
  243. u8 end_port = rdma_end_port(device);
  244. u8 port;
  245. /**
  246. * device->port_immutable is indexed directly by the port number to make
  247. * access to this data as efficient as possible.
  248. *
  249. * Therefore port_immutable is declared as a 1 based array with
  250. * potential empty slots at the beginning.
  251. */
  252. device->port_immutable = kzalloc(sizeof(*device->port_immutable)
  253. * (end_port + 1),
  254. GFP_KERNEL);
  255. if (!device->port_immutable)
  256. return -ENOMEM;
  257. for (port = start_port; port <= end_port; ++port) {
  258. ret = device->get_port_immutable(device, port,
  259. &device->port_immutable[port]);
  260. if (ret)
  261. return ret;
  262. if (verify_immutable(device, port))
  263. return -EINVAL;
  264. }
  265. return 0;
  266. }
  267. void ib_get_device_fw_str(struct ib_device *dev, char *str, size_t str_len)
  268. {
  269. if (dev->get_dev_fw_str)
  270. dev->get_dev_fw_str(dev, str, str_len);
  271. else
  272. str[0] = '\0';
  273. }
  274. EXPORT_SYMBOL(ib_get_device_fw_str);
  275. /**
  276. * ib_register_device - Register an IB device with IB core
  277. * @device:Device to register
  278. *
  279. * Low-level drivers use ib_register_device() to register their
  280. * devices with the IB core. All registered clients will receive a
  281. * callback for each device that is added. @device must be allocated
  282. * with ib_alloc_device().
  283. */
  284. int ib_register_device(struct ib_device *device,
  285. int (*port_callback)(struct ib_device *,
  286. u8, struct kobject *))
  287. {
  288. int ret;
  289. struct ib_client *client;
  290. struct ib_udata uhw = {.outlen = 0, .inlen = 0};
  291. mutex_lock(&device_mutex);
  292. if (strchr(device->name, '%')) {
  293. ret = alloc_name(device->name);
  294. if (ret)
  295. goto out;
  296. }
  297. if (ib_device_check_mandatory(device)) {
  298. ret = -EINVAL;
  299. goto out;
  300. }
  301. ret = read_port_immutable(device);
  302. if (ret) {
  303. pr_warn("Couldn't create per port immutable data %s\n",
  304. device->name);
  305. goto out;
  306. }
  307. ret = ib_cache_setup_one(device);
  308. if (ret) {
  309. pr_warn("Couldn't set up InfiniBand P_Key/GID cache\n");
  310. goto out;
  311. }
  312. memset(&device->attrs, 0, sizeof(device->attrs));
  313. ret = device->query_device(device, &device->attrs, &uhw);
  314. if (ret) {
  315. pr_warn("Couldn't query the device attributes\n");
  316. ib_cache_cleanup_one(device);
  317. goto out;
  318. }
  319. ret = ib_device_register_sysfs(device, port_callback);
  320. if (ret) {
  321. pr_warn("Couldn't register device %s with driver model\n",
  322. device->name);
  323. ib_cache_cleanup_one(device);
  324. goto out;
  325. }
  326. device->reg_state = IB_DEV_REGISTERED;
  327. list_for_each_entry(client, &client_list, list)
  328. if (client->add && !add_client_context(device, client))
  329. client->add(device);
  330. down_write(&lists_rwsem);
  331. list_add_tail(&device->core_list, &device_list);
  332. up_write(&lists_rwsem);
  333. out:
  334. mutex_unlock(&device_mutex);
  335. return ret;
  336. }
  337. EXPORT_SYMBOL(ib_register_device);
  338. /**
  339. * ib_unregister_device - Unregister an IB device
  340. * @device:Device to unregister
  341. *
  342. * Unregister an IB device. All clients will receive a remove callback.
  343. */
  344. void ib_unregister_device(struct ib_device *device)
  345. {
  346. struct ib_client_data *context, *tmp;
  347. unsigned long flags;
  348. mutex_lock(&device_mutex);
  349. down_write(&lists_rwsem);
  350. list_del(&device->core_list);
  351. spin_lock_irqsave(&device->client_data_lock, flags);
  352. list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
  353. context->going_down = true;
  354. spin_unlock_irqrestore(&device->client_data_lock, flags);
  355. downgrade_write(&lists_rwsem);
  356. list_for_each_entry_safe(context, tmp, &device->client_data_list,
  357. list) {
  358. if (context->client->remove)
  359. context->client->remove(device, context->data);
  360. }
  361. up_read(&lists_rwsem);
  362. mutex_unlock(&device_mutex);
  363. ib_device_unregister_sysfs(device);
  364. ib_cache_cleanup_one(device);
  365. down_write(&lists_rwsem);
  366. spin_lock_irqsave(&device->client_data_lock, flags);
  367. list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
  368. kfree(context);
  369. spin_unlock_irqrestore(&device->client_data_lock, flags);
  370. up_write(&lists_rwsem);
  371. device->reg_state = IB_DEV_UNREGISTERED;
  372. }
  373. EXPORT_SYMBOL(ib_unregister_device);
  374. /**
  375. * ib_register_client - Register an IB client
  376. * @client:Client to register
  377. *
  378. * Upper level users of the IB drivers can use ib_register_client() to
  379. * register callbacks for IB device addition and removal. When an IB
  380. * device is added, each registered client's add method will be called
  381. * (in the order the clients were registered), and when a device is
  382. * removed, each client's remove method will be called (in the reverse
  383. * order that clients were registered). In addition, when
  384. * ib_register_client() is called, the client will receive an add
  385. * callback for all devices already registered.
  386. */
  387. int ib_register_client(struct ib_client *client)
  388. {
  389. struct ib_device *device;
  390. mutex_lock(&device_mutex);
  391. list_for_each_entry(device, &device_list, core_list)
  392. if (client->add && !add_client_context(device, client))
  393. client->add(device);
  394. down_write(&lists_rwsem);
  395. list_add_tail(&client->list, &client_list);
  396. up_write(&lists_rwsem);
  397. mutex_unlock(&device_mutex);
  398. return 0;
  399. }
  400. EXPORT_SYMBOL(ib_register_client);
  401. /**
  402. * ib_unregister_client - Unregister an IB client
  403. * @client:Client to unregister
  404. *
  405. * Upper level users use ib_unregister_client() to remove their client
  406. * registration. When ib_unregister_client() is called, the client
  407. * will receive a remove callback for each IB device still registered.
  408. */
  409. void ib_unregister_client(struct ib_client *client)
  410. {
  411. struct ib_client_data *context, *tmp;
  412. struct ib_device *device;
  413. unsigned long flags;
  414. mutex_lock(&device_mutex);
  415. down_write(&lists_rwsem);
  416. list_del(&client->list);
  417. up_write(&lists_rwsem);
  418. list_for_each_entry(device, &device_list, core_list) {
  419. struct ib_client_data *found_context = NULL;
  420. down_write(&lists_rwsem);
  421. spin_lock_irqsave(&device->client_data_lock, flags);
  422. list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
  423. if (context->client == client) {
  424. context->going_down = true;
  425. found_context = context;
  426. break;
  427. }
  428. spin_unlock_irqrestore(&device->client_data_lock, flags);
  429. up_write(&lists_rwsem);
  430. if (client->remove)
  431. client->remove(device, found_context ?
  432. found_context->data : NULL);
  433. if (!found_context) {
  434. pr_warn("No client context found for %s/%s\n",
  435. device->name, client->name);
  436. continue;
  437. }
  438. down_write(&lists_rwsem);
  439. spin_lock_irqsave(&device->client_data_lock, flags);
  440. list_del(&found_context->list);
  441. kfree(found_context);
  442. spin_unlock_irqrestore(&device->client_data_lock, flags);
  443. up_write(&lists_rwsem);
  444. }
  445. mutex_unlock(&device_mutex);
  446. }
  447. EXPORT_SYMBOL(ib_unregister_client);
  448. /**
  449. * ib_get_client_data - Get IB client context
  450. * @device:Device to get context for
  451. * @client:Client to get context for
  452. *
  453. * ib_get_client_data() returns client context set with
  454. * ib_set_client_data().
  455. */
  456. void *ib_get_client_data(struct ib_device *device, struct ib_client *client)
  457. {
  458. struct ib_client_data *context;
  459. void *ret = NULL;
  460. unsigned long flags;
  461. spin_lock_irqsave(&device->client_data_lock, flags);
  462. list_for_each_entry(context, &device->client_data_list, list)
  463. if (context->client == client) {
  464. ret = context->data;
  465. break;
  466. }
  467. spin_unlock_irqrestore(&device->client_data_lock, flags);
  468. return ret;
  469. }
  470. EXPORT_SYMBOL(ib_get_client_data);
  471. /**
  472. * ib_set_client_data - Set IB client context
  473. * @device:Device to set context for
  474. * @client:Client to set context for
  475. * @data:Context to set
  476. *
  477. * ib_set_client_data() sets client context that can be retrieved with
  478. * ib_get_client_data().
  479. */
  480. void ib_set_client_data(struct ib_device *device, struct ib_client *client,
  481. void *data)
  482. {
  483. struct ib_client_data *context;
  484. unsigned long flags;
  485. spin_lock_irqsave(&device->client_data_lock, flags);
  486. list_for_each_entry(context, &device->client_data_list, list)
  487. if (context->client == client) {
  488. context->data = data;
  489. goto out;
  490. }
  491. pr_warn("No client context found for %s/%s\n",
  492. device->name, client->name);
  493. out:
  494. spin_unlock_irqrestore(&device->client_data_lock, flags);
  495. }
  496. EXPORT_SYMBOL(ib_set_client_data);
  497. /**
  498. * ib_register_event_handler - Register an IB event handler
  499. * @event_handler:Handler to register
  500. *
  501. * ib_register_event_handler() registers an event handler that will be
  502. * called back when asynchronous IB events occur (as defined in
  503. * chapter 11 of the InfiniBand Architecture Specification). This
  504. * callback may occur in interrupt context.
  505. */
  506. int ib_register_event_handler (struct ib_event_handler *event_handler)
  507. {
  508. unsigned long flags;
  509. spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
  510. list_add_tail(&event_handler->list,
  511. &event_handler->device->event_handler_list);
  512. spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
  513. return 0;
  514. }
  515. EXPORT_SYMBOL(ib_register_event_handler);
  516. /**
  517. * ib_unregister_event_handler - Unregister an event handler
  518. * @event_handler:Handler to unregister
  519. *
  520. * Unregister an event handler registered with
  521. * ib_register_event_handler().
  522. */
  523. int ib_unregister_event_handler(struct ib_event_handler *event_handler)
  524. {
  525. unsigned long flags;
  526. spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
  527. list_del(&event_handler->list);
  528. spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
  529. return 0;
  530. }
  531. EXPORT_SYMBOL(ib_unregister_event_handler);
  532. /**
  533. * ib_dispatch_event - Dispatch an asynchronous event
  534. * @event:Event to dispatch
  535. *
  536. * Low-level drivers must call ib_dispatch_event() to dispatch the
  537. * event to all registered event handlers when an asynchronous event
  538. * occurs.
  539. */
  540. void ib_dispatch_event(struct ib_event *event)
  541. {
  542. unsigned long flags;
  543. struct ib_event_handler *handler;
  544. spin_lock_irqsave(&event->device->event_handler_lock, flags);
  545. list_for_each_entry(handler, &event->device->event_handler_list, list)
  546. handler->handler(handler, event);
  547. spin_unlock_irqrestore(&event->device->event_handler_lock, flags);
  548. }
  549. EXPORT_SYMBOL(ib_dispatch_event);
  550. /**
  551. * ib_query_port - Query IB port attributes
  552. * @device:Device to query
  553. * @port_num:Port number to query
  554. * @port_attr:Port attributes
  555. *
  556. * ib_query_port() returns the attributes of a port through the
  557. * @port_attr pointer.
  558. */
  559. int ib_query_port(struct ib_device *device,
  560. u8 port_num,
  561. struct ib_port_attr *port_attr)
  562. {
  563. union ib_gid gid;
  564. int err;
  565. if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
  566. return -EINVAL;
  567. memset(port_attr, 0, sizeof(*port_attr));
  568. err = device->query_port(device, port_num, port_attr);
  569. if (err || port_attr->subnet_prefix)
  570. return err;
  571. if (rdma_port_get_link_layer(device, port_num) != IB_LINK_LAYER_INFINIBAND)
  572. return 0;
  573. err = ib_query_gid(device, port_num, 0, &gid, NULL);
  574. if (err)
  575. return err;
  576. port_attr->subnet_prefix = be64_to_cpu(gid.global.subnet_prefix);
  577. return 0;
  578. }
  579. EXPORT_SYMBOL(ib_query_port);
  580. /**
  581. * ib_query_gid - Get GID table entry
  582. * @device:Device to query
  583. * @port_num:Port number to query
  584. * @index:GID table index to query
  585. * @gid:Returned GID
  586. * @attr: Returned GID attributes related to this GID index (only in RoCE).
  587. * NULL means ignore.
  588. *
  589. * ib_query_gid() fetches the specified GID table entry.
  590. */
  591. int ib_query_gid(struct ib_device *device,
  592. u8 port_num, int index, union ib_gid *gid,
  593. struct ib_gid_attr *attr)
  594. {
  595. if (rdma_cap_roce_gid_table(device, port_num))
  596. return ib_get_cached_gid(device, port_num, index, gid, attr);
  597. if (attr)
  598. return -EINVAL;
  599. return device->query_gid(device, port_num, index, gid);
  600. }
  601. EXPORT_SYMBOL(ib_query_gid);
  602. /**
  603. * ib_enum_roce_netdev - enumerate all RoCE ports
  604. * @ib_dev : IB device we want to query
  605. * @filter: Should we call the callback?
  606. * @filter_cookie: Cookie passed to filter
  607. * @cb: Callback to call for each found RoCE ports
  608. * @cookie: Cookie passed back to the callback
  609. *
  610. * Enumerates all of the physical RoCE ports of ib_dev
  611. * which are related to netdevice and calls callback() on each
  612. * device for which filter() function returns non zero.
  613. */
  614. void ib_enum_roce_netdev(struct ib_device *ib_dev,
  615. roce_netdev_filter filter,
  616. void *filter_cookie,
  617. roce_netdev_callback cb,
  618. void *cookie)
  619. {
  620. u8 port;
  621. for (port = rdma_start_port(ib_dev); port <= rdma_end_port(ib_dev);
  622. port++)
  623. if (rdma_protocol_roce(ib_dev, port)) {
  624. struct net_device *idev = NULL;
  625. if (ib_dev->get_netdev)
  626. idev = ib_dev->get_netdev(ib_dev, port);
  627. if (idev &&
  628. idev->reg_state >= NETREG_UNREGISTERED) {
  629. dev_put(idev);
  630. idev = NULL;
  631. }
  632. if (filter(ib_dev, port, idev, filter_cookie))
  633. cb(ib_dev, port, idev, cookie);
  634. if (idev)
  635. dev_put(idev);
  636. }
  637. }
  638. /**
  639. * ib_enum_all_roce_netdevs - enumerate all RoCE devices
  640. * @filter: Should we call the callback?
  641. * @filter_cookie: Cookie passed to filter
  642. * @cb: Callback to call for each found RoCE ports
  643. * @cookie: Cookie passed back to the callback
  644. *
  645. * Enumerates all RoCE devices' physical ports which are related
  646. * to netdevices and calls callback() on each device for which
  647. * filter() function returns non zero.
  648. */
  649. void ib_enum_all_roce_netdevs(roce_netdev_filter filter,
  650. void *filter_cookie,
  651. roce_netdev_callback cb,
  652. void *cookie)
  653. {
  654. struct ib_device *dev;
  655. down_read(&lists_rwsem);
  656. list_for_each_entry(dev, &device_list, core_list)
  657. ib_enum_roce_netdev(dev, filter, filter_cookie, cb, cookie);
  658. up_read(&lists_rwsem);
  659. }
  660. /**
  661. * ib_query_pkey - Get P_Key table entry
  662. * @device:Device to query
  663. * @port_num:Port number to query
  664. * @index:P_Key table index to query
  665. * @pkey:Returned P_Key
  666. *
  667. * ib_query_pkey() fetches the specified P_Key table entry.
  668. */
  669. int ib_query_pkey(struct ib_device *device,
  670. u8 port_num, u16 index, u16 *pkey)
  671. {
  672. return device->query_pkey(device, port_num, index, pkey);
  673. }
  674. EXPORT_SYMBOL(ib_query_pkey);
  675. /**
  676. * ib_modify_device - Change IB device attributes
  677. * @device:Device to modify
  678. * @device_modify_mask:Mask of attributes to change
  679. * @device_modify:New attribute values
  680. *
  681. * ib_modify_device() changes a device's attributes as specified by
  682. * the @device_modify_mask and @device_modify structure.
  683. */
  684. int ib_modify_device(struct ib_device *device,
  685. int device_modify_mask,
  686. struct ib_device_modify *device_modify)
  687. {
  688. if (!device->modify_device)
  689. return -ENOSYS;
  690. return device->modify_device(device, device_modify_mask,
  691. device_modify);
  692. }
  693. EXPORT_SYMBOL(ib_modify_device);
  694. /**
  695. * ib_modify_port - Modifies the attributes for the specified port.
  696. * @device: The device to modify.
  697. * @port_num: The number of the port to modify.
  698. * @port_modify_mask: Mask used to specify which attributes of the port
  699. * to change.
  700. * @port_modify: New attribute values for the port.
  701. *
  702. * ib_modify_port() changes a port's attributes as specified by the
  703. * @port_modify_mask and @port_modify structure.
  704. */
  705. int ib_modify_port(struct ib_device *device,
  706. u8 port_num, int port_modify_mask,
  707. struct ib_port_modify *port_modify)
  708. {
  709. if (!device->modify_port)
  710. return -ENOSYS;
  711. if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
  712. return -EINVAL;
  713. return device->modify_port(device, port_num, port_modify_mask,
  714. port_modify);
  715. }
  716. EXPORT_SYMBOL(ib_modify_port);
  717. /**
  718. * ib_find_gid - Returns the port number and GID table index where
  719. * a specified GID value occurs.
  720. * @device: The device to query.
  721. * @gid: The GID value to search for.
  722. * @gid_type: Type of GID.
  723. * @ndev: The ndev related to the GID to search for.
  724. * @port_num: The port number of the device where the GID value was found.
  725. * @index: The index into the GID table where the GID was found. This
  726. * parameter may be NULL.
  727. */
  728. int ib_find_gid(struct ib_device *device, union ib_gid *gid,
  729. enum ib_gid_type gid_type, struct net_device *ndev,
  730. u8 *port_num, u16 *index)
  731. {
  732. union ib_gid tmp_gid;
  733. int ret, port, i;
  734. for (port = rdma_start_port(device); port <= rdma_end_port(device); ++port) {
  735. if (rdma_cap_roce_gid_table(device, port)) {
  736. if (!ib_find_cached_gid_by_port(device, gid, gid_type, port,
  737. ndev, index)) {
  738. *port_num = port;
  739. return 0;
  740. }
  741. }
  742. if (gid_type != IB_GID_TYPE_IB)
  743. continue;
  744. for (i = 0; i < device->port_immutable[port].gid_tbl_len; ++i) {
  745. ret = ib_query_gid(device, port, i, &tmp_gid, NULL);
  746. if (ret)
  747. return ret;
  748. if (!memcmp(&tmp_gid, gid, sizeof *gid)) {
  749. *port_num = port;
  750. if (index)
  751. *index = i;
  752. return 0;
  753. }
  754. }
  755. }
  756. return -ENOENT;
  757. }
  758. EXPORT_SYMBOL(ib_find_gid);
  759. /**
  760. * ib_find_pkey - Returns the PKey table index where a specified
  761. * PKey value occurs.
  762. * @device: The device to query.
  763. * @port_num: The port number of the device to search for the PKey.
  764. * @pkey: The PKey value to search for.
  765. * @index: The index into the PKey table where the PKey was found.
  766. */
  767. int ib_find_pkey(struct ib_device *device,
  768. u8 port_num, u16 pkey, u16 *index)
  769. {
  770. int ret, i;
  771. u16 tmp_pkey;
  772. int partial_ix = -1;
  773. for (i = 0; i < device->port_immutable[port_num].pkey_tbl_len; ++i) {
  774. ret = ib_query_pkey(device, port_num, i, &tmp_pkey);
  775. if (ret)
  776. return ret;
  777. if ((pkey & 0x7fff) == (tmp_pkey & 0x7fff)) {
  778. /* if there is full-member pkey take it.*/
  779. if (tmp_pkey & 0x8000) {
  780. *index = i;
  781. return 0;
  782. }
  783. if (partial_ix < 0)
  784. partial_ix = i;
  785. }
  786. }
  787. /*no full-member, if exists take the limited*/
  788. if (partial_ix >= 0) {
  789. *index = partial_ix;
  790. return 0;
  791. }
  792. return -ENOENT;
  793. }
  794. EXPORT_SYMBOL(ib_find_pkey);
  795. /**
  796. * ib_get_net_dev_by_params() - Return the appropriate net_dev
  797. * for a received CM request
  798. * @dev: An RDMA device on which the request has been received.
  799. * @port: Port number on the RDMA device.
  800. * @pkey: The Pkey the request came on.
  801. * @gid: A GID that the net_dev uses to communicate.
  802. * @addr: Contains the IP address that the request specified as its
  803. * destination.
  804. */
  805. struct net_device *ib_get_net_dev_by_params(struct ib_device *dev,
  806. u8 port,
  807. u16 pkey,
  808. const union ib_gid *gid,
  809. const struct sockaddr *addr)
  810. {
  811. struct net_device *net_dev = NULL;
  812. struct ib_client_data *context;
  813. if (!rdma_protocol_ib(dev, port))
  814. return NULL;
  815. down_read(&lists_rwsem);
  816. list_for_each_entry(context, &dev->client_data_list, list) {
  817. struct ib_client *client = context->client;
  818. if (context->going_down)
  819. continue;
  820. if (client->get_net_dev_by_params) {
  821. net_dev = client->get_net_dev_by_params(dev, port, pkey,
  822. gid, addr,
  823. context->data);
  824. if (net_dev)
  825. break;
  826. }
  827. }
  828. up_read(&lists_rwsem);
  829. return net_dev;
  830. }
  831. EXPORT_SYMBOL(ib_get_net_dev_by_params);
  832. static struct ibnl_client_cbs ibnl_ls_cb_table[] = {
  833. [RDMA_NL_LS_OP_RESOLVE] = {
  834. .dump = ib_nl_handle_resolve_resp,
  835. .module = THIS_MODULE },
  836. [RDMA_NL_LS_OP_SET_TIMEOUT] = {
  837. .dump = ib_nl_handle_set_timeout,
  838. .module = THIS_MODULE },
  839. [RDMA_NL_LS_OP_IP_RESOLVE] = {
  840. .dump = ib_nl_handle_ip_res_resp,
  841. .module = THIS_MODULE },
  842. };
  843. static int ib_add_ibnl_clients(void)
  844. {
  845. return ibnl_add_client(RDMA_NL_LS, ARRAY_SIZE(ibnl_ls_cb_table),
  846. ibnl_ls_cb_table);
  847. }
  848. static void ib_remove_ibnl_clients(void)
  849. {
  850. ibnl_remove_client(RDMA_NL_LS);
  851. }
  852. static int __init ib_core_init(void)
  853. {
  854. int ret;
  855. ib_wq = alloc_workqueue("infiniband", 0, 0);
  856. if (!ib_wq)
  857. return -ENOMEM;
  858. ib_comp_wq = alloc_workqueue("ib-comp-wq",
  859. WQ_UNBOUND | WQ_HIGHPRI | WQ_MEM_RECLAIM,
  860. WQ_UNBOUND_MAX_ACTIVE);
  861. if (!ib_comp_wq) {
  862. ret = -ENOMEM;
  863. goto err;
  864. }
  865. ret = class_register(&ib_class);
  866. if (ret) {
  867. pr_warn("Couldn't create InfiniBand device class\n");
  868. goto err_comp;
  869. }
  870. ret = ibnl_init();
  871. if (ret) {
  872. pr_warn("Couldn't init IB netlink interface\n");
  873. goto err_sysfs;
  874. }
  875. ret = addr_init();
  876. if (ret) {
  877. pr_warn("Could't init IB address resolution\n");
  878. goto err_ibnl;
  879. }
  880. ret = ib_mad_init();
  881. if (ret) {
  882. pr_warn("Couldn't init IB MAD\n");
  883. goto err_addr;
  884. }
  885. ret = ib_sa_init();
  886. if (ret) {
  887. pr_warn("Couldn't init SA\n");
  888. goto err_mad;
  889. }
  890. ret = ib_add_ibnl_clients();
  891. if (ret) {
  892. pr_warn("Couldn't register ibnl clients\n");
  893. goto err_sa;
  894. }
  895. ib_cache_setup();
  896. return 0;
  897. err_sa:
  898. ib_sa_cleanup();
  899. err_mad:
  900. ib_mad_cleanup();
  901. err_addr:
  902. addr_cleanup();
  903. err_ibnl:
  904. ibnl_cleanup();
  905. err_sysfs:
  906. class_unregister(&ib_class);
  907. err_comp:
  908. destroy_workqueue(ib_comp_wq);
  909. err:
  910. destroy_workqueue(ib_wq);
  911. return ret;
  912. }
  913. static void __exit ib_core_cleanup(void)
  914. {
  915. ib_cache_cleanup();
  916. ib_remove_ibnl_clients();
  917. ib_sa_cleanup();
  918. ib_mad_cleanup();
  919. addr_cleanup();
  920. ibnl_cleanup();
  921. class_unregister(&ib_class);
  922. destroy_workqueue(ib_comp_wq);
  923. /* Make sure that any pending umem accounting work is done. */
  924. destroy_workqueue(ib_wq);
  925. }
  926. module_init(ib_core_init);
  927. module_exit(ib_core_cleanup);