device.c 32 KB

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