device.c 32 KB

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