device.c 33 KB

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