device.c 33 KB

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